Blender V5.3
node_composite_dilate_erode.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 <limits>
6
7#include "BLI_index_range.hh"
8#include "BLI_math_base.hh"
10#include "BLI_task.hh"
11
12#include "RNA_enum_types.hh"
13#include "RNA_types.hh"
14
15#include "GPU_shader.hh"
16
20#include "COM_node_operation.hh"
21#include "COM_utilities.hh"
22
24
26
27static const EnumPropertyItem type_items[] = {
28 {CMP_NODE_DILATE_ERODE_STEP, "STEP", 0, N_("Steps"), ""},
29 {CMP_NODE_DILATE_ERODE_DISTANCE_THRESHOLD, "THRESHOLD", 0, N_("Threshold"), ""},
30 {CMP_NODE_DILATE_ERODE_DISTANCE, "DISTANCE", 0, N_("Distance"), ""},
31 {CMP_NODE_DILATE_ERODE_DISTANCE_FEATHER, "FEATHER", 0, N_("Feather"), ""},
32 {0, nullptr, 0, nullptr, nullptr},
33};
34
36{
37 b.add_input<decl::Float>("Mask"_ustr)
38 .default_value(0.0f)
39 .min(0.0f)
40 .max(1.0f)
41 .structure_type(StructureType::Dynamic);
42 b.add_input<decl::Int>("Size"_ustr)
43 .default_value(0)
45 .description(
46 "The size of dilation/erosion in pixels. Positive values dilates and negative values "
47 "erodes");
48 b.add_input<decl::Menu>("Type"_ustr)
49 .default_value(CMP_NODE_DILATE_ERODE_STEP)
50 .static_items(type_items)
52 b.add_input<decl::Float>("Falloff Size"_ustr)
53 .default_value(0.0f)
55 .min(0.0f)
56 .usage_by_menu("Type"_ustr, CMP_NODE_DILATE_ERODE_DISTANCE_THRESHOLD)
57 .description(
58 "The size of the falloff from the edges in pixels. If less than two pixels, the edges "
59 "will be anti-aliased");
60 b.add_input<decl::Menu>("Falloff"_ustr)
61 .default_value(PROP_SMOOTH)
64 .usage_by_menu("Type"_ustr, CMP_NODE_DILATE_ERODE_DISTANCE_FEATHER)
65 .translation_context(BLT_I18NCONTEXT_ID_CURVE_LEGACY);
66
67 b.add_output<decl::Float>("Mask"_ustr).structure_type(StructureType::Dynamic);
68}
69
70static void node_init(bNodeTree * /*ntree*/, bNode *node)
71{
72 /* Unused but kept for forward compatibility. */
73 NodeDilateErode *data = MEM_new<NodeDilateErode>(__func__);
74 node->storage = data;
75}
76
77using namespace blender::compositor;
78
80 public:
82
83 void execute() override
84 {
85 const Result &input = this->get_input("Mask");
86 Result &output = this->get_result("Mask");
87
88 if (this->is_identity()) {
89 output.share_data(input);
90 return;
91 }
92
93 switch (this->get_type()) {
96 return;
99 return;
102 return;
105 return;
106 }
107
108 output.share_data(input);
109 }
110
111 /* ----------------------------
112 * Step Morphological Operator.
113 * ---------------------------- */
114
116 {
117 Result horizontal_pass_result = execute_step_horizontal_pass();
118 execute_step_vertical_pass(horizontal_pass_result);
119 horizontal_pass_result.release();
120 }
121
123 {
124 if (this->context().use_gpu()) {
126 }
128 }
129
131 {
134
136
137 const Result &input_mask = get_input("Mask");
138 input_mask.bind_as_texture(shader, "input_tx");
139
140 /* We allocate an output image of a transposed size, that is, with a height equivalent to the
141 * width of the input and vice versa. This is done as a performance optimization. The shader
142 * will process the image horizontally and write it to the intermediate output transposed. Then
143 * the vertical pass will execute the same horizontal pass shader, but since its input is
144 * transposed, it will effectively do a vertical pass and write to the output transposed,
145 * effectively undoing the transposition in the horizontal pass. This is done to improve
146 * spatial cache locality in the shader and to avoid having two separate shaders for each of
147 * the passes. */
148 const Domain domain = compute_domain();
149 const int2 transposed_domain = int2(domain.data_size.y, domain.data_size.x);
150
151 Result horizontal_pass_result = context().create_result(ResultType::Float);
152 horizontal_pass_result.allocate_texture(transposed_domain);
153 horizontal_pass_result.bind_as_image(shader, "output_img");
154
156
158 input_mask.unbind_as_texture();
159 horizontal_pass_result.unbind_as_image();
160
161 return horizontal_pass_result;
162 }
163
165 {
166 const Result &input = get_input("Mask");
167
168 /* We allocate an output image of a transposed size, that is, with a height equivalent to the
169 * width of the input and vice versa. This is done as a performance optimization. The shader
170 * will process the image horizontally and write it to the intermediate output transposed. Then
171 * the vertical pass will execute the same horizontal pass shader, but since its input is
172 * transposed, it will effectively do a vertical pass and write to the output transposed,
173 * effectively undoing the transposition in the horizontal pass. This is done to improve
174 * spatial cache locality in the shader and to avoid having two separate shaders for each of
175 * the passes. */
176 const Domain domain = compute_domain();
177 const int2 transposed_domain = int2(domain.data_size.y, domain.data_size.x);
178
179 Result horizontal_pass_result = context().create_result(ResultType::Float);
180 horizontal_pass_result.allocate_texture(transposed_domain);
181
182 if (this->is_dilation()) {
183 this->execute_step_pass_cpu<true>(input, horizontal_pass_result);
184 }
185 else {
186 this->execute_step_pass_cpu<false>(input, horizontal_pass_result);
187 }
188
189 return horizontal_pass_result;
190 }
191
192 void execute_step_vertical_pass(Result &horizontal_pass_result)
193 {
194 if (this->context().use_gpu()) {
195 this->execute_step_vertical_pass_gpu(horizontal_pass_result);
196 }
197 else {
198 this->execute_step_vertical_pass_cpu(horizontal_pass_result);
199 }
200 }
201
202 void execute_step_vertical_pass_gpu(Result &horizontal_pass_result)
203 {
206
208
209 horizontal_pass_result.bind_as_texture(shader, "input_tx");
210
211 const Domain domain = compute_domain();
212 Result &output_mask = get_result("Mask");
213 output_mask.allocate_texture(domain);
214 output_mask.bind_as_image(shader, "output_img");
215
216 /* Notice that the domain is transposed, see the note on the horizontal pass method for more
217 * information on the reasoning behind this. */
219
221 horizontal_pass_result.unbind_as_texture();
222 output_mask.unbind_as_image();
223 }
224
226 {
227 if (this->is_dilation()) {
228 return "compositor_morphological_step_dilate";
229 }
230 return "compositor_morphological_step_erode";
231 }
232
233 void execute_step_vertical_pass_cpu(Result &horizontal_pass_result)
234 {
235 const Domain domain = compute_domain();
236 Result &output_mask = get_result("Mask");
237 output_mask.allocate_texture(domain);
238
239 if (this->is_dilation()) {
240 this->execute_step_pass_cpu<true>(horizontal_pass_result, output_mask);
241 }
242 else {
243 this->execute_step_pass_cpu<false>(horizontal_pass_result, output_mask);
244 }
245 }
246
247 /* Apply a van Herk/Gil-Werman algorithm on the input based on:
248 *
249 * Domanski, Luke, Pascal Vallotton, and Dadong Wang. "Parallel van Herk/Gil-Werman image
250 * morphology on GPUs using CUDA." GTC 2009 Conference posters. 2009.
251 *
252 * The output is written transposed for more efficient execution, see the horizontal pass method
253 * for more information. The template argument IsDilate decides if dilation or erosion will be
254 * performed. */
255 template<bool IsDilate> void execute_step_pass_cpu(const Result &input, Result &output)
256 {
257 const float limit = IsDilate ? std::numeric_limits<float>::lowest() :
258 std::numeric_limits<float>::max();
259 const auto morphology_operator = [](const float a, const float b) {
260 if constexpr (IsDilate) {
261 return math::max(a, b);
262 }
263 else {
264 return math::min(a, b);
265 }
266 };
267
268 /* Notice that the domain is transposed, see the note on the horizontal pass method for more
269 * information on the reasoning behind this. */
270 const int2 image_size = int2(output.domain().data_size.y, output.domain().data_size.x);
271
272 /* We process rows in tiles whose size is the same as the structuring element size. So we
273 * compute the number of tiles using ceiling division, noting that the last tile might not be
274 * complete. */
275 const int size = this->get_structuring_element_size();
276 const int tiles_count = int(math::ceil(float(image_size.x) / size));
277
278 /* Process along rows in parallel. */
279 threading::parallel_for(IndexRange(image_size.y), 1, [&](const IndexRange sub_y_range) {
280 Array<float> prefix_table(size);
281 Array<float> suffix_table(size);
282 for (const int64_t y : sub_y_range) {
283 for (const int64_t tile_index : IndexRange(tiles_count)) {
284 const int64_t tile_start = tile_index * size;
285 /* Compute the x texel location of the pixel at the center of the tile. Noting that the
286 * size of the structuring element is guaranteed to be odd. */
287 const int64_t tile_center = tile_start + size / 2;
288
289 float prefix_value = limit;
290 float suffix_value = limit;
291 /* Starting from the pixel at the center of the tile, recursively compute the prefix
292 * table to the right and the suffix table to the left by applying the morphology
293 * operator. */
294 for (const int64_t i : IndexRange(size)) {
295 const float right_value = input.load_pixel_fallback(int2(tile_center + i, y), limit);
296 prefix_value = morphology_operator(prefix_value, right_value);
297 prefix_table[i] = prefix_value;
298
299 /* Note that we access pixels increasingly to the left, so invert the suffix table when
300 * writing to it. */
301 const float left_value = input.load_pixel_fallback(int2(tile_center - i, y), limit);
302 suffix_value = morphology_operator(suffix_value, left_value);
303 suffix_table[size - 1 - i] = suffix_value;
304 }
305
306 const IndexRange tile_range = IndexRange(tile_start, size);
307 const IndexRange safe_tile_range = tile_range.intersect(IndexRange(image_size.x));
308 /* For each pixel in the tile, write the result of applying the morphology operator on
309 * the prefix and suffix values. */
310 for (const int64_t x : safe_tile_range) {
311 /* Compute the local table index, since the prefix and suffix tables are local to each
312 * tile. */
313 const int64_t table_index = x - tile_start;
314 const float prefix_value = prefix_table[table_index];
315 const float suffix_value = suffix_table[table_index];
316
317 const float value = morphology_operator(prefix_value, suffix_value);
318
319 /* Write the value using the transposed texel. See the horizontal pass method for more
320 * information on the rational behind this. */
321 output.store_pixel(int2(y, x), value);
322 }
323 }
324 }
325 });
326 }
327
328 /* --------------------------------
329 * Distance Morphological Operator.
330 * -------------------------------- */
331
333 {
334 morphological_distance(context(), get_input("Mask"), get_result("Mask"), this->get_size());
335 }
336
337 /* ------------------------------------------
338 * Distance Threshold Morphological Operator.
339 * ------------------------------------------ */
340
342 {
343 Result masked_pixels = this->context().create_result(ResultType::Int2, ResultPrecision::Half);
344 Result unmasked_pixels = this->context().create_result(ResultType::Int2,
346 this->compute_distance_threshold_seeds(masked_pixels, unmasked_pixels);
347
348 Result flooded_masked_pixels = this->context().create_result(ResultType::Int2,
350 Result flooded_unmasked_pixels = this->context().create_result(ResultType::Int2,
352 jump_flooding(this->context(), masked_pixels, flooded_masked_pixels);
353 masked_pixels.release();
354 jump_flooding(this->context(), unmasked_pixels, flooded_unmasked_pixels);
355 unmasked_pixels.release();
356
357 this->compute_distance_threshold(flooded_masked_pixels, flooded_unmasked_pixels);
358 flooded_masked_pixels.release();
359 flooded_unmasked_pixels.release();
360 }
361
362 /* Compute an image that marks both masked and unmasked pixels as seed pixels for the jump
363 * flooding algorithm. */
364 void compute_distance_threshold_seeds(Result &masked_pixels, Result &unmasked_pixels)
365 {
366 if (this->context().use_gpu()) {
367 this->compute_distance_threshold_seeds_gpu(masked_pixels, unmasked_pixels);
368 return;
369 }
370
371 this->compute_distance_threshold_seeds_cpu(masked_pixels, unmasked_pixels);
372 }
373
374 void compute_distance_threshold_seeds_gpu(Result &masked_pixels, Result &unmasked_pixels)
375 {
376 gpu::Shader *shader = this->context().get_shader(
377 "compositor_morphological_distance_threshold_seeds", ResultPrecision::Half);
379
380 const Result &mask = this->get_input("Mask");
381 mask.bind_as_texture(shader, "mask_tx");
382
383 const Domain domain = mask.domain();
384 masked_pixels.allocate_texture(domain);
385 masked_pixels.bind_as_image(shader, "masked_pixels_img");
386 unmasked_pixels.allocate_texture(domain);
387 unmasked_pixels.bind_as_image(shader, "unmasked_pixels_img");
388
390
391 mask.unbind_as_texture();
392 masked_pixels.unbind_as_image();
393 unmasked_pixels.unbind_as_image();
395 }
396
397 void compute_distance_threshold_seeds_cpu(Result &masked_pixels, Result &unmasked_pixels)
398 {
399 const Result &mask = this->get_input("Mask");
400
401 const Domain domain = mask.domain();
402 masked_pixels.allocate_texture(domain);
403 unmasked_pixels.allocate_texture(domain);
404
405 parallel_for(domain.data_size, [&](const int2 texel) {
406 const bool is_masked = mask.load_pixel<float>(texel) > 0.5f;
407
408 const int2 masked_jump_flooding_value = initialize_jump_flooding_value(texel, is_masked);
409 masked_pixels.store_pixel(texel, masked_jump_flooding_value);
410
411 const int2 unmasked_jump_flooding_value = initialize_jump_flooding_value(texel, !is_masked);
412 unmasked_pixels.store_pixel(texel, unmasked_jump_flooding_value);
413 });
414 }
415
416 void compute_distance_threshold(const Result &flooded_masked_pixels,
417 const Result &flooded_unmasked_pixels)
418 {
419 if (this->context().use_gpu()) {
420 this->compute_distance_threshold_gpu(flooded_masked_pixels, flooded_unmasked_pixels);
421 return;
422 }
423
424 this->compute_distance_threshold_cpu(flooded_masked_pixels, flooded_unmasked_pixels);
425 }
426
427 void compute_distance_threshold_gpu(const Result &flooded_masked_pixels,
428 const Result &flooded_unmasked_pixels)
429 {
430 gpu::Shader *shader = this->context().get_shader(
431 "compositor_morphological_distance_threshold");
433
434 GPU_shader_uniform_1i(shader, "distance_offset", this->get_size());
435 GPU_shader_uniform_1f(shader, "falloff_size", this->get_falloff_size());
436
437 const Result &input_mask = this->get_input("Mask");
438 input_mask.bind_as_texture(shader, "mask_tx");
439
440 flooded_masked_pixels.bind_as_texture(shader, "flooded_masked_pixels_tx");
441 flooded_unmasked_pixels.bind_as_texture(shader, "flooded_unmasked_pixels_tx");
442
443 const Domain domain = this->compute_domain();
444 Result &output = this->get_result("Mask");
445 output.allocate_texture(domain);
446 output.bind_as_image(shader, "output_img");
447
449
451 output.unbind_as_image();
452 input_mask.unbind_as_texture();
453 }
454
455 void compute_distance_threshold_cpu(const Result &flooded_masked_pixels,
456 const Result &flooded_unmasked_pixels)
457 {
458 const Result &mask = this->get_input("Mask");
459
460 const Domain domain = this->compute_domain();
461 Result &output = this->get_result("Mask");
462 output.allocate_texture(domain);
463
464 const float falloff_size = this->get_falloff_size();
465 const int distance_offset = this->get_size();
466
467 parallel_for(domain.data_size, [&](const int2 texel) {
468 const bool is_masked = mask.load_pixel<float>(texel) > 0.5f;
469 const int2 closest_masked_texel = flooded_masked_pixels.load_pixel<int2>(texel);
470 const int2 closest_unmasked_texel = flooded_unmasked_pixels.load_pixel<int2>(texel);
471 const int2 closest_different_texel = is_masked ? closest_unmasked_texel :
472 closest_masked_texel;
473 const float distance_to_different = math::distance(float2(texel),
474 float2(closest_different_texel));
475 const float signed_distance = is_masked ? distance_to_different : -distance_to_different;
476 const float value = math::clamp(
477 (signed_distance + distance_offset) / falloff_size, 0.0f, 1.0f);
478
479 output.store_pixel(texel, value);
480 });
481 }
482
483 /* ----------------------------------------
484 * Distance Feather Morphological Operator.
485 * ---------------------------------------- */
486
488 {
490 context(), get_input("Mask"), get_result("Mask"), this->get_size(), this->get_falloff());
491 }
492
493 /* ---------------
494 * Common Methods.
495 * --------------- */
496
498 {
499 const Result &input = get_input("Mask");
500 if (input.is_single_value()) {
501 return true;
502 }
503
505 return false;
506 }
507
508 if (this->get_size() == 0) {
509 return true;
510 }
511
512 return false;
513 }
514
515 /* Gets the size of the structuring element. See the get_size method for more information. */
517 {
518 return math::abs(this->get_size()) * 2 + 1;
519 }
520
521 /* Returns true if dilation should be performed, as opposed to erosion. See the get_size()
522 * method for more information. */
524 {
525 return this->get_size() > 0;
526 }
527
528 /* The signed radius of the structuring element, that is, half the structuring element size. The
529 * sign indicates either dilation or erosion, where negative values means erosion. */
531 {
532 return this->get_input("Size").get_single_value_default<int>();
533 }
534
536 {
537 return math::max(10e-6f, this->get_input("Falloff Size").get_single_value_default<float>());
538 }
539
541 {
543 this->get_input("Type").get_single_value_default<MenuValue>().value);
544 }
545
547 {
548 return this->get_input("Falloff").get_single_value_default<MenuValue>().value;
549 }
550};
551
553{
554 return new DilateErodeOperation(context, node);
555}
556
557static void node_register()
558{
559 static bke::bNodeType ntype;
560
561 cmp_node_type_base(&ntype, "CompositorNodeDilateErode"_ustr, CMP_NODE_DILATEERODE);
562 ntype.ui_name = "Dilate/Erode";
563 ntype.ui_description = "Expand and shrink masks";
564 ntype.enum_name_legacy = "DILATEERODE";
566 ntype.declare = node_declare;
567 ntype.initfunc = node_init;
569 ntype, "NodeDilateErode", node_free_standard_storage, node_copy_standard_storage);
571
573}
574NOD_REGISTER_NODE(node_register)
575
576} // namespace blender::nodes::node_composite_dilate_erode_cc
#define NODE_CLASS_OP_FILTER
Definition BKE_node.hh:482
#define CMP_NODE_DILATEERODE
#define BLT_I18NCONTEXT_ID_CURVE_LEGACY
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define output
constexpr IndexRange()=default
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
virtual Domain compute_domain()
Definition operation.cc:62
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 compute_distance_threshold_gpu(const Result &flooded_masked_pixels, const Result &flooded_unmasked_pixels)
void compute_distance_threshold_seeds_cpu(Result &masked_pixels, Result &unmasked_pixels)
void compute_distance_threshold_cpu(const Result &flooded_masked_pixels, const Result &flooded_unmasked_pixels)
void compute_distance_threshold(const Result &flooded_masked_pixels, const Result &flooded_unmasked_pixels)
void compute_distance_threshold_seeds_gpu(Result &masked_pixels, Result &unmasked_pixels)
void compute_distance_threshold_seeds(Result &masked_pixels, Result &unmasked_pixels)
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
void morphological_distance(Context &context, const Result &input, Result &output, const int distance)
void morphological_distance_feather(Context &context, const Result &input, Result &output, const int distance, const int falloff_type=PROP_SMOOTH)
void jump_flooding(Context &context, Result &input, Result &output)
void parallel_for(const int2 range, const Function &function)
T min(const T &a, const T &b)
T ceil(const T &a)
T max(const T &a, const T &b)
T abs(const T &a)
static NodeOperation * get_compositor_operation(Context &context, const bNode &node)
static void node_declare(NodeDeclarationBuilder &b)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
BMesh const char void * data
void GPU_shader_unbind()
@ CMP_NODE_DILATE_ERODE_DISTANCE_FEATHER
@ CMP_NODE_DILATE_ERODE_STEP
@ CMP_NODE_DILATE_ERODE_DISTANCE
@ CMP_NODE_DILATE_ERODE_DISTANCE_THRESHOLD
const EnumPropertyItem rna_enum_proportional_falloff_curve_only_items[]
Definition rna_scene.cc:119
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 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)