Culling and the frame
Debug views
A shaded image hides why it looks the way it does, and why it is slow. A debug view draws the same scene, but each pixel shows one value of the work behind it: a normal, a UV, how many fragments were shaded, which LOD, what culling did, how many lights. This page explains how each view is made and how to read it. The last part shows how RASM does it.
In short
- A debug view is the scene's own shader with the shading swapped for one value. Everything else stays: the same draws, depth test, culling and cut-outs.
- Values (normals, UVs, LOD tints, culling colors) are written as colors and shown as they are, without exposure or a tone curve.
- Counts (overdraw, lights) are numbers. Overdraw adds 1 per fragment with additive blending; the display turns each count into a heat color.
- The culling view also draws what culling left out, as see-through ghosts: the bounding box of each culled object. Freezing culling keeps one frame's results while the camera moves, so you can look at them from outside.
- A view is a shader variant compiled with a define the first time it is chosen. While it is off, the normal shaders have no extra branch: it costs nothing.
1Six views, six questions
Each view answers one question. Pick the view by the question you have.
Normals
Which way does the surface face? Shows a broken normal map, flipped faces, missing tangents.
UVs
Where does the texture go? Shows seams, stretching, mirrored or missing UVs.
Overdraw
How many fragments were shaded at each pixel? Shows wasted shading work.
LODs
Which level of detail does each draw use? Shows LODs that switch too early or too late.
Culling
What was drawn, when, and what was left out? Shows culling that keeps too much or too little.
Light count
How many lights does each pixel shade? Shows lights whose range is too large.
2Swap the shading, keep the rest
A debug view must show the frame as it really is. So it does not draw the scene some other way: it runs the same draws, with the same vertex shader, depth test and culling, and changes only what the fragment shader writes. A cut-out leaf still cuts, a skinned arm is still where the skin pass put it.
normal view = n × 0.5 + 0.5
UV view = (fract(u), fract(v), 0) × checker
checker = 1 or 0.7, 8 squares per unit
- n
- The shading normal in world space, after the normal map, turned around on a back face. Each part is −1 to 1, so it is moved to 0 to 1: a surface facing +y is (0.5, 1, 0.5), light green.
- fract
- The part after the point. A UV of 2.3 shows as 0.3: a texture that repeats shows the same ramp again. The checker shows how big a texel is and where UVs are stretched.
3Count with additive blending
Overdraw is how many fragments were shaded at one pixel. Only one of them is seen; the others were work for nothing. To count them, every fragment writes 1 and the target adds what it gets: additive blending, result = source + target.
With the depth test on, the order of the draws matters. Front to back, the first surface writes its depth and the GPU rejects the ones behind it before they are shaded. Back to front, each nearer surface passes again. A see-through surface does not write depth, so everything behind it still counts.
The count is shown as a heat color, one step for each doubling, because overdraw from 1 to 4 is normal and 30 is a problem:
| Count | 0 | 1 | 2 | 4 | 8 | 16 | 32 | 64 and more |
|---|---|---|---|---|---|---|---|---|
| Color | black | dark blue | blue | cyan | green | yellow | red | white |
Counts between two steps blend between their colors. The lab above uses the same ramp.
4Count the lights of a pixel
With clustered lighting, a pixel shades only the lights of its cluster: the box of the view, a screen tile times a depth slice, that it falls in. The light count view writes that number, plus the lights that reach every pixel (a sun), and shows it with the same heat ramp.
5Tint by LOD
A mesh with levels of detail is drawn with one of its index ranges. The LOD view tints each draw by the level it uses, 0 to 7, and keeps a little light from the camera so the shapes stay readable. Distant objects should change color in rings around the camera. A pop you see in the shaded image shows here as a draw that changes color.
The view does not need a new input: the draw already holds the first index of its range, and the mesh lists where each level's range starts. The level whose range starts there is the one drawn.
6Show what culling did
Two-pass occlusion culling draws in two passes. The early pass draws what was visible last frame. A Hi-Z is built from that depth, and the late pass draws what the early one missed and is visible now. The culling view colors each drawn object by its pass, and draws every culled object as a see-through ghost: a box around the bounding sphere that culling tested.
A ghost is a box, not the object's mesh. Drawing every culled mesh again would cost as much as drawing the whole scene without culling: in a city of 200 million triangles, about as much as 30 ms. The box is 12 triangles per object, and it is the shape culling really tested.
Ghost boxes
A box around each draw's bounding sphere, after the last pass. The vertex shader drops the ones culling kept.
Added, not tested
No depth test and additive blending: a ghost shows through walls, and more ghosts make a brighter color.
Freeze
Keep one frame's frustum, LOD camera and visibility. The early pass then culls with them; the late pass stops.
7Show the values as they are
A tone curve and exposure would change a value: a normal of 0.5 would not show as 0.5. So while a debug view is on, the tone map pass binds a debug display instead. It shows a value as the color it is, or a count as its heat color, and the bloom adds nothing.
- Scene passes write a value, or add 1, into the HDR target.
- MSAA resolve or TAA runs as usual.
- Debug display shows the value, or round(count) as heat.
- UI draws over the image.
Inside a surface every sample holds the same value, so the resolve and TAA give it back exactly. Only at edges do two values mix: a normal there is a blend of two normals, and a count is rounded to the nearer one. The image stays smooth and the inside of each surface stays exact.
8Free while it is off
A debug tool must not slow down the frames it is meant to measure. A switch in the shader would cost every frame a branch, and its code would make the shader bigger even when it never runs.
A branch
One shader for both. The normal frame carries the debug code.
A variant
One file, compiled twice. The debug one is made the first time a view is chosen; until it is ready, the view draws shaded.
9Pitfalls
- The overdraw view is blue where nothing was drawn
- Something is drawn there: a sky box, a ground plane far away. The view counts every fragment, even of a mesh you think of as background.
- Overdraw is high on a flat wall
- The draws behind it came first: back-to-front order, or a mesh drawn before the one in front. Sort opaque draws front to back, or draw a depth pre-pass.
- A green object also has a red ghost
- The early pass drew it, because it was seen last frame, and this frame's late test found it hidden. It is drawn now and will be culled next frame.
- Ghost stripes over a green surface look yellow or white
- A ghost adds its red to what is under it, and many ghosts add up. Read the stripes as "culled here", and their brightness as how many; the color of a stripe on black is its kind.
- Everything is a ghost after freezing
- Culling froze on a frame that had seen nothing yet, like the first frame, or the object appeared after the freeze. Frozen visibility never sees anything new. Freeze after a few frames, once the scene is loaded.
- A see-through object looks wrong while frozen
- Without a late pass its draws are not sorted back to front any more. The culling is right; only the order of the see-through draws changed.
- Normals look right but the light is wrong
- The view shows the normal the shading uses. Check the light count and the light itself next.
10In RASM
An application calls Renderer::setDebugView with a DebugView (getDebugView reads it back), and Renderer::setCullingFrozen. From C (1.18) they are rasm_set_debug_view, rasm_get_debug_view and rasm_set_culling_frozen; examples/33_c_render_graph takes --debug-view normals. The RASM window (F1, with EngineConfig::ui) has both under Debug views, with a color key for the view. examples/15_debug_ui and examples/05_city take --debug-view overdraw and --freeze-culling.
Where RASM follows the textbook, and where it differs
| Part | This page | RASM |
|---|---|---|
| Variant | the scene shader with a define | rasm.slang's fragMain with RASM_DEBUG_VIEW, for each alpha mode (MASK with RASM_ALPHA_MASK discards instead of alpha-to-coverage). getDebugColor in shaders/lib/debugViews.slang writes the value. The view is SceneData::debugView, the spare word that keeps animatedTransformsPtr 8-byte aligned, so SceneData keeps its size. |
| Pipelines | made when a view is first chosen | ShadedPipelines::debug (no blending) and ShadedPipelines::overdraw (BlendMode::ADDITIVE), per sample count and motion target, made in prepareSceneView like the MASK and BLEND ones. Their shaders are not warmed at startup: setDebugView queues them for the shader workers, and the view draws shaded until every pipeline it needs is ready, so no draw run is missing meanwhile (Shader warm-up). With a warm cache that is the same frame. drawSceneView and drawTransparent pick them per draw run from SceneView::debugView, the view this frame draws. |
| Overdraw | add 1 with the depth test on | Both draw passes and Transparent add; the opaque runs write depth, the blend run does not. Fragments write zero motion, so TAA's motion target is left as it was. |
| LOD | the level whose range the draw uses | getDrawLod: the LOD of GeometryData::lods whose index range holds the draw's firstIndex (a whole LOD's, or one meshlet's), in the vertex shader. |
| Meshlets | a color per piece | DebugView::MESHLETS: getMeshletKey hashes the first index of what is drawn (a meshlet's or a whole draw's) and the instance into a color, so with a meshlet path each meshlet a draw is split into has its own color, the same on the vertex-pulling and the mesh-shader path, and a mesh drawn whole has one. |
| Light count | lights of the pixel's cluster, plus global ones | getDebugLightCount: the shadowed lights, the other global lights, and the cluster's lights (every local light when the view has no clusters): the same loops as shadePBR. |
| Early and late | color by pass | The lowest bit of the draw passes' PushConstants::argument: CULL_EARLY or CULL_LATE (a mesh-shader draw has its run above it). The blend run is culled by the late pass, so see-through draws show late. |
| Ghosts | a box per culled object | drawCullingGhosts in the Transparent pass: one instanced draw of 36 vertices per draw of the list, a box around getWorldSphere's sphere, back faces culled, in diagonal bands so the surface shows between them; shaders/passes/debugGhosts.slang, a pipeline with PipelineDesc::depthTest false and additive blending. A draw counts as drawn when the visibility buffer saw it (with occlusion culling) or when culling gave it a slot (frustum culling only). |
| Freeze | one frame's culling kept | SceneView::frozen: the frustum planes, lodPixelsPerUnit and lodDepthPlane of the frame it froze in. SceneView::late is false, so the Hi-Z, Cull 2 and Scene 2 record nothing. The early pass keeps the blend run too (SceneData::blendRun set to none) and the sort is skipped. Shaded views only: shadow maps follow the camera, with the frozen LOD camera, so a surface and its shadow keep the same triangles. A draw that the frozen visibility never saw (a new entity, or a mesh that loaded after the freeze) stays hidden until culling is unfrozen. cull.slang is unchanged. |
| Frozen frustum | lines in the world | drawFrozenFrustums on ImGui's background draw list, with the UI on: the near rectangle, the edges, and a far rectangle at 200 times the near distance. |
| Display | values as they are, counts as heat | fragDebug and fragDebugHeat in toneMap.slang, for the view the camera's scene views really drew (getDrawnDebugView: a view whose scene still loads, or whose debug pipeline failed, draws shaded and is tone mapped); bloomRuns is false then. No sky is drawn, so the clear color (BACKGROUND_COLOR, near black) is behind the scene and a count starts at 0. |
| Heat colors | one ramp | getHeatColor in shaders/lib/heat.slang, which the tone map's fragDebugHeat and the RASM window's Buffers section (Inspecting GPU resources) share. |
| TAA | — | A new view starts each camera's history over, so it does not blend with the frames before. |
DebugView::NONE: the same pipelines and passes as beforeevery GPU timer of 09 and 15 against main, Release, within ±2% run to runSceneView::debugViewSceneDatathe view fills the word before animatedTransformsPtrWhere it lives in code
| What | Where |
|---|---|
| API | Renderer::setDebugView, Renderer::getDebugView, Renderer::setCullingFrozen, getDrawnDebugView in src/renderer/debugViews.cpp; the C calls in src/c/cDebug.cpp; DebugView in include/rasm/types.h |
| Pipelines, freeze, draws, ghosts | prepareSceneView, drawSceneView, drawTransparent, drawCullingGhosts in src/renderer/gpuScene.cpp |
| Shaders | shaders/lib/debugViews.slang, shaders/passes/rasm.slang, shaders/passes/debugGhosts.slang, shaders/passes/toneMap.slang |
| Display | addToneMapPass in src/renderer/toneMap.cpp, bloomRuns in src/renderer/bloom.cpp |
| RASM window, frustum lines | drawEngineWindow, drawFrozenFrustums in src/renderer/engineUi.cpp |
| Blend and depth state | BlendMode::ADDITIVE, PipelineDesc::depthTest, src/rhi/vulkan/vulkanDevice.cpp |
- The normal shaders get no debug branch: debug code lives inside
#if defined(RASM_DEBUG_VIEW). DebugView's numbers match theDEBUG_VIEW_*constants ofdebugViews.slang(astatic_assertingpuScene.cpp), and the RASM window's color keys match the shaders' colors.- No time and no random numbers in a view, so a view's image is the same in every run.
- A new value a view needs goes into the variant or into
SceneData's existing words, throughgpu-struct-sync; never into the normal path.
Further reading
- The two-pass occlusion culling, clustered lighting and LOD selection guides: what the culling, light count and LOD views show.
- Immediate-mode UI: the RASM window the views are chosen in.
- RenderDoc's overlays (wireframe, depth test, overdraw) do the same on a captured frame.