Fix frame latency management, improve rendering performance

このコミットが含まれているのは:
Sergio Pedri committed 2022-01-01 15:39:18 +01:00
1 親 0c32091987
コミット 9a0c27b71e
2ファイル変更
+30 -15

検索条件に一致するファイルはありません

+23 -15
ファイルの表示
@@ -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
}
/// <summary>
/// Updates the render resolution, if needed, and renders a new frame.
/// Updates the render resolution, if needed, and renders and presents a new frame.
/// </summary>
/// <param name="time">The current time since the start of the application.</param>
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;
}
/// <summary>
@@ -375,8 +387,6 @@ public sealed partial class ComputeShaderPanel
/// </summary>
private unsafe void OnPresent()
{
_ = Win32.WaitForSingleObjectEx(frameLatencyWaitableObject, 1000, 1);
using ComPtr<ID3D12Resource> 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();
+7
ファイルの表示
@@ -63,6 +63,13 @@ namespace TerraFX.Interop.DirectX
return ((delegate* unmanaged[Stdcall]<IDXGISwapChain3*, uint, uint, uint, DXGI_FORMAT, uint, int>)(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]<IDXGISwapChain3*, uint, int>)(lpVtbl[31]))((IDXGISwapChain3*)Unsafe.AsPointer(ref this), MaxLatency);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[VtblIndex(33)]
public HANDLE GetFrameLatencyWaitableObject()