Graphics Reference
In-Depth Information
One of the main advantages of compute shaders over other solutions is that it is designed
to integrate well with Direct3D, allowing them to be used for both compute-only applications
as well as to augment graphics algorithms.
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.
1.
Load the source image into an SRV and retrieve the texture:
var srcTextureSRV = ShaderResourceView.FromFile(device,
"Village.png");
var srcTexture = srcTextureSRV.ResourceAs<Texture2D>();
2.
Initialize a new texture that is the same size as the original texture and create an
unordered access view for it. Note that we are also setting a debug name:
var desc = srcTexture.Description;
desc.BindFlags = BindFlags.UnorderedAccess;
var target = new Texture2D(device, desc);
target.DebugName = "CSTarget";
var targetUAV = new UnorderedAccessView(device, target);
 
Search WWH ::




Custom Search