Skip to content

Commit

Permalink
Temp
Browse files Browse the repository at this point in the history
  • Loading branch information
lilleyse committed Oct 4, 2023
1 parent 63a8c6d commit e8cd224
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/core/include/cesium/omniverse/FabricGeometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions src/core/include/cesium/omniverse/FabricResourceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ class FabricResourceManager {

bool _debugRandomColors{false};

bool _useSharedMaterialForTexturedMaterials{true};

std::atomic<uint64_t> _geometryId{0};
std::atomic<uint64_t> _materialId{0};
std::atomic<uint64_t> _textureId{0};
Expand Down
4 changes: 3 additions & 1 deletion src/core/include/cesium/omniverse/FabricTexture.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,21 @@ 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);

void setActive(bool active);

[[nodiscard]] const pxr::TfToken& getAssetPathToken() const;
[[nodiscard]] uint64_t getIndex() const;

private:
void reset();

std::unique_ptr<omni::ui::DynamicTextureProvider> _texture;
pxr::TfToken _assetPathToken;
uint64_t _index;
};
} // namespace cesium::omniverse
2 changes: 2 additions & 0 deletions src/core/include/cesium/omniverse/Tokens.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
17 changes: 17 additions & 0 deletions src/core/src/FabricGeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(_path, FabricTokens::primvars_textureIndex);
textureIndexFabric[0] = static_cast<int>(textureIndex);
}

void FabricGeometry::initialize() {
const auto hasTexcoords = _geometryDefinition.hasTexcoords();
const auto hasNormals = _geometryDefinition.hasNormals();
Expand Down Expand Up @@ -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) {
Expand All @@ -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;

Expand All @@ -165,6 +177,7 @@ void FabricGeometry::initialize() {

if (hasTexcoords) {
primvarIndexSt = primvarsCount++;
primvarTextureIndex = primvarsCount++;
}

if (hasNormals) {
Expand All @@ -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<omni::fabric::TokenC>(_path, FabricTokens::primvars);
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions src/core/src/FabricPrepareRenderResources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
11 changes: 6 additions & 5 deletions src/core/src/FabricResourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ std::shared_ptr<FabricGeometry> 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;
Expand Down Expand Up @@ -168,7 +168,7 @@ std::shared_ptr<FabricMaterial> FabricResourceManager::acquireMaterial(
int64_t tilesetId) {
FabricMaterialDefinition materialDefinition(materialInfo, hasImagery, _disableTextures);

if (useSharedMaterial(materialDefinition)) {
if (useSharedMaterial(materialDefinition, _useSharedMaterialForTexturedMaterials)) {
return acquireSharedMaterial(materialInfo, materialDefinition, stageId, tilesetId);
}

Expand All @@ -191,8 +191,9 @@ std::shared_ptr<FabricMaterial> FabricResourceManager::acquireMaterial(

std::shared_ptr<FabricTexture> FabricResourceManager::acquireTexture() {
if (_disableTexturePool) {
const auto name = fmt::format("/fabric_texture_{}", getNextTextureId());
return std::make_shared<FabricTexture>(name);
const auto id = getNextTextureId();
const auto name = fmt::format("/fabric_texture_{}", id);
return std::make_shared<FabricTexture>(name, id);
}

std::scoped_lock<std::mutex> lock(_poolMutex);
Expand Down
9 changes: 7 additions & 2 deletions src/core/src/FabricTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<omni::ui::DynamicTextureProvider>(name))
, _assetPathToken(UsdUtil::getDynamicTextureProviderAssetPathToken(name)) {
, _assetPathToken(UsdUtil::getDynamicTextureProviderAssetPathToken(name))
, _index(index) {
reset();
}

Expand All @@ -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<uint8_t, 4>{{255, 255, 255, 255}};
const auto size = carb::Uint2{1, 1};
Expand Down
2 changes: 1 addition & 1 deletion src/core/src/FabricTexturePool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ FabricTexturePool::FabricTexturePool(uint64_t poolId, uint64_t initialCapacity)

std::shared_ptr<FabricTexture> FabricTexturePool::createObject(uint64_t objectId) {
const auto name = fmt::format("/fabric_texture_pool_{}_object_{}", _poolId, objectId);
return std::make_shared<FabricTexture>(name);
return std::make_shared<FabricTexture>(name, objectId);
}

void FabricTexturePool::setActive(std::shared_ptr<FabricTexture> texture, bool active) {
Expand Down

0 comments on commit e8cd224

Please sign in to comment.