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.
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.
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.
- Last writerthe draw, the compute pass, or a cube's last face
- Barrierfrom the graph: the write is visible, the image is in a read layout
- Copy passone thread per texel of the picture: read, convert, write
- Barrierthe picture moves from storage to sampled
- UI passdraws the picture in a window
Three rules make the copy correct without hand-written barriers:
Declare both
The copy reads the resource and writes the picture. The UI pass reads the picture. The graph's barriers follow.
Place it early
A sort may put the copy anywhere after the writer. Move it to just after: its only input is that writer.
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.
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.
HDR color
Times an exposure you choose, then clamped. Bright parts are white until you lower it.
One channel
Red, green, blue or alpha alone, as gray: the way to read data that is not a color.
Depth → distance
The stored depth is almost 0 for anything past a few meters. Turned into a distance, it falls evenly (next section).
Motion → hue
The direction picks the hue on a color wheel; the length in pixels, times a gain, picks how far from white.
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.
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.
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.
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.
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.
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.
What the Buffers section lists
| Entry | From | Options | Shown as |
|---|---|---|---|
| a target's color, e.g. scene_hdr, bloom_down_3, scene_hdr_taa, scene_hdr_resolved | every render target a pass of the graph writes or reads | channel, exposure; sample or average when multisampled | color × exposure, sRGB-encoded |
| scene_hdr motion | a MOTION_FORMAT color | gain | hue and strength, full at 16 px |
| scene_hdr depth, shadow_cascade_0 depth, shadow_cube_3 depth | the depth of a target a scene view draws, when the graph lets a pass sample it | face of a cube, sample, range | distance; an orthographic shadow map as it is |
| scene_hdr Hi-Z | each scene view with occlusion culling | level, range | distance of the farthest depth |
| scene_hdr light clusters | each scene view with clusters | slice 0 to 23, or the tiles | heat 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
| Part | This page | RASM |
|---|---|---|
| Copy pass | after the last writer | A 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. |
| Picture | one fixed texture | A 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. |
| Parameters | the copy's settings | gfx::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. |
| Depth | z from four projection numbers | getViewDepth 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, clusters | buffers, not images | Read through the view's SceneData::hizPtr (getHizLevel) and SceneData::clustersPtr; the Inspect pass declares the view's buffers as reads. |
| Heat | one step per doubling | getHeatColor in shaders/lib/heat.slang, shared with the debug views' display. |
| Table layout | from reflection | Each 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 rows | the uploaded copy | GpuTable::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. |
Where it lives in code
| What | Where |
|---|---|
| API | Renderer::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 sections | updateInspectPass, recordInspect, drawBuffersSection, drawTablesSection in src/renderer/inspector.cpp |
| Placement in the graph | RenderGraph::compile, RenderGraph::removePass in src/renderer/renderGraph.cpp |
| Shader | shaders/passes/inspect.slang, shaders/lib/heat.slang |
| Reflection | collectStructLayouts in src/renderer/shaderCompiler.cpp |
- 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::InspectParamsmatches the shader's struct (thestatic_asserts andvalidateRasmStructs).- The table view takes no field name from C++: only from reflection.
Further reading
- Render graphs and Transient memory and aliasing: why a resource is right only for a moment.
- Projection and depth: reversed-Z and the infinite far plane.
- Debug views: values of the shading in place of the image, and the heat colors.
- Struct layout: the scalar layout reflection reports.
- Headless rendering and readback: a real readback to the CPU (
Renderer::readTexture), for a texture madereadable, which the graph keeps out of shared memory. - RenderDoc's texture viewer does the same on a captured frame, with a range slider for depth.