# (C++, DirectX 11, UWP) - How do I update the ByteWidth of a VertexBuffer without recreating it?

I want to have one buffer used for all dynamic properties but the amount of vertices in this could vary from just a few for an interface to the whole scene. As such if it's becomes initialized with only let's say the below.

offset = 120; //example situation.
bd.ByteWidth = sizeof(VertexPositionColorTexture) * offset;


The above is 120 vertices, but if let's say the next time when I go to update. I need 160 vertices the buffer will only have space for 120 and will do one of the following.

1. Crash with an access exception.
2. Draw but it'll be missing those 40 excess vertices.

I update the Vertex Buffer by doing the following:

D3D11_MAPPED_SUBRESOURCE resource;
ZeroMemory(&resource, sizeof(D3D11_MAPPED_SUBRESOURCE));

Engine::context->Map(dynamic_interfaces_buffer.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &resource);

memcpy(resource.pData, &vertsToDraw, sizeof(VertexPositionColorTexture) * offset);

Engine::context->Unmap(dynamic_interfaces_buffer.Get(), 0);


The "offset" value changes with what ever data ends up being present. I update the existing "vertsToDraw" data as needed.

So if the title didn't make sense. I'm looking to edit the ByteWidth value that what stored in the ByteWidth of the description when the buffer was originally created. My only solution so far was to set the buffer to the maximum size I feel I would ever need then set the VertsToDraw array size to that same value. However then when I update I only update with the needed amount. I'm not sure if that causes a performance issue by having a buffer with a byteWidth of "32 Bytes" * "36,000 Vertices". When in many cases I might only use a few 1000 or less.

Another thought I had if the above is the only way was to create multiple buffer of various sizes and use which ever one fits the situation the best.

E.g. if it's 3828 vertices use a 4000 pre defined vertices buffer or if it's 18348 vertices use a 19000 pre defined buffer.

But i'm up for any solution if there's a better way to go about this i'm a bit new to DirectX.

• Remember, ByteWidth isn't just a value that's stored with your buffer. It's the size of the memory allocated for your buffer itself. Changing that number means allocating a new buffer of a different size. – DMGregory Jan 1 at 7:59
• Hmm alright thanks I think i'll just use the maximum size i'd need and possible make 2 buffers one for situations under 2000 vertices and one for anything else possibly up to 10,000 - 20,000 not sure yet still got to learn the limits on draw call ability. The allocating memory makes sense why if I created a new buffer instead of mapping it. I get a memory access violation after so many calls to that. I was thinking it was creating memory and not removing it that's why I switched to this map/unmap stuff. – Jeremy Trifilo Jan 1 at 22:05