diff --git a/samples/ComputeSharp.SwapChain.Shaders.Shared/ContouredLayers.cs b/samples/ComputeSharp.SwapChain.Shaders.Shared/ContouredLayers.cs index 04b6f7ab..aaa46d77 100644 --- a/samples/ComputeSharp.SwapChain.Shaders.Shared/ContouredLayers.cs +++ b/samples/ComputeSharp.SwapChain.Shaders.Shared/ContouredLayers.cs @@ -117,7 +117,7 @@ internal readonly partial struct ContouredLayers : IPixelShader float3 col; - col = (float3)1.0 * (1.0f - (0.75f / (1.0f + (sh * sh * 2.0f)))); + col = (float3)1.0f * (1.0f - (0.75f / (1.0f + (sh * sh * 2.0f)))); col = Hlsl.Min(col * tx * 3.0f, 1.0f); return col; 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/Assets/ContouredLayers.png b/tests/ComputeSharp.D2D1.Tests/Assets/ContouredLayers.png new file mode 100644 index 00000000..a27d462b Binary files /dev/null and b/tests/ComputeSharp.D2D1.Tests/Assets/ContouredLayers.png differ diff --git a/tests/ComputeSharp.D2D1.Tests/Assets/PyramidPattern.png b/tests/ComputeSharp.D2D1.Tests/Assets/PyramidPattern.png new file mode 100644 index 00000000..b2e39238 Binary files /dev/null and b/tests/ComputeSharp.D2D1.Tests/Assets/PyramidPattern.png differ diff --git a/tests/ComputeSharp.D2D1.Tests/Assets/TriangleGridContouring.png b/tests/ComputeSharp.D2D1.Tests/Assets/TriangleGridContouring.png new file mode 100644 index 00000000..9dc85bd3 Binary files /dev/null and b/tests/ComputeSharp.D2D1.Tests/Assets/TriangleGridContouring.png differ diff --git a/tests/ComputeSharp.D2D1.Tests/Assets/TwoTiledTruchet.png b/tests/ComputeSharp.D2D1.Tests/Assets/TwoTiledTruchet.png new file mode 100644 index 00000000..055de623 Binary files /dev/null and b/tests/ComputeSharp.D2D1.Tests/Assets/TwoTiledTruchet.png differ diff --git a/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj b/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj index 845b731f..55bf1820 100644 --- a/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj +++ b/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj @@ -59,6 +59,40 @@ PreserveNewest + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + diff --git a/tests/ComputeSharp.D2D1.Tests/Effects/BokehBlurEffect.cs b/tests/ComputeSharp.D2D1.Tests/Effects/BokehBlurEffect.cs index ab224b80..39e3c3f7 100644 --- a/tests/ComputeSharp.D2D1.Tests/Effects/BokehBlurEffect.cs +++ b/tests/ComputeSharp.D2D1.Tests/Effects/BokehBlurEffect.cs @@ -5,7 +5,6 @@ using System.IO; using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using ComputeSharp.D2D1; using ComputeSharp.D2D1.Interop; using ComputeSharp.D2D1.Tests.Extensions; using ComputeSharp.D2D1.Tests.Helpers; @@ -15,7 +14,7 @@ using Win32.Graphics.Direct2D; #pragma warning disable IDE0065 -namespace ComputeSharp.BokehBlur.Processors; +namespace ComputeSharp.D2D1.Tests.Effects; using D2D1_BORDER_EDGE_MODE = BorderEdgeMode; using D2D1_BORDER_PROP = BorderProp; diff --git a/tests/ComputeSharp.D2D1.Tests/EndToEndTests.cs b/tests/ComputeSharp.D2D1.Tests/EffectsTests.cs similarity index 90% rename from tests/ComputeSharp.D2D1.Tests/EndToEndTests.cs rename to tests/ComputeSharp.D2D1.Tests/EffectsTests.cs index c950d65c..2063222d 100644 --- a/tests/ComputeSharp.D2D1.Tests/EndToEndTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/EffectsTests.cs @@ -1,6 +1,5 @@ using System.IO; using System.Reflection; -using ComputeSharp.BokehBlur.Processors; using ComputeSharp.D2D1.Interop; using ComputeSharp.D2D1.Tests.Effects; using ComputeSharp.D2D1.Tests.Helpers; @@ -13,8 +12,8 @@ using SixLabors.ImageSharp.Processing; namespace ComputeSharp.D2D1.Tests; [TestClass] -[TestCategory("EndToEnd")] -public class EndToEndTests +[TestCategory("Effects")] +public class EffectsTests { [AssemblyInitialize] public static void ConfigureImageSharp(TestContext _) @@ -23,19 +22,19 @@ public class EndToEndTests } [TestMethod] - public unsafe void Invert() + public void Invert() { D2D1TestRunner.RunAndCompareShader(new InvertEffect(), null, "Landscape.png", "Landscape_Inverted.png"); } [TestMethod] - public unsafe void InvertWithThreshold() + public void InvertWithThreshold() { D2D1TestRunner.RunAndCompareShader(new InvertWithThresholdEffect(1), null, "Landscape.png", "Landscape_Inverted.png"); } [TestMethod] - public unsafe void Pixelate() + public void Pixelate() { D2D1TestRunner.RunAndCompareShader( new PixelateEffect(new PixelateEffect.Constants(1280, 840, 16)), @@ -45,13 +44,13 @@ public class EndToEndTests } [TestMethod] - public unsafe void ZonePlate() + public void ZonePlate() { D2D1TestRunner.RunAndCompareShader(new ZonePlateEffect(1280, 720, 800), 1280, 720, "ZonePlate.png"); } [TestMethod] - public unsafe void CheckerboardClip() + public void CheckerboardClip() { D2D1TestRunner.RunAndCompareShader(new CheckerboardClipEffect(1280, 840, 32), null, "Landscape.png", "Landscape_CheckerboardClip.png"); } diff --git a/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs b/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs index 6cc264f7..35ce6288 100644 --- a/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs +++ b/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs @@ -27,12 +27,14 @@ internal static class D2D1TestRunner /// The name of the expected result image. /// The name of the destination image to save results to. /// The shader to run. + /// The allowed difference threshold for the normalized delta. public static void RunAndCompareShader( in T shader, ID2D1TransformMapperFactory? transformMapperFactory, string originalFileName, string expectedFileName, - [CallerMemberName] string destinationFileName = "") + [CallerMemberName] string destinationFileName = "", + float threshold = 0.00001f) where T : unmanaged, ID2D1PixelShader { string assetsPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Assets"); @@ -52,7 +54,7 @@ internal static class D2D1TestRunner destinationPath); // Compare the results - TolerantImageComparer.AssertEqual(destinationPath, expectedPath, 0.00001f); + TolerantImageComparer.AssertEqual(destinationPath, expectedPath, threshold); } /// @@ -64,12 +66,16 @@ internal static class D2D1TestRunner /// The name of the expected result image. /// The name of the destination image to save results to. /// The shader to run. + /// The allowed difference threshold for the normalized delta. + /// The additional resource textures to use to run the shader. public static void RunAndCompareShader( in T shader, int width, int height, string expectedFileName, - [CallerMemberName] string destinationFileName = "") + [CallerMemberName] string destinationFileName = "", + float threshold = 0.00001f, + params (int Index, D2D1ResourceTextureManager ResourceTextureManager)[] resourceTextures) where T : unmanaged, ID2D1PixelShader { string assetsPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Assets"); @@ -86,10 +92,11 @@ internal static class D2D1TestRunner null, width, height, - destinationPath); + destinationPath, + resourceTextures); // Compare the results - TolerantImageComparer.AssertEqual(destinationPath, expectedPath, 0.00001f); + TolerantImageComparer.AssertEqual(destinationPath, expectedPath, threshold); } /// @@ -141,12 +148,14 @@ internal static class D2D1TestRunner /// The resulting height. /// A custom factory for the effect. /// The destination path for the result. + /// The additional resource textures to use to run the shader. private static unsafe void ExecutePixelShaderAndSaveToFile( in T shader, ID2D1TransformMapperFactory? transformMapperFactory, int width, int height, - string destinationPath) + string destinationPath, + params (int Index, D2D1ResourceTextureManager ResourceTextureManager)[] resourceTextures) where T : unmanaged, ID2D1PixelShader { using ComPtr d2D1Factory2 = D2D1Helper.CreateD2D1Factory2(); @@ -161,6 +170,11 @@ internal static class D2D1TestRunner D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(in shader, d2D1Effect.Get()); + foreach ((int index, D2D1ResourceTextureManager resourceTextureManager) in resourceTextures) + { + D2D1PixelShaderEffect.SetResourceTextureManagerForD2D1Effect(d2D1Effect.Get(), resourceTextureManager, index); + } + using ComPtr d2D1BitmapTarget = D2D1Helper.CreateD2D1BitmapAndSetAsTarget(d2D1DeviceContext.Get(), (uint)width, (uint)height); D2D1Helper.DrawEffect(d2D1DeviceContext.Get(), d2D1Effect.Get()); diff --git a/tests/ComputeSharp.D2D1.Tests/Shaders/ColorfulInfinity.cs b/tests/ComputeSharp.D2D1.Tests/Shaders/ColorfulInfinity.cs new file mode 100644 index 00000000..06a496f0 --- /dev/null +++ b/tests/ComputeSharp.D2D1.Tests/Shaders/ColorfulInfinity.cs @@ -0,0 +1,77 @@ +namespace ComputeSharp.D2D1.Tests.Shaders; + +/// +/// A shader creating an abstract and colorful animation. +/// Ported from . +/// Created by Benoit Marini. +/// License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. +/// +[D2DInputCount(0)] +[D2DRequiresScenePosition] +[D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] +[AutoConstructor] +internal readonly partial struct ColorfulInfinity : 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; + + /// + /// The total number of layers for the final animation. + /// + private const float NumberOfLayers = 16.0f; + + /// + /// The number of iterations to calculate a texel value. + /// + private const int NumberOfIterations = 23; + + /// + /// Makes some magic happen. + /// + private float4 Tex(float3 p) + { + float t = this.time + 78.0f; + float4 o = new(p.X, p.Y, p.Z, 3.0f * Hlsl.Sin(t * 0.1f)); + float4 dec = + new float4(1.0f, 0.9f, 0.1f, 0.15f) + + new float4(0.06f * Hlsl.Cos(t * 0.1f), 0, 0, 0.14f * Hlsl.Cos(t * 0.23f)); + + for (int i = 0; i++ < NumberOfIterations;) + { + o.XZYW = Hlsl.Abs((o / Hlsl.Dot(o, o)) - dec); + } + + return o; + } + + /// + public float4 Execute() + { + int2 xy = (int2)D2D.GetScenePosition().XY; + float2 uv = (xy - ((float2)this.dispatchSize * 0.5f)) / this.dispatchSize.Y; + float3 col = 0; + float t = this.time * 0.3f; + + for (float i = 0.0f; i <= 1.0f; i += 1.0f / NumberOfLayers) + { + float d = Hlsl.Frac(i + t); + float s = Hlsl.Lerp(5.0f, 0.5f, d); + float f = d * Hlsl.SmoothStep(1.0f, 0.9f, d); + + col += Tex(new float3(uv.X * s, uv.Y * s, i * 4.0f)).XYZ * f; + } + + col /= NumberOfLayers; + col *= new float3(2, 1.0f, 2.0f); + col = Hlsl.Pow(col, 0.5f); + + return new(col.X, col.Y, col.Z, 1.0f); + } +} \ No newline at end of file diff --git a/tests/ComputeSharp.D2D1.Tests/Shaders/ContouredLayers.cs b/tests/ComputeSharp.D2D1.Tests/Shaders/ContouredLayers.cs new file mode 100644 index 00000000..c5af9d3a --- /dev/null +++ b/tests/ComputeSharp.D2D1.Tests/Shaders/ContouredLayers.cs @@ -0,0 +1,209 @@ +namespace ComputeSharp.D2D1.Tests.Shaders; + +/// +/// Constructing some concise contoured layers, then applying various edge and shading effects to produce some faux depth. +/// Ported from . +/// Created by Shane. +/// +[D2DInputCount(0)] +[D2DRequiresScenePosition] +[D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] +[AutoConstructor] +internal readonly partial struct ContouredLayers : ID2D1PixelShader +{ + /// + /// The current time Hlsl.Since the start of the application. + /// + private readonly float time; + + /// + /// The dispatch size for the current output. + /// + public readonly int2 dispatchSize; + + /// + /// The background texture to sample. + /// + [D2DResourceTextureIndex(0)] + private readonly D2D1ResourceTexture2D texture; + + // float3 to float hash. + private static float Hash21(float2 p) + { + return Hlsl.Frac(Hlsl.Sin(Hlsl.Dot(p, new float2(41, 289))) * 45758.5453f); + } + + // float2 to float2 hash. + private float2 Hash22(float2 p) + { + float n = Hlsl.Sin(Hlsl.Dot(p, new float2(1, 113))); + + p = Hlsl.Frac(new float2(262144, 32768) * n); + + return Hlsl.Sin((p * 6.2831853f) + this.time); + } + + // float2 to float2 hash. + private static float2 Hash22B(float2 p) + { + float n = Hlsl.Sin(Hlsl.Dot(p, new float2(41, 289))); + + return (Hlsl.Frac(new float2(262144, 32768) * n) * 2.0f) - 1.0f; + } + + // Smooth 2D noise + private static float Noise2D(float2 p) + { + float2 i = Hlsl.Floor(p); + + p -= i; + + float4 v = default; + + v.X = Hlsl.Dot(Hash22B(i), p); + v.Y = Hlsl.Dot(Hash22B(i + float2.UnitX), p - float2.UnitX); + v.Z = Hlsl.Dot(Hash22B(i + float2.UnitY), p - float2.UnitY); + v.W = Hlsl.Dot(Hash22B(i + 1.0f), p - 1.0f); + + p = p * p * (3.0f - (2.0f * p)); + + return Hlsl.Lerp(Hlsl.Lerp(v.X, v.Y, p.X), Hlsl.Lerp(v.Z, v.W, p.X), p.Y); + } + + // Based on IQ's gradient noise formula. + private float Noise2D3G(float2 p) + { + float2 i = Hlsl.Floor(p); + + p -= i; + + float4 v = default; + + v.X = Hlsl.Dot(Hash22(i), p); + v.Y = Hlsl.Dot(Hash22(i + float2.UnitX), p - float2.UnitX); + v.Z = Hlsl.Dot(Hash22(i + float2.UnitY), p - float2.UnitY); + v.W = Hlsl.Dot(Hash22(i + 1.0f), p - 1.0f); + + p = p * p * p * ((p * ((p * 6.0f) - 15.0f)) + 10.0f); + + return Hlsl.Lerp(Hlsl.Lerp(v.X, v.Y, p.X), Hlsl.Lerp(v.Z, v.W, p.X), p.Y); + } + + // Map function with noise layers + private float MapNoise(float3 p, float i) + { + return (Noise2D3G(p.XY * 3.0f) * 0.66f) + (Noise2D3G(p.XY * 6.0f) * 0.34f) + (i / 10.0f * 1.0f) - 0.15f; + } + + // 2D derivative function. + private float3 GetNormal(in float3 p, float m, float i) + { + float2 e = new(0.001f, 0); + + return new float3(m - MapNoise(p - e.XYY, i), m - MapNoise(p - e.YXY, i), 0.0f) / e.X * 1.4142f; + } + + // The map layer and its derivative + private float4 MapLayer(in float3 p, float i) + { + float4 d = default; + + d.X = MapNoise(p, i); + d.YZW = GetNormal(p, d.X, i); + + return d; + } + + // Layer color. Based on the shade, layer number and smoothing factor. + private float3 GetColor(float2 p, float sh, float fi) + { + float3 tx = this.texture.Sample(p + Hash21(new float2(sh, fi))).XYZ; + + tx *= tx; + + float3 col; + + col = (float3)1.0f * (1.0f - (0.75f / (1.0f + (sh * sh * 2.0f)))); + col = Hlsl.Min(col * tx * 3.0f, 1.0f); + + return col; + } + + // A hatch-like algorithm, or a stipple... or some kind of textured pattern. + private float Hatch(float2 p, float res) + { + p *= res / 16.0f; + + float hatch = Hlsl.Clamp((Hlsl.Sin((p.X - p.Y) * 3.14159f * 200.0f) * 2.0f) + 0.5f, 0.0f, 1.0f); + + float hRnd = Hash21(Hlsl.Floor(p * 6.0f) + 0.73f); + if (hRnd > 0.66f) + { + hatch = hRnd; + } + + hatch = (hatch * 0.75f) + 0.5f; + + return hatch; + } + + /// + public float4 Execute() + { + int2 xy = (int2)D2D.GetScenePosition().XY; + float2 fragCoord = new(xy.X, this.dispatchSize.Y - xy.Y); + float res = Hlsl.Min(this.dispatchSize.Y, 700.0f); + float2 uv = (fragCoord - ((float2)this.dispatchSize * 0.5f)) / res; + float sf = 1.0f / this.dispatchSize.Y; + float3 col = GetColor(uv, 0.0f, 0.0f); + float pL = 0.0f; + float hatch = Hatch(uv, res); + + col *= hatch; + + const int lNum = 5; + float flNum = lNum; + + for (int i = 0; i < lNum; i++) + { + float fi = i; + + hatch = Hatch(uv + Hlsl.Sin(new float2(41.0f, 289.0f) * (fi + 1.0f)), res); + + float4 c = MapLayer(new float3(uv, 1.0f), fi); + float4 cSh = MapLayer(new float3(uv - (new float2(0.03f, -0.03f) * (((flNum - fi) / flNum * 0.5f) + 0.5f)), 1.0f), fi); + float sh = (fi + 1.0f) / flNum; + float3 lCol = GetColor(uv, sh, fi + 1.0f); + float3 ld = Hlsl.Normalize(new float3(-1, 1, -0.25f)); + float3 n = Hlsl.Normalize(new float3(0.0f, 0.0f, -1.0f) + c.YZW); + float diff = Hlsl.Max(Hlsl.Dot(ld, n), 0.0f); + + diff *= 2.5f; + + float3 eCol = lCol * (diff + 1.0f); + float sfL = sf * Hlsl.Length(c.YZX) * 2.0f; + float sfLSh = sf * Hlsl.Length(cSh.YZX) * 6.0f; + const float shF = 0.5f; + + col = Hlsl.Lerp(col, float3.Zero, (1.0f - Hlsl.SmoothStep(0.0f, sfLSh, Hlsl.Max(cSh.X, pL))) * shF); + col = Hlsl.Lerp(col, float3.Zero, (1.0f - Hlsl.SmoothStep(0.0f, sfL * 3.0f, c.X)) * 0.25f); + col = Hlsl.Lerp(col, float3.Zero, (1.0f - Hlsl.SmoothStep(0.0f, sfL, c.X)) * 0.85f); + col = Hlsl.Lerp(col, eCol * hatch, 1.0f - Hlsl.SmoothStep(0.0f, sfL, c.X + (Hlsl.Length(c.YZX) * 0.003f))); + col = Hlsl.Lerp(col, lCol * hatch, 1.0f - Hlsl.SmoothStep(0.0f, sfL, c.X + (Hlsl.Length(c.YZX) * 0.006f))); + + pL = c.X; + } + + col *= Hlsl.Lerp(new float3(1.8f, 1, 0.7f).ZYX, new float3(1.8f, 1, 0.7f).XZY, Noise2D(uv * 2.0f)); + + float3 rn3 = Noise2D((uv * this.dispatchSize.Y / 1.0f) + 1.7f) - Noise2D((uv * this.dispatchSize.Y / 1.0f) + 3.4f); + + col *= 0.93f + (0.07f * rn3.XYZ) + (0.07f * Hlsl.Dot(rn3, new float3(0.299f, 0.587f, 0.114f))); + + uv = fragCoord / this.dispatchSize.XY; + + col *= Hlsl.Pow(Hlsl.Abs(16.0f * uv.X * uv.Y * (1.0f - uv.X) * (1.0f - uv.Y)), 0.0625f); + + return new float4(Hlsl.Sqrt(Hlsl.Max(col, 0.0f)), 1); + } +} \ No newline at end of file diff --git a/tests/ComputeSharp.D2D1.Tests/Shaders/FractalTiling.cs b/tests/ComputeSharp.D2D1.Tests/Shaders/FractalTiling.cs new file mode 100644 index 00000000..191a2d7e --- /dev/null +++ b/tests/ComputeSharp.D2D1.Tests/Shaders/FractalTiling.cs @@ -0,0 +1,54 @@ +namespace ComputeSharp.D2D1.Tests.Shaders; + +/// +/// A shader creating a fractal tiling animation. +/// Ported from . +/// Created by Inigo Quilez. +/// License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. +/// +[D2DInputCount(0)] +[D2DRequiresScenePosition] +[D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] +[AutoConstructor] +internal readonly partial struct FractalTiling : 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; + + /// + public float4 Execute() + { + int2 xy = (int2)D2D.GetScenePosition().XY; + float2 position = (((float2)(256 * xy)) / this.dispatchSize.X) + this.time; + float4 color = 0; + + for (int i = 0; i < 6; i++) + { + float2 a = Hlsl.Floor(position); + float2 b = Hlsl.Frac(position); + float4 w = Hlsl.Frac( + (Hlsl.Sin((a.X * 7) + (31.0f * a.Y) + (0.01f * this.time)) + + new float4(0.035f, 0.01f, 0, 0.7f)) + * 13.545317f); + + color.XYZ += w.XYZ * + 2.0f * Hlsl.SmoothStep(0.45f, 0.55f, w.W) * + Hlsl.Sqrt(16.0f * b.X * b.Y * (1.0f - b.X) * (1.0f - b.Y)); + + position /= 2.0f; + color /= 2.0f; + } + + color.XYZ = Hlsl.Pow(color.XYZ, new float3(0.7f, 0.8f, 0.5f)); + color.W = 1.0f; + + return color; + } +} \ No newline at end of file diff --git a/tests/ComputeSharp.D2D1.Tests/Shaders/HelloWorld.cs b/tests/ComputeSharp.D2D1.Tests/Shaders/HelloWorld.cs new file mode 100644 index 00000000..96f0ed79 --- /dev/null +++ b/tests/ComputeSharp.D2D1.Tests/Shaders/HelloWorld.cs @@ -0,0 +1,37 @@ +namespace ComputeSharp.D2D1.Tests.Shaders; + +/// +/// A simple shader to get started with based on shadertoy new shader template. +/// Ported from . +/// +[D2DInputCount(0)] +[D2DRequiresScenePosition] +[D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] +[AutoConstructor] +internal readonly partial struct HelloWorld : 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; + + /// + public float4 Execute() + { + int2 xy = (int2)D2D.GetScenePosition().XY; + + // Normalized screen space UV coordinates from 0.0 to 1.0 + float2 uv = xy / (float2)this.dispatchSize; + + // Time varying pixel color + float3 col = 0.5f + (0.5f * Hlsl.Cos(this.time + new float3(uv, uv.X) + new float3(0, 2, 4))); + + // Output to screen + return new(col, 1f); + } +} \ No newline at end of file diff --git a/tests/ComputeSharp.D2D1.Tests/Shaders/MengerJourney.cs b/tests/ComputeSharp.D2D1.Tests/Shaders/MengerJourney.cs new file mode 100644 index 00000000..eaadc5bb --- /dev/null +++ b/tests/ComputeSharp.D2D1.Tests/Shaders/MengerJourney.cs @@ -0,0 +1,183 @@ +namespace ComputeSharp.D2D1.Tests.Shaders; + +/// +/// A shader creating a flythrough in a Menger system. +/// Ported from . +/// Created by Syntopia. +/// +[D2DInputCount(0)] +[D2DRequiresScenePosition] +[D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] +[AutoConstructor] +internal readonly partial struct MengerJourney : 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 const int MaxSteps = 30; + private const float MinimumDistance = 0.0009f; + private const float NormalDistance = 0.0002f; + private const int Iterations = 7; + private const float Scale = 3.0f; + private const float FieldOfView = 1.0f; + private const float Jitter = 0.05f; + private const float FudgeFactor = 0.7f; + private const float NonLinearPerspective = 2.0f; + private const float Ambient = 0.32184f; + private const float Diffuse = 0.5f; + + private static readonly float3 LightDir = 1.0f; + private static readonly float3 LightColor = new(1.0f, 1.0f, 0.858824f); + private static readonly float3 LightDir2 = new(1.0f, -1.0f, 1.0f); + private static readonly float3 LightColor2 = new(0, 0.333333f, 1.0f); + private static readonly float3 Offset = new(0.92858f, 0.92858f, 0.32858f); + + /// + /// Rotates a given value by a specified amount. + /// + private static float2 Rotate(float2 v, float a) + { + return new((Hlsl.Cos(a) * v.X) + (Hlsl.Sin(a) * v.Y), (-Hlsl.Sin(a) * v.X) + (Hlsl.Cos(a) * v.Y)); + } + + /// + /// Calculates the light at a specified normal and starting color. + /// Uses two light sources and no specular lights. + /// + private static float3 GetLight(in float3 color, in float3 normal) + { + float3 lightDir = Hlsl.Normalize(LightDir); + float diffuse = Hlsl.Max(0.0f, Hlsl.Dot(-normal, lightDir)); + + float3 lightDir2 = Hlsl.Normalize(LightDir2); + float diffuse2 = Hlsl.Max(0.0f, Hlsl.Dot(-normal, lightDir2)); + + return + (diffuse * Diffuse * (LightColor * color)) + + (diffuse2 * Diffuse * (LightColor2 * color)); + } + + /// + /// DE: Infinitely tiled Menger IFS. + /// For more info on KIFS, see + /// + private float DE(float3 z) + { + // Performs the GLSL mod function. + static float3 Mod(float3 x, float y) + { + return x - (y * Hlsl.Floor(x / y)); + } + + z = Hlsl.Abs(1.0f - Mod(z, 2.0f)); + + float d = 1000.0f; + + for (int n = 0; n < Iterations; n++) + { + z.XY = Rotate(z.XY, 4.0f + (2.0f * Hlsl.Cos(this.time / 8.0f))); + z = Hlsl.Abs(z); + + if (z.X < z.Y) { z.XY = z.YX; } + + if (z.X < z.Z) { z.XZ = z.ZX; } + + if (z.Y < z.Z) { z.YZ = z.ZY; } + + z = (Scale * z) - (Offset * (Scale - 1.0f)); + + if (z.Z < -0.5f * Offset.Z * (Scale - 1.0)) + { + z.Z += Offset.Z * (Scale - 1.0f); + } + + d = Hlsl.Min(d, Hlsl.Length(z) * Hlsl.Pow(Scale, -n - 1.0f)); + } + + return d - 0.001f; + } + + /// + /// Calculates the finite difference normal at a given position. + /// + private float3 getNormal(in float3 pos) + { + float3 e = new(0.0f, NormalDistance, 0.0f); + + return Hlsl.Normalize(new float3( + DE(pos + e.YXX) - DE(pos - e.YXX), + DE(pos + e.XYX) - DE(pos - e.XYX), + DE(pos + e.XXY) - DE(pos - e.XXY))); + } + + /// + /// Performs a ray march operation on a specific position. + /// + private float4 RayMarch(in float3 from, float3 dir, in float2 fragCoord) + { + // Computes a pseudo-random number + static float Rand(float2 co) + { + return Hlsl.Frac(Hlsl.Cos(Hlsl.Dot(co, new float2(4.898f, 7.23f))) * 23421.631f); + } + + float totalDistance = Jitter * Rand(fragCoord.XY + this.time); + float3 dir2 = dir; + float distance = 0; + int steps = 0; + float3 pos = 0; + + for (int i = 0; i < MaxSteps; i++) + { + dir.ZY = Rotate(dir2.ZY, totalDistance * Hlsl.Cos(this.time / 4.0f) * NonLinearPerspective); + pos = from + (totalDistance * dir); + distance = DE(pos) * FudgeFactor; + totalDistance += distance; + + if (distance < MinimumDistance) + { + break; + } + + steps = i; + } + + float smoothStep = steps + (distance / MinimumDistance); + float ao = 1.1f - (smoothStep / MaxSteps); + float3 normal = getNormal(pos - (dir * NormalDistance * 3.0f)); + float3 color = 1.0f; + float3 light = GetLight(color, normal); + + color = ((color * Ambient) + light) * ao; + + return new(color.X, color.Y, color.Z, 1.0f); + } + + /// + public float4 Execute() + { + int2 xy = (int2)D2D.GetScenePosition().XY; + float3 camPos = 0.5f * this.time * new float3(1.0f, 0.0f, 0.0f); + float3 target = camPos + new float3(1.0f, 0.0f * Hlsl.Cos(this.time), 0.0f * Hlsl.Sin(0.4f * this.time)); + float3 camDir = Hlsl.Normalize(target - camPos); + float3 camUp = new(0.0f, 1.0f, 0.0f); + + camUp = Hlsl.Normalize(camUp - (Hlsl.Dot(camDir, camUp) * camDir)); + + float3 camRight = Hlsl.Normalize(Hlsl.Cross(camDir, camUp)); + float2 coord = -1.0f + (2.0f * (float2)xy / (float2)this.dispatchSize); + + coord.X *= this.dispatchSize.X / (float)this.dispatchSize.Y; + + float3 rayDir = Hlsl.Normalize(camDir + (((coord.X * camRight) + (coord.Y * camUp)) * FieldOfView)); + + return RayMarch(camPos, rayDir, xy); + } +} \ No newline at end of file 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/Shaders/ProteanClouds.cs b/tests/ComputeSharp.D2D1.Tests/Shaders/ProteanClouds.cs new file mode 100644 index 00000000..0eeabef3 --- /dev/null +++ b/tests/ComputeSharp.D2D1.Tests/Shaders/ProteanClouds.cs @@ -0,0 +1,180 @@ +namespace ComputeSharp.D2D1.Tests.Shaders; + +/// +/// Fully procedural 3D animated volume with three evaluations per step (for shading). +/// Ported from . +/// Created by nimitz (twitter: @stormoid). +/// License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. +/// +[D2DInputCount(0)] +[D2DRequiresScenePosition] +[D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] +[AutoConstructor] +internal readonly partial struct ProteanClouds : ID2D1PixelShader +{ + /// + /// The current time Hlsl.Since the start of the application. + /// + public readonly float time; + + /// + /// The dispatch size for the current output. + /// + public readonly int2 dispatchSize; + + private static readonly float3x3 m3 = new float3x3(0.33338f, 0.56034f, -0.71817f, -0.87887f, 0.32651f, -0.15323f, 0.15162f, 0.69596f, 0.61339f) * 1.93f; + + private static float2x2 Rotate(in float a) + { + float c = Hlsl.Cos(a); + float s = Hlsl.Sin(a); + + return new(c, s, -s, c); + } + + private static float LinStep(in float mn, in float mx, in float x) + { + return Hlsl.Clamp((x - mn) / (mx - mn), 0.0f, 1.0f); + } + + private static float2 Disp(float t) + { + return new float2(Hlsl.Sin(t * 0.22f) * 1.0f, Hlsl.Cos(t * 0.175f) * 1.0f) * 2.0f; + } + + private float2 Map(float3 p, float prm1, float2 bsMo) + { + float3 p2 = p; + + p2.XY -= Disp(p.Z).XY; + p.XY = Hlsl.Mul(p.XY, Rotate((Hlsl.Sin(p.Z + this.time) * (0.1f + (prm1 * 0.05f))) + (this.time * 0.09f))); + + float cl = Hlsl.Dot(p2.XY, p2.XY); + float d = 0.0f; + + p *= 0.61f; + + float z = 1.0f; + float trk = 1.0f; + float dspAmp = 0.1f + (prm1 * 0.2f); + + for (int i = 0; i < 5; i++) + { + p += Hlsl.Sin((p.ZXY * 0.75f * trk) + (this.time * trk * 0.8f)) * dspAmp; + d -= Hlsl.Abs(Hlsl.Dot(Hlsl.Cos(p), Hlsl.Sin(p.YZX)) * z); + z *= 0.57f; + trk *= 1.4f; + + p = Hlsl.Mul(p, m3); + } + + d = Hlsl.Abs(d + (prm1 * 3.0f)) + (prm1 * 0.3f) - 2.5f + bsMo.Y; + + return new float2(d + (cl * 0.2f) + 0.25f, cl); + } + + private float4 Render(float3 ro, float3 rd, float time, float prm1, float2 bsMo) + { + float4 rez = 0; + float t = 1.5f; + float fogT = 0; + + for (int i = 0; i < 130; i++) + { + if (rez.A > 0.99f) + { + break; + } + + float3 pos = ro + (t * rd); + float2 mpv = Map(pos, prm1, bsMo); + float den = Hlsl.Clamp(mpv.X - 0.3f, 0.0f, 1.0f) * 1.12f; + float dn = Hlsl.Clamp(mpv.X + 2.0f, 0.0f, 3.0f); + float4 col = 0f; + + if (mpv.X > 0.6f) + { + + col = new float4((Hlsl.Sin(new float3(5.0f, 0.4f, 0.2f) + (mpv.Y * 0.1f) + (Hlsl.Sin(pos.Z * 0.4f) * 0.5f) + 1.8f) * 0.5f) + 0.5f, 0.08f); + col *= den * den * den; + col.RGB *= LinStep(4.0f, -2.5f, mpv.X) * 2.3f; + + float dif = Hlsl.Clamp((den - Map(pos + 0.8f, prm1, bsMo).X) / 9.0f, 0.001f, 1.0f); + + dif += Hlsl.Clamp((den - Map(pos + 0.35f, prm1, bsMo).X) / 2.5f, 0.001f, 1.0f); + col.XYZ *= den * (new float3(0.005f, 0.045f, 0.075f) + (1.5f * new float3(0.033f, 0.07f, 0.03f) * dif)); + } + + float fogC = Hlsl.Exp((t * 0.2f) - 2.2f); + + col += new float4(0.06f, 0.11f, 0.11f, 0.1f) * Hlsl.Clamp(fogC - fogT, 0.0f, 1.0f); + fogT = fogC; + rez += col * (1.0f - rez.A); + t += Hlsl.Clamp(0.5f - (dn * dn * 0.05f), 0.09f, 0.3f); + } + + return Hlsl.Clamp(rez, 0.0f, 1.0f); + } + + private static float GetSaturation(float3 c) + { + float mi = Hlsl.Min(Hlsl.Min(c.X, c.Y), c.Z); + float ma = Hlsl.Max(Hlsl.Max(c.X, c.Y), c.Z); + + return (ma - mi) / (ma + (float)1e-7); + } + + private static float3 ILerp(in float3 a, in float3 b, in float x) + { + float3 ic = Hlsl.Lerp(a, b, x) + new float3((float)1e-6, 0.0f, 0.0f); + float sd = Hlsl.Abs(GetSaturation(ic) - Hlsl.Lerp(GetSaturation(a), GetSaturation(b), x)); + float3 dir = Hlsl.Normalize(new float3((2.0f * ic.X) - ic.Y - ic.Z, (2.0f * ic.Y) - ic.X - ic.Z, (2.0f * ic.Z) - ic.Y - ic.X)); + float lgt = Hlsl.Dot(1.0f, ic); + float ff = Hlsl.Dot(dir, Hlsl.Normalize(ic)); + + ic += 1.5f * dir * sd * ff * lgt; + + return Hlsl.Clamp(ic, 0.0f, 1.0f); + } + + /// + public float4 Execute() + { + int2 xy = (int2)D2D.GetScenePosition().XY; + float2 q = (float2)xy / this.dispatchSize; + float2 p = (xy - (0.5f * (float2)this.dispatchSize)) / this.dispatchSize.Y; + float2 bsMo = -0.5f * (float2)this.dispatchSize / this.dispatchSize.Y; + float scaledTime = this.time * 3.0f; + float3 ro = new(0, 0, scaledTime); + + ro += new float3(Hlsl.Sin(this.time) * 0.5f, Hlsl.Sin(this.time * 1.0f) * 0.0f, 0); + + float dspAmp = 0.85f; + + ro.XY += Disp(ro.Z) * dspAmp; + + float tgtDst = 3.5f; + float3 target = Hlsl.Normalize(ro - new float3(Disp(scaledTime + tgtDst) * dspAmp, scaledTime + tgtDst)); + + ro.X -= bsMo.X * 2.0f; + + float3 rightdir = Hlsl.Normalize(Hlsl.Cross(target, new float3(0, 1, 0))); + float3 updir = Hlsl.Normalize(Hlsl.Cross(rightdir, target)); + + rightdir = Hlsl.Normalize(Hlsl.Cross(updir, target)); + + float3 rd = Hlsl.Normalize((((p.X * rightdir) + (p.Y * updir)) * 1.0f) - target); + + rd.XY = Hlsl.Mul(rd.XY, Rotate((-Disp(scaledTime + 3.5f).X * 0.2f) + bsMo.X)); + + float prm1 = Hlsl.SmoothStep(-0.4f, 0.4f, Hlsl.Sin(this.time * 0.3f)); + float4 scn = Render(ro, rd, scaledTime, prm1, bsMo); + float3 col = scn.RGB; + + col = ILerp(col.BGR, col.RGB, Hlsl.Clamp(1.0f - prm1, 0.05f, 1.0f)); + col = Hlsl.Pow(col, new float3(0.55f, 0.65f, 0.6f)) * new float3(1.0f, 0.97f, 0.9f); + col *= (Hlsl.Pow(Hlsl.Abs(16.0f * q.X * q.Y * (1.0f - q.X) * (1.0f - q.Y)), 0.12f) * 0.7f) + 0.3f; + + return new(col, 1.0f); + } +} \ No newline at end of file diff --git a/tests/ComputeSharp.D2D1.Tests/Shaders/PyramidPattern.cs b/tests/ComputeSharp.D2D1.Tests/Shaders/PyramidPattern.cs new file mode 100644 index 00000000..c680c786 --- /dev/null +++ b/tests/ComputeSharp.D2D1.Tests/Shaders/PyramidPattern.cs @@ -0,0 +1,197 @@ +namespace ComputeSharp.D2D1.Tests.Shaders; + +/// +/// An offset grid of square-based pyramids whose tips have been offset according to an underlying directional noise field. +/// The faux lighting is provided via bump mapping. No raymarching was harmed during the making of this example. +/// Ported from . +/// Created by Shane. +/// +[D2DInputCount(0)] +[D2DRequiresScenePosition] +[D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] +[AutoConstructor] +internal readonly partial struct PyramidPattern : ID2D1PixelShader +{ + /// + /// The current time Hlsl.Since the start of the application. + /// + public readonly float time; + + /// + /// The dispatch size for the current output. + /// + public readonly int2 dispatchSize; + + // Standard 2D rotation formula. + private static float2x2 Rotate2x2(in float a) + { + float c = Hlsl.Cos(a); + float s = Hlsl.Sin(a); + + return new(c, -s, s, c); + } + + // IQ's float2 to float hash. + private static float Hash21(float2 p) + { + return Hlsl.Frac(Hlsl.Sin(Hlsl.Dot(p, new float2(27.619f, 57.583f))) * 43758.5453f); + } + + // float2 to float2 hash. + private static float2 Hash22B(float2 p) + { + float n = Hlsl.Sin(Hlsl.Dot(p, new float2(41, 289))); + + return (Hlsl.Frac(new float2(262144, 32768) * n) * 2.0f) - 1.0f; + } + + // Based on IQ's gradient noise formula. + private static float N2D3G(float2 p) + { + float2 i = Hlsl.Floor(p); + + p -= i; + + float4 v = default; + + v.X = Hlsl.Dot(Hash22B(i), p); + v.Y = Hlsl.Dot(Hash22B(i + float2.UnitX), p - float2.UnitX); + v.Z = Hlsl.Dot(Hash22B(i + float2.UnitY), p - float2.UnitY); + v.W = Hlsl.Dot(Hash22B(i + 1.0f), p - 1.0f); + + p = p * p * (3.0f - (2.0f * p)); + + return Hlsl.Lerp(Hlsl.Lerp(v.X, v.Y, p.X), Hlsl.Lerp(v.Z, v.W, p.X), p.Y); + } + + // Two layers of noise. + private static float FBM(float2 p) + { + return (N2D3G(p) * 0.66f) + (N2D3G(p * 2.0f) * 0.34f); + } + + // HLSL mod from GLSL + private static float Mod(float x, float y) + { + return x - (y * Hlsl.Floor(x / y)); + } + + private float BMap(float2 p) + { + p = Hlsl.Mul(p, Rotate2x2(-3.14159f / 5.0f)); + + if (Mod(Hlsl.Floor(p.Y), 2.0f) < 0.5f) + { + p.X += 0.5f; + } + + float2 ip = Hlsl.Floor(p); + + p -= ip + 0.5f; + + float ang = (-3.14159f * 3.0f / 5.0f) + (FBM((ip / 8.0f) + (this.time / 3.0f)) * 6.2831f * 2.0f); + + float2 offs = new float2(Hlsl.Cos(ang), Hlsl.Sin(ang)) * 0.35f; + + if (p.X < offs.X) + { + p.X = 1.0f - ((p.X + 0.5f) / Hlsl.Abs(offs.X + 0.5f)); + } + else + { + p.X = (p.X - offs.X) / (0.5f - offs.X); + } + + if (p.Y < offs.Y) + { + p.Y = 1.0f - ((p.Y + 0.5f) / Hlsl.Abs(offs.Y + 0.5f)); + } + else + { + p.Y = (p.Y - offs.Y) / (0.5f - offs.Y); + } + + return 1.0f - Hlsl.Max(p.X, p.Y); + } + + // Standard function-based bump mapping function, with an edge value included for good measure + private float3 DoBumpMap(in float2 p, in float3 n, float bumpfactor, ref float edge) + { + float2 e = new(0.025f, 0); + + float f = BMap(p); + float fx = BMap(p - e.XY); + float fy = BMap(p - e.YX); + float fx2 = BMap(p + e.XY); + float fy2 = BMap(p + e.YX); + + float3 grad = new float3(fx - fx2, fy - fx2, 0) / e.X / 2.0f; + + edge = Hlsl.Length(new float2(fx, fy) + new float2(fx2, fy2) - (f * 2.0f)); + edge = Hlsl.SmoothStep(0.0f, 1.0f, edge / e.X); + + grad -= n * Hlsl.Dot(n, grad); + + return Hlsl.Normalize(n + (grad * bumpfactor)); + } + + // A hatch-like algorithm, or a stipple... or some kind of textured pattern + private float DoHatch(float2 p, float res) + { + p *= res / 16.0f; + + float hatch = Hlsl.Clamp((Hlsl.Sin((p.X - p.Y) * 3.14159f * 200.0f) * 2.0f) + 0.5f, 0.0f, 1.0f); + + float hRnd = Hash21(Hlsl.Floor(p * 6.0f) + 0.73f); + if (hRnd > 0.66f) + { + hatch = hRnd; + } + + return hatch; + } + + /// + public float4 Execute() + { + int2 xy = (int2)D2D.GetScenePosition().XY; + int2 coordinate = new(xy.X, this.dispatchSize.Y - xy.Y); + float iRes = Hlsl.Min(this.dispatchSize.Y, 800.0f); + float2 uv = (coordinate - ((float2)this.dispatchSize * 0.5f)) / iRes; + float3 rd = Hlsl.Normalize(new float3(uv, 0.5f)); + const float gSc = 10.0f; + float2 p = (uv * gSc) + new float2(0, this.time / 2.0f); + float2 oP = p; + float m = BMap(p); + float3 n = new(0, 0, -1); + float edge = 0.0f; + float bumpFactor = 0.25f; + + n = DoBumpMap(p, n, bumpFactor, ref edge); + + float3 lp = new float3(-0.0f + (Hlsl.Sin(this.time) * 0.3f), 0.0f + (Hlsl.Cos(this.time * 1.3f) * 0.3f), -1) - new float3(uv, 0); + float lDist = Hlsl.Max(Hlsl.Length(lp), 0.001f); + float3 ld = lp / lDist; + float diff = Hlsl.Max(Hlsl.Dot(n, ld), 0.0f); + + diff = Hlsl.Pow(diff, 4.0f); + + float spec = Hlsl.Pow(Hlsl.Max(Hlsl.Dot(Hlsl.Reflect(-ld, n), -rd), 0.0f), 16.0f); + float fre = Hlsl.Min(Hlsl.Pow(Hlsl.Max(1.0f + Hlsl.Dot(n, rd), 0.0f), 4.0f), 3.0f); + float3 col = 0.15f * (diff + 0.251f + (spec * new float3(1, 0.7f, 0.3f) * 9.0f) + (fre * new float3(0.1f, 0.3f, 1) * 12.0f)); + float rf = Hlsl.SmoothStep(0.0f, 0.35f, (BMap(Hlsl.Reflect(rd, n).XY * 2.0f) * FBM(Hlsl.Reflect(rd, n).XY * 3.0f)) + 0.1f); + + col += col * col * rf * rf * new float3(1, 0.1f, 0.1f) * 15.0f; + + float shade = (m * 0.83f) + 0.17f; + + col *= shade; + col *= 1.0f - (edge * 0.8f); + + float hatch = DoHatch(oP / gSc, iRes); + + col *= (hatch * 0.5f) + 0.7f; + + return new(Hlsl.Sqrt(Hlsl.Max(col, 0.0f)), 1); + } +} \ No newline at end of file diff --git a/tests/ComputeSharp.D2D1.Tests/Shaders/TriangleGridContouring.cs b/tests/ComputeSharp.D2D1.Tests/Shaders/TriangleGridContouring.cs new file mode 100644 index 00000000..4d4458b4 --- /dev/null +++ b/tests/ComputeSharp.D2D1.Tests/Shaders/TriangleGridContouring.cs @@ -0,0 +1,314 @@ +namespace ComputeSharp.D2D1.Tests.Shaders; + +/// +/// Utilizing a 2D simplex grid to construct the isolines of a noise function. +/// Ported from . +/// Created by Shane. +/// +[D2DInputCount(0)] +[D2DRequiresScenePosition] +[D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] +[AutoConstructor] +internal readonly partial struct TriangleGridContouring : ID2D1PixelShader +{ + /// + /// The current time Hlsl.Since the start of the application. + /// + public readonly float time; + + /// + /// The dispatch size for the current output. + /// + public readonly int2 dispatchSize; + + // Standard 2D rotation formula. + private static float2x2 Rotate2x2(in float a) + { + float c = Hlsl.Cos(a); + float s = Hlsl.Sin(a); + + return new(c, -s, s, c); + } + + // IQ's float2 to float hash. + private static float Hash21(float2 p) + { + return Hlsl.Frac(Hlsl.Sin(Hlsl.Dot(p, new float2(27.619f, 57.583f))) * 43758.5453f); + } + + // float2 to float2 hash. + private float2 Hash22(float2 p) + { + float n = Hlsl.Sin(Hlsl.Dot(p, new float2(41, 289))); + + p = Hlsl.Frac(new float2(262144, 32768) * n); + + return Hlsl.Sin((p * 6.2831853f) + this.time); + } + + // Based on IQ's gradient noise formula. + private float N2D3G(float2 p) + { + float2 i = Hlsl.Floor(p); + + p -= i; + + float4 v = default; + + v.X = Hlsl.Dot(Hash22(i), p); + v.Y = Hlsl.Dot(Hash22(i + float2.UnitX), p - float2.UnitX); + v.Z = Hlsl.Dot(Hash22(i + float2.UnitY), p - float2.UnitY); + v.W = Hlsl.Dot(Hash22(i + 1.0f), p - 1.0f); + + p = p * p * p * ((p * ((p * 6.0f) - 15.0f)) + 10.0f); + + return Hlsl.Lerp(Hlsl.Lerp(v.X, v.Y, p.X), Hlsl.Lerp(v.Z, v.W, p.X), p.Y); + } + + // The isofunction. Just a Hlsl.Single noise function, but it can be more elaborate. + private float IsoFunction(in float2 p) + { + return N2D3G((p / 4.0f) + 0.07f); + } + + // Unsigned distance to the segment joining "a" and "b". + private static float DistLine(float2 a, float2 b) + { + b = a - b; + + float h = Hlsl.Clamp(Hlsl.Dot(a, b) / Hlsl.Dot(b, b), 0.0f, 1.0f); + + return Hlsl.Length(a - (b * h)); + } + + // Based on IQ's signed distance to the segment joining "a" and "b". + private static float distEdge(float2 a, float2 b) + { + return Hlsl.Dot((a + b) * 0.5f, Hlsl.Normalize((b - a).YX * new float2(-1, 1))); + } + + // Interpolating along the edge connecting vertices v1 and v2 with respect to the isovalue. + private static float2 Interpolate(in float2 p1, in float2 p2, float v1, float v2, float isovalue) + { + return Hlsl.Lerp(p1, p2, ((isovalue - v1) / (v2 - v1) * 0.75f) + (0.25f / 2.0f)); + } + + // Isoline function. + private static int IsoLine( + float3 n3, + float2 ip0, + float2 ip1, + float2 ip2, + float isovalue, + float i, + ref float2 p0, + ref float2 p1) + { + p0 = 1e5f; + p1 = 1e5f; + + int iTh = 0; + + if (n3.X > isovalue) + { + iTh += 4; + } + + if (n3.Y > isovalue) + { + iTh += 2; + } + + if (n3.Z > isovalue) + { + iTh += 1; + } + + if (iTh == 1 || iTh == 6) + { + p0 = Interpolate(ip1, ip2, n3.Y, n3.Z, isovalue); + p1 = Interpolate(ip2, ip0, n3.Z, n3.X, isovalue); + + } + else if (iTh == 2 || iTh == 5) + { + p0 = Interpolate(ip0, ip1, n3.X, n3.Y, isovalue); + p1 = Interpolate(ip1, ip2, n3.Y, n3.Z, isovalue); + + } + else if (iTh == 3 || iTh == 4) + { + p0 = Interpolate(ip0, ip1, n3.X, n3.Y, isovalue); + p1 = Interpolate(ip2, ip0, n3.Z, n3.X, isovalue); + } + + if (iTh >= 4 && iTh <= 6) + { + float2 tmp = p0; + + p0 = p1; + p1 = tmp; + } + + if (i == 0.0f) + { + float2 tmp = p0; + + p0 = p1; + p1 = tmp; + } + + return iTh; + } + + private float3 SimplexContour(float2 p) + { + const float gSc = 8.0f; + + p *= gSc; + + float2 oP = p; + + p += new float2(N2D3G(p * 3.5f), N2D3G((p * 3.5f) + 7.3f)) * 0.015f; + + float2 s = Hlsl.Floor(p + ((p.X + p.Y) * 0.36602540378f)); + + p -= s - ((s.X + s.Y) * 0.211324865f); + + float i = p.X < p.Y ? 1.0f : 0.0f; + float2 ioffs = new(1.0f - i, i); + float2 ip0 = 0; + float2 ip1 = ioffs - 0.2113248654f; + float2 ip2 = 0.577350269f; + float2 ctr = (ip0 + ip1 + ip2) / 3.0f; + + ip0 -= ctr; + ip1 -= ctr; + ip2 -= ctr; + p -= ctr; + + float3 n3 = default; + + n3.X = IsoFunction(s); + n3.Y = IsoFunction(s + ioffs); + n3.Z = IsoFunction(s + 1.0f); + + float d = 1e5f; + float d2 = 1e5f; + float d3 = 1e5f; + float d4 = 1e5f; + float d5 = 1e5f; + float isovalue = 0; + float2 p0 = default; + float2 p1 = default; + int iTh = IsoLine(n3, ip0, ip1, ip2, isovalue, i, ref p0, ref p1); + + d = Hlsl.Min(d, distEdge(p - p0, p - p1)); + + if (iTh == 7) + { + d = 0; + } + + d3 = Hlsl.Min(d3, DistLine(p - p0, p - p1)); + d4 = Hlsl.Min(d4, Hlsl.Min(Hlsl.Length(p - p0), Hlsl.Length(p - p1))); + + float tri = Hlsl.Min(Hlsl.Min(DistLine(p - ip0, p - ip1), DistLine(p - ip1, p - ip2)), DistLine(p - ip2, p - ip0)); + + d5 = Hlsl.Min(d5, tri); + d5 = Hlsl.Min(d5, Hlsl.Length(p) - 0.02f); + isovalue = -0.15f; + + int iTh2 = IsoLine(n3, ip0, ip1, ip2, isovalue, i, ref p0, ref p1); + + d2 = Hlsl.Min(d2, distEdge(p - p0, p - p1)); + + if (iTh2 == 7) + { + d2 = 0.0f; + } + + if (iTh == 7) + { + d2 = 1e5f; + } + + d2 = Hlsl.Max(d2, -d); + d3 = Hlsl.Min(d3, DistLine(p - p0, p - p1)); + d4 = Hlsl.Min(d4, Hlsl.Min(Hlsl.Length(p - p0), Hlsl.Length(p - p1))); + d4 -= 0.075f; + d3 -= 0.0125f; + d /= gSc; + d2 /= gSc; + d3 /= gSc; + d4 /= gSc; + d5 /= gSc; + + float3 col = new(1, 0.85f, 0.6f); + float sf = 0.004f; + + if (d > 0.0f && d2 > 0.0f) + { + col = new float3(1, 1.8f, 3) * 0.45f; + } + + if (d > 0.0f) + { + col = Hlsl.Lerp(col, new float3(1, 1.85f, 3) * 0.3f, 1.0f - Hlsl.SmoothStep(0.0f, sf, d2 - 0.012f)); + } + + col = Hlsl.Lerp(col, new float3(1.1f, 0.85f, 0.6f), 1.0f - Hlsl.SmoothStep(0.0f, sf, d2)); + col = Hlsl.Lerp(col, new float3(1.5f, 0.9f, 0.6f) * 0.6f, 1.0f - Hlsl.SmoothStep(0.0f, sf, d - 0.012f)); + col = Hlsl.Lerp(col, new float3(1, 0.8f, 0.6f) * new float3(0.7f, 1.0f, 0.75f) * 0.95f, 1.0f - Hlsl.SmoothStep(0.0f, sf, d)); + + if (d2 > 0.0f) + { + col *= ((Hlsl.Abs(Hlsl.Dot(n3, float3.One)) * 1.25f) + 1.25f) / 2.0f; + } + else + { + col *= Hlsl.Max(2.0f - ((Hlsl.Dot(n3, float3.One) + 1.45f) / 1.25f), 0.0f); + } + + float pat = Hlsl.Abs(Hlsl.Frac((tri * 12.5f) + 0.4f) - 0.5f) * 2.0f; + + col *= (pat * 0.425f) + 0.75f; + col = Hlsl.Lerp(col, float3.Zero, (1.0f - Hlsl.SmoothStep(0.0f, sf, d5)) * 0.95f); + col = Hlsl.Lerp(col, float3.Zero, 1.0f - Hlsl.SmoothStep(0.0f, sf, d3)); + col = Hlsl.Lerp(col, float3.Zero, 1.0f - Hlsl.SmoothStep(0.0f, sf, d4)); + col = Hlsl.Lerp(col, float3.One, 1.0f - Hlsl.SmoothStep(0.0f, sf, d4 + 0.005f)); + + float2 q = oP * 1.5f; + + col = Hlsl.Min(col, 1.0f); + + float gr = Hlsl.Sqrt(Hlsl.Dot(col, new float3(0.299f, 0.587f, 0.114f))) * 1.25f; + float ns = (((N2D3G(q * 4.0f * new float2(1.0f / 3.0f, 3)) * 0.64f) + (N2D3G(q * 8.0f * new float2(1.0f / 3.0f, 3)) * 0.34f)) * 0.5f) + 0.5f; + + ns = gr - ns; + q = Hlsl.Mul(q, Rotate2x2(3.14159f / 3.0f)); + + float ns2 = (((N2D3G(q * 4.0f * new float2(1.0f / 3.0f, 3)) * 0.64f) + (N2D3G(q * 8.0f * new float2(1.0f / 3.0f, 3)) * 0.34f)) * 0.5f) + 0.5f; + + ns2 = gr - ns2; + ns = Hlsl.SmoothStep(0.0f, 1.0f, Hlsl.Min(ns, ns2)); + col = Hlsl.Lerp(col, col * (ns + 0.35f), 0.4f); + + return col; + } + + /// + public float4 Execute() + { + int2 xy = (int2)D2D.GetScenePosition().XY; + int2 coordinate = new(xy.X, this.dispatchSize.Y - xy.Y); + float2 uv = (coordinate - ((float2)this.dispatchSize * 0.5f)) / Hlsl.Min(650.0f, this.dispatchSize.Y); + float2 p = Hlsl.Mul(Rotate2x2(3.14159f / 12.0f), uv) + (new float2(0.8660254f, 0.5f) * this.time / 16.0f); + float3 col = SimplexContour(p); + + uv = coordinate / (float2)this.dispatchSize; + col *= Hlsl.Pow(Hlsl.Abs(16.0f * uv.X * uv.Y * (1.0f - uv.X) * (1.0f - uv.Y)), 0.0625f) + 0.1f; + + return new(Hlsl.Sqrt(Hlsl.Max(col, 0.0f)), 1); + } +} \ No newline at end of file diff --git a/tests/ComputeSharp.D2D1.Tests/Shaders/TwoTiledTruchet.cs b/tests/ComputeSharp.D2D1.Tests/Shaders/TwoTiledTruchet.cs new file mode 100644 index 00000000..f2cf9655 --- /dev/null +++ b/tests/ComputeSharp.D2D1.Tests/Shaders/TwoTiledTruchet.cs @@ -0,0 +1,152 @@ +namespace ComputeSharp.D2D1.Tests.Shaders; + +/// +/// A shader creating an animated two-tiles Truchet arrangement. +/// Ported from . +/// Created by Shane. +/// +[D2DInputCount(0)] +[D2DRequiresScenePosition] +[D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] +[AutoConstructor] +internal readonly partial struct TwoTiledTruchet : 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; + + /// + /// Calculates the Truchet distance field. + /// + /// The input value to compute the distance field for. + /// The resulting Truchet angle. + /// The distance field for the input value. + private float2 DistanceField(float2 p, out float2 ang) + { + // Fast hash for a pair of floats + static float Hash21(float2 p) + { + return Hlsl.Frac(Hlsl.Sin(Hlsl.Dot(p, new float2(27.619f, 57.583f))) * 43758.5453f); + } + + // HLSL's port of the GLSL mod intrinsic + static float Mod(float x, float y) + { + return x - (y * Hlsl.Floor(x / y)); + } + + float2 ip2 = Hlsl.Floor(p / 2.0f); + float2 ip = Hlsl.Floor(p); + float rnd = Hash21(ip); + float rnd3 = Hash21(ip + 0.57f); + float2 d = 1e5f; + + p -= ip + 0.5f; + + if (Mod(ip2.X + ip2.Y, 2.0f) < 0.5f) + { + d = Hlsl.Abs(p); + ang = p.YX; + + if (Mod(ip.X + 1.0f, 2.0f) < 0.5f) + { + ang.X *= -1.0f; + + } + + if (Mod(ip.Y + 1.0f, 2.0f) > 0.5f) + { + ang.Y *= -1.0f; + } + + if (rnd3 < .5) + { + d = d.YX; + ang = ang.YX; + } + + ang += 0.5f; + ang *= 3.0f; + + } + else + { + if (rnd < .5) + { + p = p.YX * new float2(1, -1); + } + + d.X = Hlsl.Length(p - 0.5f) - 0.5f; + d.Y = Hlsl.Length(p + 0.5f) - 0.5f; + + d = Hlsl.Abs(d); + + ang = 0; + ang.X = -Hlsl.Atan((p.Y - 0.5f) / (p.X - 0.5f)); + ang.Y = -Hlsl.Atan((p.Y + 0.5f) / (p.X + 0.5f)); + + if (Mod(ip.X + ip.Y, 2.0f) < 0.5f) + { + ang *= -1.0f; + } + + if (rnd < 0.5f) + { + ang *= -1.0f; + } + + if (rnd3 < 0.5f) + { + d = d.YX; + ang = ang.YX; + } + + ang *= 4.0f / 6.2831853f; + ang *= 2.0f; + } + + ang = Hlsl.Frac(ang + (this.time / 4.0f)); + + return d; + } + + /// + public float4 Execute() + { + int2 xy = (int2)D2D.GetScenePosition().XY; + float2 uv = (xy - ((float2)this.dispatchSize * 0.5f)) / this.dispatchSize.Y; + float gSc = 7.0f; + float2 p = (uv * gSc) - (new float2(-1, -0.5f) * this.time / 2.0f); + float sf = 2.0f / this.dispatchSize.Y * gSc; + float lSc = 6.0f; + float lw = 1.0f / lSc / gSc; + float2 d = DistanceField(p, out float2 ang) - (2.5f / lSc); + float3 col = new(1.0f, 0.9f, 0.8f); + + for (int i = 0; i < 2; i++) + { + float di = d[i] - (lw / 4.0f); + float tracks = Hlsl.Clamp(Hlsl.Sin((ang[i] * 6.2831f) + (this.time * 6.0f)) * 4.0f, 0.0f, 1.0f); + float gap = 1.0f + lw; + + col = Hlsl.Lerp(col, 0, (1.0f - Hlsl.SmoothStep(0.0f, sf * 6.0f, di)) * 0.35f); + col = Hlsl.Lerp(col, 0, 1.0f - Hlsl.SmoothStep(0.0f, sf, di)); + col = Hlsl.Lerp(col, new(1.0f, 0.9f, 0.8f), 1.0f - Hlsl.SmoothStep(0.0f, sf, di + (lw * 2.0f))); + col = Hlsl.Lerp(col, 0, 1.0f - Hlsl.SmoothStep(0.0f, sf, di + (gap / lSc))); + col = Hlsl.Lerp(col, 1, 1.0f - Hlsl.SmoothStep(0.0f, sf, di + (gap / lSc) + lw)); + col = Hlsl.Lerp(col, 0, 1.0f - Hlsl.SmoothStep(0.0f, sf, di + (2.0f * gap / lSc))); + col = Hlsl.Lerp(col, 1 * tracks, 1.0f - Hlsl.SmoothStep(0.0f, sf, di + (2.0f * gap / lSc) + lw)); + } + + float3 rgb = Hlsl.Sqrt(Hlsl.Max(col, 0.0f)); + float4 color = new(rgb.X, rgb.Y, rgb.Z, 1.0f); + + return color; + } +} \ No newline at end of file diff --git a/tests/ComputeSharp.D2D1.Tests/ShadersTests.cs b/tests/ComputeSharp.D2D1.Tests/ShadersTests.cs new file mode 100644 index 00000000..9fe22b3a --- /dev/null +++ b/tests/ComputeSharp.D2D1.Tests/ShadersTests.cs @@ -0,0 +1,116 @@ +using System; +using System.IO; +using System.Reflection; +using System.Runtime.InteropServices; +using ComputeSharp.D2D1.Interop; +using ComputeSharp.D2D1.Tests.Helpers; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using SixLabors.ImageSharp; +using SixLabors.ImageSharp.PixelFormats; + +namespace ComputeSharp.D2D1.Tests; + +[TestClass] +[TestCategory("Shaders")] +public class ShadersTests +{ + [TestMethod] + public void HelloWorld() + { + RunTest(); + } + + [TestMethod] + public void ColorfulInfinity() + { + RunTest(); + } + + [TestMethod] + public void FractalTiling() + { + RunTest(); + } + + [TestMethod] + public void MengerJourney() + { + RunTest(0.000011f); + } + + // This test and the other 3 below are skipped because they do produce valid result, + // but the resulting images are different than the ones used as reference, so they + // cannot be compared. They can be uncommented once the reason why they are seemingly + // being randomized and producing different outputs has been identified and resolved. + [TestMethod] + [Ignore] + public void TwoTiledTruchet() + { + RunTest(); + } + + [TestMethod] + public void Octagrams() + { + RunTest(); + } + + [TestMethod] + public void ProteanClouds() + { + RunTest(); + } + + [TestMethod] + [Ignore] + public void PyramidPattern() + { + RunTest(); + } + + [TestMethod] + [Ignore] + public void TriangleGridContouring() + { + RunTest(); + } + + [TestMethod] + [Ignore] + public unsafe void ContouredLayers() + { + string assemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!; + string expectedPath = Path.Combine(assemblyPath, "Assets", "Textures", "RustyMetal.png"); + + D2D1ResourceTextureManager resourceTextureManager; + + using (Image texture = Image.Load(expectedPath)) + { + if (!texture.DangerousTryGetSinglePixelMemory(out Memory pixels)) + { + Assert.Inconclusive(); + } + + resourceTextureManager = new D2D1ResourceTextureManager( + extents: stackalloc uint[] { (uint)texture.Width, (uint)texture.Height }, + bufferPrecision: D2D1BufferPrecision.UInt8Normalized, + channelDepth: D2D1ChannelDepth.Four, + filter: D2D1Filter.MinMagMipLinear, + extendModes: stackalloc D2D1ExtendMode[] { D2D1ExtendMode.Mirror, D2D1ExtendMode.Mirror }, + data: MemoryMarshal.AsBytes(pixels.Span), + strides: stackalloc uint[] { (uint)(texture.Width * sizeof(Rgba32)) }); + } + + Shaders.ContouredLayers shader = new(0f, new int2(1280, 720)); + + D2D1TestRunner.RunAndCompareShader(in shader, 1280, 720, $"{nameof(Shaders.ContouredLayers)}.png", nameof(Shaders.ContouredLayers), resourceTextures: (0, resourceTextureManager)); + } + + private static void RunTest(float threshold = 0.00001f) + where T : unmanaged, ID2D1PixelShader + { + T shader = (T)Activator.CreateInstance(typeof(T), 0f, new int2(1280, 720))!; + + D2D1TestRunner.RunAndCompareShader(in shader, 1280, 720, $"{typeof(T).Name}.png", typeof(T).Name, threshold); + } +} \ No newline at end of file diff --git a/tests/ComputeSharp.Tests/Shaders/Compute/ContouredLayers.cs b/tests/ComputeSharp.Tests/Shaders/Compute/ContouredLayers.cs index 230fc637..b47e909d 100644 --- a/tests/ComputeSharp.Tests/Shaders/Compute/ContouredLayers.cs +++ b/tests/ComputeSharp.Tests/Shaders/Compute/ContouredLayers.cs @@ -121,7 +121,7 @@ internal readonly partial struct ContouredLayers : IComputeShader float3 col; - col = (float3)1.0 * (1.0f - 0.75f / (1.0f + sh * sh * 2.0f)); + col = (float3)1.0f * (1.0f - 0.75f / (1.0f + sh * sh * 2.0f)); col = Hlsl.Min(col * tx * 3.0f, 1.0f); return col; 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; } diff --git a/tests/ComputeSharp.Tests/ShadersTests.cs b/tests/ComputeSharp.Tests/ShadersTests.cs index 3b0b2b39..e307723e 100644 --- a/tests/ComputeSharp.Tests/ShadersTests.cs +++ b/tests/ComputeSharp.Tests/ShadersTests.cs @@ -178,6 +178,10 @@ public class ShadersTests static void RunPixelShader(ReadWriteTexture2D texture) where T : struct, IPixelShader { + ShaderInfo info = ReflectionServices.GetShaderInfo(); + + Assert.IsFalse(info.RequiresDoublePrecisionSupport); + texture.GraphicsDevice.ForEach(texture, (T)Activator.CreateInstance(typeof(T), 0f)!); }