diff --git a/samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/ABI.Microsoft.Graphics.Canvas/ICanvasFactoryNative.cs b/samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/ABI.Microsoft.Graphics.Canvas/ICanvasFactoryNative.cs deleted file mode 100644 index 49bfa283..00000000 --- a/samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/ABI.Microsoft.Graphics.Canvas/ICanvasFactoryNative.cs +++ /dev/null @@ -1,80 +0,0 @@ -using System; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using TerraFX.Interop.Windows; -using TerraFX.Interop; - -#pragma warning disable CS0649, IDE0055, IDE1006 - -namespace ABI.Microsoft.Graphics.Canvas; - -/// -/// Raw bindings for the interop interface for the activation factory. -/// -internal unsafe struct ICanvasFactoryNative : IUnknown.Interface -{ - public void** lpVtbl; - - /// - static Guid* INativeGuid.NativeGuid - { - get - { - ReadOnlySpan data = - [ - 0x0D, 0x44, 0x5C, 0x69, - 0xB3, 0x04, - 0xDD, 0x4E, - 0xBF, 0xD9, - 0x63, - 0xE5, - 0x1E, - 0x9F, - 0x72, - 0x02 - ]; - - return (Guid*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(data)); - } - } - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public HRESULT QueryInterface(Guid* riid, void** ppvObject) - { - return ((delegate* unmanaged[MemberFunction])this.lpVtbl[0])((ICanvasFactoryNative*)Unsafe.AsPointer(ref this), riid, ppvObject); - } - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public uint AddRef() - { - return ((delegate* unmanaged[MemberFunction])this.lpVtbl[1])((ICanvasFactoryNative*)Unsafe.AsPointer(ref this)); - } - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public uint Release() - { - return ((delegate* unmanaged[MemberFunction])this.lpVtbl[2])((ICanvasFactoryNative*)Unsafe.AsPointer(ref this)); - } - - /// - /// Gets or creates an IInspectable Win2D wrapper for a given native D2D resource. - /// - /// The input canvas device (as a marshalled ). - /// The input native resource to create a wrapper for. - /// The realization DPIs for - /// The resulting wrapper for . - /// The HRESULT for the operation. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public HRESULT GetOrCreate(IUnknown* device, IUnknown* resource, float dpi, void** wrapper) - { - return ((delegate* unmanaged[MemberFunction])this.lpVtbl[6])( - (ICanvasFactoryNative*)Unsafe.AsPointer(ref this), - device, - resource, - dpi, - wrapper); - } -} \ No newline at end of file diff --git a/samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/Win32Application.cs b/samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/Win32Application.cs index 4eef52a2..0a6a4be3 100644 --- a/samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/Win32Application.cs +++ b/samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/Win32Application.cs @@ -1,10 +1,7 @@ using System; using System.Diagnostics.CodeAnalysis; -using ABI.Microsoft.Graphics.Canvas; -using TerraFX.Interop.DirectX; +using Microsoft.UI; using TerraFX.Interop.Windows; -using TerraFX.Interop.WinRT; -using WinRT; namespace ComputeSharp.SwapChain.D2D1.Backend; @@ -13,7 +10,6 @@ using CanvasDevice = Microsoft.Graphics.Canvas.CanvasDevice; using CanvasDrawingSession = Microsoft.Graphics.Canvas.CanvasDrawingSession; using CanvasSwapChain = Microsoft.Graphics.Canvas.CanvasSwapChain; using DirectXPixelFormat = Windows.Graphics.DirectX.DirectXPixelFormat; -using Windows = TerraFX.Interop.Windows.Windows; /// /// A simple Win32 application handling a D2D swapchain. @@ -60,88 +56,12 @@ internal sealed class Win32Application { // Create a new canvas device, which will handle DX11/D2D initialization CanvasDevice canvasDevice = new(); - CanvasSwapChain canvasSwapChain; - - // Create the swap chain to display frames - using (ComPtr direct3DDeviceUnknown = default) - using (ComPtr dxgiFactory2 = default) - using (ComPtr dxgiSwapChain1 = default) - { - HRESULT hresult; - - // Extract the underlying Direct3D device from the canvas device. - // This is needed for CreateSwapChainForHwnd, as the owning device. - using (ComPtr canvasDeviceUnknown = default) - using (ComPtr direct3DDxgiInterfaceAccess = default) - { - canvasDeviceUnknown.Attach((IUnknown*)MarshalInspectable.FromManaged(canvasDevice)); - - hresult = canvasDeviceUnknown.CopyTo(direct3DDxgiInterfaceAccess.GetAddressOf()); - - ExceptionHelpers.ThrowExceptionForHR(hresult); - - hresult = direct3DDxgiInterfaceAccess.Get()->GetInterface(Windows.__uuidof(), (void**)direct3DDeviceUnknown.GetAddressOf()); - - ExceptionHelpers.ThrowExceptionForHR(hresult); - } - - // Create the DXGIFactory2 instance to create the swapchain with - hresult = DirectX.CreateDXGIFactory2(DXGI.DXGI_CREATE_FACTORY_DEBUG, Windows.__uuidof(), (void**)dxgiFactory2.GetAddressOf()); - - ExceptionHelpers.ThrowExceptionForHR(hresult); - - DXGI_SWAP_CHAIN_DESC1 dxgiSwapChainDesc1 = default; - dxgiSwapChainDesc1.AlphaMode = DXGI_ALPHA_MODE.DXGI_ALPHA_MODE_IGNORE; - dxgiSwapChainDesc1.BufferCount = 2; - dxgiSwapChainDesc1.BufferUsage = DXGI.DXGI_USAGE_RENDER_TARGET_OUTPUT; - dxgiSwapChainDesc1.Flags = 0; - dxgiSwapChainDesc1.Format = DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM; - dxgiSwapChainDesc1.Width = this.screenWidth = 1; - dxgiSwapChainDesc1.Height = this.screenHeight = 1; - dxgiSwapChainDesc1.SampleDesc = new DXGI_SAMPLE_DESC(count: 1, quality: 0); - dxgiSwapChainDesc1.Scaling = DXGI_SCALING.DXGI_SCALING_STRETCH; - dxgiSwapChainDesc1.Stereo = 0; - dxgiSwapChainDesc1.SwapEffect = DXGI_SWAP_EFFECT.DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; - - // Create the native DXGI swapchain object to wrap - hresult = dxgiFactory2.Get()->CreateSwapChainForHwnd( - direct3DDeviceUnknown.Get(), - hwnd, - &dxgiSwapChainDesc1, - null, - null, - dxgiSwapChain1.GetAddressOf()); - - ExceptionHelpers.ThrowExceptionForHR(hresult); - - using ComPtr canvasFactoryNative = default; - - // Get the activation factory for CanvasDevice - using (IObjectReference canvasDeviceActivationFactory = ActivationFactory.Get( - typeName: "Microsoft.Graphics.Canvas.CanvasDevice", - iid: Windows.__uuidof())) - { - canvasFactoryNative.Attach((ICanvasFactoryNative*)canvasDeviceActivationFactory.GetRef()); - } - - // Create a Win2D wrapper for the swapchain - using (ComPtr canvasDeviceUnknown = default) - using (ComPtr canvasSwapChainUnknown = default) - { - canvasDeviceUnknown.Attach((IUnknown*)MarshalInspectable.FromManaged(canvasDevice)); - - hresult = canvasFactoryNative.Get()->GetOrCreate( - device: canvasDeviceUnknown.Get(), - resource: (IUnknown*)dxgiSwapChain1.Get(), - dpi: 96.0f, - wrapper: (void**)canvasSwapChainUnknown.GetAddressOf()); - - ExceptionHelpers.ThrowExceptionForHR(hresult); - - // Marshal to a WinRT managed object - canvasSwapChain = MarshalInspectable.FromAbi((IntPtr)canvasSwapChainUnknown.Get()); - } - } + CanvasSwapChain canvasSwapChain = CanvasSwapChain.CreateForWindowId( + resourceCreator: canvasDevice, + windowId: Win32Interop.GetWindowIdFromWindow(hwnd), + width: this.screenWidth = 1, + height: this.screenHeight = 1, + dpi: 96.0f); // Save the Win2D objects for later use this.canvasDevice = canvasDevice; diff --git a/samples/ComputeSharp.SwapChain.D2D1.Cli/ComputeSharp.SwapChain.D2D1.Cli.csproj b/samples/ComputeSharp.SwapChain.D2D1.Cli/ComputeSharp.SwapChain.D2D1.Cli.csproj index d91eeb26..ebb4d442 100644 --- a/samples/ComputeSharp.SwapChain.D2D1.Cli/ComputeSharp.SwapChain.D2D1.Cli.csproj +++ b/samples/ComputeSharp.SwapChain.D2D1.Cli/ComputeSharp.SwapChain.D2D1.Cli.csproj @@ -9,6 +9,13 @@ win-x64;win-arm64 computesharp.d2d1.cli ..\..\assets\icon.ico + + + true + None