diff --git a/includes/RT.hpp b/includes/RT.hpp index 2ae3c2a..87f7736 100644 --- a/includes/RT.hpp +++ b/includes/RT.hpp @@ -31,6 +31,7 @@ # include "imgui/imgui_impl_glfw.h" # include "imgui/imgui_impl_opengl3.h" +# include # include # include # include diff --git a/includes/RT/Shader.hpp b/includes/RT/Shader.hpp index 07381a3..717538e 100644 --- a/includes/RT/Shader.hpp +++ b/includes/RT/Shader.hpp @@ -24,19 +24,24 @@ class Shader void compile(void); void reload(); + + bool hasChanged(); void setDefine(const std::string &name, const std::string &value); - GLuint getShader(void) const; + GLuint getShader(void) const; + const std::string &getFilePath(void) const; private: void checkCompileErrors(); std::map _defines; - GLenum _type; - GLuint _shader_id; - std::string _file_path; + GLenum _type; + GLuint _shader_id; + std::string _file_path; + + std::unordered_map _files_timestamps; }; #endif diff --git a/includes/RT/ShaderProgram.hpp b/includes/RT/ShaderProgram.hpp index 8a788b3..9d1314d 100644 --- a/includes/RT/ShaderProgram.hpp +++ b/includes/RT/ShaderProgram.hpp @@ -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; diff --git a/shaders/compute.glsl b/shaders/compute.glsl index 9bbd2cd..e85b95f 100644 --- a/shaders/compute.glsl +++ b/shaders/compute.glsl @@ -1,4 +1,4 @@ -#if SHADER_DEBUG +#if 0 #include "shaders/debug.glsl" #else #include "shaders/raytracing.glsl" diff --git a/shaders/raytracing.glsl b/shaders/raytracing.glsl index 668ccc5..a0366bd 100644 --- a/shaders/raytracing.glsl +++ b/shaders/raytracing.glsl @@ -204,7 +204,7 @@ vec3 pathtrace(Ray ray, inout uint rng_state) float p = max(color.r, max(color.g, color.b)); if (randomValue(rng_state) >= p) break; color /= max(p, 0.001); - + GPUMaterial mat = materials[hit.mat_index]; calculateLightColor(mat, hit, color, light, rng_state); diff --git a/srcs/RT.cpp b/srcs/RT.cpp index ae1cdc5..9ca15cb 100644 --- a/srcs/RT.cpp +++ b/srcs/RT.cpp @@ -14,35 +14,13 @@ void setupScreenTriangle(GLuint *VAO); void drawScreenTriangle(GLuint VAO, GLuint output_texture, GLuint program); + std::vector generateTextures(unsigned int textures_count); + std::vector createDataOnGPU(Scene &scene); void updateDataOnGPU(Scene &scene, std::vector buffers); -void shaderDenoise(ShaderProgram &denoising_program, GPUDenoise &denoise, std::vector 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 textures); int main(int argc, char **argv) { diff --git a/srcs/RT_utils.cpp b/srcs/RT_utils.cpp index 06c585a..3acd9f6 100644 --- a/srcs/RT_utils.cpp +++ b/srcs/RT_utils.cpp @@ -115,3 +115,30 @@ void updateDataOnGPU(Scene &scene, std::vector buffers) buffers[7]->update(&scene.getVolume(), sizeof(GPUVolume)); buffers[8]->update(&scene.getDebug(), sizeof(GPUDebug)); } + + +void shaderDenoise(ShaderProgram &denoising_program, GPUDenoise &denoise, std::vector 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); +} \ No newline at end of file diff --git a/srcs/class/Shader.cpp b/srcs/class/Shader.cpp index 1e90182..365dff4 100644 --- a/srcs/class/Shader.cpp +++ b/srcs/class/Shader.cpp @@ -16,7 +16,7 @@ #include #include -std::stringstream loadFileWithIncludes(const std::string& path) +std::stringstream loadFileWithIncludes(const std::string& path, std::vector &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,8 +80,13 @@ void Shader::compile() { _shader_id = glCreateShader(_type); - std::string shader_code = loadFileWithIncludes(_file_path).str(); - + std::vector 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; shader_code = "#version 430\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); +} \ No newline at end of file diff --git a/srcs/class/ShaderProgram.cpp b/srcs/class/ShaderProgram.cpp index 0d4f81c..91dbca1 100644 --- a/srcs/class/ShaderProgram.cpp +++ b/srcs/class/ShaderProgram.cpp @@ -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;