From b3338be7dddd67ecf6c2c109813048e4b385c616 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 2 Jan 2022 13:31:35 +0100 Subject: [PATCH] Revert multiple frames latency --- .../Controls/ComputeShaderPanel.Properties.cs | 2 +- .../Controls/ComputeShaderPanel.Rendering.cs | 35 ++++++------------- .../Controls/ComputeShaderPanel.cs | 2 +- .../DirectX/shared/dxgi1_4/IDXGISwapChain3.cs | 7 ---- 4 files changed, 13 insertions(+), 33 deletions(-) diff --git a/src/ComputeSharp.UI/Controls/ComputeShaderPanel.Properties.cs b/src/ComputeSharp.UI/Controls/ComputeShaderPanel.Properties.cs index 73e49028..818f753e 100644 --- a/src/ComputeSharp.UI/Controls/ComputeShaderPanel.Properties.cs +++ b/src/ComputeSharp.UI/Controls/ComputeShaderPanel.Properties.cs @@ -10,7 +10,7 @@ namespace ComputeSharp.WinUI; #endif /// -public sealed unsafe partial class ComputeShaderPanel +partial class ComputeShaderPanel { /// /// Gets or sets the instance to use to render content. diff --git a/src/ComputeSharp.UI/Controls/ComputeShaderPanel.Rendering.cs b/src/ComputeSharp.UI/Controls/ComputeShaderPanel.Rendering.cs index 9dd2fbc3..510266b6 100644 --- a/src/ComputeSharp.UI/Controls/ComputeShaderPanel.Rendering.cs +++ b/src/ComputeSharp.UI/Controls/ComputeShaderPanel.Rendering.cs @@ -29,7 +29,7 @@ namespace ComputeSharp.WinUI; #endif /// -public sealed partial class ComputeShaderPanel +partial class ComputeShaderPanel { /// /// The render thread in use, if any. @@ -231,10 +231,6 @@ public sealed partial class ComputeShaderPanel } } - // Set the maximum frame latency to 2 to improve rendering performance. This is fine in this scenario, as we're not - // dealing with a real-time game or something that absolutely needs the least amount of frame latency as possible. - this.dxgiSwapChain3.Get()->SetMaximumFrameLatency(2).Assert(); - // Get the awaitable object to synchronizize present calls this.frameLatencyWaitableObject = this.dxgiSwapChain3.Get()->GetFrameLatencyWaitableObject(); @@ -352,10 +348,10 @@ public sealed partial class ComputeShaderPanel } /// - /// Updates the render resolution, if needed, and renders and presents a new frame. + /// Updates the render resolution, if needed, and renders a new frame. /// /// The current time since the start of the application. - private unsafe bool OnUpdate(TimeSpan time) + private void OnUpdate(TimeSpan time) { if (this.isResizePending) { @@ -367,19 +363,11 @@ public sealed partial class ComputeShaderPanel // Skip if no factory is available if (this.shaderRunner is null) { - return false; + return; } - // Wait for the swap chain to finish presenting - _ = Win32.WaitForSingleObjectEx(frameLatencyWaitableObject, 1000, true); - // Generate the new frame this.shaderRunner.Execute(this.texture!, time); - - // Present the new frame - OnPresent(); - - return true; } /// @@ -387,6 +375,9 @@ public sealed partial class ComputeShaderPanel /// private unsafe void OnPresent() { + // Wait for the swap chain to be ready for presenting + _ = Win32.WaitForSingleObjectEx(frameLatencyWaitableObject, 1000, true); + using ComPtr d3D12Resource = default; // Get the underlying ID3D12Resource pointer for the texture @@ -472,10 +463,8 @@ public sealed partial class ComputeShaderPanel // 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. - while (!@this.OnUpdate(renderStopwatch.Elapsed) && - !@this.isCancellationRequested) - { - } + @this.OnUpdate(renderStopwatch.Elapsed); + @this.OnPresent(); renderStopwatch.Start(); @@ -506,10 +495,8 @@ public sealed partial class ComputeShaderPanel frameStopwatch.Restart(); - while (!@this.OnUpdate(renderStopwatch.Elapsed) && - !@this.isCancellationRequested) - { - } + @this.OnUpdate(renderStopwatch.Elapsed); + @this.OnPresent(); } renderStopwatch.Stop(); diff --git a/src/ComputeSharp.UI/Controls/ComputeShaderPanel.cs b/src/ComputeSharp.UI/Controls/ComputeShaderPanel.cs index 70d86b9d..9b0f15d7 100644 --- a/src/ComputeSharp.UI/Controls/ComputeShaderPanel.cs +++ b/src/ComputeSharp.UI/Controls/ComputeShaderPanel.cs @@ -14,7 +14,7 @@ namespace ComputeSharp.WinUI; /// A custom that can be used to render animated backgrounds. /// It is powered by ComputeSharp and leverages compute shaders to create frames to display. /// -public sealed unsafe partial class ComputeShaderPanel : SwapChainPanel +public sealed partial class ComputeShaderPanel : SwapChainPanel { /// /// Creates a new instance. diff --git a/src/TerraFX.Interop.Windows/DirectX/shared/dxgi1_4/IDXGISwapChain3.cs b/src/TerraFX.Interop.Windows/DirectX/shared/dxgi1_4/IDXGISwapChain3.cs index 4b6b04e8..358cbf3d 100644 --- a/src/TerraFX.Interop.Windows/DirectX/shared/dxgi1_4/IDXGISwapChain3.cs +++ b/src/TerraFX.Interop.Windows/DirectX/shared/dxgi1_4/IDXGISwapChain3.cs @@ -63,13 +63,6 @@ namespace TerraFX.Interop.DirectX return ((delegate* unmanaged[Stdcall])(lpVtbl[13]))((IDXGISwapChain3*)Unsafe.AsPointer(ref this), BufferCount, Width, Height, NewFormat, SwapChainFlags); } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - [VtblIndex(31)] - public HRESULT SetMaximumFrameLatency(uint MaxLatency) - { - return ((delegate* unmanaged[Stdcall])(lpVtbl[31]))((IDXGISwapChain3*)Unsafe.AsPointer(ref this), MaxLatency); - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] [VtblIndex(33)] public HANDLE GetFrameLatencyWaitableObject()