Modify mesh from another thread

Hi,

is it possible to modify the mesh buffers from another thread? The geometry is already attached to the scene.
The separate thread should get the buffers from the mesh and update the vertex buffer with vb.updateData(newBuffer); after modifying it.

Is it possible to modifying meshes this way?

regards,

Alrik

No but you could do some kind of “double buffering”, editing a second set of vertex data on the other thread and swapping them in the update loop.

ah ok do you mean that my separate thread could copy the buffer from the mesh and modify it? After that, only the main thread swap/update the buffer of the mesh via vb.updateData(newBuffer);?

Is deep clone of the buffers necessary or is it okay to use something like buffer.duplicate() to create a new “view” of the buffer instead of to copy the whole data?

regards

Alrik

The thread can’t do the cloning. The thread that owns it has to do the cloning.

The rule is that you cannot access the mesh from multiple threads… that includes cloning.

How about you make two buffers in the first place instead of “cloning” them?

Why can’t the thread do the cloning? I could create another view of the buffer first and then copy the buffer. Each view has its own position/limit state of the buffer.

So you mean the following code should not work?

example code
[java]

// THREAD:

SafeArrayList<VertexBuffer> bufferList = mesh.getBufferList();

Buffer buffer = vertexBuffer.getDataReadOnly();
// another way: vertexBuffer.getData().duplicate();

Buffer clone = BufferUtils.clone(buffer);

// modify the clone buffer

=> add the result to a list on the main app

// MAIN-APP

=> get the result from the list and update the mesh
vertexBuffer.updateData(clone);
mesh.updateCounts();

[/java]

What is it that you are trying to do exactly? All of these clonings and duplications going on… usually it’s better just to regenerate rather than modify.

And to answer your question, no it’s not safe. You might get away with it most of the time but why bother? It’s not safe.

I have a lot of complex meshes. Rarely I want to modify a mesh but the duration to update the mesh is very important. I think its more efficient to update VBOs instead of creating a new one.
It takes too much time to do all the calculations on the mesh. For this reason I try to do all the calculations on another thread. There are some frame drops if I do the calculations on the main thread.

@alrik said: I have a lot of complex meshes. Rarely I want to modify a mesh but the duration to update the mesh is very important. I think its more efficient to update VBOs instead of creating a new one. It takes too much time to do all the calculations on the mesh. For this reason I try to do all the calculations on another thread. There are some frame drops if I do the calculations on the main thread.

So grab the data on the update loop, do your calculations on the other thread and then apply it again on the update loop, whats the problem?

Ok I will try it, thank you