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 "BLF_translation.h"
44
#include "BKE_global.h"
48
#include "node_exec.h"
49
#include "node_util.h"
50
#include "NOD_texture.h"
51
#include "node_texture_util.h"
53
#include "RE_pipeline.h"
54
#include "RE_shader_ext.h"
57
static void foreach_nodetree(Main *main, void *calldata, bNodeTreeCallback func)
60
for (tx= main->tex.first; tx; tx= tx->id.next) {
62
func(calldata, &tx->id, tx->nodetree);
67
static void foreach_nodeclass(Scene *UNUSED(scene), void *calldata, bNodeClassCallback func)
69
func(calldata, NODE_CLASS_INPUT, IFACE_("Input"));
70
func(calldata, NODE_CLASS_OUTPUT, IFACE_("Output"));
71
func(calldata, NODE_CLASS_OP_COLOR, IFACE_("Color"));
72
func(calldata, NODE_CLASS_PATTERN, IFACE_("Patterns"));
73
func(calldata, NODE_CLASS_TEXTURE, IFACE_("Textures"));
74
func(calldata, NODE_CLASS_CONVERTOR, IFACE_("Convertor"));
75
func(calldata, NODE_CLASS_DISTORT, IFACE_("Distort"));
76
func(calldata, NODE_CLASS_GROUP, IFACE_("Group"));
77
func(calldata, NODE_CLASS_LAYOUT, IFACE_("Layout"));
80
static void localize(bNodeTree *localtree, bNodeTree *UNUSED(ntree))
82
bNode *node, *node_next;
84
/* replace muted nodes by internal links */
85
for (node= localtree->nodes.first; node; node= node_next) {
86
node_next = node->next;
88
if (node->flag & NODE_MUTED) {
89
nodeInternalRelink(localtree, node);
90
nodeFreeNode(localtree, node);
95
static void local_sync(bNodeTree *localtree, bNodeTree *ntree)
99
/* copy over contents of previews */
100
for (lnode= localtree->nodes.first; lnode; lnode= lnode->next) {
101
if (ntreeNodeExists(ntree, lnode->new_node)) {
102
bNode *node= lnode->new_node;
104
if (node->preview && node->preview->rect) {
105
if (lnode->preview && lnode->preview->rect) {
106
int xsize= node->preview->xsize;
107
int ysize= node->preview->ysize;
108
memcpy(node->preview->rect, lnode->preview->rect, 4*xsize + xsize*ysize*sizeof(char)*4);
115
bNodeTreeType ntreeType_Texture = {
116
/* type */ NTREE_TEXTURE,
117
/* id_name */ "NTTexture Nodetree",
119
/* node_types */ { NULL, NULL },
121
/* free_cache */ NULL,
122
/* free_node_cache */ NULL,
123
/* foreach_nodetree */ foreach_nodetree,
124
/* foreach_nodeclass */ foreach_nodeclass,
125
/* localize */ localize,
126
/* local_sync */ local_sync,
127
/* local_merge */ NULL,
129
/* update_node */ NULL,
130
/* validate_link */ NULL,
131
/* internal_connect */ node_internal_connect_default
134
int ntreeTexTagAnimated(bNodeTree *ntree)
138
if (ntree==NULL) return 0;
140
for (node= ntree->nodes.first; node; node= node->next) {
141
if (node->type==TEX_NODE_CURVE_TIME) {
142
nodeUpdate(ntree, node);
145
else if (node->type==NODE_GROUP) {
146
if ( ntreeTexTagAnimated((bNodeTree *)node->id) ) {
155
/* XXX Group nodes must set use_tree_data to false, since their trees can be shared by multiple nodes.
156
* If use_tree_data is true, the ntree->execdata pointer is checked to avoid multiple execution of top-level trees.
158
bNodeTreeExec *ntreeTexBeginExecTree(bNodeTree *ntree, int use_tree_data)
164
/* XXX hack: prevent exec data from being generated twice.
165
* this should be handled by the renderer!
168
return ntree->execdata;
171
/* common base initialization */
172
exec = ntree_exec_begin(ntree);
174
/* allocate the thread stack listbase array */
175
exec->threadstack= MEM_callocN(BLENDER_MAX_THREADS*sizeof(ListBase), "thread stack array");
177
for (node= exec->nodetree->nodes.first; node; node= node->next)
181
/* XXX this should not be necessary, but is still used for cmp/sha/tex nodes,
182
* which only store the ntree pointer. Should be fixed at some point!
184
ntree->execdata = exec;
190
/* free texture delegates */
191
static void tex_free_delegates(bNodeTreeExec *exec)
193
bNodeThreadStack *nts;
197
for (th=0; th<BLENDER_MAX_THREADS; th++)
198
for (nts=exec->threadstack[th].first; nts; nts=nts->next)
199
for (ns= nts->stack, a=0; a<exec->stacksize; a++, ns++)
200
if (ns->data && !ns->is_copy)
204
/* XXX Group nodes must set use_tree_data to false, since their trees can be shared by multiple nodes.
205
* If use_tree_data is true, the ntree->execdata pointer is checked to avoid multiple execution of top-level trees.
207
void ntreeTexEndExecTree(bNodeTreeExec *exec, int use_tree_data)
210
bNodeTree *ntree= exec->nodetree;
211
bNodeThreadStack *nts;
214
if (exec->threadstack) {
215
tex_free_delegates(exec);
217
for (a=0; a<BLENDER_MAX_THREADS; a++) {
218
for (nts=exec->threadstack[a].first; nts; nts=nts->next)
219
if (nts->stack) MEM_freeN(nts->stack);
220
BLI_freelistN(&exec->threadstack[a]);
223
MEM_freeN(exec->threadstack);
224
exec->threadstack= NULL;
227
ntree_exec_end(exec);
230
/* XXX clear nodetree backpointer to exec data, same problem as noted in ntreeBeginExecTree */
231
ntree->execdata = NULL;
236
int ntreeTexExecTree(
240
float *dxt, float *dyt,
251
float *nor= texres->nor;
252
int retval = TEX_INT;
253
bNodeThreadStack *nts = NULL;
254
bNodeTreeExec *exec= nodes->execdata;
259
data.osatex = osatex;
260
data.target = texres;
261
data.do_preview = preview;
262
data.thread = thread;
263
data.which_output = which_output;
268
/* ensure execdata is only initialized once */
270
BLI_lock_thread(LOCK_NODES);
271
if (!nodes->execdata)
272
ntreeTexBeginExecTree(nodes, 1);
273
BLI_unlock_thread(LOCK_NODES);
275
exec= nodes->execdata;
278
nts= ntreeGetThreadStack(exec, thread);
279
ntreeExecThreadNodes(exec, nts, &data, thread);
280
ntreeReleaseThreadStack(nts);
282
if (texres->nor) retval |= TEX_NOR;
284
/* confusing stuff; the texture output node sets this to NULL to indicate no normal socket was set
285
however, the texture code checks this for other reasons (namely, a normal is required for material) */