From ada133aacd29882ede43d201e178a9c3eb92d611 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 23 Feb 2021 13:25:04 +0100 Subject: [PATCH] Added Menjer journey sample --- .../ComputeSharp.Sample.SwapChain/Program.cs | 3 +- .../Shaders/MengerJourney.cs | 171 ++++++++++++++++++ 2 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 samples/ComputeSharp.Sample.SwapChain/Shaders/MengerJourney.cs diff --git a/samples/ComputeSharp.Sample.SwapChain/Program.cs b/samples/ComputeSharp.Sample.SwapChain/Program.cs index 98d036cd..e829766c 100644 --- a/samples/ComputeSharp.Sample.SwapChain/Program.cs +++ b/samples/ComputeSharp.Sample.SwapChain/Program.cs @@ -13,7 +13,8 @@ namespace ComputeSharp.Sample.SwapChain { 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)) }; static void Main() diff --git a/samples/ComputeSharp.Sample.SwapChain/Shaders/MengerJourney.cs b/samples/ComputeSharp.Sample.SwapChain/Shaders/MengerJourney.cs new file mode 100644 index 00000000..b1f0e88f --- /dev/null +++ b/samples/ComputeSharp.Sample.SwapChain/Shaders/MengerJourney.cs @@ -0,0 +1,171 @@ +namespace ComputeSharp.Sample.SwapChain.Shaders +{ + /// + /// A shader creating a flythrough in a Menger system. + /// Ported from . + /// Created by Syntopia. + /// + [AutoConstructor] + internal readonly partial struct MengerJourney : IComputeShader + { + /// + /// The target texture. + /// + public readonly IReadWriteTexture2D texture; + + /// + /// The current time since the start of the application. + /// + public readonly float time; + + 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; + + /// + /// 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((Float3)1); + float diffuse = Hlsl.Max(0.0f, Hlsl.Dot(-normal, lightDir)); + + Float3 lightDir2 = Hlsl.Normalize(new Float3(1.0f, -1.0f, 1.0f)); + float diffuse2 = Hlsl.Max(0.0f, Hlsl.Dot(-normal, lightDir2)); + + return + (diffuse * Diffuse) * (new Float3(1.0f, 1.0f, 0.858824f) * color) + + (diffuse2 * Diffuse) * (new Float3(0.0f, 0.333333f, 1.0f) * 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; + Float3 Offset = new(0.92858f, 0.92858f, 0.32858f); + + for (int n = 0; n < Iterations; n++) + { + z.XY = Rotate(z.XY, 4.0f + 2.0f * Hlsl.Cos(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 + 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(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 void 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)); + Float3 camDir = Hlsl.Normalize(target - camPos); + Float3 camUp = new Float3(0.0f, 1.0f, 0.0f); + + camUp = Hlsl.Normalize(camUp - Hlsl.Dot(camDir, camUp) * camDir); + + Float3 camRight = Hlsl.Normalize(Hlsl.Cross(camDir, camUp)); + Float2 resolution = new(texture.Width, texture.Height); + Float2 coord = -1.0f + 2.0f * (Float2)ThreadIds.XY / resolution; + + coord.X *= resolution.X / resolution.Y; + + Float3 rayDir = Hlsl.Normalize(camDir + (coord.X * camRight + coord.Y * camUp) * FieldOfView); + + texture[ThreadIds.XY] = RayMarch(camPos, rayDir, ThreadIds.XY); + } + } +}