Graphics Reference
In-Depth Information
// We must unmap the subresource so it can be used
// within the graphics pipeline again
context.UnmapSubresource(histogramCPU, 0);
}
7.
The result of running the
HistogramCS
compute shader over the
Village.png
image is shown in the following chart:
Luminance histogram result exported to a chart
How it works…
We have already covered the calculation of the relative luminance itself; however, we now map
the normalized luminance value to the 0-255 range. To determine the luminance histogram,
we count how many texels there are within the source image at each relative luminance level.
We have done this by mapping an unstructured (raw) buffer to a byte address UAV as the
output of the histogram shader. We then use the intrinsic
InterlockedAdd
method of
the UAV to increment the appropriate index within the buffer for each texel based on its
relative luminance. For example, a luminance of 255 (white), will result in the equivalent of
output[255]++;
, and a relative luminance of 127 (gray), will result in
output[127]++;
.
The more threads there are, the more collisions with the interlock.
By processing several pixels within a single thread, we can reduce the
number of threads required, although this needs to be balanced with
having enough threads to make effective use of the available hardware.
We have created a reusable function to create the UAV from a buffer. This simply determines
if the buffer is a structured or raw buffer, and creates the UAV description accordingly with
the appropriate size and element count based on the relevant byte stride (size of
uint
for
raw or the size of the
buffer.Description.StructureByteStride
method for a
structured buffer).
The interlocked methods on the
RWByteAddressBuffer
UAV allow us to write from multiple
threads to the same buffer. Usually, a compute shader is only able to write to addresses
reserved for the current thread. The range of interlocked operations include:
Add
,
AND
,
CompareExchange
,
CompareStore
,
Exchange
,
Max
,
Min
,
OR
, and
XOR
.