Warning, /plasma/kwin/src/opengl/colormanagement.glsl is written in an unsupported language. File is not indexed.

0001 const int sRGB_EOTF = 0;
0002 const int linear_EOTF = 1;
0003 const int PQ_EOTF = 2;
0004 const int scRGB_EOTF = 3;
0005 const int gamma22_EOTF = 4;
0006 
0007 uniform mat4 colorimetryTransform;
0008 uniform int sourceNamedTransferFunction;
0009 uniform int destinationNamedTransferFunction;
0010 uniform float sdrBrightness;// in nits
0011 uniform float maxHdrBrightness; // in nits
0012 
0013 vec3 nitsToPq(vec3 nits) {
0014     vec3 normalized = clamp(nits / 10000.0, vec3(0), vec3(1));
0015     const float c1 = 0.8359375;
0016     const float c2 = 18.8515625;
0017     const float c3 = 18.6875;
0018     const float m1 = 0.1593017578125;
0019     const float m2 = 78.84375;
0020     vec3 powed = pow(normalized, vec3(m1));
0021     vec3 num = vec3(c1) + c2 * powed;
0022     vec3 denum = vec3(1.0) + c3 * powed;
0023     return pow(num / denum, vec3(m2));
0024 }
0025 vec3 pqToNits(vec3 pq) {
0026     const float c1 = 0.8359375;
0027     const float c2 = 18.8515625;
0028     const float c3 = 18.6875;
0029     const float m1_inv = 1.0 / 0.1593017578125;
0030     const float m2_inv = 1.0 / 78.84375;
0031     vec3 powed = pow(pq, vec3(m2_inv));
0032     vec3 num = max(powed - c1, vec3(0.0));
0033     vec3 den = c2 - c3 * powed;
0034     return 10000.0 * pow(num / den, vec3(m1_inv));
0035 }
0036 vec3 srgbToLinear(vec3 color) {
0037     bvec3 isLow = lessThanEqual(color, vec3(0.04045f));
0038     vec3 loPart = color / 12.92f;
0039     vec3 hiPart = pow((color + 0.055f) / 1.055f, vec3(12.0f / 5.0f));
0040 #if __VERSION__ >= 130
0041     return mix(hiPart, loPart, isLow);
0042 #else
0043     return mix(hiPart, loPart, vec3(isLow.r ? 1.0 : 0.0, isLow.g ? 1.0 : 0.0, isLow.b ? 1.0 : 0.0));
0044 #endif
0045 }
0046 
0047 vec3 linearToSrgb(vec3 color) {
0048     bvec3 isLow = lessThanEqual(color, vec3(0.0031308f));
0049     vec3 loPart = color * 12.92f;
0050     vec3 hiPart = pow(color, vec3(5.0f / 12.0f)) * 1.055f - 0.055f;
0051 #if __VERSION__ >= 130
0052     return mix(hiPart, loPart, isLow);
0053 #else
0054     return mix(hiPart, loPart, vec3(isLow.r ? 1.0 : 0.0, isLow.g ? 1.0 : 0.0, isLow.b ? 1.0 : 0.0));
0055 #endif
0056 }
0057 
0058 vec3 doTonemapping(vec3 color, float maxBrightness) {
0059     // TODO do something better here
0060     return clamp(color.rgb, vec3(0.0), vec3(maxBrightness));
0061 }
0062 
0063 vec4 encodingToNits(vec4 color, int sourceTransferFunction) {
0064     if (sourceTransferFunction == sRGB_EOTF) {
0065         color.rgb /= max(color.a, 0.001);
0066         color.rgb = sdrBrightness * srgbToLinear(color.rgb);
0067         color.rgb *= color.a;
0068     } else if (sourceTransferFunction == PQ_EOTF) {
0069         color.rgb /= max(color.a, 0.001);
0070         color.rgb = pqToNits(color.rgb);
0071         color.rgb *= color.a;
0072     } else if (sourceTransferFunction == scRGB_EOTF) {
0073         color.rgb *= 80.0;
0074     } else if (sourceTransferFunction == gamma22_EOTF) {
0075         color.rgb /= max(color.a, 0.001);
0076         color.rgb = sdrBrightness * pow(color.rgb, vec3(2.2));
0077         color.rgb *= color.a;
0078     }
0079     return color;
0080 }
0081 
0082 vec4 sourceEncodingToNitsInDestinationColorspace(vec4 color) {
0083     color = encodingToNits(color, sourceNamedTransferFunction);
0084     color.rgb = (colorimetryTransform * vec4(color.rgb, 1.0)).rgb;
0085     return vec4(doTonemapping(color.rgb, maxHdrBrightness), color.a);
0086 }
0087 
0088 vec4 nitsToEncoding(vec4 color, int destinationTransferFunction) {
0089     if (destinationTransferFunction == sRGB_EOTF) {
0090         color.rgb /= max(color.a, 0.001);
0091         color.rgb = linearToSrgb(doTonemapping(color.rgb, sdrBrightness) / sdrBrightness);
0092         color.rgb *= color.a;
0093     } else if (destinationTransferFunction == PQ_EOTF) {
0094         color.rgb /= max(color.a, 0.001);
0095         color.rgb = nitsToPq(color.rgb);
0096         color.rgb *= color.a;
0097     } else if (destinationTransferFunction == scRGB_EOTF) {
0098         color.rgb /= 80.0;
0099     } else if (destinationTransferFunction == gamma22_EOTF) {
0100         color.rgb /= max(color.a, 0.001);
0101         color.rgb = pow(color.rgb / sdrBrightness, vec3(1.0 / 2.2));
0102         color.rgb *= color.a;
0103     }
0104     return color;
0105 }
0106 
0107 vec4 nitsToDestinationEncoding(vec4 color) {
0108     return nitsToEncoding(color, destinationNamedTransferFunction);
0109 }