diff --git a/ComputeSharp.sln b/ComputeSharp.sln index 05a9aab4..2248ab6f 100644 --- a/ComputeSharp.sln +++ b/ComputeSharp.sln @@ -453,6 +453,7 @@ Global samples\ComputeSharp.SwapChain.Shaders.D2D1.Shared\ComputeSharp.SwapChain.Shaders.D2D1.Shared.projitems*{f8bf7666-7ccf-4fe4-b055-2eba4bcd4564}*SharedItemsImports = 13 src\ComputeSharp.UI\ComputeSharp.UI.projitems*{fa18ba9f-76bf-4de0-8d24-02fc7657e6cb}*SharedItemsImports = 5 samples\ComputeSharp.SwapChain.Core.Shared\ComputeSharp.SwapChain.Core.Shared.projitems*{fbd388a5-7855-4408-98ff-e689f509db5a}*SharedItemsImports = 4 + samples\ComputeSharp.SwapChain.Shaders.D2D1.Shared\ComputeSharp.SwapChain.Shaders.D2D1.Shared.projitems*{fbd388a5-7855-4408-98ff-e689f509db5a}*SharedItemsImports = 4 samples\ComputeSharp.SwapChain.Shaders.Shared\ComputeSharp.SwapChain.Shaders.Shared.projitems*{fbd388a5-7855-4408-98ff-e689f509db5a}*SharedItemsImports = 4 EndGlobalSection EndGlobal diff --git a/samples/ComputeSharp.SwapChain.Uwp/App.xaml b/samples/ComputeSharp.SwapChain.Uwp/App.xaml index b203f6c3..8d04ab03 100644 --- a/samples/ComputeSharp.SwapChain.Uwp/App.xaml +++ b/samples/ComputeSharp.SwapChain.Uwp/App.xaml @@ -3,6 +3,11 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> - + + + + + + diff --git a/samples/ComputeSharp.SwapChain.Uwp/ComputeSharp.SwapChain.Uwp.csproj b/samples/ComputeSharp.SwapChain.Uwp/ComputeSharp.SwapChain.Uwp.csproj index 26263f0b..4daf47b4 100644 --- a/samples/ComputeSharp.SwapChain.Uwp/ComputeSharp.SwapChain.Uwp.csproj +++ b/samples/ComputeSharp.SwapChain.Uwp/ComputeSharp.SwapChain.Uwp.csproj @@ -79,6 +79,7 @@ App.xaml + @@ -192,17 +193,25 @@ + + + + + MSBuild:Compile + Designer + Designer MSBuild:Compile + diff --git a/samples/ComputeSharp.SwapChain.Uwp/Controls/D2D1AnimatedPixelShaderPanel.cs b/samples/ComputeSharp.SwapChain.Uwp/Controls/D2D1AnimatedPixelShaderPanel.cs new file mode 100644 index 00000000..6134dac8 --- /dev/null +++ b/samples/ComputeSharp.SwapChain.Uwp/Controls/D2D1AnimatedPixelShaderPanel.cs @@ -0,0 +1,85 @@ +using ComputeSharp.SwapChain.Core.Shaders; +using Microsoft.Graphics.Canvas.UI.Xaml; +using Windows.UI.Xaml; +using Windows.UI.Xaml.Controls; + +#nullable enable + +namespace ComputeSharp.SwapChain.Uwp.Views; + +/// +/// A custom that can be used to render animated backgrounds via Win2D. +/// +[TemplatePart(Name = "PART_CanvasAnimatedControl", Type = typeof(CanvasAnimatedControl))] +public sealed class D2D1AnimatedPixelShaderPanel : Control +{ + /// + /// The wrapped instance used to render frames. + /// + private CanvasAnimatedControl? canvasAnimatedControl; + + /// + /// The instance currently in use, if any. + /// + private volatile ID2D1ShaderRunner? shaderRunner; + + /// + /// Creates a new instance. + /// + public D2D1AnimatedPixelShaderPanel() + { + DefaultStyleKey = typeof(D2D1AnimatedPixelShaderPanel); + } + + /// + protected override void OnApplyTemplate() + { + base.OnApplyTemplate(); + + this.canvasAnimatedControl = (CanvasAnimatedControl)GetTemplateChild("PART_CanvasAnimatedControl")!; + this.canvasAnimatedControl.Draw += CanvasAnimatedControl_Draw; + } + + /// + /// Draws a new frame on the wrapped instance. + /// + /// + private void CanvasAnimatedControl_Draw(ICanvasAnimatedControl sender, CanvasAnimatedDrawEventArgs args) + { + this.shaderRunner?.Execute(sender, args); + } + + /// + /// Gets or sets the instance to use to render content. + /// + public ID2D1ShaderRunner? ShaderRunner + { + get => (ID2D1ShaderRunner?)GetValue(ShaderRunnerProperty); + set => SetValue(ShaderRunnerProperty, value); + } + + /// + /// The backing . + /// + public static readonly DependencyProperty ShaderRunnerProperty = DependencyProperty.Register( + nameof(ShaderRunner), + typeof(ID2D1ShaderRunner), + typeof(D2D1AnimatedPixelShaderPanel), + new PropertyMetadata(null, OnShaderRunnerPropertyChanged)); + + /// + private static void OnShaderRunnerPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + D2D1AnimatedPixelShaderPanel @this = (D2D1AnimatedPixelShaderPanel)d; + ID2D1ShaderRunner? shaderRunner = (ID2D1ShaderRunner?)e.NewValue; + + // Save the new shader runner for later (as the dependency property cannot be accessed by the render thread) + @this.shaderRunner = shaderRunner; + + // Pause or start the render thread if a runner is available + if (@this.canvasAnimatedControl is { } canvasAnimatedControl) + { + canvasAnimatedControl.Paused = e.NewValue is not ID2D1ShaderRunner; + } + } +} \ No newline at end of file diff --git a/samples/ComputeSharp.SwapChain.Uwp/Controls/D2D1AnimatedPixelShaderPanel.xaml b/samples/ComputeSharp.SwapChain.Uwp/Controls/D2D1AnimatedPixelShaderPanel.xaml new file mode 100644 index 00000000..3ca0e723 --- /dev/null +++ b/samples/ComputeSharp.SwapChain.Uwp/Controls/D2D1AnimatedPixelShaderPanel.xaml @@ -0,0 +1,16 @@ + + + +