svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r20855:20928
[blender.git] / source / blender / blenkernel / intern / node.c
index 4e36df35a1492022a66b1cce81ddd856241e2076..0f42ba0d2e281ccfc3ce1f2cf801740e52c05b46 100644 (file)
  * ***** END GPL LICENSE BLOCK *****
  */
 
+#ifndef DISABLE_PYTHON
 #include <Python.h>
+#endif
+
 #include <stdlib.h>
 #include <string.h>
 
@@ -35,6 +38,7 @@
 #include "DNA_image_types.h"
 #include "DNA_node_types.h"
 #include "DNA_material_types.h"
+#include "DNA_texture_types.h"
 #include "DNA_text_types.h"
 #include "DNA_scene_types.h"
 
 #include "intern/CMP_util.h"   /* stupid include path... */
 
 #include "SHD_node.h"
+#include "TEX_node.h"
+#include "intern/TEX_util.h"
+
+#include "GPU_extensions.h"
+#include "GPU_material.h"
 
 static ListBase empty_list = {NULL, NULL};
 ListBase node_all_composit = {NULL, NULL};
 ListBase node_all_shaders = {NULL, NULL};
+ListBase node_all_textures = {NULL, NULL};
 
 /* ************** Type stuff **********  */
 
@@ -100,6 +110,8 @@ void ntreeInitTypes(bNodeTree *ntree)
                ntree->alltypes= node_all_shaders;
        else if(ntree->type==NTREE_COMPOSIT)
                ntree->alltypes= node_all_composit;
+       else if(ntree->type==NTREE_TEXTURE)
+               ntree->alltypes= node_all_textures;
        else {
                ntree->alltypes= empty_list;
                printf("Error: no type definitions for nodes\n");
@@ -655,6 +667,28 @@ void nodeVerifyGroup(bNodeTree *ngroup)
                        }
                }
        }
+       else if(ngroup->type==NTREE_TEXTURE) {
+               Tex *tx;
+               for(tx= G.main->tex.first; tx; tx= tx->id.next) {
+                       if(tx->nodetree) {
+                               bNode *node;
+                               
+                               /* find if group is in tree */
+                               for(node= tx->nodetree->nodes.first; node; node= node->next)
+                                       if(node->id == (ID *)ngroup)
+                                               break;
+                               
+                               if(node) {
+                                       /* set all type pointers OK */
+                                       ntreeInitTypes(tx->nodetree);
+                                       
+                                       for(node= tx->nodetree->nodes.first; node; node= node->next)
+                                               if(node->id == (ID *)ngroup)
+                                                       nodeVerifyType(tx->nodetree, node);
+                               }
+                       }
+               }
+       }
 }
 
 /* also to check all users of groups. Now only used in editor for hide/unhide */
@@ -711,6 +745,26 @@ void nodeGroupSocketUseFlags(bNodeTree *ngroup)
                        }
                }
        }
+       else if(ngroup->type==NTREE_TEXTURE) {
+               Tex *tx;
+               for(tx= G.main->tex.first; tx; tx= tx->id.next) {
+                       if(tx->nodetree) {
+                               for(node= tx->nodetree->nodes.first; node; node= node->next) {
+                                       if(node->id==(ID *)ngroup) {
+                                               for(sock= node->inputs.first; sock; sock= sock->next)
+                                                       if(sock->link)
+                                                               if(sock->tosock) 
+                                                                       sock->tosock->flag |= SOCK_IN_USE;
+                                               for(sock= node->outputs.first; sock; sock= sock->next)
+                                                       if(nodeCountSocketLinks(tx->nodetree, sock))
+                                                               if(sock->tosock) 
+                                                                       sock->tosock->flag |= SOCK_IN_USE;
+                                       }
+                               }
+                       }
+               }
+       }
+       
 }
 
 /* finds a node based on given socket */
@@ -895,9 +949,12 @@ bNode *nodeAddNodeType(bNodeTree *ntree, int type, bNodeTree *ngroup, ID *id)
        /* got it-bob*/
        if(ntype->initfunc!=NULL)
                ntype->initfunc(node);
+       
+       if(type==TEX_NODE_OUTPUT)
+               ntreeTexAssignIndex(ntree, node);
 
        nodeAddSockets(node, ntype);
-
+       
        return node;
 }
 
@@ -937,7 +994,7 @@ bNode *nodeCopyNode(struct bNodeTree *ntree, struct bNode *node, int internal)
        *nnode= *node;
        BLI_addtail(&ntree->nodes, nnode);
        
-       duplicatelist(&nnode->inputs, &node->inputs);
+       BLI_duplicatelist(&nnode->inputs, &node->inputs);
        oldsock= node->inputs.first;
        for(sock= nnode->inputs.first; sock; sock= sock->next, oldsock= oldsock->next) {
                oldsock->new_sock= sock;
@@ -945,7 +1002,7 @@ bNode *nodeCopyNode(struct bNodeTree *ntree, struct bNode *node, int internal)
                        sock->own_index= 0;
        }
        
