using System;
using CommunityToolkit.Diagnostics;
using ComputeSharp.D2D1;
#if WINDOWS_UWP
using ComputeSharp.D2D1.Uwp;
#else
using ComputeSharp.D2D1.WinUI;
#endif
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, IDisposable
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);
}
///
public void Dispose()
{
this.pixelShaderEffect.Dispose();
}
}