HDR Tone Mapping C++ DirectCompute Sample

The sample shows how to calculate average luminance of an image using parallel reduction, and then apply it to tone mapping. Additionally, blur and bloom effects can be calculated both on compute and pixel shaders (for comparison) and then rendered.

It also demonstrates how to setup and run image post-processing on a compute shader, it modifies a skybox texture by applying to it bloom and blur and toning it. It is based on SDK application by the same name. The program requires Windows 8 and DirectX 11.

The application computes luminance of a skybox texture, which in technical terms is a luminous flux incident on a surface per unit area or a measurement of perceived brightness, before applying it to the final rendered output. The formula to compute luminance is to add up luminance of all individual pixels in the image and divide the sum by the number of pixels.

Mathematically the algorithm is termed as convolution, or an integral that expresses the amount of overlap of one function as it is shifted over another one. When implementing convolution in pixel shader, we read all adjacent pixels for each pixel, multiply values by weight and add them together to derive the result. Compute shader eliminates redundant reads by allowing to load as many pixels as there are threads in each thread group, and then to cache the pixels in shared memory on the device, where each group of threads can work in parallel on a different block of pixels thus significantly improving performance of the algorithm. Shared memory is usually implemented as on-chip registers or a part of on-chip cache and is very fast to access.

In this example, each pixel in shared memory is loaded only once from input image using texture read.

Please find the code and better documentation on the MSDN Code Gallery.

One comment

  1. Very cool.

    Thank you so much for taking the time the upload the sample and also providing excellent documentation for it.
    I’m currently dabbling with image processing and high level shading languages and this helps.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: