Blender V5.3
shader_node.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6#include "BLI_string_ref.hh"
7
8#include "DNA_node_types.h"
9
10#include "BKE_node.hh"
11#include "BKE_node_runtime.hh"
12
13#include "GPU_material.hh"
14
15#include "COM_shader_node.hh"
16#include "COM_utilities.hh"
17
18namespace blender::compositor {
19
21{
22 this->populate_inputs();
23 this->populate_outputs();
24}
25
27{
28 node_.typeinfo->gpu_fn(
29 material, const_cast<bNode *>(&node_), nullptr, inputs_.data(), outputs_.data());
30}
31
33{
34 return GPU_node_get_input(node_, inputs_.data(), identifier);
35}
36
38{
39 return GPU_node_get_output(node_, outputs_.data(), identifier);
40}
41
43{
44 switch (socket.type) {
45 case SOCK_FLOAT:
46 return GPU_FLOAT;
47 case SOCK_INT:
48 return GPU_INT;
49 case SOCK_BOOLEAN:
50 return GPU_BOOL;
51 case SOCK_VECTOR:
52 switch (socket.default_value_typed<bNodeSocketValueVector>()->dimensions) {
53 case 2:
54 return GPU_VEC2;
55 case 3:
56 return GPU_VEC3;
57 case 4:
58 return GPU_VEC4;
59 default:
61 return GPU_NONE;
62 }
63 case SOCK_INT_VECTOR:
64 switch (socket.default_value_typed<bNodeSocketValueIntVector>()->dimensions) {
65 case 2:
66 return GPU_INT2;
67 case 3:
68 return GPU_INT3;
69 default:
71 return GPU_NONE;
72 }
73 case SOCK_RGBA:
74 case SOCK_ROTATION:
75 return GPU_VEC4;
76 case SOCK_MATRIX:
77 return GPU_MAT4;
78 case SOCK_MENU:
79 return GPU_INT;
80 case SOCK_STRING:
81 case SOCK_OBJECT:
82 case SOCK_IMAGE:
83 case SOCK_FONT:
84 case SOCK_SCENE:
85 case SOCK_TEXT_ID:
86 case SOCK_MASK:
87 case SOCK_BUNDLE:
88 case SOCK_CLOSURE:
89 /* Single only types do not support GPU code path. */
92 return GPU_NONE;
93 default:
94 /* The GPU material compiler will skip unsupported sockets if GPU_NONE is provided. So this
95 * is an appropriate and a valid type for unsupported sockets. */
96 return GPU_NONE;
97 }
98}
99
100static void populate_gpu_node_stack(const bNodeSocket &socket, GPUNodeStack &stack)
101{
102 /* Make sure this stack is not marked as the end of the stack array. */
103 stack.end = false;
104 /* This will be initialized later by the GPU material compiler or the compile method. */
105 stack.link = nullptr;
106
107 stack.sockettype = socket.type;
108 stack.type = gpu_type_from_socket(socket);
109
110 /* This will be initialized by the GPU material compiler if needed. */
112
113 stack.hasinput = socket.is_logically_linked();
114 stack.hasoutput = socket.is_logically_linked();
115}
116
117void ShaderNode::populate_inputs()
118{
119 /* Reserve a stack for each input in addition to an extra stack at the end to mark the end of the
120 * array, as this is what the GPU module functions expect. */
121 const int num_input_sockets = node_.input_sockets().size();
122 inputs_.resize(num_input_sockets + 1);
123 inputs_.last().end = true;
124
125 for (int i = 0; i < num_input_sockets; i++) {
126 populate_gpu_node_stack(node_.input_socket(i), inputs_[i]);
127 }
128}
129
130void ShaderNode::populate_outputs()
131{
132 /* Reserve a stack for each output in addition to an extra stack at the end to mark the end of
133 * the array, as this is what the GPU module functions expect. */
134 const int num_output_sockets = node_.output_sockets().size();
135 outputs_.resize(num_output_sockets + 1);
136 outputs_.last().end = true;
137
138 for (int i = 0; i < num_output_sockets; i++) {
139 populate_gpu_node_stack(node_.output_socket(i), outputs_[i]);
140 }
141}
142
143} // namespace blender::compositor
#define BLI_assert_unreachable()
ShaderNode(const NodeType *type)
virtual void compile(SVMCompiler &compiler)=0
static bool is_single_value_only_type(ResultType type)
Definition result.cc:51
GPUNodeStack & get_input(StringRef identifier)
GPUNodeStack & get_output(StringRef identifier)
static void populate_gpu_node_stack(const bNodeSocket &socket, GPUNodeStack &stack)
ResultType get_node_socket_result_type(const bNodeSocket *socket)
Definition utilities.cc:103
static GPUType gpu_type_from_socket(const bNodeSocket &socket)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
GPUNodeStack & GPU_node_get_input(const bNode &node, GPUNodeStack inputs[], StringRef identifier)
GPUNodeStackValue GPU_node_stack_default_value(const GPUType type)
GPUNodeStack & GPU_node_get_output(const bNode &node, GPUNodeStack outputs[], StringRef identifier)
GPUNodeStackValue value
eNodeSocketDatatype type