2019-08-19 23:08:54 +00:00
|
|
|
#include "lib/win/UWPCommon.hpp"
|
2017-10-24 03:09:50 +00:00
|
|
|
|
|
|
|
using namespace Windows::Foundation;
|
|
|
|
using namespace Windows::UI::Core;
|
|
|
|
using namespace Windows::ApplicationModel::Activation;
|
2017-12-07 04:08:44 +00:00
|
|
|
using namespace Windows::ApplicationModel::Core;
|
2017-10-24 03:09:50 +00:00
|
|
|
using namespace Platform;
|
|
|
|
|
|
|
|
#if _DEBUG
|
|
|
|
#define D3D11_CREATE_DEVICE_FLAGS D3D11_CREATE_DEVICE_DEBUG
|
|
|
|
#else
|
|
|
|
#define D3D11_CREATE_DEVICE_FLAGS 0
|
|
|
|
#endif
|
|
|
|
|
2019-08-19 23:08:54 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
|
2017-10-24 03:09:50 +00:00
|
|
|
#include "boo/IApplication.hpp"
|
2019-08-19 23:08:54 +00:00
|
|
|
#include "boo/System.hpp"
|
2017-12-07 04:08:44 +00:00
|
|
|
#include "boo/UWPViewProvider.hpp"
|
2019-08-19 23:08:54 +00:00
|
|
|
#include "boo/graphicsdev/D3D.hpp"
|
|
|
|
#include "boo/inputdev/DeviceFinder.hpp"
|
|
|
|
|
|
|
|
#include <logvisor/logvisor.hpp>
|
2017-10-24 03:09:50 +00:00
|
|
|
|
|
|
|
#if _WIN32_WINNT_WIN10
|
|
|
|
PFN_D3D12_SERIALIZE_ROOT_SIGNATURE D3D12SerializeRootSignaturePROC = nullptr;
|
|
|
|
#endif
|
|
|
|
pD3DCompile D3DCompilePROC = nullptr;
|
|
|
|
pD3DCreateBlob D3DCreateBlobPROC = nullptr;
|
|
|
|
|
2018-12-08 05:17:51 +00:00
|
|
|
static bool FindBestD3DCompile() {
|
|
|
|
D3DCompilePROC = D3DCompile;
|
|
|
|
D3DCreateBlobPROC = D3DCreateBlob;
|
|
|
|
return D3DCompilePROC != nullptr && D3DCreateBlobPROC != nullptr;
|
2017-10-24 03:09:50 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:17:51 +00:00
|
|
|
namespace boo {
|
2017-10-24 03:09:50 +00:00
|
|
|
static logvisor::Module Log("boo::ApplicationUWP");
|
|
|
|
|
2018-01-10 06:15:18 +00:00
|
|
|
std::shared_ptr<IWindow> _WindowUWPNew(SystemStringView title, Boo3DAppContextUWP& d3dCtx);
|
2017-10-24 03:09:50 +00:00
|
|
|
|
2018-12-08 05:17:51 +00:00
|
|
|
class ApplicationUWP final : public IApplication {
|
|
|
|
friend ref class AppView;
|
|
|
|
IApplicationCallback& m_callback;
|
|
|
|
const SystemString m_uniqueName;
|
|
|
|
const SystemString m_friendlyName;
|
|
|
|
const SystemString m_pname;
|
|
|
|
const std::vector<SystemString> m_args;
|
|
|
|
std::shared_ptr<IWindow> m_window;
|
|
|
|
bool m_singleInstance;
|
|
|
|
bool m_issuedWindow = false;
|
2017-10-24 03:09:50 +00:00
|
|
|
|
2018-12-08 05:17:51 +00:00
|
|
|
Boo3DAppContextUWP m_3dCtx;
|
2017-10-24 03:09:50 +00:00
|
|
|
|
2019-08-13 00:52:20 +00:00
|
|
|
void _deletedWindow(IWindow* window) override {}
|
2017-10-24 03:09:50 +00:00
|
|
|
|
2018-12-08 05:17:51 +00:00
|
|
|
public:
|
|
|
|
ApplicationUWP(IApplicationCallback& callback, SystemStringView uniqueName, SystemStringView friendlyName,
|
|
|
|
SystemStringView pname, const std::vector<SystemString>& args, bool singleInstance)
|
|
|
|
: m_callback(callback)
|
|
|
|
, m_uniqueName(uniqueName)
|
|
|
|
, m_friendlyName(friendlyName)
|
|
|
|
, m_pname(pname)
|
|
|
|
, m_args(args)
|
|
|
|
, m_singleInstance(singleInstance) {
|
2019-08-16 08:11:27 +00:00
|
|
|
using CreateDXGIFactory1PROC = HRESULT (WINAPI *)(REFIID riid, _COM_Outptr_ void** ppFactory);
|
2018-12-08 05:17:51 +00:00
|
|
|
CreateDXGIFactory1PROC MyCreateDXGIFactory1 = CreateDXGIFactory1;
|
|
|
|
|
|
|
|
bool no12 = true;
|
|
|
|
for (const SystemString& arg : args)
|
|
|
|
if (!arg.compare(L"--d3d12"))
|
|
|
|
no12 = false;
|
2017-10-24 03:09:50 +00:00
|
|
|
|
|
|
|
#if _WIN32_WINNT_WIN10
|
2018-12-08 05:17:51 +00:00
|
|
|
if (!no12) {
|
|
|
|
if (!FindBestD3DCompile())
|
2019-07-20 04:22:36 +00:00
|
|
|
Log.report(logvisor::Fatal, fmt("unable to find D3DCompile_[43-47].dll"));
|
2018-12-08 05:17:51 +00:00
|
|
|
|
|
|
|
D3D12SerializeRootSignaturePROC = D3D12SerializeRootSignature;
|
|
|
|
|
|
|
|
/* Create device */
|
|
|
|
PFN_D3D12_CREATE_DEVICE MyD3D12CreateDevice = D3D12CreateDevice;
|
|
|
|
|
|
|
|
/* Obtain DXGI Factory */
|
|
|
|
HRESULT hr = MyCreateDXGIFactory1(__uuidof(IDXGIFactory2), &m_3dCtx.m_ctx12.m_dxFactory);
|
|
|
|
if (FAILED(hr))
|
2019-07-20 04:22:36 +00:00
|
|
|
Log.report(logvisor::Fatal, fmt("unable to create DXGI factory"));
|
2018-12-08 05:17:51 +00:00
|
|
|
|
|
|
|
/* Adapter */
|
|
|
|
ComPtr<IDXGIAdapter1> ppAdapter;
|
|
|
|
for (UINT adapterIndex = 0;; ++adapterIndex) {
|
|
|
|
ComPtr<IDXGIAdapter1> pAdapter;
|
|
|
|
if (DXGI_ERROR_NOT_FOUND == m_3dCtx.m_ctx12.m_dxFactory->EnumAdapters1(adapterIndex, &pAdapter))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Check to see if the adapter supports Direct3D 12, but don't create the
|
|
|
|
// actual device yet.
|
|
|
|
if (SUCCEEDED(MyD3D12CreateDevice(pAdapter.Get(), D3D_FEATURE_LEVEL_11_0, _uuidof(ID3D12Device), nullptr))) {
|
|
|
|
ppAdapter = std::move(pAdapter);
|
|
|
|
break;
|
2017-10-24 03:09:50 +00:00
|
|
|
}
|
2018-12-08 05:17:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Create device */
|
|
|
|
hr = ppAdapter ? MyD3D12CreateDevice(ppAdapter.Get(), D3D_FEATURE_LEVEL_11_0, __uuidof(ID3D12Device),
|
|
|
|
&m_3dCtx.m_ctx12.m_dev)
|
|
|
|
: E_FAIL;
|
|
|
|
if (!FAILED(hr)) {
|
|
|
|
/* Establish loader objects */
|
|
|
|
if (FAILED(m_3dCtx.m_ctx12.m_dev->CreateCommandAllocator(
|
|
|
|
D3D12_COMMAND_LIST_TYPE_DIRECT, __uuidof(ID3D12CommandAllocator), &m_3dCtx.m_ctx12.m_loadqalloc)))
|
2019-07-20 04:22:36 +00:00
|
|
|
Log.report(logvisor::Fatal, fmt("unable to create loader allocator"));
|
2018-12-08 05:17:51 +00:00
|
|
|
|
|
|
|
D3D12_COMMAND_QUEUE_DESC desc = {D3D12_COMMAND_LIST_TYPE_DIRECT, D3D12_COMMAND_QUEUE_PRIORITY_NORMAL,
|
|
|
|
D3D12_COMMAND_QUEUE_FLAG_NONE};
|
|
|
|
if (FAILED(m_3dCtx.m_ctx12.m_dev->CreateCommandQueue(&desc, __uuidof(ID3D12CommandQueue),
|
|
|
|
&m_3dCtx.m_ctx12.m_loadq)))
|
2019-07-20 04:22:36 +00:00
|
|
|
Log.report(logvisor::Fatal, fmt("unable to create loader queue"));
|
2018-12-08 05:17:51 +00:00
|
|
|
|
|
|
|
if (FAILED(m_3dCtx.m_ctx12.m_dev->CreateFence(0, D3D12_FENCE_FLAG_NONE, __uuidof(ID3D12Fence),
|
|
|
|
&m_3dCtx.m_ctx12.m_loadfence)))
|
2019-07-20 04:22:36 +00:00
|
|
|
Log.report(logvisor::Fatal, fmt("unable to create loader fence"));
|
2018-12-08 05:17:51 +00:00
|
|
|
|
|
|
|
m_3dCtx.m_ctx12.m_loadfencehandle = CreateEvent(nullptr, FALSE, FALSE, nullptr);
|
|
|
|
|
|
|
|
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)))
|
2019-07-20 04:22:36 +00:00
|
|
|
Log.report(logvisor::Fatal, fmt("unable to create loader list"));
|
2018-12-08 05:17:51 +00:00
|
|
|
|
2019-07-20 04:22:36 +00:00
|
|
|
Log.report(logvisor::Info, fmt("initialized D3D12 renderer"));
|
2018-12-08 05:17:51 +00:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
/* Some Win10 client HW doesn't support D3D12 (despite being supposedly HW-agnostic) */
|
|
|
|
m_3dCtx.m_ctx12.m_dev.Reset();
|
|
|
|
m_3dCtx.m_ctx12.m_dxFactory.Reset();
|
|
|
|
}
|
2017-10-24 03:09:50 +00:00
|
|
|
}
|
2018-12-08 05:17:51 +00:00
|
|
|
#endif
|
2017-10-24 03:09:50 +00:00
|
|
|
{
|
2018-12-08 05:17:51 +00:00
|
|
|
if (!FindBestD3DCompile())
|
2019-07-20 04:22:36 +00:00
|
|
|
Log.report(logvisor::Fatal, fmt("unable to find D3DCompile_[43-47].dll"));
|
2018-12-08 05:17:51 +00:00
|
|
|
|
|
|
|
/* Create device proc */
|
|
|
|
PFN_D3D11_CREATE_DEVICE MyD3D11CreateDevice = D3D11CreateDevice;
|
|
|
|
|
|
|
|
/* Create device */
|
|
|
|
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)))
|
2019-07-20 04:22:36 +00:00
|
|
|
Log.report(logvisor::Fatal, fmt("unable to create D3D11 device"));
|
2018-12-08 05:17:51 +00:00
|
|
|
|
|
|
|
ComPtr<IDXGIDevice2> device;
|
|
|
|
if (FAILED(tempDev.As<ID3D11Device1>(&m_3dCtx.m_ctx11.m_dev)) || !m_3dCtx.m_ctx11.m_dev ||
|
|
|
|
FAILED(tempCtx.As<ID3D11DeviceContext1>(&m_3dCtx.m_ctx11.m_devCtx)) || !m_3dCtx.m_ctx11.m_devCtx ||
|
|
|
|
FAILED(m_3dCtx.m_ctx11.m_dev.As<IDXGIDevice2>(&device)) || !device) {
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Obtain DXGI Factory */
|
|
|
|
ComPtr<IDXGIAdapter> adapter;
|
|
|
|
device->GetParent(__uuidof(IDXGIAdapter), &adapter);
|
|
|
|
adapter->GetParent(__uuidof(IDXGIFactory2), &m_3dCtx.m_ctx11.m_dxFactory);
|
|
|
|
|
|
|
|
/* Build default sampler here */
|
|
|
|
CD3D11_SAMPLER_DESC sampDesc(D3D11_DEFAULT);
|
|
|
|
sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
|
|
|
|
sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
|
|
|
|
sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
|
|
|
|
m_3dCtx.m_ctx11.m_dev->CreateSamplerState(&sampDesc, &m_3dCtx.m_ctx11.m_ss[0]);
|
|
|
|
|
|
|
|
sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_BORDER;
|
|
|
|
sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_BORDER;
|
|
|
|
sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_BORDER;
|
|
|
|
m_3dCtx.m_ctx11.m_dev->CreateSamplerState(&sampDesc, &m_3dCtx.m_ctx11.m_ss[1]);
|
|
|
|
|
2019-07-20 04:22:36 +00:00
|
|
|
Log.report(logvisor::Info, fmt("initialized D3D11 renderer"));
|
2018-12-08 05:17:51 +00:00
|
|
|
return;
|
2017-10-24 03:09:50 +00:00
|
|
|
}
|
|
|
|
|
2019-07-20 04:22:36 +00:00
|
|
|
Log.report(logvisor::Fatal, fmt("system doesn't support D3D11 or D3D12"));
|
2018-12-08 05:17:51 +00:00
|
|
|
}
|
2017-12-08 02:52:38 +00:00
|
|
|
|
2019-08-13 00:52:20 +00:00
|
|
|
EPlatformType getPlatformType() const override { return EPlatformType::UWP; }
|
2017-10-24 03:09:50 +00:00
|
|
|
|
2018-12-08 05:17:51 +00:00
|
|
|
std::thread m_clientThread;
|
2019-08-13 00:52:20 +00:00
|
|
|
int run() override {
|
2018-12-08 05:17:51 +00:00
|
|
|
/* Spawn client thread */
|
|
|
|
int clientReturn = 0;
|
|
|
|
m_clientThread = std::thread([&]() {
|
|
|
|
std::string thrName = WCSTMBS(getFriendlyName().data()) + " Client Thread";
|
|
|
|
logvisor::RegisterThreadName(thrName.c_str());
|
|
|
|
clientReturn = m_callback.appMain(this);
|
|
|
|
});
|
2017-12-08 02:52:38 +00:00
|
|
|
|
2018-12-08 05:17:51 +00:00
|
|
|
CoreWindow::GetForCurrentThread()->Activate();
|
|
|
|
CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessUntilQuit);
|
|
|
|
return 0;
|
|
|
|
}
|
2017-10-24 03:09:50 +00:00
|
|
|
|
2018-12-08 05:17:51 +00:00
|
|
|
void quit() {
|
|
|
|
m_callback.appQuitting(this);
|
|
|
|
if (m_clientThread.joinable())
|
|
|
|
m_clientThread.join();
|
|
|
|
}
|
2017-10-24 03:09:50 +00:00
|
|
|
|
2019-08-13 00:52:20 +00:00
|
|
|
SystemStringView getUniqueName() const override { return m_uniqueName; }
|
2017-10-24 03:09:50 +00:00
|
|
|
|
2019-08-13 00:52:20 +00:00
|
|
|
SystemStringView getFriendlyName() const override { return m_friendlyName; }
|
2017-10-24 03:09:50 +00:00
|
|
|
|
2019-08-13 00:52:20 +00:00
|
|
|
SystemStringView getProcessName() const override { return m_pname; }
|
2017-10-24 03:09:50 +00:00
|
|
|
|
2019-08-13 00:52:20 +00:00
|
|
|
const std::vector<SystemString>& getArgs() const override { return m_args; }
|
2018-12-08 05:17:51 +00:00
|
|
|
|
2019-08-13 00:52:20 +00:00
|
|
|
std::shared_ptr<IWindow> newWindow(SystemStringView title, uint32_t sampleCount) override {
|
2018-12-08 05:17:51 +00:00
|
|
|
if (!m_issuedWindow) {
|
|
|
|
m_issuedWindow = true;
|
|
|
|
return m_window;
|
2017-10-24 03:09:50 +00:00
|
|
|
}
|
2018-12-08 05:17:51 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
void _setWindow(CoreWindow ^ window) { m_window = _WindowUWPNew(m_friendlyName, m_3dCtx); }
|
2017-10-24 03:09:50 +00:00
|
|
|
};
|
|
|
|
|
2019-08-16 07:47:57 +00:00
|
|
|
IApplication* APP = nullptr;
|
2018-12-08 05:17:51 +00:00
|
|
|
ref class AppView sealed : public IFrameworkView {
|
|
|
|
ApplicationUWP m_app;
|
2017-10-24 03:09:50 +00:00
|
|
|
|
2018-12-08 05:17:51 +00:00
|
|
|
internal : AppView(IApplicationCallback& callback, SystemStringView uniqueName, SystemStringView friendlyName,
|
|
|
|
SystemStringView pname, const std::vector<SystemString>& args, bool singleInstance)
|
|
|
|
: m_app(callback, uniqueName, friendlyName, pname, args, singleInstance) {
|
|
|
|
APP = &m_app;
|
|
|
|
}
|
2017-10-24 03:09:50 +00:00
|
|
|
|
2018-12-08 05:17:51 +00:00
|
|
|
public:
|
|
|
|
virtual void Initialize(CoreApplicationView ^ applicationView) {
|
|
|
|
applicationView->Activated +=
|
|
|
|
ref new TypedEventHandler<CoreApplicationView ^, IActivatedEventArgs ^>(this, &AppView::OnActivated);
|
|
|
|
}
|
2017-10-24 03:09:50 +00:00
|
|
|
|
2018-12-08 05:17:51 +00:00
|
|
|
virtual void SetWindow(CoreWindow ^ window) { m_app._setWindow(window); }
|
2017-10-24 03:09:50 +00:00
|
|
|
|
2018-12-08 05:17:51 +00:00
|
|
|
virtual void Load(String ^ entryPoint) {}
|
2017-10-24 03:09:50 +00:00
|
|
|
|
2018-12-08 05:17:51 +00:00
|
|
|
virtual void Run() { m_app.run(); }
|
2017-10-24 03:09:50 +00:00
|
|
|
|
2018-12-08 05:17:51 +00:00
|
|
|
virtual void Uninitialize() { m_app.quit(); }
|
2017-10-24 03:09:50 +00:00
|
|
|
|
2018-12-08 05:17:51 +00:00
|
|
|
void OnActivated(CoreApplicationView ^ applicationView, IActivatedEventArgs ^ args) {
|
|
|
|
CoreWindow::GetForCurrentThread()->Activate();
|
|
|
|
}
|
2017-10-24 03:09:50 +00:00
|
|
|
};
|
|
|
|
|
2018-12-08 05:17:51 +00:00
|
|
|
IFrameworkView ^ ViewProvider::CreateView() {
|
|
|
|
return ref new AppView(m_appCb, m_uniqueName, m_friendlyName, m_pname, m_args, m_singleInstance);
|
2017-10-24 03:09:50 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:17:51 +00:00
|
|
|
} // namespace boo
|