From 6255c348cb279f38ac5f9b95345bea3162b92433 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 7 Mar 2021 20:46:58 +0100 Subject: [PATCH] Refactored sample shaders to use shader constants --- .../Shaders/MengerJourney.cs | 15 ++++++++++----- .../Shaders/ProteanClouds.cs | 4 ++-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/samples/ComputeSharp.SwapChain/Shaders/MengerJourney.cs b/samples/ComputeSharp.SwapChain/Shaders/MengerJourney.cs index 024ef622..f4446dc6 100644 --- a/samples/ComputeSharp.SwapChain/Shaders/MengerJourney.cs +++ b/samples/ComputeSharp.SwapChain/Shaders/MengerJourney.cs @@ -30,6 +30,12 @@ private const float Ambient = 0.32184f; private const float Diffuse = 0.5f; + private static Float3 LightDir => 1.0f; + private static Float3 LightColor => new(1.0f, 1.0f, 0.858824f); + private static Float3 LightDir2 => new(1.0f, -1.0f, 1.0f); + private static Float3 LightColor2 => new(0, 0.333333f, 1.0f); + private static Float3 Offset => new(0.92858f, 0.92858f, 0.32858f); + /// /// Rotates a given value by a specified amount. /// @@ -44,15 +50,15 @@ /// private static Float3 GetLight(in Float3 color, in Float3 normal) { - Float3 lightDir = Hlsl.Normalize((Float3)1); + Float3 lightDir = Hlsl.Normalize(LightDir); float diffuse = Hlsl.Max(0.0f, Hlsl.Dot(-normal, lightDir)); - Float3 lightDir2 = Hlsl.Normalize(new Float3(1.0f, -1.0f, 1.0f)); + Float3 lightDir2 = Hlsl.Normalize(LightDir2); 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); + (diffuse * Diffuse) * (LightColor * color) + + (diffuse2 * Diffuse) * (LightColor2 * color); } /// @@ -70,7 +76,6 @@ 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++) { diff --git a/samples/ComputeSharp.SwapChain/Shaders/ProteanClouds.cs b/samples/ComputeSharp.SwapChain/Shaders/ProteanClouds.cs index 1a197dce..5909f259 100644 --- a/samples/ComputeSharp.SwapChain/Shaders/ProteanClouds.cs +++ b/samples/ComputeSharp.SwapChain/Shaders/ProteanClouds.cs @@ -19,6 +19,8 @@ /// public readonly float time; + private static 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), s = Hlsl.Sin(a); @@ -59,8 +61,6 @@ z *= 0.57f; trk *= 1.4f; - Float3x3 m3 = new Float3x3(0.33338f, 0.56034f, -0.71817f, -0.87887f, 0.32651f, -0.15323f, 0.15162f, 0.69596f, 0.61339f) * 1.93f; - p = Hlsl.Mul(p, m3); }