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);
        }
        public void SetTextureData(int mipLevel, int x, int y, int width, int height, IntPtr data, int dataSizeInBytes)
        {
            Bind();
            int pixelSize = FormatHelpers.GetPixelSizeInBytes(_veldridFormat);

            if (pixelSize < 4)
            {
                GL.PixelStore(PixelStoreParameter.UnpackAlignment, pixelSize);
                Utilities.CheckLastGLError();
            }
            GL.TexSubImage2D(TextureTarget.Texture2D, mipLevel, x, y, width, height, _pixelFormat, _pixelType, data);
            Utilities.CheckLastGLError();
            if (pixelSize < 4)
            {
                GL.PixelStore(PixelStoreParameter.UnpackAlignment, 4);
                Utilities.CheckLastGLError();
            }
        }
Esempio n. 3
0
        public override DeviceTexture2D CreateTexture(
            int mipLevels,
            int width,
            int height,
            PixelFormat format,
            DeviceTextureCreateOptions createOptions)
        {
            int pixelSizeInBytes = FormatHelpers.GetPixelSizeInBytes(format);

            OpenTK.Graphics.ES30.PixelFormat pixelFormat = OpenGLESFormats.MapPixelFormat(format);

            if (createOptions == DeviceTextureCreateOptions.DepthStencil)
            {
                if (format != PixelFormat.R16_UInt && format != PixelFormat.R32_Float)
                {
                    throw new NotImplementedException("The only supported depth texture formats are R16_UInt and R32_Float.");
                }

                pixelFormat = OpenTK.Graphics.ES30.PixelFormat.DepthComponent;
            }

            return(new OpenGLESTexture2D(mipLevels, width, height, format, pixelFormat, OpenGLESFormats.MapPixelType(format)));
        }
 public OpenGLTexture2D(int width, int height, PixelFormat format, IntPtr pixelData)
     : this(1, width, height, format, OpenGLFormats.MapPixelInternalFormat(format), OpenGLFormats.MapPixelFormat(format), OpenGLFormats.MapPixelType(format))
 {
     SetTextureData(1, 0, 0, width, height, pixelData, FormatHelpers.GetPixelSizeInBytes(format) * width * height);
 }
        private void CopyDataMultiImage(
            IntPtr pixelsFront,
            IntPtr pixelsBack,
            IntPtr pixelsLeft,
            IntPtr pixelsRight,
            IntPtr pixelsTop,
            IntPtr pixelsBottom)
        {
            StackList <IntPtr, Size6IntPtr> faces = new StackList <IntPtr, Size6IntPtr>();

            faces.Add(pixelsRight);
            faces.Add(pixelsLeft);
            faces.Add(pixelsTop);
            faces.Add(pixelsBottom);
            faces.Add(pixelsBack);
            faces.Add(pixelsFront);

            TransitionImageLayout(_image, (uint)MipLevels, 0, 6, _imageLayout, VkImageLayout.TransferDstOptimal);

            for (uint i = 0; i < 6; i++)
            {
                CreateImage(
                    _device,
                    _physicalDevice,
                    _memoryManager,
                    (uint)Width,
                    (uint)Height,
                    1,
                    _vkFormat,
                    VkImageTiling.Linear,
                    VkImageUsageFlags.TransferSrc,
                    VkMemoryPropertyFlags.HostVisible | VkMemoryPropertyFlags.HostCoherent,
                    out VkImage stagingImage, out VkMemoryBlock stagingMemory);

                VkImageSubresource subresource;
                subresource.aspectMask = VkImageAspectFlags.Color;
                subresource.arrayLayer = 0;
                subresource.mipLevel   = 0;

                vkGetImageSubresourceLayout(_device, stagingImage, ref subresource, out VkSubresourceLayout stagingLayout);
                void *   mappedPtr;
                VkResult result = vkMapMemory(_device, stagingMemory.DeviceMemory, stagingMemory.Offset, stagingLayout.size, 0, &mappedPtr);
                CheckResult(result);
                IntPtr data            = faces[i];
                ulong  dataSizeInBytes = (ulong)(Width * Height * FormatHelpers.GetPixelSizeInBytes(_format));
                ulong  rowPitch        = stagingLayout.rowPitch;
                if (rowPitch == (ulong)Width)
                {
                    Buffer.MemoryCopy(data.ToPointer(), mappedPtr, dataSizeInBytes, dataSizeInBytes);
                }
                else
                {
                    int pixelSizeInBytes = FormatHelpers.GetPixelSizeInBytes(_format);
                    for (uint yy = 0; yy < Height; yy++)
                    {
                        byte *dstRowStart = ((byte *)mappedPtr) + (rowPitch * yy);
                        byte *srcRowStart = ((byte *)data.ToPointer()) + (Width * yy * pixelSizeInBytes);
                        Unsafe.CopyBlock(dstRowStart, srcRowStart, (uint)(Width * pixelSizeInBytes));
                    }
                }

                vkUnmapMemory(_device, stagingMemory.DeviceMemory);

                TransitionImageLayout(stagingImage, 1, 0, 1, VkImageLayout.Preinitialized, VkImageLayout.TransferSrcOptimal);
                CopyImage(stagingImage, 0, 0, _image, 0, i, (uint)Width, (uint)Height);

                vkDestroyImage(_device, stagingImage, null);
                _memoryManager.Free(stagingMemory);
            }
        }
