Culling and the frame

Inspecting GPU resources

A frame is built from many buffers you never see: the HDR color before the tone map, the depth, the motion of each pixel, the bloom's small images, the shadow maps, the Hi-Z, the light lists. When the image is wrong, the fastest way to the cause is to look at them. This page explains how a renderer shows one of them while it runs: when to copy it, how to turn its numbers into a picture, and how to show a GPU table field by field. The last part shows how RASM does it.

In short

  • A resource holds what you want to see only for a while: a later pass can write it again, and a transient resource gives its memory to another one after its last use.
  • So the copy is a small pass placed right after the pass that last writes it. The render graph orders it and adds its barriers like any other pass.
  • The copy also converts: HDR color times an exposure, reversed-Z depth to linear distance, motion to a hue, counts to heat, one channel to gray. One multisampled sample, or their average.
  • A GPU table is shown field by field. The names, types and offsets come from the shader compiler's reflection, so no third copy of the struct exists. The bytes come from the CPU's copy: what was uploaded, not read back.
  • Nothing picked: no pass, no barrier, no cost.

1A resource is right only for a moment

The picture below is one frame on a time line. Each bar is a resource, from its first use to its last. Look at what each one holds at the end of the frame.

passes 1 draw 2 draw 3 bloom 4 tone map 5 blur 6 view 2 7 end scene color bloom level blur same memory depth cleared copy here: right after the last writer copy at the end of the frame: the blur's bytes, the second view's depth
The bloom level's memory belongs to the blur target after pass 4, and the depth is cleared for a second view. Only a copy made right after each resource's last writer sees what that resource really held.
end of frame: noise another target owns the bytes by now, or a later pass wrote them

Reading at the end of the frame, or from a capture tool's last state, shows what is left: noise, or a later pass's result.

after the writer: right the copy runs while the resource still holds its own data

A copy right after the last writer sees exactly what the readers of the resource see.

2Copy it right after the writer

The copy is one more pass. It reads the resource and writes a picture into a texture of its own, which the UI draws later in the frame. A render graph places it like any pass, from what it reads and writes.

  1. Last writerthe draw, the compute pass, or a cube's last face
  2. Barrierfrom the graph: the write is visible, the image is in a read layout
  3. Copy passone thread per texel of the picture: read, convert, write
  4. Barrierthe picture moves from storage to sampled
  5. UI passdraws the picture in a window

Three rules make the copy correct without hand-written barriers:

resource picture copy reads · writes

Declare both

The copy reads the resource and writes the picture. The UI pass reads the picture. The graph's barriers follow.

W copy B C moved up to the writer

Place it early

A sort may put the copy anywhere after the writer. Move it to just after: its only input is that writer.

lifetime +copy next owner

Count it as a use

The copy is a use of a transient resource, so its lifetime covers the copy and no other resource takes the memory first.

The graph changes only when the pick changes

Adding or removing the copy pass sorts the graph again, and a new lifetime can lay the transient memory out again. That costs one slow frame when the pick changes, and nothing in the frames after it. Settings that do not change what is read (an exposure, a mip, a channel) only change the copy's parameters.

3Turn the numbers into a picture

A UI image shows colors from 0 to 1. Most resources hold something else: light brighter than 1, a depth that is not a distance, a motion vector, a count. The copy converts each kind into a picture you can read.

value × exposure clamp to 0..1, encode sRGB

HDR color

Times an exposure you choose, then clamped. Bright parts are white until you lower it.

a one channel → gray

One channel

Red, green, blue or alpha alone, as gray: the way to read data that is not a color.

linear stored (reversed-Z)

Depth → distance

The stored depth is almost 0 for anything past a few meters. Turned into a distance, it falls evenly (next section).

direction → hue length → strength still → white

Motion → hue

The direction picks the hue on a color wheel; the length in pixels, times a gain, picks how far from white.

1864

Counts → heat

A count of lights or fragments as a heat color, one step per doubling, so 3 and 300 both read at a glance.

one sample or their average

Multisampled

A multisampled image has several values per pixel. Show one sample, or their plain average. That average is not the engine's own resolve, so say which one is shown.

4Reversed-Z depth as a distance

With reversed-Z and the far plane at infinity, the depth a pixel stores is the near plane's distance divided by the pixel's distance (see projection and depth). Near the camera it is 1; at 10 times the near distance it is 0.1; far away it is almost 0. Shown as gray, everything past a few meters is black.

d = n / z   ⇒   z = n / d

