I have an effect to be used on several objects. Is it ok to have one buffer object that you can update between rendering? Or is this bad for performance? I read here that mapping resources before the command buffer is finished with them will hurt performance.
Instead is it better to have one buffer object (ID3D10Buffer in this case) for each object to be rendered by this effect?
Below is a simplified exaple of what I mean, I hope it makes sence
This code is using one buffer object (ID3D10Buffer) which it initiates with the WVP matrix of the first scene object. It then renders the first scene object. Then it updates the buffer and renders the second scene object. If the command buffer is not finished with the buffer when I map it, will that cause a stall? Or is this handled behind the scene since I'm using D3D10_MAP_WRITE_DISCARD?
// create effect and technique, etc....
// Matrix structures
Matrix mWVP1 = GetWVPmatrix(my3Dobj1);
Matrix mWVP2 = GetWVPmatrix(my3Dobj2);
// Retrieve the constant buffer
ID3D10EffectConstantBuffer *pCBuf = pFX->GetConstantBufferByName(“perObj”);
// Create a buffer
cbDesc.Usage = D3D10_USAGE_DYNAMIC;
cbDesc.BindFlags = D3D10_BIND_CONSTANT_BUFFER;
cbDesc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
cbDesc.MiscFlags = 0;
cbDesc.ByteWidth = sizeof(Matrix);
initData.SysMemPitch = 0;
initData.SysMemSlicePitch = 0;
initData.pSysMem = reinterpret_cast<void*>(&mWVP1);
deviceObj->CreateBuffer(&cbDesc, &initData, &pBuf);
// Assign pBuf to the constant buffer
// Render my3Dobj1....
// ...rendering my3Dobj1 done!
// Update pBuf with my3Dobj2s WVP matrix
void *memBuf = NULL;
if(S_OK == pBuf->Map(D3D10_MAP_WRITE_DISCARD, 0, &memBuf)
memcpy_s(memBuf, sizeof(Matrix), reinterpret_cast<void*>(&mWVP2), sizeof(Matrix));
// Assign pBuf to the constant buffer again
// Render my3Dobj2....
// ...rendering my3Dobj2done!