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

MAYA-109893 Integrate 21.05 UsdPreviewSurface shader into mayaUsd plugin #1416

Merged
merged 6 commits into from
May 20, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
36 changes: 36 additions & 0 deletions cmake/modules/FindMaya.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# MAYA_INCLUDE_DIRS Path to the devkit's include directories
# MAYA_API_VERSION Maya version (6-8 digits)
# MAYA_APP_VERSION Maya app version (4 digits)
# MAYA_LIGHTAPI_VERSION Maya light API version (1 or 2)
#

#=============================================================================
Expand Down Expand Up @@ -297,6 +298,40 @@ find_program(MAYA_PY_EXECUTABLE
"Maya's Python executable path"
)

set(MAYA_LIGHTAPI_VERSION 1)
if(IS_MACOSX)
set(MAYA_DSO_SUFFIX ".dylib")
sef(MAYA_DSO_PREFIX "lib")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Typo? sef -> set

elseif(IS_WINDOWS)
set(MAYA_DSO_SUFFIX ".dll")
sef(MAYA_DSO_PREFIX "")
else(IS_LINUX)
set(MAYA_DSO_SUFFIX ".so")
sef(MAYA_DSO_PREFIX "lib")
endif()
find_file(MAYA_OGSDEVICES_LIBRARY
"${MAYA_DSO_PREFIX}OGSDevices${MAYA_DSO_SUFFIX}"
HINTS
"${MAYA_LIBRARY_DIR}"
"${MAYA_LOCATION}"
PATH_SUFFIXES
lib/
bin/
DOC
"Maya's ${MAYA_LIB} library path"
# NO_CMAKE_SYSTEM_PATH needed to avoid conflicts between
# Maya's Foundation library and OSX's framework.
NO_CMAKE_SYSTEM_PATH
)
message(INFO " Got MAYA_OGSDEVICES_LIBRARY = ${MAYA_OGSDEVICES_LIBRARY}")
if (MAYA_OGSDEVICES_LIBRARY)
file(STRINGS ${MAYA_OGSDEVICES_LIBRARY} HAS_LIGHTAPI_2 REGEX "InitializeLightShader")
if (HAS_LIGHTAPI_2)
set(MAYA_LIGHTAPI_VERSION 2)
endif()
endif()
message(INFO " Got MAYA_LIGHTAPI_VERSION = ${MAYA_LIGHTAPI_VERSION}")

# handle the QUIETLY and REQUIRED arguments and set MAYA_FOUND to TRUE if
# all listed variables are TRUE
include(FindPackageHandleStandardArgs)
Expand All @@ -310,6 +345,7 @@ find_package_handle_standard_args(Maya
MAYA_LIBRARIES
MAYA_API_VERSION
MAYA_APP_VERSION
MAYA_LIGHTAPI_VERSION
VERSION_VAR
MAYA_APP_VERSION
)
7 changes: 7 additions & 0 deletions lib/mayaUsd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ if (MAYA_API_VERSION VERSION_GREATER_EQUAL 20230000 AND UFE_VERSION VERSION_GREA
)
endif()

if (MAYA_LIGHTAPI_VERSION EQUAL 2)
target_compile_definitions(${PROJECT_NAME}
PRIVATE
MAYA_LIGHTAPI_VERSION_2=2
)
endif()

