• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1const float epsilon = 0.0001;
2const vec3 lumConv = vec3(0.27, 0.67, 0.06);
3
4float HDR_GetLum(in vec3 color){
5    return dot(color, lumConv);
6}
7
8vec4 HDR_EncodeLum(in float lum){
9    float Le = 2.0 * log2(lum + epsilon) + 127.0;
10    vec4 result = vec4(0.0);
11    result.a = fract(Le);
12    result.rgb = vec3((Le - (floor(result.a * 255.0)) / 255.0) / 255.0);
13    return result;
14}
15
16float HDR_DecodeLum(in vec4 logLum){
17    float Le = logLum.r * 255.0 + logLum.a;
18    return exp2((Le - 127.0) / 2.0);
19}
20
21const mat3 rgbToXyz = mat3(
22    0.2209, 0.3390, 0.4184,
23    0.1138, 0.6780, 0.7319,
24    0.0102, 0.1130, 0.2969);
25
26const mat3 xyzToRgb = mat3(
27    6.0013,    -2.700,    -1.7995,
28    -1.332,    3.1029,    -5.7720,
29    .3007,    -1.088,    5.6268);
30
31vec4 HDR_LogLuvEncode(in vec3 rgb){
32    vec4 result;
33    vec3 Xp_Y_XYZp = rgb * rgbToXyz;
34    Xp_Y_XYZp = max(Xp_Y_XYZp, vec3(1e-6, 1e-6, 1e-6));
35    result.xy = Xp_Y_XYZp.xy / Xp_Y_XYZp.z;
36    float Le = 2.0 * log2(Xp_Y_XYZp.y) + 127.0;
37    result.w = fract(Le);
38    result.z = (Le - (floor(result.w * 255.0)) / 255.0) / 255.0;
39    return result;
40}
41
42vec3 HDR_LogLuvDecode(in vec4 logLuv){
43    float Le = logLuv.z * 255.0 + logLuv.w;
44    vec3 Xp_Y_XYZp;
45    Xp_Y_XYZp.y = exp2((Le - 127.0) / 2.0);
46    Xp_Y_XYZp.z = Xp_Y_XYZp.y / logLuv.y;
47    Xp_Y_XYZp.x = logLuv.x * Xp_Y_XYZp.z;
48    vec3 rgb = Xp_Y_XYZp * xyzToRgb;
49    return max(rgb, 0.0);
50}
51
52vec3 HDR_ToneMap(in vec3 color, in float lumAvg, in float a, in float white){
53    white *= white;
54    float lumHDR = HDR_GetLum(color);
55    float L = (a / lumAvg) * lumHDR;
56    float Ld = 1.0 + (L / white);
57    Ld = (Ld * L) / (1.0 + L);
58    return (color / lumHDR) * Ld;
59    //return color * vec3(Ld);
60}
61
62vec3 HDR_ToneMap2(in vec3 color, in float lumAvg, in float a, in float white){
63    float scale = a / (lumAvg + 0.001);
64    return (vec3(scale) * color) / (color + vec3(1.0));
65}