Blender V5.3
node_composite_mask_to_sdf.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_math_vector.hh"
7
10#include "COM_utilities.hh"
11
13
15
17{
18 b.add_input<decl::Bool>("Mask"_ustr).hide_value().structure_type(StructureType::Dynamic);
19
20 b.add_output<decl::Float>("SDF"_ustr)
21 .structure_type(StructureType::Dynamic)
23 "The distance in pixel to the nearest pixel at the boundary of the mask. The distance "
24 "is negative inside the mask");
25 b.add_output<decl::IntVector>("Nearest Pixel"_ustr)
26 .dimensions(2)
27 .structure_type(StructureType::Dynamic)
28 .description("The integer coordinates of the nearest pixel at the boundary of the mask");
29}
30
31using namespace blender::compositor;
32
34 public:
36
37 void execute() override
38 {
39 const Result &input_mask = this->get_input("Mask");
40 Result &distance_output = this->get_result("SDF");
41
42 Result &nearest_pixel_output = this->get_result("Nearest Pixel");
43 nearest_pixel_output.set_precision(ResultPrecision::Half);
44
45 if (input_mask.is_single_value()) {
46 if (distance_output.should_compute()) {
47 distance_output.allocate_single_value();
48 distance_output.set_single_value(0.0f);
49 }
50 if (nearest_pixel_output.should_compute()) {
51 nearest_pixel_output.allocate_single_value();
52 nearest_pixel_output.set_single_value(int2(0));
53 }
54 return;
55 }
56
57 Result mask_boundary = this->compute_boundary();
58
59 Result flooded_boundary = this->context().create_result(ResultType::Int2,
61 jump_flooding(this->context(), mask_boundary, flooded_boundary);
62 mask_boundary.release();
63
64 if (distance_output.should_compute()) {
65 this->compute_signed_distance(flooded_boundary);
66 }
67
68 if (nearest_pixel_output.should_compute()) {
69 nearest_pixel_output.share_data(flooded_boundary);
70 }
71
72 flooded_boundary.release();
73 }
74
75 /* Compute an image that marks the boundary pixels of the mask region as seed pixels for
76 * the jump flooding algorithm. */
78 {
79 if (this->context().use_gpu()) {
80 return this->compute_boundary_gpu();
81 }
82
83 return this->compute_boundary_cpu();
84 }
85
87 {
88 gpu::Shader *shader = this->context().get_shader("compositor_mask_to_sdf_compute_boundary",
91
92 const Result &mask = this->get_input("Mask");
93 mask.bind_as_texture(shader, "mask_tx");
94
96 const Domain domain = mask.domain();
97 boundary.allocate_texture(domain);
98 boundary.bind_as_image(shader, "boundary_img");
99
101
102 mask.unbind_as_texture();
103 boundary.unbind_as_image();
105
106 return boundary;
107 }
108
110 {
111 const Result &mask = this->get_input("Mask");
112
114 const Domain domain = mask.domain();
115 boundary.allocate_texture(domain);
116
117 /* The mask to SDF operation uses a jump flood algorithm to flood the region to be distance
118 * transformed with the pixels at its boundary. The algorithms expects an input image whose
119 * values are those returned by the initialize_jump_flooding_value function, given the texel
120 * location and a boolean specifying if the pixel is a boundary one.
121 *
122 * Technically, we needn't restrict the output to just the boundary pixels, since the algorithm
123 * can still operate if the interior of the region was also included. However, the algorithm
124 * operates more accurately when the number of pixels to be flooded is minimum. */
125 parallel_for(domain.data_size, [&](const int2 texel) {
126 /* Identify if any of the 8 neighbors around the center pixel are unmasked. */
127 bool has_unmasked_neighbors = false;
128 for (int j = -1; j <= 1; j++) {
129 for (int i = -1; i <= 1; i++) {
130 const int2 offset = int2(i, j);
131
132 /* Exempt the center pixel. */
133 if (offset == int2(0)) {
134 continue;
135 }
136
137 if (!mask.load_pixel_extended<bool>(texel + offset)) {
138 has_unmasked_neighbors = true;
139 break;
140 }
141 }
142 }
143
144 /* The pixels at the boundary are those that are masked and have unmasked neighbors. */
145 const bool is_masked = mask.load_pixel<bool>(texel);
146 const bool is_boundary_pixel = is_masked && has_unmasked_neighbors;
147
148 /* Encode the boundary information in the format expected by the jump flooding algorithm. */
149 const int2 jump_flooding_value = initialize_jump_flooding_value(texel, is_boundary_pixel);
150
151 boundary.store_pixel(texel, jump_flooding_value);
152 });
153
154 return boundary;
155 }
156
157 void compute_signed_distance(const Result &flooded_boundary)
158 {
159 if (this->context().use_gpu()) {
160 this->compute_signed_distance_gpu(flooded_boundary);
161 }
162 else {
163 this->compute_signed_distance_cpu(flooded_boundary);
164 }
165 }
166
167 void compute_signed_distance_gpu(const Result &flooded_boundary)
168 {
169 gpu::Shader *shader = this->context().get_shader("compositor_mask_to_sdf_compute_distance");
171
172 const Result &mask = this->get_input("Mask");
173 mask.bind_as_texture(shader, "mask_tx");
174
175 flooded_boundary.bind_as_texture(shader, "flooded_boundary_tx");
176
177 const Domain domain = mask.domain();
178 Result &distance_output = this->get_result("SDF");
179 distance_output.allocate_texture(domain);
180 distance_output.bind_as_image(shader, "distance_img");
181
183
184 mask.unbind_as_texture();
185 flooded_boundary.unbind_as_texture();
186 distance_output.unbind_as_image();
188 }
189
190 void compute_signed_distance_cpu(const Result &flooded_boundary)
191 {
192 const Result &mask = this->get_input("Mask");
193
194 const Domain domain = mask.domain();
195 Result &distance_output = this->get_result("SDF");
196 distance_output.allocate_texture(domain);
197
198 parallel_for(domain.data_size, [&](const int2 texel) {
199 const bool is_inside_mask = mask.load_pixel<bool>(texel);
200 const int2 closest_boundary_texel = flooded_boundary.load_pixel<int2>(texel);
201 const float distance_to_boundary = math::distance(float2(texel),
202 float2(closest_boundary_texel));
203 const float signed_distance = is_inside_mask ? -distance_to_boundary : distance_to_boundary;
204
205 distance_output.store_pixel(texel, signed_distance);
206 });
207 }
208};
209
211{
212 return new MaskToSDFOperation(context, node);
213}
214
215static void node_register()
216{
217 static blender::bke::bNodeType ntype;
218
219 cmp_node_type_base(&ntype, "CompositorNodeMaskToSDF"_ustr);
220 ntype.ui_name = "Mask To SDF";
221 ntype.ui_description = "Computes a signed distance field from the given mask";
223 ntype.declare = node_declare;
225
227}
228NOD_REGISTER_NODE(node_register)
229
230} // namespace blender::nodes::node_composite_mask_to_sdf_cc
#define NODE_CLASS_OP_FILTER
Definition BKE_node.hh:482
#define NOD_REGISTER_NODE(REGISTER_FUNC)
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
void share_data(const Result &source)
Definition result.cc:996
bool should_compute() const
Definition result.cc:1124
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 set_single_value(const T &value)
void bind_as_texture(gpu::Shader *shader, const char *texture_name) const
Definition result.cc:854
void set_precision(ResultPrecision precision)
Definition result.cc:1154
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 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
int2 initialize_jump_flooding_value(const int2 &texel, const bool is_seed)
void jump_flooding(Context &context, Result &input, Result &output)
void parallel_for(const int2 range, const Function &function)
static NodeOperation * get_compositor_operation(Context &context, const bNode &node)
static void node_declare(NodeDeclarationBuilder &b)
void GPU_shader_unbind()
void GPU_shader_bind(gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
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
NodeDeclareFunction declare
Definition BKE_node.hh:393