Added Windows OpenGL support. D3D11 stability fixes

This commit is contained in:
Jack Andersen
2015-11-06 15:43:12 -10:00
parent 0923254b39
commit 8951008cca
11 changed files with 558 additions and 114 deletions

View File

@@ -18,16 +18,46 @@
#include "boo/inputdev/DeviceFinder.hpp"
#include <LogVisor/LogVisor.hpp>
DWORD g_mainThreadId = 0;
static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
#if _WIN32_WINNT_WIN10
PFN_D3D12_SERIALIZE_ROOT_SIGNATURE D3D12SerializeRootSignaturePROC = nullptr;
#endif
pD3DCompile D3DCompilePROC = nullptr;
static bool FindBestD3DCompile()
{
HMODULE d3dCompilelib = LoadLibraryW(L"D3DCompiler_47.dll");
if (!d3dCompilelib)
{
d3dCompilelib = LoadLibraryW(L"D3DCompiler_46.dll");
if (!d3dCompilelib)
{
d3dCompilelib = LoadLibraryW(L"D3DCompiler_45.dll");
if (!d3dCompilelib)
{
d3dCompilelib = LoadLibraryW(L"D3DCompiler_44.dll");
if (!d3dCompilelib)
{
d3dCompilelib = LoadLibraryW(L"D3DCompiler_43.dll");
}
}
}
}
if (d3dCompilelib)
{
D3DCompilePROC = (pD3DCompile)GetProcAddress(d3dCompilelib, "D3DCompile");
return D3DCompilePROC != nullptr;
}
return false;
}
namespace boo
{
static LogVisor::LogModule Log("ApplicationWin32");
IWindow* _WindowWin32New(const SystemString& title, D3DAppContext& d3dCtx);
IWindow* _WindowWin32New(const SystemString& title, Boo3DAppContext& d3dCtx);
class ApplicationWin32 final : public IApplication
{
@@ -39,7 +69,7 @@ class ApplicationWin32 final : public IApplication
std::unordered_map<HWND, IWindow*> m_allWindows;
bool m_singleInstance;
D3DAppContext m_d3dCtx;
Boo3DAppContext m_3dCtx;
void _deletedWindow(IWindow* window)
{
@@ -71,9 +101,19 @@ public:
Log.report(LogVisor::FatalError, "unable to find CreateDXGIFactory2 in DXGI.dll\n"
"Windows 7 users should install \"Platform Update for Windows 7\" from Microsoft");
bool no12 = false;
bool noD3d = false;
for (const SystemString& arg : args)
{
if (!arg.compare(L"--d3d11"))
no12 = true;
if (!arg.compare(L"--gl"))
noD3d = true;
}
#if _WIN32_WINNT_WIN10
HMODULE d3d12lib = LoadLibraryW(L"D3D12.dll");
if (d3d12lib)
if (!no12 && !noD3d && d3d12lib)
{
#if _DEBUG
{
@@ -86,6 +126,8 @@ public:
}
}
#endif
if (!FindBestD3DCompile())
Log.report(LogVisor::FatalError, "unable to find D3DCompile_[43-47].dll");
D3D12SerializeRootSignaturePROC =
(PFN_D3D12_SERIALIZE_ROOT_SIGNATURE)GetProcAddress(d3d12lib, "D3D12SerializeRootSignature");
@@ -96,18 +138,18 @@ public:
Log.report(LogVisor::FatalError, "unable to find D3D12CreateDevice in D3D12.dll");
/* Create device */
HRESULT hr = MyD3D12CreateDevice(nullptr, D3D_FEATURE_LEVEL_11_0, __uuidof(ID3D12Device), &m_d3dCtx.m_ctx12.m_dev);
HRESULT hr = MyD3D12CreateDevice(nullptr, D3D_FEATURE_LEVEL_11_0, __uuidof(ID3D12Device), &m_3dCtx.m_ctx12.m_dev);
if (FAILED(hr))
Log.report(LogVisor::FatalError, "unable to create D3D12 device");
/* Obtain DXGI Factory */
hr = MyCreateDXGIFactory2(DXGI_CREATE_FLAGS, __uuidof(IDXGIFactory4), &m_d3dCtx.m_ctx12.m_dxFactory);
hr = MyCreateDXGIFactory2(DXGI_CREATE_FLAGS, __uuidof(IDXGIFactory4), &m_3dCtx.m_ctx12.m_dxFactory);
if (FAILED(hr))
Log.report(LogVisor::FatalError, "unable to create DXGI factory");
/* Establish loader objects */
if (FAILED(m_d3dCtx.m_ctx12.m_dev->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT,
__uuidof(ID3D12CommandAllocator), &m_d3dCtx.m_ctx12.m_loadqalloc)))
if (FAILED(m_3dCtx.m_ctx12.m_dev->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT,
__uuidof(ID3D12CommandAllocator), &m_3dCtx.m_ctx12.m_loadqalloc)))
Log.report(LogVisor::FatalError, "unable to create loader allocator");
D3D12_COMMAND_QUEUE_DESC desc =
@@ -116,53 +158,66 @@ public:
D3D12_COMMAND_QUEUE_PRIORITY_NORMAL,
D3D12_COMMAND_QUEUE_FLAG_NONE
};
if (FAILED(m_d3dCtx.m_ctx12.m_dev->CreateCommandQueue(&desc, __uuidof(ID3D12CommandQueue), &m_d3dCtx.m_ctx12.m_loadq)))
if (FAILED(m_3dCtx.m_ctx12.m_dev->CreateCommandQueue(&desc, __uuidof(ID3D12CommandQueue), &m_3dCtx.m_ctx12.m_loadq)))
Log.report(LogVisor::FatalError, "unable to create loader queue");
if (FAILED(m_d3dCtx.m_ctx12.m_dev->CreateFence(0, D3D12_FENCE_FLAG_NONE, __uuidof(ID3D12Fence), &m_d3dCtx.m_ctx12.m_loadfence)))
if (FAILED(m_3dCtx.m_ctx12.m_dev->CreateFence(0, D3D12_FENCE_FLAG_NONE, __uuidof(ID3D12Fence), &m_3dCtx.m_ctx12.m_loadfence)))
Log.report(LogVisor::FatalError, "unable to create loader fence");
m_d3dCtx.m_ctx12.m_loadfencehandle = CreateEvent(nullptr, FALSE, FALSE, nullptr);
m_3dCtx.m_ctx12.m_loadfencehandle = CreateEvent(nullptr, FALSE, FALSE, nullptr);
if (FAILED(m_d3dCtx.m_ctx12.m_dev->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, m_d3dCtx.m_ctx12.m_loadqalloc.Get(),
nullptr, __uuidof(ID3D12GraphicsCommandList), &m_d3dCtx.m_ctx12.m_loadlist)))
if (FAILED(m_3dCtx.m_ctx12.m_dev->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, m_3dCtx.m_ctx12.m_loadqalloc.Get(),
nullptr, __uuidof(ID3D12GraphicsCommandList), &m_3dCtx.m_ctx12.m_loadlist)))
Log.report(LogVisor::FatalError, "unable to create loader list");
return;
}
#endif
HMODULE d3d11lib = LoadLibraryW(L"D3D11.dll");
if (d3d11lib)
if (d3d11lib && !noD3d)
{
if (!FindBestD3DCompile())
Log.report(LogVisor::FatalError, "unable to find D3DCompile_[43-47].dll");
/* Create device proc */
PFN_D3D11_CREATE_DEVICE MyD3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(d3d11lib, "D3D11CreateDevice");
if (!MyD3D11CreateDevice)
Log.report(LogVisor::FatalError, "unable to find D3D11CreateDevice in D3D11.dll");
/* Create device */
D3D_FEATURE_LEVEL level = D3D_FEATURE_LEVEL_11_1;
D3D_FEATURE_LEVEL level = D3D_FEATURE_LEVEL_11_0;
ComPtr<ID3D11Device> tempDev;
ComPtr<ID3D11DeviceContext> tempCtx;
if (FAILED(MyD3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, D3D11_CREATE_DEVICE_FLAGS, &level,
1, D3D11_SDK_VERSION, &tempDev, nullptr, &tempCtx)))
Log.report(LogVisor::FatalError, "unable to create D3D11.1 device");
tempDev.As<ID3D11Device1>(&m_d3dCtx.m_ctx11.m_dev);
tempCtx.As<ID3D11DeviceContext1>(&m_d3dCtx.m_ctx11.m_devCtx);
Log.report(LogVisor::FatalError, "unable to create D3D11 device");
tempDev.As<ID3D11Device1>(&m_3dCtx.m_ctx11.m_dev);
tempCtx.As<ID3D11DeviceContext1>(&m_3dCtx.m_ctx11.m_devCtx);
/* Obtain DXGI Factory */
ComPtr<IDXGIDevice2> device;
ComPtr<IDXGIAdapter> adapter;
m_d3dCtx.m_ctx11.m_dev.As<IDXGIDevice2>(&device);
m_3dCtx.m_ctx11.m_dev.As<IDXGIDevice2>(&device);
device->GetParent(__uuidof(IDXGIAdapter), &adapter);
adapter->GetParent(__uuidof(IDXGIFactory2), &m_d3dCtx.m_ctx11.m_dxFactory);
adapter->GetParent(__uuidof(IDXGIFactory2), &m_3dCtx.m_ctx11.m_dxFactory);
/* Build default sampler here */
m_d3dCtx.m_ctx11.m_dev->CreateSamplerState(&CD3D11_SAMPLER_DESC(D3D11_DEFAULT), &m_d3dCtx.m_ctx11.m_ss);
m_3dCtx.m_ctx11.m_dev->CreateSamplerState(&CD3D11_SAMPLER_DESC(D3D11_DEFAULT), &m_3dCtx.m_ctx11.m_ss);
return;
}
Log.report(LogVisor::FatalError, "system doesn't support D3D11.1 or D3D12");
/* Finally try OpenGL */
{
/* Obtain DXGI Factory */
HRESULT hr = MyCreateDXGIFactory2(DXGI_CREATE_FLAGS, __uuidof(IDXGIFactory4), &m_3dCtx.m_ctxOgl.m_dxFactory);
if (FAILED(hr))
Log.report(LogVisor::FatalError, "unable to create DXGI factory");
return;
}
Log.report(LogVisor::FatalError, "system doesn't support OGL, D3D11 or D3D12");
}
EPlatformType getPlatformType() const
@@ -203,10 +258,9 @@ public:
}
}
DWORD m_mainThreadId = 0;
int run()
{
m_mainThreadId = GetCurrentThreadId();
g_mainThreadId = GetCurrentThreadId();
/* Spawn client thread */
int clientReturn = 0;
@@ -261,16 +315,16 @@ public:
IWindow* m_mwret = nullptr;
IWindow* newWindow(const SystemString& title)
{
if (GetCurrentThreadId() != m_mainThreadId)
if (GetCurrentThreadId() != g_mainThreadId)
{
std::unique_lock<std::mutex> lk(m_nwmt);
if (!PostThreadMessage(m_mainThreadId, WM_USER, WPARAM(&title), 0))
if (!PostThreadMessage(g_mainThreadId, WM_USER, WPARAM(&title), 0))
Log.report(LogVisor::FatalError, "PostThreadMessage error");
m_nwcv.wait(lk);
return m_mwret;
}
IWindow* window = _WindowWin32New(title, m_d3dCtx);
IWindow* window = _WindowWin32New(title, m_3dCtx);
HWND hwnd = HWND(window->getPlatformHandle());
m_allWindows[hwnd] = window;
return window;

View File

@@ -10,6 +10,8 @@
#include <windows.h>
#include <unordered_map>
extern DWORD g_mainThreadId;
#include "boo/IWindow.hpp"
namespace boo {class IWindow;}
@@ -18,6 +20,8 @@ namespace boo {class IWindow;}
#include <dxgi1_4.h>
#include <d3d12.h>
#include <d3d11_1.h>
#include <d3dcompiler.h>
#include <wingdi.h>
struct D3D12Context
{
@@ -45,6 +49,8 @@ struct D3D12Context
#elif _WIN32_WINNT_WIN7
#include <dxgi1_2.h>
#include <d3d11_1.h>
#include <d3dcompiler.h>
#include <wingdi.h>
#else
#error Unsupported Windows target
#endif
@@ -60,16 +66,43 @@ struct D3D11Context
ComPtr<IDXGISwapChain1> m_swapChain;
bool m_needsResize = false;
size_t width, height;
bool m_needsFSTransition = false;
bool m_fs = false;
DXGI_MODE_DESC m_fsdesc = {};
};
std::unordered_map<const boo::IWindow*, Window> m_windows;
};
struct D3DAppContext
struct OGLContext
{
ComPtr<IDXGIFactory4> m_dxFactory;
HGLRC m_lastContext = 0;
struct Window
{
HWND m_hwnd;
HDC m_deviceContext;
HGLRC m_mainContext;
HGLRC m_renderContext;
bool m_needsResize = false;
size_t width, height;
bool m_fs = false;
LONG m_fsStyle;
LONG m_fsExStyle;
RECT m_fsRect;
int m_fsCountDown = 0;
};
std::unordered_map<const boo::IWindow*, Window> m_windows;
};
struct Boo3DAppContext
{
D3D11Context m_ctx11;
#if _WIN32_WINNT_WIN10
D3D12Context m_ctx12;
#endif
OGLContext m_ctxOgl;
void resize(boo::IWindow* window, size_t width, size_t height)
{
@@ -101,14 +134,16 @@ struct D3DAppContext
win.m_swapChain->GetFullscreenState(&isFScr, nullptr);
return isFScr;
}
else
#endif
if (m_ctx11.m_dev)
{
D3D11Context::Window& win = m_ctx11.m_windows[window];
BOOL isFScr;
win.m_swapChain->GetFullscreenState(&isFScr, nullptr);
return isFScr;
}
OGLContext::Window& win = m_ctxOgl.m_windows[window];
return win.m_fs;
}
bool setFullscreen(boo::IWindow* window, bool fs)
@@ -139,8 +174,8 @@ struct D3DAppContext
win.m_swapChain->SetFullscreenState(false, nullptr);
return true;
}
else
#endif
if (m_ctx11.m_dev)
{
D3D11Context::Window& win = m_ctx11.m_windows[window];
BOOL isFScr;
@@ -156,15 +191,57 @@ struct D3DAppContext
win.m_swapChain->GetContainingOutput(&out);
DXGI_OUTPUT_DESC outDesc;
out->GetDesc(&outDesc);
win.m_swapChain->SetFullscreenState(true, nullptr);
DXGI_MODE_DESC mdesc = {outDesc.DesktopCoordinates.right, outDesc.DesktopCoordinates.bottom};
win.m_swapChain->ResizeTarget(&mdesc);
win.m_fsdesc.Width = outDesc.DesktopCoordinates.right;
win.m_fsdesc.Height = outDesc.DesktopCoordinates.bottom;
}
else
win.m_swapChain->SetFullscreenState(false, nullptr);
win.m_fs = fs;
win.m_needsFSTransition = true;
return true;
}
OGLContext::Window& win = m_ctxOgl.m_windows[window];
if (fs && win.m_fs)
return false;
else if (!fs && !win.m_fs)
return false;
if (fs)
{
win.m_fsStyle = GetWindowLong(win.m_hwnd, GWL_STYLE);
win.m_fsExStyle = GetWindowLong(win.m_hwnd, GWL_EXSTYLE);
GetWindowRect(win.m_hwnd, &win.m_fsRect);
SetWindowLong(win.m_hwnd, GWL_STYLE,
win.m_fsStyle & ~(WS_CAPTION | WS_THICKFRAME));
SetWindowLong(win.m_hwnd, GWL_EXSTYLE,
win.m_fsExStyle & ~(WS_EX_DLGMODALFRAME |
WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE | WS_EX_STATICEDGE));
MONITORINFO monitor_info;
monitor_info.cbSize = sizeof(monitor_info);
GetMonitorInfo(MonitorFromWindow(win.m_hwnd, MONITOR_DEFAULTTONEAREST),
&monitor_info);
SetWindowPos(win.m_hwnd, NULL, monitor_info.rcMonitor.left, monitor_info.rcMonitor.top,
monitor_info.rcMonitor.right - monitor_info.rcMonitor.left,
monitor_info.rcMonitor.bottom - monitor_info.rcMonitor.top,
SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED | SWP_ASYNCWINDOWPOS);
win.m_fs = true;
}
else
{
SetWindowLong(win.m_hwnd, GWL_STYLE, win.m_fsStyle);
SetWindowLong(win.m_hwnd, GWL_EXSTYLE, win.m_fsExStyle);
SetWindowPos(win.m_hwnd, NULL, win.m_fsRect.left, win.m_fsRect.top,
win.m_fsRect.right - win.m_fsRect.left, win.m_fsRect.bottom - win.m_fsRect.top,
SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED | SWP_ASYNCWINDOWPOS);
win.m_fs = false;
}
return true;
}
};

