84bb53e22151bf4ac723c7eb815b245bacca7072
[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 foreach_nodeclass(Scene *UNUSED(scene), void *calldata, bNodeClassCallback func)
66 {
67         func(calldata, NODE_CLASS_INPUT, "Input");
68         func(calldata, NODE_CLASS_OUTPUT, "Output");
69         func(calldata, NODE_CLASS_OP_COLOR, "Color");
70         func(calldata, NODE_CLASS_PATTERN, "Patterns");
71         func(calldata, NODE_CLASS_TEXTURE, "Textures");
72         func(calldata, NODE_CLASS_CONVERTOR, "Convertor");
73         func(calldata, NODE_CLASS_DISTORT, "Distort");
74         func(calldata, NODE_CLASS_GROUP, "Group");
75         func(calldata, NODE_CLASS_LAYOUT, "Layout");
76 }
77
78 static void local_sync(bNodeTree *localtree, bNodeTree *ntree)
79 {
80         bNode *lnode;
81         
82         /* copy over contents of previews */
83         for(lnode= localtree->nodes.first; lnode; lnode= lnode->next) {
84                 if(ntreeNodeExists(ntree, lnode->new_node)) {
85                         bNode *node= lnode->new_node;
86                         
87                         if(node->preview && node->preview->rect) {
88                                 if(lnode->preview && lnode->preview->rect) {
89                                         int xsize= node->preview->xsize;
90                                         int ysize= node->preview->ysize;
91                                         memcpy(node->preview->rect, lnode->preview->rect, 4*xsize + xsize*ysize*sizeof(char)*4);
92                                 }
93                         }
94                 }
95         }
96 }
97
98 bNodeTreeType ntreeType_Texture = {
99         /* type */                              NTREE_TEXTURE,
100         /* id_name */                   "NTTexture Nodetree",
101         
102         /* node_types */                { NULL, NULL },
103         
104         /* free_cache */                        NULL,
105         /* free_node_cache */           NULL,
106         /* foreach_nodetree */  foreach_nodetree,
107         /* foreach_nodeclass */ foreach_nodeclass,
108         /* localize */                  NULL,
109         /* local_sync */                local_sync,
110         /* local_merge */               NULL,
111         /* update */                    NULL,
112         /* update_node */               NULL
113 };
114
115 int ntreeTexTagAnimated(bNodeTree *ntree)
116 {
117         bNode *node;
118         
119         if(ntree==NULL) return 0;
120         
121         for(node= ntree->nodes.first; node; node= node->next) {
122                 if(node->type==TEX_NODE_CURVE_TIME) {
123                         nodeUpdate(ntree, node);
124                         return 1;
125                 }
126                 else if(node->type==NODE_GROUP) {
127                         if( ntreeTexTagAnimated((bNodeTree *)node->id) ) {
128                                 return 1;
129                         }
130                 }
131         }
132         
133         return 0;
134 }
135
136 /* XXX Group nodes must set use_tree_data to false, since their trees can be shared by multiple nodes.
137  * If use_tree_data is true, the ntree->execdata pointer is checked to avoid multiple execution of top-level trees.
138  */
139 bNodeTreeExec *ntreeTexBeginExecTree(bNodeTree *ntree, int use_tree_data)
140 {
141         bNodeTreeExec *exec;
142         bNode *node;
143         
144         if (use_tree_data) {
145                 /* XXX hack: prevent exec data from being generated twice.
146                  * this should be handled by the renderer!
147                  */
148                 if (ntree->execdata)
149                         return ntree->execdata;
150         }
151         
152         /* common base initialization */
153         exec = ntree_exec_begin(ntree);
154         
155         /* allocate the thread stack listbase array */
156         exec->threadstack= MEM_callocN(BLENDER_MAX_THREADS*sizeof(ListBase), "thread stack array");
157         
158         for(node= exec->nodetree->nodes.first; node; node= node->next)
159                 node->need_exec= 1;
160         
161         if (use_tree_data) {
162                 /* XXX this should not be necessary, but is still used for cmp/sha/tex nodes,
163                  * which only store the ntree pointer. Should be fixed at some point!
164                  */
165                 ntree->execdata = exec;
166         }
167         
168         return exec;
169 }
170
171 /* free texture delegates */
172 static void tex_free_delegates(bNodeTreeExec *exec)
173 {
174         bNodeThreadStack *nts;
175         bNodeStack *ns;
176         int th, a;
177         
178         for(th=0; th<BLENDER_MAX_THREADS; th++)
179                 for(nts=exec->threadstack[th].first; nts; nts=nts->next)
180                         for(ns= nts->stack, a=0; a<exec->stacksize; a++, ns++)
181                                 if(ns->data && !ns->is_copy)
182                                         MEM_freeN(ns->data);
183 }
184
185 /* XXX Group nodes must set use_tree_data to false, since their trees can be shared by multiple nodes.
186  * If use_tree_data is true, the ntree->execdata pointer is checked to avoid multiple execution of top-level trees.
187  */
188 void ntreeTexEndExecTree(bNodeTreeExec *exec, int use_tree_data)
189 {
190         if(exec) {
191                 bNodeTree *ntree= exec->nodetree;
192                 bNodeThreadStack *nts;
193                 int a;
194                 
195                 if(exec->threadstack) {
196                         tex_free_delegates(exec);
197                         
198                         for(a=0; a<BLENDER_MAX_THREADS; a++) {
199                                 for(nts=exec->threadstack[a].first; nts; nts=nts->next)
200                                         if (nts->stack) MEM_freeN(nts->stack);
201                                 BLI_freelistN(&exec->threadstack[a]);
202                         }
203                         
204                         MEM_freeN(exec->threadstack);
205                         exec->threadstack= NULL;
206                 }
207                 
208                 ntree_exec_end(exec);
209                 
210                 if (use_tree_data) {
211                         /* XXX clear nodetree backpointer to exec data, same problem as noted in ntreeBeginExecTree */
212                         ntree->execdata = NULL;
213                 }
214         }
215 }
216
217 int ntreeTexExecTree(
218         bNodeTree *nodes,
219         TexResult *texres,
220         float *co,
221         float *dxt, float *dyt,
222         int osatex,
223         short thread, 
224         Tex *UNUSED(tex), 
225         short which_output, 
226         int cfra,
227         int preview,
228         ShadeInput *shi,
229         MTex *mtex
230 ){
231         TexCallData data;
232         float *nor= texres->nor;
233         int retval = TEX_INT;
234         bNodeThreadStack *nts = NULL;
235         bNodeTreeExec *exec= nodes->execdata;
236
237         data.co = co;
238         data.dxt = dxt;
239         data.dyt = dyt;
240         data.osatex = osatex;
241         data.target = texres;
242         data.do_preview = preview;
243         data.thread = thread;
244         data.which_output = which_output;
245         data.cfra= cfra;
246         data.mtex= mtex;
247         data.shi= shi;
248         
249         /* ensure execdata is only initialized once */
250         if (!exec) {
251                 BLI_lock_thread(LOCK_NODES);
252                 if(!nodes->execdata)
253                         ntreeTexBeginExecTree(nodes, 1);
254                 BLI_unlock_thread(LOCK_NODES);
255
256                 exec= nodes->execdata;
257         }
258         
259         nts= ntreeGetThreadStack(exec, thread);
260         ntreeExecThreadNodes(exec, nts, &data, thread);
261         ntreeReleaseThreadStack(nts);
262
263         if(texres->nor) retval |= TEX_NOR;
264         retval |= TEX_RGB;
265         /* confusing stuff; the texture output node sets this to NULL to indicate no normal socket was set
266            however, the texture code checks this for other reasons (namely, a normal is required for material) */
267         texres->nor= nor;
268
269         return retval;
270 }