# Some of the UFE classes are exporting STL classes which causes this warning.
if(UFE_FOUND AND MSVC)
target_compile_options(${PROJECT_NAME}
Expand Down
6 changes: 4 additions & 2 deletions lib/mayaUsd/render/vp2ShaderFragments/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ list(APPEND SHADERFRAGMENTS_XMLS
Float4ToFloatX.xml
Float4ToFloatY.xml
Float4ToFloatZ.xml
UsdPreviewSurface.xml
UsdPreviewSurfaceLightAPI1.xml
UsdPreviewSurfaceLightAPI2.xml
lightingContributions.xml
opacityToTransparency.xml
scaledDiffusePassThrough.xml
scaledSpecularPassThrough.xml
usdPreviewSurfaceCombiner.xml
usdPreviewSurfaceLighting.xml
usdPreviewSurfaceLightingAPI1.xml
usdPreviewSurfaceLightingAPI2.xml
# New fragments
BasisCurvesCubicColorDomain.xml
BasisCurvesCubicCPVHull.xml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ limitations under the License.
and combining them with material properties. -->
<fragment_ref name="diffuseColorPassThrough" ref="mayafloat3PassThrough"/>
<fragment_ref name="specularColorPassThrough" ref="mayafloat3PassThrough"/>
<fragment_ref name="usdPreviewSurfaceLighting" ref="usdPreviewSurfaceLighting"/>
<fragment_ref name="usdPreviewSurfaceLightingAPI1" ref="usdPreviewSurfaceLightingAPI1"/>
<fragment_ref name="lightingContributions" ref="lightingContributions"/>
<fragment_ref name="scaledDiffusePassThrough" ref="scaledDiffusePassThrough"/>
<fragment_ref name="scaledSpecularPassThrough" ref="scaledSpecularPassThrough"/>
Expand Down Expand Up @@ -61,15 +61,15 @@ limitations under the License.
<connect from="mayaShaderGeom.mayaShaderGeom" to="mayaShaderGeom_Float4GetZ.input" name="input"/>
<connect from="mayaShaderGeom.mayaShaderGeom" to="mayaShaderGeom_Float4GetW.input" name="input"/>

<connect from="mayaShaderGeom_Float4GetX.output" to="usdPreviewSurfaceLighting.NdotL" name="NdotL"/>
<connect from="mayaShaderGeom_Float4GetY.output" to="usdPreviewSurfaceLighting.NdotV" name="NdotV"/>
<connect from="mayaShaderGeom_Float4GetZ.output" to="usdPreviewSurfaceLighting.NdotH" name="NdotH"/>
<connect from="mayaShaderGeom_Float4GetW.output" to="usdPreviewSurfaceLighting.VdotH" name="VdotH"/>
<connect from="mayaShaderGeom_Float4GetX.output" to="usdPreviewSurfaceLightingAPI1.NdotL" name="NdotL"/>
<connect from="mayaShaderGeom_Float4GetY.output" to="usdPreviewSurfaceLightingAPI1.NdotV" name="NdotV"/>
<connect from="mayaShaderGeom_Float4GetZ.output" to="usdPreviewSurfaceLightingAPI1.NdotH" name="NdotH"/>
<connect from="mayaShaderGeom_Float4GetW.output" to="usdPreviewSurfaceLightingAPI1.VdotH" name="VdotH"/>
<connect from="mayaShaderGeom_Float4GetW.output" to="usdPreviewSurfaceCombiner.VdotH" name="VdotH"/>

<connect from="diffuseColorPassThrough.mayafloat3PassThrough" to="usdPreviewSurfaceLighting.diffuseColor" name="diffuseColor"/>
<connect from="specularColorPassThrough.mayafloat3PassThrough" to="usdPreviewSurfaceLighting.specularColor" name="specularColor"/>
<connect from="usdPreviewSurfaceLighting.usdPreviewSurfaceLighting" to="lightingContributions.lightingContributions" name="lightingContributions"/>
<connect from="diffuseColorPassThrough.mayafloat3PassThrough" to="usdPreviewSurfaceLightingAPI1.diffuseColor" name="diffuseColor"/>
<connect from="specularColorPassThrough.mayafloat3PassThrough" to="usdPreviewSurfaceLightingAPI1.specularColor" name="specularColor"/>
<connect from="usdPreviewSurfaceLightingAPI1.usdPreviewSurfaceLightingAPI1" to="lightingContributions.lightingContributions" name="lightingContributions"/>
<connect from="lightingContributions.lightingContributions" to="scaledDiffusePassThrough.lightingContributions" name="lightingContributions"/>
<connect from="lightingContributions.lightingContributions" to="scaledSpecularPassThrough.lightingContributions" name="lightingContributions"/>
<connect from="scaledDiffusePassThrough.scaledDiffuse" to="maya16LightAccum.scaledDiffuse" name="scaledDiffuse"/>
Expand All @@ -82,23 +82,23 @@ limitations under the License.
<connect from="usdPreviewSurfaceCombiner.usdPreviewSurfaceCombiner" to="mayaComputeSurfaceFinal.input" name="input"/>
<connect from="maya_FogDepthSurface.maya_FogDepthSurface" to="mayaComputeSurfaceFinal.maya_FogDepthSurface" name="maya_FogDepthSurface"/>
<connect from="mayaComputeSurfaceFinal.mayaComputeSurfaceFinal" to="mayaSurfaceShaderOutput.mayaSurfaceShaderOutput" name="mayaSurfaceShaderOutput"/>
<connect from="metallicPassThrough.floatPassThrough" to="usdPreviewSurfaceLighting.metallic" name="metallic"/>
<connect from="metallicPassThrough.floatPassThrough" to="usdPreviewSurfaceLightingAPI1.metallic" name="metallic"/>
<connect from="metallicPassThrough.floatPassThrough" to="usdPreviewSurfaceCombiner.metallic" name="metallic"/>
<connect from="iorPassThrough.floatPassThrough" to="usdPreviewSurfaceLighting.ior" name="ior"/>
<connect from="iorPassThrough.floatPassThrough" to="usdPreviewSurfaceLightingAPI1.ior" name="ior"/>
<connect from="iorPassThrough.floatPassThrough" to="usdPreviewSurfaceCombiner.ior" name="ior"/>
<connect from="useSpecularWorkflowPassThrough.boolPassThrough" to="usdPreviewSurfaceLighting.useSpecularWorkflow" name="useSpecularWorkflow"/>
<connect from="useSpecularWorkflowPassThrough.boolPassThrough" to="usdPreviewSurfaceLightingAPI1.useSpecularWorkflow" name="useSpecularWorkflow"/>
<connect from="useSpecularWorkflowPassThrough.boolPassThrough" to="usdPreviewSurfaceCombiner.useSpecularWorkflow" name="useSpecularWorkflow"/>
</connections>
<properties>
<!-- UsdPreviewSurface Parameters -->
<float name="clearcoat" ref="usdPreviewSurfaceLighting.clearcoatAmount"/>
<float name="clearcoatRoughness" ref="usdPreviewSurfaceLighting.clearcoatRoughness"/>
<float name="clearcoat" ref="usdPreviewSurfaceLightingAPI1.clearcoatAmount"/>
<float name="clearcoatRoughness" ref="usdPreviewSurfaceLightingAPI1.clearcoatRoughness"/>
<float3 name="diffuseColor" ref="diffuseColorPassThrough.input" flags="multiDraw"/>
<float3 name="emissiveColor" ref="usdPreviewSurfaceCombiner.emissiveColor"/>
<float name="ior" ref="iorPassThrough.input"/>
<float name="metallic" ref="metallicPassThrough.input"/>
<float name="occlusion" ref="usdPreviewSurfaceLighting.occlusion"/>
<float name="roughness" ref="usdPreviewSurfaceLighting.specularRoughness"/>
<float name="occlusion" ref="usdPreviewSurfaceLightingAPI1.occlusion"/>
<float name="roughness" ref="usdPreviewSurfaceLightingAPI1.specularRoughness"/>
<float3 name="specularColor" ref="specularColorPassThrough.input"/>
<float name="opacity" ref="opacityToTransparency.opacity"/>
<bool name="useSpecularWorkflow" ref="useSpecularWorkflowPassThrough.input"/>
Expand All @@ -108,8 +108,8 @@ limitations under the License.
<float3 name="Nw" ref="NwFaceCameraIfNAN.Nw" flags="varyingInputParam"/>
<float3 name="Lw" ref="mayaShaderGeom.Lw"/>
<float3 name="HLw" ref="mayaHVector.HLw"/>
<float3 name="diffuseI" ref="usdPreviewSurfaceLighting.diffuseIrradiance"/>
<float3 name="specularI" ref="usdPreviewSurfaceLighting.specularIrradiance"/>
<float3 name="diffuseI" ref="usdPreviewSurfaceLightingAPI1.diffuseIrradiance"/>
<float3 name="specularI" ref="usdPreviewSurfaceLightingAPI1.specularIrradiance"/>
<string name="selector" ref="maya16LightAccum.selector"/>
<bool name="mayaAlphaCut" ref="usdPreviewSurfaceCombiner.mayaAlphaCut" semantic="mayaAlphaCut" flags="isRequirementOnly"/>
<float3 name="ambientIn" ref="usdPreviewSurfaceCombiner.ambientIn"/>
Expand Down
107 changes: 107 additions & 0 deletions lib/mayaUsd/render/vp2ShaderFragments/UsdPreviewSurfaceLightAPI2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<!--
========================================================================
Copyright 2018 Pixar
Copyright 2020 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="UsdPreviewSurface" ref="UsdPreviewSurface" class="FragmentGraph" version="1.0" feature_level="0">
<fragments>
<!-- Fragments for extracting computed dot products. -->
<fragment_ref name="NwFaceCameraIfNAN" ref="NwFaceCameraIfNAN"/>

<!-- Fragments for computing material. -->
<fragment_ref name="opacityToTransparency" ref="opacityToTransparency"/>
<fragment_ref name="usdPreviewSurfaceLightingAPI2" ref="usdPreviewSurfaceLightingAPI2"/>

<!-- Maya fragments for computing the output surface values. -->
<fragment_ref name="maya_FogDepthSurface" ref="maya_FogDepthSurface"/>
<fragment_ref name="mayaComputeSurfaceFinal" ref="mayaComputeSurfaceFinal"/>
<fragment_ref name="mayaSurfaceShaderOutput" ref="mayaSurfaceShaderOutput"/>
</fragments>
<connections>
<connect from="NwFaceCameraIfNAN.output" to="usdPreviewSurfaceLightingAPI2.Nw" name="Nw"/>
<connect from="opacityToTransparency.transparency" to="usdPreviewSurfaceLightingAPI2.transparency" name="transparency"/>
<connect from="usdPreviewSurfaceLightingAPI2.surfaceShader" to="mayaComputeSurfaceFinal.input" name="input"/>
<connect from="maya_FogDepthSurface.maya_FogDepthSurface" to="mayaComputeSurfaceFinal.maya_FogDepthSurface" name="maya_FogDepthSurface"/>
<connect from="mayaComputeSurfaceFinal.mayaComputeSurfaceFinal" to="mayaSurfaceShaderOutput.mayaSurfaceShaderOutput" name="mayaSurfaceShaderOutput"/>
</connections>
<properties>
<!-- UsdPreviewSurface Parameters -->
<float name="clearcoat" ref="usdPreviewSurfaceLightingAPI2.clearcoatAmount"/>
<float name="clearcoatRoughness" ref="usdPreviewSurfaceLightingAPI2.clearcoatRoughness"/>
<float3 name="diffuseColor" ref="usdPreviewSurfaceLightingAPI2.diffuseColor" flags="multiDraw"/>
<float3 name="emissiveColor" ref="usdPreviewSurfaceLightingAPI2.emissiveColor"/>
<float name="ior" ref="usdPreviewSurfaceLightingAPI2.ior"/>
<float name="metallic" ref="usdPreviewSurfaceLightingAPI2.metallic"/>
<float name="occlusion" ref="usdPreviewSurfaceLightingAPI2.occlusion"/>
<float name="roughness" ref="usdPreviewSurfaceLightingAPI2.specularRoughness"/>
<float3 name="specularColor" ref="usdPreviewSurfaceLightingAPI2.specularColor"/>
<float name="opacity" ref="opacityToTransparency.opacity"/>
<bool name="useSpecularWorkflow" ref="usdPreviewSurfaceLightingAPI2.useSpecularWorkflow"/>

<!-- Maya Parameters for Lighting -->
<float3 name="u_viewPosition" semantic="WorldCameraPosition" flags="isRequirementOnly" ref="usdPreviewSurfaceLightingAPI2.u_viewPosition"/>
<int name="mayaExternalLightFunctions" flags="isRequirementOnly" ref="usdPreviewSurfaceLightingAPI2.mayaExternalLightFunctions"/>
<undefined name="GPUStage" ref="mayaComputeSurfaceFinal.GPUStage" semantic="GPUStage"/>
<float3 name="Nw" ref="NwFaceCameraIfNAN.Nw" flags="varyingInputParam"/>
<float3 name="Pw" ref="usdPreviewSurfaceLightingAPI2.Pw" semantic="Pw"/>
<float name="extraOpacity" ref="mayaComputeSurfaceFinal.extraOpacity"/>
<bool name="fogEnabled" ref="mayaComputeSurfaceFinal.fogEnabled"/>
<float4x4 name="ViewProj" ref="maya_FogDepthSurface.ViewProj" semantic="viewprojection"/>
<float name="fogStart" ref="maya_FogDepthSurface.fogStart"/>
<float name="fogEnd" ref="maya_FogDepthSurface.fogEnd"/>
<int name="fogMode" ref="maya_FogDepthSurface.fogMode"/>
<float name="fogDensity" ref="maya_FogDepthSurface.fogDensity"/>
<float4 name="fogColor" ref="mayaComputeSurfaceFinal.fogColor"/>
<float name="fogMultiplier" ref="mayaComputeSurfaceFinal.fogMultiplier"/>

<!-- The parameter is a workaround for VP2 to execute transparency test. For more details
see PxrMayaUsdPreviewSurfaceShadingNodeOverride::getCustomMappings(). -->
<float name="dummyTransparency" ref="opacityToTransparency.dummyTransparency"/>
</properties>
<values>
<!-- UsdPreviewSurface Parameter Default Values -->
<float name="clearcoat" value="0.0"/>
<float name="clearcoatRoughness" value="0.01"/>
<float3 name="diffuseColor" value="0.18,0.18,0.18"/>
<float3 name="emissiveColor" value="0.0,0.0,0.0"/>
<float name="ior" value="1.5"/>
<float name="metallic" value="0.0"/>
<float name="occlusion" value="1.0"/>
<float name="roughness" value="0.5"/>
<float3 name="specularColor" value="0.0,0.0,0.0"/>
<!-- The shader computes transparency from its "opacity" attribute. -->
<float name="opacity" value="1.0"/>
<bool name="useSpecularWorkflow" value="false"/>

<!-- Default values for Maya-provided parameters. -->
<float name="extraOpacity" value="1.0"/>
<bool name="fogEnabled" value="false"/>
<float name="fogStart" value="0.0"/>
<float name="fogEnd" value="92.0"/>
<int name="fogMode" value="0"/>
<float name="fogDensity" value="0.1"/>
<float4 name="fogColor" value="0.5,0.5,0.5,1.0"/>
<float name="fogMultiplier" value="1.0"/>


<!-- The parameter is a workaround for VP2 to execute transparency test. For more details
see PxrMayaUsdPreviewSurfaceShadingNodeOverride::getCustomMappings(). -->
<float name="dummyTransparency" value="0.0" />
</values>
<outputs>
<struct name="mayaSurfaceShaderOutput" ref="mayaSurfaceShaderOutput.mayaSurfaceShaderOutput"/>
</outputs>
</fragment_graph>
53 changes: 44 additions & 9 deletions lib/mayaUsd/render/vp2ShaderFragments/shaderFragments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ TF_DEFINE_PRIVATE_TOKENS(
(scaledDiffusePassThrough)
(scaledSpecularPassThrough)
(opacityToTransparency)
(usdPreviewSurfaceLighting)
(usdPreviewSurfaceLightingAPI1)
(usdPreviewSurfaceLightingAPI2)
(usdPreviewSurfaceCombiner)

(UsdPrimvarColor)
Expand All @@ -80,6 +81,10 @@ TF_DEFINE_PRIVATE_TOKENS(
(UsdPrimvarReader_float3)
(UsdPrimvarReader_float4)
(UsdPrimvarReader_vector)

// Graph:
(UsdPreviewSurfaceLightAPI1)
(UsdPreviewSurfaceLightAPI2)
);
// clang-format on

Expand Down Expand Up @@ -119,17 +124,14 @@ static const TfTokenVector _FragmentNames = { _tokens->BasisCurvesCubicColorDoma
_tokens->scaledDiffusePassThrough,
_tokens->scaledSpecularPassThrough,
_tokens->opacityToTransparency,
_tokens->usdPreviewSurfaceLighting,
_tokens->usdPreviewSurfaceLightingAPI1,
_tokens->usdPreviewSurfaceLightingAPI2,
_tokens->usdPreviewSurfaceCombiner };

static const TfTokenVector _FragmentGraphNames
= { _tokens->BasisCurvesCubicCPVShader,
_tokens->BasisCurvesCubicFallbackShader,
_tokens->BasisCurvesLinearCPVShader,
_tokens->BasisCurvesLinearFallbackShader,
_tokens->FallbackCPVShader,
_tokens->FallbackShader,
HdVP2ShaderFragmentsTokens->SurfaceFragmentGraphName };
= { _tokens->BasisCurvesCubicCPVShader, _tokens->BasisCurvesCubicFallbackShader,
_tokens->BasisCurvesLinearCPVShader, _tokens->BasisCurvesLinearFallbackShader,
_tokens->FallbackCPVShader, _tokens->FallbackShader };

namespace {
//! Get the file path of the shader fragment.
Expand Down Expand Up @@ -300,6 +302,32 @@ MStatus HdVP2ShaderFragments::registerFragments()
}
}

// Register a UsdPreviewSurface shader graph:
{
const MString fragGraphName(HdVP2ShaderFragmentsTokens->SurfaceFragmentGraphName.GetText());
#ifdef MAYA_LIGHTAPI_VERSION_2
const MString fragGraphFileName(_tokens->UsdPreviewSurfaceLightAPI2.GetText());
#else
const MString fragGraphFileName(_tokens->UsdPreviewSurfaceLightAPI1.GetText());
#endif
if (!fragmentManager->hasFragment(fragGraphName)) {
const std::string fragGraphXmlFile
= TfStringPrintf("%s.xml", fragGraphFileName.asChar());
const std::string fragGraphXmlPath = _GetResourcePath(fragGraphXmlFile);

const MString addedName
= fragmentManager->addFragmentGraphFromFile(fragGraphXmlPath.c_str());
if (addedName != fragGraphName) {
MGlobal::displayError(TfStringPrintf(
"Failed to register fragment graph '%s' from file: %s",
fragGraphName.asChar(),
fragGraphXmlPath.c_str())
.c_str());
return MS::kFailure;
}
}
}

#if MAYA_API_VERSION >= 20210000

// Register automatic shader stage input parameters.
Expand Down Expand Up @@ -361,6 +389,13 @@ MStatus HdVP2ShaderFragments::deregisterFragments()

#endif

// De-register UsdPreviewsurface graph:
if (!fragmentManager->removeFragment(
HdVP2ShaderFragmentsTokens->SurfaceFragmentGraphName.GetText())) {
MGlobal::displayWarning("Failed to remove fragment graph: UsdPreviewsurface");
return MS::kFailure;
}

// De-register all fragment graphs.
for (const TfToken& fragGraphNameToken : _FragmentGraphNames) {
const MString fragGraphName(fragGraphNameToken.GetText());
Expand Down
Loading