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