Debian Codegraph @

src/backend/gl/shaders.c

raw symbols SCIP
1 #include "gl_common.h"
2 // Don't macro expand bool!
3 #undef bool
4 // clang-format off
5 const char copy_area_frag[] = GLSL(330,
6 layout(location = UNIFORM_TEX_LOC)
7 uniform sampler2D tex;
8 in vec2 texcoord;
9 void main() {
10 vec2 texsize = textureSize(tex, 0);
11 gl_FragColor = texture2D(tex, texcoord / texsize, 0);
12 }
13 );
14
15 const char copy_area_with_dither_frag[] = GLSL(330,
16 layout(location = UNIFORM_TEX_LOC)
17 uniform sampler2D tex;
18 in vec2 texcoord;
19 vec4 dither(vec4, vec2);
20 void main() {
21 vec2 texsize = textureSize(tex, 0);
22 gl_FragColor = dither(texture2D(tex, texcoord / texsize, 0), gl_FragCoord.xy);
23 }
24 );
25
26 const char blend_with_mask_frag[] = GLSL(330,
27 layout(location = UNIFORM_TEX_LOC)
28 uniform sampler2D tex;
29 layout(location = UNIFORM_OPACITY_LOC)
30 uniform float opacity;
31 in vec2 texcoord;
32 float mask_factor();
33 void main() {
34 gl_FragColor = texelFetch(tex, ivec2(texcoord.xy), 0) * opacity * mask_factor();
35 }
36 );
37
38 const char fill_frag[] = GLSL(330,
39 layout(location = UNIFORM_COLOR_LOC)
40 uniform vec4 color;
41 void main() {
42 gl_FragColor = color;
43 }
44 );
45
46 const char fill_vert[] = GLSL(330,
47 layout(location = 0) in vec2 in_coord;
48 layout(location = UNIFORM_PROJECTION_LOC)
49 uniform mat4 projection;
50 void main() {
51 gl_Position = projection * vec4(in_coord, 0, 1);
52 }
53 );
54
55 const char interpolating_frag[] = GLSL(330,
56 layout(location = UNIFORM_TEX_LOC)
57 uniform sampler2D tex;
58 in vec2 texcoord;
59 void main() {
60 gl_FragColor = vec4(texture2D(tex, vec2(texcoord.xy), 0).rgb, 1);
61 }
62 );
63
64 const char interpolating_vert[] = GLSL(330,
65 layout(location = UNIFORM_PROJECTION_LOC)
66 uniform mat4 projection;
67 layout(location = UNIFORM_TEXSIZE_LOC)
68 uniform vec2 texsize;
69 layout(location = 0) in vec2 in_coord;
70 layout(location = 1) in vec2 in_texcoord;
71 out vec2 texcoord;
72 void main() {
73 gl_Position = projection * vec4(in_coord, 0, 1);
74 texcoord = in_texcoord / texsize;
75 }
76 );
77
78 #define MASKING_SHADER_TEMPLATE GLSL(330, \
79 layout(location = UNIFORM_MASK_TEX_LOC) \
80 uniform sampler2D mask_tex; \
81 layout(location = UNIFORM_MASK_OFFSET_LOC) \
82 uniform vec2 mask_offset; \
83 layout(location = UNIFORM_MASK_CORNER_RADIUS_LOC) \
84 uniform float mask_corner_radius; \
85 layout(location = UNIFORM_MASK_INVERTED_LOC) \
86 uniform bool mask_inverted; \
87 X(layout(location = UNIFORM_MASK_SCALE_LOC) \
88 uniform vec2 mask_scale;) \
89 in vec2 texcoord; \
90 vec2 mask_rectangle_sdf(vec2 point, vec2 half_size) { \
91 vec2 d = max(abs(point) - half_size, 0.0); \
92 float l = length(d); \
93 /* Add a small number to avoid 0/0. */ \
94 return vec2(l, l / (max(d.x, d.y) + 1e-8)); \
95 } \
96 float mask_factor() { \
97 vec2 mask_size = textureSize(mask_tex, 0); \
98 vec2 maskcoord = texcoord - mask_offset; \
99 X(maskcoord = maskcoord / mask_scale;) \
100 vec4 mask = texture2D(mask_tex, maskcoord / mask_size); \
101 if (mask_corner_radius != 0) { \
102 vec2 inner_size = mask_size - vec2(mask_corner_radius) * 2.0f; \
103 vec2 sdf = mask_rectangle_sdf(maskcoord - mask_size / 2.0f, \
104 inner_size / 2.0f); \
105 float dist = sdf.x - mask_corner_radius + sdf.y / 2.0f; \
106 if (dist > 0.0f) { \
107 mask.r *= (1.0f - clamp(dist, 0.0f, sdf.y) / (sdf.y + 1e-8));\
108 } \
109 } \
110 if (mask_inverted) { \
111 mask.rgb = 1.0 - mask.rgb; \
112 } \
113 return mask.r; \
114 } \
115 )
116 #define X(...)
117 const char masking_glsl[] = MASKING_SHADER_TEMPLATE;
118 #undef X
119 #define X(...) __VA_ARGS__
120 const char scaled_masking_glsl[] = MASKING_SHADER_TEMPLATE;
121 #undef X
122 const char blit_shader_glsl[] = GLSL(330,
123 // Opacity and dim is unused by the default shader. They are here for
124 // backwards compatibility with existing shaders.
125 //
126 // Opacity is set to tint.a, dim is set to (tint.r + tint.g + tint.b) / 3.0
127 layout(location = UNIFORM_OPACITY_LOC)
128 uniform float opacity;
129 layout(location = UNIFORM_DIM_LOC)
130 uniform float dim;
131 layout(location = UNIFORM_TINT_LOC)
132 uniform vec4 tint;
133 layout(location = UNIFORM_CORNER_RADIUS_LOC)
134 uniform float corner_radius;
135 layout(location = UNIFORM_BORDER_WIDTH_LOC)
136 uniform float border_width;
137 layout(location = UNIFORM_INVERT_COLOR_LOC)
138 uniform bool invert_color;
139 in vec2 texcoord;
140 layout(location = UNIFORM_TEX_LOC)
141 uniform sampler2D tex;
142 layout(location = UNIFORM_EFFECTIVE_SIZE_LOC)
143 uniform vec2 effective_size;
144 layout(location = UNIFORM_BRIGHTNESS_LOC)
145 uniform sampler2D brightness;
146 layout(location = UNIFORM_MAX_BRIGHTNESS_LOC)
147 uniform float max_brightness;
148 layout(location = UNIFORM_TIME_LOC)
149 uniform float time;
150 // Signed distance field for rectangle center at (0, 0), with size of
151 // half_size * 2
152 // Returns 2 number: the distance, and the approximate chord length inside
153 // the pixel around `point`.
154 vec2 rectangle_sdf(vec2 point, vec2 half_size) {
155 vec2 d = max(abs(point) - half_size, 0.0);
156 float l = length(d);
157 // Add a small number to avoid 0/0.
158 return vec2(l, l / (max(d.x, d.y) + 1e-8));
159 }
160
161 vec4 default_post_processing(vec4 c) {
162 vec4 border_color = texture(tex, vec2(0.0, 0.5));
163 if (invert_color) {
164 c = vec4(c.aaa - c.rgb, c.a);
165 border_color = vec4(border_color.aaa - border_color.rgb, border_color.a);
166 }
167 c = c * tint;
168 border_color = border_color * tint;
169
170 vec3 rgb_brightness = texelFetch(brightness, ivec2(0, 0), 0).rgb;
171 // Ref: https://en.wikipedia.org/wiki/Relative_luminance
172 float brightness = rgb_brightness.r * 0.21 +
173 rgb_brightness.g * 0.72 +
174 rgb_brightness.b * 0.07;
175 if (brightness > max_brightness) {
176 c.rgb = c.rgb * (max_brightness / brightness);
177 border_color.rgb = border_color.rgb * (max_brightness / brightness);
178 }
179
180 if (corner_radius != 0) {
181 // Rim color is the color of the outer rim of the window, if there is no
182 // border, it's the color of the window itself, otherwise it's the border.
183 // Using mix() to avoid a branch here.
184 vec4 rim_color = mix(c, border_color, clamp(border_width, 0.0f, 1.0f));
185
186 vec2 outer_size = effective_size;
187 vec2 inner_size = outer_size - vec2(corner_radius) * 2.0f;
188 vec2 sdf = rectangle_sdf(texcoord - outer_size / 2.0f,
189 inner_size / 2.0f);
190 // For anti-aliasing, we estimate how much of the pixel is covered by the rounded
191 // rectangle. This differs depends on at what angle the circle sweeps through the
192 // pixel. e.g. if it goes from corner to corner, then the coverage goes from 0 to
193 // 1 when the distance goes from -sqrt(2)/2 to +sqrt(2)/2; if it goes from edge to
194 // edge, then the coverage goes from 0 to 1 when the distance goes from -0.5 to 0.5.
195 // The chord length returned by `rectangle_sdf` is an approximation of this.
196 float rect_distance = sdf.x - corner_radius + sdf.y / 2.0f;
197 // Add a small number to sdf.y to avoid 0/0
198 if (rect_distance > 0.0f) {
199 c = (1.0f - clamp(rect_distance, 0.0f, sdf.y) / (sdf.y + 1e-8)) * rim_color;
200 } else {
201 float factor = clamp(rect_distance + border_width, 0.0f, sdf.y) / (sdf.y + 1e-8);
202 c = (1.0f - factor) * c + factor * border_color;
203 }
204 }
205
206 return c;
207 }
208
209 vec4 window_shader();
210 float mask_factor();
211
212 void main() {
213 gl_FragColor = window_shader() * mask_factor();
214 }
215 );
216
217 const char blit_shader_default[] = GLSL(330,
218 in vec2 texcoord;
219 uniform sampler2D tex;
220 vec4 default_post_processing(vec4 c);
221 vec4 window_shader() {
222 vec2 texsize = textureSize(tex, 0);
223 vec4 c = texture2D(tex, texcoord / texsize, 0);
224 return default_post_processing(c);
225 }
226 );
227
228 const char vertex_shader[] = GLSL(330,
229 layout(location = UNIFORM_PROJECTION_LOC)
230 uniform mat4 projection;
231 layout(location = UNIFORM_SCALE_LOC)
232 uniform float scale = 1.0f;
233 layout(location = 0) in vec2 coord;
234 layout(location = 1) in vec2 in_texcoord;
235 out vec2 texcoord;
236 void main() {
237 gl_Position = projection * vec4(coord, 0, scale);
238 texcoord = in_texcoord;
239 }
240 );
241 /// Add dithering for downsampling from 16-bit color to 8-bit color.
242 const char dither_glsl[] = GLSL(330,
243 // Stolen from: https://www.shadertoy.com/view/7sfXDn
244 float bayer2(vec2 a) {
245 a = floor(a);
246 return fract(a.x / 2. + a.y * a.y * .75);
247 }
248 // 16 * 16 is 2^8, so in total we have equivalent of 16-bit
249 // color depth, should be enough?
250 float bayer(vec2 a16) {
251 vec2 a8 = a16 * .5;
252 vec2 a4 = a8 * .5;
253 vec2 a2 = a4 * .5;
254 float bayer32 = ((bayer2(a2) * .25 + bayer2( a4))
255 * .25 + bayer2( a8))
256 * .25 + bayer2(a16);
257 return bayer32;
258 }
259 vec4 dither(vec4 c, vec2 coord) {
260 vec4 residual = mod(c, 1.0 / 255.0);
261 residual = min(residual, vec4(1.0 / 255.0) - residual);
262 vec4 dithered = vec4(greaterThan(residual, vec4(1.0 / 65535.0)));
263 return vec4(c + dithered * bayer(coord) / 255.0);
264 }
265 );
266 // clang-format on