Added unit test for ContouredLayers shader

このコミットが含まれているのは:
Sergio Pedri committed 2021-05-11 01:54:37 +02:00
1 親 4f8287cabf
コミット 37a6e5a7b3
4ファイル変更
+282 -1

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

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

変更後

幅:  |  高さ:  |  サイズ: 2.3 MiB

+6 -1
ファイルの表示
@@ -28,6 +28,7 @@
<Compile Include="..\..\samples\ComputeSharp.ImageProcessing\Processors\HlslGaussianBlurProcessor.cs" Link="Imaging\Processors\HlslGaussianBlurProcessor.cs" />
<Compile Include="..\..\samples\ComputeSharp.ImageProcessing\Processors\HlslGaussianBlurProcessor{TPixel}.cs" Link="Imaging\Processors\HlslGaussianBlurProcessor{TPixel}.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\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" />
@@ -39,13 +40,17 @@
</ItemGroup>
<ItemGroup>
<Content Include="..\..\samples\ComputeSharp.ImageProcessing\city.jpg" Link="Imaging\city.jpg" CopyToOutputDirectory="PreserveNewest" />
<None Include="..\..\samples\ComputeSharp.ImageProcessing\city.jpg" Link="Imaging\city.jpg" CopyToOutputDirectory="PreserveNewest" />
<None Include="..\..\samples\ComputeSharp.SwapChain\Textures\RustyMetal.png" Link="Shaders\Textures\RustyMetal.png" CopyToOutputDirectory="PreserveNewest" />
<None Update="Assets/CityAfter1024x1024Sampling.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Assets\ColorfulInfinity.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Assets\ContouredLayers.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="Assets\ExtrudedTruchetPattern.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
+202
ファイルの表示
@@ -0,0 +1,202 @@
namespace ComputeSharp.SwapChain.Shaders.Compute
{
/// <summary>
/// Constructing some concise contoured layers, then applying various edge and shading effects to produce some faux depth.
/// Ported from <see href="https://www.shadertoy.com/view/3lj3zt"/>.
/// <para>Created by Shane.</para>
/// </summary>
[AutoConstructor]
internal readonly partial struct ContouredLayers : IComputeShader
{
/// <summary>
/// The target texture.
/// </summary>
public readonly IReadWriteTexture2D<Float4> destination;
/// <summary>
/// The current time Hlsl.Since the start of the application.
/// </summary>
public readonly float time;
/// <summary>
/// The sampling texture.
/// </summary>
public readonly IReadOnlyTexture2D<Float4> texture;
// Float3 to float hash.
private static float Hash21(Float2 p)
{
return Hlsl.Frac(Hlsl.Sin(Hlsl.Dot(p, new Float2(41, 289))) * 45758.5453f);
}
// Float2 to Float2 hash.
private Float2 Hash22(Float2 p)
{
float n = Hlsl.Sin(Hlsl.Dot(p, new Float2(1, 113)));
p = Hlsl.Frac(new Float2(262144, 32768) * n);
return Hlsl.Sin(p * 6.2831853f + time);
}
// Float2 to Float2 hash.
private static Float2 Hash22B(Float2 p)
{
float n = Hlsl.Sin(Hlsl.Dot(p, new Float2(41, 289)));
return Hlsl.Frac(new Float2(262144, 32768) * n) * 2.0f - 1.0f;
}
// Smooth 2D noise
private static float Noise2D(Float2 p)
{
Float2 i = Hlsl.Floor(p);
p -= i;
Float4 v = default;
v.X = Hlsl.Dot(Hash22B(i), p);
v.Y = Hlsl.Dot(Hash22B(i + Float2.UnitX), p - Float2.UnitX);
v.Z = Hlsl.Dot(Hash22B(i + Float2.UnitY), p - Float2.UnitY);
v.W = Hlsl.Dot(Hash22B(i + 1.0f), p - 1.0f);
p = p * p * (3.0f - 2.0f * p);
return Hlsl.Lerp(Hlsl.Lerp(v.X, v.Y, p.X), Hlsl.Lerp(v.Z, v.W, p.X), p.Y);
}
// Based on IQ's gradient noise formula.
private float Noise2D3G(Float2 p)
{
Float2 i = Hlsl.Floor(p);
p -= i;
Float4 v = default;
v.X = Hlsl.Dot(Hash22(i), p);
v.Y = Hlsl.Dot(Hash22(i + Float2.UnitX), p - Float2.UnitX);
v.Z = Hlsl.Dot(Hash22(i + Float2.UnitY), p - Float2.UnitY);
v.W = Hlsl.Dot(Hash22(i + 1.0f), p - 1.0f);
p = p * p * p * (p * (p * 6.0f - 15.0f) + 10.0f);
return Hlsl.Lerp(Hlsl.Lerp(v.X, v.Y, p.X), Hlsl.Lerp(v.Z, v.W, p.X), p.Y);
}
// Map function with noise layers
private float MapNoise(Float3 p, float i)
{
return Noise2D3G(p.XY * 3.0f) * 0.66f + Noise2D3G(p.XY * 6.0f) * 0.34f + i / 10.0f * 1.0f - 0.15f;
}
// 2D derivative function.
private Float3 GetNormal(in Float3 p, float m, float i)
{
Float2 e = new(0.001f, 0);
return new Float3(m - MapNoise(p - e.XYY, i), m - MapNoise(p - e.YXY, i), 0.0f) / e.X * 1.4142f;
}
// The map layer and its derivative
private Float4 MapLayer(in Float3 p, float i)
{
Float4 d = default;
d.X = MapNoise(p, i);
d.YZW = GetNormal(p, d.X, i);
return d;
}
// Layer color. Based on the shade, layer number and smoothing factor.
private Float3 GetColor(Float2 p, float sh, float fi)
{
Float3 tx = texture[p + Hash21(new Float2(sh, fi))].XYZ;
tx *= tx;
Float3 col;
col = (Float3)1.0 * (1.0f - 0.75f / (1.0f + sh * sh * 2.0f));
col = Hlsl.Min(col * tx * 3.0f, 1.0f);
return col;
}
// A hatch-like algorithm, or a stipple... or some kind of textured pattern.
private float Hatch(Float2 p, float res)
{
p *= res / 16.0f;
float hatch = Hlsl.Clamp(Hlsl.Sin((p.X - p.Y) * 3.14159f * 200.0f) * 2.0f + 0.5f, 0.0f, 1.0f);
float hRnd = Hash21(Hlsl.Floor(p * 6.0f) + 0.73f);
if (hRnd > 0.66f) hatch = hRnd;
hatch = hatch * 0.75f + 0.5f;
return hatch;
}
/// <inheritdoc/>
public void Execute()
{
Float2 fragCoord = new(ThreadIds.X, DispatchSize.Y - ThreadIds.Y);
float res = Hlsl.Min(DispatchSize.Y, 700.0f);
Float2 uv = (fragCoord - (Float2)DispatchSize.XY * 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);
col *= hatch;
const int lNum = 5;
float flNum = lNum;
for (int i = 0; i < lNum; i++)
{
float fi = i;
hatch = Hatch(uv + Hlsl.Sin(new Float2(41.0f, 289.0f) * (fi + 1.0f)), res);
Float4 c = MapLayer(new Float3(uv, 1.0f), fi);
Float4 cSh = MapLayer(new Float3(uv - new Float2(0.03f, -0.03f) * ((flNum - fi) / flNum * 0.5f + 0.5f), 1.0f), fi);
float sh = (fi + 1.0f) / (flNum);
Float3 lCol = GetColor(uv, sh, fi + 1.0f);
Float3 ld = Hlsl.Normalize(new Float3(-1, 1, -0.25f));
Float3 n = Hlsl.Normalize(new Float3(0.0f, 0.0f, -1.0f) + c.YZW);
float diff = Hlsl.Max(Hlsl.Dot(ld, n), 0.0f);
diff *= 2.5f;
Float3 eCol = lCol * (diff + 1.0f);
float sfL = sf * Hlsl.Length(c.YZX) * 2.0f;
float sfLSh = sf * Hlsl.Length(cSh.YZX) * 6.0f;
const float shF = 0.5f;
col = Hlsl.Lerp(col, Float3.Zero, (1.0f - Hlsl.SmoothStep(0.0f, sfLSh, Hlsl.Max(cSh.X, pL))) * shF);
col = Hlsl.Lerp(col, Float3.Zero, (1.0f - Hlsl.SmoothStep(0.0f, sfL * 3.0f, c.X)) * 0.25f);
col = Hlsl.Lerp(col, Float3.Zero, (1.0f - Hlsl.SmoothStep(0.0f, sfL, c.X)) * 0.85f);
col = Hlsl.Lerp(col, eCol * hatch, (1.0f - Hlsl.SmoothStep(0.0f, sfL, c.X + Hlsl.Length(c.YZX) * 0.003f)));
col = Hlsl.Lerp(col, lCol * hatch, (1.0f - Hlsl.SmoothStep(0.0f, sfL, c.X + Hlsl.Length(c.YZX) * 0.006f)));
pL = c.X;
}
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 * 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 / DispatchSize.XY;
col *= Hlsl.Pow(16.0f * uv.X * uv.Y * (1.0f - uv.X) * (1.0f - uv.Y), 0.0625f);
destination[ThreadIds.XY] = new Float4(Hlsl.Sqrt(Hlsl.Max(col, 0.0f)), 1);
}
}
}
+74
ファイルの表示
@@ -97,11 +97,30 @@ namespace ComputeSharp.Tests
RunAndCompareShader(device, shaderType, 0.0006f);
}
[CombinatorialTestMethod]
[AllDevices]
[Data(typeof(ContouredLayers))]
[Data(typeof(SwapChain.Shaders.Compute.ContouredLayers))]
public void ContouredLayers(Device device, Type shaderType)
{
string filename = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Shaders", "Textures", "RustyMetal.png");
using ReadOnlyTexture2D<Rgba32, Float4> background = device.Get().LoadTexture(filename);
RunAndCompareShader(
device,
shaderType,
texture => new SwapChain.Shaders.Compute.ContouredLayers(texture, 0, background),
texture => new ContouredLayers(0, background),
0.0007f);
}
/// <summary>
/// Executes a given test for a specified shader.
/// </summary>
/// <typeparam name="T">The type of shader to test.</typeparam>
/// <param name="device">The device to use.</param>
/// <param name="shaderType">The type of shader being executed.</param>
/// <param name="delta">The comparison delta.</param>
private static void RunAndCompareShader(Device device, Type shaderType, float delta)
{
@@ -153,5 +172,60 @@ namespace ComputeSharp.Tests
ImagingTests.TolerantImageComparer.AssertEqual(expectedPath, actualPath, delta);
}
/// <summary>
/// Executes a given test for a specified shader.
/// </summary>
/// <typeparam name="T">The type of shader to test.</typeparam>
/// <param name="device">The device to use.</param>
/// <param name="shaderType">The type of shader being executed.</param>
/// <param name="computeFactory">The factory of compute shaders.</param>
/// <param name="pixelFactory">The factory of pixel shaders.</param>
/// <param name="delta">The comparison delta.</param>
private static void RunAndCompareShader<TCompute, TPixel>(
Device device,
Type shaderType,
Func<ReadWriteTexture2D<Rgba32, Float4>, TCompute> computeFactory,
Func<ReadWriteTexture2D<Rgba32, Float4>, TPixel> pixelFactory,
float delta)
where TCompute : struct, IComputeShader
where TPixel : struct, IPixelShader<Float4>
{
_ = device.Get();
string expectedPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Assets", $"{shaderType.Name}.png");
IImageInfo imageInfo = Image.Identify(expectedPath);
using Image<ImageSharpRgba32> image = new(imageInfo.Width, imageInfo.Height);
using (ReadWriteTexture2D<Rgba32, Float4> texture = device.Get().AllocateReadWriteTexture2D<Rgba32, Float4>(imageInfo.Width, imageInfo.Height))
{
if (shaderType.IsAssignableTo(typeof(IComputeShader)))
{
Assert.AreEqual(typeof(TCompute), shaderType);
texture.GraphicsDevice.For(texture.Width, texture.Height, computeFactory(texture));
}
else
{
Assert.AreEqual(typeof(TPixel), shaderType);
texture.GraphicsDevice.ForEach(texture, pixelFactory(texture));
}
_ = image.TryGetSinglePixelSpan(out Span<ImageSharpRgba32> span);
texture.CopyTo(MemoryMarshal.Cast<ImageSharpRgba32, Rgba32>(span));
}
string actualPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Shaders", $"{shaderType.Name}.png");
_ = Directory.CreateDirectory(Path.GetDirectoryName(actualPath)!);
image.SaveAsPng(actualPath, new PngEncoder() { CompressionLevel = PngCompressionLevel.BestCompression, ColorType = PngColorType.Rgb });
ImagingTests.TolerantImageComparer.AssertEqual(expectedPath, actualPath, delta);
}
}
}