Add initial version of IFrameRequestQueue

このコミットが含まれているのは:
Sergio Pedri committed 2022-01-02 20:32:40 +01:00
1 親 28a4ad3b48
コミット 22d429d101
6ファイル変更
+138 -22

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

+1
ファイルの表示
@@ -14,6 +14,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Helpers\SwapChainManager.Rendering.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\SwapChainManager.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\DynamicResolutionManager.cs" />
<Compile Include="$(MSBuildThisFileDirectory)IFrameRequestQueue.cs" />
<Compile Include="$(MSBuildThisFileDirectory)IShaderRunner.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ShaderRunner{T}.cs" />
</ItemGroup>
+34 -2
ファイルの表示
@@ -14,6 +14,38 @@ namespace ComputeSharp.WinUI;
/// <inheritdoc cref="ComputeShaderPanel"/>
partial class ComputeShaderPanel
{
/// <summary>
/// Gets or sets the <see cref="IFrameRequestQueue"/> instance to use to request new frames.
/// </summary>
public IFrameRequestQueue? FrameRequestQueue
{
get => (IFrameRequestQueue)GetValue(FrameRequestQueueProperty);
set => SetValue(FrameRequestQueueProperty, value);
}
/// <summary>
/// The <see cref="DependencyProperty"/> backing <see cref="ShaderRunner"/>.
/// </summary>
public static readonly DependencyProperty FrameRequestQueueProperty = DependencyProperty.Register(
nameof(FrameRequestQueue),
typeof(IFrameRequestQueue),
typeof(ComputeShaderPanel),
new PropertyMetadata(null, OnFrameRequestQueueChanged));
/// <inheritdoc cref="DependencyPropertyChangedCallback"/>
private static void OnFrameRequestQueueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var @this = (ComputeShaderPanel)d;
var frameRequestQueue = (IFrameRequestQueue?)e.NewValue;
if (@this.IsLoaded &&
!@this.IsPaused &&
@this.ShaderRunner is IShaderRunner shaderRunner)
{
@this.swapChainManager.StartRenderLoop(frameRequestQueue, shaderRunner);
}
}
/// <summary>
/// Gets or sets the <see cref="IShaderRunner"/> instance to use to render content.
/// </summary>
@@ -45,7 +77,7 @@ partial class ComputeShaderPanel
else if (@this.IsLoaded &&
!@this.IsPaused)
{
@this.swapChainManager.StartRenderLoop(shaderRunner);
@this.swapChainManager.StartRenderLoop(@this.FrameRequestQueue, shaderRunner);
}
}
@@ -138,7 +170,7 @@ partial class ComputeShaderPanel
else if (@this.IsLoaded &&
@this.ShaderRunner is IShaderRunner shaderRunner)
{
@this.swapChainManager.StartRenderLoop(shaderRunner);
@this.swapChainManager.StartRenderLoop(@this.FrameRequestQueue, shaderRunner);
}
}
}
+1 -1
ファイルの表示
@@ -58,7 +58,7 @@ public sealed partial class ComputeShaderPanel : SwapChainPanel
if (ShaderRunner is IShaderRunner shaderRunner)
{
this.swapChainManager.StartRenderLoop(shaderRunner);
this.swapChainManager.StartRenderLoop(FrameRequestQueue, shaderRunner);
}
}
+77 -19
ファイルの表示
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading;
using Microsoft.Toolkit.Diagnostics;
@@ -17,8 +18,9 @@ partial class SwapChainManager
/// <summary>
/// Starts the current render loop.
/// </summary>
/// <param name="frameRequestQueue">The <see cref="IFrameRequestQueue"/> instance to use, if available.</param>
/// <param name="shaderRunner">The <see cref="IShaderRunner"/> instance to use to render frames.</param>
public void StartRenderLoop(IShaderRunner shaderRunner)
public void StartRenderLoop(IFrameRequestQueue? frameRequestQueue, IShaderRunner shaderRunner)
{
ThrowIfDisposed();
@@ -31,6 +33,7 @@ partial class SwapChainManager
Thread newRenderThread = new(static args => ((SwapChainManager)args!).SwitchAndStartRenderLoop());
this.frameRequestQueue = frameRequestQueue;
this.shaderRunner = shaderRunner;
this.renderCancellationTokenSource = new CancellationTokenSource();
this.renderThread = newRenderThread;
@@ -154,20 +157,30 @@ partial class SwapChainManager
Stopwatch renderStopwatch = this.renderStopwatch ??= new();
CancellationToken cancellationToken = this.renderCancellationTokenSource!.Token;
if (!OnFrameRequest(out object? parameter, cancellationToken))
{
return;
}
// 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.OnResize();
this.OnUpdate(renderStopwatch.Elapsed);
this.OnPresent();
OnResize();
OnUpdate(renderStopwatch.Elapsed, parameter);
OnPresent();
renderStopwatch.Start();
// Main render loop, until cancellation is requested
while (!cancellationToken.IsCancellationRequested)
{
this.OnResize();
this.OnUpdate(renderStopwatch.Elapsed);
this.OnPresent();
if (!OnFrameRequest(out parameter, cancellationToken))
{
return;
}
OnResize();
OnUpdate(renderStopwatch.Elapsed, parameter);
OnPresent();
}
renderStopwatch.Stop();
@@ -179,20 +192,23 @@ partial class SwapChainManager
private void RenderLoopWithDynamicResolution()
{
Stopwatch renderStopwatch = this.renderStopwatch ??= new();
Stopwatch frameStopwatch = Stopwatch.StartNew();
CancellationToken cancellationToken = this.renderCancellationTokenSource!.Token;
DynamicResolutionManager.Create(out DynamicResolutionManager frameTimeWatcher);
if (!OnFrameRequest(out object? parameter, cancellationToken))
{
return;
}
// 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.OnResize();
this.OnUpdate(renderStopwatch.Elapsed);
this.OnPresent();
Stopwatch frameStopwatch = Stopwatch.StartNew();
OnResize();
OnUpdate(renderStopwatch.Elapsed, parameter);
OnPresent();
renderStopwatch.Start();
// Main render loop, until cancellation is requested
while (!cancellationToken.IsCancellationRequested)
{
// Evaluate the dynamic resolution frame time step, if the mode is enabled
@@ -201,16 +217,57 @@ partial class SwapChainManager
this.isResizePending = true;
}
if (!OnFrameRequest(out parameter, cancellationToken))
{
return;
}
frameStopwatch.Restart();
this.OnResize();
this.OnUpdate(renderStopwatch.Elapsed);
this.OnPresent();
OnResize();
OnUpdate(renderStopwatch.Elapsed, parameter);
OnPresent();
}
renderStopwatch.Stop();
}
/// <summary>
/// Waits for a new frame request and retrieves its parameter.
/// </summary>
/// <param name="parameter">The input parameter for the frame to render.</param>
/// <param name="token">A token to cancel waiting for a new frame.</param>
/// <returns>Whether the operation was canceled.</returns>
private bool OnFrameRequest(out object? parameter, CancellationToken token)
{
if (this.frameRequestQueue is IFrameRequestQueue frameRequestQueue)
{
[MethodImpl(MethodImplOptions.NoInlining)]
static bool WaitForFrameRequest(IFrameRequestQueue frameRequestQueue, out object? parameter, CancellationToken token)
{
try
{
// Wait for a new frame request
frameRequestQueue.Dequeue(out parameter, token);
return true;
}
catch (OperationCanceledException)
{
parameter = null;
return false;
}
}
return WaitForFrameRequest(frameRequestQueue, out parameter, token);
}
parameter = null;
return true;
}
/// <summary>
/// Resizes the current application, if needed.
/// </summary>
@@ -228,9 +285,10 @@ partial class SwapChainManager
/// Updates the render resolution, if needed, and renders a new frame.
/// </summary>
/// <param name="time">The current time since the start of the application.</param>
private void OnUpdate(TimeSpan time)
/// <param name="parameter">The input parameter for the frame being rendered.</param>
private void OnUpdate(TimeSpan time, object? parameter)
{
this.shaderRunner!.Execute(this.texture!, time, null);
this.shaderRunner!.Execute(this.texture!, time, parameter);
}
/// <summary>
+5
ファイルの表示
@@ -90,6 +90,11 @@ internal sealed unsafe partial class SwapChainManager : NativeObject
/// </summary>
private volatile CancellationTokenSource? renderCancellationTokenSource;
/// <summary>
/// The <see cref="IFrameRequestQueue"/> instance used to get frame requests and parameters.
/// </summary>
private volatile IFrameRequestQueue? frameRequestQueue;
/// <summary>
/// The <see cref="IShaderRunner"/> instance used to create shaders to run.
/// </summary>
+20
ファイルの表示
@@ -0,0 +1,20 @@
using System.Threading;
#if WINDOWS_UWP
namespace ComputeSharp.Uwp;
#else
namespace ComputeSharp.WinUI;
#endif
/// <summary>
/// An interface for a shader runner to be used with <see cref="ComputeShaderPanel"/>.
/// </summary>
public interface IFrameRequestQueue
{
/// <summary>
/// Waits for a new frame request to be available, retrieving its parameter, if any.
/// </summary>
/// <param name="parameter">The input parameter for the frame to render.</param>
/// <param name="token">A token to cancel waiting for a new frame.</param>
void Dequeue(out object? parameter, CancellationToken token);
}