2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * The Original Code is Copyright (C) 2007 Blender Foundation.
19 * All rights reserved.
21 * The Original Code is: all of this file.
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/nodes/shader/node_shader_tree.c
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"
41 #include "BLI_listbase.h"
43 #include "BLI_threads.h"
44 #include "BLI_utildefines.h"
46 #include "BLF_translation.h"
48 #include "BKE_global.h"
51 #include "BKE_scene.h"
52 #include "BKE_utildefines.h"
54 #include "GPU_material.h"
56 #include "RE_shader_ext.h"
58 #include "node_exec.h"
59 #include "node_util.h"
60 #include "node_shader_util.h"
62 static void foreach_nodetree(Main *main, void *calldata, bNodeTreeCallback func)
68 for(ma= main->mat.first; ma; ma= ma->id.next)
70 func(calldata, &ma->id, ma->nodetree);
72 for(la= main->lamp.first; la; la= la->id.next)
74 func(calldata, &la->id, la->nodetree);
76 for(wo= main->world.first; wo; wo= wo->id.next)
78 func(calldata, &wo->id, wo->nodetree);
81 static void foreach_nodeclass(Scene *scene, void *calldata, bNodeClassCallback func)
83 func(calldata, NODE_CLASS_INPUT, IFACE_("Input"));
84 func(calldata, NODE_CLASS_OUTPUT, IFACE_("Output"));
86 if(scene_use_new_shading_nodes(scene)) {
87 func(calldata, NODE_CLASS_SHADER, IFACE_("Shader"));
88 func(calldata, NODE_CLASS_TEXTURE, IFACE_("Texture"));
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"));
98 static void localize(bNodeTree *localtree, bNodeTree *UNUSED(ntree))
100 bNode *node, *node_next;
102 /* replace muted nodes by internal links */
103 for (node= localtree->nodes.first; node; node= node_next) {
104 node_next = node->next;
106 if (node->flag & NODE_MUTED) {
107 nodeInternalRelink(localtree, node);
108 nodeFreeNode(localtree, node);
113 static void local_sync(bNodeTree *localtree, bNodeTree *ntree)
117 /* copy over contents of previews */
118 for(lnode= localtree->nodes.first; lnode; lnode= lnode->next) {
119 if(ntreeNodeExists(ntree, lnode->new_node)) {
120 bNode *node= lnode->new_node;
122 if(node->preview && node->preview->rect) {
123 if(lnode->preview && lnode->preview->rect) {
124 int xsize= node->preview->xsize;
125 int ysize= node->preview->ysize;
126 memcpy(node->preview->rect, lnode->preview->rect, 4*xsize + xsize*ysize*sizeof(char)*4);
133 static void update(bNodeTree *ntree)
135 ntreeSetOutput(ntree);
138 bNodeTreeType ntreeType_Shader = {
139 /* type */ NTREE_SHADER,
140 /* id_name */ "NTShader Nodetree",
142 /* node_types */ { NULL, NULL },
144 /* free_cache */ NULL,
145 /* free_node_cache */ NULL,
146 /* foreach_nodetree */ foreach_nodetree,
147 /* foreach_nodeclass */ foreach_nodeclass,
148 /* localize */ localize,
149 /* local_sync */ local_sync,
150 /* local_merge */ NULL,
152 /* update_node */ NULL,
153 /* validate_link */ NULL,
154 /* internal_connect */ node_internal_connect_default
157 /* GPU material from shader nodes */
159 void ntreeGPUMaterialNodes(bNodeTree *ntree, GPUMaterial *mat)
163 exec = ntreeShaderBeginExecTree(ntree, 1);
165 ntreeExecGPUNodes(exec, mat, 1);
167 ntreeShaderEndExecTree(exec, 1);
170 /* **************** call to switch lamploop for material node ************ */
172 void (*node_shader_lamp_loop)(struct ShadeInput *, struct ShadeResult *);
174 void set_node_shader_lamp_loop(void (*lamp_loop_func)(ShadeInput *, ShadeResult *))
176 node_shader_lamp_loop= lamp_loop_func;
180 /* XXX Group nodes must set use_tree_data to false, since their trees can be shared by multiple nodes.
181 * If use_tree_data is true, the ntree->execdata pointer is checked to avoid multiple execution of top-level trees.
183 bNodeTreeExec *ntreeShaderBeginExecTree(bNodeTree *ntree, int use_tree_data)
189 /* XXX hack: prevent exec data from being generated twice.
190 * this should be handled by the renderer!
193 return ntree->execdata;
196 /* ensures only a single output node is enabled */
197 ntreeSetOutput(ntree);
199 /* common base initialization */
200 exec = ntree_exec_begin(ntree);
202 /* allocate the thread stack listbase array */
203 exec->threadstack= MEM_callocN(BLENDER_MAX_THREADS*sizeof(ListBase), "thread stack array");
205 for(node= exec->nodetree->nodes.first; node; node= node->next)
209 /* XXX this should not be necessary, but is still used for cmp/sha/tex nodes,
210 * which only store the ntree pointer. Should be fixed at some point!
212 ntree->execdata = exec;
218 /* XXX Group nodes must set use_tree_data to false, since their trees can be shared by multiple nodes.
219 * If use_tree_data is true, the ntree->execdata pointer is checked to avoid multiple execution of top-level trees.
221 void ntreeShaderEndExecTree(bNodeTreeExec *exec, int use_tree_data)
224 bNodeTree *ntree= exec->nodetree;
225 bNodeThreadStack *nts;
228 if(exec->threadstack) {
229 for(a=0; a<BLENDER_MAX_THREADS; a++) {
230 for(nts=exec->threadstack[a].first; nts; nts=nts->next)
231 if (nts->stack) MEM_freeN(nts->stack);
232 BLI_freelistN(&exec->threadstack[a]);
235 MEM_freeN(exec->threadstack);
236 exec->threadstack= NULL;
239 ntree_exec_end(exec);
242 /* XXX clear nodetree backpointer to exec data, same problem as noted in ntreeBeginExecTree */
243 ntree->execdata = NULL;
248 void ntreeShaderExecTree(bNodeTree *ntree, ShadeInput *shi, ShadeResult *shr)
252 @note: preserve material from ShadeInput for material id, nodetree execs change it
253 fix for bug "[#28012] Mat ID messy with shader nodes"
255 Material *mat = shi->mat;
256 bNodeThreadStack *nts = NULL;
257 bNodeTreeExec *exec = ntree->execdata;
259 /* convert caller data to struct */
263 /* each material node has own local shaderesult, with optional copying */
264 memset(shr, 0, sizeof(ShadeResult));
266 /* ensure execdata is only initialized once */
268 BLI_lock_thread(LOCK_NODES);
270 ntree->execdata = ntreeShaderBeginExecTree(ntree, 1);
271 BLI_unlock_thread(LOCK_NODES);
273 exec = ntree->execdata;
276 nts= ntreeGetThreadStack(exec, shi->thread);
277 ntreeExecThreadNodes(exec, nts, &scd, shi->thread);
278 ntreeReleaseThreadStack(nts);
280 // @note: set material back to preserved material
282 /* better not allow negative for now */
283 if(shr->combined[0]<0.0f) shr->combined[0]= 0.0f;
284 if(shr->combined[1]<0.0f) shr->combined[1]= 0.0f;
285 if(shr->combined[2]<0.0f) shr->combined[2]= 0.0f;