Drawing

Weighted blended transparency

See-through surfaces are usually sorted and drawn back to front (transparency). Sorting works for a few panes of glass. It fails for dense data: nested iso-surfaces, a stack of volume slices in one mesh, thousands of see-through arrows. Weighted blended order-independent transparency (McGuire and Bavoil, 2013) draws such surfaces in any order, at a fixed cost per pixel, with no popping. This page explains how, and the last part shows RASM's version.

In short

  • The over operator mixes two things: how much of the background a stack of layers hides (its coverage), and what color they show.
  • The coverage is 1 − product of (1 − alpha). A product has no order, so the coverage is exact in any order.
  • The color is replaced by a weighted mean of the layers' colors: sum of color × alpha × weight, divided by sum of alpha × weight. Sums have no order either.
  • The weight makes near layers count more. It must fall with depth at any scene scale, so it is measured inside the see-through content's own depth range, not in metres.
  • Two targets hold the sums; a composite pass divides them and lays the mean over the background. Cost: one draw of each surface, two blended writes per fragment, one full-screen pass.
  • It is exact where layers are thin or alike, and approximate where a thick layer should hide the ones behind it. Things that need an exact order (labels, UI) stay sorted.

1Why sorting fails for dense data

Sorting puts each draw in order by one depth, usually its center. Inside a draw, triangles keep the order of the index buffer. Three cases break it:

18 one mesh: drawn 1 to 8

Layers inside one draw

Volume slices or an iso-surface in one mesh: right from one side, wrong from the other.

one center, three draws

Nested objects

A housing around its parts: the centers are equal, so the order is arbitrary.

crossing: no order is right

Crossing surfaces

Each is in front at one end. The order flips as the camera moves: popping.

Sorting per triangle, or per pixel (an A-buffer, depth peeling), is exact, but its cost grows with the number of layers in a pixel. Weighted blending accepts an approximate color to get a cost that does not grow with the layers' order.

2Split over into coverage and color

For layers 1 to n over a background B, with premultiplied colors Ci and alphas ai:

c = 1 − (1 − a1) (1 − a2) … (1 − an)

mean = Σ Ci wi / Σ ai wi

result = mean · c + B · (1 − c)

c
the coverage: how much of the background the layers hide. This is exactly what over gives, in any order.
Ci
the layer's color times its alpha (premultiplied), its light included
wi
the layer's weight: larger for nearer layers (section 4)
mean
the weighted mean color of the layers; with one layer it is that layer's own color

All three parts are sums or products, so the layers can be added in any order. Only the mean is approximate: over gives a near layer more say in the color than a far one by exactly how much it hides. The weight guesses that from depth. Drag the layers and change their alpha:

Move the three layers and change their alpha. The weights come from each layer's place in the view's see-through range (the green bracket). The coverage is always exact. Close layers get a color close to the exact one; layers far apart, or thick, show where the mean differs.

3Two targets, two passes

The sums live in two textures the size of the view, cleared to 0. Each see-through fragment adds to them with the blend unit; nothing reads them until every layer is drawn.

pass 1: accumulate (any order) fragment i C, a, depth → w opaque depth: test, no write sums rgb += C·w, a += a·w coverage c = a + c·(1 − a) pass 2: composite one triangle mean · c over the scene scene color: blend premultiplied The coverage is kept as c, not as 1 − c, so both targets clear to 0 with one clear value.
The two passes. The coverage blend, c = a + c (1 − a), is the premultiplied over of alpha alone, so its product has the same order-free result as 1 − Π(1 − a).
TargetHoldsSource factorDestination factor
sums (RGBA, 16-bit float)Σ C·w in rgb, Σ a·w in alphaoneone (add)
coverage (8-bit)coneone − source alpha
with TAA: motion and coverage (RGBA, 16-bit float)Σ motion · a · w in rg, c in alphaonerg: one (add); alpha: one − source alpha

