Pulled the gui out into VkGUI to separate it out.

This commit is contained in:
Zed A. Shaw 2025-12-08 00:58:09 -05:00
parent 4f7ab6db68
commit 35198bce6b
6 changed files with 198 additions and 166 deletions

151
vk_gui.cpp Normal file
View file

@ -0,0 +1,151 @@
#include "vk_gui.h"
#include "vk_initializers.h"
#include <SDL.h>
#include <SDL_vulkan.h>
#include <imgui.h>
#include <imgui_impl_sdl2.h>
#include <imgui_impl_vulkan.h>
#include "vk_engine.h"
struct SDL_Window* VkGUI::init_window(VkExtent2D windowExtent)
{
SDL_Init(SDL_INIT_VIDEO);
SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_VULKAN);
return SDL_CreateWindow(
"Vulkan Engine",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
int(windowExtent.width),
int(windowExtent.height),
window_flags);
}
void VkGUI::init_imgui(VulkanEngine& engine)
{
// 1. create descriptor pool for IMGUI
// the size of the pool is very oversize, but it's copied from imgui demo
// itself.
VkDescriptorPoolSize pool_sizes[] = {
{ VK_DESCRIPTOR_TYPE_SAMPLER, 1000 },
{ VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1000 },
{ VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1000 },
{ VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1000 },
{ VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, 1000 },
{ VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, 1000 },
{ VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1000 },
{ VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1000 },
{ VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 1000 },
{ VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, 1000 },
{ VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, 1000 }
};
VkDescriptorPoolCreateInfo pool_info{
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT,
.maxSets = 1000,
.poolSizeCount = (uint32_t)std::size(pool_sizes),
.pPoolSizes = pool_sizes,
};
VkDescriptorPool imguiPool;
VK_CHECK(vkCreateDescriptorPool(engine._device, &pool_info, nullptr, &imguiPool));
// 2: initialize the imgui library
ImGui::CreateContext();
ImGui_ImplSDL2_InitForVulkan(engine._window);
ImGui_ImplVulkan_InitInfo init_info{
.Instance = engine._instance,
.PhysicalDevice = engine._chosenGPU,
.Device = engine._device,
.Queue = engine._graphicsQueue,
.DescriptorPool = imguiPool,
.MinImageCount = 3,
.ImageCount = 3,
.MSAASamples = VK_SAMPLE_COUNT_1_BIT,
.UseDynamicRendering = true,
.PipelineRenderingCreateInfo = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO,
.colorAttachmentCount = 1,
.pColorAttachmentFormats = &engine._swapchainImageFormat,
},
};
ImGui_ImplVulkan_Init(&init_info);
ImGui_ImplVulkan_CreateFontsTexture();
engine._mainDeletionQueue.push_function([=,this]() {
ImGui_ImplVulkan_Shutdown();
vkDestroyDescriptorPool(engine._device, imguiPool, nullptr);
});
}
void VkGUI::draw_imgui(VkExtent2D& swapchainExtent, VkCommandBuffer cmd, VkImageView targetImageView)
{
VkRenderingAttachmentInfo colorAttachment = vkinit::attachment_info(targetImageView, nullptr, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
VkRenderingInfo renderInfo = vkinit::rendering_info(swapchainExtent, &colorAttachment, nullptr);
vkCmdBeginRendering(cmd, &renderInfo);
ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), cmd);
vkCmdEndRendering(cmd);
}
void VkGUI::render_imgui(std::vector<ComputeEffect>& backgroundEffects, int* currentBackgroundEffect)
{
ImGui_ImplVulkan_NewFrame();
ImGui_ImplSDL2_NewFrame();
ImGui::NewFrame();
if (ImGui::Begin("background")) {
ComputeEffect& selected = backgroundEffects[*currentBackgroundEffect];
ImGui::Text("Selected effect: %s", selected.name);
ImGui::SliderInt("Effect Index", currentBackgroundEffect,0, backgroundEffects.size() - 1);
ImGui::InputFloat4("data1",(float*)& selected.data.data1);
ImGui::InputFloat4("data2",(float*)& selected.data.data2);
ImGui::InputFloat4("data3",(float*)& selected.data.data3);
ImGui::InputFloat4("data4",(float*)& selected.data.data4);
}
ImGui::End();
ImGui::Render();
}
void VkGUI::destroy(struct SDL_Window* _window)
{
SDL_DestroyWindow(_window);
}
void VkGUI::poll_event() {
SDL_Event event;
while(SDL_PollEvent(&event) != 0) {
//close the window when user alt-f4s or clicks the X button
if(event.type == SDL_QUIT) {
should_quit = true;
}
if(event.type == SDL_WINDOWEVENT) {
if(event.window.event == SDL_WINDOWEVENT_MINIMIZED) {
stop_rendering = true;
}
if(event.window.event == SDL_WINDOWEVENT_RESTORED) {
stop_rendering = false;
}
}
ImGui_ImplSDL2_ProcessEvent(&event);
}
}