| 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(set = 0, binding = 0) uniform sampler2D source_ssil; |
| 30 |
|
| 31 |
layout(rgba16, set = 1, binding = 0) uniform restrict writeonly image2D dest_image; |
| 32 |
|
| 33 |
layout(r8, set = 2, binding = 0) uniform restrict readonly image2D source_edges; |
| 34 |
|
| 35 |
layout(push_constant, std430) uniform Params { |
| 36 |
float edge_sharpness; |
| 37 |
float pad; |
| 38 |
vec2 half_screen_pixel_size; |
| 39 |
} |
| 40 |
params; |
| 41 |
|
| 42 |
vec4 unpack_edges(float p_packed_val) { |
| 43 |
uint packed_val = uint(p_packed_val * 255.5); |
| 44 |
vec4 edgesLRTB; |
| 45 |
edgesLRTB.x = float((packed_val >> 6) & 0x03) / 3.0; |
| 46 |
edgesLRTB.y = float((packed_val >> 4) & 0x03) / 3.0; |
| 47 |
edgesLRTB.z = float((packed_val >> 2) & 0x03) / 3.0; |
| 48 |
edgesLRTB.w = float((packed_val >> 0) & 0x03) / 3.0; |
| 49 |
|
| 50 |
return clamp(edgesLRTB + params.edge_sharpness, 0.0, 1.0); |
| 51 |
} |
| 52 |
|
| 53 |
void add_sample(vec4 p_ssil_value, float p_edge_value, inout vec4 r_sum, inout float r_sum_weight) { |
| 54 |
float weight = p_edge_value; |
| 55 |
|
| 56 |
r_sum += (weight * p_ssil_value); |
| 57 |
r_sum_weight += weight; |
| 58 |
} |
| 59 |
|
| 60 |
#ifdef MODE_WIDE |
| 61 |
vec4 sample_blurred_wide(ivec2 p_pos, vec2 p_coord) { |
| 62 |
vec4 ssil_value = textureLodOffset(source_ssil, vec2(p_coord), 0.0, ivec2(0, 0)); |
| 63 |
vec4 ssil_valueL = textureLodOffset(source_ssil, vec2(p_coord), 0.0, ivec2(-2, 0)); |
| 64 |
vec4 ssil_valueT = textureLodOffset(source_ssil, vec2(p_coord), 0.0, ivec2(0, -2)); |
| 65 |
vec4 ssil_valueR = textureLodOffset(source_ssil, vec2(p_coord), 0.0, ivec2(2, 0)); |
| 66 |
vec4 ssil_valueB = textureLodOffset(source_ssil, vec2(p_coord), 0.0, ivec2(0, 2)); |
| 67 |
|
| 68 |
vec4 edgesLRTB = unpack_edges(imageLoad(source_edges, p_pos).r); |
| 69 |
edgesLRTB.x *= unpack_edges(imageLoad(source_edges, p_pos + ivec2(-2, 0)).r).y; |
| 70 |
edgesLRTB.z *= unpack_edges(imageLoad(source_edges, p_pos + ivec2(0, -2)).r).w; |
| 71 |
edgesLRTB.y *= unpack_edges(imageLoad(source_edges, p_pos + ivec2(2, 0)).r).x; |
| 72 |
edgesLRTB.w *= unpack_edges(imageLoad(source_edges, p_pos + ivec2(0, 2)).r).z; |
| 73 |
|
| 74 |
float sum_weight = 0.8; |
| 75 |
vec4 sum = ssil_value * sum_weight; |
| 76 |
|
| 77 |
add_sample(ssil_valueL, edgesLRTB.x, sum, sum_weight); |
| 78 |
add_sample(ssil_valueR, edgesLRTB.y, sum, sum_weight); |
| 79 |
add_sample(ssil_valueT, edgesLRTB.z, sum, sum_weight); |
| 80 |
add_sample(ssil_valueB, edgesLRTB.w, sum, sum_weight); |
| 81 |
|
| 82 |
vec4 ssil_avg = sum / sum_weight; |
| 83 |
|
| 84 |
ssil_value = ssil_avg; |
| 85 |
|
| 86 |
return ssil_value; |
| 87 |
} |
| 88 |
#endif |
| 89 |
|
| 90 |
#ifdef MODE_SMART |
| 91 |
vec4 sample_blurred(ivec2 p_pos, vec2 p_coord) { |
| 92 |
vec4 vC = textureLodOffset(source_ssil, vec2(p_coord), 0.0, ivec2(0, 0)); |
| 93 |
vec4 vL = textureLodOffset(source_ssil, vec2(p_coord), 0.0, ivec2(-1, 0)); |
| 94 |
vec4 vT = textureLodOffset(source_ssil, vec2(p_coord), 0.0, ivec2(0, -1)); |
| 95 |
vec4 vR = textureLodOffset(source_ssil, vec2(p_coord), 0.0, ivec2(1, 0)); |
| 96 |
vec4 vB = textureLodOffset(source_ssil, vec2(p_coord), 0.0, ivec2(0, 1)); |
| 97 |
|
| 98 |
float packed_edges = imageLoad(source_edges, p_pos).r; |
| 99 |
vec4 edgesLRTB = unpack_edges(packed_edges); |
| 100 |
|
| 101 |
float sum_weight = 0.5; |
| 102 |
vec4 sum = vC * sum_weight; |
| 103 |
|
| 104 |
add_sample(vL, edgesLRTB.x, sum, sum_weight); |
| 105 |
add_sample(vR, edgesLRTB.y, sum, sum_weight); |
| 106 |
add_sample(vT, edgesLRTB.z, sum, sum_weight); |
| 107 |
add_sample(vB, edgesLRTB.w, sum, sum_weight); |
| 108 |
|
| 109 |
vec4 ssil_avg = sum / sum_weight; |
| 110 |
|
| 111 |
vec4 ssil_value = ssil_avg; |
| 112 |
|
| 113 |
return ssil_value; |
| 114 |
} |
| 115 |
#endif |
| 116 |
|
| 117 |
void main() { |
| 118 |
// Pixel being shaded |
| 119 |
ivec2 ssC = ivec2(gl_GlobalInvocationID.xy); |
| 120 |
|
| 121 |
#ifdef MODE_NON_SMART |
| 122 |
|
| 123 |
vec2 half_pixel = params.half_screen_pixel_size * 0.5; |
| 124 |
|
| 125 |
vec2 uv = (vec2(gl_GlobalInvocationID.xy) + vec2(0.5, 0.5)) * params.half_screen_pixel_size; |
| 126 |
|
| 127 |
vec4 center = textureLod(source_ssil, uv, 0.0); |
| 128 |
|
| 129 |
vec4 value = textureLod(source_ssil, vec2(uv + vec2(-half_pixel.x * 3, -half_pixel.y)), 0.0) * 0.2; |
| 130 |
value += textureLod(source_ssil, vec2(uv + vec2(+half_pixel.x, -half_pixel.y * 3)), 0.0) * 0.2; |
| 131 |
value += textureLod(source_ssil, vec2(uv + vec2(-half_pixel.x, +half_pixel.y * 3)), 0.0) * 0.2; |
| 132 |
value += textureLod(source_ssil, vec2(uv + vec2(+half_pixel.x * 3, +half_pixel.y)), 0.0) * 0.2; |
| 133 |
|
| 134 |
vec4 sampled = value + center * 0.2; |
| 135 |
|
| 136 |
#else |
| 137 |
#ifdef MODE_SMART |
| 138 |
vec4 sampled = sample_blurred(ssC, (vec2(gl_GlobalInvocationID.xy) + vec2(0.5, 0.5)) * params.half_screen_pixel_size); |
| 139 |
#else // MODE_WIDE |
| 140 |
vec4 sampled = sample_blurred_wide(ssC, (vec2(gl_GlobalInvocationID.xy) + vec2(0.5, 0.5)) * params.half_screen_pixel_size); |
| 141 |
#endif |
| 142 |
#endif // MODE_NON_SMART |
| 143 |
imageStore(dest_image, ssC, sampled); |
| 144 |
} |