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.
32 * \brief OpenGL helper functions used by vo_gl.c and vo_gl2.c
40 #include "old_vo_defines.h"
41 #include "gl_common.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
);
100 * \defgroup glextfunctions OpenGL extension functions
102 * the pointers to these functions are acquired when the OpenGL
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
,
116 void (GLAPIENTRY
*mpglCombinerOutput
)(GLenum
, GLenum
, GLenum
, GLenum
, GLenum
,
117 GLenum
, GLenum
, GLboolean
, 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
,
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.
160 void glAdjustAlignment(int stride
) {
164 else if (stride
% 4 == 0)
166 else if (stride
% 2 == 0)
170 mpglPixelStorei(GL_UNPACK_ALIGNMENT
, gl_alignment
);
173 struct gl_name_map_struct
{
179 #define MAP(a) {a, #a}
180 //! mapping table for the glValName function
181 static const struct gl_name_map_struct gl_name_map
[] = {
183 MAP(GL_R3_G3_B2
), MAP(GL_RGB4
), MAP(GL_RGB5
), MAP(GL_RGB8
),
184 MAP(GL_RGB10
), MAP(GL_RGB12
), MAP(GL_RGB16
), MAP(GL_RGBA2
),
185 MAP(GL_RGBA4
), MAP(GL_RGB5_A1
), MAP(GL_RGBA8
), MAP(GL_RGB10_A2
),
186 MAP(GL_RGBA12
), MAP(GL_RGBA16
), MAP(GL_LUMINANCE8
), MAP(GL_LUMINANCE16
),
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
),
193 MAP(GL_BGR
), MAP(GL_BGRA
),
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
),
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
),
211 * \brief return the name of an OpenGL constant
212 * \param value the constant
213 * \return name of the constant or "Unknown format!"
216 const char *glValName(GLint value
)
220 while (gl_name_map
[i
].name
) {
221 if (gl_name_map
[i
].value
== value
)
222 return gl_name_map
[i
].name
;
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.
245 int glFindFormat(uint32_t fmt
, int *bpp
, GLint
*gl_texfmt
,
246 GLenum
*gl_format
, GLenum
*gl_type
)
252 if (!bpp
) bpp
= &dummy1
;
253 if (!gl_texfmt
) gl_texfmt
= &dummy3
;
254 if (!gl_format
) gl_format
= &dummy2
;
255 if (!gl_type
) gl_type
= &dummy2
;
257 if (mp_get_chroma_shift(fmt
, NULL
, NULL
, NULL
)) {
258 // reduce the possible cases a bit
259 if (IMGFMT_IS_YUVP16_LE(fmt
))
260 fmt
= IMGFMT_420P16_LE
;
261 else if (IMGFMT_IS_YUVP16_BE(fmt
))
262 fmt
= IMGFMT_420P16_BE
;
267 *bpp
= IMGFMT_IS_BGR(fmt
)?IMGFMT_BGR_DEPTH(fmt
):IMGFMT_RGB_DEPTH(fmt
);
272 *gl_type
= GL_UNSIGNED_SHORT
;
276 *gl_type
= GL_UNSIGNED_BYTE
;
280 *gl_format
= GL_RGBA
;
281 *gl_type
= GL_UNSIGNED_BYTE
;
284 supported
= 0; // no native YUV support
285 *gl_texfmt
= GL_LUMINANCE16
;
287 *gl_format
= GL_LUMINANCE
;
288 *gl_type
= GL_UNSIGNED_SHORT
;
291 supported
= 0; // no native YV12 support
296 *gl_format
= GL_LUMINANCE
;
297 *gl_type
= GL_UNSIGNED_BYTE
;
300 // IMGFMT_YUY2 would be more logical for the _REV format,
301 // but gives clearly swapped colors.
303 *gl_texfmt
= GL_YCBCR_MESA
;
305 *gl_format
= GL_YCBCR_MESA
;
306 *gl_type
= fmt
== IMGFMT_UYVY
? GL_UNSIGNED_SHORT_8_8
: GL_UNSIGNED_SHORT_8_8_REV
;
309 // we do not support palettized formats, although the format the
310 // swscale produces works
313 gl_type
= GL_UNSIGNED_BYTE_2_3_3_REV
;
317 *gl_format
= GL_RGBA
;
318 *gl_type
= GL_UNSIGNED_SHORT_1_5_5_5_REV
;
322 *gl_type
= GL_UNSIGNED_SHORT_5_6_5_REV
;
326 // special case as red and blue have a differen number of bits.
327 // GL_BGR and GL_UNSIGNED_BYTE_3_3_2 isn't supported at least
328 // by nVidia drivers, and in addition would give more bits to
329 // blue than to red, which isn't wanted
331 gl_type
= GL_UNSIGNED_BYTE_3_3_2
;
335 *gl_format
= GL_BGRA
;
336 *gl_type
= GL_UNSIGNED_SHORT_1_5_5_5_REV
;
340 *gl_type
= GL_UNSIGNED_SHORT_5_6_5
;
344 *gl_type
= GL_UNSIGNED_BYTE
;
348 *gl_format
= GL_BGRA
;
349 *gl_type
= GL_UNSIGNED_BYTE
;
353 *gl_format
= GL_RGBA
;
354 *gl_type
= GL_UNSIGNED_BYTE
;
357 #ifdef TEXTUREFORMAT_ALWAYS
358 *gl_texfmt
= TEXTUREFORMAT_ALWAYS
;
367 * \brief find address of a linked function
368 * \param s name of function to find
369 * \return address of function or NULL if not found
371 static void *getdladdr(const char *s
) {
374 void *handle
= dlopen(NULL
, RTLD_LAZY
);
377 ret
= dlsym(handle
, s
);
386 const char *funcnames
[7];
390 #define DEF_FUNC_DESC(name) {&mpgl##name, NULL, {"gl"#name, NULL}, gl ##name}
391 static const extfunc_desc_t extfuncs
[] = {
392 // these aren't extension functions but we query them anyway to allow
393 // different "backends" with one binary
394 DEF_FUNC_DESC(Begin
),
396 DEF_FUNC_DESC(Viewport
),
397 DEF_FUNC_DESC(MatrixMode
),
398 DEF_FUNC_DESC(LoadIdentity
),
399 DEF_FUNC_DESC(Translated
),
400 DEF_FUNC_DESC(Scaled
),
401 DEF_FUNC_DESC(Ortho
),
402 DEF_FUNC_DESC(Frustum
),
403 DEF_FUNC_DESC(PushMatrix
),
404 DEF_FUNC_DESC(PopMatrix
),
405 DEF_FUNC_DESC(Clear
),
406 DEF_FUNC_DESC(GenLists
),
407 DEF_FUNC_DESC(DeleteLists
),
408 DEF_FUNC_DESC(NewList
),
409 DEF_FUNC_DESC(EndList
),
410 DEF_FUNC_DESC(CallList
),
411 DEF_FUNC_DESC(CallLists
),
412 DEF_FUNC_DESC(GenTextures
),
413 DEF_FUNC_DESC(DeleteTextures
),
414 DEF_FUNC_DESC(TexEnvf
),
415 DEF_FUNC_DESC(TexEnvi
),
416 DEF_FUNC_DESC(Color4ub
),
417 DEF_FUNC_DESC(Color3f
),
418 DEF_FUNC_DESC(Color4f
),
419 DEF_FUNC_DESC(ClearColor
),
420 DEF_FUNC_DESC(ClearDepth
),
421 DEF_FUNC_DESC(DepthFunc
),
422 DEF_FUNC_DESC(Enable
),
423 DEF_FUNC_DESC(Disable
),
424 DEF_FUNC_DESC(DrawBuffer
),
425 DEF_FUNC_DESC(DepthMask
),
426 DEF_FUNC_DESC(BlendFunc
),
427 DEF_FUNC_DESC(Flush
),
428 DEF_FUNC_DESC(Finish
),
429 DEF_FUNC_DESC(PixelStorei
),
430 DEF_FUNC_DESC(TexImage1D
),
431 DEF_FUNC_DESC(TexImage2D
),
432 DEF_FUNC_DESC(TexSubImage2D
),
433 DEF_FUNC_DESC(TexParameteri
),
434 DEF_FUNC_DESC(TexParameterf
),
435 DEF_FUNC_DESC(TexParameterfv
),
436 DEF_FUNC_DESC(TexCoord2f
),
437 DEF_FUNC_DESC(Vertex2f
),
438 DEF_FUNC_DESC(Vertex3f
),
439 DEF_FUNC_DESC(Normal3f
),
440 DEF_FUNC_DESC(Lightfv
),
441 DEF_FUNC_DESC(ColorMaterial
),
442 DEF_FUNC_DESC(ShadeModel
),
443 DEF_FUNC_DESC(GetIntegerv
),
444 DEF_FUNC_DESC(ColorMask
),
446 // here start the real extensions
447 {&mpglGenBuffers
, NULL
, {"glGenBuffers", "glGenBuffersARB", NULL
}},
448 {&mpglDeleteBuffers
, NULL
, {"glDeleteBuffers", "glDeleteBuffersARB", NULL
}},
449 {&mpglBindBuffer
, NULL
, {"glBindBuffer", "glBindBufferARB", NULL
}},
450 {&mpglMapBuffer
, NULL
, {"glMapBuffer", "glMapBufferARB", NULL
}},
451 {&mpglUnmapBuffer
, NULL
, {"glUnmapBuffer", "glUnmapBufferARB", NULL
}},
452 {&mpglBufferData
, NULL
, {"glBufferData", "glBufferDataARB", NULL
}},
453 {&mpglCombinerParameterfv
, "NV_register_combiners", {"glCombinerParameterfv", "glCombinerParameterfvNV", NULL
}},
454 {&mpglCombinerParameteri
, "NV_register_combiners", {"glCombinerParameteri", "glCombinerParameteriNV", NULL
}},
455 {&mpglCombinerInput
, "NV_register_combiners", {"glCombinerInput", "glCombinerInputNV", NULL
}},
456 {&mpglCombinerOutput
, "NV_register_combiners", {"glCombinerOutput", "glCombinerOutputNV", NULL
}},
457 {&mpglBeginFragmentShader
, "ATI_fragment_shader", {"glBeginFragmentShaderATI", NULL
}},
458 {&mpglEndFragmentShader
, "ATI_fragment_shader", {"glEndFragmentShaderATI", NULL
}},
459 {&mpglSampleMap
, "ATI_fragment_shader", {"glSampleMapATI", NULL
}},
460 {&mpglColorFragmentOp2
, "ATI_fragment_shader", {"glColorFragmentOp2ATI", NULL
}},
461 {&mpglColorFragmentOp3
, "ATI_fragment_shader", {"glColorFragmentOp3ATI", NULL
}},
462 {&mpglSetFragmentShaderConstant
, "ATI_fragment_shader", {"glSetFragmentShaderConstantATI", NULL
}},
463 {&mpglActiveTexture
, NULL
, {"glActiveTexture", "glActiveTextureARB", NULL
}},
464 {&mpglBindTexture
, NULL
, {"glBindTexture", "glBindTextureARB", "glBindTextureEXT", NULL
}},
465 {&mpglMultiTexCoord2f
, NULL
, {"glMultiTexCoord2f", "glMultiTexCoord2fARB", NULL
}},
466 {&mpglGenPrograms
, "_program", {"glGenProgramsARB", NULL
}},
467 {&mpglDeletePrograms
, "_program", {"glDeleteProgramsARB", NULL
}},
468 {&mpglBindProgram
, "_program", {"glBindProgramARB", NULL
}},
469 {&mpglProgramString
, "_program", {"glProgramStringARB", NULL
}},
470 {&mpglGetProgramiv
, "_program", {"glGetProgramivARB", NULL
}},
471 {&mpglProgramEnvParameter4f
, "_program", {"glProgramEnvParameter4fARB", NULL
}},
472 {&mpglSwapInterval
, "_swap_control", {"glXSwapIntervalSGI", "glXSwapInterval", "wglSwapIntervalSGI", "wglSwapInterval", "wglSwapIntervalEXT", NULL
}},
473 {&mpglTexImage3D
, NULL
, {"glTexImage3D", NULL
}},
474 {&mpglAllocateMemoryMESA
, "GLX_MESA_allocate_memory", {"glXAllocateMemoryMESA", NULL
}},
475 {&mpglFreeMemoryMESA
, "GLX_MESA_allocate_memory", {"glXFreeMemoryMESA", NULL
}},
480 * \brief find the function pointers of some useful OpenGL extensions
481 * \param getProcAddress function to resolve function names, may be NULL
482 * \param ext2 an extra extension string
484 static void getFunctions(void *(*getProcAddress
)(const GLubyte
*),
486 const extfunc_desc_t
*dsc
;
487 const char *extensions
;
491 getProcAddress
= (void *)getdladdr
;
493 // special case, we need glGetString before starting to find the other functions
494 mpglGetString
= getProcAddress("glGetString");
496 mpglGetString
= glGetString
;
498 extensions
= (const char *)mpglGetString(GL_EXTENSIONS
);
499 if (!extensions
) extensions
= "";
500 if (!ext2
) ext2
= "";
501 allexts
= malloc(strlen(extensions
) + strlen(ext2
) + 2);
502 strcpy(allexts
, extensions
);
503 strcat(allexts
, " ");
504 strcat(allexts
, ext2
);
505 mp_msg(MSGT_VO
, MSGL_DBG2
, "OpenGL extensions string:\n%s\n", allexts
);
506 for (dsc
= extfuncs
; dsc
->funcptr
; dsc
++) {
509 if (!dsc
->extstr
|| strstr(allexts
, dsc
->extstr
)) {
510 for (i
= 0; !ptr
&& dsc
->funcnames
[i
]; i
++)
511 ptr
= getProcAddress((const GLubyte
*)dsc
->funcnames
[i
]);
515 *(void **)dsc
->funcptr
= ptr
;
517 if (strstr(allexts
, "_texture_float"))
518 hqtexfmt
= GL_RGB32F
;
519 else if (strstr(allexts
, "NV_float_buffer"))
520 hqtexfmt
= GL_FLOAT_RGB32_NV
;
527 * \brief create a texture and set some defaults
528 * \param target texture taget, usually GL_TEXTURE_2D
529 * \param fmt internal texture format
530 * \param format texture host data format
531 * \param type texture host data type
532 * \param filter filter used for scaling, e.g. GL_LINEAR
533 * \param w texture width
534 * \param h texture height
535 * \param val luminance value to fill texture with
538 void glCreateClearTex(GLenum target
, GLenum fmt
, GLenum format
, GLenum type
, GLint filter
,
539 int w
, int h
, unsigned char val
) {
540 GLfloat fval
= (GLfloat
)val
/ 255.0;
541 GLfloat border
[4] = {fval
, fval
, fval
, fval
};
546 stride
= w
* glFmt2bpp(format
, type
);
548 init
= malloc(stride
* h
);
549 memset(init
, val
, stride
* h
);
550 glAdjustAlignment(stride
);
551 mpglPixelStorei(GL_UNPACK_ROW_LENGTH
, w
);
552 mpglTexImage2D(target
, 0, fmt
, w
, h
, 0, format
, type
, init
);
553 mpglTexParameterf(target
, GL_TEXTURE_PRIORITY
, 1.0);
554 mpglTexParameteri(target
, GL_TEXTURE_MIN_FILTER
, filter
);
555 mpglTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, filter
);
556 mpglTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
557 mpglTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
558 // Border texels should not be used with CLAMP_TO_EDGE
559 // We set a sane default anyway.
560 mpglTexParameterfv(target
, GL_TEXTURE_BORDER_COLOR
, border
);
565 * \brief creates a texture from a PPM file
566 * \param target texture taget, usually GL_TEXTURE_2D
567 * \param fmt internal texture format, 0 for default
568 * \param filter filter used for scaling, e.g. GL_LINEAR
569 * \param f file to read PPM from
570 * \param width [out] width of texture
571 * \param height [out] height of texture
572 * \param maxval [out] maxval value from PPM file
573 * \return 0 on error, 1 otherwise
576 int glCreatePPMTex(GLenum target
, GLenum fmt
, GLint filter
,
577 FILE *f
, int *width
, int *height
, int *maxval
) {
580 uint8_t *data
= read_pnm(f
, &w
, &h
, &bpp
, &m
);
581 if (!data
|| (bpp
!= 3 && bpp
!= 6)) {
586 fmt
= bpp
== 6 ? hqtexfmt
: 3;
587 if (fmt
== GL_FLOAT_RGB32_NV
&& target
!= GL_TEXTURE_RECTANGLE
)
590 type
= bpp
== 6 ? GL_UNSIGNED_SHORT
: GL_UNSIGNED_BYTE
;
591 glCreateClearTex(target
, fmt
, GL_RGB
, type
, filter
, w
, h
, 0);
592 glUploadTex(target
, GL_RGB
, type
,
593 data
, w
* bpp
, 0, 0, w
, h
, 0);
595 if (width
) *width
= w
;
596 if (height
) *height
= h
;
597 if (maxval
) *maxval
= m
;
602 * \brief return the number of bytes per pixel for the given format
603 * \param format OpenGL format
604 * \param type OpenGL type
605 * \return bytes per pixel
608 * Does not handle all possible variants, just those used by MPlayer
610 int glFmt2bpp(GLenum format
, GLenum type
) {
611 int component_size
= 0;
613 case GL_UNSIGNED_BYTE_3_3_2
:
614 case GL_UNSIGNED_BYTE_2_3_3_REV
:
616 case GL_UNSIGNED_SHORT_5_5_5_1
:
617 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
618 case GL_UNSIGNED_SHORT_5_6_5
:
619 case GL_UNSIGNED_SHORT_5_6_5_REV
:
621 case GL_UNSIGNED_BYTE
:
624 case GL_UNSIGNED_SHORT
:
631 return component_size
;
636 return 3 * component_size
;
639 return 4 * component_size
;
645 * \brief upload a texture, handling things like stride and slices
646 * \param target texture target, usually GL_TEXTURE_2D
647 * \param format OpenGL format of data
648 * \param type OpenGL type of data
649 * \param dataptr data to upload
650 * \param stride data stride
651 * \param x x offset in texture
652 * \param y y offset in texture
653 * \param w width of the texture part to upload
654 * \param h height of the texture part to upload
655 * \param slice height of an upload slice, 0 for all at once
658 void glUploadTex(GLenum target
, GLenum format
, GLenum type
,
659 const void *dataptr
, int stride
,
660 int x
, int y
, int w
, int h
, int slice
) {
661 const uint8_t *data
= dataptr
;
663 if (w
<= 0 || h
<= 0) return;
667 data
+= (h
- 1) * stride
;
670 // this is not always correct, but should work for MPlayer
671 glAdjustAlignment(stride
);
672 mpglPixelStorei(GL_UNPACK_ROW_LENGTH
, stride
/ glFmt2bpp(format
, type
));
673 for (; y
+ slice
<= y_max
; y
+= slice
) {
674 mpglTexSubImage2D(target
, 0, x
, y
, w
, slice
, format
, type
, data
);
675 data
+= stride
* slice
;
678 mpglTexSubImage2D(target
, 0, x
, y
, w
, y_max
- y
, format
, type
, data
);
681 static void fillUVcoeff(GLfloat
*ucoef
, GLfloat
*vcoef
,
682 float uvcos
, float uvsin
) {
684 ucoef
[0] = 0 * uvcos
+ 1.403 * uvsin
;
685 vcoef
[0] = 0 * uvsin
+ 1.403 * uvcos
;
686 ucoef
[1] = -0.344 * uvcos
+ -0.714 * uvsin
;
687 vcoef
[1] = -0.344 * uvsin
+ -0.714 * uvcos
;
688 ucoef
[2] = 1.770 * uvcos
+ 0 * uvsin
;
689 vcoef
[2] = 1.770 * uvsin
+ 0 * uvcos
;
692 // Coefficients (probably) must be in [0, 1] range, whereas they originally
693 // are in [-2, 2] range, so here comes the trick:
694 // First put them in the [-0.5, 0.5] range, then add 0.5.
695 // This can be undone with the HALF_BIAS and SCALE_BY_FOUR arguments
696 // for CombinerInput and CombinerOutput (or the respective ATI variants)
697 for (i
= 0; i
< 4; i
++) {
698 ucoef
[i
] = ucoef
[i
] * 0.25 + 0.5;
699 vcoef
[i
] = vcoef
[i
] * 0.25 + 0.5;
704 * \brief Setup register combiners for YUV to RGB conversion.
705 * \param uvcos used for saturation and hue adjustment
706 * \param uvsin used for saturation and hue adjustment
708 static void glSetupYUVCombiners(float uvcos
, float uvsin
) {
712 if (!mpglCombinerInput
|| !mpglCombinerOutput
||
713 !mpglCombinerParameterfv
|| !mpglCombinerParameteri
) {
714 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Combiner functions missing!\n");
717 mpglGetIntegerv(GL_MAX_GENERAL_COMBINERS_NV
, &i
);
719 mp_msg(MSGT_VO
, MSGL_ERR
,
720 "[gl] 2 general combiners needed for YUV combiner support (found %i)\n", i
);
721 mpglGetIntegerv(GL_MAX_TEXTURE_UNITS
, &i
);
723 mp_msg(MSGT_VO
, MSGL_ERR
,
724 "[gl] 3 texture units needed for YUV combiner support (found %i)\n", i
);
725 fillUVcoeff(ucoef
, vcoef
, uvcos
, uvsin
);
726 mpglCombinerParameterfv(GL_CONSTANT_COLOR0_NV
, ucoef
);
727 mpglCombinerParameterfv(GL_CONSTANT_COLOR1_NV
, vcoef
);
729 // UV first, like this green component cannot overflow
730 mpglCombinerInput(GL_COMBINER0_NV
, GL_RGB
, GL_VARIABLE_A_NV
,
731 GL_TEXTURE1
, GL_HALF_BIAS_NORMAL_NV
, GL_RGB
);
732 mpglCombinerInput(GL_COMBINER0_NV
, GL_RGB
, GL_VARIABLE_B_NV
,
733 GL_CONSTANT_COLOR0_NV
, GL_HALF_BIAS_NORMAL_NV
, GL_RGB
);
734 mpglCombinerInput(GL_COMBINER0_NV
, GL_RGB
, GL_VARIABLE_C_NV
,
735 GL_TEXTURE2
, GL_HALF_BIAS_NORMAL_NV
, GL_RGB
);
736 mpglCombinerInput(GL_COMBINER0_NV
, GL_RGB
, GL_VARIABLE_D_NV
,
737 GL_CONSTANT_COLOR1_NV
, GL_HALF_BIAS_NORMAL_NV
, GL_RGB
);
738 mpglCombinerOutput(GL_COMBINER0_NV
, GL_RGB
, GL_DISCARD_NV
, GL_DISCARD_NV
,
739 GL_SPARE0_NV
, GL_SCALE_BY_FOUR_NV
, GL_NONE
, GL_FALSE
,
743 mpglCombinerInput(GL_COMBINER1_NV
, GL_RGB
, GL_VARIABLE_A_NV
, GL_SPARE0_NV
,
744 GL_SIGNED_IDENTITY_NV
, GL_RGB
);
745 mpglCombinerInput(GL_COMBINER1_NV
, GL_RGB
, GL_VARIABLE_B_NV
, GL_ZERO
,
746 GL_UNSIGNED_INVERT_NV
, GL_RGB
);
747 mpglCombinerInput(GL_COMBINER1_NV
, GL_RGB
, GL_VARIABLE_C_NV
,
748 GL_TEXTURE0
, GL_SIGNED_IDENTITY_NV
, GL_RGB
);
749 mpglCombinerInput(GL_COMBINER1_NV
, GL_RGB
, GL_VARIABLE_D_NV
, GL_ZERO
,
750 GL_UNSIGNED_INVERT_NV
, GL_RGB
);
751 mpglCombinerOutput(GL_COMBINER1_NV
, GL_RGB
, GL_DISCARD_NV
, GL_DISCARD_NV
,
752 GL_SPARE0_NV
, GL_NONE
, GL_NONE
, GL_FALSE
,
755 // leave final combiner stage in default mode
756 mpglCombinerParameteri(GL_NUM_GENERAL_COMBINERS_NV
, 2);
760 * \brief Setup ATI version of register combiners for YUV to RGB conversion.
761 * \param csp_params parameters used for colorspace conversion
762 * \param text if set use the GL_ATI_text_fragment_shader API as
765 static void glSetupYUVFragmentATI(struct mp_csp_params
*csp_params
,
770 mpglGetIntegerv (GL_MAX_TEXTURE_UNITS
, &i
);
772 mp_msg(MSGT_VO
, MSGL_ERR
,
773 "[gl] 3 texture units needed for YUV combiner (ATI) support (found %i)\n", i
);
775 mp_get_yuv2rgb_coeffs(csp_params
, yuv2rgb
);
776 for (i
= 0; i
< 3; i
++) {
778 yuv2rgb
[i
][3] -= -0.5 * (yuv2rgb
[i
][1] + yuv2rgb
[i
][2]);
779 for (j
= 0; j
< 4; j
++) {
780 yuv2rgb
[i
][j
] *= 0.125;
781 yuv2rgb
[i
][j
] += 0.5;
782 if (yuv2rgb
[i
][j
] > 1)
784 if (yuv2rgb
[i
][j
] < 0)
789 GLfloat c0
[4] = {yuv2rgb
[0][0], yuv2rgb
[1][0], yuv2rgb
[2][0]};
790 GLfloat c1
[4] = {yuv2rgb
[0][1], yuv2rgb
[1][1], yuv2rgb
[2][1]};
791 GLfloat c2
[4] = {yuv2rgb
[0][2], yuv2rgb
[1][2], yuv2rgb
[2][2]};
792 GLfloat c3
[4] = {yuv2rgb
[0][3], yuv2rgb
[1][3], yuv2rgb
[2][3]};
793 if (!mpglBeginFragmentShader
|| !mpglEndFragmentShader
||
794 !mpglSetFragmentShaderConstant
|| !mpglSampleMap
||
795 !mpglColorFragmentOp2
|| !mpglColorFragmentOp3
) {
796 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Combiner (ATI) functions missing!\n");
799 mpglGetIntegerv(GL_NUM_FRAGMENT_REGISTERS_ATI
, &i
);
801 mp_msg(MSGT_VO
, MSGL_ERR
,
802 "[gl] 3 registers needed for YUV combiner (ATI) support (found %i)\n", i
);
803 mpglBeginFragmentShader();
804 mpglSetFragmentShaderConstant(GL_CON_0_ATI
, c0
);
805 mpglSetFragmentShaderConstant(GL_CON_1_ATI
, c1
);
806 mpglSetFragmentShaderConstant(GL_CON_2_ATI
, c2
);
807 mpglSetFragmentShaderConstant(GL_CON_3_ATI
, c3
);
808 mpglSampleMap(GL_REG_0_ATI
, GL_TEXTURE0
, GL_SWIZZLE_STR_ATI
);
809 mpglSampleMap(GL_REG_1_ATI
, GL_TEXTURE1
, GL_SWIZZLE_STR_ATI
);
810 mpglSampleMap(GL_REG_2_ATI
, GL_TEXTURE2
, GL_SWIZZLE_STR_ATI
);
811 mpglColorFragmentOp2(GL_MUL_ATI
, GL_REG_1_ATI
, GL_NONE
, GL_NONE
,
812 GL_REG_1_ATI
, GL_NONE
, GL_BIAS_BIT_ATI
,
813 GL_CON_1_ATI
, GL_NONE
, GL_BIAS_BIT_ATI
);
814 mpglColorFragmentOp3(GL_MAD_ATI
, GL_REG_2_ATI
, GL_NONE
, GL_NONE
,
815 GL_REG_2_ATI
, GL_NONE
, GL_BIAS_BIT_ATI
,
816 GL_CON_2_ATI
, GL_NONE
, GL_BIAS_BIT_ATI
,
817 GL_REG_1_ATI
, GL_NONE
, GL_NONE
);
818 mpglColorFragmentOp3(GL_MAD_ATI
, GL_REG_0_ATI
, GL_NONE
, GL_NONE
,
819 GL_REG_0_ATI
, GL_NONE
, GL_NONE
,
820 GL_CON_0_ATI
, GL_NONE
, GL_BIAS_BIT_ATI
,
821 GL_REG_2_ATI
, GL_NONE
, GL_NONE
);
822 mpglColorFragmentOp2(GL_ADD_ATI
, GL_REG_0_ATI
, GL_NONE
, GL_8X_BIT_ATI
,
823 GL_REG_0_ATI
, GL_NONE
, GL_NONE
,
824 GL_CON_3_ATI
, GL_NONE
, GL_BIAS_BIT_ATI
);
825 mpglEndFragmentShader();
827 static const char template[] =
830 " CONSTANT c0 = {%e, %e, %e};\n"
831 " CONSTANT c1 = {%e, %e, %e};\n"
832 " CONSTANT c2 = {%e, %e, %e};\n"
833 " CONSTANT c3 = {%e, %e, %e};\n"
836 " SampleMap r0, t0.str;\n"
837 " SampleMap r1, t1.str;\n"
838 " SampleMap r2, t2.str;\n"
839 " MUL r1.rgb, r1.bias, c1.bias;\n"
840 " MAD r2.rgb, r2.bias, c2.bias, r1;\n"
841 " MAD r0.rgb, r0, c0.bias, r2;\n"
842 " ADD r0.rgb.8x, r0, c3.bias;\n"
845 snprintf(buffer
, sizeof(buffer
), template,
846 yuv2rgb
[0][0], yuv2rgb
[1][0], yuv2rgb
[2][0],
847 yuv2rgb
[0][1], yuv2rgb
[1][1], yuv2rgb
[2][1],
848 yuv2rgb
[0][2], yuv2rgb
[1][2], yuv2rgb
[2][2],
849 yuv2rgb
[0][3], yuv2rgb
[1][3], yuv2rgb
[2][3]);
850 mp_msg(MSGT_VO
, MSGL_DBG2
, "[gl] generated fragment program:\n%s\n", buffer
);
851 loadGPUProgram(GL_TEXT_FRAGMENT_SHADER_ATI
, buffer
);
856 * \brief helper function for gen_spline_lookup_tex
857 * \param x subpixel-position ((0,1) range) to calculate weights for
858 * \param dst where to store transformed weights, must provide space for 4 GLfloats
860 * calculates the weights and stores them after appropriate transformation
861 * for the scaler fragment program.
863 static void store_weights(float x
, GLfloat
*dst
) {
864 float w0
= (((-1 * x
+ 3) * x
- 3) * x
+ 1) / 6;
865 float w1
= ((( 3 * x
- 6) * x
+ 0) * x
+ 4) / 6;
866 float w2
= (((-3 * x
+ 3) * x
+ 3) * x
+ 1) / 6;
867 float w3
= ((( 1 * x
+ 0) * x
+ 0) * x
+ 0) / 6;
868 *dst
++ = 1 + x
- w1
/ (w0
+ w1
);
869 *dst
++ = 1 - x
+ w3
/ (w2
+ w3
);
874 //! to avoid artefacts this should be rather large
875 #define LOOKUP_BSPLINE_RES (2 * 1024)
877 * \brief creates the 1D lookup texture needed for fast higher-order filtering
878 * \param unit texture unit to attach texture to
880 static void gen_spline_lookup_tex(GLenum unit
) {
881 GLfloat
*tex
= calloc(4 * LOOKUP_BSPLINE_RES
, sizeof(*tex
));
884 for (i
= 0; i
< LOOKUP_BSPLINE_RES
; i
++) {
885 float x
= (float)(i
+ 0.5) / LOOKUP_BSPLINE_RES
;
886 store_weights(x
, tp
);
889 store_weights(0, tex
);
890 store_weights(1, &tex
[4 * (LOOKUP_BSPLINE_RES
- 1)]);
891 mpglActiveTexture(unit
);
892 mpglTexImage1D(GL_TEXTURE_1D
, 0, GL_RGBA16
, LOOKUP_BSPLINE_RES
, 0, GL_RGBA
, GL_FLOAT
, tex
);
893 mpglTexParameterf(GL_TEXTURE_1D
, GL_TEXTURE_PRIORITY
, 1.0);
894 mpglTexParameteri(GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
895 mpglTexParameteri(GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
896 mpglTexParameteri(GL_TEXTURE_1D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
897 mpglActiveTexture(GL_TEXTURE0
);
901 static const char *bilin_filt_template
=
902 "TEX yuv.%c, fragment.texcoord[%c], texture[%c], %s;\n";
904 #define BICUB_FILT_MAIN(textype) \
905 /* first y-interpolation */ \
906 "ADD coord, fragment.texcoord[%c].xyxy, cdelta.xyxw;\n" \
907 "ADD coord2, fragment.texcoord[%c].xyxy, cdelta.zyzw;\n" \
908 "TEX a.r, coord.xyxy, texture[%c], "textype";\n" \
909 "TEX a.g, coord.zwzw, texture[%c], "textype";\n" \
910 /* second y-interpolation */ \
911 "TEX b.r, coord2.xyxy, texture[%c], "textype";\n" \
912 "TEX b.g, coord2.zwzw, texture[%c], "textype";\n" \
913 "LRP a.b, parmy.b, a.rrrr, a.gggg;\n" \
914 "LRP a.a, parmy.b, b.rrrr, b.gggg;\n" \
915 /* x-interpolation */ \
916 "LRP yuv.%c, parmx.b, a.bbbb, a.aaaa;\n"
918 static const char *bicub_filt_template_2D
=
919 "MAD coord.xy, fragment.texcoord[%c], {%e, %e}, {0.5, 0.5};\n"
920 "TEX parmx, coord.x, texture[%c], 1D;\n"
921 "MUL cdelta.xz, parmx.rrgg, {-%e, 0, %e, 0};\n"
922 "TEX parmy, coord.y, texture[%c], 1D;\n"
923 "MUL cdelta.yw, parmy.rrgg, {0, -%e, 0, %e};\n"
924 BICUB_FILT_MAIN("2D");
926 static const char *bicub_filt_template_RECT
=
927 "ADD coord, fragment.texcoord[%c], {0.5, 0.5};\n"
928 "TEX parmx, coord.x, texture[%c], 1D;\n"
929 "MUL cdelta.xz, parmx.rrgg, {-1, 0, 1, 0};\n"
930 "TEX parmy, coord.y, texture[%c], 1D;\n"
931 "MUL cdelta.yw, parmy.rrgg, {0, -1, 0, 1};\n"
932 BICUB_FILT_MAIN("RECT");
934 #define CALCWEIGHTS(t, s) \
935 "MAD "t", {-0.5, 0.1666, 0.3333, -0.3333}, "s", {1, 0, -0.5, 0.5};\n" \
936 "MAD "t", "t", "s", {0, 0, -0.5, 0.5};\n" \
937 "MAD "t", "t", "s", {-0.6666, 0, 0.8333, 0.1666};\n" \
938 "RCP a.x, "t".z;\n" \
939 "RCP a.y, "t".w;\n" \
940 "MAD "t".xy, "t".xyxy, a.xyxy, {1, 1, 0, 0};\n" \
941 "ADD "t".x, "t".xxxx, "s";\n" \
942 "SUB "t".y, "t".yyyy, "s";\n"
944 static const char *bicub_notex_filt_template_2D
=
945 "MAD coord.xy, fragment.texcoord[%c], {%e, %e}, {0.5, 0.5};\n"
946 "FRC coord.xy, coord.xyxy;\n"
947 CALCWEIGHTS("parmx", "coord.xxxx")
948 "MUL cdelta.xz, parmx.rrgg, {-%e, 0, %e, 0};\n"
949 CALCWEIGHTS("parmy", "coord.yyyy")
950 "MUL cdelta.yw, parmy.rrgg, {0, -%e, 0, %e};\n"
951 BICUB_FILT_MAIN("2D");
953 static const char *bicub_notex_filt_template_RECT
=
954 "ADD coord, fragment.texcoord[%c], {0.5, 0.5};\n"
955 "FRC coord.xy, coord.xyxy;\n"
956 CALCWEIGHTS("parmx", "coord.xxxx")
957 "MUL cdelta.xz, parmx.rrgg, {-1, 0, 1, 0};\n"
958 CALCWEIGHTS("parmy", "coord.yyyy")
959 "MUL cdelta.yw, parmy.rrgg, {0, -1, 0, 1};\n"
960 BICUB_FILT_MAIN("RECT");
962 #define BICUB_X_FILT_MAIN(textype) \
963 "ADD coord.xy, fragment.texcoord[%c].xyxy, cdelta.xyxy;\n" \
964 "ADD coord2.xy, fragment.texcoord[%c].xyxy, cdelta.zyzy;\n" \
965 "TEX a.r, coord, texture[%c], "textype";\n" \
966 "TEX b.r, coord2, texture[%c], "textype";\n" \
967 /* x-interpolation */ \
968 "LRP yuv.%c, parmx.b, a.rrrr, b.rrrr;\n"
970 static const char *bicub_x_filt_template_2D
=
971 "MAD coord.x, fragment.texcoord[%c], {%e}, {0.5};\n"
972 "TEX parmx, coord, texture[%c], 1D;\n"
973 "MUL cdelta.xyz, parmx.rrgg, {-%e, 0, %e};\n"
974 BICUB_X_FILT_MAIN("2D");
976 static const char *bicub_x_filt_template_RECT
=
977 "ADD coord.x, fragment.texcoord[%c], {0.5};\n"
978 "TEX parmx, coord, texture[%c], 1D;\n"
979 "MUL cdelta.xyz, parmx.rrgg, {-1, 0, 1};\n"
980 BICUB_X_FILT_MAIN("RECT");
982 static const char *unsharp_filt_template
=
983 "PARAM dcoord%c = {%e, %e, %e, %e};\n"
984 "ADD coord, fragment.texcoord[%c].xyxy, dcoord%c;\n"
985 "SUB coord2, fragment.texcoord[%c].xyxy, dcoord%c;\n"
986 "TEX a.r, fragment.texcoord[%c], texture[%c], %s;\n"
987 "TEX b.r, coord.xyxy, texture[%c], %s;\n"
988 "TEX b.g, coord.zwzw, texture[%c], %s;\n"
989 "ADD b.r, b.r, b.g;\n"
990 "TEX b.b, coord2.xyxy, texture[%c], %s;\n"
991 "TEX b.g, coord2.zwzw, texture[%c], %s;\n"
992 "DP3 b, b, {0.25, 0.25, 0.25};\n"
993 "SUB b.r, a.r, b.r;\n"
994 "MAD yuv.%c, b.r, {%e}, a.r;\n";
996 static const char *unsharp_filt_template2
=
997 "PARAM dcoord%c = {%e, %e, %e, %e};\n"
998 "PARAM dcoord2%c = {%e, 0, 0, %e};\n"
999 "ADD coord, fragment.texcoord[%c].xyxy, dcoord%c;\n"
1000 "SUB coord2, fragment.texcoord[%c].xyxy, dcoord%c;\n"
1001 "TEX a.r, fragment.texcoord[%c], texture[%c], %s;\n"
1002 "TEX b.r, coord.xyxy, texture[%c], %s;\n"
1003 "TEX b.g, coord.zwzw, texture[%c], %s;\n"
1004 "ADD b.r, b.r, b.g;\n"
1005 "TEX b.b, coord2.xyxy, texture[%c], %s;\n"
1006 "TEX b.g, coord2.zwzw, texture[%c], %s;\n"
1007 "ADD b.r, b.r, b.b;\n"
1008 "ADD b.a, b.r, b.g;\n"
1009 "ADD coord, fragment.texcoord[%c].xyxy, dcoord2%c;\n"
1010 "SUB coord2, fragment.texcoord[%c].xyxy, dcoord2%c;\n"
1011 "TEX b.r, coord.xyxy, texture[%c], %s;\n"
1012 "TEX b.g, coord.zwzw, texture[%c], %s;\n"
1013 "ADD b.r, b.r, b.g;\n"
1014 "TEX b.b, coord2.xyxy, texture[%c], %s;\n"
1015 "TEX b.g, coord2.zwzw, texture[%c], %s;\n"
1016 "DP4 b.r, b, {-0.1171875, -0.1171875, -0.1171875, -0.09765625};\n"
1017 "MAD b.r, a.r, {0.859375}, b.r;\n"
1018 "MAD yuv.%c, b.r, {%e}, a.r;\n";
1020 static const char *yuv_prog_template
=
1021 "PARAM ycoef = {%e, %e, %e};\n"
1022 "PARAM ucoef = {%e, %e, %e};\n"
1023 "PARAM vcoef = {%e, %e, %e};\n"
1024 "PARAM offsets = {%e, %e, %e};\n"
1026 "MAD res.rgb, yuv.rrrr, ycoef, offsets;\n"
1027 "MAD res.rgb, yuv.gggg, ucoef, res;\n"
1028 "MAD result.color.rgb, yuv.bbbb, vcoef, res;\n"
1031 static const char *yuv_pow_prog_template
=
1032 "PARAM ycoef = {%e, %e, %e};\n"
1033 "PARAM ucoef = {%e, %e, %e};\n"
1034 "PARAM vcoef = {%e, %e, %e};\n"
1035 "PARAM offsets = {%e, %e, %e};\n"
1036 "PARAM gamma = {%e, %e, %e};\n"
1038 "MAD res.rgb, yuv.rrrr, ycoef, offsets;\n"
1039 "MAD res.rgb, yuv.gggg, ucoef, res;\n"
1040 "MAD_SAT res.rgb, yuv.bbbb, vcoef, res;\n"
1041 "POW result.color.r, res.r, gamma.r;\n"
1042 "POW result.color.g, res.g, gamma.g;\n"
1043 "POW result.color.b, res.b, gamma.b;\n"
1046 static const char *yuv_lookup_prog_template
=
1047 "PARAM ycoef = {%e, %e, %e, 0};\n"
1048 "PARAM ucoef = {%e, %e, %e, 0};\n"
1049 "PARAM vcoef = {%e, %e, %e, 0};\n"
1050 "PARAM offsets = {%e, %e, %e, 0.125};\n"
1052 "MAD res, yuv.rrrr, ycoef, offsets;\n"
1053 "MAD res.rgb, yuv.gggg, ucoef, res;\n"
1054 "MAD res.rgb, yuv.bbbb, vcoef, res;\n"
1055 "TEX result.color.r, res.raaa, texture[%c], 2D;\n"
1056 "ADD res.a, res.a, 0.25;\n"
1057 "TEX result.color.g, res.gaaa, texture[%c], 2D;\n"
1058 "ADD res.a, res.a, 0.25;\n"
1059 "TEX result.color.b, res.baaa, texture[%c], 2D;\n"
1062 static const char *yuv_lookup3d_prog_template
=
1063 "TEX result.color, yuv, texture[%c], 3D;\n"
1067 * \brief creates and initializes helper textures needed for scaling texture read
1068 * \param scaler scaler type to create texture for
1069 * \param texu contains next free texture unit number
1070 * \param texs texture unit ids for the scaler are stored in this array
1072 static void create_scaler_textures(int scaler
, int *texu
, char *texs
) {
1074 case YUV_SCALER_BILIN
:
1075 case YUV_SCALER_BICUB_NOTEX
:
1076 case YUV_SCALER_UNSHARP
:
1077 case YUV_SCALER_UNSHARP2
:
1079 case YUV_SCALER_BICUB
:
1080 case YUV_SCALER_BICUB_X
:
1081 texs
[0] = (*texu
)++;
1082 gen_spline_lookup_tex(GL_TEXTURE0
+ texs
[0]);
1086 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] unknown scaler type %i\n", scaler
);
1090 //! resolution of texture for gamma lookup table
1091 #define LOOKUP_RES 512
1092 //! resolution for 3D yuv->rgb conversion lookup table
1093 #define LOOKUP_3DRES 32
1095 * \brief creates and initializes helper textures needed for yuv conversion
1096 * \param params struct containing parameters like brightness, gamma, ...
1097 * \param texu contains next free texture unit number
1098 * \param texs texture unit ids for the conversion are stored in this array
1100 static void create_conv_textures(gl_conversion_params_t
*params
, int *texu
, char *texs
) {
1101 unsigned char *lookup_data
= NULL
;
1102 int conv
= YUV_CONVERSION(params
->type
);
1104 case YUV_CONVERSION_FRAGMENT
:
1105 case YUV_CONVERSION_FRAGMENT_POW
:
1107 case YUV_CONVERSION_FRAGMENT_LOOKUP
:
1108 texs
[0] = (*texu
)++;
1109 mpglActiveTexture(GL_TEXTURE0
+ texs
[0]);
1110 lookup_data
= malloc(4 * LOOKUP_RES
);
1111 mp_gen_gamma_map(lookup_data
, LOOKUP_RES
, params
->csp_params
.rgamma
);
1112 mp_gen_gamma_map(&lookup_data
[LOOKUP_RES
], LOOKUP_RES
, params
->csp_params
.ggamma
);
1113 mp_gen_gamma_map(&lookup_data
[2 * LOOKUP_RES
], LOOKUP_RES
, params
->csp_params
.bgamma
);
1114 glCreateClearTex(GL_TEXTURE_2D
, GL_LUMINANCE8
, GL_LUMINANCE
, GL_UNSIGNED_BYTE
, GL_LINEAR
,
1116 glUploadTex(GL_TEXTURE_2D
, GL_LUMINANCE
, GL_UNSIGNED_BYTE
, lookup_data
,
1117 LOOKUP_RES
, 0, 0, LOOKUP_RES
, 4, 0);
1118 mpglActiveTexture(GL_TEXTURE0
);
1121 case YUV_CONVERSION_FRAGMENT_LOOKUP3D
:
1123 int sz
= LOOKUP_3DRES
+ 2; // texture size including borders
1124 if (!mpglTexImage3D
) {
1125 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] Missing 3D texture function!\n");
1128 texs
[0] = (*texu
)++;
1129 mpglActiveTexture(GL_TEXTURE0
+ texs
[0]);
1130 lookup_data
= malloc(3 * sz
* sz
* sz
);
1131 mp_gen_yuv2rgb_map(¶ms
->csp_params
, lookup_data
, LOOKUP_3DRES
);
1132 glAdjustAlignment(sz
);
1133 mpglPixelStorei(GL_UNPACK_ROW_LENGTH
, 0);
1134 mpglTexImage3D(GL_TEXTURE_3D
, 0, 3, sz
, sz
, sz
, 1,
1135 GL_RGB
, GL_UNSIGNED_BYTE
, lookup_data
);
1136 mpglTexParameterf(GL_TEXTURE_3D
, GL_TEXTURE_PRIORITY
, 1.0);
1137 mpglTexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
1138 mpglTexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
1139 mpglTexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_WRAP_S
, GL_CLAMP
);
1140 mpglTexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_WRAP_T
, GL_CLAMP
);
1141 mpglTexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_WRAP_R
, GL_CLAMP
);
1142 mpglActiveTexture(GL_TEXTURE0
);
1147 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] unknown conversion type %i\n", conv
);
1153 * \brief adds a scaling texture read at the current fragment program position
1154 * \param scaler type of scaler to insert
1155 * \param prog_pos current position in fragment program
1156 * \param remain how many bytes remain in the buffer given by prog_pos
1157 * \param texs array containing the texture unit identifiers for this scaler
1158 * \param in_tex texture unit the scaler should read from
1159 * \param out_comp component of the yuv variable the scaler stores the result in
1160 * \param rect if rectangular (pixel) adressing should be used for in_tex
1161 * \param texw width of the in_tex texture
1162 * \param texh height of the in_tex texture
1163 * \param strength strength of filter effect if the scaler does some kind of filtering
1165 static void add_scaler(int scaler
, char **prog_pos
, int *remain
, char *texs
,
1166 char in_tex
, char out_comp
, int rect
, int texw
, int texh
,
1168 const char *ttype
= rect
? "RECT" : "2D";
1169 const float ptw
= rect
? 1.0 : 1.0 / texw
;
1170 const float pth
= rect
? 1.0 : 1.0 / texh
;
1172 case YUV_SCALER_BILIN
:
1173 snprintf(*prog_pos
, *remain
, bilin_filt_template
, out_comp
, in_tex
,
1176 case YUV_SCALER_BICUB
:
1178 snprintf(*prog_pos
, *remain
, bicub_filt_template_RECT
,
1179 in_tex
, texs
[0], texs
[0],
1180 in_tex
, in_tex
, in_tex
, in_tex
, in_tex
, in_tex
, out_comp
);
1182 snprintf(*prog_pos
, *remain
, bicub_filt_template_2D
,
1183 in_tex
, (float)texw
, (float)texh
,
1184 texs
[0], ptw
, ptw
, texs
[0], pth
, pth
,
1185 in_tex
, in_tex
, in_tex
, in_tex
, in_tex
, in_tex
, out_comp
);
1187 case YUV_SCALER_BICUB_X
:
1189 snprintf(*prog_pos
, *remain
, bicub_x_filt_template_RECT
,
1191 in_tex
, in_tex
, in_tex
, in_tex
, out_comp
);
1193 snprintf(*prog_pos
, *remain
, bicub_x_filt_template_2D
,
1194 in_tex
, (float)texw
,
1196 in_tex
, in_tex
, in_tex
, in_tex
, out_comp
);
1198 case YUV_SCALER_BICUB_NOTEX
:
1200 snprintf(*prog_pos
, *remain
, bicub_notex_filt_template_RECT
,
1202 in_tex
, in_tex
, in_tex
, in_tex
, in_tex
, in_tex
, out_comp
);
1204 snprintf(*prog_pos
, *remain
, bicub_notex_filt_template_2D
,
1205 in_tex
, (float)texw
, (float)texh
, ptw
, ptw
, pth
, pth
,
1206 in_tex
, in_tex
, in_tex
, in_tex
, in_tex
, in_tex
, out_comp
);
1208 case YUV_SCALER_UNSHARP
:
1209 snprintf(*prog_pos
, *remain
, unsharp_filt_template
,
1210 out_comp
, 0.5 * ptw
, 0.5 * pth
, 0.5 * ptw
, -0.5 * pth
,
1211 in_tex
, out_comp
, in_tex
, out_comp
, in_tex
,
1212 in_tex
, ttype
, in_tex
, ttype
, in_tex
, ttype
, in_tex
, ttype
,
1213 in_tex
, ttype
, out_comp
, strength
);
1215 case YUV_SCALER_UNSHARP2
:
1216 snprintf(*prog_pos
, *remain
, unsharp_filt_template2
,
1217 out_comp
, 1.2 * ptw
, 1.2 * pth
, 1.2 * ptw
, -1.2 * pth
,
1218 out_comp
, 1.5 * ptw
, 1.5 * pth
,
1219 in_tex
, out_comp
, in_tex
, out_comp
, in_tex
,
1220 in_tex
, ttype
, in_tex
, ttype
, in_tex
, ttype
, in_tex
, ttype
,
1221 in_tex
, ttype
, in_tex
, out_comp
, in_tex
, out_comp
,
1222 in_tex
, ttype
, in_tex
, ttype
, in_tex
, ttype
,
1223 in_tex
, ttype
, out_comp
, strength
);
1226 *remain
-= strlen(*prog_pos
);
1227 *prog_pos
+= strlen(*prog_pos
);
1230 static const struct {
1235 {"instructions", 0x88A0, 0x88A1},
1236 {"native instructions", 0x88A2, 0x88A3},
1237 {"temporaries", 0x88A4, 0x88A5},
1238 {"native temporaries", 0x88A6, 0x88A7},
1239 {"parameters", 0x88A8, 0x88A9},
1240 {"native parameters", 0x88AA, 0x88AB},
1241 {"attribs", 0x88AC, 0x88AD},
1242 {"native attribs", 0x88AE, 0x88AF},
1243 {"ALU instructions", 0x8805, 0x880B},
1244 {"TEX instructions", 0x8806, 0x880C},
1245 {"TEX indirections", 0x8807, 0x880D},
1246 {"native ALU instructions", 0x8808, 0x880E},
1247 {"native TEX instructions", 0x8809, 0x880F},
1248 {"native TEX indirections", 0x880A, 0x8810},
1253 * \brief load the specified GPU Program
1254 * \param target program target to load into, only GL_FRAGMENT_PROGRAM is tested
1255 * \param prog program string
1256 * \return 1 on success, 0 otherwise
1258 int loadGPUProgram(GLenum target
, char *prog
) {
1260 GLint cur
= 0, max
= 0, err
= 0;
1261 if (!mpglProgramString
) {
1262 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] Missing GPU program function\n");
1265 mpglProgramString(target
, GL_PROGRAM_FORMAT_ASCII
, strlen(prog
), prog
);
1266 mpglGetIntegerv(GL_PROGRAM_ERROR_POSITION
, &err
);
1268 mp_msg(MSGT_VO
, MSGL_ERR
,
1269 "[gl] Error compiling fragment program, make sure your card supports\n"
1270 "[gl] GL_ARB_fragment_program (use glxinfo to check).\n"
1271 "[gl] Error message:\n %s at %.10s\n",
1272 mpglGetString(GL_PROGRAM_ERROR_STRING
), &prog
[err
]);
1275 if (!mpglGetProgramiv
|| !mp_msg_test(MSGT_VO
, MSGL_DBG2
))
1277 mp_msg(MSGT_VO
, MSGL_V
, "[gl] Program statistics:\n");
1278 for (i
= 0; progstats
[i
].name
; i
++) {
1279 mpglGetProgramiv(target
, progstats
[i
].cur
, &cur
);
1280 mpglGetProgramiv(target
, progstats
[i
].max
, &max
);
1281 mp_msg(MSGT_VO
, MSGL_V
, "[gl] %s: %i/%i\n", progstats
[i
].name
, cur
, max
);
1286 #define MAX_PROGSZ (1024*1024)
1289 * \brief setup a fragment program that will do YUV->RGB conversion
1290 * \param parms struct containing parameters like conversion and scaler type,
1293 static void glSetupYUVFragprog(gl_conversion_params_t
*params
) {
1294 int type
= params
->type
;
1295 int texw
= params
->texw
;
1296 int texh
= params
->texh
;
1297 int rect
= params
->target
== GL_TEXTURE_RECTANGLE
;
1298 static const char prog_hdr
[] =
1300 "OPTION ARB_precision_hint_fastest;\n"
1301 // all scaler variables must go here so they aren't defined
1302 // multiple times when the same scaler is used more than once
1303 "TEMP coord, coord2, cdelta, parmx, parmy, a, b, yuv;\n";
1305 char *yuv_prog
, *prog_pos
;
1307 char lum_scale_texs
[1];
1308 char chrom_scale_texs
[1];
1311 // this is the conversion matrix, with y, u, v factors
1312 // for red, green, blue and the constant offsets
1313 float yuv2rgb
[3][4];
1314 create_conv_textures(params
, &cur_texu
, conv_texs
);
1315 create_scaler_textures(YUV_LUM_SCALER(type
), &cur_texu
, lum_scale_texs
);
1316 if (YUV_CHROM_SCALER(type
) == YUV_LUM_SCALER(type
))
1317 memcpy(chrom_scale_texs
, lum_scale_texs
, sizeof(chrom_scale_texs
));
1319 create_scaler_textures(YUV_CHROM_SCALER(type
), &cur_texu
, chrom_scale_texs
);
1320 mpglGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS
, &i
);
1322 mp_msg(MSGT_VO
, MSGL_ERR
,
1323 "[gl] %i texture units needed for this type of YUV fragment support (found %i)\n",
1325 if (!mpglProgramString
) {
1326 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] ProgramString function missing!\n");
1329 yuv_prog
= malloc(MAX_PROGSZ
);
1330 strcpy(yuv_prog
, prog_hdr
);
1331 prog_pos
= yuv_prog
+ sizeof(prog_hdr
) - 1;
1332 prog_remain
= MAX_PROGSZ
- sizeof(prog_hdr
);
1333 add_scaler(YUV_LUM_SCALER(type
), &prog_pos
, &prog_remain
, lum_scale_texs
,
1334 '0', 'r', rect
, texw
, texh
, params
->filter_strength
);
1335 add_scaler(YUV_CHROM_SCALER(type
), &prog_pos
, &prog_remain
, chrom_scale_texs
,
1336 '1', 'g', rect
, params
->chrom_texw
, params
->chrom_texh
, params
->filter_strength
);
1337 add_scaler(YUV_CHROM_SCALER(type
), &prog_pos
, &prog_remain
, chrom_scale_texs
,
1338 '2', 'b', rect
, params
->chrom_texw
, params
->chrom_texh
, params
->filter_strength
);
1339 mp_get_yuv2rgb_coeffs(¶ms
->csp_params
, yuv2rgb
);
1340 switch (YUV_CONVERSION(type
)) {
1341 case YUV_CONVERSION_FRAGMENT
:
1342 snprintf(prog_pos
, prog_remain
, yuv_prog_template
,
1343 yuv2rgb
[ROW_R
][COL_Y
], yuv2rgb
[ROW_G
][COL_Y
], yuv2rgb
[ROW_B
][COL_Y
],
1344 yuv2rgb
[ROW_R
][COL_U
], yuv2rgb
[ROW_G
][COL_U
], yuv2rgb
[ROW_B
][COL_U
],
1345 yuv2rgb
[ROW_R
][COL_V
], yuv2rgb
[ROW_G
][COL_V
], yuv2rgb
[ROW_B
][COL_V
],
1346 yuv2rgb
[ROW_R
][COL_C
], yuv2rgb
[ROW_G
][COL_C
], yuv2rgb
[ROW_B
][COL_C
]);
1348 case YUV_CONVERSION_FRAGMENT_POW
:
1349 snprintf(prog_pos
, prog_remain
, yuv_pow_prog_template
,
1350 yuv2rgb
[ROW_R
][COL_Y
], yuv2rgb
[ROW_G
][COL_Y
], yuv2rgb
[ROW_B
][COL_Y
],
1351 yuv2rgb
[ROW_R
][COL_U
], yuv2rgb
[ROW_G
][COL_U
], yuv2rgb
[ROW_B
][COL_U
],
1352 yuv2rgb
[ROW_R
][COL_V
], yuv2rgb
[ROW_G
][COL_V
], yuv2rgb
[ROW_B
][COL_V
],
1353 yuv2rgb
[ROW_R
][COL_C
], yuv2rgb
[ROW_G
][COL_C
], yuv2rgb
[ROW_B
][COL_C
],
1354 (float)1.0 / params
->csp_params
.rgamma
, (float)1.0 / params
->csp_params
.bgamma
, (float)1.0 / params
->csp_params
.bgamma
);
1356 case YUV_CONVERSION_FRAGMENT_LOOKUP
:
1357 snprintf(prog_pos
, prog_remain
, yuv_lookup_prog_template
,
1358 yuv2rgb
[ROW_R
][COL_Y
], yuv2rgb
[ROW_G
][COL_Y
], yuv2rgb
[ROW_B
][COL_Y
],
1359 yuv2rgb
[ROW_R
][COL_U
], yuv2rgb
[ROW_G
][COL_U
], yuv2rgb
[ROW_B
][COL_U
],
1360 yuv2rgb
[ROW_R
][COL_V
], yuv2rgb
[ROW_G
][COL_V
], yuv2rgb
[ROW_B
][COL_V
],
1361 yuv2rgb
[ROW_R
][COL_C
], yuv2rgb
[ROW_G
][COL_C
], yuv2rgb
[ROW_B
][COL_C
],
1362 conv_texs
[0], conv_texs
[0], conv_texs
[0]);
1364 case YUV_CONVERSION_FRAGMENT_LOOKUP3D
:
1365 snprintf(prog_pos
, prog_remain
, yuv_lookup3d_prog_template
, conv_texs
[0]);
1368 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] unknown conversion type %i\n", YUV_CONVERSION(type
));
1371 mp_msg(MSGT_VO
, MSGL_DBG2
, "[gl] generated fragment program:\n%s\n", yuv_prog
);
1372 loadGPUProgram(GL_FRAGMENT_PROGRAM
, yuv_prog
);
1377 * \brief detect the best YUV->RGB conversion method available
1379 int glAutodetectYUVConversion(void) {
1380 const char *extensions
= mpglGetString(GL_EXTENSIONS
);
1381 if (!extensions
|| !mpglMultiTexCoord2f
)
1382 return YUV_CONVERSION_NONE
;
1383 if (strstr(extensions
, "GL_ARB_fragment_program"))
1384 return YUV_CONVERSION_FRAGMENT
;
1385 if (strstr(extensions
, "GL_ATI_text_fragment_shader"))
1386 return YUV_CONVERSION_TEXT_FRAGMENT
;
1387 if (strstr(extensions
, "GL_ATI_fragment_shader"))
1388 return YUV_CONVERSION_COMBINERS_ATI
;
1389 return YUV_CONVERSION_NONE
;
1393 * \brief setup YUV->RGB conversion
1394 * \param parms struct containing parameters like conversion and scaler type,
1396 * \ingroup glconversion
1398 void glSetupYUVConversion(gl_conversion_params_t
*params
) {
1399 float uvcos
= params
->csp_params
.saturation
* cos(params
->csp_params
.hue
);
1400 float uvsin
= params
->csp_params
.saturation
* sin(params
->csp_params
.hue
);
1401 if (params
->chrom_texw
== 0) params
->chrom_texw
= 1;
1402 if (params
->chrom_texh
== 0) params
->chrom_texh
= 1;
1403 switch (YUV_CONVERSION(params
->type
)) {
1404 case YUV_CONVERSION_COMBINERS
:
1405 glSetupYUVCombiners(uvcos
, uvsin
);
1407 case YUV_CONVERSION_COMBINERS_ATI
:
1408 glSetupYUVFragmentATI(¶ms
->csp_params
, 0);
1410 case YUV_CONVERSION_TEXT_FRAGMENT
:
1411 glSetupYUVFragmentATI(¶ms
->csp_params
, 1);
1413 case YUV_CONVERSION_FRAGMENT_LOOKUP
:
1414 case YUV_CONVERSION_FRAGMENT_LOOKUP3D
:
1415 case YUV_CONVERSION_FRAGMENT
:
1416 case YUV_CONVERSION_FRAGMENT_POW
:
1417 glSetupYUVFragprog(params
);
1419 case YUV_CONVERSION_NONE
:
1422 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] unknown conversion type %i\n", YUV_CONVERSION(params
->type
));
1427 * \brief enable the specified YUV conversion
1428 * \param target texture target for Y, U and V textures (e.g. GL_TEXTURE_2D)
1429 * \param type type of YUV conversion
1430 * \ingroup glconversion
1432 void glEnableYUVConversion(GLenum target
, int type
) {
1433 switch (YUV_CONVERSION(type
)) {
1434 case YUV_CONVERSION_COMBINERS
:
1435 mpglActiveTexture(GL_TEXTURE1
);
1437 mpglActiveTexture(GL_TEXTURE2
);
1439 mpglActiveTexture(GL_TEXTURE0
);
1440 mpglEnable(GL_REGISTER_COMBINERS_NV
);
1442 case YUV_CONVERSION_COMBINERS_ATI
:
1443 mpglActiveTexture(GL_TEXTURE1
);
1445 mpglActiveTexture(GL_TEXTURE2
);
1447 mpglActiveTexture(GL_TEXTURE0
);
1448 mpglEnable(GL_FRAGMENT_SHADER_ATI
);
1450 case YUV_CONVERSION_TEXT_FRAGMENT
:
1451 mpglActiveTexture(GL_TEXTURE1
);
1453 mpglActiveTexture(GL_TEXTURE2
);
1455 mpglActiveTexture(GL_TEXTURE0
);
1456 mpglEnable(GL_TEXT_FRAGMENT_SHADER_ATI
);
1458 case YUV_CONVERSION_FRAGMENT_LOOKUP3D
:
1459 case YUV_CONVERSION_FRAGMENT_LOOKUP
:
1460 case YUV_CONVERSION_FRAGMENT_POW
:
1461 case YUV_CONVERSION_FRAGMENT
:
1462 case YUV_CONVERSION_NONE
:
1463 mpglEnable(GL_FRAGMENT_PROGRAM
);
1469 * \brief disable the specified YUV conversion
1470 * \param target texture target for Y, U and V textures (e.g. GL_TEXTURE_2D)
1471 * \param type type of YUV conversion
1472 * \ingroup glconversion
1474 void glDisableYUVConversion(GLenum target
, int type
) {
1475 switch (YUV_CONVERSION(type
)) {
1476 case YUV_CONVERSION_COMBINERS
:
1477 mpglActiveTexture(GL_TEXTURE1
);
1478 mpglDisable(target
);
1479 mpglActiveTexture(GL_TEXTURE2
);
1480 mpglDisable(target
);
1481 mpglActiveTexture(GL_TEXTURE0
);
1482 mpglDisable(GL_REGISTER_COMBINERS_NV
);
1484 case YUV_CONVERSION_COMBINERS_ATI
:
1485 mpglActiveTexture(GL_TEXTURE1
);
1486 mpglDisable(target
);
1487 mpglActiveTexture(GL_TEXTURE2
);
1488 mpglDisable(target
);
1489 mpglActiveTexture(GL_TEXTURE0
);
1490 mpglDisable(GL_FRAGMENT_SHADER_ATI
);
1492 case YUV_CONVERSION_TEXT_FRAGMENT
:
1493 mpglDisable(GL_TEXT_FRAGMENT_SHADER_ATI
);
1494 // HACK: at least the Mac OS X 10.5 PPC Radeon drivers are broken and
1495 // without this disable the texture units while the program is still
1496 // running (10.4 PPC seems to work without this though).
1498 mpglActiveTexture(GL_TEXTURE1
);
1499 mpglDisable(target
);
1500 mpglActiveTexture(GL_TEXTURE2
);
1501 mpglDisable(target
);
1502 mpglActiveTexture(GL_TEXTURE0
);
1504 case YUV_CONVERSION_FRAGMENT_LOOKUP3D
:
1505 case YUV_CONVERSION_FRAGMENT_LOOKUP
:
1506 case YUV_CONVERSION_FRAGMENT_POW
:
1507 case YUV_CONVERSION_FRAGMENT
:
1508 case YUV_CONVERSION_NONE
:
1509 mpglDisable(GL_FRAGMENT_PROGRAM
);
1514 void glEnable3DLeft(int type
) {
1517 case GL_3D_RED_CYAN
:
1518 mpglColorMask(GL_TRUE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1520 case GL_3D_GREEN_MAGENTA
:
1521 mpglColorMask(GL_FALSE
, GL_TRUE
, GL_FALSE
, GL_FALSE
);
1523 case GL_3D_QUADBUFFER
:
1524 mpglGetIntegerv(GL_DRAW_BUFFER
, &buffer
);
1528 case GL_FRONT_RIGHT
:
1529 buffer
= GL_FRONT_LEFT
;
1534 buffer
= GL_BACK_LEFT
;
1537 mpglDrawBuffer(buffer
);
1542 void glEnable3DRight(int type
) {
1545 case GL_3D_RED_CYAN
:
1546 mpglColorMask(GL_FALSE
, GL_TRUE
, GL_TRUE
, GL_FALSE
);
1548 case GL_3D_GREEN_MAGENTA
:
1549 mpglColorMask(GL_TRUE
, GL_FALSE
, GL_TRUE
, GL_FALSE
);
1551 case GL_3D_QUADBUFFER
:
1552 mpglGetIntegerv(GL_DRAW_BUFFER
, &buffer
);
1556 case GL_FRONT_RIGHT
:
1557 buffer
= GL_FRONT_RIGHT
;
1562 buffer
= GL_BACK_RIGHT
;
1565 mpglDrawBuffer(buffer
);
1570 void glDisable3D(int type
) {
1573 case GL_3D_RED_CYAN
:
1574 case GL_3D_GREEN_MAGENTA
:
1575 mpglColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
1577 case GL_3D_QUADBUFFER
:
1578 mpglDrawBuffer(vo_doublebuffering
? GL_BACK
: GL_FRONT
);
1579 mpglGetIntegerv(GL_DRAW_BUFFER
, &buffer
);
1583 case GL_FRONT_RIGHT
:
1592 mpglDrawBuffer(buffer
);
1598 * \brief draw a texture part at given 2D coordinates
1599 * \param x screen top coordinate
1600 * \param y screen left coordinate
1601 * \param w screen width coordinate
1602 * \param h screen height coordinate
1603 * \param tx texture top coordinate in pixels
1604 * \param ty texture left coordinate in pixels
1605 * \param tw texture part width in pixels
1606 * \param th texture part height in pixels
1607 * \param sx width of texture in pixels
1608 * \param sy height of texture in pixels
1609 * \param rect_tex whether this texture uses texture_rectangle extension
1610 * \param is_yv12 if != 0, also draw the textures from units 1 and 2,
1611 * bits 8 - 15 and 16 - 23 specify the x and y scaling of those textures
1612 * \param flip flip the texture upside down
1613 * \ingroup gltexture
1615 void glDrawTex(GLfloat x
, GLfloat y
, GLfloat w
, GLfloat h
,
1616 GLfloat tx
, GLfloat ty
, GLfloat tw
, GLfloat th
,
1617 int sx
, int sy
, int rect_tex
, int is_yv12
, int flip
) {
1618 int chroma_x_shift
= (is_yv12
>> 8) & 31;
1619 int chroma_y_shift
= (is_yv12
>> 16) & 31;
1620 GLfloat xscale
= 1 << chroma_x_shift
;
1621 GLfloat yscale
= 1 << chroma_y_shift
;
1622 GLfloat tx2
= tx
/ xscale
, ty2
= ty
/ yscale
, tw2
= tw
/ xscale
, th2
= th
/ yscale
;
1624 tx
/= sx
; ty
/= sy
; tw
/= sx
; th
/= sy
;
1625 tx2
= tx
, ty2
= ty
, tw2
= tw
, th2
= th
;
1631 mpglBegin(GL_QUADS
);
1632 mpglTexCoord2f(tx
, ty
);
1634 mpglMultiTexCoord2f(GL_TEXTURE1
, tx2
, ty2
);
1635 mpglMultiTexCoord2f(GL_TEXTURE2
, tx2
, ty2
);
1638 mpglTexCoord2f(tx
, ty
+ th
);
1640 mpglMultiTexCoord2f(GL_TEXTURE1
, tx2
, ty2
+ th2
);
1641 mpglMultiTexCoord2f(GL_TEXTURE2
, tx2
, ty2
+ th2
);
1643 mpglVertex2f(x
, y
+ h
);
1644 mpglTexCoord2f(tx
+ tw
, ty
+ th
);
1646 mpglMultiTexCoord2f(GL_TEXTURE1
, tx2
+ tw2
, ty2
+ th2
);
1647 mpglMultiTexCoord2f(GL_TEXTURE2
, tx2
+ tw2
, ty2
+ th2
);
1649 mpglVertex2f(x
+ w
, y
+ h
);
1650 mpglTexCoord2f(tx
+ tw
, ty
);
1652 mpglMultiTexCoord2f(GL_TEXTURE1
, tx2
+ tw2
, ty2
);
1653 mpglMultiTexCoord2f(GL_TEXTURE2
, tx2
+ tw2
, ty2
);
1655 mpglVertex2f(x
+ w
, y
);
1659 #ifdef CONFIG_GL_WIN32
1660 #include "w32_common.h"
1662 * \brief little helper since wglGetProcAddress definition does not fit our
1664 * \param procName name of function to look up
1665 * \return function pointer returned by wglGetProcAddress
1667 static void *w32gpa(const GLubyte
*procName
) {
1669 void *res
= wglGetProcAddress(procName
);
1670 if (res
) return res
;
1671 oglmod
= GetModuleHandle("opengl32.dll");
1672 return GetProcAddress(oglmod
, procName
);
1675 static int setGlWindow_w32(MPGLContext
*ctx
)
1677 HWND win
= vo_w32_window
;
1678 int *vinfo
= &ctx
->vinfo
.w32
;
1679 HGLRC
*context
= &ctx
->context
.w32
;
1681 HDC windc
= vo_w32_get_dc(win
);
1682 HGLRC new_context
= 0;
1683 int keep_context
= 0;
1684 int res
= SET_WINDOW_FAILED
;
1686 // should only be needed when keeping context, but not doing glFinish
1687 // can cause flickering even when we do not keep it.
1690 new_vinfo
= GetPixelFormat(windc
);
1691 if (*context
&& *vinfo
&& new_vinfo
&& *vinfo
== new_vinfo
) {
1692 // we can keep the wglContext
1693 new_context
= *context
;
1697 new_context
= wglCreateContext(windc
);
1699 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Could not create GL context!\n");
1705 if (!wglMakeCurrent(windc
, new_context
)) {
1706 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Could not set GL context!\n");
1707 if (!keep_context
) {
1708 wglDeleteContext(new_context
);
1714 vo_w32_window
= win
;
1717 GetClientRect(win
, &rect
);
1718 vo_dwidth
= rect
.right
;
1719 vo_dheight
= rect
.bottom
;
1721 if (!keep_context
) {
1723 wglDeleteContext(*context
);
1724 *context
= new_context
;
1726 getFunctions(w32gpa
, NULL
);
1728 // and inform that reinit is neccessary
1729 res
= SET_WINDOW_REINIT
;
1731 res
= SET_WINDOW_OK
;
1734 vo_w32_release_dc(win
, windc
);
1738 static void releaseGlContext_w32(MPGLContext
*ctx
) {
1739 int *vinfo
= &ctx
->vinfo
.w32
;
1740 HGLRC
*context
= &ctx
->context
.w32
;
1743 wglMakeCurrent(0, 0);
1744 wglDeleteContext(*context
);
1749 static void swapGlBuffers_w32(MPGLContext
*ctx
) {
1750 HDC vo_hdc
= vo_w32_get_dc(vo_w32_window
);
1751 SwapBuffers(vo_hdc
);
1752 vo_w32_release_dc(vo_w32_window
, vo_hdc
);
1755 #ifdef CONFIG_GL_X11
1756 #include "x11_common.h"
1759 * \brief Returns the XVisualInfo associated with Window win.
1760 * \param win Window whose XVisualInfo is returne.
1761 * \return XVisualInfo of the window. Caller must use XFree to free it.
1763 static XVisualInfo
*getWindowVisualInfo(Window win
) {
1764 XWindowAttributes xw_attr
;
1765 XVisualInfo vinfo_template
;
1767 XGetWindowAttributes(mDisplay
, win
, &xw_attr
);
1768 vinfo_template
.visualid
= XVisualIDFromVisual(xw_attr
.visual
);
1769 return XGetVisualInfo(mDisplay
, VisualIDMask
, &vinfo_template
, &tmp
);
1772 static void appendstr(char **dst
, const char *str
)
1778 newsize
= strlen(*dst
) + 1 + strlen(str
) + 1;
1779 newstr
= realloc(*dst
, newsize
);
1788 * \brief Changes the window in which video is displayed.
1789 * If possible only transfers the context to the new window, otherwise
1790 * creates a new one, which must be initialized by the caller.
1791 * \param vinfo Currently used visual.
1792 * \param context Currently used context.
1793 * \param win window that should be used for drawing.
1794 * \return one of SET_WINDOW_FAILED, SET_WINDOW_OK or SET_WINDOW_REINIT.
1795 * In case of SET_WINDOW_REINIT the context could not be transfered
1796 * and the caller must initialize it correctly.
1797 * \ingroup glcontext
1799 static int setGlWindow_x11(MPGLContext
*ctx
)
1801 XVisualInfo
**vinfo
= &ctx
->vinfo
.x11
;
1802 GLXContext
*context
= &ctx
->context
.x11
;
1803 Window win
= vo_window
;
1804 XVisualInfo
*new_vinfo
;
1805 GLXContext new_context
= NULL
;
1806 int keep_context
= 0;
1808 // should only be needed when keeping context, but not doing glFinish
1809 // can cause flickering even when we do not keep it.
1812 new_vinfo
= getWindowVisualInfo(win
);
1813 if (*context
&& *vinfo
&& new_vinfo
&&
1814 (*vinfo
)->visualid
== new_vinfo
->visualid
) {
1815 // we can keep the GLXContext
1816 new_context
= *context
;
1822 new_context
= glXCreateContext(mDisplay
, new_vinfo
, NULL
, True
);
1824 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Could not create GLX context!\n");
1826 return SET_WINDOW_FAILED
;
1831 if (!glXMakeCurrent(mDisplay
, vo_window
, new_context
)) {
1832 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Could not set GLX context!\n");
1833 if (!keep_context
) {
1834 glXDestroyContext(mDisplay
, new_context
);
1837 return SET_WINDOW_FAILED
;
1842 vo_x11_update_geometry();
1843 if (!keep_context
) {
1844 void *(*getProcAddress
)(const GLubyte
*);
1845 const char *(*glXExtStr
)(Display
*, int);
1846 char *glxstr
= strdup("");
1848 glXDestroyContext(mDisplay
, *context
);
1849 *context
= new_context
;
1853 getProcAddress
= getdladdr("glXGetProcAddress");
1854 if (!getProcAddress
)
1855 getProcAddress
= getdladdr("glXGetProcAddressARB");
1856 glXExtStr
= getdladdr("glXQueryExtensionsString");
1858 appendstr(&glxstr
, glXExtStr(mDisplay
, DefaultScreen(mDisplay
)));
1859 glXExtStr
= getdladdr("glXGetClientString");
1861 appendstr(&glxstr
, glXExtStr(mDisplay
, GLX_EXTENSIONS
));
1862 glXExtStr
= getdladdr("glXGetServerString");
1864 appendstr(&glxstr
, glXExtStr(mDisplay
, GLX_EXTENSIONS
));
1866 getFunctions(getProcAddress
, glxstr
);
1867 if (!mpglGenPrograms
&& mpglGetString
&&
1869 strstr(mpglGetString(GL_EXTENSIONS
), "GL_ARB_vertex_program")) {
1870 mp_msg(MSGT_VO
, MSGL_WARN
, "Broken glXGetProcAddress detected, trying workaround\n");
1871 getFunctions(NULL
, glxstr
);
1875 // and inform that reinit is neccessary
1876 return SET_WINDOW_REINIT
;
1878 return SET_WINDOW_OK
;
1882 * \brief free the VisualInfo and GLXContext of an OpenGL context.
1883 * \ingroup glcontext
1885 static void releaseGlContext_x11(MPGLContext
*ctx
) {
1886 XVisualInfo
**vinfo
= &ctx
->vinfo
.x11
;
1887 GLXContext
*context
= &ctx
->context
.x11
;
1894 glXMakeCurrent(mDisplay
, None
, NULL
);
1895 glXDestroyContext(mDisplay
, *context
);
1900 static void swapGlBuffers_x11(MPGLContext
*ctx
) {
1901 glXSwapBuffers(mDisplay
, vo_window
);
1904 static int x11_check_events(void) {
1905 return vo_x11_check_events(mDisplay
);
1908 static void gl_update_xinerama_info(void)
1910 update_xinerama_info(global_vo
);
1913 static void gl_border(void)
1918 static void gl_fullscreen(void)
1920 vo_x11_fullscreen();
1923 static void gl_ontop(void)
1929 #ifdef CONFIG_GL_SDL
1930 #include "sdl_common.h"
1932 static void swapGlBuffers_sdl(MPGLContext
*ctx
) {
1933 SDL_GL_SwapBuffers();
1936 static void *sdlgpa(const GLubyte
*name
) {
1937 return SDL_GL_GetProcAddress(name
);
1940 static int setGlWindow_sdl(MPGLContext
*ctx
) {
1941 if (sdl_set_mode(0, SDL_OPENGL
| SDL_RESIZABLE
) < 0)
1942 return SET_WINDOW_FAILED
;
1943 SDL_GL_LoadLibrary(NULL
);
1944 getFunctions(sdlgpa
, NULL
);
1945 return SET_WINDOW_OK
;
1948 static int sdl_check_events(void) {
1951 while (SDL_PollEvent(&event
)) {
1952 res
|= sdl_default_handle_event(&event
);
1954 // poll "events" from within MPlayer code
1955 res
|= sdl_default_handle_event(NULL
);
1956 if (res
& VO_EVENT_RESIZE
)
1957 sdl_set_mode(0, SDL_OPENGL
| SDL_RESIZABLE
);
1963 static int setGlWindow_dummy(MPGLContext
*ctx
) {
1964 getFunctions(NULL
, NULL
);
1965 return SET_WINDOW_OK
;
1968 static void releaseGlContext_dummy(MPGLContext
*ctx
) {
1971 static int dummy_check_events(void) {
1975 static void dummy_update_xinerama_info(void) {
1976 if (vo_screenwidth
<= 0 || vo_screenheight
<= 0) {
1977 mp_msg(MSGT_VO
, MSGL_ERR
, "You must specify the screen dimensions "
1978 "with -screenw and -screenh\n");
1979 vo_screenwidth
= 1280;
1980 vo_screenheight
= 768;
1982 aspect_save_screenres(vo_screenwidth
, vo_screenheight
);
1985 int init_mpglcontext(MPGLContext
*ctx
, enum MPGLType type
) {
1986 if (type
== GLTYPE_AUTO
) {
1987 int res
= init_mpglcontext(ctx
, GLTYPE_W32
);
1988 if (res
) return res
;
1989 res
= init_mpglcontext(ctx
, GLTYPE_X11
);
1990 if (res
) return res
;
1991 res
= init_mpglcontext(ctx
, GLTYPE_SDL
);
1994 memset(ctx
, 0, sizeof(*ctx
));
1995 ctx
->setGlWindow
= setGlWindow_dummy
;
1996 ctx
->releaseGlContext
= releaseGlContext_dummy
;
1997 ctx
->update_xinerama_info
= dummy_update_xinerama_info
;
1998 ctx
->check_events
= dummy_check_events
;
2000 switch (ctx
->type
) {
2001 #ifdef CONFIG_GL_WIN32
2003 ctx
->setGlWindow
= setGlWindow_w32
;
2004 ctx
->releaseGlContext
= releaseGlContext_w32
;
2005 ctx
->swapGlBuffers
= swapGlBuffers_w32
;
2006 ctx
->update_xinerama_info
= w32_update_xinerama_info
;
2007 ctx
->border
= vo_w32_border
;
2008 ctx
->check_events
= vo_w32_check_events
;
2009 ctx
->fullscreen
= vo_w32_fullscreen
;
2010 ctx
->ontop
= vo_w32_ontop
;
2011 return vo_w32_init();
2013 #ifdef CONFIG_GL_X11
2015 ctx
->setGlWindow
= setGlWindow_x11
;
2016 ctx
->releaseGlContext
= releaseGlContext_x11
;
2017 ctx
->swapGlBuffers
= swapGlBuffers_x11
;
2018 ctx
->update_xinerama_info
= gl_update_xinerama_info
;
2019 ctx
->border
= gl_border
;
2020 ctx
->check_events
= x11_check_events
;
2021 ctx
->fullscreen
= gl_fullscreen
;
2022 ctx
->ontop
= gl_ontop
;
2025 #ifdef CONFIG_GL_SDL
2027 SDL_Init(SDL_INIT_VIDEO
);
2028 ctx
->setGlWindow
= setGlWindow_sdl
;
2029 ctx
->swapGlBuffers
= swapGlBuffers_sdl
;
2030 ctx
->check_events
= sdl_check_events
;
2031 ctx
->fullscreen
= vo_sdl_fullscreen
;
2032 return vo_sdl_init();
2039 void uninit_mpglcontext(MPGLContext
*ctx
) {
2040 ctx
->releaseGlContext(ctx
);
2041 switch (ctx
->type
) {
2042 #ifdef CONFIG_GL_WIN32
2047 #ifdef CONFIG_GL_X11
2052 #ifdef CONFIG_GL_SDL
2058 memset(ctx
, 0, sizeof(*ctx
));