From 48fcdb218d8a45a67daae99ea114c7ab001c2e1f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 9 Oct 2022 23:38:19 +0200 Subject: [PATCH] Add D2D1 Octagrams test --- .../Octagrams.cs | 2 +- .../Shaders/Octagrams.cs | 132 ++++++++++++++++++ tests/ComputeSharp.D2D1.Tests/ShadersTests.cs | 6 + .../Shaders/Compute/Octagrams.cs | 2 +- 4 files changed, 140 insertions(+), 2 deletions(-) create mode 100644 tests/ComputeSharp.D2D1.Tests/Shaders/Octagrams.cs diff --git a/samples/ComputeSharp.SwapChain.Shaders.Shared/Octagrams.cs b/samples/ComputeSharp.SwapChain.Shaders.Shared/Octagrams.cs index f10aa0c3..b9ed0603 100644 --- a/samples/ComputeSharp.SwapChain.Shaders.Shared/Octagrams.cs +++ b/samples/ComputeSharp.SwapChain.Shaders.Shared/Octagrams.cs @@ -119,7 +119,7 @@ internal readonly partial struct Octagrams : IPixelShader } float3 col = (ac * 0.02f) + new float3(0.0f, 0.2f * Hlsl.Abs(Hlsl.Sin(this.time)), 0.5f + (Hlsl.Sin(this.time) * 0.2f)); - float4 color = new(col, 1.0f - (t * (0.02f + (0.02f * Hlsl.Sin(this.time))))); + float4 color = new(col, 1); return color; } diff --git a/tests/ComputeSharp.D2D1.Tests/Shaders/Octagrams.cs b/tests/ComputeSharp.D2D1.Tests/Shaders/Octagrams.cs new file mode 100644 index 00000000..e1c1bc27 --- /dev/null +++ b/tests/ComputeSharp.D2D1.Tests/Shaders/Octagrams.cs @@ -0,0 +1,132 @@ +namespace ComputeSharp.D2D1.Tests.Shaders; + +/// +/// A shader creating an octagram animation, inspired by arabesque. +/// Ported from . +/// Created by whisky_shusuky. +/// +[D2DInputCount(0)] +[D2DRequiresScenePosition] +[D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] +[AutoConstructor] +internal readonly partial struct Octagrams : ID2D1PixelShader +{ + /// + /// The current time since the start of the application. + /// + public readonly float time; + + /// + /// The dispatch size for the current output. + /// + public readonly int2 dispatchSize; + + private static float2x2 Rotate(float a) + { + float c = Hlsl.Cos(a); + float s = Hlsl.Sin(a); + + return new(c, s, -s, c); + } + + private static float Box(float3 pos, float scale) + { + static float SDBox(float3 p, float3 b) + { + float3 q = Hlsl.Abs(p) - b; + + return Hlsl.Length(Hlsl.Max(q, 0.0f)) + + Hlsl.Min(Hlsl.Max(q.X, Hlsl.Max(q.Y, q.Z)), 0.0f); + } + + pos *= scale; + + float b = SDBox(pos, new(0.4f, 0.4f, 0.1f)) / 1.5f; + + pos.XY *= 5.0f; + pos.Y -= 3.5f; + pos.XY = Hlsl.Mul(pos.XY, Rotate(0.75f)); + + return -b; + } + + private static float BoxSet(float3 pos, float gTime) + { + float3 pos_origin = pos; + + pos = pos_origin; + pos.Y += Hlsl.Sin(gTime * 0.4f) * 2.5f; + pos.XY = Hlsl.Mul(pos.XY, Rotate(0.8f)); + + float box1 = Box(pos, 2.0f - (Hlsl.Abs(Hlsl.Sin(gTime * 0.4f)) * 1.5f)); + + pos = pos_origin; + pos.Y -= Hlsl.Sin(gTime * 0.4f) * 2.5f; + pos.XY = Hlsl.Mul(pos.XY, Rotate(0.8f)); + + float box2 = Box(pos, 2.0f - (Hlsl.Abs(Hlsl.Sin(gTime * 0.4f)) * 1.5f)); + + pos = pos_origin; + pos.X += Hlsl.Sin(gTime * 0.4f) * 2.5f; + pos.XY = Hlsl.Mul(pos.XY, Rotate(0.8f)); + + float box3 = Box(pos, 2.0f - (Hlsl.Abs(Hlsl.Sin(gTime * 0.4f)) * 1.5f)); + + pos = pos_origin; + pos.X -= Hlsl.Sin(gTime * 0.4f) * 2.5f; + pos.XY = Hlsl.Mul(pos.XY, Rotate(0.8f)); + + float box4 = Box(pos, 2.0f - (Hlsl.Abs(Hlsl.Sin(gTime * 0.4f)) * 1.5f)); + + pos = pos_origin; + pos.XY = Hlsl.Mul(pos.XY, Rotate(0.8f)); + + float box5 = Box(pos, 0.5f) * 6.0f; + + pos = pos_origin; + + float box6 = Box(pos, 0.5f) * 6.0f; + float result = Hlsl.Max(Hlsl.Max(Hlsl.Max(Hlsl.Max(Hlsl.Max(box1, box2), box3), box4), box5), box6); + + return result; + } + + /// + public float4 Execute() + { + int2 xy = (int2)D2D.GetScenePosition().XY; + float2 p = (((float2)xy * 2.0f) - this.dispatchSize) / Hlsl.Min(this.dispatchSize.X, this.dispatchSize.Y); + float3 ro = new(0.0f, -0.2f, this.time * 4.0f); + float3 ray = Hlsl.Normalize(new float3(p, 1.5f)); + + ray.XY = Hlsl.Mul(ray.XY, Rotate(Hlsl.Sin(this.time * 0.03f) * 5.0f)); + ray.YZ = Hlsl.Mul(ray.YZ, Rotate(Hlsl.Sin(this.time * 0.05f) * 0.2f)); + + float t = 0.1f; + float ac = 0.0f; + + for (int i = 0; i < 99; i++) + { + static float3 Mod(float3 x, float y) + { + return x - (y * Hlsl.Floor(x / y)); + } + + float3 pos = ro + (ray * t); + + pos = Mod(pos - 2.0f, 4.0f) - 2.0f; + + float gTime = this.time - (i * 0.01f); + float d = BoxSet(pos, gTime); + + d = Hlsl.Max(Hlsl.Abs(d), 0.01f); + ac += Hlsl.Exp(-d * 23.0f); + t += d * 0.55f; + } + + float3 col = (ac * 0.02f) + new float3(0.0f, 0.2f * Hlsl.Abs(Hlsl.Sin(this.time)), 0.5f + (Hlsl.Sin(this.time) * 0.2f)); + float4 color = new(col, 1); + + return color; + } +} \ No newline at end of file diff --git a/tests/ComputeSharp.D2D1.Tests/ShadersTests.cs b/tests/ComputeSharp.D2D1.Tests/ShadersTests.cs index 6cfb3e88..36e345f4 100644 --- a/tests/ComputeSharp.D2D1.Tests/ShadersTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/ShadersTests.cs @@ -32,6 +32,12 @@ public class ShadersTests RunTest(0.000011f); } + [TestMethod] + public void Octagrams() + { + RunTest(); + } + private static void RunTest(float threshold = 0.00001f) where T : unmanaged, ID2D1PixelShader { diff --git a/tests/ComputeSharp.Tests/Shaders/Compute/Octagrams.cs b/tests/ComputeSharp.Tests/Shaders/Compute/Octagrams.cs index a2da9f97..5c4b9141 100644 --- a/tests/ComputeSharp.Tests/Shaders/Compute/Octagrams.cs +++ b/tests/ComputeSharp.Tests/Shaders/Compute/Octagrams.cs @@ -123,7 +123,7 @@ internal readonly partial struct Octagrams : IComputeShader } 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))); + float4 color = new(col, 1); texture[ThreadIds.XY] = color; }