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.
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.
Clip
1, 4 and 20 all become byte 255. The lamp-lit wall and the sun look the same.
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.
Three ways to choose the number:
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
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
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):
Clip
min(x, 1)
No curve at all. Correct up to 1, then one flat white.
Reinhard
x / (1 + x)
Never clips. But mid tones get darker (0.18 → 0.153) and the image looks flat and gray.
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.
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.
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).
- 16, 0.8, 0.32a bright red light
- each channelthrough the curve on its own
- R stops at 1G and B are still on the steep part
- orangeG caught up with R: the hue moved
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.
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.
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:
- insetmix the channels a little
- log216.5 stops → 0 … 1
- S-curvecontrast
- outsetsaturation back
- 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.
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.
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.
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.
The HDR image needs a format that stores values above 1 with enough precision in the dark:
| Format | Bytes per pixel | Largest value | Precision | For HDR? |
|---|---|---|---|---|
| RGBA8 UNORM | 4 | 1 | 256 even steps | No: clamps at 1, bands in the dark |
| R11G11B10 float | 4 | about 65000 | 6, 6 and 5 mantissa bits, about 1.6 to 3% | Often: half the memory, but no alpha and no negative values |
| RGBA16 float | 8 | 65504 | 10 mantissa bits, about 0.1% | The usual choice |
| RGBA32 float | 16 | about 3 · 1038 | 23 mantissa bits | More 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:
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 value | Clip | Reinhard | ACES fit | AgX |
|---|---|---|---|---|
| 0.01 | 25 | 25 | 12 | 18 |
| 0.18 | 118 | 109 | 141 | 128 |
| 1 | 255 | 188 | 232 | 202 |
| 4 | 255 | 231 | 252 | 239 |
| 16 | 255 | 248 | 255 | 254 |
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:
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
| Part | This page | RASM |
|---|---|---|
| Curve | clip, Reinhard, ACES fit, AgX | AgX 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". |
| Exposure | manual, automatic, physical camera | Manual 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 curve | the lit scene | Everything 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 format | RGBA 16-bit float | SCENE_COLOR_FORMAT, R16G16B16A16_SFLOAT. Renderer::addScenePasses takes no other color format; its only other choice is a depth-only target, for shadows. |
| Where it sits | after HDR effects, before UI and the encode | The 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 decode | power 2.2, then the target's sRGB encode | The 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. |
| Alpha | pass through, or 1 | The pass writes 1. |
| Display | SDR or HDR displays | SDR only: an 8-bit sRGB swapchain. No HDR10 or scRGB output. |
Where it lives in code
| What | Where |
|---|---|
| The curve | agx, agxContrast and the constants in shaders/lib/agx.slang, which the export pass shares; fragMain multiplies by exposure first |
| The pass | Renderer::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 constants | gfx::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 frame | ViewGraph::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 format | SCENE_COLOR_FORMAT in include/rasm/renderer.h; checked in Renderer::addScenePasses, src/renderer/gpuScene.cpp |
| Exposure | Camera::exposure in include/rasm/camera.h; Renderer::setCameraExposure, Renderer::getCameraExposure in src/scene/camera.cpp |
| Material output | shadePBR in shaders/lib/pbr.slang and shadeUnlit in shaders/lib/unlit.slang: linear, no curve |
| After the tone map | Renderer::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 |
| Examples | examples/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) |
- Tone map once.
shadePBRandshadeUnlitreturn unbounded linear light; no material shader applies a curve. - Every target between the scene passes and
Tone MapisSCENE_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
ToneMapPushConstantskeep the same name and layout:validateRasmStructsfinds the shader's struct by its name, and thestatic_asserts inuberMaterialTypes.hpin 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.jscopies them).
Further reading
- Troy Sobotka, AgX: the original design and its notes on "display rendering" (github.com/sobotka/AgX); Blender 4.0 manual, "Color Management".
- Benjamin Wrensch, "Minimal AgX implementation" (iolite engine blog, 2023): the matrices and the polynomial fit used here.
- Krzysztof Narkowicz, "ACES Filmic Tone Mapping Curve" (2016): the ACES fit.
- Erik Reinhard, Michael Stark, Peter Shirley, James Ferwerda, "Photographic Tone Reproduction for Digital Images", SIGGRAPH 2002.
- Sébastien Lagarde, Charles de Rousiers, "Moving Frostbite to Physically Based Rendering" (SIGGRAPH 2014 course): physical light units and camera exposure.
- John Hable, "Filmic Tonemapping Operators" (2010): the Uncharted 2 curve.