Skip to content

Commit 83e8acc

Browse files
authored
Add WinUI3 Support (#1208)
- Update bgfx with [PR that adds support for WinUI3](bkaradzic/bgfx#3044). - Modify WinRT/UWP window contract to take an IInspectable to avoid C++/WinRT versioning issues. - Add some custom code generation for Windows App SDK for ABI-stable headers also to avoid C++/WinRT versioning issues. - Minor fixes in TestUtils for WinRT for consistency.
1 parent 6653a6e commit 83e8acc

File tree

63 files changed

+638737
-123
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+638737
-123
lines changed

Apps/Playground/UWP/App.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,25 @@
1010
#include <Babylon/Polyfills/XMLHttpRequest.h>
1111
#include <Babylon/Polyfills/Canvas.h>
1212

13-
#include <pplawait.h>
14-
#include <winrt/Windows.ApplicationModel.h>
15-
1613
#include <winrt/windows.ui.core.h>
1714

15+
namespace
16+
{
17+
template <typename T>
18+
T from_cx(Platform::Object^ from)
19+
{
20+
T to{ nullptr };
21+
22+
if (from != nullptr)
23+
{
24+
winrt::check_hresult(reinterpret_cast<::IUnknown*>(from)
25+
->QueryInterface(winrt::guid_of<T>(), winrt::put_abi(to)));
26+
}
27+
28+
return to;
29+
}
30+
}
31+
1832
using namespace Windows::ApplicationModel;
1933
using namespace Windows::ApplicationModel::Core;
2034
using namespace Windows::ApplicationModel::Activation;
@@ -348,7 +362,7 @@ void App::RestartRuntime(Windows::Foundation::Rect bounds)
348362
m_displayScale = static_cast<float>(displayInformation->RawPixelsPerViewPixel);
349363
size_t width = static_cast<size_t>(bounds.Width * m_displayScale);
350364
size_t height = static_cast<size_t>(bounds.Height * m_displayScale);
351-
auto* window = reinterpret_cast<winrt::Windows::UI::Core::ICoreWindow*>(CoreWindow::GetForCurrentThread());
365+
auto window = from_cx<winrt::Windows::Foundation::IInspectable>(CoreWindow::GetForCurrentThread());
352366

353367
Babylon::Graphics::WindowConfiguration graphicsConfig{};
354368
graphicsConfig.Window = window;

Apps/ValidationTests/UWP/App.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,25 @@
1111
#include <Babylon/Polyfills/Canvas.h>
1212
#include <Babylon/Polyfills/XMLHttpRequest.h>
1313

14-
#include <pplawait.h>
15-
#include <winrt/Windows.ApplicationModel.h>
16-
1714
#include <winrt/windows.ui.core.h>
1815

16+
namespace
17+
{
18+
template <typename T>
19+
T from_cx(Platform::Object^ from)
20+
{
21+
T to{ nullptr };
22+
23+
if (from != nullptr)
24+
{
25+
winrt::check_hresult(reinterpret_cast<::IUnknown*>(from)
26+
->QueryInterface(winrt::guid_of<T>(), winrt::put_abi(to)));
27+
}
28+
29+
return to;
30+
}
31+
}
32+
1933
using namespace Windows::ApplicationModel;
2034
using namespace Windows::ApplicationModel::Core;
2135
using namespace Windows::ApplicationModel::Activation;
@@ -192,7 +206,7 @@ void App::RestartRuntime(Windows::Foundation::Rect bounds)
192206
m_displayScale = static_cast<float>(displayInformation->RawPixelsPerViewPixel);
193207
size_t width = static_cast<size_t>(bounds.Width * m_displayScale);
194208
size_t height = static_cast<size_t>(bounds.Height * m_displayScale);
195-
auto* window = reinterpret_cast<winrt::Windows::UI::Core::ICoreWindow*>(CoreWindow::GetForCurrentThread());
209+
auto window = from_cx<winrt::Windows::Foundation::IInspectable>(CoreWindow::GetForCurrentThread());
196210

197211
Babylon::Graphics::WindowConfiguration graphicsConfig{};
198212
graphicsConfig.Window = window;

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ option(BABYLON_NATIVE_POLYFILL_CANVAS "Include Babylon Native Polyfill Canvas."
3737

3838
if(APPLE)
3939
# without this option on azure pipelines, there is a mismatch with math.h giving a lot of undefined functions on macOS.
40-
# only enabled for Apple as there is no issue for UWP/WIN32
40+
# only enabled for Apple as there is no issue for Windows
4141
set(CMAKE_NO_SYSTEM_FROM_IMPORTED TRUE)
4242
endif()
4343

@@ -49,7 +49,7 @@ elseif(IOS)
4949
elseif(APPLE)
5050
set(BABYLON_NATIVE_PLATFORM "macOS")
5151
elseif(WINDOWS_STORE)
52-
set(BABYLON_NATIVE_PLATFORM "UWP")
52+
set(BABYLON_NATIVE_PLATFORM "WinRT")
5353
elseif(WIN32)
5454
set(BABYLON_NATIVE_PLATFORM "Win32")
5555
elseif(UNIX)

Core/Graphics/CMakeLists.txt

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,5 @@
1-
cmake_dependent_option(BABYLON_NATIVE_USE_SWAPCHAINPANEL "Use swap chain panel." OFF WINDOWS_STORE OFF)
2-
3-
if(WINDOWS_STORE)
4-
if(BABYLON_NATIVE_USE_SWAPCHAINPANEL)
5-
set(PLATFORM_NAME ${BABYLON_NATIVE_PLATFORM}_SwapChainPanel)
6-
set(PLATFORM_PATH ${BABYLON_NATIVE_PLATFORM}/SwapChainPanel)
7-
else()
8-
set(PLATFORM_NAME ${BABYLON_NATIVE_PLATFORM}_CoreWindow)
9-
set(PLATFORM_PATH ${BABYLON_NATIVE_PLATFORM}/CoreWindow)
10-
endif()
11-
else()
12-
set(PLATFORM_NAME ${BABYLON_NATIVE_PLATFORM})
13-
set(PLATFORM_PATH ${BABYLON_NATIVE_PLATFORM})
14-
endif()
15-
161
set(SOURCES
17-
"Include/Platform/${PLATFORM_PATH}/Babylon/Graphics/Platform.h"
2+
"Include/Platform/${BABYLON_NATIVE_PLATFORM}/Babylon/Graphics/Platform.h"
183
"Include/RendererType/${GRAPHICS_API}/Babylon/Graphics/RendererType.h"
194
"Include/Shared/Babylon/Graphics/Device.h"
205
"InternalInclude/Babylon/Graphics/BgfxCallback.h"
@@ -29,7 +14,7 @@ set(SOURCES
2914
"Source/DeviceContext.cpp"
3015
"Source/DeviceImpl.cpp"
3116
"Source/DeviceImpl.h"
32-
"Source/DeviceImpl_${PLATFORM_NAME}.${BABYLON_NATIVE_PLATFORM_IMPL_EXT}"
17+
"Source/DeviceImpl_${BABYLON_NATIVE_PLATFORM}.${BABYLON_NATIVE_PLATFORM_IMPL_EXT}"
3318
"Source/DeviceImpl_${GRAPHICS_API}.cpp"
3419
"Source/SafeTimespanGuarantor.cpp"
3520
"Source/Texture.cpp")
@@ -39,11 +24,14 @@ warnings_as_errors(Graphics)
3924

4025
target_include_directories(Graphics
4126
PRIVATE "Include/Shared"
42-
PRIVATE "Include/Platform/${PLATFORM_PATH}"
27+
PRIVATE "Include/Platform/${BABYLON_NATIVE_PLATFORM}"
4328
PRIVATE "Include/RendererType/${GRAPHICS_API}"
4429
PRIVATE "InternalInclude/Babylon/Graphics")
4530

46-
if(WIN32 AND NOT WINDOWS_STORE)
31+
if(WINDOWS_STORE)
32+
target_link_to_dependencies(Graphics
33+
PRIVATE WindowsAppSDK)
34+
elseif(WIN32)
4735
target_link_to_dependencies(Graphics
4836
PRIVATE "shlwapi.lib")
4937
elseif(ANDROID)
@@ -78,7 +66,7 @@ endif()
7866
add_library(GraphicsDevice INTERFACE)
7967
target_include_directories(GraphicsDevice
8068
INTERFACE "Include/Shared"
81-
INTERFACE "Include/Platform/${PLATFORM_PATH}"
69+
INTERFACE "Include/Platform/${BABYLON_NATIVE_PLATFORM}"
8270
INTERFACE "Include/RendererType/${GRAPHICS_API}")
8371
target_link_to_dependencies(GraphicsDevice
8472
INTERFACE Graphics

Core/Graphics/Include/Platform/UWP/SwapChainPanel/Babylon/Graphics/Platform.h

Lines changed: 0 additions & 20 deletions
This file was deleted.

Core/Graphics/Include/Platform/UWP/CoreWindow/Babylon/Graphics/Platform.h renamed to Core/Graphics/Include/Platform/WinRT/Babylon/Graphics/Platform.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
#pragma once
22

3-
#include <winrt/windows.ui.core.h>
43
#include <stdint.h>
4+
#include <Unknwn.h>
5+
#include <winrt/base.h>
56

67
namespace Babylon::Graphics
78
{
8-
using WindowType = winrt::Windows::UI::Core::ICoreWindow*;
9+
// Supported types:
10+
// - Windows::Core::UI::ICoreWindow
11+
// - Windows::UI::Xaml::Controls::ISwapChainPanel
12+
// - Microsoft::UI::Xaml::Controls::ISwapChainPanel
13+
using WindowType = winrt::Windows::Foundation::IInspectable;
914

1015
struct WindowConfiguration
1116
{

Core/Graphics/Source/DeviceImpl_UWP_CoreWindow.cpp

Lines changed: 0 additions & 21 deletions
This file was deleted.

Core/Graphics/Source/DeviceImpl_UWP_SwapChainPanel.cpp

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <Babylon/Graphics/Platform.h>
2+
#include "DeviceImpl.h"
3+
#include <Windows.UI.Core.h>
4+
#include <Windows.UI.Xaml.Controls.h>
5+
#include <Microsoft.UI.Xaml.Controls.h>
6+
#include <winrt/Windows.Graphics.Display.h>
7+
8+
namespace Babylon::Graphics
9+
{
10+
const bool DeviceImpl::s_bgfxFlipAfterRender = false;
11+
12+
void DeviceImpl::ConfigureBgfxPlatformData(const WindowConfiguration& config, bgfx::PlatformData& pd)
13+
{
14+
// Assume window is a xaml swap chain panel if not a core window.
15+
if (!config.Window.try_as<ABI::Windows::UI::Core::ICoreWindow>())
16+
{
17+
// Set ndt greater than 1 for xaml swap chain panels.
18+
// See https://github.com/bkaradzic/bgfx/blob/23edb9c4d90744bf90a89ff9e7308b8ff6517fee/src/dxgi.cpp#L531-L552
19+
pd.ndt = reinterpret_cast<void*>(2);
20+
}
21+
22+
pd.nwh = winrt::get_abi(config.Window);
23+
}
24+
25+
float DeviceImpl::GetDevicePixelRatio(const WindowConfiguration& config)
26+
{
27+
if (auto uiElement = config.Window.try_as<ABI::Microsoft::UI::Xaml::IUIElement>())
28+
{
29+
// Use the ABI layer manually generated from the Windows App SDK winmd files to avoid C++/WinRT versioning issues.
30+
DOUBLE rasterizationScale{1};
31+
winrt::check_hresult(uiElement->get_RasterizationScale(&rasterizationScale));
32+
return static_cast<float>(rasterizationScale);
33+
}
34+
else
35+
{
36+
return static_cast<float>(winrt::Windows::Graphics::Display::DisplayInformation::GetForCurrentView().RawPixelsPerViewPixel());
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)