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/texture/node_texture_tree.c
35 #include "DNA_texture_types.h"
36 #include "DNA_node_types.h"
38 #include "BLI_listbase.h"
39 #include "BLI_threads.h"
40 #include "BLI_utildefines.h"
42 #include "BKE_global.h"
46 #include "node_exec.h"
47 #include "node_util.h"
48 #include "NOD_texture.h"
49 #include "node_texture_util.h"
51 #include "RE_pipeline.h"
52 #include "RE_shader_ext.h"
55 static void foreach_nodetree(Main *main, void *calldata, bNodeTreeCallback func)
58 for(tx= main->tex.first; tx; tx= tx->id.next) {
60 func(calldata, &tx->id, tx->nodetree);
65 static void local_sync(bNodeTree *localtree, bNodeTree *ntree)
69 /* copy over contents of previews */
70 for(lnode= localtree->nodes.first; lnode; lnode= lnode->next) {
71 if(ntreeNodeExists(ntree, lnode->new_node)) {
72 bNode *node= lnode->new_node;
74 if(node->preview && node->preview->rect) {
75 if(lnode->preview && lnode->preview->rect) {
76 int xsize= node->preview->xsize;
77 int ysize= node->preview->ysize;
78 memcpy(node->preview->rect, lnode->preview->rect, 4*xsize + xsize*ysize*sizeof(char)*4);
85 bNodeTreeType ntreeType_Texture = {
86 /* type */ NTREE_TEXTURE,
87 /* id_name */ "NTTexture Nodetree",
89 /* node_types */ { NULL, NULL },
91 /* free_cache */ NULL,
92 /* free_node_cache */ NULL,
93 /* foreach_nodetree */ foreach_nodetree,
95 /* local_sync */ local_sync,
96 /* local_merge */ NULL,
98 /* update_node */ NULL
101 int ntreeTexTagAnimated(bNodeTree *ntree)
105 if(ntree==NULL) return 0;
107 for(node= ntree->nodes.first; node; node= node->next) {
108 if(node->type==TEX_NODE_CURVE_TIME) {
109 nodeUpdate(ntree, node);
112 else if(node->type==NODE_GROUP) {
113 if( ntreeTexTagAnimated((bNodeTree *)node->id) ) {
122 /* XXX Group nodes must set use_tree_data to false, since their trees can be shared by multiple nodes.
123 * If use_tree_data is true, the ntree->execdata pointer is checked to avoid multiple execution of top-level trees.
125 bNodeTreeExec *ntreeTexBeginExecTree(bNodeTree *ntree, int use_tree_data)
131 /* XXX hack: prevent exec data from being generated twice.
132 * this should be handled by the renderer!
135 return ntree->execdata;
138 /* common base initialization */
139 exec = ntree_exec_begin(ntree);
141 /* allocate the thread stack listbase array */
142 exec->threadstack= MEM_callocN(BLENDER_MAX_THREADS*sizeof(ListBase), "thread stack array");
144 for(node= exec->nodetree->nodes.first; node; node= node->next)
148 /* XXX this should not be necessary, but is still used for cmp/sha/tex nodes,
149 * which only store the ntree pointer. Should be fixed at some point!
151 ntree->execdata = exec;
157 /* free texture delegates */
158 static void tex_free_delegates(bNodeTreeExec *exec)
160 bNodeThreadStack *nts;
164 for(th=0; th<BLENDER_MAX_THREADS; th++)
165 for(nts=exec->threadstack[th].first; nts; nts=nts->next)
166 for(ns= nts->stack, a=0; a<exec->stacksize; a++, ns++)
167 if(ns->data && !ns->is_copy)
171 /* XXX Group nodes must set use_tree_data to false, since their trees can be shared by multiple nodes.
172 * If use_tree_data is true, the ntree->execdata pointer is checked to avoid multiple execution of top-level trees.
174 void ntreeTexEndExecTree(bNodeTreeExec *exec, int use_tree_data)
177 bNodeTree *ntree= exec->nodetree;
178 bNodeThreadStack *nts;
181 if(exec->threadstack) {
182 tex_free_delegates(exec);
184 for(a=0; a<BLENDER_MAX_THREADS; a++) {
185 for(nts=exec->threadstack[a].first; nts; nts=nts->next)
186 if (nts->stack) MEM_freeN(nts->stack);
187 BLI_freelistN(&exec->threadstack[a]);
190 MEM_freeN(exec->threadstack);
191 exec->threadstack= NULL;
194 ntree_exec_end(exec);
197 /* XXX clear nodetree backpointer to exec data, same problem as noted in ntreeBeginExecTree */
198 ntree->execdata = NULL;
203 int ntreeTexExecTree(
207 float *dxt, float *dyt,
218 float *nor= texres->nor;
219 int retval = TEX_INT;
220 bNodeThreadStack *nts = NULL;
221 bNodeTreeExec *exec= nodes->execdata;
226 data.osatex = osatex;
227 data.target = texres;
228 data.do_preview = preview;
229 data.thread = thread;
230 data.which_output = which_output;
236 exec = ntreeTexBeginExecTree(nodes, 1);
238 nts= ntreeGetThreadStack(exec, thread);
239 ntreeExecThreadNodes(exec, nts, &data, thread);
240 ntreeReleaseThreadStack(nts);
242 if(texres->nor) retval |= TEX_NOR;
244 /* confusing stuff; the texture output node sets this to NULL to indicate no normal socket was set
245 however, the texture code checks this for other reasons (namely, a normal is required for material) */