using System; using System.Diagnostics.CodeAnalysis; using ABI.Microsoft.Graphics.Canvas; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; using TerraFX.Interop.WinRT; using WinRT; namespace ComputeSharp.SwapChain.D2D1.Backend; using BitmapSize = Windows.Graphics.Imaging.BitmapSize; 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. /// internal sealed class Win32Application { /// /// The instance to use. /// private CanvasDevice? canvasDevice; /// /// The instance to use to render frames. /// private CanvasSwapChain? canvasSwapChain; /// /// Whether or not the window has been resized and requires the buffers to be updated. /// private volatile bool isResizePending; /// /// The current screen width in raw pixels. /// private uint screenWidth; /// /// The current screen height in raw pixels. /// private uint screenHeight; /// /// Raised whenever a draw operation can be performed. /// public event EventHandler? Draw; /// /// Initializes the current application. /// /// The handle for the window. [MemberNotNull(nameof(canvasDevice))] [MemberNotNull(nameof(canvasSwapChain))] public unsafe void OnInitialize(HWND hwnd) { // 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()); } } // Save the Win2D objects for later use this.canvasDevice = canvasDevice; this.canvasSwapChain = canvasSwapChain; } /// /// Resizes the current application. /// public void OnResize() { this.isResizePending = true; } /// /// Updates the current application. /// /// The current time since the start of the application. public void OnUpdate(TimeSpan time) { if (this.isResizePending) { // Resize the swapchain if needed (the size is calculated automatically) this.canvasSwapChain!.ResizeBuffers( newWidth: 0, newHeight: 0, newDpi: 96.0f, newFormat: DirectXPixelFormat.Unknown, bufferCount: 0); BitmapSize bitmapSize = this.canvasSwapChain!.SizeInPixels; this.screenWidth = bitmapSize.Width; this.screenHeight = bitmapSize.Height; this.isResizePending = false; } // Create the drawing session and invoke all registered draw handler using (CanvasDrawingSession canvasDrawingSession = this.canvasSwapChain!.CreateDrawingSession(default)) { Draw?.Invoke(this, new DrawEventArgs { ScreenWidth = this.screenWidth, ScreenHeight = this.screenHeight, TotalTime = time, DrawingSession = canvasDrawingSession }); } // Wait for v-sync this.canvasSwapChain.WaitForVerticalBlank(); // Present the new frame this.canvasSwapChain.Present(syncInterval: 1); } /// /// Arguments for . /// public sealed class DrawEventArgs : EventArgs { /// /// Gets the screen width in raw pixels. /// public required uint ScreenWidth { get; init; } /// /// Gets the screen height in raw pixels. /// public required uint ScreenHeight { get; init; } /// /// Gets the total time for the rendering loop. /// public required TimeSpan TotalTime { get; init; } /// /// Gets the instance to use. /// public required CanvasDrawingSession DrawingSession { get; init; } } }