code cleanup
[blender-staging.git] / source / blender / nodes / composite / node_composite_tree.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) 2007 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s):
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/nodes/composite/node_composite_tree.c
29  *  \ingroup nodes
30  */
31
32
33 #include <stdio.h>
34
35 #include "DNA_anim_types.h"
36 #include "DNA_color_types.h"
37 #include "DNA_scene_types.h"
38 #include "DNA_node_types.h"
39
40 #include "BLI_listbase.h"
41 #include "BLI_threads.h"
42
43 #include "BLF_translation.h"
44
45 #include "BKE_animsys.h"
46 #include "BKE_colortools.h"
47 #include "BKE_context.h"
48 #include "BKE_fcurve.h"
49 #include "BKE_global.h"
50 #include "BKE_main.h"
51 #include "BKE_node.h"
52 #include "BKE_tracking.h"
53
54 #include "node_common.h"
55 #include "node_util.h"
56
57 #include "PIL_time.h"
58
59 #include "RNA_access.h"
60
61 #include "NOD_composite.h"
62 #include "node_composite_util.h"
63
64 #ifdef WITH_COMPOSITOR
65         #include "COM_compositor.h"
66 #endif
67
68 static void composite_get_from_context(const bContext *C, bNodeTreeType *UNUSED(treetype), bNodeTree **r_ntree, ID **r_id, ID **r_from)
69 {
70         Scene *scene = CTX_data_scene(C);
71         
72         *r_from = NULL;
73         *r_id = &scene->id;
74         *r_ntree = scene->nodetree;
75         
76         /* update output sockets based on available layers */
77         ntreeCompositForceHidden(scene->nodetree, scene);
78         
79 }
80
81 static void foreach_nodeclass(Scene *UNUSED(scene), void *calldata, bNodeClassCallback func)
82 {
83         func(calldata, NODE_CLASS_INPUT, N_("Input"));
84         func(calldata, NODE_CLASS_OUTPUT, N_("Output"));
85         func(calldata, NODE_CLASS_OP_COLOR, N_("Color"));
86         func(calldata, NODE_CLASS_OP_VECTOR, N_("Vector"));
87         func(calldata, NODE_CLASS_OP_FILTER, N_("Filter"));
88         func(calldata, NODE_CLASS_CONVERTOR, N_("Convertor"));
89         func(calldata, NODE_CLASS_MATTE, N_("Matte"));
90         func(calldata, NODE_CLASS_DISTORT, N_("Distort"));
91         func(calldata, NODE_CLASS_GROUP, N_("Group"));
92         func(calldata, NODE_CLASS_INTERFACE, N_("Interface"));
93         func(calldata, NODE_CLASS_LAYOUT, N_("Layout"));
94 }
95
96 static void free_node_cache(bNodeTree *UNUSED(ntree), bNode *node)
97 {
98         bNodeSocket *sock;
99         
100         for (sock= node->outputs.first; sock; sock= sock->next) {
101                 if (sock->cache) {
102                         sock->cache= NULL;
103                 }
104         }
105 }
106
107 static void free_cache(bNodeTree *ntree)
108 {
109         bNode *node;
110         for (node = ntree->nodes.first; node; node = node->next)
111                 free_node_cache(ntree, node);
112 }
113
114 /* local tree then owns all compbufs */
115 static void localize(bNodeTree *localtree, bNodeTree *ntree)
116 {
117         bNode *node, *node_next;
118         bNodeSocket *sock;
119         
120         for (node = ntree->nodes.first; node; node = node->next) {
121                 /* ensure new user input gets handled ok */
122                 node->need_exec= 0;
123                 node->new_node->original = node;
124                 
125                 /* move over the compbufs */
126                 /* right after ntreeCopyTree() oldsock pointers are valid */
127                 
128                 if (ELEM(node->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER)) {
129                         if (node->id) {
130                                 if (node->flag & NODE_DO_OUTPUT)
131                                         node->new_node->id= (ID *)node->id;
132                                 else
133                                         node->new_node->id= NULL;
134                         }
135                 }
136                 
137                 for (sock= node->outputs.first; sock; sock= sock->next) {
138                         sock->new_sock->cache= sock->cache;
139                         sock->cache= NULL;
140                         sock->new_sock->new_sock= sock;
141                 }
142         }
143         
144         /* replace muted nodes and reroute nodes by internal links */
145         for (node = localtree->nodes.first; node; node = node_next) {
146                 node_next = node->next;
147                 
148                 if (node->flag & NODE_MUTED || node->type == NODE_REROUTE) {
149                         /* make sure the update tag isn't lost when removing the muted node.
150                          * propagate this to all downstream nodes.
151                          */
152                         if (node->need_exec) {
153                                 bNodeLink *link;
154                                 for (link=localtree->links.first; link; link=link->next)
155                                         if (link->fromnode==node && link->tonode)
156                                                 link->tonode->need_exec = 1;
157                         }
158                         
159                         nodeInternalRelink(localtree, node);
160                         nodeFreeNode(localtree, node);
161                 }
162         }
163 }
164
165 static void local_sync(bNodeTree *UNUSED(localtree), bNodeTree *UNUSED(ntree))
166 {
167 }
168
169 static void local_merge(bNodeTree *localtree, bNodeTree *ntree)
170 {
171         bNode *lnode;
172         bNodeSocket *lsock;
173         
174         /* move over the compbufs and previews */
175         for (lnode= localtree->nodes.first; lnode; lnode= lnode->next) {
176                 if (ntreeNodeExists(ntree, lnode->new_node)) {
177                         if (ELEM(lnode->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER)) {
178                                 if (lnode->id && (lnode->flag & NODE_DO_OUTPUT)) {
179                                         /* image_merge does sanity check for pointers */
180                                         BKE_image_merge((Image *)lnode->new_node->id, (Image *)lnode->id);
181                                 }
182                         }
183                         else if (lnode->type==CMP_NODE_MOVIEDISTORTION) {
184                                 /* special case for distortion node: distortion context is allocating in exec function
185                                  * and to achieve much better performance on further calls this context should be
186                                  * copied back to original node */
187                                 if (lnode->storage) {
188                                         if (lnode->new_node->storage)
189                                                 BKE_tracking_distortion_free(lnode->new_node->storage);
190
191                                         lnode->new_node->storage= BKE_tracking_distortion_copy(lnode->storage);
192                                 }
193                         }
194                         
195                         for (lsock= lnode->outputs.first; lsock; lsock= lsock->next) {
196                                 if (ntreeOutputExists(lnode->new_node, lsock->new_sock)) {
197                                         lsock->new_sock->cache= lsock->cache;
198                                         lsock->cache= NULL;
199                                         lsock->new_sock= NULL;
200                                 }
201                         }
202                 }
203         }
204 }
205
206 static void update(bNodeTree *ntree)
207 {
208         ntreeSetOutput(ntree);
209         
210         ntree_update_reroute_nodes(ntree);
211         
212         if (ntree->update & NTREE_UPDATE_NODES) {
213                 /* clean up preview cache, in case nodes have been removed */
214                 BKE_node_preview_remove_unused(ntree);
215         }
216 }
217
218 bNodeTreeType *ntreeType_Composite;
219
220 void register_node_tree_type_cmp(void)
221 {
222         bNodeTreeType *tt = ntreeType_Composite = MEM_callocN(sizeof(bNodeTreeType), "compositor node tree type");
223         
224         tt->type = NTREE_COMPOSIT;
225         strcpy(tt->idname, "CompositorNodeTree");
226         strcpy(tt->ui_name, "Compositing");
227         tt->ui_icon = 0;        /* defined in drawnode.c */
228         strcpy(tt->ui_description, "");
229         
230         tt->free_cache = free_cache;
231         tt->free_node_cache = free_node_cache;
232         tt->foreach_nodeclass = foreach_nodeclass;
233         tt->localize = localize;
234         tt->local_sync = local_sync;
235         tt->local_merge = local_merge;
236         tt->update = update;
237         tt->get_from_context = composite_get_from_context;
238         
239         tt->ext.srna = &RNA_CompositorNodeTree;
240         
241         ntreeTypeAdd(tt);
242 }
243
244 void *COM_linker_hack = NULL;
245
246 void ntreeCompositExecTree(bNodeTree *ntree, RenderData *rd, int rendering, int do_preview,
247                            const ColorManagedViewSettings *view_settings,
248                            const ColorManagedDisplaySettings *display_settings)
249 {
250 #ifdef WITH_COMPOSITOR
251         COM_execute(rd, ntree, rendering, view_settings, display_settings);
252 #else
253         (void)ntree, (void)rd, (void)rendering, (void)do_preview;
254         (void)view_settings, (void)display_settings;
255 #endif
256
257         (void)do_preview;
258 }
259
260 /* *********************************************** */
261
262 static void set_output_visible(bNode *node, int passflag, int index, int pass)
263 {
264         bNodeSocket *sock = BLI_findlink(&node->outputs, index);
265         /* clear the SOCK_HIDDEN flag as well, in case a socket was hidden before */
266         if (passflag & pass)
267                 sock->flag &= ~(SOCK_HIDDEN | SOCK_UNAVAIL);
268         else
269                 sock->flag |= SOCK_UNAVAIL;
270 }
271
272 /* clumsy checking... should do dynamic outputs once */
273 static void force_hidden_passes(bNode *node, int passflag)
274 {
275         bNodeSocket *sock;
276         
277         for (sock= node->outputs.first; sock; sock= sock->next)
278                 sock->flag &= ~SOCK_UNAVAIL;
279         
280         set_output_visible(node, passflag, RRES_OUT_IMAGE,            SCE_PASS_COMBINED);
281         set_output_visible(node, passflag, RRES_OUT_ALPHA,            SCE_PASS_COMBINED);
282         
283         set_output_visible(node, passflag, RRES_OUT_Z,                SCE_PASS_Z);
284         set_output_visible(node, passflag, RRES_OUT_NORMAL,           SCE_PASS_NORMAL);
285         set_output_visible(node, passflag, RRES_OUT_VEC,              SCE_PASS_VECTOR);
286         set_output_visible(node, passflag, RRES_OUT_UV,               SCE_PASS_UV);
287         set_output_visible(node, passflag, RRES_OUT_RGBA,             SCE_PASS_RGBA);
288         set_output_visible(node, passflag, RRES_OUT_DIFF,             SCE_PASS_DIFFUSE);
289         set_output_visible(node, passflag, RRES_OUT_SPEC,             SCE_PASS_SPEC);
290         set_output_visible(node, passflag, RRES_OUT_SHADOW,           SCE_PASS_SHADOW);
291         set_output_visible(node, passflag, RRES_OUT_AO,               SCE_PASS_AO);
292         set_output_visible(node, passflag, RRES_OUT_REFLECT,          SCE_PASS_REFLECT);
293         set_output_visible(node, passflag, RRES_OUT_REFRACT,          SCE_PASS_REFRACT);
294         set_output_visible(node, passflag, RRES_OUT_INDIRECT,         SCE_PASS_INDIRECT);
295         set_output_visible(node, passflag, RRES_OUT_INDEXOB,          SCE_PASS_INDEXOB);
296         set_output_visible(node, passflag, RRES_OUT_INDEXMA,          SCE_PASS_INDEXMA);
297         set_output_visible(node, passflag, RRES_OUT_MIST,             SCE_PASS_MIST);
298         set_output_visible(node, passflag, RRES_OUT_EMIT,             SCE_PASS_EMIT);
299         set_output_visible(node, passflag, RRES_OUT_ENV,              SCE_PASS_ENVIRONMENT);
300         set_output_visible(node, passflag, RRES_OUT_DIFF_DIRECT,      SCE_PASS_DIFFUSE_DIRECT);
301         set_output_visible(node, passflag, RRES_OUT_DIFF_INDIRECT,    SCE_PASS_DIFFUSE_INDIRECT);
302         set_output_visible(node, passflag, RRES_OUT_DIFF_COLOR,       SCE_PASS_DIFFUSE_COLOR);
303         set_output_visible(node, passflag, RRES_OUT_GLOSSY_DIRECT,    SCE_PASS_GLOSSY_DIRECT);
304         set_output_visible(node, passflag, RRES_OUT_GLOSSY_INDIRECT,  SCE_PASS_GLOSSY_INDIRECT);
305         set_output_visible(node, passflag, RRES_OUT_GLOSSY_COLOR,     SCE_PASS_GLOSSY_COLOR);
306         set_output_visible(node, passflag, RRES_OUT_TRANSM_DIRECT,    SCE_PASS_TRANSM_DIRECT);
307         set_output_visible(node, passflag, RRES_OUT_TRANSM_INDIRECT,  SCE_PASS_TRANSM_INDIRECT);
308         set_output_visible(node, passflag, RRES_OUT_TRANSM_COLOR,     SCE_PASS_TRANSM_COLOR);
309 }
310
311 /* based on rules, force sockets hidden always */
312 void ntreeCompositForceHidden(bNodeTree *ntree, Scene *curscene)
313 {
314         bNode *node;
315
316         if (ntree == NULL) return;
317
318         for (node = ntree->nodes.first; node; node = node->next) {
319                 if (node->type == CMP_NODE_R_LAYERS) {
320                         Scene *sce = node->id ? (Scene *)node->id : curscene;
321                         SceneRenderLayer *srl = BLI_findlink(&sce->r.layers, node->custom1);
322                         if (srl)
323                                 force_hidden_passes(node, srl->passflag);
324                 }
325                 /* XXX this stuff is called all the time, don't want that.
326                  * Updates should only happen when actually necessary.
327                  */
328                 #if 0
329                 else if (node->type == CMP_NODE_IMAGE) {
330                         nodeUpdate(ntree, node);
331                 }
332                 #endif
333         }
334
335 }
336
337 /* called from render pipeline, to tag render input and output */
338 /* need to do all scenes, to prevent errors when you re-render 1 scene */
339 void ntreeCompositTagRender(Scene *curscene)
340 {
341         Scene *sce;
342
343         for (sce = G.main->scene.first; sce; sce = sce->id.next) {
344                 if (sce->nodetree) {
345                         bNode *node;
346
347                         for (node = sce->nodetree->nodes.first; node; node = node->next) {
348                                 if (node->id == (ID *)curscene || node->type == CMP_NODE_COMPOSITE)
349                                         nodeUpdate(sce->nodetree, node);
350                                 else if (node->type == CMP_NODE_TEXTURE) /* uses scene sizex/sizey */
351                                         nodeUpdate(sce->nodetree, node);
352                         }
353                 }
354         }
355 }
356
357 static int node_animation_properties(bNodeTree *ntree, bNode *node)
358 {
359         bNodeSocket *sock;
360         const ListBase *lb;
361         Link *link;
362         PointerRNA ptr;
363         PropertyRNA *prop;
364
365         /* check to see if any of the node's properties have fcurves */
366         RNA_pointer_create((ID *)ntree, &RNA_Node, node, &ptr);
367         lb = RNA_struct_type_properties(ptr.type);
368
369         for (link = lb->first; link; link = link->next) {
370                 int len = 1, index;
371                 bool driven;
372                 prop = (PropertyRNA *)link;
373
374                 if (RNA_property_array_check(prop))
375                         len = RNA_property_array_length(&ptr, prop);
376
377                 for (index = 0; index < len; index++) {
378                         if (rna_get_fcurve(&ptr, prop, index, NULL, &driven)) {
379                                 nodeUpdate(ntree, node);
380                                 return 1;
381                         }
382                 }
383         }
384
385         /* now check node sockets */
386         for (sock = node->inputs.first; sock; sock = sock->next) {
387                 int len = 1, index;
388                 bool driven;
389
390                 RNA_pointer_create((ID *)ntree, &RNA_NodeSocket, sock, &ptr);
391                 prop = RNA_struct_find_property(&ptr, "default_value");
392                 if (prop) {
393                         if (RNA_property_array_check(prop))
394                                 len = RNA_property_array_length(&ptr, prop);
395
396                         for (index = 0; index < len; index++) {
397                                 if (rna_get_fcurve(&ptr, prop, index, NULL, &driven)) {
398                                         nodeUpdate(ntree, node);
399                                         return 1;
400                                 }
401                         }
402                 }
403         }
404
405         return 0;
406 }
407
408 /* tags nodes that have animation capabilities */
409 int ntreeCompositTagAnimated(bNodeTree *ntree)
410 {
411         bNode *node;
412         int tagged = 0;
413
414         if (ntree == NULL) return 0;
415
416         for (node = ntree->nodes.first; node; node = node->next) {
417
418                 tagged = node_animation_properties(ntree, node);
419
420                 /* otherwise always tag these node types */
421                 if (node->type == CMP_NODE_IMAGE) {
422                         Image *ima = (Image *)node->id;
423                         if (ima && ELEM(ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE)) {
424                                 nodeUpdate(ntree, node);
425                                 tagged = 1;
426                         }
427                 }
428                 else if (node->type == CMP_NODE_TIME) {
429                         nodeUpdate(ntree, node);
430                         tagged = 1;
431                 }
432                 /* here was tag render layer, but this is called after a render, so re-composites fail */
433                 else if (node->type == NODE_GROUP) {
434                         if (ntreeCompositTagAnimated((bNodeTree *)node->id) ) {
435                                 nodeUpdate(ntree, node);
436                         }
437                 }
438                 else if (ELEM(node->type, CMP_NODE_MOVIECLIP, CMP_NODE_TRANSFORM)) {
439                         nodeUpdate(ntree, node);
440                         tagged = 1;
441                 }
442                 else if (node->type == CMP_NODE_MASK) {
443                         nodeUpdate(ntree, node);
444                         tagged = 1;
445                 }
446         }
447
448         return tagged;
449 }
450
451
452 /* called from image window preview */
453 void ntreeCompositTagGenerators(bNodeTree *ntree)
454 {
455         bNode *node;
456
457         if (ntree == NULL) return;
458
459         for (node = ntree->nodes.first; node; node = node->next) {
460                 if (ELEM(node->type, CMP_NODE_R_LAYERS, CMP_NODE_IMAGE))
461                         nodeUpdate(ntree, node);
462         }
463 }
464
465 /* XXX after render animation system gets a refresh, this call allows composite to end clean */
466 void ntreeCompositClearTags(bNodeTree *ntree)
467 {
468         bNode *node;
469
470         if (ntree == NULL) return;
471
472         for (node = ntree->nodes.first; node; node = node->next) {
473                 node->need_exec = 0;
474                 if (node->type == NODE_GROUP)
475                         ntreeCompositClearTags((bNodeTree *)node->id);
476         }
477 }