More D3D12 work

This commit is contained in:
Jack Andersen
2015-11-03 14:27:32 -10:00
parent 50bca6f58e
commit bc84471d4d
7 changed files with 293 additions and 94 deletions

View File

@@ -4,10 +4,8 @@
#include <Usbiodef.h>
#if _DEBUG
#define DXGI_FACTORY2_FLAGS DXGI_CREATE_FACTORY_DEBUG
#define D3D11_CREATE_DEVICE_FLAGS D3D11_CREATE_DEVICE_DEBUG
#else
#define DXGI_FACTORY2_FLAGS 0
#define D3D11_CREATE_DEVICE_FLAGS 0
#endif
@@ -65,18 +63,26 @@ public:
if (!MyCreateDXGIFactory2)
Log.report(LogVisor::FatalError, "unable to find CreateDXGIFactory2 in DXGI.dll\n"
"Windows 7 users should install \"Platform Update for Windows 7\" from Microsoft");
if (FAILED(MyCreateDXGIFactory2(DXGI_FACTORY2_FLAGS, __uuidof(IDXGIFactory2), &m_d3dCtx.m_dxFactory)))
Log.report(LogVisor::FatalError, "unable to create DXGI factory");
#if WINVER >= _WIN32_WINNT_WIN10
HMODULE d3d12lib = LoadLibraryW(L"D3D12.dll");
if (d3d12lib)
{
/* Create device */
PFN_D3D12_CREATE_DEVICE MyD3D12CreateDevice = (PFN_D3D12_CREATE_DEVICE)GetProcAddress(d3d12lib, "D3D12CreateDevice");
if (!MyD3D12CreateDevice)
Log.report(LogVisor::FatalError, "unable to find D3D12CreateDevice in D3D12.dll");
/* Create device */
if (FAILED(MyD3D12CreateDevice(nullptr, D3D_FEATURE_LEVEL_12_0, __uuidof(ID3D12Device), &m_d3dCtx.m_ctx12.m_dev)))
Log.report(LogVisor::FatalError, "unable to create D3D12 device");
/* Obtain DXGI Factory */
ComPtr<IDXGIDevice2> device;
ComPtr<IDXGIAdapter> adapter;
m_d3dCtx.m_ctx12.m_dev.As<IDXGIDevice2>(&device);
device->GetParent(__uuidof(IDXGIAdapter), &adapter);
adapter->GetParent(__uuidof(IDXGIFactory2), &m_d3dCtx.m_dxFactory);
return;
}
@@ -99,6 +105,13 @@ public:
tempDev.As<ID3D11Device1>(&m_d3dCtx.m_ctx11.m_dev);
tempCtx.As<ID3D11DeviceContext1>(&m_d3dCtx.m_ctx11.m_devCtx);
/* Obtain DXGI Factory */
ComPtr<IDXGIDevice2> device;
ComPtr<IDXGIAdapter> adapter;
m_d3dCtx.m_ctx11.m_dev.As<IDXGIDevice2>(&device);
device->GetParent(__uuidof(IDXGIAdapter), &adapter);
adapter->GetParent(__uuidof(IDXGIFactory2), &m_d3dCtx.m_dxFactory);
return;
}

View File

@@ -8,6 +8,9 @@
#define WIN32_LEAN_AND_MEAN 1
#endif
#include <windows.h>
#include <unordered_map>
namespace boo {class IWindow;}
#if _WIN32_WINNT_WIN10
#include <dxgi1_4.h>
@@ -17,12 +20,20 @@
struct D3D12Context
{
ComPtr<ID3D12Device> m_dev;
ComPtr<ID3D12CommandAllocator> m_qalloc;
ComPtr<ID3D12CommandAllocator> m_qalloc[2];
ComPtr<ID3D12CommandQueue> m_q;
ComPtr<ID3D12CommandAllocator> m_loadqalloc;
ComPtr<ID3D12CommandQueue> m_loadq;
ComPtr<ID3D12Fence> m_frameFence;
ComPtr<ID3D12RootSignature> m_rs;
struct Window
{
ComPtr<IDXGISwapChain3> m_swapChain;
ComPtr<ID3D12Resource> m_fb[2]; /* Double-buffered */
UINT m_backBuf = 0;
size_t width, height;
};
std::unordered_map<boo::IWindow*, Window> m_windows;
};
#elif _WIN32_WINNT_WIN7
@@ -36,6 +47,12 @@ struct D3D11Context
{
ComPtr<ID3D11Device1> m_dev;
ComPtr<ID3D11DeviceContext1> m_devCtx;
struct Window
{
IDXGISwapChain1* m_swapChain;
size_t width, height;
};
std::unordered_map<boo::IWindow*, Window> m_windows;
};
#include "boo/System.hpp"

