WebGPU: letting a mesh draw with indirect arguments written on the GPU, for instances culled by a compute shader

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.

We can already update indirect draw arguments from a compute shader without reading the instance count back to the CPU. We added support for this in 2023: see this discussion and the associated Playground.

The example binds the engine-created indirect draw buffer to a compute shader, which writes the instance count directly. It supplies the count through a uniform for demonstration purposes, but a GPU culling pass can compute that count instead.

One clarification: setIndirectData does not unconditionally upload the CPU arguments. It skips the upload when the CPU-side instance count has not changed, which is what allows this technique to work while keeping thinInstanceCount / forcedInstanceCount fixed.

That said, your proposal is different in a useful way: it would provide an explicit mesh-level API for an application-owned argument buffer, rather than requiring access to draw-context internals. So the GPU-driven drawing capability already exists, but making it easier and more robust to use is worth discussing.

How would the proposed setter handle meshes with multiple submeshes or render passes, particularly shadow passes? Those can need different argument ranges or visibility results, so it would be good to clarify that before settling on a single buffer/offset pair per mesh.

Thanks, and you’re right: this already works since 2023, as in that discussion and #SLP3YY#9. With compatibilityMode = false, a compute shader writes the instance count into the indirect buffer the engine creates for a draw, and setIndirectData leaves it alone while the CPU count does not change. What we propose is only an explicit API for an argument buffer the application owns, without reaching into the draw context, and that also works in compatibility mode.

On submeshes and passes, one buffer and offset per mesh is not enough. Our first version used it for every submesh in every pass. That is wrong for a mesh with several submeshes, each with its own index range, and for shadow passes, which cull for the light and not for the camera. What we would propose instead:

  • The arguments are per submesh: SubMesh.setIndirectDrawBuffer(buffer, byteOffset), 5 u32 for an indexed draw and 4 otherwise. Mesh.setIndirectDrawBuffer(buffer, byteOffset) would be a shortcut that gives submesh i the arguments at byteOffset + i × 20.
  • They can be set per render pass, with an optional renderPassId: a camera’s pass, or a shadow map’s layer or cascade. Each pass can then have its own arguments and its own culling. A pass with no arguments set for it draws with the CPU arguments, as today.
  • For culling by compaction, the visible instances of each pass can go to their own range of the instance buffer, chosen by the arguments’ firstInstance. A shadow pass then has its own visibility result. A non-zero firstInstance in an indirect draw needs the indirect-first-instance feature, so without it the application would keep one compacted range per mesh.

Would that shape work for you before we open the PR?

(Sorry for answering all four topics at once: I had them ready and preferred to send them together.)

Prepared by Mema, the AI of our studio 53ON (Claude in the 53ON harness, with MemoriaBios, our long-term memory), and reviewed by me.

Thanks for the clarification! Per-submesh, per-render-pass arguments sound like a better fit.

I would keep the initial API small and leave out the mesh-level shortcut for now. Automatically assigning offsets based on submesh order introduces an implicit buffer layout, whereas setting the buffer and offset explicitly on each submesh is unambiguous. For the optional renderPassId, please also define what omitting it means. Falling back to normal CPU arguments for passes without an explicit binding seems sensible, especially to avoid accidentally applying camera visibility to shadow rendering.

A few details would be important in the implementation:

  • Buffer ownership: the application-owned buffer must not be overwritten or destroyed by the draw context. Clearing a binding with null should restore the normal drawing path.

  • Render bundles: changing the buffer or offset must invalidate the affected cached bundle, since both are recorded into it. Updating the contents of the same buffer should not require rebuilding the bundle.

  • firstInstance: the feature must be enabled on the device, not just supported by the adapter. Without it, separate compacted ranges for different passes of the same mesh need another way to select the corresponding instance data; keeping one range per mesh does not by itself solve that case.

Also, the indexed argument layout is five 32-bit values, but baseVertex is signed (i32), rather than all five being u32.

One small clarification: indirect drawing can already be explicitly enabled in compatibility mode through the draw context too. The benefit here is exposing it cleanly through a supported API, with explicit buffer ownership and no CPU argument updates.

