Debian Codegraph @

shaders/src/common_graphics.fs

raw
1 //------------------------------------------------------------------------------
2 // Common color operations
3 //------------------------------------------------------------------------------
4
5 /**
6 * Computes the luminance of the specified linear RGB color using the
7 * luminance coefficients from Rec. 709.
8 *
9 * @public-api
10 */
11 float luminance(const vec3 linear) {
12 return dot(linear, vec3(0.2126, 0.7152, 0.0722));
13 }
14
15 /**
16 * Computes the pre-exposed intensity using the specified intensity and exposure.
17 * This function exists to force highp precision on the two parameters
18 */
19 float computePreExposedIntensity(const highp float intensity, const highp float exposure) {
20 return intensity * exposure;
21 }
22
23 void unpremultiply(inout vec4 color) {
24 color.rgb /= max(color.a, FLT_EPS);
25 }
26
27 /**
28 * Applies a full range YCbCr to sRGB conversion and returns an RGB color.
29 *
30 * @public-api
31 */
32 vec3 ycbcrToRgb(float luminance, vec2 cbcr) {
33 // Taken from https://developer.apple.com/documentation/arkit/arframe/2867984-capturedimage
34 const mat4 ycbcrToRgbTransform = mat4(
35 1.0000, 1.0000, 1.0000, 0.0000,
36 0.0000, -0.3441, 1.7720, 0.0000,
37 1.4020, -0.7141, 0.0000, 0.0000,
38 -0.7010, 0.5291, -0.8860, 1.0000
39 );
40 return (ycbcrToRgbTransform * vec4(luminance, cbcr, 1.0)).rgb;
41 }
42
43 //------------------------------------------------------------------------------
44 // Tone mapping operations
45 //------------------------------------------------------------------------------
46
47 /*
48 * The input must be in the [0, 1] range.
49 */
50 vec3 Inverse_Tonemap_Unreal(const vec3 x) {
51 return (x * -0.155) / (x - 1.019);
52 }
53
54 /**
55 * Applies the inverse of the tone mapping operator to the specified HDR or LDR
56 * sRGB (non-linear) color and returns a linear sRGB color. The inverse tone mapping
57 * operator may be an approximation of the real inverse operation.
58 *
59 * @public-api
60 */
61 vec3 inverseTonemapSRGB(vec3 color) {
62 // sRGB input
63 color = clamp(color, 0.0, 1.0);
64 return Inverse_Tonemap_Unreal(color);
65 }
66
67 /**
68 * Applies the inverse of the tone mapping operator to the specified HDR or LDR
69 * linear RGB color and returns a linear RGB color. The inverse tone mapping operator
70 * may be an approximation of the real inverse operation.
71 *
72 * @public-api
73 */
74 vec3 inverseTonemap(vec3 linear) {
75 // Linear input
76 linear = clamp(linear, 0.0, 1.0);
77 return Inverse_Tonemap_Unreal(pow(linear, vec3(1.0 / 2.2)));
78 }
79
80 //------------------------------------------------------------------------------
81 // Common texture operations
82 //------------------------------------------------------------------------------
83
84 /**
85 * Decodes the specified RGBM value to linear HDR RGB.
86 */
87 vec3 decodeRGBM(vec4 c) {
88 c.rgb *= (c.a * 16.0);
89 return c.rgb * c.rgb;
90 }
91
92 //------------------------------------------------------------------------------
93 // Common debug
94 //------------------------------------------------------------------------------
95
96 vec3 heatmap(float v) {
97 vec3 r = v * 2.1 - vec3(1.8, 1.14, 0.3);
98 return 1.0 - r * r;
99 }
100
101 vec3 uintToColorDebug(uint v) {
102 if (v == 0u) {
103 return vec3(0.0, 1.0, 0.0); // green
104 } else if (v == 1u) {
105 return vec3(0.0, 0.0, 1.0); // blue
106 } else if (v == 2u) {
107 return vec3(1.0, 1.0, 0.0); // yellow
108 } else if (v == 3u) {
109 return vec3(1.0, 0.0, 0.0); // red
110 } else if (v == 4u) {
111 return vec3(1.0, 0.0, 1.0); // purple
112 } else if (v == 5u) {
113 return vec3(0.0, 1.0, 1.0); // cyan
114 }
115 }