Blender V5.3
node_composite_scale.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2006 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_math_base.hh"
6#include "BLI_math_matrix.hh"
8#include "BLI_math_vector.hh"
10
11#include "DNA_node_types.h"
12
13#include "RNA_enum_types.hh"
14
15#include "GPU_shader.hh"
16#include "GPU_texture.hh"
17
18#include "COM_domain.hh"
19#include "COM_node_operation.hh"
20#include "COM_utilities.hh"
21
23
25
26static const EnumPropertyItem type_items[] = {
27 {CMP_NODE_SCALE_RELATIVE, "RELATIVE", 0, N_("Relative"), ""},
28 {CMP_NODE_SCALE_ABSOLUTE, "ABSOLUTE", 0, N_("Absolute"), ""},
29 {CMP_NODE_SCALE_RENDER_PERCENT, "SCENE_SIZE", 0, N_("Scene Size"), ""},
30 {CMP_NODE_SCALE_RENDER_SIZE, "RENDER_SIZE", 0, N_("Render Size"), ""},
31 {0, nullptr, 0, nullptr, nullptr},
32};
33
34/* Matches bgpic_camera_frame_items[]. */
36 {CMP_NODE_SCALE_RENDER_SIZE_STRETCH, "STRETCH", 0, N_("Stretch"), ""},
37 {CMP_NODE_SCALE_RENDER_SIZE_FIT, "FIT", 0, N_("Fit"), ""},
38 {CMP_NODE_SCALE_RENDER_SIZE_CROP, "CROP", 0, N_("Crop"), ""},
39 {0, nullptr, 0, nullptr, nullptr},
40};
41
43{
44 b.use_custom_socket_order();
45 b.allow_any_socket_order();
46
47 b.add_input<decl::Color>("Image"_ustr)
48 .default_value({1.0f, 1.0f, 1.0f, 1.0f})
49 .hide_value()
51 .structure_type(StructureType::Dynamic);
52 b.add_output<decl::Color>("Image"_ustr)
53 .structure_type(StructureType::Dynamic)
54 .align_with_previous();
55
56 b.add_input<decl::Menu>("Type"_ustr)
57 .default_value(CMP_NODE_SCALE_RELATIVE)
58 .static_items(type_items);
59 b.add_input<decl::Float>("X"_ustr)
60 .default_value(1.0f)
61 .min(0.0001f)
62 .max(CMP_SCALE_MAX)
63 .structure_type(StructureType::Dynamic)
64 .usage_by_menu("Type"_ustr, {CMP_NODE_SCALE_RELATIVE, CMP_NODE_SCALE_ABSOLUTE});
65 b.add_input<decl::Float>("Y"_ustr)
66 .default_value(1.0f)
67 .min(0.0001f)
68 .max(CMP_SCALE_MAX)
69 .structure_type(StructureType::Dynamic)
70 .usage_by_menu("Type"_ustr, {CMP_NODE_SCALE_RELATIVE, CMP_NODE_SCALE_ABSOLUTE});
71 b.add_input<decl::Menu>("Frame Type"_ustr)
73 .static_items(frame_type_items)
74 .usage_by_menu("Type"_ustr, CMP_NODE_SCALE_RENDER_SIZE)
76 .description("How the image fits in the camera frame");
77
78 PanelDeclarationBuilder &sampling_panel = b.add_panel("Sampling"_ustr).default_closed(true);
79 sampling_panel.add_input<decl::Menu>("Interpolation"_ustr)
83 .description("Interpolation method");
84 sampling_panel.add_input<decl::Menu>("Extension X"_ustr)
85 .default_value(CMP_NODE_EXTENSION_MODE_CLIP)
88 .description("The extension mode applied to the X axis");
89 sampling_panel.add_input<decl::Menu>("Extension Y"_ustr)
90 .default_value(CMP_NODE_EXTENSION_MODE_CLIP)
93 .description("The extension mode applied to the Y axis");
94}
95
96static void node_init(bNodeTree * /*ntree*/, bNode *node)
97{
98 /* Unused, kept for forward compatibility. */
99 NodeScaleData *data = MEM_new<NodeScaleData>(__func__);
100 node->storage = data;
101}
102
103using namespace blender::compositor;
104
106 public:
108
109 void execute() override
110 {
111 if (is_variable_size()) {
113 }
114 else {
116 }
117 }
118
120 {
121 const float2 scale = this->get_scale();
122 const float3x3 transformation = math::from_scale<float3x3>(scale);
123
124 const Result &input = this->get_input("Image");
125 Result &output = this->get_result("Image");
126 output.share_data(input);
127 output.transform(transformation);
128
129 output.get_realization_options().interpolation = this->get_interpolation();
130 output.get_realization_options().extension_x = this->get_extension_mode_x();
131 output.get_realization_options().extension_y = this->get_extension_mode_y();
132 }
133
135 {
136 const Result &input = this->get_input("Image");
137 if (input.is_single_value()) {
138 Result &output = this->get_result("Image");
139 output.share_data(input);
140 return;
141 }
142
143 if (this->context().use_gpu()) {
145 }
146 else {
148 }
149 }
150
152 {
155
156 Result &input = get_input("Image");
157 /* The texture sampler should use bilinear interpolation for both the bilinear and bicubic
158 * cases, as the logic used by the bicubic realization shader expects textures to use bilinear
159 * interpolation. */
160 const Interpolation interpolation = this->get_interpolation();
161 const Extension extension_mode_x = this->get_extension_mode_x();
162 const Extension extension_mode_y = this->get_extension_mode_y();
163
164 /* For now the EWA sampling falls back to bicubic interpolation. */
165 const bool use_bilinear = ELEM(interpolation, Interpolation::Bilinear, Interpolation::Bicubic);
166 GPU_texture_filter_mode(input, use_bilinear);
169 input.bind_as_texture(shader, "input_tx");
170
171 Result &x_scale = get_input("X");
172 gpu::Texture *x_scale_texture = x_scale.bind_as_texture_or_single_value(shader, "x_scale_tx");
173
174 Result &y_scale = get_input("Y");
175 gpu::Texture *y_scale_texture = y_scale.bind_as_texture_or_single_value(shader, "y_scale_tx");
176
177 Result &output = get_result("Image");
178 const Domain domain = compute_domain();
179 output.allocate_texture(domain);
180 output.bind_as_image(shader, "output_img");
181
183
184 input.unbind_as_texture();
185 x_scale.unbind_as_texture_or_single_value(x_scale_texture);
186 y_scale.unbind_as_texture_or_single_value(y_scale_texture);
187 output.unbind_as_image();
189 }
190
192 {
193 const Result &input = this->get_input("Image");
194 const Result &x_scale = this->get_input("X");
195 const Result &y_scale = this->get_input("Y");
196
197 Result &output = this->get_result("Image");
198 const Interpolation interpolation = this->get_interpolation();
199 const Extension extension_mode_x = this->get_extension_mode_x();
200 const Extension extension_mode_y = this->get_extension_mode_y();
201 const Domain domain = compute_domain();
202 const int2 size = domain.data_size;
203 output.allocate_texture(domain);
204
205 parallel_for(size, [&](const int2 texel) {
206 float2 coordinates = (float2(texel) + float2(0.5f)) / float2(size);
207 float2 center = float2(0.5f);
208
209 float2 scale = float2(x_scale.load_pixel<float, true>(texel),
210 y_scale.load_pixel<float, true>(texel));
211 float2 scaled_coordinates = center +
212 (coordinates - center) / math::max(scale, float2(0.0001f));
213
214 output.store_pixel(
215 texel,
216 input.sample<Color>(
217 scaled_coordinates, interpolation, extension_mode_x, extension_mode_y));
218 });
219 }
220
221 const char *get_shader_name() const
222 {
224 return "compositor_scale_variable_bicubic";
225 }
226 return "compositor_scale_variable";
227 }
228
230 {
231 const CMPNodeInterpolation interpolation = CMPNodeInterpolation(
232 this->get_input("Interpolation").get_single_value_default<MenuValue>().value);
233 switch (interpolation) {
242 }
243
245 }
246
248 {
249 const CMPExtensionMode extension_x = CMPExtensionMode(
250 this->get_input("Extension X").get_single_value_default<MenuValue>().value);
251 switch (extension_x) {
253 return Extension::Clip;
255 return Extension::Repeat;
257 return Extension::Extend;
258 }
259
260 return Extension::Clip;
261 }
262
264 {
265 const CMPExtensionMode extension_y = CMPExtensionMode(
266 this->get_input("Extension Y").get_single_value_default<MenuValue>().value);
267 switch (extension_y) {
269 return Extension::Clip;
271 return Extension::Repeat;
273 return Extension::Extend;
274 }
275
276 return Extension::Clip;
277 }
278
280 {
281 switch (get_type()) {
283 return get_scale_relative();
285 return get_scale_absolute();
289 return get_scale_render_size();
290 }
291
292 return float2(1.0f);
293 }
294
295 /* Scale by the input factors. */
297 {
298 return float2(get_input("X").get_single_value_default<float>(),
299 get_input("Y").get_single_value_default<float>());
300 }
301
302 /* Scale such that the new size matches the input absolute size. */
304 {
305 const float2 input_size = float2(get_input("Image").domain().display_size);
306 const float2 absolute_size = float2(get_input("X").get_single_value_default<float>(),
307 get_input("Y").get_single_value_default<float>());
308 return absolute_size / input_size;
309 }
310
311 /* Scale by the render resolution percentage. */
313 {
314 return float2(context().get_render_percentage());
315 }
316
330
331 /* Scale such that the new size matches the render size. Since the input is freely scaled, it is
332 * potentially stretched, hence the name. */
334 {
335 const float2 input_size = float2(get_input("Image").domain().display_size);
336 const float2 render_size = float2(context().get_compositing_domain().display_size);
337 return render_size / input_size;
338 }
339
340 /* Scale such that the dimension with the smaller scaling factor matches that of the render size
341 * while maintaining the input's aspect ratio. Since the other dimension is guaranteed not to
342 * exceed the render size region due to its larger scaling factor, the image is said to be fit
343 * inside that region, hence the name. */
345 {
346 const float2 input_size = float2(get_input("Image").domain().display_size);
347 const float2 render_size = float2(context().get_compositing_domain().display_size);
348 const float2 scale = render_size / input_size;
349 return float2(math::min(scale.x, scale.y));
350 }
351
352 /* Scale such that the dimension with the larger scaling factor matches that of the render size
353 * while maintaining the input's aspect ratio. Since the other dimension is guaranteed to exceed
354 * the render size region due to its lower scaling factor, the image will be cropped inside that
355 * region, hence the name. */
357 {
358 const float2 input_size = float2(get_input("Image").domain().display_size);
359 const float2 render_size = float2(context().get_compositing_domain().display_size);
360 const float2 scale = render_size / input_size;
361 return float2(math::max(scale.x, scale.y));
362 }
363
365 {
366 /* Only relative scaling can be variable. */
368 return false;
369 }
370
371 return !get_input("X").is_single_value() || !get_input("Y").is_single_value();
372 }
373
375 {
376 return CMPNodeScaleMethod(this->get_input("Type").get_single_value_default<MenuValue>().value);
377 }
378
380 {
382 this->get_input("Frame Type").get_single_value_default<MenuValue>().value);
383 }
384};
385
387{
388 return new ScaleOperation(context, node);
389}
390
391static void node_register()
392{
393 static bke::bNodeType ntype;
394
395 cmp_node_type_base(&ntype, "CompositorNodeScale"_ustr, CMP_NODE_SCALE);
396 ntype.ui_name = "Scale";
397 ntype.ui_description = "Change the size of the image";
398 ntype.enum_name_legacy = "SCALE";
400 ntype.declare = node_declare;
401 ntype.initfunc = node_init;
405
407}
409
410} // namespace blender::nodes::node_composite_scale_cc
#define NODE_CLASS_DISTORT
Definition BKE_node.hh:486
#define CMP_NODE_SCALE
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define ELEM(...)
#define output
gpu::Shader * get_shader(const char *info_name, ResultPrecision precision)
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
virtual Domain compute_domain()
Definition operation.cc:62
T sample(const float2 &coordinates, const Interpolation &interpolation, const Extension &extension_mode_x, const Extension &extension_mode_y, std::optional< float2x2 > jacobian=std::nullopt) const
void unbind_as_texture_or_single_value(gpu::Texture *single_value_texture) const
Definition result.cc:970
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
T load_pixel(const int2 &texel) const
gpu::Texture * bind_as_texture_or_single_value(gpu::Shader *shader, const char *texture_name) const
Definition result.cc:861
DeclType::Builder & add_input(UString name, UString identifier=""_ustr)
const CompositorInputRealizationMode & compositor_realization_mode() const
NodeOperation(Context &context, const bNode &node)
float3x3 scale(float3x3 mat, float2 scale)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void node_type_storage(bNodeType &ntype, std::optional< StringRefNull > storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:6166
void compute_dispatch_threads_at_least(gpu::Shader *shader, int2 threads_range, int2 local_size=int2(16))
Definition utilities.cc:235
ColorSceneLinear4f< eAlpha::Premultiplied > Color
Definition COM_result.hh:96
GPUSamplerExtendMode map_extension_mode_to_extend_mode(const Extension &mode)
Definition domain.cc:144
void parallel_for(const int2 range, const Function &function)
T min(const T &a, const T &b)
MatT from_scale(const VecBase< typename MatT::base_type, ScaleDim > &scale)
T max(const T &a, const T &b)
static NodeOperation * get_compositor_operation(Context &context, const bNode &node)
static void node_init(bNodeTree *, bNode *node)
static const EnumPropertyItem type_items[]
static const EnumPropertyItem frame_type_items[]
static void node_declare(NodeDeclarationBuilder &b)
const EnumPropertyItem rna_enum_node_compositor_extension_items[]
CMPNodeScaleRenderSizeMethod
@ CMP_NODE_SCALE_RENDER_SIZE_STRETCH
@ CMP_NODE_SCALE_RENDER_SIZE_CROP
@ CMP_NODE_SCALE_RENDER_SIZE_FIT
BMesh const char void * data
void GPU_shader_unbind()
void GPU_texture_filter_mode(gpu::Texture *texture, bool use_filter)
@ CMP_NODE_EXTENSION_MODE_REPEAT
@ CMP_NODE_EXTENSION_MODE_EXTEND
@ CMP_NODE_EXTENSION_MODE_CLIP
void GPU_shader_bind(gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
@ CMP_NODE_INTERPOLATION_NEAREST
@ CMP_NODE_INTERPOLATION_BICUBIC
@ CMP_NODE_INTERPOLATION_ANISOTROPIC
@ CMP_NODE_INTERPOLATION_BILINEAR
void GPU_texture_extend_mode_x(gpu::Texture *texture, GPUSamplerExtendMode extend_mode)
void GPU_texture_extend_mode_y(gpu::Texture *texture, GPUSamplerExtendMode extend_mode)
@ CMP_NODE_SCALE_ABSOLUTE
@ CMP_NODE_SCALE_RENDER_PERCENT
@ CMP_NODE_SCALE_RELATIVE
@ CMP_NODE_SCALE_RENDER_SIZE
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
const EnumPropertyItem rna_enum_node_compositor_interpolation_items[]
void cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
#define CMP_SCALE_MAX
Defines a node type.
Definition BKE_node.hh:265
std::string ui_description
Definition BKE_node.hh:271
NodeGetCompositorOperationFunction get_compositor_operation
Definition BKE_node.hh:379
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393
#define N_(msgid)