Blender V5.3
node_composite_cryptomatte.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2018 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include <cstring>
6#include <string>
7
8#include <fmt/format.h>
9
11
12#include "BLI_assert.hh"
13#include "BLI_listbase.hh"
14#include "BLI_math_vector_c.hh"
16#include "BLI_string_ref.hh"
17#include "BLI_string_utf8.hh"
18#include "BLI_utildefines.hh"
19#include "BLI_vector.hh"
20
21#include "IMB_imbuf_types.hh"
22
23#include "GPU_shader.hh"
24#include "GPU_texture.hh"
25
26#include "DNA_image_types.h"
27#include "DNA_scene_types.h"
28#include "DNA_space_types.h"
29
30#include "BKE_compositor.hh"
31#include "BKE_context.hh"
32#include "BKE_cryptomatte.hh"
33#include "BKE_image.hh"
34#include "BKE_lib_id.hh"
35#include "BKE_main.hh"
36#include "BKE_node.hh"
37#include "BKE_node_runtime.hh"
38
39#include "UI_resources.hh"
40
41#include "MEM_guardedalloc.h"
42
43#include "RE_pipeline.h"
44
46
47#include "COM_node_operation.hh"
48#include "COM_result.hh"
49#include "COM_utilities.hh"
50
51#include <optional>
52
53namespace blender {
54
56 const bNode &node, const bool build_meta_data)
57{
59
60 Scene *scene = id_cast<Scene *>(node.id);
61 if (!scene) {
62 return session;
63 }
64 BLI_assert(GS(scene->id.name) == ID_SCE);
65
67 BKE_cryptomatte_init_from_scene(scene, build_meta_data));
68 return session;
69}
70
72{
74 Image *image = id_cast<Image *>(node.id);
75 if (!image) {
76 return session;
77 }
78 BLI_assert(GS(image->id.name) == ID_IM);
79
80 NodeCryptomatte *node_cryptomatte = static_cast<NodeCryptomatte *>(node.storage);
81 ImageUser image_user = node_cryptomatte->iuser;
82 BKE_image_user_frame_calc(image, &image_user, image_user.framenr);
83
84 /* Fallback to the first frame in the image sequence if the current frame is out of range. */
85 if (!(image_user.flag & IMA_USER_FRAME_IN_RANGE)) {
87 }
88
89 ImBuf *ibuf = BKE_image_acquire_ibuf(image, &image_user, nullptr);
90 RenderResult *render_result = image->rr;
91 if (render_result) {
94 }
95 BKE_image_release_ibuf(image, ibuf, nullptr);
96 return session;
97}
98
100 const bNode &node, const bool build_meta_data)
101{
103 if (node.type_legacy != CMP_NODE_CRYPTOMATTE) {
104 return session;
105 }
106
107 switch (node.custom1) {
109 return cryptomatte_init_from_node_render(node, build_meta_data);
110 }
111
114 }
115 }
116 return session;
117}
118
119static CryptomatteEntry *cryptomatte_find(const NodeCryptomatte &n, float encoded_hash)
120{
121 for (CryptomatteEntry &entry : n.entries) {
122 if (entry.encoded_hash == encoded_hash) {
123 return &entry;
124 }
125 }
126 return nullptr;
127}
128
129static void cryptomatte_add(bNode &node, NodeCryptomatte &node_cryptomatte, float encoded_hash)
130{
131 /* Check if entry already exist. */
132 if (cryptomatte_find(node_cryptomatte, encoded_hash)) {
133 return;
134 }
135
136 CryptomatteEntry *entry = MEM_new<CryptomatteEntry>(__func__);
137 entry->encoded_hash = encoded_hash;
139 if (session) {
140 BKE_cryptomatte_find_name(session.get(), encoded_hash, entry->name, sizeof(entry->name));
141 }
142
143 BLI_addtail(&node_cryptomatte.entries, entry);
144}
145
146static void cryptomatte_remove(NodeCryptomatte &n, float encoded_hash)
147{
148 CryptomatteEntry *entry = cryptomatte_find(n, encoded_hash);
149 if (!entry) {
150 return;
151 }
152 BLI_remlink(&n.entries, entry);
153 MEM_delete(entry);
154}
155
157{
159 NodeCryptomatte *n = static_cast<NodeCryptomatte *>(node->storage);
160 if (n->runtime.add[0] != 0.0f) {
161 cryptomatte_add(*node, *n, n->runtime.add[0]);
162 zero_v3(n->runtime.add);
163 }
164}
165
167{
169 NodeCryptomatte *n = static_cast<NodeCryptomatte *>(node->storage);
170 if (n->runtime.remove[0] != 0.0f) {
173 }
174}
175
177{
178 BLI_assert(node->type_legacy == CMP_NODE_CRYPTOMATTE);
179 NodeCryptomatte *n = static_cast<NodeCryptomatte *>(node->storage);
180 n->runtime.layers.free_no_destruct();
181
183
184 if (session) {
186 CryptomatteLayer *layer = MEM_new<CryptomatteLayer>(__func__);
187 layer_name.copy_utf8_truncated(layer->name);
189 }
190 }
191}
192
193void ntreeCompositCryptomatteLayerPrefix(const bNode *node, char *r_prefix, size_t prefix_maxncpy)
194{
195 BLI_assert(node->type_legacy == CMP_NODE_CRYPTOMATTE);
196 NodeCryptomatte *node_cryptomatte = static_cast<NodeCryptomatte *>(node->storage);
198 std::string first_layer_name;
199
200 if (session) {
202 if (first_layer_name.empty()) {
203 first_layer_name = layer_name;
204 }
205
206 if (layer_name == node_cryptomatte->layer_name) {
207 BLI_strncpy_utf8(r_prefix, node_cryptomatte->layer_name, prefix_maxncpy);
208 return;
209 }
210 }
211 }
212
213 const char *cstr = first_layer_name.c_str();
214 BLI_strncpy_utf8(r_prefix, cstr, prefix_maxncpy);
215}
216
222
224
226{
228 NodeCryptomatte *nc = static_cast<NodeCryptomatte *>(node->storage);
229
230 if (nc) {
231 MEM_SAFE_DELETE(nc->matte_id);
232 nc->runtime.layers.free_no_destruct();
233 nc->entries.free_no_destruct();
234 MEM_delete(nc);
235 }
236}
237
238static void node_copy_cryptomatte(bNodeTree * /*dst_ntree*/,
239 bNode *dest_node,
240 const bNode *src_node)
241{
242 NodeCryptomatte *src_nc = static_cast<NodeCryptomatte *>(src_node->storage);
243 NodeCryptomatte *dest_nc = static_cast<NodeCryptomatte *>(MEM_dupalloc(src_nc));
244
245 BLI_duplicatelist(&dest_nc->entries, &src_nc->entries);
246 dest_nc->runtime.layers.clear_no_delete();
247 dest_nc->matte_id = static_cast<char *>(MEM_dupalloc(src_nc->matte_id));
248 dest_node->storage = dest_nc;
249}
250
252
253using namespace blender::compositor;
254
256 public:
258
259 /* Should return the input image result. */
260 virtual Result &get_input_image() = 0;
261
262 /* Should return all the Cryptomatte layers in order. The caller should release the returned
263 * layers. */
265
266 void execute() override
267 {
268 Vector<Result> layers = get_layers();
269 if (layers.is_empty()) {
271 return;
272 }
273
274 BLI_SCOPED_DEFER([&]() {
275 for (Result &layer : layers) {
276 layer.release();
277 }
278 });
279
280 Result &output_pick = get_result("Pick");
281 if (output_pick.should_compute()) {
282 compute_pick(layers);
283 }
284
285 Result &matte_output = get_result("Matte");
286 Result &image_output = get_result("Image");
287 if (!matte_output.should_compute() && !image_output.should_compute()) {
288 return;
289 }
290
291 Result matte = compute_matte(layers);
292
293 if (image_output.should_compute()) {
294 compute_image(matte);
295 }
296
297 if (matte_output.should_compute()) {
298 matte_output.share_data(matte);
299 }
300
301 matte.release();
302 }
303
304 /* Computes the pick result, which is a special human-viewable image that the user can pick
305 * entities from using the Cryptomatte picker operator. See the shader for more information. */
306 void compute_pick(const Vector<Result> &layers)
307 {
308 Result &output_pick = get_result("Pick");
309 /* Promote to full precision since it stores the identifiers of the first Cryptomatte rank,
310 * which is a 32-bit float. See the shader for more information. */
312 /* Inform viewers that the pick result should not be color managed. */
313 output_pick.meta_data.is_non_color_data = true;
314
315 if (this->context().use_gpu()) {
316 this->compute_pick_gpu(layers);
317 }
318 else {
319 this->compute_pick_cpu(layers);
320 }
321 }
322
324 {
325 /* See this->compute_pick for why full precision is necessary. */
326 gpu::Shader *shader = context().get_shader("compositor_cryptomatte_pick",
329
330 const Result &first_layer = layers[0];
331 first_layer.bind_as_texture(shader, "first_layer_tx");
332
333 const Domain domain = compute_domain();
334 Result &output_pick = get_result("Pick");
335 output_pick.allocate_texture(domain);
336 output_pick.bind_as_image(shader, "output_img");
337
339
341 first_layer.unbind_as_texture();
342 output_pick.unbind_as_image();
343 }
344
346 {
347 const Result &first_layer = layers[0];
348
349 const Domain domain = this->compute_domain();
350 Result &output = this->get_result("Pick");
351 output.allocate_texture(domain);
352
353 /* Blender provides a Cryptomatte picker operator (UI_OT_eyedropper_color) that can pick a
354 * Cryptomatte entity from an image. That image is a specially encoded image that the picker
355 * operator can understand. In particular, its red channel is the identifier of the entity in
356 * the first rank, while the green and blue channels are arbitrary [0, 1] compressed versions
357 * of the identifier to make the image more humane-viewable, but they are actually ignored by
358 * the picker operator, as can be seen in functions like eyedropper_color_sample_text_update,
359 * where only the red channel is considered.
360 *
361 * This shader just computes this special image given the first Cryptomatte layer. The output
362 * needs to be in full precision since the identifier is a 32-bit float.
363 *
364 * This is the same concept as the "keyable" image described in section "Matte Extraction:
365 * Implementation Details" in the original Cryptomatte publication:
366 *
367 * Friedman, Jonah, and Andrew C. Jones. "Fully automatic id mattes with support for motion
368 * blur and transparency." ACM SIGGRAPH 2015 Posters. 2015. 1-1.
369 *
370 * Except we put the identifier in the red channel by convention instead of the suggested blue
371 * channel. */
372 parallel_for(domain.data_size, [&](const int2 texel) {
373 /* Each layer stores two ranks, each rank contains a pair, the identifier and the coverage of
374 * the entity identified by the identifier. */
375 float2 first_rank = float4(first_layer.load_pixel<Color>(texel)).xy();
376 float id_of_first_rank = first_rank.x;
377
378 /* There is no logic to this, we just compute arbitrary compressed versions of the identifier
379 * in the [0, 1] range to make the image more human-viewable. */
380 uint32_t hash_value;
381 std::memcpy(&hash_value, &id_of_first_rank, sizeof(uint32_t));
382 float green = float(hash_value << 8) / float(0xFFFFFFFFu);
383 float blue = float(hash_value << 16) / float(0xFFFFFFFFu);
384
385 output.store_pixel(texel, Color(id_of_first_rank, green, blue, 1.0f));
386 });
387 }
388
389 /* Computes and returns the matte by accumulating the coverage of all entities whose identifiers
390 * are selected by the user, across all layers. See the shader for more information. */
392 {
393 if (this->context().use_gpu()) {
394 return this->compute_matte_gpu(layers);
395 }
396
397 return this->compute_matte_cpu(layers);
398 }
399
401 {
402 const Domain domain = compute_domain();
404 output_matte.allocate_texture(domain);
405
406 /* Clear the matte to zero to ready it to accumulate the coverage. */
407 const float4 zero_color = float4(0.0f);
408 GPU_texture_clear(output_matte, GPU_DATA_FLOAT, zero_color);
409
410 const Vector<float> identifiers = this->get_identifiers();
411 /* The user haven't selected any entities, return the currently zero matte. */
412 if (identifiers.is_empty()) {
413 return output_matte;
414 }
415
416 gpu::Shader *shader = context().get_shader("compositor_cryptomatte_matte");
418
419 const Vector<Span<float>> identifiers_slices = this->get_identifiers_slices(identifiers);
420 for (const Span<float> &identifier_slice : identifiers_slices) {
421 GPU_shader_uniform_1i(shader, "identifiers_count", identifier_slice.size());
423 shader, "identifiers", identifier_slice.size(), identifier_slice.data());
424
425 for (const Result &layer : layers) {
426 layer.bind_as_texture(shader, "layer_tx");
427
428 /* Bind the matte with read access, since we will be accumulating in it. */
429 output_matte.bind_as_image(shader, "matte_img", true);
430
432
433 layer.unbind_as_texture();
434 output_matte.unbind_as_image();
435 }
436 }
437
439
440 return output_matte;
441 }
442
443 /* Divides the given identifiers vector into a number of slices with a maximum of 32 element per
444 * slice. This is needed because the GPU shader can only be passed 32 identifiers at a time. */
446 {
447 Vector<Span<float>> slices;
448 constexpr int slice_size = 32;
449 const int slices_count = ((identifiers.size() - 1) / slice_size) + 1;
450 for (int i = 0; i < slices_count; i++) {
451 slices.append(identifiers.as_span().slice_safe(i * slice_size, slice_size));
452 }
453 return slices;
454 }
455
457 {
458 const Domain domain = compute_domain();
460 matte.allocate_texture(domain);
461
462 /* Clear the matte to zero to ready it to accumulate the coverage. */
463 parallel_for(domain.data_size, [&](const int2 texel) { matte.store_pixel(texel, 0.0f); });
464
465 Vector<float> identifiers = get_identifiers();
466 /* The user haven't selected any entities, return the currently zero matte. */
467 if (identifiers.is_empty()) {
468 return matte;
469 }
470
471 for (const Result &layer_result : layers) {
472 /* Loops over all identifiers selected by the user, and accumulate the coverage of ranks
473 * whose identifiers match that of the user selected identifiers.
474 *
475 * This is described in section "Matte Extraction: Implementation Details" in the original
476 * Cryptomatte publication:
477 *
478 * Friedman, Jonah, and Andrew C. Jones. "Fully automatic id mattes with support for motion
479 * blur and transparency." ACM SIGGRAPH 2015 Posters. 2015. 1-1.
480 */
481 parallel_for(domain.data_size, [&](const int2 texel) {
482 float4 layer = float4(layer_result.load_pixel<Color>(texel));
483
484 /* Each Cryptomatte layer stores two ranks. */
485 float2 first_rank = layer.xy();
486 float2 second_rank = layer.zw();
487
488 /* Each Cryptomatte rank stores a pair of an identifier and the coverage of the entity
489 * identified by that identifier. */
490 float identifier_of_first_rank = first_rank.x;
491 float coverage_of_first_rank = first_rank.y;
492 float identifier_of_second_rank = second_rank.x;
493 float coverage_of_second_rank = second_rank.y;
494
495 /* Loop over all identifiers selected by the user, if the identifier of either of the ranks
496 * match it, accumulate its coverage. */
497 float total_coverage = 0.0f;
498 for (const float &identifier : identifiers) {
499 if (identifier_of_first_rank == identifier) {
500 total_coverage += coverage_of_first_rank;
501 }
502 if (identifier_of_second_rank == identifier) {
503 total_coverage += coverage_of_second_rank;
504 }
505 }
506
507 /* Add the total coverage to the coverage accumulated by previous layers. */
508 matte.store_pixel(texel, matte.load_pixel<float>(texel) + total_coverage);
509 });
510 }
511
512 return matte;
513 }
514
515 /* Computes the output image result by pre-multiplying the matte to the image. */
516 void compute_image(const Result &matte)
517 {
518 if (this->context().use_gpu()) {
519 this->compute_image_gpu(matte);
520 }
521 else {
522 this->compute_image_cpu(matte);
523 }
524 }
525
526 void compute_image_gpu(const Result &matte)
527 {
528 gpu::Shader *shader = context().get_shader("compositor_cryptomatte_image");
530
531 Result &input_image = get_input_image();
532 gpu::Texture *input_texture = input_image.bind_as_texture_or_single_value(shader, "input_tx");
533
534 matte.bind_as_texture(shader, "matte_tx");
535
536 const Domain domain = compute_domain();
537 Result &image_output = get_result("Image");
538 image_output.allocate_texture(domain);
539 image_output.bind_as_image(shader, "output_img");
540
542
544 input_image.unbind_as_texture_or_single_value(input_texture);
545 matte.unbind_as_texture();
546 image_output.unbind_as_image();
547 }
548
549 void compute_image_cpu(const Result &matte)
550 {
551 Result &input = get_input_image();
552
553 const Domain domain = compute_domain();
554 Result &output = get_result("Image");
555 output.allocate_texture(domain);
556
557 parallel_for(domain.data_size, [&](const int2 texel) {
558 float4 input_color = float4(input.load_pixel<Color, true>(texel));
559 float input_matte = matte.load_pixel<float>(texel);
560
561 /* Premultiply the alpha to the image. */
562 output.store_pixel(texel, Color(input_color * input_matte));
563 });
564 }
565
566 /* Get the identifiers of the entities selected by the user to generate a matte from. The
567 * identifiers are hashes of the names of the entities encoded in floats. See the "ID Generation"
568 * section of the Cryptomatte specification for more information. */
570 {
571 Vector<float> identifiers;
572 for (CryptomatteEntry &cryptomatte_entry : node_storage(node()).entries) {
573 identifiers.append(cryptomatte_entry.encoded_hash);
574 }
575 return identifiers;
576 }
577};
578
579} // namespace nodes::node_composite_base_cryptomatte_cc
580
582
584
586{
587 b.add_input<decl::Color>("Image"_ustr)
588 .default_value({0.0f, 0.0f, 0.0f, 1.0f})
589 .structure_type(StructureType::Dynamic);
590
591 b.add_output<decl::Color>("Image"_ustr).structure_type(StructureType::Dynamic);
592 b.add_output<decl::Float>("Matte"_ustr).structure_type(StructureType::Dynamic);
593 b.add_output<decl::Color>("Pick"_ustr).structure_type(StructureType::Dynamic);
594}
595
596static void node_init(bNodeTree * /*node_tree*/, bNode *node)
597{
598 NodeCryptomatte *storage = MEM_new<NodeCryptomatte>(__func__);
599 node->storage = storage;
600}
601
602static void node_init_api(const bContext *C, PointerRNA *node_ptr)
603{
604 Scene *scene = CTX_data_scene(C);
605 bNode *node = node_ptr->data_as<bNode>();
606 node->id = &scene->id;
607 id_us_plus(node->id);
608}
609
614
615static void node_extra_info(NodeExtraInfoParams &parameters)
616{
618 return;
619 }
620
621 SpaceNode *space_node = CTX_wm_space_node(&parameters.C);
622 if (space_node->node_tree_sub_type != SNODE_COMPOSITOR_SCENE) {
624 row.text = RPT_("Node Unsupported");
625 row.tooltip = TIP_(
626 "The Cryptomatte node in render mode is only supported for scene compositing");
627 row.icon = ICON_STATUS_ERROR;
628 parameters.rows.append(std::move(row));
629 return;
630 }
631
632 /* EEVEE supports passes. */
633 const Scene *scene = CTX_data_scene(&parameters.C);
635 return;
636 }
637
639 return;
640 }
641
643 row.text = RPT_("Passes Not Supported");
644 row.tooltip = TIP_("Render passes in the Viewport compositor are only supported in EEVEE");
645 row.icon = ICON_STATUS_ERROR;
646 parameters.rows.append(std::move(row));
647}
648
649using namespace blender::compositor;
651
653 public:
654 using BaseCryptoMatteOperation::BaseCryptoMatteOperation;
655
657 {
658 return get_input("Image");
659 }
660
661 /* Returns all the relevant Cryptomatte layers from the selected source. The caller should
662 * release the returned layers. */
664 {
665 switch (get_source()) {
667 return get_layers_from_render();
669 return get_layers_from_image();
670 }
671
673 return Vector<Result>();
674 }
675
676 /* Returns all the relevant Cryptomatte layers from the selected layer. The caller should release
677 * the returned layers. */
679 {
680 Vector<Result> layers;
681
682 Scene *scene = get_scene();
683 if (!scene) {
684 return layers;
685 }
686
687 const std::string type_name = get_type_name();
688
689 for (const auto [view_layer_index, view_layer] : scene->view_layers.enumerate()) {
690 /* Find out which type of Cryptomatte layer the node uses, if non matched, then this is not
691 * the view layer used by the node and we check other view layers. */
692 const char *cryptomatte_type = nullptr;
693 const std::string layer_prefix = std::string(view_layer.name) + ".";
694 if (type_name == layer_prefix + RE_PASSNAME_CRYPTOMATTE_OBJECT) {
695 cryptomatte_type = RE_PASSNAME_CRYPTOMATTE_OBJECT;
696 }
697 else if (type_name == layer_prefix + RE_PASSNAME_CRYPTOMATTE_ASSET) {
698 cryptomatte_type = RE_PASSNAME_CRYPTOMATTE_ASSET;
699 }
700 else if (type_name == layer_prefix + RE_PASSNAME_CRYPTOMATTE_MATERIAL) {
701 cryptomatte_type = RE_PASSNAME_CRYPTOMATTE_MATERIAL;
702 }
703
704 /* Not the view layer used by the node. */
705 if (!cryptomatte_type) {
706 continue;
707 }
708
709 /* Each layer stores two ranks/levels, so do ceiling division by two. */
710 const int cryptomatte_layers_count = int(math::ceil(view_layer.cryptomatte_levels / 2.0f));
711 for (int i = 0; i < cryptomatte_layers_count; i++) {
712 const std::string pass_name = fmt::format("{}{:02}", cryptomatte_type, i);
713 Result pass_result = this->context().get_pass(scene, view_layer_index, pass_name.c_str());
714
715 /* If this Cryptomatte layer wasn't found, then all later Cryptomatte layers can't be used
716 * even if they were found. */
717 if (pass_result.is_single_value()) {
718 pass_result.release();
719 return layers;
720 }
721 layers.append(std::move(pass_result));
722 }
723
724 /* The target view later was processed already, no need to check other view layers. */
725 return layers;
726 }
727
728 return layers;
729 }
730
731 /* Returns all the relevant Cryptomatte layers from the selected EXR image. The caller should
732 * release the returned layers. */
734 {
735 Vector<Result> layers;
736
737 Image *image = this->get_image();
738 if (!image || image->type != IMA_TYPE_MULTILAYER) {
739 return layers;
740 }
741
742 /* The render result structure of the image is populated as a side effect of the acquisition of
743 * an image buffer, so acquire an image buffer and immediately release it since it is not
744 * actually needed. */
745 ImageUser image_user_for_layer = this->get_image_user();
746 ImBuf *image_buffer = BKE_image_acquire_ibuf(image, &image_user_for_layer, nullptr);
747 BKE_image_release_ibuf(image, image_buffer, nullptr);
748 if (!image_buffer || !image->rr) {
749 return layers;
750 }
751
752 RenderResult *render_result = BKE_image_acquire_renderresult(nullptr, image);
753
754 /* Gather all pass names first before retrieving the images because render layers might get
755 * freed when retrieving the images. */
756 Vector<std::string> pass_names;
757
758 int layer_index = 0;
759 const std::string type_name = this->get_type_name();
760 for (RenderLayer &render_layer : render_result->layers) {
761 /* If the Cryptomatte type name doesn't start with the layer name, then it is not a
762 * Cryptomatte layer. Unless it is an unnamed layer, in which case, we need to check its
763 * passes. */
764 const bool is_unnamed_layer = render_layer.name[0] == '\0';
765 if (!is_unnamed_layer && !StringRefNull(type_name).startswith(render_layer.name)) {
766 layer_index++;
767 continue;
768 }
769
770 for (RenderPass &render_pass : render_layer.passes) {
771 /* If the combined pass name doesn't start with the Cryptomatte type name, then it is not a
772 * Cryptomatte layer. Furthermore, if it is equal to the Cryptomatte type name with no
773 * suffix, then it can be ignored, because it is a deprecated Cryptomatte preview layer
774 * according to the "EXR File: Layer Naming" section of the Cryptomatte specification. */
775 const std::string combined_name = this->get_combined_layer_pass_name(&render_layer,
776 &render_pass);
777 if (combined_name == type_name || !StringRef(combined_name).startswith(type_name)) {
778 continue;
779 }
780
781 pass_names.append(render_pass.name);
782 }
783
784 /* If we already found Cryptomatte layers, no need to check other render layers. */
785 if (!pass_names.is_empty()) {
786 break;
787 }
788 layer_index++;
789 }
790
791 BKE_image_release_renderresult(nullptr, image, render_result);
792
793 image_user_for_layer.layer = layer_index;
794 for (const std::string &pass_name : pass_names) {
795 const Result &pass_result = context().cache_manager().cached_images.get(
796 context(), *image, image_user_for_layer, pass_name.c_str());
797
798 /* The layers will be released by the caller, so return a wrapper around the cached image
799 * instead. */
800 Result layer_result = this->context().create_result(pass_result.type(),
801 pass_result.precision());
802 layer_result.share_data(pass_result);
803
804 layers.append(std::move(layer_result));
805 }
806
807 return layers;
808 }
809
810 /* Returns the combined name of the render layer and pass using the EXR convention of a period
811 * separator. */
812 std::string get_combined_layer_pass_name(RenderLayer *render_layer, RenderPass *render_pass)
813 {
814 if (render_layer->name[0] == '\0') {
815 return std::string(render_pass->name);
816 }
817 return std::string(render_layer->name) + "." + std::string(render_pass->name);
818 }
819
820 /* Get the selected type name of the Cryptomatte from the metadata of the image/render. This type
821 * name will be used to identify the corresponding layers in the source image/render. See the
822 * "EXR File: Layer Naming" section of the Cryptomatte specification for more information on what
823 * this represents. */
824 std::string get_type_name()
825 {
826 char type_name[MAX_NAME];
827 ntreeCompositCryptomatteLayerPrefix(&node(), type_name, sizeof(type_name));
828 return std::string(type_name);
829 }
830
831 /* The domain should be centered with the same size as the source. In case of invalid source,
832 * fall back to the domain inferred from the input. */
834 {
835 switch (get_source()) {
837 return context().get_compositing_domain();
839 return compute_image_domain();
840 }
841
843 return Domain::identity();
844 }
845
846 /* In case of an image source, the domain should be centered with the same size as the source
847 * image. In case of an invalid image, fall back to the domain inferred from the input. */
849 {
851
852 Image *image = get_image();
853 if (!image) {
855 }
856
857 ImageUser image_user = this->get_image_user();
858 ImBuf *image_buffer = BKE_image_acquire_ibuf(image, &image_user, nullptr);
859 if (!image_buffer) {
861 }
862
863 const int2 image_size = int2(image_buffer->x, image_buffer->y);
864 BKE_image_release_ibuf(image, image_buffer, nullptr);
865 return Domain(image_size);
866 }
867
869 {
871
872 Image *image = this->get_image();
874
875 /* Compute the effective frame number of the image if it was animated. */
876 ImageUser image_user_for_frame = node_storage(node()).iuser;
877 BKE_image_user_frame_calc(image, &image_user_for_frame, this->context().get_frame_number());
878
879 return image_user_for_frame;
880 }
881
883 {
885 return reinterpret_cast<Scene *>(node().id);
886 }
887
889 {
891 return reinterpret_cast<Image *>(node().id);
892 }
893
895 {
896 return static_cast<CMPNodeCryptomatteSource>(node().custom1);
897 }
898};
899
901{
902 return new CryptoMatteOperation(context, node);
903}
904
905static void node_register()
906{
907 static bke::bNodeType ntype;
908
909 cmp_node_type_base(&ntype, "CompositorNodeCryptomatteV2"_ustr, CMP_NODE_CRYPTOMATTE);
910 ntype.ui_name = "Cryptomatte";
911 ntype.ui_description =
912 "Generate matte for individual objects and materials using Cryptomatte render passes";
913 ntype.enum_name_legacy = "CRYPTOMATTE_V2";
914 ntype.nclass = NODE_CLASS_MATTE;
915 ntype.declare = node_declare;
917 ntype.initfunc = node_init;
920 ntype.updatefunc = node_update;
923
925}
926NOD_REGISTER_NODE(node_register)
927
928} // namespace nodes::node_composite_cryptomatte_cc
929
931{
933 NodeCryptomatte *n = static_cast<NodeCryptomatte *>(node->storage);
934 n->inputs_num++;
935}
936
938{
940 NodeCryptomatte *n = static_cast<NodeCryptomatte *>(node->storage);
941 if (n->inputs_num < 2) {
942 return false;
943 }
944 n->inputs_num--;
945 return true;
946}
947
949
951{
952 b.add_input<decl::Color>("Image"_ustr)
953 .default_value({0.0f, 0.0f, 0.0f, 1.0f})
954 .structure_type(StructureType::Dynamic);
955
956 b.add_output<decl::Color>("Image"_ustr).structure_type(StructureType::Dynamic);
957 b.add_output<decl::Float>("Matte"_ustr).structure_type(StructureType::Dynamic);
958 b.add_output<decl::Color>("Pick"_ustr).structure_type(StructureType::Dynamic);
959
960 const bNode *node = b.node_or_null();
961 if (!node) {
962 b.add_input<decl::Color>("Crypto 00"_ustr).structure_type(StructureType::Dynamic);
963 return;
964 }
965
966 const int inputs_count = static_cast<NodeCryptomatte *>(node->storage)->inputs_num;
967 for (int i = 0; i < inputs_count; i++) {
968 const std::string name = fmt::format("Crypto {:02}", i);
969 b.add_input<decl::Color>(UString(name)).structure_type(StructureType::Dynamic);
970 }
971}
972
973static void node_init(bNodeTree * /*ntree*/, bNode *node)
974{
975 NodeCryptomatte *storage = MEM_new<NodeCryptomatte>(__func__);
976 node->storage = storage;
977
978 /* Add three inputs by default, as recommended by the Cryptomatte specification. */
979 storage->inputs_num = 3;
980}
981
982using namespace blender::compositor;
984
986 public:
987 using BaseCryptoMatteOperation::BaseCryptoMatteOperation;
988
990 {
991 return this->get_input("Image");
992 }
993
995 {
996 Vector<Result> layers;
997 /* Add all valid results of all inputs except the first input, which is the input image. */
998 for (const bNodeSocket *input_socket : node().input_sockets().drop_front(1)) {
999 if (!is_socket_available(input_socket)) {
1000 continue;
1001 }
1002
1003 const Result &input = get_input(input_socket->identifier);
1004 if (input.is_single_value()) {
1005 /* If this Cryptomatte layer is not valid, because it is not an image, then all later
1006 * Cryptomatte layers can't be used even if they were valid. */
1007 break;
1008 }
1009
1010 /* The layers will be released by the caller, so return a wrapper around the input result
1011 * instead. */
1012 Result layer_result = this->context().create_result(input.type(), input.precision());
1013 layer_result.share_data(input);
1014
1015 layers.append(std::move(layer_result));
1016 }
1017 return layers;
1018 }
1019};
1020
1022{
1023 return new LegacyCryptoMatteOperation(context, node);
1024}
1025
1026static void node_register()
1027{
1028 static bke::bNodeType ntype;
1029
1030 cmp_node_type_base(&ntype, "CompositorNodeCryptomatte"_ustr, CMP_NODE_CRYPTOMATTE_LEGACY);
1031 ntype.ui_name = "Cryptomatte (Legacy)";
1032 ntype.ui_description = "Deprecated. Use Cryptomatte Node instead";
1033 ntype.enum_name_legacy = "CRYPTOMATTE";
1034 ntype.nclass = NODE_CLASS_MATTE;
1035 ntype.declare = node_declare;
1036 ntype.initfunc = node_init;
1038 ntype.gather_link_search_ops = nullptr;
1040
1042}
1043NOD_REGISTER_NODE(node_register)
1044
1045} // namespace nodes::node_composite_legacy_cryptomatte_cc
1046
1047} // namespace blender
#define NODE_CLASS_MATTE
Definition BKE_node.hh:485
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1353
#define CMP_NODE_CRYPTOMATTE
#define CMP_NODE_CRYPTOMATTE_LEGACY
#define BLI_SCOPED_DEFER(function_to_defer)
#define RPT_(msgid)
#define TIP_(msgid)
#define MAX_NAME
Definition DNA_defs.h:63
#define RE_PASSNAME_CRYPTOMATTE_MATERIAL
#define RE_PASSNAME_CRYPTOMATTE_ASSET
#define RE_PASSNAME_CRYPTOMATTE_OBJECT
Read Blender memory allocation and freeing.
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define C
Definition RandGen.cpp:29
#define BLI_assert_unreachable()
#define ELEM(...)
#define output
Result create_result(ResultType type, ResultPrecision precision)
gpu::Shader * get_shader(const char *info_name, ResultPrecision precision)
static Domain identity()
Definition domain.cc:47
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 share_data(const Result &source)
Definition result.cc:996
void unbind_as_texture_or_single_value(gpu::Texture *single_value_texture) const
Definition result.cc:970
void store_pixel(const int2 &texel, const T &pixel_value)
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
static ResultType type(gpu::TextureFormat format)
Definition result.cc:337
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
T load_pixel(const int2 &texel) const
void unbind_as_image() const
Definition result.cc:990
static ResultPrecision precision(gpu::TextureFormat format)
Definition result.cc:307
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
Vector< Span< float > > get_identifiers_slices(const Vector< float > &identifiers)
std::string get_combined_layer_pass_name(RenderLayer *render_layer, RenderPass *render_pass)
#define storage(slot, qualifiers)
#define layer
bool is_viewport_compositor_used(const bContext &context)
std::unique_ptr< CryptomatteSession, CryptomatteSessionDeleter > CryptomatteSessionPtr
const Vector< std::string > & BKE_cryptomatte_layer_names_get(const CryptomatteSession &session)
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 parallel_for(const int2 range, const Function &function)
bool is_socket_available(const bNodeSocket *socket)
Definition utilities.cc:26
T ceil(const T &a)
static void node_copy_cryptomatte(bNodeTree *, bNode *dest_node, const bNode *src_node)
static void node_extra_info(NodeExtraInfoParams &parameters)
static NodeOperation * get_compositor_operation(Context &context, const bNode &node)
static void node_init_api(const bContext *C, PointerRNA *node_ptr)
static void node_declare(NodeDeclarationBuilder &b)
static NodeOperation * get_compositor_operation(Context &context, const bNode &node)
BMesh Iterators.
static void cryptomatte_remove(NodeCryptomatte &n, float encoded_hash)
void BKE_image_user_frame_calc(Image *ima, ImageUser *iuser, int cfra)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
RenderResult * BKE_image_acquire_renderresult(Scene *scene, Image *ima)
bool BKE_cryptomatte_find_name(const struct CryptomatteSession *session, float encoded_hash, char *r_name, int name_maxncpy)
static void cryptomatte_add(bNode &node, NodeCryptomatte &node_cryptomatte, float encoded_hash)
@ IMA_TYPE_MULTILAYER
static bke::cryptomatte::CryptomatteSessionPtr cryptomatte_init_from_node_image(const bNode &node)
void id_us_plus(ID *id)
Definition lib_id.cc:433
void BKE_image_release_renderresult(Scene *scene, Image *ima, RenderResult *render_result)
void void void void void void BLI_duplicatelist(ListBase *dst, const ListBase *src) ATTR_NONNULL(1
Definition listbase.cc:834
struct CryptomatteSession * BKE_cryptomatte_init_from_scene(const struct Scene *scene, bool build_meta_data)
char * BLI_strncpy_utf8(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
void GPU_shader_uniform_1f_array(gpu::Shader *sh, const char *name, int len, const float *val)
static bke::cryptomatte::CryptomatteSessionPtr cryptomatte_init_from_node(const bNode &node, const bool build_meta_data)
const char * RE_engine_id_BLENDER_EEVEE
Definition scene.cc:1717
CryptomatteSession * ntreeCompositCryptomatteSession(bNode *node)
@ SNODE_COMPOSITOR_SCENE
bool ntreeCompositCryptomatteRemoveSocket(bNode *node)
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
void ntreeCompositCryptomatteAddSocket(bNode *node)
void GPU_shader_unbind()
static CryptomatteEntry * cryptomatte_find(const NodeCryptomatte &n, float encoded_hash)
void ntreeCompositCryptomatteLayerPrefix(const bNode *node, char *r_prefix, size_t prefix_maxncpy)
SpaceNode * CTX_wm_space_node(const bContext *C)
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)
MINLINE void zero_v3(float r[3])
void ntreeCompositCryptomatteSyncFromRemove(bNode *node)
ImBuf * BKE_image_acquire_ibuf(Image *ima, ImageUser *iuser, void **r_lock)
static bke::cryptomatte::CryptomatteSessionPtr cryptomatte_init_from_node_render(const bNode &node, const bool build_meta_data)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
@ IMA_USER_FRAME_IN_RANGE
void GPU_texture_clear(gpu::Texture *texture, eGPUDataFormat data_format, const void *data)
ID_Type GS(const char *name)
Definition DNA_ID.h:410
void ntreeCompositCryptomatteSyncFromAdd(bNode *node)
Scene * CTX_data_scene(const bContext *C)
int BKE_image_sequence_guess_offset(Image *image)
void ntreeCompositCryptomatteUpdateLayerNames(bNode *node)
@ CMP_NODE_CRYPTOMATTE_SOURCE_IMAGE
@ CMP_NODE_CRYPTOMATTE_SOURCE_RENDER
struct CryptomatteSession * BKE_cryptomatte_init_from_render_result(const struct RenderResult *render_result)
void BKE_image_release_ibuf(Image *ima, ImBuf *ibuf, void *lock)
void cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
char name[258]
Definition DNA_ID.h:433
eImageUser_Flag flag
ListBaseT< CryptomatteLayer > layers
ListBaseT< CryptomatteEntry > entries
NodeCryptomatte_Runtime runtime
T * data_as() const
Definition RNA_types.hh:139
ListBaseT< RenderPass > passes
Definition RE_pipeline.h:99
char name[RE_MAXNAME]
Definition RE_pipeline.h:94
ListBaseT< RenderLayer > layers
ListBaseT< ViewLayer > view_layers
struct RenderData r
static constexpr int _240
Definition BKE_node.hh:250
Defines a node type.
Definition BKE_node.hh:265
std::string ui_description
Definition BKE_node.hh:271
void(*) updatefunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:312
void(*) initfunc_api(const bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:333
NodeGetCompositorOperationFunction get_compositor_operation
Definition BKE_node.hh:379
NodeExtraInfoFunction get_extra_info
Definition BKE_node.hh:412
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:409
NodeDeclareFunction declare
Definition BKE_node.hh:393
Vector< NodeExtraInfoRow > & rows
int size() const
Definition vector.hh:130