Blender V4.5
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
14#include "BLI_math_vector.h"
15#include "BLI_string.h"
16
17#include "BKE_context.hh"
18#include "BKE_node_runtime.hh"
19
21
22#include "node_shader_util.hh"
23
25
26#include "RE_engine.h"
27
28#include "node_exec.hh"
29
31 const bNodeTree *ntree,
32 const char **r_disabled_hint)
33{
34 if (!STREQ(ntree->idname, "ShaderNodeTree")) {
35 *r_disabled_hint = RPT_("Not a shader node tree");
36 return false;
37 }
38 return true;
39}
40
41static bool common_poll_default(const blender::bke::bNodeType * /*ntype*/,
42 const bNodeTree *ntree,
43 const char **r_disabled_hint)
44{
45 if (!STR_ELEM(ntree->idname, "ShaderNodeTree", "GeometryNodeTree", "CompositorNodeTree")) {
46 *r_disabled_hint = RPT_("Not a shader, geometry, or compositor node tree");
47 return false;
48 }
49 return true;
50}
51
53 std::string idname,
54 const std::optional<int16_t> legacy_type)
55{
56 blender::bke::node_type_base(*ntype, idname, legacy_type);
57
61}
62
64 std::string idname,
65 const std::optional<int16_t> legacy_type)
66{
67 sh_node_type_base(ntype, idname, legacy_type);
70}
71
73{
74 const SpaceNode *snode = CTX_wm_space_node(C);
75 return snode->shaderfrom == SNODE_SHADER_LINESTYLE;
76}
77
79{
80 const SpaceNode *snode = CTX_wm_space_node(C);
81 return snode->shaderfrom == SNODE_SHADER_WORLD;
82}
83
85{
86 const SpaceNode *snode = CTX_wm_space_node(C);
87 return snode->shaderfrom == SNODE_SHADER_OBJECT;
88}
89
91{
93 return false;
94 }
95 const RenderEngineType *engine_type = CTX_data_engine_type(C);
96 return STREQ(engine_type->idname, "CYCLES");
97}
98
100{
102 return false;
103 }
104 const RenderEngineType *engine_type = CTX_data_engine_type(C);
105 return STREQ(engine_type->idname, "BLENDER_EEVEE") ||
106 STREQ(engine_type->idname, "BLENDER_EEVEE_NEXT");
107}
108
109/* ****** */
110
111static void nodestack_get_vec(float *in, short type_in, bNodeStack *ns)
112{
113 const float *from = ns->vec;
114
115 if (type_in == SOCK_FLOAT) {
116 if (ns->sockettype == SOCK_FLOAT) {
117 *in = *from;
118 }
119 else {
120 *in = (from[0] + from[1] + from[2]) / 3.0f;
121 }
122 }
123 else if (type_in == SOCK_VECTOR) {
124 if (ns->sockettype == SOCK_FLOAT) {
125 in[0] = from[0];
126 in[1] = from[0];
127 in[2] = from[0];
128 }
129 else {
130 copy_v3_v3(in, from);
131 }
132 }
133 else { /* type_in==SOCK_RGBA */
134 if (ns->sockettype == SOCK_RGBA) {
135 copy_v4_v4(in, from);
136 }
137 else if (ns->sockettype == SOCK_FLOAT) {
138 in[0] = from[0];
139 in[1] = from[0];
140 in[2] = from[0];
141 in[3] = 1.0f;
142 }
143 else {
144 copy_v3_v3(in, from);
145 in[3] = 1.0f;
146 }
147 }
148}
149
151{
152 memset(gs, 0, sizeof(*gs));
153
154 if (ns == nullptr) {
155 /* node_get_stack() will generate nullptr bNodeStack pointers
156 * for unknown/unsupported types of sockets. */
157 zero_v4(gs->vec);
158 gs->link = nullptr;
159 gs->type = GPU_NONE;
160 gs->hasinput = false;
161 gs->hasoutput = false;
162 gs->sockettype = type;
163 }
164 else {
165 nodestack_get_vec(gs->vec, type, ns);
166 gs->link = (GPUNodeLink *)ns->data;
167
168 if (type == SOCK_FLOAT) {
169 gs->type = GPU_FLOAT;
170 }
171 else if (type == SOCK_INT) {
172 gs->type = GPU_FLOAT; /* HACK: Support as float. */
173 }
174 else if (type == SOCK_BOOLEAN) {
175 gs->type = GPU_FLOAT; /* HACK: Support as float. */
176 }
177 else if (type == SOCK_VECTOR) {
178 gs->type = GPU_VEC3;
179 }
180 else if (type == SOCK_RGBA) {
181 gs->type = GPU_VEC4;
182 }
183 else if (type == SOCK_SHADER) {
184 gs->type = GPU_CLOSURE;
185 }
186 else {
187 gs->type = GPU_NONE;
188 }
189
190 gs->hasinput = ns->hasinput && ns->data;
191 /* XXX Commented out the ns->data check here, as it seems it's not always set,
192 * even though there *is* a valid connection/output... But that might need
193 * further investigation.
194 */
195 gs->hasoutput = ns->hasoutput /*&& ns->data*/;
196 gs->sockettype = ns->sockettype;
197 }
198}
199
201{
202 copy_v4_v4(ns->vec, gs->vec);
203 ns->data = gs->link;
204 ns->sockettype = gs->sockettype;
205}
206
208{
209 int i;
210 LISTBASE_FOREACH_INDEX (bNodeSocket *, socket, sockets, i) {
211 node_gpu_stack_from_data(&gs[i], socket->type, ns[i]);
212 }
213
214 gs[i].end = true;
215}
216
218{
219 int i = 0;
220 LISTBASE_FOREACH (bNodeSocket *, socket, sockets) {
221 if (ELEM(
223 {
224 node_data_from_gpu_stack(ns[i], &gs[i]);
225 i++;
226 }
227 }
228}
229
230bool blender::bke::node_supports_active_flag(const bNode &node, int sub_activity)
231{
233 switch (sub_activity) {
235 return node.typeinfo->nclass == NODE_CLASS_TEXTURE;
238 }
239 return false;
240}
241
242static bNode *node_get_active(bNodeTree *ntree, int sub_activity)
243{
245 /* this is the node we texture paint and draw in textured draw */
246 bNode *inactivenode = nullptr, *activetexnode = nullptr, *activegroup = nullptr;
247 bool hasgroup = false;
248
249 if (!ntree) {
250 return nullptr;
251 }
252
253 for (bNode *node : ntree->all_nodes()) {
254 if (node->flag & sub_activity) {
255 activetexnode = node;
256 /* if active we can return immediately */
257 if (node->flag & NODE_ACTIVE) {
258 return node;
259 }
260 }
261 else if (!inactivenode && blender::bke::node_supports_active_flag(*node, sub_activity)) {
262 inactivenode = node;
263 }
264 else if (node->type_legacy == NODE_GROUP) {
265 if (node->flag & NODE_ACTIVE) {
266 activegroup = node;
267 }
268 else {
269 hasgroup = true;
270 }
271 }
272 }
273
274 /* first, check active group for textures */
275 if (activegroup) {
276 bNode *tnode = node_get_active((bNodeTree *)activegroup->id, sub_activity);
277 /* active node takes priority, so ignore any other possible nodes here */
278 if (tnode) {
279 return tnode;
280 }
281 }
282
283 if (activetexnode) {
284 return activetexnode;
285 }
286
287 if (hasgroup) {
288 /* node active texture node in this tree, look inside groups */
289 for (bNode *node : ntree->all_nodes()) {
290 if (node->type_legacy == NODE_GROUP) {
291 bNode *tnode = node_get_active((bNodeTree *)node->id, sub_activity);
292 if (tnode && ((tnode->flag & sub_activity) || !inactivenode)) {
293 return tnode;
294 }
295 }
296 }
297 }
298
299 return inactivenode;
300}
301
302namespace blender::bke {
303
308
313} // namespace blender::bke
314
316 GPUMaterial *mat,
317 bNode *output_node,
318 const int *depth_level)
319{
320 bNodeExec *nodeexec;
321 bNode *node;
322 int n;
323 bNodeStack *stack;
324 bNodeStack *nsin[MAX_SOCKET]; /* arbitrary... watch this */
325 bNodeStack *nsout[MAX_SOCKET]; /* arbitrary... watch this */
326 GPUNodeStack gpuin[MAX_SOCKET + 1], gpuout[MAX_SOCKET + 1];
327 bool do_it;
328
329 stack = exec->stack;
330
331 for (n = 0, nodeexec = exec->nodeexec; n < exec->totnodes; n++, nodeexec++) {
332 node = nodeexec->node;
333
334 if (depth_level && node->runtime->tmp_flag != *depth_level) {
335 continue;
336 }
337
338 do_it = false;
339 /* for groups, only execute outputs for edited group */
340 if (node->typeinfo->nclass == NODE_CLASS_OUTPUT) {
341 if ((output_node != nullptr) && (node == output_node)) {
342 do_it = true;
343 }
344 }
345 else {
346 do_it = node->runtime->need_exec;
347 node->runtime->need_exec = 0;
348 }
349
350 if (do_it) {
351 BLI_assert(!depth_level || node->runtime->tmp_flag >= 0);
352 if (node->typeinfo->gpu_fn) {
353 node_get_stack(node, stack, nsin, nsout);
354 gpu_stack_from_data_list(gpuin, &node->inputs, nsin);
355 gpu_stack_from_data_list(gpuout, &node->outputs, nsout);
356 if (node->typeinfo->gpu_fn(mat, node, &nodeexec->data, gpuin, gpuout)) {
357 data_from_gpu_stack_list(&node->outputs, nsout, gpuout);
358 }
359 }
360 }
361 }
362}
363
365{
366 GPU_link(mat, "differentiate_texco", *link, link);
367}
368
370{
371 if (!*link) {
372 *link = GPU_attribute(mat, CD_ORCO, "");
373 node_shader_gpu_bump_tex_coord(mat, node, link);
374 }
375}
376
378 bNode *node,
380 GPUNodeStack * /*out*/)
381{
382 NodeTexBase *base = (NodeTexBase *)node->storage;
383 TexMapping *texmap = &base->tex_mapping;
384 float domin = (texmap->flag & TEXMAP_CLIP_MIN) != 0;
385 float domax = (texmap->flag & TEXMAP_CLIP_MAX) != 0;
386
387 if (domin || domax || !(texmap->flag & TEXMAP_UNIT_MATRIX)) {
388 static float max[3] = {FLT_MAX, FLT_MAX, FLT_MAX};
389 static float min[3] = {-FLT_MAX, -FLT_MAX, -FLT_MAX};
390 GPUNodeLink *tmin, *tmax, *tmat0, *tmat1, *tmat2, *tmat3;
391
392 tmin = GPU_uniform((domin) ? texmap->min : min);
393 tmax = GPU_uniform((domax) ? texmap->max : max);
394 tmat0 = GPU_uniform((float *)texmap->mat[0]);
395 tmat1 = GPU_uniform((float *)texmap->mat[1]);
396 tmat2 = GPU_uniform((float *)texmap->mat[2]);
397 tmat3 = GPU_uniform((float *)texmap->mat[3]);
398
399 GPU_link(mat, "mapping_mat4", in[0].link, tmat0, tmat1, tmat2, tmat3, tmin, tmax, &in[0].link);
400
401 if (texmap->type == TEXMAP_TYPE_NORMAL) {
402 GPU_link(mat, "vector_normalize", in[0].link, &in[0].link);
403 }
404 }
405}
406
408{
410 data->r[0] = xyz_to_rgb[0][0];
411 data->r[1] = xyz_to_rgb[1][0];
412 data->r[2] = xyz_to_rgb[2][0];
413 data->g[0] = xyz_to_rgb[0][1];
414 data->g[1] = xyz_to_rgb[1][1];
415 data->g[2] = xyz_to_rgb[2][1];
416 data->b[0] = xyz_to_rgb[0][2];
417 data->b[1] = xyz_to_rgb[1][2];
418 data->b[2] = xyz_to_rgb[2][2];
419}
420
422{
423 return socket.link || socket.vec[0] > 1e-5f;
424}
426{
427 return socket.link || socket.vec[0] < 1.0f || socket.vec[1] < 1.0f || socket.vec[2] < 1.0f;
428}
430{
431 return socket.link || socket.vec[0] > 1e-5f || socket.vec[1] > 1e-5f || socket.vec[2] > 1e-5f;
432}
SpaceNode * CTX_wm_space_node(const bContext *C)
RenderEngineType * CTX_data_engine_type(const bContext *C)
#define NODE_CLASS_OUTPUT
Definition BKE_node.hh:434
#define NODE_GROUP
Definition BKE_node.hh:796
#define MAX_SOCKET
Definition BKE_node.hh:27
#define NODE_CLASS_TEXTURE
Definition BKE_node.hh:443
#define SH_NODE_TEX_IMAGE
#define SH_NODE_ATTRIBUTE
#define BLI_assert(a)
Definition BLI_assert.h:46
#define LISTBASE_FOREACH(type, var, list)
#define LISTBASE_FOREACH_INDEX(type, var, list, index_var)
MINLINE void copy_v4_v4(float r[4], const float a[4])
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE void zero_v4(float r[4])
#define STR_ELEM(...)
Definition BLI_string.h:656
#define ELEM(...)
#define STREQ(a, b)
#define RPT_(msgid)
@ NODE_ACTIVE
@ NODE_ACTIVE_TEXTURE
@ NODE_ACTIVE_PAINT_CANVAS
@ SOCK_INT
@ SOCK_VECTOR
@ SOCK_BOOLEAN
@ SOCK_SHADER
@ SOCK_FLOAT
@ SOCK_RGBA
@ SNODE_SHADER_WORLD
@ SNODE_SHADER_LINESTYLE
@ SNODE_SHADER_OBJECT
@ TEXMAP_TYPE_NORMAL
@ TEXMAP_CLIP_MIN
@ TEXMAP_UNIT_MATRIX
@ TEXMAP_CLIP_MAX
GPUNodeLink * GPU_attribute(GPUMaterial *mat, eCustomDataType type, const char *name)
@ GPU_VEC4
@ GPU_NONE
@ GPU_CLOSURE
@ GPU_VEC3
@ GPU_FLOAT
bool GPU_link(GPUMaterial *mat, const char *name,...)
GPUNodeLink * GPU_uniform(const float *num)
blender::float3x3 IMB_colormanagement_get_xyz_to_scene_linear()
#define C
Definition RandGen.cpp:29
BMesh const char void * data
#define in
bNode * node_get_active(bNodeTree &ntree)
Definition node.cc:4957
bNode * node_get_active_texture(bNodeTree &ntree)
void node_type_base(bNodeType &ntype, std::string idname, std::optional< int16_t > legacy_type=std::nullopt)
Definition node.cc:5310
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)
MatBase< float, 3, 3 > float3x3
color xyz_to_rgb(float x, float y, float z)
Definition node_color.h:73
void node_get_stack(bNode *node, bNodeStack *stack, bNodeStack **in, bNodeStack **out)
Definition node_exec.cc:37
bool line_style_shader_nodes_poll(const bContext *C)
void node_shader_gpu_tex_mapping(GPUMaterial *mat, bNode *node, GPUNodeStack *in, GPUNodeStack *)
void node_shader_gpu_default_tex_coord(GPUMaterial *mat, bNode *node, GPUNodeLink **link)
static bool common_poll_default(const blender::bke::bNodeType *, const bNodeTree *ntree, const char **r_disabled_hint)
void sh_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
void node_data_from_gpu_stack(bNodeStack *ns, GPUNodeStack *gs)
bool node_socket_not_white(const GPUNodeStack &socket)
bool object_cycles_shader_nodes_poll(const bContext *C)
static void nodestack_get_vec(float *in, short type_in, bNodeStack *ns)
void get_XYZ_to_RGB_for_gpu(XYZ_to_RGB *data)
bool object_shader_nodes_poll(const bContext *C)
bool object_eevee_shader_nodes_poll(const bContext *C)
bool node_socket_not_black(const GPUNodeStack &socket)
bool world_shader_nodes_poll(const bContext *C)
static bNode * node_get_active(bNodeTree *ntree, int sub_activity)
bool node_socket_not_zero(const GPUNodeStack &socket)
static void gpu_stack_from_data_list(GPUNodeStack *gs, ListBase *sockets, bNodeStack **ns)
void common_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
void node_gpu_stack_from_data(GPUNodeStack *gs, int type, bNodeStack *ns)
void ntreeExecGPUNodes(bNodeTreeExec *exec, GPUMaterial *mat, bNode *output_node, const int *depth_level)
static void data_from_gpu_stack_list(ListBase *sockets, bNodeStack **ns, GPUNodeStack *gs)
bool sh_node_poll_default(const blender::bke::bNodeType *, const bNodeTree *ntree, const char **r_disabled_hint)
void node_shader_gpu_bump_tex_coord(GPUMaterial *mat, bNode *, GPUNodeLink **link)
static void exec(void *data, int, bNode *node, bNodeExecData *execdata, bNodeStack **in, bNodeStack **out)
bool node_insert_link_default(bNodeTree *, bNode *, bNodeLink *)
Definition node_util.cc:279
#define min(a, b)
Definition sort.cc:36
#define FLT_MAX
Definition stdcycles.h:14
GPUNodeLink * link
TexMapping tex_mapping
char idname[64]
Definition RE_engine.h:74
float mat[4][4]
bNodeExecData data
Definition node_exec.hh:25
bNode * node
Definition node_exec.hh:24
float vec[4]
char idname[64]
bNodeTypeHandle * typeinfo
ListBase inputs
int16_t type_legacy
bNodeRuntimeHandle * runtime
void * storage
ListBase outputs
Defines a node type.
Definition BKE_node.hh:226
bool(* poll)(const bNodeType *ntype, const bNodeTree *nodetree, const char **r_disabled_hint)
Definition BKE_node.hh:309
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:371
bool(* insert_link)(bNodeTree *ntree, bNode *node, bNodeLink *link)
Definition BKE_node.hh:321
i
Definition text_draw.cc:230
max
Definition text_draw.cc:251