Blender V5.3
realize_on_domain_operation.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include <limits>
6
7#include "BLI_math_matrix.hh"
10
11#include "GPU_shader.hh"
12#include "GPU_texture.hh"
13
14#include "COM_context.hh"
15#include "COM_domain.hh"
17#include "COM_result.hh"
18#include "COM_utilities.hh"
19
21
22namespace blender::compositor {
23
24/* ------------------------------------------------------------------------------------------------
25 * Realize On Domain Operation
26 */
27
29 Domain target_domain,
30 ResultType type)
31 : SimpleOperation(context), target_domain_(target_domain)
32{
33 InputDescriptor input_descriptor;
34 input_descriptor.type = type;
35 this->declare_input_descriptor(input_descriptor);
36 this->populate_result(type);
37}
38
40{
41 const Domain input_domain = this->get_input().domain();
42 const Domain output_domain = target_domain_;
43
44 /* Create a transformation matrix that transforms the pixels in the data window from the data
45 * space to the virtual compositing space. This is done by first adding the data offset to go
46 * from the data space to the display space, then subtracting the center of the display window to
47 * go from the display space to the virtual compositing space. See the corrective translation
48 * function for more information on its function. */
49 const float2 input_center = float2(input_domain.display_size) / 2.0f;
50 const float2 input_translation = float2(input_domain.data_offset) - input_center +
51 this->compute_corrective_translation();
52 const float3x3 input_data_to_virtual = math::translate(input_domain.transformation,
53 input_translation);
54
55 /* Same as above but for the output domain. */
56 const float2 output_center = float2(output_domain.display_size) / 2.0f;
57 const float2 output_translation = float2(output_domain.data_offset) - output_center;
58 const float3x3 output_data_to_virtual = math::translate(output_domain.transformation,
59 output_translation);
60
61 /* Create a transformation matrix from the output data space to the input data space */
62 const float3x3 virtual_to_input_data = math::invert(input_data_to_virtual);
63 const float3x3 output_data_to_input_data = virtual_to_input_data * output_data_to_virtual;
64
65 /* Create a transformation matrix from the output integer texel to the input normalized sampler
66 * coordinates. This is done by adding 0.5 to evaluate the output at the center if pixels and
67 * dividing by the input size to get normalized coordinates. */
68 const float3x3 output_texel_to_output_data = math::from_location<float3x3>(float2(0.5f));
69 const float3x3 input_data_to_input_sampler = math::from_scale<float3x3, 2>(
70 1.0f / float2(input_domain.data_size));
71 const float3x3 output_texel_to_input_sampler = input_data_to_input_sampler *
72 output_data_to_input_data *
73 output_texel_to_output_data;
74
75 if (this->context().use_gpu()) {
76 this->realize_on_domain_gpu(output_texel_to_input_sampler);
77 }
78 else {
79 this->realize_on_domain_cpu(output_texel_to_input_sampler);
80 }
81}
82
83float2 RealizeOnDomainOperation::compute_corrective_translation()
84{
85 if (this->get_input().get_realization_options().interpolation == Interpolation::Nearest) {
86 /* Bias translations in case of nearest interpolation to avoids the round-to-even behavior of
87 * some GPUs at pixel boundaries. */
88 return float2(std::numeric_limits<float>::epsilon() * 10e3f);
89 }
90
91 /* Assuming no transformations, if the input size is odd and output size is even or vice versa,
92 * the centers of pixels of the input and output will be half a pixel away from each other due
93 * to the centering translation. Which introduce fuzzy result due to interpolation. So if one
94 * is odd and the other is even, detected by testing the low bit of the xor of the sizes, shift
95 * the input by 1/2 pixel so the pixels align. */
96 const int2 output_size = this->compute_domain().data_size;
97 const int2 input_size = this->get_input().domain().data_size;
98 return float2(((input_size[0] ^ output_size[0]) & 1) ? -0.5f : 0.0f,
99 ((input_size[1] ^ output_size[1]) & 1) ? -0.5f : 0.0f);
100}
101
102std::optional<float2x2> RealizeOnDomainOperation::compute_jacobian(const float3x3 &transformation)
103{
104 /* The Jacobian of a 2D transformation matrix is the matrix itself. */
105 const float2x2 jacobian = float2x2(transformation);
106
107 /* The gradients of the input across the output are the rows of the Jacobian. */
108 const float2 x_gradient = float2(jacobian[0][0], jacobian[1][0]);
109 const float2 y_gradient = float2(jacobian[0][1], jacobian[1][1]);
110
111 /* Compute the size of the input pixels in normalized space. */
112 const float2 pixel_size = math::rcp(float2(this->get_input().domain().data_size));
113
114 /* If both gradients are less than that the input pixel size with a tolerance ratio of 1.1, then
115 * each output pixel does not cover multiple input pixels and we can do simple point sampling,
116 * otherwise, each output pixel covers multiple input pixels and we do area sampling. */
117 constexpr float tolerance_ratio = 1.1f;
118 if (math::length(x_gradient) < pixel_size.x * tolerance_ratio &&
119 math::length(y_gradient) < pixel_size.y * tolerance_ratio)
120 {
121 return std::nullopt;
122 }
123
124 return jacobian;
125}
126
127void RealizeOnDomainOperation::realize_on_domain_gpu(const float3x3 &transformation)
128{
129 gpu::Shader *shader = this->context().get_shader(
130 this->get_realization_shader_name(transformation));
131 GPU_shader_bind(shader);
132
133 GPU_shader_uniform_mat3_as_mat4(shader, "transformation", transformation.ptr());
134
135 Result &input = this->get_input();
136 const RealizationOptions realization_options = input.get_realization_options();
137
138 if (!GPU_texture_has_integer_format(input)) {
139 /* The texture sampler should use bilinear interpolation for both the bilinear and bicubic
140 * cases, as the logic used by the bicubic realization shader expects textures to use bilinear
141 * interpolation. */
142 if (realization_options.interpolation == Interpolation::Anisotropic) {
144 GPU_texture_mipmap_mode(input, true, true);
145 }
146 else {
147 GPU_texture_filter_mode(input, realization_options.interpolation != Interpolation::Nearest);
148 }
149 }
150
152 map_extension_mode_to_extend_mode(realization_options.extension_x));
154 map_extension_mode_to_extend_mode(realization_options.extension_y));
155
156 input.bind_as_texture(shader, "input_tx");
157
158 const Domain domain = this->compute_domain();
159 Result &output = this->get_result();
160 output.allocate_texture(domain);
161 output.bind_as_image(shader, "domain_img");
162
163 compute_dispatch_threads_at_least(shader, output.domain().data_size);
164
165 input.unbind_as_texture();
166 output.unbind_as_image();
168}
169
170const char *RealizeOnDomainOperation::get_realization_shader_name(const float3x3 &transformation)
171{
173 if (interpolation == Interpolation::Bicubic) {
174 switch (this->get_input().type()) {
176 return "compositor_realize_on_domain_bicubic_float";
178 return "compositor_realize_on_domain_bicubic_float2";
180 /* Float3 is internally stored in a float4 texture due to GPU module limitations. */
181 return "compositor_realize_on_domain_bicubic_float4";
183 return "compositor_realize_on_domain_bicubic_float4";
185 return "compositor_realize_on_domain_bicubic_float4";
186 case ResultType::Int:
187 return "compositor_realize_on_domain_int";
188 case ResultType::Int2:
189 return "compositor_realize_on_domain_int2";
190 case ResultType::Int3:
191 /* Int3 is internally stored in a int4 texture due to GPU module limitations. */
192 return "compositor_realize_on_domain_int4";
193 case ResultType::Int4:
194 return "compositor_realize_on_domain_int4";
195 case ResultType::Bool:
196 return "compositor_realize_on_domain_bool";
198 return "compositor_realize_on_domain_float4x4";
199 case ResultType::Menu:
200 return "compositor_realize_on_domain_menu";
202 return "compositor_realize_on_domain_bicubic_float4";
206 case ResultType::Font:
208 case ResultType::Text:
209 case ResultType::Mask:
212 /* Single only types do not support GPU code path. */
215 break;
216 }
217 }
218 else {
219 switch (this->get_input().type()) {
221 return "compositor_realize_on_domain_float";
223 return "compositor_realize_on_domain_float2";
225 /* Float3 is internally stored in a float4 texture due to GPU module limitations. */
226 return "compositor_realize_on_domain_float4";
228 case ResultType::Color: {
229 const std::optional<float2x2> jacobian = this->compute_jacobian(transformation);
230 return interpolation == Interpolation::Anisotropic && jacobian.has_value() ?
231 "compositor_realize_on_domain_anisotropic_float4" :
232 "compositor_realize_on_domain_float4";
233 }
234 case ResultType::Int:
235 return "compositor_realize_on_domain_int";
236 case ResultType::Int2:
237 return "compositor_realize_on_domain_int2";
238 case ResultType::Int3:
239 /* Int3 is internally stored in a int4 texture due to GPU module limitations. */
240 return "compositor_realize_on_domain_int4";
241 case ResultType::Int4:
242 return "compositor_realize_on_domain_int4";
243 case ResultType::Bool:
244 return "compositor_realize_on_domain_bool";
246 return "compositor_realize_on_domain_float4x4";
247 case ResultType::Menu:
248 return "compositor_realize_on_domain_menu";
250 return "compositor_realize_on_domain_float4";
254 case ResultType::Font:
256 case ResultType::Text:
257 case ResultType::Mask:
260 /* Single only types do not support GPU code path. */
263 break;
264 }
265 }
266
268 return nullptr;
269}
270
271template<typename T>
272static void realize_on_domain(const Result &input,
273 Result &output,
274 const float3x3 &transformation,
275 const std::optional<float2x2> &jacobian)
276{
277 const RealizationOptions realization_options = input.get_realization_options();
278 parallel_for(output.domain().data_size, [&](const int2 texel) {
279 const float2 coordinates = math::transform_point(transformation, float2(texel));
280 T sample = input.sample<T>(coordinates,
281 realization_options.interpolation,
282 realization_options.extension_x,
283 realization_options.extension_y,
284 jacobian);
285 output.store_pixel(texel, sample);
286 });
287}
288
289void RealizeOnDomainOperation::realize_on_domain_cpu(const float3x3 &transformation)
290{
291 Result &input = this->get_input();
292 Result &output = this->get_result();
293
294 const Domain domain = this->compute_domain();
295 output.allocate_texture(domain);
296
297 const std::optional<float2x2> jacobian = this->compute_jacobian(transformation);
298
299 input.get_cpp_type()
300 .to_static_type<float,
301 float2,
302 float3,
303 float4,
304 Color,
305 int32_t,
306 int2,
307 int3,
308 int4,
309 bool,
310 float4x4,
313 [&]<typename T>() { realize_on_domain<T>(input, output, transformation, jacobian); });
314}
315
317{
318 return target_domain_;
319}
320
323 const Result &input_result,
324 const InputDescriptor &input_descriptor,
325 const Domain &operation_domain)
326{
327 /* This input doesn't need realization, the operation is not needed. */
328 if (input_descriptor.realization_mode == InputRealizationMode::None) {
329 return nullptr;
330 }
331
332 /* The input expects a single value and if no single value is provided, it will be ignored and a
333 * default value will be used, so no need to realize it and the operation is not needed. */
334 if (input_descriptor.expects_single_value) {
335 return nullptr;
336 }
337
338 /* Input result is a single value and does not need realization, the operation is not needed. */
339 if (input_result.is_single_value()) {
340 return nullptr;
341 }
342
343 /* If we are realizing on the operation domain, then our target domain is the operation domain,
344 * otherwise, we are only realizing the transforms, then our target domain is the input's one. */
345 const bool use_operation_domain = input_descriptor.realization_mode ==
347 const Domain target_domain = use_operation_domain ? operation_domain : input_result.domain();
348
349 const bool should_realize_translation = input_descriptor.realization_mode ==
351 const Domain realized_target_domain = target_domain.realize_transformation(
352 should_realize_translation);
353
354 /* The input have an almost identical domain to the realized target domain, so no need to realize
355 * it and the operation is not needed. */
356 if (Domain::is_equal(input_result.domain(), realized_target_domain)) {
357 return nullptr;
358 }
359
360 return new RealizeOnDomainOperation(context, realized_target_domain, input_descriptor.type);
361}
362
363} // namespace blender::compositor
#define BLI_assert_unreachable()
#define BLI_assert(a)
#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
static bool is_equal(const Domain &a, const Domain &b, const float epsilon=10e-6f)
Definition domain.cc:52
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
RealizeOnDomainOperation(Context &context, Domain target_domain, ResultType type)
static SimpleOperation * construct_if_needed(Context &context, const Result &input_result, const InputDescriptor &input_descriptor, const Domain &operation_domain)
RealizationOptions & get_realization_options()
Definition result.cc:1069
static bool is_single_value_only_type(ResultType type)
Definition result.cc:51
void declare_input_descriptor(InputDescriptor descriptor)
MatBase< 2, 2 > float2x2
#define T
static void realize_on_domain(const Result &input, Result &output, const float3x3 &transformation, const std::optional< float2x2 > &jacobian)
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)
QuaternionBase< float > Quaternion
T length(const VecBase< T, Size > &a)
CartesianBasis invert(const CartesianBasis &basis)
MatT from_scale(const VecBase< typename MatT::base_type, ScaleDim > &scale)
T rcp(const T &a)
MatBase< T, NumCol, NumRow > translate(const MatBase< T, NumCol, NumRow > &mat, const VectorT &translation)
MatT from_location(const typename MatT::loc_type &location)
bool GPU_texture_has_integer_format(const gpu::Texture *texture)
VecBase< int32_t, 4 > int4
void GPU_texture_anisotropic_filter(gpu::Texture *texture, bool use_aniso)
MatBase< float, 4, 4 > float4x4
VecBase< float, 4 > float4
void GPU_texture_mipmap_mode(gpu::Texture *texture, bool use_mipmap, bool use_filter)
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
VecBase< int32_t, 3 > int3
void GPU_shader_unbind()
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)
MatBase< float, 3, 3 > float3x3
void GPU_texture_extend_mode_x(gpu::Texture *texture, GPUSamplerExtendMode extend_mode)
void GPU_texture_extend_mode_y(gpu::Texture *texture, GPUSamplerExtendMode extend_mode)
signed int int32_t
Definition BKE_text.h:78
VecBase< float, 3 > float3
void GPU_shader_uniform_mat3_as_mat4(gpu::Shader *sh, const char *name, const float data[3][3])
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
float x
float y