move frame resource transitions to D3D12Binding

This commit is contained in:
Austin Eng 2017-06-07 15:11:52 -04:00 committed by Corentin Wallez
parent 39c1bce62d
commit 81bc3ad23b
4 changed files with 120 additions and 65 deletions

View File

@ -31,7 +31,7 @@ namespace backend {
namespace d3d12 { namespace d3d12 {
void Init(ComPtr<ID3D12Device> d3d12Device, nxtProcTable* procs, nxtDevice* device); void Init(ComPtr<ID3D12Device> d3d12Device, nxtProcTable* procs, nxtDevice* device);
ComPtr<ID3D12CommandQueue> GetCommandQueue(nxtDevice device); ComPtr<ID3D12CommandQueue> GetCommandQueue(nxtDevice device);
void SetNextRenderTarget(nxtDevice device, ComPtr<ID3D12Resource> renderTargetResource, D3D12_CPU_DESCRIPTOR_HANDLE renderTargetDescriptor); void SetNextRenderTargetDescriptor(nxtDevice device, D3D12_CPU_DESCRIPTOR_HANDLE renderTargetDescriptor);
} }
} }
@ -92,7 +92,6 @@ class D3D12Binding : public BackendBinding {
&swapChain1 &swapChain1
)); ));
ASSERT_SUCCESS(swapChain1.As(&swapChain)); ASSERT_SUCCESS(swapChain1.As(&swapChain));
frameIndex = swapChain->GetCurrentBackBufferIndex();
// Describe and create a render target view (RTV) descriptor heap. // Describe and create a render target view (RTV) descriptor heap.
D3D12_DESCRIPTOR_HEAP_DESC rtvHeapDesc = {}; D3D12_DESCRIPTOR_HEAP_DESC rtvHeapDesc = {};
@ -103,48 +102,129 @@ class D3D12Binding : public BackendBinding {
rtvDescriptorSize = d3d12Device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV); rtvDescriptorSize = d3d12Device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
// Create a RTV for each frame. // Create a RTV and command allocators for each frame.
{
D3D12_CPU_DESCRIPTOR_HANDLE renderTargetViewHandle = renderTargetViewHeap->GetCPUDescriptorHandleForHeapStart(); D3D12_CPU_DESCRIPTOR_HANDLE renderTargetViewHandle = renderTargetViewHeap->GetCPUDescriptorHandleForHeapStart();
for (uint32_t n = 0; n < kFrameCount; ++n) { for (uint32_t n = 0; n < kFrameCount; ++n) {
ASSERT_SUCCESS(swapChain->GetBuffer(n, IID_PPV_ARGS(&renderTargetResources[n]))); ASSERT_SUCCESS(swapChain->GetBuffer(n, IID_PPV_ARGS(&renderTargetResources[n])));
d3d12Device->CreateRenderTargetView(renderTargetResources[n].Get(), nullptr, renderTargetViewHandle); d3d12Device->CreateRenderTargetView(renderTargetResources[n].Get(), nullptr, renderTargetViewHandle);
renderTargetViewHandle.ptr += rtvDescriptorSize; renderTargetViewHandle.ptr += rtvDescriptorSize;
ASSERT_SUCCESS(d3d12Device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&commandAllocators[n])));
}
} }
ASSERT_SUCCESS(d3d12Device->CreateFence(fenceValues[frameIndex], D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence))); // Get the initial render target and arbitrarily choose a "previous" render target that's different
fenceValues[frameIndex]++; previousRenderTargetIndex = renderTargetIndex = swapChain->GetCurrentBackBufferIndex();
previousRenderTargetIndex = renderTargetIndex == 0 ? 1 : 0;
// Initialize the current frame, the last completed frame, and all last frame each render target was used to 0
// so that it looks like we completed all previous frames
currentFrameNumber = 0;
for (uint32_t n = 0; n < kFrameCount; ++n) {
lastFrameRenderTargetWasUsed[n] = 0;
}
ASSERT_SUCCESS(d3d12Device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence)));
fenceEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr); fenceEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
ASSERT(fenceEvent != nullptr); ASSERT(fenceEvent != nullptr);
SetNextRenderTarget(); // Transition the first frame to be a render target
{
ASSERT_SUCCESS(d3d12Device->CreateCommandList(
0,
D3D12_COMMAND_LIST_TYPE_DIRECT,
commandAllocators[previousRenderTargetIndex].Get(),
nullptr,
IID_PPV_ARGS(&commandList)
));
D3D12_RESOURCE_BARRIER resourceBarrier;
resourceBarrier.Transition.pResource = renderTargetResources[renderTargetIndex].Get();
resourceBarrier.Transition.StateBefore = D3D12_RESOURCE_STATE_PRESENT;
resourceBarrier.Transition.StateAfter = D3D12_RESOURCE_STATE_RENDER_TARGET;
resourceBarrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
resourceBarrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
resourceBarrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
commandList->ResourceBarrier(1, &resourceBarrier);
ASSERT_SUCCESS(commandList->Close());
ID3D12CommandList* commandLists[] = { commandList.Get() };
commandQueue->ExecuteCommandLists(_countof(commandLists), commandLists);
} }
D3D12_CPU_DESCRIPTOR_HANDLE renderTargetViewHandle = renderTargetViewHeap->GetCPUDescriptorHandleForHeapStart();
renderTargetViewHandle.ptr += rtvDescriptorSize * renderTargetIndex;
backend::d3d12::SetNextRenderTargetDescriptor(backendDevice, renderTargetViewHandle);
}
void SwapBuffers() override { void SwapBuffers() override {
// Present the rendered frame // Transition current frame's render target for presenting
{
ASSERT_SUCCESS(commandList->Reset(commandAllocators[renderTargetIndex].Get(), nullptr));
D3D12_RESOURCE_BARRIER resourceBarrier;
resourceBarrier.Transition.pResource = renderTargetResources[renderTargetIndex].Get();
resourceBarrier.Transition.StateBefore = D3D12_RESOURCE_STATE_RENDER_TARGET;
resourceBarrier.Transition.StateAfter = D3D12_RESOURCE_STATE_PRESENT;
resourceBarrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
resourceBarrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
resourceBarrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
commandList->ResourceBarrier(1, &resourceBarrier);
ASSERT_SUCCESS(commandList->Close());
ID3D12CommandList* commandLists[] = { commandList.Get() };
commandQueue->ExecuteCommandLists(_countof(commandLists), commandLists);
}
ASSERT_SUCCESS(swapChain->Present(1, 0)); ASSERT_SUCCESS(swapChain->Present(1, 0));
const uint64_t currentFenceValue = fenceValues[frameIndex]; // Transition last frame's render target back to being a render target
ASSERT_SUCCESS(commandQueue->Signal(fence.Get(), currentFenceValue)); {
ASSERT_SUCCESS(commandList->Reset(commandAllocators[renderTargetIndex].Get(), nullptr));
D3D12_RESOURCE_BARRIER resourceBarrier;
resourceBarrier.Transition.pResource = renderTargetResources[previousRenderTargetIndex].Get();
resourceBarrier.Transition.StateBefore = D3D12_RESOURCE_STATE_PRESENT;
resourceBarrier.Transition.StateAfter = D3D12_RESOURCE_STATE_RENDER_TARGET;
resourceBarrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
resourceBarrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
resourceBarrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
commandList->ResourceBarrier(1, &resourceBarrier);
ASSERT_SUCCESS(commandList->Close());
ID3D12CommandList* commandLists[] = { commandList.Get() };
commandQueue->ExecuteCommandLists(_countof(commandLists), commandLists);
}
ASSERT_SUCCESS(commandQueue->Signal(fence.Get(), currentFrameNumber));
frameIndex = swapChain->GetCurrentBackBufferIndex(); // Advance to the next frame
currentFrameNumber++;
SetNextRenderTarget(); previousRenderTargetIndex = renderTargetIndex;
renderTargetIndex = swapChain->GetCurrentBackBufferIndex();
// If the next frame is not ready to be rendered yet, wait until it is ready. // If the next render target is not ready to be rendered yet, wait until it is ready.
if (fence->GetCompletedValue() < fenceValues[frameIndex]) { // If the last completed frame is less than the last requested frame for this render target,
ASSERT_SUCCESS(fence->SetEventOnCompletion(fenceValues[frameIndex], fenceEvent)); // then the commands previously executed on this render target have not yet completed
const uint64_t lastFrameCompletedOnGPU = fence->GetCompletedValue();
if (lastFrameCompletedOnGPU < lastFrameRenderTargetWasUsed[renderTargetIndex]) {
ASSERT_SUCCESS(fence->SetEventOnCompletion(lastFrameRenderTargetWasUsed[renderTargetIndex], fenceEvent));
WaitForSingleObjectEx(fenceEvent, INFINITE, FALSE); WaitForSingleObjectEx(fenceEvent, INFINITE, FALSE);
} }
// Set the fence value for the next frame. lastFrameRenderTargetWasUsed[renderTargetIndex] = currentFrameNumber;
fenceValues[frameIndex] = currentFenceValue + 1;
// The block above checked that the commands in this allocator are done executing
ASSERT_SUCCESS(commandAllocators[renderTargetIndex]->Reset());
// Tell the backend to render to the current render target
D3D12_CPU_DESCRIPTOR_HANDLE renderTargetViewHandle = renderTargetViewHeap->GetCPUDescriptorHandleForHeapStart();
renderTargetViewHandle.ptr += rtvDescriptorSize * renderTargetIndex;
backend::d3d12::SetNextRenderTargetDescriptor(backendDevice, renderTargetViewHandle);
} }
private: private:
nxtDevice backendDevice = nullptr; nxtDevice backendDevice = nullptr;
static constexpr int kFrameCount = 2; static constexpr unsigned int kFrameCount = 2;
uint32_t frameIndex = 0;
uint32_t rtvDescriptorSize; // Initialization
ComPtr<IDXGIFactory4> factory; ComPtr<IDXGIFactory4> factory;
ComPtr<IDXGIAdapter1> hardwareAdapter; ComPtr<IDXGIAdapter1> hardwareAdapter;
ComPtr<ID3D12Device> d3d12Device; ComPtr<ID3D12Device> d3d12Device;
@ -152,16 +232,18 @@ class D3D12Binding : public BackendBinding {
ComPtr<IDXGISwapChain3> swapChain; ComPtr<IDXGISwapChain3> swapChain;
ComPtr<ID3D12DescriptorHeap> renderTargetViewHeap; ComPtr<ID3D12DescriptorHeap> renderTargetViewHeap;
ComPtr<ID3D12Resource> renderTargetResources[kFrameCount]; ComPtr<ID3D12Resource> renderTargetResources[kFrameCount];
uint64_t fenceValues[kFrameCount] = { 0, 0 }; uint32_t rtvDescriptorSize;
// Frame synchronization. Updated every frame
uint32_t renderTargetIndex;
uint32_t previousRenderTargetIndex;
uint64_t currentFrameNumber;
uint64_t lastFrameRenderTargetWasUsed[kFrameCount];
ComPtr<ID3D12CommandAllocator> commandAllocators[kFrameCount];
ComPtr<ID3D12GraphicsCommandList> commandList;
ComPtr<ID3D12Fence> fence; ComPtr<ID3D12Fence> fence;
HANDLE fenceEvent; HANDLE fenceEvent;
void SetNextRenderTarget() {
D3D12_CPU_DESCRIPTOR_HANDLE renderTargetViewHandle = renderTargetViewHeap->GetCPUDescriptorHandleForHeapStart();
renderTargetViewHandle.ptr += rtvDescriptorSize * frameIndex;
backend::d3d12::SetNextRenderTarget(backendDevice, renderTargetResources[frameIndex], renderTargetViewHandle);
}
static void ASSERT_SUCCESS(HRESULT hr) { static void ASSERT_SUCCESS(HRESULT hr) {
assert(SUCCEEDED(hr)); assert(SUCCEEDED(hr));
} }

View File

@ -57,17 +57,7 @@ namespace d3d12 {
D3D12_RECT scissorRect = { 0.f, 0.f, width, height }; D3D12_RECT scissorRect = { 0.f, 0.f, width, height };
commandList->RSSetViewports(1, &viewport); commandList->RSSetViewports(1, &viewport);
commandList->RSSetScissorRects(1, &scissorRect); commandList->RSSetScissorRects(1, &scissorRect);
commandList->OMSetRenderTargets(1, &device->GetCurrentRenderTargetDescriptor(), FALSE, nullptr);
// TODO(enga@google.com): Set the back buffer as the render target only when a new render target is set
D3D12_RESOURCE_BARRIER resourceBarrier;
resourceBarrier.Transition.pResource = device->GetNextRenderTarget().Get();
resourceBarrier.Transition.StateBefore = D3D12_RESOURCE_STATE_PRESENT;
resourceBarrier.Transition.StateAfter = D3D12_RESOURCE_STATE_RENDER_TARGET;
resourceBarrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
resourceBarrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
resourceBarrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
commandList->ResourceBarrier(1, &resourceBarrier);
commandList->OMSetRenderTargets(1, &device->GetNextRenderTargetDescriptor(), FALSE, nullptr);
} }
break; break;
@ -109,16 +99,6 @@ namespace d3d12 {
case Command::EndRenderPass: case Command::EndRenderPass:
{ {
EndRenderPassCmd* cmd = commands.NextCommand<EndRenderPassCmd>(); EndRenderPassCmd* cmd = commands.NextCommand<EndRenderPassCmd>();
// TODO(enga@google.com): Present the back buffer only before swap
D3D12_RESOURCE_BARRIER resourceBarrier;
resourceBarrier.Transition.pResource = device->GetNextRenderTarget().Get();
resourceBarrier.Transition.StateBefore = D3D12_RESOURCE_STATE_RENDER_TARGET;
resourceBarrier.Transition.StateAfter = D3D12_RESOURCE_STATE_PRESENT;
resourceBarrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
resourceBarrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
resourceBarrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
commandList->ResourceBarrier(1, &resourceBarrier);
} }
break; break;

View File

@ -37,9 +37,9 @@ namespace d3d12 {
return backendDevice->GetCommandQueue(); return backendDevice->GetCommandQueue();
} }
void SetNextRenderTarget(nxtDevice device, ComPtr<ID3D12Resource> renderTargetResource, D3D12_CPU_DESCRIPTOR_HANDLE renderTargetDescriptor) { void SetNextRenderTargetDescriptor(nxtDevice device, D3D12_CPU_DESCRIPTOR_HANDLE renderTargetDescriptor) {
Device* backendDevice = reinterpret_cast<Device*>(device); Device* backendDevice = reinterpret_cast<Device*>(device);
backendDevice->SetNextRenderTarget(renderTargetResource, renderTargetDescriptor); backendDevice->SetNextRenderTargetDescriptor(renderTargetDescriptor);
} }
void ASSERT_SUCCESS(HRESULT hr) { void ASSERT_SUCCESS(HRESULT hr) {
@ -64,16 +64,11 @@ namespace d3d12 {
return commandQueue; return commandQueue;
} }
ComPtr<ID3D12Resource> Device::GetNextRenderTarget() { D3D12_CPU_DESCRIPTOR_HANDLE Device::GetCurrentRenderTargetDescriptor() {
return renderTargetResource;
}
D3D12_CPU_DESCRIPTOR_HANDLE Device::GetNextRenderTargetDescriptor() {
return renderTargetDescriptor; return renderTargetDescriptor;
} }
void Device::SetNextRenderTarget(ComPtr<ID3D12Resource> renderTargetResource, D3D12_CPU_DESCRIPTOR_HANDLE renderTargetDescriptor) { void Device::SetNextRenderTargetDescriptor(D3D12_CPU_DESCRIPTOR_HANDLE renderTargetDescriptor) {
this->renderTargetResource = renderTargetResource;
this->renderTargetDescriptor = renderTargetDescriptor; this->renderTargetDescriptor = renderTargetDescriptor;
} }

View File

@ -104,10 +104,9 @@ namespace d3d12 {
ComPtr<ID3D12Device> GetD3D12Device(); ComPtr<ID3D12Device> GetD3D12Device();
ComPtr<ID3D12CommandQueue> GetCommandQueue(); ComPtr<ID3D12CommandQueue> GetCommandQueue();
ComPtr<ID3D12Resource> GetNextRenderTarget(); D3D12_CPU_DESCRIPTOR_HANDLE GetCurrentRenderTargetDescriptor();
D3D12_CPU_DESCRIPTOR_HANDLE GetNextRenderTargetDescriptor();
void SetNextRenderTarget(ComPtr<ID3D12Resource> renderTargetResource, D3D12_CPU_DESCRIPTOR_HANDLE renderTargetDescriptor); void SetNextRenderTargetDescriptor(D3D12_CPU_DESCRIPTOR_HANDLE renderTargetDescriptor);
// NXT API // NXT API
void Reference(); void Reference();
@ -116,7 +115,6 @@ namespace d3d12 {
private: private:
ComPtr<ID3D12Device> d3d12Device; ComPtr<ID3D12Device> d3d12Device;
ComPtr<ID3D12CommandQueue> commandQueue; ComPtr<ID3D12CommandQueue> commandQueue;
ComPtr<ID3D12Resource> renderTargetResource;
D3D12_CPU_DESCRIPTOR_HANDLE renderTargetDescriptor; D3D12_CPU_DESCRIPTOR_HANDLE renderTargetDescriptor;
}; };