# Updating dynamic vertex buffer

## Recommended Posts

Kamil9132    135

Hi,

I want to update dynamic vertex buffer.

I tried update this buffer in 2 ways, but in first i got error and in second buffer has not changed.

1.

D3D11_MAPPED_SUBRESOURCE VertexMap;

ZeroMemory (&VertexMap, sizeof(D3D11_MAPPED_SUBRESOURCE));

d3d11DevCon->Map (WorldDX->VertexBuff[K], 0, D3D11_MAP_WRITE, 0, &VertexMap);
memcpy (VertexMap.pData, &WorldDX->WM[WorldDX->wK][K][0], sizeof (INS) * WorldDX->WM[WorldDX->wK][K].size()); //error

d3d11DevCon->Unmap(WorldDX->VertexBuff[K], 0);


2.

d3d11DevCon->Map (WorldDX->VertexBuff[K], 0, D3D11_MAP_WRITE, 0, &VertexMap);
VertexMap.pData = &WorldDX->WM[WorldDX->wK][K][0];
d3d11DevCon->Unmap(WorldDX->VertexBuff[K], 0);


I can destroy and create this buffer but it takes too much time:

D3D11_BUFFER_DESC		bd;
D3D11_SUBRESOURCE_DATA	InitData;

WorldDX->VertexSize[K] = WorldDX->WM[WorldDX->wK][K].size();

if (WorldDX->VertexBuff[K]){

WorldDX->VertexBuff[K]->Release();
WorldDX->VertexBuff[K] = NULL;

}

ZeroMemory (&bd,		sizeof(bd));
ZeroMemory (&InitData,	sizeof(InitData));

bd.Usage = D3D11_USAGE_DYNAMIC;
bd.ByteWidth = sizeof (INS) * WorldDX->VertexSize[K];
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;

InitData.pSysMem = &WorldDX->WM[WorldDX->wK][K][0];
d3d11Device->CreateBuffer (&bd, &InitData, &WorldDX->VertexBuff[K]);



##### Share on other sites
HappyCoder    5052

The first way of doing it is the right way, but what is the error message you are getting? Are you trying to change the size of the vertex buffer or just the contents?

Edited by HappyCoder

##### Share on other sites
Erik Rufelt    5901

Use D3D11_MAP_WRITE_DISCARD instead, if you're replacing the contents of the entire buffer.

##### Share on other sites
Kamil9132    135

@HappyCoder

I got unhandled exception at...

I changed D3D11_MAP_WRITE to D3D11_MAP_WRITE_DISCARD and now it's working properly.

Thank you for help.

##### Share on other sites
Erik Rufelt    5901

Map() returns a HRESULT value, which failed when you didn't use discard, meaning the buffer wasn't mapped and the exception arose when trying to memcpy to an invalid address in pData (NULL, as you ZeroMemory it).

Discard-write means that whatever was in the buffer before is gone after mapping, which is generally faster and better, but ofcourse it fails if you want to replace just 10 vertices in a larger buffer that contains 100 vertices, for example.

If you want to do reasonably rare partial updates of a buffer (not every frame), use DEFAULT usage and UpdateSubresource to update the buffer instead of mapping a dynamic resource.

I wasn't able to find the reference page for it right now, but I doubt you can map dynamic vertex-buffers without discard... as it would require read CPU-access for the driver to know what part of the buffer you happened to over-write while the resource was mapped (or at least you would need to specify read-access for the CPU when creating the buffer, which is a bad idea for performance).