cleanup: remove NULL checks before free() all over the code
[mplayer/glamo.git] / libvo / gl_common.c
blobad6fc99a64f685fe15a9c64c1fb2eb58598758ca
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),
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)) {
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 = 1;
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 case IMGFMT_YUY2:
301 *gl_texfmt = GL_YCBCR_MESA;
302 *bpp = 16;
303 *gl_format = GL_YCBCR_MESA;
304 *gl_type = fmt == IMGFMT_UYVY ? GL_UNSIGNED_SHORT_8_8 : GL_UNSIGNED_SHORT_8_8_REV;
305 break;
306 #if 0
307 // we do not support palettized formats, although the format the
308 // swscale produces works
309 case IMGFMT_RGB8:
310 gl_format = GL_RGB;
311 gl_type = GL_UNSIGNED_BYTE_2_3_3_REV;
312 break;
313 #endif
314 case IMGFMT_RGB15:
315 *gl_format = GL_RGBA;
316 *gl_type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
317 break;
318 case IMGFMT_RGB16:
319 *gl_format = GL_RGB;
320 *gl_type = GL_UNSIGNED_SHORT_5_6_5_REV;
321 break;
322 #if 0
323 case IMGFMT_BGR8:
324 // special case as red and blue have a differen number of bits.
325 // GL_BGR and GL_UNSIGNED_BYTE_3_3_2 isn't supported at least
326 // by nVidia drivers, and in addition would give more bits to
327 // blue than to red, which isn't wanted
328 gl_format = GL_RGB;
329 gl_type = GL_UNSIGNED_BYTE_3_3_2;
330 break;
331 #endif
332 case IMGFMT_BGR15:
333 *gl_format = GL_BGRA;
334 *gl_type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
335 break;
336 case IMGFMT_BGR16:
337 *gl_format = GL_RGB;
338 *gl_type = GL_UNSIGNED_SHORT_5_6_5;
339 break;
340 case IMGFMT_BGR24:
341 *gl_format = GL_BGR;
342 *gl_type = GL_UNSIGNED_BYTE;
343 break;
344 case IMGFMT_BGRA:
345 *gl_texfmt = 4;
346 *gl_format = GL_BGRA;
347 *gl_type = GL_UNSIGNED_BYTE;
348 break;
349 default:
350 *gl_texfmt = 4;
351 *gl_format = GL_RGBA;
352 *gl_type = GL_UNSIGNED_BYTE;
353 supported = 0;
355 #ifdef TEXTUREFORMAT_ALWAYS
356 *gl_texfmt = TEXTUREFORMAT_ALWAYS;
357 #endif
358 return supported;
361 #ifdef HAVE_LIBDL
362 #include <dlfcn.h>
363 #endif
365 * \brief find address of a linked function
366 * \param s name of function to find
367 * \return address of function or NULL if not found
369 static void *getdladdr(const char *s) {
370 void *ret = NULL;
371 #ifdef HAVE_LIBDL
372 void *handle = dlopen(NULL, RTLD_LAZY);
373 if (!handle)
374 return NULL;
375 ret = dlsym(handle, s);
376 dlclose(handle);
377 #endif
378 return ret;
381 typedef struct {
382 void *funcptr;
383 const char *extstr;
384 const char *funcnames[7];
385 void *fallback;
386 } extfunc_desc_t;
388 #define DEF_FUNC_DESC(name) {&mpgl##name, NULL, {"gl"#name, NULL}, gl ##name}
389 static const extfunc_desc_t extfuncs[] = {
390 // these aren't extension functions but we query them anyway to allow
391 // different "backends" with one binary
392 DEF_FUNC_DESC(Begin),
393 DEF_FUNC_DESC(End),
394 DEF_FUNC_DESC(Viewport),
395 DEF_FUNC_DESC(MatrixMode),
396 DEF_FUNC_DESC(LoadIdentity),
397 DEF_FUNC_DESC(Translated),
398 DEF_FUNC_DESC(Scaled),
399 DEF_FUNC_DESC(Ortho),
400 DEF_FUNC_DESC(Frustum),
401 DEF_FUNC_DESC(PushMatrix),
402 DEF_FUNC_DESC(PopMatrix),
403 DEF_FUNC_DESC(Clear),
404 DEF_FUNC_DESC(GenLists),
405 DEF_FUNC_DESC(DeleteLists),
406 DEF_FUNC_DESC(NewList),
407 DEF_FUNC_DESC(EndList),
408 DEF_FUNC_DESC(CallList),
409 DEF_FUNC_DESC(CallLists),
410 DEF_FUNC_DESC(GenTextures),
411 DEF_FUNC_DESC(DeleteTextures),
412 DEF_FUNC_DESC(TexEnvf),
413 DEF_FUNC_DESC(TexEnvi),
414 DEF_FUNC_DESC(Color4ub),
415 DEF_FUNC_DESC(Color3f),
416 DEF_FUNC_DESC(Color4f),
417 DEF_FUNC_DESC(ClearColor),
418 DEF_FUNC_DESC(ClearDepth),
419 DEF_FUNC_DESC(DepthFunc),
420 DEF_FUNC_DESC(Enable),
421 DEF_FUNC_DESC(Disable),
422 DEF_FUNC_DESC(DrawBuffer),
423 DEF_FUNC_DESC(DepthMask),
424 DEF_FUNC_DESC(BlendFunc),
425 DEF_FUNC_DESC(Flush),
426 DEF_FUNC_DESC(Finish),
427 DEF_FUNC_DESC(PixelStorei),
428 DEF_FUNC_DESC(TexImage1D),
429 DEF_FUNC_DESC(TexImage2D),
430 DEF_FUNC_DESC(TexSubImage2D),
431 DEF_FUNC_DESC(TexParameteri),
432 DEF_FUNC_DESC(TexParameterf),
433 DEF_FUNC_DESC(TexParameterfv),
434 DEF_FUNC_DESC(TexCoord2f),
435 DEF_FUNC_DESC(Vertex2f),
436 DEF_FUNC_DESC(Vertex3f),
437 DEF_FUNC_DESC(Normal3f),
438 DEF_FUNC_DESC(Lightfv),
439 DEF_FUNC_DESC(ColorMaterial),
440 DEF_FUNC_DESC(ShadeModel),
441 DEF_FUNC_DESC(GetIntegerv),
442 DEF_FUNC_DESC(ColorMask),
444 // here start the real extensions
445 {&mpglGenBuffers, NULL, {"glGenBuffers", "glGenBuffersARB", NULL}},
446 {&mpglDeleteBuffers, NULL, {"glDeleteBuffers", "glDeleteBuffersARB", NULL}},
447 {&mpglBindBuffer, NULL, {"glBindBuffer", "glBindBufferARB", NULL}},
448 {&mpglMapBuffer, NULL, {"glMapBuffer", "glMapBufferARB", NULL}},
449 {&mpglUnmapBuffer, NULL, {"glUnmapBuffer", "glUnmapBufferARB", NULL}},
450 {&mpglBufferData, NULL, {"glBufferData", "glBufferDataARB", NULL}},
451 {&mpglCombinerParameterfv, "NV_register_combiners", {"glCombinerParameterfv", "glCombinerParameterfvNV", NULL}},
452 {&mpglCombinerParameteri, "NV_register_combiners", {"glCombinerParameteri", "glCombinerParameteriNV", NULL}},
453 {&mpglCombinerInput, "NV_register_combiners", {"glCombinerInput", "glCombinerInputNV", NULL}},
454 {&mpglCombinerOutput, "NV_register_combiners", {"glCombinerOutput", "glCombinerOutputNV", NULL}},
455 {&mpglBeginFragmentShader, "ATI_fragment_shader", {"glBeginFragmentShaderATI", NULL}},
456 {&mpglEndFragmentShader, "ATI_fragment_shader", {"glEndFragmentShaderATI", NULL}},
457 {&mpglSampleMap, "ATI_fragment_shader", {"glSampleMapATI", NULL}},
458 {&mpglColorFragmentOp2, "ATI_fragment_shader", {"glColorFragmentOp2ATI", NULL}},
459 {&mpglColorFragmentOp3, "ATI_fragment_shader", {"glColorFragmentOp3ATI", NULL}},
460 {&mpglSetFragmentShaderConstant, "ATI_fragment_shader", {"glSetFragmentShaderConstantATI", NULL}},
461 {&mpglActiveTexture, NULL, {"glActiveTexture", "glActiveTextureARB", NULL}},
462 {&mpglBindTexture, NULL, {"glBindTexture", "glBindTextureARB", "glBindTextureEXT", NULL}},
463 {&mpglMultiTexCoord2f, NULL, {"glMultiTexCoord2f", "glMultiTexCoord2fARB", NULL}},
464 {&mpglGenPrograms, "_program", {"glGenProgramsARB", NULL}},
465 {&mpglDeletePrograms, "_program", {"glDeleteProgramsARB", NULL}},
466 {&mpglBindProgram, "_program", {"glBindProgramARB", NULL}},
467 {&mpglProgramString, "_program", {"glProgramStringARB", NULL}},
468 {&mpglGetProgramiv, "_program", {"glGetProgramivARB", NULL}},
469 {&mpglProgramEnvParameter4f, "_program", {"glProgramEnvParameter4fARB", NULL}},
470 {&mpglSwapInterval, "_swap_control", {"glXSwapIntervalSGI", "glXSwapInterval", "wglSwapIntervalSGI", "wglSwapInterval", "wglSwapIntervalEXT", NULL}},
471 {&mpglTexImage3D, NULL, {"glTexImage3D", NULL}},
472 {&mpglAllocateMemoryMESA, "GLX_MESA_allocate_memory", {"glXAllocateMemoryMESA", NULL}},
473 {&mpglFreeMemoryMESA, "GLX_MESA_allocate_memory", {"glXFreeMemoryMESA", NULL}},
474 {NULL}
478 * \brief find the function pointers of some useful OpenGL extensions
479 * \param getProcAddress function to resolve function names, may be NULL
480 * \param ext2 an extra extension string
482 static void getFunctions(void *(*getProcAddress)(const GLubyte *),
483 const char *ext2) {
484 const extfunc_desc_t *dsc;
485 const char *extensions;
486 char *allexts;
488 if (!getProcAddress)
489 getProcAddress = (void *)getdladdr;
491 // special case, we need glGetString before starting to find the other functions
492 mpglGetString = getProcAddress("glGetString");
493 if (!mpglGetString)
494 mpglGetString = glGetString;
496 extensions = (const char *)mpglGetString(GL_EXTENSIONS);
497 if (!extensions) extensions = "";
498 if (!ext2) ext2 = "";
499 allexts = malloc(strlen(extensions) + strlen(ext2) + 2);
500 strcpy(allexts, extensions);
501 strcat(allexts, " ");
502 strcat(allexts, ext2);
503 mp_msg(MSGT_VO, MSGL_DBG2, "OpenGL extensions string:\n%s\n", allexts);
504 for (dsc = extfuncs; dsc->funcptr; dsc++) {
505 void *ptr = NULL;
506 int i;
507 if (!dsc->extstr || strstr(allexts, dsc->extstr)) {
508 for (i = 0; !ptr && dsc->funcnames[i]; i++)
509 ptr = getProcAddress((const GLubyte *)dsc->funcnames[i]);
511 if (!ptr)
512 ptr = dsc->fallback;
513 *(void **)dsc->funcptr = ptr;
515 if (strstr(allexts, "_texture_float"))
516 hqtexfmt = GL_RGB32F;
517 else if (strstr(allexts, "NV_float_buffer"))
518 hqtexfmt = GL_FLOAT_RGB32_NV;
519 else
520 hqtexfmt = GL_RGB16;
521 free(allexts);
525 * \brief create a texture and set some defaults
526 * \param target texture taget, usually GL_TEXTURE_2D
527 * \param fmt internal texture format
528 * \param format texture host data format
529 * \param type texture host data type
530 * \param filter filter used for scaling, e.g. GL_LINEAR
531 * \param w texture width
532 * \param h texture height
533 * \param val luminance value to fill texture with
534 * \ingroup gltexture
536 void glCreateClearTex(GLenum target, GLenum fmt, GLenum format, GLenum type, GLint filter,
537 int w, int h, unsigned char val) {
538 GLfloat fval = (GLfloat)val / 255.0;
539 GLfloat border[4] = {fval, fval, fval, fval};
540 int stride;
541 char *init;
542 if (w == 0) w = 1;
543 if (h == 0) h = 1;
544 stride = w * glFmt2bpp(format, type);
545 if (!stride) return;
546 init = malloc(stride * h);
547 memset(init, val, stride * h);
548 glAdjustAlignment(stride);
549 mpglPixelStorei(GL_UNPACK_ROW_LENGTH, w);
550 mpglTexImage2D(target, 0, fmt, w, h, 0, format, type, init);
551 mpglTexParameterf(target, GL_TEXTURE_PRIORITY, 1.0);
552 mpglTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
553 mpglTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
554 mpglTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
555 mpglTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
556 // Border texels should not be used with CLAMP_TO_EDGE
557 // We set a sane default anyway.
558 mpglTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, border);
559 free(init);
563 * \brief creates a texture from a PPM file
564 * \param target texture taget, usually GL_TEXTURE_2D
565 * \param fmt internal texture format, 0 for default
566 * \param filter filter used for scaling, e.g. GL_LINEAR
567 * \param f file to read PPM from
568 * \param width [out] width of texture
569 * \param height [out] height of texture
570 * \param maxval [out] maxval value from PPM file
571 * \return 0 on error, 1 otherwise
572 * \ingroup gltexture
574 int glCreatePPMTex(GLenum target, GLenum fmt, GLint filter,
575 FILE *f, int *width, int *height, int *maxval) {
576 int w, h, m, bpp;
577 GLenum type;
578 uint8_t *data = read_pnm(f, &w, &h, &bpp, &m);
579 if (!data || (bpp != 3 && bpp != 6)) {
580 free(data);
581 return 0;
583 if (!fmt) {
584 fmt = bpp == 6 ? hqtexfmt : 3;
585 if (fmt == GL_FLOAT_RGB32_NV && target != GL_TEXTURE_RECTANGLE)
586 fmt = GL_RGB16;
588 type = bpp == 6 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_BYTE;
589 glCreateClearTex(target, fmt, GL_RGB, type, filter, w, h, 0);
590 glUploadTex(target, GL_RGB, type,
591 data, w * bpp, 0, 0, w, h, 0);
592 free(data);
593 if (width) *width = w;
594 if (height) *height = h;
595 if (maxval) *maxval = m;
596 return 1;
600 * \brief return the number of bytes per pixel for the given format
601 * \param format OpenGL format
602 * \param type OpenGL type
603 * \return bytes per pixel
604 * \ingroup glgeneral
606 * Does not handle all possible variants, just those used by MPlayer
608 int glFmt2bpp(GLenum format, GLenum type) {
609 int component_size = 0;
610 switch (type) {
611 case GL_UNSIGNED_BYTE_3_3_2:
612 case GL_UNSIGNED_BYTE_2_3_3_REV:
613 return 1;
614 case GL_UNSIGNED_SHORT_5_5_5_1:
615 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
616 case GL_UNSIGNED_SHORT_5_6_5:
617 case GL_UNSIGNED_SHORT_5_6_5_REV:
618 return 2;
619 case GL_UNSIGNED_BYTE:
620 component_size = 1;
621 break;
622 case GL_UNSIGNED_SHORT:
623 component_size = 2;
624 break;
626 switch (format) {
627 case GL_LUMINANCE:
628 case GL_ALPHA:
629 return component_size;
630 case GL_YCBCR_MESA:
631 return 2;
632 case GL_RGB:
633 case GL_BGR:
634 return 3 * component_size;
635 case GL_RGBA:
636 case GL_BGRA:
637 return 4 * component_size;
639 return 0; // unknown
643 * \brief upload a texture, handling things like stride and slices
644 * \param target texture target, usually GL_TEXTURE_2D
645 * \param format OpenGL format of data
646 * \param type OpenGL type of data
647 * \param dataptr data to upload
648 * \param stride data stride
649 * \param x x offset in texture
650 * \param y y offset in texture
651 * \param w width of the texture part to upload
652 * \param h height of the texture part to upload
653 * \param slice height of an upload slice, 0 for all at once
654 * \ingroup gltexture
656 void glUploadTex(GLenum target, GLenum format, GLenum type,
657 const void *dataptr, int stride,
658 int x, int y, int w, int h, int slice) {
659 const uint8_t *data = dataptr;
660 int y_max = y + h;
661 if (w <= 0 || h <= 0) return;
662 if (slice <= 0)
663 slice = h;
664 if (stride < 0) {
665 data += (h - 1) * stride;
666 stride = -stride;
668 // this is not always correct, but should work for MPlayer
669 glAdjustAlignment(stride);
670 mpglPixelStorei(GL_UNPACK_ROW_LENGTH, stride / glFmt2bpp(format, type));
671 for (; y + slice <= y_max; y += slice) {
672 mpglTexSubImage2D(target, 0, x, y, w, slice, format, type, data);
673 data += stride * slice;
675 if (y < y_max)
676 mpglTexSubImage2D(target, 0, x, y, w, y_max - y, format, type, data);
679 static void fillUVcoeff(GLfloat *ucoef, GLfloat *vcoef,
680 float uvcos, float uvsin) {
681 int i;
682 ucoef[0] = 0 * uvcos + 1.403 * uvsin;
683 vcoef[0] = 0 * uvsin + 1.403 * uvcos;
684 ucoef[1] = -0.344 * uvcos + -0.714 * uvsin;
685 vcoef[1] = -0.344 * uvsin + -0.714 * uvcos;
686 ucoef[2] = 1.770 * uvcos + 0 * uvsin;
687 vcoef[2] = 1.770 * uvsin + 0 * uvcos;
688 ucoef[3] = 0;
689 vcoef[3] = 0;
690 // Coefficients (probably) must be in [0, 1] range, whereas they originally
691 // are in [-2, 2] range, so here comes the trick:
692 // First put them in the [-0.5, 0.5] range, then add 0.5.
693 // This can be undone with the HALF_BIAS and SCALE_BY_FOUR arguments
694 // for CombinerInput and CombinerOutput (or the respective ATI variants)
695 for (i = 0; i < 4; i++) {
696 ucoef[i] = ucoef[i] * 0.25 + 0.5;
697 vcoef[i] = vcoef[i] * 0.25 + 0.5;
702 * \brief Setup register combiners for YUV to RGB conversion.
703 * \param uvcos used for saturation and hue adjustment
704 * \param uvsin used for saturation and hue adjustment
706 static void glSetupYUVCombiners(float uvcos, float uvsin) {
707 GLfloat ucoef[4];
708 GLfloat vcoef[4];
709 GLint i;
710 if (!mpglCombinerInput || !mpglCombinerOutput ||
711 !mpglCombinerParameterfv || !mpglCombinerParameteri) {
712 mp_msg(MSGT_VO, MSGL_FATAL, "[gl] Combiner functions missing!\n");
713 return;
715 mpglGetIntegerv(GL_MAX_GENERAL_COMBINERS_NV, &i);
716 if (i < 2)
717 mp_msg(MSGT_VO, MSGL_ERR,
718 "[gl] 2 general combiners needed for YUV combiner support (found %i)\n", i);
719 mpglGetIntegerv(GL_MAX_TEXTURE_UNITS, &i);
720 if (i < 3)
721 mp_msg(MSGT_VO, MSGL_ERR,
722 "[gl] 3 texture units needed for YUV combiner support (found %i)\n", i);
723 fillUVcoeff(ucoef, vcoef, uvcos, uvsin);
724 mpglCombinerParameterfv(GL_CONSTANT_COLOR0_NV, ucoef);
725 mpglCombinerParameterfv(GL_CONSTANT_COLOR1_NV, vcoef);
727 // UV first, like this green component cannot overflow
728 mpglCombinerInput(GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_A_NV,
729 GL_TEXTURE1, GL_HALF_BIAS_NORMAL_NV, GL_RGB);
730 mpglCombinerInput(GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_B_NV,
731 GL_CONSTANT_COLOR0_NV, GL_HALF_BIAS_NORMAL_NV, GL_RGB);
732 mpglCombinerInput(GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_C_NV,
733 GL_TEXTURE2, GL_HALF_BIAS_NORMAL_NV, GL_RGB);
734 mpglCombinerInput(GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_D_NV,
735 GL_CONSTANT_COLOR1_NV, GL_HALF_BIAS_NORMAL_NV, GL_RGB);
736 mpglCombinerOutput(GL_COMBINER0_NV, GL_RGB, GL_DISCARD_NV, GL_DISCARD_NV,
737 GL_SPARE0_NV, GL_SCALE_BY_FOUR_NV, GL_NONE, GL_FALSE,
738 GL_FALSE, GL_FALSE);
740 // stage 2
741 mpglCombinerInput(GL_COMBINER1_NV, GL_RGB, GL_VARIABLE_A_NV, GL_SPARE0_NV,
742 GL_SIGNED_IDENTITY_NV, GL_RGB);
743 mpglCombinerInput(GL_COMBINER1_NV, GL_RGB, GL_VARIABLE_B_NV, GL_ZERO,
744 GL_UNSIGNED_INVERT_NV, GL_RGB);
745 mpglCombinerInput(GL_COMBINER1_NV, GL_RGB, GL_VARIABLE_C_NV,
746 GL_TEXTURE0, GL_SIGNED_IDENTITY_NV, GL_RGB);
747 mpglCombinerInput(GL_COMBINER1_NV, GL_RGB, GL_VARIABLE_D_NV, GL_ZERO,
748 GL_UNSIGNED_INVERT_NV, GL_RGB);
749 mpglCombinerOutput(GL_COMBINER1_NV, GL_RGB, GL_DISCARD_NV, GL_DISCARD_NV,
750 GL_SPARE0_NV, GL_NONE, GL_NONE, GL_FALSE,
751 GL_FALSE, GL_FALSE);
753 // leave final combiner stage in default mode
754 mpglCombinerParameteri(GL_NUM_GENERAL_COMBINERS_NV, 2);
758 * \brief Setup ATI version of register combiners for YUV to RGB conversion.
759 * \param csp_params parameters used for colorspace conversion
760 * \param text if set use the GL_ATI_text_fragment_shader API as
761 * used on OS X.
763 static void glSetupYUVFragmentATI(struct mp_csp_params *csp_params,
764 int text) {
765 GLint i;
766 float yuv2rgb[3][4];
768 mpglGetIntegerv (GL_MAX_TEXTURE_UNITS, &i);
769 if (i < 3)
770 mp_msg(MSGT_VO, MSGL_ERR,
771 "[gl] 3 texture units needed for YUV combiner (ATI) support (found %i)\n", i);
773 mp_get_yuv2rgb_coeffs(csp_params, yuv2rgb);
774 for (i = 0; i < 3; i++) {
775 int j;
776 yuv2rgb[i][3] -= -0.5 * (yuv2rgb[i][1] + yuv2rgb[i][2]);
777 for (j = 0; j < 4; j++) {
778 yuv2rgb[i][j] *= 0.125;
779 yuv2rgb[i][j] += 0.5;
780 if (yuv2rgb[i][j] > 1)
781 yuv2rgb[i][j] = 1;
782 if (yuv2rgb[i][j] < 0)
783 yuv2rgb[i][j] = 0;
786 if (text == 0) {
787 GLfloat c0[4] = {yuv2rgb[0][0], yuv2rgb[1][0], yuv2rgb[2][0]};
788 GLfloat c1[4] = {yuv2rgb[0][1], yuv2rgb[1][1], yuv2rgb[2][1]};
789 GLfloat c2[4] = {yuv2rgb[0][2], yuv2rgb[1][2], yuv2rgb[2][2]};
790 GLfloat c3[4] = {yuv2rgb[0][3], yuv2rgb[1][3], yuv2rgb[2][3]};
791 if (!mpglBeginFragmentShader || !mpglEndFragmentShader ||
792 !mpglSetFragmentShaderConstant || !mpglSampleMap ||
793 !mpglColorFragmentOp2 || !mpglColorFragmentOp3) {
794 mp_msg(MSGT_VO, MSGL_FATAL, "[gl] Combiner (ATI) functions missing!\n");
795 return;
797 mpglGetIntegerv(GL_NUM_FRAGMENT_REGISTERS_ATI, &i);
798 if (i < 3)
799 mp_msg(MSGT_VO, MSGL_ERR,
800 "[gl] 3 registers needed for YUV combiner (ATI) support (found %i)\n", i);
801 mpglBeginFragmentShader();
802 mpglSetFragmentShaderConstant(GL_CON_0_ATI, c0);
803 mpglSetFragmentShaderConstant(GL_CON_1_ATI, c1);
804 mpglSetFragmentShaderConstant(GL_CON_2_ATI, c2);
805 mpglSetFragmentShaderConstant(GL_CON_3_ATI, c3);
806 mpglSampleMap(GL_REG_0_ATI, GL_TEXTURE0, GL_SWIZZLE_STR_ATI);
807 mpglSampleMap(GL_REG_1_ATI, GL_TEXTURE1, GL_SWIZZLE_STR_ATI);
808 mpglSampleMap(GL_REG_2_ATI, GL_TEXTURE2, GL_SWIZZLE_STR_ATI);
809 mpglColorFragmentOp2(GL_MUL_ATI, GL_REG_1_ATI, GL_NONE, GL_NONE,
810 GL_REG_1_ATI, GL_NONE, GL_BIAS_BIT_ATI,
811 GL_CON_1_ATI, GL_NONE, GL_BIAS_BIT_ATI);
812 mpglColorFragmentOp3(GL_MAD_ATI, GL_REG_2_ATI, GL_NONE, GL_NONE,
813 GL_REG_2_ATI, GL_NONE, GL_BIAS_BIT_ATI,
814 GL_CON_2_ATI, GL_NONE, GL_BIAS_BIT_ATI,
815 GL_REG_1_ATI, GL_NONE, GL_NONE);
816 mpglColorFragmentOp3(GL_MAD_ATI, GL_REG_0_ATI, GL_NONE, GL_NONE,
817 GL_REG_0_ATI, GL_NONE, GL_NONE,
818 GL_CON_0_ATI, GL_NONE, GL_BIAS_BIT_ATI,
819 GL_REG_2_ATI, GL_NONE, GL_NONE);
820 mpglColorFragmentOp2(GL_ADD_ATI, GL_REG_0_ATI, GL_NONE, GL_8X_BIT_ATI,
821 GL_REG_0_ATI, GL_NONE, GL_NONE,
822 GL_CON_3_ATI, GL_NONE, GL_BIAS_BIT_ATI);
823 mpglEndFragmentShader();
824 } else {
825 static const char template[] =
826 "!!ATIfs1.0\n"
827 "StartConstants;\n"
828 " CONSTANT c0 = {%e, %e, %e};\n"
829 " CONSTANT c1 = {%e, %e, %e};\n"
830 " CONSTANT c2 = {%e, %e, %e};\n"
831 " CONSTANT c3 = {%e, %e, %e};\n"
832 "EndConstants;\n"
833 "StartOutputPass;\n"
834 " SampleMap r0, t0.str;\n"
835 " SampleMap r1, t1.str;\n"
836 " SampleMap r2, t2.str;\n"
837 " MUL r1.rgb, r1.bias, c1.bias;\n"
838 " MAD r2.rgb, r2.bias, c2.bias, r1;\n"
839 " MAD r0.rgb, r0, c0.bias, r2;\n"
840 " ADD r0.rgb.8x, r0, c3.bias;\n"
841 "EndPass;\n";
842 char buffer[512];
843 snprintf(buffer, sizeof(buffer), template,
844 yuv2rgb[0][0], yuv2rgb[1][0], yuv2rgb[2][0],
845 yuv2rgb[0][1], yuv2rgb[1][1], yuv2rgb[2][1],
846 yuv2rgb[0][2], yuv2rgb[1][2], yuv2rgb[2][2],
847 yuv2rgb[0][3], yuv2rgb[1][3], yuv2rgb[2][3]);
848 mp_msg(MSGT_VO, MSGL_DBG2, "[gl] generated fragment program:\n%s\n", buffer);
849 loadGPUProgram(GL_TEXT_FRAGMENT_SHADER_ATI, buffer);
854 * \brief helper function for gen_spline_lookup_tex
855 * \param x subpixel-position ((0,1) range) to calculate weights for
856 * \param dst where to store transformed weights, must provide space for 4 GLfloats
858 * calculates the weights and stores them after appropriate transformation
859 * for the scaler fragment program.
861 static void store_weights(float x, GLfloat *dst) {
862 float w0 = (((-1 * x + 3) * x - 3) * x + 1) / 6;
863 float w1 = ((( 3 * x - 6) * x + 0) * x + 4) / 6;
864 float w2 = (((-3 * x + 3) * x + 3) * x + 1) / 6;
865 float w3 = ((( 1 * x + 0) * x + 0) * x + 0) / 6;
866 *dst++ = 1 + x - w1 / (w0 + w1);
867 *dst++ = 1 - x + w3 / (w2 + w3);
868 *dst++ = w0 + w1;
869 *dst++ = 0;
872 //! to avoid artefacts this should be rather large
873 #define LOOKUP_BSPLINE_RES (2 * 1024)
875 * \brief creates the 1D lookup texture needed for fast higher-order filtering
876 * \param unit texture unit to attach texture to
878 static void gen_spline_lookup_tex(GLenum unit) {
879 GLfloat *tex = calloc(4 * LOOKUP_BSPLINE_RES, sizeof(*tex));
880 GLfloat *tp = tex;
881 int i;
882 for (i = 0; i < LOOKUP_BSPLINE_RES; i++) {
883 float x = (float)(i + 0.5) / LOOKUP_BSPLINE_RES;
884 store_weights(x, tp);
885 tp += 4;
887 store_weights(0, tex);
888 store_weights(1, &tex[4 * (LOOKUP_BSPLINE_RES - 1)]);
889 mpglActiveTexture(unit);
890 mpglTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA16, LOOKUP_BSPLINE_RES, 0, GL_RGBA, GL_FLOAT, tex);
891 mpglTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_PRIORITY, 1.0);
892 mpglTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
893 mpglTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
894 mpglTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT);
895 mpglActiveTexture(GL_TEXTURE0);
896 free(tex);
899 static const char *bilin_filt_template =
900 "TEX yuv.%c, fragment.texcoord[%c], texture[%c], %s;\n";
902 #define BICUB_FILT_MAIN(textype) \
903 /* first y-interpolation */ \
904 "ADD coord, fragment.texcoord[%c].xyxy, cdelta.xyxw;\n" \
905 "ADD coord2, fragment.texcoord[%c].xyxy, cdelta.zyzw;\n" \
906 "TEX a.r, coord.xyxy, texture[%c], "textype";\n" \
907 "TEX a.g, coord.zwzw, texture[%c], "textype";\n" \
908 /* second y-interpolation */ \
909 "TEX b.r, coord2.xyxy, texture[%c], "textype";\n" \
910 "TEX b.g, coord2.zwzw, texture[%c], "textype";\n" \
911 "LRP a.b, parmy.b, a.rrrr, a.gggg;\n" \
912 "LRP a.a, parmy.b, b.rrrr, b.gggg;\n" \
913 /* x-interpolation */ \
914 "LRP yuv.%c, parmx.b, a.bbbb, a.aaaa;\n"
916 static const char *bicub_filt_template_2D =
917 "MAD coord.xy, fragment.texcoord[%c], {%e, %e}, {0.5, 0.5};\n"
918 "TEX parmx, coord.x, texture[%c], 1D;\n"
919 "MUL cdelta.xz, parmx.rrgg, {-%e, 0, %e, 0};\n"
920 "TEX parmy, coord.y, texture[%c], 1D;\n"
921 "MUL cdelta.yw, parmy.rrgg, {0, -%e, 0, %e};\n"
922 BICUB_FILT_MAIN("2D");
924 static const char *bicub_filt_template_RECT =
925 "ADD coord, fragment.texcoord[%c], {0.5, 0.5};\n"
926 "TEX parmx, coord.x, texture[%c], 1D;\n"
927 "MUL cdelta.xz, parmx.rrgg, {-1, 0, 1, 0};\n"
928 "TEX parmy, coord.y, texture[%c], 1D;\n"
929 "MUL cdelta.yw, parmy.rrgg, {0, -1, 0, 1};\n"
930 BICUB_FILT_MAIN("RECT");
932 #define CALCWEIGHTS(t, s) \
933 "MAD "t", {-0.5, 0.1666, 0.3333, -0.3333}, "s", {1, 0, -0.5, 0.5};\n" \
934 "MAD "t", "t", "s", {0, 0, -0.5, 0.5};\n" \
935 "MAD "t", "t", "s", {-0.6666, 0, 0.8333, 0.1666};\n" \
936 "RCP a.x, "t".z;\n" \
937 "RCP a.y, "t".w;\n" \
938 "MAD "t".xy, "t".xyxy, a.xyxy, {1, 1, 0, 0};\n" \
939 "ADD "t".x, "t".xxxx, "s";\n" \
940 "SUB "t".y, "t".yyyy, "s";\n"
942 static const char *bicub_notex_filt_template_2D =
943 "MAD coord.xy, fragment.texcoord[%c], {%e, %e}, {0.5, 0.5};\n"
944 "FRC coord.xy, coord.xyxy;\n"
945 CALCWEIGHTS("parmx", "coord.xxxx")
946 "MUL cdelta.xz, parmx.rrgg, {-%e, 0, %e, 0};\n"
947 CALCWEIGHTS("parmy", "coord.yyyy")
948 "MUL cdelta.yw, parmy.rrgg, {0, -%e, 0, %e};\n"
949 BICUB_FILT_MAIN("2D");
951 static const char *bicub_notex_filt_template_RECT =
952 "ADD coord, fragment.texcoord[%c], {0.5, 0.5};\n"
953 "FRC coord.xy, coord.xyxy;\n"
954 CALCWEIGHTS("parmx", "coord.xxxx")
955 "MUL cdelta.xz, parmx.rrgg, {-1, 0, 1, 0};\n"
956 CALCWEIGHTS("parmy", "coord.yyyy")
957 "MUL cdelta.yw, parmy.rrgg, {0, -1, 0, 1};\n"
958 BICUB_FILT_MAIN("RECT");
960 #define BICUB_X_FILT_MAIN(textype) \
961 "ADD coord.xy, fragment.texcoord[%c].xyxy, cdelta.xyxy;\n" \
962 "ADD coord2.xy, fragment.texcoord[%c].xyxy, cdelta.zyzy;\n" \
963 "TEX a.r, coord, texture[%c], "textype";\n" \
964 "TEX b.r, coord2, texture[%c], "textype";\n" \
965 /* x-interpolation */ \
966 "LRP yuv.%c, parmx.b, a.rrrr, b.rrrr;\n"
968 static const char *bicub_x_filt_template_2D =
969 "MAD coord.x, fragment.texcoord[%c], {%e}, {0.5};\n"
970 "TEX parmx, coord, texture[%c], 1D;\n"
971 "MUL cdelta.xyz, parmx.rrgg, {-%e, 0, %e};\n"
972 BICUB_X_FILT_MAIN("2D");
974 static const char *bicub_x_filt_template_RECT =
975 "ADD coord.x, fragment.texcoord[%c], {0.5};\n"
976 "TEX parmx, coord, texture[%c], 1D;\n"
977 "MUL cdelta.xyz, parmx.rrgg, {-1, 0, 1};\n"
978 BICUB_X_FILT_MAIN("RECT");
980 static const char *unsharp_filt_template =
981 "PARAM dcoord%c = {%e, %e, %e, %e};\n"
982 "ADD coord, fragment.texcoord[%c].xyxy, dcoord%c;\n"
983 "SUB coord2, fragment.texcoord[%c].xyxy, dcoord%c;\n"
984 "TEX a.r, fragment.texcoord[%c], texture[%c], %s;\n"
985 "TEX b.r, coord.xyxy, texture[%c], %s;\n"
986 "TEX b.g, coord.zwzw, texture[%c], %s;\n"
987 "ADD b.r, b.r, b.g;\n"
988 "TEX b.b, coord2.xyxy, texture[%c], %s;\n"
989 "TEX b.g, coord2.zwzw, texture[%c], %s;\n"
990 "DP3 b, b, {0.25, 0.25, 0.25};\n"
991 "SUB b.r, a.r, b.r;\n"
992 "MAD yuv.%c, b.r, {%e}, a.r;\n";
994 static const char *unsharp_filt_template2 =
995 "PARAM dcoord%c = {%e, %e, %e, %e};\n"
996 "PARAM dcoord2%c = {%e, 0, 0, %e};\n"
997 "ADD coord, fragment.texcoord[%c].xyxy, dcoord%c;\n"
998 "SUB coord2, fragment.texcoord[%c].xyxy, dcoord%c;\n"
999 "TEX a.r, fragment.texcoord[%c], texture[%c], %s;\n"
1000 "TEX b.r, coord.xyxy, texture[%c], %s;\n"
1001 "TEX b.g, coord.zwzw, texture[%c], %s;\n"
1002 "ADD b.r, b.r, b.g;\n"
1003 "TEX b.b, coord2.xyxy, texture[%c], %s;\n"
1004 "TEX b.g, coord2.zwzw, texture[%c], %s;\n"
1005 "ADD b.r, b.r, b.b;\n"
1006 "ADD b.a, b.r, b.g;\n"
1007 "ADD coord, fragment.texcoord[%c].xyxy, dcoord2%c;\n"
1008 "SUB coord2, fragment.texcoord[%c].xyxy, dcoord2%c;\n"
1009 "TEX b.r, coord.xyxy, texture[%c], %s;\n"
1010 "TEX b.g, coord.zwzw, texture[%c], %s;\n"
1011 "ADD b.r, b.r, b.g;\n"
1012 "TEX b.b, coord2.xyxy, texture[%c], %s;\n"
1013 "TEX b.g, coord2.zwzw, texture[%c], %s;\n"
1014 "DP4 b.r, b, {-0.1171875, -0.1171875, -0.1171875, -0.09765625};\n"
1015 "MAD b.r, a.r, {0.859375}, b.r;\n"
1016 "MAD yuv.%c, b.r, {%e}, a.r;\n";
1018 static const char *yuv_prog_template =
1019 "PARAM ycoef = {%e, %e, %e};\n"
1020 "PARAM ucoef = {%e, %e, %e};\n"
1021 "PARAM vcoef = {%e, %e, %e};\n"
1022 "PARAM offsets = {%e, %e, %e};\n"
1023 "TEMP res;\n"
1024 "MAD res.rgb, yuv.rrrr, ycoef, offsets;\n"
1025 "MAD res.rgb, yuv.gggg, ucoef, res;\n"
1026 "MAD result.color.rgb, yuv.bbbb, vcoef, res;\n"
1027 "END";
1029 static const char *yuv_pow_prog_template =
1030 "PARAM ycoef = {%e, %e, %e};\n"
1031 "PARAM ucoef = {%e, %e, %e};\n"
1032 "PARAM vcoef = {%e, %e, %e};\n"
1033 "PARAM offsets = {%e, %e, %e};\n"
1034 "PARAM gamma = {%e, %e, %e};\n"
1035 "TEMP res;\n"
1036 "MAD res.rgb, yuv.rrrr, ycoef, offsets;\n"
1037 "MAD res.rgb, yuv.gggg, ucoef, res;\n"
1038 "MAD_SAT res.rgb, yuv.bbbb, vcoef, res;\n"
1039 "POW result.color.r, res.r, gamma.r;\n"
1040 "POW result.color.g, res.g, gamma.g;\n"
1041 "POW result.color.b, res.b, gamma.b;\n"
1042 "END";
1044 static const char *yuv_lookup_prog_template =
1045 "PARAM ycoef = {%e, %e, %e, 0};\n"
1046 "PARAM ucoef = {%e, %e, %e, 0};\n"
1047 "PARAM vcoef = {%e, %e, %e, 0};\n"
1048 "PARAM offsets = {%e, %e, %e, 0.125};\n"
1049 "TEMP res;\n"
1050 "MAD res, yuv.rrrr, ycoef, offsets;\n"
1051 "MAD res.rgb, yuv.gggg, ucoef, res;\n"
1052 "MAD res.rgb, yuv.bbbb, vcoef, res;\n"
1053 "TEX result.color.r, res.raaa, texture[%c], 2D;\n"
1054 "ADD res.a, res.a, 0.25;\n"
1055 "TEX result.color.g, res.gaaa, texture[%c], 2D;\n"
1056 "ADD res.a, res.a, 0.25;\n"
1057 "TEX result.color.b, res.baaa, texture[%c], 2D;\n"
1058 "END";
1060 static const char *yuv_lookup3d_prog_template =
1061 "TEX result.color, yuv, texture[%c], 3D;\n"
1062 "END";
1065 * \brief creates and initializes helper textures needed for scaling texture read
1066 * \param scaler scaler type to create texture for
1067 * \param texu contains next free texture unit number
1068 * \param texs texture unit ids for the scaler are stored in this array
1070 static void create_scaler_textures(int scaler, int *texu, char *texs) {
1071 switch (scaler) {
1072 case YUV_SCALER_BILIN:
1073 case YUV_SCALER_BICUB_NOTEX:
1074 case YUV_SCALER_UNSHARP:
1075 case YUV_SCALER_UNSHARP2:
1076 break;
1077 case YUV_SCALER_BICUB:
1078 case YUV_SCALER_BICUB_X:
1079 texs[0] = (*texu)++;
1080 gen_spline_lookup_tex(GL_TEXTURE0 + texs[0]);
1081 texs[0] += '0';
1082 break;
1083 default:
1084 mp_msg(MSGT_VO, MSGL_ERR, "[gl] unknown scaler type %i\n", scaler);
1088 //! resolution of texture for gamma lookup table
1089 #define LOOKUP_RES 512
1090 //! resolution for 3D yuv->rgb conversion lookup table
1091 #define LOOKUP_3DRES 32
1093 * \brief creates and initializes helper textures needed for yuv conversion
1094 * \param params struct containing parameters like brightness, gamma, ...
1095 * \param texu contains next free texture unit number
1096 * \param texs texture unit ids for the conversion are stored in this array
1098 static void create_conv_textures(gl_conversion_params_t *params, int *texu, char *texs) {
1099 unsigned char *lookup_data = NULL;
1100 int conv = YUV_CONVERSION(params->type);
1101 switch (conv) {
1102 case YUV_CONVERSION_FRAGMENT:
1103 case YUV_CONVERSION_FRAGMENT_POW:
1104 break;
1105 case YUV_CONVERSION_FRAGMENT_LOOKUP:
1106 texs[0] = (*texu)++;
1107 mpglActiveTexture(GL_TEXTURE0 + texs[0]);
1108 lookup_data = malloc(4 * LOOKUP_RES);
1109 mp_gen_gamma_map(lookup_data, LOOKUP_RES, params->csp_params.rgamma);
1110 mp_gen_gamma_map(&lookup_data[LOOKUP_RES], LOOKUP_RES, params->csp_params.ggamma);
1111 mp_gen_gamma_map(&lookup_data[2 * LOOKUP_RES], LOOKUP_RES, params->csp_params.bgamma);
1112 glCreateClearTex(GL_TEXTURE_2D, GL_LUMINANCE8, GL_LUMINANCE, GL_UNSIGNED_BYTE, GL_LINEAR,
1113 LOOKUP_RES, 4, 0);
1114 glUploadTex(GL_TEXTURE_2D, GL_LUMINANCE, GL_UNSIGNED_BYTE, lookup_data,
1115 LOOKUP_RES, 0, 0, LOOKUP_RES, 4, 0);
1116 mpglActiveTexture(GL_TEXTURE0);
1117 texs[0] += '0';
1118 break;
1119 case YUV_CONVERSION_FRAGMENT_LOOKUP3D:
1121 int sz = LOOKUP_3DRES + 2; // texture size including borders
1122 if (!mpglTexImage3D) {
1123 mp_msg(MSGT_VO, MSGL_ERR, "[gl] Missing 3D texture function!\n");
1124 break;
1126 texs[0] = (*texu)++;
1127 mpglActiveTexture(GL_TEXTURE0 + texs[0]);
1128 lookup_data = malloc(3 * sz * sz * sz);
1129 mp_gen_yuv2rgb_map(&params->csp_params, lookup_data, LOOKUP_3DRES);
1130 glAdjustAlignment(sz);
1131 mpglPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
1132 mpglTexImage3D(GL_TEXTURE_3D, 0, 3, sz, sz, sz, 1,
1133 GL_RGB, GL_UNSIGNED_BYTE, lookup_data);
1134 mpglTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_PRIORITY, 1.0);
1135 mpglTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1136 mpglTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1137 mpglTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP);
1138 mpglTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP);
1139 mpglTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP);
1140 mpglActiveTexture(GL_TEXTURE0);
1141 texs[0] += '0';
1143 break;
1144 default:
1145 mp_msg(MSGT_VO, MSGL_ERR, "[gl] unknown conversion type %i\n", conv);
1147 free(lookup_data);
1151 * \brief adds a scaling texture read at the current fragment program position
1152 * \param scaler type of scaler to insert
1153 * \param prog_pos current position in fragment program
1154 * \param remain how many bytes remain in the buffer given by prog_pos
1155 * \param texs array containing the texture unit identifiers for this scaler
1156 * \param in_tex texture unit the scaler should read from
1157 * \param out_comp component of the yuv variable the scaler stores the result in
1158 * \param rect if rectangular (pixel) adressing should be used for in_tex
1159 * \param texw width of the in_tex texture
1160 * \param texh height of the in_tex texture
1161 * \param strength strength of filter effect if the scaler does some kind of filtering
1163 static void add_scaler(int scaler, char **prog_pos, int *remain, char *texs,
1164 char in_tex, char out_comp, int rect, int texw, int texh,
1165 double strength) {
1166 const char *ttype = rect ? "RECT" : "2D";
1167 const float ptw = rect ? 1.0 : 1.0 / texw;
1168 const float pth = rect ? 1.0 : 1.0 / texh;
1169 switch (scaler) {
1170 case YUV_SCALER_BILIN:
1171 snprintf(*prog_pos, *remain, bilin_filt_template, out_comp, in_tex,
1172 in_tex, ttype);
1173 break;
1174 case YUV_SCALER_BICUB:
1175 if (rect)
1176 snprintf(*prog_pos, *remain, bicub_filt_template_RECT,
1177 in_tex, texs[0], texs[0],
1178 in_tex, in_tex, in_tex, in_tex, in_tex, in_tex, out_comp);
1179 else
1180 snprintf(*prog_pos, *remain, bicub_filt_template_2D,
1181 in_tex, (float)texw, (float)texh,
1182 texs[0], ptw, ptw, texs[0], pth, pth,
1183 in_tex, in_tex, in_tex, in_tex, in_tex, in_tex, out_comp);
1184 break;
1185 case YUV_SCALER_BICUB_X:
1186 if (rect)
1187 snprintf(*prog_pos, *remain, bicub_x_filt_template_RECT,
1188 in_tex, texs[0],
1189 in_tex, in_tex, in_tex, in_tex, out_comp);
1190 else
1191 snprintf(*prog_pos, *remain, bicub_x_filt_template_2D,
1192 in_tex, (float)texw,
1193 texs[0], ptw, ptw,
1194 in_tex, in_tex, in_tex, in_tex, out_comp);
1195 break;
1196 case YUV_SCALER_BICUB_NOTEX:
1197 if (rect)
1198 snprintf(*prog_pos, *remain, bicub_notex_filt_template_RECT,
1199 in_tex,
1200 in_tex, in_tex, in_tex, in_tex, in_tex, in_tex, out_comp);
1201 else
1202 snprintf(*prog_pos, *remain, bicub_notex_filt_template_2D,
1203 in_tex, (float)texw, (float)texh, ptw, ptw, pth, pth,
1204 in_tex, in_tex, in_tex, in_tex, in_tex, in_tex, out_comp);
1205 break;
1206 case YUV_SCALER_UNSHARP:
1207 snprintf(*prog_pos, *remain, unsharp_filt_template,
1208 out_comp, 0.5 * ptw, 0.5 * pth, 0.5 * ptw, -0.5 * pth,
1209 in_tex, out_comp, in_tex, out_comp, in_tex,
1210 in_tex, ttype, in_tex, ttype, in_tex, ttype, in_tex, ttype,
1211 in_tex, ttype, out_comp, strength);
1212 break;
1213 case YUV_SCALER_UNSHARP2:
1214 snprintf(*prog_pos, *remain, unsharp_filt_template2,
1215 out_comp, 1.2 * ptw, 1.2 * pth, 1.2 * ptw, -1.2 * pth,
1216 out_comp, 1.5 * ptw, 1.5 * pth,
1217 in_tex, out_comp, in_tex, out_comp, in_tex,
1218 in_tex, ttype, in_tex, ttype, in_tex, ttype, in_tex, ttype,
1219 in_tex, ttype, in_tex, out_comp, in_tex, out_comp,
1220 in_tex, ttype, in_tex, ttype, in_tex, ttype,
1221 in_tex, ttype, out_comp, strength);
1222 break;
1224 *remain -= strlen(*prog_pos);
1225 *prog_pos += strlen(*prog_pos);
1228 static const struct {
1229 const char *name;
1230 GLenum cur;
1231 GLenum max;
1232 } progstats[] = {
1233 {"instructions", 0x88A0, 0x88A1},
1234 {"native instructions", 0x88A2, 0x88A3},
1235 {"temporaries", 0x88A4, 0x88A5},
1236 {"native temporaries", 0x88A6, 0x88A7},
1237 {"parameters", 0x88A8, 0x88A9},
1238 {"native parameters", 0x88AA, 0x88AB},
1239 {"attribs", 0x88AC, 0x88AD},
1240 {"native attribs", 0x88AE, 0x88AF},
1241 {"ALU instructions", 0x8805, 0x880B},
1242 {"TEX instructions", 0x8806, 0x880C},
1243 {"TEX indirections", 0x8807, 0x880D},
1244 {"native ALU instructions", 0x8808, 0x880E},
1245 {"native TEX instructions", 0x8809, 0x880F},
1246 {"native TEX indirections", 0x880A, 0x8810},
1247 {NULL, 0, 0}
1251 * \brief load the specified GPU Program
1252 * \param target program target to load into, only GL_FRAGMENT_PROGRAM is tested
1253 * \param prog program string
1254 * \return 1 on success, 0 otherwise
1256 int loadGPUProgram(GLenum target, char *prog) {
1257 int i;
1258 GLint cur = 0, max = 0, err = 0;
1259 if (!mpglProgramString) {
1260 mp_msg(MSGT_VO, MSGL_ERR, "[gl] Missing GPU program function\n");
1261 return 0;
1263 mpglProgramString(target, GL_PROGRAM_FORMAT_ASCII, strlen(prog), prog);
1264 mpglGetIntegerv(GL_PROGRAM_ERROR_POSITION, &err);
1265 if (err != -1) {
1266 mp_msg(MSGT_VO, MSGL_ERR,
1267 "[gl] Error compiling fragment program, make sure your card supports\n"
1268 "[gl] GL_ARB_fragment_program (use glxinfo to check).\n"
1269 "[gl] Error message:\n %s at %.10s\n",
1270 mpglGetString(GL_PROGRAM_ERROR_STRING), &prog[err]);
1271 return 0;
1273 if (!mpglGetProgramiv || !mp_msg_test(MSGT_VO, MSGL_DBG2))
1274 return 1;
1275 mp_msg(MSGT_VO, MSGL_V, "[gl] Program statistics:\n");
1276 for (i = 0; progstats[i].name; i++) {
1277 mpglGetProgramiv(target, progstats[i].cur, &cur);
1278 mpglGetProgramiv(target, progstats[i].max, &max);
1279 mp_msg(MSGT_VO, MSGL_V, "[gl] %s: %i/%i\n", progstats[i].name, cur, max);
1281 return 1;
1284 #define MAX_PROGSZ (1024*1024)
1287 * \brief setup a fragment program that will do YUV->RGB conversion
1288 * \param parms struct containing parameters like conversion and scaler type,
1289 * brightness, ...
1291 static void glSetupYUVFragprog(gl_conversion_params_t *params) {
1292 int type = params->type;
1293 int texw = params->texw;
1294 int texh = params->texh;
1295 int rect = params->target == GL_TEXTURE_RECTANGLE;
1296 static const char prog_hdr[] =
1297 "!!ARBfp1.0\n"
1298 "OPTION ARB_precision_hint_fastest;\n"
1299 // all scaler variables must go here so they aren't defined
1300 // multiple times when the same scaler is used more than once
1301 "TEMP coord, coord2, cdelta, parmx, parmy, a, b, yuv;\n";
1302 int prog_remain;
1303 char *yuv_prog, *prog_pos;
1304 int cur_texu = 3;
1305 char lum_scale_texs[1];
1306 char chrom_scale_texs[1];
1307 char conv_texs[1];
1308 GLint i;
1309 // this is the conversion matrix, with y, u, v factors
1310 // for red, green, blue and the constant offsets
1311 float yuv2rgb[3][4];
1312 create_conv_textures(params, &cur_texu, conv_texs);
1313 create_scaler_textures(YUV_LUM_SCALER(type), &cur_texu, lum_scale_texs);
1314 if (YUV_CHROM_SCALER(type) == YUV_LUM_SCALER(type))
1315 memcpy(chrom_scale_texs, lum_scale_texs, sizeof(chrom_scale_texs));
1316 else
1317 create_scaler_textures(YUV_CHROM_SCALER(type), &cur_texu, chrom_scale_texs);
1318 mpglGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &i);
1319 if (i < cur_texu)
1320 mp_msg(MSGT_VO, MSGL_ERR,
1321 "[gl] %i texture units needed for this type of YUV fragment support (found %i)\n",
1322 cur_texu, i);
1323 if (!mpglProgramString) {
1324 mp_msg(MSGT_VO, MSGL_FATAL, "[gl] ProgramString function missing!\n");
1325 return;
1327 yuv_prog = malloc(MAX_PROGSZ);
1328 strcpy(yuv_prog, prog_hdr);
1329 prog_pos = yuv_prog + sizeof(prog_hdr) - 1;
1330 prog_remain = MAX_PROGSZ - sizeof(prog_hdr);
1331 add_scaler(YUV_LUM_SCALER(type), &prog_pos, &prog_remain, lum_scale_texs,
1332 '0', 'r', rect, texw, texh, params->filter_strength);
1333 add_scaler(YUV_CHROM_SCALER(type), &prog_pos, &prog_remain, chrom_scale_texs,
1334 '1', 'g', rect, params->chrom_texw, params->chrom_texh, params->filter_strength);
1335 add_scaler(YUV_CHROM_SCALER(type), &prog_pos, &prog_remain, chrom_scale_texs,
1336 '2', 'b', rect, params->chrom_texw, params->chrom_texh, params->filter_strength);
1337 mp_get_yuv2rgb_coeffs(&params->csp_params, yuv2rgb);
1338 switch (YUV_CONVERSION(type)) {
1339 case YUV_CONVERSION_FRAGMENT:
1340 snprintf(prog_pos, prog_remain, yuv_prog_template,
1341 yuv2rgb[ROW_R][COL_Y], yuv2rgb[ROW_G][COL_Y], yuv2rgb[ROW_B][COL_Y],
1342 yuv2rgb[ROW_R][COL_U], yuv2rgb[ROW_G][COL_U], yuv2rgb[ROW_B][COL_U],
1343 yuv2rgb[ROW_R][COL_V], yuv2rgb[ROW_G][COL_V], yuv2rgb[ROW_B][COL_V],
1344 yuv2rgb[ROW_R][COL_C], yuv2rgb[ROW_G][COL_C], yuv2rgb[ROW_B][COL_C]);
1345 break;
1346 case YUV_CONVERSION_FRAGMENT_POW:
1347 snprintf(prog_pos, prog_remain, yuv_pow_prog_template,
1348 yuv2rgb[ROW_R][COL_Y], yuv2rgb[ROW_G][COL_Y], yuv2rgb[ROW_B][COL_Y],
1349 yuv2rgb[ROW_R][COL_U], yuv2rgb[ROW_G][COL_U], yuv2rgb[ROW_B][COL_U],
1350 yuv2rgb[ROW_R][COL_V], yuv2rgb[ROW_G][COL_V], yuv2rgb[ROW_B][COL_V],
1351 yuv2rgb[ROW_R][COL_C], yuv2rgb[ROW_G][COL_C], yuv2rgb[ROW_B][COL_C],
1352 (float)1.0 / params->csp_params.rgamma, (float)1.0 / params->csp_params.bgamma, (float)1.0 / params->csp_params.bgamma);
1353 break;
1354 case YUV_CONVERSION_FRAGMENT_LOOKUP:
1355 snprintf(prog_pos, prog_remain, yuv_lookup_prog_template,
1356 yuv2rgb[ROW_R][COL_Y], yuv2rgb[ROW_G][COL_Y], yuv2rgb[ROW_B][COL_Y],
1357 yuv2rgb[ROW_R][COL_U], yuv2rgb[ROW_G][COL_U], yuv2rgb[ROW_B][COL_U],
1358 yuv2rgb[ROW_R][COL_V], yuv2rgb[ROW_G][COL_V], yuv2rgb[ROW_B][COL_V],
1359 yuv2rgb[ROW_R][COL_C], yuv2rgb[ROW_G][COL_C], yuv2rgb[ROW_B][COL_C],
1360 conv_texs[0], conv_texs[0], conv_texs[0]);
1361 break;
1362 case YUV_CONVERSION_FRAGMENT_LOOKUP3D:
1363 snprintf(prog_pos, prog_remain, yuv_lookup3d_prog_template, conv_texs[0]);
1364 break;
1365 default:
1366 mp_msg(MSGT_VO, MSGL_ERR, "[gl] unknown conversion type %i\n", YUV_CONVERSION(type));
1367 break;
1369 mp_msg(MSGT_VO, MSGL_DBG2, "[gl] generated fragment program:\n%s\n", yuv_prog);
1370 loadGPUProgram(GL_FRAGMENT_PROGRAM, yuv_prog);
1371 free(yuv_prog);
1375 * \brief detect the best YUV->RGB conversion method available
1377 int glAutodetectYUVConversion(void) {
1378 const char *extensions = mpglGetString(GL_EXTENSIONS);
1379 if (!extensions || !mpglMultiTexCoord2f)
1380 return YUV_CONVERSION_NONE;
1381 if (strstr(extensions, "GL_ARB_fragment_program"))
1382 return YUV_CONVERSION_FRAGMENT;
1383 if (strstr(extensions, "GL_ATI_text_fragment_shader"))
1384 return YUV_CONVERSION_TEXT_FRAGMENT;
1385 if (strstr(extensions, "GL_ATI_fragment_shader"))
1386 return YUV_CONVERSION_COMBINERS_ATI;
1387 return YUV_CONVERSION_NONE;
1391 * \brief setup YUV->RGB conversion
1392 * \param parms struct containing parameters like conversion and scaler type,
1393 * brightness, ...
1394 * \ingroup glconversion
1396 void glSetupYUVConversion(gl_conversion_params_t *params) {
1397 float uvcos = params->csp_params.saturation * cos(params->csp_params.hue);
1398 float uvsin = params->csp_params.saturation * sin(params->csp_params.hue);
1399 if (params->chrom_texw == 0) params->chrom_texw = 1;
1400 if (params->chrom_texh == 0) params->chrom_texh = 1;
1401 switch (YUV_CONVERSION(params->type)) {
1402 case YUV_CONVERSION_COMBINERS:
1403 glSetupYUVCombiners(uvcos, uvsin);
1404 break;
1405 case YUV_CONVERSION_COMBINERS_ATI:
1406 glSetupYUVFragmentATI(&params->csp_params, 0);
1407 break;
1408 case YUV_CONVERSION_TEXT_FRAGMENT:
1409 glSetupYUVFragmentATI(&params->csp_params, 1);
1410 break;
1411 case YUV_CONVERSION_FRAGMENT_LOOKUP:
1412 case YUV_CONVERSION_FRAGMENT_LOOKUP3D:
1413 case YUV_CONVERSION_FRAGMENT:
1414 case YUV_CONVERSION_FRAGMENT_POW:
1415 glSetupYUVFragprog(params);
1416 break;
1417 case YUV_CONVERSION_NONE:
1418 break;
1419 default:
1420 mp_msg(MSGT_VO, MSGL_ERR, "[gl] unknown conversion type %i\n", YUV_CONVERSION(params->type));
1425 * \brief enable the specified YUV conversion
1426 * \param target texture target for Y, U and V textures (e.g. GL_TEXTURE_2D)
1427 * \param type type of YUV conversion
1428 * \ingroup glconversion
1430 void glEnableYUVConversion(GLenum target, int type) {
1431 switch (YUV_CONVERSION(type)) {
1432 case YUV_CONVERSION_COMBINERS:
1433 mpglActiveTexture(GL_TEXTURE1);
1434 mpglEnable(target);
1435 mpglActiveTexture(GL_TEXTURE2);
1436 mpglEnable(target);
1437 mpglActiveTexture(GL_TEXTURE0);
1438 mpglEnable(GL_REGISTER_COMBINERS_NV);
1439 break;
1440 case YUV_CONVERSION_COMBINERS_ATI:
1441 mpglActiveTexture(GL_TEXTURE1);
1442 mpglEnable(target);
1443 mpglActiveTexture(GL_TEXTURE2);
1444 mpglEnable(target);
1445 mpglActiveTexture(GL_TEXTURE0);
1446 mpglEnable(GL_FRAGMENT_SHADER_ATI);
1447 break;
1448 case YUV_CONVERSION_TEXT_FRAGMENT:
1449 mpglActiveTexture(GL_TEXTURE1);
1450 mpglEnable(target);
1451 mpglActiveTexture(GL_TEXTURE2);
1452 mpglEnable(target);
1453 mpglActiveTexture(GL_TEXTURE0);
1454 mpglEnable(GL_TEXT_FRAGMENT_SHADER_ATI);
1455 break;
1456 case YUV_CONVERSION_FRAGMENT_LOOKUP3D:
1457 case YUV_CONVERSION_FRAGMENT_LOOKUP:
1458 case YUV_CONVERSION_FRAGMENT_POW:
1459 case YUV_CONVERSION_FRAGMENT:
1460 case YUV_CONVERSION_NONE:
1461 mpglEnable(GL_FRAGMENT_PROGRAM);
1462 break;
1467 * \brief disable the specified YUV conversion
1468 * \param target texture target for Y, U and V textures (e.g. GL_TEXTURE_2D)
1469 * \param type type of YUV conversion
1470 * \ingroup glconversion
1472 void glDisableYUVConversion(GLenum target, int type) {
1473 switch (YUV_CONVERSION(type)) {
1474 case YUV_CONVERSION_COMBINERS:
1475 mpglActiveTexture(GL_TEXTURE1);
1476 mpglDisable(target);
1477 mpglActiveTexture(GL_TEXTURE2);
1478 mpglDisable(target);
1479 mpglActiveTexture(GL_TEXTURE0);
1480 mpglDisable(GL_REGISTER_COMBINERS_NV);
1481 break;
1482 case YUV_CONVERSION_COMBINERS_ATI:
1483 mpglActiveTexture(GL_TEXTURE1);
1484 mpglDisable(target);
1485 mpglActiveTexture(GL_TEXTURE2);
1486 mpglDisable(target);
1487 mpglActiveTexture(GL_TEXTURE0);
1488 mpglDisable(GL_FRAGMENT_SHADER_ATI);
1489 break;
1490 case YUV_CONVERSION_TEXT_FRAGMENT:
1491 mpglDisable(GL_TEXT_FRAGMENT_SHADER_ATI);
1492 // HACK: at least the Mac OS X 10.5 PPC Radeon drivers are broken and
1493 // without this disable the texture units while the program is still
1494 // running (10.4 PPC seems to work without this though).
1495 mpglFlush();
1496 mpglActiveTexture(GL_TEXTURE1);
1497 mpglDisable(target);
1498 mpglActiveTexture(GL_TEXTURE2);
1499 mpglDisable(target);
1500 mpglActiveTexture(GL_TEXTURE0);
1501 break;
1502 case YUV_CONVERSION_FRAGMENT_LOOKUP3D:
1503 case YUV_CONVERSION_FRAGMENT_LOOKUP:
1504 case YUV_CONVERSION_FRAGMENT_POW:
1505 case YUV_CONVERSION_FRAGMENT:
1506 case YUV_CONVERSION_NONE:
1507 mpglDisable(GL_FRAGMENT_PROGRAM);
1508 break;
1512 void glEnable3DLeft(int type) {
1513 GLint buffer;
1514 switch (type) {
1515 case GL_3D_RED_CYAN:
1516 mpglColorMask(GL_TRUE, GL_FALSE, GL_FALSE, GL_FALSE);
1517 break;
1518 case GL_3D_GREEN_MAGENTA:
1519 mpglColorMask(GL_FALSE, GL_TRUE, GL_FALSE, GL_FALSE);
1520 break;
1521 case GL_3D_QUADBUFFER:
1522 mpglGetIntegerv(GL_DRAW_BUFFER, &buffer);
1523 switch (buffer) {
1524 case GL_FRONT:
1525 case GL_FRONT_LEFT:
1526 case GL_FRONT_RIGHT:
1527 buffer = GL_FRONT_LEFT;
1528 break;
1529 case GL_BACK:
1530 case GL_BACK_LEFT:
1531 case GL_BACK_RIGHT:
1532 buffer = GL_BACK_LEFT;
1533 break;
1535 mpglDrawBuffer(buffer);
1536 break;
1540 void glEnable3DRight(int type) {
1541 GLint buffer;
1542 switch (type) {
1543 case GL_3D_RED_CYAN:
1544 mpglColorMask(GL_FALSE, GL_TRUE, GL_TRUE, GL_FALSE);
1545 break;
1546 case GL_3D_GREEN_MAGENTA:
1547 mpglColorMask(GL_TRUE, GL_FALSE, GL_TRUE, GL_FALSE);
1548 break;
1549 case GL_3D_QUADBUFFER:
1550 mpglGetIntegerv(GL_DRAW_BUFFER, &buffer);
1551 switch (buffer) {
1552 case GL_FRONT:
1553 case GL_FRONT_LEFT:
1554 case GL_FRONT_RIGHT:
1555 buffer = GL_FRONT_RIGHT;
1556 break;
1557 case GL_BACK:
1558 case GL_BACK_LEFT:
1559 case GL_BACK_RIGHT:
1560 buffer = GL_BACK_RIGHT;
1561 break;
1563 mpglDrawBuffer(buffer);
1564 break;
1568 void glDisable3D(int type) {
1569 GLint buffer;
1570 switch (type) {
1571 case GL_3D_RED_CYAN:
1572 case GL_3D_GREEN_MAGENTA:
1573 mpglColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
1574 break;
1575 case GL_3D_QUADBUFFER:
1576 mpglDrawBuffer(vo_doublebuffering ? GL_BACK : GL_FRONT);
1577 mpglGetIntegerv(GL_DRAW_BUFFER, &buffer);
1578 switch (buffer) {
1579 case GL_FRONT:
1580 case GL_FRONT_LEFT:
1581 case GL_FRONT_RIGHT:
1582 buffer = GL_FRONT;
1583 break;
1584 case GL_BACK:
1585 case GL_BACK_LEFT:
1586 case GL_BACK_RIGHT:
1587 buffer = GL_BACK;
1588 break;
1590 mpglDrawBuffer(buffer);
1591 break;
1596 * \brief draw a texture part at given 2D coordinates
1597 * \param x screen top coordinate
1598 * \param y screen left coordinate
1599 * \param w screen width coordinate
1600 * \param h screen height coordinate
1601 * \param tx texture top coordinate in pixels
1602 * \param ty texture left coordinate in pixels
1603 * \param tw texture part width in pixels
1604 * \param th texture part height in pixels
1605 * \param sx width of texture in pixels
1606 * \param sy height of texture in pixels
1607 * \param rect_tex whether this texture uses texture_rectangle extension
1608 * \param is_yv12 if != 0, also draw the textures from units 1 and 2,
1609 * bits 8 - 15 and 16 - 23 specify the x and y scaling of those textures
1610 * \param flip flip the texture upside down
1611 * \ingroup gltexture
1613 void glDrawTex(GLfloat x, GLfloat y, GLfloat w, GLfloat h,
1614 GLfloat tx, GLfloat ty, GLfloat tw, GLfloat th,
1615 int sx, int sy, int rect_tex, int is_yv12, int flip) {
1616 int chroma_x_shift = (is_yv12 >> 8) & 31;
1617 int chroma_y_shift = (is_yv12 >> 16) & 31;
1618 GLfloat xscale = 1 << chroma_x_shift;
1619 GLfloat yscale = 1 << chroma_y_shift;
1620 GLfloat tx2 = tx / xscale, ty2 = ty / yscale, tw2 = tw / xscale, th2 = th / yscale;
1621 if (!rect_tex) {
1622 tx /= sx; ty /= sy; tw /= sx; th /= sy;
1623 tx2 = tx, ty2 = ty, tw2 = tw, th2 = th;
1625 if (flip) {
1626 y += h;
1627 h = -h;
1629 mpglBegin(GL_QUADS);
1630 mpglTexCoord2f(tx, ty);
1631 if (is_yv12) {
1632 mpglMultiTexCoord2f(GL_TEXTURE1, tx2, ty2);
1633 mpglMultiTexCoord2f(GL_TEXTURE2, tx2, ty2);
1635 mpglVertex2f(x, y);
1636 mpglTexCoord2f(tx, ty + th);
1637 if (is_yv12) {
1638 mpglMultiTexCoord2f(GL_TEXTURE1, tx2, ty2 + th2);
1639 mpglMultiTexCoord2f(GL_TEXTURE2, tx2, ty2 + th2);
1641 mpglVertex2f(x, y + h);
1642 mpglTexCoord2f(tx + tw, ty + th);
1643 if (is_yv12) {
1644 mpglMultiTexCoord2f(GL_TEXTURE1, tx2 + tw2, ty2 + th2);
1645 mpglMultiTexCoord2f(GL_TEXTURE2, tx2 + tw2, ty2 + th2);
1647 mpglVertex2f(x + w, y + h);
1648 mpglTexCoord2f(tx + tw, ty);
1649 if (is_yv12) {
1650 mpglMultiTexCoord2f(GL_TEXTURE1, tx2 + tw2, ty2);
1651 mpglMultiTexCoord2f(GL_TEXTURE2, tx2 + tw2, ty2);
1653 mpglVertex2f(x + w, y);
1654 mpglEnd();
1657 #ifdef CONFIG_GL_WIN32
1658 #include "w32_common.h"
1660 * \brief little helper since wglGetProcAddress definition does not fit our
1661 * getProcAddress
1662 * \param procName name of function to look up
1663 * \return function pointer returned by wglGetProcAddress
1665 static void *w32gpa(const GLubyte *procName) {
1666 HMODULE oglmod;
1667 void *res = wglGetProcAddress(procName);
1668 if (res) return res;
1669 oglmod = GetModuleHandle("opengl32.dll");
1670 return GetProcAddress(oglmod, procName);
1673 static int setGlWindow_w32(MPGLContext *ctx)
1675 HWND win = vo_w32_window;
1676 int *vinfo = &ctx->vinfo.w32;
1677 HGLRC *context = &ctx->context.w32;
1678 int new_vinfo;
1679 HDC windc = vo_w32_get_dc(win);
1680 HGLRC new_context = 0;
1681 int keep_context = 0;
1682 int res = SET_WINDOW_FAILED;
1684 // should only be needed when keeping context, but not doing glFinish
1685 // can cause flickering even when we do not keep it.
1686 if (*context)
1687 mpglFinish();
1688 new_vinfo = GetPixelFormat(windc);
1689 if (*context && *vinfo && new_vinfo && *vinfo == new_vinfo) {
1690 // we can keep the wglContext
1691 new_context = *context;
1692 keep_context = 1;
1693 } else {
1694 // create a context
1695 new_context = wglCreateContext(windc);
1696 if (!new_context) {
1697 mp_msg(MSGT_VO, MSGL_FATAL, "[gl] Could not create GL context!\n");
1698 goto out;
1702 // set context
1703 if (!wglMakeCurrent(windc, new_context)) {
1704 mp_msg(MSGT_VO, MSGL_FATAL, "[gl] Could not set GL context!\n");
1705 if (!keep_context) {
1706 wglDeleteContext(new_context);
1708 goto out;
1711 // set new values
1712 vo_w32_window = win;
1714 RECT rect;
1715 GetClientRect(win, &rect);
1716 vo_dwidth = rect.right;
1717 vo_dheight = rect.bottom;
1719 if (!keep_context) {
1720 if (*context)
1721 wglDeleteContext(*context);
1722 *context = new_context;
1723 *vinfo = new_vinfo;
1724 getFunctions(w32gpa, NULL);
1726 // and inform that reinit is neccessary
1727 res = SET_WINDOW_REINIT;
1728 } else
1729 res = SET_WINDOW_OK;
1731 out:
1732 vo_w32_release_dc(win, windc);
1733 return res;
1736 static void releaseGlContext_w32(MPGLContext *ctx) {
1737 int *vinfo = &ctx->vinfo.w32;
1738 HGLRC *context = &ctx->context.w32;
1739 *vinfo = 0;
1740 if (*context) {
1741 wglMakeCurrent(0, 0);
1742 wglDeleteContext(*context);
1744 *context = 0;
1747 static void swapGlBuffers_w32(MPGLContext *ctx) {
1748 HDC vo_hdc = vo_w32_get_dc(vo_w32_window);
1749 SwapBuffers(vo_hdc);
1750 vo_w32_release_dc(vo_w32_window, vo_hdc);
1752 #endif
1753 #ifdef CONFIG_GL_X11
1754 #include "x11_common.h"
1757 * \brief Returns the XVisualInfo associated with Window win.
1758 * \param win Window whose XVisualInfo is returne.
1759 * \return XVisualInfo of the window. Caller must use XFree to free it.
1761 static XVisualInfo *getWindowVisualInfo(Window win) {
1762 XWindowAttributes xw_attr;
1763 XVisualInfo vinfo_template;
1764 int tmp;
1765 XGetWindowAttributes(mDisplay, win, &xw_attr);
1766 vinfo_template.visualid = XVisualIDFromVisual(xw_attr.visual);
1767 return XGetVisualInfo(mDisplay, VisualIDMask, &vinfo_template, &tmp);
1770 static void appendstr(char **dst, const char *str)
1772 int newsize;
1773 char *newstr;
1774 if (!str)
1775 return;
1776 newsize = strlen(*dst) + 1 + strlen(str) + 1;
1777 newstr = realloc(*dst, newsize);
1778 if (!newstr)
1779 return;
1780 *dst = newstr;
1781 strcat(*dst, " ");
1782 strcat(*dst, str);
1786 * \brief Changes the window in which video is displayed.
1787 * If possible only transfers the context to the new window, otherwise
1788 * creates a new one, which must be initialized by the caller.
1789 * \param vinfo Currently used visual.
1790 * \param context Currently used context.
1791 * \param win window that should be used for drawing.
1792 * \return one of SET_WINDOW_FAILED, SET_WINDOW_OK or SET_WINDOW_REINIT.
1793 * In case of SET_WINDOW_REINIT the context could not be transfered
1794 * and the caller must initialize it correctly.
1795 * \ingroup glcontext
1797 static int setGlWindow_x11(MPGLContext *ctx)
1799 XVisualInfo **vinfo = &ctx->vinfo.x11;
1800 GLXContext *context = &ctx->context.x11;
1801 Window win = vo_window;
1802 XVisualInfo *new_vinfo;
1803 GLXContext new_context = NULL;
1804 int keep_context = 0;
1806 // should only be needed when keeping context, but not doing glFinish
1807 // can cause flickering even when we do not keep it.
1808 if (*context)
1809 mpglFinish();
1810 new_vinfo = getWindowVisualInfo(win);
1811 if (*context && *vinfo && new_vinfo &&
1812 (*vinfo)->visualid == new_vinfo->visualid) {
1813 // we can keep the GLXContext
1814 new_context = *context;
1815 XFree(new_vinfo);
1816 new_vinfo = *vinfo;
1817 keep_context = 1;
1818 } else {
1819 // create a context
1820 new_context = glXCreateContext(mDisplay, new_vinfo, NULL, True);
1821 if (!new_context) {
1822 mp_msg(MSGT_VO, MSGL_FATAL, "[gl] Could not create GLX context!\n");
1823 XFree(new_vinfo);
1824 return SET_WINDOW_FAILED;
1828 // set context
1829 if (!glXMakeCurrent(mDisplay, vo_window, new_context)) {
1830 mp_msg(MSGT_VO, MSGL_FATAL, "[gl] Could not set GLX context!\n");
1831 if (!keep_context) {
1832 glXDestroyContext(mDisplay, new_context);
1833 XFree(new_vinfo);
1835 return SET_WINDOW_FAILED;
1838 // set new values
1839 vo_window = win;
1840 vo_x11_update_geometry();
1841 if (!keep_context) {
1842 void *(*getProcAddress)(const GLubyte *);
1843 const char *(*glXExtStr)(Display *, int);
1844 char *glxstr = strdup("");
1845 if (*context)
1846 glXDestroyContext(mDisplay, *context);
1847 *context = new_context;
1848 if (*vinfo)
1849 XFree(*vinfo);
1850 *vinfo = new_vinfo;
1851 getProcAddress = getdladdr("glXGetProcAddress");
1852 if (!getProcAddress)
1853 getProcAddress = getdladdr("glXGetProcAddressARB");
1854 glXExtStr = getdladdr("glXQueryExtensionsString");
1855 if (glXExtStr)
1856 appendstr(&glxstr, glXExtStr(mDisplay, DefaultScreen(mDisplay)));
1857 glXExtStr = getdladdr("glXGetClientString");
1858 if (glXExtStr)
1859 appendstr(&glxstr, glXExtStr(mDisplay, GLX_EXTENSIONS));
1860 glXExtStr = getdladdr("glXGetServerString");
1861 if (glXExtStr)
1862 appendstr(&glxstr, glXExtStr(mDisplay, GLX_EXTENSIONS));
1864 getFunctions(getProcAddress, glxstr);
1865 if (!mpglGenPrograms && mpglGetString &&
1866 getProcAddress &&
1867 strstr(mpglGetString(GL_EXTENSIONS), "GL_ARB_vertex_program")) {
1868 mp_msg(MSGT_VO, MSGL_WARN, "Broken glXGetProcAddress detected, trying workaround\n");
1869 getFunctions(NULL, glxstr);
1871 free(glxstr);
1873 // and inform that reinit is neccessary
1874 return SET_WINDOW_REINIT;
1876 return SET_WINDOW_OK;
1880 * \brief free the VisualInfo and GLXContext of an OpenGL context.
1881 * \ingroup glcontext
1883 static void releaseGlContext_x11(MPGLContext *ctx) {
1884 XVisualInfo **vinfo = &ctx->vinfo.x11;
1885 GLXContext *context = &ctx->context.x11;
1886 if (*vinfo)
1887 XFree(*vinfo);
1888 *vinfo = NULL;
1889 if (*context)
1891 mpglFinish();
1892 glXMakeCurrent(mDisplay, None, NULL);
1893 glXDestroyContext(mDisplay, *context);
1895 *context = 0;
1898 static void swapGlBuffers_x11(MPGLContext *ctx) {
1899 glXSwapBuffers(mDisplay, vo_window);
1902 static int x11_check_events(void) {
1903 return vo_x11_check_events(mDisplay);
1906 static void gl_update_xinerama_info(void)
1908 update_xinerama_info(global_vo);
1911 static void gl_border(void)
1913 vo_x11_border();
1916 static void gl_fullscreen(void)
1918 vo_x11_fullscreen();
1921 static void gl_ontop(void)
1923 vo_x11_ontop();
1925 #endif
1927 #ifdef CONFIG_GL_SDL
1928 #include "sdl_common.h"
1930 static void swapGlBuffers_sdl(MPGLContext *ctx) {
1931 SDL_GL_SwapBuffers();
1934 static void *sdlgpa(const GLubyte *name) {
1935 return SDL_GL_GetProcAddress(name);
1938 static int setGlWindow_sdl(MPGLContext *ctx) {
1939 if (sdl_set_mode(0, SDL_OPENGL | SDL_RESIZABLE) < 0)
1940 return SET_WINDOW_FAILED;
1941 SDL_GL_LoadLibrary(NULL);
1942 getFunctions(sdlgpa, NULL);
1943 return SET_WINDOW_OK;
1946 static int sdl_check_events(void) {
1947 int res = 0;
1948 SDL_Event event;
1949 while (SDL_PollEvent(&event)) {
1950 res |= sdl_default_handle_event(&event);
1952 // poll "events" from within MPlayer code
1953 res |= sdl_default_handle_event(NULL);
1954 if (res & VO_EVENT_RESIZE)
1955 sdl_set_mode(0, SDL_OPENGL | SDL_RESIZABLE);
1956 return res;
1959 #endif
1961 static int setGlWindow_dummy(MPGLContext *ctx) {
1962 getFunctions(NULL, NULL);
1963 return SET_WINDOW_OK;
1966 static void releaseGlContext_dummy(MPGLContext *ctx) {
1969 static int dummy_check_events(void) {
1970 return 0;
1973 static void dummy_update_xinerama_info(void) {
1974 if (vo_screenwidth <= 0 || vo_screenheight <= 0) {
1975 mp_msg(MSGT_VO, MSGL_ERR, "You must specify the screen dimensions "
1976 "with -screenw and -screenh\n");
1977 vo_screenwidth = 1280;
1978 vo_screenheight = 768;
1980 aspect_save_screenres(vo_screenwidth, vo_screenheight);
1983 int init_mpglcontext(MPGLContext *ctx, enum MPGLType type) {
1984 if (type == GLTYPE_AUTO) {
1985 int res = init_mpglcontext(ctx, GLTYPE_W32);
1986 if (res) return res;
1987 res = init_mpglcontext(ctx, GLTYPE_X11);
1988 if (res) return res;
1989 res = init_mpglcontext(ctx, GLTYPE_SDL);
1990 return res;
1992 memset(ctx, 0, sizeof(*ctx));
1993 ctx->setGlWindow = setGlWindow_dummy;
1994 ctx->releaseGlContext = releaseGlContext_dummy;
1995 ctx->update_xinerama_info = dummy_update_xinerama_info;
1996 ctx->check_events = dummy_check_events;
1997 ctx->type = type;
1998 switch (ctx->type) {
1999 #ifdef CONFIG_GL_WIN32
2000 case GLTYPE_W32:
2001 ctx->setGlWindow = setGlWindow_w32;
2002 ctx->releaseGlContext = releaseGlContext_w32;
2003 ctx->swapGlBuffers = swapGlBuffers_w32;
2004 ctx->update_xinerama_info = w32_update_xinerama_info;
2005 ctx->border = vo_w32_border;
2006 ctx->check_events = vo_w32_check_events;
2007 ctx->fullscreen = vo_w32_fullscreen;
2008 ctx->ontop = vo_w32_ontop;
2009 return vo_w32_init();
2010 #endif
2011 #ifdef CONFIG_GL_X11
2012 case GLTYPE_X11:
2013 ctx->setGlWindow = setGlWindow_x11;
2014 ctx->releaseGlContext = releaseGlContext_x11;
2015 ctx->swapGlBuffers = swapGlBuffers_x11;
2016 ctx->update_xinerama_info = gl_update_xinerama_info;
2017 ctx->border = gl_border;
2018 ctx->check_events = x11_check_events;
2019 ctx->fullscreen = gl_fullscreen;
2020 ctx->ontop = gl_ontop;
2021 return vo_init();
2022 #endif
2023 #ifdef CONFIG_GL_SDL
2024 case GLTYPE_SDL:
2025 SDL_Init(SDL_INIT_VIDEO);
2026 ctx->setGlWindow = setGlWindow_sdl;
2027 ctx->swapGlBuffers = swapGlBuffers_sdl;
2028 ctx->check_events = sdl_check_events;
2029 ctx->fullscreen = vo_sdl_fullscreen;
2030 return vo_sdl_init();
2031 #endif
2032 default:
2033 return 0;
2037 void uninit_mpglcontext(MPGLContext *ctx) {
2038 ctx->releaseGlContext(ctx);
2039 switch (ctx->type) {
2040 #ifdef CONFIG_GL_WIN32
2041 case GLTYPE_W32:
2042 vo_w32_uninit();
2043 break;
2044 #endif
2045 #ifdef CONFIG_GL_X11
2046 case GLTYPE_X11:
2047 vo_x11_uninit();
2048 break;
2049 #endif
2050 #ifdef CONFIG_GL_SDL
2051 case GLTYPE_SDL:
2052 vo_sdl_uninit();
2053 break;
2054 #endif
2056 memset(ctx, 0, sizeof(*ctx));