Skip to content

Commit

Permalink
Cycles : Fix shader float->color.[rgb] connections
Browse files Browse the repository at this point in the history
We do this by registering adaptors with ShaderNetworkAlgo and then letting `addComponentConnectionAdapters()` do all the hard work for us. This also means that we are inserting the adaptors appropriately when exporting Cycles shaders to USD via the SceneWriter.

Also replaced the old handling for `color.[rgb]->float` connections with the ShaderNetworkAlgo approach. This reduces code complexity, and also means we reuse the adaptors when one source is connected to multiple destinations.

Fixes GafferHQ#5553
  • Loading branch information
johnhaddon committed Oct 24, 2024
1 parent 997a938 commit 892f719
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 45 deletions.
2 changes: 2 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ Improvements
- PythonEditor, PythonCommand, Expression, UIEditor, OSLCode : Added line numbers to code editors (#6091).
- CyclesOptions : Added `denoiseDevice` plug for configuring the device used for denoising.
- AttributeTweaks : Added tooltips and presets for all attribute values.
- SceneWriter : Improved emulation of component-level shader connections when exporting Cycles shaders to USD. Native adaptor shaders are now used instead of OSL shaders that may not be available in the destination DCC.

Fixes
-----

- Expression, OSLCode : Fixed line numbers reported in OSL parse errors.
- PathColumn : Fixed display of swatches for cells containing `Color4fData`.
- Arnold : Fixed "Flush Cache" menu items to work with renders being performed by an InteractiveRender node (rather than an InteractiveArnoldRender node).
- Cycles : Fixed rendering of shaders with connections to individual `rgb` components of a colour or `xyz` components of a vector (#5553).

API
---
Expand Down
3 changes: 3 additions & 0 deletions SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -1382,6 +1382,9 @@ libraries = {

},

# Installs `startup/IECoreScene`.
"IECoreScene" : {},

}

libraries["scripts"]["additionalFiles"].append( "bin/gaffer.cmd" if env["PLATFORM"] == "win32" else "bin/gaffer" )
Expand Down
33 changes: 33 additions & 0 deletions python/GafferCyclesTest/IECoreCyclesPreviewTest/RendererTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2039,6 +2039,39 @@ def testColorArrayParameters( self ) :
]
)

def testComponentConnections( self ) :

shader = IECoreScene.ShaderNetwork(
shaders = {
"input" : IECoreScene.Shader(
"convert_point_to_color", "cycles:shader",
{
"value_point" : imath.V3f( 1, 0.5, 0.25 ),
}
),
"output" : IECoreScene.Shader(
"emission", "cycles:surface",
{
"color" : imath.Color3f( 0 ),
"strength" : 1.0,
}
),
},
connections = [
( ( "input", "value_color.r" ), ( "output", "color.g" ) ),
( ( "input", "value_color.g" ), ( "output", "color.b" ) ),
( ( "input", "value_color.b" ), ( "output", "color.r" ) ),
],
output = "output",
)

self.__testShaderResults(
shader,
[
( imath.V2f( 0.5, 0.5 ), imath.Color4f( 0.25, 1, 0.5, 1 ) ),
]
)

def testInvalidShaderParameterValues( self ) :

