Fix error in node flag check
[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
45 /* **************** IMAGE (and RenderResult, multilayer image) ******************** */
46
47 static bNodeSocketTemplate cmp_node_rlayers_out[] = {
48         {       SOCK_RGBA,   0, N_("Image"),                                                    0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
49         {       SOCK_FLOAT,  0, N_("Alpha"),                                                    1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
50         {       SOCK_FLOAT,  0, N_(RE_PASSNAME_Z),                                              1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
51         {       SOCK_VECTOR, 0, N_(RE_PASSNAME_NORMAL),                                 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
52         {       SOCK_VECTOR, 0, N_(RE_PASSNAME_UV),                                             1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
53         {       SOCK_VECTOR, 0, N_(RE_PASSNAME_VECTOR),                                 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
54         {       SOCK_RGBA,   0, N_(RE_PASSNAME_RGBA),                                   0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
55         {       SOCK_RGBA,   0, N_(RE_PASSNAME_DIFFUSE),                                0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
56         {       SOCK_RGBA,   0, N_(RE_PASSNAME_SPEC),                                   0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
57         {       SOCK_RGBA,   0, N_(RE_PASSNAME_SHADOW),                                 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
58         {       SOCK_RGBA,   0, N_(RE_PASSNAME_AO),                                             0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
59         {       SOCK_RGBA,   0, N_(RE_PASSNAME_REFLECT),                                0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
60         {       SOCK_RGBA,   0, N_(RE_PASSNAME_REFRACT),                                0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
61         {       SOCK_RGBA,   0, N_(RE_PASSNAME_INDIRECT),                               0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
62         {       SOCK_FLOAT,  0, N_(RE_PASSNAME_INDEXOB),                                0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
63         {       SOCK_FLOAT,  0, N_(RE_PASSNAME_INDEXMA),                                0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
64         {       SOCK_FLOAT,  0, N_(RE_PASSNAME_MIST),                                   0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
65         {       SOCK_RGBA,   0, N_(RE_PASSNAME_EMIT),                                   0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
66         {       SOCK_RGBA,   0, N_(RE_PASSNAME_ENVIRONMENT),                    0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
67         {       SOCK_RGBA,   0, N_(RE_PASSNAME_DIFFUSE_DIRECT),                 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
68         {       SOCK_RGBA,   0, N_(RE_PASSNAME_DIFFUSE_INDIRECT),               0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
69         {       SOCK_RGBA,   0, N_(RE_PASSNAME_DIFFUSE_COLOR),                  0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
70         {       SOCK_RGBA,   0, N_(RE_PASSNAME_GLOSSY_DIRECT),                  0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
71         {       SOCK_RGBA,   0, N_(RE_PASSNAME_GLOSSY_INDIRECT),                0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
72         {       SOCK_RGBA,   0, N_(RE_PASSNAME_GLOSSY_COLOR),                   0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
73         {       SOCK_RGBA,   0, N_(RE_PASSNAME_TRANSM_DIRECT),                  0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
74         {       SOCK_RGBA,   0, N_(RE_PASSNAME_TRANSM_INDIRECT),                0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
75         {       SOCK_RGBA,   0, N_(RE_PASSNAME_TRANSM_COLOR),                   0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
76         {       SOCK_RGBA,   0, N_(RE_PASSNAME_SUBSURFACE_DIRECT),              0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
77         {       SOCK_RGBA,   0, N_(RE_PASSNAME_SUBSURFACE_INDIRECT),    0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
78         {       SOCK_RGBA,   0, N_(RE_PASSNAME_SUBSURFACE_COLOR),               0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f},
79         {       -1, 0, ""       }
80 };
81
82 static void cmp_node_image_add_pass_output(bNodeTree *ntree, bNode *node,
83                                            const char *name, const char *passname,
84                                            int rres_index, int type, int is_rlayers,
85                                            LinkNodePair *available_sockets, int *prev_index)
86 {
87         bNodeSocket *sock;
88         int sock_index = BLI_findstringindex(&node->outputs, name, offsetof(bNodeSocket, name));
89
90         if (sock_index < 0) {
91                 /* The first 31 sockets always are the legacy hardcoded sockets.
92                  * Any dynamically allocated sockets follow afterwards, and are sorted in the order in which they were stored in the RenderResult.
93                  * 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. */
94                 int after_index = *prev_index;
95                 if (is_rlayers && after_index < 30)
96                         after_index = 30;
97
98                 if (rres_index >= 0) {
99                         sock = node_add_socket_from_template(ntree, node, &cmp_node_rlayers_out[rres_index], SOCK_OUT);
100                 }
101                 else {
102                         sock = nodeAddStaticSocket(ntree, node, SOCK_OUT, type, PROP_NONE, name, name);
103                 }
104                 /* extra socket info */
105                 NodeImageLayer *sockdata = MEM_callocN(sizeof(NodeImageLayer), "node image layer");
106                 sock->storage = sockdata;
107
108                 BLI_strncpy(sockdata->pass_name, passname, sizeof(sockdata->pass_name));
109
110                 sock_index = BLI_listbase_count(&node->outputs)-1;
111                 if (sock_index != after_index+1) {
112                         bNodeSocket *after_sock = BLI_findlink(&node->outputs, after_index);
113                         BLI_remlink(&node->outputs, sock);
114                         BLI_insertlinkafter(&node->outputs, after_sock, sock);
115                 }
116         }
117         else {
118                 sock = BLI_findlink(&node->outputs, sock_index);
119         }
120
121         BLI_linklist_append(available_sockets, sock);
122         *prev_index = sock_index;
123 }
124
125 static void cmp_node_image_create_outputs(bNodeTree *ntree, bNode *node, LinkNodePair *available_sockets)
126 {
127         Image *ima = (Image *)node->id;
128         ImBuf *ibuf;
129         int prev_index = -1;
130         if (ima) {
131                 ImageUser *iuser = node->storage;
132                 ImageUser load_iuser = {NULL};
133                 int offset = BKE_image_sequence_guess_offset(ima);
134
135                 /* It is possible that image user in this node is not
136                  * properly updated yet. In this case loading image will
137                  * fail and sockets detection will go wrong.
138                  *
139                  * So we manually construct image user to be sure first
140                  * image from sequence (that one which is set as filename
141                  * for image datablock) is used for sockets detection
142                  */
143                 load_iuser.ok = 1;
144                 load_iuser.framenr = offset;
145
146                 /* make sure ima->type is correct */
147                 ibuf = BKE_image_acquire_ibuf(ima, &load_iuser, NULL);
148                 
149                 if (ima->rr) {
150                         RenderLayer *rl = BLI_findlink(&ima->rr->layers, iuser->layer);
151                         
152                         if (rl) {
153                                 RenderPass *rpass;
154                                 for (rpass = rl->passes.first; rpass; rpass = rpass->next) {
155                                         int type;
156                                         if (rpass->channels == 1)
157                                                 type = SOCK_FLOAT;
158                                         else
159                                                 type = SOCK_RGBA;
160
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, "Image", rpass->name, -1, type, false, available_sockets, &prev_index);
164                                                 cmp_node_image_add_pass_output(ntree, node, "Alpha", rpass->name, -1, SOCK_FLOAT, false, available_sockets, &prev_index);
165                                         }
166                                         else {
167                                                 cmp_node_image_add_pass_output(ntree, node, rpass->name, rpass->name, -1, type, false, available_sockets, &prev_index);
168                                         }
169                                 }
170                                 BKE_image_release_ibuf(ima, ibuf, NULL);
171                                 return;
172                         }
173                 }
174         }
175
176         cmp_node_image_add_pass_output(ntree, node, "Image", RE_PASSNAME_COMBINED, RRES_OUT_IMAGE, SOCK_RGBA, false, available_sockets, &prev_index);
177         cmp_node_image_add_pass_output(ntree, node, "Alpha", RE_PASSNAME_COMBINED, RRES_OUT_ALPHA, SOCK_FLOAT, false, available_sockets, &prev_index);
178
179         if (ima) {
180                 if (!ima->rr) {
181                         cmp_node_image_add_pass_output(ntree, node, RE_PASSNAME_Z, RE_PASSNAME_Z, RRES_OUT_Z, SOCK_FLOAT, false, available_sockets, &prev_index);
182                 }
183                 BKE_image_release_ibuf(ima, ibuf, NULL);
184         }
185 }
186
187 typedef struct RLayerUpdateData {
188         LinkNodePair *available_sockets;
189         int prev_index;
190 } RLayerUpdateData;
191
192 void node_cmp_rlayers_register_pass(bNodeTree *ntree, bNode *node, Scene *scene, SceneRenderLayer *srl, const char *name, int type)
193 {
194         RLayerUpdateData *data = node->storage;
195
196         if (scene == NULL || srl == NULL || data == NULL || node->id != (ID *)scene) {
197                 return;
198         }
199
200         SceneRenderLayer *node_srl = BLI_findlink(&scene->r.layers, node->custom1);
201         if (node_srl != srl) {
202                 return;
203         }
204
205         /* Special handling for the Combined pass to ensure compatibility. */
206         if (STREQ(name, RE_PASSNAME_COMBINED)) {
207                 cmp_node_image_add_pass_output(ntree, node, "Image", name, -1, type, true, data->available_sockets, &data->prev_index);
208                 cmp_node_image_add_pass_output(ntree, node, "Alpha", name, -1, SOCK_FLOAT, true, data->available_sockets, &data->prev_index);
209         }
210         else {
211                 cmp_node_image_add_pass_output(ntree, node, name, name, -1, type, true, data->available_sockets, &data->prev_index);
212         }
213 }
214
215 static void cmp_node_rlayer_create_outputs(bNodeTree *ntree, bNode *node, LinkNodePair *available_sockets)
216 {
217         Scene *scene = (Scene *)node->id;
218
219         if (scene) {
220                 RenderEngineType *engine_type = RE_engines_find(scene->r.engine);
221                 if(engine_type && engine_type->update_render_passes) {
222                         SceneRenderLayer *srl = BLI_findlink(&scene->r.layers, node->custom1);
223                         if(srl) {
224                                 RLayerUpdateData *data = MEM_mallocN(sizeof(RLayerUpdateData), "render layer update data");
225                                 data->available_sockets = available_sockets;
226                                 data->prev_index = -1;
227                                 node->storage = data;
228
229                                 RenderEngine *engine = RE_engine_create(engine_type);
230                                 engine_type->update_render_passes(engine, scene, srl);
231                                 RE_engine_free(engine);
232
233                                 MEM_freeN(data);
234                                 node->storage = NULL;
235
236                                 return;
237                         }
238                 }
239         }
240
241         int prev_index = -1;
242         cmp_node_image_add_pass_output(ntree, node, "Image", RE_PASSNAME_COMBINED, RRES_OUT_IMAGE, SOCK_RGBA, true, available_sockets, &prev_index);
243         cmp_node_image_add_pass_output(ntree, node, "Alpha", RE_PASSNAME_COMBINED, RRES_OUT_ALPHA, SOCK_FLOAT, true, available_sockets, &prev_index);
244 }
245
246 /* XXX make this into a generic socket verification function for dynamic socket replacement (multilayer, groups, static templates) */
247 static void cmp_node_image_verify_outputs(bNodeTree *ntree, bNode *node, bool rlayer)
248 {
249         bNodeSocket *sock, *sock_next;
250         LinkNodePair available_sockets = {NULL, NULL};
251         int sock_index;
252         
253         /* XXX make callback */
254         if (rlayer)
255                 cmp_node_rlayer_create_outputs(ntree, node, &available_sockets);
256         else
257                 cmp_node_image_create_outputs(ntree, node, &available_sockets);
258
259         /* Get rid of sockets whose passes are not available in the image.
260          * If sockets that are not available would be deleted, the connections to them would be lost
261          * when e.g. opening a file (since there's no render at all yet).
262          * Therefore, sockets with connected links will just be set as unavailable.
263          *
264          * Another important detail comes from compatibility with the older socket model, where there
265          * was a fixed socket per pass type that was just hidden or not. Therefore, older versions expect
266          * the first 31 passes to belong to a specific pass type.
267          * So, we keep those 31 always allocated before the others as well, even if they have no links attached. */
268         sock_index = 0;
269         for (sock = node->outputs.first; sock; sock = sock_next, sock_index++) {
270                 sock_next = sock->next;
271                 if (BLI_linklist_index(available_sockets.list, sock) >= 0) {
272                         sock->flag &= ~(SOCK_UNAVAIL | SOCK_HIDDEN);
273                 }
274                 else {
275                         bNodeLink *link;
276                         for (link = ntree->links.first; link; link = link->next) {
277                                 if (link->fromsock == sock) break;
278                         }
279                         if (!link && sock_index > 30) {
280                                 MEM_freeN(sock->storage);
281                                 nodeRemoveSocket(ntree, node, sock);
282                         }
283                         else {
284                                 sock->flag |= SOCK_UNAVAIL;
285                         }
286                 }
287         }
288
289         BLI_linklist_free(available_sockets.list, NULL);
290 }
291
292 static void cmp_node_image_update(bNodeTree *ntree, bNode *node)
293 {
294         /* avoid unnecessary updates, only changes to the image/image user data are of interest */
295         if (node->update & NODE_UPDATE_ID)
296                 cmp_node_image_verify_outputs(ntree, node, false);
297 }
298
299 static void node_composit_init_image(bNodeTree *ntree, bNode *node)
300 {
301         ImageUser *iuser = MEM_callocN(sizeof(ImageUser), "node image user");
302         node->storage = iuser;
303         iuser->frames = 1;
304         iuser->sfra = 1;
305         iuser->fie_ima = 2;
306         iuser->ok = 1;
307         
308         /* setup initial outputs */
309         cmp_node_image_verify_outputs(ntree, node, false);
310 }
311
312 static void node_composit_free_image(bNode *node)
313 {
314         bNodeSocket *sock;
315         
316         /* free extra socket info */
317         for (sock = node->outputs.first; sock; sock = sock->next)
318                 MEM_freeN(sock->storage);
319         
320         MEM_freeN(node->storage);
321 }
322
323 static void node_composit_copy_image(bNodeTree *UNUSED(dest_ntree), bNode *dest_node, bNode *src_node)
324 {
325         bNodeSocket *sock;
326         
327         dest_node->storage = MEM_dupallocN(src_node->storage);
328         
329         /* copy extra socket info */
330         for (sock = src_node->outputs.first; sock; sock = sock->next)
331                 sock->new_sock->storage = MEM_dupallocN(sock->storage);
332 }
333
334 void register_node_type_cmp_image(void)
335 {
336         static bNodeType ntype;
337
338         cmp_node_type_base(&ntype, CMP_NODE_IMAGE, "Image", NODE_CLASS_INPUT, NODE_PREVIEW);
339         node_type_init(&ntype, node_composit_init_image);
340         node_type_storage(&ntype, "ImageUser", node_composit_free_image, node_composit_copy_image);
341         node_type_update(&ntype, cmp_node_image_update, NULL);
342
343         nodeRegisterType(&ntype);
344 }
345
346
347 /* **************** RENDER RESULT ******************** */
348
349 void node_cmp_rlayers_outputs(bNodeTree *ntree, bNode *node)
350 {
351         cmp_node_image_verify_outputs(ntree, node, true);
352 }
353
354 const char* node_cmp_rlayers_sock_to_pass(int sock_index)
355 {
356         const char* sock_to_passname[] = {
357                 RE_PASSNAME_COMBINED, RE_PASSNAME_COMBINED,
358                 RE_PASSNAME_Z, RE_PASSNAME_NORMAL, RE_PASSNAME_UV, RE_PASSNAME_VECTOR, RE_PASSNAME_RGBA,
359                 RE_PASSNAME_DIFFUSE, RE_PASSNAME_SPEC, RE_PASSNAME_SHADOW, RE_PASSNAME_AO,
360                 RE_PASSNAME_REFLECT, RE_PASSNAME_REFRACT, RE_PASSNAME_INDIRECT,
361                 RE_PASSNAME_INDEXOB, RE_PASSNAME_INDEXMA, RE_PASSNAME_MIST, RE_PASSNAME_EMIT, RE_PASSNAME_ENVIRONMENT,
362                 RE_PASSNAME_DIFFUSE_DIRECT, RE_PASSNAME_DIFFUSE_INDIRECT, RE_PASSNAME_DIFFUSE_COLOR,
363                 RE_PASSNAME_GLOSSY_DIRECT, RE_PASSNAME_GLOSSY_INDIRECT, RE_PASSNAME_GLOSSY_COLOR,
364                 RE_PASSNAME_TRANSM_DIRECT, RE_PASSNAME_TRANSM_INDIRECT, RE_PASSNAME_TRANSM_COLOR,
365                 RE_PASSNAME_SUBSURFACE_DIRECT, RE_PASSNAME_SUBSURFACE_INDIRECT, RE_PASSNAME_SUBSURFACE_COLOR
366         };
367         if (sock_index > 30) {
368                 return NULL;
369         }
370         return sock_to_passname[sock_index];
371 }
372
373 static void node_composit_init_rlayers(const bContext *C, PointerRNA *ptr)
374 {
375         Scene *scene = CTX_data_scene(C);
376         bNode *node = ptr->data;
377         int sock_index = 0;
378
379         node->id = &scene->id;
380
381         for (bNodeSocket *sock = node->outputs.first; sock; sock = sock->next, sock_index++)
382         {
383                 NodeImageLayer *sockdata = MEM_callocN(sizeof(NodeImageLayer), "node image layer");
384                 sock->storage = sockdata;
385
386                 BLI_strncpy(sockdata->pass_name, node_cmp_rlayers_sock_to_pass(sock_index), sizeof(sockdata->pass_name));
387         }
388 }
389
390 static int node_composit_poll_rlayers(bNodeType *UNUSED(ntype), bNodeTree *ntree)
391 {
392         if (STREQ(ntree->idname, "CompositorNodeTree")) {
393                 Scene *scene;
394                 
395                 /* XXX ugly: check if ntree is a local scene node tree.
396                  * Render layers node can only be used in local scene->nodetree,
397                  * since it directly links to the scene.
398                  */
399                 for (scene = G.main->scene.first; scene; scene = scene->id.next)
400                         if (scene->nodetree == ntree)
401                                 break;
402                 
403                 return (scene != NULL);
404         }
405         return false;
406 }
407
408 static void node_composit_free_rlayers(bNode *node)
409 {
410         bNodeSocket *sock;
411
412         /* free extra socket info */
413         for (sock = node->outputs.first; sock; sock = sock->next)
414                 MEM_freeN(sock->storage);
415 }
416
417 static void node_composit_copy_rlayers(bNodeTree *UNUSED(dest_ntree), bNode *UNUSED(dest_node), bNode *src_node)
418 {
419         bNodeSocket *sock;
420
421         /* copy extra socket info */
422         for (sock = src_node->outputs.first; sock; sock = sock->next)
423                 sock->new_sock->storage = MEM_dupallocN(sock->storage);
424 }
425
426 static void cmp_node_rlayers_update(bNodeTree *ntree, bNode *node)
427 {
428         cmp_node_image_verify_outputs(ntree, node, true);
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
443         nodeRegisterType(&ntype);
444 }