Fix vf_tcdump's compilation
[mplayer/kovensky.git] / libvo / gl_common.c
blobce65873349cbbf7f6bdc43c04387a6d9b2afee71
1 /*
2 * common OpenGL routines
4 * copyleft (C) 2005 Reimar Döffinger <Reimar.Doeffinger@stud.uni-karlsruhe.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.
25 /**
26 * \file gl_common.c
27 * \brief OpenGL helper functions used by vo_gl.c and vo_gl2.c
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <ctype.h>
34 #include <math.h>
35 #include "old_vo_defines.h"
36 #include "gl_common.h"
37 #include "csputils.h"
39 void (GLAPIENTRY *Begin)(GLenum);
40 void (GLAPIENTRY *End)(void);
41 void (GLAPIENTRY *Viewport)(GLint, GLint, GLsizei, GLsizei);
42 void (GLAPIENTRY *MatrixMode)(GLenum);
43 void (GLAPIENTRY *LoadIdentity)(void);
44 void (GLAPIENTRY *Translated)(double, double, double);
45 void (GLAPIENTRY *Scaled)(double, double, double);
46 void (GLAPIENTRY *Ortho)(double, double, double, double, double, double);
47 void (GLAPIENTRY *Frustum)(double, double, double, double, double, double);
48 void (GLAPIENTRY *PushMatrix)(void);
49 void (GLAPIENTRY *PopMatrix)(void);
50 void (GLAPIENTRY *Clear)(GLbitfield);
51 GLuint (GLAPIENTRY *GenLists)(GLsizei);
52 void (GLAPIENTRY *DeleteLists)(GLuint, GLsizei);
53 void (GLAPIENTRY *NewList)(GLuint, GLenum);
54 void (GLAPIENTRY *EndList)(void);
55 void (GLAPIENTRY *CallList)(GLuint);
56 void (GLAPIENTRY *CallLists)(GLsizei, GLenum, const GLvoid *);
57 void (GLAPIENTRY *GenTextures)(GLsizei, GLuint *);
58 void (GLAPIENTRY *DeleteTextures)(GLsizei, const GLuint *);
59 void (GLAPIENTRY *TexEnvf)(GLenum, GLenum, GLfloat);
60 void (GLAPIENTRY *TexEnvi)(GLenum, GLenum, GLint);
61 void (GLAPIENTRY *Color4ub)(GLubyte, GLubyte, GLubyte, GLubyte);
62 void (GLAPIENTRY *Color3f)(GLfloat, GLfloat, GLfloat);
63 void (GLAPIENTRY *Color4f)(GLfloat, GLfloat, GLfloat, GLfloat);
64 void (GLAPIENTRY *ClearColor)(GLclampf, GLclampf, GLclampf, GLclampf);
65 void (GLAPIENTRY *ClearDepth)(GLclampd);
66 void (GLAPIENTRY *DepthFunc)(GLenum);
67 void (GLAPIENTRY *Enable)(GLenum);
68 void (GLAPIENTRY *Disable)(GLenum);
69 const GLubyte *(GLAPIENTRY *GetString)(GLenum);
70 void (GLAPIENTRY *DrawBuffer)(GLenum);
71 void (GLAPIENTRY *DepthMask)(GLboolean);
72 void (GLAPIENTRY *BlendFunc)(GLenum, GLenum);
73 void (GLAPIENTRY *Flush)(void);
74 void (GLAPIENTRY *Finish)(void);
75 void (GLAPIENTRY *PixelStorei)(GLenum, GLint);
76 void (GLAPIENTRY *TexImage1D)(GLenum, GLint, GLint, GLsizei, GLint, GLenum, GLenum, const GLvoid *);
77 void (GLAPIENTRY *TexImage2D)(GLenum, GLint, GLint, GLsizei, GLsizei, GLint, GLenum, GLenum, const GLvoid *);
78 void (GLAPIENTRY *TexSubImage2D)(GLenum, GLint, GLint, GLint, GLsizei, GLsizei, GLenum, GLenum, const GLvoid *);
79 void (GLAPIENTRY *TexParameteri)(GLenum, GLenum, GLint);
80 void (GLAPIENTRY *TexParameterf)(GLenum, GLenum, GLfloat);
81 void (GLAPIENTRY *TexParameterfv)(GLenum, GLenum, const GLfloat *);
82 void (GLAPIENTRY *TexCoord2f)(GLfloat, GLfloat);
83 void (GLAPIENTRY *Vertex2f)(GLfloat, GLfloat);
84 void (GLAPIENTRY *Vertex3f)(GLfloat, GLfloat, GLfloat);
85 void (GLAPIENTRY *Normal3f)(GLfloat, GLfloat, GLfloat);
86 void (GLAPIENTRY *Lightfv)(GLenum, GLenum, const GLfloat *);
87 void (GLAPIENTRY *ColorMaterial)(GLenum, GLenum);
88 void (GLAPIENTRY *ShadeModel)(GLenum);
89 void (GLAPIENTRY *GetIntegerv)(GLenum, GLint *);
91 /**
92 * \defgroup glextfunctions OpenGL extension functions
94 * the pointers to these functions are acquired when the OpenGL
95 * context is created
96 * \{
98 void (GLAPIENTRY *GenBuffers)(GLsizei, GLuint *);
99 void (GLAPIENTRY *DeleteBuffers)(GLsizei, const GLuint *);
100 void (GLAPIENTRY *BindBuffer)(GLenum, GLuint);
101 GLvoid* (GLAPIENTRY *MapBuffer)(GLenum, GLenum);
102 GLboolean (GLAPIENTRY *UnmapBuffer)(GLenum);
103 void (GLAPIENTRY *BufferData)(GLenum, intptr_t, const GLvoid *, GLenum);
104 void (GLAPIENTRY *CombinerParameterfv)(GLenum, const GLfloat *);
105 void (GLAPIENTRY *CombinerParameteri)(GLenum, GLint);
106 void (GLAPIENTRY *CombinerInput)(GLenum, GLenum, GLenum, GLenum, GLenum,
107 GLenum);
108 void (GLAPIENTRY *CombinerOutput)(GLenum, GLenum, GLenum, GLenum, GLenum,
109 GLenum, GLenum, GLboolean, GLboolean,
110 GLboolean);
111 void (GLAPIENTRY *BeginFragmentShader)(void);
112 void (GLAPIENTRY *EndFragmentShader)(void);
113 void (GLAPIENTRY *SampleMap)(GLuint, GLuint, GLenum);
114 void (GLAPIENTRY *ColorFragmentOp2)(GLenum, GLuint, GLuint, GLuint, GLuint,
115 GLuint, GLuint, GLuint, GLuint, GLuint);
116 void (GLAPIENTRY *ColorFragmentOp3)(GLenum, GLuint, GLuint, GLuint, GLuint,
117 GLuint, GLuint, GLuint, GLuint, GLuint,
118 GLuint, GLuint, GLuint);
119 void (GLAPIENTRY *SetFragmentShaderConstant)(GLuint, const GLfloat *);
120 void (GLAPIENTRY *ActiveTexture)(GLenum);
121 void (GLAPIENTRY *BindTexture)(GLenum, GLuint);
122 void (GLAPIENTRY *MultiTexCoord2f)(GLenum, GLfloat, GLfloat);
123 void (GLAPIENTRY *GenPrograms)(GLsizei, GLuint *);
124 void (GLAPIENTRY *DeletePrograms)(GLsizei, const GLuint *);
125 void (GLAPIENTRY *BindProgram)(GLenum, GLuint);
126 void (GLAPIENTRY *ProgramString)(GLenum, GLenum, GLsizei, const GLvoid *);
127 void (GLAPIENTRY *GetProgramiv)(GLenum, GLenum, GLint *);
128 void (GLAPIENTRY *ProgramEnvParameter4f)(GLenum, GLuint, GLfloat, GLfloat,
129 GLfloat, GLfloat);
130 int (GLAPIENTRY *SwapInterval)(int);
131 void (GLAPIENTRY *TexImage3D)(GLenum, GLint, GLenum, GLsizei, GLsizei, GLsizei,
132 GLint, GLenum, GLenum, const GLvoid *);
133 void* (GLAPIENTRY *AllocateMemoryMESA)(void *, int, size_t, float, float, float);
134 void (GLAPIENTRY *FreeMemoryMESA)(void *, int, void *);
135 /** \} */ // end of glextfunctions group
137 //! \defgroup glgeneral OpenGL general helper functions
139 //! \defgroup glcontext OpenGL context management helper functions
141 //! \defgroup gltexture OpenGL texture handling helper functions
143 //! \defgroup glconversion OpenGL conversion helper functions
145 static GLint hqtexfmt;
148 * \brief adjusts the GL_UNPACK_ALIGNMENT to fit the stride.
149 * \param stride number of bytes per line for which alignment should fit.
150 * \ingroup glgeneral
152 void glAdjustAlignment(int stride) {
153 GLint gl_alignment;
154 if (stride % 8 == 0)
155 gl_alignment=8;
156 else if (stride % 4 == 0)
157 gl_alignment=4;
158 else if (stride % 2 == 0)
159 gl_alignment=2;
160 else
161 gl_alignment=1;
162 PixelStorei(GL_UNPACK_ALIGNMENT, gl_alignment);
165 struct gl_name_map_struct {
166 GLint value;
167 const char *name;
170 #undef MAP
171 #define MAP(a) {a, #a}
172 //! mapping table for the glValName function
173 static const struct gl_name_map_struct gl_name_map[] = {
174 // internal format
175 MAP(GL_R3_G3_B2), MAP(GL_RGB4), MAP(GL_RGB5), MAP(GL_RGB8),
176 MAP(GL_RGB10), MAP(GL_RGB12), MAP(GL_RGB16), MAP(GL_RGBA2),
177 MAP(GL_RGBA4), MAP(GL_RGB5_A1), MAP(GL_RGBA8), MAP(GL_RGB10_A2),
178 MAP(GL_RGBA12), MAP(GL_RGBA16), MAP(GL_LUMINANCE8),
180 // format
181 MAP(GL_RGB), MAP(GL_RGBA), MAP(GL_RED), MAP(GL_GREEN), MAP(GL_BLUE),
182 MAP(GL_ALPHA), MAP(GL_LUMINANCE), MAP(GL_LUMINANCE_ALPHA),
183 MAP(GL_COLOR_INDEX),
184 // rest 1.2 only
185 MAP(GL_BGR), MAP(GL_BGRA),
187 //type
188 MAP(GL_BYTE), MAP(GL_UNSIGNED_BYTE), MAP(GL_SHORT), MAP(GL_UNSIGNED_SHORT),
189 MAP(GL_INT), MAP(GL_UNSIGNED_INT), MAP(GL_FLOAT), MAP(GL_DOUBLE),
190 MAP(GL_2_BYTES), MAP(GL_3_BYTES), MAP(GL_4_BYTES),
191 // rest 1.2 only
192 MAP(GL_UNSIGNED_BYTE_3_3_2), MAP(GL_UNSIGNED_BYTE_2_3_3_REV),
193 MAP(GL_UNSIGNED_SHORT_5_6_5), MAP(GL_UNSIGNED_SHORT_5_6_5_REV),
194 MAP(GL_UNSIGNED_SHORT_4_4_4_4), MAP(GL_UNSIGNED_SHORT_4_4_4_4_REV),
195 MAP(GL_UNSIGNED_SHORT_5_5_5_1), MAP(GL_UNSIGNED_SHORT_1_5_5_5_REV),
196 MAP(GL_UNSIGNED_INT_8_8_8_8), MAP(GL_UNSIGNED_INT_8_8_8_8_REV),
197 MAP(GL_UNSIGNED_INT_10_10_10_2), MAP(GL_UNSIGNED_INT_2_10_10_10_REV),
198 {0, 0}
200 #undef MAP
203 * \brief return the name of an OpenGL constant
204 * \param value the constant
205 * \return name of the constant or "Unknown format!"
206 * \ingroup glgeneral
208 const char *glValName(GLint value)
210 int i = 0;
212 while (gl_name_map[i].name) {
213 if (gl_name_map[i].value == value)
214 return gl_name_map[i].name;
215 i++;
217 return "Unknown format!";
220 //! always return this format as internal texture format in glFindFormat
221 #define TEXTUREFORMAT_ALWAYS GL_RGB8
222 #undef TEXTUREFORMAT_ALWAYS
225 * \brief find the OpenGL settings coresponding to format.
227 * All parameters may be NULL.
228 * \param fmt MPlayer format to analyze.
229 * \param bpp [OUT] bits per pixel of that format.
230 * \param gl_texfmt [OUT] internal texture format that fits the
231 * image format, not necessarily the best for performance.
232 * \param gl_format [OUT] OpenGL format for this image format.
233 * \param gl_type [OUT] OpenGL type for this image format.
234 * \return 1 if format is supported by OpenGL, 0 if not.
235 * \ingroup gltexture
237 int glFindFormat(uint32_t fmt, int *bpp, GLint *gl_texfmt,
238 GLenum *gl_format, GLenum *gl_type)
240 int supported = 1;
241 int dummy1;
242 GLenum dummy2;
243 GLint dummy3;
244 if (!bpp) bpp = &dummy1;
245 if (!gl_texfmt) gl_texfmt = &dummy3;
246 if (!gl_format) gl_format = &dummy2;
247 if (!gl_type) gl_type = &dummy2;
249 if (mp_get_chroma_shift(fmt, NULL, NULL)) {
250 // reduce the possible cases a bit
251 if (IMGFMT_IS_YUVP16_LE(fmt))
252 fmt = IMGFMT_420P16_LE;
253 else if (IMGFMT_IS_YUVP16_BE(fmt))
254 fmt = IMGFMT_420P16_BE;
255 else
256 fmt = IMGFMT_YV12;
259 *bpp = IMGFMT_IS_BGR(fmt)?IMGFMT_BGR_DEPTH(fmt):IMGFMT_RGB_DEPTH(fmt);
260 *gl_texfmt = 3;
261 switch (fmt) {
262 case IMGFMT_RGB48NE:
263 *gl_format = GL_RGB;
264 *gl_type = GL_UNSIGNED_SHORT;
265 break;
266 case IMGFMT_RGB24:
267 *gl_format = GL_RGB;
268 *gl_type = GL_UNSIGNED_BYTE;
269 break;
270 case IMGFMT_RGBA:
271 *gl_texfmt = 4;
272 *gl_format = GL_RGBA;
273 *gl_type = GL_UNSIGNED_BYTE;
274 break;
275 case IMGFMT_420P16:
276 supported = 0; // no native YUV support
277 *gl_texfmt = 1;
278 *bpp = 16;
279 *gl_format = GL_LUMINANCE;
280 *gl_type = GL_UNSIGNED_SHORT;
281 break;
282 case IMGFMT_YV12:
283 supported = 0; // no native YV12 support
284 case IMGFMT_Y800:
285 case IMGFMT_Y8:
286 *gl_texfmt = 1;
287 *bpp = 8;
288 *gl_format = GL_LUMINANCE;
289 *gl_type = GL_UNSIGNED_BYTE;
290 break;
291 case IMGFMT_UYVY:
292 case IMGFMT_YUY2:
293 *gl_texfmt = GL_YCBCR_MESA;
294 *bpp = 16;
295 *gl_format = GL_YCBCR_MESA;
296 *gl_type = fmt == IMGFMT_UYVY ? GL_UNSIGNED_SHORT_8_8 : GL_UNSIGNED_SHORT_8_8_REV;
297 break;
298 #if 0
299 // we do not support palettized formats, although the format the
300 // swscale produces works
301 case IMGFMT_RGB8:
302 gl_format = GL_RGB;
303 gl_type = GL_UNSIGNED_BYTE_2_3_3_REV;
304 break;
305 #endif
306 case IMGFMT_RGB15:
307 *gl_format = GL_RGBA;
308 *gl_type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
309 break;
310 case IMGFMT_RGB16:
311 *gl_format = GL_RGB;
312 *gl_type = GL_UNSIGNED_SHORT_5_6_5_REV;
313 break;
314 #if 0
315 case IMGFMT_BGR8:
316 // special case as red and blue have a differen number of bits.
317 // GL_BGR and GL_UNSIGNED_BYTE_3_3_2 isn't supported at least
318 // by nVidia drivers, and in addition would give more bits to
319 // blue than to red, which isn't wanted
320 gl_format = GL_RGB;
321 gl_type = GL_UNSIGNED_BYTE_3_3_2;
322 break;
323 #endif
324 case IMGFMT_BGR15:
325 *gl_format = GL_BGRA;
326 *gl_type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
327 break;
328 case IMGFMT_BGR16:
329 *gl_format = GL_RGB;
330 *gl_type = GL_UNSIGNED_SHORT_5_6_5;
331 break;
332 case IMGFMT_BGR24:
333 *gl_format = GL_BGR;
334 *gl_type = GL_UNSIGNED_BYTE;
335 break;
336 case IMGFMT_BGRA:
337 *gl_texfmt = 4;
338 *gl_format = GL_BGRA;
339 *gl_type = GL_UNSIGNED_BYTE;
340 break;
341 default:
342 *gl_texfmt = 4;
343 *gl_format = GL_RGBA;
344 *gl_type = GL_UNSIGNED_BYTE;
345 supported = 0;
347 #ifdef TEXTUREFORMAT_ALWAYS
348 *gl_texfmt = TEXTUREFORMAT_ALWAYS;
349 #endif
350 return supported;
353 static void *setNull(const GLubyte *s) {
354 return NULL;
357 typedef struct {
358 void *funcptr;
359 const char *extstr;
360 const char *funcnames[7];
361 void *fallback;
362 } extfunc_desc_t;
364 #define DEF_FUNC_DESC(name) {&name, NULL, {"gl"#name, NULL}, gl ##name}
365 static const extfunc_desc_t extfuncs[] = {
366 // these aren't extension functions but we query them anyway to allow
367 // different "backends" with one binary
368 DEF_FUNC_DESC(Begin),
369 DEF_FUNC_DESC(End),
370 DEF_FUNC_DESC(Viewport),
371 DEF_FUNC_DESC(MatrixMode),
372 DEF_FUNC_DESC(LoadIdentity),
373 DEF_FUNC_DESC(Translated),
374 DEF_FUNC_DESC(Scaled),
375 DEF_FUNC_DESC(Ortho),
376 DEF_FUNC_DESC(Frustum),
377 DEF_FUNC_DESC(PushMatrix),
378 DEF_FUNC_DESC(PopMatrix),
379 DEF_FUNC_DESC(Clear),
380 DEF_FUNC_DESC(GenLists),
381 DEF_FUNC_DESC(DeleteLists),
382 DEF_FUNC_DESC(NewList),
383 DEF_FUNC_DESC(EndList),
384 DEF_FUNC_DESC(CallList),
385 DEF_FUNC_DESC(CallLists),
386 DEF_FUNC_DESC(GenTextures),
387 DEF_FUNC_DESC(DeleteTextures),
388 DEF_FUNC_DESC(TexEnvf),
389 DEF_FUNC_DESC(TexEnvi),
390 DEF_FUNC_DESC(Color4ub),
391 DEF_FUNC_DESC(Color3f),
392 DEF_FUNC_DESC(Color4f),
393 DEF_FUNC_DESC(ClearColor),
394 DEF_FUNC_DESC(ClearDepth),
395 DEF_FUNC_DESC(DepthFunc),
396 DEF_FUNC_DESC(Enable),
397 DEF_FUNC_DESC(Disable),
398 DEF_FUNC_DESC(DrawBuffer),
399 DEF_FUNC_DESC(DepthMask),
400 DEF_FUNC_DESC(BlendFunc),
401 DEF_FUNC_DESC(Flush),
402 DEF_FUNC_DESC(Finish),
403 DEF_FUNC_DESC(PixelStorei),
404 DEF_FUNC_DESC(TexImage1D),
405 DEF_FUNC_DESC(TexImage2D),
406 DEF_FUNC_DESC(TexSubImage2D),
407 DEF_FUNC_DESC(TexParameteri),
408 DEF_FUNC_DESC(TexParameterf),
409 DEF_FUNC_DESC(TexParameterfv),
410 DEF_FUNC_DESC(TexCoord2f),
411 DEF_FUNC_DESC(Vertex2f),
412 DEF_FUNC_DESC(Vertex3f),
413 DEF_FUNC_DESC(Normal3f),
414 DEF_FUNC_DESC(Lightfv),
415 DEF_FUNC_DESC(ColorMaterial),
416 DEF_FUNC_DESC(ShadeModel),
417 DEF_FUNC_DESC(GetIntegerv),
419 // here start the real extensions
420 {&GenBuffers, NULL, {"glGenBuffers", "glGenBuffersARB", NULL}},
421 {&DeleteBuffers, NULL, {"glDeleteBuffers", "glDeleteBuffersARB", NULL}},
422 {&BindBuffer, NULL, {"glBindBuffer", "glBindBufferARB", NULL}},
423 {&MapBuffer, NULL, {"glMapBuffer", "glMapBufferARB", NULL}},
424 {&UnmapBuffer, NULL, {"glUnmapBuffer", "glUnmapBufferARB", NULL}},
425 {&BufferData, NULL, {"glBufferData", "glBufferDataARB", NULL}},
426 {&CombinerParameterfv, "NV_register_combiners", {"glCombinerParameterfv", "glCombinerParameterfvNV", NULL}},
427 {&CombinerParameteri, "NV_register_combiners", {"glCombinerParameteri", "glCombinerParameteriNV", NULL}},
428 {&CombinerInput, "NV_register_combiners", {"glCombinerInput", "glCombinerInputNV", NULL}},
429 {&CombinerOutput, "NV_register_combiners", {"glCombinerOutput", "glCombinerOutputNV", NULL}},
430 {&BeginFragmentShader, "ATI_fragment_shader", {"glBeginFragmentShaderATI", NULL}},
431 {&EndFragmentShader, "ATI_fragment_shader", {"glEndFragmentShaderATI", NULL}},
432 {&SampleMap, "ATI_fragment_shader", {"glSampleMapATI", NULL}},
433 {&ColorFragmentOp2, "ATI_fragment_shader", {"glColorFragmentOp2ATI", NULL}},
434 {&ColorFragmentOp3, "ATI_fragment_shader", {"glColorFragmentOp3ATI", NULL}},
435 {&SetFragmentShaderConstant, "ATI_fragment_shader", {"glSetFragmentShaderConstantATI", NULL}},
436 {&ActiveTexture, NULL, {"glActiveTexture", "glActiveTextureARB", NULL}},
437 {&BindTexture, NULL, {"glBindTexture", "glBindTextureARB", "glBindTextureEXT", NULL}},
438 {&MultiTexCoord2f, NULL, {"glMultiTexCoord2f", "glMultiTexCoord2fARB", NULL}},
439 {&GenPrograms, "_program", {"glGenProgramsARB", NULL}},
440 {&DeletePrograms, "_program", {"glDeleteProgramsARB", NULL}},
441 {&BindProgram, "_program", {"glBindProgramARB", NULL}},
442 {&ProgramString, "_program", {"glProgramStringARB", NULL}},
443 {&GetProgramiv, "_program", {"glGetProgramivARB", NULL}},
444 {&ProgramEnvParameter4f, "_program", {"glProgramEnvParameter4fARB", NULL}},
445 {&SwapInterval, "_swap_control", {"glXSwapIntervalSGI", "glXSwapInterval", "wglSwapIntervalSGI", "wglSwapInterval", "wglSwapIntervalEXT", NULL}},
446 {&TexImage3D, NULL, {"glTexImage3D", NULL}},
447 {&AllocateMemoryMESA, "GLX_MESA_allocate_memory", {"glXAllocateMemoryMESA", NULL}},
448 {&FreeMemoryMESA, "GLX_MESA_allocate_memory", {"glXFreeMemoryMESA", NULL}},
449 {NULL}
453 * \brief find the function pointers of some useful OpenGL extensions
454 * \param getProcAddress function to resolve function names, may be NULL
455 * \param ext2 an extra extension string
457 static void getFunctions(void *(*getProcAddress)(const GLubyte *),
458 const char *ext2) {
459 const extfunc_desc_t *dsc;
460 const char *extensions;
461 char *allexts;
463 if (!getProcAddress)
464 getProcAddress = setNull;
466 // special case, we need glGetString before starting to find the other functions
467 GetString = getProcAddress("glGetString");
468 if (!GetString)
469 GetString = glGetString;
471 extensions = (const char *)GetString(GL_EXTENSIONS);
472 if (!extensions) extensions = "";
473 if (!ext2) ext2 = "";
474 allexts = malloc(strlen(extensions) + strlen(ext2) + 2);
475 strcpy(allexts, extensions);
476 strcat(allexts, " ");
477 strcat(allexts, ext2);
478 mp_msg(MSGT_VO, MSGL_DBG2, "OpenGL extensions string:\n%s\n", allexts);
479 for (dsc = extfuncs; dsc->funcptr; dsc++) {
480 void *ptr = NULL;
481 int i;
482 if (!dsc->extstr || strstr(allexts, dsc->extstr)) {
483 for (i = 0; !ptr && dsc->funcnames[i]; i++)
484 ptr = getProcAddress((const GLubyte *)dsc->funcnames[i]);
486 if (!ptr)
487 ptr = dsc->fallback;
488 *(void **)dsc->funcptr = ptr;
490 if (strstr(allexts, "_texture_float"))
491 hqtexfmt = GL_RGB32F;
492 else if (strstr(allexts, "NV_float_buffer"))
493 hqtexfmt = GL_FLOAT_RGB32_NV;
494 else
495 hqtexfmt = GL_RGB16;
496 free(allexts);
500 * \brief create a texture and set some defaults
501 * \param target texture taget, usually GL_TEXTURE_2D
502 * \param fmt internal texture format
503 * \param format texture host data format
504 * \param type texture host data type
505 * \param filter filter used for scaling, e.g. GL_LINEAR
506 * \param w texture width
507 * \param h texture height
508 * \param val luminance value to fill texture with
509 * \ingroup gltexture
511 void glCreateClearTex(GLenum target, GLenum fmt, GLenum format, GLenum type, GLint filter,
512 int w, int h, unsigned char val) {
513 GLfloat fval = (GLfloat)val / 255.0;
514 GLfloat border[4] = {fval, fval, fval, fval};
515 int stride = w * glFmt2bpp(format, type);
516 char *init;
517 if (!stride) return;
518 init = malloc(stride * h);
519 memset(init, val, stride * h);
520 glAdjustAlignment(stride);
521 PixelStorei(GL_UNPACK_ROW_LENGTH, w);
522 TexImage2D(target, 0, fmt, w, h, 0, format, type, init);
523 TexParameterf(target, GL_TEXTURE_PRIORITY, 1.0);
524 TexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
525 TexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
526 TexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
527 TexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
528 // Border texels should not be used with CLAMP_TO_EDGE
529 // We set a sane default anyway.
530 TexParameterfv(target, GL_TEXTURE_BORDER_COLOR, border);
531 free(init);
535 * \brief skips whitespace and comments
536 * \param f file to read from
538 static void ppm_skip(FILE *f) {
539 int c, comment = 0;
540 do {
541 c = fgetc(f);
542 if (c == '#')
543 comment = 1;
544 if (c == '\n')
545 comment = 0;
546 } while (c != EOF && (isspace(c) || comment));
547 if (c != EOF)
548 ungetc(c, f);
551 #define MAXDIM (16 * 1024)
554 * \brief creates a texture from a PPM file
555 * \param target texture taget, usually GL_TEXTURE_2D
556 * \param fmt internal texture format, 0 for default
557 * \param filter filter used for scaling, e.g. GL_LINEAR
558 * \param f file to read PPM from
559 * \param width [out] width of texture
560 * \param height [out] height of texture
561 * \param maxval [out] maxval value from PPM file
562 * \return 0 on error, 1 otherwise
563 * \ingroup gltexture
565 int glCreatePPMTex(GLenum target, GLenum fmt, GLint filter,
566 FILE *f, int *width, int *height, int *maxval) {
567 unsigned w, h, m, val, bpp;
568 char *data;
569 GLenum type;
570 ppm_skip(f);
571 if (fgetc(f) != 'P' || fgetc(f) != '6')
572 return 0;
573 ppm_skip(f);
574 if (fscanf(f, "%u", &w) != 1)
575 return 0;
576 ppm_skip(f);
577 if (fscanf(f, "%u", &h) != 1)
578 return 0;
579 ppm_skip(f);
580 if (fscanf(f, "%u", &m) != 1)
581 return 0;
582 val = fgetc(f);
583 if (!isspace(val))
584 return 0;
585 if (w > MAXDIM || h > MAXDIM)
586 return 0;
587 bpp = (m > 255) ? 6 : 3;
588 data = malloc(w * h * bpp);
589 if (fread(data, w * bpp, h, f) != h)
590 return 0;
591 if (!fmt) {
592 fmt = (m > 255) ? hqtexfmt : 3;
593 if (fmt == GL_FLOAT_RGB32_NV && target != GL_TEXTURE_RECTANGLE)
594 fmt = GL_RGB16;
596 type = m > 255 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_BYTE;
597 glCreateClearTex(target, fmt, GL_RGB, type, filter, w, h, 0);
598 glUploadTex(target, GL_RGB, type,
599 data, w * bpp, 0, 0, w, h, 0);
600 free(data);
601 if (width) *width = w;
602 if (height) *height = h;
603 if (maxval) *maxval = m;
604 return 1;
608 * \brief return the number of bytes per pixel for the given format
609 * \param format OpenGL format
610 * \param type OpenGL type
611 * \return bytes per pixel
612 * \ingroup glgeneral
614 * Does not handle all possible variants, just those used by MPlayer
616 int glFmt2bpp(GLenum format, GLenum type) {
617 int component_size = 0;
618 switch (type) {
619 case GL_UNSIGNED_BYTE_3_3_2:
620 case GL_UNSIGNED_BYTE_2_3_3_REV:
621 return 1;
622 case GL_UNSIGNED_SHORT_5_5_5_1:
623 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
624 case GL_UNSIGNED_SHORT_5_6_5:
625 case GL_UNSIGNED_SHORT_5_6_5_REV:
626 return 2;
627 case GL_UNSIGNED_BYTE:
628 component_size = 1;
629 break;
630 case GL_UNSIGNED_SHORT:
631 component_size = 2;
632 break;
634 switch (format) {
635 case GL_LUMINANCE:
636 case GL_ALPHA:
637 return component_size;
638 case GL_YCBCR_MESA:
639 return 2;
640 case GL_RGB:
641 case GL_BGR:
642 return 3 * component_size;
643 case GL_RGBA:
644 case GL_BGRA:
645 return 4 * component_size;
647 return 0; // unknown
651 * \brief upload a texture, handling things like stride and slices
652 * \param target texture target, usually GL_TEXTURE_2D
653 * \param format OpenGL format of data
654 * \param type OpenGL type of data
655 * \param dataptr data to upload
656 * \param stride data stride
657 * \param x x offset in texture
658 * \param y y offset in texture
659 * \param w width of the texture part to upload
660 * \param h height of the texture part to upload
661 * \param slice height of an upload slice, 0 for all at once
662 * \ingroup gltexture
664 void glUploadTex(GLenum target, GLenum format, GLenum type,
665 const void *dataptr, int stride,
666 int x, int y, int w, int h, int slice) {
667 const uint8_t *data = dataptr;
668 int y_max = y + h;
669 if (w <= 0 || h <= 0) return;
670 if (slice <= 0)
671 slice = h;
672 if (stride < 0) {
673 data += (h - 1) * stride;
674 stride = -stride;
676 // this is not always correct, but should work for MPlayer
677 glAdjustAlignment(stride);
678 PixelStorei(GL_UNPACK_ROW_LENGTH, stride / glFmt2bpp(format, type));
679 for (; y + slice <= y_max; y += slice) {
680 TexSubImage2D(target, 0, x, y, w, slice, format, type, data);
681 data += stride * slice;
683 if (y < y_max)
684 TexSubImage2D(target, 0, x, y, w, y_max - y, format, type, data);
687 static void fillUVcoeff(GLfloat *ucoef, GLfloat *vcoef,
688 float uvcos, float uvsin) {
689 int i;
690 ucoef[0] = 0 * uvcos + 1.403 * uvsin;
691 vcoef[0] = 0 * uvsin + 1.403 * uvcos;
692 ucoef[1] = -0.344 * uvcos + -0.714 * uvsin;
693 vcoef[1] = -0.344 * uvsin + -0.714 * uvcos;
694 ucoef[2] = 1.770 * uvcos + 0 * uvsin;
695 vcoef[2] = 1.770 * uvsin + 0 * uvcos;
696 ucoef[3] = 0;
697 vcoef[3] = 0;
698 // Coefficients (probably) must be in [0, 1] range, whereas they originally
699 // are in [-2, 2] range, so here comes the trick:
700 // First put them in the [-0.5, 0.5] range, then add 0.5.
701 // This can be undone with the HALF_BIAS and SCALE_BY_FOUR arguments
702 // for CombinerInput and CombinerOutput (or the respective ATI variants)
703 for (i = 0; i < 4; i++) {
704 ucoef[i] = ucoef[i] * 0.25 + 0.5;
705 vcoef[i] = vcoef[i] * 0.25 + 0.5;
710 * \brief Setup register combiners for YUV to RGB conversion.
711 * \param uvcos used for saturation and hue adjustment
712 * \param uvsin used for saturation and hue adjustment
714 static void glSetupYUVCombiners(float uvcos, float uvsin) {
715 GLfloat ucoef[4];
716 GLfloat vcoef[4];
717 GLint i;
718 if (!CombinerInput || !CombinerOutput ||
719 !CombinerParameterfv || !CombinerParameteri) {
720 mp_msg(MSGT_VO, MSGL_FATAL, "[gl] Combiner functions missing!\n");
721 return;
723 GetIntegerv(GL_MAX_GENERAL_COMBINERS_NV, &i);
724 if (i < 2)
725 mp_msg(MSGT_VO, MSGL_ERR,
726 "[gl] 2 general combiners needed for YUV combiner support (found %i)\n", i);
727 GetIntegerv(GL_MAX_TEXTURE_UNITS, &i);
728 if (i < 3)
729 mp_msg(MSGT_VO, MSGL_ERR,
730 "[gl] 3 texture units needed for YUV combiner support (found %i)\n", i);
731 fillUVcoeff(ucoef, vcoef, uvcos, uvsin);
732 CombinerParameterfv(GL_CONSTANT_COLOR0_NV, ucoef);
733 CombinerParameterfv(GL_CONSTANT_COLOR1_NV, vcoef);
735 // UV first, like this green component cannot overflow
736 CombinerInput(GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_A_NV,
737 GL_TEXTURE1, GL_HALF_BIAS_NORMAL_NV, GL_RGB);
738 CombinerInput(GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_B_NV,
739 GL_CONSTANT_COLOR0_NV, GL_HALF_BIAS_NORMAL_NV, GL_RGB);
740 CombinerInput(GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_C_NV,
741 GL_TEXTURE2, GL_HALF_BIAS_NORMAL_NV, GL_RGB);
742 CombinerInput(GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_D_NV,
743 GL_CONSTANT_COLOR1_NV, GL_HALF_BIAS_NORMAL_NV, GL_RGB);
744 CombinerOutput(GL_COMBINER0_NV, GL_RGB, GL_DISCARD_NV, GL_DISCARD_NV,
745 GL_SPARE0_NV, GL_SCALE_BY_FOUR_NV, GL_NONE, GL_FALSE,
746 GL_FALSE, GL_FALSE);
748 // stage 2
749 CombinerInput(GL_COMBINER1_NV, GL_RGB, GL_VARIABLE_A_NV, GL_SPARE0_NV,
750 GL_SIGNED_IDENTITY_NV, GL_RGB);
751 CombinerInput(GL_COMBINER1_NV, GL_RGB, GL_VARIABLE_B_NV, GL_ZERO,
752 GL_UNSIGNED_INVERT_NV, GL_RGB);
753 CombinerInput(GL_COMBINER1_NV, GL_RGB, GL_VARIABLE_C_NV,
754 GL_TEXTURE0, GL_SIGNED_IDENTITY_NV, GL_RGB);
755 CombinerInput(GL_COMBINER1_NV, GL_RGB, GL_VARIABLE_D_NV, GL_ZERO,
756 GL_UNSIGNED_INVERT_NV, GL_RGB);
757 CombinerOutput(GL_COMBINER1_NV, GL_RGB, GL_DISCARD_NV, GL_DISCARD_NV,
758 GL_SPARE0_NV, GL_NONE, GL_NONE, GL_FALSE,
759 GL_FALSE, GL_FALSE);
761 // leave final combiner stage in default mode
762 CombinerParameteri(GL_NUM_GENERAL_COMBINERS_NV, 2);
766 * \brief Setup ATI version of register combiners for YUV to RGB conversion.
767 * \param uvcos used for saturation and hue adjustment
768 * \param uvsin used for saturation and hue adjustment
770 * ATI called this fragment shader, but the name is confusing in the
771 * light of a very different OpenGL 2.0 extension with the same name
773 static void glSetupYUVCombinersATI(float uvcos, float uvsin) {
774 GLfloat ucoef[4];
775 GLfloat vcoef[4];
776 GLint i;
777 if (!BeginFragmentShader || !EndFragmentShader ||
778 !SetFragmentShaderConstant || !SampleMap ||
779 !ColorFragmentOp2 || !ColorFragmentOp3) {
780 mp_msg(MSGT_VO, MSGL_FATAL, "[gl] Combiner (ATI) functions missing!\n");
781 return;
783 GetIntegerv(GL_NUM_FRAGMENT_REGISTERS_ATI, &i);
784 if (i < 3)
785 mp_msg(MSGT_VO, MSGL_ERR,
786 "[gl] 3 registers needed for YUV combiner (ATI) support (found %i)\n", i);
787 GetIntegerv (GL_MAX_TEXTURE_UNITS, &i);
788 if (i < 3)
789 mp_msg(MSGT_VO, MSGL_ERR,
790 "[gl] 3 texture units needed for YUV combiner (ATI) support (found %i)\n", i);
791 fillUVcoeff(ucoef, vcoef, uvcos, uvsin);
792 BeginFragmentShader();
793 SetFragmentShaderConstant(GL_CON_0_ATI, ucoef);
794 SetFragmentShaderConstant(GL_CON_1_ATI, vcoef);
795 SampleMap(GL_REG_0_ATI, GL_TEXTURE0, GL_SWIZZLE_STR_ATI);
796 SampleMap(GL_REG_1_ATI, GL_TEXTURE1, GL_SWIZZLE_STR_ATI);
797 SampleMap(GL_REG_2_ATI, GL_TEXTURE2, GL_SWIZZLE_STR_ATI);
798 // UV first, like this green component cannot overflow
799 ColorFragmentOp2(GL_MUL_ATI, GL_REG_1_ATI, GL_NONE, GL_NONE,
800 GL_REG_1_ATI, GL_NONE, GL_BIAS_BIT_ATI,
801 GL_CON_0_ATI, GL_NONE, GL_BIAS_BIT_ATI);
802 ColorFragmentOp3(GL_MAD_ATI, GL_REG_2_ATI, GL_NONE, GL_4X_BIT_ATI,
803 GL_REG_2_ATI, GL_NONE, GL_BIAS_BIT_ATI,
804 GL_CON_1_ATI, GL_NONE, GL_BIAS_BIT_ATI,
805 GL_REG_1_ATI, GL_NONE, GL_NONE);
806 ColorFragmentOp2(GL_ADD_ATI, GL_REG_0_ATI, GL_NONE, GL_NONE,
807 GL_REG_0_ATI, GL_NONE, GL_NONE,
808 GL_REG_2_ATI, GL_NONE, GL_NONE);
809 EndFragmentShader();
813 * \brief helper function for gen_spline_lookup_tex
814 * \param x subpixel-position ((0,1) range) to calculate weights for
815 * \param dst where to store transformed weights, must provide space for 4 GLfloats
817 * calculates the weights and stores them after appropriate transformation
818 * for the scaler fragment program.
820 static void store_weights(float x, GLfloat *dst) {
821 float w0 = (((-1 * x + 3) * x - 3) * x + 1) / 6;
822 float w1 = ((( 3 * x - 6) * x + 0) * x + 4) / 6;
823 float w2 = (((-3 * x + 3) * x + 3) * x + 1) / 6;
824 float w3 = ((( 1 * x + 0) * x + 0) * x + 0) / 6;
825 *dst++ = 1 + x - w1 / (w0 + w1);
826 *dst++ = 1 - x + w3 / (w2 + w3);
827 *dst++ = w0 + w1;
828 *dst++ = 0;
831 //! to avoid artefacts this should be rather large
832 #define LOOKUP_BSPLINE_RES (2 * 1024)
834 * \brief creates the 1D lookup texture needed for fast higher-order filtering
835 * \param unit texture unit to attach texture to
837 static void gen_spline_lookup_tex(GLenum unit) {
838 GLfloat *tex = calloc(4 * LOOKUP_BSPLINE_RES, sizeof(*tex));
839 GLfloat *tp = tex;
840 int i;
841 for (i = 0; i < LOOKUP_BSPLINE_RES; i++) {
842 float x = (float)(i + 0.5) / LOOKUP_BSPLINE_RES;
843 store_weights(x, tp);
844 tp += 4;
846 store_weights(0, tex);
847 store_weights(1, &tex[4 * (LOOKUP_BSPLINE_RES - 1)]);
848 ActiveTexture(unit);
849 TexImage1D(GL_TEXTURE_1D, 0, GL_RGBA16, LOOKUP_BSPLINE_RES, 0, GL_RGBA, GL_FLOAT, tex);
850 TexParameterf(GL_TEXTURE_1D, GL_TEXTURE_PRIORITY, 1.0);
851 TexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
852 TexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
853 TexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT);
854 ActiveTexture(GL_TEXTURE0);
855 free(tex);
858 static const char *bilin_filt_template =
859 "TEX yuv.%c, fragment.texcoord[%c], texture[%c], %s;";
861 #define BICUB_FILT_MAIN(textype) \
862 /* first y-interpolation */ \
863 "ADD coord, fragment.texcoord[%c].xyxy, cdelta.xyxw;" \
864 "ADD coord2, fragment.texcoord[%c].xyxy, cdelta.zyzw;" \
865 "TEX a.r, coord.xyxy, texture[%c], "textype";" \
866 "TEX a.g, coord.zwzw, texture[%c], "textype";" \
867 /* second y-interpolation */ \
868 "TEX b.r, coord2.xyxy, texture[%c], "textype";" \
869 "TEX b.g, coord2.zwzw, texture[%c], "textype";" \
870 "LRP a.b, parmy.b, a.rrrr, a.gggg;" \
871 "LRP a.a, parmy.b, b.rrrr, b.gggg;" \
872 /* x-interpolation */ \
873 "LRP yuv.%c, parmx.b, a.bbbb, a.aaaa;"
875 static const char *bicub_filt_template_2D =
876 "MAD coord.xy, fragment.texcoord[%c], {%e, %e}, {0.5, 0.5};"
877 "TEX parmx, coord.x, texture[%c], 1D;"
878 "MUL cdelta.xz, parmx.rrgg, {-%e, 0, %e, 0};"
879 "TEX parmy, coord.y, texture[%c], 1D;"
880 "MUL cdelta.yw, parmy.rrgg, {0, -%e, 0, %e};"
881 BICUB_FILT_MAIN("2D");
883 static const char *bicub_filt_template_RECT =
884 "ADD coord, fragment.texcoord[%c], {0.5, 0.5};"
885 "TEX parmx, coord.x, texture[%c], 1D;"
886 "MUL cdelta.xz, parmx.rrgg, {-1, 0, 1, 0};"
887 "TEX parmy, coord.y, texture[%c], 1D;"
888 "MUL cdelta.yw, parmy.rrgg, {0, -1, 0, 1};"
889 BICUB_FILT_MAIN("RECT");
891 #define CALCWEIGHTS(t, s) \
892 "MAD "t", {-0.5, 0.1666, 0.3333, -0.3333}, "s", {1, 0, -0.5, 0.5};" \
893 "MAD "t", "t", "s", {0, 0, -0.5, 0.5};" \
894 "MAD "t", "t", "s", {-0.6666, 0, 0.8333, 0.1666};" \
895 "RCP a.x, "t".z;" \
896 "RCP a.y, "t".w;" \
897 "MAD "t".xy, "t".xyxy, a.xyxy, {1, 1, 0, 0};" \
898 "ADD "t".x, "t".xxxx, "s";" \
899 "SUB "t".y, "t".yyyy, "s";"
901 static const char *bicub_notex_filt_template_2D =
902 "MAD coord.xy, fragment.texcoord[%c], {%e, %e}, {0.5, 0.5};"
903 "FRC coord.xy, coord.xyxy;"
904 CALCWEIGHTS("parmx", "coord.xxxx")
905 "MUL cdelta.xz, parmx.rrgg, {-%e, 0, %e, 0};"
906 CALCWEIGHTS("parmy", "coord.yyyy")
907 "MUL cdelta.yw, parmy.rrgg, {0, -%e, 0, %e};"
908 BICUB_FILT_MAIN("2D");
910 static const char *bicub_notex_filt_template_RECT =
911 "ADD coord, fragment.texcoord[%c], {0.5, 0.5};"
912 "FRC coord.xy, coord.xyxy;"
913 CALCWEIGHTS("parmx", "coord.xxxx")
914 "MUL cdelta.xz, parmx.rrgg, {-1, 0, 1, 0};"
915 CALCWEIGHTS("parmy", "coord.yyyy")
916 "MUL cdelta.yw, parmy.rrgg, {0, -1, 0, 1};"
917 BICUB_FILT_MAIN("RECT");
919 #define BICUB_X_FILT_MAIN(textype) \
920 "ADD coord.xy, fragment.texcoord[%c].xyxy, cdelta.xyxy;" \
921 "ADD coord2.xy, fragment.texcoord[%c].xyxy, cdelta.zyzy;" \
922 "TEX a.r, coord, texture[%c], "textype";" \
923 "TEX b.r, coord2, texture[%c], "textype";" \
924 /* x-interpolation */ \
925 "LRP yuv.%c, parmx.b, a.rrrr, b.rrrr;"
927 static const char *bicub_x_filt_template_2D =
928 "MAD coord.x, fragment.texcoord[%c], {%e}, {0.5};"
929 "TEX parmx, coord, texture[%c], 1D;"
930 "MUL cdelta.xyz, parmx.rrgg, {-%e, 0, %e};"
931 BICUB_X_FILT_MAIN("2D");
933 static const char *bicub_x_filt_template_RECT =
934 "ADD coord.x, fragment.texcoord[%c], {0.5};"
935 "TEX parmx, coord, texture[%c], 1D;"
936 "MUL cdelta.xyz, parmx.rrgg, {-1, 0, 1};"
937 BICUB_X_FILT_MAIN("RECT");
939 static const char *unsharp_filt_template =
940 "PARAM dcoord%c = {%e, %e, %e, %e};"
941 "ADD coord, fragment.texcoord[%c].xyxy, dcoord%c;"
942 "SUB coord2, fragment.texcoord[%c].xyxy, dcoord%c;"
943 "TEX a.r, fragment.texcoord[%c], texture[%c], %s;"
944 "TEX b.r, coord.xyxy, texture[%c], %s;"
945 "TEX b.g, coord.zwzw, texture[%c], %s;"
946 "ADD b.r, b.r, b.g;"
947 "TEX b.b, coord2.xyxy, texture[%c], %s;"
948 "TEX b.g, coord2.zwzw, texture[%c], %s;"
949 "DP3 b, b, {0.25, 0.25, 0.25};"
950 "SUB b.r, a.r, b.r;"
951 "MAD yuv.%c, b.r, {%e}, a.r;";
953 static const char *unsharp_filt_template2 =
954 "PARAM dcoord%c = {%e, %e, %e, %e};"
955 "PARAM dcoord2%c = {%e, 0, 0, %e};"
956 "ADD coord, fragment.texcoord[%c].xyxy, dcoord%c;"
957 "SUB coord2, fragment.texcoord[%c].xyxy, dcoord%c;"
958 "TEX a.r, fragment.texcoord[%c], texture[%c], %s;"
959 "TEX b.r, coord.xyxy, texture[%c], %s;"
960 "TEX b.g, coord.zwzw, texture[%c], %s;"
961 "ADD b.r, b.r, b.g;"
962 "TEX b.b, coord2.xyxy, texture[%c], %s;"
963 "TEX b.g, coord2.zwzw, texture[%c], %s;"
964 "ADD b.r, b.r, b.b;"
965 "ADD b.a, b.r, b.g;"
966 "ADD coord, fragment.texcoord[%c].xyxy, dcoord2%c;"
967 "SUB coord2, fragment.texcoord[%c].xyxy, dcoord2%c;"
968 "TEX b.r, coord.xyxy, texture[%c], %s;"
969 "TEX b.g, coord.zwzw, texture[%c], %s;"
970 "ADD b.r, b.r, b.g;"
971 "TEX b.b, coord2.xyxy, texture[%c], %s;"
972 "TEX b.g, coord2.zwzw, texture[%c], %s;"
973 "DP4 b.r, b, {-0.1171875, -0.1171875, -0.1171875, -0.09765625};"
974 "MAD b.r, a.r, {0.859375}, b.r;"
975 "MAD yuv.%c, b.r, {%e}, a.r;";
977 static const char *yuv_prog_template =
978 "PARAM ycoef = {%e, %e, %e};"
979 "PARAM ucoef = {%e, %e, %e};"
980 "PARAM vcoef = {%e, %e, %e};"
981 "PARAM offsets = {%e, %e, %e};"
982 "TEMP res;"
983 "MAD res.rgb, yuv.rrrr, ycoef, offsets;"
984 "MAD res.rgb, yuv.gggg, ucoef, res;"
985 "MAD result.color.rgb, yuv.bbbb, vcoef, res;"
986 "END";
988 static const char *yuv_pow_prog_template =
989 "PARAM ycoef = {%e, %e, %e};"
990 "PARAM ucoef = {%e, %e, %e};"
991 "PARAM vcoef = {%e, %e, %e};"
992 "PARAM offsets = {%e, %e, %e};"
993 "PARAM gamma = {%e, %e, %e};"
994 "TEMP res;"
995 "MAD res.rgb, yuv.rrrr, ycoef, offsets;"
996 "MAD res.rgb, yuv.gggg, ucoef, res;"
997 "MAD_SAT res.rgb, yuv.bbbb, vcoef, res;"
998 "POW result.color.r, res.r, gamma.r;"
999 "POW result.color.g, res.g, gamma.g;"
1000 "POW result.color.b, res.b, gamma.b;"
1001 "END";
1003 static const char *yuv_lookup_prog_template =
1004 "PARAM ycoef = {%e, %e, %e, 0};"
1005 "PARAM ucoef = {%e, %e, %e, 0};"
1006 "PARAM vcoef = {%e, %e, %e, 0};"
1007 "PARAM offsets = {%e, %e, %e, 0.125};"
1008 "TEMP res;"
1009 "MAD res, yuv.rrrr, ycoef, offsets;"
1010 "MAD res.rgb, yuv.gggg, ucoef, res;"
1011 "MAD res.rgb, yuv.bbbb, vcoef, res;"
1012 "TEX result.color.r, res.raaa, texture[%c], 2D;"
1013 "ADD res.a, res.a, 0.25;"
1014 "TEX result.color.g, res.gaaa, texture[%c], 2D;"
1015 "ADD res.a, res.a, 0.25;"
1016 "TEX result.color.b, res.baaa, texture[%c], 2D;"
1017 "END";
1019 static const char *yuv_lookup3d_prog_template =
1020 "TEX result.color, yuv, texture[%c], 3D;"
1021 "END";
1024 * \brief creates and initializes helper textures needed for scaling texture read
1025 * \param scaler scaler type to create texture for
1026 * \param texu contains next free texture unit number
1027 * \param texs texture unit ids for the scaler are stored in this array
1029 static void create_scaler_textures(int scaler, int *texu, char *texs) {
1030 switch (scaler) {
1031 case YUV_SCALER_BILIN:
1032 case YUV_SCALER_BICUB_NOTEX:
1033 case YUV_SCALER_UNSHARP:
1034 case YUV_SCALER_UNSHARP2:
1035 break;
1036 case YUV_SCALER_BICUB:
1037 case YUV_SCALER_BICUB_X:
1038 texs[0] = (*texu)++;
1039 gen_spline_lookup_tex(GL_TEXTURE0 + texs[0]);
1040 texs[0] += '0';
1041 break;
1042 default:
1043 mp_msg(MSGT_VO, MSGL_ERR, "[gl] unknown scaler type %i\n", scaler);
1047 //! resolution of texture for gamma lookup table
1048 #define LOOKUP_RES 512
1049 //! resolution for 3D yuv->rgb conversion lookup table
1050 #define LOOKUP_3DRES 32
1052 * \brief creates and initializes helper textures needed for yuv conversion
1053 * \param params struct containing parameters like brightness, gamma, ...
1054 * \param texu contains next free texture unit number
1055 * \param texs texture unit ids for the conversion are stored in this array
1057 static void create_conv_textures(gl_conversion_params_t *params, int *texu, char *texs) {
1058 unsigned char *lookup_data = NULL;
1059 int conv = YUV_CONVERSION(params->type);
1060 switch (conv) {
1061 case YUV_CONVERSION_FRAGMENT:
1062 case YUV_CONVERSION_FRAGMENT_POW:
1063 break;
1064 case YUV_CONVERSION_FRAGMENT_LOOKUP:
1065 texs[0] = (*texu)++;
1066 ActiveTexture(GL_TEXTURE0 + texs[0]);
1067 lookup_data = malloc(4 * LOOKUP_RES);
1068 mp_gen_gamma_map(lookup_data, LOOKUP_RES, params->csp_params.rgamma);
1069 mp_gen_gamma_map(&lookup_data[LOOKUP_RES], LOOKUP_RES, params->csp_params.ggamma);
1070 mp_gen_gamma_map(&lookup_data[2 * LOOKUP_RES], LOOKUP_RES, params->csp_params.bgamma);
1071 glCreateClearTex(GL_TEXTURE_2D, GL_LUMINANCE8, GL_LUMINANCE, GL_UNSIGNED_BYTE, GL_LINEAR,
1072 LOOKUP_RES, 4, 0);
1073 glUploadTex(GL_TEXTURE_2D, GL_LUMINANCE, GL_UNSIGNED_BYTE, lookup_data,
1074 LOOKUP_RES, 0, 0, LOOKUP_RES, 4, 0);
1075 ActiveTexture(GL_TEXTURE0);
1076 texs[0] += '0';
1077 break;
1078 case YUV_CONVERSION_FRAGMENT_LOOKUP3D:
1080 int sz = LOOKUP_3DRES + 2; // texture size including borders
1081 if (!TexImage3D) {
1082 mp_msg(MSGT_VO, MSGL_ERR, "[gl] Missing 3D texture function!\n");
1083 break;
1085 texs[0] = (*texu)++;
1086 ActiveTexture(GL_TEXTURE0 + texs[0]);
1087 lookup_data = malloc(3 * sz * sz * sz);
1088 mp_gen_yuv2rgb_map(&params->csp_params, lookup_data, LOOKUP_3DRES);
1089 glAdjustAlignment(sz);
1090 PixelStorei(GL_UNPACK_ROW_LENGTH, 0);
1091 TexImage3D(GL_TEXTURE_3D, 0, 3, sz, sz, sz, 1,
1092 GL_RGB, GL_UNSIGNED_BYTE, lookup_data);
1093 TexParameterf(GL_TEXTURE_3D, GL_TEXTURE_PRIORITY, 1.0);
1094 TexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1095 TexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1096 TexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP);
1097 TexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP);
1098 TexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP);
1099 ActiveTexture(GL_TEXTURE0);
1100 texs[0] += '0';
1102 break;
1103 default:
1104 mp_msg(MSGT_VO, MSGL_ERR, "[gl] unknown conversion type %i\n", conv);
1106 if (lookup_data)
1107 free(lookup_data);
1111 * \brief adds a scaling texture read at the current fragment program position
1112 * \param scaler type of scaler to insert
1113 * \param prog_pos current position in fragment program
1114 * \param remain how many bytes remain in the buffer given by prog_pos
1115 * \param texs array containing the texture unit identifiers for this scaler
1116 * \param in_tex texture unit the scaler should read from
1117 * \param out_comp component of the yuv variable the scaler stores the result in
1118 * \param rect if rectangular (pixel) adressing should be used for in_tex
1119 * \param texw width of the in_tex texture
1120 * \param texh height of the in_tex texture
1121 * \param strength strength of filter effect if the scaler does some kind of filtering
1123 static void add_scaler(int scaler, char **prog_pos, int *remain, char *texs,
1124 char in_tex, char out_comp, int rect, int texw, int texh,
1125 double strength) {
1126 const char *ttype = rect ? "RECT" : "2D";
1127 const float ptw = rect ? 1.0 : 1.0 / texw;
1128 const float pth = rect ? 1.0 : 1.0 / texh;
1129 switch (scaler) {
1130 case YUV_SCALER_BILIN:
1131 snprintf(*prog_pos, *remain, bilin_filt_template, out_comp, in_tex,
1132 in_tex, ttype);
1133 break;
1134 case YUV_SCALER_BICUB:
1135 if (rect)
1136 snprintf(*prog_pos, *remain, bicub_filt_template_RECT,
1137 in_tex, texs[0], texs[0],
1138 in_tex, in_tex, in_tex, in_tex, in_tex, in_tex, out_comp);
1139 else
1140 snprintf(*prog_pos, *remain, bicub_filt_template_2D,
1141 in_tex, (float)texw, (float)texh,
1142 texs[0], ptw, ptw, texs[0], pth, pth,
1143 in_tex, in_tex, in_tex, in_tex, in_tex, in_tex, out_comp);
1144 break;
1145 case YUV_SCALER_BICUB_X:
1146 if (rect)
1147 snprintf(*prog_pos, *remain, bicub_x_filt_template_RECT,
1148 in_tex, texs[0],
1149 in_tex, in_tex, in_tex, in_tex, out_comp);
1150 else
1151 snprintf(*prog_pos, *remain, bicub_x_filt_template_2D,
1152 in_tex, (float)texw,
1153 texs[0], ptw, ptw,
1154 in_tex, in_tex, in_tex, in_tex, out_comp);
1155 break;
1156 case YUV_SCALER_BICUB_NOTEX:
1157 if (rect)
1158 snprintf(*prog_pos, *remain, bicub_notex_filt_template_RECT,
1159 in_tex,
1160 in_tex, in_tex, in_tex, in_tex, in_tex, in_tex, out_comp);
1161 else
1162 snprintf(*prog_pos, *remain, bicub_notex_filt_template_2D,
1163 in_tex, (float)texw, (float)texh, ptw, ptw, pth, pth,
1164 in_tex, in_tex, in_tex, in_tex, in_tex, in_tex, out_comp);
1165 break;
1166 case YUV_SCALER_UNSHARP:
1167 snprintf(*prog_pos, *remain, unsharp_filt_template,
1168 out_comp, 0.5 * ptw, 0.5 * pth, 0.5 * ptw, -0.5 * pth,
1169 in_tex, out_comp, in_tex, out_comp, in_tex,
1170 in_tex, ttype, in_tex, ttype, in_tex, ttype, in_tex, ttype,
1171 in_tex, ttype, out_comp, strength);
1172 break;
1173 case YUV_SCALER_UNSHARP2:
1174 snprintf(*prog_pos, *remain, unsharp_filt_template2,
1175 out_comp, 1.2 * ptw, 1.2 * pth, 1.2 * ptw, -1.2 * pth,
1176 out_comp, 1.5 * ptw, 1.5 * pth,
1177 in_tex, out_comp, in_tex, out_comp, in_tex,
1178 in_tex, ttype, in_tex, ttype, in_tex, ttype, in_tex, ttype,
1179 in_tex, ttype, in_tex, out_comp, in_tex, out_comp,
1180 in_tex, ttype, in_tex, ttype, in_tex, ttype,
1181 in_tex, ttype, out_comp, strength);
1182 break;
1184 *remain -= strlen(*prog_pos);
1185 *prog_pos += strlen(*prog_pos);
1188 static const struct {
1189 const char *name;
1190 GLenum cur;
1191 GLenum max;
1192 } progstats[] = {
1193 {"instructions", 0x88A0, 0x88A1},
1194 {"native instructions", 0x88A2, 0x88A3},
1195 {"temporaries", 0x88A4, 0x88A5},
1196 {"native temporaries", 0x88A6, 0x88A7},
1197 {"parameters", 0x88A8, 0x88A9},
1198 {"native parameters", 0x88AA, 0x88AB},
1199 {"attribs", 0x88AC, 0x88AD},
1200 {"native attribs", 0x88AE, 0x88AF},
1201 {"ALU instructions", 0x8805, 0x880B},
1202 {"TEX instructions", 0x8806, 0x880C},
1203 {"TEX indirections", 0x8807, 0x880D},
1204 {"native ALU instructions", 0x8808, 0x880E},
1205 {"native TEX instructions", 0x8809, 0x880F},
1206 {"native TEX indirections", 0x880A, 0x8810},
1207 {NULL, 0, 0}
1211 * \brief load the specified GPU Program
1212 * \param target program target to load into, only GL_FRAGMENT_PROGRAM is tested
1213 * \param prog program string
1214 * \return 1 on success, 0 otherwise
1216 int loadGPUProgram(GLenum target, char *prog) {
1217 int i;
1218 GLint cur = 0, max = 0, err = 0;
1219 if (!ProgramString) {
1220 mp_msg(MSGT_VO, MSGL_ERR, "[gl] Missing GPU program function\n");
1221 return 0;
1223 ProgramString(target, GL_PROGRAM_FORMAT_ASCII, strlen(prog), prog);
1224 GetIntegerv(GL_PROGRAM_ERROR_POSITION, &err);
1225 if (err != -1) {
1226 mp_msg(MSGT_VO, MSGL_ERR,
1227 "[gl] Error compiling fragment program, make sure your card supports\n"
1228 "[gl] GL_ARB_fragment_program (use glxinfo to check).\n"
1229 "[gl] Error message:\n %s at %.10s\n",
1230 GetString(GL_PROGRAM_ERROR_STRING), &prog[err]);
1231 return 0;
1233 if (!GetProgramiv || !mp_msg_test(MSGT_VO, MSGL_DBG2))
1234 return 1;
1235 mp_msg(MSGT_VO, MSGL_V, "[gl] Program statistics:\n");
1236 for (i = 0; progstats[i].name; i++) {
1237 GetProgramiv(target, progstats[i].cur, &cur);
1238 GetProgramiv(target, progstats[i].max, &max);
1239 mp_msg(MSGT_VO, MSGL_V, "[gl] %s: %i/%i\n", progstats[i].name, cur, max);
1241 return 1;
1244 #define MAX_PROGSZ (1024*1024)
1247 * \brief setup a fragment program that will do YUV->RGB conversion
1248 * \param parms struct containing parameters like conversion and scaler type,
1249 * brightness, ...
1251 static void glSetupYUVFragprog(gl_conversion_params_t *params) {
1252 int type = params->type;
1253 int texw = params->texw;
1254 int texh = params->texh;
1255 int rect = params->target == GL_TEXTURE_RECTANGLE;
1256 static const char prog_hdr[] =
1257 "!!ARBfp1.0\n"
1258 "OPTION ARB_precision_hint_fastest;"
1259 // all scaler variables must go here so they aren't defined
1260 // multiple times when the same scaler is used more than once
1261 "TEMP coord, coord2, cdelta, parmx, parmy, a, b, yuv;";
1262 int prog_remain;
1263 char *yuv_prog, *prog_pos;
1264 int cur_texu = 3;
1265 char lum_scale_texs[1];
1266 char chrom_scale_texs[1];
1267 char conv_texs[1];
1268 GLint i;
1269 // this is the conversion matrix, with y, u, v factors
1270 // for red, green, blue and the constant offsets
1271 float yuv2rgb[3][4];
1272 create_conv_textures(params, &cur_texu, conv_texs);
1273 create_scaler_textures(YUV_LUM_SCALER(type), &cur_texu, lum_scale_texs);
1274 if (YUV_CHROM_SCALER(type) == YUV_LUM_SCALER(type))
1275 memcpy(chrom_scale_texs, lum_scale_texs, sizeof(chrom_scale_texs));
1276 else
1277 create_scaler_textures(YUV_CHROM_SCALER(type), &cur_texu, chrom_scale_texs);
1278 GetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &i);
1279 if (i < cur_texu)
1280 mp_msg(MSGT_VO, MSGL_ERR,
1281 "[gl] %i texture units needed for this type of YUV fragment support (found %i)\n",
1282 cur_texu, i);
1283 if (!ProgramString) {
1284 mp_msg(MSGT_VO, MSGL_FATAL, "[gl] ProgramString function missing!\n");
1285 return;
1287 yuv_prog = malloc(MAX_PROGSZ);
1288 strcpy(yuv_prog, prog_hdr);
1289 prog_pos = yuv_prog + sizeof(prog_hdr) - 1;
1290 prog_remain = MAX_PROGSZ - sizeof(prog_hdr);
1291 add_scaler(YUV_LUM_SCALER(type), &prog_pos, &prog_remain, lum_scale_texs,
1292 '0', 'r', rect, texw, texh, params->filter_strength);
1293 add_scaler(YUV_CHROM_SCALER(type), &prog_pos, &prog_remain, chrom_scale_texs,
1294 '1', 'g', rect, params->chrom_texw, params->chrom_texh, params->filter_strength);
1295 add_scaler(YUV_CHROM_SCALER(type), &prog_pos, &prog_remain, chrom_scale_texs,
1296 '2', 'b', rect, params->chrom_texw, params->chrom_texh, params->filter_strength);
1297 mp_get_yuv2rgb_coeffs(&params->csp_params, yuv2rgb);
1298 switch (YUV_CONVERSION(type)) {
1299 case YUV_CONVERSION_FRAGMENT:
1300 snprintf(prog_pos, prog_remain, yuv_prog_template,
1301 yuv2rgb[ROW_R][COL_Y], yuv2rgb[ROW_G][COL_Y], yuv2rgb[ROW_B][COL_Y],
1302 yuv2rgb[ROW_R][COL_U], yuv2rgb[ROW_G][COL_U], yuv2rgb[ROW_B][COL_U],
1303 yuv2rgb[ROW_R][COL_V], yuv2rgb[ROW_G][COL_V], yuv2rgb[ROW_B][COL_V],
1304 yuv2rgb[ROW_R][COL_C], yuv2rgb[ROW_G][COL_C], yuv2rgb[ROW_B][COL_C]);
1305 break;
1306 case YUV_CONVERSION_FRAGMENT_POW:
1307 snprintf(prog_pos, prog_remain, yuv_pow_prog_template,
1308 yuv2rgb[ROW_R][COL_Y], yuv2rgb[ROW_G][COL_Y], yuv2rgb[ROW_B][COL_Y],
1309 yuv2rgb[ROW_R][COL_U], yuv2rgb[ROW_G][COL_U], yuv2rgb[ROW_B][COL_U],
1310 yuv2rgb[ROW_R][COL_V], yuv2rgb[ROW_G][COL_V], yuv2rgb[ROW_B][COL_V],
1311 yuv2rgb[ROW_R][COL_C], yuv2rgb[ROW_G][COL_C], yuv2rgb[ROW_B][COL_C],
1312 (float)1.0 / params->csp_params.rgamma, (float)1.0 / params->csp_params.bgamma, (float)1.0 / params->csp_params.bgamma);
1313 break;
1314 case YUV_CONVERSION_FRAGMENT_LOOKUP:
1315 snprintf(prog_pos, prog_remain, yuv_lookup_prog_template,
1316 yuv2rgb[ROW_R][COL_Y], yuv2rgb[ROW_G][COL_Y], yuv2rgb[ROW_B][COL_Y],
1317 yuv2rgb[ROW_R][COL_U], yuv2rgb[ROW_G][COL_U], yuv2rgb[ROW_B][COL_U],
1318 yuv2rgb[ROW_R][COL_V], yuv2rgb[ROW_G][COL_V], yuv2rgb[ROW_B][COL_V],
1319 yuv2rgb[ROW_R][COL_C], yuv2rgb[ROW_G][COL_C], yuv2rgb[ROW_B][COL_C],
1320 conv_texs[0], conv_texs[0], conv_texs[0]);
1321 break;
1322 case YUV_CONVERSION_FRAGMENT_LOOKUP3D:
1323 snprintf(prog_pos, prog_remain, yuv_lookup3d_prog_template, conv_texs[0]);
1324 break;
1325 default:
1326 mp_msg(MSGT_VO, MSGL_ERR, "[gl] unknown conversion type %i\n", YUV_CONVERSION(type));
1327 break;
1329 mp_msg(MSGT_VO, MSGL_DBG2, "[gl] generated fragment program:\n%s\n", yuv_prog);
1330 loadGPUProgram(GL_FRAGMENT_PROGRAM, yuv_prog);
1331 free(yuv_prog);
1335 * \brief setup YUV->RGB conversion
1336 * \param parms struct containing parameters like conversion and scaler type,
1337 * brightness, ...
1338 * \ingroup glconversion
1340 void glSetupYUVConversion(gl_conversion_params_t *params) {
1341 float uvcos = params->csp_params.saturation * cos(params->csp_params.hue);
1342 float uvsin = params->csp_params.saturation * sin(params->csp_params.hue);
1343 switch (YUV_CONVERSION(params->type)) {
1344 case YUV_CONVERSION_COMBINERS:
1345 glSetupYUVCombiners(uvcos, uvsin);
1346 break;
1347 case YUV_CONVERSION_COMBINERS_ATI:
1348 glSetupYUVCombinersATI(uvcos, uvsin);
1349 break;
1350 case YUV_CONVERSION_FRAGMENT_LOOKUP:
1351 case YUV_CONVERSION_FRAGMENT_LOOKUP3D:
1352 case YUV_CONVERSION_FRAGMENT:
1353 case YUV_CONVERSION_FRAGMENT_POW:
1354 glSetupYUVFragprog(params);
1355 break;
1356 case YUV_CONVERSION_NONE:
1357 break;
1358 default:
1359 mp_msg(MSGT_VO, MSGL_ERR, "[gl] unknown conversion type %i\n", YUV_CONVERSION(params->type));
1364 * \brief enable the specified YUV conversion
1365 * \param target texture target for Y, U and V textures (e.g. GL_TEXTURE_2D)
1366 * \param type type of YUV conversion
1367 * \ingroup glconversion
1369 void glEnableYUVConversion(GLenum target, int type) {
1370 switch (YUV_CONVERSION(type)) {
1371 case YUV_CONVERSION_COMBINERS:
1372 ActiveTexture(GL_TEXTURE1);
1373 Enable(target);
1374 ActiveTexture(GL_TEXTURE2);
1375 Enable(target);
1376 ActiveTexture(GL_TEXTURE0);
1377 Enable(GL_REGISTER_COMBINERS_NV);
1378 break;
1379 case YUV_CONVERSION_COMBINERS_ATI:
1380 ActiveTexture(GL_TEXTURE1);
1381 Enable(target);
1382 ActiveTexture(GL_TEXTURE2);
1383 Enable(target);
1384 ActiveTexture(GL_TEXTURE0);
1385 Enable(GL_FRAGMENT_SHADER_ATI);
1386 break;
1387 case YUV_CONVERSION_FRAGMENT_LOOKUP3D:
1388 case YUV_CONVERSION_FRAGMENT_LOOKUP:
1389 case YUV_CONVERSION_FRAGMENT_POW:
1390 case YUV_CONVERSION_FRAGMENT:
1391 case YUV_CONVERSION_NONE:
1392 Enable(GL_FRAGMENT_PROGRAM);
1393 break;
1398 * \brief disable the specified YUV conversion
1399 * \param target texture target for Y, U and V textures (e.g. GL_TEXTURE_2D)
1400 * \param type type of YUV conversion
1401 * \ingroup glconversion
1403 void glDisableYUVConversion(GLenum target, int type) {
1404 switch (YUV_CONVERSION(type)) {
1405 case YUV_CONVERSION_COMBINERS:
1406 ActiveTexture(GL_TEXTURE1);
1407 Disable(target);
1408 ActiveTexture(GL_TEXTURE2);
1409 Disable(target);
1410 ActiveTexture(GL_TEXTURE0);
1411 Disable(GL_REGISTER_COMBINERS_NV);
1412 break;
1413 case YUV_CONVERSION_COMBINERS_ATI:
1414 ActiveTexture(GL_TEXTURE1);
1415 Disable(target);
1416 ActiveTexture(GL_TEXTURE2);
1417 Disable(target);
1418 ActiveTexture(GL_TEXTURE0);
1419 Disable(GL_FRAGMENT_SHADER_ATI);
1420 break;
1421 case YUV_CONVERSION_FRAGMENT_LOOKUP3D:
1422 case YUV_CONVERSION_FRAGMENT_LOOKUP:
1423 case YUV_CONVERSION_FRAGMENT_POW:
1424 case YUV_CONVERSION_FRAGMENT:
1425 case YUV_CONVERSION_NONE:
1426 Disable(GL_FRAGMENT_PROGRAM);
1427 break;
1432 * \brief draw a texture part at given 2D coordinates
1433 * \param x screen top coordinate
1434 * \param y screen left coordinate
1435 * \param w screen width coordinate
1436 * \param h screen height coordinate
1437 * \param tx texture top coordinate in pixels
1438 * \param ty texture left coordinate in pixels
1439 * \param tw texture part width in pixels
1440 * \param th texture part height in pixels
1441 * \param sx width of texture in pixels
1442 * \param sy height of texture in pixels
1443 * \param rect_tex whether this texture uses texture_rectangle extension
1444 * \param is_yv12 if != 0, also draw the textures from units 1 and 2,
1445 * bits 8 - 15 and 16 - 23 specify the x and y scaling of those textures
1446 * \param flip flip the texture upside down
1447 * \ingroup gltexture
1449 void glDrawTex(GLfloat x, GLfloat y, GLfloat w, GLfloat h,
1450 GLfloat tx, GLfloat ty, GLfloat tw, GLfloat th,
1451 int sx, int sy, int rect_tex, int is_yv12, int flip) {
1452 int chroma_x_shift = (is_yv12 >> 8) & 31;
1453 int chroma_y_shift = (is_yv12 >> 16) & 31;
1454 GLfloat xscale = 1 << chroma_x_shift;
1455 GLfloat yscale = 1 << chroma_y_shift;
1456 GLfloat tx2 = tx / xscale, ty2 = ty / yscale, tw2 = tw / xscale, th2 = th / yscale;
1457 if (!rect_tex) {
1458 tx /= sx; ty /= sy; tw /= sx; th /= sy;
1459 tx2 = tx, ty2 = ty, tw2 = tw, th2 = th;
1461 if (flip) {
1462 y += h;
1463 h = -h;
1465 Begin(GL_QUADS);
1466 TexCoord2f(tx, ty);
1467 if (is_yv12) {
1468 MultiTexCoord2f(GL_TEXTURE1, tx2, ty2);
1469 MultiTexCoord2f(GL_TEXTURE2, tx2, ty2);
1471 Vertex2f(x, y);
1472 TexCoord2f(tx, ty + th);
1473 if (is_yv12) {
1474 MultiTexCoord2f(GL_TEXTURE1, tx2, ty2 + th2);
1475 MultiTexCoord2f(GL_TEXTURE2, tx2, ty2 + th2);
1477 Vertex2f(x, y + h);
1478 TexCoord2f(tx + tw, ty + th);
1479 if (is_yv12) {
1480 MultiTexCoord2f(GL_TEXTURE1, tx2 + tw2, ty2 + th2);
1481 MultiTexCoord2f(GL_TEXTURE2, tx2 + tw2, ty2 + th2);
1483 Vertex2f(x + w, y + h);
1484 TexCoord2f(tx + tw, ty);
1485 if (is_yv12) {
1486 MultiTexCoord2f(GL_TEXTURE1, tx2 + tw2, ty2);
1487 MultiTexCoord2f(GL_TEXTURE2, tx2 + tw2, ty2);
1489 Vertex2f(x + w, y);
1490 End();
1493 #ifdef CONFIG_GL_WIN32
1494 #include "w32_common.h"
1496 * \brief little helper since wglGetProcAddress definition does not fit our
1497 * getProcAddress
1498 * \param procName name of function to look up
1499 * \return function pointer returned by wglGetProcAddress
1501 static void *w32gpa(const GLubyte *procName) {
1502 HMODULE oglmod;
1503 void *res = wglGetProcAddress(procName);
1504 if (res) return res;
1505 oglmod = GetModuleHandle("opengl32.dll");
1506 return GetProcAddress(oglmod, procName);
1509 static int setGlWindow_w32(MPGLContext *ctx)
1511 HWND win = vo_w32_window;
1512 int *vinfo = &ctx->vinfo.w32;
1513 HGLRC *context = &ctx->context.w32;
1514 int new_vinfo;
1515 HDC windc = vo_w32_get_dc(win);
1516 HGLRC new_context = 0;
1517 int keep_context = 0;
1518 int res = SET_WINDOW_FAILED;
1520 // should only be needed when keeping context, but not doing glFinish
1521 // can cause flickering even when we do not keep it.
1522 if (*context)
1523 Finish();
1524 new_vinfo = GetPixelFormat(windc);
1525 if (*context && *vinfo && new_vinfo && *vinfo == new_vinfo) {
1526 // we can keep the wglContext
1527 new_context = *context;
1528 keep_context = 1;
1529 } else {
1530 // create a context
1531 new_context = wglCreateContext(windc);
1532 if (!new_context) {
1533 mp_msg(MSGT_VO, MSGL_FATAL, "[gl] Could not create GL context!\n");
1534 goto out;
1538 // set context
1539 if (!wglMakeCurrent(windc, new_context)) {
1540 mp_msg(MSGT_VO, MSGL_FATAL, "[gl] Could not set GL context!\n");
1541 if (!keep_context) {
1542 wglDeleteContext(new_context);
1544 goto out;
1547 // set new values
1548 vo_w32_window = win;
1550 RECT rect;
1551 GetClientRect(win, &rect);
1552 vo_dwidth = rect.right;
1553 vo_dheight = rect.bottom;
1555 if (!keep_context) {
1556 if (*context)
1557 wglDeleteContext(*context);
1558 *context = new_context;
1559 *vinfo = new_vinfo;
1560 getFunctions(w32gpa, NULL);
1562 // and inform that reinit is neccessary
1563 res = SET_WINDOW_REINIT;
1564 } else
1565 res = SET_WINDOW_OK;
1567 out:
1568 vo_w32_release_dc(win, windc);
1569 return res;
1572 static void releaseGlContext_w32(MPGLContext *ctx) {
1573 int *vinfo = &ctx->vinfo.w32;
1574 HGLRC *context = &ctx->context.w32;
1575 *vinfo = 0;
1576 if (*context) {
1577 wglMakeCurrent(0, 0);
1578 wglDeleteContext(*context);
1580 *context = 0;
1583 static void swapGlBuffers_w32(MPGLContext *ctx) {
1584 HDC vo_hdc = vo_w32_get_dc(vo_w32_window);
1585 SwapBuffers(vo_hdc);
1586 vo_w32_release_dc(vo_w32_window, vo_hdc);
1588 #endif
1589 #ifdef CONFIG_GL_X11
1590 #ifdef HAVE_LIBDL
1591 #include <dlfcn.h>
1592 #endif
1593 #include "x11_common.h"
1595 * \brief find address of a linked function
1596 * \param s name of function to find
1597 * \return address of function or NULL if not found
1599 static void *getdladdr(const char *s) {
1600 void *ret = NULL;
1601 #ifdef HAVE_LIBDL
1602 void *handle = dlopen(NULL, RTLD_LAZY);
1603 if (!handle)
1604 return NULL;
1605 ret = dlsym(handle, s);
1606 dlclose(handle);
1607 #endif
1608 return ret;
1612 * \brief Returns the XVisualInfo associated with Window win.
1613 * \param win Window whose XVisualInfo is returne.
1614 * \return XVisualInfo of the window. Caller must use XFree to free it.
1616 static XVisualInfo *getWindowVisualInfo(Window win) {
1617 XWindowAttributes xw_attr;
1618 XVisualInfo vinfo_template;
1619 int tmp;
1620 XGetWindowAttributes(mDisplay, win, &xw_attr);
1621 vinfo_template.visualid = XVisualIDFromVisual(xw_attr.visual);
1622 return XGetVisualInfo(mDisplay, VisualIDMask, &vinfo_template, &tmp);
1625 static void appendstr(char **dst, const char *str)
1627 int newsize;
1628 char *newstr;
1629 if (!str)
1630 return;
1631 newsize = strlen(*dst) + 1 + strlen(str) + 1;
1632 newstr = realloc(*dst, newsize);
1633 if (!newstr)
1634 return;
1635 *dst = newstr;
1636 strcat(*dst, " ");
1637 strcat(*dst, str);
1641 * \brief Changes the window in which video is displayed.
1642 * If possible only transfers the context to the new window, otherwise
1643 * creates a new one, which must be initialized by the caller.
1644 * \param vinfo Currently used visual.
1645 * \param context Currently used context.
1646 * \param win window that should be used for drawing.
1647 * \return one of SET_WINDOW_FAILED, SET_WINDOW_OK or SET_WINDOW_REINIT.
1648 * In case of SET_WINDOW_REINIT the context could not be transfered
1649 * and the caller must initialize it correctly.
1650 * \ingroup glcontext
1652 static int setGlWindow_x11(MPGLContext *ctx)
1654 XVisualInfo **vinfo = &ctx->vinfo.x11;
1655 GLXContext *context = &ctx->context.x11;
1656 Window win = vo_window;
1657 XVisualInfo *new_vinfo;
1658 GLXContext new_context = NULL;
1659 int keep_context = 0;
1661 // should only be needed when keeping context, but not doing glFinish
1662 // can cause flickering even when we do not keep it.
1663 if (*context)
1664 Finish();
1665 new_vinfo = getWindowVisualInfo(win);
1666 if (*context && *vinfo && new_vinfo &&
1667 (*vinfo)->visualid == new_vinfo->visualid) {
1668 // we can keep the GLXContext
1669 new_context = *context;
1670 XFree(new_vinfo);
1671 new_vinfo = *vinfo;
1672 keep_context = 1;
1673 } else {
1674 // create a context
1675 new_context = glXCreateContext(mDisplay, new_vinfo, NULL, True);
1676 if (!new_context) {
1677 mp_msg(MSGT_VO, MSGL_FATAL, "[gl] Could not create GLX context!\n");
1678 XFree(new_vinfo);
1679 return SET_WINDOW_FAILED;
1683 // set context
1684 if (!glXMakeCurrent(mDisplay, vo_window, new_context)) {
1685 mp_msg(MSGT_VO, MSGL_FATAL, "[gl] Could not set GLX context!\n");
1686 if (!keep_context) {
1687 glXDestroyContext(mDisplay, new_context);
1688 XFree(new_vinfo);
1690 return SET_WINDOW_FAILED;
1693 // set new values
1694 vo_window = win;
1695 vo_x11_update_geometry();
1696 if (!keep_context) {
1697 void *(*getProcAddress)(const GLubyte *);
1698 const char *(*glXExtStr)(Display *, int);
1699 char *glxstr = strdup("");
1700 if (*context)
1701 glXDestroyContext(mDisplay, *context);
1702 *context = new_context;
1703 if (*vinfo)
1704 XFree(*vinfo);
1705 *vinfo = new_vinfo;
1706 getProcAddress = getdladdr("glXGetProcAddress");
1707 if (!getProcAddress)
1708 getProcAddress = getdladdr("glXGetProcAddressARB");
1709 if (!getProcAddress)
1710 getProcAddress = (void *)getdladdr;
1711 glXExtStr = getdladdr("glXQueryExtensionsString");
1712 if (glXExtStr)
1713 appendstr(&glxstr, glXExtStr(mDisplay, DefaultScreen(mDisplay)));
1714 glXExtStr = getdladdr("glXGetClientString");
1715 if (glXExtStr)
1716 appendstr(&glxstr, glXExtStr(mDisplay, GLX_EXTENSIONS));
1717 glXExtStr = getdladdr("glXGetServerString");
1718 if (glXExtStr)
1719 appendstr(&glxstr, glXExtStr(mDisplay, GLX_EXTENSIONS));
1721 getFunctions(getProcAddress, glxstr);
1722 free(glxstr);
1724 // and inform that reinit is neccessary
1725 return SET_WINDOW_REINIT;
1727 return SET_WINDOW_OK;
1731 * \brief free the VisualInfo and GLXContext of an OpenGL context.
1732 * \ingroup glcontext
1734 static void releaseGlContext_x11(MPGLContext *ctx) {
1735 XVisualInfo **vinfo = &ctx->vinfo.x11;
1736 GLXContext *context = &ctx->context.x11;
1737 if (*vinfo)
1738 XFree(*vinfo);
1739 *vinfo = NULL;
1740 if (*context)
1742 Finish();
1743 glXMakeCurrent(mDisplay, None, NULL);
1744 glXDestroyContext(mDisplay, *context);
1746 *context = 0;
1749 static void swapGlBuffers_x11(MPGLContext *ctx) {
1750 glXSwapBuffers(mDisplay, vo_window);
1753 static int x11_check_events(void) {
1754 return vo_x11_check_events(mDisplay);
1757 static void gl_update_xinerama_info(void)
1759 update_xinerama_info(global_vo);
1762 static void gl_border(void)
1764 vo_x11_border();
1767 static void gl_fullscreen(void)
1769 vo_x11_fullscreen();
1772 static void gl_ontop(void)
1774 vo_x11_ontop();
1776 #endif
1778 int init_mpglcontext(MPGLContext *ctx, enum MPGLType type) {
1779 memset(ctx, 0, sizeof(*ctx));
1780 ctx->type = type;
1781 switch (ctx->type) {
1782 #ifdef CONFIG_GL_WIN32
1783 case GLTYPE_W32:
1784 ctx->setGlWindow = setGlWindow_w32;
1785 ctx->releaseGlContext = releaseGlContext_w32;
1786 ctx->swapGlBuffers = swapGlBuffers_w32;
1787 ctx->update_xinerama_info = w32_update_xinerama_info;
1788 ctx->border = vo_w32_border;
1789 ctx->check_events = vo_w32_check_events;
1790 ctx->fullscreen = vo_w32_fullscreen;
1791 ctx->ontop = vo_w32_ontop;
1792 return vo_w32_init();
1793 #endif
1794 #ifdef CONFIG_GL_X11
1795 case GLTYPE_X11:
1796 ctx->setGlWindow = setGlWindow_x11;
1797 ctx->releaseGlContext = releaseGlContext_x11;
1798 ctx->swapGlBuffers = swapGlBuffers_x11;
1799 ctx->update_xinerama_info = gl_update_xinerama_info;
1800 ctx->border = gl_border;
1801 ctx->check_events = x11_check_events;
1802 ctx->fullscreen = gl_fullscreen;
1803 ctx->ontop = gl_ontop;
1804 return vo_init();
1805 #endif
1806 default:
1807 return 0;
1811 void uninit_mpglcontext(MPGLContext *ctx) {
1812 ctx->releaseGlContext(ctx);
1813 switch (ctx->type) {
1814 #ifdef CONFIG_GL_WIN32
1815 case GLTYPE_W32:
1816 vo_w32_uninit();
1817 break;
1818 #endif
1819 #ifdef CONFIG_GL_X11
1820 case GLTYPE_X11:
1821 vo_x11_uninit();
1822 break;
1823 #endif
1825 memset(ctx, 0, sizeof(*ctx));