diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2023-08-25 23:29:38 -0400 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2023-08-25 23:29:38 -0400 |
| commit | 135aefb1f8f48077fdafb898d547f7afcf027312 (patch) | |
| tree | 47cbbe51f943c90c5d4dc6970ee02c71a6316364 /src | |
| parent | e8d97366ef74db62d0927d7e4bf93a74bbd39d23 (diff) | |
impelemented imgui sans input handling
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.cpp | 2 | ||||
| -rw-r--r-- | src/window.cpp | 158 | ||||
| -rw-r--r-- | src/window.hpp | 3 |
3 files changed, 154 insertions, 9 deletions
diff --git a/src/main.cpp b/src/main.cpp index d221f50..026ddb8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -70,9 +70,7 @@ int main() { Tick(deltaThisTick); if (shouldRender) { - PreRender(); Render(); - PostRender(); } if (shouldRender == false && (currentTimePoint - lastLogTimePoint).count() > std::chrono::nanoseconds::period::den) { diff --git a/src/window.cpp b/src/window.cpp index aa304e8..55473c4 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -17,12 +17,14 @@ VkPhysicalDevice vkPhysicalDevice = nullptr; VkDevice vkDevice = nullptr; VkQueue graphicsQueue = nullptr; VkQueue presentQueue = nullptr; +VkQueue transferQueue = nullptr; VkSurfaceKHR vkSurfaceKHR = nullptr; VkDebugReportCallbackEXT vkDebugReport = nullptr; VkAllocationCallbacks vkAllocatorStruct = {}; VkAllocationCallbacks *vkAllocator = nullptr; unsigned int graphicsFamilyIndex; unsigned int presentFamilyIndex; +unsigned int transferFamilyIndex; /* * Instantiation @@ -48,16 +50,34 @@ VkDescriptorSet *presentDescriptorSets = nullptr; VkPipelineLayout pipelineLayout; VkPipeline graphicsPipeline; VkFramebuffer *swapchainFramebuffers = nullptr; +VkCommandPool transferCommandPool; +VkCommandBuffer transferCommandBuffer; VkCommandPool vkCommandPool; VkCommandBuffer presentCommandBuffers[MAX_FRAMES_IN_FLIGHT]; VkSemaphore presentImageAvailableSemaphores[MAX_FRAMES_IN_FLIGHT]; VkSemaphore presentRenderFinishedSemaphores[MAX_FRAMES_IN_FLIGHT]; VkFence presentInFlightFences[MAX_FRAMES_IN_FLIGHT]; +/* + * ImGui + */ +bool isImGuiRenderActive = true; +VkDescriptorPool imGuiDescriptorPool; + const std::vector<const char *> REQUIRED_EXTENSIONS = std::vector<const char *> { VK_KHR_SWAPCHAIN_EXTENSION_NAME }; +static void ImGuiCheckVkResult(VkResult err) { + if (err == 0) { + return; + } + fprintf(stderr, "[imgui][vulkan] Error: VkResult = %d\n", err); + if (err < 0) { + abort(); + } +} + VkBool32 UserDebugCallback( VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, VkDebugUtilsMessageTypeFlagsEXT messageType, @@ -286,14 +306,14 @@ void InitVulkan() { // Create logical device { - unsigned int transferQueueIndex = FindQueueFamilyIndex(vkPhysicalDevice, 0, VK_QUEUE_TRANSFER_BIT, VK_QUEUE_GRAPHICS_BIT); - if (transferQueueIndex == 0xFFFFFFFF) { - transferQueueIndex = FindQueueFamilyIndex(vkPhysicalDevice, -1, VK_QUEUE_TRANSFER_BIT, VK_QUEUE_GRAPHICS_BIT); + transferFamilyIndex = FindQueueFamilyIndex(vkPhysicalDevice, 0, VK_QUEUE_TRANSFER_BIT, VK_QUEUE_GRAPHICS_BIT); + if (transferFamilyIndex == 0xFFFFFFFF) { + transferFamilyIndex = FindQueueFamilyIndex(vkPhysicalDevice, -1, VK_QUEUE_TRANSFER_BIT, VK_QUEUE_GRAPHICS_BIT); } - if (transferQueueIndex == 0xFFFFFFFF) { - transferQueueIndex = FindQueueFamilyIndex(vkPhysicalDevice, -1, VK_QUEUE_TRANSFER_BIT); + if (transferFamilyIndex == 0xFFFFFFFF) { + transferFamilyIndex = FindQueueFamilyIndex(vkPhysicalDevice, -1, VK_QUEUE_TRANSFER_BIT); } - if (transferQueueIndex == 0xFFFFFFFF) { + if (transferFamilyIndex == 0xFFFFFFFF) { printf("Failed to find transfer queue index"); throw("Failed to find transfer queue index"); } @@ -312,7 +332,7 @@ void InitVulkan() { deviceQueueCreateInfos[1].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; deviceQueueCreateInfos[1].pNext = nullptr; deviceQueueCreateInfos[1].flags = 0; - deviceQueueCreateInfos[1].queueFamilyIndex = transferQueueIndex; + deviceQueueCreateInfos[1].queueFamilyIndex = transferFamilyIndex; deviceQueueCreateInfos[1].queueCount = 1l; deviceQueueCreateInfos[1].pQueuePriorities = transferPriorities; @@ -349,6 +369,7 @@ void InitVulkan() { { vkGetDeviceQueue(vkDevice, graphicsFamilyIndex, 0, &graphicsQueue); vkGetDeviceQueue(vkDevice, presentFamilyIndex, 0, &presentQueue); + vkGetDeviceQueue(vkDevice, transferFamilyIndex, 0, &transferQueue); } // generic present sampler @@ -802,6 +823,13 @@ void CreateCommandPool() { if (result != VK_SUCCESS) { throw "failed to create command pool"; } + + poolInfo.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT; + poolInfo.queueFamilyIndex = transferFamilyIndex; + result = vkCreateCommandPool(vkDevice, &poolInfo, vkAllocator, &transferCommandPool); + if (result != VK_SUCCESS) { + throw "failed to create transfer command pool"; + } } void CreateCommandBuffer() { @@ -815,6 +843,13 @@ void CreateCommandBuffer() { if (result != VK_SUCCESS) { throw "failed to allocate command buffer"; } + + allocInfo.commandBufferCount = 1; + allocInfo.commandPool = transferCommandPool; + result = vkAllocateCommandBuffers(vkDevice, &allocInfo, &transferCommandBuffer); + if (result != VK_SUCCESS) { + throw "failed to allocate command buffer"; + } } void CreateSyncObjects() { @@ -836,7 +871,89 @@ void CreateSyncObjects() { } } +void CreateImGui() { + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + ImGui::StyleColorsDark(); + + VkDescriptorPoolSize poolSizes[] = { + {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 descriptorPoolCreateInfo{}; + descriptorPoolCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; + descriptorPoolCreateInfo.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT; + descriptorPoolCreateInfo.maxSets = 1000 * IM_ARRAYSIZE(poolSizes), + descriptorPoolCreateInfo.poolSizeCount = static_cast<uint32_t>(IM_ARRAYSIZE(poolSizes)); + descriptorPoolCreateInfo.pPoolSizes = poolSizes; + + vkCreateDescriptorPool(vkDevice, &descriptorPoolCreateInfo, vkAllocator, &imGuiDescriptorPool); + + ImGui_ImplGlfw_InitForVulkan(window, false); + /* + ImGui_ImplGlfw_InstallCallbacks(window); + */ + + ImGui_ImplVulkan_InitInfo initInfo{}; + initInfo.Allocator = vkAllocator; + initInfo.CheckVkResultFn = ImGuiCheckVkResult; + initInfo.ColorAttachmentFormat = VkFormat::VK_FORMAT_B8G8R8A8_SRGB; + initInfo.DescriptorPool = imGuiDescriptorPool; + initInfo.Device = vkDevice; + initInfo.Instance = vkInstance; + initInfo.ImageCount = MAX_FRAMES_IN_FLIGHT; + initInfo.MinImageCount = MAX_FRAMES_IN_FLIGHT; + initInfo.MSAASamples = VK_SAMPLE_COUNT_1_BIT; + initInfo.PhysicalDevice = vkPhysicalDevice; + initInfo.PipelineCache = {}; + initInfo.Queue = graphicsQueue; + initInfo.QueueFamily = graphicsFamilyIndex; + initInfo.Subpass = 0; + initInfo.UseDynamicRendering = false; + + ImGui_ImplVulkan_Init(&initInfo, renderPass); + + // font + { + VkCommandBufferBeginInfo begInfo{}; + begInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; + begInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; + begInfo.pInheritanceInfo = {}; + begInfo.pNext = nullptr; + vkBeginCommandBuffer(presentCommandBuffers[0], &begInfo); + + ImGui_ImplVulkan_CreateFontsTexture(presentCommandBuffers[0]); + + vkEndCommandBuffer(presentCommandBuffers[0]); + + VkSubmitInfo submitInfo{}; + submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; + submitInfo.waitSemaphoreCount = 0; + submitInfo.pWaitSemaphores = nullptr; + submitInfo.pWaitDstStageMask = nullptr; + submitInfo.commandBufferCount = 1; + submitInfo.pCommandBuffers = presentCommandBuffers; + submitInfo.signalSemaphoreCount = 0; + submitInfo.pSignalSemaphores = nullptr; + vkQueueSubmit(graphicsQueue, 1, &submitInfo, nullptr); + vkQueueWaitIdle(graphicsQueue); + } + ImGui_ImplVulkan_DestroyFontUploadObjects(); +} + void RecordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex) { + vkResetCommandBuffer(commandBuffer, 0); + VkCommandBufferBeginInfo beginInfo {}; beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; beginInfo.flags = 0; @@ -880,6 +997,27 @@ void RecordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex) { // reminder that present.vert is a triangle vkCmdDraw(commandBuffer, 3, 1, 0, 0); + // ImGui + if (isImGuiRenderActive) { + ImGui_ImplVulkan_NewFrame(); + ImGui_ImplGlfw_NewFrame(); + ImGui::NewFrame(); + + /* + ImGuiDockNodeFlags dockNodeFlags = ImGuiDockNodeFlags_PassthruCentralNode; + ImGui::DockSpaceOverViewport(nullptr, ImGuiDockNodeFlags_PassthruCentralNode); + */ + + ImGui::ShowDemoWindow(); + + ImGui::Render(); + auto drawData = ImGui::GetDrawData(); + const bool isMinimized = drawData->DisplaySize.x <= 0.0f || drawData->DisplaySize.y <= 0.0f; + if (isMinimized) return; + + ImGui_ImplVulkan_RenderDrawData(drawData, commandBuffer); + } + vkCmdEndRenderPass(commandBuffer); result = vkEndCommandBuffer(commandBuffer); @@ -945,10 +1083,14 @@ void CreateWindow(PKEWindowProperties *wp) { CreateCommandPool(); CreateCommandBuffer(); CreateSyncObjects(); + CreateImGui(); } void DestroyWindow() { if (vkInstance == nullptr) return; + ImGui_ImplVulkan_Shutdown(); + ImGui_ImplGlfw_Shutdown(); + ImGui::DestroyContext(); DestroySwapchain(); for (long i = 0; i < MAX_FRAMES_IN_FLIGHT; ++i) { vkDestroySemaphore(vkDevice, presentImageAvailableSemaphores[i], vkAllocator); @@ -956,11 +1098,13 @@ void DestroyWindow() { vkDestroyFence(vkDevice, presentInFlightFences[i], vkAllocator); } vkDestroyCommandPool(vkDevice, vkCommandPool, vkAllocator); + vkDestroyCommandPool(vkDevice, transferCommandPool, vkAllocator); vkDestroyPipeline(vkDevice, graphicsPipeline, vkAllocator); vkDestroyPipelineLayout(vkDevice, pipelineLayout, vkAllocator); /* vkDestroyDescriptorPool(vkDevice, presentDescriptorPool, vkAllocator); */ + vkDestroyDescriptorPool(vkDevice, imGuiDescriptorPool, vkAllocator); vkDestroyDescriptorSetLayout(vkDevice, vkDescriptorSetLayout, vkAllocator); vkDestroyRenderPass(vkDevice, renderPass, vkAllocator); vkDestroySurfaceKHR(vkInstance, vkSurfaceKHR, vkAllocator); diff --git a/src/window.hpp b/src/window.hpp index 3d66ed4..a3cebf4 100644 --- a/src/window.hpp +++ b/src/window.hpp @@ -10,6 +10,9 @@ #include <cassert> #include <GLFW/glfw3.h> #include <vulkan/vulkan.h> +#include "backends/imgui_impl_glfw.h" +#include "backends/imgui_impl_vulkan.h" +#include "imgui.h" const unsigned int MAX_FRAMES_IN_FLIGHT = 2; |
