Ejemplo n.º 1
0
 private void Build()
 {
     if (buf != null && graph != null)
     {
         buf.Clear();
         buf.AddRange(values);
         graph.Peak  = Mathf.Max(Mathf.Abs(values.Max()), Mathf.Abs(values.Min()));
         graph.Input = buf;
     }
 }
Ejemplo n.º 2
0
        protected virtual void Rebuild()
        {
            vout.Clear();
            vin.Clear();
            indices.Clear();
            barycentric.Clear();
            foreach (var t in triangles)
            {
                var offset = vout.Count;
                vout.AddRange(t.VertexOutput);
                vin.AddRange(t.VertexInput);
                indices.AddRange(t.Indices.Select(i => i + offset));
                barycentric.AddRange(t.BarycentricWeights);
            }

            ReleaseBlendTex();
        }
Ejemplo n.º 3
0
        public byte[] FilterGPU(byte[] bgra, ushort[] depth, DepthSpacePoint[] depthSpaceData,
                                int nearThresh, int farThresh, int haloSize)
        {
            if (computeShader == null)
            {
                return(new byte[0]);
            }

            // Initialize last frame with current color frame, if it was reset
            if (bLastFrameReset)
            {
                lastFramePixels = bgra;
                bLastFrameReset = false;
            }

            // -- Create halo array --

            List <int2> halo = new List <int2>();

            int s              = haloSize;
            int xd             = s;
            int yd             = s / 2;
            int S              = (xd + yd) / 2;
            int x0             = -xd;
            int x1             = +xd;
            int y0             = -yd;
            int y1             = +yd;
            int actualHaloSize = 0;

            for (int y = y0; y < y1; ++y)
            {
                for (int x = x0; x < x1; ++x)
                {
                    if (Math.Abs(x) + Math.Abs(y) <= S)
                    {
                        halo.Add(new int2(x, y));
                        ++actualHaloSize;
                    }
                }
            }

            // --

            // -- Perform data transformations so the arrays can be passed to the GPU --

            var bgraDataTransformed = new int4[1920 * 1080];

            for (int i = 0, j = 0; i < bgra.Length; i += 4, ++j)
            {
                bgraDataTransformed[j] = new int4(bgra[i], bgra[i + 1], bgra[i + 2], bgra[i + 3]);
            }

            var lastFrameDataTransformed = new int4[1920 * 1080];

            for (int i = 0, j = 0; i < bgra.Length; i += 4, ++j)
            {
                lastFrameDataTransformed[j] = new int4(lastFramePixels[i], lastFramePixels[i + 1], lastFramePixels[i + 2], lastFramePixels[i + 3]);
            }

            // --

            //var sw = Stopwatch.StartNew();

            // Create a constant buffer to pass the filter configuration
            var cbuffer = GPGPUHelper.CreateConstantBuffer(device, new int[] { nearThresh, farThresh, haloSize });

            // -- Create GPULists using the immediate context and pass the data --

            GPUList <int4> bgraData = new GPUList <int4>(device.ImmediateContext);

            bgraData.AddRange(bgraDataTransformed);

            GPUList <uint> depthData = new GPUList <uint>(device.ImmediateContext);

            depthData.AddRange(depth.Select(d => (uint)d));

            GPUList <DepthSpacePoint> depthSpacePointData = new GPUList <DepthSpacePoint>(device.ImmediateContext, depthSpaceData);
            //depthSpacePointData.AddRange(depthSpaceData.Select(dsp => {

            //    if (dsp.X == float.NegativeInfinity || dsp.Y == -float.NegativeInfinity)
            //    {
            //        return new DepthSpacePoint() { X = -1, Y = -1 };
            //    }
            //    else
            //    {
            //        return dsp;
            //    }
            //}));

            GPUList <int4> lastFrameData = new GPUList <int4>(device.ImmediateContext);

            lastFrameData.AddRange(lastFrameDataTransformed);

            var            resultArray = new int4[1920 * 1080];
            GPUList <int4> resultData  = new GPUList <int4>(device.ImmediateContext, resultArray);

            GPUList <int2> haloData = new GPUList <int2>(device.ImmediateContext, halo);

            // --

            var sw = Stopwatch.StartNew();

            // Set the buffers and uavs
            device.ImmediateContext.ComputeShader.Set(computeShader);
            device.ImmediateContext.ComputeShader.SetConstantBuffer(cbuffer, 0);
            device.ImmediateContext.ComputeShader.SetUnorderedAccessView(bgraData.UnorderedAccess, 0);
            device.ImmediateContext.ComputeShader.SetUnorderedAccessView(depthData.UnorderedAccess, 1);
            device.ImmediateContext.ComputeShader.SetUnorderedAccessView(depthSpacePointData.UnorderedAccess, 2);
            device.ImmediateContext.ComputeShader.SetUnorderedAccessView(lastFrameData.UnorderedAccess, 3);
            device.ImmediateContext.ComputeShader.SetUnorderedAccessView(resultData.UnorderedAccess, 4);
            device.ImmediateContext.ComputeShader.SetUnorderedAccessView(haloData.UnorderedAccess, 5);

            // Run the compute shader
            device.ImmediateContext.Dispatch(1920 * 1080 / 256, 1, 1);

            // Get result. This call blocks, until the result was calculated
            // because the MapSubresource call waits.
            var result = resultData.ToArray();

            sw.Stop();

            // -- Clean up --

            device.ImmediateContext.ComputeShader.SetConstantBuffer(null, 0);
            device.ImmediateContext.ComputeShader.SetUnorderedAccessView(null, 0);
            device.ImmediateContext.ComputeShader.SetUnorderedAccessView(null, 1);
            device.ImmediateContext.ComputeShader.SetUnorderedAccessView(null, 2);
            device.ImmediateContext.ComputeShader.SetUnorderedAccessView(null, 3);
            device.ImmediateContext.ComputeShader.SetUnorderedAccessView(null, 4);
            device.ImmediateContext.ComputeShader.SetUnorderedAccessView(null, 5);

            cbuffer.Dispose();
            bgraData.Dispose();
            depthData.Dispose();
            depthSpacePointData.Dispose();
            lastFrameData.Dispose();
            resultData.Dispose();
            haloData.Dispose();

            // --

            Debug.WriteLine($"Filtering took {sw.ElapsedMilliseconds} ms");

            var resultBytes = new byte[1920 * 1080 * 4];

            for (int i = 0, j = 0; i < resultBytes.Length; i += 4, ++j)
            {
                resultBytes[i]     = (byte)result[j].x;
                resultBytes[i + 1] = (byte)result[j].y;
                resultBytes[i + 2] = (byte)result[j].z;
                resultBytes[i + 3] = (byte)result[j].a;
            }

            lastFramePixels = resultBytes;

            return(resultBytes);
        }
