Graphics Reference
In-Depth Information
If using sRGB input textures, the non-linear RGB components will be automatically linearized
for you when sampling; however, writing back to the UAV will require a manual reapplication
of the gamma correction because an sRGB texture cannot be bound to a UAV. This would look
something similar to the following:
output[dispatchThreadId.xy] = pow(value, (1/2.2));
After calculating the relative luminance, we apply this to the red, green, and blue components
of the result and write this value back to the appropriate location within the UAV—as we are
using RWTexture2D , we take advantage of the video hardware's automatic conversion from
float4 to the appropriate pixel format. We have now desaturated the source pixel
by 100 percent.
The completed project for this chapter supports rendering the output
back to the render target. It also supports using the render target as
the input into the compute shaders as well as a number of sample
images ( Ctrl + /- cycles through these).
There's more…
It is often useful to be able to apply a percentage of the effect upon the source image.
For example, rather than a 100 percent desaturation, we might want to only apply
50 percent of the effect.
This can easily be achieved by linearly interpolating (lerp) between the original pixel and
target pixel using the lerp HLSL intrinsic function. In fact, because linear interpolation can
be extrapolated beyond the 0 to 1 range, we can use this approach to implement the reverse
of desaturation and saturate the image.
We can add a constant buffer to the previous HLSL and create the equivalent structure
in C# to pass in this information in the same way we can with vertex or pixel shaders.
1.
Create a new constant buffer in the compute shader:
cbuffer ComputeConstants : register(b0)
{
float Intensity;
};
2.
The corresponding C# structure for the constant buffer is shown in the following
code snippet:
[System.Runtime.InteropServices.StructLayout(System.Runtime.
InteropServices.LayoutKind.Sequential)]
public struct ComputeConstants
{
 
Search WWH ::




Custom Search