The targets blend differently, so the device must allow a different blend state per color target (Vulkan's independentBlend, present on desktop and mobile GPUs). The sums need a float format: they pass 1, and a fixed-point format would lose small weights. The coverage stays between 0 and 1, so 8 bits hold it, as in the paper.

Every target costs

Each fragment blends into every target, so the cost grows with the number of targets more than with their bytes. Measured on an RTX 3060 in a dense scene: the sums alone cost about what sorted blending costs; adding an 8-bit coverage target adds a third; a third target for motion doubled it. So with TAA the motion shares the coverage's target (motion in rg, coverage in alpha, one blend state each), and the cost stays at two targets.

4A weight that works at any scale

The paper's weights put fixed distances in metres into the formula, for example 10 / (10−5 + (z/5)2 + (z/200)6), kept between 0.01 and 3000. That fits a room a few metres deep. A map seen from 5 km, or a part seen from 2 cm, gets a flat weight: every layer counts the same, and far layers bleed through near ones.

The fix is to measure depth where the content is. Each frame, culling finds the nearest and the farthest depth of the see-through draws in view. A fragment's place is where its depth lies in that range, 0 at the nearest and 1 at the farthest, measured the way the projection measures size:

perspective: place = log(z / znear) / log(zfar / znear)

orthographic: place = (z − znear) / (zfar − znear)

w = 2−5·place   (exp2(−5 · place))

z
the fragment's view depth: its distance along the camera's axis, not the depth buffer's value, so reversed-Z or an infinite far plane do not matter
log
with perspective, a layer twice as far looks half as big: equal steps per doubling of distance
w
halves every fifth of the range: 1 at the nearest, 1/32 at the farthest
Move the scene from 4 m to 40 km away. The weight with fixed metres goes flat; the weight from the place in the range does not change. Try the orthographic projection too.

Why 1/32? Over a test set of random stacks (2 to 10 layers, alpha 0.05 to 0.7, spread over part of the range), the error against the exact over is smallest for a weight that falls by about 2−5 to 2−8 across the range; a flat weight is 40 % worse. Thin layers (alpha up to 0.3) like a gentler fall, about 2−3, thick ones (0.4 to 0.8) a steeper one: 2−5 is the middle. A weight that also grows with alpha, as in the paper, did not help on this set.

5Keeping the weights steady

The range comes from the draws in view, so it moves when an object enters or leaves the view. Every weight then changes, and the color of the whole see-through layer shifts. With TAA that looks like a flicker. Two rules keep it steady:

4 steps 2 0 a far object enters it leaves small moves: no change time → measured far end · the far end the weights use
The measured far end (amber) jumps. The one the weights use (green) snaps to steps with hysteresis, then moves toward them over a quarter second.

Fragments outside the range (an object that just came in) clamp their place to 0 or 1, so they still get a valid weight while the range catches up.

6MSAA and TAA

sums persample

MSAA: per sample

The targets have the scene's samples, and the composite runs once per sample (sample-rate shading). An opaque edge in front of the see-through surface stays sharp and clean.

4 samples: 4 × the target memory, 4 composite runs a pixel

one meanfor all

MSAA: per pixel

One composite per pixel from the samples' mean: cheaper, but the opaque edge's samples get the see-through color too, a faint halo. A fallback where sample shading is missing.

motion = mean · c + behind · (1 − c)

TAA: motion

The coverage's target, 16-bit with TAA, also sums motion × alpha × weight in rg. The composite lays the mean motion over the motion behind by the coverage, so a dense moving layer is followed, and a faint one leaves the background's motion.

7Mixing with sorted and additive draws

Weighted surfaces are composited once, as one layer. Sorted see-through draws are drawn after that, over the composite. So they cannot go behind a weighted surface:

weighted sorted drawn on top

Sorted behind weighted

The sorted pane is behind, but it is drawn over the weighted layer.

sorted label weighted on top

Sorted in front of weighted

Labels, UI and overlays in front of the data come out right.

Use one method for see-through things that overlap each other

Make all of a dataset's see-through surfaces weighted, and keep sorted blending for what must be exact and lies in front: labels, text, map layers with a draw order, UI.

Additive surfaces (glows, density or X-ray views) add their light and hide nothing. Addition has no order, so they need no targets at all: they are drawn straight into the scene, in any order, after the weighted composite. A pure-additive surface has no coverage, so it would vanish in the weighted mean (0 / 0); it belongs in the additive method instead. Emissive light on a see-through surface is part of its color and takes part in the mean.

8Pitfalls

A bright far layer glows through a dark near one
The mean is not over: a thick near layer should hide more. Steeper weights help; for a nearly opaque layer, use alpha 1 (opaque) or sorted blending.
The see-through layer's color shifts when an object enters the view
The weights' range jumped. Keep the range with hysteresis and smooth it (section 5).
Far layers bleed through at a large scale
The weight has fixed metres in it and is flat at that distance. Measure the place in the content's own range.
Black or white sparkles
A sum overflowed or underflowed its float format: keep weights between about 1/1000 and 1, with colors as bright as the scene's own target holds.
A see-through surface vanishes where an additive one is
An alpha of 0 adds nothing to the coverage: draw light-only surfaces additive.
A halo along opaque edges with MSAA
The composite ran per pixel. Run it per sample.

9In RASM

A BLEND material picks its method with Material::blendMethod: BlendMethod::SORTED (the default), WEIGHTED or ADDITIVE. A GLYPHS material with AlphaMode::BLEND is always weighted, as glyphs cannot be sorted; a VECTOR one is always sorted, by its drawOrder.

rasm::Material material = engine.getMaterial(handle);
material.alphaMode = rasm::AlphaMode::BLEND;
material.blendMethod = rasm::BlendMethod::WEIGHTED;
engine.updateMaterial(handle, material);
Cull 2range of weighted Scene 2late draws, glyphs, points, sky, then … Transparentgrid · composite · additive · sorted · ghosts Weighted (in Scene 2) weighted run + weighted glyphs sums RGBA16F coverage R8 TAA: motion rg,coverage a (16F) the composite reads them The weighted target is the view's own, made the first frame the view has weighted draws: no memory and no pass without them.
Where the weighted draws go in a shaded view's passes. The draw pass stops its rendering, draws the weighted surfaces into the view's weighted target with the scene's depth (tested, not written), and goes on.

Where RASM follows the page, and where it differs

PartThis pageRASM
Choiceper surfacePer material: Material::blendMethod. The draw list has a run per method after the point run (getRunKey): weighted, additive, sorted last. MAX_DRAW_RUNS is 10.
Targetssums, coverage, motionWEIGHTED_ACCUM_FORMAT (RGBA16F) and WEIGHTED_COVERAGE_FORMAT (R8), or with TAA WEIGHTED_MOTION_FORMAT (RGBA16F: the motion sums in rg, the coverage in alpha), in one render target per view (getWeightedTargetDesc), with the scene target's size and samples. Memory at 1920 × 1080: 19 MB, 33 MB with TAA, 75 MB with 4× MSAA.
Blend statesadd; over for coverageBlendMode::WEIGHTED: color 1 premultiplied, the others add; BlendMode::WEIGHTED_MOTION (TAA): color 1's rg add and its alpha is premultiplied (VulkanDevice::createGraphicsPipeline). Without RenderContext::hasIndependentBlend weighted and additive materials are drawn sorted, with a warning.
Weightplace in the content's rangegetWeight and getWeightedPlace in shaders/lib/weightedBlend.slang; WEIGHT_HALVINGS is 5. The cull writes the nearest and farthest view depth of the kept weighted draws and weighted glyph chunks (their bounding spheres, getSphereDepths) to two words of the view (SceneData::weightedRangePtr, with getDepthBits).
Steady weightssnap, hysteresis, smoothingstepWeightedRange in src/renderer/weightedBlend.cpp: steps of √2 (perspective) or an eighth of the range (orthographic), two steps of hysteresis, WEIGHTED_RANGE_SECONDS 0.25. It reads the words the cull wrote two frames before (the frame slot's), so the first two frames use the cull's own words of the frame.
Accumulatea passRenderer::drawWeighted, at the end of the view's last draw pass: its rendering stops, the weighted run (rasm.slang with RASM_ALPHA_BLEND and RASM_WEIGHTED) and the weighted glyphs draw into the weighted target with the scene's depth, and the pass goes on. GPU timer Weighted.
Compositea full-screen passRenderer::drawWeightedComposite in the Transparent pass, after the grid (shaders/passes/weightedComposite.slang). With MSAA per sample (RASM_PER_SAMPLE), or the pixel's mean where RenderContext::hasSampleRateShading is false. GPU timer OIT Composite.
Additivestraight into the sceneRenderer::drawAdditive after the composite: BlendMode::ADDITIVE_COLOR adds the light, and lays its motion over the motion behind by its alpha. An additive surface casts no shadow; a weighted one casts a hashed-alpha shadow like a sorted one.
Glyphs—The glyph cull puts a weighted chunk's draw commands after the opaque chunks' room (appendGlyphChunk, GlyphCullHeader::weightedCommandCount), so each list is one indirect draw.
Picking—Weighted and additive runs are picked with a pipeline that writes depth, so the nearest surface whose alpha is at least PickDesc::minAlpha is hit; a weighted glyph tests its alpha too.
Debug views—Show the weighted and additive runs unsorted with the sorted run's debug pipeline: a mean of normals would mean nothing. Weighted glyphs are drawn shaded, as the other glyphs (D52).

What it costs

Measured in examples/31_oit without its arrows (Release, RTX 3060, 1920 × 1080, a perspective TAA view and an orthographic 4× MSAA view, the last 1500 of 3000 frames). The same surfaces, weighted against sorted:

0.59 msthe Weighted draws of both viewssorted: about 0.48 ms of draws + 0.04 ms of sort
0.09 msthe composite, per sample under MSAA0.06 ms with each pixel's mean
1.45 msGPU frame, weightedsorted: 1.28 ms
0.63 msweighted draws with both views at one sample, the camera nearersorted: 0.53 ms of draws
2.1×weighted over sorted in a 4× MSAA perspective view2.13 against 0.99 ms, the camera nearer: each sample blends into two targets
≤ 1/255the weighted layer's change a frame while a far draw flies in and out--range-test; up to 6/255 in one frame with the raw range

The weighted cost does not grow with how the layers overlap, and it removes the order errors: at one sample it costs a fifth more than sorted draws. The extra cost is the second target: per sample under MSAA it doubles the accumulation. A scene without weighted draws pays nothing: 05_city, 20_views, 25_vector_map, 26_picking, 27_cad_views and 30_glyphs take the same GPU time as before within ±1 %.

Where it lives in code

WhatWhere
The method of a materialBlendMethod, Material::blendMethod in include/rasm/material.h; C: rasm_blend_method, rasm_material_desc (1.15)
Runs by methodgetBlendMethod, getRunKey, Renderer::updateDrawModes, Renderer::rebuildDrawList in src/renderer/gpuScene.cpp
Targets, pipelines, the rangeRenderer::prepareWeightedBlend in src/renderer/weightedBlend.cpp
Accumulate, composite, additiveRenderer::drawWeighted, drawWeightedComposite, drawAdditive in src/renderer/weightedBlend.cpp; toWeightedOutput in shaders/passes/rasm.slang
The range in the cullcountMain in shaders/passes/cull.slang
Teststests/weightedBlendTests.cpp (the depth words, the range's snapping, hysteresis and smoothing); image tests weighted, weighted_msaa4, weighted_taa, weighted_ortho and far_weighted in tests/image/imageTests.cpp (nested shells, slices in one mesh, see-through arrows, an additive glow, a sorted pane, picks by minAlpha)
An exampleexamples/31_oit: nested iso-surfaces, 48 slices in one mesh, a see-through assembly and see-through arrows; --sorted to compare, --additive for an X-ray view of the slices
Keep these true

The weighted, additive and sorted runs are the last three runs, in that order, each with cull mode NONE; the early cull keeps none of them. Nothing but drawWeighted and the composite touches a view's weighted target. A pick's SceneData has no weightedRangePtr: the range is the view's to measure.

Further reading