Log inGet started
Package

bloom.shader

Newest version

This package holds more than this page lists. See all of its files in its world.

README.md

bloom post-process shader

The light a bright surface spills into the pixels around it. A half-resolution bright pass extracts what the picture shows above threshold, a pyramid of downsample passes carries it down to 1/256 of the view, and a chain of upsample passes carries it back up, blending each octave into the one above it. composite adds the result to the scene. tools.use("pp", "add", "glow", "bloom") registers it — the shader declares space: light, so the effect takes the scene layer: the rendered picture before the UI is drawn, over the scene's own light in a float target, which is where a bloom belongs.

What blooms is judged in exposed light: the scene's light times the exposure in force (zero_display_exposure(), the gain __display__ applies before its curve). The same scene shown brighter or darker blooms the same highlights, and a scene lit four times brighter and exposed four times lower is one picture with one glow. Below the threshold nothing blooms, so a diffuse surface the picture shows under its highlights lifts no shadow. Above it the glow is carried in the scene's own light and grows in proportion to the light a source stands above the threshold with, so the brightest things in a frame keep their order through their glow.

One look at every resolution

The halo is measured in the frame's own units: it covers the same share of the picture at 360 lines, 1080 and 4K, on a landscape, a square and a portrait frame, so a screenshot, a phone held either way and a monitor show one glow. Every pass of the pyramid below the bright pass declares lines: 1080, so it is drawn at the size it has in a 1080-line view of the frame's shape: its finest level, up0, is 540 texels tall and each level below it half the one above, at every output size. Octave n is a texel 2^n lines of a 1080-line frame, the same share of the height of a frame of any size and shape (zero_view_size() names the frame's size to every pass), and every frame holds the same images of the same octaves. A frame under 540 lines draws the levels that would outgrow it at its own size, and those levels pass the image they read through unchanged, so its pyramid starts at the finest octave its own texels resolve.

The bright pass declares maxLines: 1080. On a frame of 1080 lines or fewer it is drawn at half the frame's own size, so each of its texels is the 2x2 block of the frame's pixels it covers and every pixel's light is taken once at the cost of four reads; on a taller frame it is 540 texels tall, each texel the block of the frame's pixels it covers (four on a side at 2160 lines). The image down1 reads is therefore never more than twice down1's size, so its taps reach every texel of it and a small source glows alike wherever it stands on the frame. down1 spaces its taps in halves of its own texel, the spacing a 1080-line frame's first level has on any frame, and up0 reads the bright image at its own texel. A frame under 1080 lines therefore spends less on the bright pass than a 1080-line frame, and keeps every octave of the pyramid.

The weights follow one curve over that size: falling by scatter per octave from octave 0.5 out to levels - 0.5, with the last octave, out to levels + 0.5, carrying what the fall leaves. The light the curve holds between two octaves is shared between them by how near it lies to each, the way a filter between two mip levels blends them, the finest octave taking every share finer than itself; the shares sum to the whole of the curve, so the bloomed image holds exactly the light the bright pass took, at every size.

The deepest octave a frame keeps is the last whose image is 8 texels tall, octave 7 (8 of a 1080-line view's rows, an eighth of the frame's height): the floor is met on the frame's height, the axis the octaves are measured on, so a portrait frame keeps every octave a landscape frame of its height keeps.

Measured on one disc a 36th of the frame's height across, under threshold = 1 and intensity = 4 with every other property at its default (the mean light of the ring 0.02 of the height wide):

capturering at 0.05 hring at 0.10 hring at 0.18 hring at 0.25 h
640x3600.30030.05750.007340.00101
1280x7200.30460.05850.007490.00103
1600x9000.30540.05860.007500.00103
1920x10800.30570.05870.007510.00103
3840x21600.30730.05890.007550.00104
1280x12800.30430.05870.007450.00102
720x12800.30700.05960.007850.00111

Every frame of one shape reads within 3% of the 1080-line frame at every ring, and so do levels = 3, levels = 8 and scatter = 1. A portrait frame reads its inner halo as a landscape frame of its height does; its outer rings come within a few octave texels of its sides, where the light an octave carries past the frame's edge is held at the edge rather than lost, and read up to 9% brighter at 720x1280.

Passes

PassSize (rows)What it does
brighthalf the frame's, at most 540Extracts what each texel of the block of the scene it covers (2x2 up to 1080 lines, a third texel in part on a frame of an odd size, 4x4 at 2160) shows above threshold, soft-kneed by softKnee, and averages them weighed by the share of each the block covers. A texel holding more light than the half-float targets carry (an infinity a shader overflowed to) is taken at the largest light they hold, and one holding no number is taken as none, so one texel spreads glow and never a value that blacks out the octaves it reaches.
down1…down7270 … 4Each halves the previous octave's resolution with a 13-tap filter, its taps spaced in halves of its own texel; the first of them that reads the bright image also Karis-averages its five groups, each weighed against the plain average of the five, so a group standing far above its neighbours (a firefly) weighs little while groups alike average plainly at every brightness.
up6…up08 … 540Each carries the smaller octave up with a 3x3 tent and blends it into the next one, by the share of the halo the octaves below carry against the share this one adds to them. up0 blends into the bright image.
composite(view size)Adds the bloomed image to the scene, scaled by intensity.

Properties

PropertyDefaultRangeMeaning
threshold4.00–16The exposed brightness (light times the exposure in force) the bright pass extracts above. 1 is what a linear display shows as white; the default 4 sits two stops over it, where the display's curve is in its shoulder, so diffuse surfaces stay out of the glow and emitters, sun glints and the sky by the sun bloom.
softKnee0.50–1How wide the roll-in is around threshold, as a share of it, so a surface crossing it brightens rather than pops: at the defaults the roll-in runs from 2 to 6.
intensity0.60–4How much of the bloomed image the composite pass adds back to the scene.
scatter0.70–1How much of the glow each octave carries out to the next one: each octave out carries scatter times the light of the one inside it, so a higher scatter carries the halo further.
levels61–8How far the halo reaches: its widest octave is a texel 2^levels lines of a 1080-line frame tall (64 lines at 6, about a seventeenth of the height), the same share of a frame of any size and shape. A fraction reaches part of the way into the next octave; past 7 the deepest octave, an eighth of the height, carries what reaches further. At 1 the glow stays within a line or two of a 1080-line frame around its source.

The weights and the 8-texel floor an octave's height must clear are both read per pass, not baked into the pass list: an octave past either one returns the larger image it was handed unchanged, so a slider drag or a resize changes what the sixteen passes DO and never how many there are.

Fifteen scratch targets in the scene format, the largest 540 texels tall and as wide as the frame's shape makes it: about 11 MB on a 16:9 frame 1080 lines tall or taller, and less on a smaller one.