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_