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 b2b1ace
Show file tree
Hide file tree
Showing 13 changed files with 207 additions and 18 deletions.
32 changes: 32 additions & 0 deletions exts/cesium.omniverse/mdl/cesium.mdl
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,35 @@ module [[
// For internal use only. See note in FabricMaterial.cpp
export gltf_texture_lookup_value cesium_texture_lookup(*) [[ anno::hidden() ]] = gltf_texture_lookup();
export material cesium_material(*) [[ anno::hidden() ]] = gltf_material();

export gltf_texture_lookup_value cesium_texture_array_lookup(
uniform texture_2d texture_0,
uniform texture_2d texture_1,
uniform texture_2d texture_2,
float2 min_world = float2(-5000.0, -5000.0),
float2 max_world = float2(5000.0, 5000.0),
up_axis_mode up_axis = Y
)
{
gltf_texture_lookup_value tex_ret;
tex_ret.valid = true;

uniform texture_2d[3] textures;
textures[0] = texture_0;
textures[1] = texture_1;
textures[2] = texture_2;

int texture_index = ::scene::data_lookup_int("textureIndex");

for (int i = 0; i < 3; i++) {
if (i == texture_index) {
tex_ret.value = tex::lookup_float4(
tex: textures[i],
coord: world_coordinate_2d(min_world, max_world, up_axis),
wrap_u: ::tex::wrap_clamp,
wrap_v: ::tex::wrap_clamp);
}
}

return tex_ret;
}
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
14 changes: 13 additions & 1 deletion src/core/include/cesium/omniverse/FabricMaterial.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ class FabricMaterial {
const omni::fabric::Path& path,
const FabricMaterialDefinition& materialDefinition,
const pxr::TfToken& defaultTextureAssetPathToken,
long stageId);
long stageId,
bool useTextureArray,
uint64_t textureArrayLength);
~FabricMaterial();

void setMaterial(int64_t tilesetId, const MaterialInfo& materialInfo);
Expand All @@ -43,13 +45,20 @@ class FabricMaterial {
const omni::fabric::Path& texturePath,
const omni::fabric::Path& shaderPath,
const omni::fabric::Token& shaderInput);
void createTextureArray(
const omni::fabric::Path& texturePath,
const omni::fabric::Path& shaderPath,
const omni::fabric::Token& shaderInput);

void reset();
void setShaderValues(const omni::fabric::Path& shaderPath, const MaterialInfo& materialInfo);
void setTextureValues(
const omni::fabric::Path& texturePath,
const pxr::TfToken& textureAssetPathToken,
const TextureInfo& textureInfo);
void setTextureArrayValues(
const omni::fabric::Path& texturePath,
const std::vector<pxr::TfToken>& textureAssetPathTokens);
void setTilesetId(int64_t tilesetId);
bool stageDestroyed();

Expand All @@ -60,6 +69,9 @@ class FabricMaterial {

omni::fabric::Path _shaderPath;
omni::fabric::Path _baseColorTexturePath;

bool _useTextureArray;
uint64_t _textureArrayLength;
};

} // namespace cesium::omniverse
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 _useTextureArray{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
6 changes: 6 additions & 0 deletions src/core/include/cesium/omniverse/Tokens.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ __pragma(warning(push)) __pragma(warning(disable : 4003))
#define USD_TOKENS \
(baseColorTexture) \
(cesium_material) \
(cesium_texture_array_lookup) \
(cesium_texture_lookup) \
(constant) \
(doubleSided) \
Expand Down Expand Up @@ -60,6 +61,9 @@ __pragma(warning(push)) __pragma(warning(disable : 4003))
((inputs_scale, "inputs:scale")) \
((inputs_tex_coord_index, "inputs:tex_coord_index")) \
((inputs_texture, "inputs:texture")) \
((inputs_texture_0, "inputs:texture_0")) \
((inputs_texture_1, "inputs:texture_1")) \
((inputs_texture_2, "inputs:texture_2")) \
((inputs_vertex_color_name, "inputs:vertex_color_name")) \
((inputs_wrap_s, "inputs:wrap_s")) \
((inputs_wrap_t, "inputs:wrap_t")) \
Expand All @@ -72,6 +76,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 +145,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
116 changes: 113 additions & 3 deletions src/core/src/FabricMaterial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ FabricMaterial::FabricMaterial(
const omni::fabric::Path& path,
const FabricMaterialDefinition& materialDefinition,
const pxr::TfToken& defaultTextureAssetPathToken,
long stageId)
long stageId,
bool useTextureArray,
uint64_t textureArrayLength)
: _materialPath(path)
, _materialDefinition(materialDefinition)
, _defaultTextureAssetPathToken(defaultTextureAssetPathToken)
, _stageId(stageId) {
, _stageId(stageId)
, _useTextureArray(useTextureArray)
, _textureArrayLength(textureArrayLength) {

if (stageDestroyed()) {
return;
Expand Down Expand Up @@ -80,7 +84,23 @@ void FabricMaterial::initialize() {
createShader(shaderPath, materialPath);

if (hasBaseColorTexture) {
createTexture(baseColorTexturePath, shaderPath, FabricTokens::inputs_base_color_texture);
if (_useTextureArray) {
createTexture(baseColorTexturePath, shaderPath, FabricTokens::inputs_base_color_texture);
} else {
createTextureArray(baseColorTexturePath, shaderPath, FabricTokens::inputs_base_color_texture);

// TODO: temp
auto textureAssetPathTokens = std::vector<pxr::TfToken>();
textureAssetPathTokens.reserve(_textureArrayLength);

for (uint64_t i = 0; i < _textureArrayLength; i++) {
const auto name = fmt::format("/fabric_texture_pool_{}_object_{}", 0, i);
auto assetPath = UsdUtil::getDynamicTextureProviderAssetPathToken(name);
textureAssetPathTokens.emplace_back(std::move(assetPath));
}

setTextureArrayValues(baseColorTexturePath, textureAssetPathTokens);
}
}
}

Expand Down Expand Up @@ -224,6 +244,70 @@ void FabricMaterial::createTexture(
srw.createConnection(shaderPath, shaderInput, omni::fabric::Connection{texturePath, FabricTokens::outputs_out});
}

void FabricMaterial::createTextureArray(
const omni::fabric::Path& texturePath,
const omni::fabric::Path& shaderPath,
const omni::fabric::Token& shaderInput) {

const auto inputTextureTokens = std::vector<omni::fabric::Token>{
FabricTokens::inputs_texture_0,
FabricTokens::inputs_texture_1,
FabricTokens::inputs_texture_2,
};

assert(inputTextureTokens.size() == _textureArrayLength);

auto srw = UsdUtil::getFabricStageReaderWriter();

srw.createPrim(texturePath);

FabricAttributesBuilder attributes;

for (uint64_t i = 0; i < _textureArrayLength; i++) {
attributes.addAttribute(FabricTypes::inputs_texture, inputTextureTokens[i]);
}

// clang-format off
attributes.addAttribute(FabricTypes::inputs_excludeFromWhiteMode, FabricTokens::inputs_excludeFromWhiteMode);
attributes.addAttribute(FabricTypes::outputs_out, FabricTokens::outputs_out);
attributes.addAttribute(FabricTypes::info_implementationSource, FabricTokens::info_implementationSource);
attributes.addAttribute(FabricTypes::info_mdl_sourceAsset, FabricTokens::info_mdl_sourceAsset);
attributes.addAttribute(FabricTypes::info_mdl_sourceAsset_subIdentifier, FabricTokens::info_mdl_sourceAsset_subIdentifier);
attributes.addAttribute(FabricTypes::_paramColorSpace, FabricTokens::_paramColorSpace);
attributes.addAttribute(FabricTypes::_sdrMetadata, FabricTokens::_sdrMetadata);
attributes.addAttribute(FabricTypes::Shader, FabricTokens::Shader);
attributes.addAttribute(FabricTypes::_cesium_tilesetId, FabricTokens::_cesium_tilesetId);
// clang-format on

attributes.createAttributes(texturePath);

// _paramColorSpace is an array of pairs: [texture_parameter_token, color_space_enum], [texture_parameter_token, color_space_enum], ...
srw.setArrayAttributeSize(texturePath, FabricTokens::_paramColorSpace, _textureArrayLength * 2);
srw.setArrayAttributeSize(texturePath, FabricTokens::_sdrMetadata, 0);

// clang-format off
auto inputsExcludeFromWhiteModeFabric = srw.getAttributeWr<bool>(texturePath, FabricTokens::inputs_excludeFromWhiteMode);
auto infoImplementationSourceFabric = srw.getAttributeWr<omni::fabric::TokenC>(texturePath, FabricTokens::info_implementationSource);
auto infoMdlSourceAssetFabric = srw.getAttributeWr<omni::fabric::AssetPath>(texturePath, FabricTokens::info_mdl_sourceAsset);
auto infoMdlSourceAssetSubIdentifierFabric = srw.getAttributeWr<omni::fabric::TokenC>(texturePath, FabricTokens::info_mdl_sourceAsset_subIdentifier);
auto paramColorSpaceFabric = srw.getArrayAttributeWr<omni::fabric::TokenC>(texturePath, FabricTokens::_paramColorSpace);
// clang-format on

*inputsExcludeFromWhiteModeFabric = false;
*infoImplementationSourceFabric = FabricTokens::sourceAsset;
infoMdlSourceAssetFabric->assetPath = Context::instance().getCesiumMdlPathToken();
infoMdlSourceAssetFabric->resolvedPath = pxr::TfToken();
*infoMdlSourceAssetSubIdentifierFabric = FabricTokens::cesium_texture_array_lookup;

for (uint64_t i = 0; i < _textureArrayLength; i++) {
paramColorSpaceFabric[i * 2] = inputTextureTokens[i];
paramColorSpaceFabric[i * 2 + 1] = FabricTokens::_auto;
}

// Create connection from shader to texture.
srw.createConnection(shaderPath, shaderInput, omni::fabric::Connection{texturePath, FabricTokens::outputs_out});
}

void FabricMaterial::reset() {
clearMaterial();
clearBaseColorTexture();
Expand All @@ -241,6 +325,8 @@ void FabricMaterial::setMaterial(int64_t tilesetId, const MaterialInfo& material
}

void FabricMaterial::setBaseColorTexture(const pxr::TfToken& textureAssetPathToken, const TextureInfo& textureInfo) {
assert(!_useTextureArray);

if (stageDestroyed()) {
return;
}
Expand All @@ -257,6 +343,8 @@ void FabricMaterial::clearMaterial() {
}

void FabricMaterial::clearBaseColorTexture() {
assert(!_useTextureArray);

setBaseColorTexture(_defaultTextureAssetPathToken, GltfUtil::getDefaultTextureInfo());
}

Expand Down Expand Up @@ -325,6 +413,28 @@ void FabricMaterial::setTextureValues(
*scaleFabric = UsdUtil::glmToUsdVector(glm::fvec2(scale));
}

void FabricMaterial::setTextureArrayValues(
const omni::fabric::Path& texturePath,
const std::vector<pxr::TfToken>& textureAssetPathTokens) {
auto srw = UsdUtil::getFabricStageReaderWriter();

assert(textureAssetPathTokens.size() == _textureArrayLength);

const auto inputTextureTokens = std::vector<omni::fabric::Token>{
FabricTokens::inputs_texture_0,
FabricTokens::inputs_texture_1,
FabricTokens::inputs_texture_2,
};

assert(inputTextureTokens.size() == _textureArrayLength);

for (uint64_t i = 0; i < textureAssetPathTokens.size(); i++) {
auto textureFabric = srw.getAttributeWr<omni::fabric::AssetPath>(texturePath, inputTextureTokens[i]);
textureFabric->assetPath = textureAssetPathTokens[i];
textureFabric->resolvedPath = pxr::TfToken();
}
}

bool FabricMaterial::stageDestroyed() {
// Add this guard to all public member functions, including constructors and destructors. Tile render resources can
// continue to be processed asynchronously even after the tileset and USD stage have been destroyed, so prevent any
Expand Down
3 changes: 2 additions & 1 deletion src/core/src/FabricMaterialPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ const FabricMaterialDefinition& FabricMaterialPool::getMaterialDefinition() cons
std::shared_ptr<FabricMaterial> 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<FabricMaterial>(path, _materialDefinition, _defaultTextureAssetPathToken, _stageId);
return std::make_shared<FabricMaterial>(
path, _materialDefinition, _defaultTextureAssetPathToken, _stageId, false, 0);
}

void FabricMaterialPool::setActive(std::shared_ptr<FabricMaterial> material, bool active) {
Expand Down
3 changes: 1 addition & 2 deletions src/core/src/FabricPrepareRenderResources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Loading

0 comments on commit b2b1ace

Please sign in to comment.