D3D12: Move the NativeSwapChainImpl in the backend

This commit is contained in:
Corentin Wallez 2018-02-06 10:50:24 -05:00 committed by Corentin Wallez
parent 02d24d3c5e
commit 9b491437b1
6 changed files with 213 additions and 172 deletions

View File

@ -245,6 +245,8 @@ if (NXT_ENABLE_D3D12)
${D3D12_DIR}/FramebufferD3D12.h
${D3D12_DIR}/InputStateD3D12.cpp
${D3D12_DIR}/InputStateD3D12.h
${D3D12_DIR}/NativeSwapChainImplD3D12.cpp
${D3D12_DIR}/NativeSwapChainImplD3D12.h
${D3D12_DIR}/PipelineLayoutD3D12.cpp
${D3D12_DIR}/PipelineLayoutD3D12.h
${D3D12_DIR}/QueueD3D12.cpp

View File

@ -25,6 +25,7 @@
#include "backend/d3d12/DescriptorHeapAllocator.h"
#include "backend/d3d12/FramebufferD3D12.h"
#include "backend/d3d12/InputStateD3D12.h"
#include "backend/d3d12/NativeSwapChainImplD3D12.h"
#include "backend/d3d12/PipelineLayoutD3D12.h"
#include "backend/d3d12/QueueD3D12.h"
#include "backend/d3d12/RenderPipelineD3D12.h"
@ -35,44 +36,40 @@
#include "backend/d3d12/SwapChainD3D12.h"
#include "backend/d3d12/TextureD3D12.h"
#include "common/Assert.h"
#include "common/SwapChainUtils.h"
namespace backend { namespace d3d12 {
nxtProcTable GetNonValidatingProcs();
nxtProcTable GetValidatingProcs();
void Init(ComPtr<ID3D12Device> d3d12Device, nxtProcTable* procs, nxtDevice* device) {
void Init(ComPtr<IDXGIFactory4> factory,
ComPtr<ID3D12Device> d3d12Device,
nxtProcTable* procs,
nxtDevice* device) {
*device = nullptr;
*procs = GetValidatingProcs();
*device = reinterpret_cast<nxtDevice>(new Device(d3d12Device));
*device = reinterpret_cast<nxtDevice>(new Device(factory, d3d12Device));
}
ComPtr<ID3D12CommandQueue> GetCommandQueue(nxtDevice device) {
nxtSwapChainImplementation CreateNativeSwapChainImpl(nxtDevice device, HWND window) {
Device* backendDevice = reinterpret_cast<Device*>(device);
return backendDevice->GetCommandQueue();
return CreateSwapChainImplementation(new NativeSwapChainImpl(backendDevice, window));
}
uint64_t GetSerial(const nxtDevice device) {
const Device* backendDevice = reinterpret_cast<const Device*>(device);
return backendDevice->GetSerial();
}
void NextSerial(nxtDevice device) {
Device* backendDevice = reinterpret_cast<Device*>(device);
backendDevice->NextSerial();
}
void WaitForSerial(nxtDevice device, uint64_t serial) {
Device* backendDevice = reinterpret_cast<Device*>(device);
backendDevice->WaitForSerial(serial);
nxtTextureFormat GetNativeSwapChainPreferredFormat(
const nxtSwapChainImplementation* swapChain) {
NativeSwapChainImpl* impl = reinterpret_cast<NativeSwapChainImpl*>(swapChain->userData);
return static_cast<nxtTextureFormat>(impl->GetPreferredFormat());
}
void ASSERT_SUCCESS(HRESULT hr) {
ASSERT(SUCCEEDED(hr));
}
Device::Device(ComPtr<ID3D12Device> d3d12Device)
: mD3d12Device(d3d12Device),
Device::Device(ComPtr<IDXGIFactory4> factory, ComPtr<ID3D12Device> d3d12Device)
: mFactory(factory),
mD3d12Device(d3d12Device),
mCommandAllocatorManager(new CommandAllocatorManager(this)),
mDescriptorHeapAllocator(new DescriptorHeapAllocator(this)),
mMapReadRequestTracker(new MapReadRequestTracker(this)),
@ -103,6 +100,10 @@ namespace backend { namespace d3d12 {
delete mResourceUploader;
}
ComPtr<IDXGIFactory4> Device::GetFactory() {
return mFactory;
}
ComPtr<ID3D12Device> Device::GetD3D12Device() {
return mD3d12Device;
}

View File

@ -86,7 +86,7 @@ namespace backend { namespace d3d12 {
// Definition of backend types
class Device : public DeviceBase {
public:
Device(ComPtr<ID3D12Device> d3d12Device);
Device(ComPtr<IDXGIFactory4> factory, ComPtr<ID3D12Device> d3d12Device);
~Device();
BindGroupBase* CreateBindGroup(BindGroupBuilder* builder) override;
@ -111,6 +111,7 @@ namespace backend { namespace d3d12 {
void TickImpl() override;
ComPtr<IDXGIFactory4> GetFactory();
ComPtr<ID3D12Device> GetD3D12Device();
ComPtr<ID3D12CommandQueue> GetCommandQueue();
@ -133,6 +134,7 @@ namespace backend { namespace d3d12 {
ComPtr<ID3D12Fence> mFence;
HANDLE mFenceEvent;
ComPtr<IDXGIFactory4> mFactory;
ComPtr<ID3D12Device> mD3d12Device;
ComPtr<ID3D12CommandQueue> mCommandQueue;

View File

@ -0,0 +1,119 @@
// Copyright 2018 The NXT Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "backend/d3d12/NativeSwapChainImplD3D12.h"
#include "backend/d3d12/D3D12Backend.h"
#include "backend/d3d12/TextureD3D12.h"
#include "common/Assert.h"
namespace backend { namespace d3d12 {
namespace {
DXGI_USAGE D3D12SwapChainBufferUsage(nxtTextureUsageBit allowedUsages) {
DXGI_USAGE usage = DXGI_CPU_ACCESS_NONE;
if (allowedUsages & NXT_TEXTURE_USAGE_BIT_SAMPLED) {
usage |= DXGI_USAGE_SHADER_INPUT;
}
if (allowedUsages & NXT_TEXTURE_USAGE_BIT_STORAGE) {
usage |= DXGI_USAGE_UNORDERED_ACCESS;
}
if (allowedUsages & NXT_TEXTURE_USAGE_BIT_OUTPUT_ATTACHMENT) {
usage |= DXGI_USAGE_RENDER_TARGET_OUTPUT;
}
return usage;
}
static constexpr unsigned int kFrameCount = 2;
} // anonymous namespace
NativeSwapChainImpl::NativeSwapChainImpl(Device* device, HWND window)
: mWindow(window), mDevice(device) {
}
NativeSwapChainImpl::~NativeSwapChainImpl() {
}
void NativeSwapChainImpl::Init(nxtWSIContextD3D12* /*context*/) {
}
nxtSwapChainError NativeSwapChainImpl::Configure(nxtTextureFormat format,
nxtTextureUsageBit usage,
uint32_t width,
uint32_t height) {
ASSERT(width > 0);
ASSERT(height > 0);
ASSERT(format == static_cast<nxtTextureFormat>(GetPreferredFormat()));
ComPtr<IDXGIFactory4> factory = mDevice->GetFactory();
ComPtr<ID3D12CommandQueue> queue = mDevice->GetCommandQueue();
// Create the D3D12 swapchain, assuming only two buffers for now
DXGI_SWAP_CHAIN_DESC1 swapChainDesc = {};
swapChainDesc.Width = width;
swapChainDesc.Height = height;
swapChainDesc.Format = D3D12TextureFormat(GetPreferredFormat());
swapChainDesc.BufferUsage = D3D12SwapChainBufferUsage(usage);
swapChainDesc.BufferCount = kFrameCount;
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.SampleDesc.Quality = 0;
ComPtr<IDXGISwapChain1> swapChain1;
ASSERT_SUCCESS(factory->CreateSwapChainForHwnd(queue.Get(), mWindow, &swapChainDesc,
nullptr, nullptr, &swapChain1));
ASSERT_SUCCESS(swapChain1.As(&mSwapChain));
// Gather the resources that will be used to present to the swapchain
mBuffers.resize(kFrameCount);
for (uint32_t i = 0; i < kFrameCount; ++i) {
ASSERT_SUCCESS(mSwapChain->GetBuffer(i, IID_PPV_ARGS(&mBuffers[i])));
}
// Set the initial serial of buffers to 0 so that we don't wait on them when they are first
// used
mBufferSerials.resize(kFrameCount, 0);
return NXT_SWAP_CHAIN_NO_ERROR;
}
nxtSwapChainError NativeSwapChainImpl::GetNextTexture(nxtSwapChainNextTexture* nextTexture) {
mCurrentBuffer = mSwapChain->GetCurrentBackBufferIndex();
nextTexture->texture.ptr = mBuffers[mCurrentBuffer].Get();
// TODO(cwallez@chromium.org) Currently we force the CPU to wait for the GPU to be finished
// with the buffer. Ideally the synchronization should be all done on the GPU.
mDevice->WaitForSerial(mBufferSerials[mCurrentBuffer]);
return NXT_SWAP_CHAIN_NO_ERROR;
}
nxtSwapChainError NativeSwapChainImpl::Present() {
// Flush pending commands that include the transition of the texture to present.
mDevice->ExecuteCommandLists({});
ASSERT_SUCCESS(mSwapChain->Present(1, 0));
// TODO(cwallez@chromium.org): Make the serial ticking implicit.
mDevice->NextSerial();
mBufferSerials[mCurrentBuffer] = mDevice->GetSerial();
return NXT_SWAP_CHAIN_NO_ERROR;
}
nxt::TextureFormat NativeSwapChainImpl::GetPreferredFormat() const {
return nxt::TextureFormat::R8G8B8A8Unorm;
}
}} // namespace backend::d3d12

View File

@ -0,0 +1,58 @@
// Copyright 2018 The NXT Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef BACKEND_D3D12_NATIVESWAPCHAINIMPLD3D12_H_
#define BACKEND_D3D12_NATIVESWAPCHAINIMPLD3D12_H_
#include "backend/d3d12/d3d12_platform.h"
#include "nxt/nxt_wsi.h"
#include "nxt/nxtcpp.h"
#include <vector>
namespace backend { namespace d3d12 {
class Device;
class NativeSwapChainImpl {
public:
using WSIContext = nxtWSIContextD3D12;
NativeSwapChainImpl(Device* device, HWND window);
~NativeSwapChainImpl();
void Init(nxtWSIContextD3D12* context);
nxtSwapChainError Configure(nxtTextureFormat format,
nxtTextureUsageBit,
uint32_t width,
uint32_t height);
nxtSwapChainError GetNextTexture(nxtSwapChainNextTexture* nextTexture);
nxtSwapChainError Present();
nxt::TextureFormat GetPreferredFormat() const;
private:
HWND mWindow = nullptr;
Device* mDevice = nullptr;
ComPtr<IDXGISwapChain3> mSwapChain = nullptr;
std::vector<ComPtr<ID3D12Resource>> mBuffers;
std::vector<uint64_t> mBufferSerials;
uint32_t mCurrentBuffer;
};
}} // namespace backend::d3d12
#endif // BACKEND_D3D12_NATIVESWAPCHAINIMPLD3D12_H_

View File

@ -15,7 +15,6 @@
#include "utils/BackendBinding.h"
#include "common/Assert.h"
#include "common/SwapChainUtils.h"
#include "nxt/nxt_wsi.h"
#define GLFW_EXPOSE_NATIVE_WIN32
@ -33,11 +32,13 @@
using Microsoft::WRL::ComPtr;
namespace backend { namespace d3d12 {
void Init(ComPtr<ID3D12Device> d3d12Device, nxtProcTable* procs, nxtDevice* device);
ComPtr<ID3D12CommandQueue> GetCommandQueue(nxtDevice device);
uint64_t GetSerial(const nxtDevice device);
void NextSerial(nxtDevice device);
void WaitForSerial(nxtDevice device, uint64_t serial);
void Init(ComPtr<IDXGIFactory4> factory,
ComPtr<ID3D12Device> d3d12Device,
nxtProcTable* procs,
nxtDevice* device);
nxtSwapChainImplementation CreateNativeSwapChainImpl(nxtDevice device, HWND window);
nxtTextureFormat GetNativeSwapChainPreferredFormat(const nxtSwapChainImplementation* swapChain);
}} // namespace backend::d3d12
namespace utils {
@ -76,151 +77,8 @@ namespace utils {
return factory;
}
DXGI_USAGE D3D12SwapChainBufferUsage(nxtTextureUsageBit allowedUsages) {
DXGI_USAGE usage = DXGI_CPU_ACCESS_NONE;
if (allowedUsages & NXT_TEXTURE_USAGE_BIT_SAMPLED) {
usage |= DXGI_USAGE_SHADER_INPUT;
}
if (allowedUsages & NXT_TEXTURE_USAGE_BIT_STORAGE) {
usage |= DXGI_USAGE_UNORDERED_ACCESS;
}
if (allowedUsages & NXT_TEXTURE_USAGE_BIT_OUTPUT_ATTACHMENT) {
usage |= DXGI_USAGE_RENDER_TARGET_OUTPUT;
}
return usage;
}
D3D12_RESOURCE_STATES D3D12ResourceState(nxtTextureUsageBit usage) {
D3D12_RESOURCE_STATES resourceState = D3D12_RESOURCE_STATE_COMMON;
if (usage & NXT_TEXTURE_USAGE_BIT_TRANSFER_SRC) {
resourceState |= D3D12_RESOURCE_STATE_COPY_SOURCE;
}
if (usage & NXT_TEXTURE_USAGE_BIT_TRANSFER_DST) {
resourceState |= D3D12_RESOURCE_STATE_COPY_DEST;
}
if (usage & NXT_TEXTURE_USAGE_BIT_SAMPLED) {
resourceState |= (D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE |
D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE);
}
if (usage & NXT_TEXTURE_USAGE_BIT_STORAGE) {
resourceState |= D3D12_RESOURCE_STATE_UNORDERED_ACCESS;
}
if (usage & NXT_TEXTURE_USAGE_BIT_OUTPUT_ATTACHMENT) {
resourceState |= D3D12_RESOURCE_STATE_RENDER_TARGET;
}
return resourceState;
}
} // namespace
class SwapChainImplD3D12 {
public:
using WSIContext = nxtWSIContextD3D12;
SwapChainImplD3D12(HWND window, nxtProcTable procs)
: mWindow(window), mProcs(procs), mFactory(CreateFactory()) {
}
~SwapChainImplD3D12() {
}
void Init(nxtWSIContextD3D12* ctx) {
mBackendDevice = ctx->device;
mCommandQueue = backend::d3d12::GetCommandQueue(mBackendDevice);
}
nxtSwapChainError Configure(nxtTextureFormat format,
nxtTextureUsageBit allowedUsage,
uint32_t width,
uint32_t height) {
if (format != NXT_TEXTURE_FORMAT_R8_G8_B8_A8_UNORM) {
return "unsupported format";
}
ASSERT(width > 0);
ASSERT(height > 0);
DXGI_SWAP_CHAIN_DESC1 swapChainDesc = {};
swapChainDesc.Width = width;
swapChainDesc.Height = height;
swapChainDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
swapChainDesc.BufferUsage = D3D12SwapChainBufferUsage(allowedUsage);
swapChainDesc.BufferCount = kFrameCount;
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.SampleDesc.Quality = 0;
ComPtr<IDXGISwapChain1> swapChain1;
ASSERT_SUCCESS(mFactory->CreateSwapChainForHwnd(
mCommandQueue.Get(), mWindow, &swapChainDesc, nullptr, nullptr, &swapChain1));
ASSERT_SUCCESS(swapChain1.As(&mSwapChain));
for (uint32_t n = 0; n < kFrameCount; ++n) {
ASSERT_SUCCESS(mSwapChain->GetBuffer(n, IID_PPV_ARGS(&mRenderTargetResources[n])));
}
// Get the initial render target and arbitrarily choose a "previous" render target
// that's different
mPreviousRenderTargetIndex = mRenderTargetIndex =
mSwapChain->GetCurrentBackBufferIndex();
mPreviousRenderTargetIndex = mRenderTargetIndex == 0 ? 1 : 0;
// Initial the serial for all render targets
const uint64_t initialSerial = backend::d3d12::GetSerial(mBackendDevice);
for (uint32_t n = 0; n < kFrameCount; ++n) {
mLastSerialRenderTargetWasUsed[n] = initialSerial;
}
return NXT_SWAP_CHAIN_NO_ERROR;
}
nxtSwapChainError GetNextTexture(nxtSwapChainNextTexture* nextTexture) {
nextTexture->texture.ptr = mRenderTargetResources[mRenderTargetIndex].Get();
return NXT_SWAP_CHAIN_NO_ERROR;
}
nxtSwapChainError Present() {
// Current frame already transitioned to Present by the application, but
// we need to flush the D3D12 backend's pending transitions.
mProcs.deviceTick(mBackendDevice);
ASSERT_SUCCESS(mSwapChain->Present(1, 0));
backend::d3d12::NextSerial(mBackendDevice);
mPreviousRenderTargetIndex = mRenderTargetIndex;
mRenderTargetIndex = mSwapChain->GetCurrentBackBufferIndex();
// If the next render target is not ready to be rendered yet, wait until it is ready.
// If the last completed serial is less than the last requested serial for this render
// target, then the commands previously executed on this render target have not yet
// completed
backend::d3d12::WaitForSerial(mBackendDevice,
mLastSerialRenderTargetWasUsed[mRenderTargetIndex]);
mLastSerialRenderTargetWasUsed[mRenderTargetIndex] =
backend::d3d12::GetSerial(mBackendDevice);
return NXT_SWAP_CHAIN_NO_ERROR;
}
private:
nxtDevice mBackendDevice = nullptr;
nxtProcTable mProcs = {};
static constexpr unsigned int kFrameCount = 2;
HWND mWindow = 0;
ComPtr<IDXGIFactory4> mFactory = {};
ComPtr<ID3D12CommandQueue> mCommandQueue = {};
ComPtr<IDXGISwapChain3> mSwapChain = {};
ComPtr<ID3D12Resource> mRenderTargetResources[kFrameCount] = {};
// Frame synchronization. Updated every frame
uint32_t mRenderTargetIndex = 0;
uint32_t mPreviousRenderTargetIndex = 0;
uint64_t mLastSerialRenderTargetWasUsed[kFrameCount] = {};
};
class D3D12Binding : public BackendBinding {
public:
void SetupGLFWWindowHints() override {
@ -233,7 +91,7 @@ namespace utils {
ASSERT_SUCCESS(D3D12CreateDevice(mHardwareAdapter.Get(), D3D_FEATURE_LEVEL_11_0,
IID_PPV_ARGS(&mD3d12Device)));
backend::d3d12::Init(mD3d12Device, procs, device);
backend::d3d12::Init(mFactory, mD3d12Device, procs, device);
mBackendDevice = *device;
mProcTable = *procs;
}
@ -242,13 +100,14 @@ namespace utils {
if (mSwapchainImpl.userData == nullptr) {
HWND win32Window = glfwGetWin32Window(mWindow);
mSwapchainImpl =
CreateSwapChainImplementation(new SwapChainImplD3D12(win32Window, mProcTable));
backend::d3d12::CreateNativeSwapChainImpl(mBackendDevice, win32Window);
}
return reinterpret_cast<uint64_t>(&mSwapchainImpl);
}
nxtTextureFormat GetPreferredSwapChainTextureFormat() override {
return NXT_TEXTURE_FORMAT_R8_G8_B8_A8_UNORM;
ASSERT(mSwapchainImpl.userData != nullptr);
return backend::d3d12::GetNativeSwapChainPreferredFormat(&mSwapchainImpl);
}
private: