Porting DXSDK GPU Bitonic Sort to Unity

Good night fellas,

As an academic exercise to test the feasibility of Unity as a research platform, I’m in the process of porting over the GPGPU Smoothed Particle Hydrodynamics sample using DirectCompute from the DirectX SDK.

In order to determine the contribution to the density and the forces exerted over a particle by it’s neighbors, I have an spatial hash structure where neighbors list are stored in a vector. Each of the elements of the vector consists of a key-value tuple with the particle id and the computed hash for the particle’s position. This part seems to work properly when comparing it by my CPU-generated results. However, in order to able to use the list and generate the offsets table that point to the beginning of each bucket, I need to sort the vector by the computed hash.

Therefore I have ported the Bitonic Sort provided in the same SDK over to Unity, but I’m having troubles making it work. Seems like it’s only sorting the values that are within the bitonic group size (currently 512 elements). As I’m not too experienced with the DX11 API, I might have screwed up translating the DX11 API calls but they seem OK to me.

I’ve spent a bunch of hours struggling with it and I’m getting close to the point of desperation. So here’s my cry for help: have any of you guys successfully ported the Bitonic sort, or has a ready-to-use implementation of a sorting algorithm for DirectCompute?

Any help is welcome, as there doesn’t seem to be many samples out there and even less properly documented. [13412-gpusort.zip|13412] It includes a simple test case MonoBehaviour, just create an empty scene and attach the test script to an empty gameobject. The results are written into the debug window.

Thanks for the help!

Solved. Seems like not even the original sorting algorithm worked properly. I managed to tweak the comparison condition a bit so it’s sorted in descending order and afterwards rearranged as a temporal workaround.

Finally got this thing working!

It might be too late for this answer but on the documentation page for the Bitonic sort with DirectCompute I found those two lines…

// The number of elements to sort is limited to an even power of 2 \

// At minimum 8,192 elements - BITONIC_BLOCK_SIZE * TRANSPOSE_BLOCK_SIZE

// At maximum 262,144 elements - BITONIC_BLOCK_SIZE * BITONIC_BLOCK_SIZE

When I set the number of elements to 8192 your algorithm works fine on my machine… :slight_smile:

It might be too late for this answer but on the documentation page for the Bitonic sort with DirectCompute I found those comments…

// At minimum 8,192 elements - BITONIC_BLOCK_SIZE * TRANSPOSE_BLOCK_SIZE

// At maximum 262,144 elements - BITONIC_BLOCK_SIZE * BITONIC_BLOCK_SIZE

When I set the number of elements to 8192, your code works fine on my machine… :slight_smile: