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_material_types.h"
36 #include "DNA_node_types.h"
38 #include "BLI_listbase.h"
40 #include "BLI_threads.h"
41 #include "BLI_utildefines.h"
43 #include "BKE_global.h"
46 #include "BKE_utildefines.h"
48 #include "GPU_material.h"
50 #include "RE_shader_ext.h"
52 #include "node_exec.h"
53 #include "node_util.h"
54 #include "node_shader_util.h"
56 static void foreach_nodetree(Main *main, void *calldata, bNodeTreeCallback func)
59 for(ma= main->mat.first; ma; ma= ma->id.next) {
61 func(calldata, &ma->id, ma->nodetree);
66 static void local_sync(bNodeTree *localtree, bNodeTree *ntree)
70 /* copy over contents of previews */
71 for(lnode= localtree->nodes.first; lnode; lnode= lnode->next) {
72 if(ntreeNodeExists(ntree, lnode->new_node)) {
73 bNode *node= lnode->new_node;
75 if(node->preview && node->preview->rect) {
76 if(lnode->preview && lnode->preview->rect) {
77 int xsize= node->preview->xsize;
78 int ysize= node->preview->ysize;
79 memcpy(node->preview->rect, lnode->preview->rect, 4*xsize + xsize*ysize*sizeof(char)*4);
86 static void update(bNodeTree *ntree)
88 ntreeSetOutput(ntree);
91 bNodeTreeType ntreeType_Shader = {
92 /* type */ NTREE_SHADER,
93 /* id_name */ "NTShader Nodetree",
95 /* node_types */ { NULL, NULL },
97 /* free_cache */ NULL,
98 /* free_node_cache */ NULL,
99 /* foreach_nodetree */ foreach_nodetree,
101 /* local_sync */ local_sync,
102 /* local_merge */ NULL,
104 /* update_node */ NULL
107 /* GPU material from shader nodes */
109 void ntreeGPUMaterialNodes(bNodeTree *ntree, GPUMaterial *mat)
113 exec = ntreeShaderBeginExecTree(ntree, 1);
115 ntreeExecGPUNodes(exec, mat, 1);
117 ntreeShaderEndExecTree(exec, 1);
120 /* **************** call to switch lamploop for material node ************ */
122 void (*node_shader_lamp_loop)(struct ShadeInput *, struct ShadeResult *);
124 void set_node_shader_lamp_loop(void (*lamp_loop_func)(ShadeInput *, ShadeResult *))
126 node_shader_lamp_loop= lamp_loop_func;
130 /* XXX Group nodes must set use_tree_data to false, since their trees can be shared by multiple nodes.
131 * If use_tree_data is true, the ntree->execdata pointer is checked to avoid multiple execution of top-level trees.
133 bNodeTreeExec *ntreeShaderBeginExecTree(bNodeTree *ntree, int use_tree_data)
139 /* XXX hack: prevent exec data from being generated twice.
140 * this should be handled by the renderer!
143 return ntree->execdata;
146 /* ensures only a single output node is enabled */
147 ntreeSetOutput(ntree);
149 /* common base initialization */
150 exec = ntree_exec_begin(ntree);
152 /* allocate the thread stack listbase array */
153 exec->threadstack= MEM_callocN(BLENDER_MAX_THREADS*sizeof(ListBase), "thread stack array");
155 for(node= exec->nodetree->nodes.first; node; node= node->next)
159 /* XXX this should not be necessary, but is still used for cmp/sha/tex nodes,
160 * which only store the ntree pointer. Should be fixed at some point!
162 ntree->execdata = exec;
168 /* XXX Group nodes must set use_tree_data to false, since their trees can be shared by multiple nodes.
169 * If use_tree_data is true, the ntree->execdata pointer is checked to avoid multiple execution of top-level trees.
171 void ntreeShaderEndExecTree(bNodeTreeExec *exec, int use_tree_data)
174 bNodeTree *ntree= exec->nodetree;
175 bNodeThreadStack *nts;
178 if(exec->threadstack) {
179 for(a=0; a<BLENDER_MAX_THREADS; a++) {
180 for(nts=exec->threadstack[a].first; nts; nts=nts->next)
181 if (nts->stack) MEM_freeN(nts->stack);
182 BLI_freelistN(&exec->threadstack[a]);
185 MEM_freeN(exec->threadstack);
186 exec->threadstack= NULL;
189 ntree_exec_end(exec);
192 /* XXX clear nodetree backpointer to exec data, same problem as noted in ntreeBeginExecTree */
193 ntree->execdata = NULL;
198 void ntreeShaderExecTree(bNodeTree *ntree, ShadeInput *shi, ShadeResult *shr)
202 @note: preserve material from ShadeInput for material id, nodetree execs change it
203 fix for bug "[#28012] Mat ID messy with shader nodes"
205 Material *mat = shi->mat; bNodeThreadStack *nts = NULL;
206 bNodeTreeExec *exec = ntree->execdata;
208 /* convert caller data to struct */
212 /* each material node has own local shaderesult, with optional copying */
213 memset(shr, 0, sizeof(ShadeResult));
216 exec = ntree->execdata = ntreeShaderBeginExecTree(ntree, 1);
218 nts= ntreeGetThreadStack(exec, shi->thread);
219 ntreeExecThreadNodes(exec, nts, &scd, shi->thread);
220 ntreeReleaseThreadStack(nts);
222 // @note: set material back to preserved material
224 /* better not allow negative for now */
225 if(shr->combined[0]<0.0f) shr->combined[0]= 0.0f;
226 if(shr->combined[1]<0.0f) shr->combined[1]= 0.0f;
227 if(shr->combined[2]<0.0f) shr->combined[2]= 0.0f;