From 09c0c1e8743846dd1f886cfe68620fe3f7d4ae78 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 15 Apr 2023 19:40:50 +0200 Subject: [PATCH] Handle size changes when drawing frames --- .../Backend/Win32Application.cs | 40 +++++++++++++++++-- .../ComputeSharp.SwapChain.D2D1/Program.cs | 4 +- .../Backend/SwapChainApplication{T}.cs | 2 +- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/samples/ComputeSharp.SwapChain.D2D1/Backend/Win32Application.cs b/samples/ComputeSharp.SwapChain.D2D1/Backend/Win32Application.cs index 99459482..c100ad18 100644 --- a/samples/ComputeSharp.SwapChain.D2D1/Backend/Win32Application.cs +++ b/samples/ComputeSharp.SwapChain.D2D1/Backend/Win32Application.cs @@ -6,6 +6,7 @@ using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; using TerraFX.Interop.WinRT; using Windows.Graphics.DirectX; +using Windows.Graphics.Imaging; using WinRT; namespace ComputeSharp.SwapChain.D2D1.Backend; @@ -33,7 +34,17 @@ internal sealed class Win32Application /// /// Whether or not the window has been resized and requires the buffers to be updated. /// - private bool isResizePending; + 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. @@ -86,8 +97,8 @@ internal sealed class Win32Application dxgiSwapChainDesc1.BufferUsage = DXGI.DXGI_USAGE_RENDER_TARGET_OUTPUT; dxgiSwapChainDesc1.Flags = 0; dxgiSwapChainDesc1.Format = DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM; - dxgiSwapChainDesc1.Width = 1; - dxgiSwapChainDesc1.Height = 1; + 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; @@ -166,13 +177,24 @@ internal sealed class Win32Application 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 { TotalTime = time, DrawingSession = canvasDrawingSession }); + Draw?.Invoke(this, new DrawEventArgs + { + ScreenWidth = this.screenWidth, + ScreenHeight = this.screenHeight, + TotalTime = time, + DrawingSession = canvasDrawingSession + }); } // Wait for v-sync @@ -187,6 +209,16 @@ internal sealed class Win32Application /// 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. /// diff --git a/samples/ComputeSharp.SwapChain.D2D1/Program.cs b/samples/ComputeSharp.SwapChain.D2D1/Program.cs index 0b06db95..7da94709 100644 --- a/samples/ComputeSharp.SwapChain.D2D1/Program.cs +++ b/samples/ComputeSharp.SwapChain.D2D1/Program.cs @@ -59,8 +59,8 @@ class Program { // Set the effect properties effect.ElapsedTime = e.TotalTime; - effect.ScreenWidth = 1280; - effect.ScreenHeight = 720; + effect.ScreenWidth = (int)e.ScreenWidth; + effect.ScreenHeight = (int)e.ScreenHeight; // Draw the effect e.DrawingSession.DrawImage(effect); diff --git a/samples/ComputeSharp.SwapChain/Backend/SwapChainApplication{T}.cs b/samples/ComputeSharp.SwapChain/Backend/SwapChainApplication{T}.cs index 41713dbe..d4d15aec 100644 --- a/samples/ComputeSharp.SwapChain/Backend/SwapChainApplication{T}.cs +++ b/samples/ComputeSharp.SwapChain/Backend/SwapChainApplication{T}.cs @@ -71,7 +71,7 @@ internal sealed class SwapChainApplication : Win32Application /// /// Whether or not the window has been resized and requires the buffers to be updated. /// - private bool isResizePending; + private volatile bool isResizePending; /// /// Creates a new instance with the specified parameters.