Skip to content

GL Renderer : Support PointInstancers - #7294

Open
johnhaddon wants to merge 6 commits into
1.7_maintenancefrom
glPointInstancer
Open

johnhaddon wants to merge 6 commits into
1.7_maintenancefrom
glPointInstancer

Conversation

@johnhaddon

Copy link
Copy Markdown
Member

This adds drawing of PointInstancers in the OpenGL viewport, using instanced draw calls in GL, and giving us much better framerates than the old adaptor workflow. I do wonder if we want some sort of switch to allow folks to render these as points instead, but I haven't done that here - people can always use the visible set to control what they see as they do for standard geometry.

This also meant fixing a bug whereby SceneGadget could call `render()` before having set the "camera" option.

@murraystevenson murraystevenson left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks John, aside from the test failure, the platform24 build issue, and the crash I'm running into, this is looking pretty reasonable at first glance.

I do wonder if we want some sort of switch to allow folks to render these as points instead, but I haven't done that here - people can always use the visible set to control what they see as they do for standard geometry.

I haven't done a huge amount of scale testing due to the crashes noted inline, but I do also wonder if in-built support for decimation and/or visualisting per-instance bounds may also be necessary for production-scale PointInstancers.

Always drawing the instances does make it harder to inspect IDs with the VisualiserTool, as the point labels can now fail their depth test if the instance geometry is drawn on top of the point. Users could work around it by selecting the PointInstancer and then collapsing/excluding the location so it is only drawn by the VisualiserTool, but that isn't a particularly great workflow (nor would it work if/when we make the VisualiserTool use SceneAlgo::visitVisibleLocations()). Maybe we can get away with disabling the label depth test for PointInstancers, or figure out how to bias the label test by the bounds of the instance...

public :

// Can be called concurrently with other calls to `get()`.
IECoreGL::RenderablePtr get( const IECoreScene::PointInstancer *instancer, const std::vector<IECoreScenePreview::Renderer::Prototype> &prototypes )

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm running into repeatable crashes with a PointInstancer generating GafferBots once the number of points increases to around 20,000. From the stack traces below, the robot is suggesting it's a race between get() and clearUnused() in PointInstancerCache, but I haven't verified that.

Here's the scene, it'd be good to know if it also reliably crashes on your end.

import Gaffer
import GafferScene
import IECore
import imath

Gaffer.Metadata.registerValue( parent, "serialiser:milestoneVersion", 1, persistent=False )
Gaffer.Metadata.registerValue( parent, "serialiser:majorVersion", 7, persistent=False )
Gaffer.Metadata.registerValue( parent, "serialiser:minorVersion", 7, persistent=False )
Gaffer.Metadata.registerValue( parent, "serialiser:patchVersion", 0, persistent=False )

__children = {}

