~ | Working atrous algorithm

This commit is contained in:
TheRedShip
2025-02-08 21:43:49 +01:00
parent cbc2550d78
commit 39196a00ff
12 changed files with 121 additions and 39 deletions

View File

@ -4,6 +4,9 @@ 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 = 3, rgba32f) uniform image2D normal_texture;
layout(binding = 4, rgba32f) uniform image2D position_texture;
struct GPUObject {
mat4 rotation;
@ -176,7 +179,7 @@ vec3 pathtrace(Ray ray, inout uint rng_state)
for (int i = 0; i < camera.bounce; i++)
{
hitInfo hit = traceRay(ray);
#if 0
float t_scatter = 0.0;
bool scatter_valid = bool(volume.enabled != 0 && atmosScatter(hit, t_scatter, rng_state));
@ -193,6 +196,12 @@ vec3 pathtrace(Ray ray, inout uint rng_state)
break;
}
if (i == 0)
{
imageStore(normal_texture, ivec2(gl_GlobalInvocationID.xy), vec4(hit.normal, 1.0));
imageStore(position_texture, ivec2(gl_GlobalInvocationID.xy), vec4(hit.position, 1.0));
}
float p = max(color.r, max(color.g, color.b));
if (randomValue(rng_state) >= p) break;
color /= max(p, 0.001);

View File

@ -4,7 +4,12 @@ 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;
layout(binding = 3, rgba32f) uniform image2D position_texture;
layout(binding = 4, rgba32f) uniform image2D normal_texture;
uniform vec2 u_resolution;
uniform int u_pass;
void main()
{
@ -12,9 +17,55 @@ void main()
if (pixel_coords.x >= int(u_resolution.x) || pixel_coords.y >= int(u_resolution.y))
return;
vec4 color = imageLoad(read_texture, pixel_coords);
float c_phi = 1.0;
float p_phi = 1.0;
float n_phi = 1.0;
int holes = int(pow(2, u_pass));
color *= 0.5;
vec4 color_center = imageLoad(read_texture, pixel_coords);
vec3 position_center = imageLoad(position_texture, pixel_coords).xyz;
vec3 normal_center = imageLoad(normal_texture, pixel_coords).xyz;
imageStore(write_texture, pixel_coords, color);
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);
for (int x = -2; x <= 2; x++)
{
for (int y = -2; y <= 2; y++)
{
ivec2 coords = pixel_coords + ivec2(x * holes, y * holes);
if (coords.x < 0. || coords.y < 0. || coords.x >= int(u_resolution.x) || coords.y >= int(u_resolution.y))
continue ;
// coords = clamp(coords, ivec2(-1.0), u_resolution);
vec4 color_sample = imageLoad(read_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 / c_phi);
// Position weight
float posDist = distance(position_center, position_sample);
float w_p = exp(-posDist / p_phi);
// Normal weight
float normalDist = distance(normal_center, normal_sample);
float w_n = exp(-normalDist / n_phi);
float weight = kernel[x+2] * kernel[y+2] * w_c * w_p * w_n;
color += color_sample * weight;
totalWeight += weight;
}
}
imageStore(write_texture, pixel_coords, color / totalWeight);
}

View File

@ -2,7 +2,7 @@ hitInfo traceRay(Ray ray);
vec3 GetEnvironmentLight(Ray ray)
{
// return vec3(0.);
return vec3(0.);
vec3 sun_pos = vec3(-0.5, 0.5, 0.5);
float SunFocus = 1.5;
float SunIntensity = 1.;
@ -65,7 +65,7 @@ vec3 sampleQuadLight(vec3 position, GPUObject obj, int light_index, GPUMaterial
vec3 normal = normalize(crossQuad);
float cos_theta = max(0.0, dot(normal, -light_dir));
return mat.emission * mat.color / (light_dist);
return mat.emission * mat.color / (light_dist * light_dist) * pdf;
}
vec3 sampleLights(vec3 position, inout uint rng_state)
@ -134,6 +134,6 @@ void calculateLightColor(GPUMaterial mat, hitInfo hit, inout vec3 color, inou
{
color *= mat.color;
light += mat.emission * mat.color;
// light += sampleLights(hit.position, rng_state);
}
// light += sampleLights(hit.position, rng_state);
}