From cb975e552ad43ce2b8835a897c693f8eb39cefe4 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 6 Jan 2022 17:08:53 +0100 Subject: [PATCH] Fix resolution scale setting in shader panels --- .../Helpers/SwapChainManager.Rendering.cs | 8 ++++---- .../Helpers/SwapChainManager.cs | 18 ++++++++++++------ 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs b/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs index b270648c..bcacdab2 100644 --- a/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs +++ b/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs @@ -44,6 +44,7 @@ partial class SwapChainManager this.renderCancellationTokenSource = new CancellationTokenSource(); this.renderThread = newRenderThread; this.renderSemaphore = new SemaphoreSlim(0, 1); + this.isResizePending = true; newRenderThread.Start(this); } @@ -85,6 +86,7 @@ partial class SwapChainManager this.renderThread = newRenderThread; this.renderSemaphore = new SemaphoreSlim(0, 1); this.isDynamicResolutionEnabled = isDynamicResolutionEnabled; + this.isResizePending = true; newRenderThread.Start(this); } @@ -149,14 +151,12 @@ partial class SwapChainManager if (this.isDynamicResolutionEnabled) { - this.targetResolutionScale = 1.0f; + this.dynamicResolutionScale = this.resolutionScale; RenderLoopWithDynamicResolution(); } else { - this.targetResolutionScale = this.resolutionScale; - RenderLoop(); } @@ -256,7 +256,7 @@ partial class SwapChainManager OnPresent(); // Evaluate the dynamic resolution frame time step - if (frameTimeWatcher.Advance(frameStopwatch.ElapsedTicks, ref this.targetResolutionScale)) + if (frameTimeWatcher.Advance(frameStopwatch.ElapsedTicks, ref this.dynamicResolutionScale)) { this.isResizePending = true; } diff --git a/src/ComputeSharp.UI/Helpers/SwapChainManager.cs b/src/ComputeSharp.UI/Helpers/SwapChainManager.cs index 500654f6..4021ed1f 100644 --- a/src/ComputeSharp.UI/Helpers/SwapChainManager.cs +++ b/src/ComputeSharp.UI/Helpers/SwapChainManager.cs @@ -166,9 +166,9 @@ internal sealed unsafe partial class SwapChainManager : NativeObject private volatile float resolutionScale; /// - /// The resolution scale used to render frames, in either render mode. + /// The dynamic resolution scale used to render frames. /// - private volatile float targetResolutionScale; + private volatile float dynamicResolutionScale; /// /// Whether dynamic resolution is currently enabled. @@ -327,8 +327,14 @@ internal sealed unsafe partial class SwapChainManager : NativeObject /// private unsafe void ApplyResize() { - uint resizedWidth = (uint)Math.Max(Math.Ceiling(this.width * this.compositionScaleX * this.targetResolutionScale), 1.0); - uint resizeHeight = (uint)Math.Max(Math.Ceiling(this.height * this.compositionScaleY * this.targetResolutionScale), 1.0); + // The target resolution scale is either the dynamic resolution scale, being updated by the render thread, + // or the fixed resolution scale set by the user. If dynamic resolution is enabled, the latter is ignored. + float targetResolutionScale = this.isDynamicResolutionEnabled + ? this.dynamicResolutionScale + : this.resolutionScale; + + uint resizedWidth = (uint)Math.Max(Math.Ceiling(this.width * this.compositionScaleX * targetResolutionScale), 1.0); + uint resizeHeight = (uint)Math.Max(Math.Ceiling(this.height * this.compositionScaleY * targetResolutionScale), 1.0); if (this.texture is null || this.texture.Width != resizedWidth || @@ -355,8 +361,8 @@ internal sealed unsafe partial class SwapChainManager : NativeObject // Apply the necessary scale transform DXGI_MATRIX_3X2_F transformMatrix = default; - transformMatrix._11 = (1 / this.compositionScaleX) * (1 / this.targetResolutionScale); - transformMatrix._22 = (1 / this.compositionScaleY) * (1 / this.targetResolutionScale); + transformMatrix._11 = (1 / this.compositionScaleX) * (1 / targetResolutionScale); + transformMatrix._22 = (1 / this.compositionScaleY) * (1 / targetResolutionScale); this.dxgiSwapChain3.Get()->SetMatrixTransform(&transformMatrix).Assert();