using System; using ComputeSharp.D2D1; #if WINDOWS_UWP using ComputeSharp.D2D1.Uwp; #else using ComputeSharp.D2D1.WinUI; #endif using Microsoft.Graphics.Canvas; #nullable enable namespace ComputeSharp.SwapChain.Core.Shaders; /// /// An base effect for an animated pixel shader. /// public abstract class PixelShaderEffect : CanvasEffect { /// /// The current elapsed time. /// private TimeSpan elapsedTime; /// /// The current screen width in raw pixels. /// private int screenWidth; /// /// The current screen height in raw pixels. /// private int screenHeight; /// /// Gets or sets the total elapsed time. /// public TimeSpan ElapsedTime { get => this.elapsedTime; set => SetAndInvalidateEffectGraph(ref this.elapsedTime, value); } /// /// Gets or sets the screen width in raw pixels. /// public int ScreenWidth { get => this.screenWidth; set => SetAndInvalidateEffectGraph(ref this.screenWidth, value); } /// /// Gets or sets the screen height in raw pixels. /// public int ScreenHeight { get => this.screenHeight; set => SetAndInvalidateEffectGraph(ref this.screenHeight, value); } /// /// An effect for an animated pixel shader. /// /// The type of pixel shader to render. public sealed class For : PixelShaderEffect where T : unmanaged, ID2D1PixelShader { /// /// The factory to use. /// private readonly Factory factory; /// /// The instance in use. /// private PixelShaderEffect? effect; /// /// Creates a new instance with the specified parameters. /// /// The input factory. public For(Factory factory) { this.factory = factory; } /// protected override ICanvasImage BuildEffectGraph() { this.effect = new PixelShaderEffect(); return this.effect; } /// protected override void ConfigureEffectGraph() { this.effect!.ConstantBuffer = this.factory(ElapsedTime, ScreenWidth, ScreenHeight); } /// protected override void Dispose(bool disposing) { base.Dispose(disposing); if (disposing) { this.effect?.Dispose(); } } /// /// A factory of a given shader instance. /// /// The total elapsed time. /// The screen width in raw pixels. /// The screen height in raw pixels. /// A shader instance to render. public delegate T Factory(TimeSpan elapsedTime, int screenWidth, int screenHeight); } }