Add AnimatedComputeShaderPanel control

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

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

+2 -2
ファイルの表示
@@ -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}"
+2 -2
ファイルの表示
@@ -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}"
+2
ファイルの表示
@@ -9,6 +9,8 @@
<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)Helpers\SwapChainManager.Rendering.cs" />
+144
ファイルの表示
@@ -0,0 +1,144 @@
using System;
#if WINDOWS_UWP
using Windows.UI.Xaml;
#else
using Microsoft.UI.Xaml;
#endif
#if WINDOWS_UWP
namespace ComputeSharp.Uwp;
#else
namespace ComputeSharp.WinUI;
#endif
/// <inheritdoc cref="AnimatedComputeShaderPanel"/>
partial class AnimatedComputeShaderPanel
{
/// <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();
}
}
}
+88
ファイルの表示
@@ -0,0 +1,88 @@
#if WINDOWS_UWP
using System.Runtime.InteropServices;
using ComputeSharp.Uwp.Helpers;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using TerraFX.Interop.Windows;
#else
using ComputeSharp.WinUI.Helpers;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using TerraFX.Interop.Windows;
using WinRT;
#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"/> instance handling rendering.
/// </summary>
private readonly SwapChainManager swapChainManager;
/// <summary>
/// Creates a new <see cref="AnimatedComputeShaderPanel"/> instance.
/// </summary>
public unsafe AnimatedComputeShaderPanel()
{
#if WINDOWS_UWP
IUnknown* swapChainPanel = (IUnknown*)Marshal.GetIUnknownForObject(this);
#else
IUnknown* swapChainPanel = (IUnknown*)((IWinRTObject)this).NativeObject.ThisPtr;
#endif
this.swapChainManager = new SwapChainManager(swapChainPanel);
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);
}
}
+12 -44
ファイルの表示
@@ -36,14 +36,17 @@ partial class ComputeShaderPanel
private static void OnFrameRequestQueueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var @this = (ComputeShaderPanel)d;
var frameRequestQueue = (IFrameRequestQueue?)e.NewValue;
if (@this.IsLoaded &&
!@this.IsPaused &&
e.NewValue is IFrameRequestQueue frameRequestQueue &&
@this.ShaderRunner is IShaderRunner shaderRunner)
{
@this.swapChainManager.StartRenderLoop(frameRequestQueue, shaderRunner);
}
else
{
@this.swapChainManager.StopRenderLoop();
}
}
/// <summary>
@@ -68,17 +71,17 @@ partial class ComputeShaderPanel
private static void OnShaderRunnerPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var @this = (ComputeShaderPanel)d;
var shaderRunner = (IShaderRunner?)e.NewValue;
if (shaderRunner is null)
if (@this.IsLoaded &&
@this.FrameRequestQueue is IFrameRequestQueue frameRequestQueue &&
e.NewValue is IShaderRunner shaderRunner)
{
@this.swapChainManager.StartRenderLoop(frameRequestQueue, shaderRunner);
}
else
{
@this.swapChainManager.StopRenderLoop();
}
else if (@this.IsLoaded &&
!@this.IsPaused)
{
@this.swapChainManager.StartRenderLoop(@this.FrameRequestQueue, shaderRunner);
}
}
/// <summary>
@@ -138,39 +141,4 @@ partial class ComputeShaderPanel
@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(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.swapChainManager.StopRenderLoop();
}
else if (@this.IsLoaded &&
@this.ShaderRunner is IShaderRunner shaderRunner)
{
@this.swapChainManager.StartRenderLoop(@this.FrameRequestQueue, shaderRunner);
}
}
}
+8 -2
ファイルの表示
@@ -19,8 +19,13 @@ 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 partial class ComputeShaderPanel : SwapChainPanel
{
@@ -56,9 +61,10 @@ public sealed partial class ComputeShaderPanel : SwapChainPanel
this.swapChainManager.QueueResolutionScaleChange(ResolutionScale);
this.swapChainManager.QueueDynamicResolutionModeChange(IsDynamicResolutionEnabled);
if (ShaderRunner is IShaderRunner shaderRunner)
if (FrameRequestQueue is IFrameRequestQueue frameRequestQueue &&
ShaderRunner is IShaderRunner shaderRunner)
{
this.swapChainManager.StartRenderLoop(FrameRequestQueue, shaderRunner);
this.swapChainManager.StartRenderLoop(frameRequestQueue, shaderRunner);
}
}