Drawing

Tone mapping and exposure

Light in a scene has no upper limit: a lamp can be a thousand times brighter than the wall it lights. A screen shows values from 0 to 1. Exposure picks which part of the scene's range matters, and a tone curve squeezes the whole range into 0 to 1 so that bright things roll off into white instead of being cut. This page explains both, the common curves, what they do to color, and the AgX curve step by step. The last part shows how RASM does it.

In short

  • Lighting gives linear light, often far above 1. Writing it to the screen as it is clips every bright area to the same flat white.
  • Exposure is one multiplier before the curve. Photographers count it in stops: +1 stop is ×2, −1 stop is ×0.5.
  • A tone curve maps 0 … ∞ to 0 … 1, keeps the order of brightness, and keeps contrast in the mid tones. Common ones: Reinhard, filmic curves like the ACES fit, and AgX.
  • A curve applied to each channel on its own changes the hue of bright saturated colors: a red light turns orange, then yellow. AgX mixes the channels a little first, so bright colors go to white instead.
  • Tone mapping runs once, on a 16-bit float image, after lighting and HDR effects like bloom, and before the UI and the sRGB encode.

1The problem: too much range

Light values in a scene spread over many stops, where one stop is a factor of 2. Below, a few example values on a log axis, where each grid step is one stop. A screen shows values from 0 to 1: everything at or above 1 is the same white.

what the screen can show: 0 to 1 above 1: all the same white
dark corner 0.005 gray wall 0.18 lamp-lit wall 4 sunlit spot 20 a lamp 100 1 the screen: 0 to 1, each value its own shade clipped: one flat white one grid step = one stop = twice the light. From 0.005 to 100 is about 14 stops. example values, in the units the lights are set in
An 8-bit screen has 256 codes for its whole range. A scene has detail in every stop: the shape of a lamp, a bright cloud, the edge of a sunlit spot. Clipping keeps the dark stops and throws away the bright ones.

The bars below show the same four values on screen, as the byte the display gets. Clipping makes three of them equal. A tone curve keeps them apart.

white 118 255 255 255 0.18 1 4 20

Clip

1, 4 and 20 all become byte 255. The lamp-lit wall and the sun look the same.

white 128 202 239 255 0.18 1 4 20

Tone map

Every value keeps its own shade. Bright values get closer together, but never equal, until far above the range.

Bar heights are the screen code (the sRGB value) from 0 to 1, which is close to how bright each bar looks. To have values above 1 to map at all, the scene must be drawn into an image that can store them: a floating-point format (section 6). Such an image is called HDR (high dynamic range); the 0 to 1 image for the screen is LDR or SDR.

2Exposure: one multiplier, counted in stops

A camera lets more or less light reach the sensor. A renderer does the same with one number: it multiplies the scene's linear color before the tone curve.

exposed = scene · 2EV

scene
the linear color from lighting, any value from 0 up
EV
the exposure in stops: +1 doubles the light, −1 halves it, 0 changes nothing
2EV
the linear factor: +2 stops is ×4, −3 stops is ×0.125, +3.32 stops is ×10

On a log axis, a multiplier is a shift. Exposure slides the range the curve can show along the scene's values, and so decides which scene value becomes middle gray: 0.18, the value of a surface that reflects 18% of the light, which looks halfway between black and white.

the scene values the curve can show (16.5 stops for AgX) the scene value that becomes middle gray
−2 stops · × 0.25: darker, bright values get room 0.72 → middle gray 0 stops · × 1 0.18 → middle gray +2 stops · × 4: brighter, dark values get room 0.045 → middle gray 0.0001 0.001 0.01 0.18 1 10 100 scene value, linear, on a log axis →
The range keeps its width of 16.5 stops; exposure only moves it. A scene lit by the sun needs a lower exposure than a room lit by one lamp, exactly like a real camera.

Three ways to choose the number:

−4 0 +4 stops

Manual

The artist or the app sets it per camera or per level. Simple, and the same image every frame.

Needs a value per lighting setup

average EV

Automatic (eye adaptation)

Each frame, measure the image's average log brightness (or a histogram) and set the exposure that maps it to middle gray. Change it slowly, like an eye.

A reduction pass per frame

f/8 aperture 1/125 s shutter ISO 100

Physical camera

Lights in real units, and the exposure from aperture, shutter time and ISO, as on a real camera (EV100).

Needs physical light units everywhere

3Tone curves

A tone curve maps any value from 0 up to the range 0 to 1. A good one keeps black at 0, keeps the mid tones bright and with contrast, and bends the bright values slowly into white. The four common answers, plotted on the same log axis (screen code up, scene value across, the dot is 0.18):

