xo-imgui: refactor ex4a: record_command_buffer()

This commit is contained in:
Roland Conybeare 2025-11-11 16:44:16 -05:00
commit e74ab54a8b
2 changed files with 57 additions and 50 deletions

View file

@ -568,4 +568,59 @@ MinimalImGuiVulkan::draw_frame()
this->current_frame_ = (current_frame_ + 1) % MAX_FRAMES_IN_FLIGHT; this->current_frame_ = (current_frame_ + 1) % MAX_FRAMES_IN_FLIGHT;
} }
void
MinimalImGuiVulkan::record_command_buffer(VkCommandBuffer cmdbuf, uint32_t image_ix)
{
VkCommandBufferBeginInfo begin_info{};
begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
if (vkBeginCommandBuffer(cmdbuf, &begin_info) != VK_SUCCESS) {
throw std::runtime_error("Failed to begin recording command buffer!");
}
VkRenderPassBeginInfo render_pass_info{};
render_pass_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
render_pass_info.renderPass = render_pass_;
render_pass_info.framebuffer = framebuffers_[image_ix];
render_pass_info.renderArea.offset = {0, 0};
render_pass_info.renderArea.extent = swapchain_extent_;
VkClearValue clear_color = {{{0.0f, 0.0f, 0.0f, 1.0f}}};
render_pass_info.clearValueCount = 1;
render_pass_info.pClearValues = &clear_color;
vkCmdBeginRenderPass(cmdbuf, &render_pass_info, VK_SUBPASS_CONTENTS_INLINE);
// Start the Dear ImGui frame
{
ImGui_ImplVulkan_NewFrame();
ImGui_ImplSDL2_NewFrame();
ImGui::NewFrame();
// Create a simple ImGui window
ImGui::Begin("Hello, Vulkan + SDL2!");
ImGui::Text("This is a minimal ImGui + Vulkan + SDL2 example!");
static float f = 0.0f;
static int counter = 0;
ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
if (ImGui::Button("Button"))
counter++;
ImGui::SameLine();
ImGui::Text("counter = %d", counter);
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
ImGui::End();
// Rendering
ImGui::Render();
ImDrawData* draw_data = ImGui::GetDrawData();
ImGui_ImplVulkan_RenderDrawData(draw_data, cmdbuf);
}
vkCmdEndRenderPass(cmdbuf);
if (vkEndCommandBuffer(cmdbuf) != VK_SUCCESS) {
throw std::runtime_error("Failed to record command buffer!");
}
}
/* end VulkanApp.cpp */ /* end VulkanApp.cpp */

View file

@ -117,56 +117,8 @@ private:
*/ */
void draw_frame(); void draw_frame();
void record_command_buffer(VkCommandBuffer commandBuffer, uint32_t imageIndex) { /* record draw instructions into cmdbuf, framebuffers_[image_ix] */
VkCommandBufferBeginInfo beginInfo{}; void record_command_buffer(VkCommandBuffer commandBuffer, uint32_t image_ix);
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
if (vkBeginCommandBuffer(commandBuffer, &beginInfo) != VK_SUCCESS) {
throw std::runtime_error("Failed to begin recording command buffer!");
}
VkRenderPassBeginInfo renderPassInfo{};
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
renderPassInfo.renderPass = render_pass_;
renderPassInfo.framebuffer = framebuffers_[imageIndex];
renderPassInfo.renderArea.offset = {0, 0};
renderPassInfo.renderArea.extent = swapchain_extent_;
VkClearValue clearColor = {{{0.0f, 0.0f, 0.0f, 1.0f}}};
renderPassInfo.clearValueCount = 1;
renderPassInfo.pClearValues = &clearColor;
vkCmdBeginRenderPass(commandBuffer, &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
// Start the Dear ImGui frame
ImGui_ImplVulkan_NewFrame();
ImGui_ImplSDL2_NewFrame();
ImGui::NewFrame();
// Create a simple ImGui window
ImGui::Begin("Hello, Vulkan + SDL2!");
ImGui::Text("This is a minimal ImGui + Vulkan + SDL2 example!");
static float f = 0.0f;
static int counter = 0;
ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
if (ImGui::Button("Button"))
counter++;
ImGui::SameLine();
ImGui::Text("counter = %d", counter);
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
ImGui::End();
// Rendering
ImGui::Render();
ImDrawData* draw_data = ImGui::GetDrawData();
ImGui_ImplVulkan_RenderDrawData(draw_data, commandBuffer);
vkCmdEndRenderPass(commandBuffer);
if (vkEndCommandBuffer(commandBuffer) != VK_SUCCESS) {
throw std::runtime_error("Failed to record command buffer!");
}
}
void recreate_swapchain() { void recreate_swapchain() {
// handle window minimization: wait until window has valid size // handle window minimization: wait until window has valid size