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