Skip to content

Commit

Permalink
Draw on a different thread from the one reading input
Browse files Browse the repository at this point in the history
  • Loading branch information
Hjaltesorgenfrei committed Oct 12, 2022
1 parent fd6e5ee commit 4ef5bf2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
44 changes: 31 additions & 13 deletions src/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <functional>
#include <cstdlib>
#include <chrono>
#include <thread>

#include "Renderer.h"
#include "Application.h"
Expand All @@ -23,12 +24,21 @@ void App::run() {

void App::setupCallBacks() {
glfwSetWindowUserPointer(window->getGLFWwindow(), this);
// glfwSetFramebufferSizeCallback(window->getGLFWwindow(), framebufferResizeCallback);
glfwSetCursorPosCallback(window->getGLFWwindow(), cursorPosCallback);
glfwSetCursorEnterCallback(window->getGLFWwindow(), cursorEnterCallback);
glfwSetMouseButtonCallback(window->getGLFWwindow(), mouseButtonCallback);
glfwSetKeyCallback(window->getGLFWwindow(), keyCallback);
}

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);
}

void App::cursorPosCallback(GLFWwindow* window, double xPosIn, double yPosIn) {
auto* const app = static_cast<App*>(glfwGetWindowUserPointer(window));
if (app->mouseCaptured) {
Expand Down Expand Up @@ -91,18 +101,8 @@ void App::keyCallback(GLFWwindow* window, int key, int scancode, int action, int
}
}

void App::mainLoop() {
auto timeStart = std::chrono::high_resolution_clock::now();
std::vector<std::shared_ptr<RenderObject>> objects;
objects.push_back(std::make_shared<RenderObject>(Mesh::LoadFromObj("resources/lost_empire.obj"), Material{}));
objects.push_back(std::make_shared<RenderObject>(Mesh::LoadFromObj("resources/rat.obj"), Material{}));
renderer->uploadMeshes(objects);
while (!window->windowShouldClose()) {
auto now = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> delta = now - timeStart;
timeStart = now;
glfwPollEvents();
processPressedKeys(delta.count());
void App::drawLoop() {
while (!window->windowShouldClose()) {
ImGui_ImplVulkan_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
Expand All @@ -117,8 +117,26 @@ void App::mainLoop() {
frameInfo.objects = objects;
frameInfo.camera = camera;

renderer->drawFrame(frameInfo);
renderer->drawFrame(frameInfo);
}
}

void App::mainLoop() {
auto timeStart = std::chrono::high_resolution_clock::now();
// objects.push_back(std::make_shared<RenderObject>(Mesh::LoadFromObj("resources/lost_empire.obj"), Material{}));
objects.push_back(std::make_shared<RenderObject>(Mesh::LoadFromObj("resources/rat.obj"), Material{}));
renderer->uploadMeshes(objects);

std::thread drawThread([&](){drawLoop();});

while (!window->windowShouldClose()) {
auto now = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> delta = now - timeStart;
timeStart = now;
glfwPollEvents();
processPressedKeys(delta.count());
}
drawThread.join();
}

void App::drawImGuizmo(glm::mat4* matrix) {
Expand Down
3 changes: 3 additions & 0 deletions src/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class App {
std::shared_ptr<WindowWrapper> window = std::make_shared<WindowWrapper>(WIDTH, HEIGHT, "Vulkan Tutorial");
std::unique_ptr<Renderer> renderer;
std::shared_ptr<BehDevice> device;
std::vector<std::shared_ptr<RenderObject>> objects;
BehCamera camera{};
bool mouseCaptured = false;
bool showImguizmo = true;
Expand All @@ -30,4 +31,6 @@ class App {
static void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);

bool shiftPressed = false;

void drawLoop();
};
13 changes: 4 additions & 9 deletions src/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,8 @@ void Renderer::createSwapChain() {
}

void Renderer::recreateSwapchain() {
int width = 0, height = 0;
auto glfwWindow = window->getGLFWwindow();
glfwGetFramebufferSize(glfwWindow, &width, &height);
while (width == 0 || height == 0) {
glfwGetFramebufferSize(glfwWindow, &width, &height);
glfwWaitEvents();
}
auto timeStart = std::chrono::high_resolution_clock::now();

device->device().waitIdle(); // Has to wait for rendering to finish before creating new swapchain. Better solutions exists.
oldSwapChain = swapChain;
createSwapChain();
device->device().destroySwapchainKHR(oldSwapChain);
Expand All @@ -181,7 +174,9 @@ void Renderer::recreateSwapchain() {
for (int i = 0; i < swapChainImages.size(); i++) {
awaitingSwapchainImageUsed |= 1UL << i;
}
std::cout << "Recreated swapchain" << std::endl;
auto now = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> delta = now - timeStart;
std::cout << "Recreated swapchain in " << delta.count() << "ms" << std::endl;
}

vk::SurfaceFormatKHR
Expand Down

0 comments on commit 4ef5bf2

Please sign in to comment.