Add Unit Test for new FourColorGradient Shader

このコミットが含まれているのは:
Michael A. Hawker committed 2021-11-07 15:07:40 -08:00
1 親 6a1d1e2b13
コミット c5cac584d4
4ファイル変更
+124

検索条件に一致するファイルはありません

バイナリファイルは表示されません。

変更後

幅:  |  高さ:  |  サイズ: 280 KiB

+4
ファイルの表示
@@ -30,6 +30,7 @@
<Compile Include="..\..\samples\ComputeSharp.SwapChain\Shaders\HelloWorld.cs" Link="Shaders\Pixel\HelloWorld.cs" />
<Compile Include="..\..\samples\ComputeSharp.SwapChain\Shaders\ColorfulInfinity.cs" Link="Shaders\Pixel\ColorfulInfinity.cs" />
<Compile Include="..\..\samples\ComputeSharp.SwapChain\Shaders\ContouredLayers.cs" Link="Shaders\Pixel\ContouredLayers.cs" />
<Compile Include="..\..\samples\ComputeSharp.SwapChain\Shaders\FourColorGradient.cs" Link="Shaders\Pixel\FourColorGradient.cs" />
<Compile Include="..\..\samples\ComputeSharp.SwapChain\Shaders\ExtrudedTruchetPattern.cs" Link="Shaders\Pixel\ExtrudedTruchetPattern.cs" />
<Compile Include="..\..\samples\ComputeSharp.SwapChain\Shaders\FractalTiling.cs" Link="Shaders\Pixel\FractalTiling.cs" />
<Compile Include="..\..\samples\ComputeSharp.SwapChain\Shaders\MengerJourney.cs" Link="Shaders\Pixel\MengerJourney.cs" />
@@ -58,6 +59,9 @@
<None Include="Assets\ExtrudedTruchetPattern.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Assets\FourColorGradient.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Assets\FractalTiling.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
+111
ファイルの表示
@@ -0,0 +1,111 @@
namespace ComputeSharp.SwapChain.Shaders.Compute;
/// <summary>
/// A shader which interpolates four different colors into a rotating gradient pattern.
/// Ported from <see href="https://www.shadertoy.com/view/7lc3R4"/>.
/// <para>Created by Inigo Quilez.</para>
/// <para>License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.</para>
/// </summary>
[AutoConstructor]
internal readonly partial struct FourColorGradient : IComputeShader
{
/// <summary>
/// The target texture.
/// </summary>
public readonly IReadWriteTexture2D<float4> texture;
/// <summary>
/// The current time since the start of the application.
/// </summary>
public readonly float time;
// Colors to blend together
private static readonly float3 colorOne = new(0.999f, 0.999f, 0.999f);
private static readonly float3 colorTwo = new(0.999f, 0f, 0f);
private static readonly float3 colorThree = new(0f, 0.999f, 0f);
private static readonly float3 colorFour = new(0f, 0f, 0.999f);
/// <summary>
/// Standard 2D rotation formula.
/// </summary>
private static float2x2 Rotate(float a)
{
float c = Hlsl.Cos(a);
float s = Hlsl.Sin(a);
return new(c, s, -s, c);
}
/// <summary>
/// Generate a hash from a vector.
/// </summary>
/// <param name="p">Vector to hash</param>
/// <returns>Hashed vector</returns>
private static float2 Hash(float2 p)
{
p = new float2(Hlsl.Dot(p, new float2(2127.1f, 81.17f)), Hlsl.Dot(p, new float2(1269.5f, 283.37f)));
return Hlsl.Frac(Hlsl.Sin(p) * 43758.5453f);
}
/// <summary>
/// Generate a smoothed noise pattern.
/// </summary>
/// <param name="p">Position vector</param>
/// <returns>Depth</returns>
private static float Noise(float2 p)
{
float2 i = Hlsl.Floor(p);
float2 f = Hlsl.Frac(p);
float2 u = f * f * (3.0f - 2.0f * f);
// Mix -> Lerp
float n = Hlsl.Lerp(
Hlsl.Lerp(
Hlsl.Dot(-1.0f + 2.0f * Hash(i + new float2(0.0f, 0.0f)), f - new float2(0.0f, 0.0f)),
Hlsl.Dot(-1.0f + 2.0f * Hash(i + new float2(1.0f, 0.0f)), f - new float2(1.0f, 0.0f)),
u.X),
Hlsl.Lerp(
Hlsl.Dot(-1.0f + 2.0f * Hash(i + new float2(0.0f, 1.0f)), f - new float2(0.0f, 1.0f)),
Hlsl.Dot(-1.0f + 2.0f * Hash(i + new float2(1.0f, 1.0f)), f - new float2(1.0f, 1.0f)),
u.X),
u.Y);
return 0.5f + 0.5f * n;
}
/// <inheritdoc/>
public void Execute()
{
float2 fragCoord = new(ThreadIds.X, DispatchSize.Y - ThreadIds.Y);
float2 uv = fragCoord / DispatchSize.XY;
float ratio = DispatchSize.X / DispatchSize.Y;
float2 tuv = uv;
tuv -= 0.5f;
// Rotate with noise
float degree = Noise(new float2(time * 0.1f, tuv.X * tuv.Y));
tuv.Y *= 1.0f / ratio;
tuv.XY = Hlsl.Mul(tuv.XY, Rotate(Hlsl.Radians((degree - 0.5f) * 720.0f + 180.0f)));
tuv.Y *= ratio;
// Wave warp with sin
float frequency = 5.0f;
float amplitude = 30.0f;
float speed = time * 2.0f;
tuv.X += Hlsl.Sin(tuv.Y * frequency + speed) / amplitude;
tuv.Y += Hlsl.Sin(tuv.X * frequency * 1.5f + speed) / (amplitude * 0.5f);
// Draw the image
float3 layer1 = Hlsl.Lerp(colorOne, colorTwo, Hlsl.SmoothStep(-0.3f, 0.2f, Hlsl.Mul(tuv.XY, Rotate(Hlsl.Radians(-5.0f))).X));
float3 layer2 = Hlsl.Lerp(colorThree, colorFour, Hlsl.SmoothStep(-0.3f, 0.2f, Hlsl.Mul(tuv.XY, Rotate(Hlsl.Radians(-5.0f))).X));
float3 finalComp = Hlsl.Lerp(layer1, layer2, Hlsl.SmoothStep(0.5f, -0.3f, tuv.Y));
float3 col = finalComp;
texture[ThreadIds.XY] = new(col, 1.0f);
}
}
+9
ファイルの表示
@@ -34,6 +34,15 @@ public class ShadersTests
RunAndCompareShader(device, shaderType, 0.0000004f);
}
[CombinatorialTestMethod]
[AllDevices]
[Data(typeof(FourColorGradient))]
[Data(typeof(SwapChain.Shaders.Compute.FourColorGradient))]
public void FourColorGradient(Device device, Type shaderType)
{
RunAndCompareShader(device, shaderType, 0.00021f);
}
[CombinatorialTestMethod]
[AllDevices]
[Data(typeof(FractalTiling))]