| 1 |
|
| 2 |
|
| 3 |
#include <ctype.h> |
| 4 |
#include <epoxy/gl.h> |
| 5 |
#include <stdbool.h> |
| 6 |
#include <stddef.h> |
| 7 |
#include <stdint.h> |
| 8 |
#include <stdio.h> |
| 9 |
#include <string.h> |
| 10 |
#include <time.h> |
| 11 |
#include <xcb/render.h> |
| 12 |
|
| 13 |
#include <picom/types.h> |
| 14 |
|
| 15 |
#include "backend/backend_common.h" |
| 16 |
#include "common.h" |
| 17 |
#include "compiler.h" |
| 18 |
#include "config.h" |
| 19 |
#include "log.h" |
| 20 |
#include "region.h" |
| 21 |
#include "test.h" |
| 22 |
#include "utils/misc.h" |
| 23 |
|
| 24 |
#include "gl_common.h" |
| 25 |
|
| 26 |
void gl_prepare(backend_t *base, const region_t *reg attr_unused) { |
| 27 |
auto gd = (struct gl_data *)base; |
| 28 |
glBeginQuery(GL_TIME_ELAPSED, gd->frame_timing[gd->current_frame_timing]); |
| 29 |
} |
| 30 |
|
| 31 |
GLuint gl_create_shader(GLenum shader_type, const char *shader_str) { |
| 32 |
log_trace("===\n%s\n===", shader_str); |
| 33 |
|
| 34 |
bool success = false; |
| 35 |
GLuint shader = glCreateShader(shader_type); |
| 36 |
if (!shader) { |
| 37 |
log_error("Failed to create shader with type %#x.", shader_type); |
| 38 |
goto end; |
| 39 |
} |
| 40 |
glShaderSource(shader, 1, &shader_str, NULL); |
| 41 |
glCompileShader(shader); |
| 42 |
|
| 43 |
|
| 44 |
{ |
| 45 |
GLint status = GL_FALSE; |
| 46 |
glGetShaderiv(shader, GL_COMPILE_STATUS, &status); |
| 47 |
if (status == GL_FALSE) { |
| 48 |
GLint log_len = 0; |
| 49 |
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_len); |
| 50 |
if (log_len) { |
| 51 |
char log[log_len + 1]; |
| 52 |
glGetShaderInfoLog(shader, log_len, NULL, log); |
| 53 |
log_error("Failed to compile shader with type %d: %s", |
| 54 |
shader_type, log); |
| 55 |
} |
| 56 |
goto end; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
success = true; |
| 61 |
|
| 62 |
end: |
| 63 |
if (shader && !success) { |
| 64 |
glDeleteShader(shader); |
| 65 |
shader = 0; |
| 66 |
} |
| 67 |
gl_check_err(); |
| 68 |
|
| 69 |
return shader; |
| 70 |
} |
| 71 |
|
| 72 |
GLuint gl_create_program(const GLuint *const shaders, int nshaders) { |
| 73 |
bool success = false; |
| 74 |
GLuint program = glCreateProgram(); |
| 75 |
if (!program) { |
| 76 |
log_error("Failed to create program."); |
| 77 |
goto end; |
| 78 |
} |
| 79 |
|
| 80 |
for (int i = 0; i < nshaders; ++i) { |
| 81 |
glAttachShader(program, shaders[i]); |
| 82 |
} |
| 83 |
glLinkProgram(program); |
| 84 |
|
| 85 |
|
| 86 |
{ |
| 87 |
GLint status = GL_FALSE; |
| 88 |
glGetProgramiv(program, GL_LINK_STATUS, &status); |
| 89 |
if (status == GL_FALSE) { |
| 90 |
GLint log_len = 0; |
| 91 |
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &log_len); |
| 92 |
if (log_len) { |
| 93 |
char log[log_len + 1]; |
| 94 |
glGetProgramInfoLog(program, log_len, NULL, log); |
| 95 |
log_error("Failed to link program: %s", log); |
| 96 |
} |
| 97 |
goto end; |
| 98 |
} |
| 99 |
} |
| 100 |
success = true; |
| 101 |
|
| 102 |
end: |
| 103 |
if (program) { |
| 104 |
for (int i = 0; i < nshaders; ++i) { |
| 105 |
glDetachShader(program, shaders[i]); |
| 106 |
} |
| 107 |
} |
| 108 |
if (program && !success) { |
| 109 |
glDeleteProgram(program); |
| 110 |
program = 0; |
| 111 |
} |
| 112 |
gl_check_err(); |
| 113 |
|
| 114 |
return program; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
GLuint gl_create_program_from_strv(const char **vert_shaders, const char **frag_shaders) { |
| 122 |
int vert_count, frag_count; |
| 123 |
for (vert_count = 0; vert_shaders && vert_shaders[vert_count]; ++vert_count) { |
| 124 |
} |
| 125 |
for (frag_count = 0; frag_shaders && frag_shaders[frag_count]; ++frag_count) { |
| 126 |
} |
| 127 |
|
| 128 |
GLuint prog = 0; |
| 129 |
auto shaders = (GLuint *)ccalloc(vert_count + frag_count, GLuint); |
| 130 |
for (int i = 0; i < vert_count; ++i) { |
| 131 |
shaders[i] = gl_create_shader(GL_VERTEX_SHADER, vert_shaders[i]); |
| 132 |
if (shaders[i] == 0) { |
| 133 |
goto out; |
| 134 |
} |
| 135 |
} |
| 136 |
for (int i = 0; i < frag_count; ++i) { |
| 137 |
shaders[vert_count + i] = |
| 138 |
gl_create_shader(GL_FRAGMENT_SHADER, frag_shaders[i]); |
| 139 |
if (shaders[vert_count + i] == 0) { |
| 140 |
goto out; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
prog = gl_create_program(shaders, vert_count + frag_count); |
| 145 |
|
| 146 |
out: |
| 147 |
for (int i = 0; i < vert_count + frag_count; ++i) { |
| 148 |
if (shaders[i] != 0) { |
| 149 |
glDeleteShader(shaders[i]); |
| 150 |
} |
| 151 |
} |
| 152 |
free(shaders); |
| 153 |
gl_check_err(); |
| 154 |
|
| 155 |
return prog; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
GLuint gl_create_program_from_str(const char *vert_shader_str, const char *frag_shader_str) { |
| 162 |
const char *vert_shaders[2] = {vert_shader_str, NULL}; |
| 163 |
const char *frag_shaders[2] = {frag_shader_str, NULL}; |
| 164 |
|
| 165 |
return gl_create_program_from_strv(vert_shaders, frag_shaders); |
| 166 |
} |
| 167 |
|
| 168 |
static void gl_destroy_window_shader_inner(struct gl_shader *shader) { |
| 169 |
if (!shader) { |
| 170 |
return; |
| 171 |
} |
| 172 |
|
| 173 |
if (shader->prog) { |
| 174 |
glDeleteProgram(shader->prog); |
| 175 |
shader->prog = 0; |
| 176 |
} |
| 177 |
gl_check_err(); |
| 178 |
} |
| 179 |
|
| 180 |
void gl_destroy_window_shader(backend_t *backend_data attr_unused, shader_handle shader) { |
| 181 |
gl_destroy_window_shader_inner((struct gl_shader *)shader); |
| 182 |
free(shader); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
static GLuint |
| 200 |
_gl_average_texture_color(GLuint source_texture, GLuint destination_texture, |
| 201 |
GLuint auxiliary_texture, GLuint fbo, int width, int height) { |
| 202 |
const int max_width = 1; |
| 203 |
const int max_height = 1; |
| 204 |
const int from_width = next_power_of_two(width); |
| 205 |
const int from_height = next_power_of_two(height); |
| 206 |
const int to_width = from_width > max_width ? from_width / 2 : from_width; |
| 207 |
const int to_height = from_height > max_height ? from_height / 2 : from_height; |
| 208 |
|
| 209 |
|
| 210 |
GLint coord[] = { |
| 211 |
|
| 212 |
0, 0, |
| 213 |
0, 0, |
| 214 |
|
| 215 |
|
| 216 |
to_width, 0, |
| 217 |
width, 0, |
| 218 |
|
| 219 |
|
| 220 |
to_width, to_height, |
| 221 |
width, height, |
| 222 |
|
| 223 |
|
| 224 |
0, to_height, |
| 225 |
0, height, |
| 226 |
}; |
| 227 |
glBufferSubData(GL_ARRAY_BUFFER, 0, (long)sizeof(*coord) * 16, coord); |
| 228 |
|
| 229 |
|
| 230 |
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, |
| 231 |
destination_texture, 0); |
| 232 |
gl_check_fb_complete(GL_FRAMEBUFFER); |
| 233 |
|
| 234 |
|
| 235 |
glActiveTexture(GL_TEXTURE0); |
| 236 |
glBindTexture(GL_TEXTURE_2D, source_texture); |
| 237 |
glBindSampler(0, 0); |
| 238 |
|
| 239 |
|
| 240 |
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, NULL); |
| 241 |
|
| 242 |
|
| 243 |
GLuint result; |
| 244 |
if (to_width > max_width || to_height > max_height) { |
| 245 |
GLuint new_source_texture = destination_texture; |
| 246 |
GLuint new_destination_texture = |
| 247 |
auxiliary_texture != 0 ? auxiliary_texture : source_texture; |
| 248 |
result = _gl_average_texture_color( |
| 249 |
new_source_texture, new_destination_texture, 0, fbo, to_width, to_height); |
| 250 |
} else { |
| 251 |
result = destination_texture; |
| 252 |
} |
| 253 |
|
| 254 |
return result; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
static GLuint gl_average_texture_color(struct gl_data *gd, struct gl_texture *img) { |
| 265 |
|
| 266 |
|
| 267 |
const int texture_count = ARR_SIZE(img->auxiliary_texture); |
| 268 |
if (!img->auxiliary_texture[0]) { |
| 269 |
assert(!img->auxiliary_texture[1]); |
| 270 |
glGenTextures(texture_count, img->auxiliary_texture); |
| 271 |
glActiveTexture(GL_TEXTURE0); |
| 272 |
for (int i = 0; i < texture_count; i++) { |
| 273 |
glBindTexture(GL_TEXTURE_2D, img->auxiliary_texture[i]); |
| 274 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 275 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); |
| 276 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); |
| 277 |
glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, |
| 278 |
(GLint[]){0, 0, 0, 0}); |
| 279 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, img->width, img->height, |
| 280 |
0, GL_BGR, GL_UNSIGNED_BYTE, NULL); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, gd->temp_fbo); |
| 286 |
glDrawBuffer(GL_COLOR_ATTACHMENT0); |
| 287 |
|
| 288 |
|
| 289 |
glUseProgram(gd->brightness_shader.prog); |
| 290 |
glUniform2f(UNIFORM_TEXSIZE_LOC, (GLfloat)img->width, (GLfloat)img->height); |
| 291 |
|
| 292 |
|
| 293 |
glBindVertexArray(gd->vertex_array_objects[0]); |
| 294 |
glBindBuffer(GL_ARRAY_BUFFER, gd->buffer_objects[0]); |
| 295 |
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gd->buffer_objects[1]); |
| 296 |
glEnableVertexAttribArray(vert_coord_loc); |
| 297 |
glEnableVertexAttribArray(vert_in_texcoord_loc); |
| 298 |
glVertexAttribPointer(vert_coord_loc, 2, GL_INT, GL_FALSE, sizeof(GLint) * 4, NULL); |
| 299 |
glVertexAttribPointer(vert_in_texcoord_loc, 2, GL_INT, GL_FALSE, |
| 300 |
sizeof(GLint) * 4, (void *)(sizeof(GLint) * 2)); |
| 301 |
|
| 302 |
|
| 303 |
GLint coord[16] = {0}; |
| 304 |
GLuint indices[] = {0, 1, 2, 2, 3, 0}; |
| 305 |
glBufferData(GL_ARRAY_BUFFER, (long)sizeof(*coord) * 16, coord, GL_STREAM_DRAW); |
| 306 |
glBufferData(GL_ELEMENT_ARRAY_BUFFER, (long)sizeof(*indices) * 6, indices, |
| 307 |
GL_STREAM_DRAW); |
| 308 |
|
| 309 |
|
| 310 |
GLuint result_texture = _gl_average_texture_color( |
| 311 |
img->texture, img->auxiliary_texture[0], img->auxiliary_texture[1], |
| 312 |
gd->temp_fbo, img->width, img->height); |
| 313 |
|
| 314 |
|
| 315 |
glDisableVertexAttribArray(vert_coord_loc); |
| 316 |
glDisableVertexAttribArray(vert_in_texcoord_loc); |
| 317 |
|
| 318 |
|
| 319 |
glBufferData(GL_ARRAY_BUFFER, (long)sizeof(*coord) * 16, NULL, GL_STREAM_DRAW); |
| 320 |
glBufferData(GL_ELEMENT_ARRAY_BUFFER, (long)sizeof(*indices) * 6, NULL, GL_STREAM_DRAW); |
| 321 |
|
| 322 |
|
| 323 |
glBindBuffer(GL_ARRAY_BUFFER, 0); |
| 324 |
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
| 325 |
glBindVertexArray(0); |
| 326 |
|
| 327 |
|
| 328 |
glUseProgram(0); |
| 329 |
|
| 330 |
|
| 331 |
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); |
| 332 |
glDrawBuffer(GL_BACK); |
| 333 |
|
| 334 |
|
| 335 |
glBindTexture(GL_TEXTURE_2D, 0); |
| 336 |
|
| 337 |
gl_check_err(); |
| 338 |
|
| 339 |
return result_texture; |
| 340 |
} |
| 341 |
struct gl_texture_unit { |
| 342 |
GLuint texture; |
| 343 |
GLuint sampler; |
| 344 |
}; |
| 345 |
|
| 346 |
struct gl_uniform_value { |
| 347 |
GLenum type; |
| 348 |
union { |
| 349 |
struct gl_texture_unit tu; |
| 350 |
GLint i; |
| 351 |
GLfloat f; |
| 352 |
GLint i2[2]; |
| 353 |
GLfloat f2[2]; |
| 354 |
GLfloat f4[4]; |
| 355 |
}; |
| 356 |
}; |
| 357 |
|
| 358 |
struct gl_vertex_attrib { |
| 359 |
GLenum type; |
| 360 |
GLuint loc; |
| 361 |
void *offset; |
| 362 |
}; |
| 363 |
|
| 364 |
struct gl_vertex_attribs_definition { |
| 365 |
GLsizeiptr stride; |
| 366 |
unsigned count; |
| 367 |
struct gl_vertex_attrib attribs[]; |
| 368 |
}; |
| 369 |
|
| 370 |
static const struct gl_vertex_attribs_definition gl_blit_vertex_attribs = { |
| 371 |
.stride = sizeof(GLfloat) * 4, |
| 372 |
.count = 2, |
| 373 |
.attribs = {{GL_FLOAT, vert_coord_loc, NULL}, |
| 374 |
{GL_FLOAT, vert_in_texcoord_loc, ((GLfloat *)NULL) + 2}}, |
| 375 |
}; |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
static void |
| 390 |
gl_blit_inner(struct gl_data *gd, GLuint target_fbo, int nrects, GLfloat *coord, |
| 391 |
GLuint *indices, const struct gl_vertex_attribs_definition *vert_attribs, |
| 392 |
const struct gl_shader *shader, int nuniforms, |
| 393 |
struct gl_uniform_value *uniforms) { |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
log_trace("Blitting %d rectangles", nrects); |
| 398 |
assert(shader); |
| 399 |
assert(shader->prog); |
| 400 |
glUseProgram(shader->prog); |
| 401 |
|
| 402 |
glActiveTexture(GL_TEXTURE0); |
| 403 |
glBindTexture(GL_TEXTURE_2D, 0); |
| 404 |
GLuint texture_unit = GL_TEXTURE1; |
| 405 |
for (int i = 0; i < nuniforms; i++) { |
| 406 |
if (!(shader->uniform_bitmask & (1 << i))) { |
| 407 |
continue; |
| 408 |
} |
| 409 |
|
| 410 |
auto uniform = &uniforms[i]; |
| 411 |
switch (uniform->type) { |
| 412 |
case 0: break; |
| 413 |
case GL_TEXTURE_2D: |
| 414 |
if (uniform->tu.texture == 0) { |
| 415 |
glUniform1i(i, 0); |
| 416 |
} else { |
| 417 |
glActiveTexture(texture_unit); |
| 418 |
glBindTexture(GL_TEXTURE_2D, uniform->tu.texture); |
| 419 |
glBindSampler(texture_unit - GL_TEXTURE0, uniform->tu.sampler); |
| 420 |
glUniform1i(i, (GLint)(texture_unit - GL_TEXTURE0)); |
| 421 |
texture_unit += 1; |
| 422 |
} |
| 423 |
break; |
| 424 |
case GL_INT: glUniform1i(i, uniform->i); break; |
| 425 |
case GL_FLOAT: glUniform1f(i, uniform->f); break; |
| 426 |
case GL_INT_VEC2: glUniform2iv(i, 1, uniform->i2); break; |
| 427 |
case GL_FLOAT_VEC2: glUniform2fv(i, 1, uniform->f2); break; |
| 428 |
case GL_FLOAT_VEC4: glUniform4fv(i, 1, uniform->f4); break; |
| 429 |
default: assert(false); |
| 430 |
} |
| 431 |
} |
| 432 |
gl_check_err(); |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
glBindVertexArray(gd->vertex_array_objects[0]); |
| 438 |
|
| 439 |
glBindBuffer(GL_ARRAY_BUFFER, gd->buffer_objects[0]); |
| 440 |
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gd->buffer_objects[1]); |
| 441 |
glBufferData(GL_ARRAY_BUFFER, vert_attribs->stride * nrects * 4, coord, GL_STREAM_DRAW); |
| 442 |
glBufferData(GL_ELEMENT_ARRAY_BUFFER, (long)sizeof(*indices) * nrects * 6, |
| 443 |
indices, GL_STREAM_DRAW); |
| 444 |
for (unsigned i = 0; i < vert_attribs->count; i++) { |
| 445 |
auto attrib = &vert_attribs->attribs[i]; |
| 446 |
glEnableVertexAttribArray(attrib->loc); |
| 447 |
glVertexAttribPointer(attrib->loc, 2, attrib->type, GL_FALSE, |
| 448 |
(GLsizei)vert_attribs->stride, attrib->offset); |
| 449 |
} |
| 450 |
|
| 451 |
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, target_fbo); |
| 452 |
glDrawElements(GL_TRIANGLES, nrects * 6, GL_UNSIGNED_INT, NULL); |
| 453 |
|
| 454 |
glDisableVertexAttribArray(vert_coord_loc); |
| 455 |
glDisableVertexAttribArray(vert_in_texcoord_loc); |
| 456 |
|
| 457 |
|
| 458 |
glBufferData(GL_ARRAY_BUFFER, vert_attribs->stride * nrects * 4, NULL, GL_STREAM_DRAW); |
| 459 |
glBufferData(GL_ELEMENT_ARRAY_BUFFER, (long)sizeof(*indices) * nrects * 6, NULL, |
| 460 |
GL_STREAM_DRAW); |
| 461 |
|
| 462 |
glBindBuffer(GL_ARRAY_BUFFER, 0); |
| 463 |
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
| 464 |
glBindVertexArray(0); |
| 465 |
|
| 466 |
|
| 467 |
for (GLuint i = GL_TEXTURE1; i < texture_unit; i++) { |
| 468 |
glActiveTexture(i); |
| 469 |
glBindTexture(GL_TEXTURE_2D, 0); |
| 470 |
} |
| 471 |
glActiveTexture(GL_TEXTURE0); |
| 472 |
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); |
| 473 |
glDrawBuffer(GL_BACK); |
| 474 |
|
| 475 |
glUseProgram(0); |
| 476 |
|
| 477 |
gl_check_err(); |
| 478 |
} |
| 479 |
|
| 480 |
void gl_mask_rects_to_coords(ivec2 origin, int nrects, const rect_t *rects, vec2 scale, |
| 481 |
GLfloat *coord, GLuint *indices) { |
| 482 |
for (ptrdiff_t i = 0; i < nrects; i++) { |
| 483 |
|
| 484 |
rect_t rect_src = region_translate_rect(rects[i], ivec2_neg(origin)); |
| 485 |
|
| 486 |
rect_t rect_dst = rects[i]; |
| 487 |
|
| 488 |
|
| 489 |
memcpy(&coord[i * 16], |
| 490 |
((GLfloat[][2]){ |
| 491 |
|
| 492 |
{(GLfloat)rect_dst.x1, (GLfloat)rect_dst.y1}, |
| 493 |
{(GLfloat)(rect_src.x1 / scale.x), (GLfloat)(rect_src.y1 / scale.y)}, |
| 494 |
{(GLfloat)rect_dst.x2, (GLfloat)rect_dst.y1}, |
| 495 |
{(GLfloat)(rect_src.x2 / scale.x), (GLfloat)(rect_src.y1 / scale.y)}, |
| 496 |
{(GLfloat)rect_dst.x2, (GLfloat)rect_dst.y2}, |
| 497 |
{(GLfloat)(rect_src.x2 / scale.x), (GLfloat)(rect_src.y2 / scale.y)}, |
| 498 |
{(GLfloat)rect_dst.x1, (GLfloat)rect_dst.y2}, |
| 499 |
{(GLfloat)(rect_src.x1 / scale.x), (GLfloat)(rect_src.y2 / scale.y)}, |
| 500 |
}), |
| 501 |
sizeof(GLint[2]) * 8); |
| 502 |
|
| 503 |
|
| 504 |
GLuint u = (GLuint)(i * 4); |
| 505 |
memcpy(&indices[i * 6], |
| 506 |
((GLuint[]){u + 0, u + 1, u + 2, u + 2, u + 3, u + 0}), |
| 507 |
sizeof(GLuint) * 6); |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
static inline void gl_y_flip_texture(int nrects, GLfloat *coord, GLint texture_height) { |
| 518 |
for (ptrdiff_t i = 0; i < nrects; i++) { |
| 519 |
auto current_rect = &coord[i * 16]; |
| 520 |
for (ptrdiff_t j = 0; j < 4; j++) { |
| 521 |
|
| 522 |
auto current_vertex = ¤t_rect[j * 4 + 2]; |
| 523 |
current_vertex[1] = (GLfloat)texture_height - current_vertex[1]; |
| 524 |
} |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
static int |
| 531 |
gl_lower_blit_args(struct gl_data *gd, ivec2 origin, const struct backend_blit_args *args, |
| 532 |
GLfloat **coord, GLuint **indices, struct gl_shader **shader, |
| 533 |
struct gl_uniform_value *uniforms) { |
| 534 |
auto img = (struct gl_texture *)args->source_image; |
| 535 |
int nrects; |
| 536 |
const rect_t *rects; |
| 537 |
rects = pixman_region32_rectangles(args->target_mask, &nrects); |
| 538 |
if (!nrects) { |
| 539 |
|
| 540 |
return 0; |
| 541 |
} |
| 542 |
*coord = ccalloc(nrects * 16, GLfloat); |
| 543 |
*indices = ccalloc(nrects * 6, GLuint); |
| 544 |
gl_mask_rects_to_coords(origin, nrects, rects, args->scale, *coord, *indices); |
| 545 |
if (!img->y_inverted) { |
| 546 |
gl_y_flip_texture(nrects, *coord, img->height); |
| 547 |
} |
| 548 |
|
| 549 |
auto mask_texture = gd->default_mask_texture; |
| 550 |
auto mask_sampler = gd->samplers[GL_SAMPLER_REPEAT]; |
| 551 |
if (args->source_mask != NULL) { |
| 552 |
mask_texture = ((struct gl_texture *)args->source_mask->image)->texture; |
| 553 |
mask_sampler = gd->samplers[GL_SAMPLER_BORDER]; |
| 554 |
} |
| 555 |
GLuint brightness = 0; |
| 556 |
|
| 557 |
|
| 558 |
if (args->max_brightness < 1.0) { |
| 559 |
brightness = gl_average_texture_color(gd, img); |
| 560 |
} |
| 561 |
auto border_width = args->border_width; |
| 562 |
if (border_width > args->corner_radius) { |
| 563 |
border_width = 0; |
| 564 |
} |
| 565 |
double dim_compat = 1.0 - (args->tint.red + args->tint.green + args->tint.blue) / 3.0; |
| 566 |
|
| 567 |
auto tex_sampler = vec2_eq(args->scale, SCALE_IDENTITY) ? |
| 568 |
gd->samplers[GL_SAMPLER_REPEAT] : gd->samplers[GL_SAMPLER_REPEAT_SCALE]; |
| 569 |
struct gl_uniform_value from_uniforms[] = { |
| 570 |
[UNIFORM_OPACITY_LOC] = {.type = GL_FLOAT, .f = (float)args->tint.alpha}, |
| 571 |
[UNIFORM_INVERT_COLOR_LOC] = {.type = GL_INT, .i = args->color_inverted}, |
| 572 |
[UNIFORM_TEX_LOC] = {.type = GL_TEXTURE_2D, |
| 573 |
.tu = {img->texture, tex_sampler}}, |
| 574 |
[UNIFORM_EFFECTIVE_SIZE_LOC] = {.type = GL_FLOAT_VEC2, |
| 575 |
.f2 = {(float)args->effective_size.width, |
| 576 |
(float)args->effective_size.height}}, |
| 577 |
[UNIFORM_DIM_LOC] = {.type = GL_FLOAT, .f = (float)dim_compat}, |
| 578 |
[UNIFORM_TINT_LOC] = {.type = GL_FLOAT_VEC4, |
| 579 |
.f4 = {(float)args->tint.red, (float)args->tint.green, |
| 580 |
(float)args->tint.blue, (float)args->tint.alpha}}, |
| 581 |
[UNIFORM_BRIGHTNESS_LOC] = {.type = GL_TEXTURE_2D, |
| 582 |
.tu = {brightness, gd->samplers[GL_SAMPLER_EDGE]}}, |
| 583 |
[UNIFORM_MAX_BRIGHTNESS_LOC] = {.type = GL_FLOAT, .f = (float)args->max_brightness}, |
| 584 |
[UNIFORM_CORNER_RADIUS_LOC] = {.type = GL_FLOAT, .f = (float)args->corner_radius}, |
| 585 |
[UNIFORM_BORDER_WIDTH_LOC] = {.type = GL_FLOAT, .f = (float)border_width}, |
| 586 |
[UNIFORM_MASK_TEX_LOC] = {.type = GL_TEXTURE_2D, |
| 587 |
.tu = {mask_texture, mask_sampler}}, |
| 588 |
[UNIFORM_MASK_OFFSET_LOC] = {.type = GL_FLOAT_VEC2, .f2 = {0.0F, 0.0F}}, |
| 589 |
[UNIFORM_MASK_INVERTED_LOC] = {.type = GL_INT, .i = 0}, |
| 590 |
[UNIFORM_MASK_CORNER_RADIUS_LOC] = {.type = GL_FLOAT, .f = 0.0F}, |
| 591 |
}; |
| 592 |
|
| 593 |
|
| 594 |
if (args->source_mask != NULL) { |
| 595 |
from_uniforms[UNIFORM_MASK_OFFSET_LOC].f2[0] = |
| 596 |
(float)args->source_mask->origin.x; |
| 597 |
from_uniforms[UNIFORM_MASK_OFFSET_LOC].f2[1] = |
| 598 |
(float)args->source_mask->origin.y; |
| 599 |
from_uniforms[UNIFORM_MASK_CORNER_RADIUS_LOC].f = |
| 600 |
(float)args->source_mask->corner_radius; |
| 601 |
from_uniforms[UNIFORM_MASK_INVERTED_LOC].i = args->source_mask->inverted; |
| 602 |
} |
| 603 |
*shader = args->shader ? (struct gl_shader *)args->shader : &gd->default_shader; |
| 604 |
if ((*shader)->uniform_bitmask & (1 << UNIFORM_TIME_LOC)) { |
| 605 |
struct timespec ts; |
| 606 |
clock_gettime(CLOCK_MONOTONIC, &ts); |
| 607 |
from_uniforms[UNIFORM_TIME_LOC] = (struct gl_uniform_value){ |
| 608 |
.type = GL_FLOAT, |
| 609 |
.f = (float)ts.tv_sec * 1000.0F + (float)ts.tv_nsec / 1.0e6F, |
| 610 |
}; |
| 611 |
} |
| 612 |
memcpy(uniforms, from_uniforms, sizeof(from_uniforms)); |
| 613 |
return nrects; |
| 614 |
} |
| 615 |
|
| 616 |
bool gl_blit(backend_t *base, ivec2 origin, image_handle target_, |
| 617 |
const struct backend_blit_args *args) { |
| 618 |
auto gd = (struct gl_data *)base; |
| 619 |
auto source = (struct gl_texture *)args->source_image; |
| 620 |
auto target = (struct gl_texture *)target_; |
| 621 |
|
| 622 |
if (source == &gd->back_image) { |
| 623 |
log_error("Trying to blit from the back texture, this is not allowed"); |
| 624 |
return false; |
| 625 |
} |
| 626 |
|
| 627 |
GLfloat *coord; |
| 628 |
GLuint *indices; |
| 629 |
struct gl_shader *shader; |
| 630 |
struct gl_uniform_value uniforms[NUMBER_OF_UNIFORMS] = {}; |
| 631 |
int nrects = |
| 632 |
gl_lower_blit_args(gd, origin, args, &coord, &indices, &shader, uniforms); |
| 633 |
if (nrects == 0) { |
| 634 |
return true; |
| 635 |
} |
| 636 |
if (!target->y_inverted) { |
| 637 |
log_trace("Flipping target texture"); |
| 638 |
gl_y_flip_target(nrects, coord, target->height); |
| 639 |
} |
| 640 |
|
| 641 |
auto fbo = gl_bind_image_to_fbo(gd, target_); |
| 642 |
|
| 643 |
|
| 644 |
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); |
| 645 |
gl_blit_inner(gd, fbo, nrects, coord, indices, &gl_blit_vertex_attribs, shader, |
| 646 |
NUMBER_OF_UNIFORMS, uniforms); |
| 647 |
|
| 648 |
free(indices); |
| 649 |
free(coord); |
| 650 |
return true; |
| 651 |
} |
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
static bool gl_copy_area_blit_fbo(struct gl_data *gd, ivec2 origin, image_handle target, |
| 656 |
const region_t *region) { |
| 657 |
gl_bind_image_to_fbo(gd, target); |
| 658 |
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0); |
| 659 |
|
| 660 |
int nrects; |
| 661 |
const rect_t *rects = pixman_region32_rectangles(region, &nrects); |
| 662 |
for (ptrdiff_t i = 0; i < nrects; i++) { |
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
glBlitFramebuffer( |
| 667 |
rects[i].x1 , gd->back_image.height - rects[i].y1, |
| 668 |
rects[i].x2 , gd->back_image.height - rects[i].y2, |
| 669 |
rects[i].x1 + origin.x, rects[i].y1 + origin.y , |
| 670 |
rects[i].x2 + origin.x, rects[i].y2 + origin.y , |
| 671 |
GL_COLOR_BUFFER_BIT, GL_NEAREST); |
| 672 |
|
| 673 |
} |
| 674 |
gl_check_err(); |
| 675 |
return true; |
| 676 |
} |
| 677 |
|
| 678 |
|
| 679 |
static bool gl_copy_area_draw(struct gl_data *gd, ivec2 origin, |
| 680 |
image_handle target_handle, image_handle source_handle, |
| 681 |
struct gl_shader *shader, const region_t *region) { |
| 682 |
auto source = (struct gl_texture *)source_handle; |
| 683 |
auto target = (struct gl_texture *)target_handle; |
| 684 |
assert(source->y_inverted); |
| 685 |
|
| 686 |
int nrects; |
| 687 |
const rect_t *rects = pixman_region32_rectangles(region, &nrects); |
| 688 |
if (nrects == 0) { |
| 689 |
return true; |
| 690 |
} |
| 691 |
|
| 692 |
auto coord = ccalloc(16 * nrects, GLfloat); |
| 693 |
auto indices = ccalloc(6 * nrects, GLuint); |
| 694 |
gl_mask_rects_to_coords(origin, nrects, rects, SCALE_IDENTITY, coord, indices); |
| 695 |
if (!target->y_inverted) { |
| 696 |
gl_y_flip_target(nrects, coord, target->height); |
| 697 |
} |
| 698 |
|
| 699 |
struct gl_uniform_value uniforms[] = { |
| 700 |
[UNIFORM_TEX_LOC] = {.type = GL_TEXTURE_2D, |
| 701 |
.tu = {source->texture, gd->samplers[GL_SAMPLER_EDGE]}}, |
| 702 |
}; |
| 703 |
auto fbo = gl_bind_image_to_fbo(gd, target_handle); |
| 704 |
glBlendFunc(GL_ONE, GL_ZERO); |
| 705 |
gl_blit_inner(gd, fbo, nrects, coord, indices, &gl_blit_vertex_attribs, shader, |
| 706 |
ARR_SIZE(uniforms), uniforms); |
| 707 |
free(indices); |
| 708 |
free(coord); |
| 709 |
return true; |
| 710 |
} |
| 711 |
|
| 712 |
bool gl_copy_area(backend_t *backend_data, ivec2 origin, image_handle target, |
| 713 |
image_handle source, const region_t *region) { |
| 714 |
auto gd = (struct gl_data *)backend_data; |
| 715 |
if ((struct gl_texture *)source == &gd->back_image) { |
| 716 |
return gl_copy_area_blit_fbo(gd, origin, target, region); |
| 717 |
} |
| 718 |
return gl_copy_area_draw(gd, origin, target, source, &gd->copy_area_prog, region); |
| 719 |
} |
| 720 |
|
| 721 |
bool gl_copy_area_quantize(backend_t *backend_data, ivec2 origin, image_handle target_handle, |
| 722 |
image_handle source_handle, const region_t *region) { |
| 723 |
auto gd = (struct gl_data *)backend_data; |
| 724 |
auto target = (struct gl_texture *)target_handle; |
| 725 |
auto source = (struct gl_texture *)source_handle; |
| 726 |
if (source->format != BACKEND_IMAGE_FORMAT_PIXMAP_HIGH || |
| 727 |
source->format == target->format) { |
| 728 |
return gl_copy_area(backend_data, origin, target_handle, source_handle, region); |
| 729 |
} |
| 730 |
return gl_copy_area_draw(gd, origin, target_handle, source_handle, |
| 731 |
&gd->copy_area_with_dither_prog, region); |
| 732 |
} |
| 733 |
|
| 734 |
uint32_t gl_image_capabilities(backend_t *base, image_handle img) { |
| 735 |
auto gd = (struct gl_data *)base; |
| 736 |
auto inner = (struct gl_texture *)img; |
| 737 |
if (&gd->back_image == inner) { |
| 738 |
return BACKEND_IMAGE_CAP_DST; |
| 739 |
} |
| 740 |
if (inner->user_data) { |
| 741 |
|
| 742 |
return BACKEND_IMAGE_CAP_SRC; |
| 743 |
} |
| 744 |
return BACKEND_IMAGE_CAP_SRC | BACKEND_IMAGE_CAP_DST; |
| 745 |
} |
| 746 |
|
| 747 |
bool gl_is_format_supported(backend_t *base attr_unused, |
| 748 |
enum backend_image_format format attr_unused) { |
| 749 |
return true; |
| 750 |
} |
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
static bool gl_shader_from_stringv(const char **vshader_strv, const char **fshader_strv, |
| 756 |
struct gl_shader *ret) { |
| 757 |
|
| 758 |
ret->prog = gl_create_program_from_strv(vshader_strv, fshader_strv); |
| 759 |
if (!ret->prog) { |
| 760 |
log_error("Failed to create GLSL program."); |
| 761 |
gl_check_err(); |
| 762 |
return false; |
| 763 |
} |
| 764 |
|
| 765 |
gl_check_err(); |
| 766 |
|
| 767 |
return true; |
| 768 |
} |
| 769 |
|
| 770 |
void gl_root_change(backend_t *base, session_t *ps) { |
| 771 |
auto gd = (struct gl_data *)base; |
| 772 |
gl_resize(gd, ps->root_width, ps->root_height); |
| 773 |
} |
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
void gl_resize(struct gl_data *gd, int width, int height) { |
| 779 |
GLint viewport_dimensions[2]; |
| 780 |
glGetIntegerv(GL_MAX_VIEWPORT_DIMS, viewport_dimensions); |
| 781 |
|
| 782 |
gd->back_image.height = height; |
| 783 |
gd->back_image.width = width; |
| 784 |
|
| 785 |
assert(viewport_dimensions[0] >= gd->back_image.width); |
| 786 |
assert(viewport_dimensions[1] >= gd->back_image.height); |
| 787 |
|
| 788 |
gl_check_err(); |
| 789 |
} |
| 790 |
|
| 791 |
xcb_pixmap_t gl_release_image(backend_t *base, image_handle image) { |
| 792 |
auto inner = (struct gl_texture *)image; |
| 793 |
auto gd = (struct gl_data *)base; |
| 794 |
if (inner == &gd->back_image) { |
| 795 |
return XCB_NONE; |
| 796 |
} |
| 797 |
|
| 798 |
xcb_pixmap_t pixmap = inner->user_data ? inner->pixmap : XCB_NONE; |
| 799 |
if (inner->user_data) { |
| 800 |
gd->release_user_data(base, inner); |
| 801 |
} |
| 802 |
assert(inner->user_data == NULL); |
| 803 |
|
| 804 |
glDeleteTextures(1, &inner->texture); |
| 805 |
glDeleteTextures(2, inner->auxiliary_texture); |
| 806 |
free(inner); |
| 807 |
gl_check_err(); |
| 808 |
return pixmap; |
| 809 |
} |
| 810 |
|
| 811 |
static inline void gl_init_uniform_bitmask(struct gl_shader *shader) { |
| 812 |
GLint number_of_uniforms = 0; |
| 813 |
glGetProgramiv(shader->prog, GL_ACTIVE_UNIFORMS, &number_of_uniforms); |
| 814 |
for (int i = 0; i < number_of_uniforms; i++) { |
| 815 |
char name[32]; |
| 816 |
glGetActiveUniformName(shader->prog, (GLuint)i, sizeof(name), NULL, name); |
| 817 |
GLint loc = glGetUniformLocation(shader->prog, name); |
| 818 |
assert(loc >= 0 && loc < NUMBER_OF_UNIFORMS); |
| 819 |
shader->uniform_bitmask |= 1U << loc; |
| 820 |
} |
| 821 |
} |
| 822 |
|
| 823 |
struct glsl_parse_state { |
| 824 |
const char *cursor; |
| 825 |
bool last_char_is_space; |
| 826 |
}; |
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
static char glsl_next_char(struct glsl_parse_state *state) { |
| 831 |
while (true) { |
| 832 |
char curr = *state->cursor; |
| 833 |
|
| 834 |
int step = curr ? 1 : 0; |
| 835 |
|
| 836 |
if (curr == '/') { |
| 837 |
char next = state->cursor[1]; |
| 838 |
|
| 839 |
if (next == '*') { |
| 840 |
curr = ' '; |
| 841 |
for (step++; state->cursor[step]; step++) { |
| 842 |
if (state->cursor[step] == '*' && |
| 843 |
state->cursor[step + 1] == '/') { |
| 844 |
step += 2; |
| 845 |
break; |
| 846 |
} |
| 847 |
} |
| 848 |
} else if (next == '/') { |
| 849 |
curr = ' '; |
| 850 |
for (step++; state->cursor[step] && state->cursor[step] != '\n'; |
| 851 |
step++) { |
| 852 |
if (state->cursor[step] == '\\' && |
| 853 |
state->cursor[step + 1] == '\n') { |
| 854 |
step++; |
| 855 |
} |
| 856 |
} |
| 857 |
} |
| 858 |
} else if (curr == '\\') { |
| 859 |
char next = state->cursor[1]; |
| 860 |
if (next == '\n') { |
| 861 |
|
| 862 |
curr = state->cursor[2]; |
| 863 |
step = 3; |
| 864 |
} |
| 865 |
} else if (isspace(curr) && curr != '\n') { |
| 866 |
|
| 867 |
curr = ' '; |
| 868 |
} |
| 869 |
|
| 870 |
state->cursor += step; |
| 871 |
if (!state->last_char_is_space || curr != ' ') { |
| 872 |
|
| 873 |
|
| 874 |
state->last_char_is_space = (curr == ' '); |
| 875 |
return curr; |
| 876 |
} |
| 877 |
} |
| 878 |
} |
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
static const char *skip_glsl_header(const char *source) { |
| 885 |
struct glsl_parse_state p = { |
| 886 |
.cursor = source, |
| 887 |
.last_char_is_space = false, |
| 888 |
}; |
| 889 |
const char *begin_of_line = source; |
| 890 |
while (true) { |
| 891 |
char curr = glsl_next_char(&p); |
| 892 |
if (!curr) { |
| 893 |
return p.cursor; |
| 894 |
} |
| 895 |
if (curr == ' ') { |
| 896 |
continue; |
| 897 |
} |
| 898 |
if (curr == '\n') { |
| 899 |
begin_of_line = p.cursor; |
| 900 |
continue; |
| 901 |
} |
| 902 |
if (curr != '#') { |
| 903 |
|
| 904 |
|
| 905 |
return begin_of_line; |
| 906 |
} |
| 907 |
|
| 908 |
char next = glsl_next_char(&p); |
| 909 |
if (next == ' ') { |
| 910 |
next = glsl_next_char(&p); |
| 911 |
} |
| 912 |
if (next == '\n' || !next) { |
| 913 |
log_warn("Syntax error in shader: %s.", source); |
| 914 |
return NULL; |
| 915 |
} |
| 916 |
|
| 917 |
|
| 918 |
char directive[10]; |
| 919 |
size_t pos = 0; |
| 920 |
while (next && next != ' ' && next != '\n') { |
| 921 |
if (pos >= ARR_SIZE(directive) - 1) { |
| 922 |
log_warn("Invalid GLSL directive in shader: %s.", source); |
| 923 |
return NULL; |
| 924 |
} |
| 925 |
directive[pos++] = next; |
| 926 |
next = glsl_next_char(&p); |
| 927 |
} |
| 928 |
directive[pos++] = '\0'; |
| 929 |
if (strcmp(directive, "extension") != 0 && strcmp(directive, "version") != 0) { |
| 930 |
return begin_of_line; |
| 931 |
} |
| 932 |
while (next && next != '\n') { |
| 933 |
next = glsl_next_char(&p); |
| 934 |
} |
| 935 |
begin_of_line = p.cursor; |
| 936 |
} |
| 937 |
} |
| 938 |
|
| 939 |
TEST_CASE(glsl_parser) { |
| 940 |
const char *shader = "// aewr\n # version 110\n /* comment */ # extension\n/* " |
| 941 |
"# extension asdf\n#extension\n*/ int main() {}"; |
| 942 |
const char *end_of_header = skip_glsl_header(shader); |
| 943 |
TEST_EQUAL(end_of_header, &shader[50]); |
| 944 |
|
| 945 |
shader = "# vers\\\ni\\\non 11\\\n0 \nasdf// #extension \nasdf"; |
| 946 |
end_of_header = skip_glsl_header(shader); |
| 947 |
TEST_EQUAL(end_of_header, &shader[21]); |
| 948 |
|
| 949 |
shader = "#version 460\n asdf /*\n*/ #extension"; |
| 950 |
end_of_header = skip_glsl_header(shader); |
| 951 |
TEST_EQUAL(end_of_header, &shader[13]); |
| 952 |
|
| 953 |
shader = "#version 460\n#waeroihasdfnoae"; |
| 954 |
end_of_header = skip_glsl_header(shader); |
| 955 |
TEST_EQUAL(end_of_header, NULL); |
| 956 |
} |
| 957 |
|
| 958 |
static bool |
| 959 |
gl_create_window_shader_inner(struct gl_shader *out_shader, |
| 960 |
const struct shader_specification *spec, const char *source) { |
| 961 |
char *expanded_source = (char *)source; |
| 962 |
const char tmpl[] = "#define"; |
| 963 |
size_t defines_len = 0; |
| 964 |
struct shader_defines_iter def_it; |
| 965 |
if (spec && shader_spec_get_defines(spec, &def_it)) { |
| 966 |
const char *source_pos = skip_glsl_header(source); |
| 967 |
if (!source_pos) { |
| 968 |
|
| 969 |
source_pos = source; |
| 970 |
} |
| 971 |
|
| 972 |
long header_len = source_pos - source; |
| 973 |
|
| 974 |
do { |
| 975 |
|
| 976 |
defines_len += ARR_SIZE(tmpl) - 1 + 3 + strlen(def_it.name) + |
| 977 |
strlen(def_it.value); |
| 978 |
} while (shader_spec_defines_iter_next(spec, &def_it)); |
| 979 |
expanded_source = ccalloc(defines_len + strlen(source) + 1, char); |
| 980 |
|
| 981 |
char *pos = expanded_source; |
| 982 |
memcpy(pos, source, (size_t)header_len); |
| 983 |
pos += header_len; |
| 984 |
|
| 985 |
shader_spec_get_defines(spec, &def_it); |
| 986 |
do { |
| 987 |
int len = snprintf(pos, defines_len + 1, "%s %s %s\n", tmpl, |
| 988 |
def_it.name, def_it.value); |
| 989 |
BUG_ON(len < 0); |
| 990 |
assert((size_t)len <= defines_len); |
| 991 |
pos += len; |
| 992 |
defines_len -= (size_t)len; |
| 993 |
} while (shader_spec_defines_iter_next(spec, &def_it)); |
| 994 |
strcpy(pos, source_pos); |
| 995 |
} |
| 996 |
const char *vert[2] = {vertex_shader, NULL}; |
| 997 |
const char *frag[] = {blit_shader_glsl, masking_glsl, expanded_source, NULL}; |
| 998 |
|
| 999 |
bool succeeded = gl_shader_from_stringv(vert, frag, out_shader); |
| 1000 |
if (expanded_source != source) { |
| 1001 |
free(expanded_source); |
| 1002 |
} |
| 1003 |
if (!succeeded) { |
| 1004 |
return false; |
| 1005 |
} |
| 1006 |
|
| 1007 |
GLint viewport_dimensions[2]; |
| 1008 |
glGetIntegerv(GL_MAX_VIEWPORT_DIMS, viewport_dimensions); |
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
GLfloat projection_matrix[4][4] = {{2.0F / (GLfloat)viewport_dimensions[0], 0, 0, 0}, |
| 1014 |
{0, 2.0F / (GLfloat)viewport_dimensions[1], 0, 0}, |
| 1015 |
{0, 0, 0, 0}, |
| 1016 |
{-1, -1, 0, 1}}; |
| 1017 |
|
| 1018 |
glUseProgram(out_shader->prog); |
| 1019 |
glUniformMatrix4fv(UNIFORM_PROJECTION_LOC, 1, false, projection_matrix[0]); |
| 1020 |
glUseProgram(0); |
| 1021 |
|
| 1022 |
gl_init_uniform_bitmask(out_shader); |
| 1023 |
gl_check_err(); |
| 1024 |
|
| 1025 |
return true; |
| 1026 |
} |
| 1027 |
|
| 1028 |
void *gl_create_window_shader(backend_t *backend_data attr_unused, |
| 1029 |
const struct shader_specification *spec, const char *source) { |
| 1030 |
auto ret = ccalloc(1, struct gl_shader); |
| 1031 |
if (!gl_create_window_shader_inner(ret, spec, source)) { |
| 1032 |
free(ret); |
| 1033 |
return NULL; |
| 1034 |
} |
| 1035 |
return ret; |
| 1036 |
} |
| 1037 |
|
| 1038 |
uint64_t gl_get_shader_attributes(backend_t *backend_data attr_unused, shader_handle shader) { |
| 1039 |
auto win_shader = (struct gl_shader *)shader; |
| 1040 |
uint64_t ret = 0; |
| 1041 |
if (glGetUniformLocation(win_shader->prog, "time") >= 0) { |
| 1042 |
ret |= SHADER_ATTRIBUTE_ANIMATED; |
| 1043 |
} |
| 1044 |
return ret; |
| 1045 |
} |
| 1046 |
|
| 1047 |
static const struct { |
| 1048 |
GLint filter; |
| 1049 |
GLint wrap; |
| 1050 |
} gl_sampler_params[] = { |
| 1051 |
[GL_SAMPLER_REPEAT] = {GL_NEAREST, GL_REPEAT}, |
| 1052 |
[GL_SAMPLER_REPEAT_SCALE] = {GL_LINEAR, GL_REPEAT}, |
| 1053 |
[GL_SAMPLER_BLUR] = {GL_LINEAR, GL_CLAMP_TO_EDGE}, |
| 1054 |
[GL_SAMPLER_EDGE] = {GL_NEAREST, GL_CLAMP_TO_EDGE}, |
| 1055 |
[GL_SAMPLER_BORDER] = {GL_NEAREST, GL_CLAMP_TO_BORDER}, |
| 1056 |
}; |
| 1057 |
|
| 1058 |
bool gl_init(struct gl_data *gd, session_t *ps) { |
| 1059 |
if (!epoxy_has_gl_extension("GL_ARB_explicit_uniform_location")) { |
| 1060 |
log_error("GL_ARB_explicit_uniform_location support is required but " |
| 1061 |
"missing."); |
| 1062 |
return false; |
| 1063 |
} |
| 1064 |
glGenQueries(2, gd->frame_timing); |
| 1065 |
gd->current_frame_timing = 0; |
| 1066 |
|
| 1067 |
glGenBuffers(4, gd->buffer_objects); |
| 1068 |
glGenVertexArrays(2, gd->vertex_array_objects); |
| 1069 |
|
| 1070 |
|
| 1071 |
glDisable(GL_DEPTH_TEST); |
| 1072 |
glDepthMask(GL_FALSE); |
| 1073 |
|
| 1074 |
glEnable(GL_BLEND); |
| 1075 |
|
| 1076 |
|
| 1077 |
glDisable(GL_STENCIL_TEST); |
| 1078 |
glStencilMask(0x1); |
| 1079 |
glStencilFunc(GL_EQUAL, 0x1, 0x1); |
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
GLint viewport_dimensions[2]; |
| 1086 |
glGetIntegerv(GL_MAX_VIEWPORT_DIMS, viewport_dimensions); |
| 1087 |
glViewport(0, 0, viewport_dimensions[0], viewport_dimensions[1]); |
| 1088 |
|
| 1089 |
|
| 1090 |
glClearColor(0.0F, 0.0F, 0.0F, 1.0F); |
| 1091 |
glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); |
| 1092 |
|
| 1093 |
glGenFramebuffers(1, &gd->temp_fbo); |
| 1094 |
|
| 1095 |
gd->default_mask_texture = gl_new_texture(); |
| 1096 |
if (!gd->default_mask_texture) { |
| 1097 |
log_error("Failed to generate a default mask texture"); |
| 1098 |
return false; |
| 1099 |
} |
| 1100 |
|
| 1101 |
glBindTexture(GL_TEXTURE_2D, gd->default_mask_texture); |
| 1102 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, 1, 1, 0, GL_RED, GL_UNSIGNED_BYTE, |
| 1103 |
(GLubyte[]){0xff}); |
| 1104 |
glBindTexture(GL_TEXTURE_2D, 0); |
| 1105 |
|
| 1106 |
|
| 1107 |
if (!gl_create_window_shader_inner(&gd->default_shader, NULL, blit_shader_default)) { |
| 1108 |
log_error("Failed to create window shaders"); |
| 1109 |
return false; |
| 1110 |
} |
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
GLfloat projection_matrix[4][4] = {{2.0F / (GLfloat)viewport_dimensions[0], 0, 0, 0}, |
| 1116 |
{0, 2.0F / (GLfloat)viewport_dimensions[1], 0, 0}, |
| 1117 |
{0, 0, 0, 0}, |
| 1118 |
{-1, -1, 0, 1}}; |
| 1119 |
|
| 1120 |
gd->fill_shader.prog = gl_create_program_from_str(fill_vert, fill_frag); |
| 1121 |
gd->fill_shader.uniform_bitmask = (uint32_t)-1; |
| 1122 |
|
| 1123 |
glUseProgram(gd->fill_shader.prog); |
| 1124 |
glUniformMatrix4fv(UNIFORM_PROJECTION_LOC, 1, false, projection_matrix[0]); |
| 1125 |
glUseProgram(0); |
| 1126 |
|
| 1127 |
gd->dithered_present = ps->o.dithered_present; |
| 1128 |
gd->copy_area_prog.prog = gl_create_program_from_strv( |
| 1129 |
(const char *[]){vertex_shader, NULL}, (const char *[]){copy_area_frag, NULL}); |
| 1130 |
if (!gd->copy_area_prog.prog) { |
| 1131 |
log_error("Failed to create the copy_area shader"); |
| 1132 |
return false; |
| 1133 |
} |
| 1134 |
gd->copy_area_prog.uniform_bitmask = (uint32_t)-1; |
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
glUseProgram(gd->copy_area_prog.prog); |
| 1139 |
glUniform1i(UNIFORM_TEX_LOC, 0); |
| 1140 |
glUniformMatrix4fv(UNIFORM_PROJECTION_LOC, 1, false, projection_matrix[0]); |
| 1141 |
|
| 1142 |
gd->copy_area_with_dither_prog.prog = gl_create_program_from_strv( |
| 1143 |
(const char *[]){vertex_shader, NULL}, |
| 1144 |
(const char *[]){copy_area_with_dither_frag, dither_glsl, NULL}); |
| 1145 |
if (!gd->copy_area_with_dither_prog.prog) { |
| 1146 |
log_error("Failed to create the copy_area with dither shader"); |
| 1147 |
return false; |
| 1148 |
} |
| 1149 |
gd->copy_area_with_dither_prog.uniform_bitmask = (uint32_t)-1; |
| 1150 |
|
| 1151 |
glUseProgram(gd->copy_area_with_dither_prog.prog); |
| 1152 |
glUniform1i(UNIFORM_TEX_LOC, 0); |
| 1153 |
glUniformMatrix4fv(UNIFORM_PROJECTION_LOC, 1, false, projection_matrix[0]); |
| 1154 |
|
| 1155 |
gd->brightness_shader.prog = |
| 1156 |
gl_create_program_from_str(interpolating_vert, interpolating_frag); |
| 1157 |
if (!gd->brightness_shader.prog) { |
| 1158 |
log_error("Failed to create the brightness shader"); |
| 1159 |
return false; |
| 1160 |
} |
| 1161 |
glUseProgram(gd->brightness_shader.prog); |
| 1162 |
glUniform1i(UNIFORM_TEX_LOC, 0); |
| 1163 |
glUniformMatrix4fv(UNIFORM_PROJECTION_LOC, 1, false, projection_matrix[0]); |
| 1164 |
glUseProgram(0); |
| 1165 |
|
| 1166 |
gd->back_image.width = ps->root_width; |
| 1167 |
gd->back_image.height = ps->root_height; |
| 1168 |
|
| 1169 |
glGenSamplers(GL_MAX_SAMPLERS, gd->samplers); |
| 1170 |
for (size_t i = 0; i < ARR_SIZE(gl_sampler_params); i++) { |
| 1171 |
glSamplerParameteri(gd->samplers[i], GL_TEXTURE_MIN_FILTER, |
| 1172 |
gl_sampler_params[i].filter); |
| 1173 |
glSamplerParameteri(gd->samplers[i], GL_TEXTURE_MAG_FILTER, |
| 1174 |
gl_sampler_params[i].filter); |
| 1175 |
glSamplerParameteri(gd->samplers[i], GL_TEXTURE_WRAP_S, |
| 1176 |
gl_sampler_params[i].wrap); |
| 1177 |
glSamplerParameteri(gd->samplers[i], GL_TEXTURE_WRAP_T, |
| 1178 |
gl_sampler_params[i].wrap); |
| 1179 |
} |
| 1180 |
|
| 1181 |
gd->logger = gl_string_marker_logger_new(); |
| 1182 |
if (gd->logger) { |
| 1183 |
log_add_target_tls(gd->logger); |
| 1184 |
} |
| 1185 |
|
| 1186 |
const char *vendor = (const char *)glGetString(GL_VENDOR); |
| 1187 |
log_debug("GL_VENDOR = %s", vendor); |
| 1188 |
if (strcmp(vendor, "NVIDIA Corporation") == 0) { |
| 1189 |
log_info("GL vendor is NVIDIA, enable xrender sync fence."); |
| 1190 |
gd->is_nvidia = true; |
| 1191 |
} else { |
| 1192 |
gd->is_nvidia = false; |
| 1193 |
} |
| 1194 |
gd->has_robustness = epoxy_has_gl_extension("GL_ARB_robustness"); |
| 1195 |
gd->has_egl_image_storage = epoxy_has_gl_extension("GL_EXT_EGL_image_storage"); |
| 1196 |
gd->back_image.y_inverted = false; |
| 1197 |
|
| 1198 |
gl_check_err(); |
| 1199 |
|
| 1200 |
return true; |
| 1201 |
} |
| 1202 |
|
| 1203 |
void gl_deinit(struct gl_data *gd) { |
| 1204 |
if (gd->logger) { |
| 1205 |
log_remove_target_tls(gd->logger); |
| 1206 |
gd->logger = NULL; |
| 1207 |
} |
| 1208 |
|
| 1209 |
gl_destroy_window_shader_inner(&gd->default_shader); |
| 1210 |
glDeleteProgram(gd->copy_area_prog.prog); |
| 1211 |
glDeleteProgram(gd->copy_area_with_dither_prog.prog); |
| 1212 |
gd->copy_area_prog.prog = 0; |
| 1213 |
gd->copy_area_with_dither_prog.prog = 0; |
| 1214 |
|
| 1215 |
glDeleteProgram(gd->fill_shader.prog); |
| 1216 |
glDeleteProgram(gd->brightness_shader.prog); |
| 1217 |
gd->fill_shader.prog = 0; |
| 1218 |
gd->brightness_shader.prog = 0; |
| 1219 |
|
| 1220 |
glDeleteTextures(1, &gd->default_mask_texture); |
| 1221 |
|
| 1222 |
for (int i = 0; i < GL_MAX_SAMPLERS; i++) { |
| 1223 |
glDeleteSamplers(1, &gd->samplers[i]); |
| 1224 |
} |
| 1225 |
|
| 1226 |
glDeleteFramebuffers(1, &gd->temp_fbo); |
| 1227 |
|
| 1228 |
glDeleteBuffers(4, gd->buffer_objects); |
| 1229 |
glDeleteVertexArrays(2, gd->vertex_array_objects); |
| 1230 |
|
| 1231 |
glDeleteQueries(2, gd->frame_timing); |
| 1232 |
|
| 1233 |
gl_check_err(); |
| 1234 |
} |
| 1235 |
|
| 1236 |
GLuint gl_new_texture(void) { |
| 1237 |
GLuint texture; |
| 1238 |
glGenTextures(1, &texture); |
| 1239 |
if (!texture) { |
| 1240 |
log_error("Failed to generate texture"); |
| 1241 |
return 0; |
| 1242 |
} |
| 1243 |
return texture; |
| 1244 |
} |
| 1245 |
|
| 1246 |
bool gl_clear(backend_t *backend_data, image_handle target, struct color color) { |
| 1247 |
auto gd = (struct gl_data *)backend_data; |
| 1248 |
auto fbo = gl_bind_image_to_fbo(gd, target); |
| 1249 |
auto target_image = (struct gl_texture *)target; |
| 1250 |
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo); |
| 1251 |
if (target_image->format == BACKEND_IMAGE_FORMAT_MASK) { |
| 1252 |
glClearColor((GLfloat)color.alpha, 0, 0, 1); |
| 1253 |
} else { |
| 1254 |
glClearColor((GLfloat)color.red, (GLfloat)color.green, |
| 1255 |
(GLfloat)color.blue, (GLfloat)color.alpha); |
| 1256 |
} |
| 1257 |
glClear(GL_COLOR_BUFFER_BIT); |
| 1258 |
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); |
| 1259 |
return true; |
| 1260 |
} |
| 1261 |
|
| 1262 |
image_handle gl_back_buffer(struct backend_base *base) { |
| 1263 |
auto gd = (struct gl_data *)base; |
| 1264 |
return (image_handle)&gd->back_image; |
| 1265 |
} |
| 1266 |
|
| 1267 |
image_handle gl_new_image(backend_t *backend_data attr_unused, |
| 1268 |
enum backend_image_format format, ivec2 size) { |
| 1269 |
auto tex = ccalloc(1, struct gl_texture); |
| 1270 |
log_trace("Creating texture %dx%d", size.width, size.height); |
| 1271 |
tex->format = format; |
| 1272 |
tex->width = size.width; |
| 1273 |
tex->height = size.height; |
| 1274 |
tex->texture = gl_new_texture(); |
| 1275 |
tex->y_inverted = true; |
| 1276 |
tex->user_data = NULL; |
| 1277 |
tex->pixmap = XCB_NONE; |
| 1278 |
|
| 1279 |
GLint gl_format; |
| 1280 |
switch (format) { |
| 1281 |
case BACKEND_IMAGE_FORMAT_PIXMAP: gl_format = GL_RGBA8; break; |
| 1282 |
case BACKEND_IMAGE_FORMAT_PIXMAP_HIGH: gl_format = GL_RGBA16; break; |
| 1283 |
case BACKEND_IMAGE_FORMAT_MASK: gl_format = GL_R8; break; |
| 1284 |
} |
| 1285 |
glActiveTexture(GL_TEXTURE0); |
| 1286 |
glBindTexture(GL_TEXTURE_2D, tex->texture); |
| 1287 |
glTexImage2D(GL_TEXTURE_2D, 0, gl_format, size.width, size.height, 0, GL_RGBA, |
| 1288 |
GL_UNSIGNED_BYTE, NULL); |
| 1289 |
if (format == BACKEND_IMAGE_FORMAT_MASK) { |
| 1290 |
|
| 1291 |
|
| 1292 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); |
| 1293 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); |
| 1294 |
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, |
| 1295 |
(GLfloat[]){0, 0, 0, 0}); |
| 1296 |
|
| 1297 |
|
| 1298 |
glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, |
| 1299 |
(GLint[]){GL_RED, GL_RED, GL_RED, GL_RED}); |
| 1300 |
} |
| 1301 |
glBindTexture(GL_TEXTURE_2D, 0); |
| 1302 |
return (image_handle)tex; |
| 1303 |
} |
| 1304 |
|
| 1305 |
bool gl_apply_alpha(backend_t *base, image_handle target, double alpha, const region_t *reg_op) { |
| 1306 |
auto gd = (struct gl_data *)base; |
| 1307 |
static const struct gl_vertex_attribs_definition vertex_attribs = { |
| 1308 |
.stride = sizeof(GLfloat) * 4, |
| 1309 |
.count = 1, |
| 1310 |
.attribs = {{GL_FLOAT, vert_coord_loc, NULL}}, |
| 1311 |
}; |
| 1312 |
if (alpha == 1.0 || !pixman_region32_not_empty(reg_op)) { |
| 1313 |
return true; |
| 1314 |
} |
| 1315 |
gl_bind_image_to_fbo(gd, target); |
| 1316 |
|
| 1317 |
glBlendFunc(GL_ZERO, GL_CONSTANT_ALPHA); |
| 1318 |
glBlendColor(0, 0, 0, (GLclampf)alpha); |
| 1319 |
|
| 1320 |
int nrects; |
| 1321 |
const rect_t *rect = pixman_region32_rectangles(reg_op, &nrects); |
| 1322 |
|
| 1323 |
auto coord = ccalloc(nrects * 16, GLfloat); |
| 1324 |
auto indices = ccalloc(nrects * 6, GLuint); |
| 1325 |
|
| 1326 |
struct gl_uniform_value uniforms[] = { |
| 1327 |
[UNIFORM_COLOR_LOC] = {.type = GL_FLOAT_VEC4, .f4 = {0, 0, 0, 0}}, |
| 1328 |
}; |
| 1329 |
gl_mask_rects_to_coords_simple(nrects, rect, coord, indices); |
| 1330 |
gl_blit_inner(gd, gd->temp_fbo, nrects, coord, indices, &vertex_attribs, |
| 1331 |
&gd->fill_shader, ARR_SIZE(uniforms), uniforms); |
| 1332 |
free(indices); |
| 1333 |
free(coord); |
| 1334 |
|
| 1335 |
gl_check_err(); |
| 1336 |
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); |
| 1337 |
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); |
| 1338 |
return true; |
| 1339 |
} |
| 1340 |
|
| 1341 |
bool gl_last_render_time(backend_t *base, struct timespec *ts) { |
| 1342 |
auto gd = (struct gl_data *)base; |
| 1343 |
GLint available = 0; |
| 1344 |
glGetQueryObjectiv(gd->frame_timing[gd->current_frame_timing ^ 1], |
| 1345 |
GL_QUERY_RESULT_AVAILABLE, &available); |
| 1346 |
if (!available) { |
| 1347 |
return false; |
| 1348 |
} |
| 1349 |
|
| 1350 |
GLuint64 time; |
| 1351 |
glGetQueryObjectui64v(gd->frame_timing[gd->current_frame_timing ^ 1], |
| 1352 |
GL_QUERY_RESULT, &time); |
| 1353 |
ts->tv_sec = (long)(time / 1000000000); |
| 1354 |
ts->tv_nsec = (long)(time % 1000000000); |
| 1355 |
gl_check_err(); |
| 1356 |
return true; |
| 1357 |
} |
| 1358 |
|
| 1359 |
enum device_status gl_device_status(backend_t *base) { |
| 1360 |
auto gd = (struct gl_data *)base; |
| 1361 |
if (!gd->has_robustness) { |
| 1362 |
return DEVICE_STATUS_NORMAL; |
| 1363 |
} |
| 1364 |
if (glGetGraphicsResetStatusARB() == GL_NO_ERROR) { |
| 1365 |
return DEVICE_STATUS_NORMAL; |
| 1366 |
} |
| 1367 |
return DEVICE_STATUS_RESETTING; |
| 1368 |
} |