public void TestBitonicSort() { int[] arr = { 3, 7, 4, 8, 6, 2, 1, 5 }; int n = arr.Length; int up = 1; var actual = sort.Sort(arr, n, up); var expected = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 }; Assert.Equal(expected, actual); }
public async Task Insert(Vector3[] data) { uint gx, gy, gz; shader.GetKernelThreadGroupSizes(computeLeavesKernel, out gx, out gy, out gz); int numGroupsX = Mathf.CeilToInt((float)data.Length / gx); using (var leaves = new StructuredBuffer <int>(data.Length)) using (var leafCount = new RawBuffer <uint>(1)) using (var keys = new CounterBuffer <int>(data.Length)) using (var points = new StructuredBuffer <Vector3>(data.Length)) { points.SetData(data); shader.SetFloats("size", bounds.size.x, bounds.size.y, bounds.size.z); shader.SetFloats("min_corner", bounds.min.x, bounds.min.y, bounds.min.z); shader.SetInt("max_depth", maxDepth); shader.SetInt("point_count", data.Length); shader.SetBuffer(computeLeavesKernel, "leaves", leaves.Buffer); shader.SetBuffer(computeLeavesKernel, "points", points.Buffer); shader.Dispatch(computeLeavesKernel, numGroupsX, 1, 1); sorter.Sort(leaves, data.Length); shader.SetBuffer(markUniqueLeavesKernel, "leaves", leaves.Buffer); shader.SetBuffer(markUniqueLeavesKernel, "unique", keys.Buffer); shader.Dispatch(markUniqueLeavesKernel, numGroupsX, 1, 1); compactor.Compact(leaves, keys, data.Length); keys.CopyCount(indirectArgs); shader.SetBuffer(computeArgsKernel, "args", indirectArgs.Buffer); shader.Dispatch(computeArgsKernel, 1, 1, 1); keys.CopyCount(leafCount); shader.SetBuffer(subdivideKernel, "leaf_count", leafCount.Buffer); shader.SetBuffer(subdivideKernel, "leaves", leaves.Buffer); shader.SetBuffer(subdivideKernel, "nodes", nodes.Buffer); for (int i = 0; i < maxDepth; i++) { shader.SetInt("current_level", i); shader.DispatchIndirect(subdivideKernel, indirectArgs.Buffer); } nodeData = await nodes.GetDataAsync(); nodeCount = (int)nodes.GetCounterValue(); } }
public void TestUnalignedSizes(int count) { using (StructuredBuffer <int> buffer = new StructuredBuffer <int>(count)) { int[] testData = new int[count]; for (int i = 0; i < count; i++) { testData[i] = count - 1 - i; } buffer.SetData(testData); BitonicSort sorter = new BitonicSort(); sorter.Sort(buffer, buffer.Count); int[] result = buffer.GetData(); for (int i = 0; i < count; i++) { Assert.AreEqual(result[i], i); } } }
public void GridSort(ref ComputeBuffer objectsBufferInput) { GridSortCS.SetInt("_NumParticles", numObjects); SetCSVariables(); int kernel = 0; #region GridOptimization // Build Grid kernel = GridSortCS.FindKernel("BuildGridCS"); GridSortCS.SetBuffer(kernel, "_ParticlesBufferRead", objectsBufferInput); GridSortCS.SetBuffer(kernel, "_GridBufferWrite", gridBuffer); GridSortCS.Dispatch(kernel, threadGroupSize, 1, 1); // Sort Grid bitonicSort.Sort(ref gridBuffer, ref gridPingPongBuffer); // Build Grid Indices kernel = GridSortCS.FindKernel("ClearGridIndicesCS"); GridSortCS.SetBuffer(kernel, "_GridIndicesBufferWrite", gridIndicesBuffer); GridSortCS.Dispatch(kernel, (int)(numGrid / SIMULATION_BLOCK_SIZE_FOR_GRID), 1, 1); kernel = GridSortCS.FindKernel("BuildGridIndicesCS"); GridSortCS.SetBuffer(kernel, "_GridBufferRead", gridBuffer); GridSortCS.SetBuffer(kernel, "_GridIndicesBufferWrite", gridIndicesBuffer); GridSortCS.Dispatch(kernel, threadGroupSize, 1, 1); // Rearrange kernel = GridSortCS.FindKernel("RearrangeParticlesCS"); GridSortCS.SetBuffer(kernel, "_GridBufferRead", gridBuffer); GridSortCS.SetBuffer(kernel, "_ParticlesBufferRead", objectsBufferInput); GridSortCS.SetBuffer(kernel, "_ParticlesBufferWrite", sortedObjectsBufferOutput); GridSortCS.Dispatch(kernel, threadGroupSize, 1, 1); #endregion GridOptimization // Copy buffer kernel = GridSortCS.FindKernel("CopyBuffer"); GridSortCS.SetBuffer(kernel, "_ParticlesBufferRead", sortedObjectsBufferOutput); GridSortCS.SetBuffer(kernel, "_ParticlesBufferWrite", objectsBufferInput); GridSortCS.Dispatch(kernel, threadGroupSize, 1, 1); }
public unsafe void BitonicSort() => BitonicSort <T> .Sort((int *)_arrayPtrs[_iterationIndex++], N);
public void GridSort(ref ComputeBuffer objectsBufferInput) { GridSortCS.SetInt("_NumParticles", numObjects); SetCSVariables(); int kernel = 0; #region GridOptimization // Build Grid kernel = GridSortCS.FindKernel("BuildGridCS"); GridSortCS.SetBuffer(kernel, "_ParticlesBufferRead", objectsBufferInput); GridSortCS.SetBuffer(kernel, "_GridBufferWrite", gridBuffer); GridSortCS.Dispatch(kernel, threadGroupSize, 1, 1); // Sort Grid bitonicSort.Sort(ref gridBuffer, ref gridPingPongBuffer); // Debug Test to see if bitonic sort works //var gridBufferTemp = new ParticleFlock.Uint2[numObjects]; //gridBuffer.GetData(gridBufferTemp); //int indice2Count = 0; //for (int i = 0; i < 2000; i++) //{ // if (gridBufferTemp[i].x == 2) // { // indice2Count++; // } //} //Debug.Log(indice2Count); // Build Grid Indices kernel = GridSortCS.FindKernel("ClearGridIndicesCS"); GridSortCS.SetBuffer(kernel, "_GridIndicesBufferWrite", gridIndicesBuffer); GridSortCS.Dispatch(kernel, (int)(numGrid / SIMULATION_BLOCK_SIZE_FOR_GRID), 1, 1); kernel = GridSortCS.FindKernel("BuildGridIndicesCS"); GridSortCS.SetBuffer(kernel, "_GridBufferRead", gridBuffer); GridSortCS.SetBuffer(kernel, "_GridIndicesBufferWrite", gridIndicesBuffer); GridSortCS.Dispatch(kernel, threadGroupSize, 1, 1); //Debug Test to see if GridIndeces works //var gridIndicesBufferTest = new ParticleFlock.Uint2[numGrid]; //gridIndicesBuffer.GetData(gridIndicesBufferTest); //Debug.Log(gridIndicesBufferTest[2].x); //Debug.Log(gridIndicesBufferTest[2].y); //Debug.Log("-----------------------------"); // Rearrange kernel = GridSortCS.FindKernel("RearrangeParticlesCS"); GridSortCS.SetBuffer(kernel, "_GridBufferRead", gridBuffer); GridSortCS.SetBuffer(kernel, "_ParticlesBufferRead", objectsBufferInput); GridSortCS.SetBuffer(kernel, "_ParticlesBufferWrite", sortedObjectsBufferOutput); GridSortCS.Dispatch(kernel, threadGroupSize, 1, 1); #endregion GridOptimization //Debug Test to see if rearrange particles work //var gridIndicesBufferTest = new ParticleFlock.Uint2[numGrid]; //gridIndicesBuffer.GetData(gridIndicesBufferTest); //uint totalFor1 = gridIndicesBufferTest[1].y - gridIndicesBufferTest[1].x; //var gridBufferTemp = new ParticleFlock.Uint2[numObjects]; //gridBuffer.GetData(gridBufferTemp); //int total1 = 0; //for (uint i = 0; i < 500; i++) //{ // Vector3 cellIndex = CalculateCell(sortedParticleBuffer[i].position); // Vector3Int cellIndexCasted = new Vector3Int((int)Mathf.Floor(cellIndex.x), (int)Mathf.Floor(cellIndex.y), (int)Mathf.Floor(cellIndex.z)); // int gridKey = GetGridKey(cellIndexCasted); // if (gridKey == 1) // { // total1++; // } //} //Debug.Log(totalFor1); //Debug.Log(total1); //Debug.Log("---------------"); kernel = GridSortCS.FindKernel("CopyBuffer"); GridSortCS.SetBuffer(kernel, "_ParticlesBufferRead", sortedObjectsBufferOutput); GridSortCS.SetBuffer(kernel, "_ParticlesBufferWrite", objectsBufferInput); GridSortCS.Dispatch(kernel, threadGroupSize, 1, 1); //Debug Test to see if copy particles works //var gridIndicesBufferTest = new ParticleFlock.Uint2[numGrid]; //gridIndicesBuffer.GetData(gridIndicesBufferTest); //for (int i = 1000; i < 1100; i++) //{ // Debug.Log(gridIndicesBufferTest[i].x); // Debug.Log(gridIndicesBufferTest[i].y); //} //Debug.Log("---------"); //uint totalFor1 = gridIndicesBufferTest[2].y - gridIndicesBufferTest[2].x; //uint start100 = gridIndicesBufferTest[7].y; //var gridBufferTemp = new ParticleFlock.Uint2[numObjects]; //gridBuffer.GetData(gridBufferTemp); //var sortedParticleBuffer = new ParticleFlock.ParticleBoid[numObjects]; //objectsBufferInput.GetData(sortedParticleBuffer); //int total1 = 0; //for (uint i = 0; i < 300; i++) //{ // Vector3 cellIndex = CalculateCell(sortedParticleBuffer[i].position); // Vector3Int cellIndexCasted = new Vector3Int((int)Mathf.Floor(cellIndex.x), (int)Mathf.Floor(cellIndex.y), (int)Mathf.Floor(cellIndex.z)); // int gridKey = GetGridKey(cellIndexCasted); // if (gridKey == 2) // { // total1++; // } //} //Debug.Log(totalFor1); //Debug.Log(total1); //var sortedParticleBuffer = new ParticleFlock.ParticleBoid[numObjects]; //sortedObjectsBufferOutput.GetData(sortedParticleBuffer); // Vector3 cellIndex = CalculateCell(sortedParticleBuffer[i].position); // Vector3Int cellIndexCasted = new Vector3Int((int)Mathf.Floor(cellIndex.x), (int)Mathf.Floor(cellIndex.y), (int)Mathf.Floor(cellIndex.z)); // int gridKey = GetGridKey(cellIndexCasted); //Debug.Log(GetGridKey()) }