| 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 |
layout(set = 0, binding = 0) uniform sampler2D source_normal; |
| 10 |
layout(r8, set = 1, binding = 0) uniform restrict writeonly image2D dest_roughness; |
| 11 |
|
| 12 |
layout(push_constant, std430) uniform Params { |
| 13 |
ivec2 screen_size; |
| 14 |
float curve; |
| 15 |
uint pad; |
| 16 |
} |
| 17 |
params; |
| 18 |
|
| 19 |
#define HALF_PI 1.5707963267948966 |
| 20 |
|
| 21 |
void main() { |
| 22 |
// Pixel being shaded |
| 23 |
ivec2 pos = ivec2(gl_GlobalInvocationID.xy); |
| 24 |
if (any(greaterThan(pos, params.screen_size))) { //too large, do nothing |
| 25 |
return; |
| 26 |
} |
| 27 |
|
| 28 |
vec3 normal_accum = vec3(0.0); |
| 29 |
float accum = 0.0; |
| 30 |
for (int i = 0; i <= 1; i++) { |
| 31 |
for (int j = 0; j <= 1; j++) { |
| 32 |
normal_accum += normalize(texelFetch(source_normal, pos + ivec2(i, j), 0).xyz * 2.0 - 1.0); |
| 33 |
accum += 1.0; |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
normal_accum /= accum; |
| 38 |
|
| 39 |
float r = length(normal_accum); |
| 40 |
|
| 41 |
float limit; |
| 42 |
|
| 43 |
if (r < 1.0) { |
| 44 |
float threshold = 0.4; |
| 45 |
|
| 46 |
/* |
| 47 |
//Formula from Filament, does not make sense to me. |
| 48 |
|
| 49 |
float r2 = r * r; |
| 50 |
float kappa = (3.0f * r - r * r2) / (1.0f - r2); |
| 51 |
float variance = 0.25f / kappa; |
| 52 |
limit = sqrt(min(2.0f * variance, threshold * threshold)); |
| 53 |
*/ |
| 54 |
/* |
| 55 |
//Formula based on probability distribution graph |
| 56 |
|
| 57 |
float width = acos(max(0.0,r)); // convert to angle (width) |
| 58 |
float roughness = pow(width,1.7)*0.854492; //approximate (crappy) formula to convert to roughness |
| 59 |
limit = min(sqrt(roughness), threshold); //convert to perceptual roughness and apply threshold |
| 60 |
*/ |
| 61 |
|
| 62 |
limit = min(sqrt(pow(acos(max(0.0, r)) / HALF_PI, params.curve)), threshold); //convert to perceptual roughness and apply threshold |
| 63 |
|
| 64 |
//limit = 0.5; |
| 65 |
} else { |
| 66 |
limit = 0.0; |
| 67 |
} |
| 68 |
|
| 69 |
imageStore(dest_roughness, pos, vec4(limit)); |
| 70 |
} |