Esempio n. 6
0
        public void SetTextureData(int mipLevel, int x, int y, int width, int height, IntPtr data, int dataSizeInBytes)
        {
            if (x != 0 || y != 0)
            {
                throw new NotImplementedException();
            }

            // First, create a staging texture.
            CreateImage(
                _device,
                _physicalDevice,
                _memoryManager,
                (uint)width,
                (uint)height,
                1,
                Format,
                VkImageTiling.Linear,
                VkImageUsageFlags.TransferSrc,
                VkMemoryPropertyFlags.HostVisible | VkMemoryPropertyFlags.HostCoherent,
                out VkImage stagingImage,
                out VkMemoryBlock stagingMemory);

            VkImageSubresource subresource = new VkImageSubresource();

            subresource.aspectMask = VkImageAspectFlags.Color;
            subresource.mipLevel   = 0;
            subresource.arrayLayer = 0;
            vkGetImageSubresourceLayout(_device, stagingImage, ref subresource, out VkSubresourceLayout stagingLayout);
            ulong rowPitch = stagingLayout.rowPitch;

            void *   mappedPtr;
            VkResult result = vkMapMemory(_device, stagingMemory.DeviceMemory, stagingMemory.Offset, stagingLayout.size, 0, &mappedPtr);

            CheckResult(result);

            if (rowPitch == (ulong)width)
            {
                Buffer.MemoryCopy(data.ToPointer(), mappedPtr, dataSizeInBytes, dataSizeInBytes);
            }
            else
            {
                int pixelSizeInBytes = FormatHelpers.GetPixelSizeInBytes(_veldridFormat);
                for (uint yy = 0; yy < height; yy++)
                {
                    byte *dstRowStart = ((byte *)mappedPtr) + (rowPitch * yy);
                    byte *srcRowStart = ((byte *)data.ToPointer()) + (width * yy * pixelSizeInBytes);
                    Unsafe.CopyBlock(dstRowStart, srcRowStart, (uint)(width * pixelSizeInBytes));
                }
            }

            vkUnmapMemory(_device, stagingMemory.DeviceMemory);

            TransitionImageLayout(stagingImage, 1, VkImageLayout.Preinitialized, VkImageLayout.TransferSrcOptimal);
            TransitionImageLayout(_image, (uint)MipLevels, _imageLayout, VkImageLayout.TransferDstOptimal);
            CopyImage(stagingImage, 0, _image, (uint)mipLevel, (uint)width, (uint)height);
            TransitionImageLayout(_image, (uint)MipLevels, VkImageLayout.TransferDstOptimal, VkImageLayout.ShaderReadOnlyOptimal);
            _imageLayout = VkImageLayout.ShaderReadOnlyOptimal;

            vkDestroyImage(_device, stagingImage, null);
            _memoryManager.Free(stagingMemory);
        }