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 "old_vo_defines.h"
36 #include "gl_common.h"
40 void (GLAPIENTRY
*mpglBegin
)(GLenum
);
41 void (GLAPIENTRY
*mpglEnd
)(void);
42 void (GLAPIENTRY
*mpglViewport
)(GLint
, GLint
, GLsizei
, GLsizei
);
43 void (GLAPIENTRY
*mpglMatrixMode
)(GLenum
);
44 void (GLAPIENTRY
*mpglLoadIdentity
)(void);
45 void (GLAPIENTRY
*mpglTranslated
)(double, double, double);
46 void (GLAPIENTRY
*mpglScaled
)(double, double, double);
47 void (GLAPIENTRY
*mpglOrtho
)(double, double, double, double, double, double);
48 void (GLAPIENTRY
*mpglFrustum
)(double, double, double, double, double, double);
49 void (GLAPIENTRY
*mpglPushMatrix
)(void);
50 void (GLAPIENTRY
*mpglPopMatrix
)(void);
51 void (GLAPIENTRY
*mpglClear
)(GLbitfield
);
52 GLuint (GLAPIENTRY
*mpglGenLists
)(GLsizei
);
53 void (GLAPIENTRY
*mpglDeleteLists
)(GLuint
, GLsizei
);
54 void (GLAPIENTRY
*mpglNewList
)(GLuint
, GLenum
);
55 void (GLAPIENTRY
*mpglEndList
)(void);
56 void (GLAPIENTRY
*mpglCallList
)(GLuint
);
57 void (GLAPIENTRY
*mpglCallLists
)(GLsizei
, GLenum
, const GLvoid
*);
58 void (GLAPIENTRY
*mpglGenTextures
)(GLsizei
, GLuint
*);
59 void (GLAPIENTRY
*mpglDeleteTextures
)(GLsizei
, const GLuint
*);
60 void (GLAPIENTRY
*mpglTexEnvf
)(GLenum
, GLenum
, GLfloat
);
61 void (GLAPIENTRY
*mpglTexEnvi
)(GLenum
, GLenum
, GLint
);
62 void (GLAPIENTRY
*mpglColor4ub
)(GLubyte
, GLubyte
, GLubyte
, GLubyte
);
63 void (GLAPIENTRY
*mpglColor3f
)(GLfloat
, GLfloat
, GLfloat
);
64 void (GLAPIENTRY
*mpglColor4f
)(GLfloat
, GLfloat
, GLfloat
, GLfloat
);
65 void (GLAPIENTRY
*mpglClearColor
)(GLclampf
, GLclampf
, GLclampf
, GLclampf
);
66 void (GLAPIENTRY
*mpglClearDepth
)(GLclampd
);
67 void (GLAPIENTRY
*mpglDepthFunc
)(GLenum
);
68 void (GLAPIENTRY
*mpglEnable
)(GLenum
);
69 void (GLAPIENTRY
*mpglDisable
)(GLenum
);
70 const GLubyte
*(GLAPIENTRY
*mpglGetString
)(GLenum
);
71 void (GLAPIENTRY
*mpglDrawBuffer
)(GLenum
);
72 void (GLAPIENTRY
*mpglDepthMask
)(GLboolean
);
73 void (GLAPIENTRY
*mpglBlendFunc
)(GLenum
, GLenum
);
74 void (GLAPIENTRY
*mpglFlush
)(void);
75 void (GLAPIENTRY
*mpglFinish
)(void);
76 void (GLAPIENTRY
*mpglPixelStorei
)(GLenum
, GLint
);
77 void (GLAPIENTRY
*mpglTexImage1D
)(GLenum
, GLint
, GLint
, GLsizei
, GLint
, GLenum
, GLenum
, const GLvoid
*);
78 void (GLAPIENTRY
*mpglTexImage2D
)(GLenum
, GLint
, GLint
, GLsizei
, GLsizei
, GLint
, GLenum
, GLenum
, const GLvoid
*);
79 void (GLAPIENTRY
*mpglTexSubImage2D
)(GLenum
, GLint
, GLint
, GLint
, GLsizei
, GLsizei
, GLenum
, GLenum
, const GLvoid
*);
80 void (GLAPIENTRY
*mpglTexParameteri
)(GLenum
, GLenum
, GLint
);
81 void (GLAPIENTRY
*mpglTexParameterf
)(GLenum
, GLenum
, GLfloat
);
82 void (GLAPIENTRY
*mpglTexParameterfv
)(GLenum
, GLenum
, const GLfloat
*);
83 void (GLAPIENTRY
*mpglTexCoord2f
)(GLfloat
, GLfloat
);
84 void (GLAPIENTRY
*mpglVertex2f
)(GLfloat
, GLfloat
);
85 void (GLAPIENTRY
*mpglVertex3f
)(GLfloat
, GLfloat
, GLfloat
);
86 void (GLAPIENTRY
*mpglNormal3f
)(GLfloat
, GLfloat
, GLfloat
);
87 void (GLAPIENTRY
*mpglLightfv
)(GLenum
, GLenum
, const GLfloat
*);
88 void (GLAPIENTRY
*mpglColorMaterial
)(GLenum
, GLenum
);
89 void (GLAPIENTRY
*mpglShadeModel
)(GLenum
);
90 void (GLAPIENTRY
*mpglGetIntegerv
)(GLenum
, GLint
*);
93 * \defgroup glextfunctions OpenGL extension functions
95 * the pointers to these functions are acquired when the OpenGL
99 void (GLAPIENTRY
*mpglGenBuffers
)(GLsizei
, GLuint
*);
100 void (GLAPIENTRY
*mpglDeleteBuffers
)(GLsizei
, const GLuint
*);
101 void (GLAPIENTRY
*mpglBindBuffer
)(GLenum
, GLuint
);
102 GLvoid
* (GLAPIENTRY
*mpglMapBuffer
)(GLenum
, GLenum
);
103 GLboolean (GLAPIENTRY
*mpglUnmapBuffer
)(GLenum
);
104 void (GLAPIENTRY
*mpglBufferData
)(GLenum
, intptr_t, const GLvoid
*, GLenum
);
105 void (GLAPIENTRY
*mpglCombinerParameterfv
)(GLenum
, const GLfloat
*);
106 void (GLAPIENTRY
*mpglCombinerParameteri
)(GLenum
, GLint
);
107 void (GLAPIENTRY
*mpglCombinerInput
)(GLenum
, GLenum
, GLenum
, GLenum
, GLenum
,
109 void (GLAPIENTRY
*mpglCombinerOutput
)(GLenum
, GLenum
, GLenum
, GLenum
, GLenum
,
110 GLenum
, GLenum
, GLboolean
, GLboolean
,
112 void (GLAPIENTRY
*mpglBeginFragmentShader
)(void);
113 void (GLAPIENTRY
*mpglEndFragmentShader
)(void);
114 void (GLAPIENTRY
*mpglSampleMap
)(GLuint
, GLuint
, GLenum
);
115 void (GLAPIENTRY
*mpglColorFragmentOp2
)(GLenum
, GLuint
, GLuint
, GLuint
, GLuint
,
116 GLuint
, GLuint
, GLuint
, GLuint
, GLuint
);
117 void (GLAPIENTRY
*mpglColorFragmentOp3
)(GLenum
, GLuint
, GLuint
, GLuint
, GLuint
,
118 GLuint
, GLuint
, GLuint
, GLuint
, GLuint
,
119 GLuint
, GLuint
, GLuint
);
120 void (GLAPIENTRY
*mpglSetFragmentShaderConstant
)(GLuint
, const GLfloat
*);
121 void (GLAPIENTRY
*mpglActiveTexture
)(GLenum
);
122 void (GLAPIENTRY
*mpglBindTexture
)(GLenum
, GLuint
);
123 void (GLAPIENTRY
*mpglMultiTexCoord2f
)(GLenum
, GLfloat
, GLfloat
);
124 void (GLAPIENTRY
*mpglGenPrograms
)(GLsizei
, GLuint
*);
125 void (GLAPIENTRY
*mpglDeletePrograms
)(GLsizei
, const GLuint
*);
126 void (GLAPIENTRY
*mpglBindProgram
)(GLenum
, GLuint
);
127 void (GLAPIENTRY
*mpglProgramString
)(GLenum
, GLenum
, GLsizei
, const GLvoid
*);
128 void (GLAPIENTRY
*mpglGetProgramiv
)(GLenum
, GLenum
, GLint
*);
129 void (GLAPIENTRY
*mpglProgramEnvParameter4f
)(GLenum
, GLuint
, GLfloat
, GLfloat
,
131 int (GLAPIENTRY
*mpglSwapInterval
)(int);
132 void (GLAPIENTRY
*mpglTexImage3D
)(GLenum
, GLint
, GLenum
, GLsizei
, GLsizei
, GLsizei
,
133 GLint
, GLenum
, GLenum
, const GLvoid
*);
134 void* (GLAPIENTRY
*mpglAllocateMemoryMESA
)(void *, int, size_t, float, float, float);
135 void (GLAPIENTRY
*mpglFreeMemoryMESA
)(void *, int, void *);
136 /** \} */ // end of glextfunctions group
138 //! \defgroup glgeneral OpenGL general helper functions
140 //! \defgroup glcontext OpenGL context management helper functions
142 //! \defgroup gltexture OpenGL texture handling helper functions
144 //! \defgroup glconversion OpenGL conversion helper functions
146 static GLint hqtexfmt
;
149 * \brief adjusts the GL_UNPACK_ALIGNMENT to fit the stride.
150 * \param stride number of bytes per line for which alignment should fit.
153 void glAdjustAlignment(int stride
) {
157 else if (stride
% 4 == 0)
159 else if (stride
% 2 == 0)
163 mpglPixelStorei(GL_UNPACK_ALIGNMENT
, gl_alignment
);
166 struct gl_name_map_struct
{
172 #define MAP(a) {a, #a}
173 //! mapping table for the glValName function
174 static const struct gl_name_map_struct gl_name_map
[] = {
176 MAP(GL_R3_G3_B2
), MAP(GL_RGB4
), MAP(GL_RGB5
), MAP(GL_RGB8
),
177 MAP(GL_RGB10
), MAP(GL_RGB12
), MAP(GL_RGB16
), MAP(GL_RGBA2
),
178 MAP(GL_RGBA4
), MAP(GL_RGB5_A1
), MAP(GL_RGBA8
), MAP(GL_RGB10_A2
),
179 MAP(GL_RGBA12
), MAP(GL_RGBA16
), MAP(GL_LUMINANCE8
),
182 MAP(GL_RGB
), MAP(GL_RGBA
), MAP(GL_RED
), MAP(GL_GREEN
), MAP(GL_BLUE
),
183 MAP(GL_ALPHA
), MAP(GL_LUMINANCE
), MAP(GL_LUMINANCE_ALPHA
),
186 MAP(GL_BGR
), MAP(GL_BGRA
),
189 MAP(GL_BYTE
), MAP(GL_UNSIGNED_BYTE
), MAP(GL_SHORT
), MAP(GL_UNSIGNED_SHORT
),
190 MAP(GL_INT
), MAP(GL_UNSIGNED_INT
), MAP(GL_FLOAT
), MAP(GL_DOUBLE
),
191 MAP(GL_2_BYTES
), MAP(GL_3_BYTES
), MAP(GL_4_BYTES
),
193 MAP(GL_UNSIGNED_BYTE_3_3_2
), MAP(GL_UNSIGNED_BYTE_2_3_3_REV
),
194 MAP(GL_UNSIGNED_SHORT_5_6_5
), MAP(GL_UNSIGNED_SHORT_5_6_5_REV
),
195 MAP(GL_UNSIGNED_SHORT_4_4_4_4
), MAP(GL_UNSIGNED_SHORT_4_4_4_4_REV
),
196 MAP(GL_UNSIGNED_SHORT_5_5_5_1
), MAP(GL_UNSIGNED_SHORT_1_5_5_5_REV
),
197 MAP(GL_UNSIGNED_INT_8_8_8_8
), MAP(GL_UNSIGNED_INT_8_8_8_8_REV
),
198 MAP(GL_UNSIGNED_INT_10_10_10_2
), MAP(GL_UNSIGNED_INT_2_10_10_10_REV
),
204 * \brief return the name of an OpenGL constant
205 * \param value the constant
206 * \return name of the constant or "Unknown format!"
209 const char *glValName(GLint value
)
213 while (gl_name_map
[i
].name
) {
214 if (gl_name_map
[i
].value
== value
)
215 return gl_name_map
[i
].name
;
218 return "Unknown format!";
221 //! always return this format as internal texture format in glFindFormat
222 #define TEXTUREFORMAT_ALWAYS GL_RGB8
223 #undef TEXTUREFORMAT_ALWAYS
226 * \brief find the OpenGL settings coresponding to format.
228 * All parameters may be NULL.
229 * \param fmt MPlayer format to analyze.
230 * \param bpp [OUT] bits per pixel of that format.
231 * \param gl_texfmt [OUT] internal texture format that fits the
232 * image format, not necessarily the best for performance.
233 * \param gl_format [OUT] OpenGL format for this image format.
234 * \param gl_type [OUT] OpenGL type for this image format.
235 * \return 1 if format is supported by OpenGL, 0 if not.
238 int glFindFormat(uint32_t fmt
, int *bpp
, GLint
*gl_texfmt
,
239 GLenum
*gl_format
, GLenum
*gl_type
)
245 if (!bpp
) bpp
= &dummy1
;
246 if (!gl_texfmt
) gl_texfmt
= &dummy3
;
247 if (!gl_format
) gl_format
= &dummy2
;
248 if (!gl_type
) gl_type
= &dummy2
;
250 if (mp_get_chroma_shift(fmt
, NULL
, NULL
)) {
251 // reduce the possible cases a bit
252 if (IMGFMT_IS_YUVP16_LE(fmt
))
253 fmt
= IMGFMT_420P16_LE
;
254 else if (IMGFMT_IS_YUVP16_BE(fmt
))
255 fmt
= IMGFMT_420P16_BE
;
260 *bpp
= IMGFMT_IS_BGR(fmt
)?IMGFMT_BGR_DEPTH(fmt
):IMGFMT_RGB_DEPTH(fmt
);
265 *gl_type
= GL_UNSIGNED_SHORT
;
269 *gl_type
= GL_UNSIGNED_BYTE
;
273 *gl_format
= GL_RGBA
;
274 *gl_type
= GL_UNSIGNED_BYTE
;
277 supported
= 0; // no native YUV support
280 *gl_format
= GL_LUMINANCE
;
281 *gl_type
= GL_UNSIGNED_SHORT
;
284 supported
= 0; // no native YV12 support
289 *gl_format
= GL_LUMINANCE
;
290 *gl_type
= GL_UNSIGNED_BYTE
;
294 *gl_texfmt
= GL_YCBCR_MESA
;
296 *gl_format
= GL_YCBCR_MESA
;
297 *gl_type
= fmt
== IMGFMT_UYVY
? GL_UNSIGNED_SHORT_8_8
: GL_UNSIGNED_SHORT_8_8_REV
;
300 // we do not support palettized formats, although the format the
301 // swscale produces works
304 gl_type
= GL_UNSIGNED_BYTE_2_3_3_REV
;
308 *gl_format
= GL_RGBA
;
309 *gl_type
= GL_UNSIGNED_SHORT_1_5_5_5_REV
;
313 *gl_type
= GL_UNSIGNED_SHORT_5_6_5_REV
;
317 // special case as red and blue have a differen number of bits.
318 // GL_BGR and GL_UNSIGNED_BYTE_3_3_2 isn't supported at least
319 // by nVidia drivers, and in addition would give more bits to
320 // blue than to red, which isn't wanted
322 gl_type
= GL_UNSIGNED_BYTE_3_3_2
;
326 *gl_format
= GL_BGRA
;
327 *gl_type
= GL_UNSIGNED_SHORT_1_5_5_5_REV
;
331 *gl_type
= GL_UNSIGNED_SHORT_5_6_5
;
335 *gl_type
= GL_UNSIGNED_BYTE
;
339 *gl_format
= GL_BGRA
;
340 *gl_type
= GL_UNSIGNED_BYTE
;
344 *gl_format
= GL_RGBA
;
345 *gl_type
= GL_UNSIGNED_BYTE
;
348 #ifdef TEXTUREFORMAT_ALWAYS
349 *gl_texfmt
= TEXTUREFORMAT_ALWAYS
;
358 * \brief find address of a linked function
359 * \param s name of function to find
360 * \return address of function or NULL if not found
362 static void *getdladdr(const char *s
) {
365 void *handle
= dlopen(NULL
, RTLD_LAZY
);
368 ret
= dlsym(handle
, s
);
377 const char *funcnames
[7];
381 #define DEF_FUNC_DESC(name) {&mpgl##name, NULL, {"gl"#name, NULL}, gl ##name}
382 static const extfunc_desc_t extfuncs
[] = {
383 // these aren't extension functions but we query them anyway to allow
384 // different "backends" with one binary
385 DEF_FUNC_DESC(Begin
),
387 DEF_FUNC_DESC(Viewport
),
388 DEF_FUNC_DESC(MatrixMode
),
389 DEF_FUNC_DESC(LoadIdentity
),
390 DEF_FUNC_DESC(Translated
),
391 DEF_FUNC_DESC(Scaled
),
392 DEF_FUNC_DESC(Ortho
),
393 DEF_FUNC_DESC(Frustum
),
394 DEF_FUNC_DESC(PushMatrix
),
395 DEF_FUNC_DESC(PopMatrix
),
396 DEF_FUNC_DESC(Clear
),
397 DEF_FUNC_DESC(GenLists
),
398 DEF_FUNC_DESC(DeleteLists
),
399 DEF_FUNC_DESC(NewList
),
400 DEF_FUNC_DESC(EndList
),
401 DEF_FUNC_DESC(CallList
),
402 DEF_FUNC_DESC(CallLists
),
403 DEF_FUNC_DESC(GenTextures
),
404 DEF_FUNC_DESC(DeleteTextures
),
405 DEF_FUNC_DESC(TexEnvf
),
406 DEF_FUNC_DESC(TexEnvi
),
407 DEF_FUNC_DESC(Color4ub
),
408 DEF_FUNC_DESC(Color3f
),
409 DEF_FUNC_DESC(Color4f
),
410 DEF_FUNC_DESC(ClearColor
),
411 DEF_FUNC_DESC(ClearDepth
),
412 DEF_FUNC_DESC(DepthFunc
),
413 DEF_FUNC_DESC(Enable
),
414 DEF_FUNC_DESC(Disable
),
415 DEF_FUNC_DESC(DrawBuffer
),
416 DEF_FUNC_DESC(DepthMask
),
417 DEF_FUNC_DESC(BlendFunc
),
418 DEF_FUNC_DESC(Flush
),
419 DEF_FUNC_DESC(Finish
),
420 DEF_FUNC_DESC(PixelStorei
),
421 DEF_FUNC_DESC(TexImage1D
),
422 DEF_FUNC_DESC(TexImage2D
),
423 DEF_FUNC_DESC(TexSubImage2D
),
424 DEF_FUNC_DESC(TexParameteri
),
425 DEF_FUNC_DESC(TexParameterf
),
426 DEF_FUNC_DESC(TexParameterfv
),
427 DEF_FUNC_DESC(TexCoord2f
),
428 DEF_FUNC_DESC(Vertex2f
),
429 DEF_FUNC_DESC(Vertex3f
),
430 DEF_FUNC_DESC(Normal3f
),
431 DEF_FUNC_DESC(Lightfv
),
432 DEF_FUNC_DESC(ColorMaterial
),
433 DEF_FUNC_DESC(ShadeModel
),
434 DEF_FUNC_DESC(GetIntegerv
),
436 // here start the real extensions
437 {&mpglGenBuffers
, NULL
, {"glGenBuffers", "glGenBuffersARB", NULL
}},
438 {&mpglDeleteBuffers
, NULL
, {"glDeleteBuffers", "glDeleteBuffersARB", NULL
}},
439 {&mpglBindBuffer
, NULL
, {"glBindBuffer", "glBindBufferARB", NULL
}},
440 {&mpglMapBuffer
, NULL
, {"glMapBuffer", "glMapBufferARB", NULL
}},
441 {&mpglUnmapBuffer
, NULL
, {"glUnmapBuffer", "glUnmapBufferARB", NULL
}},
442 {&mpglBufferData
, NULL
, {"glBufferData", "glBufferDataARB", NULL
}},
443 {&mpglCombinerParameterfv
, "NV_register_combiners", {"glCombinerParameterfv", "glCombinerParameterfvNV", NULL
}},
444 {&mpglCombinerParameteri
, "NV_register_combiners", {"glCombinerParameteri", "glCombinerParameteriNV", NULL
}},
445 {&mpglCombinerInput
, "NV_register_combiners", {"glCombinerInput", "glCombinerInputNV", NULL
}},
446 {&mpglCombinerOutput
, "NV_register_combiners", {"glCombinerOutput", "glCombinerOutputNV", NULL
}},
447 {&mpglBeginFragmentShader
, "ATI_fragment_shader", {"glBeginFragmentShaderATI", NULL
}},
448 {&mpglEndFragmentShader
, "ATI_fragment_shader", {"glEndFragmentShaderATI", NULL
}},
449 {&mpglSampleMap
, "ATI_fragment_shader", {"glSampleMapATI", NULL
}},
450 {&mpglColorFragmentOp2
, "ATI_fragment_shader", {"glColorFragmentOp2ATI", NULL
}},
451 {&mpglColorFragmentOp3
, "ATI_fragment_shader", {"glColorFragmentOp3ATI", NULL
}},
452 {&mpglSetFragmentShaderConstant
, "ATI_fragment_shader", {"glSetFragmentShaderConstantATI", NULL
}},
453 {&mpglActiveTexture
, NULL
, {"glActiveTexture", "glActiveTextureARB", NULL
}},
454 {&mpglBindTexture
, NULL
, {"glBindTexture", "glBindTextureARB", "glBindTextureEXT", NULL
}},
455 {&mpglMultiTexCoord2f
, NULL
, {"glMultiTexCoord2f", "glMultiTexCoord2fARB", NULL
}},
456 {&mpglGenPrograms
, "_program", {"glGenProgramsARB", NULL
}},
457 {&mpglDeletePrograms
, "_program", {"glDeleteProgramsARB", NULL
}},
458 {&mpglBindProgram
, "_program", {"glBindProgramARB", NULL
}},
459 {&mpglProgramString
, "_program", {"glProgramStringARB", NULL
}},
460 {&mpglGetProgramiv
, "_program", {"glGetProgramivARB", NULL
}},
461 {&mpglProgramEnvParameter4f
, "_program", {"glProgramEnvParameter4fARB", NULL
}},
462 {&mpglSwapInterval
, "_swap_control", {"glXSwapIntervalSGI", "glXSwapInterval", "wglSwapIntervalSGI", "wglSwapInterval", "wglSwapIntervalEXT", NULL
}},
463 {&mpglTexImage3D
, NULL
, {"glTexImage3D", NULL
}},
464 {&mpglAllocateMemoryMESA
, "GLX_MESA_allocate_memory", {"glXAllocateMemoryMESA", NULL
}},
465 {&mpglFreeMemoryMESA
, "GLX_MESA_allocate_memory", {"glXFreeMemoryMESA", NULL
}},
470 * \brief find the function pointers of some useful OpenGL extensions
471 * \param getProcAddress function to resolve function names, may be NULL
472 * \param ext2 an extra extension string
474 static void getFunctions(void *(*getProcAddress
)(const GLubyte
*),
476 const extfunc_desc_t
*dsc
;
477 const char *extensions
;
481 getProcAddress
= (void *)getdladdr
;
483 // special case, we need glGetString before starting to find the other functions
484 mpglGetString
= getProcAddress("glGetString");
486 mpglGetString
= glGetString
;
488 extensions
= (const char *)mpglGetString(GL_EXTENSIONS
);
489 if (!extensions
) extensions
= "";
490 if (!ext2
) ext2
= "";
491 allexts
= malloc(strlen(extensions
) + strlen(ext2
) + 2);
492 strcpy(allexts
, extensions
);
493 strcat(allexts
, " ");
494 strcat(allexts
, ext2
);
495 mp_msg(MSGT_VO
, MSGL_DBG2
, "OpenGL extensions string:\n%s\n", allexts
);
496 for (dsc
= extfuncs
; dsc
->funcptr
; dsc
++) {
499 if (!dsc
->extstr
|| strstr(allexts
, dsc
->extstr
)) {
500 for (i
= 0; !ptr
&& dsc
->funcnames
[i
]; i
++)
501 ptr
= getProcAddress((const GLubyte
*)dsc
->funcnames
[i
]);
505 *(void **)dsc
->funcptr
= ptr
;
507 if (strstr(allexts
, "_texture_float"))
508 hqtexfmt
= GL_RGB32F
;
509 else if (strstr(allexts
, "NV_float_buffer"))
510 hqtexfmt
= GL_FLOAT_RGB32_NV
;
517 * \brief create a texture and set some defaults
518 * \param target texture taget, usually GL_TEXTURE_2D
519 * \param fmt internal texture format
520 * \param format texture host data format
521 * \param type texture host data type
522 * \param filter filter used for scaling, e.g. GL_LINEAR
523 * \param w texture width
524 * \param h texture height
525 * \param val luminance value to fill texture with
528 void glCreateClearTex(GLenum target
, GLenum fmt
, GLenum format
, GLenum type
, GLint filter
,
529 int w
, int h
, unsigned char val
) {
530 GLfloat fval
= (GLfloat
)val
/ 255.0;
531 GLfloat border
[4] = {fval
, fval
, fval
, fval
};
536 stride
= w
* glFmt2bpp(format
, type
);
538 init
= malloc(stride
* h
);
539 memset(init
, val
, stride
* h
);
540 glAdjustAlignment(stride
);
541 mpglPixelStorei(GL_UNPACK_ROW_LENGTH
, w
);
542 mpglTexImage2D(target
, 0, fmt
, w
, h
, 0, format
, type
, init
);
543 mpglTexParameterf(target
, GL_TEXTURE_PRIORITY
, 1.0);
544 mpglTexParameteri(target
, GL_TEXTURE_MIN_FILTER
, filter
);
545 mpglTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, filter
);
546 mpglTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
547 mpglTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
548 // Border texels should not be used with CLAMP_TO_EDGE
549 // We set a sane default anyway.
550 mpglTexParameterfv(target
, GL_TEXTURE_BORDER_COLOR
, border
);
555 * \brief skips whitespace and comments
556 * \param f file to read from
558 static void ppm_skip(FILE *f
) {
566 } while (c
!= EOF
&& (isspace(c
) || comment
));
571 #define MAXDIM (16 * 1024)
574 * \brief creates a texture from a PPM file
575 * \param target texture taget, usually GL_TEXTURE_2D
576 * \param fmt internal texture format, 0 for default
577 * \param filter filter used for scaling, e.g. GL_LINEAR
578 * \param f file to read PPM from
579 * \param width [out] width of texture
580 * \param height [out] height of texture
581 * \param maxval [out] maxval value from PPM file
582 * \return 0 on error, 1 otherwise
585 int glCreatePPMTex(GLenum target
, GLenum fmt
, GLint filter
,
586 FILE *f
, int *width
, int *height
, int *maxval
) {
587 unsigned w
, h
, m
, val
, bpp
;
591 if (fgetc(f
) != 'P' || fgetc(f
) != '6')
594 if (fscanf(f
, "%u", &w
) != 1)
597 if (fscanf(f
, "%u", &h
) != 1)
600 if (fscanf(f
, "%u", &m
) != 1)
605 if (w
> MAXDIM
|| h
> MAXDIM
)
607 bpp
= (m
> 255) ? 6 : 3;
608 data
= malloc(w
* h
* bpp
);
609 if (fread(data
, w
* bpp
, h
, f
) != h
)
612 fmt
= (m
> 255) ? hqtexfmt
: 3;
613 if (fmt
== GL_FLOAT_RGB32_NV
&& target
!= GL_TEXTURE_RECTANGLE
)
616 type
= m
> 255 ? GL_UNSIGNED_SHORT
: GL_UNSIGNED_BYTE
;
617 glCreateClearTex(target
, fmt
, GL_RGB
, type
, filter
, w
, h
, 0);
618 glUploadTex(target
, GL_RGB
, type
,
619 data
, w
* bpp
, 0, 0, w
, h
, 0);
621 if (width
) *width
= w
;
622 if (height
) *height
= h
;
623 if (maxval
) *maxval
= m
;
628 * \brief return the number of bytes per pixel for the given format
629 * \param format OpenGL format
630 * \param type OpenGL type
631 * \return bytes per pixel
634 * Does not handle all possible variants, just those used by MPlayer
636 int glFmt2bpp(GLenum format
, GLenum type
) {
637 int component_size
= 0;
639 case GL_UNSIGNED_BYTE_3_3_2
:
640 case GL_UNSIGNED_BYTE_2_3_3_REV
:
642 case GL_UNSIGNED_SHORT_5_5_5_1
:
643 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
644 case GL_UNSIGNED_SHORT_5_6_5
:
645 case GL_UNSIGNED_SHORT_5_6_5_REV
:
647 case GL_UNSIGNED_BYTE
:
650 case GL_UNSIGNED_SHORT
:
657 return component_size
;
662 return 3 * component_size
;
665 return 4 * component_size
;
671 * \brief upload a texture, handling things like stride and slices
672 * \param target texture target, usually GL_TEXTURE_2D
673 * \param format OpenGL format of data
674 * \param type OpenGL type of data
675 * \param dataptr data to upload
676 * \param stride data stride
677 * \param x x offset in texture
678 * \param y y offset in texture
679 * \param w width of the texture part to upload
680 * \param h height of the texture part to upload
681 * \param slice height of an upload slice, 0 for all at once
684 void glUploadTex(GLenum target
, GLenum format
, GLenum type
,
685 const void *dataptr
, int stride
,
686 int x
, int y
, int w
, int h
, int slice
) {
687 const uint8_t *data
= dataptr
;
689 if (w
<= 0 || h
<= 0) return;
693 data
+= (h
- 1) * stride
;
696 // this is not always correct, but should work for MPlayer
697 glAdjustAlignment(stride
);
698 mpglPixelStorei(GL_UNPACK_ROW_LENGTH
, stride
/ glFmt2bpp(format
, type
));
699 for (; y
+ slice
<= y_max
; y
+= slice
) {
700 mpglTexSubImage2D(target
, 0, x
, y
, w
, slice
, format
, type
, data
);
701 data
+= stride
* slice
;
704 mpglTexSubImage2D(target
, 0, x
, y
, w
, y_max
- y
, format
, type
, data
);
707 static void fillUVcoeff(GLfloat
*ucoef
, GLfloat
*vcoef
,
708 float uvcos
, float uvsin
) {
710 ucoef
[0] = 0 * uvcos
+ 1.403 * uvsin
;
711 vcoef
[0] = 0 * uvsin
+ 1.403 * uvcos
;
712 ucoef
[1] = -0.344 * uvcos
+ -0.714 * uvsin
;
713 vcoef
[1] = -0.344 * uvsin
+ -0.714 * uvcos
;
714 ucoef
[2] = 1.770 * uvcos
+ 0 * uvsin
;
715 vcoef
[2] = 1.770 * uvsin
+ 0 * uvcos
;
718 // Coefficients (probably) must be in [0, 1] range, whereas they originally
719 // are in [-2, 2] range, so here comes the trick:
720 // First put them in the [-0.5, 0.5] range, then add 0.5.
721 // This can be undone with the HALF_BIAS and SCALE_BY_FOUR arguments
722 // for CombinerInput and CombinerOutput (or the respective ATI variants)
723 for (i
= 0; i
< 4; i
++) {
724 ucoef
[i
] = ucoef
[i
] * 0.25 + 0.5;
725 vcoef
[i
] = vcoef
[i
] * 0.25 + 0.5;
730 * \brief Setup register combiners for YUV to RGB conversion.
731 * \param uvcos used for saturation and hue adjustment
732 * \param uvsin used for saturation and hue adjustment
734 static void glSetupYUVCombiners(float uvcos
, float uvsin
) {
738 if (!mpglCombinerInput
|| !mpglCombinerOutput
||
739 !mpglCombinerParameterfv
|| !mpglCombinerParameteri
) {
740 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Combiner functions missing!\n");
743 mpglGetIntegerv(GL_MAX_GENERAL_COMBINERS_NV
, &i
);
745 mp_msg(MSGT_VO
, MSGL_ERR
,
746 "[gl] 2 general combiners needed for YUV combiner support (found %i)\n", i
);
747 mpglGetIntegerv(GL_MAX_TEXTURE_UNITS
, &i
);
749 mp_msg(MSGT_VO
, MSGL_ERR
,
750 "[gl] 3 texture units needed for YUV combiner support (found %i)\n", i
);
751 fillUVcoeff(ucoef
, vcoef
, uvcos
, uvsin
);
752 mpglCombinerParameterfv(GL_CONSTANT_COLOR0_NV
, ucoef
);
753 mpglCombinerParameterfv(GL_CONSTANT_COLOR1_NV
, vcoef
);
755 // UV first, like this green component cannot overflow
756 mpglCombinerInput(GL_COMBINER0_NV
, GL_RGB
, GL_VARIABLE_A_NV
,
757 GL_TEXTURE1
, GL_HALF_BIAS_NORMAL_NV
, GL_RGB
);
758 mpglCombinerInput(GL_COMBINER0_NV
, GL_RGB
, GL_VARIABLE_B_NV
,
759 GL_CONSTANT_COLOR0_NV
, GL_HALF_BIAS_NORMAL_NV
, GL_RGB
);
760 mpglCombinerInput(GL_COMBINER0_NV
, GL_RGB
, GL_VARIABLE_C_NV
,
761 GL_TEXTURE2
, GL_HALF_BIAS_NORMAL_NV
, GL_RGB
);
762 mpglCombinerInput(GL_COMBINER0_NV
, GL_RGB
, GL_VARIABLE_D_NV
,
763 GL_CONSTANT_COLOR1_NV
, GL_HALF_BIAS_NORMAL_NV
, GL_RGB
);
764 mpglCombinerOutput(GL_COMBINER0_NV
, GL_RGB
, GL_DISCARD_NV
, GL_DISCARD_NV
,
765 GL_SPARE0_NV
, GL_SCALE_BY_FOUR_NV
, GL_NONE
, GL_FALSE
,
769 mpglCombinerInput(GL_COMBINER1_NV
, GL_RGB
, GL_VARIABLE_A_NV
, GL_SPARE0_NV
,
770 GL_SIGNED_IDENTITY_NV
, GL_RGB
);
771 mpglCombinerInput(GL_COMBINER1_NV
, GL_RGB
, GL_VARIABLE_B_NV
, GL_ZERO
,
772 GL_UNSIGNED_INVERT_NV
, GL_RGB
);
773 mpglCombinerInput(GL_COMBINER1_NV
, GL_RGB
, GL_VARIABLE_C_NV
,
774 GL_TEXTURE0
, GL_SIGNED_IDENTITY_NV
, GL_RGB
);
775 mpglCombinerInput(GL_COMBINER1_NV
, GL_RGB
, GL_VARIABLE_D_NV
, GL_ZERO
,
776 GL_UNSIGNED_INVERT_NV
, GL_RGB
);
777 mpglCombinerOutput(GL_COMBINER1_NV
, GL_RGB
, GL_DISCARD_NV
, GL_DISCARD_NV
,
778 GL_SPARE0_NV
, GL_NONE
, GL_NONE
, GL_FALSE
,
781 // leave final combiner stage in default mode
782 mpglCombinerParameteri(GL_NUM_GENERAL_COMBINERS_NV
, 2);
786 * \brief Setup ATI version of register combiners for YUV to RGB conversion.
787 * \param csp_params parameters used for colorspace conversion
788 * \param text if set use the GL_ATI_text_fragment_shader API as
791 static void glSetupYUVFragmentATI(struct mp_csp_params
*csp_params
,
796 mpglGetIntegerv (GL_MAX_TEXTURE_UNITS
, &i
);
798 mp_msg(MSGT_VO
, MSGL_ERR
,
799 "[gl] 3 texture units needed for YUV combiner (ATI) support (found %i)\n", i
);
801 mp_get_yuv2rgb_coeffs(csp_params
, yuv2rgb
);
802 for (i
= 0; i
< 3; i
++) {
804 yuv2rgb
[i
][3] -= -0.5 * (yuv2rgb
[i
][1] + yuv2rgb
[i
][2]);
805 for (j
= 0; j
< 4; j
++) {
806 yuv2rgb
[i
][j
] *= 0.125;
807 yuv2rgb
[i
][j
] += 0.5;
808 if (yuv2rgb
[i
][j
] > 1)
810 if (yuv2rgb
[i
][j
] < 0)
815 GLfloat c0
[4] = {yuv2rgb
[0][0], yuv2rgb
[1][0], yuv2rgb
[2][0]};
816 GLfloat c1
[4] = {yuv2rgb
[0][1], yuv2rgb
[1][1], yuv2rgb
[2][1]};
817 GLfloat c2
[4] = {yuv2rgb
[0][2], yuv2rgb
[1][2], yuv2rgb
[2][2]};
818 GLfloat c3
[4] = {yuv2rgb
[0][3], yuv2rgb
[1][3], yuv2rgb
[2][3]};
819 if (!mpglBeginFragmentShader
|| !mpglEndFragmentShader
||
820 !mpglSetFragmentShaderConstant
|| !mpglSampleMap
||
821 !mpglColorFragmentOp2
|| !mpglColorFragmentOp3
) {
822 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Combiner (ATI) functions missing!\n");
825 mpglGetIntegerv(GL_NUM_FRAGMENT_REGISTERS_ATI
, &i
);
827 mp_msg(MSGT_VO
, MSGL_ERR
,
828 "[gl] 3 registers needed for YUV combiner (ATI) support (found %i)\n", i
);
829 mpglBeginFragmentShader();
830 mpglSetFragmentShaderConstant(GL_CON_0_ATI
, c0
);
831 mpglSetFragmentShaderConstant(GL_CON_1_ATI
, c1
);
832 mpglSetFragmentShaderConstant(GL_CON_2_ATI
, c2
);
833 mpglSetFragmentShaderConstant(GL_CON_3_ATI
, c3
);
834 mpglSampleMap(GL_REG_0_ATI
, GL_TEXTURE0
, GL_SWIZZLE_STR_ATI
);
835 mpglSampleMap(GL_REG_1_ATI
, GL_TEXTURE1
, GL_SWIZZLE_STR_ATI
);
836 mpglSampleMap(GL_REG_2_ATI
, GL_TEXTURE2
, GL_SWIZZLE_STR_ATI
);
837 mpglColorFragmentOp2(GL_MUL_ATI
, GL_REG_1_ATI
, GL_NONE
, GL_NONE
,
838 GL_REG_1_ATI
, GL_NONE
, GL_BIAS_BIT_ATI
,
839 GL_CON_1_ATI
, GL_NONE
, GL_BIAS_BIT_ATI
);
840 mpglColorFragmentOp3(GL_MAD_ATI
, GL_REG_2_ATI
, GL_NONE
, GL_NONE
,
841 GL_REG_2_ATI
, GL_NONE
, GL_BIAS_BIT_ATI
,
842 GL_CON_2_ATI
, GL_NONE
, GL_BIAS_BIT_ATI
,
843 GL_REG_1_ATI
, GL_NONE
, GL_NONE
);
844 mpglColorFragmentOp3(GL_MAD_ATI
, GL_REG_0_ATI
, GL_NONE
, GL_NONE
,
845 GL_REG_0_ATI
, GL_NONE
, GL_NONE
,
846 GL_CON_0_ATI
, GL_NONE
, GL_BIAS_BIT_ATI
,
847 GL_REG_2_ATI
, GL_NONE
, GL_NONE
);
848 mpglColorFragmentOp2(GL_ADD_ATI
, GL_REG_0_ATI
, GL_NONE
, GL_8X_BIT_ATI
,
849 GL_REG_0_ATI
, GL_NONE
, GL_NONE
,
850 GL_CON_3_ATI
, GL_NONE
, GL_BIAS_BIT_ATI
);
851 mpglEndFragmentShader();
853 static const char template[] =
856 " CONSTANT c0 = {%e, %e, %e};\n"
857 " CONSTANT c1 = {%e, %e, %e};\n"
858 " CONSTANT c2 = {%e, %e, %e};\n"
859 " CONSTANT c3 = {%e, %e, %e};\n"
862 " SampleMap r0, t0.str;\n"
863 " SampleMap r1, t1.str;\n"
864 " SampleMap r2, t2.str;\n"
865 " MUL r1.rgb, r1.bias, c1.bias;\n"
866 " MAD r2.rgb, r2.bias, c2.bias, r1;\n"
867 " MAD r0.rgb, r0, c0.bias, r2;\n"
868 " ADD r0.rgb.8x, r0, c3.bias;\n"
871 snprintf(buffer
, sizeof(buffer
), template,
872 yuv2rgb
[0][0], yuv2rgb
[1][0], yuv2rgb
[2][0],
873 yuv2rgb
[0][1], yuv2rgb
[1][1], yuv2rgb
[2][1],
874 yuv2rgb
[0][2], yuv2rgb
[1][2], yuv2rgb
[2][2],
875 yuv2rgb
[0][3], yuv2rgb
[1][3], yuv2rgb
[2][3]);
876 mp_msg(MSGT_VO
, MSGL_DBG2
, "[gl] generated fragment program:\n%s\n", buffer
);
877 loadGPUProgram(GL_TEXT_FRAGMENT_SHADER_ATI
, buffer
);
882 * \brief helper function for gen_spline_lookup_tex
883 * \param x subpixel-position ((0,1) range) to calculate weights for
884 * \param dst where to store transformed weights, must provide space for 4 GLfloats
886 * calculates the weights and stores them after appropriate transformation
887 * for the scaler fragment program.
889 static void store_weights(float x
, GLfloat
*dst
) {
890 float w0
= (((-1 * x
+ 3) * x
- 3) * x
+ 1) / 6;
891 float w1
= ((( 3 * x
- 6) * x
+ 0) * x
+ 4) / 6;
892 float w2
= (((-3 * x
+ 3) * x
+ 3) * x
+ 1) / 6;
893 float w3
= ((( 1 * x
+ 0) * x
+ 0) * x
+ 0) / 6;
894 *dst
++ = 1 + x
- w1
/ (w0
+ w1
);
895 *dst
++ = 1 - x
+ w3
/ (w2
+ w3
);
900 //! to avoid artefacts this should be rather large
901 #define LOOKUP_BSPLINE_RES (2 * 1024)
903 * \brief creates the 1D lookup texture needed for fast higher-order filtering
904 * \param unit texture unit to attach texture to
906 static void gen_spline_lookup_tex(GLenum unit
) {
907 GLfloat
*tex
= calloc(4 * LOOKUP_BSPLINE_RES
, sizeof(*tex
));
910 for (i
= 0; i
< LOOKUP_BSPLINE_RES
; i
++) {
911 float x
= (float)(i
+ 0.5) / LOOKUP_BSPLINE_RES
;
912 store_weights(x
, tp
);
915 store_weights(0, tex
);
916 store_weights(1, &tex
[4 * (LOOKUP_BSPLINE_RES
- 1)]);
917 mpglActiveTexture(unit
);
918 mpglTexImage1D(GL_TEXTURE_1D
, 0, GL_RGBA16
, LOOKUP_BSPLINE_RES
, 0, GL_RGBA
, GL_FLOAT
, tex
);
919 mpglTexParameterf(GL_TEXTURE_1D
, GL_TEXTURE_PRIORITY
, 1.0);
920 mpglTexParameteri(GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
921 mpglTexParameteri(GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
922 mpglTexParameteri(GL_TEXTURE_1D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
923 mpglActiveTexture(GL_TEXTURE0
);
927 static const char *bilin_filt_template
=
928 "TEX yuv.%c, fragment.texcoord[%c], texture[%c], %s;\n";
930 #define BICUB_FILT_MAIN(textype) \
931 /* first y-interpolation */ \
932 "ADD coord, fragment.texcoord[%c].xyxy, cdelta.xyxw;\n" \
933 "ADD coord2, fragment.texcoord[%c].xyxy, cdelta.zyzw;\n" \
934 "TEX a.r, coord.xyxy, texture[%c], "textype";\n" \
935 "TEX a.g, coord.zwzw, texture[%c], "textype";\n" \
936 /* second y-interpolation */ \
937 "TEX b.r, coord2.xyxy, texture[%c], "textype";\n" \
938 "TEX b.g, coord2.zwzw, texture[%c], "textype";\n" \
939 "LRP a.b, parmy.b, a.rrrr, a.gggg;\n" \
940 "LRP a.a, parmy.b, b.rrrr, b.gggg;\n" \
941 /* x-interpolation */ \
942 "LRP yuv.%c, parmx.b, a.bbbb, a.aaaa;\n"
944 static const char *bicub_filt_template_2D
=
945 "MAD coord.xy, fragment.texcoord[%c], {%e, %e}, {0.5, 0.5};\n"
946 "TEX parmx, coord.x, texture[%c], 1D;\n"
947 "MUL cdelta.xz, parmx.rrgg, {-%e, 0, %e, 0};\n"
948 "TEX parmy, coord.y, texture[%c], 1D;\n"
949 "MUL cdelta.yw, parmy.rrgg, {0, -%e, 0, %e};\n"
950 BICUB_FILT_MAIN("2D");
952 static const char *bicub_filt_template_RECT
=
953 "ADD coord, fragment.texcoord[%c], {0.5, 0.5};\n"
954 "TEX parmx, coord.x, texture[%c], 1D;\n"
955 "MUL cdelta.xz, parmx.rrgg, {-1, 0, 1, 0};\n"
956 "TEX parmy, coord.y, texture[%c], 1D;\n"
957 "MUL cdelta.yw, parmy.rrgg, {0, -1, 0, 1};\n"
958 BICUB_FILT_MAIN("RECT");
960 #define CALCWEIGHTS(t, s) \
961 "MAD "t", {-0.5, 0.1666, 0.3333, -0.3333}, "s", {1, 0, -0.5, 0.5};\n" \
962 "MAD "t", "t", "s", {0, 0, -0.5, 0.5};\n" \
963 "MAD "t", "t", "s", {-0.6666, 0, 0.8333, 0.1666};\n" \
964 "RCP a.x, "t".z;\n" \
965 "RCP a.y, "t".w;\n" \
966 "MAD "t".xy, "t".xyxy, a.xyxy, {1, 1, 0, 0};\n" \
967 "ADD "t".x, "t".xxxx, "s";\n" \
968 "SUB "t".y, "t".yyyy, "s";\n"
970 static const char *bicub_notex_filt_template_2D
=
971 "MAD coord.xy, fragment.texcoord[%c], {%e, %e}, {0.5, 0.5};\n"
972 "FRC coord.xy, coord.xyxy;\n"
973 CALCWEIGHTS("parmx", "coord.xxxx")
974 "MUL cdelta.xz, parmx.rrgg, {-%e, 0, %e, 0};\n"
975 CALCWEIGHTS("parmy", "coord.yyyy")
976 "MUL cdelta.yw, parmy.rrgg, {0, -%e, 0, %e};\n"
977 BICUB_FILT_MAIN("2D");
979 static const char *bicub_notex_filt_template_RECT
=
980 "ADD coord, fragment.texcoord[%c], {0.5, 0.5};\n"
981 "FRC coord.xy, coord.xyxy;\n"
982 CALCWEIGHTS("parmx", "coord.xxxx")
983 "MUL cdelta.xz, parmx.rrgg, {-1, 0, 1, 0};\n"
984 CALCWEIGHTS("parmy", "coord.yyyy")
985 "MUL cdelta.yw, parmy.rrgg, {0, -1, 0, 1};\n"
986 BICUB_FILT_MAIN("RECT");
988 #define BICUB_X_FILT_MAIN(textype) \
989 "ADD coord.xy, fragment.texcoord[%c].xyxy, cdelta.xyxy;\n" \
990 "ADD coord2.xy, fragment.texcoord[%c].xyxy, cdelta.zyzy;\n" \
991 "TEX a.r, coord, texture[%c], "textype";\n" \
992 "TEX b.r, coord2, texture[%c], "textype";\n" \
993 /* x-interpolation */ \
994 "LRP yuv.%c, parmx.b, a.rrrr, b.rrrr;\n"
996 static const char *bicub_x_filt_template_2D
=
997 "MAD coord.x, fragment.texcoord[%c], {%e}, {0.5};\n"
998 "TEX parmx, coord, texture[%c], 1D;\n"
999 "MUL cdelta.xyz, parmx.rrgg, {-%e, 0, %e};\n"
1000 BICUB_X_FILT_MAIN("2D");
1002 static const char *bicub_x_filt_template_RECT
=
1003 "ADD coord.x, fragment.texcoord[%c], {0.5};\n"
1004 "TEX parmx, coord, texture[%c], 1D;\n"
1005 "MUL cdelta.xyz, parmx.rrgg, {-1, 0, 1};\n"
1006 BICUB_X_FILT_MAIN("RECT");
1008 static const char *unsharp_filt_template
=
1009 "PARAM dcoord%c = {%e, %e, %e, %e};\n"
1010 "ADD coord, fragment.texcoord[%c].xyxy, dcoord%c;\n"
1011 "SUB coord2, fragment.texcoord[%c].xyxy, dcoord%c;\n"
1012 "TEX a.r, fragment.texcoord[%c], texture[%c], %s;\n"
1013 "TEX b.r, coord.xyxy, texture[%c], %s;\n"
1014 "TEX b.g, coord.zwzw, texture[%c], %s;\n"
1015 "ADD b.r, b.r, b.g;\n"
1016 "TEX b.b, coord2.xyxy, texture[%c], %s;\n"
1017 "TEX b.g, coord2.zwzw, texture[%c], %s;\n"
1018 "DP3 b, b, {0.25, 0.25, 0.25};\n"
1019 "SUB b.r, a.r, b.r;\n"
1020 "MAD yuv.%c, b.r, {%e}, a.r;\n";
1022 static const char *unsharp_filt_template2
=
1023 "PARAM dcoord%c = {%e, %e, %e, %e};\n"
1024 "PARAM dcoord2%c = {%e, 0, 0, %e};\n"
1025 "ADD coord, fragment.texcoord[%c].xyxy, dcoord%c;\n"
1026 "SUB coord2, fragment.texcoord[%c].xyxy, dcoord%c;\n"
1027 "TEX a.r, fragment.texcoord[%c], texture[%c], %s;\n"
1028 "TEX b.r, coord.xyxy, texture[%c], %s;\n"
1029 "TEX b.g, coord.zwzw, texture[%c], %s;\n"
1030 "ADD b.r, b.r, b.g;\n"
1031 "TEX b.b, coord2.xyxy, texture[%c], %s;\n"
1032 "TEX b.g, coord2.zwzw, texture[%c], %s;\n"
1033 "ADD b.r, b.r, b.b;\n"
1034 "ADD b.a, b.r, b.g;\n"
1035 "ADD coord, fragment.texcoord[%c].xyxy, dcoord2%c;\n"
1036 "SUB coord2, fragment.texcoord[%c].xyxy, dcoord2%c;\n"
1037 "TEX b.r, coord.xyxy, texture[%c], %s;\n"
1038 "TEX b.g, coord.zwzw, texture[%c], %s;\n"
1039 "ADD b.r, b.r, b.g;\n"
1040 "TEX b.b, coord2.xyxy, texture[%c], %s;\n"
1041 "TEX b.g, coord2.zwzw, texture[%c], %s;\n"
1042 "DP4 b.r, b, {-0.1171875, -0.1171875, -0.1171875, -0.09765625};\n"
1043 "MAD b.r, a.r, {0.859375}, b.r;\n"
1044 "MAD yuv.%c, b.r, {%e}, a.r;\n";
1046 static const char *yuv_prog_template
=
1047 "PARAM ycoef = {%e, %e, %e};\n"
1048 "PARAM ucoef = {%e, %e, %e};\n"
1049 "PARAM vcoef = {%e, %e, %e};\n"
1050 "PARAM offsets = {%e, %e, %e};\n"
1052 "MAD res.rgb, yuv.rrrr, ycoef, offsets;\n"
1053 "MAD res.rgb, yuv.gggg, ucoef, res;\n"
1054 "MAD result.color.rgb, yuv.bbbb, vcoef, res;\n"
1057 static const char *yuv_pow_prog_template
=
1058 "PARAM ycoef = {%e, %e, %e};\n"
1059 "PARAM ucoef = {%e, %e, %e};\n"
1060 "PARAM vcoef = {%e, %e, %e};\n"
1061 "PARAM offsets = {%e, %e, %e};\n"
1062 "PARAM gamma = {%e, %e, %e};\n"
1064 "MAD res.rgb, yuv.rrrr, ycoef, offsets;\n"
1065 "MAD res.rgb, yuv.gggg, ucoef, res;\n"
1066 "MAD_SAT res.rgb, yuv.bbbb, vcoef, res;\n"
1067 "POW result.color.r, res.r, gamma.r;\n"
1068 "POW result.color.g, res.g, gamma.g;\n"
1069 "POW result.color.b, res.b, gamma.b;\n"
1072 static const char *yuv_lookup_prog_template
=
1073 "PARAM ycoef = {%e, %e, %e, 0};\n"
1074 "PARAM ucoef = {%e, %e, %e, 0};\n"
1075 "PARAM vcoef = {%e, %e, %e, 0};\n"
1076 "PARAM offsets = {%e, %e, %e, 0.125};\n"
1078 "MAD res, yuv.rrrr, ycoef, offsets;\n"
1079 "MAD res.rgb, yuv.gggg, ucoef, res;\n"
1080 "MAD res.rgb, yuv.bbbb, vcoef, res;\n"
1081 "TEX result.color.r, res.raaa, texture[%c], 2D;\n"
1082 "ADD res.a, res.a, 0.25;\n"
1083 "TEX result.color.g, res.gaaa, texture[%c], 2D;\n"
1084 "ADD res.a, res.a, 0.25;\n"
1085 "TEX result.color.b, res.baaa, texture[%c], 2D;\n"
1088 static const char *yuv_lookup3d_prog_template
=
1089 "TEX result.color, yuv, texture[%c], 3D;\n"
1093 * \brief creates and initializes helper textures needed for scaling texture read
1094 * \param scaler scaler type to create texture for
1095 * \param texu contains next free texture unit number
1096 * \param texs texture unit ids for the scaler are stored in this array
1098 static void create_scaler_textures(int scaler
, int *texu
, char *texs
) {
1100 case YUV_SCALER_BILIN
:
1101 case YUV_SCALER_BICUB_NOTEX
:
1102 case YUV_SCALER_UNSHARP
:
1103 case YUV_SCALER_UNSHARP2
:
1105 case YUV_SCALER_BICUB
:
1106 case YUV_SCALER_BICUB_X
:
1107 texs
[0] = (*texu
)++;
1108 gen_spline_lookup_tex(GL_TEXTURE0
+ texs
[0]);
1112 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] unknown scaler type %i\n", scaler
);
1116 //! resolution of texture for gamma lookup table
1117 #define LOOKUP_RES 512
1118 //! resolution for 3D yuv->rgb conversion lookup table
1119 #define LOOKUP_3DRES 32
1121 * \brief creates and initializes helper textures needed for yuv conversion
1122 * \param params struct containing parameters like brightness, gamma, ...
1123 * \param texu contains next free texture unit number
1124 * \param texs texture unit ids for the conversion are stored in this array
1126 static void create_conv_textures(gl_conversion_params_t
*params
, int *texu
, char *texs
) {
1127 unsigned char *lookup_data
= NULL
;
1128 int conv
= YUV_CONVERSION(params
->type
);
1130 case YUV_CONVERSION_FRAGMENT
:
1131 case YUV_CONVERSION_FRAGMENT_POW
:
1133 case YUV_CONVERSION_FRAGMENT_LOOKUP
:
1134 texs
[0] = (*texu
)++;
1135 mpglActiveTexture(GL_TEXTURE0
+ texs
[0]);
1136 lookup_data
= malloc(4 * LOOKUP_RES
);
1137 mp_gen_gamma_map(lookup_data
, LOOKUP_RES
, params
->csp_params
.rgamma
);
1138 mp_gen_gamma_map(&lookup_data
[LOOKUP_RES
], LOOKUP_RES
, params
->csp_params
.ggamma
);
1139 mp_gen_gamma_map(&lookup_data
[2 * LOOKUP_RES
], LOOKUP_RES
, params
->csp_params
.bgamma
);
1140 glCreateClearTex(GL_TEXTURE_2D
, GL_LUMINANCE8
, GL_LUMINANCE
, GL_UNSIGNED_BYTE
, GL_LINEAR
,
1142 glUploadTex(GL_TEXTURE_2D
, GL_LUMINANCE
, GL_UNSIGNED_BYTE
, lookup_data
,
1143 LOOKUP_RES
, 0, 0, LOOKUP_RES
, 4, 0);
1144 mpglActiveTexture(GL_TEXTURE0
);
1147 case YUV_CONVERSION_FRAGMENT_LOOKUP3D
:
1149 int sz
= LOOKUP_3DRES
+ 2; // texture size including borders
1150 if (!mpglTexImage3D
) {
1151 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] Missing 3D texture function!\n");
1154 texs
[0] = (*texu
)++;
1155 mpglActiveTexture(GL_TEXTURE0
+ texs
[0]);
1156 lookup_data
= malloc(3 * sz
* sz
* sz
);
1157 mp_gen_yuv2rgb_map(¶ms
->csp_params
, lookup_data
, LOOKUP_3DRES
);
1158 glAdjustAlignment(sz
);
1159 mpglPixelStorei(GL_UNPACK_ROW_LENGTH
, 0);
1160 mpglTexImage3D(GL_TEXTURE_3D
, 0, 3, sz
, sz
, sz
, 1,
1161 GL_RGB
, GL_UNSIGNED_BYTE
, lookup_data
);
1162 mpglTexParameterf(GL_TEXTURE_3D
, GL_TEXTURE_PRIORITY
, 1.0);
1163 mpglTexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
1164 mpglTexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
1165 mpglTexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_WRAP_S
, GL_CLAMP
);
1166 mpglTexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_WRAP_T
, GL_CLAMP
);
1167 mpglTexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_WRAP_R
, GL_CLAMP
);
1168 mpglActiveTexture(GL_TEXTURE0
);
1173 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] unknown conversion type %i\n", conv
);
1180 * \brief adds a scaling texture read at the current fragment program position
1181 * \param scaler type of scaler to insert
1182 * \param prog_pos current position in fragment program
1183 * \param remain how many bytes remain in the buffer given by prog_pos
1184 * \param texs array containing the texture unit identifiers for this scaler
1185 * \param in_tex texture unit the scaler should read from
1186 * \param out_comp component of the yuv variable the scaler stores the result in
1187 * \param rect if rectangular (pixel) adressing should be used for in_tex
1188 * \param texw width of the in_tex texture
1189 * \param texh height of the in_tex texture
1190 * \param strength strength of filter effect if the scaler does some kind of filtering
1192 static void add_scaler(int scaler
, char **prog_pos
, int *remain
, char *texs
,
1193 char in_tex
, char out_comp
, int rect
, int texw
, int texh
,
1195 const char *ttype
= rect
? "RECT" : "2D";
1196 const float ptw
= rect
? 1.0 : 1.0 / texw
;
1197 const float pth
= rect
? 1.0 : 1.0 / texh
;
1199 case YUV_SCALER_BILIN
:
1200 snprintf(*prog_pos
, *remain
, bilin_filt_template
, out_comp
, in_tex
,
1203 case YUV_SCALER_BICUB
:
1205 snprintf(*prog_pos
, *remain
, bicub_filt_template_RECT
,
1206 in_tex
, texs
[0], texs
[0],
1207 in_tex
, in_tex
, in_tex
, in_tex
, in_tex
, in_tex
, out_comp
);
1209 snprintf(*prog_pos
, *remain
, bicub_filt_template_2D
,
1210 in_tex
, (float)texw
, (float)texh
,
1211 texs
[0], ptw
, ptw
, texs
[0], pth
, pth
,
1212 in_tex
, in_tex
, in_tex
, in_tex
, in_tex
, in_tex
, out_comp
);
1214 case YUV_SCALER_BICUB_X
:
1216 snprintf(*prog_pos
, *remain
, bicub_x_filt_template_RECT
,
1218 in_tex
, in_tex
, in_tex
, in_tex
, out_comp
);
1220 snprintf(*prog_pos
, *remain
, bicub_x_filt_template_2D
,
1221 in_tex
, (float)texw
,
1223 in_tex
, in_tex
, in_tex
, in_tex
, out_comp
);
1225 case YUV_SCALER_BICUB_NOTEX
:
1227 snprintf(*prog_pos
, *remain
, bicub_notex_filt_template_RECT
,
1229 in_tex
, in_tex
, in_tex
, in_tex
, in_tex
, in_tex
, out_comp
);
1231 snprintf(*prog_pos
, *remain
, bicub_notex_filt_template_2D
,
1232 in_tex
, (float)texw
, (float)texh
, ptw
, ptw
, pth
, pth
,
1233 in_tex
, in_tex
, in_tex
, in_tex
, in_tex
, in_tex
, out_comp
);
1235 case YUV_SCALER_UNSHARP
:
1236 snprintf(*prog_pos
, *remain
, unsharp_filt_template
,
1237 out_comp
, 0.5 * ptw
, 0.5 * pth
, 0.5 * ptw
, -0.5 * pth
,
1238 in_tex
, out_comp
, in_tex
, out_comp
, in_tex
,
1239 in_tex
, ttype
, in_tex
, ttype
, in_tex
, ttype
, in_tex
, ttype
,
1240 in_tex
, ttype
, out_comp
, strength
);
1242 case YUV_SCALER_UNSHARP2
:
1243 snprintf(*prog_pos
, *remain
, unsharp_filt_template2
,
1244 out_comp
, 1.2 * ptw
, 1.2 * pth
, 1.2 * ptw
, -1.2 * pth
,
1245 out_comp
, 1.5 * ptw
, 1.5 * pth
,
1246 in_tex
, out_comp
, in_tex
, out_comp
, in_tex
,
1247 in_tex
, ttype
, in_tex
, ttype
, in_tex
, ttype
, in_tex
, ttype
,
1248 in_tex
, ttype
, in_tex
, out_comp
, in_tex
, out_comp
,
1249 in_tex
, ttype
, in_tex
, ttype
, in_tex
, ttype
,
1250 in_tex
, ttype
, out_comp
, strength
);
1253 *remain
-= strlen(*prog_pos
);
1254 *prog_pos
+= strlen(*prog_pos
);
1257 static const struct {
1262 {"instructions", 0x88A0, 0x88A1},
1263 {"native instructions", 0x88A2, 0x88A3},
1264 {"temporaries", 0x88A4, 0x88A5},
1265 {"native temporaries", 0x88A6, 0x88A7},
1266 {"parameters", 0x88A8, 0x88A9},
1267 {"native parameters", 0x88AA, 0x88AB},
1268 {"attribs", 0x88AC, 0x88AD},
1269 {"native attribs", 0x88AE, 0x88AF},
1270 {"ALU instructions", 0x8805, 0x880B},
1271 {"TEX instructions", 0x8806, 0x880C},
1272 {"TEX indirections", 0x8807, 0x880D},
1273 {"native ALU instructions", 0x8808, 0x880E},
1274 {"native TEX instructions", 0x8809, 0x880F},
1275 {"native TEX indirections", 0x880A, 0x8810},
1280 * \brief load the specified GPU Program
1281 * \param target program target to load into, only GL_FRAGMENT_PROGRAM is tested
1282 * \param prog program string
1283 * \return 1 on success, 0 otherwise
1285 int loadGPUProgram(GLenum target
, char *prog
) {
1287 GLint cur
= 0, max
= 0, err
= 0;
1288 if (!mpglProgramString
) {
1289 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] Missing GPU program function\n");
1292 mpglProgramString(target
, GL_PROGRAM_FORMAT_ASCII
, strlen(prog
), prog
);
1293 mpglGetIntegerv(GL_PROGRAM_ERROR_POSITION
, &err
);
1295 mp_msg(MSGT_VO
, MSGL_ERR
,
1296 "[gl] Error compiling fragment program, make sure your card supports\n"
1297 "[gl] GL_ARB_fragment_program (use glxinfo to check).\n"
1298 "[gl] Error message:\n %s at %.10s\n",
1299 mpglGetString(GL_PROGRAM_ERROR_STRING
), &prog
[err
]);
1302 if (!mpglGetProgramiv
|| !mp_msg_test(MSGT_VO
, MSGL_DBG2
))
1304 mp_msg(MSGT_VO
, MSGL_V
, "[gl] Program statistics:\n");
1305 for (i
= 0; progstats
[i
].name
; i
++) {
1306 mpglGetProgramiv(target
, progstats
[i
].cur
, &cur
);
1307 mpglGetProgramiv(target
, progstats
[i
].max
, &max
);
1308 mp_msg(MSGT_VO
, MSGL_V
, "[gl] %s: %i/%i\n", progstats
[i
].name
, cur
, max
);
1313 #define MAX_PROGSZ (1024*1024)
1316 * \brief setup a fragment program that will do YUV->RGB conversion
1317 * \param parms struct containing parameters like conversion and scaler type,
1320 static void glSetupYUVFragprog(gl_conversion_params_t
*params
) {
1321 int type
= params
->type
;
1322 int texw
= params
->texw
;
1323 int texh
= params
->texh
;
1324 int rect
= params
->target
== GL_TEXTURE_RECTANGLE
;
1325 static const char prog_hdr
[] =
1327 "OPTION ARB_precision_hint_fastest;\n"
1328 // all scaler variables must go here so they aren't defined
1329 // multiple times when the same scaler is used more than once
1330 "TEMP coord, coord2, cdelta, parmx, parmy, a, b, yuv;\n";
1332 char *yuv_prog
, *prog_pos
;
1334 char lum_scale_texs
[1];
1335 char chrom_scale_texs
[1];
1338 // this is the conversion matrix, with y, u, v factors
1339 // for red, green, blue and the constant offsets
1340 float yuv2rgb
[3][4];
1341 create_conv_textures(params
, &cur_texu
, conv_texs
);
1342 create_scaler_textures(YUV_LUM_SCALER(type
), &cur_texu
, lum_scale_texs
);
1343 if (YUV_CHROM_SCALER(type
) == YUV_LUM_SCALER(type
))
1344 memcpy(chrom_scale_texs
, lum_scale_texs
, sizeof(chrom_scale_texs
));
1346 create_scaler_textures(YUV_CHROM_SCALER(type
), &cur_texu
, chrom_scale_texs
);
1347 mpglGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS
, &i
);
1349 mp_msg(MSGT_VO
, MSGL_ERR
,
1350 "[gl] %i texture units needed for this type of YUV fragment support (found %i)\n",
1352 if (!mpglProgramString
) {
1353 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] ProgramString function missing!\n");
1356 yuv_prog
= malloc(MAX_PROGSZ
);
1357 strcpy(yuv_prog
, prog_hdr
);
1358 prog_pos
= yuv_prog
+ sizeof(prog_hdr
) - 1;
1359 prog_remain
= MAX_PROGSZ
- sizeof(prog_hdr
);
1360 add_scaler(YUV_LUM_SCALER(type
), &prog_pos
, &prog_remain
, lum_scale_texs
,
1361 '0', 'r', rect
, texw
, texh
, params
->filter_strength
);
1362 add_scaler(YUV_CHROM_SCALER(type
), &prog_pos
, &prog_remain
, chrom_scale_texs
,
1363 '1', 'g', rect
, params
->chrom_texw
, params
->chrom_texh
, params
->filter_strength
);
1364 add_scaler(YUV_CHROM_SCALER(type
), &prog_pos
, &prog_remain
, chrom_scale_texs
,
1365 '2', 'b', rect
, params
->chrom_texw
, params
->chrom_texh
, params
->filter_strength
);
1366 mp_get_yuv2rgb_coeffs(¶ms
->csp_params
, yuv2rgb
);
1367 switch (YUV_CONVERSION(type
)) {
1368 case YUV_CONVERSION_FRAGMENT
:
1369 snprintf(prog_pos
, prog_remain
, yuv_prog_template
,
1370 yuv2rgb
[ROW_R
][COL_Y
], yuv2rgb
[ROW_G
][COL_Y
], yuv2rgb
[ROW_B
][COL_Y
],
1371 yuv2rgb
[ROW_R
][COL_U
], yuv2rgb
[ROW_G
][COL_U
], yuv2rgb
[ROW_B
][COL_U
],
1372 yuv2rgb
[ROW_R
][COL_V
], yuv2rgb
[ROW_G
][COL_V
], yuv2rgb
[ROW_B
][COL_V
],
1373 yuv2rgb
[ROW_R
][COL_C
], yuv2rgb
[ROW_G
][COL_C
], yuv2rgb
[ROW_B
][COL_C
]);
1375 case YUV_CONVERSION_FRAGMENT_POW
:
1376 snprintf(prog_pos
, prog_remain
, yuv_pow_prog_template
,
1377 yuv2rgb
[ROW_R
][COL_Y
], yuv2rgb
[ROW_G
][COL_Y
], yuv2rgb
[ROW_B
][COL_Y
],
1378 yuv2rgb
[ROW_R
][COL_U
], yuv2rgb
[ROW_G
][COL_U
], yuv2rgb
[ROW_B
][COL_U
],
1379 yuv2rgb
[ROW_R
][COL_V
], yuv2rgb
[ROW_G
][COL_V
], yuv2rgb
[ROW_B
][COL_V
],
1380 yuv2rgb
[ROW_R
][COL_C
], yuv2rgb
[ROW_G
][COL_C
], yuv2rgb
[ROW_B
][COL_C
],
1381 (float)1.0 / params
->csp_params
.rgamma
, (float)1.0 / params
->csp_params
.bgamma
, (float)1.0 / params
->csp_params
.bgamma
);
1383 case YUV_CONVERSION_FRAGMENT_LOOKUP
:
1384 snprintf(prog_pos
, prog_remain
, yuv_lookup_prog_template
,
1385 yuv2rgb
[ROW_R
][COL_Y
], yuv2rgb
[ROW_G
][COL_Y
], yuv2rgb
[ROW_B
][COL_Y
],
1386 yuv2rgb
[ROW_R
][COL_U
], yuv2rgb
[ROW_G
][COL_U
], yuv2rgb
[ROW_B
][COL_U
],
1387 yuv2rgb
[ROW_R
][COL_V
], yuv2rgb
[ROW_G
][COL_V
], yuv2rgb
[ROW_B
][COL_V
],
1388 yuv2rgb
[ROW_R
][COL_C
], yuv2rgb
[ROW_G
][COL_C
], yuv2rgb
[ROW_B
][COL_C
],
1389 conv_texs
[0], conv_texs
[0], conv_texs
[0]);
1391 case YUV_CONVERSION_FRAGMENT_LOOKUP3D
:
1392 snprintf(prog_pos
, prog_remain
, yuv_lookup3d_prog_template
, conv_texs
[0]);
1395 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] unknown conversion type %i\n", YUV_CONVERSION(type
));
1398 mp_msg(MSGT_VO
, MSGL_DBG2
, "[gl] generated fragment program:\n%s\n", yuv_prog
);
1399 loadGPUProgram(GL_FRAGMENT_PROGRAM
, yuv_prog
);
1404 * \brief detect the best YUV->RGB conversion method available
1406 int glAutodetectYUVConversion(void) {
1407 const char *extensions
= mpglGetString(GL_EXTENSIONS
);
1408 if (!extensions
|| !mpglMultiTexCoord2f
)
1409 return YUV_CONVERSION_NONE
;
1410 if (strstr(extensions
, "GL_ARB_fragment_program"))
1411 return YUV_CONVERSION_FRAGMENT
;
1412 if (strstr(extensions
, "GL_ATI_text_fragment_shader"))
1413 return YUV_CONVERSION_TEXT_FRAGMENT
;
1414 if (strstr(extensions
, "GL_ATI_fragment_shader"))
1415 return YUV_CONVERSION_COMBINERS_ATI
;
1416 return YUV_CONVERSION_NONE
;
1420 * \brief setup YUV->RGB conversion
1421 * \param parms struct containing parameters like conversion and scaler type,
1423 * \ingroup glconversion
1425 void glSetupYUVConversion(gl_conversion_params_t
*params
) {
1426 float uvcos
= params
->csp_params
.saturation
* cos(params
->csp_params
.hue
);
1427 float uvsin
= params
->csp_params
.saturation
* sin(params
->csp_params
.hue
);
1428 if (params
->chrom_texw
== 0) params
->chrom_texw
= 1;
1429 if (params
->chrom_texh
== 0) params
->chrom_texh
= 1;
1430 switch (YUV_CONVERSION(params
->type
)) {
1431 case YUV_CONVERSION_COMBINERS
:
1432 glSetupYUVCombiners(uvcos
, uvsin
);
1434 case YUV_CONVERSION_COMBINERS_ATI
:
1435 glSetupYUVFragmentATI(¶ms
->csp_params
, 0);
1437 case YUV_CONVERSION_TEXT_FRAGMENT
:
1438 glSetupYUVFragmentATI(¶ms
->csp_params
, 1);
1440 case YUV_CONVERSION_FRAGMENT_LOOKUP
:
1441 case YUV_CONVERSION_FRAGMENT_LOOKUP3D
:
1442 case YUV_CONVERSION_FRAGMENT
:
1443 case YUV_CONVERSION_FRAGMENT_POW
:
1444 glSetupYUVFragprog(params
);
1446 case YUV_CONVERSION_NONE
:
1449 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] unknown conversion type %i\n", YUV_CONVERSION(params
->type
));
1454 * \brief enable the specified YUV conversion
1455 * \param target texture target for Y, U and V textures (e.g. GL_TEXTURE_2D)
1456 * \param type type of YUV conversion
1457 * \ingroup glconversion
1459 void glEnableYUVConversion(GLenum target
, int type
) {
1460 switch (YUV_CONVERSION(type
)) {
1461 case YUV_CONVERSION_COMBINERS
:
1462 mpglActiveTexture(GL_TEXTURE1
);
1464 mpglActiveTexture(GL_TEXTURE2
);
1466 mpglActiveTexture(GL_TEXTURE0
);
1467 mpglEnable(GL_REGISTER_COMBINERS_NV
);
1469 case YUV_CONVERSION_COMBINERS_ATI
:
1470 mpglActiveTexture(GL_TEXTURE1
);
1472 mpglActiveTexture(GL_TEXTURE2
);
1474 mpglActiveTexture(GL_TEXTURE0
);
1475 mpglEnable(GL_FRAGMENT_SHADER_ATI
);
1477 case YUV_CONVERSION_TEXT_FRAGMENT
:
1478 mpglActiveTexture(GL_TEXTURE1
);
1480 mpglActiveTexture(GL_TEXTURE2
);
1482 mpglActiveTexture(GL_TEXTURE0
);
1483 mpglEnable(GL_TEXT_FRAGMENT_SHADER_ATI
);
1485 case YUV_CONVERSION_FRAGMENT_LOOKUP3D
:
1486 case YUV_CONVERSION_FRAGMENT_LOOKUP
:
1487 case YUV_CONVERSION_FRAGMENT_POW
:
1488 case YUV_CONVERSION_FRAGMENT
:
1489 case YUV_CONVERSION_NONE
:
1490 mpglEnable(GL_FRAGMENT_PROGRAM
);
1496 * \brief disable the specified YUV conversion
1497 * \param target texture target for Y, U and V textures (e.g. GL_TEXTURE_2D)
1498 * \param type type of YUV conversion
1499 * \ingroup glconversion
1501 void glDisableYUVConversion(GLenum target
, int type
) {
1502 switch (YUV_CONVERSION(type
)) {
1503 case YUV_CONVERSION_COMBINERS
:
1504 mpglActiveTexture(GL_TEXTURE1
);
1505 mpglDisable(target
);
1506 mpglActiveTexture(GL_TEXTURE2
);
1507 mpglDisable(target
);
1508 mpglActiveTexture(GL_TEXTURE0
);
1509 mpglDisable(GL_REGISTER_COMBINERS_NV
);
1511 case YUV_CONVERSION_COMBINERS_ATI
:
1512 mpglActiveTexture(GL_TEXTURE1
);
1513 mpglDisable(target
);
1514 mpglActiveTexture(GL_TEXTURE2
);
1515 mpglDisable(target
);
1516 mpglActiveTexture(GL_TEXTURE0
);
1517 mpglDisable(GL_FRAGMENT_SHADER_ATI
);
1519 case YUV_CONVERSION_TEXT_FRAGMENT
:
1520 mpglDisable(GL_TEXT_FRAGMENT_SHADER_ATI
);
1521 // HACK: at least the Mac OS X 10.5 PPC Radeon drivers are broken and
1522 // without this disable the texture units while the program is still
1523 // running (10.4 PPC seems to work without this though).
1525 mpglActiveTexture(GL_TEXTURE1
);
1526 mpglDisable(target
);
1527 mpglActiveTexture(GL_TEXTURE2
);
1528 mpglDisable(target
);
1529 mpglActiveTexture(GL_TEXTURE0
);
1531 case YUV_CONVERSION_FRAGMENT_LOOKUP3D
:
1532 case YUV_CONVERSION_FRAGMENT_LOOKUP
:
1533 case YUV_CONVERSION_FRAGMENT_POW
:
1534 case YUV_CONVERSION_FRAGMENT
:
1535 case YUV_CONVERSION_NONE
:
1536 mpglDisable(GL_FRAGMENT_PROGRAM
);
1542 * \brief draw a texture part at given 2D coordinates
1543 * \param x screen top coordinate
1544 * \param y screen left coordinate
1545 * \param w screen width coordinate
1546 * \param h screen height coordinate
1547 * \param tx texture top coordinate in pixels
1548 * \param ty texture left coordinate in pixels
1549 * \param tw texture part width in pixels
1550 * \param th texture part height in pixels
1551 * \param sx width of texture in pixels
1552 * \param sy height of texture in pixels
1553 * \param rect_tex whether this texture uses texture_rectangle extension
1554 * \param is_yv12 if != 0, also draw the textures from units 1 and 2,
1555 * bits 8 - 15 and 16 - 23 specify the x and y scaling of those textures
1556 * \param flip flip the texture upside down
1557 * \ingroup gltexture
1559 void glDrawTex(GLfloat x
, GLfloat y
, GLfloat w
, GLfloat h
,
1560 GLfloat tx
, GLfloat ty
, GLfloat tw
, GLfloat th
,
1561 int sx
, int sy
, int rect_tex
, int is_yv12
, int flip
) {
1562 int chroma_x_shift
= (is_yv12
>> 8) & 31;
1563 int chroma_y_shift
= (is_yv12
>> 16) & 31;
1564 GLfloat xscale
= 1 << chroma_x_shift
;
1565 GLfloat yscale
= 1 << chroma_y_shift
;
1566 GLfloat tx2
= tx
/ xscale
, ty2
= ty
/ yscale
, tw2
= tw
/ xscale
, th2
= th
/ yscale
;
1568 tx
/= sx
; ty
/= sy
; tw
/= sx
; th
/= sy
;
1569 tx2
= tx
, ty2
= ty
, tw2
= tw
, th2
= th
;
1575 mpglBegin(GL_QUADS
);
1576 mpglTexCoord2f(tx
, ty
);
1578 mpglMultiTexCoord2f(GL_TEXTURE1
, tx2
, ty2
);
1579 mpglMultiTexCoord2f(GL_TEXTURE2
, tx2
, ty2
);
1582 mpglTexCoord2f(tx
, ty
+ th
);
1584 mpglMultiTexCoord2f(GL_TEXTURE1
, tx2
, ty2
+ th2
);
1585 mpglMultiTexCoord2f(GL_TEXTURE2
, tx2
, ty2
+ th2
);
1587 mpglVertex2f(x
, y
+ h
);
1588 mpglTexCoord2f(tx
+ tw
, ty
+ th
);
1590 mpglMultiTexCoord2f(GL_TEXTURE1
, tx2
+ tw2
, ty2
+ th2
);
1591 mpglMultiTexCoord2f(GL_TEXTURE2
, tx2
+ tw2
, ty2
+ th2
);
1593 mpglVertex2f(x
+ w
, y
+ h
);
1594 mpglTexCoord2f(tx
+ tw
, ty
);
1596 mpglMultiTexCoord2f(GL_TEXTURE1
, tx2
+ tw2
, ty2
);
1597 mpglMultiTexCoord2f(GL_TEXTURE2
, tx2
+ tw2
, ty2
);
1599 mpglVertex2f(x
+ w
, y
);
1603 #ifdef CONFIG_GL_WIN32
1604 #include "w32_common.h"
1606 * \brief little helper since wglGetProcAddress definition does not fit our
1608 * \param procName name of function to look up
1609 * \return function pointer returned by wglGetProcAddress
1611 static void *w32gpa(const GLubyte
*procName
) {
1613 void *res
= wglGetProcAddress(procName
);
1614 if (res
) return res
;
1615 oglmod
= GetModuleHandle("opengl32.dll");
1616 return GetProcAddress(oglmod
, procName
);
1619 static int setGlWindow_w32(MPGLContext
*ctx
)
1621 HWND win
= vo_w32_window
;
1622 int *vinfo
= &ctx
->vinfo
.w32
;
1623 HGLRC
*context
= &ctx
->context
.w32
;
1625 HDC windc
= vo_w32_get_dc(win
);
1626 HGLRC new_context
= 0;
1627 int keep_context
= 0;
1628 int res
= SET_WINDOW_FAILED
;
1630 // should only be needed when keeping context, but not doing glFinish
1631 // can cause flickering even when we do not keep it.
1634 new_vinfo
= GetPixelFormat(windc
);
1635 if (*context
&& *vinfo
&& new_vinfo
&& *vinfo
== new_vinfo
) {
1636 // we can keep the wglContext
1637 new_context
= *context
;
1641 new_context
= wglCreateContext(windc
);
1643 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Could not create GL context!\n");
1649 if (!wglMakeCurrent(windc
, new_context
)) {
1650 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Could not set GL context!\n");
1651 if (!keep_context
) {
1652 wglDeleteContext(new_context
);
1658 vo_w32_window
= win
;
1661 GetClientRect(win
, &rect
);
1662 vo_dwidth
= rect
.right
;
1663 vo_dheight
= rect
.bottom
;
1665 if (!keep_context
) {
1667 wglDeleteContext(*context
);
1668 *context
= new_context
;
1670 getFunctions(w32gpa
, NULL
);
1672 // and inform that reinit is neccessary
1673 res
= SET_WINDOW_REINIT
;
1675 res
= SET_WINDOW_OK
;
1678 vo_w32_release_dc(win
, windc
);
1682 static void releaseGlContext_w32(MPGLContext
*ctx
) {
1683 int *vinfo
= &ctx
->vinfo
.w32
;
1684 HGLRC
*context
= &ctx
->context
.w32
;
1687 wglMakeCurrent(0, 0);
1688 wglDeleteContext(*context
);
1693 static void swapGlBuffers_w32(MPGLContext
*ctx
) {
1694 HDC vo_hdc
= vo_w32_get_dc(vo_w32_window
);
1695 SwapBuffers(vo_hdc
);
1696 vo_w32_release_dc(vo_w32_window
, vo_hdc
);
1699 #ifdef CONFIG_GL_X11
1700 #include "x11_common.h"
1703 * \brief Returns the XVisualInfo associated with Window win.
1704 * \param win Window whose XVisualInfo is returne.
1705 * \return XVisualInfo of the window. Caller must use XFree to free it.
1707 static XVisualInfo
*getWindowVisualInfo(Window win
) {
1708 XWindowAttributes xw_attr
;
1709 XVisualInfo vinfo_template
;
1711 XGetWindowAttributes(mDisplay
, win
, &xw_attr
);
1712 vinfo_template
.visualid
= XVisualIDFromVisual(xw_attr
.visual
);
1713 return XGetVisualInfo(mDisplay
, VisualIDMask
, &vinfo_template
, &tmp
);
1716 static void appendstr(char **dst
, const char *str
)
1722 newsize
= strlen(*dst
) + 1 + strlen(str
) + 1;
1723 newstr
= realloc(*dst
, newsize
);
1732 * \brief Changes the window in which video is displayed.
1733 * If possible only transfers the context to the new window, otherwise
1734 * creates a new one, which must be initialized by the caller.
1735 * \param vinfo Currently used visual.
1736 * \param context Currently used context.
1737 * \param win window that should be used for drawing.
1738 * \return one of SET_WINDOW_FAILED, SET_WINDOW_OK or SET_WINDOW_REINIT.
1739 * In case of SET_WINDOW_REINIT the context could not be transfered
1740 * and the caller must initialize it correctly.
1741 * \ingroup glcontext
1743 static int setGlWindow_x11(MPGLContext
*ctx
)
1745 XVisualInfo
**vinfo
= &ctx
->vinfo
.x11
;
1746 GLXContext
*context
= &ctx
->context
.x11
;
1747 Window win
= vo_window
;
1748 XVisualInfo
*new_vinfo
;
1749 GLXContext new_context
= NULL
;
1750 int keep_context
= 0;
1752 // should only be needed when keeping context, but not doing glFinish
1753 // can cause flickering even when we do not keep it.
1756 new_vinfo
= getWindowVisualInfo(win
);
1757 if (*context
&& *vinfo
&& new_vinfo
&&
1758 (*vinfo
)->visualid
== new_vinfo
->visualid
) {
1759 // we can keep the GLXContext
1760 new_context
= *context
;
1766 new_context
= glXCreateContext(mDisplay
, new_vinfo
, NULL
, True
);
1768 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Could not create GLX context!\n");
1770 return SET_WINDOW_FAILED
;
1775 if (!glXMakeCurrent(mDisplay
, vo_window
, new_context
)) {
1776 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Could not set GLX context!\n");
1777 if (!keep_context
) {
1778 glXDestroyContext(mDisplay
, new_context
);
1781 return SET_WINDOW_FAILED
;
1786 vo_x11_update_geometry();
1787 if (!keep_context
) {
1788 void *(*getProcAddress
)(const GLubyte
*);
1789 const char *(*glXExtStr
)(Display
*, int);
1790 char *glxstr
= strdup("");
1792 glXDestroyContext(mDisplay
, *context
);
1793 *context
= new_context
;
1797 getProcAddress
= getdladdr("glXGetProcAddress");
1798 if (!getProcAddress
)
1799 getProcAddress
= getdladdr("glXGetProcAddressARB");
1800 glXExtStr
= getdladdr("glXQueryExtensionsString");
1802 appendstr(&glxstr
, glXExtStr(mDisplay
, DefaultScreen(mDisplay
)));
1803 glXExtStr
= getdladdr("glXGetClientString");
1805 appendstr(&glxstr
, glXExtStr(mDisplay
, GLX_EXTENSIONS
));
1806 glXExtStr
= getdladdr("glXGetServerString");
1808 appendstr(&glxstr
, glXExtStr(mDisplay
, GLX_EXTENSIONS
));
1810 getFunctions(getProcAddress
, glxstr
);
1811 if (!mpglGenPrograms
&& mpglGetString
&&
1813 strstr(mpglGetString(GL_EXTENSIONS
), "GL_ARB_vertex_program")) {
1814 mp_msg(MSGT_VO
, MSGL_WARN
, "Broken glXGetProcAddress detected, trying workaround\n");
1815 getFunctions(NULL
, glxstr
);
1819 // and inform that reinit is neccessary
1820 return SET_WINDOW_REINIT
;
1822 return SET_WINDOW_OK
;
1826 * \brief free the VisualInfo and GLXContext of an OpenGL context.
1827 * \ingroup glcontext
1829 static void releaseGlContext_x11(MPGLContext
*ctx
) {
1830 XVisualInfo
**vinfo
= &ctx
->vinfo
.x11
;
1831 GLXContext
*context
= &ctx
->context
.x11
;
1838 glXMakeCurrent(mDisplay
, None
, NULL
);
1839 glXDestroyContext(mDisplay
, *context
);
1844 static void swapGlBuffers_x11(MPGLContext
*ctx
) {
1845 glXSwapBuffers(mDisplay
, vo_window
);
1848 static int x11_check_events(void) {
1849 return vo_x11_check_events(mDisplay
);
1852 static void gl_update_xinerama_info(void)
1854 update_xinerama_info(global_vo
);
1857 static void gl_border(void)
1862 static void gl_fullscreen(void)
1864 vo_x11_fullscreen();
1867 static void gl_ontop(void)
1873 #ifdef CONFIG_GL_SDL
1874 #include "sdl_common.h"
1876 static void swapGlBuffers_sdl(MPGLContext
*ctx
) {
1877 SDL_GL_SwapBuffers();
1880 static void *sdlgpa(const GLubyte
*name
) {
1881 return SDL_GL_GetProcAddress(name
);
1884 static int setGlWindow_sdl(MPGLContext
*ctx
) {
1885 if (sdl_set_mode(0, SDL_OPENGL
| SDL_RESIZABLE
) < 0)
1886 return SET_WINDOW_FAILED
;
1887 SDL_GL_LoadLibrary(NULL
);
1888 getFunctions(sdlgpa
, NULL
);
1889 return SET_WINDOW_OK
;
1892 static int sdl_check_events(void) {
1895 while (SDL_PollEvent(&event
)) {
1896 res
|= sdl_default_handle_event(&event
);
1898 // poll "events" from within MPlayer code
1899 res
|= sdl_default_handle_event(NULL
);
1900 if (res
& VO_EVENT_RESIZE
)
1901 sdl_set_mode(0, SDL_OPENGL
| SDL_RESIZABLE
);
1907 static int setGlWindow_dummy(MPGLContext
*ctx
) {
1908 getFunctions(NULL
, NULL
);
1909 return SET_WINDOW_OK
;
1912 static void releaseGlContext_dummy(MPGLContext
*ctx
) {
1915 static int dummy_check_events(void) {
1919 static void dummy_update_xinerama_info(void) {
1920 if (vo_screenwidth
<= 0 || vo_screenheight
<= 0) {
1921 mp_msg(MSGT_VO
, MSGL_ERR
, "You must specify the screen dimensions "
1922 "with -screenw and -screenh\n");
1923 vo_screenwidth
= 1280;
1924 vo_screenheight
= 768;
1926 aspect_save_screenres(vo_screenwidth
, vo_screenheight
);
1929 int init_mpglcontext(MPGLContext
*ctx
, enum MPGLType type
) {
1930 if (type
== GLTYPE_AUTO
) {
1931 int res
= init_mpglcontext(ctx
, GLTYPE_W32
);
1932 if (res
) return res
;
1933 res
= init_mpglcontext(ctx
, GLTYPE_X11
);
1934 if (res
) return res
;
1935 res
= init_mpglcontext(ctx
, GLTYPE_SDL
);
1938 memset(ctx
, 0, sizeof(*ctx
));
1939 ctx
->setGlWindow
= setGlWindow_dummy
;
1940 ctx
->releaseGlContext
= releaseGlContext_dummy
;
1941 ctx
->update_xinerama_info
= dummy_update_xinerama_info
;
1942 ctx
->check_events
= dummy_check_events
;
1944 switch (ctx
->type
) {
1945 #ifdef CONFIG_GL_WIN32
1947 ctx
->setGlWindow
= setGlWindow_w32
;
1948 ctx
->releaseGlContext
= releaseGlContext_w32
;
1949 ctx
->swapGlBuffers
= swapGlBuffers_w32
;
1950 ctx
->update_xinerama_info
= w32_update_xinerama_info
;
1951 ctx
->border
= vo_w32_border
;
1952 ctx
->check_events
= vo_w32_check_events
;
1953 ctx
->fullscreen
= vo_w32_fullscreen
;
1954 ctx
->ontop
= vo_w32_ontop
;
1955 return vo_w32_init();
1957 #ifdef CONFIG_GL_X11
1959 ctx
->setGlWindow
= setGlWindow_x11
;
1960 ctx
->releaseGlContext
= releaseGlContext_x11
;
1961 ctx
->swapGlBuffers
= swapGlBuffers_x11
;
1962 ctx
->update_xinerama_info
= gl_update_xinerama_info
;
1963 ctx
->border
= gl_border
;
1964 ctx
->check_events
= x11_check_events
;
1965 ctx
->fullscreen
= gl_fullscreen
;
1966 ctx
->ontop
= gl_ontop
;
1969 #ifdef CONFIG_GL_SDL
1971 SDL_Init(SDL_INIT_VIDEO
);
1972 ctx
->setGlWindow
= setGlWindow_sdl
;
1973 ctx
->swapGlBuffers
= swapGlBuffers_sdl
;
1974 ctx
->check_events
= sdl_check_events
;
1975 ctx
->fullscreen
= vo_sdl_fullscreen
;
1976 return vo_sdl_init();
1983 void uninit_mpglcontext(MPGLContext
*ctx
) {
1984 ctx
->releaseGlContext(ctx
);
1985 switch (ctx
->type
) {
1986 #ifdef CONFIG_GL_WIN32
1991 #ifdef CONFIG_GL_X11
1996 #ifdef CONFIG_GL_SDL
2002 memset(ctx
, 0, sizeof(*ctx
));