From 044ddb86487ea3a432def270e05274a53263dcf2 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 6 Jan 2022 01:24:49 +0100 Subject: [PATCH] Add ability for IShaderRunner to skip presenting frames --- .../Shaders/Runners/ContouredLayersRunner.cs | 4 +- .../Helpers/SwapChainManager.Rendering.cs | 56 +++++++++++-------- src/ComputeSharp.UI/IShaderRunner.cs | 5 +- src/ComputeSharp.UI/ShaderRunner{T}.cs | 7 ++- 4 files changed, 45 insertions(+), 27 deletions(-) diff --git a/samples/ComputeSharp.SwapChain.Core.Shared/Shaders/Runners/ContouredLayersRunner.cs b/samples/ComputeSharp.SwapChain.Core.Shared/Shaders/Runners/ContouredLayersRunner.cs index 2aa1a353..87a93399 100644 --- a/samples/ComputeSharp.SwapChain.Core.Shared/Shaders/Runners/ContouredLayersRunner.cs +++ b/samples/ComputeSharp.SwapChain.Core.Shared/Shaders/Runners/ContouredLayersRunner.cs @@ -23,7 +23,7 @@ public sealed class ContouredLayersRunner : IShaderRunner private ReadOnlyTexture2D? texture; /// - public void Execute(IReadWriteTexture2D texture, TimeSpan timespan, object? parameter) + public bool TryExecute(IReadWriteTexture2D texture, TimeSpan timespan, object? parameter) { if (this.texture is null) { @@ -33,5 +33,7 @@ public sealed class ContouredLayersRunner : IShaderRunner } GraphicsDevice.Default.ForEach(texture, new ContouredLayers((float)timespan.TotalSeconds, this.texture)); + + return true; } } diff --git a/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs b/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs index ef540584..b270648c 100644 --- a/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs +++ b/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs @@ -185,11 +185,14 @@ partial class SwapChainManager return; } - // Start the initial frame separately, before the timer starts. This ensures that - // resuming after a pause correctly renders the first frame at the right time. OnResize(); - OnUpdate(renderStopwatch.Elapsed, parameter); - OnPresent(); + + // Start the initial frame separately, before the timer starts. This ensures that + // resuming after a pause correctly renders the first frame at the right time. + if (OnUpdate(renderStopwatch.Elapsed, parameter)) + { + OnPresent(); + } renderStopwatch.Start(); @@ -202,8 +205,11 @@ partial class SwapChainManager } OnResize(); - OnUpdate(renderStopwatch.Elapsed, parameter); - OnPresent(); + + if (OnUpdate(renderStopwatch.Elapsed, parameter)) + { + OnPresent(); + } } renderStopwatch.Stop(); @@ -215,41 +221,46 @@ partial class SwapChainManager private void RenderLoopWithDynamicResolution() { Stopwatch renderStopwatch = this.renderStopwatch ??= new(); + Stopwatch frameStopwatch = new(); CancellationToken cancellationToken = this.renderCancellationTokenSource!.Token; DynamicResolutionManager.Create(out DynamicResolutionManager frameTimeWatcher); - + if (!OnFrameRequest(out object? parameter, cancellationToken)) { return; } - Stopwatch frameStopwatch = Stopwatch.StartNew(); - OnResize(); - OnUpdate(renderStopwatch.Elapsed, parameter); - OnPresent(); + + if (OnUpdate(renderStopwatch.Elapsed, parameter)) + { + OnPresent(); + } renderStopwatch.Start(); while (!cancellationToken.IsCancellationRequested) { - // Evaluate the dynamic resolution frame time step, if the mode is enabled - if (frameTimeWatcher.Advance(frameStopwatch.ElapsedTicks, ref this.targetResolutionScale)) - { - this.isResizePending = true; - } - if (!OnFrameRequest(out parameter, cancellationToken)) { return; } + OnResize(); + frameStopwatch.Restart(); - OnResize(); - OnUpdate(renderStopwatch.Elapsed, parameter); - OnPresent(); + if (OnUpdate(renderStopwatch.Elapsed, parameter)) + { + OnPresent(); + + // Evaluate the dynamic resolution frame time step + if (frameTimeWatcher.Advance(frameStopwatch.ElapsedTicks, ref this.targetResolutionScale)) + { + this.isResizePending = true; + } + } } renderStopwatch.Stop(); @@ -309,9 +320,10 @@ partial class SwapChainManager /// /// The current time since the start of the application. /// The input parameter for the frame being rendered. - private void OnUpdate(TimeSpan time, object? parameter) + /// Whether or not to present the resulting frame. + private bool OnUpdate(TimeSpan time, object? parameter) { - this.shaderRunner!.Execute(this.texture!, time, parameter); + return this.shaderRunner!.TryExecute(this.texture!, time, parameter); } /// diff --git a/src/ComputeSharp.UI/IShaderRunner.cs b/src/ComputeSharp.UI/IShaderRunner.cs index a3c5aca3..63b66582 100644 --- a/src/ComputeSharp.UI/IShaderRunner.cs +++ b/src/ComputeSharp.UI/IShaderRunner.cs @@ -12,10 +12,11 @@ namespace ComputeSharp.WinUI; public interface IShaderRunner { /// - /// Renders a single frame to a texture. + /// Tries to render a single frame to a texture, optionally skipping the frame if needed. /// /// The target texture to render the frame to. /// The timespan for the current frame. /// The input parameter for the frame being rendered. - void Execute(IReadWriteTexture2D texture, TimeSpan timespan, object? parameter); + /// Whether or not to present the current frame. If , the frame will be skipped. + bool TryExecute(IReadWriteTexture2D texture, TimeSpan timespan, object? parameter); } diff --git a/src/ComputeSharp.UI/ShaderRunner{T}.cs b/src/ComputeSharp.UI/ShaderRunner{T}.cs index e1d43722..98b84b35 100644 --- a/src/ComputeSharp.UI/ShaderRunner{T}.cs +++ b/src/ComputeSharp.UI/ShaderRunner{T}.cs @@ -8,7 +8,8 @@ namespace ComputeSharp.WinUI; #endif /// -/// An implementation powered by a supplied shader type. +/// A simple implementation powered by a supplied shader type. +/// All rendered frames produced by this type will always be presented. /// /// The type of shader to use to render frames. public sealed class ShaderRunner : IShaderRunner @@ -56,7 +57,7 @@ public sealed class ShaderRunner : IShaderRunner } /// - public void Execute(IReadWriteTexture2D texture, TimeSpan time, object? parameter) + public bool TryExecute(IReadWriteTexture2D texture, TimeSpan time, object? parameter) { if (this.shaderFactory is Func shaderFactory) { @@ -66,5 +67,7 @@ public sealed class ShaderRunner : IShaderRunner { GraphicsDevice.Default.ForEach(texture, this.statefulShaderFactory!(time, parameter)); } + + return true; } }