mirror of
https://github.com/TheRedShip/RT_GPU.git
synced 2025-09-27 10:48:34 +02:00
Merge remote-tracking branch 'origin/Denoising'
This commit is contained in:
28
srcs/RT.cpp
28
srcs/RT.cpp
@ -21,7 +21,7 @@ int main(int argc, char **argv)
|
||||
if (scene.fail())
|
||||
return (1);
|
||||
Window window(&scene, WIDTH, HEIGHT, "RT_GPU", 0, args);
|
||||
Shader shader("shaders/vertex.vert", "shaders/frag.frag", "shaders/compute.glsl");
|
||||
Shader shader("shaders/vertex.vert", "shaders/frag.frag", "shaders/compute.glsl", "shaders/denoising.glsl");
|
||||
// Shader shader("shaders/vertex.vert", "shaders/frag.frag", "shaders/debug.glsl");
|
||||
|
||||
|
||||
@ -141,6 +141,29 @@ int main(int argc, char **argv)
|
||||
glDispatchCompute((WIDTH + 15) / 16, (HEIGHT + 15) / 16, 1);
|
||||
glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
|
||||
|
||||
GPUDenoise denoise = scene.getDenoise();
|
||||
if (denoise.enabled)
|
||||
{
|
||||
glUseProgram(shader.getProgramComputeDenoising());
|
||||
|
||||
glUniform2fv(glGetUniformLocation(shader.getProgramComputeDenoising(), "u_resolution"), 1, glm::value_ptr(glm::vec2(WIDTH, HEIGHT)));
|
||||
glUniform1f(glGetUniformLocation(shader.getProgramComputeDenoising(), "u_c_phi"), denoise.c_phi);
|
||||
glUniform1f(glGetUniformLocation(shader.getProgramComputeDenoising(), "u_p_phi"), denoise.p_phi);
|
||||
glUniform1f(glGetUniformLocation(shader.getProgramComputeDenoising(), "u_n_phi"), denoise.n_phi);
|
||||
|
||||
for (int pass = 0; pass < denoise.pass ; ++pass)
|
||||
{
|
||||
shader.flipOutputDenoising(pass % 2 == 0);
|
||||
|
||||
glUniform1i(glGetUniformLocation(shader.getProgramComputeDenoising(), "u_pass"), pass);
|
||||
|
||||
glDispatchCompute((WIDTH + 15) / 16, (HEIGHT + 15) / 16, 1);
|
||||
glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
|
||||
}
|
||||
shader.flipOutputDenoising(true);
|
||||
}
|
||||
|
||||
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
window.imGuiNewFrame();
|
||||
@ -152,6 +175,9 @@ int main(int argc, char **argv)
|
||||
|
||||
window.display();
|
||||
window.pollEvents();
|
||||
|
||||
glClearTexImage(shader.getNormalTexture(), 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
|
||||
glClearTexImage(shader.getPositionTexture(), 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
|
||||
}
|
||||
|
||||
ImGui_ImplOpenGL3_Shutdown();
|
||||
|
@ -33,6 +33,11 @@ Scene::Scene(std::string &name)
|
||||
_gpu_debug.triangle_treshold = 1;
|
||||
_gpu_debug.box_treshold = 1;
|
||||
|
||||
_gpu_denoise.enabled = 0;
|
||||
_gpu_denoise.pass = 0;
|
||||
_gpu_denoise.c_phi = 0.1f;
|
||||
_gpu_denoise.p_phi = 0.1f;
|
||||
_gpu_denoise.n_phi = 0.1f;
|
||||
|
||||
if (!file.is_open())
|
||||
{
|
||||
@ -366,6 +371,11 @@ GPUDebug &Scene::getDebug()
|
||||
return (_gpu_debug);
|
||||
}
|
||||
|
||||
GPUDenoise &Scene::getDenoise()
|
||||
{
|
||||
return (_gpu_denoise);
|
||||
}
|
||||
|
||||
std::vector<GPUBvhData> &Scene::getBvhData()
|
||||
{
|
||||
return (_gpu_bvh_data);
|
||||
|
@ -61,11 +61,12 @@ void printWithLineNumbers(const char *str)
|
||||
std::cout << lineNumber++ << ": " << line << std::endl;
|
||||
}
|
||||
|
||||
Shader::Shader(std::string vertexPath, std::string fragmentPath, std::string computePath)
|
||||
Shader::Shader(std::string vertexPath, std::string fragmentPath, std::string computePath, std::string denoisingPath)
|
||||
{
|
||||
const char *vertexCode = loadFileWithIncludes(vertexPath);
|
||||
const char *fragmentCode = loadFileWithIncludes(fragmentPath);
|
||||
const char *computeCode = loadFileWithIncludes(computePath);
|
||||
const char *denoisingCode = loadFileWithIncludes(denoisingPath);
|
||||
|
||||
// printWithLineNumbers(computeCode);
|
||||
|
||||
@ -89,6 +90,13 @@ Shader::Shader(std::string vertexPath, std::string fragmentPath, std::string com
|
||||
glCompileShader(_compute);
|
||||
|
||||
checkCompileErrors(_compute);
|
||||
|
||||
_denoising = glCreateShader(GL_COMPUTE_SHADER);
|
||||
|
||||
glShaderSource(_denoising, 1, &denoisingCode, NULL);
|
||||
glCompileShader(_denoising);
|
||||
|
||||
checkCompileErrors(_denoising);
|
||||
}
|
||||
|
||||
Shader::~Shader(void)
|
||||
@ -98,15 +106,14 @@ Shader::~Shader(void)
|
||||
glDeleteShader(_compute);
|
||||
glDeleteProgram(_program);
|
||||
glDeleteProgram(_program_compute);
|
||||
glDeleteProgram(_denoising);
|
||||
}
|
||||
|
||||
void Shader::attach(void)
|
||||
{
|
||||
_program = glCreateProgram();
|
||||
_program_compute = glCreateProgram();
|
||||
|
||||
glProgramParameteri(_program_compute, GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GL_TRUE);
|
||||
glProgramParameteri(_program, GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GL_TRUE);
|
||||
_program_denoising = glCreateProgram();
|
||||
|
||||
glEnable(GL_DEBUG_OUTPUT);
|
||||
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
|
||||
@ -114,10 +121,14 @@ void Shader::attach(void)
|
||||
|
||||
glAttachShader(_program, _vertex);
|
||||
glAttachShader(_program, _fragment);
|
||||
|
||||
glAttachShader(_program_compute, _compute);
|
||||
|
||||
glAttachShader(_program_denoising, _denoising);
|
||||
|
||||
glLinkProgram(_program);
|
||||
glLinkProgram(_program_compute);
|
||||
glLinkProgram(_program_denoising);
|
||||
|
||||
glGenTextures(1, &_output_texture);
|
||||
glBindTexture(GL_TEXTURE_2D, _output_texture);
|
||||
@ -126,7 +137,7 @@ void Shader::attach(void)
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, WIDTH, HEIGHT, 0, GL_RGBA, GL_FLOAT, NULL);
|
||||
glBindImageTexture(0, _output_texture, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA32F);
|
||||
glBindImageTexture(0, _output_texture, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F);
|
||||
|
||||
glGenTextures(1, &_accumulation_texture);
|
||||
glBindTexture(GL_TEXTURE_2D, _accumulation_texture);
|
||||
@ -136,6 +147,33 @@ void Shader::attach(void)
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, WIDTH, HEIGHT, 0, GL_RGBA, GL_FLOAT, NULL);
|
||||
glBindImageTexture(1, _accumulation_texture, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F);
|
||||
|
||||
glGenTextures(1, &_denoising_texture);
|
||||
glBindTexture(GL_TEXTURE_2D, _denoising_texture);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, WIDTH, HEIGHT, 0, GL_RGBA, GL_FLOAT, NULL);
|
||||
glBindImageTexture(2, _denoising_texture, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F);
|
||||
|
||||
glGenTextures(1, &_normal_texture);
|
||||
glBindTexture(GL_TEXTURE_2D, _normal_texture);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, WIDTH, HEIGHT, 0, GL_RGBA, GL_FLOAT, NULL);
|
||||
glBindImageTexture(3, _normal_texture, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F);
|
||||
|
||||
glGenTextures(1, &_position_texture);
|
||||
glBindTexture(GL_TEXTURE_2D, _position_texture);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, WIDTH, HEIGHT, 0, GL_RGBA, GL_FLOAT, NULL);
|
||||
glBindImageTexture(4, _position_texture, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F);
|
||||
}
|
||||
|
||||
void Shader::checkCompileErrors(GLuint shader)
|
||||
@ -187,6 +225,20 @@ void Shader::drawTriangles()
|
||||
glDrawArrays(GL_TRIANGLES, 0, _size * 3);
|
||||
}
|
||||
|
||||
void Shader::flipOutputDenoising(bool pass)
|
||||
{
|
||||
if (pass)
|
||||
{
|
||||
glBindImageTexture(0, _output_texture, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA32F);
|
||||
glBindImageTexture(2, _denoising_texture, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA32F);
|
||||
}
|
||||
else
|
||||
{
|
||||
glBindImageTexture(0, _denoising_texture, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA32F);
|
||||
glBindImageTexture(2, _output_texture, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA32F);
|
||||
}
|
||||
}
|
||||
|
||||
void Shader::set_int(const std::string &name, int value) const
|
||||
{
|
||||
glUniform1i(glGetUniformLocation(_program_compute, name.c_str()), value);
|
||||
@ -245,6 +297,21 @@ GLuint Shader::getProgramCompute(void) const
|
||||
return (_program_compute);
|
||||
}
|
||||
|
||||
GLuint Shader::getProgramComputeDenoising(void) const
|
||||
{
|
||||
return (_program_denoising);
|
||||
}
|
||||
|
||||
GLuint Shader::getNormalTexture(void) const
|
||||
{
|
||||
return (_normal_texture);
|
||||
}
|
||||
|
||||
GLuint Shader::getPositionTexture(void) const
|
||||
{
|
||||
return (_position_texture);
|
||||
}
|
||||
|
||||
std::vector<float> Shader::getOutputImage(void)
|
||||
{
|
||||
std::vector<float> res(WIDTH * HEIGHT * 4);
|
||||
|
@ -272,17 +272,37 @@ void Window::imGuiRender()
|
||||
has_changed = true;
|
||||
}
|
||||
|
||||
if (ImGui::CollapsingHeader("Denoiser"))
|
||||
{
|
||||
ImGui::PushID(0);
|
||||
|
||||
ImGui::Checkbox("Enable", (bool *)(&_scene->getDenoise().enabled));
|
||||
ImGui::Separator();
|
||||
if (ImGui::SliderInt("Pass", &_scene->getDenoise().pass, 0, 8))
|
||||
_scene->getDenoise().pass = (_scene->getDenoise().pass / 2) * 2; // make sure it's even
|
||||
|
||||
ImGui::SliderFloat("Color diff", &_scene->getDenoise().c_phi, 0.0f, 1.0f);
|
||||
ImGui::SliderFloat("Position diff", &_scene->getDenoise().p_phi, 0.0f, 1.0f);
|
||||
ImGui::SliderFloat("Normal diff", &_scene->getDenoise().n_phi, 0.0f, 1.0f);
|
||||
|
||||
ImGui::PopID();
|
||||
}
|
||||
|
||||
if (ImGui::CollapsingHeader("Debug"))
|
||||
{
|
||||
ImGui::PushID(0);
|
||||
|
||||
has_changed |= ImGui::Checkbox("Enable", (bool *)(&_scene->getDebug().enabled));
|
||||
ImGui::Separator();
|
||||
has_changed |= ImGui::SliderInt("Debug mode", &_scene->getDebug().mode, 0, 2);
|
||||
has_changed |= ImGui::SliderInt("Box treshold", &_scene->getDebug().box_treshold, 1, 2000);
|
||||
has_changed |= ImGui::SliderInt("Triangle treshold", &_scene->getDebug().triangle_treshold, 1, 2000);
|
||||
|
||||
ImGui::PopID();
|
||||
}
|
||||
|
||||
_renderer->renderImgui();;
|
||||
|
||||
_renderer->renderImgui();
|
||||
|
||||
ImGui::End();
|
||||
|
||||
|
Reference in New Issue
Block a user