From 120652fcf0eb3e545ac1b24b91978e28e1af7351 Mon Sep 17 00:00:00 2001 From: Sean Lilley Date: Thu, 31 Aug 2023 20:07:45 -0400 Subject: [PATCH] Temp --- .../include/cesium/omniverse/FabricGeometry.h | 1 + .../include/cesium/omniverse/FabricMaterial.h | 4 +++- .../cesium/omniverse/FabricResourceManager.h | 2 ++ .../include/cesium/omniverse/FabricTexture.h | 4 +++- src/core/include/cesium/omniverse/Tokens.h | 2 ++ src/core/src/FabricGeometry.cpp | 17 +++++++++++++++++ src/core/src/FabricMaterial.cpp | 6 ++++-- src/core/src/FabricMaterialPool.cpp | 2 +- src/core/src/FabricPrepareRenderResources.cpp | 3 +-- src/core/src/FabricResourceManager.cpp | 16 +++++++++------- src/core/src/FabricTexture.cpp | 9 +++++++-- src/core/src/FabricTexturePool.cpp | 2 +- 12 files changed, 51 insertions(+), 17 deletions(-) diff --git a/src/core/include/cesium/omniverse/FabricGeometry.h b/src/core/include/cesium/omniverse/FabricGeometry.h index 10f977f6d..781cede10 100644 --- a/src/core/include/cesium/omniverse/FabricGeometry.h +++ b/src/core/include/cesium/omniverse/FabricGeometry.h @@ -39,6 +39,7 @@ class FabricGeometry { [[nodiscard]] const FabricGeometryDefinition& getGeometryDefinition() const; void setMaterial(const omni::fabric::Path& materialPath); + void setTextureIndex(uint64_t textureIndex); private: void initialize(); diff --git a/src/core/include/cesium/omniverse/FabricMaterial.h b/src/core/include/cesium/omniverse/FabricMaterial.h index dc5bc7535..090f8cf4d 100644 --- a/src/core/include/cesium/omniverse/FabricMaterial.h +++ b/src/core/include/cesium/omniverse/FabricMaterial.h @@ -20,7 +20,8 @@ class FabricMaterial { const omni::fabric::Path& path, const FabricMaterialDefinition& materialDefinition, const pxr::TfToken& defaultTextureAssetPathToken, - long stageId); + long stageId, + bool useSharedMaterialForTexturedMaterials); ~FabricMaterial(); void setMaterial(int64_t tilesetId, const MaterialInfo& materialInfo); @@ -57,6 +58,7 @@ class FabricMaterial { const FabricMaterialDefinition _materialDefinition; const pxr::TfToken _defaultTextureAssetPathToken; const long _stageId; + const bool _useSharedMaterialForTexturedMaterials; omni::fabric::Path _shaderPath; omni::fabric::Path _baseColorTexturePath; diff --git a/src/core/include/cesium/omniverse/FabricResourceManager.h b/src/core/include/cesium/omniverse/FabricResourceManager.h index dc5db3bb3..066d379b8 100644 --- a/src/core/include/cesium/omniverse/FabricResourceManager.h +++ b/src/core/include/cesium/omniverse/FabricResourceManager.h @@ -132,6 +132,8 @@ class FabricResourceManager { bool _debugRandomColors{false}; + bool _useSharedMaterialForTexturedMaterials{true}; + std::atomic _geometryId{0}; std::atomic _materialId{0}; std::atomic _textureId{0}; diff --git a/src/core/include/cesium/omniverse/FabricTexture.h b/src/core/include/cesium/omniverse/FabricTexture.h index 48378b4b0..58393b217 100644 --- a/src/core/include/cesium/omniverse/FabricTexture.h +++ b/src/core/include/cesium/omniverse/FabricTexture.h @@ -17,7 +17,7 @@ struct ImageCesium; namespace cesium::omniverse { class FabricTexture { public: - FabricTexture(const std::string& name); + FabricTexture(const std::string& name, uint64_t index); ~FabricTexture(); void setImage(const CesiumGltf::ImageCesium& image); @@ -25,11 +25,13 @@ class FabricTexture { void setActive(bool active); [[nodiscard]] const pxr::TfToken& getAssetPathToken() const; + [[nodiscard]] uint64_t getIndex() const; private: void reset(); std::unique_ptr _texture; pxr::TfToken _assetPathToken; + uint64_t _index; }; } // namespace cesium::omniverse diff --git a/src/core/include/cesium/omniverse/Tokens.h b/src/core/include/cesium/omniverse/Tokens.h index c16bd1b58..8a17822eb 100644 --- a/src/core/include/cesium/omniverse/Tokens.h +++ b/src/core/include/cesium/omniverse/Tokens.h @@ -72,6 +72,7 @@ __pragma(warning(push)) __pragma(warning(disable : 4003)) ((primvars_displayOpacity, "primvars:displayOpacity")) \ ((primvars_normals, "primvars:normals")) \ ((primvars_st, "primvars:st")) \ + ((primvars_textureIndex, "primvars:textureIndex")) \ ((primvars_vertexColor, "primvars:vertexColor")) TF_DECLARE_PUBLIC_TOKENS(UsdTokens, USD_TOKENS); @@ -140,6 +141,7 @@ const omni::fabric::Type primvars_displayColor(omni::fabric::BaseDataType::eFloa const omni::fabric::Type primvars_displayOpacity(omni::fabric::BaseDataType::eFloat, 1, 1, omni::fabric::AttributeRole::eNone); const omni::fabric::Type primvars_normals(omni::fabric::BaseDataType::eFloat, 3, 1, omni::fabric::AttributeRole::eNormal); const omni::fabric::Type primvars_st(omni::fabric::BaseDataType::eFloat, 2, 1, omni::fabric::AttributeRole::eTexCoord); +const omni::fabric::Type primvars_textureIndex(omni::fabric::BaseDataType::eInt, 1, 0, omni::fabric::AttributeRole::eNone); const omni::fabric::Type primvars_vertexColor(omni::fabric::BaseDataType::eFloat, 3, 1, omni::fabric::AttributeRole::eColor); const omni::fabric::Type Shader(omni::fabric::BaseDataType::eTag, 1, 0, omni::fabric::AttributeRole::ePrimTypeName); const omni::fabric::Type subdivisionScheme(omni::fabric::BaseDataType::eToken, 1, 0, omni::fabric::AttributeRole::eNone); diff --git a/src/core/src/FabricGeometry.cpp b/src/core/src/FabricGeometry.cpp index 09eb99d03..562adae17 100644 --- a/src/core/src/FabricGeometry.cpp +++ b/src/core/src/FabricGeometry.cpp @@ -101,6 +101,16 @@ void FabricGeometry::setMaterial(const omni::fabric::Path& materialPath) { materialBindingFabric[0] = materialPath; } +void FabricGeometry::setTextureIndex(uint64_t textureIndex) { + if (stageDestroyed()) { + return; + } + + auto srw = UsdUtil::getFabricStageReaderWriter(); + auto textureIndexFabric = srw.getArrayAttributeWr(_path, FabricTokens::primvars_textureIndex); + textureIndexFabric[0] = static_cast(textureIndex); +} + void FabricGeometry::initialize() { const auto hasTexcoords = _geometryDefinition.hasTexcoords(); const auto hasNormals = _geometryDefinition.hasNormals(); @@ -134,6 +144,7 @@ void FabricGeometry::initialize() { if (hasTexcoords) { attributes.addAttribute(FabricTypes::primvars_st, FabricTokens::primvars_st); + attributes.addAttribute(FabricTypes::primvars_textureIndex, FabricTokens::primvars_textureIndex); } if (hasNormals) { @@ -157,6 +168,7 @@ void FabricGeometry::initialize() { // Initialize primvars size_t primvarsCount = 0; size_t primvarIndexSt = 0; + size_t primvarTextureIndex = 0; size_t primvarIndexNormal = 0; size_t primvarIndexVertexColor = 0; @@ -165,6 +177,7 @@ void FabricGeometry::initialize() { if (hasTexcoords) { primvarIndexSt = primvarsCount++; + primvarTextureIndex = primvarsCount++; } if (hasNormals) { @@ -179,6 +192,7 @@ void FabricGeometry::initialize() { srw.setArrayAttributeSize(_path, FabricTokens::primvarInterpolations, primvarsCount); srw.setArrayAttributeSize(_path, FabricTokens::primvars_displayColor, 1); srw.setArrayAttributeSize(_path, FabricTokens::primvars_displayOpacity, 1); + srw.setArrayAttributeSize(_path, FabricTokens::primvars_textureIndex, 1); // clang-format off auto primvarsFabric = srw.getArrayAttributeWr(_path, FabricTokens::primvars); @@ -194,6 +208,8 @@ void FabricGeometry::initialize() { if (hasTexcoords) { primvarsFabric[primvarIndexSt] = FabricTokens::primvars_st; primvarInterpolationsFabric[primvarIndexSt] = FabricTokens::vertex; + primvarsFabric[primvarTextureIndex] = FabricTokens::primvars_textureIndex; + primvarInterpolationsFabric[primvarTextureIndex] = FabricTokens::constant; } if (hasNormals) { @@ -245,6 +261,7 @@ void FabricGeometry::reset() { if (hasTexcoords) { srw.setArrayAttributeSize(_path, FabricTokens::primvars_st, 0); + srw.setArrayAttributeSize(_path, FabricTokens::primvars_textureIndex, 0); } if (hasNormals) { diff --git a/src/core/src/FabricMaterial.cpp b/src/core/src/FabricMaterial.cpp index f5ccf6cc6..95155b9d0 100644 --- a/src/core/src/FabricMaterial.cpp +++ b/src/core/src/FabricMaterial.cpp @@ -16,11 +16,13 @@ FabricMaterial::FabricMaterial( const omni::fabric::Path& path, const FabricMaterialDefinition& materialDefinition, const pxr::TfToken& defaultTextureAssetPathToken, - long stageId) + long stageId, + bool useSharedMaterialForTexturedMaterials) : _materialPath(path) , _materialDefinition(materialDefinition) , _defaultTextureAssetPathToken(defaultTextureAssetPathToken) - , _stageId(stageId) { + , _stageId(stageId) + , _useSharedMaterialForTexturedMaterials(useSharedMaterialForTexturedMaterials) { if (stageDestroyed()) { return; diff --git a/src/core/src/FabricMaterialPool.cpp b/src/core/src/FabricMaterialPool.cpp index 3d4058b28..0feae144c 100644 --- a/src/core/src/FabricMaterialPool.cpp +++ b/src/core/src/FabricMaterialPool.cpp @@ -25,7 +25,7 @@ const FabricMaterialDefinition& FabricMaterialPool::getMaterialDefinition() cons std::shared_ptr FabricMaterialPool::createObject(uint64_t objectId) { const auto pathStr = fmt::format("/fabric_material_pool_{}_object_{}", _poolId, objectId); const auto path = omni::fabric::Path(pathStr.c_str()); - return std::make_shared(path, _materialDefinition, _defaultTextureAssetPathToken, _stageId); + return std::make_shared(path, _materialDefinition, _defaultTextureAssetPathToken, _stageId, false); } void FabricMaterialPool::setActive(std::shared_ptr material, bool active) { diff --git a/src/core/src/FabricPrepareRenderResources.cpp b/src/core/src/FabricPrepareRenderResources.cpp index 963a46122..a992e0d97 100644 --- a/src/core/src/FabricPrepareRenderResources.cpp +++ b/src/core/src/FabricPrepareRenderResources.cpp @@ -190,8 +190,7 @@ void setFabricMeshes( geometry->setMaterial(material->getPath()); if (baseColorTexture != nullptr && materialInfo.baseColorTexture.has_value()) { - material->setBaseColorTexture( - baseColorTexture->getAssetPathToken(), materialInfo.baseColorTexture.value()); + geometry->setTextureIndex(baseColorTexture->getIndex()); } } else if (!tilesetMaterialPath.IsEmpty()) { geometry->setMaterial(FabricUtil::toFabricPath(tilesetMaterialPath)); diff --git a/src/core/src/FabricResourceManager.cpp b/src/core/src/FabricResourceManager.cpp index a163069fb..e1b1d8ce2 100644 --- a/src/core/src/FabricResourceManager.cpp +++ b/src/core/src/FabricResourceManager.cpp @@ -79,9 +79,9 @@ std::shared_ptr FabricResourceManager::acquireGeometry( return geometry; } -bool useSharedMaterial(const FabricMaterialDefinition& materialDefinition) { +bool useSharedMaterial(const FabricMaterialDefinition& materialDefinition, bool useSharedMaterialForTexturedMaterials) { if (materialDefinition.hasBaseColorTexture()) { - return false; + return useSharedMaterialForTexturedMaterials; } return true; @@ -91,7 +91,8 @@ std::shared_ptr FabricResourceManager::createMaterial(const FabricMaterialDefinition& materialDefinition, long stageId) { const auto pathStr = fmt::format("/fabric_material_{}", getNextMaterialId()); const auto path = omni::fabric::Path(pathStr.c_str()); - return std::make_shared(path, materialDefinition, _defaultTextureAssetPathToken, stageId); + return std::make_shared( + path, materialDefinition, _defaultTextureAssetPathToken, stageId, _useSharedMaterialForTexturedMaterials); } void FabricResourceManager::removeSharedMaterial(const SharedMaterial& sharedMaterial) { @@ -168,7 +169,7 @@ std::shared_ptr FabricResourceManager::acquireMaterial( int64_t tilesetId) { FabricMaterialDefinition materialDefinition(materialInfo, hasImagery, _disableTextures); - if (useSharedMaterial(materialDefinition)) { + if (useSharedMaterial(materialDefinition, _useSharedMaterialForTexturedMaterials)) { return acquireSharedMaterial(materialInfo, materialDefinition, stageId, tilesetId); } @@ -191,8 +192,9 @@ std::shared_ptr FabricResourceManager::acquireMaterial( std::shared_ptr FabricResourceManager::acquireTexture() { if (_disableTexturePool) { - const auto name = fmt::format("/fabric_texture_{}", getNextTextureId()); - return std::make_shared(name); + const auto id = getNextTextureId(); + const auto name = fmt::format("/fabric_texture_{}", id); + return std::make_shared(name, id); } std::scoped_lock lock(_poolMutex); @@ -223,7 +225,7 @@ void FabricResourceManager::releaseGeometry(const std::shared_ptr& material) { const auto& materialDefinition = material->getMaterialDefinition(); - if (useSharedMaterial(materialDefinition)) { + if (useSharedMaterial(materialDefinition, _useSharedMaterialForTexturedMaterials)) { releaseSharedMaterial(material); return; } diff --git a/src/core/src/FabricTexture.cpp b/src/core/src/FabricTexture.cpp index a549b4b61..7a336692d 100644 --- a/src/core/src/FabricTexture.cpp +++ b/src/core/src/FabricTexture.cpp @@ -32,9 +32,10 @@ carb::Format getCompressedImageFormat(CesiumGltf::GpuCompressedPixelFormat pixel } // namespace -FabricTexture::FabricTexture(const std::string& name) +FabricTexture::FabricTexture(const std::string& name, uint64_t index) : _texture(std::make_unique(name)) - , _assetPathToken(UsdUtil::getDynamicTextureProviderAssetPathToken(name)) { + , _assetPathToken(UsdUtil::getDynamicTextureProviderAssetPathToken(name)) + , _index(index) { reset(); } @@ -50,6 +51,10 @@ const pxr::TfToken& FabricTexture::getAssetPathToken() const { return _assetPathToken; } +uint64_t FabricTexture::getIndex() const { + return _index; +} + void FabricTexture::reset() { const auto bytes = std::array{{255, 255, 255, 255}}; const auto size = carb::Uint2{1, 1}; diff --git a/src/core/src/FabricTexturePool.cpp b/src/core/src/FabricTexturePool.cpp index ce195787f..299ff903a 100644 --- a/src/core/src/FabricTexturePool.cpp +++ b/src/core/src/FabricTexturePool.cpp @@ -12,7 +12,7 @@ FabricTexturePool::FabricTexturePool(uint64_t poolId, uint64_t initialCapacity) std::shared_ptr FabricTexturePool::createObject(uint64_t objectId) { const auto name = fmt::format("/fabric_texture_pool_{}_object_{}", _poolId, objectId); - return std::make_shared(name); + return std::make_shared(name, objectId); } void FabricTexturePool::setActive(std::shared_ptr texture, bool active) {