Blender V5.3
node_composite_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_assert.hh"
6#include "BLI_math_vector.hh"
8
9#include "RNA_types.hh"
10
12
13#include "GPU_shader.hh"
14
15#include "COM_algorithm_pad.hh"
19#include "COM_node_operation.hh"
21#include "COM_utilities.hh"
22
24
26
27static const EnumPropertyItem type_items[] = {
28 {CMP_NODE_BLUR_TYPE_BOX, "FLAT", 0, N_("Flat"), N_("Applies a box blur filter")},
29 {CMP_NODE_BLUR_TYPE_TENT, "TENT", 0, N_("Tent"), N_("Applies a triangle blur filter")},
30 {CMP_NODE_BLUR_TYPE_QUAD, "QUAD", 0, N_("Quadratic"), N_("Applies a quadratic blur filter")},
31 {CMP_NODE_BLUR_TYPE_CUBIC, "CUBIC", 0, N_("Cubic"), N_("Applies a cubic blur filter")},
32 {CMP_NODE_BLUR_TYPE_GAUSS, "GAUSS", 0, N_("Gaussian"), N_("Applies a Gaussian blur")},
34 "FAST_GAUSS",
35 0,
36 N_("Fast Gaussian"),
37 N_("Applies a recursive Gaussian blur that can be faster and more accurate in some cases, "
38 "but less accurate in other cases")},
40 "CATROM",
41 0,
42 N_("Catrom"),
43 N_("Applies a cubic Catmull-Rom filter")},
45 "MITCH",
46 0,
47 N_("Mitch"),
48 N_("Applies a cubic Mitchell-Netravali filter")},
49 {0, nullptr, 0, nullptr, nullptr},
50};
51
53{
54 b.use_custom_socket_order();
55 b.allow_any_socket_order();
56 b.add_input<decl::Color>("Image"_ustr)
57 .default_value({1.0f, 1.0f, 1.0f, 1.0f})
58 .hide_value()
59 .structure_type(StructureType::Dynamic);
60 b.add_output<decl::Color>("Image"_ustr)
61 .structure_type(StructureType::Dynamic)
62 .align_with_previous();
63
64 b.add_input<decl::Vector>("Size"_ustr)
65 .dimensions(2)
66 .default_value({0.0f, 0.0f})
67 .subtype(PROP_PIXEL)
68 .min(0.0f)
69 .structure_type(StructureType::Dynamic);
70 b.add_input<decl::Menu>("Type"_ustr)
71 .default_value(CMP_NODE_BLUR_TYPE_GAUSS)
72 .static_items(type_items)
74 b.add_input<decl::Bool>("Extend Bounds"_ustr).default_value(false);
75 b.add_input<decl::Bool>("Separable"_ustr)
76 .default_value(true)
78 "Use faster approximation by blurring along the horizontal and vertical directions "
79 "independently");
80}
81
82static void node_init(bNodeTree * /*ntree*/, bNode *node)
83{
84 /* Unused, but allocated for forward compatibility. */
85 NodeBlurData *data = MEM_new<NodeBlurData>(__func__);
86 node->storage = data;
87}
88
113
114using namespace blender::compositor;
115
117 public:
119
120 void execute() override
121 {
122 const Result &input = this->get_input("Image");
123 Result &output = this->get_result("Image");
124 if (this->is_identity()) {
125 output.share_data(input);
126 return;
127 }
128
129 const Result &size = this->get_input("Size");
130 if (!size.is_single_value()) {
131 this->execute_variable_size(input, size, output);
132 return;
133 }
134
137 this->context(), input, output, this->get_blur_size(), this->get_extend_bounds());
138 }
139 else if (use_separable_filter()) {
141 input,
142 output,
143 this->get_blur_size(),
145 this->get_extend_bounds());
146 }
147 else {
148 this->execute_constant_size(input, output);
149 }
150 }
151
153 {
154 if (this->get_extend_bounds()) {
155 Result padded_input = this->context().create_result(input.type());
156
157 const int2 padding_size = int2(math::ceil(this->get_blur_size()));
158
159 pad(this->context(), input, padded_input, padding_size, PaddingMethod::Zero);
160
161 if (this->context().use_gpu()) {
162 this->execute_constant_size_gpu(padded_input, output);
163 }
164 else {
165 this->execute_constant_size_cpu(padded_input, output);
166 }
167
168 padded_input.release();
169 }
170 else {
171 if (this->context().use_gpu()) {
172 this->execute_constant_size_gpu(input, output);
173 }
174 else {
175 this->execute_constant_size_cpu(input, output);
176 }
177 }
178 }
179
181 {
182 gpu::Shader *shader = context().get_shader("compositor_symmetric_blur");
184
185 input.bind_as_texture(shader, "input_tx");
186
187 const float2 blur_radius = this->get_blur_size();
188
190 context(), blur_type_to_kernel(this->get_type()), blur_radius);
191 weights.bind_as_texture(shader, "weights_tx");
192
193 const Domain domain = input.domain();
194 output.allocate_texture(domain);
195 output.bind_as_image(shader, "output_img");
196
198
200 output.unbind_as_image();
201 input.unbind_as_texture();
202 weights.unbind_as_texture();
203 }
204
206 {
207 const float2 blur_radius = this->get_blur_size();
208 const Result &weights = this->context().cache_manager().symmetric_blur_weights.get(
209 this->context(), blur_type_to_kernel(this->get_type()), blur_radius);
210
211 const Domain domain = input.domain();
212 output.allocate_texture(domain);
213
214 parallel_for(domain.data_size, [&](const int2 texel) {
215 float4 accumulated_color = float4(0.0f);
216
217 /* First, compute the contribution of the center pixel. */
218 float4 center_color = float4(input.load_pixel_extended<Color>(texel));
219 accumulated_color += center_color * weights.load_pixel<float>(int2(0));
220
221 int2 weights_size = weights.domain().data_size;
222
223 /* Then, compute the contributions of the pixels along the x axis of the filter, noting that
224 * the weights texture only stores the weights for the positive half, but since the filter is
225 * symmetric, the same weight is used for the negative half and we add both of their
226 * contributions. */
227 for (int x = 1; x < weights_size.x; x++) {
228 float weight = weights.load_pixel<float>(int2(x, 0));
229 accumulated_color += float4(input.load_pixel_extended<Color>(texel + int2(x, 0))) * weight;
230 accumulated_color += float4(input.load_pixel_extended<Color>(texel + int2(-x, 0))) *
231 weight;
232 }
233
234 /* Then, compute the contributions of the pixels along the y axis of the filter, noting that
235 * the weights texture only stores the weights for the positive half, but since the filter is
236 * symmetric, the same weight is used for the negative half and we add both of their
237 * contributions. */
238 for (int y = 1; y < weights_size.y; y++) {
239 float weight = weights.load_pixel<float>(int2(0, y));
240 accumulated_color += float4(input.load_pixel_extended<Color>(texel + int2(0, y))) * weight;
241 accumulated_color += float4(input.load_pixel_extended<Color>(texel + int2(0, -y))) *
242 weight;
243 }
244
245 /* Finally, compute the contributions of the pixels in the four quadrants of the filter,
246 * noting that the weights texture only stores the weights for the upper right quadrant, but
247 * since the filter is symmetric, the same weight is used for the rest of the quadrants and
248 * we add all four of their contributions. */
249 for (int y = 1; y < weights_size.y; y++) {
250 for (int x = 1; x < weights_size.x; x++) {
251 float weight = weights.load_pixel<float>(int2(x, y));
252 accumulated_color += float4(input.load_pixel_extended<Color>(texel + int2(x, y))) *
253 weight;
254 accumulated_color += float4(input.load_pixel_extended<Color>(texel + int2(-x, y))) *
255 weight;
256 accumulated_color += float4(input.load_pixel_extended<Color>(texel + int2(x, -y))) *
257 weight;
258 accumulated_color += float4(input.load_pixel_extended<Color>(texel + int2(-x, -y))) *
259 weight;
260 }
261 }
262
263 output.store_pixel(texel, Color(accumulated_color));
264 });
265 }
266
267 void execute_variable_size(const Result &input, const Result &size, Result &output)
268 {
269 if (this->get_extend_bounds()) {
270 Result padded_input = this->context().create_result(input.type());
271 Result padded_size = this->context().create_result(ResultType::Float2);
272
273 const int2 padding_size = int2(math::ceil(this->compute_maximum_blur_size()));
274
275 pad(this->context(), input, padded_input, padding_size, PaddingMethod::Zero);
276 pad(this->context(), size, padded_size, padding_size, PaddingMethod::Extend);
277
278 if (this->context().use_gpu()) {
279 this->execute_variable_size_gpu(padded_input, padded_size, output);
280 }
281 else {
282 this->execute_variable_size_cpu(padded_input, padded_size, output);
283 }
284
285 padded_input.release();
286 padded_size.release();
287 }
288 else {
289 if (this->context().use_gpu()) {
290 this->execute_variable_size_gpu(input, size, output);
291 }
292 else {
293 this->execute_variable_size_cpu(input, size, output);
294 }
295 }
296 }
297
298 void execute_variable_size_gpu(const Result &input, const Result &size_input, Result &output)
299 {
300 const float2 blur_radius = this->compute_maximum_blur_size();
301 const Result &weights = context().cache_manager().symmetric_blur_weights.get(
302 context(), blur_type_to_kernel(this->get_type()), blur_radius);
303
304 gpu::Shader *shader = context().get_shader("compositor_symmetric_blur_variable_size");
306
307 input.bind_as_texture(shader, "input_tx");
308 weights.bind_as_texture(shader, "weights_tx");
309 size_input.bind_as_texture(shader, "size_tx");
310
311 const Domain domain = input.domain();
312 output.allocate_texture(domain);
313 output.bind_as_image(shader, "output_img");
314
316
318 output.unbind_as_image();
319 input.unbind_as_texture();
320 weights.unbind_as_texture();
321 size_input.unbind_as_texture();
322 }
323
324 void execute_variable_size_cpu(const Result &input, const Result &size_input, Result &output)
325 {
326 const float2 blur_radius = this->compute_maximum_blur_size();
327 const Result &weights = this->context().cache_manager().symmetric_blur_weights.get(
328 this->context(), blur_type_to_kernel(this->get_type()), blur_radius);
329
330 const Domain domain = input.domain();
331 output.allocate_texture(domain);
332
333 parallel_for(domain.data_size, [&](const int2 texel) {
334 float4 accumulated_color = float4(0.0f);
335 float4 accumulated_weight = float4(0.0f);
336
337 const float2 size = math::max(float2(0.0f), size_input.load_pixel_extended<float2>(texel));
338 int2 radius = int2(math::ceil(size));
339 float2 coordinates_scale = float2(1.0f) / (size + float2(1.0f));
340
341 /* First, compute the contribution of the center pixel. */
342 float4 center_color = float4(input.load_pixel_extended<Color>(texel));
343 float center_weight = weights.load_pixel<float>(int2(0));
344 accumulated_color += center_color * center_weight;
345 accumulated_weight += center_weight;
346
347 /* Then, compute the contributions of the pixels along the x axis of the filter, noting that
348 * the weights texture only stores the weights for the positive half, but since the filter is
349 * symmetric, the same weight is used for the negative half and we add both of their
350 * contributions. */
351 for (int x = 1; x <= radius.x; x++) {
352 float weight_coordinates = (x + 0.5f) * coordinates_scale.x;
353 float weight = weights.sample_bilinear_extended<float>(float2(weight_coordinates, 0.0f));
354 accumulated_color += float4(input.load_pixel_extended<Color>(texel + int2(x, 0))) * weight;
355 accumulated_color += float4(input.load_pixel_extended<Color>(texel + int2(-x, 0))) *
356 weight;
357 accumulated_weight += weight * 2.0f;
358 }
359
360 /* Then, compute the contributions of the pixels along the y axis of the filter, noting that
361 * the weights texture only stores the weights for the positive half, but since the filter is
362 * symmetric, the same weight is used for the negative half and we add both of their
363 * contributions. */
364 for (int y = 1; y <= radius.y; y++) {
365 float weight_coordinates = (y + 0.5f) * coordinates_scale.y;
366 float weight = weights.sample_bilinear_extended<float>(float2(0.0f, weight_coordinates));
367 accumulated_color += float4(input.load_pixel_extended<Color>(texel + int2(0, y))) * weight;
368 accumulated_color += float4(input.load_pixel_extended<Color>(texel + int2(0, -y))) *
369 weight;
370 accumulated_weight += weight * 2.0f;
371 }
372
373 /* Finally, compute the contributions of the pixels in the four quadrants of the filter,
374 * noting that the weights texture only stores the weights for the upper right quadrant, but
375 * since the filter is symmetric, the same weight is used for the rest of the quadrants and
376 * we add all four of their contributions. */
377 for (int y = 1; y <= radius.y; y++) {
378 for (int x = 1; x <= radius.x; x++) {
379 float2 weight_coordinates = (float2(x, y) + float2(0.5f)) * coordinates_scale;
380 float weight = weights.sample_bilinear_extended<float>(weight_coordinates);
381 accumulated_color += float4(input.load_pixel_extended<Color>(texel + int2(x, y))) *
382 weight;
383 accumulated_color += float4(input.load_pixel_extended<Color>(texel + int2(-x, y))) *
384 weight;
385 accumulated_color += float4(input.load_pixel_extended<Color>(texel + int2(x, -y))) *
386 weight;
387 accumulated_color += float4(input.load_pixel_extended<Color>(texel + int2(-x, -y))) *
388 weight;
389 accumulated_weight += weight * 4.0f;
390 }
391 }
392
393 accumulated_color = math::safe_divide(accumulated_color, accumulated_weight);
394
395 output.store_pixel(texel, Color(accumulated_color));
396 });
397 }
398
400 {
401 return math::max(float2(0.0f), maximum_float2(this->context(), this->get_input("Size")));
402 }
403
405 {
406 const Result &input = this->get_input("Image");
407 if (input.is_single_value()) {
408 return true;
409 }
410
411 const Result &size = this->get_input("Size");
412 if (!size.is_single_value()) {
413 return false;
414 }
415
416 if (this->get_blur_size() == float2(0.0)) {
417 return true;
418 }
419
420 return false;
421 }
422
423 /* The blur node can operate with different filter types, evaluated on the normalized distance to
424 * the center of the filter. Some of those filters are separable and can be computed as such. If
425 * the Separable input is true, then the filter is always computed as separable even if it is not
426 * in fact separable, in which case, the used filter is a cheaper approximation to the actual
427 * filter. Otherwise, the filter is computed as separable if it is in fact separable and as a
428 * normal 2D filter otherwise. */
430 {
431 if (this->get_separable()) {
432 return true;
433 }
434
435 /* Only Gaussian filters are separable. The rest is not. */
436 switch (this->get_type()) {
439 return true;
440 default:
441 return false;
442 }
443 }
444
446 {
447 BLI_assert(this->get_input("Size").is_single_value());
448 return math::max(float2(0.0f), this->get_input("Size").get_single_value<float2>());
449 }
450
452 {
453 return this->get_input("Separable").get_single_value_default<bool>();
454 }
455
457 {
458 return this->get_input("Extend Bounds").get_single_value_default<bool>();
459 }
460
462 {
463 return CMPNodeBlurType(this->get_input("Type").get_single_value_default<MenuValue>().value);
464 }
465};
466
468{
469 return new BlurOperation(context, node);
470}
471
472static void node_register()
473{
474 static bke::bNodeType ntype;
475
476 cmp_node_type_base(&ntype, "CompositorNodeBlur"_ustr, CMP_NODE_BLUR);
477 ntype.ui_name = "Blur";
478 ntype.ui_description = "Blur an image, using several blur modes";
479 ntype.enum_name_legacy = "BLUR";
481 ntype.declare = node_declare;
482 ntype.flag |= NODE_PREVIEW;
483 ntype.initfunc = node_init;
487
489}
490NOD_REGISTER_NODE(node_register)
491
492} // namespace blender::nodes::node_composite_blur_cc
#define NODE_CLASS_OP_FILTER
Definition BKE_node.hh:482
#define CMP_NODE_BLUR
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define BLI_assert_unreachable()
#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
T sample_bilinear_extended(const float2 &coordinates) const
void unbind_as_texture() const
Definition result.cc:964
static ResultType type(gpu::TextureFormat format)
Definition result.cc:337
void bind_as_texture(gpu::Shader *shader, const char *texture_name) const
Definition result.cc:854
T load_pixel_extended(const int2 &texel) const
T load_pixel(const int2 &texel) const
SymmetricBlurWeightsContainer symmetric_blur_weights
Result & get(Context &context, math::FilterKernel type, float2 radius)
NodeOperation(Context &context, const bNode &node)
void execute_variable_size_cpu(const Result &input, const Result &size_input, Result &output)
void execute_constant_size_gpu(const Result &input, Result &output)
void execute_variable_size(const Result &input, const Result &size, Result &output)
void execute_constant_size(const Result &input, Result &output)
void execute_constant_size_cpu(const Result &input, Result &output)
void execute_variable_size_gpu(const Result &input, const Result &size_input, Result &output)
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
float2 maximum_float2(Context &context, const Result &input)
void symmetric_separable_blur(Context &context, const Result &input, Result &output, const float2 &radius, const math::FilterKernel filter_type=math::FilterKernel::Gauss, const bool extend_bounds=false)
ColorSceneLinear4f< eAlpha::Premultiplied > Color
Definition COM_result.hh:96
void pad(Context &context, const Result &input, Result &output, const int2 size, const PaddingMethod padding_method)
Definition pad.cc:113
void recursive_gaussian_blur(Context &context, const Result &input, Result &output, const float2 &radius, const bool extend_bounds=false)
void parallel_for(const int2 range, const Function &function)
T safe_divide(const T &a, const T &b)
T ceil(const T &a)
T max(const T &a, const T &b)
static math::FilterKernel blur_type_to_kernel(CMPNodeBlurType type)
static const EnumPropertyItem type_items[]
static NodeOperation * get_compositor_operation(Context &context, const bNode &node)
static void node_declare(NodeDeclarationBuilder &b)
static void node_init(bNodeTree *, bNode *node)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
@ CMP_NODE_BLUR_TYPE_GAUSS
@ CMP_NODE_BLUR_TYPE_BOX
@ CMP_NODE_BLUR_TYPE_CATROM
@ CMP_NODE_BLUR_TYPE_CUBIC
@ CMP_NODE_BLUR_TYPE_FAST_GAUSS
@ CMP_NODE_BLUR_TYPE_QUAD
@ CMP_NODE_BLUR_TYPE_TENT
@ CMP_NODE_BLUR_TYPE_MITCH
BMesh const char void * data
void GPU_shader_unbind()
void GPU_shader_bind(gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
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
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
#define N_(msgid)