| 1 |
#version 440 |
| 2 |
|
| 3 |
layout(location = 0) in vec2 v_texcoord; |
| 4 |
|
| 5 |
layout(location = 0) out vec4 fragColor; |
| 6 |
|
| 7 |
layout(std140, binding = 0) uniform buf { |
| 8 |
mat4 mvp; |
| 9 |
int flip; |
| 10 |
int sdr; |
| 11 |
float tonemapInMax; |
| 12 |
float tonemapOutMax; |
| 13 |
} ubuf; |
| 14 |
|
| 15 |
layout(binding = 1) uniform sampler2D tex; |
| 16 |
|
| 17 |
vec3 linearToSRGB(vec3 color) |
| 18 |
{ |
| 19 |
vec3 S1 = sqrt(color); |
| 20 |
vec3 S2 = sqrt(S1); |
| 21 |
vec3 S3 = sqrt(S2); |
| 22 |
return 0.585122381 * S1 + 0.783140355 * S2 - 0.368262736 * S3; |
| 23 |
} |
| 24 |
|
| 25 |
// https://learn.microsoft.com/en-us/windows/win32/direct3darticles/high-dynamic-range |
| 26 |
vec3 simpleReinhardTonemapper(vec3 c, float inMax, float outMax) |
| 27 |
{ |
| 28 |
c /= vec3(inMax); |
| 29 |
c.r = c.r / (1 + c.r); |
| 30 |
c.g = c.g / (1 + c.g); |
| 31 |
c.b = c.b / (1 + c.b); |
| 32 |
c *= vec3(outMax); |
| 33 |
return c; |
| 34 |
} |
| 35 |
|
| 36 |
void main() |
| 37 |
{ |
| 38 |
vec4 c = texture(tex, v_texcoord); |
| 39 |
if (ubuf.tonemapInMax != 0) |
| 40 |
c.rgb = simpleReinhardTonemapper(c.rgb, ubuf.tonemapInMax, ubuf.tonemapOutMax); |
| 41 |
else if (ubuf.sdr != 0) |
| 42 |
c.rgb = linearToSRGB(c.rgb); |
| 43 |
fragColor = vec4(c.rgb * c.a, c.a); |
| 44 |
} |