| 1 |
/* |
| 2 |
* Compton - a compositor for X11 |
| 3 |
* |
| 4 |
* Based on `xcompmgr` - Copyright (c) 2003, Keith Packard |
| 5 |
* |
| 6 |
* Copyright (c) 2011-2013, Christopher Jeffrey |
| 7 |
* See LICENSE for more information. |
| 8 |
* |
| 9 |
*/ |
| 10 |
|
| 11 |
#include "opengl.h" |
| 12 |
|
| 13 |
#ifdef CONFIG_GLX_SYNC |
| 14 |
void |
| 15 |
xr_glx_sync(session_t *ps, Drawable d, XSyncFence *pfence) { |
| 16 |
if (*pfence) { |
| 17 |
// GLsync sync = ps->psglx->glFenceSyncProc(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); |
| 18 |
GLsync sync = ps->psglx->glImportSyncEXT(GL_SYNC_X11_FENCE_EXT, *pfence, 0); |
| 19 |
/* GLenum ret = ps->psglx->glClientWaitSyncProc(sync, GL_SYNC_FLUSH_COMMANDS_BIT, |
| 20 |
1000); |
| 21 |
assert(GL_CONDITION_SATISFIED == ret); */ |
| 22 |
XSyncTriggerFence(ps->dpy, *pfence); |
| 23 |
XFlush(ps->dpy); |
| 24 |
ps->psglx->glWaitSyncProc(sync, 0, GL_TIMEOUT_IGNORED); |
| 25 |
// ps->psglx->glDeleteSyncProc(sync); |
| 26 |
// XSyncResetFence(ps->dpy, *pfence); |
| 27 |
} |
| 28 |
glx_check_err(ps); |
| 29 |
} |
| 30 |
#endif |
| 31 |
|
| 32 |
static inline GLXFBConfig |
| 33 |
get_fbconfig_from_visualinfo(session_t *ps, const XVisualInfo *visualinfo) { |
| 34 |
int nelements = 0; |
| 35 |
GLXFBConfig *fbconfigs = glXGetFBConfigs(ps->dpy, visualinfo->screen, |
| 36 |
&nelements); |
| 37 |
for (int i = 0; i < nelements; ++i) { |
| 38 |
int visual_id = 0; |
| 39 |
if (Success == glXGetFBConfigAttrib(ps->dpy, fbconfigs[i], GLX_VISUAL_ID, &visual_id) |
| 40 |
&& visual_id == visualinfo->visualid) |
| 41 |
return fbconfigs[i]; |
| 42 |
} |
| 43 |
|
| 44 |
return NULL; |
| 45 |
} |
| 46 |
|
| 47 |
#ifdef DEBUG_GLX_DEBUG_CONTEXT |
| 48 |
static void |
| 49 |
glx_debug_msg_callback(GLenum source, GLenum type, |
| 50 |
GLuint id, GLenum severity, GLsizei length, const GLchar *message, |
| 51 |
GLvoid *userParam) { |
| 52 |
printf_dbgf("(): source 0x%04X, type 0x%04X, id %u, severity 0x%0X, \"%s\"\n", |
| 53 |
source, type, id, severity, message); |
| 54 |
} |
| 55 |
#endif |
| 56 |
|
| 57 |
/** |
| 58 |
* Initialize OpenGL. |
| 59 |
*/ |
| 60 |
bool |
| 61 |
glx_init(session_t *ps, bool need_render) { |
| 62 |
bool success = false; |
| 63 |
XVisualInfo *pvis = NULL; |
| 64 |
|
| 65 |
// Check for GLX extension |
| 66 |
if (!ps->glx_exists) { |
| 67 |
if (glXQueryExtension(ps->dpy, &ps->glx_event, &ps->glx_error)) |
| 68 |
ps->glx_exists = true; |
| 69 |
else { |
| 70 |
printf_errf("(): No GLX extension."); |
| 71 |
goto glx_init_end; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
// Get XVisualInfo |
| 76 |
pvis = get_visualinfo_from_visual(ps, ps->vis); |
| 77 |
if (!pvis) { |
| 78 |
printf_errf("(): Failed to acquire XVisualInfo for current visual."); |
| 79 |
goto glx_init_end; |
| 80 |
} |
| 81 |
|
| 82 |
// Ensure the visual is double-buffered |
| 83 |
if (need_render) { |
| 84 |
int value = 0; |
| 85 |
if (Success != glXGetConfig(ps->dpy, pvis, GLX_USE_GL, &value) || !value) { |
| 86 |
printf_errf("(): Root visual is not a GL visual."); |
| 87 |
goto glx_init_end; |
| 88 |
} |
| 89 |
|
| 90 |
if (Success != glXGetConfig(ps->dpy, pvis, GLX_DOUBLEBUFFER, &value) |
| 91 |
|| !value) { |
| 92 |
printf_errf("(): Root visual is not a double buffered GL visual."); |
| 93 |
goto glx_init_end; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
// Ensure GLX_EXT_texture_from_pixmap exists |
| 98 |
if (need_render && !glx_hasglxext(ps, "GLX_EXT_texture_from_pixmap")) |
| 99 |
goto glx_init_end; |
| 100 |
|
| 101 |
// Initialize GLX data structure |
| 102 |
if (!ps->psglx) { |
| 103 |
static const glx_session_t CGLX_SESSION_DEF = CGLX_SESSION_INIT; |
| 104 |
ps->psglx = cmalloc(1, glx_session_t); |
| 105 |
memcpy(ps->psglx, &CGLX_SESSION_DEF, sizeof(glx_session_t)); |
| 106 |
|
| 107 |
#ifdef CONFIG_VSYNC_OPENGL_GLSL |
| 108 |
for (int i = 0; i < MAX_BLUR_PASS; ++i) { |
| 109 |
glx_blur_pass_t *ppass = &ps->psglx->blur_passes[i]; |
| 110 |
ppass->unifm_factor_center = -1; |
| 111 |
ppass->unifm_offset_x = -1; |
| 112 |
ppass->unifm_offset_y = -1; |
| 113 |
} |
| 114 |
#endif |
| 115 |
} |
| 116 |
|
| 117 |
glx_session_t *psglx = ps->psglx; |
| 118 |
|
| 119 |
if (!psglx->context) { |
| 120 |
// Get GLX context |
| 121 |
#ifndef DEBUG_GLX_DEBUG_CONTEXT |
| 122 |
psglx->context = glXCreateContext(ps->dpy, pvis, None, GL_TRUE); |
| 123 |
#else |
| 124 |
{ |
| 125 |
GLXFBConfig fbconfig = get_fbconfig_from_visualinfo(ps, pvis); |
| 126 |
if (!fbconfig) { |
| 127 |
printf_errf("(): Failed to get GLXFBConfig for root visual %#lx.", |
| 128 |
pvis->visualid); |
| 129 |
goto glx_init_end; |
| 130 |
} |
| 131 |
|
| 132 |
f_glXCreateContextAttribsARB p_glXCreateContextAttribsARB = |
| 133 |
(f_glXCreateContextAttribsARB) |
| 134 |
glXGetProcAddress((const GLubyte *) "glXCreateContextAttribsARB"); |
| 135 |
if (!p_glXCreateContextAttribsARB) { |
| 136 |
printf_errf("(): Failed to get glXCreateContextAttribsARB()."); |
| 137 |
goto glx_init_end; |
| 138 |
} |
| 139 |
|
| 140 |
static const int attrib_list[] = { |
| 141 |
GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_DEBUG_BIT_ARB, |
| 142 |
None |
| 143 |
}; |
| 144 |
psglx->context = p_glXCreateContextAttribsARB(ps->dpy, fbconfig, NULL, |
| 145 |
GL_TRUE, attrib_list); |
| 146 |
} |
| 147 |
#endif |
| 148 |
|
| 149 |
if (!psglx->context) { |
| 150 |
printf_errf("(): Failed to get GLX context."); |
| 151 |
goto glx_init_end; |
| 152 |
} |
| 153 |
|
| 154 |
// Attach GLX context |
| 155 |
if (!glXMakeCurrent(ps->dpy, get_tgt_window(ps), psglx->context)) { |
| 156 |
printf_errf("(): Failed to attach GLX context."); |
| 157 |
goto glx_init_end; |
| 158 |
} |
| 159 |
|
| 160 |
#ifdef DEBUG_GLX_DEBUG_CONTEXT |
| 161 |
{ |
| 162 |
f_DebugMessageCallback p_DebugMessageCallback = |
| 163 |
(f_DebugMessageCallback) |
| 164 |
glXGetProcAddress((const GLubyte *) "glDebugMessageCallback"); |
| 165 |
if (!p_DebugMessageCallback) { |
| 166 |
printf_errf("(): Failed to get glDebugMessageCallback(0."); |
| 167 |
goto glx_init_end; |
| 168 |
} |
| 169 |
p_DebugMessageCallback(glx_debug_msg_callback, ps); |
| 170 |
} |
| 171 |
#endif |
| 172 |
|
| 173 |
} |
| 174 |
|
| 175 |
// Ensure we have a stencil buffer. X Fixes does not guarantee rectangles |
| 176 |
// in regions don't overlap, so we must use stencil buffer to make sure |
| 177 |
// we don't paint a region for more than one time, I think? |
| 178 |
if (need_render && !ps->o.glx_no_stencil) { |
| 179 |
GLint val = 0; |
| 180 |
glGetIntegerv(GL_STENCIL_BITS, &val); |
| 181 |
if (!val) { |
| 182 |
printf_errf("(): Target window doesn't have stencil buffer."); |
| 183 |
goto glx_init_end; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
// Check GL_ARB_texture_non_power_of_two, requires a GLX context and |
| 188 |
// must precede FBConfig fetching |
| 189 |
if (need_render) |
| 190 |
psglx->has_texture_non_power_of_two = glx_hasglext(ps, |
| 191 |
"GL_ARB_texture_non_power_of_two"); |
| 192 |
|
| 193 |
// Acquire function addresses |
| 194 |
if (need_render) { |
| 195 |
#ifdef DEBUG_GLX_MARK |
| 196 |
psglx->glStringMarkerGREMEDY = (f_StringMarkerGREMEDY) |
| 197 |
glXGetProcAddress((const GLubyte *) "glStringMarkerGREMEDY"); |
| 198 |
psglx->glFrameTerminatorGREMEDY = (f_FrameTerminatorGREMEDY) |
| 199 |
glXGetProcAddress((const GLubyte *) "glFrameTerminatorGREMEDY"); |
| 200 |
#endif |
| 201 |
|
| 202 |
psglx->glXBindTexImageProc = (f_BindTexImageEXT) |
| 203 |
glXGetProcAddress((const GLubyte *) "glXBindTexImageEXT"); |
| 204 |
psglx->glXReleaseTexImageProc = (f_ReleaseTexImageEXT) |
| 205 |
glXGetProcAddress((const GLubyte *) "glXReleaseTexImageEXT"); |
| 206 |
if (!psglx->glXBindTexImageProc || !psglx->glXReleaseTexImageProc) { |
| 207 |
printf_errf("(): Failed to acquire glXBindTexImageEXT() / glXReleaseTexImageEXT()."); |
| 208 |
goto glx_init_end; |
| 209 |
} |
| 210 |
|
| 211 |
if (ps->o.glx_use_copysubbuffermesa) { |
| 212 |
psglx->glXCopySubBufferProc = (f_CopySubBuffer) |
| 213 |
glXGetProcAddress((const GLubyte *) "glXCopySubBufferMESA"); |
| 214 |
if (!psglx->glXCopySubBufferProc) { |
| 215 |
printf_errf("(): Failed to acquire glXCopySubBufferMESA()."); |
| 216 |
goto glx_init_end; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
#ifdef CONFIG_GLX_SYNC |
| 221 |
psglx->glFenceSyncProc = (f_FenceSync) |
| 222 |
glXGetProcAddress((const GLubyte *) "glFenceSync"); |
| 223 |
psglx->glIsSyncProc = (f_IsSync) |
| 224 |
glXGetProcAddress((const GLubyte *) "glIsSync"); |
| 225 |
psglx->glDeleteSyncProc = (f_DeleteSync) |
| 226 |
glXGetProcAddress((const GLubyte *) "glDeleteSync"); |
| 227 |
psglx->glClientWaitSyncProc = (f_ClientWaitSync) |
| 228 |
glXGetProcAddress((const GLubyte *) "glClientWaitSync"); |
| 229 |
psglx->glWaitSyncProc = (f_WaitSync) |
| 230 |
glXGetProcAddress((const GLubyte *) "glWaitSync"); |
| 231 |
psglx->glImportSyncEXT = (f_ImportSyncEXT) |
| 232 |
glXGetProcAddress((const GLubyte *) "glImportSyncEXT"); |
| 233 |
if (!psglx->glFenceSyncProc || !psglx->glIsSyncProc || !psglx->glDeleteSyncProc |
| 234 |
|| !psglx->glClientWaitSyncProc || !psglx->glWaitSyncProc |
| 235 |
|| !psglx->glImportSyncEXT) { |
| 236 |
printf_errf("(): Failed to acquire GLX sync functions."); |
| 237 |
goto glx_init_end; |
| 238 |
} |
| 239 |
#endif |
| 240 |
} |
| 241 |
|
| 242 |
// Acquire FBConfigs |
| 243 |
if (need_render && !glx_update_fbconfig(ps)) |
| 244 |
goto glx_init_end; |
| 245 |
|
| 246 |
// Render preparations |
| 247 |
if (need_render) { |
| 248 |
glx_on_root_change(ps); |
| 249 |
|
| 250 |
glDisable(GL_DEPTH_TEST); |
| 251 |
glDepthMask(GL_FALSE); |
| 252 |
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
| 253 |
glDisable(GL_BLEND); |
| 254 |
|
| 255 |
if (!ps->o.glx_no_stencil) { |
| 256 |
// Initialize stencil buffer |
| 257 |
glClear(GL_STENCIL_BUFFER_BIT); |
| 258 |
glDisable(GL_STENCIL_TEST); |
| 259 |
glStencilMask(0x1); |
| 260 |
glStencilFunc(GL_EQUAL, 0x1, 0x1); |
| 261 |
} |
| 262 |
|
| 263 |
// Clear screen |
| 264 |
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); |
| 265 |
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 266 |
// glXSwapBuffers(ps->dpy, get_tgt_window(ps)); |
| 267 |
} |
| 268 |
|
| 269 |
success = true; |
| 270 |
|
| 271 |
glx_init_end: |
| 272 |
cxfree(pvis); |
| 273 |
|
| 274 |
if (!success) |
| 275 |
glx_destroy(ps); |
| 276 |
|
| 277 |
return success; |
| 278 |
} |
| 279 |
|
| 280 |
#ifdef CONFIG_VSYNC_OPENGL_GLSL |
| 281 |
|
| 282 |
static void |
| 283 |
glx_free_prog_main(session_t *ps, glx_prog_main_t *pprogram) { |
| 284 |
if (!pprogram) |
| 285 |
return; |
| 286 |
if (pprogram->prog) { |
| 287 |
glDeleteProgram(pprogram->prog); |
| 288 |
pprogram->prog = 0; |
| 289 |
} |
| 290 |
pprogram->unifm_opacity = -1; |
| 291 |
pprogram->unifm_invert_color = -1; |
| 292 |
pprogram->unifm_tex = -1; |
| 293 |
} |
| 294 |
|
| 295 |
#endif |
| 296 |
|
| 297 |
/** |
| 298 |
* Destroy GLX related resources. |
| 299 |
*/ |
| 300 |
void |
| 301 |
glx_destroy(session_t *ps) { |
| 302 |
if (!ps->psglx) |
| 303 |
return; |
| 304 |
|
| 305 |
// Free all GLX resources of windows |
| 306 |
for (win *w = ps->list; w; w = w->next) |
| 307 |
free_win_res_glx(ps, w); |
| 308 |
|
| 309 |
#ifdef CONFIG_VSYNC_OPENGL_GLSL |
| 310 |
// Free GLSL shaders/programs |
| 311 |
for (int i = 0; i < MAX_BLUR_PASS; ++i) { |
| 312 |
glx_blur_pass_t *ppass = &ps->psglx->blur_passes[i]; |
| 313 |
if (ppass->frag_shader) |
| 314 |
glDeleteShader(ppass->frag_shader); |
| 315 |
if (ppass->prog) |
| 316 |
glDeleteProgram(ppass->prog); |
| 317 |
} |
| 318 |
|
| 319 |
glx_free_prog_main(ps, &ps->o.glx_prog_win); |
| 320 |
|
| 321 |
glx_check_err(ps); |
| 322 |
#endif |
| 323 |
|
| 324 |
// Free FBConfigs |
| 325 |
for (int i = 0; i <= OPENGL_MAX_DEPTH; ++i) { |
| 326 |
free(ps->psglx->fbconfigs[i]); |
| 327 |
ps->psglx->fbconfigs[i] = NULL; |
| 328 |
} |
| 329 |
|
| 330 |
// Destroy GLX context |
| 331 |
if (ps->psglx->context) { |
| 332 |
glXDestroyContext(ps->dpy, ps->psglx->context); |
| 333 |
ps->psglx->context = NULL; |
| 334 |
} |
| 335 |
|
| 336 |
free(ps->psglx); |
| 337 |
ps->psglx = NULL; |
| 338 |
} |
| 339 |
|
| 340 |
/** |
| 341 |
* Reinitialize GLX. |
| 342 |
*/ |
| 343 |
bool |
| 344 |
glx_reinit(session_t *ps, bool need_render) { |
| 345 |
// Reinitialize VSync as well |
| 346 |
vsync_deinit(ps); |
| 347 |
|
| 348 |
glx_destroy(ps); |
| 349 |
if (!glx_init(ps, need_render)) { |
| 350 |
printf_errf("(): Failed to initialize GLX."); |
| 351 |
return false; |
| 352 |
} |
| 353 |
|
| 354 |
if (!vsync_init(ps)) { |
| 355 |
printf_errf("(): Failed to initialize VSync."); |
| 356 |
return false; |
| 357 |
} |
| 358 |
|
| 359 |
return true; |
| 360 |
} |
| 361 |
|
| 362 |
/** |
| 363 |
* Callback to run on root window size change. |
| 364 |
*/ |
| 365 |
void |
| 366 |
glx_on_root_change(session_t *ps) { |
| 367 |
glViewport(0, 0, ps->root_width, ps->root_height); |
| 368 |
|
| 369 |
// Initialize matrix, copied from dcompmgr |
| 370 |
glMatrixMode(GL_PROJECTION); |
| 371 |
glLoadIdentity(); |
| 372 |
glOrtho(0, ps->root_width, 0, ps->root_height, -1000.0, 1000.0); |
| 373 |
glMatrixMode(GL_MODELVIEW); |
| 374 |
glLoadIdentity(); |
| 375 |
} |
| 376 |
|
| 377 |
/** |
| 378 |
* Initialize GLX blur filter. |
| 379 |
*/ |
| 380 |
bool |
| 381 |
glx_init_blur(session_t *ps) { |
| 382 |
assert(ps->o.blur_kerns[0]); |
| 383 |
|
| 384 |
// Allocate PBO if more than one blur kernel is present |
| 385 |
if (ps->o.blur_kerns[1]) { |
| 386 |
#ifdef CONFIG_VSYNC_OPENGL_FBO |
| 387 |
// Try to generate a framebuffer |
| 388 |
GLuint fbo = 0; |
| 389 |
glGenFramebuffers(1, &fbo); |
| 390 |
if (!fbo) { |
| 391 |
printf_errf("(): Failed to generate Framebuffer. Cannot do " |
| 392 |
"multi-pass blur with GLX backend."); |
| 393 |
return false; |
| 394 |
} |
| 395 |
glDeleteFramebuffers(1, &fbo); |
| 396 |
#else |
| 397 |
printf_errf("(): FBO support not compiled in. Cannot do multi-pass blur " |
| 398 |
"with GLX backend."); |
| 399 |
return false; |
| 400 |
#endif |
| 401 |
} |
| 402 |
|
| 403 |
#ifdef CONFIG_VSYNC_OPENGL_GLSL |
| 404 |
{ |
| 405 |
char *lc_numeric_old = mstrcpy(setlocale(LC_NUMERIC, NULL)); |
| 406 |
// Enforce LC_NUMERIC locale "C" here to make sure decimal point is sane |
| 407 |
// Thanks to hiciu for reporting. |
| 408 |
setlocale(LC_NUMERIC, "C"); |
| 409 |
|
| 410 |
static const char *FRAG_SHADER_BLUR_PREFIX = |
| 411 |
"#version 110\n" |
| 412 |
"%s" |
| 413 |
"uniform float offset_x;\n" |
| 414 |
"uniform float offset_y;\n" |
| 415 |
"uniform float factor_center;\n" |
| 416 |
"uniform %s tex_scr;\n" |
| 417 |
"\n" |
| 418 |
"void main() {\n" |
| 419 |
" vec4 sum = vec4(0.0, 0.0, 0.0, 0.0);\n"; |
| 420 |
static const char *FRAG_SHADER_BLUR_ADD = |
| 421 |
" sum += float(%.7g) * %s(tex_scr, vec2(gl_TexCoord[0].x + offset_x * float(%d), gl_TexCoord[0].y + offset_y * float(%d)));\n"; |
| 422 |
static const char *FRAG_SHADER_BLUR_ADD_GPUSHADER4 = |
| 423 |
" sum += float(%.7g) * %sOffset(tex_scr, vec2(gl_TexCoord[0].x, gl_TexCoord[0].y), ivec2(%d, %d));\n"; |
| 424 |
static const char *FRAG_SHADER_BLUR_SUFFIX = |
| 425 |
" sum += %s(tex_scr, vec2(gl_TexCoord[0].x, gl_TexCoord[0].y)) * factor_center;\n" |
| 426 |
" gl_FragColor = sum / (factor_center + float(%.7g));\n" |
| 427 |
"}\n"; |
| 428 |
|
| 429 |
const bool use_texture_rect = !ps->psglx->has_texture_non_power_of_two; |
| 430 |
const char *sampler_type = (use_texture_rect ? |
| 431 |
"sampler2DRect": "sampler2D"); |
| 432 |
const char *texture_func = (use_texture_rect ? |
| 433 |
"texture2DRect": "texture2D"); |
| 434 |
const char *shader_add = FRAG_SHADER_BLUR_ADD; |
| 435 |
char *extension = mstrcpy(""); |
| 436 |
if (use_texture_rect) |
| 437 |
mstrextend(&extension, "#extension GL_ARB_texture_rectangle : require\n"); |
| 438 |
if (ps->o.glx_use_gpushader4) { |
| 439 |
mstrextend(&extension, "#extension GL_EXT_gpu_shader4 : require\n"); |
| 440 |
shader_add = FRAG_SHADER_BLUR_ADD_GPUSHADER4; |
| 441 |
} |
| 442 |
|
| 443 |
for (int i = 0; i < MAX_BLUR_PASS && ps->o.blur_kerns[i]; ++i) { |
| 444 |
XFixed *kern = ps->o.blur_kerns[i]; |
| 445 |
if (!kern) |
| 446 |
break; |
| 447 |
|
| 448 |
glx_blur_pass_t *ppass = &ps->psglx->blur_passes[i]; |
| 449 |
|
| 450 |
// Build shader |
| 451 |
{ |
| 452 |
int wid = XFixedToDouble(kern[0]), hei = XFixedToDouble(kern[1]); |
| 453 |
int nele = wid * hei - 1; |
| 454 |
int len = strlen(FRAG_SHADER_BLUR_PREFIX) + strlen(sampler_type) + strlen(extension) + (strlen(shader_add) + strlen(texture_func) + 42) * nele + strlen(FRAG_SHADER_BLUR_SUFFIX) + strlen(texture_func) + 12 + 1; |
| 455 |
char *shader_str = calloc(len, sizeof(char)); |
| 456 |
if (!shader_str) { |
| 457 |
printf_errf("(): Failed to allocate %d bytes for shader string.", len); |
| 458 |
return false; |
| 459 |
} |
| 460 |
{ |
| 461 |
char *pc = shader_str; |
| 462 |
sprintf(pc, FRAG_SHADER_BLUR_PREFIX, extension, sampler_type); |
| 463 |
pc += strlen(pc); |
| 464 |
assert(strlen(shader_str) < len); |
| 465 |
|
| 466 |
double sum = 0.0; |
| 467 |
for (int j = 0; j < hei; ++j) { |
| 468 |
for (int k = 0; k < wid; ++k) { |
| 469 |
if (hei / 2 == j && wid / 2 == k) |
| 470 |
continue; |
| 471 |
double val = XFixedToDouble(kern[2 + j * wid + k]); |
| 472 |
if (0.0 == val) |
| 473 |
continue; |
| 474 |
sum += val; |
| 475 |
sprintf(pc, shader_add, val, texture_func, k - wid / 2, j - hei / 2); |
| 476 |
pc += strlen(pc); |
| 477 |
assert(strlen(shader_str) < len); |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
sprintf(pc, FRAG_SHADER_BLUR_SUFFIX, texture_func, sum); |
| 482 |
assert(strlen(shader_str) < len); |
| 483 |
} |
| 484 |
ppass->frag_shader = glx_create_shader(GL_FRAGMENT_SHADER, shader_str); |
| 485 |
free(shader_str); |
| 486 |
} |
| 487 |
|
| 488 |
if (!ppass->frag_shader) { |
| 489 |
printf_errf("(): Failed to create fragment shader %d.", i); |
| 490 |
return false; |
| 491 |
} |
| 492 |
|
| 493 |
// Build program |
| 494 |
ppass->prog = glx_create_program(&ppass->frag_shader, 1); |
| 495 |
if (!ppass->prog) { |
| 496 |
printf_errf("(): Failed to create GLSL program."); |
| 497 |
return false; |
| 498 |
} |
| 499 |
|
| 500 |
// Get uniform addresses |
| 501 |
#define P_GET_UNIFM_LOC(name, target) { \ |
| 502 |
ppass->target = glGetUniformLocation(ppass->prog, name); \ |
| 503 |
if (ppass->target < 0) { \ |
| 504 |
printf_errf("(): Failed to get location of %d-th uniform '" name "'. Might be troublesome.", i); \ |
| 505 |
} \ |
| 506 |
} |
| 507 |
|
| 508 |
P_GET_UNIFM_LOC("factor_center", unifm_factor_center); |
| 509 |
if (!ps->o.glx_use_gpushader4) { |
| 510 |
P_GET_UNIFM_LOC("offset_x", unifm_offset_x); |
| 511 |
P_GET_UNIFM_LOC("offset_y", unifm_offset_y); |
| 512 |
} |
| 513 |
|
| 514 |
#undef P_GET_UNIFM_LOC |
| 515 |
} |
| 516 |
free(extension); |
| 517 |
|
| 518 |
// Restore LC_NUMERIC |
| 519 |
setlocale(LC_NUMERIC, lc_numeric_old); |
| 520 |
free(lc_numeric_old); |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
glx_check_err(ps); |
| 525 |
|
| 526 |
return true; |
| 527 |
#else |
| 528 |
printf_errf("(): GLSL support not compiled in. Cannot do blur with GLX backend."); |
| 529 |
return false; |
| 530 |
#endif |
| 531 |
} |
| 532 |
|
| 533 |
#ifdef CONFIG_VSYNC_OPENGL_GLSL |
| 534 |
|
| 535 |
/** |
| 536 |
* Load a GLSL main program from shader strings. |
| 537 |
*/ |
| 538 |
bool |
| 539 |
glx_load_prog_main(session_t *ps, |
| 540 |
const char *vshader_str, const char *fshader_str, |
| 541 |
glx_prog_main_t *pprogram) { |
| 542 |
assert(pprogram); |
| 543 |
|
| 544 |
// Build program |
| 545 |
pprogram->prog = glx_create_program_from_str(vshader_str, fshader_str); |
| 546 |
if (!pprogram->prog) { |
| 547 |
printf_errf("(): Failed to create GLSL program."); |
| 548 |
return false; |
| 549 |
} |
| 550 |
|
| 551 |
// Get uniform addresses |
| 552 |
#define P_GET_UNIFM_LOC(name, target) { \ |
| 553 |
pprogram->target = glGetUniformLocation(pprogram->prog, name); \ |
| 554 |
if (pprogram->target < 0) { \ |
| 555 |
printf_errf("(): Failed to get location of uniform '" name "'. Might be troublesome."); \ |
| 556 |
} \ |
| 557 |
} |
| 558 |
P_GET_UNIFM_LOC("opacity", unifm_opacity); |
| 559 |
P_GET_UNIFM_LOC("invert_color", unifm_invert_color); |
| 560 |
P_GET_UNIFM_LOC("tex", unifm_tex); |
| 561 |
#undef P_GET_UNIFM_LOC |
| 562 |
|
| 563 |
glx_check_err(ps); |
| 564 |
|
| 565 |
return true; |
| 566 |
} |
| 567 |
|
| 568 |
#endif |
| 569 |
|
| 570 |
/** |
| 571 |
* @brief Update the FBConfig of given depth. |
| 572 |
*/ |
| 573 |
static inline void |
| 574 |
glx_update_fbconfig_bydepth(session_t *ps, int depth, glx_fbconfig_t *pfbcfg) { |
| 575 |
// Make sure the depth is sane |
| 576 |
if (depth < 0 || depth > OPENGL_MAX_DEPTH) |
| 577 |
return; |
| 578 |
|
| 579 |
// Compare new FBConfig with current one |
| 580 |
if (glx_cmp_fbconfig(ps, ps->psglx->fbconfigs[depth], pfbcfg) < 0) { |
| 581 |
#ifdef DEBUG_GLX |
| 582 |
printf_dbgf("(%d): %#x overrides %#x, target %#x.\n", depth, (unsigned) pfbcfg->cfg, (ps->psglx->fbconfigs[depth] ? (unsigned) ps->psglx->fbconfigs[depth]->cfg: 0), pfbcfg->texture_tgts); |
| 583 |
#endif |
| 584 |
if (!ps->psglx->fbconfigs[depth]) { |
| 585 |
ps->psglx->fbconfigs[depth] = malloc(sizeof(glx_fbconfig_t)); |
| 586 |
allocchk(ps->psglx->fbconfigs[depth]); |
| 587 |
} |
| 588 |
(*ps->psglx->fbconfigs[depth]) = *pfbcfg; |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
/** |
| 593 |
* Get GLX FBConfigs for all depths. |
| 594 |
*/ |
| 595 |
static bool |
| 596 |
glx_update_fbconfig(session_t *ps) { |
| 597 |
// Acquire all FBConfigs and loop through them |
| 598 |
int nele = 0; |
| 599 |
GLXFBConfig* pfbcfgs = glXGetFBConfigs(ps->dpy, ps->scr, &nele); |
| 600 |
|
| 601 |
for (GLXFBConfig *pcur = pfbcfgs; pcur < pfbcfgs + nele; pcur++) { |
| 602 |
glx_fbconfig_t fbinfo = { |
| 603 |
.cfg = *pcur, |
| 604 |
.texture_fmt = 0, |
| 605 |
.texture_tgts = 0, |
| 606 |
.y_inverted = false, |
| 607 |
}; |
| 608 |
int id = (int) (pcur - pfbcfgs); |
| 609 |
int depth = 0, depth_alpha = 0, val = 0; |
| 610 |
|
| 611 |
// Skip over multi-sampled visuals |
| 612 |
// http://people.freedesktop.org/~glisse/0001-glx-do-not-use-multisample-visual-config-for-front-o.patch |
| 613 |
#ifdef GLX_SAMPLES |
| 614 |
if (Success == glXGetFBConfigAttrib(ps->dpy, *pcur, GLX_SAMPLES, &val) |
| 615 |
&& val > 1) |
| 616 |
continue; |
| 617 |
#endif |
| 618 |
|
| 619 |
if (Success != glXGetFBConfigAttrib(ps->dpy, *pcur, GLX_BUFFER_SIZE, &depth) |
| 620 |
|| Success != glXGetFBConfigAttrib(ps->dpy, *pcur, GLX_ALPHA_SIZE, &depth_alpha)) { |
| 621 |
printf_errf("(): Failed to retrieve buffer size and alpha size of FBConfig %d.", id); |
| 622 |
continue; |
| 623 |
} |
| 624 |
if (Success != glXGetFBConfigAttrib(ps->dpy, *pcur, GLX_BIND_TO_TEXTURE_TARGETS_EXT, &fbinfo.texture_tgts)) { |
| 625 |
printf_errf("(): Failed to retrieve BIND_TO_TEXTURE_TARGETS_EXT of FBConfig %d.", id); |
| 626 |
continue; |
| 627 |
} |
| 628 |
|
| 629 |
int visualdepth = 0; |
| 630 |
{ |
| 631 |
XVisualInfo *pvi = glXGetVisualFromFBConfig(ps->dpy, *pcur); |
| 632 |
if (!pvi) { |
| 633 |
// On nvidia-drivers-325.08 this happens slightly too often... |
| 634 |
// printf_errf("(): Failed to retrieve X Visual of FBConfig %d.", id); |
| 635 |
continue; |
| 636 |
} |
| 637 |
visualdepth = pvi->depth; |
| 638 |
cxfree(pvi); |
| 639 |
} |
| 640 |
|
| 641 |
bool rgb = false; |
| 642 |
bool rgba = false; |
| 643 |
|
| 644 |
if (depth >= 32 && depth_alpha && Success == glXGetFBConfigAttrib(ps->dpy, *pcur, GLX_BIND_TO_TEXTURE_RGBA_EXT, &val) && val) |
| 645 |
rgba = true; |
| 646 |
|
| 647 |
if (Success == glXGetFBConfigAttrib(ps->dpy, *pcur, GLX_BIND_TO_TEXTURE_RGB_EXT, &val) && val) |
| 648 |
rgb = true; |
| 649 |
|
| 650 |
if (Success == glXGetFBConfigAttrib(ps->dpy, *pcur, GLX_Y_INVERTED_EXT, &val)) |
| 651 |
fbinfo.y_inverted = val; |
| 652 |
|
| 653 |
{ |
| 654 |
int tgtdpt = depth - depth_alpha; |
| 655 |
if (tgtdpt == visualdepth && tgtdpt < 32 && rgb) { |
| 656 |
fbinfo.texture_fmt = GLX_TEXTURE_FORMAT_RGB_EXT; |
| 657 |
glx_update_fbconfig_bydepth(ps, tgtdpt, &fbinfo); |
| 658 |
} |
| 659 |
} |
| 660 |
|
| 661 |
if (depth == visualdepth && rgba) { |
| 662 |
fbinfo.texture_fmt = GLX_TEXTURE_FORMAT_RGBA_EXT; |
| 663 |
glx_update_fbconfig_bydepth(ps, depth, &fbinfo); |
| 664 |
} |
| 665 |
} |
| 666 |
|
| 667 |
cxfree(pfbcfgs); |
| 668 |
|
| 669 |
// Sanity checks |
| 670 |
if (!ps->psglx->fbconfigs[ps->depth]) { |
| 671 |
printf_errf("(): No FBConfig found for default depth %d.", ps->depth); |
| 672 |
return false; |
| 673 |
} |
| 674 |
|
| 675 |
if (!ps->psglx->fbconfigs[32]) { |
| 676 |
printf_errf("(): No FBConfig found for depth 32. Expect crazy things."); |
| 677 |
} |
| 678 |
|
| 679 |
#ifdef DEBUG_GLX |
| 680 |
printf_dbgf("(): %d-bit: %#3x, 32-bit: %#3x\n", |
| 681 |
ps->depth, (int) ps->psglx->fbconfigs[ps->depth]->cfg, |
| 682 |
(int) ps->psglx->fbconfigs[32]->cfg); |
| 683 |
#endif |
| 684 |
|
| 685 |
return true; |
| 686 |
} |
| 687 |
|
| 688 |
static inline int |
| 689 |
glx_cmp_fbconfig_cmpattr(session_t *ps, |
| 690 |
const glx_fbconfig_t *pfbc_a, const glx_fbconfig_t *pfbc_b, |
| 691 |
int attr) { |
| 692 |
int attr_a = 0, attr_b = 0; |
| 693 |
|
| 694 |
// TODO: Error checking |
| 695 |
glXGetFBConfigAttrib(ps->dpy, pfbc_a->cfg, attr, &attr_a); |
| 696 |
glXGetFBConfigAttrib(ps->dpy, pfbc_b->cfg, attr, &attr_b); |
| 697 |
|
| 698 |
return attr_a - attr_b; |
| 699 |
} |
| 700 |
|
| 701 |
/** |
| 702 |
* Compare two GLX FBConfig's to find the preferred one. |
| 703 |
*/ |
| 704 |
static int |
| 705 |
glx_cmp_fbconfig(session_t *ps, |
| 706 |
const glx_fbconfig_t *pfbc_a, const glx_fbconfig_t *pfbc_b) { |
| 707 |
int result = 0; |
| 708 |
|
| 709 |
if (!pfbc_a) |
| 710 |
return -1; |
| 711 |
if (!pfbc_b) |
| 712 |
return 1; |
| 713 |
int tmpattr; |
| 714 |
|
| 715 |
// Avoid 10-bit colors |
| 716 |
glXGetFBConfigAttrib(ps->dpy, pfbc_a->cfg, GLX_RED_SIZE, &tmpattr); |
| 717 |
if (tmpattr != 8) |
| 718 |
return -1; |
| 719 |
|
| 720 |
glXGetFBConfigAttrib(ps->dpy, pfbc_b->cfg, GLX_RED_SIZE, &tmpattr); |
| 721 |
if (tmpattr != 8) |
| 722 |
return 1; |
| 723 |
|
| 724 |
#define P_CMPATTR_LT(attr) { if ((result = glx_cmp_fbconfig_cmpattr(ps, pfbc_a, pfbc_b, (attr)))) return -result; } |
| 725 |
#define P_CMPATTR_GT(attr) { if ((result = glx_cmp_fbconfig_cmpattr(ps, pfbc_a, pfbc_b, (attr)))) return result; } |
| 726 |
|
| 727 |
P_CMPATTR_LT(GLX_BIND_TO_TEXTURE_RGBA_EXT); |
| 728 |
P_CMPATTR_LT(GLX_DOUBLEBUFFER); |
| 729 |
P_CMPATTR_LT(GLX_STENCIL_SIZE); |
| 730 |
P_CMPATTR_LT(GLX_DEPTH_SIZE); |
| 731 |
P_CMPATTR_GT(GLX_BIND_TO_MIPMAP_TEXTURE_EXT); |
| 732 |
|
| 733 |
return 0; |
| 734 |
} |
| 735 |
|
| 736 |
/** |
| 737 |
* Bind a X pixmap to an OpenGL texture. |
| 738 |
*/ |
| 739 |
bool |
| 740 |
glx_bind_pixmap(session_t *ps, glx_texture_t **pptex, Pixmap pixmap, |
| 741 |
unsigned width, unsigned height, unsigned depth) { |
| 742 |
if (!pixmap) { |
| 743 |
printf_errf("(%#010lx): Binding to an empty pixmap. This can't work.", |
| 744 |
pixmap); |
| 745 |
return false; |
| 746 |
} |
| 747 |
|
| 748 |
glx_texture_t *ptex = *pptex; |
| 749 |
bool need_release = true; |
| 750 |
|
| 751 |
// Allocate structure |
| 752 |
if (!ptex) { |
| 753 |
static const glx_texture_t GLX_TEX_DEF = { |
| 754 |
.texture = 0, |
| 755 |
.glpixmap = 0, |
| 756 |
.pixmap = 0, |
| 757 |
.target = 0, |
| 758 |
.width = 0, |
| 759 |
.height = 0, |
| 760 |
.depth = 0, |
| 761 |
.y_inverted = false, |
| 762 |
}; |
| 763 |
|
| 764 |
ptex = malloc(sizeof(glx_texture_t)); |
| 765 |
allocchk(ptex); |
| 766 |
memcpy(ptex, &GLX_TEX_DEF, sizeof(glx_texture_t)); |
| 767 |
*pptex = ptex; |
| 768 |
} |
| 769 |
|
| 770 |
// Release pixmap if parameters are inconsistent |
| 771 |
if (ptex->texture && ptex->pixmap != pixmap) { |
| 772 |
glx_release_pixmap(ps, ptex); |
| 773 |
} |
| 774 |
|
| 775 |
// Create GLX pixmap |
| 776 |
if (!ptex->glpixmap) { |
| 777 |
need_release = false; |
| 778 |
|
| 779 |
// Retrieve pixmap parameters, if they aren't provided |
| 780 |
if (!(width && height && depth)) { |
| 781 |
Window rroot = None; |
| 782 |
int rx = 0, ry = 0; |
| 783 |
unsigned rbdwid = 0; |
| 784 |
if (!XGetGeometry(ps->dpy, pixmap, &rroot, &rx, &ry, |
| 785 |
&width, &height, &rbdwid, &depth)) { |
| 786 |
printf_errf("(%#010lx): Failed to query Pixmap info.", pixmap); |
| 787 |
return false; |
| 788 |
} |
| 789 |
if (depth > OPENGL_MAX_DEPTH) { |
| 790 |
printf_errf("(%d): Requested depth higher than %d.", depth, |
| 791 |
OPENGL_MAX_DEPTH); |
| 792 |
return false; |
| 793 |
} |
| 794 |
} |
| 795 |
|
| 796 |
const glx_fbconfig_t *pcfg = ps->psglx->fbconfigs[depth]; |
| 797 |
if (!pcfg) { |
| 798 |
printf_errf("(%d): Couldn't find FBConfig with requested depth.", depth); |
| 799 |
return false; |
| 800 |
} |
| 801 |
|
| 802 |
// Determine texture target, copied from compiz |
| 803 |
// The assumption we made here is the target never changes based on any |
| 804 |
// pixmap-specific parameters, and this may change in the future |
| 805 |
GLenum tex_tgt = 0; |
| 806 |
if (GLX_TEXTURE_2D_BIT_EXT & pcfg->texture_tgts |
| 807 |
&& ps->psglx->has_texture_non_power_of_two) |
| 808 |
tex_tgt = GLX_TEXTURE_2D_EXT; |
| 809 |
else if (GLX_TEXTURE_RECTANGLE_BIT_EXT & pcfg->texture_tgts) |
| 810 |
tex_tgt = GLX_TEXTURE_RECTANGLE_EXT; |
| 811 |
else if (!(GLX_TEXTURE_2D_BIT_EXT & pcfg->texture_tgts)) |
| 812 |
tex_tgt = GLX_TEXTURE_RECTANGLE_EXT; |
| 813 |
else |
| 814 |
tex_tgt = GLX_TEXTURE_2D_EXT; |
| 815 |
|
| 816 |
#ifdef DEBUG_GLX |
| 817 |
printf_dbgf("(): depth %d, tgt %#x, rgba %d\n", depth, tex_tgt, |
| 818 |
(GLX_TEXTURE_FORMAT_RGBA_EXT == pcfg->texture_fmt)); |
| 819 |
#endif |
| 820 |
|
| 821 |
GLint attrs[] = { |
| 822 |
GLX_TEXTURE_FORMAT_EXT, |
| 823 |
pcfg->texture_fmt, |
| 824 |
GLX_TEXTURE_TARGET_EXT, |
| 825 |
tex_tgt, |
| 826 |
0, |
| 827 |
}; |
| 828 |
|
| 829 |
ptex->glpixmap = glXCreatePixmap(ps->dpy, pcfg->cfg, pixmap, attrs); |
| 830 |
ptex->pixmap = pixmap; |
| 831 |
ptex->target = (GLX_TEXTURE_2D_EXT == tex_tgt ? GL_TEXTURE_2D: |
| 832 |
GL_TEXTURE_RECTANGLE); |
| 833 |
ptex->width = width; |
| 834 |
ptex->height = height; |
| 835 |
ptex->depth = depth; |
| 836 |
ptex->y_inverted = pcfg->y_inverted; |
| 837 |
} |
| 838 |
if (!ptex->glpixmap) { |
| 839 |
printf_errf("(): Failed to allocate GLX pixmap."); |
| 840 |
return false; |
| 841 |
} |
| 842 |
|
| 843 |
glEnable(ptex->target); |
| 844 |
|
| 845 |
// Create texture |
| 846 |
if (!ptex->texture) { |
| 847 |
need_release = false; |
| 848 |
|
| 849 |
GLuint texture = 0; |
| 850 |
glGenTextures(1, &texture); |
| 851 |
glBindTexture(ptex->target, texture); |
| 852 |
|
| 853 |
glTexParameteri(ptex->target, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 854 |
glTexParameteri(ptex->target, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 855 |
glTexParameteri(ptex->target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 856 |
glTexParameteri(ptex->target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 857 |
|
| 858 |
glBindTexture(ptex->target, 0); |
| 859 |
|
| 860 |
ptex->texture = texture; |
| 861 |
} |
| 862 |
if (!ptex->texture) { |
| 863 |
printf_errf("(): Failed to allocate texture."); |
| 864 |
return false; |
| 865 |
} |
| 866 |
|
| 867 |
glBindTexture(ptex->target, ptex->texture); |
| 868 |
|
| 869 |
// The specification requires rebinding whenever the content changes... |
| 870 |
// We can't follow this, too slow. |
| 871 |
if (need_release) |
| 872 |
ps->psglx->glXReleaseTexImageProc(ps->dpy, ptex->glpixmap, GLX_FRONT_LEFT_EXT); |
| 873 |
|
| 874 |
ps->psglx->glXBindTexImageProc(ps->dpy, ptex->glpixmap, GLX_FRONT_LEFT_EXT, NULL); |
| 875 |
|
| 876 |
// Cleanup |
| 877 |
glBindTexture(ptex->target, 0); |
| 878 |
glDisable(ptex->target); |
| 879 |
|
| 880 |
glx_check_err(ps); |
| 881 |
|
| 882 |
return true; |
| 883 |
} |
| 884 |
|
| 885 |
/** |
| 886 |
* @brief Release binding of a texture. |
| 887 |
*/ |
| 888 |
void |
| 889 |
glx_release_pixmap(session_t *ps, glx_texture_t *ptex) { |
| 890 |
// Release binding |
| 891 |
if (ptex->glpixmap && ptex->texture) { |
| 892 |
glBindTexture(ptex->target, ptex->texture); |
| 893 |
ps->psglx->glXReleaseTexImageProc(ps->dpy, ptex->glpixmap, GLX_FRONT_LEFT_EXT); |
| 894 |
glBindTexture(ptex->target, 0); |
| 895 |
} |
| 896 |
|
| 897 |
// Free GLX Pixmap |
| 898 |
if (ptex->glpixmap) { |
| 899 |
glXDestroyPixmap(ps->dpy, ptex->glpixmap); |
| 900 |
ptex->glpixmap = 0; |
| 901 |
} |
| 902 |
|
| 903 |
glx_check_err(ps); |
| 904 |
} |
| 905 |
|
| 906 |
/** |
| 907 |
* Preprocess function before start painting. |
| 908 |
*/ |
| 909 |
void |
| 910 |
glx_paint_pre(session_t *ps, XserverRegion *preg) { |
| 911 |
ps->psglx->z = 0.0; |
| 912 |
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 913 |
|
| 914 |
// Get buffer age |
| 915 |
bool trace_damage = (ps->o.glx_swap_method < 0 || ps->o.glx_swap_method > 1); |
| 916 |
|
| 917 |
// Trace raw damage regions |
| 918 |
XserverRegion newdamage = None; |
| 919 |
if (trace_damage && *preg) |
| 920 |
newdamage = copy_region(ps, *preg); |
| 921 |
|
| 922 |
// OpenGL doesn't support partial repaint without GLX_MESA_copy_sub_buffer, |
| 923 |
// we could redraw the whole screen or copy unmodified pixels from |
| 924 |
// front buffer with --glx-copy-from-front. |
| 925 |
if (ps->o.glx_use_copysubbuffermesa || !*preg) { |
| 926 |
} |
| 927 |
else { |
| 928 |
int buffer_age = ps->o.glx_swap_method; |
| 929 |
|
| 930 |
// Getting buffer age |
| 931 |
{ |
| 932 |
// Query GLX_EXT_buffer_age for buffer age |
| 933 |
if (SWAPM_BUFFER_AGE == buffer_age) { |
| 934 |
unsigned val = 0; |
| 935 |
glXQueryDrawable(ps->dpy, get_tgt_window(ps), |
| 936 |
GLX_BACK_BUFFER_AGE_EXT, &val); |
| 937 |
buffer_age = val; |
| 938 |
} |
| 939 |
|
| 940 |
// Buffer age too high |
| 941 |
if (buffer_age > CGLX_MAX_BUFFER_AGE + 1) |
| 942 |
buffer_age = 0; |
| 943 |
|
| 944 |
// Make sure buffer age >= 0 |
| 945 |
buffer_age = max_i(buffer_age, 0); |
| 946 |
|
| 947 |
// Check if we have we have empty regions |
| 948 |
if (buffer_age > 1) { |
| 949 |
for (int i = 0; i < buffer_age - 1; ++i) |
| 950 |
if (!ps->all_damage_last[i]) { buffer_age = 0; break; } |
| 951 |
} |
| 952 |
} |
| 953 |
|
| 954 |
// Do nothing for buffer_age 1 (copy) |
| 955 |
if (1 != buffer_age) { |
| 956 |
// Copy pixels |
| 957 |
if (ps->o.glx_copy_from_front) { |
| 958 |
// Determine copy area |
| 959 |
XserverRegion reg_copy = XFixesCreateRegion(ps->dpy, NULL, 0); |
| 960 |
if (!buffer_age) { |
| 961 |
XFixesSubtractRegion(ps->dpy, reg_copy, ps->screen_reg, *preg); |
| 962 |
} |
| 963 |
else { |
| 964 |
for (int i = 0; i < buffer_age - 1; ++i) |
| 965 |
XFixesUnionRegion(ps->dpy, reg_copy, reg_copy, |
| 966 |
ps->all_damage_last[i]); |
| 967 |
XFixesSubtractRegion(ps->dpy, reg_copy, reg_copy, *preg); |
| 968 |
} |
| 969 |
|
| 970 |
// Actually copy pixels |
| 971 |
{ |
| 972 |
GLfloat raster_pos[4]; |
| 973 |
GLfloat curx = 0.0f, cury = 0.0f; |
| 974 |
glGetFloatv(GL_CURRENT_RASTER_POSITION, raster_pos); |
| 975 |
glReadBuffer(GL_FRONT); |
| 976 |
glRasterPos2f(0.0, 0.0); |
| 977 |
{ |
| 978 |
int nrects = 0; |
| 979 |
XRectangle *rects = XFixesFetchRegion(ps->dpy, reg_copy, &nrects); |
| 980 |
for (int i = 0; i < nrects; ++i) { |
| 981 |
const int x = rects[i].x; |
| 982 |
const int y = ps->root_height - rects[i].y - rects[i].height; |
| 983 |
// Kwin patch says glRasterPos2f() causes artifacts on bottom |
| 984 |
// screen edge with some drivers |
| 985 |
glBitmap(0, 0, 0, 0, x - curx, y - cury, NULL); |
| 986 |
curx = x; |
| 987 |
cury = y; |
| 988 |
glCopyPixels(x, y, rects[i].width, rects[i].height, GL_COLOR); |
| 989 |
} |
| 990 |
cxfree(rects); |
| 991 |
} |
| 992 |
glReadBuffer(GL_BACK); |
| 993 |
glRasterPos4fv(raster_pos); |
| 994 |
} |
| 995 |
|
| 996 |
free_region(ps, ®_copy); |
| 997 |
} |
| 998 |
|
| 999 |
// Determine paint area |
| 1000 |
if (ps->o.glx_copy_from_front) { } |
| 1001 |
else if (buffer_age) { |
| 1002 |
for (int i = 0; i < buffer_age - 1; ++i) |
| 1003 |
XFixesUnionRegion(ps->dpy, *preg, *preg, ps->all_damage_last[i]); |
| 1004 |
} |
| 1005 |
else { |
| 1006 |
free_region(ps, preg); |
| 1007 |
} |
| 1008 |
} |
| 1009 |
} |
| 1010 |
|
| 1011 |
if (trace_damage) { |
| 1012 |
free_region(ps, &ps->all_damage_last[CGLX_MAX_BUFFER_AGE - 1]); |
| 1013 |
memmove(ps->all_damage_last + 1, ps->all_damage_last, |
| 1014 |
(CGLX_MAX_BUFFER_AGE - 1) * sizeof(XserverRegion)); |
| 1015 |
ps->all_damage_last[0] = newdamage; |
| 1016 |
} |
| 1017 |
|
| 1018 |
glx_set_clip(ps, *preg, NULL); |
| 1019 |
|
| 1020 |
#ifdef DEBUG_GLX_PAINTREG |
| 1021 |
glx_render_color(ps, 0, 0, ps->root_width, ps->root_height, 0, *preg, NULL); |
| 1022 |
#endif |
| 1023 |
|
| 1024 |
glx_check_err(ps); |
| 1025 |
} |
| 1026 |
|
| 1027 |
/** |
| 1028 |
* Set clipping region on the target window. |
| 1029 |
*/ |
| 1030 |
void |
| 1031 |
glx_set_clip(session_t *ps, XserverRegion reg, const reg_data_t *pcache_reg) { |
| 1032 |
// Quit if we aren't using stencils |
| 1033 |
if (ps->o.glx_no_stencil) |
| 1034 |
return; |
| 1035 |
|
| 1036 |
static XRectangle rect_blank = { .x = 0, .y = 0, .width = 0, .height = 0 }; |
| 1037 |
|
| 1038 |
glDisable(GL_STENCIL_TEST); |
| 1039 |
glDisable(GL_SCISSOR_TEST); |
| 1040 |
|
| 1041 |
if (!reg) |
| 1042 |
return; |
| 1043 |
|
| 1044 |
int nrects = 0; |
| 1045 |
XRectangle *rects_free = NULL; |
| 1046 |
const XRectangle *rects = NULL; |
| 1047 |
if (pcache_reg) { |
| 1048 |
rects = pcache_reg->rects; |
| 1049 |
nrects = pcache_reg->nrects; |
| 1050 |
} |
| 1051 |
if (!rects) { |
| 1052 |
nrects = 0; |
| 1053 |
rects = rects_free = XFixesFetchRegion(ps->dpy, reg, &nrects); |
| 1054 |
} |
| 1055 |
// Use one empty rectangle if the region is empty |
| 1056 |
if (!nrects) { |
| 1057 |
cxfree(rects_free); |
| 1058 |
rects_free = NULL; |
| 1059 |
nrects = 1; |
| 1060 |
rects = &rect_blank; |
| 1061 |
} |
| 1062 |
|
| 1063 |
assert(nrects); |
| 1064 |
if (1 == nrects) { |
| 1065 |
glEnable(GL_SCISSOR_TEST); |
| 1066 |
glScissor(rects[0].x, ps->root_height - rects[0].y - rects[0].height, |
| 1067 |
rects[0].width, rects[0].height); |
| 1068 |
} |
| 1069 |
else { |
| 1070 |
glEnable(GL_STENCIL_TEST); |
| 1071 |
glClear(GL_STENCIL_BUFFER_BIT); |
| 1072 |
|
| 1073 |
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); |
| 1074 |
glDepthMask(GL_FALSE); |
| 1075 |
glStencilOp(GL_REPLACE, GL_KEEP, GL_KEEP); |
| 1076 |
|
| 1077 |
glBegin(GL_QUADS); |
| 1078 |
|
| 1079 |
for (int i = 0; i < nrects; ++i) { |
| 1080 |
GLint rx = rects[i].x; |
| 1081 |
GLint ry = ps->root_height - rects[i].y; |
| 1082 |
GLint rxe = rx + rects[i].width; |
| 1083 |
GLint rye = ry - rects[i].height; |
| 1084 |
GLint z = 0; |
| 1085 |
|
| 1086 |
#ifdef DEBUG_GLX |
| 1087 |
printf_dbgf("(): Rect %d: %d, %d, %d, %d\n", i, rx, ry, rxe, rye); |
| 1088 |
#endif |
| 1089 |
|
| 1090 |
glVertex3i(rx, ry, z); |
| 1091 |
glVertex3i(rxe, ry, z); |
| 1092 |
glVertex3i(rxe, rye, z); |
| 1093 |
glVertex3i(rx, rye, z); |
| 1094 |
} |
| 1095 |
|
| 1096 |
glEnd(); |
| 1097 |
|
| 1098 |
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); |
| 1099 |
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); |
| 1100 |
// glDepthMask(GL_TRUE); |
| 1101 |
} |
| 1102 |
|
| 1103 |
cxfree(rects_free); |
| 1104 |
|
| 1105 |
glx_check_err(ps); |
| 1106 |
} |
| 1107 |
|
| 1108 |
#define P_PAINTREG_START() \ |
| 1109 |
XserverRegion reg_new = None; \ |
| 1110 |
XRectangle rec_all = { .x = dx, .y = dy, .width = width, .height = height }; \ |
| 1111 |
XRectangle *rects = &rec_all; \ |
| 1112 |
int nrects = 1; \ |
| 1113 |
\ |
| 1114 |
if (ps->o.glx_no_stencil && reg_tgt) { \ |
| 1115 |
if (pcache_reg) { \ |
| 1116 |
rects = pcache_reg->rects; \ |
| 1117 |
nrects = pcache_reg->nrects; \ |
| 1118 |
} \ |
| 1119 |
else { \ |
| 1120 |
reg_new = XFixesCreateRegion(ps->dpy, &rec_all, 1); \ |
| 1121 |
XFixesIntersectRegion(ps->dpy, reg_new, reg_new, reg_tgt); \ |
| 1122 |
\ |
| 1123 |
nrects = 0; \ |
| 1124 |
rects = XFixesFetchRegion(ps->dpy, reg_new, &nrects); \ |
| 1125 |
} \ |
| 1126 |
} \ |
| 1127 |
glBegin(GL_QUADS); \ |
| 1128 |
\ |
| 1129 |
for (int ri = 0; ri < nrects; ++ri) { \ |
| 1130 |
XRectangle crect; \ |
| 1131 |
rect_crop(&crect, &rects[ri], &rec_all); \ |
| 1132 |
\ |
| 1133 |
if (!crect.width || !crect.height) \ |
| 1134 |
continue; \ |
| 1135 |
|
| 1136 |
#define P_PAINTREG_END() \ |
| 1137 |
} \ |
| 1138 |
glEnd(); \ |
| 1139 |
\ |
| 1140 |
if (rects && rects != &rec_all && !(pcache_reg && pcache_reg->rects == rects)) \ |
| 1141 |
cxfree(rects); \ |
| 1142 |
free_region(ps, ®_new); \ |
| 1143 |
|
| 1144 |
static inline GLuint |
| 1145 |
glx_gen_texture(session_t *ps, GLenum tex_tgt, int width, int height) { |
| 1146 |
GLuint tex = 0; |
| 1147 |
glGenTextures(1, &tex); |
| 1148 |
if (!tex) return 0; |
| 1149 |
glEnable(tex_tgt); |
| 1150 |
glBindTexture(tex_tgt, tex); |
| 1151 |
glTexParameteri(tex_tgt, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 1152 |
glTexParameteri(tex_tgt, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 1153 |
glTexParameteri(tex_tgt, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 1154 |
glTexParameteri(tex_tgt, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 1155 |
glTexImage2D(tex_tgt, 0, GL_RGB, width, height, 0, GL_RGB, |
| 1156 |
GL_UNSIGNED_BYTE, NULL); |
| 1157 |
glBindTexture(tex_tgt, 0); |
| 1158 |
|
| 1159 |
return tex; |
| 1160 |
} |
| 1161 |
|
| 1162 |
static inline void |
| 1163 |
glx_copy_region_to_tex(session_t *ps, GLenum tex_tgt, int basex, int basey, |
| 1164 |
int dx, int dy, int width, int height) { |
| 1165 |
if (width > 0 && height > 0) |
| 1166 |
glCopyTexSubImage2D(tex_tgt, 0, dx - basex, dy - basey, |
| 1167 |
dx, ps->root_height - dy - height, width, height); |
| 1168 |
} |
| 1169 |
|
| 1170 |
#ifdef CONFIG_VSYNC_OPENGL_GLSL |
| 1171 |
/** |
| 1172 |
* Blur contents in a particular region. |
| 1173 |
*/ |
| 1174 |
bool |
| 1175 |
glx_blur_dst(session_t *ps, int dx, int dy, int width, int height, float z, |
| 1176 |
GLfloat factor_center, |
| 1177 |
XserverRegion reg_tgt, const reg_data_t *pcache_reg, |
| 1178 |
glx_blur_cache_t *pbc) { |
| 1179 |
assert(ps->psglx->blur_passes[0].prog); |
| 1180 |
const bool more_passes = ps->psglx->blur_passes[1].prog; |
| 1181 |
const bool have_scissors = glIsEnabled(GL_SCISSOR_TEST); |
| 1182 |
const bool have_stencil = glIsEnabled(GL_STENCIL_TEST); |
| 1183 |
bool ret = false; |
| 1184 |
|
| 1185 |
// Calculate copy region size |
| 1186 |
glx_blur_cache_t ibc = { .width = 0, .height = 0 }; |
| 1187 |
if (!pbc) |
| 1188 |
pbc = &ibc; |
| 1189 |
|
| 1190 |
int mdx = dx, mdy = dy, mwidth = width, mheight = height; |
| 1191 |
#ifdef DEBUG_GLX |
| 1192 |
printf_dbgf("(): %d, %d, %d, %d\n", mdx, mdy, mwidth, mheight); |
| 1193 |
#endif |
| 1194 |
|
| 1195 |
/* |
| 1196 |
if (ps->o.resize_damage > 0) { |
| 1197 |
int inc_x = 0, inc_y = 0; |
| 1198 |
for (int i = 0; i < MAX_BLUR_PASS; ++i) { |
| 1199 |
XFixed *kern = ps->o.blur_kerns[i]; |
| 1200 |
if (!kern) break; |
| 1201 |
inc_x += XFixedToDouble(kern[0]) / 2; |
| 1202 |
inc_y += XFixedToDouble(kern[1]) / 2; |
| 1203 |
} |
| 1204 |
inc_x = min_i(ps->o.resize_damage, inc_x); |
| 1205 |
inc_y = min_i(ps->o.resize_damage, inc_y); |
| 1206 |
|
| 1207 |
mdx = max_i(dx - inc_x, 0); |
| 1208 |
mdy = max_i(dy - inc_y, 0); |
| 1209 |
int mdx2 = min_i(dx + width + inc_x, ps->root_width), |
| 1210 |
mdy2 = min_i(dy + height + inc_y, ps->root_height); |
| 1211 |
mwidth = mdx2 - mdx; |
| 1212 |
mheight = mdy2 - mdy; |
| 1213 |
} |
| 1214 |
*/ |
| 1215 |
|
| 1216 |
GLenum tex_tgt = GL_TEXTURE_RECTANGLE; |
| 1217 |
if (ps->psglx->has_texture_non_power_of_two) |
| 1218 |
tex_tgt = GL_TEXTURE_2D; |
| 1219 |
|
| 1220 |
// Free textures if size inconsistency discovered |
| 1221 |
if (mwidth != pbc->width || mheight != pbc->height) |
| 1222 |
free_glx_bc_resize(ps, pbc); |
| 1223 |
|
| 1224 |
// Generate FBO and textures if needed |
| 1225 |
if (!pbc->textures[0]) |
| 1226 |
pbc->textures[0] = glx_gen_texture(ps, tex_tgt, mwidth, mheight); |
| 1227 |
GLuint tex_scr = pbc->textures[0]; |
| 1228 |
if (more_passes && !pbc->textures[1]) |
| 1229 |
pbc->textures[1] = glx_gen_texture(ps, tex_tgt, mwidth, mheight); |
| 1230 |
pbc->width = mwidth; |
| 1231 |
pbc->height = mheight; |
| 1232 |
GLuint tex_scr2 = pbc->textures[1]; |
| 1233 |
#ifdef CONFIG_VSYNC_OPENGL_FBO |
| 1234 |
if (more_passes && !pbc->fbo) |
| 1235 |
glGenFramebuffers(1, &pbc->fbo); |
| 1236 |
const GLuint fbo = pbc->fbo; |
| 1237 |
#endif |
| 1238 |
|
| 1239 |
if (!tex_scr || (more_passes && !tex_scr2)) { |
| 1240 |
printf_errf("(): Failed to allocate texture."); |
| 1241 |
goto glx_blur_dst_end; |
| 1242 |
} |
| 1243 |
#ifdef CONFIG_VSYNC_OPENGL_FBO |
| 1244 |
if (more_passes && !fbo) { |
| 1245 |
printf_errf("(): Failed to allocate framebuffer."); |
| 1246 |
goto glx_blur_dst_end; |
| 1247 |
} |
| 1248 |
#endif |
| 1249 |
|
| 1250 |
// Read destination pixels into a texture |
| 1251 |
glEnable(tex_tgt); |
| 1252 |
glBindTexture(tex_tgt, tex_scr); |
| 1253 |
glx_copy_region_to_tex(ps, tex_tgt, mdx, mdy, mdx, mdy, mwidth, mheight); |
| 1254 |
/* |
| 1255 |
if (tex_scr2) { |
| 1256 |
glBindTexture(tex_tgt, tex_scr2); |
| 1257 |
glx_copy_region_to_tex(ps, tex_tgt, mdx, mdy, mdx, mdy, mwidth, dx - mdx); |
| 1258 |
glx_copy_region_to_tex(ps, tex_tgt, mdx, mdy, mdx, dy + height, |
| 1259 |
mwidth, mdy + mheight - dy - height); |
| 1260 |
glx_copy_region_to_tex(ps, tex_tgt, mdx, mdy, mdx, dy, dx - mdx, height); |
| 1261 |
glx_copy_region_to_tex(ps, tex_tgt, mdx, mdy, dx + width, dy, |
| 1262 |
mdx + mwidth - dx - width, height); |
| 1263 |
} */ |
| 1264 |
|
| 1265 |
// Texture scaling factor |
| 1266 |
GLfloat texfac_x = 1.0f, texfac_y = 1.0f; |
| 1267 |
if (GL_TEXTURE_2D == tex_tgt) { |
| 1268 |
texfac_x /= mwidth; |
| 1269 |
texfac_y /= mheight; |
| 1270 |
} |
| 1271 |
|
| 1272 |
// Paint it back |
| 1273 |
if (more_passes) { |
| 1274 |
glDisable(GL_STENCIL_TEST); |
| 1275 |
glDisable(GL_SCISSOR_TEST); |
| 1276 |
} |
| 1277 |
|
| 1278 |
bool last_pass = false; |
| 1279 |
for (int i = 0; !last_pass; ++i) { |
| 1280 |
last_pass = !ps->psglx->blur_passes[i + 1].prog; |
| 1281 |
assert(i < MAX_BLUR_PASS - 1); |
| 1282 |
const glx_blur_pass_t *ppass = &ps->psglx->blur_passes[i]; |
| 1283 |
assert(ppass->prog); |
| 1284 |
|
| 1285 |
assert(tex_scr); |
| 1286 |
glBindTexture(tex_tgt, tex_scr); |
| 1287 |
|
| 1288 |
#ifdef CONFIG_VSYNC_OPENGL_FBO |
| 1289 |
if (!last_pass) { |
| 1290 |
static const GLenum DRAWBUFS[2] = { GL_COLOR_ATTACHMENT0 }; |
| 1291 |
glBindFramebuffer(GL_FRAMEBUFFER, fbo); |
| 1292 |
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, |
| 1293 |
GL_TEXTURE_2D, tex_scr2, 0); |
| 1294 |
glDrawBuffers(1, DRAWBUFS); |
| 1295 |
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) |
| 1296 |
!= GL_FRAMEBUFFER_COMPLETE) { |
| 1297 |
printf_errf("(): Framebuffer attachment failed."); |
| 1298 |
goto glx_blur_dst_end; |
| 1299 |
} |
| 1300 |
} |
| 1301 |
else { |
| 1302 |
static const GLenum DRAWBUFS[2] = { GL_BACK }; |
| 1303 |
glBindFramebuffer(GL_FRAMEBUFFER, 0); |
| 1304 |
glDrawBuffers(1, DRAWBUFS); |
| 1305 |
if (have_scissors) |
| 1306 |
glEnable(GL_SCISSOR_TEST); |
| 1307 |
if (have_stencil) |
| 1308 |
glEnable(GL_STENCIL_TEST); |
| 1309 |
} |
| 1310 |
#endif |
| 1311 |
|
| 1312 |
// Color negation for testing... |
| 1313 |
// glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); |
| 1314 |
// glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE); |
| 1315 |
// glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_ONE_MINUS_SRC_COLOR); |
| 1316 |
|
| 1317 |
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
| 1318 |
glUseProgram(ppass->prog); |
| 1319 |
if (ppass->unifm_offset_x >= 0) |
| 1320 |
glUniform1f(ppass->unifm_offset_x, texfac_x); |
| 1321 |
if (ppass->unifm_offset_y >= 0) |
| 1322 |
glUniform1f(ppass->unifm_offset_y, texfac_y); |
| 1323 |
if (ppass->unifm_factor_center >= 0) |
| 1324 |
glUniform1f(ppass->unifm_factor_center, factor_center); |
| 1325 |
|
| 1326 |
{ |
| 1327 |
P_PAINTREG_START(); |
| 1328 |
{ |
| 1329 |
const GLfloat rx = (crect.x - mdx) * texfac_x; |
| 1330 |
const GLfloat ry = (mheight - (crect.y - mdy)) * texfac_y; |
| 1331 |
const GLfloat rxe = rx + crect.width * texfac_x; |
| 1332 |
const GLfloat rye = ry - crect.height * texfac_y; |
| 1333 |
GLfloat rdx = crect.x - mdx; |
| 1334 |
GLfloat rdy = mheight - crect.y + mdy; |
| 1335 |
GLfloat rdxe = rdx + crect.width; |
| 1336 |
GLfloat rdye = rdy - crect.height; |
| 1337 |
|
| 1338 |
if (last_pass) { |
| 1339 |
rdx = crect.x; |
| 1340 |
rdy = ps->root_height - crect.y; |
| 1341 |
rdxe = rdx + crect.width; |
| 1342 |
rdye = rdy - crect.height; |
| 1343 |
} |
| 1344 |
|
| 1345 |
#ifdef DEBUG_GLX |
| 1346 |
printf_dbgf("(): %f, %f, %f, %f -> %f, %f, %f, %f\n", rx, ry, rxe, rye, rdx, rdy, rdxe, rdye); |
| 1347 |
#endif |
| 1348 |
|
| 1349 |
glTexCoord2f(rx, ry); |
| 1350 |
glVertex3f(rdx, rdy, z); |
| 1351 |
|
| 1352 |
glTexCoord2f(rxe, ry); |
| 1353 |
glVertex3f(rdxe, rdy, z); |
| 1354 |
|
| 1355 |
glTexCoord2f(rxe, rye); |
| 1356 |
glVertex3f(rdxe, rdye, z); |
| 1357 |
|
| 1358 |
glTexCoord2f(rx, rye); |
| 1359 |
glVertex3f(rdx, rdye, z); |
| 1360 |
} |
| 1361 |
P_PAINTREG_END(); |
| 1362 |
} |
| 1363 |
|
| 1364 |
glUseProgram(0); |
| 1365 |
|
| 1366 |
// Swap tex_scr and tex_scr2 |
| 1367 |
{ |
| 1368 |
GLuint tmp = tex_scr2; |
| 1369 |
tex_scr2 = tex_scr; |
| 1370 |
tex_scr = tmp; |
| 1371 |
} |
| 1372 |
} |
| 1373 |
|
| 1374 |
ret = true; |
| 1375 |
|
| 1376 |
glx_blur_dst_end: |
| 1377 |
#ifdef CONFIG_VSYNC_OPENGL_FBO |
| 1378 |
glBindFramebuffer(GL_FRAMEBUFFER, 0); |
| 1379 |
#endif |
| 1380 |
glBindTexture(tex_tgt, 0); |
| 1381 |
glDisable(tex_tgt); |
| 1382 |
if (have_scissors) |
| 1383 |
glEnable(GL_SCISSOR_TEST); |
| 1384 |
if (have_stencil) |
| 1385 |
glEnable(GL_STENCIL_TEST); |
| 1386 |
|
| 1387 |
if (&ibc == pbc) { |
| 1388 |
free_glx_bc(ps, pbc); |
| 1389 |
} |
| 1390 |
|
| 1391 |
glx_check_err(ps); |
| 1392 |
|
| 1393 |
return ret; |
| 1394 |
} |
| 1395 |
#endif |
| 1396 |
|
| 1397 |
bool |
| 1398 |
glx_dim_dst(session_t *ps, int dx, int dy, int width, int height, float z, |
| 1399 |
GLfloat factor, XserverRegion reg_tgt, const reg_data_t *pcache_reg) { |
| 1400 |
// It's possible to dim in glx_render(), but it would be over-complicated |
| 1401 |
// considering all those mess in color negation and modulation |
| 1402 |
glEnable(GL_BLEND); |
| 1403 |
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); |
| 1404 |
glColor4f(0.0f, 0.0f, 0.0f, factor); |
| 1405 |
|
| 1406 |
{ |
| 1407 |
P_PAINTREG_START(); |
| 1408 |
{ |
| 1409 |
GLint rdx = crect.x; |
| 1410 |
GLint rdy = ps->root_height - crect.y; |
| 1411 |
GLint rdxe = rdx + crect.width; |
| 1412 |
GLint rdye = rdy - crect.height; |
| 1413 |
|
| 1414 |
glVertex3i(rdx, rdy, z); |
| 1415 |
glVertex3i(rdxe, rdy, z); |
| 1416 |
glVertex3i(rdxe, rdye, z); |
| 1417 |
glVertex3i(rdx, rdye, z); |
| 1418 |
} |
| 1419 |
P_PAINTREG_END(); |
| 1420 |
} |
| 1421 |
|
| 1422 |
glEnd(); |
| 1423 |
|
| 1424 |
glColor4f(0.0f, 0.0f, 0.0f, 0.0f); |
| 1425 |
glDisable(GL_BLEND); |
| 1426 |
|
| 1427 |
glx_check_err(ps); |
| 1428 |
|
| 1429 |
return true; |
| 1430 |
} |
| 1431 |
|
| 1432 |
/** |
| 1433 |
* @brief Render a region with texture data. |
| 1434 |
*/ |
| 1435 |
bool |
| 1436 |
glx_render_(session_t *ps, const glx_texture_t *ptex, |
| 1437 |
int x, int y, int dx, int dy, int width, int height, int z, |
| 1438 |
double opacity, bool argb, bool neg, |
| 1439 |
XserverRegion reg_tgt, const reg_data_t *pcache_reg |
| 1440 |
#ifdef CONFIG_VSYNC_OPENGL_GLSL |
| 1441 |
, const glx_prog_main_t *pprogram |
| 1442 |
#endif |
| 1443 |
) { |
| 1444 |
if (!ptex || !ptex->texture) { |
| 1445 |
printf_errf("(): Missing texture."); |
| 1446 |
return false; |
| 1447 |
} |
| 1448 |
|
| 1449 |
#ifdef DEBUG_GLX_PAINTREG |
| 1450 |
glx_render_dots(ps, dx, dy, width, height, z, reg_tgt, pcache_reg); |
| 1451 |
return true; |
| 1452 |
#endif |
| 1453 |
|
| 1454 |
argb = argb || (GLX_TEXTURE_FORMAT_RGBA_EXT == |
| 1455 |
ps->psglx->fbconfigs[ptex->depth]->texture_fmt); |
| 1456 |
#ifdef CONFIG_VSYNC_OPENGL_GLSL |
| 1457 |
const bool has_prog = pprogram && pprogram->prog; |
| 1458 |
#endif |
| 1459 |
bool dual_texture = false; |
| 1460 |
|
| 1461 |
// It's required by legacy versions of OpenGL to enable texture target |
| 1462 |
// before specifying environment. Thanks to madsy for telling me. |
| 1463 |
glEnable(ptex->target); |
| 1464 |
|
| 1465 |
// Enable blending if needed |
| 1466 |
if (opacity < 1.0 || argb) { |
| 1467 |
|
| 1468 |
glEnable(GL_BLEND); |
| 1469 |
|
| 1470 |
// Needed for handling opacity of ARGB texture |
| 1471 |
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); |
| 1472 |
|
| 1473 |
// This is all weird, but X Render is using premultiplied ARGB format, and |
| 1474 |
// we need to use those things to correct it. Thanks to derhass for help. |
| 1475 |
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); |
| 1476 |
glColor4f(opacity, opacity, opacity, opacity); |
| 1477 |
} |
| 1478 |
|
| 1479 |
#ifdef CONFIG_VSYNC_OPENGL_GLSL |
| 1480 |
if (!has_prog) |
| 1481 |
#endif |
| 1482 |
{ |
| 1483 |
// The default, fixed-function path |
| 1484 |
// Color negation |
| 1485 |
if (neg) { |
| 1486 |
// Simple color negation |
| 1487 |
if (!glIsEnabled(GL_BLEND)) { |
| 1488 |
glEnable(GL_COLOR_LOGIC_OP); |
| 1489 |
glLogicOp(GL_COPY_INVERTED); |
| 1490 |
} |
| 1491 |
// ARGB texture color negation |
| 1492 |
else if (argb) { |
| 1493 |
dual_texture = true; |
| 1494 |
|
| 1495 |
// Use two texture stages because the calculation is too complicated, |
| 1496 |
// thanks to madsy for providing code |
| 1497 |
// Texture stage 0 |
| 1498 |
glActiveTexture(GL_TEXTURE0); |
| 1499 |
|
| 1500 |
// Negation for premultiplied color: color = A - C |
| 1501 |
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); |
| 1502 |
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_SUBTRACT); |
| 1503 |
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_TEXTURE); |
| 1504 |
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_ALPHA); |
| 1505 |
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_TEXTURE); |
| 1506 |
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR); |
| 1507 |
|
| 1508 |
// Pass texture alpha through |
| 1509 |
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE); |
| 1510 |
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_TEXTURE); |
| 1511 |
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA); |
| 1512 |
|
| 1513 |
// Texture stage 1 |
| 1514 |
glActiveTexture(GL_TEXTURE1); |
| 1515 |
glEnable(ptex->target); |
| 1516 |
glBindTexture(ptex->target, ptex->texture); |
| 1517 |
|
| 1518 |
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); |
| 1519 |
|
| 1520 |
// Modulation with constant factor |
| 1521 |
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE); |
| 1522 |
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PREVIOUS); |
| 1523 |
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR); |
| 1524 |
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_PRIMARY_COLOR); |
| 1525 |
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_ALPHA); |
| 1526 |
|
| 1527 |
// Modulation with constant factor |
| 1528 |
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_MODULATE); |
| 1529 |
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_PREVIOUS); |
| 1530 |
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA); |
| 1531 |
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA, GL_PRIMARY_COLOR); |
| 1532 |
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, GL_SRC_ALPHA); |
| 1533 |
|
| 1534 |
glActiveTexture(GL_TEXTURE0); |
| 1535 |
} |
| 1536 |
// RGB blend color negation |
| 1537 |
else { |
| 1538 |
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); |
| 1539 |
|
| 1540 |
// Modulation with constant factor |
| 1541 |
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE); |
| 1542 |
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_TEXTURE); |
| 1543 |
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_ONE_MINUS_SRC_COLOR); |
| 1544 |
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_PRIMARY_COLOR); |
| 1545 |
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR); |
| 1546 |
|
| 1547 |
// Modulation with constant factor |
| 1548 |
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_MODULATE); |
| 1549 |
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_TEXTURE); |
| 1550 |
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA); |
| 1551 |
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA, GL_PRIMARY_COLOR); |
| 1552 |
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, GL_SRC_ALPHA); |
| 1553 |
} |
| 1554 |
} |
| 1555 |
} |
| 1556 |
#ifdef CONFIG_VSYNC_OPENGL_GLSL |
| 1557 |
else { |
| 1558 |
// Programmable path |
| 1559 |
assert(pprogram->prog); |
| 1560 |
glUseProgram(pprogram->prog); |
| 1561 |
if (pprogram->unifm_opacity >= 0) |
| 1562 |
glUniform1f(pprogram->unifm_opacity, opacity); |
| 1563 |
if (pprogram->unifm_invert_color >= 0) |
| 1564 |
glUniform1i(pprogram->unifm_invert_color, neg); |
| 1565 |
if (pprogram->unifm_tex >= 0) |
| 1566 |
glUniform1i(pprogram->unifm_tex, 0); |
| 1567 |
} |
| 1568 |
#endif |
| 1569 |
|
| 1570 |
#ifdef DEBUG_GLX |
| 1571 |
printf_dbgf("(): Draw: %d, %d, %d, %d -> %d, %d (%d, %d) z %d\n", x, y, width, height, dx, dy, ptex->width, ptex->height, z); |
| 1572 |
#endif |
| 1573 |
|
| 1574 |
// Bind texture |
| 1575 |
glBindTexture(ptex->target, ptex->texture); |
| 1576 |
if (dual_texture) { |
| 1577 |
glActiveTexture(GL_TEXTURE1); |
| 1578 |
glBindTexture(ptex->target, ptex->texture); |
| 1579 |
glActiveTexture(GL_TEXTURE0); |
| 1580 |
} |
| 1581 |
|
| 1582 |
// Painting |
| 1583 |
{ |
| 1584 |
P_PAINTREG_START(); |
| 1585 |
{ |
| 1586 |
GLfloat rx = (double) (crect.x - dx + x); |
| 1587 |
GLfloat ry = (double) (crect.y - dy + y); |
| 1588 |
GLfloat rxe = rx + (double) crect.width; |
| 1589 |
GLfloat rye = ry + (double) crect.height; |
| 1590 |
// Rectangle textures have [0-w] [0-h] while 2D texture has [0-1] [0-1] |
| 1591 |
// Thanks to amonakov for pointing out! |
| 1592 |
if (GL_TEXTURE_2D == ptex->target) { |
| 1593 |
rx = rx / ptex->width; |
| 1594 |
ry = ry / ptex->height; |
| 1595 |
rxe = rxe / ptex->width; |
| 1596 |
rye = rye / ptex->height; |
| 1597 |
} |
| 1598 |
GLint rdx = crect.x; |
| 1599 |
GLint rdy = ps->root_height - crect.y; |
| 1600 |
GLint rdxe = rdx + crect.width; |
| 1601 |
GLint rdye = rdy - crect.height; |
| 1602 |
|
| 1603 |
// Invert Y if needed, this may not work as expected, though. I don't |
| 1604 |
// have such a FBConfig to test with. |
| 1605 |
if (!ptex->y_inverted) { |
| 1606 |
ry = 1.0 - ry; |
| 1607 |
rye = 1.0 - rye; |
| 1608 |
} |
| 1609 |
|
| 1610 |
#ifdef DEBUG_GLX |
| 1611 |
printf_dbgf("(): Rect %d: %f, %f, %f, %f -> %d, %d, %d, %d\n", ri, rx, ry, rxe, rye, rdx, rdy, rdxe, rdye); |
| 1612 |
#endif |
| 1613 |
|
| 1614 |
#define P_TEXCOORD(cx, cy) { \ |
| 1615 |
if (dual_texture) { \ |
| 1616 |
glMultiTexCoord2f(GL_TEXTURE0, cx, cy); \ |
| 1617 |
glMultiTexCoord2f(GL_TEXTURE1, cx, cy); \ |
| 1618 |
} \ |
| 1619 |
else glTexCoord2f(cx, cy); \ |
| 1620 |
} |
| 1621 |
P_TEXCOORD(rx, ry); |
| 1622 |
glVertex3i(rdx, rdy, z); |
| 1623 |
|
| 1624 |
P_TEXCOORD(rxe, ry); |
| 1625 |
glVertex3i(rdxe, rdy, z); |
| 1626 |
|
| 1627 |
P_TEXCOORD(rxe, rye); |
| 1628 |
glVertex3i(rdxe, rdye, z); |
| 1629 |
|
| 1630 |
P_TEXCOORD(rx, rye); |
| 1631 |
glVertex3i(rdx, rdye, z); |
| 1632 |
} |
| 1633 |
P_PAINTREG_END(); |
| 1634 |
} |
| 1635 |
|
| 1636 |
// Cleanup |
| 1637 |
glBindTexture(ptex->target, 0); |
| 1638 |
glColor4f(0.0f, 0.0f, 0.0f, 0.0f); |
| 1639 |
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
| 1640 |
glDisable(GL_BLEND); |
| 1641 |
glDisable(GL_COLOR_LOGIC_OP); |
| 1642 |
glDisable(ptex->target); |
| 1643 |
|
| 1644 |
if (dual_texture) { |
| 1645 |
glActiveTexture(GL_TEXTURE1); |
| 1646 |
glBindTexture(ptex->target, 0); |
| 1647 |
glDisable(ptex->target); |
| 1648 |
glActiveTexture(GL_TEXTURE0); |
| 1649 |
} |
| 1650 |
|
| 1651 |
#ifdef CONFIG_VSYNC_OPENGL_GLSL |
| 1652 |
if (has_prog) |
| 1653 |
glUseProgram(0); |
| 1654 |
#endif |
| 1655 |
|
| 1656 |
glx_check_err(ps); |
| 1657 |
|
| 1658 |
return true; |
| 1659 |
} |
| 1660 |
|
| 1661 |
/** |
| 1662 |
* Render a region with color. |
| 1663 |
* |
| 1664 |
* Unused but can be useful for debugging |
| 1665 |
*/ |
| 1666 |
static void __attribute__((unused)) |
| 1667 |
glx_render_color(session_t *ps, int dx, int dy, int width, int height, int z, |
| 1668 |
XserverRegion reg_tgt, const reg_data_t *pcache_reg) { |
| 1669 |
static int color = 0; |
| 1670 |
|
| 1671 |
color = color % (3 * 3 * 3 - 1) + 1; |
| 1672 |
glColor4f(1.0 / 3.0 * (color / (3 * 3)), |
| 1673 |
1.0 / 3.0 * (color % (3 * 3) / 3), |
| 1674 |
1.0 / 3.0 * (color % 3), |
| 1675 |
1.0f |
| 1676 |
); |
| 1677 |
z -= 0.2; |
| 1678 |
|
| 1679 |
{ |
| 1680 |
P_PAINTREG_START(); |
| 1681 |
{ |
| 1682 |
GLint rdx = crect.x; |
| 1683 |
GLint rdy = ps->root_height - crect.y; |
| 1684 |
GLint rdxe = rdx + crect.width; |
| 1685 |
GLint rdye = rdy - crect.height; |
| 1686 |
|
| 1687 |
glVertex3i(rdx, rdy, z); |
| 1688 |
glVertex3i(rdxe, rdy, z); |
| 1689 |
glVertex3i(rdxe, rdye, z); |
| 1690 |
glVertex3i(rdx, rdye, z); |
| 1691 |
} |
| 1692 |
P_PAINTREG_END(); |
| 1693 |
} |
| 1694 |
glColor4f(0.0f, 0.0f, 0.0f, 0.0f); |
| 1695 |
|
| 1696 |
glx_check_err(ps); |
| 1697 |
} |
| 1698 |
|
| 1699 |
/** |
| 1700 |
* Render a region with dots. |
| 1701 |
* |
| 1702 |
* Unused but can be useful for debugging |
| 1703 |
*/ |
| 1704 |
static void __attribute__((unused)) |
| 1705 |
glx_render_dots(session_t *ps, int dx, int dy, int width, int height, int z, |
| 1706 |
XserverRegion reg_tgt, const reg_data_t *pcache_reg) { |
| 1707 |
glColor4f(0.0f, 0.0f, 0.0f, 1.0f); |
| 1708 |
z -= 0.1; |
| 1709 |
|
| 1710 |
{ |
| 1711 |
P_PAINTREG_START(); |
| 1712 |
{ |
| 1713 |
static const GLint BLK_WID = 5, BLK_HEI = 5; |
| 1714 |
|
| 1715 |
glEnd(); |
| 1716 |
glPointSize(1.0); |
| 1717 |
glBegin(GL_POINTS); |
| 1718 |
|
| 1719 |
GLint rdx = crect.x; |
| 1720 |
GLint rdy = ps->root_height - crect.y; |
| 1721 |
GLint rdxe = rdx + crect.width; |
| 1722 |
GLint rdye = rdy - crect.height; |
| 1723 |
rdx = (rdx) / BLK_WID * BLK_WID; |
| 1724 |
rdy = (rdy) / BLK_HEI * BLK_HEI; |
| 1725 |
rdxe = (rdxe) / BLK_WID * BLK_WID; |
| 1726 |
rdye = (rdye) / BLK_HEI * BLK_HEI; |
| 1727 |
|
| 1728 |
for (GLint cdx = rdx; cdx < rdxe; cdx += BLK_WID) |
| 1729 |
for (GLint cdy = rdy; cdy > rdye; cdy -= BLK_HEI) |
| 1730 |
glVertex3i(cdx + BLK_WID / 2, cdy - BLK_HEI / 2, z); |
| 1731 |
} |
| 1732 |
P_PAINTREG_END(); |
| 1733 |
} |
| 1734 |
glColor4f(0.0f, 0.0f, 0.0f, 0.0f); |
| 1735 |
|
| 1736 |
glx_check_err(ps); |
| 1737 |
} |
| 1738 |
|
| 1739 |
/** |
| 1740 |
* Swap buffer with glXCopySubBufferMESA(). |
| 1741 |
*/ |
| 1742 |
void |
| 1743 |
glx_swap_copysubbuffermesa(session_t *ps, XserverRegion reg) { |
| 1744 |
int nrects = 0; |
| 1745 |
XRectangle *rects = XFixesFetchRegion(ps->dpy, reg, &nrects); |
| 1746 |
|
| 1747 |
if (1 == nrects && rect_is_fullscreen(ps, rects[0].x, rects[0].y, |
| 1748 |
rects[0].width, rects[0].height)) { |
| 1749 |
glXSwapBuffers(ps->dpy, get_tgt_window(ps)); |
| 1750 |
} |
| 1751 |
else { |
| 1752 |
glx_set_clip(ps, None, NULL); |
| 1753 |
for (int i = 0; i < nrects; ++i) { |
| 1754 |
const int x = rects[i].x; |
| 1755 |
const int y = ps->root_height - rects[i].y - rects[i].height; |
| 1756 |
const int wid = rects[i].width; |
| 1757 |
const int hei = rects[i].height; |
| 1758 |
|
| 1759 |
#ifdef DEBUG_GLX |
| 1760 |
printf_dbgf("(): %d, %d, %d, %d\n", x, y, wid, hei); |
| 1761 |
#endif |
| 1762 |
ps->psglx->glXCopySubBufferProc(ps->dpy, get_tgt_window(ps), x, y, wid, hei); |
| 1763 |
} |
| 1764 |
} |
| 1765 |
|
| 1766 |
glx_check_err(ps); |
| 1767 |
|
| 1768 |
cxfree(rects); |
| 1769 |
} |
| 1770 |
|
| 1771 |
/** |
| 1772 |
* @brief Get tightly packed RGB888 data from GL front buffer. |
| 1773 |
* |
| 1774 |
* Don't expect any sort of decent performance. |
| 1775 |
* |
| 1776 |
* @returns tightly packed RGB888 data of the size of the screen, |
| 1777 |
* to be freed with `free()` |
| 1778 |
*/ |
| 1779 |
unsigned char * |
| 1780 |
glx_take_screenshot(session_t *ps, int *out_length) { |
| 1781 |
int length = 3 * ps->root_width * ps->root_height; |
| 1782 |
GLint unpack_align_old = 0; |
| 1783 |
glGetIntegerv(GL_UNPACK_ALIGNMENT, &unpack_align_old); |
| 1784 |
assert(unpack_align_old > 0); |
| 1785 |
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 1786 |
unsigned char *buf = cmalloc(length, unsigned char); |
| 1787 |
glReadBuffer(GL_FRONT); |
| 1788 |
glReadPixels(0, 0, ps->root_width, ps->root_height, GL_RGB, |
| 1789 |
GL_UNSIGNED_BYTE, buf); |
| 1790 |
glReadBuffer(GL_BACK); |
| 1791 |
glPixelStorei(GL_UNPACK_ALIGNMENT, unpack_align_old); |
| 1792 |
if (out_length) |
| 1793 |
*out_length = sizeof(unsigned char) * length; |
| 1794 |
return buf; |
| 1795 |
} |
| 1796 |
|
| 1797 |
#ifdef CONFIG_VSYNC_OPENGL_GLSL |
| 1798 |
GLuint |
| 1799 |
glx_create_shader(GLenum shader_type, const char *shader_str) { |
| 1800 |
#ifdef DEBUG_GLX_GLSL |
| 1801 |
printf("glx_create_shader(): ===\n%s\n===\n", shader_str); |
| 1802 |
fflush(stdout); |
| 1803 |
#endif |
| 1804 |
|
| 1805 |
bool success = false; |
| 1806 |
GLuint shader = glCreateShader(shader_type); |
| 1807 |
if (!shader) { |
| 1808 |
printf_errf("(): Failed to create shader with type %#x.", shader_type); |
| 1809 |
goto glx_create_shader_end; |
| 1810 |
} |
| 1811 |
glShaderSource(shader, 1, &shader_str, NULL); |
| 1812 |
glCompileShader(shader); |
| 1813 |
|
| 1814 |
// Get shader status |
| 1815 |
{ |
| 1816 |
GLint status = GL_FALSE; |
| 1817 |
glGetShaderiv(shader, GL_COMPILE_STATUS, &status); |
| 1818 |
if (GL_FALSE == status) { |
| 1819 |
GLint log_len = 0; |
| 1820 |
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_len); |
| 1821 |
if (log_len) { |
| 1822 |
char log[log_len + 1]; |
| 1823 |
glGetShaderInfoLog(shader, log_len, NULL, log); |
| 1824 |
printf_errf("(): Failed to compile shader with type %d: %s", |
| 1825 |
shader_type, log); |
| 1826 |
} |
| 1827 |
goto glx_create_shader_end; |
| 1828 |
} |
| 1829 |
} |
| 1830 |
|
| 1831 |
success = true; |
| 1832 |
|
| 1833 |
glx_create_shader_end: |
| 1834 |
if (shader && !success) { |
| 1835 |
glDeleteShader(shader); |
| 1836 |
shader = 0; |
| 1837 |
} |
| 1838 |
|
| 1839 |
return shader; |
| 1840 |
} |
| 1841 |
|
| 1842 |
GLuint |
| 1843 |
glx_create_program(const GLuint * const shaders, int nshaders) { |
| 1844 |
bool success = false; |
| 1845 |
GLuint program = glCreateProgram(); |
| 1846 |
if (!program) { |
| 1847 |
printf_errf("(): Failed to create program."); |
| 1848 |
goto glx_create_program_end; |
| 1849 |
} |
| 1850 |
|
| 1851 |
for (int i = 0; i < nshaders; ++i) |
| 1852 |
glAttachShader(program, shaders[i]); |
| 1853 |
glLinkProgram(program); |
| 1854 |
|
| 1855 |
// Get program status |
| 1856 |
{ |
| 1857 |
GLint status = GL_FALSE; |
| 1858 |
glGetProgramiv(program, GL_LINK_STATUS, &status); |
| 1859 |
if (GL_FALSE == status) { |
| 1860 |
GLint log_len = 0; |
| 1861 |
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &log_len); |
| 1862 |
if (log_len) { |
| 1863 |
char log[log_len + 1]; |
| 1864 |
glGetProgramInfoLog(program, log_len, NULL, log); |
| 1865 |
printf_errf("(): Failed to link program: %s", log); |
| 1866 |
} |
| 1867 |
goto glx_create_program_end; |
| 1868 |
} |
| 1869 |
} |
| 1870 |
success = true; |
| 1871 |
|
| 1872 |
glx_create_program_end: |
| 1873 |
if (program) { |
| 1874 |
for (int i = 0; i < nshaders; ++i) |
| 1875 |
glDetachShader(program, shaders[i]); |
| 1876 |
} |
| 1877 |
if (program && !success) { |
| 1878 |
glDeleteProgram(program); |
| 1879 |
program = 0; |
| 1880 |
} |
| 1881 |
|
| 1882 |
return program; |
| 1883 |
} |
| 1884 |
|
| 1885 |
/** |
| 1886 |
* @brief Create a program from vertex and fragment shader strings. |
| 1887 |
*/ |
| 1888 |
GLuint |
| 1889 |
glx_create_program_from_str(const char *vert_shader_str, |
| 1890 |
const char *frag_shader_str) { |
| 1891 |
GLuint vert_shader = 0; |
| 1892 |
GLuint frag_shader = 0; |
| 1893 |
GLuint prog = 0; |
| 1894 |
|
| 1895 |
if (vert_shader_str) |
| 1896 |
vert_shader = glx_create_shader(GL_VERTEX_SHADER, vert_shader_str); |
| 1897 |
if (frag_shader_str) |
| 1898 |
frag_shader = glx_create_shader(GL_FRAGMENT_SHADER, frag_shader_str); |
| 1899 |
|
| 1900 |
{ |
| 1901 |
GLuint shaders[2]; |
| 1902 |
int count = 0; |
| 1903 |
if (vert_shader) |
| 1904 |
shaders[count++] = vert_shader; |
| 1905 |
if (frag_shader) |
| 1906 |
shaders[count++] = frag_shader; |
| 1907 |
assert(count <= sizeof(shaders) / sizeof(shaders[0])); |
| 1908 |
if (count) |
| 1909 |
prog = glx_create_program(shaders, count); |
| 1910 |
} |
| 1911 |
|
| 1912 |
if (vert_shader) |
| 1913 |
glDeleteShader(vert_shader); |
| 1914 |
if (frag_shader) |
| 1915 |
glDeleteShader(frag_shader); |
| 1916 |
|
| 1917 |
return prog; |
| 1918 |
} |
| 1919 |
#endif |
| 1920 |
|