diff --git a/samples/ComputeSharp.SwapChain.Cli/Program.cs b/samples/ComputeSharp.SwapChain.Cli/Program.cs index 68d9578a..be23924e 100644 --- a/samples/ComputeSharp.SwapChain.Cli/Program.cs +++ b/samples/ComputeSharp.SwapChain.Cli/Program.cs @@ -14,22 +14,39 @@ if (args is [] or [_]) shaderName = nameof(ColorfulInfinity); } - // Get the requested shader, if possible - Win32Application? application = shaderName.ToLowerInvariant() switch + static HelloWorld HelloWorld(TimeSpan time) => new((float)time.TotalSeconds); + static ColorfulInfinity ColorfulInfinity(TimeSpan time) => new((float)time.TotalSeconds); + static FractalTiling FractalTiling(TimeSpan time) => new((float)time.TotalSeconds); + static TwoTiledTruchet TwoTiledTruchet(TimeSpan time) => new((float)time.TotalSeconds); + static MengerJourney MengerJourney(TimeSpan time) => new((float)time.TotalSeconds); + static Octagrams Octagrams(TimeSpan time) => new((float)time.TotalSeconds); + static ProteanClouds ProteanClouds(TimeSpan time) => new((float)time.TotalSeconds); + static ExtrudedTruchetPattern ExtrudedTruchetPattern(TimeSpan time) => new((float)time.TotalSeconds); + static PyramidPattern PyramidPattern(TimeSpan time) => new((float)time.TotalSeconds); + static TriangleGridContouring TriangleGridContouring(TimeSpan time) => new((float)time.TotalSeconds); + static TerracedHills TerracedHills(TimeSpan time) => new((float)time.TotalSeconds); + + Win32Application? application; + + unsafe { - "helloworld" => new SwapChainApplication(static time => new((float)time.TotalSeconds)), - "colorfulinfinity" => new SwapChainApplication(static time => new((float)time.TotalSeconds)), - "fractaltiling" => new SwapChainApplication(static time => new((float)time.TotalSeconds)), - "twotiledtruchet" => new SwapChainApplication(static time => new((float)time.TotalSeconds)), - "mengerjourney" => new SwapChainApplication(static time => new((float)time.TotalSeconds)), - "octagrams" => new SwapChainApplication(static time => new((float)time.TotalSeconds)), - "proteanclouds" => new SwapChainApplication(static time => new((float)time.TotalSeconds)), - "extrudedtruchetpattern" => new SwapChainApplication(static time => new((float)time.TotalSeconds)), - "pyramidpattern" => new SwapChainApplication(static time => new((float)time.TotalSeconds)), - "trianglegridcontouring" => new SwapChainApplication(static time => new((float)time.TotalSeconds)), - "terracedhills" => new SwapChainApplication(static time => new((float)time.TotalSeconds)), - _ => null - }; + // Get the requested shader, if possible + application = shaderName.ToLowerInvariant() switch + { + "helloworld" => new SwapChainApplication(&HelloWorld), + "colorfulinfinity" => new SwapChainApplication(&ColorfulInfinity), + "fractaltiling" => new SwapChainApplication(&FractalTiling), + "twotiledtruchet" => new SwapChainApplication(&TwoTiledTruchet), + "mengerjourney" => new SwapChainApplication(&MengerJourney), + "octagrams" => new SwapChainApplication(&Octagrams), + "proteanclouds" => new SwapChainApplication(&ProteanClouds), + "extrudedtruchetpattern" => new SwapChainApplication(&ExtrudedTruchetPattern), + "pyramidpattern" => new SwapChainApplication(&PyramidPattern), + "trianglegridcontouring" => new SwapChainApplication(&TriangleGridContouring), + "terracedhills" => new SwapChainApplication(&TerracedHills), + _ => null + }; + } // If a shader is found, run it if (application is not null) diff --git a/samples/ComputeSharp.SwapChain/Backend/SwapChainApplication{T}.cs b/samples/ComputeSharp.SwapChain/Backend/SwapChainApplication{T}.cs index d4d15aec..377140ec 100644 --- a/samples/ComputeSharp.SwapChain/Backend/SwapChainApplication{T}.cs +++ b/samples/ComputeSharp.SwapChain/Backend/SwapChainApplication{T}.cs @@ -5,13 +5,17 @@ using TerraFX.Interop.Windows; namespace ComputeSharp.SwapChain.Backend; -internal sealed class SwapChainApplication : Win32Application +/// +/// A type that renders an animated shader on a swapchain on a target . +/// +/// The shader type to render. +internal sealed unsafe class SwapChainApplication : Win32Application where T : struct, IPixelShader { /// - /// The instance used to create shaders to run. + /// The function pointer used to create shaders to run. /// - private readonly Func shaderFactory; + private readonly delegate* shaderFactory; /// /// The pointer for the device currently in use. @@ -76,8 +80,8 @@ internal sealed class SwapChainApplication : Win32Application /// /// Creates a new instance with the specified parameters. /// - /// The instance used to create shaders to run. - public SwapChainApplication(Func shaderFactory) + /// The function pointer used to create shaders to run. + public SwapChainApplication(delegate* shaderFactory) { this.shaderFactory = shaderFactory; } diff --git a/samples/ComputeSharp.SwapChain/Program.cs b/samples/ComputeSharp.SwapChain/Program.cs index 3d2330b6..f94f3eb2 100644 --- a/samples/ComputeSharp.SwapChain/Program.cs +++ b/samples/ComputeSharp.SwapChain/Program.cs @@ -13,28 +13,45 @@ class Program /// private static readonly Lazy> RustyMetal = new(static () => LoadTexture()); - /// - /// The mapping of available samples to choose from. - /// - private static readonly (string ShaderName, Win32Application Application)[] Samples = new (string, Win32Application)[] - { - (nameof(HelloWorld), new SwapChainApplication(static time => new((float)time.TotalSeconds))), - (nameof(FourColorGradient), new SwapChainApplication(static time => new((float)time.TotalSeconds))), - (nameof(ColorfulInfinity), new SwapChainApplication(static time => new((float)time.TotalSeconds))), - (nameof(FractalTiling), new SwapChainApplication(static time => new((float)time.TotalSeconds))), - (nameof(TwoTiledTruchet), new SwapChainApplication(static time => new((float)time.TotalSeconds))), - (nameof(MengerJourney), new SwapChainApplication(static time => new((float)time.TotalSeconds))), - (nameof(Octagrams), new SwapChainApplication(static time => new((float)time.TotalSeconds))), - (nameof(ProteanClouds), new SwapChainApplication(static time => new((float)time.TotalSeconds))), - (nameof(ExtrudedTruchetPattern), new SwapChainApplication(static time => new((float)time.TotalSeconds))), - (nameof(PyramidPattern), new SwapChainApplication(static time => new((float)time.TotalSeconds))), - (nameof(TriangleGridContouring), new SwapChainApplication(static time => new((float)time.TotalSeconds))), - (nameof(ContouredLayers), new SwapChainApplication(static time => new((float)time.TotalSeconds, RustyMetal.Value))), - (nameof(TerracedHills), new SwapChainApplication(static time => new((float)time.TotalSeconds))) - }; - static void Main() { + static HelloWorld HelloWorld(TimeSpan time) => new((float)time.TotalSeconds); + static FourColorGradient FourColorGradient(TimeSpan time) => new((float)time.TotalSeconds); + static ColorfulInfinity ColorfulInfinity(TimeSpan time) => new((float)time.TotalSeconds); + static FractalTiling FractalTiling(TimeSpan time) => new((float)time.TotalSeconds); + static TwoTiledTruchet TwoTiledTruchet(TimeSpan time) => new((float)time.TotalSeconds); + static MengerJourney MengerJourney(TimeSpan time) => new((float)time.TotalSeconds); + static Octagrams Octagrams(TimeSpan time) => new((float)time.TotalSeconds); + static ProteanClouds ProteanClouds(TimeSpan time) => new((float)time.TotalSeconds); + static ExtrudedTruchetPattern ExtrudedTruchetPattern(TimeSpan time) => new((float)time.TotalSeconds); + static PyramidPattern PyramidPattern(TimeSpan time) => new((float)time.TotalSeconds); + static TriangleGridContouring TriangleGridContouring(TimeSpan time) => new((float)time.TotalSeconds); + static ContouredLayers ContouredLayers(TimeSpan time) => new((float)time.TotalSeconds, RustyMetal.Value); + static TerracedHills TerracedHills(TimeSpan time) => new((float)time.TotalSeconds); + + (string ShaderName, Win32Application Application)[] samples; + + unsafe + { + // Prepare the mapping of available samples to choose from + samples = new (string, Win32Application)[] + { + (nameof(HelloWorld), new SwapChainApplication(&HelloWorld)), + (nameof(FourColorGradient), new SwapChainApplication(&FourColorGradient)), + (nameof(ColorfulInfinity), new SwapChainApplication(&ColorfulInfinity)), + (nameof(FractalTiling), new SwapChainApplication(&FractalTiling)), + (nameof(TwoTiledTruchet), new SwapChainApplication(&TwoTiledTruchet)), + (nameof(MengerJourney), new SwapChainApplication(&MengerJourney)), + (nameof(Octagrams), new SwapChainApplication(&Octagrams)), + (nameof(ProteanClouds), new SwapChainApplication(&ProteanClouds)), + (nameof(ExtrudedTruchetPattern), new SwapChainApplication(&ExtrudedTruchetPattern)), + (nameof(PyramidPattern), new SwapChainApplication(&PyramidPattern)), + (nameof(TriangleGridContouring), new SwapChainApplication(&TriangleGridContouring)), + (nameof(ContouredLayers), new SwapChainApplication(&ContouredLayers)), + (nameof(TerracedHills), new SwapChainApplication(&TerracedHills)) + }; + } + int index; do @@ -43,12 +60,12 @@ class Program Console.WriteLine("Available samples:"); Console.WriteLine(); - for (int i = 0; i < Samples.Length; i++) + for (int i = 0; i < samples.Length; i++) { - Console.WriteLine($"{i}: {Samples[i].ShaderName}"); + 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($"{samples.Length}+: Exit (Use Escape, 'Q', or Alt + F4 to exit a sample once chosen)"); Console.WriteLine(); do @@ -57,15 +74,15 @@ class Program } while (!int.TryParse(Console.ReadLine(), out index)); - if (index >= 0 && index < Samples.Length) + if (index >= 0 && index < samples.Length) { Console.WriteLine(); - Console.WriteLine($"Starting {Samples[index].ShaderName}..."); + Console.WriteLine($"Starting {samples[index].ShaderName}..."); - _ = Win32ApplicationRunner.Run(Samples[index].Application, "ComputeSharp.SwapChain", Samples[index].ShaderName); + _ = Win32ApplicationRunner.Run(samples[index].Application, "ComputeSharp.SwapChain", samples[index].ShaderName); } } - while (index >= 0 && index < Samples.Length); + while (index >= 0 && index < samples.Length); } ///