-       duplicatelist(&nnode->outputs, &node->outputs);
+       BLI_duplicatelist(&nnode->outputs, &node->outputs);
        oldsock= node->outputs.first;
        for(sock= nnode->outputs.first; sock; sock= sock->next, oldsock= oldsock->next) {
                if(internal)
@@ -955,8 +1012,7 @@ bNode *nodeCopyNode(struct bNodeTree *ntree, struct bNode *node, int internal)
                oldsock->new_sock= sock;
        }
        
-       if(nnode->id)
-               nnode->id->us++;
+       /* don't increase node->id users, freenode doesn't decrement either */
        
        if(node->typeinfo->copystoragefunc)
                node->typeinfo->copystoragefunc(node, nnode);
@@ -964,6 +1020,9 @@ bNode *nodeCopyNode(struct bNodeTree *ntree, struct bNode *node, int internal)
        node->new_node= nnode;
        nnode->new_node= NULL;
        nnode->preview= NULL;
+       
+       if(node->type==TEX_NODE_OUTPUT)
+               ntreeTexAssignIndex(ntree, node);
 
        return nnode;
 }
@@ -996,6 +1055,14 @@ bNodeTree *ntreeAddTree(int type)
        ntree->type= type;
        ntree->alltypes.first = NULL;
        ntree->alltypes.last = NULL;
+
+       /* this helps RNA identify ID pointers as nodetree */
+    if(ntree->type==NTREE_SHADER)
+               BLI_strncpy(ntree->id.name, "NTShader Nodetree", sizeof(ntree->id.name));
+    else if(ntree->type==NTREE_COMPOSIT)
+               BLI_strncpy(ntree->id.name, "NTComposit Nodetree", sizeof(ntree->id.name));
+    else if(ntree->type==NTREE_TEXTURE)
+               BLI_strncpy(ntree->id.name, "NTTexture Nodetree", sizeof(ntree->id.name));
        
        ntreeInitTypes(ntree);
        return ntree;
@@ -1064,7 +1131,8 @@ bNodeTree *ntreeCopyTree(bNodeTree *ntree, int internal_select)
        
        /* check for copying links */
        for(link= ntree->links.first; link; link= link->next) {
-               if(link->fromnode->new_node && link->tonode->new_node) {
+               if(link->fromnode==NULL || link->tonode==NULL);
+               else if(link->fromnode->new_node && link->tonode->new_node) {
                        nlink= nodeAddLink(newtree, link->fromnode->new_node, NULL, link->tonode->new_node, NULL);
                        /* sockets were copied in order */
                        for(a=0, sock= link->fromnode->outputs.first; sock; sock= sock->next, a++) {
@@ -1098,6 +1166,104 @@ bNodeTree *ntreeCopyTree(bNodeTree *ntree, int internal_select)
        return newtree;
 }
 
+/* *************** preview *********** */
+/* if node->preview, then we assume the rect to exist */
+
+static void node_free_preview(bNode *node)
+{
+       if(node->preview) {
+               if(node->preview->rect)
+                       MEM_freeN(node->preview->rect);
+               MEM_freeN(node->preview);
+               node->preview= NULL;
+       }       
+}
+
+static void node_init_preview(bNode *node, int xsize, int ysize)
+{
+       
+       if(node->preview==NULL) {
+               node->preview= MEM_callocN(sizeof(bNodePreview), "node preview");
+               //              printf("added preview %s\n", node->name);
+       }
+       
+       /* node previews can get added with variable size this way */
+       if(xsize==0 || ysize==0)
+               return;
+       
+       /* sanity checks & initialize */
+       if(node->preview->rect) {
+               if(node->preview->xsize!=xsize && node->preview->ysize!=ysize) {
+                       MEM_freeN(node->preview->rect);
+                       node->preview->rect= NULL;
+               }
+       }
+       
+       if(node->preview->rect==NULL) {
+               node->preview->rect= MEM_callocN(4*xsize + xsize*ysize*sizeof(float)*4, "node preview rect");
+               node->preview->xsize= xsize;
+               node->preview->ysize= ysize;
+       }
+}
+
+void ntreeInitPreview(bNodeTree *ntree, int xsize, int ysize)
+{
+       bNode *node;
+       
+       if(ntree==NULL)
+               return;
+       
+       for(node= ntree->nodes.first; node; node= node->next) {
+               if(node->typeinfo->flag & NODE_PREVIEW) /* hrms, check for closed nodes? */
+                       node_init_preview(node, xsize, ysize);
+               if(node->type==NODE_GROUP && (node->flag & NODE_GROUP_EDIT))
+                       ntreeInitPreview((bNodeTree *)node->id, xsize, ysize);
+       }               
+}
+
+static void nodeClearPreview(bNode *node)
+{
+       if(node->preview && node->preview->rect)
+               memset(node->preview->rect, 0, MEM_allocN_len(node->preview->rect));
+}
+
+/* use it to enforce clear */
+void ntreeClearPreview(bNodeTree *ntree)
+{
+       bNode *node;
+       
+       if(ntree==NULL)
+               return;
+       
+       for(node= ntree->nodes.first; node; node= node->next) {
+               if(node->typeinfo->flag & NODE_PREVIEW)
+                       nodeClearPreview(node);
+               if(node->type==NODE_GROUP && (node->flag & NODE_GROUP_EDIT))
+                       ntreeClearPreview((bNodeTree *)node->id);
+       }               
+}
+
+/* hack warning! this function is only used for shader previews, and 
+since it gets called multiple times per pixel for Ztransp we only
+add the color once. Preview gets cleared before it starts render though */
+void nodeAddToPreview(bNode *node, float *col, int x, int y)
+{
+       bNodePreview *preview= node->preview;
+       if(preview) {
+               if(x>=0 && y>=0) {
+                       if(x<preview->xsize && y<preview->ysize) {
+                               float *tar= preview->rect+ 4*((preview->xsize*y) + x);
+                               //if(tar[0]==0.0f) {
+                               QUATCOPY(tar, col);
+                               //}
+                       }
+                       //else printf("prv out bound x y %d %d\n", x, y);
+               }
+               //else printf("prv out bound x y %d %d\n", x, y);
+       }
+}
+
+
 /* ************** Free stuff ********** */
 
 /* goes over entire tree */
@@ -1155,11 +1321,8 @@ void nodeFreeNode(bNodeTree *ntree, bNode *node)
        BLI_freelistN(&node->inputs);
        BLI_freelistN(&node->outputs);
        
-       if(node->preview) {
-               if(node->preview->rect)
-                       MEM_freeN(node->preview->rect);
-               MEM_freeN(node->preview);
-       }
+       node_free_preview(node);
+
        if(node->typeinfo && node->typeinfo->freestoragefunc) {
                node->typeinfo->freestoragefunc(node);
        }
@@ -1254,6 +1417,22 @@ void ntreeMakeLocal(bNodeTree *ntree)
                        }
                }
        }
