Blender V5.3
node_composite_bilateral_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
7#include "GPU_shader.hh"
8
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 b.add_input<decl::Color>("Image"_ustr)
22 .default_value({1.0f, 1.0f, 1.0f, 1.0f})
23 .hide_value()
24 .structure_type(StructureType::Dynamic);
25 b.add_output<decl::Color>("Image"_ustr)
26 .structure_type(StructureType::Dynamic)
27 .align_with_previous();
28
29 b.add_input<decl::Color>("Determinator"_ustr)
30 .default_value({1.0f, 1.0f, 1.0f, 1.0f})
31 .structure_type(StructureType::Dynamic);
32 b.add_input<decl::Int>("Size"_ustr)
33 .default_value(0)
35 .min(0)
36 .description("The size of the blur in pixels");
37 b.add_input<decl::Float>("Threshold"_ustr)
38 .default_value(0.1f)
39 .min(0.0f)
41 "Pixels are considered in the blur area if the average difference between their "
42 "determinator and the determinator of the center pixel is less than this threshold");
43}
44
45using namespace blender::compositor;
46
48 public:
50
51 void execute() override
52 {
53 const Result &input_image = this->get_input("Image");
54 Result &output_image = this->get_result("Image");
55 if (input_image.is_single_value() || this->get_blur_radius() == 0 ||
56 this->get_threshold() == 0.0f)
57 {
58 output_image.share_data(input_image);
59 return;
60 }
61
62 /* If the determinator is a single value, then the node essentially becomes a box blur. */
63 const Result &determinator_image = get_input("Determinator");
64 if (determinator_image.is_single_value()) {
66 input_image,
67 output_image,
68 float2(this->get_blur_radius()),
70 return;
71 }
72
73 if (this->context().use_gpu()) {
74 this->execute_gpu();
75 }
76 else {
77 this->execute_cpu();
78 }
79 }
80
82 {
83 gpu::Shader *shader = context().get_shader("compositor_bilateral_blur");
85
88
89 const Result &input_image = get_input("Image");
90 input_image.bind_as_texture(shader, "input_tx");
91
92 const Result &determinator_image = get_input("Determinator");
93 determinator_image.bind_as_texture(shader, "determinator_tx");
94
95 const Domain domain = compute_domain();
96 Result &output_image = get_result("Image");
97 output_image.allocate_texture(domain);
98 output_image.bind_as_image(shader, "output_img");
99
101
103 output_image.unbind_as_image();
104 input_image.unbind_as_texture();
105 determinator_image.unbind_as_texture();
106 }
107
109 {
110 const int radius = this->get_blur_radius();
111 const float threshold = this->get_threshold();
112
113 const Result &input = get_input("Image");
114 const Result &determinator_image = get_input("Determinator");
115
116 const Domain domain = compute_domain();
117 Result &output = get_result("Image");
118 output.allocate_texture(domain);
119
120 parallel_for(domain.data_size, [&](const int2 texel) {
121 float4 center_determinator = float4(determinator_image.load_pixel<Color>(texel));
122
123 /* Go over the pixels in the blur window of the specified radius around the center pixel, and
124 * for pixels whose determinator is close enough to the determinator of the center pixel,
125 * accumulate their color as well as their weights. */
126 float accumulated_weight = 0.0f;
127 float4 accumulated_color = float4(0.0f);
128 for (int y = -radius; y <= radius; y++) {
129 for (int x = -radius; x <= radius; x++) {
130 float4 determinator = float4(
131 determinator_image.load_pixel_extended<Color>(texel + int2(x, y)));
132 float difference = math::dot(math::abs(center_determinator - determinator).xyz(),
133 float3(1.0f)) /
134 3.0f;
135
136 if (difference < threshold) {
137 accumulated_weight += 1.0f;
138 accumulated_color += float4(input.load_pixel_extended<Color>(texel + int2(x, y)));
139 }
140 }
141 }
142
143 /* Write the accumulated color divided by the accumulated weight if any pixel in the window
144 * was accumulated, otherwise, write a fallback black color. */
145 float4 fallback = float4(float3(0.0f), 1.0f);
146 float4 color = (accumulated_weight != 0.0f) ? (accumulated_color / accumulated_weight) :
147 fallback;
148 output.store_pixel(texel, Color(color));
149 });
150 }
151
153 {
154 return math::max(0, this->get_input("Size").get_single_value_default<int>());
155 }
156
158 {
159 return math::max(0.0f, this->get_input("Threshold").get_single_value_default<float>());
160 }
161};
162
164{
165 return new BilateralBlurOperation(context, node);
166}
167
168static void node_register()
169{
170 static bke::bNodeType ntype;
171
172 cmp_node_type_base(&ntype, "CompositorNodeBilateralblur"_ustr, CMP_NODE_BILATERALBLUR);
173 ntype.ui_name = "Bilateral Blur";
174 ntype.ui_description = "Adaptively blur image, while retaining sharp edges";
175 ntype.enum_name_legacy = "BILATERALBLUR";
177 ntype.declare = node_declare;
179
181}
182NOD_REGISTER_NODE(node_register)
183
184} // namespace blender::nodes::node_composite_bilateral_blur_cc
#define NODE_CLASS_OP_FILTER
Definition BKE_node.hh:482
#define CMP_NODE_BILATERALBLUR
#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 share_data(const Result &source)
Definition result.cc:996
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
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
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
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 parallel_for(const int2 range, const Function &function)
T max(const T &a, const T &b)
static NodeOperation * get_compositor_operation(Context &context, const bNode &node)
void GPU_shader_unbind()
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_1f(gpu::Shader *sh, const char *name, float value)
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
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393