Blender V5.3
node_shader_util.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2005 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <optional>
10
11#include "DNA_node_types.h"
12#include "DNA_space_types.h"
13
15#include "BLI_math_vector_c.hh"
16#include "BLI_string.hh"
17
18#include "BKE_context.hh"
19#include "BKE_node_runtime.hh"
20
22
23#include "node_shader_util.hh"
24
26
27#include "RE_engine.h"
28
29#include "node_exec.hh"
30
31namespace blender {
32
33bool sh_node_poll_default(const bke::bNodeType * /*ntype*/,
34 const bNodeTree *ntree,
35 const char **r_disabled_hint)
36{
37 if (!STREQ(ntree->idname, "ShaderNodeTree")) {
38 *r_disabled_hint = RPT_("Not a shader node tree");
39 return false;
40 }
41 return true;
42}
43
44static bool sh_geo_poll_default(const bke::bNodeType * /*ntype*/,
45 const bNodeTree *ntree,
46 const char **r_disabled_hint)
47{
48 if (!STR_ELEM(ntree->idname, "ShaderNodeTree", "GeometryNodeTree")) {
49 *r_disabled_hint = RPT_("Not a shader or geometry node tree");
50 return false;
51 }
52 return true;
53}
54
55static bool common_poll_default(const bke::bNodeType * /*ntype*/,
56 const bNodeTree *ntree,
57 const char **r_disabled_hint)
58{
59 if (!STR_ELEM(ntree->idname, "ShaderNodeTree", "GeometryNodeTree", "CompositorNodeTree")) {
60 *r_disabled_hint = RPT_("Not a shader, geometry, or compositor node tree");
61 return false;
62 }
63 return true;
64}
65
67 UString idname,
68 const std::optional<int16_t> legacy_type)
69{
70 bke::node_type_base(*ntype, idname, legacy_type);
71
75}
76
78 UString idname,
79 const std::optional<int16_t> legacy_type)
80{
81 bke::node_type_base(*ntype, idname, legacy_type);
82
86}
87
89 UString idname,
90 const std::optional<int16_t> legacy_type)
91{
92 sh_node_type_base(ntype, idname, legacy_type);
95}
96
98{
99 const SpaceNode *snode = CTX_wm_space_node(C);
100 return snode->shaderfrom == SNODE_SHADER_LINESTYLE;
101}
102
104{
105 const SpaceNode *snode = CTX_wm_space_node(C);
106 return snode->shaderfrom == SNODE_SHADER_WORLD;
107}
108
110{
111 const SpaceNode *snode = CTX_wm_space_node(C);
112 return snode->shaderfrom == SNODE_SHADER_OBJECT;
113}
114
116{
118 return false;
119 }
120 const RenderEngineType *engine_type = CTX_data_engine_type(C);
121 return STREQ(engine_type->idname, "CYCLES");
122}
123
125{
127 return false;
128 }
129 const RenderEngineType *engine_type = CTX_data_engine_type(C);
130 return STREQ(engine_type->idname, "BLENDER_EEVEE") ||
131 STREQ(engine_type->idname, "BLENDER_EEVEE");
132}
133
134/* ****** */
135
136static void nodestack_get_vec(float *in, short type_in, bNodeStack *ns)
137{
138 const float *from = ns->vec;
139
140 switch (type_in) {
141 case SOCK_FLOAT:
142 case SOCK_INT: {
143 switch (ns->sockettype) {
144 case SOCK_FLOAT:
145 case SOCK_INT:
146 case SOCK_BOOLEAN:
147 *in = from[0];
148 break;
149 case SOCK_RGBA:
151 break;
152 case SOCK_VECTOR:
153 *in = (from[0] + from[1] + from[2]) / 3.0f;
154 break;
155 case SOCK_ROTATION:
156 *in = (from[0] + from[1] + from[2] + from[3]) / 4.0f;
157 break;
158 case SOCK_SHADER:
159 case SOCK_STRING:
160 case SOCK_MENU:
161 case SOCK_BUNDLE:
162 case SOCK_CLOSURE:
163 break;
164 default:
166 break;
167 }
168 break;
169 }
170 case SOCK_BOOLEAN: {
171 switch (ns->sockettype) {
172 case SOCK_FLOAT:
173 case SOCK_INT:
174 case SOCK_BOOLEAN:
175 *in = from[0];
176 break;
177 case SOCK_RGBA:
179 break;
180 case SOCK_VECTOR:
181 *in = !is_zero_v3(from);
182 break;
183 case SOCK_ROTATION:
184 *in = !is_zero_v4(from);
185 break;
186 case SOCK_SHADER:
187 case SOCK_STRING:
188 case SOCK_MENU:
189 case SOCK_BUNDLE:
190 case SOCK_CLOSURE:
191 break;
192 default:
194 break;
195 }
196 break;
197 }
198 case SOCK_VECTOR: {
199 switch (ns->sockettype) {
200 case SOCK_FLOAT:
201 case SOCK_INT:
202 case SOCK_BOOLEAN:
203 copy_v4_fl(in, from[0]);
204 break;
205 case SOCK_VECTOR:
206 case SOCK_RGBA:
207 case SOCK_ROTATION:
208 copy_v4_v4(in, from);
209 break;
210 case SOCK_SHADER:
211 case SOCK_STRING:
212 case SOCK_MENU:
213 case SOCK_BUNDLE:
214 case SOCK_CLOSURE:
215 break;
216 default:
218 break;
219 }
220 break;
221 }
222 case SOCK_RGBA: {
223 switch (ns->sockettype) {
224 case SOCK_FLOAT:
225 case SOCK_INT:
226 case SOCK_BOOLEAN:
227 copy_v3_fl(in, from[0]);
228 in[3] = 1.0f;
229 break;
230 case SOCK_VECTOR:
231 copy_v3_v3(in, from);
232 in[3] = 1.0f;
233 break;
234 case SOCK_RGBA:
235 case SOCK_ROTATION:
236 copy_v4_v4(in, from);
237 break;
238 case SOCK_SHADER:
239 case SOCK_STRING:
240 case SOCK_MENU:
241 case SOCK_BUNDLE:
242 case SOCK_CLOSURE:
243 break;
244 default:
246 break;
247 }
248 break;
249 }
250 case SOCK_ROTATION: {
251 switch (ns->sockettype) {
252 case SOCK_FLOAT:
253 case SOCK_INT:
254 case SOCK_BOOLEAN: {
255 const float eul[3] = {from[0], from[0], from[0]};
256 eul_to_quat(in, eul);
257 break;
258 }
259 case SOCK_VECTOR:
260 eul_to_quat(in, from);
261 break;
262 case SOCK_RGBA:
263 case SOCK_ROTATION:
264 copy_v4_v4(in, from);
265 break;
266 case SOCK_SHADER:
267 case SOCK_STRING:
268 case SOCK_MENU:
269 case SOCK_BUNDLE:
270 case SOCK_CLOSURE:
271 break;
272 default:
274 break;
275 }
276 break;
277 }
278 case SOCK_SHADER:
279 case SOCK_STRING:
280 case SOCK_MENU:
281 case SOCK_BUNDLE:
282 case SOCK_CLOSURE:
283 break;
284 default:
286 break;
287 }
288}
289
291{
292 *gs = GPUNodeStack{};
293
294 if (ns == nullptr) {
295 /* node_get_stack() will generate nullptr bNodeStack pointers
296 * for unknown/unsupported types of sockets. */
297 gs->value = std::monostate{};
298 gs->link = nullptr;
299 gs->type = GPU_NONE;
300 gs->hasinput = false;
301 gs->hasoutput = false;
302 gs->sockettype = socket->type;
303 }
304 else {
305 gs->link = static_cast<GPUNodeLink *>(ns->data);
306
307 /* The value is retrieved as float4, then stored using the type of the socket. */
308 float4 vec(0.0f);
309 nodestack_get_vec(vec, socket->type, ns);
310
311 switch (socket->type) {
312 case SOCK_FLOAT:
313 gs->type = GPU_FLOAT;
314 gs->value = vec.x;
315 break;
316 case SOCK_INT:
317 gs->type = GPU_INT;
318 gs->value = int(vec.x);
319 break;
320 case SOCK_BOOLEAN:
321 gs->type = GPU_BOOL;
322 gs->value = vec.x > 0.0f;
323 break;
324 case SOCK_VECTOR:
325 switch (socket->default_value_typed<bNodeSocketValueVector>()->dimensions) {
326 case 2:
327 gs->type = GPU_VEC2;
328 gs->value = float2(vec);
329 break;
330 case 3:
331 gs->type = GPU_VEC3;
332 gs->value = float3(vec);
333 break;
334 case 4:
335 gs->type = GPU_VEC4;
336 gs->value = vec;
337 break;
338 default:
340 break;
341 }
342 break;
343 case SOCK_RGBA:
344 case SOCK_ROTATION:
345 gs->type = GPU_VEC4;
346 gs->value = vec;
347 break;
348 case SOCK_SHADER:
349 gs->type = GPU_CLOSURE;
350 gs->value = std::monostate{};
351 break;
352 default:
353 gs->type = GPU_NONE;
354 gs->value = std::monostate{};
355 break;
356 }
357
358 gs->hasinput = ns->hasinput && ns->data;
359 /* XXX Commented out the ns->data check here, as it seems it's not always set,
360 * even though there *is* a valid connection/output... But that might need
361 * further investigation.
362 */
363 gs->hasoutput = ns->hasoutput /*&& ns->data*/;
364 gs->sockettype = ns->sockettype;
365 }
366}
367
369{
370 float4 vec(0.0f);
371 switch (gs->type) {
372 case GPU_FLOAT:
373 vec.x = std::get<float>(gs->value);
374 break;
375 case GPU_INT:
376 vec.x = float(std::get<int>(gs->value));
377 break;
378 case GPU_BOOL:
379 vec.x = float(std::get<bool>(gs->value));
380 break;
381 case GPU_VEC2:
382 vec = float4(std::get<float2>(gs->value), 0.0f, 0.0f);
383 break;
384 case GPU_VEC3:
385 vec = float4(std::get<float3>(gs->value), 0.0f);
386 break;
387 case GPU_VEC4:
388 vec = std::get<float4>(gs->value);
389 break;
390 default:
391 break;
392 }
393 copy_v4_v4(ns->vec, vec);
394 ns->data = gs->link;
395 ns->sockettype = gs->sockettype;
396}
397
399 ListBaseT<bNodeSocket> *sockets,
400 bNodeStack **ns)
401{
402 int i = 0;
403 for (bNodeSocket &socket : *sockets) {
404 node_gpu_stack_from_data(&gs[i], &socket, ns[i]);
405 i++;
406 }
407
408 gs[i].end = true;
409}
410
411static void data_from_gpu_stack_list(ListBaseT<bNodeSocket> *sockets,
412 bNodeStack **ns,
413 GPUNodeStack *gs)
414{
415 int i = 0;
416 for (bNodeSocket &socket : *sockets) {
418 {
419 node_data_from_gpu_stack(ns[i], &gs[i]);
420 i++;
421 }
422 }
423}
424
425bool bke::node_supports_active_flag(const bNode &node, int sub_activity)
426{
428 switch (sub_activity) {
430 return node.typeinfo->nclass == NODE_CLASS_TEXTURE;
432 return ELEM(node.type_legacy, SH_NODE_TEX_IMAGE, SH_NODE_ATTRIBUTE);
433 }
434 return false;
435}
436
437static bNode *node_get_active(bNodeTree *ntree, int sub_activity)
438{
440 /* this is the node we texture paint and draw in textured draw */
441 bNode *inactivenode = nullptr, *activetexnode = nullptr, *activegroup = nullptr;
442 bool hasgroup = false;
443
444 if (!ntree) {
445 return nullptr;
446 }
447
448 for (bNode *node : ntree->all_nodes()) {
449 if (node->flag & sub_activity) {
450 activetexnode = node;
451 /* if active we can return immediately */
452 if (node->flag & NODE_ACTIVE) {
453 return node;
454 }
455 }
456 else if (!inactivenode && bke::node_supports_active_flag(*node, sub_activity)) {
457 inactivenode = node;
458 }
459 else if (node->type_legacy == NODE_GROUP) {
460 if (node->flag & NODE_ACTIVE) {
461 activegroup = node;
462 }
463 else {
464 hasgroup = true;
465 }
466 }
467 }
468
469 /* first, check active group for textures */
470 if (activegroup) {
471 bNode *tnode = node_get_active(id_cast<bNodeTree *>(activegroup->id), sub_activity);
472 /* active node takes priority, so ignore any other possible nodes here */
473 if (tnode) {
474 return tnode;
475 }
476 }
477
478 if (activetexnode) {
479 return activetexnode;
480 }
481
482 if (hasgroup) {
483 /* node active texture node in this tree, look inside groups */
484 for (bNode *node : ntree->all_nodes()) {
485 if (node->type_legacy == NODE_GROUP) {
486 bNode *tnode = node_get_active(id_cast<bNodeTree *>(node->id), sub_activity);
487 if (tnode && ((tnode->flag & sub_activity) || !inactivenode)) {
488 return tnode;
489 }
490 }
491 }
492 }
493
494 return inactivenode;
495}
496
497namespace bke {
498
503
508} // namespace bke
509
511 GPUMaterial *mat,
512 bNode *output_node,
513 const int *depth_level)
514{
515 bNodeExec *nodeexec;
516 bNode *node;
517 int n;
518 bNodeStack *stack;
519 bNodeStack *nsin[MAX_SOCKET]; /* arbitrary... watch this */
520 bNodeStack *nsout[MAX_SOCKET]; /* arbitrary... watch this */
521 GPUNodeStack gpuin[MAX_SOCKET + 1], gpuout[MAX_SOCKET + 1];
522 bool do_it;
523
524 stack = exec->stack;
525
526 for (n = 0, nodeexec = exec->nodeexec; n < exec->totnodes; n++, nodeexec++) {
527 node = nodeexec->node;
528
529 if (depth_level && node->runtime->tmp_flag != *depth_level) {
530 continue;
531 }
532
533 do_it = false;
534 /* for groups, only execute outputs for edited group */
535 if (node->typeinfo->nclass == NODE_CLASS_OUTPUT) {
536 if ((output_node != nullptr) && (node == output_node)) {
537 do_it = true;
538 }
539 }
540 else {
541 do_it = node->runtime->need_exec;
542 node->runtime->need_exec = 0;
543 }
544
545 if (do_it) {
546 BLI_assert(!depth_level || node->runtime->tmp_flag >= 0);
547 if (node->typeinfo->gpu_fn) {
548 node_get_stack(node, stack, nsin, nsout);
549 gpu_stack_from_data_list(gpuin, &node->inputs, nsin);
550 gpu_stack_from_data_list(gpuout, &node->outputs, nsout);
551 if (node->typeinfo->gpu_fn(mat, node, &nodeexec->data, gpuin, gpuout)) {
552 data_from_gpu_stack_list(&node->outputs, nsout, gpuout);
553 }
554 }
555 }
556 }
557}
558
560{
561 GPU_link(mat, "differentiate_texco", *link, link);
562}
563
565{
566 if (!*link) {
567 *link = GPU_attribute(mat, CD_ORCO, "");
569 }
570}
571
573 bNode *node,
575 GPUNodeStack * /*out*/)
576{
577 NodeTexBase *base = static_cast<NodeTexBase *>(node->storage);
578 TexMapping *texmap = &base->tex_mapping;
579 float domin = (texmap->flag & TEXMAP_CLIP_MIN) != 0;
580 float domax = (texmap->flag & TEXMAP_CLIP_MAX) != 0;
581
582 if (domin || domax || !(texmap->flag & TEXMAP_UNIT_MATRIX)) {
583 static float max[3] = {FLT_MAX, FLT_MAX, FLT_MAX};
584 static float min[3] = {-FLT_MAX, -FLT_MAX, -FLT_MAX};
585 GPUNodeLink *tmin, *tmax, *tmat0, *tmat1, *tmat2, *tmat3;
586
587 tmin = GPU_uniform((domin) ? texmap->min : min);
588 tmax = GPU_uniform((domax) ? texmap->max : max);
589 tmat0 = GPU_uniform(static_cast<float *>(texmap->mat[0]));
590 tmat1 = GPU_uniform(static_cast<float *>(texmap->mat[1]));
591 tmat2 = GPU_uniform(static_cast<float *>(texmap->mat[2]));
592 tmat3 = GPU_uniform(static_cast<float *>(texmap->mat[3]));
593
594 GPU_link(mat, "mapping_mat4", in[0].link, tmat0, tmat1, tmat2, tmat3, tmin, tmax, &in[0].link);
595
596 if (texmap->type == TEXMAP_TYPE_NORMAL) {
597 GPU_link(mat, "vector_normalize", in[0].link, &in[0].link);
598 }
599 }
600}
601
603{
605 data->r[0] = xyz_to_rgb[0][0];
606 data->r[1] = xyz_to_rgb[1][0];
607 data->r[2] = xyz_to_rgb[2][0];
608 data->g[0] = xyz_to_rgb[0][1];
609 data->g[1] = xyz_to_rgb[1][1];
610 data->g[2] = xyz_to_rgb[2][1];
611 data->b[0] = xyz_to_rgb[0][2];
612 data->b[1] = xyz_to_rgb[1][2];
613 data->b[2] = xyz_to_rgb[2][2];
614}
615
617{
618 static Set<UString> skip_socket_identifiers = {"Weight"_ustr};
619 nodes::search_filtered_link_ops_for_basic_node(params, skip_socket_identifiers);
620}
621
623{
624 static Set<UString> skip_socket_identifiers = {
625 "LightIndex"_ustr, "ShaderZoneIO"_ustr, "Weight"_ustr};
626 nodes::search_filtered_link_ops_for_basic_node(params, skip_socket_identifiers);
627}
628
629} // namespace blender
#define NODE_CLASS_OUTPUT
Definition BKE_node.hh:479
#define NODE_GROUP
Definition BKE_node.hh:865
#define MAX_SOCKET
Definition BKE_node.hh:30
#define NODE_CLASS_TEXTURE
Definition BKE_node.hh:488
#define SH_NODE_TEX_IMAGE
#define SH_NODE_ATTRIBUTE
#define STR_ELEM(...)
#define RPT_(msgid)
#define C
Definition RandGen.cpp:29
#define BLI_assert_unreachable()
#define ELEM(...)
#define STREQ(a, b)
btScalar max
#define FLT_MAX
bNode * node_get_active(bNodeTree &ntree)
Definition node.cc:5445
bNode * node_get_active_texture(bNodeTree &ntree)
void node_type_base(bNodeType &ntype, UString idname, std::optional< int16_t > legacy_type=std::nullopt)
Definition node.cc:5866
bNode * node_get_active_paint_canvas(bNodeTree &ntree)
bool node_supports_active_flag(const bNode &node, int sub_activity)
Does the given node supports the sub active flag.
void search_link_ops_for_basic_node(GatherLinkSearchOpParams &params)
void search_filtered_link_ops_for_basic_node(GatherLinkSearchOpParams &params, const Set< UString > &skip_socket_identifiers)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
@ NODE_ACTIVE_TEXTURE
@ NODE_ACTIVE_PAINT_CANVAS
@ SNODE_SHADER_LINESTYLE
void node_gpu_stack_from_data(GPUNodeStack *gs, bNodeSocket *socket, bNodeStack *ns)
bool object_shader_nodes_poll(const bContext *C)
MINLINE void copy_v3_v3(float r[3], const float a[3])
bool sh_node_poll_default(const bke::bNodeType *, const bNodeTree *ntree, const char **r_disabled_hint)
bool object_cycles_shader_nodes_poll(const bContext *C)
static void gpu_stack_from_data_list(GPUNodeStack *gs, ListBaseT< bNodeSocket > *sockets, bNodeStack **ns)
MINLINE void copy_v4_fl(float r[4], float f)
static void nodestack_get_vec(float *in, short type_in, bNodeStack *ns)
RenderEngineType * CTX_data_engine_type(const bContext *C)
void sh_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
static void data_from_gpu_stack_list(ListBaseT< bNodeSocket > *sockets, bNodeStack **ns, GPUNodeStack *gs)
GPUNodeLink * GPU_uniform(const float *num)
MINLINE bool is_zero_v4(const float v[4]) ATTR_WARN_UNUSED_RESULT
static bool sh_geo_poll_default(const bke::bNodeType *, const bNodeTree *ntree, const char **r_disabled_hint)
VecBase< float, 4 > float4
void node_data_from_gpu_stack(bNodeStack *ns, GPUNodeStack *gs)
bool line_style_shader_nodes_poll(const bContext *C)
void sh_geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
VecBase< float, 2 > float2
bool GPU_link(GPUMaterial *mat, const char *name,...)
void node_shader_gpu_tex_mapping(GPUMaterial *mat, bNode *node, GPUNodeStack *in, GPUNodeStack *)
BMesh const char void * data
void node_shader_gpu_default_tex_coord(GPUMaterial *mat, bNode *node, GPUNodeLink **link)
void search_link_ops_for_shader_bsdf_node(nodes::GatherLinkSearchOpParams &params)
void eul_to_quat(float quat[4], const float eul[3])
bool world_shader_nodes_poll(const bContext *C)
SpaceNode * CTX_wm_space_node(const bContext *C)
bool node_insert_link_default(bke::NodeInsertLinkParams &)
Definition node_util.cc:223
bool object_eevee_shader_nodes_poll(const bContext *C)
void get_XYZ_to_RGB_for_gpu(XYZ_to_RGB *data)
MatBase< float, 3, 3 > float3x3
void node_shader_gpu_bump_tex_coord(GPUMaterial *mat, bNode *, GPUNodeLink **link)
BLI_INLINE float IMB_colormanagement_get_luminance(const float rgb[3])
MINLINE void copy_v3_fl(float r[3], float f)
float3x3 IMB_colormanagement_get_xyz_to_scene_linear()
GPUNodeLink * GPU_attribute(GPUMaterial *mat, eCustomDataType type, const char *name)
MINLINE void copy_v4_v4(float r[4], const float a[4])
VecBase< float, 3 > float3
void search_link_ops_for_shader_material_lighting_node(nodes::GatherLinkSearchOpParams &params)
void node_get_stack(bNode *node, bNodeStack *stack, bNodeStack **in, bNodeStack **out)
Definition node_exec.cc:47
static bool common_poll_default(const bke::bNodeType *, const bNodeTree *ntree, const char **r_disabled_hint)
MINLINE bool is_zero_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
void ntreeExecGPUNodes(bNodeTreeExec *exec, GPUMaterial *mat, bNode *output_node, const int *depth_level)
void common_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
color xyz_to_rgb(float x, float y, float z)
Definition node_color.h:73
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38
GPUNodeStackValue value
eSpaceNode_ShaderFrom shaderfrom
eTexMapping_Type type
eTexMapping_Flag flag
bNodeExecData data
Definition node_exec.hh:28
eNodeSocketDatatype type
Defines a node type.
Definition BKE_node.hh:265
bool(*) poll(const bNodeType *ntype, const bNodeTree *nodetree, const char **r_disabled_hint)
Definition BKE_node.hh:352
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:409
bool(*) insert_link(NodeInsertLinkParams &params)
Definition BKE_node.hh:364