renderer = GafferScene.Private.IECoreScenePreview.Renderer.create( "Cycles" )
Expand Down
48 changes: 3 additions & 45 deletions src/GafferCycles/IECoreCyclesPreview/ShaderNetworkAlgo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,51 +269,6 @@ ccl::ShaderNode *convertWalk( const ShaderNetwork::Parameter &outputParameter, c

InternedString sourceName = connection.source.name;

// Need to create converters if only one of a color or vector's components is connected
std::vector<std::string> splitName;
boost::split( splitName, sourceName.string(), boost::is_any_of( "." ) );
if( splitName.size() > 1 )
{
ccl::ShaderNode *snode;
std::string baseSourceName = splitName.front();
std::string component = splitName.back();
std::string input;
if( ( component == "r" ) || ( component == "g" ) || ( component == "b" ) )
{
input = "color";
ccl::SeparateRGBNode *separateRGBNode = shaderGraph->create_node<ccl::SeparateRGBNode>();
snode = (ccl::ShaderNode*)separateRGBNode;
snode = shaderGraph->add( snode );
}
else if( ( component == "x" ) || ( component == "y" ) || ( component == "z" ) )
{
input = "vector";
ccl::SeparateXYZNode *separateXYZNode = shaderGraph->create_node<ccl::SeparateXYZNode>();
snode = (ccl::ShaderNode*)separateXYZNode;
snode = shaderGraph->add( snode );
}
else
{
continue;
}

if( ccl::ShaderOutput *shaderOutput = IECoreCycles::ShaderNetworkAlgo::output( sourceNode, baseSourceName ) )
{
if( ccl::ShaderInput *shaderSepInput = IECoreCycles::ShaderNetworkAlgo::input( snode, input ) )
{
shaderGraph->connect( shaderOutput, shaderSepInput );
if( ccl::ShaderOutput *shaderSepOutput = IECoreCycles::ShaderNetworkAlgo::output( snode, component ) )
{
if( ccl::ShaderInput *shaderInput = IECoreCycles::ShaderNetworkAlgo::input( node, parameterName ) )
{
shaderGraph->connect( shaderSepOutput, shaderInput );
}
}
}
}
continue;
}

if( ccl::ShaderOutput *shaderOutput = IECoreCycles::ShaderNetworkAlgo::output( sourceNode, sourceName ) )
{
if( ccl::ShaderInput *shaderInput = IECoreCycles::ShaderNetworkAlgo::input( node, parameterName ) )
Expand Down Expand Up @@ -512,6 +467,9 @@ ccl::ShaderGraph *convertGraph( const IECoreScene::ShaderNetwork *surfaceShader,
/// Hardcoded to the old OSL version to indicate that component connection adapters are
/// required - even though OSL now supports component connections, the Cycles API AFAIK doesn't.
IECoreScene::ShaderNetworkAlgo::convertToOSLConventions( toConvert.get(), 10900 );
// The above only added component connection adaptors for OSL. Now add them for native
// Cycles shaders.
IECoreScene::ShaderNetworkAlgo::addComponentConnectionAdapters( toConvert.get() );
IECoreCycles::ShaderNetworkAlgo::convertUSDShaders( toConvert.get() );
ShaderMap converted;
ccl::ShaderNode *node = convertWalk( toConvert->getOutput(), toConvert.get(), namePrefix, shaderManager, graph, converted );
Expand Down
65 changes: 65 additions & 0 deletions startup/IECoreScene/componentConnectionAdaptors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
##########################################################################
#
# Copyright (c) 2024, 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.
#
##########################################################################

import IECore
import IECoreScene

# This file registers adaptors for connections between scalars and the child
# components of vector and color parameters in shaders. These adaptors are
# necessary for exporting to USD, where component-level connections are not
# supported. They may also be necessary for rendering in certain renderers
# where component-level connections are not supported.

# Cycles
# ======

for c in "rgb" :
IECoreScene.ShaderNetworkAlgo.registerSplitAdapter(
"cycles", c, IECoreScene.Shader( "separate_rgb", "cycles:shader" ), "color", c
)

for c in "xyz" :
IECoreScene.ShaderNetworkAlgo.registerSplitAdapter(
"cycles", c, IECoreScene.Shader( "separate_xyz", "cycles:shader" ), "vector", c
)

IECoreScene.ShaderNetworkAlgo.registerJoinAdapter(
"cycles", IECore.Color3fData, IECoreScene.Shader( "combine_rgb", "cycles:shader" ), ( "r", "g", "b" ), "image"
)

IECoreScene.ShaderNetworkAlgo.registerJoinAdapter(
"cycles", IECore.V3fData, IECoreScene.Shader( "combine_xyz", "cycles:shader" ), ( "x", "y", "z" ), "vector"
)

0 comments on commit 892f719

Please sign in to comment.