using ComputeSharp.D2D1; using ComputeSharp.D2D1.WinUI; using Windows.Foundation; namespace ComputeSharp.NativeLibrary; /// /// A hello world effect that displays a color gradient. /// public sealed partial class HelloWorldEffect : CanvasEffect { /// /// The node in use. /// private static readonly CanvasEffectNode> EffectNode = new(); /// private float time; /// private Rect dispatchArea; /// /// Gets or sets the current time since the start of the application. /// public float Time { get => this.time; set => SetPropertyAndInvalidateEffectGraph(ref this.time, value); } /// /// Gets or sets the dispatch area for the current output. /// public Rect DispatchArea { get => this.dispatchArea; set => SetPropertyAndInvalidateEffectGraph(ref this.dispatchArea, value); } /// protected override void BuildEffectGraph(CanvasEffectGraph effectGraph) { effectGraph.RegisterOutputNode(EffectNode, new PixelShaderEffect()); } /// protected override void ConfigureEffectGraph(CanvasEffectGraph effectGraph) { effectGraph.GetNode(EffectNode).ConstantBuffer = new Shader( time: this.time, dispatchSize: new int2( x: (int)double.Round(this.dispatchArea.Width), y: (int)double.Round(this.dispatchArea.Height))); } /// /// A hello world effect that displays a color gradient. /// /// The current time since the start of the application. /// The dispatch size for the current output. [D2DEffectDisplayName(nameof(HelloWorldEffect))] [D2DEffectDescription("A hello world effect that displays a color gradient.")] [D2DEffectCategory("Render")] [D2DEffectAuthor("ComputeSharp.D2D1")] [D2DInputCount(0)] [D2DRequiresScenePosition] [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] [D2DGeneratedPixelShaderDescriptor] internal readonly partial struct Shader(float time, int2 dispatchSize) : ID2D1PixelShader { /// public float4 Execute() { int2 xy = (int2)D2D.GetScenePosition().XY; float2 uv = xy / (float2)dispatchSize; float3 color = 0.5f + (0.5f * Hlsl.Cos(time + new float3(uv, uv.X) + new float3(0, 2, 4))); return new(color, 1f); } } }