Merge pull request #147 from hawkerm/samples/four-color-gradient

Add FourColorGradient Sample
このコミットが含まれているのは:
Sergio Pedri authored and GitHub committed 2021-11-08 00:32:43 +01:00
コミット 539c6d49a4
7ファイル変更
+242

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

+13
ファイルの表示
@@ -249,6 +249,19 @@ public readonly partial struct GrayscaleEffect : IComputeShader
> **NOTE:** this is just an example to illustrate how these texture types can help with automatic pixel format conversion. You're free to use any library of choice to load and save image data, as well as to how to structure your compute shaders representing image effects. This is just one of the infinite possible effects that could be achieved by using **ComputeSharp**.
You can also use a similar technique to create a blank texture, render a single frame of a pixel shader, and save the output to a file:
```csharp
// Create a blank 1280x720 surface for us to render to
using var texture = Gpu.Default.AllocateReadWriteTexture2D<Bgra32, float4>(1280, 720);
// Using an existing shader from our samples, we can render a specific time frame
Gpu.Default.ForEach(texture, new FourColorGradient(0));
// Save the result to a file on disk
texture.Save("output.png");
```
## Shader metaprogramming
One of the reasons why **ComputeSharp** compiles shaders at runtime is that it allows it to support a number of dynamic scenarios, including shader metaprogramming. What this means is that it's possible to have a shader that captures an instance of a `Delegate` type (provided the signature matches the supported types mentioned above), and reuse it with different methods. The library will automatically run different variations of the shaders depending on what methods they are capturing. Here is an example:
+1
ファイルの表示
@@ -21,6 +21,7 @@ class Program
private static readonly Win32Application[] Samples = new Win32Application[]
{
new SwapChainApplication<HelloWorld>(static time => new((float)time.TotalSeconds)),
new SwapChainApplication<FourColorGradient>(static time => new((float)time.TotalSeconds)),
new SwapChainApplication<ColorfulInfinity>(static time => new((float)time.TotalSeconds)),
new SwapChainApplication<FractalTiling>(static time => new((float)time.TotalSeconds)),
new SwapChainApplication<TwoTiledTruchet>(static time => new((float)time.TotalSeconds)),
+105
ファイルの表示
@@ -0,0 +1,105 @@
namespace ComputeSharp.SwapChain.Shaders;
/// <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 : IPixelShader<float4>
{
/// <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 value.</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 float4 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 col = Hlsl.Lerp(layer1, layer2, Hlsl.SmoothStep(0.5f, -0.3f, tuv.Y));
return new(col, 1.0f);
}
}
バイナリファイルは表示されません。

変更後

幅:  |  高さ:  |  サイズ: 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>
+110
ファイルの表示
@@ -0,0 +1,110 @@
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 value.</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 col = Hlsl.Lerp(layer1, layer2, Hlsl.SmoothStep(0.5f, -0.3f, tuv.Y));
texture[ThreadIds.XY] = new float4(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))]