Skip to content

Add option to fallback to mayaDefaultStandardSurface shader fragment #3957

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

Merged
merged 7 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions lib/mayaUsd/render/vp2RenderDelegate/material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ TF_DEFINE_PRIVATE_TOKENS(
(Float3ToFloatX)
(Float3ToFloatY)
(Float3ToFloatZ)
(FloatToFloat3)

// When using OCIO from Maya:
(Maya_OCIO_)
Expand Down
22 changes: 17 additions & 5 deletions lib/mayaUsd/render/vp2RenderDelegate/renderDelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ TF_DEFINE_ENV_SETTING(
false,
"This env tells the viewport to only draw glslfx UsdPreviewSurface shading networks.");

TF_DEFINE_ENV_SETTING(
MAYAUSD_VP2_USE_STANDARDSURFACE_FALLBACK,
false,
"This env flag allows setting the common fallback shader to the UsdPreviewSurface shader fragment.");

namespace {

/*! \brief List of supported Rprims by VP2 render delegate
Expand Down Expand Up @@ -101,17 +106,22 @@ enum class FallbackShaderType
constexpr size_t FallbackShaderTypeCount = static_cast<size_t>(FallbackShaderType::kCount);

//! Array of constant-color shader fragment names indexed by FallbackShaderType
const MString _fallbackShaderNames[] = { "FallbackShader",
const MString _fallbackShaderNames[] = { TfGetEnvSetting(MAYAUSD_VP2_USE_STANDARDSURFACE_FALLBACK) ?
"FallbackShaderStandardSurface" : "FallbackShader",
"BasisCurvesLinearFallbackShader",
"BasisCurvesCubicFallbackShader",
"BasisCurvesCubicFallbackShader",
"BasisCurvesCubicFallbackShader",
"PointsFallbackShader" };

//! Array of varying-color shader fragment names indexed by FallbackShaderType
const MString _cpvFallbackShaderNames[]
= { "FallbackCPVShader", "BasisCurvesLinearCPVShader", "BasisCurvesCubicCPVShader",
"BasisCurvesCubicCPVShader", "BasisCurvesCubicCPVShader", "PointsFallbackCPVShader" };
const MString _cpvFallbackShaderNames[] = { TfGetEnvSetting(MAYAUSD_VP2_USE_STANDARDSURFACE_FALLBACK) ?
"FallbackCPVShaderStandardSurface" : "FallbackCPVShader",
"BasisCurvesLinearCPVShader",
"BasisCurvesCubicCPVShader",
"BasisCurvesCubicCPVShader",
"BasisCurvesCubicCPVShader",
"PointsFallbackCPVShader" };

//! "curveBasis" parameter values for three different cubic curves
const std::unordered_map<FallbackShaderType, int> _curveBasisParameterValueMapping
Expand Down Expand Up @@ -184,7 +194,9 @@ class MShaderCache final
return;

_3dDefaultMaterialShader
= shaderMgr->getStockShader(MHWRender::MShaderManager::k3dDefaultMaterialShader);
= TfGetEnvSetting(MAYAUSD_VP2_USE_STANDARDSURFACE_FALLBACK) ?
shaderMgr->getStockShader(MHWRender::MShaderManager::k3dStandardSurfaceShader) :
shaderMgr->getStockShader(MHWRender::MShaderManager::k3dDefaultMaterialShader);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say lets live in the future and not use an environment variable in this situation. There is a MEL call that will tell you what the default shader name is:

    _3dDefaultMaterialShader
        = (MGlobal::executeCommandStringResult("defaultShaderName()") != "lambert1") ?
        shaderMgr->getStockShader(MHWRender::MShaderManager::k3dStandardSurfaceShader) :
        shaderMgr->getStockShader(MHWRender::MShaderManager::k3dDefaultMaterialShader);

If you are curious or want to test that everything still works in a legacy universe, you can either open an old scene that was saved when Lambert was the default, or use the following env var to temporarily restore Lambert as the default: set MAYA_DEFAULT_SURFACE_SHADER=lambert.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh. That brings back bad memories of PR #2049. The number of unit test results to update would be quite important if you modernize the code.

That would be asking a lot, so let's try a progressive solution: introduce an env var, but instead of having it introduce the new behavior, use it to get back to the previous one:

    bool useStandardSurface = (MGlobal::executeCommandStringResult("defaultShaderName()") != "lambert1");
    if (TfGetEnvSetting(MAYAUSD_VP2_USE_LAMBERT_FALLBACK) {
        // Explicit request for legacy Lambert:
        useStandardSurface  = false;
    }
    _3dDefaultMaterialShader
        = useStandardSurface ?
        shaderMgr->getStockShader(MHWRender::MShaderManager::k3dStandardSurfaceShader) :
        shaderMgr->getStockShader(MHWRender::MShaderManager::k3dDefaultMaterialShader);

This way you can add that env var to test\lib\mayaUsd\render\vp2RenderDelegate\CMakeLists.txt to not have to update all results, and remove the env var for unit tests that would shine with your new code.

One unit test I have in mind would be test\lib\mayaUsd\render\vp2RenderDelegate\testVP2RenderDelegateDisplayColors.py with a standardSurface-based scene when mel.eval("defaultShaderName()") != "lambert1". Would require a different set of baseline images in the modern code path. Just add a _standardSurface suffix to them like we do in other modernization situations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a great idea to live in the future and have the env var work the other way around. Thanks for the hint of the mel command to get the default shader name, this is a good way to go.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Changed to MAYAUSD_VP2_USE_LAMBERT_FALLBACK and comparison of the default shader name.
  • Updated tests cmake file to use this environment for legacy tests.


TF_VERIFY(_3dDefaultMaterialShader);

Expand Down
3 changes: 3 additions & 0 deletions lib/mayaUsd/render/vp2ShaderFragments/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ list(APPEND SHADERFRAGMENTS_XMLS
Float3ToFloatX.xml
Float3ToFloatY.xml
Float3ToFloatZ.xml
FloatToFloat3.xml
UsdPreviewSurfaceLightAPI1.xml
UsdPreviewSurfaceLightAPI2.xml
UsdPreviewSurfaceLightAPI3.xml
Expand Down Expand Up @@ -49,7 +50,9 @@ list(APPEND SHADERFRAGMENTS_XMLS
BasisCurvesLinearFallbackShader.xml
BasisCurvesLinearHull.xml
FallbackCPVShader.xml
FallbackCPVShaderStandardSurface.xml
FallbackShader.xml
FallbackShaderStandardSurface.xml
Float4ToFloat3.xml
Float4ToFloat4.xml
NwFaceCameraIfNAN.xml
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<!--
========================================================================
Copyright 2019 Autodesk

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
========================================================================
-->
<fragment_graph name="FallbackCPVShaderStandardSurface" ref="FallbackCPVShaderStandardSurface" class="FragmentGraph" version="1.0" feature_level="0" >
<fragments>
<fragment_ref name="mayaDefaultStandardSurface" ref="mayaDefaultStandardSurface" />
<fragment_ref name="FloatToFloat3" ref="FloatToFloat3" />
<fragment_ref name="Float4ToFloat3" ref="Float4ToFloat3" />
<fragment_ref name="Float4ToFloatW" ref="Float4ToFloatW" />
<fragment_ref name="mayaCPVPassing" ref="mayaCPVPassing" />
</fragments>
<connections>
<connect from="Float4ToFloat3.output" to="mayaDefaultStandardSurface.baseColor" name="color" />
<connect from="FloatToFloat3.output" to="mayaDefaultStandardSurface.opacity" name="opacity" />
<connect from="Float4ToFloatW.output" to="FloatToFloat3.input" name="alphavec" />
<connect from="mayaCPVPassing.C_4F" to="Float4ToFloat3.input" name="rgb" />
<connect from="mayaCPVPassing.C_4F" to="Float4ToFloatW.input" name="a" />
</connections>
<properties>
<float3 name="mayaTangentIn" ref="mayaDefaultStandardSurface.mayaTangentIn" semantic="tangent" flags="varyingInputParam" />
<float name="base" ref="mayaDefaultStandardSurface.base" />
<float name="diffuseRoughness" ref="mayaDefaultStandardSurface.diffuseRoughness" />
<float name="metalness" ref="mayaDefaultStandardSurface.metalness" />
<float name="specular" ref="mayaDefaultStandardSurface.specular" />
<float3 name="specularColor" ref="mayaDefaultStandardSurface.specularColor" />
<float name="specularIOR" ref="mayaDefaultStandardSurface.specularIOR" />
<float name="specularRoughness" ref="mayaDefaultStandardSurface.specularRoughness" />
<float name="specularAnisotropy" ref="mayaDefaultStandardSurface.specularAnisotropy" />
<float name="specularRotation" ref="mayaDefaultStandardSurface.specularRotation" />
<float name="transmission" ref="mayaDefaultStandardSurface.transmission" />
<float3 name="transmissionColor" ref="mayaDefaultStandardSurface.transmissionColor" />
<float name="subsurface" ref="mayaDefaultStandardSurface.subsurface" />
<float3 name="subsurfaceColor" ref="mayaDefaultStandardSurface.subsurfaceColor" />
<float name="coat" ref="mayaDefaultStandardSurface.coat" />
<float3 name="coatColor" ref="mayaDefaultStandardSurface.coatColor" />
<float name="coatIOR" ref="mayaDefaultStandardSurface.coatIOR" />
<float name="coatRoughness" ref="mayaDefaultStandardSurface.coatRoughness" />
<float name="coatAnisotropy" ref="mayaDefaultStandardSurface.coatAnisotropy" />
<float name="coatRotation" ref="mayaDefaultStandardSurface.coatRotation" />
<float name="coatAffectRoughness" ref="mayaDefaultStandardSurface.coatAffectRoughness" />
<float name="coatAffectColor" ref="mayaDefaultStandardSurface.coatAffectColor" />
<float name="sheen" ref="mayaDefaultStandardSurface.sheen" />
<float3 name="sheenColor" ref="mayaDefaultStandardSurface.sheenColor" />
<float name="sheenRoughness" ref="mayaDefaultStandardSurface.sheenRoughness" />
<float name="emission" ref="mayaDefaultStandardSurface.emission" />
<float3 name="emissionColor" ref="mayaDefaultStandardSurface.emissionColor" />
<float name="extraOpacity" ref="mayaDefaultStandardSurface.extraOpacity" />
<bool name="fogEnabled" ref="mayaDefaultStandardSurface.fogEnabled" />
<float3 name="Pw" ref="mayaDefaultStandardSurface.Pw" semantic="Pw" />
<float4x4 name="ViewProj" ref="mayaDefaultStandardSurface.ViewProj" semantic="viewprojection" />
<float name="fogStart" ref="mayaDefaultStandardSurface.fogStart" />
<float name="fogEnd" ref="mayaDefaultStandardSurface.fogEnd" />
<int name="fogMode" ref="mayaDefaultStandardSurface.fogMode" />
<float name="fogDensity" ref="mayaDefaultStandardSurface.fogDensity" />
<float4 name="fogColor" ref="mayaDefaultStandardSurface.fogColor" />
<float name="fogMultiplier" ref="mayaDefaultStandardSurface.fogMultiplier" />
<float3 name="IrradianceEnv" ref="mayaDefaultStandardSurface.IrradianceEnv" />
<float3 name="SpecularEnv" ref="mayaDefaultStandardSurface.SpecularEnv" />
<int name="selectionIndex" ref="mayaDefaultStandardSurface.selectionIndex" />
<struct name="light0" ref="mayaDefaultStandardSurface.light0" />
<struct name="light1" ref="mayaDefaultStandardSurface.light1" />
<struct name="light2" ref="mayaDefaultStandardSurface.light2" />
<struct name="light3" ref="mayaDefaultStandardSurface.light3" />
<struct name="light4" ref="mayaDefaultStandardSurface.light4" />
<struct name="light5" ref="mayaDefaultStandardSurface.light5" />
<struct name="light6" ref="mayaDefaultStandardSurface.light6" />
<struct name="light7" ref="mayaDefaultStandardSurface.light7" />
<struct name="light8" ref="mayaDefaultStandardSurface.light8" />
<struct name="light9" ref="mayaDefaultStandardSurface.light9" />
<struct name="light10" ref="mayaDefaultStandardSurface.light10" />
<struct name="light11" ref="mayaDefaultStandardSurface.light11" />
<struct name="light12" ref="mayaDefaultStandardSurface.light12" />
<struct name="light13" ref="mayaDefaultStandardSurface.light13" />
<struct name="light14" ref="mayaDefaultStandardSurface.light14" />
<struct name="light15" ref="mayaDefaultStandardSurface.light15" />
<float3 name="Nw" ref="mayaDefaultStandardSurface.N" flags="varyingInputParam" />
<float3 name="Vw" ref="mayaDefaultStandardSurface.V" flags="varyingInputParam" />
<string name="selector" ref="mayaDefaultStandardSurface.selector" />
<float3 name="ambientColor" ref="mayaDefaultStandardSurface.ambientColor" />
<float3 name="ambientIn" ref="mayaDefaultStandardSurface.input" />
<undefined name="GPUStage" ref="mayaDefaultStandardSurface.GPUStage" semantic="GPUStage" />
<float4 name="diffuseColor" ref="mayaCPVPassing.colorIn" flags="varyingInputParam" />
</properties>
<values>
<float name="base" value="1.000000" />
<float name="specular" value="1.000000" />
<float3 name="specularColor" value="1.000000,1.000000,1.000000" />
<float name="specularIOR" value="1.500000" />
<float name="specularRoughness" value="0.500000" />
<float3 name="transmissionColor" value="1.000000,1.000000,1.000000" />
<float3 name="subsurfaceColor" value="1.000000,1.000000,1.000000" />
<float3 name="coatColor" value="1.000000,1.000000,1.000000" />
<float name="coatIOR" value="1.500000" />
<float name="coatRoughness" value="0.100000" />
<float3 name="sheenColor" value="1.000000,1.000000,1.000000" />
<float name="sheenRoughness" value="0.300000" />
<float3 name="emissionColor" value="1.000000,1.000000,1.000000" />
<float name="extraOpacity" value="1.000000" />
<bool name="fogEnabled" value="false" />
<float name="fogStart" value="0.000000" />
<float name="fogEnd" value="92.000000" />
<int name="fogMode" value="0" />
<float name="fogDensity" value="0.100000" />
<float4 name="fogColor" value="0.500000,0.500000,0.500000,1.000000" />
<float name="fogMultiplier" value="1.000000" />
<float3 name="IrradianceEnv" value="0.000000,0.000000,0.000000" />
<float3 name="SpecularEnv" value="0.000000,0.000000,0.000000" />
<int name="selectionIndex" value="0" />
<string name="selector" value="mayaLightSelector16" />
<float3 name="ambientColor" value="0.000000,0.000000,0.000000" />
<float4 name="diffuseColor" value="0.18,0.18,0.18,1.0" />
</values>
<outputs>
<float4 name="outSurfaceFinal" ref="mayaDefaultStandardSurface.outSurfaceFinal" />
</outputs>
</fragment_graph>
Loading