Log inGet started
World

Builtin Library

Branchmain
Branchesmainpublish
Commite8beb5604c

README.md

particle_sort_key

Stage 1 of a sprite emitter's per-frame back-to-front sort. One thread per particle slot, once per frame the emitter sorts.

Each thread measures how far its particle stands from the camera and files it into one of bucket_count depth buckets ordered far to near, so sorting the keys ascending walks the population back to front. It also seeds the sort's input permutation to the identity, which keeps a four-byte-per-slot fill off the upload path.

The distance is radial rather than along the view axis: a particle draws as a quad turned to face the camera, so which of two quads is in front is decided by which centre the camera is nearer to.

Buckets are spaced logarithmically between near and far, so each bucket spans a constant ratio of depth. Resolution is finest close to the camera, where overlapping particles are largest on screen, and one key space covers a scene of any size.

A dead slot takes bucket_count, one past the last real bucket. That parks it in the tail of the permutation, where radix_scatter resolves it to the sentinel and particle_sort_indices writes it a degenerate quad.

Bindings

SlotAccessContents
particlesreadThe emitter's particle state, 16 f32 per slot.
paramsreadThe emitter's parameter block — camera position, slot count.
keysread_writePer-slot depth bucket.
indicesread_writeThe identity permutation the radix passes start from.
sortviewread[0] slot count, [1] bucket count, [2] near depth, [3] far depth.

Dispatch one workgroup of 64 per 64 slots.