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

This topic is 2444 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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:
Device.ImmediateContext.UpdateSubresource(VertexArray, VertexBuffer);
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 on other sites

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:
Device.ImmediateContext.UpdateSubresource(VertexArray, VertexBuffer);
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)?

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 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 on other sites
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.

##### Share on other sites

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.

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 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

Thanks again.

• 34
• 12
• 10
• 9
• 9
• ### Forum Statistics

• Total Topics
631355
• Total Posts
2999513
×