Multithread rendering

This commit is contained in:
2025-01-04 14:37:26 -07:00
parent bd3bb970f8
commit bf8e7d2259
3 changed files with 67 additions and 41 deletions

View File

@ -1,4 +1,5 @@
#include "Window.h" #include "Window.h"
#include <iostream>
Window::Window(int width, int height, const char* title) { Window::Window(int width, int height, const char* title) {
glfw_window = glfwCreateWindow(width, height, title, NULL, NULL); glfw_window = glfwCreateWindow(width, height, title, NULL, NULL);
@ -7,7 +8,8 @@ Window::Window(int width, int height, const char* title) {
setSize(width, height); setSize(width, height);
setTitle(title); setTitle(title);
glfwSetFramebufferSizeCallback(glfw_window, onResize); glfwSetFramebufferSizeCallback(glfw_window, onResize);
glfwSetWindowUserPointer(glfw_window, this);
resized = false;
} }
Window::~Window() { Window::~Window() {
@ -36,6 +38,12 @@ void Window::setSize(int width, int height) {
glfwSetWindowSize(glfw_window, width, height); glfwSetWindowSize(glfw_window, width, height);
} }
void Window::resizeViewport(int width, int height) {
this->width = width;
this->height = height;
resized = true;
}
void Window::setTitle(const char* title) { void Window::setTitle(const char* title) {
this->title = title; this->title = title;
glfwSetWindowTitle(glfw_window, title); glfwSetWindowTitle(glfw_window, title);
@ -116,6 +124,11 @@ bool Window::isResizable() {
void Window::beginRender() { void Window::beginRender() {
glfwMakeContextCurrent(glfw_window); glfwMakeContextCurrent(glfw_window);
if (resized) {
std::cout << "Resizing viewport\n";
glViewport(0, 0, width, height);
resized = false;
}
} }
void Window::endRender() { void Window::endRender() {
@ -127,6 +140,5 @@ bool Window::isClosing() {
} }
void Window::onResize(GLFWwindow* window, int width, int height) { void Window::onResize(GLFWwindow* window, int width, int height) {
glfwMakeContextCurrent(window); ((Window*)glfwGetWindowUserPointer(window))->resizeViewport(width, height);
glViewport(0, 0, width, height);
} }

View File

@ -10,6 +10,7 @@ private:
bool is_fullscreen; bool is_fullscreen;
bool is_resizable; bool is_resizable;
int windowed_mode_x, windowed_mode_y; int windowed_mode_x, windowed_mode_y;
bool resized;
public: public:
Window(int width, int height, const char* title); Window(int width, int height, const char* title);
@ -19,6 +20,7 @@ public:
void setWidth(int width); void setWidth(int width);
void setHeight(int height); void setHeight(int height);
void setSize(int width, int height); void setSize(int width, int height);
void resizeViewport(int width, int height);
const char* getTitle(); const char* getTitle();
void setTitle(const char* title); void setTitle(const char* title);
std::vector<GLFWvidmode> getVideoModes(); std::vector<GLFWvidmode> getVideoModes();

View File

@ -14,9 +14,11 @@
#include "Square.h" #include "Square.h"
#include "deff.h" #include "deff.h"
#include <array> #include <array>
#include <thread>
int OpenGLVersion; int OpenGLVersion;
spdlog::logger logger("none"); spdlog::logger logger("none");
std::mutex ready;
float square[] = { float square[] = {
1.0f, 1.0f, 0.0f, // top right 1.0f, 1.0f, 0.0f, // top right
@ -65,6 +67,19 @@ const char* fragmentShaderSource = "#version 330 core\n"
#define ERR(...) logger.error(__VA_ARGS__) #define ERR(...) logger.error(__VA_ARGS__)
#define CRITICAL(...) logger.critical(__VA_ARGS__) #define CRITICAL(...) logger.critical(__VA_ARGS__)
bool running = true;
Window* window;
unsigned int shaderProgram;
void openGLContextInit() {
OpenGLVersion = gladLoadGL();
if (OpenGLVersion == 0) {
ERR("Unable to load GLAD");
throw ("Failed to load GLAD");
}
INFO("GLAD Initialized");
}
void init() { void init() {
auto console = std::make_shared<spdlog::sinks::stdout_color_sink_mt>(); auto console = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
auto file = std::make_shared<spdlog::sinks::basic_file_sink_mt>("log.txt", true); auto file = std::make_shared<spdlog::sinks::basic_file_sink_mt>("log.txt", true);
@ -76,29 +91,14 @@ void init() {
throw ("Unable to init GLFW"); throw ("Unable to init GLFW");
} }
INFO("GLFW Initialized!"); INFO("GLFW Initialized!");
window = new Window(640, 480, "Test Window");
} }
void openGLContextInit() { void renderInit() {
OpenGLVersion = gladLoadGL();
if (OpenGLVersion == 0) {
ERR("Unable to load GLAD");
throw ("Failed to load GLAD");
}
INFO("GLAD Initialized");
}
void deinit() {
glfwTerminate();
}
int main() {
init();
Window* window = new Window(640, 480, "Test Window");
//window->setWindowedFullscreen(); //window->setWindowedFullscreen();
window->beginRender(); window->beginRender();
glfwSwapInterval(0); //glfwSwapInterval(1);
window->setWindowedFullscreen();
openGLContextInit(); openGLContextInit();
unsigned int vertexShader; unsigned int vertexShader;
@ -112,7 +112,7 @@ int main() {
if (!success) { if (!success) {
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog); glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
ERR("Vertex Shader Compile Failed: {}", infoLog); ERR("Vertex Shader Compile Failed: {}", infoLog);
return -1; return;
} }
unsigned int fragmentShader; unsigned int fragmentShader;
@ -124,10 +124,9 @@ int main() {
if (!success) { if (!success) {
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog); glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
ERR("Fragment Shader Compile Failed: {}", infoLog); ERR("Fragment Shader Compile Failed: {}", infoLog);
return -1; return;
} }
unsigned int shaderProgram;
shaderProgram = glCreateProgram(); shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader); glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader); glAttachShader(shaderProgram, fragmentShader);
@ -137,7 +136,7 @@ int main() {
if (!success) { if (!success) {
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog); glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
ERR("Program Link Failed: {}", infoLog); ERR("Program Link Failed: {}", infoLog);
return -1; return;
} }
glUseProgram(shaderProgram); glUseProgram(shaderProgram);
@ -147,26 +146,39 @@ int main() {
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glUniformMatrix4fv(UNIFORM_PROJ_MAT, 1, GL_FALSE, glm::value_ptr(glm::ortho(0.0f, 800.0f, 600.0f, 0.0f))); glUniformMatrix4fv(UNIFORM_PROJ_MAT, 1, GL_FALSE, glm::value_ptr(glm::ortho(0.0f, 800.0f, 600.0f, 0.0f)));
std::array<Square*, 100*100> squares; }
for (int i = 0; i < 100 * 100; i++) {
int x = i % 100;
int y = i / 100;
squares[i] = new Square(glm::vec3(x*6, y*6, 0.0f), glm::vec3(6.0f, 6.0f, 1.0f), glm::vec4(0.0f, 1.0f, 0.0f, 1.0f));
}
//Square square({ 0, 0, 0 }, { 6, 6, 0 }, {0.0f, 1.0f, 0.0f, 1.0f});
while (!window->isClosing()) {
//square.Draw();
for (auto square : squares) {
square->Draw();
}
void deinit() {
glDeleteProgram(shaderProgram);
glfwTerminate();
}
void render() {
INFO("Render thread starting");
renderInit();
while (running) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
window->beginRender();
window->endRender(); window->endRender();
}
}
void update() {
INFO("Update Thread Starting");
while (!window->isClosing()) {
glfwPollEvents(); glfwPollEvents();
} }
running = false;
}
for (auto square : squares) { int main() {
delete square;
} init();
std::thread renderThread(render);
Sleep(10);
update();
renderThread.join();
deinit(); deinit();