Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pr/use usd preview surface for cards draw mode #2680

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 38 additions & 17 deletions lib/mayaUsd/render/vp2RenderDelegate/material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,18 +184,14 @@ TF_DEFINE_PRIVATE_TOKENS(
(useSpecularWorkflow)
(st)
(varname)
(result)
(cardsUv)
(sourceColorSpace)
(sRGB)
(raw)
(glslfx)
(fallback)

(input)
(output)

(diffuseColor)
(rgb)
(r)
(g)
Expand All @@ -214,17 +210,25 @@ TF_DEFINE_PRIVATE_TOKENS(
(Float4ToFloatW)
(Float4ToFloat3)

(UsdDrawModeCards)
(FallbackShader)
(mayaIsBackFacing)
(isBackfacing)
((DrawMode, "drawMode.glslfx"))

(UsdPrimvarReader_color)
(UsdPrimvarReader_vector)

(Unknown)
(Computed)

// XXX Deprecated in PXR_VERSION > 2211
(result)
(cardsUv)
(diffuseColor)

(glslfx)

(UsdDrawModeCards)
((DrawMode, "drawMode.glslfx"))

(mayaIsBackFacing)
(isBackfacing)
(FallbackShader)
);
// clang-format on

Expand Down Expand Up @@ -910,13 +914,20 @@ void _AddMissingTangents(mx::DocumentPtr& mtlxDoc)

#endif // WANT_MATERIALX_BUILD

#if PXR_VERSION <= 2211
bool _IsUsdDrawModeId(const TfToken& id)
{
return id == _tokens->DrawMode || id == _tokens->UsdDrawModeCards;
}

bool _IsUsdDrawModeNode(const HdMaterialNode& node) { return _IsUsdDrawModeId(node.identifier); }

bool _IsUsdFloat2PrimvarReader(const HdMaterialNode& node)
{
return (node.identifier == UsdImagingTokens->UsdPrimvarReader_float2);
}
#endif

//! Helper utility function to test whether a node is a UsdShade primvar reader.
bool _IsUsdPrimvarReader(const HdMaterialNode& node)
{
Expand All @@ -929,11 +940,6 @@ bool _IsUsdPrimvarReader(const HdMaterialNode& node)
|| id == UsdImagingTokens->UsdPrimvarReader_int);
}

bool _IsUsdFloat2PrimvarReader(const HdMaterialNode& node)
{
return (node.identifier == UsdImagingTokens->UsdPrimvarReader_float2);
}

