summaryrefslogtreecommitdiff
path: root/examples/noc/notes/perf.md
blob: b458b90449845de296c442d4fee19195b92dca4e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# perf: why the GPU vector renderer is no faster than the CPU one

2026-10-30. The question: the flocking port draws 1000 boids at about 9 ms/frame with either
`vector.Renderer(.cpu)` or `vector.Renderer(.gpu)`. Is the host's per-draw pipeline lookup
the cause, and does fixing it change the answer?

**Short answer.** Yes, it is the main cost in Debug (85% of the GPU path's host time). In
ReleaseFast it is about a quarter of the frame (28% of `Gpu.run`). After three small host fixes, gpu mode at 1000 boids
goes from 4.8 to 3.1 ms/frame in ReleaseFast and from 47 to 11 ms in Debug. Even so, the
vector GPU path still costs about 30× more than drawing the same triangles in one draw (`raw`:
0.1 ms). What is left is the per-draw cost of 4 draws per boid, with a pipeline switch on every
draw. It shows up on the CPU in the NVIDIA driver and SDL_GPU, and on the GPU front end. It is
not fill: with 1× MSAA the GPU time is the same as with 8×.

## Method

- Bench: a copy of the library and a bench app in
  `/tmp/claude-1000/-home-goblin-00-projects/b03e088f-d250-44e5-b526-5614572b9fb9/scratchpad/perf/`.
  `bench/src/main.zig` draws `BENCH_N` static boids (a filled triangle plus a 1 px stroke) at
  640×360. `BENCH_MODE` picks `cpu` (vector CPU renderer, then a `f.pixels` upload), `gpu`
  (vector GPU renderer: stencil fan plus cover quad per fill, 8× MSAA) or `raw` (every triangle
  plus three edge quads per boid in one `f.gpu` draw, 4× MSAA). Every boid has two fills (fill
  and stroke), so the gpu mode makes 4 draws per boid: 4000 at N=1000 and 20000 at N=5000.
- Runs are headless (`--headless --fresh --script`): `frames 330` in ReleaseFast and
  `frames 130` in Debug. The first 30 frames are dropped as warm-up (pipeline creation and buffer
  growth), so 300 or 100 frames are measured.
- Instrumentation, in the copy only (`instr-*.patch`, not part of the fixes):
  - `Host.zig` adds `SDL_GetTicksNS` timers around the sketch's `draw` ("sketch draw"), each
    `Gpu.run` ("Gpu.run"), the 2D `gfx.draw` commands, and `RenderPresent` plus `endFrame`. At
    exit it waits for the GPU to go idle and prints the totals. "wall" is the time from frame 30
    to that idle point, divided by the frame count, so a GPU backlog would show up in it.
    `Host.print` also prints in headless mode: the original drops headless logs.
  - `gpu.zig --profile-gpu` already waits for an idle GPU before each pass, but it books its time
    from *before* command recording, so it measures CPU recording plus GPU time (4.9 ms vs
    1.15 ms here). I added the time from submit to fence ("GPU exec"). That is GPU execution on
    an otherwise idle GPU, plus about 0.03 ms of submit latency (an empty pass measures
    0.034–0.055 ms). It comes from a separate `--profile-gpu` run.
- Matrix: modes {cpu, gpu, raw} × N {120, 1000, 5000} × {ReleaseFast, Debug}, 3 repeats, median
  reported (`harness/bench.py`, `harness/table.py` in the scratchpad). To measure each fix on its
  own I used interleaved A/B runs (orig, fix1, fix2, fix3 in turn: 6 rounds in ReleaseFast and 3
  in Debug, median, `harness/ab.sh`). Separate matrix runs drifted by up to 30% on cells no
  patch touches.
- Profiling: `perf record -e cpu-clock:u -F 4999`, flat (self time by DSO and symbol). Call
  graphs (fp, dwarf, lbr) are refused in this environment, so stack attribution comes from the
  timers above.
- Machine: 16 threads. GPU is an RTX 3050 on driver 615.78 through SDL_GPU Vulkan, with SDL
  built from source in the same optimize mode as the app. **The GPU was shared**: the
  compositor (driftwm), Xwayland, Chrome's GPU process, Firefox and two pardes-gui windows were
  on it, at 13–26% utilization and P5 clocks before the runs. An unrelated `zig build-exe` used
  one core during part of the runs (load average about 6). GPU-exec numbers depend on the clock
  state; Debug runs leave the GPU idle longer, so their GPU exec reads higher (for example 13 ms
  vs 7.9 ms at N=5000) for identical GPU work.
- Correctness: the gpu-mode screenshots from orig, fix1, fix2 and fix3 are byte-identical
  (sha256 4169017b…), and so are the cpu-mode ones.

## Baseline (original host), median of 3, ms per frame

wall ≈ host CPU in every cell, so the bench is CPU-bound throughout and the GPU never limits
throughput. "2D+present" is the `gfx.draw` commands plus `RenderPresent`.

| build | mode | N | wall | sketch draw | Gpu.run | 2D+present | GPU exec |
|---|---|---:|---:|---:|---:|---:|---:|
| Release | cpu | 120 | 0.98 | 0.42 | — | 0.56 | (SDL 2D, not profiled) |
| Release | cpu | 1000 | 4.05 | 3.38 | — | 0.67 | — |
| Release | cpu | 5000 | 17.4 | 16.5 | — | 0.97 | — |
| Release | gpu | 120 | 0.55 | 0.08 | 0.46 | 0.02 | 0.18 |
| Release | gpu | 1000 | **4.85** | 0.84 | **3.96** | 0.03 | 1.18 |
| Release | gpu | 5000 | **28.2** | 5.05 | **23.2** | 0.06 | 7.9 |
| Release | raw | 120 | 0.05 | 0.005 | 0.03 | 0.02 | 0.04 |
| Release | raw | 1000 | **0.095** | 0.035 | 0.047 | 0.01 | 0.10 |
| Release | raw | 5000 | 0.32 | 0.18 | 0.12 | 0.02 | 0.27 |
| Debug | cpu | 120 | 2.46 | 1.81 | — | 0.66 | — |
| Debug | cpu | 1000 | 14.5 | 13.6 | — | 0.86 | — |
| Debug | cpu | 5000 | 67.0 | 65.4 | — | 1.97 | — |
| Debug | gpu | 120 | 5.46 | 0.73 | 4.68 | 0.03 | 0.21 |
| Debug | gpu | 1000 | **47.2** | 6.14 | **40.9** | 0.09 | 2.0 |
| Debug | gpu | 5000 | **269** | 30.8 | **238** | 0.12 | 13.4 |
| Debug | raw | 120 | 0.12 | 0.06 | 0.03 | 0.02 | 0.05 |
| Debug | raw | 1000 | 0.50 | 0.42 | 0.05 | 0.02 | 0.11 |
| Debug | raw | 5000 | 2.27 | 2.08 | 0.15 | 0.03 | 0.32 |

Spread (half the max−min range over the median) was ≤4% in most cells, up to ±10–11% for
release gpu at 5000 and raw at 1000. The porter's result matches: in ReleaseFast the gpu
mode (4.85) is slower than cpu (4.05), and in Debug much slower (47 vs 14.5). In the gpu mode
82–89% of the frame is `Gpu.run`, the host turning the pass into SDL calls. The sketch-side
tessellation (`Polygons.fill/stroke`, `Gpu.record`) is 0.84 ms at N=1000.

## Profile attribution: gpu mode, N=1000 (flat perf, share of all user samples)

| | ReleaseFast orig (4.87 ms/frame) | ReleaseFast fix2 (3.50) | Debug orig (48.8) | Debug fix2 (12.8) |
|---|---:|---:|---:|---:|
| Wyhash of SPIR-V (`update`/`final`/`init`/`hash`) + `gpu.shader` | **15.4%** (≈0.75 ms) | 0 | **66.1%** (≈32 ms) | 0 |
| NVIDIA driver (`libnvidia-glcore`: vkCmd*, descriptor writes) | 45.8% (≈2.2 ms) | 54.5% (≈1.9 ms) | 5.8% (≈2.8 ms) | 17.0% (≈2.2 ms) |
| SDL_GPU Vulkan layer (BindGraphicsDescriptorSets, FetchDescriptorSet, TrackBuffer, Bind*/Push*) | ≈5% | ≈7% | ≈5% | ≈20% (≈2.6 ms) |
| memcpy (likely Paint uniform blocks, 720 B each: built, copied, pushed) | 8.8% | 8.1% | 0.8% | 2.5% |
| sketch: Drawing builder, Polygons, `Gpu.record`, arena | ≈16% | ≈23% | ≈12% | ≈50% (≈6.4 ms) |
| libc locks, vdso clock, libm | ≈10% | ≈11% | ≈3% | ≈3% |

How to read it:
- **Debug, original:** two thirds of all CPU time is Wyhash going over each shader's whole
  SPIR-V, twice per draw. A stencil draw hashes path.vert (1044 B) plus stencil.frag (288 B); a
  cover draw hashes path.vert plus cover.frag (4828 B). That is about 14 MB per frame at
  N=1000. Unoptimized Zig hashes it slowly.
- **ReleaseFast, original:** hashing is 0.75 ms of 4.9. The largest item is the NVIDIA driver.
  Every draw switches pipeline (stencil, cover, stencil, …), and SDL_GPU's
  `VULKAN_BindGraphicsPipeline` then marks every descriptor set and uniform offset dirty. So
  each of the 4000 draws gets a new descriptor set (`FetchDescriptorSet` plus
  `vkUpdateDescriptorSets`), `vkCmdBindDescriptorSets`, `vkCmdBindPipeline`, a vertex-buffer
  rebind, uniform pushes, stencil ref and scissor, then `vkCmdDraw`. The host cannot remove
  this cost: it follows from one pipeline switch per draw.

## Fixes (in the copy, each a separate patch against the original `src/host/gpu.zig`)

Measured with interleaved A/B; median `Gpu.run` time in ms per frame, gpu mode (wall in
parentheses). The sketch-draw column did not change, as expected (0.71–0.86 ms at N=1000 in
ReleaseFast, 6.1 ms in Debug).

| | Release N=120 | Release N=1000 | Release N=5000 | Debug N=1000 | Debug N=5000 |
|---|---:|---:|---:|---:|---:|
| orig | 0.454 (0.548) | 3.94 (4.77) | 24.5 (29.9) | 41.0 (47.3) | 221 (254) |
| + fix1: pipeline per pass | 0.283 (0.376) | 2.82 (3.72) | 17.0 (22.0) | 6.14 (12.3) | 42.7 (73.9) |
| + fix2: skip redundant state | 0.272 (0.366) | 2.46 (3.19) | 16.4 (21.6) | 5.84 (12.0) | 42.6 (75.1) |
| + fix3: one vertex binding | 0.267 (0.359) | 2.37 (3.13) | 14.6 (19.8) | 4.80 (11.1) | 26.6 (57.8) |

What each fix did on its own:
- **fix1**: `Gpu.run` −38% at 120, −28 to −30% at 1000/5000 (ReleaseFast); −85% and −81% in
  Debug. Wall at Debug N=1000 drops 47.3 → 12.3 ms (3.8×), at N=5000 254 → 74 ms.
- **fix2**: −4% (ReleaseFast) and −0 to −5% (Debug). The A/B at 1000 shows −10% from fix1, but
  fix1's median there carries one noisy round; a separate 8-round A/B gave −3.6%. Small; it only
  avoids `vkCmdSetScissor`/`vkCmdSetStencilReference` and a 16-byte uniform copy per draw.
- **fix3**: −4% at 1000 and −11% at 5000 (ReleaseFast); −18% at 1000 and −38% at 5000
  (Debug). SDL's `TrackBuffer` does a linear search of the command buffer's used-buffer list,
  and `vkCmdBindVertexBuffers` ran on every draw.
- All three together: gpu-mode wall at N=1000 4.77 → 3.13 ms (ReleaseFast) and 47.3 → 11.1 ms
  (Debug); at N=5000 29.9 → 19.8 and 254 → 58.

### fix1: resolve each pass pipeline once per pass (the suspected culprit)

```diff
--- a/src/host/gpu.zig
+++ b/src/host/gpu.zig
@@ -187,6 +187,12 @@
     if (pass.width == 0 or pass.height == 0 or pass.draws.len == 0 and pass.clear == null) return;
     const samples = g.supportedSamples(pass.samples);
     const t = try g.renderTarget(images, pass.image, pass.width, pass.height, samples, pass.stencil);
+    // Each pipeline is looked up on first use in the pass, not per draw:
+    // the lookup hashes the shaders' code. Outer null: not looked up yet;
+    // inner null: it failed (warned once).
+    const built = try g.gpa.alloc(??*c.SDL_GPUGraphicsPipeline, pass.pipelines.len);
+    defer g.gpa.free(built);
+    @memset(built, null);
 
     const start = g.startTiming();
     // Every draw's vertices go up in one copy.
@@ -235,13 +241,15 @@
     for (pass.draws) |d| {
         defer offset += @intCast(std.mem.alignForward(usize, d.vertices.len, 4));
         if (d.pipeline >= pass.pipelines.len or d.vertex_count == 0) continue;
-        const built = g.pipeline(pass.pipelines[d.pipeline], samples, pass.stencil) catch |err| {
+        const slot = &built[d.pipeline];
+        if (slot.* == null) slot.* = g.pipeline(pass.pipelines[d.pipeline], samples, pass.stencil) catch |err| blk: {
             std.log.warn("gpu pipeline {d}: {t} ({s})", .{ d.pipeline, err, c.SDL_GetError() });
-            continue;
+            break :blk @as(?*c.SDL_GPUGraphicsPipeline, null);
         };
-        if (bound != built) {
-            c.SDL_BindGPUGraphicsPipeline(rp, built);
-            bound = built;
+        const chosen = slot.*.? orelse continue;
+        if (bound != chosen) {
+            c.SDL_BindGPUGraphicsPipeline(rp, chosen);
+            bound = chosen;
         }
         if (d.vertices.len > 0) c.SDL_BindGPUVertexBuffers(rp, 0, &.{ .buffer = g.vertices, .offset = offset }, 1);
         var bound_storage: [8]*c.SDL_GPUBuffer = undefined;
```

A second effect: a failing pipeline now warns once per pass, not once per draw. Another option
was caching the shader key by code pointer and length. I did not take it, because a reload can
map new code at the same address.

### fix2: skip redundant vertex-uniform push, stencil reference and scissor

SDL documents pushed uniforms as staying in effect for later draws in the command buffer, and
SDL's Vulkan backend keeps them across pipeline binds. The vector module passes one `viewport`
slice to every draw, so comparing pointer and length is enough.

```diff
--- a/src/host/gpu.zig
+++ b/src/host/gpu.zig
@@ -241,6 +241,10 @@
     }
     const rp = c.SDL_BeginGPURenderPass(cmd, &color, 1, if (pass.stencil) &depth else null) orelse return error.GpuRenderPass;
     var bound: ?*c.SDL_GPUGraphicsPipeline = null;
+    // State already set in this pass, so draws that repeat it skip the call.
+    var vertex_uniforms: ?[]const u8 = null;
+    var stencil_reference: ?u8 = null;
+    var scissor: ?c.SDL_Rect = null;
     var offset: u32 = 0;
     for (pass.draws) |d| {
         defer offset += @intCast(std.mem.alignForward(usize, d.vertices.len, 4));
@@ -259,12 +263,24 @@
         var bound_storage: [8]*c.SDL_GPUBuffer = undefined;
         if (d.vertex_storage.len > 0) c.SDL_BindGPUVertexStorageBuffers(rp, 0, &bound_storage, g.resolve(d.vertex_storage, &bound_storage) catch continue);
         if (d.fragment_storage.len > 0) c.SDL_BindGPUFragmentStorageBuffers(rp, 0, &bound_storage, g.resolve(d.fragment_storage, &bound_storage) catch continue);
-        if (d.vertex_uniforms.len > 0) c.SDL_PushGPUVertexUniformData(cmd, 0, d.vertex_uniforms.ptr, @intCast(d.vertex_uniforms.len));
+        // Pushed uniforms stay for later draws: the same bytes (sketches
+        // share one slice for a pass's viewport) need no second push.
+        if (d.vertex_uniforms.len > 0) if (vertex_uniforms == null or vertex_uniforms.?.ptr != d.vertex_uniforms.ptr or vertex_uniforms.?.len != d.vertex_uniforms.len) {
+            c.SDL_PushGPUVertexUniformData(cmd, 0, d.vertex_uniforms.ptr, @intCast(d.vertex_uniforms.len));
+            vertex_uniforms = d.vertex_uniforms;
+        };
         if (d.fragment_uniforms.len > 0) c.SDL_PushGPUFragmentUniformData(cmd, 0, d.fragment_uniforms.ptr, @intCast(d.fragment_uniforms.len));
-        if (pass.stencil) c.SDL_SetGPUStencilReference(rp, d.stencil_reference);
+        if (pass.stencil and stencil_reference != d.stencil_reference) {
+            c.SDL_SetGPUStencilReference(rp, d.stencil_reference);
+            stencil_reference = d.stencil_reference;
+        }
         const full: abi.Rect = .{ .w = @floatFromInt(pass.width), .h = @floatFromInt(pass.height) };
         const s = d.scissor orelse full;
-        c.SDL_SetGPUScissor(rp, &.{ .x = @intFromFloat(@max(s.x, 0)), .y = @intFromFloat(@max(s.y, 0)), .w = @intFromFloat(@max(s.w, 0)), .h = @intFromFloat(@max(s.h, 0)) });
+        const rect: c.SDL_Rect = .{ .x = @intFromFloat(@max(s.x, 0)), .y = @intFromFloat(@max(s.y, 0)), .w = @intFromFloat(@max(s.w, 0)), .h = @intFromFloat(@max(s.h, 0)) };
+        if (scissor == null or !std.meta.eql(scissor.?, rect)) {
+            c.SDL_SetGPUScissor(rp, &rect);
+            scissor = rect;
+        }
         c.SDL_DrawGPUPrimitives(rp, d.vertex_count, 1, 0, 0);
     }
     c.SDL_EndGPURenderPass(rp);
```

### fix3: one vertex-buffer binding; draws use `first_vertex`

```diff
--- a/src/host/gpu.zig
+++ b/src/host/gpu.zig
@@ -245,6 +245,7 @@
     var vertex_uniforms: ?[]const u8 = null;
     var stencil_reference: ?u8 = null;
     var scissor: ?c.SDL_Rect = null;
+    var vertex_base: ?u32 = null;
     var offset: u32 = 0;
     for (pass.draws) |d| {
         defer offset += @intCast(std.mem.alignForward(usize, d.vertices.len, 4));
@@ -259,7 +260,18 @@
             c.SDL_BindGPUGraphicsPipeline(rp, chosen);
             bound = chosen;
         }
-        if (d.vertices.len > 0) c.SDL_BindGPUVertexBuffers(rp, 0, &.{ .buffer = g.vertices, .offset = offset }, 1);
+        // Vertices at a multiple of the stride are reached from one binding
+        // at 0 by the draw's first vertex; others get a binding of their own.
+        var first: u32 = 0;
+        if (d.vertices.len > 0) {
+            const stride = pass.pipelines[d.pipeline].stride;
+            const base = if (stride > 0 and offset % stride == 0) 0 else offset;
+            if (base == 0 and stride > 0) first = offset / stride;
+            if (vertex_base != base) {
+                c.SDL_BindGPUVertexBuffers(rp, 0, &.{ .buffer = g.vertices, .offset = base }, 1);
+                vertex_base = base;
+            }
+        }
         var bound_storage: [8]*c.SDL_GPUBuffer = undefined;
         if (d.vertex_storage.len > 0) c.SDL_BindGPUVertexStorageBuffers(rp, 0, &bound_storage, g.resolve(d.vertex_storage, &bound_storage) catch continue);
         if (d.fragment_storage.len > 0) c.SDL_BindGPUFragmentStorageBuffers(rp, 0, &bound_storage, g.resolve(d.fragment_storage, &bound_storage) catch continue);
@@ -281,7 +293,7 @@
             c.SDL_SetGPUScissor(rp, &rect);
             scissor = rect;
         }
-        c.SDL_DrawGPUPrimitives(rp, d.vertex_count, 1, 0, 0);
+        c.SDL_DrawGPUPrimitives(rp, d.vertex_count, 1, first, 0);
     }
     c.SDL_EndGPURenderPass(rp);
     try g.submit(cmd, if (pass.label.len > 0) pass.label else "pass", start);
```

A pass that mixes strides can still land some draws at an offset that is not a multiple of
their stride. Those draws fall back to their own binding, as before. Aligning each draw's upload
offset to its stride would remove that case, but the vector module never hits it: all its
vertices are 8 bytes.

The patch files (`fix1.patch`, `fix2.patch`, `fix3.patch`, `instr-gpu.patch`, `instr-host.patch`)
are in `/tmp/claude-1000/-home-goblin-00-projects/b03e088f-d250-44e5-b526-5614572b9fb9/scratchpad/diffs/`.
fixN applies on top of fix(N−1); `zig fmt --check` passes.

## Fill test: is 8× MSAA plus stencil the GPU cost?

Setup: `vector/Gpu.zig` `samples = 1` (in a scratch build only, since reverted), host at fix3,
4 interleaved runs, GPU exec (submit to fence) in ms:

| N | 8× MSAA | 1× |
|---:|---|---|
| 1000 | 1.14 / 1.16 / 1.16 / 1.19 | 1.13 / 1.14 / 1.16 / 1.16 |
| 5000 | 6.64 / 6.64 / 6.83 / 6.97 | 6.29 / 6.38 / 7.83 / 7.90 |

Result: no difference. The GPU time is a per-draw cost of about 0.28–0.33 µs per draw on the
GPU front end, for state changes and draws. It is not samples or fill: the cover quads are about
17×17 px. `raw` draws the same picture in 0.10 ms of GPU time (N=1000), 11× less.

## Where the time goes now (gpu mode, after fix1+2+3, ReleaseFast)

| | N=1000 | N=5000 |
|---|---:|---:|
| wall per frame | 3.13 | 19.8 |
| sketch draw (Drawing builder, Polygons fill/stroke tessellation, `Gpu.record`, Paint blocks) | 0.73 | 5.2 |
| `Gpu.run` host: about 80% NVIDIA driver plus SDL_GPU per-draw work (pipeline bind, descriptor set fetch and update, uniform pushes, draw); the rest is vertex upload memcpy and submit | 2.37 | 14.6 |
| GPU exec (overlaps the CPU, never the limit here) | ≈1.15 | ≈6.6 |
| `raw`, same picture, wall | 0.095 | 0.32 |

In Debug at N=1000 (11.1 ms) it splits evenly: sketch tessellation is 6.1 ms (Debug Zig) and
`Gpu.run` is 4.8 ms (Debug SDL C plus the driver).

So in ReleaseFast the remaining cost is **draw-call overhead** (about 0.6–0.7 µs of CPU and 0.3 µs
of GPU per draw, 4 draws per boid, with a pipeline switch on every draw), then **CPU
tessellation** (about 0.7 µs per boid). **GPU fill with 8× MSAA plus stencil is not a factor.**
In Debug, after fix1, tessellation and draw overhead are about equal.

## What this means

**(a) The vector module's GPU backend.** The fixes keep the host from being pathological, but
the stencil-then-cover-per-fill design puts a floor of about 0.6 µs CPU per draw on any host
built on SDL_GPU. A pipeline switch per draw also makes SDL write new descriptor sets on every
draw. To beat the CPU renderer clearly on many small shapes, the backend needs fewer draws, not
a faster host:
1. Batch fills into one stencil draw plus one cover draw per group. For consecutive fills
   whose cover boxes do not overlap, the result is the same as drawing them one at a time,
   because the cover zeroes its own stencil. Paint then has to move from a per-draw uniform to
   per-vertex data or a storage buffer indexed per vertex (`fragment_storage` already exists).
   The 720-byte Paint uniform per cover (built, copied, then pushed) is the likely source of most of the 8% memcpy in the profile; flat profiles cannot confirm this.
2. Convex fast path: a single convex contour under either fill rule needs no stencil. Draw the
   fan directly with a per-vertex color, all in one pipeline and one draw. Boid fills qualify.
   Strokes qualify only when opaque, since overlapping stroke pieces would double-blend
   otherwise.
3. Keep 8× MSAA: it costs nothing measurable here.

**(b) The proposed `Command.shape`.** The `raw` numbers are what a batched-triangle command
costs: at N=1000, 0.035 ms of sketch time plus 0.05 ms host plus 0.10 ms GPU. That is about 33×
cheaper than the fixed vector GPU path and 40× cheaper than the CPU renderer. It scales close
to linearly to 0.32 ms at 5000. That supports the porter's and critic's call that many small
shapes want one batched triangle command with per-vertex color, not a different path renderer.
Caveat: `raw`'s edges are plain quads at 4× MSAA. They have no joins or exact coverage and
are fine only for opaque 1 px outlines. Exact strokes still need Polygons tessellation on the
CPU (0.7 µs per boid), which `Command.shape` could take directly.

For the flocking port itself (about 9 ms/frame, of which update is about 4.6 ms): with the fixes,
GPU drawing is 3.1 ms vs 4.0 for the CPU renderer. That is now ahead, but `raw` or
`Command.shape` would take drawing to about 0.1 ms, leaving the O(n²) update as the frame.