Blender V5.3
node_exec.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 "DNA_node_types.h"
10
11#include "BLI_listbase.hh"
12#include "BLI_utildefines.hh"
13
14#include "BKE_global.hh"
15#include "BKE_node_runtime.hh"
18
19#include "MEM_guardedalloc.h"
20
21#include "node_exec.hh"
22#include "node_util.hh"
23
24namespace blender {
25
27{
28 /* NOTE: INT and BOOL supported as FLOAT. Only for EEVEE. */
29 return ELEM(sock->type,
37}
38
40{
41 if (stack && sock && sock->stack_index >= 0) {
42 return stack + sock->stack_index;
43 }
44 return nullptr;
45}
46
48{
49 /* build pointer stack */
50 if (in) {
51 for (bNodeSocket &sock : node->inputs) {
52 *(in++) = node_get_socket_stack(stack, &sock);
53 }
54 }
55
56 if (out) {
57 for (bNodeSocket &sock : node->outputs) {
58 *(out++) = node_get_socket_stack(stack, &sock);
59 }
60 }
61}
62
63static void node_init_input_index(bNodeSocket *sock, int *index)
64{
65 /* Only consider existing link when the `from` socket is valid! */
66 if (sock->link && !(sock->link->flag & NODE_LINK_MUTED) && sock->link->fromsock &&
67 sock->link->fromsock->stack_index >= 0)
68 {
69 sock->stack_index = sock->link->fromsock->stack_index;
70 }
71 else {
73 sock->stack_index = (*index)++;
74 }
75 else {
76 sock->stack_index = -1;
77 }
78 }
79}
80
82{
83 /* Copy the stack index from the internally connected input to skip the node. */
84 if (const bNodeSocket *internal_input = sock->runtime->internal_link_input) {
85 sock->stack_index = internal_input->stack_index;
86 return;
87 }
88 /* If not internally connected, assign a new stack index anyway to avoid bad stack access. */
90 sock->stack_index = (*index)++;
91 }
92 else {
93 sock->stack_index = -1;
94 }
95}
96
98{
100 sock->stack_index = (*index)++;
101 }
102 else {
103 sock->stack_index = -1;
104 }
105}
106
107/* basic preparation of socket stacks */
109{
110 bNodeStack *ns = node_get_socket_stack(stack, sock);
111 if (!ns) {
112 return nullptr;
113 }
114
115 /* don't mess with remote socket stacks, these are initialized by other nodes! */
116 if (sock->link && !(sock->link->flag & NODE_LINK_MUTED)) {
117 return ns;
118 }
119 /* Outputs of muted nodes forward the internally linked input's stack value. */
120 if (node->is_muted() && sock->runtime->internal_link_input != nullptr) {
121 return ns;
122 }
123 if (sock->is_output() && node->is_reroute()) {
124 return ns;
125 }
126
127 ns->sockettype = sock->type;
128
129 switch (sock->type) {
130 case SOCK_INT:
131 ns->vec[0] = node_socket_get_int(ntree, node, sock);
132 break;
133 case SOCK_BOOLEAN:
134 ns->vec[0] = node_socket_get_bool(ntree, node, sock);
135 break;
136 case SOCK_FLOAT:
137 ns->vec[0] = node_socket_get_float(ntree, node, sock);
138 break;
139 case SOCK_VECTOR:
140 node_socket_get_vector(ntree, node, sock, ns->vec);
141 break;
142 case SOCK_RGBA:
143 node_socket_get_color(ntree, node, sock, ns->vec);
144 break;
145 case SOCK_ROTATION:
146 node_socket_get_rotation(ntree, node, sock, ns->vec);
147 break;
148 default:
149 break;
150 }
151
152 return ns;
153}
154
156{
157 ntree.ensure_topology_cache();
158 Vector<bNode *> nodes = ntree.toposort_left_to_right();
159 const bke::bNodeTreeZones *zones = ntree.zones();
160 if (!zones || zones->zones.is_empty()) {
161 return nodes;
162 }
163 /* Insertion sort to make sure that all nodes in a zone are packed together right before the zone
164 * output. */
165 for (int old_i = nodes.size() - 1; old_i >= 0; old_i--) {
166 bNode *node = nodes[old_i];
167 const bke::bNodeTreeZone *zone = zones->get_zone_by_node(node->identifier);
168 if (zone && zone->output_node_id == node->identifier) {
169 /* The output of a zone should not be moved, except for nested zones,
170 * which still have to be packed together with the rest of the parent zone. */
171 zone = zone->parent_zone;
172 }
173 if (!zone) {
174 /* Nodes outside of any zone can stay where they are. */
175 continue;
176 }
177 for (int new_i = old_i + 1; new_i < nodes.size(); new_i++) {
178 bNode *next_node = nodes[new_i];
179 const bke::bNodeTreeZone *zone_to_check = zones->get_zone_by_node(next_node->identifier);
180 if (zone_to_check &&
181 (zone == zone_to_check || zone->contains_zone_recursively(*zone_to_check)))
182 {
183 /* Don't move the node further than the next node in the zone. */
184 break;
185 }
186 std::swap(nodes[new_i - 1], nodes[new_i]);
187 }
188 }
189 return nodes;
190}
191
193 bNodeTree *ntree,
194 bNodeInstanceKey parent_key)
195{
196 bNodeTreeExec *exec;
197 bNode *node;
198 bNodeExec *nodeexec;
199 bNodeInstanceKey nodekey;
200 bNodeStack *ns;
201 int index;
202 /* XXX: texture-nodes have threading issues with muting, have to disable it there. */
203
204 /* ensure all sock->link pointers and node levels are correct */
205 /* Using global main here is likely totally wrong, not sure what to do about that one though...
206 * We cannot even check ntree is in global main,
207 * since most of the time it won't be (thanks to ntree design)!!! */
209
210 ntree->ensure_topology_cache();
211 Vector<bNode *> nodelist = get_node_code_gen_order(*ntree);
212
213 /* XXX could let callbacks do this for specialized data */
214 exec = MEM_new_zeroed<bNodeTreeExec>("node tree execution data");
215 /* Back-pointer to node tree. */
216 exec->nodetree = ntree;
217
218 /* set stack indices */
219 index = 0;
220 for (const int n : nodelist.index_range()) {
221 node = nodelist[n];
222
223 /* init node socket stack indexes */
224 for (bNodeSocket &sock : node->inputs) {
226 }
227
228 if (node->is_muted() || node->is_reroute()) {
229 for (bNodeSocket &sock : node->outputs) {
231 }
232 }
233 else {
234 for (bNodeSocket &sock : node->outputs) {
236 }
237 }
238 }
239
240 /* allocated exec data pointers for nodes */
241 exec->totnodes = nodelist.size();
242 exec->nodeexec = MEM_new_array_zeroed<bNodeExec>(exec->totnodes, "node execution data");
243 /* allocate data pointer for node stack */
244 exec->stacksize = index;
245 exec->stack = MEM_new_array<bNodeStack>(exec->stacksize, "bNodeStack");
246
247 /* all non-const results are considered inputs */
248 int n;
249 for (n = 0; n < exec->stacksize; n++) {
250 exec->stack[n].hasinput = 1;
251 }
252
253 /* prepare all nodes for execution */
254 for (n = 0, nodeexec = exec->nodeexec; n < nodelist.size(); n++, nodeexec++) {
255 node = nodeexec->node = nodelist[n];
256 nodeexec->free_exec_fn = node->typeinfo->free_exec_fn;
257
258 /* tag inputs */
259 for (bNodeSocket &sock : node->inputs) {
260 /* disable the node if an input link is invalid */
261 if (sock.link && !(sock.link->flag & NODE_LINK_VALID)) {
262 node->runtime->need_exec = 0;
263 }
264
265 ns = setup_stack(exec->stack, ntree, node, &sock);
266 if (ns) {
267 ns->hasoutput = 1;
268 }
269 }
270
271 /* tag all outputs */
272 for (bNodeSocket &sock : node->outputs) {
273 /* ns = */ setup_stack(exec->stack, ntree, node, &sock);
274 }
275
276 nodekey = bke::node_instance_key(parent_key, ntree, node);
277 if (node->typeinfo->init_exec_fn) {
278 nodeexec->data.data = node->typeinfo->init_exec_fn(context, node, nodekey);
279 }
280 }
281
282 return exec;
283}
284
286{
287 bNodeExec *nodeexec;
288 int n;
289
290 if (exec->stack) {
291 MEM_delete(exec->stack);
292 }
293
294 for (n = 0, nodeexec = exec->nodeexec; n < exec->totnodes; n++, nodeexec++) {
295 if (nodeexec->free_exec_fn) {
296 nodeexec->free_exec_fn(nodeexec->data.data);
297 }
298 }
299
300 if (exec->nodeexec) {
301 MEM_delete(exec->nodeexec);
302 }
303
304 MEM_delete(exec);
305}
306
307} // namespace blender
Read Blender memory allocation and freeing.
#define ELEM(...)
int64_t size() const
IndexRange index_range() const
bool contains_zone_recursively(const bNodeTreeZone &other_zone) const
std::optional< int > output_node_id
Vector< bNodeTreeZone * > zones
const bNodeTreeZone * get_zone_by_node(const int32_t node_id) const
bNodeInstanceKey node_instance_key(bNodeInstanceKey parent_key, const bNodeTree *ntree, const bNode *node)
Definition node.cc:5621
BMesh Iterators.
static void node_init_input_index(bNodeSocket *sock, int *index)
Definition node_exec.cc:63
int node_socket_get_int(bNodeTree *ntree, bNode *, bNodeSocket *sock)
Definition node_util.cc:234
static Vector< bNode * > get_node_code_gen_order(bNodeTree &ntree)
Definition node_exec.cc:155
float node_socket_get_float(bNodeTree *ntree, bNode *, bNodeSocket *sock)
Definition node_util.cc:258
static void node_init_output_index(bNodeSocket *sock, int *index)
Definition node_exec.cc:97
static bNodeStack * setup_stack(bNodeStack *stack, bNodeTree *ntree, bNode *node, bNodeSocket *sock)
Definition node_exec.cc:108
Global G
Definition blender.cc:57
void ntree_exec_end(bNodeTreeExec *exec)
Definition node_exec.cc:285
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
bNodeStack * node_get_socket_stack(bNodeStack *stack, bNodeSocket *sock)
Definition node_exec.cc:39
void BKE_ntree_update_after_single_tree_change(Main &bmain, bNodeTree &modified_tree, const NodeTreeUpdateExtraParams &params={})
void node_socket_get_color(bNodeTree *ntree, bNode *, bNodeSocket *sock, float *value)
Definition node_util.cc:270
void node_socket_get_rotation(bNodeTree *ntree, bNode *, bNodeSocket *sock, float *value)
Definition node_util.cc:300
static void node_init_output_index_muted(bNodeSocket *sock, int *index)
Definition node_exec.cc:81
bNodeTreeExec * ntree_exec_begin(bNodeExecContext *context, bNodeTree *ntree, bNodeInstanceKey parent_key)
Definition node_exec.cc:192
static int node_exec_socket_use_stack(bNodeSocket *sock)
Definition node_exec.cc:26
bool node_socket_get_bool(bNodeTree *ntree, bNode *, bNodeSocket *sock)
Definition node_util.cc:246
void node_socket_get_vector(bNodeTree *ntree, bNode *, bNodeSocket *sock, float *value)
Definition node_util.cc:285
void node_get_stack(bNode *node, bNodeStack *stack, bNodeStack **in, bNodeStack **out)
Definition node_exec.cc:47
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
bNodeExecData data
Definition node_exec.hh:28
bke::NodeFreeExecFunction free_exec_fn
Definition node_exec.hh:31
eNodeSocketDatatype type
struct bNodeLink * link
bke::bNodeSocketRuntime * runtime