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