Debian Codegraph @

src/backend/gl/gl_common.h

raw symbols SCIP
1 // SPDX-License-Identifier: MPL-2.0
2 // Copyright (c) Yuxuan Shui <[email protected]>
3 #pragma once
4 #include <epoxy/gl.h>
5 #include <picom/backend.h>
6 #include <stdbool.h>
7 #include <xcb/xproto.h>
8
9 #include "log.h"
10 #include "region.h"
11
12 #define CASESTRRET(s) \
13 case s: return #s
14 struct gl_blur_context;
15
16 // Fragment shader uniforms
17 #define UNIFORM_OPACITY_LOC 1
18 #define UNIFORM_INVERT_COLOR_LOC 2
19 #define UNIFORM_TEX_LOC 3
20 #define UNIFORM_EFFECTIVE_SIZE_LOC 4
21 #define UNIFORM_DIM_LOC 5
22 #define UNIFORM_BRIGHTNESS_LOC 6
23 #define UNIFORM_MAX_BRIGHTNESS_LOC 7
24 #define UNIFORM_CORNER_RADIUS_LOC 8
25 #define UNIFORM_BORDER_WIDTH_LOC 9
26 #define UNIFORM_TIME_LOC 10
27 #define UNIFORM_COLOR_LOC 11
28 #define UNIFORM_PIXEL_NORM_LOC 12
29 #define UNIFORM_TEX_SRC_LOC 13
30 #define UNIFORM_MASK_TEX_LOC 14
31 #define UNIFORM_MASK_OFFSET_LOC 15
32 #define UNIFORM_MASK_CORNER_RADIUS_LOC 16
33 #define UNIFORM_MASK_INVERTED_LOC 17
34
35 // Vertex shader uniforms
36 #define UNIFORM_SCALE_LOC 18
37 #define UNIFORM_PROJECTION_LOC 19
38 #define UNIFORM_TEXSIZE_LOC 21
39
40 // Added in backend API 2.0
41 #define UNIFORM_TINT_LOC 22
42 #define UNIFORM_MASK_SCALE_LOC 23
43 #define NUMBER_OF_UNIFORMS (UNIFORM_TINT_LOC + 1)
44
45 struct gl_shader {
46 GLuint prog;
47 // If the shader is user controlled, we don't know which uniform will be
48 // active, so we need to track which one is.
49 // This is not used if the shader code is fully controlled by us.
50 uint32_t uniform_bitmask;
51 };
52
53 /// @brief Wrapper of a bound GL texture.
54 struct gl_texture {
55 enum backend_image_format format;
56 GLuint texture;
57 int width, height;
58 /// Whether the texture is Y-inverted
59 /// This is always true for all our internal textures. Textures created from
60 /// binding a X pixmap might not be. And our OpenGL back buffer is never
61 /// Y-inverted (until we can start using glClipControl).
62 bool y_inverted;
63 xcb_pixmap_t pixmap;
64
65 // Textures for auxiliary uses.
66 GLuint auxiliary_texture[2];
67 void *user_data;
68 };
69
70 enum gl_sampler {
71 /// A sampler that repeats the texture, with nearest filtering.
72 GL_SAMPLER_REPEAT = 0,
73 /// A sampler that repeats the texture, with linear filtering.
74 GL_SAMPLER_REPEAT_SCALE,
75 /// Clamp to edge
76 GL_SAMPLER_EDGE,
77 /// Clamp to border, border color will be (0, 0, 0, 0)
78 GL_SAMPLER_BORDER,
79 /// Special sampler for blurring, same as `GL_SAMPLER_CLAMP_TO_EDGE`,
80 /// but uses linear filtering.
81 GL_SAMPLER_BLUR,
82 GL_MAX_SAMPLERS = GL_SAMPLER_BLUR + 1,
83 };
84
85 struct gl_data {
86 struct backend_base base;
87 // If we are using proprietary NVIDIA driver
88 bool is_nvidia;
89 // If ARB_robustness extension is present
90 bool has_robustness;
91 // If EXT_EGL_image_storage extension is present
92 bool has_egl_image_storage;
93 /// A symbolic image representing the back buffer.
94 struct gl_texture back_image;
95 struct gl_shader default_shader;
96 struct gl_shader brightness_shader;
97 struct gl_shader fill_shader;
98 GLuint temp_fbo;
99 GLuint frame_timing[2];
100 int current_frame_timing;
101 struct gl_shader copy_area_prog;
102 struct gl_shader copy_area_with_dither_prog;
103 GLuint samplers[GL_MAX_SAMPLERS];
104 GLuint buffer_objects[4];
105 GLuint vertex_array_objects[2];
106
107 bool dithered_present;
108
109 GLuint default_mask_texture;
110
111 /// Release the user data attached to a gl_texture
112 void (*release_user_data)(backend_t *base, struct gl_texture *);
113
114 struct log_target *logger;
115 };
116
117 typedef struct session session_t;
118
119 void gl_prepare(backend_t *base, const region_t *reg);
120 /// Convert a mask formed by a collection of rectangles to OpenGL vertex and texture
121 /// coordinates.
122 ///
123 /// @param[in] origin origin of the source image in target coordinates
124 /// @param[in] mask_origin origin of the mask in source coordinates
125 /// @param[in] nrects number of rectangles
126 /// @param[in] rects mask rectangles, in mask coordinates
127 /// @param[out] coord OpenGL vertex coordinates, suitable for creating VAO/VBO
128 /// @param[out] indices OpenGL vertex indices, suitable for creating VAO/VBO
129 void gl_mask_rects_to_coords(ivec2 origin, int nrects, const rect_t *rects, vec2 scale,
130 GLfloat *coord, GLuint *indices);
131 /// Like `gl_mask_rects_to_coords`, but with `origin` is (0, 0).
132 static inline void gl_mask_rects_to_coords_simple(int nrects, const rect_t *rects,
133 GLfloat *coord, GLuint *indices) {
134 return gl_mask_rects_to_coords((ivec2){0, 0}, nrects, rects, SCALE_IDENTITY,
135 coord, indices);
136 }
137
138 GLuint gl_create_shader(GLenum shader_type, const char *shader_str);
139 GLuint gl_create_program(const GLuint *shaders, int nshaders);
140 GLuint gl_create_program_from_str(const char *vert_shader_str, const char *frag_shader_str);
141 GLuint gl_create_program_from_strv(const char **vert_shaders, const char **frag_shaders);
142 void *gl_create_window_shader(backend_t *backend_data,
143 const struct shader_specification *spec, const char *source);
144 void gl_destroy_window_shader(backend_t *backend_data, shader_handle shader);
145 uint64_t gl_get_shader_attributes(backend_t *backend_data, shader_handle shader);
146 bool gl_last_render_time(backend_t *backend_data, struct timespec *time);
147
148 bool gl_blit(backend_t *base, ivec2 origin, image_handle target,
149 const struct backend_blit_args *args);
150 image_handle gl_new_image(backend_t *backend_data attr_unused,
151 enum backend_image_format format, ivec2 size);
152 bool gl_clear(backend_t *backend_data, image_handle target, struct color color);
153
154 void gl_root_change(backend_t *base, session_t *);
155
156 void gl_resize(struct gl_data *, int width, int height);
157
158 bool gl_init(struct gl_data *gd, session_t *);
159 void gl_deinit(struct gl_data *gd);
160
161 GLuint gl_new_texture(void);
162
163 xcb_pixmap_t gl_release_image(backend_t *base, image_handle image);
164
165 bool gl_blur(struct backend_base *gd, ivec2 origin, image_handle target,
166 const struct backend_blur_args *args);
167 bool gl_copy_area(backend_t *backend_data, ivec2 origin, image_handle target,
168 image_handle source, const region_t *region);
169 bool gl_copy_area_quantize(backend_t *backend_data, ivec2 origin, image_handle target_handle,
170 image_handle source_handle, const region_t *region);
171 bool gl_apply_alpha(backend_t *base, image_handle target, double alpha, const region_t *reg_op);
172 image_handle gl_back_buffer(struct backend_base *base);
173 uint32_t gl_image_capabilities(backend_t *base, image_handle img);
174 bool gl_is_format_supported(backend_t *base, enum backend_image_format format);
175 void *gl_create_blur_context(backend_t *base, enum blur_method,
176 enum backend_image_format format, void *args);
177 void gl_destroy_blur_context(backend_t *base, void *ctx);
178 void gl_get_blur_size(void *blur_context, int *width, int *height);
179
180 enum device_status gl_device_status(backend_t *base);
181
182 #define gl_check_fb_complete(fb) gl_check_fb_complete_(__func__, __LINE__, (fb))
183 static inline bool gl_check_fb_complete_(const char *func, int line, GLenum fb);
184
185 static inline void gl_finish_render(struct gl_data *gd) {
186 glEndQuery(GL_TIME_ELAPSED);
187 gd->current_frame_timing ^= 1;
188 }
189
190 /// Return a FBO with `image` bound to the first color attachment. `GL_DRAW_FRAMEBUFFER`
191 /// will be bound to the returned FBO.
192 static inline GLuint gl_bind_image_to_fbo(struct gl_data *gd, image_handle image_) {
193 auto image = (struct gl_texture *)image_;
194 if (image == &gd->back_image) {
195 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
196 return 0;
197 }
198 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, gd->temp_fbo);
199 glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
200 image->texture, 0);
201 CHECK(gl_check_fb_complete(GL_DRAW_FRAMEBUFFER));
202 glDrawBuffer(GL_COLOR_ATTACHMENT0);
203 return gd->temp_fbo;
204 }
205
206 /// Flip the target coordinates returned by `gl_mask_rects_to_coords` vertically relative
207 /// to the target. Texture coordinates are unchanged.
208 ///
209 /// @param[in] nrects number of rectangles
210 /// @param[in] coord OpenGL vertex coordinates
211 /// @param[in] target_height height of the target image
212 static inline void gl_y_flip_target(int nrects, GLfloat *coord, GLint target_height) {
213 for (ptrdiff_t i = 0; i < nrects; i++) {
214 auto current_rect = &coord[i * 16]; // 16 numbers per rectangle
215 for (ptrdiff_t j = 0; j < 4; j++) {
216 // 4 numbers per vertex, target coordinates are the first two
217 auto current_vertex = &current_rect[j * 4];
218 current_vertex[1] = (GLfloat)target_height - current_vertex[1];
219 }
220 }
221 }
222
223 /**
224 * Get a textual representation of an OpenGL error.
225 */
226 static inline const char *gl_get_err_str(GLenum err) {
227 switch (err) {
228 CASESTRRET(GL_NO_ERROR);
229 CASESTRRET(GL_INVALID_ENUM);
230 CASESTRRET(GL_INVALID_VALUE);
231 CASESTRRET(GL_INVALID_OPERATION);
232 CASESTRRET(GL_INVALID_FRAMEBUFFER_OPERATION);
233 CASESTRRET(GL_OUT_OF_MEMORY);
234 CASESTRRET(GL_STACK_UNDERFLOW);
235 CASESTRRET(GL_STACK_OVERFLOW);
236 CASESTRRET(GL_FRAMEBUFFER_UNDEFINED);
237 CASESTRRET(GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT);
238 CASESTRRET(GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT);
239 CASESTRRET(GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER);
240 CASESTRRET(GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER);
241 CASESTRRET(GL_FRAMEBUFFER_UNSUPPORTED);
242 CASESTRRET(GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE);
243 CASESTRRET(GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS);
244 }
245 return NULL;
246 }
247
248 /**
249 * Check for GL error.
250 *
251 * http://blog.nobel-joergensen.com/2013/01/29/debugging-opengl-using-glgeterror/
252 */
253 static inline void gl_check_err_(const char *func, int line) {
254 GLenum err = GL_NO_ERROR;
255
256 while (GL_NO_ERROR != (err = glGetError())) {
257 const char *errtext = gl_get_err_str(err);
258 if (errtext) {
259 log_printf(tls_logger, LOG_LEVEL_ERROR, func,
260 "GL error at line %d: %s", line, errtext);
261 } else {
262 log_printf(tls_logger, LOG_LEVEL_ERROR, func,
263 "GL error at line %d: %d", line, err);
264 }
265 if (err == GL_CONTEXT_LOST) {
266 // GL_CONTEXT_LOST repeats until the context is restored.
267 break;
268 }
269 }
270 }
271
272 static inline void gl_clear_err(void) {
273 while (glGetError() != GL_NO_ERROR) {
274 }
275 }
276
277 #define gl_check_err() gl_check_err_(__func__, __LINE__)
278
279 /**
280 * Check for GL framebuffer completeness.
281 */
282 static inline bool gl_check_fb_complete_(const char *func, int line, GLenum fb) {
283 GLenum status = glCheckFramebufferStatus(fb);
284
285 if (status == GL_FRAMEBUFFER_COMPLETE) {
286 return true;
287 }
288
289 const char *stattext = gl_get_err_str(status);
290 if (stattext) {
291 log_printf(tls_logger, LOG_LEVEL_ERROR, func,
292 "Framebuffer attachment failed at line %d: %s", line, stattext);
293 } else {
294 log_printf(tls_logger, LOG_LEVEL_ERROR, func,
295 "Framebuffer attachment failed at line %d: %d", line, status);
296 }
297
298 return false;
299 }
300
301 static const GLuint vert_coord_loc = 0;
302 static const GLuint vert_in_texcoord_loc = 1;
303
304 // Add one level of indirection so macros in VA_ARGS can be expanded.
305 #define GLSL_(version, ...) \
306 "#version " #version "\n" \
307 "#extension GL_ARB_explicit_uniform_location : enable\n" #__VA_ARGS__
308 #define GLSL(version, ...) GLSL_(version, __VA_ARGS__)
309 #define QUOTE(...) #__VA_ARGS__
310
311 extern const char vertex_shader[], blend_with_mask_frag[], masking_glsl[],
312 scaled_masking_glsl[], copy_area_frag[], copy_area_with_dither_frag[], fill_frag[],
313 fill_vert[], interpolating_frag[], interpolating_vert[], blit_shader_glsl[],
314 blit_shader_default[], present_vertex_shader[], dither_glsl[];