Repository navigation
Description
Summary
For a GPU (!) call, how deep the parallel fork goes before switching to
straight-line work changes the frame time by up to 8x. The same sweep on the CPU
is flat, so the variable is invisible unless you happen to test on the device.
Nothing in the guide mentions tuning it, and the one piece of guidance I found —
the Cell.fork comment in demos/app_slash_boss_3d/bend3d.bend, "a fork per
pixel drowns in scheduling, a fork per cell leaves lanes idle" — steered me to
the coarse end, which is the wrong end for a raycaster.
Measurements
A voxel raycaster at 512². The benchmark sums every pixel colour and prints the
checksum, so the rows are verified to render the same image. 40 frames per run,
median of 5, IO.now() inside the process.
| fork depth | leaf | --gpu 2GB |
--gpu off, 10 threads |
|---|---|---|---|
| 6 levels | 8×8 tile | 14 (14–16) | 19 (18–19) |
| 7 levels | 4×4 tile | 6 (6–8) | 20 (19–20) |
| 8 levels | 2×2 tile | 8 (8–9) | 20 (20–21) |
| 9 levels | 1 pixel | 7 (7–8) | 21 (20–21) |
An interactive voxel world at 128², which also reads a shared Data quadtree
per DDA step. Checksum 747218888 on every row:
| fork depth | leaf | --gpu 2GB |
|---|---|---|
| 4 levels | 8×8 tile | 234 (232–236) |
| 5 levels | 4×4 tile | 87 (87) |
| 6 levels | 2×2 tile | 28 (28–34) |
--gpu off on 10 threads: 178 ms. So at the coarse granularity that program was
slower on the GPU than on the CPU, and 8.4x slower than the same program
forked two levels deeper.
What is surprising
- The CPU column is flat, so a CPU-side experiment reports "granularity does not
matter" and is wrong about the device. I drew exactly that conclusion and
shipped it. - The optimum is not universal: the raycaster wants a 4×4 leaf, the voxel world
wants 2×2. It has to be swept per program. - Granularity is necessary but not sufficient. A third program forks per pixel
and still does not win (48 with!vs 43 on 10 threads) because it lacks a
ray/box clip, so cheap rays never get cut.
Suggested fix
A paragraph in the guide saying that fork depth for ! is a tuning parameter,
that the CPU will not reveal it, and that the useful range is a leaf of a few
pixels rather than a tile. If bend3d.bend is the reference people are pointed
to, the Cell.fork comment could note that binning per tile pays when the tile
shares work (its triangle list), and not when every leaf is independent.
I am not reporting this as a bug — the scheduler behaving this way is
reasonable. It is that the one number that mattered most for my GPU performance
is undocumented and invisible from the CPU.
Environment
Bend 2.0.6, macOS 26.6.2, Apple M5 (10 CPU / 8 GPU cores), Metal.
Full sources: https://github.com/AdrielSantana/metal-bending
Activity
AdrielSantana commentedon Sep 18, 2026
Re-verified on 2.0.9 (I was on 2.0.6 when I filed). Everything reproduces,
and the sources changed slightly in between — the per-pixel read now picks its
quadrant with nested
Bool.pickinstead of amatchhelper, which is why theabsolute numbers moved a little.
Raycaster, 512², 40 frames per run, median of 5. Checksum
1173697204on everyrow, so all of them render the same image:
--gpu 2GB--gpu off, 10 threads!, 10 threadsVoxel world, 128², checksum
747218888on every row:--gpu 2GB--gpu off, 10 threadsSo on 2.0.9 the coarse-granularity version is still slower with the GPU than
without it (202 vs 177), and still 7.8x slower than the same program forked two
levels deeper. The CPU columns remain flat throughout.
The guide names --gpu off, points the Parallelism section at the shad…
VictorTaelin commentedon Sep 19, 2026
Not a bug: your numbers fit the lane cube exactly. A bang runs 16384 lanes (128 x 128), so 4^7 leaves is the target: at 512^2 with 4x4 tiles, 7 levels = the cube (6 ms); 6 levels = a quarter (14 ms); 8-9 levels cost an extra kernel iteration each. At 128^2, 4 levels keep 256 lanes of 16384 busy (234 ms), 5 keep 1024 (87 ms), 6 keep 4096 (28 ms). The CPU pool is flat because 10 workers are fed by any of those leaf counts, so it cannot tune this.
bend guide shaders(2.0.10) states the model, and 2.0.13 adds the rule of thumb you asked for: aim for 4^7 leaves per bang and sweep one level either side on the device.Note: this reply was written by an AI after it reported the issue to me and I made the decision. If anything here is wrong, reply and I will review it myself.
The guide names --gpu off, points the Parallelism section at the shad…