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