diff --git a/src/ComputeSharp.UI/ComputeSharp.UI.projitems b/src/ComputeSharp.UI/ComputeSharp.UI.projitems
index c990a06c..200af187 100644
--- a/src/ComputeSharp.UI/ComputeSharp.UI.projitems
+++ b/src/ComputeSharp.UI/ComputeSharp.UI.projitems
@@ -14,6 +14,7 @@
+
diff --git a/src/ComputeSharp.UI/Controls/ComputeShaderPanel.Properties.cs b/src/ComputeSharp.UI/Controls/ComputeShaderPanel.Properties.cs
index 01ea621d..04939c0d 100644
--- a/src/ComputeSharp.UI/Controls/ComputeShaderPanel.Properties.cs
+++ b/src/ComputeSharp.UI/Controls/ComputeShaderPanel.Properties.cs
@@ -14,6 +14,38 @@ namespace ComputeSharp.WinUI;
///
partial class ComputeShaderPanel
{
+ ///
+ /// Gets or sets the instance to use to request new frames.
+ ///
+ public IFrameRequestQueue? FrameRequestQueue
+ {
+ get => (IFrameRequestQueue)GetValue(FrameRequestQueueProperty);
+ set => SetValue(FrameRequestQueueProperty, value);
+ }
+
+ ///
+ /// The backing .
+ ///
+ public static readonly DependencyProperty FrameRequestQueueProperty = DependencyProperty.Register(
+ nameof(FrameRequestQueue),
+ typeof(IFrameRequestQueue),
+ typeof(ComputeShaderPanel),
+ new PropertyMetadata(null, OnFrameRequestQueueChanged));
+
+ ///
+ 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);
+ }
+ }
+
///
/// Gets or sets the instance to use to render content.
///
@@ -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);
}
}
}
diff --git a/src/ComputeSharp.UI/Controls/ComputeShaderPanel.cs b/src/ComputeSharp.UI/Controls/ComputeShaderPanel.cs
index 9b2ebb8f..13bc5a40 100644
--- a/src/ComputeSharp.UI/Controls/ComputeShaderPanel.cs
+++ b/src/ComputeSharp.UI/Controls/ComputeShaderPanel.cs
@@ -58,7 +58,7 @@ public sealed partial class ComputeShaderPanel : SwapChainPanel
if (ShaderRunner is IShaderRunner shaderRunner)
{
- this.swapChainManager.StartRenderLoop(shaderRunner);
+ this.swapChainManager.StartRenderLoop(FrameRequestQueue, shaderRunner);
}
}
diff --git a/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs b/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs
index f8b75f6d..fd265119 100644
--- a/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs
+++ b/src/ComputeSharp.UI/Helpers/SwapChainManager.Rendering.cs
@@ -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
///
/// Starts the current render loop.
///
+ /// The instance to use, if available.
/// The instance to use to render frames.
- 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();
}
+ ///
+ /// Waits for a new frame request and retrieves its parameter.
+ ///
+ /// The input parameter for the frame to render.
+ /// A token to cancel waiting for a new frame.
+ /// Whether the operation was canceled.
+ 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;
+ }
+
///
/// Resizes the current application, if needed.
///
@@ -228,9 +285,10 @@ partial class SwapChainManager
/// Updates the render resolution, if needed, and renders a new frame.
///
/// The current time since the start of the application.
- private void OnUpdate(TimeSpan time)
+ /// The input parameter for the frame being rendered.
+ private void OnUpdate(TimeSpan time, object? parameter)
{
- this.shaderRunner!.Execute(this.texture!, time, null);
+ this.shaderRunner!.Execute(this.texture!, time, parameter);
}
///
diff --git a/src/ComputeSharp.UI/Helpers/SwapChainManager.cs b/src/ComputeSharp.UI/Helpers/SwapChainManager.cs
index 8e2a3ea4..80533ead 100644
--- a/src/ComputeSharp.UI/Helpers/SwapChainManager.cs
+++ b/src/ComputeSharp.UI/Helpers/SwapChainManager.cs
@@ -90,6 +90,11 @@ internal sealed unsafe partial class SwapChainManager : NativeObject
///
private volatile CancellationTokenSource? renderCancellationTokenSource;
+ ///
+ /// The instance used to get frame requests and parameters.
+ ///
+ private volatile IFrameRequestQueue? frameRequestQueue;
+
///
/// The instance used to create shaders to run.
///
diff --git a/src/ComputeSharp.UI/IFrameRequestQueue.cs b/src/ComputeSharp.UI/IFrameRequestQueue.cs
new file mode 100644
index 00000000..41a1e719
--- /dev/null
+++ b/src/ComputeSharp.UI/IFrameRequestQueue.cs
@@ -0,0 +1,20 @@
+using System.Threading;
+
+#if WINDOWS_UWP
+namespace ComputeSharp.Uwp;
+#else
+namespace ComputeSharp.WinUI;
+#endif
+
+///
+/// An interface for a shader runner to be used with .
+///
+public interface IFrameRequestQueue
+{
+ ///
+ /// Waits for a new frame request to be available, retrieving its parameter, if any.
+ ///
+ /// The input parameter for the frame to render.
+ /// A token to cancel waiting for a new frame.
+ void Dequeue(out object? parameter, CancellationToken token);
+}