完成命令缓冲区初始化

master
InkSoul 2024-03-09 22:42:37 +08:00
parent b095fdb291
commit 2ad423318c
2 changed files with 103 additions and 0 deletions

View File

@ -116,6 +116,11 @@ void HelloTriangleApplication::initVulkan() {
createGraphicPipeline(); createGraphicPipeline();
creatFramebuffers(); creatFramebuffers();
createCommandPool();
createCommandBuffer();
} }
@ -125,6 +130,7 @@ void HelloTriangleApplication::mainLoop(GLFWwindow* window){
while (!glfwWindowShouldClose(window)) while (!glfwWindowShouldClose(window))
{ {
glfwPollEvents(); glfwPollEvents();
drawFrame();
} }
} }
@ -135,6 +141,7 @@ void HelloTriangleApplication::cleanup(GLFWwindow* window) {
std::cout << "\nstart to destroy resource\n" << std::endl; std::cout << "\nstart to destroy resource\n" << std::endl;
vkDestroyCommandPool(device, commandPool, nullptr);
for (auto framebuffer : swapChainFramebuffers) for (auto framebuffer : swapChainFramebuffers)
{ {
@ -640,6 +647,91 @@ void HelloTriangleApplication::creatFramebuffers()
} }
} }
void HelloTriangleApplication::createCommandPool()
{
QueueFamilyIndices queueFamilyIndices = findQueueFamilies(physicalDevice);
VkCommandPoolCreateInfo poolInfo{};
poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
poolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
poolInfo.queueFamilyIndex = queueFamilyIndices.graphicsFamily.value();
if (vkCreateCommandPool(device,&poolInfo,nullptr,&commandPool) != VK_SUCCESS)
{
throw std::runtime_error("failed to create command pool in createCommandpool");
}
}
void HelloTriangleApplication::createCommandBuffer()
{
VkCommandBufferAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
allocInfo.commandPool = commandPool;
allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
allocInfo.commandBufferCount = 1;
if (vkAllocateCommandBuffers(device,&allocInfo,&commandBuffer) != VK_SUCCESS)
{
throw std::runtime_error("failed to allocate command buffers in createCommandBuffer");
}
}
void HelloTriangleApplication::recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex)
{
VkCommandBufferBeginInfo beginInfo{};
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
beginInfo.flags = 0;
beginInfo.pInheritanceInfo = nullptr;
if (vkBeginCommandBuffer(commandBuffer,&beginInfo) != VK_SUCCESS)
{
throw std::runtime_error("failed to begin recording command buffer in recordCommandBuffer");
}
VkRenderPassBeginInfo renderPassBeginInfo{};
renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
renderPassBeginInfo.renderPass = renderPass;
renderPassBeginInfo.framebuffer = swapChainFramebuffers[imageIndex];
renderPassBeginInfo.renderArea.extent = swapChainExtent;
VkClearValue clearColor = { {{0.0f,0.0f,0.0f,1.0f}} };
renderPassBeginInfo.clearValueCount = 1;
renderPassBeginInfo.pClearValues = &clearColor;
vkCmdBeginRenderPass(commandBuffer, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline);
VkViewport viewport{};
viewport.x = 0.0f;
viewport.y = 0.0f;
viewport.width = static_cast<float>(swapChainExtent.width);
viewport.height = static_cast<float>(swapChainExtent.height);
viewport.minDepth = 0.0f;
viewport.maxDepth = 1.0f;
vkCmdSetViewport(commandBuffer, 0, 1, &viewport);
VkRect2D scissor{};
scissor.offset = { 0,0 };
scissor.extent = swapChainExtent;
vkCmdSetScissor(commandBuffer, 0, 1, &scissor);
vkCmdDraw(commandBuffer, 3, 1, 0, 0);
vkCmdEndRenderPass(commandBuffer);
if (vkEndCommandBuffer(commandBuffer) != VK_SUCCESS)
{
throw std::runtime_error("failed to record command buffer in recordCommandBuffer");
}
}
void HelloTriangleApplication::drawFrame()
{
}
void HelloTriangleApplication::createRenderPass() void HelloTriangleApplication::createRenderPass()
{ {

View File

@ -87,6 +87,9 @@ private:
std::vector<VkFramebuffer> swapChainFramebuffers; std::vector<VkFramebuffer> swapChainFramebuffers;
VkCommandPool commandPool;
VkCommandBuffer commandBuffer;
GLFWwindow* initWindow(int Width, int Height); GLFWwindow* initWindow(int Width, int Height);
@ -142,6 +145,14 @@ private:
void creatFramebuffers(); void creatFramebuffers();
void createCommandPool();
void createCommandBuffer();
void recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex);
void drawFrame();
}; };
HelloTriangleApplication::HelloTriangleApplication() HelloTriangleApplication::HelloTriangleApplication()