0.18 1

Clip

min(x, 1)

No curve at all. Correct up to 1, then one flat white.

0.18 1

Reinhard

x / (1 + x)

Never clips. But mid tones get darker (0.18 → 0.153) and the image looks flat and gray.

0.18 1

Filmic: the ACES fit

x (2.51 x + 0.03) / (x (2.43 x + 0.59) + 0.14)

An S-curve like film: deep shadows, punchy mid tones, white from about 4. Fitted to the ACES film look.

0.18 1

AgX

log2 encode, S-curve, matrices

A gentle S-curve over 16.5 stops, with a color step before and after. 0.18 lands on byte 128. Section 5.

Try the four curves. The plot runs each formula, and the ramp under it draws the same scene values through the chosen curve.

clip Reinhard ACES fit AgX
Pick Reinhard and raise the exposure until middle gray reaches about byte 128: then look where white begins. Then pick Clip and lower the exposure: the bright end only moves, it never rolls off.
Many more curves exist

Hable's filmic curve (from Uncharted 2), the full ACES reference transforms, Khronos PBR Neutral, Lottes, and many in-house curves. They differ in shadow and highlight shape and in how they treat color. The rest of this page uses the four above.

4What curves do to color

Clip, Reinhard and the ACES fit are applied to each channel on its own. For a gray that is fine: all three channels are equal and stay equal. For a saturated color it is not. Take a red light, (1, 0.05, 0.02), made 16 times brighter: (16, 0.8, 0.32).

  1. 16, 0.8, 0.32a bright red light
  2. each channelthrough the curve on its own
  3. R stops at 1G and B are still on the steep part
  4. orangeG caught up with R: the hue moved
red channel green channel blue channel
the light's mix on screen, ACES fit RGB RGB 0.75 0.46 hue 2° red → 32° orange

Per channel: the ACES fit

The big channel is stuck at 1, the small ones keep rising. Red goes to orange, then yellow, then white. Blue goes to cyan or purple.

the light's mix on screen, AgX RGB RGB 0.66 0.57 hue 2° red → 13° pale red

AgX: mix first, then curve

Green and blue rise together, so the color gets paler but stays near its hue, and ends as white.

Bar heights are the linear values on screen. The hue is the angle on the color wheel: 0° red, 30° orange, 60° yellow. Make the light brighter below and watch each row.

Keep red and move the brightness from +0 to +8: the ACES row turns orange and yellow, the AgX row turns pale and then white. Then try blue.
Why this matters

Fire, neon signs, sunsets, colored lamps and emissive materials are bright and saturated. With a per-channel curve they shift hue as they get brighter, and an artist cannot make a "brighter red" any more: it turns orange. A bright light that goes toward white, like on film, looks brighter, not differently colored.

5AgX, step by step

AgX is the tone mapper of Blender 4, designed by Troy Sobotka. Real-time engines usually use the "minimal" form by Benjamin Wrensch: two 3×3 matrices, a log2 step and a polynomial fitted to Blender's curve. Five steps, on linear RGB:

  1. insetmix the channels a little
  2. log216.5 stops → 0 … 1
  3. S-curvecontrast
  4. outsetsaturation back
  5. decode^ 2.2 → linear

Inset and outset: the color step

The inset matrix keeps about 84 to 88% of each channel and mixes about 4 to 8% of each other channel into it. Each row adds up to 1, so a gray stays the same gray: 0.18 stays 0.18. A saturated color moves a little toward gray: the red light (16, 0.8, 0.32) becomes (13.6, 1.41, 1.02). Its small channels are no longer tiny, so they climb the curve with the big one, and the color heads to white. The outset matrix after the curve roughly undoes the mix, so mid-tone colors keep their saturation.

inset (row by row: out.r = 0.8425 r + 0.0784 g + 0.0792 b, ...)
    0.842479062253094    0.0784335999999992   0.0792237451477643
    0.0423282422610123   0.878468636469772    0.0791661274605434
    0.0423756549057051   0.0784336            0.879142973793104

outset
    1.19687900512017    -0.0980208811401368  -0.0990297440797205
   -0.0528968517574562   1.15190312990417    -0.0989611768448433
   -0.0529716355144438  -0.0980434501171241   1.15107367264116

The log2 window: 16.5 stops around middle gray

The curve works on stops, not on light. It takes log2 of each channel and keeps a window from 10 stops below middle gray to 6.5 stops above it. Below the window is black, above it white.

0.00018 2^−12.47 0.18 2^−2.47 1.0 2^0 16.3 2^4.03 0 0.606 0.756 1 black white 10 stops below middle gray 6.5 above top: the value and its log2 · bottom: where it lands in 0 … 1, the curve's input
The window is lopsided on purpose: most of a scene's detail is in the shadows and mid tones. 1.0, the brightest value an SDR image could hold, is only 2.47 stops above middle gray.

The S-curve

A sigmoid (an S-shaped curve) on the window's 0 … 1. Flat near the ends, so shadows and highlights fade gently; steep in the middle, which gives the mid tones their contrast.

0 0.497 0.787 1 0 0.606 0.756 1 curve output (still display-encoded) position in the log2 window The polynomial (fit to Blender's curve) y = 15.5 x^6 − 40.14 x^5 + 31.96 x^4 − 6.868 x^3 + 0.4298 x^2 + 0.1191 x − 0.00232 x = 0 gives −0.002: clamped to black later. x = 1 gives 0.9986: just under white. Steepest near x = 0.62: most contrast just above middle gray.
Dashed straight line: no contrast change. The curve's output is a display value with a 2.2 gamma built in, not linear light yet; the last step decodes it.

The whole curve

v = inset · max(color, 0)       linear color, after exposure
v = clamp(log2(max(v, 1e-10)), −12.47393, 4.026069)
v = (v + 12.47393) / 16.5       window → 0 … 1
v = curve(v)                    the polynomial
v = outset · v
display = saturate(v) ^ 2.2     linear, for an sRGB target

−12.47393 and 4.026069 are log2(0.18) − 10 and log2(0.18) + 6.5. The last step returns linear display light because the target encodes to sRGB on write. A target without that encode would take the curve's output as it is.

Matrix order

The matrices above are written row by row: out.r is the first row times (r, g, b). In shading languages where a matrix constructor fills columns (GLSL), the same numbers must be written transposed, or the color step mixes the wrong way.

6Where it sits in a frame

Everything before the tone map works on linear HDR light, everything after it on display values from 0 to 1. Tone mapping runs once, in a full-screen pass.

shader work images other passes
linear light, no upper limit (HDR) lighting materials, lights HDR image RGBA 16-bit float HDR effects bloom, depth of field, blur tone map × exposure, curve display values, 0 to 1 (LDR) LDR effects FXAA, film grain, dithering UI, text exact colors, no curve swapchain, sRGB format encodes on write → display
Bloom must see values above 1, or every bright spot glows the same. The UI goes after the curve, so a white button is exactly white. The sRGB encode is the very last step, done by the target's format (Color spaces).

The HDR image needs a format that stores values above 1 with enough precision in the dark:

FormatBytes per pixelLargest valuePrecisionFor HDR?
RGBA8 UNORM41256 even stepsNo: clamps at 1, bands in the dark
R11G11B10 float4about 650006, 6 and 5 mantissa bits, about 1.6 to 3%Often: half the memory, but no alpha and no negative values
RGBA16 float86550410 mantissa bits, about 0.1%The usual choice
RGBA32 float16about 3 · 103823 mantissa bitsMore than needed; twice the bandwidth

At 1920 × 1080, a 16-bit float RGBA image is 15.8 MiB, twice an 8-bit one (7.9 MiB). Floats keep the same relative precision at every brightness, which suits light: a step in the shadows is as small, relative to the value, as a step in the highlights.

7Worked example

Middle gray, 0.18, with exposure 0 stops, through AgX:

0.18after the inset: a gray stays grayeach row sums to 1
−2.474log2log2(0.18)
0.606place in the window(−2.474 + 12.474) / 16.5
0.497after the S-curve and the outsetpolynomial at 0.606
0.2145linear display light0.497 ^ 2.2
0.50 · 128sRGB code and byte on screensRGB encode of 0.2145

So middle gray lands in the middle of the screen's range. The same for scene value 1.0: window 0.756, curve 0.787, linear 0.590, code 0.792, byte 202. One stop more exposure turns 0.18 into 0.36: linear 0.351, byte 160.

The same values through every curve, as the byte on screen:

Scene valueClipReinhardACES fitAgX
0.0125251218
0.18118109141128
1255188232202
4255231252239
16255248255254

Read it by column. Clip spends all its range below 1. Reinhard keeps the brights apart but makes 0.18 darker than without any curve. The ACES fit has the most contrast and crushes the shadows (0.01 → byte 12). AgX sits between them.

8Pitfalls

The image is flat, gray and too dark in the brights
Tone mapping twice: a material shader applies a curve, and the final pass applies another. Tone map once, in one pass; material shaders write unbounded linear light.
Wrong contrast, colors look odd
Tone mapping values that are already sRGB-encoded. The curve expects linear light: run it before the encode, and decode any sRGB input first.
UI looks dim or tinted; text is gray instead of white
The UI was drawn into the HDR image and went through the curve (AgX maps 1.0 to byte 202). Draw the UI after the tone map, onto the display image.
Bright areas are flat white even with a curve
An 8-bit or UNORM target between lighting and the tone map clamped the values to 1. Every image before the tone map, including post passes, must be float.
Bloom glows the same around every light
Bloom runs after the tone map, on values that stop at 1. Run it on the HDR image.
Bright saturated lights change hue
A per-channel curve (section 4). Use a curve with a color step like AgX, or tone map a luminance and keep the color ratios.
Transparent edges or blended UI look wrong
The curve was applied to alpha, or to colors already multiplied by alpha. Alpha is coverage, not light: pass it through, or write 1 for the final image.
The whole image pumps brighter and darker
Automatic exposure that adapts too fast. Smooth it over time.

9In RASM

RASM draws the scene into a 16-bit float target and maps it to the swapchain in one pass, Tone Map, with AgX in its minimal form. The default frame of Renderer::render builds it as a render graph. With several views (Many views of one scene) each view has its own target and its own Tone Map, into its rect of the window or into a view texture:

graph passes images shader steps the application
the graph of Renderer::render, every frame scene passes Cull 1, Scene 1, Hi-Z, Cull 2, Scene 2 scene_hdr R16G16B16A16_SFLOAT + D32_SFLOAT depth MSAA Resolve or TAA antiAliasing only Tone Map reads color 0, writes its rect UI with config.ui toneMap.slang, per pixel sample source, lerp to bloom × exposure (push constant) agx: inset, log2, curve, outset ^ 2.2, alpha = 1 B8G8R8A8_SRGB encodes on write after the graph: Renderer::endFrame drawStatsOverlay UI off only; onto the swapchain, not tone mapped present setCameraExposure(camera, e) read by the pass when it runs scene_hdr is cleared to BACKGROUND_COLOR, 0.018 linear gray, which also goes through the curve: byte 33 on screen. Without occlusion culling the scene passes are just Cull and Scene.
The graph is built once per view and kept; the exposure is read inside the pass's record function, so changing it costs nothing. Applications with their own graph add the pass themselves with Renderer::addToneMapPass, after any HDR post passes.

An application that builds its own graph, as examples/06_shadows does with a post pass in between:

const rasm::RenderTargetHandle sceneTarget = graph.createRenderTarget({
    .name = "scene_rt",
    .swapchainScale = 1.0f,
    .colorFormats = {rasm::SCENE_COLOR_FORMAT},
    .depthFormat = rasm::Format::D32_SFLOAT,
});
if (!engine.addScenePasses(graph, scene, camera, sceneTarget))
    return 1;
if (!engine.addToneMapPass(graph, sceneTarget, camera))
    return 1;
graph.compile();

engine.setCameraExposure(camera, 2.0f);

2.0f is one stop brighter. In examples/06_shadows the Post pass (a vignette) reads scene_rt and writes post_rt, also SCENE_COLOR_FORMAT, and the tone map reads post_rt. Its Inset pass then draws the shadow map's raw depths in a corner of the swapchain, after the tone map, so the curve does not change them, and Renderer::addUiPass comes last. examples/01_render_graph draws an unlit bunny into an HDR target and has no tone-map pass: its unlit colors are already in display range.

Where RASM follows the textbook, and where it differs

PartThis pageRASM
Curveclip, Reinhard, ACES fit, AgXAgX only, the minimal form: Blender's matrices, the 16.5-stop window and the polynomial fit (AGX_INSET, AGX_OUTSET, AGX_MIN_EV, AGX_MAX_EV, agxContrast). No choice of curve, and none of Blender's "looks".
Exposuremanual, automatic, physical cameraManual only, per camera: Camera::exposure, a linear factor (not stops), 1 by default. Renderer::setCameraExposure logs an error and keeps the old value for zero or a negative value. No automatic exposure, no EV100.
What goes through the curvethe lit sceneEverything in the source target: PBR materials, unlit materials and the clear color. So an unlit white (1.0) shows as byte 202, not 255.
HDR formatRGBA 16-bit floatSCENE_COLOR_FORMAT, R16G16B16A16_SFLOAT. Renderer::addScenePasses takes no other color format; its only other choice is a depth-only target, for shadows.
Where it sitsafter HDR effects, before UI and the encodeThe last HDR pass of the default graph. It reads one value per pixel, so a multisampled target is resolved first (Renderer::addMsaaResolvePass), and with TAA it reads the output of Renderer::addTaaPass. The one HDR effect built in is bloom: Renderer::addBloomPasses blurs the HDR target, and the tone map mixes it in, lerp(scene, bloom, Camera::bloomStrength), before the exposure (EngineConfig::bloom for Renderer::render, off by default). An app adds other effects of its own before the pass. After it, not tone mapped, come the Dear ImGui windows (Renderer::addUiPass, with EngineConfig::ui) or, with the UI off, the stats overlay, and overlays like the shadow-map inset of examples/06_shadows. The swapchain encodes.
Display decodepower 2.2, then the target's sRGB encodeThe same: pow(saturate(v), 2.2), then B8G8R8A8_SRGB (a headless engine's offscreen swapchain is R8G8B8A8_SRGB, also sRGB). The two curves differ near black: a curve output of 0.05 shows as code 0.018.
Alphapass through, or 1The pass writes 1.
DisplaySDR or HDR displaysSDR only: an 8-bit sRGB swapchain. No HDR10 or scRGB output.

Where it lives in code

WhatWhere
The curveagx, agxContrast and the constants in shaders/lib/agx.slang, which the export pass shares; fragMain multiplies by exposure first
The passRenderer::addToneMapPass in src/renderer/toneMap.cpp: checks the camera, the source, the bloom target and the output, and adds Tone Map with read of the source (and the bloom) and writeSwapchain, or write of the output's target (a view texture: one color, no depth). When it runs it sets the viewport and the scissor to the output's rect (getViewPixels) and takes its pipeline from getToneMapPipeline, made the first time an output format needs it (Renderer::ToneMapPipelines, at most MAX_TONE_MAP_FORMATS); on a shader error it draws nothing and tries again next frame. While a debug view is shown (getDrawnDebugView), it binds that format's debug pipeline instead (fragDebug: the values as they are; fragDebugHeat: a count as a heat color), with no exposure, bloom or curve.
Push constantsgfx::ToneMapPushConstants in src/renderer/uberMaterialTypes.h and ToneMapPushConstants in shaders/passes/toneMap.slang: source (bindless index), exposure, bloom (bindless index, INVALID_TEXTURE_INDEX without one) and bloomStrength: 16 bytes, all of PUSH_CONSTANTS_SIZE. validateRasmStructs in src/renderer/shaderCompiler.cpp compares them.
The default frameViewGraph::render and ViewGraph::addView in src/renderer/viewGraph.cpp: each view's target (scene_hdr, then scene_hdr_1, ...), cleared to BACKGROUND_COLOR (src/renderer/viewGraph.h), and with bloom (EngineConfig::bloom, View::bloom) the bloom passes
The HDR formatSCENE_COLOR_FORMAT in include/rasm/renderer.h; checked in Renderer::addScenePasses, src/renderer/gpuScene.cpp
ExposureCamera::exposure in include/rasm/camera.h; Renderer::setCameraExposure, Renderer::getCameraExposure in src/scene/camera.cpp
Material outputshadePBR in shaders/lib/pbr.slang and shadeUnlit in shaders/lib/unlit.slang: linear, no curve
After the tone mapRenderer::addUiPass in src/renderer/ui.cpp, added by ViewGraph::render; without the UI, drawStatsOverlay in src/renderer/statsOverlay.cpp, from Renderer::endFrame in src/renderer/renderer.cpp
Examplesexamples/06_shadows/main.cpp (scene → Post → tone map → Inset); examples/01_render_graph/main.cpp (HDR target, no tone map); examples/20_views/main.cpp (a tone map per view, into rects of the window and into a view texture)
Keep these true
  • Tone map once. shadePBR and shadeUnlit return unbounded linear light; no material shader applies a curve.
  • Every target between the scene passes and Tone Map is SCENE_COLOR_FORMAT, post passes too, or the values clamp at 1 before the curve.
  • The shader returns linear display light and relies on the sRGB swapchain to encode. It never applies the sRGB curve itself.
  • The C++ and Slang ToneMapPushConstants keep the same name and layout: validateRasmStructs finds the shader's struct by its name, and the static_asserts in uberMaterialTypes.h pin the C++ side.
  • UI and debug overlays draw after Tone Map, onto the swapchain.
  • A change to the curve's matrices, window or polynomial changes the labs on this page too (docs/assets/labs/tone-mapping.js copies them).

Further reading