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