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.
Without bloom
The curve rolls off into white, so very different lights look alike. Nothing on screen says which one is the sun.
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:
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).
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:
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 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:
Plain weights
The texel is 125, then 31 when the firefly moves by one source texel: the glow flashes.
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, 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.
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.
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:
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.
| Source | Levels, each halved | Levels that run |
|---|---|---|
| 1920 × 1080 | 960 × 540, 480 × 270, … 30 × 16 | 6 |
| 256 × 144 | 128 × 72, 64 × 36, 32 × 18, 16 × 9, 8 × 4, 4 × 2 | 6 |
| 100 × 30 | 50 × 15, 25 × 7, 12 × 3, 6 × 1, 3 × 1, 1 × 1 | 3 |
| 3 × 3 | 1 × 1 at every level | 1 |
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. Uselerp, 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.
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 declares | The graph records before it |
|---|---|
a compute pass: write(rt) of a target made with RenderTargetDesc::storage | each 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 target | each color of the last frame's copy → SAMPLED, from the STORAGE the frame before left |
| the first pass of a transient target | aliasingBarrier, 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)].
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
| Part | This page | RASM |
|---|---|---|
| Levels | a fixed number of halvings | BLOOM_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 images | stop at the last level of at least 2 × 2 | The 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 filter | 13 taps, Karis on the first step | The same: firstDownsampleMain (Karis) and downsampleMain in shaders/passes/bloom.slang. |
| Up filter | tent, add, divide by the levels | The same: upsampleMain; Bloom Up 1 multiplies by 1 / levels (BloomPushConstants::scale). |
| Mix | lerp before the curve, no threshold | Tone 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). |
| Format | a float format | SCENE_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
Bloom timer, examples/09_gltf_materials, RTX 3060, ReleaseMAX_BINDLESS_STORAGE_IMAGES (1024), one per target, given back with the graphWhere it lives in code
| What | Where |
|---|---|
| The passes | Renderer::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 rule | getBloomLevelSize, getBloomLevelCount, BLOOM_LEVELS, BLOOM_MIN_LEVEL_SIZE in src/renderer/bloom.h; tested in tests/bloomTests.cpp |
| The filters | shaders/passes/bloom.slang: downsample, getKarisWeight, upsampleMain; push constants gfx::BloomPushConstants in src/renderer/uberMaterialTypes.h, checked by validateRasmStructs |
| The mix | fragMain in shaders/passes/toneMap.slang; Renderer::addToneMapPass in src/renderer/toneMap.cpp (its bloom argument) |
| Storage targets | RenderTargetDesc::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 |
| Strength | Camera::bloomStrength in include/rasm/camera.h; Renderer::setCameraBloomStrength, Renderer::getCameraBloomStrength in src/scene/camera.cpp |
| Examples | examples/09_gltf_materials/main.cpp: bloom on, --no-bloom to compare, --bloom-strength for setCameraBloomStrength; ViewGraph::render with EngineConfig::bloom |
- 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.cppand 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
BloomPushConstantskeep one name and layout, likeToneMapPushConstants.
Further reading
- Jorge Jimenez, "Next Generation Post Processing in Call of Duty: Advanced Warfare" (SIGGRAPH 2014, Advances in Real-Time Rendering): the 13-tap down filter, the tent up filter and the Karis average on the first step.
- Brian Karis, "Tone mapping" (graphicrants.blogspot.com, 2013): weighting by 1 / (1 + luma) against fireflies.
- Greg Spencer, Peter Shirley, Kurt Zimmerman, Donald Greenberg, "Physically-Based Glare Effects for Digital Images", SIGGRAPH 1995: how the eye spreads bright light.
- Masaki Kawase, "Frame Buffer Postprocessing Effects in DOUBLE-S.T.E.A.L" (GDC 2003): blurs from a chain of smaller images.