Blender V5.3
node_composite_depth_combine.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"
6#include "BLI_math_vector.hh"
8
10#include "COM_node_operation.hh"
11#include "COM_utilities.hh"
12
13#include "GPU_shader.hh"
14
16
18
20{
21 b.add_input<decl::Color>("A"_ustr)
22 .default_value({1.0f, 1.0f, 1.0f, 1.0f})
23 .structure_type(StructureType::Dynamic);
24 b.add_input<decl::Float>("Depth A"_ustr)
25 .default_value(1.0f)
26 .min(0.0f)
27 .max(10000.0f)
28 .structure_type(StructureType::Dynamic);
29 b.add_input<decl::Color>("B"_ustr)
30 .default_value({1.0f, 1.0f, 1.0f, 1.0f})
31 .structure_type(StructureType::Dynamic);
32 b.add_input<decl::Float>("Depth B"_ustr)
33 .default_value(1.0f)
34 .min(0.0f)
35 .max(10000.0f)
36 .structure_type(StructureType::Dynamic);
37 b.add_input<decl::Bool>("Use Alpha"_ustr)
38 .default_value(false)
40 "Use the alpha of the first input as mixing factor and return the more opaque alpha of "
41 "the two inputs");
42 b.add_input<decl::Bool>("Anti-Alias"_ustr)
43 .default_value(true)
45 "Anti-alias the generated mask before combining for smoother boundaries at the cost of "
46 "more expensive processing");
47
48 b.add_output<decl::Color>("Result"_ustr).structure_type(StructureType::Dynamic);
49 b.add_output<decl::Float>("Depth"_ustr).structure_type(StructureType::Dynamic);
50}
51
52using namespace blender::compositor;
53
55 public:
57
58 void execute() override
59 {
60 if (this->get_input("A").is_single_value() && this->get_input("B").is_single_value() &&
61 this->get_input("Depth A").is_single_value() &&
62 this->get_input("Depth B").is_single_value())
63 {
65 }
66 else if (use_anti_aliasing()) {
68 }
69 else {
71 }
72 }
73
75 {
76 const float4 first_color = float4(get_input("A").get_single_value<Color>());
77 const float4 second_color = float4(get_input("B").get_single_value<Color>());
78 const float first_z_value = get_input("Depth A").get_single_value<float>();
79 const float second_z_value = get_input("Depth B").get_single_value<float>();
80
81 /* Mix between the first and second images using a mask such that the image with the object
82 * closer to the camera is returned. The mask value is then 1, and thus returns the first image
83 * if its Depth value is less than that of the second image. Otherwise, its value is 0, and
84 * thus returns the second image. Furthermore, if the object in the first image is closer but
85 * has a non-opaque alpha, then the alpha is used as a mask, but only if Use Alpha is enabled.
86 */
87 const float z_combine_factor = float(first_z_value < second_z_value);
88 const float alpha_factor = use_alpha() ? first_color.w : 1.0f;
89 const float mix_factor = z_combine_factor * alpha_factor;
90
91 Result &combined = get_result("Result");
92 if (combined.should_compute()) {
93 float4 combined_color = math::interpolate(second_color, first_color, mix_factor);
94 /* Use the more opaque alpha from the two images. */
95 combined_color.w = use_alpha() ? math::max(second_color.w, first_color.w) : combined_color.w;
96
97 combined.allocate_single_value();
98 combined.set_single_value(Color(combined_color));
99 }
100
101 Result &combined_z = get_result("Depth");
102 if (combined_z.should_compute()) {
103 const float combined_z_value = math::interpolate(second_z_value, first_z_value, mix_factor);
104 combined_z.allocate_single_value();
105 combined_z.set_single_value(combined_z_value);
106 }
107 }
108
110 {
111 if (this->context().use_gpu()) {
112 this->execute_simple_gpu();
113 }
114 else {
115 this->execute_simple_cpu();
116 }
117 }
118
120 {
121 if (this->get_result("Result").should_compute()) {
123 }
124
125 if (this->get_result("Depth").should_compute()) {
127 }
128 }
129
131 {
132 gpu::Shader *shader = this->context().get_shader("compositor_z_combine_simple_image");
134
135 GPU_shader_uniform_1b(shader, "use_alpha", this->use_alpha());
136
137 const Result &first = this->get_input("A");
138 gpu::Texture *first_texture = first.bind_as_texture_or_single_value(shader, "first_tx");
139 const Result &first_z = this->get_input("Depth A");
140 gpu::Texture *first_z_texture = first_z.bind_as_texture_or_single_value(shader, "first_z_tx");
141 const Result &second = this->get_input("B");
142 gpu::Texture *second_texture = second.bind_as_texture_or_single_value(shader, "second_tx");
143 const Result &second_z = this->get_input("Depth B");
144 gpu::Texture *second_z_texture = second_z.bind_as_texture_or_single_value(shader,
145 "second_z_tx");
146
147 Result &combined = this->get_result("Result");
148 const Domain domain = this->compute_domain();
149 combined.allocate_texture(domain);
150 combined.bind_as_image(shader, "combined_img");
151
153
154 first.unbind_as_texture_or_single_value(first_texture);
155 first_z.unbind_as_texture_or_single_value(first_z_texture);
156 second.unbind_as_texture_or_single_value(second_texture);
157 second_z.unbind_as_texture_or_single_value(second_z_texture);
158 combined.unbind_as_image();
160 }
161
163 {
164 gpu::Shader *shader = this->context().get_shader("compositor_z_combine_simple_depth");
166
167 const Result &first_z = this->get_input("Depth A");
168 gpu::Texture *first_z_texture = first_z.bind_as_texture_or_single_value(shader, "first_z_tx");
169 const Result &second_z = this->get_input("Depth B");
170 gpu::Texture *second_z_texture = second_z.bind_as_texture_or_single_value(shader,
171 "second_z_tx");
172
173 Result &combined_z = this->get_result("Depth");
174 const Domain domain = this->compute_domain();
175 combined_z.allocate_texture(domain);
176 combined_z.bind_as_image(shader, "combined_z_img");
177
179
180 first_z.unbind_as_texture_or_single_value(first_z_texture);
181 second_z.unbind_as_texture_or_single_value(second_z_texture);
182 combined_z.unbind_as_image();
184 }
185
187 {
188 const bool use_alpha = this->use_alpha();
189
190 const Result &first = this->get_input("A");
191 const Result &first_z = this->get_input("Depth A");
192 const Result &second = this->get_input("B");
193 const Result &second_z = this->get_input("Depth B");
194
195 const Domain domain = this->compute_domain();
196 Result &combined = this->get_result("Result");
197 if (combined.should_compute()) {
198 combined.allocate_texture(domain);
199 parallel_for(domain.data_size, [&](const int2 texel) {
200 float4 first_color = float4(first.load_pixel<Color, true>(texel));
201 float4 second_color = float4(second.load_pixel<Color, true>(texel));
202 float first_z_value = first_z.load_pixel<float, true>(texel);
203 float second_z_value = second_z.load_pixel<float, true>(texel);
204
205 /* Choose the closer pixel as the foreground, that is, the pixel with the lower z value. If
206 * Use Alpha is disabled, return the foreground, otherwise, mix between the foreground and
207 * background using the alpha of the foreground. */
208 float4 foreground_color = first_z_value < second_z_value ? first_color : second_color;
209 float4 background_color = first_z_value < second_z_value ? second_color : first_color;
210 float mix_factor = use_alpha ? foreground_color.w : 1.0f;
211 float4 combined_color = math::interpolate(background_color, foreground_color, mix_factor);
212
213 /* Use the more opaque alpha from the two images. */
214 combined_color.w = use_alpha ? math::max(second_color.w, first_color.w) : combined_color.w;
215 combined.store_pixel(texel, Color(combined_color));
216 });
217 }
218
219 Result &combined_z_output = this->get_result("Depth");
220 if (combined_z_output.should_compute()) {
221 combined_z_output.allocate_texture(domain);
222 parallel_for(domain.data_size, [&](const int2 texel) {
223 float first_z_value = first_z.load_pixel<float, true>(texel);
224 float second_z_value = second_z.load_pixel<float, true>(texel);
225 float combined_z = math::min(first_z_value, second_z_value);
226 combined_z_output.store_pixel(texel, combined_z);
227 });
228 }
229 }
230
232 {
234
235 Result anti_aliased_mask = this->context().create_result(ResultType::Float);
236 smaa(this->context(), mask, anti_aliased_mask);
237 mask.release();
238
239 if (this->context().use_gpu()) {
240 this->execute_anti_aliased_gpu(anti_aliased_mask);
241 }
242 else {
243 this->execute_anti_aliased_cpu(anti_aliased_mask);
244 }
245
246 anti_aliased_mask.release();
247 }
248
250 {
251 if (this->get_result("Result").should_compute()) {
253 }
254
255 if (this->get_result("Depth").should_compute()) {
257 }
258 }
259
261 {
262 gpu::Shader *shader = this->context().get_shader("compositor_z_combine_from_mask_image");
264
265 GPU_shader_uniform_1b(shader, "use_alpha", this->use_alpha());
266
267 const Result &first = this->get_input("A");
268 gpu::Texture *first_texture = first.bind_as_texture_or_single_value(shader, "first_tx");
269 const Result &second = this->get_input("B");
270 gpu::Texture *second_texture = second.bind_as_texture_or_single_value(shader, "second_tx");
271 mask.bind_as_texture(shader, "mask_tx");
272
273 Result &combined = this->get_result("Result");
274 const Domain domain = this->compute_domain();
275 combined.allocate_texture(domain);
276 combined.bind_as_image(shader, "combined_img");
277
279
280 first.unbind_as_texture_or_single_value(first_texture);
281 second.unbind_as_texture_or_single_value(second_texture);
282 mask.unbind_as_texture();
283 combined.unbind_as_image();
285 }
286
288 {
289 gpu::Shader *shader = this->context().get_shader("compositor_z_combine_from_mask_depth");
291
292 const Result &first_z = this->get_input("Depth A");
293 gpu::Texture *first_z_texture = first_z.bind_as_texture_or_single_value(shader, "first_z_tx");
294 const Result &second_z = this->get_input("Depth B");
295 gpu::Texture *second_z_texture = second_z.bind_as_texture_or_single_value(shader,
296 "second_z_tx");
297
298 Result &combined_z = this->get_result("Depth");
299 const Domain domain = this->compute_domain();
300 combined_z.allocate_texture(domain);
301 combined_z.bind_as_image(shader, "combined_z_img");
302
304
305 first_z.unbind_as_texture_or_single_value(first_z_texture);
306 second_z.unbind_as_texture_or_single_value(second_z_texture);
307 combined_z.unbind_as_image();
309 }
310
312 {
313 const bool use_alpha = this->use_alpha();
314
315 const Result &first = this->get_input("A");
316 const Result &first_z = this->get_input("Depth A");
317 const Result &second = this->get_input("B");
318 const Result &second_z = this->get_input("Depth B");
319
320 const Domain domain = this->compute_domain();
321 Result &combined = this->get_result("Result");
322 if (combined.should_compute()) {
323 combined.allocate_texture(domain);
324 parallel_for(domain.data_size, [&](const int2 texel) {
325 float4 first_color = float4(first.load_pixel<Color, true>(texel));
326 float4 second_color = float4(second.load_pixel<Color, true>(texel));
327 float mask_value = mask.load_pixel<float>(texel);
328
329 /* Choose the closer pixel as the foreground, that is, the masked pixel with the lower z
330 * value. If Use Alpha is disabled, return the foreground, otherwise, mix between the
331 * foreground and background using the alpha of the foreground. */
332 float4 foreground_color = math::interpolate(second_color, first_color, mask_value);
333 float4 background_color = math::interpolate(first_color, second_color, mask_value);
334 float mix_factor = use_alpha ? foreground_color.w : 1.0f;
335 float4 combined_color = math::interpolate(background_color, foreground_color, mix_factor);
336
337 /* Use the more opaque alpha from the two images. */
338 combined_color.w = use_alpha ? math::max(second_color.w, first_color.w) : combined_color.w;
339 combined.store_pixel(texel, Color(combined_color));
340 });
341 }
342
343 Result &combined_z_output = this->get_result("Depth");
344 if (combined_z_output.should_compute()) {
345 combined_z_output.allocate_texture(domain);
346 parallel_for(domain.data_size, [&](const int2 texel) {
347 float first_z_value = first_z.load_pixel<float, true>(texel);
348 float second_z_value = second_z.load_pixel<float, true>(texel);
349 float combined_z = math::min(first_z_value, second_z_value);
350 combined_z_output.store_pixel(texel, combined_z);
351 });
352 }
353 }
354
356 {
357 if (this->context().use_gpu()) {
358 return this->compute_mask_gpu();
359 }
360
361 return this->compute_mask_cpu();
362 }
363
365 {
366 gpu::Shader *shader = context().get_shader("compositor_z_combine_compute_mask");
368
369 const Result &first_z = get_input("Depth A");
370 gpu::Texture *first_z_texture = first_z.bind_as_texture_or_single_value(shader, "first_z_tx");
371 const Result &second_z = get_input("Depth B");
372 gpu::Texture *second_z_texture = second_z.bind_as_texture_or_single_value(shader,
373 "second_z_tx");
374
375 const Domain domain = compute_domain();
377 mask.allocate_texture(domain);
378 mask.bind_as_image(shader, "mask_img");
379
381
382 first_z.unbind_as_texture_or_single_value(first_z_texture);
383 second_z.unbind_as_texture_or_single_value(second_z_texture);
384 mask.unbind_as_image();
386
387 return mask;
388 }
389
391 {
392 const Result &first_z = this->get_input("Depth A");
393 const Result &second_z = this->get_input("Depth B");
394
395 const Domain domain = this->compute_domain();
397 mask.allocate_texture(domain);
398
399 parallel_for(domain.data_size, [&](const int2 texel) {
400 float first_z_value = first_z.load_pixel<float, true>(texel);
401 float second_z_value = second_z.load_pixel<float, true>(texel);
402 float z_combine_factor = float(first_z_value < second_z_value);
403 mask.store_pixel(texel, z_combine_factor);
404 });
405
406 return mask;
407 }
408
410 {
411 return this->get_input("Use Alpha").get_single_value_default<bool>();
412 }
413
415 {
416 return this->get_input("Anti-Alias").get_single_value_default<bool>();
417 }
418};
419
421{
422 return new ZCombineOperation(context, node);
423}
424
425static void node_register()
426{
427 static bke::bNodeType ntype;
428
429 cmp_node_type_base(&ntype, "CompositorNodeZcombine"_ustr, CMP_NODE_ZCOMBINE);
430 ntype.ui_name = "Depth Combine";
431 ntype.ui_description = "Combine two images using depth maps";
432 ntype.enum_name_legacy = "ZCOMBINE";
434 ntype.declare = node_declare;
436
438}
440
441} // namespace blender::nodes::node_composite_depth_combin_cc
#define NODE_CLASS_OP_COLOR
Definition BKE_node.hh:480
#define CMP_NODE_ZCOMBINE
#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
virtual Domain compute_domain()
Definition operation.cc:62
void unbind_as_texture_or_single_value(gpu::Texture *single_value_texture) const
Definition result.cc:970
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 set_single_value(const T &value)
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
const T & get_single_value() const
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 smaa(Context &context, const Result &input, Result &output, const float threshold=0.1f, const float local_contrast_adaptation_factor=2.0f, const int corner_rounding=25)
Definition smaa.cc:1693
void parallel_for(const int2 range, const Function &function)
T interpolate(const T &a, const T &b, const FactorT &t)
T max(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)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
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