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