Drawing

Bloom

A screen cannot show light brighter than white. A real camera or eye shows it anyway: very bright light spreads into a soft glow around it. Bloom copies that glow. It blurs the image at many sizes, from a few pixels to a large part of the screen, and mixes a small share of the blur back in. This page explains why the blur has many sizes, how a chain of half-size images makes it cheap, the two filters of the chain, and how to mix it in without making the image brighter. The last part shows how RASM does it.

In short

  • After tone mapping, a lamp 16 times brighter than white and one 256 times brighter look the same. A glow around them shows how bright they are.
  • A real lens spreads light as a sharp core with a long, faint tail. One blur cannot make that shape; a sum of blurs of growing size can.
  • A chain of images, each half the size of the one before, gives those blurs for about 8 texture reads per screen pixel, where one wide blur would need hundreds.
  • Going down, a 13-tap filter halves the image without flicker; the first step uses a Karis average so one very bright pixel cannot flash. Going up, a tent filter adds each level to the one above.
  • Mix it in with lerp(scene, bloom, strength), no threshold: the light's total stays the same, and only very bright light shows a glow.

1The problem: brighter than white

Three lamps, 1, 16 and 256 times as bright as white. The tone curve maps them to bytes 202, 254 and 255 (Tone mapping): on screen they are the same white disc.

1 → 202 16 → 254 256 → 255

Without bloom

The curve rolls off into white, so very different lights look alike. Nothing on screen says which one is the sun.

1 16 256

With bloom

A small share of each lamp's light spreads around it. The glow grows with the brightness, so the eye reads 256 as much brighter than 1.

The glow is not a trick of games. In a camera, a little light scatters in the glass, the coatings and the dust, and in the eye it scatters in the cornea and the lens. Most of a point's light lands where it should; a small part spreads over a wide area. Around a dim light that part is too faint to see. Around the sun it is brighter than the sky next to it.

2The shape of the glow: a sum of blurs

How a lens spreads one point of light is its point spread function. Measured, it has a sharp core and a long tail that falls off slowly. A single Gaussian blur falls off fast, so it is either too narrow to reach far, or too wide and loses the core. A sum of blurs, each twice as wide as the one before and each with the same total light, has the right shape:

each blur: 2, 4, 8, 16, 32, 64 pixels wide, the same light each their sum: a sharp core and a long tail
10% 0.1% 0.001% −200 px the point +200 px share of the point's light per pixel, log scale: one grid line = 10 times the curves are Gaussians of standard deviation 2 to 64 pixels
Near the point the narrow blurs win: a sharp core. Far away only the widest blur is left: a long tail. A chain of half-size images gives exactly such a set of blurs, one per level.

3The chain: blurs at many sizes, cheaply

A wide blur is costly at full size. A blur 64 pixels wide reads hundreds of pixels for every pixel. But a blur of a few texels on an image 64 times smaller covers the same area, and that image has 4096 times fewer pixels. So the image is halved again and again (the down chain), and then walked back up, each level adding a soft copy of the level below (the up chain).

images, each half the size of the one before down: halve with a 13-tap filter up: add a tent-blurred copy of the level below
down: halve again and again scene 1920 × 1080 1/2 960 × 540 1/4 1/8 1/16 1/32 1/64 up 5 up 4 up 3 up 2 up 1 ÷ 6 the bloom up: add the level below, blurred dashed: each up level starts from the down level of its size, then adds the blurred level below. Up 1 holds 6 blurred copies of the scene, so it is divided by 6 to keep the light's total.
Sizes for a 1920 × 1080 scene. Each down level is a blur of the scene (the levels are blurrier the smaller they are); the up chain sums them, like the sum of blurs above.

Every level has a quarter of the pixels of the one above, so the whole chain costs about a third of one more pass at the first level's size:

≈ 4.3reads per screen pixel, going down13 taps × (1/4 + 1/16 + … + 1/4096)
≈ 3.3reads per screen pixel, going up10 taps × (1/4 + 1/16 + … + 1/1024)
≈ 540reads per pixel for one blur of radius 135 pxa separable Gaussian: 2 × 271 taps

4Going down: the 13-tap filter

