diff --git a/src/ComputeSharp.UI/Controls/ComputeShaderPanel.Rendering.cs b/src/ComputeSharp.UI/Controls/ComputeShaderPanel.Rendering.cs
index 855c3223..9dd2fbc3 100644
--- a/src/ComputeSharp.UI/Controls/ComputeShaderPanel.Rendering.cs
+++ b/src/ComputeSharp.UI/Controls/ComputeShaderPanel.Rendering.cs
@@ -231,6 +231,10 @@ 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();
@@ -348,10 +352,10 @@ public sealed partial class ComputeShaderPanel
}
///
- /// Updates the render resolution, if needed, and renders a new frame.
+ /// Updates the render resolution, if needed, and renders and presents a new frame.
///
/// The current time since the start of the application.
- private unsafe void OnUpdate(TimeSpan time)
+ private unsafe bool OnUpdate(TimeSpan time)
{
if (this.isResizePending)
{
@@ -363,11 +367,19 @@ public sealed partial class ComputeShaderPanel
// Skip if no factory is available
if (this.shaderRunner is null)
{
- return;
+ return false;
}
+ // 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;
}
///
@@ -375,8 +387,6 @@ public sealed partial class ComputeShaderPanel
///
private unsafe void OnPresent()
{
- _ = Win32.WaitForSingleObjectEx(frameLatencyWaitableObject, 1000, 1);
-
using ComPtr d3D12Resource = default;
// Get the underlying ID3D12Resource pointer for the texture
@@ -462,13 +472,13 @@ 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.
- @this.OnUpdate(renderStopwatch.Elapsed);
- @this.OnPresent();
+ while (!@this.OnUpdate(renderStopwatch.Elapsed) &&
+ !@this.isCancellationRequested)
+ {
+ }
renderStopwatch.Start();
- const long targetFrameTimeInTicksFor61fps = 163934;
-
// Main render loop, until cancellation is requested
while (!@this.isCancellationRequested)
{
@@ -494,14 +504,12 @@ public sealed partial class ComputeShaderPanel
@this.isResizePending = true;
}
- while (frameStopwatch.ElapsedTicks < targetFrameTimeInTicksFor61fps)
- {
- }
-
frameStopwatch.Restart();
- @this.OnUpdate(renderStopwatch.Elapsed);
- @this.OnPresent();
+ while (!@this.OnUpdate(renderStopwatch.Elapsed) &&
+ !@this.isCancellationRequested)
+ {
+ }
}
renderStopwatch.Stop();
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 358cbf3d..4b6b04e8 100644
--- a/src/TerraFX.Interop.Windows/DirectX/shared/dxgi1_4/IDXGISwapChain3.cs
+++ b/src/TerraFX.Interop.Windows/DirectX/shared/dxgi1_4/IDXGISwapChain3.cs
@@ -63,6 +63,13 @@ 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()