ID3D11Device::Map, or how to make fast updates of vertex buffers

Started by
10 comments, last by PLCrashy 11 years, 8 months ago
Hi gamedev,

I'm requesting your help as I know there is a lot of D3D11 experts here.

First, let me tell you about the background that led me to create this topic:

I use Ogre for the graphic part of my game engine, and Ogre has a nice heightfield module that allows dynamic sculpting of the terrain.
However, if the sculpting (and, in the background, the update of the vertex buffer) is fast enough in DirectX 9, it is way too much slow in DirectX 11, and I would like to improve its speed.

Basically, here is the global setup of the buffers:

  1. The terrain hardware buffer is created with an usage==D3D11_USAGE_DEFAULT and CPUAccessFlags==0
  2. At initialisation:

  • A staging buffer is created with an usage==D3D11_USAGE_STAGING and CPUAccessFlags == D3D11_CPU_ACCESS_WRITE|3D11_CPU_ACCESS_READ

  • The staging buffer is mapped with flags D3D11_MAP_WRITE to get a pointer to the vertex data.
  • vertex data is written at the previously given location.
  • Stating buffer is unmapped.
  • Using ID3D11Device::GetImmediateContext()::CopyResource the vertex position is copied from the staging buffer to the original terrain vertex buffer.
  • Stating buffer is deleted.

When editing the terrain dynamically, it is the same global process described in "2" that is made, but after some debugging I've seen that the ID3D11Device::Map call on the staging buffer is really slow (basically using the "break all" technique to see where the program is spending time.) During this operation, the whole original buffer is updated which is not performance friendly, but the same was done in Direct3d 9 without a huge performance hit.

I know that Direct3D 11 api is really different from the Direct3D 9 one, but is there a way to achieve fast update of a vertex buffer like with the old IDirect3DVertexBuffer9::Lock/Unlock ?

I'm sorry if it's not really clear, english is not my native language and I'm still beginning with all this D3D11 stuff.
If you need more details, please ask.


Thanks.
Advertisement
I'm beginner myself, but I could suggest to try to create buffer with D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE flags, and store dublicate heightmap data in RAM. Most likely that will double memory usage.

When you need to update heightmap use Map() and then memcpy() from RAM into buffer. I've been using this method for my tool and I see no FPS difference even if I update it every frame.
Yes but doing it like that means that when rendering the terrain in game mode(==no real time editing), it's going to be slower, no?
You can edit, why not? Just use Map() and upload new data. You can't read it though, that's why you have to save duplicate data in RAM.
Are you creating the buffer every frame in which you edit your terrain? If so, don't do that.

Also you can map only a certain region of a buffer so you don't need to copy everything.

You can also check the ID3D11DeviceContext::CopySubresourceRegion function and check how it performs.

Are you creating the buffer every frame in which you edit your terrain? If so, don't do that.

The staging buffer is created at each update, yes.


Also you can map only a certain region of a buffer so you don't need to copy everything.
[/quote]
Yes, but the implementation of the terrain in ogre always "lock" the whole buffer, even when editing a small part of the terrain. It's non optimal but is fast enough in Direct3D9. If I cannot achieve to speed up in Direct3d 11, I'll rewrite this code to update only the edited part.

You can also check the ID3D11DeviceContext::CopySubresourceRegion function and check how it performs.
[/quote]
Related to previous quote.

Are you creating the buffer every frame in which you edit your terrain? If so, don't do that.

The staging buffer is created at each update, yes.


Also you can map only a certain region of a buffer so you don't need to copy everything.
[/quote]
Yes, but the implementation of the terrain in ogre always "lock" the whole buffer, even when editing a small part of the terrain. It's non optimal but is fast enough in Direct3D9. If I cannot achieve to speed up in Direct3d 11, I'll rewrite this code to update only the edited part.

You can also check the ID3D11DeviceContext::CopySubresourceRegion function and check how it performs.
[/quote]
Related to previous quote.



I'm beginner myself, but I could suggest to try to create buffer with D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE flags, and store dublicate heightmap data in RAM. Most likely that will double memory usage.[/quote]

I tried doing like that, with D3D11_CPU_ACCESS_WRITE and D3D11_USAGE_DYNAMIC, using a shadow buffer and mapping the buffer with D3D11_MAP_WRITE_DISCARD. There is still huge latency when updating terrain geometry.
Are you creating the buffer every frame in which you edit your terrain? If so, don't do that.[/quote]

That was the point! Now I create and fill the staging buffer only at the first edition and terrain editing is fast:)

Thanks for your help, I must now find a way to integrate it properly in Ogre.
before you do so, as boys have stated, you should(!) always create a static buffer, and if for some reason you need to alter it, do so in a shader. (do not update data of buffer that has been streamed to gpu, use shader to firm it)
You mean I'd rather do a kind of displacement shader?

This topic is closed to new replies.

Advertisement