Playground: https://playground.babylonjs.com/?webgpu#NZYTE0#0
Babylon can already draw indirectly on WebGPU (enableIndirectDraw on the draw context), but the CPU always writes the arguments (setIndirectData, from the draw call’s own counts). We would like a mesh to draw with arguments the GPU writes: then a compute shader can cull its instances and decide how many are drawn, and the CPU never needs the count, so nothing is read back.
The proposal is one method, Mesh.setIndirectDrawBuffer(buffer: Nullable<DataBuffer>, byteOffset = 0). While a buffer is set, the mesh draws with drawIndexedIndirect (or drawIndirect when it is not indexed) on that buffer, and the CPU never writes the arguments. Other engines keep drawing with the CPU arguments. In our branch it takes about 60 lines in four files: the mesh keeps the buffer, _draw hands it to the draw context, and WebGPUEngine draws with it, also when it replays the render bundles of the non-compatibility mode.
The Playground does the same through the internals of the draw context. A compute shader tests the bounding sphere of each of 100,000 thin instances against the camera frustum, copies the matrices of the visible ones into a storage buffer the mesh uses as its instance matrices, and counts them atomically in the instance count of the arguments. While the camera turns, it is compared with culling on the CPU (the visible matrices uploaded every frame) and with no culling. Measured on 9.32.0 (Chromium 147, RTX 3080 Laptop GPU, Windows 11), per frame:
| Instances drawn | CPU time of the render (medians of two runs) | Of which, culling | Written to the GPU by the CPU | |
|---|---|---|---|---|
| No culling | 100,000 | 0.5 ms | – | 0.3 KB |
| Culled on the CPU | 18,913 on average | 2.7 and 2.3 ms | 2.4 and 2.0 ms | 1,182 KB |
| Culled on the GPU | decided on the GPU | 0.5 and 0.6 ms | 0.1 and 0.2 ms: the frustum planes and two dispatches | 0.4 KB |
At the same pose, the CPU and the GPU culling both keep 14,130 instances, and the two images are identical: 0 of 401,310 pixels differ. We did not time the GPU here; the instance counts show the vertex work the culling saves.
Would a PR adding Mesh.setIndirectDrawBuffer be welcome? It does not change anything for meshes that do not set a buffer.
Prepared by Mema, the AI of our studio 53ON (Claude in the 53ON harness, with MemoriaBios, our long-term memory), and reviewed by me.