From cbc2550d7840ad89347463150192a39afb7fd19c Mon Sep 17 00:00:00 2001 From: TheRedShip Date: Fri, 7 Feb 2025 23:32:52 +0100 Subject: [PATCH] ~ | Pass system shader --- includes/RT/Shader.hpp | 7 ++++++- shaders/denoising.glsl | 20 ++++++++++++++++++++ shaders/frag.frag | 38 +------------------------------------- srcs/RT.cpp | 16 +++++++++++++++- srcs/class/Shader.cpp | 38 ++++++++++++++++++++++++++++++++++---- 5 files changed, 76 insertions(+), 43 deletions(-) create mode 100644 shaders/denoising.glsl diff --git a/includes/RT/Shader.hpp b/includes/RT/Shader.hpp index 2f4db93..30cf82f 100644 --- a/includes/RT/Shader.hpp +++ b/includes/RT/Shader.hpp @@ -18,13 +18,15 @@ class Shader { public: - Shader(std::string vertexPath, std::string fragmentPath, std::string computePath); + Shader(std::string vertexPath, std::string fragmentPath, std::string computePath, std::string denoisingPath); ~Shader(void); void attach(void); void setupVertexBuffer(); void drawTriangles(); + void flipOutputDenoising(bool pass); + // void setBool(const std::string &name, bool value) const; void set_int(const std::string &name, int value) const; void set_float(const std::string &name, float value) const; @@ -37,6 +39,7 @@ class Shader GLuint getProgram(void) const; GLuint getProgramCompute(void) const; + GLuint getProgramComputeDenoising(void) const; std::vector getOutputImage(void); @@ -46,6 +49,7 @@ class Shader GLuint _program; GLuint _program_compute; + GLuint _program_denoising; GLuint _output_texture; GLuint _accumulation_texture; @@ -54,6 +58,7 @@ class Shader GLuint _vertex; GLuint _fragment; GLuint _compute; + GLuint _denoising; size_t _size; diff --git a/shaders/denoising.glsl b/shaders/denoising.glsl new file mode 100644 index 0000000..1a0680c --- /dev/null +++ b/shaders/denoising.glsl @@ -0,0 +1,20 @@ +#version 430 core + +layout(local_size_x = 16, local_size_y = 16) in; +layout(binding = 0, rgba32f) uniform image2D read_texture; +layout(binding = 2, rgba32f) uniform image2D write_texture; + +uniform vec2 u_resolution; + +void main() +{ + ivec2 pixel_coords = ivec2(gl_GlobalInvocationID.xy); + if (pixel_coords.x >= int(u_resolution.x) || pixel_coords.y >= int(u_resolution.y)) + return; + + vec4 color = imageLoad(read_texture, pixel_coords); + + color *= 0.5; + + imageStore(write_texture, pixel_coords, color); +} \ No newline at end of file diff --git a/shaders/frag.frag b/shaders/frag.frag index 05e1568..017073a 100644 --- a/shaders/frag.frag +++ b/shaders/frag.frag @@ -5,41 +5,5 @@ out vec4 FragColor; uniform sampler2D screenTexture; void main() { - vec2 uv = TexCoords; - - float incr_x = 1.0 / 1920.0 ; - float incr_y = 1.0 / 1080.0 ; - - int iteration = 5; - - if (iteration == 0) {FragColor = texture(screenTexture, uv);return;} - - vec4 color = vec4(vec3(0.), 1.0); - float totalWeight = 0.; - - float kernel[5] = float[5](1.0/16.0, 1.0/4.0, 3.0/8.0, 1.0/4.0, 1.0/16.0); - - for (int i = 0; i < iteration; i++) - { - int holes = int(pow(2, i)); - - for (int x = -2; x <= 2; x++) - { - for (int y = -2; y <= 2; y++) - { - vec2 current_uv = uv + vec2(x * holes * incr_x, y * holes * incr_y); - // if (current_uv.x < 0. || current_uv.y < 0. || current_uv.x > 1. || current_uv.y > 1.) - // continue ; - // current_uv = clamp(current_uv, vec2(0.), vec2(1.)); - - float weight = kernel[x+2] * kernel[y+2]; - - totalWeight += weight; - color += texture(screenTexture, current_uv) * weight; - } - } - } - - - FragColor = color / totalWeight; + FragColor = texture(screenTexture, TexCoords); } \ No newline at end of file diff --git a/srcs/RT.cpp b/srcs/RT.cpp index 4bf3961..88bb58d 100644 --- a/srcs/RT.cpp +++ b/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,20 @@ int main(int argc, char **argv) glDispatchCompute((WIDTH + 15) / 16, (HEIGHT + 15) / 16, 1); glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT); + // + glUseProgram(shader.getProgramComputeDenoising()); + glUniform2fv(glGetUniformLocation(shader.getProgramComputeDenoising(), "u_resolution"), 1, glm::value_ptr(glm::vec2(WIDTH, HEIGHT))); + + for (int pass = 0; pass < 1; ++pass) + { + shader.flipOutputDenoising(pass % 2 == 0); + + 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(); diff --git a/srcs/class/Shader.cpp b/srcs/class/Shader.cpp index 9992b8d..db1c1c4 100644 --- a/srcs/class/Shader.cpp +++ b/srcs/class/Shader.cpp @@ -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); @@ -196,6 +207,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); @@ -252,6 +277,11 @@ GLuint Shader::getProgramCompute(void) const return (_program_compute); } +GLuint Shader::getProgramComputeDenoising(void) const +{ + return (_program_denoising); +} + std::vector Shader::getOutputImage(void) { std::vector res(WIDTH * HEIGHT * 4);