From c424f54de9a6a540f9a5a6b17dc8a01c80f15044 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 22 Oct 2023 11:35:28 +0200 Subject: [PATCH] Implement HelloWorldEffect for WinRT library sample --- ComputeSharp.sln | 16 ++-- .../ComputeSharp.NativeLibrary.WinRT.csproj | 6 +- .../HelloWorldEffect.cs | 91 +++++++++++++++++++ 3 files changed, 103 insertions(+), 10 deletions(-) create mode 100644 samples/ComputeSharp.NativeLibrary.WinRT/HelloWorldEffect.cs diff --git a/ComputeSharp.sln b/ComputeSharp.sln index 21b6900a..af1ec1b2 100644 --- a/ComputeSharp.sln +++ b/ComputeSharp.sln @@ -456,14 +456,14 @@ Global {1A3E20B3-F711-41C0-82EA-3B53A575955C}.Release|ARM64.Build.0 = Release|Any CPU {1A3E20B3-F711-41C0-82EA-3B53A575955C}.Release|x64.ActiveCfg = Release|Any CPU {1A3E20B3-F711-41C0-82EA-3B53A575955C}.Release|x64.Build.0 = Release|Any CPU - {551EF4FB-F34F-412A-B3E6-E345797560ED}.Debug|ARM64.ActiveCfg = Debug|Any CPU - {551EF4FB-F34F-412A-B3E6-E345797560ED}.Debug|ARM64.Build.0 = Debug|Any CPU - {551EF4FB-F34F-412A-B3E6-E345797560ED}.Debug|x64.ActiveCfg = Debug|Any CPU - {551EF4FB-F34F-412A-B3E6-E345797560ED}.Debug|x64.Build.0 = Debug|Any CPU - {551EF4FB-F34F-412A-B3E6-E345797560ED}.Release|ARM64.ActiveCfg = Release|Any CPU - {551EF4FB-F34F-412A-B3E6-E345797560ED}.Release|ARM64.Build.0 = Release|Any CPU - {551EF4FB-F34F-412A-B3E6-E345797560ED}.Release|x64.ActiveCfg = Release|Any CPU - {551EF4FB-F34F-412A-B3E6-E345797560ED}.Release|x64.Build.0 = Release|Any CPU + {551EF4FB-F34F-412A-B3E6-E345797560ED}.Debug|ARM64.ActiveCfg = Debug|ARM64 + {551EF4FB-F34F-412A-B3E6-E345797560ED}.Debug|ARM64.Build.0 = Debug|ARM64 + {551EF4FB-F34F-412A-B3E6-E345797560ED}.Debug|x64.ActiveCfg = Debug|x64 + {551EF4FB-F34F-412A-B3E6-E345797560ED}.Debug|x64.Build.0 = Debug|x64 + {551EF4FB-F34F-412A-B3E6-E345797560ED}.Release|ARM64.ActiveCfg = Release|ARM64 + {551EF4FB-F34F-412A-B3E6-E345797560ED}.Release|ARM64.Build.0 = Release|ARM64 + {551EF4FB-F34F-412A-B3E6-E345797560ED}.Release|x64.ActiveCfg = Release|x64 + {551EF4FB-F34F-412A-B3E6-E345797560ED}.Release|x64.Build.0 = Release|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/samples/ComputeSharp.NativeLibrary.WinRT/ComputeSharp.NativeLibrary.WinRT.csproj b/samples/ComputeSharp.NativeLibrary.WinRT/ComputeSharp.NativeLibrary.WinRT.csproj index eba952e1..86a66fd8 100644 --- a/samples/ComputeSharp.NativeLibrary.WinRT/ComputeSharp.NativeLibrary.WinRT.csproj +++ b/samples/ComputeSharp.NativeLibrary.WinRT/ComputeSharp.NativeLibrary.WinRT.csproj @@ -1,7 +1,8 @@ - net8.0 - AnyCPU;x64;ARM64 + net8.0-windows10.0.22621.0 + x64;ARM64 + win-x64;win-arm64 @@ -21,6 +22,7 @@ + diff --git a/samples/ComputeSharp.NativeLibrary.WinRT/HelloWorldEffect.cs b/samples/ComputeSharp.NativeLibrary.WinRT/HelloWorldEffect.cs new file mode 100644 index 00000000..c6552951 --- /dev/null +++ b/samples/ComputeSharp.NativeLibrary.WinRT/HelloWorldEffect.cs @@ -0,0 +1,91 @@ +using ComputeSharp.D2D1; +using ComputeSharp.D2D1.WinUI; +using Windows.Foundation; + +namespace ComputeSharp.NativeLibrary.WinRT; + +/// +/// 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 => SetAndInvalidateEffectGraph(ref this.time, value); + } + + /// + /// Gets or sets the dispatch area for the current output. + /// + public Rect DispatchArea + { + get => this.dispatchArea; + set => SetAndInvalidateEffectGraph(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))); + } + + /// + /// The D2D1 shader for . + /// + [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] + [AutoConstructor] + internal readonly partial struct Shader : ID2D1PixelShader + { + /// + /// The current time since the start of the application. + /// + private readonly float time; + + /// + /// The dispatch size for the current output. + /// + private readonly int2 dispatchSize; + + /// + public float4 Execute() + { + int2 xy = (int2)D2D.GetScenePosition().XY; + float2 uv = xy / (float2)this.dispatchSize; + float3 color = 0.5f + (0.5f * Hlsl.Cos(this.time + new float3(uv, uv.X) + new float3(0, 2, 4))); + + return new(color, 1f); + } + } +}