feat (wip): real-time gui with rendering in opengl

This commit is contained in:
2025-01-03 19:57:10 +01:00
parent c92d0bc2a9
commit c23a33ebef
22 changed files with 656 additions and 62 deletions

125
src/gui/MainWindow.cpp Normal file
View File

@@ -0,0 +1,125 @@
#include "MainWindow.h"
#include <iostream>
#include <memory>
Window::Window(unsigned int w, unsigned int h) {
Window::w = w;
Window::h = h;
}
void framebuffer_size_callback(GLFWwindow* window, int w, int h) {
// This function is called by glfw when the window is reized.
glViewport(0 , 0, w, h);
Window* newWin = reinterpret_cast<Window*>(glfwGetWindowUserPointer(window));
newWin->resize(w, h);
}
int Window::init() {
// init glfw
glfwInit();
// requesting context version 1.0 makes glfw try to provide the latest version if possible
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 1);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
this->window = glfwCreateWindow(this->w, this->h, "CUDA ray tracing", NULL, NULL);
//hide cursor
glfwSetInputMode(this->window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetWindowUserPointer(this->window, reinterpret_cast<void*>(this));
if (this->window == NULL) {
std::cout << "Failed to create window\n";
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(this->window);
// init glad(opengl)
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
std::cout << "Failed to initialize GLAD\n";
return -1;
}
// init framebuffer
glViewport(0, 0, this->w, this->h);
if (glfwSetFramebufferSizeCallback(this->window, framebuffer_size_callback) != 0) return -1;
if (init_quad()) return -1;
this->last_frame = std::chrono::steady_clock::now();
while (!glfwWindowShouldClose(window)) {
Window::tick();
}
Window::free();
return 0;
}
int Window::init_quad() {
this->current_quad = std::make_unique<Quad>(this->w, this->h);
this->current_quad->cuda_init();
// TODO: default shaders
return 0;
}
void Window::free() {
// To preserve the proper destruction order we forcefully set the quads to null (calling their destructor in the process)
// Not strictly necessary, but i saw some weird errors on exit without this so best to keep it in.
this->current_quad = nullptr;
glfwDestroyWindow(window);
glfwTerminate();
}
void Window::tick() {
// manually track time diff
std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
float diff = (float) std::chrono::duration_cast<std::chrono::milliseconds>(now - this->last_frame).count();
this->last_frame = now;
// TODO: remove debug line at some point
std::cout << 1000.0/diff << " fps\n";
// TODO: code input logic and class/struct and stuff
// ticking input probably involves 4? steps:
// * check if window needs to be closed (escape/q pressed)
// * check if camera moved (wasd/hjkl pressed)
// (phase 3/do later): check if we switched from realtime tracing to that other option - maybe a pause function? (p pressed?)
// * if moved -> update camera (raytracing will involve some logic here too? see when i get there)
// tick render
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glDisable(GL_DEPTH_TEST);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// render frame
glBindFramebuffer(GL_FRAMEBUFFER, this->current_quad->fb);
this->current_quad->render();
glBindVertexArray(this->current_quad->VAO);
glBindTexture(GL_TEXTURE_2D, this->current_quad->tex);
glDrawArrays(GL_TRIANGLES, 0, 6); // draw current frame to texture
// check for events
// + swap buffers; TODO: check if necessary?
glfwSwapBuffers(this->window);
glfwPollEvents();
}
void Window::resize(unsigned int w, unsigned int h) {
this->w = w;
this->h = h;
this->current_quad->resize(w, h);
}

32
src/gui/MainWindow.h Normal file
View File

@@ -0,0 +1,32 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "Quad.h"
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <chrono>
class Window {
public:
unsigned int w;
unsigned int h;
Window(unsigned int w, unsigned int h);
int init();
void free();
void resize(unsigned int w, unsigned int h);
private:
GLFWwindow* window;
std::unique_ptr<Quad> current_quad;
std::chrono::steady_clock::time_point last_frame;
void tick();
int init_quad();
// std::unique_ptr<Shader> shader;
};
#endif // MAINWINDOW_H

136
src/gui/Quad.cpp Normal file
View File

@@ -0,0 +1,136 @@
#include "Quad.h"
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <cuda_gl_interop.h>
#include <iostream>
Quad::Quad(unsigned int w, unsigned int h) {
this->w = w;
this->h = h;
std::vector<float> vertices = {
-1.0f, 1.0f, 0.0f, 1.0f,
-1.0f, -1.0f, 0.0f, 0.0f,
1.0f, -1.0f, 1.0f, 0.0f,
-1.0f, 1.0f, 0.0f, 1.0f,
1.0f, -1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 1.0f, 1.0f
};
glGenBuffers(1, &VBO);
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
// copy vertex data to buffer on gpu
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), vertices.data(), GL_STATIC_DRAW);
// set our vertex attributes pointers
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2 * sizeof(float)));
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
// texture stuff
glGenBuffers(1, &PBO);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, PBO);
glBufferData(GL_PIXEL_UNPACK_BUFFER, w * h * 4, NULL, GL_DYNAMIC_COPY);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
// parameters for texture
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
glBindTexture(GL_TEXTURE_2D, 0);
// register the FBO
glGenFramebuffers(1, &fb);
glBindFramebuffer(GL_FRAMEBUFFER, fb);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, this->tex, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
};
Quad::~Quad() {
int res = cudaGraphicsUnregisterResource(CGR);
if (res) {
std::cout << "CUDA error while deregistering the graphics resource: " << res;
cudaDeviceReset();
exit(1);
}
};
void Quad::cuda_init() {
int res = cudaGraphicsGLRegisterBuffer(&this->CGR, this->PBO, cudaGraphicsRegisterFlagsNone);
if (res) {
std::cout << "CUDA error while registering the graphics resource: " << res;
cudaDeviceReset();
exit(1);
}
this->renderer = std::make_unique<Raycaster>(this->CGR, this->w, this->h);
};
void Quad::render() {
glBindTexture(GL_TEXTURE_2D, 0);
this->renderer->render();
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->PBO);
glBindTexture(GL_TEXTURE_2D, this->tex);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, this->w, this->h, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
};
void Quad::resize(unsigned int w, unsigned int h) {
this->w = w;
this->h = h;
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->PBO);
glBufferData(GL_PIXEL_UNPACK_BUFFER, w * h * 4, NULL, GL_DYNAMIC_COPY);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
glBindTexture(GL_TEXTURE_2D, this->tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
glBindTexture(GL_TEXTURE_2D, 0);
glBindFramebuffer(GL_FRAMEBUFFER, this->fb);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, this->tex, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
if (this->renderer != nullptr) {
// TODO: probably make a function for the cuda error checking
int res = cudaGraphicsUnregisterResource(CGR);
if (res) {
std::cout << "CUDA error while deregistering the graphics resource: " << res;
cudaDeviceReset();
exit(1);
}
res = cudaGraphicsGLRegisterBuffer(&this->CGR, this->PBO, cudaGraphicsRegisterFlagsNone);
if (res) {
std::cout << "CUDA error while registering the graphics resource: " << res;
cudaDeviceReset();
exit(1);
}
this->renderer->resources = this->CGR;
this->renderer->resize(w, h);
}
};

36
src/gui/Quad.h Normal file
View File

@@ -0,0 +1,36 @@
#ifndef QUAD_H
#define QUAD_H
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <cuda_runtime.h>
#include "illumination/Raycaster.h"
#include <vector>
#include <memory>
class Quad {
public:
unsigned int VAO;
unsigned int VBO;
unsigned int PBO;
cudaGraphicsResource_t CGR;
unsigned int tex;
unsigned int fb;
unsigned int w;
unsigned int h;
std::unique_ptr<Raycaster> renderer;
Quad(unsigned int w, unsigned int h);
~Quad();
void render();
void resize(unsigned int w, unsigned int h);
void cuda_init();
};
#endif // QUAD_H

View File

@@ -0,0 +1,19 @@
#version 330 core
out vec4 FragColor;
in vec2 TexCoords;
uniform sampler2D currentFrameTex;
uniform sampler2D lastFrameTex;
uniform int frameCount;
#define MAX_FRAMES 500.0f
void main()
{
vec3 col = texture(currentFrameTex, TexCoords).rgb;
vec3 col2 = texture(lastFrameTex, TexCoords).rgb;
col = mix(col, col2, min(frameCount/MAX_FRAMES,1.0f));
FragColor = vec4(col, 1.0);
}

View File

@@ -0,0 +1,11 @@
#version 330 core
layout(location = 0) in vec2 aPos;
layout(location = 1) in vec2 aTexCoords;
out vec2 TexCoords;
void main()
{
TexCoords = aTexCoords;
gl_Position = vec4(aPos.x, aPos.y, 0.0, 1.0);
}

View File

@@ -0,0 +1,12 @@
#version 330 core
out vec4 FragColor;
in vec2 TexCoords;
uniform sampler2D screenTexture;
void main()
{
vec3 col = texture(screenTexture, TexCoords).rgb;
FragColor = vec4(col, 1.0);
}

View File

@@ -0,0 +1,11 @@
#version 330 core
layout(location = 0) in vec2 aPos;
layout(location = 1) in vec2 aTexCoords;
out vec2 TexCoords;
void main()
{
TexCoords = aTexCoords;
gl_Position = vec4(aPos.x, aPos.y, 0.0, 1.0);
}