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:
Layers inside one draw
Volume slices or an iso-surface in one mesh: right from one side, wrong from the other.
Nested objects
A housing around its parts: the centers are equal, so the order is arbitrary.
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:
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.
| Target | Holds | Source factor | Destination factor |
|---|---|---|---|
| sums (RGBA, 16-bit float) | Σ C·w in rgb, Σ a·w in alpha | one | one (add) |
| coverage (8-bit) | c | one | one − source alpha |
| with TAA: motion and coverage (RGBA, 16-bit float) | Σ motion · a · w in rg, c in alpha | one | rg: 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.
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
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:
- Snap with hysteresis. Each end snaps to a step: √2 in depth with perspective, an eighth of the range orthographic. It moves out at once when the content passes it, and in only when the content is two steps inside. An arrow culled at the edge of the view, or a part that moves a little, changes nothing.
- Smooth. The range the weights use moves toward the snapped one with a time constant of 0.25 s, in the same space as the place (log with perspective). A big change, a far object coming into view, becomes a fade of a quarter second instead of a jump.
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
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
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.
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:
Sorted behind weighted
The sorted pane is behind, but it is drawn over the weighted layer.
Sorted in front of weighted
Labels, UI and overlays in front of the data come out right.
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);
Where RASM follows the page, and where it differs
| Part | This page | RASM |
|---|---|---|
| Choice | per surface | Per 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. |
| Targets | sums, coverage, motion | WEIGHTED_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 states | add; over for coverage | BlendMode::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. |
| Weight | place in the content's range | getWeight 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 weights | snap, hysteresis, smoothing | stepWeightedRange 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. |
| Accumulate | a pass | Renderer::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. |
| Composite | a full-screen pass | Renderer::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. |
| Additive | straight into the scene | Renderer::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:
Weighted draws of both viewssorted: about 0.48 ms of draws + 0.04 ms of sortThe 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
| What | Where |
|---|---|
| The method of a material | BlendMethod, Material::blendMethod in include/rasm/material.h; C: rasm_blend_method, rasm_material_desc (1.15) |
| Runs by method | getBlendMethod, getRunKey, Renderer::updateDrawModes, Renderer::rebuildDrawList in src/renderer/gpuScene.cpp |
| Targets, pipelines, the range | Renderer::prepareWeightedBlend in src/renderer/weightedBlend.cpp |
| Accumulate, composite, additive | Renderer::drawWeighted, drawWeightedComposite, drawAdditive in src/renderer/weightedBlend.cpp; toWeightedOutput in shaders/passes/rasm.slang |
| The range in the cull | countMain in shaders/passes/cull.slang |
| Tests | tests/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 example | examples/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 |
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
- Morgan McGuire and Louis Bavoil, Weighted Blended Order-Independent Transparency, Journal of Computer Graphics Techniques 2(2), 2013.
- Morgan McGuire, Implementing Weighted, Blended Order-Independent Transparency, 2015.
- Thomas Porter and Tom Duff, Compositing Digital Images, SIGGRAPH 1984.
- Louis Bavoil and Kevin Myers, Order Independent Transparency with Dual Depth Peeling, NVIDIA, 2008: the exact alternative whose cost grows with the layers.