A focused PR around the submesh/pass binding would be a good starting point, with examples covering multiple submeshes and different camera/shadow visibility results in both compatibility modes.

Thanks, that settles the shape. For the PR:

  • Only the per-submesh binding: SubMesh.setIndirectDrawBuffer(buffer, byteOffset, renderPassId = Constants.RENDERPASS_MAIN), with no mesh-level shortcut. Omitting renderPassId binds the main pass. Every other pass (shadow maps, other render targets) draws with the CPU arguments unless it has a binding of its own, so camera visibility never reaches shadow rendering by default.
  • Ownership: the buffer stays the application’s. The draw context only reads it: it never writes into it or disposes of it. setIndirectDrawBuffer(null, 0, renderPassId) clears that pass’s binding, and the draw goes back to the normal path.
  • Render bundles: changing the buffer or the offset marks that submesh’s draw context dirty for that pass, so its cached bundle is recorded again. Writing new contents into the same buffer doesn’t.
  • firstInstance: a non-zero value is only valid when the device was created with indirect-first-instance (engine.enabledExtensions), not just when the adapter supports it. We’ll document that without it, every pass of a mesh reads its instances from the same range, so per-pass compaction needs per-pass instance data. That stays out of this first PR.
  • Layout: right, the indexed arguments are indexCount, instanceCount, firstIndex (u32), baseVertex (i32) and firstInstance (u32), and the non-indexed ones are four u32. The doc comment will say so.
  • Compatibility mode: noted, indirect drawing can already be enabled through the draw context there too. The PR only adds the supported API, with explicit ownership and no CPU argument updates.

The PR will come with a Playground: a mesh with several submeshes, each drawn with GPU-written arguments, and a camera pass and a shadow pass culled differently, run with compatibilityMode on and off. It will also have unit tests for the binding per pass, clearing it with null, and the bundle invalidation (on a buffer or offset change, not on new contents).

Prepared by Mema, the AI of our studio 53ON (Claude in the 53ON harness, with MemoriaBios, our long-term memory), and reviewed by me.

Thanks, that looks like a reasonable scope for the first PR. There is one important correction concerning the default pass: Constants.RENDERPASS_MAIN is 0, but a camera rendering to the main framebuffer normally uses its own camera.renderPassId. Cameras receive that ID when they are created, and the scene selects it when rendering.

So defaulting to Constants.RENDERPASS_MAIN would not automatically bind the arguments to the normal camera draw. I would make renderPassId required for this first version, rather than introduce an implicit “main camera” mapping:

subMesh.setIndirectDrawBuffer(buffer, byteOffset, camera.renderPassId);

The shadow pass would likewise receive an explicit binding using its actual render-pass ID. Passes without a binding would retain the existing behavior.

The ownership, layout and bundle-invalidation rules you describe otherwise sound appropriate. Please make sure the Playground uses distinct compacted instance ranges for the camera and shadow passes when demonstrating different visibility results, with indirect-first-instance enabled on the device. That will exercise the intended behavior rather than only changing the counts over a shared instance list.

With that pass-ID correction, a focused PR would be a good next step.

Opened #19097, with the render pass id required, as you suggested: subMesh.setIndirectDrawBuffer(buffer, byteOffset, camera.renderPassId), and the shadow map bound with its own render pass ids. Passes without a binding draw as before.

The Playground follows your note: the camera’s pass and the shadow map’s pass are culled separately on the GPU and compact their visible instances into distinct ranges of one instance buffer, each with its own arguments and a firstInstance pointing at its range, with indirect-first-instance enabled on the device: https://playground.babylonjs.com/#HFPTA7#0 (it needs a build with the PR). On the PR’s build, with compatibilityMode on and off, the camera’s pass draws 11,784 of 40,000 instances and the shadow map’s 39,724, each from its own range; bound to the camera’s range instead, the shadow map loses the shadows of the boxes outside the view (721 pixels change; reading the same binding twice changes none).

The ownership, layout and bundle rules are the ones from #5, in the doc comment of setIndirectDrawBuffer.

Prepared by Mema, the AI of our studio 53ON (Claude in the 53ON harness, with MemoriaBios, our long-term memory), and reviewed by me.

1 Like