typo fixes
[mplayer/greg.git] / libvo / gl_common.c
blobe6126c05901e8da949c89872becb1be86a2b922e
1 /**
2 * \file gl_common.c
3 * \brief OpenGL helper functions used by vo_gl.c and vo_gl2.c
5 * Common OpenGL routines.
6 * Copyleft (C) Reimar Döffinger <Reimar.Doeffinger@stud.uni-karlsruhe.de>, 2005
7 * Licensend under the GNU GPL v2.
8 * Special thanks go to the xine team and Matthias Hopf, whose video_out_opengl.c
9 * gave me lots of good ideas.
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <ctype.h>
15 #include <math.h>
16 #include "gl_common.h"
18 /**
19 * \defgroup glextfunctions OpenGL extension functions
21 * the pointers to these functions are acquired when the OpenGL
22 * context is created
23 * \{
25 void (APIENTRY *GenBuffers)(GLsizei, GLuint *);
26 void (APIENTRY *DeleteBuffers)(GLsizei, const GLuint *);
27 void (APIENTRY *BindBuffer)(GLenum, GLuint);
28 GLvoid* (APIENTRY *MapBuffer)(GLenum, GLenum);
29 GLboolean (APIENTRY *UnmapBuffer)(GLenum);
30 void (APIENTRY *BufferData)(GLenum, intptr_t, const GLvoid *, GLenum);
31 void (APIENTRY *CombinerParameterfv)(GLenum, const GLfloat *);
32 void (APIENTRY *CombinerParameteri)(GLenum, GLint);
33 void (APIENTRY *CombinerInput)(GLenum, GLenum, GLenum, GLenum, GLenum,
34 GLenum);
35 void (APIENTRY *CombinerOutput)(GLenum, GLenum, GLenum, GLenum, GLenum,
36 GLenum, GLenum, GLboolean, GLboolean,
37 GLboolean);
38 void (APIENTRY *BeginFragmentShader)(void);
39 void (APIENTRY *EndFragmentShader)(void);
40 void (APIENTRY *SampleMap)(GLuint, GLuint, GLenum);
41 void (APIENTRY *ColorFragmentOp2)(GLenum, GLuint, GLuint, GLuint, GLuint,
42 GLuint, GLuint, GLuint, GLuint, GLuint);
43 void (APIENTRY *ColorFragmentOp3)(GLenum, GLuint, GLuint, GLuint, GLuint,
44 GLuint, GLuint, GLuint, GLuint, GLuint,
45 GLuint, GLuint, GLuint);
46 void (APIENTRY *SetFragmentShaderConstant)(GLuint, const GLfloat *);
47 void (APIENTRY *ActiveTexture)(GLenum);
48 void (APIENTRY *BindTexture)(GLenum, GLuint);
49 void (APIENTRY *MultiTexCoord2f)(GLenum, GLfloat, GLfloat);
50 void (APIENTRY *GenPrograms)(GLsizei, GLuint *);
51 void (APIENTRY *DeletePrograms)(GLsizei, const GLuint *);
52 void (APIENTRY *BindProgram)(GLenum, GLuint);
53 void (APIENTRY *ProgramString)(GLenum, GLenum, GLsizei, const GLvoid *);
54 void (APIENTRY *ProgramEnvParameter4f)(GLenum, GLuint, GLfloat, GLfloat,
55 GLfloat, GLfloat);
56 int (APIENTRY *SwapInterval)(int);
57 /** \} */ // end of glextfunctions group
59 //! \defgroup glgeneral OpenGL general helper functions
61 //! \defgroup glcontext OpenGL context management helper functions
63 //! \defgroup gltexture OpenGL texture handling helper functions
65 //! \defgroup glconversion OpenGL conversion helper functions
67 /**
68 * \brief adjusts the GL_UNPACK_ALIGNMENT to fit the stride.
69 * \param stride number of bytes per line for which alignment should fit.
70 * \ingroup glgeneral
72 void glAdjustAlignment(int stride) {
73 GLint gl_alignment;
74 if (stride % 8 == 0)
75 gl_alignment=8;
76 else if (stride % 4 == 0)
77 gl_alignment=4;
78 else if (stride % 2 == 0)
79 gl_alignment=2;
80 else
81 gl_alignment=1;
82 glPixelStorei (GL_UNPACK_ALIGNMENT, gl_alignment);
85 struct gl_name_map_struct {
86 GLint value;
87 char *name;
90 #undef MAP
91 #define MAP(a) {a, #a}
92 //! mapping table for the glValName function
93 static const struct gl_name_map_struct gl_name_map[] = {
94 // internal format
95 MAP(GL_R3_G3_B2), MAP(GL_RGB4), MAP(GL_RGB5), MAP(GL_RGB8),
96 MAP(GL_RGB10), MAP(GL_RGB12), MAP(GL_RGB16), MAP(GL_RGBA2),
97 MAP(GL_RGBA4), MAP(GL_RGB5_A1), MAP(GL_RGBA8), MAP(GL_RGB10_A2),
98 MAP(GL_RGBA12), MAP(GL_RGBA16), MAP(GL_LUMINANCE8),
100 // format
101 MAP(GL_RGB), MAP(GL_RGBA), MAP(GL_RED), MAP(GL_GREEN), MAP(GL_BLUE),
102 MAP(GL_ALPHA), MAP(GL_LUMINANCE), MAP(GL_LUMINANCE_ALPHA),
103 MAP(GL_COLOR_INDEX),
104 // rest 1.2 only
105 MAP(GL_BGR), MAP(GL_BGRA),
107 //type
108 MAP(GL_BYTE), MAP(GL_UNSIGNED_BYTE), MAP(GL_SHORT), MAP(GL_UNSIGNED_SHORT),
109 MAP(GL_INT), MAP(GL_UNSIGNED_INT), MAP(GL_FLOAT), MAP(GL_DOUBLE),
110 MAP(GL_2_BYTES), MAP(GL_3_BYTES), MAP(GL_4_BYTES),
111 // rest 1.2 only
112 MAP(GL_UNSIGNED_BYTE_3_3_2), MAP(GL_UNSIGNED_BYTE_2_3_3_REV),
113 MAP(GL_UNSIGNED_SHORT_5_6_5), MAP(GL_UNSIGNED_SHORT_5_6_5_REV),
114 MAP(GL_UNSIGNED_SHORT_4_4_4_4), MAP(GL_UNSIGNED_SHORT_4_4_4_4_REV),
115 MAP(GL_UNSIGNED_SHORT_5_5_5_1), MAP(GL_UNSIGNED_SHORT_1_5_5_5_REV),
116 MAP(GL_UNSIGNED_INT_8_8_8_8), MAP(GL_UNSIGNED_INT_8_8_8_8_REV),
117 MAP(GL_UNSIGNED_INT_10_10_10_2), MAP(GL_UNSIGNED_INT_2_10_10_10_REV),
118 {0, 0}
120 #undef MAP
123 * \brief return the name of an OpenGL constant
124 * \param value the constant
125 * \return name of the constant or "Unknown format!"
126 * \ingroup glgeneral
128 const char *glValName(GLint value)
130 int i = 0;
132 while (gl_name_map[i].name) {
133 if (gl_name_map[i].value == value)
134 return gl_name_map[i].name;
135 i++;
137 return "Unknown format!";
140 //! always return this format as internal texture format in glFindFormat
141 #define TEXTUREFORMAT_ALWAYS GL_RGB8
142 #undef TEXTUREFORMAT_ALWAYS
145 * \brief find the OpenGL settings coresponding to format.
147 * All parameters may be NULL.
148 * \param fmt MPlayer format to analyze.
149 * \param bpp [OUT] bits per pixel of that format.
150 * \param gl_texfmt [OUT] internal texture format that fits the
151 * image format, not necessarily the best for performance.
152 * \param gl_format [OUT] OpenGL format for this image format.
153 * \param gl_type [OUT] OpenGL type for this image format.
154 * \return 1 if format is supported by OpenGL, 0 if not.
155 * \ingroup gltexture
157 int glFindFormat(uint32_t fmt, int *bpp, GLint *gl_texfmt,
158 GLenum *gl_format, GLenum *gl_type)
160 int supported = 1;
161 int dummy1;
162 GLenum dummy2;
163 GLint dummy3;
164 if (bpp == NULL) bpp = &dummy1;
165 if (gl_texfmt == NULL) gl_texfmt = &dummy3;
166 if (gl_format == NULL) gl_format = &dummy2;
167 if (gl_type == NULL) gl_type = &dummy2;
169 *bpp = IMGFMT_IS_BGR(fmt)?IMGFMT_BGR_DEPTH(fmt):IMGFMT_RGB_DEPTH(fmt);
170 *gl_texfmt = 3;
171 switch (fmt) {
172 case IMGFMT_RGB24:
173 *gl_format = GL_RGB;
174 *gl_type = GL_UNSIGNED_BYTE;
175 break;
176 case IMGFMT_RGBA:
177 *gl_texfmt = 4;
178 *gl_format = GL_RGBA;
179 *gl_type = GL_UNSIGNED_BYTE;
180 break;
181 case IMGFMT_YV12:
182 supported = 0; // no native YV12 support
183 case IMGFMT_Y800:
184 case IMGFMT_Y8:
185 *gl_texfmt = 1;
186 *bpp = 8;
187 *gl_format = GL_LUMINANCE;
188 *gl_type = GL_UNSIGNED_BYTE;
189 break;
190 #if 0
191 // we do not support palettized formats, although the format the
192 // swscale produces works
193 case IMGFMT_RGB8:
194 gl_format = GL_RGB;
195 gl_type = GL_UNSIGNED_BYTE_2_3_3_REV;
196 break;
197 #endif
198 case IMGFMT_RGB15:
199 *gl_format = GL_RGBA;
200 *gl_type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
201 break;
202 case IMGFMT_RGB16:
203 *gl_format = GL_RGB;
204 *gl_type = GL_UNSIGNED_SHORT_5_6_5_REV;
205 break;
206 #if 0
207 case IMGFMT_BGR8:
208 // special case as red and blue have a differen number of bits.
209 // GL_BGR and GL_UNSIGNED_BYTE_3_3_2 isn't supported at least
210 // by nVidia drivers, and in addition would give more bits to
211 // blue than to red, which isn't wanted
212 gl_format = GL_RGB;
213 gl_type = GL_UNSIGNED_BYTE_3_3_2;
214 break;
215 #endif
216 case IMGFMT_BGR15:
217 *gl_format = GL_BGRA;
218 *gl_type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
219 break;
220 case IMGFMT_BGR16:
221 *gl_format = GL_RGB;
222 *gl_type = GL_UNSIGNED_SHORT_5_6_5;
223 break;
224 case IMGFMT_BGR24:
225 *gl_format = GL_BGR;
226 *gl_type = GL_UNSIGNED_BYTE;
227 break;
228 case IMGFMT_BGRA:
229 *gl_texfmt = 4;
230 *gl_format = GL_BGRA;
231 *gl_type = GL_UNSIGNED_BYTE;
232 break;
233 default:
234 *gl_texfmt = 4;
235 *gl_format = GL_RGBA;
236 *gl_type = GL_UNSIGNED_BYTE;
237 supported = 0;
239 #ifdef TEXTUREFORMAT_ALWAYS
240 *gl_texfmt = TEXTUREFORMAT_ALWAYS;
241 #endif
242 return supported;
245 static void *setNull(const GLubyte *s) {
246 return NULL;
249 typedef struct {
250 void **funcptr;
251 char *extstr;
252 char *funcnames[7];
253 } extfunc_desc_t;
255 static const extfunc_desc_t extfuncs[] = {
256 {(void **)&GenBuffers, NULL, {"glGenBuffers", "glGenBuffersARB", NULL}},
257 {(void **)&DeleteBuffers, NULL, {"glDeleteBuffers", "glDeleteBuffersARB", NULL}},
258 {(void **)&BindBuffer, NULL, {"glBindBuffer", "glBindBufferARB", NULL}},
259 {(void **)&MapBuffer, NULL, {"glMapBuffer", "glMapBufferARB", NULL}},
260 {(void **)&UnmapBuffer, NULL, {"glUnmapBuffer", "glUnmapBufferARB", NULL}},
261 {(void **)&BufferData, NULL, {"glBufferData", "glBufferDataARB", NULL}},
262 {(void **)&CombinerParameterfv, NULL, {"glCombinerParameterfv", "glCombinerParameterfvNV", NULL}},
263 {(void **)&CombinerParameteri, NULL, {"glCombinerParameteri", "glCombinerParameteriNV", NULL}},
264 {(void **)&CombinerInput, NULL, {"glCombinerInput", "glCombinerInputNV", NULL}},
265 {(void **)&CombinerOutput, NULL, {"glCombinerOutput", "glCombinerOutputNV", NULL}},
266 {(void **)&BeginFragmentShader, NULL, {"glBeginFragmentShaderATI", NULL}},
267 {(void **)&EndFragmentShader, NULL, {"glEndFragmentShaderATI", NULL}},
268 {(void **)&SampleMap, NULL, {"glSampleMapATI", NULL}},
269 {(void **)&ColorFragmentOp2, NULL, {"glColorFragmentOp2ATI", NULL}},
270 {(void **)&ColorFragmentOp3, NULL, {"glColorFragmentOp3ATI", NULL}},
271 {(void **)&SetFragmentShaderConstant, NULL, {"glSetFragmentShaderConstantATI", NULL}},
272 {(void **)&ActiveTexture, NULL, {"glActiveTexture", "glActiveTextureARB", NULL}},
273 {(void **)&BindTexture, NULL, {"glBindTexture", "glBindTextureARB", "glBindTextureEXT", NULL}},
274 {(void **)&MultiTexCoord2f, NULL, {"glMultiTexCoord2f", "glMultiTexCoord2fARB", NULL}},
275 {(void **)&GenPrograms, NULL, {"glGenPrograms", "glGenProgramsARB", "glGenProgramsNV", NULL}},
276 {(void **)&DeletePrograms, NULL, {"glDeletePrograms", "glDeleteProgramsARB", "glDeleteProgramsNV", NULL}},
277 {(void **)&BindProgram, NULL, {"glBindProgram", "glBindProgramARB", "glBindProgramNV", NULL}},
278 {(void **)&ProgramString, NULL, {"glProgramString", "glProgramStringARB", "glProgramStringNV", NULL}},
279 {(void **)&ProgramEnvParameter4f, NULL, {"glProgramEnvParameter4f", "glProgramEnvParameter4fARB", "glProgramEnvParameter4fNV", NULL}},
280 {(void **)&SwapInterval, "_swap_control", {"glXSwapInterval", "glXSwapIntervalEXT", "glXSwapIntervalSGI", "wglSwapInterval", "wglSwapIntervalEXT", "wglSwapIntervalSGI", NULL}},
281 {NULL}
285 * \brief find the function pointers of some useful OpenGL extensions
286 * \param getProcAddress function to resolve function names, may be NULL
287 * \param ext2 an extra extension string
289 static void getFunctions(void *(*getProcAddress)(const GLubyte *),
290 const char *ext2) {
291 const extfunc_desc_t *dsc;
292 const char *extensions = (const char *)glGetString(GL_EXTENSIONS);
293 char *allexts;
294 if (!extensions) extensions = "";
295 if (!ext2) ext2 = "";
296 allexts = (char *)malloc(strlen(extensions) + strlen(ext2) + 2);
297 strcpy(allexts, extensions);
298 strcat(allexts, " ");
299 strcat(allexts, ext2);
300 mp_msg(MSGT_VO, MSGL_V, "OpenGL extensions string:\n%s\n", allexts);
301 if (!getProcAddress)
302 getProcAddress = setNull;
303 for (dsc = extfuncs; dsc->funcptr; dsc++) {
304 void *ptr = NULL;
305 int i;
306 if (!dsc->extstr || strstr(allexts, dsc->extstr)) {
307 for (i = 0; !ptr && dsc->funcnames[i]; i++)
308 ptr = getProcAddress((const GLubyte *)dsc->funcnames[i]);
310 *(dsc->funcptr) = ptr;
312 free(allexts);
316 * \brief create a texture and set some defaults
317 * \param target texture taget, usually GL_TEXTURE_2D
318 * \param fmt internal texture format
319 * \param filter filter used for scaling, e.g. GL_LINEAR
320 * \param w texture width
321 * \param h texture height
322 * \param val luminance value to fill texture with
323 * \ingroup gltexture
325 void glCreateClearTex(GLenum target, GLenum fmt, GLint filter,
326 int w, int h, unsigned char val) {
327 GLfloat fval = (GLfloat)val / 255.0;
328 GLfloat border[4] = {fval, fval, fval, fval};
329 GLenum clrfmt = (fmt == GL_ALPHA) ? GL_ALPHA : GL_LUMINANCE;
330 char *init = (char *)malloc(w * h);
331 memset(init, val, w * h);
332 glAdjustAlignment(w);
333 glPixelStorei(GL_UNPACK_ROW_LENGTH, w);
334 glTexImage2D(target, 0, fmt, w, h, 0, clrfmt, GL_UNSIGNED_BYTE, init);
335 glTexParameterf(target, GL_TEXTURE_PRIORITY, 1.0);
336 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
337 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
338 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
339 glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
340 // Border texels should not be used with CLAMP_TO_EDGE
341 // We set a sane default anyway.
342 glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, border);
343 free(init);
347 * \brief skips whitespace and comments
348 * \param f file to read from
350 static void ppm_skip(FILE *f) {
351 int c, comment = 0;
352 do {
353 c = fgetc(f);
354 if (c == '#')
355 comment = 1;
356 if (c == '\n')
357 comment = 0;
358 } while (c != EOF && (isspace(c) || comment));
359 if (c != EOF)
360 ungetc(c, f);
363 #define MAXDIM (16 * 1024)
366 * \brief creates a texture from a PPM file
367 * \param target texture taget, usually GL_TEXTURE_2D
368 * \param fmt internal texture format
369 * \param filter filter used for scaling, e.g. GL_LINEAR
370 * \param f file to read PPM from
371 * \param width [out] width of texture
372 * \param height [out] height of texture
373 * \param maxval [out] maxval value from PPM file
374 * \return 0 on error, 1 otherwise
375 * \ingroup gltexture
377 int glCreatePPMTex(GLenum target, GLenum fmt, GLint filter,
378 FILE *f, int *width, int *height, int *maxval) {
379 unsigned w, h, m, val;
380 char *data;
381 ppm_skip(f);
382 if (fgetc(f) != 'P' || fgetc(f) != '6')
383 return 0;
384 ppm_skip(f);
385 if (fscanf(f, "%u", &w) != 1)
386 return 0;
387 ppm_skip(f);
388 if (fscanf(f, "%u", &h) != 1)
389 return 0;
390 ppm_skip(f);
391 if (fscanf(f, "%u", &m) != 1)
392 return 0;
393 val = fgetc(f);
394 if (!isspace(val))
395 return 0;
396 if (w > MAXDIM || h > MAXDIM)
397 return 0;
398 data = (char *)malloc(w * h * 3);
399 if (fread(data, w * 3, h, f) != h)
400 return 0;
401 glCreateClearTex(target, fmt, filter, w, h, 0);
402 glUploadTex(target, GL_RGB, GL_UNSIGNED_BYTE, data, w * 3, 0, 0, w, h, 0);
403 free(data);
404 if (width) *width = w;
405 if (height) *height = h;
406 if (maxval) *maxval = m;
407 return 1;
411 * \brief return the number of bytes per pixel for the given format
412 * \param format OpenGL format
413 * \param type OpenGL type
414 * \return bytes per pixel
415 * \ingroup glgeneral
417 * Does not handle all possible variants, just those used by MPlayer
419 int glFmt2bpp(GLenum format, GLenum type) {
420 switch (type) {
421 case GL_UNSIGNED_BYTE_3_3_2:
422 case GL_UNSIGNED_BYTE_2_3_3_REV:
423 return 1;
424 case GL_UNSIGNED_SHORT_5_5_5_1:
425 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
426 case GL_UNSIGNED_SHORT_5_6_5:
427 case GL_UNSIGNED_SHORT_5_6_5_REV:
428 return 2;
430 if (type != GL_UNSIGNED_BYTE)
431 return 0; //not implemented
432 switch (format) {
433 case GL_LUMINANCE:
434 case GL_ALPHA:
435 return 1;
436 case GL_RGB:
437 case GL_BGR:
438 return 3;
439 case GL_RGBA:
440 case GL_BGRA:
441 return 4;
443 return 0; // unknown
447 * \brief upload a texture, handling things like stride and slices
448 * \param target texture target, usually GL_TEXTURE_2D
449 * \param format OpenGL format of data
450 * \param type OpenGL type of data
451 * \param data data to upload
452 * \param stride data stride
453 * \param x x offset in texture
454 * \param y y offset in texture
455 * \param w width of the texture part to upload
456 * \param h height of the texture part to upload
457 * \param slice height of an upload slice, 0 for all at once
458 * \ingroup gltexture
460 void glUploadTex(GLenum target, GLenum format, GLenum type,
461 const void *data, int stride,
462 int x, int y, int w, int h, int slice) {
463 int y_max = y + h;
464 if (w <= 0 || h <= 0) return;
465 if (slice <= 0)
466 slice = h;
467 if (stride < 0) {
468 data += h * stride;
469 stride = -stride;
471 // this is not always correct, but should work for MPlayer
472 glAdjustAlignment(stride);
473 glPixelStorei(GL_UNPACK_ROW_LENGTH, stride / glFmt2bpp(format, type));
474 for (; y + slice <= y_max; y += slice) {
475 glTexSubImage2D(target, 0, x, y, w, slice, format, type, data);
476 data += stride * slice;
478 if (y < y_max)
479 glTexSubImage2D(target, 0, x, y, w, y_max - y, format, type, data);
482 static void fillUVcoeff(GLfloat *ucoef, GLfloat *vcoef,
483 float uvcos, float uvsin) {
484 int i;
485 ucoef[0] = 0 * uvcos + 1.403 * uvsin;
486 vcoef[0] = 0 * uvsin + 1.403 * uvcos;
487 ucoef[1] = -0.344 * uvcos + -0.714 * uvsin;
488 vcoef[1] = -0.344 * uvsin + -0.714 * uvcos;
489 ucoef[2] = 1.770 * uvcos + 0 * uvsin;
490 vcoef[2] = 1.770 * uvsin + 0 * uvcos;
491 ucoef[3] = 0;
492 vcoef[3] = 0;
493 // Coefficients (probably) must be in [0, 1] range, whereas they originally
494 // are in [-2, 2] range, so here comes the trick:
495 // First put them in the [-0.5, 0.5] range, then add 0.5.
496 // This can be undone with the HALF_BIAS and SCALE_BY_FOUR arguments
497 // for CombinerInput and CombinerOutput (or the respective ATI variants)
498 for (i = 0; i < 4; i++) {
499 ucoef[i] = ucoef[i] * 0.25 + 0.5;
500 vcoef[i] = vcoef[i] * 0.25 + 0.5;
505 * \brief Setup register combiners for YUV to RGB conversion.
506 * \param uvcos used for saturation and hue adjustment
507 * \param uvsin used for saturation and hue adjustment
509 static void glSetupYUVCombiners(float uvcos, float uvsin) {
510 GLfloat ucoef[4];
511 GLfloat vcoef[4];
512 GLint i;
513 glGetIntegerv(GL_MAX_GENERAL_COMBINERS_NV, &i);
514 if (i < 2)
515 mp_msg(MSGT_VO, MSGL_ERR,
516 "[gl] 2 general combiners needed for YUV combiner support (found %i)\n", i);
517 glGetIntegerv (GL_MAX_TEXTURE_UNITS, &i);
518 if (i < 3)
519 mp_msg(MSGT_VO, MSGL_ERR,
520 "[gl] 3 texture units needed for YUV combiner support (found %i)\n", i);
521 if (!CombinerInput || !CombinerOutput ||
522 !CombinerParameterfv || !CombinerParameteri) {
523 mp_msg(MSGT_VO, MSGL_FATAL, "[gl] Combiner functions missing!\n");
524 return;
526 fillUVcoeff(ucoef, vcoef, uvcos, uvsin);
527 CombinerParameterfv(GL_CONSTANT_COLOR0_NV, ucoef);
528 CombinerParameterfv(GL_CONSTANT_COLOR1_NV, vcoef);
530 // UV first, like this green component cannot overflow
531 CombinerInput(GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_A_NV,
532 GL_TEXTURE1, GL_HALF_BIAS_NORMAL_NV, GL_RGB);
533 CombinerInput(GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_B_NV,
534 GL_CONSTANT_COLOR0_NV, GL_HALF_BIAS_NORMAL_NV, GL_RGB);
535 CombinerInput(GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_C_NV,
536 GL_TEXTURE2, GL_HALF_BIAS_NORMAL_NV, GL_RGB);
537 CombinerInput(GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_D_NV,
538 GL_CONSTANT_COLOR1_NV, GL_HALF_BIAS_NORMAL_NV, GL_RGB);
539 CombinerOutput(GL_COMBINER0_NV, GL_RGB, GL_DISCARD_NV, GL_DISCARD_NV,
540 GL_SPARE0_NV, GL_SCALE_BY_FOUR_NV, GL_NONE, GL_FALSE,
541 GL_FALSE, GL_FALSE);
543 // stage 2
544 CombinerInput(GL_COMBINER1_NV, GL_RGB, GL_VARIABLE_A_NV, GL_SPARE0_NV,
545 GL_SIGNED_IDENTITY_NV, GL_RGB);
546 CombinerInput(GL_COMBINER1_NV, GL_RGB, GL_VARIABLE_B_NV, GL_ZERO,
547 GL_UNSIGNED_INVERT_NV, GL_RGB);
548 CombinerInput(GL_COMBINER1_NV, GL_RGB, GL_VARIABLE_C_NV,
549 GL_TEXTURE0, GL_SIGNED_IDENTITY_NV, GL_RGB);
550 CombinerInput(GL_COMBINER1_NV, GL_RGB, GL_VARIABLE_D_NV, GL_ZERO,
551 GL_UNSIGNED_INVERT_NV, GL_RGB);
552 CombinerOutput(GL_COMBINER1_NV, GL_RGB, GL_DISCARD_NV, GL_DISCARD_NV,
553 GL_SPARE0_NV, GL_NONE, GL_NONE, GL_FALSE,
554 GL_FALSE, GL_FALSE);
556 // leave final combiner stage in default mode
557 CombinerParameteri(GL_NUM_GENERAL_COMBINERS_NV, 2);
561 * \brief Setup ATI version of register combiners for YUV to RGB conversion.
562 * \param uvcos used for saturation and hue adjustment
563 * \param uvsin used for saturation and hue adjustment
565 * ATI called this fragment shader, but the name is confusing in the
566 * light of a very different OpenGL 2.0 extension with the same name
568 static void glSetupYUVCombinersATI(float uvcos, float uvsin) {
569 GLfloat ucoef[4];
570 GLfloat vcoef[4];
571 GLint i;
572 glGetIntegerv(GL_NUM_FRAGMENT_REGISTERS_ATI, &i);
573 if (i < 3)
574 mp_msg(MSGT_VO, MSGL_ERR,
575 "[gl] 3 registers needed for YUV combiner (ATI) support (found %i)\n", i);
576 glGetIntegerv (GL_MAX_TEXTURE_UNITS, &i);
577 if (i < 3)
578 mp_msg(MSGT_VO, MSGL_ERR,
579 "[gl] 3 texture units needed for YUV combiner (ATI) support (found %i)\n", i);
580 if (!BeginFragmentShader || !EndFragmentShader ||
581 !SetFragmentShaderConstant || !SampleMap ||
582 !ColorFragmentOp2 || !ColorFragmentOp3) {
583 mp_msg(MSGT_VO, MSGL_FATAL, "[gl] Combiner (ATI) functions missing!\n");
584 return;
586 fillUVcoeff(ucoef, vcoef, uvcos, uvsin);
587 BeginFragmentShader();
588 SetFragmentShaderConstant(GL_CON_0_ATI, ucoef);
589 SetFragmentShaderConstant(GL_CON_1_ATI, vcoef);
590 SampleMap(GL_REG_0_ATI, GL_TEXTURE0, GL_SWIZZLE_STR_ATI);
591 SampleMap(GL_REG_1_ATI, GL_TEXTURE1, GL_SWIZZLE_STR_ATI);
592 SampleMap(GL_REG_2_ATI, GL_TEXTURE2, GL_SWIZZLE_STR_ATI);
593 // UV first, like this green component cannot overflow
594 ColorFragmentOp2(GL_MUL_ATI, GL_REG_1_ATI, GL_NONE, GL_NONE,
595 GL_REG_1_ATI, GL_NONE, GL_BIAS_BIT_ATI,
596 GL_CON_0_ATI, GL_NONE, GL_BIAS_BIT_ATI);
597 ColorFragmentOp3(GL_MAD_ATI, GL_REG_2_ATI, GL_NONE, GL_4X_BIT_ATI,
598 GL_REG_2_ATI, GL_NONE, GL_BIAS_BIT_ATI,
599 GL_CON_1_ATI, GL_NONE, GL_BIAS_BIT_ATI,
600 GL_REG_1_ATI, GL_NONE, GL_NONE);
601 ColorFragmentOp2(GL_ADD_ATI, GL_REG_0_ATI, GL_NONE, GL_NONE,
602 GL_REG_0_ATI, GL_NONE, GL_NONE,
603 GL_REG_2_ATI, GL_NONE, GL_NONE);
604 EndFragmentShader();
607 static const char *yuv_prog_template =
608 "!!ARBfp1.0\n"
609 "OPTION ARB_precision_hint_fastest;"
610 "PARAM ycoef = {%.4f, %.4f, %.4f};"
611 "PARAM ucoef = {%.4f, %.4f, %.4f};"
612 "PARAM vcoef = {%.4f, %.4f, %.4f};"
613 "PARAM offsets = {%.4f, %.4f, %.4f};"
614 "PARAM gamma = {%.4f, %.4f, %.4f};"
615 "TEMP res, y, u, v;"
616 "TEX y, fragment.texcoord[0], texture[0], %s;"
617 "MAD res, y, ycoef, offsets;"
618 "TEX u, fragment.texcoord[1], texture[1], %s;"
619 "MAD res, u, ucoef, res;"
620 "TEX v, fragment.texcoord[2], texture[2], %s;"
621 "MAD result.color, v, vcoef, res;"
622 "END";
624 static const char *yuv_pow_prog_template =
625 "!!ARBfp1.0\n"
626 "OPTION ARB_precision_hint_fastest;"
627 "PARAM ycoef = {%.4f, %.4f, %.4f};"
628 "PARAM ucoef = {%.4f, %.4f, %.4f};"
629 "PARAM vcoef = {%.4f, %.4f, %.4f};"
630 "PARAM offsets = {%.4f, %.4f, %.4f};"
631 "PARAM gamma = {%.4f, %.4f, %.4f};"
632 "TEMP res, y, u, v;"
633 "TEX y, fragment.texcoord[0], texture[0], %s;"
634 "MAD res, y, ycoef, offsets;"
635 "TEX u, fragment.texcoord[1], texture[1], %s;"
636 "MAD res, u, ucoef, res;"
637 "TEX v, fragment.texcoord[2], texture[2], %s;"
638 "MAD_SAT res, v, vcoef, res;"
639 "POW result.color.r, res.r, gamma.r;"
640 "POW result.color.g, res.g, gamma.g;"
641 "POW result.color.b, res.b, gamma.b;"
642 "END";
644 static const char *yuv_lookup_prog_template =
645 "!!ARBfp1.0\n"
646 "OPTION ARB_precision_hint_fastest;"
647 "PARAM ycoef = {%.4f, %.4f, %.4f, 0};"
648 "PARAM ucoef = {%.4f, %.4f, %.4f, 0};"
649 "PARAM vcoef = {%.4f, %.4f, %.4f, 0};"
650 "PARAM offsets = {%.4f, %.4f, %.4f, 0.125};"
651 "PARAM gamma = {%.4f, %.4f, %.4f};"
652 "TEMP res, y, u, v;"
653 "TEX y, fragment.texcoord[0], texture[0], %s;"
654 "MAD res, y, ycoef, offsets;"
655 "TEX u, fragment.texcoord[1], texture[1], %s;"
656 "MAD res, u, ucoef, res;"
657 "TEX v, fragment.texcoord[2], texture[2], %s;"
658 "MAD res, v, vcoef, res;"
659 "TEX result.color.r, res.raaa, texture[3], 2D;"
660 "ADD res.a, res.a, 0.25;"
661 "TEX result.color.g, res.gaaa, texture[3], 2D;"
662 "ADD res.a, res.a, 0.25;"
663 "TEX result.color.b, res.baaa, texture[3], 2D;"
664 "END";
667 * \brief setup a fragment program that will do YUV->RGB conversion
668 * \param brightness brightness adjustment offset
669 * \param contrast contrast adjustment factor
670 * \param uvcos used for saturation and hue adjustment
671 * \param uvsin used for saturation and hue adjustment
672 * \param lookup use fragment program that uses texture unit 4 to
673 * do additional conversion via lookup.
675 static void glSetupYUVFragprog(float brightness, float contrast,
676 float uvcos, float uvsin, float rgamma,
677 float ggamma, float bgamma, int type, int rect) {
678 char yuv_prog[1000];
679 const char *prog_template = yuv_prog_template;
680 char *tex_type = rect ? "RECT" : "2D";
681 int lookup = 0;
682 GLint i;
683 // this is the conversion matrix, with y, u, v factors
684 // for red, green, blue and the constant offsets
685 float ry, ru, rv, rc;
686 float gy, gu, gv, gc;
687 float by, bu, bv, bc;
688 switch (type) {
689 case YUV_CONVERSION_FRAGMENT_POW:
690 prog_template = yuv_pow_prog_template;
691 break;
692 case YUV_CONVERSION_FRAGMENT_LOOKUP:
693 prog_template = yuv_lookup_prog_template;
694 lookup = 1;
695 break;
697 glGetIntegerv (GL_MAX_TEXTURE_UNITS, &i);
698 if (i < 3)
699 mp_msg(MSGT_VO, MSGL_ERR,
700 "[gl] 3 texture units needed for YUV fragment support (found %i)\n", i);
701 if (lookup && i < 4)
702 mp_msg(MSGT_VO, MSGL_ERR,
703 "[gl] 4 texture units needed for YUV fragment support with lookup (found %i)\n", i);
704 if (!ProgramString) {
705 mp_msg(MSGT_VO, MSGL_FATAL, "[gl] ProgramString function missing!\n");
706 return;
708 ry = 1.164 * contrast;
709 gy = 1.164 * contrast;
710 by = 1.164 * contrast;
711 ru = 0 * uvcos + 1.596 * uvsin;
712 rv = 0 * uvsin + 1.596 * uvcos;
713 gu = -0.391 * uvcos + -0.813 * uvsin;
714 gv = -0.391 * uvsin + -0.813 * uvcos;
715 bu = 2.018 * uvcos + 0 * uvsin;
716 bv = 2.018 * uvsin + 0 * uvcos;
717 rc = (-16 * ry + (-128) * ru + (-128) * rv) / 255.0 + brightness;
718 gc = (-16 * gy + (-128) * gu + (-128) * gv) / 255.0 + brightness;
719 bc = (-16 * by + (-128) * bu + (-128) * bv) / 255.0 + brightness;
720 // these "center" contrast control so that e.g. a contrast of 0
721 // leads to a grey image, not a black one
722 rc += 0.5 - contrast / 2.0;
723 gc += 0.5 - contrast / 2.0;
724 bc += 0.5 - contrast / 2.0;
725 rgamma = 1.0 / rgamma;
726 ggamma = 1.0 / ggamma;
727 bgamma = 1.0 / bgamma;
728 snprintf(yuv_prog, 1000, prog_template, ry, gy, by, ru, gu, bu, rv, gv, bv,
729 rc, gc, bc, rgamma, bgamma, bgamma, tex_type, tex_type, tex_type);
730 ProgramString(GL_FRAGMENT_PROGRAM, GL_PROGRAM_FORMAT_ASCII,
731 strlen(yuv_prog), yuv_prog);
732 glGetIntegerv(GL_PROGRAM_ERROR_POSITION, &i);
733 if (i != -1)
734 mp_msg(MSGT_VO, MSGL_ERR,
735 "[gl] Error compiling fragment program, make sure your card supports\n"
736 "GL_ARB_fragment_program (use glxinfo to check).%.10s\n", &yuv_prog[i]);
740 * \brief little helper function to create a lookup table for gamma
741 * \param map buffer to create map into
742 * \param size size of buffer
743 * \param gamma gamma value
745 static void gen_gamma_map(unsigned char *map, int size, float gamma) {
746 int i;
747 gamma = 1.0 / gamma;
748 for (i = 0; i < size; i++) {
749 float tmp = (float)i / (size - 1.0);
750 tmp = pow(tmp, gamma);
751 if (tmp > 1.0) tmp = 1.0;
752 if (tmp < 0.0) tmp = 0.0;
753 map[i] = 255 * tmp;
757 //! resolution of texture for gamma lookup table
758 #define LOOKUP_RES 512
761 * \brief setup YUV->RGB conversion
762 * \param target texture target for Y, U and V textures (e.g. GL_TEXTURE_2D)
763 * \param type YUV conversion type
764 * \param brightness brightness adjustment offset
765 * \param contrast contrast adjustment factor
766 * \param hue hue adjustment angle
767 * \param saturation saturation adjustment factor
768 * \param rgamma gamma value for red channel
769 * \param ggamma gamma value for green channel
770 * \param bgamma gamma value for blue channel
771 * \ingroup glconversion
773 void glSetupYUVConversion(GLenum target, int type,
774 float brightness, float contrast,
775 float hue, float saturation,
776 float rgamma, float ggamma, float bgamma) {
777 float uvcos = saturation * cos(hue);
778 float uvsin = saturation * sin(hue);
779 switch (type) {
780 case YUV_CONVERSION_COMBINERS:
781 glSetupYUVCombiners(uvcos, uvsin);
782 break;
783 case YUV_CONVERSION_COMBINERS_ATI:
784 glSetupYUVCombinersATI(uvcos, uvsin);
785 break;
786 case YUV_CONVERSION_FRAGMENT_LOOKUP:
788 unsigned char lookup_data[4 * LOOKUP_RES];
789 gen_gamma_map(lookup_data, LOOKUP_RES, rgamma);
790 gen_gamma_map(&lookup_data[LOOKUP_RES], LOOKUP_RES, ggamma);
791 gen_gamma_map(&lookup_data[2 * LOOKUP_RES], LOOKUP_RES, bgamma);
792 ActiveTexture(GL_TEXTURE3);
793 glCreateClearTex(GL_TEXTURE_2D, GL_LUMINANCE8, GL_LINEAR,
794 LOOKUP_RES, 4, 0);
795 glUploadTex(GL_TEXTURE_2D, GL_LUMINANCE, GL_UNSIGNED_BYTE, lookup_data,
796 LOOKUP_RES, 0, 0, LOOKUP_RES, 4, 0);
797 ActiveTexture(GL_TEXTURE0);
799 case YUV_CONVERSION_FRAGMENT:
800 case YUV_CONVERSION_FRAGMENT_POW:
801 glSetupYUVFragprog(brightness, contrast, uvcos, uvsin,
802 rgamma, ggamma, bgamma, type,
803 target == GL_TEXTURE_RECTANGLE);
804 break;
809 * \brief enable the specified YUV conversion
810 * \param target texture target for Y, U and V textures (e.g. GL_TEXTURE_2D)
811 * \param type type of YUV conversion
812 * \ingroup glconversion
814 void glEnableYUVConversion(GLenum target, int type) {
815 if (type <= 0) return;
816 switch (type) {
817 case YUV_CONVERSION_COMBINERS:
818 ActiveTexture(GL_TEXTURE1);
819 glEnable(target);
820 ActiveTexture(GL_TEXTURE2);
821 glEnable(target);
822 ActiveTexture(GL_TEXTURE0);
823 glEnable(GL_REGISTER_COMBINERS_NV);
824 break;
825 case YUV_CONVERSION_COMBINERS_ATI:
826 ActiveTexture(GL_TEXTURE1);
827 glEnable(target);
828 ActiveTexture(GL_TEXTURE2);
829 glEnable(target);
830 ActiveTexture(GL_TEXTURE0);
831 glEnable(GL_FRAGMENT_SHADER_ATI);
832 break;
833 case YUV_CONVERSION_FRAGMENT_LOOKUP:
834 case YUV_CONVERSION_FRAGMENT_POW:
835 case YUV_CONVERSION_FRAGMENT:
836 glEnable(GL_FRAGMENT_PROGRAM);
837 break;
842 * \brief disable the specified YUV conversion
843 * \param target texture target for Y, U and V textures (e.g. GL_TEXTURE_2D)
844 * \param type type of YUV conversion
845 * \ingroup glconversion
847 void glDisableYUVConversion(GLenum target, int type) {
848 if (type <= 0) return;
849 switch (type) {
850 case YUV_CONVERSION_COMBINERS:
851 ActiveTexture(GL_TEXTURE1);
852 glDisable(target);
853 ActiveTexture(GL_TEXTURE2);
854 glDisable(target);
855 ActiveTexture(GL_TEXTURE0);
856 glDisable(GL_REGISTER_COMBINERS_NV);
857 break;
858 case YUV_CONVERSION_COMBINERS_ATI:
859 ActiveTexture(GL_TEXTURE1);
860 glDisable(target);
861 ActiveTexture(GL_TEXTURE2);
862 glDisable(target);
863 ActiveTexture(GL_TEXTURE0);
864 glDisable(GL_FRAGMENT_SHADER_ATI);
865 break;
866 case YUV_CONVERSION_FRAGMENT_LOOKUP:
867 case YUV_CONVERSION_FRAGMENT_POW:
868 case YUV_CONVERSION_FRAGMENT:
869 glDisable(GL_FRAGMENT_PROGRAM);
870 break;
875 * \brief draw a texture part at given 2D coordinates
876 * \param x screen top coordinate
877 * \param y screen left coordinate
878 * \param w screen width coordinate
879 * \param h screen height coordinate
880 * \param tx texture top coordinate in pixels
881 * \param ty texture left coordinate in pixels
882 * \param tw texture part width in pixels
883 * \param th texture part height in pixels
884 * \param sx width of texture in pixels
885 * \param sy height of texture in pixels
886 * \param rect_tex whether this texture uses texture_rectangle extension
887 * \param is_yv12 if set, also draw the textures from units 1 and 2
888 * \param flip flip the texture upside down
889 * \ingroup gltexture
891 void glDrawTex(GLfloat x, GLfloat y, GLfloat w, GLfloat h,
892 GLfloat tx, GLfloat ty, GLfloat tw, GLfloat th,
893 int sx, int sy, int rect_tex, int is_yv12, int flip) {
894 GLfloat tx2 = tx / 2, ty2 = ty / 2, tw2 = tw / 2, th2 = th / 2;
895 if (!rect_tex) {
896 tx /= sx; ty /= sy; tw /= sx; th /= sy;
897 tx2 = tx, ty2 = ty, tw2 = tw, th2 = th;
899 if (flip) {
900 y += h;
901 h = -h;
903 glBegin(GL_QUADS);
904 glTexCoord2f(tx, ty);
905 if (is_yv12) {
906 MultiTexCoord2f(GL_TEXTURE1, tx2, ty2);
907 MultiTexCoord2f(GL_TEXTURE2, tx2, ty2);
909 glVertex2f(x, y);
910 glTexCoord2f(tx, ty + th);
911 if (is_yv12) {
912 MultiTexCoord2f(GL_TEXTURE1, tx2, ty2 + th2);
913 MultiTexCoord2f(GL_TEXTURE2, tx2, ty2 + th2);
915 glVertex2f(x, y + h);
916 glTexCoord2f(tx + tw, ty + th);
917 if (is_yv12) {
918 MultiTexCoord2f(GL_TEXTURE1, tx2 + tw2, ty2 + th2);
919 MultiTexCoord2f(GL_TEXTURE2, tx2 + tw2, ty2 + th2);
921 glVertex2f(x + w, y + h);
922 glTexCoord2f(tx + tw, ty);
923 if (is_yv12) {
924 MultiTexCoord2f(GL_TEXTURE1, tx2 + tw2, ty2);
925 MultiTexCoord2f(GL_TEXTURE2, tx2 + tw2, ty2);
927 glVertex2f(x + w, y);
928 glEnd();
931 #ifdef GL_WIN32
932 #include "w32_common.h"
934 * \brief little helper since wglGetProcAddress definition does not fit our
935 * getProcAddress
936 * \param procName name of function to look up
937 * \return function pointer returned by wglGetProcAddress
939 static void *w32gpa(const GLubyte *procName) {
940 return wglGetProcAddress(procName);
943 int setGlWindow(int *vinfo, HGLRC *context, HWND win)
945 int new_vinfo;
946 HDC windc = GetDC(win);
947 HGLRC new_context = 0;
948 int keep_context = 0;
950 // should only be needed when keeping context, but not doing glFinish
951 // can cause flickering even when we do not keep it.
952 if (*context)
953 glFinish();
954 new_vinfo = GetPixelFormat(windc);
955 if (*context && *vinfo && new_vinfo && *vinfo == new_vinfo) {
956 // we can keep the wglContext
957 new_context = *context;
958 keep_context = 1;
959 } else {
960 // create a context
961 new_context = wglCreateContext(windc);
962 if (!new_context) {
963 mp_msg(MSGT_VO, MSGL_FATAL, "[gl] Could not create GL context!\n");
964 return SET_WINDOW_FAILED;
968 // set context
969 if (!wglMakeCurrent(windc, new_context)) {
970 mp_msg (MSGT_VO, MSGL_FATAL, "[gl] Could not set GL context!\n");
971 if (!keep_context) {
972 wglDeleteContext(new_context);
974 return SET_WINDOW_FAILED;
977 // set new values
978 vo_window = win;
979 vo_hdc = windc;
981 RECT rect;
982 GetClientRect(win, &rect);
983 vo_dwidth = rect.right;
984 vo_dheight = rect.bottom;
986 if (!keep_context) {
987 if (*context)
988 wglDeleteContext(*context);
989 *context = new_context;
990 *vinfo = new_vinfo;
991 getFunctions(w32gpa, NULL);
993 // and inform that reinit is neccessary
994 return SET_WINDOW_REINIT;
996 return SET_WINDOW_OK;
999 void releaseGlContext(int *vinfo, HGLRC *context) {
1000 *vinfo = 0;
1001 if (*context) {
1002 wglMakeCurrent(0, 0);
1003 wglDeleteContext(*context);
1005 *context = 0;
1008 void swapGlBuffers() {
1009 SwapBuffers(vo_hdc);
1011 #else
1012 #ifdef HAVE_LIBDL
1013 #include <dlfcn.h>
1014 #endif
1015 #include "x11_common.h"
1017 * \brief find address of a linked function
1018 * \param s name of function to find
1019 * \return address of function or NULL if not found
1021 * Copied from xine
1023 static void *getdladdr(const char *s) {
1024 #ifdef HAVE_LIBDL
1025 #if defined(__sun) || defined(__sgi)
1026 static void *handle = NULL;
1027 if (!handle)
1028 handle = dlopen(NULL, RTLD_LAZY);
1029 return dlsym(handle, s);
1030 #else
1031 return dlsym(0, s);
1032 #endif
1033 #else
1034 return NULL;
1035 #endif
1039 * \brief Returns the XVisualInfo associated with Window win.
1040 * \param win Window whose XVisualInfo is returne.
1041 * \return XVisualInfo of the window. Caller must use XFree to free it.
1043 static XVisualInfo *getWindowVisualInfo(Window win) {
1044 XWindowAttributes xw_attr;
1045 XVisualInfo vinfo_template;
1046 int tmp;
1047 XGetWindowAttributes(mDisplay, win, &xw_attr);
1048 vinfo_template.visualid = XVisualIDFromVisual(xw_attr.visual);
1049 return XGetVisualInfo(mDisplay, VisualIDMask, &vinfo_template, &tmp);
1053 * \brief Changes the window in which video is displayed.
1054 * If possible only transfers the context to the new window, otherwise
1055 * creates a new one, which must be initialized by the caller.
1056 * \param vinfo Currently used visual.
1057 * \param context Currently used context.
1058 * \param win window that should be used for drawing.
1059 * \return one of SET_WINDOW_FAILED, SET_WINDOW_OK or SET_WINDOW_REINIT.
1060 * In case of SET_WINDOW_REINIT the context could not be transfered
1061 * and the caller must initialize it correctly.
1062 * \ingroup glcontext
1064 int setGlWindow(XVisualInfo **vinfo, GLXContext *context, Window win)
1066 XVisualInfo *new_vinfo;
1067 GLXContext new_context = NULL;
1068 int keep_context = 0;
1070 // should only be needed when keeping context, but not doing glFinish
1071 // can cause flickering even when we do not keep it.
1072 if (*context)
1073 glFinish();
1074 new_vinfo = getWindowVisualInfo(win);
1075 if (*context && *vinfo && new_vinfo &&
1076 (*vinfo)->visualid == new_vinfo->visualid) {
1077 // we can keep the GLXContext
1078 new_context = *context;
1079 XFree(new_vinfo);
1080 new_vinfo = *vinfo;
1081 keep_context = 1;
1082 } else {
1083 // create a context
1084 new_context = glXCreateContext(mDisplay, new_vinfo, NULL, True);
1085 if (!new_context) {
1086 mp_msg(MSGT_VO, MSGL_FATAL, "[gl] Could not create GLX context!\n");
1087 XFree(new_vinfo);
1088 return SET_WINDOW_FAILED;
1092 // set context
1093 if (!glXMakeCurrent(mDisplay, vo_window, new_context)) {
1094 mp_msg (MSGT_VO, MSGL_FATAL, "[gl] Could not set GLX context!\n");
1095 if (!keep_context) {
1096 glXDestroyContext (mDisplay, new_context);
1097 XFree(new_vinfo);
1099 return SET_WINDOW_FAILED;
1102 // set new values
1103 vo_window = win;
1105 Window root;
1106 int tmp;
1107 unsigned utmp;
1108 XGetGeometry(mDisplay, vo_window, &root, &tmp, &tmp,
1109 (unsigned *)&vo_dwidth, (unsigned *)&vo_dheight, &utmp, &utmp);
1111 if (!keep_context) {
1112 void *(*getProcAddress)(const GLubyte *);
1113 const char *(*glXExtStr)(Display *, int);
1114 if (*context)
1115 glXDestroyContext(mDisplay, *context);
1116 *context = new_context;
1117 if (*vinfo)
1118 XFree(*vinfo);
1119 *vinfo = new_vinfo;
1120 getProcAddress = getdladdr("glXGetProcAddress");
1121 if (!getProcAddress)
1122 getProcAddress = getdladdr("glXGetProcAddressARB");
1123 if (!getProcAddress)
1124 getProcAddress = (void *)getdladdr;
1125 glXExtStr = getdladdr("glXQueryExtensionsString");
1126 getFunctions(getProcAddress, !glXExtStr ? NULL :
1127 glXExtStr(mDisplay, DefaultScreen(mDisplay)));
1129 // and inform that reinit is neccessary
1130 return SET_WINDOW_REINIT;
1132 return SET_WINDOW_OK;
1136 * \brief free the VisualInfo and GLXContext of an OpenGL context.
1137 * \ingroup glcontext
1139 void releaseGlContext(XVisualInfo **vinfo, GLXContext *context) {
1140 if (*vinfo)
1141 XFree(*vinfo);
1142 *vinfo = NULL;
1143 if (*context)
1145 glFinish();
1146 glXMakeCurrent(mDisplay, None, NULL);
1147 glXDestroyContext(mDisplay, *context);
1149 *context = 0;
1152 void swapGlBuffers(void) {
1153 glXSwapBuffers(mDisplay, vo_window);
1155 #endif