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