Blender V5.3
node_composite_displace.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 "MEM_guardedalloc.h"
6
7#include "BLI_math_matrix.hh"
8#include "BLI_math_vector.hh"
10#include "BLI_utildefines.hh"
11
12#include "DNA_node_types.h"
13
14#include "RNA_enum_types.hh"
15
16#include "GPU_shader.hh"
17#include "GPU_texture.hh"
18
19#include "BKE_node.hh"
20
21#include "COM_domain.hh"
22#include "COM_node_operation.hh"
23#include "COM_utilities.hh"
24
26
28
30{
31 b.use_custom_socket_order();
32 b.allow_any_socket_order();
33
34 b.add_input<decl::Color>("Image"_ustr)
35 .default_value({1.0f, 1.0f, 1.0f, 1.0f})
36 .hide_value()
37 .structure_type(StructureType::Dynamic);
38 b.add_output<decl::Color>("Image"_ustr)
39 .structure_type(StructureType::Dynamic)
40 .align_with_previous();
41
42 b.add_input<decl::Vector>("Displacement"_ustr)
43 .dimensions(2)
44 .default_value({0.0f, 0.0f})
45 .structure_type(StructureType::Dynamic);
46
47 PanelDeclarationBuilder &sampling_panel = b.add_panel("Sampling"_ustr).default_closed(true);
48 sampling_panel.add_input<decl::Menu>("Interpolation"_ustr)
51 .description("Interpolation method")
52 .optional_label();
53 sampling_panel.add_input<decl::Menu>("Extension X"_ustr)
54 .default_value(CMP_NODE_EXTENSION_MODE_CLIP)
56 .description("The extension mode applied to the X axis")
57 .optional_label();
58 sampling_panel.add_input<decl::Menu>("Extension Y"_ustr)
59 .default_value(CMP_NODE_EXTENSION_MODE_CLIP)
61 .description("The extension mode applied to the Y axis")
62 .optional_label();
63}
64
65static void node_init(bNodeTree * /*ntree*/, bNode *node)
66{
67 /* Unused, kept for forward compatibility. */
68 NodeDisplaceData *data = MEM_new<NodeDisplaceData>(__func__);
69 node->storage = data;
70}
71
72using namespace blender::compositor;
73
75 public:
77
78 void execute() override
79 {
80 const Result &input = this->get_input("Image");
81 Result &output = this->get_result("Image");
82 if (input.is_single_value()) {
83 output.share_data(input);
84 return;
85 }
86
87 const Result &displacement = this->get_input("Displacement");
88 if (displacement.is_single_value()) {
89 output.share_data(input);
91 output.get_realization_options().interpolation = this->get_interpolation();
92 output.get_realization_options().extension_x = this->get_extension_mode_x();
93 output.get_realization_options().extension_y = this->get_extension_mode_y();
94 return;
95 }
96
97 if (this->context().use_gpu()) {
98 this->execute_gpu();
99 }
100 else {
101 this->execute_cpu();
102 }
103 }
104
106 {
107 const Interpolation interpolation = this->get_interpolation();
108 gpu::Shader *shader = this->context().get_shader(this->get_shader_name(interpolation));
110
111 const Result &input_image = this->get_input("Image");
112 if (interpolation == Interpolation::Anisotropic) {
113 GPU_texture_anisotropic_filter(input_image, true);
114 GPU_texture_mipmap_mode(input_image, true, true);
115 }
116 else {
117 const bool use_bilinear = ELEM(
119 GPU_texture_filter_mode(input_image, use_bilinear);
120 }
121
122 const Extension extension_x = this->get_extension_mode_x();
123 const Extension extension_y = this->get_extension_mode_y();
126 input_image.bind_as_texture(shader, "input_tx");
127
128 const Result &displacement = this->get_input("Displacement");
129 displacement.bind_as_texture(shader, "displacement_tx");
130
131 const Domain domain = this->compute_domain();
132 Result &output_image = this->get_result("Image");
133 output_image.allocate_texture(domain);
134 output_image.bind_as_image(shader, "output_img");
135
137
138 input_image.unbind_as_texture();
139 displacement.unbind_as_texture();
140 output_image.unbind_as_image();
142 }
143
145 {
146 const Result &image = this->get_input("Image");
147 const Result &displacement = this->get_input("Displacement");
148
149 const Interpolation interpolation = this->get_interpolation();
150 const Extension extension_x = this->get_extension_mode_x();
151 const Extension extension_y = this->get_extension_mode_y();
152 const Domain domain = this->compute_domain();
153 Result &output = this->get_result("Image");
154 output.allocate_texture(domain);
155
156 const int2 size = domain.data_size;
157
158 if (interpolation == Interpolation::Anisotropic) {
159 this->compute_anisotropic(size, image, output, displacement);
160 }
161 else {
163 interpolation, size, image, output, displacement, extension_x, extension_y);
164 }
165 }
166
167 void compute_interpolation(const Interpolation &interpolation,
168 const int2 &size,
169 const Result &image,
170 Result &output,
171 const Result &displacement,
172 const Extension &extension_mode_x,
173 const Extension &extension_mode_y) const
174 {
175 parallel_for(size, [&](const int2 base_texel) {
176 const float2 coordinates = this->compute_coordinates(base_texel, size, displacement);
177 output.store_pixel(
178 base_texel,
179 image.sample<Color>(coordinates, interpolation, extension_mode_x, extension_mode_y));
180 });
181 }
182
183 /* In order to perform EWA sampling, we need to compute the partial derivative of the
184 * displaced coordinates along the x and y directions using a finite difference
185 * approximation. But in order to avoid loading multiple neighboring displacement values for
186 * each pixel, we operate on the image in 2x2 blocks of pixels, where the derivatives are
187 * computed horizontally and vertically across the 2x2 block such that odd texels use a
188 * forward finite difference equation while even invocations use a backward finite difference
189 * equation. */
190 void compute_anisotropic(const int2 &size,
191 const Result &image,
192 Result &output,
193 const Result &displacement) const
194 {
195 parallel_for(math::divide_ceil(size, int2(2)), [&](const int2 base_texel) {
196 /* Compute each of the pixels in the 2x2 block, making sure to exempt out of bounds right
197 * and upper pixels. */
198 const int x = base_texel.x * 2;
199 const int y = base_texel.y * 2;
200
201 const int2 lower_left_texel = int2(x, y);
202 const int2 lower_right_texel = int2(x + 1, y);
203 const int2 upper_left_texel = int2(x, y + 1);
204 const int2 upper_right_texel = int2(x + 1, y + 1);
205
206 const float2 lower_left_coordinates = this->compute_coordinates(
207 lower_left_texel, size, displacement);
208 const float2 lower_right_coordinates = this->compute_coordinates(
209 lower_right_texel, size, displacement);
210 const float2 upper_left_coordinates = this->compute_coordinates(
211 upper_left_texel, size, displacement);
212 const float2 upper_right_coordinates = this->compute_coordinates(
213 upper_right_texel, size, displacement);
214
215 /* Compute the partial derivatives using finite difference. */
216 const float2 lower_x_gradient = lower_right_coordinates - lower_left_coordinates;
217 const float2 left_y_gradient = upper_left_coordinates - lower_left_coordinates;
218 const float2 right_y_gradient = upper_right_coordinates - lower_right_coordinates;
219 const float2 upper_x_gradient = upper_right_coordinates - upper_left_coordinates;
220
221 /* Computes one of the 2x2 pixels given its texel location, coordinates, and gradients. */
222 auto compute_anisotropic_pixel = [&](const int2 &texel,
223 const float2 &coordinates,
224 const float2 &x_gradient,
225 const float2 &y_gradient) {
226 /* Sample the input using the displaced coordinates passing in the computed gradients in
227 * order to utilize the anisotropic filtering capabilities of the sampler. */
228 const float2x2 jacobian = float2x2(x_gradient, y_gradient);
229 output.store_pixel(texel,
230 image.sample<Color>(coordinates,
234 jacobian));
235 };
236
237 compute_anisotropic_pixel(
238 lower_left_texel, lower_left_coordinates, lower_x_gradient, left_y_gradient);
239 if (lower_right_texel.x != size.x) {
240 compute_anisotropic_pixel(
241 lower_right_texel, lower_right_coordinates, lower_x_gradient, right_y_gradient);
242 }
243 if (upper_left_texel.y != size.y) {
244 compute_anisotropic_pixel(
245 upper_left_texel, upper_left_coordinates, upper_x_gradient, left_y_gradient);
246 }
247 if (upper_right_texel.x != size.x && upper_right_texel.y != size.y) {
248 compute_anisotropic_pixel(
249 upper_right_texel, upper_right_coordinates, upper_x_gradient, right_y_gradient);
250 }
251 });
252 }
253
254 float2 compute_coordinates(const int2 &texel, const int2 &size, const Result &displacement) const
255 {
256 /* Note that the input displacement is in pixel space, so divide by the input size to
257 * transform it into the normalized sampler space. */
258 const float2 coordinates = (float2(texel) + float2(0.5f)) / float2(size);
259 return coordinates - displacement.load_pixel_extended<float2>(texel) / float2(size);
260 }
261
262 const char *get_shader_name(const Interpolation &interpolation) const
263 {
264 switch (interpolation) {
266 return "compositor_displace_anisotropic";
268 return "compositor_displace_bicubic";
271 return "compositor_displace";
272 }
273
274 return "compositor_displace";
275 }
276
278 {
279 const CMPNodeInterpolation interpolation = static_cast<CMPNodeInterpolation>(
280 this->get_input("Interpolation").get_single_value_default<MenuValue>().value);
281 switch (interpolation) {
290 }
291
293 }
294
296 {
297 const CMPExtensionMode extension_x = static_cast<CMPExtensionMode>(
298 this->get_input("Extension X").get_single_value_default<MenuValue>().value);
299 switch (extension_x) {
301 return Extension::Clip;
303 return Extension::Repeat;
305 return Extension::Extend;
306 }
307
308 return Extension::Clip;
309 }
310
312 {
313 const CMPExtensionMode extension_y = static_cast<CMPExtensionMode>(
314 this->get_input("Extension Y").get_single_value_default<MenuValue>().value);
315 switch (extension_y) {
317 return Extension::Clip;
319 return Extension::Repeat;
321 return Extension::Extend;
322 }
323
324 return Extension::Clip;
325 }
326};
327
329{
330 return new DisplaceOperation(context, node);
331}
332
333static void node_register()
334{
335 static bke::bNodeType ntype;
336
337 cmp_node_type_base(&ntype, "CompositorNodeDisplace"_ustr, CMP_NODE_DISPLACE);
338 ntype.ui_name = "Displace";
339 ntype.ui_description = "Displace pixel position using an offset vector";
340 ntype.enum_name_legacy = "DISPLACE";
342 ntype.declare = node_declare;
343 ntype.initfunc = node_init;
345 ntype, "NodeDisplaceData", node_free_standard_storage, node_copy_standard_storage);
347
349}
351
352} // namespace blender::nodes::node_composite_displace_cc
#define NODE_CLASS_DISTORT
Definition BKE_node.hh:486
#define CMP_NODE_DISPLACE
Read Blender memory allocation and freeing.
#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
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 bind_as_texture(gpu::Shader *shader, const char *texture_name) const
Definition result.cc:854
T load_pixel_extended(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
const T & get_single_value() const
DeclType::Builder & add_input(UString name, UString identifier=""_ustr)
void compute_interpolation(const Interpolation &interpolation, const int2 &size, const Result &image, Result &output, const Result &displacement, const Extension &extension_mode_x, const Extension &extension_mode_y) const
float2 compute_coordinates(const int2 &texel, const int2 &size, const Result &displacement) const
const char * get_shader_name(const Interpolation &interpolation) const
void compute_anisotropic(const int2 &size, const Result &image, Result &output, const Result &displacement) const
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)
VecBase< T, Size > divide_ceil(const VecBase< T, Size > &a, const VecBase< T, Size > &b)
MatT from_location(const typename MatT::loc_type &location)
static void node_init(bNodeTree *, bNode *node)
static void node_declare(NodeDeclarationBuilder &b)
static NodeOperation * get_compositor_operation(Context &context, const 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_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)
@ 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
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)
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