Fixed/restored translation of node classes (in foreach_nodeclass funcs)
[blender-staging.git] / source / blender / nodes / shader / node_shader_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/shader/node_shader_tree.c
29  *  \ingroup nodes
30  */
31
32
33 #include <string.h>
34
35 #include "DNA_lamp_types.h"
36 #include "DNA_material_types.h"
37 #include "DNA_node_types.h"
38 #include "DNA_scene_types.h"
39 #include "DNA_world_types.h"
40
41 #include "BLI_listbase.h"
42 #include "BLI_math.h"
43 #include "BLI_threads.h"
44 #include "BLI_utildefines.h"
45
46 #include "BLF_translation.h"
47
48 #include "BKE_global.h"
49 #include "BKE_main.h"
50 #include "BKE_node.h"
51 #include "BKE_scene.h"
52 #include "BKE_utildefines.h"
53
54 #include "GPU_material.h"
55
56 #include "RE_shader_ext.h"
57
58 #include "node_exec.h"
59 #include "node_util.h"
60 #include "node_shader_util.h"
61
62 static void foreach_nodetree(Main *main, void *calldata, bNodeTreeCallback func)
63 {
64         Material *ma;
65         Lamp *la;
66         World *wo;
67
68         for(ma= main->mat.first; ma; ma= ma->id.next)
69                 if(ma->nodetree)
70                         func(calldata, &ma->id, ma->nodetree);
71
72         for(la= main->lamp.first; la; la= la->id.next)
73                 if(la->nodetree)
74                         func(calldata, &la->id, la->nodetree);
75
76         for(wo= main->world.first; wo; wo= wo->id.next)
77                 if(wo->nodetree)
78                         func(calldata, &wo->id, wo->nodetree);
79 }
80
81 static void foreach_nodeclass(Scene *scene, void *calldata, bNodeClassCallback func)
82 {
83         func(calldata, NODE_CLASS_INPUT, IFACE_("Input"));
84         func(calldata, NODE_CLASS_OUTPUT, IFACE_("Output"));
85
86         if(scene_use_new_shading_nodes(scene)) {
87                 func(calldata, NODE_CLASS_SHADER, IFACE_("Shader"));
88                 func(calldata, NODE_CLASS_TEXTURE, IFACE_("Texture"));
89         }
90
91         func(calldata, NODE_CLASS_OP_COLOR, IFACE_("Color"));
92         func(calldata, NODE_CLASS_OP_VECTOR, IFACE_("Vector"));
93         func(calldata, NODE_CLASS_CONVERTOR, IFACE_("Convertor"));
94         func(calldata, NODE_CLASS_GROUP, IFACE_("Group"));
95         func(calldata, NODE_CLASS_LAYOUT, IFACE_("Layout"));
96 }
97
98 static void local_sync(bNodeTree *localtree, bNodeTree *ntree)
99 {
100         bNode *lnode;
101         
102         /* copy over contents of previews */
103         for(lnode= localtree->nodes.first; lnode; lnode= lnode->next) {
104                 if(ntreeNodeExists(ntree, lnode->new_node)) {
105                         bNode *node= lnode->new_node;
106                         
107                         if(node->preview && node->preview->rect) {
108                                 if(lnode->preview && lnode->preview->rect) {
109                                         int xsize= node->preview->xsize;
110                                         int ysize= node->preview->ysize;
111                                         memcpy(node->preview->rect, lnode->preview->rect, 4*xsize + xsize*ysize*sizeof(char)*4);
112                                 }
113                         }
114                 }
115         }
116 }
117
118 static void update(bNodeTree *ntree)
119 {
120         ntreeSetOutput(ntree);
121 }
122
123 bNodeTreeType ntreeType_Shader = {
124         /* type */                              NTREE_SHADER,
125         /* id_name */                   "NTShader Nodetree",
126         
127         /* node_types */                { NULL, NULL },
128         
129         /* free_cache */                NULL,
130         /* free_node_cache */   NULL,
131         /* foreach_nodetree */  foreach_nodetree,
132         /* foreach_nodeclass */ foreach_nodeclass,
133         /* localize */                  NULL,
134         /* local_sync */                local_sync,
135         /* local_merge */               NULL,
136         /* update */                    update,
137         /* update_node */               NULL
138 };
139
140 /* GPU material from shader nodes */
141
142 void ntreeGPUMaterialNodes(bNodeTree *ntree, GPUMaterial *mat)
143 {
144         bNodeTreeExec *exec;
145
146         exec = ntreeShaderBeginExecTree(ntree, 1);
147
148         ntreeExecGPUNodes(exec, mat, 1);
149
150         ntreeShaderEndExecTree(exec, 1);
151 }
152
153 /* **************** call to switch lamploop for material node ************ */
154
155 void (*node_shader_lamp_loop)(struct ShadeInput *, struct ShadeResult *);
156
157 void set_node_shader_lamp_loop(void (*lamp_loop_func)(ShadeInput *, ShadeResult *))
158 {
159         node_shader_lamp_loop= lamp_loop_func;
160 }
161
162
163 /* XXX Group nodes must set use_tree_data to false, since their trees can be shared by multiple nodes.
164  * If use_tree_data is true, the ntree->execdata pointer is checked to avoid multiple execution of top-level trees.
165  */
166 bNodeTreeExec *ntreeShaderBeginExecTree(bNodeTree *ntree, int use_tree_data)
167 {
168         bNodeTreeExec *exec;
169         bNode *node;
170         
171         if (use_tree_data) {
172                 /* XXX hack: prevent exec data from being generated twice.
173                  * this should be handled by the renderer!
174                  */
175                 if (ntree->execdata)
176                         return ntree->execdata;
177         }
178         
179         /* ensures only a single output node is enabled */
180         ntreeSetOutput(ntree);
181         
182         /* common base initialization */
183         exec = ntree_exec_begin(ntree);
184         
185         /* allocate the thread stack listbase array */
186         exec->threadstack= MEM_callocN(BLENDER_MAX_THREADS*sizeof(ListBase), "thread stack array");
187         
188         for(node= exec->nodetree->nodes.first; node; node= node->next)
189                 node->need_exec= 1;
190         
191         if (use_tree_data) {
192                 /* XXX this should not be necessary, but is still used for cmp/sha/tex nodes,
193                  * which only store the ntree pointer. Should be fixed at some point!
194                  */
195                 ntree->execdata = exec;
196         }
197         
198         return exec;
199 }
200
201 /* XXX Group nodes must set use_tree_data to false, since their trees can be shared by multiple nodes.
202  * If use_tree_data is true, the ntree->execdata pointer is checked to avoid multiple execution of top-level trees.
203  */
204 void ntreeShaderEndExecTree(bNodeTreeExec *exec, int use_tree_data)
205 {
206         if(exec) {
207                 bNodeTree *ntree= exec->nodetree;
208                 bNodeThreadStack *nts;
209                 int a;
210                 
211                 if(exec->threadstack) {
212                         for(a=0; a<BLENDER_MAX_THREADS; a++) {
213                                 for(nts=exec->threadstack[a].first; nts; nts=nts->next)
214                                         if (nts->stack) MEM_freeN(nts->stack);
215                                 BLI_freelistN(&exec->threadstack[a]);
216                         }
217                         
218                         MEM_freeN(exec->threadstack);
219                         exec->threadstack= NULL;
220                 }
221                 
222                 ntree_exec_end(exec);
223                 
224                 if (use_tree_data) {
225                         /* XXX clear nodetree backpointer to exec data, same problem as noted in ntreeBeginExecTree */
226                         ntree->execdata = NULL;
227                 }
228         }
229 }
230
231 void ntreeShaderExecTree(bNodeTree *ntree, ShadeInput *shi, ShadeResult *shr)
232 {
233         ShaderCallData scd;
234         /*
235         @note: preserve material from ShadeInput for material id, nodetree execs change it
236         fix for bug "[#28012] Mat ID messy with shader nodes"
237         */
238         Material *mat = shi->mat;
239         bNodeThreadStack *nts = NULL;
240         bNodeTreeExec *exec = ntree->execdata;
241         
242         /* convert caller data to struct */
243         scd.shi= shi;
244         scd.shr= shr;
245         
246         /* each material node has own local shaderesult, with optional copying */
247         memset(shr, 0, sizeof(ShadeResult));
248         
249         /* ensure execdata is only initialized once */
250         if (!exec) {
251                 BLI_lock_thread(LOCK_NODES);
252                 if(!ntree->execdata)
253                         ntree->execdata = ntreeShaderBeginExecTree(ntree, 1);
254                 BLI_unlock_thread(LOCK_NODES);
255
256                 exec = ntree->execdata;
257         }
258         
259         nts= ntreeGetThreadStack(exec, shi->thread);
260         ntreeExecThreadNodes(exec, nts, &scd, shi->thread);
261         ntreeReleaseThreadStack(nts);
262         
263         // @note: set material back to preserved material
264         shi->mat = mat;
265         /* better not allow negative for now */
266         if(shr->combined[0]<0.0f) shr->combined[0]= 0.0f;
267         if(shr->combined[1]<0.0f) shr->combined[1]= 0.0f;
268         if(shr->combined[2]<0.0f) shr->combined[2]= 0.0f;
269 }