Skip to content

Commit

Permalink
Get mdl working
Browse files Browse the repository at this point in the history
  • Loading branch information
lilleyse committed Oct 16, 2023
1 parent 72b6b11 commit ef86b82
Show file tree
Hide file tree
Showing 5 changed files with 284 additions and 70 deletions.
62 changes: 61 additions & 1 deletion exts/cesium.omniverse/mdl/cesium.mdl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,66 @@ module [[
anno::display_name("Cesium MDL functions")
]];

// For internal use only. See note in FabricMaterial.cpp
// For internal use only
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 int texture_count = 0,
gltf_texture_lookup_value texture_0 = gltf_texture_lookup(),
gltf_texture_lookup_value texture_1 = gltf_texture_lookup(),
gltf_texture_lookup_value texture_2 = gltf_texture_lookup(),
gltf_texture_lookup_value texture_3 = gltf_texture_lookup(),
gltf_texture_lookup_value texture_4 = gltf_texture_lookup(),
gltf_texture_lookup_value texture_5 = gltf_texture_lookup(),
gltf_texture_lookup_value texture_6 = gltf_texture_lookup(),
gltf_texture_lookup_value texture_7 = gltf_texture_lookup(),
gltf_texture_lookup_value texture_8 = gltf_texture_lookup(),
gltf_texture_lookup_value texture_9 = gltf_texture_lookup(),
gltf_texture_lookup_value texture_10 = gltf_texture_lookup(),
gltf_texture_lookup_value texture_11 = gltf_texture_lookup(),
gltf_texture_lookup_value texture_12 = gltf_texture_lookup(),
gltf_texture_lookup_value texture_13 = gltf_texture_lookup(),
gltf_texture_lookup_value texture_14 = gltf_texture_lookup(),
gltf_texture_lookup_value texture_15 = gltf_texture_lookup()
) [[ anno::hidden() ]]
{
// This should match the value in Tokens.h
const int MAX_TEXTURE_LAYER_COUNT = 16;

gltf_texture_lookup_value[MAX_TEXTURE_LAYER_COUNT] texture_values(
texture_0,
texture_1,
texture_2,
texture_3,
texture_4,
texture_5,
texture_6,
texture_7,
texture_8,
texture_9,
texture_10,
texture_11,
texture_12,
texture_13,
texture_14,
texture_15,
);

float3 final = float3(1.0, 1.0, 1.0);

for (int i = 0; i < texture_count; i++) {
gltf_texture_lookup_value value = texture_values[i];
if (value.valid) {
float3 rgb = float3(value.value[0], value.value[1], value.value[2]);
float alpha = value.value[3];
final = alpha * rgb + (1.0 - alpha) * final;
}
}

gltf_texture_lookup_value tex_ret;
tex_ret.value = float4(final[0], final[1], final[2], 1.0);
tex_ret.valid = true;

return tex_ret;
}
10 changes: 8 additions & 2 deletions src/core/include/cesium/omniverse/FabricMaterial.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ 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,
uint64_t textureCount);
void reset();
void setShaderValues(const omni::fabric::Path& shaderPath, const MaterialInfo& materialInfo);
void setTextureValues(
Expand All @@ -58,6 +62,7 @@ class FabricMaterial {
uint64_t texcoordIndex);
void setTilesetId(int64_t tilesetId);
bool stageDestroyed();
void clearBaseColorTextures();

omni::fabric::Path _materialPath;
const FabricMaterialDefinition _materialDefinition;
Expand All @@ -66,7 +71,8 @@ class FabricMaterial {
const long _stageId;

omni::fabric::Path _shaderPath;
std::vector<omni::fabric::Path> _baseColorTexturePaths;
omni::fabric::Path _baseColorTexturePath;
std::vector<omni::fabric::Path> _baseColorTextureLayerPaths;
};

} // namespace cesium::omniverse
99 changes: 77 additions & 22 deletions src/core/include/cesium/omniverse/Tokens.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,27 @@ __pragma(warning(push)) __pragma(warning(disable : 4003))
// Note: variable names should match the USD token names as closely as possible, with special characters converted to underscores

