Color Management, Stage 2: Switch color pipeline to use OpenColorIO
[blender.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_fcurve.h"
48 #include "BKE_global.h"
49 #include "BKE_main.h"
50 #include "BKE_node.h"
51 #include "BKE_tracking.h"
52
53 #include "node_common.h"
54 #include "node_exec.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 foreach_nodetree(Main *main, void *calldata, bNodeTreeCallback func)
69 {
70         Scene *sce;
71         for (sce= main->scene.first; sce; sce= sce->id.next) {
72                 if (sce->nodetree) {
73                         func(calldata, &sce->id, sce->nodetree);
74                 }
75         }
76 }
77
78 static void foreach_nodeclass(Scene *UNUSED(scene), void *calldata, bNodeClassCallback func)
79 {
80         func(calldata, NODE_CLASS_INPUT, N_("Input"));
81         func(calldata, NODE_CLASS_OUTPUT, N_("Output"));
82         func(calldata, NODE_CLASS_OP_COLOR, N_("Color"));
83         func(calldata, NODE_CLASS_OP_VECTOR, N_("Vector"));
84         func(calldata, NODE_CLASS_OP_FILTER, N_("Filter"));
85         func(calldata, NODE_CLASS_CONVERTOR, N_("Convertor"));
86         func(calldata, NODE_CLASS_MATTE, N_("Matte"));
87         func(calldata, NODE_CLASS_DISTORT, N_("Distort"));
88         func(calldata, NODE_CLASS_GROUP, N_("Group"));
89         func(calldata, NODE_CLASS_LAYOUT, N_("Layout"));
90 }
91
92 static void free_node_cache(bNodeTree *UNUSED(ntree), bNode *node)
93 {
94         bNodeSocket *sock;
95         
96         for (sock= node->outputs.first; sock; sock= sock->next) {
97                 if (sock->cache) {
98 #ifdef WITH_COMPOSITOR_LEGACY
99                         free_compbuf(sock->cache);
100 #endif
101                         sock->cache= NULL;
102                 }
103         }
104 }
105
106 static void free_cache(bNodeTree *ntree)
107 {
108         bNode *node;
109         for (node= ntree->nodes.first; node; node= node->next)
110                 free_node_cache(ntree, node);
111 }
112
113 static void update_node(bNodeTree *ntree, bNode *node)
114 {
115         bNodeSocket *sock;
116
117         for (sock= node->outputs.first; sock; sock= sock->next) {
118                 if (sock->cache) {
119                         //free_compbuf(sock->cache);
120                         //sock->cache= NULL;
121                 }
122         }
123         node->need_exec= 1;
124         /* individual node update call */
125         if (node->typeinfo->updatefunc)
126                 node->typeinfo->updatefunc(ntree, node);
127 }
128
129 /* local tree then owns all compbufs */
130 static void localize(bNodeTree *localtree, bNodeTree *ntree)
131 {
132         bNode *node, *node_next;
133         bNodeSocket *sock;
134         
135         for (node= ntree->nodes.first; node; node= node->next) {
136                 /* ensure new user input gets handled ok */
137                 node->need_exec= 0;
138                 node->new_node->original = node;
139                 
140                 /* move over the compbufs */
141                 /* right after ntreeCopyTree() oldsock pointers are valid */
142                 
143                 if (ELEM(node->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER)) {
144                         if (node->id) {
145                                 if (node->flag & NODE_DO_OUTPUT)
146                                         node->new_node->id= (ID *)node->id;
147                                 else
148                                         node->new_node->id= NULL;
149                         }
150                 }
151                 
152                 /* copy over the preview buffers to update graduatly */
153                 if (node->preview) {
154                         bNodePreview *preview = MEM_callocN(sizeof(bNodePreview), "Preview");
155                         preview->pad = node->preview->pad;
156                         preview->xsize = node->preview->xsize;
157                         preview->ysize = node->preview->ysize;
158                         preview->rect = MEM_dupallocN(node->preview->rect);
159                         node->new_node->preview = preview;
160                 }
161                 
162                 for (sock= node->outputs.first; sock; sock= sock->next) {
163                         sock->new_sock->cache= sock->cache;
164 #ifdef WITH_COMPOSITOR_LEGACY
165                         compbuf_set_node(sock->new_sock->cache, node->new_node);
166 #endif
167                         sock->cache= NULL;
168                         sock->new_sock->new_sock= sock;
169                 }
170         }
171         
172         /* replace muted nodes and reroute nodes by internal links */
173         for (node= localtree->nodes.first; node; node= node_next) {
174                 node_next = node->next;
175                 
176                 if (node->flag & NODE_MUTED || node->type == NODE_REROUTE) {
177                         /* make sure the update tag isn't lost when removing the muted node.
178                          * propagate this to all downstream nodes.
179                          */
180                         if (node->need_exec) {
181                                 bNodeLink *link;
182                                 for (link=localtree->links.first; link; link=link->next)
183                                         if (link->fromnode==node && link->tonode)
184                                                 link->tonode->need_exec = 1;
185                         }
186                         
187                         nodeInternalRelink(localtree, node);
188                         nodeFreeNode(localtree, node);
189                 }
190         }
191 }
192
193 static void local_sync(bNodeTree *localtree, bNodeTree *ntree)
194 {
195         bNode *lnode;
196         
197         /* move over the compbufs and previews */
198         for (lnode= localtree->nodes.first; lnode; lnode= lnode->next) {
199                 if ( (lnode->exec & NODE_READY) && !(lnode->exec & NODE_SKIPPED) ) {
200                         if (ntreeNodeExists(ntree, lnode->new_node)) {
201                                 
202                                 if (lnode->preview && lnode->preview->rect) {
203                                         nodeFreePreview(lnode->new_node);
204                                         lnode->new_node->preview= lnode->preview;
205                                         lnode->preview= NULL;
206                                 }
207                                 
208                         }
209                 }
210         }
211 }
212
213 static void local_merge(bNodeTree *localtree, bNodeTree *ntree)
214 {
215         bNode *lnode;
216         bNodeSocket *lsock;
217         
218         /* move over the compbufs and previews */
219         for (lnode= localtree->nodes.first; lnode; lnode= lnode->next) {
220                 if (ntreeNodeExists(ntree, lnode->new_node)) {
221                         if (ELEM(lnode->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER)) {
222                                 if (lnode->id && (lnode->flag & NODE_DO_OUTPUT)) {
223                                         /* image_merge does sanity check for pointers */
224                                         BKE_image_merge((Image *)lnode->new_node->id, (Image *)lnode->id);
225                                 }
226                         }
227                         else if (lnode->type==CMP_NODE_MOVIEDISTORTION) {
228                                 /* special case for distortion node: distortion context is allocating in exec function
229                                  * and to achive much better performance on further calls this context should be
230                                  * copied back to original node */
231                                 if (lnode->storage) {
232                                         if (lnode->new_node->storage)
233                                                 BKE_tracking_distortion_free(lnode->new_node->storage);
234
235                                         lnode->new_node->storage= BKE_tracking_distortion_copy(lnode->storage);
236                                 }
237                         }
238                         
239                         for (lsock= lnode->outputs.first; lsock; lsock= lsock->next) {
240                                 if (ntreeOutputExists(lnode->new_node, lsock->new_sock)) {
241                                         lsock->new_sock->cache= lsock->cache;
242 #ifdef WITH_COMPOSITOR_LEGACY
243                                         compbuf_set_node(lsock->new_sock->cache, lnode->new_node);
244 #endif
245                                         lsock->cache= NULL;
246                                         lsock->new_sock= NULL;
247                                 }
248                         }
249                 }
250         }
251 }
252
253 static void update(bNodeTree *ntree)
254 {
255         ntreeSetOutput(ntree);
256         
257         ntree_update_reroute_nodes(ntree);
258 }
259
260 bNodeTreeType ntreeType_Composite = {
261         /* type */                              NTREE_COMPOSIT,
262         /* idname */                    "NTCompositing Nodetree",
263         
264         /* node_types */                { NULL, NULL },
265         
266         /* free_cache */                free_cache,
267         /* free_node_cache */   free_node_cache,
268         /* foreach_nodetree */  foreach_nodetree,
269         /* foreach_nodeclass */ foreach_nodeclass,
270         /* localize */                  localize,
271         /* local_sync */                local_sync,
272         /* local_merge */               local_merge,
273         /* update */                    update,
274         /* update_node */               update_node,
275         /* validate_link */             NULL,
276         /* internal_connect */  node_internal_connect_default
277 };
278
279
280 /* XXX Group nodes must set use_tree_data to false, since their trees can be shared by multiple nodes.
281  * If use_tree_data is true, the ntree->execdata pointer is checked to avoid multiple execution of top-level trees.
282  */
283 struct bNodeTreeExec *ntreeCompositBeginExecTree(bNodeTree *ntree, int use_tree_data)
284 {
285         bNodeTreeExec *exec;
286         bNode *node;
287         bNodeSocket *sock;
288         
289         if (use_tree_data) {
290                 /* XXX hack: prevent exec data from being generated twice.
291                  * this should be handled by the renderer!
292                  */
293                 if (ntree->execdata)
294                         return ntree->execdata;
295         }
296         
297         /* ensures only a single output node is enabled */
298         ntreeSetOutput(ntree);
299         
300         exec = ntree_exec_begin(ntree);
301         
302         for (node= exec->nodetree->nodes.first; node; node= node->next) {
303                 /* initialize needed for groups */
304                 node->exec= 0;  
305                 
306                 for (sock= node->outputs.first; sock; sock= sock->next) {
307                         bNodeStack *ns= node_get_socket_stack(exec->stack, sock);
308                         if (ns && sock->cache) {
309                                 ns->data= sock->cache;
310                                 sock->cache= NULL;
311                         }
312                 }
313                 /* cannot initialize them while using in threads */
314                 if (ELEM4(node->type, CMP_NODE_TIME, CMP_NODE_CURVE_VEC, CMP_NODE_CURVE_RGB, CMP_NODE_HUECORRECT)) {
315                         curvemapping_initialize(node->storage);
316                         if (node->type==CMP_NODE_CURVE_RGB)
317                                 curvemapping_premultiply(node->storage, 0);
318                 }
319         }
320         
321         if (use_tree_data) {
322                 /* XXX this should not be necessary, but is still used for cmp/sha/tex nodes,
323                  * which only store the ntree pointer. Should be fixed at some point!
324                  */
325                 ntree->execdata = exec;
326         }
327         
328         return exec;
329 }
330
331 /* XXX Group nodes must set use_tree_data to false, since their trees can be shared by multiple nodes.
332  * If use_tree_data is true, the ntree->execdata pointer is checked to avoid multiple execution of top-level trees.
333  */
334 void ntreeCompositEndExecTree(bNodeTreeExec *exec, int use_tree_data)
335 {
336         if (exec) {
337                 bNodeTree *ntree= exec->nodetree;
338                 bNode *node;
339                 bNodeStack *ns;
340                 
341                 for (node= exec->nodetree->nodes.first; node; node= node->next) {
342                         bNodeSocket *sock;
343                         
344                         for (sock= node->outputs.first; sock; sock= sock->next) {
345                                 ns = node_get_socket_stack(exec->stack, sock);
346                                 if (ns && ns->data) {
347                                         sock->cache= ns->data;
348                                         ns->data= NULL;
349                                 }
350                         }
351                         if (node->type==CMP_NODE_CURVE_RGB)
352                                 curvemapping_premultiply(node->storage, 1);
353                         
354                         node->need_exec= 0;
355                 }
356         
357                 ntree_exec_end(exec);
358                 
359                 if (use_tree_data) {
360                         /* XXX clear nodetree backpointer to exec data, same problem as noted in ntreeBeginExecTree */
361                         ntree->execdata = NULL;
362                 }
363         }
364 }
365
366 #ifdef WITH_COMPOSITOR
367 #ifdef WITH_COMPOSITOR_LEGACY
368
369 /* ***************************** threaded version for execute composite nodes ************* */
370 /* these are nodes without input, only giving values */
371 /* or nodes with only value inputs */
372 static int node_only_value(bNode *node)
373 {
374         bNodeSocket *sock;
375         
376         if (ELEM3(node->type, CMP_NODE_TIME, CMP_NODE_VALUE, CMP_NODE_RGB))
377                 return 1;
378         
379         /* doing this for all node types goes wrong. memory free errors */
380         if (node->inputs.first && node->type==CMP_NODE_MAP_VALUE) {
381                 int retval= 1;
382                 for (sock= node->inputs.first; sock; sock= sock->next) {
383                         if (sock->link)
384                                 retval &= node_only_value(sock->link->fromnode);
385                 }
386                 return retval;
387         }
388         return 0;
389 }
390
391 /* not changing info, for thread callback */
392 typedef struct ThreadData {
393         bNodeStack *stack;
394         RenderData *rd;
395 } ThreadData;
396
397 static void *exec_composite_node(void *nodeexec_v)
398 {
399         bNodeStack *nsin[MAX_SOCKET];   /* arbitrary... watch this */
400         bNodeStack *nsout[MAX_SOCKET];  /* arbitrary... watch this */
401         bNodeExec *nodeexec= nodeexec_v;
402         bNode *node= nodeexec->node;
403         ThreadData *thd= (ThreadData *)node->threaddata;
404         
405         node_get_stack(node, thd->stack, nsin, nsout);
406         
407         if (node->typeinfo->execfunc)
408                 node->typeinfo->execfunc(thd->rd, node, nsin, nsout);
409         else if (node->typeinfo->newexecfunc)
410                 node->typeinfo->newexecfunc(thd->rd, 0, node, nodeexec->data, nsin, nsout);
411         
412         node->exec |= NODE_READY;
413         return NULL;
414 }
415
416 /* return total of executable nodes, for timecursor */
417 static int setExecutableNodes(bNodeTreeExec *exec, ThreadData *thd)
418 {
419         bNodeTree *ntree = exec->nodetree;
420         bNodeStack *nsin[MAX_SOCKET];   /* arbitrary... watch this */
421         bNodeStack *nsout[MAX_SOCKET];  /* arbitrary... watch this */
422         bNodeExec *nodeexec;
423         bNode *node;
424         bNodeSocket *sock;
425         int n, totnode= 0, group_edit= 0;
426         
427         /* if we are in group edit, viewer nodes get skipped when group has viewer */
428         for (node= ntree->nodes.first; node; node= node->next)
429                 if (node->type==NODE_GROUP && (node->flag & NODE_GROUP_EDIT))
430                         if (ntreeHasType((bNodeTree *)node->id, CMP_NODE_VIEWER))
431                                 group_edit= 1;
432         
433         /* NB: using the exec data list here to have valid dependency sort */
434         for (n=0, nodeexec=exec->nodeexec; n < exec->totnodes; ++n, ++nodeexec) {
435                 int a;
436                 node = nodeexec->node;
437                 
438                 node_get_stack(node, exec->stack, nsin, nsout);
439                 
440                 /* test the outputs */
441                 /* skip value-only nodes (should be in type!) */
442                 if (!node_only_value(node)) {
443                         for (a=0, sock= node->outputs.first; sock; sock= sock->next, a++) {
444                                 if (nsout[a]->data==NULL && nsout[a]->hasoutput) {
445                                         node->need_exec= 1;
446                                         break;
447                                 }
448                         }
449                 }
450                 
451                 /* test the inputs */
452                 for (a=0, sock= node->inputs.first; sock; sock= sock->next, a++) {
453                         /* skip viewer nodes in bg render or group edit */
454                         if ( ELEM(node->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER) && (G.background || group_edit))
455                                 node->need_exec= 0;
456                         /* is sock in use? */
457                         else if (sock->link) {
458                                 bNodeLink *link= sock->link;
459                                 
460                                 /* this is the test for a cyclic case */
461                                 if (link->fromnode==NULL || link->tonode==NULL);
462                                 else if (link->fromnode->level >= link->tonode->level && link->tonode->level!=0xFFF) {
463                                         if (link->fromnode->need_exec) {
464                                                 node->need_exec= 1;
465                                                 break;
466                                         }
467                                 }
468                                 else {
469                                         node->need_exec= 0;
470                                         printf("Node %s skipped, cyclic dependency\n", node->name);
471                                 }
472                         }
473                 }
474                 
475                 if (node->need_exec) {
476                         
477                         /* free output buffers */
478                         for (a=0, sock= node->outputs.first; sock; sock= sock->next, a++) {
479                                 if (nsout[a]->data) {
480                                         free_compbuf(nsout[a]->data);
481                                         nsout[a]->data= NULL;
482                                 }
483                         }
484                         totnode++;
485                         /* printf("node needs exec %s\n", node->name); */
486                         
487                         /* tag for getExecutableNode() */
488                         node->exec= 0;
489                 }
490                 else {
491                         /* tag for getExecutableNode() */
492                         node->exec= NODE_READY|NODE_FINISHED|NODE_SKIPPED;
493                         
494                 }
495         }
496         
497         /* last step: set the stack values for only-value nodes */
498         /* just does all now, compared to a full buffer exec this is nothing */
499         if (totnode) {
500                 for (n=0, nodeexec=exec->nodeexec; n < exec->totnodes; ++n, ++nodeexec) {
501                         node = nodeexec->node;
502                         if (node->need_exec==0 && node_only_value(node)) {
503                                 if (node->typeinfo->execfunc) {
504                                         node_get_stack(node, exec->stack, nsin, nsout);
505                                         node->typeinfo->execfunc(thd->rd, node, nsin, nsout);
506                                 }
507                         }
508                 }
509         }
510         
511         return totnode;
512 }
513
514 /* while executing tree, free buffers from nodes that are not needed anymore */
515 static void freeExecutableNode(bNodeTreeExec *exec)
516 {
517         /* node outputs can be freed when:
518         - not a render result or image node
519         - when node outputs go to nodes all being set NODE_FINISHED
520         */
521         bNodeTree *ntree = exec->nodetree;
522         bNodeExec *nodeexec;
523         bNode *node;
524         bNodeSocket *sock;
525         int n;
526         
527         /* set exec flag for finished nodes that might need freed */
528         for (node= ntree->nodes.first; node; node= node->next) {
529                 if (node->type!=CMP_NODE_R_LAYERS)
530                         if (node->exec & NODE_FINISHED)
531                                 node->exec |= NODE_FREEBUFS;
532         }
533         /* clear this flag for input links that are not done yet.
534          * Using the exec data for valid dependency sort.
535          */
536         for (n=0, nodeexec=exec->nodeexec; n < exec->totnodes; ++n, ++nodeexec) {
537                 node = nodeexec->node;
538                 if ((node->exec & NODE_FINISHED)==0) {
539                         for (sock= node->inputs.first; sock; sock= sock->next)
540                                 if (sock->link)
541                                         sock->link->fromnode->exec &= ~NODE_FREEBUFS;
542                 }
543         }
544         /* now we can free buffers */
545         for (node= ntree->nodes.first; node; node= node->next) {
546                 if (node->exec & NODE_FREEBUFS) {
547                         for (sock= node->outputs.first; sock; sock= sock->next) {
548                                 bNodeStack *ns= node_get_socket_stack(exec->stack, sock);
549                                 if (ns && ns->data) {
550                                         free_compbuf(ns->data);
551                                         ns->data= NULL;
552                                         // printf("freed buf node %s\n", node->name);
553                                 }
554                         }
555                 }
556         }
557 }
558
559 static bNodeExec *getExecutableNode(bNodeTreeExec *exec)
560 {
561         bNodeExec *nodeexec;
562         bNodeSocket *sock;
563         int n;
564         
565         for (n=0, nodeexec=exec->nodeexec; n < exec->totnodes; ++n, ++nodeexec) {
566                 if (nodeexec->node->exec==0) {
567                         /* input sockets should be ready */
568                         for (sock= nodeexec->node->inputs.first; sock; sock= sock->next) {
569                                 if (sock->link && sock->link->fromnode)
570                                         if ((sock->link->fromnode->exec & NODE_READY)==0)
571                                                 break;
572                         }
573                         if (sock==NULL)
574                                 return nodeexec;
575                 }
576         }
577         return NULL;
578 }
579
580 /* check if texture nodes need exec or end */
581 static  void ntree_composite_texnode(bNodeTree *ntree, int init)
582 {
583         bNode *node;
584         
585         for (node= ntree->nodes.first; node; node= node->next) {
586                 if (node->type==CMP_NODE_TEXTURE && node->id) {
587                         Tex *tex= (Tex *)node->id;
588                         if (tex->nodetree && tex->use_nodes) {
589                                 /* has internal flag to detect it only does it once */
590                                 if (init) {
591                                         if (!tex->nodetree->execdata)
592                                                 tex->nodetree->execdata = ntreeTexBeginExecTree(tex->nodetree, 1); 
593                                 }
594                                 else
595                                         ntreeTexEndExecTree(tex->nodetree->execdata, 1);
596                                         tex->nodetree->execdata = NULL;
597                         }
598                 }
599         }
600
601 }
602
603 /* optimized tree execute test for compositing */
604 static void ntreeCompositExecTreeOld(bNodeTree *ntree, RenderData *rd, int do_preview)
605 {
606         bNodeExec *nodeexec;
607         bNode *node;
608         ListBase threads;
609         ThreadData thdata;
610         int totnode, curnode, rendering = TRUE, n;
611         bNodeTreeExec *exec = ntree->execdata;
612
613         if (do_preview)
614                 ntreeInitPreview(ntree, 0, 0);
615         
616         if (!ntree->execdata) {
617                 /* XXX this is the top-level tree, so we use the ntree->execdata pointer. */
618                 exec = ntreeCompositBeginExecTree(ntree, 1);
619         }
620         ntree_composite_texnode(ntree, 1);
621         
622         /* prevent unlucky accidents */
623         if (G.background)
624                 rd->scemode &= ~R_COMP_CROP;
625         
626         /* setup callerdata for thread callback */
627         thdata.rd= rd;
628         thdata.stack= exec->stack;
629         
630         /* fixed seed, for example noise texture */
631         BLI_srandom(rd->cfra);
632
633         /* sets need_exec tags in nodes */
634         curnode = totnode= setExecutableNodes(exec, &thdata);
635
636         BLI_init_threads(&threads, exec_composite_node, rd->threads);
637         
638         while (rendering) {
639                 
640                 if (BLI_available_threads(&threads)) {
641                         nodeexec= getExecutableNode(exec);
642                         if (nodeexec) {
643                                 node = nodeexec->node;
644                                 if (ntree->progress && totnode)
645                                         ntree->progress(ntree->prh, (1.0f - curnode/(float)totnode));
646                                 if (ntree->stats_draw) {
647                                         char str[128];
648                                         BLI_snprintf(str, sizeof(str), "Compositing %d %s", curnode, node->name);
649                                         ntree->stats_draw(ntree->sdh, str);
650                                 }
651                                 curnode--;
652                                 
653                                 node->threaddata = &thdata;
654                                 node->exec= NODE_PROCESSING;
655                                 BLI_insert_thread(&threads, nodeexec);
656                         }
657                         else
658                                 PIL_sleep_ms(50);
659                 }
660                 else
661                         PIL_sleep_ms(50);
662                 
663                 rendering= 0;
664                 /* test for ESC */
665                 if (ntree->test_break && ntree->test_break(ntree->tbh)) {
666                         for (node= ntree->nodes.first; node; node= node->next)
667                                 node->exec |= NODE_READY;
668                 }
669                 
670                 /* check for ready ones, and if we need to continue */
671                 for (n=0, nodeexec=exec->nodeexec; n < exec->totnodes; ++n, ++nodeexec) {
672                         node = nodeexec->node;
673                         if (node->exec & NODE_READY) {
674                                 if ((node->exec & NODE_FINISHED)==0) {
675                                         BLI_remove_thread(&threads, nodeexec); /* this waits for running thread to finish btw */
676                                         node->exec |= NODE_FINISHED;
677                                         
678                                         /* freeing unused buffers */
679                                         if (rd->scemode & R_COMP_FREE)
680                                                 freeExecutableNode(exec);
681                                 }
682                         }
683                         else rendering= 1;
684                 }
685         }
686         
687         BLI_end_threads(&threads);
688         
689         /* XXX top-level tree uses the ntree->execdata pointer */
690         ntreeCompositEndExecTree(exec, 1);
691 }
692 #endif  /* WITH_COMPOSITOR_LEGACY */
693 #endif  /* WITH_COMPOSITOR */
694
695 void *COM_linker_hack = NULL;
696
697 void ntreeCompositExecTree(bNodeTree *ntree, RenderData *rd, int rendering, int do_preview,
698                            const ColorManagedViewSettings *view_settings,
699                            const ColorManagedDisplaySettings *display_settings)
700 {
701 #ifdef WITH_COMPOSITOR
702 #ifdef WITH_COMPOSITOR_LEGACY
703         if (G.debug_value == 200)
704         {
705                 ntreeCompositExecTreeOld(ntree, rd, do_preview);
706         }
707         else
708 #endif
709         {
710                 COM_execute(rd, ntree, rendering, view_settings, display_settings);
711         }
712 #else
713         (void)ntree, (void)rd, (void)rendering, (void)do_preview;
714         (void)view_settings, (void)display_settings;
715 #endif
716
717         (void)do_preview;
718 }
719
720 /* *********************************************** */
721
722 /* clumsy checking... should do dynamic outputs once */
723 static void force_hidden_passes(bNode *node, int passflag)
724 {
725         bNodeSocket *sock;
726         
727         for (sock= node->outputs.first; sock; sock= sock->next)
728                 sock->flag &= ~SOCK_UNAVAIL;
729         
730         if (!(passflag & SCE_PASS_COMBINED)) {
731                 sock= BLI_findlink(&node->outputs, RRES_OUT_IMAGE);
732                 sock->flag |= SOCK_UNAVAIL;
733                 sock= BLI_findlink(&node->outputs, RRES_OUT_ALPHA);
734                 sock->flag |= SOCK_UNAVAIL;
735         }
736         
737         sock= BLI_findlink(&node->outputs, RRES_OUT_Z);
738         if (!(passflag & SCE_PASS_Z)) sock->flag |= SOCK_UNAVAIL;
739         sock= BLI_findlink(&node->outputs, RRES_OUT_NORMAL);
740         if (!(passflag & SCE_PASS_NORMAL)) sock->flag |= SOCK_UNAVAIL;
741         sock= BLI_findlink(&node->outputs, RRES_OUT_VEC);
742         if (!(passflag & SCE_PASS_VECTOR)) sock->flag |= SOCK_UNAVAIL;
743         sock= BLI_findlink(&node->outputs, RRES_OUT_UV);
744         if (!(passflag & SCE_PASS_UV)) sock->flag |= SOCK_UNAVAIL;
745         sock= BLI_findlink(&node->outputs, RRES_OUT_RGBA);
746         if (!(passflag & SCE_PASS_RGBA)) sock->flag |= SOCK_UNAVAIL;
747         sock= BLI_findlink(&node->outputs, RRES_OUT_DIFF);
748         if (!(passflag & SCE_PASS_DIFFUSE)) sock->flag |= SOCK_UNAVAIL;
749         sock= BLI_findlink(&node->outputs, RRES_OUT_SPEC);
750         if (!(passflag & SCE_PASS_SPEC)) sock->flag |= SOCK_UNAVAIL;
751         sock= BLI_findlink(&node->outputs, RRES_OUT_SHADOW);
752         if (!(passflag & SCE_PASS_SHADOW)) sock->flag |= SOCK_UNAVAIL;
753         sock= BLI_findlink(&node->outputs, RRES_OUT_AO);
754         if (!(passflag & SCE_PASS_AO)) sock->flag |= SOCK_UNAVAIL;
755         sock= BLI_findlink(&node->outputs, RRES_OUT_REFLECT);
756         if (!(passflag & SCE_PASS_REFLECT)) sock->flag |= SOCK_UNAVAIL;
757         sock= BLI_findlink(&node->outputs, RRES_OUT_REFRACT);
758         if (!(passflag & SCE_PASS_REFRACT)) sock->flag |= SOCK_UNAVAIL;
759         sock= BLI_findlink(&node->outputs, RRES_OUT_INDIRECT);
760         if (!(passflag & SCE_PASS_INDIRECT)) sock->flag |= SOCK_UNAVAIL;
761         sock= BLI_findlink(&node->outputs, RRES_OUT_INDEXOB);
762         if (!(passflag & SCE_PASS_INDEXOB)) sock->flag |= SOCK_UNAVAIL;
763         sock= BLI_findlink(&node->outputs, RRES_OUT_INDEXMA);
764         if (!(passflag & SCE_PASS_INDEXMA)) sock->flag |= SOCK_UNAVAIL;
765         sock= BLI_findlink(&node->outputs, RRES_OUT_MIST);
766         if (!(passflag & SCE_PASS_MIST)) sock->flag |= SOCK_UNAVAIL;
767         sock= BLI_findlink(&node->outputs, RRES_OUT_EMIT);
768         if (!(passflag & SCE_PASS_EMIT)) sock->flag |= SOCK_UNAVAIL;
769         sock= BLI_findlink(&node->outputs, RRES_OUT_ENV);
770         if (!(passflag & SCE_PASS_ENVIRONMENT)) sock->flag |= SOCK_UNAVAIL;
771
772         sock= BLI_findlink(&node->outputs, RRES_OUT_DIFF_DIRECT);
773         if (!(passflag & SCE_PASS_DIFFUSE_DIRECT)) sock->flag |= SOCK_UNAVAIL;
774         sock= BLI_findlink(&node->outputs, RRES_OUT_DIFF_INDIRECT);
775         if (!(passflag & SCE_PASS_DIFFUSE_INDIRECT)) sock->flag |= SOCK_UNAVAIL;
776         sock= BLI_findlink(&node->outputs, RRES_OUT_DIFF_COLOR);
777         if (!(passflag & SCE_PASS_DIFFUSE_COLOR)) sock->flag |= SOCK_UNAVAIL;
778
779         sock= BLI_findlink(&node->outputs, RRES_OUT_GLOSSY_DIRECT);
780         if (!(passflag & SCE_PASS_GLOSSY_DIRECT)) sock->flag |= SOCK_UNAVAIL;
781         sock= BLI_findlink(&node->outputs, RRES_OUT_GLOSSY_INDIRECT);
782         if (!(passflag & SCE_PASS_GLOSSY_INDIRECT)) sock->flag |= SOCK_UNAVAIL;
783         sock= BLI_findlink(&node->outputs, RRES_OUT_GLOSSY_COLOR);
784         if (!(passflag & SCE_PASS_GLOSSY_COLOR)) sock->flag |= SOCK_UNAVAIL;
785
786         sock= BLI_findlink(&node->outputs, RRES_OUT_TRANSM_DIRECT);
787         if (!(passflag & SCE_PASS_TRANSM_DIRECT)) sock->flag |= SOCK_UNAVAIL;
788         sock= BLI_findlink(&node->outputs, RRES_OUT_TRANSM_INDIRECT);
789         if (!(passflag & SCE_PASS_TRANSM_INDIRECT)) sock->flag |= SOCK_UNAVAIL;
790         sock= BLI_findlink(&node->outputs, RRES_OUT_TRANSM_COLOR);
791         if (!(passflag & SCE_PASS_TRANSM_COLOR)) sock->flag |= SOCK_UNAVAIL;
792 }
793
794 /* based on rules, force sockets hidden always */
795 void ntreeCompositForceHidden(bNodeTree *ntree, Scene *curscene)
796 {
797         bNode *node;
798
799         if (ntree == NULL) return;
800
801         for (node = ntree->nodes.first; node; node = node->next) {
802                 if (node->type == CMP_NODE_R_LAYERS) {
803                         Scene *sce = node->id ? (Scene *)node->id : curscene;
804                         SceneRenderLayer *srl = BLI_findlink(&sce->r.layers, node->custom1);
805                         if (srl)
806                                 force_hidden_passes(node, srl->passflag);
807                 }
808                 /* XXX this stuff is called all the time, don't want that.
809                  * Updates should only happen when actually necessary.
810                  */
811                 #if 0
812                 else if (node->type == CMP_NODE_IMAGE) {
813                         nodeUpdate(ntree, node);
814                 }
815                 #endif
816         }
817
818 }
819
820 /* called from render pipeline, to tag render input and output */
821 /* need to do all scenes, to prevent errors when you re-render 1 scene */
822 void ntreeCompositTagRender(Scene *curscene)
823 {
824         Scene *sce;
825
826         for (sce = G.main->scene.first; sce; sce = sce->id.next) {
827                 if (sce->nodetree) {
828                         bNode *node;
829
830                         for (node = sce->nodetree->nodes.first; node; node = node->next) {
831                                 if (node->id == (ID *)curscene || node->type == CMP_NODE_COMPOSITE)
832                                         nodeUpdate(sce->nodetree, node);
833                                 else if (node->type == CMP_NODE_TEXTURE) /* uses scene sizex/sizey */
834                                         nodeUpdate(sce->nodetree, node);
835                         }
836                 }
837         }
838 }
839
840 static int node_animation_properties(bNodeTree *ntree, bNode *node)
841 {
842         bNodeSocket *sock;
843         const ListBase *lb;
844         Link *link;
845         PointerRNA ptr;
846         PropertyRNA *prop;
847
848         /* check to see if any of the node's properties have fcurves */
849         RNA_pointer_create((ID *)ntree, &RNA_Node, node, &ptr);
850         lb = RNA_struct_type_properties(ptr.type);
851
852         for (link = lb->first; link; link = link->next) {
853                 int driven, len = 1, index;
854                 prop = (PropertyRNA *)link;
855
856                 if (RNA_property_array_check(prop))
857                         len = RNA_property_array_length(&ptr, prop);
858
859                 for (index = 0; index < len; index++) {
860                         if (rna_get_fcurve(&ptr, prop, index, NULL, &driven)) {
861                                 nodeUpdate(ntree, node);
862                                 return 1;
863                         }
864                 }
865         }
866
867         /* now check node sockets */
868         for (sock = node->inputs.first; sock; sock = sock->next) {
869                 int driven, len = 1, index;
870
871                 RNA_pointer_create((ID *)ntree, &RNA_NodeSocket, sock, &ptr);
872                 prop = RNA_struct_find_property(&ptr, "default_value");
873                 if (prop) {
874                         if (RNA_property_array_check(prop))
875                                 len = RNA_property_array_length(&ptr, prop);
876
877                         for (index = 0; index < len; index++) {
878                                 if (rna_get_fcurve(&ptr, prop, index, NULL, &driven)) {
879                                         nodeUpdate(ntree, node);
880                                         return 1;
881                                 }
882                         }
883                 }
884         }
885
886         return 0;
887 }
888
889 /* tags nodes that have animation capabilities */
890 int ntreeCompositTagAnimated(bNodeTree *ntree)
891 {
892         bNode *node;
893         int tagged = 0;
894
895         if (ntree == NULL) return 0;
896
897         for (node = ntree->nodes.first; node; node = node->next) {
898
899                 tagged = node_animation_properties(ntree, node);
900
901                 /* otherwise always tag these node types */
902                 if (node->type == CMP_NODE_IMAGE) {
903                         Image *ima = (Image *)node->id;
904                         if (ima && ELEM(ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE)) {
905                                 nodeUpdate(ntree, node);
906                                 tagged = 1;
907                         }
908                 }
909                 else if (node->type == CMP_NODE_TIME) {
910                         nodeUpdate(ntree, node);
911                         tagged = 1;
912                 }
913                 /* here was tag render layer, but this is called after a render, so re-composites fail */
914                 else if (node->type == NODE_GROUP) {
915                         if (ntreeCompositTagAnimated((bNodeTree *)node->id) ) {
916                                 nodeUpdate(ntree, node);
917                         }
918                 }
919                 else if (ELEM(node->type, CMP_NODE_MOVIECLIP, CMP_NODE_TRANSFORM)) {
920                         nodeUpdate(ntree, node);
921                         tagged = 1;
922                 }
923                 else if (node->type == CMP_NODE_MASK) {
924                         nodeUpdate(ntree, node);
925                         tagged = 1;
926                 }
927         }
928
929         return tagged;
930 }
931
932
933 /* called from image window preview */
934 void ntreeCompositTagGenerators(bNodeTree *ntree)
935 {
936         bNode *node;
937
938         if (ntree == NULL) return;
939
940         for (node = ntree->nodes.first; node; node = node->next) {
941                 if (ELEM(node->type, CMP_NODE_R_LAYERS, CMP_NODE_IMAGE))
942                         nodeUpdate(ntree, node);
943         }
944 }
945
946 /* XXX after render animation system gets a refresh, this call allows composite to end clean */
947 void ntreeCompositClearTags(bNodeTree *ntree)
948 {
949         bNode *node;
950
951         if (ntree == NULL) return;
952
953         for (node = ntree->nodes.first; node; node = node->next) {
954                 node->need_exec = 0;
955                 if (node->type == NODE_GROUP)
956                         ntreeCompositClearTags((bNodeTree *)node->id);
957         }
958 }