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.

  1. The input continues to be a Texture2D SRV; however, this time our output UAV will be RWByteAddressBuffer.
    Texture2D<float4> input : register(t0);
    RWByteAddressBuffer outputByteBuffer : register(u0); ...

Get Direct3D Rendering Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.