From 13310e4ba07d77b47f10d583b119cf1bf9b23c73 Mon Sep 17 00:00:00 2001 From: "kunyoung.park" <38232477+kunyoungparkk@users.noreply.github.com> Date: Wed, 23 Oct 2024 02:45:32 +0900 Subject: [PATCH] Fix issue where material instance name cannot be set without creating a default instance (#8213) --- filament/src/details/Engine.cpp | 5 +++-- filament/src/details/Engine.h | 3 ++- filament/src/details/Material.cpp | 4 ++-- filament/src/details/MaterialInstance.cpp | 6 ++++-- filament/src/details/MaterialInstance.h | 3 ++- 5 files changed, 13 insertions(+), 8 deletions(-) diff --git a/filament/src/details/Engine.cpp b/filament/src/details/Engine.cpp index e62a94d0307..a6c5f2eead8 100644 --- a/filament/src/details/Engine.cpp +++ b/filament/src/details/Engine.cpp @@ -835,8 +835,9 @@ FMaterialInstance* FEngine::createMaterialInstance(const FMaterial* material, return p; } -FMaterialInstance* FEngine::createMaterialInstance(const FMaterial* material) noexcept { - FMaterialInstance* p = mHeapAllocator.make(*this, material); +FMaterialInstance* FEngine::createMaterialInstance(const FMaterial* material, + const char* name) noexcept { + FMaterialInstance* p = mHeapAllocator.make(*this, material, name); if (UTILS_UNLIKELY(p)) { // should never happen auto pos = mMaterialInstances.emplace(material, "MaterialInstance"); pos.first->second.insert(p); diff --git a/filament/src/details/Engine.h b/filament/src/details/Engine.h index 331f934d993..272bafd4db9 100644 --- a/filament/src/details/Engine.h +++ b/filament/src/details/Engine.h @@ -303,7 +303,8 @@ class FEngine : public Engine { FMaterialInstance* createMaterialInstance(const FMaterial* material, const FMaterialInstance* other, const char* name) noexcept; - FMaterialInstance* createMaterialInstance(const FMaterial* material) noexcept; + FMaterialInstance* createMaterialInstance(const FMaterial* material, + const char* name) noexcept; FScene* createScene() noexcept; FView* createView() noexcept; diff --git a/filament/src/details/Material.cpp b/filament/src/details/Material.cpp index d60087c4bd4..90f65b64013 100644 --- a/filament/src/details/Material.cpp +++ b/filament/src/details/Material.cpp @@ -462,13 +462,13 @@ FMaterialInstance* FMaterial::createInstance(const char* name) const noexcept { return FMaterialInstance::duplicate(mDefaultMaterialInstance, name); } else { // but if we don't, just create an instance with all the default parameters - return mEngine.createMaterialInstance(this); + return mEngine.createMaterialInstance(this, name); } } FMaterialInstance* FMaterial::getDefaultInstance() noexcept { if (UTILS_UNLIKELY(!mDefaultMaterialInstance)) { - mDefaultMaterialInstance = mEngine.createMaterialInstance(this); + mDefaultMaterialInstance = mEngine.createMaterialInstance(this, mName.c_str()); mDefaultMaterialInstance->setDefaultInstance(true); } return mDefaultMaterialInstance; diff --git a/filament/src/details/MaterialInstance.cpp b/filament/src/details/MaterialInstance.cpp index afc02418aa4..4bbf44eddfa 100644 --- a/filament/src/details/MaterialInstance.cpp +++ b/filament/src/details/MaterialInstance.cpp @@ -56,7 +56,8 @@ namespace filament { using namespace backend; -FMaterialInstance::FMaterialInstance(FEngine& engine, FMaterial const* material) noexcept +FMaterialInstance::FMaterialInstance(FEngine& engine, FMaterial const* material, + const char* name) noexcept : mMaterial(material), mDescriptorSet(material->getDescriptorSetLayout()), mCulling(CullingMode::BACK), @@ -66,7 +67,8 @@ FMaterialInstance::FMaterialInstance(FEngine& engine, FMaterial const* material) mHasScissor(false), mIsDoubleSided(false), mIsDefaultInstance(false), - mTransparencyMode(TransparencyMode::DEFAULT) { + mTransparencyMode(TransparencyMode::DEFAULT), + mName(name ? CString(name) : material->getName()) { FEngine::DriverApi& driver = engine.getDriverApi(); diff --git a/filament/src/details/MaterialInstance.h b/filament/src/details/MaterialInstance.h index b31c0a65273..0a43831e12a 100644 --- a/filament/src/details/MaterialInstance.h +++ b/filament/src/details/MaterialInstance.h @@ -53,7 +53,8 @@ class FTexture; class FMaterialInstance : public MaterialInstance { public: - FMaterialInstance(FEngine& engine, FMaterial const* material) noexcept; + FMaterialInstance(FEngine& engine, FMaterial const* material, + const char* name) noexcept; FMaterialInstance(FEngine& engine, FMaterialInstance const* other, const char* name); FMaterialInstance(const FMaterialInstance& rhs) = delete; FMaterialInstance& operator=(const FMaterialInstance& rhs) = delete;