ImGUI is now working in the program.
This commit is contained in:
parent
fe79797313
commit
14f307b1b3
5 changed files with 190 additions and 7 deletions
135
vk_engine.cpp
135
vk_engine.cpp
|
|
@ -12,11 +12,14 @@
|
|||
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
#include "imgui.h"
|
||||
#include "imgui_impl_sdl2.h"
|
||||
#include "imgui_impl_vulkan.h"
|
||||
|
||||
#define VMA_IMPLEMENTATION
|
||||
#include "vk_mem_alloc.h"
|
||||
|
||||
constexpr bool bUseValidationLayers = true;
|
||||
constexpr bool bUseValidationLayers = false;
|
||||
|
||||
VulkanEngine* loadedEngine = nullptr;
|
||||
|
||||
|
|
@ -48,6 +51,7 @@ void VulkanEngine::init()
|
|||
init_sync_structures();
|
||||
init_descriptors();
|
||||
init_pipelines();
|
||||
init_imgui();
|
||||
|
||||
//everything went fine
|
||||
_isInitialized = true;
|
||||
|
|
@ -119,6 +123,10 @@ void VulkanEngine::draw()
|
|||
_swapchainImages[swapchainImageIndex],
|
||||
_drawExtent, _swapchainExtent);
|
||||
|
||||
vkutil::transition_image(cmd, _swapchainImages[swapchainImageIndex], VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
|
||||
|
||||
draw_imgui(cmd, _swapchainImageViews[swapchainImageIndex]);
|
||||
|
||||
vkutil::transition_image(cmd, _swapchainImages[swapchainImageIndex], VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR);
|
||||
|
||||
VK_CHECK(vkEndCommandBuffer(cmd));
|
||||
|
|
@ -142,7 +150,7 @@ void VulkanEngine::draw()
|
|||
// this will put the image we just rendered to into the visible window.
|
||||
// we want to wait on the _renderSemaphore for that,
|
||||
// as its necessary that drawing commands have finished before the image is displayed to the user
|
||||
VkPresentInfoKHR presentInfo = {};
|
||||
VkPresentInfoKHR presentInfo{};
|
||||
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
|
||||
presentInfo.pNext = nullptr;
|
||||
presentInfo.pSwapchains = &_swapchain;
|
||||
|
|
@ -185,6 +193,8 @@ void VulkanEngine::run()
|
|||
stop_rendering = false;
|
||||
}
|
||||
}
|
||||
|
||||
ImGui_ImplSDL2_ProcessEvent(&e);
|
||||
}
|
||||
|
||||
if(stop_rendering) {
|
||||
|
|
@ -192,6 +202,13 @@ void VulkanEngine::run()
|
|||
continue;
|
||||
}
|
||||
|
||||
ImGui_ImplVulkan_NewFrame();
|
||||
ImGui_ImplSDL2_NewFrame();
|
||||
ImGui::NewFrame();
|
||||
|
||||
ImGui::ShowDemoWindow();
|
||||
ImGui::Render();
|
||||
|
||||
draw();
|
||||
}
|
||||
}
|
||||
|
|
@ -345,6 +362,16 @@ void VulkanEngine::init_commands() {
|
|||
|
||||
VK_CHECK(vkAllocateCommandBuffers(_device, &cmdAllocInfo, &_frames[i]._mainCommandBuffer));
|
||||
}
|
||||
|
||||
VK_CHECK(vkCreateCommandPool(_device, &commandPoolInfo, nullptr, &_immCommandPool));
|
||||
|
||||
VkCommandBufferAllocateInfo cmdAllocInfo = vkinit::command_buffer_allocate_info(_immCommandPool, 1);
|
||||
|
||||
VK_CHECK(vkAllocateCommandBuffers(_device, &cmdAllocInfo, &_immCommandBuffer));
|
||||
|
||||
_mainDeletionQueue.push_function([=,this]() {
|
||||
vkDestroyCommandPool(_device, _immCommandPool, nullptr);
|
||||
});
|
||||
}
|
||||
|
||||
void VulkanEngine::init_sync_structures() {
|
||||
|
|
@ -357,6 +384,12 @@ void VulkanEngine::init_sync_structures() {
|
|||
VK_CHECK(vkCreateSemaphore(_device, &semaphoreCreateInfo, nullptr, &_frames[i]._swapchainSemaphore));
|
||||
VK_CHECK(vkCreateSemaphore(_device, &semaphoreCreateInfo, nullptr, &_frames[i]._renderSemaphore));
|
||||
}
|
||||
|
||||
VK_CHECK(vkCreateFence(_device, &fenceCreateInfo, nullptr, &_immFence));
|
||||
|
||||
_mainDeletionQueue.push_function([=,this]() {
|
||||
vkDestroyFence(_device, _immFence, nullptr);
|
||||
});
|
||||
}
|
||||
|
||||
void VulkanEngine::draw_background(VkCommandBuffer cmd)
|
||||
|
|
@ -405,14 +438,14 @@ void VulkanEngine::init_descriptors() {
|
|||
drawImageWrite.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
|
||||
drawImageWrite.pImageInfo = &imgInfo;
|
||||
|
||||
vkUpdateDescriptorSets(_device, 1, &drawImageWrite, 0, nullptr);
|
||||
vkUpdateDescriptorSets(_device, 1, &drawImageWrite, 0, nullptr),
|
||||
|
||||
//make sure both the descriptor allocator and the new layout get cleaned up properly
|
||||
_mainDeletionQueue.push_function([&]() {
|
||||
globalDescriptorAllocator.destroy_pool(_device);
|
||||
|
||||
vkDestroyDescriptorSetLayout(_device, _drawImageDescriptorLayout, nullptr);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
void VulkanEngine::init_pipelines()
|
||||
|
|
@ -458,3 +491,97 @@ void VulkanEngine::init_background_pipelines()
|
|||
vkDestroyPipeline(_device, _gradientPipeline, nullptr);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
void VulkanEngine::immediate_submit(std::function<void(VkCommandBuffer cmd)>&& function)
|
||||
{
|
||||
VK_CHECK(vkResetFences(_device, 1, &_immFence));
|
||||
VK_CHECK(vkResetCommandBuffer(_immCommandBuffer, 0));
|
||||
|
||||
VkCommandBuffer cmd = _immCommandBuffer;
|
||||
|
||||
VkCommandBufferBeginInfo cmdBeginInfo = vkinit::command_buffer_begin_info(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT);
|
||||
|
||||
VK_CHECK(vkBeginCommandBuffer(cmd, &cmdBeginInfo));
|
||||
|
||||
function(cmd);
|
||||
|
||||
VK_CHECK(vkEndCommandBuffer(cmd));
|
||||
|
||||
VkCommandBufferSubmitInfo cmdinfo = vkinit::command_buffer_submit_info(cmd);
|
||||
VkSubmitInfo2 submit = vkinit::submit_info(&cmdinfo, nullptr, nullptr);
|
||||
|
||||
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{};
|
||||
pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
|
||||
pool_info.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT;
|
||||
pool_info.maxSets = 1000;
|
||||
pool_info.poolSizeCount = (uint32_t)std::size(pool_sizes);
|
||||
pool_info.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{};
|
||||
init_info.Instance = _instance;
|
||||
init_info.PhysicalDevice = _chosenGPU;
|
||||
init_info.Device = _device;
|
||||
init_info.Queue = _graphicsQueue;
|
||||
init_info.DescriptorPool = imguiPool;
|
||||
init_info.MinImageCount = 3;
|
||||
init_info.ImageCount = 3;
|
||||
init_info.UseDynamicRendering = true;
|
||||
|
||||
init_info.PipelineRenderingCreateInfo = {.sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO};
|
||||
init_info.PipelineRenderingCreateInfo.colorAttachmentCount = 1;
|
||||
init_info.PipelineRenderingCreateInfo.pColorAttachmentFormats = &_swapchainImageFormat;
|
||||
|
||||
init_info.MSAASamples = VK_SAMPLE_COUNT_1_BIT;
|
||||
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