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