예제 #1
0
 public void Covariance(Texture tex, ComputeBuffer outputBuf)
 {
     using (var averageBuf = new DisposableBuffer(1, STRIDE_OF_VECTOR4)) {
         Average(tex, averageBuf);
         Covariance(tex, averageBuf, outputBuf);
     }
 }
예제 #2
0
        public void Covariance(Texture tex, ComputeBuffer averageBuf, ComputeBuffer outputBuf)
        {
            var totalWidth  = Mathf.CeilToInt(tex.width / (float)NUM_THREADS);
            var totalHeight = Mathf.CeilToInt(tex.height / (float)NUM_THREADS);

            using (var totalCovBuf = new DisposableBuffer(totalWidth * totalHeight, STRIDE_OF_VECTOR4x4))
                using (var totalBuf = new DisposableBuffer(1, STRIDE_OF_VECTOR4x4)) {
                    cs.SetInts(PROP_INPUT_SIZE, tex.width, tex.height);
                    cs.SetTexture(kernelCovariance, PROP_INPUT_IMAGE, tex);

                    cs.SetInts(PROP_OUTPUT_SIZE, totalWidth, totalHeight);
                    cs.SetBuffer(kernelCovariance, PROP_OUTPUT4x4, totalCovBuf);

                    cs.SetBuffer(kernelCovariance, PROP_PARAM_BUF0_4, averageBuf);

                    cs.Dispatch(kernelCovariance, totalWidth, totalHeight, 1);

                    reduction.Accumulate4x4(totalCovBuf, totalWidth, totalHeight, totalBuf);

                    var pixelCount = tex.width * tex.height;
                    var v          = Vector4.one / pixelCount;
                    var m          = new Matrix4x4(v, v, v, v);
                    Multiply4x4(totalBuf, m, outputBuf);
                }
        }
예제 #3
0
 public void Average(Texture tex, ComputeBuffer outputBuf)
 {
     using (var totalBuf = new DisposableBuffer(1, STRIDE_OF_VECTOR4)) {
         var pixelCount = tex.width * tex.height;
         Sum(tex, totalBuf);
         Multiply4(totalBuf, Vector4.one / pixelCount, outputBuf);
     }
 }
예제 #4
0
 public Vector4 Average(Texture tex)
 {
     using (var averageBuf = new DisposableBuffer(1, STRIDE_OF_VECTOR4)) {
         Average(tex, averageBuf);
         var average = new Vector4[1];
         averageBuf.Buffer.GetData(average);
         return(average[0]);
     }
 }
예제 #5
0
        public Matrix4x4 Accumulate4x4(ComputeBuffer grid4x4, int width, int height)
        {
            var reduction = new Matrix4x4[1];

            using (var reductionBuf = new DisposableBuffer(reduction.Length, STRIDE_OF_VECTOR4x4)) {
                Accumulate4x4(grid4x4, width, height, reductionBuf);
                reductionBuf.Buffer.GetData(reduction);
                return(reduction[0]);
            }
        }
예제 #6
0
        public Vector4 Sum(Texture tex)
        {
            var total = new Vector4[1];

            using (var totalBuffer = new DisposableBuffer(total.Length, STRIDE_OF_VECTOR4)) {
                totalBuffer.Buffer.SetData(total);
                Sum(tex, totalBuffer);
                totalBuffer.Buffer.GetData(total);
                return(total[0]);
            }
        }
예제 #7
0
        public void Accumulate4x4(ComputeBuffer grid4x4, int width, int height, ComputeBuffer outputBuf)
        {
            using (var lineoutBuf = new DisposableBuffer(height, STRIDE_OF_VECTOR4x4)) {
                cs.SetInts(PROP_INPUT_SIZE, width, height);
                cs.SetBuffer(kernelAccumX4x4, PROP_INPUT4x4, grid4x4);
                cs.SetBuffer(kernelAccumX4x4, PROP_OUTPUT4x4, lineoutBuf);
                cs.Dispatch(kernelAccumX4x4, 1, Mathf.CeilToInt(height / (float)NUM_THREADS), 1);

                cs.SetInts(PROP_INPUT_SIZE, 1, height);
                cs.SetBuffer(kernelAccumY4x4, PROP_INPUT4x4, lineoutBuf);
                cs.SetBuffer(kernelAccumY4x4, PROP_OUTPUT4x4, outputBuf);
                cs.Dispatch(kernelAccumY4x4, 1, 1, 1);
            }
        }
예제 #8
0
 public Matrix4x4 Covariance(Texture tex, out Vector4 average)
 {
     using (var covarianceBuf = new DisposableBuffer(1, STRIDE_OF_VECTOR4x4))
         using (var averageBuf = new DisposableBuffer(1, STRIDE_OF_VECTOR4)) {
             Average(tex, averageBuf);
             Covariance(tex, averageBuf, covarianceBuf);
             var covarianceData = new Matrix4x4[1];
             covarianceBuf.Buffer.GetData(covarianceData);
             var averageData = new Vector4[1];
             averageBuf.Buffer.GetData(averageData);
             average = averageData[0];
             return(covarianceData[0]);
         }
 }
예제 #9
0
        public void Sum(Texture tex, ComputeBuffer outputBuf)
        {
            var totalWidth  = Mathf.CeilToInt(tex.width / (float)NUM_THREADS);
            var totalHeight = Mathf.CeilToInt(tex.width / (float)NUM_THREADS);

            using (var totalBuf = new DisposableBuffer(totalWidth * totalHeight, STRIDE_OF_VECTOR4)) {
                cs.SetInts(PROP_INPUT_SIZE, tex.width, tex.height);
                cs.SetTexture(kernelSum, PROP_INPUT_IMAGE, tex);

                cs.SetInts(PROP_OUTPUT_SIZE, totalWidth, totalHeight);
                cs.SetBuffer(kernelSum, PROP_OUTPUT4, totalBuf);

                cs.Dispatch(kernelSum, totalWidth, totalHeight, 1);

                reduction.Accumulate4(totalBuf, totalWidth, totalHeight, outputBuf);
            }
        }