Debian Codegraph @

servers/rendering/renderer_rd/shaders/effects/ssil_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 // 2021-05-27: clayjohn: convert SSAO to SSIL
19 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
20
21 #[compute]
22
23 #version 450
24
25 #VERSION_DEFINES
26
27 layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
28
29 layout(rgba16, set = 0, binding = 0) uniform restrict writeonly image2D dest_image;
30 layout(set = 1, binding = 0) uniform sampler2DArray source_texture;
31 layout(r8, set = 2, binding = 0) uniform restrict readonly image2DArray source_edges;
32
33 layout(push_constant, std430) uniform Params {
34 float inv_sharpness;
35 uint size_modifier;
36 vec2 pixel_size;
37 }
38 params;
39
40 vec4 unpack_edges(float p_packed_val) {
41 uint packed_val = uint(p_packed_val * 255.5);
42 vec4 edgesLRTB;
43 edgesLRTB.x = float((packed_val >> 6) & 0x03) / 3.0;
44 edgesLRTB.y = float((packed_val >> 4) & 0x03) / 3.0;
45 edgesLRTB.z = float((packed_val >> 2) & 0x03) / 3.0;
46 edgesLRTB.w = float((packed_val >> 0) & 0x03) / 3.0;
47
48 return clamp(edgesLRTB + params.inv_sharpness, 0.0, 1.0);
49 }
50
51 void main() {
52 ivec2 ssC = ivec2(gl_GlobalInvocationID.xy);
53 if (any(greaterThanEqual(ssC, ivec2(1.0 / params.pixel_size)))) { //too large, do nothing
54 return;
55 }
56
57 #ifdef MODE_SMART
58 uvec2 pix_pos = uvec2(gl_GlobalInvocationID.xy);
59 vec2 uv = (gl_GlobalInvocationID.xy + vec2(0.5)) * params.pixel_size;
60
61 // calculate index in the four deinterleaved source array texture
62 int mx = int(pix_pos.x % 2);
63 int my = int(pix_pos.y % 2);
64 int index_center = mx + my * 2; // center index
65 int index_horizontal = (1 - mx) + my * 2; // neighboring, horizontal
66 int index_vertical = mx + (1 - my) * 2; // neighboring, vertical
67 int index_diagonal = (1 - mx) + (1 - my) * 2; // diagonal
68
69 vec4 color = texelFetch(source_texture, ivec3(pix_pos / uvec2(params.size_modifier), index_center), 0);
70
71 vec4 edgesLRTB = unpack_edges(imageLoad(source_edges, ivec3(pix_pos / uvec2(params.size_modifier), index_center)).r);
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 vec4 color_horizontal = textureLod(source_texture, vec3(uv_horizontal, index_horizontal), 0.0);
84 vec2 uv_vertical = (gl_GlobalInvocationID.xy + vec2(0.5) + vec2(0.5 - fmx, fmy - 0.5 + fmye)) * params.pixel_size;
85 vec4 color_vertical = textureLod(source_texture, vec3(uv_vertical, index_vertical), 0.0);
86 vec2 uv_diagonal = (gl_GlobalInvocationID.xy + vec2(0.5) + vec2(fmx - 0.5 + fmxe, fmy - 0.5 + fmye)) * params.pixel_size;
87 vec4 color_diagonal = textureLod(source_texture, vec3(uv_diagonal, index_diagonal), 0.0);
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 color += color_horizontal * blendWeights.y;
99 color += color_vertical * blendWeights.z;
100 color += color_diagonal * blendWeights.w;
101 color /= blendWeightsSum;
102
103 imageStore(dest_image, ivec2(gl_GlobalInvocationID.xy), color);
104 #else // !MODE_SMART
105
106 vec2 uv = (gl_GlobalInvocationID.xy + vec2(0.5)) * params.pixel_size;
107 #ifdef MODE_HALF
108 vec4 a = textureLod(source_texture, vec3(uv, 0), 0.0);
109 vec4 d = textureLod(source_texture, vec3(uv, 3), 0.0);
110 vec4 avg = (a + d) * 0.5;
111
112 #else
113 vec4 a = textureLod(source_texture, vec3(uv, 0), 0.0);
114 vec4 b = textureLod(source_texture, vec3(uv, 1), 0.0);
115 vec4 c = textureLod(source_texture, vec3(uv, 2), 0.0);
116 vec4 d = textureLod(source_texture, vec3(uv, 3), 0.0);
117 vec4 avg = (a + b + c + d) * 0.25;
118
119 #endif
120 imageStore(dest_image, ivec2(gl_GlobalInvocationID.xy), avg);
121 #endif
122 }