From e8cd2246d6398f281a22f1fc027a3bf820007338 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 + .../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/FabricPrepareRenderResources.cpp | 5 +++-- src/core/src/FabricResourceManager.cpp | 11 ++++++----- src/core/src/FabricTexture.cpp | 9 +++++++-- src/core/src/FabricTexturePool.cpp | 2 +- 9 files changed, 42 insertions(+), 11 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/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/FabricPrepareRenderResources.cpp b/src/core/src/FabricPrepareRenderResources.cpp index 963a46122..f4d398988 100644 --- a/src/core/src/FabricPrepareRenderResources.cpp +++ b/src/core/src/FabricPrepareRenderResources.cpp @@ -190,8 +190,9 @@ void setFabricMeshes( geometry->setMaterial(material->getPath()); if (baseColorTexture != nullptr && materialInfo.baseColorTexture.has_value()) { - material->setBaseColorTexture( - baseColorTexture->getAssetPathToken(), materialInfo.baseColorTexture.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..cf26a1f34 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; @@ -168,7 +168,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 +191,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); 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) {