Drawing
Transparency and alpha modes
Most surfaces hide what is behind them. A leaf has holes, a window lets light through, smoke is thin. A renderer marks such surfaces with alpha, a fourth number next to red, green and blue, and draws them in a different way. This page explains the two ways, cut-out and see-through, why see-through surfaces must be drawn last and from back to front, and how they cast shadows. The last part shows how RASM does it.
In short
- Alpha is how much of a surface is there: 1 solid, 0 nothing. glTF has three alpha modes: opaque (alpha ignored), mask (cut-out) and blend (see-through).
- A cut-out throws away the pixels whose alpha is below a cutoff (discard). Everything else is a normal solid surface. A shader with discard can lose the early depth test, so keep it out of the opaque pipeline.
- A see-through surface mixes its color with what is behind it: the over operator. With premultiplied alpha the mix is one blend state.
- Over needs to know what is behind, so see-through surfaces are drawn after the solid ones, back to front, with the depth test on and the depth write off.
- Sorting whole objects is fast but not exact: two objects that cross, or one object that overlaps itself, can still mix in the wrong order.
- In a shadow map, a cut-out keeps its cutoff. A see-through surface can use hashed alpha: a fixed noise threshold, so the share of texels that cast follows alpha and the shadow filter gives a light shadow for light glass.
1Alpha and the three modes
Alpha comes from the material: a factor, a texture's fourth channel, and sometimes a vertex color, multiplied together. What a renderer does with it depends on the mode.
Opaque
Alpha is ignored. The surface hides everything behind it. Most surfaces.
Mask (cut-out)
Each pixel is all or nothing. Leaves, fences, chains, grass. It still writes depth, so it is drawn with the solid surfaces.
Blend (see-through)
The surface and what is behind it are mixed. Glass, water, smoke, ghosts, fading UI. It needs order.
2Cut-outs
The fragment shader computes alpha and, when it is below the cutoff, throws the pixel away with discard. No color and no depth are written for it. Everything else is drawn like a solid surface, in any order, with the depth buffer sorting it out.
One shader with discard for all
A shader that can discard may decide that a pixel writes no depth, so many GPUs test depth only after it runs. Solid surfaces lose the early test too.
A pipeline per mode
The solid pipeline has no discard and keeps the early test. Only cut-out draws pay. Draw the solid ones first, so the cut-outs are tested against them.
Alpha-to-coverage needs more than one sample
With MSAA, each pixel has several samples, and a pipeline can turn alpha into a coverage mask: alpha 0.5 covers half of the samples. The resolve then averages them, which gives a soft, anti-aliased edge instead of a staircase. With one sample per pixel there is nothing to average: alpha-to-coverage becomes a fixed cut at 0.5 and ignores the material's cutoff. So without MSAA, a cut-out is a discard. With MSAA, sharpen alpha around the cutoff, so the edge keeps its place and stays one pixel wide:
alpha′ = (alpha − cutoff) / fwidth(alpha) + 0.5
- fwidth(alpha)
- How much alpha changes from one pixel to the next on screen.
- alpha′
- 0.5 exactly at the cutoff, and 0 or 1 one pixel away from it; the coverage mask follows.
- Leaves and fences thin out and vanish in the distance
- The small mips average a texel's alpha with the empty texels around it, and more and more of them fall under the cutoff. Scale each mip's alpha so the share of texels above the cutoff stays that of the full-size image (coverage-preserving mips), or sharpen alpha as above.
- A cut-out's edge picks a wrong mip, or a driver crashes there
- A
discardthat ends the pixel's invocation (SPIR-VOpKill) takes it out of its 2×2 quad, and every texture sample after it needs that quad for its derivatives (mipmaps and sampling). Many GPUs keep going; the result is undefined. Discard by demoting the pixel to a helper (OpDemoteToHelperInvocation, core in Vulkan 1.3): it writes nothing, but still runs for its neighbors.
3Blending: the over operator
A see-through surface with color C and alpha a, in front of a color B already in the image, gives:
result = C · a + B · (1 − a)
- C · a
- The light the surface itself sends: its color, as much as it is there.
- B · (1 − a)
- The light from behind that gets through.
The GPU does this in its blend unit, after the fragment shader: the shader's output is the source, the image already in the target is the destination, and a blend state says how to mix them.
Straight alpha
The shader outputs the color and alpha apart; the blend unit multiplies.
Premultiplied alpha
The shader multiplies first. One rule serves color and alpha, filtering and mips stay right, and light that should not dim with alpha (a glow, a reflection) can simply be added.
4Order, and the depth buffer
Over is not commutative: the surface in front must be mixed in last. Two panes, a red one in front of a blue one, give a reddish purple. Drawn the other way round, the blue pane is mixed over the red one, and the result is bluish, as if the blue pane were in front.
So a renderer draws see-through surfaces in a separate step:
- Solid and cut-outdrawn first, in any order; they write depth
- Skywhere nothing was drawn
- See-through, back to frontdepth test on, so solid surfaces in front hide them; depth write off, so one pane does not hide the pane behind it
Move the panes, then draw them in list order or sorted:
Depth write on
If the near pane is drawn first (sorting is never perfect), it writes depth and the far pane fails the test behind it: a hole where glass should be.
Test on, write off
Each pane is mixed where it is not behind a solid surface, and no pane removes another.
5How exact is the order?
Sorting needs a key per thing drawn. The cheapest is one key per object: the depth of its bounding sphere's center from the camera. That is enough for separate objects, and wrong in a few cases.
Separate objects
Sort by the bounds' center: right.
One key per object.
Crossing objects
No order of whole objects is right everywhere. Split them, or accept the error.
Needs finer keys or OIT.
One object over itself
A closed glass object overlaps itself; its triangles come in index order. Cull its back faces, or draw it twice (back faces, then front faces).
Per triangle order.
Methods like weighted blended OIT, depth peeling or per-pixel lists need no sort at all: they collect every layer of a pixel, or an approximation of them, and mix them later. They cost memory or accuracy, and suit dense data (point clouds, particles, CAD). Sorted blending is still the usual answer for a game or a map with a few panes and sprites.
6Shadows of see-through surfaces
A shadow map stores one depth per texel: something blocks the light, or nothing does. A cut-out writes depth where it is solid, with the same cutoff, and its holes let light through. A see-through surface has no single answer: thin glass should cast a light shadow, a dense leaf a full one.
Hashed alpha answers with noise: each texel keeps the surface when its alpha is above a random threshold. On average a share a of the texels keep it, and the shadow filter (PCF), which averages a few texels, turns that share into a shadow of density a.
Take the threshold from a hash of a position on the object, not of the frame number or the screen pixel. Scale the position so one noise cell is about one shadow-map texel, from the derivatives of the position, and blend the two nearest power-of-two scales so cells do not pop as the object moves nearer (Wyman and McGuire). Then the shadow keeps still while the camera and the object move.
7In RASM
A material's AlphaMode (NONE is glTF's opaque) picks its draw run and its pipeline. Every alpha mode has its own pipeline; MASK and BLEND pipelines are made only once a scene has such draws.
Where RASM follows the page, and where it differs
| Part | This page | RASM |
|---|---|---|
| Alpha | factor × texture × vertex color | The same (getBaseAlpha in core.slang); the factor is the fourth float of PbrParam::ALBEDO. |
| Pipelines | one per mode | One entry point, fragMain of rasm.slang, compiled three times: plain, with RASM_ALPHA_MASK and with RASM_ALPHA_BLEND (Renderer::createScenePipeline puts the define before the source). Moving the shading into a shared function would read every varying up front, which cost the opaque draws about 5% in 05_city. |
| Cut-out | discard, or alpha-to-coverage with MSAA | Discard below alphaCutoff, as a demote to a helper, so the material's other samples after it keep their derivatives (B33; the session's spvDemoteToHelperInvocation capability in ShaderCompiler::getTargetDesc). With MSAA (a target with RenderTargetDesc::samples above 1) the MASK pipeline uses alpha-to-coverage with the sharpened alpha getCoverage instead (MSAA and alpha-to-coverage); shadow maps keep discard. No coverage-preserving mips yet (D25). |
| Blend state | premultiplied or straight | Premultiplied: BlendMode::PREMULTIPLIED_ALPHA in the PipelineDesc, with depthWrite false. The shader multiplies color by alpha at the end (toTarget). |
| Order | solid, sky, then see-through back to front | The Transparent pass after Scene 2 and its sky. The sort runs on the GPU after culling (sorting on the GPU), keyed by the view depth of each draw's bounding sphere center. |
| Back faces of see-through objects | cull them, or draw them first | The blend run is one run with cull mode NONE, so all of it is in one order; a single-sided material discards its back faces in the shader instead (MATERIAL_DOUBLE_SIDED). |
| Shadows | cutoff for cut-outs; hashed alpha for see-through | Both, in fragMask of shadow.slang (getHashedAlphaThreshold), with a PCG hash of the object-space position. |
| Exactness | per object, per triangle or OIT | Per draw (one sub-mesh of one entity) for the default BlendMethod::SORTED. A material can pick BlendMethod::WEIGHTED, order-independent transparency (weighted blended transparency), or BlendMethod::ADDITIVE; each has a run of its own before the sorted one, and both go under the sorted draws. |
Where it lives in code
| What | Where |
|---|---|
| The alpha mode and cutoff of a material | Material::alphaMode, alphaCutoff in include/rasm/material.h; glTF import in AssetLoader::Impl::loadMaterial (src/assets/material.cpp) |
| Runs by alpha mode and cull mode | Renderer::rebuildDrawList, getRunKey in src/renderer/gpuScene.cpp; DrawRun in src/renderer/gpuScene.h |
| The pipelines | Renderer::prepareSceneView in src/renderer/gpuScene.cpp; fragMain in shaders/passes/rasm.slang; vertMask, fragMask in shaders/passes/shadow.slang |
| The blend state | BlendMode in include/rasm/types.h, PipelineDesc in include/rasm/render/resource.h, VulkanDevice::createGraphicsPipeline |
| The early cull leaving the blend run out | countMain in shaders/passes/cull.slang (SceneData::blendRun) |
| The sort and the Transparent pass | Renderer::recordBlendSort, Renderer::drawTransparent, Renderer::addSceneViewPasses in src/renderer/gpuScene.cpp |
| An example | examples/09_gltf_materials: the glTF AlphaBlendModeTest and a block of 1536 see-through cubes |
- The opaque pipeline must not discard. Put new alpha tests under
RASM_ALPHA_MASKorRASM_ALPHA_BLEND. - The blend run is the last run and has cull mode
NONE;getCullModeforces it. Splitting it by cull mode would break the sorted order. - The sky is drawn before
Transparent: it fills only where no depth was written, and see-through draws write none. - A depth-only view has no blend run (
SceneData::blendRunisMAX_DRAW_RUNS), so its culling keeps the blend draws in the early phase like any other.
Further reading
- Thomas Porter and Tom Duff, Compositing Digital Images, SIGGRAPH 1984: the over operator and premultiplied alpha.
- Chris Wyman and Morgan McGuire, Hashed Alpha Testing, I3D 2017.
- Morgan McGuire and Louis Bavoil, Weighted Blended Order-Independent Transparency, JCGT 2013.
- Ignacio Castaño, Computing Alpha Mipmaps, 2010; and Ben Golus, Anti-aliased Alpha Test: The Esoteric Alpha To Coverage, 2017.
- The glTF 2.0 specification, section "Alpha Coverage".