Changeset View
Changeset View
Standalone View
Standalone View
source/blender/blenkernel/intern/cryptomatte.cc
| Show All 35 Lines | |||||
| struct CryptomatteSession { | struct CryptomatteSession { | ||||
| blender::Map<std::string, blender::bke::cryptomatte::CryptomatteLayer> layers; | blender::Map<std::string, blender::bke::cryptomatte::CryptomatteLayer> layers; | ||||
| /* Layer names in order of creation. */ | /* Layer names in order of creation. */ | ||||
| blender::Vector<std::string> layer_names; | blender::Vector<std::string> layer_names; | ||||
| CryptomatteSession() = default; | CryptomatteSession() = default; | ||||
| CryptomatteSession(const Main *bmain); | CryptomatteSession(const Main *bmain); | ||||
| CryptomatteSession(StampData *stamp_data); | CryptomatteSession(StampData *stamp_data); | ||||
| CryptomatteSession(const ViewLayer *view_layer); | |||||
| CryptomatteSession(const Scene *scene); | CryptomatteSession(const Scene *scene); | ||||
| void init(const ViewLayer *view_layer); | |||||
| blender::bke::cryptomatte::CryptomatteLayer &add_layer(std::string layer_name); | blender::bke::cryptomatte::CryptomatteLayer &add_layer(std::string layer_name); | ||||
| std::optional<std::string> operator[](float encoded_hash) const; | std::optional<std::string> operator[](float encoded_hash) const; | ||||
| #ifdef WITH_CXX_GUARDEDALLOC | #ifdef WITH_CXX_GUARDEDALLOC | ||||
| MEM_CXX_CLASS_ALLOC_FUNCS("cryptomatte:CryptomatteSession") | MEM_CXX_CLASS_ALLOC_FUNCS("cryptomatte:CryptomatteSession") | ||||
| #endif | #endif | ||||
| }; | }; | ||||
| CryptomatteSession::CryptomatteSession(const Main *bmain) | CryptomatteSession::CryptomatteSession(const Main *bmain) | ||||
| { | { | ||||
| if (!BLI_listbase_is_empty(&bmain->objects)) { | if (!BLI_listbase_is_empty(&bmain->objects)) { | ||||
| blender::bke::cryptomatte::CryptomatteLayer &objects = add_layer("CryptoObject"); | blender::bke::cryptomatte::CryptomatteLayer &objects = add_layer( | ||||
| RE_PASSNAME_CRYPTOMATTE_OBJECT); | |||||
| LISTBASE_FOREACH (ID *, id, &bmain->objects) { | LISTBASE_FOREACH (ID *, id, &bmain->objects) { | ||||
| objects.add_ID(*id); | objects.add_ID(*id); | ||||
| } | } | ||||
| } | } | ||||
| if (!BLI_listbase_is_empty(&bmain->materials)) { | if (!BLI_listbase_is_empty(&bmain->materials)) { | ||||
| blender::bke::cryptomatte::CryptomatteLayer &materials = add_layer("CryptoMaterial"); | blender::bke::cryptomatte::CryptomatteLayer &materials = add_layer( | ||||
| RE_PASSNAME_CRYPTOMATTE_MATERIAL); | |||||
| LISTBASE_FOREACH (ID *, id, &bmain->materials) { | LISTBASE_FOREACH (ID *, id, &bmain->materials) { | ||||
| materials.add_ID(*id); | materials.add_ID(*id); | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| CryptomatteSession::CryptomatteSession(StampData *stamp_data) | CryptomatteSession::CryptomatteSession(StampData *stamp_data) | ||||
| { | { | ||||
| blender::bke::cryptomatte::CryptomatteStampDataCallbackData callback_data; | blender::bke::cryptomatte::CryptomatteStampDataCallbackData callback_data; | ||||
| callback_data.session = this; | callback_data.session = this; | ||||
| BKE_stamp_info_callback( | BKE_stamp_info_callback( | ||||
| &callback_data, | &callback_data, | ||||
| stamp_data, | stamp_data, | ||||
| blender::bke::cryptomatte::CryptomatteStampDataCallbackData::extract_layer_names, | blender::bke::cryptomatte::CryptomatteStampDataCallbackData::extract_layer_names, | ||||
| false); | false); | ||||
| BKE_stamp_info_callback( | BKE_stamp_info_callback( | ||||
| &callback_data, | &callback_data, | ||||
| stamp_data, | stamp_data, | ||||
| blender::bke::cryptomatte::CryptomatteStampDataCallbackData::extract_layer_manifest, | blender::bke::cryptomatte::CryptomatteStampDataCallbackData::extract_layer_manifest, | ||||
| false); | false); | ||||
| } | } | ||||
| CryptomatteSession::CryptomatteSession(const ViewLayer *view_layer) | |||||
| { | |||||
| init(view_layer); | |||||
| } | |||||
| CryptomatteSession::CryptomatteSession(const Scene *scene) | CryptomatteSession::CryptomatteSession(const Scene *scene) | ||||
| { | { | ||||
| LISTBASE_FOREACH (ViewLayer *, view_layer, &scene->view_layers) { | LISTBASE_FOREACH (const ViewLayer *, view_layer, &scene->view_layers) { | ||||
| init(view_layer); | |||||
| } | |||||
| } | |||||
| void CryptomatteSession::init(const ViewLayer *view_layer) | |||||
| { | |||||
| eViewLayerCryptomatteFlags cryptoflags = static_cast<eViewLayerCryptomatteFlags>( | eViewLayerCryptomatteFlags cryptoflags = static_cast<eViewLayerCryptomatteFlags>( | ||||
| view_layer->cryptomatte_flag & VIEW_LAYER_CRYPTOMATTE_ALL); | view_layer->cryptomatte_flag & VIEW_LAYER_CRYPTOMATTE_ALL); | ||||
| if (cryptoflags == 0) { | if (cryptoflags == 0) { | ||||
| cryptoflags = static_cast<eViewLayerCryptomatteFlags>(VIEW_LAYER_CRYPTOMATTE_ALL); | cryptoflags = static_cast<eViewLayerCryptomatteFlags>(VIEW_LAYER_CRYPTOMATTE_ALL); | ||||
| } | } | ||||
| if (cryptoflags & VIEW_LAYER_CRYPTOMATTE_OBJECT) { | if (cryptoflags & VIEW_LAYER_CRYPTOMATTE_OBJECT) { | ||||
| add_layer(blender::StringRefNull(view_layer->name) + ".CryptoObject"); | add_layer(blender::StringRefNull(view_layer->name) + "." + RE_PASSNAME_CRYPTOMATTE_OBJECT); | ||||
| } | } | ||||
| if (cryptoflags & VIEW_LAYER_CRYPTOMATTE_ASSET) { | if (cryptoflags & VIEW_LAYER_CRYPTOMATTE_ASSET) { | ||||
| add_layer(blender::StringRefNull(view_layer->name) + ".CryptoAsset"); | add_layer(blender::StringRefNull(view_layer->name) + "." + RE_PASSNAME_CRYPTOMATTE_ASSET); | ||||
| } | } | ||||
| if (cryptoflags & VIEW_LAYER_CRYPTOMATTE_MATERIAL) { | if (cryptoflags & VIEW_LAYER_CRYPTOMATTE_MATERIAL) { | ||||
| add_layer(blender::StringRefNull(view_layer->name) + ".CryptoMaterial"); | add_layer(blender::StringRefNull(view_layer->name) + "." + RE_PASSNAME_CRYPTOMATTE_MATERIAL); | ||||
| } | |||||
| } | } | ||||
| } | } | ||||
| blender::bke::cryptomatte::CryptomatteLayer &CryptomatteSession::add_layer(std::string layer_name) | blender::bke::cryptomatte::CryptomatteLayer &CryptomatteSession::add_layer(std::string layer_name) | ||||
| { | { | ||||
| if (!layer_names.contains(layer_name)) { | if (!layer_names.contains(layer_name)) { | ||||
| layer_names.append(layer_name); | layer_names.append(layer_name); | ||||
| } | } | ||||
| Show All 25 Lines | |||||
| } | } | ||||
| struct CryptomatteSession *BKE_cryptomatte_init_from_scene(const struct Scene *scene) | struct CryptomatteSession *BKE_cryptomatte_init_from_scene(const struct Scene *scene) | ||||
| { | { | ||||
| CryptomatteSession *session = new CryptomatteSession(scene); | CryptomatteSession *session = new CryptomatteSession(scene); | ||||
| return session; | return session; | ||||
| } | } | ||||
| struct CryptomatteSession *BKE_cryptomatte_init_from_view_layer(const struct ViewLayer *view_layer) | |||||
| { | |||||
| CryptomatteSession *session = new CryptomatteSession(view_layer); | |||||
| return session; | |||||
| } | |||||
| void BKE_cryptomatte_add_layer(struct CryptomatteSession *session, const char *layer_name) | void BKE_cryptomatte_add_layer(struct CryptomatteSession *session, const char *layer_name) | ||||
| { | { | ||||
| session->add_layer(layer_name); | session->add_layer(layer_name); | ||||
| } | } | ||||
| void BKE_cryptomatte_free(CryptomatteSession *session) | void BKE_cryptomatte_free(CryptomatteSession *session) | ||||
| { | { | ||||
| BLI_assert(session != nullptr); | BLI_assert(session != nullptr); | ||||
| ▲ Show 20 Lines • Show All 327 Lines • ▼ Show 20 Lines | StringRef BKE_cryptomatte_extract_layer_name(const StringRef render_pass_name) | ||||
| } | } | ||||
| return render_pass_name.substr(0, last_token); | return render_pass_name.substr(0, last_token); | ||||
| } | } | ||||
| CryptomatteHash::CryptomatteHash(uint32_t hash) : hash(hash) | CryptomatteHash::CryptomatteHash(uint32_t hash) : hash(hash) | ||||
| { | { | ||||
| } | } | ||||
| CryptomatteHash::CryptomatteHash(const char *name, const int name_len) | |||||
| { | |||||
| hash = BLI_hash_mm3((const unsigned char *)name, name_len, 0); | |||||
| } | |||||
| CryptomatteHash CryptomatteHash::from_hex_encoded(blender::StringRef hex_encoded) | CryptomatteHash CryptomatteHash::from_hex_encoded(blender::StringRef hex_encoded) | ||||
| { | { | ||||
| CryptomatteHash result(0); | CryptomatteHash result(0); | ||||
| std::istringstream(hex_encoded) >> std::hex >> result.hash; | std::istringstream(hex_encoded) >> std::hex >> result.hash; | ||||
| return result; | return result; | ||||
| } | } | ||||
| std::string CryptomatteHash::hex_encoded() const | std::string CryptomatteHash::hex_encoded() const | ||||
| { | { | ||||
| std::stringstream encoded; | std::stringstream encoded; | ||||
| encoded << std::setfill('0') << std::setw(sizeof(uint32_t) * 2) << std::hex << hash; | encoded << std::setfill('0') << std::setw(sizeof(uint32_t) * 2) << std::hex << hash; | ||||
| return encoded.str(); | return encoded.str(); | ||||
| } | } | ||||
| float CryptomatteHash::float_encoded() const | |||||
| { | |||||
| uint32_t mantissa = hash & ((1 << 23) - 1); | |||||
| uint32_t exponent = (hash >> 23) & ((1 << 8) - 1); | |||||
| exponent = MAX2(exponent, (uint32_t)1); | |||||
| exponent = MIN2(exponent, (uint32_t)254); | |||||
| exponent = exponent << 23; | |||||
| uint32_t sign = (hash >> 31); | |||||
| sign = sign << 31; | |||||
| uint32_t float_bits = sign | exponent | mantissa; | |||||
| float f; | |||||
| memcpy(&f, &float_bits, sizeof(uint32_t)); | |||||
| return f; | |||||
| } | |||||
| std::unique_ptr<CryptomatteLayer> CryptomatteLayer::read_from_manifest( | std::unique_ptr<CryptomatteLayer> CryptomatteLayer::read_from_manifest( | ||||
| blender::StringRefNull manifest) | blender::StringRefNull manifest) | ||||
| { | { | ||||
| std::unique_ptr<CryptomatteLayer> layer = std::make_unique<CryptomatteLayer>(); | std::unique_ptr<CryptomatteLayer> layer = std::make_unique<CryptomatteLayer>(); | ||||
| blender::bke::cryptomatte::manifest::from_manifest(*layer, manifest); | blender::bke::cryptomatte::manifest::from_manifest(*layer, manifest); | ||||
| return layer; | return layer; | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 90 Lines • ▼ Show 20 Lines | |||||
| } | } | ||||
| const blender::Vector<std::string> &BKE_cryptomatte_layer_names_get( | const blender::Vector<std::string> &BKE_cryptomatte_layer_names_get( | ||||
| const CryptomatteSession &session) | const CryptomatteSession &session) | ||||
| { | { | ||||
| return session.layer_names; | return session.layer_names; | ||||
| } | } | ||||
| CryptomatteLayer *BKE_cryptomatte_layer_get(CryptomatteSession &session, StringRef layer_name) | |||||
| { | |||||
| return session.layers.lookup_ptr(layer_name); | |||||
| } | |||||
| } // namespace blender::bke::cryptomatte | } // namespace blender::bke::cryptomatte | ||||