예제 #1
0
        public void TestCompact(int count)
        {
            int[] data     = new int[count];
            int[] selected = new int[count];

            for (int i = 0; i < count; i++)
            {
                data[i]     = i;
                selected[i] = i % 2;
            }

            using (StructuredBuffer <int> buffer = new StructuredBuffer <int>(count))
                using (StructuredBuffer <int> keys = new StructuredBuffer <int>(count))
                {
                    buffer.SetData(data);
                    keys.SetData(selected);

                    ScanCompact compactor = new ScanCompact();
                    compactor.Compact(buffer, keys, buffer.Count);

                    int[] result = buffer.GetData();
                    for (int i = 0; i < count / 2; i++)
                    {
                        Assert.AreEqual(result[i], 2 * i + 1);
                    }
                }
        }
예제 #2
0
    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();
                    }
    }