78792956684dd897348c00557f96ecf5d1e37902
[blender.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                         nodeUpdate(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 /* XXX Group nodes must set use_tree_data to false, since their trees can be shared by multiple nodes.
123  * If use_tree_data is true, the ntree->execdata pointer is checked to avoid multiple execution of top-level trees.
124  */
125 bNodeTreeExec *ntreeTexBeginExecTree(bNodeTree *ntree, int use_tree_data)
126 {
127         bNodeTreeExec *exec;
128         bNode *node;
129         
130         if (use_tree_data) {
131                 /* XXX hack: prevent exec data from being generated twice.
132                  * this should be handled by the renderer!
133                  */
134                 if (ntree->execdata)
135                         return ntree->execdata;
136         }
137         
138         /* common base initialization */
139         exec = ntree_exec_begin(ntree);
140         
141         /* allocate the thread stack listbase array */
142         exec->threadstack= MEM_callocN(BLENDER_MAX_THREADS*sizeof(ListBase), "thread stack array");
143         
144         for(node= exec->nodetree->nodes.first; node; node= node->next)
145                 node->need_exec= 1;
146         
147         if (use_tree_data) {
148                 /* XXX this should not be necessary, but is still used for cmp/sha/tex nodes,
149                  * which only store the ntree pointer. Should be fixed at some point!
150                  */
151                 ntree->execdata = exec;
152         }
153         
154         return exec;
155 }
156
157 /* free texture delegates */
158 static void tex_free_delegates(bNodeTreeExec *exec)
159 {
160         bNodeThreadStack *nts;
161         bNodeStack *ns;
162         int th, a;
163         
164         for(th=0; th<BLENDER_MAX_THREADS; th++)
165                 for(nts=exec->threadstack[th].first; nts; nts=nts->next)
166                         for(ns= nts->stack, a=0; a<exec->stacksize; a++, ns++)
167                                 if(ns->data && !ns->is_copy)
168                                         MEM_freeN(ns->data);
169 }
170
171 /* XXX Group nodes must set use_tree_data to false, since their trees can be shared by multiple nodes.
172  * If use_tree_data is true, the ntree->execdata pointer is checked to avoid multiple execution of top-level trees.
173  */
174 void ntreeTexEndExecTree(bNodeTreeExec *exec, int use_tree_data)
175 {
176         if(exec) {
177                 bNodeTree *ntree= exec->nodetree;
178                 bNodeThreadStack *nts;
179                 int a;
180                 
181                 if(exec->threadstack) {
182                         tex_free_delegates(exec);
183                         
184                         for(a=0; a<BLENDER_MAX_THREADS; a++) {
185                                 for(nts=exec->threadstack[a].first; nts; nts=nts->next)
186                                         if (nts->stack) MEM_freeN(nts->stack);
187                                 BLI_freelistN(&exec->threadstack[a]);
188                         }
189                         
190                         MEM_freeN(exec->threadstack);
191                         exec->threadstack= NULL;
192                 }
193                 
194                 ntree_exec_end(exec);
195                 
196                 if (use_tree_data) {
197                         /* XXX clear nodetree backpointer to exec data, same problem as noted in ntreeBeginExecTree */
198                         ntree->execdata = NULL;
199                 }
200         }
201 }
202
203 int ntreeTexExecTree(
204         bNodeTree *nodes,
205         TexResult *texres,
206         float *co,
207         float *dxt, float *dyt,
208         int osatex,
209         short thread, 
210         Tex *UNUSED(tex), 
211         short which_output, 
212         int cfra,
213         int preview,
214         ShadeInput *shi,
215         MTex *mtex
216 ){
217         TexCallData data;
218         float *nor= texres->nor;
219         int retval = TEX_INT;
220         bNodeThreadStack *nts = NULL;
221         bNodeTreeExec *exec= nodes->execdata;
222
223         data.co = co;
224         data.dxt = dxt;
225         data.dyt = dyt;
226         data.osatex = osatex;
227         data.target = texres;
228         data.do_preview = preview;
229         data.thread = thread;
230         data.which_output = which_output;
231         data.cfra= cfra;
232         data.mtex= mtex;
233         data.shi= shi;
234         
235         if (!exec)
236                 exec = ntreeTexBeginExecTree(nodes, 1);
237         
238         nts= ntreeGetThreadStack(exec, thread);
239         ntreeExecThreadNodes(exec, nts, &data, thread);
240         ntreeReleaseThreadStack(nts);
241
242         if(texres->nor) retval |= TEX_NOR;
243         retval |= TEX_RGB;
244         /* confusing stuff; the texture output node sets this to NULL to indicate no normal socket was set
245            however, the texture code checks this for other reasons (namely, a normal is required for material) */
246         texres->nor= nor;
247
248         return retval;
249 }