From 32ac2cd853d4f9ca6ab457e1122747882c031f5a Mon Sep 17 00:00:00 2001 From: JimmyBinoculars Date: Fri, 20 Feb 2026 11:58:57 +0000 Subject: [PATCH] Finished command buffers --- cve_pipeline.cpp | 4 ++++ cve_pipeline.hpp | 1 + first_app.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/cve_pipeline.cpp b/cve_pipeline.cpp index 320fc8b..903711b 100644 --- a/cve_pipeline.cpp +++ b/cve_pipeline.cpp @@ -114,6 +114,10 @@ namespace cve { } } + void CvePipeline::bind(VkCommandBuffer commandBuffer){ + vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline); + } + PipelineConfigInfo CvePipeline::defaultPipelineConfigInfo(uint32_t width, uint32_t height) { PipelineConfigInfo configInfo{}; diff --git a/cve_pipeline.hpp b/cve_pipeline.hpp index ea1a5c4..092b677 100644 --- a/cve_pipeline.hpp +++ b/cve_pipeline.hpp @@ -28,6 +28,7 @@ namespace cve { CvePipeline(const CvePipeline&) = delete; void operator=(const CvePipeline&) = delete; + void bind(VkCommandBuffer commandBuffer); static PipelineConfigInfo defaultPipelineConfigInfo(uint32_t width, uint32_t height); private: diff --git a/first_app.cpp b/first_app.cpp index de3a35c..b9b15c5 100644 --- a/first_app.cpp +++ b/first_app.cpp @@ -1,6 +1,7 @@ #include "first_app.hpp" #include +#include namespace cve { FirstApp::FirstApp() { @@ -43,6 +44,52 @@ namespace cve { ); } - void FirstApp::createCommandBuffers() {}; + void FirstApp::createCommandBuffers() { + commandBuffers.resize(cveSwapChain.imageCount()); + + VkCommandBufferAllocateInfo allocInfo{}; + allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; + allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; + allocInfo.commandPool = cveDevice.getCommandPool(); + allocInfo.commandBufferCount = static_cast(commandBuffers.size()); + + if (vkAllocateCommandBuffers(cveDevice.device(), &allocInfo, commandBuffers.data()) != + VK_SUCCESS) { + throw std::runtime_error("Failed to allocate command buffers"); + } + + for (int i=0; i < commandBuffers.size(); i++) { + VkCommandBufferBeginInfo beginInfo{}; + beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; + + if (vkBeginCommandBuffer(commandBuffers[i], &beginInfo) != VK_SUCCESS) { + throw std::runtime_error("Failed to begin command buffer recording"); + } + + VkRenderPassBeginInfo renderPassInfo{}; + renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; + renderPassInfo.renderPass = cveSwapChain.getRenderPass(); + renderPassInfo.framebuffer = cveSwapChain.getFrameBuffer(i); + + renderPassInfo.renderArea.offset = {0, 0}; + renderPassInfo.renderArea.extent = cveSwapChain.getSwapChainExtent(); + + std::array clearValues{}; + clearValues[0].color = {0.1f, 0.1f, 0.1f, 1.0f}; + clearValues[1].depthStencil = {1.0f, 0}; + renderPassInfo.clearValueCount = static_cast(clearValues.size()); + renderPassInfo.pClearValues = clearValues.data(); + + vkCmdBeginRenderPass(commandBuffers[i], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE); + + cvePipeline->bind(commandBuffers[i]); + vkCmdDraw(commandBuffers[i], 3, 1, 0, 0); + + vkCmdEndRenderPass(commandBuffers[i]); + if (vkEndCommandBuffer(commandBuffers[i]) != VK_SUCCESS) { + throw std::runtime_error("Failed to record command buffer!"); + } + } + } void FirstApp::drawFrame() {}; }