Graphics Reference
In-Depth Information
Detecting edges with the Sobel edge-
detection filter
In this recipe we will implement a single pass 3x3 Sobel convolution filter. This is the
first convolution filter we will look at that does not implement a simple "sum of products"
calculation as well as not being separable.
To keep things simple, this implementation has not undergone the same level of optimization
as the separable convolution filter compute shaders; however, it would be a fairly simple task
to modify it to work with the group-shared memory approach.
Getting ready
This recipe makes use of the constant buffer and the lerpKeepAlpha function that we
implemented in the Adjusting the contrast and brightness recipe.
You will also use the LUMINANCE #define macro we created in the Running a compute
shader - desaturation (grayscale) recipe.
How to do it…
We will implement two variations of the Sobel edge-detection compute shader, one will overlay
the result onto the original image (producing an outlining effect), and the other will return the
black and white result. Both use the same Sobel function to detect the edge.
1.
Add the following HLSL code, for the Sobel edge overlay, to a string or HLSL shader
file. We will implement the SobelEdge function used here, shortly.
#define THREADSX 32
#define THREADSY 32
// used for RGB/sRGB color models
#define LUMINANCE_RGB float3(0.2125, 0.7154, 0.0721)
#define LUMINANCE(_V) dot(_V.rgb, LUMINANCE_RGB)
[numthreads(THREADSX, THREADSY, 1)]
void SobelEdgeOverlayCS(uint groupIndex: SV_GroupIndex,
uint3 groupId : SV_GroupID, uint3 groupThreadId:
SV_GroupThreadID, uint3 dispatchThreadId :
SV_DispatchThreadID)
{
float4 sample = input[dispatchThreadId.xy];
float threshold = 0.4f;
float thickness = 1;
 
Search WWH ::




Custom Search