Blender V5.3
node_composite_directional_blur.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"
7
8#include "GPU_shader.hh"
9
10#include "COM_node_operation.hh"
11#include "COM_utilities.hh"
12
14
16
18{
19 b.use_custom_socket_order();
20 b.allow_any_socket_order();
21
22 b.add_input<decl::Color>("Image"_ustr)
23 .default_value({1.0f, 1.0f, 1.0f, 1.0f})
24 .hide_value()
25 .structure_type(StructureType::Dynamic);
26 b.add_output<decl::Color>("Image"_ustr)
27 .structure_type(StructureType::Dynamic)
28 .align_with_previous();
29
30 b.add_input<decl::Int>("Samples"_ustr)
31 .default_value(1)
32 .min(1)
33 .max(29)
35 "The number of samples used to compute the blur. The more samples the smoother the "
36 "result, but at the expense of more compute time. The actual number of samples is two "
37 "to the power of this input, so it increases exponentially");
38 b.add_input<decl::Vector>("Center"_ustr)
39 .subtype(PROP_FACTOR)
40 .dimensions(2)
41 .default_value({0.5f, 0.5f})
42 .min(0.0f)
43 .max(1.0f)
44 .description(
45 "The position at which the transformations pivot around. Defined in normalized "
46 "coordinates, so 0 means lower left corner and 1 means upper right corner of the image");
47
48 b.add_input<decl::Float>("Rotation"_ustr)
49 .default_value(0.0f)
51 .description("The amount of rotation that the blur spans");
52 b.add_input<decl::Float>("Scale"_ustr)
53 .default_value(1.0f)
54 .min(0.0f)
55 .description("The amount of scaling that the blur spans");
56
57 PanelDeclarationBuilder &translation_panel =
58 b.add_panel("Translation"_ustr).default_closed(false);
59 translation_panel.add_input<decl::Float>("Amount"_ustr, "Translation Amount"_ustr)
60 .default_value(0.0f)
62 .min(-1.0f)
63 .max(1.0f)
64 .description(
65 "The amount of translation that the blur spans in the specified direction relative to "
66 "the size of the image. Negative values indicate translation in the opposite direction");
67 translation_panel.add_input<decl::Float>("Direction"_ustr, "Translation Direction"_ustr)
68 .default_value(0.0f)
70 .description("The angle that defines the direction of the translation");
71}
72
73using namespace blender::compositor;
74
76 public:
78
79 void execute() override
80 {
81 if (this->is_identity()) {
82 const Result &input = this->get_input("Image");
83 Result &output = this->get_result("Image");
84 output.share_data(input);
85 return;
86 }
87
88 if (this->context().use_gpu()) {
89 this->execute_gpu();
90 }
91 else {
92 this->execute_cpu();
93 }
94 }
95
97 {
98 gpu::Shader *shader = context().get_shader("compositor_directional_blur");
100
101 /* The number of iterations does not cover the original image, that is, the image with no
102 * transformation. So add an extra iteration for the original image and put that into
103 * consideration in the shader. */
104 GPU_shader_uniform_1i(shader, "iterations", this->get_iterations() + 1);
105 GPU_shader_uniform_2fv(shader, "origin", this->get_origin());
106 GPU_shader_uniform_2fv(shader, "delta_translation", this->get_delta_translation());
107 GPU_shader_uniform_1f(shader, "delta_rotation_sin", math::sin(this->get_delta_rotation()));
108 GPU_shader_uniform_1f(shader, "delta_rotation_cos", math::cos(this->get_delta_rotation()));
109 GPU_shader_uniform_1f(shader, "delta_scale", this->get_delta_scale());
110
111 const Result &input_image = get_input("Image");
112 GPU_texture_filter_mode(input_image, true);
114 input_image.bind_as_texture(shader, "input_tx");
115
116 const Domain domain = compute_domain();
117 Result &output_image = get_result("Image");
118 output_image.allocate_texture(domain);
119 output_image.bind_as_image(shader, "output_img");
120
122
124 output_image.unbind_as_image();
125 input_image.unbind_as_texture();
126 }
127
129 {
130 /* The number of iterations does not cover the original image, that is, the image with no
131 * transformation. So add an extra iteration for the original image and put that into
132 * consideration in the code. */
133 const int iterations = this->get_iterations() + 1;
134 const float2 origin = this->get_origin();
135 const float2 delta_translation = this->get_delta_translation();
136 const float delta_rotation_sin = math::sin(this->get_delta_rotation());
137 const float delta_rotation_cos = math::cos(this->get_delta_rotation());
138 const float delta_scale = this->get_delta_scale();
139
140 const Result &input = get_input("Image");
141
142 const Domain domain = compute_domain();
143 Result &output = get_result("Image");
144 output.allocate_texture(domain);
145
146 const int2 size = domain.data_size;
147 parallel_for(size, [&](const int2 texel) {
148 float2 coordinates = float2(texel) + float2(0.5f);
149
150 float current_sin = 0.0f;
151 float current_cos = 1.0f;
152 float current_scale = 1.0f;
153 float2 current_translation = float2(0.0f);
154
155 /* For each iteration, accumulate the input at the transformed coordinates, then increment
156 * the transformations for the next iteration. */
157 float4 accumulated_color = float4(0.0f);
158 for (int i = 0; i < iterations; i++) {
159 /* Transform the coordinates by first offsetting the origin, scaling, translating,
160 * rotating, then finally restoring the origin. Notice that we do the inverse of each of
161 * the transforms, since we are transforming the coordinates, not the image. */
162 float2 transformed_coordinates = coordinates;
163 transformed_coordinates -= origin;
164 transformed_coordinates /= current_scale;
165 transformed_coordinates -= current_translation;
166 transformed_coordinates = transformed_coordinates *
167 float2x2(float2(current_cos, current_sin),
168 float2(-current_sin, current_cos));
169 transformed_coordinates += origin;
170
171 accumulated_color += float4(
172 input.sample_bilinear_zero<Color>(transformed_coordinates / float2(size)));
173
174 current_scale += delta_scale;
175 current_translation += delta_translation;
176
177 /* Those are the sine and cosine addition identities. Used to avoid computing sine and
178 * cosine at each iteration. */
179 float new_sin = current_sin * delta_rotation_cos + current_cos * delta_rotation_sin;
180 current_cos = current_cos * delta_rotation_cos - current_sin * delta_rotation_sin;
181 current_sin = new_sin;
182 }
183
184 output.store_pixel(texel, Color(accumulated_color / iterations));
185 });
186 }
187
188 /* Get the delta that will be added to the translation on each iteration. The translation is in
189 * the negative x direction rotated in the clock-wise direction, hence the negative sign for the
190 * rotation and translation vector. */
192 {
193 const float2 input_size = float2(get_input("Image").domain().data_size);
194 const float diagonal_length = math::length(input_size);
195 const float translation_amount = diagonal_length * this->get_translation_amount();
198 const float2 translation = rotation *
199 float2(-translation_amount / this->get_iterations(), 0.0f);
200 return translation;
201 }
202
203 /* Get the delta that will be added to the rotation on each iteration. */
205 {
206 return this->get_rotation() / this->get_iterations();
207 }
208
209 /* Get the delta that will be added to the scale on each iteration. */
211 {
212 return (this->get_scale() - 1.0f) / this->get_iterations();
213 }
214
216 {
217 const float2 input_size = float2(get_input("Image").domain().data_size);
218 return this->get_center() * input_size;
219 }
220
221 /* The actual number of iterations is 2 to the power of the user supplied iterations. The power
222 * is implemented using a bit shift. But also make sure it doesn't exceed the upper limit which
223 * is the number of diagonal pixels. */
225 {
226 const int iterations = 2 << (this->get_samples() - 1);
227 const int upper_limit = math::ceil(
228 math::length(float2(get_input("Image").domain().data_size)));
229 return math::min(iterations, upper_limit);
230 }
231
233 {
234 const Result &input = this->get_input("Image");
235 if (input.is_single_value()) {
236 return true;
237 }
238
239 if (this->get_translation_amount() != 0.0f) {
240 return false;
241 }
242
243 if (this->get_rotation() != 0.0f) {
244 return false;
245 }
246
247 if (this->get_scale() != 1.0f) {
248 return false;
249 }
250
251 return true;
252 }
253
255 {
256 return math::clamp(this->get_input("Samples").get_single_value_default<int>(), 1, 29);
257 }
258
260 {
261 return math::clamp(
262 this->get_input("Center").get_single_value_default<float2>(), float2(0.0f), float2(1.0f));
263 }
264
266 {
267 return math::clamp(
268 this->get_input("Translation Amount").get_single_value_default<float>(), -1.0f, 1.0f);
269 }
270
272 {
273 return this->get_input("Translation Direction").get_single_value_default<float>();
274 }
275
277 {
278 return this->get_input("Rotation").get_single_value_default<float>();
279 }
280
281 float get_scale()
282 {
283 return math::max(10e-6f, this->get_input("Scale").get_single_value_default<float>());
284 }
285};
286
288{
289 return new DirectionalBlurOperation(context, node);
290}
291
292static void node_register()
293{
294 static bke::bNodeType ntype;
295
296 cmp_node_type_base(&ntype, "CompositorNodeDBlur"_ustr, CMP_NODE_DBLUR);
297 ntype.ui_name = "Directional Blur";
298 ntype.ui_description = "Blur an image along a direction";
299 ntype.enum_name_legacy = "DBLUR";
301 ntype.declare = node_declare;
303
305}
307
308} // namespace blender::nodes::node_composite_directional_blur_cc
#define NODE_CLASS_OP_FILTER
Definition BKE_node.hh:482
#define CMP_NODE_DBLUR
#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
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 node_register_type(bNodeType &ntype)
Definition node.cc:2920
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)
AngleRadianBase< float > AngleRadian
T cos(const AngleRadianBase< T > &a)
T clamp(const T &a, const T &min, const T &max)
T length(const VecBase< T, Size > &a)
T min(const T &a, const T &b)
T ceil(const T &a)
T sin(const AngleRadianBase< T > &a)
T max(const T &a, const T &b)
MatT from_rotation(const RotationT &rotation)
static NodeOperation * get_compositor_operation(Context &context, const bNode &node)
MatBase< float, 2, 2 > float2x2
@ GPU_SAMPLER_EXTEND_MODE_CLAMP_TO_BORDER
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)
void GPU_shader_uniform_1i(gpu::Shader *sh, const char *name, int value)
void GPU_shader_uniform_2fv(gpu::Shader *sh, const char *name, const float data[2])
void GPU_texture_extend_mode(gpu::Texture *texture, GPUSamplerExtendMode extend_mode)
void GPU_shader_uniform_1f(gpu::Shader *sh, const char *name, float value)
@ PROP_FACTOR
Definition RNA_types.hh:308
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
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
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393