View File

@@ -5,6 +5,18 @@
#include <LogVisor/LogVisor.hpp>
#include "boo/graphicsdev/D3D.hpp"
#include "boo/graphicsdev/GL.hpp"
#include "boo/graphicsdev/wglew.h"
static const int ContextAttribs[] =
{
WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
//WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_DEBUG_BIT_ARB,
//WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
0, 0
};
namespace boo
{
@@ -16,17 +28,25 @@ IGraphicsDataFactory* _NewD3D12DataFactory(D3D12Context* ctx, IGraphicsContext*
#endif
IGraphicsCommandQueue* _NewD3D11CommandQueue(D3D11Context* ctx, D3D11Context::Window* windowCtx, IGraphicsContext* parent);
IGraphicsDataFactory* _NewD3D11DataFactory(D3D11Context* ctx, IGraphicsContext* parent);
IGraphicsCommandQueue* _NewGLCommandQueue(IGraphicsContext* parent);
struct GraphicsContextWin32 : IGraphicsContext
{
EGraphicsAPI m_api;
EPixelFormat m_pf;
IWindow* m_parentWindow;
D3DAppContext& m_d3dCtx;
ComPtr<IDXGISwapChain1> m_swapChain;
Boo3DAppContext& m_3dCtx;
ComPtr<IDXGIOutput> m_output;
GraphicsContextWin32(EGraphicsAPI api, IWindow* parentWindow, Boo3DAppContext& b3dCtx)
: m_api(api),
m_pf(PF_RGBA8),
m_parentWindow(parentWindow),
m_3dCtx(b3dCtx) {}
};
struct GraphicsContextWin32D3D : GraphicsContextWin32
{
ComPtr<IDXGISwapChain1> m_swapChain;
IGraphicsCommandQueue* m_commandQueue = nullptr;
IGraphicsDataFactory* m_dataFactory = nullptr;
@@ -34,11 +54,8 @@ struct GraphicsContextWin32 : IGraphicsContext
public:
IWindowCallback* m_callback;
GraphicsContextWin32(EGraphicsAPI api, IWindow* parentWindow, HWND hwnd, D3DAppContext& d3dCtx)
: m_api(api),
m_pf(PF_RGBA8),
m_parentWindow(parentWindow),
m_d3dCtx(d3dCtx)
GraphicsContextWin32D3D(EGraphicsAPI api, IWindow* parentWindow, HWND hwnd, Boo3DAppContext& b3dCtx)
: GraphicsContextWin32(api, parentWindow, b3dCtx)
{
/* Create Swap Chain */
DXGI_SWAP_CHAIN_DESC1 scDesc = {};
@@ -50,21 +67,21 @@ public:
scDesc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
#if _WIN32_WINNT_WIN10
if (d3dCtx.m_ctx12.m_dev)
if (b3dCtx.m_ctx12.m_dev)
{
auto insIt = d3dCtx.m_ctx12.m_windows.emplace(std::make_pair(parentWindow, D3D12Context::Window()));
auto insIt = b3dCtx.m_ctx12.m_windows.emplace(std::make_pair(parentWindow, D3D12Context::Window()));
D3D12Context::Window& w = insIt.first->second;
ID3D12CommandQueue* cmdQueue;
m_dataFactory = _NewD3D12DataFactory(&d3dCtx.m_ctx12, this);
m_commandQueue = _NewD3D12CommandQueue(&d3dCtx.m_ctx12, &w, this, &cmdQueue);
m_dataFactory = _NewD3D12DataFactory(&b3dCtx.m_ctx12, this);
m_commandQueue = _NewD3D12CommandQueue(&b3dCtx.m_ctx12, &w, this, &cmdQueue);
scDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
HRESULT hr = d3dCtx.m_ctx12.m_dxFactory->CreateSwapChainForHwnd(cmdQueue,
HRESULT hr = b3dCtx.m_ctx12.m_dxFactory->CreateSwapChainForHwnd(cmdQueue,
hwnd, &scDesc, nullptr, nullptr, &m_swapChain);
if (FAILED(hr))
Log.report(LogVisor::FatalError, "unable to create swap chain");
d3dCtx.m_ctx12.m_dxFactory->MakeWindowAssociation(hwnd, DXGI_MWA_NO_ALT_ENTER);
b3dCtx.m_ctx12.m_dxFactory->MakeWindowAssociation(hwnd, DXGI_MWA_NO_ALT_ENTER);
m_swapChain.As<IDXGISwapChain3>(&w.m_swapChain);
ComPtr<ID3D12Resource> fb;
@@ -73,16 +90,19 @@ public:
D3D12_RESOURCE_DESC resDesc = fb->GetDesc();
w.width = resDesc.Width;
w.height = resDesc.Height;
if (FAILED(m_swapChain->GetContainingOutput(&m_output)))
Log.report(LogVisor::FatalError, "unable to get DXGI output");
}
else
#endif
{
if (FAILED(d3dCtx.m_ctx11.m_dxFactory->CreateSwapChainForHwnd(d3dCtx.m_ctx11.m_dev.Get(),
if (FAILED(b3dCtx.m_ctx11.m_dxFactory->CreateSwapChainForHwnd(b3dCtx.m_ctx11.m_dev.Get(),
hwnd, &scDesc, nullptr, nullptr, &m_swapChain)))
Log.report(LogVisor::FatalError, "unable to create swap chain");
d3dCtx.m_ctx11.m_dxFactory->MakeWindowAssociation(hwnd, DXGI_MWA_NO_ALT_ENTER);
b3dCtx.m_ctx11.m_dxFactory->MakeWindowAssociation(hwnd, DXGI_MWA_NO_ALT_ENTER);
auto insIt = d3dCtx.m_ctx11.m_windows.emplace(std::make_pair(parentWindow, D3D11Context::Window()));
auto insIt = b3dCtx.m_ctx11.m_windows.emplace(std::make_pair(parentWindow, D3D11Context::Window()));
D3D11Context::Window& w = insIt.first->second;
m_swapChain.As<IDXGISwapChain1>(&w.m_swapChain);
@@ -92,22 +112,22 @@ public:
fbRes->GetDesc(&resDesc);
w.width = resDesc.Width;
w.height = resDesc.Height;
m_dataFactory = _NewD3D11DataFactory(&d3dCtx.m_ctx11, this);
m_commandQueue = _NewD3D11CommandQueue(&d3dCtx.m_ctx11, &insIt.first->second, this);
}
m_dataFactory = _NewD3D11DataFactory(&b3dCtx.m_ctx11, this);
m_commandQueue = _NewD3D11CommandQueue(&b3dCtx.m_ctx11, &insIt.first->second, this);
if (FAILED(m_swapChain->GetContainingOutput(&m_output)))
Log.report(LogVisor::FatalError, "unable to get DXGI output");
if (FAILED(m_swapChain->GetContainingOutput(&m_output)))
Log.report(LogVisor::FatalError, "unable to get DXGI output");
}
}
~GraphicsContextWin32()
~GraphicsContextWin32D3D()
{
#if _WIN32_WINNT_WIN10
if (m_d3dCtx.m_ctx12.m_dev)
m_d3dCtx.m_ctx12.m_windows.erase(m_parentWindow);
if (m_3dCtx.m_ctx12.m_dev)
m_3dCtx.m_ctx12.m_windows.erase(m_parentWindow);
else
#endif
m_d3dCtx.m_ctx11.m_windows.erase(m_parentWindow);
m_3dCtx.m_ctx11.m_windows.erase(m_parentWindow);
}
void _setCallback(IWindowCallback* cb)
@@ -150,13 +170,183 @@ public:
return m_dataFactory;
}
/* Creates a new context on current thread!! Call from client loading thread */
IGraphicsDataFactory* getLoadContextDataFactory()
{
return m_dataFactory;
}
};
struct GraphicsContextWin32GL : GraphicsContextWin32
{
IGraphicsCommandQueue* m_commandQueue = nullptr;
IGraphicsDataFactory* m_dataFactory = nullptr;
public:
IWindowCallback* m_callback;
GraphicsContextWin32GL(EGraphicsAPI api, IWindow* parentWindow, HWND hwnd, Boo3DAppContext& b3dCtx)
: GraphicsContextWin32(api, parentWindow, b3dCtx)
{
HMONITOR testMon = MonitorFromWindow(hwnd, MONITOR_DEFAULTTOPRIMARY);
ComPtr<IDXGIAdapter1> adapter;
ComPtr<IDXGIOutput> foundOut;
int i=0;
while (b3dCtx.m_ctxOgl.m_dxFactory->EnumAdapters1(i, &adapter) != DXGI_ERROR_NOT_FOUND)
{
int j=0;
ComPtr<IDXGIOutput> out;
while (adapter->EnumOutputs(j, &out) != DXGI_ERROR_NOT_FOUND)
{
DXGI_OUTPUT_DESC desc;
out->GetDesc(&desc);
if (desc.Monitor == testMon)
{
out.As<IDXGIOutput>(&m_output);
break;
}
++j;
}
if (m_output)
break;
++i;
}
if (!m_output)
Log.report(LogVisor::FatalError, "unable to find window's IDXGIOutput");
auto insIt = b3dCtx.m_ctxOgl.m_windows.emplace(std::make_pair(parentWindow, OGLContext::Window()));
OGLContext::Window& w = insIt.first->second;
w.m_hwnd = hwnd;
w.m_deviceContext = GetDC(hwnd);
if (!w.m_deviceContext)
Log.report(LogVisor::FatalError, "unable to create window's device context");
if (!m_3dCtx.m_ctxOgl.m_lastContext)
{
PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, //Flags
PFD_TYPE_RGBA, //The kind of framebuffer. RGBA or palette.
32, //Colordepth of the framebuffer.
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
24, //Number of bits for the depthbuffer
8, //Number of bits for the stencilbuffer
0, //Number of Aux buffers in the framebuffer.
PFD_MAIN_PLANE,
0,
0, 0, 0
};
int pf = ChoosePixelFormat(w.m_deviceContext, &pfd);
SetPixelFormat(w.m_deviceContext, pf, &pfd);
}
w.m_mainContext = wglCreateContext(w.m_deviceContext);
if (!w.m_mainContext)
Log.report(LogVisor::FatalError, "unable to create window's main context");
if (m_3dCtx.m_ctxOgl.m_lastContext)
if (!wglShareLists(w.m_mainContext, m_3dCtx.m_ctxOgl.m_lastContext))
Log.report(LogVisor::FatalError, "unable to share contexts");
m_3dCtx.m_ctxOgl.m_lastContext = w.m_mainContext;
m_dataFactory = new GLDataFactory(this);
m_commandQueue = _NewGLCommandQueue(this);
}
~GraphicsContextWin32GL()
{
m_3dCtx.m_ctxOgl.m_windows.erase(m_parentWindow);
}
void _setCallback(IWindowCallback* cb)
{
m_callback = cb;
}
EGraphicsAPI getAPI() const
{
return m_api;
}
EPixelFormat getPixelFormat() const
{
return m_pf;
}
void setPixelFormat(EPixelFormat pf)
{
if (pf > PF_RGBAF32_Z24)
return;
m_pf = pf;
}
void initializeContext() {}
void makeCurrent()
{
OGLContext::Window& w = m_3dCtx.m_ctxOgl.m_windows[m_parentWindow];
if (!wglMakeCurrent(w.m_deviceContext, w.m_mainContext))
Log.report(LogVisor::FatalError, "unable to make WGL context current");
}
void postInit()
{
OGLContext::Window& w = m_3dCtx.m_ctxOgl.m_windows[m_parentWindow];
wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)
wglGetProcAddress("wglCreateContextAttribsARB");
w.m_renderContext = wglCreateContextAttribsARB(w.m_deviceContext, w.m_mainContext, ContextAttribs);
if (!w.m_renderContext)
Log.report(LogVisor::FatalError, "unable to make new WGL context");
if (!wglMakeCurrent(w.m_deviceContext, w.m_renderContext))
Log.report(LogVisor::FatalError, "unable to make WGL context current");
if (!WGLEW_EXT_swap_control)
Log.report(LogVisor::FatalError, "WGL_EXT_swap_control not available");
wglSwapIntervalEXT(1);
}
void present()
{
OGLContext::Window& w = m_3dCtx.m_ctxOgl.m_windows[m_parentWindow];
if (!SwapBuffers(w.m_deviceContext))
Log.report(LogVisor::FatalError, "SwapBuffers err");
}
IGraphicsCommandQueue* getCommandQueue()
{
return m_commandQueue;
}
IGraphicsDataFactory* getDataFactory()
{
return m_dataFactory;
}
/* Creates a new context on current thread!! Call from client loading thread */
HGLRC m_loadCtx = 0;
IGraphicsDataFactory* getLoadContextDataFactory()
{
OGLContext::Window& w = m_3dCtx.m_ctxOgl.m_windows[m_parentWindow];
if (!m_loadCtx)
{
m_loadCtx = wglCreateContextAttribsARB(w.m_deviceContext, w.m_mainContext, ContextAttribs);
if (!m_loadCtx)
Log.report(LogVisor::FatalError, "unable to make load WGL context");
}
if (!wglMakeCurrent(w.m_deviceContext, m_loadCtx))
Log.report(LogVisor::FatalError, "unable to make load WGL context current");
return m_dataFactory;
}
};
static void genFrameDefault(MONITORINFO* screen, int& xOut, int& yOut, int& wOut, int& hOut)
{
float width = screen->rcMonitor.right * 2.0 / 3.0;
@@ -233,12 +423,20 @@ class WindowWin32 : public IWindow
public:
WindowWin32(const SystemString& title, D3DAppContext& d3dCtx)
WindowWin32(const SystemString& title, Boo3DAppContext& b3dCtx)
{
m_hwnd = CreateWindowW(L"BooWindow", title.c_str(), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, NULL, NULL);
m_gfxCtx.reset(new GraphicsContextWin32(IGraphicsContext::API_D3D11, this, m_hwnd, d3dCtx));
IGraphicsContext::EGraphicsAPI api = IGraphicsContext::API_D3D11;
if (b3dCtx.m_ctx12.m_dev)
api = IGraphicsContext::API_D3D12;
else if (b3dCtx.m_ctxOgl.m_dxFactory)
{
m_gfxCtx.reset(new GraphicsContextWin32GL(IGraphicsContext::API_OPENGL_3_3, this, m_hwnd, b3dCtx));
return;
}
m_gfxCtx.reset(new GraphicsContextWin32D3D(api, this, m_hwnd, b3dCtx));
}
~WindowWin32()
@@ -319,12 +517,12 @@ public:
bool isFullscreen() const
{
return m_gfxCtx->m_d3dCtx.isFullscreen(this);
return m_gfxCtx->m_3dCtx.isFullscreen(this);
}
void setFullscreen(bool fs)
{
m_gfxCtx->m_d3dCtx.setFullscreen(this, fs);
m_gfxCtx->m_3dCtx.setFullscreen(this, fs);
}
void waitForRetrace()
@@ -382,7 +580,7 @@ public:
getWindowFrame(rect.location[0], rect.location[1], rect.size[0], rect.size[1]);
if (!rect.size[0] || !rect.size[1])
return;
m_gfxCtx->m_d3dCtx.resize(this, rect.size[0], rect.size[1]);
m_gfxCtx->m_3dCtx.resize(this, rect.size[0], rect.size[1]);
if (m_callback)
m_callback->resized(rect);
return;
@@ -546,7 +744,7 @@ public:
};
IWindow* _WindowWin32New(const SystemString& title, D3DAppContext& d3dCtx)
IWindow* _WindowWin32New(const SystemString& title, Boo3DAppContext& d3dCtx)
{
return new WindowWin32(title, d3dCtx);
}