2 * common OpenGL routines
4 * copyleft (C) 2005 Reimar Döffinger <Reimar.Doeffinger@stud.uni-karlsruhe.de>
5 * Special thanks go to the xine team and Matthias Hopf, whose video_out_opengl.c
6 * gave me lots of good ideas.
8 * This file is part of MPlayer.
10 * MPlayer is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * MPlayer is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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"
39 void (GLAPIENTRY
*Begin
)(GLenum
);
40 void (GLAPIENTRY
*End
)(void);
41 void (GLAPIENTRY
*Viewport
)(GLint
, GLint
, GLsizei
, GLsizei
);
42 void (GLAPIENTRY
*MatrixMode
)(GLenum
);
43 void (GLAPIENTRY
*LoadIdentity
)(void);
44 void (GLAPIENTRY
*Translated
)(double, double, double);
45 void (GLAPIENTRY
*Scaled
)(double, double, double);
46 void (GLAPIENTRY
*Ortho
)(double, double, double, double, double, double);
47 void (GLAPIENTRY
*Frustum
)(double, double, double, double, double, double);
48 void (GLAPIENTRY
*PushMatrix
)(void);
49 void (GLAPIENTRY
*PopMatrix
)(void);
50 void (GLAPIENTRY
*Clear
)(GLbitfield
);
51 GLuint (GLAPIENTRY
*GenLists
)(GLsizei
);
52 void (GLAPIENTRY
*DeleteLists
)(GLuint
, GLsizei
);
53 void (GLAPIENTRY
*NewList
)(GLuint
, GLenum
);
54 void (GLAPIENTRY
*EndList
)(void);
55 void (GLAPIENTRY
*CallList
)(GLuint
);
56 void (GLAPIENTRY
*CallLists
)(GLsizei
, GLenum
, const GLvoid
*);
57 void (GLAPIENTRY
*GenTextures
)(GLsizei
, GLuint
*);
58 void (GLAPIENTRY
*DeleteTextures
)(GLsizei
, const GLuint
*);
59 void (GLAPIENTRY
*TexEnvf
)(GLenum
, GLenum
, GLfloat
);
60 void (GLAPIENTRY
*TexEnvi
)(GLenum
, GLenum
, GLint
);
61 void (GLAPIENTRY
*Color4ub
)(GLubyte
, GLubyte
, GLubyte
, GLubyte
);
62 void (GLAPIENTRY
*Color3f
)(GLfloat
, GLfloat
, GLfloat
);
63 void (GLAPIENTRY
*Color4f
)(GLfloat
, GLfloat
, GLfloat
, GLfloat
);
64 void (GLAPIENTRY
*ClearColor
)(GLclampf
, GLclampf
, GLclampf
, GLclampf
);
65 void (GLAPIENTRY
*ClearDepth
)(GLclampd
);
66 void (GLAPIENTRY
*DepthFunc
)(GLenum
);
67 void (GLAPIENTRY
*Enable
)(GLenum
);
68 void (GLAPIENTRY
*Disable
)(GLenum
);
69 const GLubyte
*(GLAPIENTRY
*GetString
)(GLenum
);
70 void (GLAPIENTRY
*DrawBuffer
)(GLenum
);
71 void (GLAPIENTRY
*DepthMask
)(GLboolean
);
72 void (GLAPIENTRY
*BlendFunc
)(GLenum
, GLenum
);
73 void (GLAPIENTRY
*Flush
)(void);
74 void (GLAPIENTRY
*Finish
)(void);
75 void (GLAPIENTRY
*PixelStorei
)(GLenum
, GLint
);
76 void (GLAPIENTRY
*TexImage1D
)(GLenum
, GLint
, GLint
, GLsizei
, GLint
, GLenum
, GLenum
, const GLvoid
*);
77 void (GLAPIENTRY
*TexImage2D
)(GLenum
, GLint
, GLint
, GLsizei
, GLsizei
, GLint
, GLenum
, GLenum
, const GLvoid
*);
78 void (GLAPIENTRY
*TexSubImage2D
)(GLenum
, GLint
, GLint
, GLint
, GLsizei
, GLsizei
, GLenum
, GLenum
, const GLvoid
*);
79 void (GLAPIENTRY
*TexParameteri
)(GLenum
, GLenum
, GLint
);
80 void (GLAPIENTRY
*TexParameterf
)(GLenum
, GLenum
, GLfloat
);
81 void (GLAPIENTRY
*TexParameterfv
)(GLenum
, GLenum
, const GLfloat
*);
82 void (GLAPIENTRY
*TexCoord2f
)(GLfloat
, GLfloat
);
83 void (GLAPIENTRY
*Vertex2f
)(GLfloat
, GLfloat
);
84 void (GLAPIENTRY
*Vertex3f
)(GLfloat
, GLfloat
, GLfloat
);
85 void (GLAPIENTRY
*Normal3f
)(GLfloat
, GLfloat
, GLfloat
);
86 void (GLAPIENTRY
*Lightfv
)(GLenum
, GLenum
, const GLfloat
*);
87 void (GLAPIENTRY
*ColorMaterial
)(GLenum
, GLenum
);
88 void (GLAPIENTRY
*ShadeModel
)(GLenum
);
89 void (GLAPIENTRY
*GetIntegerv
)(GLenum
, GLint
*);
92 * \defgroup glextfunctions OpenGL extension functions
94 * the pointers to these functions are acquired when the OpenGL
98 void (GLAPIENTRY
*GenBuffers
)(GLsizei
, GLuint
*);
99 void (GLAPIENTRY
*DeleteBuffers
)(GLsizei
, const GLuint
*);
100 void (GLAPIENTRY
*BindBuffer
)(GLenum
, GLuint
);
101 GLvoid
* (GLAPIENTRY
*MapBuffer
)(GLenum
, GLenum
);
102 GLboolean (GLAPIENTRY
*UnmapBuffer
)(GLenum
);
103 void (GLAPIENTRY
*BufferData
)(GLenum
, intptr_t, const GLvoid
*, GLenum
);
104 void (GLAPIENTRY
*CombinerParameterfv
)(GLenum
, const GLfloat
*);
105 void (GLAPIENTRY
*CombinerParameteri
)(GLenum
, GLint
);
106 void (GLAPIENTRY
*CombinerInput
)(GLenum
, GLenum
, GLenum
, GLenum
, GLenum
,
108 void (GLAPIENTRY
*CombinerOutput
)(GLenum
, GLenum
, GLenum
, GLenum
, GLenum
,
109 GLenum
, GLenum
, GLboolean
, GLboolean
,
111 void (GLAPIENTRY
*BeginFragmentShader
)(void);
112 void (GLAPIENTRY
*EndFragmentShader
)(void);
113 void (GLAPIENTRY
*SampleMap
)(GLuint
, GLuint
, GLenum
);
114 void (GLAPIENTRY
*ColorFragmentOp2
)(GLenum
, GLuint
, GLuint
, GLuint
, GLuint
,
115 GLuint
, GLuint
, GLuint
, GLuint
, GLuint
);
116 void (GLAPIENTRY
*ColorFragmentOp3
)(GLenum
, GLuint
, GLuint
, GLuint
, GLuint
,
117 GLuint
, GLuint
, GLuint
, GLuint
, GLuint
,
118 GLuint
, GLuint
, GLuint
);
119 void (GLAPIENTRY
*SetFragmentShaderConstant
)(GLuint
, const GLfloat
*);
120 void (GLAPIENTRY
*ActiveTexture
)(GLenum
);
121 void (GLAPIENTRY
*BindTexture
)(GLenum
, GLuint
);
122 void (GLAPIENTRY
*MultiTexCoord2f
)(GLenum
, GLfloat
, GLfloat
);
123 void (GLAPIENTRY
*GenPrograms
)(GLsizei
, GLuint
*);
124 void (GLAPIENTRY
*DeletePrograms
)(GLsizei
, const GLuint
*);
125 void (GLAPIENTRY
*BindProgram
)(GLenum
, GLuint
);
126 void (GLAPIENTRY
*ProgramString
)(GLenum
, GLenum
, GLsizei
, const GLvoid
*);
127 void (GLAPIENTRY
*GetProgramiv
)(GLenum
, GLenum
, GLint
*);
128 void (GLAPIENTRY
*ProgramEnvParameter4f
)(GLenum
, GLuint
, GLfloat
, GLfloat
,
130 int (GLAPIENTRY
*SwapInterval
)(int);
131 void (GLAPIENTRY
*TexImage3D
)(GLenum
, GLint
, GLenum
, GLsizei
, GLsizei
, GLsizei
,
132 GLint
, GLenum
, GLenum
, const GLvoid
*);
133 void* (GLAPIENTRY
*AllocateMemoryMESA
)(void *, int, size_t, float, float, float);
134 void (GLAPIENTRY
*FreeMemoryMESA
)(void *, int, void *);
135 /** \} */ // end of glextfunctions group
137 //! \defgroup glgeneral OpenGL general helper functions
139 //! \defgroup glcontext OpenGL context management helper functions
141 //! \defgroup gltexture OpenGL texture handling helper functions
143 //! \defgroup glconversion OpenGL conversion helper functions
145 static GLint hqtexfmt
;
148 * \brief adjusts the GL_UNPACK_ALIGNMENT to fit the stride.
149 * \param stride number of bytes per line for which alignment should fit.
152 void glAdjustAlignment(int stride
) {
156 else if (stride
% 4 == 0)
158 else if (stride
% 2 == 0)
162 PixelStorei(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
;
353 static void *setNull(const GLubyte
*s
) {
360 const char *funcnames
[7];
364 #define DEF_FUNC_DESC(name) {&name, NULL, {"gl"#name, NULL}, gl ##name}
365 static const extfunc_desc_t extfuncs
[] = {
366 // these aren't extension functions but we query them anyway to allow
367 // different "backends" with one binary
368 DEF_FUNC_DESC(Begin
),
370 DEF_FUNC_DESC(Viewport
),
371 DEF_FUNC_DESC(MatrixMode
),
372 DEF_FUNC_DESC(LoadIdentity
),
373 DEF_FUNC_DESC(Translated
),
374 DEF_FUNC_DESC(Scaled
),
375 DEF_FUNC_DESC(Ortho
),
376 DEF_FUNC_DESC(Frustum
),
377 DEF_FUNC_DESC(PushMatrix
),
378 DEF_FUNC_DESC(PopMatrix
),
379 DEF_FUNC_DESC(Clear
),
380 DEF_FUNC_DESC(GenLists
),
381 DEF_FUNC_DESC(DeleteLists
),
382 DEF_FUNC_DESC(NewList
),
383 DEF_FUNC_DESC(EndList
),
384 DEF_FUNC_DESC(CallList
),
385 DEF_FUNC_DESC(CallLists
),
386 DEF_FUNC_DESC(GenTextures
),
387 DEF_FUNC_DESC(DeleteTextures
),
388 DEF_FUNC_DESC(TexEnvf
),
389 DEF_FUNC_DESC(TexEnvi
),
390 DEF_FUNC_DESC(Color4ub
),
391 DEF_FUNC_DESC(Color3f
),
392 DEF_FUNC_DESC(Color4f
),
393 DEF_FUNC_DESC(ClearColor
),
394 DEF_FUNC_DESC(ClearDepth
),
395 DEF_FUNC_DESC(DepthFunc
),
396 DEF_FUNC_DESC(Enable
),
397 DEF_FUNC_DESC(Disable
),
398 DEF_FUNC_DESC(DrawBuffer
),
399 DEF_FUNC_DESC(DepthMask
),
400 DEF_FUNC_DESC(BlendFunc
),
401 DEF_FUNC_DESC(Flush
),
402 DEF_FUNC_DESC(Finish
),
403 DEF_FUNC_DESC(PixelStorei
),
404 DEF_FUNC_DESC(TexImage1D
),
405 DEF_FUNC_DESC(TexImage2D
),
406 DEF_FUNC_DESC(TexSubImage2D
),
407 DEF_FUNC_DESC(TexParameteri
),
408 DEF_FUNC_DESC(TexParameterf
),
409 DEF_FUNC_DESC(TexParameterfv
),
410 DEF_FUNC_DESC(TexCoord2f
),
411 DEF_FUNC_DESC(Vertex2f
),
412 DEF_FUNC_DESC(Vertex3f
),
413 DEF_FUNC_DESC(Normal3f
),
414 DEF_FUNC_DESC(Lightfv
),
415 DEF_FUNC_DESC(ColorMaterial
),
416 DEF_FUNC_DESC(ShadeModel
),
417 DEF_FUNC_DESC(GetIntegerv
),
419 // here start the real extensions
420 {&GenBuffers
, NULL
, {"glGenBuffers", "glGenBuffersARB", NULL
}},
421 {&DeleteBuffers
, NULL
, {"glDeleteBuffers", "glDeleteBuffersARB", NULL
}},
422 {&BindBuffer
, NULL
, {"glBindBuffer", "glBindBufferARB", NULL
}},
423 {&MapBuffer
, NULL
, {"glMapBuffer", "glMapBufferARB", NULL
}},
424 {&UnmapBuffer
, NULL
, {"glUnmapBuffer", "glUnmapBufferARB", NULL
}},
425 {&BufferData
, NULL
, {"glBufferData", "glBufferDataARB", NULL
}},
426 {&CombinerParameterfv
, "NV_register_combiners", {"glCombinerParameterfv", "glCombinerParameterfvNV", NULL
}},
427 {&CombinerParameteri
, "NV_register_combiners", {"glCombinerParameteri", "glCombinerParameteriNV", NULL
}},
428 {&CombinerInput
, "NV_register_combiners", {"glCombinerInput", "glCombinerInputNV", NULL
}},
429 {&CombinerOutput
, "NV_register_combiners", {"glCombinerOutput", "glCombinerOutputNV", NULL
}},
430 {&BeginFragmentShader
, "ATI_fragment_shader", {"glBeginFragmentShaderATI", NULL
}},
431 {&EndFragmentShader
, "ATI_fragment_shader", {"glEndFragmentShaderATI", NULL
}},
432 {&SampleMap
, "ATI_fragment_shader", {"glSampleMapATI", NULL
}},
433 {&ColorFragmentOp2
, "ATI_fragment_shader", {"glColorFragmentOp2ATI", NULL
}},
434 {&ColorFragmentOp3
, "ATI_fragment_shader", {"glColorFragmentOp3ATI", NULL
}},
435 {&SetFragmentShaderConstant
, "ATI_fragment_shader", {"glSetFragmentShaderConstantATI", NULL
}},
436 {&ActiveTexture
, NULL
, {"glActiveTexture", "glActiveTextureARB", NULL
}},
437 {&BindTexture
, NULL
, {"glBindTexture", "glBindTextureARB", "glBindTextureEXT", NULL
}},
438 {&MultiTexCoord2f
, NULL
, {"glMultiTexCoord2f", "glMultiTexCoord2fARB", NULL
}},
439 {&GenPrograms
, "_program", {"glGenProgramsARB", NULL
}},
440 {&DeletePrograms
, "_program", {"glDeleteProgramsARB", NULL
}},
441 {&BindProgram
, "_program", {"glBindProgramARB", NULL
}},
442 {&ProgramString
, "_program", {"glProgramStringARB", NULL
}},
443 {&GetProgramiv
, "_program", {"glGetProgramivARB", NULL
}},
444 {&ProgramEnvParameter4f
, "_program", {"glProgramEnvParameter4fARB", NULL
}},
445 {&SwapInterval
, "_swap_control", {"glXSwapInterval", "glXSwapIntervalSGI", "wglSwapInterval", "wglSwapIntervalEXT", "wglSwapIntervalSGI", NULL
}},
446 {&TexImage3D
, NULL
, {"glTexImage3D", NULL
}},
447 {&AllocateMemoryMESA
, "GLX_MESA_allocate_memory", {"glXAllocateMemoryMESA", NULL
}},
448 {&FreeMemoryMESA
, "GLX_MESA_allocate_memory", {"glXFreeMemoryMESA", NULL
}},
453 * \brief find the function pointers of some useful OpenGL extensions
454 * \param getProcAddress function to resolve function names, may be NULL
455 * \param ext2 an extra extension string
457 static void getFunctions(void *(*getProcAddress
)(const GLubyte
*),
459 const extfunc_desc_t
*dsc
;
460 const char *extensions
;
464 getProcAddress
= setNull
;
466 // special case, we need glGetString before starting to find the other functions
467 GetString
= getProcAddress("glGetString");
469 GetString
= glGetString
;
471 extensions
= (const char *)GetString(GL_EXTENSIONS
);
472 if (!extensions
) extensions
= "";
473 if (!ext2
) ext2
= "";
474 allexts
= malloc(strlen(extensions
) + strlen(ext2
) + 2);
475 strcpy(allexts
, extensions
);
476 strcat(allexts
, " ");
477 strcat(allexts
, ext2
);
478 mp_msg(MSGT_VO
, MSGL_DBG2
, "OpenGL extensions string:\n%s\n", allexts
);
479 for (dsc
= extfuncs
; dsc
->funcptr
; dsc
++) {
482 if (!dsc
->extstr
|| strstr(allexts
, dsc
->extstr
)) {
483 for (i
= 0; !ptr
&& dsc
->funcnames
[i
]; i
++)
484 ptr
= getProcAddress((const GLubyte
*)dsc
->funcnames
[i
]);
488 *(void **)dsc
->funcptr
= ptr
;
490 if (strstr(allexts
, "_texture_float"))
491 hqtexfmt
= GL_RGB32F
;
492 else if (strstr(allexts
, "NV_float_buffer"))
493 hqtexfmt
= GL_FLOAT_RGB32_NV
;
500 * \brief create a texture and set some defaults
501 * \param target texture taget, usually GL_TEXTURE_2D
502 * \param fmt internal texture format
503 * \param format texture host data format
504 * \param type texture host data type
505 * \param filter filter used for scaling, e.g. GL_LINEAR
506 * \param w texture width
507 * \param h texture height
508 * \param val luminance value to fill texture with
511 void glCreateClearTex(GLenum target
, GLenum fmt
, GLenum format
, GLenum type
, GLint filter
,
512 int w
, int h
, unsigned char val
) {
513 GLfloat fval
= (GLfloat
)val
/ 255.0;
514 GLfloat border
[4] = {fval
, fval
, fval
, fval
};
515 int stride
= w
* glFmt2bpp(format
, type
);
518 init
= malloc(stride
* h
);
519 memset(init
, val
, stride
* h
);
520 glAdjustAlignment(stride
);
521 PixelStorei(GL_UNPACK_ROW_LENGTH
, w
);
522 TexImage2D(target
, 0, fmt
, w
, h
, 0, format
, type
, init
);
523 TexParameterf(target
, GL_TEXTURE_PRIORITY
, 1.0);
524 TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, filter
);
525 TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, filter
);
526 TexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
527 TexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
528 // Border texels should not be used with CLAMP_TO_EDGE
529 // We set a sane default anyway.
530 TexParameterfv(target
, GL_TEXTURE_BORDER_COLOR
, border
);
535 * \brief skips whitespace and comments
536 * \param f file to read from
538 static void ppm_skip(FILE *f
) {
546 } while (c
!= EOF
&& (isspace(c
) || comment
));
551 #define MAXDIM (16 * 1024)
554 * \brief creates a texture from a PPM file
555 * \param target texture taget, usually GL_TEXTURE_2D
556 * \param fmt internal texture format, 0 for default
557 * \param filter filter used for scaling, e.g. GL_LINEAR
558 * \param f file to read PPM from
559 * \param width [out] width of texture
560 * \param height [out] height of texture
561 * \param maxval [out] maxval value from PPM file
562 * \return 0 on error, 1 otherwise
565 int glCreatePPMTex(GLenum target
, GLenum fmt
, GLint filter
,
566 FILE *f
, int *width
, int *height
, int *maxval
) {
567 unsigned w
, h
, m
, val
, bpp
;
571 if (fgetc(f
) != 'P' || fgetc(f
) != '6')
574 if (fscanf(f
, "%u", &w
) != 1)
577 if (fscanf(f
, "%u", &h
) != 1)
580 if (fscanf(f
, "%u", &m
) != 1)
585 if (w
> MAXDIM
|| h
> MAXDIM
)
587 bpp
= (m
> 255) ? 6 : 3;
588 data
= malloc(w
* h
* bpp
);
589 if (fread(data
, w
* bpp
, h
, f
) != h
)
592 fmt
= (m
> 255) ? hqtexfmt
: 3;
593 if (fmt
== GL_FLOAT_RGB32_NV
&& target
!= GL_TEXTURE_RECTANGLE
)
596 type
= m
> 255 ? GL_UNSIGNED_SHORT
: GL_UNSIGNED_BYTE
;
597 glCreateClearTex(target
, fmt
, GL_RGB
, type
, filter
, w
, h
, 0);
598 glUploadTex(target
, GL_RGB
, type
,
599 data
, w
* bpp
, 0, 0, w
, h
, 0);
601 if (width
) *width
= w
;
602 if (height
) *height
= h
;
603 if (maxval
) *maxval
= m
;
608 * \brief return the number of bytes per pixel for the given format
609 * \param format OpenGL format
610 * \param type OpenGL type
611 * \return bytes per pixel
614 * Does not handle all possible variants, just those used by MPlayer
616 int glFmt2bpp(GLenum format
, GLenum type
) {
617 int component_size
= 0;
619 case GL_UNSIGNED_BYTE_3_3_2
:
620 case GL_UNSIGNED_BYTE_2_3_3_REV
:
622 case GL_UNSIGNED_SHORT_5_5_5_1
:
623 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
624 case GL_UNSIGNED_SHORT_5_6_5
:
625 case GL_UNSIGNED_SHORT_5_6_5_REV
:
627 case GL_UNSIGNED_BYTE
:
630 case GL_UNSIGNED_SHORT
:
637 return component_size
;
642 return 3 * component_size
;
645 return 4 * component_size
;
651 * \brief upload a texture, handling things like stride and slices
652 * \param target texture target, usually GL_TEXTURE_2D
653 * \param format OpenGL format of data
654 * \param type OpenGL type of data
655 * \param dataptr data to upload
656 * \param stride data stride
657 * \param x x offset in texture
658 * \param y y offset in texture
659 * \param w width of the texture part to upload
660 * \param h height of the texture part to upload
661 * \param slice height of an upload slice, 0 for all at once
664 void glUploadTex(GLenum target
, GLenum format
, GLenum type
,
665 const void *dataptr
, int stride
,
666 int x
, int y
, int w
, int h
, int slice
) {
667 const uint8_t *data
= dataptr
;
669 if (w
<= 0 || h
<= 0) return;
673 data
+= (h
- 1) * stride
;
676 // this is not always correct, but should work for MPlayer
677 glAdjustAlignment(stride
);
678 PixelStorei(GL_UNPACK_ROW_LENGTH
, stride
/ glFmt2bpp(format
, type
));
679 for (; y
+ slice
<= y_max
; y
+= slice
) {
680 TexSubImage2D(target
, 0, x
, y
, w
, slice
, format
, type
, data
);
681 data
+= stride
* slice
;
684 TexSubImage2D(target
, 0, x
, y
, w
, y_max
- y
, format
, type
, data
);
687 static void fillUVcoeff(GLfloat
*ucoef
, GLfloat
*vcoef
,
688 float uvcos
, float uvsin
) {
690 ucoef
[0] = 0 * uvcos
+ 1.403 * uvsin
;
691 vcoef
[0] = 0 * uvsin
+ 1.403 * uvcos
;
692 ucoef
[1] = -0.344 * uvcos
+ -0.714 * uvsin
;
693 vcoef
[1] = -0.344 * uvsin
+ -0.714 * uvcos
;
694 ucoef
[2] = 1.770 * uvcos
+ 0 * uvsin
;
695 vcoef
[2] = 1.770 * uvsin
+ 0 * uvcos
;
698 // Coefficients (probably) must be in [0, 1] range, whereas they originally
699 // are in [-2, 2] range, so here comes the trick:
700 // First put them in the [-0.5, 0.5] range, then add 0.5.
701 // This can be undone with the HALF_BIAS and SCALE_BY_FOUR arguments
702 // for CombinerInput and CombinerOutput (or the respective ATI variants)
703 for (i
= 0; i
< 4; i
++) {
704 ucoef
[i
] = ucoef
[i
] * 0.25 + 0.5;
705 vcoef
[i
] = vcoef
[i
] * 0.25 + 0.5;
710 * \brief Setup register combiners for YUV to RGB conversion.
711 * \param uvcos used for saturation and hue adjustment
712 * \param uvsin used for saturation and hue adjustment
714 static void glSetupYUVCombiners(float uvcos
, float uvsin
) {
718 if (!CombinerInput
|| !CombinerOutput
||
719 !CombinerParameterfv
|| !CombinerParameteri
) {
720 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Combiner functions missing!\n");
723 GetIntegerv(GL_MAX_GENERAL_COMBINERS_NV
, &i
);
725 mp_msg(MSGT_VO
, MSGL_ERR
,
726 "[gl] 2 general combiners needed for YUV combiner support (found %i)\n", i
);
727 GetIntegerv(GL_MAX_TEXTURE_UNITS
, &i
);
729 mp_msg(MSGT_VO
, MSGL_ERR
,
730 "[gl] 3 texture units needed for YUV combiner support (found %i)\n", i
);
731 fillUVcoeff(ucoef
, vcoef
, uvcos
, uvsin
);
732 CombinerParameterfv(GL_CONSTANT_COLOR0_NV
, ucoef
);
733 CombinerParameterfv(GL_CONSTANT_COLOR1_NV
, vcoef
);
735 // UV first, like this green component cannot overflow
736 CombinerInput(GL_COMBINER0_NV
, GL_RGB
, GL_VARIABLE_A_NV
,
737 GL_TEXTURE1
, GL_HALF_BIAS_NORMAL_NV
, GL_RGB
);
738 CombinerInput(GL_COMBINER0_NV
, GL_RGB
, GL_VARIABLE_B_NV
,
739 GL_CONSTANT_COLOR0_NV
, GL_HALF_BIAS_NORMAL_NV
, GL_RGB
);
740 CombinerInput(GL_COMBINER0_NV
, GL_RGB
, GL_VARIABLE_C_NV
,
741 GL_TEXTURE2
, GL_HALF_BIAS_NORMAL_NV
, GL_RGB
);
742 CombinerInput(GL_COMBINER0_NV
, GL_RGB
, GL_VARIABLE_D_NV
,
743 GL_CONSTANT_COLOR1_NV
, GL_HALF_BIAS_NORMAL_NV
, GL_RGB
);
744 CombinerOutput(GL_COMBINER0_NV
, GL_RGB
, GL_DISCARD_NV
, GL_DISCARD_NV
,
745 GL_SPARE0_NV
, GL_SCALE_BY_FOUR_NV
, GL_NONE
, GL_FALSE
,
749 CombinerInput(GL_COMBINER1_NV
, GL_RGB
, GL_VARIABLE_A_NV
, GL_SPARE0_NV
,
750 GL_SIGNED_IDENTITY_NV
, GL_RGB
);
751 CombinerInput(GL_COMBINER1_NV
, GL_RGB
, GL_VARIABLE_B_NV
, GL_ZERO
,
752 GL_UNSIGNED_INVERT_NV
, GL_RGB
);
753 CombinerInput(GL_COMBINER1_NV
, GL_RGB
, GL_VARIABLE_C_NV
,
754 GL_TEXTURE0
, GL_SIGNED_IDENTITY_NV
, GL_RGB
);
755 CombinerInput(GL_COMBINER1_NV
, GL_RGB
, GL_VARIABLE_D_NV
, GL_ZERO
,
756 GL_UNSIGNED_INVERT_NV
, GL_RGB
);
757 CombinerOutput(GL_COMBINER1_NV
, GL_RGB
, GL_DISCARD_NV
, GL_DISCARD_NV
,
758 GL_SPARE0_NV
, GL_NONE
, GL_NONE
, GL_FALSE
,
761 // leave final combiner stage in default mode
762 CombinerParameteri(GL_NUM_GENERAL_COMBINERS_NV
, 2);
766 * \brief Setup ATI version of register combiners for YUV to RGB conversion.
767 * \param uvcos used for saturation and hue adjustment
768 * \param uvsin used for saturation and hue adjustment
770 * ATI called this fragment shader, but the name is confusing in the
771 * light of a very different OpenGL 2.0 extension with the same name
773 static void glSetupYUVCombinersATI(float uvcos
, float uvsin
) {
777 if (!BeginFragmentShader
|| !EndFragmentShader
||
778 !SetFragmentShaderConstant
|| !SampleMap
||
779 !ColorFragmentOp2
|| !ColorFragmentOp3
) {
780 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Combiner (ATI) functions missing!\n");
783 GetIntegerv(GL_NUM_FRAGMENT_REGISTERS_ATI
, &i
);
785 mp_msg(MSGT_VO
, MSGL_ERR
,
786 "[gl] 3 registers needed for YUV combiner (ATI) support (found %i)\n", i
);
787 GetIntegerv (GL_MAX_TEXTURE_UNITS
, &i
);
789 mp_msg(MSGT_VO
, MSGL_ERR
,
790 "[gl] 3 texture units needed for YUV combiner (ATI) support (found %i)\n", i
);
791 fillUVcoeff(ucoef
, vcoef
, uvcos
, uvsin
);
792 BeginFragmentShader();
793 SetFragmentShaderConstant(GL_CON_0_ATI
, ucoef
);
794 SetFragmentShaderConstant(GL_CON_1_ATI
, vcoef
);
795 SampleMap(GL_REG_0_ATI
, GL_TEXTURE0
, GL_SWIZZLE_STR_ATI
);
796 SampleMap(GL_REG_1_ATI
, GL_TEXTURE1
, GL_SWIZZLE_STR_ATI
);
797 SampleMap(GL_REG_2_ATI
, GL_TEXTURE2
, GL_SWIZZLE_STR_ATI
);
798 // UV first, like this green component cannot overflow
799 ColorFragmentOp2(GL_MUL_ATI
, GL_REG_1_ATI
, GL_NONE
, GL_NONE
,
800 GL_REG_1_ATI
, GL_NONE
, GL_BIAS_BIT_ATI
,
801 GL_CON_0_ATI
, GL_NONE
, GL_BIAS_BIT_ATI
);
802 ColorFragmentOp3(GL_MAD_ATI
, GL_REG_2_ATI
, GL_NONE
, GL_4X_BIT_ATI
,
803 GL_REG_2_ATI
, GL_NONE
, GL_BIAS_BIT_ATI
,
804 GL_CON_1_ATI
, GL_NONE
, GL_BIAS_BIT_ATI
,
805 GL_REG_1_ATI
, GL_NONE
, GL_NONE
);
806 ColorFragmentOp2(GL_ADD_ATI
, GL_REG_0_ATI
, GL_NONE
, GL_NONE
,
807 GL_REG_0_ATI
, GL_NONE
, GL_NONE
,
808 GL_REG_2_ATI
, GL_NONE
, GL_NONE
);
813 * \brief helper function for gen_spline_lookup_tex
814 * \param x subpixel-position ((0,1) range) to calculate weights for
815 * \param dst where to store transformed weights, must provide space for 4 GLfloats
817 * calculates the weights and stores them after appropriate transformation
818 * for the scaler fragment program.
820 static void store_weights(float x
, GLfloat
*dst
) {
821 float w0
= (((-1 * x
+ 3) * x
- 3) * x
+ 1) / 6;
822 float w1
= ((( 3 * x
- 6) * x
+ 0) * x
+ 4) / 6;
823 float w2
= (((-3 * x
+ 3) * x
+ 3) * x
+ 1) / 6;
824 float w3
= ((( 1 * x
+ 0) * x
+ 0) * x
+ 0) / 6;
825 *dst
++ = 1 + x
- w1
/ (w0
+ w1
);
826 *dst
++ = 1 - x
+ w3
/ (w2
+ w3
);
831 //! to avoid artefacts this should be rather large
832 #define LOOKUP_BSPLINE_RES (2 * 1024)
834 * \brief creates the 1D lookup texture needed for fast higher-order filtering
835 * \param unit texture unit to attach texture to
837 static void gen_spline_lookup_tex(GLenum unit
) {
838 GLfloat
*tex
= calloc(4 * LOOKUP_BSPLINE_RES
, sizeof(*tex
));
841 for (i
= 0; i
< LOOKUP_BSPLINE_RES
; i
++) {
842 float x
= (float)(i
+ 0.5) / LOOKUP_BSPLINE_RES
;
843 store_weights(x
, tp
);
846 store_weights(0, tex
);
847 store_weights(1, &tex
[4 * (LOOKUP_BSPLINE_RES
- 1)]);
849 TexImage1D(GL_TEXTURE_1D
, 0, GL_RGBA16
, LOOKUP_BSPLINE_RES
, 0, GL_RGBA
, GL_FLOAT
, tex
);
850 TexParameterf(GL_TEXTURE_1D
, GL_TEXTURE_PRIORITY
, 1.0);
851 TexParameteri(GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
852 TexParameteri(GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
853 TexParameteri(GL_TEXTURE_1D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
854 ActiveTexture(GL_TEXTURE0
);
858 static const char *bilin_filt_template
=
859 "TEX yuv.%c, fragment.texcoord[%c], texture[%c], %s;";
861 #define BICUB_FILT_MAIN(textype) \
862 /* first y-interpolation */ \
863 "ADD coord, fragment.texcoord[%c].xyxy, cdelta.xyxw;" \
864 "ADD coord2, fragment.texcoord[%c].xyxy, cdelta.zyzw;" \
865 "TEX a.r, coord.xyxy, texture[%c], "textype";" \
866 "TEX a.g, coord.zwzw, texture[%c], "textype";" \
867 /* second y-interpolation */ \
868 "TEX b.r, coord2.xyxy, texture[%c], "textype";" \
869 "TEX b.g, coord2.zwzw, texture[%c], "textype";" \
870 "LRP a.b, parmy.b, a.rrrr, a.gggg;" \
871 "LRP a.a, parmy.b, b.rrrr, b.gggg;" \
872 /* x-interpolation */ \
873 "LRP yuv.%c, parmx.b, a.bbbb, a.aaaa;"
875 static const char *bicub_filt_template_2D
=
876 "MAD coord.xy, fragment.texcoord[%c], {%e, %e}, {0.5, 0.5};"
877 "TEX parmx, coord.x, texture[%c], 1D;"
878 "MUL cdelta.xz, parmx.rrgg, {-%e, 0, %e, 0};"
879 "TEX parmy, coord.y, texture[%c], 1D;"
880 "MUL cdelta.yw, parmy.rrgg, {0, -%e, 0, %e};"
881 BICUB_FILT_MAIN("2D");
883 static const char *bicub_filt_template_RECT
=
884 "ADD coord, fragment.texcoord[%c], {0.5, 0.5};"
885 "TEX parmx, coord.x, texture[%c], 1D;"
886 "MUL cdelta.xz, parmx.rrgg, {-1, 0, 1, 0};"
887 "TEX parmy, coord.y, texture[%c], 1D;"
888 "MUL cdelta.yw, parmy.rrgg, {0, -1, 0, 1};"
889 BICUB_FILT_MAIN("RECT");
891 #define CALCWEIGHTS(t, s) \
892 "MAD "t", {-0.5, 0.1666, 0.3333, -0.3333}, "s", {1, 0, -0.5, 0.5};" \
893 "MAD "t", "t", "s", {0, 0, -0.5, 0.5};" \
894 "MAD "t", "t", "s", {-0.6666, 0, 0.8333, 0.1666};" \
897 "MAD "t".xy, "t".xyxy, a.xyxy, {1, 1, 0, 0};" \
898 "ADD "t".x, "t".xxxx, "s";" \
899 "SUB "t".y, "t".yyyy, "s";"
901 static const char *bicub_notex_filt_template_2D
=
902 "MAD coord.xy, fragment.texcoord[%c], {%e, %e}, {0.5, 0.5};"
903 "FRC coord.xy, coord.xyxy;"
904 CALCWEIGHTS("parmx", "coord.xxxx")
905 "MUL cdelta.xz, parmx.rrgg, {-%e, 0, %e, 0};"
906 CALCWEIGHTS("parmy", "coord.yyyy")
907 "MUL cdelta.yw, parmy.rrgg, {0, -%e, 0, %e};"
908 BICUB_FILT_MAIN("2D");
910 static const char *bicub_notex_filt_template_RECT
=
911 "ADD coord, fragment.texcoord[%c], {0.5, 0.5};"
912 "FRC coord.xy, coord.xyxy;"
913 CALCWEIGHTS("parmx", "coord.xxxx")
914 "MUL cdelta.xz, parmx.rrgg, {-1, 0, 1, 0};"
915 CALCWEIGHTS("parmy", "coord.yyyy")
916 "MUL cdelta.yw, parmy.rrgg, {0, -1, 0, 1};"
917 BICUB_FILT_MAIN("RECT");
919 #define BICUB_X_FILT_MAIN(textype) \
920 "ADD coord.xy, fragment.texcoord[%c].xyxy, cdelta.xyxy;" \
921 "ADD coord2.xy, fragment.texcoord[%c].xyxy, cdelta.zyzy;" \
922 "TEX a.r, coord, texture[%c], "textype";" \
923 "TEX b.r, coord2, texture[%c], "textype";" \
924 /* x-interpolation */ \
925 "LRP yuv.%c, parmx.b, a.rrrr, b.rrrr;"
927 static const char *bicub_x_filt_template_2D
=
928 "MAD coord.x, fragment.texcoord[%c], {%e}, {0.5};"
929 "TEX parmx, coord, texture[%c], 1D;"
930 "MUL cdelta.xyz, parmx.rrgg, {-%e, 0, %e};"
931 BICUB_X_FILT_MAIN("2D");
933 static const char *bicub_x_filt_template_RECT
=
934 "ADD coord.x, fragment.texcoord[%c], {0.5};"
935 "TEX parmx, coord, texture[%c], 1D;"
936 "MUL cdelta.xyz, parmx.rrgg, {-1, 0, 1};"
937 BICUB_X_FILT_MAIN("RECT");
939 static const char *unsharp_filt_template
=
940 "PARAM dcoord%c = {%e, %e, %e, %e};"
941 "ADD coord, fragment.texcoord[%c].xyxy, dcoord%c;"
942 "SUB coord2, fragment.texcoord[%c].xyxy, dcoord%c;"
943 "TEX a.r, fragment.texcoord[%c], texture[%c], %s;"
944 "TEX b.r, coord.xyxy, texture[%c], %s;"
945 "TEX b.g, coord.zwzw, texture[%c], %s;"
947 "TEX b.b, coord2.xyxy, texture[%c], %s;"
948 "TEX b.g, coord2.zwzw, texture[%c], %s;"
949 "DP3 b, b, {0.25, 0.25, 0.25};"
951 "MAD yuv.%c, b.r, {%e}, a.r;";
953 static const char *unsharp_filt_template2
=
954 "PARAM dcoord%c = {%e, %e, %e, %e};"
955 "PARAM dcoord2%c = {%e, 0, 0, %e};"
956 "ADD coord, fragment.texcoord[%c].xyxy, dcoord%c;"
957 "SUB coord2, fragment.texcoord[%c].xyxy, dcoord%c;"
958 "TEX a.r, fragment.texcoord[%c], texture[%c], %s;"
959 "TEX b.r, coord.xyxy, texture[%c], %s;"
960 "TEX b.g, coord.zwzw, texture[%c], %s;"
962 "TEX b.b, coord2.xyxy, texture[%c], %s;"
963 "TEX b.g, coord2.zwzw, texture[%c], %s;"
966 "ADD coord, fragment.texcoord[%c].xyxy, dcoord2%c;"
967 "SUB coord2, fragment.texcoord[%c].xyxy, dcoord2%c;"
968 "TEX b.r, coord.xyxy, texture[%c], %s;"
969 "TEX b.g, coord.zwzw, texture[%c], %s;"
971 "TEX b.b, coord2.xyxy, texture[%c], %s;"
972 "TEX b.g, coord2.zwzw, texture[%c], %s;"
973 "DP4 b.r, b, {-0.1171875, -0.1171875, -0.1171875, -0.09765625};"
974 "MAD b.r, a.r, {0.859375}, b.r;"
975 "MAD yuv.%c, b.r, {%e}, a.r;";
977 static const char *yuv_prog_template
=
978 "PARAM ycoef = {%e, %e, %e};"
979 "PARAM ucoef = {%e, %e, %e};"
980 "PARAM vcoef = {%e, %e, %e};"
981 "PARAM offsets = {%e, %e, %e};"
983 "MAD res.rgb, yuv.rrrr, ycoef, offsets;"
984 "MAD res.rgb, yuv.gggg, ucoef, res;"
985 "MAD result.color.rgb, yuv.bbbb, vcoef, res;"
988 static const char *yuv_pow_prog_template
=
989 "PARAM ycoef = {%e, %e, %e};"
990 "PARAM ucoef = {%e, %e, %e};"
991 "PARAM vcoef = {%e, %e, %e};"
992 "PARAM offsets = {%e, %e, %e};"
993 "PARAM gamma = {%e, %e, %e};"
995 "MAD res.rgb, yuv.rrrr, ycoef, offsets;"
996 "MAD res.rgb, yuv.gggg, ucoef, res;"
997 "MAD_SAT res.rgb, yuv.bbbb, vcoef, res;"
998 "POW result.color.r, res.r, gamma.r;"
999 "POW result.color.g, res.g, gamma.g;"
1000 "POW result.color.b, res.b, gamma.b;"
1003 static const char *yuv_lookup_prog_template
=
1004 "PARAM ycoef = {%e, %e, %e, 0};"
1005 "PARAM ucoef = {%e, %e, %e, 0};"
1006 "PARAM vcoef = {%e, %e, %e, 0};"
1007 "PARAM offsets = {%e, %e, %e, 0.125};"
1009 "MAD res, yuv.rrrr, ycoef, offsets;"
1010 "MAD res.rgb, yuv.gggg, ucoef, res;"
1011 "MAD res.rgb, yuv.bbbb, vcoef, res;"
1012 "TEX result.color.r, res.raaa, texture[%c], 2D;"
1013 "ADD res.a, res.a, 0.25;"
1014 "TEX result.color.g, res.gaaa, texture[%c], 2D;"
1015 "ADD res.a, res.a, 0.25;"
1016 "TEX result.color.b, res.baaa, texture[%c], 2D;"
1019 static const char *yuv_lookup3d_prog_template
=
1020 "TEX result.color, yuv, texture[%c], 3D;"
1024 * \brief creates and initializes helper textures needed for scaling texture read
1025 * \param scaler scaler type to create texture for
1026 * \param texu contains next free texture unit number
1027 * \param texs texture unit ids for the scaler are stored in this array
1029 static void create_scaler_textures(int scaler
, int *texu
, char *texs
) {
1031 case YUV_SCALER_BILIN
:
1032 case YUV_SCALER_BICUB_NOTEX
:
1033 case YUV_SCALER_UNSHARP
:
1034 case YUV_SCALER_UNSHARP2
:
1036 case YUV_SCALER_BICUB
:
1037 case YUV_SCALER_BICUB_X
:
1038 texs
[0] = (*texu
)++;
1039 gen_spline_lookup_tex(GL_TEXTURE0
+ texs
[0]);
1043 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] unknown scaler type %i\n", scaler
);
1047 //! resolution of texture for gamma lookup table
1048 #define LOOKUP_RES 512
1049 //! resolution for 3D yuv->rgb conversion lookup table
1050 #define LOOKUP_3DRES 32
1052 * \brief creates and initializes helper textures needed for yuv conversion
1053 * \param params struct containing parameters like brightness, gamma, ...
1054 * \param texu contains next free texture unit number
1055 * \param texs texture unit ids for the conversion are stored in this array
1057 static void create_conv_textures(gl_conversion_params_t
*params
, int *texu
, char *texs
) {
1058 unsigned char *lookup_data
= NULL
;
1059 int conv
= YUV_CONVERSION(params
->type
);
1061 case YUV_CONVERSION_FRAGMENT
:
1062 case YUV_CONVERSION_FRAGMENT_POW
:
1064 case YUV_CONVERSION_FRAGMENT_LOOKUP
:
1065 texs
[0] = (*texu
)++;
1066 ActiveTexture(GL_TEXTURE0
+ texs
[0]);
1067 lookup_data
= malloc(4 * LOOKUP_RES
);
1068 mp_gen_gamma_map(lookup_data
, LOOKUP_RES
, params
->csp_params
.rgamma
);
1069 mp_gen_gamma_map(&lookup_data
[LOOKUP_RES
], LOOKUP_RES
, params
->csp_params
.ggamma
);
1070 mp_gen_gamma_map(&lookup_data
[2 * LOOKUP_RES
], LOOKUP_RES
, params
->csp_params
.bgamma
);
1071 glCreateClearTex(GL_TEXTURE_2D
, GL_LUMINANCE8
, GL_LUMINANCE
, GL_UNSIGNED_BYTE
, GL_LINEAR
,
1073 glUploadTex(GL_TEXTURE_2D
, GL_LUMINANCE
, GL_UNSIGNED_BYTE
, lookup_data
,
1074 LOOKUP_RES
, 0, 0, LOOKUP_RES
, 4, 0);
1075 ActiveTexture(GL_TEXTURE0
);
1078 case YUV_CONVERSION_FRAGMENT_LOOKUP3D
:
1080 int sz
= LOOKUP_3DRES
+ 2; // texture size including borders
1082 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] Missing 3D texture function!\n");
1085 texs
[0] = (*texu
)++;
1086 ActiveTexture(GL_TEXTURE0
+ texs
[0]);
1087 lookup_data
= malloc(3 * sz
* sz
* sz
);
1088 mp_gen_yuv2rgb_map(¶ms
->csp_params
, lookup_data
, LOOKUP_3DRES
);
1089 glAdjustAlignment(sz
);
1090 PixelStorei(GL_UNPACK_ROW_LENGTH
, 0);
1091 TexImage3D(GL_TEXTURE_3D
, 0, 3, sz
, sz
, sz
, 1,
1092 GL_RGB
, GL_UNSIGNED_BYTE
, lookup_data
);
1093 TexParameterf(GL_TEXTURE_3D
, GL_TEXTURE_PRIORITY
, 1.0);
1094 TexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
1095 TexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
1096 TexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_WRAP_S
, GL_CLAMP
);
1097 TexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_WRAP_T
, GL_CLAMP
);
1098 TexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_WRAP_R
, GL_CLAMP
);
1099 ActiveTexture(GL_TEXTURE0
);
1104 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] unknown conversion type %i\n", conv
);
1111 * \brief adds a scaling texture read at the current fragment program position
1112 * \param scaler type of scaler to insert
1113 * \param prog_pos current position in fragment program
1114 * \param remain how many bytes remain in the buffer given by prog_pos
1115 * \param texs array containing the texture unit identifiers for this scaler
1116 * \param in_tex texture unit the scaler should read from
1117 * \param out_comp component of the yuv variable the scaler stores the result in
1118 * \param rect if rectangular (pixel) adressing should be used for in_tex
1119 * \param texw width of the in_tex texture
1120 * \param texh height of the in_tex texture
1121 * \param strength strength of filter effect if the scaler does some kind of filtering
1123 static void add_scaler(int scaler
, char **prog_pos
, int *remain
, char *texs
,
1124 char in_tex
, char out_comp
, int rect
, int texw
, int texh
,
1126 const char *ttype
= rect
? "RECT" : "2D";
1127 const float ptw
= rect
? 1.0 : 1.0 / texw
;
1128 const float pth
= rect
? 1.0 : 1.0 / texh
;
1130 case YUV_SCALER_BILIN
:
1131 snprintf(*prog_pos
, *remain
, bilin_filt_template
, out_comp
, in_tex
,
1134 case YUV_SCALER_BICUB
:
1136 snprintf(*prog_pos
, *remain
, bicub_filt_template_RECT
,
1137 in_tex
, texs
[0], texs
[0],
1138 in_tex
, in_tex
, in_tex
, in_tex
, in_tex
, in_tex
, out_comp
);
1140 snprintf(*prog_pos
, *remain
, bicub_filt_template_2D
,
1141 in_tex
, (float)texw
, (float)texh
,
1142 texs
[0], ptw
, ptw
, texs
[0], pth
, pth
,
1143 in_tex
, in_tex
, in_tex
, in_tex
, in_tex
, in_tex
, out_comp
);
1145 case YUV_SCALER_BICUB_X
:
1147 snprintf(*prog_pos
, *remain
, bicub_x_filt_template_RECT
,
1149 in_tex
, in_tex
, in_tex
, in_tex
, out_comp
);
1151 snprintf(*prog_pos
, *remain
, bicub_x_filt_template_2D
,
1152 in_tex
, (float)texw
,
1154 in_tex
, in_tex
, in_tex
, in_tex
, out_comp
);
1156 case YUV_SCALER_BICUB_NOTEX
:
1158 snprintf(*prog_pos
, *remain
, bicub_notex_filt_template_RECT
,
1160 in_tex
, in_tex
, in_tex
, in_tex
, in_tex
, in_tex
, out_comp
);
1162 snprintf(*prog_pos
, *remain
, bicub_notex_filt_template_2D
,
1163 in_tex
, (float)texw
, (float)texh
, ptw
, ptw
, pth
, pth
,
1164 in_tex
, in_tex
, in_tex
, in_tex
, in_tex
, in_tex
, out_comp
);
1166 case YUV_SCALER_UNSHARP
:
1167 snprintf(*prog_pos
, *remain
, unsharp_filt_template
,
1168 out_comp
, 0.5 * ptw
, 0.5 * pth
, 0.5 * ptw
, -0.5 * pth
,
1169 in_tex
, out_comp
, in_tex
, out_comp
, in_tex
,
1170 in_tex
, ttype
, in_tex
, ttype
, in_tex
, ttype
, in_tex
, ttype
,
1171 in_tex
, ttype
, out_comp
, strength
);
1173 case YUV_SCALER_UNSHARP2
:
1174 snprintf(*prog_pos
, *remain
, unsharp_filt_template2
,
1175 out_comp
, 1.2 * ptw
, 1.2 * pth
, 1.2 * ptw
, -1.2 * pth
,
1176 out_comp
, 1.5 * ptw
, 1.5 * pth
,
1177 in_tex
, out_comp
, in_tex
, out_comp
, in_tex
,
1178 in_tex
, ttype
, in_tex
, ttype
, in_tex
, ttype
, in_tex
, ttype
,
1179 in_tex
, ttype
, in_tex
, out_comp
, in_tex
, out_comp
,
1180 in_tex
, ttype
, in_tex
, ttype
, in_tex
, ttype
,
1181 in_tex
, ttype
, out_comp
, strength
);
1184 *remain
-= strlen(*prog_pos
);
1185 *prog_pos
+= strlen(*prog_pos
);
1188 static const struct {
1193 {"instructions", 0x88A0, 0x88A1},
1194 {"native instructions", 0x88A2, 0x88A3},
1195 {"temporaries", 0x88A4, 0x88A5},
1196 {"native temporaries", 0x88A6, 0x88A7},
1197 {"parameters", 0x88A8, 0x88A9},
1198 {"native parameters", 0x88AA, 0x88AB},
1199 {"attribs", 0x88AC, 0x88AD},
1200 {"native attribs", 0x88AE, 0x88AF},
1201 {"ALU instructions", 0x8805, 0x880B},
1202 {"TEX instructions", 0x8806, 0x880C},
1203 {"TEX indirections", 0x8807, 0x880D},
1204 {"native ALU instructions", 0x8808, 0x880E},
1205 {"native TEX instructions", 0x8809, 0x880F},
1206 {"native TEX indirections", 0x880A, 0x8810},
1211 * \brief load the specified GPU Program
1212 * \param target program target to load into, only GL_FRAGMENT_PROGRAM is tested
1213 * \param prog program string
1214 * \return 1 on success, 0 otherwise
1216 int loadGPUProgram(GLenum target
, char *prog
) {
1218 GLint cur
= 0, max
= 0, err
= 0;
1219 if (!ProgramString
) {
1220 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] Missing GPU program function\n");
1223 ProgramString(target
, GL_PROGRAM_FORMAT_ASCII
, strlen(prog
), prog
);
1224 GetIntegerv(GL_PROGRAM_ERROR_POSITION
, &err
);
1226 mp_msg(MSGT_VO
, MSGL_ERR
,
1227 "[gl] Error compiling fragment program, make sure your card supports\n"
1228 "[gl] GL_ARB_fragment_program (use glxinfo to check).\n"
1229 "[gl] Error message:\n %s at %.10s\n",
1230 GetString(GL_PROGRAM_ERROR_STRING
), &prog
[err
]);
1233 if (!GetProgramiv
|| !mp_msg_test(MSGT_VO
, MSGL_DBG2
))
1235 mp_msg(MSGT_VO
, MSGL_V
, "[gl] Program statistics:\n");
1236 for (i
= 0; progstats
[i
].name
; i
++) {
1237 GetProgramiv(target
, progstats
[i
].cur
, &cur
);
1238 GetProgramiv(target
, progstats
[i
].max
, &max
);
1239 mp_msg(MSGT_VO
, MSGL_V
, "[gl] %s: %i/%i\n", progstats
[i
].name
, cur
, max
);
1244 #define MAX_PROGSZ (1024*1024)
1247 * \brief setup a fragment program that will do YUV->RGB conversion
1248 * \param parms struct containing parameters like conversion and scaler type,
1251 static void glSetupYUVFragprog(gl_conversion_params_t
*params
) {
1252 int type
= params
->type
;
1253 int texw
= params
->texw
;
1254 int texh
= params
->texh
;
1255 int rect
= params
->target
== GL_TEXTURE_RECTANGLE
;
1256 static const char prog_hdr
[] =
1258 "OPTION ARB_precision_hint_fastest;"
1259 // all scaler variables must go here so they aren't defined
1260 // multiple times when the same scaler is used more than once
1261 "TEMP coord, coord2, cdelta, parmx, parmy, a, b, yuv;";
1263 char *yuv_prog
, *prog_pos
;
1265 char lum_scale_texs
[1];
1266 char chrom_scale_texs
[1];
1269 // this is the conversion matrix, with y, u, v factors
1270 // for red, green, blue and the constant offsets
1271 float yuv2rgb
[3][4];
1272 create_conv_textures(params
, &cur_texu
, conv_texs
);
1273 create_scaler_textures(YUV_LUM_SCALER(type
), &cur_texu
, lum_scale_texs
);
1274 if (YUV_CHROM_SCALER(type
) == YUV_LUM_SCALER(type
))
1275 memcpy(chrom_scale_texs
, lum_scale_texs
, sizeof(chrom_scale_texs
));
1277 create_scaler_textures(YUV_CHROM_SCALER(type
), &cur_texu
, chrom_scale_texs
);
1278 GetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS
, &i
);
1280 mp_msg(MSGT_VO
, MSGL_ERR
,
1281 "[gl] %i texture units needed for this type of YUV fragment support (found %i)\n",
1283 if (!ProgramString
) {
1284 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] ProgramString function missing!\n");
1287 yuv_prog
= malloc(MAX_PROGSZ
);
1288 strcpy(yuv_prog
, prog_hdr
);
1289 prog_pos
= yuv_prog
+ sizeof(prog_hdr
) - 1;
1290 prog_remain
= MAX_PROGSZ
- sizeof(prog_hdr
);
1291 add_scaler(YUV_LUM_SCALER(type
), &prog_pos
, &prog_remain
, lum_scale_texs
,
1292 '0', 'r', rect
, texw
, texh
, params
->filter_strength
);
1293 add_scaler(YUV_CHROM_SCALER(type
), &prog_pos
, &prog_remain
, chrom_scale_texs
,
1294 '1', 'g', rect
, params
->chrom_texw
, params
->chrom_texh
, params
->filter_strength
);
1295 add_scaler(YUV_CHROM_SCALER(type
), &prog_pos
, &prog_remain
, chrom_scale_texs
,
1296 '2', 'b', rect
, params
->chrom_texw
, params
->chrom_texh
, params
->filter_strength
);
1297 mp_get_yuv2rgb_coeffs(¶ms
->csp_params
, yuv2rgb
);
1298 switch (YUV_CONVERSION(type
)) {
1299 case YUV_CONVERSION_FRAGMENT
:
1300 snprintf(prog_pos
, prog_remain
, yuv_prog_template
,
1301 yuv2rgb
[ROW_R
][COL_Y
], yuv2rgb
[ROW_G
][COL_Y
], yuv2rgb
[ROW_B
][COL_Y
],
1302 yuv2rgb
[ROW_R
][COL_U
], yuv2rgb
[ROW_G
][COL_U
], yuv2rgb
[ROW_B
][COL_U
],
1303 yuv2rgb
[ROW_R
][COL_V
], yuv2rgb
[ROW_G
][COL_V
], yuv2rgb
[ROW_B
][COL_V
],
1304 yuv2rgb
[ROW_R
][COL_C
], yuv2rgb
[ROW_G
][COL_C
], yuv2rgb
[ROW_B
][COL_C
]);
1306 case YUV_CONVERSION_FRAGMENT_POW
:
1307 snprintf(prog_pos
, prog_remain
, yuv_pow_prog_template
,
1308 yuv2rgb
[ROW_R
][COL_Y
], yuv2rgb
[ROW_G
][COL_Y
], yuv2rgb
[ROW_B
][COL_Y
],
1309 yuv2rgb
[ROW_R
][COL_U
], yuv2rgb
[ROW_G
][COL_U
], yuv2rgb
[ROW_B
][COL_U
],
1310 yuv2rgb
[ROW_R
][COL_V
], yuv2rgb
[ROW_G
][COL_V
], yuv2rgb
[ROW_B
][COL_V
],
1311 yuv2rgb
[ROW_R
][COL_C
], yuv2rgb
[ROW_G
][COL_C
], yuv2rgb
[ROW_B
][COL_C
],
1312 (float)1.0 / params
->csp_params
.rgamma
, (float)1.0 / params
->csp_params
.bgamma
, (float)1.0 / params
->csp_params
.bgamma
);
1314 case YUV_CONVERSION_FRAGMENT_LOOKUP
:
1315 snprintf(prog_pos
, prog_remain
, yuv_lookup_prog_template
,
1316 yuv2rgb
[ROW_R
][COL_Y
], yuv2rgb
[ROW_G
][COL_Y
], yuv2rgb
[ROW_B
][COL_Y
],
1317 yuv2rgb
[ROW_R
][COL_U
], yuv2rgb
[ROW_G
][COL_U
], yuv2rgb
[ROW_B
][COL_U
],
1318 yuv2rgb
[ROW_R
][COL_V
], yuv2rgb
[ROW_G
][COL_V
], yuv2rgb
[ROW_B
][COL_V
],
1319 yuv2rgb
[ROW_R
][COL_C
], yuv2rgb
[ROW_G
][COL_C
], yuv2rgb
[ROW_B
][COL_C
],
1320 conv_texs
[0], conv_texs
[0], conv_texs
[0]);
1322 case YUV_CONVERSION_FRAGMENT_LOOKUP3D
:
1323 snprintf(prog_pos
, prog_remain
, yuv_lookup3d_prog_template
, conv_texs
[0]);
1326 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] unknown conversion type %i\n", YUV_CONVERSION(type
));
1329 mp_msg(MSGT_VO
, MSGL_DBG2
, "[gl] generated fragment program:\n%s\n", yuv_prog
);
1330 loadGPUProgram(GL_FRAGMENT_PROGRAM
, yuv_prog
);
1335 * \brief setup YUV->RGB conversion
1336 * \param parms struct containing parameters like conversion and scaler type,
1338 * \ingroup glconversion
1340 void glSetupYUVConversion(gl_conversion_params_t
*params
) {
1341 float uvcos
= params
->csp_params
.saturation
* cos(params
->csp_params
.hue
);
1342 float uvsin
= params
->csp_params
.saturation
* sin(params
->csp_params
.hue
);
1343 switch (YUV_CONVERSION(params
->type
)) {
1344 case YUV_CONVERSION_COMBINERS
:
1345 glSetupYUVCombiners(uvcos
, uvsin
);
1347 case YUV_CONVERSION_COMBINERS_ATI
:
1348 glSetupYUVCombinersATI(uvcos
, uvsin
);
1350 case YUV_CONVERSION_FRAGMENT_LOOKUP
:
1351 case YUV_CONVERSION_FRAGMENT_LOOKUP3D
:
1352 case YUV_CONVERSION_FRAGMENT
:
1353 case YUV_CONVERSION_FRAGMENT_POW
:
1354 glSetupYUVFragprog(params
);
1356 case YUV_CONVERSION_NONE
:
1359 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] unknown conversion type %i\n", YUV_CONVERSION(params
->type
));
1364 * \brief enable the specified YUV conversion
1365 * \param target texture target for Y, U and V textures (e.g. GL_TEXTURE_2D)
1366 * \param type type of YUV conversion
1367 * \ingroup glconversion
1369 void glEnableYUVConversion(GLenum target
, int type
) {
1370 switch (YUV_CONVERSION(type
)) {
1371 case YUV_CONVERSION_COMBINERS
:
1372 ActiveTexture(GL_TEXTURE1
);
1374 ActiveTexture(GL_TEXTURE2
);
1376 ActiveTexture(GL_TEXTURE0
);
1377 Enable(GL_REGISTER_COMBINERS_NV
);
1379 case YUV_CONVERSION_COMBINERS_ATI
:
1380 ActiveTexture(GL_TEXTURE1
);
1382 ActiveTexture(GL_TEXTURE2
);
1384 ActiveTexture(GL_TEXTURE0
);
1385 Enable(GL_FRAGMENT_SHADER_ATI
);
1387 case YUV_CONVERSION_FRAGMENT_LOOKUP3D
:
1388 case YUV_CONVERSION_FRAGMENT_LOOKUP
:
1389 case YUV_CONVERSION_FRAGMENT_POW
:
1390 case YUV_CONVERSION_FRAGMENT
:
1391 case YUV_CONVERSION_NONE
:
1392 Enable(GL_FRAGMENT_PROGRAM
);
1398 * \brief disable the specified YUV conversion
1399 * \param target texture target for Y, U and V textures (e.g. GL_TEXTURE_2D)
1400 * \param type type of YUV conversion
1401 * \ingroup glconversion
1403 void glDisableYUVConversion(GLenum target
, int type
) {
1404 switch (YUV_CONVERSION(type
)) {
1405 case YUV_CONVERSION_COMBINERS
:
1406 ActiveTexture(GL_TEXTURE1
);
1408 ActiveTexture(GL_TEXTURE2
);
1410 ActiveTexture(GL_TEXTURE0
);
1411 Disable(GL_REGISTER_COMBINERS_NV
);
1413 case YUV_CONVERSION_COMBINERS_ATI
:
1414 ActiveTexture(GL_TEXTURE1
);
1416 ActiveTexture(GL_TEXTURE2
);
1418 ActiveTexture(GL_TEXTURE0
);
1419 Disable(GL_FRAGMENT_SHADER_ATI
);
1421 case YUV_CONVERSION_FRAGMENT_LOOKUP3D
:
1422 case YUV_CONVERSION_FRAGMENT_LOOKUP
:
1423 case YUV_CONVERSION_FRAGMENT_POW
:
1424 case YUV_CONVERSION_FRAGMENT
:
1425 case YUV_CONVERSION_NONE
:
1426 Disable(GL_FRAGMENT_PROGRAM
);
1432 * \brief draw a texture part at given 2D coordinates
1433 * \param x screen top coordinate
1434 * \param y screen left coordinate
1435 * \param w screen width coordinate
1436 * \param h screen height coordinate
1437 * \param tx texture top coordinate in pixels
1438 * \param ty texture left coordinate in pixels
1439 * \param tw texture part width in pixels
1440 * \param th texture part height in pixels
1441 * \param sx width of texture in pixels
1442 * \param sy height of texture in pixels
1443 * \param rect_tex whether this texture uses texture_rectangle extension
1444 * \param is_yv12 if != 0, also draw the textures from units 1 and 2,
1445 * bits 8 - 15 and 16 - 23 specify the x and y scaling of those textures
1446 * \param flip flip the texture upside down
1447 * \ingroup gltexture
1449 void glDrawTex(GLfloat x
, GLfloat y
, GLfloat w
, GLfloat h
,
1450 GLfloat tx
, GLfloat ty
, GLfloat tw
, GLfloat th
,
1451 int sx
, int sy
, int rect_tex
, int is_yv12
, int flip
) {
1452 int chroma_x_shift
= (is_yv12
>> 8) & 31;
1453 int chroma_y_shift
= (is_yv12
>> 16) & 31;
1454 GLfloat xscale
= 1 << chroma_x_shift
;
1455 GLfloat yscale
= 1 << chroma_y_shift
;
1456 GLfloat tx2
= tx
/ xscale
, ty2
= ty
/ yscale
, tw2
= tw
/ xscale
, th2
= th
/ yscale
;
1458 tx
/= sx
; ty
/= sy
; tw
/= sx
; th
/= sy
;
1459 tx2
= tx
, ty2
= ty
, tw2
= tw
, th2
= th
;
1468 MultiTexCoord2f(GL_TEXTURE1
, tx2
, ty2
);
1469 MultiTexCoord2f(GL_TEXTURE2
, tx2
, ty2
);
1472 TexCoord2f(tx
, ty
+ th
);
1474 MultiTexCoord2f(GL_TEXTURE1
, tx2
, ty2
+ th2
);
1475 MultiTexCoord2f(GL_TEXTURE2
, tx2
, ty2
+ th2
);
1478 TexCoord2f(tx
+ tw
, ty
+ th
);
1480 MultiTexCoord2f(GL_TEXTURE1
, tx2
+ tw2
, ty2
+ th2
);
1481 MultiTexCoord2f(GL_TEXTURE2
, tx2
+ tw2
, ty2
+ th2
);
1483 Vertex2f(x
+ w
, y
+ h
);
1484 TexCoord2f(tx
+ tw
, ty
);
1486 MultiTexCoord2f(GL_TEXTURE1
, tx2
+ tw2
, ty2
);
1487 MultiTexCoord2f(GL_TEXTURE2
, tx2
+ tw2
, ty2
);
1493 #ifdef CONFIG_GL_WIN32
1494 #include "w32_common.h"
1496 * \brief little helper since wglGetProcAddress definition does not fit our
1498 * \param procName name of function to look up
1499 * \return function pointer returned by wglGetProcAddress
1501 static void *w32gpa(const GLubyte
*procName
) {
1503 void *res
= wglGetProcAddress(procName
);
1504 if (res
) return res
;
1505 oglmod
= GetModuleHandle("opengl32.dll");
1506 return GetProcAddress(oglmod
, procName
);
1509 static int setGlWindow_w32(MPGLContext
*ctx
)
1511 HWND win
= vo_w32_window
;
1512 int *vinfo
= &ctx
->vinfo
.w32
;
1513 HGLRC
*context
= &ctx
->context
.w32
;
1515 HDC windc
= vo_w32_get_dc(win
);
1516 HGLRC new_context
= 0;
1517 int keep_context
= 0;
1518 int res
= SET_WINDOW_FAILED
;
1520 // should only be needed when keeping context, but not doing glFinish
1521 // can cause flickering even when we do not keep it.
1524 new_vinfo
= GetPixelFormat(windc
);
1525 if (*context
&& *vinfo
&& new_vinfo
&& *vinfo
== new_vinfo
) {
1526 // we can keep the wglContext
1527 new_context
= *context
;
1531 new_context
= wglCreateContext(windc
);
1533 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Could not create GL context!\n");
1539 if (!wglMakeCurrent(windc
, new_context
)) {
1540 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Could not set GL context!\n");
1541 if (!keep_context
) {
1542 wglDeleteContext(new_context
);
1548 vo_w32_window
= win
;
1551 GetClientRect(win
, &rect
);
1552 vo_dwidth
= rect
.right
;
1553 vo_dheight
= rect
.bottom
;
1555 if (!keep_context
) {
1557 wglDeleteContext(*context
);
1558 *context
= new_context
;
1560 getFunctions(w32gpa
, NULL
);
1562 // and inform that reinit is neccessary
1563 res
= SET_WINDOW_REINIT
;
1565 res
= SET_WINDOW_OK
;
1568 vo_w32_release_dc(win
, windc
);
1572 static void releaseGlContext_w32(MPGLContext
*ctx
) {
1573 int *vinfo
= &ctx
->vinfo
.w32
;
1574 HGLRC
*context
= &ctx
->context
.w32
;
1577 wglMakeCurrent(0, 0);
1578 wglDeleteContext(*context
);
1583 static void swapGlBuffers_w32(MPGLContext
*ctx
) {
1584 HDC vo_hdc
= vo_w32_get_dc(vo_w32_window
);
1585 SwapBuffers(vo_hdc
);
1586 vo_w32_release_dc(vo_w32_window
, vo_hdc
);
1589 #ifdef CONFIG_GL_X11
1593 #include "x11_common.h"
1595 * \brief find address of a linked function
1596 * \param s name of function to find
1597 * \return address of function or NULL if not found
1599 static void *getdladdr(const char *s
) {
1602 void *handle
= dlopen(NULL
, RTLD_LAZY
);
1605 ret
= dlsym(handle
, s
);
1612 * \brief Returns the XVisualInfo associated with Window win.
1613 * \param win Window whose XVisualInfo is returne.
1614 * \return XVisualInfo of the window. Caller must use XFree to free it.
1616 static XVisualInfo
*getWindowVisualInfo(Window win
) {
1617 XWindowAttributes xw_attr
;
1618 XVisualInfo vinfo_template
;
1620 XGetWindowAttributes(mDisplay
, win
, &xw_attr
);
1621 vinfo_template
.visualid
= XVisualIDFromVisual(xw_attr
.visual
);
1622 return XGetVisualInfo(mDisplay
, VisualIDMask
, &vinfo_template
, &tmp
);
1625 static void appendstr(char **dst
, const char *str
)
1631 newsize
= strlen(*dst
) + 1 + strlen(str
) + 1;
1632 newstr
= realloc(*dst
, newsize
);
1641 * \brief Changes the window in which video is displayed.
1642 * If possible only transfers the context to the new window, otherwise
1643 * creates a new one, which must be initialized by the caller.
1644 * \param vinfo Currently used visual.
1645 * \param context Currently used context.
1646 * \param win window that should be used for drawing.
1647 * \return one of SET_WINDOW_FAILED, SET_WINDOW_OK or SET_WINDOW_REINIT.
1648 * In case of SET_WINDOW_REINIT the context could not be transfered
1649 * and the caller must initialize it correctly.
1650 * \ingroup glcontext
1652 static int setGlWindow_x11(MPGLContext
*ctx
)
1654 XVisualInfo
**vinfo
= &ctx
->vinfo
.x11
;
1655 GLXContext
*context
= &ctx
->context
.x11
;
1656 Window win
= vo_window
;
1657 XVisualInfo
*new_vinfo
;
1658 GLXContext new_context
= NULL
;
1659 int keep_context
= 0;
1661 // should only be needed when keeping context, but not doing glFinish
1662 // can cause flickering even when we do not keep it.
1665 new_vinfo
= getWindowVisualInfo(win
);
1666 if (*context
&& *vinfo
&& new_vinfo
&&
1667 (*vinfo
)->visualid
== new_vinfo
->visualid
) {
1668 // we can keep the GLXContext
1669 new_context
= *context
;
1675 new_context
= glXCreateContext(mDisplay
, new_vinfo
, NULL
, True
);
1677 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Could not create GLX context!\n");
1679 return SET_WINDOW_FAILED
;
1684 if (!glXMakeCurrent(mDisplay
, vo_window
, new_context
)) {
1685 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Could not set GLX context!\n");
1686 if (!keep_context
) {
1687 glXDestroyContext(mDisplay
, new_context
);
1690 return SET_WINDOW_FAILED
;
1695 vo_x11_update_geometry();
1696 if (!keep_context
) {
1697 void *(*getProcAddress
)(const GLubyte
*);
1698 const char *(*glXExtStr
)(Display
*, int);
1699 char *glxstr
= strdup("");
1701 glXDestroyContext(mDisplay
, *context
);
1702 *context
= new_context
;
1706 getProcAddress
= getdladdr("glXGetProcAddress");
1707 if (!getProcAddress
)
1708 getProcAddress
= getdladdr("glXGetProcAddressARB");
1709 if (!getProcAddress
)
1710 getProcAddress
= (void *)getdladdr
;
1711 glXExtStr
= getdladdr("glXQueryExtensionsString");
1713 appendstr(&glxstr
, glXExtStr(mDisplay
, DefaultScreen(mDisplay
)));
1714 glXExtStr
= getdladdr("glXGetClientString");
1716 appendstr(&glxstr
, glXExtStr(mDisplay
, GLX_EXTENSIONS
));
1717 glXExtStr
= getdladdr("glXGetServerString");
1719 appendstr(&glxstr
, glXExtStr(mDisplay
, GLX_EXTENSIONS
));
1721 getFunctions(getProcAddress
, glxstr
);
1724 // and inform that reinit is neccessary
1725 return SET_WINDOW_REINIT
;
1727 return SET_WINDOW_OK
;
1731 * \brief free the VisualInfo and GLXContext of an OpenGL context.
1732 * \ingroup glcontext
1734 static void releaseGlContext_x11(MPGLContext
*ctx
) {
1735 XVisualInfo
**vinfo
= &ctx
->vinfo
.x11
;
1736 GLXContext
*context
= &ctx
->context
.x11
;
1743 glXMakeCurrent(mDisplay
, None
, NULL
);
1744 glXDestroyContext(mDisplay
, *context
);
1749 static void swapGlBuffers_x11(MPGLContext
*ctx
) {
1750 glXSwapBuffers(mDisplay
, vo_window
);
1753 static int x11_check_events(void) {
1754 return vo_x11_check_events(mDisplay
);
1757 static void gl_update_xinerama_info(void)
1759 update_xinerama_info(global_vo
);
1762 static void gl_border(void)
1767 static void gl_fullscreen(void)
1769 vo_x11_fullscreen();
1772 static void gl_ontop(void)
1778 int init_mpglcontext(MPGLContext
*ctx
, enum MPGLType type
) {
1779 memset(ctx
, 0, sizeof(*ctx
));
1781 switch (ctx
->type
) {
1782 #ifdef CONFIG_GL_WIN32
1784 ctx
->setGlWindow
= setGlWindow_w32
;
1785 ctx
->releaseGlContext
= releaseGlContext_w32
;
1786 ctx
->swapGlBuffers
= swapGlBuffers_w32
;
1787 ctx
->update_xinerama_info
= w32_update_xinerama_info
;
1788 ctx
->border
= vo_w32_border
;
1789 ctx
->check_events
= vo_w32_check_events
;
1790 ctx
->fullscreen
= vo_w32_fullscreen
;
1791 ctx
->ontop
= vo_w32_ontop
;
1792 return vo_w32_init();
1794 #ifdef CONFIG_GL_X11
1796 ctx
->setGlWindow
= setGlWindow_x11
;
1797 ctx
->releaseGlContext
= releaseGlContext_x11
;
1798 ctx
->swapGlBuffers
= swapGlBuffers_x11
;
1799 ctx
->update_xinerama_info
= gl_update_xinerama_info
;
1800 ctx
->border
= gl_border
;
1801 ctx
->check_events
= x11_check_events
;
1802 ctx
->fullscreen
= gl_fullscreen
;
1803 ctx
->ontop
= gl_ontop
;
1811 void uninit_mpglcontext(MPGLContext
*ctx
) {
1812 ctx
->releaseGlContext(ctx
);
1813 switch (ctx
->type
) {
1814 #ifdef CONFIG_GL_WIN32
1819 #ifdef CONFIG_GL_X11
1825 memset(ctx
, 0, sizeof(*ctx
));