| 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 |
#ifdef GENERATE_MAP |
| 30 |
layout(set = 0, binding = 0) uniform sampler2DArray source_texture; |
| 31 |
#else |
| 32 |
layout(set = 0, binding = 0) uniform sampler2D source_importance; |
| 33 |
#endif |
| 34 |
layout(r8, set = 1, binding = 0) uniform restrict writeonly image2D dest_image; |
| 35 |
|
| 36 |
#ifdef PROCESS_MAPB |
| 37 |
layout(set = 2, binding = 0, std430) buffer Counter { |
| 38 |
uint sum; |
| 39 |
} |
| 40 |
counter; |
| 41 |
#endif |
| 42 |
|
| 43 |
layout(push_constant, std430) uniform Params { |
| 44 |
vec2 half_screen_pixel_size; |
| 45 |
float intensity; |
| 46 |
float pad; |
| 47 |
} |
| 48 |
params; |
| 49 |
|
| 50 |
void main() { |
| 51 |
// Pixel being shaded |
| 52 |
ivec2 ssC = ivec2(gl_GlobalInvocationID.xy); |
| 53 |
|
| 54 |
#ifdef GENERATE_MAP |
| 55 |
// importance map stuff |
| 56 |
uvec2 base_position = ssC * 2; |
| 57 |
|
| 58 |
float avg = 0.0; |
| 59 |
float minV = 1.0; |
| 60 |
float maxV = 0.0; |
| 61 |
for (int i = 0; i < 4; i++) { |
| 62 |
vec3 value_a = texelFetch(source_texture, ivec3(base_position, i), 0).rgb * params.intensity; |
| 63 |
vec3 value_b = texelFetch(source_texture, ivec3(base_position, i) + ivec3(0, 1, 0), 0).rgb * params.intensity; |
| 64 |
vec3 value_c = texelFetch(source_texture, ivec3(base_position, i) + ivec3(1, 0, 0), 0).rgb * params.intensity; |
| 65 |
vec3 value_d = texelFetch(source_texture, ivec3(base_position, i) + ivec3(1, 1, 0), 0).rgb * params.intensity; |
| 66 |
|
| 67 |
// Calculate luminance (black and white value) |
| 68 |
float a = dot(value_a, vec3(0.2125, 0.7154, 0.0721)); |
| 69 |
float b = dot(value_b, vec3(0.2125, 0.7154, 0.0721)); |
| 70 |
float c = dot(value_c, vec3(0.2125, 0.7154, 0.0721)); |
| 71 |
float d = dot(value_d, vec3(0.2125, 0.7154, 0.0721)); |
| 72 |
|
| 73 |
maxV = max(maxV, max(max(a, b), max(c, d))); |
| 74 |
minV = min(minV, min(min(a, b), min(c, d))); |
| 75 |
} |
| 76 |
|
| 77 |
float min_max_diff = maxV - minV; |
| 78 |
|
| 79 |
imageStore(dest_image, ssC, vec4(pow(clamp(min_max_diff * 2.0, 0.0, 1.0), 0.6))); |
| 80 |
#endif |
| 81 |
|
| 82 |
#ifdef PROCESS_MAPA |
| 83 |
vec2 uv = (vec2(ssC) + 0.5) * params.half_screen_pixel_size * 2.0; |
| 84 |
|
| 85 |
float center = textureLod(source_importance, uv, 0.0).x; |
| 86 |
|
| 87 |
vec2 half_pixel = params.half_screen_pixel_size; |
| 88 |
|
| 89 |
vec4 vals; |
| 90 |
vals.x = textureLod(source_importance, uv + vec2(-half_pixel.x * 3, -half_pixel.y), 0.0).x; |
| 91 |
vals.y = textureLod(source_importance, uv + vec2(+half_pixel.x, -half_pixel.y * 3), 0.0).x; |
| 92 |
vals.z = textureLod(source_importance, uv + vec2(+half_pixel.x * 3, +half_pixel.y), 0.0).x; |
| 93 |
vals.w = textureLod(source_importance, uv + vec2(-half_pixel.x, +half_pixel.y * 3), 0.0).x; |
| 94 |
|
| 95 |
float avg = dot(vals, vec4(0.25, 0.25, 0.25, 0.25)); |
| 96 |
|
| 97 |
imageStore(dest_image, ssC, vec4(avg)); |
| 98 |
#endif |
| 99 |
|
| 100 |
#ifdef PROCESS_MAPB |
| 101 |
vec2 uv = (vec2(ssC) + 0.5f) * params.half_screen_pixel_size * 2.0; |
| 102 |
|
| 103 |
float center = textureLod(source_importance, uv, 0.0).x; |
| 104 |
|
| 105 |
vec2 half_pixel = params.half_screen_pixel_size; |
| 106 |
|
| 107 |
vec4 vals; |
| 108 |
vals.x = textureLod(source_importance, uv + vec2(-half_pixel.x, -half_pixel.y * 3), 0.0).x; |
| 109 |
vals.y = textureLod(source_importance, uv + vec2(+half_pixel.x * 3, -half_pixel.y), 0.0).x; |
| 110 |
vals.z = textureLod(source_importance, uv + vec2(+half_pixel.x, +half_pixel.y * 3), 0.0).x; |
| 111 |
vals.w = textureLod(source_importance, uv + vec2(-half_pixel.x * 3, +half_pixel.y), 0.0).x; |
| 112 |
|
| 113 |
float avg = dot(vals, vec4(0.25, 0.25, 0.25, 0.25)); |
| 114 |
|
| 115 |
imageStore(dest_image, ssC, vec4(avg)); |
| 116 |
|
| 117 |
// sum the average; to avoid overflowing we assume max AO resolution is not bigger than 16384x16384; so quarter res (used here) will be 4096x4096, which leaves us with 8 bits per pixel |
| 118 |
uint sum = uint(clamp(avg, 0.0, 1.0) * 255.0 + 0.5); |
| 119 |
|
| 120 |
// save every 9th to avoid InterlockedAdd congestion - since we're blurring, this is good enough; compensated by multiplying load_counter_avg_div by 9 |
| 121 |
if (((ssC.x % 3) + (ssC.y % 3)) == 0) { |
| 122 |
atomicAdd(counter.sum, sum); |
| 123 |
} |
| 124 |
#endif |
| 125 |
} |