[2.3] Bug when creating vertex buffer for GL3+ on a different thread

Discussion area about developing with Ogre-Next (2.1, 2.2 and beyond)


crjc
Gnoblar
Posts: 14
Joined: Sat Oct 10, 2020 10:19 pm

[2.3] Bug when creating vertex buffer for GL3+ on a different thread

Post by crjc »

When attempting to create a vertex buffer with OpenGL3+ on a non-main thread, Ogre throws an exception as it attempts to copy memory to 0x0 here:
https://github.com/OGRECave/ogre-next/b ... ce.cpp#L61

Doing a little bit of digging, I found that the 0x0 ptr is coming from `GL3PlusStagingBuffer::mapImpl`: https://github.com/OGRECave/ogre-next/b ... r.cpp#L204

I can reproduce this only with OpenGL3+ as the render system on both Windows and Linux. The issue does not happen on Windows with D3D11 or on Mac with Metal.


To reproduce:
The quickest way to repro I think is to modify the CustomRenderable sample. I simply wrapped the creation of the renderable in a std::thread.

line 23 of CustomRenderableGameState.cpp https://github.com/OGRECave/ogre-next/b ... te.cpp#L23

becomes

Code: Select all

	std::thread thread( [this, sceneManager]() {
            mMyCustomRenderable = OGRE_NEW Ogre::MyCustomRenderable(
                Ogre::Id::generateNewId<Ogre::MovableObject>(),
                &sceneManager->_getEntityMemoryManager( Ogre::SCENE_DYNAMIC ), sceneManager, 10u );
        });

        thread.join();
User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5436
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1343

Re: [2.3] Bug when creating vertex buffer for GL3+ on a different thread

Post by dark_sylinc »

In all APIs you're dealing with potential race conditions.

Mapping or creating a vertex buffer from another thread is not supported; and it would also be very difficult to support safely.

Assuming you explicitly ensure there are no race conditions (e.g main thread does nothing rendering related), then
GL in particular will complain about lack of GL context (there must be 1 GL context per thread). Setting up shared contexts is hard and I personally never got it work correctly; other people have succeeded but there's always drivers bugs lurking in the corner.

In other words, getting it to work with GL is a known issue and a very hard one to support. Also please note it's quite possible your D3D11 & Metal versions will eventually end up with crashes, glitches or corruption.

Cheers
paroj
OGRE Team Member
OGRE Team Member
Posts: 2108
Joined: Sun Mar 30, 2014 2:51 pm
x 1134

Re: [2.3] Bug when creating vertex buffer for GL3+ on a different thread

Post by paroj »

dark_sylinc wrote: Tue Nov 16, 2021 2:06 am Assuming you explicitly ensure there are no race conditions (e.g main thread does nothing rendering related), then
GL in particular will complain about lack of GL context (there must be 1 GL context per thread). Setting up shared contexts is hard and I personally never got it work correctly; other people have succeeded but there's always drivers bugs lurking in the corner.
In case you want to try, this is how:
https://ogrecave.github.io/ogre-next/ap ... 51069f543a