Debian Codegraph @

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

raw
1 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2016, Intel Corporation
3 // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
4 // documentation files (the "Software"), to deal in the Software without restriction, including without limitation
5 // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to the following conditions:
7 // The above copyright notice and this permission notice shall be included in all copies or substantial portions of
8 // the Software.
9 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
10 // THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
11 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
12 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
13 // SOFTWARE.
14 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
15 // File changes (yyyy-mm-dd)
16 // 2016-09-07: [email protected]: first commit
17 // 2020-12-05: clayjohn: convert to Vulkan and Godot
18 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
19
20 #[compute]
21
22 #version 450
23
24 #VERSION_DEFINES
25
26 layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
27
28 layout(set = 0, binding = 0) uniform sampler2D source_ssao;
29
30 layout(rg8, set = 1, binding = 0) uniform restrict writeonly image2D dest_image;
31
32 layout(push_constant, std430) uniform Params {
33 float edge_sharpness;
34 float pad;
35 vec2 half_screen_pixel_size;
36 }
37 params;
38
39 vec4 unpack_edges(float p_packed_val) {
40 uint packed_val = uint(p_packed_val * 255.5);
41 vec4 edgesLRTB;
42 edgesLRTB.x = float((packed_val >> 6) & 0x03) / 3.0;
43 edgesLRTB.y = float((packed_val >> 4) & 0x03) / 3.0;
44 edgesLRTB.z = float((packed_val >> 2) & 0x03) / 3.0;
45 edgesLRTB.w = float((packed_val >> 0) & 0x03) / 3.0;
46
47 return clamp(edgesLRTB + params.edge_sharpness, 0.0, 1.0);
48 }
49
50 void add_sample(float p_ssao_value, float p_edge_value, inout float r_sum, inout float r_sum_weight) {
51 float weight = p_edge_value;
52
53 r_sum += (weight * p_ssao_value);
54 r_sum_weight += weight;
55 }
56
57 #ifdef MODE_WIDE
58 vec2 sample_blurred_wide(vec2 p_coord) {
59 vec2 vC = textureLodOffset(source_ssao, vec2(p_coord), 0.0, ivec2(0, 0)).xy;
60 vec2 vL = textureLodOffset(source_ssao, vec2(p_coord), 0.0, ivec2(-2, 0)).xy;
61 vec2 vT = textureLodOffset(source_ssao, vec2(p_coord), 0.0, ivec2(0, -2)).xy;
62 vec2 vR = textureLodOffset(source_ssao, vec2(p_coord), 0.0, ivec2(2, 0)).xy;
63 vec2 vB = textureLodOffset(source_ssao, vec2(p_coord), 0.0, ivec2(0, 2)).xy;
64
65 float packed_edges = vC.y;
66 vec4 edgesLRTB = unpack_edges(packed_edges);
67 edgesLRTB.x *= unpack_edges(vL.y).y;
68 edgesLRTB.z *= unpack_edges(vT.y).w;
69 edgesLRTB.y *= unpack_edges(vR.y).x;
70 edgesLRTB.w *= unpack_edges(vB.y).z;
71
72 float ssao_value = vC.x;
73 float ssao_valueL = vL.x;
74 float ssao_valueT = vT.x;
75 float ssao_valueR = vR.x;
76 float ssao_valueB = vB.x;
77
78 float sum_weight = 0.8f;
79 float sum = ssao_value * sum_weight;
80
81 add_sample(ssao_valueL, edgesLRTB.x, sum, sum_weight);
82 add_sample(ssao_valueR, edgesLRTB.y, sum, sum_weight);
83 add_sample(ssao_valueT, edgesLRTB.z, sum, sum_weight);
84 add_sample(ssao_valueB, edgesLRTB.w, sum, sum_weight);
85
86 float ssao_avg = sum / sum_weight;
87
88 ssao_value = ssao_avg;
89
90 return vec2(ssao_value, packed_edges);
91 }
92 #endif
93
94 #ifdef MODE_SMART
95 vec2 sample_blurred(vec3 p_pos, vec2 p_coord) {
96 float packed_edges = texelFetch(source_ssao, ivec2(p_pos.xy), 0).y;
97 vec4 edgesLRTB = unpack_edges(packed_edges);
98
99 vec4 valuesUL = textureGather(source_ssao, vec2(p_coord - params.half_screen_pixel_size * 0.5));
100 vec4 valuesBR = textureGather(source_ssao, vec2(p_coord + params.half_screen_pixel_size * 0.5));
101
102 float ssao_value = valuesUL.y;
103 float ssao_valueL = valuesUL.x;
104 float ssao_valueT = valuesUL.z;
105 float ssao_valueR = valuesBR.z;
106 float ssao_valueB = valuesBR.x;
107
108 float sum_weight = 0.5;
109 float sum = ssao_value * sum_weight;
110
111 add_sample(ssao_valueL, edgesLRTB.x, sum, sum_weight);
112 add_sample(ssao_valueR, edgesLRTB.y, sum, sum_weight);
113
114 add_sample(ssao_valueT, edgesLRTB.z, sum, sum_weight);
115 add_sample(ssao_valueB, edgesLRTB.w, sum, sum_weight);
116
117 float ssao_avg = sum / sum_weight;
118
119 ssao_value = ssao_avg;
120
121 return vec2(ssao_value, packed_edges);
122 }
123 #endif
124
125 void main() {
126 // Pixel being shaded
127 ivec2 ssC = ivec2(gl_GlobalInvocationID.xy);
128
129 #ifdef MODE_NON_SMART
130
131 vec2 half_pixel = params.half_screen_pixel_size * 0.5;
132
133 vec2 uv = (vec2(gl_GlobalInvocationID.xy) + vec2(0.5, 0.5)) * params.half_screen_pixel_size;
134
135 vec2 center = textureLod(source_ssao, vec2(uv), 0.0).xy;
136
137 vec4 vals;
138 vals.x = textureLod(source_ssao, vec2(uv + vec2(-half_pixel.x * 3, -half_pixel.y)), 0.0).x;
139 vals.y = textureLod(source_ssao, vec2(uv + vec2(+half_pixel.x, -half_pixel.y * 3)), 0.0).x;
140 vals.z = textureLod(source_ssao, vec2(uv + vec2(-half_pixel.x, +half_pixel.y * 3)), 0.0).x;
141 vals.w = textureLod(source_ssao, vec2(uv + vec2(+half_pixel.x * 3, +half_pixel.y)), 0.0).x;
142
143 vec2 sampled = vec2(dot(vals, vec4(0.2)) + center.x * 0.2, center.y);
144
145 #else
146 #ifdef MODE_SMART
147 vec2 sampled = sample_blurred(vec3(gl_GlobalInvocationID), (vec2(gl_GlobalInvocationID.xy) + vec2(0.5, 0.5)) * params.half_screen_pixel_size);
148 #else // MODE_WIDE
149 vec2 sampled = sample_blurred_wide((vec2(gl_GlobalInvocationID.xy) + vec2(0.5, 0.5)) * params.half_screen_pixel_size);
150 #endif
151
152 #endif
153 imageStore(dest_image, ivec2(ssC), vec4(sampled, 0.0, 0.0));
154 }