Blender V5.3
node_composite_relative_to_pixel.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_assert.hh"
6#include "BLI_math_vector.hh"
8
10#include "UI_resources.hh"
11
12#include "NOD_rna_define.hh"
13
14#include "COM_node_operation.hh"
16#include "COM_utilities.hh"
17
19
21
23{
24 b.add_input<decl::Vector>("Value"_ustr, "Vector Value"_ustr)
25 .subtype(PROP_FACTOR)
26 .dimensions(2)
27 .default_value({0.0f, 0.0f})
28 .min(0.0f)
29 .max(1.0f)
30 .structure_type(StructureType::Dynamic)
31 .description(
32 "A value that is relative to the image size and needs to be converted to be in pixels");
33 b.add_input<decl::Float>("Value"_ustr, "Float Value"_ustr)
34 .default_value(0.0f)
36 .min(0.0f)
37 .max(1.0f)
38 .structure_type(StructureType::Dynamic)
39 .description(
40 "A value that is relative to the image size and needs to be converted to be in pixels");
41 b.add_input<decl::Color>("Image"_ustr)
42 .compositor_realization_mode(CompositorInputRealizationMode::None)
43 .structure_type(StructureType::Dynamic);
44
45 b.add_output<decl::Float>("Value"_ustr, "Float Value"_ustr)
46 .structure_type(StructureType::Dynamic);
47 b.add_output<decl::Vector>("Value"_ustr, "Vector Value"_ustr)
48 .dimensions(2)
49 .structure_type(StructureType::Dynamic);
50}
51
57
58static void node_update(bNodeTree *ntree, bNode *node)
59{
60 const auto data_type = CMPNodeRelativeToPixelDataType(node->custom1);
61 const auto reference_dimension = CMPNodeRelativeToPixelReferenceDimension(node->custom2);
62
63 bNodeSocket *float_input = bke::node_find_socket(*node, SOCK_IN, "Float Value"_ustr);
65 *ntree, *float_input, data_type == CMP_NODE_RELATIVE_TO_PIXEL_DATA_TYPE_FLOAT);
66
67 bNodeSocket *vector_input = bke::node_find_socket(*node, SOCK_IN, "Vector Value"_ustr);
69 *ntree, *vector_input, data_type == CMP_NODE_RELATIVE_TO_PIXEL_DATA_TYPE_VECTOR);
70
71 /* The float output doesn't exist if the reference is per dimension, since each dimension can be
72 * different. */
73 const bool is_per_dimension = reference_dimension ==
75 bNodeSocket *float_output = bke::node_find_socket(*node, SOCK_OUT, "Float Value"_ustr);
77 *float_output,
79 !is_per_dimension);
80
81 /* The vector output exist if the reference is per dimension even if the data type is float,
82 * since each dimension can be different. */
83 bNodeSocket *vector_output = bke::node_find_socket(*node, SOCK_OUT, "Vector Value"_ustr);
85 *vector_output,
87 is_per_dimension);
88}
89
90static void node_rna(StructRNA *srna)
91{
92 static const EnumPropertyItem data_type_items[] = {
93 {CMP_NODE_RELATIVE_TO_PIXEL_DATA_TYPE_FLOAT, "FLOAT", ICON_NONE, "Float", "Float value"},
94 {CMP_NODE_RELATIVE_TO_PIXEL_DATA_TYPE_VECTOR, "VECTOR", ICON_NONE, "Vector", "Vector value"},
95 {0, nullptr, 0, nullptr, nullptr},
96 };
97
99 "data_type",
100 "Data Type",
101 "The type of data",
102 data_type_items,
105 nullptr,
106 true);
107
108 static const EnumPropertyItem reference_dimension_items[] = {
110 "PER_DIMENSION",
111 ICON_NONE,
112 "Per Dimension",
113 "The value is relative to each of the dimensions of the image independently"},
115 "X",
116 ICON_NONE,
117 "X",
118 "The value is relative to the X dimension of the image"},
120 "Y",
121 ICON_NONE,
122 "Y",
123 "The value is relative to the Y dimension of the image"},
125 "Greater",
126 ICON_NONE,
127 "Greater",
128 "The value is relative to the greater dimension of the image"},
130 "Smaller",
131 ICON_NONE,
132 "Smaller",
133 "The value is relative to the smaller dimension of the image"},
135 "Diagonal",
136 ICON_NONE,
137 "Diagonal",
138 "The value is relative to the diagonal of the image"},
139 {0, nullptr, 0, nullptr, nullptr},
140 };
141
143 srna,
144 "reference_dimension",
145 "Reference Dimension",
146 "Defines the dimension of the image that the relative value is in reference to",
147 reference_dimension_items,
150 nullptr,
151 true);
152}
153
154static void node_draw_buttons(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
155{
156 layout.prop(ptr, "data_type", UI_ITEM_NONE, "", ICON_NONE);
157 layout.prop(ptr, "reference_dimension", UI_ITEM_NONE, "", ICON_NONE);
158}
159
160using namespace blender::compositor;
161
163 public:
165 {
166 InputDescriptor &image_descriptor = this->get_input_descriptor("Image");
167 image_descriptor.skip_type_conversion = true;
168 }
169
170 void execute() override
171 {
173 const Result &input = this->get_input("Float Value");
174 if (this->get_reference_dimension() !=
176 {
177 if (input.is_single_value()) {
178 this->execute_float_single();
179 }
180 else {
181 this->execute_float();
182 }
183 }
184 else {
185 if (input.is_single_value()) {
187 }
188 else {
190 }
191 }
192 }
193 else {
194 const Result &input = this->get_input("Vector Value");
195 if (input.is_single_value()) {
196 this->execute_vector_single();
197 }
198 else {
199 this->execute_vector();
200 }
201 }
202 }
203
205 {
206 const float input_value = this->get_input("Float Value").get_single_value_default<float>();
207 const float2 reference_size = this->compute_reference_size();
208
209 const float value_in_pixels = input_value * reference_size.x;
210
211 Result &output = this->get_result("Float Value");
212 output.allocate_single_value();
213 output.set_single_value(value_in_pixels);
214 }
215
217 {
218 if (this->context().use_gpu()) {
219 this->execute_float_gpu();
220 }
221 else {
222 this->execute_float_cpu();
223 }
224 }
225
227 {
228 gpu::Shader *shader = context().get_shader("compositor_relative_to_pixel_float");
230
231 const float reference_size = this->compute_reference_size().x;
232 GPU_shader_uniform_1f(shader, "reference_size", reference_size);
233
234 const Result &input = this->get_input("Float Value");
235 input.bind_as_texture(shader, "input_tx");
236
237 Result &output = this->get_result("Float Value");
238 output.allocate_texture(input.domain());
239 output.bind_as_image(shader, "output_img");
240
242
243 input.unbind_as_texture();
244 output.unbind_as_image();
246 }
247
249 {
250 const Result &input = this->get_input("Float Value");
251 const float reference_size = this->compute_reference_size().x;
252
253 Result &output = this->get_result("Float Value");
254 output.allocate_texture(input.domain());
255 parallel_for(input.domain().data_size, [&](const int2 texel) {
256 output.store_pixel(texel, input.load_pixel<float>(texel) * reference_size);
257 });
258 }
259
261 {
262 const float input_value = this->get_input("Float Value").get_single_value_default<float>();
263 const float2 reference_size = this->compute_reference_size();
264
265 const float2 value_in_pixels = input_value * reference_size;
266
267 Result &output = this->get_result("Vector Value");
268 output.allocate_single_value();
269 output.set_single_value(value_in_pixels);
270 }
271
273 {
274 if (this->context().use_gpu()) {
276 }
277 else {
279 }
280 }
281
283 {
284 gpu::Shader *shader = context().get_shader("compositor_relative_to_pixel_float_per_dimension");
286
287 const float2 reference_size = this->compute_reference_size();
288 GPU_shader_uniform_2fv(shader, "reference_size", reference_size);
289
290 const Result &input = this->get_input("Float Value");
291 input.bind_as_texture(shader, "input_tx");
292
293 Result &output = this->get_result("Vector Value");
294 output.allocate_texture(input.domain());
295 output.bind_as_image(shader, "output_img");
296
298
299 input.unbind_as_texture();
300 output.unbind_as_image();
302 }
303
305 {
306 const Result &input = this->get_input("Float Value");
307 const float2 reference_size = this->compute_reference_size();
308
309 Result &output = this->get_result("Vector Value");
310 output.allocate_texture(input.domain());
311 parallel_for(input.domain().data_size, [&](const int2 texel) {
312 output.store_pixel(texel, input.load_pixel<float>(texel) * reference_size);
313 });
314 }
315
317 {
318 const float2 input_value = this->get_input("Vector Value").get_single_value_default<float2>();
319 const float2 reference_size = this->compute_reference_size();
320
321 const float2 value_in_pixels = input_value * reference_size;
322
323 Result &output = this->get_result("Vector Value");
324 output.allocate_single_value();
325 output.set_single_value(value_in_pixels);
326 }
327
329 {
330 if (this->context().use_gpu()) {
331 this->execute_vector_gpu();
332 }
333 else {
334 this->execute_vector_cpu();
335 }
336 }
337
339 {
340 gpu::Shader *shader = context().get_shader("compositor_relative_to_pixel_vector");
342
343 const float2 reference_size = this->compute_reference_size();
344 GPU_shader_uniform_2fv(shader, "reference_size", reference_size);
345
346 const Result &input = this->get_input("Vector Value");
347 input.bind_as_texture(shader, "input_tx");
348
349 Result &output = this->get_result("Vector Value");
350 output.allocate_texture(input.domain());
351 output.bind_as_image(shader, "output_img");
352
354
355 input.unbind_as_texture();
356 output.unbind_as_image();
358 }
359
361 {
362 const Result &input = this->get_input("Vector Value");
363 const float2 reference_size = this->compute_reference_size();
364
365 Result &output = this->get_result("Vector Value");
366 output.allocate_texture(input.domain());
367 parallel_for(input.domain().data_size, [&](const int2 texel) {
368 output.store_pixel(texel, input.load_pixel<float2>(texel) * reference_size);
369 });
370 }
371
373 {
374 const Result &input_image = this->get_input("Image");
375 if (input_image.is_single_value()) {
376 return float2(1.0f);
377 }
378
379 const Domain domain = input_image.domain().realize_transformation();
380 const float2 image_size = float2(domain.display_size);
381 switch (this->get_reference_dimension()) {
383 return image_size;
385 return float2(image_size.x);
387 return float2(image_size.y);
389 return float2(math::reduce_max(image_size));
391 return float2(math::reduce_min(image_size));
393 return float2(math::length(image_size));
394 }
395
397 return float2(1.0f);
398 }
399
404
409};
410
412{
413 return new RelativeToPixelOperation(context, node);
414}
415
416static void node_register()
417{
418 static bke::bNodeType ntype;
419
420 cmp_node_type_base(&ntype, "CompositorNodeRelativeToPixel"_ustr);
421 ntype.ui_name = "Relative To Pixel";
422 ntype.ui_description =
423 "Converts values that are relative to the image size to be in terms of pixels";
425 ntype.declare = node_declare;
426 ntype.initfunc = node_init;
427 ntype.updatefunc = node_update;
430
432
433 node_rna(ntype.rna_ext.srna);
434}
436
437} // namespace blender::nodes::node_composite_relative_to_pixel_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:484
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
#define UI_ITEM_NONE
#define BLI_assert_unreachable()
#define output
gpu::Shader * get_shader(const char *info_name, ResultPrecision precision)
Domain realize_transformation(const bool realize_translation=false) const
Definition domain.cc:59
NodeOperation(Context &context, const bNode &node)
Result & get_result(StringRef identifier)
Definition operation.cc:40
Context & context() const
Definition operation.cc:57
Result & get_input(StringRef identifier) const
Definition operation.cc:35
InputDescriptor & get_input_descriptor(StringRef identifier)
Definition operation.cc:132
void unbind_as_texture() const
Definition result.cc:964
void bind_as_texture(gpu::Shader *shader, const char *texture_name) const
Definition result.cc:854
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void node_set_socket_availability(bNodeTree &ntree, bNodeSocket &sock, bool is_available)
Definition node.cc:5499
bNodeSocket * node_find_socket(bNode &node, eNodeSocketInOut in_out, UString identifier)
Definition node.cc:3036
void compute_dispatch_threads_at_least(gpu::Shader *shader, int2 threads_range, int2 local_size=int2(16))
Definition utilities.cc:235
void parallel_for(const int2 range, const Function &function)
T reduce_max(const VecBase< T, Size > &a)
T length(const VecBase< T, Size > &a)
T reduce_min(const VecBase< T, Size > &a)
static NodeOperation * get_compositor_operation(Context &context, const bNode &node)
static void node_draw_buttons(ui::Layout &layout, bContext *, PointerRNA *ptr)
PropertyRNA * RNA_def_node_enum(StructRNA *srna, const char *identifier, const char *ui_name, const char *ui_description, const EnumPropertyItem *static_items, const EnumRNAAccessors accessors, std::optional< int > default_value, const EnumPropertyItemFunc item_func, const bool allow_animation)
CMPNodeRelativeToPixelReferenceDimension
@ CMP_NODE_RELATIVE_TO_PIXEL_REFERENCE_DIMENSION_DIAGONAL
@ CMP_NODE_RELATIVE_TO_PIXEL_REFERENCE_DIMENSION_GREATER
@ CMP_NODE_RELATIVE_TO_PIXEL_REFERENCE_DIMENSION_SMALLER
@ CMP_NODE_RELATIVE_TO_PIXEL_REFERENCE_DIMENSION_X
@ CMP_NODE_RELATIVE_TO_PIXEL_REFERENCE_DIMENSION_PER_DIMENSION
@ CMP_NODE_RELATIVE_TO_PIXEL_REFERENCE_DIMENSION_Y
CMPNodeRelativeToPixelDataType
@ CMP_NODE_RELATIVE_TO_PIXEL_DATA_TYPE_FLOAT
@ CMP_NODE_RELATIVE_TO_PIXEL_DATA_TYPE_VECTOR
void GPU_shader_unbind()
void GPU_shader_bind(gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_shader_uniform_2fv(gpu::Shader *sh, const char *name, const float data[2])
void GPU_shader_uniform_1f(gpu::Shader *sh, const char *name, float value)
@ PROP_FACTOR
Definition RNA_types.hh:308
void cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
#define min(a, b)
Definition sort.cc:38
Defines a node type.
Definition BKE_node.hh:265
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
std::string ui_description
Definition BKE_node.hh:271
void(*) updatefunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:312
NodeGetCompositorOperationFunction get_compositor_operation
Definition BKE_node.hh:379
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
NodeDeclareFunction declare
Definition BKE_node.hh:393
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)