How do you measure what draw calls and batching actually cost a frame, on the CPU and on the GPU, instead of just counting draws?
answer
- a count is only a proxy
- time the submitting thread
- timestamps around passes
- read results frames later
- A/B one category
basics
~20 sTime both sides: CPU milliseconds on the thread that records and submits draws, and GPU milliseconds per pass from timestamp queries or a timing capture. Batching pays off mainly when the measured CPU submission time is the side over budget.
solid answer
~50 sA draw count is a proxy: the CPU cost of a draw depends on what changes between draws, and its GPU cost on the vertices and pixels it touches. So I measure time. On the CPU, instrumented zones around the code that records and submits draws give milliseconds per frame on that thread. On the GPU, timestamp queries written at pass boundaries give milliseconds per pass, read back a couple of frames later so the readback does not stall the CPU. Then I A/B a change, say instancing one category of objects, and compare frame time on a fixed camera path. If the frame is GPU-bound at 21 ms, cutting submission from 9 ms to 3 ms changes nothing, and merging meshes can even add GPU time because merged objects can no longer be culled one by one.
go deeper
Recall that a draw call is a CPU request to draw something, and that the count alone does not say how many milliseconds it costs.
Explain how timestamp queries measure GPU time per pass, why their results are read a few frames late, and how to time submission on the CPU.
Design the A/B test for a batching change and predict its effect from which side limits the frame, including the culling cost of merged meshes.
Decide which GPU and submission metrics the team tracks per pass and per platform, so draw-count arguments are settled by milliseconds rather than by rules of thumb.
## Why a draw count is not a cost A **draw call** is one request from the CPU to the GPU to draw a set of primitives with a given state. Counting draws is easy, which is why the number is quoted so often, but it is only a proxy. The CPU cost of a draw varies widely with how much state changes between it and the previous one (why that is belongs with GPU command submission), and its GPU cost depends on how many vertices and pixels it touches, not on the fact that it is a separate draw. Two frames with the same draw count can differ by several milliseconds on either side. "Do draw calls cost us too much?" is therefore answered by measuring time on both sides of the frame. ## Measuring the CPU side - Put instrumented zones around the code that records and submits draws, on whichever thread does it: a render thread, or several workers. - Read the result as milliseconds per frame, not draws per frame. Four thousand draws submitted in 2 ms is fine; fifteen hundred in 9 ms is a problem. - Note where the time lands. Submission time on the thread that limits the frame lengthens the frame; the same time on a worker with slack may cost nothing. ## Measuring the GPU side The GPU's work is invisible to a CPU profiler: it runs asynchronously, often a frame or more behind the CPU. Its time is measured with **timestamp queries**: - the CPU records "write a timestamp here" commands into the command stream at pass boundaries; - when the GPU reaches each one, it writes its counter value into a query buffer; - the difference between two values, divided by the counter's frequency, gives the GPU time for that range; - results are read back a couple of frames later from a small ring of query buffers, because waiting for them in the same frame forces the CPU to wait for the GPU and destroys the overlap being measured. GPUs are deeply pipelined and overlap neighbouring work, so a timestamp range around one small draw is blurry, while ranges around whole passes (shadows, main geometry, lighting, post-processing) are the dependable budget unit. GPU clocks also change with load and temperature, so comparisons average many frames and, where the platform allows it, pin the clocks. ## Timing captures and single-frame captures | | Timing capture | Single-frame capture | |---|---|---| | Records | CPU threads and GPU queues over many frames | Every command of one frame, replayed | | Answers | Where did this frame's time go, and on which side? | Which draws and passes are expensive relative to each other? | | Timing fidelity | Live, with real overlap | Replay timings, often with work serialised | | Use for budgets | Yes | Ranking and inspection, not absolute budgets | Per-draw timings from a replayed frame usually do not add up to the live GPU frame time, because the replay measures draws in isolation while the live GPU overlaps them. ## Measuring batching with an A/B test Batching and instancing cut the number of draws by drawing many objects per call. Whether that helps is a measurement, not a belief: 1. Pick a fixed, repeatable view, such as a scripted camera path through a dense forest. 2. Record CPU submission time, GPU time per pass and frame time. 3. Apply the batching change to one category of objects only. 4. Record the same numbers again and compare frame time, not draw count. The outcome depends on which side was limiting. If the frame is CPU-bound and submission drops from 9 ms to 3 ms, the frame can get up to 6 ms faster, until the GPU becomes the limit. If the GPU is the limit at 21 ms, the frame stays near 21 ms however much CPU time is saved. And batching can cost GPU time: a mesh merged from a hundred objects is culled as one, so the GPU also processes the parts that are off screen. ## Traps - Reporting "draws went from 5,000 to 2,000" as a win without a millisecond figure on either side. - Summing per-draw replay timings and calling the total the GPU frame time. - Reading timestamp results in the frame that wrote them. - Optimising draw submission on a frame the GPU is limiting.
- Why can merging every static mesh in a level into one big mesh make a frame slower?A merged mesh is culled and level-of-detail selected as a single object, so when any part is visible the GPU processes all of it, including the parts behind the camera. CPU submission falls, but GPU vertex work can rise, and on a GPU-bound frame that is a net loss. Measure GPU pass time before and after.
- Why do per-draw timings from a single-frame capture not add up to the live GPU frame time?Replay tools often time each draw in isolation, with the GPU drained between them, while the live GPU overlaps neighbouring draws in its pipeline. The isolated numbers are good for ranking which draws are expensive, not for budgets; the budget comes from pass-level timestamps in live frames.
- Fewer draw calls always make a frame faster.
- Each draw call has one fixed cost, so the count is the cost.
- Reading GPU timestamp results in the same frame is free.
- Per-draw timings from a frame capture add up to the live GPU frame time.
- Batching speeds up a frame limited by GPU pixel work.