From 56bb37bd44a0725f6eaab721ad76cbf99beea585 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 23 Nov 2023 22:34:02 +0100 Subject: [PATCH] Switch D2D1 sample shaders to primary constructors --- .../ColorfulInfinity.cs | 21 ++++--------- .../ContouredLayers.cs | 29 ++++++----------- .../FractalTiling.cs | 19 +++--------- .../HelloWorld.cs | 19 +++--------- .../MengerJourney.cs | 29 ++++++----------- .../Octagrams.cs | 27 ++++++---------- .../ProteanClouds.cs | 31 +++++++------------ .../PyramidPattern.cs | 27 ++++++---------- .../TerracedHills.cs | 23 +++++--------- .../TriangleGridContouring.cs | 25 +++++---------- .../TwoTiledTruchet.cs | 25 +++++---------- 11 files changed, 88 insertions(+), 187 deletions(-) diff --git a/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/ColorfulInfinity.cs b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/ColorfulInfinity.cs index 84671219..f47a7c8a 100644 --- a/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/ColorfulInfinity.cs +++ b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/ColorfulInfinity.cs @@ -8,23 +8,14 @@ namespace ComputeSharp.SwapChain.Shaders.D2D1; /// Created by Benoit Marini. /// License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. /// +/// The current time since the start of the application. +/// The dispatch size for the current output. [D2DInputCount(0)] [D2DRequiresScenePosition] [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] [D2DGeneratedPixelShaderDescriptor] -[AutoConstructor] -internal readonly partial struct ColorfulInfinity : ID2D1PixelShader +internal readonly partial struct ColorfulInfinity(float time, int2 dispatchSize) : ID2D1PixelShader { - /// - /// The current time since the start of the application. - /// - private readonly float time; - - /// - /// The dispatch size for the current output. - /// - private readonly int2 dispatchSize; - /// /// The total number of layers for the final animation. /// @@ -40,7 +31,7 @@ internal readonly partial struct ColorfulInfinity : ID2D1PixelShader /// private float4 Tex(float3 p) { - float t = this.time + 78.0f; + float t = 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) + @@ -58,9 +49,9 @@ internal readonly partial struct ColorfulInfinity : ID2D1PixelShader public float4 Execute() { int2 xy = (int2)D2D.GetScenePosition().XY; - float2 uv = (xy - ((float2)this.dispatchSize * 0.5f)) / this.dispatchSize.Y; + float2 uv = (xy - ((float2)dispatchSize * 0.5f)) / dispatchSize.Y; float3 col = 0; - float t = this.time * 0.3f; + float t = time * 0.3f; for (float i = 0.0f; i <= 1.0f; i += 1.0f / NumberOfLayers) { diff --git a/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/ContouredLayers.cs b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/ContouredLayers.cs index d2cf799e..4a5c165d 100644 --- a/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/ContouredLayers.cs +++ b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/ContouredLayers.cs @@ -7,23 +7,14 @@ namespace ComputeSharp.SwapChain.Shaders.D2D1; /// Ported from . /// Created by Shane. /// +/// The current time since the start of the application. +/// The dispatch size for the current output. [D2DInputCount(0)] [D2DRequiresScenePosition] [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] [D2DGeneratedPixelShaderDescriptor] -[AutoConstructor] -internal readonly partial struct ContouredLayers : ID2D1PixelShader +internal readonly partial struct ContouredLayers(float time, int2 dispatchSize) : ID2D1PixelShader { - /// - /// The current time Hlsl.Since the start of the application. - /// - private readonly float time; - - /// - /// The dispatch size for the current output. - /// - private readonly int2 dispatchSize; - /// /// The background texture to sample. /// @@ -43,7 +34,7 @@ internal readonly partial struct ContouredLayers : ID2D1PixelShader p = Hlsl.Frac(new float2(262144, 32768) * n); - return Hlsl.Sin((p * 6.2831853f) + this.time); + return Hlsl.Sin((p * 6.2831853f) + time); } // float2 to float2 hash. @@ -154,10 +145,10 @@ internal readonly partial struct ContouredLayers : ID2D1PixelShader 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; + float2 fragCoord = new(xy.X, dispatchSize.Y - xy.Y); + float res = Hlsl.Min(dispatchSize.Y, 700.0f); + float2 uv = (fragCoord - ((float2)dispatchSize * 0.5f)) / res; + float sf = 1.0f / dispatchSize.Y; float3 col = GetColor(uv, 0.0f, 0.0f); float pL = 0.0f; float hatch = Hatch(uv, res); @@ -199,11 +190,11 @@ internal readonly partial struct ContouredLayers : ID2D1PixelShader 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); + float3 rn3 = Noise2D((uv * dispatchSize.Y / 1.0f) + 1.7f) - Noise2D((uv * 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; + uv = fragCoord / dispatchSize.XY; col *= Hlsl.Pow(Hlsl.Abs(16.0f * uv.X * uv.Y * (1.0f - uv.X) * (1.0f - uv.Y)), 0.0625f); diff --git a/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/FractalTiling.cs b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/FractalTiling.cs index 9b3e4995..68f0a79a 100644 --- a/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/FractalTiling.cs +++ b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/FractalTiling.cs @@ -8,28 +8,19 @@ namespace ComputeSharp.SwapChain.Shaders.D2D1; /// Created by Inigo Quilez. /// License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. /// +/// The current time since the start of the application. +/// The dispatch size for the current output. [D2DInputCount(0)] [D2DRequiresScenePosition] [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] [D2DGeneratedPixelShaderDescriptor] -[AutoConstructor] -internal readonly partial struct FractalTiling : ID2D1PixelShader +internal readonly partial struct FractalTiling(float time, int2 dispatchSize) : ID2D1PixelShader { - /// - /// The current time since the start of the application. - /// - private readonly float time; - - /// - /// The dispatch size for the current output. - /// - private readonly int2 dispatchSize; - /// public float4 Execute() { int2 xy = (int2)D2D.GetScenePosition().XY; - float2 position = (((float2)(256 * xy)) / this.dispatchSize.X) + this.time; + float2 position = (((float2)(256 * xy)) / dispatchSize.X) + time; float4 color = 0; for (int i = 0; i < 6; i++) @@ -37,7 +28,7 @@ internal readonly partial struct FractalTiling : ID2D1PixelShader 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)) + + (Hlsl.Sin((a.X * 7) + (31.0f * a.Y) + (0.01f * time)) + new float4(0.035f, 0.01f, 0, 0.7f)) * 13.545317f); diff --git a/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/HelloWorld.cs b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/HelloWorld.cs index f6970705..a6f021e0 100644 --- a/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/HelloWorld.cs +++ b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/HelloWorld.cs @@ -6,33 +6,24 @@ namespace ComputeSharp.SwapChain.Shaders.D2D1; /// A simple shader to get started with based on shadertoy new shader template. /// Ported from . /// +/// The current time since the start of the application. +/// The dispatch size for the current output. [D2DInputCount(0)] [D2DRequiresScenePosition] [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] [D2DGeneratedPixelShaderDescriptor] -[AutoConstructor] -internal readonly partial struct HelloWorld : ID2D1PixelShader +internal readonly partial struct HelloWorld(float time, int2 dispatchSize) : ID2D1PixelShader { - /// - /// The current time since the start of the application. - /// - private readonly float time; - - /// - /// The dispatch size for the current output. - /// - private 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; + float2 uv = xy / (float2)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))); + float3 col = 0.5f + (0.5f * Hlsl.Cos(time + new float3(uv, uv.X) + new float3(0, 2, 4))); // Output to screen return new(col, 1f); diff --git a/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/MengerJourney.cs b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/MengerJourney.cs index ef1a0d13..159d64a3 100644 --- a/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/MengerJourney.cs +++ b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/MengerJourney.cs @@ -7,23 +7,14 @@ namespace ComputeSharp.SwapChain.Shaders.D2D1; /// Ported from . /// Created by Syntopia. /// +/// The current time since the start of the application. +/// The dispatch size for the current output. [D2DInputCount(0)] [D2DRequiresScenePosition] [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] [D2DGeneratedPixelShaderDescriptor] -[AutoConstructor] -internal readonly partial struct MengerJourney : ID2D1PixelShader +internal readonly partial struct MengerJourney(float time, int2 dispatchSize) : ID2D1PixelShader { - /// - /// The current time since the start of the application. - /// - private readonly float time; - - /// - /// The dispatch size for the current output. - /// - private readonly int2 dispatchSize; - private const int MaxSteps = 30; private const float MinimumDistance = 0.0009f; private const float NormalDistance = 0.0002f; @@ -85,7 +76,7 @@ internal readonly partial struct MengerJourney : ID2D1PixelShader for (int n = 0; n < Iterations; n++) { - z.XY = Rotate(z.XY, 4.0f + (2.0f * Hlsl.Cos(this.time / 8.0f))); + 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; } @@ -131,7 +122,7 @@ internal readonly partial struct MengerJourney : ID2D1PixelShader return Hlsl.Frac(Hlsl.Cos(Hlsl.Dot(co, new float2(4.898f, 7.23f))) * 23421.631f); } - float totalDistance = Jitter * Rand(fragCoord.XY + this.time); + float totalDistance = Jitter * Rand(fragCoord.XY + time); float3 dir2 = dir; float distance = 0; int steps = 0; @@ -139,7 +130,7 @@ internal readonly partial struct MengerJourney : ID2D1PixelShader for (int i = 0; i < MaxSteps; i++) { - dir.ZY = Rotate(dir2.ZY, totalDistance * Hlsl.Cos(this.time / 4.0f) * NonLinearPerspective); + dir.ZY = Rotate(dir2.ZY, totalDistance * Hlsl.Cos(time / 4.0f) * NonLinearPerspective); pos = from + (totalDistance * dir); distance = DE(pos) * FudgeFactor; totalDistance += distance; @@ -167,17 +158,17 @@ internal readonly partial struct MengerJourney : ID2D1PixelShader 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 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(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); + float2 coord = -1.0f + (2.0f * (float2)xy / (float2)dispatchSize); - coord.X *= this.dispatchSize.X / (float)this.dispatchSize.Y; + coord.X *= dispatchSize.X / (float)dispatchSize.Y; float3 rayDir = Hlsl.Normalize(camDir + (((coord.X * camRight) + (coord.Y * camUp)) * FieldOfView)); diff --git a/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/Octagrams.cs b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/Octagrams.cs index 2e83fe79..ddcbcd45 100644 --- a/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/Octagrams.cs +++ b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/Octagrams.cs @@ -7,23 +7,14 @@ namespace ComputeSharp.SwapChain.Shaders.D2D1; /// Ported from . /// Created by whisky_shusuky. /// +/// The current time since the start of the application. +/// The dispatch size for the current output. [D2DInputCount(0)] [D2DRequiresScenePosition] [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] [D2DGeneratedPixelShaderDescriptor] -[AutoConstructor] -internal readonly partial struct Octagrams : ID2D1PixelShader +internal readonly partial struct Octagrams(float time, int2 dispatchSize) : ID2D1PixelShader { - /// - /// The current time since the start of the application. - /// - private readonly float time; - - /// - /// The dispatch size for the current output. - /// - private readonly int2 dispatchSize; - private static float2x2 Rotate(float a) { float c = Hlsl.Cos(a); @@ -98,12 +89,12 @@ internal readonly partial struct Octagrams : ID2D1PixelShader 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); + float2 p = (((float2)xy * 2.0f) - dispatchSize) / Hlsl.Min(dispatchSize.X, dispatchSize.Y); + float3 ro = new(0.0f, -0.2f, 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)); + ray.XY = Hlsl.Mul(ray.XY, Rotate(Hlsl.Sin(time * 0.03f) * 5.0f)); + ray.YZ = Hlsl.Mul(ray.YZ, Rotate(Hlsl.Sin(time * 0.05f) * 0.2f)); float t = 0.1f; float ac = 0.0f; @@ -119,7 +110,7 @@ internal readonly partial struct Octagrams : ID2D1PixelShader pos = Mod(pos - 2.0f, 4.0f) - 2.0f; - float gTime = this.time - (i * 0.01f); + float gTime = time - (i * 0.01f); float d = BoxSet(pos, gTime); d = Hlsl.Max(Hlsl.Abs(d), 0.01f); @@ -127,7 +118,7 @@ internal readonly partial struct Octagrams : ID2D1PixelShader 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)); + 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); return color; diff --git a/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/ProteanClouds.cs b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/ProteanClouds.cs index 00527c5c..957435b6 100644 --- a/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/ProteanClouds.cs +++ b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/ProteanClouds.cs @@ -8,23 +8,14 @@ namespace ComputeSharp.SwapChain.Shaders.D2D1; /// Created by nimitz (twitter: @stormoid). /// License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. /// +/// The current time since the start of the application. +/// The dispatch size for the current output. [D2DInputCount(0)] [D2DRequiresScenePosition] [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] [D2DGeneratedPixelShaderDescriptor] -[AutoConstructor] -internal readonly partial struct ProteanClouds : ID2D1PixelShader +internal readonly partial struct ProteanClouds(float time, int2 dispatchSize) : ID2D1PixelShader { - /// - /// The current time Hlsl.Since the start of the application. - /// - private readonly float time; - - /// - /// The dispatch size for the current output. - /// - private 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) @@ -50,7 +41,7 @@ internal readonly partial struct ProteanClouds : ID2D1PixelShader 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))); + p.XY = Hlsl.Mul(p.XY, Rotate((Hlsl.Sin(p.Z + time) * (0.1f + (prm1 * 0.05f))) + (time * 0.09f))); float cl = Hlsl.Dot(p2.XY, p2.XY); float d = 0.0f; @@ -63,7 +54,7 @@ internal readonly partial struct ProteanClouds : ID2D1PixelShader for (int i = 0; i < 5; i++) { - p += Hlsl.Sin((p.ZXY * 0.75f * trk) + (this.time * trk * 0.8f)) * dspAmp; + p += Hlsl.Sin((p.ZXY * 0.75f * trk) + (time * trk * 0.8f)) * dspAmp; d -= Hlsl.Abs(Hlsl.Dot(Hlsl.Cos(p), Hlsl.Sin(p.YZX)) * z); z *= 0.57f; trk *= 1.4f; @@ -144,13 +135,13 @@ internal readonly partial struct ProteanClouds : ID2D1PixelShader 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; + float2 q = (float2)xy / dispatchSize; + float2 p = (xy - (0.5f * (float2)dispatchSize)) / dispatchSize.Y; + float2 bsMo = -0.5f * (float2)dispatchSize / dispatchSize.Y; + float scaledTime = 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); + ro += new float3(Hlsl.Sin(time) * 0.5f, Hlsl.Sin(time * 1.0f) * 0.0f, 0); float dspAmp = 0.85f; @@ -170,7 +161,7 @@ internal readonly partial struct ProteanClouds : ID2D1PixelShader 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)); + float prm1 = Hlsl.SmoothStep(-0.4f, 0.4f, Hlsl.Sin(time * 0.3f)); float4 scn = Render(ro, rd, scaledTime, prm1, bsMo); float3 col = scn.RGB; diff --git a/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/PyramidPattern.cs b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/PyramidPattern.cs index 93bff4b0..c0d2bef3 100644 --- a/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/PyramidPattern.cs +++ b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/PyramidPattern.cs @@ -8,23 +8,14 @@ namespace ComputeSharp.SwapChain.Shaders.D2D1; /// Ported from . /// Created by Shane. /// +/// The current time since the start of the application. +/// The dispatch size for the current output. [D2DInputCount(0)] [D2DRequiresScenePosition] [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] [D2DGeneratedPixelShaderDescriptor] -[AutoConstructor] -internal readonly partial struct PyramidPattern : ID2D1PixelShader +internal readonly partial struct PyramidPattern(float time, int2 dispatchSize) : ID2D1PixelShader { - /// - /// The current time Hlsl.Since the start of the application. - /// - private readonly float time; - - /// - /// The dispatch size for the current output. - /// - private readonly int2 dispatchSize; - // Standard 2D rotation formula. private static float2x2 Rotate2x2(in float a) { @@ -92,7 +83,7 @@ internal readonly partial struct PyramidPattern : ID2D1PixelShader p -= ip + 0.5f; - float ang = (-3.14159f * 3.0f / 5.0f) + (FBM((ip / 8.0f) + (this.time / 3.0f)) * 6.2831f * 2.0f); + float ang = (-3.14159f * 3.0f / 5.0f) + (FBM((ip / 8.0f) + (time / 3.0f)) * 6.2831f * 2.0f); float2 offs = new float2(Hlsl.Cos(ang), Hlsl.Sin(ang)) * 0.35f; @@ -158,12 +149,12 @@ internal readonly partial struct PyramidPattern : ID2D1PixelShader 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; + int2 coordinate = new(xy.X, dispatchSize.Y - xy.Y); + float iRes = Hlsl.Min(dispatchSize.Y, 800.0f); + float2 uv = (coordinate - ((float2)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 p = (uv * gSc) + new float2(0, time / 2.0f); float2 oP = p; float m = BMap(p); float3 n = new(0, 0, -1); @@ -172,7 +163,7 @@ internal readonly partial struct PyramidPattern : ID2D1PixelShader 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); + float3 lp = new float3(-0.0f + (Hlsl.Sin(time) * 0.3f), 0.0f + (Hlsl.Cos(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 --git a/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/TerracedHills.cs b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/TerracedHills.cs index b45fe7c1..79c62167 100644 --- a/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/TerracedHills.cs +++ b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/TerracedHills.cs @@ -7,23 +7,14 @@ namespace ComputeSharp.SwapChain.Shaders.D2D1; /// Ported from . /// Created by Shane. /// +/// The current time since the start of the application. +/// The dispatch size for the current output. [D2DInputCount(0)] [D2DRequiresScenePosition] [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] [D2DGeneratedPixelShaderDescriptor] -[AutoConstructor] -internal readonly partial struct TerracedHills : ID2D1PixelShader +internal readonly partial struct TerracedHills(float time, int2 dispatchSize) : ID2D1PixelShader { - /// - /// The current time since the start of the application. - /// - private readonly float time; - - /// - /// The dispatch size for the current output. - /// - private readonly int2 dispatchSize; - // 2x2 matrix rotation. Angle vector, courtesy of Fabrice private static float2x2 Rot2(float th) { @@ -96,13 +87,13 @@ internal readonly partial struct TerracedHills : ID2D1PixelShader public float4 Execute() { int2 xy = (int2)D2D.GetScenePosition().XY; - int2 coordinate = new(xy.X, this.dispatchSize.Y - xy.Y); + int2 coordinate = new(xy.X, dispatchSize.Y - xy.Y); - float3 rd = Hlsl.Normalize(new float3((float2)coordinate - (this.dispatchSize.Y * 0.5f), this.dispatchSize.Y)); + float3 rd = Hlsl.Normalize(new float3((float2)coordinate - (dispatchSize.Y * 0.5f), dispatchSize.Y)); rd.YZ = Rot2(0.35f) * rd.YZ; - float3 ro = new(this.time * 0.4f, 0.5f, this.time * 0.2f); + float3 ro = new(time * 0.4f, 0.5f, time * 0.2f); float t = 0, d; for (int i = 0; i < 96; i++) @@ -120,7 +111,7 @@ internal readonly partial struct TerracedHills : ID2D1PixelShader float3 sp = ro + (rd * t); float3 ld = new(-0.676f, 0.408f, 0.613f); float edge = 0; - float3 n = Normal(sp, ref edge, this.dispatchSize.Y); + float3 n = Normal(sp, ref edge, dispatchSize.Y); float dif = Hlsl.Max(Hlsl.Dot(ld, n), 0.0f); float spe = Hlsl.Pow(Hlsl.Max(Hlsl.Dot(Hlsl.Reflect(rd, n), ld), 0.0f), 16.0f); float sh = HMap(sp.XZ); diff --git a/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/TriangleGridContouring.cs b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/TriangleGridContouring.cs index f2d3b62a..ec84aa37 100644 --- a/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/TriangleGridContouring.cs +++ b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/TriangleGridContouring.cs @@ -7,23 +7,14 @@ namespace ComputeSharp.SwapChain.Shaders.D2D1; /// Ported from . /// Created by Shane. /// +/// The current time since the start of the application. +/// The dispatch size for the current output. [D2DInputCount(0)] [D2DRequiresScenePosition] [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] [D2DGeneratedPixelShaderDescriptor] -[AutoConstructor] -internal readonly partial struct TriangleGridContouring : ID2D1PixelShader +internal readonly partial struct TriangleGridContouring(float time, int2 dispatchSize) : ID2D1PixelShader { - /// - /// The current time Hlsl.Since the start of the application. - /// - private readonly float time; - - /// - /// The dispatch size for the current output. - /// - private readonly int2 dispatchSize; - // Standard 2D rotation formula. private static float2x2 Rotate2x2(in float a) { @@ -40,7 +31,7 @@ internal readonly partial struct TriangleGridContouring : ID2D1PixelShader p = Hlsl.Frac(new float2(262144, 32768) * n); - return Hlsl.Sin((p * 6.2831853f) + this.time); + return Hlsl.Sin((p * 6.2831853f) + time); } // Based on IQ's gradient noise formula. @@ -298,12 +289,12 @@ internal readonly partial struct TriangleGridContouring : ID2D1PixelShader 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); + int2 coordinate = new(xy.X, dispatchSize.Y - xy.Y); + float2 uv = (coordinate - ((float2)dispatchSize * 0.5f)) / Hlsl.Min(650.0f, dispatchSize.Y); + float2 p = Hlsl.Mul(Rotate2x2(3.14159f / 12.0f), uv) + (new float2(0.8660254f, 0.5f) * time / 16.0f); float3 col = SimplexContour(p); - uv = coordinate / (float2)this.dispatchSize; + uv = coordinate / (float2)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); diff --git a/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/TwoTiledTruchet.cs b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/TwoTiledTruchet.cs index 4e61a9a1..441787b8 100644 --- a/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/TwoTiledTruchet.cs +++ b/samples/ComputeSharp.SwapChain.Shaders.D2D1.Shared/TwoTiledTruchet.cs @@ -7,23 +7,14 @@ namespace ComputeSharp.SwapChain.Shaders.D2D1; /// Ported from . /// Created by Shane. /// +/// The current time since the start of the application. +/// The dispatch size for the current output. [D2DInputCount(0)] [D2DRequiresScenePosition] [D2DShaderProfile(D2D1ShaderProfile.PixelShader50)] [D2DGeneratedPixelShaderDescriptor] -[AutoConstructor] -internal readonly partial struct TwoTiledTruchet : ID2D1PixelShader +internal readonly partial struct TwoTiledTruchet(float time, int2 dispatchSize) : ID2D1PixelShader { - /// - /// The current time since the start of the application. - /// - private readonly float time; - - /// - /// The dispatch size for the current output. - /// - private readonly int2 dispatchSize; - /// /// Calculates the Truchet distance field. /// @@ -114,7 +105,7 @@ internal readonly partial struct TwoTiledTruchet : ID2D1PixelShader ang *= 2.0f; } - ang = Hlsl.Frac(ang + (this.time / 4.0f)); + ang = Hlsl.Frac(ang + (time / 4.0f)); return d; } @@ -123,10 +114,10 @@ internal readonly partial struct TwoTiledTruchet : ID2D1PixelShader public float4 Execute() { int2 xy = (int2)D2D.GetScenePosition().XY; - float2 uv = (xy - ((float2)this.dispatchSize * 0.5f)) / this.dispatchSize.Y; + float2 uv = (xy - ((float2)dispatchSize * 0.5f)) / 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; + float2 p = (uv * gSc) - (new float2(-1, -0.5f) * time / 2.0f); + float sf = 2.0f / dispatchSize.Y * gSc; float lSc = 6.0f; float lw = 1.0f / lSc / gSc; float2 d = DistanceField(p, out float2 ang) - (2.5f / lSc); @@ -135,7 +126,7 @@ internal readonly partial struct TwoTiledTruchet : ID2D1PixelShader 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 tracks = Hlsl.Clamp(Hlsl.Sin((ang[i] * 6.2831f) + (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);