mirror of
https://github.com/AxioDL/boo.git
synced 2025-12-14 07:36:26 +00:00
Added Windows OpenGL support. D3D11 stability fixes
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user