Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
10.7.x.x (relative to 10.7.4.1)
========

Improvements
------------

- IECoreGL::CachedConverter : Added `convert()` overload taking a `resultType` parameter.

Fixes
-----

Expand Down
6 changes: 6 additions & 0 deletions include/IECoreGL/CachedConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ class IECOREGL_API CachedConverter : public IECore::RefCounted
/// Returns the object converted to an appropriate IECoreGL type, reusing
/// a previous conversion where possible.
IECore::ConstRunTimeTypedPtr convert( const IECore::Object *object );
/// As above, but specifying the required result type. This is used
/// to select a converter via `ToGLConverter::create()`.
/// > Note : The cache key includes the result type, so two conversions
/// > with different result types will never share results, even if they
/// > would otherwise be equivalent.
IECore::ConstRunTimeTypedPtr convert( const IECore::Object *object, IECore::TypeId resultType );

/// Returns the maximum amount of memory (in bytes) the cache will use.
size_t getMaxMemory() const;
Expand Down
21 changes: 15 additions & 6 deletions src/IECoreGL/CachedConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ struct CacheGetterKey
{

CacheGetterKey()
: object( nullptr )
: object( nullptr ), resultType( IECore::InvalidTypeId )
{
}

CacheGetterKey( const IECore::Object *o )
: object( o ), hash( o->hash() )
CacheGetterKey( const IECore::Object *o, IECore::TypeId resultType )
: object( o ), resultType( resultType ), hash( o->hash().append( resultType ) )
{
}

Expand All @@ -82,6 +82,7 @@ struct CacheGetterKey
}

const IECore::Object *object;
const IECore::TypeId resultType;
const IECore::MurmurHash hash;

};
Expand All @@ -98,11 +99,14 @@ struct CachedConverter::MemberData
static IECore::RunTimeTypedPtr getter( const CacheGetterKey &key, size_t &cost )
{
cost = key.object->memoryUsage();
ToGLConverterPtr converter = ToGLConverter::create( key.object );
ToGLConverterPtr converter = ToGLConverter::create( key.object, key.resultType );
if( !converter )
{
throw IECore::Exception(
fmt::format( "Unable to create converter for Object of type \"{}\"", key.object->typeName() )
fmt::format(
"Unable to create converter from \"{}\" to \"{}\"",
key.object->typeName(), IECore::RunTimeTyped::typeNameFromTypeId( key.resultType )
)
);
}
return converter->convert();
Expand Down Expand Up @@ -130,7 +134,12 @@ CachedConverter::~CachedConverter()

IECore::ConstRunTimeTypedPtr CachedConverter::convert( const IECore::Object *object )
{
return m_data->cache.get( CacheGetterKey( object ) );
return convert( object, IECore::RunTimeTypedTypeId );
}

IECore::ConstRunTimeTypedPtr CachedConverter::convert( const IECore::Object *object, IECore::TypeId resultType )
{
return m_data->cache.get( CacheGetterKey( object, resultType ) );
}

size_t CachedConverter::getMaxMemory() const
Expand Down
6 changes: 3 additions & 3 deletions src/IECoreGL/bindings/CachedConverterBinding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ using namespace IECoreGL;
namespace
{

IECore::RunTimeTypedPtr convert( CachedConverter &c, IECore::ObjectPtr o )
IECore::RunTimeTypedPtr convert( CachedConverter &c, IECore::ObjectPtr o, IECore::TypeId resultType )
{
IECorePython::ScopedGILRelease gilRelease;
return boost::const_pointer_cast<IECore::RunTimeTyped>( c.convert( o.get() ) );
return boost::const_pointer_cast<IECore::RunTimeTyped>( c.convert( o.get(), resultType ) );
}

void clearUnused( CachedConverter &c )
Expand All @@ -65,7 +65,7 @@ void IECoreGL::bindCachedConverter()
{
IECorePython::RefCountedClass<CachedConverter, IECore::RefCounted>( "CachedConverter" )
.def( init<size_t>() )
.def( "convert", &convert )
.def( "convert", &convert, ( arg( "object" ), arg( "resultType" ) = IECore::RunTimeTypedTypeId ) )
.def( "getMaxMemory", &CachedConverter::getMaxMemory )
.def( "setMaxMemory", &CachedConverter::setMaxMemory )
.def( "clearUnused", &clearUnused )
Expand Down
17 changes: 17 additions & 0 deletions test/IECoreGL/CachedConverterTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,5 +208,22 @@ def t() :
for thread in threads :
thread.join()

def testResultType( self ) :

converter = IECoreGL.CachedConverter( 500 * 1024 * 1024 ) # 500 megs

mesh = IECoreScene.MeshPrimitive.createPlane( imath.Box2f( imath.V2f( -1 ), imath.V2f( 1 ) ) )
glMesh1 = converter.convert( mesh )
glMesh2 = converter.convert( mesh, IECore.RunTimeTyped.staticTypeId() )
self.assertIsInstance( glMesh1, IECoreGL.MeshPrimitive )
self.assertTrue( glMesh1.isSame( glMesh2 ) )

glMesh3 = converter.convert( mesh, IECoreGL.Renderable.staticTypeId() )
self.assertIsInstance( glMesh3, IECoreGL.MeshPrimitive )
self.assertFalse( glMesh3.isSame( glMesh1 ) )

with self.assertRaisesRegex( RuntimeError, 'Unable to create converter from "MeshPrimitive" to "Object"' ) :
converter.convert( mesh, IECore.Object.staticTypeId() )

if __name__ == "__main__":
unittest.main()
Loading