diff --git a/imgui.ini b/imgui.ini index d9f9c23..22bd0f8 100644 --- a/imgui.ini +++ b/imgui.ini @@ -3,6 +3,6 @@ Pos=60,60 Size=400,400 [Window][Agnosia Debug] -Pos=410,116 -Size=494,375 +Pos=124,277 +Size=583,225 diff --git a/src/agnosiaimgui.cpp b/src/agnosiaimgui.cpp index 482fcc3..8194690 100644 --- a/src/agnosiaimgui.cpp +++ b/src/agnosiaimgui.cpp @@ -1,19 +1,113 @@ #include "agnosiaimgui.h" -#include "imgui.h" +#include "graphics/buffers.h" namespace agnosia_imgui { -struct { -} ImGuiSettings; +VkDescriptorPool imGuiDescriptorPool; -void Gui::drawTabs() { +void initWindow() { + + ImGui::DragFloat3("Object Position", buffers_libs::Buffers::getObjPos()); + ImGui::DragFloat3("Camera Position", buffers_libs::Buffers::getCamPos()); + ImGui::DragFloat3("Center Position", buffers_libs::Buffers::getCenterPos()); + ImGui::DragFloat3("Up Direction", buffers_libs::Buffers::getUpDir()); + ImGui::DragFloat("Depth of Field", buffers_libs::Buffers::getDepthField(), + 0.1f, 1.0f, 180.0f, NULL, ImGuiSliderFlags_AlwaysClamp); + ImGui::DragFloat2("Near and Far fields", + buffers_libs::Buffers::getDistanceField()); +} + +void drawTabs() { if (ImGui::BeginTabBar("MainTabBar", ImGuiTabBarFlags_Reorderable)) { - if (ImGui::BeginTabItem("Graphics Pipeline")) { - ImGui::Text("Test"); + if (ImGui::BeginTabItem("Transforms Control")) { + initWindow(); ImGui::EndTabItem(); } ImGui::EndTabBar(); } } + +void Gui::drawImGui() { + + ImGui_ImplVulkan_NewFrame(); + ImGui_ImplGlfw_NewFrame(); + ImGui::NewFrame(); + // 2. Show a simple window that we create ourselves. We use a Begin/End pair + // to create a named window. + + ImGui::Begin("Agnosia Debug"); + + ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", + 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); + + drawTabs(); + + ImGui::End(); + + ImGui::Render(); +} + +void Gui::initImgui(VkInstance instance) { + auto load_vk_func = [&](const char *fn) { + if (auto proc = vkGetDeviceProcAddr(Global::device, fn)) + return proc; + return vkGetInstanceProcAddr(instance, fn); + }; + ImGui_ImplVulkan_LoadFunctions( + [](const char *fn, void *data) { + return (*(decltype(load_vk_func) *)data)(fn); + }, + &load_vk_func); + + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + // TODO + ImGuiIO &io = ImGui::GetIO(); + (void)io; + io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; + + ImGui::StyleColorsDark(); + + ImGui_ImplGlfw_InitForVulkan(Global::window, true); + + VkDescriptorPoolSize ImGuiPoolSizes[]{ + {VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1}, + }; + VkDescriptorPoolCreateInfo ImGuiPoolInfo{ + .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, + .flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT, + .maxSets = 1, + .poolSizeCount = 1, + .pPoolSizes = ImGuiPoolSizes, + }; + if (vkCreateDescriptorPool(Global::device, &ImGuiPoolInfo, nullptr, + &imGuiDescriptorPool) != VK_SUCCESS) { + throw std::runtime_error("Failed to create ImGui descriptor pool!"); + } + + VkPipelineRenderingCreateInfo pipelineRenderingCreateInfo{ + .sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO, + .colorAttachmentCount = 1, + .pColorAttachmentFormats = device_libs::DeviceControl::getImageFormat(), + .depthAttachmentFormat = texture_libs::Texture::findDepthFormat(), + }; + + ImGui_ImplVulkan_InitInfo initInfo{ + .Instance = instance, + .PhysicalDevice = Global::physicalDevice, + .Device = Global::device, + .QueueFamily = Global::findQueueFamilies(Global::physicalDevice) + .graphicsFamily.value(), + .Queue = Global::graphicsQueue, + .DescriptorPool = imGuiDescriptorPool, + .MinImageCount = Global::MAX_FRAMES_IN_FLIGHT, + .ImageCount = Global::MAX_FRAMES_IN_FLIGHT, + .MSAASamples = Global::perPixelSampleCount, + .UseDynamicRendering = true, + .PipelineRenderingCreateInfo = pipelineRenderingCreateInfo, + }; + + ImGui_ImplVulkan_Init(&initInfo); +} } // namespace agnosia_imgui diff --git a/src/agnosiaimgui.h b/src/agnosiaimgui.h index 4b4624c..2ad392d 100644 --- a/src/agnosiaimgui.h +++ b/src/agnosiaimgui.h @@ -1,11 +1,15 @@ #pragma once #include "global.h" +#include "graphics/texture.h" #include "imgui.h" +#include "imgui_impl_glfw.h" +#include "imgui_impl_vulkan.h" #include namespace agnosia_imgui { class Gui { public: - static void drawTabs(); + static void drawImGui(); + static void initImgui(VkInstance instance); }; } // namespace agnosia_imgui diff --git a/src/entrypoint.cpp b/src/entrypoint.cpp index 7a95159..e4c53d2 100644 --- a/src/entrypoint.cpp +++ b/src/entrypoint.cpp @@ -1,3 +1,4 @@ +#include "agnosiaimgui.h" #include "entrypoint.h" #include "global.h" #include "graphics/texture.h" @@ -105,14 +106,14 @@ void initVulkan() { buffers_libs::Buffers::createDescriptorSets(); graphics_pipeline::Graphics::createCommandBuffer(); render_present::Render::createSyncObject(); - render_present::Render::init_imgui(vulkaninstance); + agnosia_imgui::Gui::initImgui(vulkaninstance); } void mainLoop() { while (!glfwWindowShouldClose(Global::window)) { glfwPollEvents(); - render_present::Render::drawImGui(); + agnosia_imgui::Gui::drawImGui(); render_present::Render::drawFrame(); } vkDeviceWaitIdle(Global::device); diff --git a/src/graphics/buffers.cpp b/src/graphics/buffers.cpp index 922d217..431b198 100644 --- a/src/graphics/buffers.cpp +++ b/src/graphics/buffers.cpp @@ -1,4 +1,5 @@ #include "buffers.h" +#include VkBuffer vertexBuffer; VkDeviceMemory vertexBufferMemory; @@ -10,6 +11,13 @@ std::vector uniformBuffers; std::vector uniformBuffersMemory; std::vector uniformBuffersMapped; +float objPos[4] = {0.0f, 0.0f, 0.0f, 0.44f}; +float camPos[4] = {2.0f, 2.0f, 2.0f, 0.44f}; +float centerPos[4] = {0.0f, 0.0f, 0.0f, 0.44f}; +float upDir[4] = {0.0f, 0.0f, 1.0f, 0.44f}; +float depthField = 45.0f; +float distanceField[2] = {0.1f, 100.0f}; + namespace buffers_libs { uint32_t Buffers::findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties) { @@ -238,20 +246,22 @@ void Buffers::updateUniformBuffer(uint32_t currentImage) { ubo.time = time; // Modify the model projection transformation to rotate around the Z over // time. - ubo.model = glm::rotate(glm::mat4(1.0f), time * glm::radians(30.0f), - glm::vec3(0.0f, 0.0f, 1.0f)); + ubo.model = glm::translate(glm::mat4(1.0f), + glm::vec3(objPos[0], objPos[1], objPos[2])); + // ubo.model = glm::rotate(glm::mat4(1.0f), time * glm::radians(30.0f), + // glm::vec3(0.0f, 0.0f, 1.0f)); // Modify the view transformation to look at the object from above at a 45 // degree angle. This takes the eye position, center position, and the up // direction. - ubo.view = - glm::lookAt(glm::vec3(2.0f, 2.0f * sin(time), 2.0f), - glm::vec3(0.0f, 0.0f, 0.5f), glm::vec3(0.0f, 0.0f, 1.0f)); + ubo.view = glm::lookAt(glm::vec3(camPos[0], camPos[1], camPos[2]), + glm::vec3(centerPos[0], centerPos[1], centerPos[2]), + glm::vec3(upDir[0], upDir[1], upDir[2])); // 45 degree field of view, set aspect ratio, and near and far clipping range. ubo.proj = glm::perspective( - glm::radians(45.0f), + glm::radians(depthField), device_libs::DeviceControl::getSwapChainExtent().width / (float)device_libs::DeviceControl::getSwapChainExtent().height, - 0.1f, 100.0f); + distanceField[0], distanceField[1]); // GLM was created for OpenGL, where the Y coordinate was inverted. This // simply flips the sign. @@ -341,4 +351,10 @@ void Buffers::destroyDescriptorPool() { vkDestroyDescriptorPool(Global::device, descriptorPool, nullptr); } VkDescriptorPool Buffers::getDescriptorPool() { return descriptorPool; } +float *Buffers::getObjPos() { return objPos; } +float *Buffers::getCamPos() { return camPos; } +float *Buffers::getCenterPos() { return centerPos; } +float *Buffers::getUpDir() { return upDir; } +float *Buffers::getDepthField() { return &depthField; } +float *Buffers::getDistanceField() { return distanceField; } } // namespace buffers_libs diff --git a/src/graphics/buffers.h b/src/graphics/buffers.h index 9df3eb7..3c9d59b 100644 --- a/src/graphics/buffers.h +++ b/src/graphics/buffers.h @@ -24,5 +24,12 @@ public: static void createDescriptorSets(); static void destroyDescriptorPool(); static VkDescriptorPool getDescriptorPool(); + + static float *getObjPos(); + static float *getCamPos(); + static float *getCenterPos(); + static float *getUpDir(); + static float *getDepthField(); + static float *getDistanceField(); }; } // namespace buffers_libs diff --git a/src/graphics/render.cpp b/src/graphics/render.cpp index db2e98b..e71cdb4 100644 --- a/src/graphics/render.cpp +++ b/src/graphics/render.cpp @@ -6,9 +6,7 @@ #include "texture.h" #include -#include "../agnosiaimgui.h" #include "imgui.h" -#include "imgui_impl_glfw.h" #include "imgui_impl_vulkan.h" namespace render_present { @@ -16,9 +14,6 @@ namespace render_present { std::vector imageAvailableSemaphores; std::vector renderFinishedSemaphores; std::vector inFlightFences; -VkDescriptorPool imGuiDescriptorPool; - -static float floatBar = 0.0f; void recreateSwapChain() { int width = 0, height = 0; @@ -118,32 +113,7 @@ void Render::drawFrame() { Global::currentFrame = (Global::currentFrame + 1) % Global::MAX_FRAMES_IN_FLIGHT; } -void Render::drawImGui() { - ImGui_ImplVulkan_NewFrame(); - ImGui_ImplGlfw_NewFrame(); - ImGui::NewFrame(); - // 2. Show a simple window that we create ourselves. We use a Begin/End pair - // to create a named window. - - static int counter = 0; - - ImGui::Begin("Agnosia Debug"); // Create a window called "Hello, world!" and - // append into it. - - ImGui::Text("This is some useful text."); // Display some text (you can use - // a format strings too) - ImGui::SliderFloat("float", &floatBar, 0.0f, - 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f - - agnosia_imgui::Gui::drawTabs(); - - ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", - 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); - ImGui::End(); - - ImGui::Render(); -} #pragma info // SEMAPHORES // Synchronization of execution on the GPU in Vulkan is *explicit* The Order of @@ -212,66 +182,4 @@ void Render::cleanupSwapChain() { vkDestroySwapchainKHR(Global::device, Global::swapChain, nullptr); } -void Render::init_imgui(VkInstance instance) { - auto load_vk_func = [&](const char *fn) { - if (auto proc = vkGetDeviceProcAddr(Global::device, fn)) - return proc; - return vkGetInstanceProcAddr(instance, fn); - }; - ImGui_ImplVulkan_LoadFunctions( - [](const char *fn, void *data) { - return (*(decltype(load_vk_func) *)data)(fn); - }, - &load_vk_func); - - IMGUI_CHECKVERSION(); - ImGui::CreateContext(); - // TODO - ImGuiIO &io = ImGui::GetIO(); - (void)io; - io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; - - ImGui::StyleColorsDark(); - - ImGui_ImplGlfw_InitForVulkan(Global::window, true); - - VkDescriptorPoolSize ImGuiPoolSizes[]{ - {VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1}, - }; - VkDescriptorPoolCreateInfo ImGuiPoolInfo{ - .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, - .flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT, - .maxSets = 1, - .poolSizeCount = 1, - .pPoolSizes = ImGuiPoolSizes, - }; - if (vkCreateDescriptorPool(Global::device, &ImGuiPoolInfo, nullptr, - &imGuiDescriptorPool) != VK_SUCCESS) { - throw std::runtime_error("Failed to create ImGui descriptor pool!"); - } - - VkPipelineRenderingCreateInfo pipelineRenderingCreateInfo{ - .sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO, - .colorAttachmentCount = 1, - .pColorAttachmentFormats = device_libs::DeviceControl::getImageFormat(), - .depthAttachmentFormat = texture_libs::Texture::findDepthFormat(), - }; - - ImGui_ImplVulkan_InitInfo initInfo{ - .Instance = instance, - .PhysicalDevice = Global::physicalDevice, - .Device = Global::device, - .QueueFamily = Global::findQueueFamilies(Global::physicalDevice) - .graphicsFamily.value(), - .Queue = Global::graphicsQueue, - .DescriptorPool = imGuiDescriptorPool, - .MinImageCount = Global::MAX_FRAMES_IN_FLIGHT, - .ImageCount = Global::MAX_FRAMES_IN_FLIGHT, - .MSAASamples = Global::perPixelSampleCount, - .UseDynamicRendering = true, - .PipelineRenderingCreateInfo = pipelineRenderingCreateInfo, - }; - - ImGui_ImplVulkan_Init(&initInfo); -} } // namespace render_present diff --git a/src/graphics/render.h b/src/graphics/render.h index 88e3f8d..9a13585 100644 --- a/src/graphics/render.h +++ b/src/graphics/render.h @@ -8,8 +8,6 @@ public: static void createSyncObject(); static void destroyFenceSemaphores(); static void cleanupSwapChain(); - static void init_imgui(VkInstance instance); - static void drawImGui(); static float getFloatBar(); }; } // namespace render_present