Skip to content

Commit

Permalink
Recreate the swapchain from the application if resize is detected, sh…
Browse files Browse the repository at this point in the history
…ould be more crossplatform.
  • Loading branch information
Hjaltesorgenfrei committed Oct 12, 2022
1 parent 4ef5bf2 commit fe3d334
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 33 deletions.
49 changes: 30 additions & 19 deletions src/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -33,10 +33,7 @@ void App::setupCallBacks() {

void App::framebufferResizeCallback(GLFWwindow* window, int width, int height) {
auto* const app = static_cast<App*>(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) {
Expand Down Expand Up @@ -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<std::mutex> 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();
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class App {
BehCamera camera{};
bool mouseCaptured = false;
bool showImguizmo = true;
bool updateWindowSize = false;

void mainLoop();
void processPressedKeys(double delta);
Expand All @@ -33,4 +34,7 @@ class App {
bool shiftPressed = false;

void drawLoop();

int drawFrame();

};
17 changes: 7 additions & 10 deletions src/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint64_t>::max())
== vk::Result::eTimeout) {
Expand All @@ -1040,18 +1040,16 @@ 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!");
}


}
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());
Expand Down Expand Up @@ -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);
Expand All @@ -1137,6 +1133,7 @@ void Renderer::drawFrame(FrameInfo &frameInfo) {
}

currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
return 0;
}

void Renderer::cleanup() {
Expand Down
7 changes: 3 additions & 4 deletions src/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::shared_ptr<RenderObject>>& objects);
Material createMaterial(std::vector<std::string>& texturePaths);
RendererMode rendererMode = RendererMode::NORMAL;
void recreateSwapchain();
RendererMode rendererMode = RendererMode::NORMAL;

private:
std::shared_ptr<WindowWrapper> window;
Expand Down Expand Up @@ -107,8 +108,6 @@ class Renderer {

void createSwapChain();

void recreateSwapchain();

vk::SurfaceFormatKHR chooseSwapSurfaceFormat(const std::vector<struct vk::SurfaceFormatKHR>& availableFormats);

vk::PresentModeKHR chooseSwapPresentMode(const std::vector<enum vk::PresentModeKHR>& availablePresentModes);
Expand Down

0 comments on commit fe3d334

Please sign in to comment.