If you need the correct index per-thread, as you do when you're going to write the samples to the buffer, there's some more wave-ops involved, since you also need to calculate the local offset for each thread on the wave.
WaveReadLaneFirst/WavePrefixCountBits sorts you out, here is how it'd look: