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