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 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
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
66 changes: 51 additions & 15 deletions lib/mayaUsd/render/vp2RenderDelegate/renderDelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@
#include <pxr/imaging/hd/rprim.h>
#include <pxr/imaging/hd/tokens.h>

#include <maya/MGlobal.h>
#include <maya/MProfiler.h>
#include <maya/MString.h>
#include <maya/MStringArray.h>

#include <tbb/spin_rw_mutex.h>

Expand All @@ -49,6 +52,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_LAMBERT_FALLBACK,
false,
"This env flag allows forcing the fallback shaders to the legacy lambert shader fragments.");

namespace {

/*! \brief List of supported Rprims by VP2 render delegate
Expand Down Expand Up @@ -85,6 +93,18 @@ const MString kPointSizeParameterName = "pointSize"; //!< Shader parameter
const MString kCurveBasisParameterName = "curveBasis"; //!< Shader parameter name
const MString kStructOutputName = "outSurfaceFinal"; //!< Output struct name of the fallback shader

//! Returns a boolean of whether or not we want the standardSurface shader fragment graph fallbacks
bool WantStandardSurfaceFallback()
{
bool useStandardSurface
= (MGlobal::executeCommandStringResult("defaultShaderName()") != "lambert1");
if (TfGetEnvSetting(MAYAUSD_VP2_USE_LAMBERT_FALLBACK)) {
// Explicit request for legacy Lambert:
useStandardSurface = false;
}
return useStandardSurface;
}

//! Enum class for fallback shader types
enum class FallbackShaderType
{
Expand All @@ -100,19 +120,6 @@ enum class FallbackShaderType
//! Total number of fallback shader types
constexpr size_t FallbackShaderTypeCount = static_cast<size_t>(FallbackShaderType::kCount);

//! Array of constant-color shader fragment names indexed by FallbackShaderType
const MString _fallbackShaderNames[] = { "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" };

//! "curveBasis" parameter values for three different cubic curves
const std::unordered_map<FallbackShaderType, int> _curveBasisParameterValueMapping
= { { FallbackShaderType::kBasisCurvesCubicBezier, 0 },
Expand Down Expand Up @@ -177,14 +184,33 @@ class MShaderCache final
if (_isInitialized)
return;

_useStandardSurface = WantStandardSurfaceFallback();

_fallbackShaderNames.append(
_useStandardSurface ? "FallbackShaderStandardSurface" : "FallbackShader");
_fallbackShaderNames.append("BasisCurvesLinearFallbackShader");
_fallbackShaderNames.append("BasisCurvesCubicFallbackShader");
_fallbackShaderNames.append("BasisCurvesCubicFallbackShader");
_fallbackShaderNames.append("BasisCurvesCubicFallbackShader");
_fallbackShaderNames.append("PointsFallbackShader");

_cpvFallbackShaderNames.append(
_useStandardSurface ? "FallbackCPVShaderStandardSurface" : "FallbackCPVShader");
_cpvFallbackShaderNames.append("BasisCurvesLinearCPVShader");
_cpvFallbackShaderNames.append("BasisCurvesCubicCPVShader");
_cpvFallbackShaderNames.append("BasisCurvesCubicCPVShader");
_cpvFallbackShaderNames.append("BasisCurvesCubicCPVShader");
_cpvFallbackShaderNames.append("PointsFallbackCPVShader");

MHWRender::MRenderer* renderer = MHWRender::MRenderer::theRenderer();
const MHWRender::MShaderManager* shaderMgr
= renderer ? renderer->getShaderManager() : nullptr;
if (!TF_VERIFY(shaderMgr))
return;

_3dDefaultMaterialShader
= shaderMgr->getStockShader(MHWRender::MShaderManager::k3dDefaultMaterialShader);
_3dDefaultMaterialShader = _useStandardSurface
? shaderMgr->getStockShader(MHWRender::MShaderManager::k3dStandardSurfaceShader)
: shaderMgr->getStockShader(MHWRender::MShaderManager::k3dDefaultMaterialShader);

TF_VERIFY(_3dDefaultMaterialShader);

Expand Down Expand Up @@ -518,6 +544,16 @@ class MShaderCache final
private:
bool _isInitialized { false }; //!< Whether the shader cache is initialized

//! Boolean of whether or not to use shader fragments using the default
//! standardSurface material.
bool _useStandardSurface { false };

//! Array of constant-color shader fragment names indexed by FallbackShaderType
MStringArray _fallbackShaderNames;

//! Array of varying-color shader fragment names indexed by FallbackShaderType
MStringArray _cpvFallbackShaderNames;

//! Shader registry used by fallback shaders
MShaderMap _fallbackShaders[FallbackShaderTypeCount];
MShaderMap _3dSolidShaders;
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 2024 Atomic Cartoons Inc.

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