add D3D12Binding with swap chain
This commit is contained in:
parent
fc2bac7e45
commit
eb6d22242a
|
@ -14,20 +14,174 @@
|
|||
|
||||
#include "BackendBinding.h"
|
||||
|
||||
#define GLFW_EXPOSE_NATIVE_WIN32
|
||||
#include "GLFW/glfw3.h"
|
||||
#include "GLFW/glfw3native.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <wrl.h>
|
||||
#include <d3d12.h>
|
||||
#include <dxgi1_4.h>
|
||||
|
||||
#define ASSERT assert
|
||||
|
||||
using Microsoft::WRL::ComPtr;
|
||||
|
||||
namespace backend {
|
||||
namespace d3d12 {
|
||||
void Init(nxtProcTable* procs, nxtDevice* device);
|
||||
void Init(ComPtr<ID3D12Device> d3d12Device, nxtProcTable* procs, nxtDevice* device);
|
||||
ComPtr<ID3D12CommandQueue> GetCommandQueue(nxtDevice device);
|
||||
void SetNextRenderTarget(nxtDevice device, ComPtr<ID3D12Resource> renderTargetResource, D3D12_CPU_DESCRIPTOR_HANDLE renderTargetDescriptor);
|
||||
}
|
||||
}
|
||||
|
||||
class D3D12Binding : public BackendBinding {
|
||||
public:
|
||||
void SetupGLFWWindowHints() override {
|
||||
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
|
||||
}
|
||||
|
||||
void GetProcAndDevice(nxtProcTable* procs, nxtDevice* device) override {
|
||||
backend::d3d12::Init(procs, device);
|
||||
uint32_t dxgiFactoryFlags = 0;
|
||||
#ifdef _DEBUG
|
||||
// Enable the debug layer (requires the Graphics Tools "optional feature").
|
||||
// NOTE: Enabling the debug layer after device creation will invalidate the active device.
|
||||
{
|
||||
ComPtr<ID3D12Debug> debugController;
|
||||
if (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&debugController))))
|
||||
{
|
||||
debugController->EnableDebugLayer();
|
||||
|
||||
// Enable additional debug layers.
|
||||
dxgiFactoryFlags |= DXGI_CREATE_FACTORY_DEBUG;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
ASSERT_SUCCESS(CreateDXGIFactory2(dxgiFactoryFlags, IID_PPV_ARGS(&factory)));
|
||||
ASSERT(GetHardwareAdapter(factory.Get(), &hardwareAdapter));
|
||||
ASSERT_SUCCESS(D3D12CreateDevice(
|
||||
hardwareAdapter.Get(),
|
||||
D3D_FEATURE_LEVEL_11_0,
|
||||
IID_PPV_ARGS(&d3d12Device)
|
||||
));
|
||||
|
||||
backend::d3d12::Init(d3d12Device, procs, device);
|
||||
backendDevice = *device;
|
||||
commandQueue = backend::d3d12::GetCommandQueue(backendDevice);
|
||||
|
||||
int width, height;
|
||||
glfwGetWindowSize(window, &width, &height);
|
||||
DXGI_SWAP_CHAIN_DESC1 swapChainDesc = {};
|
||||
swapChainDesc.Width = width;
|
||||
swapChainDesc.Height = height;
|
||||
swapChainDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
||||
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
|
||||
swapChainDesc.BufferCount = kFrameCount;
|
||||
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
|
||||
swapChainDesc.SampleDesc.Count = 1;
|
||||
|
||||
HWND win32Window = glfwGetWin32Window(window);
|
||||
ComPtr<IDXGISwapChain1> swapChain1;
|
||||
ASSERT_SUCCESS(factory->CreateSwapChainForHwnd(
|
||||
commandQueue.Get(),
|
||||
win32Window,
|
||||
&swapChainDesc,
|
||||
nullptr,
|
||||
nullptr,
|
||||
&swapChain1
|
||||
));
|
||||
ASSERT_SUCCESS(swapChain1.As(&swapChain));
|
||||
frameIndex = swapChain->GetCurrentBackBufferIndex();
|
||||
|
||||
// Describe and create a render target view (RTV) descriptor heap.
|
||||
D3D12_DESCRIPTOR_HEAP_DESC rtvHeapDesc = {};
|
||||
rtvHeapDesc.NumDescriptors = kFrameCount;
|
||||
rtvHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
|
||||
rtvHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
|
||||
ASSERT_SUCCESS(d3d12Device->CreateDescriptorHeap(&rtvHeapDesc, IID_PPV_ARGS(&renderTargetViewHeap)));
|
||||
|
||||
rtvDescriptorSize = d3d12Device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
|
||||
|
||||
// Create a RTV for each frame.
|
||||
D3D12_CPU_DESCRIPTOR_HANDLE renderTargetViewHandle = renderTargetViewHeap->GetCPUDescriptorHandleForHeapStart();
|
||||
for (uint32_t n = 0; n < kFrameCount; ++n) {
|
||||
ASSERT_SUCCESS(swapChain->GetBuffer(n, IID_PPV_ARGS(&renderTargetResources[n])));
|
||||
d3d12Device->CreateRenderTargetView(renderTargetResources[n].Get(), nullptr, renderTargetViewHandle);
|
||||
renderTargetViewHandle.ptr += rtvDescriptorSize;
|
||||
}
|
||||
|
||||
ASSERT_SUCCESS(d3d12Device->CreateFence(fenceValues[frameIndex], D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence)));
|
||||
fenceValues[frameIndex]++;
|
||||
fenceEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
|
||||
ASSERT(fenceEvent != nullptr);
|
||||
|
||||
SetNextRenderTarget();
|
||||
}
|
||||
void SwapBuffers() override {
|
||||
// Present the rendered frame
|
||||
ASSERT_SUCCESS(swapChain->Present(1, 0));
|
||||
|
||||
const uint64_t currentFenceValue = fenceValues[frameIndex];
|
||||
ASSERT_SUCCESS(commandQueue->Signal(fence.Get(), currentFenceValue));
|
||||
|
||||
frameIndex = swapChain->GetCurrentBackBufferIndex();
|
||||
|
||||
SetNextRenderTarget();
|
||||
|
||||
// If the next frame is not ready to be rendered yet, wait until it is ready.
|
||||
if (fence->GetCompletedValue() < fenceValues[frameIndex]) {
|
||||
ASSERT_SUCCESS(fence->SetEventOnCompletion(fenceValues[frameIndex], fenceEvent));
|
||||
WaitForSingleObjectEx(fenceEvent, INFINITE, FALSE);
|
||||
}
|
||||
|
||||
// Set the fence value for the next frame.
|
||||
fenceValues[frameIndex] = currentFenceValue + 1;
|
||||
}
|
||||
|
||||
private:
|
||||
nxtDevice backendDevice = nullptr;
|
||||
|
||||
static constexpr int kFrameCount = 2;
|
||||
uint32_t frameIndex = 0;
|
||||
uint32_t rtvDescriptorSize;
|
||||
ComPtr<IDXGIFactory4> factory;
|
||||
ComPtr<IDXGIAdapter1> hardwareAdapter;
|
||||
ComPtr<ID3D12Device> d3d12Device;
|
||||
ComPtr<ID3D12CommandQueue> commandQueue;
|
||||
ComPtr<IDXGISwapChain3> swapChain;
|
||||
ComPtr<ID3D12DescriptorHeap> renderTargetViewHeap;
|
||||
ComPtr<ID3D12Resource> renderTargetResources[kFrameCount];
|
||||
uint64_t fenceValues[kFrameCount] = { 0, 0 };
|
||||
ComPtr<ID3D12Fence> fence;
|
||||
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) {
|
||||
assert(SUCCEEDED(hr));
|
||||
}
|
||||
|
||||
static bool GetHardwareAdapter(IDXGIFactory4* factory, IDXGIAdapter1** hardwareAdapter) {
|
||||
*hardwareAdapter = nullptr;
|
||||
for (uint32_t adapterIndex = 0; ; ++adapterIndex) {
|
||||
IDXGIAdapter1* adapter = nullptr;
|
||||
if (factory->EnumAdapters1(adapterIndex, &adapter) == DXGI_ERROR_NOT_FOUND) {
|
||||
break; // No more adapters to enumerate.
|
||||
}
|
||||
|
||||
// Check to see if the adapter supports Direct3D 12, but don't create the actual device yet.
|
||||
if (SUCCEEDED(D3D12CreateDevice(adapter, D3D_FEATURE_LEVEL_11_0, _uuidof(ID3D12Device), nullptr))) {
|
||||
*hardwareAdapter = adapter;
|
||||
return true;
|
||||
}
|
||||
adapter->Release();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -156,7 +156,58 @@ if (WIN32)
|
|||
${GENERATOR_COMMON_ARGS}
|
||||
-T d3d12
|
||||
)
|
||||
# WIN10_SDK_PATH will be something like C:\Program Files (x86)\Windows Kits\10
|
||||
get_filename_component(WIN10_SDK_PATH "[HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\Microsoft\\Microsoft SDKs\\Windows\\v10.0;InstallationFolder]" ABSOLUTE CACHE)
|
||||
|
||||
# TEMP_WIN10_SDK_VERSION will be something like ${CMAKE_CURRENT_SOURCE_DIR}\10.0.14393 or ${CMAKE_CURRENT_SOURCE_DIR}\10.0.14393.0
|
||||
get_filename_component(TEMP_WIN10_SDK_VERSION "[HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\Microsoft\\Microsoft SDKs\\Windows\\v10.0;ProductVersion]" ABSOLUTE CACHE)
|
||||
|
||||
# strip off ${CMAKE_CURRENT_SOURCE_DIR} to get just the version number
|
||||
get_filename_component(WIN10_SDK_VERSION ${TEMP_WIN10_SDK_VERSION} NAME)
|
||||
# WIN10_SDK_VERSION will be something like 10.0.14393 or 10.0.14393.0; we need the one that matches the directory name.
|
||||
if (IS_DIRECTORY "${WIN10_SDK_PATH}/Include/${WIN10_SDK_VERSION}.0")
|
||||
set(WIN10_SDK_VERSION "${WIN10_SDK_VERSION}.0")
|
||||
endif()
|
||||
|
||||
# Find the d3d12 and dxgi include path, it will typically look something like this.
|
||||
# C:\Program Files (x86)\Windows Kits\10\Include\10.0.10586.0\um\d3d12.h
|
||||
# C:\Program Files (x86)\Windows Kits\10\Include\10.0.10586.0\shared\dxgi1_4.h
|
||||
find_path(D3D12_INCLUDE_DIR # Set variable D3D12_INCLUDE_DIR
|
||||
d3d12.h # Find a path with d3d12.h
|
||||
HINTS "${WIN10_SDK_PATH}/Include/${WIN10_SDK_VERSION}/um"
|
||||
DOC "path to WIN10 SDK header files"
|
||||
HINTS
|
||||
)
|
||||
|
||||
find_path(DXGI_INCLUDE_DIR # Set variable DXGI_INCLUDE_DIR
|
||||
dxgi1_4.h # Find a path with dxgi1_4.h
|
||||
HINTS "${WIN10_SDK_PATH}/Include/${WIN10_SDK_VERSION}/shared"
|
||||
DOC "path to WIN10 SDK header files"
|
||||
HINTS
|
||||
)
|
||||
|
||||
if (CMAKE_GENERATOR MATCHES "Visual Studio.*ARM" )
|
||||
set(WIN10_SDK_LIB_PATH ${WIN10_SDK_PATH}/Lib/${WIN10_SDK_VERSION}/um/arm)
|
||||
elseif (CMAKE_GENERATOR MATCHES "Visual Studio.*ARM64" )
|
||||
set(WIN10_SDK_LIB_PATH ${WIN10_SDK_PATH}/Lib/${WIN10_SDK_VERSION}/um/arm64)
|
||||
elseif (CMAKE_GENERATOR MATCHES "Visual Studio.*Win64" )
|
||||
set(WIN10_SDK_LIB_PATH ${WIN10_SDK_PATH}/Lib/${WIN10_SDK_VERSION}/um/x64)
|
||||
else()
|
||||
set(WIN10_SDK_LIB_PATH ${WIN10_SDK_PATH}/Lib/${WIN10_SDK_VERSION}/um/x86)
|
||||
endif()
|
||||
|
||||
find_library(D3D12_LIBRARY NAMES d3d12.lib HINTS ${WIN10_SDK_LIB_PATH})
|
||||
find_library(DXGI_LIBRARY NAMES dxgi.lib HINTS ${WIN10_SDK_LIB_PATH})
|
||||
find_library(D3DCOMPILER_LIBRARY NAMES d3dcompiler.lib HINTS ${WIN10_SDK_LIB_PATH})
|
||||
|
||||
set(D3D12_LIBRARIES
|
||||
${D3D12_LIBRARY}
|
||||
${DXGI_LIBRARY}
|
||||
${D3DCOMPILER_LIBRARY}
|
||||
)
|
||||
|
||||
target_link_libraries(d3d12_autogen glfw nxtcpp ${D3D12_LIBRARIES})
|
||||
target_include_directories(d3d12_autogen SYSTEM PRIVATE ${D3D12_INCLUDE_DIR} ${DXGI_INCLUDE_DIR})
|
||||
target_include_directories(d3d12_autogen PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
target_include_directories(d3d12_autogen PUBLIC ${GENERATED_DIR})
|
||||
SetCXX14(d3d12_autogen)
|
||||
|
|
|
@ -20,17 +20,74 @@ namespace d3d12 {
|
|||
nxtProcTable GetNonValidatingProcs();
|
||||
nxtProcTable GetValidatingProcs();
|
||||
|
||||
void Init(nxtProcTable* procs, nxtDevice* device) {
|
||||
void Init(ComPtr<ID3D12Device> d3d12Device, nxtProcTable* procs, nxtDevice* device) {
|
||||
*device = nullptr;
|
||||
*procs = GetValidatingProcs();
|
||||
*device = reinterpret_cast<nxtDevice>(new Device(d3d12Device));
|
||||
}
|
||||
|
||||
Device::Device() {
|
||||
ComPtr<ID3D12CommandQueue> GetCommandQueue(nxtDevice device) {
|
||||
Device* backendDevice = reinterpret_cast<Device*>(device);
|
||||
return backendDevice->GetCommandQueue();
|
||||
}
|
||||
|
||||
void SetNextRenderTarget(nxtDevice device, ComPtr<ID3D12Resource> renderTargetResource, D3D12_CPU_DESCRIPTOR_HANDLE renderTargetDescriptor) {
|
||||
Device* backendDevice = reinterpret_cast<Device*>(device);
|
||||
backendDevice->SetNextRenderTarget(renderTargetResource, renderTargetDescriptor);
|
||||
}
|
||||
|
||||
void ASSERT_SUCCESS(HRESULT hr) {
|
||||
ASSERT(SUCCEEDED(hr));
|
||||
}
|
||||
|
||||
Device::Device(ComPtr<ID3D12Device> d3d12Device) : d3d12Device(d3d12Device) {
|
||||
D3D12_COMMAND_QUEUE_DESC queueDesc = {};
|
||||
queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
|
||||
queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
|
||||
ASSERT_SUCCESS(d3d12Device->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(&commandQueue)));
|
||||
|
||||
// Create an empty root signature.
|
||||
D3D12_ROOT_SIGNATURE_DESC rootSignatureDescriptor;
|
||||
rootSignatureDescriptor.NumParameters = 0;
|
||||
rootSignatureDescriptor.pParameters = nullptr;
|
||||
rootSignatureDescriptor.NumStaticSamplers = 0;
|
||||
rootSignatureDescriptor.pStaticSamplers = nullptr;
|
||||
rootSignatureDescriptor.Flags = D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT;
|
||||
|
||||
ComPtr<ID3DBlob> signature;
|
||||
ComPtr<ID3DBlob> error;
|
||||
ASSERT_SUCCESS(D3D12SerializeRootSignature(&rootSignatureDescriptor, D3D_ROOT_SIGNATURE_VERSION_1, &signature, &error));
|
||||
ASSERT_SUCCESS(d3d12Device->CreateRootSignature(0, signature->GetBufferPointer(), signature->GetBufferSize(), IID_PPV_ARGS(&rootSignature)));
|
||||
}
|
||||
|
||||
Device::~Device() {
|
||||
}
|
||||
|
||||
ComPtr<ID3D12Device> Device::GetD3D12Device() {
|
||||
return d3d12Device;
|
||||
}
|
||||
|
||||
ComPtr<ID3D12RootSignature> Device::GetRootSignature() {
|
||||
return rootSignature;
|
||||
}
|
||||
|
||||
ComPtr<ID3D12CommandQueue> Device::GetCommandQueue() {
|
||||
return commandQueue;
|
||||
}
|
||||
|
||||
Microsoft::WRL::ComPtr<ID3D12Resource> Device::GetNextRenderTarget() {
|
||||
return renderTargetResource;
|
||||
}
|
||||
|
||||
D3D12_CPU_DESCRIPTOR_HANDLE Device::GetNextRenderTargetDescriptor() {
|
||||
return renderTargetDescriptor;
|
||||
}
|
||||
|
||||
void Device::SetNextRenderTarget(ComPtr<ID3D12Resource> renderTargetResource, D3D12_CPU_DESCRIPTOR_HANDLE renderTargetDescriptor) {
|
||||
this->renderTargetResource = renderTargetResource;
|
||||
this->renderTargetDescriptor = renderTargetDescriptor;
|
||||
}
|
||||
|
||||
BindGroupBase* Device::CreateBindGroup(BindGroupBuilder* builder) {
|
||||
return new BindGroup(this, builder);
|
||||
}
|
||||
|
|
|
@ -34,6 +34,8 @@
|
|||
#include "common/Texture.h"
|
||||
#include "common/ToBackend.h"
|
||||
|
||||
#include "d3d12_platform.h"
|
||||
|
||||
namespace backend {
|
||||
namespace d3d12 {
|
||||
|
||||
|
@ -78,10 +80,12 @@ namespace d3d12 {
|
|||
return ToBackendBase<D3D12BackendTraits>(common);
|
||||
}
|
||||
|
||||
void ASSERT_SUCCESS(HRESULT hr);
|
||||
|
||||
// Definition of backend types
|
||||
class Device : public DeviceBase {
|
||||
public:
|
||||
Device();
|
||||
Device(Microsoft::WRL::ComPtr<ID3D12Device> d3d12Device);
|
||||
~Device();
|
||||
|
||||
BindGroupBase* CreateBindGroup(BindGroupBuilder* builder) override;
|
||||
|
@ -101,9 +105,24 @@ namespace d3d12 {
|
|||
TextureBase* CreateTexture(TextureBuilder* builder) override;
|
||||
TextureViewBase* CreateTextureView(TextureViewBuilder* builder) override;
|
||||
|
||||
Microsoft::WRL::ComPtr<ID3D12Device> GetD3D12Device();
|
||||
Microsoft::WRL::ComPtr<ID3D12RootSignature> GetRootSignature();
|
||||
Microsoft::WRL::ComPtr<ID3D12CommandQueue> GetCommandQueue();
|
||||
Microsoft::WRL::ComPtr<ID3D12Resource> GetNextRenderTarget();
|
||||
D3D12_CPU_DESCRIPTOR_HANDLE GetNextRenderTargetDescriptor();
|
||||
|
||||
void SetNextRenderTarget(Microsoft::WRL::ComPtr<ID3D12Resource> renderTargetResource, D3D12_CPU_DESCRIPTOR_HANDLE renderTargetDescriptor);
|
||||
|
||||
// NXT API
|
||||
void Reference();
|
||||
void Release();
|
||||
|
||||
private:
|
||||
Microsoft::WRL::ComPtr<ID3D12Device> d3d12Device;
|
||||
Microsoft::WRL::ComPtr<ID3D12CommandQueue> commandQueue;
|
||||
Microsoft::WRL::ComPtr<ID3D12RootSignature> rootSignature;
|
||||
Microsoft::WRL::ComPtr<ID3D12Resource> renderTargetResource;
|
||||
D3D12_CPU_DESCRIPTOR_HANDLE renderTargetDescriptor;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
// Copyright 2017 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_D3D12_H_
|
||||
#define BACKEND_D3D12_D3D12_H_
|
||||
|
||||
#include <wrl.h>
|
||||
#include <d3d12.h>
|
||||
#include <dxgi1_4.h>
|
||||
|
||||
using Microsoft::WRL::ComPtr;
|
||||
|
||||
#endif // BACKEND_D3D12_D3D12_H_
|
|
@ -62,6 +62,8 @@ add_library(spirv-cross STATIC
|
|||
${CMAKE_CURRENT_SOURCE_DIR}/spirv-cross/spirv.hpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/spirv-cross/spirv_msl.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/spirv-cross/spirv_msl.hpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/spirv-cross/spirv_hlsl.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/spirv-cross/spirv_hlsl.hpp
|
||||
)
|
||||
target_include_directories(spirv-cross PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
SetCXX14(spirv-cross)
|
||||
|
|
Loading…
Reference in New Issue