//! Helper utility function to test whether a node is a UsdShade UV texture.
bool _IsUsdUVTexture(const HdMaterialNode& node)
{
Expand Down Expand Up @@ -2061,11 +2067,13 @@ void HdVP2Material::CompiledNetwork::_ApplyVP2Fixes(
tmpNet.nodes.reserve(numNodes);
tmpNet.relationships.reserve(numRelationships);

#if PXR_VERSION <= 2211
// Some material networks require us to add nodes and connections to the base
// HdMaterialNetwork. Keep track of the existence of some key nodes to help
// with performance.
HdMaterialNode* usdDrawModeCardsNode = nullptr;
HdMaterialNode* cardsUvPrimvarReader = nullptr;
#endif

// Get the shader registry so I can look up the real names of shading nodes.
SdrRegistry& shaderReg = SdrRegistry::GetInstance();
Expand All @@ -2082,15 +2090,20 @@ void HdVP2Material::CompiledNetwork::_ApplyVP2Fixes(
tmpNet.nodes.push_back(node);

HdMaterialNode& outNode = tmpNet.nodes.back();

#if PXR_VERSION <= 2211
// For card draw mode the HdMaterialNode will have an identifier which is the hash of the
// file path to drawMode.glslfx on disk. Using that value I can get the SdrShaderNode, and
// then get the actual name of the shader "drawMode.glslfx". For other node names the
// HdMaterialNode identifier and the SdrShaderNode name seem to be the same, so just convert
// everything to use the SdrShaderNode name.
SdrShaderNodeConstPtr sdrNode
= shaderReg.GetShaderNodeByIdentifierAndType(outNode.identifier, _tokens->glslfx);

#else
// Ensure that our node identifiers are correct. The HdMaterialNode identifier
// and the SdrShaderNode name seem to be the same in most cases, but we
// convert everything to use the SdrShaderNode name to be sure.
SdrShaderNodeConstPtr sdrNode = shaderReg.GetShaderNodeByIdentifier(outNode.identifier);
#endif
if (_IsUsdUVTexture(node)) {
// We need to rename according to the Maya color working space pref:
if (!mayaWorkingColorSpace.length()) {
Expand All @@ -2108,6 +2121,7 @@ void HdVP2Material::CompiledNetwork::_ApplyVP2Fixes(
outNode.identifier = TfToken(sdrNode->GetName());
}

#if PXR_VERSION <= 2211
if (_IsUsdDrawModeNode(outNode)) {
// I can't easily name a Maya fragment something with a '.' in it, so pick a different
// fragment name.
Expand All @@ -2121,6 +2135,7 @@ void HdVP2Material::CompiledNetwork::_ApplyVP2Fixes(
TF_VERIFY(!cardsUvPrimvarReader);
cardsUvPrimvarReader = &outNode;
}
#endif

outNode.path = SdfPath(outNode.identifier.GetString() + std::to_string(++nodeCounter));

Expand All @@ -2140,6 +2155,7 @@ void HdVP2Material::CompiledNetwork::_ApplyVP2Fixes(
outNet.relationships.reserve(numRelationships * 2);
outNet.primvars.reserve(numNodes);

#if PXR_VERSION <= 2211
// Add additional nodes necessary for Maya's fragment compiler
// to work that are logical predecessors of node.
auto addPredecessorNodes = [&](const HdMaterialNode& node) {
Expand Down Expand Up @@ -2205,10 +2221,12 @@ void HdVP2Material::CompiledNetwork::_ApplyVP2Fixes(
outNet.relationships.push_back(newRel);
}
};
#endif

// Add additional nodes necessary for Maya's fragment compiler
// to work that are logical successors of node.
auto addSuccessorNodes = [&](const HdMaterialNode& node, const TfToken& primvarToRead) {
#if PXR_VERSION <= 2211
// If the node is a DrawModeCardsFragment add the fallback material after it to do
// the lighting etc.
if (_IsUsdDrawModeNode(node)) {
Expand All @@ -2235,6 +2253,7 @@ void HdVP2Material::CompiledNetwork::_ApplyVP2Fixes(
// shader.
return;
}
#endif

// Copy outgoing connections and if needed add passthrough node/connection.
for (const HdMaterialRelationship& rel : tmpNet.relationships) {
Expand Down Expand Up @@ -2289,7 +2308,9 @@ void HdVP2Material::CompiledNetwork::_ApplyVP2Fixes(
}
}

#if PXR_VERSION <= 2211
addPredecessorNodes(node);
#endif
outNet.nodes.push_back(node);

// If the primvar reader is reading color or opacity, replace it with
Expand Down
6 changes: 5 additions & 1 deletion lib/mayaUsd/render/vp2ShaderFragments/shaderFragments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ TF_DEFINE_PRIVATE_TOKENS(
(scaledDiffusePassThrough)
(scaledSpecularPassThrough)
(opacityToTransparency)
(UsdDrawModeCards)
(usdPreviewSurfaceLightingAPI1)
(usdPreviewSurfaceLightingAPI2)
(usdPreviewSurfaceLightingAPI3)
Expand All @@ -104,6 +103,9 @@ TF_DEFINE_PRIVATE_TOKENS(
(UsdPreviewSurfaceLightAPI1)
(UsdPreviewSurfaceLightAPI2)
(UsdPreviewSurfaceLightAPI3)

// XXX Deprecated in PXR_VERSION > 2211
(UsdDrawModeCards)
);
// clang-format on

Expand Down Expand Up @@ -145,7 +147,9 @@ static const TfTokenVector _FragmentNames = { _tokens->BasisCurvesCubicColorDoma
_tokens->scaledDiffusePassThrough,
_tokens->scaledSpecularPassThrough,
_tokens->opacityToTransparency,
#if PXR_VERSION <= 2211
_tokens->UsdDrawModeCards,
#endif
_tokens->usdPreviewSurfaceLightingAPI1,
_tokens->usdPreviewSurfaceLightingAPI2,
_tokens->usdPreviewSurfaceLightingAPI3,
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.