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