Skip to content

Commit

Permalink
Merge pull request #5612 from GafferHQ/arnoldIntegralTypeFix
Browse files Browse the repository at this point in the history
Arnold Renderer : Fix translation of `uchar` data from USD
  • Loading branch information
johnhaddon authored Jan 5, 2024
2 parents 2e36129 + d7f812e commit 9ca5bcf
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 3 deletions.
5 changes: 5 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ Improvements
- USDLight : Added file browser for `shaping:ies:file` parameter.
- OpenColorIOContext : Added file browser for `config` plug.

Fixes
-----

- Arnold : Fixed translation of USD `uchar` attributes and shader parameters.

API
---

Expand Down
30 changes: 30 additions & 0 deletions python/IECoreArnoldTest/RendererTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1254,6 +1254,36 @@ def testSubdivisionAttributes( self ) :
self.assertEqual( arnold.AiNodeGetStr( node, "subdiv_adaptive_space" ), "raster" )
self.assertEqual( arnold.AiNodeGetBool( node, "subdiv_frustum_ignore" ), True )

def testUCharSubdivIterations( self ) :

r = GafferScene.Private.IECoreScenePreview.Renderer.create(
"Arnold",
GafferScene.Private.IECoreScenePreview.Renderer.RenderType.SceneDescription,
str( self.temporaryDirectory() / "test.ass" )
)

subdivPlane = IECoreScene.MeshPrimitive.createPlane( imath.Box2f( imath.V2f( -1 ), imath.V2f( 1 ) ) )
subdivPlane.interpolation = "catmullClark"

r.object(
"plane",
subdivPlane,
r.attributes(
IECore.CompoundObject( {
"ai:polymesh:subdiv_iterations" : IECore.UCharData( 10 ),
} )
)
)

r.render()
del r

with IECoreArnold.UniverseBlock( writable = True ) as universe :

arnold.AiSceneLoad( universe, str( self.temporaryDirectory() / "test.ass" ), None )
node = arnold.AiNodeGetPtr( arnold.AiNodeLookUpByName( universe, "plane" ), "node" )
self.assertEqual( arnold.AiNodeGetByte( node, "subdiv_iterations" ), 10 )

def testSSSSetNameAttribute( self ) :

r = GafferScene.Private.IECoreScenePreview.Renderer.create(
Expand Down
26 changes: 26 additions & 0 deletions python/IECoreArnoldTest/ShaderNetworkAlgoTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,32 @@ def assertNoiseAndFlatNodes() :
ctypes.addressof( nodes[0].contents )
)

def testUCharParameters( self ) :

for dataType in ( IECore.IntData, IECore.UCharData ) :

with self.subTest( dataType = dataType ) :

network = IECoreScene.ShaderNetwork(
shaders = {
"imageHandle" : IECoreScene.Shader(
"image", "surface",
{
"start_channel" : dataType( 10 ),
}
),
},
output = "imageHandle"
)

with IECoreArnold.UniverseBlock( writable = True ) as universe :

nodes = IECoreArnold.ShaderNetworkAlgo.convert( network, universe, "test" )

self.assertEqual( len( nodes ), 1 )
self.assertEqual( arnold.AiNodeEntryGetName( arnold.AiNodeGetNodeEntry( nodes[0] ) ), "image" )
self.assertEqual( arnold.AiNodeGetByte( nodes[0], "start_channel" ), 10 )

def testBlindData( self ) :

flat = IECoreScene.Shader( "flat" )
Expand Down
6 changes: 5 additions & 1 deletion src/IECoreArnold/ParameterAlgo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ void setParameterInternal( AtNode *node, AtString name, int parameterType, bool
}
break;
case AI_TYPE_BYTE :
if( const IntData *data = dataCast<IntData>( name, value ) )
if( const IntData *data = runTimeCast<const IntData>( value ) )
{
AiNodeSetByte( node, name, data->readable() );
}
else if( const UCharData *data = dataCast<UCharData>( name, value ) )
{
AiNodeSetByte( node, name, data->readable() );
}
Expand Down
35 changes: 33 additions & 2 deletions src/IECoreArnold/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1537,7 +1537,7 @@ class ArnoldAttributes : public IECoreScenePreview::Renderer::AttributesInterfac

PolyMesh( const IECore::CompoundObject *attributes )
{
subdivIterations = attributeValue<int>( g_polyMeshSubdivIterationsAttributeName, attributes, 1 );
subdivIterations = integralAttribute<uint8_t>( g_polyMeshSubdivIterationsAttributeName, attributes, 1 );
subdivAdaptiveError = attributeValue<float>( g_polyMeshSubdivAdaptiveErrorAttributeName, attributes, 0.0f );

const IECore::StringData *subdivAdaptiveMetricData = attribute<IECore::StringData>( g_polyMeshSubdivAdaptiveMetricAttributeName, attributes );
Expand Down Expand Up @@ -1574,7 +1574,7 @@ class ArnoldAttributes : public IECoreScenePreview::Renderer::AttributesInterfac
subdivFrustumIgnore = attributeValue<bool>( g_polyMeshSubdivFrustumIgnoreAttributeName, attributes, false );
}

int subdivIterations;
uint8_t subdivIterations;
float subdivAdaptiveError;
AtString subdivAdaptiveMetric;
AtString subdivAdaptiveSpace;
Expand Down Expand Up @@ -1902,6 +1902,37 @@ class ArnoldAttributes : public IECoreScenePreview::Renderer::AttributesInterfac
return defaultValue;
}

template<typename T>
static T integralAttribute( const IECore::InternedString &name, const IECore::CompoundObject *attributes, T defaultValue )
{
IECore::CompoundObject::ObjectMap::const_iterator it = attributes->members().find( name );
if( it == attributes->members().end() )
{
return defaultValue;
}

if constexpr( !std::is_same_v<T, int> )
{
// Gaffer currently uses IntData for all integral attributes regardless of the
// actual type in Arnold, so first check for that.
/// \todo Produce the expected types in Gaffer.
if( auto intData = IECore::runTimeCast<const IECore::IntData>( it->second.get() ) )
{
return intData->readable();
}
}

// Data coming from USD will use the exact Arnold type - for instance `unsigned char`
// for `subdiv_iterations`.
using DataType = IECore::TypedData<T>;
if( auto sd = reportedCast<const DataType>( it->second.get(), "attribute", name ) )
{
return sd->readable();
};

return defaultValue;
}

static void updateVisibility( unsigned char &visibility, const IECore::InternedString &name, unsigned char rayType, const IECore::CompoundObject *attributes )
{
if( const IECore::BoolData *d = attribute<IECore::BoolData>( name, attributes ) )
Expand Down

0 comments on commit 9ca5bcf

Please sign in to comment.