| 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(r8, set = 0, binding = 0) uniform restrict writeonly image2D current_image; |
| 10 |
|
| 11 |
// This shader is used to generate a "best fit normal texture" as described by: |
| 12 |
// https://advances.realtimerendering.com/s2010/Kaplanyan-CryEngine3(SIGGRAPH%202010%20Advanced%20RealTime%20Rendering%20Course).pdf |
| 13 |
// This texture tells you what length of normal can be used to store a unit vector |
| 14 |
// with the lest amount of error. |
| 15 |
|
| 16 |
vec3 quantize(vec3 c) { |
| 17 |
return round(clamp(c * 0.5 + 0.5, 0.0, 1.0) * 255.0) * (1.0 / 255.0) * 2.0 - 1.0; |
| 18 |
} |
| 19 |
|
| 20 |
float find_minimum_error(vec3 normal) { |
| 21 |
float min_error = 100000.0; |
| 22 |
float t_best = 0.0; |
| 23 |
for (float nstep = 1.5; nstep < 127.5; ++nstep) { |
| 24 |
float t = nstep / 127.5; |
| 25 |
vec3 vp = normal * t; |
| 26 |
vec3 quantizedp = quantize(vp); |
| 27 |
vec3 vdiff = (quantizedp - vp) / t; |
| 28 |
float error = max(abs(vdiff.x), max(abs(vdiff.y), abs(vdiff.z))); |
| 29 |
if (error < min_error) { |
| 30 |
min_error = error; |
| 31 |
t_best = t; |
| 32 |
} |
| 33 |
} |
| 34 |
return t_best; |
| 35 |
} |
| 36 |
|
| 37 |
void main() { |
| 38 |
vec2 uv = vec2(gl_GlobalInvocationID.xy) * vec2(1.0 / 1024.0) + vec2(0.5 / 1024.0); |
| 39 |
uv.y *= uv.x; |
| 40 |
|
| 41 |
vec3 dir = vec3(uv.x, uv.y, 1.0); |
| 42 |
imageStore(current_image, ivec2(gl_GlobalInvocationID.xy), vec4(find_minimum_error(dir), 1.0, 1.0, 1.0)); |
| 43 |
} |