diff --git a/samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml b/samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml index fe94efd1..5b07b170 100644 --- a/samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml +++ b/samples/ComputeSharp.SwapChain.Uwp/Views/MainView.xaml @@ -125,7 +125,7 @@ - + - - + - ComputeSharp.UI + + diff --git a/src/ComputeSharp.UI/Controls/AnimatedComputeShaderPanel.Properties.cs b/src/ComputeSharp.UI/Controls/AnimatedComputeShaderPanel.Properties.cs new file mode 100644 index 00000000..9899e75f --- /dev/null +++ b/src/ComputeSharp.UI/Controls/AnimatedComputeShaderPanel.Properties.cs @@ -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 + +/// +partial class AnimatedComputeShaderPanel +{ + /// + /// Gets or sets the instance to use to render content. + /// + public IShaderRunner? ShaderRunner + { + get => (IShaderRunner?)GetValue(ShaderRunnerProperty); + set => SetValue(ShaderRunnerProperty, value); + } + + /// + /// The backing . + /// + public static readonly DependencyProperty ShaderRunnerProperty = DependencyProperty.Register( + nameof(ShaderRunner), + typeof(IShaderRunner), + typeof(AnimatedComputeShaderPanel), + new PropertyMetadata(null, OnShaderRunnerPropertyChanged)); + + /// + 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(); + } + } + + /// + /// Gets or sets the resolution scale to be used to render frames. + /// This can help achieve higher framerates with more complex shaders. + /// + /// The accepted range is [0.1 and 1] (default is 1, exceeding values will be clamped). + public double ResolutionScale + { + get => (double)GetValue(ResolutionScaleProperty); + set => SetValue(ResolutionScaleProperty, Math.Clamp(value, 0.1, 1.0)); + } + + /// + /// The backing . + /// + public static readonly DependencyProperty ResolutionScaleProperty = DependencyProperty.Register( + nameof(ResolutionScale), + typeof(double), + typeof(AnimatedComputeShaderPanel), + new PropertyMetadata(1.0, OnResolutionScalePropertyChanged)); + + /// + private static void OnResolutionScalePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + var @this = (AnimatedComputeShaderPanel)d; + var resolutionScale = (double)e.NewValue; + + @this.swapChainManager.QueueResolutionScaleChange(resolutionScale); + } + + /// + /// 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. + /// The default value for this property is . + /// + public bool IsDynamicResolutionEnabled + { + get => (bool)GetValue(IsDynamicResolutionEnabledProperty); + set => SetValue(IsDynamicResolutionEnabledProperty, value); + } + + /// + /// The backing . + /// + public static readonly DependencyProperty IsDynamicResolutionEnabledProperty = DependencyProperty.Register( + nameof(IsDynamicResolutionEnabled), + typeof(bool), + typeof(AnimatedComputeShaderPanel), + new PropertyMetadata(true, OnIsDynamicResolutionEnabledPropertyChanged)); + + /// + private static void OnIsDynamicResolutionEnabledPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + var @this = (AnimatedComputeShaderPanel)d; + var isDynamicResolutionEnabled = (bool)e.NewValue; + + @this.swapChainManager.QueueDynamicResolutionModeChange(isDynamicResolutionEnabled); + } + + /// + /// Gets or sets whether or not the rendering is paused. + /// + public bool IsPaused + { + get => (bool)GetValue(IsPausedProperty); + set => SetValue(IsPausedProperty, value); + } + + /// + /// The backing . + /// + public static readonly DependencyProperty IsPausedProperty = DependencyProperty.Register( + nameof(IsPaused), + typeof(bool), + typeof(AnimatedComputeShaderPanel), + new PropertyMetadata(false, OnIsPausedPropertyChanged)); + + /// + 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(); + } + } +} diff --git a/src/ComputeSharp.UI/Controls/AnimatedComputeShaderPanel.cs b/src/ComputeSharp.UI/Controls/AnimatedComputeShaderPanel.cs new file mode 100644 index 00000000..85c00936 --- /dev/null +++ b/src/ComputeSharp.UI/Controls/AnimatedComputeShaderPanel.cs @@ -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 + +/// +/// +/// A custom that can be used to render animated backgrounds. +/// It is powered by ComputeSharp and leverages compute shaders to create frames to display. +/// +/// +/// This panel will always render new frames at the current display refresh rate, automatically. +/// +/// +public sealed partial class AnimatedComputeShaderPanel : SwapChainPanel +{ + /// + /// The instance handling rendering. + /// + private readonly SwapChainManager swapChainManager; + + /// + /// Creates a new instance. + /// + 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); + } +} diff --git a/src/ComputeSharp.UI/Controls/ComputeShaderPanel.Properties.cs b/src/ComputeSharp.UI/Controls/ComputeShaderPanel.Properties.cs index 04939c0d..3372bb85 100644 --- a/src/ComputeSharp.UI/Controls/ComputeShaderPanel.Properties.cs +++ b/src/ComputeSharp.UI/Controls/ComputeShaderPanel.Properties.cs @@ -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(); + } } /// @@ -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); - } } /// @@ -138,39 +141,4 @@ partial class ComputeShaderPanel @this.swapChainManager.QueueDynamicResolutionModeChange(isDynamicResolutionEnabled); } - - /// - /// Gets or sets whether or not the rendering is paused. - /// - public bool IsPaused - { - get => (bool)GetValue(IsPausedProperty); - set => SetValue(IsPausedProperty, value); - } - - /// - /// The backing . - /// - public static readonly DependencyProperty IsPausedProperty = DependencyProperty.Register( - nameof(IsPaused), - typeof(bool), - typeof(ComputeShaderPanel), - new PropertyMetadata(false, OnIsPausedPropertyChanged)); - - /// - 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); - } - } } diff --git a/src/ComputeSharp.UI/Controls/ComputeShaderPanel.cs b/src/ComputeSharp.UI/Controls/ComputeShaderPanel.cs index 13bc5a40..9be6d942 100644 --- a/src/ComputeSharp.UI/Controls/ComputeShaderPanel.cs +++ b/src/ComputeSharp.UI/Controls/ComputeShaderPanel.cs @@ -19,8 +19,13 @@ namespace ComputeSharp.WinUI; #endif /// +/// /// A custom that can be used to render animated backgrounds. /// It is powered by ComputeSharp and leverages compute shaders to create frames to display. +/// +/// +/// This panel accepts a custom instance to customize frame requests. +/// /// 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); } }