Blender V5.3
node_composite_lens_distortion.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_base_c.hh"
7#include "BLI_math_vector.hh"
9#include "BLI_noise.hh"
10
11#include "RNA_types.hh"
12
13#include "GPU_shader.hh"
14#include "GPU_texture.hh"
15
16#include "COM_node_operation.hh"
17#include "COM_utilities.hh"
18
20
21/* Distortion can't be exactly -1.0 as it will cause infinite pincushion distortion. */
22#define MINIMUM_DISTORTION -0.999f
23/* Arbitrary scaling factor for the dispersion input in horizontal distortion mode. */
24#define HORIZONTAL_DISPERSION_SCALE 5.0f
25/* Arbitrary scaling factor for the dispersion input in radial distortion mode. */
26#define RADIAL_DISPERSION_SCALE 4.0f
27/* Arbitrary scaling factor for the distortion input. */
28#define DISTORTION_SCALE 4.0f
29
31
32static const EnumPropertyItem type_items[] = {
34 "RADIAL",
35 0,
36 N_("Radial"),
37 N_("Radially distorts the image to create a barrel or a Pincushion distortion")},
39 "HORIZONTAL",
40 0,
41 N_("Horizontal"),
42 N_("Horizontally distorts the image to create a channel/color shifting effect")},
43 {0, nullptr, 0, nullptr, nullptr},
44};
45
47{
48 b.use_custom_socket_order();
49 b.allow_any_socket_order();
50 b.add_input<decl::Color>("Image"_ustr)
51 .default_value({1.0f, 1.0f, 1.0f, 1.0f})
52 .hide_value()
53 .structure_type(StructureType::Dynamic);
54 b.add_output<decl::Color>("Image"_ustr)
55 .structure_type(StructureType::Dynamic)
56 .align_with_previous();
57
58 b.add_input<decl::Menu>("Type"_ustr)
60 .static_items(type_items)
62 b.add_input<decl::Float>("Distortion"_ustr)
63 .default_value(0.0f)
66 .max(1.0f)
67 .usage_by_single_menu(CMP_NODE_LENS_DISTORTION_RADIAL)
68 .description(
69 "The amount of distortion. 0 means no distortion, -1 means full Pincushion distortion, "
70 "and 1 means full Barrel distortion");
71 b.add_input<decl::Float>("Dispersion"_ustr)
72 .default_value(0.0f)
74 .min(0.0f)
75 .max(1.0f)
76 .description("The amount of chromatic aberration to add to the distortion");
77 b.add_input<decl::Bool>("Jitter"_ustr)
78 .default_value(false)
79 .usage_by_single_menu(CMP_NODE_LENS_DISTORTION_RADIAL)
81 "Introduces jitter while doing distortion, which can be faster but can produce grainy "
82 "or noisy results");
83 b.add_input<decl::Bool>("Fit"_ustr)
84 .default_value(false)
85 .usage_by_single_menu(CMP_NODE_LENS_DISTORTION_RADIAL)
87 "Scales the image such that it fits entirely in the frame, leaving no empty spaces at "
88 "the corners");
89}
90
91static void node_init(bNodeTree * /*ntree*/, bNode *node)
92{
93 /* Unused, kept for forward compatibility. */
94 NodeLensDist *data = MEM_new<NodeLensDist>(__func__);
95 node->storage = data;
96}
97
98using namespace blender::compositor;
99
100/* --------------------------------------------------------------------
101 * Screen Lens Distortion
102 */
103
104/* A model that approximates lens distortion parameterized by a distortion parameter and dependent
105 * on the squared distance to the center of the image. The distorted pixel is then computed as the
106 * scalar multiplication of the pixel coordinates with the value returned by this model. See the
107 * compute_distorted_uv function for more details. */
108static float compute_distortion_scale(const float distortion, const float distance_squared)
109{
110 return 1.0f / (1.0f + math::sqrt(math::max(0.0f, 1.0f - distortion * distance_squared)));
111}
112
113/* A vectorized version of compute_distortion_scale that is applied on the chromatic distortion
114 * parameters passed to the shader. */
115static float3 compute_chromatic_distortion_scale(const float3 &chromatic_distortion,
116 const float distance_squared)
117{
118 return 1.0f / (1.0f + math::sqrt(math::max(float3(0.0f),
119 1.0f - chromatic_distortion * distance_squared)));
120}
121
122/* Compute the image coordinates after distortion by the given distortion scale computed by the
123 * compute_distortion_scale function. Note that the function expects centered normalized UV
124 * coordinates but outputs non-centered image coordinates. */
125static float2 compute_distorted_uv(const float2 &uv, const float uv_scale, const int2 &size)
126{
127 return (uv * uv_scale + 0.5f) * float2(size);
128}
129
130/* Compute the number of integration steps that should be used to approximate the distorted pixel
131 * using a heuristic, see the compute_number_of_steps function for more details. The numbers of
132 * steps is proportional to the number of pixels spanned by the distortion amount. For jitter
133 * distortion, the square root of the distortion amount plus 1 is used with a minimum of 2 steps.
134 * For non-jitter distortion, the distortion amount plus 1 is used as the number of steps */
135static int compute_number_of_integration_steps_heuristic(const float distortion,
136 const bool use_jitter)
137{
138 if (use_jitter) {
139 return distortion < 4.0f ? 2 : int(math::sqrt(distortion + 1.0f));
140 }
141 return int(distortion + 1.0f);
142}
143
144/* Compute the number of integration steps that should be used to compute each channel of the
145 * distorted pixel. Each of the channels are distorted by their respective chromatic distortion
146 * amount, then the amount of distortion between each two consecutive channels is computed, this
147 * amount is then used to heuristically infer the number of needed integration steps, see the
148 * integrate_distortion function for more information. */
149static int4 compute_number_of_integration_steps(const float3 &chromatic_distortion,
150 const int2 &size,
151 const float2 &uv,
152 const float distance_squared,
153 const bool use_jitter)
154{
155 /* Distort each channel by its respective chromatic distortion amount. */
156 float3 distortion_scale = compute_chromatic_distortion_scale(chromatic_distortion,
158 float2 distorted_uv_red = compute_distorted_uv(uv, distortion_scale.x, size);
159 float2 distorted_uv_green = compute_distorted_uv(uv, distortion_scale.y, size);
160 float2 distorted_uv_blue = compute_distorted_uv(uv, distortion_scale.z, size);
161
162 /* Infer the number of needed integration steps to compute the distorted red channel starting
163 * from the green channel. */
164 float distortion_red = math::distance(distorted_uv_red, distorted_uv_green);
165 int steps_red = compute_number_of_integration_steps_heuristic(distortion_red, use_jitter);
166
167 /* Infer the number of needed integration steps to compute the distorted blue channel starting
168 * from the green channel. */
169 float distortion_blue = math::distance(distorted_uv_green, distorted_uv_blue);
170 int steps_blue = compute_number_of_integration_steps_heuristic(distortion_blue, use_jitter);
171
172 /* The number of integration steps used to compute the green and the alpha channels is the sum
173 * of both the red and the blue channels steps because they are computed once with each of them.
174 */
175 return int4(steps_red, steps_red + steps_blue, steps_blue, steps_red + steps_blue);
176}
177
178/* Returns a random jitter amount, which is essentially a random value in the [0, 1] range. If
179 * jitter is not enabled, return a constant 0.5 value instead. */
180static float get_jitter(const int2 &texel, const int seed, const bool use_jitter)
181{
182 if (use_jitter) {
183 return noise::hash_to_float(texel.x, texel.y, seed);
184 }
185 return 0.5f;
186}
187
188/* Each color channel may have a different distortion with the guarantee that the red will have the
189 * lowest distortion while the blue will have the highest one. If each channel is distorted
190 * independently, the image will look disintegrated, with each channel seemingly merely shifted.
191 * Consequently, the distorted pixels needs to be computed by integrating along the path of change
192 * of distortion starting from one channel to another. For instance, to compute the distorted red
193 * from the distorted green, we accumulate the color of the distorted pixel starting from the
194 * distortion of the red, taking small steps until we reach the distortion of the green. The pixel
195 * color is weighted such that it is maximum at the start distortion and zero at the end distortion
196 * in an arithmetic progression. The integration steps can be augmented with random values to
197 * simulate lens jitter. Finally, it should be noted that this function integrates both the start
198 * and end channels in reverse directions for more efficient computation. */
199static float4 integrate_distortion(const int2 &texel,
200 const Result &input,
201 const int2 &size,
202 const float3 &chromatic_distortion,
203 const int start,
204 const int end,
205 const float distance_squared,
206 const float2 &uv,
207 const int steps,
208 const bool use_jitter)
209{
210 float4 accumulated_color = float4(0.0f);
211 float distortion_amount = chromatic_distortion[end] - chromatic_distortion[start];
212 for (int i = 0; i < steps; i++) {
213 /* The increment will be in the [0, 1) range across iterations. Include the start channel in
214 * the jitter seed to make sure each channel gets a different jitter. */
215 float increment = (i + get_jitter(texel, start * steps + i, use_jitter)) / steps;
216 float distortion = chromatic_distortion[start] + increment * distortion_amount;
217 float distortion_scale = compute_distortion_scale(distortion, distance_squared);
218
219 /* Sample the color at the distorted coordinates and accumulate it weighted by the increment
220 * value for both the start and end channels. */
221 float2 distorted_uv = compute_distorted_uv(uv, distortion_scale, size);
222 float4 color = float4(input.sample_bilinear_zero<Color>(distorted_uv / float2(size)));
223 accumulated_color[start] += (1.0f - increment) * color[start];
224 accumulated_color[end] += increment * color[end];
225 accumulated_color.w += color.w;
226 }
227 return accumulated_color;
228}
229
230static void radial_lens_distortion(const int2 texel,
231 const Result &input,
232 Result &output,
233 const int2 &size,
234 const float3 &chromatic_distortion,
235 const float scale,
236 const bool use_jitter)
237{
238 /* Compute the UV image coordinates in the range [-1, 1] as well as the squared distance to the
239 * center of the image, which is at (0, 0) in the UV coordinates. */
240 float2 center = float2(size) / 2.0f;
241 float2 uv = scale * (float2(texel) + float2(0.5f) - center) / center;
242 float distance_squared = math::dot(uv, uv);
243
244 /* If any of the color channels will get distorted outside of the screen beyond what is possible,
245 * write a zero transparent color and return. */
246 float3 distortion_bounds = chromatic_distortion * distance_squared;
247 if (distortion_bounds.x > 1.0f || distortion_bounds.y > 1.0f || distortion_bounds.z > 1.0f) {
248 output.store_pixel(texel, Color(float4(0.0f)));
249 return;
250 }
251
252 /* Compute the number of integration steps that should be used to compute each channel of the
253 * distorted pixel. */
255 chromatic_distortion, size, uv, distance_squared, use_jitter);
256
257 /* Integrate the distortion of the red and green, then the green and blue channels. That means
258 * the green will be integrated twice, but this is accounted for in the number of steps which the
259 * color will later be divided by. See the compute_number_of_integration_steps function for more
260 * details. */
261 float4 color = float4(0.0f);
263 input,
264 size,
265 chromatic_distortion,
266 0,
267 1,
269 uv,
270 number_of_steps.x,
271 use_jitter);
273 input,
274 size,
275 chromatic_distortion,
276 1,
277 2,
279 uv,
280 number_of_steps.z,
281 use_jitter);
282
283 /* The integration above performed weighted accumulation, and thus the color needs to be divided
284 * by the sum of the weights. Assuming no jitter, the weights are generated as an arithmetic
285 * progression starting from (0.5 / n) to ((n - 0.5) / n) for n terms. The sum of an arithmetic
286 * progression can be computed as (n * (start + end) / 2), which when subsisting the start and
287 * end reduces to (n / 2). So the color should be multiplied by 2 / n. On the other hand alpha
288 * is not weighted by the arithmetic progression, so it is multiplied by (1.0) and it is
289 * normalized by averaging only (i.e. division by (n)). The jitter sequence approximately sums to
290 * the same value because it is a uniform random value whose mean value is 0.5, so the expression
291 * doesn't change regardless of jitter. */
292 color *= float4(float3(2.0f), 1.0f) / float4(number_of_steps);
293
294 output.store_pixel(texel, Color(color));
295}
296
298 public:
300
301 void execute() override
302 {
303 const Result &input = this->get_input("Image");
304 Result &output = this->get_result("Image");
305
306 if (this->is_identity()) {
307 output.share_data(input);
308 return;
309 }
310
311 switch (this->get_type()) {
314 return;
317 return;
318 }
319
320 output.share_data(input);
321 }
322
324 {
325 if (this->context().use_gpu()) {
327 }
328 else {
330 }
331 }
332
334 {
335 gpu::Shader *shader = context().get_shader("compositor_horizontal_lens_distortion");
337
338 const Result &input_image = get_input("Image");
339 GPU_texture_filter_mode(input_image, true);
341 input_image.bind_as_texture(shader, "input_tx");
342
343 const Domain domain = compute_domain();
344
345 const float dispersion = (get_dispersion() * HORIZONTAL_DISPERSION_SCALE) / domain.data_size.x;
346 GPU_shader_uniform_1f(shader, "dispersion", dispersion);
347
348 Result &output_image = get_result("Image");
349 output_image.allocate_texture(domain);
350 output_image.bind_as_image(shader, "output_img");
351
353
354 input_image.unbind_as_texture();
355 output_image.unbind_as_image();
357 }
358
360 {
361 const Domain domain = compute_domain();
362 const float dispersion = (get_dispersion() * HORIZONTAL_DISPERSION_SCALE) / domain.data_size.x;
363
364 const Result &input = get_input("Image");
365
366 Result &output = get_result("Image");
367 output.allocate_texture(domain);
368
369 const int2 size = domain.data_size;
370 parallel_for(size, [&](const int2 texel) {
371 /* Get the normalized coordinates of the pixel centers. */
372 float2 normalized_texel = (float2(texel) + float2(0.5f)) / float2(size);
373
374 /* Sample the red and blue channels shifted by the dispersion amount. */
375 const float4 red = float4(
376 input.sample_bilinear_zero<Color>(normalized_texel + float2(dispersion, 0.0f)));
377 const float4 green = float4(input.load_pixel<Color>(texel));
378 const float4 blue = float4(
379 input.sample_bilinear_zero<Color>(normalized_texel - float2(dispersion, 0.0f)));
380
381 const float alpha = math::dot(float3(red.w, green.w, blue.w), float3(1.0f)) / 3.0f;
382
383 output.store_pixel(texel, Color(red.x, green.y, blue.z, alpha));
384 });
385 }
386
388 {
389 if (this->context().use_gpu()) {
391 }
392 else {
394 }
395 }
396
398 {
401
402 const Result &input_image = get_input("Image");
403 GPU_texture_filter_mode(input_image, true);
405 input_image.bind_as_texture(shader, "input_tx");
406
407 const Domain domain = compute_domain();
408
409 const float3 chromatic_distortion = compute_chromatic_distortion();
410 GPU_shader_uniform_3fv(shader, "chromatic_distortion", chromatic_distortion);
411
413
414 Result &output_image = get_result("Image");
415 output_image.allocate_texture(domain);
416 output_image.bind_as_image(shader, "output_img");
417
419
420 input_image.unbind_as_texture();
421 output_image.unbind_as_image();
423 }
424
426 {
427 if (get_use_jitter()) {
428 return "compositor_radial_lens_distortion_jitter";
429 }
430 return "compositor_radial_lens_distortion";
431 }
432
434 {
435 const float scale = this->compute_scale();
436 const bool use_jitter = this->get_use_jitter();
437 const float3 chromatic_distortion = this->compute_chromatic_distortion();
438
439 const Result &input = get_input("Image");
440
441 const Domain domain = compute_domain();
442 Result &output = get_result("Image");
443 output.allocate_texture(domain);
444
445 const int2 size = domain.data_size;
446 parallel_for(size, [&](const int2 texel) {
447 radial_lens_distortion(texel, input, output, size, chromatic_distortion, scale, use_jitter);
448 });
449 }
450
452 {
453 const Result &input = get_input("Distortion");
454 return clamp_f(input.get_single_value_default<float>(), MINIMUM_DISTORTION, 1.0f);
455 }
456
458 {
459 const Result &input = get_input("Dispersion");
460 return clamp_f(input.get_single_value_default<float>(), 0.0f, 1.0f);
461 }
462
463 /* Get the distortion amount for each channel. The green channel has a distortion amount that
464 * matches that specified in the node inputs, while the red and blue channels have higher and
465 * lower distortion amounts respectively based on the dispersion value. */
467 {
468 const float green_distortion = get_distortion();
469 const float dispersion = get_dispersion() / RADIAL_DISPERSION_SCALE;
470 const float red_distortion = clamp_f(green_distortion + dispersion, MINIMUM_DISTORTION, 1.0f);
471 const float blue_distortion = clamp_f(green_distortion - dispersion, MINIMUM_DISTORTION, 1.0f);
472 return float3(red_distortion, green_distortion, blue_distortion) * DISTORTION_SCALE;
473 }
474
475 /* The distortion model will distort the image in such a way that the result will no longer
476 * fit the domain of the original image, so we scale the image to account for that. If get_is_fit
477 * is false, then the scaling factor will be such that the furthest pixels horizontally and
478 * vertically are at the boundary of the image. Otherwise, if get_is_fit is true, the scaling
479 * factor will be such that the furthest pixels diagonally are at the corner of the image. */
481 {
483 const float maximum_distortion = std::max({distortion[0], distortion[1], distortion[2]});
484
485 if (get_is_fit() && (maximum_distortion > 0.0f)) {
486 return 1.0f / (1.0f + 2.0f * maximum_distortion);
487 }
488 return 1.0f / (1.0f + maximum_distortion);
489 }
490
492 {
494 this->get_input("Type").get_single_value_default<MenuValue>().value);
495 }
496
498 {
499 return this->get_input("Jitter").get_single_value_default<bool>();
500 }
501
503 {
504 return this->get_input("Fit").get_single_value_default<bool>();
505 }
506
507 /* Returns true if the operation does nothing and the input can be passed through. */
509 {
510 /* The input is a single value and the operation does nothing. */
511 if (this->get_input("Image").is_single_value()) {
512 return true;
513 }
514
515 /* Projector have zero dispersion and does nothing. */
517 return this->get_dispersion() == 0.0f;
518 }
519
520 /* Both distortion and dispersion are zero and the operation does nothing. Jittering has an
521 * effect regardless, so its gets an exemption. */
522 if (!this->get_use_jitter() && this->get_distortion() == 0.0f &&
523 this->get_dispersion() == 0.0f)
524 {
525 return true;
526 }
527
528 return false;
529 }
530};
531
533{
534 return new LensDistortionOperation(context, node);
535}
536
537static void node_register()
538{
539 static bke::bNodeType ntype;
540
541 cmp_node_type_base(&ntype, "CompositorNodeLensdist"_ustr, CMP_NODE_LENSDIST);
542 ntype.ui_name = "Lens Distortion";
543 ntype.ui_description = "Simulate distortion and dispersion from camera lenses";
544 ntype.enum_name_legacy = "LENSDIST";
546 ntype.declare = node_declare;
547 ntype.initfunc = node_init;
551
553}
555
556} // namespace blender::nodes::node_composite_lens_distortion_cc
#define NODE_CLASS_DISTORT
Definition BKE_node.hh:486
#define CMP_NODE_LENSDIST
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#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 sample_bilinear_zero(const float2 &coordinates) const
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
float3x3 scale(float3x3 mat, float2 scale)
float distance_squared(VecT a, VecT b)
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
void parallel_for(const int2 range, const Function &function)
T sqrt(const T &a)
T distance(const T &a, const T &b)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
T max(const T &a, const T &b)
static float4 integrate_distortion(const int2 &texel, const Result &input, const int2 &size, const float3 &chromatic_distortion, const int start, const int end, const float distance_squared, const float2 &uv, const int steps, const bool use_jitter)
static float2 compute_distorted_uv(const float2 &uv, const float uv_scale, const int2 &size)
static int compute_number_of_integration_steps_heuristic(const float distortion, const bool use_jitter)
static float compute_distortion_scale(const float distortion, const float distance_squared)
static float get_jitter(const int2 &texel, const int seed, const bool use_jitter)
static NodeOperation * get_compositor_operation(Context &context, const bNode &node)
static void radial_lens_distortion(const int2 texel, const Result &input, Result &output, const int2 &size, const float3 &chromatic_distortion, const float scale, const bool use_jitter)
static float3 compute_chromatic_distortion_scale(const float3 &chromatic_distortion, const float distance_squared)
static int4 compute_number_of_integration_steps(const float3 &chromatic_distortion, const int2 &size, const float2 &uv, const float distance_squared, const bool use_jitter)
float hash_to_float(uint32_t kx)
Definition noise.cc:232
@ GPU_SAMPLER_EXTEND_MODE_CLAMP_TO_BORDER
@ CMP_NODE_LENS_DISTORTION_HORIZONTAL
@ CMP_NODE_LENS_DISTORTION_RADIAL
BMesh const char void * data
void GPU_shader_unbind()
void GPU_shader_uniform_3fv(gpu::Shader *sh, const char *name, const float data[3])
void GPU_texture_filter_mode(gpu::Texture *texture, bool use_filter)
void GPU_shader_bind(gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_texture_extend_mode(gpu::Texture *texture, GPUSamplerExtendMode extend_mode)
void GPU_shader_uniform_1f(gpu::Shader *sh, const char *name, float value)
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
MINLINE float clamp_f(float value, float min, float max)
void cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
#define DISTORTION_SCALE
#define HORIZONTAL_DISPERSION_SCALE
#define MINIMUM_DISTORTION
#define RADIAL_DISPERSION_SCALE
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)