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