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.
27 * \brief OpenGL helper functions used by vo_gl.c and vo_gl2.c
35 #include "gl_common.h"
39 void (GLAPIENTRY
*mpglBegin
)(GLenum
);
40 void (GLAPIENTRY
*mpglEnd
)(void);
41 void (GLAPIENTRY
*mpglViewport
)(GLint
, GLint
, GLsizei
, GLsizei
);
42 void (GLAPIENTRY
*mpglMatrixMode
)(GLenum
);
43 void (GLAPIENTRY
*mpglLoadIdentity
)(void);
44 void (GLAPIENTRY
*mpglTranslated
)(double, double, double);
45 void (GLAPIENTRY
*mpglScaled
)(double, double, double);
46 void (GLAPIENTRY
*mpglOrtho
)(double, double, double, double, double, double);
47 void (GLAPIENTRY
*mpglFrustum
)(double, double, double, double, double, double);
48 void (GLAPIENTRY
*mpglPushMatrix
)(void);
49 void (GLAPIENTRY
*mpglPopMatrix
)(void);
50 void (GLAPIENTRY
*mpglClear
)(GLbitfield
);
51 GLuint (GLAPIENTRY
*mpglGenLists
)(GLsizei
);
52 void (GLAPIENTRY
*mpglDeleteLists
)(GLuint
, GLsizei
);
53 void (GLAPIENTRY
*mpglNewList
)(GLuint
, GLenum
);
54 void (GLAPIENTRY
*mpglEndList
)(void);
55 void (GLAPIENTRY
*mpglCallList
)(GLuint
);
56 void (GLAPIENTRY
*mpglCallLists
)(GLsizei
, GLenum
, const GLvoid
*);
57 void (GLAPIENTRY
*mpglGenTextures
)(GLsizei
, GLuint
*);
58 void (GLAPIENTRY
*mpglDeleteTextures
)(GLsizei
, const GLuint
*);
59 void (GLAPIENTRY
*mpglTexEnvf
)(GLenum
, GLenum
, GLfloat
);
60 void (GLAPIENTRY
*mpglTexEnvi
)(GLenum
, GLenum
, GLint
);
61 void (GLAPIENTRY
*mpglColor4ub
)(GLubyte
, GLubyte
, GLubyte
, GLubyte
);
62 void (GLAPIENTRY
*mpglColor3f
)(GLfloat
, GLfloat
, GLfloat
);
63 void (GLAPIENTRY
*mpglColor4f
)(GLfloat
, GLfloat
, GLfloat
, GLfloat
);
64 void (GLAPIENTRY
*mpglClearColor
)(GLclampf
, GLclampf
, GLclampf
, GLclampf
);
65 void (GLAPIENTRY
*mpglClearDepth
)(GLclampd
);
66 void (GLAPIENTRY
*mpglDepthFunc
)(GLenum
);
67 void (GLAPIENTRY
*mpglEnable
)(GLenum
);
68 void (GLAPIENTRY
*mpglDisable
)(GLenum
);
69 const GLubyte
*(GLAPIENTRY
*mpglGetString
)(GLenum
);
70 void (GLAPIENTRY
*mpglDrawBuffer
)(GLenum
);
71 void (GLAPIENTRY
*mpglDepthMask
)(GLboolean
);
72 void (GLAPIENTRY
*mpglBlendFunc
)(GLenum
, GLenum
);
73 void (GLAPIENTRY
*mpglFlush
)(void);
74 void (GLAPIENTRY
*mpglFinish
)(void);
75 void (GLAPIENTRY
*mpglPixelStorei
)(GLenum
, GLint
);
76 void (GLAPIENTRY
*mpglTexImage1D
)(GLenum
, GLint
, GLint
, GLsizei
, GLint
, GLenum
, GLenum
, const GLvoid
*);
77 void (GLAPIENTRY
*mpglTexImage2D
)(GLenum
, GLint
, GLint
, GLsizei
, GLsizei
, GLint
, GLenum
, GLenum
, const GLvoid
*);
78 void (GLAPIENTRY
*mpglTexSubImage2D
)(GLenum
, GLint
, GLint
, GLint
, GLsizei
, GLsizei
, GLenum
, GLenum
, const GLvoid
*);
79 void (GLAPIENTRY
*mpglTexParameteri
)(GLenum
, GLenum
, GLint
);
80 void (GLAPIENTRY
*mpglTexParameterf
)(GLenum
, GLenum
, GLfloat
);
81 void (GLAPIENTRY
*mpglTexParameterfv
)(GLenum
, GLenum
, const GLfloat
*);
82 void (GLAPIENTRY
*mpglTexCoord2f
)(GLfloat
, GLfloat
);
83 void (GLAPIENTRY
*mpglVertex2f
)(GLfloat
, GLfloat
);
84 void (GLAPIENTRY
*mpglVertex3f
)(GLfloat
, GLfloat
, GLfloat
);
85 void (GLAPIENTRY
*mpglNormal3f
)(GLfloat
, GLfloat
, GLfloat
);
86 void (GLAPIENTRY
*mpglLightfv
)(GLenum
, GLenum
, const GLfloat
*);
87 void (GLAPIENTRY
*mpglColorMaterial
)(GLenum
, GLenum
);
88 void (GLAPIENTRY
*mpglShadeModel
)(GLenum
);
89 void (GLAPIENTRY
*mpglGetIntegerv
)(GLenum
, GLint
*);
92 * \defgroup glextfunctions OpenGL extension functions
94 * the pointers to these functions are acquired when the OpenGL
98 void (GLAPIENTRY
*mpglGenBuffers
)(GLsizei
, GLuint
*);
99 void (GLAPIENTRY
*mpglDeleteBuffers
)(GLsizei
, const GLuint
*);
100 void (GLAPIENTRY
*mpglBindBuffer
)(GLenum
, GLuint
);
101 GLvoid
* (GLAPIENTRY
*mpglMapBuffer
)(GLenum
, GLenum
);
102 GLboolean (GLAPIENTRY
*mpglUnmapBuffer
)(GLenum
);
103 void (GLAPIENTRY
*mpglBufferData
)(GLenum
, intptr_t, const GLvoid
*, GLenum
);
104 void (GLAPIENTRY
*mpglCombinerParameterfv
)(GLenum
, const GLfloat
*);
105 void (GLAPIENTRY
*mpglCombinerParameteri
)(GLenum
, GLint
);
106 void (GLAPIENTRY
*mpglCombinerInput
)(GLenum
, GLenum
, GLenum
, GLenum
, GLenum
,
108 void (GLAPIENTRY
*mpglCombinerOutput
)(GLenum
, GLenum
, GLenum
, GLenum
, GLenum
,
109 GLenum
, GLenum
, GLboolean
, GLboolean
,
111 void (GLAPIENTRY
*mpglBeginFragmentShader
)(void);
112 void (GLAPIENTRY
*mpglEndFragmentShader
)(void);
113 void (GLAPIENTRY
*mpglSampleMap
)(GLuint
, GLuint
, GLenum
);
114 void (GLAPIENTRY
*mpglColorFragmentOp2
)(GLenum
, GLuint
, GLuint
, GLuint
, GLuint
,
115 GLuint
, GLuint
, GLuint
, GLuint
, GLuint
);
116 void (GLAPIENTRY
*mpglColorFragmentOp3
)(GLenum
, GLuint
, GLuint
, GLuint
, GLuint
,
117 GLuint
, GLuint
, GLuint
, GLuint
, GLuint
,
118 GLuint
, GLuint
, GLuint
);
119 void (GLAPIENTRY
*mpglSetFragmentShaderConstant
)(GLuint
, const GLfloat
*);
120 void (GLAPIENTRY
*mpglActiveTexture
)(GLenum
);
121 void (GLAPIENTRY
*mpglBindTexture
)(GLenum
, GLuint
);
122 void (GLAPIENTRY
*mpglMultiTexCoord2f
)(GLenum
, GLfloat
, GLfloat
);
123 void (GLAPIENTRY
*mpglGenPrograms
)(GLsizei
, GLuint
*);
124 void (GLAPIENTRY
*mpglDeletePrograms
)(GLsizei
, const GLuint
*);
125 void (GLAPIENTRY
*mpglBindProgram
)(GLenum
, GLuint
);
126 void (GLAPIENTRY
*mpglProgramString
)(GLenum
, GLenum
, GLsizei
, const GLvoid
*);
127 void (GLAPIENTRY
*mpglGetProgramiv
)(GLenum
, GLenum
, GLint
*);
128 void (GLAPIENTRY
*mpglProgramEnvParameter4f
)(GLenum
, GLuint
, GLfloat
, GLfloat
,
130 int (GLAPIENTRY
*mpglSwapInterval
)(int);
131 void (GLAPIENTRY
*mpglTexImage3D
)(GLenum
, GLint
, GLenum
, GLsizei
, GLsizei
, GLsizei
,
132 GLint
, GLenum
, GLenum
, const GLvoid
*);
133 void* (GLAPIENTRY
*mpglAllocateMemoryMESA
)(void *, int, size_t, float, float, float);
134 void (GLAPIENTRY
*mpglFreeMemoryMESA
)(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.
152 void glAdjustAlignment(int stride
) {
156 else if (stride
% 4 == 0)
158 else if (stride
% 2 == 0)
162 mpglPixelStorei(GL_UNPACK_ALIGNMENT
, gl_alignment
);
165 struct gl_name_map_struct
{
171 #define MAP(a) {a, #a}
172 //! mapping table for the glValName function
173 static const struct gl_name_map_struct gl_name_map
[] = {
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
),
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
),
185 MAP(GL_BGR
), MAP(GL_BGRA
),
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
),
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
),
203 * \brief return the name of an OpenGL constant
204 * \param value the constant
205 * \return name of the constant or "Unknown format!"
208 const char *glValName(GLint value
)
212 while (gl_name_map
[i
].name
) {
213 if (gl_name_map
[i
].value
== value
)
214 return gl_name_map
[i
].name
;
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.
237 int glFindFormat(uint32_t fmt
, int *bpp
, GLint
*gl_texfmt
,
238 GLenum
*gl_format
, GLenum
*gl_type
)
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
;
259 *bpp
= IMGFMT_IS_BGR(fmt
)?IMGFMT_BGR_DEPTH(fmt
):IMGFMT_RGB_DEPTH(fmt
);
264 *gl_type
= GL_UNSIGNED_SHORT
;
268 *gl_type
= GL_UNSIGNED_BYTE
;
272 *gl_format
= GL_RGBA
;
273 *gl_type
= GL_UNSIGNED_BYTE
;
276 supported
= 0; // no native YUV support
279 *gl_format
= GL_LUMINANCE
;
280 *gl_type
= GL_UNSIGNED_SHORT
;
283 supported
= 0; // no native YV12 support
288 *gl_format
= GL_LUMINANCE
;
289 *gl_type
= GL_UNSIGNED_BYTE
;
293 *gl_texfmt
= GL_YCBCR_MESA
;
295 *gl_format
= GL_YCBCR_MESA
;
296 *gl_type
= fmt
== IMGFMT_UYVY
? GL_UNSIGNED_SHORT_8_8
: GL_UNSIGNED_SHORT_8_8_REV
;
299 // we do not support palettized formats, although the format the
300 // swscale produces works
303 gl_type
= GL_UNSIGNED_BYTE_2_3_3_REV
;
307 *gl_format
= GL_RGBA
;
308 *gl_type
= GL_UNSIGNED_SHORT_1_5_5_5_REV
;
312 *gl_type
= GL_UNSIGNED_SHORT_5_6_5_REV
;
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
321 gl_type
= GL_UNSIGNED_BYTE_3_3_2
;
325 *gl_format
= GL_BGRA
;
326 *gl_type
= GL_UNSIGNED_SHORT_1_5_5_5_REV
;
330 *gl_type
= GL_UNSIGNED_SHORT_5_6_5
;
334 *gl_type
= GL_UNSIGNED_BYTE
;
338 *gl_format
= GL_BGRA
;
339 *gl_type
= GL_UNSIGNED_BYTE
;
343 *gl_format
= GL_RGBA
;
344 *gl_type
= GL_UNSIGNED_BYTE
;
347 #ifdef TEXTUREFORMAT_ALWAYS
348 *gl_texfmt
= TEXTUREFORMAT_ALWAYS
;
357 * \brief find address of a linked function
358 * \param s name of function to find
359 * \return address of function or NULL if not found
361 static void *getdladdr(const char *s
) {
364 void *handle
= dlopen(NULL
, RTLD_LAZY
);
367 ret
= dlsym(handle
, s
);
376 const char *funcnames
[7];
380 #define DEF_FUNC_DESC(name) {&mpgl##name, NULL, {"gl"#name, NULL}, gl ##name}
381 static const extfunc_desc_t extfuncs
[] = {
382 // these aren't extension functions but we query them anyway to allow
383 // different "backends" with one binary
384 DEF_FUNC_DESC(Begin
),
386 DEF_FUNC_DESC(Viewport
),
387 DEF_FUNC_DESC(MatrixMode
),
388 DEF_FUNC_DESC(LoadIdentity
),
389 DEF_FUNC_DESC(Translated
),
390 DEF_FUNC_DESC(Scaled
),
391 DEF_FUNC_DESC(Ortho
),
392 DEF_FUNC_DESC(Frustum
),
393 DEF_FUNC_DESC(PushMatrix
),
394 DEF_FUNC_DESC(PopMatrix
),
395 DEF_FUNC_DESC(Clear
),
396 DEF_FUNC_DESC(GenLists
),
397 DEF_FUNC_DESC(DeleteLists
),
398 DEF_FUNC_DESC(NewList
),
399 DEF_FUNC_DESC(EndList
),
400 DEF_FUNC_DESC(CallList
),
401 DEF_FUNC_DESC(CallLists
),
402 DEF_FUNC_DESC(GenTextures
),
403 DEF_FUNC_DESC(DeleteTextures
),
404 DEF_FUNC_DESC(TexEnvf
),
405 DEF_FUNC_DESC(TexEnvi
),
406 DEF_FUNC_DESC(Color4ub
),
407 DEF_FUNC_DESC(Color3f
),
408 DEF_FUNC_DESC(Color4f
),
409 DEF_FUNC_DESC(ClearColor
),
410 DEF_FUNC_DESC(ClearDepth
),
411 DEF_FUNC_DESC(DepthFunc
),
412 DEF_FUNC_DESC(Enable
),
413 DEF_FUNC_DESC(Disable
),
414 DEF_FUNC_DESC(DrawBuffer
),
415 DEF_FUNC_DESC(DepthMask
),
416 DEF_FUNC_DESC(BlendFunc
),
417 DEF_FUNC_DESC(Flush
),
418 DEF_FUNC_DESC(Finish
),
419 DEF_FUNC_DESC(PixelStorei
),
420 DEF_FUNC_DESC(TexImage1D
),
421 DEF_FUNC_DESC(TexImage2D
),
422 DEF_FUNC_DESC(TexSubImage2D
),
423 DEF_FUNC_DESC(TexParameteri
),
424 DEF_FUNC_DESC(TexParameterf
),
425 DEF_FUNC_DESC(TexParameterfv
),
426 DEF_FUNC_DESC(TexCoord2f
),
427 DEF_FUNC_DESC(Vertex2f
),
428 DEF_FUNC_DESC(Vertex3f
),
429 DEF_FUNC_DESC(Normal3f
),
430 DEF_FUNC_DESC(Lightfv
),
431 DEF_FUNC_DESC(ColorMaterial
),
432 DEF_FUNC_DESC(ShadeModel
),
433 DEF_FUNC_DESC(GetIntegerv
),
435 // here start the real extensions
436 {&mpglGenBuffers
, NULL
, {"glGenBuffers", "glGenBuffersARB", NULL
}},
437 {&mpglDeleteBuffers
, NULL
, {"glDeleteBuffers", "glDeleteBuffersARB", NULL
}},
438 {&mpglBindBuffer
, NULL
, {"glBindBuffer", "glBindBufferARB", NULL
}},
439 {&mpglMapBuffer
, NULL
, {"glMapBuffer", "glMapBufferARB", NULL
}},
440 {&mpglUnmapBuffer
, NULL
, {"glUnmapBuffer", "glUnmapBufferARB", NULL
}},
441 {&mpglBufferData
, NULL
, {"glBufferData", "glBufferDataARB", NULL
}},
442 {&mpglCombinerParameterfv
, "NV_register_combiners", {"glCombinerParameterfv", "glCombinerParameterfvNV", NULL
}},
443 {&mpglCombinerParameteri
, "NV_register_combiners", {"glCombinerParameteri", "glCombinerParameteriNV", NULL
}},
444 {&mpglCombinerInput
, "NV_register_combiners", {"glCombinerInput", "glCombinerInputNV", NULL
}},
445 {&mpglCombinerOutput
, "NV_register_combiners", {"glCombinerOutput", "glCombinerOutputNV", NULL
}},
446 {&mpglBeginFragmentShader
, "ATI_fragment_shader", {"glBeginFragmentShaderATI", NULL
}},
447 {&mpglEndFragmentShader
, "ATI_fragment_shader", {"glEndFragmentShaderATI", NULL
}},
448 {&mpglSampleMap
, "ATI_fragment_shader", {"glSampleMapATI", NULL
}},
449 {&mpglColorFragmentOp2
, "ATI_fragment_shader", {"glColorFragmentOp2ATI", NULL
}},
450 {&mpglColorFragmentOp3
, "ATI_fragment_shader", {"glColorFragmentOp3ATI", NULL
}},
451 {&mpglSetFragmentShaderConstant
, "ATI_fragment_shader", {"glSetFragmentShaderConstantATI", NULL
}},
452 {&mpglActiveTexture
, NULL
, {"glActiveTexture", "glActiveTextureARB", NULL
}},
453 {&mpglBindTexture
, NULL
, {"glBindTexture", "glBindTextureARB", "glBindTextureEXT", NULL
}},
454 {&mpglMultiTexCoord2f
, NULL
, {"glMultiTexCoord2f", "glMultiTexCoord2fARB", NULL
}},
455 {&mpglGenPrograms
, "_program", {"glGenProgramsARB", NULL
}},
456 {&mpglDeletePrograms
, "_program", {"glDeleteProgramsARB", NULL
}},
457 {&mpglBindProgram
, "_program", {"glBindProgramARB", NULL
}},
458 {&mpglProgramString
, "_program", {"glProgramStringARB", NULL
}},
459 {&mpglGetProgramiv
, "_program", {"glGetProgramivARB", NULL
}},
460 {&mpglProgramEnvParameter4f
, "_program", {"glProgramEnvParameter4fARB", NULL
}},
461 {&mpglSwapInterval
, "_swap_control", {"glXSwapIntervalSGI", "glXSwapInterval", "wglSwapIntervalSGI", "wglSwapInterval", "wglSwapIntervalEXT", NULL
}},
462 {&mpglTexImage3D
, NULL
, {"glTexImage3D", NULL
}},
463 {&mpglAllocateMemoryMESA
, "GLX_MESA_allocate_memory", {"glXAllocateMemoryMESA", NULL
}},
464 {&mpglFreeMemoryMESA
, "GLX_MESA_allocate_memory", {"glXFreeMemoryMESA", NULL
}},
469 * \brief find the function pointers of some useful OpenGL extensions
470 * \param getProcAddress function to resolve function names, may be NULL
471 * \param ext2 an extra extension string
473 static void getFunctions(void *(*getProcAddress
)(const GLubyte
*),
475 const extfunc_desc_t
*dsc
;
476 const char *extensions
;
480 getProcAddress
= (void *)getdladdr
;
482 // special case, we need glGetString before starting to find the other functions
483 mpglGetString
= getProcAddress("glGetString");
485 mpglGetString
= glGetString
;
487 extensions
= (const char *)mpglGetString(GL_EXTENSIONS
);
488 if (!extensions
) extensions
= "";
489 if (!ext2
) ext2
= "";
490 allexts
= malloc(strlen(extensions
) + strlen(ext2
) + 2);
491 strcpy(allexts
, extensions
);
492 strcat(allexts
, " ");
493 strcat(allexts
, ext2
);
494 mp_msg(MSGT_VO
, MSGL_DBG2
, "OpenGL extensions string:\n%s\n", allexts
);
495 for (dsc
= extfuncs
; dsc
->funcptr
; dsc
++) {
498 if (!dsc
->extstr
|| strstr(allexts
, dsc
->extstr
)) {
499 for (i
= 0; !ptr
&& dsc
->funcnames
[i
]; i
++)
500 ptr
= getProcAddress((const GLubyte
*)dsc
->funcnames
[i
]);
504 *(void **)dsc
->funcptr
= ptr
;
506 if (strstr(allexts
, "_texture_float"))
507 hqtexfmt
= GL_RGB32F
;
508 else if (strstr(allexts
, "NV_float_buffer"))
509 hqtexfmt
= GL_FLOAT_RGB32_NV
;
516 * \brief create a texture and set some defaults
517 * \param target texture taget, usually GL_TEXTURE_2D
518 * \param fmt internal texture format
519 * \param format texture host data format
520 * \param type texture host data type
521 * \param filter filter used for scaling, e.g. GL_LINEAR
522 * \param w texture width
523 * \param h texture height
524 * \param val luminance value to fill texture with
527 void glCreateClearTex(GLenum target
, GLenum fmt
, GLenum format
, GLenum type
, GLint filter
,
528 int w
, int h
, unsigned char val
) {
529 GLfloat fval
= (GLfloat
)val
/ 255.0;
530 GLfloat border
[4] = {fval
, fval
, fval
, fval
};
535 stride
= w
* glFmt2bpp(format
, type
);
537 init
= malloc(stride
* h
);
538 memset(init
, val
, stride
* h
);
539 glAdjustAlignment(stride
);
540 mpglPixelStorei(GL_UNPACK_ROW_LENGTH
, w
);
541 mpglTexImage2D(target
, 0, fmt
, w
, h
, 0, format
, type
, init
);
542 mpglTexParameterf(target
, GL_TEXTURE_PRIORITY
, 1.0);
543 mpglTexParameteri(target
, GL_TEXTURE_MIN_FILTER
, filter
);
544 mpglTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, filter
);
545 mpglTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
546 mpglTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
547 // Border texels should not be used with CLAMP_TO_EDGE
548 // We set a sane default anyway.
549 mpglTexParameterfv(target
, GL_TEXTURE_BORDER_COLOR
, border
);
554 * \brief skips whitespace and comments
555 * \param f file to read from
557 static void ppm_skip(FILE *f
) {
565 } while (c
!= EOF
&& (isspace(c
) || comment
));
570 #define MAXDIM (16 * 1024)
573 * \brief creates a texture from a PPM file
574 * \param target texture taget, usually GL_TEXTURE_2D
575 * \param fmt internal texture format, 0 for default
576 * \param filter filter used for scaling, e.g. GL_LINEAR
577 * \param f file to read PPM from
578 * \param width [out] width of texture
579 * \param height [out] height of texture
580 * \param maxval [out] maxval value from PPM file
581 * \return 0 on error, 1 otherwise
584 int glCreatePPMTex(GLenum target
, GLenum fmt
, GLint filter
,
585 FILE *f
, int *width
, int *height
, int *maxval
) {
586 unsigned w
, h
, m
, val
, bpp
;
590 if (fgetc(f
) != 'P' || fgetc(f
) != '6')
593 if (fscanf(f
, "%u", &w
) != 1)
596 if (fscanf(f
, "%u", &h
) != 1)
599 if (fscanf(f
, "%u", &m
) != 1)
604 if (w
> MAXDIM
|| h
> MAXDIM
)
606 bpp
= (m
> 255) ? 6 : 3;
607 data
= malloc(w
* h
* bpp
);
608 if (fread(data
, w
* bpp
, h
, f
) != h
)
611 fmt
= (m
> 255) ? hqtexfmt
: 3;
612 if (fmt
== GL_FLOAT_RGB32_NV
&& target
!= GL_TEXTURE_RECTANGLE
)
615 type
= m
> 255 ? GL_UNSIGNED_SHORT
: GL_UNSIGNED_BYTE
;
616 glCreateClearTex(target
, fmt
, GL_RGB
, type
, filter
, w
, h
, 0);
617 glUploadTex(target
, GL_RGB
, type
,
618 data
, w
* bpp
, 0, 0, w
, h
, 0);
620 if (width
) *width
= w
;
621 if (height
) *height
= h
;
622 if (maxval
) *maxval
= m
;
627 * \brief return the number of bytes per pixel for the given format
628 * \param format OpenGL format
629 * \param type OpenGL type
630 * \return bytes per pixel
633 * Does not handle all possible variants, just those used by MPlayer
635 int glFmt2bpp(GLenum format
, GLenum type
) {
636 int component_size
= 0;
638 case GL_UNSIGNED_BYTE_3_3_2
:
639 case GL_UNSIGNED_BYTE_2_3_3_REV
:
641 case GL_UNSIGNED_SHORT_5_5_5_1
:
642 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
643 case GL_UNSIGNED_SHORT_5_6_5
:
644 case GL_UNSIGNED_SHORT_5_6_5_REV
:
646 case GL_UNSIGNED_BYTE
:
649 case GL_UNSIGNED_SHORT
:
656 return component_size
;
661 return 3 * component_size
;
664 return 4 * component_size
;
670 * \brief upload a texture, handling things like stride and slices
671 * \param target texture target, usually GL_TEXTURE_2D
672 * \param format OpenGL format of data
673 * \param type OpenGL type of data
674 * \param dataptr data to upload
675 * \param stride data stride
676 * \param x x offset in texture
677 * \param y y offset in texture
678 * \param w width of the texture part to upload
679 * \param h height of the texture part to upload
680 * \param slice height of an upload slice, 0 for all at once
683 void glUploadTex(GLenum target
, GLenum format
, GLenum type
,
684 const void *dataptr
, int stride
,
685 int x
, int y
, int w
, int h
, int slice
) {
686 const uint8_t *data
= dataptr
;
688 if (w
<= 0 || h
<= 0) return;
692 data
+= (h
- 1) * stride
;
695 // this is not always correct, but should work for MPlayer
696 glAdjustAlignment(stride
);
697 mpglPixelStorei(GL_UNPACK_ROW_LENGTH
, stride
/ glFmt2bpp(format
, type
));
698 for (; y
+ slice
<= y_max
; y
+= slice
) {
699 mpglTexSubImage2D(target
, 0, x
, y
, w
, slice
, format
, type
, data
);
700 data
+= stride
* slice
;
703 mpglTexSubImage2D(target
, 0, x
, y
, w
, y_max
- y
, format
, type
, data
);
706 static void fillUVcoeff(GLfloat
*ucoef
, GLfloat
*vcoef
,
707 float uvcos
, float uvsin
) {
709 ucoef
[0] = 0 * uvcos
+ 1.403 * uvsin
;
710 vcoef
[0] = 0 * uvsin
+ 1.403 * uvcos
;
711 ucoef
[1] = -0.344 * uvcos
+ -0.714 * uvsin
;
712 vcoef
[1] = -0.344 * uvsin
+ -0.714 * uvcos
;
713 ucoef
[2] = 1.770 * uvcos
+ 0 * uvsin
;
714 vcoef
[2] = 1.770 * uvsin
+ 0 * uvcos
;
717 // Coefficients (probably) must be in [0, 1] range, whereas they originally
718 // are in [-2, 2] range, so here comes the trick:
719 // First put them in the [-0.5, 0.5] range, then add 0.5.
720 // This can be undone with the HALF_BIAS and SCALE_BY_FOUR arguments
721 // for CombinerInput and CombinerOutput (or the respective ATI variants)
722 for (i
= 0; i
< 4; i
++) {
723 ucoef
[i
] = ucoef
[i
] * 0.25 + 0.5;
724 vcoef
[i
] = vcoef
[i
] * 0.25 + 0.5;
729 * \brief Setup register combiners for YUV to RGB conversion.
730 * \param uvcos used for saturation and hue adjustment
731 * \param uvsin used for saturation and hue adjustment
733 static void glSetupYUVCombiners(float uvcos
, float uvsin
) {
737 if (!mpglCombinerInput
|| !mpglCombinerOutput
||
738 !mpglCombinerParameterfv
|| !mpglCombinerParameteri
) {
739 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Combiner functions missing!\n");
742 mpglGetIntegerv(GL_MAX_GENERAL_COMBINERS_NV
, &i
);
744 mp_msg(MSGT_VO
, MSGL_ERR
,
745 "[gl] 2 general combiners needed for YUV combiner support (found %i)\n", i
);
746 mpglGetIntegerv(GL_MAX_TEXTURE_UNITS
, &i
);
748 mp_msg(MSGT_VO
, MSGL_ERR
,
749 "[gl] 3 texture units needed for YUV combiner support (found %i)\n", i
);
750 fillUVcoeff(ucoef
, vcoef
, uvcos
, uvsin
);
751 mpglCombinerParameterfv(GL_CONSTANT_COLOR0_NV
, ucoef
);
752 mpglCombinerParameterfv(GL_CONSTANT_COLOR1_NV
, vcoef
);
754 // UV first, like this green component cannot overflow
755 mpglCombinerInput(GL_COMBINER0_NV
, GL_RGB
, GL_VARIABLE_A_NV
,
756 GL_TEXTURE1
, GL_HALF_BIAS_NORMAL_NV
, GL_RGB
);
757 mpglCombinerInput(GL_COMBINER0_NV
, GL_RGB
, GL_VARIABLE_B_NV
,
758 GL_CONSTANT_COLOR0_NV
, GL_HALF_BIAS_NORMAL_NV
, GL_RGB
);
759 mpglCombinerInput(GL_COMBINER0_NV
, GL_RGB
, GL_VARIABLE_C_NV
,
760 GL_TEXTURE2
, GL_HALF_BIAS_NORMAL_NV
, GL_RGB
);
761 mpglCombinerInput(GL_COMBINER0_NV
, GL_RGB
, GL_VARIABLE_D_NV
,
762 GL_CONSTANT_COLOR1_NV
, GL_HALF_BIAS_NORMAL_NV
, GL_RGB
);
763 mpglCombinerOutput(GL_COMBINER0_NV
, GL_RGB
, GL_DISCARD_NV
, GL_DISCARD_NV
,
764 GL_SPARE0_NV
, GL_SCALE_BY_FOUR_NV
, GL_NONE
, GL_FALSE
,
768 mpglCombinerInput(GL_COMBINER1_NV
, GL_RGB
, GL_VARIABLE_A_NV
, GL_SPARE0_NV
,
769 GL_SIGNED_IDENTITY_NV
, GL_RGB
);
770 mpglCombinerInput(GL_COMBINER1_NV
, GL_RGB
, GL_VARIABLE_B_NV
, GL_ZERO
,
771 GL_UNSIGNED_INVERT_NV
, GL_RGB
);
772 mpglCombinerInput(GL_COMBINER1_NV
, GL_RGB
, GL_VARIABLE_C_NV
,
773 GL_TEXTURE0
, GL_SIGNED_IDENTITY_NV
, GL_RGB
);
774 mpglCombinerInput(GL_COMBINER1_NV
, GL_RGB
, GL_VARIABLE_D_NV
, GL_ZERO
,
775 GL_UNSIGNED_INVERT_NV
, GL_RGB
);
776 mpglCombinerOutput(GL_COMBINER1_NV
, GL_RGB
, GL_DISCARD_NV
, GL_DISCARD_NV
,
777 GL_SPARE0_NV
, GL_NONE
, GL_NONE
, GL_FALSE
,
780 // leave final combiner stage in default mode
781 mpglCombinerParameteri(GL_NUM_GENERAL_COMBINERS_NV
, 2);
785 * \brief Setup ATI version of register combiners for YUV to RGB conversion.
786 * \param csp_params parameters used for colorspace conversion
787 * \param text if set use the GL_ATI_text_fragment_shader API as
790 static void glSetupYUVFragmentATI(struct mp_csp_params
*csp_params
,
795 mpglGetIntegerv (GL_MAX_TEXTURE_UNITS
, &i
);
797 mp_msg(MSGT_VO
, MSGL_ERR
,
798 "[gl] 3 texture units needed for YUV combiner (ATI) support (found %i)\n", i
);
800 mp_get_yuv2rgb_coeffs(csp_params
, yuv2rgb
);
801 for (i
= 0; i
< 3; i
++) {
803 yuv2rgb
[i
][3] -= -0.5 * (yuv2rgb
[i
][1] + yuv2rgb
[i
][2]);
804 for (j
= 0; j
< 4; j
++) {
805 yuv2rgb
[i
][j
] *= 0.125;
806 yuv2rgb
[i
][j
] += 0.5;
807 if (yuv2rgb
[i
][j
] > 1)
809 if (yuv2rgb
[i
][j
] < 0)
814 GLfloat c0
[4] = {yuv2rgb
[0][0], yuv2rgb
[1][0], yuv2rgb
[2][0]};
815 GLfloat c1
[4] = {yuv2rgb
[0][1], yuv2rgb
[1][1], yuv2rgb
[2][1]};
816 GLfloat c2
[4] = {yuv2rgb
[0][2], yuv2rgb
[1][2], yuv2rgb
[2][2]};
817 GLfloat c3
[4] = {yuv2rgb
[0][3], yuv2rgb
[1][3], yuv2rgb
[2][3]};
818 if (!mpglBeginFragmentShader
|| !mpglEndFragmentShader
||
819 !mpglSetFragmentShaderConstant
|| !mpglSampleMap
||
820 !mpglColorFragmentOp2
|| !mpglColorFragmentOp3
) {
821 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Combiner (ATI) functions missing!\n");
824 mpglGetIntegerv(GL_NUM_FRAGMENT_REGISTERS_ATI
, &i
);
826 mp_msg(MSGT_VO
, MSGL_ERR
,
827 "[gl] 3 registers needed for YUV combiner (ATI) support (found %i)\n", i
);
828 mpglBeginFragmentShader();
829 mpglSetFragmentShaderConstant(GL_CON_0_ATI
, c0
);
830 mpglSetFragmentShaderConstant(GL_CON_1_ATI
, c1
);
831 mpglSetFragmentShaderConstant(GL_CON_2_ATI
, c2
);
832 mpglSetFragmentShaderConstant(GL_CON_3_ATI
, c3
);
833 mpglSampleMap(GL_REG_0_ATI
, GL_TEXTURE0
, GL_SWIZZLE_STR_ATI
);
834 mpglSampleMap(GL_REG_1_ATI
, GL_TEXTURE1
, GL_SWIZZLE_STR_ATI
);
835 mpglSampleMap(GL_REG_2_ATI
, GL_TEXTURE2
, GL_SWIZZLE_STR_ATI
);
836 mpglColorFragmentOp2(GL_MUL_ATI
, GL_REG_1_ATI
, GL_NONE
, GL_NONE
,
837 GL_REG_1_ATI
, GL_NONE
, GL_BIAS_BIT_ATI
,
838 GL_CON_1_ATI
, GL_NONE
, GL_BIAS_BIT_ATI
);
839 mpglColorFragmentOp3(GL_MAD_ATI
, GL_REG_2_ATI
, GL_NONE
, GL_NONE
,
840 GL_REG_2_ATI
, GL_NONE
, GL_BIAS_BIT_ATI
,
841 GL_CON_2_ATI
, GL_NONE
, GL_BIAS_BIT_ATI
,
842 GL_REG_1_ATI
, GL_NONE
, GL_NONE
);
843 mpglColorFragmentOp3(GL_MAD_ATI
, GL_REG_0_ATI
, GL_NONE
, GL_NONE
,
844 GL_REG_0_ATI
, GL_NONE
, GL_NONE
,
845 GL_CON_0_ATI
, GL_NONE
, GL_BIAS_BIT_ATI
,
846 GL_REG_2_ATI
, GL_NONE
, GL_NONE
);
847 mpglColorFragmentOp2(GL_ADD_ATI
, GL_REG_0_ATI
, GL_NONE
, GL_8X_BIT_ATI
,
848 GL_REG_0_ATI
, GL_NONE
, GL_NONE
,
849 GL_CON_3_ATI
, GL_NONE
, GL_BIAS_BIT_ATI
);
850 mpglEndFragmentShader();
852 static const char template[] =
855 " CONSTANT c0 = {%e, %e, %e};\n"
856 " CONSTANT c1 = {%e, %e, %e};\n"
857 " CONSTANT c2 = {%e, %e, %e};\n"
858 " CONSTANT c3 = {%e, %e, %e};\n"
861 " SampleMap r0, t0.str;\n"
862 " SampleMap r1, t1.str;\n"
863 " SampleMap r2, t2.str;\n"
864 " MUL r1.rgb, r1.bias, c1.bias;\n"
865 " MAD r2.rgb, r2.bias, c2.bias, r1;\n"
866 " MAD r0.rgb, r0, c0.bias, r2;\n"
867 " ADD r0.rgb.8x, r0, c3.bias;\n"
870 snprintf(buffer
, sizeof(buffer
), template,
871 yuv2rgb
[0][0], yuv2rgb
[1][0], yuv2rgb
[2][0],
872 yuv2rgb
[0][1], yuv2rgb
[1][1], yuv2rgb
[2][1],
873 yuv2rgb
[0][2], yuv2rgb
[1][2], yuv2rgb
[2][2],
874 yuv2rgb
[0][3], yuv2rgb
[1][3], yuv2rgb
[2][3]);
875 mp_msg(MSGT_VO
, MSGL_DBG2
, "[gl] generated fragment program:\n%s\n", buffer
);
876 loadGPUProgram(GL_TEXT_FRAGMENT_SHADER_ATI
, buffer
);
881 * \brief helper function for gen_spline_lookup_tex
882 * \param x subpixel-position ((0,1) range) to calculate weights for
883 * \param dst where to store transformed weights, must provide space for 4 GLfloats
885 * calculates the weights and stores them after appropriate transformation
886 * for the scaler fragment program.
888 static void store_weights(float x
, GLfloat
*dst
) {
889 float w0
= (((-1 * x
+ 3) * x
- 3) * x
+ 1) / 6;
890 float w1
= ((( 3 * x
- 6) * x
+ 0) * x
+ 4) / 6;
891 float w2
= (((-3 * x
+ 3) * x
+ 3) * x
+ 1) / 6;
892 float w3
= ((( 1 * x
+ 0) * x
+ 0) * x
+ 0) / 6;
893 *dst
++ = 1 + x
- w1
/ (w0
+ w1
);
894 *dst
++ = 1 - x
+ w3
/ (w2
+ w3
);
899 //! to avoid artefacts this should be rather large
900 #define LOOKUP_BSPLINE_RES (2 * 1024)
902 * \brief creates the 1D lookup texture needed for fast higher-order filtering
903 * \param unit texture unit to attach texture to
905 static void gen_spline_lookup_tex(GLenum unit
) {
906 GLfloat
*tex
= calloc(4 * LOOKUP_BSPLINE_RES
, sizeof(*tex
));
909 for (i
= 0; i
< LOOKUP_BSPLINE_RES
; i
++) {
910 float x
= (float)(i
+ 0.5) / LOOKUP_BSPLINE_RES
;
911 store_weights(x
, tp
);
914 store_weights(0, tex
);
915 store_weights(1, &tex
[4 * (LOOKUP_BSPLINE_RES
- 1)]);
916 mpglActiveTexture(unit
);
917 mpglTexImage1D(GL_TEXTURE_1D
, 0, GL_RGBA16
, LOOKUP_BSPLINE_RES
, 0, GL_RGBA
, GL_FLOAT
, tex
);
918 mpglTexParameterf(GL_TEXTURE_1D
, GL_TEXTURE_PRIORITY
, 1.0);
919 mpglTexParameteri(GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
920 mpglTexParameteri(GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
921 mpglTexParameteri(GL_TEXTURE_1D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
922 mpglActiveTexture(GL_TEXTURE0
);
926 static const char *bilin_filt_template
=
927 "TEX yuv.%c, fragment.texcoord[%c], texture[%c], %s;\n";
929 #define BICUB_FILT_MAIN(textype) \
930 /* first y-interpolation */ \
931 "ADD coord, fragment.texcoord[%c].xyxy, cdelta.xyxw;\n" \
932 "ADD coord2, fragment.texcoord[%c].xyxy, cdelta.zyzw;\n" \
933 "TEX a.r, coord.xyxy, texture[%c], "textype";\n" \
934 "TEX a.g, coord.zwzw, texture[%c], "textype";\n" \
935 /* second y-interpolation */ \
936 "TEX b.r, coord2.xyxy, texture[%c], "textype";\n" \
937 "TEX b.g, coord2.zwzw, texture[%c], "textype";\n" \
938 "LRP a.b, parmy.b, a.rrrr, a.gggg;\n" \
939 "LRP a.a, parmy.b, b.rrrr, b.gggg;\n" \
940 /* x-interpolation */ \
941 "LRP yuv.%c, parmx.b, a.bbbb, a.aaaa;\n"
943 static const char *bicub_filt_template_2D
=
944 "MAD coord.xy, fragment.texcoord[%c], {%e, %e}, {0.5, 0.5};\n"
945 "TEX parmx, coord.x, texture[%c], 1D;\n"
946 "MUL cdelta.xz, parmx.rrgg, {-%e, 0, %e, 0};\n"
947 "TEX parmy, coord.y, texture[%c], 1D;\n"
948 "MUL cdelta.yw, parmy.rrgg, {0, -%e, 0, %e};\n"
949 BICUB_FILT_MAIN("2D");
951 static const char *bicub_filt_template_RECT
=
952 "ADD coord, fragment.texcoord[%c], {0.5, 0.5};\n"
953 "TEX parmx, coord.x, texture[%c], 1D;\n"
954 "MUL cdelta.xz, parmx.rrgg, {-1, 0, 1, 0};\n"
955 "TEX parmy, coord.y, texture[%c], 1D;\n"
956 "MUL cdelta.yw, parmy.rrgg, {0, -1, 0, 1};\n"
957 BICUB_FILT_MAIN("RECT");
959 #define CALCWEIGHTS(t, s) \
960 "MAD "t", {-0.5, 0.1666, 0.3333, -0.3333}, "s", {1, 0, -0.5, 0.5};\n" \
961 "MAD "t", "t", "s", {0, 0, -0.5, 0.5};\n" \
962 "MAD "t", "t", "s", {-0.6666, 0, 0.8333, 0.1666};\n" \
963 "RCP a.x, "t".z;\n" \
964 "RCP a.y, "t".w;\n" \
965 "MAD "t".xy, "t".xyxy, a.xyxy, {1, 1, 0, 0};\n" \
966 "ADD "t".x, "t".xxxx, "s";\n" \
967 "SUB "t".y, "t".yyyy, "s";\n"
969 static const char *bicub_notex_filt_template_2D
=
970 "MAD coord.xy, fragment.texcoord[%c], {%e, %e}, {0.5, 0.5};\n"
971 "FRC coord.xy, coord.xyxy;\n"
972 CALCWEIGHTS("parmx", "coord.xxxx")
973 "MUL cdelta.xz, parmx.rrgg, {-%e, 0, %e, 0};\n"
974 CALCWEIGHTS("parmy", "coord.yyyy")
975 "MUL cdelta.yw, parmy.rrgg, {0, -%e, 0, %e};\n"
976 BICUB_FILT_MAIN("2D");
978 static const char *bicub_notex_filt_template_RECT
=
979 "ADD coord, fragment.texcoord[%c], {0.5, 0.5};\n"
980 "FRC coord.xy, coord.xyxy;\n"
981 CALCWEIGHTS("parmx", "coord.xxxx")
982 "MUL cdelta.xz, parmx.rrgg, {-1, 0, 1, 0};\n"
983 CALCWEIGHTS("parmy", "coord.yyyy")
984 "MUL cdelta.yw, parmy.rrgg, {0, -1, 0, 1};\n"
985 BICUB_FILT_MAIN("RECT");
987 #define BICUB_X_FILT_MAIN(textype) \
988 "ADD coord.xy, fragment.texcoord[%c].xyxy, cdelta.xyxy;\n" \
989 "ADD coord2.xy, fragment.texcoord[%c].xyxy, cdelta.zyzy;\n" \
990 "TEX a.r, coord, texture[%c], "textype";\n" \
991 "TEX b.r, coord2, texture[%c], "textype";\n" \
992 /* x-interpolation */ \
993 "LRP yuv.%c, parmx.b, a.rrrr, b.rrrr;\n"
995 static const char *bicub_x_filt_template_2D
=
996 "MAD coord.x, fragment.texcoord[%c], {%e}, {0.5};\n"
997 "TEX parmx, coord, texture[%c], 1D;\n"
998 "MUL cdelta.xyz, parmx.rrgg, {-%e, 0, %e};\n"
999 BICUB_X_FILT_MAIN("2D");
1001 static const char *bicub_x_filt_template_RECT
=
1002 "ADD coord.x, fragment.texcoord[%c], {0.5};\n"
1003 "TEX parmx, coord, texture[%c], 1D;\n"
1004 "MUL cdelta.xyz, parmx.rrgg, {-1, 0, 1};\n"
1005 BICUB_X_FILT_MAIN("RECT");
1007 static const char *unsharp_filt_template
=
1008 "PARAM dcoord%c = {%e, %e, %e, %e};\n"
1009 "ADD coord, fragment.texcoord[%c].xyxy, dcoord%c;\n"
1010 "SUB coord2, fragment.texcoord[%c].xyxy, dcoord%c;\n"
1011 "TEX a.r, fragment.texcoord[%c], texture[%c], %s;\n"
1012 "TEX b.r, coord.xyxy, texture[%c], %s;\n"
1013 "TEX b.g, coord.zwzw, texture[%c], %s;\n"
1014 "ADD b.r, b.r, b.g;\n"
1015 "TEX b.b, coord2.xyxy, texture[%c], %s;\n"
1016 "TEX b.g, coord2.zwzw, texture[%c], %s;\n"
1017 "DP3 b, b, {0.25, 0.25, 0.25};\n"
1018 "SUB b.r, a.r, b.r;\n"
1019 "MAD yuv.%c, b.r, {%e}, a.r;\n";
1021 static const char *unsharp_filt_template2
=
1022 "PARAM dcoord%c = {%e, %e, %e, %e};\n"
1023 "PARAM dcoord2%c = {%e, 0, 0, %e};\n"
1024 "ADD coord, fragment.texcoord[%c].xyxy, dcoord%c;\n"
1025 "SUB coord2, fragment.texcoord[%c].xyxy, dcoord%c;\n"
1026 "TEX a.r, fragment.texcoord[%c], texture[%c], %s;\n"
1027 "TEX b.r, coord.xyxy, texture[%c], %s;\n"
1028 "TEX b.g, coord.zwzw, texture[%c], %s;\n"
1029 "ADD b.r, b.r, b.g;\n"
1030 "TEX b.b, coord2.xyxy, texture[%c], %s;\n"
1031 "TEX b.g, coord2.zwzw, texture[%c], %s;\n"
1032 "ADD b.r, b.r, b.b;\n"
1033 "ADD b.a, b.r, b.g;\n"
1034 "ADD coord, fragment.texcoord[%c].xyxy, dcoord2%c;\n"
1035 "SUB coord2, fragment.texcoord[%c].xyxy, dcoord2%c;\n"
1036 "TEX b.r, coord.xyxy, texture[%c], %s;\n"
1037 "TEX b.g, coord.zwzw, texture[%c], %s;\n"
1038 "ADD b.r, b.r, b.g;\n"
1039 "TEX b.b, coord2.xyxy, texture[%c], %s;\n"
1040 "TEX b.g, coord2.zwzw, texture[%c], %s;\n"
1041 "DP4 b.r, b, {-0.1171875, -0.1171875, -0.1171875, -0.09765625};\n"
1042 "MAD b.r, a.r, {0.859375}, b.r;\n"
1043 "MAD yuv.%c, b.r, {%e}, a.r;\n";
1045 static const char *yuv_prog_template
=
1046 "PARAM ycoef = {%e, %e, %e};\n"
1047 "PARAM ucoef = {%e, %e, %e};\n"
1048 "PARAM vcoef = {%e, %e, %e};\n"
1049 "PARAM offsets = {%e, %e, %e};\n"
1051 "MAD res.rgb, yuv.rrrr, ycoef, offsets;\n"
1052 "MAD res.rgb, yuv.gggg, ucoef, res;\n"
1053 "MAD result.color.rgb, yuv.bbbb, vcoef, res;\n"
1056 static const char *yuv_pow_prog_template
=
1057 "PARAM ycoef = {%e, %e, %e};\n"
1058 "PARAM ucoef = {%e, %e, %e};\n"
1059 "PARAM vcoef = {%e, %e, %e};\n"
1060 "PARAM offsets = {%e, %e, %e};\n"
1061 "PARAM gamma = {%e, %e, %e};\n"
1063 "MAD res.rgb, yuv.rrrr, ycoef, offsets;\n"
1064 "MAD res.rgb, yuv.gggg, ucoef, res;\n"
1065 "MAD_SAT res.rgb, yuv.bbbb, vcoef, res;\n"
1066 "POW result.color.r, res.r, gamma.r;\n"
1067 "POW result.color.g, res.g, gamma.g;\n"
1068 "POW result.color.b, res.b, gamma.b;\n"
1071 static const char *yuv_lookup_prog_template
=
1072 "PARAM ycoef = {%e, %e, %e, 0};\n"
1073 "PARAM ucoef = {%e, %e, %e, 0};\n"
1074 "PARAM vcoef = {%e, %e, %e, 0};\n"
1075 "PARAM offsets = {%e, %e, %e, 0.125};\n"
1077 "MAD res, yuv.rrrr, ycoef, offsets;\n"
1078 "MAD res.rgb, yuv.gggg, ucoef, res;\n"
1079 "MAD res.rgb, yuv.bbbb, vcoef, res;\n"
1080 "TEX result.color.r, res.raaa, texture[%c], 2D;\n"
1081 "ADD res.a, res.a, 0.25;\n"
1082 "TEX result.color.g, res.gaaa, texture[%c], 2D;\n"
1083 "ADD res.a, res.a, 0.25;\n"
1084 "TEX result.color.b, res.baaa, texture[%c], 2D;\n"
1087 static const char *yuv_lookup3d_prog_template
=
1088 "TEX result.color, yuv, texture[%c], 3D;\n"
1092 * \brief creates and initializes helper textures needed for scaling texture read
1093 * \param scaler scaler type to create texture for
1094 * \param texu contains next free texture unit number
1095 * \param texs texture unit ids for the scaler are stored in this array
1097 static void create_scaler_textures(int scaler
, int *texu
, char *texs
) {
1099 case YUV_SCALER_BILIN
:
1100 case YUV_SCALER_BICUB_NOTEX
:
1101 case YUV_SCALER_UNSHARP
:
1102 case YUV_SCALER_UNSHARP2
:
1104 case YUV_SCALER_BICUB
:
1105 case YUV_SCALER_BICUB_X
:
1106 texs
[0] = (*texu
)++;
1107 gen_spline_lookup_tex(GL_TEXTURE0
+ texs
[0]);
1111 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] unknown scaler type %i\n", scaler
);
1115 //! resolution of texture for gamma lookup table
1116 #define LOOKUP_RES 512
1117 //! resolution for 3D yuv->rgb conversion lookup table
1118 #define LOOKUP_3DRES 32
1120 * \brief creates and initializes helper textures needed for yuv conversion
1121 * \param params struct containing parameters like brightness, gamma, ...
1122 * \param texu contains next free texture unit number
1123 * \param texs texture unit ids for the conversion are stored in this array
1125 static void create_conv_textures(gl_conversion_params_t
*params
, int *texu
, char *texs
) {
1126 unsigned char *lookup_data
= NULL
;
1127 int conv
= YUV_CONVERSION(params
->type
);
1129 case YUV_CONVERSION_FRAGMENT
:
1130 case YUV_CONVERSION_FRAGMENT_POW
:
1132 case YUV_CONVERSION_FRAGMENT_LOOKUP
:
1133 texs
[0] = (*texu
)++;
1134 mpglActiveTexture(GL_TEXTURE0
+ texs
[0]);
1135 lookup_data
= malloc(4 * LOOKUP_RES
);
1136 mp_gen_gamma_map(lookup_data
, LOOKUP_RES
, params
->csp_params
.rgamma
);
1137 mp_gen_gamma_map(&lookup_data
[LOOKUP_RES
], LOOKUP_RES
, params
->csp_params
.ggamma
);
1138 mp_gen_gamma_map(&lookup_data
[2 * LOOKUP_RES
], LOOKUP_RES
, params
->csp_params
.bgamma
);
1139 glCreateClearTex(GL_TEXTURE_2D
, GL_LUMINANCE8
, GL_LUMINANCE
, GL_UNSIGNED_BYTE
, GL_LINEAR
,
1141 glUploadTex(GL_TEXTURE_2D
, GL_LUMINANCE
, GL_UNSIGNED_BYTE
, lookup_data
,
1142 LOOKUP_RES
, 0, 0, LOOKUP_RES
, 4, 0);
1143 mpglActiveTexture(GL_TEXTURE0
);
1146 case YUV_CONVERSION_FRAGMENT_LOOKUP3D
:
1148 int sz
= LOOKUP_3DRES
+ 2; // texture size including borders
1149 if (!mpglTexImage3D
) {
1150 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] Missing 3D texture function!\n");
1153 texs
[0] = (*texu
)++;
1154 mpglActiveTexture(GL_TEXTURE0
+ texs
[0]);
1155 lookup_data
= malloc(3 * sz
* sz
* sz
);
1156 mp_gen_yuv2rgb_map(¶ms
->csp_params
, lookup_data
, LOOKUP_3DRES
);
1157 glAdjustAlignment(sz
);
1158 mpglPixelStorei(GL_UNPACK_ROW_LENGTH
, 0);
1159 mpglTexImage3D(GL_TEXTURE_3D
, 0, 3, sz
, sz
, sz
, 1,
1160 GL_RGB
, GL_UNSIGNED_BYTE
, lookup_data
);
1161 mpglTexParameterf(GL_TEXTURE_3D
, GL_TEXTURE_PRIORITY
, 1.0);
1162 mpglTexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
1163 mpglTexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
1164 mpglTexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_WRAP_S
, GL_CLAMP
);
1165 mpglTexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_WRAP_T
, GL_CLAMP
);
1166 mpglTexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_WRAP_R
, GL_CLAMP
);
1167 mpglActiveTexture(GL_TEXTURE0
);
1172 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] unknown conversion type %i\n", conv
);
1179 * \brief adds a scaling texture read at the current fragment program position
1180 * \param scaler type of scaler to insert
1181 * \param prog_pos current position in fragment program
1182 * \param remain how many bytes remain in the buffer given by prog_pos
1183 * \param texs array containing the texture unit identifiers for this scaler
1184 * \param in_tex texture unit the scaler should read from
1185 * \param out_comp component of the yuv variable the scaler stores the result in
1186 * \param rect if rectangular (pixel) adressing should be used for in_tex
1187 * \param texw width of the in_tex texture
1188 * \param texh height of the in_tex texture
1189 * \param strength strength of filter effect if the scaler does some kind of filtering
1191 static void add_scaler(int scaler
, char **prog_pos
, int *remain
, char *texs
,
1192 char in_tex
, char out_comp
, int rect
, int texw
, int texh
,
1194 const char *ttype
= rect
? "RECT" : "2D";
1195 const float ptw
= rect
? 1.0 : 1.0 / texw
;
1196 const float pth
= rect
? 1.0 : 1.0 / texh
;
1198 case YUV_SCALER_BILIN
:
1199 snprintf(*prog_pos
, *remain
, bilin_filt_template
, out_comp
, in_tex
,
1202 case YUV_SCALER_BICUB
:
1204 snprintf(*prog_pos
, *remain
, bicub_filt_template_RECT
,
1205 in_tex
, texs
[0], texs
[0],
1206 in_tex
, in_tex
, in_tex
, in_tex
, in_tex
, in_tex
, out_comp
);
1208 snprintf(*prog_pos
, *remain
, bicub_filt_template_2D
,
1209 in_tex
, (float)texw
, (float)texh
,
1210 texs
[0], ptw
, ptw
, texs
[0], pth
, pth
,
1211 in_tex
, in_tex
, in_tex
, in_tex
, in_tex
, in_tex
, out_comp
);
1213 case YUV_SCALER_BICUB_X
:
1215 snprintf(*prog_pos
, *remain
, bicub_x_filt_template_RECT
,
1217 in_tex
, in_tex
, in_tex
, in_tex
, out_comp
);
1219 snprintf(*prog_pos
, *remain
, bicub_x_filt_template_2D
,
1220 in_tex
, (float)texw
,
1222 in_tex
, in_tex
, in_tex
, in_tex
, out_comp
);
1224 case YUV_SCALER_BICUB_NOTEX
:
1226 snprintf(*prog_pos
, *remain
, bicub_notex_filt_template_RECT
,
1228 in_tex
, in_tex
, in_tex
, in_tex
, in_tex
, in_tex
, out_comp
);
1230 snprintf(*prog_pos
, *remain
, bicub_notex_filt_template_2D
,
1231 in_tex
, (float)texw
, (float)texh
, ptw
, ptw
, pth
, pth
,
1232 in_tex
, in_tex
, in_tex
, in_tex
, in_tex
, in_tex
, out_comp
);
1234 case YUV_SCALER_UNSHARP
:
1235 snprintf(*prog_pos
, *remain
, unsharp_filt_template
,
1236 out_comp
, 0.5 * ptw
, 0.5 * pth
, 0.5 * ptw
, -0.5 * pth
,
1237 in_tex
, out_comp
, in_tex
, out_comp
, in_tex
,
1238 in_tex
, ttype
, in_tex
, ttype
, in_tex
, ttype
, in_tex
, ttype
,
1239 in_tex
, ttype
, out_comp
, strength
);
1241 case YUV_SCALER_UNSHARP2
:
1242 snprintf(*prog_pos
, *remain
, unsharp_filt_template2
,
1243 out_comp
, 1.2 * ptw
, 1.2 * pth
, 1.2 * ptw
, -1.2 * pth
,
1244 out_comp
, 1.5 * ptw
, 1.5 * pth
,
1245 in_tex
, out_comp
, in_tex
, out_comp
, in_tex
,
1246 in_tex
, ttype
, in_tex
, ttype
, in_tex
, ttype
, in_tex
, ttype
,
1247 in_tex
, ttype
, in_tex
, out_comp
, in_tex
, out_comp
,
1248 in_tex
, ttype
, in_tex
, ttype
, in_tex
, ttype
,
1249 in_tex
, ttype
, out_comp
, strength
);
1252 *remain
-= strlen(*prog_pos
);
1253 *prog_pos
+= strlen(*prog_pos
);
1256 static const struct {
1261 {"instructions", 0x88A0, 0x88A1},
1262 {"native instructions", 0x88A2, 0x88A3},
1263 {"temporaries", 0x88A4, 0x88A5},
1264 {"native temporaries", 0x88A6, 0x88A7},
1265 {"parameters", 0x88A8, 0x88A9},
1266 {"native parameters", 0x88AA, 0x88AB},
1267 {"attribs", 0x88AC, 0x88AD},
1268 {"native attribs", 0x88AE, 0x88AF},
1269 {"ALU instructions", 0x8805, 0x880B},
1270 {"TEX instructions", 0x8806, 0x880C},
1271 {"TEX indirections", 0x8807, 0x880D},
1272 {"native ALU instructions", 0x8808, 0x880E},
1273 {"native TEX instructions", 0x8809, 0x880F},
1274 {"native TEX indirections", 0x880A, 0x8810},
1279 * \brief load the specified GPU Program
1280 * \param target program target to load into, only GL_FRAGMENT_PROGRAM is tested
1281 * \param prog program string
1282 * \return 1 on success, 0 otherwise
1284 int loadGPUProgram(GLenum target
, char *prog
) {
1286 GLint cur
= 0, max
= 0, err
= 0;
1287 if (!mpglProgramString
) {
1288 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] Missing GPU program function\n");
1291 mpglProgramString(target
, GL_PROGRAM_FORMAT_ASCII
, strlen(prog
), prog
);
1292 mpglGetIntegerv(GL_PROGRAM_ERROR_POSITION
, &err
);
1294 mp_msg(MSGT_VO
, MSGL_ERR
,
1295 "[gl] Error compiling fragment program, make sure your card supports\n"
1296 "[gl] GL_ARB_fragment_program (use glxinfo to check).\n"
1297 "[gl] Error message:\n %s at %.10s\n",
1298 mpglGetString(GL_PROGRAM_ERROR_STRING
), &prog
[err
]);
1301 if (!mpglGetProgramiv
|| !mp_msg_test(MSGT_VO
, MSGL_DBG2
))
1303 mp_msg(MSGT_VO
, MSGL_V
, "[gl] Program statistics:\n");
1304 for (i
= 0; progstats
[i
].name
; i
++) {
1305 mpglGetProgramiv(target
, progstats
[i
].cur
, &cur
);
1306 mpglGetProgramiv(target
, progstats
[i
].max
, &max
);
1307 mp_msg(MSGT_VO
, MSGL_V
, "[gl] %s: %i/%i\n", progstats
[i
].name
, cur
, max
);
1312 #define MAX_PROGSZ (1024*1024)
1315 * \brief setup a fragment program that will do YUV->RGB conversion
1316 * \param parms struct containing parameters like conversion and scaler type,
1319 static void glSetupYUVFragprog(gl_conversion_params_t
*params
) {
1320 int type
= params
->type
;
1321 int texw
= params
->texw
;
1322 int texh
= params
->texh
;
1323 int rect
= params
->target
== GL_TEXTURE_RECTANGLE
;
1324 static const char prog_hdr
[] =
1326 "OPTION ARB_precision_hint_fastest;\n"
1327 // all scaler variables must go here so they aren't defined
1328 // multiple times when the same scaler is used more than once
1329 "TEMP coord, coord2, cdelta, parmx, parmy, a, b, yuv;\n";
1331 char *yuv_prog
, *prog_pos
;
1333 char lum_scale_texs
[1];
1334 char chrom_scale_texs
[1];
1337 // this is the conversion matrix, with y, u, v factors
1338 // for red, green, blue and the constant offsets
1339 float yuv2rgb
[3][4];
1340 create_conv_textures(params
, &cur_texu
, conv_texs
);
1341 create_scaler_textures(YUV_LUM_SCALER(type
), &cur_texu
, lum_scale_texs
);
1342 if (YUV_CHROM_SCALER(type
) == YUV_LUM_SCALER(type
))
1343 memcpy(chrom_scale_texs
, lum_scale_texs
, sizeof(chrom_scale_texs
));
1345 create_scaler_textures(YUV_CHROM_SCALER(type
), &cur_texu
, chrom_scale_texs
);
1346 mpglGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS
, &i
);
1348 mp_msg(MSGT_VO
, MSGL_ERR
,
1349 "[gl] %i texture units needed for this type of YUV fragment support (found %i)\n",
1351 if (!mpglProgramString
) {
1352 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] ProgramString function missing!\n");
1355 yuv_prog
= malloc(MAX_PROGSZ
);
1356 strcpy(yuv_prog
, prog_hdr
);
1357 prog_pos
= yuv_prog
+ sizeof(prog_hdr
) - 1;
1358 prog_remain
= MAX_PROGSZ
- sizeof(prog_hdr
);
1359 add_scaler(YUV_LUM_SCALER(type
), &prog_pos
, &prog_remain
, lum_scale_texs
,
1360 '0', 'r', rect
, texw
, texh
, params
->filter_strength
);
1361 add_scaler(YUV_CHROM_SCALER(type
), &prog_pos
, &prog_remain
, chrom_scale_texs
,
1362 '1', 'g', rect
, params
->chrom_texw
, params
->chrom_texh
, params
->filter_strength
);
1363 add_scaler(YUV_CHROM_SCALER(type
), &prog_pos
, &prog_remain
, chrom_scale_texs
,
1364 '2', 'b', rect
, params
->chrom_texw
, params
->chrom_texh
, params
->filter_strength
);
1365 mp_get_yuv2rgb_coeffs(¶ms
->csp_params
, yuv2rgb
);
1366 switch (YUV_CONVERSION(type
)) {
1367 case YUV_CONVERSION_FRAGMENT
:
1368 snprintf(prog_pos
, prog_remain
, yuv_prog_template
,
1369 yuv2rgb
[ROW_R
][COL_Y
], yuv2rgb
[ROW_G
][COL_Y
], yuv2rgb
[ROW_B
][COL_Y
],
1370 yuv2rgb
[ROW_R
][COL_U
], yuv2rgb
[ROW_G
][COL_U
], yuv2rgb
[ROW_B
][COL_U
],
1371 yuv2rgb
[ROW_R
][COL_V
], yuv2rgb
[ROW_G
][COL_V
], yuv2rgb
[ROW_B
][COL_V
],
1372 yuv2rgb
[ROW_R
][COL_C
], yuv2rgb
[ROW_G
][COL_C
], yuv2rgb
[ROW_B
][COL_C
]);
1374 case YUV_CONVERSION_FRAGMENT_POW
:
1375 snprintf(prog_pos
, prog_remain
, yuv_pow_prog_template
,
1376 yuv2rgb
[ROW_R
][COL_Y
], yuv2rgb
[ROW_G
][COL_Y
], yuv2rgb
[ROW_B
][COL_Y
],
1377 yuv2rgb
[ROW_R
][COL_U
], yuv2rgb
[ROW_G
][COL_U
], yuv2rgb
[ROW_B
][COL_U
],
1378 yuv2rgb
[ROW_R
][COL_V
], yuv2rgb
[ROW_G
][COL_V
], yuv2rgb
[ROW_B
][COL_V
],
1379 yuv2rgb
[ROW_R
][COL_C
], yuv2rgb
[ROW_G
][COL_C
], yuv2rgb
[ROW_B
][COL_C
],
1380 (float)1.0 / params
->csp_params
.rgamma
, (float)1.0 / params
->csp_params
.bgamma
, (float)1.0 / params
->csp_params
.bgamma
);
1382 case YUV_CONVERSION_FRAGMENT_LOOKUP
:
1383 snprintf(prog_pos
, prog_remain
, yuv_lookup_prog_template
,
1384 yuv2rgb
[ROW_R
][COL_Y
], yuv2rgb
[ROW_G
][COL_Y
], yuv2rgb
[ROW_B
][COL_Y
],
1385 yuv2rgb
[ROW_R
][COL_U
], yuv2rgb
[ROW_G
][COL_U
], yuv2rgb
[ROW_B
][COL_U
],
1386 yuv2rgb
[ROW_R
][COL_V
], yuv2rgb
[ROW_G
][COL_V
], yuv2rgb
[ROW_B
][COL_V
],
1387 yuv2rgb
[ROW_R
][COL_C
], yuv2rgb
[ROW_G
][COL_C
], yuv2rgb
[ROW_B
][COL_C
],
1388 conv_texs
[0], conv_texs
[0], conv_texs
[0]);
1390 case YUV_CONVERSION_FRAGMENT_LOOKUP3D
:
1391 snprintf(prog_pos
, prog_remain
, yuv_lookup3d_prog_template
, conv_texs
[0]);
1394 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] unknown conversion type %i\n", YUV_CONVERSION(type
));
1397 mp_msg(MSGT_VO
, MSGL_DBG2
, "[gl] generated fragment program:\n%s\n", yuv_prog
);
1398 loadGPUProgram(GL_FRAGMENT_PROGRAM
, yuv_prog
);
1403 * \brief detect the best YUV->RGB conversion method available
1405 int glAutodetectYUVConversion(void) {
1406 const char *extensions
= mpglGetString(GL_EXTENSIONS
);
1407 if (!extensions
|| !mpglMultiTexCoord2f
)
1408 return YUV_CONVERSION_NONE
;
1409 if (strstr(extensions
, "GL_ARB_fragment_program"))
1410 return YUV_CONVERSION_FRAGMENT
;
1411 if (strstr(extensions
, "GL_ATI_text_fragment_shader"))
1412 return YUV_CONVERSION_TEXT_FRAGMENT
;
1413 if (strstr(extensions
, "GL_ATI_fragment_shader"))
1414 return YUV_CONVERSION_COMBINERS_ATI
;
1415 return YUV_CONVERSION_NONE
;
1419 * \brief setup YUV->RGB conversion
1420 * \param parms struct containing parameters like conversion and scaler type,
1422 * \ingroup glconversion
1424 void glSetupYUVConversion(gl_conversion_params_t
*params
) {
1425 float uvcos
= params
->csp_params
.saturation
* cos(params
->csp_params
.hue
);
1426 float uvsin
= params
->csp_params
.saturation
* sin(params
->csp_params
.hue
);
1427 if (params
->chrom_texw
== 0) params
->chrom_texw
= 1;
1428 if (params
->chrom_texh
== 0) params
->chrom_texh
= 1;
1429 switch (YUV_CONVERSION(params
->type
)) {
1430 case YUV_CONVERSION_COMBINERS
:
1431 glSetupYUVCombiners(uvcos
, uvsin
);
1433 case YUV_CONVERSION_COMBINERS_ATI
:
1434 glSetupYUVFragmentATI(¶ms
->csp_params
, 0);
1436 case YUV_CONVERSION_TEXT_FRAGMENT
:
1437 glSetupYUVFragmentATI(¶ms
->csp_params
, 1);
1439 case YUV_CONVERSION_FRAGMENT_LOOKUP
:
1440 case YUV_CONVERSION_FRAGMENT_LOOKUP3D
:
1441 case YUV_CONVERSION_FRAGMENT
:
1442 case YUV_CONVERSION_FRAGMENT_POW
:
1443 glSetupYUVFragprog(params
);
1445 case YUV_CONVERSION_NONE
:
1448 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] unknown conversion type %i\n", YUV_CONVERSION(params
->type
));
1453 * \brief enable the specified YUV conversion
1454 * \param target texture target for Y, U and V textures (e.g. GL_TEXTURE_2D)
1455 * \param type type of YUV conversion
1456 * \ingroup glconversion
1458 void glEnableYUVConversion(GLenum target
, int type
) {
1459 switch (YUV_CONVERSION(type
)) {
1460 case YUV_CONVERSION_COMBINERS
:
1461 mpglActiveTexture(GL_TEXTURE1
);
1463 mpglActiveTexture(GL_TEXTURE2
);
1465 mpglActiveTexture(GL_TEXTURE0
);
1466 mpglEnable(GL_REGISTER_COMBINERS_NV
);
1468 case YUV_CONVERSION_COMBINERS_ATI
:
1469 mpglActiveTexture(GL_TEXTURE1
);
1471 mpglActiveTexture(GL_TEXTURE2
);
1473 mpglActiveTexture(GL_TEXTURE0
);
1474 mpglEnable(GL_FRAGMENT_SHADER_ATI
);
1476 case YUV_CONVERSION_TEXT_FRAGMENT
:
1477 mpglActiveTexture(GL_TEXTURE1
);
1479 mpglActiveTexture(GL_TEXTURE2
);
1481 mpglActiveTexture(GL_TEXTURE0
);
1482 mpglEnable(GL_TEXT_FRAGMENT_SHADER_ATI
);
1484 case YUV_CONVERSION_FRAGMENT_LOOKUP3D
:
1485 case YUV_CONVERSION_FRAGMENT_LOOKUP
:
1486 case YUV_CONVERSION_FRAGMENT_POW
:
1487 case YUV_CONVERSION_FRAGMENT
:
1488 case YUV_CONVERSION_NONE
:
1489 mpglEnable(GL_FRAGMENT_PROGRAM
);
1495 * \brief disable the specified YUV conversion
1496 * \param target texture target for Y, U and V textures (e.g. GL_TEXTURE_2D)
1497 * \param type type of YUV conversion
1498 * \ingroup glconversion
1500 void glDisableYUVConversion(GLenum target
, int type
) {
1501 switch (YUV_CONVERSION(type
)) {
1502 case YUV_CONVERSION_COMBINERS
:
1503 mpglActiveTexture(GL_TEXTURE1
);
1504 mpglDisable(target
);
1505 mpglActiveTexture(GL_TEXTURE2
);
1506 mpglDisable(target
);
1507 mpglActiveTexture(GL_TEXTURE0
);
1508 mpglDisable(GL_REGISTER_COMBINERS_NV
);
1510 case YUV_CONVERSION_COMBINERS_ATI
:
1511 mpglActiveTexture(GL_TEXTURE1
);
1512 mpglDisable(target
);
1513 mpglActiveTexture(GL_TEXTURE2
);
1514 mpglDisable(target
);
1515 mpglActiveTexture(GL_TEXTURE0
);
1516 mpglDisable(GL_FRAGMENT_SHADER_ATI
);
1518 case YUV_CONVERSION_TEXT_FRAGMENT
:
1519 mpglDisable(GL_TEXT_FRAGMENT_SHADER_ATI
);
1520 // HACK: at least the Mac OS X 10.5 PPC Radeon drivers are broken and
1521 // without this disable the texture units while the program is still
1522 // running (10.4 PPC seems to work without this though).
1524 mpglActiveTexture(GL_TEXTURE1
);
1525 mpglDisable(target
);
1526 mpglActiveTexture(GL_TEXTURE2
);
1527 mpglDisable(target
);
1528 mpglActiveTexture(GL_TEXTURE0
);
1530 case YUV_CONVERSION_FRAGMENT_LOOKUP3D
:
1531 case YUV_CONVERSION_FRAGMENT_LOOKUP
:
1532 case YUV_CONVERSION_FRAGMENT_POW
:
1533 case YUV_CONVERSION_FRAGMENT
:
1534 case YUV_CONVERSION_NONE
:
1535 mpglDisable(GL_FRAGMENT_PROGRAM
);
1541 * \brief draw a texture part at given 2D coordinates
1542 * \param x screen top coordinate
1543 * \param y screen left coordinate
1544 * \param w screen width coordinate
1545 * \param h screen height coordinate
1546 * \param tx texture top coordinate in pixels
1547 * \param ty texture left coordinate in pixels
1548 * \param tw texture part width in pixels
1549 * \param th texture part height in pixels
1550 * \param sx width of texture in pixels
1551 * \param sy height of texture in pixels
1552 * \param rect_tex whether this texture uses texture_rectangle extension
1553 * \param is_yv12 if != 0, also draw the textures from units 1 and 2,
1554 * bits 8 - 15 and 16 - 23 specify the x and y scaling of those textures
1555 * \param flip flip the texture upside down
1556 * \ingroup gltexture
1558 void glDrawTex(GLfloat x
, GLfloat y
, GLfloat w
, GLfloat h
,
1559 GLfloat tx
, GLfloat ty
, GLfloat tw
, GLfloat th
,
1560 int sx
, int sy
, int rect_tex
, int is_yv12
, int flip
) {
1561 int chroma_x_shift
= (is_yv12
>> 8) & 31;
1562 int chroma_y_shift
= (is_yv12
>> 16) & 31;
1563 GLfloat xscale
= 1 << chroma_x_shift
;
1564 GLfloat yscale
= 1 << chroma_y_shift
;
1565 GLfloat tx2
= tx
/ xscale
, ty2
= ty
/ yscale
, tw2
= tw
/ xscale
, th2
= th
/ yscale
;
1567 tx
/= sx
; ty
/= sy
; tw
/= sx
; th
/= sy
;
1568 tx2
= tx
, ty2
= ty
, tw2
= tw
, th2
= th
;
1574 mpglBegin(GL_QUADS
);
1575 mpglTexCoord2f(tx
, ty
);
1577 mpglMultiTexCoord2f(GL_TEXTURE1
, tx2
, ty2
);
1578 mpglMultiTexCoord2f(GL_TEXTURE2
, tx2
, ty2
);
1581 mpglTexCoord2f(tx
, ty
+ th
);
1583 mpglMultiTexCoord2f(GL_TEXTURE1
, tx2
, ty2
+ th2
);
1584 mpglMultiTexCoord2f(GL_TEXTURE2
, tx2
, ty2
+ th2
);
1586 mpglVertex2f(x
, y
+ h
);
1587 mpglTexCoord2f(tx
+ tw
, ty
+ th
);
1589 mpglMultiTexCoord2f(GL_TEXTURE1
, tx2
+ tw2
, ty2
+ th2
);
1590 mpglMultiTexCoord2f(GL_TEXTURE2
, tx2
+ tw2
, ty2
+ th2
);
1592 mpglVertex2f(x
+ w
, y
+ h
);
1593 mpglTexCoord2f(tx
+ tw
, ty
);
1595 mpglMultiTexCoord2f(GL_TEXTURE1
, tx2
+ tw2
, ty2
);
1596 mpglMultiTexCoord2f(GL_TEXTURE2
, tx2
+ tw2
, ty2
);
1598 mpglVertex2f(x
+ w
, y
);
1602 #ifdef CONFIG_GL_WIN32
1603 #include "w32_common.h"
1605 * \brief little helper since wglGetProcAddress definition does not fit our
1607 * \param procName name of function to look up
1608 * \return function pointer returned by wglGetProcAddress
1610 static void *w32gpa(const GLubyte
*procName
) {
1612 void *res
= wglGetProcAddress(procName
);
1613 if (res
) return res
;
1614 oglmod
= GetModuleHandle("opengl32.dll");
1615 return GetProcAddress(oglmod
, procName
);
1618 static int setGlWindow_w32(MPGLContext
*ctx
)
1620 HWND win
= vo_w32_window
;
1621 int *vinfo
= &ctx
->vinfo
.w32
;
1622 HGLRC
*context
= &ctx
->context
.w32
;
1624 HDC windc
= vo_w32_get_dc(win
);
1625 HGLRC new_context
= 0;
1626 int keep_context
= 0;
1627 int res
= SET_WINDOW_FAILED
;
1629 // should only be needed when keeping context, but not doing glFinish
1630 // can cause flickering even when we do not keep it.
1633 new_vinfo
= GetPixelFormat(windc
);
1634 if (*context
&& *vinfo
&& new_vinfo
&& *vinfo
== new_vinfo
) {
1635 // we can keep the wglContext
1636 new_context
= *context
;
1640 new_context
= wglCreateContext(windc
);
1642 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Could not create GL context!\n");
1648 if (!wglMakeCurrent(windc
, new_context
)) {
1649 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Could not set GL context!\n");
1650 if (!keep_context
) {
1651 wglDeleteContext(new_context
);
1657 vo_w32_window
= win
;
1660 GetClientRect(win
, &rect
);
1661 vo_dwidth
= rect
.right
;
1662 vo_dheight
= rect
.bottom
;
1664 if (!keep_context
) {
1666 wglDeleteContext(*context
);
1667 *context
= new_context
;
1669 getFunctions(w32gpa
, NULL
);
1671 // and inform that reinit is neccessary
1672 res
= SET_WINDOW_REINIT
;
1674 res
= SET_WINDOW_OK
;
1677 vo_w32_release_dc(win
, windc
);
1681 static void releaseGlContext_w32(MPGLContext
*ctx
) {
1682 int *vinfo
= &ctx
->vinfo
.w32
;
1683 HGLRC
*context
= &ctx
->context
.w32
;
1686 wglMakeCurrent(0, 0);
1687 wglDeleteContext(*context
);
1692 static void swapGlBuffers_w32(MPGLContext
*ctx
) {
1693 HDC vo_hdc
= vo_w32_get_dc(vo_w32_window
);
1694 SwapBuffers(vo_hdc
);
1695 vo_w32_release_dc(vo_w32_window
, vo_hdc
);
1698 #ifdef CONFIG_GL_X11
1699 #include "x11_common.h"
1702 * \brief Returns the XVisualInfo associated with Window win.
1703 * \param win Window whose XVisualInfo is returne.
1704 * \return XVisualInfo of the window. Caller must use XFree to free it.
1706 static XVisualInfo
*getWindowVisualInfo(Window win
) {
1707 XWindowAttributes xw_attr
;
1708 XVisualInfo vinfo_template
;
1710 XGetWindowAttributes(mDisplay
, win
, &xw_attr
);
1711 vinfo_template
.visualid
= XVisualIDFromVisual(xw_attr
.visual
);
1712 return XGetVisualInfo(mDisplay
, VisualIDMask
, &vinfo_template
, &tmp
);
1715 static void appendstr(char **dst
, const char *str
)
1721 newsize
= strlen(*dst
) + 1 + strlen(str
) + 1;
1722 newstr
= realloc(*dst
, newsize
);
1731 * \brief Changes the window in which video is displayed.
1732 * If possible only transfers the context to the new window, otherwise
1733 * creates a new one, which must be initialized by the caller.
1734 * \param vinfo Currently used visual.
1735 * \param context Currently used context.
1736 * \param win window that should be used for drawing.
1737 * \return one of SET_WINDOW_FAILED, SET_WINDOW_OK or SET_WINDOW_REINIT.
1738 * In case of SET_WINDOW_REINIT the context could not be transfered
1739 * and the caller must initialize it correctly.
1740 * \ingroup glcontext
1742 static int setGlWindow_x11(MPGLContext
*ctx
)
1744 XVisualInfo
**vinfo
= &ctx
->vinfo
.x11
;
1745 GLXContext
*context
= &ctx
->context
.x11
;
1746 Window win
= vo_window
;
1747 XVisualInfo
*new_vinfo
;
1748 GLXContext new_context
= NULL
;
1749 int keep_context
= 0;
1751 // should only be needed when keeping context, but not doing glFinish
1752 // can cause flickering even when we do not keep it.
1755 new_vinfo
= getWindowVisualInfo(win
);
1756 if (*context
&& *vinfo
&& new_vinfo
&&
1757 (*vinfo
)->visualid
== new_vinfo
->visualid
) {
1758 // we can keep the GLXContext
1759 new_context
= *context
;
1765 new_context
= glXCreateContext(mDisplay
, new_vinfo
, NULL
, True
);
1767 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Could not create GLX context!\n");
1769 return SET_WINDOW_FAILED
;
1774 if (!glXMakeCurrent(mDisplay
, vo_window
, new_context
)) {
1775 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Could not set GLX context!\n");
1776 if (!keep_context
) {
1777 glXDestroyContext(mDisplay
, new_context
);
1780 return SET_WINDOW_FAILED
;
1785 vo_x11_update_geometry();
1786 if (!keep_context
) {
1787 void *(*getProcAddress
)(const GLubyte
*);
1788 const char *(*glXExtStr
)(Display
*, int);
1789 char *glxstr
= strdup("");
1791 glXDestroyContext(mDisplay
, *context
);
1792 *context
= new_context
;
1796 getProcAddress
= getdladdr("glXGetProcAddress");
1797 if (!getProcAddress
)
1798 getProcAddress
= getdladdr("glXGetProcAddressARB");
1799 glXExtStr
= getdladdr("glXQueryExtensionsString");
1801 appendstr(&glxstr
, glXExtStr(mDisplay
, DefaultScreen(mDisplay
)));
1802 glXExtStr
= getdladdr("glXGetClientString");
1804 appendstr(&glxstr
, glXExtStr(mDisplay
, GLX_EXTENSIONS
));
1805 glXExtStr
= getdladdr("glXGetServerString");
1807 appendstr(&glxstr
, glXExtStr(mDisplay
, GLX_EXTENSIONS
));
1809 getFunctions(getProcAddress
, glxstr
);
1810 if (!mpglGenPrograms
&& mpglGetString
&&
1812 strstr(mpglGetString(GL_EXTENSIONS
), "GL_ARB_vertex_program")) {
1813 mp_msg(MSGT_VO
, MSGL_WARN
, "Broken glXGetProcAddress detected, trying workaround\n");
1814 getFunctions(NULL
, glxstr
);
1818 // and inform that reinit is neccessary
1819 return SET_WINDOW_REINIT
;
1821 return SET_WINDOW_OK
;
1825 * \brief free the VisualInfo and GLXContext of an OpenGL context.
1826 * \ingroup glcontext
1828 static void releaseGlContext_x11(MPGLContext
*ctx
) {
1829 XVisualInfo
**vinfo
= &ctx
->vinfo
.x11
;
1830 GLXContext
*context
= &ctx
->context
.x11
;
1837 glXMakeCurrent(mDisplay
, None
, NULL
);
1838 glXDestroyContext(mDisplay
, *context
);
1843 static void swapGlBuffers_x11(MPGLContext
*ctx
) {
1844 glXSwapBuffers(mDisplay
, vo_window
);
1847 static int x11_check_events(void) {
1848 return vo_x11_check_events(mDisplay
);
1852 #ifdef CONFIG_GL_SDL
1853 #include "sdl_common.h"
1855 static void swapGlBuffers_sdl(MPGLContext
*ctx
) {
1856 SDL_GL_SwapBuffers();
1859 static void *sdlgpa(const GLubyte
*name
) {
1860 return SDL_GL_GetProcAddress(name
);
1863 static int setGlWindow_sdl(MPGLContext
*ctx
) {
1864 if (sdl_set_mode(0, SDL_OPENGL
| SDL_RESIZABLE
) < 0)
1865 return SET_WINDOW_FAILED
;
1866 SDL_GL_LoadLibrary(NULL
);
1867 getFunctions(sdlgpa
, NULL
);
1868 return SET_WINDOW_OK
;
1871 static int sdl_check_events(void) {
1874 while (SDL_PollEvent(&event
)) {
1875 res
|= sdl_default_handle_event(&event
);
1877 // poll "events" from within MPlayer code
1878 res
|= sdl_default_handle_event(NULL
);
1879 if (res
& VO_EVENT_RESIZE
)
1880 sdl_set_mode(0, SDL_OPENGL
| SDL_RESIZABLE
);
1886 static int setGlWindow_dummy(MPGLContext
*ctx
) {
1887 getFunctions(NULL
, NULL
);
1888 return SET_WINDOW_OK
;
1891 static void releaseGlContext_dummy(MPGLContext
*ctx
) {
1894 static int dummy_check_events(void) {
1898 static void dummy_update_xinerama_info(void) {
1899 if (vo_screenwidth
<= 0 || vo_screenheight
<= 0) {
1900 mp_msg(MSGT_VO
, MSGL_ERR
, "You must specify the screen dimensions "
1901 "with -screenw and -screenh\n");
1902 vo_screenwidth
= 1280;
1903 vo_screenheight
= 768;
1905 aspect_save_screenres(vo_screenwidth
, vo_screenheight
);
1908 int init_mpglcontext(MPGLContext
*ctx
, enum MPGLType type
) {
1909 if (type
== GLTYPE_AUTO
) {
1910 int res
= init_mpglcontext(ctx
, GLTYPE_W32
);
1911 if (res
) return res
;
1912 res
= init_mpglcontext(ctx
, GLTYPE_X11
);
1913 if (res
) return res
;
1914 res
= init_mpglcontext(ctx
, GLTYPE_SDL
);
1917 memset(ctx
, 0, sizeof(*ctx
));
1918 ctx
->setGlWindow
= setGlWindow_dummy
;
1919 ctx
->releaseGlContext
= releaseGlContext_dummy
;
1920 ctx
->update_xinerama_info
= dummy_update_xinerama_info
;
1921 ctx
->check_events
= dummy_check_events
;
1923 switch (ctx
->type
) {
1924 #ifdef CONFIG_GL_WIN32
1926 ctx
->setGlWindow
= setGlWindow_w32
;
1927 ctx
->releaseGlContext
= releaseGlContext_w32
;
1928 ctx
->swapGlBuffers
= swapGlBuffers_w32
;
1929 ctx
->update_xinerama_info
= w32_update_xinerama_info
;
1930 ctx
->border
= vo_w32_border
;
1931 ctx
->check_events
= vo_w32_check_events
;
1932 ctx
->fullscreen
= vo_w32_fullscreen
;
1933 ctx
->ontop
= vo_w32_ontop
;
1934 return vo_w32_init();
1936 #ifdef CONFIG_GL_X11
1938 ctx
->setGlWindow
= setGlWindow_x11
;
1939 ctx
->releaseGlContext
= releaseGlContext_x11
;
1940 ctx
->swapGlBuffers
= swapGlBuffers_x11
;
1941 ctx
->update_xinerama_info
= update_xinerama_info
;
1942 ctx
->border
= vo_x11_border
;
1943 ctx
->check_events
= x11_check_events
;
1944 ctx
->fullscreen
= vo_x11_fullscreen
;
1945 ctx
->ontop
= vo_x11_ontop
;
1948 #ifdef CONFIG_GL_SDL
1950 SDL_Init(SDL_INIT_VIDEO
);
1951 ctx
->setGlWindow
= setGlWindow_sdl
;
1952 ctx
->swapGlBuffers
= swapGlBuffers_sdl
;
1953 ctx
->check_events
= sdl_check_events
;
1954 ctx
->fullscreen
= vo_sdl_fullscreen
;
1955 return vo_sdl_init();
1962 void uninit_mpglcontext(MPGLContext
*ctx
) {
1963 ctx
->releaseGlContext(ctx
);
1964 switch (ctx
->type
) {
1965 #ifdef CONFIG_GL_WIN32
1970 #ifdef CONFIG_GL_X11
1975 #ifdef CONFIG_GL_SDL
1981 memset(ctx
, 0, sizeof(*ctx
));