Builtin Library
- ..
- README.md1.9 KB
- bindings.yaml1.0 KB
- shader.wgsl4.7 KB
README.md
particle_sort_local
A sprite emitter's whole back-to-front sort in one dispatch, for a population of up to 2048 live particles. One workgroup of 256 threads, once per frame the emitter sorts and its live particles fit.
The threads walk every slot, give each live particle its depth key
(particle_sort_depth) and gather it into workgroup memory as one word: the key
in the high bits and the slot in the low sortlocal[0] bits. A bitonic network
sorts those words, sized to the next power of two at or above the live count, so
the population comes out ordered by key and, inside one key, by slot: the same
total order the radix path (particle_sort_key, three gpuSort passes per
eight bits of key, particle_sort_indices) produces.
The sorted words are written out as the emitter's index buffer. Run n takes the
two triangles of the n-th particle, and every run past the live count is six
copies of index 0, so the buffer holds exactly what the radix path leaves. Only
the runs between this write's live count and sortlocal[1] (the runs the
previous write may have left holding triangles) are cleared, so the write costs
the live population rather than the emitter's capacity.
An emitter takes this path when the slot and the key fit one 32-bit word together and its live count is at most 2048; past that, the radix path orders it.
Bindings
| Slot | Access | Contents |
|---|---|---|
particles | read | The emitter's particle state, 16 f32 per slot. |
params | read | The emitter's parameter block: the camera position. |
out_indices | read_write | The emitter's index buffer, six indices per slot. |
sortview | read | [0] slot count, [1] bucket count, [2] near, [3] far. |
sortlocal | read_write | [0] slot bits, [1] runs that may hold triangles, [2] live particles found. |
Dispatch exactly one workgroup.