Blender V5.3
node_composite_corner_pin.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2013 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_math_geom_c.hh"
8
9#include "DNA_node_types.h"
10
11#include "RNA_enum_types.hh"
12
13#include "GPU_shader.hh"
14#include "GPU_texture.hh"
15
16#include "BKE_node.hh"
17#include "BKE_tracking.hh"
18
19#include "MEM_guardedalloc.h"
20
21#include "COM_algorithm_smaa.hh"
22#include "COM_domain.hh"
23#include "COM_node_operation.hh"
24#include "COM_utilities.hh"
25
27
29
31{
32 b.use_custom_socket_order();
33 b.allow_any_socket_order();
34
35 b.add_input<decl::Color>("Image"_ustr)
36 .default_value({1.0f, 1.0f, 1.0f, 1.0f})
37 .hide_value()
38 .structure_type(StructureType::Dynamic);
39 b.add_output<decl::Color>("Image"_ustr)
40 .structure_type(StructureType::Dynamic)
41 .align_with_previous();
42 b.add_output<decl::Float>("Plane"_ustr).structure_type(StructureType::Dynamic);
43
44 b.add_input<decl::Vector>("Upper Left"_ustr)
45 .subtype(PROP_FACTOR)
46 .dimensions(2)
47 .default_value({0.0f, 1.0f})
48 .min(0.0f)
49 .max(1.0f);
50 b.add_input<decl::Vector>("Upper Right"_ustr)
51 .subtype(PROP_FACTOR)
52 .dimensions(2)
53 .default_value({1.0f, 1.0f})
54 .min(0.0f)
55 .max(1.0f);
56 b.add_input<decl::Vector>("Lower Left"_ustr)
57 .subtype(PROP_FACTOR)
58 .dimensions(2)
59 .default_value({0.0f, 0.0f})
60 .min(0.0f)
61 .max(1.0f);
62 b.add_input<decl::Vector>("Lower Right"_ustr)
63 .subtype(PROP_FACTOR)
64 .dimensions(2)
65 .default_value({1.0f, 0.0f})
66 .min(0.0f)
67 .max(1.0f);
68
69 PanelDeclarationBuilder &sampling_panel = b.add_panel("Sampling"_ustr).default_closed(true);
70 sampling_panel.add_input<decl::Menu>("Interpolation"_ustr)
73 .description("Interpolation method")
74 .optional_label();
75 sampling_panel.add_input<decl::Menu>("Extension X"_ustr)
76 .default_value(CMP_NODE_EXTENSION_MODE_CLIP)
78 .description("The extension mode applied to the X axis")
79 .optional_label();
80 sampling_panel.add_input<decl::Menu>("Extension Y"_ustr)
81 .default_value(CMP_NODE_EXTENSION_MODE_CLIP)
83 .description("The extension mode applied to the Y axis")
84 .optional_label();
85}
86
87static void node_init(bNodeTree * /*ntree*/, bNode *node)
88{
89 /* Unused, kept for forward compatibility. */
90 NodeCornerPinData *data = MEM_new<NodeCornerPinData>(__func__);
91 node->storage = data;
92}
93
94using namespace blender::compositor;
95
97 public:
99
100 void execute() override
101 {
102 const float3x3 homography_matrix = compute_homography_matrix();
103
104 const Result &input_image = this->get_input("Image");
105 Result &output_image = this->get_result("Image");
106 Result &output_mask = this->get_result("Plane");
107 if (input_image.is_single_value() || homography_matrix == float3x3::identity()) {
108 if (output_image.should_compute()) {
109 output_image.share_data(input_image);
110 }
111 if (output_mask.should_compute()) {
112 output_mask.allocate_single_value();
113 output_mask.set_single_value(1.0f);
114 }
115 return;
116 }
117
118 /* Only compute the mask if extension modes are not set to clip and if it is not used as an
119 * output. */
120 if (this->should_compute_mask()) {
121 Result plane_mask = compute_plane_mask(homography_matrix);
122 Result anti_aliased_plane_mask = context().create_result(ResultType::Float);
123 smaa(context(), plane_mask, anti_aliased_plane_mask);
124 plane_mask.release();
125
126 if (output_image.should_compute()) {
127 this->compute_plane(homography_matrix, &anti_aliased_plane_mask);
128 }
129
130 if (output_mask.should_compute()) {
131 output_mask.share_data(anti_aliased_plane_mask);
132 }
133
134 anti_aliased_plane_mask.release();
135 }
136 else {
137 if (output_image.should_compute()) {
138 this->compute_plane(homography_matrix, nullptr);
139 }
140 }
141 }
142
143 void compute_plane(const float3x3 &homography_matrix, Result *plane_mask)
144 {
145 if (this->context().use_gpu()) {
146 this->compute_plane_gpu(homography_matrix, plane_mask);
147 }
148 else {
149 this->compute_plane_cpu(homography_matrix, plane_mask);
150 }
151 }
152
153 void compute_plane_gpu(const float3x3 &homography_matrix, Result *plane_mask)
154 {
157
158 GPU_shader_uniform_mat3_as_mat4(shader, "homography_matrix", homography_matrix.ptr());
159
160 Result &input_image = get_input("Image");
161 GPU_texture_mipmap_mode(input_image, true, true);
162 /* The texture sampler should use bilinear interpolation for both the bilinear and bicubic
163 * cases, as the logic used by the bicubic realization shader expects textures to use
164 * bilinear interpolation. */
165 const Interpolation interpolation = this->get_interpolation();
166 const Extension extension_mode_x = this->get_extension_mode_x();
167 const Extension extension_mode_y = this->get_extension_mode_y();
168
169 const bool use_bilinear = ELEM(interpolation, Interpolation::Bicubic, Interpolation::Bilinear);
170 const bool use_anisotropic = interpolation == Interpolation::Anisotropic;
171 GPU_texture_filter_mode(input_image, use_bilinear);
172 GPU_texture_anisotropic_filter(input_image, use_anisotropic);
173 GPU_texture_extend_mode_x(input_image, map_extension_mode_to_extend_mode(extension_mode_x));
174 GPU_texture_extend_mode_y(input_image, map_extension_mode_to_extend_mode(extension_mode_y));
175 input_image.bind_as_texture(shader, "input_tx");
176 if (plane_mask) {
177 plane_mask->bind_as_texture(shader, "mask_tx");
178 }
179
180 const Domain domain = compute_domain();
181 Result &output_image = get_result("Image");
182 output_image.allocate_texture(domain);
183 output_image.bind_as_image(shader, "output_img");
184
186
187 input_image.unbind_as_texture();
188 if (plane_mask) {
189 plane_mask->unbind_as_texture();
190 }
191
192 output_image.unbind_as_image();
194 }
195
196 void compute_plane_cpu(const float3x3 &homography_matrix, Result *plane_mask)
197 {
198 Result &input = get_input("Image");
199
200 const Domain domain = compute_domain();
201 Result &output = get_result("Image");
202 output.allocate_texture(domain);
203 const Interpolation interpolation = this->get_interpolation();
204 const Extension extension_mode_x = this->get_extension_mode_x();
205 const Extension extension_mode_y = this->get_extension_mode_y();
206
207 const int2 size = domain.data_size;
208 parallel_for(size, [&](const int2 texel) {
209 float2 coordinates = (float2(texel) + float2(0.5f)) / float2(size);
210
211 float3 transformed_coordinates = float3x3(homography_matrix) * float3(coordinates, 1.0f);
212 /* Point is at infinity and will be zero when sampled, so early exit. */
213 if (transformed_coordinates.z == 0.0f) {
214 output.store_pixel(texel, Color(float4(0.0f)));
215 return;
216 }
217
218 float2 projected_coordinates = transformed_coordinates.xy() / transformed_coordinates.z;
219 float4 sampled_color;
220
221 if (interpolation != Interpolation::Anisotropic) {
222 sampled_color = float4(input.sample<Color>(
223 projected_coordinates, interpolation, extension_mode_x, extension_mode_y));
224 }
225 else {
226 /* Derivative of transformed_coordinates.xy / transformed_coordinates.z vs texels. */
227 float2 x_gradient = (homography_matrix[0].xy() * transformed_coordinates.z -
228 transformed_coordinates.xy() * homography_matrix[0].z) /
229 (math::square(transformed_coordinates.z) * size.x);
230 float2 y_gradient = (homography_matrix[1].xy() * transformed_coordinates.z -
231 transformed_coordinates.xy() * homography_matrix[1].z) /
232 (math::square(transformed_coordinates.z) * size.y);
233 const float2x2 jacobian = float2x2(x_gradient, y_gradient);
234 sampled_color = float4(input.sample<Color>(projected_coordinates,
238 jacobian));
239 }
240
241 float4 plane_color = plane_mask ? sampled_color * plane_mask->load_pixel<float>(texel) :
242 sampled_color;
243
244 output.store_pixel(texel, Color(plane_color));
245 });
246 }
247
248 Result compute_plane_mask(const float3x3 &homography_matrix)
249 {
250 if (this->context().use_gpu()) {
251 return this->compute_plane_mask_gpu(homography_matrix);
252 }
253
254 return this->compute_plane_mask_cpu(homography_matrix);
255 }
256
257 Result compute_plane_mask_gpu(const float3x3 &homography_matrix)
258 {
259 const bool is_x_clipped = this->get_extension_mode_x() == Extension::Clip;
260 const bool is_y_clipped = this->get_extension_mode_y() == Extension::Clip;
261
262 gpu::Shader *shader = context().get_shader("compositor_plane_deform_mask");
264
265 GPU_shader_uniform_mat3_as_mat4(shader, "homography_matrix", homography_matrix.ptr());
266 GPU_shader_uniform_1b(shader, "is_x_clipped", is_x_clipped);
267 GPU_shader_uniform_1b(shader, "is_y_clipped", is_y_clipped);
268
269 const Domain domain = compute_domain();
271 plane_mask.allocate_texture(domain);
272 plane_mask.bind_as_image(shader, "mask_img");
273
275
276 plane_mask.unbind_as_image();
278
279 return plane_mask;
280 }
281
282 Result compute_plane_mask_cpu(const float3x3 &homography_matrix)
283 {
284 const bool is_x_clipped = this->get_extension_mode_x() == Extension::Clip;
285 const bool is_y_clipped = this->get_extension_mode_y() == Extension::Clip;
286 const Domain domain = compute_domain();
288 plane_mask.allocate_texture(domain);
289
290 const int2 size = domain.data_size;
291 parallel_for(size, [&](const int2 texel) {
292 float2 coordinates = (float2(texel) + float2(0.5f)) / float2(size);
293
294 float3 transformed_coordinates = float3x3(homography_matrix) * float3(coordinates, 1.0f);
295 /* Point is at infinity and will be zero when sampled, so early exit. */
296 if (transformed_coordinates.z == 0.0f) {
297 plane_mask.store_pixel(texel, 0.0f);
298 return;
299 }
300 float2 projected_coordinates = transformed_coordinates.xy() / transformed_coordinates.z;
301 bool is_inside_plane_x = projected_coordinates.x >= 0.0f && projected_coordinates.x <= 1.0f;
302 bool is_inside_plane_y = projected_coordinates.y >= 0.0f && projected_coordinates.y <= 1.0f;
303
304 /* If not inside the plane and not clipped, use extend or repeat extension mode for the
305 * mask. */
306 bool is_x_masked = is_inside_plane_x || !is_x_clipped;
307 bool is_y_masked = is_inside_plane_y || !is_y_clipped;
308 float mask_value = is_x_masked && is_y_masked ? 1.0f : 0.0f;
309
310 plane_mask.store_pixel(texel, mask_value);
311 });
312
313 return plane_mask;
314 }
315
317 {
318 float2 lower_left = get_input("Lower Left").get_single_value_default<float2>();
319 float2 lower_right = get_input("Lower Right").get_single_value_default<float2>();
320 float2 upper_right = get_input("Upper Right").get_single_value_default<float2>();
321 float2 upper_left = get_input("Upper Left").get_single_value_default<float2>();
322
323 /* The inputs are invalid because the plane is not convex, fall back to an identity operation
324 * in that case. */
325 if (!is_quad_convex_v2(lower_left, lower_right, upper_right, upper_left)) {
326 return float3x3::identity();
327 }
328
329 /* Compute a 2D projection matrix that projects from the corners of the image in normalized
330 * coordinates into the corners of the input plane. */
331 float3x3 homography_matrix;
332 float corners[4][2] = {{lower_left.x, lower_left.y},
333 {lower_right.x, lower_right.y},
334 {upper_right.x, upper_right.y},
335 {upper_left.x, upper_left.y}};
336 float identity_corners[4][2] = {{0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f}};
337 BKE_tracking_homography_between_two_quads(corners, identity_corners, homography_matrix.ptr());
338 return homography_matrix;
339 }
340
342 {
343 const CMPNodeInterpolation interpolation = static_cast<CMPNodeInterpolation>(
344 this->get_input("Interpolation").get_single_value_default<MenuValue>().value);
345 switch (interpolation) {
354 }
355
357 }
358
360 {
362 return Extension::Clip;
363 }
364
365 const CMPExtensionMode extension_x = static_cast<CMPExtensionMode>(
366 this->get_input("Extension X").get_single_value_default<MenuValue>().value);
367 switch (extension_x) {
369 return Extension::Clip;
371 return Extension::Repeat;
373 return Extension::Extend;
374 }
375
376 return Extension::Clip;
377 }
378
380 {
382 return Extension::Clip;
383 }
384
385 const CMPExtensionMode extension_y = static_cast<CMPExtensionMode>(
386 this->get_input("Extension Y").get_single_value_default<MenuValue>().value);
387 switch (extension_y) {
389 return Extension::Clip;
391 return Extension::Repeat;
393 return Extension::Extend;
394 }
395
396 return Extension::Clip;
397 }
398
399 const char *get_shader_name()
400 {
401 if (this->should_compute_mask()) {
402 switch (this->get_interpolation()) {
405 return "compositor_plane_deform_masked";
407 return "compositor_plane_deform_bicubic_masked";
409 return "compositor_plane_deform_anisotropic_masked";
410 }
411 }
412
413 switch (this->get_interpolation()) {
416 return "compositor_plane_deform";
418 return "compositor_plane_deform_bicubic";
419 /* Anisotropic does not implement extension modes. Return masked shader. */
421 break;
422 }
423
424 return "compositor_plane_deform_anisotropic_masked";
425 }
426
428 {
429 Result &output_mask = this->get_result("Plane");
430 const bool is_clipped_x = this->get_extension_mode_x() == Extension::Clip;
431 const bool is_clipped_y = this->get_extension_mode_y() == Extension::Clip;
432 const bool output_needed = output_mask.should_compute();
433 const bool use_anisotropic = this->get_interpolation() == Interpolation::Anisotropic;
434
435 return is_clipped_x || is_clipped_y || output_needed || use_anisotropic;
436 }
437
439 {
440 Domain domain = this->get_input("Image").domain();
441 /* Reset the location of the domain such that translations take effect, this will result in
442 * clipping but is more expected for the user. */
443 domain.transformation.location() = float2(0.0f);
444 return domain;
445 }
446};
447
449{
450 return new CornerPinOperation(context, node);
451}
452
453static void node_register()
454{
455 static bke::bNodeType ntype;
456
457 cmp_node_type_base(&ntype, "CompositorNodeCornerPin"_ustr, CMP_NODE_CORNERPIN);
458 ntype.ui_name = "Corner Pin";
459 ntype.ui_description = "Plane warp transformation using explicit corner values";
460 ntype.enum_name_legacy = "CORNERPIN";
462 ntype.declare = node_declare;
463 ntype.initfunc = node_init;
466 ntype, "NodeCornerPinData", node_free_standard_storage, node_copy_standard_storage);
468}
470
471} // namespace blender::nodes::node_composite_corner_pin_cc
#define NODE_CLASS_DISTORT
Definition BKE_node.hh:486
#define CMP_NODE_CORNERPIN
Read Blender memory allocation and freeing.
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define ELEM(...)
#define output
Result create_result(ResultType type, ResultPrecision precision)
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
void share_data(const Result &source)
Definition result.cc:996
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 store_pixel(const int2 &texel, const T &pixel_value)
bool should_compute() const
Definition result.cc:1124
void allocate_texture(const Domain domain, const bool from_pool=true, const std::optional< ResultStorageType > storage_type=std::nullopt)
Definition result.cc:554
void unbind_as_texture() const
Definition result.cc:964
void set_single_value(const T &value)
void bind_as_texture(gpu::Shader *shader, const char *texture_name) const
Definition result.cc:854
T load_pixel(const int2 &texel) const
void unbind_as_image() const
Definition result.cc:990
void bind_as_image(gpu::Shader *shader, const char *image_name, bool read=false) const
Definition result.cc:951
DeclType::Builder & add_input(UString name, UString identifier=""_ustr)
void compute_plane_gpu(const float3x3 &homography_matrix, Result *plane_mask)
void compute_plane(const float3x3 &homography_matrix, Result *plane_mask)
void compute_plane_cpu(const float3x3 &homography_matrix, Result *plane_mask)
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 smaa(Context &context, const Result &input, Result &output, const float threshold=0.1f, const float local_contrast_adaptation_factor=2.0f, const int corner_rounding=25)
Definition smaa.cc:1693
void parallel_for(const int2 range, const Function &function)
T square(const T &a)
static NodeOperation * get_compositor_operation(Context &context, const bNode &node)
static void node_declare(NodeDeclarationBuilder &b)
static void node_init(bNodeTree *, bNode *node)
const EnumPropertyItem rna_enum_node_compositor_extension_items[]
MatBase< float, 2, 2 > float2x2
void GPU_texture_anisotropic_filter(gpu::Texture *texture, bool use_aniso)
void GPU_shader_uniform_1b(gpu::Shader *sh, const char *name, bool value)
void GPU_texture_mipmap_mode(gpu::Texture *texture, bool use_mipmap, bool use_filter)
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)
void BKE_tracking_homography_between_two_quads(float reference_corners[4][2], float corners[4][2], float H[3][3])
@ 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)
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
@ PROP_FACTOR
Definition RNA_types.hh:308
void GPU_shader_uniform_mat3_as_mat4(gpu::Shader *sh, const char *name, const float data[3][3])
const EnumPropertyItem rna_enum_node_compositor_interpolation_items[]
bool is_quad_convex_v2(const float v1[2], const float v2[2], const float v3[2], const float v4[2])
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
const c_style_mat & ptr() const
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