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

OSL ShadingEngine : add *_index* as user data #2067

Merged
merged 1 commit into from
May 5, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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