gray = 1 − z / range, clamped to 0..1

d
the stored depth, 1 at the near plane, 0 at infinity (the sky)
n
the near plane's distance
z
the distance along the view direction: the view depth
range
the distance shown black; you choose it for the scene

For any projection without skew, the same idea is one division: the depth is (P₂₂ z + P₂₃) / (P₃₂ z + P₃₃) of the view-space z, so z comes back from four numbers of the projection matrix. An orthographic projection (a sun's shadow map) stores a depth that is already linear, so it is shown as it is.

Posts stand at 0.5 m to 64 m. Compare the stored depth with the linear distance. Move the near plane and watch the stored row go black, then set the range so no post looks like the sky.

5One picture for any size

Resources come in every size: a 2048 × 2048 shadow map, a 160 × 90 bloom level, a 16 × 9 grid of light tiles. The copy writes into one fixed texture, scaled to fit with the aspect kept, and the UI shows only the part it filled.

1024 × 576 1280 × 720 color 1024 × 1024 2048 × 2048 shadow map 16 × 9 tiles: blocks UI shows the filled part each tile 64 × 64 texels
Each texel of the picture reads the source texel under it, with no filtering, so small sources show crisp blocks and large ones are thinned out.

6GPU tables, field by field

A GPU table is an array of structs that shaders read through a pointer. To show a row, the tool needs each field's name, type and offset. Writing them down by hand makes a third copy of the struct, next to the C++ one and the shader one, and it goes wrong the day a field moves. The shader compiler already knows the layout: its reflection lists every field with its offset in the layout the shaders really read.

one row, 160 bytes, as reflection lays it out world · 0 48 normalMatrix · 60 previousWorld · 104 translationLow · 48 geometryIndex · 96 materialIndex · 100 animationRow · 152 meshletVisibility · 156 name, type and offset: from the shader compiler, for the struct the shaders read the bytes: from the CPU's copy of the table, the same bytes the upload sends
The tool never names a field itself. A field added to the shader struct shows up in the table view by itself.
CPU copy GPU table upload read here: now, free

The uploaded copy: free and immediate, and exactly what the GPU receives. It does not show what a shader wrote into the table, or a bad upload.

GPU table readback copy frames late, a copy pass, a wait

A readback: shows what the GPU holds, but needs a copy pass, a host-visible buffer and a wait for the frame to finish. The view must say which one it shows.

7Free while nothing is picked

A debug tool must not change what it measures. While nothing is picked, or the window is closed, the copy pass is not in the graph: the passes, their order, the barriers, the transient memory layout and the GPU timers are the ones of the frame without the tool. The copy's shader compiles the first time something is picked.

8Pitfalls

The picture is noise or another image
The copy ran after the resource's memory changed owner, or after a later pass wrote it. Copy right after the last writer, and count the copy as a use of the resource.
The depth is all black
Reversed-Z depth is near 0 for anything past a few near-plane distances. Show the distance n / d with a range, not the stored value.
The picture is white
HDR light is far above 1. Lower the exposure, or look at one channel.
The multisampled image has jagged edges
One sample per pixel is shown. Pick the average, or the resolved target.
A table field shows nonsense
The tool's idea of the layout differs from the shader's. Take it from reflection, never from a hand-written list.
The timings change when the tool is open
That is the copy pass: measure with nothing picked.

9In RASM

The RASM window (F1, with EngineConfig::ui) has a Buffers section and a GPU tables section. An application can pick from code with Renderer::setInspection and an Inspection, or from C (1.18) with rasm_set_inspection and a sized rasm_inspection, whose target name the call copies; rasm_get_inspection gives the pick back. examples/09_gltf_materials and examples/11_many_lights take --inspect <target> <color|depth|hiz|clusters> <n>, and 09 also --inspect-rows.

CPU · start of RenderGraph::execute updateInspectPasslist targets and views pick changed? remove the old pass,add "Inspect <resource>" compilesort, then move Inspect up GPU · the frame last writere.g. Bloom Down 3 barrier Inspect bloom_down_3inspectMain inspect target1024², RGBA16F barrier UI passImGui::Image The Inspect pass reads the resource (read, readDepth, or the view's buffers) and writes the target; the UI pass reads it. Both barriers come from those declarations. Nothing picked, or the section closed: no Inspect pass, no read. Parameters (exposure, face, level, sample, channel, range) go to a host-visible buffer per frame slot, so changing them changes no graph.
The pick takes effect in the next frame: the UI is built after the graph has started its frame.

What the Buffers section lists

EntryFromOptionsShown as
a target's color, e.g. scene_hdr, bloom_down_3, scene_hdr_taa, scene_hdr_resolvedevery render target a pass of the graph writes or readschannel, exposure; sample or average when multisampledcolor × exposure, sRGB-encoded
scene_hdr motiona MOTION_FORMAT colorgainhue and strength, full at 16 px
scene_hdr depth, shadow_cascade_0 depth, shadow_cube_3 depththe depth of a target a scene view draws, when the graph lets a pass sample itface of a cube, sample, rangedistance; an orthographic shadow map as it is
scene_hdr Hi-Zeach scene view with occlusion cullinglevel, rangedistance of the farthest depth
scene_hdr light clusterseach scene view with clustersslice 0 to 23, or the tilesheat on the 16 × 9 tiles

A target is found by its name, so two targets with one name (the cascades of two shadowed suns) show the first one (D32).

Where RASM follows the textbook, and where it differs

PartThis pageRASM
Copy passafter the last writerA compute pass added with RenderGraph::addComputePass by Renderer::updateInspectPass, named Inspect and the entry's label. RenderGraph::inspectPass holds its index; compile moves it right after the last pass with an edge into it. It is always on the graphics queue, so a Hi-Z or cluster write on the async compute queue is waited for there.
Pictureone fixed textureA storage render target inspect, INSPECT_SIZE square, SCENE_COLOR_FORMAT, made on the first pick and kept. The UI pass (the pass with Pass::encodedSwapchain) gets it in Pass::inputRenderTargets while the Inspect pass exists. A graph without a UI pass gets no Inspect pass.
Parametersthe copy's settingsgfx::InspectParams in a host-visible buffer per frame slot, filled in recordInspect; gfx::InspectPushConstants holds its address. The shader defines RASM_OWN_PUSH_CONSTANTS before it includes core.slang, so it can reach SceneData with push constants of its own.
Depthz from four projection numbersgetViewDepth in shaders/passes/inspect.slang, with the projection of the view that drew the target (the face's view for a cube), from its SceneData. A projection with projection[3][3] == 1 is orthographic: the depth is shown as it is.
Hi-Z, clustersbuffers, not imagesRead through the view's SceneData::hizPtr (getHizLevel) and SceneData::clustersPtr; the Inspect pass declares the view's buffers as reads.
Heatone step per doublinggetHeatColor in shaders/lib/heat.slang, shared with the debug views' display.
Table layoutfrom reflectionEach compile walks its push constants through every pointer and keeps each struct's flattened fields (StructField), and a shader read from the shader cache brings the ones its compile stored; findStructLayout finds them. A struct no shader reached yet is shown as such.
Table rowsthe uploaded copyGpuTable::row, the CPU copy every slot receives; GpuTable::uploadedRows counts the rows the last upload sent. Instance, material, light, animated entity and morph weight tables; the geometry table has no CPU copy.
0 mswith nothing picked: the same passes and timers as without the UIevery GPU timer of 09 against main, Release
0.14–0.35 msone copy of a 1920 × 1080 sourcethe Inspect pass's timer in 09, Release, RTX 3060: depth 0.14, Hi-Z 0.16, a bloom level 0.16, a 4× MSAA sample 0.15, HDR color 0.35
8 MiBthe picture target, made on the first pick1024² × 8 B, one for both frame slots: the graph orders each frame's write after the last frame's read

Where it lives in code

WhatWhere
APIRenderer::setInspection, Renderer::getInspection in src/renderer/renderer.cpp; rasm_set_inspection, rasm_get_inspection in src/c/cDebug.cpp; Inspection, InspectKind in include/rasm/types.h
Listing, the pass, the sectionsupdateInspectPass, recordInspect, drawBuffersSection, drawTablesSection in src/renderer/inspector.cpp
Placement in the graphRenderGraph::compile, RenderGraph::removePass in src/renderer/renderGraph.cpp
Shadershaders/passes/inspect.slang, shaders/lib/heat.slang
ReflectioncollectStructLayouts in src/renderer/shaderCompiler.cpp
Keep these true
  • Nothing picked, or the section closed: no Inspect pass. The graph dump and the GPU timers equal those without the UI.
  • The Inspect pass declares every resource it reads, so the graph places its barriers. No hand-written barrier around it.
  • gfx::InspectParams matches the shader's struct (the static_asserts and validateRasmStructs).
  • The table view takes no field name from C++: only from reflection.

Further reading