Merged the particles-2010 branch with node improvements into trunk.
[blender-staging.git] / source / blender / nodes / texture / node_texture_tree.c
1 /**
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2007 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s):
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/nodes/texture/node_texture_tree.c
29  *  \ingroup nodes
30  */
31
32
33 #include <string.h>
34
35 #include "DNA_texture_types.h"
36 #include "DNA_node_types.h"
37
38 #include "BLI_listbase.h"
39 #include "BLI_threads.h"
40 #include "BLI_utildefines.h"
41
42 #include "BKE_global.h"
43 #include "BKE_main.h"
44 #include "BKE_node.h"
45
46 #include "node_exec.h"
47 #include "node_util.h"
48 #include "NOD_texture.h"
49 #include "node_texture_util.h"
50
51 #include "RE_pipeline.h"
52 #include "RE_shader_ext.h"
53
54
55 static void foreach_nodetree(Main *main, void *calldata, bNodeTreeCallback func)
56 {
57         Tex *tx;
58         for(tx= main->tex.first; tx; tx= tx->id.next) {
59                 if(tx->nodetree) {
60                         func(calldata, &tx->id, tx->nodetree);
61                 }
62         }
63 }
64
65 static void local_sync(bNodeTree *localtree, bNodeTree *ntree)
66 {
67         bNode *lnode;
68         
69         /* copy over contents of previews */
70         for(lnode= localtree->nodes.first; lnode; lnode= lnode->next) {
71                 if(ntreeNodeExists(ntree, lnode->new_node)) {
72                         bNode *node= lnode->new_node;
73                         
74                         if(node->preview && node->preview->rect) {
75                                 if(lnode->preview && lnode->preview->rect) {
76                                         int xsize= node->preview->xsize;
77                                         int ysize= node->preview->ysize;
78                                         memcpy(node->preview->rect, lnode->preview->rect, 4*xsize + xsize*ysize*sizeof(char)*4);
79                                 }
80                         }
81                 }
82         }
83 }
84
85 bNodeTreeType ntreeType_Texture = {
86         /* type */                              NTREE_TEXTURE,
87         /* id_name */                   "NTTexture Nodetree",
88         
89         /* node_types */                { NULL, NULL },
90         
91         /* free_cache */                        NULL,
92         /* free_node_cache */           NULL,
93         /* foreach_nodetree */  foreach_nodetree,
94         /* localize */                  NULL,
95         /* local_sync */                local_sync,
96         /* local_merge */               NULL,
97         /* update */                    NULL,
98         /* update_node */               NULL
99 };
100
101 int ntreeTexTagAnimated(bNodeTree *ntree)
102 {
103         bNode *node;
104         
105         if(ntree==NULL) return 0;
106         
107         for(node= ntree->nodes.first; node; node= node->next) {
108                 if(node->type==TEX_NODE_CURVE_TIME) {
109                         NodeTagChanged(ntree, node);
110                         return 1;
111                 }
112                 else if(node->type==NODE_GROUP) {
113                         if( ntreeTexTagAnimated((bNodeTree *)node->id) ) {
114                                 return 1;
115                         }
116                 }
117         }
118         
119         return 0;
120 }
121
122 bNodeTreeExec *ntreeTexBeginExecTree(bNodeTree *ntree)
123 {
124         bNodeTreeExec *exec;
125         bNode *node;
126         
127         /* XXX hack: prevent exec data from being generated twice.
128          * this should be handled by the renderer!
129          */
130         if (ntree->execdata)
131                 return ntree->execdata;
132         
133         /* common base initialization */
134         exec = ntree_exec_begin(ntree);
135         
136         /* allocate the thread stack listbase array */
137         exec->threadstack= MEM_callocN(BLENDER_MAX_THREADS*sizeof(ListBase), "thread stack array");
138         
139         for(node= exec->nodetree->nodes.first; node; node= node->next)
140                 node->need_exec= 1;
141         
142         /* XXX this should not be necessary, but is still used for cmp/sha/tex nodes,
143          * which only store the ntree pointer. Should be fixed at some point!
144          */
145         ntree->execdata = exec;
146         
147         return exec;
148 }
149
150 /* free texture delegates */
151 static void tex_free_delegates(bNodeTreeExec *exec)
152 {
153         bNodeThreadStack *nts;
154         bNodeStack *ns;
155         int th, a;
156         
157         for(th=0; th<BLENDER_MAX_THREADS; th++)
158                 for(nts=exec->threadstack[th].first; nts; nts=nts->next)
159                         for(ns= nts->stack, a=0; a<exec->stacksize; a++, ns++)
160                                 if(ns->data && !ns->is_copy)
161                                         MEM_freeN(ns->data);
162 }
163
164 void ntreeTexEndExecTree(bNodeTreeExec *exec)
165 {
166         if(exec) {
167                 bNodeTree *ntree= exec->nodetree;
168                 bNodeThreadStack *nts;
169                 int a;
170                 
171                 if(exec->threadstack) {
172                         tex_free_delegates(exec);
173                         
174                         for(a=0; a<BLENDER_MAX_THREADS; a++) {
175                                 for(nts=exec->threadstack[a].first; nts; nts=nts->next)
176                                         if (nts->stack) MEM_freeN(nts->stack);
177                                 BLI_freelistN(&exec->threadstack[a]);
178                         }
179                         
180                         MEM_freeN(exec->threadstack);
181                         exec->threadstack= NULL;
182                 }
183                 
184                 ntree_exec_end(exec);
185                 
186                 /* XXX clear nodetree backpointer to exec data, same problem as noted in ntreeBeginExecTree */
187                 ntree->execdata = NULL;
188         }
189 }
190
191 int ntreeTexExecTree(
192         bNodeTree *nodes,
193         TexResult *texres,
194         float *co,
195         float *dxt, float *dyt,
196         int osatex,
197         short thread, 
198         Tex *UNUSED(tex), 
199         short which_output, 
200         int cfra,
201         int preview,
202         ShadeInput *shi,
203         MTex *mtex
204 ){
205         TexCallData data;
206         float *nor= texres->nor;
207         int retval = TEX_INT;
208         bNodeThreadStack *nts = NULL;
209         bNodeTreeExec *exec= nodes->execdata;
210
211         data.co = co;
212         data.dxt = dxt;
213         data.dyt = dyt;
214         data.osatex = osatex;
215         data.target = texres;
216         data.do_preview = preview;
217         data.thread = thread;
218         data.which_output = which_output;
219         data.cfra= cfra;
220         data.mtex= mtex;
221         data.shi= shi;
222         
223         if (!exec)
224                 exec = ntreeTexBeginExecTree(nodes);
225         
226         nts= ntreeGetThreadStack(exec, thread);
227         ntreeExecThreadNodes(exec, nts, &data, thread);
228         ntreeReleaseThreadStack(nts);
229
230         if(texres->nor) retval |= TEX_NOR;
231         retval |= TEX_RGB;
232         /* confusing stuff; the texture output node sets this to NULL to indicate no normal socket was set
233            however, the texture code checks this for other reasons (namely, a normal is required for material) */
234         texres->nor= nor;
235
236         return retval;
237 }