diff --git a/samples/ComputeSharp.SwapChain/Backend/SwapChainApplication{T}.cs b/samples/ComputeSharp.SwapChain/Backend/SwapChainApplication{T}.cs index 3340a9dd..2fd92fa0 100644 --- a/samples/ComputeSharp.SwapChain/Backend/SwapChainApplication{T}.cs +++ b/samples/ComputeSharp.SwapChain/Backend/SwapChainApplication{T}.cs @@ -1,5 +1,4 @@ using System; -using System.Drawing; using ComputeSharp.Interop; using TerraFX.Interop; using FX = TerraFX.Interop.Windows; @@ -69,6 +68,11 @@ namespace ComputeSharp.SwapChain.Backend /// private ReadWriteTexture2D? texture; + /// + /// Whether or not the window has been resized and requires the buffers to be updated. + /// + private bool isResizePending; + /// /// Creates a new instance with the specified parameters. /// @@ -79,10 +83,7 @@ namespace ComputeSharp.SwapChain.Backend } /// - public override string Title => default(T).ToString()!; - - /// - public override unsafe void OnInitialize(Size size, HWND hwnd) + public override unsafe void OnInitialize(HWND hwnd) { // Get the underlying ID3D12Device in use fixed (ID3D12Device** d3D12Device = this.d3D12Device) @@ -169,7 +170,15 @@ namespace ComputeSharp.SwapChain.Backend } /// - public override unsafe void OnResize(Size size) + public override unsafe void OnResize() + { + this.isResizePending = true; + } + + /// + /// Applies the actual resize logic that was scheduled from . + /// + private unsafe void ApplyResize() { this.d3D12CommandQueue.Get()->Signal(this.d3D12Fence.Get(), this.nextD3D12FenceValue); @@ -214,6 +223,13 @@ namespace ComputeSharp.SwapChain.Backend /// public override unsafe void OnUpdate(TimeSpan time) { + if (this.isResizePending) + { + ApplyResize(); + + this.isResizePending = false; + } + // Generate the new frame Gpu.Default.For(this.texture!.Width, this.texture.Height, this.shaderFactory(this.texture, time)); diff --git a/samples/ComputeSharp.SwapChain/Backend/Win32Application.cs b/samples/ComputeSharp.SwapChain/Backend/Win32Application.cs index 733a64ed..af15cf6b 100644 --- a/samples/ComputeSharp.SwapChain/Backend/Win32Application.cs +++ b/samples/ComputeSharp.SwapChain/Backend/Win32Application.cs @@ -1,5 +1,4 @@ using System; -using System.Drawing; using TerraFX.Interop; namespace ComputeSharp.SwapChain.Backend @@ -9,23 +8,16 @@ namespace ComputeSharp.SwapChain.Backend /// internal abstract class Win32Application { - /// - /// Gets the title of the current application. - /// - public abstract string Title { get; } - /// /// Initializes the current application. /// - /// The window size. /// The handle for the window. - public abstract void OnInitialize(Size size, HWND hwnd); + public abstract void OnInitialize(HWND hwnd); /// /// Resizes the current application. /// - /// The new window size. - public abstract void OnResize(Size size); + public abstract void OnResize(); /// /// Updates the current application. diff --git a/samples/ComputeSharp.SwapChain/Backend/Win32ApplicationRunner.cs b/samples/ComputeSharp.SwapChain/Backend/Win32ApplicationRunner.cs index e7ca1c7a..60f8de39 100644 --- a/samples/ComputeSharp.SwapChain/Backend/Win32ApplicationRunner.cs +++ b/samples/ComputeSharp.SwapChain/Backend/Win32ApplicationRunner.cs @@ -29,11 +29,6 @@ namespace ComputeSharp.SwapChain.Backend /// private static HWND hwnd; - /// - /// The current size of the application window. - /// - private static Size windowSize; - /// /// A instance used to track the elapsed time for the application. /// @@ -45,14 +40,9 @@ namespace ComputeSharp.SwapChain.Backend private static Win32Application application = null!; /// - /// The number of elapsed milliseconds since the window title was last updated. + /// The thread running the render loop. /// - private static long elapsedMillisecondsForLastTitleUpdate; - - /// - /// The number of elapsed frames since the window title was last updated. - /// - private static int elapsedFramesSinceLastTitleUpdate; + private static Thread renderThread = null!; /// /// Runs a specified application and starts the main loop to update its state. @@ -68,11 +58,9 @@ namespace ComputeSharp.SwapChain.Backend Win32ApplicationRunner.application = application; IntPtr hInstance = FX.GetModuleHandleW(null); - uint width = 1280; - uint height = 720; fixed (char* name = Assembly.GetExecutingAssembly().FullName) - fixed (char* windowTitle = application.Title) + fixed (char* windowTitle = application.GetType().ToString()) { // Initialize the window class WNDCLASSEXW windowClassEx = new() @@ -88,13 +76,14 @@ namespace ComputeSharp.SwapChain.Backend // Register the window class _ = FX.RegisterClassExW(&windowClassEx); - Rectangle windowRect = new(0, 0, (int)width, (int)height); + Rectangle windowRect = new(0, 0, 1280, 720); // Set the target window size _ = FX.AdjustWindowRect((RECT*)&windowRect, FX.WS_OVERLAPPEDWINDOW, FX.FALSE); - height = (uint)(windowRect.Bottom - windowRect.Top); - width = (uint)(windowRect.Right - windowRect.Left); + uint + height = (uint)(windowRect.Bottom - windowRect.Top), + width = (uint)(windowRect.Right - windowRect.Left); // Create the window and store a handle to it hwnd = FX.CreateWindowExW( @@ -121,10 +110,8 @@ namespace ComputeSharp.SwapChain.Backend _ = FX.DwmExtendFrameIntoClientArea(hwnd, &margins); } - windowSize = new Size((int)height, (int)width); - // Initialize the application - application.OnInitialize(windowSize, hwnd); + application.OnInitialize(hwnd); // Display the window _ = FX.ShowWindow(hwnd, FX.SW_SHOWDEFAULT); @@ -134,6 +121,21 @@ namespace ComputeSharp.SwapChain.Backend MSG msg = default; + // Setup the render thread that enables smooth resizing of the window + renderThread = new Thread(static args => + { + (Win32Application application, Stopwatch stopwatch, CancellationToken token) = ((Win32Application, Stopwatch, CancellationToken))args!; + + while (!token.IsCancellationRequested) + { + application.OnUpdate(stopwatch.Elapsed); + } + }); + + CancellationTokenSource tokenSource = new(); + + renderThread.Start((application, stopwatch, tokenSource.Token)); + // Process any messages in the queue while (msg.message != FX.WM_QUIT) { @@ -142,49 +144,20 @@ namespace ComputeSharp.SwapChain.Backend _ = FX.TranslateMessage(&msg); _ = FX.DispatchMessageW(&msg); } - else if (!isPaused || true) - { - RunApp(); - } - else + else if (isPaused) { Thread.Sleep(100); } } + tokenSource.Cancel(); + + renderThread.Join(); + // Return this part of the WM_QUIT message to Windows return (int)msg.wParam; } - /// - /// The application state update method that is invoked within the main application loop. - /// - private static void RunApp() - { - // Update window title approx every second - if (stopwatch.ElapsedMilliseconds - elapsedMillisecondsForLastTitleUpdate >= 1000) - { - string title = isPaused switch - { - true => $"{application.Title} - Paused", - false => $"{application.Title} - FPS: {elapsedFramesSinceLastTitleUpdate}" - }; - - fixed (char* pTitle = title) - { - _ = FX.SetWindowTextW(hwnd, (ushort*)pTitle); - } - - elapsedMillisecondsForLastTitleUpdate = stopwatch.ElapsedMilliseconds; - elapsedFramesSinceLastTitleUpdate = 0; - } - - // Update the application state - application.OnUpdate(stopwatch.Elapsed); - - elapsedFramesSinceLastTitleUpdate++; - } - /// /// Processes incoming messages for a window. /// @@ -245,7 +218,7 @@ namespace ComputeSharp.SwapChain.Backend case FX.WM_EXITSIZEMOVE: { isResizing = false; - application.OnResize(windowSize); + application.OnResize(); return 0; } @@ -253,13 +226,9 @@ namespace ComputeSharp.SwapChain.Backend // Size update case FX.WM_SIZE: { - uint size = (uint)lParam; - - windowSize = new Size(FX.LOWORD(size), FX.HIWORD(size)); - if (!isResizing && wParam != FX.SIZE_MINIMIZED) { - application.OnResize(windowSize); + application.OnResize(); } return 0;