public unsafe void GetTextureData(int mipLevel, IntPtr destination, int storageSizeInBytes)
        {
            int width  = MipmapHelper.GetDimension(Width, mipLevel);
            int height = MipmapHelper.GetDimension(Height, mipLevel);

            Bind();
            GL.GetTexImage(TextureTarget.Texture2D, mipLevel, OpenTK.Graphics.OpenGL.PixelFormat.Alpha, _pixelType, destination);
            GL.BindTexture(TextureTarget.Texture2D, 0);
            GL.PixelStore(PixelStoreParameter.PackAlignment, 1);

            // Need to reverse the rows vertically.
            int    pixelSizeInBytes = FormatHelpers.GetPixelSizeInBytes(_veldridFormat);
            int    rowBytes         = width * pixelSizeInBytes;
            IntPtr stagingRow       = Marshal.AllocHGlobal(rowBytes);
            byte * stagingPtr       = (byte *)stagingRow.ToPointer();
            byte * sourcePtr        = (byte *)destination.ToPointer();

            for (int y = height - 1, destY = 0; y > (height / 2); y--)
            {
                Buffer.MemoryCopy(sourcePtr + (y * rowBytes), stagingPtr, rowBytes, rowBytes);
                Buffer.MemoryCopy(sourcePtr + (destY * rowBytes), sourcePtr + (y * rowBytes), rowBytes, rowBytes);
                Buffer.MemoryCopy(stagingPtr, sourcePtr + (destY * rowBytes), rowBytes, rowBytes);

                destY++;
            }

            // Reset to default value.
            GL.PixelStore(PixelStoreParameter.PackAlignment, 4);
        }
Example #2
0
        public void GetTextureData(int mipLevel, IntPtr destination, int storageSizeInBytes)
        {
            int width  = MipmapHelper.GetDimension(Width, mipLevel);
            int height = MipmapHelper.GetDimension(Height, mipLevel);

            D3DTexture2D stagingTexture = new D3DTexture2D(_device, new Texture2DDescription()
            {
                Width             = width,
                Height            = height,
                Usage             = ResourceUsage.Staging,
                BindFlags         = BindFlags.None,
                CpuAccessFlags    = CpuAccessFlags.Read,
                OptionFlags       = ResourceOptionFlags.None,
                MipLevels         = 1,
                ArraySize         = 1,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                Format            = DeviceTexture.Description.Format
            });

            // Copy the data from the GPU to the staging texture.
            _device.ImmediateContext.CopySubresourceRegion(DeviceTexture, mipLevel, null, stagingTexture.DeviceTexture, 0);

            int elementCount = width * height;
            // Copy the data to the array.
            DataBox db = _device.ImmediateContext.MapSubresource(
                stagingTexture.DeviceTexture,
                0,
                MapMode.Read,
                MapFlags.None,
                out DataStream ds);

            int pixelSizeInBytes = D3DFormats.GetPixelSize(DeviceTexture.Description.Format);
            int rowSize          = pixelSizeInBytes * width;

            // If the pitch exactly matches the row size, we can simply copy all the data.
            if (rowSize == db.RowPitch)
            {
                SharpDX.Utilities.CopyMemory(destination, db.DataPointer, elementCount * pixelSizeInBytes);
            }
            else
            {
                // The texture data may not have a pitch exactly equal to the row width.
                // This means that the pixel data is not "tightly packed" into the buffer given
                // to us, and has empty data at the end of each row.

                for (int rowNumber = 0; rowNumber < height; rowNumber++)
                {
                    int rowStartOffsetInBytes = rowNumber * width * pixelSizeInBytes;
                    ds.Read(destination, rowStartOffsetInBytes, width * pixelSizeInBytes);

                    // At the end of the row, seek the stream to skip the extra filler data,
                    // which is equal to (RowPitch - RowSize) bytes.
                    ds.Seek(db.RowPitch - rowSize, SeekOrigin.Current);
                }
            }

            stagingTexture.Dispose();
        }