From 74326fe2c885291692588d4b635ef60fc7671bf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E4=BF=8A=E5=98=89?= Date: Tue, 27 Apr 2021 16:43:27 +0000 Subject: [PATCH] Add support for UWP CoreWindow in SwapChain and Surface MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add SurfaceDescriptorFromWindowsCoreWindow structure in codegen. Add WindowsCoreWindow surface type. Add support for WindowsCoreWindow surface in SwapChain. Bug: dawn:766 Change-Id: If89258dc68896b9ba22c677d37ca3ba68c6fceb7 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/48762 Reviewed-by: Corentin Wallez Commit-Queue: 陈俊嘉 --- dawn.json | 11 ++++- dawn_wire.json | 3 +- src/dawn_native/Surface.cpp | 59 +++++++++++++++++++----- src/dawn_native/Surface.h | 16 ++++++- src/dawn_native/d3d12/SwapChainD3D12.cpp | 28 +++++++++-- 5 files changed, 97 insertions(+), 20 deletions(-) diff --git a/dawn.json b/dawn.json index 9f6b4a61b7..be0062c994 100644 --- a/dawn.json +++ b/dawn.json @@ -1897,6 +1897,14 @@ {"name": "window", "type": "uint32_t"} ] }, + "surface descriptor from windows core window": { + "category": "structure", + "chained": true, + "javascript": false, + "members": [ + {"name": "core window", "type": "void", "annotation": "*"} + ] + }, "swap chain": { "category": "object", "methods": [ @@ -1937,7 +1945,8 @@ {"value": 4, "name": "surface descriptor from canvas HTML selector"}, {"value": 5, "name": "shader module SPIRV descriptor"}, {"value": 6, "name": "shader module WGSL descriptor"}, - {"value": 7, "name": "primitive depth clamping state"} + {"value": 7, "name": "primitive depth clamping state"}, + {"value": 8, "name": "surface descriptor from windows core window"} ] }, "texture": { diff --git a/dawn_wire.json b/dawn_wire.json index a6e39ed9ea..dcaac56b11 100644 --- a/dawn_wire.json +++ b/dawn_wire.json @@ -149,7 +149,8 @@ "client_side_structures": [ "SurfaceDescriptorFromMetalLayer", "SurfaceDescriptorFromWindowsHWND", - "SurfaceDescriptorFromXlib" + "SurfaceDescriptorFromXlib", + "SurfaceDescriptorFromWindowsCoreWindow" ], "client_side_commands": [ "BufferMapAsync", diff --git a/src/dawn_native/Surface.cpp b/src/dawn_native/Surface.cpp index 9122d7c3a3..3a68832c28 100644 --- a/src/dawn_native/Surface.cpp +++ b/src/dawn_native/Surface.cpp @@ -20,8 +20,8 @@ #include "dawn_native/SwapChain.h" #if defined(DAWN_PLATFORM_WINDOWS) -# include "common/windows_with_undefs.h" -#endif // DAWN_PLATFORM_WINDOWS +# include +#endif // defined(DAWN_PLATFORM_WINDOWS) #if defined(DAWN_USE_X11) # include "common/xlib_with_undefs.h" @@ -40,9 +40,10 @@ namespace dawn_native { } DAWN_TRY(ValidateSingleSType(descriptor->nextInChain, - wgpu::SType::SurfaceDescriptorFromMetalLayer, - wgpu::SType::SurfaceDescriptorFromWindowsHWND, - wgpu::SType::SurfaceDescriptorFromXlib)); + wgpu::SType::SurfaceDescriptorFromMetalLayer, + wgpu::SType::SurfaceDescriptorFromWindowsHWND, + wgpu::SType::SurfaceDescriptorFromWindowsCoreWindow, + wgpu::SType::SurfaceDescriptorFromXlib)); #if defined(DAWN_ENABLE_BACKEND_METAL) const SurfaceDescriptorFromMetalLayer* metalDesc = nullptr; @@ -56,17 +57,31 @@ namespace dawn_native { } #endif // defined(DAWN_ENABLE_BACKEND_METAL) -#if defined(DAWN_PLATFORM_WIN32) +#if defined(DAWN_PLATFORM_WINDOWS) +# if defined(DAWN_PLATFORM_WIN32) const SurfaceDescriptorFromWindowsHWND* hwndDesc = nullptr; FindInChain(descriptor->nextInChain, &hwndDesc); - if (!hwndDesc) { - return DAWN_VALIDATION_ERROR("Unsupported sType"); + if (hwndDesc) { + if (IsWindow(static_cast(hwndDesc->hwnd)) == 0) { + return DAWN_VALIDATION_ERROR("Invalid HWND"); + } + return {}; } - // Validate the hwnd using the windows.h IsWindow function. - if (IsWindow(static_cast(hwndDesc->hwnd)) == 0) { - return DAWN_VALIDATION_ERROR("Invalid HWND"); +# endif // defined(DAWN_PLATFORM_WIN32) + const SurfaceDescriptorFromWindowsCoreWindow* coreWindowDesc = nullptr; + FindInChain(descriptor->nextInChain, &coreWindowDesc); + if (coreWindowDesc) { + // Validate the coreWindow by query for ICoreWindow interface + ComPtr coreWindow; + if (coreWindowDesc->coreWindow == nullptr || + FAILED(static_cast(coreWindowDesc->coreWindow) + ->QueryInterface(IID_PPV_ARGS(&coreWindow)))) { + return DAWN_VALIDATION_ERROR("Invalid CoreWindow"); + } + return {}; } -#endif // defined(DAWN_PLATFORM_WIN32) + return DAWN_VALIDATION_ERROR("Unsupported sType"); +#endif // defined(DAWN_PLATFORM_WINDOWS) #if defined(DAWN_USE_X11) const SurfaceDescriptorFromXlib* xDesc = nullptr; @@ -98,22 +113,33 @@ namespace dawn_native { ASSERT(descriptor->nextInChain != nullptr); const SurfaceDescriptorFromMetalLayer* metalDesc = nullptr; const SurfaceDescriptorFromWindowsHWND* hwndDesc = nullptr; + const SurfaceDescriptorFromWindowsCoreWindow* coreWindowDesc = nullptr; const SurfaceDescriptorFromXlib* xDesc = nullptr; FindInChain(descriptor->nextInChain, &metalDesc); FindInChain(descriptor->nextInChain, &hwndDesc); + FindInChain(descriptor->nextInChain, &coreWindowDesc); FindInChain(descriptor->nextInChain, &xDesc); ASSERT(metalDesc || hwndDesc || xDesc); if (metalDesc) { mType = Type::MetalLayer; mMetalLayer = metalDesc->layer; } else if (hwndDesc) { +#if defined(DAWN_PLATFORM_WIN32) mType = Type::WindowsHWND; mHInstance = hwndDesc->hinstance; mHWND = hwndDesc->hwnd; +#endif // defined(DAWN_PLATFORM_WIN32) + } else if (coreWindowDesc) { +#if defined(DAWN_PLATFORM_WINDOWS) + mType = Type::WindowsCoreWindow; + mCoreWindow = static_cast(coreWindowDesc->coreWindow); +#endif // defined(DAWN_PLATFORM_WINDOWS) } else if (xDesc) { mType = Type::Xlib; mXDisplay = xDesc->display; mXWindow = xDesc->window; + } else { + UNREACHABLE(); } } @@ -154,6 +180,15 @@ namespace dawn_native { return mHWND; } + void* Surface::GetCoreWindow() const { + ASSERT(mType == Type::WindowsCoreWindow); +#if defined(DAWN_PLATFORM_WINDOWS) + return mCoreWindow.Get(); +#else + return nullptr; +#endif + } + void* Surface::GetXDisplay() const { ASSERT(mType == Type::Xlib); return mXDisplay; diff --git a/src/dawn_native/Surface.h b/src/dawn_native/Surface.h index 5863109024..2871bbb1ab 100644 --- a/src/dawn_native/Surface.h +++ b/src/dawn_native/Surface.h @@ -21,6 +21,12 @@ #include "dawn_native/dawn_platform.h" +#include "common/Platform.h" + +#if defined(DAWN_PLATFORM_WINDOWS) +# include "dawn_native/d3d12/d3d12_platform.h" +#endif // defined(DAWN_PLATFORM_WINDOWS) + namespace dawn_native { MaybeError ValidateSurfaceDescriptor(const InstanceBase* instance, @@ -39,7 +45,7 @@ namespace dawn_native { NewSwapChainBase* GetAttachedSwapChain(); // These are valid to call on all Surfaces. - enum class Type { MetalLayer, WindowsHWND, Xlib }; + enum class Type { MetalLayer, WindowsHWND, WindowsCoreWindow, Xlib }; Type GetType() const; InstanceBase* GetInstance(); @@ -50,6 +56,9 @@ namespace dawn_native { void* GetHInstance() const; void* GetHWND() const; + // Valid to call if the type is WindowsCoreWindow + void* GetCoreWindow() const; + // Valid to call if the type is WindowsXlib void* GetXDisplay() const; uint32_t GetXWindow() const; @@ -70,6 +79,11 @@ namespace dawn_native { void* mHInstance = nullptr; void* mHWND = nullptr; +#if defined(DAWN_PLATFORM_WINDOWS) + // WindowsCoreWindow + ComPtr mCoreWindow; +#endif // defined(DAWN_PLATFORM_WINDOWS) + // Xlib void* mXDisplay = nullptr; uint32_t mXWindow = 0; diff --git a/src/dawn_native/d3d12/SwapChainD3D12.cpp b/src/dawn_native/d3d12/SwapChainD3D12.cpp index de9dc30d8d..166551338c 100644 --- a/src/dawn_native/d3d12/SwapChainD3D12.cpp +++ b/src/dawn_native/d3d12/SwapChainD3D12.cpp @@ -244,11 +244,29 @@ namespace dawn_native { namespace d3d12 { "Getting IDXGIFactory2")); ComPtr swapChain1; - DAWN_TRY(CheckHRESULT( - factory2->CreateSwapChainForHwnd(device->GetCommandQueue().Get(), - static_cast(GetSurface()->GetHWND()), - &swapChainDesc, nullptr, nullptr, &swapChain1), - "Creating the IDXGISwapChain1")); + switch (GetSurface()->GetType()) { +#if defined(DAWN_PLATFORM_WIN32) + case Surface::Type::WindowsHWND: { + DAWN_TRY(CheckHRESULT( + factory2->CreateSwapChainForHwnd(device->GetCommandQueue().Get(), + static_cast(GetSurface()->GetHWND()), + &swapChainDesc, nullptr, nullptr, &swapChain1), + "Creating the IDXGISwapChain1")); + break; + } +#endif // defined(DAWN_PLATFORM_WIN32) + case Surface::Type::WindowsCoreWindow: { + DAWN_TRY(CheckHRESULT(factory2->CreateSwapChainForCoreWindow( + device->GetCommandQueue().Get(), + static_cast(GetSurface()->GetCoreWindow()), + &swapChainDesc, nullptr, &swapChain1), + "Creating the IDXGISwapChain1")); + break; + } + default: + UNREACHABLE(); + } + DAWN_TRY(CheckHRESULT(swapChain1.As(&mDXGISwapChain), "Gettting IDXGISwapChain1")); return CollectSwapChainBuffers();