color.effect 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 = saturate(rgb);
  42. rgb = pow(rgb, float3(1. / 2.4, 1. / 2.4, 1. / 2.4));
  43. rgb = srgb_nonlinear_to_linear(rgb);
  44. return rgb;
  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. float3 linear_to_st2084(float3 rgb)
  52. {
  53. return float3(linear_to_st2084_channel(rgb.r), linear_to_st2084_channel(rgb.g), linear_to_st2084_channel(rgb.b));
  54. }
  55. float st2084_to_linear_channel(float u)
  56. {
  57. float c = pow(abs(u), 1. / 78.84375);
  58. return pow(abs(max(c - 0.8359375, 0.) / (18.8515625 - 18.6875 * c)), 1. / 0.1593017578);
  59. }
  60. float3 st2084_to_linear(float3 rgb)
  61. {
  62. return float3(st2084_to_linear_channel(rgb.r), st2084_to_linear_channel(rgb.g), st2084_to_linear_channel(rgb.b));
  63. }
  64. float eetf_0_Lmax(float maxRGB1_pq, float Lw, float Lmax)
  65. {
  66. float Lw_pq = linear_to_st2084_channel(Lw / 10000.);
  67. float E1 = saturate(maxRGB1_pq / Lw_pq); // Ensure normalization in case Lw is a lie
  68. float maxLum = linear_to_st2084_channel(Lmax / 10000.) / Lw_pq;
  69. float KS = (1.5 * maxLum) - 0.5;
  70. float E2 = E1;
  71. if (E1 > KS)
  72. {
  73. float T = (E1 - KS) / (1. - KS);
  74. float Tsquared = T * T;
  75. float Tcubed = Tsquared * T;
  76. float P = (2. * Tcubed - 3. * Tsquared + 1.) * KS + (Tcubed - 2. * Tsquared + T) * (1. - KS) + (-2. * Tcubed + 3. * Tsquared) * maxLum;
  77. E2 = P;
  78. }
  79. float E3 = E2;
  80. float E4 = E3 * Lw_pq;
  81. return E4;
  82. }
  83. float3 maxRGB_eetf_internal(float3 rgb_linear, float maxRGB1_linear, float maxRGB1_pq, float Lw, float Lmax)
  84. {
  85. float maxRGB2_pq = eetf_0_Lmax(maxRGB1_pq, Lw, Lmax);
  86. float maxRGB2_linear = st2084_to_linear_channel(maxRGB2_pq);
  87. // avoid divide-by-zero possibility
  88. maxRGB1_linear = max(6.10352e-5, maxRGB1_linear);
  89. rgb_linear *= maxRGB2_linear / maxRGB1_linear;
  90. return rgb_linear;
  91. }
  92. float3 maxRGB_eetf_pq_to_linear(float3 rgb_pq, float Lw, float Lmax)
  93. {
  94. float3 rgb_linear = st2084_to_linear(rgb_pq);
  95. float maxRGB1_linear = max(max(rgb_linear.r, rgb_linear.g), rgb_linear.b);
  96. float maxRGB1_pq = max(max(rgb_pq.r, rgb_pq.g), rgb_pq.b);
  97. return maxRGB_eetf_internal(rgb_linear, maxRGB1_linear, maxRGB1_pq, Lw, Lmax);
  98. }
  99. float3 maxRGB_eetf_linear_to_linear(float3 rgb_linear, float Lw, float Lmax)
  100. {
  101. float maxRGB1_linear = max(max(rgb_linear.r, rgb_linear.g), rgb_linear.b);
  102. float maxRGB1_pq = linear_to_st2084_channel(maxRGB1_linear);
  103. return maxRGB_eetf_internal(rgb_linear, maxRGB1_linear, maxRGB1_pq, Lw, Lmax);
  104. }
  105. float3 st2084_to_linear_eetf(float3 rgb, float Lw, float Lmax)
  106. {
  107. return (Lw > Lmax) ? maxRGB_eetf_pq_to_linear(rgb, Lw, Lmax) : st2084_to_linear(rgb);
  108. }
  109. float linear_to_hlg_channel(float u)
  110. {
  111. float ln2_i = 1. / log(2.);
  112. float m = 0.17883277 / ln2_i;
  113. return (u <= (1. / 12.)) ? sqrt(3. * u) : ((log2((12. * u) - 0.28466892) * m) + 0.55991073);
  114. }
  115. float3 linear_to_hlg(float3 rgb, float Lw)
  116. {
  117. rgb = saturate(rgb);
  118. if (Lw > 1000.)
  119. {
  120. rgb = maxRGB_eetf_linear_to_linear(rgb, Lw, 1000.);
  121. rgb *= 10000. / Lw;
  122. }
  123. else
  124. {
  125. rgb *= 10.;
  126. }
  127. float Yd = dot(rgb, float3(0.2627, 0.678, 0.0593));
  128. // pow(0., exponent) can lead to NaN, use smallest positive normal number
  129. Yd = max(6.10352e-5, Yd);
  130. rgb *= pow(Yd, -1. / 6.);
  131. return float3(linear_to_hlg_channel(rgb.r), linear_to_hlg_channel(rgb.g), linear_to_hlg_channel(rgb.b));
  132. }
  133. float hlg_to_linear_channel(float u)
  134. {
  135. float ln2_i = 1. / log(2.);
  136. float m = ln2_i / 0.17883277;
  137. float a = -ln2_i * 0.55991073 / 0.17883277;
  138. return (u <= 0.5) ? ((u * u) / 3.) : ((exp2(u * m + a) + 0.28466892) / 12.);
  139. }
  140. float3 hlg_to_linear(float3 v, float exponent)
  141. {
  142. float3 rgb = float3(hlg_to_linear_channel(v.r), hlg_to_linear_channel(v.g), hlg_to_linear_channel(v.b));
  143. float Ys = dot(rgb, float3(0.2627, 0.678, 0.0593));
  144. rgb *= pow(Ys, exponent);
  145. return rgb;
  146. }