Blender V5.3
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.hh"
16#include "BLI_threads.hh"
17#include "BLI_utildefines.hh"
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
37namespace blender {
38
40 bke::bNodeTreeType * /*treetype*/,
41 bNodeTree **r_ntree,
42 ID **r_id,
43 ID **r_from)
44{
45 const Main *bmain = CTX_data_main(C);
47 Scene *scene = CTX_data_scene(C);
48 ViewLayer *view_layer = CTX_data_view_layer(C);
49 BKE_view_layer_synced_ensure(*bmain, scene, view_layer);
51 Tex *tx = nullptr;
52
53 if (snode->texfrom == SNODE_TEX_BRUSH) {
54 Brush *brush = nullptr;
55
56 if (ob && (ob->mode & OB_MODE_SCULPT)) {
58 }
59 else {
61 }
62
63 if (brush) {
64 *r_from = reinterpret_cast<ID *>(brush);
66 if (tx) {
67 *r_id = &tx->id;
68 *r_ntree = tx->nodetree;
69 }
70 }
71 }
72 else if (snode->texfrom == SNODE_TEX_LINESTYLE) {
74 if (linestyle) {
75 *r_from = id_cast<ID *>(linestyle);
76 tx = give_current_linestyle_texture(linestyle);
77 if (tx) {
78 *r_id = &tx->id;
79 *r_ntree = tx->nodetree;
80 }
81 }
82 }
83}
84
85static void foreach_nodeclass(void *calldata, bke::bNodeClassCallback func)
86{
87 func(calldata, NODE_CLASS_INPUT, N_("Input"));
88 func(calldata, NODE_CLASS_OUTPUT, N_("Output"));
89 func(calldata, NODE_CLASS_OP_COLOR, N_("Color"));
90 func(calldata, NODE_CLASS_PATTERN, N_("Patterns"));
91 func(calldata, NODE_CLASS_TEXTURE, N_("Textures"));
92 func(calldata, NODE_CLASS_CONVERTER, N_("Converter"));
93 func(calldata, NODE_CLASS_DISTORT, N_("Distort"));
94 func(calldata, NODE_CLASS_GROUP, N_("Group"));
95 func(calldata, NODE_CLASS_INTERFACE, N_("Interface"));
96 func(calldata, NODE_CLASS_LAYOUT, N_("Layout"));
97}
98
99/* XXX muting disabled in previews because of threading issues with the main execution
100 * it works here, but disabled for consistency
101 */
102#if 1
103static void localize(bNodeTree *localtree, bNodeTree * /*ntree*/)
104{
105 bNode *node, *node_next;
106
107 /* replace muted nodes and reroute nodes by internal links */
108 for (node = localtree->nodes.first(); node; node = node_next) {
109 node_next = node->next;
110
111 if (node->is_muted() || node->is_reroute()) {
112 bke::node_internal_relink(*localtree, *node);
114 }
115 }
116}
117#else
118static void localize(bNodeTree * /*localtree*/, bNodeTree * /*ntree*/) {}
119#endif
120
121static void update(bNodeTree *ntree)
122{
124}
125
127 bke::bNodeSocketType *socket_type)
128{
129 return bke::node_is_static_socket_type(*socket_type) &&
130 ELEM(socket_type->type, SOCK_FLOAT, SOCK_VECTOR, SOCK_RGBA);
131}
132
134
136{
137 bke::bNodeTreeType *tt = ntreeType_Texture = MEM_new<bke::bNodeTreeType>(__func__);
138
139 tt->type = NTREE_TEXTURE;
140 tt->idname = "TextureNodeTree"_ustr;
141 tt->group_idname = "TextureNodeGroup"_ustr;
142 tt->ui_name = N_("Texture Node Editor");
143 tt->ui_icon = ICON_NODE_TEXTURE; /* Defined in `drawnode.cc`. */
144 tt->ui_description = N_("Edit textures using nodes");
145 /* Don't define this yet since we don't know which exact catalog name to use yet. Otherwise this
146 * has to be kept for compatibility. */
147 // tt->asset_catalog_path_prefix = "Textures";
148
150 tt->update = update;
151 tt->localize = localize;
154
155 tt->rna_ext.srna = RNA_TextureNodeTree;
156
158}
159
160/**** Material/Texture trees ****/
161
163{
164 ListBaseT<bNodeThreadStack> *lb = &exec->threadstack[thread];
165 bNodeThreadStack *nts;
166
167 for (nts = lb->first(); nts; nts = nts->next) {
168 if (!nts->used) {
169 nts->used = true;
170 break;
171 }
172 }
173
174 if (!nts) {
175 nts = MEM_new_zeroed<bNodeThreadStack>("bNodeThreadStack");
176 nts->stack = MEM_dupalloc(exec->stack);
177 nts->used = true;
178 BLI_addtail(lb, nts);
179 }
180
181 return nts;
182}
183
185{
186 nts->used = false;
187}
188
189bool ntreeExecThreadNodes(bNodeTreeExec *exec, bNodeThreadStack *nts, void *callerdata, int thread)
190{
191 bNodeStack *nsin[MAX_SOCKET] = {nullptr}; /* arbitrary... watch this */
192 bNodeStack *nsout[MAX_SOCKET] = {nullptr}; /* arbitrary... watch this */
193 bNodeExec *nodeexec;
194 bNode *node;
195 int n;
196
197 /* nodes are presorted, so exec is in order of list */
198
199 for (n = 0, nodeexec = exec->nodeexec; n < exec->totnodes; n++, nodeexec++) {
200 node = nodeexec->node;
201 if (node->runtime->need_exec) {
202 node_get_stack(node, nts->stack, nsin, nsout);
203 /* Handle muted nodes...
204 * If the mute func is not set, assume the node should never be muted,
205 * and hence execute it!
206 */
207 if (node->typeinfo->exec_fn && !node->is_muted()) {
208 node->typeinfo->exec_fn(callerdata, thread, node, &nodeexec->data, nsin, nsout);
209 }
210 }
211 }
212
213 /* signal to that all went OK, for render */
214 return true;
215}
216
218 bNodeTree *ntree,
219 bNodeInstanceKey parent_key)
220{
221 bNodeTreeExec *exec;
222
223 /* common base initialization */
224 exec = ntree_exec_begin(context, ntree, parent_key);
225
226 /* allocate the thread stack listbase array */
228 "thread stack array");
229
230 for (bNode &node : exec->nodetree->nodes) {
231 node.runtime->need_exec = 1;
232 }
233
234 return exec;
235}
236
238{
239 bNodeExecContext context;
240 bNodeTreeExec *exec;
241
242 /* XXX hack: prevent exec data from being generated twice.
243 * this should be handled by the renderer!
244 */
245 if (ntree->runtime->execdata) {
246 return ntree->runtime->execdata;
247 }
248
250
251 /* XXX this should not be necessary, but is still used for compositor/shading/texture nodes,
252 * which only store the ntree pointer. Should be fixed at some point!
253 */
254 ntree->runtime->execdata = exec;
255
256 return exec;
257}
258
259/* free texture delegates */
261{
262 bNodeStack *ns;
263 int th, a;
264
265 for (th = 0; th < BLENDER_MAX_THREADS; th++) {
266 for (bNodeThreadStack &nts : exec->threadstack[th]) {
267 for (ns = nts.stack, a = 0; a < exec->stacksize; a++, ns++) {
268 if (ns->data && !ns->is_copy) {
269 MEM_delete(static_cast<TexDelegate *>(ns->data));
270 }
271 }
272 }
273 }
274}
275
277{
278 int a;
279
280 if (exec->threadstack) {
281 tex_free_delegates(exec);
282
283 for (a = 0; a < BLENDER_MAX_THREADS; a++) {
284 for (bNodeThreadStack &nts : exec->threadstack[a]) {
285 if (nts.stack) {
286 MEM_delete(nts.stack);
287 }
288 }
289 BLI_freelistN(&exec->threadstack[a]);
290 }
291
292 MEM_delete(exec->threadstack);
293 exec->threadstack = nullptr;
294 }
295
296 ntree_exec_end(exec);
297}
298
300{
301 if (exec) {
302 /* exec may get freed, so assign ntree */
303 bNodeTree *ntree = exec->nodetree;
305
306 /* XXX: clear node-tree back-pointer to exec data,
307 * same problem as noted in #ntreeBeginExecTree. */
308 ntree->runtime->execdata = nullptr;
309 }
310}
311
313 TexResult *target,
314 const float co[3],
315 const short thread,
316 const Tex * /*tex*/,
317 short which_output,
318 int cfra,
319 int preview,
320 MTex *mtex)
321{
323 int retval = TEX_INT;
324 bNodeThreadStack *nts = nullptr;
325 bNodeTreeExec *exec = ntree->runtime->execdata;
326
327 data.co = co;
328 data.target = target;
329 data.do_preview = preview;
330 data.do_manage = true;
331 data.thread = thread;
332 data.which_output = which_output;
333 data.cfra = cfra;
334 data.mtex = mtex;
335
336 /* ensure execdata is only initialized once */
337 if (!exec) {
339 if (!ntree->runtime->execdata) {
341 }
343
344 exec = ntree->runtime->execdata;
345 }
346
347 nts = ntreeGetThreadStack(exec, thread);
348 ntreeExecThreadNodes(exec, nts, &data, thread);
350
351 retval |= TEX_RGB;
352
353 return retval;
354}
355
356} // namespace blender
Blender kernel freestyle line style functionality.
#define NODE_CLASS_OUTPUT
Definition BKE_node.hh:479
#define NODE_CLASS_INTERFACE
Definition BKE_node.hh:490
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:484
#define NODE_CLASS_PATTERN
Definition BKE_node.hh:487
#define NODE_CLASS_DISTORT
Definition BKE_node.hh:486
#define NODE_CLASS_LAYOUT
Definition BKE_node.hh:494
#define MAX_SOCKET
Definition BKE_node.hh:30
#define NODE_CLASS_OP_COLOR
Definition BKE_node.hh:480
#define NODE_CLASS_INPUT
Definition BKE_node.hh:478
#define NODE_CLASS_GROUP
Definition BKE_node.hh:483
#define NODE_CLASS_TEXTURE
Definition BKE_node.hh:488
#define BLENDER_MAX_THREADS
#define C
Definition RandGen.cpp:29
#define ELEM(...)
void node_tree_free_local_node(bNodeTree &ntree, bNode &node)
Definition node.cc:5141
void node_tree_type_add(bNodeTreeType &nt)
Definition node.cc:2844
void node_internal_relink(bNodeTree &ntree, bNode &node)
Definition node.cc:4812
const bNodeInstanceKey NODE_INSTANCE_KEY_BASE
Definition node.cc:5600
void(*)(void *calldata, int nclass, StringRefNull name) bNodeClassCallback
Definition BKE_node.hh:523
bool node_is_static_socket_type(const bNodeSocketType &stype)
Definition node.cc:3483
BMesh Iterators.
struct bNodeTreeExec * ntreeTexBeginExecTree(struct bNodeTree *ntree)
Object * BKE_view_layer_active_object_get(const ViewLayer *view_layer)
void ntreeTexEndExecTree_internal(bNodeTreeExec *exec)
static void localize(bNodeTree *localtree, bNodeTree *)
ViewLayer * CTX_data_view_layer(const bContext *C)
static void texture_get_from_context(const bContext *C, bke::bNodeTreeType *, bNodeTree **r_ntree, ID **r_id, ID **r_from)
struct bke::bNodeTreeType * ntreeType_Texture
void ntree_exec_end(bNodeTreeExec *exec)
Definition node_exec.cc:285
bNodeTreeExec * ntreeTexBeginExecTree_internal(bNodeExecContext *context, bNodeTree *ntree, bNodeInstanceKey parent_key)
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void void BLI_freelistN(ListBase *listbase) ATTR_NONNULL(1)
Definition listbase.cc:504
BMesh const char void * data
struct Tex * give_current_linestyle_texture(struct FreestyleLineStyle *linestyle)
Definition texture.cc:443
static bool texture_node_tree_socket_type_valid(bke::bNodeTreeType *, bke::bNodeSocketType *socket_type)
bool BKE_view_layer_synced_ensure(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
void ntreeTexEndExecTree(struct bNodeTreeExec *exec)
SpaceNode * CTX_wm_space_node(const bContext *C)
bool ntreeExecThreadNodes(bNodeTreeExec *exec, bNodeThreadStack *nts, void *callerdata, int thread)
struct Tex * give_current_brush_texture(struct Brush *br)
Definition texture.cc:527
bNodeTreeExec * ntree_exec_begin(bNodeExecContext *context, bNodeTree *ntree, bNodeInstanceKey parent_key)
Definition node_exec.cc:192
static void update(bNodeTree *ntree)
int ntreeTexExecTree(struct bNodeTree *ntree, struct TexResult *target, const float co[3], short thread, const struct Tex *tex, short which_output, int cfra, int preview, struct MTex *mtex)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
Main * CTX_data_main(const bContext *C)
bNodeThreadStack * ntreeGetThreadStack(bNodeTreeExec *exec, int thread)
void ntree_update_reroute_nodes(bNodeTree *ntree)
FreestyleLineStyle * BKE_linestyle_active_from_view_layer(struct ViewLayer *view_layer)
Definition linestyle.cc:773
static void tex_free_delegates(bNodeTreeExec *exec)
Brush * BKE_paint_brush(Paint *paint)
Scene * CTX_data_scene(const bContext *C)
void register_node_tree_type_tex()
void BLI_thread_unlock(int type)
Definition threads.cc:336
static void foreach_nodeclass(void *calldata, bke::bNodeClassCallback func)
void node_get_stack(bNode *node, bNodeStack *stack, bNodeStack **in, bNodeStack **out)
Definition node_exec.cc:47
void BLI_thread_lock(int type)
Definition threads.cc:331
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void ntreeReleaseThreadStack(bNodeThreadStack *nts)
struct ToolSettings * toolsettings
eSpaceNode_TexFrom texfrom
struct bNodeTree * nodetree
struct ImagePaintSettings imapaint
bNodeExecData data
Definition node_exec.hh:28
bNodeThreadStack * next
Definition node_exec.hh:49
ListBaseT< bNodeThreadStack > * threadstack
Definition node_exec.hh:44
bke::bNodeTreeRuntime * runtime
ListBaseT< bNode > nodes
Defines a socket type.
Definition BKE_node.hh:163
eNodeSocketDatatype type
Definition BKE_node.hh:198
bool(*) valid_socket_type(bNodeTreeType *ntreetype, bNodeSocketType *socket_type)
Definition BKE_node.hh:569
void(*) get_from_context(const bContext *C, bNodeTreeType *ntreetype, bNodeTree **r_ntree, ID **r_id, ID **r_from)
Definition BKE_node.hh:554
void(*) update(bNodeTree *ntree)
Definition BKE_node.hh:564
void(*) foreach_nodeclass(void *calldata, bNodeClassCallback func)
Definition BKE_node.hh:550
void(*) localize(bNodeTree *localtree, bNodeTree *ntree)
Definition BKE_node.hh:561
#define N_(msgid)