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

USDShader : Add new node for loading shaders from pxr::SdrRegistry #5384

Merged
merged 3 commits into from
Jul 18, 2023
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
5 changes: 5 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
1.3.x.x (relative to 1.3.0.0)
=======

Features
--------

- USDShader : Added a node for loading shaders from USD's `SdrRegistry`. This includes shaders such as `UsdPreviewSurface` and `UsdUVTexture`, which are now available in the `USD/Shader` section of the node menu.

Fixes
-----

Expand Down
2 changes: 1 addition & 1 deletion SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -1352,7 +1352,7 @@ libraries = {

"GafferUSD" : {
"envAppends" : {
"LIBS" : [ "Gaffer", "GafferDispatch", "GafferScene", "GafferImage", "IECoreScene$CORTEX_LIB_SUFFIX" ] + [ "${USD_LIB_PREFIX}" + x for x in ( [ "sdf", "arch", "tf", "vt" ] if not env["USD_MONOLITHIC"] else [ "usd_ms" ] ) ],
"LIBS" : [ "Gaffer", "GafferDispatch", "GafferScene", "GafferImage", "IECoreScene$CORTEX_LIB_SUFFIX" ] + [ "${USD_LIB_PREFIX}" + x for x in ( [ "sdf", "arch", "tf", "vt", "ndr", "sdr" ] if not env["USD_MONOLITHIC"] else [ "usd_ms" ] ) ],
# USD includes "at least one deprecated or antiquated header", so we
# have to drop our usual strict warning levels.
"CXXFLAGS" : [ "-Wno-deprecated" if env["PLATFORM"] != "win32" else "/wd4996" ],
Expand Down
1 change: 1 addition & 0 deletions include/GafferUSD/TypeIds.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ enum TypeId

USDLayerWriterTypeId = 110225,
USDAttributesTypeId = 110226,
USDShaderTypeId = 110227,

LastTypeId = 110250,

Expand Down
67 changes: 67 additions & 0 deletions include/GafferUSD/USDShader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2023, Cinesite VFX Ltd. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above
// copyright notice, this list of conditions and the following
// disclaimer.
//
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided with
// the distribution.
//
// * Neither the name of John Haddon nor the names of
// any other contributors to this software may be used to endorse or
// promote products derived from this software without specific prior
// written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//////////////////////////////////////////////////////////////////////////

#pragma once

#include "GafferUSD/Export.h"
#include "GafferUSD/TypeIds.h"

#include "GafferScene/Shader.h"

namespace GafferUSD
{

class GAFFERUSD_API USDShader : public GafferScene::Shader
{

public :

explicit USDShader( const std::string &name=defaultName<USDShader>() );
~USDShader() override;

GAFFER_NODE_DECLARE_TYPE( GafferUSD::USDShader, USDShaderTypeId, GafferScene::Shader );

void loadShader( const std::string &shaderName, bool keepExistingValues = false ) override;

protected :

IECore::ConstCompoundObjectPtr attributes( const Gaffer::Plug *output ) const override;

};

IE_CORE_DECLAREPTR( USDShader )

} // namespace GafferUSD
262 changes: 262 additions & 0 deletions python/GafferUSDTest/USDShaderTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
##########################################################################
#
# Copyright (c) 2023, Cinesite VFX Ltd. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
#
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided with
# the distribution.
#
# * Neither the name of Image Engine Design Inc nor the names of
# any other contributors to this software may be used to endorse or
# promote products derived from this software without specific prior
# written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
##########################################################################

import unittest

import imath

import IECore
import IECoreScene

import Gaffer
import GafferUSD
import GafferScene
import GafferSceneTest

class USDShaderTest( GafferSceneTest.SceneTestCase ) :

def __assertShaderLoads( self, name, expectedParameters, expectedOutputs ) :

shader = GafferUSD.USDShader()
shader.loadShader( name )
self.assertEqual( shader["name"].getValue(), name )

for parent, expected in {
shader["parameters"] : expectedParameters,
shader["out"] : expectedOutputs
}.items() :

allNames = set()
for name, plugType, defaultValue in expected :
with self.subTest( name = name ) :
self.assertIn( name, parent )
plug = parent[name]
self.assertIs( type( plug ), plugType )
self.assertEqual( plug.direction(), parent.direction() )
self.assertEqual( plug.getFlags(), Gaffer.Plug.Flags.Default )
if defaultValue is not None :
self.assertEqual( plug.defaultValue(), defaultValue )
else :
self.assertFalse( hasattr( plug, "defaultValue" ) )
allNames.add( name )

## \todo Assert that order is also what we expect, if we can get USD
# to preserve order in the first place.
# See https://github.com/PixarAnimationStudios/OpenUSD/pull/2497.
self.assertEqual( set( parent.keys() ), allNames )

def testLoadUsdPreviewSurface( self ) :

self.__assertShaderLoads(
"UsdPreviewSurface",
[
( "diffuseColor", Gaffer.Color3fPlug, imath.Color3f( 0.18 ) ),
( "emissiveColor", Gaffer.Color3fPlug, imath.Color3f( 0 ) ),
( "useSpecularWorkflow", Gaffer.IntPlug, 0 ),
( "specularColor", Gaffer.Color3fPlug, imath.Color3f( 0 ) ),
( "metallic", Gaffer.FloatPlug, 0 ),
( "roughness", Gaffer.FloatPlug, 0.5 ),
( "clearcoat", Gaffer.FloatPlug, 0 ),
( "clearcoatRoughness", Gaffer.FloatPlug, 0.009999999776482582 ),
( "opacity", Gaffer.FloatPlug, 1 ),
( "opacityThreshold", Gaffer.FloatPlug, 0 ),
( "ior", Gaffer.FloatPlug, 1.5 ),
( "normal", Gaffer.V3fPlug, imath.V3f( 0, 0, 1 ) ),
( "displacement", Gaffer.FloatPlug, 0 ),
( "occlusion", Gaffer.FloatPlug, 1 ),
],
[
( "surface", Gaffer.Plug, None ),
( "displacement", Gaffer.Plug, None ),
]
)

def testLoadUsdPrimvarReader( self ) :

self.__assertShaderLoads(
"UsdPrimvarReader_float",
[
( "varname", Gaffer.StringPlug, "" ),
( "fallback", Gaffer.FloatPlug, 0 ),
],
[
( "result", Gaffer.FloatPlug, 0 ),
]
)

def testLoadUsdUVTexture( self ) :

self.__assertShaderLoads(
"UsdUVTexture",
[
( "file", Gaffer.StringPlug, "" ),
( "st", Gaffer.V2fPlug, imath.V2f( 0 ) ),
( "wrapS", Gaffer.StringPlug, "useMetadata" ),
( "wrapT", Gaffer.StringPlug, "useMetadata" ),
( "fallback", Gaffer.Color4fPlug, imath.Color4f( 0, 0, 0, 1 ) ),
( "scale", Gaffer.Color4fPlug, imath.Color4f( 1 ) ),
( "bias", Gaffer.Color4fPlug, imath.Color4f( 0 ) ),
( "sourceColorSpace", Gaffer.StringPlug, "auto" ),
],
[
( "r", Gaffer.FloatPlug, 0 ),
( "g", Gaffer.FloatPlug, 0 ),
( "b", Gaffer.FloatPlug, 0 ),
( "a", Gaffer.FloatPlug, 0 ),
( "rgb", Gaffer.V3fPlug, imath.V3f( 0 ) ),
]
)

def testKeepExistingValues( self ) :

shader = GafferUSD.USDShader()
shader.loadShader( "UsdPrimvarReader_float" )

shader["parameters"]["fallback"].setValue( 1 )
shader.loadShader( "UsdPrimvarReader_float", keepExistingValues = True )
self.assertEqual( shader["parameters"]["fallback"].getValue(), 1 )

shader.loadShader( "UsdPrimvarReader_float", keepExistingValues = False )
self.assertEqual( shader["parameters"]["fallback"].getValue(), 0 )

def testLoadDifferentShader( self ) :

for keepExistingValues in [ True, False ] :
with self.subTest( keepExistingValues = keepExistingValues ) :

shader = GafferUSD.USDShader()
shader.loadShader( "UsdPreviewSurface" )
self.assertIn( "diffuseColor", shader["parameters"] )

shader.loadShader( "UsdPrimvarReader_float", keepExistingValues = keepExistingValues )
self.assertEqual( set( shader["parameters"].keys() ), { "varname", "fallback" } )

def testMissingShader( self ) :

shader = GafferUSD.USDShader()
with self.assertRaisesRegex( RuntimeError, "Shader \"missing\" not found in SdrRegistry" ) :
shader.loadShader( "missing" )

def testGeometricInterpretation( self ) :

for interpretation in [ "Normal", "Point", "Vector" ] :
with self.subTest( interpretation = interpretation ) :

shader = GafferUSD.USDShader()
shader.loadShader( "UsdPrimvarReader_{}".format( interpretation.lower() ) )

self.assertEqual(
shader["parameters"]["fallback"].interpretation(),
getattr( IECore.GeometricData.Interpretation, interpretation )
)

def testSerialisation( self ) :

script = Gaffer.ScriptNode()
script["shader"] = GafferUSD.USDShader()
script["shader"].loadShader( "UsdPrimvarReader_float" )
script["shader"]["parameters"]["fallback"].setValue( 10 )

script2 = Gaffer.ScriptNode()
script2.execute( script.serialise() )
self.assertEqual( script2["shader"]["name"].getValue(), script["shader"]["name"].getValue() )
self.assertEqual( script2["shader"]["type"].getValue(), script["shader"]["type"].getValue() )
self.assertEqual( script2["shader"]["parameters"].keys(), script["shader"]["parameters"].keys() )
self.assertEqual( script2["shader"]["parameters"]["fallback"].getValue(), script["shader"]["parameters"]["fallback"].getValue() )

def testShaderNetwork( self ) :

uvReader = GafferUSD.USDShader( "uvReader" )
uvReader.loadShader( "UsdPrimvarReader_float2" )

transform2D = GafferUSD.USDShader( "transform2D" )
transform2D.loadShader( "UsdTransform2d" )
transform2D["parameters"]["in"].setInput( uvReader["out"]["result"] )
transform2D["parameters"]["scale"].setValue( imath.V2f( 2, 3 ) )

texture = GafferUSD.USDShader( "texture" )
texture.loadShader( "UsdUVTexture" )
texture["parameters"]["file"].setValue( "test.tx" )
texture["parameters"]["st"].setInput( transform2D["out"]["result"] )

surface = GafferUSD.USDShader( "surface" )
surface.loadShader( "UsdPreviewSurface" )
surface["parameters"]["diffuseColor"].setInput( texture["out"]["rgb"] )

shaderPlug = GafferScene.ShaderPlug()
shaderPlug.setInput( surface["out"]["surface"] )

network = shaderPlug.attributes()["surface"]
self.assertIsInstance( network, IECoreScene.ShaderNetwork )

self.assertEqual( network.getOutput(), ( "surface", "surface" ) )
self.assertEqual(
network.inputConnections( "surface" ),
[ ( ( "texture", "rgb" ), ( "surface", "diffuseColor" ) ) ]
)
self.assertEqual(
network.inputConnections( "texture" ),
[ ( ( "transform2D", "result" ), ( "texture", "st" ) ) ]
)
self.assertEqual(
network.inputConnections( "transform2D" ),
[ ( ( "uvReader", "result" ), ( "transform2D", "in" ) ) ]
)

def testUsdPreviewSurfaceAssignment( self ) :

sphere = GafferScene.Sphere()

shader = GafferUSD.USDShader()
shader.loadShader( "UsdPreviewSurface" )

sphereFilter = GafferScene.PathFilter()
sphereFilter["paths"].setValue( IECore.StringVectorData( [ "/sphere" ] ) )

shaderAssignment = GafferScene.ShaderAssignment()
shaderAssignment["in"].setInput( sphere["out"] )
shaderAssignment["filter"].setInput( sphereFilter["out"] )
shaderAssignment["shader"].setInput( shader["out"]["surface"] )

self.assertEqual( shaderAssignment["out"].attributes( "/sphere" ).keys(), [ "surface" ] )
self.assertIsInstance( shaderAssignment["out"].attributes( "/sphere" )["surface"], IECoreScene.ShaderNetwork )

shaderAssignment["shader"].setInput( shader["out"]["displacement"] )
self.assertEqual( shaderAssignment["out"].attributes( "/sphere" ).keys(), [ "displacement" ] )
self.assertIsInstance( shaderAssignment["out"].attributes( "/sphere" )["displacement"], IECoreScene.ShaderNetwork )

if __name__ == "__main__":
unittest.main()
1 change: 1 addition & 0 deletions python/GafferUSDTest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from .ModuleTest import ModuleTest
from .USDAttributesTest import USDAttributesTest
from .USDLayerWriterTest import USDLayerWriterTest
from .USDShaderTest import USDShaderTest

if __name__ == "__main__":
import unittest
Expand Down
Loading
Loading