| 1 |
#[compute] |
| 2 |
|
| 3 |
#version 450 |
| 4 |
|
| 5 |
#VERSION_DEFINES |
| 6 |
|
| 7 |
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in; |
| 8 |
|
| 9 |
#ifdef MODE_RESOLVE_DEPTH |
| 10 |
layout(set = 0, binding = 0) uniform sampler2DMS source_depth; |
| 11 |
layout(r32f, set = 1, binding = 0) uniform restrict writeonly image2D dest_depth; |
| 12 |
#endif |
| 13 |
|
| 14 |
#ifdef MODE_RESOLVE_GI |
| 15 |
layout(set = 0, binding = 0) uniform sampler2DMS source_depth; |
| 16 |
layout(set = 0, binding = 1) uniform sampler2DMS source_normal_roughness; |
| 17 |
|
| 18 |
layout(r32f, set = 1, binding = 0) uniform restrict writeonly image2D dest_depth; |
| 19 |
layout(rgba8, set = 1, binding = 1) uniform restrict writeonly image2D dest_normal_roughness; |
| 20 |
|
| 21 |
#ifdef VOXEL_GI_RESOLVE |
| 22 |
layout(set = 2, binding = 0) uniform usampler2DMS source_voxel_gi; |
| 23 |
layout(rg8ui, set = 3, binding = 0) uniform restrict writeonly uimage2D dest_voxel_gi; |
| 24 |
#endif |
| 25 |
|
| 26 |
#endif |
| 27 |
|
| 28 |
layout(push_constant, std430) uniform Params { |
| 29 |
ivec2 screen_size; |
| 30 |
int sample_count; |
| 31 |
uint pad; |
| 32 |
} |
| 33 |
params; |
| 34 |
|
| 35 |
void main() { |
| 36 |
// Pixel being shaded |
| 37 |
ivec2 pos = ivec2(gl_GlobalInvocationID.xy); |
| 38 |
if (any(greaterThanEqual(pos, params.screen_size))) { //too large, do nothing |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
#ifdef MODE_RESOLVE_DEPTH |
| 43 |
|
| 44 |
float depth_avg = 0.0; |
| 45 |
for (int i = 0; i < params.sample_count; i++) { |
| 46 |
depth_avg += texelFetch(source_depth, pos, i).r; |
| 47 |
} |
| 48 |
depth_avg /= float(params.sample_count); |
| 49 |
imageStore(dest_depth, pos, vec4(depth_avg)); |
| 50 |
|
| 51 |
#endif |
| 52 |
|
| 53 |
#ifdef MODE_RESOLVE_GI |
| 54 |
|
| 55 |
float best_depth = 1e20; |
| 56 |
vec4 best_normal_roughness = vec4(0.0); |
| 57 |
#ifdef VOXEL_GI_RESOLVE |
| 58 |
uvec2 best_voxel_gi; |
| 59 |
#endif |
| 60 |
|
| 61 |
#if 0 |
| 62 |
|
| 63 |
for(int i=0;i<params.sample_count;i++) { |
| 64 |
float depth = texelFetch(source_depth,pos,i).r; |
| 65 |
if (depth < best_depth) { //use the depth closest to camera |
| 66 |
best_depth = depth; |
| 67 |
best_normal_roughness = texelFetch(source_normal_roughness,pos,i); |
| 68 |
|
| 69 |
#ifdef VOXEL_GI_RESOLVE |
| 70 |
best_voxel_gi = texelFetch(source_voxel_gi,pos,i).rg; |
| 71 |
#endif |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
#else |
| 76 |
|
| 77 |
#if 1 |
| 78 |
|
| 79 |
vec4 group1; |
| 80 |
vec4 group2; |
| 81 |
vec4 group3; |
| 82 |
vec4 group4; |
| 83 |
int best_index = 0; |
| 84 |
|
| 85 |
//2X |
| 86 |
group1.x = texelFetch(source_depth, pos, 0).r; |
| 87 |
group1.y = texelFetch(source_depth, pos, 1).r; |
| 88 |
|
| 89 |
//4X |
| 90 |
if (params.sample_count >= 4) { |
| 91 |
group1.z = texelFetch(source_depth, pos, 2).r; |
| 92 |
group1.w = texelFetch(source_depth, pos, 3).r; |
| 93 |
} |
| 94 |
//8X |
| 95 |
if (params.sample_count >= 8) { |
| 96 |
group2.x = texelFetch(source_depth, pos, 4).r; |
| 97 |
group2.y = texelFetch(source_depth, pos, 5).r; |
| 98 |
group2.z = texelFetch(source_depth, pos, 6).r; |
| 99 |
group2.w = texelFetch(source_depth, pos, 7).r; |
| 100 |
} |
| 101 |
//16X |
| 102 |
if (params.sample_count >= 16) { |
| 103 |
group3.x = texelFetch(source_depth, pos, 8).r; |
| 104 |
group3.y = texelFetch(source_depth, pos, 9).r; |
| 105 |
group3.z = texelFetch(source_depth, pos, 10).r; |
| 106 |
group3.w = texelFetch(source_depth, pos, 11).r; |
| 107 |
|
| 108 |
group4.x = texelFetch(source_depth, pos, 12).r; |
| 109 |
group4.y = texelFetch(source_depth, pos, 13).r; |
| 110 |
group4.z = texelFetch(source_depth, pos, 14).r; |
| 111 |
group4.w = texelFetch(source_depth, pos, 15).r; |
| 112 |
} |
| 113 |
|
| 114 |
if (params.sample_count == 2) { |
| 115 |
best_index = (group1.x < group1.y) ? 1 : 0; // Not much can be done here. Pick the sample closer to the camera. |
| 116 |
} else if (params.sample_count == 4) { |
| 117 |
vec4 freq = vec4(equal(group1, vec4(group1.x))); |
| 118 |
freq += vec4(equal(group1, vec4(group1.y))); |
| 119 |
freq += vec4(equal(group1, vec4(group1.z))); |
| 120 |
freq += vec4(equal(group1, vec4(group1.w))); |
| 121 |
|
| 122 |
float min_f = freq.x; |
| 123 |
best_index = 0; |
| 124 |
if (freq.y < min_f) { |
| 125 |
best_index = 1; |
| 126 |
min_f = freq.y; |
| 127 |
} |
| 128 |
if (freq.z < min_f) { |
| 129 |
best_index = 2; |
| 130 |
min_f = freq.z; |
| 131 |
} |
| 132 |
if (freq.w < min_f) { |
| 133 |
best_index = 3; |
| 134 |
} |
| 135 |
} else if (params.sample_count == 8) { |
| 136 |
vec4 freq0 = vec4(equal(group1, vec4(group1.x))); |
| 137 |
vec4 freq1 = vec4(equal(group2, vec4(group1.x))); |
| 138 |
freq0 += vec4(equal(group1, vec4(group1.y))); |
| 139 |
freq1 += vec4(equal(group2, vec4(group1.y))); |
| 140 |
freq0 += vec4(equal(group1, vec4(group1.z))); |
| 141 |
freq1 += vec4(equal(group2, vec4(group1.z))); |
| 142 |
freq0 += vec4(equal(group1, vec4(group1.w))); |
| 143 |
freq1 += vec4(equal(group2, vec4(group1.w))); |
| 144 |
freq0 += vec4(equal(group1, vec4(group2.x))); |
| 145 |
freq1 += vec4(equal(group2, vec4(group2.x))); |
| 146 |
freq0 += vec4(equal(group1, vec4(group2.y))); |
| 147 |
freq1 += vec4(equal(group2, vec4(group2.y))); |
| 148 |
freq0 += vec4(equal(group1, vec4(group2.z))); |
| 149 |
freq1 += vec4(equal(group2, vec4(group2.z))); |
| 150 |
freq0 += vec4(equal(group1, vec4(group2.w))); |
| 151 |
freq1 += vec4(equal(group2, vec4(group2.w))); |
| 152 |
|
| 153 |
float min_f0 = freq0.x; |
| 154 |
int best_index0 = 0; |
| 155 |
if (freq0.y < min_f0) { |
| 156 |
best_index0 = 1; |
| 157 |
min_f0 = freq0.y; |
| 158 |
} |
| 159 |
if (freq0.z < min_f0) { |
| 160 |
best_index0 = 2; |
| 161 |
min_f0 = freq0.z; |
| 162 |
} |
| 163 |
if (freq0.w < min_f0) { |
| 164 |
best_index0 = 3; |
| 165 |
min_f0 = freq0.w; |
| 166 |
} |
| 167 |
|
| 168 |
float min_f1 = freq1.x; |
| 169 |
int best_index1 = 4; |
| 170 |
if (freq1.y < min_f1) { |
| 171 |
best_index1 = 5; |
| 172 |
min_f1 = freq1.y; |
| 173 |
} |
| 174 |
if (freq1.z < min_f1) { |
| 175 |
best_index1 = 6; |
| 176 |
min_f1 = freq1.z; |
| 177 |
} |
| 178 |
if (freq1.w < min_f1) { |
| 179 |
best_index1 = 7; |
| 180 |
min_f1 = freq1.w; |
| 181 |
} |
| 182 |
|
| 183 |
best_index = mix(best_index0, best_index1, min_f0 < min_f1); |
| 184 |
} |
| 185 |
|
| 186 |
#else |
| 187 |
float depths[16]; |
| 188 |
int depth_indices[16]; |
| 189 |
int depth_amount[16]; |
| 190 |
int depth_count = 0; |
| 191 |
|
| 192 |
for (int i = 0; i < params.sample_count; i++) { |
| 193 |
float depth = texelFetch(source_depth, pos, i).r; |
| 194 |
int depth_index = -1; |
| 195 |
for (int j = 0; j < depth_count; j++) { |
| 196 |
if (abs(depths[j] - depth) < 0.000001) { |
| 197 |
depth_index = j; |
| 198 |
break; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
if (depth_index == -1) { |
| 203 |
depths[depth_count] = depth; |
| 204 |
depth_indices[depth_count] = i; |
| 205 |
depth_amount[depth_count] = 1; |
| 206 |
depth_count += 1; |
| 207 |
} else { |
| 208 |
depth_amount[depth_index] += 1; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
int depth_least = 0xFFFF; |
| 213 |
int best_index = 0; |
| 214 |
for (int j = 0; j < depth_count; j++) { |
| 215 |
if (depth_amount[j] < depth_least) { |
| 216 |
best_index = depth_indices[j]; |
| 217 |
depth_least = depth_amount[j]; |
| 218 |
} |
| 219 |
} |
| 220 |
#endif |
| 221 |
best_depth = texelFetch(source_depth, pos, best_index).r; |
| 222 |
best_normal_roughness = texelFetch(source_normal_roughness, pos, best_index); |
| 223 |
#ifdef VOXEL_GI_RESOLVE |
| 224 |
best_voxel_gi = texelFetch(source_voxel_gi, pos, best_index).rg; |
| 225 |
#endif |
| 226 |
|
| 227 |
#endif |
| 228 |
|
| 229 |
imageStore(dest_depth, pos, vec4(best_depth)); |
| 230 |
imageStore(dest_normal_roughness, pos, vec4(best_normal_roughness)); |
| 231 |
#ifdef VOXEL_GI_RESOLVE |
| 232 |
imageStore(dest_voxel_gi, pos, uvec4(best_voxel_gi, 0, 0)); |
| 233 |
#endif |
| 234 |
|
| 235 |
#endif |
| 236 |
} |