Add D2D1 ContouredLayers test

このコミットが含まれているのは:
Sergio Pedri committed 2022-10-15 22:30:45 +02:00
1 親 b6325b5c74
コミット f2f7402bc2
5ファイル変更
+263 -7

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

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

変更後

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

+5 -4
ファイルの表示
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net472;netcoreapp3.1;net6.0</TargetFrameworks>
@@ -62,9 +62,6 @@
<None Include="..\ComputeSharp.Tests\Assets\ColorfulInfinity.png" Link="Assets\ColorfulInfinity.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="..\ComputeSharp.Tests\Assets\ContouredLayers.png" Link="Assets\ContouredLayers.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="..\ComputeSharp.Tests\Assets\ExtrudedTruchetPattern.png" Link="Assets\ExtrudedTruchetPattern.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
@@ -92,6 +89,10 @@
<None Update="Assets\TwoTiledTruchet.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Assets\ContouredLayers.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="..\..\samples\ComputeSharp.SwapChain\Textures\RustyMetal.png" Link="Assets\Textures\RustyMetal.png" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
</Project>
+13 -3
ファイルの表示
@@ -67,13 +67,15 @@ internal static class D2D1TestRunner
/// <param name="destinationFileName">The name of the destination image to save results to.</param>
/// <param name="shader">The shader to run.</param>
/// <param name="threshold">The allowed difference threshold for the normalized delta.</param>
/// <param name="resourceTextures">The additional resource textures to use to run the shader.</param>
public static void RunAndCompareShader<T>(
in T shader,
int width,
int height,
string expectedFileName,
[CallerMemberName] string destinationFileName = "",
float threshold = 0.00001f)
float threshold = 0.00001f,
params (int Index, D2D1ResourceTextureManager ResourceTextureManager)[] resourceTextures)
where T : unmanaged, ID2D1PixelShader
{
string assetsPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Assets");
@@ -90,7 +92,8 @@ internal static class D2D1TestRunner
null,
width,
height,
destinationPath);
destinationPath,
resourceTextures);
// Compare the results
TolerantImageComparer.AssertEqual(destinationPath, expectedPath, threshold);
@@ -145,12 +148,14 @@ internal static class D2D1TestRunner
/// <param name="height">The resulting height.</param>
/// <param name="transformMapperFactory">A custom <see cref="ID2D1TransformMapper{T}"/> factory for the effect.</param>
/// <param name="destinationPath">The destination path for the result.</param>
/// <param name="resourceTextures">The additional resource textures to use to run the shader.</param>
private static unsafe void ExecutePixelShaderAndSaveToFile<T>(
in T shader,
ID2D1TransformMapperFactory<T>? transformMapperFactory,
int width,
int height,
string destinationPath)
string destinationPath,
params (int Index, D2D1ResourceTextureManager ResourceTextureManager)[] resourceTextures)
where T : unmanaged, ID2D1PixelShader
{
using ComPtr<ID2D1Factory2> d2D1Factory2 = D2D1Helper.CreateD2D1Factory2();
@@ -165,6 +170,11 @@ internal static class D2D1TestRunner
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(in shader, d2D1Effect.Get());
foreach ((int index, D2D1ResourceTextureManager resourceTextureManager) in resourceTextures)
{
D2D1PixelShaderEffect.SetResourceTextureManagerForD2D1Effect(d2D1Effect.Get(), resourceTextureManager, index);
}
using ComPtr<ID2D1Bitmap> d2D1BitmapTarget = D2D1Helper.CreateD2D1BitmapAndSetAsTarget(d2D1DeviceContext.Get(), (uint)width, (uint)height);
D2D1Helper.DrawEffect(d2D1DeviceContext.Get(), d2D1Effect.Get());
+209
ファイルの表示
@@ -0,0 +1,209 @@
namespace ComputeSharp.D2D1.Tests.Shaders;
/// <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>
[D2DInputCount(0)]
[D2DRequiresScenePosition]
[D2DShaderProfile(D2D1ShaderProfile.PixelShader50)]
[AutoConstructor]
internal readonly partial struct ContouredLayers : ID2D1PixelShader
{
/// <summary>
/// The current time Hlsl.Since the start of the application.
/// </summary>
private readonly float time;
/// <summary>
/// The dispatch size for the current output.
/// </summary>
public readonly int2 dispatchSize;
/// <summary>
/// The background texture to sample.
/// </summary>
[D2DResourceTextureIndex(0)]
private readonly D2D1ResourceTexture2D<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) + this.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 = this.texture.Sample(p + Hash21(new float2(sh, fi))).XYZ;
tx *= tx;
float3 col;
col = (float3)1.0f * (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 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;
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 * this.dispatchSize.Y / 1.0f) + 1.7f) - Noise2D((uv * this.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;
col *= Hlsl.Pow(Hlsl.Abs(16.0f * uv.X * uv.Y * (1.0f - uv.X) * (1.0f - uv.Y)), 0.0625f);
return new float4(Hlsl.Sqrt(Hlsl.Max(col, 0.0f)), 1);
}
}
+36
ファイルの表示
@@ -1,6 +1,12 @@
using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using ComputeSharp.D2D1.Interop;
using ComputeSharp.D2D1.Tests.Helpers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
namespace ComputeSharp.D2D1.Tests;
@@ -62,6 +68,36 @@ public class ShadersTests
RunTest<Shaders.TriangleGridContouring>();
}
[TestMethod]
public unsafe void ContouredLayers()
{
string assemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!;
string expectedPath = Path.Combine(assemblyPath, "Assets", "Textures", "RustyMetal.png");
D2D1ResourceTextureManager resourceTextureManager;
using (Image<Rgba32> texture = Image.Load<Rgba32>(expectedPath))
{
if (!texture.DangerousTryGetSinglePixelMemory(out Memory<Rgba32> pixels))
{
Assert.Inconclusive();
}
resourceTextureManager = new D2D1ResourceTextureManager(
extents: stackalloc uint[] { (uint)texture.Width, (uint)texture.Height },
bufferPrecision: D2D1BufferPrecision.UInt8Normalized,
channelDepth: D2D1ChannelDepth.Four,
filter: D2D1Filter.MinMagMipLinear,
extendModes: stackalloc D2D1ExtendMode[] { D2D1ExtendMode.Mirror, D2D1ExtendMode.Mirror },
data: MemoryMarshal.AsBytes(pixels.Span),
strides: stackalloc uint[] { (uint)(texture.Width * sizeof(Rgba32)) });
}
Shaders.ContouredLayers shader = new(0f, new int2(1280, 720));
D2D1TestRunner.RunAndCompareShader(in shader, 1280, 720, $"{nameof(Shaders.ContouredLayers)}.png", nameof(Shaders.ContouredLayers), resourceTextures: (0, resourceTextureManager));
}
private static void RunTest<T>(float threshold = 0.00001f)
where T : unmanaged, ID2D1PixelShader
{