#define USD_TOKENS \
(baseColorTexture_0) \
(baseColorTexture_1) \
(baseColorTexture_2) \
(baseColorTexture_3) \
(baseColorTexture_4) \
(baseColorTexture_5) \
(baseColorTexture_6) \
(baseColorTexture_7) \
(baseColorTexture_8) \
(baseColorTexture_9) \
(base_color_texture) \
(base_color_texture_0) \
(base_color_texture_1) \
(base_color_texture_2) \
(base_color_texture_3) \
(base_color_texture_4) \
(base_color_texture_5) \
(base_color_texture_6) \
(base_color_texture_7) \
(base_color_texture_8) \
(base_color_texture_9) \
(base_color_texture_10) \
(base_color_texture_11) \
(base_color_texture_12) \
(base_color_texture_13) \
(base_color_texture_14) \
(base_color_texture_15) \
(base_color_texture_array) \
(cesium) \
(cesium_material) \
(cesium_texture_array_lookup) \
(cesium_texture_lookup) \
(constant) \
(doubleSided) \
Expand Down Expand Up @@ -70,6 +79,23 @@ __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_texture_3, "inputs:texture_3")) \
((inputs_texture_4, "inputs:texture_4")) \
((inputs_texture_5, "inputs:texture_5")) \
((inputs_texture_6, "inputs:texture_6")) \
((inputs_texture_7, "inputs:texture_7")) \
((inputs_texture_8, "inputs:texture_8")) \
((inputs_texture_9, "inputs:texture_9")) \
((inputs_texture_10, "inputs:texture_10")) \
((inputs_texture_11, "inputs:texture_11")) \
((inputs_texture_12, "inputs:texture_12")) \
((inputs_texture_13, "inputs:texture_13")) \
((inputs_texture_14, "inputs:texture_14")) \
((inputs_texture_15, "inputs:texture_15")) \
((inputs_texture_count, "inputs:texture_count")) \
((inputs_vertex_color_name, "inputs:vertex_color_name")) \
((inputs_wrap_s, "inputs:wrap_s")) \
((inputs_wrap_t, "inputs:wrap_t")) \
Expand Down Expand Up @@ -123,7 +149,10 @@ __pragma(warning(pop))
namespace cesium::omniverse::FabricTokens {
FABRIC_DECLARE_TOKENS(USD_TOKENS);

const std::array<const omni::fabric::TokenC, 10> primvars_st_n = {{
const uint64_t MAX_PRIMVAR_ST_COUNT = 10;
const uint64_t MAX_TEXTURE_LAYER_COUNT = 16;

const std::array<const omni::fabric::TokenC, MAX_PRIMVAR_ST_COUNT> primvars_st_n = {{
primvars_st_0,
primvars_st_1,
primvars_st_2,
Expand All @@ -136,17 +165,42 @@ const std::array<const omni::fabric::TokenC, 10> primvars_st_n = {{
primvars_st_9,
}};

const std::array<const omni::fabric::TokenC, 10> baseColorTexture_n = {{
baseColorTexture_0,
baseColorTexture_1,
baseColorTexture_2,
baseColorTexture_3,
baseColorTexture_4,
baseColorTexture_5,
baseColorTexture_6,
baseColorTexture_7,
baseColorTexture_8,
baseColorTexture_9,
const std::array<const omni::fabric::TokenC, MAX_TEXTURE_LAYER_COUNT> base_color_texture_n = {{
base_color_texture_0,
base_color_texture_1,
base_color_texture_2,
base_color_texture_3,
base_color_texture_4,
base_color_texture_5,
base_color_texture_6,
base_color_texture_7,
base_color_texture_8,
base_color_texture_9,
base_color_texture_10,
base_color_texture_11,
base_color_texture_12,
base_color_texture_13,
base_color_texture_14,
base_color_texture_15,
}};

const std::array<const omni::fabric::TokenC, MAX_TEXTURE_LAYER_COUNT> inputs_texture_n = {{
inputs_texture_0,
inputs_texture_1,
inputs_texture_2,
inputs_texture_3,
inputs_texture_4,
inputs_texture_5,
inputs_texture_6,
inputs_texture_7,
inputs_texture_8,
inputs_texture_9,
inputs_texture_10,
inputs_texture_11,
inputs_texture_12,
inputs_texture_13,
inputs_texture_14,
inputs_texture_15,
}};

}
Expand All @@ -173,6 +227,7 @@ const omni::fabric::Type inputs_roughness_factor(omni::fabric::BaseDataType::eFl
const omni::fabric::Type inputs_scale(omni::fabric::BaseDataType::eFloat, 2, 0, omni::fabric::AttributeRole::eNone);
const omni::fabric::Type inputs_tex_coord_index(omni::fabric::BaseDataType::eInt, 1, 0, omni::fabric::AttributeRole::eNone);
const omni::fabric::Type inputs_texture(omni::fabric::BaseDataType::eAsset, 1, 0, omni::fabric::AttributeRole::eNone);
const omni::fabric::Type inputs_texture_count(omni::fabric::BaseDataType::eInt, 1, 0, omni::fabric::AttributeRole::eNone);
const omni::fabric::Type inputs_vertex_color_name(omni::fabric::BaseDataType::eUChar, 1, 1, omni::fabric::AttributeRole::eText);
const omni::fabric::Type inputs_wrap_s(omni::fabric::BaseDataType::eInt, 1, 0, omni::fabric::AttributeRole::eNone);
const omni::fabric::Type inputs_wrap_t(omni::fabric::BaseDataType::eInt, 1, 0, omni::fabric::AttributeRole::eNone);
Expand Down
29 changes: 12 additions & 17 deletions src/core/src/FabricGeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,22 @@ const auto DEFAULT_SCALE = pxr::GfVec3f(1.0f, 1.0f, 1.0f);
const auto DEFAULT_MATRIX = pxr::GfMatrix4d(1.0);
const auto DEFAULT_VISIBILITY = false;

const auto MAX_ST_PRIMVAR_COUNT = FabricTokens::primvars_st_n.size();

uint64_t getTexcoordSetCount(const FabricGeometryDefinition& geometryDefinition) {
auto texcoordSetCount = geometryDefinition.getTexcoordSetCount();

if (texcoordSetCount > MAX_ST_PRIMVAR_COUNT) {
if (texcoordSetCount > FabricTokens::MAX_PRIMVAR_ST_COUNT) {
CESIUM_LOG_WARN(
"Number of texcoord sets ({}) exceeds maximum number of texcoord sets ({}). Excess texcoords sets will use "
"texcoord set 0.",
"Number of texcoord sets ({}) exceeds maximum number of texcoord sets ({}). Textures using excessive "
"texcoord sets will be ignored.",
texcoordSetCount,
MAX_ST_PRIMVAR_COUNT,
MAX_ST_PRIMVAR_COUNT);
FabricTokens::MAX_PRIMVAR_ST_COUNT);
}

texcoordSetCount = glm::min(texcoordSetCount, MAX_ST_PRIMVAR_COUNT);
texcoordSetCount = glm::min(texcoordSetCount, FabricTokens::MAX_PRIMVAR_ST_COUNT);

return texcoordSetCount;
}

uint64_t getTexcoordIndex(uint64_t texcoordIndex) {
if (texcoordIndex > MAX_ST_PRIMVAR_COUNT - 1) {
return 0;
}

return texcoordIndex;
}

} // namespace

FabricGeometry::FabricGeometry(
Expand Down Expand Up @@ -427,7 +416,13 @@ void FabricGeometry::setGeometry(
positions.fill(pointsFabric);

const auto fillTexcoords = [this, &srw](uint64_t texcoordIndex, const TexcoordsAccessor& texcoords) {
const auto& primvarStToken = FabricTokens::primvars_st_n[getTexcoordIndex(texcoordIndex)];
assert(texcoordIndex < _geometryDefinition.getTexcoordSetCount());

if (texcoordIndex >= FabricTokens::MAX_PRIMVAR_ST_COUNT) {
return;
}

const auto& primvarStToken = FabricTokens::primvars_st_n[texcoordIndex];
srw.setArrayAttributeSize(_path, primvarStToken, texcoords.size());
auto stFabric = srw.getArrayAttributeWr<glm::fvec2>(_path, primvarStToken);
texcoords.fill(stFabric);
Expand Down
Loading

0 comments on commit ef86b82

Please sign in to comment.