From 3bd0ecd39fbaddf6c0ca911e3001aea6f4cd53b9 Mon Sep 17 00:00:00 2001 From: John Haddon Date: Fri, 27 Nov 2026 14:52:48 +0000 Subject: [PATCH] GL CachedConverter : Add `convert()` overload specifying result type This allows the result type to be chosen when several ToGLConverters have been registered against the same source type. I think the "different result types create different cache entries" gotcha is reasonable, since we've never needed to specify result type at all to date. I only have one niche use case in mind (in Gaffer's GL renderer) so the only client to pass result types will be ones that _need_ a different result than the default. --- Changes | 5 +++++ include/IECoreGL/CachedConverter.h | 6 ++++++ src/IECoreGL/CachedConverter.cpp | 21 +++++++++++++------ .../bindings/CachedConverterBinding.cpp | 6 +++--- test/IECoreGL/CachedConverterTest.py | 17 +++++++++++++++ 5 files changed, 46 insertions(+), 9 deletions(-) diff --git a/Changes b/Changes index a6a1b29eab..1c955891c5 100644 --- a/Changes +++ b/Changes @@ -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 ----- diff --git a/include/IECoreGL/CachedConverter.h b/include/IECoreGL/CachedConverter.h index 49e2becff3..3dcd9b7b6f 100644 --- a/include/IECoreGL/CachedConverter.h +++ b/include/IECoreGL/CachedConverter.h @@ -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; diff --git a/src/IECoreGL/CachedConverter.cpp b/src/IECoreGL/CachedConverter.cpp index 4316fa2c7f..6aad77f4e3 100644 --- a/src/IECoreGL/CachedConverter.cpp +++ b/src/IECoreGL/CachedConverter.cpp @@ -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 ) ) { } @@ -82,6 +82,7 @@ struct CacheGetterKey } const IECore::Object *object; + const IECore::TypeId resultType; const IECore::MurmurHash hash; }; @@ -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(); @@ -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 diff --git a/src/IECoreGL/bindings/CachedConverterBinding.cpp b/src/IECoreGL/bindings/CachedConverterBinding.cpp index 71e5990ef5..faddbdfb2b 100644 --- a/src/IECoreGL/bindings/CachedConverterBinding.cpp +++ b/src/IECoreGL/bindings/CachedConverterBinding.cpp @@ -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( c.convert( o.get() ) ); + return boost::const_pointer_cast( c.convert( o.get(), resultType ) ); } void clearUnused( CachedConverter &c ) @@ -65,7 +65,7 @@ void IECoreGL::bindCachedConverter() { IECorePython::RefCountedClass( "CachedConverter" ) .def( init() ) - .def( "convert", &convert ) + .def( "convert", &convert, ( arg( "object" ), arg( "resultType" ) = IECore::RunTimeTypedTypeId ) ) .def( "getMaxMemory", &CachedConverter::getMaxMemory ) .def( "setMaxMemory", &CachedConverter::setMaxMemory ) .def( "clearUnused", &clearUnused ) diff --git a/test/IECoreGL/CachedConverterTest.py b/test/IECoreGL/CachedConverterTest.py index b2d5e589ad..04f382530d 100644 --- a/test/IECoreGL/CachedConverterTest.py +++ b/test/IECoreGL/CachedConverterTest.py @@ -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()