+       else if(ntree->type==NTREE_TEXTURE) {
+               Tex *tx;
+               for(tx= G.main->tex.first; tx; tx= tx->id.next) {
+                       if(tx->nodetree) {
+                               bNode *node;
+                               
+                               /* find if group is in tree */
+                               for(node= tx->nodetree->nodes.first; node; node= node->next) {
+                                       if(node->id == (ID *)ntree) {
+                                               if(tx->id.lib) lib= 1;
+                                               else local= 1;
+                                       }
+                               }
+                       }
+               }
+       }
        
        /* if all users are local, we simply make tree local */
        if(local && lib==0) {
@@ -1305,6 +1484,25 @@ void ntreeMakeLocal(bNodeTree *ntree)
                                }
                        }
                }
+               else if(ntree->type==NTREE_TEXTURE) {
+                       Tex *tx;
+                       for(tx= G.main->tex.first; tx; tx= tx->id.next) {
+                               if(tx->nodetree) {
+                                       bNode *node;
+                                       
+                                       /* find if group is in tree */
+                                       for(node= tx->nodetree->nodes.first; node; node= node->next) {
+                                               if(node->id == (ID *)ntree) {
+                                                       if(tx->id.lib==NULL) {
+                                                               node->id= &newtree->id;
+                                                               newtree->id.us++;
+                                                               ntree->id.us--;
+                                                       }
+                                               }
+                                       }
+                               }
+                       }
+               }
        }
 }
 
@@ -1549,7 +1747,8 @@ void ntreeSolveOrder(bNodeTree *ntree)
                might be different for editor or for "real" use... */
 }
 
