diff --git a/README.md b/README.md index b420d6a7..60e8e113 100644 --- a/README.md +++ b/README.md @@ -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(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: diff --git a/samples/ComputeSharp.SwapChain/Program.cs b/samples/ComputeSharp.SwapChain/Program.cs index 158eceb4..a9240019 100644 --- a/samples/ComputeSharp.SwapChain/Program.cs +++ b/samples/ComputeSharp.SwapChain/Program.cs @@ -21,6 +21,7 @@ class Program private static readonly Win32Application[] Samples = new Win32Application[] { new SwapChainApplication(static time => new((float)time.TotalSeconds)), + new SwapChainApplication(static time => new((float)time.TotalSeconds)), new SwapChainApplication(static time => new((float)time.TotalSeconds)), new SwapChainApplication(static time => new((float)time.TotalSeconds)), new SwapChainApplication(static time => new((float)time.TotalSeconds)), diff --git a/samples/ComputeSharp.SwapChain/Shaders/FourColorGradient.cs b/samples/ComputeSharp.SwapChain/Shaders/FourColorGradient.cs new file mode 100644 index 00000000..590d5236 --- /dev/null +++ b/samples/ComputeSharp.SwapChain/Shaders/FourColorGradient.cs @@ -0,0 +1,105 @@ +namespace ComputeSharp.SwapChain.Shaders; + +/// +/// A shader which interpolates four different colors into a rotating gradient pattern. +/// Ported from . +/// Created by Inigo Quilez. +/// License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. +/// +[AutoConstructor] +internal readonly partial struct FourColorGradient : IPixelShader +{ + /// + /// The current time since the start of the application. + /// + 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); + + /// + /// Standard 2D rotation formula. + /// + private static float2x2 Rotate(float a) + { + float c = Hlsl.Cos(a); + float s = Hlsl.Sin(a); + + return new(c, s, -s, c); + } + + /// + /// Generate a hash from a vector. + /// + /// Vector to hash. + /// Hashed vector. + 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); + } + + /// + /// Generate a smoothed noise pattern. + /// + /// Position vector. + /// Depth value. + 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; + } + + + /// + 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); + } +} diff --git a/tests/ComputeSharp.Tests/Assets/FourColorGradient.png b/tests/ComputeSharp.Tests/Assets/FourColorGradient.png new file mode 100644 index 00000000..14b5b669 Binary files /dev/null and b/tests/ComputeSharp.Tests/Assets/FourColorGradient.png differ diff --git a/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj b/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj index a25cd42b..83731df6 100644 --- a/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj +++ b/tests/ComputeSharp.Tests/ComputeSharp.Tests.csproj @@ -30,6 +30,7 @@ + @@ -58,6 +59,9 @@ PreserveNewest + + PreserveNewest + PreserveNewest diff --git a/tests/ComputeSharp.Tests/Shaders/Compute/FourColorGradient.cs b/tests/ComputeSharp.Tests/Shaders/Compute/FourColorGradient.cs new file mode 100644 index 00000000..db346e4c --- /dev/null +++ b/tests/ComputeSharp.Tests/Shaders/Compute/FourColorGradient.cs @@ -0,0 +1,110 @@ +namespace ComputeSharp.SwapChain.Shaders.Compute; + +/// +/// A shader which interpolates four different colors into a rotating gradient pattern. +/// Ported from . +/// Created by Inigo Quilez. +/// License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. +/// +[AutoConstructor] +internal readonly partial struct FourColorGradient : IComputeShader +{ + /// + /// The target texture. + /// + public readonly IReadWriteTexture2D texture; + + /// + /// The current time since the start of the application. + /// + 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); + + /// + /// Standard 2D rotation formula. + /// + private static float2x2 Rotate(float a) + { + float c = Hlsl.Cos(a); + float s = Hlsl.Sin(a); + + return new(c, s, -s, c); + } + + /// + /// Generate a hash from a vector. + /// + /// Vector to hash. + /// Hashed vector. + 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); + } + + /// + /// Generate a smoothed noise pattern. + /// + /// Position vector. + /// Depth value. + 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; + } + + + /// + 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); + } +} diff --git a/tests/ComputeSharp.Tests/ShadersTests.cs b/tests/ComputeSharp.Tests/ShadersTests.cs index 41460107..dfebe543 100644 --- a/tests/ComputeSharp.Tests/ShadersTests.cs +++ b/tests/ComputeSharp.Tests/ShadersTests.cs @@ -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))]