Debian Codegraph @

servers/rendering/renderer_rd/shaders/effects/ssao_interleave.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 #[compute]
20
21 #version 450
22
23 #VERSION_DEFINES
24
25 layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
26
27 layout(rgba8, set = 0, binding = 0) uniform restrict writeonly image2D dest_image;
28 layout(set = 1, binding = 0) uniform sampler2DArray source_texture;
29
30 layout(push_constant, std430) uniform Params {
31 float inv_sharpness;
32 uint size_modifier;
33 vec2 pixel_size;
34 }
35 params;
36
37 vec4 unpack_edges(float p_packed_val) {
38 uint packed_val = uint(p_packed_val * 255.5);
39 vec4 edgesLRTB;
40 edgesLRTB.x = float((packed_val >> 6) & 0x03) / 3.0;
41 edgesLRTB.y = float((packed_val >> 4) & 0x03) / 3.0;
42 edgesLRTB.z = float((packed_val >> 2) & 0x03) / 3.0;
43 edgesLRTB.w = float((packed_val >> 0) & 0x03) / 3.0;
44
45 return clamp(edgesLRTB + params.inv_sharpness, 0.0, 1.0);
46 }
47
48 void main() {
49 ivec2 ssC = ivec2(gl_GlobalInvocationID.xy);
50 if (any(greaterThanEqual(ssC, ivec2(1.0 / params.pixel_size)))) { //too large, do nothing
51 return;
52 }
53
54 #ifdef MODE_SMART
55 float ao;
56 uvec2 pix_pos = uvec2(gl_GlobalInvocationID.xy);
57 vec2 uv = (gl_GlobalInvocationID.xy + vec2(0.5)) * params.pixel_size;
58
59 // calculate index in the four deinterleaved source array texture
60 int mx = int(pix_pos.x % 2);
61 int my = int(pix_pos.y % 2);
62 int index_center = mx + my * 2; // center index
63 int index_horizontal = (1 - mx) + my * 2; // neighboring, horizontal
64 int index_vertical = mx + (1 - my) * 2; // neighboring, vertical
65 int index_diagonal = (1 - mx) + (1 - my) * 2; // diagonal
66
67 vec2 center_val = texelFetch(source_texture, ivec3(pix_pos / uvec2(params.size_modifier), index_center), 0).xy;
68
69 ao = center_val.x;
70
71 vec4 edgesLRTB = unpack_edges(center_val.y);
72
73 // convert index shifts to sampling offsets
74 float fmx = float(mx);
75 float fmy = float(my);
76
77 // in case of an edge, push sampling offsets away from the edge (towards pixel center)
78 float fmxe = (edgesLRTB.y - edgesLRTB.x);
79 float fmye = (edgesLRTB.w - edgesLRTB.z);
80
81 // calculate final sampling offsets and sample using bilinear filter
82 vec2 uv_horizontal = (gl_GlobalInvocationID.xy + vec2(0.5) + vec2(fmx + fmxe - 0.5, 0.5 - fmy)) * params.pixel_size;
83 float ao_horizontal = textureLod(source_texture, vec3(uv_horizontal, index_horizontal), 0.0).x;
84 vec2 uv_vertical = (gl_GlobalInvocationID.xy + vec2(0.5) + vec2(0.5 - fmx, fmy - 0.5 + fmye)) * params.pixel_size;
85 float ao_vertical = textureLod(source_texture, vec3(uv_vertical, index_vertical), 0.0).x;
86 vec2 uv_diagonal = (gl_GlobalInvocationID.xy + vec2(0.5) + vec2(fmx - 0.5 + fmxe, fmy - 0.5 + fmye)) * params.pixel_size;
87 float ao_diagonal = textureLod(source_texture, vec3(uv_diagonal, index_diagonal), 0.0).x;
88
89 // reduce weight for samples near edge - if the edge is on both sides, weight goes to 0
90 vec4 blendWeights;
91 blendWeights.x = 1.0;
92 blendWeights.y = (edgesLRTB.x + edgesLRTB.y) * 0.5;
93 blendWeights.z = (edgesLRTB.z + edgesLRTB.w) * 0.5;
94 blendWeights.w = (blendWeights.y + blendWeights.z) * 0.5;
95
96 // calculate weighted average
97 float blendWeightsSum = dot(blendWeights, vec4(1.0, 1.0, 1.0, 1.0));
98 ao = dot(vec4(ao, ao_horizontal, ao_vertical, ao_diagonal), blendWeights) / blendWeightsSum;
99
100 imageStore(dest_image, ivec2(gl_GlobalInvocationID.xy), vec4(ao));
101 #else // !MODE_SMART
102
103 vec2 uv = (gl_GlobalInvocationID.xy + vec2(0.5)) * params.pixel_size;
104 #ifdef MODE_HALF
105 float a = textureLod(source_texture, vec3(uv, 0), 0.0).x;
106 float d = textureLod(source_texture, vec3(uv, 3), 0.0).x;
107 float avg = (a + d) * 0.5;
108
109 #else
110 float a = textureLod(source_texture, vec3(uv, 0), 0.0).x;
111 float b = textureLod(source_texture, vec3(uv, 1), 0.0).x;
112 float c = textureLod(source_texture, vec3(uv, 2), 0.0).x;
113 float d = textureLod(source_texture, vec3(uv, 3), 0.0).x;
114 float avg = (a + b + c + d) * 0.25;
115
116 #endif
117 imageStore(dest_image, ivec2(gl_GlobalInvocationID.xy), vec4(avg));
118 #endif
119 }