mirror of
https://github.com/TheRedShip/RT_GPU.git
synced 2025-09-27 10:48:34 +02:00
+ | Denoising on light texture
This commit is contained in:
@ -1,18 +1,22 @@
|
||||
layout(local_size_x = 16, local_size_y = 16) in;
|
||||
layout(binding = 0, rgba32f) uniform image2D read_texture;
|
||||
layout(binding = 0, rgba32f) uniform image2D output_texture;
|
||||
layout(binding = 2, rgba32f) uniform image2D write_texture;
|
||||
|
||||
layout(binding = 3, rgba32f) uniform image2D normal_texture;
|
||||
layout(binding = 4, rgba32f) uniform image2D position_texture;
|
||||
layout(binding = 5, rgba32f) uniform image2D light_texture;
|
||||
layout(binding = 7, rgba32f) uniform image2D color_texture;
|
||||
|
||||
|
||||
uniform vec2 u_resolution;
|
||||
uniform int u_pass_count;
|
||||
uniform int u_pass;
|
||||
|
||||
uniform float u_c_phi;
|
||||
uniform float u_p_phi;
|
||||
uniform float u_n_phi;
|
||||
|
||||
|
||||
void main()
|
||||
{
|
||||
ivec2 pixel_coords = ivec2(gl_GlobalInvocationID.xy);
|
||||
@ -21,14 +25,14 @@ void main()
|
||||
|
||||
int holes = int(pow(2, u_pass));
|
||||
|
||||
vec4 color_center = imageLoad(read_texture, pixel_coords);
|
||||
vec4 light_center = imageLoad(light_texture, pixel_coords);
|
||||
vec3 position_center = imageLoad(position_texture, pixel_coords).xyz;
|
||||
vec3 normal_center = imageLoad(normal_texture, pixel_coords).xyz;
|
||||
|
||||
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);
|
||||
|
||||
float totalWeight = 0.;
|
||||
vec4 color = vec4(vec3(0.), 1.0);
|
||||
vec4 light = vec4(vec3(0.), 1.0);
|
||||
|
||||
|
||||
for (int x = -2; x <= 2; x++)
|
||||
@ -40,15 +44,15 @@ void main()
|
||||
continue ;
|
||||
// coords = clamp(coords, ivec2(-1.0), u_resolution);
|
||||
|
||||
vec4 color_sample = imageLoad(read_texture, coords);
|
||||
vec4 light_sample = imageLoad(light_texture, coords);
|
||||
vec3 position_sample = imageLoad(position_texture, coords).xyz;
|
||||
vec3 normal_sample = imageLoad(normal_texture, coords).xyz;
|
||||
|
||||
// Calculate edge-stopping weights
|
||||
|
||||
// Color weight
|
||||
float colorDist = distance(color_center, color_sample);
|
||||
float w_c = exp(-colorDist / u_c_phi);
|
||||
// light weight
|
||||
float lightDist = distance(light_center, light_sample);
|
||||
float w_c = exp(-lightDist / u_c_phi);
|
||||
|
||||
// Position weight
|
||||
float posDist = distance(position_center, position_sample);
|
||||
@ -60,10 +64,17 @@ void main()
|
||||
|
||||
float weight = kernel[x+2] * kernel[y+2] * w_c * w_p * w_n;
|
||||
|
||||
color += color_sample * weight;
|
||||
light += light_sample * weight;
|
||||
totalWeight += weight;
|
||||
}
|
||||
}
|
||||
|
||||
imageStore(write_texture, pixel_coords, color / totalWeight);
|
||||
light /= totalWeight;
|
||||
if (u_pass == u_pass_count - 1)
|
||||
{
|
||||
vec4 color = light * imageLoad(color_texture, pixel_coords);
|
||||
imageStore(output_texture, pixel_coords, color);
|
||||
return;
|
||||
}
|
||||
|
||||
imageStore(write_texture, pixel_coords, light);
|
||||
}
|
@ -1,10 +1,13 @@
|
||||
|
||||
layout(local_size_x = 16, local_size_y = 16) in;
|
||||
layout(binding = 0, rgba32f) uniform image2D output_image;
|
||||
layout(binding = 1, rgba32f) uniform image2D accumulation_image;
|
||||
layout(binding = 0, rgba32f) uniform image2D output_texture;
|
||||
layout(binding = 1, rgba32f) uniform image2D output_accum_texture;
|
||||
|
||||
layout(binding = 3, rgba32f) uniform image2D normal_texture;
|
||||
layout(binding = 4, rgba32f) uniform image2D position_texture;
|
||||
layout(binding = 5, rgba32f) uniform image2D light_texture;
|
||||
layout(binding = 6, rgba32f) uniform image2D light_accum_texture;
|
||||
layout(binding = 7, rgba32f) uniform image2D color_texture;
|
||||
|
||||
struct GPUObject {
|
||||
mat4 rotation;
|
||||
@ -169,7 +172,9 @@ struct hitInfo
|
||||
#include "shaders/volumetric.glsl"
|
||||
#include "shaders/trace.glsl"
|
||||
|
||||
vec3 pathtrace(Ray ray, inout uint rng_state)
|
||||
#define accumulate(texture, new_color, color) imageLoad(texture, ivec2(gl_GlobalInvocationID.xy)); new_color.rgb = mix(new_color.rgb, color, 1.0 / float(u_frameCount + 1)); new_color.a = 1.0; imageStore(texture, ivec2(gl_GlobalInvocationID.xy), new_color);
|
||||
|
||||
vec3[2] pathtrace(Ray ray, inout uint rng_state)
|
||||
{
|
||||
vec3 color = vec3(1.0);
|
||||
vec3 light = vec3(0.0);
|
||||
@ -215,7 +220,8 @@ vec3 pathtrace(Ray ray, inout uint rng_state)
|
||||
ray.inv_direction = 1.0 / ray.direction;
|
||||
}
|
||||
|
||||
return color * light;
|
||||
vec3[2] result = {color, light};
|
||||
return result;
|
||||
}
|
||||
|
||||
Ray initRay(vec2 uv, inout uint rng_state)
|
||||
@ -241,6 +247,7 @@ Ray initRay(vec2 uv, inout uint rng_state)
|
||||
return (Ray(origin, ray_direction, 1.0 / ray_direction));
|
||||
}
|
||||
|
||||
|
||||
void main()
|
||||
{
|
||||
ivec2 pixel_coords = ivec2(gl_GlobalInvocationID.xy);
|
||||
@ -259,19 +266,20 @@ void main()
|
||||
uv.x *= u_resolution.x / u_resolution.y;
|
||||
|
||||
Ray ray = initRay(uv, rng_state);
|
||||
vec3 color = pathtrace(ray, rng_state);
|
||||
vec3[2] color_light = pathtrace(ray, rng_state);
|
||||
vec3 color = color_light[0] * color_light[1];
|
||||
|
||||
float blend = 1.0 / float(u_frameCount + 1);
|
||||
vec4 accum = imageLoad(accumulation_image, pixel_coords);
|
||||
accum.rgb = mix(accum.rgb, color, blend);
|
||||
accum.a = 1.0;
|
||||
vec4 accum_color = accumulate(color_texture, accum_color, sqrt(color_light[0]));
|
||||
|
||||
imageStore(accumulation_image, pixel_coords, accum);
|
||||
vec4 accum_light = accumulate(light_accum_texture, accum_light, color_light[1]);
|
||||
vec4 final_light = sqrt(accum_light);
|
||||
imageStore(light_texture, pixel_coords, final_light);
|
||||
|
||||
vec4 final_color = vec4(sqrt(accum.r), sqrt(accum.g), sqrt(accum.b), accum.a);
|
||||
vec4 accum = accumulate(output_accum_texture, accum, color);
|
||||
vec4 final_output_color = sqrt(accum);
|
||||
|
||||
for (int y = 0; y < u_pixelisation; y++)
|
||||
for (int x = 0; x < u_pixelisation; x++)
|
||||
imageStore(output_image, pixel_coords + ivec2(x, y), final_color);
|
||||
imageStore(output_texture, pixel_coords + ivec2(x, y), final_output_color);
|
||||
}
|
||||
|
||||
|
14
srcs/RT.cpp
14
srcs/RT.cpp
@ -37,7 +37,15 @@ int main(int argc, char **argv)
|
||||
GLuint VAO;
|
||||
setupScreenTriangle(&VAO);
|
||||
|
||||
std::vector<GLuint> textures = generateTextures(5);
|
||||
std::vector<GLuint> textures = generateTextures(8);
|
||||
//0 output
|
||||
//1 output_accumulation
|
||||
//2 denoising
|
||||
//3 normal
|
||||
//4 position
|
||||
//5 light
|
||||
//6 light_accum
|
||||
//7 color
|
||||
|
||||
ShaderProgram raytracing_program;
|
||||
Shader compute = Shader(GL_COMPUTE_SHADER, "shaders/compute.glsl");
|
||||
@ -99,8 +107,8 @@ int main(int argc, char **argv)
|
||||
window.display();
|
||||
window.pollEvents();
|
||||
|
||||
glClearTexImage(textures[3], 0, GL_RGBA, GL_FLOAT, nullptr);
|
||||
glClearTexImage(textures[4], 0, GL_RGBA, GL_FLOAT, nullptr);
|
||||
// glClearTexImage(textures[3], 0, GL_RGBA, GL_FLOAT, nullptr);
|
||||
// glClearTexImage(textures[4], 0, GL_RGBA, GL_FLOAT, nullptr);
|
||||
}
|
||||
|
||||
ImGui_ImplOpenGL3_Shutdown();
|
||||
|
@ -122,16 +122,17 @@ void shaderDenoise(ShaderProgram &denoising_program, GPUDenoise &denoise, std::v
|
||||
denoising_program.use();
|
||||
|
||||
denoising_program.set_vec2("u_resolution", glm::vec2(WIDTH, HEIGHT));
|
||||
denoising_program.set_int("u_pass_count", denoise.pass);
|
||||
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 output_texture = 5;
|
||||
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(5, 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);
|
||||
@ -140,5 +141,5 @@ void shaderDenoise(ShaderProgram &denoising_program, GPUDenoise &denoise, std::v
|
||||
std::swap(output_texture, denoising_texture);
|
||||
}
|
||||
|
||||
glBindImageTexture(0, textures[output_texture], 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F);
|
||||
glBindImageTexture(5, textures[output_texture], 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F);
|
||||
}
|
Reference in New Issue
Block a user