PPTonemapCommon.bslinc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. Technique : base("PPTonemapCommon") =
  2. {
  3. Language = "HLSL11";
  4. Pass =
  5. {
  6. Fragment =
  7. {
  8. static const float3x3 sRGBToXYZMatrix =
  9. {
  10. 0.4124564f, 0.3575761f, 0.1804375f,
  11. 0.2126729f, 0.7151522f, 0.0721750f,
  12. 0.0193339f, 0.1191920f, 0.9503041f,
  13. };
  14. static const float3x3 XYZTosRGBMatrix =
  15. {
  16. 3.2409699419f, -1.5373831776f, -0.4986107603f,
  17. -0.9692436363f, 1.8759675015f, 0.0415550574f,
  18. 0.0556300797f, -0.2039769589f, 1.0569715142f,
  19. };
  20. static const float3x3 D65ToD60Matrix =
  21. {
  22. 1.01303, 0.00610531, -0.014971,
  23. 0.00769823, 0.998165, -0.00503203,
  24. -0.00284131, 0.00468516, 0.924507,
  25. };
  26. static const float3x3 D60ToD65Matrix =
  27. {
  28. 0.987224, -0.00611327, 0.0159533,
  29. -0.00759836, 1.00186, 0.00533002,
  30. 0.00307257, -0.00509595, 1.08168,
  31. };
  32. static const float3x3 XYZToACES2065Matrix =
  33. {
  34. 1.0498110175, 0.0000000000,-0.0000974845,
  35. -0.4959030231, 1.3733130458, 0.0982400361,
  36. 0.0000000000, 0.0000000000, 0.9912520182,
  37. };
  38. static const float3x3 XYZToACEScgMatrix =
  39. {
  40. 1.6410233797, -0.3248032942, -0.2364246952,
  41. -0.6636628587, 1.6153315917, 0.0167563477,
  42. 0.0117218943, -0.0082844420, 0.9883948585,
  43. };
  44. static const float3x3 ACEScgToXYZMatrix =
  45. {
  46. 0.6624541811, 0.1340042065, 0.1561876870,
  47. 0.2722287168, 0.6740817658, 0.0536895174,
  48. -0.0055746495, 0.0040607335, 1.0103391003,
  49. };
  50. /**
  51. * Encodes a 10bit linear color into 8bits by converting it to log space.
  52. *
  53. * @param linearColor Linear color.
  54. * @return Encoded color in log space.
  55. */
  56. float3 LinearToLogColor(float3 linearColor)
  57. {
  58. float linearRange = 14.0f;
  59. float linearGrey = 0.18f;
  60. float exposureGrey = 444.0f;
  61. float3 logColor = log2(linearColor) / linearRange - log2(linearGrey) / linearRange + exposureGrey / 1023.0f;
  62. return saturate(logColor);
  63. }
  64. /**
  65. * Decodes a 8bit log encoded color back into linear space.
  66. *
  67. * @param logColor Log space color.
  68. * @return Color in linear space.
  69. */
  70. float3 LogToLinearColor(float3 logColor)
  71. {
  72. float linearRange = 14.0f;
  73. float linearGrey = 0.18f;
  74. float exposureGrey = 444.0f;
  75. return exp2((logColor - exposureGrey / 1023.0f) * linearRange) * linearGrey;
  76. }
  77. /**
  78. * Converts a linear color value in sRGB/Rec.709 color space into gamma space (applies Rec.709 transfer function).
  79. * Rec.709 values are suitable for HDTVs and projectors.
  80. *
  81. * @param linearColor Linear color in sRGB/Rec.709 color space.
  82. * @return Gamma corrected color.
  83. */
  84. float3 LinearToGammaRec709(float3 linearColor)
  85. {
  86. // TODO: Clamp lower end of linear color so it isn't denormalized?
  87. return min(linearColor * 4.5f, pow(max(linearColor, 0.018f), 0.45f) * 1.099f - 0.099f);
  88. }
  89. /**
  90. * Converts a linear color value in sRGB/Rec.709 color space into gamma space (applies sRGB transfer function).
  91. * sRGB values are suitable for PC displays.
  92. *
  93. * @param linearColor Linear color in sRGB/Rec.709 color space.
  94. * @return Gamma corrected color.
  95. */
  96. float3 LinearToGammasRGB(float3 linearColor)
  97. {
  98. // TODO: Clamp lower end of linear color so it isn't denormalized?
  99. return min(linearColor * 12.92f, pow(max(linearColor, 0.00313067f), 1.0f/2.4f) * 1.055f - 0.055f);
  100. }
  101. };
  102. };
  103. };
  104. Technique : base("PPTonemapCommon") =
  105. {
  106. Language = "GLSL";
  107. Pass =
  108. {
  109. Fragment =
  110. {
  111. const mat3x3 sRGBToXYZMatrix = mat3x3(
  112. vec3(0.4124564f, 0.2126729f, 0.0193339f),
  113. vec3(0.3575761f, 0.7151522f, 0.1191920f),
  114. vec3(0.1804375f, 0.0721750f, 0.9503041f)
  115. );
  116. const mat3x3 XYZTosRGBMatrix = mat3x3(
  117. vec3(3.2409699419f, -0.9692436363f, 0.0556300797f),
  118. vec3(-1.5373831776f, 1.8759675015f, -0.2039769589f),
  119. vec3(-0.4986107603f, 0.0415550574f, 1.0569715142f)
  120. );
  121. const mat3x3 D65ToD60Matrix = mat3x3(
  122. vec3(1.01303f, 0.00769823f, -0.00284131f),
  123. vec3(0.00610531f, 0.998165f, 0.00468516f),
  124. vec3(-0.014971f, -0.00503203f, 0.924507f)
  125. );
  126. const mat3x3 D60ToD65Matrix = mat3x3(
  127. vec3(0.987224f, -0.00759836f, 0.00307257f),
  128. vec3(-0.00611327f, 1.00186f, -0.00509595f),
  129. vec3(0.0159533f, 0.00533002f, 1.08168f)
  130. );
  131. const mat3x3 XYZToACES2065Matrix = mat3x3(
  132. vec3(1.0498110175f, -0.4959030231f, 0.0000000000f),
  133. vec3(0.0000000000f, 1.3733130458f, 0.0000000000f),
  134. vec3(-0.0000974845f, 0.0982400361f, 0.9912520182f)
  135. );
  136. const mat3x3 XYZToACEScgMatrix = mat3x3(
  137. vec3(1.6410233797f, -0.6636628587f, 0.0117218943f),
  138. vec3(-0.3248032942f, 1.6153315917f, -0.0082844420f),
  139. vec3(-0.2364246952f, 0.0167563477f, 0.9883948585f)
  140. );
  141. const mat3x3 ACEScgToXYZMatrix = mat3x3(
  142. vec3(0.6624541811f, 0.2722287168f, -0.0055746495f),
  143. vec3(0.1340042065f, 0.6740817658f, 0.0040607335f),
  144. vec3(0.1561876870f, 0.0536895174f, 1.0103391003f)
  145. );
  146. /**
  147. * Encodes a 10bit linear color into 8bits by converting it to log space.
  148. *
  149. * @param linearColor Linear color.
  150. * @return Encoded color in log space.
  151. */
  152. void LinearToLogColor(vec3 linearColor, out vec3 result)
  153. {
  154. float linearRange = 14.0f;
  155. float linearGrey = 0.18f;
  156. float exposureGrey = 444.0f;
  157. vec3 logColor = log2(linearColor) / linearRange - log2(linearGrey) / linearRange + exposureGrey / 1023.0f;
  158. result = clamp(logColor, 0.0f, 1.0f);
  159. }
  160. /**
  161. * Decodes a 8bit log encoded color back into linear space.
  162. *
  163. * @param logColor Log space color.
  164. * @return Color in linear space.
  165. */
  166. void LogToLinearColor(vec3 logColor, out vec3 result)
  167. {
  168. float linearRange = 14.0f;
  169. float linearGrey = 0.18f;
  170. float exposureGrey = 444.0f;
  171. result = exp2((logColor - exposureGrey / 1023.0f) * linearRange) * linearGrey;
  172. }
  173. /**
  174. * Converts a linear color value in sRGB/Rec.709 color space into gamma space (applies Rec.709 transfer function).
  175. * Rec.709 values are suitable for HDTVs and projectors.
  176. *
  177. * @param linearColor Linear color in sRGB/Rec.709 color space.
  178. * @return Gamma corrected color.
  179. */
  180. void LinearToGammaRec709(vec3 linearColor, out vec3 result)
  181. {
  182. // TODO: Clamp lower end of linear color so it isn't denormalized?
  183. result = min(linearColor * 4.5f, pow(max(linearColor, 0.018f), vec3(0.45f)) * 1.099f - 0.099f);
  184. }
  185. /**
  186. * Converts a linear color value in sRGB/Rec.709 color space into gamma space (applies sRGB transfer function).
  187. * sRGB values are suitable for PC displays.
  188. *
  189. * @param linearColor Linear color in sRGB/Rec.709 color space.
  190. * @return Gamma corrected color.
  191. */
  192. void LinearToGammasRGB(vec3 linearColor, out vec3 result)
  193. {
  194. // TODO: Clamp lower end of linear color so it isn't denormalized?
  195. result = min(linearColor * 12.92f, pow(max(linearColor, 0.00313067f), vec3(1.0f/2.4f)) * 1.055f - 0.055f);
  196. }
  197. };
  198. };
  199. };