diff --git a/src/Application.cpp b/src/Application.cpp index 4650081..2e0ed1e 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -24,7 +24,7 @@ void App::run() { void App::setupCallBacks() { glfwSetWindowUserPointer(window->getGLFWwindow(), this); - // glfwSetFramebufferSizeCallback(window->getGLFWwindow(), framebufferResizeCallback); + glfwSetFramebufferSizeCallback(window->getGLFWwindow(), framebufferResizeCallback); glfwSetCursorPosCallback(window->getGLFWwindow(), cursorPosCallback); glfwSetCursorEnterCallback(window->getGLFWwindow(), cursorEnterCallback); glfwSetMouseButtonCallback(window->getGLFWwindow(), mouseButtonCallback); @@ -33,10 +33,7 @@ void App::setupCallBacks() { void App::framebufferResizeCallback(GLFWwindow* window, int width, int height) { auto* const app = static_cast(glfwGetWindowUserPointer(window)); - FrameInfo frameInfo{}; - frameInfo.objects = app->objects; - frameInfo.camera = app->camera; - app->renderer->drawFrame(frameInfo); + app->updateWindowSize = true; } void App::cursorPosCallback(GLFWwindow* window, double xPosIn, double yPosIn) { @@ -101,23 +98,37 @@ void App::keyCallback(GLFWwindow* window, int key, int scancode, int action, int } } -void App::drawLoop() { - while (!window->windowShouldClose()) { - ImGui_ImplVulkan_NewFrame(); - ImGui_ImplGlfw_NewFrame(); - ImGui::NewFrame(); +int App::drawFrame() { + // std::lock_guard lockGuard(rendererMutex); + ImGui_ImplVulkan_NewFrame(); + ImGui_ImplGlfw_NewFrame(); + ImGui::NewFrame(); - if (showImguizmo && !objects.empty()) { - auto lastModel = objects.back(); // Just a testing statement - drawImGuizmo(&lastModel->transformMatrix.model); - } + if (showImguizmo && !objects.empty()) { + auto lastModel = objects.back(); // Just a testing statement + drawImGuizmo(&lastModel->transformMatrix.model); + } + + //ImGui::ShowDemoWindow(); + FrameInfo frameInfo{}; + frameInfo.objects = objects; + frameInfo.camera = camera; - //ImGui::ShowDemoWindow(); - FrameInfo frameInfo{}; - frameInfo.objects = objects; - frameInfo.camera = camera; + auto result = renderer->drawFrame(frameInfo); + ImGui::EndFrame(); + return result; +} - renderer->drawFrame(frameInfo); +void App::drawLoop() { + while (!window->windowShouldClose()) { + if (updateWindowSize) { + renderer->recreateSwapchain(); + updateWindowSize = false; + } + auto result = drawFrame(); + if (result == 1) { + renderer->recreateSwapchain(); + } } } diff --git a/src/Application.h b/src/Application.h index 5c42bf1..7f44c8a 100644 --- a/src/Application.h +++ b/src/Application.h @@ -18,6 +18,7 @@ class App { BehCamera camera{}; bool mouseCaptured = false; bool showImguizmo = true; + bool updateWindowSize = false; void mainLoop(); void processPressedKeys(double delta); @@ -33,4 +34,7 @@ class App { bool shiftPressed = false; void drawLoop(); + + int drawFrame(); + }; diff --git a/src/Renderer.cpp b/src/Renderer.cpp index 7c16c0b..6a01f9f 100644 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -1019,7 +1019,7 @@ void Renderer::updateUniformBuffer(uint32_t currentImage, FrameInfo &frameInfo) vmaUnmapMemory(device->allocator(), uniformBuffers[currentImage]._allocation); } -void Renderer::drawFrame(FrameInfo &frameInfo) { +int Renderer::drawFrame(FrameInfo &frameInfo) { while (device->device().waitForFences(inFlightFences[currentFrame], VK_TRUE, std::numeric_limits::max()) == vk::Result::eTimeout) { @@ -1040,9 +1040,7 @@ void Renderer::drawFrame(FrameInfo &frameInfo) { imageIndex = acquired.value; break; case vk::Result::eErrorOutOfDateKHR: - recreateSwapchain(); - - return; + return 1; default: throw std::runtime_error("failed to acquire swap chain image!"); } @@ -1050,8 +1048,8 @@ void Renderer::drawFrame(FrameInfo &frameInfo) { } catch (vk::OutOfDateKHRError err) { // https://github.com/KhronosGroup/Vulkan-Hpp/issues/599 - recreateSwapchain(); - return; + // recreateSwapchain(); + return 1; } catch (vk::SystemError &err) { throw std::runtime_error(std::string("failed to acquire swap chain image!") + err.what()); @@ -1117,14 +1115,12 @@ void Renderer::drawFrame(FrameInfo &frameInfo) { break; case vk::Result::eSuboptimalKHR: case vk::Result::eErrorOutOfDateKHR: // What we would like to do :) But it's actually an exception - recreateSwapchain(); - break; + return 1; default: throw std::runtime_error("failed to present swap chain image!"); // an unexpected result is returned! } } catch (vk::OutOfDateKHRError &err) { - std::cerr << "Recreating Swapchain: " << err.what() << "\n"; - recreateSwapchain(); + return 1; } awaitingSwapchainImageUsed &= ~(1UL << imageIndex); @@ -1137,6 +1133,7 @@ void Renderer::drawFrame(FrameInfo &frameInfo) { } currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT; + return 0; } void Renderer::cleanup() { diff --git a/src/Renderer.h b/src/Renderer.h index 0a68d13..8cca83c 100644 --- a/src/Renderer.h +++ b/src/Renderer.h @@ -36,10 +36,11 @@ class Renderer { Renderer& operator=(const Renderer&) = delete; Renderer(const Renderer&) = delete; - void drawFrame(FrameInfo& frameInfo); + int drawFrame(FrameInfo& frameInfo); void uploadMeshes(const std::vector>& objects); Material createMaterial(std::vector& texturePaths); - RendererMode rendererMode = RendererMode::NORMAL; + void recreateSwapchain(); + RendererMode rendererMode = RendererMode::NORMAL; private: std::shared_ptr window; @@ -107,8 +108,6 @@ class Renderer { void createSwapChain(); - void recreateSwapchain(); - vk::SurfaceFormatKHR chooseSwapSurfaceFormat(const std::vector& availableFormats); vk::PresentModeKHR chooseSwapPresentMode(const std::vector& availablePresentModes);