Blender V5.0
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_resources.hh"
12
13#include "GPU_shader.hh"
14
16#include "COM_node_operation.hh"
17#include "COM_utilities.hh"
18
20
21/* **************** BILATERALBLUR ******************** */
22
24
26{
27 b.use_custom_socket_order();
28 b.allow_any_socket_order();
29 b.add_input<decl::Color>("Image")
30 .default_value({1.0f, 1.0f, 1.0f, 1.0f})
31 .hide_value()
32 .structure_type(StructureType::Dynamic);
33 b.add_output<decl::Color>("Image").structure_type(StructureType::Dynamic).align_with_previous();
34
35 b.add_input<decl::Color>("Determinator")
36 .default_value({1.0f, 1.0f, 1.0f, 1.0f})
37 .structure_type(StructureType::Dynamic);
38 b.add_input<decl::Int>("Size").default_value(0).min(0).description(
39 "The size of the blur in pixels");
40 b.add_input<decl::Float>("Threshold")
41 .default_value(0.1f)
42 .min(0.0f)
44 "Pixels are considered in the blur area if the average difference between their "
45 "determinator and the determinator of the center pixel is less than this threshold");
46}
47
48using namespace blender::compositor;
49
51 public:
53
54 void execute() override
55 {
56 const Result &input_image = this->get_input("Image");
57 Result &output_image = this->get_result("Image");
58 if (input_image.is_single_value() || this->get_blur_radius() == 0 ||
59 this->get_threshold() == 0.0f)
60 {
61 output_image.share_data(input_image);
62 return;
63 }
64
65 /* If the determinator is a single value, then the node essentially becomes a box blur. */
66 const Result &determinator_image = get_input("Determinator");
67 if (determinator_image.is_single_value()) {
69 input_image,
70 output_image,
71 float2(this->get_blur_radius()),
73 return;
74 }
75
76 if (this->context().use_gpu()) {
77 this->execute_gpu();
78 }
79 else {
80 this->execute_cpu();
81 }
82 }
83
85 {
86 gpu::Shader *shader = context().get_shader("compositor_bilateral_blur");
87 GPU_shader_bind(shader);
88
89 GPU_shader_uniform_1i(shader, "radius", get_blur_radius());
90 GPU_shader_uniform_1f(shader, "threshold", get_threshold());
91
92 const Result &input_image = get_input("Image");
93 input_image.bind_as_texture(shader, "input_tx");
94
95 const Result &determinator_image = get_input("Determinator");
96 determinator_image.bind_as_texture(shader, "determinator_tx");
97
98 const Domain domain = compute_domain();
99 Result &output_image = get_result("Image");
100 output_image.allocate_texture(domain);
101 output_image.bind_as_image(shader, "output_img");
102
104
106 output_image.unbind_as_image();
107 input_image.unbind_as_texture();
108 determinator_image.unbind_as_texture();
109 }
110
112 {
113 const int radius = this->get_blur_radius();
114 const float threshold = this->get_threshold();
115
116 const Result &input = get_input("Image");
117 const Result &determinator_image = get_input("Determinator");
118
119 const Domain domain = compute_domain();
120 Result &output = get_result("Image");
121 output.allocate_texture(domain);
122
123 parallel_for(domain.size, [&](const int2 texel) {
124 float4 center_determinator = determinator_image.load_pixel<float4>(texel);
125
126 /* Go over the pixels in the blur window of the specified radius around the center pixel, and
127 * for pixels whose determinator is close enough to the determinator of the center pixel,
128 * accumulate their color as well as their weights. */
129 float accumulated_weight = 0.0f;
130 float4 accumulated_color = float4(0.0f);
131 for (int y = -radius; y <= radius; y++) {
132 for (int x = -radius; x <= radius; x++) {
133 float4 determinator = determinator_image.load_pixel_extended<float4>(texel + int2(x, y));
134 float difference = math::dot(math::abs(center_determinator - determinator).xyz(),
135 float3(1.0f)) /
136 3.0f;
137
138 if (difference < threshold) {
139 accumulated_weight += 1.0f;
140 accumulated_color += input.load_pixel_extended<float4>(texel + int2(x, y));
141 }
142 }
143 }
144
145 /* Write the accumulated color divided by the accumulated weight if any pixel in the window
146 * was accumulated, otherwise, write a fallback black color. */
147 float4 fallback = float4(float3(0.0f), 1.0f);
148 float4 color = (accumulated_weight != 0.0f) ? (accumulated_color / accumulated_weight) :
149 fallback;
150 output.store_pixel(texel, color);
151 });
152 }
153
155 {
156 return math::max(0, this->get_input("Size").get_single_value_default(0));
157 }
158
160 {
161 return math::max(0.0f, this->get_input("Threshold").get_single_value_default(0.1f));
162 }
163};
164
166{
167 return new BilateralBlurOperation(context, node);
168}
169
170} // namespace blender::nodes::node_composite_bilateralblur_cc
171
173{
175
176 static blender::bke::bNodeType ntype;
177
178 cmp_node_type_base(&ntype, "CompositorNodeBilateralblur", CMP_NODE_BILATERALBLUR);
179 ntype.ui_name = "Bilateral Blur";
180 ntype.ui_description = "Adaptively blur image, while retaining sharp edges";
181 ntype.enum_name_legacy = "BILATERALBLUR";
183 ntype.declare = file_ns::cmp_node_bilateralblur_declare;
184 ntype.get_compositor_operation = file_ns::get_compositor_operation;
185
187}
#define NODE_CLASS_OP_FILTER
Definition BKE_node.hh:451
#define CMP_NODE_BILATERALBLUR
@ R_FILTER_BOX
void GPU_shader_uniform_1f(blender::gpu::Shader *sh, const char *name, float value)
void GPU_shader_bind(blender::gpu::Shader *shader, const blender::gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_shader_uniform_1i(blender::gpu::Shader *sh, const char *name, int value)
void GPU_shader_unbind()
#define NOD_REGISTER_NODE(REGISTER_FUNC)
gpu::Shader * 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:523
void allocate_texture(const Domain domain, const bool from_pool=true, const std::optional< ResultStorageType > storage_type=std::nullopt)
Definition result.cc:389
void unbind_as_texture() const
Definition result.cc:511
void bind_as_texture(gpu::Shader *shader, const char *texture_name) const
Definition result.cc:487
void unbind_as_image() const
Definition result.cc:517
void bind_as_image(gpu::Shader *shader, const char *image_name, bool read=false) const
Definition result.cc:498
bool is_single_value() const
Definition result.cc:758
#define input
#define output
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
void symmetric_separable_blur(Context &context, const Result &input, Result &output, const float2 &radius, const int filter_type=R_FILTER_GAUSS)
void compute_dispatch_threads_at_least(gpu::Shader *shader, int2 threads_range, int2 local_size=int2(16))
Definition utilities.cc:196
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:238
std::string ui_description
Definition BKE_node.hh:244
NodeGetCompositorOperationFunction get_compositor_operation
Definition BKE_node.hh:348
const char * enum_name_legacy
Definition BKE_node.hh:247
NodeDeclareFunction declare
Definition BKE_node.hh:362
static pxr::UsdShadeInput get_input(const pxr::UsdShadeShader &usd_shader, const pxr::TfToken &input_name)