Blender  V3.3
node_texture_tree.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2007 Blender Foundation. All rights reserved. */
3 
8 #include <string.h>
9 
10 #include "DNA_node_types.h"
11 #include "DNA_space_types.h"
12 #include "DNA_texture_types.h"
13 
14 #include "BLI_listbase.h"
15 #include "BLI_threads.h"
16 #include "BLI_utildefines.h"
17 
18 #include "BLT_translation.h"
19 
20 #include "BKE_context.h"
21 #include "BKE_linestyle.h"
22 #include "BKE_node.h"
23 #include "BKE_paint.h"
24 
25 #include "NOD_texture.h"
26 #include "node_common.h"
27 #include "node_exec.h"
28 #include "node_texture_util.h"
29 #include "node_util.h"
30 
31 #include "DEG_depsgraph.h"
32 
33 #include "RNA_access.h"
34 #include "RNA_prototypes.h"
35 
36 #include "RE_texture.h"
37 
38 #include "UI_resources.h"
39 
40 static void texture_get_from_context(const bContext *C,
41  bNodeTreeType *UNUSED(treetype),
42  bNodeTree **r_ntree,
43  ID **r_id,
44  ID **r_from)
45 {
46  SpaceNode *snode = CTX_wm_space_node(C);
48  ViewLayer *view_layer = CTX_data_view_layer(C);
49  Object *ob = OBACT(view_layer);
50  Tex *tx = NULL;
51 
52  if (snode->texfrom == SNODE_TEX_BRUSH) {
53  struct Brush *brush = NULL;
54 
55  if (ob && (ob->mode & OB_MODE_SCULPT)) {
57  }
58  else {
60  }
61 
62  if (brush) {
63  *r_from = (ID *)brush;
64  tx = give_current_brush_texture(brush);
65  if (tx) {
66  *r_id = &tx->id;
67  *r_ntree = tx->nodetree;
68  }
69  }
70  }
71  else if (snode->texfrom == SNODE_TEX_LINESTYLE) {
73  if (linestyle) {
74  *r_from = (ID *)linestyle;
76  if (tx) {
77  *r_id = &tx->id;
78  *r_ntree = tx->nodetree;
79  }
80  }
81  }
82 }
83 
84 static void foreach_nodeclass(Scene *UNUSED(scene), void *calldata, bNodeClassCallback func)
85 {
86  func(calldata, NODE_CLASS_INPUT, N_("Input"));
87  func(calldata, NODE_CLASS_OUTPUT, N_("Output"));
88  func(calldata, NODE_CLASS_OP_COLOR, N_("Color"));
89  func(calldata, NODE_CLASS_PATTERN, N_("Patterns"));
90  func(calldata, NODE_CLASS_TEXTURE, N_("Textures"));
91  func(calldata, NODE_CLASS_CONVERTER, N_("Converter"));
92  func(calldata, NODE_CLASS_DISTORT, N_("Distort"));
93  func(calldata, NODE_CLASS_GROUP, N_("Group"));
94  func(calldata, NODE_CLASS_INTERFACE, N_("Interface"));
95  func(calldata, NODE_CLASS_LAYOUT, N_("Layout"));
96 }
97 
98 /* XXX muting disabled in previews because of threading issues with the main execution
99  * it works here, but disabled for consistency
100  */
101 #if 1
102 static void localize(bNodeTree *localtree, bNodeTree *UNUSED(ntree))
103 {
104  bNode *node, *node_next;
105 
106  /* replace muted nodes and reroute nodes by internal links */
107  for (node = localtree->nodes.first; node; node = node_next) {
108  node_next = node->next;
109 
110  if (node->flag & NODE_MUTED || node->type == NODE_REROUTE) {
111  nodeInternalRelink(localtree, node);
112  ntreeFreeLocalNode(localtree, node);
113  }
114  }
115 }
116 #else
117 static void localize(bNodeTree *UNUSED(localtree), bNodeTree *UNUSED(ntree))
118 {
119 }
120 #endif
121 
122 static void update(bNodeTree *ntree)
123 {
125 }
126 
128  bNodeSocketType *socket_type)
129 {
130  return nodeIsStaticSocketType(socket_type) &&
131  ELEM(socket_type->type, SOCK_FLOAT, SOCK_VECTOR, SOCK_RGBA);
132 }
133 
135 
137 {
139  "texture node tree type");
140 
141  tt->type = NTREE_TEXTURE;
142  strcpy(tt->idname, "TextureNodeTree");
143  strcpy(tt->ui_name, N_("Texture Node Editor"));
144  tt->ui_icon = ICON_NODE_TEXTURE; /* Defined in `drawnode.c`. */
145  strcpy(tt->ui_description, N_("Texture nodes"));
146 
148  tt->update = update;
149  tt->localize = localize;
152 
153  tt->rna_ext.srna = &RNA_TextureNodeTree;
154 
155  ntreeTypeAdd(tt);
156 }
157 
158 /**** Material/Texture trees ****/
159 
161 {
162  ListBase *lb = &exec->threadstack[thread];
163  bNodeThreadStack *nts;
164 
165  for (nts = (bNodeThreadStack *)lb->first; nts; nts = nts->next) {
166  if (!nts->used) {
167  nts->used = true;
168  break;
169  }
170  }
171 
172  if (!nts) {
173  nts = MEM_callocN(sizeof(bNodeThreadStack), "bNodeThreadStack");
174  nts->stack = (bNodeStack *)MEM_dupallocN(exec->stack);
175  nts->used = true;
176  BLI_addtail(lb, nts);
177  }
178 
179  return nts;
180 }
181 
183 {
184  nts->used = false;
185 }
186 
187 bool ntreeExecThreadNodes(bNodeTreeExec *exec, bNodeThreadStack *nts, void *callerdata, int thread)
188 {
189  bNodeStack *nsin[MAX_SOCKET] = {NULL}; /* arbitrary... watch this */
190  bNodeStack *nsout[MAX_SOCKET] = {NULL}; /* arbitrary... watch this */
191  bNodeExec *nodeexec;
192  bNode *node;
193  int n;
194 
195  /* nodes are presorted, so exec is in order of list */
196 
197  for (n = 0, nodeexec = exec->nodeexec; n < exec->totnodes; n++, nodeexec++) {
198  node = nodeexec->node;
199  if (node->need_exec) {
200  node_get_stack(node, nts->stack, nsin, nsout);
201  /* Handle muted nodes...
202  * If the mute func is not set, assume the node should never be muted,
203  * and hence execute it!
204  */
205  if (node->typeinfo->exec_fn && !(node->flag & NODE_MUTED)) {
206  node->typeinfo->exec_fn(callerdata, thread, node, &nodeexec->data, nsin, nsout);
207  }
208  }
209  }
210 
211  /* signal to that all went OK, for render */
212  return true;
213 }
214 
216  bNodeTree *ntree,
217  bNodeInstanceKey parent_key)
218 {
220  bNode *node;
221 
222  /* common base initialization */
223  exec = ntree_exec_begin(context, ntree, parent_key);
224 
225  /* allocate the thread stack listbase array */
226  exec->threadstack = MEM_callocN(BLENDER_MAX_THREADS * sizeof(ListBase), "thread stack array");
227 
228  for (node = exec->nodetree->nodes.first; node; node = node->next) {
229  node->need_exec = 1;
230  }
231 
232  return exec;
233 }
234 
236 {
239 
240  /* XXX hack: prevent exec data from being generated twice.
241  * this should be handled by the renderer!
242  */
243  if (ntree->execdata) {
244  return ntree->execdata;
245  }
246 
247  context.previews = ntree->previews;
248 
250 
251  /* XXX this should not be necessary, but is still used for cmp/sha/tex nodes,
252  * which only store the ntree pointer. Should be fixed at some point!
253  */
254  ntree->execdata = exec;
255 
256  return exec;
257 }
258 
259 /* free texture delegates */
261 {
262  bNodeThreadStack *nts;
263  bNodeStack *ns;
264  int th, a;
265 
266  for (th = 0; th < BLENDER_MAX_THREADS; th++) {
267  for (nts = exec->threadstack[th].first; nts; nts = nts->next) {
268  for (ns = nts->stack, a = 0; a < exec->stacksize; a++, ns++) {
269  if (ns->data && !ns->is_copy) {
270  MEM_freeN(ns->data);
271  }
272  }
273  }
274  }
275 }
276 
278 {
279  bNodeThreadStack *nts;
280  int a;
281 
282  if (exec->threadstack) {
284 
285  for (a = 0; a < BLENDER_MAX_THREADS; a++) {
286  for (nts = exec->threadstack[a].first; nts; nts = nts->next) {
287  if (nts->stack) {
288  MEM_freeN(nts->stack);
289  }
290  }
291  BLI_freelistN(&exec->threadstack[a]);
292  }
293 
294  MEM_freeN(exec->threadstack);
295  exec->threadstack = NULL;
296  }
297 
299 }
300 
302 {
303  if (exec) {
304  /* exec may get freed, so assign ntree */
305  bNodeTree *ntree = exec->nodetree;
307 
308  /* XXX clear nodetree backpointer to exec data, same problem as noted in ntreeBeginExecTree */
309  ntree->execdata = NULL;
310  }
311 }
312 
314  TexResult *target,
315  const float co[3],
316  float dxt[3],
317  float dyt[3],
318  int osatex,
319  const short thread,
320  const Tex *UNUSED(tex),
321  short which_output,
322  int cfra,
323  int preview,
324  MTex *mtex)
325 {
327  int retval = TEX_INT;
328  bNodeThreadStack *nts = NULL;
330 
331  data.co = co;
332  data.dxt = dxt;
333  data.dyt = dyt;
334  data.osatex = osatex;
335  data.target = target;
336  data.do_preview = preview;
337  data.do_manage = true;
338  data.thread = thread;
339  data.which_output = which_output;
340  data.cfra = cfra;
341  data.mtex = mtex;
342 
343  /* ensure execdata is only initialized once */
344  if (!exec) {
346  if (!ntree->execdata) {
348  }
350 
351  exec = ntree->execdata;
352  }
353 
357 
358  retval |= TEX_RGB;
359 
360  return retval;
361 }
struct Scene * CTX_data_scene(const bContext *C)
Definition: context.c:1090
struct SpaceNode * CTX_wm_space_node(const bContext *C)
Definition: context.c:878
struct ViewLayer * CTX_data_view_layer(const bContext *C)
Definition: context.c:1100
Blender kernel freestyle line style functionality.
FreestyleLineStyle * BKE_linestyle_active_from_view_layer(struct ViewLayer *view_layer)
Definition: linestyle.c:806
void ntreeTypeAdd(struct bNodeTreeType *nt)
Definition: node.cc:1292
#define NODE_CLASS_OUTPUT
Definition: BKE_node.h:346
#define NODE_REROUTE
Definition: BKE_node.h:986
#define NODE_CLASS_INTERFACE
Definition: BKE_node.h:357
void nodeInternalRelink(struct bNodeTree *ntree, struct bNode *node)
Definition: node.cc:2490
const bNodeInstanceKey NODE_INSTANCE_KEY_BASE
Definition: node.cc:3896
#define NODE_CLASS_CONVERTER
Definition: BKE_node.h:351
void ntreeFreeLocalNode(struct bNodeTree *ntree, struct bNode *node)
Definition: node.cc:2998
void(* bNodeClassCallback)(void *calldata, int nclass, const char *name)
Definition: BKE_node.h:372
#define NODE_CLASS_PATTERN
Definition: BKE_node.h:354
#define NODE_CLASS_DISTORT
Definition: BKE_node.h:353
bool nodeIsStaticSocketType(const struct bNodeSocketType *stype)
Definition: node.cc:1701
#define NODE_CLASS_LAYOUT
Definition: BKE_node.h:361
#define MAX_SOCKET
Definition: BKE_node.h:30
#define NODE_CLASS_OP_COLOR
Definition: BKE_node.h:347
#define NODE_CLASS_INPUT
Definition: BKE_node.h:345
#define NODE_CLASS_GROUP
Definition: BKE_node.h:350
#define NODE_CLASS_TEXTURE
Definition: BKE_node.h:355
struct Brush * BKE_paint_brush(struct Paint *paint)
Definition: paint.c:607
struct Tex * give_current_brush_texture(struct Brush *br)
Definition: texture.c:530
struct Tex * give_current_linestyle_texture(struct FreestyleLineStyle *linestyle)
Definition: texture.c:446
void void BLI_freelistN(struct ListBase *listbase) ATTR_NONNULL(1)
Definition: listbase.c:466
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:80
void BLI_thread_unlock(int type)
Definition: threads.cc:361
@ LOCK_NODES
Definition: BLI_threads.h:70
void BLI_thread_lock(int type)
Definition: threads.cc:356
#define BLENDER_MAX_THREADS
Definition: BLI_threads.h:19
#define UNUSED(x)
#define ELEM(...)
#define NTREE_TEXTURE
#define NODE_MUTED
@ SOCK_VECTOR
@ SOCK_FLOAT
@ SOCK_RGBA
@ OB_MODE_SCULPT
#define OBACT(_view_layer)
@ SNODE_TEX_BRUSH
@ SNODE_TEX_LINESTYLE
#define TEX_INT
#define TEX_RGB
#define C
Definition: RandGen.cpp:25
Definition: thread.h:34
OperationNode * node
Scene scene
FreestyleLineStyle linestyle
bNodeTree * ntree
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
void *(* MEM_dupallocN)(const void *vmemh)
Definition: mallocn.c:28
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:31
static unsigned a[3]
Definition: RandGen.cpp:78
static const pxr::TfToken preview("preview", pxr::TfToken::Immortal)
void ntree_update_reroute_nodes(bNodeTree *ntree)
Definition: node_common.cc:321
bNodeTreeExec * ntree_exec_begin(bNodeExecContext *context, bNodeTree *ntree, bNodeInstanceKey parent_key)
Definition: node_exec.cc:139
void node_get_stack(bNode *node, bNodeStack *stack, bNodeStack **in, bNodeStack **out)
Definition: node_exec.cc:36
void ntree_exec_end(bNodeTreeExec *exec)
Definition: node_exec.cc:242
static void exec(void *data, int UNUSED(thread), bNode *node, bNodeExecData *execdata, bNodeStack **in, bNodeStack **out)
static void foreach_nodeclass(Scene *UNUSED(scene), void *calldata, bNodeClassCallback func)
bNodeThreadStack * ntreeGetThreadStack(bNodeTreeExec *exec, int thread)
bNodeTreeType * ntreeType_Texture
bool ntreeExecThreadNodes(bNodeTreeExec *exec, bNodeThreadStack *nts, void *callerdata, int thread)
void ntreeTexEndExecTree_internal(bNodeTreeExec *exec)
static void texture_get_from_context(const bContext *C, bNodeTreeType *UNUSED(treetype), bNodeTree **r_ntree, ID **r_id, ID **r_from)
static void localize(bNodeTree *localtree, bNodeTree *UNUSED(ntree))
bNodeTreeExec * ntreeTexBeginExecTree_internal(bNodeExecContext *context, bNodeTree *ntree, bNodeInstanceKey parent_key)
static void tex_free_delegates(bNodeTreeExec *exec)
static bool texture_node_tree_socket_type_valid(bNodeTreeType *UNUSED(ntreetype), bNodeSocketType *socket_type)
static void update(bNodeTree *ntree)
void ntreeTexEndExecTree(bNodeTreeExec *exec)
bNodeTreeExec * ntreeTexBeginExecTree(bNodeTree *ntree)
int ntreeTexExecTree(bNodeTree *ntree, TexResult *target, const float co[3], float dxt[3], float dyt[3], int osatex, const short thread, const Tex *UNUSED(tex), short which_output, int cfra, int preview, MTex *mtex)
void register_node_tree_type_tex(void)
void ntreeReleaseThreadStack(bNodeThreadStack *nts)
struct MTex mtex
StructRNA * srna
Definition: RNA_types.h:766
Definition: DNA_ID.h:368
void * first
Definition: DNA_listBase.h:31
struct ToolSettings * toolsettings
Paint paint
struct bNodeTree * nodetree
struct ImagePaintSettings imapaint
bNodeExecData data
Definition: node_exec.h:32
struct bNode * node
Definition: node_exec.h:31
Defines a socket type.
Definition: BKE_node.h:143
struct bNodeThreadStack * next
Definition: node_exec.h:53
struct bNodeStack * stack
Definition: node_exec.h:54
char idname[64]
Definition: BKE_node.h:375
void(* update)(struct bNodeTree *ntree)
Definition: BKE_node.h:400
void(* get_from_context)(const struct bContext *C, struct bNodeTreeType *ntreetype, struct bNodeTree **r_ntree, struct ID **r_id, struct ID **r_from)
Definition: BKE_node.h:389
char ui_name[64]
Definition: BKE_node.h:377
void(* localize)(struct bNodeTree *localtree, struct bNodeTree *ntree)
Definition: BKE_node.h:396
char ui_description[256]
Definition: BKE_node.h:378
bool(* valid_socket_type)(struct bNodeTreeType *ntreetype, struct bNodeSocketType *socket_type)
Definition: BKE_node.h:407
void(* foreach_nodeclass)(struct Scene *scene, void *calldata, bNodeClassCallback func)
Definition: BKE_node.h:385
ExtensionRNA rna_ext
Definition: BKE_node.h:410
struct bNodeInstanceHash * previews
ListBase nodes
struct bNodeTreeExec * execdata
#define N_(msgid)