Blender V5.3
node_composite_bokeh_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"
7
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({0.8f, 0.8f, 0.8f, 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>("Bokeh"_ustr)
30 .default_value({1.0f, 1.0f, 1.0f, 1.0f})
31 .compositor_realization_mode(CompositorInputRealizationMode::Transforms)
32 .structure_type(StructureType::Dynamic);
33 b.add_input<decl::Float>("Size"_ustr)
34 .default_value(0.0f)
36 .min(0.0f)
37 .structure_type(StructureType::Dynamic);
38 b.add_input<decl::Float>("Mask"_ustr)
39 .default_value(1.0f)
40 .min(0.0f)
41 .max(1.0f)
42 .structure_type(StructureType::Dynamic);
43 b.add_input<decl::Bool>("Extend Bounds"_ustr).default_value(false);
44}
45
46using namespace blender::compositor;
47
49 public:
51
52 void execute() override
53 {
54 const Result &input = this->get_input("Image");
55 Result &output = this->get_result("Image");
56 if (this->is_identity()) {
57 output.share_data(input);
58 return;
59 }
60
61 const Result &size = this->get_input("Size");
62 const Result &mask = this->get_input("Mask");
63 if (this->get_extend_bounds()) {
64 Result padded_input = this->context().create_result(ResultType::Color);
65 Result padded_size = this->context().create_result(ResultType::Float);
66 Result padded_mask = this->context().create_result(ResultType::Float);
67
68 const int2 padding_size = int2(this->compute_extended_boundary_size(size));
69
70 pad(this->context(), input, padded_input, padding_size, PaddingMethod::Zero);
71 pad(this->context(), size, padded_size, padding_size, PaddingMethod::Extend);
72 pad(this->context(), mask, padded_mask, padding_size, PaddingMethod::Extend);
73
74 this->execute_blur(padded_input, padded_size, padded_mask);
75 padded_input.release();
76 padded_size.release();
77 padded_mask.release();
78 }
79 else {
80 this->execute_blur(input, size, mask);
81 }
82 }
83
84 /* Computes the number of pixels that the image should be extended by if Extend Bounds is
85 * enabled. */
87 {
89
90 /* For constant sized blur, the extension should just be the blur radius. */
91 if (size.is_single_value()) {
92 return this->get_blur_radius();
93 }
94
95 /* For variable sized blur, the extension should be the bokeh search radius. */
97 }
98
99 void execute_blur(const Result &input, const Result &size, const Result &mask)
100 {
101 if (size.is_single_value()) {
102 this->execute_constant_size(input, mask);
103 }
104 else {
105 this->execute_variable_size(input, size, mask);
106 }
107 }
108
109 void execute_constant_size(const Result &input, const Result &mask)
110 {
111 if (this->context().use_gpu()) {
112 this->execute_constant_size_gpu(input, mask);
113 }
114 else {
115 this->execute_constant_size_cpu(input, mask);
116 }
117 }
118
119 void execute_constant_size_gpu(const Result &input, const Result &mask)
120 {
121 gpu::Shader *shader = context().get_shader("compositor_bokeh_blur");
123
124 GPU_shader_uniform_1i(shader, "radius", this->get_blur_radius());
125
126 input.bind_as_texture(shader, "input_tx");
127
128 const Result &input_weights = this->get_input("Bokeh");
129 gpu::Texture *input_weights_texture = input_weights.bind_as_texture_or_single_value(
130 shader, "weights_tx");
131
132 gpu::Texture *mask_texture = mask.bind_as_texture_or_single_value(shader, "mask_tx");
133
134 const Domain domain = input.domain();
135 Result &output_image = this->get_result("Image");
136 output_image.allocate_texture(domain);
137 output_image.bind_as_image(shader, "output_img");
138
140
142 output_image.unbind_as_image();
143 input.unbind_as_texture();
144 input_weights.unbind_as_texture_or_single_value(input_weights_texture);
145 mask.unbind_as_texture_or_single_value(mask_texture);
146 }
147
148 void execute_constant_size_cpu(const Result &input, const Result &mask_image)
149 {
150 const int radius = this->get_blur_radius();
151
152 const Domain domain = input.domain();
153 Result &output = this->get_result("Image");
154 output.allocate_texture(domain);
155
156 Result blur_kernel = this->compute_blur_kernel(radius);
157
158 parallel_for(domain.data_size, [&](const int2 texel) {
159 /* The mask input is treated as a boolean. If it is zero, then no blurring happens for this
160 * pixel. Otherwise, the pixel is blurred normally and the mask value is irrelevant. */
161 float mask = mask_image.load_pixel<float, true>(texel);
162 if (mask == 0.0f) {
163 output.store_pixel(texel, input.load_pixel<Color>(texel));
164 return;
165 }
166
167 /* Go over the window of the given radius and accumulate the colors multiplied by their
168 * respective weights as well as the weights themselves. */
169 float4 accumulated_color = float4(0.0f);
170 float4 accumulated_weight = float4(0.0f);
171 for (int y = -radius; y <= radius; y++) {
172 for (int x = -radius; x <= radius; x++) {
173 float4 weight = float4(blur_kernel.load_pixel<Color>(int2(x, y) + radius));
174 accumulated_color += float4(input.load_pixel_extended<Color>(texel + int2(x, y))) *
175 weight;
176 accumulated_weight += weight;
177 }
178 }
179
180 output.store_pixel(texel, Color(math::safe_divide(accumulated_color, accumulated_weight)));
181 });
182
183 blur_kernel.release();
184 }
185
186 void execute_variable_size(const Result &input, const Result &size, const Result &mask)
187 {
188 if (this->context().use_gpu()) {
189 this->execute_variable_size_gpu(input, size, mask);
190 }
191 else {
192 this->execute_variable_size_cpu(input, size, mask);
193 }
194 }
195
196 void execute_variable_size_gpu(const Result &input, const Result &size, const Result &mask)
197 {
198 const int search_radius = this->compute_variable_size_search_radius();
199
200 gpu::Shader *shader = this->context().get_shader("compositor_bokeh_blur_variable_size");
202
203 GPU_shader_uniform_1i(shader, "search_radius", search_radius);
204
205 input.bind_as_texture(shader, "input_tx");
206
207 const Result &input_weights = this->get_input("Bokeh");
208 gpu::Texture *input_weights_texture = input_weights.bind_as_texture_or_single_value(
209 shader, "weights_tx");
210
211 size.bind_as_texture(shader, "size_tx");
212
213 gpu::Texture *mask_texture = mask.bind_as_texture_or_single_value(shader, "mask_tx");
214
215 const Domain domain = input.domain();
216 Result &output_image = this->get_result("Image");
217 output_image.allocate_texture(domain);
218 output_image.bind_as_image(shader, "output_img");
219
221
223 output_image.unbind_as_image();
224 input.unbind_as_texture();
225 input_weights.unbind_as_texture_or_single_value(input_weights_texture);
226 size.unbind_as_texture();
227 mask.unbind_as_texture_or_single_value(mask_texture);
228 }
229
231 const Result &size_input,
232 const Result &mask_image)
233 {
234 const int search_radius = this->compute_variable_size_search_radius();
235
236 const Result &weights = this->get_input("Bokeh");
237
238 const Domain domain = input.domain();
239 Result &output = this->get_result("Image");
240 output.allocate_texture(domain);
241
242 /* Given the texel in the range [-radius, radius] in both axis, load the appropriate weight
243 * from the weights image, where the given texel (0, 0) corresponds the center of weights
244 * image. Note that we load the weights image inverted along both directions to maintain
245 * the shape of the weights if it was not symmetrical. To understand why inversion makes sense,
246 * consider a 1D weights image whose right half is all ones and whose left half is all zeros.
247 * Further, consider that we are blurring a single white pixel on a black background. When
248 * computing the value of a pixel that is to the right of the white pixel, the white pixel will
249 * be in the left region of the search window, and consequently, without inversion, a zero will
250 * be sampled from the left side of the weights image and result will be zero. However, what
251 * we expect is that pixels to the right of the white pixel will be white, that is, they should
252 * sample a weight of 1 from the right side of the weights image, hence the need for
253 * inversion. */
254 auto load_weight = [&](const int2 &texel, const float radius) {
255 /* The center zero texel is always assigned a unit weight regardless of the corresponding
256 * weight in the weights image. That's to guarantee that at last the center pixel will be
257 * accumulated even if the weights image is zero at its center. */
258 if (texel.x == 0 && texel.y == 0) {
259 return float4(1.0f);
260 }
261
262 /* Add the radius to transform the texel into the range [0, radius * 2], with an additional
263 * 0.5 to sample at the center of the pixels, then divide by the upper bound plus one to
264 * transform the texel into the normalized range [0, 1] needed to sample the weights sampler.
265 * Finally, invert the textures coordinates by subtracting from 1 to maintain the shape of
266 * the weights as mentioned in the function description. */
267 return float4(weights.sample_bilinear_extended<Color, true>(
268 1.0f - ((float2(texel) + float2(radius + 0.5f)) / (radius * 2.0f + 1.0f))));
269 };
270
271 parallel_for(domain.data_size, [&](const int2 texel) {
272 /* The mask input is treated as a boolean. If it is zero, then no blurring happens for this
273 * pixel. Otherwise, the pixel is blurred normally and the mask value is irrelevant. */
274 float mask = mask_image.load_pixel<float, true>(texel);
275 if (mask == 0.0f) {
276 output.store_pixel(texel, input.load_pixel<Color>(texel));
277 return;
278 }
279
280 float center_size = math::max(0.0f, size_input.load_pixel<float>(texel));
281
282 /* Go over the window of the given search radius and accumulate the colors multiplied by
283 * their respective weights as well as the weights themselves, but only if both the size of
284 * the center pixel and the size of the candidate pixel are less than both the x and y
285 * distances of the candidate pixel. */
286 float4 accumulated_color = float4(0.0f);
287 float4 accumulated_weight = float4(0.0f);
288 for (int y = -search_radius; y <= search_radius; y++) {
289 for (int x = -search_radius; x <= search_radius; x++) {
290 float candidate_size = math::max(
291 0.0f, size_input.load_pixel_extended<float>(texel + int2(x, y)));
292
293 /* Skip accumulation if either the x or y distances of the candidate pixel are larger
294 * than either the center or candidate pixel size. Note that the max and min functions
295 * here denote "either" in the aforementioned description. */
296 float size = math::min(center_size, candidate_size);
297 if (math::max(math::abs(x), math::abs(y)) > size) {
298 continue;
299 }
300
301 float4 weight = load_weight(int2(x, y), size);
302 accumulated_color += float4(input.load_pixel_extended<Color>(texel + int2(x, y))) *
303 weight;
304 accumulated_weight += weight;
305 }
306 }
307
308 output.store_pixel(texel, Color(math::safe_divide(accumulated_color, accumulated_weight)));
309 });
310 }
311
312 /* Compute a blur kernel from the bokeh result by interpolating it to the size of the kernel.
313 * Note that we load the bokeh result inverted along both directions to maintain the shape of the
314 * weights if it was not symmetrical. To understand why inversion makes sense, consider a 1D
315 * weights image whose right half is all ones and whose left half is all zeros. Further, consider
316 * that we are blurring a single white pixel on a black background. When computing the value of a
317 * pixel that is to the right of the white pixel, the white pixel will be in the left region of
318 * the search window, and consequently, without inversion, a zero will be sampled from the left
319 * side of the weights image and result will be zero. However, what we expect is that pixels to
320 * the right of the white pixel will be white, that is, they should sample a weight of 1 from the
321 * right side of the weights image, hence the need for inversion. */
322 Result compute_blur_kernel(const int radius)
323 {
324 const Result &bokeh = this->get_input("Bokeh");
325
326 Result kernel = this->context().create_result(ResultType::Color);
327 const int2 kernel_size = int2(radius * 2 + 1);
328 kernel.allocate_texture(kernel_size);
329 parallel_for(kernel_size, [&](const int2 texel) {
330 /* Add 0.5 to sample at the center of the pixels, then divide by the kernel size to transform
331 * the texel into the normalized range [0, 1] needed to sample the bokeh result. Finally,
332 * invert the textures coordinates by subtracting from 1 to maintain the shape of the weights
333 * as mentioned above. */
334 const float2 weight_coordinates = 1.0f - ((float2(texel) + 0.5f) / float2(kernel_size));
335 float4 weight = float4(bokeh.sample_bilinear_extended<Color, true>(weight_coordinates));
336 kernel.store_pixel(texel, Color(weight));
337 });
338
339 return kernel;
340 }
341
343 {
344 return math::max(0, int(maximum_float(context(), this->get_input("Size"))));
345 }
346
348 {
349 return math::max(0, int(this->get_input("Size").get_single_value<float>()));
350 }
351
353 {
354 const Result &input = this->get_input("Image");
355 if (input.is_single_value()) {
356 return true;
357 }
358
359 const Result &size = this->get_input("Size");
360 if (size.is_single_value() && this->get_blur_radius() == 0) {
361 return true;
362 }
363
364 const Result &mask = this->get_input("Mask");
365 if (mask.is_single_value() && mask.get_single_value<float>() == 0.0) {
366 return true;
367 }
368
369 return false;
370 }
371
373 {
374 return this->get_input("Extend Bounds").get_single_value_default<bool>();
375 }
376};
377
379{
380 return new BokehBlurOperation(context, node);
381}
382
383static void node_register()
384{
385 static bke::bNodeType ntype;
386
387 cmp_node_type_base(&ntype, "CompositorNodeBokehBlur"_ustr, CMP_NODE_BOKEHBLUR);
388 ntype.ui_name = "Bokeh Blur";
389 ntype.ui_description =
390 "Generate a bokeh type blur similar to Defocus. Unlike defocus an in-focus region is "
391 "defined in the compositor";
392 ntype.enum_name_legacy = "BOKEHBLUR";
394 ntype.declare = node_declare;
397
399}
400NOD_REGISTER_NODE(node_register)
401
402} // namespace blender::nodes::node_composite_bokeh_blur_cc
#define NODE_CLASS_OP_FILTER
Definition BKE_node.hh:482
#define CMP_NODE_BOKEHBLUR
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#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_or_single_value(gpu::Texture *single_value_texture) const
Definition result.cc:970
void store_pixel(const int2 &texel, const T &pixel_value)
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 load_pixel_extended(const int2 &texel) const
T load_pixel(const int2 &texel) 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
gpu::Texture * bind_as_texture_or_single_value(gpu::Shader *shader, const char *texture_name) const
Definition result.cc:861
void execute_constant_size_gpu(const Result &input, const Result &mask)
void execute_constant_size_cpu(const Result &input, const Result &mask_image)
void execute_variable_size_gpu(const Result &input, const Result &size, const Result &mask)
void execute_variable_size_cpu(const Result &input, const Result &size_input, const Result &mask_image)
void execute_blur(const Result &input, const Result &size, const Result &mask)
void execute_variable_size(const Result &input, const Result &size, const Result &mask)
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 pad(Context &context, const Result &input, Result &output, const int2 size, const PaddingMethod padding_method)
Definition pad.cc:113
static float bokeh(const float2 point, const float circumradius, const float exterior_angle, const float rotation, const float roundness, const float catadioptric)
void parallel_for(const int2 range, const Function &function)
float maximum_float(Context &context, const Result &input)
T safe_divide(const T &a, const T &b)
T min(const T &a, const T &b)
T max(const T &a, const T &b)
T abs(const T &a)
static void node_declare(NodeDeclarationBuilder &b)
static NodeOperation * get_compositor_operation(Context &context, const bNode &node)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
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 cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
static constexpr int _160
Definition BKE_node.hh:246
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