Debian Codegraph @

servers/rendering/renderer_rd/shaders/effects/screen_space_reflection_resolve.glsl

raw
1 #[compute]
2
3 #version 450
4
5 #VERSION_DEFINES
6
7 layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
8
9 layout(set = 0, binding = 0) uniform sampler2D source_depth;
10 layout(set = 0, binding = 1) uniform sampler2D source_normal_roughness;
11 layout(set = 0, binding = 2) uniform sampler2D source_depth_half;
12 layout(set = 0, binding = 3) uniform sampler2D source_normal_roughness_half;
13 layout(set = 0, binding = 4) uniform sampler2D source_color;
14 layout(set = 0, binding = 5) uniform sampler2D source_mip_level;
15 layout(rgba16f, set = 0, binding = 6) uniform restrict writeonly image2D output_color;
16
17 layout(push_constant, std430) uniform Params {
18 ivec2 screen_size;
19 }
20 params;
21
22 void get_sample(float depth, vec3 normal, float roughness, ivec2 pixel_pos, out vec4 color, out float weight) {
23 float sample_depth = texelFetch(source_depth_half, pixel_pos, 0).x;
24 vec4 sample_normal_roughness = texelFetch(source_normal_roughness_half, pixel_pos, 0);
25 vec3 sample_normal = normalize(sample_normal_roughness.xyz * 2.0 - 1.0);
26 float sample_roughness = sample_normal_roughness.w;
27 if (sample_roughness > 0.5) {
28 sample_roughness = 1.0 - sample_roughness;
29 }
30 sample_roughness /= (127.0 / 255.0);
31
32 vec2 uv = (pixel_pos + 0.5) / (params.screen_size * 0.5);
33
34 float mip_level = texelFetch(source_mip_level, pixel_pos, 0).x * 14.0;
35 color = textureLod(source_color, uv, mip_level);
36
37 // Invert the tone mapping we applied in the main trace pass.
38 const vec3 rec709_luminance_weights = vec3(0.2126, 0.7152, 0.0722);
39 color.rgb /= 1.0 - dot(color.rgb, rec709_luminance_weights);
40
41 const float DEPTH_FACTOR = 2048.0;
42 const float NORMAL_FACTOR = 32.0;
43 const float ROUGHNESS_FACTOR = 16.0;
44
45 float depth_diff = abs(depth - sample_depth);
46 float weight_depth = exp(-depth_diff * DEPTH_FACTOR);
47
48 float normal_diff = clamp(1.0 - dot(normal, sample_normal), 0.0, 1.0);
49 float weight_normal = exp(-normal_diff * NORMAL_FACTOR);
50
51 float roughness_diff = abs(roughness - sample_roughness);
52 float weight_roughness = exp(-roughness_diff * ROUGHNESS_FACTOR);
53
54 weight = weight_depth * weight_normal * weight_roughness;
55 }
56
57 void main() {
58 ivec2 pixel_pos = ivec2(gl_GlobalInvocationID.xy);
59
60 if (any(greaterThanEqual(pixel_pos, params.screen_size))) {
61 return;
62 }
63
64 float depth = texelFetch(source_depth, pixel_pos, 0).x;
65 vec4 normal_roughness = texelFetch(source_normal_roughness, pixel_pos, 0);
66 vec3 normal = normalize(normal_roughness.xyz * 2.0 - 1.0);
67 float roughness = normal_roughness.w;
68 if (roughness > 0.5) {
69 roughness = 1.0 - roughness;
70 }
71 roughness /= (127.0 / 255.0);
72
73 vec2 half_tex_coord = (pixel_pos + 0.5) * 0.5;
74 vec2 bilinear_weights = fract(half_tex_coord);
75
76 vec4 color0, color1, color2, color3;
77 float weight0, weight1, weight2, weight3;
78
79 get_sample(depth, normal, roughness, ((pixel_pos - 1) / 2) + ivec2(0, 0), color0, weight0);
80 get_sample(depth, normal, roughness, ((pixel_pos - 1) / 2) + ivec2(1, 0), color1, weight1);
81 get_sample(depth, normal, roughness, ((pixel_pos - 1) / 2) + ivec2(0, 1), color2, weight2);
82 get_sample(depth, normal, roughness, ((pixel_pos - 1) / 2) + ivec2(1, 1), color3, weight3);
83
84 weight0 *= bilinear_weights.x * bilinear_weights.y;
85 weight1 *= (1.0 - bilinear_weights.x) * bilinear_weights.y;
86 weight2 *= bilinear_weights.x * (1.0 - bilinear_weights.y);
87 weight3 *= (1.0 - bilinear_weights.x) * (1.0 - bilinear_weights.y);
88
89 vec4 result_color = color0 * weight0 + color1 * weight1 + color2 * weight2 + color3 * weight3;
90 float result_weight = weight0 + weight1 + weight2 + weight3;
91 if (result_weight > 0.0) {
92 result_color /= result_weight;
93 } else {
94 result_color = vec4(0.0);
95 }
96
97 imageStore(output_color, pixel_pos, result_color);
98 }