From c26fb7b16215fca821adbbf7ae53be7cae6f9745 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Thu, 17 Oct 2024 08:07:45 +1100 Subject: [PATCH] UWP: 3D rendering works sort of --- misc/UWP/Window_UWP.cpp | 62 +++++++++++++++++++++++++++++++---------- src/Core.h | 2 +- src/Graphics_D3D11.c | 54 +++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 16 deletions(-) diff --git a/misc/UWP/Window_UWP.cpp b/misc/UWP/Window_UWP.cpp index ab7467cfa..8fc26d40f 100644 --- a/misc/UWP/Window_UWP.cpp +++ b/misc/UWP/Window_UWP.cpp @@ -1,5 +1,6 @@ #include "../../src/Core.h" +#include #include #include #include @@ -13,6 +14,7 @@ using namespace winrt; using namespace Windows::ApplicationModel; using namespace Windows::ApplicationModel::Core; +using namespace Windows::ApplicationModel::DataTransfer; using namespace Windows::ApplicationModel::Activation; using namespace Windows::Devices::Input; using namespace Windows::Graphics::Display; @@ -27,6 +29,7 @@ using namespace Windows::UI::Input; #include "../../src/Bitmap.h" #include "../../src/Options.h" #include "../../src/Errors.h" +#define UWP_STRING(str) ((wchar_t*)(str)->uni) /*########################################################################################################################* @@ -37,6 +40,7 @@ void Window_PreInit(void) { } void Window_Init(void) { + CoreWindow& window = CoreWindow::GetForCurrentThread(); Input.Sources = INPUT_SOURCE_NORMAL; DisplayInfo.Width = 640; @@ -44,6 +48,13 @@ void Window_Init(void) { DisplayInfo.Depth = 32; DisplayInfo.ScaleX = 1.0f; DisplayInfo.ScaleY = 1.0f; + + Rect bounds = window.Bounds(); + + WindowInfo.UIScaleX = DEFAULT_UI_SCALE_X; + WindowInfo.UIScaleY = DEFAULT_UI_SCALE_Y; + WindowInfo.Width = bounds.Width; + WindowInfo.Height = bounds.Height; } void Window_Free(void) { } @@ -58,9 +69,18 @@ void Window_SetTitle(const cc_string* title) { } void Clipboard_GetText(cc_string* value) { + DataPackageView content = Clipboard::GetContent(); + hstring str = content.GetTextAsync().get(); } void Clipboard_SetText(const cc_string* value) { + cc_winstring raw; + Platform_EncodeString(&raw, value); + auto str = hstring(UWP_STRING(&raw)); + + DataPackage package = DataPackage(); + package.SetText(str); + Clipboard::SetContent(package); } int Window_GetWindowState(void) { @@ -88,6 +108,10 @@ void Window_RequestClose(void) { } void Window_ProcessEvents(float delta) { + CoreWindow& window = CoreWindow::GetForCurrentThread(); + + CoreDispatcher& dispatcher = window.Dispatcher(); + dispatcher.ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent); } void Gamepads_Init(void) { @@ -97,11 +121,18 @@ void Gamepads_Init(void) { void Gamepads_Process(float delta) { } static void Cursor_GetRawPos(int* x, int* y) { - *x = 0; - *y = 0; + CoreWindow& window = CoreWindow::GetForCurrentThread(); + + Point point = window.PointerPosition(); + *x = point.X; + *y = point.Y; } -void Cursor_SetPosition(int x, int y) { +void Cursor_SetPosition(int x, int y) { + CoreWindow& window = CoreWindow::GetForCurrentThread(); + + Point point = Point(x, y); + window.PointerPosition(point); } static void Cursor_DoSetVisible(cc_bool visible) { @@ -209,8 +240,15 @@ void OpenFileDialog(void) { //Windows::Storage::StorageFile file = picker->PickSingleFileAsync(); } -struct CCApp : implements +struct CCApp : implements { + + // IFrameworkViewSource interface + IFrameworkView CreateView() + { + return *this; + } + // IFrameworkView interface void Initialize(CoreApplicationView const& view) { @@ -227,10 +265,11 @@ struct CCApp : implements void Run() { CoreWindow& window = CoreWindow::GetForCurrentThread(); - window.Activate(); + window.Activate(); + Window_Main.Handle.ptr = get_abi(window); - CoreDispatcher& dispatcher = window.Dispatcher(); - dispatcher.ProcessEvents(CoreProcessEventsOption::ProcessUntilQuit); + extern int main(int argc, char** argv); + main(0, NULL); } void SetWindow(CoreWindow const& win) @@ -238,15 +277,8 @@ struct CCApp : implements } }; -struct CCAppSource : implements -{ - IFrameworkView CreateView() { - return make(); - } -}; - int __stdcall wWinMain(void*, void*, wchar_t** argv, int argc) { - auto app = make(); + auto app = make(); CoreApplication::Run(app); } \ No newline at end of file diff --git a/src/Core.h b/src/Core.h index cffa41cd2..c42548045 100644 --- a/src/Core.h +++ b/src/Core.h @@ -190,7 +190,7 @@ typedef cc_uint8 cc_bool; #define CC_BUILD_NOMUSIC #define CC_BUILD_NOSOUNDS #define DEFAULT_NET_BACKEND CC_NET_BACKEND_BUILTIN - #define DEFAULT_GFX_BACKEND CC_GFX_BACKEND_SOFTGPU + #define DEFAULT_GFX_BACKEND CC_GFX_BACKEND_D3D11 #elif defined _WIN32 #define CC_BUILD_WIN #define DEFAULT_NET_BACKEND CC_NET_BACKEND_BUILTIN diff --git a/src/Graphics_D3D11.c b/src/Graphics_D3D11.c index a65bb8143..d79519982 100644 --- a/src/Graphics_D3D11.c +++ b/src/Graphics_D3D11.c @@ -12,8 +12,16 @@ #define NOIME #define COBJMACROS #include + static const GUID guid_ID3D11Texture2D = { 0x6f15aaf2, 0xd208, 0x4e89, { 0x9a, 0xb4, 0x48, 0x95, 0x35, 0xd3, 0x4f, 0x9c } }; static const GUID guid_IXDGIDevice = { 0x54ec77fa, 0x1377, 0x44e6, { 0x8c, 0x32, 0x88, 0xfd, 0x5f, 0x44, 0xc8, 0x4c } }; +static const GUID guid_IDXGIFactory = { 0x7b7166ec, 0x21c7, 0x44ae, { 0xb2, 0x1a, 0xc9, 0xae, 0x32, 0x1a, 0xe3, 0x69 } }; +static const GUID guid_IDXGIDevice = { 0x54ec77fa, 0x1377, 0x44e6, { 0x8c, 0x32, 0x88, 0xfd, 0x5f, 0x44, 0xc8, 0x4c } }; +static const GUID guid_IDXGIFactory2 = { 0x50c83a1c, 0xe072, 0x4c48, { 0x87, 0xb0, 0x36, 0x30, 0xfa, 0x36, 0xa6, 0xd0 } }; +#ifdef CC_BUILD_UWP +#include +#endif + // some generally useful debugging links // https://docs.microsoft.com/en-us/visualstudio/debugger/graphics/visual-studio-graphics-diagnostics @@ -46,6 +54,51 @@ static void PS_UpdateShader(void); static void InitPipeline(void); static void FreePipeline(void); +#ifdef CC_BUILD_UWP +static void LoadD3D11Library(void) { } + +static void CreateDeviceAndSwapChain(void) { + // https://docs.microsoft.com/en-us/windows/uwp/gaming/simple-port-from-direct3d-9-to-11-1-part-1--initializing-direct3d + DWORD createFlags = 0; + D3D_FEATURE_LEVEL fl; + HRESULT hr; +#ifdef _DEBUG + createFlags |= D3D11_CREATE_DEVICE_DEBUG; +#endif + + hr = D3D11CreateDevice(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, + createFlags, NULL, 0, D3D11_SDK_VERSION, + &device, &fl, &context); + if (hr) Logger_Abort2(hr, "Failed to create D3D11 device"); + + Gfx.MaxTexWidth = fl < D3D_FEATURE_LEVEL_11_0 ? 8192 : 16384; + Gfx.MaxTexHeight = fl < D3D_FEATURE_LEVEL_11_0 ? 8192 : 16384; + + DXGI_SWAP_CHAIN_DESC1 desc = { 0 }; + desc.BufferCount = 2; // TODO 1?? + desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; + desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; + desc.SampleDesc.Count = 1; + desc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD; + desc.Scaling = DXGI_SCALING_NONE; + + IDXGIDevice* dxgi_device = NULL; + hr = ID3D11Device_QueryInterface(device, &guid_IDXGIDevice, &dxgi_device); + if (FAILED(hr)) Logger_Abort2(hr, "Querying DXGI device"); + + IDXGIAdapter* dxgi_adapter = NULL; + hr = IDXGIDevice_GetAdapter(dxgi_device , &dxgi_adapter); + if (FAILED(hr)) Logger_Abort2(hr, "Querying DXGI adapter"); + + IDXGIFactory2* dxgi_factory2 = NULL; + hr = IDXGIAdapter_GetParent(dxgi_adapter, &guid_IDXGIFactory2, &dxgi_factory2); + if (FAILED(hr)) Logger_Abort2(hr, "Querying DXGI factory"); + + void* window = Window_Main.Handle.ptr; + hr = IDXGIFactory2_CreateSwapChainForCoreWindow(dxgi_factory2, device, window, &desc, NULL, &swapchain); + if (FAILED(hr)) Logger_Abort2(hr, "Creating swap chain"); +} +#else static PFN_D3D11_CREATE_DEVICE_AND_SWAP_CHAIN _D3D11CreateDeviceAndSwapChain; static void LoadD3D11Library(void) { @@ -99,6 +152,7 @@ static void CreateDeviceAndSwapChain(void) { Gfx.MaxTexWidth = fl < D3D_FEATURE_LEVEL_11_0 ? 8192 : 16384; Gfx.MaxTexHeight = fl < D3D_FEATURE_LEVEL_11_0 ? 8192 : 16384; } +#endif void Gfx_Create(void) { LoadD3D11Library();