From c2fd1f0ac248fbacb491c5b883a78aa098207f86 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 7 Mar 2021 22:35:21 +0100 Subject: [PATCH] Added readme docs about shader constants --- README.md | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7dfbcafb..37b7fe60 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ - [GPU resource types](#gpu-resource-types) - [HLSL vector and matrix types](#hlsl-vector-and-matrix-types) - [HLSL intrinsics](#hlsl-intrinsics) + - [Shader constants](#shader-constants) - [Working with images](#working-with-images) - [Shader metaprogramming](#shader-metaprogramming) - [Requirements](#requirements) @@ -139,7 +140,7 @@ public readonly struct SoftmaxActivation : IComputeShader public readonly ReadWriteBuffer buffer; public readonly float k; - public void Execute(ThreadIds ids) + public void Execute() { float exp = Hlsl.Exp(k * buffer[ThreadIds.X]); float log = Hlsl.Log(1 + exp); @@ -149,6 +150,28 @@ public readonly struct SoftmaxActivation : IComputeShader } ``` +## Shader constants + +One common approach to make shaders easier to modify and experiment with is to separate the parameters being used from the code using them, by defining them as constants. **ComputeSharp** has special handling for this, and will rewrite static readonly properties as static constants in the generated shaders, which are optimized by the compiler for frequent access during execution. The advantage of using constants is that they're directly embedded into a compiled shader, so they don't need to be loaded in memory whenever a shader is dispatched. Constant properties can be of any of the supported HLSL primitive types, including vector and matrix types. Furthermore, the initialization of these constants can also use any of the available HLSL intrinsics. If the same result is being used multiple times while a shader is executed, moving values to a shader constant is a good way to avoid repeatedly computing the same value over and over for each invocation. + +Here is an example of how shader constants can be declared and used: + +```csharp +[AutoConstructor] +public readonly struct SampleShaderWithConstants : IComputeShader +{ + public readonly ReadWriteBuffer buffer; + + private static float Pi => 3.14f; + private static Float2 SinCosPi => new(Hlsl.Sin(Pi), Hlsl.Cos(Pi)); + + public void Execute() + { + buffer[ThreadIds.X] = Pi + SinCosPi.X * SinCosPi.Y; + } +} +``` + ## Working with images As mentioned in the [GPU resource types](#gpu-resource-types) paragraph, there are several texture types that are specialized to work on image pixels, such as `ReadWriteTexture2D`. Let's imagine we want to write a compute shader that applies some image processing filter: we will need to load an image (eg. using [ImageSharp](https://github.com/SixLabors/ImageSharp), or just the `System.Drawing` APIs) and then process it on the GPU, but without spending time on the CPU to convert pixels from a format such as RGBA32 to the normalized `float` values we want our shader to work on. We can do this by utilizing the `ReadWriteTexture2D` type as follows: