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"
37 #include "DNA_space_types.h"
39 #include "BLI_listbase.h"
40 #include "BLI_threads.h"
41 #include "BLI_utildefines.h"
43 #include "BLF_translation.h"
45 #include "BKE_context.h"
46 #include "BKE_linestyle.h"
48 #include "BKE_paint.h"
50 #include "node_common.h"
51 #include "node_exec.h"
52 #include "node_util.h"
53 #include "NOD_texture.h"
54 #include "node_texture_util.h"
56 #include "RNA_access.h"
58 #include "RE_shader_ext.h"
61 static void texture_get_from_context(const bContext *C, bNodeTreeType *UNUSED(treetype), bNodeTree **r_ntree, ID **r_id, ID **r_from)
63 SpaceNode *snode = CTX_wm_space_node(C);
64 Scene *scene = CTX_data_scene(C);
68 if (snode->texfrom == SNODE_TEX_OBJECT) {
70 tx = give_current_object_texture(ob);
72 if (ob->type == OB_LAMP)
73 *r_from = (ID *)ob->data;
75 *r_from = (ID *)give_current_material(ob, ob->actcol);
77 /* from is not set fully for material nodes, should be ID + Node then */
79 *r_ntree = tx->nodetree;
83 else if (snode->texfrom == SNODE_TEX_WORLD) {
85 *r_from = (ID *)scene->world;
86 tx = give_current_world_texture(scene->world);
89 *r_ntree = tx->nodetree;
93 else if (snode->texfrom == SNODE_TEX_BRUSH) {
94 struct Brush *brush = NULL;
96 if (ob && (ob->mode & OB_MODE_SCULPT))
97 brush = BKE_paint_brush(&scene->toolsettings->sculpt->paint);
99 brush = BKE_paint_brush(&scene->toolsettings->imapaint.paint);
102 *r_from = (ID *)brush;
103 tx = give_current_brush_texture(brush);
106 *r_ntree = tx->nodetree;
110 else if (snode->texfrom == SNODE_TEX_LINESTYLE) {
111 FreestyleLineStyle *linestyle = BKE_linestyle_active_from_scene(scene);
113 *r_from = (ID *)linestyle;
114 tx = give_current_linestyle_texture(linestyle);
117 *r_ntree = tx->nodetree;
123 static void foreach_nodeclass(Scene *UNUSED(scene), void *calldata, bNodeClassCallback func)
125 func(calldata, NODE_CLASS_INPUT, N_("Input"));
126 func(calldata, NODE_CLASS_OUTPUT, N_("Output"));
127 func(calldata, NODE_CLASS_OP_COLOR, N_("Color"));
128 func(calldata, NODE_CLASS_PATTERN, N_("Patterns"));
129 func(calldata, NODE_CLASS_TEXTURE, N_("Textures"));
130 func(calldata, NODE_CLASS_CONVERTOR, N_("Convertor"));
131 func(calldata, NODE_CLASS_DISTORT, N_("Distort"));
132 func(calldata, NODE_CLASS_GROUP, N_("Group"));
133 func(calldata, NODE_CLASS_INTERFACE, N_("Interface"));
134 func(calldata, NODE_CLASS_LAYOUT, N_("Layout"));
137 /* XXX muting disabled in previews because of threading issues with the main execution
138 * it works here, but disabled for consistency
141 static void localize(bNodeTree *localtree, bNodeTree *UNUSED(ntree))
143 bNode *node, *node_next;
145 /* replace muted nodes and reroute nodes by internal links */
146 for (node = localtree->nodes.first; node; node = node_next) {
147 node_next = node->next;
149 if (node->flag & NODE_MUTED || node->type == NODE_REROUTE) {
150 nodeInternalRelink(localtree, node);
151 nodeFreeNode(localtree, node);
156 static void localize(bNodeTree *UNUSED(localtree), bNodeTree *UNUSED(ntree))
161 static void local_sync(bNodeTree *localtree, bNodeTree *ntree)
163 BKE_node_preview_sync_tree(ntree, localtree);
166 static void local_merge(bNodeTree *localtree, bNodeTree *ntree)
168 BKE_node_preview_merge_tree(ntree, localtree, true);
171 static void update(bNodeTree *ntree)
173 ntree_update_reroute_nodes(ntree);
175 if (ntree->update & NTREE_UPDATE_NODES) {
176 /* clean up preview cache, in case nodes have been removed */
177 BKE_node_preview_remove_unused(ntree);
181 bNodeTreeType *ntreeType_Texture;
183 void register_node_tree_type_tex(void)
185 bNodeTreeType *tt = ntreeType_Texture = MEM_callocN(sizeof(bNodeTreeType), "texture node tree type");
187 tt->type = NTREE_TEXTURE;
188 strcpy(tt->idname, "TextureNodeTree");
189 strcpy(tt->ui_name, "Texture");
190 tt->ui_icon = 0; /* defined in drawnode.c */
191 strcpy(tt->ui_description, "Texture nodes");
193 tt->foreach_nodeclass = foreach_nodeclass;
195 tt->localize = localize;
196 tt->local_sync = local_sync;
197 tt->local_merge = local_merge;
198 tt->get_from_context = texture_get_from_context;
200 tt->ext.srna = &RNA_TextureNodeTree;
205 int ntreeTexTagAnimated(bNodeTree *ntree)
209 if (ntree == NULL) return 0;
211 for (node = ntree->nodes.first; node; node = node->next) {
212 if (node->type == TEX_NODE_CURVE_TIME) {
213 nodeUpdate(ntree, node);
216 else if (node->type == NODE_GROUP) {
217 if (ntreeTexTagAnimated((bNodeTree *)node->id) ) {
226 bNodeTreeExec *ntreeTexBeginExecTree_internal(bNodeExecContext *context, bNodeTree *ntree, bNodeInstanceKey parent_key)
231 /* common base initialization */
232 exec = ntree_exec_begin(context, ntree, parent_key);
234 /* allocate the thread stack listbase array */
235 exec->threadstack = MEM_callocN(BLENDER_MAX_THREADS * sizeof(ListBase), "thread stack array");
237 for (node = exec->nodetree->nodes.first; node; node = node->next)
243 bNodeTreeExec *ntreeTexBeginExecTree(bNodeTree *ntree)
245 bNodeExecContext context;
248 /* XXX hack: prevent exec data from being generated twice.
249 * this should be handled by the renderer!
252 return ntree->execdata;
254 context.previews = ntree->previews;
256 exec = ntreeTexBeginExecTree_internal(&context, ntree, NODE_INSTANCE_KEY_BASE);
258 /* XXX this should not be necessary, but is still used for cmp/sha/tex nodes,
259 * which only store the ntree pointer. Should be fixed at some point!
261 ntree->execdata = exec;
266 /* free texture delegates */
267 static void tex_free_delegates(bNodeTreeExec *exec)
269 bNodeThreadStack *nts;
273 for (th = 0; th < BLENDER_MAX_THREADS; th++)
274 for (nts = exec->threadstack[th].first; nts; nts = nts->next)
275 for (ns = nts->stack, a = 0; a < exec->stacksize; a++, ns++)
276 if (ns->data && !ns->is_copy)
280 void ntreeTexEndExecTree_internal(bNodeTreeExec *exec)
282 bNodeThreadStack *nts;
285 if (exec->threadstack) {
286 tex_free_delegates(exec);
288 for (a = 0; a < BLENDER_MAX_THREADS; a++) {
289 for (nts = exec->threadstack[a].first; nts; nts = nts->next)
290 if (nts->stack) MEM_freeN(nts->stack);
291 BLI_freelistN(&exec->threadstack[a]);
294 MEM_freeN(exec->threadstack);
295 exec->threadstack = NULL;
298 ntree_exec_end(exec);
301 void ntreeTexEndExecTree(bNodeTreeExec *exec)
304 /* exec may get freed, so assign ntree */
305 bNodeTree *ntree = exec->nodetree;
306 ntreeTexEndExecTree_internal(exec);
308 /* XXX clear nodetree backpointer to exec data, same problem as noted in ntreeBeginExecTree */
309 ntree->execdata = NULL;
313 int ntreeTexExecTree(
317 float dxt[3], float dyt[3],
328 float *nor = texres->nor;
329 int retval = TEX_INT;
330 bNodeThreadStack *nts = NULL;
331 bNodeTreeExec *exec = nodes->execdata;
336 data.osatex = osatex;
337 data.target = texres;
338 data.do_preview = preview;
339 data.do_manage = (shi) ? shi->do_manage : true;
340 data.thread = thread;
341 data.which_output = which_output;
346 /* ensure execdata is only initialized once */
348 BLI_lock_thread(LOCK_NODES);
349 if (!nodes->execdata)
350 ntreeTexBeginExecTree(nodes);
351 BLI_unlock_thread(LOCK_NODES);
353 exec = nodes->execdata;
356 nts = ntreeGetThreadStack(exec, thread);
357 ntreeExecThreadNodes(exec, nts, &data, thread);
358 ntreeReleaseThreadStack(nts);
360 if (texres->nor) retval |= TEX_NOR;
362 /* confusing stuff; the texture output node sets this to NULL to indicate no normal socket was set
363 * however, the texture code checks this for other reasons (namely, a normal is required for material) */