[D3D12] The number of rendered objects and the number of constant buffer views

Started by
2 comments, last by Shigeo.K 7 years, 2 months ago

Hi everyone,
The sample of the SDK renders a single triangle per frame.
I want to render multiple objects per frame (of course, it is fine if there is only one vertex buffer).

Before Dx11, it was possible to implement the rendering of a single object or of multiple objects by simply increasing the loop counter.
In Dx12, however, it seems that this is no longer the case.
Through trial and error, I was finally able to figure out how to do it!

However, I have concerns about whether it is a suitable solution.

For now, I am implementing multiple renderings by creating multiple CBVs.
For example, if I want to render 5 objects, I create 5 CBVs, one for each.


****Initializing Phase****
for (int i = 0; i < 5; i++)
{
	cbvDesc.BufferLocation = g_constantBuffer->GetGPUVirtualAddress()+i*CBSize;	
	D3D12_CPU_DESCRIPTOR_HANDLE cHandle = g_pCbvHeap->GetCPUDescriptorHandleForHeapStart();			
	cHandle.ptr += i*Stride;
	g_pDevice->CreateConstantBufferView(&cbvDesc, cHandle);
}

****Draw Phase****

//Update constant buffer
for (int i = 0; i<5; i++)
{	
	char* ptr = reinterpret_cast<char*>(g_pCbvDataBegin)+256*i;		
			
(some matrix oparation...)

	g_constantBufferData.mWVP = mWVP;
	memcpy(ptr, &g_constantBufferData, sizeof(g_constantBufferData));
}

//Draw
for(int i=0;i<5;i++)
{
	cbvSrvUavDescHeap.ptr += i * size;
	g_pCommandList->SetGraphicsRootDescriptorTable(0, cbvSrvUavDescHeap);
        g_pCommandList->DrawInstanced(3, 1, 0, 0);
}

.

Something about this solution feels ‘off’, so I wanted to see what you all think.
Is this the right way to handle this problem?

Thanks!

Best regards,

Advertisement
D3D12 does not handle lifetime for you. Any memory you initialize from the cpu need to survive until the gpu is done with it, using fences after your command lists to track completion.

Usually, you allocate a memory block and advance in it allocating in a ring buffer fashion anytime you need to store something (constants, geometry, captain age).
D3D11 drivers handled dynamic buffers through a process known as "versioning". Basically you see 1 buffer at an API level, but behind the scenes the driver switches to a new buffer allocation every time that you call Map. This allows the driver to fill the GPU-visible memory a frame or more before the GPU actually executes it (somewhat similar to how you're doing it in D3D12), while presenting a user-facing API that makes it look as though the buffer update happens synchronously with the draw calls or dispatches. In practice doing this was rather tricky for drivers, and added a lot of overhead. It essentially combined the typical problems of a memory allocator with the additional complexities of tracking whether the GPU was still reading from a particular piece of memory. It also required tracking tons of state behind the scenes, since you could Map a buffer that was already bound to the pipeline and the driver had to handle that transparently.

As galop1n already explained, you're totally on your own with this in D3D12. If you want similar facilities to what you had in D3D11 you'll need to implement them yourself. The good news is that you can create simple layers that are exactly tailored for your use case, which can let you do things much more efficiently than a D3D11 driver could. For instance, in our engine we support the idea of "temporary" dynamic buffers. The contents of these buffers don't persist from one frame to another, which means that the backing buffer memory can be sub-allocated from a larger buffer that's tied to a single GPU frame. So at the end of every frame we can swap buffers and say "the entire contents of this buffer are no longer being used, and we don't care because it was temporary". This makes versioning as cheap as incrementing a pointer, as long as we're willing to dedicated enough memory to hold 2x the number of temporary allocations. Alternatively you can use a ring buffer as galo1n suggested, and move forward the low watermark when a fence is signaled. This could allow you to be more efficient with your memory usage, while making the versioning process a bit more complicated. Really my point is that there's many approaches that you could take, so you should experiment to find something that works for your use cases.

Also, I wanted to point out that root constant buffer views can make allocating temporary constant buffers very easy:


void* tempCBMemCPUAddr = BigTempBufferCPUAddr + BigTempBufferUsed;
uint64_t tempCBMemGPUAddr = BigTempBufferGPUAddr + BigTempBufferUsed;
BigTempBufferUsed += cbSize;
memcpy(tempCBMemCPUAddr, cbData, cbSize);
cmdList->SetGraphicsRootConstantBufferView(cbRootParam, tempCBMemGPUAddr);
you see 1 buffer at an API level

That clears up everything. Thanks!

D3D12 does not handle lifetime for you.

And thank you for the confirmation.

I was confident that my procedure was one of several correct approaches.
I also realized that there are several other approaches to achieve the same result.

By increasing the degree of freedom of the API, we increased our responsibility, but there are more options for programmers than with Dx11.As a API user, I think that this is a good trend.

I don’t entirely understand the “temporary buffer” you mentioned, but I will do my best to figure it out.
I’m sure it is a useful technique.

Thank you for showing me the snippet of the source code.
I will do my best to understand it.

It also seems that I misunderstood the notion.And also the thread title.
In actuality, I prepare multiple CB memory blocks, not multiple CBVs.

This topic is closed to new replies.

Advertisement