Jump to content

  • Log In with Google      Sign In   
  • Create Account

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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 LM-Crashy   Members   -  Reputation: 470

Like
0Likes
Like

Posted 01 August 2012 - 02:24 AM

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:
  • The terrain hardware buffer is created with an usage==D3D11_USAGE_DEFAULT and CPUAccessFlags==0
  • 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.

Sponsor:

#2 Ripiz   Members   -  Reputation: 529

Like
0Likes
Like

Posted 01 August 2012 - 02:39 AM

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.

#3 LM-Crashy   Members   -  Reputation: 470

Like
0Likes
Like

Posted 01 August 2012 - 03:06 AM

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?

#4 Ripiz   Members   -  Reputation: 529

Like
0Likes
Like

Posted 01 August 2012 - 03:10 AM

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.

#5 n3Xus   Members   -  Reputation: 720

Like
1Likes
Like

Posted 01 August 2012 - 03:41 AM

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.

#6 LM-Crashy   Members   -  Reputation: 470

Like
0Likes
Like

Posted 01 August 2012 - 03:58 AM

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.

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.

Related to previous quote.

#7 LM-Crashy   Members   -  Reputation: 470

Like
0Likes
Like

Posted 01 August 2012 - 04:05 AM

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.

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.

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.


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.

Edited by CrashyCartman, 01 August 2012 - 04:06 AM.


#8 LM-Crashy   Members   -  Reputation: 470

Like
0Likes
Like

Posted 01 August 2012 - 05:58 AM

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


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.

#9 JohnnyCode   Members   -  Reputation: 292

Like
0Likes
Like

Posted 01 August 2012 - 07:41 PM

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)

#10 LM-Crashy   Members   -  Reputation: 470

Like
0Likes
Like

Posted 03 August 2012 - 01:54 AM

You mean I'd rather do a kind of displacement shader?

#11 L. Spiro   Crossbones+   -  Reputation: 14257

Like
1Likes
Like

Posted 03 August 2012 - 02:39 AM

english is not my native language

Your post’s English was better than most native speakers’ English.
The only problem was, “an usage”, which should be, “a usage”.



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

Related to previous quote.

I don’t see how the previous quote (a reminder to the audience: It was about updating the whole buffer at once) has any relevance.

ID3D11DeviceContext::CopySubresourceRegion() was the correct answer. It allows you to update a portion of the buffer, which by default means that it is also possible to update the entire buffer all at once.

Use it and check the performance. It should be fine.
And if not, use double-buffering. It requires double the vertex storage but avoids the stall when the resource you want to modify is in the command buffer waiting to be used by the GPU.


L. Spiro
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#12 LM-Crashy   Members   -  Reputation: 470

Like
0Likes
Like

Posted 11 August 2012 - 03:41 AM

Sorry for the long delays, a lot of other problems to resolve :)

CopySubresourceRegion is already used to copy from one buffer to another in Ogre D3D11 implementation. However, when the size to copy is equal to the size of the destination buffer, CopySubresource is used.
I think that CopyResource is as fast as CopySubresourceRegion when copying the whole buffer, isn't it? It would be really weird if it wasn't.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS