Merge pull request #186 from Sergio0694/feature/improved-shader-panel
ShaderPanel control revamp
このコミットが含まれているのは:
18ファイル変更
+1003
-295
検索条件に一致するファイルはありません
@@ -23,7 +23,7 @@ public sealed class ContouredLayersRunner : IShaderRunner
|
||||
private ReadOnlyTexture2D<Rgba32, Float4>? texture;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Execute(IReadWriteTexture2D<Float4> texture, TimeSpan timespan)
|
||||
public void Execute(IReadWriteTexture2D<Float4> texture, TimeSpan timespan, object? parameter)
|
||||
{
|
||||
if (this.texture is null)
|
||||
{
|
||||
|
||||
@@ -123,6 +123,7 @@
|
||||
<DependentUpon>App.xaml</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Properties\SkipLocalsInitAttribute.cs" />
|
||||
<Compile Include="Views\MainView.xaml.cs">
|
||||
<DependentUpon>MainView.xaml</DependentUpon>
|
||||
</Compile>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
@@ -25,4 +26,6 @@ using System.Runtime.InteropServices;
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
[assembly: ComVisible(false)]
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
[module: SkipLocalsInit]
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace System.Runtime.CompilerServices;
|
||||
|
||||
/// <summary>
|
||||
/// Used to indicate to the compiler that the <c>.locals init</c> flag should not be set in method headers.
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Module
|
||||
| AttributeTargets.Class
|
||||
| AttributeTargets.Struct
|
||||
| AttributeTargets.Interface
|
||||
| AttributeTargets.Constructor
|
||||
| AttributeTargets.Method
|
||||
| AttributeTargets.Property
|
||||
| AttributeTargets.Event, Inherited = false)]
|
||||
internal sealed class SkipLocalsInitAttribute : Attribute
|
||||
{
|
||||
}
|
||||
@@ -125,7 +125,7 @@
|
||||
<Grid>
|
||||
|
||||
<!-- Shader preview -->
|
||||
<computesharp:ComputeShaderPanel ShaderRunner="{x:Bind ShaderRunner}" />
|
||||
<computesharp:AnimatedComputeShaderPanel ShaderRunner="{x:Bind ShaderRunner}" />
|
||||
|
||||
<!--
|
||||
Note: here we have a selection property to work around {x:Bind} not working correctly outside of the
|
||||
@@ -153,7 +153,7 @@
|
||||
</Grid.Resources>
|
||||
|
||||
<!-- Shader -->
|
||||
<computesharp:ComputeShaderPanel
|
||||
<computesharp:AnimatedComputeShaderPanel
|
||||
x:Name="ShaderPanel"
|
||||
Grid.Row="1"
|
||||
IsDynamicResolutionEnabled="{x:Bind ViewModel.IsDynamicResolutionEnabled, Mode=OneWay}"
|
||||
|
||||
@@ -123,7 +123,7 @@
|
||||
<Grid>
|
||||
|
||||
<!-- Shader preview -->
|
||||
<computesharp:ComputeShaderPanel ShaderRunner="{x:Bind ShaderRunner}" />
|
||||
<computesharp:AnimatedComputeShaderPanel ShaderRunner="{x:Bind ShaderRunner}" />
|
||||
|
||||
<!--
|
||||
Note: here we have a selection property to work around {x:Bind} not working correctly outside of the
|
||||
@@ -151,7 +151,7 @@
|
||||
</Grid.Resources>
|
||||
|
||||
<!-- Shader -->
|
||||
<computesharp:ComputeShaderPanel
|
||||
<computesharp:AnimatedComputeShaderPanel
|
||||
x:Name="ShaderPanel"
|
||||
Grid.Row="1"
|
||||
IsDynamicResolutionEnabled="{x:Bind ViewModel.IsDynamicResolutionEnabled, Mode=OneWay}"
|
||||
|
||||
@@ -9,10 +9,15 @@
|
||||
<Import_RootNamespace>ComputeSharp.UI</Import_RootNamespace>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Controls\AnimatedComputeShaderPanel.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Controls\AnimatedComputeShaderPanel.Properties.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Controls\ComputeShaderPanel.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Controls\ComputeShaderPanel.Properties.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Controls\ComputeShaderPanel.Rendering.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Extensions\SemaphoreSlimExtensions.cs" />
|
||||
<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>
|
||||
|
||||
@@ -0,0 +1,172 @@
|
||||
using System;
|
||||
#if WINDOWS_UWP
|
||||
using Windows.UI.Xaml;
|
||||
#else
|
||||
using Microsoft.UI.Xaml;
|
||||
#endif
|
||||
using Windows.Foundation;
|
||||
|
||||
#if WINDOWS_UWP
|
||||
namespace ComputeSharp.Uwp;
|
||||
#else
|
||||
namespace ComputeSharp.WinUI;
|
||||
#endif
|
||||
|
||||
/// <inheritdoc cref="AnimatedComputeShaderPanel"/>
|
||||
partial class AnimatedComputeShaderPanel
|
||||
{
|
||||
/// <summary>
|
||||
/// Raised whenever rendering starts.
|
||||
/// </summary>
|
||||
public event TypedEventHandler<AnimatedComputeShaderPanel, EventArgs>? RenderingStarted
|
||||
{
|
||||
add => this.swapChainManager.RenderingStarted += value;
|
||||
remove => this.swapChainManager.RenderingStarted -= value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Raised whenever rendering stops.
|
||||
/// </summary>
|
||||
public event TypedEventHandler<AnimatedComputeShaderPanel, EventArgs>? RenderingStopped
|
||||
{
|
||||
add => this.swapChainManager.RenderingStopped += value;
|
||||
remove => this.swapChainManager.RenderingStopped -= value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Raised whenever rendering fails.
|
||||
/// </summary>
|
||||
public event TypedEventHandler<AnimatedComputeShaderPanel, Exception>? RenderingFailed
|
||||
{
|
||||
add => this.swapChainManager.RenderingFailed += value;
|
||||
remove => this.swapChainManager.RenderingFailed -= value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the <see cref="IShaderRunner"/> instance to use to render content.
|
||||
/// </summary>
|
||||
public IShaderRunner? ShaderRunner
|
||||
{
|
||||
get => (IShaderRunner?)GetValue(ShaderRunnerProperty);
|
||||
set => SetValue(ShaderRunnerProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="DependencyProperty"/> backing <see cref="ShaderRunner"/>.
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty ShaderRunnerProperty = DependencyProperty.Register(
|
||||
nameof(ShaderRunner),
|
||||
typeof(IShaderRunner),
|
||||
typeof(AnimatedComputeShaderPanel),
|
||||
new PropertyMetadata(null, OnShaderRunnerPropertyChanged));
|
||||
|
||||
/// <inheritdoc cref="DependencyPropertyChangedCallback"/>
|
||||
private static void OnShaderRunnerPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var @this = (AnimatedComputeShaderPanel)d;
|
||||
|
||||
if (@this.IsLoaded &&
|
||||
!@this.IsPaused &&
|
||||
e.NewValue is IShaderRunner shaderRunner)
|
||||
{
|
||||
@this.swapChainManager.StartRenderLoop(null, shaderRunner);
|
||||
}
|
||||
else
|
||||
{
|
||||
@this.swapChainManager.StopRenderLoop();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the resolution scale to be used to render frames.
|
||||
/// This can help achieve higher framerates with more complex shaders.
|
||||
/// </summary>
|
||||
/// <remarks>The accepted range is [0.1 and 1] (default is 1, exceeding values will be clamped).</remarks>
|
||||
public double ResolutionScale
|
||||
{
|
||||
get => (double)GetValue(ResolutionScaleProperty);
|
||||
set => SetValue(ResolutionScaleProperty, Math.Clamp(value, 0.1, 1.0));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="DependencyProperty"/> backing <see cref="ResolutionScale"/>.
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty ResolutionScaleProperty = DependencyProperty.Register(
|
||||
nameof(ResolutionScale),
|
||||
typeof(double),
|
||||
typeof(AnimatedComputeShaderPanel),
|
||||
new PropertyMetadata(1.0, OnResolutionScalePropertyChanged));
|
||||
|
||||
/// <inheritdoc cref="DependencyPropertyChangedCallback"/>
|
||||
private static void OnResolutionScalePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var @this = (AnimatedComputeShaderPanel)d;
|
||||
var resolutionScale = (double)e.NewValue;
|
||||
|
||||
@this.swapChainManager.QueueResolutionScaleChange(resolutionScale);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets whether dynamic resolution is enabled. If it is, the internal render
|
||||
/// resolution of the shader being run will be automatically adjusted to reach 60fps.
|
||||
/// <para>The default value for this property is <see langword="true"/>.</para>
|
||||
/// </summary>
|
||||
public bool IsDynamicResolutionEnabled
|
||||
{
|
||||
get => (bool)GetValue(IsDynamicResolutionEnabledProperty);
|
||||
set => SetValue(IsDynamicResolutionEnabledProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="DependencyProperty"/> backing <see cref="IsDynamicResolutionEnabled"/>.
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty IsDynamicResolutionEnabledProperty = DependencyProperty.Register(
|
||||
nameof(IsDynamicResolutionEnabled),
|
||||
typeof(bool),
|
||||
typeof(AnimatedComputeShaderPanel),
|
||||
new PropertyMetadata(true, OnIsDynamicResolutionEnabledPropertyChanged));
|
||||
|
||||
/// <inheritdoc cref="DependencyPropertyChangedCallback"/>
|
||||
private static void OnIsDynamicResolutionEnabledPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var @this = (AnimatedComputeShaderPanel)d;
|
||||
var isDynamicResolutionEnabled = (bool)e.NewValue;
|
||||
|
||||
@this.swapChainManager.QueueDynamicResolutionModeChange(isDynamicResolutionEnabled);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets whether or not the rendering is paused.
|
||||
/// </summary>
|
||||
public bool IsPaused
|
||||
{
|
||||
get => (bool)GetValue(IsPausedProperty);
|
||||
set => SetValue(IsPausedProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="DependencyProperty"/> backing <see cref="IsPaused"/>.
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty IsPausedProperty = DependencyProperty.Register(
|
||||
nameof(IsPaused),
|
||||
typeof(bool),
|
||||
typeof(AnimatedComputeShaderPanel),
|
||||
new PropertyMetadata(false, OnIsPausedPropertyChanged));
|
||||
|
||||
/// <inheritdoc cref="DependencyPropertyChangedCallback"/>
|
||||
private static void OnIsPausedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var @this = (AnimatedComputeShaderPanel)d;
|
||||
|
||||
if (@this.IsLoaded &&
|
||||
(bool)e.NewValue is false &&
|
||||
@this.ShaderRunner is IShaderRunner shaderRunner)
|
||||
{
|
||||
@this.swapChainManager.StartRenderLoop(null, shaderRunner);
|
||||
}
|
||||
else
|
||||
{
|
||||
@this.swapChainManager.StopRenderLoop();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
#if WINDOWS_UWP
|
||||
using ComputeSharp.Uwp.Helpers;
|
||||
using Windows.UI.Xaml;
|
||||
using Windows.UI.Xaml.Controls;
|
||||
#else
|
||||
using ComputeSharp.WinUI.Helpers;
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
#endif
|
||||
|
||||
#if WINDOWS_UWP
|
||||
namespace ComputeSharp.Uwp;
|
||||
#else
|
||||
namespace ComputeSharp.WinUI;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// A custom <see cref="SwapChainPanel"/> that can be used to render animated backgrounds.
|
||||
/// It is powered by ComputeSharp and leverages compute shaders to create frames to display.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// This panel will always render new frames at the current display refresh rate, automatically.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public sealed partial class AnimatedComputeShaderPanel : SwapChainPanel
|
||||
{
|
||||
/// <summary>
|
||||
/// The <see cref="SwapChainManager{TOwner}"/> instance handling rendering.
|
||||
/// </summary>
|
||||
private readonly SwapChainManager<AnimatedComputeShaderPanel> swapChainManager;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="AnimatedComputeShaderPanel"/> instance.
|
||||
/// </summary>
|
||||
public unsafe AnimatedComputeShaderPanel()
|
||||
{
|
||||
this.swapChainManager = new SwapChainManager<AnimatedComputeShaderPanel>(this);
|
||||
|
||||
this.Loaded += AnimatedComputeShaderPanel_Loaded;
|
||||
this.Unloaded += AnimatedComputeShaderPanel_Unloaded;
|
||||
this.SizeChanged += AnimatedComputeShaderPanel_SizeChanged;
|
||||
this.CompositionScaleChanged += AnimatedComputeShaderPanel_CompositionScaleChanged;
|
||||
}
|
||||
|
||||
// Initializes the swap chain and starts the render thread
|
||||
private void AnimatedComputeShaderPanel_Loaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
this.swapChainManager.QueueResize(ActualWidth, ActualHeight);
|
||||
this.swapChainManager.QueueCompositionScaleChange(CompositionScaleX, CompositionScaleY);
|
||||
this.swapChainManager.QueueResolutionScaleChange(ResolutionScale);
|
||||
this.swapChainManager.QueueDynamicResolutionModeChange(IsDynamicResolutionEnabled);
|
||||
|
||||
if (!IsPaused &&
|
||||
ShaderRunner is IShaderRunner shaderRunner)
|
||||
{
|
||||
this.swapChainManager.StartRenderLoop(null, shaderRunner);
|
||||
}
|
||||
}
|
||||
|
||||
// Requests a cancellation for the render thread
|
||||
private void AnimatedComputeShaderPanel_Unloaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
this.swapChainManager.StopRenderLoop();
|
||||
}
|
||||
|
||||
// Updates the background store for the frame size factors used by the render thread
|
||||
private void AnimatedComputeShaderPanel_SizeChanged(object sender, SizeChangedEventArgs e)
|
||||
{
|
||||
this.swapChainManager.QueueResize(e.NewSize.Width, e.NewSize.Height);
|
||||
}
|
||||
|
||||
// Updates the background store for the composition scale factors used by the render thread
|
||||
private void AnimatedComputeShaderPanel_CompositionScaleChanged(SwapChainPanel sender, object args)
|
||||
{
|
||||
this.swapChainManager.QueueCompositionScaleChange(CompositionScaleX, CompositionScaleY);
|
||||
}
|
||||
}
|
||||
@@ -1,34 +1,116 @@
|
||||
using System;
|
||||
#if WINDOWS_UWP
|
||||
using Windows.UI.Xaml;
|
||||
#else
|
||||
using Microsoft.UI.Xaml;
|
||||
#endif
|
||||
using Windows.Foundation;
|
||||
|
||||
#if WINDOWS_UWP
|
||||
namespace ComputeSharp.Uwp;
|
||||
#else
|
||||
|
||||
using Microsoft.UI.Xaml;
|
||||
namespace ComputeSharp.WinUI;
|
||||
#endif
|
||||
|
||||
/// <inheritdoc cref="ComputeShaderPanel"/>
|
||||
public sealed unsafe partial class ComputeShaderPanel
|
||||
partial class ComputeShaderPanel
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the <see cref="IShaderRunner"/> instance to use to render content.
|
||||
/// Raised whenever rendering starts.
|
||||
/// </summary>
|
||||
public IShaderRunner? ShaderRunner
|
||||
public event TypedEventHandler<ComputeShaderPanel, EventArgs>? RenderingStarted
|
||||
{
|
||||
get => (IShaderRunner?)GetValue(ShaderFactoryProperty);
|
||||
set => SetValue(ShaderFactoryProperty, value);
|
||||
add => this.swapChainManager.RenderingStarted += value;
|
||||
remove => this.swapChainManager.RenderingStarted -= value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Raised whenever rendering stops.
|
||||
/// </summary>
|
||||
public event TypedEventHandler<ComputeShaderPanel, EventArgs>? RenderingStopped
|
||||
{
|
||||
add => this.swapChainManager.RenderingStopped += value;
|
||||
remove => this.swapChainManager.RenderingStopped -= value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Raised whenever rendering fails.
|
||||
/// </summary>
|
||||
public event TypedEventHandler<ComputeShaderPanel, Exception>? RenderingFailed
|
||||
{
|
||||
add => this.swapChainManager.RenderingFailed += value;
|
||||
remove => this.swapChainManager.RenderingFailed -= value;
|
||||
}
|
||||
|
||||
/// <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 ShaderFactoryProperty = DependencyProperty.Register(
|
||||
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;
|
||||
|
||||
if (@this.IsLoaded &&
|
||||
e.NewValue is IFrameRequestQueue frameRequestQueue &&
|
||||
@this.ShaderRunner is IShaderRunner shaderRunner)
|
||||
{
|
||||
@this.swapChainManager.StartRenderLoop(frameRequestQueue, shaderRunner);
|
||||
}
|
||||
else
|
||||
{
|
||||
@this.swapChainManager.StopRenderLoop();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the <see cref="IShaderRunner"/> instance to use to render content.
|
||||
/// </summary>
|
||||
public IShaderRunner? ShaderRunner
|
||||
{
|
||||
get => (IShaderRunner?)GetValue(ShaderRunnerProperty);
|
||||
set => SetValue(ShaderRunnerProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="DependencyProperty"/> backing <see cref="ShaderRunner"/>.
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty ShaderRunnerProperty = DependencyProperty.Register(
|
||||
nameof(ShaderRunner),
|
||||
typeof(IShaderRunner),
|
||||
typeof(ComputeShaderPanel),
|
||||
new PropertyMetadata(null, static (d, e) => ((ComputeShaderPanel)d).shaderRunner = (IShaderRunner?)e.NewValue));
|
||||
new PropertyMetadata(null, OnShaderRunnerPropertyChanged));
|
||||
|
||||
/// <inheritdoc cref="DependencyPropertyChangedCallback"/>
|
||||
private static void OnShaderRunnerPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var @this = (ComputeShaderPanel)d;
|
||||
|
||||
if (@this.IsLoaded &&
|
||||
@this.FrameRequestQueue is IFrameRequestQueue frameRequestQueue &&
|
||||
e.NewValue is IShaderRunner shaderRunner)
|
||||
{
|
||||
@this.swapChainManager.StartRenderLoop(frameRequestQueue, shaderRunner);
|
||||
}
|
||||
else
|
||||
{
|
||||
@this.swapChainManager.StopRenderLoop();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the resolution scale to be used to render frames.
|
||||
@@ -56,12 +138,7 @@ public sealed unsafe partial class ComputeShaderPanel
|
||||
var @this = (ComputeShaderPanel)d;
|
||||
var resolutionScale = (double)e.NewValue;
|
||||
|
||||
@this.targetResolutionScale = (float)resolutionScale;
|
||||
|
||||
if (!@this.isDynamicResolutionEnabled)
|
||||
{
|
||||
@this.OnResize();
|
||||
}
|
||||
@this.swapChainManager.QueueResolutionScaleChange(resolutionScale);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -90,40 +167,6 @@ public sealed unsafe partial class ComputeShaderPanel
|
||||
var @this = (ComputeShaderPanel)d;
|
||||
var isDynamicResolutionEnabled = (bool)e.NewValue;
|
||||
|
||||
@this.isDynamicResolutionEnabled = isDynamicResolutionEnabled;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets whether or not the rendering is paused.
|
||||
/// </summary>
|
||||
public bool IsPaused
|
||||
{
|
||||
get => (bool)GetValue(IsPausedProperty);
|
||||
set => SetValue(IsPausedProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="DependencyProperty"/> backing <see cref="IsPaused"/>.
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty IsPausedProperty = DependencyProperty.Register(
|
||||
nameof(IsPaused),
|
||||
typeof(bool),
|
||||
typeof(ComputeShaderPanel),
|
||||
new PropertyMetadata(false, OnIsPausedPropertyChanged));
|
||||
|
||||
/// <inheritdoc cref="DependencyPropertyChangedCallback"/>
|
||||
private static void OnIsPausedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
var @this = (ComputeShaderPanel)d;
|
||||
var isPaused = (bool)e.NewValue;
|
||||
|
||||
if (isPaused)
|
||||
{
|
||||
@this.OnStopRenderLoop();
|
||||
}
|
||||
else
|
||||
{
|
||||
@this.OnStartRenderLoop();
|
||||
}
|
||||
@this.swapChainManager.QueueDynamicResolutionModeChange(isDynamicResolutionEnabled);
|
||||
}
|
||||
}
|
||||
@@ -1,76 +1,78 @@
|
||||
#if WINDOWS_UWP
|
||||
using ComputeSharp.Uwp.Helpers;
|
||||
using Windows.UI.Xaml;
|
||||
using Windows.UI.Xaml.Controls;
|
||||
|
||||
namespace ComputeSharp.Uwp;
|
||||
#else
|
||||
using ComputeSharp.WinUI.Helpers;
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
#endif
|
||||
|
||||
#if WINDOWS_UWP
|
||||
namespace ComputeSharp.Uwp;
|
||||
#else
|
||||
namespace ComputeSharp.WinUI;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// A custom <see cref="SwapChainPanel"/> that can be used to render animated backgrounds.
|
||||
/// It is powered by ComputeSharp and leverages compute shaders to create frames to display.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// This panel accepts a custom <see cref="IFrameRequestQueue"/> instance to customize frame requests.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public sealed unsafe partial class ComputeShaderPanel : SwapChainPanel
|
||||
public sealed partial class ComputeShaderPanel : SwapChainPanel
|
||||
{
|
||||
/// <summary>
|
||||
/// The <see cref="SwapChainManager{TOwner}"/> instance handling rendering.
|
||||
/// </summary>
|
||||
private readonly SwapChainManager<ComputeShaderPanel> swapChainManager;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ComputeShaderPanel"/> instance.
|
||||
/// </summary>
|
||||
public ComputeShaderPanel()
|
||||
public unsafe ComputeShaderPanel()
|
||||
{
|
||||
this.swapChainManager = new SwapChainManager<ComputeShaderPanel>(this);
|
||||
|
||||
this.Loaded += ComputeShaderPanel_Loaded;
|
||||
this.Unloaded += ComputeShaderPanel_Unloaded;
|
||||
this.SizeChanged += ComputeShaderPanel_SizeChanged;
|
||||
this.CompositionScaleChanged += ComputeShaderPanel_CompositionScaleChanged;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Finalizes the current <see cref="ComputeShaderPanel"/> instance and releases all resources.
|
||||
/// </summary>
|
||||
~ComputeShaderPanel()
|
||||
{
|
||||
OnDisposed();
|
||||
}
|
||||
|
||||
// Initializes the swap chain and starts the render thread
|
||||
private void ComputeShaderPanel_Loaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
this.width = (float)ActualWidth;
|
||||
this.height = (float)ActualHeight;
|
||||
this.compositionScaleX = CompositionScaleX;
|
||||
this.compositionScaleY = CompositionScaleY;
|
||||
this.resolutionScale = (float)ResolutionScale;
|
||||
this.targetResolutionScale = 1.0f;
|
||||
this.isDynamicResolutionEnabled = true;
|
||||
this.swapChainManager.QueueResize(ActualWidth, ActualHeight);
|
||||
this.swapChainManager.QueueCompositionScaleChange(CompositionScaleX, CompositionScaleY);
|
||||
this.swapChainManager.QueueResolutionScaleChange(ResolutionScale);
|
||||
this.swapChainManager.QueueDynamicResolutionModeChange(IsDynamicResolutionEnabled);
|
||||
|
||||
OnInitialize();
|
||||
OnStartRenderLoop();
|
||||
if (FrameRequestQueue is IFrameRequestQueue frameRequestQueue &&
|
||||
ShaderRunner is IShaderRunner shaderRunner)
|
||||
{
|
||||
this.swapChainManager.StartRenderLoop(frameRequestQueue, shaderRunner);
|
||||
}
|
||||
}
|
||||
|
||||
// Requests a cancellation for the render thread
|
||||
private void ComputeShaderPanel_Unloaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
OnStopRenderLoop();
|
||||
this.swapChainManager.StopRenderLoop();
|
||||
}
|
||||
|
||||
// Updates the background store for the frame size factors used by the render thread
|
||||
private void ComputeShaderPanel_SizeChanged(object sender, SizeChangedEventArgs e)
|
||||
{
|
||||
this.width = (float)e.NewSize.Width;
|
||||
this.height = (float)e.NewSize.Height;
|
||||
|
||||
OnResize();
|
||||
this.swapChainManager.QueueResize(e.NewSize.Width, e.NewSize.Height);
|
||||
}
|
||||
|
||||
// Updates the background store for the composition scale factors used by the render thread
|
||||
private void ComputeShaderPanel_CompositionScaleChanged(SwapChainPanel sender, object args)
|
||||
{
|
||||
this.compositionScaleX = CompositionScaleX;
|
||||
this.compositionScaleY = CompositionScaleY;
|
||||
|
||||
OnResize();
|
||||
this.swapChainManager.QueueCompositionScaleChange(CompositionScaleX, CompositionScaleY);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
#pragma warning disable CS0618
|
||||
|
||||
#if WINDOWS_UWP
|
||||
namespace ComputeSharp.Uwp.Extensions;
|
||||
#else
|
||||
namespace ComputeSharp.WinUI.Extensions;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// A helper type for <see cref="SemaphoreSlim"/>.
|
||||
/// </summary>
|
||||
internal static class SemaphoreSlimExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Acquires a lock for the current instance, that is automatically released outside the <see langword="using"/> block.
|
||||
/// </summary>
|
||||
public static async ValueTask<Lock> LockAsync(this SemaphoreSlim semaphore)
|
||||
{
|
||||
await semaphore.WaitAsync().ConfigureAwait(false);
|
||||
|
||||
return new(semaphore);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Private class that implements the automatic release of the semaphore
|
||||
/// </summary>
|
||||
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||
public readonly struct Lock : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// The underlying semaphore used by this instance.
|
||||
/// </summary>
|
||||
private readonly SemaphoreSlim semaphore;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="Lock"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="semaphore">The <see cref="SemaphoreSlim"/> instance to wrap.</param>
|
||||
[Obsolete("Use AsyncMutex.LockAsync instead.")]
|
||||
public Lock(SemaphoreSlim semaphore)
|
||||
{
|
||||
this.semaphore = semaphore;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Dispose()
|
||||
{
|
||||
this.semaphore.Release();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -118,10 +118,9 @@ internal unsafe ref struct DynamicResolutionManager
|
||||
|
||||
if (averageFrameTimeInTicks is < LowerFrameTimeThresholdInTicksFor60fps or > UpperFrameTimeThresholdInTicksFor60fps)
|
||||
{
|
||||
float
|
||||
frameTimeDelta = (TargetFrameTimeInTicksFor60fps - averageFrameTimeInTicks) / (float)averageFrameTimeInTicks,
|
||||
scaleFactorDelta = scaleFactor * frameTimeDelta * ScaleFactorDeltaK,
|
||||
updatedScaleFactor = Math.Clamp(scaleFactor + scaleFactorDelta, 0.10f, 1.0f);
|
||||
float frameTimeDelta = (TargetFrameTimeInTicksFor60fps - averageFrameTimeInTicks) / (float)averageFrameTimeInTicks;
|
||||
float scaleFactorDelta = scaleFactor * frameTimeDelta * ScaleFactorDeltaK;
|
||||
float updatedScaleFactor = Math.Clamp(scaleFactor + scaleFactorDelta, 0.10f, 1.0f);
|
||||
|
||||
// Apply the scale factor update if the target scale has changed enough. This helps to avoid
|
||||
// too frequence resolution changes if the scale factor only changes very little every time.
|
||||
|
||||
@@ -0,0 +1,360 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading;
|
||||
#if WINDOWS_UWP
|
||||
using ComputeSharp.Uwp.Extensions;
|
||||
#else
|
||||
using ComputeSharp.WinUI.Extensions;
|
||||
#endif
|
||||
using Microsoft.Toolkit.Diagnostics;
|
||||
|
||||
#pragma warning disable CS0420
|
||||
|
||||
#if WINDOWS_UWP
|
||||
namespace ComputeSharp.Uwp.Helpers;
|
||||
#else
|
||||
namespace ComputeSharp.WinUI.Helpers;
|
||||
#endif
|
||||
|
||||
/// <inheritdoc/>
|
||||
partial class SwapChainManager<TOwner>
|
||||
{
|
||||
/// <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 async void StartRenderLoop(IFrameRequestQueue? frameRequestQueue, IShaderRunner shaderRunner)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
|
||||
Guard.IsNotNull(shaderRunner, nameof(shaderRunner));
|
||||
|
||||
using (await this.setupSemaphore.LockAsync())
|
||||
{
|
||||
this.renderCancellationTokenSource?.Cancel();
|
||||
|
||||
await this.renderSemaphore.WaitAsync();
|
||||
|
||||
Thread newRenderThread = new(static args => ((SwapChainManager<TOwner>)args!).SwitchAndStartRenderLoop());
|
||||
|
||||
this.frameRequestQueue = frameRequestQueue;
|
||||
this.shaderRunner = shaderRunner;
|
||||
this.renderCancellationTokenSource = new CancellationTokenSource();
|
||||
this.renderThread = newRenderThread;
|
||||
this.renderSemaphore = new SemaphoreSlim(0, 1);
|
||||
|
||||
newRenderThread.Start(this);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stops the current render loop, if one is running.
|
||||
/// </summary>
|
||||
public async void StopRenderLoop()
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
|
||||
using (await this.setupSemaphore.LockAsync())
|
||||
{
|
||||
this.renderCancellationTokenSource?.Cancel();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Queues a change in the dynamic resolution mode.
|
||||
/// </summary>
|
||||
/// <param name="isDynamicResolutionEnabled">Whether or not to use dynamic resolution.</param>
|
||||
public async void QueueDynamicResolutionModeChange(bool isDynamicResolutionEnabled)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
|
||||
using (await this.setupSemaphore.LockAsync())
|
||||
{
|
||||
// If there is a render thread currently running, stop it and restart it
|
||||
if (this.renderCancellationTokenSource?.IsCancellationRequested == false)
|
||||
{
|
||||
this.renderCancellationTokenSource?.Cancel();
|
||||
|
||||
await this.renderSemaphore.WaitAsync();
|
||||
|
||||
Thread newRenderThread = new(static args => ((SwapChainManager<TOwner>)args!).SwitchAndStartRenderLoop());
|
||||
|
||||
this.renderCancellationTokenSource = new CancellationTokenSource();
|
||||
this.renderThread = newRenderThread;
|
||||
this.renderSemaphore = new SemaphoreSlim(0, 1);
|
||||
this.isDynamicResolutionEnabled = isDynamicResolutionEnabled;
|
||||
|
||||
newRenderThread.Start(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.isDynamicResolutionEnabled = isDynamicResolutionEnabled;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Queues a resize operation.
|
||||
/// </summary>
|
||||
/// <param name="width">The width of the render resolution.</param>
|
||||
/// <param name="height">The height of the render resolution.</param>
|
||||
public void QueueResize(double width, double height)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
|
||||
this.width = (float)width;
|
||||
this.height = (float)height;
|
||||
|
||||
this.isResizePending = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Queues a change in the composition scale factors.
|
||||
/// </summary>
|
||||
/// <param name="compositionScaleX">The composition scale on the X axis.</param>
|
||||
/// <param name="compositionScaleY">The composition scale on the Y axis</param>
|
||||
public void QueueCompositionScaleChange(double compositionScaleX, double compositionScaleY)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
|
||||
this.compositionScaleX = (float)compositionScaleX;
|
||||
this.compositionScaleY = (float)compositionScaleY;
|
||||
|
||||
this.isResizePending = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Queues a change in the resolution scale factor.
|
||||
/// </summary>
|
||||
/// <param name="resolutionScale">The resolution scale factor to use.</param>
|
||||
public void QueueResolutionScaleChange(double resolutionScale)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
|
||||
this.resolutionScale = (float)resolutionScale;
|
||||
|
||||
this.isResizePending = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Selects the right render loop to start.
|
||||
/// </summary>
|
||||
private void SwitchAndStartRenderLoop()
|
||||
{
|
||||
try
|
||||
{
|
||||
OnRenderingStarted();
|
||||
|
||||
if (this.isDynamicResolutionEnabled)
|
||||
{
|
||||
this.targetResolutionScale = 1.0f;
|
||||
|
||||
RenderLoopWithDynamicResolution();
|
||||
}
|
||||
else
|
||||
{
|
||||
this.targetResolutionScale = this.resolutionScale;
|
||||
|
||||
RenderLoop();
|
||||
}
|
||||
|
||||
OnRenderingStopped();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
OnRenderingFailed(e);
|
||||
}
|
||||
finally
|
||||
{
|
||||
this.renderSemaphore.Release();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The core render loop.
|
||||
/// </summary>
|
||||
private void RenderLoop()
|
||||
{
|
||||
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.
|
||||
OnResize();
|
||||
OnUpdate(renderStopwatch.Elapsed, parameter);
|
||||
OnPresent();
|
||||
|
||||
renderStopwatch.Start();
|
||||
|
||||
// Main render loop, until cancellation is requested
|
||||
while (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
if (!OnFrameRequest(out parameter, cancellationToken))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
OnResize();
|
||||
OnUpdate(renderStopwatch.Elapsed, parameter);
|
||||
OnPresent();
|
||||
}
|
||||
|
||||
renderStopwatch.Stop();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The core render loop with dynamic resolution.
|
||||
/// </summary>
|
||||
private void RenderLoopWithDynamicResolution()
|
||||
{
|
||||
Stopwatch renderStopwatch = this.renderStopwatch ??= new();
|
||||
CancellationToken cancellationToken = this.renderCancellationTokenSource!.Token;
|
||||
|
||||
DynamicResolutionManager.Create(out DynamicResolutionManager frameTimeWatcher);
|
||||
|
||||
if (!OnFrameRequest(out object? parameter, cancellationToken))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Stopwatch frameStopwatch = Stopwatch.StartNew();
|
||||
|
||||
OnResize();
|
||||
OnUpdate(renderStopwatch.Elapsed, parameter);
|
||||
OnPresent();
|
||||
|
||||
renderStopwatch.Start();
|
||||
|
||||
while (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
// Evaluate the dynamic resolution frame time step, if the mode is enabled
|
||||
if (frameTimeWatcher.Advance(frameStopwatch.ElapsedTicks, ref this.targetResolutionScale))
|
||||
{
|
||||
this.isResizePending = true;
|
||||
}
|
||||
|
||||
if (!OnFrameRequest(out parameter, cancellationToken))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
frameStopwatch.Restart();
|
||||
|
||||
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>
|
||||
private void OnResize()
|
||||
{
|
||||
if (this.isResizePending)
|
||||
{
|
||||
ApplyResize();
|
||||
|
||||
this.isResizePending = false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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>
|
||||
/// <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, parameter);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Presents the last rendered frame for the current application.
|
||||
/// </summary>
|
||||
private unsafe partial void OnPresent();
|
||||
|
||||
/// <summary>
|
||||
/// Raises <see cref="RenderingStarted"/>.
|
||||
/// </summary>
|
||||
private void OnRenderingStarted()
|
||||
{
|
||||
_ = this.dispatcherQueue.TryEnqueue(() => RenderingStarted?.Invoke(this.owner, EventArgs.Empty));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Raises <see cref="RenderingStopped"/>.
|
||||
/// </summary>
|
||||
private void OnRenderingStopped()
|
||||
{
|
||||
_ = this.dispatcherQueue.TryEnqueue(() => RenderingStopped?.Invoke(this.owner, EventArgs.Empty));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Raises <see cref="RenderingFailed"/>.
|
||||
/// </summary>
|
||||
/// <param name="e">The <see cref="Exception"/> being thrown that caused rendering to stop.</param>
|
||||
private void OnRenderingFailed(Exception e)
|
||||
{
|
||||
_ = this.dispatcherQueue.TryEnqueue(() => RenderingFailed?.Invoke(this.owner, e));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stops the current render loop, if one is running, and waits for it.
|
||||
/// </summary>
|
||||
/// <remarks>This method doesn't check for disposal.</remarks>
|
||||
private void UnsafeStopRenderLoopAndWait()
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
|
||||
this.setupSemaphore.Wait();
|
||||
|
||||
this.renderCancellationTokenSource?.Cancel();
|
||||
this.renderSemaphore.Wait();
|
||||
}
|
||||
}
|
||||
+123
-196
@@ -1,45 +1,54 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
#if WINDOWS_UWP
|
||||
using System.Runtime.InteropServices;
|
||||
#endif
|
||||
using System.Threading;
|
||||
using ComputeSharp.Core.Extensions;
|
||||
using ComputeSharp.Graphics.Helpers;
|
||||
using ComputeSharp.Interop;
|
||||
#if WINDOWS_UWP
|
||||
using ComputeSharp.Uwp.Helpers;
|
||||
#else
|
||||
using ComputeSharp.WinUI.Helpers;
|
||||
#if !WINDOWS_UWP
|
||||
using Microsoft.UI.Dispatching;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
#endif
|
||||
using TerraFX.Interop.DirectX;
|
||||
using TerraFX.Interop.Windows;
|
||||
using TerraFX.Interop.WinRT;
|
||||
#if !WINDOWS_UWP
|
||||
#if WINDOWS_UWP
|
||||
using System.Runtime.InteropServices;
|
||||
using Windows.Foundation;
|
||||
using Windows.UI.Xaml.Controls;
|
||||
using Windows.System;
|
||||
#else
|
||||
using Windows.Foundation;
|
||||
using WinRT;
|
||||
#endif
|
||||
using Win32 = TerraFX.Interop.Windows.Windows;
|
||||
|
||||
#pragma warning disable CS0420
|
||||
|
||||
#if WINDOWS_UWP
|
||||
namespace ComputeSharp.Uwp;
|
||||
namespace ComputeSharp.Uwp.Helpers;
|
||||
#else
|
||||
namespace ComputeSharp.WinUI;
|
||||
namespace ComputeSharp.WinUI.Helpers;
|
||||
#endif
|
||||
|
||||
/// <inheritdoc cref="ComputeShaderPanel"/>
|
||||
public sealed partial class ComputeShaderPanel
|
||||
/// <summary>
|
||||
/// A type managing rendering on a target swap chain object.
|
||||
/// </summary>
|
||||
/// <typeparam name="TOwner">The type of the owner <see cref="SwapChainPanel"/> instance.</typeparam>
|
||||
internal sealed unsafe partial class SwapChainManager<TOwner> : NativeObject
|
||||
where TOwner : SwapChainPanel
|
||||
{
|
||||
/// <summary>
|
||||
/// The render thread in use, if any.
|
||||
/// The owning <typeparamref name="TOwner"/> instance.
|
||||
/// </summary>
|
||||
private Thread? renderThread;
|
||||
private readonly TOwner owner;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="IShaderRunner"/> instance used to create shaders to run.
|
||||
/// The captured <see cref="SynchronizationContext"/> for the current instance.
|
||||
/// </summary>
|
||||
private IShaderRunner? shaderRunner;
|
||||
private readonly DispatcherQueue dispatcherQueue = DispatcherQueue.GetForCurrentThread();
|
||||
|
||||
/// <summary>
|
||||
/// An <see cref="SemaphoreSlim"/> object for the render loop management.
|
||||
/// </summary>
|
||||
private readonly SemaphoreSlim setupSemaphore = new(1, 1);
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="ID3D12Device"/> pointer for the device currently in use.
|
||||
@@ -59,7 +68,7 @@ public sealed partial class ComputeShaderPanel
|
||||
/// <summary>
|
||||
/// The next fence value for graphics operations using <see cref="d3D12CommandQueue"/>.
|
||||
/// </summary>
|
||||
private ulong nextD3D12FenceValue = 1;
|
||||
private ulong nextD3D12FenceValue;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="ID3D12CommandAllocator"/> object to create command lists.
|
||||
@@ -94,12 +103,37 @@ public sealed partial class ComputeShaderPanel
|
||||
/// <summary>
|
||||
/// The index of the next buffer that can be used to present content.
|
||||
/// </summary>
|
||||
private uint currentBufferIndex;
|
||||
private volatile uint currentBufferIndex;
|
||||
|
||||
/// <summary>
|
||||
/// The render thread in use, if any.
|
||||
/// </summary>
|
||||
private volatile Thread? renderThread;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="SemaphoreSlim"/> used to wait for render threads to complete.
|
||||
/// </summary>
|
||||
private volatile SemaphoreSlim renderSemaphore = new(1, 1);
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="CancellationTokenSource"/> to use for <see cref="renderThread"/>.
|
||||
/// </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>
|
||||
private volatile IShaderRunner? shaderRunner;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="ReadWriteTexture2D{T, TPixel}"/> instance used to prepare frames to display.
|
||||
/// </summary>
|
||||
private ReadWriteTexture2D<Rgba32, Float4>? texture;
|
||||
private volatile ReadWriteTexture2D<Rgba32, Float4>? texture;
|
||||
|
||||
/// <summary>
|
||||
/// Whether or not the window has been resized and requires the buffers to be updated.
|
||||
@@ -141,37 +175,57 @@ public sealed partial class ComputeShaderPanel
|
||||
/// </summary>
|
||||
private volatile bool isDynamicResolutionEnabled;
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether or not the rendering has been canceled.
|
||||
/// </summary>
|
||||
private volatile bool isCancellationRequested;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="Stopwatch"/> instance tracking time since the first rendered frame.
|
||||
/// </summary>
|
||||
private Stopwatch? renderStopwatch;
|
||||
private volatile Stopwatch? renderStopwatch;
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether or not <see cref="OnInitialize"/> has already been called.
|
||||
/// Raised whenever rendering starts.
|
||||
/// </summary>
|
||||
private bool isInitialized;
|
||||
public event TypedEventHandler<TOwner, EventArgs>? RenderingStarted;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the current application.
|
||||
/// Raised whenever rendering stops.
|
||||
/// </summary>
|
||||
private unsafe void OnInitialize()
|
||||
public event TypedEventHandler<TOwner, EventArgs>? RenderingStopped;
|
||||
|
||||
/// <summary>
|
||||
/// Raised whenever rendering fails.
|
||||
/// </summary>
|
||||
public event TypedEventHandler<TOwner, Exception>? RenderingFailed;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SwapChainManager{TOwner}"/> type.
|
||||
/// </summary>
|
||||
/// <param name="owner">The input swap chain instance being used.</param>
|
||||
public SwapChainManager(TOwner owner)
|
||||
{
|
||||
if (this.isInitialized)
|
||||
{
|
||||
return;
|
||||
}
|
||||
this.owner = owner;
|
||||
|
||||
this.isInitialized = true;
|
||||
// Extract the ISwapChainPanelNative reference from the current panel, then query the
|
||||
// IDXGISwapChain reference just created and set that as the swap chain panel to use.
|
||||
using ComPtr<ISwapChainPanelNative> swapChainPanelNative = default;
|
||||
|
||||
#if WINDOWS_UWP
|
||||
IUnknown* swapChainPanel = (IUnknown*)Marshal.GetIUnknownForObject(owner);
|
||||
|
||||
swapChainPanel->QueryInterface(
|
||||
Win32.__uuidof<ISwapChainPanelNative>(),
|
||||
(void**)&swapChainPanelNative).Assert();
|
||||
#else
|
||||
IUnknown* swapChainPanel = (IUnknown*)((IWinRTObject)owner).NativeObject.ThisPtr;
|
||||
Guid iSwapChainPanelNativeUuid = new(0x63AAD0B8, 0x7C24, 0x40FF, 0x85, 0xA8, 0x64, 0x0D, 0x94, 0x4C, 0xC3, 0x25);
|
||||
|
||||
swapChainPanel->QueryInterface(
|
||||
&iSwapChainPanelNativeUuid,
|
||||
(void**)&swapChainPanelNative).Assert();
|
||||
#endif
|
||||
|
||||
// Get the underlying ID3D12Device in use
|
||||
fixed (ID3D12Device** d3D12Device = this.d3D12Device)
|
||||
{
|
||||
InteropServices.TryGetID3D12Device(GraphicsDevice.Default, Win32.__uuidof<ID3D12Device>(), (void**)d3D12Device).Assert();
|
||||
GraphicsDevice.Default.D3D12Device->QueryInterface(Win32.__uuidof<ID3D12Device>(), (void**)d3D12Device).Assert();
|
||||
}
|
||||
|
||||
// Create the direct command queue to use
|
||||
@@ -258,25 +312,6 @@ public sealed partial class ComputeShaderPanel
|
||||
// Close the command list to prepare it for future use
|
||||
this.d3D12GraphicsCommandList.Get()->Close().Assert();
|
||||
|
||||
// Extract the ISwapChainPanelNative reference from the current panel, then query the
|
||||
// IDXGISwapChain reference just created and set that as the swap chain panel to use.
|
||||
using ComPtr<ISwapChainPanelNative> swapChainPanelNative = default;
|
||||
|
||||
#if WINDOWS_UWP
|
||||
IUnknown* swapChainPanel = (IUnknown*)Marshal.GetIUnknownForObject(this);
|
||||
|
||||
swapChainPanel->QueryInterface(
|
||||
Win32.__uuidof<ISwapChainPanelNative>(),
|
||||
(void**)&swapChainPanelNative).Assert();
|
||||
#else
|
||||
IUnknown* swapChainPanel = (IUnknown*)((IWinRTObject)this).NativeObject.ThisPtr;
|
||||
Guid iSwapChainPanelNativeUuid = new(0x63AAD0B8, 0x7C24, 0x40FF, 0x85, 0xA8, 0x64, 0x0D, 0x94, 0x4C, 0xC3, 0x25);
|
||||
|
||||
swapChainPanel->QueryInterface(
|
||||
&iSwapChainPanelNativeUuid,
|
||||
(void**)&swapChainPanelNative).Assert();
|
||||
#endif
|
||||
|
||||
using (ComPtr<IDXGISwapChain> idxgiSwapChain = default)
|
||||
{
|
||||
this.dxgiSwapChain3.CopyTo(&idxgiSwapChain).Assert();
|
||||
@@ -287,25 +322,17 @@ public sealed partial class ComputeShaderPanel
|
||||
GC.KeepAlive(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resizes the current application.
|
||||
/// </summary>
|
||||
private void OnResize()
|
||||
{
|
||||
this.isResizePending = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Applies the actual resize logic that was scheduled from <see cref="OnResize"/>.
|
||||
/// </summary>
|
||||
private unsafe void ApplyResize()
|
||||
{
|
||||
this.d3D12CommandQueue.Get()->Signal(this.d3D12Fence.Get(), this.nextD3D12FenceValue).Assert();
|
||||
ulong updatedFenceValue = ++this.nextD3D12FenceValue;
|
||||
|
||||
this.d3D12CommandQueue.Get()->Signal(this.d3D12Fence.Get(), updatedFenceValue).Assert();
|
||||
|
||||
// Wait for the fence again to ensure there are no pending operations
|
||||
this.d3D12Fence.Get()->SetEventOnCompletion(this.nextD3D12FenceValue, default).Assert();
|
||||
|
||||
this.nextD3D12FenceValue++;
|
||||
this.d3D12Fence.Get()->SetEventOnCompletion(updatedFenceValue, default).Assert();
|
||||
|
||||
// Dispose the old buffers before resizing the buffer
|
||||
this.d3D12Resource0.Dispose();
|
||||
@@ -347,40 +374,16 @@ public sealed partial class ComputeShaderPanel
|
||||
(int)d3D12Resource0Description.Height);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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 unsafe void OnUpdate(TimeSpan time)
|
||||
/// <inheritdoc/>
|
||||
private unsafe partial void OnPresent()
|
||||
{
|
||||
if (this.isResizePending)
|
||||
{
|
||||
ApplyResize();
|
||||
|
||||
this.isResizePending = false;
|
||||
}
|
||||
|
||||
// Skip if no factory is available
|
||||
if (this.shaderRunner is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Generate the new frame
|
||||
this.shaderRunner.Execute(this.texture!, time);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Presents the last rendered frame for the current application.
|
||||
/// </summary>
|
||||
private unsafe void OnPresent()
|
||||
{
|
||||
_ = Win32.WaitForSingleObjectEx(frameLatencyWaitableObject, 1000, 1);
|
||||
// Wait for the swap chain to be ready for presenting
|
||||
_ = Win32.WaitForSingleObjectEx(frameLatencyWaitableObject, 1000, true);
|
||||
|
||||
using ComPtr<ID3D12Resource> d3D12Resource = default;
|
||||
|
||||
// Get the underlying ID3D12Resource pointer for the texture
|
||||
InteropServices.TryGetID3D12Resource(this.texture!, Win32.__uuidof<ID3D12Resource>(), (void**)d3D12Resource.GetAddressOf()).Assert();
|
||||
this.texture!.D3D12Resource->QueryInterface(Win32.__uuidof<ID3D12Resource>(), (void**)d3D12Resource.GetAddressOf()).Assert();
|
||||
|
||||
// Get the target back buffer to update
|
||||
ID3D12Resource* d3D12ResourceBackBuffer = this.currentBufferIndex switch
|
||||
@@ -431,116 +434,40 @@ public sealed partial class ComputeShaderPanel
|
||||
|
||||
// Execute the command list to perform the copy
|
||||
this.d3D12CommandQueue.Get()->ExecuteCommandLists(1, (ID3D12CommandList**)this.d3D12GraphicsCommandList.GetAddressOf());
|
||||
this.d3D12CommandQueue.Get()->Signal(this.d3D12Fence.Get(), this.nextD3D12FenceValue).Assert();
|
||||
|
||||
// Increment the fence value and signal the fence. Just like with normal dispatches,
|
||||
// the increment must be done after executing the command list and before signaling.
|
||||
ulong updatedFenceValue = ++this.nextD3D12FenceValue;
|
||||
|
||||
this.d3D12CommandQueue.Get()->Signal(this.d3D12Fence.Get(), updatedFenceValue).Assert();
|
||||
|
||||
// Present the new frame
|
||||
this.dxgiSwapChain3.Get()->Present(0, 0).Assert();
|
||||
|
||||
if (this.nextD3D12FenceValue > this.d3D12Fence.Get()->GetCompletedValue())
|
||||
if (updatedFenceValue > this.d3D12Fence.Get()->GetCompletedValue())
|
||||
{
|
||||
this.d3D12Fence.Get()->SetEventOnCompletion(this.nextD3D12FenceValue, default).Assert();
|
||||
this.d3D12Fence.Get()->SetEventOnCompletion(updatedFenceValue, default).Assert();
|
||||
}
|
||||
|
||||
this.nextD3D12FenceValue++;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes the render loop for the current panel.
|
||||
/// </summary>
|
||||
/// <remarks>This method assumes to be invoked from a background thread.</remarks>
|
||||
private void OnStartRenderLoop()
|
||||
/// <inheritdoc/>
|
||||
protected override void OnDispose()
|
||||
{
|
||||
static void ExecuteRenderLoop(ComputeShaderPanel @this)
|
||||
ThreadPool.UnsafeQueueUserWorkItem(static state =>
|
||||
{
|
||||
Stopwatch
|
||||
renderStopwatch = @this.renderStopwatch ??= new(),
|
||||
frameStopwatch = Stopwatch.StartNew();
|
||||
SwapChainManager<TOwner> @this = (SwapChainManager<TOwner>)state!;
|
||||
|
||||
DynamicResolutionManager.Create(out DynamicResolutionManager frameTimeWatcher);
|
||||
@this.UnsafeStopRenderLoopAndWait();
|
||||
|
||||
bool isDynamicResolutionEnabled = @this.isDynamicResolutionEnabled;
|
||||
|
||||
// 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();
|
||||
|
||||
renderStopwatch.Start();
|
||||
|
||||
const long targetFrameTimeInTicksFor61fps = 163934;
|
||||
|
||||
// Main render loop, until cancellation is requested
|
||||
while (!@this.isCancellationRequested)
|
||||
{
|
||||
// Update the resolution mode, if needed
|
||||
if (isDynamicResolutionEnabled != @this.isDynamicResolutionEnabled)
|
||||
{
|
||||
isDynamicResolutionEnabled = !isDynamicResolutionEnabled;
|
||||
|
||||
if (isDynamicResolutionEnabled)
|
||||
{
|
||||
DynamicResolutionManager.Create(out frameTimeWatcher);
|
||||
}
|
||||
else
|
||||
{
|
||||
@this.targetResolutionScale = @this.resolutionScale;
|
||||
}
|
||||
}
|
||||
|
||||
// Evaluate the dynamic resolution frame time step, if the mode is enabled
|
||||
if (isDynamicResolutionEnabled &&
|
||||
frameTimeWatcher.Advance(frameStopwatch.ElapsedTicks, ref @this.targetResolutionScale))
|
||||
{
|
||||
@this.isResizePending = true;
|
||||
}
|
||||
|
||||
while (frameStopwatch.ElapsedTicks < targetFrameTimeInTicksFor61fps)
|
||||
{
|
||||
}
|
||||
|
||||
frameStopwatch.Restart();
|
||||
|
||||
@this.OnUpdate(renderStopwatch.Elapsed);
|
||||
@this.OnPresent();
|
||||
}
|
||||
|
||||
renderStopwatch.Stop();
|
||||
}
|
||||
|
||||
this.isCancellationRequested = false;
|
||||
this.renderThread = new Thread(static args => ExecuteRenderLoop((ComputeShaderPanel)args!));
|
||||
this.renderThread.Start(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stops the current render loop, if one is running.
|
||||
/// </summary>
|
||||
private void OnStopRenderLoop()
|
||||
{
|
||||
this.isCancellationRequested = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stops the current render loop, and releases all resources.
|
||||
/// </summary>
|
||||
private void OnDisposed()
|
||||
{
|
||||
this.isCancellationRequested = true;
|
||||
|
||||
// Ensure the rendering has stopped
|
||||
if (this.renderThread is Thread renderThread)
|
||||
{
|
||||
renderThread.Join();
|
||||
}
|
||||
|
||||
this.d3D12Device.Dispose();
|
||||
this.d3D12CommandQueue.Dispose();
|
||||
this.d3D12Fence.Dispose();
|
||||
this.d3D12CommandAllocator.Dispose();
|
||||
this.d3D12GraphicsCommandList.Dispose();
|
||||
this.dxgiSwapChain3.Dispose();
|
||||
this.d3D12Resource0.Dispose();
|
||||
this.d3D12Resource1.Dispose();
|
||||
this.texture?.Dispose();
|
||||
@this.d3D12Device.Dispose();
|
||||
@this.d3D12CommandQueue.Dispose();
|
||||
@this.d3D12Fence.Dispose();
|
||||
@this.d3D12CommandAllocator.Dispose();
|
||||
@this.d3D12GraphicsCommandList.Dispose();
|
||||
@this.dxgiSwapChain3.Dispose();
|
||||
@this.d3D12Resource0.Dispose();
|
||||
@this.d3D12Resource1.Dispose();
|
||||
@this.texture?.Dispose();
|
||||
}, this);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -16,5 +16,6 @@ public interface IShaderRunner
|
||||
/// </summary>
|
||||
/// <param name="texture">The target texture to render the frame to.</param>
|
||||
/// <param name="timespan">The timespan for the current frame.</param>
|
||||
public void Execute(IReadWriteTexture2D<Float4> texture, TimeSpan timespan);
|
||||
/// <param name="parameter">The input parameter for the frame being rendered.</param>
|
||||
void Execute(IReadWriteTexture2D<Float4> texture, TimeSpan timespan, object? parameter);
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using Microsoft.Toolkit.Diagnostics;
|
||||
|
||||
#if WINDOWS_UWP
|
||||
namespace ComputeSharp.Uwp;
|
||||
@@ -14,9 +15,14 @@ public sealed class ShaderRunner<T> : IShaderRunner
|
||||
where T : struct, IPixelShader<Float4>
|
||||
{
|
||||
/// <summary>
|
||||
/// The <see cref="Func{T1, TResult}"/> instance used to create shaders to run.
|
||||
/// The <see cref="Func{T1,TResult}"/> instance used to create shaders to run.
|
||||
/// </summary>
|
||||
private readonly Func<TimeSpan, T> shaderFactory;
|
||||
private readonly Func<TimeSpan, T>? shaderFactory;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="Func{T1,T2,TResult}"/> instance used to create shaders to run.
|
||||
/// </summary>
|
||||
private readonly Func<TimeSpan, object?, T>? statefulShaderFactory;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ShaderRunner{T}"/> instance that will create shader instances with
|
||||
@@ -25,21 +31,40 @@ public sealed class ShaderRunner<T> : IShaderRunner
|
||||
/// </summary>
|
||||
public ShaderRunner()
|
||||
{
|
||||
this.shaderFactory = static _ => default;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ShaderRunner{T}"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="shaderFactory">The <see cref="Func{T1, T2, TResult}"/> instance used to create shaders to run.</param>
|
||||
/// <param name="shaderFactory">The <see cref="Func{T1,TResult}"/> instance used to create shaders to run.</param>
|
||||
public ShaderRunner(Func<TimeSpan, T> shaderFactory)
|
||||
{
|
||||
Guard.IsNotNull(shaderFactory, nameof(shaderFactory));
|
||||
|
||||
this.shaderFactory = shaderFactory;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Execute(IReadWriteTexture2D<Float4> texture, TimeSpan time)
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ShaderRunner{T}"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="shaderFactory">The <see cref="Func{T1,T2,TResult}"/> instance used to create shaders to run.</param>
|
||||
public ShaderRunner(Func<TimeSpan, object?, T> shaderFactory)
|
||||
{
|
||||
GraphicsDevice.Default.ForEach(texture, this.shaderFactory(time));
|
||||
Guard.IsNotNull(shaderFactory, nameof(shaderFactory));
|
||||
|
||||
this.statefulShaderFactory = shaderFactory;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Execute(IReadWriteTexture2D<Float4> texture, TimeSpan time, object? parameter)
|
||||
{
|
||||
if (this.shaderFactory is Func<TimeSpan, T> shaderFactory)
|
||||
{
|
||||
GraphicsDevice.Default.ForEach(texture, this.shaderFactory(time));
|
||||
}
|
||||
else
|
||||
{
|
||||
GraphicsDevice.Default.ForEach(texture, this.statefulShaderFactory!(time, parameter));
|
||||
}
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする