Search code examples
c++dynamicbufferdirectx-11vertex-buffer

Dynamic Constantbuffer or Dynamic Vertex Buffer in c++ and DX11


I have a question realted to meemory usage by using

Dynamic ConstantBuffer vs Constant Buffer updated frequentlky(Using Defualt usagetype) vs Dynamic Vertex Buffer

I always Defined Constnat buffer usage as defualt and updated the changes in realtime like so

Eg 1

D3D11_BUFFER_DESC desc;
desc.Usage = D3D11_USAGE_DEFAULT;
// irrelevant code omitted

void Render()
{
WORLD = XMMatrixTranslation(x,y,z); // x,y,z are chaned dynamiclly
 ConstantBuffer cb;
 cb.world = WORLD;

devcon->UpdateSubResource(constantBuffer,0,0,&cb,0,0);

// Set the VSSetBuffer and PSSetBuffer
}

But Recently I came across a tutorial from rastertek that used devcon->Map() and devcon->Unmap() to update them and he had defined the usage as Dynamic

Eg 2

void CreateBuffer(){
D3D11_BUFFER_DESC desc;
desc.Usage = D3D11_USAGE_DYNAMIC; // irrelkavant code ommited
}
void Render()
{
 WORLD = XMMatrixTranslation(x,y,z); // x,y,z are chaned dynamiclly
 D3D11_MAPPED_SUBRESOURCE mappedRes;
 ConstantBuffer *cbPtr;

devcon->Map(constantBuffer,0,D3D11_MAP_WRITE_DISCARD,0,&mappedRes);
 cbPtr = (ConstantBuffer*)mappedRes.pData;
 cbPtr->World = WORLD;
devcon->UnMap(constantBuffer,0);
}

So the question is .

Is there any performance gain or hit by using Dynamic Constant buffer(eg2) over the the default ConstatnBuffer updatee at runtim(eg1) ??

Please do help me clear this doubt..

Thanks


Solution

  • The answer here like most performance advice is "It depends". Both are valid and it really depends on your content and rendering pattern.

    The classic performance reference here is Windows to Reality: Getting the Most out of Direct3D 10 Graphics in Your Games from Gamefest 2007.

    If you are dealing with lots of constants, Map of a DYNAMIC Constant Buffer is better if your data is scattered about and is collected as part of the update cycle. If all your constants are already laid out correctly in system memory, then UpdateSubResource is probably better. If you are reusing the same CB many times a frame and Map/Locking it, then you might run into 'rename' limits with Map/Lock that are less problematic with UpdateSuResource so "it depends' is really the answer here.

    And of course, all of this goes out the window with DirectX 12 which has an entirely different mechanism for handling the equivalent of dynamic updates.