32 {0,
nullptr, 0,
nullptr,
nullptr},
46 "The size of dilation/erosion in pixels. Positive values dilates and negative values "
58 "The size of the falloff from the edges in pixels. If less than two pixels, the edges "
59 "will be anti-aliased");
67 b.add_output<
decl::Float>(
"Mask"_ustr).structure_type(StructureType::Dynamic);
119 horizontal_pass_result.
release();
124 if (this->
context().use_gpu()) {
161 return horizontal_pass_result;
189 return horizontal_pass_result;
194 if (this->
context().use_gpu()) {
228 return "compositor_morphological_step_dilate";
230 return "compositor_morphological_step_erode";
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) {
276 const int tiles_count = int(
math::ceil(
float(image_size.x) / size));
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;
287 const int64_t tile_center = tile_start + size / 2;
289 float prefix_value = limit;
290 float suffix_value = limit;
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;
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;
306 const IndexRange tile_range = IndexRange(tile_start, size);
307 const IndexRange safe_tile_range = tile_range.intersect(IndexRange(image_size.x));
310 for (const int64_t x : safe_tile_range) {
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];
317 const float value = morphology_operator(prefix_value, suffix_value);
321 output.store_pixel(int2(y, x), value);
358 flooded_masked_pixels.
release();
359 flooded_unmasked_pixels.
release();
366 if (this->
context().use_gpu()) {
391 mask.unbind_as_texture();
406 const bool is_masked = mask.load_pixel<float>(texel) > 0.5f;
408 const int2 masked_jump_flooding_value = initialize_jump_flooding_value(texel, is_masked);
409 masked_pixels.store_pixel(texel, masked_jump_flooding_value);
411 const int2 unmasked_jump_flooding_value = initialize_jump_flooding_value(texel, !is_masked);
412 unmasked_pixels.store_pixel(texel, unmasked_jump_flooding_value);
417 const Result &flooded_unmasked_pixels)
419 if (this->
context().use_gpu()) {
428 const Result &flooded_unmasked_pixels)
431 "compositor_morphological_distance_threshold");
445 output.allocate_texture(domain);
456 const Result &flooded_unmasked_pixels)
462 output.allocate_texture(domain);
465 const int distance_offset = this->
get_size();
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);
479 output.store_pixel(texel, value);
532 return this->
get_input(
"Size").get_single_value_default<
int>();
537 return math::max(10e-6f, this->
get_input(
"Falloff Size").get_single_value_default<float>());
543 this->
get_input(
"Type").get_single_value_default<MenuValue>().value);
562 ntype.
ui_name =
"Dilate/Erode";
#define NODE_CLASS_OP_FILTER
#define CMP_NODE_DILATEERODE
#define BLT_I18NCONTEXT_ID_CURVE_LEGACY
#define NOD_REGISTER_NODE(REGISTER_FUNC)
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)
Context & context() const
Result & get_input(StringRef identifier) const
virtual Domain compute_domain()
bool is_single_value() const
void allocate_texture(const Domain domain, const bool from_pool=true, const std::optional< ResultStorageType > storage_type=std::nullopt)
void unbind_as_texture() const
void bind_as_texture(gpu::Shader *shader, const char *texture_name) const
void unbind_as_image() const
void bind_as_image(gpu::Shader *shader, const char *image_name, bool read=false) const
StructureType structure_type
void compute_distance_threshold_gpu(const Result &flooded_masked_pixels, const Result &flooded_unmasked_pixels)
NodeOperation(Context &context, const bNode &node)
int get_structuring_element_size()
void execute_distance_threshold()
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 execute_step_vertical_pass_gpu(Result &horizontal_pass_result)
void execute_step_vertical_pass_cpu(Result &horizontal_pass_result)
void compute_distance_threshold(const Result &flooded_masked_pixels, const Result &flooded_unmasked_pixels)
void execute_distance_feather()
CMPNodeDilateErodeMethod get_type()
Result execute_step_horizontal_pass_gpu()
void execute_step_vertical_pass(Result &horizontal_pass_result)
const char * get_morphological_step_shader_name()
Result execute_step_horizontal_pass()
void compute_distance_threshold_seeds_gpu(Result &masked_pixels, Result &unmasked_pixels)
void execute_step_pass_cpu(const Result &input, Result &output)
void compute_distance_threshold_seeds(Result &masked_pixels, Result &unmasked_pixels)
Result execute_step_horizontal_pass_cpu()
void node_register_type(bNodeType &ntype)
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))
void compute_dispatch_threads_at_least(gpu::Shader *shader, int2 threads_range, int2 local_size=int2(16))
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 max(const T &a, const T &b)
static void node_init(bNodeTree *, bNode *node)
static NodeOperation * get_compositor_operation(Context &context, const bNode &node)
static void node_declare(NodeDeclarationBuilder &b)
static const EnumPropertyItem type_items[]
static void node_register()
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
BMesh const char void * data
@ 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[]
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)
void node_free_standard_storage(bNode *node)
void cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
std::string ui_description
NodeGetCompositorOperationFunction get_compositor_operation
void(*) initfunc(bNodeTree *ntree, bNode *node)
const char * enum_name_legacy
NodeDeclareFunction declare