-/* should be callback! */
+/* Should be callback! */
+/* Do not call execs here */
 void NodeTagChanged(bNodeTree *ntree, bNode *node)
 {
        if(ntree->type==NTREE_COMPOSIT) {
@@ -1557,14 +1756,8 @@ void NodeTagChanged(bNodeTree *ntree, bNode *node)
 
                for(sock= node->outputs.first; sock; sock= sock->next) {
                        if(sock->ns.data) {
-                               free_compbuf(sock->ns.data);
-                               sock->ns.data= NULL;
-                               
-                               //if(node->preview && node->preview->rect) {
-                               //      MEM_freeN(node->preview->rect);
-                               //      node->preview->rect= NULL;
-                               //}
-                                       
+                               //free_compbuf(sock->ns.data);
+                               //sock->ns.data= NULL;
                        }
                }
                node->need_exec= 1;
@@ -1586,95 +1779,6 @@ void NodeTagIDChanged(bNodeTree *ntree, ID *id)
 }
 
 
-/* *************** preview *********** */
-
-/* if node->preview, then we assume the rect to exist */
-
-static void nodeInitPreview(bNode *node, int xsize, int ysize)
-{
-       
-       if(node->preview==NULL) {
-               node->preview= MEM_callocN(sizeof(bNodePreview), "node preview");
-//             printf("added preview %s\n", node->name);
-       }
-       
-       /* node previews can get added with variable size this way */
-       if(xsize==0 || ysize==0)
-               return;
-       
-       /* sanity checks & initialize */
-       if(node->preview->rect) {
-               if(node->preview->xsize!=xsize && node->preview->ysize!=ysize) {
-                       MEM_freeN(node->preview->rect);
-                       node->preview->rect= NULL;
-               }
-       }
-       
-       if(node->preview->rect==NULL) {
-               node->preview->rect= MEM_callocN(4*xsize + xsize*ysize*sizeof(float)*4, "node preview rect");
-               node->preview->xsize= xsize;
-               node->preview->ysize= ysize;
-       }
-}
-
-void ntreeInitPreview(bNodeTree *ntree, int xsize, int ysize)
-{
-       bNode *node;
-       
-       if(ntree==NULL)
-               return;
-       
-       for(node= ntree->nodes.first; node; node= node->next) {
-               if(node->typeinfo->flag & NODE_PREVIEW) /* hrms, check for closed nodes? */
-                       nodeInitPreview(node, xsize, ysize);
-               if(node->type==NODE_GROUP && (node->flag & NODE_GROUP_EDIT))
-                       ntreeInitPreview((bNodeTree *)node->id, xsize, ysize);
-       }               
-}
-
-static void nodeClearPreview(bNode *node)
-{
-       if(node->preview && node->preview->rect)
-               memset(node->preview->rect, 0, MEM_allocN_len(node->preview->rect));
-}
-
-/* use it to enforce clear */
-void ntreeClearPreview(bNodeTree *ntree)
-{
-       bNode *node;
-       
-       if(ntree==NULL)
-               return;
-       
-       for(node= ntree->nodes.first; node; node= node->next) {
-               if(node->typeinfo->flag & NODE_PREVIEW)
-                       nodeClearPreview(node);
-               if(node->type==NODE_GROUP && (node->flag & NODE_GROUP_EDIT))
-                       ntreeClearPreview((bNodeTree *)node->id);
-       }               
-}
-
-/* hack warning! this function is only used for shader previews, and 
-   since it gets called multiple times per pixel for Ztransp we only
-   add the color once. Preview gets cleared before it starts render though */
-void nodeAddToPreview(bNode *node, float *col, int x, int y)
-{
-       bNodePreview *preview= node->preview;
-       if(preview) {
-               if(x>=0 && y>=0) {
-                       if(x<preview->xsize && y<preview->ysize) {
-                               float *tar= preview->rect+ 4*((preview->xsize*y) + x);
-                               if(tar[0]==0.0f) {
-                                       QUATCOPY(tar, col);
-                               }
-                       }
-                       //else printf("prv out bound x y %d %d\n", x, y);
-               }
-               //else printf("prv out bound x y %d %d\n", x, y);
-       }
-}
-
-
 
 /* ******************* executing ************* */
 
@@ -1807,7 +1911,7 @@ static void composit_begin_exec(bNodeTree *ntree, int is_group)
                                        sock->ns.data= NULL;
                                }
                        }
-               }               
+               }
                /* cannot initialize them while using in threads */
                if(ELEM3(node->type, CMP_NODE_TIME, CMP_NODE_CURVE_VEC, CMP_NODE_CURVE_RGB)) {
                        curvemapping_initialize(node->storage);
@@ -1886,9 +1990,9 @@ static void group_tag_used_outputs(bNode *gnode, bNodeStack *stack)
        }
 }
 
+/* notes below are ancient! (ton) */
 /* stack indices make sure all nodes only write in allocated data, for making it thread safe */
 /* only root tree gets the stack, to enable instances to have own stack entries */
-/* only two threads now! */
 /* per tree (and per group) unique indices are created */
 /* the index_ext we need to be able to map from groups to the group-node own stack */
 
@@ -1902,14 +2006,13 @@ static bNodeThreadStack *ntreeGetThreadStack(bNodeTree *ntree, int thread)
 {
        ListBase *lb= &ntree->threadstack[thread];
        bNodeThreadStack *nts;
-
+       
        for(nts=lb->first; nts; nts=nts->next) {
                if(!nts->used) {
                        nts->used= 1;
                        return nts;
                }
        }
-       
        nts= MEM_callocN(sizeof(bNodeThreadStack), "bNodeThreadStack");
        nts->stack= MEM_dupallocN(ntree->stack);
        nts->used= 1;
@@ -1923,12 +2026,28 @@ static void ntreeReleaseThreadStack(bNodeThreadStack *nts)
        nts->used= 0;
 }
 
