Skip to content

Commit

Permalink
OSL ShadingEngine : add 'shading:index' special attr to getattribute
Browse files Browse the repository at this point in the history
  • Loading branch information
donboie committed May 5, 2017
1 parent b8341fb commit 6f6b4e4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
9 changes: 9 additions & 0 deletions python/GafferOSLTest/ShadingEngineTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,15 @@ def testUserDataViaGetAttribute( self ) :
for i, c in enumerate( p["Ci"] ) :
self.assertEqual( c, rp["colorUserData"][i] )


e = GafferOSL.ShadingEngine( IECore.ObjectVector( [
IECore.Shader( shader, "surface", { "name" : "shading:index" } ),
] ) )
p = e.shade( rp )

for i, c in enumerate( p["Ci"] ) :
self.assertEqual( c, IECore.Color3f(i) )

def testStructs( self ) :

shader = self.compileShader( os.path.dirname( __file__ ) + "/shaders/structs.osl" )
Expand Down
20 changes: 20 additions & 0 deletions src/GafferOSL/ShadingEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
//
//////////////////////////////////////////////////////////////////////////

#include <limits>

#include "tbb/spin_mutex.h"

#include "boost/algorithm/string/split.hpp"
Expand Down Expand Up @@ -154,6 +156,8 @@ DataPtr dataFromTypeDesc( TypeDesc type, void *&basePointer )
namespace
{

OIIO::ustring gIndex( "shading:index" );

class RenderState
{

Expand Down Expand Up @@ -191,6 +195,22 @@ class RenderState

bool userData( ustring name, TypeDesc type, void *value ) const
{
if( name == gIndex )
{
// if a 4 byte type has been requested then ensure we fit and cast to narrower type
// this way f32 reads of shading:index will succeed.
if( type.size() == sizeof( int ) && m_pointIndex <= ( (size_t) std::numeric_limits<int>::max() ) )
{
int v = ( int ) m_pointIndex;
return ShadingSystem::convert_value( value, type, &v, OIIO::TypeDesc( OIIO::TypeDesc::INT32 ) );
}
else
{
// OSL language doesn't define UINT64 type so we'll probably never enter this branch.
return ShadingSystem::convert_value( value, type, &m_pointIndex, OIIO::TypeDesc( OIIO::TypeDesc::UINT64 ) );
}
}

vector<UserData>::const_iterator it = lower_bound(
m_userData.begin(),
m_userData.end(),
Expand Down

0 comments on commit 6f6b4e4

Please sign in to comment.