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/ComputeSharp.D2D1.Tests.csproj b/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj index 9e182186..55bf1820 100644 --- a/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj +++ b/tests/ComputeSharp.D2D1.Tests/ComputeSharp.D2D1.Tests.csproj @@ -1,4 +1,4 @@ - + net472;netcoreapp3.1;net6.0 @@ -62,9 +62,6 @@ PreserveNewest - - PreserveNewest - PreserveNewest @@ -92,6 +89,10 @@ PreserveNewest + + PreserveNewest + + diff --git a/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs b/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs index c1a74e47..35ce6288 100644 --- a/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs +++ b/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs @@ -67,13 +67,15 @@ internal static class D2D1TestRunner /// 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 = "", - float threshold = 0.00001f) + float threshold = 0.00001f, + params (int Index, D2D1ResourceTextureManager ResourceTextureManager)[] resourceTextures) where T : unmanaged, ID2D1PixelShader { string assetsPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Assets"); @@ -90,7 +92,8 @@ internal static class D2D1TestRunner null, width, height, - destinationPath); + destinationPath, + resourceTextures); // Compare the results TolerantImageComparer.AssertEqual(destinationPath, expectedPath, threshold); @@ -145,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(); @@ -165,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/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/ShadersTests.cs b/tests/ComputeSharp.D2D1.Tests/ShadersTests.cs index edeb866a..850b38ca 100644 --- a/tests/ComputeSharp.D2D1.Tests/ShadersTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/ShadersTests.cs @@ -1,6 +1,12 @@ 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; @@ -62,6 +68,36 @@ public class ShadersTests RunTest(); } + [TestMethod] + 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 {