Microsoft的HelloWorld示例大多使用单个CommandAllocator,然后等到前一帧完全完成。然而,他们也说(在所有大写字母中)这不是应该这样做的方式。
所以我的想法是在交换链中为每帧创建一个分配器,并将要等待的栅栏值保留在循环缓冲区中:
struct frame_resources{
ID3D12Resource* renderTarget;
ID3D12CommandAllocator* allocator;
uint64 fenceValue;
} resources[FRAME_COUNT];
uint frameIndex = swapChain->GetCurrentBackBufferIndex();
UINT64 lastFence;
MSG msg;
ZeroMemory(&msg, sizeof(msg));
while (msg.message != WM_QUIT)
{
if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
continue;
}
if (fence->GetCompletedValue() < resources[frameIndex].fenceValue)
{
fence->SetEventOnCompletion(resources[frameIndex].fenceValue, fenceEvent);
DWORD result = MsgWaitForMultipleObjects(1, &fenceEvent, FALSE, INFINITE, QS_ALLEVENTS);
if(result == WAIT_OBJECT_0 + 1)
continue; //message in the queue
}
resources[frameIndex].allocator->Reset();
commandList->Reset(resources[frameIndex].allocator, 0);
//...
commandList->Close();
commandQueue->ExecuteCommandLists(1, &CommandList);
lastFence++;
resources[frameIndex].fenceValue = lastFence;
commandQueue->Signal(fence, lastFence);
swapChain->Present(0, DXGI_PRESENT_RESTART);
frameIndex = swapChain->GetCurrentBackBufferIndex();
}
这是一种理智的方法吗?还是有更好的方法?
你现在可能已经找到了答案,但我会回答以防万一。当 GPU 可能正在执行存储在与命令分配器关联的内存中的命令列表时,您无法重置命令分配器,因此您的方法是正确的,您需要为每个帧缓冲区使用单独的命令分配器。
另一方面,命令列表可以在调用 ExecuteCommandLists() 后立即重置。这意味着,虽然每帧必须有一个命令分配器,但每个"线程"只需要一个命令列表。
命令列表一次只能由一个线程填充,这意味着您需要为每个线程提供一个命令列表来填充命令列表。
因此,您需要具有numFrames*numThreads命令分配器和numThreads命令列表