diff --git a/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs b/tests/ComputeSharp.D2D1.Tests/Helpers/D2D1TestRunner.cs
index 6cc264f7..c1a74e47 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,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,
int width,
int height,
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");
@@ -89,7 +93,7 @@ internal static class D2D1TestRunner
destinationPath);
// Compare the results
- TolerantImageComparer.AssertEqual(destinationPath, expectedPath, 0.00001f);
+ TolerantImageComparer.AssertEqual(destinationPath, expectedPath, threshold);
}
///
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/ShadersTests.cs b/tests/ComputeSharp.D2D1.Tests/ShadersTests.cs
index 7da9e1c7..6cfb3e88 100644
--- a/tests/ComputeSharp.D2D1.Tests/ShadersTests.cs
+++ b/tests/ComputeSharp.D2D1.Tests/ShadersTests.cs
@@ -26,11 +26,17 @@ public class ShadersTests
RunTest();
}
- private static void RunTest()
+ [TestMethod]
+ public void MengerJourney()
+ {
+ RunTest(0.000011f);
+ }
+
+ 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}.png");
+ D2D1TestRunner.RunAndCompareShader(in shader, 1280, 720, $"{typeof(T).Name}.png", $"{typeof(T).Name}.png", threshold);
}
}
\ No newline at end of file