From f6388dcd150b5caa843340753ce8fe2cb2fc7043 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sat, 15 Apr 2023 19:34:33 +0200 Subject: [PATCH] Implement D2D1 drawing in new WASDK sample --- ComputeSharp.sln | 3 +- .../ICanvasFactoryNative.cs | 2 +- .../Backend/PixelShaderEffect.cs | 115 ++++++++++++++++++ .../Backend/Win32Application.cs | 20 ++- .../ComputeSharp.SwapChain.D2D1.csproj | 11 ++ .../ComputeSharp.SwapChain.D2D1/Program.cs | 73 ++++++++++- 6 files changed, 219 insertions(+), 5 deletions(-) create mode 100644 samples/ComputeSharp.SwapChain.D2D1/Backend/PixelShaderEffect.cs diff --git a/ComputeSharp.sln b/ComputeSharp.sln index 55cdf7ce..3d05bd7f 100644 --- a/ComputeSharp.sln +++ b/ComputeSharp.sln @@ -153,7 +153,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{B54B6DCA tests\Directory.Build.targets = tests\Directory.Build.targets EndProjectSection EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputeSharp.SwapChain.D2D1", "samples\ComputeSharp.SwapChain.D2D1\ComputeSharp.SwapChain.D2D1.csproj", "{9EA5AE9D-C39A-4F43-B03E-0A848EA2558A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputeSharp.SwapChain.D2D1", "samples\ComputeSharp.SwapChain.D2D1\ComputeSharp.SwapChain.D2D1.csproj", "{9EA5AE9D-C39A-4F43-B03E-0A848EA2558A}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -478,6 +478,7 @@ Global src\ComputeSharp.D2D1.UI\ComputeSharp.D2D1.UI.projitems*{902d7b44-2c6c-4fcd-840b-ebcae811ee63}*SharedItemsImports = 13 src\ComputeSharp.SourceGeneration.Hlsl\ComputeSharp.SourceGeneration.Hlsl.projitems*{9ac496a3-bbf0-4c8f-a50d-a20bf01c5e05}*SharedItemsImports = 13 src\TerraFX.Interop.Windows.D2D1\TerraFX.Interop.Windows.D2D1.projitems*{9da1da9f-f8b2-4b25-be80-c21f773029e3}*SharedItemsImports = 13 + samples\ComputeSharp.SwapChain.Shaders.D2D1.Shared\ComputeSharp.SwapChain.Shaders.D2D1.Shared.projitems*{9ea5ae9d-c39a-4f43-b03e-0a848ea2558a}*SharedItemsImports = 5 src\ComputeSharp.NetStandard\ComputeSharp.NetStandard.projitems*{a5997814-d8d2-4541-87ac-c3e53caa67c0}*SharedItemsImports = 13 src\ComputeSharp.UI\ComputeSharp.UI.projitems*{afaafdc9-be42-45d4-8c79-0ee8c8edbf52}*SharedItemsImports = 5 src\ComputeSharp.D2D1.UI\ComputeSharp.D2D1.UI.projitems*{bd9e6556-357e-4c20-bfcd-fb131f9372fa}*SharedItemsImports = 5 diff --git a/samples/ComputeSharp.SwapChain.D2D1/Backend/ABI.Microsoft.Graphics.Canvas/ICanvasFactoryNative.cs b/samples/ComputeSharp.SwapChain.D2D1/Backend/ABI.Microsoft.Graphics.Canvas/ICanvasFactoryNative.cs index 3cd722ae..ba7a363a 100644 --- a/samples/ComputeSharp.SwapChain.D2D1/Backend/ABI.Microsoft.Graphics.Canvas/ICanvasFactoryNative.cs +++ b/samples/ComputeSharp.SwapChain.D2D1/Backend/ABI.Microsoft.Graphics.Canvas/ICanvasFactoryNative.cs @@ -66,7 +66,7 @@ internal unsafe struct ICanvasFactoryNative : IUnknown.Interface /// The input native resource to create a wrapper for. /// The realization DPIs for /// The resulting wrapper for . - /// The for the operation. + /// The HRESULT for the operation. [MethodImpl(MethodImplOptions.AggressiveInlining)] public HRESULT GetOrCreate(IUnknown* device, IUnknown* resource, float dpi, void** wrapper) { diff --git a/samples/ComputeSharp.SwapChain.D2D1/Backend/PixelShaderEffect.cs b/samples/ComputeSharp.SwapChain.D2D1/Backend/PixelShaderEffect.cs new file mode 100644 index 00000000..44175d6a --- /dev/null +++ b/samples/ComputeSharp.SwapChain.D2D1/Backend/PixelShaderEffect.cs @@ -0,0 +1,115 @@ +using System; +using ComputeSharp.D2D1; +using ComputeSharp.D2D1.WinUI; +using Microsoft.Graphics.Canvas; + +namespace ComputeSharp.SwapChain.D2D1.Backend; + +/// +/// An base effect for an animated pixel shader. +/// +internal 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); + } +} diff --git a/samples/ComputeSharp.SwapChain.D2D1/Backend/Win32Application.cs b/samples/ComputeSharp.SwapChain.D2D1/Backend/Win32Application.cs index 442914dc..99459482 100644 --- a/samples/ComputeSharp.SwapChain.D2D1/Backend/Win32Application.cs +++ b/samples/ComputeSharp.SwapChain.D2D1/Backend/Win32Application.cs @@ -38,7 +38,7 @@ internal sealed class Win32Application /// /// Raised whenever a draw operation can be performed. /// - public event EventHandler? Draw; + public event EventHandler? Draw; /// /// Initializes the current application. @@ -172,7 +172,7 @@ internal sealed class Win32Application // Create the drawing session and invoke all registered draw handler using (CanvasDrawingSession canvasDrawingSession = this.canvasSwapChain!.CreateDrawingSession(default)) { - Draw?.Invoke(this, canvasDrawingSession); + Draw?.Invoke(this, new DrawEventArgs { TotalTime = time, DrawingSession = canvasDrawingSession }); } // Wait for v-sync @@ -181,4 +181,20 @@ internal sealed class Win32Application // Present the new frame this.canvasSwapChain.Present(syncInterval: 1); } + + /// + /// Arguments for . + /// + public sealed class DrawEventArgs : EventArgs + { + /// + /// Gets the total time for the rendering loop. + /// + public required TimeSpan TotalTime { get; init; } + + /// + /// Gets the instance to use. + /// + public required CanvasDrawingSession DrawingSession { get; init; } + } } diff --git a/samples/ComputeSharp.SwapChain.D2D1/ComputeSharp.SwapChain.D2D1.csproj b/samples/ComputeSharp.SwapChain.D2D1/ComputeSharp.SwapChain.D2D1.csproj index 77c27502..6eb5e77e 100644 --- a/samples/ComputeSharp.SwapChain.D2D1/ComputeSharp.SwapChain.D2D1.csproj +++ b/samples/ComputeSharp.SwapChain.D2D1/ComputeSharp.SwapChain.D2D1.csproj @@ -9,7 +9,18 @@ + + + + + + + + + + + diff --git a/samples/ComputeSharp.SwapChain.D2D1/Program.cs b/samples/ComputeSharp.SwapChain.D2D1/Program.cs index 6d1b619f..0b06db95 100644 --- a/samples/ComputeSharp.SwapChain.D2D1/Program.cs +++ b/samples/ComputeSharp.SwapChain.D2D1/Program.cs @@ -1,3 +1,74 @@ +using System; using ComputeSharp.SwapChain.D2D1.Backend; +using ComputeSharp.SwapChain.Shaders.D2D1; -return Win32ApplicationRunner.Run(new Win32Application(), "ComputeSharp.SwapChain.D2D1", "test"); \ No newline at end of file +namespace ComputeSharp.SwapChain; + +class Program +{ + /// + /// The mapping of available samples to choose from. + /// + private static readonly (string ShaderName, PixelShaderEffect Effect)[] Samples = new (string, PixelShaderEffect)[] + { + (nameof(HelloWorld), new PixelShaderEffect.For(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))), + (nameof(ColorfulInfinity), new PixelShaderEffect.For(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))), + (nameof(FractalTiling), new PixelShaderEffect.For(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))), + (nameof(TwoTiledTruchet), new PixelShaderEffect.For(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))), + (nameof(MengerJourney), new PixelShaderEffect.For(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))), + (nameof(Octagrams), new PixelShaderEffect.For(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))), + (nameof(ProteanClouds), new PixelShaderEffect.For(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))), + (nameof(PyramidPattern), new PixelShaderEffect.For(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))), + (nameof(TriangleGridContouring), new PixelShaderEffect.For(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))), + (nameof(TerracedHills), new PixelShaderEffect.For(static (time, width, height) => new((float)time.TotalSeconds, new int2(width, height)))) + }; + + static void Main() + { + int index; + + do + { + Console.Clear(); + Console.WriteLine("Available samples:"); + Console.WriteLine(); + + for (int i = 0; i < Samples.Length; i++) + { + Console.WriteLine($"{i}: {Samples[i].ShaderName}"); + } + + Console.WriteLine($"{Samples.Length}+: Exit (Use Escape, 'Q', or Alt + F4 to exit a sample once chosen)"); + Console.WriteLine(); + + do + { + Console.Write("Enter the index of the sample to run: "); + } + while (!int.TryParse(Console.ReadLine(), out index)); + + if (index >= 0 && index < Samples.Length) + { + Console.WriteLine(); + Console.WriteLine($"Starting {Samples[index].ShaderName}..."); + + Win32Application win32Application = new(); + PixelShaderEffect effect = Samples[index].Effect; + + win32Application.Draw += (_, e) => + { + // Set the effect properties + effect.ElapsedTime = e.TotalTime; + effect.ScreenWidth = 1280; + effect.ScreenHeight = 720; + + // Draw the effect + e.DrawingSession.DrawImage(effect); + }; + + _ = Win32ApplicationRunner.Run(win32Application, "ComputeSharp.SwapChain.D2D1", Samples[index].ShaderName); + } + } + while (index >= 0 && index < Samples.Length); + } +} \ No newline at end of file