The simplest way to halve an image is the mean of each 2 × 2 block. For bloom it fails: a small bright light that moves by one pixel jumps from one block to the next, and its glow blinks. A wider filter that overlaps the blocks keeps it steady. The filter of Jimenez (Call of Duty: Advanced Warfare, 2014) reads 13 bilinear taps. The new texel sits on a corner between 2 × 2 old texels, and so does every tap, so each tap is already the mean of 2 × 2 texels. Together they cover 6 × 6:

the inner box (taps j, k, l, m): weight 0.5, the new texel's own area four corner boxes of 4 taps each, sharing e: weight 0.125 each
a b c d e f g h i j k l m 6 × 6 source texels; the new texel covers the middle 2 × 2 weights inner box (j + k + l + m) / 4 × 0.5 4 corners (a + b + d + e) / 4 × 0.125 (b + c + e + f) / 4, … × 0.125 Per tap: e 1/8, j k l m 1/8, b d f h 1/16, a c g i 1/32. Per texel along a row: 1, 3, 4, 4, 3, 1 sixteenths. The weights add up to 1, so a flat image stays flat.
13 reads instead of 36, because bilinear filtering averages each 2 × 2 for free. The overlap with the neighbours' footprints is what stops the blinking.

The first level: the Karis average

A scene can hold a single pixel that is far brighter than the rest: a sparkle of specular light on a tiny bump, called a firefly. The blur spreads it into a large, bright blob, and as the pixel comes and goes from frame to frame, the blob flashes. The first down step therefore weights each of the five boxes by 1 / (1 + its brightness) (Karis 2013), so one box with a firefly cannot outweigh the others. Here, a firefly of 1000 on a background of 0.1:

125 31 in the inner box one texel later

Plain weights

The texel is 125, then 31 when the firefly moves by one source texel: the glow flashes.

1.19 0.26 in the inner box one texel later

Karis weights

The box with the firefly gets weight 0.5 / 251. The texel stays near the background, wherever the firefly is.

Bars on the same linear scale. The Karis average is only for the first step: the later levels hold no single bright pixels any more, and weighting them would darken the glow.

5Going up: the tent filter

Each up level starts from the down level of the same size and adds the level below, scaled up and softened by a 3 × 3 tent filter: nine bilinear taps, one texel of the smaller level apart. The tent smooths the blocky texels of the small image, so no square edges show.

upi = downi + tent(upi+1)

bloom = up1 / levels

tent
weights 1 2 1 / 2 4 2 / 1 2 1, divided by 16
upi+1
for the smallest level, downi+1: it has nothing below it
levels
how many levels the chain has: up1 holds that many blurred copies of the scene

6Mixing it in

The bloom is the scene, blurred at every size, with the scene's total light. Two ways to put it back:

threshold no glow glow

Threshold, then add

scene + k · blur(max(scene − t, 0)). Adds light, and a glow appears and vanishes as a value crosses t. Needs tuning per scene.

faint glow clear glow

Lerp, no threshold

lerp(scene, bloom, s). Every pixel gives s of its light to its glow and keeps the rest. The total stays the same; only bright light shows.

light bloom curve on HDR values, before the curve

Before the tone curve

Bloom must see values above 1. After the curve every light is at most 1, and every glow is the same.

A strength of about 0.04 looks natural: each pixel gives 4 % of its light to its glow. Try it on one row of pixels. The lab runs the chain of this page in one dimension:

the scene: a background of 0.05 and one bright pixel the bloom what the tone curve gets: lerp(scene, bloom, strength)
Raise the spot from 0 to 12 stops: at strength 0.04 the glow shows only for a very bright spot, and "light the bloom keeps" stays near 100 %. Then pick a source of 24 or 6 pixels: the chain stops early, and the glow gets smaller.

7Small images

A fixed number of levels assumes a big image. A 256 × 144 image halves 6 times to 4 × 2. A window made very small, or a source of 6 pixels, reaches 1 × 1 earlier, and halving 1 × 1 again only copies it. So the chain stops at the last level that is at least 2 × 2, and the last up step divides by the levels that ran. The first level always runs, so an image of any size, even 1 × 1, still gets a bloom image and never a level of zero size.

