Exemplo n.º 1
0
        private T Reduce <T>(UploadBuffer data, ReduceShader shader) where T : struct
        {
            Console.WriteLine("num groups: " + Utility.DivideRoundUp(data.ByteSize / 4, ReduceShader.ElementsPerGroup));

            using (var buf = new GpuBuffer(4, data.ByteSize / 4))
            {
                buf.CopyFrom(data);

                using (var timer = new GpuTimer())
                {
                    timer.Start();
                    //for(int i = 0; i < 100; ++i)
                    shader.Run(buf, data.ByteSize / 4);
                    timer.Stop();
                    Console.WriteLine(timer.GetDelta());
                }

                using (var res = new DownloadBuffer(4))
                {
                    res.CopyFrom(buf, Marshal.SizeOf <T>());

                    var resData = res.GetData <T>();

                    return(resData);
                }
            }
        }
Exemplo n.º 2
0
        //public bool HasAlpha => !(Min.Alpha == 1.0f && Max.Alpha == 1.0f);

        internal float GetStats(ITexture texture, LayerMipmapRange lm, StatisticsShader statShader, ReduceShader redShader, bool normalize)
        {
            Debug.Assert(lm.IsSingleMipmap);

            // obtain a buffer that is big enough
            int numElements = texture.Size.GetMip(lm.FirstMipmap).Product;

            if (lm.AllLayer)
            {
                numElements *= texture.LayerMipmap.Layers;
            }

            // allocate buffer that is big enough
            GetBuffer(numElements);

            // copy all values into buffer
            statShader.CopyToBuffer(texture, buffer, lm);

            // execute reduce
            redShader.Run(buffer, numElements);

            shared.Download.CopyFrom(buffer, sizeof(float));

            var res = shared.Download.GetData <float>();

            if (normalize)
            {
                res /= numElements;
            }
            return(res);
        }
Exemplo n.º 3
0
        //public bool HasAlpha => !(Min.Alpha == 1.0f && Max.Alpha == 1.0f);

        internal float GetStats(ITexture texture, int layer, int mipmap, StatisticsShader statShader, ReduceShader redShader, bool normalize)
        {
            // obtain a buffer that is big enough
            int numElements = texture.Size.GetMip(mipmap).Product;

            if (layer == -1)
            {
                numElements *= texture.NumLayers;
            }

            if (buffer == null || buffer.ElementCount < numElements)
            {
                buffer?.Dispose();
                buffer = new GpuBuffer(4, numElements);
            }

            // copy all values into buffer
            statShader.CopyToBuffer(texture, buffer, layer, mipmap);

            // execute reduce
            redShader.Run(buffer, numElements);

            shared.Download.CopyFrom(buffer);

            var res = shared.Download.GetData <float>();

            if (normalize)
            {
                res /= numElements;
            }
            return(res);
        }