Update vertex buffer in non UI thread using DirectX11

Started by
4 comments, last by YixunLiu 6 years, 9 months ago

Hi,

 
 

I want to render a object with a dynamic vertex buffer and I do rendering in UI thread. I am thinking is it possible to change this vertex buffer content in a non UI thread using Map and Unmap.

Thanks.

YL

Advertisement

Yes, you can either call Map/Unmap on the main thread, but use another thread to fill the buffer.
e.g. Thread#1 calls Map, Thread#2 fills the buffer, Thread#1 calls Unmap.

Or, you can use a deferred context:
Thread #2 calls Map/Unmap and FinishCommandList, then Thread#1 calls ExecuteCommandList

Thanks Hodgman.

I am not clear about the first method you mentioned.

For example, I can run the following code in main thread,


m_d3dContext->Map(vertexBuffer2.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
	//	Update the vertex buffer here.
	memcpy(mappedResource.pData, vertices, sizeof(vertices));
	//	Reenable GPU access to the vertex buffer data.
	m_d3dContext->Unmap(vertexBuffer2.Get(), 0);

 

To run the above code in two threads, I run map and unmap in main thread and memcpy in another thread.

However, is it safe to call unmap while another thread is updating the buffer?

Thanks.

 

No that's unsafe.

You need to synchronise your threads so that the memcpy doesn't begin until after the call to Map, and the call to Unmap doesn't occur until some time after the memcpy is complete.

Thanks, Hodgman!

This topic is closed to new replies.

Advertisement