Exemple #1
0
        private void SetDataStatic(int offset, IntPtr data, int size)
        {
            var uploadBufferAlloc = context.AllocateUploadBuffer((ulong)size, 1);

            GraphicsUtility.CopyMemory(uploadBufferAlloc.Data, data, size);
            context.EndRenderPass();
            context.EnsureCommandBuffer();
            context.CommandBuffer.PipelineBarrier(
                pipelineStageMask, SharpVulkan.PipelineStageFlags.Transfer,
                SharpVulkan.DependencyFlags.None, 0, null, 0, null, 0, null);
            var copyRegion = new SharpVulkan.BufferCopy {
                SourceOffset      = uploadBufferAlloc.BufferOffset,
                DestinationOffset = (ulong)offset,
                Size = (ulong)size
            };

            context.CommandBuffer.CopyBuffer(uploadBufferAlloc.Buffer, backingBuffer.Buffer, 1, &copyRegion);
            var postMemoryBarrier = new SharpVulkan.MemoryBarrier {
                StructureType         = SharpVulkan.StructureType.MemoryBarrier,
                SourceAccessMask      = SharpVulkan.AccessFlags.TransferWrite,
                DestinationAccessMask = accessMask
            };

            context.CommandBuffer.PipelineBarrier(
                SharpVulkan.PipelineStageFlags.Transfer, pipelineStageMask,
                SharpVulkan.DependencyFlags.None, 1, &postMemoryBarrier, 0, null, 0, null);
        }
Exemple #2
0
        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, &copyRegion);
                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);
            }
        }