View File

@@ -11,8 +11,8 @@ namespace boo
{
static LogVisor::LogModule Log("WindowWin32");
class WindowWin32;
IGraphicsCommandQueue* _NewD3D12CommandQueue(D3D12Context* ctx, IGraphicsContext* parent);
IGraphicsCommandQueue* _NewD3D11CommandQueue(D3D11Context* ctx, IGraphicsContext* parent);
IGraphicsCommandQueue* _NewD3D12CommandQueue(D3D12Context* ctx, D3D12Context::Window* windowCtx, IGraphicsContext* parent);
IGraphicsCommandQueue* _NewD3D11CommandQueue(D3D11Context* ctx, D3D11Context::Window* windowCtx, IGraphicsContext* parent);
struct GraphicsContextWin32 : IGraphicsContext
{
@@ -47,9 +47,14 @@ public:
scDesc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
#if _WIN32_WINNT_WIN10
IUnknown* dev = d3dCtx.m_ctx12.m_dev ?
static_cast<IUnknown*>(d3dCtx.m_ctx12.m_dev.Get()) :
static_cast<IUnknown*>(d3dCtx.m_ctx11.m_dev.Get());
IUnknown* dev;
if (d3dCtx.m_ctx12.m_dev)
{
scDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL;
dev = static_cast<IUnknown*>(d3dCtx.m_ctx12.m_dev.Get());
}
else
dev = static_cast<IUnknown*>(d3dCtx.m_ctx11.m_dev.Get());
#else
IUnknown* dev = static_cast<IUnknown*>(d3dCtx.m_ctx11.m_dev.Get());
#endif
@@ -63,20 +68,45 @@ public:
#if _WIN32_WINNT_WIN10
if (d3dCtx.m_ctx12.m_dev)
{
auto insIt = d3dCtx.m_ctx12.m_windows.emplace(std::make_pair(parentWindow, D3D12Context::Window()));
D3D12Context::Window& w = insIt.first->second;
m_swapChain.As<IDXGISwapChain3>(&w.m_swapChain);
m_swapChain->GetBuffer(0, __uuidof(ID3D12Resource), &w.m_fb[0]);
m_swapChain->GetBuffer(1, __uuidof(ID3D12Resource), &w.m_fb[1]);
w.m_backBuf = w.m_swapChain->GetCurrentBackBufferIndex();
D3D12_RESOURCE_DESC resDesc = w.m_fb[0]->GetDesc();
w.width = resDesc.Width;
w.height = resDesc.Height;
m_dataFactory = new D3D12DataFactory(this, &d3dCtx.m_ctx12);
m_commandQueue = _NewD3D12CommandQueue(&d3dCtx.m_ctx12, this);
m_commandQueue = _NewD3D12CommandQueue(&d3dCtx.m_ctx12, &w, this);
}
else
#endif
{
auto insIt = d3dCtx.m_ctx11.m_windows.emplace(std::make_pair(parentWindow, D3D11Context::Window()));
D3D11Context::Window& w = insIt.first->second;
ComPtr<ID3D11Texture2D> fbRes;
m_swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), &fbRes);
D3D11_TEXTURE2D_DESC resDesc;
fbRes->GetDesc(&resDesc);
w.width = resDesc.Width;
w.height = resDesc.Height;
m_dataFactory = new D3D11DataFactory(this, &d3dCtx.m_ctx11);
m_commandQueue = _NewD3D11CommandQueue(&d3dCtx.m_ctx11, this);
m_commandQueue = _NewD3D11CommandQueue(&d3dCtx.m_ctx11, &insIt.first->second, this);
}
}
~GraphicsContextWin32()
{
#if _WIN32_WINNT_WIN10
if (m_d3dCtx.m_ctx12.m_dev)
{
m_d3dCtx.m_ctx12.m_windows.erase(m_parentWindow);
}
else
#endif
{
}
}
void _setCallback(IWindowCallback* cb)