fork/wezterm

git clone https://git.julia.blue/fork/wezterm

Wez Furlongs,wez/wezterm,wezterm/wezterm,11c4b1166

termwiz-0.23.0
4.6 KiB161 linesraw
1// This is the Glyph fragment shader.
2// It is responsible for laying down the glyph graphics on top of the other layers.
3#extension GL_EXT_blend_func_extended: enable
4precision highp float;
5
6in float o_has_color;
7in vec2 o_tex;
8in vec3 o_hsv;
9in vec4 o_fg_color;
10in vec4 o_fg_color_alt;
11in float o_fg_color_mix;
12
13// The color + alpha
14layout(location=0, index=0) out vec4 color;
15// Individual alpha channels for RGBA in color, used for subpixel
16// antialiasing blending
17layout(location=0, index=1) out vec4 colorMask;
18
19uniform vec3 foreground_text_hsb;
20uniform sampler2D atlas_nearest_sampler;
21uniform sampler2D atlas_linear_sampler;
22uniform bool subpixel_aa;
23uniform uint milliseconds;
24
25struct ColorEase {
26  vec4 in_function;
27  vec4 out_function;
28  uint in_duration_ms;
29  uint out_duration_ms;
30};
31
32float evaluate_cubic_bezier(vec4 bezier, float x) {
33  return pow(1.0 - x, 3.0) * bezier[0]
34    + 3.0 * pow(1.0 - x, 2.) * x * bezier[1]
35    + 3.0 * (1.0 - x) * pow(x, 2.) * bezier[2]
36    + pow(x, 3.) * bezier[3];
37}
38
39float colorease_intensity(ColorEase ease) {
40  uint total_duration = ease.in_duration_ms + ease.out_duration_ms;
41  uint elapsed = milliseconds % total_duration;
42  if (elapsed < ease.in_duration_ms) {
43    return evaluate_cubic_bezier(
44      ease.in_function,
45      float(elapsed) / float(ease.in_duration_ms)
46    );
47  }
48  return 1.0 - evaluate_cubic_bezier(
49    ease.out_function,
50    float(elapsed - ease.in_duration_ms) / float(ease.out_duration_ms)
51  );
52}
53
54uniform ColorEase cursor_blink;
55uniform ColorEase blink;
56uniform ColorEase rapid_blink;
57
58vec3 rgb2hsv(vec3 c)
59{
60    vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
61    vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
62    vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
63
64    float d = q.x - min(q.w, q.y);
65    float e = 1.0e-10;
66    return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
67}
68
69vec3 hsv2rgb(vec3 c)
70{
71    vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
72    vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
73    return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
74}
75
76const vec3 unit3 = vec3(1.0);
77
78vec4 apply_hsv(vec4 c, vec3 transform)
79{
80  if (transform == unit3) {
81    return c;
82  }
83  vec3 hsv = rgb2hsv(c.rgb) * transform;
84  return vec4(hsv2rgb(hsv).rgb, c.a);
85}
86
87/*
88float to_srgb(float x) {
89  if (x <= 0.0031308) {
90    return 12.92 * x;
91  }
92  return 1.055 * pow(x, (1.0 / 2.4)) - 0.055;
93}
94
95vec3 to_srgb(vec3 c) {
96  return vec3(to_srgb(c.r), to_srgb(c.g), to_srgb(c.b));
97}
98
99vec4 to_srgb(vec4 v) {
100  return vec4(to_srgb(v.rgb), v.a);
101}
102*/
103
104vec4 to_srgb(vec4 linearRGB)
105{
106  bvec3 cutoff = lessThan(linearRGB.rgb, vec3(0.0031308));
107  vec3 higher = vec3(1.055)*pow(linearRGB.rgb, vec3(1.0/2.4)) - vec3(0.055);
108  vec3 lower = linearRGB.rgb * vec3(12.92);
109
110  return vec4(mix(higher, lower, cutoff), linearRGB.a);
111}
112
113void main() {
114  vec4 fg_color = mix(o_fg_color, o_fg_color_alt, o_fg_color_mix);
115  if (o_has_color == 3.0) {
116    // Solid color block
117    color = fg_color;
118    colorMask = vec4(1.0);
119  } else if (o_has_color == 2.0) {
120    // The window background attachment
121    color = texture(atlas_linear_sampler, o_tex);
122    // Apply window_background_image_opacity to the background image
123    if (subpixel_aa) {
124      colorMask = fg_color.aaaa;
125    } else {
126      color.a *= fg_color.a;
127    }
128  } else if (o_has_color == 1.0) {
129    // the texture is full color info (eg: color emoji glyph)
130    color = texture(atlas_nearest_sampler, o_tex);
131    // this is the alpha
132    colorMask = color.aaaa;
133  } else if (o_has_color == 4.0) {
134    // Grayscale poly quad for non-aa text render layers
135    colorMask = texture(atlas_nearest_sampler, o_tex);
136    color = fg_color;
137    // On Intel hardware/drivers, we need to recompute the alpha this way.
138    // We don't know why; it doesn't make sense.
139    // The inputs are already in range and work fine on other platforms.
140    // See discussion starting at:
141    // <https://github.com/wezterm/wezterm/issues/1180#issuecomment-1496102764>
142    // for the background.
143    color.a = mix(o_fg_color.a, o_fg_color_alt.a, clamp(o_fg_color_mix, 0.0, 1.0));
144    color.a *= colorMask.a;
145  } else if (o_has_color == 0.0) {
146    // the texture is the alpha channel/color mask
147    colorMask = texture(atlas_nearest_sampler, o_tex);
148    // and we need to tint with the fg_color
149    color = fg_color;
150    if (!subpixel_aa) {
151      color.a = colorMask.a;
152    }
153    color = apply_hsv(color, foreground_text_hsb);
154  }
155
156  color = apply_hsv(color, o_hsv);
157
158  // We MUST output SRGB and tell glium that we do that (outputs_srgb),
159  // otherwise something in glium over-gamma-corrects depending on the gl setup.
160  color = to_srgb(color);
161}