SourceLevels, each halvedLevels that run
1920 × 1080960 × 540, 480 × 270, … 30 × 166
256 × 144128 × 72, 64 × 36, 32 × 18, 16 × 9, 8 × 4, 4 × 26
100 × 3050 × 15, 25 × 7, 12 × 3, 6 × 1, 3 × 1, 1 × 13
3 × 31 × 1 at every level1

8Pitfalls

Every light glows the same
Bloom ran on tone-mapped values, which stop at 1. Run it on the HDR image, before the curve.
The whole image gets brighter and hazy
The bloom was added (scene + bloom) instead of mixed in. Use lerp, or keep the strength small.
Glows flash or blink as the camera moves
A 2 × 2 box down filter, or no Karis average on the first step. Use the 13-tap filter and the Karis weights.
Square blocks in the glow
The up chain uses point sampling, or skips the tent. Sample bilinearly and blur with the tent at every level.
The glow is a different size at another resolution
The levels go down to a fixed pixel size. Use a fixed number of halvings, so each level is the same share of the screen.
A glow pops in or out as a light gets brighter
A threshold. Without one, the glow grows smoothly with the light.

9In RASM

Renderer::addBloomPasses adds the chain to a render graph as 11 compute passes, after the scene passes. Each pass reads its inputs by sampling and writes one image, a storage render target; the graph does the barriers. The tone map then mixes the result in. With Renderer::render, one line turns it on, before the engine is made:

config.bloom = true;

The chain reads one value per pixel. Renderer::render gives it the view's HDR target after anti-aliasing (EngineConfig::antiAliasing): scene_hdr without it, its MSAA resolve scene_hdr_resolved (MSAA), or the TAA result scene_hdr_taa (TAA). The tone map reads the same target. A multisampled source logs an error and adds no pass: resolve it first with Renderer::addMsaaResolvePass.

compute passes of the graph transient storage targets the camera's setting
scene_hdr or its resolve Bloom Down 1 … 6: firstDownsampleMain, then downsampleMain down_1 down_2 down_3 down_4 down_5 down_6 up_5 up_4 up_3 up_2 up_1 ÷ levels Tone Map lerp(scene, bloom, strength), × exposure, AgX setCameraBloomStrength(camera, 0.04) read when the passes run; 0 dispatches nothing Bloom Up 5 … 1: upsampleMain Targets are named bloom_down_1 … bloom_down_6 and bloom_up_1 … bloom_up_5, all SCENE_COLOR_FORMAT, sized from the source.
The 11 passes run one after another on the graphics queue and share one GPU timer, Bloom. Before each pass the graph moves the target it writes to STORAGE and the targets it reads to SAMPLED; the targets are transient, so they share memory with the frame's other transient targets.

Images that compute passes write

A render graph pass that draws gets its target as an attachment. A compute pass has no attachments: it writes an image through a storage image descriptor, in the GENERAL layout, and other passes sample it later in the read-only layout. So a graph that lets compute passes write images must move each image between the two layouts, with barriers, as it does for attachments. RASM does it through the render target:

The pass declaresThe graph records before it
a compute pass: write(rt) of a target made with RenderTargetDesc::storageeach color of this frame slot → STORAGE (from STORAGE: a write-after-write barrier)
any pass: read(rt)each color → SAMPLED; nothing after a draw, whose endPass left it there
any pass: readPrevious(rt) of a storage history targeteach color of the last frame's copy → SAMPLED, from the STORAGE the frame before left
the first pass of a transient targetaliasingBarrier, which also covers compute storage writes; its colors move to STORAGE from UNKNOWN, dropping the old bytes

A storage target is still a render target: it is sized from the window (swapchainScale) and resized with it, its memory is laid out with the graph's other transient targets, and its colors are in the bindless set for sampling. Each color also has a storage slot in binding 1 of the bindless set for the target's life, which Renderer::getRenderTargetStorageIndex returns. The shader writes it as gStorageImages[pc.target][uint3(id.xy, 0)].

Write-only, read by sampling

No pass reads and writes one image. Each reads its inputs by sampling and writes its output once per texel. That is why the up chain has its own targets instead of adding into the down ones. Every API can do this: WebGPU core can write an RGBA16F storage texture but not read and write one, and a backend without a storage format can run the same pass as a full-screen draw into the target.

Where RASM follows this page, and where it differs

