cosmetics: av_sub
[mplayer/greg.git] / libvo / gl_common.c
blobe455acc21337dbeaabc221e01f1f4449373b77fc
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)
581 * \brief creates a texture from a PPM file
582 * \param target texture taget, usually GL_TEXTURE_2D
583 * \param fmt internal texture format, 0 for default
584 * \param filter filter used for scaling, e.g. GL_LINEAR
585 * \param f file to read PPM from
586 * \param width [out] width of texture
587 * \param height [out] height of texture
588 * \param maxval [out] maxval value from PPM file
589 * \return 0 on error, 1 otherwise
590 * \ingroup gltexture
592 int glCreatePPMTex(GLenum target, GLenum fmt, GLint filter,
593 FILE *f, int *width, int *height, int *maxval) {
594 unsigned w, h, m, val, bpp;
595 char *data;
596 GLenum type;
597 ppm_skip(f);
598 if (fgetc(f) != 'P' || fgetc(f) != '6')
599 return 0;
600 ppm_skip(f);
601 if (fscanf(f, "%u", &w) != 1)
602 return 0;
603 ppm_skip(f);
604 if (fscanf(f, "%u", &h) != 1)
605 return 0;
606 ppm_skip(f);
607 if (fscanf(f, "%u", &m) != 1)
608 return 0;
609 val = fgetc(f);
610 if (!isspace(val))
611 return 0;
612 if (w > MAXDIM || h > MAXDIM)
613 return 0;
614 bpp = (m > 255) ? 6 : 3;
615 data = malloc(w * h * bpp);
616 if (fread(data, w * bpp, h, f) != h)
617 return 0;
618 if (!fmt) {
619 fmt = (m > 255) ? hqtexfmt : 3;
620 if (fmt == GL_FLOAT_RGB32_NV && target != GL_TEXTURE_RECTANGLE)
621 fmt = GL_RGB16;
623 type = m > 255 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_BYTE;
624 glCreateClearTex(target, fmt, GL_RGB, type, filter, w, h, 0);
625 glUploadTex(target, GL_RGB, type,
626 data, w * bpp, 0, 0, w, h, 0);
627 free(data);
628 if (width) *width = w;
629 if (height) *height = h;
630 if (maxval) *maxval = m;
631 return 1;
635 * \brief return the number of bytes per pixel for the given format
636 * \param format OpenGL format
637 * \param type OpenGL type
638 * \return bytes per pixel
639 * \ingroup glgeneral
641 * Does not handle all possible variants, just those used by MPlayer
643 int glFmt2bpp(GLenum format, GLenum type) {
644 int component_size = 0;
645 switch (type) {
646 case GL_UNSIGNED_BYTE_3_3_2:
647 case GL_UNSIGNED_BYTE_2_3_3_REV:
648 return 1;
649 case GL_UNSIGNED_SHORT_5_5_5_1:
650 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
651 case GL_UNSIGNED_SHORT_5_6_5:
652 case GL_UNSIGNED_SHORT_5_6_5_REV:
653 return 2;
654 case GL_UNSIGNED_BYTE:
655 component_size = 1;
656 break;
657 case GL_UNSIGNED_SHORT:
658 component_size = 2;
659 break;
661 switch (format) {
662 case GL_LUMINANCE:
663 case GL_ALPHA:
664 return component_size;
665 case GL_YCBCR_MESA:
666 return 2;
667 case GL_RGB:
668 case GL_BGR:
669 return 3 * component_size;
670 case GL_RGBA:
671 case GL_BGRA:
672 return 4 * component_size;
674 return 0; // unknown
678 * \brief upload a texture, handling things like stride and slices
679 * \param target texture target, usually GL_TEXTURE_2D
680 * \param format OpenGL format of data
681 * \param type OpenGL type of data
682 * \param dataptr data to upload
683 * \param stride data stride
684 * \param x x offset in texture
685 * \param y y offset in texture
686 * \param w width of the texture part to upload
687 * \param h height of the texture part to upload
688 * \param slice height of an upload slice, 0 for all at once
689 * \ingroup gltexture
691 void glUploadTex(GLenum target, GLenum format, GLenum type,
692 const void *dataptr, int stride,
693 int x, int y, int w, int h, int slice) {
694 const uint8_t *data = dataptr;
695 int y_max = y + h;
696 if (w <= 0 || h <= 0) return;
697 if (slice <= 0)
698 slice = h;
699 if (stride < 0) {
700 data += (h - 1) * stride;
701 stride = -stride;
703 // this is not always correct, but should work for MPlayer
704 glAdjustAlignment(stride);
705 mpglPixelStorei(GL_UNPACK_ROW_LENGTH, stride / glFmt2bpp(format, type));
706 for (; y + slice <= y_max; y += slice) {
707 mpglTexSubImage2D(target, 0, x, y, w, slice, format, type, data);
708 data += stride * slice;
710 if (y < y_max)
711 mpglTexSubImage2D(target, 0, x, y, w, y_max - y, format, type, data);
714 static void fillUVcoeff(GLfloat *ucoef, GLfloat *vcoef,
715 float uvcos, float uvsin) {
716 int i;
717 ucoef[0] = 0 * uvcos + 1.403 * uvsin;
718 vcoef[0] = 0 * uvsin + 1.403 * uvcos;
719 ucoef[1] = -0.344 * uvcos + -0.714 * uvsin;
720 vcoef[1] = -0.344 * uvsin + -0.714 * uvcos;
721 ucoef[2] = 1.770 * uvcos + 0 * uvsin;
722 vcoef[2] = 1.770 * uvsin + 0 * uvcos;
723 ucoef[3] = 0;
724 vcoef[3] = 0;
725 // Coefficients (probably) must be in [0, 1] range, whereas they originally
726 // are in [-2, 2] range, so here comes the trick:
727 // First put them in the [-0.5, 0.5] range, then add 0.5.
728 // This can be undone with the HALF_BIAS and SCALE_BY_FOUR arguments
729 // for CombinerInput and CombinerOutput (or the respective ATI variants)
730 for (i = 0; i < 4; i++) {
731 ucoef[i] = ucoef[i] * 0.25 + 0.5;
732 vcoef[i] = vcoef[i] * 0.25 + 0.5;
737 * \brief Setup register combiners for YUV to RGB conversion.
738 * \param uvcos used for saturation and hue adjustment
739 * \param uvsin used for saturation and hue adjustment
741 static void glSetupYUVCombiners(float uvcos, float uvsin) {
742 GLfloat ucoef[4];
743 GLfloat vcoef[4];
744 GLint i;
745 if (!mpglCombinerInput || !mpglCombinerOutput ||
746 !mpglCombinerParameterfv || !mpglCombinerParameteri) {
747 mp_msg(MSGT_VO, MSGL_FATAL, "[gl] Combiner functions missing!\n");
748 return;
750 mpglGetIntegerv(GL_MAX_GENERAL_COMBINERS_NV, &i);
751 if (i < 2)
752 mp_msg(MSGT_VO, MSGL_ERR,
753 "[gl] 2 general combiners needed for YUV combiner support (found %i)\n", i);
754 mpglGetIntegerv(GL_MAX_TEXTURE_UNITS, &i);
755 if (i < 3)
756 mp_msg(MSGT_VO, MSGL_ERR,
757 "[gl] 3 texture units needed for YUV combiner support (found %i)\n", i);
758 fillUVcoeff(ucoef, vcoef, uvcos, uvsin);
759 mpglCombinerParameterfv(GL_CONSTANT_COLOR0_NV, ucoef);
760 mpglCombinerParameterfv(GL_CONSTANT_COLOR1_NV, vcoef);
762 // UV first, like this green component cannot overflow
763 mpglCombinerInput(GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_A_NV,
764 GL_TEXTURE1, GL_HALF_BIAS_NORMAL_NV, GL_RGB);
765 mpglCombinerInput(GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_B_NV,
766 GL_CONSTANT_COLOR0_NV, GL_HALF_BIAS_NORMAL_NV, GL_RGB);
767 mpglCombinerInput(GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_C_NV,
768 GL_TEXTURE2, GL_HALF_BIAS_NORMAL_NV, GL_RGB);
769 mpglCombinerInput(GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_D_NV,
770 GL_CONSTANT_COLOR1_NV, GL_HALF_BIAS_NORMAL_NV, GL_RGB);
771 mpglCombinerOutput(GL_COMBINER0_NV, GL_RGB, GL_DISCARD_NV, GL_DISCARD_NV,
772 GL_SPARE0_NV, GL_SCALE_BY_FOUR_NV, GL_NONE, GL_FALSE,
773 GL_FALSE, GL_FALSE);
775 // stage 2
776 mpglCombinerInput(GL_COMBINER1_NV, GL_RGB, GL_VARIABLE_A_NV, GL_SPARE0_NV,
777 GL_SIGNED_IDENTITY_NV, GL_RGB);
778 mpglCombinerInput(GL_COMBINER1_NV, GL_RGB, GL_VARIABLE_B_NV, GL_ZERO,
779 GL_UNSIGNED_INVERT_NV, GL_RGB);
780 mpglCombinerInput(GL_COMBINER1_NV, GL_RGB, GL_VARIABLE_C_NV,
781 GL_TEXTURE0, GL_SIGNED_IDENTITY_NV, GL_RGB);
782 mpglCombinerInput(GL_COMBINER1_NV, GL_RGB, GL_VARIABLE_D_NV, GL_ZERO,
783 GL_UNSIGNED_INVERT_NV, GL_RGB);
784 mpglCombinerOutput(GL_COMBINER1_NV, GL_RGB, GL_DISCARD_NV, GL_DISCARD_NV,
785 GL_SPARE0_NV, GL_NONE, GL_NONE, GL_FALSE,
786 GL_FALSE, GL_FALSE);
788 // leave final combiner stage in default mode
789 mpglCombinerParameteri(GL_NUM_GENERAL_COMBINERS_NV, 2);
793 * \brief Setup ATI version of register combiners for YUV to RGB conversion.
794 * \param csp_params parameters used for colorspace conversion
795 * \param text if set use the GL_ATI_text_fragment_shader API as
796 * used on OS X.
798 static void glSetupYUVFragmentATI(struct mp_csp_params *csp_params,
799 int text) {
800 GLint i;
801 float yuv2rgb[3][4];
803 mpglGetIntegerv (GL_MAX_TEXTURE_UNITS, &i);
804 if (i < 3)
805 mp_msg(MSGT_VO, MSGL_ERR,
806 "[gl] 3 texture units needed for YUV combiner (ATI) support (found %i)\n", i);
808 mp_get_yuv2rgb_coeffs(csp_params, yuv2rgb);
809 for (i = 0; i < 3; i++) {
810 int j;
811 yuv2rgb[i][3] -= -0.5 * (yuv2rgb[i][1] + yuv2rgb[i][2]);
812 for (j = 0; j < 4; j++) {
813 yuv2rgb[i][j] *= 0.125;
814 yuv2rgb[i][j] += 0.5;
815 if (yuv2rgb[i][j] > 1)
816 yuv2rgb[i][j] = 1;
817 if (yuv2rgb[i][j] < 0)
818 yuv2rgb[i][j] = 0;
821 if (text == 0) {
822 GLfloat c0[4] = {yuv2rgb[0][0], yuv2rgb[1][0], yuv2rgb[2][0]};
823 GLfloat c1[4] = {yuv2rgb[0][1], yuv2rgb[1][1], yuv2rgb[2][1]};
824 GLfloat c2[4] = {yuv2rgb[0][2], yuv2rgb[1][2], yuv2rgb[2][2]};
825 GLfloat c3[4] = {yuv2rgb[0][3], yuv2rgb[1][3], yuv2rgb[2][3]};
826 if (!mpglBeginFragmentShader || !mpglEndFragmentShader ||
827 !mpglSetFragmentShaderConstant || !mpglSampleMap ||
828 !mpglColorFragmentOp2 || !mpglColorFragmentOp3) {
829 mp_msg(MSGT_VO, MSGL_FATAL, "[gl] Combiner (ATI) functions missing!\n");
830 return;
832 mpglGetIntegerv(GL_NUM_FRAGMENT_REGISTERS_ATI, &i);
833 if (i < 3)
834 mp_msg(MSGT_VO, MSGL_ERR,
835 "[gl] 3 registers needed for YUV combiner (ATI) support (found %i)\n", i);
836 mpglBeginFragmentShader();
837 mpglSetFragmentShaderConstant(GL_CON_0_ATI, c0);
838 mpglSetFragmentShaderConstant(GL_CON_1_ATI, c1);
839 mpglSetFragmentShaderConstant(GL_CON_2_ATI, c2);
840 mpglSetFragmentShaderConstant(GL_CON_3_ATI, c3);
841 mpglSampleMap(GL_REG_0_ATI, GL_TEXTURE0, GL_SWIZZLE_STR_ATI);
842 mpglSampleMap(GL_REG_1_ATI, GL_TEXTURE1, GL_SWIZZLE_STR_ATI);
843 mpglSampleMap(GL_REG_2_ATI, GL_TEXTURE2, GL_SWIZZLE_STR_ATI);
844 mpglColorFragmentOp2(GL_MUL_ATI, GL_REG_1_ATI, GL_NONE, GL_NONE,
845 GL_REG_1_ATI, GL_NONE, GL_BIAS_BIT_ATI,
846 GL_CON_1_ATI, GL_NONE, GL_BIAS_BIT_ATI);
847 mpglColorFragmentOp3(GL_MAD_ATI, GL_REG_2_ATI, GL_NONE, GL_NONE,
848 GL_REG_2_ATI, GL_NONE, GL_BIAS_BIT_ATI,
849 GL_CON_2_ATI, GL_NONE, GL_BIAS_BIT_ATI,
850 GL_REG_1_ATI, GL_NONE, GL_NONE);
851 mpglColorFragmentOp3(GL_MAD_ATI, GL_REG_0_ATI, GL_NONE, GL_NONE,
852 GL_REG_0_ATI, GL_NONE, GL_NONE,
853 GL_CON_0_ATI, GL_NONE, GL_BIAS_BIT_ATI,
854 GL_REG_2_ATI, GL_NONE, GL_NONE);
855 mpglColorFragmentOp2(GL_ADD_ATI, GL_REG_0_ATI, GL_NONE, GL_8X_BIT_ATI,
856 GL_REG_0_ATI, GL_NONE, GL_NONE,
857 GL_CON_3_ATI, GL_NONE, GL_BIAS_BIT_ATI);
858 mpglEndFragmentShader();
859 } else {
860 static const char template[] =
861 "!!ATIfs1.0\n"
862 "StartConstants;\n"
863 " CONSTANT c0 = {%e, %e, %e};\n"
864 " CONSTANT c1 = {%e, %e, %e};\n"
865 " CONSTANT c2 = {%e, %e, %e};\n"
866 " CONSTANT c3 = {%e, %e, %e};\n"
867 "EndConstants;\n"
868 "StartOutputPass;\n"
869 " SampleMap r0, t0.str;\n"
870 " SampleMap r1, t1.str;\n"
871 " SampleMap r2, t2.str;\n"
872 " MUL r1.rgb, r1.bias, c1.bias;\n"
873 " MAD r2.rgb, r2.bias, c2.bias, r1;\n"
874 " MAD r0.rgb, r0, c0.bias, r2;\n"
875 " ADD r0.rgb.8x, r0, c3.bias;\n"
876 "EndPass;\n";
877 char buffer[512];
878 snprintf(buffer, sizeof(buffer), template,
879 yuv2rgb[0][0], yuv2rgb[1][0], yuv2rgb[2][0],
880 yuv2rgb[0][1], yuv2rgb[1][1], yuv2rgb[2][1],
881 yuv2rgb[0][2], yuv2rgb[1][2], yuv2rgb[2][2],
882 yuv2rgb[0][3], yuv2rgb[1][3], yuv2rgb[2][3]);
883 mp_msg(MSGT_VO, MSGL_DBG2, "[gl] generated fragment program:\n%s\n", buffer);
884 loadGPUProgram(GL_TEXT_FRAGMENT_SHADER_ATI, buffer);
889 * \brief helper function for gen_spline_lookup_tex
890 * \param x subpixel-position ((0,1) range) to calculate weights for
891 * \param dst where to store transformed weights, must provide space for 4 GLfloats
893 * calculates the weights and stores them after appropriate transformation
894 * for the scaler fragment program.
896 static void store_weights(float x, GLfloat *dst) {
897 float w0 = (((-1 * x + 3) * x - 3) * x + 1) / 6;
898 float w1 = ((( 3 * x - 6) * x + 0) * x + 4) / 6;
899 float w2 = (((-3 * x + 3) * x + 3) * x + 1) / 6;
900 float w3 = ((( 1 * x + 0) * x + 0) * x + 0) / 6;
901 *dst++ = 1 + x - w1 / (w0 + w1);
902 *dst++ = 1 - x + w3 / (w2 + w3);
903 *dst++ = w0 + w1;
904 *dst++ = 0;
907 //! to avoid artefacts this should be rather large
908 #define LOOKUP_BSPLINE_RES (2 * 1024)
910 * \brief creates the 1D lookup texture needed for fast higher-order filtering
911 * \param unit texture unit to attach texture to
913 static void gen_spline_lookup_tex(GLenum unit) {
914 GLfloat *tex = calloc(4 * LOOKUP_BSPLINE_RES, sizeof(*tex));
915 GLfloat *tp = tex;
916 int i;
917 for (i = 0; i < LOOKUP_BSPLINE_RES; i++) {
918 float x = (float)(i + 0.5) / LOOKUP_BSPLINE_RES;
919 store_weights(x, tp);
920 tp += 4;
922 store_weights(0, tex);
923 store_weights(1, &tex[4 * (LOOKUP_BSPLINE_RES - 1)]);
924 mpglActiveTexture(unit);
925 mpglTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA16, LOOKUP_BSPLINE_RES, 0, GL_RGBA, GL_FLOAT, tex);
926 mpglTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_PRIORITY, 1.0);
927 mpglTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
928 mpglTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
929 mpglTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT);
930 mpglActiveTexture(GL_TEXTURE0);
931 free(tex);
934 static const char *bilin_filt_template =
935 "TEX yuv.%c, fragment.texcoord[%c], texture[%c], %s;\n";
937 #define BICUB_FILT_MAIN(textype) \
938 /* first y-interpolation */ \
939 "ADD coord, fragment.texcoord[%c].xyxy, cdelta.xyxw;\n" \
940 "ADD coord2, fragment.texcoord[%c].xyxy, cdelta.zyzw;\n" \
941 "TEX a.r, coord.xyxy, texture[%c], "textype";\n" \
942 "TEX a.g, coord.zwzw, texture[%c], "textype";\n" \
943 /* second y-interpolation */ \
944 "TEX b.r, coord2.xyxy, texture[%c], "textype";\n" \
945 "TEX b.g, coord2.zwzw, texture[%c], "textype";\n" \
946 "LRP a.b, parmy.b, a.rrrr, a.gggg;\n" \
947 "LRP a.a, parmy.b, b.rrrr, b.gggg;\n" \
948 /* x-interpolation */ \
949 "LRP yuv.%c, parmx.b, a.bbbb, a.aaaa;\n"
951 static const char *bicub_filt_template_2D =
952 "MAD coord.xy, fragment.texcoord[%c], {%e, %e}, {0.5, 0.5};\n"
953 "TEX parmx, coord.x, texture[%c], 1D;\n"
954 "MUL cdelta.xz, parmx.rrgg, {-%e, 0, %e, 0};\n"
955 "TEX parmy, coord.y, texture[%c], 1D;\n"
956 "MUL cdelta.yw, parmy.rrgg, {0, -%e, 0, %e};\n"
957 BICUB_FILT_MAIN("2D");
959 static const char *bicub_filt_template_RECT =
960 "ADD coord, fragment.texcoord[%c], {0.5, 0.5};\n"
961 "TEX parmx, coord.x, texture[%c], 1D;\n"
962 "MUL cdelta.xz, parmx.rrgg, {-1, 0, 1, 0};\n"
963 "TEX parmy, coord.y, texture[%c], 1D;\n"
964 "MUL cdelta.yw, parmy.rrgg, {0, -1, 0, 1};\n"
965 BICUB_FILT_MAIN("RECT");
967 #define CALCWEIGHTS(t, s) \
968 "MAD "t", {-0.5, 0.1666, 0.3333, -0.3333}, "s", {1, 0, -0.5, 0.5};\n" \
969 "MAD "t", "t", "s", {0, 0, -0.5, 0.5};\n" \
970 "MAD "t", "t", "s", {-0.6666, 0, 0.8333, 0.1666};\n" \
971 "RCP a.x, "t".z;\n" \
972 "RCP a.y, "t".w;\n" \
973 "MAD "t".xy, "t".xyxy, a.xyxy, {1, 1, 0, 0};\n" \
974 "ADD "t".x, "t".xxxx, "s";\n" \
975 "SUB "t".y, "t".yyyy, "s";\n"
977 static const char *bicub_notex_filt_template_2D =
978 "MAD coord.xy, fragment.texcoord[%c], {%e, %e}, {0.5, 0.5};\n"
979 "FRC coord.xy, coord.xyxy;\n"
980 CALCWEIGHTS("parmx", "coord.xxxx")
981 "MUL cdelta.xz, parmx.rrgg, {-%e, 0, %e, 0};\n"
982 CALCWEIGHTS("parmy", "coord.yyyy")
983 "MUL cdelta.yw, parmy.rrgg, {0, -%e, 0, %e};\n"
984 BICUB_FILT_MAIN("2D");
986 static const char *bicub_notex_filt_template_RECT =
987 "ADD coord, fragment.texcoord[%c], {0.5, 0.5};\n"
988 "FRC coord.xy, coord.xyxy;\n"
989 CALCWEIGHTS("parmx", "coord.xxxx")
990 "MUL cdelta.xz, parmx.rrgg, {-1, 0, 1, 0};\n"
991 CALCWEIGHTS("parmy", "coord.yyyy")
992 "MUL cdelta.yw, parmy.rrgg, {0, -1, 0, 1};\n"
993 BICUB_FILT_MAIN("RECT");
995 #define BICUB_X_FILT_MAIN(textype) \
996 "ADD coord.xy, fragment.texcoord[%c].xyxy, cdelta.xyxy;\n" \
997 "ADD coord2.xy, fragment.texcoord[%c].xyxy, cdelta.zyzy;\n" \
998 "TEX a.r, coord, texture[%c], "textype";\n" \
999 "TEX b.r, coord2, texture[%c], "textype";\n" \
1000 /* x-interpolation */ \
1001 "LRP yuv.%c, parmx.b, a.rrrr, b.rrrr;\n"
1003 static const char *bicub_x_filt_template_2D =
1004 "MAD coord.x, fragment.texcoord[%c], {%e}, {0.5};\n"
1005 "TEX parmx, coord, texture[%c], 1D;\n"
1006 "MUL cdelta.xyz, parmx.rrgg, {-%e, 0, %e};\n"
1007 BICUB_X_FILT_MAIN("2D");
1009 static const char *bicub_x_filt_template_RECT =
1010 "ADD coord.x, fragment.texcoord[%c], {0.5};\n"
1011 "TEX parmx, coord, texture[%c], 1D;\n"
1012 "MUL cdelta.xyz, parmx.rrgg, {-1, 0, 1};\n"
1013 BICUB_X_FILT_MAIN("RECT");
1015 static const char *unsharp_filt_template =
1016 "PARAM dcoord%c = {%e, %e, %e, %e};\n"
1017 "ADD coord, fragment.texcoord[%c].xyxy, dcoord%c;\n"
1018 "SUB coord2, fragment.texcoord[%c].xyxy, dcoord%c;\n"
1019 "TEX a.r, fragment.texcoord[%c], texture[%c], %s;\n"
1020 "TEX b.r, coord.xyxy, texture[%c], %s;\n"
1021 "TEX b.g, coord.zwzw, texture[%c], %s;\n"
1022 "ADD b.r, b.r, b.g;\n"
1023 "TEX b.b, coord2.xyxy, texture[%c], %s;\n"
1024 "TEX b.g, coord2.zwzw, texture[%c], %s;\n"
1025 "DP3 b, b, {0.25, 0.25, 0.25};\n"
1026 "SUB b.r, a.r, b.r;\n"
1027 "MAD yuv.%c, b.r, {%e}, a.r;\n";
1029 static const char *unsharp_filt_template2 =
1030 "PARAM dcoord%c = {%e, %e, %e, %e};\n"
1031 "PARAM dcoord2%c = {%e, 0, 0, %e};\n"
1032 "ADD coord, fragment.texcoord[%c].xyxy, dcoord%c;\n"
1033 "SUB coord2, fragment.texcoord[%c].xyxy, dcoord%c;\n"
1034 "TEX a.r, fragment.texcoord[%c], texture[%c], %s;\n"
1035 "TEX b.r, coord.xyxy, texture[%c], %s;\n"
1036 "TEX b.g, coord.zwzw, texture[%c], %s;\n"
1037 "ADD b.r, b.r, b.g;\n"
1038 "TEX b.b, coord2.xyxy, texture[%c], %s;\n"
1039 "TEX b.g, coord2.zwzw, texture[%c], %s;\n"
1040 "ADD b.r, b.r, b.b;\n"
1041 "ADD b.a, b.r, b.g;\n"
1042 "ADD coord, fragment.texcoord[%c].xyxy, dcoord2%c;\n"
1043 "SUB coord2, fragment.texcoord[%c].xyxy, dcoord2%c;\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 "DP4 b.r, b, {-0.1171875, -0.1171875, -0.1171875, -0.09765625};\n"
1050 "MAD b.r, a.r, {0.859375}, b.r;\n"
1051 "MAD yuv.%c, b.r, {%e}, a.r;\n";
1053 static const char *yuv_prog_template =
1054 "PARAM ycoef = {%e, %e, %e};\n"
1055 "PARAM ucoef = {%e, %e, %e};\n"
1056 "PARAM vcoef = {%e, %e, %e};\n"
1057 "PARAM offsets = {%e, %e, %e};\n"
1058 "TEMP res;\n"
1059 "MAD res.rgb, yuv.rrrr, ycoef, offsets;\n"
1060 "MAD res.rgb, yuv.gggg, ucoef, res;\n"
1061 "MAD result.color.rgb, yuv.bbbb, vcoef, res;\n"
1062 "END";
1064 static const char *yuv_pow_prog_template =
1065 "PARAM ycoef = {%e, %e, %e};\n"
1066 "PARAM ucoef = {%e, %e, %e};\n"
1067 "PARAM vcoef = {%e, %e, %e};\n"
1068 "PARAM offsets = {%e, %e, %e};\n"
1069 "PARAM gamma = {%e, %e, %e};\n"
1070 "TEMP res;\n"
1071 "MAD res.rgb, yuv.rrrr, ycoef, offsets;\n"
1072 "MAD res.rgb, yuv.gggg, ucoef, res;\n"
1073 "MAD_SAT res.rgb, yuv.bbbb, vcoef, res;\n"
1074 "POW result.color.r, res.r, gamma.r;\n"
1075 "POW result.color.g, res.g, gamma.g;\n"
1076 "POW result.color.b, res.b, gamma.b;\n"
1077 "END";
1079 static const char *yuv_lookup_prog_template =
1080 "PARAM ycoef = {%e, %e, %e, 0};\n"
1081 "PARAM ucoef = {%e, %e, %e, 0};\n"
1082 "PARAM vcoef = {%e, %e, %e, 0};\n"
1083 "PARAM offsets = {%e, %e, %e, 0.125};\n"
1084 "TEMP res;\n"
1085 "MAD res, yuv.rrrr, ycoef, offsets;\n"
1086 "MAD res.rgb, yuv.gggg, ucoef, res;\n"
1087 "MAD res.rgb, yuv.bbbb, vcoef, res;\n"
1088 "TEX result.color.r, res.raaa, texture[%c], 2D;\n"
1089 "ADD res.a, res.a, 0.25;\n"
1090 "TEX result.color.g, res.gaaa, texture[%c], 2D;\n"
1091 "ADD res.a, res.a, 0.25;\n"
1092 "TEX result.color.b, res.baaa, texture[%c], 2D;\n"
1093 "END";
1095 static const char *yuv_lookup3d_prog_template =
1096 "TEX result.color, yuv, texture[%c], 3D;\n"
1097 "END";
1100 * \brief creates and initializes helper textures needed for scaling texture read
1101 * \param scaler scaler type to create texture for
1102 * \param texu contains next free texture unit number
1103 * \param texs texture unit ids for the scaler are stored in this array
1105 static void create_scaler_textures(int scaler, int *texu, char *texs) {
1106 switch (scaler) {
1107 case YUV_SCALER_BILIN:
1108 case YUV_SCALER_BICUB_NOTEX:
1109 case YUV_SCALER_UNSHARP:
1110 case YUV_SCALER_UNSHARP2:
1111 break;
1112 case YUV_SCALER_BICUB:
1113 case YUV_SCALER_BICUB_X:
1114 texs[0] = (*texu)++;
1115 gen_spline_lookup_tex(GL_TEXTURE0 + texs[0]);
1116 texs[0] += '0';
1117 break;
1118 default:
1119 mp_msg(MSGT_VO, MSGL_ERR, "[gl] unknown scaler type %i\n", scaler);
1123 //! resolution of texture for gamma lookup table
1124 #define LOOKUP_RES 512
1125 //! resolution for 3D yuv->rgb conversion lookup table
1126 #define LOOKUP_3DRES 32
1128 * \brief creates and initializes helper textures needed for yuv conversion
1129 * \param params struct containing parameters like brightness, gamma, ...
1130 * \param texu contains next free texture unit number
1131 * \param texs texture unit ids for the conversion are stored in this array
1133 static void create_conv_textures(gl_conversion_params_t *params, int *texu, char *texs) {
1134 unsigned char *lookup_data = NULL;
1135 int conv = YUV_CONVERSION(params->type);
1136 switch (conv) {
1137 case YUV_CONVERSION_FRAGMENT:
1138 case YUV_CONVERSION_FRAGMENT_POW:
1139 break;
1140 case YUV_CONVERSION_FRAGMENT_LOOKUP:
1141 texs[0] = (*texu)++;
1142 mpglActiveTexture(GL_TEXTURE0 + texs[0]);
1143 lookup_data = malloc(4 * LOOKUP_RES);
1144 mp_gen_gamma_map(lookup_data, LOOKUP_RES, params->csp_params.rgamma);
1145 mp_gen_gamma_map(&lookup_data[LOOKUP_RES], LOOKUP_RES, params->csp_params.ggamma);
1146 mp_gen_gamma_map(&lookup_data[2 * LOOKUP_RES], LOOKUP_RES, params->csp_params.bgamma);
1147 glCreateClearTex(GL_TEXTURE_2D, GL_LUMINANCE8, GL_LUMINANCE, GL_UNSIGNED_BYTE, GL_LINEAR,
1148 LOOKUP_RES, 4, 0);
1149 glUploadTex(GL_TEXTURE_2D, GL_LUMINANCE, GL_UNSIGNED_BYTE, lookup_data,
1150 LOOKUP_RES, 0, 0, LOOKUP_RES, 4, 0);
1151 mpglActiveTexture(GL_TEXTURE0);
1152 texs[0] += '0';
1153 break;
1154 case YUV_CONVERSION_FRAGMENT_LOOKUP3D:
1156 int sz = LOOKUP_3DRES + 2; // texture size including borders
1157 if (!mpglTexImage3D) {
1158 mp_msg(MSGT_VO, MSGL_ERR, "[gl] Missing 3D texture function!\n");
1159 break;
1161 texs[0] = (*texu)++;
1162 mpglActiveTexture(GL_TEXTURE0 + texs[0]);
1163 lookup_data = malloc(3 * sz * sz * sz);
1164 mp_gen_yuv2rgb_map(&params->csp_params, lookup_data, LOOKUP_3DRES);
1165 glAdjustAlignment(sz);
1166 mpglPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
1167 mpglTexImage3D(GL_TEXTURE_3D, 0, 3, sz, sz, sz, 1,
1168 GL_RGB, GL_UNSIGNED_BYTE, lookup_data);
1169 mpglTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_PRIORITY, 1.0);
1170 mpglTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1171 mpglTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1172 mpglTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP);
1173 mpglTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP);
1174 mpglTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP);
1175 mpglActiveTexture(GL_TEXTURE0);
1176 texs[0] += '0';
1178 break;
1179 default:
1180 mp_msg(MSGT_VO, MSGL_ERR, "[gl] unknown conversion type %i\n", conv);
1182 if (lookup_data)
1183 free(lookup_data);
1187 * \brief adds a scaling texture read at the current fragment program position
1188 * \param scaler type of scaler to insert
1189 * \param prog_pos current position in fragment program
1190 * \param remain how many bytes remain in the buffer given by prog_pos
1191 * \param texs array containing the texture unit identifiers for this scaler
1192 * \param in_tex texture unit the scaler should read from
1193 * \param out_comp component of the yuv variable the scaler stores the result in
1194 * \param rect if rectangular (pixel) adressing should be used for in_tex
1195 * \param texw width of the in_tex texture
1196 * \param texh height of the in_tex texture
1197 * \param strength strength of filter effect if the scaler does some kind of filtering
1199 static void add_scaler(int scaler, char **prog_pos, int *remain, char *texs,
1200 char in_tex, char out_comp, int rect, int texw, int texh,
1201 double strength) {
1202 const char *ttype = rect ? "RECT" : "2D";
1203 const float ptw = rect ? 1.0 : 1.0 / texw;
1204 const float pth = rect ? 1.0 : 1.0 / texh;
1205 switch (scaler) {
1206 case YUV_SCALER_BILIN:
1207 snprintf(*prog_pos, *remain, bilin_filt_template, out_comp, in_tex,
1208 in_tex, ttype);
1209 break;
1210 case YUV_SCALER_BICUB:
1211 if (rect)
1212 snprintf(*prog_pos, *remain, bicub_filt_template_RECT,
1213 in_tex, texs[0], texs[0],
1214 in_tex, in_tex, in_tex, in_tex, in_tex, in_tex, out_comp);
1215 else
1216 snprintf(*prog_pos, *remain, bicub_filt_template_2D,
1217 in_tex, (float)texw, (float)texh,
1218 texs[0], ptw, ptw, texs[0], pth, pth,
1219 in_tex, in_tex, in_tex, in_tex, in_tex, in_tex, out_comp);
1220 break;
1221 case YUV_SCALER_BICUB_X:
1222 if (rect)
1223 snprintf(*prog_pos, *remain, bicub_x_filt_template_RECT,
1224 in_tex, texs[0],
1225 in_tex, in_tex, in_tex, in_tex, out_comp);
1226 else
1227 snprintf(*prog_pos, *remain, bicub_x_filt_template_2D,
1228 in_tex, (float)texw,
1229 texs[0], ptw, ptw,
1230 in_tex, in_tex, in_tex, in_tex, out_comp);
1231 break;
1232 case YUV_SCALER_BICUB_NOTEX:
1233 if (rect)
1234 snprintf(*prog_pos, *remain, bicub_notex_filt_template_RECT,
1235 in_tex,
1236 in_tex, in_tex, in_tex, in_tex, in_tex, in_tex, out_comp);
1237 else
1238 snprintf(*prog_pos, *remain, bicub_notex_filt_template_2D,
1239 in_tex, (float)texw, (float)texh, ptw, ptw, pth, pth,
1240 in_tex, in_tex, in_tex, in_tex, in_tex, in_tex, out_comp);
1241 break;
1242 case YUV_SCALER_UNSHARP:
1243 snprintf(*prog_pos, *remain, unsharp_filt_template,
1244 out_comp, 0.5 * ptw, 0.5 * pth, 0.5 * ptw, -0.5 * pth,
1245 in_tex, out_comp, in_tex, out_comp, in_tex,
1246 in_tex, ttype, in_tex, ttype, in_tex, ttype, in_tex, ttype,
1247 in_tex, ttype, out_comp, strength);
1248 break;
1249 case YUV_SCALER_UNSHARP2:
1250 snprintf(*prog_pos, *remain, unsharp_filt_template2,
1251 out_comp, 1.2 * ptw, 1.2 * pth, 1.2 * ptw, -1.2 * pth,
1252 out_comp, 1.5 * ptw, 1.5 * pth,
1253 in_tex, out_comp, in_tex, out_comp, in_tex,
1254 in_tex, ttype, in_tex, ttype, in_tex, ttype, in_tex, ttype,
1255 in_tex, ttype, in_tex, out_comp, in_tex, out_comp,
1256 in_tex, ttype, in_tex, ttype, in_tex, ttype,
1257 in_tex, ttype, out_comp, strength);
1258 break;
1260 *remain -= strlen(*prog_pos);
1261 *prog_pos += strlen(*prog_pos);
1264 static const struct {
1265 const char *name;
1266 GLenum cur;
1267 GLenum max;
1268 } progstats[] = {
1269 {"instructions", 0x88A0, 0x88A1},
1270 {"native instructions", 0x88A2, 0x88A3},
1271 {"temporaries", 0x88A4, 0x88A5},
1272 {"native temporaries", 0x88A6, 0x88A7},
1273 {"parameters", 0x88A8, 0x88A9},
1274 {"native parameters", 0x88AA, 0x88AB},
1275 {"attribs", 0x88AC, 0x88AD},
1276 {"native attribs", 0x88AE, 0x88AF},
1277 {"ALU instructions", 0x8805, 0x880B},
1278 {"TEX instructions", 0x8806, 0x880C},
1279 {"TEX indirections", 0x8807, 0x880D},
1280 {"native ALU instructions", 0x8808, 0x880E},
1281 {"native TEX instructions", 0x8809, 0x880F},
1282 {"native TEX indirections", 0x880A, 0x8810},
1283 {NULL, 0, 0}
1287 * \brief load the specified GPU Program
1288 * \param target program target to load into, only GL_FRAGMENT_PROGRAM is tested
1289 * \param prog program string
1290 * \return 1 on success, 0 otherwise
1292 int loadGPUProgram(GLenum target, char *prog) {
1293 int i;
1294 GLint cur = 0, max = 0, err = 0;
1295 if (!mpglProgramString) {
1296 mp_msg(MSGT_VO, MSGL_ERR, "[gl] Missing GPU program function\n");
1297 return 0;
1299 mpglProgramString(target, GL_PROGRAM_FORMAT_ASCII, strlen(prog), prog);
1300 mpglGetIntegerv(GL_PROGRAM_ERROR_POSITION, &err);
1301 if (err != -1) {
1302 mp_msg(MSGT_VO, MSGL_ERR,
1303 "[gl] Error compiling fragment program, make sure your card supports\n"
1304 "[gl] GL_ARB_fragment_program (use glxinfo to check).\n"
1305 "[gl] Error message:\n %s at %.10s\n",
1306 mpglGetString(GL_PROGRAM_ERROR_STRING), &prog[err]);
1307 return 0;
1309 if (!mpglGetProgramiv || !mp_msg_test(MSGT_VO, MSGL_DBG2))
1310 return 1;
1311 mp_msg(MSGT_VO, MSGL_V, "[gl] Program statistics:\n");
1312 for (i = 0; progstats[i].name; i++) {
1313 mpglGetProgramiv(target, progstats[i].cur, &cur);
1314 mpglGetProgramiv(target, progstats[i].max, &max);
1315 mp_msg(MSGT_VO, MSGL_V, "[gl] %s: %i/%i\n", progstats[i].name, cur, max);
1317 return 1;
1320 #define MAX_PROGSZ (1024*1024)
1323 * \brief setup a fragment program that will do YUV->RGB conversion
1324 * \param parms struct containing parameters like conversion and scaler type,
1325 * brightness, ...
1327 static void glSetupYUVFragprog(gl_conversion_params_t *params) {
1328 int type = params->type;
1329 int texw = params->texw;
1330 int texh = params->texh;
1331 int rect = params->target == GL_TEXTURE_RECTANGLE;
1332 static const char prog_hdr[] =
1333 "!!ARBfp1.0\n"
1334 "OPTION ARB_precision_hint_fastest;\n"
1335 // all scaler variables must go here so they aren't defined
1336 // multiple times when the same scaler is used more than once
1337 "TEMP coord, coord2, cdelta, parmx, parmy, a, b, yuv;\n";
1338 int prog_remain;
1339 char *yuv_prog, *prog_pos;
1340 int cur_texu = 3;
1341 char lum_scale_texs[1];
1342 char chrom_scale_texs[1];
1343 char conv_texs[1];
1344 GLint i;
1345 // this is the conversion matrix, with y, u, v factors
1346 // for red, green, blue and the constant offsets
1347 float yuv2rgb[3][4];
1348 create_conv_textures(params, &cur_texu, conv_texs);
1349 create_scaler_textures(YUV_LUM_SCALER(type), &cur_texu, lum_scale_texs);
1350 if (YUV_CHROM_SCALER(type) == YUV_LUM_SCALER(type))
1351 memcpy(chrom_scale_texs, lum_scale_texs, sizeof(chrom_scale_texs));
1352 else
1353 create_scaler_textures(YUV_CHROM_SCALER(type), &cur_texu, chrom_scale_texs);
1354 mpglGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &i);
1355 if (i < cur_texu)
1356 mp_msg(MSGT_VO, MSGL_ERR,
1357 "[gl] %i texture units needed for this type of YUV fragment support (found %i)\n",
1358 cur_texu, i);
1359 if (!mpglProgramString) {
1360 mp_msg(MSGT_VO, MSGL_FATAL, "[gl] ProgramString function missing!\n");
1361 return;
1363 yuv_prog = malloc(MAX_PROGSZ);
1364 strcpy(yuv_prog, prog_hdr);
1365 prog_pos = yuv_prog + sizeof(prog_hdr) - 1;
1366 prog_remain = MAX_PROGSZ - sizeof(prog_hdr);
1367 add_scaler(YUV_LUM_SCALER(type), &prog_pos, &prog_remain, lum_scale_texs,
1368 '0', 'r', rect, texw, texh, params->filter_strength);
1369 add_scaler(YUV_CHROM_SCALER(type), &prog_pos, &prog_remain, chrom_scale_texs,
1370 '1', 'g', rect, params->chrom_texw, params->chrom_texh, params->filter_strength);
1371 add_scaler(YUV_CHROM_SCALER(type), &prog_pos, &prog_remain, chrom_scale_texs,
1372 '2', 'b', rect, params->chrom_texw, params->chrom_texh, params->filter_strength);
1373 mp_get_yuv2rgb_coeffs(&params->csp_params, yuv2rgb);
1374 switch (YUV_CONVERSION(type)) {
1375 case YUV_CONVERSION_FRAGMENT:
1376 snprintf(prog_pos, prog_remain, yuv_prog_template,
1377 yuv2rgb[ROW_R][COL_Y], yuv2rgb[ROW_G][COL_Y], yuv2rgb[ROW_B][COL_Y],
1378 yuv2rgb[ROW_R][COL_U], yuv2rgb[ROW_G][COL_U], yuv2rgb[ROW_B][COL_U],
1379 yuv2rgb[ROW_R][COL_V], yuv2rgb[ROW_G][COL_V], yuv2rgb[ROW_B][COL_V],
1380 yuv2rgb[ROW_R][COL_C], yuv2rgb[ROW_G][COL_C], yuv2rgb[ROW_B][COL_C]);
1381 break;
1382 case YUV_CONVERSION_FRAGMENT_POW:
1383 snprintf(prog_pos, prog_remain, yuv_pow_prog_template,
1384 yuv2rgb[ROW_R][COL_Y], yuv2rgb[ROW_G][COL_Y], yuv2rgb[ROW_B][COL_Y],
1385 yuv2rgb[ROW_R][COL_U], yuv2rgb[ROW_G][COL_U], yuv2rgb[ROW_B][COL_U],
1386 yuv2rgb[ROW_R][COL_V], yuv2rgb[ROW_G][COL_V], yuv2rgb[ROW_B][COL_V],
1387 yuv2rgb[ROW_R][COL_C], yuv2rgb[ROW_G][COL_C], yuv2rgb[ROW_B][COL_C],
1388 (float)1.0 / params->csp_params.rgamma, (float)1.0 / params->csp_params.bgamma, (float)1.0 / params->csp_params.bgamma);
1389 break;
1390 case YUV_CONVERSION_FRAGMENT_LOOKUP:
1391 snprintf(prog_pos, prog_remain, yuv_lookup_prog_template,
1392 yuv2rgb[ROW_R][COL_Y], yuv2rgb[ROW_G][COL_Y], yuv2rgb[ROW_B][COL_Y],
1393 yuv2rgb[ROW_R][COL_U], yuv2rgb[ROW_G][COL_U], yuv2rgb[ROW_B][COL_U],
1394 yuv2rgb[ROW_R][COL_V], yuv2rgb[ROW_G][COL_V], yuv2rgb[ROW_B][COL_V],
1395 yuv2rgb[ROW_R][COL_C], yuv2rgb[ROW_G][COL_C], yuv2rgb[ROW_B][COL_C],
1396 conv_texs[0], conv_texs[0], conv_texs[0]);
1397 break;
1398 case YUV_CONVERSION_FRAGMENT_LOOKUP3D:
1399 snprintf(prog_pos, prog_remain, yuv_lookup3d_prog_template, conv_texs[0]);
1400 break;
1401 default:
1402 mp_msg(MSGT_VO, MSGL_ERR, "[gl] unknown conversion type %i\n", YUV_CONVERSION(type));
1403 break;
1405 mp_msg(MSGT_VO, MSGL_DBG2, "[gl] generated fragment program:\n%s\n", yuv_prog);
1406 loadGPUProgram(GL_FRAGMENT_PROGRAM, yuv_prog);
1407 free(yuv_prog);
1411 * \brief detect the best YUV->RGB conversion method available
1413 int glAutodetectYUVConversion(void) {
1414 const char *extensions = mpglGetString(GL_EXTENSIONS);
1415 if (!extensions || !mpglMultiTexCoord2f)
1416 return YUV_CONVERSION_NONE;
1417 if (strstr(extensions, "GL_ARB_fragment_program"))
1418 return YUV_CONVERSION_FRAGMENT;
1419 if (strstr(extensions, "GL_ATI_text_fragment_shader"))
1420 return YUV_CONVERSION_TEXT_FRAGMENT;
1421 if (strstr(extensions, "GL_ATI_fragment_shader"))
1422 return YUV_CONVERSION_COMBINERS_ATI;
1423 return YUV_CONVERSION_NONE;
1427 * \brief setup YUV->RGB conversion
1428 * \param parms struct containing parameters like conversion and scaler type,
1429 * brightness, ...
1430 * \ingroup glconversion
1432 void glSetupYUVConversion(gl_conversion_params_t *params) {
1433 float uvcos = params->csp_params.saturation * cos(params->csp_params.hue);
1434 float uvsin = params->csp_params.saturation * sin(params->csp_params.hue);
1435 if (params->chrom_texw == 0) params->chrom_texw = 1;
1436 if (params->chrom_texh == 0) params->chrom_texh = 1;
1437 switch (YUV_CONVERSION(params->type)) {
1438 case YUV_CONVERSION_COMBINERS:
1439 glSetupYUVCombiners(uvcos, uvsin);
1440 break;
1441 case YUV_CONVERSION_COMBINERS_ATI:
1442 glSetupYUVFragmentATI(&params->csp_params, 0);
1443 break;
1444 case YUV_CONVERSION_TEXT_FRAGMENT:
1445 glSetupYUVFragmentATI(&params->csp_params, 1);
1446 break;
1447 case YUV_CONVERSION_FRAGMENT_LOOKUP:
1448 case YUV_CONVERSION_FRAGMENT_LOOKUP3D:
1449 case YUV_CONVERSION_FRAGMENT:
1450 case YUV_CONVERSION_FRAGMENT_POW:
1451 glSetupYUVFragprog(params);
1452 break;
1453 case YUV_CONVERSION_NONE:
1454 break;
1455 default:
1456 mp_msg(MSGT_VO, MSGL_ERR, "[gl] unknown conversion type %i\n", YUV_CONVERSION(params->type));
1461 * \brief enable the specified YUV conversion
1462 * \param target texture target for Y, U and V textures (e.g. GL_TEXTURE_2D)
1463 * \param type type of YUV conversion
1464 * \ingroup glconversion
1466 void glEnableYUVConversion(GLenum target, int type) {
1467 switch (YUV_CONVERSION(type)) {
1468 case YUV_CONVERSION_COMBINERS:
1469 mpglActiveTexture(GL_TEXTURE1);
1470 mpglEnable(target);
1471 mpglActiveTexture(GL_TEXTURE2);
1472 mpglEnable(target);
1473 mpglActiveTexture(GL_TEXTURE0);
1474 mpglEnable(GL_REGISTER_COMBINERS_NV);
1475 break;
1476 case YUV_CONVERSION_COMBINERS_ATI:
1477 mpglActiveTexture(GL_TEXTURE1);
1478 mpglEnable(target);
1479 mpglActiveTexture(GL_TEXTURE2);
1480 mpglEnable(target);
1481 mpglActiveTexture(GL_TEXTURE0);
1482 mpglEnable(GL_FRAGMENT_SHADER_ATI);
1483 break;
1484 case YUV_CONVERSION_TEXT_FRAGMENT:
1485 mpglActiveTexture(GL_TEXTURE1);
1486 mpglEnable(target);
1487 mpglActiveTexture(GL_TEXTURE2);
1488 mpglEnable(target);
1489 mpglActiveTexture(GL_TEXTURE0);
1490 mpglEnable(GL_TEXT_FRAGMENT_SHADER_ATI);
1491 break;
1492 case YUV_CONVERSION_FRAGMENT_LOOKUP3D:
1493 case YUV_CONVERSION_FRAGMENT_LOOKUP:
1494 case YUV_CONVERSION_FRAGMENT_POW:
1495 case YUV_CONVERSION_FRAGMENT:
1496 case YUV_CONVERSION_NONE:
1497 mpglEnable(GL_FRAGMENT_PROGRAM);
1498 break;
1503 * \brief disable the specified YUV conversion
1504 * \param target texture target for Y, U and V textures (e.g. GL_TEXTURE_2D)
1505 * \param type type of YUV conversion
1506 * \ingroup glconversion
1508 void glDisableYUVConversion(GLenum target, int type) {
1509 switch (YUV_CONVERSION(type)) {
1510 case YUV_CONVERSION_COMBINERS:
1511 mpglActiveTexture(GL_TEXTURE1);
1512 mpglDisable(target);
1513 mpglActiveTexture(GL_TEXTURE2);
1514 mpglDisable(target);
1515 mpglActiveTexture(GL_TEXTURE0);
1516 mpglDisable(GL_REGISTER_COMBINERS_NV);
1517 break;
1518 case YUV_CONVERSION_COMBINERS_ATI:
1519 mpglActiveTexture(GL_TEXTURE1);
1520 mpglDisable(target);
1521 mpglActiveTexture(GL_TEXTURE2);
1522 mpglDisable(target);
1523 mpglActiveTexture(GL_TEXTURE0);
1524 mpglDisable(GL_FRAGMENT_SHADER_ATI);
1525 break;
1526 case YUV_CONVERSION_TEXT_FRAGMENT:
1527 mpglDisable(GL_TEXT_FRAGMENT_SHADER_ATI);
1528 // HACK: at least the Mac OS X 10.5 PPC Radeon drivers are broken and
1529 // without this disable the texture units while the program is still
1530 // running (10.4 PPC seems to work without this though).
1531 mpglFlush();
1532 mpglActiveTexture(GL_TEXTURE1);
1533 mpglDisable(target);
1534 mpglActiveTexture(GL_TEXTURE2);
1535 mpglDisable(target);
1536 mpglActiveTexture(GL_TEXTURE0);
1537 break;
1538 case YUV_CONVERSION_FRAGMENT_LOOKUP3D:
1539 case YUV_CONVERSION_FRAGMENT_LOOKUP:
1540 case YUV_CONVERSION_FRAGMENT_POW:
1541 case YUV_CONVERSION_FRAGMENT:
1542 case YUV_CONVERSION_NONE:
1543 mpglDisable(GL_FRAGMENT_PROGRAM);
1544 break;
1548 void glEnable3DLeft(int type) {
1549 GLint buffer;
1550 switch (type) {
1551 case GL_3D_RED_CYAN:
1552 mpglColorMask(GL_TRUE, GL_FALSE, GL_FALSE, GL_FALSE);
1553 break;
1554 case GL_3D_GREEN_MAGENTA:
1555 mpglColorMask(GL_FALSE, GL_TRUE, GL_FALSE, GL_FALSE);
1556 break;
1557 case GL_3D_QUADBUFFER:
1558 mpglGetIntegerv(GL_DRAW_BUFFER, &buffer);
1559 switch (buffer) {
1560 case GL_FRONT:
1561 case GL_FRONT_LEFT:
1562 case GL_FRONT_RIGHT:
1563 buffer = GL_FRONT_LEFT;
1564 break;
1565 case GL_BACK:
1566 case GL_BACK_LEFT:
1567 case GL_BACK_RIGHT:
1568 buffer = GL_BACK_LEFT;
1569 break;
1571 mpglDrawBuffer(buffer);
1572 break;
1576 void glEnable3DRight(int type) {
1577 GLint buffer;
1578 switch (type) {
1579 case GL_3D_RED_CYAN:
1580 mpglColorMask(GL_FALSE, GL_TRUE, GL_TRUE, GL_FALSE);
1581 break;
1582 case GL_3D_GREEN_MAGENTA:
1583 mpglColorMask(GL_TRUE, GL_FALSE, GL_TRUE, GL_FALSE);
1584 break;
1585 case GL_3D_QUADBUFFER:
1586 mpglGetIntegerv(GL_DRAW_BUFFER, &buffer);
1587 switch (buffer) {
1588 case GL_FRONT:
1589 case GL_FRONT_LEFT:
1590 case GL_FRONT_RIGHT:
1591 buffer = GL_FRONT_RIGHT;
1592 break;
1593 case GL_BACK:
1594 case GL_BACK_LEFT:
1595 case GL_BACK_RIGHT:
1596 buffer = GL_BACK_RIGHT;
1597 break;
1599 mpglDrawBuffer(buffer);
1600 break;
1604 void glDisable3D(int type) {
1605 GLint buffer;
1606 switch (type) {
1607 case GL_3D_RED_CYAN:
1608 case GL_3D_GREEN_MAGENTA:
1609 mpglColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
1610 break;
1611 case GL_3D_QUADBUFFER:
1612 mpglDrawBuffer(vo_doublebuffering ? GL_BACK : GL_FRONT);
1613 mpglGetIntegerv(GL_DRAW_BUFFER, &buffer);
1614 switch (buffer) {
1615 case GL_FRONT:
1616 case GL_FRONT_LEFT:
1617 case GL_FRONT_RIGHT:
1618 buffer = GL_FRONT;
1619 break;
1620 case GL_BACK:
1621 case GL_BACK_LEFT:
1622 case GL_BACK_RIGHT:
1623 buffer = GL_BACK;
1624 break;
1626 mpglDrawBuffer(buffer);
1627 break;
1632 * \brief draw a texture part at given 2D coordinates
1633 * \param x screen top coordinate
1634 * \param y screen left coordinate
1635 * \param w screen width coordinate
1636 * \param h screen height coordinate
1637 * \param tx texture top coordinate in pixels
1638 * \param ty texture left coordinate in pixels
1639 * \param tw texture part width in pixels
1640 * \param th texture part height in pixels
1641 * \param sx width of texture in pixels
1642 * \param sy height of texture in pixels
1643 * \param rect_tex whether this texture uses texture_rectangle extension
1644 * \param is_yv12 if != 0, also draw the textures from units 1 and 2,
1645 * bits 8 - 15 and 16 - 23 specify the x and y scaling of those textures
1646 * \param flip flip the texture upside down
1647 * \ingroup gltexture
1649 void glDrawTex(GLfloat x, GLfloat y, GLfloat w, GLfloat h,
1650 GLfloat tx, GLfloat ty, GLfloat tw, GLfloat th,
1651 int sx, int sy, int rect_tex, int is_yv12, int flip) {
1652 int chroma_x_shift = (is_yv12 >> 8) & 31;
1653 int chroma_y_shift = (is_yv12 >> 16) & 31;
1654 GLfloat xscale = 1 << chroma_x_shift;
1655 GLfloat yscale = 1 << chroma_y_shift;
1656 GLfloat tx2 = tx / xscale, ty2 = ty / yscale, tw2 = tw / xscale, th2 = th / yscale;
1657 if (!rect_tex) {
1658 tx /= sx; ty /= sy; tw /= sx; th /= sy;
1659 tx2 = tx, ty2 = ty, tw2 = tw, th2 = th;
1661 if (flip) {
1662 y += h;
1663 h = -h;
1665 mpglBegin(GL_QUADS);
1666 mpglTexCoord2f(tx, ty);
1667 if (is_yv12) {
1668 mpglMultiTexCoord2f(GL_TEXTURE1, tx2, ty2);
1669 mpglMultiTexCoord2f(GL_TEXTURE2, tx2, ty2);
1671 mpglVertex2f(x, y);
1672 mpglTexCoord2f(tx, ty + th);
1673 if (is_yv12) {
1674 mpglMultiTexCoord2f(GL_TEXTURE1, tx2, ty2 + th2);
1675 mpglMultiTexCoord2f(GL_TEXTURE2, tx2, ty2 + th2);
1677 mpglVertex2f(x, y + h);
1678 mpglTexCoord2f(tx + tw, ty + th);
1679 if (is_yv12) {
1680 mpglMultiTexCoord2f(GL_TEXTURE1, tx2 + tw2, ty2 + th2);
1681 mpglMultiTexCoord2f(GL_TEXTURE2, tx2 + tw2, ty2 + th2);
1683 mpglVertex2f(x + w, y + h);
1684 mpglTexCoord2f(tx + tw, ty);
1685 if (is_yv12) {
1686 mpglMultiTexCoord2f(GL_TEXTURE1, tx2 + tw2, ty2);
1687 mpglMultiTexCoord2f(GL_TEXTURE2, tx2 + tw2, ty2);
1689 mpglVertex2f(x + w, y);
1690 mpglEnd();
1693 #ifdef CONFIG_GL_WIN32
1694 #include "w32_common.h"
1696 * \brief little helper since wglGetProcAddress definition does not fit our
1697 * getProcAddress
1698 * \param procName name of function to look up
1699 * \return function pointer returned by wglGetProcAddress
1701 static void *w32gpa(const GLubyte *procName) {
1702 HMODULE oglmod;
1703 void *res = wglGetProcAddress(procName);
1704 if (res) return res;
1705 oglmod = GetModuleHandle("opengl32.dll");
1706 return GetProcAddress(oglmod, procName);
1709 static int setGlWindow_w32(MPGLContext *ctx)
1711 HWND win = vo_w32_window;
1712 int *vinfo = &ctx->vinfo.w32;
1713 HGLRC *context = &ctx->context.w32;
1714 int new_vinfo;
1715 HDC windc = vo_w32_get_dc(win);
1716 HGLRC new_context = 0;
1717 int keep_context = 0;
1718 int res = SET_WINDOW_FAILED;
1720 // should only be needed when keeping context, but not doing glFinish
1721 // can cause flickering even when we do not keep it.
1722 if (*context)
1723 mpglFinish();
1724 new_vinfo = GetPixelFormat(windc);
1725 if (*context && *vinfo && new_vinfo && *vinfo == new_vinfo) {
1726 // we can keep the wglContext
1727 new_context = *context;
1728 keep_context = 1;
1729 } else {
1730 // create a context
1731 new_context = wglCreateContext(windc);
1732 if (!new_context) {
1733 mp_msg(MSGT_VO, MSGL_FATAL, "[gl] Could not create GL context!\n");
1734 goto out;
1738 // set context
1739 if (!wglMakeCurrent(windc, new_context)) {
1740 mp_msg(MSGT_VO, MSGL_FATAL, "[gl] Could not set GL context!\n");
1741 if (!keep_context) {
1742 wglDeleteContext(new_context);
1744 goto out;
1747 // set new values
1748 vo_w32_window = win;
1750 RECT rect;
1751 GetClientRect(win, &rect);
1752 vo_dwidth = rect.right;
1753 vo_dheight = rect.bottom;
1755 if (!keep_context) {
1756 if (*context)
1757 wglDeleteContext(*context);
1758 *context = new_context;
1759 *vinfo = new_vinfo;
1760 getFunctions(w32gpa, NULL);
1762 // and inform that reinit is neccessary
1763 res = SET_WINDOW_REINIT;
1764 } else
1765 res = SET_WINDOW_OK;
1767 out:
1768 vo_w32_release_dc(win, windc);
1769 return res;
1772 static void releaseGlContext_w32(MPGLContext *ctx) {
1773 int *vinfo = &ctx->vinfo.w32;
1774 HGLRC *context = &ctx->context.w32;
1775 *vinfo = 0;
1776 if (*context) {
1777 wglMakeCurrent(0, 0);
1778 wglDeleteContext(*context);
1780 *context = 0;
1783 static void swapGlBuffers_w32(MPGLContext *ctx) {
1784 HDC vo_hdc = vo_w32_get_dc(vo_w32_window);
1785 SwapBuffers(vo_hdc);
1786 vo_w32_release_dc(vo_w32_window, vo_hdc);
1788 #endif
1789 #ifdef CONFIG_GL_X11
1790 #include "x11_common.h"
1793 * \brief Returns the XVisualInfo associated with Window win.
1794 * \param win Window whose XVisualInfo is returne.
1795 * \return XVisualInfo of the window. Caller must use XFree to free it.
1797 static XVisualInfo *getWindowVisualInfo(Window win) {
1798 XWindowAttributes xw_attr;
1799 XVisualInfo vinfo_template;
1800 int tmp;
1801 XGetWindowAttributes(mDisplay, win, &xw_attr);
1802 vinfo_template.visualid = XVisualIDFromVisual(xw_attr.visual);
1803 return XGetVisualInfo(mDisplay, VisualIDMask, &vinfo_template, &tmp);
1806 static void appendstr(char **dst, const char *str)
1808 int newsize;
1809 char *newstr;
1810 if (!str)
1811 return;
1812 newsize = strlen(*dst) + 1 + strlen(str) + 1;
1813 newstr = realloc(*dst, newsize);
1814 if (!newstr)
1815 return;
1816 *dst = newstr;
1817 strcat(*dst, " ");
1818 strcat(*dst, str);
1822 * \brief Changes the window in which video is displayed.
1823 * If possible only transfers the context to the new window, otherwise
1824 * creates a new one, which must be initialized by the caller.
1825 * \param vinfo Currently used visual.
1826 * \param context Currently used context.
1827 * \param win window that should be used for drawing.
1828 * \return one of SET_WINDOW_FAILED, SET_WINDOW_OK or SET_WINDOW_REINIT.
1829 * In case of SET_WINDOW_REINIT the context could not be transfered
1830 * and the caller must initialize it correctly.
1831 * \ingroup glcontext
1833 static int setGlWindow_x11(MPGLContext *ctx)
1835 XVisualInfo **vinfo = &ctx->vinfo.x11;
1836 GLXContext *context = &ctx->context.x11;
1837 Window win = vo_window;
1838 XVisualInfo *new_vinfo;
1839 GLXContext new_context = NULL;
1840 int keep_context = 0;
1842 // should only be needed when keeping context, but not doing glFinish
1843 // can cause flickering even when we do not keep it.
1844 if (*context)
1845 mpglFinish();
1846 new_vinfo = getWindowVisualInfo(win);
1847 if (*context && *vinfo && new_vinfo &&
1848 (*vinfo)->visualid == new_vinfo->visualid) {
1849 // we can keep the GLXContext
1850 new_context = *context;
1851 XFree(new_vinfo);
1852 new_vinfo = *vinfo;
1853 keep_context = 1;
1854 } else {
1855 // create a context
1856 new_context = glXCreateContext(mDisplay, new_vinfo, NULL, True);
1857 if (!new_context) {
1858 mp_msg(MSGT_VO, MSGL_FATAL, "[gl] Could not create GLX context!\n");
1859 XFree(new_vinfo);
1860 return SET_WINDOW_FAILED;
1864 // set context
1865 if (!glXMakeCurrent(mDisplay, vo_window, new_context)) {
1866 mp_msg(MSGT_VO, MSGL_FATAL, "[gl] Could not set GLX context!\n");
1867 if (!keep_context) {
1868 glXDestroyContext(mDisplay, new_context);
1869 XFree(new_vinfo);
1871 return SET_WINDOW_FAILED;
1874 // set new values
1875 vo_window = win;
1876 vo_x11_update_geometry();
1877 if (!keep_context) {
1878 void *(*getProcAddress)(const GLubyte *);
1879 const char *(*glXExtStr)(Display *, int);
1880 char *glxstr = strdup("");
1881 if (*context)
1882 glXDestroyContext(mDisplay, *context);
1883 *context = new_context;
1884 if (*vinfo)
1885 XFree(*vinfo);
1886 *vinfo = new_vinfo;
1887 getProcAddress = getdladdr("glXGetProcAddress");
1888 if (!getProcAddress)
1889 getProcAddress = getdladdr("glXGetProcAddressARB");
1890 glXExtStr = getdladdr("glXQueryExtensionsString");
1891 if (glXExtStr)
1892 appendstr(&glxstr, glXExtStr(mDisplay, DefaultScreen(mDisplay)));
1893 glXExtStr = getdladdr("glXGetClientString");
1894 if (glXExtStr)
1895 appendstr(&glxstr, glXExtStr(mDisplay, GLX_EXTENSIONS));
1896 glXExtStr = getdladdr("glXGetServerString");
1897 if (glXExtStr)
1898 appendstr(&glxstr, glXExtStr(mDisplay, GLX_EXTENSIONS));
1900 getFunctions(getProcAddress, glxstr);
1901 if (!mpglGenPrograms && mpglGetString &&
1902 getProcAddress &&
1903 strstr(mpglGetString(GL_EXTENSIONS), "GL_ARB_vertex_program")) {
1904 mp_msg(MSGT_VO, MSGL_WARN, "Broken glXGetProcAddress detected, trying workaround\n");
1905 getFunctions(NULL, glxstr);
1907 free(glxstr);
1909 // and inform that reinit is neccessary
1910 return SET_WINDOW_REINIT;
1912 return SET_WINDOW_OK;
1916 * \brief free the VisualInfo and GLXContext of an OpenGL context.
1917 * \ingroup glcontext
1919 static void releaseGlContext_x11(MPGLContext *ctx) {
1920 XVisualInfo **vinfo = &ctx->vinfo.x11;
1921 GLXContext *context = &ctx->context.x11;
1922 if (*vinfo)
1923 XFree(*vinfo);
1924 *vinfo = NULL;
1925 if (*context)
1927 mpglFinish();
1928 glXMakeCurrent(mDisplay, None, NULL);
1929 glXDestroyContext(mDisplay, *context);
1931 *context = 0;
1934 static void swapGlBuffers_x11(MPGLContext *ctx) {
1935 glXSwapBuffers(mDisplay, vo_window);
1938 static int x11_check_events(void) {
1939 return vo_x11_check_events(mDisplay);
1942 static void gl_update_xinerama_info(void)
1944 update_xinerama_info(global_vo);
1947 static void gl_border(void)
1949 vo_x11_border();
1952 static void gl_fullscreen(void)
1954 vo_x11_fullscreen();
1957 static void gl_ontop(void)
1959 vo_x11_ontop();
1961 #endif
1963 #ifdef CONFIG_GL_SDL
1964 #include "sdl_common.h"
1966 static void swapGlBuffers_sdl(MPGLContext *ctx) {
1967 SDL_GL_SwapBuffers();
1970 static void *sdlgpa(const GLubyte *name) {
1971 return SDL_GL_GetProcAddress(name);
1974 static int setGlWindow_sdl(MPGLContext *ctx) {
1975 if (sdl_set_mode(0, SDL_OPENGL | SDL_RESIZABLE) < 0)
1976 return SET_WINDOW_FAILED;
1977 SDL_GL_LoadLibrary(NULL);
1978 getFunctions(sdlgpa, NULL);
1979 return SET_WINDOW_OK;
1982 static int sdl_check_events(void) {
1983 int res = 0;
1984 SDL_Event event;
1985 while (SDL_PollEvent(&event)) {
1986 res |= sdl_default_handle_event(&event);
1988 // poll "events" from within MPlayer code
1989 res |= sdl_default_handle_event(NULL);
1990 if (res & VO_EVENT_RESIZE)
1991 sdl_set_mode(0, SDL_OPENGL | SDL_RESIZABLE);
1992 return res;
1995 #endif
1997 static int setGlWindow_dummy(MPGLContext *ctx) {
1998 getFunctions(NULL, NULL);
1999 return SET_WINDOW_OK;
2002 static void releaseGlContext_dummy(MPGLContext *ctx) {
2005 static int dummy_check_events(void) {
2006 return 0;
2009 static void dummy_update_xinerama_info(void) {
2010 if (vo_screenwidth <= 0 || vo_screenheight <= 0) {
2011 mp_msg(MSGT_VO, MSGL_ERR, "You must specify the screen dimensions "
2012 "with -screenw and -screenh\n");
2013 vo_screenwidth = 1280;
2014 vo_screenheight = 768;
2016 aspect_save_screenres(vo_screenwidth, vo_screenheight);
2019 int init_mpglcontext(MPGLContext *ctx, enum MPGLType type) {
2020 if (type == GLTYPE_AUTO) {
2021 int res = init_mpglcontext(ctx, GLTYPE_W32);
2022 if (res) return res;
2023 res = init_mpglcontext(ctx, GLTYPE_X11);
2024 if (res) return res;
2025 res = init_mpglcontext(ctx, GLTYPE_SDL);
2026 return res;
2028 memset(ctx, 0, sizeof(*ctx));
2029 ctx->setGlWindow = setGlWindow_dummy;
2030 ctx->releaseGlContext = releaseGlContext_dummy;
2031 ctx->update_xinerama_info = dummy_update_xinerama_info;
2032 ctx->check_events = dummy_check_events;
2033 ctx->type = type;
2034 switch (ctx->type) {
2035 #ifdef CONFIG_GL_WIN32
2036 case GLTYPE_W32:
2037 ctx->setGlWindow = setGlWindow_w32;
2038 ctx->releaseGlContext = releaseGlContext_w32;
2039 ctx->swapGlBuffers = swapGlBuffers_w32;
2040 ctx->update_xinerama_info = w32_update_xinerama_info;
2041 ctx->border = vo_w32_border;
2042 ctx->check_events = vo_w32_check_events;
2043 ctx->fullscreen = vo_w32_fullscreen;
2044 ctx->ontop = vo_w32_ontop;
2045 return vo_w32_init();
2046 #endif
2047 #ifdef CONFIG_GL_X11
2048 case GLTYPE_X11:
2049 ctx->setGlWindow = setGlWindow_x11;
2050 ctx->releaseGlContext = releaseGlContext_x11;
2051 ctx->swapGlBuffers = swapGlBuffers_x11;
2052 ctx->update_xinerama_info = gl_update_xinerama_info;
2053 ctx->border = gl_border;
2054 ctx->check_events = x11_check_events;
2055 ctx->fullscreen = gl_fullscreen;
2056 ctx->ontop = gl_ontop;
2057 return vo_init();
2058 #endif
2059 #ifdef CONFIG_GL_SDL
2060 case GLTYPE_SDL:
2061 SDL_Init(SDL_INIT_VIDEO);
2062 ctx->setGlWindow = setGlWindow_sdl;
2063 ctx->swapGlBuffers = swapGlBuffers_sdl;
2064 ctx->check_events = sdl_check_events;
2065 ctx->fullscreen = vo_sdl_fullscreen;
2066 return vo_sdl_init();
2067 #endif
2068 default:
2069 return 0;
2073 void uninit_mpglcontext(MPGLContext *ctx) {
2074 ctx->releaseGlContext(ctx);
2075 switch (ctx->type) {
2076 #ifdef CONFIG_GL_WIN32
2077 case GLTYPE_W32:
2078 vo_w32_uninit();
2079 break;
2080 #endif
2081 #ifdef CONFIG_GL_X11
2082 case GLTYPE_X11:
2083 vo_x11_uninit();
2084 break;
2085 #endif
2086 #ifdef CONFIG_GL_SDL
2087 case GLTYPE_SDL:
2088 vo_sdl_uninit();
2089 break;
2090 #endif
2092 memset(ctx, 0, sizeof(*ctx));