private void CopyBuffer(Vulkan.Buffer srcBuffer, Vulkan.Buffer dstBuffer, DeviceSize size)
        {
            var allocInfo = new CommandBufferAllocateInfo()
            {
                CommandPool        = vkCommandPool,
                CommandBufferCount = 1,
                Level = CommandBufferLevel.Primary,
            };

            var commmandBuffer = vkDevice.AllocateCommandBuffers(allocInfo)[0];

            commmandBuffer.Begin(new CommandBufferBeginInfo()
            {
                Flags = CommandBufferUsageFlags.OneTimeSubmit
            });
            commmandBuffer.CmdCopyBuffer(srcBuffer, dstBuffer, new BufferCopy[] { new BufferCopy()
                                                                                  {
                                                                                      Size = size
                                                                                  } });
            commmandBuffer.End();

            vkGraphicsQueue.Submit(new SubmitInfo()
            {
                CommandBuffers = new CommandBuffer[] { commmandBuffer }
            });
            vkGraphicsQueue.WaitIdle();

            vkDevice.FreeCommandBuffer(vkCommandPool, commmandBuffer);
        }
예제 #2
0
        void CopyImageToBuffer(CommandBuffer cmdBuffer, ImageData imageData, Buffer imageBuffer, uint width, uint height)
        {
            var subresource = new ImageSubresourceLayers(ImageAspectFlags.Color, 0, 0, 1);
            var imageCopy   = new BufferImageCopy(0, width, height, subresource, new Offset3D(0, 0, 0), new Extent3D(width, height, 0));

            cmdBuffer.CopyImageToBuffer(imageData.Image, ImageLayout.TransferSrcOptimal, imageBuffer, new[] { imageCopy });
        }
예제 #3
0
        DeviceMemory BindBuffer(Buffer buffer, MemoryAllocateInfo allocInfo)
        {
            var bufferMem = device.AllocateMemory(allocInfo);

            device.BindBufferMemory(buffer, bufferMem, 0);
            return(bufferMem);
        }
예제 #4
0
        void CopyBufferToImage(CommandBuffer cmdBuffer, ImageData imageData, Buffer imageBuffer)
        {
            var subresource = new ImageSubresourceLayers(ImageAspectFlags.Color, 0, 0, 1);
            var imageCopy   = new BufferImageCopy(0, 0, 0, subresource, new Offset3D(0, 0, 0), new Extent3D(imageData.Width, imageData.Height, 1));

            cmdBuffer.CopyBufferToImage(imageBuffer, imageData.Image, ImageLayout.TransferDstOptimal, new BufferImageCopy[] { imageCopy });
        }
예제 #5
0
        public static void CopyBuffer(Vk.Buffer src, Vk.Buffer dest, Vk.DeviceSize size,
                                      VkContext state)
        {
            var commandBuffer = state.BeginSingleTimeCommands(state.TransferCommandPool);

            var copyRegion = new Vk.BufferCopy();

            copyRegion.SrcOffset = 0;
            copyRegion.DstOffset = 0;
            copyRegion.Size      = size;

            commandBuffer.CmdCopyBuffer(src, dest, copyRegion);

            state.EndSingleTimeCommands(state.TransferQueue, state.TransferCommandPool, commandBuffer);
        }
예제 #6
0
        private void createIndexBuffer()
        {
            var size          = Marshal.SizeOf(typeof(short)) * this.indices.Count;
            var transferUsage = Vk.BufferUsageFlags.TransferSrc;
            var indexUsage    = Vk.BufferUsageFlags.IndexBuffer
                                | Vk.BufferUsageFlags.TransferDst;
            var transferMemoryProps = Vk.MemoryPropertyFlags.DeviceLocal;
            var indexMemoryProps    = Vk.MemoryPropertyFlags.HostVisible
                                      | Vk.MemoryPropertyFlags.HostCoherent;
            var sharingMode = this.GetSharingMode();

            BufferWithMemory stagingBuffer;

            try {
                stagingBuffer = VkHelper.CreateBuffer(this, size, transferUsage,
                                                      transferMemoryProps, sharingMode);
            } catch (Vk.ResultException result) {
                throw new VkException("An error occurred while creating the staging buffer.", result);
            }

            IntPtr memory     = this.Device.MapMemory(stagingBuffer.Memory, 0, size);
            var    indexArray = this.indices.ToArray();

            MemoryManagement.ArrayToPtr <short>(indexArray, memory, false);
            this.Device.UnmapMemory(stagingBuffer.Memory);

            try {
                BufferWithMemory indexBuffer = VkHelper.CreateBuffer(this, size, indexUsage,
                                                                     indexMemoryProps, sharingMode);
                this.vkIndexBuffer       = indexBuffer.Buffer;
                this.vkIndexBufferMemory = indexBuffer.Memory;
            } catch (Vk.ResultException result) {
                throw new VkException("An error occurred while creating the index buffer.", result);
            }

            VkHelper.CopyBuffer(stagingBuffer.Buffer, this.vkIndexBuffer, size, this);

            stagingBuffer.Destroy(this.Device);
        }
        private void CreateBuffer(DeviceSize size, BufferUsageFlags usageFlags, MemoryPropertyFlags properties, out Vulkan.Buffer buffer, out DeviceMemory bufferMemory)
        {
            var bufferInfo = new BufferCreateInfo()
            {
                Size        = size,
                Usage       = usageFlags,
                SharingMode = SharingMode.Exclusive,
            };

            buffer = vkDevice.CreateBuffer(bufferInfo);

            var memRequirements = vkDevice.GetBufferMemoryRequirements(buffer);

            var allocInfo = new MemoryAllocateInfo()
            {
                AllocationSize  = memRequirements.Size,
                MemoryTypeIndex = FindMemoryType(memRequirements.MemoryTypeBits, properties),
            };

            bufferMemory = vkDevice.AllocateMemory(allocInfo);

            vkDevice.BindBufferMemory(buffer, bufferMemory, 0);
        }
