More D3D12 work

This commit is contained in:
Jack Andersen
2015-11-02 18:19:41 -10:00
parent 4f650ce5f5
commit facacd4e95
11 changed files with 719 additions and 238 deletions

View File

@@ -1,32 +1,41 @@
#define _CRT_SECURE_NO_WARNINGS 1 /* STFU MSVC */
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN 1
#endif
#include <windows.h>
#include "Win32Common.hpp"
#include <shellapi.h>
#include <initguid.h>
#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
#include <unordered_map>
#include "boo/System.hpp"
#include "boo/IApplication.hpp"
#include "boo/inputdev/DeviceFinder.hpp"
#include <LogVisor/LogVisor.hpp>
namespace boo
{
static LogVisor::LogModule Log("ApplicationWin32");
IWindow* _WindowWin32New(const SystemString& title);
IWindow* _WindowWin32New(const SystemString& title, D3DAppContext& d3dCtx);
class ApplicationWin32 final : public IApplication
{
const IApplicationCallback& m_callback;
IApplicationCallback& m_callback;
const SystemString m_uniqueName;
const SystemString m_friendlyName;
const SystemString m_pname;
const std::vector<SystemString> m_args;
std::unordered_map<HWND, IWindow*> m_allWindows;
bool m_singleInstance;
D3DAppContext m_d3dCtx;
void _deletedWindow(IWindow* window)
{
m_allWindows.erase(HWND(window->getPlatformHandle()));
@@ -34,7 +43,7 @@ class ApplicationWin32 final : public IApplication
public:
ApplicationWin32(const IApplicationCallback& callback,
ApplicationWin32(IApplicationCallback& callback,
const SystemString& uniqueName,
const SystemString& friendlyName,
const SystemString& pname,
@@ -46,7 +55,55 @@ public:
m_pname(pname),
m_args(args),
m_singleInstance(singleInstance)
{}
{
HMODULE dxgilib = LoadLibraryW(L"dxgi.dll");
if (!dxgilib)
Log.report(LogVisor::FatalError, "unable to load dxgi.dll");
typedef HRESULT(WINAPI*CreateDXGIFactory2PROC)(UINT Flags, REFIID riid, _COM_Outptr_ void **ppFactory);
CreateDXGIFactory2PROC MyCreateDXGIFactory2 = (CreateDXGIFactory2PROC)GetProcAddress(dxgilib, "CreateDXGIFactory2");
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)
{
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");
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");
return;
}
#endif
HMODULE d3d11lib = LoadLibraryW(L"D3D11.dll");
if (d3d11lib)
{
/* 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;
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);
return;
}
Log.report(LogVisor::FatalError, "system doesn't support D3D11.1 or D3D12");
}
EPlatformType getPlatformType() const
{
@@ -65,13 +122,32 @@ public:
case WM_DEVICECHANGE:
return DeviceFinder::winDevChangedHandler(wParam, lParam);
case WM_SIZE:
case WM_KEYDOWN:
case WM_KEYUP:
case WM_LBUTTONDOWN:
case WM_LBUTTONUP:
case WM_RBUTTONDOWN:
case WM_RBUTTONUP:
case WM_MBUTTONDOWN:
case WM_MBUTTONUP:
case WM_XBUTTONDOWN:
case WM_XBUTTONUP:
case WM_MOUSEMOVE:
window->_incomingEvent(&HWNDEvent(uMsg, wParam, lParam));
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
}
void pump()
int run()
{
/* Spawn client thread */
int clientReturn = 0;
std::thread clientThread([&]()
{clientReturn = m_callback.appMain(this);});
/* Pump messages */
MSG msg = {0};
while (GetMessage(&msg, NULL, 0, 0))
@@ -79,6 +155,10 @@ public:
TranslateMessage(&msg);
DispatchMessage(&msg);
}
m_callback.appQuitting(this);
clientThread.join();
return clientReturn;
}
const SystemString& getUniqueName() const
@@ -103,7 +183,7 @@ public:
IWindow* newWindow(const SystemString& title)
{
IWindow* window = _WindowWin32New(title);
IWindow* window = _WindowWin32New(title, m_d3dCtx);
HWND hwnd = HWND(window->getPlatformHandle());
m_allWindows[hwnd] = window;
return window;
@@ -111,23 +191,21 @@ public:
};
IApplication* APP = NULL;
std::unique_ptr<IApplication>
ApplicationBootstrap(IApplication::EPlatformType platform,
IApplicationCallback& cb,
const SystemString& uniqueName,
const SystemString& friendlyName,
const SystemString& pname,
const std::vector<SystemString>& args,
bool singleInstance)
int ApplicationRun(IApplication::EPlatformType platform,
IApplicationCallback& cb,
const SystemString& uniqueName,
const SystemString& friendlyName,
const SystemString& pname,
const std::vector<SystemString>& args,
bool singleInstance)
{
if (!APP)
{
if (platform != IApplication::PLAT_WIN32 &&
platform != IApplication::PLAT_AUTO)
return NULL;
APP = new ApplicationWin32(cb, uniqueName, friendlyName, pname, args, singleInstance);
}
return std::unique_ptr<IApplication>(APP);
if (APP)
return 1;
if (platform != IApplication::PLAT_WIN32 &&
platform != IApplication::PLAT_AUTO)
return 1;
APP = new ApplicationWin32(cb, uniqueName, friendlyName, pname, args, singleInstance);
return APP->run();
}
}
@@ -154,7 +232,7 @@ static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM l
int wmain(int argc, wchar_t** argv);
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE, LPWSTR lpCmdLine, int)
{
#if DEBUG
#if _DEBUG
/* Debug console */
AllocConsole();
freopen("CONOUT$", "w", stdout);