Skip to content

Commit

Permalink
Add DAWN_USE_WINDOWS_UI option for Zig & MinGW compilers
Browse files Browse the repository at this point in the history
In Mach engine, we're compiling Dawn using Zig as a C/C++
compiler. Zig provides it's own libc implementation, build
system, and system headers.

Dawn today makes use of newer `windows.ui.*.h` headers introduced in
more recent Windows versions. However, Zig relies on MinGW for it's
system headers and it is not straightforward/desirable to use the
official Windows SDK headers: Zig does not have these headers today.

Since Dawn does not truly require these headers, we are using a
preprocessor directive `DAWN_USE_WINDOWS_UI` to disable this functionality
and enable compilation of Dawn with Zig/MinGW compilers.

I have based the implementation on the existing `DAWN_USE_X11` and
`DAWN_USE_WAYLAND` build configuration approaches.

Signed-off-by: Stephen Gutekanst <[email protected]>
Change-Id: If41cafb95666946115b58567fef753df3fbe940a
  • Loading branch information
slimsag committed Dec 30, 2022
1 parent da69157 commit 48e5dc5
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 12 deletions.
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ set(ENABLE_DESKTOP_GL OFF)
set(ENABLE_VULKAN OFF)
set(USE_WAYLAND OFF)
set(USE_X11 OFF)
set(USE_WINDOWS_UI OFF)
set(BUILD_SAMPLES OFF)
if (WIN32)
set(USE_WINDOWS_UI ON)
set(ENABLE_D3D12 ON)
if (NOT WINDOWS_STORE)
# Enable Vulkan in win32 compilation only
Expand Down Expand Up @@ -129,6 +131,7 @@ option_if_not_defined(DAWN_ENABLE_VULKAN "Enable compilation of the Vulkan backe
option_if_not_defined(DAWN_ALWAYS_ASSERT "Enable assertions on all build types" OFF)
option_if_not_defined(DAWN_USE_WAYLAND "Enable support for Wayland surface" ${USE_WAYLAND})
option_if_not_defined(DAWN_USE_X11 "Enable support for X11 surface" ${USE_X11})
option_if_not_defined(DAWN_USE_WINDOWS_UI "Enable support for Windows UI surface" ${USE_WINDOWS_UI})

option_if_not_defined(DAWN_BUILD_SAMPLES "Enables building Dawn's samples" ${BUILD_SAMPLES})
option_if_not_defined(DAWN_BUILD_NODE_BINDINGS "Enables building Dawn's NodeJS bindings" OFF)
Expand Down Expand Up @@ -270,6 +273,7 @@ message(STATUS "Dawn build Null backend: ${DAWN_ENABLE_NULL}")
message(STATUS "Dawn build with asserts in all configurations: ${DAWN_ALWAYS_ASSERT}")
message(STATUS "Dawn build Wayland support: ${DAWN_USE_WAYLAND}")
message(STATUS "Dawn build X11 support: ${DAWN_USE_X11}")
message(STATUS "Dawn build Windows UI support: ${DAWN_USE_WINDOWS_UI}")

message(STATUS "Dawn build samples: ${DAWN_BUILD_SAMPLES}")
message(STATUS "Dawn build Node bindings: ${DAWN_BUILD_NODE_BINDINGS}")
Expand Down Expand Up @@ -413,6 +417,9 @@ endif()
if (DAWN_USE_X11)
target_compile_definitions(dawn_internal_config INTERFACE "DAWN_USE_X11")
endif()
if (DAWN_USE_WINDOWS_UI)
target_compile_definitions(dawn_internal_config INTERFACE "DAWN_USE_WINDOWS_UI")
endif()
if (WIN32)
target_compile_definitions(dawn_internal_config INTERFACE "NOMINMAX" "WIN32_LEAN_AND_MEAN")
endif()
Expand Down
1 change: 1 addition & 0 deletions scripts/dawn_features.gni
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ if (build_with_chromium) {

# Enable the compilation for UWP
dawn_is_winuwp = is_win && target_os == "winuwp"
dawn_use_windows_ui = is_win

declare_args() {
# TODO(dawn:1545): Re-enable dawn_use_angle on Android. In non-component
Expand Down
3 changes: 3 additions & 0 deletions src/dawn/common/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ config("internal_config") {
if (dawn_use_x11) {
defines += [ "DAWN_USE_X11" ]
}
if (dawn_use_windows_ui) {
defines += [ "DAWN_USE_WINDOWS_UI" ]
}

if (dawn_enable_error_injection) {
defines += [ "DAWN_ENABLE_ERROR_INJECTION" ]
Expand Down
20 changes: 10 additions & 10 deletions src/dawn/native/Surface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
#include "dawn/native/Instance.h"
#include "dawn/native/SwapChain.h"

#if DAWN_PLATFORM_IS(WINDOWS)
#if defined(DAWN_USE_WINDOWS_UI)
#include <windows.ui.core.h>
#include <windows.ui.xaml.controls.h>
#endif // DAWN_PLATFORM_IS(WINDOWS)
#endif // defined(DAWN_USE_WINDOWS_UI)

#if defined(DAWN_USE_X11)
#include "dawn/common/xlib_with_undefs.h"
Expand Down Expand Up @@ -98,7 +98,6 @@ MaybeError ValidateSurfaceDescriptor(const InstanceBase* instance,
}
#endif // DAWN_PLATFORM_IS(ANDROID)

#if DAWN_PLATFORM_IS(WINDOWS)
#if DAWN_PLATFORM_IS(WIN32)
const SurfaceDescriptorFromWindowsHWND* hwndDesc = nullptr;
FindInChain(descriptor->nextInChain, &hwndDesc);
Expand All @@ -107,6 +106,7 @@ MaybeError ValidateSurfaceDescriptor(const InstanceBase* instance,
return {};
}
#endif // DAWN_PLATFORM_IS(WIN32)
#if defined(DAWN_USE_WINDOWS_UI)
const SurfaceDescriptorFromWindowsCoreWindow* coreWindowDesc = nullptr;
FindInChain(descriptor->nextInChain, &coreWindowDesc);
if (coreWindowDesc) {
Expand All @@ -129,7 +129,7 @@ MaybeError ValidateSurfaceDescriptor(const InstanceBase* instance,
"Invalid SwapChainPanel");
return {};
}
#endif // DAWN_PLATFORM_IS(WINDOWS)
#endif // defined(DAWN_USE_WINDOWS_UI)

#if defined(DAWN_USE_WAYLAND)
const SurfaceDescriptorFromWaylandSurface* waylandDesc = nullptr;
Expand Down Expand Up @@ -203,15 +203,15 @@ Surface::Surface(InstanceBase* instance, const SurfaceDescriptor* descriptor)
mHInstance = hwndDesc->hinstance;
mHWND = hwndDesc->hwnd;
} else if (coreWindowDesc) {
#if DAWN_PLATFORM_IS(WINDOWS)
#if defined(DAWN_USE_WINDOWS_UI)
mType = Type::WindowsCoreWindow;
mCoreWindow = static_cast<IUnknown*>(coreWindowDesc->coreWindow);
#endif // DAWN_PLATFORM_IS(WINDOWS)
#endif // defined(DAWN_USE_WINDOWS_UI)
} else if (swapChainPanelDesc) {
#if DAWN_PLATFORM_IS(WINDOWS)
#if defined(DAWN_USE_WINDOWS_UI)
mType = Type::WindowsSwapChainPanel;
mSwapChainPanel = static_cast<IUnknown*>(swapChainPanelDesc->swapChainPanel);
#endif // DAWN_PLATFORM_IS(WINDOWS)
#endif // defined(DAWN_USE_WINDOWS_UI)
} else if (xDesc) {
mType = Type::XlibWindow;
mXDisplay = xDesc->display;
Expand Down Expand Up @@ -283,7 +283,7 @@ void* Surface::GetHWND() const {
IUnknown* Surface::GetCoreWindow() const {
ASSERT(!IsError());
ASSERT(mType == Type::WindowsCoreWindow);
#if DAWN_PLATFORM_IS(WINDOWS)
#if defined(DAWN_USE_WINDOWS_UI)
return mCoreWindow.Get();
#else
return nullptr;
Expand All @@ -293,7 +293,7 @@ IUnknown* Surface::GetCoreWindow() const {
IUnknown* Surface::GetSwapChainPanel() const {
ASSERT(!IsError());
ASSERT(mType == Type::WindowsSwapChainPanel);
#if DAWN_PLATFORM_IS(WINDOWS)
#if defined(DAWN_USE_WINDOWS_UI)
return mSwapChainPanel.Get();
#else
return nullptr;
Expand Down
4 changes: 2 additions & 2 deletions src/dawn/native/Surface.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ class Surface final : public ErrorMonad {
void* mHInstance = nullptr;
void* mHWND = nullptr;

#if DAWN_PLATFORM_IS(WINDOWS)
#if defined(DAWN_USE_WINDOWS_UI)
// WindowsCoreWindow
ComPtr<IUnknown> mCoreWindow;

// WindowsSwapChainPanel
ComPtr<IUnknown> mSwapChainPanel;
#endif // DAWN_PLATFORM_IS(WINDOWS)
#endif // defined(DAWN_USE_WINDOWS_UI)

// Xlib
void* mXDisplay = nullptr;
Expand Down
4 changes: 4 additions & 0 deletions src/dawn/native/d3d12/SwapChainD3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

#include "dawn/native/d3d12/SwapChainD3D12.h"

#if defined(DAWN_USE_WINDOWS_UI)
#include <windows.ui.xaml.media.dxinterop.h>
#endif // defined(DAWN_USE_WINDOWS_UI)

#include <utility>

Expand Down Expand Up @@ -257,6 +259,7 @@ MaybeError SwapChain::InitializeSwapChainFromScratch() {
"Creating the IDXGISwapChain1"));
break;
}
#if defined(DAWN_USE_WINDOWS_UI)
case Surface::Type::WindowsCoreWindow: {
DAWN_TRY(CheckHRESULT(
factory2->CreateSwapChainForCoreWindow(device->GetCommandQueue().Get(),
Expand All @@ -278,6 +281,7 @@ MaybeError SwapChain::InitializeSwapChainFromScratch() {
"Setting SwapChain"));
break;
}
#endif // defined(DAWN_USE_WINDOWS_UI)
default:
UNREACHABLE();
}
Expand Down

0 comments on commit 48e5dc5

Please sign in to comment.