From 02336e6f9993f02b8c4bff868b1b9827a59accfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E4=BF=8A=E5=98=89?= Date: Fri, 23 Apr 2021 02:16:12 +0000 Subject: [PATCH] Add UWP support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add necessary cflags and cflags_cc for winrt compilation. Add 'dawn_is_winuwp'. Set 'dawn_enable_vulkan' and 'dawn_supports_glfw_for_windowing' when compiling for UWP. Link d3d12, d3d11 and dxgi stub libs when compiling for UWP. Use LoadPackagedLibrary instead of LoadLibraryA in DynamicLib when compiling for UWP. Swapchain related changes will be in another commit. Bug: dawn:766 Change-Id: I1210798a21cc175bab77281403d262d4bfb02d99 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/48480 Commit-Queue: 陈俊嘉 Reviewed-by: Rafael Cintron --- scripts/dawn_features.gni | 11 +++++-- src/common/BUILD.gn | 10 ++++++ src/common/DynamicLib.cpp | 8 ++++- src/common/PlacementAllocated.h | 5 +++ src/common/Platform.h | 8 +++++ src/dawn_native/BUILD.gn | 14 +++++++++ src/dawn_native/Surface.cpp | 4 +-- src/dawn_native/d3d12/PlatformFunctions.cpp | 35 ++++++++++++++++++++- src/utils/BUILD.gn | 2 +- 9 files changed, 89 insertions(+), 8 deletions(-) diff --git a/scripts/dawn_features.gni b/scripts/dawn_features.gni index 924f240420..117c661843 100644 --- a/scripts/dawn_features.gni +++ b/scripts/dawn_features.gni @@ -26,6 +26,9 @@ if (build_with_chromium) { } } +# Enable the compilation for UWP +dawn_is_winuwp = is_win && target_os == "winuwp" + declare_args() { dawn_use_angle = true @@ -62,8 +65,9 @@ declare_args() { dawn_enable_opengl = is_linux && !is_chromeos # Enables the compilation of Dawn's Vulkan backend - dawn_enable_vulkan = is_linux || is_chromeos || is_win || is_fuchsia || - is_android || dawn_use_swiftshader + # Disables vulkan when compiling for UWP, since UWP only supports d3d + dawn_enable_vulkan = is_linux || is_chromeos || (is_win && !dawn_is_winuwp) || + is_fuchsia || is_android || dawn_use_swiftshader # Enable use of reflection compiler in spirv-cross. This is needed # if performing reflection on systems that the platform language @@ -90,5 +94,6 @@ declare_args() { dawn_enable_vulkan_loader = dawn_enable_vulkan && is_mac } +# UWP only supports CoreWindow for windowing dawn_supports_glfw_for_windowing = - is_win || (is_linux && !is_chromeos) || is_mac + (is_win && !dawn_is_winuwp) || (is_linux && !is_chromeos) || is_mac diff --git a/src/common/BUILD.gn b/src/common/BUILD.gn index 176e82ea41..c7dc03047d 100644 --- a/src/common/BUILD.gn +++ b/src/common/BUILD.gn @@ -129,6 +129,16 @@ config("dawn_internal") { # Dawn extends wgpu enums with internal enums. # MSVC considers these invalid switch values. crbug.com/dawn/397. cflags += [ "/wd4063" ] + if (dawn_is_winuwp) { + # /ZW makes sure we don't add calls that are forbidden in UWP. + # and /EHsc is required to be used in combination with it, + # even if it is already added by the windows GN defaults, + # we still add it to make every /ZW paired with a /EHsc + cflags_cc = [ + "/ZW:nostdlib", + "/EHsc", + ] + } } } diff --git a/src/common/DynamicLib.cpp b/src/common/DynamicLib.cpp index 6de7ced7f3..119ec42f4a 100644 --- a/src/common/DynamicLib.cpp +++ b/src/common/DynamicLib.cpp @@ -18,6 +18,9 @@ #if DAWN_PLATFORM_WINDOWS # include "common/windows_with_undefs.h" +# if DAWN_PLATFORM_WINUWP +# include "common/WindowsUtils.h" +# endif #elif DAWN_PLATFORM_POSIX # include #else @@ -43,8 +46,11 @@ bool DynamicLib::Valid() const { bool DynamicLib::Open(const std::string& filename, std::string* error) { #if DAWN_PLATFORM_WINDOWS +# if DAWN_PLATFORM_WINUWP + mHandle = LoadPackagedLibrary(UTF8ToWStr(filename.c_str()).c_str(), 0); +# else mHandle = LoadLibraryA(filename.c_str()); - +# endif if (mHandle == nullptr && error != nullptr) { *error = "Windows Error: " + std::to_string(GetLastError()); } diff --git a/src/common/PlacementAllocated.h b/src/common/PlacementAllocated.h index 6bb329c3d5..6c715ca66a 100644 --- a/src/common/PlacementAllocated.h +++ b/src/common/PlacementAllocated.h @@ -32,6 +32,11 @@ class PlacementAllocated { void operator delete(void* ptr) { // Object is placement-allocated. Don't free the memory. } + + void operator delete(void*, void*) { + // This is added to match new(size_t size, void* ptr) + // Otherwise it triggers C4291 warning in MSVC + } }; #endif // COMMON_PLACEMENTALLOCATED_H_ diff --git a/src/common/Platform.h b/src/common/Platform.h index af7b175151..f9471021fd 100644 --- a/src/common/Platform.h +++ b/src/common/Platform.h @@ -16,7 +16,15 @@ #define COMMON_PLATFORM_H_ #if defined(_WIN32) || defined(_WIN64) +# include # define DAWN_PLATFORM_WINDOWS 1 +# if WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP +# define DAWN_PLATFORM_WIN32 1 +# elif WINAPI_FAMILY == WINAPI_FAMILY_PC_APP +# define DAWN_PLATFORM_WINUWP 1 +# else +# error "Unsupported Windows platform." +# endif #elif defined(__linux__) # define DAWN_PLATFORM_LINUX 1 diff --git a/src/dawn_native/BUILD.gn b/src/dawn_native/BUILD.gn index 2a5a59b436..515ca9d3e3 100644 --- a/src/dawn_native/BUILD.gn +++ b/src/dawn_native/BUILD.gn @@ -311,6 +311,20 @@ source_set("dawn_native_sources") { if (is_win) { libs += [ "user32.lib" ] + if (dawn_is_winuwp) { + # UWP has a limited DLL search space. + # So we have to link with stub libs provided by Windows SDK + # to load DLLs correctly. + libs += [ + "dxgi.lib", + "d3d12.lib", + "d3d11.lib", + + # TODO(dawn:766): + # Somehow use dxcompiler.lib makes CoreApp unable to activate + # WinPIX should be added as third party tools and linked statically + ] + } } if (dawn_enable_d3d12) { diff --git a/src/dawn_native/Surface.cpp b/src/dawn_native/Surface.cpp index 9b317bcdb8..9122d7c3a3 100644 --- a/src/dawn_native/Surface.cpp +++ b/src/dawn_native/Surface.cpp @@ -56,7 +56,7 @@ namespace dawn_native { } #endif // defined(DAWN_ENABLE_BACKEND_METAL) -#if defined(DAWN_PLATFORM_WINDOWS) +#if defined(DAWN_PLATFORM_WIN32) const SurfaceDescriptorFromWindowsHWND* hwndDesc = nullptr; FindInChain(descriptor->nextInChain, &hwndDesc); if (!hwndDesc) { @@ -66,7 +66,7 @@ namespace dawn_native { if (IsWindow(static_cast(hwndDesc->hwnd)) == 0) { return DAWN_VALIDATION_ERROR("Invalid HWND"); } -#endif // defined(DAWN_PLATFORM_WINDOWS) +#endif // defined(DAWN_PLATFORM_WIN32) #if defined(DAWN_USE_X11) const SurfaceDescriptorFromXlib* xDesc = nullptr; diff --git a/src/dawn_native/d3d12/PlatformFunctions.cpp b/src/dawn_native/d3d12/PlatformFunctions.cpp index 112250c990..ba1868e22e 100644 --- a/src/dawn_native/d3d12/PlatformFunctions.cpp +++ b/src/dawn_native/d3d12/PlatformFunctions.cpp @@ -114,6 +114,15 @@ namespace dawn_native { namespace d3d12 { } MaybeError PlatformFunctions::LoadD3D12() { +#if DAWN_PLATFORM_WINUWP + d3d12CreateDevice = &D3D12CreateDevice; + d3d12GetDebugInterface = &D3D12GetDebugInterface; + d3d12SerializeRootSignature = &D3D12SerializeRootSignature; + d3d12CreateRootSignatureDeserializer = &D3D12CreateRootSignatureDeserializer; + d3d12SerializeVersionedRootSignature = &D3D12SerializeVersionedRootSignature; + d3d12CreateVersionedRootSignatureDeserializer = + &D3D12CreateVersionedRootSignatureDeserializer; +#else std::string error; if (!mD3D12Lib.Open("d3d12.dll", &error) || !mD3D12Lib.GetProc(&d3d12CreateDevice, "D3D12CreateDevice", &error) || @@ -128,32 +137,48 @@ namespace dawn_native { namespace d3d12 { "D3D12CreateVersionedRootSignatureDeserializer", &error)) { return DAWN_INTERNAL_ERROR(error.c_str()); } +#endif return {}; } MaybeError PlatformFunctions::LoadD3D11() { +#if DAWN_PLATFORM_WINUWP + d3d11on12CreateDevice = &D3D11On12CreateDevice; +#else std::string error; if (!mD3D11Lib.Open("d3d11.dll", &error) || !mD3D11Lib.GetProc(&d3d11on12CreateDevice, "D3D11On12CreateDevice", &error)) { return DAWN_INTERNAL_ERROR(error.c_str()); } +#endif return {}; } MaybeError PlatformFunctions::LoadDXGI() { +#if DAWN_PLATFORM_WINUWP + dxgiGetDebugInterface1 = &DXGIGetDebugInterface1; + createDxgiFactory2 = &CreateDXGIFactory2; +#else std::string error; if (!mDXGILib.Open("dxgi.dll", &error) || !mDXGILib.GetProc(&dxgiGetDebugInterface1, "DXGIGetDebugInterface1", &error) || !mDXGILib.GetProc(&createDxgiFactory2, "CreateDXGIFactory2", &error)) { return DAWN_INTERNAL_ERROR(error.c_str()); } +#endif return {}; } void PlatformFunctions::LoadDXCLibraries() { + // TODO(dawn:766) + // Statically linked with dxcompiler.lib in UWP + // currently linked with dxcompiler.lib making CoreApp unable to activate + // LoadDXIL and LoadDXCompiler will fail in UWP, but LoadFunctions() can still be + // successfully executed. + const std::string& windowsSDKBasePath = GetWindowsSDKBasePath(); LoadDXIL(windowsSDKBasePath); @@ -199,12 +224,15 @@ namespace dawn_native { namespace d3d12 { } MaybeError PlatformFunctions::LoadFXCompiler() { +#if DAWN_PLATFORM_WINUWP + d3dCompile = &D3DCompile; +#else std::string error; if (!mFXCompilerLib.Open("d3dcompiler_47.dll", &error) || !mFXCompilerLib.GetProc(&d3dCompile, "D3DCompile", &error)) { return DAWN_INTERNAL_ERROR(error.c_str()); } - +#endif return {}; } @@ -217,6 +245,11 @@ namespace dawn_native { namespace d3d12 { } void PlatformFunctions::LoadPIXRuntime() { + // TODO(dawn:766): + // In UWP PIX should be statically linked WinPixEventRuntime_UAP.lib + // So maybe we should put WinPixEventRuntime as a third party package + // Currently PIX is not going to be loaded in UWP since the following + // mPIXEventRuntimeLib.Open will fail. if (!mPIXEventRuntimeLib.Open("WinPixEventRuntime.dll") || !mPIXEventRuntimeLib.GetProc(&pixBeginEventOnCommandList, "PIXBeginEventOnCommandList") || diff --git a/src/utils/BUILD.gn b/src/utils/BUILD.gn index 64431bfd37..1bd40735b9 100644 --- a/src/utils/BUILD.gn +++ b/src/utils/BUILD.gn @@ -95,7 +95,7 @@ static_library("dawn_utils") { libs = [] frameworks = [] - if (is_win) { + if (is_win && !dawn_is_winuwp) { sources += [ "WindowsDebugLogger.cpp" ] } else { sources += [ "EmptyDebugLogger.cpp" ]