Last active
June 2, 2022 07:55
-
-
Save Akira-Hayasaka/c7858a4d7e60e86e9ec5448d2224d632 to your computer and use it in GitHub Desktop.
hdr.frag
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#version 150 | |
uniform sampler2DRect tex; | |
uniform float r_for_hlg; | |
in vec2 texCoordVarying; | |
out vec4 outputColor; | |
// HLG OETF | |
vec3 hlg_oetf(vec3 E) | |
{ | |
const float a = 0.17883277; | |
const float b = 1.0 - 4.0 * a; | |
const float c = 0.5 - a * log(4.0 * a); | |
const float r = 0.5; | |
const float one_over_12 = 1.0 / 12.0; | |
float r_prime = 0.0; | |
float g_prime = 0.0; | |
float b_prime = 0.0; | |
if (0.0 <= E.r && E.r <= one_over_12) | |
r_prime = sqrt(3.0 * E.r); | |
else if (one_over_12 < E.r && E.r <= 1.0) | |
r_prime = a * log(12.0 * E.r - b) + c; | |
if (0.0 <= E.g && E.g <= one_over_12) | |
g_prime = sqrt(3.0 * E.g); | |
else if (one_over_12 < E.g && E.g <= 1.0) | |
g_prime = a * log(12.0 * E.g - b) + c; | |
if (0.0 <= E.b && E.b <= one_over_12) | |
b_prime = sqrt(3.0 * E.b); | |
else if (one_over_12 < E.b && E.b <= 1.0) | |
b_prime = a * log(12.0 * E.b - b) + c; | |
return vec3(r_prime, g_prime, b_prime); | |
} | |
// HLG inverse OOTF | |
vec3 hlg_iootf(vec3 rgb_d) | |
{ | |
const float gamma = 1.2734; | |
const float alpha = 1500.0; | |
float y_d = 0.2627 * rgb_d.r + 0.678 * rgb_d.g + 0.0593 * rgb_d.b; | |
float tmp = pow(y_d / alpha, (1.0 - gamma) / gamma); | |
float r_s = tmp * (rgb_d.r / alpha); | |
float g_s = tmp * (rgb_d.g / alpha); | |
float b_s = tmp * (rgb_d.b / alpha); | |
return vec3(r_s, g_s, b_s); | |
} | |
// HLG inverse EOTF | |
vec3 hlg_ieotf(vec3 rgb) | |
{ | |
return hlg_oetf(hlg_iootf(rgb)); | |
} | |
void main() | |
{ | |
vec2 tc = texCoordVarying.st; | |
vec4 sdr_0_1 = texture(tex, tc); // 0.0 - 1.0 | |
// conv hlg | |
vec3 hlg_col = hlg_ieotf(sdr_0_1.rgb); | |
outputColor = vec4(hlg_col, sdr_0_1.a); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment