diff options
author | Jeroen Bakker <jeroen@blender.org> | 2021-02-24 17:17:29 +0300 |
---|---|---|
committer | Jeroen Bakker <jeroen@blender.org> | 2021-02-24 17:18:31 +0300 |
commit | 79f34447d649a0fc680e3ebf95fa730981831668 (patch) | |
tree | 71cd255508a05a933f7a5637202a676826d9de8d /source/blender/blenkernel | |
parent | 4fb0c83c1c8529b61d38d8a409caf58d05a53bd9 (diff) |
Cleanup: CryptomatteLayer structure.
Current implementation was to restricting for future enhancements where
the CryptomatterLayer could be read from existing metadata.
Diffstat (limited to 'source/blender/blenkernel')
-rw-r--r-- | source/blender/blenkernel/BKE_cryptomatte.h | 1 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/cryptomatte.cc | 82 |
2 files changed, 31 insertions, 52 deletions
diff --git a/source/blender/blenkernel/BKE_cryptomatte.h b/source/blender/blenkernel/BKE_cryptomatte.h index 2b5a6a2044b..4b62c795a5a 100644 --- a/source/blender/blenkernel/BKE_cryptomatte.h +++ b/source/blender/blenkernel/BKE_cryptomatte.h @@ -38,7 +38,6 @@ struct Object; struct RenderResult; struct CryptomatteSession *BKE_cryptomatte_init(void); -void BKE_cryptomatte_finish(struct CryptomatteSession *session); void BKE_cryptomatte_free(struct CryptomatteSession *session); uint32_t BKE_cryptomatte_hash(const char *name, int name_len); diff --git a/source/blender/blenkernel/intern/cryptomatte.cc b/source/blender/blenkernel/intern/cryptomatte.cc index db83547fe36..d2a3f6eebbe 100644 --- a/source/blender/blenkernel/intern/cryptomatte.cc +++ b/source/blender/blenkernel/intern/cryptomatte.cc @@ -35,7 +35,7 @@ #include "BLI_dynstr.h" #include "BLI_hash_mm3.h" #include "BLI_listbase.h" -#include "BLI_set.hh" +#include "BLI_map.hh" #include "BLI_string.h" #include "MEM_guardedalloc.h" @@ -47,54 +47,47 @@ #include <string> #include <string_view> -enum class CryptomatteLayerState { - EMPTY, - FILLED, - CLOSED, -}; - struct CryptomatteLayer { - CryptomatteLayerState state = CryptomatteLayerState::EMPTY; - blender::Set<std::string> names; - std::stringstream manifest; + blender::Map<std::string, std::string> hashes; #ifdef WITH_CXX_GUARDEDALLOC MEM_CXX_CLASS_ALLOC_FUNCS("cryptomatte:CryptomatteLayer") #endif - - void add_hash(std::string name, uint32_t cryptomatte_hash) + std::string encode_hash(uint32_t cryptomatte_hash) { - BLI_assert(state != CryptomatteLayerState::CLOSED); - const bool first_item = names.is_empty(); - if (!names.add(name)) { - return; - } + std::stringstream encoded; + encoded << std::setfill('0') << std::setw(sizeof(uint32_t) * 2) << std::hex + << cryptomatte_hash; + return encoded.str(); + } - if (first_item) { - state = CryptomatteLayerState::FILLED; - manifest << "{"; - } - else { - manifest << ","; - } - manifest << quoted(name) << ":\""; - manifest << std::setfill('0') << std::setw(sizeof(uint32_t) * 2) << std::hex - << cryptomatte_hash; - manifest << "\""; + void add_hash(blender::StringRef name, uint32_t cryptomatte_hash) + { + hashes.add_overwrite(name, encode_hash(cryptomatte_hash)); } - void close_manifest() + void add_encoded_hash(blender::StringRef name, std::string &cryptomatte_encoded_hash) { - BLI_assert(state != CryptomatteLayerState::CLOSED); - if (state == CryptomatteLayerState::FILLED) { - manifest << "}"; - } - state = CryptomatteLayerState::CLOSED; + hashes.add_overwrite(name, cryptomatte_encoded_hash); } - std::string manifest_get_string() + std::string manifest() { - BLI_assert(state == CryptomatteLayerState::CLOSED); + std::stringstream manifest; + + bool is_first = true; + const blender::Map<std::string, std::string> &const_map = hashes; + manifest << "{"; + for (blender::Map<std::string, std::string>::Item item : const_map.items()) { + if (is_first) { + is_first = false; + } + else { + manifest << ","; + } + manifest << quoted(item.key) << ":\"" << item.value << "\""; + } + manifest << "}"; return manifest.str(); } }; @@ -107,13 +100,6 @@ struct CryptomatteSession { #ifdef WITH_CXX_GUARDEDALLOC MEM_CXX_CLASS_ALLOC_FUNCS("cryptomatte:CryptomatteSession") #endif - - void finish() - { - objects.close_manifest(); - materials.close_manifest(); - assets.close_manifest(); - } }; CryptomatteSession *BKE_cryptomatte_init(void) @@ -122,12 +108,6 @@ CryptomatteSession *BKE_cryptomatte_init(void) return session; } -void BKE_cryptomatte_finish(CryptomatteSession *session) -{ - BLI_assert(session != nullptr); - session->finish(); -} - void BKE_cryptomatte_free(CryptomatteSession *session) { BLI_assert(session != nullptr); @@ -147,7 +127,7 @@ static uint32_t cryptomatte_hash(CryptomatteLayer *layer, const ID *id) uint32_t cryptohash_int = BKE_cryptomatte_hash(name, name_len); if (layer != nullptr) { - layer->add_hash(std::string(name, name_len), cryptohash_int); + layer->add_hash(blender::StringRef(name, name_len), cryptohash_int); } return cryptohash_int; @@ -336,7 +316,7 @@ void BKE_cryptomatte_store_metadata(struct CryptomatteSession *session, break; } - const std::string manifest = layer->manifest_get_string(); + const std::string manifest = layer->manifest(); const std::string name = cryptomatte_determine_name(view_layer, cryptomatte_layer_name); /* Store the meta data into the render result. */ |