예제 #8
0
        void CopyBufferToImage(Queue queue, CommandPool cmdPool, ImageData imageData, Buffer imageBuffer)
        {
            var cmdBuffers = AllocateCommandBuffers(cmdPool, 1);
            var cmdBuffer  = cmdBuffers[0];

            var beginInfo = new CommandBufferBeginInfo();

            cmdBuffer.Begin(beginInfo);

            PipelineBarrierSetLayout(cmdBuffer, imageData.Image, ImageLayout.Preinitialized, ImageLayout.TransferDstOptimal, AccessFlags.HostWrite, AccessFlags.TransferWrite);

            var subresource = new ImageSubresourceLayers(ImageAspectFlags.Color, 0, 0, 1);
            var imageCopy   = new BufferImageCopy(0, 0, 0, subresource, new Offset3D(0, 0, 0), new Extent3D(imageData.Width, imageData.Height, 1));

            cmdBuffer.CopyBufferToImage(imageBuffer, imageData.Image, ImageLayout.TransferDstOptimal, new BufferImageCopy[] { imageCopy });

            PipelineBarrierSetLayout(cmdBuffer, imageData.Image, ImageLayout.TransferDstOptimal, ImageLayout.ColorAttachmentOptimal, AccessFlags.TransferWrite, AccessFlags.ColorAttachmentWrite);

            cmdBuffer.End();

            var submitInfo = new SubmitInfo(null, null, new[] { cmdBuffer }, null);

            queue.Submit(new[] { submitInfo });
            submitInfo.Dispose();
            queue.WaitIdle();

            device.FreeCommandBuffers(cmdPool, new[] { cmdBuffer });
        }
예제 #9
0
        void Render(Queue queue, CommandPool cmdPool, VertexData vertexData, ImageData imageData, Buffer imageBuffer, RenderPass renderPass, Pipeline pipeline, Framebuffer framebuffer)
        {
            uint width  = imageData.Width;
            uint height = imageData.Height;

            var cmdBuffers = AllocateCommandBuffers(cmdPool, 1);
            var cmdBuffer  = cmdBuffers[0];

            var beginInfo = new CommandBufferBeginInfo();

            cmdBuffer.Begin(beginInfo);  // CommandBuffer Begin

            PipelineBarrierSetLayout(cmdBuffer, imageData.Image, ImageLayout.Preinitialized, ImageLayout.ColorAttachmentOptimal, AccessFlags.HostWrite, AccessFlags.ColorAttachmentWrite);

            var clearRange = new ImageSubresourceRange(ImageAspectFlags.Color, 0, 1, 0, 1);

            cmdBuffer.ClearColorImage(imageData.Image, ImageLayout.TransferDstOptimal, new ClearColorValue(), new[] { clearRange });

            RenderTriangle(cmdBuffer, vertexData, renderPass, pipeline, framebuffer, width, height);

            // Prepare the render target for copying
            PipelineBarrierSetLayout(cmdBuffer, imageData.Image, ImageLayout.ColorAttachmentOptimal, ImageLayout.TransferSrcOptimal, AccessFlags.ColorAttachmentWrite, AccessFlags.TransferRead);

            // Copy the render target image to a buffer
            CopyImageToBuffer(cmdBuffer, imageData, imageBuffer, width, height);

            // End recording commands to the buffer
            cmdBuffer.End();

            SubmitForExecution(queue, cmdBuffer);

            queue.WaitIdle(); // wait for execution to finish

            device.FreeCommandBuffers(cmdPool, new[] { cmdBuffer });
        }