private void Initialize(TextureParams textureParams) { compressed = Format.IsCompressed(); GLHelper.GetGLTextureFormat(Context, Format, out GLInternalFormat, out GLFormat, out GLPixelType); GLTexture = GL.GenTexture(); GLHelper.CheckGLErrors(); GL.ActiveTexture(TextureUnit.Texture0); GLHelper.CheckGLErrors(); GL.BindTexture(TextureTarget.Texture2D, GLTexture); GLHelper.CheckGLErrors(); for (var level = 0; level < LevelCount; level++) { GraphicsUtility.CalculateMipLevelSize(level, Width, Height, out var levelWidth, out var levelHeight); if (compressed) { var imageSize = GraphicsUtility.CalculateImageDataSize(Format, levelWidth, levelHeight); GL.CompressedTexImage2D(TextureTarget.Texture2D, level, (PixelInternalFormat)GLInternalFormat, levelWidth, levelHeight, 0, imageSize, IntPtr.Zero); GLHelper.CheckGLErrors(); } else { GL.TexImage2D(TextureTarget.Texture2D, level, (PixelInternalFormat)GLInternalFormat, levelWidth, levelHeight, 0, (PixelFormat)GLFormat, (PixelType)GLPixelType, IntPtr.Zero); GLHelper.CheckGLErrors(); } } UpdateTextureParams(textureParams); Context.InvalidateTextureBinding(0); }
public void SetData(int level, int x, int y, int width, int height, IntPtr data) { GL.ActiveTexture(TextureUnit.Texture0); GLHelper.CheckGLErrors(); GL.BindTexture(TextureTarget.Texture2D, GLTexture); GLHelper.CheckGLErrors(); if (compressed) { var imageSize = GraphicsUtility.CalculateImageDataSize(Format, width, height); GL.CompressedTexSubImage2D(TextureTarget.Texture2D, level, x, y, width, height, (PixelFormat)GLInternalFormat, imageSize, data); GLHelper.CheckGLErrors(); } else { GL.TexSubImage2D(TextureTarget.Texture2D, level, x, y, width, height, (PixelFormat)GLFormat, (PixelType)GLPixelType, data); GLHelper.CheckGLErrors(); } Context.InvalidateTextureBinding(0); }
internal void GetData(Format dstFormat, int level, int x, int y, int width, int height, IntPtr data) { if (IsPvrtc1Format(Format)) { throw new NotImplementedException(); } var dataSize = GraphicsUtility.CalculateImageDataSize(Format, width, height); context.EnsureReadbackBuffer((ulong)dataSize); context.EndRenderPass(); context.EnsureCommandBuffer(); var preMemoryBarrier = new SharpVulkan.ImageMemoryBarrier { StructureType = SharpVulkan.StructureType.ImageMemoryBarrier, Image = image, OldLayout = SharpVulkan.ImageLayout.ShaderReadOnlyOptimal, NewLayout = SharpVulkan.ImageLayout.TransferSourceOptimal, SourceAccessMask = SharpVulkan.AccessFlags.ShaderRead, DestinationAccessMask = SharpVulkan.AccessFlags.TransferRead, SubresourceRange = new SharpVulkan.ImageSubresourceRange(SharpVulkan.ImageAspectFlags.Color, 0, 1, (uint)level, 1) }; context.CommandBuffer.PipelineBarrier( SharpVulkan.PipelineStageFlags.VertexShader | SharpVulkan.PipelineStageFlags.FragmentShader, SharpVulkan.PipelineStageFlags.Transfer, SharpVulkan.DependencyFlags.None, 0, null, 0, null, 1, &preMemoryBarrier); var copyRegion = new SharpVulkan.BufferImageCopy { BufferOffset = 0, BufferRowLength = (uint)width, BufferImageHeight = (uint)height, ImageOffset = new SharpVulkan.Offset3D(x, y, 0), ImageExtent = new SharpVulkan.Extent3D((uint)width, (uint)height, 1), ImageSubresource = new SharpVulkan.ImageSubresourceLayers(SharpVulkan.ImageAspectFlags.Color, 0, 1, (uint)level) }; context.CommandBuffer.CopyImageToBuffer( image, SharpVulkan.ImageLayout.TransferSourceOptimal, context.ReadbackBuffer, 1, ©Region); var postMemoryBarrier = new SharpVulkan.ImageMemoryBarrier { StructureType = SharpVulkan.StructureType.ImageMemoryBarrier, Image = image, OldLayout = SharpVulkan.ImageLayout.TransferSourceOptimal, NewLayout = SharpVulkan.ImageLayout.ShaderReadOnlyOptimal, SourceAccessMask = SharpVulkan.AccessFlags.TransferRead, DestinationAccessMask = SharpVulkan.AccessFlags.ShaderRead, SubresourceRange = new SharpVulkan.ImageSubresourceRange(SharpVulkan.ImageAspectFlags.Color, 0, 1, (uint)level, 1) }; context.CommandBuffer.PipelineBarrier( SharpVulkan.PipelineStageFlags.Transfer, SharpVulkan.PipelineStageFlags.VertexShader | SharpVulkan.PipelineStageFlags.FragmentShader, SharpVulkan.DependencyFlags.None, 0, null, 0, null, 1, &postMemoryBarrier); context.Finish(); var readbackBufferMemory = context.ReadbackBufferMemory; var readbackBufferMappedMemory = context.MemoryAllocator.Map(readbackBufferMemory); try { context.MemoryAllocator.InvalidateMappedMemoryRange(readbackBufferMemory, 0, (ulong)dataSize); if (dstFormat == Format) { GraphicsUtility.CopyMemory(data, readbackBufferMappedMemory, Format.GetSize() * width * height); } else { var decoder = FormatConverter.GetDecoder(Format); var encoder = FormatConverter.GetEncoder(dstFormat); var srcTexelSize = Format.GetSize(); var dstTexelSize = dstFormat.GetSize(); var srcData = readbackBufferMappedMemory; var dstData = data; var texelCount = width * height; while (texelCount-- > 0) { encoder(dstData, decoder(srcData)); srcData += srcTexelSize; dstData += dstTexelSize; } } } finally { context.MemoryAllocator.Unmap(readbackBufferMemory); } }
public void SetData(int level, int x, int y, int width, int height, IntPtr data) { var dataSize = GraphicsUtility.CalculateImageDataSize(Format, width, height); if (IsPvrtc1Format(Format)) { if (x != 0 || y != 0 || width != Width || height != Height) { throw new NotSupportedException(); } if (context.NextFenceValue <= WriteFenceValue) { context.Flush(); } context.WaitForFence(WriteFenceValue); var dstData = Context.MemoryAllocator.Map(memory); try { GraphicsUtility.CopyMemory(dstData, data, dataSize); } finally { Context.MemoryAllocator.Unmap(memory); } } else { ulong bufferOffsetAlignment = 4; bufferOffsetAlignment = GraphicsUtility.CombineAlignment(bufferOffsetAlignment, (ulong)Format.GetSize()); bufferOffsetAlignment = GraphicsUtility.CombineAlignment(bufferOffsetAlignment, context.PhysicalDeviceLimits.OptimalBufferCopyOffsetAlignment); var uploadBufferAlloc = context.AllocateUploadBuffer((ulong)dataSize, bufferOffsetAlignment); GraphicsUtility.CopyMemory(uploadBufferAlloc.Data, data, dataSize); context.EndRenderPass(); context.EnsureCommandBuffer(); var preMemoryBarrier = new SharpVulkan.ImageMemoryBarrier { StructureType = SharpVulkan.StructureType.ImageMemoryBarrier, Image = image, OldLayout = SharpVulkan.ImageLayout.ShaderReadOnlyOptimal, NewLayout = SharpVulkan.ImageLayout.TransferDestinationOptimal, SourceAccessMask = SharpVulkan.AccessFlags.ShaderRead, DestinationAccessMask = SharpVulkan.AccessFlags.TransferWrite, SubresourceRange = new SharpVulkan.ImageSubresourceRange(SharpVulkan.ImageAspectFlags.Color, 0, 1, (uint)level, 1) }; context.CommandBuffer.PipelineBarrier( SharpVulkan.PipelineStageFlags.VertexShader | SharpVulkan.PipelineStageFlags.FragmentShader, SharpVulkan.PipelineStageFlags.Transfer, SharpVulkan.DependencyFlags.None, 0, null, 0, null, 1, &preMemoryBarrier); var copyRegion = new SharpVulkan.BufferImageCopy { BufferOffset = uploadBufferAlloc.BufferOffset, BufferRowLength = (uint)width, BufferImageHeight = (uint)height, ImageOffset = new SharpVulkan.Offset3D(x, y, 0), ImageExtent = new SharpVulkan.Extent3D((uint)width, (uint)height, 1), ImageSubresource = new SharpVulkan.ImageSubresourceLayers(SharpVulkan.ImageAspectFlags.Color, 0, 1, (uint)level) }; context.CommandBuffer.CopyBufferToImage( uploadBufferAlloc.Buffer, image, SharpVulkan.ImageLayout.TransferDestinationOptimal, 1, ©Region); var postMemoryBarrier = new SharpVulkan.ImageMemoryBarrier { StructureType = SharpVulkan.StructureType.ImageMemoryBarrier, Image = image, OldLayout = SharpVulkan.ImageLayout.TransferDestinationOptimal, NewLayout = SharpVulkan.ImageLayout.ShaderReadOnlyOptimal, SourceAccessMask = SharpVulkan.AccessFlags.TransferWrite, DestinationAccessMask = SharpVulkan.AccessFlags.ShaderRead, SubresourceRange = new SharpVulkan.ImageSubresourceRange(SharpVulkan.ImageAspectFlags.Color, 0, 1, (uint)level, 1) }; context.CommandBuffer.PipelineBarrier( SharpVulkan.PipelineStageFlags.Transfer, SharpVulkan.PipelineStageFlags.VertexShader | SharpVulkan.PipelineStageFlags.FragmentShader, SharpVulkan.DependencyFlags.None, 0, null, 0, null, 1, &postMemoryBarrier); } }