Jump to content
  • Advertisement
Sign in to follow this  
Kamil9132

Updating dynamic vertex buffer

This topic is 1418 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
Advertisement

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

@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 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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!