From 76d2f7d83ee92111aaded5944f2992a7425c4b7f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 5 May 2021 16:38:32 +0200 Subject: [PATCH] Switched sample shaders to pixel shaders --- .../Backend/SwapChainApplication{T}.cs | 14 +++++++------- samples/ComputeSharp.SwapChain/Program.cs | 18 +++++++++--------- .../Shaders/ColorfulInfinity.cs | 13 +++---------- .../Shaders/ExtrudedTruchetPattern.cs | 11 +++-------- .../Shaders/FractalTiling.cs | 11 +++-------- .../Shaders/MengerJourney.cs | 11 +++-------- .../Shaders/Octagrams.cs | 11 +++-------- .../Shaders/ProteanClouds.cs | 11 +++-------- .../Shaders/PyramidPattern.cs | 13 +++---------- .../Shaders/TriangleGridContouring.cs | 13 +++---------- .../Shaders/TwoTiledTruchet.cs | 11 +++-------- 11 files changed, 43 insertions(+), 94 deletions(-) diff --git a/samples/ComputeSharp.SwapChain/Backend/SwapChainApplication{T}.cs b/samples/ComputeSharp.SwapChain/Backend/SwapChainApplication{T}.cs index 2fd92fa0..26f4f0cf 100644 --- a/samples/ComputeSharp.SwapChain/Backend/SwapChainApplication{T}.cs +++ b/samples/ComputeSharp.SwapChain/Backend/SwapChainApplication{T}.cs @@ -6,12 +6,12 @@ using FX = TerraFX.Interop.Windows; namespace ComputeSharp.SwapChain.Backend { internal sealed class SwapChainApplication : Win32Application - where T : struct, IComputeShader + where T : struct, IPixelShader { /// - /// The instance used to create shaders to run. + /// The instance used to create shaders to run. /// - private readonly Func, TimeSpan, T> shaderFactory; + private readonly Func shaderFactory; /// /// The pointer for the device currently in use. @@ -76,8 +76,8 @@ namespace ComputeSharp.SwapChain.Backend /// /// Creates a new instance with the specified parameters. /// - /// The instance used to create shaders to run. - public SwapChainApplication(Func, TimeSpan, T> shaderFactory) + /// The instance used to create shaders to run. + public SwapChainApplication(Func shaderFactory) { this.shaderFactory = shaderFactory; } @@ -231,12 +231,12 @@ namespace ComputeSharp.SwapChain.Backend } // Generate the new frame - Gpu.Default.For(this.texture!.Width, this.texture.Height, this.shaderFactory(this.texture, time)); + Gpu.Default.ForEach(this.texture!, this.shaderFactory(time)); using ComPtr d3D12Resource = default; // Get the underlying ID3D12Resource pointer for the texture - _ = InteropServices.TryGetID3D12Resource(this.texture, FX.__uuidof(), (void**)d3D12Resource.GetAddressOf()); + _ = InteropServices.TryGetID3D12Resource(this.texture!, FX.__uuidof(), (void**)d3D12Resource.GetAddressOf()); // Get the target back buffer to update ID3D12Resource* d3D12ResourceBackBuffer = this.currentBufferIndex switch diff --git a/samples/ComputeSharp.SwapChain/Program.cs b/samples/ComputeSharp.SwapChain/Program.cs index 2ab00306..a08a912c 100644 --- a/samples/ComputeSharp.SwapChain/Program.cs +++ b/samples/ComputeSharp.SwapChain/Program.cs @@ -11,15 +11,15 @@ namespace ComputeSharp.SwapChain /// private static readonly Win32Application[] Samples = new Win32Application[] { - new SwapChainApplication(static (texture, time) => new(texture, (float)time.TotalSeconds)), - new SwapChainApplication(static (texture, time) => new(texture, (float)time.TotalSeconds)), - new SwapChainApplication(static (texture, time) => new(texture, (float)time.TotalSeconds)), - new SwapChainApplication(static (texture, time) => new(texture, (float)time.TotalSeconds)), - new SwapChainApplication(static (texture, time) => new(texture, (float)time.TotalSeconds)), - new SwapChainApplication(static (texture, time) => new(texture, (float)time.TotalSeconds)), - new SwapChainApplication(static (texture, time) => new(texture, (float)time.TotalSeconds)), - new SwapChainApplication(static (texture, time) => new(texture, (float)time.TotalSeconds)), - new SwapChainApplication(static (texture, time) => new(texture, (float)time.TotalSeconds)) + new SwapChainApplication(static time => new((float)time.TotalSeconds)), + new SwapChainApplication(static time => new((float)time.TotalSeconds)), + new SwapChainApplication(static time => new((float)time.TotalSeconds)), + new SwapChainApplication(static time => new((float)time.TotalSeconds)), + new SwapChainApplication(static time => new((float)time.TotalSeconds)), + new SwapChainApplication(static time => new((float)time.TotalSeconds)), + new SwapChainApplication(static time => new((float)time.TotalSeconds)), + new SwapChainApplication(static time => new((float)time.TotalSeconds)), + new SwapChainApplication(static time => new((float)time.TotalSeconds)) }; static void Main() diff --git a/samples/ComputeSharp.SwapChain/Shaders/ColorfulInfinity.cs b/samples/ComputeSharp.SwapChain/Shaders/ColorfulInfinity.cs index 034330fa..267c2d55 100644 --- a/samples/ComputeSharp.SwapChain/Shaders/ColorfulInfinity.cs +++ b/samples/ComputeSharp.SwapChain/Shaders/ColorfulInfinity.cs @@ -7,13 +7,8 @@ /// License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. /// [AutoConstructor] - internal readonly partial struct ColorfulInfinity : IComputeShader + internal readonly partial struct ColorfulInfinity : IPixelShader { - /// - /// The target texture. - /// - public readonly IReadWriteTexture2D texture; - /// /// The current time since the start of the application. /// @@ -49,7 +44,7 @@ } /// - public void Execute() + public Float4 Execute() { Float2 uv = (ThreadIds.XY - (Float2)DispatchSize.XY * 0.5f) / DispatchSize.Y; Float3 col = 0; @@ -68,9 +63,7 @@ col *= new Float3(2, 1.0f, 2.0f); col = Hlsl.Pow(col, 0.5f); - Float4 color = new(col.X, col.Y, col.Z, 1.0f); - - texture[ThreadIds.XY] = color; + return new(col.X, col.Y, col.Z, 1.0f); } } } diff --git a/samples/ComputeSharp.SwapChain/Shaders/ExtrudedTruchetPattern.cs b/samples/ComputeSharp.SwapChain/Shaders/ExtrudedTruchetPattern.cs index c0d333f6..a66a3aef 100644 --- a/samples/ComputeSharp.SwapChain/Shaders/ExtrudedTruchetPattern.cs +++ b/samples/ComputeSharp.SwapChain/Shaders/ExtrudedTruchetPattern.cs @@ -6,13 +6,8 @@ /// Created by Shane. /// [AutoConstructor] - internal readonly partial struct ExtrudedTruchetPattern : IComputeShader + internal readonly partial struct ExtrudedTruchetPattern : IPixelShader { - /// - /// The target texture. - /// - public readonly IReadWriteTexture2D texture; - /// /// The current time since the start of the application. /// @@ -180,7 +175,7 @@ } /// - public void Execute() + public Float4 Execute() { Int2 coords = new(ThreadIds.X, DispatchSize.Y - ThreadIds.Y); Float2 u = (coords - (Float2)DispatchSize.XY * 0.5f) / DispatchSize.Y; @@ -292,7 +287,7 @@ c = Hlsl.Sqrt(Hlsl.Max(c, 0)); - texture[ThreadIds.XY] = c; + return c; } } } diff --git a/samples/ComputeSharp.SwapChain/Shaders/FractalTiling.cs b/samples/ComputeSharp.SwapChain/Shaders/FractalTiling.cs index 8831eaec..54b119e9 100644 --- a/samples/ComputeSharp.SwapChain/Shaders/FractalTiling.cs +++ b/samples/ComputeSharp.SwapChain/Shaders/FractalTiling.cs @@ -7,20 +7,15 @@ /// License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. /// [AutoConstructor] - internal readonly partial struct FractalTiling : IComputeShader + internal readonly partial struct FractalTiling : IPixelShader { - /// - /// The target texture. - /// - public readonly IReadWriteTexture2D texture; - /// /// The current time since the start of the application. /// public readonly float time; /// - public void Execute() + public Float4 Execute() { Float2 position = ((Float2)(256 * ThreadIds.XY)) / DispatchSize.X + time; Float4 color = 0; @@ -45,7 +40,7 @@ color.XYZ = Hlsl.Pow(color.XYZ, new Float3(0.7f, 0.8f, 0.5f)); color.W = 1.0f; - texture[ThreadIds.XY] = color; + return color; } } } diff --git a/samples/ComputeSharp.SwapChain/Shaders/MengerJourney.cs b/samples/ComputeSharp.SwapChain/Shaders/MengerJourney.cs index 9a594438..e56eb5db 100644 --- a/samples/ComputeSharp.SwapChain/Shaders/MengerJourney.cs +++ b/samples/ComputeSharp.SwapChain/Shaders/MengerJourney.cs @@ -6,13 +6,8 @@ /// Created by Syntopia. /// [AutoConstructor] - internal readonly partial struct MengerJourney : IComputeShader + internal readonly partial struct MengerJourney : IPixelShader { - /// - /// The target texture. - /// - public readonly IReadWriteTexture2D texture; - /// /// The current time since the start of the application. /// @@ -153,7 +148,7 @@ } /// - public void Execute() + public Float4 Execute() { Float3 camPos = 0.5f * time * new Float3(1.0f, 0.0f, 0.0f); Float3 target = camPos + new Float3(1.0f, 0.0f * Hlsl.Cos(time), 0.0f * Hlsl.Sin(0.4f * time)); @@ -169,7 +164,7 @@ Float3 rayDir = Hlsl.Normalize(camDir + (coord.X * camRight + coord.Y * camUp) * FieldOfView); - texture[ThreadIds.XY] = RayMarch(camPos, rayDir, ThreadIds.XY); + return RayMarch(camPos, rayDir, ThreadIds.XY); } } } diff --git a/samples/ComputeSharp.SwapChain/Shaders/Octagrams.cs b/samples/ComputeSharp.SwapChain/Shaders/Octagrams.cs index 4bca70ef..180eb7c3 100644 --- a/samples/ComputeSharp.SwapChain/Shaders/Octagrams.cs +++ b/samples/ComputeSharp.SwapChain/Shaders/Octagrams.cs @@ -6,13 +6,8 @@ /// Created by whisky_shusuky. /// [AutoConstructor] - internal readonly partial struct Octagrams : IComputeShader + internal readonly partial struct Octagrams : IPixelShader { - /// - /// The target texture. - /// - public readonly IReadWriteTexture2D texture; - /// /// The current time since the start of the application. /// @@ -88,7 +83,7 @@ } /// - public void Execute() + public Float4 Execute() { Float2 p = ((Float2)ThreadIds.XY * 2.0f - DispatchSize.XY) / Hlsl.Min(DispatchSize.X, DispatchSize.Y); Float3 ro = new(0.0f, -0.2f, time * 4.0f); @@ -122,7 +117,7 @@ Float3 col = ac * 0.02f + new Float3(0.0f, 0.2f * Hlsl.Abs(Hlsl.Sin(time)), 0.5f + Hlsl.Sin(time) * 0.2f); Float4 color = new(col, 1.0f - t * (0.02f + 0.02f * Hlsl.Sin(time))); - texture[ThreadIds.XY] = color; + return color; } } } diff --git a/samples/ComputeSharp.SwapChain/Shaders/ProteanClouds.cs b/samples/ComputeSharp.SwapChain/Shaders/ProteanClouds.cs index 5a4f4bc9..1374106c 100644 --- a/samples/ComputeSharp.SwapChain/Shaders/ProteanClouds.cs +++ b/samples/ComputeSharp.SwapChain/Shaders/ProteanClouds.cs @@ -7,13 +7,8 @@ /// License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. /// [AutoConstructor] - internal readonly partial struct ProteanClouds : IComputeShader + internal readonly partial struct ProteanClouds : IPixelShader { - /// - /// The target texture. - /// - public readonly IReadWriteTexture2D texture; - /// /// The current time Hlsl.Since the start of the application. /// @@ -131,7 +126,7 @@ } /// - public void Execute() + public Float4 Execute() { Float2 q = (Float2)ThreadIds.XY / DispatchSize.XY; Float2 p = (ThreadIds.XY - 0.5f * (Float2)DispatchSize.XY) / DispatchSize.Y; @@ -167,7 +162,7 @@ col = Hlsl.Pow(col, new Float3(0.55f, 0.65f, 0.6f)) * new Float3(1.0f, 0.97f, 0.9f); col *= Hlsl.Pow(16.0f * q.X * q.Y * (1.0f - q.X) * (1.0f - q.Y), 0.12f) * 0.7f + 0.3f; - texture[ThreadIds.XY] = new Float4(col, 1.0f); + return new(col, 1.0f); } } } diff --git a/samples/ComputeSharp.SwapChain/Shaders/PyramidPattern.cs b/samples/ComputeSharp.SwapChain/Shaders/PyramidPattern.cs index 8ce8d1b7..7f807140 100644 --- a/samples/ComputeSharp.SwapChain/Shaders/PyramidPattern.cs +++ b/samples/ComputeSharp.SwapChain/Shaders/PyramidPattern.cs @@ -7,13 +7,8 @@ /// Created by Shane. /// [AutoConstructor] - internal readonly partial struct PyramidPattern : IComputeShader + internal readonly partial struct PyramidPattern : IPixelShader { - /// - /// The target texture. - /// - public readonly IReadWriteTexture2D texture; - /// /// The current time Hlsl.Since the start of the application. /// @@ -135,7 +130,7 @@ } /// - public void Execute() + public Float4 Execute() { Int2 coordinate = new(ThreadIds.X, DispatchSize.Y - ThreadIds.Y); float iRes = Hlsl.Min(DispatchSize.Y, 800.0f); @@ -175,9 +170,7 @@ col *= hatch * 0.5f + 0.7f; - Float4 color = new(Hlsl.Sqrt(Hlsl.Max(col, 0.0f)), 1); - - texture[ThreadIds.XY] = color; + return new(Hlsl.Sqrt(Hlsl.Max(col, 0.0f)), 1); } } } diff --git a/samples/ComputeSharp.SwapChain/Shaders/TriangleGridContouring.cs b/samples/ComputeSharp.SwapChain/Shaders/TriangleGridContouring.cs index 122facb9..0d4dc01c 100644 --- a/samples/ComputeSharp.SwapChain/Shaders/TriangleGridContouring.cs +++ b/samples/ComputeSharp.SwapChain/Shaders/TriangleGridContouring.cs @@ -6,13 +6,8 @@ /// Created by Shane. /// [AutoConstructor] - internal readonly partial struct TriangleGridContouring : IComputeShader + internal readonly partial struct TriangleGridContouring : IPixelShader { - /// - /// The target texture. - /// - public readonly IReadWriteTexture2D texture; - /// /// The current time Hlsl.Since the start of the application. /// @@ -268,7 +263,7 @@ } /// - public void Execute() + public Float4 Execute() { Int2 coordinate = new(ThreadIds.X, DispatchSize.Y - ThreadIds.Y); Float2 uv = (coordinate - (Float2)DispatchSize.XY * 0.5f) / Hlsl.Min(650.0f, DispatchSize.Y); @@ -278,9 +273,7 @@ uv = coordinate / (Float2)DispatchSize.XY; col *= Hlsl.Pow(16.0f * uv.X * uv.Y * (1.0f - uv.X) * (1.0f - uv.Y), 0.0625f) + 0.1f; - Float4 color = new(Hlsl.Sqrt(Hlsl.Max(col, 0.0f)), 1); - - texture[ThreadIds.XY] = color; + return new(Hlsl.Sqrt(Hlsl.Max(col, 0.0f)), 1); } } } diff --git a/samples/ComputeSharp.SwapChain/Shaders/TwoTiledTruchet.cs b/samples/ComputeSharp.SwapChain/Shaders/TwoTiledTruchet.cs index d1d90f34..90cf73e1 100644 --- a/samples/ComputeSharp.SwapChain/Shaders/TwoTiledTruchet.cs +++ b/samples/ComputeSharp.SwapChain/Shaders/TwoTiledTruchet.cs @@ -6,13 +6,8 @@ /// Created by Shane. /// [AutoConstructor] - internal readonly partial struct TwoTiledTruchet : IComputeShader + internal readonly partial struct TwoTiledTruchet : IPixelShader { - /// - /// The target texture. - /// - public readonly IReadWriteTexture2D texture; - /// /// The current time since the start of the application. /// @@ -114,7 +109,7 @@ } /// - public void Execute() + public Float4 Execute() { Float2 uv = (ThreadIds.XY - (Float2)DispatchSize.XY * 0.5f) / DispatchSize.Y; float gSc = 7.0f; @@ -143,7 +138,7 @@ Float3 rgb = Hlsl.Sqrt(Hlsl.Max(col, 0.0f)); Float4 color = new(rgb.X, rgb.Y, rgb.Z, 1.0f); - texture[ThreadIds.XY] = color; + return color; } } }