mirror of
https://github.com/TheRedShip/RT_GPU.git
synced 2025-09-27 10:48:34 +02:00
~ | Shader hot reloading
This commit is contained in:
@ -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,8 +80,13 @@ 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;
|
||||
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);
|
||||
}
|
@ -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;
|
||||
|
Reference in New Issue
Block a user