color.effect 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. float3 srgb_linear_to_nonlinear(float3 v)
  6. {
  7. return float3(srgb_linear_to_nonlinear_channel(v.r), srgb_linear_to_nonlinear_channel(v.g), srgb_linear_to_nonlinear_channel(v.b));
  8. }
  9. float srgb_nonlinear_to_linear_channel(float u)
  10. {
  11. return (u <= 0.04045) ? (u / 12.92) : pow((u + 0.055) / 1.055, 2.4);
  12. }
  13. float3 srgb_nonlinear_to_linear(float3 v)
  14. {
  15. return float3(srgb_nonlinear_to_linear_channel(v.r), srgb_nonlinear_to_linear_channel(v.g), srgb_nonlinear_to_linear_channel(v.b));
  16. }
  17. float3 rec709_to_rec2020(float3 v)
  18. {
  19. float r = dot(v, float3(0.62740389593469903, 0.32928303837788370, 0.043313065687417225));
  20. float g = dot(v, float3(0.069097289358232075, 0.91954039507545871, 0.011362315566309178));
  21. float b = dot(v, float3(0.016391438875150280, 0.088013307877225749, 0.89559525324762401));
  22. return float3(r, g, b);
  23. }
  24. float3 d65p3_to_rec709(float3 v)
  25. {
  26. float r = dot(v, float3(1.2249401762805598, -0.22494017628055996, 0.));
  27. float g = dot(v, float3(-0.042056954709688163, 1.0420569547096881, 0.));
  28. float b = dot(v, float3(-0.019637554590334432, -0.078636045550631889, 1.0982736001409663));
  29. return float3(r, g, b);
  30. }
  31. float3 rec2020_to_rec709(float3 v)
  32. {
  33. float r = dot(v, float3(1.6604910021084345, -0.58764113878854951, -0.072849863319884883));
  34. float g = dot(v, float3(-0.12455047452159074, 1.1328998971259603, -0.0083494226043694768));
  35. float b = dot(v, float3(-0.018150763354905303, -0.10057889800800739, 1.1187296613629127));
  36. return float3(r, g, b);
  37. }
  38. float3 reinhard(float3 rgb)
  39. {
  40. rgb /= rgb + float3(1., 1., 1.);
  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. float linear_to_st2084_channel(float x)
  46. {
  47. float c = pow(abs(x), 0.1593017578);
  48. return pow((0.8359375 + 18.8515625 * c) / (1. + 18.6875 * c), 78.84375);
  49. }
  50. float3 linear_to_st2084(float3 rgb)
  51. {
  52. return float3(linear_to_st2084_channel(rgb.r), linear_to_st2084_channel(rgb.g), linear_to_st2084_channel(rgb.b));
  53. }
  54. float st2084_to_linear_channel(float u)
  55. {
  56. float c = pow(abs(u), 1. / 78.84375);
  57. return pow(abs(max(c - 0.8359375, 0.) / (18.8515625 - 18.6875 * c)), 1. / 0.1593017578);
  58. }
  59. float3 st2084_to_linear(float3 rgb)
  60. {
  61. return float3(st2084_to_linear_channel(rgb.r), st2084_to_linear_channel(rgb.g), st2084_to_linear_channel(rgb.b));
  62. }
  63. float eetf_0_Lmax(float maxRGB1_pq, float Lw, float Lmax)
  64. {
  65. float Lw_pq = linear_to_st2084_channel(Lw / 10000.);
  66. float E1 = saturate(maxRGB1_pq / Lw_pq); // Ensure normalization in case Lw is a lie
  67. float maxLum = linear_to_st2084_channel(Lmax / 10000.) / Lw_pq;
  68. float KS = (1.5 * maxLum) - 0.5;
  69. float E2 = E1;
  70. if (E1 > KS)
  71. {
  72. float T = (E1 - KS) / (1. - KS);
  73. float Tsquared = T * T;
  74. float Tcubed = Tsquared * T;
  75. float P = (2. * Tcubed - 3. * Tsquared + 1.) * KS + (Tcubed - 2. * Tsquared + T) * (1. - KS) + (-2. * Tcubed + 3. * Tsquared) * maxLum;
  76. E2 = P;
  77. }
  78. float E3 = E2;
  79. float E4 = E3 * Lw_pq;
  80. return E4;
  81. }
  82. float3 maxRGB_eetf_internal(float3 rgb_linear, float maxRGB1_linear, float maxRGB1_pq, float Lw, float Lmax)
  83. {
  84. float maxRGB2_pq = eetf_0_Lmax(maxRGB1_pq, Lw, Lmax);
  85. float maxRGB2_linear = st2084_to_linear_channel(maxRGB2_pq);
  86. // avoid divide-by-zero possibility
  87. maxRGB1_linear = max(6.10352e-5, maxRGB1_linear);
  88. rgb_linear *= maxRGB2_linear / maxRGB1_linear;
  89. return rgb_linear;
  90. }
  91. float3 maxRGB_eetf_pq_to_linear(float3 rgb_pq, float Lw, float Lmax)
  92. {
  93. float3 rgb_linear = st2084_to_linear(rgb_pq);
  94. float maxRGB1_linear = max(max(rgb_linear.r, rgb_linear.g), rgb_linear.b);
  95. float maxRGB1_pq = max(max(rgb_pq.r, rgb_pq.g), rgb_pq.b);
  96. return maxRGB_eetf_internal(rgb_linear, maxRGB1_linear, maxRGB1_pq, Lw, Lmax);
  97. }
  98. float3 maxRGB_eetf_linear_to_linear(float3 rgb_linear, float Lw, float Lmax)
  99. {
  100. float maxRGB1_linear = max(max(rgb_linear.r, rgb_linear.g), rgb_linear.b);
  101. float maxRGB1_pq = linear_to_st2084_channel(maxRGB1_linear);
  102. return maxRGB_eetf_internal(rgb_linear, maxRGB1_linear, maxRGB1_pq, Lw, Lmax);
  103. }
  104. float3 st2084_to_linear_eetf(float3 rgb, float Lw, float Lmax)
  105. {
  106. return (Lw > Lmax) ? maxRGB_eetf_pq_to_linear(rgb, Lw, Lmax) : st2084_to_linear(rgb);
  107. }
  108. float linear_to_hlg_channel(float u)
  109. {
  110. float ln2_i = 1. / log(2.);
  111. float m = 0.17883277 / ln2_i;
  112. return (u <= (1. / 12.)) ? sqrt(3. * u) : ((log2((12. * u) - 0.28466892) * m) + 0.55991073);
  113. }
  114. float3 linear_to_hlg(float3 rgb, float Lw)
  115. {
  116. rgb = saturate(rgb);
  117. if (Lw > 1000.)
  118. {
  119. rgb = maxRGB_eetf_linear_to_linear(rgb, Lw, 1000.);
  120. rgb *= 10000. / Lw;
  121. }
  122. else
  123. {
  124. rgb *= 10.;
  125. }
  126. float Yd = dot(rgb, float3(0.2627, 0.678, 0.0593));
  127. // pow(0., exponent) can lead to NaN, use smallest positive normal number
  128. Yd = max(6.10352e-5, Yd);
  129. rgb *= pow(Yd, -1. / 6.);
  130. return float3(linear_to_hlg_channel(rgb.r), linear_to_hlg_channel(rgb.g), linear_to_hlg_channel(rgb.b));
  131. }
  132. float hlg_to_linear_channel(float u)
  133. {
  134. float ln2_i = 1. / log(2.);
  135. float m = ln2_i / 0.17883277;
  136. float a = -ln2_i * 0.55991073 / 0.17883277;
  137. return (u <= 0.5) ? ((u * u) / 3.) : ((exp2(u * m + a) + 0.28466892) / 12.);
  138. }
  139. float3 hlg_to_linear(float3 v, float exponent)
  140. {
  141. float3 rgb = float3(hlg_to_linear_channel(v.r), hlg_to_linear_channel(v.g), hlg_to_linear_channel(v.b));
  142. float Ys = dot(rgb, float3(0.2627, 0.678, 0.0593));
  143. rgb *= pow(Ys, exponent);
  144. return rgb;
  145. }