diff --git a/samples/ComputeSharp.SwapChain.Core.Shared/ComputeSharp.SwapChain.Core.Shared.projitems b/samples/ComputeSharp.SwapChain.Core.Shared/ComputeSharp.SwapChain.Core.Shared.projitems index bc2b2625..6021791c 100644 --- a/samples/ComputeSharp.SwapChain.Core.Shared/ComputeSharp.SwapChain.Core.Shared.projitems +++ b/samples/ComputeSharp.SwapChain.Core.Shared/ComputeSharp.SwapChain.Core.Shared.projitems @@ -16,6 +16,8 @@ + + diff --git a/samples/ComputeSharp.SwapChain.Core.Shared/Shaders/D2D1ShaderRunner{T}.cs b/samples/ComputeSharp.SwapChain.Core.Shared/Shaders/D2D1ShaderRunner{T}.cs new file mode 100644 index 00000000..5f03fedf --- /dev/null +++ b/samples/ComputeSharp.SwapChain.Core.Shared/Shaders/D2D1ShaderRunner{T}.cs @@ -0,0 +1,51 @@ +using System; +using CommunityToolkit.Diagnostics; +using ComputeSharp.D2D1; +using ComputeSharp.D2D1.Uwp; +using Microsoft.Graphics.Canvas; +using Microsoft.Graphics.Canvas.UI.Xaml; + +namespace ComputeSharp.SwapChain.Core.Shaders; + +/// +/// A simple implementation powered by a supplied shader type. +/// +/// The type of shader to use to render frames. +public sealed class D2D1ShaderRunner : ID2D1ShaderRunner + where T : unmanaged, ID2D1PixelShader +{ + /// + /// The instance used to create shaders to run. + /// + private readonly Func shaderFactory; + + /// + /// The reusable instance to use to render frames. + /// + private readonly PixelShaderEffect pixelShaderEffect; + + /// + /// Creates a new instance. + /// + /// The instance used to create shaders to run. + public D2D1ShaderRunner(Func shaderFactory) + { + Guard.IsNotNull(shaderFactory); + + this.shaderFactory = shaderFactory; + this.pixelShaderEffect = new PixelShaderEffect(); + } + + /// + public void Execute(ICanvasAnimatedControl sender, CanvasAnimatedDrawEventArgs args) + { + int widthInPixels = sender.ConvertDipsToPixels((float)sender.Size.Width, CanvasDpiRounding.Round); + int heightInPixels = sender.ConvertDipsToPixels((float)sender.Size.Height, CanvasDpiRounding.Round); + + // Set the constant buffer + this.pixelShaderEffect.ConstantBuffer = this.shaderFactory(args.Timing.TotalTime, widthInPixels, heightInPixels); + + // Draw the shader + args.DrawingSession.DrawImage(this.pixelShaderEffect); + } +} \ No newline at end of file diff --git a/samples/ComputeSharp.SwapChain.Core.Shared/Shaders/ID2D1ShaderRunner.cs b/samples/ComputeSharp.SwapChain.Core.Shared/Shaders/ID2D1ShaderRunner.cs new file mode 100644 index 00000000..14e3996c --- /dev/null +++ b/samples/ComputeSharp.SwapChain.Core.Shared/Shaders/ID2D1ShaderRunner.cs @@ -0,0 +1,16 @@ +using Microsoft.Graphics.Canvas.UI.Xaml; + +namespace ComputeSharp.SwapChain.Core.Shaders; + +/// +/// An interface for a shader runner to be used with a Win2D . +/// +public interface ID2D1ShaderRunner +{ + /// + /// Draws a new frame on a target instance. + /// + /// The target instance. + /// The instance to use to draw the new frame. + void Execute(ICanvasAnimatedControl sender, CanvasAnimatedDrawEventArgs args); +} \ No newline at end of file