예제 #1
0
        private void createCommandBuffers()
        {
            int commandBuffersCount = swapChainFramebuffers.Length;

            commandBuffers = new VkCommandBuffer[commandBuffersCount];

            VkCommandBufferAllocateInfo allocInfo = new VkCommandBufferAllocateInfo();

            allocInfo.sType              = VkStructureType.VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
            allocInfo.commandPool        = commandPool;
            allocInfo.level              = VkCommandBufferLevel.VK_COMMAND_BUFFER_LEVEL_PRIMARY;
            allocInfo.commandBufferCount = commandBuffersCount;

            VkResult result = Vulkan.vkAllocateCommandBuffers(device, allocInfo, commandBuffers);

            if (result != VkResult.VK_SUCCESS)
            {
                throw Program.Throw("failed to allocate command buffers!", result);
            }

            for (int i = 0; i < commandBuffersCount; i++)
            {
                VkCommandBufferBeginInfo beginInfo = new VkCommandBufferBeginInfo();
                beginInfo.sType = VkStructureType.VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
                beginInfo.flags = VkCommandBufferUsageFlagBits.VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;

                Vulkan.vkBeginCommandBuffer(commandBuffers[i], beginInfo);

                VkRenderPassBeginInfo renderPassInfo = new VkRenderPassBeginInfo();
                renderPassInfo.sType             = VkStructureType.VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
                renderPassInfo.renderPass        = renderPass;
                renderPassInfo.framebuffer       = swapChainFramebuffers[i];
                renderPassInfo.renderArea.offset = VkOffset2D.Create(0, 0);
                renderPassInfo.renderArea.extent = swapChainExtent;

                VkClearValue clearColor = VkClearValue.Create(0.01f, 0.03f, 0.01f, 1.0f);
                renderPassInfo.clearValueCount = 1;
                renderPassInfo.pClearValues    = new VkClearValue[] { clearColor };

                Vulkan.vkCmdBeginRenderPass(commandBuffers[i], renderPassInfo, VkSubpassContents.VK_SUBPASS_CONTENTS_INLINE);

                Vulkan.vkCmdBindPipeline(commandBuffers[i], VkPipelineBindPoint.VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline);

                VkBuffer[] vertexBuffers = new VkBuffer[] { vertexBuffer };
                int[]      offsets       = new int[] { 0 };
                Vulkan.vkCmdBindVertexBuffers(commandBuffers[i], 0, 1, vertexBuffers, offsets);

                Vulkan.vkCmdDraw(commandBuffers[i], vertices.Length, 1, 0, 0);

                Vulkan.vkCmdEndRenderPass(commandBuffers[i]);

                result = Vulkan.vkEndCommandBuffer(commandBuffers[i]);
                if (result != VkResult.VK_SUCCESS)
                {
                    throw Program.Throw("failed to record command buffer!", result);
                }
            }
        }