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;
}
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 */