vo_gl: Extract code to read a pnm file into a separate function
[mplayer/glamo.git] / libvo / gl_common.c
blob048f3185ce021a201093b0dcf989145fdcdc1cd3
1 /*
2 * common OpenGL routines
4 * copyleft (C) 2005-2010 Reimar Döffinger <Reimar.Doeffinger@gmx.de>
5 * Special thanks go to the xine team and Matthias Hopf, whose video_out_opengl.c
6 * gave me lots of good ideas.
8 * This file is part of MPlayer.
10 * MPlayer is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * MPlayer is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * You can alternatively redistribute this file and/or
25 * modify it under the terms of the GNU Lesser General Public
26 * License as published by the Free Software Foundation; either
27 * version 2.1 of the License, or (at your option) any later version.
30 /**
31 * \file gl_common.c
32 * \brief OpenGL helper functions used by vo_gl.c and vo_gl2.c
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <ctype.h>
39 #include <math.h>
40 #include "old_vo_defines.h"
41 #include "gl_common.h"
42 #include "csputils.h"
43 #include "aspect.h"
45 void (GLAPIENTRY *mpglBegin)(GLenum);
46 void (GLAPIENTRY *mpglEnd)(void);
47 void (GLAPIENTRY *mpglViewport)(GLint, GLint, GLsizei, GLsizei);
48 void (GLAPIENTRY *mpglMatrixMode)(GLenum);
49 void (GLAPIENTRY *mpglLoadIdentity)(void);
50 void (GLAPIENTRY *mpglTranslated)(double, double, double);
51 void (GLAPIENTRY *mpglScaled)(double, double, double);
52 void (GLAPIENTRY *mpglOrtho)(double, double, double, double, double, double);
53 void (GLAPIENTRY *mpglFrustum)(double, double, double, double, double, double);
54 void (GLAPIENTRY *mpglPushMatrix)(void);
55 void (GLAPIENTRY *mpglPopMatrix)(void);
56 void (GLAPIENTRY *mpglClear)(GLbitfield);
57 GLuint (GLAPIENTRY *mpglGenLists)(GLsizei);
58 void (GLAPIENTRY *mpglDeleteLists)(GLuint, GLsizei);
59 void (GLAPIENTRY *mpglNewList)(GLuint, GLenum);
60 void (GLAPIENTRY *mpglEndList)(void);
61 void (GLAPIENTRY *mpglCallList)(GLuint);
62 void (GLAPIENTRY *mpglCallLists)(GLsizei, GLenum, const GLvoid *);
63 void (GLAPIENTRY *mpglGenTextures)(GLsizei, GLuint *);
64 void (GLAPIENTRY *mpglDeleteTextures)(GLsizei, const GLuint *);
65 void (GLAPIENTRY *mpglTexEnvf)(GLenum, GLenum, GLfloat);
66 void (GLAPIENTRY *mpglTexEnvi)(GLenum, GLenum, GLint);
67 void (GLAPIENTRY *mpglColor4ub)(GLubyte, GLubyte, GLubyte, GLubyte);
68 void (GLAPIENTRY *mpglColor3f)(GLfloat, GLfloat, GLfloat);
69 void (GLAPIENTRY *mpglColor4f)(GLfloat, GLfloat, GLfloat, GLfloat);
70 void (GLAPIENTRY *mpglClearColor)(GLclampf, GLclampf, GLclampf, GLclampf);
71 void (GLAPIENTRY *mpglClearDepth)(GLclampd);
72 void (GLAPIENTRY *mpglDepthFunc)(GLenum);
73 void (GLAPIENTRY *mpglEnable)(GLenum);
74 void (GLAPIENTRY *mpglDisable)(GLenum);
75 const GLubyte *(GLAPIENTRY *mpglGetString)(GLenum);
76 void (GLAPIENTRY *mpglDrawBuffer)(GLenum);
77 void (GLAPIENTRY *mpglDepthMask)(GLboolean);
78 void (GLAPIENTRY *mpglBlendFunc)(GLenum, GLenum);
79 void (GLAPIENTRY *mpglFlush)(void);
80 void (GLAPIENTRY *mpglFinish)(void);
81 void (GLAPIENTRY *mpglPixelStorei)(GLenum, GLint);
82 void (GLAPIENTRY *mpglTexImage1D)(GLenum, GLint, GLint, GLsizei, GLint, GLenum, GLenum, const GLvoid *);
83 void (GLAPIENTRY *mpglTexImage2D)(GLenum, GLint, GLint, GLsizei, GLsizei, GLint, GLenum, GLenum, const GLvoid *);
84 void (GLAPIENTRY *mpglTexSubImage2D)(GLenum, GLint, GLint, GLint, GLsizei, GLsizei, GLenum, GLenum, const GLvoid *);
85 void (GLAPIENTRY *mpglTexParameteri)(GLenum, GLenum, GLint);
86 void (GLAPIENTRY *mpglTexParameterf)(GLenum, GLenum, GLfloat);
87 void (GLAPIENTRY *mpglTexParameterfv)(GLenum, GLenum, const GLfloat *);
88 void (GLAPIENTRY *mpglTexCoord2f)(GLfloat, GLfloat);
89 void (GLAPIENTRY *mpglVertex2f)(GLfloat, GLfloat);
90 void (GLAPIENTRY *mpglVertex3f)(GLfloat, GLfloat, GLfloat);
91 void (GLAPIENTRY *mpglNormal3f)(GLfloat, GLfloat, GLfloat);
92 void (GLAPIENTRY *mpglLightfv)(GLenum, GLenum, const GLfloat *);
93 void (GLAPIENTRY *mpglColorMaterial)(GLenum, GLenum);
94 void (GLAPIENTRY *mpglShadeModel)(GLenum);
95 void (GLAPIENTRY *mpglGetIntegerv)(GLenum, GLint *);
96 void (GLAPIENTRY *mpglColorMask)(GLboolean, GLboolean, GLboolean, GLboolean);
98 /**
99 * \defgroup glextfunctions OpenGL extension functions
101 * the pointers to these functions are acquired when the OpenGL
102 * context is created
103 * \{
105 void (GLAPIENTRY *mpglGenBuffers)(GLsizei, GLuint *);
106 void (GLAPIENTRY *mpglDeleteBuffers)(GLsizei, const GLuint *);
107 void (GLAPIENTRY *mpglBindBuffer)(GLenum, GLuint);
108 GLvoid* (GLAPIENTRY *mpglMapBuffer)(GLenum, GLenum);
109 GLboolean (GLAPIENTRY *mpglUnmapBuffer)(GLenum);
110 void (GLAPIENTRY *mpglBufferData)(GLenum, intptr_t, const GLvoid *, GLenum);
111 void (GLAPIENTRY *mpglCombinerParameterfv)(GLenum, const GLfloat *);
112 void (GLAPIENTRY *mpglCombinerParameteri)(GLenum, GLint);
113 void (GLAPIENTRY *mpglCombinerInput)(GLenum, GLenum, GLenum, GLenum, GLenum,
114 GLenum);
115 void (GLAPIENTRY *mpglCombinerOutput)(GLenum, GLenum, GLenum, GLenum, GLenum,
116 GLenum, GLenum, GLboolean, GLboolean,
117 GLboolean);
118 void (GLAPIENTRY *mpglBeginFragmentShader)(void);
119 void (GLAPIENTRY *mpglEndFragmentShader)(void);
120 void (GLAPIENTRY *mpglSampleMap)(GLuint, GLuint, GLenum);
121 void (GLAPIENTRY *mpglColorFragmentOp2)(GLenum, GLuint, GLuint, GLuint, GLuint,
122 GLuint, GLuint, GLuint, GLuint, GLuint);
123 void (GLAPIENTRY *mpglColorFragmentOp3)(GLenum, GLuint, GLuint, GLuint, GLuint,
124 GLuint, GLuint, GLuint, GLuint, GLuint,
125 GLuint, GLuint, GLuint);
126 void (GLAPIENTRY *mpglSetFragmentShaderConstant)(GLuint, const GLfloat *);
127 void (GLAPIENTRY *mpglActiveTexture)(GLenum);
128 void (GLAPIENTRY *mpglBindTexture)(GLenum, GLuint);
129 void (GLAPIENTRY *mpglMultiTexCoord2f)(GLenum, GLfloat, GLfloat);
130 void (GLAPIENTRY *mpglGenPrograms)(GLsizei, GLuint *);
131 void (GLAPIENTRY *mpglDeletePrograms)(GLsizei, const GLuint *);
132 void (GLAPIENTRY *mpglBindProgram)(GLenum, GLuint);
133 void (GLAPIENTRY *mpglProgramString)(GLenum, GLenum, GLsizei, const GLvoid *);
134 void (GLAPIENTRY *mpglGetProgramiv)(GLenum, GLenum, GLint *);
135 void (GLAPIENTRY *mpglProgramEnvParameter4f)(GLenum, GLuint, GLfloat, GLfloat,
136 GLfloat, GLfloat);
137 int (GLAPIENTRY *mpglSwapInterval)(int);
138 void (GLAPIENTRY *mpglTexImage3D)(GLenum, GLint, GLenum, GLsizei, GLsizei, GLsizei,
139 GLint, GLenum, GLenum, const GLvoid *);
140 void* (GLAPIENTRY *mpglAllocateMemoryMESA)(void *, int, size_t, float, float, float);
141 void (GLAPIENTRY *mpglFreeMemoryMESA)(void *, int, void *);
142 /** \} */ // end of glextfunctions group
144 //! \defgroup glgeneral OpenGL general helper functions
146 //! \defgroup glcontext OpenGL context management helper functions
148 //! \defgroup gltexture OpenGL texture handling helper functions
150 //! \defgroup glconversion OpenGL conversion helper functions
152 static GLint hqtexfmt;
155 * \brief adjusts the GL_UNPACK_ALIGNMENT to fit the stride.
156 * \param stride number of bytes per line for which alignment should fit.
157 * \ingroup glgeneral
159 void glAdjustAlignment(int stride) {
160 GLint gl_alignment;
161 if (stride % 8 == 0)
162 gl_alignment=8;
163 else if (stride % 4 == 0)
164 gl_alignment=4;
165 else if (stride % 2 == 0)
166 gl_alignment=2;
167 else
168 gl_alignment=1;
169 mpglPixelStorei(GL_UNPACK_ALIGNMENT, gl_alignment);
172 struct gl_name_map_struct {
173 GLint value;
174 const char *name;
177 #undef MAP
178 #define MAP(a) {a, #a}
179 //! mapping table for the glValName function
180 static const struct gl_name_map_struct gl_name_map[] = {
181 // internal format
182 MAP(GL_R3_G3_B2), MAP(GL_RGB4), MAP(GL_RGB5), MAP(GL_RGB8),
183 MAP(GL_RGB10), MAP(GL_RGB12), MAP(GL_RGB16), MAP(GL_RGBA2),
184 MAP(GL_RGBA4), MAP(GL_RGB5_A1), MAP(GL_RGBA8), MAP(GL_RGB10_A2),
185 MAP(GL_RGBA12), MAP(GL_RGBA16), MAP(GL_LUMINANCE8),
187 // format
188 MAP(GL_RGB), MAP(GL_RGBA), MAP(GL_RED), MAP(GL_GREEN), MAP(GL_BLUE),
189 MAP(GL_ALPHA), MAP(GL_LUMINANCE), MAP(GL_LUMINANCE_ALPHA),
190 MAP(GL_COLOR_INDEX),
191 // rest 1.2 only
192 MAP(GL_BGR), MAP(GL_BGRA),
194 //type
195 MAP(GL_BYTE), MAP(GL_UNSIGNED_BYTE), MAP(GL_SHORT), MAP(GL_UNSIGNED_SHORT),
196 MAP(GL_INT), MAP(GL_UNSIGNED_INT), MAP(GL_FLOAT), MAP(GL_DOUBLE),
197 MAP(GL_2_BYTES), MAP(GL_3_BYTES), MAP(GL_4_BYTES),
198 // rest 1.2 only
199 MAP(GL_UNSIGNED_BYTE_3_3_2), MAP(GL_UNSIGNED_BYTE_2_3_3_REV),
200 MAP(GL_UNSIGNED_SHORT_5_6_5), MAP(GL_UNSIGNED_SHORT_5_6_5_REV),
201 MAP(GL_UNSIGNED_SHORT_4_4_4_4), MAP(GL_UNSIGNED_SHORT_4_4_4_4_REV),
202 MAP(GL_UNSIGNED_SHORT_5_5_5_1), MAP(GL_UNSIGNED_SHORT_1_5_5_5_REV),
203 MAP(GL_UNSIGNED_INT_8_8_8_8), MAP(GL_UNSIGNED_INT_8_8_8_8_REV),
204 MAP(GL_UNSIGNED_INT_10_10_10_2), MAP(GL_UNSIGNED_INT_2_10_10_10_REV),
205 {0, 0}
207 #undef MAP
210 * \brief return the name of an OpenGL constant
211 * \param value the constant
212 * \return name of the constant or "Unknown format!"
213 * \ingroup glgeneral
215 const char *glValName(GLint value)
217 int i = 0;
219 while (gl_name_map[i].name) {
220 if (gl_name_map[i].value == value)
221 return gl_name_map[i].name;
222 i++;
224 return "Unknown format!";
227 //! always return this format as internal texture format in glFindFormat
228 #define TEXTUREFORMAT_ALWAYS GL_RGB8
229 #undef TEXTUREFORMAT_ALWAYS
232 * \brief find the OpenGL settings coresponding to format.
234 * All parameters may be NULL.
235 * \param fmt MPlayer format to analyze.
236 * \param bpp [OUT] bits per pixel of that format.
237 * \param gl_texfmt [OUT] internal texture format that fits the
238 * image format, not necessarily the best for performance.
239 * \param gl_format [OUT] OpenGL format for this image format.
240 * \param gl_type [OUT] OpenGL type for this image format.
241 * \return 1 if format is supported by OpenGL, 0 if not.
242 * \ingroup gltexture
244 int glFindFormat(uint32_t fmt, int *bpp, GLint *gl_texfmt,
245 GLenum *gl_format, GLenum *gl_type)
247 int supported = 1;
248 int dummy1;
249 GLenum dummy2;
250 GLint dummy3;
251 if (!bpp) bpp = &dummy1;
252 if (!gl_texfmt) gl_texfmt = &dummy3;
253 if (!gl_format) gl_format = &dummy2;
254 if (!gl_type) gl_type = &dummy2;
256 if (mp_get_chroma_shift(fmt, NULL, NULL)) {
257 // reduce the possible cases a bit
258 if (IMGFMT_IS_YUVP16_LE(fmt))
259 fmt = IMGFMT_420P16_LE;
260 else if (IMGFMT_IS_YUVP16_BE(fmt))
261 fmt = IMGFMT_420P16_BE;
262 else
263 fmt = IMGFMT_YV12;
266 *bpp = IMGFMT_IS_BGR(fmt)?IMGFMT_BGR_DEPTH(fmt):IMGFMT_RGB_DEPTH(fmt);
267 *gl_texfmt = 3;
268 switch (fmt) {
269 case IMGFMT_RGB48NE:
270 *gl_format = GL_RGB;
271 *gl_type = GL_UNSIGNED_SHORT;
272 break;
273 case IMGFMT_RGB24:
274 *gl_format = GL_RGB;
275 *gl_type = GL_UNSIGNED_BYTE;
276 break;
277 case IMGFMT_RGBA:
278 *gl_texfmt = 4;
279 *gl_format = GL_RGBA;
280 *gl_type = GL_UNSIGNED_BYTE;
281 break;
282 case IMGFMT_420P16:
283 supported = 0; // no native YUV support
284 *gl_texfmt = 1;
285 *bpp = 16;
286 *gl_format = GL_LUMINANCE;
287 *gl_type = GL_UNSIGNED_SHORT;
288 break;
289 case IMGFMT_YV12:
290 supported = 0; // no native YV12 support
291 case IMGFMT_Y800:
292 case IMGFMT_Y8:
293 *gl_texfmt = 1;
294 *bpp = 8;
295 *gl_format = GL_LUMINANCE;
296 *gl_type = GL_UNSIGNED_BYTE;
297 break;
298 case IMGFMT_UYVY:
299 case IMGFMT_YUY2:
300 *gl_texfmt = GL_YCBCR_MESA;
301 *bpp = 16;
302 *gl_format = GL_YCBCR_MESA;
303 *gl_type = fmt == IMGFMT_UYVY ? GL_UNSIGNED_SHORT_8_8 : GL_UNSIGNED_SHORT_8_8_REV;
304 break;
305 #if 0
306 // we do not support palettized formats, although the format the
307 // swscale produces works
308 case IMGFMT_RGB8:
309 gl_format = GL_RGB;
310 gl_type = GL_UNSIGNED_BYTE_2_3_3_REV;
311 break;
312 #endif
313 case IMGFMT_RGB15:
314 *gl_format = GL_RGBA;
315 *gl_type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
316 break;
317 case IMGFMT_RGB16:
318 *gl_format = GL_RGB;
319 *gl_type = GL_UNSIGNED_SHORT_5_6_5_REV;
320 break;
321 #if 0
322 case IMGFMT_BGR8:
323 // special case as red and blue have a differen number of bits.
324 // GL_BGR and GL_UNSIGNED_BYTE_3_3_2 isn't supported at least
325 // by nVidia drivers, and in addition would give more bits to
326 // blue than to red, which isn't wanted
327 gl_format = GL_RGB;
328 gl_type = GL_UNSIGNED_BYTE_3_3_2;
329 break;
330 #endif
331 case IMGFMT_BGR15:
332 *gl_format = GL_BGRA;
333 *gl_type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
334 break;
335 case IMGFMT_BGR16:
336 *gl_format = GL_RGB;
337 *gl_type = GL_UNSIGNED_SHORT_5_6_5;
338 break;
339 case IMGFMT_BGR24:
340 *gl_format = GL_BGR;
341 *gl_type = GL_UNSIGNED_BYTE;
342 break;
343 case IMGFMT_BGRA:
344 *gl_texfmt = 4;
345 *gl_format = GL_BGRA;
346 *gl_type = GL_UNSIGNED_BYTE;
347 break;
348 default:
349 *gl_texfmt = 4;
350 *gl_format = GL_RGBA;
351 *gl_type = GL_UNSIGNED_BYTE;
352 supported = 0;
354 #ifdef TEXTUREFORMAT_ALWAYS
355 *gl_texfmt = TEXTUREFORMAT_ALWAYS;
356 #endif
357 return supported;
360 #ifdef HAVE_LIBDL
361 #include <dlfcn.h>
362 #endif
364 * \brief find address of a linked function
365 * \param s name of function to find
366 * \return address of function or NULL if not found
368 static void *getdladdr(const char *s) {
369 void *ret = NULL;
370 #ifdef HAVE_LIBDL
371 void *handle = dlopen(NULL, RTLD_LAZY);
372 if (!handle)
373 return NULL;
374 ret = dlsym(handle, s);
375 dlclose(handle);
376 #endif
377 return ret;
380 typedef struct {
381 void *funcptr;
382 const char *extstr;
383 const char *funcnames[7];
384 void *fallback;
385 } extfunc_desc_t;
387 #define DEF_FUNC_DESC(name) {&mpgl##name, NULL, {"gl"#name, NULL}, gl ##name}
388 static const extfunc_desc_t extfuncs[] = {
389 // these aren't extension functions but we query them anyway to allow
390 // different "backends" with one binary
391 DEF_FUNC_DESC(Begin),
392 DEF_FUNC_DESC(End),
393 DEF_FUNC_DESC(Viewport),
394 DEF_FUNC_DESC(MatrixMode),
395 DEF_FUNC_DESC(LoadIdentity),
396 DEF_FUNC_DESC(Translated),
397 DEF_FUNC_DESC(Scaled),
398 DEF_FUNC_DESC(Ortho),
399 DEF_FUNC_DESC(Frustum),
400 DEF_FUNC_DESC(PushMatrix),
401 DEF_FUNC_DESC(PopMatrix),
402 DEF_FUNC_DESC(Clear),
403 DEF_FUNC_DESC(GenLists),
404 DEF_FUNC_DESC(DeleteLists),
405 DEF_FUNC_DESC(NewList),
406 DEF_FUNC_DESC(EndList),
407 DEF_FUNC_DESC(CallList),
408 DEF_FUNC_DESC(CallLists),
409 DEF_FUNC_DESC(GenTextures),
410 DEF_FUNC_DESC(DeleteTextures),
411 DEF_FUNC_DESC(TexEnvf),
412 DEF_FUNC_DESC(TexEnvi),
413 DEF_FUNC_DESC(Color4ub),
414 DEF_FUNC_DESC(Color3f),
415 DEF_FUNC_DESC(Color4f),
416 DEF_FUNC_DESC(ClearColor),
417 DEF_FUNC_DESC(ClearDepth),
418 DEF_FUNC_DESC(DepthFunc),
419 DEF_FUNC_DESC(Enable),
420 DEF_FUNC_DESC(Disable),
421 DEF_FUNC_DESC(DrawBuffer),
422 DEF_FUNC_DESC(DepthMask),
423 DEF_FUNC_DESC(BlendFunc),
424 DEF_FUNC_DESC(Flush),
425 DEF_FUNC_DESC(Finish),
426 DEF_FUNC_DESC(PixelStorei),
427 DEF_FUNC_DESC(TexImage1D),
428 DEF_FUNC_DESC(TexImage2D),
429 DEF_FUNC_DESC(TexSubImage2D),
430 DEF_FUNC_DESC(TexParameteri),
431 DEF_FUNC_DESC(TexParameterf),
432 DEF_FUNC_DESC(TexParameterfv),
433 DEF_FUNC_DESC(TexCoord2f),
434 DEF_FUNC_DESC(Vertex2f),
435 DEF_FUNC_DESC(Vertex3f),
436 DEF_FUNC_DESC(Normal3f),
437 DEF_FUNC_DESC(Lightfv),
438 DEF_FUNC_DESC(ColorMaterial),
439 DEF_FUNC_DESC(ShadeModel),
440 DEF_FUNC_DESC(GetIntegerv),
441 DEF_FUNC_DESC(ColorMask),
443 // here start the real extensions
444 {&mpglGenBuffers, NULL, {"glGenBuffers", "glGenBuffersARB", NULL}},
445 {&mpglDeleteBuffers, NULL, {"glDeleteBuffers", "glDeleteBuffersARB", NULL}},
446 {&mpglBindBuffer, NULL, {"glBindBuffer", "glBindBufferARB", NULL}},
447 {&mpglMapBuffer, NULL, {"glMapBuffer", "glMapBufferARB", NULL}},
448 {&mpglUnmapBuffer, NULL, {"glUnmapBuffer", "glUnmapBufferARB", NULL}},
449 {&mpglBufferData, NULL, {"glBufferData", "glBufferDataARB", NULL}},
450 {&mpglCombinerParameterfv, "NV_register_combiners", {"glCombinerParameterfv", "glCombinerParameterfvNV", NULL}},
451 {&mpglCombinerParameteri, "NV_register_combiners", {"glCombinerParameteri", "glCombinerParameteriNV", NULL}},
452 {&mpglCombinerInput, "NV_register_combiners", {"glCombinerInput", "glCombinerInputNV", NULL}},
453 {&mpglCombinerOutput, "NV_register_combiners", {"glCombinerOutput", "glCombinerOutputNV", NULL}},
454 {&mpglBeginFragmentShader, "ATI_fragment_shader", {"glBeginFragmentShaderATI", NULL}},
455 {&mpglEndFragmentShader, "ATI_fragment_shader", {"glEndFragmentShaderATI", NULL}},
456 {&mpglSampleMap, "ATI_fragment_shader", {"glSampleMapATI", NULL}},
457 {&mpglColorFragmentOp2, "ATI_fragment_shader", {"glColorFragmentOp2ATI", NULL}},
458 {&mpglColorFragmentOp3, "ATI_fragment_shader", {"glColorFragmentOp3ATI", NULL}},
459 {&mpglSetFragmentShaderConstant, "ATI_fragment_shader", {"glSetFragmentShaderConstantATI", NULL}},
460 {&mpglActiveTexture, NULL, {"glActiveTexture", "glActiveTextureARB", NULL}},
461 {&mpglBindTexture, NULL, {"glBindTexture", "glBindTextureARB", "glBindTextureEXT", NULL}},
462 {&mpglMultiTexCoord2f, NULL, {"glMultiTexCoord2f", "glMultiTexCoord2fARB", NULL}},
463 {&mpglGenPrograms, "_program", {"glGenProgramsARB", NULL}},
464 {&mpglDeletePrograms, "_program", {"glDeleteProgramsARB", NULL}},
465 {&mpglBindProgram, "_program", {"glBindProgramARB", NULL}},
466 {&mpglProgramString, "_program", {"glProgramStringARB", NULL}},
467 {&mpglGetProgramiv, "_program", {"glGetProgramivARB", NULL}},
468 {&mpglProgramEnvParameter4f, "_program", {"glProgramEnvParameter4fARB", NULL}},
469 {&mpglSwapInterval, "_swap_control", {"glXSwapIntervalSGI", "glXSwapInterval", "wglSwapIntervalSGI", "wglSwapInterval", "wglSwapIntervalEXT", NULL}},
470 {&mpglTexImage3D, NULL, {"glTexImage3D", NULL}},
471 {&mpglAllocateMemoryMESA, "GLX_MESA_allocate_memory", {"glXAllocateMemoryMESA", NULL}},
472 {&mpglFreeMemoryMESA, "GLX_MESA_allocate_memory", {"glXFreeMemoryMESA", NULL}},
473 {NULL}
477 * \brief find the function pointers of some useful OpenGL extensions
478 * \param getProcAddress function to resolve function names, may be NULL
479 * \param ext2 an extra extension string
481 static void getFunctions(void *(*getProcAddress)(const GLubyte *),
482 const char *ext2) {
483 const extfunc_desc_t *dsc;
484 const char *extensions;
485 char *allexts;
487 if (!getProcAddress)
488 getProcAddress = (void *)getdladdr;
490 // special case, we need glGetString before starting to find the other functions
491 mpglGetString = getProcAddress("glGetString");
492 if (!mpglGetString)
493 mpglGetString = glGetString;
495 extensions = (const char *)mpglGetString(GL_EXTENSIONS);
496 if (!extensions) extensions = "";
497 if (!ext2) ext2 = "";
498 allexts = malloc(strlen(extensions) + strlen(ext2) + 2);
499 strcpy(allexts, extensions);
500 strcat(allexts, " ");
501 strcat(allexts, ext2);
502 mp_msg(MSGT_VO, MSGL_DBG2, "OpenGL extensions string:\n%s\n", allexts);
503 for (dsc = extfuncs; dsc->funcptr; dsc++) {
504 void *ptr = NULL;
505 int i;
506 if (!dsc->extstr || strstr(allexts, dsc->extstr)) {
507 for (i = 0; !ptr && dsc->funcnames[i]; i++)
508 ptr = getProcAddress((const GLubyte *)dsc->funcnames[i]);
510 if (!ptr)
511 ptr = dsc->fallback;
512 *(void **)dsc->funcptr = ptr;
514 if (strstr(allexts, "_texture_float"))
515 hqtexfmt = GL_RGB32F;
516 else if (strstr(allexts, "NV_float_buffer"))
517 hqtexfmt = GL_FLOAT_RGB32_NV;
518 else
519 hqtexfmt = GL_RGB16;
520 free(allexts);
524 * \brief create a texture and set some defaults
525 * \param target texture taget, usually GL_TEXTURE_2D
526 * \param fmt internal texture format
527 * \param format texture host data format
528 * \param type texture host data type
529 * \param filter filter used for scaling, e.g. GL_LINEAR
530 * \param w texture width
531 * \param h texture height
532 * \param val luminance value to fill texture with
533 * \ingroup gltexture
535 void glCreateClearTex(GLenum target, GLenum fmt, GLenum format, GLenum type, GLint filter,
536 int w, int h, unsigned char val) {
537 GLfloat fval = (GLfloat)val / 255.0;
538 GLfloat border[4] = {fval, fval, fval, fval};
539 int stride;
540 char *init;
541 if (w == 0) w = 1;
542 if (h == 0) h = 1;
543 stride = w * glFmt2bpp(format, type);
544 if (!stride) return;
545 init = malloc(stride * h);
546 memset(init, val, stride * h);
547 glAdjustAlignment(stride);
548 mpglPixelStorei(GL_UNPACK_ROW_LENGTH, w);
549 mpglTexImage2D(target, 0, fmt, w, h, 0, format, type, init);
550 mpglTexParameterf(target, GL_TEXTURE_PRIORITY, 1.0);
551 mpglTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
552 mpglTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
553 mpglTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
554 mpglTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
555 // Border texels should not be used with CLAMP_TO_EDGE
556 // We set a sane default anyway.
557 mpglTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, border);
558 free(init);
562 * \brief skips whitespace and comments
563 * \param f file to read from
565 static void ppm_skip(FILE *f) {
566 int c, comment = 0;
567 do {
568 c = fgetc(f);
569 if (c == '#')
570 comment = 1;
571 if (c == '\n')
572 comment = 0;
573 } while (c != EOF && (isspace(c) || comment));
574 if (c != EOF)
575 ungetc(c, f);
578 #define MAXDIM (16 * 1024)
580 static uint8_t *read_pnm(FILE *f, int *width, int *height,
581 int *bytes_per_pixel, int *maxval) {
582 uint8_t *data;
583 int type;
584 unsigned w, h, m, val, bpp;
585 *width = *height = *bytes_per_pixel = *maxval = 0;
586 ppm_skip(f);
587 if (fgetc(f) != 'P')
588 return NULL;
589 type = fgetc(f);
590 if (type != '5' && type != '6')
591 return NULL;
592 ppm_skip(f);
593 if (fscanf(f, "%u", &w) != 1)
594 return NULL;
595 ppm_skip(f);
596 if (fscanf(f, "%u", &h) != 1)
597 return NULL;
598 ppm_skip(f);
599 if (fscanf(f, "%u", &m) != 1)
600 return NULL;
601 val = fgetc(f);
602 if (!isspace(val))
603 return NULL;
604 if (w > MAXDIM || h > MAXDIM)
605 return NULL;
606 bpp = (m > 255) ? 2 : 1;
607 if (type == '6')
608 bpp *= 3;
609 data = malloc(w * h * bpp);
610 if (fread(data, w * bpp, h, f) != h) {
611 free(data);
612 return NULL;
614 *width = w;
615 *height = h;
616 *bytes_per_pixel = bpp;
617 *maxval = m;
618 return data;
622 * \brief creates a texture from a PPM file
623 * \param target texture taget, usually GL_TEXTURE_2D
624 * \param fmt internal texture format, 0 for default
625 * \param filter filter used for scaling, e.g. GL_LINEAR
626 * \param f file to read PPM from
627 * \param width [out] width of texture
628 * \param height [out] height of texture
629 * \param maxval [out] maxval value from PPM file
630 * \return 0 on error, 1 otherwise
631 * \ingroup gltexture
633 int glCreatePPMTex(GLenum target, GLenum fmt, GLint filter,
634 FILE *f, int *width, int *height, int *maxval) {
635 int w, h, m, bpp;
636 GLenum type;
637 uint8_t *data = read_pnm(f, &w, &h, &bpp, &m);
638 if (!data || (bpp != 3 && bpp != 6)) {
639 free(data);
640 return 0;
642 if (!fmt) {
643 fmt = bpp == 6 ? hqtexfmt : 3;
644 if (fmt == GL_FLOAT_RGB32_NV && target != GL_TEXTURE_RECTANGLE)
645 fmt = GL_RGB16;
647 type = bpp == 6 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_BYTE;
648 glCreateClearTex(target, fmt, GL_RGB, type, filter, w, h, 0);
649 glUploadTex(target, GL_RGB, type,
650 data, w * bpp, 0, 0, w, h, 0);
651 free(data);
652 if (width) *width = w;
653 if (height) *height = h;
654 if (maxval) *maxval = m;
655 return 1;
659 * \brief return the number of bytes per pixel for the given format
660 * \param format OpenGL format
661 * \param type OpenGL type
662 * \return bytes per pixel
663 * \ingroup glgeneral
665 * Does not handle all possible variants, just those used by MPlayer
667 int glFmt2bpp(GLenum format, GLenum type) {
668 int component_size = 0;
669 switch (type) {
670 case GL_UNSIGNED_BYTE_3_3_2:
671 case GL_UNSIGNED_BYTE_2_3_3_REV:
672 return 1;
673 case GL_UNSIGNED_SHORT_5_5_5_1:
674 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
675 case GL_UNSIGNED_SHORT_5_6_5:
676 case GL_UNSIGNED_SHORT_5_6_5_REV:
677 return 2;
678 case GL_UNSIGNED_BYTE:
679 component_size = 1;
680 break;
681 case GL_UNSIGNED_SHORT:
682 component_size = 2;
683 break;
685 switch (format) {
686 case GL_LUMINANCE:
687 case GL_ALPHA:
688 return component_size;
689 case GL_YCBCR_MESA:
690 return 2;
691 case GL_RGB:
692 case GL_BGR:
693 return 3 * component_size;
694 case GL_RGBA:
695 case GL_BGRA:
696 return 4 * component_size;
698 return 0; // unknown
702 * \brief upload a texture, handling things like stride and slices
703 * \param target texture target, usually GL_TEXTURE_2D
704 * \param format OpenGL format of data
705 * \param type OpenGL type of data
706 * \param dataptr data to upload
707 * \param stride data stride
708 * \param x x offset in texture
709 * \param y y offset in texture
710 * \param w width of the texture part to upload
711 * \param h height of the texture part to upload
712 * \param slice height of an upload slice, 0 for all at once
713 * \ingroup gltexture
715 void glUploadTex(GLenum target, GLenum format, GLenum type,
716 const void *dataptr, int stride,
717 int x, int y, int w, int h, int slice) {
718 const uint8_t *data = dataptr;
719 int y_max = y + h;
720 if (w <= 0 || h <= 0) return;
721 if (slice <= 0)
722 slice = h;
723 if (stride < 0) {
724 data += (h - 1) * stride;
725 stride = -stride;
727 // this is not always correct, but should work for MPlayer
728 glAdjustAlignment(stride);
729 mpglPixelStorei(GL_UNPACK_ROW_LENGTH, stride / glFmt2bpp(format, type));
730 for (; y + slice <= y_max; y += slice) {
731 mpglTexSubImage2D(target, 0, x, y, w, slice, format, type, data);
732 data += stride * slice;
734 if (y < y_max)
735 mpglTexSubImage2D(target, 0, x, y, w, y_max - y, format, type, data);
738 static void fillUVcoeff(GLfloat *ucoef, GLfloat *vcoef,
739 float uvcos, float uvsin) {
740 int i;
741 ucoef[0] = 0 * uvcos + 1.403 * uvsin;
742 vcoef[0] = 0 * uvsin + 1.403 * uvcos;
743 ucoef[1] = -0.344 * uvcos + -0.714 * uvsin;
744 vcoef[1] = -0.344 * uvsin + -0.714 * uvcos;
745 ucoef[2] = 1.770 * uvcos + 0 * uvsin;
746 vcoef[2] = 1.770 * uvsin + 0 * uvcos;
747 ucoef[3] = 0;
748 vcoef[3] = 0;
749 // Coefficients (probably) must be in [0, 1] range, whereas they originally
750 // are in [-2, 2] range, so here comes the trick:
751 // First put them in the [-0.5, 0.5] range, then add 0.5.
752 // This can be undone with the HALF_BIAS and SCALE_BY_FOUR arguments
753 // for CombinerInput and CombinerOutput (or the respective ATI variants)
754 for (i = 0; i < 4; i++) {
755 ucoef[i] = ucoef[i] * 0.25 + 0.5;
756 vcoef[i] = vcoef[i] * 0.25 + 0.5;
761 * \brief Setup register combiners for YUV to RGB conversion.
762 * \param uvcos used for saturation and hue adjustment
763 * \param uvsin used for saturation and hue adjustment
765 static void glSetupYUVCombiners(float uvcos, float uvsin) {
766 GLfloat ucoef[4];
767 GLfloat vcoef[4];
768 GLint i;
769 if (!mpglCombinerInput || !mpglCombinerOutput ||
770 !mpglCombinerParameterfv || !mpglCombinerParameteri) {
771 mp_msg(MSGT_VO, MSGL_FATAL, "[gl] Combiner functions missing!\n");
772 return;
774 mpglGetIntegerv(GL_MAX_GENERAL_COMBINERS_NV, &i);
775 if (i < 2)
776 mp_msg(MSGT_VO, MSGL_ERR,
777 "[gl] 2 general combiners needed for YUV combiner support (found %i)\n", i);
778 mpglGetIntegerv(GL_MAX_TEXTURE_UNITS, &i);
779 if (i < 3)
780 mp_msg(MSGT_VO, MSGL_ERR,
781 "[gl] 3 texture units needed for YUV combiner support (found %i)\n", i);
782 fillUVcoeff(ucoef, vcoef, uvcos, uvsin);
783 mpglCombinerParameterfv(GL_CONSTANT_COLOR0_NV, ucoef);
784 mpglCombinerParameterfv(GL_CONSTANT_COLOR1_NV, vcoef);
786 // UV first, like this green component cannot overflow
787 mpglCombinerInput(GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_A_NV,
788 GL_TEXTURE1, GL_HALF_BIAS_NORMAL_NV, GL_RGB);
789 mpglCombinerInput(GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_B_NV,
790 GL_CONSTANT_COLOR0_NV, GL_HALF_BIAS_NORMAL_NV, GL_RGB);
791 mpglCombinerInput(GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_C_NV,
792 GL_TEXTURE2, GL_HALF_BIAS_NORMAL_NV, GL_RGB);
793 mpglCombinerInput(GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_D_NV,
794 GL_CONSTANT_COLOR1_NV, GL_HALF_BIAS_NORMAL_NV, GL_RGB);
795 mpglCombinerOutput(GL_COMBINER0_NV, GL_RGB, GL_DISCARD_NV, GL_DISCARD_NV,
796 GL_SPARE0_NV, GL_SCALE_BY_FOUR_NV, GL_NONE, GL_FALSE,
797 GL_FALSE, GL_FALSE);
799 // stage 2
800 mpglCombinerInput(GL_COMBINER1_NV, GL_RGB, GL_VARIABLE_A_NV, GL_SPARE0_NV,
801 GL_SIGNED_IDENTITY_NV, GL_RGB);
802 mpglCombinerInput(GL_COMBINER1_NV, GL_RGB, GL_VARIABLE_B_NV, GL_ZERO,
803 GL_UNSIGNED_INVERT_NV, GL_RGB);
804 mpglCombinerInput(GL_COMBINER1_NV, GL_RGB, GL_VARIABLE_C_NV,
805 GL_TEXTURE0, GL_SIGNED_IDENTITY_NV, GL_RGB);
806 mpglCombinerInput(GL_COMBINER1_NV, GL_RGB, GL_VARIABLE_D_NV, GL_ZERO,
807 GL_UNSIGNED_INVERT_NV, GL_RGB);
808 mpglCombinerOutput(GL_COMBINER1_NV, GL_RGB, GL_DISCARD_NV, GL_DISCARD_NV,
809 GL_SPARE0_NV, GL_NONE, GL_NONE, GL_FALSE,
810 GL_FALSE, GL_FALSE);
812 // leave final combiner stage in default mode
813 mpglCombinerParameteri(GL_NUM_GENERAL_COMBINERS_NV, 2);
817 * \brief Setup ATI version of register combiners for YUV to RGB conversion.
818 * \param csp_params parameters used for colorspace conversion
819 * \param text if set use the GL_ATI_text_fragment_shader API as
820 * used on OS X.
822 static void glSetupYUVFragmentATI(struct mp_csp_params *csp_params,
823 int text) {
824 GLint i;
825 float yuv2rgb[3][4];
827 mpglGetIntegerv (GL_MAX_TEXTURE_UNITS, &i);
828 if (i < 3)
829 mp_msg(MSGT_VO, MSGL_ERR,
830 "[gl] 3 texture units needed for YUV combiner (ATI) support (found %i)\n", i);
832 mp_get_yuv2rgb_coeffs(csp_params, yuv2rgb);
833 for (i = 0; i < 3; i++) {
834 int j;
835 yuv2rgb[i][3] -= -0.5 * (yuv2rgb[i][1] + yuv2rgb[i][2]);
836 for (j = 0; j < 4; j++) {
837 yuv2rgb[i][j] *= 0.125;
838 yuv2rgb[i][j] += 0.5;
839 if (yuv2rgb[i][j] > 1)
840 yuv2rgb[i][j] = 1;
841 if (yuv2rgb[i][j] < 0)
842 yuv2rgb[i][j] = 0;
845 if (text == 0) {
846 GLfloat c0[4] = {yuv2rgb[0][0], yuv2rgb[1][0], yuv2rgb[2][0]};
847 GLfloat c1[4] = {yuv2rgb[0][1], yuv2rgb[1][1], yuv2rgb[2][1]};
848 GLfloat c2[4] = {yuv2rgb[0][2], yuv2rgb[1][2], yuv2rgb[2][2]};
849 GLfloat c3[4] = {yuv2rgb[0][3], yuv2rgb[1][3], yuv2rgb[2][3]};
850 if (!mpglBeginFragmentShader || !mpglEndFragmentShader ||
851 !mpglSetFragmentShaderConstant || !mpglSampleMap ||
852 !mpglColorFragmentOp2 || !mpglColorFragmentOp3) {
853 mp_msg(MSGT_VO, MSGL_FATAL, "[gl] Combiner (ATI) functions missing!\n");
854 return;
856 mpglGetIntegerv(GL_NUM_FRAGMENT_REGISTERS_ATI, &i);
857 if (i < 3)
858 mp_msg(MSGT_VO, MSGL_ERR,
859 "[gl] 3 registers needed for YUV combiner (ATI) support (found %i)\n", i);
860 mpglBeginFragmentShader();
861 mpglSetFragmentShaderConstant(GL_CON_0_ATI, c0);
862 mpglSetFragmentShaderConstant(GL_CON_1_ATI, c1);
863 mpglSetFragmentShaderConstant(GL_CON_2_ATI, c2);
864 mpglSetFragmentShaderConstant(GL_CON_3_ATI, c3);
865 mpglSampleMap(GL_REG_0_ATI, GL_TEXTURE0, GL_SWIZZLE_STR_ATI);
866 mpglSampleMap(GL_REG_1_ATI, GL_TEXTURE1, GL_SWIZZLE_STR_ATI);
867 mpglSampleMap(GL_REG_2_ATI, GL_TEXTURE2, GL_SWIZZLE_STR_ATI);
868 mpglColorFragmentOp2(GL_MUL_ATI, GL_REG_1_ATI, GL_NONE, GL_NONE,
869 GL_REG_1_ATI, GL_NONE, GL_BIAS_BIT_ATI,
870 GL_CON_1_ATI, GL_NONE, GL_BIAS_BIT_ATI);
871 mpglColorFragmentOp3(GL_MAD_ATI, GL_REG_2_ATI, GL_NONE, GL_NONE,
872 GL_REG_2_ATI, GL_NONE, GL_BIAS_BIT_ATI,
873 GL_CON_2_ATI, GL_NONE, GL_BIAS_BIT_ATI,
874 GL_REG_1_ATI, GL_NONE, GL_NONE);
875 mpglColorFragmentOp3(GL_MAD_ATI, GL_REG_0_ATI, GL_NONE, GL_NONE,
876 GL_REG_0_ATI, GL_NONE, GL_NONE,
877 GL_CON_0_ATI, GL_NONE, GL_BIAS_BIT_ATI,
878 GL_REG_2_ATI, GL_NONE, GL_NONE);
879 mpglColorFragmentOp2(GL_ADD_ATI, GL_REG_0_ATI, GL_NONE, GL_8X_BIT_ATI,
880 GL_REG_0_ATI, GL_NONE, GL_NONE,
881 GL_CON_3_ATI, GL_NONE, GL_BIAS_BIT_ATI);
882 mpglEndFragmentShader();
883 } else {
884 static const char template[] =
885 "!!ATIfs1.0\n"
886 "StartConstants;\n"
887 " CONSTANT c0 = {%e, %e, %e};\n"
888 " CONSTANT c1 = {%e, %e, %e};\n"
889 " CONSTANT c2 = {%e, %e, %e};\n"
890 " CONSTANT c3 = {%e, %e, %e};\n"
891 "EndConstants;\n"
892 "StartOutputPass;\n"
893 " SampleMap r0, t0.str;\n"
894 " SampleMap r1, t1.str;\n"
895 " SampleMap r2, t2.str;\n"
896 " MUL r1.rgb, r1.bias, c1.bias;\n"
897 " MAD r2.rgb, r2.bias, c2.bias, r1;\n"
898 " MAD r0.rgb, r0, c0.bias, r2;\n"
899 " ADD r0.rgb.8x, r0, c3.bias;\n"
900 "EndPass;\n";
901 char buffer[512];
902 snprintf(buffer, sizeof(buffer), template,
903 yuv2rgb[0][0], yuv2rgb[1][0], yuv2rgb[2][0],
904 yuv2rgb[0][1], yuv2rgb[1][1], yuv2rgb[2][1],
905 yuv2rgb[0][2], yuv2rgb[1][2], yuv2rgb[2][2],
906 yuv2rgb[0][3], yuv2rgb[1][3], yuv2rgb[2][3]);
907 mp_msg(MSGT_VO, MSGL_DBG2, "[gl] generated fragment program:\n%s\n", buffer);
908 loadGPUProgram(GL_TEXT_FRAGMENT_SHADER_ATI, buffer);
913 * \brief helper function for gen_spline_lookup_tex
914 * \param x subpixel-position ((0,1) range) to calculate weights for
915 * \param dst where to store transformed weights, must provide space for 4 GLfloats
917 * calculates the weights and stores them after appropriate transformation
918 * for the scaler fragment program.
920 static void store_weights(float x, GLfloat *dst) {
921 float w0 = (((-1 * x + 3) * x - 3) * x + 1) / 6;
922 float w1 = ((( 3 * x - 6) * x + 0) * x + 4) / 6;
923 float w2 = (((-3 * x + 3) * x + 3) * x + 1) / 6;
924 float w3 = ((( 1 * x + 0) * x + 0) * x + 0) / 6;
925 *dst++ = 1 + x - w1 / (w0 + w1);
926 *dst++ = 1 - x + w3 / (w2 + w3);
927 *dst++ = w0 + w1;
928 *dst++ = 0;
931 //! to avoid artefacts this should be rather large
932 #define LOOKUP_BSPLINE_RES (2 * 1024)
934 * \brief creates the 1D lookup texture needed for fast higher-order filtering
935 * \param unit texture unit to attach texture to
937 static void gen_spline_lookup_tex(GLenum unit) {
938 GLfloat *tex = calloc(4 * LOOKUP_BSPLINE_RES, sizeof(*tex));
939 GLfloat *tp = tex;
940 int i;
941 for (i = 0; i < LOOKUP_BSPLINE_RES; i++) {
942 float x = (float)(i + 0.5) / LOOKUP_BSPLINE_RES;
943 store_weights(x, tp);
944 tp += 4;
946 store_weights(0, tex);
947 store_weights(1, &tex[4 * (LOOKUP_BSPLINE_RES - 1)]);
948 mpglActiveTexture(unit);
949 mpglTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA16, LOOKUP_BSPLINE_RES, 0, GL_RGBA, GL_FLOAT, tex);
950 mpglTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_PRIORITY, 1.0);
951 mpglTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
952 mpglTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
953 mpglTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT);
954 mpglActiveTexture(GL_TEXTURE0);
955 free(tex);
958 static const char *bilin_filt_template =
959 "TEX yuv.%c, fragment.texcoord[%c], texture[%c], %s;\n";
961 #define BICUB_FILT_MAIN(textype) \
962 /* first y-interpolation */ \
963 "ADD coord, fragment.texcoord[%c].xyxy, cdelta.xyxw;\n" \
964 "ADD coord2, fragment.texcoord[%c].xyxy, cdelta.zyzw;\n" \
965 "TEX a.r, coord.xyxy, texture[%c], "textype";\n" \
966 "TEX a.g, coord.zwzw, texture[%c], "textype";\n" \
967 /* second y-interpolation */ \
968 "TEX b.r, coord2.xyxy, texture[%c], "textype";\n" \
969 "TEX b.g, coord2.zwzw, texture[%c], "textype";\n" \
970 "LRP a.b, parmy.b, a.rrrr, a.gggg;\n" \
971 "LRP a.a, parmy.b, b.rrrr, b.gggg;\n" \
972 /* x-interpolation */ \
973 "LRP yuv.%c, parmx.b, a.bbbb, a.aaaa;\n"
975 static const char *bicub_filt_template_2D =
976 "MAD coord.xy, fragment.texcoord[%c], {%e, %e}, {0.5, 0.5};\n"
977 "TEX parmx, coord.x, texture[%c], 1D;\n"
978 "MUL cdelta.xz, parmx.rrgg, {-%e, 0, %e, 0};\n"
979 "TEX parmy, coord.y, texture[%c], 1D;\n"
980 "MUL cdelta.yw, parmy.rrgg, {0, -%e, 0, %e};\n"
981 BICUB_FILT_MAIN("2D");
983 static const char *bicub_filt_template_RECT =
984 "ADD coord, fragment.texcoord[%c], {0.5, 0.5};\n"
985 "TEX parmx, coord.x, texture[%c], 1D;\n"
986 "MUL cdelta.xz, parmx.rrgg, {-1, 0, 1, 0};\n"
987 "TEX parmy, coord.y, texture[%c], 1D;\n"
988 "MUL cdelta.yw, parmy.rrgg, {0, -1, 0, 1};\n"
989 BICUB_FILT_MAIN("RECT");
991 #define CALCWEIGHTS(t, s) \
992 "MAD "t", {-0.5, 0.1666, 0.3333, -0.3333}, "s", {1, 0, -0.5, 0.5};\n" \
993 "MAD "t", "t", "s", {0, 0, -0.5, 0.5};\n" \
994 "MAD "t", "t", "s", {-0.6666, 0, 0.8333, 0.1666};\n" \
995 "RCP a.x, "t".z;\n" \
996 "RCP a.y, "t".w;\n" \
997 "MAD "t".xy, "t".xyxy, a.xyxy, {1, 1, 0, 0};\n" \
998 "ADD "t".x, "t".xxxx, "s";\n" \
999 "SUB "t".y, "t".yyyy, "s";\n"
1001 static const char *bicub_notex_filt_template_2D =
1002 "MAD coord.xy, fragment.texcoord[%c], {%e, %e}, {0.5, 0.5};\n"
1003 "FRC coord.xy, coord.xyxy;\n"
1004 CALCWEIGHTS("parmx", "coord.xxxx")
1005 "MUL cdelta.xz, parmx.rrgg, {-%e, 0, %e, 0};\n"
1006 CALCWEIGHTS("parmy", "coord.yyyy")
1007 "MUL cdelta.yw, parmy.rrgg, {0, -%e, 0, %e};\n"
1008 BICUB_FILT_MAIN("2D");
1010 static const char *bicub_notex_filt_template_RECT =
1011 "ADD coord, fragment.texcoord[%c], {0.5, 0.5};\n"
1012 "FRC coord.xy, coord.xyxy;\n"
1013 CALCWEIGHTS("parmx", "coord.xxxx")
1014 "MUL cdelta.xz, parmx.rrgg, {-1, 0, 1, 0};\n"
1015 CALCWEIGHTS("parmy", "coord.yyyy")
1016 "MUL cdelta.yw, parmy.rrgg, {0, -1, 0, 1};\n"
1017 BICUB_FILT_MAIN("RECT");
1019 #define BICUB_X_FILT_MAIN(textype) \
1020 "ADD coord.xy, fragment.texcoord[%c].xyxy, cdelta.xyxy;\n" \
1021 "ADD coord2.xy, fragment.texcoord[%c].xyxy, cdelta.zyzy;\n" \
1022 "TEX a.r, coord, texture[%c], "textype";\n" \
1023 "TEX b.r, coord2, texture[%c], "textype";\n" \
1024 /* x-interpolation */ \
1025 "LRP yuv.%c, parmx.b, a.rrrr, b.rrrr;\n"
1027 static const char *bicub_x_filt_template_2D =
1028 "MAD coord.x, fragment.texcoord[%c], {%e}, {0.5};\n"
1029 "TEX parmx, coord, texture[%c], 1D;\n"
1030 "MUL cdelta.xyz, parmx.rrgg, {-%e, 0, %e};\n"
1031 BICUB_X_FILT_MAIN("2D");
1033 static const char *bicub_x_filt_template_RECT =
1034 "ADD coord.x, fragment.texcoord[%c], {0.5};\n"
1035 "TEX parmx, coord, texture[%c], 1D;\n"
1036 "MUL cdelta.xyz, parmx.rrgg, {-1, 0, 1};\n"
1037 BICUB_X_FILT_MAIN("RECT");
1039 static const char *unsharp_filt_template =
1040 "PARAM dcoord%c = {%e, %e, %e, %e};\n"
1041 "ADD coord, fragment.texcoord[%c].xyxy, dcoord%c;\n"
1042 "SUB coord2, fragment.texcoord[%c].xyxy, dcoord%c;\n"
1043 "TEX a.r, fragment.texcoord[%c], texture[%c], %s;\n"
1044 "TEX b.r, coord.xyxy, texture[%c], %s;\n"
1045 "TEX b.g, coord.zwzw, texture[%c], %s;\n"
1046 "ADD b.r, b.r, b.g;\n"
1047 "TEX b.b, coord2.xyxy, texture[%c], %s;\n"
1048 "TEX b.g, coord2.zwzw, texture[%c], %s;\n"
1049 "DP3 b, b, {0.25, 0.25, 0.25};\n"
1050 "SUB b.r, a.r, b.r;\n"
1051 "MAD yuv.%c, b.r, {%e}, a.r;\n";
1053 static const char *unsharp_filt_template2 =
1054 "PARAM dcoord%c = {%e, %e, %e, %e};\n"
1055 "PARAM dcoord2%c = {%e, 0, 0, %e};\n"
1056 "ADD coord, fragment.texcoord[%c].xyxy, dcoord%c;\n"
1057 "SUB coord2, fragment.texcoord[%c].xyxy, dcoord%c;\n"
1058 "TEX a.r, fragment.texcoord[%c], texture[%c], %s;\n"
1059 "TEX b.r, coord.xyxy, texture[%c], %s;\n"
1060 "TEX b.g, coord.zwzw, texture[%c], %s;\n"
1061 "ADD b.r, b.r, b.g;\n"
1062 "TEX b.b, coord2.xyxy, texture[%c], %s;\n"
1063 "TEX b.g, coord2.zwzw, texture[%c], %s;\n"
1064 "ADD b.r, b.r, b.b;\n"
1065 "ADD b.a, b.r, b.g;\n"
1066 "ADD coord, fragment.texcoord[%c].xyxy, dcoord2%c;\n"
1067 "SUB coord2, fragment.texcoord[%c].xyxy, dcoord2%c;\n"
1068 "TEX b.r, coord.xyxy, texture[%c], %s;\n"
1069 "TEX b.g, coord.zwzw, texture[%c], %s;\n"
1070 "ADD b.r, b.r, b.g;\n"
1071 "TEX b.b, coord2.xyxy, texture[%c], %s;\n"
1072 "TEX b.g, coord2.zwzw, texture[%c], %s;\n"
1073 "DP4 b.r, b, {-0.1171875, -0.1171875, -0.1171875, -0.09765625};\n"
1074 "MAD b.r, a.r, {0.859375}, b.r;\n"
1075 "MAD yuv.%c, b.r, {%e}, a.r;\n";
1077 static const char *yuv_prog_template =
1078 "PARAM ycoef = {%e, %e, %e};\n"
1079 "PARAM ucoef = {%e, %e, %e};\n"
1080 "PARAM vcoef = {%e, %e, %e};\n"
1081 "PARAM offsets = {%e, %e, %e};\n"
1082 "TEMP res;\n"
1083 "MAD res.rgb, yuv.rrrr, ycoef, offsets;\n"
1084 "MAD res.rgb, yuv.gggg, ucoef, res;\n"
1085 "MAD result.color.rgb, yuv.bbbb, vcoef, res;\n"
1086 "END";
1088 static const char *yuv_pow_prog_template =
1089 "PARAM ycoef = {%e, %e, %e};\n"
1090 "PARAM ucoef = {%e, %e, %e};\n"
1091 "PARAM vcoef = {%e, %e, %e};\n"
1092 "PARAM offsets = {%e, %e, %e};\n"
1093 "PARAM gamma = {%e, %e, %e};\n"
1094 "TEMP res;\n"
1095 "MAD res.rgb, yuv.rrrr, ycoef, offsets;\n"
1096 "MAD res.rgb, yuv.gggg, ucoef, res;\n"
1097 "MAD_SAT res.rgb, yuv.bbbb, vcoef, res;\n"
1098 "POW result.color.r, res.r, gamma.r;\n"
1099 "POW result.color.g, res.g, gamma.g;\n"
1100 "POW result.color.b, res.b, gamma.b;\n"
1101 "END";
1103 static const char *yuv_lookup_prog_template =
1104 "PARAM ycoef = {%e, %e, %e, 0};\n"
1105 "PARAM ucoef = {%e, %e, %e, 0};\n"
1106 "PARAM vcoef = {%e, %e, %e, 0};\n"
1107 "PARAM offsets = {%e, %e, %e, 0.125};\n"
1108 "TEMP res;\n"
1109 "MAD res, yuv.rrrr, ycoef, offsets;\n"
1110 "MAD res.rgb, yuv.gggg, ucoef, res;\n"
1111 "MAD res.rgb, yuv.bbbb, vcoef, res;\n"
1112 "TEX result.color.r, res.raaa, texture[%c], 2D;\n"
1113 "ADD res.a, res.a, 0.25;\n"
1114 "TEX result.color.g, res.gaaa, texture[%c], 2D;\n"
1115 "ADD res.a, res.a, 0.25;\n"
1116 "TEX result.color.b, res.baaa, texture[%c], 2D;\n"
1117 "END";
1119 static const char *yuv_lookup3d_prog_template =
1120 "TEX result.color, yuv, texture[%c], 3D;\n"
1121 "END";
1124 * \brief creates and initializes helper textures needed for scaling texture read
1125 * \param scaler scaler type to create texture for
1126 * \param texu contains next free texture unit number
1127 * \param texs texture unit ids for the scaler are stored in this array
1129 static void create_scaler_textures(int scaler, int *texu, char *texs) {
1130 switch (scaler) {
1131 case YUV_SCALER_BILIN:
1132 case YUV_SCALER_BICUB_NOTEX:
1133 case YUV_SCALER_UNSHARP:
1134 case YUV_SCALER_UNSHARP2:
1135 break;
1136 case YUV_SCALER_BICUB:
1137 case YUV_SCALER_BICUB_X:
1138 texs[0] = (*texu)++;
1139 gen_spline_lookup_tex(GL_TEXTURE0 + texs[0]);
1140 texs[0] += '0';
1141 break;
1142 default:
1143 mp_msg(MSGT_VO, MSGL_ERR, "[gl] unknown scaler type %i\n", scaler);
1147 //! resolution of texture for gamma lookup table
1148 #define LOOKUP_RES 512
1149 //! resolution for 3D yuv->rgb conversion lookup table
1150 #define LOOKUP_3DRES 32
1152 * \brief creates and initializes helper textures needed for yuv conversion
1153 * \param params struct containing parameters like brightness, gamma, ...
1154 * \param texu contains next free texture unit number
1155 * \param texs texture unit ids for the conversion are stored in this array
1157 static void create_conv_textures(gl_conversion_params_t *params, int *texu, char *texs) {
1158 unsigned char *lookup_data = NULL;
1159 int conv = YUV_CONVERSION(params->type);
1160 switch (conv) {
1161 case YUV_CONVERSION_FRAGMENT:
1162 case YUV_CONVERSION_FRAGMENT_POW:
1163 break;
1164 case YUV_CONVERSION_FRAGMENT_LOOKUP:
1165 texs[0] = (*texu)++;
1166 mpglActiveTexture(GL_TEXTURE0 + texs[0]);
1167 lookup_data = malloc(4 * LOOKUP_RES);
1168 mp_gen_gamma_map(lookup_data, LOOKUP_RES, params->csp_params.rgamma);
1169 mp_gen_gamma_map(&lookup_data[LOOKUP_RES], LOOKUP_RES, params->csp_params.ggamma);
1170 mp_gen_gamma_map(&lookup_data[2 * LOOKUP_RES], LOOKUP_RES, params->csp_params.bgamma);
1171 glCreateClearTex(GL_TEXTURE_2D, GL_LUMINANCE8, GL_LUMINANCE, GL_UNSIGNED_BYTE, GL_LINEAR,
1172 LOOKUP_RES, 4, 0);
1173 glUploadTex(GL_TEXTURE_2D, GL_LUMINANCE, GL_UNSIGNED_BYTE, lookup_data,
1174 LOOKUP_RES, 0, 0, LOOKUP_RES, 4, 0);
1175 mpglActiveTexture(GL_TEXTURE0);
1176 texs[0] += '0';
1177 break;
1178 case YUV_CONVERSION_FRAGMENT_LOOKUP3D:
1180 int sz = LOOKUP_3DRES + 2; // texture size including borders
1181 if (!mpglTexImage3D) {
1182 mp_msg(MSGT_VO, MSGL_ERR, "[gl] Missing 3D texture function!\n");
1183 break;
1185 texs[0] = (*texu)++;
1186 mpglActiveTexture(GL_TEXTURE0 + texs[0]);
1187 lookup_data = malloc(3 * sz * sz * sz);
1188 mp_gen_yuv2rgb_map(&params->csp_params, lookup_data, LOOKUP_3DRES);
1189 glAdjustAlignment(sz);
1190 mpglPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
1191 mpglTexImage3D(GL_TEXTURE_3D, 0, 3, sz, sz, sz, 1,
1192 GL_RGB, GL_UNSIGNED_BYTE, lookup_data);
1193 mpglTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_PRIORITY, 1.0);
1194 mpglTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1195 mpglTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1196 mpglTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP);
1197 mpglTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP);
1198 mpglTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP);
1199 mpglActiveTexture(GL_TEXTURE0);
1200 texs[0] += '0';
1202 break;
1203 default:
1204 mp_msg(MSGT_VO, MSGL_ERR, "[gl] unknown conversion type %i\n", conv);
1206 if (lookup_data)
1207 free(lookup_data);
1211 * \brief adds a scaling texture read at the current fragment program position
1212 * \param scaler type of scaler to insert
1213 * \param prog_pos current position in fragment program
1214 * \param remain how many bytes remain in the buffer given by prog_pos
1215 * \param texs array containing the texture unit identifiers for this scaler
1216 * \param in_tex texture unit the scaler should read from
1217 * \param out_comp component of the yuv variable the scaler stores the result in
1218 * \param rect if rectangular (pixel) adressing should be used for in_tex
1219 * \param texw width of the in_tex texture
1220 * \param texh height of the in_tex texture
1221 * \param strength strength of filter effect if the scaler does some kind of filtering
1223 static void add_scaler(int scaler, char **prog_pos, int *remain, char *texs,
1224 char in_tex, char out_comp, int rect, int texw, int texh,
1225 double strength) {
1226 const char *ttype = rect ? "RECT" : "2D";
1227 const float ptw = rect ? 1.0 : 1.0 / texw;
1228 const float pth = rect ? 1.0 : 1.0 / texh;
1229 switch (scaler) {
1230 case YUV_SCALER_BILIN:
1231 snprintf(*prog_pos, *remain, bilin_filt_template, out_comp, in_tex,
1232 in_tex, ttype);
1233 break;
1234 case YUV_SCALER_BICUB:
1235 if (rect)
1236 snprintf(*prog_pos, *remain, bicub_filt_template_RECT,
1237 in_tex, texs[0], texs[0],
1238 in_tex, in_tex, in_tex, in_tex, in_tex, in_tex, out_comp);
1239 else
1240 snprintf(*prog_pos, *remain, bicub_filt_template_2D,
1241 in_tex, (float)texw, (float)texh,
1242 texs[0], ptw, ptw, texs[0], pth, pth,
1243 in_tex, in_tex, in_tex, in_tex, in_tex, in_tex, out_comp);
1244 break;
1245 case YUV_SCALER_BICUB_X:
1246 if (rect)
1247 snprintf(*prog_pos, *remain, bicub_x_filt_template_RECT,
1248 in_tex, texs[0],
1249 in_tex, in_tex, in_tex, in_tex, out_comp);
1250 else
1251 snprintf(*prog_pos, *remain, bicub_x_filt_template_2D,
1252 in_tex, (float)texw,
1253 texs[0], ptw, ptw,
1254 in_tex, in_tex, in_tex, in_tex, out_comp);
1255 break;
1256 case YUV_SCALER_BICUB_NOTEX:
1257 if (rect)
1258 snprintf(*prog_pos, *remain, bicub_notex_filt_template_RECT,
1259 in_tex,
1260 in_tex, in_tex, in_tex, in_tex, in_tex, in_tex, out_comp);
1261 else
1262 snprintf(*prog_pos, *remain, bicub_notex_filt_template_2D,
1263 in_tex, (float)texw, (float)texh, ptw, ptw, pth, pth,
1264 in_tex, in_tex, in_tex, in_tex, in_tex, in_tex, out_comp);
1265 break;
1266 case YUV_SCALER_UNSHARP:
1267 snprintf(*prog_pos, *remain, unsharp_filt_template,
1268 out_comp, 0.5 * ptw, 0.5 * pth, 0.5 * ptw, -0.5 * pth,
1269 in_tex, out_comp, in_tex, out_comp, in_tex,
1270 in_tex, ttype, in_tex, ttype, in_tex, ttype, in_tex, ttype,
1271 in_tex, ttype, out_comp, strength);
1272 break;
1273 case YUV_SCALER_UNSHARP2:
1274 snprintf(*prog_pos, *remain, unsharp_filt_template2,
1275 out_comp, 1.2 * ptw, 1.2 * pth, 1.2 * ptw, -1.2 * pth,
1276 out_comp, 1.5 * ptw, 1.5 * pth,
1277 in_tex, out_comp, in_tex, out_comp, in_tex,
1278 in_tex, ttype, in_tex, ttype, in_tex, ttype, in_tex, ttype,
1279 in_tex, ttype, in_tex, out_comp, in_tex, out_comp,
1280 in_tex, ttype, in_tex, ttype, in_tex, ttype,
1281 in_tex, ttype, out_comp, strength);
1282 break;
1284 *remain -= strlen(*prog_pos);
1285 *prog_pos += strlen(*prog_pos);
1288 static const struct {
1289 const char *name;
1290 GLenum cur;
1291 GLenum max;
1292 } progstats[] = {
1293 {"instructions", 0x88A0, 0x88A1},
1294 {"native instructions", 0x88A2, 0x88A3},
1295 {"temporaries", 0x88A4, 0x88A5},
1296 {"native temporaries", 0x88A6, 0x88A7},
1297 {"parameters", 0x88A8, 0x88A9},
1298 {"native parameters", 0x88AA, 0x88AB},
1299 {"attribs", 0x88AC, 0x88AD},
1300 {"native attribs", 0x88AE, 0x88AF},
1301 {"ALU instructions", 0x8805, 0x880B},
1302 {"TEX instructions", 0x8806, 0x880C},
1303 {"TEX indirections", 0x8807, 0x880D},
1304 {"native ALU instructions", 0x8808, 0x880E},
1305 {"native TEX instructions", 0x8809, 0x880F},
1306 {"native TEX indirections", 0x880A, 0x8810},
1307 {NULL, 0, 0}
1311 * \brief load the specified GPU Program
1312 * \param target program target to load into, only GL_FRAGMENT_PROGRAM is tested
1313 * \param prog program string
1314 * \return 1 on success, 0 otherwise
1316 int loadGPUProgram(GLenum target, char *prog) {
1317 int i;
1318 GLint cur = 0, max = 0, err = 0;
1319 if (!mpglProgramString) {
1320 mp_msg(MSGT_VO, MSGL_ERR, "[gl] Missing GPU program function\n");
1321 return 0;
1323 mpglProgramString(target, GL_PROGRAM_FORMAT_ASCII, strlen(prog), prog);
1324 mpglGetIntegerv(GL_PROGRAM_ERROR_POSITION, &err);
1325 if (err != -1) {
1326 mp_msg(MSGT_VO, MSGL_ERR,
1327 "[gl] Error compiling fragment program, make sure your card supports\n"
1328 "[gl] GL_ARB_fragment_program (use glxinfo to check).\n"
1329 "[gl] Error message:\n %s at %.10s\n",
1330 mpglGetString(GL_PROGRAM_ERROR_STRING), &prog[err]);
1331 return 0;
1333 if (!mpglGetProgramiv || !mp_msg_test(MSGT_VO, MSGL_DBG2))
1334 return 1;
1335 mp_msg(MSGT_VO, MSGL_V, "[gl] Program statistics:\n");
1336 for (i = 0; progstats[i].name; i++) {
1337 mpglGetProgramiv(target, progstats[i].cur, &cur);
1338 mpglGetProgramiv(target, progstats[i].max, &max);
1339 mp_msg(MSGT_VO, MSGL_V, "[gl] %s: %i/%i\n", progstats[i].name, cur, max);
1341 return 1;
1344 #define MAX_PROGSZ (1024*1024)
1347 * \brief setup a fragment program that will do YUV->RGB conversion
1348 * \param parms struct containing parameters like conversion and scaler type,
1349 * brightness, ...
1351 static void glSetupYUVFragprog(gl_conversion_params_t *params) {
1352 int type = params->type;
1353 int texw = params->texw;
1354 int texh = params->texh;
1355 int rect = params->target == GL_TEXTURE_RECTANGLE;
1356 static const char prog_hdr[] =
1357 "!!ARBfp1.0\n"
1358 "OPTION ARB_precision_hint_fastest;\n"
1359 // all scaler variables must go here so they aren't defined
1360 // multiple times when the same scaler is used more than once
1361 "TEMP coord, coord2, cdelta, parmx, parmy, a, b, yuv;\n";
1362 int prog_remain;
1363 char *yuv_prog, *prog_pos;
1364 int cur_texu = 3;
1365 char lum_scale_texs[1];
1366 char chrom_scale_texs[1];
1367 char conv_texs[1];
1368 GLint i;
1369 // this is the conversion matrix, with y, u, v factors
1370 // for red, green, blue and the constant offsets
1371 float yuv2rgb[3][4];
1372 create_conv_textures(params, &cur_texu, conv_texs);
1373 create_scaler_textures(YUV_LUM_SCALER(type), &cur_texu, lum_scale_texs);
1374 if (YUV_CHROM_SCALER(type) == YUV_LUM_SCALER(type))
1375 memcpy(chrom_scale_texs, lum_scale_texs, sizeof(chrom_scale_texs));
1376 else
1377 create_scaler_textures(YUV_CHROM_SCALER(type), &cur_texu, chrom_scale_texs);
1378 mpglGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &i);
1379 if (i < cur_texu)
1380 mp_msg(MSGT_VO, MSGL_ERR,
1381 "[gl] %i texture units needed for this type of YUV fragment support (found %i)\n",
1382 cur_texu, i);
1383 if (!mpglProgramString) {
1384 mp_msg(MSGT_VO, MSGL_FATAL, "[gl] ProgramString function missing!\n");
1385 return;
1387 yuv_prog = malloc(MAX_PROGSZ);
1388 strcpy(yuv_prog, prog_hdr);
1389 prog_pos = yuv_prog + sizeof(prog_hdr) - 1;
1390 prog_remain = MAX_PROGSZ - sizeof(prog_hdr);
1391 add_scaler(YUV_LUM_SCALER(type), &prog_pos, &prog_remain, lum_scale_texs,
1392 '0', 'r', rect, texw, texh, params->filter_strength);
1393 add_scaler(YUV_CHROM_SCALER(type), &prog_pos, &prog_remain, chrom_scale_texs,
1394 '1', 'g', rect, params->chrom_texw, params->chrom_texh, params->filter_strength);
1395 add_scaler(YUV_CHROM_SCALER(type), &prog_pos, &prog_remain, chrom_scale_texs,
1396 '2', 'b', rect, params->chrom_texw, params->chrom_texh, params->filter_strength);
1397 mp_get_yuv2rgb_coeffs(&params->csp_params, yuv2rgb);
1398 switch (YUV_CONVERSION(type)) {
1399 case YUV_CONVERSION_FRAGMENT:
1400 snprintf(prog_pos, prog_remain, yuv_prog_template,
1401 yuv2rgb[ROW_R][COL_Y], yuv2rgb[ROW_G][COL_Y], yuv2rgb[ROW_B][COL_Y],
1402 yuv2rgb[ROW_R][COL_U], yuv2rgb[ROW_G][COL_U], yuv2rgb[ROW_B][COL_U],
1403 yuv2rgb[ROW_R][COL_V], yuv2rgb[ROW_G][COL_V], yuv2rgb[ROW_B][COL_V],
1404 yuv2rgb[ROW_R][COL_C], yuv2rgb[ROW_G][COL_C], yuv2rgb[ROW_B][COL_C]);
1405 break;
1406 case YUV_CONVERSION_FRAGMENT_POW:
1407 snprintf(prog_pos, prog_remain, yuv_pow_prog_template,
1408 yuv2rgb[ROW_R][COL_Y], yuv2rgb[ROW_G][COL_Y], yuv2rgb[ROW_B][COL_Y],
1409 yuv2rgb[ROW_R][COL_U], yuv2rgb[ROW_G][COL_U], yuv2rgb[ROW_B][COL_U],
1410 yuv2rgb[ROW_R][COL_V], yuv2rgb[ROW_G][COL_V], yuv2rgb[ROW_B][COL_V],
1411 yuv2rgb[ROW_R][COL_C], yuv2rgb[ROW_G][COL_C], yuv2rgb[ROW_B][COL_C],
1412 (float)1.0 / params->csp_params.rgamma, (float)1.0 / params->csp_params.bgamma, (float)1.0 / params->csp_params.bgamma);
1413 break;
1414 case YUV_CONVERSION_FRAGMENT_LOOKUP:
1415 snprintf(prog_pos, prog_remain, yuv_lookup_prog_template,
1416 yuv2rgb[ROW_R][COL_Y], yuv2rgb[ROW_G][COL_Y], yuv2rgb[ROW_B][COL_Y],
1417 yuv2rgb[ROW_R][COL_U], yuv2rgb[ROW_G][COL_U], yuv2rgb[ROW_B][COL_U],
1418 yuv2rgb[ROW_R][COL_V], yuv2rgb[ROW_G][COL_V], yuv2rgb[ROW_B][COL_V],
1419 yuv2rgb[ROW_R][COL_C], yuv2rgb[ROW_G][COL_C], yuv2rgb[ROW_B][COL_C],
1420 conv_texs[0], conv_texs[0], conv_texs[0]);
1421 break;
1422 case YUV_CONVERSION_FRAGMENT_LOOKUP3D:
1423 snprintf(prog_pos, prog_remain, yuv_lookup3d_prog_template, conv_texs[0]);
1424 break;
1425 default:
1426 mp_msg(MSGT_VO, MSGL_ERR, "[gl] unknown conversion type %i\n", YUV_CONVERSION(type));
1427 break;
1429 mp_msg(MSGT_VO, MSGL_DBG2, "[gl] generated fragment program:\n%s\n", yuv_prog);
1430 loadGPUProgram(GL_FRAGMENT_PROGRAM, yuv_prog);
1431 free(yuv_prog);
1435 * \brief detect the best YUV->RGB conversion method available
1437 int glAutodetectYUVConversion(void) {
1438 const char *extensions = mpglGetString(GL_EXTENSIONS);
1439 if (!extensions || !mpglMultiTexCoord2f)
1440 return YUV_CONVERSION_NONE;
1441 if (strstr(extensions, "GL_ARB_fragment_program"))
1442 return YUV_CONVERSION_FRAGMENT;
1443 if (strstr(extensions, "GL_ATI_text_fragment_shader"))
1444 return YUV_CONVERSION_TEXT_FRAGMENT;
1445 if (strstr(extensions, "GL_ATI_fragment_shader"))
1446 return YUV_CONVERSION_COMBINERS_ATI;
1447 return YUV_CONVERSION_NONE;
1451 * \brief setup YUV->RGB conversion
1452 * \param parms struct containing parameters like conversion and scaler type,
1453 * brightness, ...
1454 * \ingroup glconversion
1456 void glSetupYUVConversion(gl_conversion_params_t *params) {
1457 float uvcos = params->csp_params.saturation * cos(params->csp_params.hue);
1458 float uvsin = params->csp_params.saturation * sin(params->csp_params.hue);
1459 if (params->chrom_texw == 0) params->chrom_texw = 1;
1460 if (params->chrom_texh == 0) params->chrom_texh = 1;
1461 switch (YUV_CONVERSION(params->type)) {
1462 case YUV_CONVERSION_COMBINERS:
1463 glSetupYUVCombiners(uvcos, uvsin);
1464 break;
1465 case YUV_CONVERSION_COMBINERS_ATI:
1466 glSetupYUVFragmentATI(&params->csp_params, 0);
1467 break;
1468 case YUV_CONVERSION_TEXT_FRAGMENT:
1469 glSetupYUVFragmentATI(&params->csp_params, 1);
1470 break;
1471 case YUV_CONVERSION_FRAGMENT_LOOKUP:
1472 case YUV_CONVERSION_FRAGMENT_LOOKUP3D:
1473 case YUV_CONVERSION_FRAGMENT:
1474 case YUV_CONVERSION_FRAGMENT_POW:
1475 glSetupYUVFragprog(params);
1476 break;
1477 case YUV_CONVERSION_NONE:
1478 break;
1479 default:
1480 mp_msg(MSGT_VO, MSGL_ERR, "[gl] unknown conversion type %i\n", YUV_CONVERSION(params->type));
1485 * \brief enable the specified YUV conversion
1486 * \param target texture target for Y, U and V textures (e.g. GL_TEXTURE_2D)
1487 * \param type type of YUV conversion
1488 * \ingroup glconversion
1490 void glEnableYUVConversion(GLenum target, int type) {
1491 switch (YUV_CONVERSION(type)) {
1492 case YUV_CONVERSION_COMBINERS:
1493 mpglActiveTexture(GL_TEXTURE1);
1494 mpglEnable(target);
1495 mpglActiveTexture(GL_TEXTURE2);
1496 mpglEnable(target);
1497 mpglActiveTexture(GL_TEXTURE0);
1498 mpglEnable(GL_REGISTER_COMBINERS_NV);
1499 break;
1500 case YUV_CONVERSION_COMBINERS_ATI:
1501 mpglActiveTexture(GL_TEXTURE1);
1502 mpglEnable(target);
1503 mpglActiveTexture(GL_TEXTURE2);
1504 mpglEnable(target);
1505 mpglActiveTexture(GL_TEXTURE0);
1506 mpglEnable(GL_FRAGMENT_SHADER_ATI);
1507 break;
1508 case YUV_CONVERSION_TEXT_FRAGMENT:
1509 mpglActiveTexture(GL_TEXTURE1);
1510 mpglEnable(target);
1511 mpglActiveTexture(GL_TEXTURE2);
1512 mpglEnable(target);
1513 mpglActiveTexture(GL_TEXTURE0);
1514 mpglEnable(GL_TEXT_FRAGMENT_SHADER_ATI);
1515 break;
1516 case YUV_CONVERSION_FRAGMENT_LOOKUP3D:
1517 case YUV_CONVERSION_FRAGMENT_LOOKUP:
1518 case YUV_CONVERSION_FRAGMENT_POW:
1519 case YUV_CONVERSION_FRAGMENT:
1520 case YUV_CONVERSION_NONE:
1521 mpglEnable(GL_FRAGMENT_PROGRAM);
1522 break;
1527 * \brief disable the specified YUV conversion
1528 * \param target texture target for Y, U and V textures (e.g. GL_TEXTURE_2D)
1529 * \param type type of YUV conversion
1530 * \ingroup glconversion
1532 void glDisableYUVConversion(GLenum target, int type) {
1533 switch (YUV_CONVERSION(type)) {
1534 case YUV_CONVERSION_COMBINERS:
1535 mpglActiveTexture(GL_TEXTURE1);
1536 mpglDisable(target);
1537 mpglActiveTexture(GL_TEXTURE2);
1538 mpglDisable(target);
1539 mpglActiveTexture(GL_TEXTURE0);
1540 mpglDisable(GL_REGISTER_COMBINERS_NV);
1541 break;
1542 case YUV_CONVERSION_COMBINERS_ATI:
1543 mpglActiveTexture(GL_TEXTURE1);
1544 mpglDisable(target);
1545 mpglActiveTexture(GL_TEXTURE2);
1546 mpglDisable(target);
1547 mpglActiveTexture(GL_TEXTURE0);
1548 mpglDisable(GL_FRAGMENT_SHADER_ATI);
1549 break;
1550 case YUV_CONVERSION_TEXT_FRAGMENT:
1551 mpglDisable(GL_TEXT_FRAGMENT_SHADER_ATI);
1552 // HACK: at least the Mac OS X 10.5 PPC Radeon drivers are broken and
1553 // without this disable the texture units while the program is still
1554 // running (10.4 PPC seems to work without this though).
1555 mpglFlush();
1556 mpglActiveTexture(GL_TEXTURE1);
1557 mpglDisable(target);
1558 mpglActiveTexture(GL_TEXTURE2);
1559 mpglDisable(target);
1560 mpglActiveTexture(GL_TEXTURE0);
1561 break;
1562 case YUV_CONVERSION_FRAGMENT_LOOKUP3D:
1563 case YUV_CONVERSION_FRAGMENT_LOOKUP:
1564 case YUV_CONVERSION_FRAGMENT_POW:
1565 case YUV_CONVERSION_FRAGMENT:
1566 case YUV_CONVERSION_NONE:
1567 mpglDisable(GL_FRAGMENT_PROGRAM);
1568 break;
1572 void glEnable3DLeft(int type) {
1573 GLint buffer;
1574 switch (type) {
1575 case GL_3D_RED_CYAN:
1576 mpglColorMask(GL_TRUE, GL_FALSE, GL_FALSE, GL_FALSE);
1577 break;
1578 case GL_3D_GREEN_MAGENTA:
1579 mpglColorMask(GL_FALSE, GL_TRUE, GL_FALSE, GL_FALSE);
1580 break;
1581 case GL_3D_QUADBUFFER:
1582 mpglGetIntegerv(GL_DRAW_BUFFER, &buffer);
1583 switch (buffer) {
1584 case GL_FRONT:
1585 case GL_FRONT_LEFT:
1586 case GL_FRONT_RIGHT:
1587 buffer = GL_FRONT_LEFT;
1588 break;
1589 case GL_BACK:
1590 case GL_BACK_LEFT:
1591 case GL_BACK_RIGHT:
1592 buffer = GL_BACK_LEFT;
1593 break;
1595 mpglDrawBuffer(buffer);
1596 break;
1600 void glEnable3DRight(int type) {
1601 GLint buffer;
1602 switch (type) {
1603 case GL_3D_RED_CYAN:
1604 mpglColorMask(GL_FALSE, GL_TRUE, GL_TRUE, GL_FALSE);
1605 break;
1606 case GL_3D_GREEN_MAGENTA:
1607 mpglColorMask(GL_TRUE, GL_FALSE, GL_TRUE, GL_FALSE);
1608 break;
1609 case GL_3D_QUADBUFFER:
1610 mpglGetIntegerv(GL_DRAW_BUFFER, &buffer);
1611 switch (buffer) {
1612 case GL_FRONT:
1613 case GL_FRONT_LEFT:
1614 case GL_FRONT_RIGHT:
1615 buffer = GL_FRONT_RIGHT;
1616 break;
1617 case GL_BACK:
1618 case GL_BACK_LEFT:
1619 case GL_BACK_RIGHT:
1620 buffer = GL_BACK_RIGHT;
1621 break;
1623 mpglDrawBuffer(buffer);
1624 break;
1628 void glDisable3D(int type) {
1629 GLint buffer;
1630 switch (type) {
1631 case GL_3D_RED_CYAN:
1632 case GL_3D_GREEN_MAGENTA:
1633 mpglColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
1634 break;
1635 case GL_3D_QUADBUFFER:
1636 mpglDrawBuffer(vo_doublebuffering ? GL_BACK : GL_FRONT);
1637 mpglGetIntegerv(GL_DRAW_BUFFER, &buffer);
1638 switch (buffer) {
1639 case GL_FRONT:
1640 case GL_FRONT_LEFT:
1641 case GL_FRONT_RIGHT:
1642 buffer = GL_FRONT;
1643 break;
1644 case GL_BACK:
1645 case GL_BACK_LEFT:
1646 case GL_BACK_RIGHT:
1647 buffer = GL_BACK;
1648 break;
1650 mpglDrawBuffer(buffer);
1651 break;
1656 * \brief draw a texture part at given 2D coordinates
1657 * \param x screen top coordinate
1658 * \param y screen left coordinate
1659 * \param w screen width coordinate
1660 * \param h screen height coordinate
1661 * \param tx texture top coordinate in pixels
1662 * \param ty texture left coordinate in pixels
1663 * \param tw texture part width in pixels
1664 * \param th texture part height in pixels
1665 * \param sx width of texture in pixels
1666 * \param sy height of texture in pixels
1667 * \param rect_tex whether this texture uses texture_rectangle extension
1668 * \param is_yv12 if != 0, also draw the textures from units 1 and 2,
1669 * bits 8 - 15 and 16 - 23 specify the x and y scaling of those textures
1670 * \param flip flip the texture upside down
1671 * \ingroup gltexture
1673 void glDrawTex(GLfloat x, GLfloat y, GLfloat w, GLfloat h,
1674 GLfloat tx, GLfloat ty, GLfloat tw, GLfloat th,
1675 int sx, int sy, int rect_tex, int is_yv12, int flip) {
1676 int chroma_x_shift = (is_yv12 >> 8) & 31;
1677 int chroma_y_shift = (is_yv12 >> 16) & 31;
1678 GLfloat xscale = 1 << chroma_x_shift;
1679 GLfloat yscale = 1 << chroma_y_shift;
1680 GLfloat tx2 = tx / xscale, ty2 = ty / yscale, tw2 = tw / xscale, th2 = th / yscale;
1681 if (!rect_tex) {
1682 tx /= sx; ty /= sy; tw /= sx; th /= sy;
1683 tx2 = tx, ty2 = ty, tw2 = tw, th2 = th;
1685 if (flip) {
1686 y += h;
1687 h = -h;
1689 mpglBegin(GL_QUADS);
1690 mpglTexCoord2f(tx, ty);
1691 if (is_yv12) {
1692 mpglMultiTexCoord2f(GL_TEXTURE1, tx2, ty2);
1693 mpglMultiTexCoord2f(GL_TEXTURE2, tx2, ty2);
1695 mpglVertex2f(x, y);
1696 mpglTexCoord2f(tx, ty + th);
1697 if (is_yv12) {
1698 mpglMultiTexCoord2f(GL_TEXTURE1, tx2, ty2 + th2);
1699 mpglMultiTexCoord2f(GL_TEXTURE2, tx2, ty2 + th2);
1701 mpglVertex2f(x, y + h);
1702 mpglTexCoord2f(tx + tw, ty + th);
1703 if (is_yv12) {
1704 mpglMultiTexCoord2f(GL_TEXTURE1, tx2 + tw2, ty2 + th2);
1705 mpglMultiTexCoord2f(GL_TEXTURE2, tx2 + tw2, ty2 + th2);
1707 mpglVertex2f(x + w, y + h);
1708 mpglTexCoord2f(tx + tw, ty);
1709 if (is_yv12) {
1710 mpglMultiTexCoord2f(GL_TEXTURE1, tx2 + tw2, ty2);
1711 mpglMultiTexCoord2f(GL_TEXTURE2, tx2 + tw2, ty2);
1713 mpglVertex2f(x + w, y);
1714 mpglEnd();
1717 #ifdef CONFIG_GL_WIN32
1718 #include "w32_common.h"
1720 * \brief little helper since wglGetProcAddress definition does not fit our
1721 * getProcAddress
1722 * \param procName name of function to look up
1723 * \return function pointer returned by wglGetProcAddress
1725 static void *w32gpa(const GLubyte *procName) {
1726 HMODULE oglmod;
1727 void *res = wglGetProcAddress(procName);
1728 if (res) return res;
1729 oglmod = GetModuleHandle("opengl32.dll");
1730 return GetProcAddress(oglmod, procName);
1733 static int setGlWindow_w32(MPGLContext *ctx)
1735 HWND win = vo_w32_window;
1736 int *vinfo = &ctx->vinfo.w32;
1737 HGLRC *context = &ctx->context.w32;
1738 int new_vinfo;
1739 HDC windc = vo_w32_get_dc(win);
1740 HGLRC new_context = 0;
1741 int keep_context = 0;
1742 int res = SET_WINDOW_FAILED;
1744 // should only be needed when keeping context, but not doing glFinish
1745 // can cause flickering even when we do not keep it.
1746 if (*context)
1747 mpglFinish();
1748 new_vinfo = GetPixelFormat(windc);
1749 if (*context && *vinfo && new_vinfo && *vinfo == new_vinfo) {
1750 // we can keep the wglContext
1751 new_context = *context;
1752 keep_context = 1;
1753 } else {
1754 // create a context
1755 new_context = wglCreateContext(windc);
1756 if (!new_context) {
1757 mp_msg(MSGT_VO, MSGL_FATAL, "[gl] Could not create GL context!\n");
1758 goto out;
1762 // set context
1763 if (!wglMakeCurrent(windc, new_context)) {
1764 mp_msg(MSGT_VO, MSGL_FATAL, "[gl] Could not set GL context!\n");
1765 if (!keep_context) {
1766 wglDeleteContext(new_context);
1768 goto out;
1771 // set new values
1772 vo_w32_window = win;
1774 RECT rect;
1775 GetClientRect(win, &rect);
1776 vo_dwidth = rect.right;
1777 vo_dheight = rect.bottom;
1779 if (!keep_context) {
1780 if (*context)
1781 wglDeleteContext(*context);
1782 *context = new_context;
1783 *vinfo = new_vinfo;
1784 getFunctions(w32gpa, NULL);
1786 // and inform that reinit is neccessary
1787 res = SET_WINDOW_REINIT;
1788 } else
1789 res = SET_WINDOW_OK;
1791 out:
1792 vo_w32_release_dc(win, windc);
1793 return res;
1796 static void releaseGlContext_w32(MPGLContext *ctx) {
1797 int *vinfo = &ctx->vinfo.w32;
1798 HGLRC *context = &ctx->context.w32;
1799 *vinfo = 0;
1800 if (*context) {
1801 wglMakeCurrent(0, 0);
1802 wglDeleteContext(*context);
1804 *context = 0;
1807 static void swapGlBuffers_w32(MPGLContext *ctx) {
1808 HDC vo_hdc = vo_w32_get_dc(vo_w32_window);
1809 SwapBuffers(vo_hdc);
1810 vo_w32_release_dc(vo_w32_window, vo_hdc);
1812 #endif
1813 #ifdef CONFIG_GL_X11
1814 #include "x11_common.h"
1817 * \brief Returns the XVisualInfo associated with Window win.
1818 * \param win Window whose XVisualInfo is returne.
1819 * \return XVisualInfo of the window. Caller must use XFree to free it.
1821 static XVisualInfo *getWindowVisualInfo(Window win) {
1822 XWindowAttributes xw_attr;
1823 XVisualInfo vinfo_template;
1824 int tmp;
1825 XGetWindowAttributes(mDisplay, win, &xw_attr);
1826 vinfo_template.visualid = XVisualIDFromVisual(xw_attr.visual);
1827 return XGetVisualInfo(mDisplay, VisualIDMask, &vinfo_template, &tmp);
1830 static void appendstr(char **dst, const char *str)
1832 int newsize;
1833 char *newstr;
1834 if (!str)
1835 return;
1836 newsize = strlen(*dst) + 1 + strlen(str) + 1;
1837 newstr = realloc(*dst, newsize);
1838 if (!newstr)
1839 return;
1840 *dst = newstr;
1841 strcat(*dst, " ");
1842 strcat(*dst, str);
1846 * \brief Changes the window in which video is displayed.
1847 * If possible only transfers the context to the new window, otherwise
1848 * creates a new one, which must be initialized by the caller.
1849 * \param vinfo Currently used visual.
1850 * \param context Currently used context.
1851 * \param win window that should be used for drawing.
1852 * \return one of SET_WINDOW_FAILED, SET_WINDOW_OK or SET_WINDOW_REINIT.
1853 * In case of SET_WINDOW_REINIT the context could not be transfered
1854 * and the caller must initialize it correctly.
1855 * \ingroup glcontext
1857 static int setGlWindow_x11(MPGLContext *ctx)
1859 XVisualInfo **vinfo = &ctx->vinfo.x11;
1860 GLXContext *context = &ctx->context.x11;
1861 Window win = vo_window;
1862 XVisualInfo *new_vinfo;
1863 GLXContext new_context = NULL;
1864 int keep_context = 0;
1866 // should only be needed when keeping context, but not doing glFinish
1867 // can cause flickering even when we do not keep it.
1868 if (*context)
1869 mpglFinish();
1870 new_vinfo = getWindowVisualInfo(win);
1871 if (*context && *vinfo && new_vinfo &&
1872 (*vinfo)->visualid == new_vinfo->visualid) {
1873 // we can keep the GLXContext
1874 new_context = *context;
1875 XFree(new_vinfo);
1876 new_vinfo = *vinfo;
1877 keep_context = 1;
1878 } else {
1879 // create a context
1880 new_context = glXCreateContext(mDisplay, new_vinfo, NULL, True);
1881 if (!new_context) {
1882 mp_msg(MSGT_VO, MSGL_FATAL, "[gl] Could not create GLX context!\n");
1883 XFree(new_vinfo);
1884 return SET_WINDOW_FAILED;
1888 // set context
1889 if (!glXMakeCurrent(mDisplay, vo_window, new_context)) {
1890 mp_msg(MSGT_VO, MSGL_FATAL, "[gl] Could not set GLX context!\n");
1891 if (!keep_context) {
1892 glXDestroyContext(mDisplay, new_context);
1893 XFree(new_vinfo);
1895 return SET_WINDOW_FAILED;
1898 // set new values
1899 vo_window = win;
1900 vo_x11_update_geometry();
1901 if (!keep_context) {
1902 void *(*getProcAddress)(const GLubyte *);
1903 const char *(*glXExtStr)(Display *, int);
1904 char *glxstr = strdup("");
1905 if (*context)
1906 glXDestroyContext(mDisplay, *context);
1907 *context = new_context;
1908 if (*vinfo)
1909 XFree(*vinfo);
1910 *vinfo = new_vinfo;
1911 getProcAddress = getdladdr("glXGetProcAddress");
1912 if (!getProcAddress)
1913 getProcAddress = getdladdr("glXGetProcAddressARB");
1914 glXExtStr = getdladdr("glXQueryExtensionsString");
1915 if (glXExtStr)
1916 appendstr(&glxstr, glXExtStr(mDisplay, DefaultScreen(mDisplay)));
1917 glXExtStr = getdladdr("glXGetClientString");
1918 if (glXExtStr)
1919 appendstr(&glxstr, glXExtStr(mDisplay, GLX_EXTENSIONS));
1920 glXExtStr = getdladdr("glXGetServerString");
1921 if (glXExtStr)
1922 appendstr(&glxstr, glXExtStr(mDisplay, GLX_EXTENSIONS));
1924 getFunctions(getProcAddress, glxstr);
1925 if (!mpglGenPrograms && mpglGetString &&
1926 getProcAddress &&
1927 strstr(mpglGetString(GL_EXTENSIONS), "GL_ARB_vertex_program")) {
1928 mp_msg(MSGT_VO, MSGL_WARN, "Broken glXGetProcAddress detected, trying workaround\n");
1929 getFunctions(NULL, glxstr);
1931 free(glxstr);
1933 // and inform that reinit is neccessary
1934 return SET_WINDOW_REINIT;
1936 return SET_WINDOW_OK;
1940 * \brief free the VisualInfo and GLXContext of an OpenGL context.
1941 * \ingroup glcontext
1943 static void releaseGlContext_x11(MPGLContext *ctx) {
1944 XVisualInfo **vinfo = &ctx->vinfo.x11;
1945 GLXContext *context = &ctx->context.x11;
1946 if (*vinfo)
1947 XFree(*vinfo);
1948 *vinfo = NULL;
1949 if (*context)
1951 mpglFinish();
1952 glXMakeCurrent(mDisplay, None, NULL);
1953 glXDestroyContext(mDisplay, *context);
1955 *context = 0;
1958 static void swapGlBuffers_x11(MPGLContext *ctx) {
1959 glXSwapBuffers(mDisplay, vo_window);
1962 static int x11_check_events(void) {
1963 return vo_x11_check_events(mDisplay);
1966 static void gl_update_xinerama_info(void)
1968 update_xinerama_info(global_vo);
1971 static void gl_border(void)
1973 vo_x11_border();
1976 static void gl_fullscreen(void)
1978 vo_x11_fullscreen();
1981 static void gl_ontop(void)
1983 vo_x11_ontop();
1985 #endif
1987 #ifdef CONFIG_GL_SDL
1988 #include "sdl_common.h"
1990 static void swapGlBuffers_sdl(MPGLContext *ctx) {
1991 SDL_GL_SwapBuffers();
1994 static void *sdlgpa(const GLubyte *name) {
1995 return SDL_GL_GetProcAddress(name);
1998 static int setGlWindow_sdl(MPGLContext *ctx) {
1999 if (sdl_set_mode(0, SDL_OPENGL | SDL_RESIZABLE) < 0)
2000 return SET_WINDOW_FAILED;
2001 SDL_GL_LoadLibrary(NULL);
2002 getFunctions(sdlgpa, NULL);
2003 return SET_WINDOW_OK;
2006 static int sdl_check_events(void) {
2007 int res = 0;
2008 SDL_Event event;
2009 while (SDL_PollEvent(&event)) {
2010 res |= sdl_default_handle_event(&event);
2012 // poll "events" from within MPlayer code
2013 res |= sdl_default_handle_event(NULL);
2014 if (res & VO_EVENT_RESIZE)
2015 sdl_set_mode(0, SDL_OPENGL | SDL_RESIZABLE);
2016 return res;
2019 #endif
2021 static int setGlWindow_dummy(MPGLContext *ctx) {
2022 getFunctions(NULL, NULL);
2023 return SET_WINDOW_OK;
2026 static void releaseGlContext_dummy(MPGLContext *ctx) {
2029 static int dummy_check_events(void) {
2030 return 0;
2033 static void dummy_update_xinerama_info(void) {
2034 if (vo_screenwidth <= 0 || vo_screenheight <= 0) {
2035 mp_msg(MSGT_VO, MSGL_ERR, "You must specify the screen dimensions "
2036 "with -screenw and -screenh\n");
2037 vo_screenwidth = 1280;
2038 vo_screenheight = 768;
2040 aspect_save_screenres(vo_screenwidth, vo_screenheight);
2043 int init_mpglcontext(MPGLContext *ctx, enum MPGLType type) {
2044 if (type == GLTYPE_AUTO) {
2045 int res = init_mpglcontext(ctx, GLTYPE_W32);
2046 if (res) return res;
2047 res = init_mpglcontext(ctx, GLTYPE_X11);
2048 if (res) return res;
2049 res = init_mpglcontext(ctx, GLTYPE_SDL);
2050 return res;
2052 memset(ctx, 0, sizeof(*ctx));
2053 ctx->setGlWindow = setGlWindow_dummy;
2054 ctx->releaseGlContext = releaseGlContext_dummy;
2055 ctx->update_xinerama_info = dummy_update_xinerama_info;
2056 ctx->check_events = dummy_check_events;
2057 ctx->type = type;
2058 switch (ctx->type) {
2059 #ifdef CONFIG_GL_WIN32
2060 case GLTYPE_W32:
2061 ctx->setGlWindow = setGlWindow_w32;
2062 ctx->releaseGlContext = releaseGlContext_w32;
2063 ctx->swapGlBuffers = swapGlBuffers_w32;
2064 ctx->update_xinerama_info = w32_update_xinerama_info;
2065 ctx->border = vo_w32_border;
2066 ctx->check_events = vo_w32_check_events;
2067 ctx->fullscreen = vo_w32_fullscreen;
2068 ctx->ontop = vo_w32_ontop;
2069 return vo_w32_init();
2070 #endif
2071 #ifdef CONFIG_GL_X11
2072 case GLTYPE_X11:
2073 ctx->setGlWindow = setGlWindow_x11;
2074 ctx->releaseGlContext = releaseGlContext_x11;
2075 ctx->swapGlBuffers = swapGlBuffers_x11;
2076 ctx->update_xinerama_info = gl_update_xinerama_info;
2077 ctx->border = gl_border;
2078 ctx->check_events = x11_check_events;
2079 ctx->fullscreen = gl_fullscreen;
2080 ctx->ontop = gl_ontop;
2081 return vo_init();
2082 #endif
2083 #ifdef CONFIG_GL_SDL
2084 case GLTYPE_SDL:
2085 SDL_Init(SDL_INIT_VIDEO);
2086 ctx->setGlWindow = setGlWindow_sdl;
2087 ctx->swapGlBuffers = swapGlBuffers_sdl;
2088 ctx->check_events = sdl_check_events;
2089 ctx->fullscreen = vo_sdl_fullscreen;
2090 return vo_sdl_init();
2091 #endif
2092 default:
2093 return 0;
2097 void uninit_mpglcontext(MPGLContext *ctx) {
2098 ctx->releaseGlContext(ctx);
2099 switch (ctx->type) {
2100 #ifdef CONFIG_GL_WIN32
2101 case GLTYPE_W32:
2102 vo_w32_uninit();
2103 break;
2104 #endif
2105 #ifdef CONFIG_GL_X11
2106 case GLTYPE_X11:
2107 vo_x11_uninit();
2108 break;
2109 #endif
2110 #ifdef CONFIG_GL_SDL
2111 case GLTYPE_SDL:
2112 vo_sdl_uninit();
2113 break;
2114 #endif
2116 memset(ctx, 0, sizeof(*ctx));