__children["Plane"] = GafferScene.Plane( "Plane" )
parent.addChild( __children["Plane"] )
__children["Plane"].addChild( Gaffer.V2fPlug( "__uiPosition", defaultValue = imath.V2f( 0, 0 ), flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )
__children["FreezeTransform"] = GafferScene.FreezeTransform( "FreezeTransform" )
parent.addChild( __children["FreezeTransform"] )
__children["FreezeTransform"].addChild( Gaffer.V2fPlug( "__uiPosition", defaultValue = imath.V2f( 0, 0 ), flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )
__children["PathFilter"] = GafferScene.PathFilter( "PathFilter" )
parent.addChild( __children["PathFilter"] )
__children["PathFilter"].addChild( Gaffer.V2fPlug( "__uiPosition", defaultValue = imath.V2f( 0, 0 ), flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )
__children["Scatter"] = GafferScene.Scatter( "Scatter" )
parent.addChild( __children["Scatter"] )
__children["Scatter"].addChild( Gaffer.V2fPlug( "__uiPosition", defaultValue = imath.V2f( 0, 0 ), flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )
__children["PointInstancer"] = GafferScene.PointInstancer( "PointInstancer" )
parent.addChild( __children["PointInstancer"] )
__children["PointInstancer"].addChild( Gaffer.V2fPlug( "__uiPosition", defaultValue = imath.V2f( 0, 0 ), flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )
__children["GafferBot"] = GafferScene.SceneReader( "GafferBot" )
parent.addChild( __children["GafferBot"] )
__children["GafferBot"].addChild( Gaffer.V2fPlug( "__uiPosition", defaultValue = imath.V2f( 0, 0 ), flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )
__children["Orientation"] = GafferScene.Orientation( "Orientation" )
parent.addChild( __children["Orientation"] )
__children["Orientation"].addChild( Gaffer.V2fPlug( "__uiPosition", defaultValue = imath.V2f( 0, 0 ), flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )
__children["PathFilter2"] = GafferScene.PathFilter( "PathFilter2" )
parent.addChild( __children["PathFilter2"] )
__children["PathFilter2"].addChild( Gaffer.V2fPlug( "__uiPosition", defaultValue = imath.V2f( 0, 0 ), flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )
__children["PrimitiveVariableTweaks"] = GafferScene.PrimitiveVariableTweaks( "PrimitiveVariableTweaks" )
parent.addChild( __children["PrimitiveVariableTweaks"] )
__children["PrimitiveVariableTweaks"]["tweaks"].addChild( Gaffer.TweakPlug( name = "tweak0", valuePlug = Gaffer.V3fPlug( "value", defaultValue = imath.V3f( 0, 0, 0 ), ), flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic ) )
__children["PrimitiveVariableTweaks"].addChild( Gaffer.V2fPlug( "__uiPosition", defaultValue = imath.V2f( 0, 0 ), flags = Gaffer.Plug.Flags.Default | Gaffer.Plug.Flags.Dynamic, ) )
__children["Plane"]["transform"]["rotate"].setValue( imath.V3f( -90, 0, 0 ) )
__children["Plane"]["dimensions"].setValue( imath.V2f( 1000, 1000 ) )
__children["Plane"]["__uiPosition"].setValue( imath.V2f( -27.2000046, 32.3075218 ) )
__children["FreezeTransform"]["in"].setInput( __children["Plane"]["out"] )
__children["FreezeTransform"]["filter"].setInput( __children["PathFilter"]["out"] )
__children["FreezeTransform"]["__uiPosition"].setValue( imath.V2f( -27.1991882, 24.1434593 ) )
__children["PathFilter"]["paths"].setValue( IECore.StringVectorData( [ '/plane' ] ) )
__children["PathFilter"]["__uiPosition"].setValue( imath.V2f( -10.2260952, 29.7942963 ) )
__children["Scatter"]["in"].setInput( __children["FreezeTransform"]["out"] )
__children["Scatter"]["filter"].setInput( __children["PathFilter"]["out"] )
__children["Scatter"]["name"].setValue( 'points' )
__children["Scatter"]["density"].setValue( 0.019999999552965164 )
__children["Scatter"]["__uiPosition"].setValue( imath.V2f( -27.1983261, 15.9793968 ) )
__children["PointInstancer"]["in"].setInput( __children["Orientation"]["out"] )
__children["PointInstancer"]["filter"].setInput( __children["PathFilter2"]["out"] )
__children["PointInstancer"]["prototypes"].setInput( __children["GafferBot"]["out"] )
__children["PointInstancer"]["out"].setInput( __children["PointInstancer"]["BoxOut"]["__out"] )
__children["PointInstancer"]["prototypesList"].setValue( IECore.StringVectorData( [ '/GAFFERBOT' ] ) )
__children["PointInstancer"]["__uiPosition"].setValue( imath.V2f( -25.6983318, -8.41015625 ) )
__children["GafferBot"]["fileName"].setValue( '${GAFFER_ROOT}/resources/gafferBot/caches/gafferBot.scc' )
__children["GafferBot"]["__uiPosition"].setValue( imath.V2f( -3.31926537, -0.246093512 ) )
__children["Orientation"]["in"].setInput( __children["PrimitiveVariableTweaks"]["out"] )
__children["Orientation"]["filter"].setInput( __children["PathFilter2"]["out"] )
__children["Orientation"]["inEuler"].setValue( 'rot' )
__children["Orientation"]["randomEnabled"].setValue( True )
__children["Orientation"]["randomSpread"].setValue( 90.0 )
__children["Orientation"]["randomTwist"].setValue( 90.0 )
__children["Orientation"]["__uiPosition"].setValue( imath.V2f( -27.1983318, -0.24609381 ) )
__children["PathFilter2"]["paths"].setValue( IECore.StringVectorData( [ '/plane/points' ] ) )
__children["PathFilter2"]["__uiPosition"].setValue( imath.V2f( -9.17493153, 13.0713129 ) )
__children["PrimitiveVariableTweaks"]["in"].setInput( __children["Scatter"]["out"] )
__children["PrimitiveVariableTweaks"]["filter"].setInput( __children["PathFilter2"]["out"] )
__children["PrimitiveVariableTweaks"]["interpolation"].setValue( 3 )
__children["PrimitiveVariableTweaks"]["tweaks"]["tweak0"]["name"].setValue( 'rot' )
__children["PrimitiveVariableTweaks"]["tweaks"]["tweak0"]["mode"].setValue( 5 )
__children["PrimitiveVariableTweaks"]["__uiPosition"].setValue( imath.V2f( -27.1983318, 7.91796875 ) )


del __children

Crash 1

Thread 1 (Thread 0x7f7d42bfa6c0 (LWP 4160043)):
#0  0x00007f7d781059a4 in std::once_flag::_Prepare_execution::_Prepare_execution<std::call_once<(anonymous namespace)::PointInstancerCache::get(IECoreScene::PointInstancer const*, std::vector<IECoreScenePreview::Renderer::Prototype, std::allocator<IECoreScenePreview::Renderer::Prototype> > const&)::{lambda()#1}>(std::once_flag&, (anonymous namespace)::PointInstancerCache::get(IECoreScene::PointInstancer const*, std::vector<IECoreScenePreview::Renderer::Prototype, std::allocator<IECoreScenePreview::Renderer::Prototype> > const&)::{lambda()#1}&&)::{lambda()#1}>((anonymous namespace)::PointInstancerCache::get(IECoreScene::PointInstancer const*, std::vector<IECoreScenePreview::Renderer::Prototype, std::allocator<IECoreScenePreview::Renderer::Prototype> > const&)::{lambda()#1}&)::{lambda()#1}::_FUN() () from .../lib/libGafferScene.so
#1  0x00007f7daa4862c4 in __pthread_once_slow.isra.0 () from /lib64/libc.so.6
#2  0x00007f7daa486339 in pthread_once@GLIBC_2.2.5 () from /lib64/libc.so.6
#3  0x00007f7d781060bc in (anonymous namespace)::OpenGLRenderer::pointInstancer(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, boost::container::small_vector<boost::intrusive_ptr<IECoreScene::PointInstancer const>, 2ul, void, void> const&, boost::container::small_vector<float, 2ul, void, void> const&, std::vector<IECoreScenePreview::Renderer::Prototype, std::allocator<IECoreScenePreview::Renderer::Prototype> > const&, IECoreScenePreview::Renderer::AttributesInterface const*) () from .../lib/libGafferScene.so
#4  0x00007f7d783022be in GafferScene::Private::RendererAlgo::outputObject(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, GafferScene::Private::RendererAlgo::SampledObject const&, IECoreScenePreview::Renderer::AttributesInterface const*, GafferScene::Private::RendererAlgo::RenderOptions const&, GafferScene::ScenePlug const*, IECoreScenePreview::Renderer*) () from .../lib/libGafferScene.so
#5  0x00007f7d782e736d in GafferScene::RenderController::SceneGraph::updateObject(GafferScene::ScenePlug const*, GafferScene::RenderController::SceneGraph::Type, IECoreScenePreview::Renderer*, GafferScene::Private::RendererAlgo::RenderOptions const&, GafferScene::Private::RendererAlgo::LightLinks*) () from .../lib/libGafferScene.so
#6  0x00007f7d782ef23b in GafferScene::RenderController::SceneGraph::updateLocation(std::vector<IECore::InternedString, std::allocator<IECore::InternedString> > const&, unsigned int, GafferScene::RenderController::SceneGraph::Type, GafferScene::RenderController*) () from .../lib/libGafferScene.so
#7  0x00007f7d782efc58 in GafferScene::RenderController::SceneGraph::update(GafferScene::RenderController*, GafferScene::RenderController::SceneGraph::Type, unsigned int, Gaffer::ThreadState const&, std::vector<IECore::InternedString, std::allocator<IECore::InternedString> > const&, std::function<void (Gaffer::BackgroundTask::Status)> const&, IECore::PathMatcher const*, tbb::detail::d1::task_group_context&) () from .../lib/libGafferScene.so
#8  0x00007f7d782efefa in GafferScene::RenderController::SceneGraph::update(GafferScene::RenderController*, GafferScene::RenderController::SceneGraph::Type, unsigned int, Gaffer::ThreadState const&, std::vector<IECore::InternedString, std::allocator<IECore::InternedString> > const&, std::function<void (Gaffer::BackgroundTask::Status)> const&, IECore::PathMatcher const*, tbb::detail::d1::task_group_context&) () from .../lib/libGafferScene.so
#9  0x00007f7d782efefa in GafferScene::RenderController::SceneGraph::update(GafferScene::RenderController*, GafferScene::RenderController::SceneGraph::Type, unsigned int, Gaffer::ThreadState const&, std::vector<IECore::InternedString, std::allocator<IECore::InternedString> > const&, std::function<void (Gaffer::BackgroundTask::Status)> const&, IECore::PathMatcher const*, tbb::detail::d1::task_group_context&) () from .../lib/libGafferScene.so
#10 0x00007f7d782d945e in GafferScene::RenderController::updateInternal(std::function<void (Gaffer::BackgroundTask::Status)> const&, IECore::PathMatcher const*, bool) () from .../lib/libGafferScene.so
#11 0x00007f7d95e1f8fb in std::_Function_handler<void (IECore::Canceller const&), Gaffer::ParallelAlgo::callOnBackgroundThread(Gaffer::Plug const*, std::function<void ()>)::{lambda(IECore::Canceller const&)#1}>::_M_invoke(std::_Any_data const&, IECore::Canceller const&) () from .../lib/libGaffer.so
#12 0x00007f7d95d21af1 in tbb::detail::d1::enqueue_task<Gaffer::BackgroundTask::BackgroundTask(Gaffer::Plug const*, std::function<void (IECore::Canceller const&)> const&)::{lambda()#1}>::execute(tbb::detail::d1::execution_data&) () from .../lib/libGaffer.so
#13 0x00007f7d98fd7817 in tbb::detail::r1::task_dispatcher::local_wait_for_all<false, tbb::detail::r1::outermost_worker_waiter> (t=<optimized out>, waiter=..., this=0x7f7d7910cb00) at /__w/dependencies/dependencies/TBB/working/oneTBB-2021.13.0/src/tbb/task_dispatcher.h:323
#14 tbb::detail::r1::task_dispatcher::local_wait_for_all<tbb::detail::r1::outermost_worker_waiter> (t=0x0, waiter=..., this=0x7f7d7910cb00) at /__w/dependencies/dependencies/TBB/working/oneTBB-2021.13.0/src/tbb/task_dispatcher.h:459
#15 tbb::detail::r1::arena::process (tls=..., this=<optimized out>) at /__w/dependencies/dependencies/TBB/working/oneTBB-2021.13.0/src/tbb/arena.cpp:217
#16 tbb::detail::r1::thread_dispatcher_client::process (td=..., this=<optimized out>) at /__w/dependencies/dependencies/TBB/working/oneTBB-2021.13.0/src/tbb/thread_dispatcher_client.h:41
#17 tbb::detail::r1::thread_dispatcher::process (this=<optimized out>, j=...) at /__w/dependencies/dependencies/TBB/working/oneTBB-2021.13.0/src/tbb/thread_dispatcher.cpp:195
#18 0x00007f7d98fce003 in tbb::detail::r1::rml::private_worker::run (this=0x7f7d79106e00) at /__w/dependencies/dependencies/TBB/working/oneTBB-2021.13.0/src/tbb/private_server.cpp:271
#19 tbb::detail::r1::rml::private_worker::thread_routine (arg=0x7f7d79106e00) at /__w/dependencies/dependencies/TBB/working/oneTBB-2021.13.0/src/tbb/private_server.cpp:221
#20 0x00007f7daa480f34 in start_thread () from /lib64/libc.so.6

Crash 2

pure virtual method called
terminate called without an active exception
Aborted (core dumped)

#0  0x00007fa7e7682e7c in __pthread_kill_implementation () from /lib64/libc.so.6
#1  0x00007fa7e7628f0e in raise () from /lib64/libc.so.6
#2  0x00007fa7e76106d0 in abort () from /lib64/libc.so.6
#3  0x00007fa7e78091b4 in __gnu_cxx::__verbose_terminate_handler() [clone .cold] () from /lib64/libstdc++.so.6
#4  0x00007fa7e781ebfc in __cxxabiv1::__terminate(void (*)()) () from /lib64/libstdc++.so.6
#5  0x00007fa7e7808d3a in std::terminate() () from /lib64/libstdc++.so.6
#6  0x00007fa7e781faa5 in __cxa_pure_virtual () from /lib64/libstdc++.so.6
#7  0x00007fa7d1271d2d in IECoreGL::Group::bound() const () from .../lib/libIECoreGL.so
#8  0x00007fa7d1271d2d in IECoreGL::Group::bound() const () from .../lib/libIECoreGL.so
#9  0x00007fa7b59014ef in (anonymous namespace)::OpenGLRenderer::command(IECore::InternedString, std::map<IECore::InternedString, boost::intrusive_ptr<IECore::Data>, std::less<IECore::InternedString>, std::allocator<std::pair<IECore::InternedString const, boost::intrusive_ptr<IECore::Data> > > > const&)
    () from .../lib/libGafferScene.so
#10 0x00007fa76ef9f868 in GafferSceneUI::SceneGadget::bound(bool, IECore::PathMatcher const*) const () from .../lib/libGafferSceneUI.so
#11 0x00007fa76ef9ff2e in GafferSceneUI::SceneGadget::bound() const () from .../lib/libGafferSceneUI.so
#12 0x00007fa7d0cf7db5 in GafferUI::Gadget::bound() const () from .../lib/libGafferUI.so
#13 0x00007fa7d0db1340 in GafferUI::ViewportGadget::renderInternal(GafferUI::Gadget::RenderReason, GafferUI::Gadget::Layer) const () from .../lib/libGafferUI.so
#14 0x00007fa7d0db2942 in GafferUI::ViewportGadget::render() const () from .../lib/libGafferUI.so
#15 0x00007fa7d1fe3f7d in (anonymous namespace)::render(GafferUI::ViewportGadget const&) () from .../python/GafferUI/_GafferUI.so
#16 0x00007fa7d1ff5532 in boost::python::detail::caller_arity<1u>::impl<void (*)(GafferUI::ViewportGadget const&), boost::python::default_call_policies, boost::mpl::vector2<void, GafferUI::ViewportGadget const&> >::operator()(_object*, _object*) ()
   from .../python/GafferUI/_GafferUI.so

@johnhaddon johnhaddon Nov 27, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've pushed 39b30cf to take care of this.

Comment on lines +809 to +816
if( instanceOrientationActive )
{
float orientationLength = length( instanceOrientation );
if( orientationLength > 0.0 )
{
orientation = instanceOrientation / orientationLength;
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it's worth normalising orientations on the way in, so we don't have to do this work in the shader?

@johnhaddon johnhaddon Nov 27, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't measure any difference in framerate from it, but it does make sense to do this once upfront rather than per-frame - 8746fa7.

@johnhaddon johnhaddon changed the title GL Renderer : Support Pointinstancers GL Renderer : Support PointInstancers Nov 27, 2026
Fix crash caused by PointInstancerCache. I had modelled this on the caches for other renderer backends, but had forgotten a key distinction of the GL renderer - it renders on the foreground thread even as objects arrive on the background thread. This means we could call `PointInstancerCache::clearUnused()` concurrently with `get()`, which as documented was not safe.

The solution is to lean on IECoreGL's existing CachedConverter class, which was designed with the GL renderer's needs in mind.

There's a bit of a compromise here regarding the bound. It is now just the bound of the points, since we don't have the prototypes to hand in the converter. I think this is probably OK - we don't compute a more accurate bound in Gaffer anyway. But we could bring back the accurate bound by stashing the bounds of the prototypes in the PointInstancer passed to the converter (there's no side-channel we could use for this). This may become more palatable after we've done decimation, which will need to take a similar approach.
Normalize orientation once on CPU during conversion, rather than repeating it per-frame on GPU.
@johnhaddon

Copy link
Copy Markdown
Member Author

aside from the test failure, the platform24 build issue

I'm hoping the test failure is fixed by 7d4ec19, which also re-enables a bunch of skipped tests in the hope that they might be fixed too. The problem was in the tests rather than in the production code. If this works out and we're feeling lucky, there are a few more skipIs we could try removing...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Pending Review

Development

Successfully merging this pull request may close these issues.

2 participants