Durian request:
[blender.git] / source / blender / blenkernel / intern / node.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2005 Blender Foundation.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): Bob Holcomb.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #ifndef DISABLE_PYTHON
31 #include <Python.h>
32 #endif
33
34 #include <stdlib.h>
35 #include <stddef.h>
36 #include <string.h>
37
38 #include "DNA_ID.h"
39 #include "DNA_image_types.h"
40 #include "DNA_node_types.h"
41 #include "DNA_material_types.h"
42 #include "DNA_texture_types.h"
43 #include "DNA_text_types.h"
44 #include "DNA_scene_types.h"
45
46 #include "BKE_blender.h"
47 #include "BKE_colortools.h"
48 #include "BKE_global.h"
49 #include "BKE_image.h"
50 #include "BKE_library.h"
51 #include "BKE_main.h"
52 #include "BKE_node.h"
53 #include "BKE_texture.h"
54 #include "BKE_text.h"
55 #include "BKE_utildefines.h"
56
57 #include "BLI_math.h"
58 #include "BLI_blenlib.h"
59 #include "BLI_rand.h"
60 #include "BLI_threads.h"
61
62 #include "PIL_time.h"
63
64 #include "MEM_guardedalloc.h"
65 #include "IMB_imbuf.h"
66
67 #include "RE_pipeline.h"
68 #include "RE_shader_ext.h"              /* <- TexResult */
69 #include "RE_render_ext.h"              /* <- ibuf_sample() */
70
71 #include "CMP_node.h"
72 #include "intern/CMP_util.h"    /* stupid include path... */
73
74 #include "SHD_node.h"
75 #include "TEX_node.h"
76 #include "intern/TEX_util.h"
77
78 #include "GPU_extensions.h"
79 #include "GPU_material.h"
80
81 static ListBase empty_list = {NULL, NULL};
82 ListBase node_all_composit = {NULL, NULL};
83 ListBase node_all_shaders = {NULL, NULL};
84 ListBase node_all_textures = {NULL, NULL};
85
86 /* ************** Type stuff **********  */
87
88 static bNodeType *node_get_type(bNodeTree *ntree, int type, bNodeTree *ngroup, ID *id)
89 {
90         if(type==NODE_GROUP) {
91                 if(ngroup && GS(ngroup->id.name)==ID_NT) {
92                         return ngroup->owntype;
93                 }
94                 return NULL;
95         }
96         else {
97                 bNodeType *ntype = ntree->alltypes.first;
98                 for(; ntype; ntype= ntype->next)
99                         if(ntype->type==type && id==ntype->id )
100                                 return ntype;
101                 
102                 return NULL;
103         }
104 }
105
106 void ntreeInitTypes(bNodeTree *ntree)
107 {
108         bNode *node, *next;
109         
110         if(ntree->type==NTREE_SHADER)
111                 ntree->alltypes= node_all_shaders;
112         else if(ntree->type==NTREE_COMPOSIT)
113                 ntree->alltypes= node_all_composit;
114         else if(ntree->type==NTREE_TEXTURE)
115                 ntree->alltypes= node_all_textures;
116         else {
117                 ntree->alltypes= empty_list;
118                 printf("Error: no type definitions for nodes\n");
119         }
120         
121         for(node= ntree->nodes.first; node; node= next) {
122                 next= node->next;
123                 if(node->type==NODE_DYNAMIC) {
124                         bNodeType *stype= NULL;
125                         if(node->id==NULL) { /* empty script node */
126                                 stype= node_get_type(ntree, node->type, NULL, NULL);
127                         } else { /* not an empty script node */
128                                 stype= node_get_type(ntree, node->type, NULL, node->id);
129                                 if(!stype) {
130                                         stype= node_get_type(ntree, node->type, NULL, NULL);
131                                         /* needed info if the pynode script fails now: */
132                                         if (node->id) node->storage= ntree;
133                                 } else {
134                                         node->custom1= 0;
135                                         node->custom1= BSET(node->custom1,NODE_DYNAMIC_ADDEXIST);
136                                 }
137                         }
138                         node->typeinfo= stype;
139                         node->typeinfo->initfunc(node);
140                 } else {
141                         node->typeinfo= node_get_type(ntree, node->type, (bNodeTree *)node->id, NULL);
142                 }
143
144                 if(node->typeinfo==NULL) {
145                         printf("Error: Node type %s doesn't exist anymore, removed\n", node->name);
146                         nodeFreeNode(ntree, node);
147                 }
148         }
149                         
150         ntree->init |= NTREE_TYPE_INIT;
151 }
152
153 /* updates node with (modified) bNodeType.. this should be done for all trees */
154 void ntreeUpdateType(bNodeTree *ntree, bNodeType *ntype)
155 {
156         bNode *node;
157
158         for(node= ntree->nodes.first; node; node= node->next) {
159                 if(node->typeinfo== ntype) {
160                         nodeUpdateType(ntree, node, ntype);
161                 }
162         }
163 }
164
165 /* only used internal... we depend on type definitions! */
166 static bNodeSocket *node_add_socket_type(ListBase *lb, bNodeSocketType *stype)
167 {
168         bNodeSocket *sock= MEM_callocN(sizeof(bNodeSocket), "sock");
169         
170         BLI_strncpy(sock->name, stype->name, NODE_MAXSTR);
171         if(stype->limit==0) sock->limit= 0xFFF;
172         else sock->limit= stype->limit;
173         sock->type= stype->type;
174         
175         sock->to_index= stype->own_index;
176         sock->tosock= stype->internsock;
177         
178         sock->ns.vec[0]= stype->val1;
179         sock->ns.vec[1]= stype->val2;
180         sock->ns.vec[2]= stype->val3;
181         sock->ns.vec[3]= stype->val4;
182         sock->ns.min= stype->min;
183         sock->ns.max= stype->max;
184         
185         if(lb)
186                 BLI_addtail(lb, sock);
187
188         return sock;
189 }
190
191 static void node_rem_socket(bNodeTree *ntree, ListBase *lb, bNodeSocket *sock)
192 {
193         bNodeLink *link, *next;
194         
195         for(link= ntree->links.first; link; link= next) {
196                 next= link->next;
197                 if(link->fromsock==sock || link->tosock==sock) {
198                         nodeRemLink(ntree, link);
199                 }
200         }
201         
202         BLI_remlink(lb, sock);
203         MEM_freeN(sock);
204 }
205
206 static bNodeSocket *verify_socket(ListBase *lb, bNodeSocketType *stype)
207 {
208         bNodeSocket *sock;
209         
210         for(sock= lb->first; sock; sock= sock->next) {
211                 /* both indices are zero for non-groups, otherwise it's a unique index */
212                 if(sock->to_index==stype->own_index)
213                         if(strncmp(sock->name, stype->name, NODE_MAXSTR)==0)
214                                 break;
215         }
216         if(sock) {
217                 sock->type= stype->type;                /* in future, read this from tydefs! */
218                 if(stype->limit==0) sock->limit= 0xFFF;
219                 else sock->limit= stype->limit;
220                 sock->ns.min= stype->min;
221                 sock->ns.max= stype->max;
222                 sock->tosock= stype->internsock;
223                 
224                 BLI_remlink(lb, sock);
225                 
226                 return sock;
227         }
228         else {
229                 return node_add_socket_type(NULL, stype);
230         }
231 }
232
233 static void verify_socket_list(bNodeTree *ntree, ListBase *lb, bNodeSocketType *stype_first)
234 {
235         bNodeSocketType *stype;
236         
237         /* no inputs anymore? */
238         if(stype_first==NULL) {
239                 while(lb->first)
240                         node_rem_socket(ntree, lb, lb->first);
241         }
242         else {
243                 /* step by step compare */
244                 stype= stype_first;
245                 while(stype->type != -1) {
246                         stype->sock= verify_socket(lb, stype);
247                         stype++;
248                 }
249                 /* leftovers are removed */
250                 while(lb->first)
251                         node_rem_socket(ntree, lb, lb->first);
252                 /* and we put back the verified sockets */
253                 stype= stype_first;
254                 while(stype->type != -1) {
255                         BLI_addtail(lb, stype->sock);
256                         stype++;
257                 }
258         }
259 }
260
261 void nodeVerifyType(bNodeTree *ntree, bNode *node)
262 {
263         bNodeType *ntype= node->typeinfo;
264         
265         if(ntype) {
266                 /* might add some other verify stuff here */
267                 
268                 verify_socket_list(ntree, &node->inputs, ntype->inputs);
269                 verify_socket_list(ntree, &node->outputs, ntype->outputs);
270         }
271 }
272
273 void ntreeVerifyTypes(bNodeTree *ntree)
274 {
275         bNode *node;
276         
277         /* if((ntree->init & NTREE_TYPE_INIT)==0) */
278         ntreeInitTypes(ntree);
279
280         /* check inputs and outputs, and remove or insert them */
281         for(node= ntree->nodes.first; node; node= node->next)
282                 nodeVerifyType(ntree, node);
283         
284 }
285
286 /* ************** Group stuff ********** */
287
288 bNodeType node_group_typeinfo= {
289         /* next,prev   */       NULL, NULL,
290         /* type code   */       NODE_GROUP,
291         /* name        */       "Group",
292         /* width+range */       120, 60, 200,
293         /* class+opts  */       NODE_CLASS_GROUP, NODE_OPTIONS,
294         /* input sock  */       NULL,
295         /* output sock */       NULL,
296         /* storage     */       "",
297         /* execfunc    */       NULL,
298         /* butfunc     */       NULL,
299         /* initfunc    */       NULL,
300         /* freestoragefunc    */        NULL,
301         /* copystoragefunc    */        NULL,
302         /* id          */       NULL
303 };
304
305 /* tag internal sockets */
306 static void group_tag_internal_sockets(bNodeTree *ngroup)
307 {
308         bNode *node;
309         bNodeSocket *sock;
310         bNodeLink *link;
311         
312         /* clear intern tag, but check already for hidden sockets */
313         for(node= ngroup->nodes.first; node; node= node->next) {
314                 for(sock= node->inputs.first; sock; sock= sock->next)
315                         sock->intern= sock->flag & SOCK_HIDDEN;
316                 for(sock= node->outputs.first; sock; sock= sock->next)
317                         sock->intern= sock->flag & (SOCK_HIDDEN|SOCK_UNAVAIL);
318         }
319         /* set tag */
320         for(link= ngroup->links.first; link; link= link->next) {
321                 link->fromsock->intern= 1;
322                 link->tosock->intern= 1;
323         }
324         
325         /* remove link pointer to external links (only happens on create group) */
326         for(node= ngroup->nodes.first; node; node= node->next) {
327                 for(sock= node->inputs.first; sock; sock= sock->next)
328                         if(sock->intern==0)
329                                 sock->link= NULL;
330         }
331
332         /* set all intern sockets to own_index zero, makes sure that later use won't mixup */
333         for(node= ngroup->nodes.first; node; node= node->next) {
334                 for(sock= node->inputs.first; sock; sock= sock->next)
335                         if(sock->intern)
336                                 sock->own_index= 0;
337                 for(sock= node->outputs.first; sock; sock= sock->next)
338                         if(sock->intern)
339                                 sock->own_index= 0;
340         }
341 }
342
343 /* after editing group, new sockets are zero */
344 /* this routine ensures unique identifiers for zero sockets that are exposed */
345 static void group_verify_own_indices(bNodeTree *ngroup)
346 {
347         bNode *node;
348         bNodeSocket *sock;
349         
350         for(node= ngroup->nodes.first; node; node= node->next) {
351                 for(sock= node->inputs.first; sock; sock= sock->next)
352                         if(sock->own_index==0 && sock->intern==0)
353                                 sock->own_index= ++(ngroup->cur_index);
354                 for(sock= node->outputs.first; sock; sock= sock->next)
355                         if(sock->own_index==0 && sock->intern==0)
356                                 sock->own_index= ++(ngroup->cur_index);
357         }
358         //printf("internal index %d\n", ngroup->cur_index);
359 }
360
361
362 /* nodetrees can be used as groups, so we need typeinfo structs generated */
363 void ntreeMakeOwnType(bNodeTree *ngroup)
364 {
365         bNode *node;
366         bNodeSocket *sock;
367         int totin= 0, totout=0, a;
368
369         /* tags socket when internal linked */
370         group_tag_internal_sockets(ngroup);
371         
372         /* ensure all sockets have own unique id */
373         group_verify_own_indices(ngroup);
374         
375         /* counting stats */
376         for(node= ngroup->nodes.first; node; node= node->next) {
377                 if(node->type==NODE_GROUP)
378                         break;
379                 for(sock= node->inputs.first; sock; sock= sock->next)
380                         if(sock->intern==0) 
381                                 totin++;
382                 for(sock= node->outputs.first; sock; sock= sock->next)
383                         if(sock->intern==0) 
384                                 totout++;
385         }
386         /* debug: nodetrees in nodetrees not handled yet */
387         if(node) {
388                 printf("group in group, not supported yet\n");
389                 return;
390         }
391         
392         /* free own type struct */
393         if(ngroup->owntype) {
394                 if(ngroup->owntype->inputs)
395                         MEM_freeN(ngroup->owntype->inputs);
396                 if(ngroup->owntype->outputs)
397                         MEM_freeN(ngroup->owntype->outputs);
398                 MEM_freeN(ngroup->owntype);
399         }
400         
401         /* make own type struct */
402         ngroup->owntype= MEM_callocN(sizeof(bNodeType), "group type");
403         *ngroup->owntype= node_group_typeinfo; /* copy data, for init */
404         
405         /* input type arrays */
406         if(totin) {
407                 bNodeSocketType *stype;
408                 bNodeSocketType *inputs= MEM_callocN(sizeof(bNodeSocketType)*(totin+1), "bNodeSocketType");
409                 a= 0;
410                 
411                 for(node= ngroup->nodes.first; node; node= node->next) {
412                         /* nodes are presumed fully verified, stype and socket list are in sync */
413                         stype= node->typeinfo->inputs;
414                         for(sock= node->inputs.first; sock; sock= sock->next, stype++) {
415                                 if(sock->intern==0) {
416                                         /* debug only print */
417                                         if(stype==NULL || stype->type==-1) printf("group verification error %s\n", ngroup->id.name);
418                                         
419                                         inputs[a]= *stype;
420                                         inputs[a].own_index= sock->own_index;
421                                         inputs[a].internsock= sock;     
422                                         a++;
423                                 }
424                         }
425                 }
426                 inputs[a].type= -1;     /* terminator code */
427                 ngroup->owntype->inputs= inputs;
428         }       
429         
430         /* output type arrays */
431         if(totout) {
432                 bNodeSocketType *stype;
433                 bNodeSocketType *outputs= MEM_callocN(sizeof(bNodeSocketType)*(totout+1), "bNodeSocketType");
434                 a= 0;
435                 
436                 for(node= ngroup->nodes.first; node; node= node->next) {
437                         /* nodes are presumed fully verified, stype and socket list are in sync */
438                         stype= node->typeinfo->outputs;
439                         for(sock= node->outputs.first; sock; sock= sock->next, stype++) {
440                                 if(sock->intern==0) {
441                                         /* debug only print */
442                                         if(stype==NULL || stype->type==-1) printf("group verification error %s\n", ngroup->id.name);
443                                         
444                                         outputs[a]= *stype;
445                                         outputs[a].own_index= sock->own_index;
446                                         outputs[a].internsock= sock;    
447                                         a++;
448                                 }
449                         }
450                 }
451                 outputs[a].type= -1;    /* terminator code */
452                 ngroup->owntype->outputs= outputs;
453         }
454         
455         /* voila, the nodetree has the full definition for generating group-node instances! */
456 }
457
458
459 static bNodeSocket *groupnode_find_tosock(bNode *gnode, int index)
460 {
461         bNodeSocket *sock;
462         
463         for(sock= gnode->inputs.first; sock; sock= sock->next)
464                 if(sock->to_index==index)
465                         return sock;
466         return NULL;
467 }
468
469 static bNodeSocket *groupnode_find_fromsock(bNode *gnode, int index)
470 {
471         bNodeSocket *sock;
472         
473         for(sock= gnode->outputs.first; sock; sock= sock->next)
474                 if(sock->to_index==index)
475                         return sock;
476         return NULL;
477 }
478
479 bNode *nodeMakeGroupFromSelected(bNodeTree *ntree)
480 {
481         bNodeLink *link, *linkn;
482         bNode *node, *gnode, *nextn;
483         bNodeSocket *sock;
484         bNodeTree *ngroup;
485         float min[2], max[2];
486         int totnode=0;
487         
488         INIT_MINMAX2(min, max);
489         
490         /* is there something to group? also do some clearing */
491         for(node= ntree->nodes.first; node; node= node->next) {
492                 if(node->flag & NODE_SELECT) {
493                         /* no groups in groups */
494                         if(node->type==NODE_GROUP)
495                                 return NULL;
496                         DO_MINMAX2( (&node->locx), min, max);
497                         totnode++;
498                 }
499                 node->done= 0;
500         }
501         if(totnode==0) return NULL;
502         
503         /* check if all connections are OK, no unselected node has both
504                 inputs and outputs to a selection */
505         for(link= ntree->links.first; link; link= link->next) {
506                 if(link->fromnode->flag & NODE_SELECT)
507                         link->tonode->done |= 1;
508                 if(link->tonode->flag & NODE_SELECT)
509                         link->fromnode->done |= 2;
510         }       
511         
512         for(node= ntree->nodes.first; node; node= node->next) {
513                 if((node->flag & NODE_SELECT)==0)
514                         if(node->done==3)
515                                 break;
516         }
517         if(node) 
518                 return NULL;
519         
520         /* OK! new nodetree */
521         ngroup= alloc_libblock(&G.main->nodetree, ID_NT, "NodeGroup");
522         ngroup->type= ntree->type;
523         ngroup->alltypes= ntree->alltypes;
524         
525         /* move nodes over */
526         for(node= ntree->nodes.first; node; node= nextn) {
527                 nextn= node->next;
528                 if(node->flag & NODE_SELECT) {
529                         BLI_remlink(&ntree->nodes, node);
530                         BLI_addtail(&ngroup->nodes, node);
531                         node->locx-= 0.5f*(min[0]+max[0]);
532                         node->locy-= 0.5f*(min[1]+max[1]);
533
534                         /* set selin and selout of the nodetree */
535                         for(sock= node->inputs.first; sock; sock= sock->next) {
536                                 if(sock->flag & SOCK_SEL) {
537                                         ngroup->selin= sock;
538                                         break;
539                                 }
540                         }
541                         for(sock= node->outputs.first; sock; sock= sock->next) {
542                                 if(sock->flag & SOCK_SEL) {
543                                         ngroup->selout= sock;
544                                         break;
545                                 }
546                         }
547
548                         /* set socket own_index to zero since it can still have a value
549                          * from being in a group before, otherwise it doesn't get a unique
550                          * index in group_verify_own_indices */
551                         for(sock= node->inputs.first; sock; sock= sock->next)
552                                 sock->own_index= 0;
553                         for(sock= node->outputs.first; sock; sock= sock->next)
554                                 sock->own_index= 0;
555                 }
556         }
557
558         /* move links over */
559         for(link= ntree->links.first; link; link= linkn) {
560                 linkn= link->next;
561                 if(link->fromnode->flag & link->tonode->flag & NODE_SELECT) {
562                         BLI_remlink(&ntree->links, link);
563                         BLI_addtail(&ngroup->links, link);
564                 }
565         }
566         
567         /* now we can make own group typeinfo */
568         ntreeMakeOwnType(ngroup);
569         
570         /* make group node */
571         gnode= nodeAddNodeType(ntree, NODE_GROUP, ngroup, NULL);
572         gnode->locx= 0.5f*(min[0]+max[0]);
573         gnode->locy= 0.5f*(min[1]+max[1]);
574         
575         /* relink external sockets */
576         for(link= ntree->links.first; link; link= linkn) {
577                 linkn= link->next;
578                 
579                 if(link->tonode->flag & NODE_SELECT) {
580                         link->tonode= gnode;
581                         sock= groupnode_find_tosock(gnode, link->tosock->own_index);
582                         if(sock==NULL) {
583                                 nodeRemLink(ntree, link);       
584                                 printf("Removed link, cannot mix internal and external sockets in group\n");
585                         }
586                         else link->tosock= sock;
587                 }
588                 else if(link->fromnode->flag & NODE_SELECT) {
589                         link->fromnode= gnode;
590                         sock= groupnode_find_fromsock(gnode, link->fromsock->own_index);
591                         if(sock==NULL) {
592                                 nodeRemLink(ntree, link);       
593                                 printf("Removed link, cannot mix internal and external sockets in group\n");
594                         }
595                         else link->fromsock= sock;
596                 }
597         }
598         
599         /* initialize variables of unused input sockets */
600         for(node= ngroup->nodes.first; node; node= node->next) {
601                 for(sock= node->inputs.first; sock; sock= sock->next) {
602                         if(sock->intern==0) {
603                                 bNodeSocket *nsock= groupnode_find_tosock(gnode, sock->own_index);
604                                 if(nsock) {
605                                         QUATCOPY(nsock->ns.vec, sock->ns.vec);
606                                 }
607                         }
608                 }
609         }
610
611         /* update node levels */
612         ntreeSolveOrder(ntree);
613
614         return gnode;
615 }
616
617 /* note: ungroup: group_indices zero! */
618
619 /* here's a nasty little one, need to check users... */
620 /* should become callbackable... */
621 void nodeVerifyGroup(bNodeTree *ngroup)
622 {
623         
624         /* group changed, so we rebuild the type definition */
625         ntreeMakeOwnType(ngroup);
626         
627         if(ngroup->type==NTREE_SHADER) {
628                 Material *ma;
629                 for(ma= G.main->mat.first; ma; ma= ma->id.next) {
630                         if(ma->nodetree) {
631                                 bNode *node;
632                                 
633                                 /* find if group is in tree */
634                                 for(node= ma->nodetree->nodes.first; node; node= node->next)
635                                         if(node->id == (ID *)ngroup)
636                                                 break;
637                                 
638                                 if(node) {
639                                         /* set all type pointers OK */
640                                         ntreeInitTypes(ma->nodetree);
641                                         
642                                         for(node= ma->nodetree->nodes.first; node; node= node->next)
643                                                 if(node->id == (ID *)ngroup)
644                                                         nodeVerifyType(ma->nodetree, node);
645                                 }
646                         }
647                 }
648         }
649         else if(ngroup->type==NTREE_COMPOSIT) {
650                 Scene *sce;
651                 for(sce= G.main->scene.first; sce; sce= sce->id.next) {
652                         if(sce->nodetree) {
653                                 bNode *node;
654                                 
655                                 /* find if group is in tree */
656                                 for(node= sce->nodetree->nodes.first; node; node= node->next)
657                                         if(node->id == (ID *)ngroup)
658                                                 break;
659                                 
660                                 if(node) {
661                                         /* set all type pointers OK */
662                                         ntreeInitTypes(sce->nodetree);
663                                         
664                                         for(node= sce->nodetree->nodes.first; node; node= node->next)
665                                                 if(node->id == (ID *)ngroup)
666                                                         nodeVerifyType(sce->nodetree, node);
667                                 }
668                         }
669                 }
670         }
671         else if(ngroup->type==NTREE_TEXTURE) {
672                 Tex *tx;
673                 for(tx= G.main->tex.first; tx; tx= tx->id.next) {
674                         if(tx->nodetree) {
675                                 bNode *node;
676                                 
677                                 /* find if group is in tree */
678                                 for(node= tx->nodetree->nodes.first; node; node= node->next)
679                                         if(node->id == (ID *)ngroup)
680                                                 break;
681                                 
682                                 if(node) {
683                                         /* set all type pointers OK */
684                                         ntreeInitTypes(tx->nodetree);
685                                         
686                                         for(node= tx->nodetree->nodes.first; node; node= node->next)
687                                                 if(node->id == (ID *)ngroup)
688                                                         nodeVerifyType(tx->nodetree, node);
689                                 }
690                         }
691                 }
692         }
693 }
694
695 /* also to check all users of groups. Now only used in editor for hide/unhide */
696 /* should become callbackable? */
697 void nodeGroupSocketUseFlags(bNodeTree *ngroup)
698 {
699         bNode *node;
700         bNodeSocket *sock;
701
702         /* clear flags */
703         for(node= ngroup->nodes.first; node; node= node->next) {
704                 for(sock= node->inputs.first; sock; sock= sock->next)
705                         sock->flag &= ~SOCK_IN_USE;
706                 for(sock= node->outputs.first; sock; sock= sock->next)
707                         sock->flag &= ~SOCK_IN_USE;
708         }
709         
710         /* tag all thats in use */
711         if(ngroup->type==NTREE_SHADER) {
712                 Material *ma;
713                 for(ma= G.main->mat.first; ma; ma= ma->id.next) {
714                         if(ma->nodetree) {
715                                 for(node= ma->nodetree->nodes.first; node; node= node->next) {
716                                         if(node->id==(ID *)ngroup) {
717                                                 for(sock= node->inputs.first; sock; sock= sock->next)
718                                                         if(sock->link)
719                                                                 if(sock->tosock) 
720                                                                         sock->tosock->flag |= SOCK_IN_USE;
721                                                 for(sock= node->outputs.first; sock; sock= sock->next)
722                                                         if(nodeCountSocketLinks(ma->nodetree, sock))
723                                                                 if(sock->tosock) 
724                                                                         sock->tosock->flag |= SOCK_IN_USE;
725                                         }
726                                 }
727                         }
728                 }
729         }
730         else if(ngroup->type==NTREE_COMPOSIT) {
731                 Scene *sce;
732                 for(sce= G.main->scene.first; sce; sce= sce->id.next) {
733                         if(sce->nodetree) {
734                                 for(node= sce->nodetree->nodes.first; node; node= node->next) {
735                                         if(node->id==(ID *)ngroup) {
736                                                 for(sock= node->inputs.first; sock; sock= sock->next)
737                                                         if(sock->link)
738                                                                 if(sock->tosock) 
739                                                                         sock->tosock->flag |= SOCK_IN_USE;
740                                                 for(sock= node->outputs.first; sock; sock= sock->next)
741                                                         if(nodeCountSocketLinks(sce->nodetree, sock))
742                                                                 if(sock->tosock) 
743                                                                         sock->tosock->flag |= SOCK_IN_USE;
744                                         }
745                                 }
746                         }
747                 }
748         }
749         else if(ngroup->type==NTREE_TEXTURE) {
750                 Tex *tx;
751                 for(tx= G.main->tex.first; tx; tx= tx->id.next) {
752                         if(tx->nodetree) {
753                                 for(node= tx->nodetree->nodes.first; node; node= node->next) {
754                                         if(node->id==(ID *)ngroup) {
755                                                 for(sock= node->inputs.first; sock; sock= sock->next)
756                                                         if(sock->link)
757                                                                 if(sock->tosock) 
758                                                                         sock->tosock->flag |= SOCK_IN_USE;
759                                                 for(sock= node->outputs.first; sock; sock= sock->next)
760                                                         if(nodeCountSocketLinks(tx->nodetree, sock))
761                                                                 if(sock->tosock) 
762                                                                         sock->tosock->flag |= SOCK_IN_USE;
763                                         }
764                                 }
765                         }
766                 }
767         }
768         
769 }
770 /* finds a node based on its name */
771 bNode *nodeFindNodebyName(bNodeTree *ntree, const char *name)
772 {
773         bNode *node=NULL;
774         
775         for(node= ntree->nodes.first; node; node= node->next) {
776                 if (strcmp(name, node->name) == 0)
777                         break;
778         }
779         return node;
780 }
781
782 /* finds a node based on given socket */
783 int nodeFindNode(bNodeTree *ntree, bNodeSocket *sock, bNode **nodep, int *sockindex)
784 {
785         bNode *node;
786         bNodeSocket *tsock;
787         int index= 0;
788         
789         for(node= ntree->nodes.first; node; node= node->next) {
790                 for(index=0, tsock= node->inputs.first; tsock; tsock= tsock->next, index++)
791                         if(tsock==sock)
792                                 break;
793                 if(tsock)
794                         break;
795                 for(index=0, tsock= node->outputs.first; tsock; tsock= tsock->next, index++)
796                         if(tsock==sock)
797                                 break;
798                 if(tsock)
799                         break;
800         }
801
802         if(node) {
803                 *nodep= node;
804                 if(sockindex) *sockindex= index;
805                 return 1;
806         }
807         
808         *nodep= NULL;
809         return 0;
810 }
811
812 /* returns 1 if its OK */
813 int nodeGroupUnGroup(bNodeTree *ntree, bNode *gnode)
814 {
815         bNodeLink *link, *linkn;
816         bNode *node, *nextn;
817         bNodeTree *ngroup, *wgroup;
818         int index;
819         
820         ngroup= (bNodeTree *)gnode->id;
821         if(ngroup==NULL) return 0;
822         
823         /* clear new pointers, set in copytree */
824         for(node= ntree->nodes.first; node; node= node->next)
825                 node->new_node= NULL;
826
827         wgroup= ntreeCopyTree(ngroup, 0);
828         
829         /* add the nodes into the ntree */
830         for(node= wgroup->nodes.first; node; node= nextn) {
831                 nextn= node->next;
832                 BLI_remlink(&wgroup->nodes, node);
833                 BLI_addtail(&ntree->nodes, node);
834                 node->locx+= gnode->locx;
835                 node->locy+= gnode->locy;
836                 node->flag |= NODE_SELECT;
837         }
838         /* and the internal links */
839         for(link= wgroup->links.first; link; link= linkn) {
840                 linkn= link->next;
841                 BLI_remlink(&wgroup->links, link);
842                 BLI_addtail(&ntree->links, link);
843         }
844
845         /* restore links to and from the gnode */
846         for(link= ntree->links.first; link; link= link->next) {
847                 if(link->tonode==gnode) {
848                         /* link->tosock->tosock is on the node we look for */
849                         nodeFindNode(ngroup, link->tosock->tosock, &nextn, &index);
850                         if(nextn==NULL) printf("wrong stuff!\n");
851                         else if(nextn->new_node==NULL) printf("wrong stuff too!\n");
852                         else {
853                                 link->tonode= nextn->new_node;
854                                 link->tosock= BLI_findlink(&link->tonode->inputs, index);
855                         }
856                 }
857                 else if(link->fromnode==gnode) {
858                         /* link->fromsock->tosock is on the node we look for */
859                         nodeFindNode(ngroup, link->fromsock->tosock, &nextn, &index);
860                         if(nextn==NULL) printf("1 wrong stuff!\n");
861                         else if(nextn->new_node==NULL) printf("1 wrong stuff too!\n");
862                         else {
863                                 link->fromnode= nextn->new_node;
864                                 link->fromsock= BLI_findlink(&link->fromnode->outputs, index);
865                         }
866                 }
867         }
868         
869         /* remove the gnode & work tree */
870         free_libblock(&G.main->nodetree, wgroup);
871         
872         nodeFreeNode(ntree, gnode);
873         
874         /* solve order goes fine, but the level tags not... doing it twice works for now. solve this once */
875         ntreeSolveOrder(ntree);
876         ntreeSolveOrder(ntree);
877
878         return 1;
879 }
880
881 void nodeCopyGroup(bNode *gnode)
882 {
883         bNodeSocket *sock;
884
885         gnode->id->us--;
886         gnode->id= (ID *)ntreeCopyTree((bNodeTree *)gnode->id, 0);
887
888         /* new_sock was set in nodeCopyNode */
889         for(sock=gnode->inputs.first; sock; sock=sock->next)
890                 if(sock->tosock)
891                         sock->tosock= sock->tosock->new_sock;
892
893         for(sock=gnode->outputs.first; sock; sock=sock->next)
894                 if(sock->tosock)
895                         sock->tosock= sock->tosock->new_sock;
896 }
897
898 /* ************** Add stuff ********** */
899 void nodeAddSockets(bNode *node, bNodeType *ntype)
900 {
901         bNodeSocketType *stype;
902
903         if(ntype->inputs) {
904                 stype= ntype->inputs;
905                 while(stype->type != -1) {
906                         node_add_socket_type(&node->inputs, stype);
907                         stype++;
908                 }
909         }
910         if(ntype->outputs) {
911                 stype= ntype->outputs;
912                 while(stype->type != -1) {
913                         node_add_socket_type(&node->outputs, stype);
914                         stype++;
915                 }
916         }
917 }
918 /* Find the first available, non-duplicate name for a given node */
919 void nodeUniqueName(bNodeTree *ntree, bNode *node)
920 {
921         BLI_uniquename(&ntree->nodes, node, "Node", '.', offsetof(bNode, name), 32);
922 }
923
924 bNode *nodeAddNodeType(bNodeTree *ntree, int type, bNodeTree *ngroup, ID *id)
925 {
926         bNode *node= NULL;
927         bNodeType *ntype= NULL;
928
929         if(type>=NODE_DYNAMIC_MENU) {
930                 int a=0, idx= type-NODE_DYNAMIC_MENU;
931                 ntype= ntree->alltypes.first;
932                 while(ntype) {
933                         if(ntype->type==NODE_DYNAMIC) {
934                                 if(a==idx)
935                                         break;
936                                 a++;
937                         }
938                         ntype= ntype->next;
939                 }
940         } else
941                 ntype= node_get_type(ntree, type, ngroup, id);
942
943         node= MEM_callocN(sizeof(bNode), "new node");
944         BLI_addtail(&ntree->nodes, node);
945         node->typeinfo= ntype;
946         if(type>=NODE_DYNAMIC_MENU)
947                 node->custom2= type; /* for node_dynamic_init */
948
949         if(ngroup)
950                 BLI_strncpy(node->name, ngroup->id.name+2, NODE_MAXSTR);
951         else if(type>NODE_DYNAMIC_MENU) {
952                 BLI_strncpy(node->name, ntype->id->name+2, NODE_MAXSTR);
953         }
954         else
955                 BLI_strncpy(node->name, ntype->name, NODE_MAXSTR);
956
957         nodeUniqueName(ntree, node);
958         
959         node->type= ntype->type;
960         node->flag= NODE_SELECT|ntype->flag;
961         node->width= ntype->width;
962         node->miniwidth= 42.0f;         /* small value only, allows print of first chars */
963
964         if(type==NODE_GROUP)
965                 node->id= (ID *)ngroup;
966
967         /* need init handler later? */
968         /* got it-bob*/
969         if(ntype->initfunc!=NULL)
970                 ntype->initfunc(node);
971         
972         nodeAddSockets(node, ntype);
973         
974         return node;
975 }
976
977 void nodeMakeDynamicType(bNode *node)
978 {
979         /* find SH_DYNAMIC_NODE ntype */
980         bNodeType *ntype= node_all_shaders.first;
981         while(ntype) {
982                 if(ntype->type==NODE_DYNAMIC && ntype->id==NULL)
983                         break;
984                 ntype= ntype->next;
985         }
986
987         /* make own type struct to fill */
988         if(ntype) {
989                 /*node->typeinfo= MEM_dupallocN(ntype);*/
990                 bNodeType *newtype= MEM_callocN(sizeof(bNodeType), "dynamic bNodeType");
991                 *newtype= *ntype;
992                 newtype->name= BLI_strdup(ntype->name);
993                 node->typeinfo= newtype;
994         }
995 }
996
997 void nodeUpdateType(bNodeTree *ntree, bNode* node, bNodeType *ntype)
998 {
999         verify_socket_list(ntree, &node->inputs, ntype->inputs);
1000         verify_socket_list(ntree, &node->outputs, ntype->outputs);
1001 }
1002
1003 /* keep socket listorder identical, for copying links */
1004 /* ntree is the target tree */
1005 bNode *nodeCopyNode(struct bNodeTree *ntree, struct bNode *node, int internal)
1006 {
1007         bNode *nnode= MEM_callocN(sizeof(bNode), "dupli node");
1008         bNodeSocket *sock, *oldsock;
1009
1010         *nnode= *node;
1011         nodeUniqueName(ntree, nnode);
1012         
1013         BLI_addtail(&ntree->nodes, nnode);
1014         
1015         BLI_duplicatelist(&nnode->inputs, &node->inputs);
1016         oldsock= node->inputs.first;
1017         for(sock= nnode->inputs.first; sock; sock= sock->next, oldsock= oldsock->next) {
1018                 oldsock->new_sock= sock;
1019                 if(internal)
1020                         sock->own_index= 0;
1021         }
1022         
1023         BLI_duplicatelist(&nnode->outputs, &node->outputs);
1024         oldsock= node->outputs.first;
1025         for(sock= nnode->outputs.first; sock; sock= sock->next, oldsock= oldsock->next) {
1026                 if(internal)
1027                         sock->own_index= 0;
1028                 sock->stack_index= 0;
1029                 sock->ns.data= NULL;
1030                 oldsock->new_sock= sock;
1031         }
1032         
1033         /* don't increase node->id users, freenode doesn't decrement either */
1034         
1035         if(node->typeinfo->copystoragefunc)
1036                 node->typeinfo->copystoragefunc(node, nnode);
1037         
1038         node->new_node= nnode;
1039         nnode->new_node= NULL;
1040         nnode->preview= NULL;
1041         
1042         return nnode;
1043 }
1044
1045 bNodeLink *nodeAddLink(bNodeTree *ntree, bNode *fromnode, bNodeSocket *fromsock, bNode *tonode, bNodeSocket *tosock)
1046 {
1047         bNodeLink *link= MEM_callocN(sizeof(bNodeLink), "link");
1048         
1049         BLI_addtail(&ntree->links, link);
1050         link->fromnode= fromnode;
1051         link->fromsock= fromsock;
1052         link->tonode= tonode;
1053         link->tosock= tosock;
1054         
1055         return link;
1056 }
1057
1058 void nodeRemLink(bNodeTree *ntree, bNodeLink *link)
1059 {
1060         BLI_remlink(&ntree->links, link);
1061         if(link->tosock)
1062                 link->tosock->link= NULL;
1063         MEM_freeN(link);
1064 }
1065
1066
1067 bNodeTree *ntreeAddTree(int type)
1068 {
1069         bNodeTree *ntree= MEM_callocN(sizeof(bNodeTree), "new node tree");
1070         ntree->type= type;
1071         ntree->alltypes.first = NULL;
1072         ntree->alltypes.last = NULL;
1073
1074         /* this helps RNA identify ID pointers as nodetree */
1075     if(ntree->type==NTREE_SHADER)
1076                 BLI_strncpy(ntree->id.name, "NTShader Nodetree", sizeof(ntree->id.name));
1077     else if(ntree->type==NTREE_COMPOSIT)
1078                 BLI_strncpy(ntree->id.name, "NTCompositing Nodetree", sizeof(ntree->id.name));
1079     else if(ntree->type==NTREE_TEXTURE)
1080                 BLI_strncpy(ntree->id.name, "NTTexture Nodetree", sizeof(ntree->id.name));
1081         
1082         ntreeInitTypes(ntree);
1083         return ntree;
1084 }
1085
1086 /* Warning: this function gets called during some rather unexpected times
1087  *      - internal_select is only 1 when used for duplicating selected nodes (i.e. Shift-D duplicate operator)
1088  *      - this gets called when executing compositing updates (for threaded previews)
1089  *      - when the nodetree datablock needs to be copied (i.e. when users get copied)
1090  */
1091 bNodeTree *ntreeCopyTree(bNodeTree *ntree, int internal_select)
1092 {
1093         bNodeTree *newtree;
1094         bNode *node, *nnode, *last;
1095         bNodeLink *link, *nlink;
1096         bNodeSocket *sock;
1097         int a;
1098         
1099         if(ntree==NULL) return NULL;
1100         
1101         if(internal_select==0) {
1102                 /* is ntree part of library? */
1103                 for(newtree=G.main->nodetree.first; newtree; newtree= newtree->id.next)
1104                         if(newtree==ntree) break;
1105                 if(newtree)
1106                         newtree= copy_libblock(ntree);
1107                 else
1108                         newtree= MEM_dupallocN(ntree);
1109                 newtree->nodes.first= newtree->nodes.last= NULL;
1110                 newtree->links.first= newtree->links.last= NULL;
1111         }
1112         else
1113                 newtree= ntree;
1114         
1115         last= ntree->nodes.last;
1116         for(node= ntree->nodes.first; node; node= node->next) {
1117                 
1118                 node->new_node= NULL;
1119                 if(internal_select==0 || (node->flag & NODE_SELECT)) {
1120                         nnode= nodeCopyNode(newtree, node, internal_select);    /* sets node->new */
1121                         if(internal_select) {
1122                                 node->flag &= ~(NODE_SELECT|NODE_ACTIVE);
1123                                 nnode->flag |= NODE_SELECT;
1124                         }
1125
1126                         /* deselect original sockets */
1127                         for(sock= node->inputs.first; sock; sock= sock->next) {
1128                                 if(sock->flag & SOCK_SEL) sock->flag&= ~SOCK_SEL;
1129                         }
1130                         for(sock= node->outputs.first; sock; sock= sock->next) {
1131                                 if(sock->flag & SOCK_SEL) sock->flag&= ~SOCK_SEL;
1132                         }
1133                         
1134                         /* set tree selin and selout to new sockets */
1135                         for(sock= nnode->inputs.first; sock; sock= sock->next) {
1136                                 if(sock->flag & SOCK_SEL) {
1137                                         ntree->selin= sock;
1138                                         break;
1139                                 }
1140                         }
1141                         for(sock= nnode->outputs.first; sock; sock= sock->next) {
1142                                 if(sock->flag & SOCK_SEL) {
1143                                         ntree->selout= sock;
1144                                         break;
1145                                 }
1146                         }
1147                 }
1148                 if(node==last) break;
1149         }
1150         
1151         /* check for copying links */
1152         for(link= ntree->links.first; link; link= link->next) {
1153                 if(link->fromnode==NULL || link->tonode==NULL);
1154                 else if(link->fromnode->new_node && link->tonode->new_node) {
1155                         nlink= nodeAddLink(newtree, link->fromnode->new_node, NULL, link->tonode->new_node, NULL);
1156                         /* sockets were copied in order */
1157                         for(a=0, sock= link->fromnode->outputs.first; sock; sock= sock->next, a++) {
1158                                 if(sock==link->fromsock)
1159                                         break;
1160                         }
1161                         nlink->fromsock= BLI_findlink(&link->fromnode->new_node->outputs, a);
1162                         
1163                         for(a=0, sock= link->tonode->inputs.first; sock; sock= sock->next, a++) {
1164                                 if(sock==link->tosock)
1165                                         break;
1166                         }
1167                         nlink->tosock= BLI_findlink(&link->tonode->new_node->inputs, a);
1168                 }
1169         }
1170         
1171         /* own type definition for group usage */
1172         if(internal_select==0) {
1173                 if(ntree->owntype) {
1174                         newtree->owntype= MEM_dupallocN(ntree->owntype);
1175                         if(ntree->owntype->inputs)
1176                                 newtree->owntype->inputs= MEM_dupallocN(ntree->owntype->inputs);
1177                         if(ntree->owntype->outputs)
1178                                 newtree->owntype->outputs= MEM_dupallocN(ntree->owntype->outputs);
1179                 }
1180         }
1181         /* weird this is required... there seem to be link pointers wrong still? */
1182         /* anyhoo, doing this solves crashes on copying entire tree (copy scene) and delete nodes */
1183         ntreeSolveOrder(newtree);
1184
1185         return newtree;
1186 }
1187
1188 /* *************** preview *********** */
1189 /* if node->preview, then we assume the rect to exist */
1190
1191 static void node_free_preview(bNode *node)
1192 {
1193         if(node->preview) {
1194                 if(node->preview->rect)
1195                         MEM_freeN(node->preview->rect);
1196                 MEM_freeN(node->preview);
1197                 node->preview= NULL;
1198         }       
1199 }
1200
1201 static void node_init_preview(bNode *node, int xsize, int ysize)
1202 {
1203         
1204         if(node->preview==NULL) {
1205                 node->preview= MEM_callocN(sizeof(bNodePreview), "node preview");
1206                 //              printf("added preview %s\n", node->name);
1207         }
1208         
1209         /* node previews can get added with variable size this way */
1210         if(xsize==0 || ysize==0)
1211                 return;
1212         
1213         /* sanity checks & initialize */
1214         if(node->preview->rect) {
1215                 if(node->preview->xsize!=xsize && node->preview->ysize!=ysize) {
1216                         MEM_freeN(node->preview->rect);
1217                         node->preview->rect= NULL;
1218                 }
1219         }
1220         
1221         if(node->preview->rect==NULL) {
1222                 node->preview->rect= MEM_callocN(4*xsize + xsize*ysize*sizeof(float)*4, "node preview rect");
1223                 node->preview->xsize= xsize;
1224                 node->preview->ysize= ysize;
1225         }
1226         else
1227                 memset(node->preview->rect, 0, 4*xsize + xsize*ysize*sizeof(float)*4);
1228 }
1229
1230 void ntreeInitPreview(bNodeTree *ntree, int xsize, int ysize)
1231 {
1232         bNode *node;
1233         
1234         if(ntree==NULL)
1235                 return;
1236         
1237         for(node= ntree->nodes.first; node; node= node->next) {
1238                 if(node->typeinfo->flag & NODE_PREVIEW) /* hrms, check for closed nodes? */
1239                         node_init_preview(node, xsize, ysize);
1240                 if(node->type==NODE_GROUP && (node->flag & NODE_GROUP_EDIT))
1241                         ntreeInitPreview((bNodeTree *)node->id, xsize, ysize);
1242         }               
1243 }
1244
1245 static void nodeClearPreview(bNode *node)
1246 {
1247         if(node->preview && node->preview->rect)
1248                 memset(node->preview->rect, 0, MEM_allocN_len(node->preview->rect));
1249 }
1250
1251 /* use it to enforce clear */
1252 void ntreeClearPreview(bNodeTree *ntree)
1253 {
1254         bNode *node;
1255         
1256         if(ntree==NULL)
1257                 return;
1258         
1259         for(node= ntree->nodes.first; node; node= node->next) {
1260                 if(node->typeinfo->flag & NODE_PREVIEW)
1261                         nodeClearPreview(node);
1262                 if(node->type==NODE_GROUP && (node->flag & NODE_GROUP_EDIT))
1263                         ntreeClearPreview((bNodeTree *)node->id);
1264         }               
1265 }
1266
1267 /* hack warning! this function is only used for shader previews, and 
1268 since it gets called multiple times per pixel for Ztransp we only
1269 add the color once. Preview gets cleared before it starts render though */
1270 void nodeAddToPreview(bNode *node, float *col, int x, int y)
1271 {
1272         bNodePreview *preview= node->preview;
1273         if(preview) {
1274                 if(x>=0 && y>=0) {
1275                         if(x<preview->xsize && y<preview->ysize) {
1276                                 unsigned char *tar= preview->rect+ 4*((preview->xsize*y) + x);
1277                                 //if(tar[0]==0.0f) {
1278                                 tar[0]= FTOCHAR(col[0]);
1279                                 tar[1]= FTOCHAR(col[1]);
1280                                 tar[2]= FTOCHAR(col[2]);
1281                                 tar[3]= FTOCHAR(col[3]);
1282                                 //}
1283                         }
1284                         //else printf("prv out bound x y %d %d\n", x, y);
1285                 }
1286                 //else printf("prv out bound x y %d %d\n", x, y);
1287         }
1288 }
1289
1290
1291 /* ************** Free stuff ********** */
1292
1293 /* goes over entire tree */
1294 void nodeUnlinkNode(bNodeTree *ntree, bNode *node)
1295 {
1296         bNodeLink *link, *next;
1297         bNodeSocket *sock;
1298         ListBase *lb;
1299         
1300         for(link= ntree->links.first; link; link= next) {
1301                 next= link->next;
1302                 
1303                 if(link->fromnode==node) {
1304                         lb= &node->outputs;
1305                         NodeTagChanged(ntree, link->tonode);
1306                 }
1307                 else if(link->tonode==node)
1308                         lb= &node->inputs;
1309                 else
1310                         lb= NULL;
1311
1312                 if(lb) {
1313                         for(sock= lb->first; sock; sock= sock->next) {
1314                                 if(link->fromsock==sock || link->tosock==sock)
1315                                         break;
1316                         }
1317                         if(sock) {
1318                                 nodeRemLink(ntree, link);
1319                         }
1320                 }
1321         }
1322 }
1323
1324 static void composit_free_node_cache(bNode *node)
1325 {
1326         bNodeSocket *sock;
1327         
1328         for(sock= node->outputs.first; sock; sock= sock->next) {
1329                 if(sock->ns.data) {
1330                         free_compbuf(sock->ns.data);
1331                         sock->ns.data= NULL;
1332                 }
1333         }
1334 }
1335
1336 void nodeFreeNode(bNodeTree *ntree, bNode *node)
1337 {
1338         nodeUnlinkNode(ntree, node);
1339         BLI_remlink(&ntree->nodes, node);
1340
1341         /* since it is called while free database, node->id is undefined */
1342         
1343         if(ntree->type==NTREE_COMPOSIT)
1344                 composit_free_node_cache(node);
1345         BLI_freelistN(&node->inputs);
1346         BLI_freelistN(&node->outputs);
1347         
1348         node_free_preview(node);
1349
1350         if(node->typeinfo && node->typeinfo->freestoragefunc) {
1351                 node->typeinfo->freestoragefunc(node);
1352         }
1353
1354         MEM_freeN(node);
1355 }
1356
1357 /* do not free ntree itself here, free_libblock calls this function too */
1358 void ntreeFreeTree(bNodeTree *ntree)
1359 {
1360         bNode *node, *next;
1361         
1362         if(ntree==NULL) return;
1363         
1364         ntreeEndExecTree(ntree);        /* checks for if it is still initialized */
1365         
1366         BLI_freelistN(&ntree->links);   /* do first, then unlink_node goes fast */
1367         
1368         for(node= ntree->nodes.first; node; node= next) {
1369                 next= node->next;
1370                 nodeFreeNode(ntree, node);
1371         }
1372         
1373         if(ntree->owntype) {
1374                 if(ntree->owntype->inputs)
1375                         MEM_freeN(ntree->owntype->inputs);
1376                 if(ntree->owntype->outputs)
1377                         MEM_freeN(ntree->owntype->outputs);
1378                 MEM_freeN(ntree->owntype);
1379         }
1380 }
1381
1382 void ntreeFreeCache(bNodeTree *ntree)
1383 {
1384         bNode *node;
1385         
1386         if(ntree==NULL) return;
1387
1388         if(ntree->type==NTREE_COMPOSIT)
1389                 for(node= ntree->nodes.first; node; node= node->next)
1390                         composit_free_node_cache(node);
1391
1392 }
1393
1394 void ntreeMakeLocal(bNodeTree *ntree)
1395 {
1396         int local=0, lib=0;
1397         
1398         /* - only lib users: do nothing
1399             * - only local users: set flag
1400             * - mixed: make copy
1401             */
1402         
1403         if(ntree->id.lib==NULL) return;
1404         if(ntree->id.us==1) {
1405                 ntree->id.lib= 0;
1406                 ntree->id.flag= LIB_LOCAL;
1407                 new_id(0, (ID *)ntree, 0);
1408                 return;
1409         }
1410         
1411         /* now check users of groups... again typedepending, callback... */
1412         if(ntree->type==NTREE_SHADER) {
1413                 Material *ma;
1414                 for(ma= G.main->mat.first; ma; ma= ma->id.next) {
1415                         if(ma->nodetree) {
1416                                 bNode *node;
1417                                 
1418                                 /* find if group is in tree */
1419                                 for(node= ma->nodetree->nodes.first; node; node= node->next) {
1420                                         if(node->id == (ID *)ntree) {
1421                                                 if(ma->id.lib) lib= 1;
1422                                                 else local= 1;
1423                                         }
1424                                 }
1425                         }
1426                 }
1427         }
1428         else if(ntree->type==NTREE_COMPOSIT) {
1429                 Scene *sce;
1430                 for(sce= G.main->scene.first; sce; sce= sce->id.next) {
1431                         if(sce->nodetree) {
1432                                 bNode *node;
1433                                 
1434                                 /* find if group is in tree */
1435                                 for(node= sce->nodetree->nodes.first; node; node= node->next) {
1436                                         if(node->id == (ID *)ntree) {
1437                                                 if(sce->id.lib) lib= 1;
1438                                                 else local= 1;
1439                                         }
1440                                 }
1441                         }
1442                 }
1443         }
1444         else if(ntree->type==NTREE_TEXTURE) {
1445                 Tex *tx;
1446                 for(tx= G.main->tex.first; tx; tx= tx->id.next) {
1447                         if(tx->nodetree) {
1448                                 bNode *node;
1449                                 
1450                                 /* find if group is in tree */
1451                                 for(node= tx->nodetree->nodes.first; node; node= node->next) {
1452                                         if(node->id == (ID *)ntree) {
1453                                                 if(tx->id.lib) lib= 1;
1454                                                 else local= 1;
1455                                         }
1456                                 }
1457                         }
1458                 }
1459         }
1460         
1461         /* if all users are local, we simply make tree local */
1462         if(local && lib==0) {
1463                 ntree->id.lib= NULL;
1464                 ntree->id.flag= LIB_LOCAL;
1465                 new_id(0, (ID *)ntree, 0);
1466         }
1467         else if(local && lib) {
1468                 /* this is the mixed case, we copy the tree and assign it to local users */
1469                 bNodeTree *newtree= ntreeCopyTree(ntree, 0);
1470                 
1471                 newtree->id.us= 0;
1472                 
1473                 if(ntree->type==NTREE_SHADER) {
1474                         Material *ma;
1475                         for(ma= G.main->mat.first; ma; ma= ma->id.next) {
1476                                 if(ma->nodetree) {
1477                                         bNode *node;
1478                                         
1479                                         /* find if group is in tree */
1480                                         for(node= ma->nodetree->nodes.first; node; node= node->next) {
1481                                                 if(node->id == (ID *)ntree) {
1482                                                         if(ma->id.lib==NULL) {
1483                                                                 node->id= &newtree->id;
1484                                                                 newtree->id.us++;
1485                                                                 ntree->id.us--;
1486                                                         }
1487                                                 }
1488                                         }
1489                                 }
1490                         }
1491                 }
1492                 else if(ntree->type==NTREE_COMPOSIT) {
1493                         Scene *sce;
1494                         for(sce= G.main->scene.first; sce; sce= sce->id.next) {
1495                                 if(sce->nodetree) {
1496                                         bNode *node;
1497                                         
1498                                         /* find if group is in tree */
1499                                         for(node= sce->nodetree->nodes.first; node; node= node->next) {
1500                                                 if(node->id == (ID *)ntree) {
1501                                                         if(sce->id.lib==NULL) {
1502                                                                 node->id= &newtree->id;
1503                                                                 newtree->id.us++;
1504                                                                 ntree->id.us--;
1505                                                         }
1506                                                 }
1507                                         }
1508                                 }
1509                         }
1510                 }
1511                 else if(ntree->type==NTREE_TEXTURE) {
1512                         Tex *tx;
1513                         for(tx= G.main->tex.first; tx; tx= tx->id.next) {
1514                                 if(tx->nodetree) {
1515                                         bNode *node;
1516                                         
1517                                         /* find if group is in tree */
1518                                         for(node= tx->nodetree->nodes.first; node; node= node->next) {
1519                                                 if(node->id == (ID *)ntree) {
1520                                                         if(tx->id.lib==NULL) {
1521                                                                 node->id= &newtree->id;
1522                                                                 newtree->id.us++;
1523                                                                 ntree->id.us--;
1524                                                         }
1525                                                 }
1526                                         }
1527                                 }
1528                         }
1529                 }
1530         }
1531 }
1532
1533
1534 /* ************ find stuff *************** */
1535
1536 static int ntreeHasType(bNodeTree *ntree, int type)
1537 {
1538         bNode *node;
1539         
1540         if(ntree)
1541                 for(node= ntree->nodes.first; node; node= node->next)
1542                         if(node->type == type)
1543                                 return 1;
1544         return 0;
1545 }
1546
1547 bNodeLink *nodeFindLink(bNodeTree *ntree, bNodeSocket *from, bNodeSocket *to)
1548 {
1549         bNodeLink *link;
1550         
1551         for(link= ntree->links.first; link; link= link->next) {
1552                 if(link->fromsock==from && link->tosock==to)
1553                         return link;
1554                 if(link->fromsock==to && link->tosock==from)    /* hrms? */
1555                         return link;
1556         }
1557         return NULL;
1558 }
1559
1560 int nodeCountSocketLinks(bNodeTree *ntree, bNodeSocket *sock)
1561 {
1562         bNodeLink *link;
1563         int tot= 0;
1564         
1565         for(link= ntree->links.first; link; link= link->next) {
1566                 if(link->fromsock==sock || link->tosock==sock)
1567                         tot++;
1568         }
1569         return tot;
1570 }
1571
1572 bNode *nodeGetActive(bNodeTree *ntree)
1573 {
1574         bNode *node;
1575         
1576         if(ntree==NULL) return NULL;
1577         
1578         for(node= ntree->nodes.first; node; node= node->next)
1579                 if(node->flag & NODE_ACTIVE)
1580                         break;
1581         return node;
1582 }
1583
1584 /* two active flags, ID nodes have special flag for buttons display */
1585 bNode *nodeGetActiveID(bNodeTree *ntree, short idtype)
1586 {
1587         bNode *node;
1588         
1589         if(ntree==NULL) return NULL;
1590
1591         /* check for group edit */
1592     for(node= ntree->nodes.first; node; node= node->next)
1593                 if(node->flag & NODE_GROUP_EDIT)
1594                         break;
1595
1596         if(node)
1597                 ntree= (bNodeTree*)node->id;
1598         
1599         /* now find active node with this id */
1600         for(node= ntree->nodes.first; node; node= node->next)
1601                 if(node->id && GS(node->id->name)==idtype)
1602                         if(node->flag & NODE_ACTIVE_ID)
1603                                 break;
1604
1605         return node;
1606 }
1607
1608 int nodeSetActiveID(bNodeTree *ntree, short idtype, ID *id)
1609 {
1610         bNode *node;
1611         int ok= FALSE;
1612
1613         if(ntree==NULL) return ok;
1614
1615         /* check for group edit */
1616     for(node= ntree->nodes.first; node; node= node->next)
1617                 if(node->flag & NODE_GROUP_EDIT)
1618                         break;
1619
1620         if(node)
1621                 ntree= (bNodeTree*)node->id;
1622
1623         /* now find active node with this id */
1624         for(node= ntree->nodes.first; node; node= node->next) {
1625                 if(node->id && GS(node->id->name)==idtype) {
1626                         if(id && ok==FALSE && node->id==id) {
1627                                 node->flag |= NODE_ACTIVE_ID;
1628                                 ok= TRUE;
1629                         } else {
1630                                 node->flag &= ~NODE_ACTIVE_ID;
1631                         }
1632                 }
1633         }
1634
1635         return ok;
1636 }
1637
1638
1639 /* two active flags, ID nodes have special flag for buttons display */
1640 void nodeClearActiveID(bNodeTree *ntree, short idtype)
1641 {
1642         bNode *node;
1643         
1644         if(ntree==NULL) return;
1645         
1646         for(node= ntree->nodes.first; node; node= node->next)
1647                 if(node->id && GS(node->id->name)==idtype)
1648                         node->flag &= ~NODE_ACTIVE_ID;
1649 }
1650
1651 /* two active flags, ID nodes have special flag for buttons display */
1652 void nodeSetActive(bNodeTree *ntree, bNode *node)
1653 {
1654         bNode *tnode;
1655         
1656         /* make sure only one node is active, and only one per ID type */
1657         for(tnode= ntree->nodes.first; tnode; tnode= tnode->next) {
1658                 tnode->flag &= ~NODE_ACTIVE;
1659                 
1660                 if(node->id && tnode->id) {
1661                         if(GS(node->id->name) == GS(tnode->id->name))
1662                                 tnode->flag &= ~NODE_ACTIVE_ID;
1663                 }
1664         }
1665         
1666         node->flag |= NODE_ACTIVE;
1667         if(node->id)
1668                 node->flag |= NODE_ACTIVE_ID;
1669 }
1670
1671 /* use flags are not persistant yet, groups might need different tagging, so we do it each time
1672    when we need to get this info */
1673 void ntreeSocketUseFlags(bNodeTree *ntree)
1674 {
1675         bNode *node;
1676         bNodeSocket *sock;
1677         bNodeLink *link;
1678         
1679         /* clear flags */
1680         for(node= ntree->nodes.first; node; node= node->next) {
1681                 for(sock= node->inputs.first; sock; sock= sock->next)
1682                         sock->flag &= ~SOCK_IN_USE;
1683                 for(sock= node->outputs.first; sock; sock= sock->next)
1684                         sock->flag &= ~SOCK_IN_USE;
1685         }
1686         
1687         /* tag all thats in use */
1688         for(link= ntree->links.first; link; link= link->next) {
1689         
1690                 if(link->fromsock) // FIXME, see below
1691                         link->fromsock->flag |= SOCK_IN_USE;
1692                 if(link->tosock) // FIXME This can be NULL, when dragging a new link in the UI, should probably copy the node tree for preview render - campbell
1693                         link->tosock->flag |= SOCK_IN_USE;
1694         }
1695 }
1696
1697 /* ************** dependency stuff *********** */
1698
1699 /* node is guaranteed to be not checked before */
1700 static int node_recurs_check(bNode *node, bNode ***nsort, int level)
1701 {
1702         bNode *fromnode;
1703         bNodeSocket *sock;
1704         int has_inputlinks= 0;
1705         
1706         node->done= 1;
1707         level++;
1708         
1709         for(sock= node->inputs.first; sock; sock= sock->next) {
1710                 if(sock->link) {
1711                         has_inputlinks= 1;
1712                         fromnode= sock->link->fromnode;
1713                         if(fromnode->done==0) {
1714                                 fromnode->level= node_recurs_check(fromnode, nsort, level);
1715                         }
1716                 }
1717         }
1718 //      printf("node sort %s level %d\n", node->name, level);
1719         **nsort= node;
1720         (*nsort)++;
1721         
1722         if(has_inputlinks)
1723                 return level;
1724         else 
1725                 return 0xFFF;
1726 }
1727
1728 void ntreeSolveOrder(bNodeTree *ntree)
1729 {
1730         bNode *node, **nodesort, **nsort;
1731         bNodeSocket *sock;
1732         bNodeLink *link;
1733         int a, totnode=0;
1734         
1735         /* the solve-order is called on each tree change, so we should be sure no exec can be running */
1736         ntreeEndExecTree(ntree);
1737
1738         /* set links pointers the input sockets, to find dependencies */
1739         /* first clear data */
1740         for(node= ntree->nodes.first; node; node= node->next) {
1741                 node->done= 0;
1742                 totnode++;
1743                 for(sock= node->inputs.first; sock; sock= sock->next)
1744                         sock->link= NULL;
1745         }
1746         if(totnode==0)
1747                 return;
1748         
1749         for(link= ntree->links.first; link; link= link->next) {
1750                 link->tosock->link= link;
1751         }
1752         
1753         nsort= nodesort= MEM_callocN(totnode*sizeof(void *), "sorted node array");
1754         
1755         /* recursive check */
1756         for(node= ntree->nodes.first; node; node= node->next) {
1757                 if(node->done==0) {
1758                         node->level= node_recurs_check(node, &nsort, 0);
1759                 }
1760         }
1761         
1762         /* re-insert nodes in order, first a paranoia check */
1763         for(a=0; a<totnode; a++) {
1764                 if(nodesort[a]==NULL)
1765                         break;
1766         }
1767         if(a<totnode)
1768                 printf("sort error in node tree");
1769         else {
1770                 ntree->nodes.first= ntree->nodes.last= NULL;
1771                 for(a=0; a<totnode; a++)
1772                         BLI_addtail(&ntree->nodes, nodesort[a]);
1773         }
1774         
1775         MEM_freeN(nodesort);
1776         
1777         /* find the active outputs, might become tree type dependant handler */
1778         for(node= ntree->nodes.first; node; node= node->next) {
1779                 if(node->typeinfo->nclass==NODE_CLASS_OUTPUT) {
1780                         bNode *tnode;
1781                         int output= 0;
1782                         
1783                         /* we need a check for which output node should be tagged like this, below an exception */
1784                         if(node->type==CMP_NODE_OUTPUT_FILE)
1785                            continue;
1786                            
1787                         /* there is more types having output class, each one is checked */
1788                         for(tnode= ntree->nodes.first; tnode; tnode= tnode->next) {
1789                                 if(tnode->typeinfo->nclass==NODE_CLASS_OUTPUT) {
1790                                         if(tnode->type==node->type) {
1791                                                 if(tnode->flag & NODE_DO_OUTPUT) {
1792                                                         output++;
1793                                                         if(output>1)
1794                                                                 tnode->flag &= ~NODE_DO_OUTPUT;
1795                                                 }
1796                                         }
1797                                 }
1798                         }
1799                         if(output==0)
1800                                 node->flag |= NODE_DO_OUTPUT;
1801                 }
1802         }
1803         
1804         /* here we could recursively set which nodes have to be done,
1805                 might be different for editor or for "real" use... */
1806 }
1807
1808 /* Should be callback! */
1809 /* Do not call execs here */
1810 void NodeTagChanged(bNodeTree *ntree, bNode *node)
1811 {
1812         if(ntree->type==NTREE_COMPOSIT) {
1813                 bNodeSocket *sock;
1814
1815                 for(sock= node->outputs.first; sock; sock= sock->next) {
1816                         if(sock->ns.data) {
1817                                 //free_compbuf(sock->ns.data);
1818                                 //sock->ns.data= NULL;
1819                         }
1820                 }
1821                 node->need_exec= 1;
1822         }
1823 }
1824
1825 void NodeTagIDChanged(bNodeTree *ntree, ID *id)
1826 {
1827         if(id==NULL)
1828                 return;
1829         
1830         if(ntree->type==NTREE_COMPOSIT) {
1831                 bNode *node;
1832                 
1833                 for(node= ntree->nodes.first; node; node= node->next)
1834                         if(node->id==id)
1835                                 NodeTagChanged(ntree, node);
1836         }
1837 }
1838
1839
1840
1841 /* ******************* executing ************* */
1842
1843 /* see notes at ntreeBeginExecTree */
1844 static void group_node_get_stack(bNode *node, bNodeStack *stack, bNodeStack **in, bNodeStack **out, bNodeStack **gin, bNodeStack **gout)
1845 {
1846         bNodeSocket *sock;
1847         
1848         /* build pointer stack */
1849         for(sock= node->inputs.first; sock; sock= sock->next) {
1850                 if(sock->intern) {
1851                         /* yep, intern can have link or is hidden socket */
1852                         if(sock->link)
1853                                 *(in++)= stack + sock->link->fromsock->stack_index;
1854                         else
1855                                 *(in++)= &sock->ns;
1856                 }
1857                 else
1858                         *(in++)= gin[sock->stack_index_ext];
1859         }
1860         
1861         for(sock= node->outputs.first; sock; sock= sock->next) {
1862                 if(sock->intern)
1863                         *(out++)= stack + sock->stack_index;
1864                 else
1865                         *(out++)= gout[sock->stack_index_ext];
1866         }
1867 }
1868
1869 static void node_group_execute(bNodeStack *stack, void *data, bNode *gnode, bNodeStack **in, bNodeStack **out)
1870 {
1871         bNode *node;
1872         bNodeTree *ntree= (bNodeTree *)gnode->id;
1873         bNodeStack *nsin[MAX_SOCKET];   /* arbitrary... watch this */
1874         bNodeStack *nsout[MAX_SOCKET];  /* arbitrary... watch this */
1875         
1876         if(ntree==NULL) return;
1877         
1878         stack+= gnode->stack_index;
1879                 
1880         for(node= ntree->nodes.first; node; node= node->next) {
1881                 if(node->typeinfo->execfunc) {
1882                         group_node_get_stack(node, stack, nsin, nsout, in, out);
1883                         
1884                         /* for groups, only execute outputs for edited group */
1885                         if(node->typeinfo->nclass==NODE_CLASS_OUTPUT) {
1886                                 if(gnode->flag & NODE_GROUP_EDIT)
1887                                         if(node->flag & NODE_DO_OUTPUT)
1888                                                 node->typeinfo->execfunc(data, node, nsin, nsout);
1889                         }
1890                         else
1891                                 node->typeinfo->execfunc(data, node, nsin, nsout);
1892                 }
1893         }
1894         
1895         /* free internal group output nodes */
1896         if(ntree->type==NTREE_COMPOSIT) {
1897                 for(node= ntree->nodes.first; node; node= node->next) {
1898                         if(node->typeinfo->execfunc) {
1899                                 bNodeSocket *sock;
1900                                 
1901                                 for(sock= node->outputs.first; sock; sock= sock->next) {
1902                                         if(sock->intern) {
1903                                                 bNodeStack *ns= stack + sock->stack_index;
1904                                                 if(ns->data) {
1905                                                         free_compbuf(ns->data);
1906                                                         ns->data= NULL;
1907                                                 }
1908                                         }
1909                                 }
1910                         }
1911                 }
1912         }
1913 }
1914
1915 /* recursively called for groups */
1916 /* we set all trees on own local indices, but put a total counter
1917    in the groups, so each instance of a group has own stack */
1918 static int ntree_begin_exec_tree(bNodeTree *ntree)
1919 {
1920         bNode *node;
1921         bNodeSocket *sock;
1922         int index= 0, index_in= 0, index_out= 0;
1923         
1924         if((ntree->init & NTREE_TYPE_INIT)==0)
1925                 ntreeInitTypes(ntree);
1926         
1927         /* create indices for stack, check preview */
1928         for(node= ntree->nodes.first; node; node= node->next) {
1929                 
1930                 for(sock= node->inputs.first; sock; sock= sock->next) {
1931                         if(sock->intern==0)
1932                                 sock->stack_index_ext= index_in++;
1933                 }
1934                 
1935                 for(sock= node->outputs.first; sock; sock= sock->next) {
1936                         sock->stack_index= index++;
1937                         if(sock->intern==0)
1938                                 sock->stack_index_ext= index_out++;
1939                 }
1940                 
1941                 if(node->type==NODE_GROUP) {
1942                         if(node->id) {
1943                                 node->stack_index= index;
1944                                 index+= ntree_begin_exec_tree((bNodeTree *)node->id);
1945                         }
1946                 }
1947         }
1948         
1949         return index;
1950 }
1951
1952 /* copy socket compbufs to stack, initialize usage of curve nodes */
1953 static void composit_begin_exec(bNodeTree *ntree, int is_group)
1954 {
1955         bNode *node;
1956         bNodeSocket *sock;
1957         
1958         for(node= ntree->nodes.first; node; node= node->next) {
1959                 
1960                 /* initialize needed for groups */
1961                 node->exec= 0;  
1962                 
1963                 if(is_group==0) {
1964                         for(sock= node->outputs.first; sock; sock= sock->next) {
1965                                 bNodeStack *ns= ntree->stack + sock->stack_index;
1966                                 
1967                                 if(sock->ns.data) {
1968                                         ns->data= sock->ns.data;
1969                                         sock->ns.data= NULL;
1970                                 }
1971                         }
1972                 }
1973                 /* cannot initialize them while using in threads */
1974                 if(ELEM3(node->type, CMP_NODE_TIME, CMP_NODE_CURVE_VEC, CMP_NODE_CURVE_RGB)) {
1975                         curvemapping_initialize(node->storage);
1976                         if(node->type==CMP_NODE_CURVE_RGB)
1977                                 curvemapping_premultiply(node->storage, 0);
1978                 }
1979                 if(node->type==NODE_GROUP)
1980                         composit_begin_exec((bNodeTree *)node->id, 1);
1981
1982         }
1983 }
1984
1985 /* copy stack compbufs to sockets */
1986 static void composit_end_exec(bNodeTree *ntree, int is_group)
1987 {
1988         extern void print_compbuf(char *str, struct CompBuf *cbuf);
1989         bNode *node;
1990         bNodeStack *ns;
1991         int a;
1992
1993         for(node= ntree->nodes.first; node; node= node->next) {
1994                 if(is_group==0) {
1995                         bNodeSocket *sock;
1996                 
1997                         for(sock= node->outputs.first; sock; sock= sock->next) {
1998                                 ns= ntree->stack + sock->stack_index;
1999                                 if(ns->data) {
2000                                         sock->ns.data= ns->data;
2001                                         ns->data= NULL;
2002                                 }
2003                         }
2004                 }
2005                 if(node->type==CMP_NODE_CURVE_RGB)
2006                         curvemapping_premultiply(node->storage, 1);
2007                 
2008                 if(node->type==NODE_GROUP)
2009                         composit_end_exec((bNodeTree *)node->id, 1);
2010
2011                 node->need_exec= 0;
2012         }
2013         
2014         if(is_group==0) {
2015                 /* internally, group buffers are not stored */
2016                 for(ns= ntree->stack, a=0; a<ntree->stacksize; a++, ns++) {
2017                         if(ns->data) {
2018                                 printf("freed leftover buffer from stack\n");
2019                                 free_compbuf(ns->data);
2020                                 ns->data= NULL;
2021                         }
2022                 }
2023         }
2024 }
2025
2026 static void group_tag_used_outputs(bNode *gnode, bNodeStack *stack)
2027 {
2028         bNodeTree *ntree= (bNodeTree *)gnode->id;
2029         bNode *node;
2030         
2031         stack+= gnode->stack_index;
2032         
2033         for(node= ntree->nodes.first; node; node= node->next) {
2034                 if(node->typeinfo->execfunc) {
2035                         bNodeSocket *sock;
2036                         
2037                         for(sock= node->inputs.first; sock; sock= sock->next) {
2038                                 if(sock->intern) {
2039                                         if(sock->link) {
2040                                                 bNodeStack *ns= stack + sock->link->fromsock->stack_index;
2041                                                 ns->hasoutput= 1;
2042                                                 ns->sockettype= sock->link->fromsock->type;
2043                                         }
2044                                         else
2045                                                 sock->ns.sockettype= sock->type;
2046                                 }
2047                         }
2048                 }
2049         }
2050 }
2051
2052 /* notes below are ancient! (ton) */
2053 /* stack indices make sure all nodes only write in allocated data, for making it thread safe */
2054 /* only root tree gets the stack, to enable instances to have own stack entries */
2055 /* per tree (and per group) unique indices are created */
2056 /* the index_ext we need to be able to map from groups to the group-node own stack */
2057
2058 typedef struct bNodeThreadStack {
2059         struct bNodeThreadStack *next, *prev;
2060         bNodeStack *stack;
2061         int used;
2062 } bNodeThreadStack;
2063
2064 static bNodeThreadStack *ntreeGetThreadStack(bNodeTree *ntree, int thread)
2065 {
2066         ListBase *lb= &ntree->threadstack[thread];
2067         bNodeThreadStack *nts;
2068         
2069         for(nts=lb->first; nts; nts=nts->next) {
2070                 if(!nts->used) {
2071                         nts->used= 1;
2072                         return nts;
2073                 }
2074         }
2075         nts= MEM_callocN(sizeof(bNodeThreadStack), "bNodeThreadStack");
2076         nts->stack= MEM_dupallocN(ntree->stack);
2077         nts->used= 1;
2078         BLI_addtail(lb, nts);
2079
2080         return nts;
2081 }
2082
2083 static void ntreeReleaseThreadStack(bNodeThreadStack *nts)
2084 {
2085         nts->used= 0;
2086 }
2087
2088 /* free texture delegates */
2089 static void tex_end_exec(bNodeTree *ntree)
2090 {
2091         bNodeThreadStack *nts;
2092         bNodeStack *ns;
2093         int th, a;
2094         
2095         if(ntree->threadstack)
2096                 for(th=0; th<BLENDER_MAX_THREADS; th++)
2097                         for(nts=ntree->threadstack[th].first; nts; nts=nts->next)
2098                                 for(ns= nts->stack, a=0; a<ntree->stacksize; a++, ns++)
2099                                         if(ns->data)
2100                                                 MEM_freeN(ns->data);
2101                                                 
2102 }
2103
2104 void ntreeBeginExecTree(bNodeTree *ntree)
2105 {
2106         /* let's make it sure */
2107         if(ntree->init & NTREE_EXEC_INIT)
2108                 return;
2109         
2110         /* allocate the thread stack listbase array */
2111         if(ntree->type!=NTREE_COMPOSIT)
2112                 ntree->threadstack= MEM_callocN(BLENDER_MAX_THREADS*sizeof(ListBase), "thread stack array");
2113
2114         /* goes recursive over all groups */
2115         ntree->stacksize= ntree_begin_exec_tree(ntree);
2116
2117         if(ntree->stacksize) {
2118                 bNode *node;
2119                 bNodeStack *ns;
2120                 int a;
2121                 
2122                 /* allocate the base stack */
2123                 ns=ntree->stack= MEM_callocN(ntree->stacksize*sizeof(bNodeStack), "node stack");
2124                 
2125                 /* tag inputs, the get_stack() gives own socket stackdata if not in use */
2126                 for(a=0; a<ntree->stacksize; a++, ns++) ns->hasinput= 1;
2127                 
2128                 /* tag used outputs, so we know when we can skip operations */
2129                 for(node= ntree->nodes.first; node; node= node->next) {
2130                         bNodeSocket *sock;
2131                         
2132                         /* composite has own need_exec tag handling */
2133                         if(ntree->type!=NTREE_COMPOSIT)
2134                                 node->need_exec= 1;
2135
2136                         for(sock= node->inputs.first; sock; sock= sock->next) {
2137                                 if(sock->link) {
2138                                         ns= ntree->stack + sock->link->fromsock->stack_index;
2139                                         ns->hasoutput= 1;
2140                                         ns->sockettype= sock->link->fromsock->type;
2141                                 }
2142                                 else
2143                                         sock->ns.sockettype= sock->type;
2144                                 
2145                                 if(sock->link) {
2146                                         bNodeLink *link= sock->link;
2147                                         /* this is the test for a cyclic case */
2148                                         if(link->fromnode && link->tonode) {
2149                                                 if(link->fromnode->level >= link->tonode->level && link->tonode->level!=0xFFF);
2150                                                 else {
2151                                                         node->need_exec= 0;
2152                                                 }
2153                                         }
2154                                 }
2155                         }
2156                         
2157                         if(node->type==NODE_GROUP && node->id)
2158                                 group_tag_used_outputs(node, ntree->stack);
2159                         
2160                 }
2161                 
2162                 if(ntree->type==NTREE_COMPOSIT)
2163                         composit_begin_exec(ntree, 0);
2164         }
2165         
2166         ntree->init |= NTREE_EXEC_INIT;
2167 }
2168
2169 void ntreeEndExecTree(bNodeTree *ntree)
2170 {
2171         
2172         if(ntree->init & NTREE_EXEC_INIT) {
2173                 bNodeThreadStack *nts;
2174                 int a;
2175                 
2176                 /* another callback candidate! */
2177                 if(ntree->type==NTREE_COMPOSIT)
2178                         composit_end_exec(ntree, 0);
2179                 else if(ntree->type==NTREE_TEXTURE)
2180                         tex_end_exec(ntree);
2181                 
2182                 if(ntree->stack) {
2183                         MEM_freeN(ntree->stack);
2184                         ntree->stack= NULL;
2185                 }
2186
2187                 if(ntree->threadstack) {
2188                         for(a=0; a<BLENDER_MAX_THREADS; a++) {
2189                                 for(nts=ntree->threadstack[a].first; nts; nts=nts->next)
2190                                         if (nts->stack) MEM_freeN(nts->stack);
2191                                 BLI_freelistN(&ntree->threadstack[a]);
2192                         }
2193
2194                         MEM_freeN(ntree->threadstack);
2195                         ntree->threadstack= NULL;
2196                 }
2197
2198                 ntree->init &= ~NTREE_EXEC_INIT;
2199         }
2200 }
2201
2202 static void node_get_stack(bNode *node, bNodeStack *stack, bNodeStack **in, bNodeStack **out)
2203 {
2204         bNodeSocket *sock;
2205         
2206         /* build pointer stack */
2207         for(sock= node->inputs.first; sock; sock= sock->next) {
2208                 if(sock->link)
2209                         *(in++)= stack + sock->link->fromsock->stack_index;
2210                 else
2211                         *(in++)= &sock->ns;
2212         }
2213         
2214         for(sock= node->outputs.first; sock; sock= sock->next) {
2215                 *(out++)= stack + sock->stack_index;
2216         }
2217 }
2218
2219 /* nodes are presorted, so exec is in order of list */
2220 void ntreeExecTree(bNodeTree *ntree, void *callerdata, int thread)
2221 {
2222         bNode *node;
2223         bNodeStack *nsin[MAX_SOCKET];   /* arbitrary... watch this */
2224         bNodeStack *nsout[MAX_SOCKET];  /* arbitrary... watch this */
2225         bNodeStack *stack;
2226         bNodeThreadStack *nts = NULL;
2227         
2228         /* only when initialized */
2229         if((ntree->init & NTREE_EXEC_INIT)==0)
2230                 ntreeBeginExecTree(ntree);
2231                 
2232         /* composite does 1 node per thread, so no multiple stacks needed */
2233         if(ntree->type==NTREE_COMPOSIT) {
2234                 stack= ntree->stack;
2235         }
2236         else {
2237                 nts= ntreeGetThreadStack(ntree, thread);
2238                 stack= nts->stack;
2239         }
2240         
2241         for(node= ntree->nodes.first; node; node= node->next) {
2242                 if(node->need_exec) {
2243                         if(node->typeinfo->execfunc) {
2244                                 node_get_stack(node, stack, nsin, nsout);
2245                                 node->typeinfo->execfunc(callerdata, node, nsin, nsout);
2246                         }
2247                         else if(node->type==NODE_GROUP && node->id) {
2248                                 node_get_stack(node, stack, nsin, nsout);
2249                                 node_group_execute(stack, callerdata, node, nsin, nsout); 
2250                         }
2251                 }
2252         }
2253
2254         if(nts)
2255                 ntreeReleaseThreadStack(nts);
2256 }
2257
2258
2259 /* ***************************** threaded version for execute composite nodes ************* */
2260 /* these are nodes without input, only giving values */
2261 /* or nodes with only value inputs */
2262 static int node_only_value(bNode *node)
2263 {
2264         bNodeSocket *sock;
2265         
2266         if(ELEM3(node->type, CMP_NODE_TIME, CMP_NODE_VALUE, CMP_NODE_RGB))
2267                 return 1;
2268         
2269         /* doing this for all node types goes wrong. memory free errors */
2270         if(node->inputs.first && node->type==CMP_NODE_MAP_VALUE) {
2271                 int retval= 1;
2272                 for(sock= node->inputs.first; sock; sock= sock->next) {
2273                         if(sock->link)
2274                                 retval &= node_only_value(sock->link->fromnode);
2275                 }
2276                 return retval;
2277         }
2278         return 0;
2279 }
2280
2281
2282 /* not changing info, for thread callback */
2283 typedef struct ThreadData {
2284         bNodeStack *stack;
2285         RenderData *rd;
2286 } ThreadData;
2287
2288 static void *exec_composite_node(void *node_v)
2289 {
2290         bNodeStack *nsin[MAX_SOCKET];   /* arbitrary... watch this */
2291         bNodeStack *nsout[MAX_SOCKET];  /* arbitrary... watch this */
2292         bNode *node= node_v;
2293         ThreadData *thd= (ThreadData *)node->threaddata;
2294         
2295         node_get_stack(node, thd->stack, nsin, nsout);
2296         
2297         if((node->flag & NODE_MUTED) && (!node_only_value(node))) {
2298                 /* viewers we execute, for feedback to user */
2299                 if(ELEM(node->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER)) 
2300                         node->typeinfo->execfunc(thd->rd, node, nsin, nsout);
2301                 else
2302                         node_compo_pass_on(node, nsin, nsout);
2303         }
2304         else if(node->typeinfo->execfunc) {
2305                 node->typeinfo->execfunc(thd->rd, node, nsin, nsout);
2306         }
2307         else if(node->type==NODE_GROUP && node->id) {
2308                 node_group_execute(thd->stack, thd->rd, node, nsin, nsout); 
2309         }
2310         
2311         node->exec |= NODE_READY;
2312         return 0;
2313 }
2314
2315 /* return total of executable nodes, for timecursor */
2316 /* only compositor uses it */
2317 static int setExecutableNodes(bNodeTree *ntree, ThreadData *thd)
2318 {
2319         bNodeStack *nsin[MAX_SOCKET];   /* arbitrary... watch this */
2320         bNodeStack *nsout[MAX_SOCKET];  /* arbitrary... watch this */
2321         bNode *node;
2322         bNodeSocket *sock;
2323         int totnode= 0, group_edit= 0;
2324         
2325         /* note; do not add a dependency sort here, the stack was created already */
2326         
2327         /* if we are in group edit, viewer nodes get skipped when group has viewer */
2328         for(node= ntree->nodes.first; node; node= node->next)
2329                 if(node->type==NODE_GROUP && (node->flag & NODE_GROUP_EDIT))
2330                         if(ntreeHasType((bNodeTree *)node->id, CMP_NODE_VIEWER))
2331                                 group_edit= 1;
2332         
2333         for(node= ntree->nodes.first; node; node= node->next) {
2334                 int a;
2335                 
2336                 node_get_stack(node, thd->stack, nsin, nsout);
2337                 
2338                 /* test the outputs */
2339                 /* skip value-only nodes (should be in type!) */
2340                 if(!node_only_value(node)) {
2341                         for(a=0, sock= node->outputs.first; sock; sock= sock->next, a++) {
2342                                 if(nsout[a]->data==NULL && nsout[a]->hasoutput) {
2343                                         node->need_exec= 1;
2344                                         break;
2345                                 }
2346                         }
2347                 }
2348                 
2349                 /* test the inputs */
2350                 for(a=0, sock= node->inputs.first; sock; sock= sock->next, a++) {
2351                         /* skip viewer nodes in bg render or group edit */
2352                         if( ELEM(node->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER) && (G.background || group_edit))
2353                                 node->need_exec= 0;
2354                         /* is sock in use? */
2355                         else if(sock->link) {
2356                                 bNodeLink *link= sock->link;
2357                                 
2358                                 /* this is the test for a cyclic case */
2359                                 if(link->fromnode==NULL || link->tonode==NULL);
2360                                 else if(link->fromnode->level >= link->tonode->level && link->tonode->level!=0xFFF) {
2361                                         if(link->fromnode->need_exec) {
2362                                                 node->need_exec= 1;
2363                                                 break;
2364                                         }
2365                                 }
2366                                 else {
2367                                         node->need_exec= 0;
2368                                         printf("Node %s skipped, cyclic dependency\n", node->name);
2369                                 }
2370                         }
2371                 }
2372                 
2373                 if(node->need_exec) {
2374                         
2375                         /* free output buffers */
2376                         for(a=0, sock= node->outputs.first; sock; sock= sock->next, a++) {
2377                                 if(nsout[a]->data) {
2378                                         free_compbuf(nsout[a]->data);
2379                                         nsout[a]->data= NULL;
2380                                 }
2381                         }
2382                         totnode++;
2383                         /* printf("node needs exec %s\n", node->name); */
2384                         
2385                         /* tag for getExecutableNode() */
2386                         node->exec= 0;
2387                 }
2388                 else {
2389                         /* tag for getExecutableNode() */
2390                         node->exec= NODE_READY|NODE_FINISHED|NODE_SKIPPED;
2391                         
2392                 }
2393         }
2394         
2395         /* last step: set the stack values for only-value nodes */
2396         /* just does all now, compared to a full buffer exec this is nothing */
2397         if(totnode) {
2398                 for(node= ntree->nodes.first; node; node= node->next) {
2399                         if(node->need_exec==0 && node_only_value(node)) {
2400                                 if(node->typeinfo->execfunc) {
2401                                         node_get_stack(node, thd->stack, nsin, nsout);
2402                                         node->typeinfo->execfunc(thd->rd, node, nsin, nsout);
2403                                 }
2404                         }
2405                 }
2406         }
2407         
2408         return totnode;
2409 }
2410
2411 /* while executing tree, free buffers from nodes that are not needed anymore */
2412 static void freeExecutableNode(bNodeTree *ntree)
2413 {
2414         /* node outputs can be freed when:
2415         - not a render result or image node
2416         - when node outputs go to nodes all being set NODE_FINISHED
2417         */
2418         bNode *node;
2419         bNodeSocket *sock;
2420         
2421         /* set exec flag for finished nodes that might need freed */
2422         for(node= ntree->nodes.first; node; node= node->next) {
2423                 if(node->type!=CMP_NODE_R_LAYERS)
2424                         if(node->exec & NODE_FINISHED)
2425                                 node->exec |= NODE_FREEBUFS;
2426         }
2427         /* clear this flag for input links that are not done yet */
2428         for(node= ntree->nodes.first; node; node= node->next) {
2429                 if((node->exec & NODE_FINISHED)==0) {
2430                         for(sock= node->inputs.first; sock; sock= sock->next)
2431                                 if(sock->link)
2432                                         sock->link->fromnode->exec &= ~NODE_FREEBUFS;
2433                 }
2434         }
2435         /* now we can free buffers */
2436         for(node= ntree->nodes.first; node; node= node->next) {
2437                 if(node->exec & NODE_FREEBUFS) {
2438                         for(sock= node->outputs.first; sock; sock= sock->next) {
2439                                 bNodeStack *ns= ntree->stack + sock->stack_index;
2440                                 if(ns->data) {
2441                                         free_compbuf(ns->data);
2442                                         ns->data= NULL;
2443                                         // printf("freed buf node %s \n", node->name);
2444                                 }
2445                         }
2446                 }
2447         }
2448 }
2449
2450 static bNode *getExecutableNode(bNodeTree *ntree)
2451 {
2452         bNode *node;
2453         bNodeSocket *sock;
2454         
2455         for(node= ntree->nodes.first; node; node= node->next) {
2456                 if(node->exec==0) {
2457                         
2458                         /* input sockets should be ready */
2459                         for(sock= node->inputs.first; sock; sock= sock->next) {
2460                                 if(sock->link)
2461                                         if((sock->link->fromnode->exec & NODE_READY)==0)
2462                                                 break;
2463                         }
2464                         if(sock==NULL)
2465                                 return node;
2466                 }
2467         }
2468         return NULL;
2469 }
2470
2471
2472 /* optimized tree execute test for compositing */
2473 void ntreeCompositExecTree(bNodeTree *ntree, RenderData *rd, int do_preview)
2474 {
2475         bNode *node;
2476         ListBase threads;
2477         ThreadData thdata;
2478         int totnode, rendering= 1;
2479         
2480         if(ntree==NULL) return;
2481         
2482         if(do_preview)
2483                 ntreeInitPreview(ntree, 0, 0);
2484         
2485         ntreeBeginExecTree(ntree);
2486         
2487         /* prevent unlucky accidents */
2488         if(G.background)
2489                 rd->scemode &= ~R_COMP_CROP;
2490         
2491         /* setup callerdata for thread callback */
2492         thdata.rd= rd;
2493         thdata.stack= ntree->stack;
2494         
2495         /* fixed seed, for example noise texture */
2496         BLI_srandom(rd->cfra);
2497
2498         /* sets need_exec tags in nodes */
2499         totnode= setExecutableNodes(ntree, &thdata);
2500
2501         BLI_init_threads(&threads, exec_composite_node, rd->threads);
2502         
2503         while(rendering) {
2504                 
2505                 if(BLI_available_threads(&threads)) {
2506                         node= getExecutableNode(ntree);
2507                         if(node) {
2508                                 
2509                                 if(ntree->timecursor)
2510                                         ntree->timecursor(ntree->tch, totnode);
2511                                 if(ntree->stats_draw) {
2512                                         char str[64];
2513                                         sprintf(str, "Compositing %d %s", totnode, node->name);
2514                                         ntree->stats_draw(ntree->sdh, str);
2515                                 }
2516                                 totnode--;
2517                                 
2518                                 node->threaddata = &thdata;
2519                                 node->exec= NODE_PROCESSING;
2520                                 BLI_insert_thread(&threads, node);
2521                         }
2522                         else
2523                                 PIL_sleep_ms(50);
2524                 }
2525                 else
2526                         PIL_sleep_ms(50);
2527                 
2528                 rendering= 0;
2529                 /* test for ESC */
2530                 if(ntree->test_break && ntree->test_break(ntree->tbh)) {
2531                         for(node= ntree->nodes.first; node; node= node->next)
2532                                 node->exec |= NODE_READY;
2533                 }
2534                 
2535                 /* check for ready ones, and if we need to continue */
2536                 for(node= ntree->nodes.first; node; node= node->next) {
2537                         if(node->exec & NODE_READY) {
2538                                 if((node->exec & NODE_FINISHED)==0) {
2539                                         BLI_remove_thread(&threads, node); /* this waits for running thread to finish btw */
2540                                         node->exec |= NODE_FINISHED;
2541                                         
2542                                         /* freeing unused buffers */
2543                                         if(rd->scemode & R_COMP_FREE)
2544                                                 freeExecutableNode(ntree);
2545                                 }
2546                         }
2547                         else rendering= 1;
2548                 }
2549         }
2550         
2551         BLI_end_threads(&threads);
2552         
2553         ntreeEndExecTree(ntree);
2554 }
2555
2556
2557 /* ********** copy composite tree entirely, to allow threaded exec ******************* */
2558 /* ***************** do NOT execute this in a thread!               ****************** */
2559
2560 /* returns localized composite tree for execution in threads */
2561 /* local tree then owns all compbufs */
2562 bNodeTree *ntreeLocalize(bNodeTree *ntree)
2563 {
2564         bNodeTree *ltree= ntreeCopyTree(ntree, 0);
2565         bNode *node;
2566         bNodeSocket *sock;
2567         
2568         /* move over the compbufs */
2569         /* right after ntreeCopyTree() oldsock pointers are valid */
2570         for(node= ntree->nodes.first; node; node= node->next) {
2571                 
2572                 /* store new_node pointer to original */
2573                 node->new_node->new_node= node;
2574                 /* ensure new user input gets handled ok */
2575                 node->need_exec= 0;
2576                 
2577                 if(ELEM(node->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER)) {
2578                         if(node->id) {
2579                                 if(node->flag & NODE_DO_OUTPUT)
2580                                         node->new_node->id= (ID *)BKE_image_copy((Image *)node->id);
2581                                 else
2582                                         node->new_node->id= NULL;
2583                         }
2584                 }
2585                 
2586                 for(sock= node->outputs.first; sock; sock= sock->next) {
2587                         
2588                         sock->new_sock->ns.data= sock->ns.data;
2589                         sock->ns.data= NULL;
2590                         sock->new_sock->new_sock= sock;
2591                 }
2592         }
2593         
2594         return ltree;
2595 }
2596
2597 static int node_exists(bNodeTree *ntree, bNode *testnode)
2598 {
2599         bNode *node= ntree->nodes.first;
2600         for(; node; node= node->next)
2601                 if(node==testnode)
2602                         return 1;
2603         return 0;
2604 }
2605
2606 static int outsocket_exists(bNode *node, bNodeSocket *testsock)
2607 {
2608         bNodeSocket *sock= node->outputs.first;
2609         for(; sock; sock= sock->next)
2610                 if(sock==testsock)
2611                         return 1;
2612         return 0;
2613 }
2614
2615
2616 /* sync local composite with real tree */
2617 /* local composite is supposed to be running, be careful moving previews! */
2618 void ntreeLocalSync(bNodeTree *localtree, bNodeTree *ntree)
2619 {
2620         bNode *lnode;
2621         
2622         /* move over the compbufs and previews */
2623         for(lnode= localtree->nodes.first; lnode; lnode= lnode->next) {
2624                 if( (lnode->exec & NODE_READY) && !(lnode->exec & NODE_SKIPPED) ) {
2625                         if(node_exists(ntree, lnode->new_node)) {
2626                                 
2627                                 if(lnode->preview && lnode->preview->rect) {
2628                                         node_free_preview(lnode->new_node);
2629                                         lnode->new_node->preview= lnode->preview;
2630                                         lnode->preview= NULL;
2631                                 }
2632                         }
2633                 }
2634         }
2635 }
2636
2637 /* merge local tree results back, and free local tree */
2638 /* we have to assume the editor already changed completely */
2639 void ntreeLocalMerge(bNodeTree *localtree, bNodeTree *ntree)
2640 {
2641         bNode *lnode;
2642         bNodeSocket *lsock;
2643         
2644         /* move over the compbufs and previews */
2645         for(lnode= localtree->nodes.first; lnode; lnode= lnode->next) {
2646                 if(node_exists(ntree, lnode->new_node)) {
2647                         
2648                         if(lnode->preview && lnode->preview->rect) {
2649                                 node_free_preview(lnode->new_node);
2650                                 lnode->new_node->preview= lnode->preview;
2651                                 lnode->preview= NULL;
2652                         }
2653                         
2654                         if(ELEM(lnode->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER)) {
2655                                 if(lnode->id && (lnode->flag & NODE_DO_OUTPUT)) {
2656                                         /* image_merge does sanity check for pointers */
2657                                         BKE_image_merge((Image *)lnode->new_node->id, (Image *)lnode->id);
2658                                 }
2659                         }
2660                         
2661                         for(lsock= lnode->outputs.first; lsock; lsock= lsock->next) {
2662                                 if(outsocket_exists(lnode->new_node, lsock->new_sock)) {
2663                                         lsock->new_sock->ns.data= lsock->ns.data;
2664                                         lsock->ns.data= NULL;
2665                                                 lsock->new_sock= NULL;
2666                                 }
2667                         }
2668                 }
2669         }
2670         ntreeFreeTree(localtree);
2671         MEM_freeN(localtree);
2672 }
2673
2674 /* *********************************************** */
2675
2676 /* GPU material from shader nodes */
2677
2678 static void gpu_from_node_stack(ListBase *sockets, bNodeStack **ns, GPUNodeStack *gs)
2679 {
2680         bNodeSocket *sock;
2681         int i;
2682
2683         for (sock=sockets->first, i=0; sock; sock=sock->next, i++) {
2684                 memset(&gs[i], 0, sizeof(gs[i]));
2685
2686         QUATCOPY(gs[i].vec, ns[i]->vec);
2687                 gs[i].link= ns[i]->data;
2688
2689                 if (sock->type == SOCK_VALUE)
2690                         gs[i].type= GPU_FLOAT;
2691                 else if (sock->type == SOCK_VECTOR)
2692                         gs[i].type= GPU_VEC3;
2693                 else if (sock->type == SOCK_RGBA)
2694                         gs[i].type= GPU_VEC4;
2695                 else
2696                         gs[i].type= GPU_NONE;
2697
2698                 gs[i].name = "";
2699                 gs[i].hasinput= ns[i]->hasinput && ns[i]->data;
2700                 gs[i].hasoutput= ns[i]->hasinput && ns[i]->data;
2701                 gs[i].sockettype= ns[i]->sockettype;
2702         }
2703
2704         gs[i].type= GPU_NONE;
2705 }
2706
2707 static void data_from_gpu_stack(ListBase *sockets, bNodeStack **ns, GPUNodeStack *gs)
2708 {
2709         bNodeSocket *sock;
2710         int i;
2711
2712         for (sock=sockets->first, i=0; sock; sock=sock->next, i++) {
2713                 ns[i]->data= gs[i].link;
2714                 ns[i]->hasinput= gs[i].hasinput && gs[i].link;
2715                 ns[i]->hasoutput= gs[i].hasoutput;
2716                 ns[i]->sockettype= gs[i].sockettype;
2717         }
2718 }
2719
2720 static void gpu_node_group_execute(bNodeStack *stack, GPUMaterial *mat, bNode *gnode, bNodeStack **in, bNodeStack **out)
2721 {
2722         bNode *node;
2723         bNodeTree *ntree= (bNodeTree *)gnode->id;
2724         bNodeStack *nsin[MAX_SOCKET];   /* arbitrary... watch this */
2725         bNodeStack *nsout[MAX_SOCKET];  /* arbitrary... watch this */
2726         GPUNodeStack gpuin[MAX_SOCKET+1], gpuout[MAX_SOCKET+1];
2727         int doit = 0;
2728         
2729         if(ntree==NULL) return;
2730         
2731         stack+= gnode->stack_index;
2732                 
2733         for(node= ntree->nodes.first; node; node= node->next) {
2734                 if(node->typeinfo->gpufunc) {
2735                         group_node_get_stack(node, stack, nsin, nsout, in, out);
2736
2737                         doit = 0;
2738                         
2739                         /* for groups, only execute outputs for edited group */
2740                         if(node->typeinfo->nclass==NODE_CLASS_OUTPUT) {
2741                                 if(gnode->flag & NODE_GROUP_EDIT)
2742                                         if(node->flag & NODE_DO_OUTPUT)
2743                                                 doit = 1;
2744                         }
2745                         else
2746                                 doit = 1;
2747
2748                         if(doit)  {
2749                                 gpu_from_node_stack(&node->inputs, nsin, gpuin);
2750                                 gpu_from_node_stack(&node->outputs, nsout, gpuout);
2751                                 if(node->typeinfo->gpufunc(mat, node, gpuin, gpuout))
2752                                         data_from_gpu_stack(&node->outputs, nsout, gpuout);
2753                         }
2754                 }
2755         }
2756 }
2757
2758 void ntreeGPUMaterialNodes(bNodeTree *ntree, GPUMaterial *mat)
2759 {
2760         bNode *node;
2761         bNodeStack *stack;
2762         bNodeStack *nsin[MAX_SOCKET];   /* arbitrary... watch this */
2763         bNodeStack *nsout[MAX_SOCKET];  /* arbitrary... watch this */
2764         GPUNodeStack gpuin[MAX_SOCKET+1], gpuout[MAX_SOCKET+1];
2765
2766         if((ntree->init & NTREE_EXEC_INIT)==0)
2767                 ntreeBeginExecTree(ntree);
2768
2769         stack= ntree->stack;
2770
2771         for(node= ntree->nodes.first; node; node= node->next) {
2772                 if(node->typeinfo->gpufunc) {
2773                         node_get_stack(node, stack, nsin, nsout);
2774                         gpu_from_node_stack(&node->inputs, nsin, gpuin);
2775                         gpu_from_node_stack(&node->outputs, nsout, gpuout);
2776                         if(node->typeinfo->gpufunc(mat, node, gpuin, gpuout))
2777                                 data_from_gpu_stack(&node->outputs, nsout, gpuout);
2778                 }
2779         else if(node->type==NODE_GROUP && node->id) {
2780                         node_get_stack(node, stack, nsin, nsout);
2781                         gpu_node_group_execute(stack, mat, node, nsin, nsout);
2782                 }
2783         }
2784
2785         ntreeEndExecTree(ntree);
2786 }
2787
2788 /* **************** call to switch lamploop for material node ************ */
2789
2790 void (*node_shader_lamp_loop)(struct ShadeInput *, struct ShadeResult *);
2791
2792 void set_node_shader_lamp_loop(void (*lamp_loop_func)(ShadeInput *, ShadeResult *))
2793 {
2794         node_shader_lamp_loop= lamp_loop_func;
2795 }
2796
2797 /* clumsy checking... should do dynamic outputs once */
2798 static void force_hidden_passes(bNode *node, int passflag)
2799 {
2800         bNodeSocket *sock;
2801         
2802         for(sock= node->outputs.first; sock; sock= sock->next)
2803                 sock->flag &= ~SOCK_UNAVAIL;
2804         
2805         sock= BLI_findlink(&node->outputs, RRES_OUT_Z);
2806         if(!(passflag & SCE_PASS_Z)) sock->flag |= SOCK_UNAVAIL;
2807         sock= BLI_findlink(&node->outputs, RRES_OUT_NORMAL);
2808         if(!(passflag & SCE_PASS_NORMAL)) sock->flag |= SOCK_UNAVAIL;
2809         sock= BLI_findlink(&node->outputs, RRES_OUT_VEC);
2810         if(!(passflag & SCE_PASS_VECTOR)) sock->flag |= SOCK_UNAVAIL;
2811         sock= BLI_findlink(&node->outputs, RRES_OUT_UV);
2812         if(!(passflag & SCE_PASS_UV)) sock->flag |= SOCK_UNAVAIL;
2813         sock= BLI_findlink(&node->outputs, RRES_OUT_RGBA);
2814         if(!(passflag & SCE_PASS_RGBA)) sock->flag |= SOCK_UNAVAIL;
2815         sock= BLI_findlink(&node->outputs, RRES_OUT_DIFF);
2816         if(!(passflag & SCE_PASS_DIFFUSE)) sock->flag |= SOCK_UNAVAIL;
2817         sock= BLI_findlink(&node->outputs, RRES_OUT_SPEC);
2818         if(!(passflag & SCE_PASS_SPEC)) sock->flag |= SOCK_UNAVAIL;
2819         sock= BLI_findlink(&node->outputs, RRES_OUT_SHADOW);
2820         if(!(passflag & SCE_PASS_SHADOW)) sock->flag |= SOCK_UNAVAIL;
2821         sock= BLI_findlink(&node->outputs, RRES_OUT_AO);
2822         if(!(passflag & SCE_PASS_AO)) sock->flag |= SOCK_UNAVAIL;
2823         sock= BLI_findlink(&node->outputs, RRES_OUT_REFLECT);
2824         if(!(passflag & SCE_PASS_REFLECT)) sock->flag |= SOCK_UNAVAIL;
2825         sock= BLI_findlink(&node->outputs, RRES_OUT_REFRACT);
2826         if(!(passflag & SCE_PASS_REFRACT)) sock->flag |= SOCK_UNAVAIL;
2827         sock= BLI_findlink(&node->outputs, RRES_OUT_RADIO);
2828         if(!(passflag & SCE_PASS_RADIO)) sock->flag |= SOCK_UNAVAIL;
2829         sock= BLI_findlink(&node->outputs, RRES_OUT_INDEXOB);
2830         if(!(passflag & SCE_PASS_INDEXOB)) sock->flag |= SOCK_UNAVAIL;
2831         sock= BLI_findlink(&node->outputs, RRES_OUT_MIST);
2832         if(!(passflag & SCE_PASS_MIST)) sock->flag |= SOCK_UNAVAIL;
2833         
2834 }
2835
2836 /* based on rules, force sockets hidden always */
2837 void ntreeCompositForceHidden(bNodeTree *ntree, Scene *curscene)
2838 {
2839         bNode *node;
2840         
2841         if(ntree==NULL) return;
2842         
2843         for(node= ntree->nodes.first; node; node= node->next) {
2844                 if( node->type==CMP_NODE_R_LAYERS) {
2845                         Scene *sce= node->id?(Scene *)node->id:curscene;
2846                         SceneRenderLayer *srl= BLI_findlink(&sce->r.layers, node->custom1);
2847                         if(srl)
2848                                 force_hidden_passes(node, srl->passflag);
2849                 }
2850                 else if( node->type==CMP_NODE_IMAGE) {
2851                         Image *ima= (Image *)node->id;
2852                         if(ima) {
2853                                 if(ima->rr) {
2854                                         ImageUser *iuser= node->storage;
2855                                         RenderLayer *rl= BLI_findlink(&ima->rr->layers, iuser->layer);
2856                                         if(rl)
2857                                                 force_hidden_passes(node, rl->passflag);
2858                                         else
2859                                                 force_hidden_passes(node, 0);
2860                                 }
2861                                 else if(ima->type!=IMA_TYPE_MULTILAYER) {       /* if ->rr not yet read we keep inputs */
2862                                         force_hidden_passes(node, RRES_OUT_Z);
2863                                 }
2864                                 else
2865                                         force_hidden_passes(node, 0);
2866                         }
2867                         else
2868                                 force_hidden_passes(node, 0);
2869                 }
2870         }
2871
2872 }
2873
2874 /* called from render pipeline, to tag render input and output */
2875 /* need to do all scenes, to prevent errors when you re-render 1 scene */
2876 void ntreeCompositTagRender(Scene *curscene)
2877 {
2878         Scene *sce;
2879         
2880         for(sce= G.main->scene.first; sce; sce= sce->id.next) {
2881                 if(sce->nodetree) {
2882                         bNode *node;
2883                         
2884                         for(node= sce->nodetree->nodes.first; node; node= node->next) {
2885                                 if(node->id==(ID *)curscene || node->type==CMP_NODE_COMPOSITE)
2886                                         NodeTagChanged(sce->nodetree, node);
2887                         }
2888                 }
2889         }
2890 }
2891
2892 /* tags nodes that have animation capabilities */
2893 int ntreeCompositTagAnimated(bNodeTree *ntree)
2894 {
2895         bNode *node;
2896         int tagged= 0;
2897         
2898         if(ntree==NULL) return 0;
2899         
2900         for(node= ntree->nodes.first; node; node= node->next) {
2901                 if(node->type==CMP_NODE_IMAGE) {
2902                         Image *ima= (Image *)node->id;
2903                         if(ima && ELEM(ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE)) {
2904                                 NodeTagChanged(ntree, node);
2905                                 tagged= 1;
2906                         }
2907                 }
2908                 else if(node->type==CMP_NODE_TIME) {
2909                         NodeTagChanged(ntree, node);
2910                         tagged= 1;
2911                 }
2912                 else if(node->type==CMP_NODE_R_LAYERS) {
2913                         NodeTagChanged(ntree, node);
2914                         tagged= 1;
2915                 }
2916                 else if(node->type==NODE_GROUP) {
2917                         if( ntreeCompositTagAnimated((bNodeTree *)node->id) ) {
2918                                 NodeTagChanged(ntree, node);
2919                         }
2920                 }
2921         }
2922         
2923         return tagged;
2924 }
2925
2926
2927 /* called from image window preview */
2928 void ntreeCompositTagGenerators(bNodeTree *ntree)
2929 {
2930         bNode *node;
2931         
2932         if(ntree==NULL) return;
2933         
2934         for(node= ntree->nodes.first; node; node= node->next) {
2935                 if( ELEM(node->type, CMP_NODE_R_LAYERS, CMP_NODE_IMAGE))
2936                         NodeTagChanged(ntree, node);
2937         }
2938 }
2939
2940 int ntreeTexTagAnimated(bNodeTree *ntree)
2941 {
2942         bNode *node;
2943         
2944         if(ntree==NULL) return 0;
2945         
2946         for(node= ntree->nodes.first; node; node= node->next) {
2947                 if(node->type==TEX_NODE_CURVE_TIME) {
2948                         NodeTagChanged(ntree, node);
2949                         return 1;
2950                 }
2951                 else if(node->type==NODE_GROUP) {
2952                         if( ntreeTexTagAnimated((bNodeTree *)node->id) ) {
2953                                 return 1;
2954                         }
2955                 }
2956         }
2957         
2958         return 0;
2959 }
2960
2961 /* ************* node definition init ********** */
2962
2963 static bNodeType *is_nodetype_registered(ListBase *typelist, int type, ID *id) 
2964 {
2965         bNodeType *ntype= typelist->first;
2966         
2967         for(;ntype; ntype= ntype->next )
2968                 if(ntype->type==type && ntype->id==id)
2969                         return ntype;
2970         
2971         return NULL;
2972 }
2973
2974 /* type can be from a static array, we make copy for duplicate types (like group) */
2975 void nodeRegisterType(ListBase *typelist, const bNodeType *ntype) 
2976 {
2977         bNodeType *found= is_nodetype_registered(typelist, ntype->type, ntype->id);
2978         
2979         if(found==NULL) {
2980                 bNodeType *ntypen= MEM_callocN(sizeof(bNodeType), "node type");
2981                 *ntypen= *ntype;
2982                 BLI_addtail(typelist, ntypen);
2983         }
2984 }
2985
2986 static void registerCompositNodes(ListBase *ntypelist)
2987 {
2988         nodeRegisterType(ntypelist, &node_group_typeinfo);
2989         nodeRegisterType(ntypelist, &cmp_node_rlayers);
2990         nodeRegisterType(ntypelist, &cmp_node_image);
2991         nodeRegisterType(ntypelist, &cmp_node_texture);
2992         nodeRegisterType(ntypelist, &cmp_node_value);
2993         nodeRegisterType(ntypelist, &cmp_node_rgb);
2994         nodeRegisterType(ntypelist, &cmp_node_curve_time);
2995         
2996         nodeRegisterType(ntypelist, &cmp_node_composite);
2997         nodeRegisterType(ntypelist, &cmp_node_viewer);
2998         nodeRegisterType(ntypelist, &cmp_node_splitviewer);
2999         nodeRegisterType(ntypelist, &cmp_node_output_file);
3000         nodeRegisterType(ntypelist, &cmp_node_view_levels);
3001         
3002         nodeRegisterType(ntypelist, &cmp_node_curve_rgb);
3003         nodeRegisterType(ntypelist, &cmp_node_mix_rgb);
3004         nodeRegisterType(ntypelist, &cmp_node_hue_sat);
3005         nodeRegisterType(ntypelist, &cmp_node_brightcontrast);
3006         nodeRegisterType(ntypelist, &cmp_node_gamma);
3007         nodeRegisterType(ntypelist, &cmp_node_invert);
3008         nodeRegisterType(ntypelist, &cmp_node_alphaover);
3009         nodeRegisterType(ntypelist, &cmp_node_zcombine);
3010         
3011         nodeRegisterType(ntypelist, &cmp_node_normal);
3012         nodeRegisterType(ntypelist, &cmp_node_curve_vec);
3013         nodeRegisterType(ntypelist, &cmp_node_map_value);
3014         nodeRegisterType(ntypelist, &cmp_node_normalize);
3015         
3016         nodeRegisterType(ntypelist, &cmp_node_filter);
3017         nodeRegisterType(ntypelist, &cmp_node_blur);
3018         nodeRegisterType(ntypelist, &cmp_node_dblur);
3019         nodeRegisterType(ntypelist, &cmp_node_bilateralblur);
3020         nodeRegisterType(ntypelist, &cmp_node_vecblur);
3021         nodeRegisterType(ntypelist, &cmp_node_dilateerode);
3022         nodeRegisterType(ntypelist, &cmp_node_defocus);
3023         
3024         nodeRegisterType(ntypelist, &cmp_node_valtorgb);
3025         nodeRegisterType(ntypelist, &cmp_node_rgbtobw);
3026         nodeRegisterType(ntypelist, &cmp_node_setalpha);
3027         nodeRegisterType(ntypelist, &cmp_node_idmask);
3028         nodeRegisterType(ntypelist, &cmp_node_math);
3029         nodeRegisterType(ntypelist, &cmp_node_seprgba);
3030         nodeRegisterType(ntypelist, &cmp_node_combrgba);
3031         nodeRegisterType(ntypelist, &cmp_node_sephsva);
3032         nodeRegisterType(ntypelist, &cmp_node_combhsva);
3033         nodeRegisterType(ntypelist, &cmp_node_sepyuva);
3034         nodeRegisterType(ntypelist, &cmp_node_combyuva);
3035         nodeRegisterType(ntypelist, &cmp_node_sepycca);
3036         nodeRegisterType(ntypelist, &cmp_node_combycca);
3037         nodeRegisterType(ntypelist, &cmp_node_premulkey);
3038         
3039         nodeRegisterType(ntypelist, &cmp_node_diff_matte);
3040         nodeRegisterType(ntypelist, &cmp_node_distance_matte);
3041         nodeRegisterType(ntypelist, &cmp_node_chroma_matte);
3042         nodeRegisterType(ntypelist, &cmp_node_color_matte);
3043         nodeRegisterType(ntypelist, &cmp_node_channel_matte);
3044         nodeRegisterType(ntypelist, &cmp_node_color_spill);
3045         nodeRegisterType(ntypelist, &cmp_node_luma_matte);
3046         
3047         nodeRegisterType(ntypelist, &cmp_node_translate);
3048         nodeRegisterType(ntypelist, &cmp_node_rotate);
3049         nodeRegisterType(ntypelist, &cmp_node_scale);
3050         nodeRegisterType(ntypelist, &cmp_node_flip);
3051         nodeRegisterType(ntypelist, &cmp_node_crop);
3052         nodeRegisterType(ntypelist, &cmp_node_displace);
3053         nodeRegisterType(ntypelist, &cmp_node_mapuv);
3054         nodeRegisterType(ntypelist, &cmp_node_glare);
3055         nodeRegisterType(ntypelist, &cmp_node_tonemap);
3056         nodeRegisterType(ntypelist, &cmp_node_lensdist);
3057 }
3058
3059 static void registerShaderNodes(ListBase *ntypelist) 
3060 {
3061         nodeRegisterType(ntypelist, &node_group_typeinfo);
3062         nodeRegisterType(ntypelist, &sh_node_output);
3063         nodeRegisterType(ntypelist, &sh_node_mix_rgb);
3064         nodeRegisterType(ntypelist, &sh_node_valtorgb);
3065         nodeRegisterType(ntypelist, &sh_node_rgbtobw);
3066         nodeRegisterType(ntypelist, &sh_node_normal);
3067         nodeRegisterType(ntypelist, &sh_node_geom);
3068         nodeRegisterType(ntypelist, &sh_node_mapping);
3069         nodeRegisterType(ntypelist, &sh_node_curve_vec);
3070         nodeRegisterType(ntypelist, &sh_node_curve_rgb);
3071         nodeRegisterType(ntypelist, &sh_node_math);
3072         nodeRegisterType(ntypelist, &sh_node_vect_math);
3073         nodeRegisterType(ntypelist, &sh_node_squeeze);
3074         nodeRegisterType(ntypelist, &sh_node_camera);
3075         nodeRegisterType(ntypelist, &sh_node_material);
3076         nodeRegisterType(ntypelist, &sh_node_material_ext);
3077         nodeRegisterType(ntypelist, &sh_node_value);
3078         nodeRegisterType(ntypelist, &sh_node_rgb);
3079         nodeRegisterType(ntypelist, &sh_node_texture);
3080         nodeRegisterType(ntypelist, &node_dynamic_typeinfo);
3081         nodeRegisterType(ntypelist, &sh_node_invert);
3082         nodeRegisterType(ntypelist, &sh_node_seprgb);
3083         nodeRegisterType(ntypelist, &sh_node_combrgb);
3084         nodeRegisterType(ntypelist, &sh_node_hue_sat);
3085 }
3086
3087 static void registerTextureNodes(ListBase *ntypelist)
3088 {
3089         nodeRegisterType(ntypelist, &node_group_typeinfo);
3090         nodeRegisterType(ntypelist, &tex_node_math);
3091         nodeRegisterType(ntypelist, &tex_node_mix_rgb);
3092         nodeRegisterType(ntypelist, &tex_node_valtorgb);
3093         nodeRegisterType(ntypelist, &tex_node_rgbtobw);
3094         nodeRegisterType(ntypelist, &tex_node_valtonor);
3095         nodeRegisterType(ntypelist, &tex_node_curve_rgb);
3096         nodeRegisterType(ntypelist, &tex_node_curve_time);
3097         nodeRegisterType(ntypelist, &tex_node_invert);
3098         nodeRegisterType(ntypelist, &tex_node_hue_sat);
3099         nodeRegisterType(ntypelist, &tex_node_coord);
3100         nodeRegisterType(ntypelist, &tex_node_distance);