Enabled shader tests for both compute and pixel modes
このコミットが含まれているのは:
1 親
877b32befc
コミット
4544a3a581
11ファイル変更
+1592
-34
検索条件に一致するファイルはありません
@@ -27,15 +27,15 @@
|
||||
<Compile Include="..\..\samples\ComputeSharp.ImageProcessing\Processors\HlslBokehBlurProcessor{TPixel}.cs" Link="Imaging\Processors\HlslBokehBlurProcessor{TPixel}.cs" />
|
||||
<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\ColorfulInfinity.cs" />
|
||||
<Compile Include="..\..\samples\ComputeSharp.SwapChain\Shaders\ExtrudedTruchetPattern.cs" Link="Shaders\ExtrudedTruchetPattern.cs" />
|
||||
<Compile Include="..\..\samples\ComputeSharp.SwapChain\Shaders\FractalTiling.cs" Link="Shaders\FractalTiling.cs" />
|
||||
<Compile Include="..\..\samples\ComputeSharp.SwapChain\Shaders\MengerJourney.cs" Link="Shaders\MengerJourney.cs" />
|
||||
<Compile Include="..\..\samples\ComputeSharp.SwapChain\Shaders\TwoTiledTruchet.cs" Link="Shaders\TwoTiledTruchet.cs" />
|
||||
<Compile Include="..\..\samples\ComputeSharp.SwapChain\Shaders\Octagrams.cs" Link="Shaders\Octagrams.cs" />
|
||||
<Compile Include="..\..\samples\ComputeSharp.SwapChain\Shaders\ProteanClouds.cs" Link="Shaders\ProteanClouds.cs" />
|
||||
<Compile Include="..\..\samples\ComputeSharp.SwapChain\Shaders\PyramidPattern.cs" Link="Shaders\PyramidPattern.cs" />
|
||||
<Compile Include="..\..\samples\ComputeSharp.SwapChain\Shaders\TriangleGridContouring.cs" Link="Shaders\TriangleGridContouring.cs" />
|
||||
<Compile Include="..\..\samples\ComputeSharp.SwapChain\Shaders\ColorfulInfinity.cs" Link="Shaders\Pixel\ColorfulInfinity.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" />
|
||||
<Compile Include="..\..\samples\ComputeSharp.SwapChain\Shaders\TwoTiledTruchet.cs" Link="Shaders\Pixel\TwoTiledTruchet.cs" />
|
||||
<Compile Include="..\..\samples\ComputeSharp.SwapChain\Shaders\Octagrams.cs" Link="Shaders\Pixel\Octagrams.cs" />
|
||||
<Compile Include="..\..\samples\ComputeSharp.SwapChain\Shaders\ProteanClouds.cs" Link="Shaders\Pixel\ProteanClouds.cs" />
|
||||
<Compile Include="..\..\samples\ComputeSharp.SwapChain\Shaders\PyramidPattern.cs" Link="Shaders\Pixel\PyramidPattern.cs" />
|
||||
<Compile Include="..\..\samples\ComputeSharp.SwapChain\Shaders\TriangleGridContouring.cs" Link="Shaders\Pixel\TriangleGridContouring.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
namespace ComputeSharp.SwapChain.Shaders.Compute
|
||||
{
|
||||
/// <summary>
|
||||
/// A shader creating an abstract and colorful animation.
|
||||
/// Ported from <see href="https://www.shadertoy.com/view/WtjyzR"/>.
|
||||
/// <para>Created by Benoit Marini.</para>
|
||||
/// <para>License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.</para>
|
||||
/// </summary>
|
||||
[AutoConstructor]
|
||||
internal readonly partial struct ColorfulInfinity : 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;
|
||||
|
||||
/// <summary>
|
||||
/// The total number of layers for the final animation.
|
||||
/// </summary>
|
||||
private const float NumberOfLayers = 16.0f;
|
||||
|
||||
/// <summary>
|
||||
/// The number of iterations to calculate a texel value.
|
||||
/// </summary>
|
||||
private const int NumberOfIterations = 23;
|
||||
|
||||
/// <summary>
|
||||
/// Makes some magic happen.
|
||||
/// </summary>
|
||||
private Float4 Tex(Float3 p)
|
||||
{
|
||||
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) +
|
||||
new Float4(0.06f * Hlsl.Cos(t * 0.1f), 0, 0, 0.14f * Hlsl.Cos(t * 0.23f));
|
||||
|
||||
for (int i = 0; i++ < NumberOfIterations;)
|
||||
{
|
||||
o.XZYW = Hlsl.Abs(o / Hlsl.Dot(o, o) - dec);
|
||||
}
|
||||
|
||||
return o;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Execute()
|
||||
{
|
||||
Float2 uv = (ThreadIds.XY - (Float2)DispatchSize.XY * 0.5f) / DispatchSize.Y;
|
||||
Float3 col = 0;
|
||||
float t = time * 0.3f;
|
||||
|
||||
for (float i = 0.0f; i <= 1.0f; i += 1.0f / NumberOfLayers)
|
||||
{
|
||||
float d = Hlsl.Frac(i + t);
|
||||
float s = Hlsl.Lerp(5.0f, 0.5f, d);
|
||||
float f = d * Hlsl.SmoothStep(1.0f, 0.9f, d);
|
||||
|
||||
col += Tex(new Float3(uv.X * s, uv.Y * s, i * 4.0f)).XYZ * f;
|
||||
}
|
||||
|
||||
col /= NumberOfLayers;
|
||||
col *= new Float3(2, 1.0f, 2.0f);
|
||||
col = Hlsl.Pow(col, 0.5f);
|
||||
|
||||
Float4 color = new(col.X, col.Y, col.Z, 1.0f);
|
||||
|
||||
texture[ThreadIds.XY] = color;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,298 @@
|
||||
namespace ComputeSharp.SwapChain.Shaders.Compute
|
||||
{
|
||||
/// <summary>
|
||||
/// A basic extruded square grid-based blobby Truchet pattern.
|
||||
/// Ported from <see href="https://www.shadertoy.com/view/ttVBzd"/>.
|
||||
/// <para>Created by Shane.</para>
|
||||
/// </summary>
|
||||
[AutoConstructor]
|
||||
internal readonly partial struct ExtrudedTruchetPattern : 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;
|
||||
|
||||
/// <summary>
|
||||
/// Curve shape - Round: 0, Semi-round: 1, Octagonal: 2, Superellipse: 3, Straight: 4.
|
||||
/// </summary>
|
||||
private static readonly int SHAPE = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Object ID: Either the back plane or the metaballs.
|
||||
/// </summary>
|
||||
private static int objID;
|
||||
|
||||
// Standard 2D rotation formula.
|
||||
private static Float2x2 Rotate2x2(float a)
|
||||
{
|
||||
float c = Hlsl.Cos(a), s = Hlsl.Sin(a);
|
||||
|
||||
return new(c, -s, s, c);
|
||||
}
|
||||
|
||||
// IQ's Float2 to float hash.
|
||||
private static float Hash21(Float2 p)
|
||||
{
|
||||
return Hlsl.Frac(Hlsl.Sin(Hlsl.Dot(p, new Float2(27.619f, 57.583f))) * 43758.5453f);
|
||||
}
|
||||
|
||||
// Various distance metrics.
|
||||
private static float Distance(Float2 p)
|
||||
{
|
||||
if (SHAPE == 0)
|
||||
{
|
||||
return Hlsl.Length(p);
|
||||
}
|
||||
else p = Hlsl.Abs(p);
|
||||
|
||||
switch (SHAPE)
|
||||
{
|
||||
case 1: return Hlsl.Max(Hlsl.Length(p), (p.X + p.Y) * 0.7071f + 0.015f);
|
||||
case 2: return Hlsl.Max((p.X + p.Y) * 0.7071f, Hlsl.Max(p.X, p.Y));
|
||||
case 3: return Hlsl.Pow(Hlsl.Dot(Hlsl.Pow(p, 3), 1), 1.0f / 3.0f);
|
||||
default: return (p.X + p.Y) * 0.7071f;
|
||||
}
|
||||
}
|
||||
|
||||
// GLSL's mod function
|
||||
private static float Mod(float x, float y)
|
||||
{
|
||||
return x - y * Hlsl.Floor(x / y);
|
||||
}
|
||||
|
||||
// A standard square grid 2D blobby Truchet routine: Render circles
|
||||
// in opposite corners of a tile, reverse the pattern on alternate
|
||||
// checker tiles, and randomly rotate.
|
||||
private static float Truchet(Float2 p)
|
||||
{
|
||||
const float sc = 0.5f;
|
||||
Float2 ip = Hlsl.Floor(p / sc) + 0.5f;
|
||||
|
||||
p -= ip * sc;
|
||||
|
||||
float rnd = Hlsl.Frac(Hlsl.Sin(Hlsl.Dot(ip, new Float2(1, 113))) * 45758.5453f);
|
||||
|
||||
if (rnd < .5) p.Y = -p.Y;
|
||||
|
||||
float d = Hlsl.Min(Distance(p - 0.5f * sc), Distance(p + 0.5f * sc)) - 0.5f * sc;
|
||||
|
||||
if (SHAPE == 4)
|
||||
{
|
||||
d += (0.5f - 0.5f / Hlsl.Sqrt(2.0f)) * sc;
|
||||
}
|
||||
|
||||
if (rnd < 0.5f) d = -d;
|
||||
if (Mod(ip.X + ip.Y, 2.0f) < 0.5f) d = -d;
|
||||
|
||||
return d - 0.03f;
|
||||
}
|
||||
|
||||
// The scene's distance function: There'd be faster ways to do this, but it's
|
||||
// more readable this way. Plus, this is a pretty simple scene, so it's
|
||||
// efficient enough.
|
||||
private static float M(Float3 p)
|
||||
{
|
||||
float fl = -p.Z;
|
||||
float obj = Truchet(p.XY);
|
||||
|
||||
obj = Hlsl.Max(obj, Hlsl.Abs(p.Z) - 0.125f) - Hlsl.SmoothStep(0.03f, 0.25f, -obj) * 0.1f;
|
||||
|
||||
float studs = 1e5f;
|
||||
const float sc = 0.5f;
|
||||
Float2 q = p.XY + 0.5f * sc;
|
||||
Float2 iq = Hlsl.Floor(q / sc) + 0.5f;
|
||||
|
||||
q -= iq * sc;
|
||||
|
||||
if (Mod(iq.X + iq.Y, 2.0f) > 0.5f)
|
||||
{
|
||||
studs = Hlsl.Max(Hlsl.Length(q) - 0.1f * sc - 0.02f, Hlsl.Abs(p.Z) - 0.26f);
|
||||
}
|
||||
|
||||
objID = fl < obj && fl < studs ? 0 : obj < studs ? 1 : 2;
|
||||
|
||||
return Hlsl.Min(Hlsl.Min(fl, obj), studs);
|
||||
}
|
||||
|
||||
// Cheap shadows are hard. In fact, I'd almost say, shadowing particular scenes with limited
|
||||
// iterations is impossible... However, I'd be very grateful if someone could prove me wrong. :)
|
||||
private static float SoftShadow(Float3 ro, Float3 lp, Float3 n, float k)
|
||||
{
|
||||
const int iter = 24;
|
||||
|
||||
ro += n * 0.0015f;
|
||||
|
||||
Float3 rd = lp - ro;
|
||||
float shade = 1.0f;
|
||||
float t = 0;
|
||||
float end = Hlsl.Max(Hlsl.Length(rd), 0.0001f);
|
||||
|
||||
rd /= end;
|
||||
|
||||
for (int i = 0; i < iter; i++)
|
||||
{
|
||||
float d = M(ro + rd * t);
|
||||
|
||||
shade = Hlsl.Min(shade, k * d / t);
|
||||
t += Hlsl.Clamp(d, 0.01f, 0.25f);
|
||||
|
||||
if (d < 0.0f || t > end) break;
|
||||
}
|
||||
|
||||
return Hlsl.Max(shade, 0.0f);
|
||||
}
|
||||
|
||||
|
||||
// I keep a collection of occlusion routines... OK, that sounded really nerdy. :)
|
||||
// Anyway, I like this one. I'm assuHlsl.Ming it's based on IQ's original.
|
||||
private float CalculateAO(in Float3 p, in Float3 n)
|
||||
{
|
||||
float sca = 2.0f, occ = 0.0f;
|
||||
|
||||
for (int i = (int)Hlsl.Min(time, 0f); i < 5; i++)
|
||||
{
|
||||
float hr = (i + 1f) * 0.15f / 5.0f;
|
||||
float d = M(p + n * hr);
|
||||
occ += (hr - d) * sca;
|
||||
sca *= 0.7f;
|
||||
|
||||
if (sca > 1e5f) break;
|
||||
}
|
||||
|
||||
return Hlsl.Clamp(1.0f - occ, 0.0f, 1.0f);
|
||||
}
|
||||
|
||||
// Standard normal function.
|
||||
private static Float3 Normal(in Float3 p)
|
||||
{
|
||||
Float2 e = new(0.001f, 0);
|
||||
|
||||
return Hlsl.Normalize(new Float3(
|
||||
M(p + e.XYY) - M(p - e.XYY),
|
||||
M(p + e.YXY) - M(p - e.YXY),
|
||||
M(p + e.YYX) - M(p - e.YYX)));
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Execute()
|
||||
{
|
||||
Int2 coords = new(ThreadIds.X, DispatchSize.Y - ThreadIds.Y);
|
||||
Float2 u = (coords - (Float2)DispatchSize.XY * 0.5f) / DispatchSize.Y;
|
||||
Float3
|
||||
r = Hlsl.Normalize(new Float3(u, 1)),
|
||||
o = new(0, time / 2.0f, -3),
|
||||
l = o + new Float3(0.25f, 0.25f, 2.0f);
|
||||
|
||||
r.YZ = Hlsl.Mul(Rotate2x2(0.15f), r.YZ);
|
||||
r.XZ = Hlsl.Mul(Rotate2x2(-Hlsl.Cos(time * 3.14159f / 32.0f) / 8.0f), r.XZ);
|
||||
r.XY = Hlsl.Mul(Rotate2x2(Hlsl.Sin(time * 3.14159f / 32.0f) / 8.0f), r.XY);
|
||||
|
||||
float d, t = Hash21(r.XY * 57.0f + Hlsl.Frac(time)) * 0.5f, glow = 0;
|
||||
|
||||
for (int i = 0; i < 96; i++)
|
||||
{
|
||||
d = M(o + r * t);
|
||||
|
||||
if (Hlsl.Abs(d) < .001) break;
|
||||
|
||||
t += d * 0.7f;
|
||||
glow += 0.2f / (1.0f + Hlsl.Abs(d) * 5.0f);
|
||||
}
|
||||
|
||||
int gObjID = objID;
|
||||
Float3 p = o + r * t, n = Normal(p);
|
||||
Float2 uv = p.XY;
|
||||
float sc = 0.5f;
|
||||
Float2 iuv = Hlsl.Floor(uv / sc) + 0.5f;
|
||||
|
||||
uv -= iuv * sc;
|
||||
|
||||
Float2 uv2 = p.XY + 0.5f * sc;
|
||||
Float2 iuv2 = Hlsl.Floor(uv2 / sc) + 0.5f;
|
||||
|
||||
uv2 -= iuv2 * sc;
|
||||
|
||||
float bord = Hlsl.Max(Hlsl.Abs(uv.X), Hlsl.Abs(uv.Y)) - 0.5f * sc;
|
||||
|
||||
bord = Hlsl.Abs(bord) - 0.002f;
|
||||
d = Truchet(p.XY);
|
||||
|
||||
float lSc = 20.0f;
|
||||
float pat = (Hlsl.Abs(Hlsl.Frac((uv.X - uv.Y) * lSc - 0.5f) - 0.5f) * 2.0f - 0.5f) / lSc;
|
||||
float pat2 = (Hlsl.Abs(Hlsl.Frac((uv.X + uv.Y) * lSc + 0.5f) - 0.5f) * 2.0f - 0.5f) / lSc;
|
||||
float sf = Hlsl.Dot(Hlsl.Sin(p.XY - Hlsl.Cos(p.YX * 2.0f)), 0.5f);
|
||||
float sf2 = Hlsl.Dot(Hlsl.Sin(p.XY * 1.5f - Hlsl.Cos(p.YX * 3.0f)), 0.5f);
|
||||
Float4 col1 = Hlsl.Lerp(new Float4(1, 0.75f, 0.6f, 0), new Float4(1, 0.85f, 0.65f, 0), Hlsl.SmoothStep(-0.5f, 0.5f, sf));
|
||||
Float4 col2 = Hlsl.Lerp(new Float4(0.4f, 0.7f, 1, 0), new Float4(0.3f, 0.85f, 0.95f, 0), Hlsl.SmoothStep(-0.5f, 0.5f, sf2) * 0.5f);
|
||||
|
||||
col1 = Hlsl.Pow(col1, 1.6f);
|
||||
col2 = Hlsl.Lerp(col1.ZYXW, Hlsl.Pow(col2, 1.4f), 0.666f);
|
||||
|
||||
Float4 oCol;
|
||||
|
||||
if (gObjID == 0)
|
||||
{
|
||||
oCol = Hlsl.Lerp(col2, 0, (1.0f - Hlsl.SmoothStep(0, 0.01f, pat2)) * 0.35f);
|
||||
oCol = Hlsl.Lerp(oCol, 0, (1.0f - Hlsl.SmoothStep(0, 0.01f, bord)) * 0.8f);
|
||||
|
||||
if (Mod(iuv.X + iuv.Y, 2.0f) > 0.5f) oCol *= 0.8f;
|
||||
|
||||
oCol = Hlsl.Lerp(oCol, 0, (1.0f - Hlsl.SmoothStep(0, 0.01f, d - 0.015f)) * 0.8f);
|
||||
}
|
||||
else if (gObjID == 1)
|
||||
{
|
||||
oCol = Hlsl.Lerp(1, 0, 1.0f - Hlsl.SmoothStep(0, 0.01f, d + 0.05f));
|
||||
Float4 fCol = Hlsl.Lerp(col1, 0, (1.0f - Hlsl.SmoothStep(0, 0.01f, pat)) * 0.35f);
|
||||
fCol = Hlsl.Lerp(fCol, 0, (1.0f - Hlsl.SmoothStep(0, 0.01f, bord)) * 0.8f);
|
||||
|
||||
if (Mod(iuv.X + iuv.Y, 2.0f) < 0.5f) fCol *= 0.8f;
|
||||
|
||||
oCol = Hlsl.Lerp(oCol, fCol, 1 - Hlsl.SmoothStep(0, 0.01f, d + 0.08f));
|
||||
oCol = Hlsl.Lerp(oCol, 0, (1 - Hlsl.SmoothStep(0, 0.01f, Hlsl.Length(uv2) - 0.08f)) * 0.8f);
|
||||
}
|
||||
else
|
||||
{
|
||||
oCol = col1;
|
||||
|
||||
float ht = 0.26f;
|
||||
|
||||
if (Mod(iuv2.X + iuv2.Y + 1.0f, 2.0f) > 0.5f)
|
||||
{
|
||||
float tmp = pat; pat = pat2; pat2 = tmp;
|
||||
|
||||
oCol = col2;
|
||||
ht = 0.05f;
|
||||
}
|
||||
|
||||
float mark = Hlsl.Length(uv2);
|
||||
float markRim = Hlsl.Max(Hlsl.Abs(mark - 0.07f), Hlsl.Abs(p.Z + ht)) - 0.003f;
|
||||
|
||||
oCol = Hlsl.Lerp(oCol, 0, (1 - Hlsl.SmoothStep(0, 0.01f, markRim)) * 0.8f);
|
||||
oCol = Hlsl.Lerp(oCol, 0, (1 - Hlsl.SmoothStep(0, 0.01f, mark - 0.015f)) * 0.8f);
|
||||
}
|
||||
|
||||
Float3 ld = l - p;
|
||||
float lDist = Hlsl.Length(ld);
|
||||
|
||||
ld /= lDist;
|
||||
|
||||
float at = 1.0f / (1.0f + lDist * lDist * 0.125f);
|
||||
float sh = SoftShadow(p, l, n, 8);
|
||||
float ao = CalculateAO(p, n);
|
||||
float df = Hlsl.Max(Hlsl.Dot(n, ld), 0);
|
||||
float sp = Hlsl.Pow(Hlsl.Max(Hlsl.Dot(Hlsl.Reflect(r, n), ld), 0), 32);
|
||||
|
||||
Float4 c = oCol * (df * sh + sp * sh + 0.5f) * at * ao;
|
||||
|
||||
c = Hlsl.Sqrt(Hlsl.Max(c, 0));
|
||||
|
||||
texture[ThreadIds.XY] = c;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
namespace ComputeSharp.SwapChain.Shaders.Compute
|
||||
{
|
||||
/// <summary>
|
||||
/// A shader creating a fractal tiling animation.
|
||||
/// Ported from <see href="https://www.shadertoy.com/view/Ml2GWy"/>.
|
||||
/// <para>Created by Inigo Quilez.</para>
|
||||
/// <para>License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.</para>
|
||||
/// </summary>
|
||||
[AutoConstructor]
|
||||
internal readonly partial struct FractalTiling : 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;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Execute()
|
||||
{
|
||||
Float2 position = ((Float2)(256 * ThreadIds.XY)) / DispatchSize.X + time;
|
||||
Float4 color = 0;
|
||||
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
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 * time) +
|
||||
new Float4(0.035f, 0.01f, 0, 0.7f))
|
||||
* 13.545317f);
|
||||
|
||||
color.XYZ += w.XYZ *
|
||||
2.0f * Hlsl.SmoothStep(0.45f, 0.55f, w.W) *
|
||||
Hlsl.Sqrt(16.0f * b.X * b.Y * (1.0f - b.X) * (1.0f - b.Y));
|
||||
|
||||
position /= 2.0f;
|
||||
color /= 2.0f;
|
||||
}
|
||||
|
||||
color.XYZ = Hlsl.Pow(color.XYZ, new Float3(0.7f, 0.8f, 0.5f));
|
||||
color.W = 1.0f;
|
||||
|
||||
texture[ThreadIds.XY] = color;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
namespace ComputeSharp.SwapChain.Shaders.Compute
|
||||
{
|
||||
/// <summary>
|
||||
/// A shader creating a flythrough in a Menger system.
|
||||
/// Ported from <see href="https://www.shadertoy.com/view/Mdf3z7"/>.
|
||||
/// <para>Created by Syntopia.</para>
|
||||
/// </summary>
|
||||
[AutoConstructor]
|
||||
internal readonly partial struct MengerJourney : 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;
|
||||
|
||||
private const int MaxSteps = 30;
|
||||
private const float MinimumDistance = 0.0009f;
|
||||
private const float NormalDistance = 0.0002f;
|
||||
private const int Iterations = 7;
|
||||
private const float Scale = 3.0f;
|
||||
private const float FieldOfView = 1.0f;
|
||||
private const float Jitter = 0.05f;
|
||||
private const float FudgeFactor = 0.7f;
|
||||
private const float NonLinearPerspective = 2.0f;
|
||||
private const float Ambient = 0.32184f;
|
||||
private const float Diffuse = 0.5f;
|
||||
|
||||
private static readonly Float3 LightDir = 1.0f;
|
||||
private static readonly Float3 LightColor = new(1.0f, 1.0f, 0.858824f);
|
||||
private static readonly Float3 LightDir2 = new(1.0f, -1.0f, 1.0f);
|
||||
private static readonly Float3 LightColor2 = new(0, 0.333333f, 1.0f);
|
||||
private static readonly Float3 Offset = new(0.92858f, 0.92858f, 0.32858f);
|
||||
|
||||
/// <summary>
|
||||
/// Rotates a given <see cref="Float2"/> value by a specified amount.
|
||||
/// </summary>
|
||||
private static Float2 Rotate(Float2 v, float a)
|
||||
{
|
||||
return new(Hlsl.Cos(a) * v.X + Hlsl.Sin(a) * v.Y, -Hlsl.Sin(a) * v.X + Hlsl.Cos(a) * v.Y);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the light at a specified normal and starting color.
|
||||
/// Uses two light sources and no specular lights.
|
||||
/// </summary>
|
||||
private static Float3 GetLight(in Float3 color, in Float3 normal)
|
||||
{
|
||||
Float3 lightDir = Hlsl.Normalize(LightDir);
|
||||
float diffuse = Hlsl.Max(0.0f, Hlsl.Dot(-normal, lightDir));
|
||||
|
||||
Float3 lightDir2 = Hlsl.Normalize(LightDir2);
|
||||
float diffuse2 = Hlsl.Max(0.0f, Hlsl.Dot(-normal, lightDir2));
|
||||
|
||||
return
|
||||
(diffuse * Diffuse) * (LightColor * color) +
|
||||
(diffuse2 * Diffuse) * (LightColor2 * color);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// DE: Infinitely tiled Menger IFS.
|
||||
/// For more info on KIFS, see <a href="http://www.fractalforums.com/3d-fractal-generation/kaleidoscopic-%28escape-time-ifs%29/"/>
|
||||
/// </summary>
|
||||
private float DE(Float3 z)
|
||||
{
|
||||
// Performs the GLSL mod function.
|
||||
static Float3 Mod(Float3 x, float y)
|
||||
{
|
||||
return x - y * Hlsl.Floor(x / y);
|
||||
}
|
||||
|
||||
z = Hlsl.Abs(1.0f - Mod(z, 2.0f));
|
||||
|
||||
float d = 1000.0f;
|
||||
|
||||
for (int n = 0; n < Iterations; n++)
|
||||
{
|
||||
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; }
|
||||
if (z.X < z.Z) { z.XZ = z.ZX; }
|
||||
if (z.Y < z.Z) { z.YZ = z.ZY; }
|
||||
|
||||
z = Scale * z - Offset * (Scale - 1.0f);
|
||||
|
||||
if (z.Z < -0.5f * Offset.Z * (Scale - 1.0))
|
||||
{
|
||||
z.Z += Offset.Z * (Scale - 1.0f);
|
||||
}
|
||||
|
||||
d = Hlsl.Min(d, Hlsl.Length(z) * Hlsl.Pow(Scale, -n - 1.0f));
|
||||
}
|
||||
|
||||
return d - 0.001f;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the finite difference normal at a given position.
|
||||
/// </summary>
|
||||
private Float3 getNormal(in Float3 pos)
|
||||
{
|
||||
Float3 e = new(0.0f, NormalDistance, 0.0f);
|
||||
|
||||
return Hlsl.Normalize(new Float3(
|
||||
DE(pos + e.YXX) - DE(pos - e.YXX),
|
||||
DE(pos + e.XYX) - DE(pos - e.XYX),
|
||||
DE(pos + e.XXY) - DE(pos - e.XXY)));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Performs a ray march operation on a specific position.
|
||||
/// </summary>
|
||||
private Float4 RayMarch(in Float3 from, Float3 dir, in Float2 fragCoord)
|
||||
{
|
||||
// Computes a pseudo-random number
|
||||
static float Rand(Float2 co)
|
||||
{
|
||||
return Hlsl.Frac(Hlsl.Cos(Hlsl.Dot(co, new Float2(4.898f, 7.23f))) * 23421.631f);
|
||||
}
|
||||
|
||||
float totalDistance = Jitter * Rand(fragCoord.XY + time);
|
||||
Float3 dir2 = dir;
|
||||
float distance = 0;
|
||||
int steps = 0;
|
||||
Float3 pos = 0;
|
||||
|
||||
for (int i = 0; i < MaxSteps; i++)
|
||||
{
|
||||
dir.ZY = Rotate(dir2.ZY, totalDistance * Hlsl.Cos(time / 4.0f) * NonLinearPerspective);
|
||||
pos = from + totalDistance * dir;
|
||||
distance = DE(pos) * FudgeFactor;
|
||||
totalDistance += distance;
|
||||
|
||||
if (distance < MinimumDistance) break;
|
||||
|
||||
steps = i;
|
||||
}
|
||||
|
||||
float smoothStep = steps + distance / MinimumDistance;
|
||||
float ao = 1.1f - smoothStep / MaxSteps;
|
||||
Float3 normal = getNormal(pos - dir * NormalDistance * 3.0f);
|
||||
Float3 color = 1.0f;
|
||||
Float3 light = GetLight(color, normal);
|
||||
|
||||
color = (color * Ambient + light) * ao;
|
||||
|
||||
return new(color.X, color.Y, color.Z, 1.0f);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Execute()
|
||||
{
|
||||
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)ThreadIds.XY / DispatchSize.XY;
|
||||
|
||||
coord.X *= (float)DispatchSize.X / DispatchSize.Y;
|
||||
|
||||
Float3 rayDir = Hlsl.Normalize(camDir + (coord.X * camRight + coord.Y * camUp) * FieldOfView);
|
||||
|
||||
texture[ThreadIds.XY] = RayMarch(camPos, rayDir, ThreadIds.XY);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
namespace ComputeSharp.SwapChain.Shaders.Compute
|
||||
{
|
||||
/// <summary>
|
||||
/// A shader creating an octagram animation, inspired by arabesque.
|
||||
/// Ported from <see href="https://www.shadertoy.com/view/tlVGDt"/>.
|
||||
/// <para>Created by whisky_shusuky.</para>
|
||||
/// </summary>
|
||||
[AutoConstructor]
|
||||
internal readonly partial struct Octagrams : 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;
|
||||
|
||||
private static Float2x2 Rotate(float a)
|
||||
{
|
||||
float c = Hlsl.Cos(a), s = Hlsl.Sin(a);
|
||||
|
||||
return new(c, s, -s, c);
|
||||
}
|
||||
|
||||
private static float Box(Float3 pos, float scale)
|
||||
{
|
||||
static float SDBox(Float3 p, Float3 b)
|
||||
{
|
||||
Float3 q = Hlsl.Abs(p) - b;
|
||||
|
||||
return Hlsl.Length(Hlsl.Max(q, 0.0f)) +
|
||||
Hlsl.Min(Hlsl.Max(q.X, Hlsl.Max(q.Y, q.Z)), 0.0f);
|
||||
}
|
||||
|
||||
pos *= scale;
|
||||
|
||||
float b = SDBox(pos, new(0.4f, 0.4f, 0.1f)) / 1.5f;
|
||||
|
||||
pos.XY *= 5.0f;
|
||||
pos.Y -= 3.5f;
|
||||
pos.XY = Hlsl.Mul(pos.XY, Rotate(0.75f));
|
||||
|
||||
return -b;
|
||||
}
|
||||
|
||||
private static float BoxSet(Float3 pos, float gTime)
|
||||
{
|
||||
Float3 pos_origin = pos;
|
||||
|
||||
pos = pos_origin;
|
||||
pos.Y += Hlsl.Sin(gTime * 0.4f) * 2.5f;
|
||||
pos.XY = Hlsl.Mul(pos.XY, Rotate(0.8f));
|
||||
|
||||
float box1 = Box(pos, 2.0f - Hlsl.Abs(Hlsl.Sin(gTime * 0.4f)) * 1.5f);
|
||||
|
||||
pos = pos_origin;
|
||||
pos.Y -= Hlsl.Sin(gTime * 0.4f) * 2.5f;
|
||||
pos.XY = Hlsl.Mul(pos.XY, Rotate(0.8f));
|
||||
|
||||
float box2 = Box(pos, 2.0f - Hlsl.Abs(Hlsl.Sin(gTime * 0.4f)) * 1.5f);
|
||||
|
||||
pos = pos_origin;
|
||||
pos.X += Hlsl.Sin(gTime * 0.4f) * 2.5f;
|
||||
pos.XY = Hlsl.Mul(pos.XY, Rotate(0.8f));
|
||||
|
||||
float box3 = Box(pos, 2.0f - Hlsl.Abs(Hlsl.Sin(gTime * 0.4f)) * 1.5f);
|
||||
|
||||
pos = pos_origin;
|
||||
pos.X -= Hlsl.Sin(gTime * 0.4f) * 2.5f;
|
||||
pos.XY = Hlsl.Mul(pos.XY, Rotate(0.8f));
|
||||
|
||||
float box4 = Box(pos, 2.0f - Hlsl.Abs(Hlsl.Sin(gTime * 0.4f)) * 1.5f);
|
||||
|
||||
pos = pos_origin;
|
||||
pos.XY = Hlsl.Mul(pos.XY, Rotate(0.8f));
|
||||
|
||||
float box5 = Box(pos, 0.5f) * 6.0f;
|
||||
|
||||
pos = pos_origin;
|
||||
|
||||
float box6 = Box(pos, 0.5f) * 6.0f;
|
||||
float result = Hlsl.Max(Hlsl.Max(Hlsl.Max(Hlsl.Max(Hlsl.Max(box1, box2), box3), box4), box5), box6);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Execute()
|
||||
{
|
||||
Float2 p = ((Float2)ThreadIds.XY * 2.0f - DispatchSize.XY) / 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(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;
|
||||
|
||||
for (int i = 0; i < 99; i++)
|
||||
{
|
||||
static Float3 Mod(Float3 x, float y)
|
||||
{
|
||||
return x - y * Hlsl.Floor(x / y);
|
||||
}
|
||||
|
||||
Float3 pos = ro + ray * t;
|
||||
|
||||
pos = Mod(pos - 2.0f, 4.0f) - 2.0f;
|
||||
|
||||
float gTime = time - i * 0.01f;
|
||||
float d = BoxSet(pos, gTime);
|
||||
|
||||
d = Hlsl.Max(Hlsl.Abs(d), 0.01f);
|
||||
ac += Hlsl.Exp(-d * 23.0f);
|
||||
t += d * 0.55f;
|
||||
}
|
||||
|
||||
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.0f - t * (0.02f + 0.02f * Hlsl.Sin(time)));
|
||||
|
||||
texture[ThreadIds.XY] = color;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
namespace ComputeSharp.SwapChain.Shaders.Compute
|
||||
{
|
||||
/// <summary>
|
||||
/// Fully procedural 3D animated volume with three evaluations per step (for shading).
|
||||
/// Ported from <see href="https://www.shadertoy.com/view/3l23Rh"/>.
|
||||
/// <para>Created by nimitz (twitter: @stormoid).</para>
|
||||
/// <para>License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.</para>
|
||||
/// </summary>
|
||||
[AutoConstructor]
|
||||
internal readonly partial struct ProteanClouds : IComputeShader
|
||||
{
|
||||
/// <summary>
|
||||
/// The target texture.
|
||||
/// </summary>
|
||||
public readonly IReadWriteTexture2D<Float4> texture;
|
||||
|
||||
/// <summary>
|
||||
/// The current time Hlsl.Since the start of the application.
|
||||
/// </summary>
|
||||
public readonly float time;
|
||||
|
||||
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)
|
||||
{
|
||||
float c = Hlsl.Cos(a), s = Hlsl.Sin(a);
|
||||
|
||||
return new(c, s, -s, c);
|
||||
}
|
||||
|
||||
private static float LinStep(in float mn, in float mx, in float x)
|
||||
{
|
||||
return Hlsl.Clamp((x - mn) / (mx - mn), 0.0f, 1.0f);
|
||||
}
|
||||
|
||||
private static Float2 Disp(float t)
|
||||
{
|
||||
return new Float2(Hlsl.Sin(t * 0.22f) * 1.0f, Hlsl.Cos(t * 0.175f) * 1.0f) * 2.0f;
|
||||
}
|
||||
|
||||
private Float2 Map(Float3 p, float prm1, Float2 bsMo)
|
||||
{
|
||||
Float3 p2 = p;
|
||||
|
||||
p2.XY -= Disp(p.Z).XY;
|
||||
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;
|
||||
|
||||
p *= 0.61f;
|
||||
|
||||
float z = 1.0f;
|
||||
float trk = 1.0f;
|
||||
float dspAmp = 0.1f + prm1 * 0.2f;
|
||||
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
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;
|
||||
|
||||
p = Hlsl.Mul(p, m3);
|
||||
}
|
||||
|
||||
d = Hlsl.Abs(d + prm1 * 3.0f) + prm1 * 0.3f - 2.5f + bsMo.Y;
|
||||
|
||||
return new Float2(d + cl * 0.2f + 0.25f, cl);
|
||||
}
|
||||
|
||||
private Float4 Render(Float3 ro, Float3 rd, float time, float prm1, Float2 bsMo)
|
||||
{
|
||||
Float4 rez = 0;
|
||||
float t = 1.5f;
|
||||
float fogT = 0;
|
||||
|
||||
for (int i = 0; i < 130; i++)
|
||||
{
|
||||
if (rez.A > 0.99f) break;
|
||||
|
||||
Float3 pos = ro + t * rd;
|
||||
Float2 mpv = Map(pos, prm1, bsMo);
|
||||
float den = Hlsl.Clamp(mpv.X - 0.3f, 0.0f, 1.0f) * 1.12f;
|
||||
float dn = Hlsl.Clamp((mpv.X + 2.0f), 0.0f, 3.0f);
|
||||
Float4 col = 0f;
|
||||
|
||||
if (mpv.X > 0.6f)
|
||||
{
|
||||
|
||||
col = new Float4(Hlsl.Sin(new Float3(5.0f, 0.4f, 0.2f) + mpv.Y * 0.1f + Hlsl.Sin(pos.Z * 0.4f) * 0.5f + 1.8f) * 0.5f + 0.5f, 0.08f);
|
||||
col *= den * den * den;
|
||||
col.RGB *= LinStep(4.0f, -2.5f, mpv.X) * 2.3f;
|
||||
|
||||
float dif = Hlsl.Clamp((den - Map(pos + 0.8f, prm1, bsMo).X) / 9.0f, 0.001f, 1.0f);
|
||||
|
||||
dif += Hlsl.Clamp((den - Map(pos + 0.35f, prm1, bsMo).X) / 2.5f, 0.001f, 1.0f);
|
||||
col.XYZ *= den * (new Float3(0.005f, 0.045f, 0.075f) + 1.5f * new Float3(0.033f, 0.07f, 0.03f) * dif);
|
||||
}
|
||||
|
||||
float fogC = Hlsl.Exp(t * 0.2f - 2.2f);
|
||||
|
||||
col += new Float4(0.06f, 0.11f, 0.11f, 0.1f) * Hlsl.Clamp(fogC - fogT, 0.0f, 1.0f);
|
||||
fogT = fogC;
|
||||
rez += col * (1.0f - rez.A);
|
||||
t += Hlsl.Clamp(0.5f - dn * dn * 0.05f, 0.09f, 0.3f);
|
||||
}
|
||||
|
||||
return Hlsl.Clamp(rez, 0.0f, 1.0f);
|
||||
}
|
||||
|
||||
private static float GetSaturation(Float3 c)
|
||||
{
|
||||
float mi = Hlsl.Min(Hlsl.Min(c.X, c.Y), c.Z);
|
||||
float ma = Hlsl.Max(Hlsl.Max(c.X, c.Y), c.Z);
|
||||
|
||||
return (ma - mi) / (ma + (float)1e-7);
|
||||
}
|
||||
|
||||
private static Float3 ILerp(in Float3 a, in Float3 b, in float x)
|
||||
{
|
||||
Float3 ic = Hlsl.Lerp(a, b, x) + new Float3((float)1e-6, 0.0f, 0.0f);
|
||||
float sd = Hlsl.Abs(GetSaturation(ic) - Hlsl.Lerp(GetSaturation(a), GetSaturation(b), x));
|
||||
Float3 dir = Hlsl.Normalize(new Float3(2.0f * ic.X - ic.Y - ic.Z, 2.0f * ic.Y - ic.X - ic.Z, 2.0f * ic.Z - ic.Y - ic.X));
|
||||
float lgt = Hlsl.Dot(1.0f, ic);
|
||||
float ff = Hlsl.Dot(dir, Hlsl.Normalize(ic));
|
||||
|
||||
ic += 1.5f * dir * sd * ff * lgt;
|
||||
|
||||
return Hlsl.Clamp(ic, 0.0f, 1.0f);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Execute()
|
||||
{
|
||||
Float2 q = (Float2)ThreadIds.XY / DispatchSize.XY;
|
||||
Float2 p = (ThreadIds.XY - 0.5f * (Float2)DispatchSize.XY) / DispatchSize.Y;
|
||||
Float2 bsMo = -0.5f * (Float2)DispatchSize.XY / DispatchSize.Y;
|
||||
float scaledTime = time * 3.0f;
|
||||
Float3 ro = new(0, 0, scaledTime);
|
||||
|
||||
ro += new Float3(Hlsl.Sin(time) * 0.5f, Hlsl.Sin(time * 1.0f) * 0.0f, 0);
|
||||
|
||||
float dspAmp = 0.85f;
|
||||
|
||||
ro.XY += Disp(ro.Z) * dspAmp;
|
||||
|
||||
float tgtDst = 3.5f;
|
||||
Float3 target = Hlsl.Normalize(ro - new Float3(Disp(scaledTime + tgtDst) * dspAmp, scaledTime + tgtDst));
|
||||
|
||||
ro.X -= bsMo.X * 2.0f;
|
||||
|
||||
Float3 rightdir = Hlsl.Normalize(Hlsl.Cross(target, new Float3(0, 1, 0)));
|
||||
Float3 updir = Hlsl.Normalize(Hlsl.Cross(rightdir, target));
|
||||
|
||||
rightdir = Hlsl.Normalize(Hlsl.Cross(updir, target));
|
||||
|
||||
Float3 rd = Hlsl.Normalize((p.X * rightdir + p.Y * updir) * 1.0f - target);
|
||||
|
||||
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(time * 0.3f));
|
||||
Float4 scn = Render(ro, rd, scaledTime, prm1, bsMo);
|
||||
Float3 col = scn.RGB;
|
||||
|
||||
col = ILerp(col.BGR, col.RGB, Hlsl.Clamp(1.0f - prm1, 0.05f, 1.0f));
|
||||
col = Hlsl.Pow(col, new Float3(0.55f, 0.65f, 0.6f)) * new Float3(1.0f, 0.97f, 0.9f);
|
||||
col *= Hlsl.Pow(16.0f * q.X * q.Y * (1.0f - q.X) * (1.0f - q.Y), 0.12f) * 0.7f + 0.3f;
|
||||
|
||||
texture[ThreadIds.XY] = new Float4(col, 1.0f);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
namespace ComputeSharp.SwapChain.Shaders.Compute
|
||||
{
|
||||
/// <summary>
|
||||
/// An offset grid of square-based pyramids whose tips have been offset according to an underlying directional noise field.
|
||||
/// The faux lighting is provided via bump mapping. No raymarching was harmed during the making of this example.
|
||||
/// Ported from <see href="https://www.shadertoy.com/view/wlscWX"/>.
|
||||
/// <para>Created by Shane.</para>
|
||||
/// </summary>
|
||||
[AutoConstructor]
|
||||
internal readonly partial struct PyramidPattern : IComputeShader
|
||||
{
|
||||
/// <summary>
|
||||
/// The target texture.
|
||||
/// </summary>
|
||||
public readonly IReadWriteTexture2D<Float4> texture;
|
||||
|
||||
/// <summary>
|
||||
/// The current time Hlsl.Since the start of the application.
|
||||
/// </summary>
|
||||
public readonly float time;
|
||||
|
||||
// Standard 2D rotation formula.
|
||||
private static Float2x2 Rotate2x2(in float a)
|
||||
{
|
||||
float c = Hlsl.Cos(a), s = Hlsl.Sin(a);
|
||||
|
||||
return new(c, -s, s, c);
|
||||
}
|
||||
|
||||
// IQ's Float2 to float hash.
|
||||
private static float Hash21(Float2 p)
|
||||
{
|
||||
return Hlsl.Frac(Hlsl.Sin(Hlsl.Dot(p, new Float2(27.619f, 57.583f))) * 43758.5453f);
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
// Based on IQ's gradient noise formula.
|
||||
private static float N2D3G(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);
|
||||
}
|
||||
|
||||
// Two layers of noise.
|
||||
private static float FBM(Float2 p)
|
||||
{
|
||||
return N2D3G(p) * 0.66f + N2D3G(p * 2.0f) * 0.34f;
|
||||
}
|
||||
|
||||
// HLSL mod from GLSL
|
||||
private static float Mod(float x, float y)
|
||||
{
|
||||
return x - y * Hlsl.Floor(x / y);
|
||||
}
|
||||
|
||||
private float BMap(Float2 p)
|
||||
{
|
||||
p = Hlsl.Mul(p, Rotate2x2(-3.14159f / 5.0f));
|
||||
|
||||
if (Mod(Hlsl.Floor(p.Y), 2.0f) < 0.5f)
|
||||
{
|
||||
p.X += 0.5f;
|
||||
}
|
||||
|
||||
Float2 ip = Hlsl.Floor(p);
|
||||
|
||||
p -= ip + 0.5f;
|
||||
|
||||
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;
|
||||
|
||||
if (p.X < offs.X) p.X = 1.0f - (p.X + 0.5f) / Hlsl.Abs(offs.X + 0.5f);
|
||||
else p.X = (p.X - offs.X) / (0.5f - offs.X);
|
||||
|
||||
if (p.Y < offs.Y) p.Y = 1.0f - (p.Y + 0.5f) / Hlsl.Abs(offs.Y + 0.5f);
|
||||
else p.Y = (p.Y - offs.Y) / (0.5f - offs.Y);
|
||||
|
||||
return 1.0f - Hlsl.Max(p.X, p.Y);
|
||||
}
|
||||
|
||||
|
||||
// Standard function-based bump mapping function, with an edge value included for good measure
|
||||
private Float3 DoBumpMap(in Float2 p, in Float3 n, float bumpfactor, ref float edge)
|
||||
{
|
||||
Float2 e = new(0.025f, 0);
|
||||
|
||||
float f = BMap(p);
|
||||
float fx = BMap(p - e.XY);
|
||||
float fy = BMap(p - e.YX);
|
||||
float fx2 = BMap(p + e.XY);
|
||||
float fy2 = BMap(p + e.YX);
|
||||
|
||||
Float3 grad = (new Float3(fx - fx2, fy - fx2, 0)) / e.X / 2.0f;
|
||||
|
||||
edge = Hlsl.Length(new Float2(fx, fy) + new Float2(fx2, fy2) - f * 2.0f);
|
||||
edge = Hlsl.SmoothStep(0.0f, 1.0f, edge / e.X);
|
||||
|
||||
grad -= n * Hlsl.Dot(n, grad);
|
||||
|
||||
return Hlsl.Normalize(n + grad * bumpfactor);
|
||||
}
|
||||
|
||||
|
||||
// A hatch-like algorithm, or a stipple... or some kind of textured pattern
|
||||
private float DoHatch(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;
|
||||
|
||||
return hatch;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Execute()
|
||||
{
|
||||
Int2 coordinate = new(ThreadIds.X, DispatchSize.Y - ThreadIds.Y);
|
||||
float iRes = Hlsl.Min(DispatchSize.Y, 800.0f);
|
||||
Float2 uv = (coordinate - (Float2)DispatchSize.XY * 0.5f) / iRes;
|
||||
Float3 rd = Hlsl.Normalize(new Float3(uv, 0.5f));
|
||||
const float gSc = 10.0f;
|
||||
Float2 p = uv * gSc + new Float2(0, time / 2.0f);
|
||||
Float2 oP = p;
|
||||
float m = BMap(p);
|
||||
Float3 n = new(0, 0, -1);
|
||||
float
|
||||
edge = 0.0f,
|
||||
bumpFactor = 0.25f;
|
||||
|
||||
n = DoBumpMap(p, n, bumpFactor, ref edge);
|
||||
|
||||
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 = Hlsl.Pow(diff, 4.0f);
|
||||
|
||||
float spec = Hlsl.Pow(Hlsl.Max(Hlsl.Dot(Hlsl.Reflect(-ld, n), -rd), 0.0f), 16.0f);
|
||||
float fre = Hlsl.Min(Hlsl.Pow(Hlsl.Max(1.0f + Hlsl.Dot(n, rd), 0.0f), 4.0f), 3.0f);
|
||||
Float3 col = 0.15f * (diff + 0.251f + spec * new Float3(1, 0.7f, 0.3f) * 9.0f + fre * new Float3(0.1f, 0.3f, 1) * 12.0f);
|
||||
float rf = Hlsl.SmoothStep(0.0f, 0.35f, BMap(Hlsl.Reflect(rd, n).XY * 2.0f) * FBM(Hlsl.Reflect(rd, n).XY * 3.0f) + 0.1f);
|
||||
|
||||
col += col * col * rf * rf * new Float3(1, 0.1f, 0.1f) * 15.0f;
|
||||
|
||||
float shade = m * 0.83f + 0.17f;
|
||||
|
||||
col *= shade;
|
||||
col *= 1.0f - edge * 0.8f;
|
||||
|
||||
float hatch = DoHatch(oP / gSc, iRes);
|
||||
|
||||
col *= hatch * 0.5f + 0.7f;
|
||||
|
||||
Float4 color = new(Hlsl.Sqrt(Hlsl.Max(col, 0.0f)), 1);
|
||||
|
||||
texture[ThreadIds.XY] = color;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,286 @@
|
||||
namespace ComputeSharp.SwapChain.Shaders.Compute
|
||||
{
|
||||
/// <summary>
|
||||
/// Utilizing a 2D simplex grid to construct the isolines of a noise function.
|
||||
/// Ported from <see href="https://www.shadertoy.com/view/WtfGDX"/>.
|
||||
/// <para>Created by Shane.</para>
|
||||
/// </summary>
|
||||
[AutoConstructor]
|
||||
internal readonly partial struct TriangleGridContouring : IComputeShader
|
||||
{
|
||||
/// <summary>
|
||||
/// The target texture.
|
||||
/// </summary>
|
||||
public readonly IReadWriteTexture2D<Float4> texture;
|
||||
|
||||
/// <summary>
|
||||
/// The current time Hlsl.Since the start of the application.
|
||||
/// </summary>
|
||||
public readonly float time;
|
||||
|
||||
// Standard 2D rotation formula.
|
||||
private static Float2x2 Rotate2x2(in float a)
|
||||
{
|
||||
float c = Hlsl.Cos(a), s = Hlsl.Sin(a);
|
||||
|
||||
return new(c, -s, s, c);
|
||||
}
|
||||
|
||||
// IQ's Float2 to float hash.
|
||||
private static float Hash21(Float2 p)
|
||||
{
|
||||
return Hlsl.Frac(Hlsl.Sin(Hlsl.Dot(p, new Float2(27.619f, 57.583f))) * 43758.5453f);
|
||||
}
|
||||
|
||||
// Float2 to Float2 hash.
|
||||
private Float2 Hash22(Float2 p)
|
||||
{
|
||||
float n = Hlsl.Sin(Hlsl.Dot(p, new Float2(41, 289)));
|
||||
|
||||
p = Hlsl.Frac(new Float2(262144, 32768) * n);
|
||||
|
||||
return Hlsl.Sin(p * 6.2831853f + time);
|
||||
}
|
||||
|
||||
// Based on IQ's gradient noise formula.
|
||||
private float N2D3G(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);
|
||||
}
|
||||
|
||||
|
||||
// The isofunction. Just a Hlsl.Single noise function, but it can be more elaborate.
|
||||
private float IsoFunction(in Float2 p)
|
||||
{
|
||||
return N2D3G(p / 4.0f + 0.07f);
|
||||
}
|
||||
|
||||
// Unsigned distance to the segment joining "a" and "b".
|
||||
private static float DistLine(Float2 a, Float2 b)
|
||||
{
|
||||
b = a - b;
|
||||
|
||||
float h = Hlsl.Clamp(Hlsl.Dot(a, b) / Hlsl.Dot(b, b), 0.0f, 1.0f);
|
||||
|
||||
return Hlsl.Length(a - b * h);
|
||||
}
|
||||
|
||||
// Based on IQ's signed distance to the segment joining "a" and "b".
|
||||
private static float distEdge(Float2 a, Float2 b)
|
||||
{
|
||||
return Hlsl.Dot((a + b) * 0.5f, Hlsl.Normalize((b - a).YX * new Float2(-1, 1)));
|
||||
}
|
||||
|
||||
// Interpolating along the edge connecting vertices v1 and v2 with respect to the isovalue.
|
||||
private static Float2 Interpolate(in Float2 p1, in Float2 p2, float v1, float v2, float isovalue)
|
||||
{
|
||||
return Hlsl.Lerp(p1, p2, (isovalue - v1) / (v2 - v1) * 0.75f + 0.25f / 2.0f);
|
||||
}
|
||||
|
||||
// Isoline function.
|
||||
private static int IsoLine(
|
||||
Float3 n3,
|
||||
Float2 ip0,
|
||||
Float2 ip1,
|
||||
Float2 ip2,
|
||||
float isovalue,
|
||||
float i,
|
||||
ref Float2 p0,
|
||||
ref Float2 p1)
|
||||
{
|
||||
p0 = 1e5f;
|
||||
p1 = 1e5f;
|
||||
|
||||
int iTh = 0;
|
||||
|
||||
if (n3.X > isovalue) iTh += 4;
|
||||
if (n3.Y > isovalue) iTh += 2;
|
||||
if (n3.Z > isovalue) iTh += 1;
|
||||
|
||||
if (iTh == 1 || iTh == 6)
|
||||
{
|
||||
p0 = Interpolate(ip1, ip2, n3.Y, n3.Z, isovalue);
|
||||
p1 = Interpolate(ip2, ip0, n3.Z, n3.X, isovalue);
|
||||
|
||||
}
|
||||
else if (iTh == 2 || iTh == 5)
|
||||
{
|
||||
p0 = Interpolate(ip0, ip1, n3.X, n3.Y, isovalue);
|
||||
p1 = Interpolate(ip1, ip2, n3.Y, n3.Z, isovalue);
|
||||
|
||||
}
|
||||
else if (iTh == 3 || iTh == 4)
|
||||
{
|
||||
p0 = Interpolate(ip0, ip1, n3.X, n3.Y, isovalue);
|
||||
p1 = Interpolate(ip2, ip0, n3.Z, n3.X, isovalue);
|
||||
}
|
||||
|
||||
if (iTh >= 4 && iTh <= 6)
|
||||
{
|
||||
Float2 tmp = p0;
|
||||
|
||||
p0 = p1;
|
||||
p1 = tmp;
|
||||
}
|
||||
|
||||
if (i == 0.0f)
|
||||
{
|
||||
Float2 tmp = p0;
|
||||
|
||||
p0 = p1;
|
||||
p1 = tmp;
|
||||
}
|
||||
|
||||
return iTh;
|
||||
}
|
||||
|
||||
private Float3 SimplexContour(Float2 p)
|
||||
{
|
||||
const float gSc = 8.0f;
|
||||
|
||||
p *= gSc;
|
||||
|
||||
Float2 oP = p;
|
||||
|
||||
p += new Float2(N2D3G(p * 3.5f), N2D3G(p * 3.5f + 7.3f)) * 0.015f;
|
||||
|
||||
Float2 s = Hlsl.Floor(p + (p.X + p.Y) * 0.36602540378f);
|
||||
|
||||
p -= s - (s.X + s.Y) * 0.211324865f;
|
||||
|
||||
float i = p.X < p.Y ? 1.0f : 0.0f;
|
||||
Float2
|
||||
ioffs = new(1.0f - i, i),
|
||||
ip0 = 0,
|
||||
ip1 = ioffs - 0.2113248654f,
|
||||
ip2 = 0.577350269f,
|
||||
ctr = (ip0 + ip1 + ip2) / 3.0f;
|
||||
|
||||
ip0 -= ctr;
|
||||
ip1 -= ctr;
|
||||
ip2 -= ctr;
|
||||
p -= ctr;
|
||||
|
||||
Float3 n3 = default;
|
||||
|
||||
n3.X = IsoFunction(s);
|
||||
n3.Y = IsoFunction(s + ioffs);
|
||||
n3.Z = IsoFunction(s + 1.0f);
|
||||
|
||||
float
|
||||
d = 1e5f,
|
||||
d2 = 1e5f,
|
||||
d3 = 1e5f,
|
||||
d4 = 1e5f,
|
||||
d5 = 1e5f;
|
||||
|
||||
float isovalue = 0;
|
||||
Float2 p0 = default, p1 = default;
|
||||
int iTh = IsoLine(n3, ip0, ip1, ip2, isovalue, i, ref p0, ref p1);
|
||||
|
||||
d = Hlsl.Min(d, distEdge(p - p0, p - p1));
|
||||
|
||||
if (iTh == 7)
|
||||
{
|
||||
d = 0;
|
||||
}
|
||||
|
||||
d3 = Hlsl.Min(d3, DistLine((p - p0), (p - p1)));
|
||||
d4 = Hlsl.Min(d4, Hlsl.Min(Hlsl.Length(p - p0), Hlsl.Length(p - p1)));
|
||||
|
||||
float tri = Hlsl.Min(Hlsl.Min(DistLine(p - ip0, p - ip1), DistLine(p - ip1, p - ip2)), DistLine(p - ip2, p - ip0));
|
||||
|
||||
d5 = Hlsl.Min(d5, tri);
|
||||
d5 = Hlsl.Min(d5, Hlsl.Length(p) - 0.02f);
|
||||
isovalue = -0.15f;
|
||||
|
||||
int iTh2 = IsoLine(n3, ip0, ip1, ip2, isovalue, i, ref p0, ref p1);
|
||||
|
||||
d2 = Hlsl.Min(d2, distEdge(p - p0, p - p1));
|
||||
|
||||
float oldD2 = d2;
|
||||
|
||||
if (iTh2 == 7) d2 = 0.0f;
|
||||
if (iTh == 7) d2 = 1e5f;
|
||||
|
||||
d2 = Hlsl.Max(d2, -d);
|
||||
d3 = Hlsl.Min(d3, DistLine((p - p0), (p - p1)));
|
||||
d4 = Hlsl.Min(d4, Hlsl.Min(Hlsl.Length(p - p0), Hlsl.Length(p - p1)));
|
||||
d4 -= 0.075f;
|
||||
d3 -= 0.0125f;
|
||||
d /= gSc;
|
||||
d2 /= gSc;
|
||||
d3 /= gSc;
|
||||
d4 /= gSc;
|
||||
d5 /= gSc;
|
||||
|
||||
Float3 col = new(1, 0.85f, 0.6f);
|
||||
float sf = 0.004f;
|
||||
|
||||
if (d > 0.0f && d2 > 0.0f) col = new Float3(1, 1.8f, 3) * 0.45f;
|
||||
if (d > 0.0f) col = Hlsl.Lerp(col, new Float3(1, 1.85f, 3) * 0.3f, (1.0f - Hlsl.SmoothStep(0.0f, sf, d2 - 0.012f)));
|
||||
|
||||
col = Hlsl.Lerp(col, new Float3(1.1f, 0.85f, 0.6f), (1.0f - Hlsl.SmoothStep(0.0f, sf, d2)));
|
||||
col = Hlsl.Lerp(col, new Float3(1.5f, 0.9f, 0.6f) * 0.6f, (1.0f - Hlsl.SmoothStep(0.0f, sf, d - 0.012f)));
|
||||
col = Hlsl.Lerp(col, new Float3(1, 0.8f, 0.6f) * new Float3(0.7f, 1.0f, 0.75f) * 0.95f, (1.0f - Hlsl.SmoothStep(0.0f, sf, d)));
|
||||
|
||||
if (d2 > 0.0f) col *= (Hlsl.Abs(Hlsl.Dot(n3, Float3.One)) * 1.25f + 1.25f) / 2.0f;
|
||||
else col *= Hlsl.Max(2.0f - (Hlsl.Dot(n3, Float3.One) + 1.45f) / 1.25f, 0.0f);
|
||||
|
||||
float pat = Hlsl.Abs(Hlsl.Frac(tri * 12.5f + 0.4f) - 0.5f) * 2.0f;
|
||||
|
||||
col *= pat * 0.425f + 0.75f;
|
||||
col = Hlsl.Lerp(col, Float3.Zero, (1.0f - Hlsl.SmoothStep(0.0f, sf, d5)) * 0.95f);
|
||||
col = Hlsl.Lerp(col, Float3.Zero, (1.0f - Hlsl.SmoothStep(0.0f, sf, d3)));
|
||||
col = Hlsl.Lerp(col, Float3.Zero, (1.0f - Hlsl.SmoothStep(0.0f, sf, d4)));
|
||||
col = Hlsl.Lerp(col, Float3.One, (1.0f - Hlsl.SmoothStep(0.0f, sf, d4 + 0.005f)));
|
||||
|
||||
Float2 q = oP * 1.5f;
|
||||
|
||||
col = Hlsl.Min(col, 1.0f);
|
||||
|
||||
float gr = Hlsl.Sqrt(Hlsl.Dot(col, new Float3(0.299f, 0.587f, 0.114f))) * 1.25f;
|
||||
float ns = (N2D3G(q * 4.0f * new Float2(1.0f / 3.0f, 3)) * 0.64f + N2D3G(q * 8.0f * new Float2(1.0f / 3.0f, 3)) * 0.34f) * 0.5f + 0.5f;
|
||||
|
||||
ns = gr - ns;
|
||||
q = Hlsl.Mul(q, Rotate2x2(3.14159f / 3.0f));
|
||||
|
||||
float ns2 = (N2D3G(q * 4.0f * new Float2(1.0f / 3.0f, 3)) * 0.64f + N2D3G(q * 8.0f * new Float2(1.0f / 3.0f, 3)) * 0.34f) * 0.5f + 0.5f;
|
||||
|
||||
ns2 = gr - ns2;
|
||||
ns = Hlsl.SmoothStep(0.0f, 1.0f, Hlsl.Min(ns, ns2));
|
||||
col = Hlsl.Lerp(col, col * (ns + 0.35f), 0.4f);
|
||||
|
||||
return col;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Execute()
|
||||
{
|
||||
Int2 coordinate = new(ThreadIds.X, DispatchSize.Y - ThreadIds.Y);
|
||||
Float2 uv = (coordinate - (Float2)DispatchSize.XY * 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)DispatchSize.XY;
|
||||
col *= Hlsl.Pow(16.0f * uv.X * uv.Y * (1.0f - uv.X) * (1.0f - uv.Y), 0.0625f) + 0.1f;
|
||||
|
||||
Float4 color = new(Hlsl.Sqrt(Hlsl.Max(col, 0.0f)), 1);
|
||||
|
||||
texture[ThreadIds.XY] = color;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
namespace ComputeSharp.SwapChain.Shaders.Compute
|
||||
{
|
||||
/// <summary>
|
||||
/// A shader creating an animated two-tiles Truchet arrangement.
|
||||
/// Ported from <see href="https://www.shadertoy.com/view/tsSfWK"/>.
|
||||
/// <para>Created by Shane.</para>
|
||||
/// </summary>
|
||||
[AutoConstructor]
|
||||
internal readonly partial struct TwoTiledTruchet : 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;
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the Truchet distance field.
|
||||
/// </summary>
|
||||
/// <param name="p">The input value to compute the distance field for.</param>
|
||||
/// <param name="ang">The resulting Truchet angle.</param>
|
||||
/// <returns>The distance field for the input value.</returns>
|
||||
private Float2 DistanceField(Float2 p, out Float2 ang)
|
||||
{
|
||||
// Fast hash for a pair of floats
|
||||
static float Hash21(Float2 p)
|
||||
{
|
||||
return Hlsl.Frac(Hlsl.Sin(Hlsl.Dot(p, new Float2(27.619f, 57.583f))) * 43758.5453f);
|
||||
}
|
||||
|
||||
// HLSL's port of the GLSL mod intrinsic
|
||||
static float Mod(float x, float y)
|
||||
{
|
||||
return x - y * Hlsl.Floor(x / y);
|
||||
}
|
||||
|
||||
Float2 ip2 = Hlsl.Floor(p / 2.0f);
|
||||
Float2 ip = Hlsl.Floor(p);
|
||||
float rnd = Hash21(ip);
|
||||
float rnd3 = Hash21(ip + 0.57f);
|
||||
Float2 d = 1e5f;
|
||||
|
||||
p -= ip + 0.5f;
|
||||
|
||||
if (Mod(ip2.X + ip2.Y, 2.0f) < 0.5f)
|
||||
{
|
||||
d = Hlsl.Abs(p);
|
||||
ang = p.YX;
|
||||
|
||||
if (Mod(ip.X + 1.0f, 2.0f) < 0.5f)
|
||||
{
|
||||
ang.X *= -1.0f;
|
||||
|
||||
}
|
||||
|
||||
if (Mod(ip.Y + 1.0f, 2.0f) > 0.5f)
|
||||
{
|
||||
ang.Y *= -1.0f;
|
||||
}
|
||||
|
||||
if (rnd3 < .5)
|
||||
{
|
||||
d = d.YX;
|
||||
ang = ang.YX;
|
||||
}
|
||||
|
||||
ang += 0.5f;
|
||||
ang *= 3.0f;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
if (rnd < .5)
|
||||
{
|
||||
p = p.YX * new Float2(1, -1);
|
||||
}
|
||||
|
||||
d.X = Hlsl.Length(p - 0.5f) - 0.5f;
|
||||
d.Y = Hlsl.Length(p + 0.5f) - 0.5f;
|
||||
|
||||
d = Hlsl.Abs(d);
|
||||
|
||||
ang = 0;
|
||||
ang.X = -Hlsl.Atan((p.Y - 0.5f) / (p.X - 0.5f));
|
||||
ang.Y = -Hlsl.Atan((p.Y + 0.5f) / (p.X + 0.5f));
|
||||
|
||||
if (Mod(ip.X + ip.Y, 2.0f) < 0.5f)
|
||||
{
|
||||
ang *= -1.0f;
|
||||
}
|
||||
|
||||
if (rnd < 0.5f)
|
||||
{
|
||||
ang *= -1.0f;
|
||||
}
|
||||
|
||||
if (rnd3 < 0.5f)
|
||||
{
|
||||
d = d.YX;
|
||||
ang = ang.YX;
|
||||
}
|
||||
|
||||
ang *= 4.0f / 6.2831853f;
|
||||
ang *= 2.0f;
|
||||
}
|
||||
|
||||
ang = Hlsl.Frac(ang + time / 4.0f);
|
||||
|
||||
return d;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Execute()
|
||||
{
|
||||
Float2 uv = (ThreadIds.XY - (Float2)DispatchSize.XY * 0.5f) / DispatchSize.Y;
|
||||
float gSc = 7.0f;
|
||||
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;
|
||||
Float3 col = new(1.0f, 0.9f, 0.8f);
|
||||
|
||||
for (int i = 0; i < 2; i++)
|
||||
{
|
||||
float di = d[i] - lw / 4.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);
|
||||
col = Hlsl.Lerp(col, 0, 1.0f - Hlsl.SmoothStep(0.0f, sf, di));
|
||||
col = Hlsl.Lerp(col, new(1.0f, 0.9f, 0.8f), 1.0f - Hlsl.SmoothStep(0.0f, sf, di + lw * 2.0f));
|
||||
col = Hlsl.Lerp(col, 0, 1.0f - Hlsl.SmoothStep(0.0f, sf, di + gap / lSc));
|
||||
col = Hlsl.Lerp(col, 1, 1.0f - Hlsl.SmoothStep(0.0f, sf, di + gap / lSc + lw));
|
||||
col = Hlsl.Lerp(col, 0, 1.0f - Hlsl.SmoothStep(0.0f, sf, di + 2.0f * gap / lSc));
|
||||
col = Hlsl.Lerp(col, 1 * tracks, 1.0f - Hlsl.SmoothStep(0.0f, sf, di + 2.0f * gap / lSc + lw));
|
||||
}
|
||||
|
||||
Float3 rgb = Hlsl.Sqrt(Hlsl.Max(col, 0.0f));
|
||||
Float4 color = new(rgb.X, rgb.Y, rgb.Z, 1.0f);
|
||||
|
||||
texture[ThreadIds.XY] = color;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,66 +17,84 @@ namespace ComputeSharp.Tests
|
||||
public class ShadersTests
|
||||
{
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
public void ColorfulInfinity(Device device)
|
||||
[Device(Device.Discrete)]
|
||||
[Data(typeof(ColorfulInfinity))]
|
||||
[Data(typeof(SwapChain.Shaders.Compute.ColorfulInfinity))]
|
||||
public void ColorfulInfinity(Device device, Type shaderType)
|
||||
{
|
||||
RunAndCompareShader(device, static texture => new ColorfulInfinity(texture, 0), 0.0000004f);
|
||||
RunAndCompareShader(device, shaderType, 0.0000004f);
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
public void FractalTiling(Device device)
|
||||
[Data(typeof(FractalTiling))]
|
||||
[Data(typeof(SwapChain.Shaders.Compute.FractalTiling))]
|
||||
public void FractalTiling(Device device, Type shaderType)
|
||||
{
|
||||
RunAndCompareShader(device, static texture => new FractalTiling(texture, 0), 0.0000005f);
|
||||
RunAndCompareShader(device, shaderType, 0.0000005f);
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
public void MengerJourney(Device device)
|
||||
[Data(typeof(MengerJourney))]
|
||||
[Data(typeof(SwapChain.Shaders.Compute.MengerJourney))]
|
||||
public void MengerJourney(Device device, Type shaderType)
|
||||
{
|
||||
RunAndCompareShader(device, static texture => new MengerJourney(texture, 0), 0.000011f);
|
||||
RunAndCompareShader(device, shaderType, 0.000011f);
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
public void TwoTiledTruchet(Device device)
|
||||
[Data(typeof(TwoTiledTruchet))]
|
||||
[Data(typeof(SwapChain.Shaders.Compute.TwoTiledTruchet))]
|
||||
public void TwoTiledTruchet(Device device, Type shaderType)
|
||||
{
|
||||
RunAndCompareShader(device, static texture => new TwoTiledTruchet(texture, 0), 0.00032f);
|
||||
RunAndCompareShader(device, shaderType, 0.00032f);
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
public void Octagrams(Device device)
|
||||
[Data(typeof(Octagrams))]
|
||||
[Data(typeof(SwapChain.Shaders.Compute.Octagrams))]
|
||||
public void Octagrams(Device device, Type shaderType)
|
||||
{
|
||||
RunAndCompareShader(device, static texture => new Octagrams(texture, 0), 0.0000006f);
|
||||
RunAndCompareShader(device, shaderType, 0.0000006f);
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
public void ProteanClouds(Device device)
|
||||
[Data(typeof(ProteanClouds))]
|
||||
[Data(typeof(SwapChain.Shaders.Compute.ProteanClouds))]
|
||||
public void ProteanClouds(Device device, Type shaderType)
|
||||
{
|
||||
RunAndCompareShader(device, static texture => new ProteanClouds(texture, 0), 0.0000004f);
|
||||
RunAndCompareShader(device, shaderType, 0.0000004f);
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
public void ExtrudedTruchetPattern(Device device)
|
||||
[Data(typeof(ExtrudedTruchetPattern))]
|
||||
[Data(typeof(SwapChain.Shaders.Compute.ExtrudedTruchetPattern))]
|
||||
public void ExtrudedTruchetPattern(Device device, Type shaderType)
|
||||
{
|
||||
RunAndCompareShader(device, static texture => new ExtrudedTruchetPattern(texture, 0), 0.00011f);
|
||||
RunAndCompareShader(device, shaderType, 0.00011f);
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
public void PyramidPattern(Device device)
|
||||
[Data(typeof(PyramidPattern))]
|
||||
[Data(typeof(SwapChain.Shaders.Compute.PyramidPattern))]
|
||||
public void PyramidPattern(Device device, Type shaderType)
|
||||
{
|
||||
RunAndCompareShader(device, static texture => new PyramidPattern(texture, 0), 0.00021f);
|
||||
RunAndCompareShader(device, shaderType, 0.00021f);
|
||||
}
|
||||
|
||||
[CombinatorialTestMethod]
|
||||
[AllDevices]
|
||||
public void TriangleGridContouring(Device device)
|
||||
[Data(typeof(TriangleGridContouring))]
|
||||
[Data(typeof(SwapChain.Shaders.Compute.TriangleGridContouring))]
|
||||
public void TriangleGridContouring(Device device, Type shaderType)
|
||||
{
|
||||
RunAndCompareShader(device, static texture => new TriangleGridContouring(texture, 0), 0.0006f);
|
||||
RunAndCompareShader(device, shaderType, 0.0006f);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -84,14 +102,12 @@ namespace ComputeSharp.Tests
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of shader to test.</typeparam>
|
||||
/// <param name="device">The device to use.</param>
|
||||
/// <param name="factory">A producer to create an instance of the shader to run.</param>
|
||||
/// <param name="delta">The comparison delta.</param>
|
||||
private static void RunAndCompareShader<T>(Device device, Func<ReadWriteTexture2D<Rgba32, Float4>, T> factory, float delta)
|
||||
where T : struct, IComputeShader
|
||||
private static void RunAndCompareShader(Device device, Type shaderType, float delta)
|
||||
{
|
||||
_ = device.Get();
|
||||
|
||||
string expectedPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Assets", $"{typeof(T).Name}.png");
|
||||
string expectedPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Assets", $"{shaderType.Name}.png");
|
||||
|
||||
IImageInfo imageInfo = Image.Identify(expectedPath);
|
||||
|
||||
@@ -99,14 +115,37 @@ namespace ComputeSharp.Tests
|
||||
|
||||
using (ReadWriteTexture2D<Rgba32, Float4> texture = device.Get().AllocateReadWriteTexture2D<Rgba32, Float4>(imageInfo.Width, imageInfo.Height))
|
||||
{
|
||||
device.Get().For(texture.Width, texture.Height, factory(texture));
|
||||
if (shaderType.IsAssignableTo(typeof(IComputeShader)))
|
||||
{
|
||||
static void RunComputeShader<T>(ReadWriteTexture2D<Rgba32, Float4> texture)
|
||||
where T : struct, IComputeShader
|
||||
{
|
||||
texture.GraphicsDevice.For(texture.Width, texture.Height, (T)Activator.CreateInstance(typeof(T), texture, 0f)!);
|
||||
}
|
||||
|
||||
var action = new Action<ReadWriteTexture2D<Rgba32, Float4>>(RunComputeShader<SwapChain.Shaders.Compute.ColorfulInfinity>);
|
||||
|
||||
action.Method.GetGenericMethodDefinition().MakeGenericMethod(shaderType).Invoke(null, new[] { texture });
|
||||
}
|
||||
else
|
||||
{
|
||||
static void RunPixelShader<T>(ReadWriteTexture2D<Rgba32, Float4> texture)
|
||||
where T : struct, IPixelShader<Float4>
|
||||
{
|
||||
texture.GraphicsDevice.ForEach(texture, (T)Activator.CreateInstance(typeof(T), 0f)!);
|
||||
}
|
||||
|
||||
var action = new Action<ReadWriteTexture2D<Rgba32, Float4>>(RunPixelShader<ColorfulInfinity>);
|
||||
|
||||
action.Method.GetGenericMethodDefinition().MakeGenericMethod(shaderType).Invoke(null, new[] { texture });
|
||||
}
|
||||
|
||||
_ = image.TryGetSinglePixelSpan(out Span<ImageSharpRgba32> span);
|
||||
|
||||
texture.CopyTo(MemoryMarshal.Cast<ImageSharpRgba32, Rgba32>(span));
|
||||
}
|
||||
|
||||
string actualPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Shaders", $"{typeof(T).Name}.png");
|
||||
string actualPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Shaders", $"{shaderType.Name}.png");
|
||||
|
||||
_ = Directory.CreateDirectory(Path.GetDirectoryName(actualPath)!);
|
||||
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする