| 1 |
|
| 2 |
|
| 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 |
|
| 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 |
|
| 36 |
#define UNIFORM_SCALE_LOC 18 |
| 37 |
#define UNIFORM_PROJECTION_LOC 19 |
| 38 |
#define UNIFORM_TEXSIZE_LOC 21 |
| 39 |
|
| 40 |
|
| 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 |
|
| 48 |
|
| 49 |
|
| 50 |
uint32_t uniform_bitmask; |
| 51 |
}; |
| 52 |
|
| 53 |
|
| 54 |
struct gl_texture { |
| 55 |
enum backend_image_format format; |
| 56 |
GLuint texture; |
| 57 |
int width, height; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
bool y_inverted; |
| 63 |
xcb_pixmap_t pixmap; |
| 64 |
|
| 65 |
|
| 66 |
GLuint auxiliary_texture[2]; |
| 67 |
void *user_data; |
| 68 |
}; |
| 69 |
|
| 70 |
enum gl_sampler { |
| 71 |
|
| 72 |
GL_SAMPLER_REPEAT = 0, |
| 73 |
|
| 74 |
GL_SAMPLER_REPEAT_SCALE, |
| 75 |
|
| 76 |
GL_SAMPLER_EDGE, |
| 77 |
|
| 78 |
GL_SAMPLER_BORDER, |
| 79 |
|
| 80 |
|
| 81 |
GL_SAMPLER_BLUR, |
| 82 |
GL_MAX_SAMPLERS = GL_SAMPLER_BLUR + 1, |
| 83 |
}; |
| 84 |
|
| 85 |
struct gl_data { |
| 86 |
struct backend_base base; |
| 87 |
|
| 88 |
bool is_nvidia; |
| 89 |
|
| 90 |
bool has_robustness; |
| 91 |
|
| 92 |
bool has_egl_image_storage; |
| 93 |
|
| 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 |
|
| 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 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
void gl_mask_rects_to_coords(ivec2 origin, int nrects, const rect_t *rects, vec2 scale, |
| 130 |
GLfloat *coord, GLuint *indices); |
| 131 |
|
| 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 |
|
| 191 |
|
| 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 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 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]; |
| 215 |
for (ptrdiff_t j = 0; j < 4; j++) { |
| 216 |
|
| 217 |
auto current_vertex = ¤t_rect[j * 4]; |
| 218 |
current_vertex[1] = (GLfloat)target_height - current_vertex[1]; |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 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 |
|
| 250 |
|
| 251 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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[]; |