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.

x → red y → green z → blue

Normals

Which way does the surface face? Shows a broken normal map, flipped faces, missing tangents.

u → red v → green

UVs

Where does the texture go? Shows seams, stretching, mirrored or missing UVs.

1 2 3 1

Overdraw

How many fragments were shaded at each pixel? Shows wasted shading work.

LOD 0 1 2 distance

LODs

Which level of detail does each draw use? Shows LODs that switch too early or too late.

occluded outside

Culling

What was drawn, when, and what was left out? Shows culling that keeps too much or too little.

121 120 lights per cluster

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.

inputs position, normal tangent, UVs material alpha test: a cut-out still discards normal: shade lights, shadows, environment debug: one value normal, UV, 1, LOD, phase, lights radiance tone mapped value or count shown as it is, or heat
The debug shader is the scene's shader with its last step replaced. The target it writes and the passes after it stay the same.

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.

A B C near → far back to front: C, B, A C B A count 3 front to back: A, B, C A B C count 1 dashed: failed the depth test before shading
The depth test stays on, so the count is the real shading work: a fragment that fails the test before its shader runs is not counted.

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.

Change the draw order and switch the depth test. Find the order where every covered pixel shades exactly once.

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:

Count01248163264 and more
Colorblackdark bluebluecyangreenyellowredwhite

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.

this pixel's cluster misses count = 2 lights of the cluster + 1 sun = 3
The count is what the shading loops over, so it is the light part of the pixel's cost. A light whose sphere is much bigger than its visible glow raises the count for nothing.

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.

drawn early: seen last frame drawn late: seen only after the Hi-Z ghost: in the frustum, occluded ghost: outside the frustum
frozen camera wall live camera the live camera sees the ghosts through what is in front of them
With culling frozen, the frozen camera's choices stay while the live camera flies around them. The ghosts are drawn without a depth test, so nothing hides them.

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.

drawn dropped culled ghost

Ghost boxes

A box around each draw's bounding sphere, after the last pass. The vertex shader drops the ones culling kept.

more red

Added, not tested

No depth test and additive blending: a ghost shows through walls, and more ghosts make a brighter color.

frozen frustum

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.

  1. Scene passes write a value, or add 1, into the HDR target.
  2. MSAA resolve or TAA runs as usual.
  3. Debug display shows the value, or round(count) as heat.
  4. UI draws over the image.
Anti-aliasing stays on

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.

if (debugView != NONE) return debugValue(); return shade(); every frame pays for the test

A branch

One shader for both. The normal frame carries the debug code.

no defineshade() DEBUG_VIEWdebugValue() the CPU picks one per draw run

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.

a shaded scene view, debug view on Cull 1early Scene 1debug, arg 0 Hi-Z Cull 2late Scene 2debug, arg 1 Transparentdebug blend run, then ghosts frozen: record nothing scene_hdr: values or countsno sky behind them MSAA resolve / TAAas usual Bloomnothing Tone MapfragDebug or fragDebugHeat SceneData::debugView says which view; PushConstants::argument's lowest bit says the pass (0 early, 1 late). Shadow maps and every other pass run unchanged.
The graph is the same with and without a debug view: only the pipelines each pass binds change. That is why switching a view needs no new graph.

Where RASM follows the textbook, and where it differs

PartThis pageRASM
Variantthe scene shader with a definerasm.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.
Pipelinesmade when a view is first chosenShadedPipelines::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.
Overdrawadd 1 with the depth test onBoth 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.
LODthe level whose range the draw usesgetDrawLod: 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.
Meshletsa color per pieceDebugView::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 countlights of the pixel's cluster, plus global onesgetDebugLightCount: 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 latecolor by passThe 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.
Ghostsa box per culled objectdrawCullingGhosts 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).
Freezeone frame's culling keptSceneView::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 frustumlines in the worlddrawFrozenFrustums 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.
Displayvalues as they are, counts as heatfragDebug 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 colorsone rampgetHeatColor 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.
0 mswith DebugView::NONE: the same pipelines and passes as beforeevery GPU timer of 09 and 15 against main, Release, within ±2% run to run
1compare per draw run on the CPUSceneView::debugView
0 Bmore SceneDatathe view fills the word before animatedTransformsPtr
4.1–4.4 msGPU frame in each view (5.0 ms shaded)09 at 1920 × 1080, RTX 3060, Release: no lights, sky or bloom
0.6 msthe ghost boxes of 05_city, 145.6K draws1.7 ms frozen, with many boxes outside
32 msthe same ghosts as meshes at LOD 0202M triangles: why they are boxes

Where it lives in code

WhatWhere
APIRenderer::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, ghostsprepareSceneView, drawSceneView, drawTransparent, drawCullingGhosts in src/renderer/gpuScene.cpp
Shadersshaders/lib/debugViews.slang, shaders/passes/rasm.slang, shaders/passes/debugGhosts.slang, shaders/passes/toneMap.slang
DisplayaddToneMapPass in src/renderer/toneMap.cpp, bloomRuns in src/renderer/bloom.cpp
RASM window, frustum linesdrawEngineWindow, drawFrozenFrustums in src/renderer/engineUi.cpp
Blend and depth stateBlendMode::ADDITIVE, PipelineDesc::depthTest, src/rhi/vulkan/vulkanDevice.cpp
Keep these true
  • The normal shaders get no debug branch: debug code lives inside #if defined(RASM_DEBUG_VIEW).
  • DebugView's numbers match the DEBUG_VIEW_* constants of debugViews.slang (a static_assert in gpuScene.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, through gpu-struct-sync; never into the normal path.

Further reading