Add UWP support

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: 陈俊嘉 <cjj19970505@live.cn>
Reviewed-by: Rafael Cintron <rafael.cintron@microsoft.com>
diff --git a/scripts/dawn_features.gni b/scripts/dawn_features.gni
index 924f240..117c661 100644
--- a/scripts/dawn_features.gni
+++ b/scripts/dawn_features.gni
@@ -26,6 +26,9 @@
   }
 }
 
+# Enable the compilation for UWP
+dawn_is_winuwp = is_win && target_os == "winuwp"
+
 declare_args() {
   dawn_use_angle = true
 
@@ -62,8 +65,9 @@
   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 @@
   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 176e82e..c7dc030 100644
--- a/src/common/BUILD.gn
+++ b/src/common/BUILD.gn
@@ -129,6 +129,16 @@
     # 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 6de7ced..119ec42 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 <dlfcn.h>
 #else
@@ -43,8 +46,11 @@
 
 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 6bb329c..6c715ca 100644
--- a/src/common/PlacementAllocated.h
+++ b/src/common/PlacementAllocated.h
@@ -32,6 +32,11 @@
     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 af7b175..f947102 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 <winapifamily.h>
 #    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 2a5a59b..515ca9d 100644
--- a/src/dawn_native/BUILD.gn
+++ b/src/dawn_native/BUILD.gn
@@ -311,6 +311,20 @@
 
   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 9b317bc..9122d7c 100644
--- a/src/dawn_native/Surface.cpp
+++ b/src/dawn_native/Surface.cpp
@@ -56,7 +56,7 @@
         }
 #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 @@
         if (IsWindow(static_cast<HWND>(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 112250c..ba1868e 100644
--- a/src/dawn_native/d3d12/PlatformFunctions.cpp
+++ b/src/dawn_native/d3d12/PlatformFunctions.cpp
@@ -114,6 +114,15 @@
     }
 
     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 @@
                                "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 @@
     }
 
     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 @@
     }
 
     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 64431bf..1bd4073 100644
--- a/src/utils/BUILD.gn
+++ b/src/utils/BUILD.gn
@@ -95,7 +95,7 @@
   libs = []
   frameworks = []
 
-  if (is_win) {
+  if (is_win && !dawn_is_winuwp) {
     sources += [ "WindowsDebugLogger.cpp" ]
   } else {
     sources += [ "EmptyDebugLogger.cpp" ]