Calculating an image's luminance histogram
In this recipe we will explore using a compute shader to gather characteristics from the source image and output to a buffer. The characteristic that we will be determining is the image's luminance histogram, that is, how many texels are there within the texture for each luminance value (mapped from 0.0-1.0 to 0-255).
We will also cover how to retrieve the data from the GPU and load it into an array that is accessible from the CPU.
How to do it…
We'll begin with the HLSL code necessary to calculate the histogram.
The input continues to be a
Texture2D
SRV; however, this time our output UAV will beRWByteAddressBuffer
.Texture2D<float4> input : register(t0); RWByteAddressBuffer outputByteBuffer : register(u0); #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)
Our actual compute shader is quite simple:
// Calculate the luminance...