Blender  V2.93
node_texture_tree.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2007 Blender Foundation.
17  * All rights reserved.
18  */
19 
24 #include <string.h>
25 
26 #include "DNA_node_types.h"
27 #include "DNA_space_types.h"
28 #include "DNA_texture_types.h"
29 
30 #include "BLI_listbase.h"
31 #include "BLI_threads.h"
32 #include "BLI_utildefines.h"
33 
34 #include "BLT_translation.h"
35 
36 #include "BKE_context.h"
37 #include "BKE_linestyle.h"
38 #include "BKE_node.h"
39 #include "BKE_paint.h"
40 
41 #include "NOD_texture.h"
42 #include "node_common.h"
43 #include "node_exec.h"
44 #include "node_texture_util.h"
45 #include "node_util.h"
46 
47 #include "DEG_depsgraph.h"
48 
49 #include "RNA_access.h"
50 
51 #include "RE_texture.h"
52 
53 static void texture_get_from_context(const bContext *C,
54  bNodeTreeType *UNUSED(treetype),
55  bNodeTree **r_ntree,
56  ID **r_id,
57  ID **r_from)
58 {
59  SpaceNode *snode = CTX_wm_space_node(C);
61  ViewLayer *view_layer = CTX_data_view_layer(C);
62  Object *ob = OBACT(view_layer);
63  Tex *tx = NULL;
64 
65  if (snode->texfrom == SNODE_TEX_BRUSH) {
66  struct Brush *brush = NULL;
67 
68  if (ob && (ob->mode & OB_MODE_SCULPT)) {
70  }
71  else {
73  }
74 
75  if (brush) {
76  *r_from = (ID *)brush;
77  tx = give_current_brush_texture(brush);
78  if (tx) {
79  *r_id = &tx->id;
80  *r_ntree = tx->nodetree;
81  }
82  }
83  }
84  else if (snode->texfrom == SNODE_TEX_LINESTYLE) {
86  if (linestyle) {
87  *r_from = (ID *)linestyle;
89  if (tx) {
90  *r_id = &tx->id;
91  *r_ntree = tx->nodetree;
92  }
93  }
94  }
95 }
96 
97 static void foreach_nodeclass(Scene *UNUSED(scene), void *calldata, bNodeClassCallback func)
98 {
99  func(calldata, NODE_CLASS_INPUT, N_("Input"));
100  func(calldata, NODE_CLASS_OUTPUT, N_("Output"));
101  func(calldata, NODE_CLASS_OP_COLOR, N_("Color"));
102  func(calldata, NODE_CLASS_PATTERN, N_("Patterns"));
103  func(calldata, NODE_CLASS_TEXTURE, N_("Textures"));
104  func(calldata, NODE_CLASS_CONVERTOR, N_("Convertor"));
105  func(calldata, NODE_CLASS_DISTORT, N_("Distort"));
106  func(calldata, NODE_CLASS_GROUP, N_("Group"));
107  func(calldata, NODE_CLASS_INTERFACE, N_("Interface"));
108  func(calldata, NODE_CLASS_LAYOUT, N_("Layout"));
109 }
110 
111 /* XXX muting disabled in previews because of threading issues with the main execution
112  * it works here, but disabled for consistency
113  */
114 #if 1
115 static void localize(bNodeTree *localtree, bNodeTree *UNUSED(ntree))
116 {
117  bNode *node, *node_next;
118 
119  /* replace muted nodes and reroute nodes by internal links */
120  for (node = localtree->nodes.first; node; node = node_next) {
121  node_next = node->next;
122 
123  if (node->flag & NODE_MUTED || node->type == NODE_REROUTE) {
124  nodeInternalRelink(localtree, node);
125  ntreeFreeLocalNode(localtree, node);
126  }
127  }
128 }
129 #else
130 static void localize(bNodeTree *UNUSED(localtree), bNodeTree *UNUSED(ntree))
131 {
132 }
133 #endif
134 
135 static void local_sync(bNodeTree *localtree, bNodeTree *ntree)
136 {
137  BKE_node_preview_sync_tree(ntree, localtree);
138 }
139 
140 static void local_merge(Main *UNUSED(bmain), bNodeTree *localtree, bNodeTree *ntree)
141 {
142  BKE_node_preview_merge_tree(ntree, localtree, true);
143 }
144 
145 static void update(bNodeTree *ntree)
146 {
148 
150  /* clean up preview cache, in case nodes have been removed */
152  }
153 }
154 
156 
158 {
160  "texture node tree type");
161 
162  tt->type = NTREE_TEXTURE;
163  strcpy(tt->idname, "TextureNodeTree");
164  strcpy(tt->ui_name, N_("Texture Node Editor"));
165  tt->ui_icon = 0; /* defined in drawnode.c */
166  strcpy(tt->ui_description, N_("Texture nodes"));
167 
169  tt->update = update;
170  tt->localize = localize;
171  tt->local_sync = local_sync;
172  tt->local_merge = local_merge;
174 
176 
177  ntreeTypeAdd(tt);
178 }
179 
181  bNodeTree *ntree,
182  bNodeInstanceKey parent_key)
183 {
185  bNode *node;
186 
187  /* common base initialization */
188  exec = ntree_exec_begin(context, ntree, parent_key);
189 
190  /* allocate the thread stack listbase array */
191  exec->threadstack = MEM_callocN(BLENDER_MAX_THREADS * sizeof(ListBase), "thread stack array");
192 
193  for (node = exec->nodetree->nodes.first; node; node = node->next) {
194  node->need_exec = 1;
195  }
196 
197  return exec;
198 }
199 
201 {
204 
205  /* XXX hack: prevent exec data from being generated twice.
206  * this should be handled by the renderer!
207  */
208  if (ntree->execdata) {
209  return ntree->execdata;
210  }
211 
212  context.previews = ntree->previews;
213 
215 
216  /* XXX this should not be necessary, but is still used for cmp/sha/tex nodes,
217  * which only store the ntree pointer. Should be fixed at some point!
218  */
219  ntree->execdata = exec;
220 
221  return exec;
222 }
223 
224 /* free texture delegates */
226 {
227  bNodeThreadStack *nts;
228  bNodeStack *ns;
229  int th, a;
230 
231  for (th = 0; th < BLENDER_MAX_THREADS; th++) {
232  for (nts = exec->threadstack[th].first; nts; nts = nts->next) {
233  for (ns = nts->stack, a = 0; a < exec->stacksize; a++, ns++) {
234  if (ns->data && !ns->is_copy) {
235  MEM_freeN(ns->data);
236  }
237  }
238  }
239  }
240 }
241 
243 {
244  bNodeThreadStack *nts;
245  int a;
246 
247  if (exec->threadstack) {
249 
250  for (a = 0; a < BLENDER_MAX_THREADS; a++) {
251  for (nts = exec->threadstack[a].first; nts; nts = nts->next) {
252  if (nts->stack) {
253  MEM_freeN(nts->stack);
254  }
255  }
256  BLI_freelistN(&exec->threadstack[a]);
257  }
258 
259  MEM_freeN(exec->threadstack);
260  exec->threadstack = NULL;
261  }
262 
264 }
265 
267 {
268  if (exec) {
269  /* exec may get freed, so assign ntree */
270  bNodeTree *ntree = exec->nodetree;
272 
273  /* XXX clear nodetree backpointer to exec data, same problem as noted in ntreeBeginExecTree */
274  ntree->execdata = NULL;
275  }
276 }
277 
279  TexResult *target,
280  const float co[3],
281  float dxt[3],
282  float dyt[3],
283  int osatex,
284  const short thread,
285  const Tex *UNUSED(tex),
286  short which_output,
287  int cfra,
288  int preview,
289  MTex *mtex)
290 {
292  float *nor = target->nor;
293  int retval = TEX_INT;
294  bNodeThreadStack *nts = NULL;
296 
297  data.co = co;
298  data.dxt = dxt;
299  data.dyt = dyt;
300  data.osatex = osatex;
301  data.target = target;
302  data.do_preview = preview;
303  data.do_manage = true;
304  data.thread = thread;
305  data.which_output = which_output;
306  data.cfra = cfra;
307  data.mtex = mtex;
308 
309  /* ensure execdata is only initialized once */
310  if (!exec) {
312  if (!ntree->execdata) {
314  }
316 
317  exec = ntree->execdata;
318  }
319 
323 
324  if (target->nor) {
325  retval |= TEX_NOR;
326  }
327  retval |= TEX_RGB;
328  /* confusing stuff; the texture output node sets this to NULL to indicate no normal socket was
329  * set however, the texture code checks this for other reasons
330  * (namely, a normal is required for material). */
331  target->nor = nor;
332 
333  return retval;
334 }
struct Scene * CTX_data_scene(const bContext *C)
Definition: context.c:1034
struct SpaceNode * CTX_wm_space_node(const bContext *C)
Definition: context.c:854
struct ViewLayer * CTX_data_view_layer(const bContext *C)
Definition: context.c:1044
Blender kernel freestyle line style functionality.
FreestyleLineStyle * BKE_linestyle_active_from_view_layer(struct ViewLayer *view_layer)
Definition: linestyle.c:818
void ntreeTypeAdd(struct bNodeTreeType *nt)
Definition: node.cc:1219
#define NODE_CLASS_OUTPUT
Definition: BKE_node.h:335
#define NODE_REROUTE
Definition: BKE_node.h:873
#define NODE_CLASS_INTERFACE
Definition: BKE_node.h:357
void BKE_node_preview_sync_tree(struct bNodeTree *to_ntree, struct bNodeTree *from_ntree)
Definition: node.cc:2749
void nodeInternalRelink(struct bNodeTree *ntree, struct bNode *node)
Definition: node.cc:2364
const bNodeInstanceKey NODE_INSTANCE_KEY_BASE
Definition: node.cc:3908
void ntreeFreeLocalNode(struct bNodeTree *ntree, struct bNode *node)
Definition: node.cc:2925
void BKE_node_preview_remove_unused(struct bNodeTree *ntree)
Definition: node.cc:2690
void(* bNodeClassCallback)(void *calldata, int nclass, const char *name)
Definition: BKE_node.h:378
#define NODE_CLASS_PATTERN
Definition: BKE_node.h:345
#define NODE_CLASS_CONVERTOR
Definition: BKE_node.h:341
#define NODE_CLASS_DISTORT
Definition: BKE_node.h:343
void BKE_node_preview_merge_tree(struct bNodeTree *to_ntree, struct bNodeTree *from_ntree, bool remove_old)
Definition: node.cc:2770
#define NODE_CLASS_LAYOUT
Definition: BKE_node.h:361
#define NODE_CLASS_OP_COLOR
Definition: BKE_node.h:336
#define NODE_CLASS_INPUT
Definition: BKE_node.h:334
#define NODE_CLASS_GROUP
Definition: BKE_node.h:339
#define NODE_CLASS_TEXTURE
Definition: BKE_node.h:346
struct Brush * BKE_paint_brush(struct Paint *paint)
Definition: paint.c:604
struct Tex * give_current_brush_texture(struct Brush *br)
Definition: texture.c:546
struct Tex * give_current_linestyle_texture(struct FreestyleLineStyle *linestyle)
Definition: texture.c:461
void void BLI_freelistN(struct ListBase *listbase) ATTR_NONNULL(1)
Definition: listbase.c:547
void BLI_thread_unlock(int type)
Definition: threads.cc:389
void BLI_thread_lock(int type)
Definition: threads.cc:384
@ LOCK_NODES
Definition: BLI_threads.h:71
#define BLENDER_MAX_THREADS
Definition: BLI_threads.h:35
#define UNUSED(x)
#define N_(msgid)
#define NTREE_TEXTURE
#define NODE_MUTED
@ NTREE_UPDATE_NODES
@ OB_MODE_SCULPT
#define OBACT(_view_layer)
@ SNODE_TEX_BRUSH
@ SNODE_TEX_LINESTYLE
#define TEX_NOR
#define TEX_INT
#define TEX_RGB
StructRNA RNA_TextureNodeTree
#define C
Definition: RandGen.cpp:39
OperationNode * node
Scene scene
FreestyleLineStyle linestyle
bNodeTree * ntree
uint nor
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
static unsigned a[3]
Definition: RandGen.cpp:92
void ntree_update_reroute_nodes(bNodeTree *ntree)
Definition: node_common.c:357
bNodeTreeExec * ntree_exec_begin(bNodeExecContext *context, bNodeTree *ntree, bNodeInstanceKey parent_key)
Definition: node_exec.cc:156
bNodeThreadStack * ntreeGetThreadStack(bNodeTreeExec *exec, int thread)
Definition: node_exec.cc:283
bool ntreeExecThreadNodes(bNodeTreeExec *exec, bNodeThreadStack *nts, void *callerdata, int thread)
Definition: node_exec.cc:310
void ntree_exec_end(bNodeTreeExec *exec)
Definition: node_exec.cc:259
void ntreeReleaseThreadStack(bNodeThreadStack *nts)
Definition: node_exec.cc:305
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)
bNodeTreeType * ntreeType_Texture
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 local_sync(bNodeTree *localtree, bNodeTree *ntree)
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 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)
static void local_merge(Main *UNUSED(bmain), bNodeTree *localtree, bNodeTree *ntree)
struct SELECTID_Context context
Definition: select_engine.c:47
struct MTex mtex
PreviewImage * preview
StructRNA * srna
Definition: RNA_types.h:681
Definition: DNA_ID.h:273
void * first
Definition: DNA_listBase.h:47
Definition: BKE_main.h:116
struct ToolSettings * toolsettings
Paint paint
float * nor
Definition: RE_texture.h:86
struct bNodeTree * nodetree
struct ImagePaintSettings imapaint
struct bNodeThreadStack * next
Definition: node_exec.h:69
struct bNodeStack * stack
Definition: node_exec.h:70
char idname[64]
Definition: BKE_node.h:381
void(* update)(struct bNodeTree *ntree)
Definition: BKE_node.h:407
void(* local_merge)(struct Main *bmain, struct bNodeTree *localtree, struct bNodeTree *ntree)
Definition: BKE_node.h:404
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:395
char ui_name[64]
Definition: BKE_node.h:383
void(* localize)(struct bNodeTree *localtree, struct bNodeTree *ntree)
Definition: BKE_node.h:402
char ui_description[256]
Definition: BKE_node.h:384
void(* foreach_nodeclass)(struct Scene *scene, void *calldata, bNodeClassCallback func)
Definition: BKE_node.h:391
void(* local_sync)(struct bNodeTree *localtree, struct bNodeTree *ntree)
Definition: BKE_node.h:403
ExtensionRNA rna_ext
Definition: BKE_node.h:414
struct bNodeInstanceHash * previews
ListBase nodes
struct bNodeTreeExec * execdata