+/* free texture delegates */
+static void tex_end_exec(bNodeTree *ntree)
+{
+       bNodeThreadStack *nts;
+       bNodeStack *ns;
+       int th, a;
+       
+       if(ntree->threadstack)
+               for(th=0; th<BLENDER_MAX_THREADS; th++)
+                       for(nts=ntree->threadstack[th].first; nts; nts=nts->next)
+                               for(ns= nts->stack, a=0; a<ntree->stacksize; a++, ns++)
+                                       if(ns->data)
+                                               MEM_freeN(ns->data);
+                                               
+}
+
 void ntreeBeginExecTree(bNodeTree *ntree)
 {
        /* let's make it sure */
        if(ntree->init & NTREE_EXEC_INIT)
                return;
-
+       
        /* allocate the thread stack listbase array */
        if(ntree->type!=NTREE_COMPOSIT)
                ntree->threadstack= MEM_callocN(BLENDER_MAX_THREADS*sizeof(ListBase), "thread stack array");
@@ -1950,6 +2069,11 @@ void ntreeBeginExecTree(bNodeTree *ntree)
                /* tag used outputs, so we know when we can skip operations */
                for(node= ntree->nodes.first; node; node= node->next) {
                        bNodeSocket *sock;
+                       
+                       /* composite has own need_exec tag handling */
+                       if(ntree->type!=NTREE_COMPOSIT)
+                               node->need_exec= 1;
+
                        for(sock= node->inputs.first; sock; sock= sock->next) {
                                if(sock->link) {
                                        ns= ntree->stack + sock->link->fromsock->stack_index;
@@ -1958,9 +2082,22 @@ void ntreeBeginExecTree(bNodeTree *ntree)
                                }
                                else
                                        sock->ns.sockettype= sock->type;
+                               
+                               if(sock->link) {
+                                       bNodeLink *link= sock->link;
+                                       /* this is the test for a cyclic case */
+                                       if(link->fromnode && link->tonode) {
+                                               if(link->fromnode->level >= link->tonode->level && link->tonode->level!=0xFFF);
+                                               else {
+                                                       node->need_exec= 0;
+                                               }
+                                       }
+                               }
                        }
+                       
                        if(node->type==NODE_GROUP && node->id)
                                group_tag_used_outputs(node, ntree->stack);
+                       
                }
                
                if(ntree->type==NTREE_COMPOSIT)
@@ -1980,6 +2117,8 @@ void ntreeEndExecTree(bNodeTree *ntree)
                /* another callback candidate! */
                if(ntree->type==NTREE_COMPOSIT)
                        composit_end_exec(ntree, 0);
+               else if(ntree->type==NTREE_TEXTURE)
+                       tex_end_exec(ntree);
                
                if(ntree->stack) {
                        MEM_freeN(ntree->stack);
@@ -2041,13 +2180,15 @@ void ntreeExecTree(bNodeTree *ntree, void *callerdata, int thread)
        }
        
        for(node= ntree->nodes.first; node; node= node->next) {
-               if(node->typeinfo->execfunc) {
-                       node_get_stack(node, stack, nsin, nsout);
-                       node->typeinfo->execfunc(callerdata, node, nsin, nsout);
-               }
-               else if(node->type==NODE_GROUP && node->id) {
-                       node_get_stack(node, stack, nsin, nsout);
-                       node_group_execute(stack, callerdata, node, nsin, nsout); 
+               if(node->need_exec) {
+                       if(node->typeinfo->execfunc) {
+                               node_get_stack(node, stack, nsin, nsout);
+                               node->typeinfo->execfunc(callerdata, node, nsin, nsout);
+                       }
+                       else if(node->type==NODE_GROUP && node->id) {
+                               node_get_stack(node, stack, nsin, nsout);
+                               node_group_execute(stack, callerdata, node, nsin, nsout); 
+                       }
                }
        }
 
@@ -2090,7 +2231,7 @@ static void *exec_composite_node(void *node_v)
        bNodeStack *nsin[MAX_SOCKET];   /* arbitrary... watch this */
        bNodeStack *nsout[MAX_SOCKET];  /* arbitrary... watch this */
        bNode *node= node_v;
-       ThreadData *thd= (ThreadData *)node->new_node; /* abuse */
+       ThreadData *thd= (ThreadData *)node->threaddata;
        
        node_get_stack(node, thd->stack, nsin, nsout);
        
@@ -2154,8 +2295,10 @@ static int setExecutableNodes(bNodeTree *ntree, ThreadData *thd)
                        /* is sock in use? */
                        else if(sock->link) {
                                bNodeLink *link= sock->link;
+                               
                                /* this is the test for a cyclic case */
-                               if(link->fromnode->level >= link->tonode->level && link->tonode->level!=0xFFF) {
+                               if(link->fromnode==NULL || link->tonode==NULL);
+                               else if(link->fromnode->level >= link->tonode->level && link->tonode->level!=0xFFF) {
                                        if(link->fromnode->need_exec) {
                                                node->need_exec= 1;
                                                break;
@@ -2185,7 +2328,7 @@ static int setExecutableNodes(bNodeTree *ntree, ThreadData *thd)
                }
                else {
                        /* tag for getExecutableNode() */
-                       node->exec= NODE_READY|NODE_FINISHED;
+                       node->exec= NODE_READY|NODE_FINISHED|NODE_SKIPPED;
                        
                }
        }
@@ -2295,7 +2438,7 @@ void ntreeCompositExecTree(bNodeTree *ntree, RenderData *rd, int do_preview)
 
        /* sets need_exec tags in nodes */
        totnode= setExecutableNodes(ntree, &thdata);
-       
+
        BLI_init_threads(&threads, exec_composite_node, rd->threads);
        
        while(rendering) {
@@ -2303,17 +2446,17 @@ void ntreeCompositExecTree(bNodeTree *ntree, RenderData *rd, int do_preview)
                if(BLI_available_threads(&threads)) {
                        node= getExecutableNode(ntree);
                        if(node) {
-
+                               
                                if(ntree->timecursor)
-                                       ntree->timecursor(totnode);
+                                       ntree->timecursor(ntree->tch, totnode);
                                if(ntree->stats_draw) {
                                        char str[64];
                                        sprintf(str, "Compositing %d %s", totnode, node->name);
-                                       ntree->stats_draw(str);
+                                       ntree->stats_draw(ntree->sdh, str);
                                }
                                totnode--;
                                
-                               node->new_node = (bNode *)&thdata;
+                               node->threaddata = &thdata;
                                node->exec= NODE_PROCESSING;
                                BLI_insert_thread(&threads, node);
                        }
@@ -2325,7 +2468,7 @@ void ntreeCompositExecTree(bNodeTree *ntree, RenderData *rd, int do_preview)
                
                rendering= 0;
                /* test for ESC */
-               if(ntree->test_break && ntree->test_break()) {
+               if(ntree->test_break && ntree->test_break(ntree->tbh)) {
                        for(node= ntree->nodes.first; node; node= node->next)
                                node->exec |= NODE_READY;
                }
@@ -2346,13 +2489,243 @@ void ntreeCompositExecTree(bNodeTree *ntree, RenderData *rd, int do_preview)
                }
        }
        
-       
        BLI_end_threads(&threads);
        
        ntreeEndExecTree(ntree);
 }
 
 
+/* ********** copy composite tree entirely, to allow threaded exec ******************* */
+/* ***************** do NOT execute this in a thread!               ****************** */
+
+/* returns localized composite tree for execution in threads */
+/* local tree then owns all compbufs */
+bNodeTree *ntreeLocalize(bNodeTree *ntree)
+{
+       bNodeTree *ltree= ntreeCopyTree(ntree, 0);
+       bNode *node;
+       bNodeSocket *sock;
+       
+       /* move over the compbufs */
+       /* right after ntreeCopyTree() oldsock pointers are valid */
+       for(node= ntree->nodes.first; node; node= node->next) {
+               
+               /* store new_node pointer to original */
+               node->new_node->new_node= node;
+               /* ensure new user input gets handled ok */
+               node->need_exec= 0;
+               
+               if(ELEM(node->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER)) {
+                       if(node->id) {
+                               if(node->flag & NODE_DO_OUTPUT)
+                                       node->new_node->id= (ID *)BKE_image_copy((Image *)node->id);
+                               else
+                                       node->new_node->id= NULL;
+                       }
+               }
+               
+               for(sock= node->outputs.first; sock; sock= sock->next) {
+                       
+                       sock->new_sock->ns.data= sock->ns.data;
+                       sock->ns.data= NULL;
+                       sock->new_sock->new_sock= sock;
+               }
+       }
+       
+       return ltree;
+}
+
+static int node_exists(bNodeTree *ntree, bNode *testnode)
+{
+       bNode *node= ntree->nodes.first;
+       for(; node; node= node->next)
+               if(node==testnode)
+                       return 1;
+       return 0;
+}
+
+static int outsocket_exists(bNode *node, bNodeSocket *testsock)
+{
+       bNodeSocket *sock= node->outputs.first;
+       for(; sock; sock= sock->next)
+               if(sock==testsock)
+                       return 1;
+       return 0;
+}
+
+
+/* sync local composite with real tree */
+/* local composite is supposed to be running, be careful moving previews! */
+void ntreeLocalSync(bNodeTree *localtree, bNodeTree *ntree)
+{
+       bNode *lnode;
+       
+       /* move over the compbufs and previews */
+       for(lnode= localtree->nodes.first; lnode; lnode= lnode->next) {
+               if( (lnode->exec & NODE_READY) && !(lnode->exec & NODE_SKIPPED) ) {
+                       if(node_exists(ntree, lnode->new_node)) {
+                               
+                               if(lnode->preview && lnode->preview->rect) {
+                                       node_free_preview(lnode->new_node);
+                                       lnode->new_node->preview= lnode->preview;
+                                       lnode->preview= NULL;
+                               }
+                       }
+               }
+       }
+}
+
+/* merge local tree results back, and free local tree */
+/* we have to assume the editor already changed completely */
+void ntreeLocalMerge(bNodeTree *localtree, bNodeTree *ntree)
+{
+       bNode *lnode;
+       bNodeSocket *lsock;
+       
+       /* move over the compbufs and previews */
+       for(lnode= localtree->nodes.first; lnode; lnode= lnode->next) {
+               if(node_exists(ntree, lnode->new_node)) {
+                       
+                       if(lnode->preview && lnode->preview->rect) {
+                               node_free_preview(lnode->new_node);
+                               lnode->new_node->preview= lnode->preview;
+                               lnode->preview= NULL;
+                       }
+                       
+                       if(ELEM(lnode->type, CMP_NODE_VIEWER, CMP_NODE_SPLITVIEWER)) {
+                               if(lnode->id && (lnode->flag & NODE_DO_OUTPUT)) {
+                                       /* image_merge does sanity check for pointers */
+                                       BKE_image_merge((Image *)lnode->new_node->id, (Image *)lnode->id);
+                               }
+                       }
+                       
+                       for(lsock= lnode->outputs.first; lsock; lsock= lsock->next) {
+                               if(outsocket_exists(lnode->new_node, lsock->new_sock)) {
+                                       lsock->new_sock->ns.data= lsock->ns.data;
+                                       lsock->ns.data= NULL;
+                                       lsock->new_sock= NULL;
+                               }
+                       }
+               }
+       }
+       ntreeFreeTree(localtree);
+       MEM_freeN(localtree);
+}
+
+/* *********************************************** */
+
+/* GPU material from shader nodes */
+
+static void gpu_from_node_stack(ListBase *sockets, bNodeStack **ns, GPUNodeStack *gs)
+{
+       bNodeSocket *sock;
+       int i;
+
+       for (sock=sockets->first, i=0; sock; sock=sock->next, i++) {
+               memset(&gs[i], 0, sizeof(gs[i]));
+
+       QUATCOPY(gs[i].vec, ns[i]->vec);
+               gs[i].link= ns[i]->data;
+
+               if (sock->type == SOCK_VALUE)
+                       gs[i].type= GPU_FLOAT;
+               else if (sock->type == SOCK_VECTOR)
+                       gs[i].type= GPU_VEC3;
+               else if (sock->type == SOCK_RGBA)
+                       gs[i].type= GPU_VEC4;
+               else
+                       gs[i].type= GPU_NONE;
+
+               gs[i].name = "";
+               gs[i].hasinput= ns[i]->hasinput && ns[i]->data;
+               gs[i].hasoutput= ns[i]->hasinput && ns[i]->data;
+               gs[i].sockettype= ns[i]->sockettype;
+       }
+
+       gs[i].type= GPU_NONE;
+}
+
+static void data_from_gpu_stack(ListBase *sockets, bNodeStack **ns, GPUNodeStack *gs)
+{
+       bNodeSocket *sock;
+       int i;
+
+       for (sock=sockets->first, i=0; sock; sock=sock->next, i++) {
+               ns[i]->data= gs[i].link;
+               ns[i]->hasinput= gs[i].hasinput && gs[i].link;
+               ns[i]->hasoutput= gs[i].hasoutput;
+               ns[i]->sockettype= gs[i].sockettype;
+       }
+}
+
+static void gpu_node_group_execute(bNodeStack *stack, GPUMaterial *mat, bNode *gnode, bNodeStack **in, bNodeStack **out)
+{
+       bNode *node;
+       bNodeTree *ntree= (bNodeTree *)gnode->id;
+       bNodeStack *nsin[MAX_SOCKET];   /* arbitrary... watch this */
+       bNodeStack *nsout[MAX_SOCKET];  /* arbitrary... watch this */
+       GPUNodeStack gpuin[MAX_SOCKET+1], gpuout[MAX_SOCKET+1];
+       int doit = 0;
+       
+       if(ntree==NULL) return;
+       
+       stack+= gnode->stack_index;
+               
+       for(node= ntree->nodes.first; node; node= node->next) {
+               if(node->typeinfo->gpufunc) {
+                       group_node_get_stack(node, stack, nsin, nsout, in, out);
+
+                       doit = 0;
+                       
+                       /* for groups, only execute outputs for edited group */
+                       if(node->typeinfo->nclass==NODE_CLASS_OUTPUT) {
+                               if(gnode->flag & NODE_GROUP_EDIT)
+                                       if(node->flag & NODE_DO_OUTPUT)
+                                               doit = 1;
+                       }
+                       else
+                               doit = 1;
+
+                       if(doit)  {
+                               gpu_from_node_stack(&node->inputs, nsin, gpuin);
+                               gpu_from_node_stack(&node->outputs, nsout, gpuout);
+                               if(node->typeinfo->gpufunc(mat, node, gpuin, gpuout))
+                                       data_from_gpu_stack(&node->outputs, nsout, gpuout);
+                       }
+               }
+       }
+}
+
+void ntreeGPUMaterialNodes(bNodeTree *ntree, GPUMaterial *mat)
+{
+       bNode *node;
+       bNodeStack *stack;
+       bNodeStack *nsin[MAX_SOCKET];   /* arbitrary... watch this */
+       bNodeStack *nsout[MAX_SOCKET];  /* arbitrary... watch this */
+       GPUNodeStack gpuin[MAX_SOCKET+1], gpuout[MAX_SOCKET+1];
+
+       if((ntree->init & NTREE_EXEC_INIT)==0)
+               ntreeBeginExecTree(ntree);
+
+       stack= ntree->stack;
+
+       for(node= ntree->nodes.first; node; node= node->next) {
+               if(node->typeinfo->gpufunc) {
+                       node_get_stack(node, stack, nsin, nsout);
+                       gpu_from_node_stack(&node->inputs, nsin, gpuin);
+                       gpu_from_node_stack(&node->outputs, nsout, gpuout);
+                       if(node->typeinfo->gpufunc(mat, node, gpuin, gpuout))
+                               data_from_gpu_stack(&node->outputs, nsout, gpuout);
+               }
+        else if(node->type==NODE_GROUP && node->id) {
+                       node_get_stack(node, stack, nsin, nsout);
+                       gpu_node_group_execute(stack, mat, node, nsin, nsout);
+               }
+       }
+
+       ntreeEndExecTree(ntree);
+}
+
 /* **************** call to switch lamploop for material node ************ */
 
 void (*node_shader_lamp_loop)(struct ShadeInput *, struct ShadeResult *);
@@ -2402,7 +2775,7 @@ static void force_hidden_passes(bNode *node, int passflag)
 }
 
 /* based on rules, force sockets hidden always */
-void ntreeCompositForceHidden(bNodeTree *ntree)
+void ntreeCompositForceHidden(bNodeTree *ntree, Scene *curscene)
 {
        bNode *node;
        
@@ -2410,7 +2783,7 @@ void ntreeCompositForceHidden(bNodeTree *ntree)
        
        for(node= ntree->nodes.first; node; node= node->next) {
                if( node->type==CMP_NODE_R_LAYERS) {
-                       Scene *sce= node->id?(Scene *)node->id:G.scene; /* G.scene is WEAK! */
+                       Scene *sce= node->id?(Scene *)node->id:curscene;
                        SceneRenderLayer *srl= BLI_findlink(&sce->r.layers, node->custom1);
                        if(srl)
                                force_hidden_passes(node, srl->passflag);
@@ -2505,6 +2878,27 @@ void ntreeCompositTagGenerators(bNodeTree *ntree)
        }
 }
 
+int ntreeTexTagAnimated(bNodeTree *ntree)
+{
+       bNode *node;
+       
+       if(ntree==NULL) return 0;
+       
+       for(node= ntree->nodes.first; node; node= node->next) {
+               if(node->type==TEX_NODE_CURVE_TIME) {
+                       NodeTagChanged(ntree, node);
+                       return 1;
+               }
+               else if(node->type==NODE_GROUP) {
+                       if( ntreeTexTagAnimated((bNodeTree *)node->id) ) {
+                               return 1;
+                       }
+               }
+       }
+       
+       return 0;
+}
+
 /* ************* node definition init ********** */
 
 static bNodeType *is_nodetype_registered(ListBase *typelist, int type, ID *id) 
@@ -2628,6 +3022,48 @@ static void registerShaderNodes(ListBase *ntypelist)
        nodeRegisterType(ntypelist, &sh_node_hue_sat);
 }
 
+static void registerTextureNodes(ListBase *ntypelist)
+{
+       nodeRegisterType(ntypelist, &node_group_typeinfo);
+       nodeRegisterType(ntypelist, &tex_node_math);
+       nodeRegisterType(ntypelist, &tex_node_mix_rgb);
+       nodeRegisterType(ntypelist, &tex_node_valtorgb);
+       nodeRegisterType(ntypelist, &tex_node_rgbtobw);
+       nodeRegisterType(ntypelist, &tex_node_valtonor);
+       nodeRegisterType(ntypelist, &tex_node_curve_rgb);
+       nodeRegisterType(ntypelist, &tex_node_curve_time);
+       nodeRegisterType(ntypelist, &tex_node_invert);
+       nodeRegisterType(ntypelist, &tex_node_hue_sat);
+       nodeRegisterType(ntypelist, &tex_node_coord);
+       nodeRegisterType(ntypelist, &tex_node_distance);
+       nodeRegisterType(ntypelist, &tex_node_compose);
+       nodeRegisterType(ntypelist, &tex_node_decompose);
+       
+       nodeRegisterType(ntypelist, &tex_node_output);
+       nodeRegisterType(ntypelist, &tex_node_viewer);
+       
+       nodeRegisterType(ntypelist, &tex_node_checker);
+       nodeRegisterType(ntypelist, &tex_node_texture);
+       nodeRegisterType(ntypelist, &tex_node_bricks);
+       nodeRegisterType(ntypelist, &tex_node_image);
+       
+       nodeRegisterType(ntypelist, &tex_node_rotate);
+       nodeRegisterType(ntypelist, &tex_node_translate);
+       nodeRegisterType(ntypelist, &tex_node_scale);
+       nodeRegisterType(ntypelist, &tex_node_at);
+       
+       nodeRegisterType(ntypelist, &tex_node_proc_voronoi);
+       nodeRegisterType(ntypelist, &tex_node_proc_blend);
+       nodeRegisterType(ntypelist, &tex_node_proc_magic);
+       nodeRegisterType(ntypelist, &tex_node_proc_marble);
+       nodeRegisterType(ntypelist, &tex_node_proc_clouds);
+       nodeRegisterType(ntypelist, &tex_node_proc_wood);
+       nodeRegisterType(ntypelist, &tex_node_proc_musgrave);
+       nodeRegisterType(ntypelist, &tex_node_proc_noise);
+       nodeRegisterType(ntypelist, &tex_node_proc_stucci);
+       nodeRegisterType(ntypelist, &tex_node_proc_distnoise);
+}
+
 static void remove_dynamic_typeinfos(ListBase *list)
 {
        bNodeType *ntype= list->first;
@@ -2665,6 +3101,7 @@ void init_nodesystem(void)
 {
        registerCompositNodes(&node_all_composit);
        registerShaderNodes(&node_all_shaders);
+       registerTextureNodes(&node_all_textures);
 }
 
 void free_nodesystem(void) 
@@ -2673,4 +3110,5 @@ void free_nodesystem(void)
        BLI_freelistN(&node_all_composit);
        remove_dynamic_typeinfos(&node_all_shaders);
        BLI_freelistN(&node_all_shaders);
+       BLI_freelistN(&node_all_textures);
 }