PartThis pageRASM
Levelsa fixed number of halvingsBLOOM_LEVELS (6), from 1/2 to 1/64 of the source. A source sized from the window (swapchainScale) gives levels with swapchainScale / 2level; a fixed-size source gives getBloomLevelSize, its size halved, at least 1 × 1.
Small imagesstop at the last level of at least 2 × 2The same, with BLOOM_MIN_LEVEL_SIZE (2): every frame, Bloom Down 1 reads the levels' sizes and getBloomLevelCount picks the levels that run, so a resize needs no new graph; the other passes read its count. The passes of the other levels dispatch nothing.
Down filter13 taps, Karis on the first stepThe same: firstDownsampleMain (Karis) and downsampleMain in shaders/passes/bloom.slang.
Up filtertent, add, divide by the levelsThe same: upsampleMain; Bloom Up 1 multiplies by 1 / levels (BloomPushConstants::scale).
Mixlerp before the curve, no thresholdTone Map samples the bloom (half size, bilinear) and draws lerp(scene, bloom, Camera::bloomStrength) before the exposure and AgX. Strength 0.04 by default, from 0 to 1 (Renderer::setCameraBloomStrength).
Formata float formatSCENE_COLOR_FORMAT (RGBA16F) for every level: a storage format on every target backend.
Turning it off–Strength 0, or while a debug view is shown (Renderer::setDebugView): the passes dispatch nothing (the graph still records their barriers) and the tone map does not sample (Renderer::bloomRuns, one rule for both). To drop the passes and their memory, build the graph without them. EngineConfig::bloom is off by default, so an engine update does not change how an application looks.

What it costs

≈ 0.2 msat 1280 × 7200.10 to 0.27 ms from run to run; Bloom timer, examples/09_gltf_materials, RTX 3060, Release
0.37 msat 1920 × 1080--render-size 1920 1080
0.75 msat 3840 × 2160--render-size 3840 2160
13.4 MiBthe 11 targets at 1080p, each in its own memoryRGBA16F, with the memory alignment
0 MiBmore in the example at 1080p, aliasedthe levels reuse the shadow map's memory, free by then; +27.2 MiB at 4K
11storage slotsof MAX_BINDLESS_STORAGE_IMAGES (1024), one per target, given back with the graph

Where it lives in code

WhatWhere
The passesRenderer::addBloomPasses in src/renderer/bloom.cpp: the 11 targets and passes, Pass::timerName = Bloom; Renderer::createBloomPipelines, made on the first pass that runs, retried on a shader error
The level rulegetBloomLevelSize, getBloomLevelCount, BLOOM_LEVELS, BLOOM_MIN_LEVEL_SIZE in src/renderer/bloom.h; tested in tests/bloomTests.cpp
The filtersshaders/passes/bloom.slang: downsample, getKarisWeight, upsampleMain; push constants gfx::BloomPushConstants in src/renderer/uberMaterialTypes.h, checked by validateRasmStructs
The mixfragMain in shaders/passes/toneMap.slang; Renderer::addToneMapPass in src/renderer/toneMap.cpp (its bloom argument)
Storage targetsRenderTargetDesc::storage in include/rasm/render/resource.h; slots in RenderContext::createRenderTarget and createTransientRenderTarget, src/rhi/renderContext.cpp; the transitions in RenderGraph::execute, src/renderer/renderGraph.cpp
StrengthCamera::bloomStrength in include/rasm/camera.h; Renderer::setCameraBloomStrength, Renderer::getCameraBloomStrength in src/scene/camera.cpp
Examplesexamples/09_gltf_materials/main.cpp: bloom on, --no-bloom to compare, --bloom-strength for setCameraBloomStrength; ViewGraph::render with EngineConfig::bloom
Keep these true
  • Bloom runs on HDR light, before Tone Map, and every level is a float format.
  • A pass never reads and writes one image: each up level has its own target.
  • Every level is at least 1 × 1, and the chain stops at the last level of at least 2 × 2; a change to that rule changes tests/bloomTests.cpp and the lab (docs/assets/labs/bloom.js).
  • The bloom keeps the scene's light: the down weights add up to 1, the tent adds up to 1, and up 1 divides by the levels.
  • The C++ and Slang BloomPushConstants keep one name and layout, like ToneMapPushConstants.

Further reading