Fix T59922: crash reading files with older renderlayers
[blender.git] / source / blender / nodes / composite / nodes / node_composite_image.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2006 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/nodes/composite/nodes/node_composite_image.c
29  *  \ingroup cmpnodes
30  */
31
32 #include "node_composite_util.h"
33
34 #include "BLI_utildefines.h"
35 #include "BLI_linklist.h"
36
37 #include "DNA_scene_types.h"
38
39 #include "RE_engine.h"
40
41 #include "BKE_context.h"
42 #include "BKE_global.h"
43 #include "BKE_main.h"
44 #include "BKE_scene.h"
45
46 /* **************** IMAGE (and RenderResult, multilayer image) ******************** */
47
48 static bNodeSocketTemplate cmp_node_rlayers_out[] = {
49         {       SOCK_RGBA,   0, N_("Image"),                                                    0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
50         {       SOCK_FLOAT,  0, N_("Alpha"),                                                    1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
51         {       SOCK_FLOAT,  0, N_(RE_PASSNAME_Z),                                              1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
52         {       SOCK_VECTOR, 0, N_(RE_PASSNAME_NORMAL),                                 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
53         {       SOCK_VECTOR, 0, N_(RE_PASSNAME_UV),                                             1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
54         {       SOCK_VECTOR, 0, N_(RE_PASSNAME_VECTOR),                                 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
55         {       SOCK_RGBA,   0, N_(RE_PASSNAME_SHADOW),                                 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
56         {       SOCK_RGBA,   0, N_(RE_PASSNAME_AO),                                             0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
57         {       SOCK_FLOAT,  0, N_(RE_PASSNAME_INDEXOB),                                0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
58         {       SOCK_FLOAT,  0, N_(RE_PASSNAME_INDEXMA),                                0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
59         {       SOCK_FLOAT,  0, N_(RE_PASSNAME_MIST),                                   0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
60         {       SOCK_RGBA,   0, N_(RE_PASSNAME_EMIT),                                   0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
61         {       SOCK_RGBA,   0, N_(RE_PASSNAME_ENVIRONMENT),                    0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
62         {       SOCK_RGBA,   0, N_(RE_PASSNAME_DIFFUSE_DIRECT),                 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
63         {       SOCK_RGBA,   0, N_(RE_PASSNAME_DIFFUSE_INDIRECT),               0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
64         {       SOCK_RGBA,   0, N_(RE_PASSNAME_DIFFUSE_COLOR),                  0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
65         {       SOCK_RGBA,   0, N_(RE_PASSNAME_GLOSSY_DIRECT),                  0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
66         {       SOCK_RGBA,   0, N_(RE_PASSNAME_GLOSSY_INDIRECT),                0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
67         {       SOCK_RGBA,   0, N_(RE_PASSNAME_GLOSSY_COLOR),                   0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
68         {       SOCK_RGBA,   0, N_(RE_PASSNAME_TRANSM_DIRECT),                  0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
69         {       SOCK_RGBA,   0, N_(RE_PASSNAME_TRANSM_INDIRECT),                0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
70         {       SOCK_RGBA,   0, N_(RE_PASSNAME_TRANSM_COLOR),                   0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
71         {       SOCK_RGBA,   0, N_(RE_PASSNAME_SUBSURFACE_DIRECT),              0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
72         {       SOCK_RGBA,   0, N_(RE_PASSNAME_SUBSURFACE_INDIRECT),    0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
73         {       SOCK_RGBA,   0, N_(RE_PASSNAME_SUBSURFACE_COLOR),               0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
74         {       -1, 0, ""       }
75 };
76
77 static void cmp_node_image_add_pass_output(bNodeTree *ntree, bNode *node,
78                                            const char *name, const char *passname,
79                                            int rres_index, int type, int is_rlayers,
80                                            LinkNodePair *available_sockets, int *prev_index)
81 {
82         bNodeSocket *sock;
83         int sock_index = BLI_findstringindex(&node->outputs, name, offsetof(bNodeSocket, name));
84
85         if (sock_index < 0) {
86                 /* The first 31 sockets always are the legacy hardcoded sockets.
87                  * Any dynamically allocated sockets follow afterwards, and are sorted in the order in which they were stored in the RenderResult.
88                  * Therefore, we remember the index of the last matched socket. New sockets are placed behind the previously traversed one, but always after the first 31. */
89                 int after_index = *prev_index;
90                 if (is_rlayers && after_index < 30)
91                         after_index = 30;
92
93                 if (rres_index >= 0) {
94                         sock = node_add_socket_from_template(ntree, node, &cmp_node_rlayers_out[rres_index], SOCK_OUT);
95                 }
96                 else {
97                         sock = nodeAddStaticSocket(ntree, node, SOCK_OUT, type, PROP_NONE, name, name);
98                 }
99                 /* extra socket info */
100                 NodeImageLayer *sockdata = MEM_callocN(sizeof(NodeImageLayer), "node image layer");
101                 sock->storage = sockdata;
102
103                 BLI_strncpy(sockdata->pass_name, passname, sizeof(sockdata->pass_name));
104
105                 sock_index = BLI_listbase_count(&node->outputs) - 1;
106                 if (sock_index != after_index + 1) {
107                         bNodeSocket *after_sock = BLI_findlink(&node->outputs, after_index);
108                         BLI_remlink(&node->outputs, sock);
109                         BLI_insertlinkafter(&node->outputs, after_sock, sock);
110                 }
111         }
112         else {
113                 sock = BLI_findlink(&node->outputs, sock_index);
114                 NodeImageLayer *sockdata = sock->storage;
115                 if (sockdata) {
116                         BLI_strncpy(sockdata->pass_name, passname, sizeof(sockdata->pass_name));
117                 }
118         }
119
120         BLI_linklist_append(available_sockets, sock);
121         *prev_index = sock_index;
122 }
123
124 static void cmp_node_image_create_outputs(bNodeTree *ntree, bNode *node, LinkNodePair *available_sockets)
125 {
126         Image *ima = (Image *)node->id;
127         ImBuf *ibuf;
128         int prev_index = -1;
129         if (ima) {
130                 ImageUser *iuser = node->storage;
131                 ImageUser load_iuser = {NULL};
132                 int offset = BKE_image_sequence_guess_offset(ima);
133
134                 /* It is possible that image user in this node is not
135                  * properly updated yet. In this case loading image will
136                  * fail and sockets detection will go wrong.
137                  *
138                  * So we manually construct image user to be sure first
139                  * image from sequence (that one which is set as filename
140                  * for image datablock) is used for sockets detection
141                  */
142                 load_iuser.ok = 1;
143                 load_iuser.framenr = offset;
144
145                 /* make sure ima->type is correct */
146                 ibuf = BKE_image_acquire_ibuf(ima, &load_iuser, NULL);
147
148                 if (ima->rr) {
149                         RenderLayer *rl = BLI_findlink(&ima->rr->layers, iuser->layer);
150
151                         if (rl) {
152                                 RenderPass *rpass;
153                                 for (rpass = rl->passes.first; rpass; rpass = rpass->next) {
154                                         int type;
155                                         if (rpass->channels == 1)
156                                                 type = SOCK_FLOAT;
157                                         else
158                                                 type = SOCK_RGBA;
159
160                                         cmp_node_image_add_pass_output(ntree, node, rpass->name, rpass->name, -1, type, false, available_sockets, &prev_index);
161                                         /* Special handling for the Combined pass to ensure compatibility. */
162                                         if (STREQ(rpass->name, RE_PASSNAME_COMBINED)) {
163                                                 cmp_node_image_add_pass_output(ntree, node, "Alpha", rpass->name, -1, SOCK_FLOAT, false, available_sockets, &prev_index);
164                                         }
165                                 }
166                                 BKE_image_release_ibuf(ima, ibuf, NULL);
167                                 return;
168                         }
169                 }
170         }
171
172         cmp_node_image_add_pass_output(ntree, node, "Image", RE_PASSNAME_COMBINED, -1, SOCK_RGBA, false, available_sockets, &prev_index);
173         cmp_node_image_add_pass_output(ntree, node, "Alpha", RE_PASSNAME_COMBINED, -1, SOCK_FLOAT, false, available_sockets, &prev_index);
174
175         if (ima) {
176                 if (!ima->rr) {
177                         cmp_node_image_add_pass_output(ntree, node, RE_PASSNAME_Z, RE_PASSNAME_Z, -1, SOCK_FLOAT, false, available_sockets, &prev_index);
178                 }
179                 BKE_image_release_ibuf(ima, ibuf, NULL);
180         }
181 }
182
183 typedef struct RLayerUpdateData {
184         LinkNodePair *available_sockets;
185         int prev_index;
186 } RLayerUpdateData;
187
188 void node_cmp_rlayers_register_pass(bNodeTree *ntree, bNode *node, Scene *scene, ViewLayer *view_layer, const char *name, int type)
189 {
190         RLayerUpdateData *data = node->storage;
191
192         if (scene == NULL || view_layer == NULL || data == NULL || node->id != (ID *)scene) {
193                 return;
194         }
195
196         ViewLayer *node_view_layer = BLI_findlink(&scene->view_layers, node->custom1);
197         if (node_view_layer != view_layer) {
198                 return;
199         }
200
201         /* Special handling for the Combined pass to ensure compatibility. */
202         if (STREQ(name, RE_PASSNAME_COMBINED)) {
203                 cmp_node_image_add_pass_output(ntree, node, "Image", name, -1, type, true, data->available_sockets, &data->prev_index);
204                 cmp_node_image_add_pass_output(ntree, node, "Alpha", name, -1, SOCK_FLOAT, true, data->available_sockets, &data->prev_index);
205         }
206         else {
207                 cmp_node_image_add_pass_output(ntree, node, name, name, -1, type, true, data->available_sockets, &data->prev_index);
208         }
209 }
210
211 static void cmp_node_rlayer_create_outputs(bNodeTree *ntree, bNode *node, LinkNodePair *available_sockets)
212 {
213         Scene *scene = (Scene *)node->id;
214
215         if (scene) {
216                 RenderEngineType *engine_type = RE_engines_find(scene->r.engine);
217                 if (engine_type && engine_type->update_render_passes) {
218                         ViewLayer *view_layer = BLI_findlink(&scene->view_layers, node->custom1);
219                         if (view_layer) {
220                                 RLayerUpdateData *data = MEM_mallocN(sizeof(RLayerUpdateData), "render layer update data");
221                                 data->available_sockets = available_sockets;
222                                 data->prev_index = -1;
223                                 node->storage = data;
224
225                                 RenderEngine *engine = RE_engine_create(engine_type);
226                                 engine_type->update_render_passes(engine, scene, view_layer);
227                                 RE_engine_free(engine);
228
229                                 MEM_freeN(data);
230                                 node->storage = NULL;
231
232                                 return;
233                         }
234                 }
235         }
236
237         int prev_index = -1;
238         cmp_node_image_add_pass_output(ntree, node, "Image", RE_PASSNAME_COMBINED, RRES_OUT_IMAGE, SOCK_RGBA, true, available_sockets, &prev_index);
239         cmp_node_image_add_pass_output(ntree, node, "Alpha", RE_PASSNAME_COMBINED, RRES_OUT_ALPHA, SOCK_FLOAT, true, available_sockets, &prev_index);
240 }
241
242 /* XXX make this into a generic socket verification function for dynamic socket replacement (multilayer, groups, static templates) */
243 static void cmp_node_image_verify_outputs(bNodeTree *ntree, bNode *node, bool rlayer)
244 {
245         bNodeSocket *sock, *sock_next;
246         LinkNodePair available_sockets = {NULL, NULL};
247         int sock_index;
248
249         /* XXX make callback */
250         if (rlayer)
251                 cmp_node_rlayer_create_outputs(ntree, node, &available_sockets);
252         else
253                 cmp_node_image_create_outputs(ntree, node, &available_sockets);
254
255         /* Get rid of sockets whose passes are not available in the image.
256          * If sockets that are not available would be deleted, the connections to them would be lost
257          * when e.g. opening a file (since there's no render at all yet).
258          * Therefore, sockets with connected links will just be set as unavailable.
259          *
260          * Another important detail comes from compatibility with the older socket model, where there
261          * was a fixed socket per pass type that was just hidden or not. Therefore, older versions expect
262          * the first 31 passes to belong to a specific pass type.
263          * So, we keep those 31 always allocated before the others as well, even if they have no links attached. */
264         sock_index = 0;
265         for (sock = node->outputs.first; sock; sock = sock_next, sock_index++) {
266                 sock_next = sock->next;
267                 if (BLI_linklist_index(available_sockets.list, sock) >= 0) {
268                         sock->flag &= ~(SOCK_UNAVAIL | SOCK_HIDDEN);
269                 }
270                 else {
271                         bNodeLink *link;
272                         for (link = ntree->links.first; link; link = link->next) {
273                                 if (link->fromsock == sock) break;
274                         }
275                         if (!link && (!rlayer || sock_index > 30)) {
276                                 MEM_freeN(sock->storage);
277                                 nodeRemoveSocket(ntree, node, sock);
278                         }
279                         else {
280                                 sock->flag |= SOCK_UNAVAIL;
281                         }
282                 }
283         }
284
285         BLI_linklist_free(available_sockets.list, NULL);
286 }
287
288 static void cmp_node_image_update(bNodeTree *ntree, bNode *node)
289 {
290         /* avoid unnecessary updates, only changes to the image/image user data are of interest */
291         if (node->update & NODE_UPDATE_ID)
292                 cmp_node_image_verify_outputs(ntree, node, false);
293
294         cmp_node_update_default(ntree, node);
295 }
296
297 static void node_composit_init_image(bNodeTree *ntree, bNode *node)
298 {
299         ImageUser *iuser = MEM_callocN(sizeof(ImageUser), "node image user");
300         node->storage = iuser;
301         iuser->frames = 1;
302         iuser->sfra = 1;
303         iuser->ok = 1;
304         iuser->flag |= IMA_ANIM_ALWAYS;
305
306         /* setup initial outputs */
307         cmp_node_image_verify_outputs(ntree, node, false);
308 }
309
310 static void node_composit_free_image(bNode *node)
311 {
312         bNodeSocket *sock;
313
314         /* free extra socket info */
315         for (sock = node->outputs.first; sock; sock = sock->next)
316                 MEM_freeN(sock->storage);
317
318         MEM_freeN(node->storage);
319 }
320
321 static void node_composit_copy_image(bNodeTree *UNUSED(dest_ntree), bNode *dest_node, bNode *src_node)
322 {
323         bNodeSocket *sock;
324
325         dest_node->storage = MEM_dupallocN(src_node->storage);
326
327         /* copy extra socket info */
328         for (sock = src_node->outputs.first; sock; sock = sock->next)
329                 sock->new_sock->storage = MEM_dupallocN(sock->storage);
330 }
331
332 void register_node_type_cmp_image(void)
333 {
334         static bNodeType ntype;
335
336         cmp_node_type_base(&ntype, CMP_NODE_IMAGE, "Image", NODE_CLASS_INPUT, NODE_PREVIEW);
337         node_type_init(&ntype, node_composit_init_image);
338         node_type_storage(&ntype, "ImageUser", node_composit_free_image, node_composit_copy_image);
339         node_type_update(&ntype, cmp_node_image_update, NULL);
340         node_type_label(&ntype, node_image_label);
341
342         nodeRegisterType(&ntype);
343 }
344
345
346 /* **************** RENDER RESULT ******************** */
347
348 void node_cmp_rlayers_outputs(bNodeTree *ntree, bNode *node)
349 {
350         cmp_node_image_verify_outputs(ntree, node, true);
351 }
352
353 const char *node_cmp_rlayers_sock_to_pass(int sock_index)
354 {
355         const char *sock_to_passname[] = {
356                 RE_PASSNAME_COMBINED, RE_PASSNAME_COMBINED,
357                 RE_PASSNAME_Z, RE_PASSNAME_NORMAL, RE_PASSNAME_UV, RE_PASSNAME_VECTOR, RE_PASSNAME_DEPRECATED,
358                 RE_PASSNAME_DEPRECATED, RE_PASSNAME_DEPRECATED, RE_PASSNAME_SHADOW, RE_PASSNAME_AO,
359                 RE_PASSNAME_DEPRECATED, RE_PASSNAME_DEPRECATED, RE_PASSNAME_DEPRECATED,
360                 RE_PASSNAME_INDEXOB, RE_PASSNAME_INDEXMA, RE_PASSNAME_MIST, RE_PASSNAME_EMIT, RE_PASSNAME_ENVIRONMENT,
361                 RE_PASSNAME_DIFFUSE_DIRECT, RE_PASSNAME_DIFFUSE_INDIRECT, RE_PASSNAME_DIFFUSE_COLOR,
362                 RE_PASSNAME_GLOSSY_DIRECT, RE_PASSNAME_GLOSSY_INDIRECT, RE_PASSNAME_GLOSSY_COLOR,
363                 RE_PASSNAME_TRANSM_DIRECT, RE_PASSNAME_TRANSM_INDIRECT, RE_PASSNAME_TRANSM_COLOR,
364                 RE_PASSNAME_SUBSURFACE_DIRECT, RE_PASSNAME_SUBSURFACE_INDIRECT, RE_PASSNAME_SUBSURFACE_COLOR
365         };
366         if (sock_index > 30) {
367                 return NULL;
368         }
369         return sock_to_passname[sock_index];
370 }
371
372 static void node_composit_init_rlayers(const bContext *C, PointerRNA *ptr)
373 {
374         Scene *scene = CTX_data_scene(C);
375         bNode *node = ptr->data;
376         int sock_index = 0;
377
378         node->id = &scene->id;
379
380         for (bNodeSocket *sock = node->outputs.first; sock; sock = sock->next, sock_index++) {
381                 NodeImageLayer *sockdata = MEM_callocN(sizeof(NodeImageLayer), "node image layer");
382                 sock->storage = sockdata;
383
384                 BLI_strncpy(sockdata->pass_name, node_cmp_rlayers_sock_to_pass(sock_index), sizeof(sockdata->pass_name));
385         }
386 }
387
388 static bool node_composit_poll_rlayers(bNodeType *UNUSED(ntype), bNodeTree *ntree)
389 {
390         if (STREQ(ntree->idname, "CompositorNodeTree")) {
391                 Scene *scene;
392
393                 /* XXX ugly: check if ntree is a local scene node tree.
394                  * Render layers node can only be used in local scene->nodetree,
395                  * since it directly links to the scene.
396                  */
397                 for (scene = G.main->scene.first; scene; scene = scene->id.next)
398                         if (scene->nodetree == ntree)
399                                 break;
400
401                 return (scene != NULL);
402         }
403         return false;
404 }
405
406 static void node_composit_free_rlayers(bNode *node)
407 {
408         bNodeSocket *sock;
409
410         /* free extra socket info */
411         for (sock = node->outputs.first; sock; sock = sock->next)
412                 MEM_freeN(sock->storage);
413 }
414
415 static void node_composit_copy_rlayers(bNodeTree *UNUSED(dest_ntree), bNode *UNUSED(dest_node), bNode *src_node)
416 {
417         bNodeSocket *sock;
418
419         /* copy extra socket info */
420         for (sock = src_node->outputs.first; sock; sock = sock->next)
421                 sock->new_sock->storage = MEM_dupallocN(sock->storage);
422 }
423
424 static void cmp_node_rlayers_update(bNodeTree *ntree, bNode *node)
425 {
426         cmp_node_image_verify_outputs(ntree, node, true);
427
428         cmp_node_update_default(ntree, node);
429 }
430
431 void register_node_type_cmp_rlayers(void)
432 {
433         static bNodeType ntype;
434
435         cmp_node_type_base(&ntype, CMP_NODE_R_LAYERS, "Render Layers", NODE_CLASS_INPUT, NODE_PREVIEW);
436         node_type_socket_templates(&ntype, NULL, cmp_node_rlayers_out);
437         ntype.initfunc_api = node_composit_init_rlayers;
438         ntype.poll = node_composit_poll_rlayers;
439         node_type_storage(&ntype, NULL, node_composit_free_rlayers, node_composit_copy_rlayers);
440         node_type_update(&ntype, cmp_node_rlayers_update, NULL);
441         node_type_init(&ntype, node_cmp_rlayers_outputs);
442         node_type_size_preset(&ntype, NODE_SIZE_LARGE);
443
444         nodeRegisterType(&ntype);
445 }