Pulled the gui out into VkGUI to separate it out.
This commit is contained in:
parent
4f7ab6db68
commit
35198bce6b
6 changed files with 198 additions and 166 deletions
155
vk_engine.cpp
155
vk_engine.cpp
|
|
@ -3,18 +3,13 @@
|
|||
#include "vk_pipelines.h"
|
||||
#include <print>
|
||||
|
||||
#include <SDL.h>
|
||||
#include <SDL_vulkan.h>
|
||||
|
||||
#include <vk_types.h>
|
||||
#include <vk_initializers.h>
|
||||
#include "VkBootstrap.h"
|
||||
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
#include "imgui.h"
|
||||
#include "imgui_impl_sdl2.h"
|
||||
#include "imgui_impl_vulkan.h"
|
||||
#include <SDL_vulkan.h>
|
||||
|
||||
#define VMA_IMPLEMENTATION
|
||||
#include "vk_mem_alloc.h"
|
||||
|
|
@ -32,18 +27,7 @@ void VulkanEngine::init()
|
|||
assert(loadedEngine == nullptr);
|
||||
loadedEngine = this;
|
||||
|
||||
// We initialize SDL and create a window with it.
|
||||
SDL_Init(SDL_INIT_VIDEO);
|
||||
|
||||
SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_VULKAN);
|
||||
|
||||
_window = SDL_CreateWindow(
|
||||
"Vulkan Engine",
|
||||
SDL_WINDOWPOS_UNDEFINED,
|
||||
SDL_WINDOWPOS_UNDEFINED,
|
||||
int(_windowExtent.width),
|
||||
int(_windowExtent.height),
|
||||
window_flags);
|
||||
_window = _gui.init_window(_windowExtent);
|
||||
|
||||
init_vulkan();
|
||||
init_swapchain();
|
||||
|
|
@ -51,7 +35,7 @@ void VulkanEngine::init()
|
|||
init_sync_structures();
|
||||
init_descriptors();
|
||||
init_pipelines();
|
||||
init_imgui();
|
||||
_gui.init_imgui(VulkanEngine::Get());
|
||||
|
||||
//everything went fine
|
||||
_isInitialized = true;
|
||||
|
|
@ -81,7 +65,7 @@ void VulkanEngine::cleanup()
|
|||
vkDestroyDevice(_device, nullptr);
|
||||
vkb::destroy_debug_utils_messenger(_instance, _debug_messenger);
|
||||
vkDestroyInstance(_instance, nullptr);
|
||||
SDL_DestroyWindow(_window);
|
||||
_gui.destroy(_window);
|
||||
}
|
||||
|
||||
loadedEngine = nullptr;
|
||||
|
|
@ -125,7 +109,7 @@ void VulkanEngine::draw()
|
|||
|
||||
vkutil::transition_image(cmd, _swapchainImages[swapchainImageIndex], VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
|
||||
|
||||
draw_imgui(cmd, _swapchainImageViews[swapchainImageIndex]);
|
||||
_gui.draw_imgui(_swapchainExtent, cmd, _swapchainImageViews[swapchainImageIndex]);
|
||||
|
||||
vkutil::transition_image(cmd, _swapchainImages[swapchainImageIndex], VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR);
|
||||
|
||||
|
|
@ -168,68 +152,21 @@ void VulkanEngine::draw()
|
|||
|
||||
void VulkanEngine::run()
|
||||
{
|
||||
SDL_Event e;
|
||||
bool bQuit = false;
|
||||
bool stop_rendering = false;
|
||||
|
||||
//main loop
|
||||
while(!bQuit)
|
||||
while(!_gui.should_quit)
|
||||
{
|
||||
//Handle events on queue
|
||||
while(SDL_PollEvent(&e) != 0)
|
||||
{
|
||||
//close the window when user alt-f4s or clicks the X button
|
||||
if(e.type == SDL_QUIT) {
|
||||
bQuit = true;
|
||||
}
|
||||
_gui.poll_event();
|
||||
|
||||
if(e.type == SDL_WINDOWEVENT) {
|
||||
if(e.window.event == SDL_WINDOWEVENT_MINIMIZED) {
|
||||
stop_rendering = true;
|
||||
}
|
||||
|
||||
if(e.window.event == SDL_WINDOWEVENT_RESTORED) {
|
||||
stop_rendering = false;
|
||||
}
|
||||
}
|
||||
|
||||
ImGui_ImplSDL2_ProcessEvent(&e);
|
||||
}
|
||||
|
||||
if(stop_rendering) {
|
||||
if(_gui.stop_rendering) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
continue;
|
||||
}
|
||||
|
||||
render_imgui();
|
||||
_gui.render_imgui(backgroundEffects, ¤tBackgroundEffect);
|
||||
draw();
|
||||
}
|
||||
}
|
||||
|
||||
void VulkanEngine::render_imgui() {
|
||||
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", ¤tBackgroundEffect,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 VulkanEngine::init_vulkan() {
|
||||
vkb::InstanceBuilder builder;
|
||||
|
||||
|
|
@ -570,81 +507,7 @@ void VulkanEngine::immediate_submit(std::function<void(VkCommandBuffer cmd)>&& f
|
|||
|
||||
VK_CHECK(vkQueueSubmit2(_graphicsQueue, 1, &submit, _immFence));
|
||||
VK_CHECK(vkWaitForFences(_device, 1, &_immFence, true,9999999999));
|
||||
|
||||
}
|
||||
|
||||
|
||||
void VulkanEngine::init_imgui()
|
||||
{
|
||||
// 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(_device, &pool_info, nullptr, &imguiPool));
|
||||
|
||||
// 2: initialize the imgui library
|
||||
ImGui::CreateContext();
|
||||
|
||||
ImGui_ImplSDL2_InitForVulkan(_window);
|
||||
|
||||
ImGui_ImplVulkan_InitInfo init_info{
|
||||
.Instance = _instance,
|
||||
.PhysicalDevice = _chosenGPU,
|
||||
.Device = _device,
|
||||
.Queue = _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 = &_swapchainImageFormat,
|
||||
},
|
||||
};
|
||||
|
||||
ImGui_ImplVulkan_Init(&init_info);
|
||||
ImGui_ImplVulkan_CreateFontsTexture();
|
||||
|
||||
_mainDeletionQueue.push_function([=,this]() {
|
||||
ImGui_ImplVulkan_Shutdown();
|
||||
vkDestroyDescriptorPool(_device, imguiPool, nullptr);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
void VulkanEngine::draw_imgui(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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue