~ | Shader hot reloading

This commit is contained in:
TheRedShip
2025-02-14 00:10:46 +01:00
parent 4ddacdaadd
commit 583e15685a
9 changed files with 90 additions and 40 deletions

View File

@ -31,6 +31,7 @@
# include "imgui/imgui_impl_glfw.h"
# include "imgui/imgui_impl_opengl3.h"
# include <filesystem>
# include <algorithm>
# include <string.h>
# include <iostream>

View File

@ -25,9 +25,12 @@ class Shader
void compile(void);
void reload();
bool hasChanged();
void setDefine(const std::string &name, const std::string &value);
GLuint getShader(void) const;
const std::string &getFilePath(void) const;
private:
void checkCompileErrors();
@ -37,6 +40,8 @@ class Shader
GLenum _type;
GLuint _shader_id;
std::string _file_path;
std::unordered_map<std::string, std::filesystem::file_time_type> _files_timestamps;
};
#endif

View File

@ -26,11 +26,12 @@ class ShaderProgram
void link(void);
void use(void) const;
void dispathCompute(GLuint x, GLuint y, GLuint z) const;
void use(void);
void dispathCompute(GLuint x, GLuint y, GLuint z);
void bindImageTexture(GLuint texture_id, GLuint unit, GLenum access, GLenum format) const;
void watchForChanges(void);
void reloadShaders(void);
void set_int(const std::string &name, int value) const;

View File

@ -1,4 +1,4 @@
#if SHADER_DEBUG
#if 0
#include "shaders/debug.glsl"
#else
#include "shaders/raytracing.glsl"

View File

@ -14,35 +14,13 @@
void setupScreenTriangle(GLuint *VAO);
void drawScreenTriangle(GLuint VAO, GLuint output_texture, GLuint program);
std::vector<GLuint> generateTextures(unsigned int textures_count);
std::vector<Buffer *> createDataOnGPU(Scene &scene);
void updateDataOnGPU(Scene &scene, std::vector<Buffer *> buffers);
void shaderDenoise(ShaderProgram &denoising_program, GPUDenoise &denoise, std::vector<GLuint> textures)
{
denoising_program.use();
denoising_program.set_vec2("u_resolution", glm::vec2(WIDTH, HEIGHT));
denoising_program.set_float("u_c_phi", denoise.c_phi);
denoising_program.set_float("u_p_phi", denoise.p_phi);
denoising_program.set_float("u_n_phi", denoise.n_phi);
int output_texture = 0;
int denoising_texture = 2;
for (int pass = 0; pass < denoise.pass ; ++pass)
{
glBindImageTexture(0, textures[output_texture], 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F);
glBindImageTexture(2, textures[denoising_texture], 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F);
denoising_program.set_int("u_pass", pass);
denoising_program.dispathCompute((WIDTH + 15) / 16, (HEIGHT + 15) / 16, 1);
std::swap(output_texture, denoising_texture);
}
glBindImageTexture(0, textures[output_texture], 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F);
}
void shaderDenoise(ShaderProgram &denoising_program, GPUDenoise &denoise, std::vector<GLuint> textures);
int main(int argc, char **argv)
{

View File

@ -115,3 +115,30 @@ void updateDataOnGPU(Scene &scene, std::vector<Buffer *> buffers)
buffers[7]->update(&scene.getVolume(), sizeof(GPUVolume));
buffers[8]->update(&scene.getDebug(), sizeof(GPUDebug));
}
void shaderDenoise(ShaderProgram &denoising_program, GPUDenoise &denoise, std::vector<GLuint> textures)
{
denoising_program.use();
denoising_program.set_vec2("u_resolution", glm::vec2(WIDTH, HEIGHT));
denoising_program.set_float("u_c_phi", denoise.c_phi);
denoising_program.set_float("u_p_phi", denoise.p_phi);
denoising_program.set_float("u_n_phi", denoise.n_phi);
int output_texture = 0;
int denoising_texture = 2;
for (int pass = 0; pass < denoise.pass ; ++pass)
{
glBindImageTexture(0, textures[output_texture], 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F);
glBindImageTexture(2, textures[denoising_texture], 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F);
denoising_program.set_int("u_pass", pass);
denoising_program.dispathCompute((WIDTH + 15) / 16, (HEIGHT + 15) / 16, 1);
std::swap(output_texture, denoising_texture);
}
glBindImageTexture(0, textures[output_texture], 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F);
}

View File

@ -16,7 +16,7 @@
#include <stdlib.h>
#include <string.h>
std::stringstream loadFileWithIncludes(const std::string& path)
std::stringstream loadFileWithIncludes(const std::string& path, std::vector<std::string> &included_files)
{
std::ifstream file(path);
if (!file.is_open()) {
@ -36,7 +36,9 @@ std::stringstream loadFileWithIncludes(const std::string& path)
if (start != std::string::npos && end != std::string::npos && end > start)
{
std::string includePath = line.substr(start + 1, end - start - 1);
std::string includedContent = loadFileWithIncludes(includePath).str();
included_files.push_back(includePath);
std::string includedContent = loadFileWithIncludes(includePath, included_files).str();
fileContent << includedContent << "\n";
}
}
@ -44,7 +46,7 @@ std::stringstream loadFileWithIncludes(const std::string& path)
fileContent << line << "\n";
}
return fileContent;
return (fileContent);
}
@ -78,7 +80,12 @@ void Shader::compile()
{
_shader_id = glCreateShader(_type);
std::string shader_code = loadFileWithIncludes(_file_path).str();
std::vector<std::string> files;
files.push_back(_file_path);
std::string shader_code = loadFileWithIncludes(_file_path, files).str();
for (auto &file : files)
_files_timestamps[file] = std::filesystem::last_write_time(file);
for (auto &define : _defines)
shader_code = "#define SHADER_" + define.first + " " + define.second + "\n" + shader_code;
@ -93,6 +100,19 @@ void Shader::compile()
this->checkCompileErrors();
}
bool Shader::hasChanged()
{
for (auto &file : _files_timestamps)
{
if (std::filesystem::last_write_time(file.first) != file.second)
{
_files_timestamps[file.first] = std::filesystem::last_write_time(file.first);
return (true);
}
}
return (false);
}
void Shader::reload()
{
glDeleteShader(_shader_id);
@ -122,3 +142,7 @@ GLuint Shader::getShader(void) const
return (_shader_id);
}
const std::string &Shader::getFilePath(void) const
{
return (_file_path);
}

View File

@ -50,12 +50,13 @@ void ShaderProgram::link()
}
}
void ShaderProgram::use() const
void ShaderProgram::use()
{
glUseProgram(_program);
this->watchForChanges();
}
void ShaderProgram::dispathCompute(GLuint x, GLuint y, GLuint z) const
void ShaderProgram::dispathCompute(GLuint x, GLuint y, GLuint z)
{
this->use();
glDispatchCompute(x, y, z);
@ -67,6 +68,19 @@ void ShaderProgram::bindImageTexture(GLuint texture_id, GLuint unit, GLenum acce
glBindImageTexture(unit, texture_id, 0, GL_FALSE, 0, access, format);
}
void ShaderProgram::watchForChanges(void)
{
for (Shader *shader : _shaders)
{
if (shader->hasChanged())
{
std::cout << "Shader " << shader->getFilePath() << " has changed" << std::endl;
this->reloadShaders();
break;
}
}
}
void ShaderProgram::reloadShaders(void)
{
std::cout << "Reloading shaders" << std::endl;