Graphics Reference
In-Depth Information
result += BlurKernel[i + FILTERRADIUS] *
FilterGroupMemX[j];
}
// Write the result to the output
output[dispatchThreadId.xy] = result;
This completes our horizontal filter. The vertical filter function
BlurFilterVerticalCS works similarly to the horizontal filter. The source for
the function can be found in the downloadable code for this chapter available on
Packt's website.
6.
In order to run both the horizontal and vertical filters over the source image, it is
necessary to use two texture resources and ping-pong between them.
7.
We begin by changing our target resource description so that desc.BindFlags also
indicates that the resource will be used as an SRV. We then create an SRV for the first
target in addition to the UAV. Thereafter, define a second target ( target2 ) with the
same description, and create its SRV and UAV.
var srcTextureSRV = ShaderResourceView.FromFile(device,
"Village.png");
var srcTexture = srcTextureSRV.ResourceAs<Texture2D>();
var desc = srcTexture.Description;
desc.BindFlags = BindFlags.ShaderResource |
BindFlags.UnorderedAccess;
var target = new Texture2D(device, desc);
target.DebugName = "CSTarget";
var targetUAV = new UnorderedAccessView(device, target);
var targetSRV = new ShaderResourceView(device, target);
var target2 = new Texture2D(device, desc);
target2.DebugName = "CSTarget2";
var target2UAV = new UnorderedAccessView(device, target2);
var target2SRV = new ShaderResourceView(device, target2);
8.
With our two resources ready, and the shader code for the horizontal and vertical
shaders in the strings horizHLSLCode and vertHLSLCode , respectively, we need
to compile both the horizontal and vertical shaders, and then dispatch the thread
groups for each, switching the SRV and UAV each time.
// Compile the shaders
using (var horizBC = ShaderBytecode.Compile(horizHLSLCode,
"BlurFilterHorizontalCS", "cs_5_0"))
using (var vertBC = ShaderBytecode.Compile(vertHLSLCode,
"BlurFilterVerticalCS", "cs_5_0"))
using (var horizCS = new ComputeShader(device, horizBC))
 
Search WWH ::




Custom Search