Blender V5.3
utilities.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
5#include <optional>
6
7#include "BLI_assert.hh"
8#include "BLI_math_vector.hh"
10
11#include "DNA_node_types.h"
12
13#include "BKE_node.hh"
14#include "BKE_node_runtime.hh"
15
17
18#include "GPU_compute.hh"
19#include "GPU_shader.hh"
20
21#include "COM_result.hh"
22#include "COM_utilities.hh"
23
24namespace blender::compositor {
25
27{
28 return socket->is_available() && StringRef(socket->idname) != "NodeSocketVirtual";
29}
30
32{
33 if (!input.is_logically_linked()) {
34 return nullptr;
35 }
36 return input.logically_linked_sockets()[0];
37}
38
40 const std::optional<int> dimensions)
41{
42 switch (data_type) {
43 case SOCK_FLOAT:
44 return ResultType::Float;
45 case SOCK_INT:
46 return ResultType::Int;
47 case SOCK_BOOLEAN:
48 return ResultType::Bool;
49 case SOCK_VECTOR:
50 switch (dimensions.value_or(3)) {
51 case 2:
52 return ResultType::Float2;
53 case 3:
54 return ResultType::Float3;
55 case 4:
56 return ResultType::Float4;
57 default:
59 return ResultType::Float;
60 }
61 case SOCK_INT_VECTOR:
62 switch (dimensions.value_or(2)) {
63 case 2:
64 return ResultType::Int2;
65 case 3:
66 return ResultType::Int3;
67 default:
69 return ResultType::Float;
70 }
71 case SOCK_RGBA:
72 return ResultType::Color;
73 case SOCK_MATRIX:
75 case SOCK_MENU:
76 return ResultType::Menu;
77 case SOCK_STRING:
78 return ResultType::String;
79 case SOCK_ROTATION:
81 case SOCK_OBJECT:
82 return ResultType::Object;
83 case SOCK_IMAGE:
84 return ResultType::Image;
85 case SOCK_FONT:
86 return ResultType::Font;
87 case SOCK_SCENE:
88 return ResultType::Scene;
89 case SOCK_TEXT_ID:
90 return ResultType::Text;
91 case SOCK_MASK:
92 return ResultType::Mask;
93 case SOCK_BUNDLE:
94 return ResultType::Bundle;
95 case SOCK_CLOSURE:
97 default:
99 return ResultType::Float;
100 }
101}
102
104{
105 /* Gracefully handle undefined sockets, falling back to a float. */
106 if (socket->typeinfo == &bke::NodeSocketTypeUndefined) {
107 return ResultType::Float;
108 }
109
110 const eNodeSocketDatatype socket_type = static_cast<eNodeSocketDatatype>(socket->type);
111 if (socket_type == SOCK_VECTOR) {
113 socket_type, socket->default_value_typed<bNodeSocketValueVector>()->dimensions);
114 }
115 if (socket_type == SOCK_INT_VECTOR) {
117 socket_type, socket->default_value_typed<bNodeSocketValueIntVector>()->dimensions);
118 }
119
120 return socket_data_type_to_result_type(socket_type);
121}
122
124{
125 /* Gracefully handle undefined interface sockets, falling back to a float. */
126 if (socket.socket_typeinfo() == &bke::NodeSocketTypeUndefined) {
127 return ResultType::Float;
128 }
129
130 const eNodeSocketDatatype socket_type = socket.socket_typeinfo()->type;
131 if (socket_type == SOCK_VECTOR) {
133 socket_type, static_cast<bNodeSocketValueVector *>(socket.socket_data)->dimensions);
134 }
135 if (socket_type == SOCK_INT_VECTOR) {
137 socket_type, static_cast<bNodeSocketValueIntVector *>(socket.socket_data)->dimensions);
138 }
139
140 return socket_data_type_to_result_type(socket_type);
141}
142
144 FunctionRef<bool(const bNodeSocket &)> condition)
145{
146 for (const bNodeSocket *input : output.logically_linked_sockets()) {
147 if (condition(*input)) {
148 return true;
149 }
150 }
151 return false;
152}
153
155{
156 return node.typeinfo->build_multi_function;
157}
158
183
184static int get_domain_priority(const bNodeSocket *input,
185 const nodes::SocketDeclaration *socket_declaration)
186{
187 /* Negative priority means no priority is set and we fall back to the index, that is, we
188 * prioritize inputs according to their order. */
189 if (socket_declaration->compositor_domain_priority() < 0) {
190 return input->index();
191 }
192 return socket_declaration->compositor_domain_priority();
193}
194
196{
197 InputDescriptor input_descriptor;
198 input_descriptor.type = get_node_socket_result_type(socket);
199
200 /* Default to the index of the input as its domain priority in case the node does not have a
201 * declaration. */
202 input_descriptor.domain_priority = socket->index();
203
204 /* Not every node has a declaration or the declaration might be empty due to a skipped update, in
205 * which case we assume the default values for the rest of the properties. */
206 const nodes::NodeDeclaration *node_declaration = socket->owner_node().declaration();
207 if (!node_declaration || node_declaration->skip_updating_sockets) {
208 return input_descriptor;
209 }
210 const nodes::SocketDeclaration *socket_declaration = node_declaration->inputs[socket->index()];
211 input_descriptor.domain_priority = get_domain_priority(socket, socket_declaration);
212 input_descriptor.expects_single_value = socket_declaration->structure_type ==
213 nodes::StructureType::Single;
214 input_descriptor.realization_mode = static_cast<InputRealizationMode>(
215 socket_declaration->compositor_realization_mode());
216 input_descriptor.implicit_input = get_implicit_input(socket_declaration->default_input_type);
217
218 return input_descriptor;
219}
220
222 const bNodeTreeInterfaceSocket &socket)
223{
224 InputDescriptor input_descriptor;
225 input_descriptor.type = get_node_interface_socket_result_type(socket);
226 input_descriptor.domain_priority = node_group.interface_input_index(socket);
227 input_descriptor.expects_single_value = socket.structure_type ==
230 input_descriptor.implicit_input = get_implicit_input(socket.default_input);
231
232 return input_descriptor;
233}
234
236{
237 /* If the threads range is divisible by the local size, dispatch the number of needed groups,
238 * which is their division. If it is not divisible, then dispatch an extra group to cover the
239 * remaining invocations, which means the actual threads range of the dispatch will be a bit
240 * larger than the given one. */
241 const int2 groups_to_dispatch = math::divide_ceil(threads_range, local_size);
242 GPU_compute_dispatch(shader, groups_to_dispatch.x, groups_to_dispatch.y, 1);
243}
244
246{
247 if (!(node.flag & NODE_PREVIEW)) {
248 return false;
249 }
250
251 if (node.flag & NODE_COLLAPSED) {
252 return false;
253 }
254
255 return true;
256}
257
259{
261 return nullptr;
262 }
263
264 for (const bNodeSocket *output : node.output_sockets()) {
265 if (is_socket_available(output) && output->is_logically_linked()) {
266 return output;
267 }
268 }
269
270 return nullptr;
271}
272
273} // namespace blender::compositor
#define BLI_assert_unreachable()
#define output
constexpr StringRef()
std::optional< ImplicitInputType > implicit_input
Vector< SocketDeclaration * > inputs
const CompositorInputRealizationMode & compositor_realization_mode() const
#define condition(cond)
#define local_size(...)
bNodeSocketType NodeSocketTypeUndefined
Definition node.cc:135
ResultType socket_data_type_to_result_type(const eNodeSocketDatatype data_type, const std::optional< int > dimensions=std::nullopt)
Definition utilities.cc:39
void compute_dispatch_threads_at_least(gpu::Shader *shader, int2 threads_range, int2 local_size=int2(16))
Definition utilities.cc:235
const bNodeSocket * find_preview_output_socket(const bNode &node)
Definition utilities.cc:258
bool is_node_preview_needed(const bNode &node)
Definition utilities.cc:245
ResultType get_node_interface_socket_result_type(const bNodeTreeInterfaceSocket &socket)
Definition utilities.cc:123
const bNodeSocket * get_output_linked_to_input(const bNodeSocket &input)
Definition utilities.cc:31
static std::optional< ImplicitInputType > get_implicit_input(const NodeDefaultInputType node_default_input_type)
Definition utilities.cc:159
bool is_output_linked_to_input_conditioned(const bNodeSocket &output, FunctionRef< bool(const bNodeSocket &)> condition)
Definition utilities.cc:143
static int get_domain_priority(const bNodeSocket *input, const nodes::SocketDeclaration *socket_declaration)
Definition utilities.cc:184
InputDescriptor input_descriptor_from_interface_input(const bNodeTree &node_group, const bNodeTreeInterfaceSocket &socket)
Definition utilities.cc:221
ResultType get_node_socket_result_type(const bNodeSocket *socket)
Definition utilities.cc:103
bool is_pixel_node(const bNode &node)
Definition utilities.cc:154
InputDescriptor input_descriptor_from_input_socket(const bNodeSocket *socket)
Definition utilities.cc:195
bool is_socket_available(const bNodeSocket *socket)
Definition utilities.cc:26
VecBase< T, Size > divide_ceil(const VecBase< T, Size > &a, const VecBase< T, Size > &b)
void GPU_compute_dispatch(gpu::Shader *shader, uint groups_x_len, uint groups_y_len, uint groups_z_len, const gpu::shader::SpecializationConstants *constants_state=nullptr)
bke::bNodeSocketType * typeinfo
eNodeSocketDatatype type
NodeSocketInterfaceStructureType structure_type