Blender V4.5
node_composite_bilateralblur.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
8
9#include "BLI_math_base.hh"
10
11#include "UI_interface.hh"
12#include "UI_resources.hh"
13
14#include "GPU_shader.hh"
15
17#include "COM_node_operation.hh"
18#include "COM_utilities.hh"
19
21
22/* **************** BILATERALBLUR ******************** */
23
25
27{
28 b.add_input<decl::Color>("Image").default_value({1.0f, 1.0f, 1.0f, 1.0f});
29 b.add_input<decl::Color>("Determinator").default_value({1.0f, 1.0f, 1.0f, 1.0f});
30 b.add_input<decl::Int>("Size")
31 .default_value(0)
32 .min(0)
33 .description("The size of the blur in pixels")
34 .compositor_expects_single_value();
35 b.add_input<decl::Float>("Threshold")
36 .default_value(0.1f)
37 .min(0.0f)
39 "Pixels are considered in the blur area if the average difference between their "
40 "determinator and the determinator of the center pixel is less than this threshold")
41 .compositor_expects_single_value();
42
43 b.add_output<decl::Color>("Image");
44}
45
46using namespace blender::compositor;
47
49 public:
51
52 void execute() override
53 {
54 const Result &input_image = this->get_input("Image");
55 Result &output_image = this->get_result("Image");
56 if (input_image.is_single_value() || this->get_blur_radius() == 0 ||
57 this->get_threshold() == 0.0f)
58 {
59 output_image.share_data(input_image);
60 return;
61 }
62
63 /* If the determinator is a single value, then the node essentially becomes a box blur. */
64 const Result &determinator_image = get_input("Determinator");
65 if (determinator_image.is_single_value()) {
67 input_image,
68 output_image,
69 float2(this->get_blur_radius()),
71 return;
72 }
73
74 if (this->context().use_gpu()) {
75 this->execute_gpu();
76 }
77 else {
78 this->execute_cpu();
79 }
80 }
81
83 {
84 GPUShader *shader = context().get_shader("compositor_bilateral_blur");
85 GPU_shader_bind(shader);
86
87 GPU_shader_uniform_1i(shader, "radius", get_blur_radius());
88 GPU_shader_uniform_1f(shader, "threshold", get_threshold());
89
90 const Result &input_image = get_input("Image");
91 input_image.bind_as_texture(shader, "input_tx");
92
93 const Result &determinator_image = get_input("Determinator");
94 determinator_image.bind_as_texture(shader, "determinator_tx");
95
96 const Domain domain = compute_domain();
97 Result &output_image = get_result("Image");
98 output_image.allocate_texture(domain);
99 output_image.bind_as_image(shader, "output_img");
100
102
104 output_image.unbind_as_image();
105 input_image.unbind_as_texture();
106 determinator_image.unbind_as_texture();
107 }
108
110 {
111 const int radius = this->get_blur_radius();
112 const float threshold = this->get_threshold();
113
114 const Result &input = get_input("Image");
115 const Result &determinator_image = get_input("Determinator");
116
117 const Domain domain = compute_domain();
118 Result &output = get_result("Image");
119 output.allocate_texture(domain);
120
121 parallel_for(domain.size, [&](const int2 texel) {
122 float4 center_determinator = determinator_image.load_pixel<float4>(texel);
123
124 /* Go over the pixels in the blur window of the specified radius around the center pixel, and
125 * for pixels whose determinator is close enough to the determinator of the center pixel,
126 * accumulate their color as well as their weights. */
127 float accumulated_weight = 0.0f;
128 float4 accumulated_color = float4(0.0f);
129 for (int y = -radius; y <= radius; y++) {
130 for (int x = -radius; x <= radius; x++) {
131 float4 determinator = determinator_image.load_pixel_extended<float4>(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 += input.load_pixel_extended<float4>(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);
149 });
150 }
151
153 {
154 return math::max(0, this->get_input("Size").get_single_value_default(0));
155 }
156
158 {
159 return math::max(0.0f, this->get_input("Threshold").get_single_value_default(0.1f));
160 }
161};
162
164{
165 return new BilateralBlurOperation(context, node);
166}
167
168} // namespace blender::nodes::node_composite_bilateralblur_cc
169
171{
173
174 static blender::bke::bNodeType ntype;
175
176 cmp_node_type_base(&ntype, "CompositorNodeBilateralblur", CMP_NODE_BILATERALBLUR);
177 ntype.ui_name = "Bilateral Blur";
178 ntype.ui_description = "Adaptively blur image, while retaining sharp edges";
179 ntype.enum_name_legacy = "BILATERALBLUR";
181 ntype.declare = file_ns::cmp_node_bilateralblur_declare;
182 ntype.get_compositor_operation = file_ns::get_compositor_operation;
183
185}
#define NODE_CLASS_OP_FILTER
Definition BKE_node.hh:437
#define CMP_NODE_BILATERALBLUR
@ R_FILTER_BOX
void GPU_shader_uniform_1i(GPUShader *sh, const char *name, int value)
void GPU_shader_uniform_1f(GPUShader *sh, const char *name, float value)
void GPU_shader_bind(GPUShader *shader, const blender::gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_shader_unbind()
#define NOD_REGISTER_NODE(REGISTER_FUNC)
GPUShader * get_shader(const char *info_name, ResultPrecision precision)
NodeOperation(Context &context, DNode node)
Result & get_result(StringRef identifier)
Definition operation.cc:39
Result & get_input(StringRef identifier) const
Definition operation.cc:138
virtual Domain compute_domain()
Definition operation.cc:56
void share_data(const Result &source)
Definition result.cc:401
void allocate_texture(Domain domain, bool from_pool=true)
Definition result.cc:309
void unbind_as_texture() const
Definition result.cc:389
void bind_as_texture(GPUShader *shader, const char *texture_name) const
Definition result.cc:365
void bind_as_image(GPUShader *shader, const char *image_name, bool read=false) const
Definition result.cc:376
void unbind_as_image() const
Definition result.cc:395
bool is_single_value() const
Definition result.cc:622
#define input
#define this
#define output
void node_register_type(bNodeType &ntype)
Definition node.cc:2748
void compute_dispatch_threads_at_least(GPUShader *shader, int2 threads_range, int2 local_size=int2(16))
Definition utilities.cc:170
void symmetric_separable_blur(Context &context, const Result &input, Result &output, const float2 &radius, const int filter_type=R_FILTER_GAUSS, const bool extend_bounds=false)
void parallel_for(const int2 range, const Function &function)
T max(const T &a, const T &b)
static NodeOperation * get_compositor_operation(Context &context, DNode node)
static void cmp_node_bilateralblur_declare(NodeDeclarationBuilder &b)
VecBase< float, 4 > float4
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
VecBase< float, 3 > float3
static void register_node_type_cmp_bilateralblur()
void cmp_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
Defines a node type.
Definition BKE_node.hh:226
std::string ui_description
Definition BKE_node.hh:232
NodeGetCompositorOperationFunction get_compositor_operation
Definition BKE_node.hh:336
const char * enum_name_legacy
Definition BKE_node.hh:235
NodeDeclareFunction declare
Definition BKE_node.hh:355
static pxr::UsdShadeInput get_input(const pxr::UsdShadeShader &usd_shader, const pxr::TfToken &input_name)