From 227a1ffcc234d65a7839d2c94eeca35ad055c488 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 2 Aug 2021 00:42:13 +0200 Subject: [PATCH] Enabled toggle for dynamic resolution mode --- .../ViewModels/MainViewModel.cs | 12 ++++ .../Views/MainWindow.xaml | 5 ++ .../Controls/ComputeShaderPanel.Properties.cs | 37 ++++++++++- .../Controls/ComputeShaderPanel.Rendering.cs | 63 ++++++++++++++----- .../Controls/ComputeShaderPanel.cs | 12 ++-- .../Helpers/DynamicResolutionManager.cs | 17 ++--- 6 files changed, 115 insertions(+), 31 deletions(-) diff --git a/samples/ComputeSharp.SwapChain.WinUI/ComputeSharp.SwapChain.WinUI/ViewModels/MainViewModel.cs b/samples/ComputeSharp.SwapChain.WinUI/ComputeSharp.SwapChain.WinUI/ViewModels/MainViewModel.cs index 0dc7fa19..f994457c 100644 --- a/samples/ComputeSharp.SwapChain.WinUI/ComputeSharp.SwapChain.WinUI/ViewModels/MainViewModel.cs +++ b/samples/ComputeSharp.SwapChain.WinUI/ComputeSharp.SwapChain.WinUI/ViewModels/MainViewModel.cs @@ -18,6 +18,7 @@ namespace ComputeSharp.SwapChain.WinUI.ViewModels /// public MainViewModel() { + this.isDynamicResolutionEnabled = true; this.selectedResolutionScale = 100; this.selectedComputeShader = ComputeShaderOptions[0]; this.selectedComputeShader.IsSelected = true; @@ -31,6 +32,17 @@ namespace ComputeSharp.SwapChain.WinUI.ViewModels /// public IList ResolutionScaleOptions { get; } = new[] { 25, 50, 75, 100 }; + private bool isDynamicResolutionEnabled; + + /// + /// Gets or sets whether the dynamic resolution is enabled. + /// + public bool IsDynamicResolutionEnabled + { + get => this.isDynamicResolutionEnabled; + set => SetProperty(ref this.isDynamicResolutionEnabled, value); + } + private int selectedResolutionScale; /// diff --git a/samples/ComputeSharp.SwapChain.WinUI/ComputeSharp.SwapChain.WinUI/Views/MainWindow.xaml b/samples/ComputeSharp.SwapChain.WinUI/ComputeSharp.SwapChain.WinUI/Views/MainWindow.xaml index 5ab52fd5..0268c06d 100644 --- a/samples/ComputeSharp.SwapChain.WinUI/ComputeSharp.SwapChain.WinUI/Views/MainWindow.xaml +++ b/samples/ComputeSharp.SwapChain.WinUI/ComputeSharp.SwapChain.WinUI/Views/MainWindow.xaml @@ -134,6 +134,7 @@ x:Name="ShaderPanel" Grid.Row="1" ShaderRunner="{x:Bind ViewModel.SelectedComputeShader.ShaderRunner, Mode=OneWay}" + IsDynamicResolutionEnabled="{x:Bind ViewModel.IsDynamicResolutionEnabled, Mode=OneWay}" ResolutionScale="{x:Bind converters:ResolutionScaleConverter.ConvertPercentageToScale(ViewModel.SelectedResolutionScale), Mode=OneWay}" IsPaused="{x:Bind ViewModel.IsRenderingPaused, Mode=OneWay}"/> @@ -171,6 +172,10 @@ + + + /// Gets or sets whether dynamic resolution is enabled. If it is, the internal render + /// resolution of the shader being run will be automatically adjusted to reach 60fps. + /// The default value for this property is . + /// + public bool IsDynamicResolutionEnabled + { + get => (bool)GetValue(IsDynamicResolutionEnabledProperty); + set => SetValue(IsDynamicResolutionEnabledProperty, value); + } + + /// + /// The backing . + /// + public static readonly DependencyProperty IsDynamicResolutionEnabledProperty = DependencyProperty.Register( + nameof(IsDynamicResolutionEnabled), + typeof(bool), + typeof(ComputeShaderPanel), + new PropertyMetadata(true, OnIsDynamicResolutionEnabledPropertyChanged)); + + /// + private static void OnIsDynamicResolutionEnabledPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + var @this = (ComputeShaderPanel)d; + var isDynamicResolutionEnabled = (bool)e.NewValue; + + @this.isDynamicResolutionEnabled = isDynamicResolutionEnabled; } /// diff --git a/src/ComputeSharp.WinUI/Controls/ComputeShaderPanel.Rendering.cs b/src/ComputeSharp.WinUI/Controls/ComputeShaderPanel.Rendering.cs index b5631010..1214ff36 100644 --- a/src/ComputeSharp.WinUI/Controls/ComputeShaderPanel.Rendering.cs +++ b/src/ComputeSharp.WinUI/Controls/ComputeShaderPanel.Rendering.cs @@ -8,6 +8,8 @@ using TerraFX.Interop; using WinRT; using FX = TerraFX.Interop.Windows; +#pragma warning disable CS0420 + namespace ComputeSharp.WinUI { /// @@ -86,32 +88,42 @@ namespace ComputeSharp.WinUI /// /// Whether or not the window has been resized and requires the buffers to be updated. /// - private bool isResizePending; + private volatile bool isResizePending; /// /// The backing store for for the render thread. /// - private double width; + private volatile float width; /// /// The backing store for for the render thread. /// - private double height; + private volatile float height; /// /// The backing store for for the render thread. /// - private float compositionScaleX; + private volatile float compositionScaleX; /// /// The backing store for for the render thread. /// - private float compositionScaleY; + private volatile float compositionScaleY; /// - /// The resolution scale used to render frames. + /// The resolution scale used to render frames when explicitly controlled by the user. /// - private double resolutionScale; + private volatile float resolutionScale; + + /// + /// The resolution scale used to render frames, in either render mode. + /// + private volatile float targetResolutionScale; + + /// + /// Whether dynamic resolution is currently enabled. + /// + private volatile bool isDynamicResolutionEnabled; /// /// Indicates whether or not the rendering has been canceled. @@ -181,8 +193,8 @@ namespace ComputeSharp.WinUI dxgiSwapChainDesc1.BufferCount = 2; dxgiSwapChainDesc1.Flags = (uint)DXGI_SWAP_CHAIN_FLAG.DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT; dxgiSwapChainDesc1.Format = DXGI_FORMAT.DXGI_FORMAT_R8G8B8A8_UNORM; - dxgiSwapChainDesc1.Width = (uint)Math.Max(Math.Ceiling(this.width * this.compositionScaleX * this.resolutionScale), 1.0); - dxgiSwapChainDesc1.Height = (uint)Math.Max(Math.Ceiling(this.height * this.compositionScaleY * this.resolutionScale), 1.0); + dxgiSwapChainDesc1.Width = (uint)Math.Max(Math.Ceiling(this.width * this.compositionScaleX * this.targetResolutionScale), 1.0); + dxgiSwapChainDesc1.Height = (uint)Math.Max(Math.Ceiling(this.height * this.compositionScaleY * this.targetResolutionScale), 1.0); dxgiSwapChainDesc1.SampleDesc = new DXGI_SAMPLE_DESC(count: 1, quality: 0); dxgiSwapChainDesc1.Scaling = DXGI_SCALING.DXGI_SCALING_STRETCH; dxgiSwapChainDesc1.Stereo = 0; @@ -277,15 +289,15 @@ namespace ComputeSharp.WinUI // Resize the swap chain buffers this.dxgiSwapChain2.Get()->ResizeBuffers( 2, - (uint)Math.Max(Math.Ceiling(this.width * this.compositionScaleX * this.resolutionScale), 1.0), - (uint)Math.Max(Math.Ceiling(this.height * this.compositionScaleY * this.resolutionScale), 1.0), + (uint)Math.Max(Math.Ceiling(this.width * this.compositionScaleX * this.targetResolutionScale), 1.0), + (uint)Math.Max(Math.Ceiling(this.height * this.compositionScaleY * this.targetResolutionScale), 1.0), DXGI_FORMAT.DXGI_FORMAT_UNKNOWN, (uint)DXGI_SWAP_CHAIN_FLAG.DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT).Assert(); // Apply the necessary scale transform DXGI_MATRIX_3X2_F transformMatrix = default; - transformMatrix._11 = (float)((1 / this.compositionScaleX) * (1 / this.resolutionScale)); - transformMatrix._22 = (float)((1 / this.compositionScaleY) * (1 / this.resolutionScale)); + transformMatrix._11 = (1 / this.compositionScaleX) * (1 / this.targetResolutionScale); + transformMatrix._22 = (1 / this.compositionScaleY) * (1 / this.targetResolutionScale); this.dxgiSwapChain2.Get()->SetMatrixTransform(&transformMatrix).Assert(); @@ -420,25 +432,44 @@ namespace ComputeSharp.WinUI DynamicResolutionManager.Create(out DynamicResolutionManager frameTimeWatcher); + bool isDynamicResolutionEnabled = @this.isDynamicResolutionEnabled; + // 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. @this.OnUpdate(renderStopwatch.Elapsed); renderStopwatch.Start(); - const long targetFrameTimeInTicksFor60fps = 166666; + const long targetFrameTimeInTicksFor61fps = 163934; // Main render loop, until cancellation is requested while (!@this.isCancellationRequested) { _ = FX.WaitForSingleObjectEx(frameLatencyWaitableObject, 1000, 1); - if (frameTimeWatcher.Advance(frameStopwatch.ElapsedTicks, ref @this.resolutionScale)) + // Update the resolution mode, if needed + if (isDynamicResolutionEnabled != @this.isDynamicResolutionEnabled) + { + isDynamicResolutionEnabled = !isDynamicResolutionEnabled; + + if (isDynamicResolutionEnabled) + { + DynamicResolutionManager.Create(out frameTimeWatcher); + } + else + { + @this.targetResolutionScale = @this.resolutionScale; + } + } + + // Evaluate the dynamic resolution frame time step, if the mode is enabled + if (isDynamicResolutionEnabled && + frameTimeWatcher.Advance(frameStopwatch.ElapsedTicks, ref @this.targetResolutionScale)) { @this.isResizePending = true; } - while (frameStopwatch.ElapsedTicks < targetFrameTimeInTicksFor60fps) + while (frameStopwatch.ElapsedTicks < targetFrameTimeInTicksFor61fps) { } diff --git a/src/ComputeSharp.WinUI/Controls/ComputeShaderPanel.cs b/src/ComputeSharp.WinUI/Controls/ComputeShaderPanel.cs index 06bf076c..6a8d241d 100644 --- a/src/ComputeSharp.WinUI/Controls/ComputeShaderPanel.cs +++ b/src/ComputeSharp.WinUI/Controls/ComputeShaderPanel.cs @@ -31,11 +31,13 @@ namespace ComputeSharp.WinUI // Initializes the swap chain and starts the render thread private void ComputeShaderPanel_Loaded(object sender, RoutedEventArgs e) { - this.width = ActualWidth; - this.height = ActualHeight; + this.width = (float)ActualWidth; + this.height = (float)ActualHeight; this.compositionScaleX = CompositionScaleX; this.compositionScaleY = CompositionScaleY; - this.resolutionScale = ResolutionScale; + this.resolutionScale = (float)ResolutionScale; + this.targetResolutionScale = 1.0f; + this.isDynamicResolutionEnabled = true; OnInitialize(); OnStartRenderLoop(); @@ -50,8 +52,8 @@ namespace ComputeSharp.WinUI // Updates the background store for the frame size factors used by the render thread private void ComputeShaderPanel_SizeChanged(object sender, SizeChangedEventArgs e) { - this.width = e.NewSize.Width; - this.height = e.NewSize.Height; + this.width = (float)e.NewSize.Width; + this.height = (float)e.NewSize.Height; OnResize(); } diff --git a/src/ComputeSharp.WinUI/Helpers/DynamicResolutionManager.cs b/src/ComputeSharp.WinUI/Helpers/DynamicResolutionManager.cs index af81f181..78d9dcdb 100644 --- a/src/ComputeSharp.WinUI/Helpers/DynamicResolutionManager.cs +++ b/src/ComputeSharp.WinUI/Helpers/DynamicResolutionManager.cs @@ -1,5 +1,6 @@ using System; using System.Runtime.InteropServices; +using System.Threading; namespace ComputeSharp.WinUI.Helpers { @@ -11,7 +12,7 @@ namespace ComputeSharp.WinUI.Helpers /// /// The target frame time in ticks to reach 60fps (set to 64fps). /// - private const long TargetFrameTimeInTicksFor60fps = 156250; + public const long TargetFrameTimeInTicksFor60fps = 156250; /// /// The upper frame time threshold in ticks to reach 60fps. @@ -31,12 +32,12 @@ namespace ComputeSharp.WinUI.Helpers /// /// The threshold for scale factor updates. /// - private const double ScaleFactorDeltaThreshold = 0.005; + private const float ScaleFactorDeltaThreshold = 0.005f; /// /// The constant multiplicative factor for the scale factor. /// - private const double ScaleFactorDeltaK = 0.8; + private const float ScaleFactorDeltaK = 0.8f; /// /// The window of registered frame times for previous frames. @@ -76,7 +77,7 @@ namespace ComputeSharp.WinUI.Helpers /// The frame time in ticks for the last rendered frame. /// The current scale factor value being used to render frames. /// Whether or not has been updated and a resize is needed. - public bool Advance(long frameTimeInTicks, ref double scaleFactor) + public bool Advance(long frameTimeInTicks, ref float scaleFactor) { // Ignore the current frame if a resize has just happened if (this.frameTimeOffset == -1) @@ -104,16 +105,16 @@ namespace ComputeSharp.WinUI.Helpers if (averageFrameTimeInTicks is < LowerFrameTimeThresholdInTicksFor60fps or > UpperFrameTimeThresholdInTicksFor60fps) { - double - frameTimeDelta = (TargetFrameTimeInTicksFor60fps - averageFrameTimeInTicks) / (double)averageFrameTimeInTicks, + float + frameTimeDelta = (TargetFrameTimeInTicksFor60fps - averageFrameTimeInTicks) / (float)averageFrameTimeInTicks, scaleFactorDelta = scaleFactor * frameTimeDelta * ScaleFactorDeltaK, - updatedScaleFactor = Math.Clamp(scaleFactor + scaleFactorDelta, 0.10, 1.0); + updatedScaleFactor = Math.Clamp(scaleFactor + scaleFactorDelta, 0.10f, 1.0f); // Apply the scale factor update if the target scale has changed enough. This helps to avoid // too frequence resolution changes if the scale factor only changes very little every time. if (Math.Abs(scaleFactor - updatedScaleFactor) >= ScaleFactorDeltaThreshold) { - scaleFactor = updatedScaleFactor; + Volatile.Write(ref scaleFactor, updatedScaleFactor); this.frameTimeOffset = -1;