cc8e161957005da7efedaec4963a83d08e74a73f
[blender.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_material_types.h"
36 #include "DNA_node_types.h"
37
38 #include "BLI_listbase.h"
39 #include "BLI_math.h"
40 #include "BLI_threads.h"
41 #include "BLI_utildefines.h"
42
43 #include "BKE_global.h"
44 #include "BKE_main.h"
45 #include "BKE_node.h"
46 #include "BKE_utildefines.h"
47
48 #include "GPU_material.h"
49
50 #include "RE_shader_ext.h"
51
52 #include "node_exec.h"
53 #include "node_util.h"
54 #include "node_shader_util.h"
55
56 static void foreach_nodetree(Main *main, void *calldata, bNodeTreeCallback func)
57 {
58         Material *ma;
59         for(ma= main->mat.first; ma; ma= ma->id.next) {
60                 if(ma->nodetree) {
61                         func(calldata, &ma->id, ma->nodetree);
62                 }
63         }
64 }
65
66 static void local_sync(bNodeTree *localtree, bNodeTree *ntree)
67 {
68         bNode *lnode;
69         
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;
74                         
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);
80                                 }
81                         }
82                 }
83         }
84 }
85
86 static void update(bNodeTree *ntree)
87 {
88         ntreeSetOutput(ntree);
89 }
90
91 bNodeTreeType ntreeType_Shader = {
92         /* type */                              NTREE_SHADER,
93         /* id_name */                   "NTShader Nodetree",
94         
95         /* node_types */                { NULL, NULL },
96         
97         /* free_cache */                NULL,
98         /* free_node_cache */   NULL,
99         /* foreach_nodetree */  foreach_nodetree,
100         /* localize */                  NULL,
101         /* local_sync */                local_sync,
102         /* local_merge */               NULL,
103         /* update */                    update,
104         /* update_node */               NULL
105 };
106
107 /* GPU material from shader nodes */
108
109 void ntreeGPUMaterialNodes(bNodeTree *ntree, GPUMaterial *mat)
110 {
111         bNodeTreeExec *exec;
112
113         exec = ntreeShaderBeginExecTree(ntree, 1);
114
115         ntreeExecGPUNodes(exec, mat, 1);
116
117         ntreeShaderEndExecTree(exec, 1);
118 }
119
120 /* **************** call to switch lamploop for material node ************ */
121
122 void (*node_shader_lamp_loop)(struct ShadeInput *, struct ShadeResult *);
123
124 void set_node_shader_lamp_loop(void (*lamp_loop_func)(ShadeInput *, ShadeResult *))
125 {
126         node_shader_lamp_loop= lamp_loop_func;
127 }
128
129
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.
132  */
133 bNodeTreeExec *ntreeShaderBeginExecTree(bNodeTree *ntree, int use_tree_data)
134 {
135         bNodeTreeExec *exec;
136         bNode *node;
137         
138         if (use_tree_data) {
139                 /* XXX hack: prevent exec data from being generated twice.
140                  * this should be handled by the renderer!
141                  */
142                 if (ntree->execdata)
143                         return ntree->execdata;
144         }
145         
146         /* ensures only a single output node is enabled */
147         ntreeSetOutput(ntree);
148         
149         /* common base initialization */
150         exec = ntree_exec_begin(ntree);
151         
152         /* allocate the thread stack listbase array */
153         exec->threadstack= MEM_callocN(BLENDER_MAX_THREADS*sizeof(ListBase), "thread stack array");
154         
155         for(node= exec->nodetree->nodes.first; node; node= node->next)
156                 node->need_exec= 1;
157         
158         if (use_tree_data) {
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!
161                  */
162                 ntree->execdata = exec;
163         }
164         
165         return exec;
166 }
167
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.
170  */
171 void ntreeShaderEndExecTree(bNodeTreeExec *exec, int use_tree_data)
172 {
173         if(exec) {
174                 bNodeTree *ntree= exec->nodetree;
175                 bNodeThreadStack *nts;
176                 int a;
177                 
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]);
183                         }
184                         
185                         MEM_freeN(exec->threadstack);
186                         exec->threadstack= NULL;
187                 }
188                 
189                 ntree_exec_end(exec);
190                 
191                 if (use_tree_data) {
192                         /* XXX clear nodetree backpointer to exec data, same problem as noted in ntreeBeginExecTree */
193                         ntree->execdata = NULL;
194                 }
195         }
196 }
197
198 void ntreeShaderExecTree(bNodeTree *ntree, ShadeInput *shi, ShadeResult *shr)
199 {
200         ShaderCallData scd;
201         /*
202         @note: preserve material from ShadeInput for material id, nodetree execs change it
203         fix for bug "[#28012] Mat ID messy with shader nodes"
204         */
205         Material *mat = shi->mat;       bNodeThreadStack *nts = NULL;
206         bNodeTreeExec *exec = ntree->execdata;
207         
208         /* convert caller data to struct */
209         scd.shi= shi;
210         scd.shr= shr;
211         
212         /* each material node has own local shaderesult, with optional copying */
213         memset(shr, 0, sizeof(ShadeResult));
214         
215         if (!exec)
216                 exec = ntree->execdata = ntreeShaderBeginExecTree(ntree, 1);
217         
218         nts= ntreeGetThreadStack(exec, shi->thread);
219         ntreeExecThreadNodes(exec, nts, &scd, shi->thread);
220         ntreeReleaseThreadStack(nts);
221         
222         // @note: set material back to preserved material
223         shi->mat = mat;
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;
228 }