|
| 1 | +float srgb_linear_to_nonlinear_channel(float u) |
| 2 | +{ |
| 3 | + return (u <= 0.0031308) ? (12.92 * u) : ((1.055 * pow(u, 1. / 2.4)) - 0.055); |
| 4 | +} |
| 5 | + |
| 6 | +float3 srgb_linear_to_nonlinear(float3 v) |
| 7 | +{ |
| 8 | + return float3(srgb_linear_to_nonlinear_channel(v.r), srgb_linear_to_nonlinear_channel(v.g), srgb_linear_to_nonlinear_channel(v.b)); |
| 9 | +} |
| 10 | + |
| 11 | +float srgb_nonlinear_to_linear_channel(float u) |
| 12 | +{ |
| 13 | + return (u <= 0.04045) ? (u / 12.92) : pow((u + 0.055) / 1.055, 2.4); |
| 14 | +} |
| 15 | + |
| 16 | +float3 srgb_nonlinear_to_linear(float3 v) |
| 17 | +{ |
| 18 | + return float3(srgb_nonlinear_to_linear_channel(v.r), srgb_nonlinear_to_linear_channel(v.g), srgb_nonlinear_to_linear_channel(v.b)); |
| 19 | +} |
| 20 | + |
| 21 | +float3 rec709_to_rec2020(float3 v) |
| 22 | +{ |
| 23 | + float r = dot(v, float3(0.62740389593469903, 0.32928303837788370, 0.043313065687417225)); |
| 24 | + float g = dot(v, float3(0.069097289358232075, 0.91954039507545871, 0.011362315566309178)); |
| 25 | + float b = dot(v, float3(0.016391438875150280, 0.088013307877225749, 0.89559525324762401)); |
| 26 | + return float3(r, g, b); |
| 27 | +} |
| 28 | + |
| 29 | +float3 rec2020_to_rec709(float3 v) |
| 30 | +{ |
| 31 | + float r = dot(v, float3(1.6604910021084345, -0.58764113878854951, -0.072849863319884883)); |
| 32 | + float g = dot(v, float3(-0.12455047452159074, 1.1328998971259603, -0.0083494226043694768)); |
| 33 | + float b = dot(v, float3(-0.018150763354905303, -0.10057889800800739, 1.1187296613629127)); |
| 34 | + return float3(r, g, b); |
| 35 | +} |
| 36 | + |
| 37 | +float3 reinhard(float3 rgb) |
| 38 | +{ |
| 39 | + rgb /= rgb + float3(1., 1., 1.); |
| 40 | + rgb = saturate(rgb); |
| 41 | + rgb = pow(rgb, float3(1. / 2.4, 1. / 2.4, 1. / 2.4)); |
| 42 | + rgb = srgb_nonlinear_to_linear(rgb); |
| 43 | + return rgb; |
| 44 | +} |
| 45 | + |
| 46 | +float linear_to_st2084_channel(float x) |
| 47 | +{ |
| 48 | + float c = pow(abs(x), 0.1593017578); |
| 49 | + return pow((0.8359375 + 18.8515625 * c) / (1. + 18.6875 * c), 78.84375); |
| 50 | +} |
| 51 | + |
| 52 | +float st2084_to_linear_channel(float u) |
| 53 | +{ |
| 54 | + float c = pow(abs(u), 1. / 78.84375); |
| 55 | + return pow(abs(max(c - 0.8359375, 0.) / (18.8515625 - 18.6875 * c)), 1. / 0.1593017578); |
| 56 | +} |
| 57 | + |
| 58 | +float eetf_0_Lmax(float maxRGB1_pq, float Lw, float Lmax) |
| 59 | +{ |
| 60 | + float Lw_pq = linear_to_st2084_channel(Lw / 10000.); |
| 61 | + float E1 = saturate(maxRGB1_pq / Lw_pq); // Ensure normalization in case Lw is a lie |
| 62 | + float maxLum = linear_to_st2084_channel(Lmax / 10000.) / Lw_pq; |
| 63 | + float KS = (1.5 * maxLum) - 0.5; |
| 64 | + float E2 = E1; |
| 65 | + if (E1 > KS) |
| 66 | + { |
| 67 | + float T = (E1 - KS) / (1. - KS); |
| 68 | + float Tsquared = T * T; |
| 69 | + float Tcubed = Tsquared * T; |
| 70 | + float P = (2. * Tcubed - 3. * Tsquared + 1.) * KS + (Tcubed - 2. * Tsquared + T) * (1. - KS) + (-2. * Tcubed + 3. * Tsquared) * maxLum; |
| 71 | + E2 = P; |
| 72 | + } |
| 73 | + float E3 = E2; |
| 74 | + float E4 = E3 * Lw_pq; |
| 75 | + return E4; |
| 76 | +} |
| 77 | + |
| 78 | +float3 maxRGB_eetf_internal(float3 rgb_linear, float maxRGB1_linear, float maxRGB1_pq, float Lw, float Lmax) |
| 79 | +{ |
| 80 | + float maxRGB2_pq = eetf_0_Lmax(maxRGB1_pq, Lw, Lmax); |
| 81 | + float maxRGB2_linear = st2084_to_linear_channel(maxRGB2_pq); |
| 82 | + |
| 83 | + // avoid divide-by-zero possibility |
| 84 | + maxRGB1_linear = max(6.10352e-5, maxRGB1_linear); |
| 85 | + |
| 86 | + rgb_linear *= maxRGB2_linear / maxRGB1_linear; |
| 87 | + return rgb_linear; |
| 88 | +} |
| 89 | + |
| 90 | +float3 maxRGB_eetf_linear_to_linear(float3 rgb_linear, float Lw, float Lmax) |
| 91 | +{ |
| 92 | + float maxRGB1_linear = max(max(rgb_linear.r, rgb_linear.g), rgb_linear.b); |
| 93 | + float maxRGB1_pq = linear_to_st2084_channel(maxRGB1_linear); |
| 94 | + return maxRGB_eetf_internal(rgb_linear, maxRGB1_linear, maxRGB1_pq, Lw, Lmax); |
| 95 | +} |
0 commit comments