Running a compute shader – desaturation (grayscale)
The first task we will tackle is a simple conversion of an input image to grayscale (color desaturation); the input image can be an image file or any texture resource (such as a rendered scene). The formula is used in subsequent recipes for filters, such as the Sobel edge detector.
Getting ready
For this recipe we just need to have a Direct3D device and device context available, and an image to process. The image used for illustration here is available within the downloadable content from Packt's website.
How to do it…
First we will prepare our input and output shader resources. The compute shader will take a Shader Resource View (SRV) of a Texture2D
variable as input and an Unordered Access View (UAV) of another Texture2D
as output.
Load the source image into an SRV and retrieve the texture:
var srcTextureSRV = ShaderResourceView.FromFile(device, "Village.png"); var srcTexture = srcTextureSRV.ResourceAs<Texture2D>();
Initialize a new...