Untitled
Loading...
Searching...
No Matches
lightmap_denoise.comp
Go to the documentation of this file.
1
2#version 430 core
3layout(local_size_x = 16, local_size_y = 16) in;
4
5layout(rgba32f, binding = 0) uniform readonly image2D uInputLightmap;
6layout(rgba32f, binding = 1) uniform writeonly image2D uOutputLightmap;
7layout(rgba16f, binding = 2) uniform readonly image2D uInputDir;
8layout(rgba16f, binding = 3) uniform writeonly image2D uOutputDir;
9
10uniform sampler2D uLightmapPosTex;
11uniform sampler2D uLightmapNormalTex;
12uniform isampler2D uLightmapIDTex;
13uniform int uLightmapWidth;
14uniform int uLightmapHeight;
15
16const int KERNEL_RADIUS = 4;
17
18void main() {
19 ivec2 coord = ivec2(gl_GlobalInvocationID.xy);
20 if (coord.x < 0 || coord.y < 0 || coord.x >= uLightmapWidth || coord.y >= uLightmapHeight) return;
21
22 vec4 center_color = imageLoad(uInputLightmap, coord);
23 vec4 center_dir = imageLoad(uInputDir, coord);
24 vec3 center_pos = texelFetch(uLightmapPosTex, coord, 0).rgb;
25 vec3 center_normal = texelFetch(uLightmapNormalTex, coord, 0).rgb;
26 ivec3 ids = texelFetch(uLightmapIDTex, coord, 0).rgb;
27
28 if (ids.y < 0) {
29 imageStore(uOutputLightmap, coord, vec4(0.0));
30 imageStore(uOutputDir, coord, vec4(0.0));
31 return;
32 }
33
34 vec4 sum_color = vec4(0.0);
35 vec4 sum_dir = vec4(0.0);
36 float total_weight = 0.0;
37
38 for (int y = -KERNEL_RADIUS; y <= KERNEL_RADIUS; y++) {
39 for (int x = -KERNEL_RADIUS; x <= KERNEL_RADIUS; x++) {
40 ivec2 neighbor_coord = coord + ivec2(x, y);
41 if (neighbor_coord.x < 0 || neighbor_coord.y < 0 || neighbor_coord.x >= uLightmapWidth || neighbor_coord.y >= uLightmapHeight) continue;
42
43 vec4 neighbor_color = imageLoad(uInputLightmap, neighbor_coord);
44 vec4 neighbor_dir = imageLoad(uInputDir, neighbor_coord);
45 vec3 neighbor_pos = texelFetch(uLightmapPosTex, neighbor_coord, 0).rgb;
46 vec3 neighbor_normal = texelFetch(uLightmapNormalTex, neighbor_coord, 0).rgb;
47 ivec3 neighbor_ids = texelFetch(uLightmapIDTex, neighbor_coord, 0).rgb;
48 if (neighbor_ids.y < 0) continue;
49
50 float dist_sq = float(x*x + y*y);
51 float w_spatial = exp(-dist_sq / 16.0);
52
53 float normal_dot = max(0.0, dot(center_normal, neighbor_normal));
54 float w_normal = pow(normal_dot, 16.0);
55
56 float pos_dist_sq = dot(center_pos - neighbor_pos, center_pos - neighbor_pos);
57 float w_position = exp(-pos_dist_sq / 0.05);
58
59 float weight = w_spatial * w_normal * w_position;
60
61 sum_color += neighbor_color * weight;
62 sum_dir += neighbor_dir * weight;
63 total_weight += weight;
64 }
65 }
66
67 vec4 final_color = (total_weight > 0.0) ? (sum_color / total_weight) : center_color;
68 vec4 final_dir = (total_weight > 0.0) ? (sum_dir / total_weight) : center_dir;
69 imageStore(uOutputLightmap, coord, final_color);
70 imageStore(uOutputDir, coord, final_dir);
71}
layout(local_size_x=16, local_size_y=16) in
void main()
uniform int uLightmapHeight
uniform int uLightmapWidth
uniform sampler2D uLightmapPosTex
uniform sampler2D uLightmapNormalTex
uniform isampler2D uLightmapIDTex
const int KERNEL_RADIUS
int y
Definition types.h:15
int y
Definition types.h:14
int x
Definition types.h:14
Definition types.h:14
Definition types.h:15
Definition types.h:12
Definition types.h:13