private void CreateCommandBuffers()
        {
            this.commandBuffers = new VkCommandBuffer[this.swapChainFramebuffers.Length];

            VkCommandBufferAllocateInfo allocInfo = new VkCommandBufferAllocateInfo()
            {
                sType              = VkStructureType.VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
                commandPool        = commandPool,
                level              = VkCommandBufferLevel.VK_COMMAND_BUFFER_LEVEL_PRIMARY,
                commandBufferCount = (uint)commandBuffers.Length,
            };

            fixed(VkCommandBuffer *commandBuffersPtr = &this.commandBuffers[0])
            {
                Helpers.CheckErrors(VulkanNative.vkAllocateCommandBuffers(this.device, &allocInfo, commandBuffersPtr));
            }

            // Begin
            for (uint i = 0; i < this.commandBuffers.Length; i++)
            {
                VkCommandBufferBeginInfo beginInfo = new VkCommandBufferBeginInfo()
                {
                    sType            = VkStructureType.VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
                    flags            = 0,    // Optional
                    pInheritanceInfo = null, // Optional
                };

                Helpers.CheckErrors(VulkanNative.vkBeginCommandBuffer(this.commandBuffers[i], &beginInfo));

                // Pass
                VkClearValue clearColor = new VkClearValue()
                {
                    color = new VkClearColorValue(0.0f, 0.0f, 0.0f, 1.0f),
                };

                VkRenderPassBeginInfo renderPassInfo = new VkRenderPassBeginInfo()
                {
                    sType           = VkStructureType.VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
                    renderPass      = this.renderPass,
                    framebuffer     = this.swapChainFramebuffers[i],
                    renderArea      = new VkRect2D(0, 0, this.swapChainExtent.width, this.swapChainExtent.height),
                    clearValueCount = 1,
                    pClearValues    = &clearColor,
                };

                VulkanNative.vkCmdBeginRenderPass(this.commandBuffers[i], &renderPassInfo, VkSubpassContents.VK_SUBPASS_CONTENTS_INLINE);

                // Draw
                VulkanNative.vkCmdBindPipeline(this.commandBuffers[i], VkPipelineBindPoint.VK_PIPELINE_BIND_POINT_GRAPHICS, this.graphicsPipeline);

                VulkanNative.vkCmdDraw(this.commandBuffers[i], 3, 1, 0, 0);

                VulkanNative.vkCmdEndRenderPass(this.commandBuffers[i]);

                Helpers.CheckErrors(VulkanNative.vkEndCommandBuffer(this.commandBuffers[i]));
            }
        }
Exemple #2
0
        private void CreateCommandBuffers()
        {
            var allocInfo = new VkCommandBufferAllocateInfo()
            {
                sType              = VkStructureType.VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
                commandPool        = vkCommandPool,
                level              = VkCommandBufferLevel.VK_COMMAND_BUFFER_LEVEL_PRIMARY,
                commandBufferCount = (uint)vkSwapChainFramebuffers.Length,
            };

            vkCommandBuffers = new VkCommandBuffer[vkSwapChainFramebuffers.Length];

            fixed(VkCommandBuffer *newCommandBuffer = vkCommandBuffers)
            VulkanNative.vkAllocateCommandBuffers(vkDevice, &allocInfo, newCommandBuffer);

            for (int i = 0; i < vkCommandBuffers.Length; i++)
            {
                var commandBuffer = vkCommandBuffers[i];

                var beginInfo = new VkCommandBufferBeginInfo()
                {
                    sType            = VkStructureType.VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
                    flags            = VkCommandBufferUsageFlagBits.VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
                    pInheritanceInfo = null,
                };

                var result = VulkanNative.vkBeginCommandBuffer(commandBuffer, &beginInfo);
                Helpers.CheckErrors(result);

                VkClearValue clearColor = new VkClearValue()
                {
                    color = new VkClearColorValue(0, 0, 0, 1f),
                };

                var renderPassInfo = new VkRenderPassBeginInfo()
                {
                    sType       = VkStructureType.VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
                    renderPass  = vkRenderPass,
                    framebuffer = vkSwapChainFramebuffers[i],
                    renderArea  = new VkRect2D()
                    {
                        extent = vkSwapChainExtent
                    },
                    pClearValues    = &clearColor,
                    clearValueCount = 1,
                };

                VulkanNative.vkCmdBeginRenderPass(commandBuffer, &renderPassInfo, VkSubpassContents.VK_SUBPASS_CONTENTS_INLINE);
                VulkanNative.vkCmdBindPipeline(commandBuffer, VkPipelineBindPoint.VK_PIPELINE_BIND_POINT_GRAPHICS, vkGraphicsPipeline);
                VulkanNative.vkCmdDraw(commandBuffer, 3, 1, 0, 0);
                VulkanNative.vkCmdEndRenderPass(commandBuffer);
                result = VulkanNative.vkEndCommandBuffer(commandBuffer);
                Helpers.CheckErrors(result);
            }
        }
Exemple #3
0
        public unsafe void BeginRenderPass(
            Framebuffer framebuffer,
            VkSubpassContents subpassContents
            )
        {
            var renderPass  = framebuffer.RenderPass;
            var clearValues = new NativeList <VkClearValue>();

            foreach (var attachment in renderPass.Attachments)
            {
                if (attachment.Format == RenderPassAttachment.Default.Format)
                {
                    clearValues.Add(new VkClearValue()
                    {
                        color = new VkClearColorValue(
                            0.0f,
                            0.0f,
                            0.0f,
                            0.0f
                            )
                    });
                }
                else if (attachment.Format == RenderPassAttachment.DefaultDepth.Format)
                {
                    clearValues.Add(new VkClearValue()
                    {
                        depthStencil = new VkClearDepthStencilValue(
                            1.0f,
                            0
                            )
                    });
                }
            }

            var renderPassBeginInfo = new VkRenderPassBeginInfo
            {
                sType           = VkStructureType.RenderPassBeginInfo,
                clearValueCount = clearValues.Count,
                pClearValues    = (VkClearValue *)clearValues.Data.ToPointer(),
                framebuffer     = framebuffer.Handle,
                renderPass      = renderPass.Handle,
                renderArea      = new VkRect2D
                {
                    offset = new VkOffset2D
                    {
                        x = 0,
                        y = 0
                    },
                    extent = new VkExtent2D
                    {
                        width  = framebuffer.Width,
                        height = framebuffer.Height
                    }
                }
            };

            VulkanNative.vkCmdBeginRenderPass(
                _handle,
                &renderPassBeginInfo,
                subpassContents
                );
            // update the layout for images
            for (int i = 0; i < framebuffer.Images.Count; i++)
            {
                Array.Fill(framebuffer.Images[i].Layout, framebuffer.RenderPass.Attachments[i].FinalLayout);
            }
        }