Sign in to follow this  

Updating dynamic vertex buffer

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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


Link to post
Share on other sites

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


Link to post
Share on other sites

Some more info about the reason behind the error..

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

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

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