[ogre 2.1+] Is it possible to create mesh and texture in background thread Topic is solved

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


Post Reply
yisky
Gnoblar
Posts: 14
Joined: Tue Sep 17, 2013 12:06 pm

[ogre 2.1+] Is it possible to create mesh and texture in background thread

Post by yisky »

I used to use Ogre 1.9 developed a GIS platform. Nowdays I decide to upgrade it to Ogre 2.1 and I met a trouble. In Ogre 1.9 we create mesh and texture in a single thread but when we do the same thing in Ogre 2.1 we caught a exception:
"two threads were found to be executing functions associated with the same device[context] at the same time."
After search online I found that in DX11 "Device Context" is not thread safe. So is there other way to create mesh and texture in background thread?
Our GIS platform has a lot of models to render during executing and create mesh and texture in a single thread is only way we can choose!!!

Sorry for my poor english, hope everybody know what i mean! :(
User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5298
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1279
Contact:

Re: [ogre 2.1+] Is it possible to create mesh and texture in background thread

Post by dark_sylinc »

Hi!

I'm afraid not. You can check the FAQ regarding multithreading.

We do not support background mesh or texture creation in Ogre 2.1.
Ogre 2.2 does support background texture streaming though, and is soon getting out of WIP state.

If you're creating the meshes yourself programmatically (e.g. procedurally generated content or similar), you can create the Mesh pointer in the main thread, map the buffers, and then fill them in a background thread, then unmap the buffers in the main thread when done, and create the Items.

Basically a ping pong of messages between the main thread and worker thread so that the main thread performs the API calls, and the worker thread does the heavy generation work and fill the GPU buffers.
That is how Ogre 2.2 deals with background texture streaming.

We've had an advanced user who somehow managed to use the NULLRenderSystem to fool the resource system and load the meshes in the background, then copy the buffers in RAM over to the actual RenderSystem. But this is a very advanced use case and we don't do it out of the box.
yisky
Gnoblar
Posts: 14
Joined: Tue Sep 17, 2013 12:06 pm

Re: [ogre 2.1+] Is it possible to create mesh and texture in background thread

Post by yisky »

dark_sylinc wrote: Wed Jan 16, 2019 5:16 am Hi!

I'm afraid not. You can check the FAQ regarding multithreading.

We do not support background mesh or texture creation in Ogre 2.1.
Ogre 2.2 does support background texture streaming though, and is soon getting out of WIP state.

If you're creating the meshes yourself programmatically (e.g. procedurally generated content or similar), you can create the Mesh pointer in the main thread, map the buffers, and then fill them in a background thread, then unmap the buffers in the main thread when done, and create the Items.

Basically a ping pong of messages between the main thread and worker thread so that the main thread performs the API calls, and the worker thread does the heavy generation work and fill the GPU buffers.
That is how Ogre 2.2 deals with background texture streaming.

We've had an advanced user who somehow managed to use the NULLRenderSystem to fool the resource system and load the meshes in the background, then copy the buffers in RAM over to the actual RenderSystem. But this is a very advanced use case and we don't do it out of the box.
Thanks for your reply!I have tried as you say, create mesh and map the buffers in main thread, and then fill buffers in a background thread, then unmap buffers in main thread.But I got another exception:
"DrawIndexedInstanced: Draw cannot be invoked while a bound Resource is currently mapped. The Vertex Buffer at slot (0) is still mapped"
User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5298
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1279
Contact:

Re: [ogre 2.1+] Is it possible to create mesh and texture in background thread

Post by dark_sylinc »

1. I'm afraid a reasonable explanation to that message is that you forgot to unmap a buffer. Perhaps you're unmapping the wrong buffer, or you're mapping it again by accident.

2. If you're using BT_DYNAMIC_* buffers, this would make sense, though, since we actually only give you a chunk of a larger buffer. Thus if you keep the buffer mapped, it will raise this error because we're also using another region of that buffer for something else, and we need the whole buffer to be unmapped.
So if that's the case, it's not your fault really.

3. This reminds me that, for optimum performance, you should use buffers that are BT_DEFAULT or BT_IMMUTABLE unless these meshes are actually being updated from scratch every frame.
Every time the GPU reads from BT_DYNAMIC_*, it has to fetch the vertex data over the PCI-E lane.

In order to update a BT_DEFAULT mesh you can either use the BufferPacked::upload interface, or use a StagingBuffer directly (less user friendly but more efficient because you can write directly to the GPU mapped memory). Each StagingBuffers gets its own internal buffer (i.e. it's not shared), so you can keep it mapped as long you want.
See the implementation of BufferInterface::upload as it uses a StagingBuffer behind the scene.

That way you won't have complaints that the buffer is currently mapped, and keep an efficient representation for meshes.
yisky
Gnoblar
Posts: 14
Joined: Tue Sep 17, 2013 12:06 pm

Re: [ogre 2.1+] Is it possible to create mesh and texture in background thread

Post by yisky »

Sorry to trouble again!

I have to create mesh as you say and it works well now. But there is still another quesiton that how to handle lots of textures during executing in Ogre 2.1. Any good suggestions!!!

Thank you!!!
Post Reply