Ejemplo n.º 1
0
        public void MinMaxShaderWorksForOneBlock()
        {
            int width  = blockDim.x;
            int height = blockDim.y;
            int depth  = blockDim.z;
            int size   = width * height * depth;

            float[] voxels = new float[size];
            for (int i = 0; i < size; i++)
            {
                voxels[i] = 0.0f;
            }

            voxels[0] = 1.0f;
            voxels[1] = .5f;
            voxels[2] = -.1f;
            voxels[7 + 3 * width + 3 * width * height] = -.1f;

            ComputeBuffer minMaxBuffer = new ComputeBuffer(1, sizeof(float) * 2);
            ComputeBuffer voxelBuffer  = new ComputeBuffer(voxels.Length, sizeof(float));

            voxelBuffer.SetData(voxels);

            shader.SetInts("numBlocks", new int[] { 1, 1, 1 });
            shader.SetInts("size", new int[] { width, height, depth });
            shader.SetBuffer(minMaxKernelIndex, "minMaxBuffer", minMaxBuffer);
            shader.SetBuffer(minMaxKernelIndex, "voxelBuffer", voxelBuffer);

            shader.Dispatch(minMaxKernelIndex, 1, 1, 1);

            MinMaxPair[] result = new MinMaxPair[1];
            minMaxBuffer.GetData(result);

            Assert.AreEqual(1, result.Length);
            foreach (MinMaxPair pair in result)
            {
                Assert.AreEqual(-.1f, pair.min);
                Assert.AreEqual(1.0f, pair.max);
            }

            minMaxBuffer.Release();
            voxelBuffer.Release();
        }
Ejemplo n.º 2
0
        public void MinMaxShaderWorksForMisalignedData()
        {
            int blockMultiplier = 4;
            int width           = (blockDim.x - 1) * blockMultiplier + 5;
            int height          = (blockDim.y - 1) * blockMultiplier + 2;
            int depth           = (blockDim.z - 1) * blockMultiplier + 3;
            int size            = width * height * depth;

            float[] voxels = new float[size];
            for (int i = 0; i < size; i++)
            {
                voxels[i] = 0.0f;
            }

            MinMaxPair[] expectedValues = new MinMaxPair[Mathf.CeilToInt((float)width / (blockDim.x - 1)) * Mathf.CeilToInt((float)height / (blockDim.y - 1)) * Mathf.CeilToInt((float)depth / (blockDim.z - 1))];

            for (int i = 0; i < expectedValues.Length; i++)
            {
                expectedValues[i].min = (float)-i * 10;
                expectedValues[i].max = (float)i * 10;
            }

            int blockIdx = 0;

            for (int z = 1; z < depth; z += blockDim.z - 1)
            {
                for (int y = 1; y < height; y += blockDim.y - 1)
                {
                    for (int x = 1; x < width; x += blockDim.x - 1)
                    {
                        voxels[x + y * width + z * width * height] = expectedValues[blockIdx].min;

                        int maxBlockX = Math.Min(x + blockDim.x - 2, width) - 1;
                        int maxBlockY = Math.Min(y + blockDim.y - 2, height) - 1;
                        int maxBlockZ = Math.Min(z + blockDim.z - 2, depth) - 1;
                        voxels[maxBlockX + maxBlockY * width + maxBlockZ * width * height] = expectedValues[blockIdx].max;

                        blockIdx++;
                    }
                }
            }

            ComputeBuffer minMaxBuffer = new ComputeBuffer(expectedValues.Length, sizeof(float) * 2);
            ComputeBuffer voxelBuffer  = new ComputeBuffer(voxels.Length, sizeof(float));

            voxelBuffer.SetData(voxels);

            shader.SetInts("numBlocks", new int[] { blockMultiplier + 1, blockMultiplier + 1, blockMultiplier + 1 });
            shader.SetInts("size", new int[] { width, height, depth });
            shader.SetBuffer(minMaxKernelIndex, "minMaxBuffer", minMaxBuffer);
            shader.SetBuffer(minMaxKernelIndex, "voxelBuffer", voxelBuffer);

            shader.Dispatch(minMaxKernelIndex, blockMultiplier + 1, blockMultiplier + 1, blockMultiplier + 1);

            MinMaxPair[] result = new MinMaxPair[expectedValues.Length];
            minMaxBuffer.GetData(result);

            Assert.AreEqual(expectedValues.Length, result.Length);
            for (int i = 0; i < result.Length; i++)
            {
                Assert.AreEqual(expectedValues[i].min, result[i].min);
                Assert.AreEqual(expectedValues[i].max, result[i].max);
            }

            minMaxBuffer.Release();
            voxelBuffer.Release();
        }
Ejemplo n.º 3
0
        public void MinMaxShaderWorksForMultipleBlocks()
        {
            int blockMultiplier = 4;
            int width           = (blockDim.x - 1) * blockMultiplier + 1;
            int height          = (blockDim.y - 1) * blockMultiplier + 1;
            int depth           = (blockDim.z - 1) * blockMultiplier + 1;
            int size            = width * height * depth;

            float[] voxels = new float[size];
            for (int i = 0; i < size; i++)
            {
                voxels[i] = 0.0f;
            }

            MinMaxPair[] expectedValues = new MinMaxPair[blockMultiplier * blockMultiplier * blockMultiplier];

            for (int i = 0; i < expectedValues.Length; i++)
            {
                expectedValues[i].min = (float)-i * 10;
                expectedValues[i].max = (float)i * 10;
            }

            int blockIdx = 0;

            for (int z = 1; z < depth - 1; z += blockDim.z - 1)
            {
                for (int y = 1; y < height - 1; y += blockDim.y - 1)
                {
                    for (int x = 1; x < width - 1; x += blockDim.x - 1)
                    {
                        voxels[x + y * width + z * width * height] = expectedValues[blockIdx].min;
                        voxels[(x + blockDim.x - 2) + (y + blockDim.y - 2) * width + (z + blockDim.z - 2) * width * height] = expectedValues[blockIdx].max;
                        blockIdx++;
                    }
                }
            }

            ComputeBuffer minMaxBuffer = new ComputeBuffer(expectedValues.Length, sizeof(float) * 2);
            ComputeBuffer voxelBuffer  = new ComputeBuffer(voxels.Length, sizeof(float));

            voxelBuffer.SetData(voxels);

            shader.SetInts("numBlocks", new int[] { blockMultiplier, blockMultiplier, blockMultiplier });
            shader.SetInts("size", new int[] { width, height, depth });
            shader.SetBuffer(minMaxKernelIndex, "minMaxBuffer", minMaxBuffer);
            shader.SetBuffer(minMaxKernelIndex, "voxelBuffer", voxelBuffer);

            shader.Dispatch(minMaxKernelIndex, blockMultiplier, blockMultiplier, blockMultiplier);

            MinMaxPair[] result = new MinMaxPair[expectedValues.Length];
            minMaxBuffer.GetData(result);

            Assert.AreEqual(expectedValues.Length, result.Length);
            for (int i = 0; i < result.Length; i++)
            {
                Assert.AreEqual(expectedValues[i].min, result[i].min);
                Assert.AreEqual(expectedValues[i].max, result[i].max);
            }

            minMaxBuffer.Release();
            voxelBuffer.Release();
        }