Sign in to follow this  
wuppy

Fastest way to update vertex buffers? (DirectX, SharpDX)

Recommended Posts

Ok, so for some reason, my code for updating a vertex buffer of 1,000,0000 vertices every frame flies smoothly with 200fps with XNA, but with SharpDX it gets stuck below 10.

In SharpDX, I'm trying to use UpdateSubresource like this:
[code]Device.ImmediateContext.UpdateSubresource(VertexArray, VertexBuffer);[/code]
It renders correctly.
If I comment this line out, the FPS skyrockets, so I'm sure this is the bottleneck.

Actually, if I just create a new buffer every frame, I get roughly the same performance.


I can't figure out what I'm doing wrong. Have been searching after this and tried different ways without success.
Could someone please tell me a fast way to update a vertex buffer in SharpDX (or DirectX11)?

Share this post


Link to post
Share on other sites
[quote name='wuppy' timestamp='1317812964' post='4869347']
Ok, so for some reason, my code for updating a vertex buffer of 1,000,0000 vertices every frame flies smoothly with 200fps with XNA, but with SharpDX it gets stuck below 10.

In SharpDX, I'm trying to use UpdateSubresource like this:
[code]Device.ImmediateContext.UpdateSubresource(VertexArray, VertexBuffer);[/code]
It renders correctly.
If I comment this line out, the FPS skyrockets, so I'm sure this is the bottleneck.

Actually, if I just create a new buffer every frame, I get roughly the same performance.


I can't figure out what I'm doing wrong. Have been searching after this and tried different ways without success.
Could someone please tell me a fast way to update a vertex buffer in SharpDX (or DirectX11)?
[/quote]

As far as I know, UpdateSubresource in most of the cases isn't the fastest way to perform update.

You could try using dynamic buffer (with CPU access) with Map and memcpy (or equivalent to the language you are using).

Cheers!

Share this post


Link to post
Share on other sites
Another option for copying data into a default resource is to use an auxiliary staging buffer. Staging resources are read/write by the CPU. You fill the staging buffer with your data (map/unmap API) then use the CopyResource API to copy the contents of the staging buffer to your vertex buffer. If you're updating the whole thing, CopyResource, if not CopySubresourceRegion. The reverse is also possible, for reading back data from a default-usage vertex buffer.

This is the method I use for my vertex buffers, where the staging buffer is created lazily only once (first Get or SetData call) and kept around thereafter. So setting initial data in the constructor and not touching the buffer contents would not create an extra resources. I *believe* this is similar to how XNA works under the hood, but I can't say for certain.

Of course, if you're updating a vertex buffer every frame then you're going to want to look into a dynamic resources, as they are meant for that sort of usage in the first place. Updating default resources should only happen infrequently.

Share this post


Link to post
Share on other sites
[quote name='MJP' timestamp='1317839873' post='4869489']
Just create it as DYNAMIC, and map it with MAP_WRITE_DISCARD. The whole point of DYNAMIC is for resources whose contents you completely update every frame.
[/quote]

Are you sure?.... because most of the time I'm using Updatesubresource as it is much faster, and making a quick test with a 28Mo vertex buffer, Updatesubresource is 50% faster then using dynamic/MAP_WRITE_DISCARD...

@wuppy, don't know much what's going wrong with your sample, as it is difficult to investigate from a single line of code. You should probably make a simple example (using for example Direct3D11/MiniCube) as I just did: on my system, everything is running at 200fps (using map/unmap) to 300fps (using updatesubresource)

Share this post


Link to post
Share on other sites
Thank you everyone for the answers. I needed to switch to dynamic buffers.
This is strange, because in XNA, switching between static and dynamic only resulted in an around 2-3x performance difference at most, but in SharpDX, it made rendeing over 100 times faster.

@Alexandre: Yeah, I stick with using Updatesubresource in the end.

BTW your work is great, and I'm really surprised you answered me here, I don't understand how you have the time for all this [img]http://public.gamedev.net/public/style_emoticons/default/smile.gif[/img]

Thanks again.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this