Ejemplo n.º 4
0
        public byte[] FilterGPU(byte[] bgra, ushort[] depth, DepthSpacePoint[] depthSpaceData,
            int nearThresh, int farThresh, int haloSize)
        {
            if (computeShader == null)
            {
                return new byte[0];
            }

            // Initialize last frame with current color frame, if it was reset
            if (bLastFrameReset)
            {
                lastFramePixels = bgra;
                bLastFrameReset = false;
            }

            // -- Create halo array --

            List<int2> halo = new List<int2>();

            int s = haloSize;
            int xd = s;
            int yd = s / 2;
            int S = (xd + yd) / 2;
            int x0 = -xd;
            int x1 = +xd;
            int y0 = -yd;
            int y1 = +yd;
            int actualHaloSize = 0;
            for (int y = y0; y < y1; ++y)
            {
                for (int x = x0; x < x1; ++x)
                {
                    if (Math.Abs(x) + Math.Abs(y) <= S)
                    {
                        halo.Add(new int2(x, y));
                        ++actualHaloSize;
                    }
                }
            }

            // --

            // -- Perform data transformations so the arrays can be passed to the GPU --

            var bgraDataTransformed = new int4[1920 * 1080];
            for (int i = 0, j = 0; i < bgra.Length; i += 4, ++j)
            {
                bgraDataTransformed[j] = new int4(bgra[i], bgra[i + 1], bgra[i + 2], bgra[i + 3]);
            }

            var lastFrameDataTransformed = new int4[1920 * 1080];
            for (int i = 0, j = 0; i < bgra.Length; i += 4, ++j)
            {
                lastFrameDataTransformed[j] = new int4(lastFramePixels[i], lastFramePixels[i + 1], lastFramePixels[i + 2], lastFramePixels[i + 3]);
            }

            // --

            //var sw = Stopwatch.StartNew();

            // Create a constant buffer to pass the filter configuration
            var cbuffer = GPGPUHelper.CreateConstantBuffer(device, new int[] { nearThresh, farThresh, haloSize });

            // -- Create GPULists using the immediate context and pass the data --

            GPUList<int4> bgraData = new GPUList<int4>(device.ImmediateContext);
            bgraData.AddRange(bgraDataTransformed);

            GPUList<uint> depthData = new GPUList<uint>(device.ImmediateContext);
            depthData.AddRange(depth.Select(d => (uint)d));

            GPUList<DepthSpacePoint> depthSpacePointData = new GPUList<DepthSpacePoint>(device.ImmediateContext, depthSpaceData);
            //depthSpacePointData.AddRange(depthSpaceData.Select(dsp => {

            //    if (dsp.X == float.NegativeInfinity || dsp.Y == -float.NegativeInfinity)
            //    {
            //        return new DepthSpacePoint() { X = -1, Y = -1 };
            //    }
            //    else
            //    {
            //        return dsp;
            //    }
            //}));

            GPUList<int4> lastFrameData = new GPUList<int4>(device.ImmediateContext);
            lastFrameData.AddRange(lastFrameDataTransformed);

            var resultArray = new int4[1920 * 1080];
            GPUList<int4> resultData = new GPUList<int4>(device.ImmediateContext, resultArray);

            GPUList<int2> haloData = new GPUList<int2>(device.ImmediateContext, halo);

            // --

            var sw = Stopwatch.StartNew();

            // Set the buffers and uavs
            device.ImmediateContext.ComputeShader.Set(computeShader);
            device.ImmediateContext.ComputeShader.SetConstantBuffer(cbuffer, 0);
            device.ImmediateContext.ComputeShader.SetUnorderedAccessView(bgraData.UnorderedAccess, 0);
            device.ImmediateContext.ComputeShader.SetUnorderedAccessView(depthData.UnorderedAccess, 1);
            device.ImmediateContext.ComputeShader.SetUnorderedAccessView(depthSpacePointData.UnorderedAccess, 2);
            device.ImmediateContext.ComputeShader.SetUnorderedAccessView(lastFrameData.UnorderedAccess, 3);
            device.ImmediateContext.ComputeShader.SetUnorderedAccessView(resultData.UnorderedAccess, 4);
            device.ImmediateContext.ComputeShader.SetUnorderedAccessView(haloData.UnorderedAccess, 5);

            // Run the compute shader
            device.ImmediateContext.Dispatch(1920 * 1080 / 256, 1, 1);

            // Get result. This call blocks, until the result was calculated
            // because the MapSubresource call waits.
            var result = resultData.ToArray();

            sw.Stop();

            // -- Clean up --

            device.ImmediateContext.ComputeShader.SetConstantBuffer(null, 0);
            device.ImmediateContext.ComputeShader.SetUnorderedAccessView(null, 0);
            device.ImmediateContext.ComputeShader.SetUnorderedAccessView(null, 1);
            device.ImmediateContext.ComputeShader.SetUnorderedAccessView(null, 2);
            device.ImmediateContext.ComputeShader.SetUnorderedAccessView(null, 3);
            device.ImmediateContext.ComputeShader.SetUnorderedAccessView(null, 4);
            device.ImmediateContext.ComputeShader.SetUnorderedAccessView(null, 5);

            cbuffer.Dispose();
            bgraData.Dispose();
            depthData.Dispose();
            depthSpacePointData.Dispose();
            lastFrameData.Dispose();
            resultData.Dispose();
            haloData.Dispose();

            // --

            Debug.WriteLine($"Filtering took {sw.ElapsedMilliseconds} ms");

            var resultBytes = new byte[1920 * 1080 * 4];

            for (int i = 0, j = 0; i < resultBytes.Length; i += 4, ++j)
            {
                resultBytes[i] = (byte)result[j].x;
                resultBytes[i+1] = (byte)result[j].y;
                resultBytes[i+2] = (byte)result[j].z;
                resultBytes[i+3] = (byte)result[j].a;
            }

            lastFramePixels = resultBytes;

            return resultBytes;
        }