Blender V5.0
node_texture_tree.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2007 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstring>
10
11#include "DNA_node_types.h"
12#include "DNA_space_types.h"
13#include "DNA_texture_types.h"
14
15#include "BLI_listbase.h"
16#include "BLI_threads.h"
17#include "BLI_utildefines.h"
18
19#include "BKE_context.hh"
20#include "BKE_layer.hh"
21#include "BKE_linestyle.h"
22#include "BKE_node.hh"
23#include "BKE_node_runtime.hh"
24#include "BKE_paint.hh"
25#include "BKE_texture.h"
26
27#include "NOD_texture.h"
28#include "node_common.h"
29#include "node_exec.hh"
30#include "node_texture_util.hh"
31#include "node_util.hh"
32
33#include "RNA_prototypes.hh"
34
35#include "UI_resources.hh"
36
38 blender::bke::bNodeTreeType * /*treetype*/,
39 bNodeTree **r_ntree,
40 ID **r_id,
41 ID **r_from)
42{
44 Scene *scene = CTX_data_scene(C);
45 ViewLayer *view_layer = CTX_data_view_layer(C);
46 BKE_view_layer_synced_ensure(scene, view_layer);
48 Tex *tx = nullptr;
49
50 if (snode->texfrom == SNODE_TEX_BRUSH) {
51 Brush *brush = nullptr;
52
53 if (ob && (ob->mode & OB_MODE_SCULPT)) {
54 brush = BKE_paint_brush(&scene->toolsettings->sculpt->paint);
55 }
56 else {
58 }
59
60 if (brush) {
61 *r_from = (ID *)brush;
63 if (tx) {
64 *r_id = &tx->id;
65 *r_ntree = tx->nodetree;
66 }
67 }
68 }
69 else if (snode->texfrom == SNODE_TEX_LINESTYLE) {
71 if (linestyle) {
72 *r_from = (ID *)linestyle;
73 tx = give_current_linestyle_texture(linestyle);
74 if (tx) {
75 *r_id = &tx->id;
76 *r_ntree = tx->nodetree;
77 }
78 }
79 }
80}
81
82static void foreach_nodeclass(void *calldata, blender::bke::bNodeClassCallback func)
83{
84 func(calldata, NODE_CLASS_INPUT, N_("Input"));
85 func(calldata, NODE_CLASS_OUTPUT, N_("Output"));
86 func(calldata, NODE_CLASS_OP_COLOR, N_("Color"));
87 func(calldata, NODE_CLASS_PATTERN, N_("Patterns"));
88 func(calldata, NODE_CLASS_TEXTURE, N_("Textures"));
89 func(calldata, NODE_CLASS_CONVERTER, N_("Converter"));
90 func(calldata, NODE_CLASS_DISTORT, N_("Distort"));
91 func(calldata, NODE_CLASS_GROUP, N_("Group"));
92 func(calldata, NODE_CLASS_INTERFACE, N_("Interface"));
93 func(calldata, NODE_CLASS_LAYOUT, N_("Layout"));
94}
95
96/* XXX muting disabled in previews because of threading issues with the main execution
97 * it works here, but disabled for consistency
98 */
99#if 1
100static void localize(bNodeTree *localtree, bNodeTree * /*ntree*/)
101{
102 bNode *node, *node_next;
103
104 /* replace muted nodes and reroute nodes by internal links */
105 for (node = static_cast<bNode *>(localtree->nodes.first); node; node = node_next) {
106 node_next = node->next;
107
108 if (node->is_muted() || node->is_reroute()) {
109 blender::bke::node_internal_relink(*localtree, *node);
111 }
112 }
113}
114#else
115static void localize(bNodeTree * /*localtree*/, bNodeTree * /*ntree*/) {}
116#endif
117
118static void update(bNodeTree *ntree)
119{
121}
122
125{
126 return blender::bke::node_is_static_socket_type(*socket_type) &&
127 ELEM(socket_type->type, SOCK_FLOAT, SOCK_VECTOR, SOCK_RGBA);
128}
129
131
133{
134 blender::bke::bNodeTreeType *tt = ntreeType_Texture = MEM_new<blender::bke::bNodeTreeType>(
135 __func__);
136
137 tt->type = NTREE_TEXTURE;
138 tt->idname = "TextureNodeTree";
139 tt->group_idname = "TextureNodeGroup";
140 tt->ui_name = N_("Texture Node Editor");
141 tt->ui_icon = ICON_NODE_TEXTURE; /* Defined in `drawnode.cc`. */
142 tt->ui_description = N_("Edit textures using nodes");
143
145 tt->update = update;
146 tt->localize = localize;
149
150 tt->rna_ext.srna = &RNA_TextureNodeTree;
151
153}
154
155/**** Material/Texture trees ****/
156
158{
159 ListBase *lb = &exec->threadstack[thread];
160 bNodeThreadStack *nts;
161
162 for (nts = (bNodeThreadStack *)lb->first; nts; nts = nts->next) {
163 if (!nts->used) {
164 nts->used = true;
165 break;
166 }
167 }
168
169 if (!nts) {
170 nts = MEM_callocN<bNodeThreadStack>("bNodeThreadStack");
171 nts->stack = (bNodeStack *)MEM_dupallocN(exec->stack);
172 nts->used = true;
173 BLI_addtail(lb, nts);
174 }
175
176 return nts;
177}
178
180{
181 nts->used = false;
182}
183
185{
186 bNodeStack *nsin[MAX_SOCKET] = {nullptr}; /* arbitrary... watch this */
187 bNodeStack *nsout[MAX_SOCKET] = {nullptr}; /* arbitrary... watch this */
188 bNodeExec *nodeexec;
189 bNode *node;
190 int n;
191
192 /* nodes are presorted, so exec is in order of list */
193
194 for (n = 0, nodeexec = exec->nodeexec; n < exec->totnodes; n++, nodeexec++) {
195 node = nodeexec->node;
196 if (node->runtime->need_exec) {
197 node_get_stack(node, nts->stack, nsin, nsout);
198 /* Handle muted nodes...
199 * If the mute func is not set, assume the node should never be muted,
200 * and hence execute it!
201 */
202 if (node->typeinfo->exec_fn && !node->is_muted()) {
203 node->typeinfo->exec_fn(callerdata, thread, node, &nodeexec->data, nsin, nsout);
204 }
205 }
206 }
207
208 /* signal to that all went OK, for render */
209 return true;
210}
211
213 bNodeTree *ntree,
214 bNodeInstanceKey parent_key)
215{
217
218 /* common base initialization */
219 exec = ntree_exec_begin(context, ntree, parent_key);
220
221 /* allocate the thread stack listbase array */
222 exec->threadstack = MEM_calloc_arrayN<ListBase>(BLENDER_MAX_THREADS, "thread stack array");
223
224 LISTBASE_FOREACH (bNode *, node, &exec->nodetree->nodes) {
225 node->runtime->need_exec = 1;
226 }
227
228 return exec;
229}
230
232{
233 bNodeExecContext context;
235
236 /* XXX hack: prevent exec data from being generated twice.
237 * this should be handled by the renderer!
238 */
239 if (ntree->runtime->execdata) {
240 return ntree->runtime->execdata;
241 }
242
244
245 /* XXX this should not be necessary, but is still used for compositor/shading/texture nodes,
246 * which only store the ntree pointer. Should be fixed at some point!
247 */
248 ntree->runtime->execdata = exec;
249
250 return exec;
251}
252
253/* free texture delegates */
255{
256 bNodeStack *ns;
257 int th, a;
258
259 for (th = 0; th < BLENDER_MAX_THREADS; th++) {
260 LISTBASE_FOREACH (bNodeThreadStack *, nts, &exec->threadstack[th]) {
261 for (ns = nts->stack, a = 0; a < exec->stacksize; a++, ns++) {
262 if (ns->data && !ns->is_copy) {
263 MEM_freeN(ns->data);
264 }
265 }
266 }
267 }
268}
269
271{
272 int a;
273
274 if (exec->threadstack) {
276
277 for (a = 0; a < BLENDER_MAX_THREADS; a++) {
278 LISTBASE_FOREACH (bNodeThreadStack *, nts, &exec->threadstack[a]) {
279 if (nts->stack) {
280 MEM_freeN(nts->stack);
281 }
282 }
283 BLI_freelistN(&exec->threadstack[a]);
284 }
285
286 MEM_freeN(exec->threadstack);
287 exec->threadstack = nullptr;
288 }
289
291}
292
294{
295 if (exec) {
296 /* exec may get freed, so assign ntree */
297 bNodeTree *ntree = exec->nodetree;
299
300 /* XXX: clear node-tree back-pointer to exec data,
301 * same problem as noted in #ntreeBeginExecTree. */
302 ntree->runtime->execdata = nullptr;
303 }
304}
305
307 TexResult *target,
308 const float co[3],
309 const short thread,
310 const Tex * /*tex*/,
311 short which_output,
312 int cfra,
313 int preview,
314 MTex *mtex)
315{
317 int retval = TEX_INT;
318 bNodeThreadStack *nts = nullptr;
319 bNodeTreeExec *exec = ntree->runtime->execdata;
320
321 data.co = co;
322 data.target = target;
323 data.do_preview = preview;
324 data.do_manage = true;
325 data.thread = thread;
326 data.which_output = which_output;
327 data.cfra = cfra;
328 data.mtex = mtex;
329
330 /* ensure execdata is only initialized once */
331 if (!exec) {
333 if (!ntree->runtime->execdata) {
335 }
337
338 exec = ntree->runtime->execdata;
339 }
340
344
345 retval |= TEX_RGB;
346
347 return retval;
348}
SpaceNode * CTX_wm_space_node(const bContext *C)
Scene * CTX_data_scene(const bContext *C)
ViewLayer * CTX_data_view_layer(const bContext *C)
void BKE_view_layer_synced_ensure(const Scene *scene, ViewLayer *view_layer)
Object * BKE_view_layer_active_object_get(const ViewLayer *view_layer)
Blender kernel freestyle line style functionality.
FreestyleLineStyle * BKE_linestyle_active_from_view_layer(struct ViewLayer *view_layer)
Definition linestyle.cc:714
#define NODE_CLASS_OUTPUT
Definition BKE_node.hh:448
#define NODE_CLASS_INTERFACE
Definition BKE_node.hh:459
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:453
#define NODE_CLASS_PATTERN
Definition BKE_node.hh:456
#define NODE_CLASS_DISTORT
Definition BKE_node.hh:455
#define NODE_CLASS_LAYOUT
Definition BKE_node.hh:463
#define MAX_SOCKET
Definition BKE_node.hh:28
#define NODE_CLASS_OP_COLOR
Definition BKE_node.hh:449
#define NODE_CLASS_INPUT
Definition BKE_node.hh:447
#define NODE_CLASS_GROUP
Definition BKE_node.hh:452
#define NODE_CLASS_TEXTURE
Definition BKE_node.hh:457
Brush * BKE_paint_brush(Paint *paint)
Definition paint.cc:645
struct Tex * give_current_linestyle_texture(struct FreestyleLineStyle *linestyle)
Definition texture.cc:443
struct Tex * give_current_brush_texture(struct Brush *br)
Definition texture.cc:527
#define LISTBASE_FOREACH(type, var, list)
void void BLI_freelistN(ListBase *listbase) ATTR_NONNULL(1)
Definition listbase.cc:497
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:111
void BLI_thread_unlock(int type)
Definition threads.cc:333
void BLI_thread_lock(int type)
Definition threads.cc:328
#define BLENDER_MAX_THREADS
Definition BLI_threads.h:16
@ LOCK_NODES
Definition BLI_threads.h:67
#define ELEM(...)
@ NTREE_TEXTURE
@ SOCK_VECTOR
@ SOCK_FLOAT
@ SOCK_RGBA
@ OB_MODE_SCULPT
@ SNODE_TEX_BRUSH
@ SNODE_TEX_LINESTYLE
@ TEX_INT
@ TEX_RGB
struct blender::bke::bNodeTreeType * ntreeType_Texture
#define C
Definition RandGen.cpp:29
BMesh const char void * data
void * MEM_calloc_arrayN(size_t len, size_t size, const char *str)
Definition mallocn.cc:123
void * MEM_callocN(size_t len, const char *str)
Definition mallocn.cc:118
void * MEM_dupallocN(const void *vmemh)
Definition mallocn.cc:143
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
void node_tree_free_local_node(bNodeTree &ntree, bNode &node)
Definition node.cc:4372
void node_tree_type_add(bNodeTreeType &nt)
Definition node.cc:2340
void node_internal_relink(bNodeTree &ntree, bNode &node)
Definition node.cc:3908
const bNodeInstanceKey NODE_INSTANCE_KEY_BASE
Definition node.cc:4846
void(*)(void *calldata, int nclass, StringRefNull name) bNodeClassCallback
Definition BKE_node.hh:492
bool node_is_static_socket_type(const bNodeSocketType &stype)
Definition node.cc:2826
void ntree_update_reroute_nodes(bNodeTree *ntree)
static void localize(bNodeTree *localtree, bNodeTree *ntree)
static void update(bNodeTree *ntree)
static void foreach_nodeclass(void *calldata, blender::bke::bNodeClassCallback func)
bNodeTreeExec * ntree_exec_begin(bNodeExecContext *context, bNodeTree *ntree, bNodeInstanceKey parent_key)
Definition node_exec.cc:183
void node_get_stack(bNode *node, bNodeStack *stack, bNodeStack **in, bNodeStack **out)
Definition node_exec.cc:38
void ntree_exec_end(bNodeTreeExec *exec)
Definition node_exec.cc:277
static void exec(void *data, int, bNode *node, bNodeExecData *execdata, bNodeStack **in, bNodeStack **out)
void register_node_tree_type_tex()
bNodeThreadStack * ntreeGetThreadStack(bNodeTreeExec *exec, int thread)
bool ntreeExecThreadNodes(bNodeTreeExec *exec, bNodeThreadStack *nts, void *callerdata, int thread)
bNodeTreeExec * ntreeTexBeginExecTree(bNodeTree *ntree)
bNodeTreeExec * ntreeTexBeginExecTree_internal(bNodeExecContext *context, bNodeTree *ntree, bNodeInstanceKey parent_key)
void ntreeTexEndExecTree_internal(bNodeTreeExec *exec)
static void tex_free_delegates(bNodeTreeExec *exec)
static void update(bNodeTree *ntree)
static bool texture_node_tree_socket_type_valid(blender::bke::bNodeTreeType *, blender::bke::bNodeSocketType *socket_type)
void ntreeTexEndExecTree(bNodeTreeExec *exec)
static void texture_get_from_context(const bContext *C, blender::bke::bNodeTreeType *, bNodeTree **r_ntree, ID **r_id, ID **r_from)
int ntreeTexExecTree(bNodeTree *ntree, TexResult *target, const float co[3], const short thread, const Tex *, short which_output, int cfra, int preview, MTex *mtex)
void ntreeReleaseThreadStack(bNodeThreadStack *nts)
static void localize(bNodeTree *localtree, bNodeTree *)
static void foreach_nodeclass(void *calldata, blender::bke::bNodeClassCallback func)
StructRNA * srna
Definition DNA_ID.h:414
void * first
struct ToolSettings * toolsettings
struct bNodeTree * nodetree
struct ImagePaintSettings imapaint
bNodeExecData data
Definition node_exec.hh:25
bNode * node
Definition node_exec.hh:24
bNodeStack * stack
Definition node_exec.hh:47
bNodeThreadStack * next
Definition node_exec.hh:46
bNodeTreeRuntimeHandle * runtime
ListBase nodes
bNodeTypeHandle * typeinfo
struct bNode * next
bNodeRuntimeHandle * runtime
Defines a socket type.
Definition BKE_node.hh:158
eNodeSocketDatatype type
Definition BKE_node.hh:193
void(* update)(bNodeTree *ntree)
Definition BKE_node.hh:522
void(* foreach_nodeclass)(void *calldata, bNodeClassCallback func)
Definition BKE_node.hh:507
void(* get_from_context)(const bContext *C, bNodeTreeType *ntreetype, bNodeTree **r_ntree, ID **r_id, ID **r_from)
Definition BKE_node.hh:511
void(* localize)(bNodeTree *localtree, bNodeTree *ntree)
Definition BKE_node.hh:518
bool(* valid_socket_type)(bNodeTreeType *ntreetype, bNodeSocketType *socket_type)
Definition BKE_node.hh:529
#define N_(msgid)