Blender V5.3
node_composite_double_edge_mask.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
7#include "COM_utilities.hh"
8
10
12
14{
15 b.add_input<decl::Float>("Outer Mask"_ustr)
16 .default_value(0.8f)
17 .min(0.0f)
18 .max(1.0f)
19 .structure_type(StructureType::Dynamic);
20 b.add_input<decl::Float>("Inner Mask"_ustr)
21 .default_value(0.8f)
22 .min(0.0f)
23 .max(1.0f)
24 .structure_type(StructureType::Dynamic);
25 b.add_input<decl::Bool>("Image Edges"_ustr)
26 .default_value(false)
28 "The edges of the image that intersects the outer mask will be considered edges of the "
29 "outer mask. Otherwise, the outer mask will be considered open-ended");
30 b.add_input<decl::Bool>("Only Inside Outer"_ustr)
31 .default_value(false)
33 "Only edges of the inner mask that lie inside the outer mask will be considered. "
34 "Otherwise, all edges of the inner mask will be considered");
35
36 b.add_output<decl::Float>("Mask"_ustr).structure_type(StructureType::Dynamic);
37}
38
39using namespace blender::compositor;
40
42 public:
44
45 void execute() override
46 {
47 Result &inner_mask = get_input("Inner Mask");
48 Result &outer_mask = get_input("Outer Mask");
49 if (inner_mask.is_single_value() || outer_mask.is_single_value()) {
51 return;
52 }
53
54 /* Compute an image that marks the boundary pixels of the masks as seed pixels in the format
55 * expected by the jump flooding algorithm. */
58 compute_boundary(inner_boundary, outer_boundary);
59
60 /* Compute a jump flooding table for each mask boundary to get a distance transform to each of
61 * the boundaries. */
62 Result flooded_inner_boundary = context().create_result(ResultType::Int2,
64 Result flooded_outer_boundary = context().create_result(ResultType::Int2,
66 jump_flooding(context(), inner_boundary, flooded_inner_boundary);
67 jump_flooding(context(), outer_boundary, flooded_outer_boundary);
68 inner_boundary.release();
69 outer_boundary.release();
70
71 /* Compute the gradient based on the jump flooding table. */
72 compute_gradient(flooded_inner_boundary, flooded_outer_boundary);
73 flooded_inner_boundary.release();
74 flooded_outer_boundary.release();
75 }
76
77 void compute_boundary(Result &inner_boundary, Result &outer_boundary)
78 {
79 if (this->context().use_gpu()) {
80 this->compute_boundary_gpu(inner_boundary, outer_boundary);
81 }
82 else {
83 this->compute_boundary_cpu(inner_boundary, outer_boundary);
84 }
85 }
86
87 void compute_boundary_gpu(Result &inner_boundary, Result &outer_boundary)
88 {
89 gpu::Shader *shader = context().get_shader("compositor_double_edge_mask_compute_boundary",
92
93 GPU_shader_uniform_1b(shader, "include_all_inner_edges", include_all_inner_edges());
94 GPU_shader_uniform_1b(shader, "include_edges_of_image", include_edges_of_image());
95
96 const Result &inner_mask = get_input("Inner Mask");
97 inner_mask.bind_as_texture(shader, "inner_mask_tx");
98
99 const Result &outer_mask = get_input("Outer Mask");
100 outer_mask.bind_as_texture(shader, "outer_mask_tx");
101
102 const Domain domain = compute_domain();
103
104 inner_boundary.allocate_texture(domain);
105 inner_boundary.bind_as_image(shader, "inner_boundary_img");
106
107 outer_boundary.allocate_texture(domain);
108 outer_boundary.bind_as_image(shader, "outer_boundary_img");
109
111
112 inner_mask.unbind_as_texture();
113 outer_mask.unbind_as_texture();
114 inner_boundary.unbind_as_image();
115 outer_boundary.unbind_as_image();
117 }
118
119 void compute_boundary_cpu(Result &inner_boundary, Result &outer_boundary)
120 {
121 const bool include_all_inner_edges = this->include_all_inner_edges();
122 const bool include_edges_of_image = this->include_edges_of_image();
123
124 const Result &inner_mask = get_input("Inner Mask");
125 const Result &outer_mask = get_input("Outer Mask");
126
127 const Domain domain = compute_domain();
128 inner_boundary.allocate_texture(domain);
129 outer_boundary.allocate_texture(domain);
130
131 /* The Double Edge Mask operation uses a jump flood algorithm to compute a distance transform
132 * to the boundary of the inner and outer masks. The algorithm expects an input image whose
133 * values are those returned by the initialize_jump_flooding_value function, given the texel
134 * location and a boolean specifying if the pixel is a boundary one.
135 *
136 * Technically, we needn't restrict the output to just the boundary pixels, since the algorithm
137 * can still operate if the interior of the masks was also included. However, the algorithm
138 * operates more accurately when the number of pixels to be flooded is minimum. */
139 parallel_for(domain.data_size, [&](const int2 texel) {
140 /* Identify if any of the 8 neighbors around the center pixel are not masked. */
141 bool has_inner_non_masked_neighbors = false;
142 bool has_outer_non_masked_neighbors = false;
143 for (int j = -1; j <= 1; j++) {
144 for (int i = -1; i <= 1; i++) {
145 int2 offset = int2(i, j);
146
147 /* Exempt the center pixel. */
148 if (offset == int2(0)) {
149 continue;
150 }
151
152 if (inner_mask.load_pixel_extended<float>(texel + offset) == 0.0f) {
153 has_inner_non_masked_neighbors = true;
154 }
155
156 /* If the user specified include_edges_of_image to be true, then we assume the outer mask
157 * is bounded by the image boundary, otherwise, we assume the outer mask is open-ended.
158 * This is practically implemented by falling back to 0.0f or 1.0f for out of bound
159 * pixels. */
160 float boundary_fallback = include_edges_of_image ? 0.0f : 1.0f;
161 if (outer_mask.load_pixel_fallback(texel + offset, boundary_fallback) == 0.0f) {
162 has_outer_non_masked_neighbors = true;
163 }
164
165 /* Both are true, no need to continue. */
166 if (has_inner_non_masked_neighbors && has_outer_non_masked_neighbors) {
167 break;
168 }
169 }
170 }
171
172 bool is_inner_masked = inner_mask.load_pixel<float>(texel) > 0.0f;
173 bool is_outer_masked = outer_mask.load_pixel<float>(texel) > 0.0f;
174
175 /* The pixels at the boundary are those that are masked and have non masked neighbors. The
176 * inner boundary has a specialization, if include_all_inner_edges is false, only inner
177 * boundaries that lie inside the outer mask will be considered a boundary. The outer
178 * boundary is only considered if it is not inside the inner mask. */
179 bool is_inner_boundary = is_inner_masked && has_inner_non_masked_neighbors &&
180 (is_outer_masked || include_all_inner_edges);
181 bool is_outer_boundary = is_outer_masked && !is_inner_masked &&
182 has_outer_non_masked_neighbors;
183
184 /* Encode the boundary information in the format expected by the jump flooding algorithm. */
185 int2 inner_jump_flooding_value = initialize_jump_flooding_value(texel, is_inner_boundary);
186 int2 outer_jump_flooding_value = initialize_jump_flooding_value(texel, is_outer_boundary);
187
188 inner_boundary.store_pixel(texel, inner_jump_flooding_value);
189 outer_boundary.store_pixel(texel, outer_jump_flooding_value);
190 });
191 }
192
193 void compute_gradient(const Result &flooded_inner_boundary, const Result &flooded_outer_boundary)
194 {
195 if (this->context().use_gpu()) {
196 this->compute_gradient_gpu(flooded_inner_boundary, flooded_outer_boundary);
197 }
198 else {
199 this->compute_gradient_cpu(flooded_inner_boundary, flooded_outer_boundary);
200 }
201 }
202
203 void compute_gradient_gpu(const Result &flooded_inner_boundary,
204 const Result &flooded_outer_boundary)
205 {
206 gpu::Shader *shader = context().get_shader("compositor_double_edge_mask_compute_gradient");
208
209 const Result &inner_mask = get_input("Inner Mask");
210 inner_mask.bind_as_texture(shader, "inner_mask_tx");
211
212 const Result &outer_mask = get_input("Outer Mask");
213 outer_mask.bind_as_texture(shader, "outer_mask_tx");
214
215 flooded_inner_boundary.bind_as_texture(shader, "flooded_inner_boundary_tx");
216 flooded_outer_boundary.bind_as_texture(shader, "flooded_outer_boundary_tx");
217
218 const Domain domain = compute_domain();
219 Result &output = get_result("Mask");
220 output.allocate_texture(domain);
221 output.bind_as_image(shader, "output_img");
222
224
225 inner_mask.unbind_as_texture();
226 outer_mask.unbind_as_texture();
227 output.unbind_as_image();
229 }
230
231 void compute_gradient_cpu(const Result &flooded_inner_boundary,
232 const Result &flooded_outer_boundary)
233 {
234 const Result &inner_mask_input = get_input("Inner Mask");
235 const Result &outer_mask_input = get_input("Outer Mask");
236
237 const Domain domain = compute_domain();
238 Result &output = get_result("Mask");
239 output.allocate_texture(domain);
240
241 /* Computes a linear gradient from the outer mask boundary to the inner mask boundary, starting
242 * from 0 and ending at 1. This is computed using the equation:
243 *
244 * Gradient = O / (O + I)
245 *
246 * Where O is the distance to the outer boundary and I is the distance to the inner boundary.
247 * This can be viewed as computing the ratio between the distance to the outer boundary to the
248 * distance between the outer and inner boundaries as can be seen in the following illustration
249 * where the $ sign designates a pixel between both boundaries.
250 *
251 * | O I |
252 * Outer Boundary |---------$---------| Inner Boundary
253 * | |
254 */
255 parallel_for(domain.data_size, [&](const int2 texel) {
256 /* Pixels inside the inner mask are always 1.0. */
257 float inner_mask = inner_mask_input.load_pixel<float>(texel);
258 if (inner_mask != 0.0f) {
259 output.store_pixel(texel, 1.0f);
260 return;
261 }
262
263 /* Pixels outside the outer mask are always 0.0. */
264 float outer_mask = outer_mask_input.load_pixel<float>(texel);
265 if (outer_mask == 0.0f) {
266 output.store_pixel(texel, 0.0f);
267 return;
268 }
269
270 /* Compute the distances to the inner and outer boundaries from the jump flooding tables. */
271 int2 inner_boundary_texel = flooded_inner_boundary.load_pixel<int2>(texel);
272 int2 outer_boundary_texel = flooded_outer_boundary.load_pixel<int2>(texel);
273 float distance_to_inner = math::distance(float2(texel), float2(inner_boundary_texel));
274 float distance_to_outer = math::distance(float2(texel), float2(outer_boundary_texel));
275
276 float gradient = distance_to_outer / (distance_to_outer + distance_to_inner);
277
278 output.store_pixel(texel, gradient);
279 });
280 }
281
283 {
284 return !this->get_input("Only Inside Outer").get_single_value_default<bool>();
285 }
286
288 {
289 return this->get_input("Image Edges").get_single_value_default<bool>();
290 }
291};
292
294{
295 return new DoubleEdgeMaskOperation(context, node);
296}
297
298static void node_register()
299{
300 static bke::bNodeType ntype;
301
302 cmp_node_type_base(&ntype, "CompositorNodeDoubleEdgeMask"_ustr, CMP_NODE_DOUBLEEDGEMASK);
303 ntype.ui_name = "Double Edge Mask";
304 ntype.ui_description = "Create a gradient between two masks";
305 ntype.enum_name_legacy = "DOUBLEEDGEMASK";
306 ntype.nclass = NODE_CLASS_MATTE;
307 ntype.declare = node_declare;
310
312}
313NOD_REGISTER_NODE(node_register)
314
315} // namespace blender::nodes::node_composite_double_edge_mask_cc
#define NODE_CLASS_MATTE
Definition BKE_node.hh:485
#define CMP_NODE_DOUBLEEDGEMASK
#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
virtual Domain compute_domain()
Definition operation.cc:62
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(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
void compute_gradient_gpu(const Result &flooded_inner_boundary, const Result &flooded_outer_boundary)
void compute_gradient_cpu(const Result &flooded_inner_boundary, const Result &flooded_outer_boundary)
void compute_gradient(const Result &flooded_inner_boundary, const Result &flooded_outer_boundary)
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)
T distance(const T &a, const T &b)
static NodeOperation * get_compositor_operation(Context &context, const bNode &node)
void GPU_shader_uniform_1b(gpu::Shader *sh, const char *name, bool value)
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)
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