2 * common OpenGL routines
4 * copyleft (C) 2005-2010 Reimar Döffinger <Reimar.Doeffinger@gmx.de>
5 * Special thanks go to the xine team and Matthias Hopf, whose video_out_opengl.c
6 * gave me lots of good ideas.
8 * This file is part of MPlayer.
10 * MPlayer is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * MPlayer is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * You can alternatively redistribute this file and/or
25 * modify it under the terms of the GNU Lesser General Public
26 * License as published by the Free Software Foundation; either
27 * version 2.1 of the License, or (at your option) any later version.
32 * \brief OpenGL helper functions used by vo_gl.c and vo_gl2.c
43 #include "gl_common.h"
44 #include "old_vo_wrapper.h"
47 #include "pnm_loader.h"
50 //! \defgroup glgeneral OpenGL general helper functions
52 // GLU has this as gluErrorString (we don't use GLU, as it is legacy-OpenGL)
53 static const char *gl_error_to_string(GLenum error
)
56 case GL_INVALID_ENUM
: return "INVALID_ENUM";
57 case GL_INVALID_VALUE
: return "INVALID_VALUE";
58 case GL_INVALID_OPERATION
: return "INVALID_OPERATION";
59 case GL_INVALID_FRAMEBUFFER_OPERATION
:
60 return "INVALID_FRAMEBUFFER_OPERATION";
61 case GL_OUT_OF_MEMORY
: return "OUT_OF_MEMORY";
62 default: return "unknown";
66 void glCheckError(GL
*gl
, const char *info
)
69 GLenum error
= gl
->GetError();
70 if (error
== GL_NO_ERROR
)
72 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] %s: OpenGL error %s.\n", info
,
73 gl_error_to_string(error
));
77 //! \defgroup glcontext OpenGL context management helper functions
79 //! \defgroup gltexture OpenGL texture handling helper functions
81 //! \defgroup glconversion OpenGL conversion helper functions
84 * \brief adjusts the GL_UNPACK_ALIGNMENT to fit the stride.
85 * \param stride number of bytes per line for which alignment should fit.
88 void glAdjustAlignment(GL
*gl
, int stride
)
93 else if (stride
% 4 == 0)
95 else if (stride
% 2 == 0)
99 gl
->PixelStorei(GL_UNPACK_ALIGNMENT
, gl_alignment
);
100 gl
->PixelStorei(GL_PACK_ALIGNMENT
, gl_alignment
);
103 struct gl_name_map_struct
{
109 #define MAP(a) {a, # a}
110 //! mapping table for the glValName function
111 static const struct gl_name_map_struct gl_name_map
[] = {
113 MAP(GL_R3_G3_B2
), MAP(GL_RGB4
), MAP(GL_RGB5
), MAP(GL_RGB8
),
114 MAP(GL_RGB10
), MAP(GL_RGB12
), MAP(GL_RGB16
), MAP(GL_RGBA2
),
115 MAP(GL_RGBA4
), MAP(GL_RGB5_A1
), MAP(GL_RGBA8
), MAP(GL_RGB10_A2
),
116 MAP(GL_RGBA12
), MAP(GL_RGBA16
), MAP(GL_LUMINANCE8
), MAP(GL_LUMINANCE16
),
120 MAP(GL_RGB
), MAP(GL_RGBA
), MAP(GL_RED
), MAP(GL_GREEN
), MAP(GL_BLUE
),
121 MAP(GL_ALPHA
), MAP(GL_LUMINANCE
), MAP(GL_LUMINANCE_ALPHA
),
124 MAP(GL_BGR
), MAP(GL_BGRA
),
127 MAP(GL_BYTE
), MAP(GL_UNSIGNED_BYTE
), MAP(GL_SHORT
), MAP(GL_UNSIGNED_SHORT
),
128 MAP(GL_INT
), MAP(GL_UNSIGNED_INT
), MAP(GL_FLOAT
), MAP(GL_DOUBLE
),
129 MAP(GL_2_BYTES
), MAP(GL_3_BYTES
), MAP(GL_4_BYTES
),
131 MAP(GL_UNSIGNED_BYTE_3_3_2
), MAP(GL_UNSIGNED_BYTE_2_3_3_REV
),
132 MAP(GL_UNSIGNED_SHORT_5_6_5
), MAP(GL_UNSIGNED_SHORT_5_6_5_REV
),
133 MAP(GL_UNSIGNED_SHORT_4_4_4_4
), MAP(GL_UNSIGNED_SHORT_4_4_4_4_REV
),
134 MAP(GL_UNSIGNED_SHORT_5_5_5_1
), MAP(GL_UNSIGNED_SHORT_1_5_5_5_REV
),
135 MAP(GL_UNSIGNED_INT_8_8_8_8
), MAP(GL_UNSIGNED_INT_8_8_8_8_REV
),
136 MAP(GL_UNSIGNED_INT_10_10_10_2
), MAP(GL_UNSIGNED_INT_2_10_10_10_REV
),
142 * \brief return the name of an OpenGL constant
143 * \param value the constant
144 * \return name of the constant or "Unknown format!"
147 const char *glValName(GLint value
)
151 while (gl_name_map
[i
].name
) {
152 if (gl_name_map
[i
].value
== value
)
153 return gl_name_map
[i
].name
;
156 return "Unknown format!";
159 //! always return this format as internal texture format in glFindFormat
160 #define TEXTUREFORMAT_ALWAYS GL_RGB8
161 #undef TEXTUREFORMAT_ALWAYS
164 * \brief find the OpenGL settings coresponding to format.
166 * All parameters may be NULL.
167 * \param fmt MPlayer format to analyze.
168 * \param bpp [OUT] bits per pixel of that format.
169 * \param gl_texfmt [OUT] internal texture format that fits the
170 * image format, not necessarily the best for performance.
171 * \param gl_format [OUT] OpenGL format for this image format.
172 * \param gl_type [OUT] OpenGL type for this image format.
173 * \return 1 if format is supported by OpenGL, 0 if not.
176 int glFindFormat(uint32_t fmt
, int have_texture_rg
, int *bpp
, GLint
*gl_texfmt
,
177 GLenum
*gl_format
, GLenum
*gl_type
)
192 if (mp_get_chroma_shift(fmt
, NULL
, NULL
, NULL
)) {
193 // reduce the possible cases a bit
194 if (IMGFMT_IS_YUVP16_LE(fmt
))
195 fmt
= IMGFMT_420P16_LE
;
196 else if (IMGFMT_IS_YUVP16_BE(fmt
))
197 fmt
= IMGFMT_420P16_BE
;
202 *bpp
= IMGFMT_IS_BGR(fmt
) ? IMGFMT_BGR_DEPTH(fmt
) : IMGFMT_RGB_DEPTH(fmt
);
207 *gl_type
= GL_UNSIGNED_SHORT
;
211 *gl_type
= GL_UNSIGNED_BYTE
;
215 *gl_format
= GL_RGBA
;
216 *gl_type
= GL_UNSIGNED_BYTE
;
219 supported
= 0; // no native YUV support
220 *gl_texfmt
= have_texture_rg
? GL_R16
: GL_LUMINANCE16
;
222 *gl_format
= have_texture_rg
? GL_RED
: GL_LUMINANCE
;
223 *gl_type
= GL_UNSIGNED_SHORT
;
226 supported
= 0; // no native YV12 support
231 *gl_format
= GL_LUMINANCE
;
232 *gl_type
= GL_UNSIGNED_BYTE
;
235 // IMGFMT_YUY2 would be more logical for the _REV format,
236 // but gives clearly swapped colors.
238 *gl_texfmt
= GL_YCBCR_MESA
;
240 *gl_format
= GL_YCBCR_MESA
;
241 *gl_type
= fmt
== IMGFMT_UYVY
? GL_UNSIGNED_SHORT_8_8
: GL_UNSIGNED_SHORT_8_8_REV
;
244 // we do not support palettized formats, although the format the
245 // swscale produces works
248 *gl_type
= GL_UNSIGNED_BYTE_2_3_3_REV
;
252 *gl_format
= GL_RGBA
;
253 *gl_type
= GL_UNSIGNED_SHORT_1_5_5_5_REV
;
257 *gl_type
= GL_UNSIGNED_SHORT_5_6_5_REV
;
261 // special case as red and blue have a different number of bits.
262 // GL_BGR and GL_UNSIGNED_BYTE_3_3_2 isn't supported at least
263 // by nVidia drivers, and in addition would give more bits to
264 // blue than to red, which isn't wanted
266 *gl_type
= GL_UNSIGNED_BYTE_3_3_2
;
270 *gl_format
= GL_BGRA
;
271 *gl_type
= GL_UNSIGNED_SHORT_1_5_5_5_REV
;
275 *gl_type
= GL_UNSIGNED_SHORT_5_6_5
;
279 *gl_type
= GL_UNSIGNED_BYTE
;
283 *gl_format
= GL_BGRA
;
284 *gl_type
= GL_UNSIGNED_BYTE
;
288 *gl_format
= GL_RGBA
;
289 *gl_type
= GL_UNSIGNED_BYTE
;
292 #ifdef TEXTUREFORMAT_ALWAYS
293 *gl_texfmt
= TEXTUREFORMAT_ALWAYS
;
302 * \brief find address of a linked function
303 * \param s name of function to find
304 * \return address of function or NULL if not found
306 static void *getdladdr(const char *s
)
310 void *handle
= dlopen(NULL
, RTLD_LAZY
);
313 ret
= dlsym(handle
, s
);
320 ptrdiff_t offset
; // offset to the function pointer in struct GL
322 const char *funcnames
[7];
327 #define DEF_FUNC_DESC(name) \
328 {offsetof(GL, name), NULL, {"gl" # name}, gl ## name}
329 #define DEF_EXT_FUNCS(...) __VA_ARGS__
330 #define DEF_EXT_DESC(name, ext, funcnames) \
331 {offsetof(GL, name), ext, {DEF_EXT_FUNCS funcnames}}
332 // These are mostly handled the same, but needed because at least the MESA
333 // headers don't define any function prototypes for these.
334 #define DEF_GL3_DESC(name) \
335 {offsetof(GL, name), NULL, {"gl" # name}, NULL, .is_gl3 = true}
337 static const extfunc_desc_t extfuncs
[] = {
338 // these aren't extension functions but we query them anyway to allow
339 // different "backends" with one binary
340 DEF_FUNC_DESC(Viewport
),
341 DEF_FUNC_DESC(Clear
),
342 DEF_FUNC_DESC(GenTextures
),
343 DEF_FUNC_DESC(DeleteTextures
),
344 DEF_FUNC_DESC(TexEnvi
),
345 DEF_FUNC_DESC(ClearColor
),
346 DEF_FUNC_DESC(Enable
),
347 DEF_FUNC_DESC(Disable
),
348 DEF_FUNC_DESC(DrawBuffer
),
349 DEF_FUNC_DESC(DepthMask
),
350 DEF_FUNC_DESC(BlendFunc
),
351 DEF_FUNC_DESC(Flush
),
352 DEF_FUNC_DESC(Finish
),
353 DEF_FUNC_DESC(PixelStorei
),
354 DEF_FUNC_DESC(TexImage1D
),
355 DEF_FUNC_DESC(TexImage2D
),
356 DEF_FUNC_DESC(TexSubImage2D
),
357 DEF_FUNC_DESC(GetTexImage
),
358 DEF_FUNC_DESC(TexParameteri
),
359 DEF_FUNC_DESC(TexParameterf
),
360 DEF_FUNC_DESC(TexParameterfv
),
361 DEF_FUNC_DESC(GetIntegerv
),
362 DEF_FUNC_DESC(GetBooleanv
),
363 DEF_FUNC_DESC(ColorMask
),
364 DEF_FUNC_DESC(ReadPixels
),
365 DEF_FUNC_DESC(ReadBuffer
),
366 DEF_FUNC_DESC(DrawArrays
),
367 DEF_FUNC_DESC(GetString
),
368 DEF_FUNC_DESC(GetError
),
370 // legacy GL functions (1.x - 2.x)
371 DEF_FUNC_DESC(Begin
),
373 DEF_FUNC_DESC(MatrixMode
),
374 DEF_FUNC_DESC(LoadIdentity
),
375 DEF_FUNC_DESC(Translated
),
376 DEF_FUNC_DESC(Scaled
),
377 DEF_FUNC_DESC(Ortho
),
378 DEF_FUNC_DESC(PushMatrix
),
379 DEF_FUNC_DESC(PopMatrix
),
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(Color4ub
),
387 DEF_FUNC_DESC(Color4f
),
388 DEF_FUNC_DESC(TexCoord2f
),
389 DEF_FUNC_DESC(TexCoord2fv
),
390 DEF_FUNC_DESC(Vertex2f
),
391 DEF_FUNC_DESC(VertexPointer
),
392 DEF_FUNC_DESC(ColorPointer
),
393 DEF_FUNC_DESC(TexCoordPointer
),
394 DEF_FUNC_DESC(EnableClientState
),
395 DEF_FUNC_DESC(DisableClientState
),
397 // OpenGL extension functions
398 DEF_EXT_DESC(GenBuffers
, NULL
,
399 ("glGenBuffers", "glGenBuffersARB")),
400 DEF_EXT_DESC(DeleteBuffers
, NULL
,
401 ("glDeleteBuffers", "glDeleteBuffersARB")),
402 DEF_EXT_DESC(BindBuffer
, NULL
,
403 ("glBindBuffer", "glBindBufferARB")),
404 DEF_EXT_DESC(MapBuffer
, NULL
,
405 ("glMapBuffer", "glMapBufferARB")),
406 DEF_EXT_DESC(UnmapBuffer
, NULL
,
407 ("glUnmapBuffer", "glUnmapBufferARB")),
408 DEF_EXT_DESC(BufferData
, NULL
,
409 ("glBufferData", "glBufferDataARB")),
410 DEF_EXT_DESC(ActiveTexture
, NULL
,
411 ("glActiveTexture", "glActiveTextureARB")),
412 DEF_EXT_DESC(BindTexture
, NULL
,
413 ("glBindTexture", "glBindTextureARB", "glBindTextureEXT")),
414 DEF_EXT_DESC(MultiTexCoord2f
, NULL
,
415 ("glMultiTexCoord2f", "glMultiTexCoord2fARB")),
416 DEF_EXT_DESC(GenPrograms
, "_program",
417 ("glGenProgramsARB")),
418 DEF_EXT_DESC(DeletePrograms
, "_program",
419 ("glDeleteProgramsARB")),
420 DEF_EXT_DESC(BindProgram
, "_program",
421 ("glBindProgramARB")),
422 DEF_EXT_DESC(ProgramString
, "_program",
423 ("glProgramStringARB")),
424 DEF_EXT_DESC(GetProgramivARB
, "_program",
425 ("glGetProgramivARB")),
426 DEF_EXT_DESC(ProgramEnvParameter4f
, "_program",
427 ("glProgramEnvParameter4fARB")),
428 DEF_EXT_DESC(SwapInterval
, "_swap_control",
429 ("glXSwapIntervalSGI", "glXSwapInterval", "wglSwapIntervalSGI",
430 "wglSwapInterval", "wglSwapIntervalEXT")),
431 DEF_EXT_DESC(TexImage3D
, NULL
,
434 // ancient ATI extensions
435 DEF_EXT_DESC(BeginFragmentShader
, "ATI_fragment_shader",
436 ("glBeginFragmentShaderATI")),
437 DEF_EXT_DESC(EndFragmentShader
, "ATI_fragment_shader",
438 ("glEndFragmentShaderATI")),
439 DEF_EXT_DESC(SampleMap
, "ATI_fragment_shader",
441 DEF_EXT_DESC(ColorFragmentOp2
, "ATI_fragment_shader",
442 ("glColorFragmentOp2ATI")),
443 DEF_EXT_DESC(ColorFragmentOp3
, "ATI_fragment_shader",
444 ("glColorFragmentOp3ATI")),
445 DEF_EXT_DESC(SetFragmentShaderConstant
, "ATI_fragment_shader",
446 ("glSetFragmentShaderConstantATI")),
448 // GL 3, possibly in GL 2.x as well in form of extensions
449 DEF_GL3_DESC(GenBuffers
),
450 DEF_GL3_DESC(DeleteBuffers
),
451 DEF_GL3_DESC(BindBuffer
),
452 DEF_GL3_DESC(MapBuffer
),
453 DEF_GL3_DESC(UnmapBuffer
),
454 DEF_GL3_DESC(BufferData
),
455 DEF_GL3_DESC(ActiveTexture
),
456 DEF_GL3_DESC(BindTexture
),
457 DEF_GL3_DESC(GenVertexArrays
),
458 DEF_GL3_DESC(BindVertexArray
),
459 DEF_GL3_DESC(GetAttribLocation
),
460 DEF_GL3_DESC(EnableVertexAttribArray
),
461 DEF_GL3_DESC(DisableVertexAttribArray
),
462 DEF_GL3_DESC(VertexAttribPointer
),
463 DEF_GL3_DESC(DeleteVertexArrays
),
464 DEF_GL3_DESC(UseProgram
),
465 DEF_GL3_DESC(GetUniformLocation
),
466 DEF_GL3_DESC(CompileShader
),
467 DEF_GL3_DESC(CreateProgram
),
468 DEF_GL3_DESC(CreateShader
),
469 DEF_GL3_DESC(ShaderSource
),
470 DEF_GL3_DESC(LinkProgram
),
471 DEF_GL3_DESC(AttachShader
),
472 DEF_GL3_DESC(DeleteShader
),
473 DEF_GL3_DESC(DeleteProgram
),
474 DEF_GL3_DESC(GetShaderInfoLog
),
475 DEF_GL3_DESC(GetShaderiv
),
476 DEF_GL3_DESC(GetProgramInfoLog
),
477 DEF_GL3_DESC(GetProgramiv
),
478 DEF_GL3_DESC(GetStringi
),
479 DEF_GL3_DESC(BindAttribLocation
),
480 DEF_GL3_DESC(BindFramebuffer
),
481 DEF_GL3_DESC(GenFramebuffers
),
482 DEF_GL3_DESC(DeleteFramebuffers
),
483 DEF_GL3_DESC(CheckFramebufferStatus
),
484 DEF_GL3_DESC(FramebufferTexture2D
),
485 DEF_GL3_DESC(Uniform1f
),
486 DEF_GL3_DESC(Uniform3f
),
487 DEF_GL3_DESC(Uniform1i
),
488 DEF_GL3_DESC(UniformMatrix3fv
),
489 DEF_GL3_DESC(UniformMatrix4x3fv
),
495 * \brief find the function pointers of some useful OpenGL extensions
496 * \param getProcAddress function to resolve function names, may be NULL
497 * \param ext2 an extra extension string
499 static void getFunctions(GL
*gl
, void *(*getProcAddress
)(const GLubyte
*),
500 const char *ext2
, bool is_gl3
)
502 const extfunc_desc_t
*dsc
;
503 char *allexts
= talloc_strdup(NULL
, ext2
? ext2
: "");
508 getProcAddress
= (void *)getdladdr
;
511 gl
->GetStringi
= getProcAddress("glGetStringi");
512 gl
->GetIntegerv
= getProcAddress("glGetIntegerv");
514 if (!(gl
->GetStringi
&& gl
->GetIntegerv
))
518 gl
->GetIntegerv(GL_NUM_EXTENSIONS
, &exts
);
519 for (int n
= 0; n
< exts
; n
++) {
520 allexts
= talloc_asprintf_append(allexts
, " %s",
521 gl
->GetStringi(GL_EXTENSIONS
, n
));
524 gl
->GetString
= getProcAddress("glGetString");
526 gl
->GetString
= glGetString
;
527 const char *ext
= (char*)gl
->GetString(GL_EXTENSIONS
);
528 allexts
= talloc_asprintf_append(allexts
, " %s", ext
);
531 mp_msg(MSGT_VO
, MSGL_DBG2
, "OpenGL extensions string:\n%s\n", allexts
);
532 for (dsc
= extfuncs
; dsc
->offset
>= 0; dsc
++) {
534 if (!dsc
->extstr
|| strstr(allexts
, dsc
->extstr
)) {
535 for (int i
= 0; !ptr
&& dsc
->funcnames
[i
]; i
++)
536 ptr
= getProcAddress((const GLubyte
*)dsc
->funcnames
[i
]);
540 if (!ptr
&& !dsc
->extstr
&& (!dsc
->is_gl3
|| is_gl3
))
541 mp_msg(MSGT_VO
, MSGL_WARN
, "[gl] OpenGL function not found: %s\n",
543 void **funcptr
= (void**)(((char*)gl
) + dsc
->offset
);
546 talloc_free(allexts
);
550 * \brief create a texture and set some defaults
551 * \param target texture taget, usually GL_TEXTURE_2D
552 * \param fmt internal texture format
553 * \param format texture host data format
554 * \param type texture host data type
555 * \param filter filter used for scaling, e.g. GL_LINEAR
556 * \param w texture width
557 * \param h texture height
558 * \param val luminance value to fill texture with
561 void glCreateClearTex(GL
*gl
, GLenum target
, GLenum fmt
, GLenum format
,
562 GLenum type
, GLint filter
, int w
, int h
,
565 GLfloat fval
= (GLfloat
)val
/ 255.0;
566 GLfloat border
[4] = {
567 fval
, fval
, fval
, fval
575 stride
= w
* glFmt2bpp(format
, type
);
578 init
= malloc(stride
* h
);
579 memset(init
, val
, stride
* h
);
580 glAdjustAlignment(gl
, stride
);
581 gl
->PixelStorei(GL_UNPACK_ROW_LENGTH
, w
);
582 gl
->TexImage2D(target
, 0, fmt
, w
, h
, 0, format
, type
, init
);
583 gl
->TexParameterf(target
, GL_TEXTURE_PRIORITY
, 1.0);
584 gl
->TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, filter
);
585 gl
->TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, filter
);
586 gl
->TexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
587 gl
->TexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
588 // Border texels should not be used with CLAMP_TO_EDGE
589 // We set a sane default anyway.
590 gl
->TexParameterfv(target
, GL_TEXTURE_BORDER_COLOR
, border
);
594 static GLint
detect_hqtexfmt(GL
*gl
)
596 const char *extensions
= (const char *)gl
->GetString(GL_EXTENSIONS
);
597 if (strstr(extensions
, "_texture_float"))
599 else if (strstr(extensions
, "NV_float_buffer"))
600 return GL_FLOAT_RGB32_NV
;
605 * \brief creates a texture from a PPM file
606 * \param target texture taget, usually GL_TEXTURE_2D
607 * \param fmt internal texture format, 0 for default
608 * \param filter filter used for scaling, e.g. GL_LINEAR
609 * \param f file to read PPM from
610 * \param width [out] width of texture
611 * \param height [out] height of texture
612 * \param maxval [out] maxval value from PPM file
613 * \return 0 on error, 1 otherwise
616 int glCreatePPMTex(GL
*gl
, GLenum target
, GLenum fmt
, GLint filter
,
617 FILE *f
, int *width
, int *height
, int *maxval
)
621 uint8_t *data
= read_pnm(f
, &w
, &h
, &bpp
, &m
);
622 GLint hqtexfmt
= detect_hqtexfmt(gl
);
623 if (!data
|| (bpp
!= 3 && bpp
!= 6)) {
628 fmt
= bpp
== 6 ? hqtexfmt
: 3;
629 if (fmt
== GL_FLOAT_RGB32_NV
&& target
!= GL_TEXTURE_RECTANGLE
)
632 type
= bpp
== 6 ? GL_UNSIGNED_SHORT
: GL_UNSIGNED_BYTE
;
633 glCreateClearTex(gl
, target
, fmt
, GL_RGB
, type
, filter
, w
, h
, 0);
634 glUploadTex(gl
, target
, GL_RGB
, type
,
635 data
, w
* bpp
, 0, 0, w
, h
, 0);
647 * \brief return the number of bytes per pixel for the given format
648 * \param format OpenGL format
649 * \param type OpenGL type
650 * \return bytes per pixel
653 * Does not handle all possible variants, just those used by MPlayer
655 int glFmt2bpp(GLenum format
, GLenum type
)
657 int component_size
= 0;
659 case GL_UNSIGNED_BYTE_3_3_2
:
660 case GL_UNSIGNED_BYTE_2_3_3_REV
:
662 case GL_UNSIGNED_SHORT_5_5_5_1
:
663 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
664 case GL_UNSIGNED_SHORT_5_6_5
:
665 case GL_UNSIGNED_SHORT_5_6_5_REV
:
667 case GL_UNSIGNED_BYTE
:
670 case GL_UNSIGNED_SHORT
:
677 return component_size
;
682 return 3 * component_size
;
685 return 4 * component_size
;
687 return component_size
;
689 case GL_LUMINANCE_ALPHA
:
690 return 2 * component_size
;
696 * \brief upload a texture, handling things like stride and slices
697 * \param target texture target, usually GL_TEXTURE_2D
698 * \param format OpenGL format of data
699 * \param type OpenGL type of data
700 * \param dataptr data to upload
701 * \param stride data stride
702 * \param x x offset in texture
703 * \param y y offset in texture
704 * \param w width of the texture part to upload
705 * \param h height of the texture part to upload
706 * \param slice height of an upload slice, 0 for all at once
709 void glUploadTex(GL
*gl
, GLenum target
, GLenum format
, GLenum type
,
710 const void *dataptr
, int stride
,
711 int x
, int y
, int w
, int h
, int slice
)
713 const uint8_t *data
= dataptr
;
715 if (w
<= 0 || h
<= 0)
720 data
+= (h
- 1) * stride
;
723 // this is not always correct, but should work for MPlayer
724 glAdjustAlignment(gl
, stride
);
725 gl
->PixelStorei(GL_UNPACK_ROW_LENGTH
, stride
/ glFmt2bpp(format
, type
));
726 for (; y
+ slice
<= y_max
; y
+= slice
) {
727 gl
->TexSubImage2D(target
, 0, x
, y
, w
, slice
, format
, type
, data
);
728 data
+= stride
* slice
;
731 gl
->TexSubImage2D(target
, 0, x
, y
, w
, y_max
- y
, format
, type
, data
);
735 * \brief download a texture, handling things like stride and slices
736 * \param target texture target, usually GL_TEXTURE_2D
737 * \param format OpenGL format of data
738 * \param type OpenGL type of data
739 * \param dataptr destination memory for download
740 * \param stride data stride (must be positive)
743 void glDownloadTex(GL
*gl
, GLenum target
, GLenum format
, GLenum type
,
744 void *dataptr
, int stride
)
746 // this is not always correct, but should work for MPlayer
747 glAdjustAlignment(gl
, stride
);
748 gl
->PixelStorei(GL_PACK_ROW_LENGTH
, stride
/ glFmt2bpp(format
, type
));
749 gl
->GetTexImage(target
, 0, format
, type
, dataptr
);
753 * \brief Setup ATI version of register combiners for YUV to RGB conversion.
754 * \param csp_params parameters used for colorspace conversion
755 * \param text if set use the GL_ATI_text_fragment_shader API as
758 static void glSetupYUVFragmentATI(GL
*gl
, struct mp_csp_params
*csp_params
,
764 gl
->GetIntegerv(GL_MAX_TEXTURE_UNITS
, &i
);
766 mp_msg(MSGT_VO
, MSGL_ERR
,
767 "[gl] 3 texture units needed for YUV combiner (ATI) support (found %i)\n", i
);
769 mp_get_yuv2rgb_coeffs(csp_params
, yuv2rgb
);
770 for (i
= 0; i
< 3; i
++) {
772 yuv2rgb
[i
][3] -= -0.5 * (yuv2rgb
[i
][1] + yuv2rgb
[i
][2]);
773 for (j
= 0; j
< 4; j
++) {
774 yuv2rgb
[i
][j
] *= 0.125;
775 yuv2rgb
[i
][j
] += 0.5;
776 if (yuv2rgb
[i
][j
] > 1)
778 if (yuv2rgb
[i
][j
] < 0)
783 GLfloat c0
[4] = { yuv2rgb
[0][0], yuv2rgb
[1][0], yuv2rgb
[2][0] };
784 GLfloat c1
[4] = { yuv2rgb
[0][1], yuv2rgb
[1][1], yuv2rgb
[2][1] };
785 GLfloat c2
[4] = { yuv2rgb
[0][2], yuv2rgb
[1][2], yuv2rgb
[2][2] };
786 GLfloat c3
[4] = { yuv2rgb
[0][3], yuv2rgb
[1][3], yuv2rgb
[2][3] };
787 if (!gl
->BeginFragmentShader
|| !gl
->EndFragmentShader
||
788 !gl
->SetFragmentShaderConstant
|| !gl
->SampleMap
||
789 !gl
->ColorFragmentOp2
|| !gl
->ColorFragmentOp3
) {
790 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Combiner (ATI) functions missing!\n");
793 gl
->GetIntegerv(GL_NUM_FRAGMENT_REGISTERS_ATI
, &i
);
795 mp_msg(MSGT_VO
, MSGL_ERR
,
796 "[gl] 3 registers needed for YUV combiner (ATI) support (found %i)\n", i
);
797 gl
->BeginFragmentShader();
798 gl
->SetFragmentShaderConstant(GL_CON_0_ATI
, c0
);
799 gl
->SetFragmentShaderConstant(GL_CON_1_ATI
, c1
);
800 gl
->SetFragmentShaderConstant(GL_CON_2_ATI
, c2
);
801 gl
->SetFragmentShaderConstant(GL_CON_3_ATI
, c3
);
802 gl
->SampleMap(GL_REG_0_ATI
, GL_TEXTURE0
, GL_SWIZZLE_STR_ATI
);
803 gl
->SampleMap(GL_REG_1_ATI
, GL_TEXTURE1
, GL_SWIZZLE_STR_ATI
);
804 gl
->SampleMap(GL_REG_2_ATI
, GL_TEXTURE2
, GL_SWIZZLE_STR_ATI
);
805 gl
->ColorFragmentOp2(GL_MUL_ATI
, GL_REG_1_ATI
, GL_NONE
, GL_NONE
,
806 GL_REG_1_ATI
, GL_NONE
, GL_BIAS_BIT_ATI
,
807 GL_CON_1_ATI
, GL_NONE
, GL_BIAS_BIT_ATI
);
808 gl
->ColorFragmentOp3(GL_MAD_ATI
, GL_REG_2_ATI
, GL_NONE
, GL_NONE
,
809 GL_REG_2_ATI
, GL_NONE
, GL_BIAS_BIT_ATI
,
810 GL_CON_2_ATI
, GL_NONE
, GL_BIAS_BIT_ATI
,
811 GL_REG_1_ATI
, GL_NONE
, GL_NONE
);
812 gl
->ColorFragmentOp3(GL_MAD_ATI
, GL_REG_0_ATI
, GL_NONE
, GL_NONE
,
813 GL_REG_0_ATI
, GL_NONE
, GL_NONE
,
814 GL_CON_0_ATI
, GL_NONE
, GL_BIAS_BIT_ATI
,
815 GL_REG_2_ATI
, GL_NONE
, GL_NONE
);
816 gl
->ColorFragmentOp2(GL_ADD_ATI
, GL_REG_0_ATI
, GL_NONE
, GL_8X_BIT_ATI
,
817 GL_REG_0_ATI
, GL_NONE
, GL_NONE
,
818 GL_CON_3_ATI
, GL_NONE
, GL_BIAS_BIT_ATI
);
819 gl
->EndFragmentShader();
821 static const char template[] =
824 " CONSTANT c0 = {%e, %e, %e};\n"
825 " CONSTANT c1 = {%e, %e, %e};\n"
826 " CONSTANT c2 = {%e, %e, %e};\n"
827 " CONSTANT c3 = {%e, %e, %e};\n"
830 " SampleMap r0, t0.str;\n"
831 " SampleMap r1, t1.str;\n"
832 " SampleMap r2, t2.str;\n"
833 " MUL r1.rgb, r1.bias, c1.bias;\n"
834 " MAD r2.rgb, r2.bias, c2.bias, r1;\n"
835 " MAD r0.rgb, r0, c0.bias, r2;\n"
836 " ADD r0.rgb.8x, r0, c3.bias;\n"
839 snprintf(buffer
, sizeof(buffer
), template,
840 yuv2rgb
[0][0], yuv2rgb
[1][0], yuv2rgb
[2][0],
841 yuv2rgb
[0][1], yuv2rgb
[1][1], yuv2rgb
[2][1],
842 yuv2rgb
[0][2], yuv2rgb
[1][2], yuv2rgb
[2][2],
843 yuv2rgb
[0][3], yuv2rgb
[1][3], yuv2rgb
[2][3]);
844 mp_msg(MSGT_VO
, MSGL_DBG2
, "[gl] generated fragment program:\n%s\n",
846 loadGPUProgram(gl
, GL_TEXT_FRAGMENT_SHADER_ATI
, buffer
);
850 // Replace all occurances of variables named "$"+name (e.g. $foo) in *text with
851 // replace, and return the result. *text must have been allocated with talloc.
852 static void replace_var_str(char **text
, const char *name
, const char *replace
)
854 size_t namelen
= strlen(name
);
855 char *nextvar
= *text
;
856 void *parent
= talloc_parent(*text
);
858 nextvar
= strchr(nextvar
, '$');
861 char *until
= nextvar
;
863 if (strncmp(nextvar
, name
, namelen
) != 0)
866 // try not to replace prefixes of other vars (e.g. $foo vs. $foo_bar)
867 char term
= nextvar
[0];
868 if (isalnum(term
) || term
== '_')
870 int prelength
= until
- *text
;
871 int postlength
= nextvar
- *text
;
872 char *n
= talloc_asprintf(parent
, "%.*s%s%s", prelength
, *text
, replace
,
876 nextvar
= *text
+ postlength
;
880 static void replace_var_float(char **text
, const char *name
, float replace
)
882 char *s
= talloc_asprintf(NULL
, "%e", replace
);
883 replace_var_str(text
, name
, s
);
887 static void replace_var_char(char **text
, const char *name
, char replace
)
889 char s
[2] = { replace
, '\0' };
890 replace_var_str(text
, name
, s
);
893 // Append template to *text. Possibly initialize *text if it's NULL.
894 static void append_template(char **text
, const char* template)
897 *text
= talloc_strdup(NULL
, template);
899 *text
= talloc_strdup_append(*text
, template);
903 * \brief helper function for gen_spline_lookup_tex
904 * \param x subpixel-position ((0,1) range) to calculate weights for
905 * \param dst where to store transformed weights, must provide space for 4 GLfloats
907 * calculates the weights and stores them after appropriate transformation
908 * for the scaler fragment program.
910 static void store_weights(float x
, GLfloat
*dst
)
912 float w0
= (((-1 * x
+ 3) * x
- 3) * x
+ 1) / 6;
913 float w1
= (((3 * x
- 6) * x
+ 0) * x
+ 4) / 6;
914 float w2
= (((-3 * x
+ 3) * x
+ 3) * x
+ 1) / 6;
915 float w3
= (((1 * x
+ 0) * x
+ 0) * x
+ 0) / 6;
916 *dst
++ = 1 + x
- w1
/ (w0
+ w1
);
917 *dst
++ = 1 - x
+ w3
/ (w2
+ w3
);
922 //! to avoid artefacts this should be rather large
923 #define LOOKUP_BSPLINE_RES (2 * 1024)
925 * \brief creates the 1D lookup texture needed for fast higher-order filtering
926 * \param unit texture unit to attach texture to
928 static void gen_spline_lookup_tex(GL
*gl
, GLenum unit
)
930 GLfloat
*tex
= calloc(4 * LOOKUP_BSPLINE_RES
, sizeof(*tex
));
933 for (i
= 0; i
< LOOKUP_BSPLINE_RES
; i
++) {
934 float x
= (float)(i
+ 0.5) / LOOKUP_BSPLINE_RES
;
935 store_weights(x
, tp
);
938 store_weights(0, tex
);
939 store_weights(1, &tex
[4 * (LOOKUP_BSPLINE_RES
- 1)]);
940 gl
->ActiveTexture(unit
);
941 gl
->TexImage1D(GL_TEXTURE_1D
, 0, GL_RGBA16
, LOOKUP_BSPLINE_RES
, 0, GL_RGBA
,
943 gl
->TexParameterf(GL_TEXTURE_1D
, GL_TEXTURE_PRIORITY
, 1.0);
944 gl
->TexParameteri(GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
945 gl
->TexParameteri(GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
946 gl
->TexParameteri(GL_TEXTURE_1D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
947 gl
->ActiveTexture(GL_TEXTURE0
);
951 #define NOISE_RES 2048
954 * \brief creates the 1D lookup texture needed to generate pseudo-random numbers.
955 * \param unit texture unit to attach texture to
957 static void gen_noise_lookup_tex(GL
*gl
, GLenum unit
) {
958 GLfloat
*tex
= calloc(NOISE_RES
, sizeof(*tex
));
959 uint32_t lcg
= 0x79381c11;
961 for (i
= 0; i
< NOISE_RES
; i
++)
962 tex
[i
] = (double)i
/ (NOISE_RES
- 1);
963 for (i
= 0; i
< NOISE_RES
- 1; i
++) {
964 int remain
= NOISE_RES
- i
;
965 int idx
= i
+ (lcg
>> 16) % remain
;
966 GLfloat tmp
= tex
[i
];
969 lcg
= lcg
* 1664525 + 1013904223;
971 gl
->ActiveTexture(unit
);
972 gl
->TexImage1D(GL_TEXTURE_1D
, 0, 1, NOISE_RES
, 0, GL_RED
, GL_FLOAT
, tex
);
973 gl
->TexParameterf(GL_TEXTURE_1D
, GL_TEXTURE_PRIORITY
, 1.0);
974 gl
->TexParameteri(GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
975 gl
->TexParameteri(GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
976 gl
->TexParameteri(GL_TEXTURE_1D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
977 gl
->ActiveTexture(GL_TEXTURE0
);
981 #define SAMPLE(dest, coord, texture) \
982 "TEX textemp, " coord ", " texture ", $tex_type;\n" \
983 "MOV " dest ", textemp.r;\n"
985 static const char bilin_filt_template
[] =
986 SAMPLE("yuv.$out_comp","fragment.texcoord[$in_tex]","texture[$in_tex]");
988 #define BICUB_FILT_MAIN \
989 /* first y-interpolation */ \
990 "ADD coord, fragment.texcoord[$in_tex].xyxy, cdelta.xyxw;\n" \
991 "ADD coord2, fragment.texcoord[$in_tex].xyxy, cdelta.zyzw;\n" \
992 SAMPLE("a.r","coord.xyxy","texture[$in_tex]") \
993 SAMPLE("a.g","coord.zwzw","texture[$in_tex]") \
994 /* second y-interpolation */ \
995 SAMPLE("b.r","coord2.xyxy","texture[$in_tex]") \
996 SAMPLE("b.g","coord2.zwzw","texture[$in_tex]") \
997 "LRP a.b, parmy.b, a.rrrr, a.gggg;\n" \
998 "LRP a.a, parmy.b, b.rrrr, b.gggg;\n" \
999 /* x-interpolation */ \
1000 "LRP yuv.$out_comp, parmx.b, a.bbbb, a.aaaa;\n"
1002 static const char bicub_filt_template_2D
[] =
1003 "MAD coord.xy, fragment.texcoord[$in_tex], {$texw, $texh}, {0.5, 0.5};\n"
1004 "TEX parmx, coord.x, texture[$texs], 1D;\n"
1005 "MUL cdelta.xz, parmx.rrgg, {-$ptw, 0, $ptw, 0};\n"
1006 "TEX parmy, coord.y, texture[$texs], 1D;\n"
1007 "MUL cdelta.yw, parmy.rrgg, {0, -$pth, 0, $pth};\n"
1010 static const char bicub_filt_template_RECT
[] =
1011 "ADD coord, fragment.texcoord[$in_tex], {0.5, 0.5};\n"
1012 "TEX parmx, coord.x, texture[$texs], 1D;\n"
1013 "MUL cdelta.xz, parmx.rrgg, {-1, 0, 1, 0};\n"
1014 "TEX parmy, coord.y, texture[$texs], 1D;\n"
1015 "MUL cdelta.yw, parmy.rrgg, {0, -1, 0, 1};\n"
1018 #define CALCWEIGHTS(t, s) \
1019 "MAD "t ", {-0.5, 0.1666, 0.3333, -0.3333}, "s ", {1, 0, -0.5, 0.5};\n" \
1020 "MAD "t ", "t ", "s ", {0, 0, -0.5, 0.5};\n" \
1021 "MAD "t ", "t ", "s ", {-0.6666, 0, 0.8333, 0.1666};\n" \
1022 "RCP a.x, "t ".z;\n" \
1023 "RCP a.y, "t ".w;\n" \
1024 "MAD "t ".xy, "t ".xyxy, a.xyxy, {1, 1, 0, 0};\n" \
1025 "ADD "t ".x, "t ".xxxx, "s ";\n" \
1026 "SUB "t ".y, "t ".yyyy, "s ";\n"
1028 static const char bicub_notex_filt_template_2D
[] =
1029 "MAD coord.xy, fragment.texcoord[$in_tex], {$texw, $texh}, {0.5, 0.5};\n"
1030 "FRC coord.xy, coord.xyxy;\n"
1031 CALCWEIGHTS("parmx", "coord.xxxx")
1032 "MUL cdelta.xz, parmx.rrgg, {-$ptw, 0, $ptw, 0};\n"
1033 CALCWEIGHTS("parmy", "coord.yyyy")
1034 "MUL cdelta.yw, parmy.rrgg, {0, -$pth, 0, $pth};\n"
1037 static const char bicub_notex_filt_template_RECT
[] =
1038 "ADD coord, fragment.texcoord[$in_tex], {0.5, 0.5};\n"
1039 "FRC coord.xy, coord.xyxy;\n"
1040 CALCWEIGHTS("parmx", "coord.xxxx")
1041 "MUL cdelta.xz, parmx.rrgg, {-1, 0, 1, 0};\n"
1042 CALCWEIGHTS("parmy", "coord.yyyy")
1043 "MUL cdelta.yw, parmy.rrgg, {0, -1, 0, 1};\n"
1046 #define BICUB_X_FILT_MAIN \
1047 "ADD coord.xy, fragment.texcoord[$in_tex].xyxy, cdelta.xyxy;\n" \
1048 "ADD coord2.xy, fragment.texcoord[$in_tex].xyxy, cdelta.zyzy;\n" \
1049 SAMPLE("a.r","coord","texture[$in_tex]") \
1050 SAMPLE("b.r","coord2","texture[$in_tex]") \
1051 /* x-interpolation */ \
1052 "LRP yuv.$out_comp, parmx.b, a.rrrr, b.rrrr;\n"
1054 static const char bicub_x_filt_template_2D
[] =
1055 "MAD coord.x, fragment.texcoord[$in_tex], {$texw}, {0.5};\n"
1056 "TEX parmx, coord, texture[$texs], 1D;\n"
1057 "MUL cdelta.xyz, parmx.rrgg, {-$ptw, 0, $ptw};\n"
1060 static const char bicub_x_filt_template_RECT
[] =
1061 "ADD coord.x, fragment.texcoord[$in_tex], {0.5};\n"
1062 "TEX parmx, coord, texture[$texs], 1D;\n"
1063 "MUL cdelta.xyz, parmx.rrgg, {-1, 0, 1};\n"
1066 static const char unsharp_filt_template
[] =
1067 "PARAM dcoord$out_comp = {$ptw_05, $pth_05, $ptw_05, -$pth_05};\n"
1068 "ADD coord, fragment.texcoord[$in_tex].xyxy, dcoord$out_comp;\n"
1069 "SUB coord2, fragment.texcoord[$in_tex].xyxy, dcoord$out_comp;\n"
1070 SAMPLE("a.r","fragment.texcoord[$in_tex]","texture[$in_tex]")
1071 SAMPLE("b.r","coord.xyxy","texture[$in_tex]")
1072 SAMPLE("b.g","coord.zwzw","texture[$in_tex]")
1073 "ADD b.r, b.r, b.g;\n"
1074 SAMPLE("b.b","coord2.xyxy","texture[$in_tex]")
1075 SAMPLE("b.g","coord2.zwzw","texture[$in_tex]")
1076 "DP3 b, b, {0.25, 0.25, 0.25};\n"
1077 "SUB b.r, a.r, b.r;\n"
1078 "MAD textemp.r, b.r, {$strength}, a.r;\n"
1079 "MOV yuv.$out_comp, textemp.r;\n";
1081 static const char unsharp_filt_template2
[] =
1082 "PARAM dcoord$out_comp = {$ptw_12, $pth_12, $ptw_12, -$pth_12};\n"
1083 "PARAM dcoord2$out_comp = {$ptw_15, 0, 0, $pth_15};\n"
1084 "ADD coord, fragment.texcoord[$in_tex].xyxy, dcoord$out_comp;\n"
1085 "SUB coord2, fragment.texcoord[$in_tex].xyxy, dcoord$out_comp;\n"
1086 SAMPLE("a.r","fragment.texcoord[$in_tex]","texture[$in_tex]")
1087 SAMPLE("b.r","coord.xyxy","texture[$in_tex]")
1088 SAMPLE("b.g","coord.zwzw","texture[$in_tex]")
1089 "ADD b.r, b.r, b.g;\n"
1090 SAMPLE("b.b","coord2.xyxy","texture[$in_tex]")
1091 SAMPLE("b.g","coord2.zwzw","texture[$in_tex]")
1092 "ADD b.r, b.r, b.b;\n"
1093 "ADD b.a, b.r, b.g;\n"
1094 "ADD coord, fragment.texcoord[$in_tex].xyxy, dcoord2$out_comp;\n"
1095 "SUB coord2, fragment.texcoord[$in_tex].xyxy, dcoord2$out_comp;\n"
1096 SAMPLE("b.r","coord.xyxy","texture[$in_tex]")
1097 SAMPLE("b.g","coord.zwzw","texture[$in_tex]")
1098 "ADD b.r, b.r, b.g;\n"
1099 SAMPLE("b.b","coord2.xyxy","texture[$in_tex]")
1100 SAMPLE("b.g","coord2.zwzw","texture[$in_tex]")
1101 "DP4 b.r, b, {-0.1171875, -0.1171875, -0.1171875, -0.09765625};\n"
1102 "MAD b.r, a.r, {0.859375}, b.r;\n"
1103 "MAD textemp.r, b.r, {$strength}, a.r;\n"
1104 "MOV yuv.$out_comp, textemp.r;\n";
1106 static const char yuv_prog_template
[] =
1107 "PARAM ycoef = {$cm11, $cm21, $cm31};\n"
1108 "PARAM ucoef = {$cm12, $cm22, $cm32};\n"
1109 "PARAM vcoef = {$cm13, $cm23, $cm33};\n"
1110 "PARAM offsets = {$cm14, $cm24, $cm34};\n"
1112 "MAD res.rgb, yuv.rrrr, ycoef, offsets;\n"
1113 "MAD res.rgb, yuv.gggg, ucoef, res;\n"
1114 "MAD res.rgb, yuv.bbbb, vcoef, res;\n";
1116 static const char yuv_pow_prog_template
[] =
1117 "PARAM ycoef = {$cm11, $cm21, $cm31};\n"
1118 "PARAM ucoef = {$cm12, $cm22, $cm32};\n"
1119 "PARAM vcoef = {$cm13, $cm23, $cm33};\n"
1120 "PARAM offsets = {$cm14, $cm24, $cm34};\n"
1121 "PARAM gamma = {$gamma_r, $gamma_g, $gamma_b};\n"
1123 "MAD res.rgb, yuv.rrrr, ycoef, offsets;\n"
1124 "MAD res.rgb, yuv.gggg, ucoef, res;\n"
1125 "MAD_SAT res.rgb, yuv.bbbb, vcoef, res;\n"
1126 "POW res.r, res.r, gamma.r;\n"
1127 "POW res.g, res.g, gamma.g;\n"
1128 "POW res.b, res.b, gamma.b;\n";
1130 static const char yuv_lookup_prog_template
[] =
1131 "PARAM ycoef = {$cm11, $cm21, $cm31, 0};\n"
1132 "PARAM ucoef = {$cm12, $cm22, $cm32, 0};\n"
1133 "PARAM vcoef = {$cm13, $cm23, $cm33, 0};\n"
1134 "PARAM offsets = {$cm14, $cm24, $cm34, 0.125};\n"
1136 "MAD res, yuv.rrrr, ycoef, offsets;\n"
1137 "MAD res.rgb, yuv.gggg, ucoef, res;\n"
1138 "MAD res.rgb, yuv.bbbb, vcoef, res;\n"
1139 "TEX res.r, res.raaa, texture[$conv_tex0], 2D;\n"
1140 "ADD res.a, res.a, 0.25;\n"
1141 "TEX res.g, res.gaaa, texture[$conv_tex0], 2D;\n"
1142 "ADD res.a, res.a, 0.25;\n"
1143 "TEX res.b, res.baaa, texture[$conv_tex0], 2D;\n";
1145 static const char yuv_lookup3d_prog_template
[] =
1147 "TEX res, yuv, texture[$conv_tex0], 3D;\n";
1149 static const char noise_filt_template
[] =
1150 "MUL coord.xy, fragment.texcoord[0], {$noise_sx, $noise_sy};\n"
1152 "TEX rand.r, coord.x, texture[$noise_filt_tex], 1D;\n"
1153 "ADD rand.r, rand.r, coord.y;\n"
1154 "TEX rand.r, rand.r, texture[$noise_filt_tex], 1D;\n"
1155 "MAD res.rgb, rand.rrrr, {$noise_str, $noise_str, $noise_str}, res;\n";
1158 * \brief creates and initializes helper textures needed for scaling texture read
1159 * \param scaler scaler type to create texture for
1160 * \param texu contains next free texture unit number
1161 * \param texs texture unit ids for the scaler are stored in this array
1163 static void create_scaler_textures(GL
*gl
, int scaler
, int *texu
, char *texs
)
1166 case YUV_SCALER_BILIN
:
1167 case YUV_SCALER_BICUB_NOTEX
:
1168 case YUV_SCALER_UNSHARP
:
1169 case YUV_SCALER_UNSHARP2
:
1171 case YUV_SCALER_BICUB
:
1172 case YUV_SCALER_BICUB_X
:
1173 texs
[0] = (*texu
)++;
1174 gen_spline_lookup_tex(gl
, GL_TEXTURE0
+ texs
[0]);
1178 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] unknown scaler type %i\n", scaler
);
1182 //! resolution of texture for gamma lookup table
1183 #define LOOKUP_RES 512
1184 //! resolution for 3D yuv->rgb conversion lookup table
1185 #define LOOKUP_3DRES 32
1187 * \brief creates and initializes helper textures needed for yuv conversion
1188 * \param params struct containing parameters like brightness, gamma, ...
1189 * \param texu contains next free texture unit number
1190 * \param texs texture unit ids for the conversion are stored in this array
1192 static void create_conv_textures(GL
*gl
, gl_conversion_params_t
*params
,
1193 int *texu
, char *texs
)
1195 unsigned char *lookup_data
= NULL
;
1196 int conv
= YUV_CONVERSION(params
->type
);
1198 case YUV_CONVERSION_FRAGMENT
:
1199 case YUV_CONVERSION_FRAGMENT_POW
:
1201 case YUV_CONVERSION_FRAGMENT_LOOKUP
:
1202 texs
[0] = (*texu
)++;
1203 gl
->ActiveTexture(GL_TEXTURE0
+ texs
[0]);
1204 lookup_data
= malloc(4 * LOOKUP_RES
);
1205 mp_gen_gamma_map(lookup_data
, LOOKUP_RES
, params
->csp_params
.rgamma
);
1206 mp_gen_gamma_map(&lookup_data
[LOOKUP_RES
], LOOKUP_RES
,
1207 params
->csp_params
.ggamma
);
1208 mp_gen_gamma_map(&lookup_data
[2 * LOOKUP_RES
], LOOKUP_RES
,
1209 params
->csp_params
.bgamma
);
1210 glCreateClearTex(gl
, GL_TEXTURE_2D
, GL_LUMINANCE8
, GL_LUMINANCE
,
1211 GL_UNSIGNED_BYTE
, GL_LINEAR
, LOOKUP_RES
, 4, 0);
1212 glUploadTex(gl
, GL_TEXTURE_2D
, GL_LUMINANCE
, GL_UNSIGNED_BYTE
,
1213 lookup_data
, LOOKUP_RES
, 0, 0, LOOKUP_RES
, 4, 0);
1214 gl
->ActiveTexture(GL_TEXTURE0
);
1217 case YUV_CONVERSION_FRAGMENT_LOOKUP3D
:
1219 int sz
= LOOKUP_3DRES
+ 2; // texture size including borders
1220 if (!gl
->TexImage3D
) {
1221 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] Missing 3D texture function!\n");
1224 texs
[0] = (*texu
)++;
1225 gl
->ActiveTexture(GL_TEXTURE0
+ texs
[0]);
1226 lookup_data
= malloc(3 * sz
* sz
* sz
);
1227 mp_gen_yuv2rgb_map(¶ms
->csp_params
, lookup_data
, LOOKUP_3DRES
);
1228 glAdjustAlignment(gl
, sz
);
1229 gl
->PixelStorei(GL_UNPACK_ROW_LENGTH
, 0);
1230 gl
->TexImage3D(GL_TEXTURE_3D
, 0, 3, sz
, sz
, sz
, 1,
1231 GL_RGB
, GL_UNSIGNED_BYTE
, lookup_data
);
1232 gl
->TexParameterf(GL_TEXTURE_3D
, GL_TEXTURE_PRIORITY
, 1.0);
1233 gl
->TexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
1234 gl
->TexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
1235 gl
->TexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_WRAP_S
, GL_CLAMP
);
1236 gl
->TexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_WRAP_T
, GL_CLAMP
);
1237 gl
->TexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_WRAP_R
, GL_CLAMP
);
1238 gl
->ActiveTexture(GL_TEXTURE0
);
1243 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] unknown conversion type %i\n", conv
);
1249 * \brief adds a scaling texture read at the current fragment program position
1250 * \param scaler type of scaler to insert
1251 * \param prog pointer to fragment program so far
1252 * \param texs array containing the texture unit identifiers for this scaler
1253 * \param in_tex texture unit the scaler should read from
1254 * \param out_comp component of the yuv variable the scaler stores the result in
1255 * \param rect if rectangular (pixel) adressing should be used for in_tex
1256 * \param texw width of the in_tex texture
1257 * \param texh height of the in_tex texture
1258 * \param strength strength of filter effect if the scaler does some kind of filtering
1260 static void add_scaler(int scaler
, char **prog
, char *texs
,
1261 char in_tex
, char out_comp
, int rect
, int texw
, int texh
,
1264 const char *ttype
= rect
? "RECT" : "2D";
1265 const float ptw
= rect
? 1.0 : 1.0 / texw
;
1266 const float pth
= rect
? 1.0 : 1.0 / texh
;
1268 case YUV_SCALER_BILIN
:
1269 append_template(prog
, bilin_filt_template
);
1271 case YUV_SCALER_BICUB
:
1273 append_template(prog
, bicub_filt_template_RECT
);
1275 append_template(prog
, bicub_filt_template_2D
);
1277 case YUV_SCALER_BICUB_X
:
1279 append_template(prog
, bicub_x_filt_template_RECT
);
1281 append_template(prog
, bicub_x_filt_template_2D
);
1283 case YUV_SCALER_BICUB_NOTEX
:
1285 append_template(prog
, bicub_notex_filt_template_RECT
);
1287 append_template(prog
, bicub_notex_filt_template_2D
);
1289 case YUV_SCALER_UNSHARP
:
1290 append_template(prog
, unsharp_filt_template
);
1292 case YUV_SCALER_UNSHARP2
:
1293 append_template(prog
, unsharp_filt_template2
);
1297 replace_var_char(prog
, "texs", texs
[0]);
1298 replace_var_char(prog
, "in_tex", in_tex
);
1299 replace_var_char(prog
, "out_comp", out_comp
);
1300 replace_var_str(prog
, "tex_type", ttype
);
1301 replace_var_float(prog
, "texw", texw
);
1302 replace_var_float(prog
, "texh", texh
);
1303 replace_var_float(prog
, "ptw", ptw
);
1304 replace_var_float(prog
, "pth", pth
);
1306 // this is silly, not sure if that couldn't be in the shader source instead
1307 replace_var_float(prog
, "ptw_05", ptw
* 0.5);
1308 replace_var_float(prog
, "pth_05", pth
* 0.5);
1309 replace_var_float(prog
, "ptw_15", ptw
* 1.5);
1310 replace_var_float(prog
, "pth_15", pth
* 1.5);
1311 replace_var_float(prog
, "ptw_12", ptw
* 1.2);
1312 replace_var_float(prog
, "pth_12", pth
* 1.2);
1314 replace_var_float(prog
, "strength", strength
);
1317 static const struct {
1322 {"instructions", 0x88A0, 0x88A1},
1323 {"native instructions", 0x88A2, 0x88A3},
1324 {"temporaries", 0x88A4, 0x88A5},
1325 {"native temporaries", 0x88A6, 0x88A7},
1326 {"parameters", 0x88A8, 0x88A9},
1327 {"native parameters", 0x88AA, 0x88AB},
1328 {"attribs", 0x88AC, 0x88AD},
1329 {"native attribs", 0x88AE, 0x88AF},
1330 {"ALU instructions", 0x8805, 0x880B},
1331 {"TEX instructions", 0x8806, 0x880C},
1332 {"TEX indirections", 0x8807, 0x880D},
1333 {"native ALU instructions", 0x8808, 0x880E},
1334 {"native TEX instructions", 0x8809, 0x880F},
1335 {"native TEX indirections", 0x880A, 0x8810},
1340 * \brief load the specified GPU Program
1341 * \param target program target to load into, only GL_FRAGMENT_PROGRAM is tested
1342 * \param prog program string
1343 * \return 1 on success, 0 otherwise
1345 int loadGPUProgram(GL
*gl
, GLenum target
, char *prog
)
1348 GLint cur
= 0, max
= 0, err
= 0;
1349 if (!gl
->ProgramString
) {
1350 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] Missing GPU program function\n");
1353 gl
->ProgramString(target
, GL_PROGRAM_FORMAT_ASCII
, strlen(prog
), prog
);
1354 gl
->GetIntegerv(GL_PROGRAM_ERROR_POSITION
, &err
);
1356 mp_msg(MSGT_VO
, MSGL_ERR
,
1357 "[gl] Error compiling fragment program, make sure your card supports\n"
1358 "[gl] GL_ARB_fragment_program (use glxinfo to check).\n"
1359 "[gl] Error message:\n %s at %.10s\n",
1360 gl
->GetString(GL_PROGRAM_ERROR_STRING
), &prog
[err
]);
1363 if (!gl
->GetProgramivARB
|| !mp_msg_test(MSGT_VO
, MSGL_DBG2
))
1365 mp_msg(MSGT_VO
, MSGL_V
, "[gl] Program statistics:\n");
1366 for (i
= 0; progstats
[i
].name
; i
++) {
1367 gl
->GetProgramivARB(target
, progstats
[i
].cur
, &cur
);
1368 gl
->GetProgramivARB(target
, progstats
[i
].max
, &max
);
1369 mp_msg(MSGT_VO
, MSGL_V
, "[gl] %s: %i/%i\n", progstats
[i
].name
, cur
,
1375 #define MAX_PROGSZ (1024 * 1024)
1378 * \brief setup a fragment program that will do YUV->RGB conversion
1379 * \param parms struct containing parameters like conversion and scaler type,
1382 static void glSetupYUVFragprog(GL
*gl
, gl_conversion_params_t
*params
)
1384 int type
= params
->type
;
1385 int texw
= params
->texw
;
1386 int texh
= params
->texh
;
1387 int rect
= params
->target
== GL_TEXTURE_RECTANGLE
;
1388 static const char prog_hdr
[] =
1390 "OPTION ARB_precision_hint_fastest;\n"
1391 // all scaler variables must go here so they aren't defined
1392 // multiple times when the same scaler is used more than once
1393 "TEMP coord, coord2, cdelta, parmx, parmy, a, b, yuv, textemp;\n";
1394 char *yuv_prog
= NULL
;
1395 char **prog
= &yuv_prog
;
1397 char lum_scale_texs
[1];
1398 char chrom_scale_texs
[1];
1400 char filt_texs
[1] = {0};
1402 // this is the conversion matrix, with y, u, v factors
1403 // for red, green, blue and the constant offsets
1404 float yuv2rgb
[3][4];
1405 int noise
= params
->noise_strength
!= 0;
1406 create_conv_textures(gl
, params
, &cur_texu
, conv_texs
);
1407 create_scaler_textures(gl
, YUV_LUM_SCALER(type
), &cur_texu
, lum_scale_texs
);
1408 if (YUV_CHROM_SCALER(type
) == YUV_LUM_SCALER(type
))
1409 memcpy(chrom_scale_texs
, lum_scale_texs
, sizeof(chrom_scale_texs
));
1411 create_scaler_textures(gl
, YUV_CHROM_SCALER(type
), &cur_texu
,
1415 gen_noise_lookup_tex(gl
, cur_texu
);
1416 filt_texs
[0] = '0' + cur_texu
++;
1419 gl
->GetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS
, &i
);
1421 mp_msg(MSGT_VO
, MSGL_ERR
,
1422 "[gl] %i texture units needed for this type of YUV fragment support (found %i)\n",
1424 if (!gl
->ProgramString
) {
1425 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] ProgramString function missing!\n");
1428 append_template(prog
, prog_hdr
);
1429 add_scaler(YUV_LUM_SCALER(type
), prog
, lum_scale_texs
,
1430 '0', 'r', rect
, texw
, texh
, params
->filter_strength
);
1431 add_scaler(YUV_CHROM_SCALER(type
), prog
,
1432 chrom_scale_texs
, '1', 'g', rect
, params
->chrom_texw
,
1433 params
->chrom_texh
, params
->filter_strength
);
1434 add_scaler(YUV_CHROM_SCALER(type
), prog
,
1435 chrom_scale_texs
, '2', 'b', rect
, params
->chrom_texw
,
1436 params
->chrom_texh
, params
->filter_strength
);
1437 mp_get_yuv2rgb_coeffs(¶ms
->csp_params
, yuv2rgb
);
1438 switch (YUV_CONVERSION(type
)) {
1439 case YUV_CONVERSION_FRAGMENT
:
1440 append_template(prog
, yuv_prog_template
);
1442 case YUV_CONVERSION_FRAGMENT_POW
:
1443 append_template(prog
, yuv_pow_prog_template
);
1445 case YUV_CONVERSION_FRAGMENT_LOOKUP
:
1446 append_template(prog
, yuv_lookup_prog_template
);
1448 case YUV_CONVERSION_FRAGMENT_LOOKUP3D
:
1449 append_template(prog
, yuv_lookup3d_prog_template
);
1452 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] unknown conversion type %i\n",
1453 YUV_CONVERSION(type
));
1456 for (int r
= 0; r
< 3; r
++) {
1457 for (int c
= 0; c
< 4; c
++) {
1459 char var
[] = { 'c', 'm', '1' + r
, '1' + c
, '\0' };
1460 replace_var_float(prog
, var
, yuv2rgb
[r
][c
]);
1463 replace_var_float(prog
, "gamma_r", (float)1.0 / params
->csp_params
.rgamma
);
1464 replace_var_float(prog
, "gamma_g", (float)1.0 / params
->csp_params
.ggamma
);
1465 replace_var_float(prog
, "gamma_b", (float)1.0 / params
->csp_params
.bgamma
);
1466 replace_var_char(prog
, "conv_tex0", conv_texs
[0]);
1469 // 1.0 strength is suitable for dithering 8 to 6 bit
1470 double str
= params
->noise_strength
* (1.0 / 64);
1471 double scale_x
= (double)NOISE_RES
/ texw
;
1472 double scale_y
= (double)NOISE_RES
/ texh
;
1477 append_template(prog
, noise_filt_template
);
1478 replace_var_float(prog
, "noise_sx", scale_x
);
1479 replace_var_float(prog
, "noise_sy", scale_y
);
1480 replace_var_char(prog
, "noise_filt_tex", filt_texs
[0]);
1481 replace_var_float(prog
, "noise_str", str
);
1484 append_template(prog
, "MOV result.color.rgb, res;\nEND");
1486 mp_msg(MSGT_VO
, MSGL_DBG2
, "[gl] generated fragment program:\n%s\n",
1488 loadGPUProgram(gl
, GL_FRAGMENT_PROGRAM
, yuv_prog
);
1489 talloc_free(yuv_prog
);
1493 * \brief detect the best YUV->RGB conversion method available
1495 int glAutodetectYUVConversion(GL
*gl
)
1497 const char *extensions
= gl
->GetString(GL_EXTENSIONS
);
1498 if (!extensions
|| !gl
->MultiTexCoord2f
)
1499 return YUV_CONVERSION_NONE
;
1500 if (strstr(extensions
, "GL_ARB_fragment_program"))
1501 return YUV_CONVERSION_FRAGMENT
;
1502 if (strstr(extensions
, "GL_ATI_text_fragment_shader"))
1503 return YUV_CONVERSION_TEXT_FRAGMENT
;
1504 if (strstr(extensions
, "GL_ATI_fragment_shader"))
1505 return YUV_CONVERSION_COMBINERS_ATI
;
1506 return YUV_CONVERSION_NONE
;
1510 * \brief setup YUV->RGB conversion
1511 * \param parms struct containing parameters like conversion and scaler type,
1513 * \ingroup glconversion
1515 void glSetupYUVConversion(GL
*gl
, gl_conversion_params_t
*params
)
1517 if (params
->chrom_texw
== 0)
1518 params
->chrom_texw
= 1;
1519 if (params
->chrom_texh
== 0)
1520 params
->chrom_texh
= 1;
1521 switch (YUV_CONVERSION(params
->type
)) {
1522 case YUV_CONVERSION_COMBINERS_ATI
:
1523 glSetupYUVFragmentATI(gl
, ¶ms
->csp_params
, 0);
1525 case YUV_CONVERSION_TEXT_FRAGMENT
:
1526 glSetupYUVFragmentATI(gl
, ¶ms
->csp_params
, 1);
1528 case YUV_CONVERSION_FRAGMENT_LOOKUP
:
1529 case YUV_CONVERSION_FRAGMENT_LOOKUP3D
:
1530 case YUV_CONVERSION_FRAGMENT
:
1531 case YUV_CONVERSION_FRAGMENT_POW
:
1532 glSetupYUVFragprog(gl
, params
);
1534 case YUV_CONVERSION_NONE
:
1537 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] unknown conversion type %i\n",
1538 YUV_CONVERSION(params
->type
));
1543 * \brief enable the specified YUV conversion
1544 * \param target texture target for Y, U and V textures (e.g. GL_TEXTURE_2D)
1545 * \param type type of YUV conversion
1546 * \ingroup glconversion
1548 void glEnableYUVConversion(GL
*gl
, GLenum target
, int type
)
1550 switch (YUV_CONVERSION(type
)) {
1551 case YUV_CONVERSION_COMBINERS_ATI
:
1552 gl
->ActiveTexture(GL_TEXTURE1
);
1554 gl
->ActiveTexture(GL_TEXTURE2
);
1556 gl
->ActiveTexture(GL_TEXTURE0
);
1557 gl
->Enable(GL_FRAGMENT_SHADER_ATI
);
1559 case YUV_CONVERSION_TEXT_FRAGMENT
:
1560 gl
->ActiveTexture(GL_TEXTURE1
);
1562 gl
->ActiveTexture(GL_TEXTURE2
);
1564 gl
->ActiveTexture(GL_TEXTURE0
);
1565 gl
->Enable(GL_TEXT_FRAGMENT_SHADER_ATI
);
1567 case YUV_CONVERSION_FRAGMENT_LOOKUP3D
:
1568 case YUV_CONVERSION_FRAGMENT_LOOKUP
:
1569 case YUV_CONVERSION_FRAGMENT_POW
:
1570 case YUV_CONVERSION_FRAGMENT
:
1571 case YUV_CONVERSION_NONE
:
1572 gl
->Enable(GL_FRAGMENT_PROGRAM
);
1578 * \brief disable the specified YUV conversion
1579 * \param target texture target for Y, U and V textures (e.g. GL_TEXTURE_2D)
1580 * \param type type of YUV conversion
1581 * \ingroup glconversion
1583 void glDisableYUVConversion(GL
*gl
, GLenum target
, int type
)
1585 switch (YUV_CONVERSION(type
)) {
1586 case YUV_CONVERSION_COMBINERS_ATI
:
1587 gl
->ActiveTexture(GL_TEXTURE1
);
1588 gl
->Disable(target
);
1589 gl
->ActiveTexture(GL_TEXTURE2
);
1590 gl
->Disable(target
);
1591 gl
->ActiveTexture(GL_TEXTURE0
);
1592 gl
->Disable(GL_FRAGMENT_SHADER_ATI
);
1594 case YUV_CONVERSION_TEXT_FRAGMENT
:
1595 gl
->Disable(GL_TEXT_FRAGMENT_SHADER_ATI
);
1596 // HACK: at least the Mac OS X 10.5 PPC Radeon drivers are broken and
1597 // without this disable the texture units while the program is still
1598 // running (10.4 PPC seems to work without this though).
1600 gl
->ActiveTexture(GL_TEXTURE1
);
1601 gl
->Disable(target
);
1602 gl
->ActiveTexture(GL_TEXTURE2
);
1603 gl
->Disable(target
);
1604 gl
->ActiveTexture(GL_TEXTURE0
);
1606 case YUV_CONVERSION_FRAGMENT_LOOKUP3D
:
1607 case YUV_CONVERSION_FRAGMENT_LOOKUP
:
1608 case YUV_CONVERSION_FRAGMENT_POW
:
1609 case YUV_CONVERSION_FRAGMENT
:
1610 case YUV_CONVERSION_NONE
:
1611 gl
->Disable(GL_FRAGMENT_PROGRAM
);
1616 void glEnable3DLeft(GL
*gl
, int type
)
1620 case GL_3D_RED_CYAN
:
1621 gl
->ColorMask(GL_TRUE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
1623 case GL_3D_GREEN_MAGENTA
:
1624 gl
->ColorMask(GL_FALSE
, GL_TRUE
, GL_FALSE
, GL_FALSE
);
1626 case GL_3D_QUADBUFFER
:
1627 gl
->GetIntegerv(GL_DRAW_BUFFER
, &buffer
);
1631 case GL_FRONT_RIGHT
:
1632 buffer
= GL_FRONT_LEFT
;
1637 buffer
= GL_BACK_LEFT
;
1640 gl
->DrawBuffer(buffer
);
1645 void glEnable3DRight(GL
*gl
, int type
)
1649 case GL_3D_RED_CYAN
:
1650 gl
->ColorMask(GL_FALSE
, GL_TRUE
, GL_TRUE
, GL_FALSE
);
1652 case GL_3D_GREEN_MAGENTA
:
1653 gl
->ColorMask(GL_TRUE
, GL_FALSE
, GL_TRUE
, GL_FALSE
);
1655 case GL_3D_QUADBUFFER
:
1656 gl
->GetIntegerv(GL_DRAW_BUFFER
, &buffer
);
1660 case GL_FRONT_RIGHT
:
1661 buffer
= GL_FRONT_RIGHT
;
1666 buffer
= GL_BACK_RIGHT
;
1669 gl
->DrawBuffer(buffer
);
1674 void glDisable3D(GL
*gl
, int type
)
1678 case GL_3D_RED_CYAN
:
1679 case GL_3D_GREEN_MAGENTA
:
1680 gl
->ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
1682 case GL_3D_QUADBUFFER
:
1683 gl
->DrawBuffer(vo_doublebuffering
? GL_BACK
: GL_FRONT
);
1684 gl
->GetIntegerv(GL_DRAW_BUFFER
, &buffer
);
1688 case GL_FRONT_RIGHT
:
1697 gl
->DrawBuffer(buffer
);
1703 * \brief draw a texture part at given 2D coordinates
1704 * \param x screen top coordinate
1705 * \param y screen left coordinate
1706 * \param w screen width coordinate
1707 * \param h screen height coordinate
1708 * \param tx texture top coordinate in pixels
1709 * \param ty texture left coordinate in pixels
1710 * \param tw texture part width in pixels
1711 * \param th texture part height in pixels
1712 * \param sx width of texture in pixels
1713 * \param sy height of texture in pixels
1714 * \param rect_tex whether this texture uses texture_rectangle extension
1715 * \param is_yv12 if != 0, also draw the textures from units 1 and 2,
1716 * bits 8 - 15 and 16 - 23 specify the x and y scaling of those textures
1717 * \param flip flip the texture upside down
1718 * \ingroup gltexture
1720 void glDrawTex(GL
*gl
, GLfloat x
, GLfloat y
, GLfloat w
, GLfloat h
,
1721 GLfloat tx
, GLfloat ty
, GLfloat tw
, GLfloat th
,
1722 int sx
, int sy
, int rect_tex
, int is_yv12
, int flip
)
1724 int chroma_x_shift
= (is_yv12
>> 8) & 31;
1725 int chroma_y_shift
= (is_yv12
>> 16) & 31;
1726 GLfloat xscale
= 1 << chroma_x_shift
;
1727 GLfloat yscale
= 1 << chroma_y_shift
;
1728 GLfloat tx2
= tx
/ xscale
, ty2
= ty
/ yscale
, tw2
= tw
/ xscale
, th2
= th
/ yscale
;
1734 tx2
= tx
, ty2
= ty
, tw2
= tw
, th2
= th
;
1740 gl
->Begin(GL_QUADS
);
1741 gl
->TexCoord2f(tx
, ty
);
1743 gl
->MultiTexCoord2f(GL_TEXTURE1
, tx2
, ty2
);
1744 gl
->MultiTexCoord2f(GL_TEXTURE2
, tx2
, ty2
);
1747 gl
->TexCoord2f(tx
, ty
+ th
);
1749 gl
->MultiTexCoord2f(GL_TEXTURE1
, tx2
, ty2
+ th2
);
1750 gl
->MultiTexCoord2f(GL_TEXTURE2
, tx2
, ty2
+ th2
);
1752 gl
->Vertex2f(x
, y
+ h
);
1753 gl
->TexCoord2f(tx
+ tw
, ty
+ th
);
1755 gl
->MultiTexCoord2f(GL_TEXTURE1
, tx2
+ tw2
, ty2
+ th2
);
1756 gl
->MultiTexCoord2f(GL_TEXTURE2
, tx2
+ tw2
, ty2
+ th2
);
1758 gl
->Vertex2f(x
+ w
, y
+ h
);
1759 gl
->TexCoord2f(tx
+ tw
, ty
);
1761 gl
->MultiTexCoord2f(GL_TEXTURE1
, tx2
+ tw2
, ty2
);
1762 gl
->MultiTexCoord2f(GL_TEXTURE2
, tx2
+ tw2
, ty2
);
1764 gl
->Vertex2f(x
+ w
, y
);
1768 #ifdef CONFIG_GL_COCOA
1769 #include "cocoa_common.h"
1770 static int create_window_cocoa(struct MPGLContext
*ctx
, uint32_t d_width
,
1771 uint32_t d_height
, uint32_t flags
)
1773 if (vo_cocoa_create_window(ctx
->vo
, d_width
, d_height
, flags
) == 0) {
1774 return SET_WINDOW_OK
;
1776 return SET_WINDOW_FAILED
;
1779 static int setGlWindow_cocoa(MPGLContext
*ctx
)
1781 vo_cocoa_change_attributes(ctx
->vo
);
1782 getFunctions(ctx
->gl
, (void *)vo_cocoa_glgetaddr
, NULL
, false);
1783 if (!ctx
->gl
->SwapInterval
)
1784 ctx
->gl
->SwapInterval
= vo_cocoa_swap_interval
;
1785 return SET_WINDOW_OK
;
1788 static void releaseGlContext_cocoa(MPGLContext
*ctx
)
1792 static void swapGlBuffers_cocoa(MPGLContext
*ctx
)
1794 vo_cocoa_swap_buffers();
1797 static int cocoa_check_events(struct vo
*vo
)
1799 return vo_cocoa_check_events(vo
);
1802 static void cocoa_update_xinerama_info(struct vo
*vo
)
1804 vo_cocoa_update_xinerama_info(vo
);
1807 static void cocoa_fullscreen(struct vo
*vo
)
1809 vo_cocoa_fullscreen(vo
);
1813 #ifdef CONFIG_GL_WIN32
1814 #include "w32_common.h"
1817 static int create_window_w32(struct MPGLContext
*ctx
, uint32_t d_width
,
1818 uint32_t d_height
, uint32_t flags
)
1820 if (!vo_w32_config(d_width
, d_height
, flags
))
1826 * \brief little helper since wglGetProcAddress definition does not fit our
1828 * \param procName name of function to look up
1829 * \return function pointer returned by wglGetProcAddress
1831 static void *w32gpa(const GLubyte
*procName
)
1834 void *res
= wglGetProcAddress(procName
);
1837 oglmod
= GetModuleHandle("opengl32.dll");
1838 return GetProcAddress(oglmod
, procName
);
1841 static int create_window_w32_gl3(struct MPGLContext
*ctx
, int gl_flags
,
1842 int gl_version
, uint32_t d_width
,
1843 uint32_t d_height
, uint32_t flags
) {
1844 if (!vo_w32_config(d_width
, d_height
, flags
))
1847 HGLRC
*context
= &ctx
->context
.w32
;
1849 if (*context
) // reuse existing context
1850 return 0; // not reusing it breaks gl3!
1852 HWND win
= vo_w32_window
;
1853 HDC windc
= vo_w32_get_dc(win
);
1854 HGLRC new_context
= 0;
1856 new_context
= wglCreateContext(windc
);
1858 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Could not create GL context!\n");
1863 if (!wglMakeCurrent(windc
, new_context
)) {
1864 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Could not set GL context!\n");
1868 const char *(GLAPIENTRY
*wglGetExtensionsStringARB
)(HDC hdc
)
1869 = w32gpa((const GLubyte
*)"wglGetExtensionsStringARB");
1871 if (!wglGetExtensionsStringARB
)
1874 const char *wgl_exts
= wglGetExtensionsStringARB(windc
);
1875 if (!strstr(wgl_exts
, "WGL_ARB_create_context"))
1878 HGLRC (GLAPIENTRY
*wglCreateContextAttribsARB
)(HDC hDC
, HGLRC hShareContext
,
1879 const int *attribList
)
1880 = w32gpa((const GLubyte
*)"wglCreateContextAttribsARB");
1882 if (!wglCreateContextAttribsARB
)
1886 WGL_CONTEXT_MAJOR_VERSION_ARB
, MPGL_VER_GET_MAJOR(gl_version
),
1887 WGL_CONTEXT_MINOR_VERSION_ARB
, MPGL_VER_GET_MINOR(gl_version
),
1888 WGL_CONTEXT_FLAGS_ARB
, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
,
1889 WGL_CONTEXT_PROFILE_MASK_ARB
, WGL_CONTEXT_CORE_PROFILE_BIT_ARB
,
1893 *context
= wglCreateContextAttribsARB(windc
, 0, attribs
);
1895 // NVidia, instead of ignoring WGL_CONTEXT_FLAGS_ARB, will error out if
1896 // it's present on pre-3.2 contexts.
1897 // Remove it from attribs and retry the context creation.
1898 attribs
[6] = attribs
[7] = 0;
1899 *context
= wglCreateContextAttribsARB(windc
, 0, attribs
);
1902 int err
= GetLastError();
1903 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Could not create an OpenGL 3.x"
1904 " context: error 0x%x\n", err
);
1908 wglMakeCurrent(NULL
, NULL
);
1909 wglDeleteContext(new_context
);
1911 if (!wglMakeCurrent(windc
, *context
)) {
1912 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Could not set GL3 context!\n");
1913 wglDeleteContext(*context
);
1917 /* update function pointers */
1918 getFunctions(ctx
->gl
, w32gpa
, NULL
, true);
1920 int pfmt
= GetPixelFormat(windc
);
1921 PIXELFORMATDESCRIPTOR pfd
;
1922 if (DescribePixelFormat(windc
, pfmt
, sizeof(PIXELFORMATDESCRIPTOR
), &pfd
)) {
1923 ctx
->depth_r
= pfd
.cRedBits
;
1924 ctx
->depth_g
= pfd
.cGreenBits
;
1925 ctx
->depth_b
= pfd
.cBlueBits
;
1931 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] The current OpenGL implementation does"
1932 " not support OpenGL 3.x \n");
1934 wglDeleteContext(new_context
);
1938 static int setGlWindow_w32(MPGLContext
*ctx
)
1940 HWND win
= vo_w32_window
;
1941 int *vinfo
= &ctx
->vinfo
.w32
;
1942 HGLRC
*context
= &ctx
->context
.w32
;
1944 HDC windc
= vo_w32_get_dc(win
);
1945 HGLRC new_context
= 0;
1946 int keep_context
= 0;
1947 int res
= SET_WINDOW_FAILED
;
1950 // should only be needed when keeping context, but not doing glFinish
1951 // can cause flickering even when we do not keep it.
1954 new_vinfo
= GetPixelFormat(windc
);
1955 if (*context
&& *vinfo
&& new_vinfo
&& *vinfo
== new_vinfo
) {
1956 // we can keep the wglContext
1957 new_context
= *context
;
1961 new_context
= wglCreateContext(windc
);
1963 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Could not create GL context!\n");
1969 if (!wglMakeCurrent(windc
, new_context
)) {
1970 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Could not set GL context!\n");
1972 wglDeleteContext(new_context
);
1977 vo_w32_window
= win
;
1980 GetClientRect(win
, &rect
);
1981 ctx
->vo
->dwidth
= rect
.right
;
1982 ctx
->vo
->dheight
= rect
.bottom
;
1984 if (!keep_context
) {
1986 wglDeleteContext(*context
);
1987 *context
= new_context
;
1990 getFunctions(ctx
->gl
, w32gpa
, NULL
, false);
1992 // and inform that reinit is neccessary
1993 res
= SET_WINDOW_REINIT
;
1995 res
= SET_WINDOW_OK
;
1998 vo_w32_release_dc(win
, windc
);
2002 static void releaseGlContext_w32(MPGLContext
*ctx
)
2004 int *vinfo
= &ctx
->vinfo
.w32
;
2005 HGLRC
*context
= &ctx
->context
.w32
;
2008 wglMakeCurrent(0, 0);
2009 wglDeleteContext(*context
);
2014 static void swapGlBuffers_w32(MPGLContext
*ctx
)
2016 HDC vo_hdc
= vo_w32_get_dc(vo_w32_window
);
2017 SwapBuffers(vo_hdc
);
2018 vo_w32_release_dc(vo_w32_window
, vo_hdc
);
2021 //trivial wrappers (w32 code uses old vo API)
2022 static void new_vo_w32_ontop(struct vo
*vo
) { vo_w32_ontop(); }
2023 static void new_vo_w32_border(struct vo
*vo
) { vo_w32_border(); }
2024 static void new_vo_w32_fullscreen(struct vo
*vo
) { vo_w32_fullscreen(); }
2025 static int new_vo_w32_check_events(struct vo
*vo
) { return vo_w32_check_events(); }
2026 static void new_w32_update_xinerama_info(struct vo
*vo
) { w32_update_xinerama_info(); }
2028 #ifdef CONFIG_GL_X11
2029 #include "x11_common.h"
2031 static int create_window_x11(struct MPGLContext
*ctx
, uint32_t d_width
,
2032 uint32_t d_height
, uint32_t flags
)
2034 struct vo
*vo
= ctx
->vo
;
2036 static int default_glx_attribs
[] = {
2037 GLX_RGBA
, GLX_RED_SIZE
, 1, GLX_GREEN_SIZE
, 1, GLX_BLUE_SIZE
, 1,
2038 GLX_DOUBLEBUFFER
, None
2040 static int stereo_glx_attribs
[] = {
2041 GLX_RGBA
, GLX_RED_SIZE
, 1, GLX_GREEN_SIZE
, 1, GLX_BLUE_SIZE
, 1,
2042 GLX_DOUBLEBUFFER
, GLX_STEREO
, None
2044 XVisualInfo
*vinfo
= NULL
;
2045 if (flags
& VOFLAG_STEREO
) {
2046 vinfo
= glXChooseVisual(vo
->x11
->display
, vo
->x11
->screen
,
2047 stereo_glx_attribs
);
2049 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] Could not find a stereo visual,"
2050 " 3D will probably not work!\n");
2053 vinfo
= glXChooseVisual(vo
->x11
->display
, vo
->x11
->screen
,
2054 default_glx_attribs
);
2056 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] no GLX support present\n");
2059 mp_msg(MSGT_VO
, MSGL_V
, "[gl] GLX chose visual with ID 0x%x\n",
2060 (int)vinfo
->visualid
);
2062 Colormap colormap
= XCreateColormap(vo
->x11
->display
, vo
->x11
->rootwin
,
2063 vinfo
->visual
, AllocNone
);
2064 vo_x11_create_vo_window(vo
, vinfo
, vo
->dx
, vo
->dy
, d_width
, d_height
,
2065 flags
, colormap
, "gl");
2071 * \brief Returns the XVisualInfo associated with Window win.
2072 * \param win Window whose XVisualInfo is returne.
2073 * \return XVisualInfo of the window. Caller must use XFree to free it.
2075 static XVisualInfo
*getWindowVisualInfo(MPGLContext
*ctx
, Window win
)
2077 XWindowAttributes xw_attr
;
2078 XVisualInfo vinfo_template
;
2080 XGetWindowAttributes(ctx
->vo
->x11
->display
, win
, &xw_attr
);
2081 vinfo_template
.visualid
= XVisualIDFromVisual(xw_attr
.visual
);
2082 return XGetVisualInfo(ctx
->vo
->x11
->display
, VisualIDMask
, &vinfo_template
, &tmp
);
2085 static char *get_glx_exts(MPGLContext
*ctx
)
2087 Display
*display
= ctx
->vo
->x11
->display
;
2088 const char *(*glXExtStr
)(Display
*, int);
2089 char *glxstr
= talloc_strdup(NULL
, "");
2091 glXExtStr
= getdladdr("glXQueryExtensionsString");
2093 glxstr
= talloc_asprintf_append(glxstr
, " %s",
2094 glXExtStr(display
, ctx
->vo
->x11
->screen
));
2095 glXExtStr
= getdladdr("glXGetClientString");
2097 glxstr
= talloc_asprintf_append(glxstr
, " %s",
2098 glXExtStr(display
, GLX_EXTENSIONS
));
2099 glXExtStr
= getdladdr("glXGetServerString");
2101 glxstr
= talloc_asprintf_append(glxstr
, " %s",
2102 glXExtStr(display
, GLX_EXTENSIONS
));
2108 * \brief Changes the window in which video is displayed.
2109 * If possible only transfers the context to the new window, otherwise
2110 * creates a new one, which must be initialized by the caller.
2111 * \param vinfo Currently used visual.
2112 * \param context Currently used context.
2113 * \param win window that should be used for drawing.
2114 * \return one of SET_WINDOW_FAILED, SET_WINDOW_OK or SET_WINDOW_REINIT.
2115 * In case of SET_WINDOW_REINIT the context could not be transfered
2116 * and the caller must initialize it correctly.
2117 * \ingroup glcontext
2119 static int setGlWindow_x11(MPGLContext
*ctx
)
2121 XVisualInfo
**vinfo
= &ctx
->vinfo
.x11
;
2122 GLXContext
*context
= &ctx
->context
.x11
;
2123 Display
*display
= ctx
->vo
->x11
->display
;
2124 Window win
= ctx
->vo
->x11
->window
;
2125 XVisualInfo
*new_vinfo
;
2126 GLXContext new_context
= NULL
;
2127 int keep_context
= 0;
2130 // should only be needed when keeping context, but not doing glFinish
2131 // can cause flickering even when we do not keep it.
2134 new_vinfo
= getWindowVisualInfo(ctx
, win
);
2135 if (*context
&& *vinfo
&& new_vinfo
&&
2136 (*vinfo
)->visualid
== new_vinfo
->visualid
) {
2137 // we can keep the GLXContext
2138 new_context
= *context
;
2144 new_context
= glXCreateContext(display
, new_vinfo
, NULL
, True
);
2146 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Could not create GLX context!\n");
2148 return SET_WINDOW_FAILED
;
2153 if (!glXMakeCurrent(display
, ctx
->vo
->x11
->window
, new_context
)) {
2154 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Could not set GLX context!\n");
2155 if (!keep_context
) {
2156 glXDestroyContext(display
, new_context
);
2159 return SET_WINDOW_FAILED
;
2163 ctx
->vo
->x11
->window
= win
;
2164 vo_x11_update_geometry(ctx
->vo
, 1);
2165 if (!keep_context
) {
2166 void *(*getProcAddress
)(const GLubyte
*);
2168 glXDestroyContext(display
, *context
);
2169 *context
= new_context
;
2173 getProcAddress
= getdladdr("glXGetProcAddress");
2174 if (!getProcAddress
)
2175 getProcAddress
= getdladdr("glXGetProcAddressARB");
2177 char *glxstr
= get_glx_exts(ctx
);
2179 getFunctions(gl
, getProcAddress
, glxstr
, false);
2180 if (!gl
->GenPrograms
&& gl
->GetString
&&
2182 strstr(gl
->GetString(GL_EXTENSIONS
), "GL_ARB_vertex_program")) {
2183 mp_msg(MSGT_VO
, MSGL_WARN
,
2184 "Broken glXGetProcAddress detected, trying workaround\n");
2185 getFunctions(gl
, NULL
, glxstr
, false);
2188 talloc_free(glxstr
);
2190 // and inform that reinit is neccessary
2191 return SET_WINDOW_REINIT
;
2193 return SET_WINDOW_OK
;
2196 // The GL3 initialization code roughly follows/copies from:
2197 // http://www.opengl.org/wiki/Tutorial:_OpenGL_3.0_Context_Creation_(GLX)
2198 // but also uses some of the old code.
2200 static GLXFBConfig
select_fb_config(struct vo
*vo
, const int *attribs
)
2203 GLXFBConfig
*fbc
= glXChooseFBConfig(vo
->x11
->display
, vo
->x11
->screen
,
2208 // The list in fbc is sorted (so that the first element is the best).
2209 GLXFBConfig fbconfig
= fbc
[0];
2216 typedef GLXContext (*glXCreateContextAttribsARBProc
)
2217 (Display
*, GLXFBConfig
, GLXContext
, Bool
, const int*);
2219 static int create_window_x11_gl3(struct MPGLContext
*ctx
, int gl_flags
,
2220 int gl_version
, uint32_t d_width
,
2221 uint32_t d_height
, uint32_t flags
)
2223 struct vo
*vo
= ctx
->vo
;
2225 if (ctx
->context
.x11
) {
2226 // GL context and window already exist.
2227 // Only update window geometry etc.
2228 Colormap colormap
= XCreateColormap(vo
->x11
->display
, vo
->x11
->rootwin
,
2229 ctx
->vinfo
.x11
->visual
, AllocNone
);
2230 vo_x11_create_vo_window(vo
, ctx
->vinfo
.x11
, vo
->dx
, vo
->dy
, d_width
,
2231 d_height
, flags
, colormap
, "gl");
2232 XFreeColormap(vo
->x11
->display
, colormap
);
2233 return SET_WINDOW_OK
;
2236 int glx_major
, glx_minor
;
2238 // FBConfigs were added in GLX version 1.3.
2239 if (!glXQueryVersion(vo
->x11
->display
, &glx_major
, &glx_minor
) ||
2240 (MPGL_VER(glx_major
, glx_minor
) < MPGL_VER(1, 3)))
2242 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] GLX version older than 1.3.\n");
2243 return SET_WINDOW_FAILED
;
2246 const int glx_attribs_stereo_value_idx
= 1; // index of GLX_STEREO + 1
2247 int glx_attribs
[] = {
2249 GLX_X_RENDERABLE
, True
,
2253 GLX_DOUBLEBUFFER
, True
,
2256 GLXFBConfig fbc
= NULL
;
2257 if (flags
& VOFLAG_STEREO
) {
2258 glx_attribs
[glx_attribs_stereo_value_idx
] = True
;
2259 fbc
= select_fb_config(vo
, glx_attribs
);
2261 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] Could not find a stereo visual,"
2262 " 3D will probably not work!\n");
2263 glx_attribs
[glx_attribs_stereo_value_idx
] = False
;
2267 fbc
= select_fb_config(vo
, glx_attribs
);
2269 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] no GLX support present\n");
2270 return SET_WINDOW_FAILED
;
2273 glXGetFBConfigAttrib(vo
->x11
->display
, fbc
, GLX_RED_SIZE
, &ctx
->depth_r
);
2274 glXGetFBConfigAttrib(vo
->x11
->display
, fbc
, GLX_GREEN_SIZE
, &ctx
->depth_g
);
2275 glXGetFBConfigAttrib(vo
->x11
->display
, fbc
, GLX_BLUE_SIZE
, &ctx
->depth_b
);
2277 XVisualInfo
*vinfo
= glXGetVisualFromFBConfig(vo
->x11
->display
, fbc
);
2278 mp_msg(MSGT_VO
, MSGL_V
, "[gl] GLX chose visual with ID 0x%x\n",
2279 (int)vinfo
->visualid
);
2280 Colormap colormap
= XCreateColormap(vo
->x11
->display
, vo
->x11
->rootwin
,
2281 vinfo
->visual
, AllocNone
);
2282 vo_x11_create_vo_window(vo
, vinfo
, vo
->dx
, vo
->dy
, d_width
, d_height
,
2283 flags
, colormap
, "gl");
2284 XFreeColormap(vo
->x11
->display
, colormap
);
2286 glXCreateContextAttribsARBProc glXCreateContextAttribsARB
=
2287 (glXCreateContextAttribsARBProc
)
2288 glXGetProcAddressARB((const GLubyte
*)"glXCreateContextAttribsARB");
2290 char *glxstr
= get_glx_exts(ctx
);
2291 bool have_ctx_ext
= !!strstr(glxstr
, "GLX_ARB_create_context");
2293 if (!(have_ctx_ext
&& glXCreateContextAttribsARB
))
2296 talloc_free(glxstr
);
2297 return SET_WINDOW_FAILED
;
2300 int context_attribs
[] = {
2301 GLX_CONTEXT_MAJOR_VERSION_ARB
, MPGL_VER_GET_MAJOR(gl_version
),
2302 GLX_CONTEXT_MINOR_VERSION_ARB
, MPGL_VER_GET_MINOR(gl_version
),
2303 GLX_CONTEXT_PROFILE_MASK_ARB
, GLX_CONTEXT_CORE_PROFILE_BIT_ARB
,
2304 GLX_CONTEXT_FLAGS_ARB
, GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB
2305 | (gl_flags
& MPGLFLAG_DEBUG
? GLX_CONTEXT_DEBUG_BIT_ARB
: 0),
2308 GLXContext context
= glXCreateContextAttribsARB(vo
->x11
->display
, fbc
, 0,
2309 True
, context_attribs
);
2311 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Could not create GLX context!\n");
2313 talloc_free(glxstr
);
2314 return SET_WINDOW_FAILED
;
2318 if (!glXMakeCurrent(vo
->x11
->display
, vo
->x11
->window
, context
)) {
2319 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Could not set GLX context!\n");
2320 glXDestroyContext(vo
->x11
->display
, context
);
2322 talloc_free(glxstr
);
2323 return SET_WINDOW_FAILED
;
2326 ctx
->vinfo
.x11
= vinfo
;
2327 ctx
->context
.x11
= context
;
2329 getFunctions(ctx
->gl
, (void *)glXGetProcAddress
, glxstr
, true);
2331 talloc_free(glxstr
);
2333 return SET_WINDOW_REINIT
;
2337 * \brief free the VisualInfo and GLXContext of an OpenGL context.
2338 * \ingroup glcontext
2340 static void releaseGlContext_x11(MPGLContext
*ctx
)
2342 XVisualInfo
**vinfo
= &ctx
->vinfo
.x11
;
2343 GLXContext
*context
= &ctx
->context
.x11
;
2344 Display
*display
= ctx
->vo
->x11
->display
;
2351 glXMakeCurrent(display
, None
, NULL
);
2352 glXDestroyContext(display
, *context
);
2357 static void swapGlBuffers_x11(MPGLContext
*ctx
)
2359 glXSwapBuffers(ctx
->vo
->x11
->display
, ctx
->vo
->x11
->window
);
2363 #ifdef CONFIG_GL_SDL
2364 #include "sdl_common.h"
2366 static int create_window_sdl(struct MPGLContext
*ctx
, uint32_t d_width
,
2367 uint32_t d_height
, uint32_t flags
)
2369 SDL_WM_SetCaption(vo_get_window_title(ctx
->vo
), NULL
);
2370 ctx
->vo
->dwidth
= d_width
;
2371 ctx
->vo
->dheight
= d_height
;
2375 static void swapGlBuffers_sdl(MPGLContext
*ctx
)
2377 SDL_GL_SwapBuffers();
2380 static void *sdlgpa(const GLubyte
*name
)
2382 return SDL_GL_GetProcAddress(name
);
2385 static int setGlWindow_sdl(MPGLContext
*ctx
)
2387 if (sdl_set_mode(0, SDL_OPENGL
| SDL_RESIZABLE
) < 0)
2388 return SET_WINDOW_FAILED
;
2389 SDL_GL_LoadLibrary(NULL
);
2390 getFunctions(ctx
->gl
, sdlgpa
, NULL
, false);
2391 return SET_WINDOW_OK
;
2394 static void releaseGlContext_sdl(MPGLContext
*ctx
)
2398 static int sdl_check_events(struct vo
*vo
)
2402 while (SDL_PollEvent(&event
))
2403 res
|= sdl_default_handle_event(&event
);
2404 // poll "events" from within MPlayer code
2405 res
|= sdl_default_handle_event(NULL
);
2406 if (res
& VO_EVENT_RESIZE
)
2407 sdl_set_mode(0, SDL_OPENGL
| SDL_RESIZABLE
);
2411 static void new_sdl_update_xinerama_info(struct vo
*vo
) { sdl_update_xinerama_info(); }
2412 static void new_vo_sdl_fullscreen(struct vo
*vo
) { vo_sdl_fullscreen(); }
2421 static struct backend backends
[] = {
2422 {"auto", GLTYPE_AUTO
},
2423 {"cocoa", GLTYPE_COCOA
},
2424 {"win", GLTYPE_W32
},
2425 {"x11", GLTYPE_X11
},
2426 {"sdl", GLTYPE_SDL
},
2427 // mplayer-svn aliases (note that mplayer-svn couples these with the numeric
2428 // values of the internal GLTYPE_* constants)
2429 {"-1", GLTYPE_AUTO
},
2437 int mpgl_find_backend(const char *name
)
2439 for (const struct backend
*entry
= backends
; entry
->name
; entry
++) {
2440 if (strcmp(entry
->name
, name
) == 0)
2446 MPGLContext
*init_mpglcontext(enum MPGLType type
, struct vo
*vo
)
2449 if (type
== GLTYPE_AUTO
) {
2450 ctx
= init_mpglcontext(GLTYPE_COCOA
, vo
);
2453 ctx
= init_mpglcontext(GLTYPE_W32
, vo
);
2456 ctx
= init_mpglcontext(GLTYPE_X11
, vo
);
2459 return init_mpglcontext(GLTYPE_SDL
, vo
);
2461 ctx
= talloc_zero(NULL
, MPGLContext
);
2462 ctx
->gl
= talloc_zero(ctx
, GL
);
2465 switch (ctx
->type
) {
2466 #ifdef CONFIG_GL_COCOA
2468 ctx
->create_window
= create_window_cocoa
;
2469 ctx
->setGlWindow
= setGlWindow_cocoa
;
2470 ctx
->releaseGlContext
= releaseGlContext_cocoa
;
2471 ctx
->swapGlBuffers
= swapGlBuffers_cocoa
;
2472 ctx
->check_events
= cocoa_check_events
;
2473 ctx
->update_xinerama_info
= cocoa_update_xinerama_info
;
2474 ctx
->fullscreen
= cocoa_fullscreen
;
2475 ctx
->ontop
= vo_cocoa_ontop
;
2476 if (vo_cocoa_init(vo
))
2480 #ifdef CONFIG_GL_WIN32
2482 ctx
->create_window
= create_window_w32
;
2483 ctx
->create_window_gl3
= create_window_w32_gl3
;
2484 ctx
->setGlWindow
= setGlWindow_w32
;
2485 ctx
->releaseGlContext
= releaseGlContext_w32
;
2486 ctx
->swapGlBuffers
= swapGlBuffers_w32
;
2487 ctx
->update_xinerama_info
= new_w32_update_xinerama_info
;
2488 ctx
->border
= new_vo_w32_border
;
2489 ctx
->check_events
= new_vo_w32_check_events
;
2490 ctx
->fullscreen
= new_vo_w32_fullscreen
;
2491 ctx
->ontop
= new_vo_w32_ontop
;
2492 //the win32 code is hardcoded to use the deprecated vo API
2498 #ifdef CONFIG_GL_X11
2500 ctx
->create_window
= create_window_x11
;
2501 ctx
->setGlWindow
= setGlWindow_x11
;
2502 ctx
->create_window_gl3
= create_window_x11_gl3
;
2503 ctx
->releaseGlContext
= releaseGlContext_x11
;
2504 ctx
->swapGlBuffers
= swapGlBuffers_x11
;
2505 ctx
->update_xinerama_info
= update_xinerama_info
;
2506 ctx
->border
= vo_x11_border
;
2507 ctx
->check_events
= vo_x11_check_events
;
2508 ctx
->fullscreen
= vo_x11_fullscreen
;
2509 ctx
->ontop
= vo_x11_ontop
;
2514 #ifdef CONFIG_GL_SDL
2516 ctx
->create_window
= create_window_sdl
;
2517 ctx
->setGlWindow
= setGlWindow_sdl
;
2518 ctx
->releaseGlContext
= releaseGlContext_sdl
;
2519 ctx
->swapGlBuffers
= swapGlBuffers_sdl
;
2520 ctx
->update_xinerama_info
= new_sdl_update_xinerama_info
;
2521 ctx
->check_events
= sdl_check_events
;
2522 ctx
->fullscreen
= new_vo_sdl_fullscreen
;
2523 //the SDL code is hardcoded to use the deprecated vo API
2534 int create_mpglcontext(struct MPGLContext
*ctx
, int gl_flags
, int gl_version
,
2535 uint32_t d_width
, uint32_t d_height
, uint32_t flags
)
2537 if (gl_version
< MPGL_VER(3, 0)) {
2538 if (ctx
->create_window(ctx
, d_width
, d_height
, flags
) < 0)
2539 return SET_WINDOW_FAILED
;
2540 return ctx
->setGlWindow(ctx
);
2542 if (!ctx
->create_window_gl3
) {
2543 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] OpenGL 3.x context creation not "
2545 return SET_WINDOW_FAILED
;
2547 return ctx
->create_window_gl3(ctx
, gl_flags
, gl_version
, d_width
,
2552 void uninit_mpglcontext(MPGLContext
*ctx
)
2556 ctx
->releaseGlContext(ctx
);
2557 switch (ctx
->type
) {
2558 #ifdef CONFIG_GL_COCOA
2560 vo_cocoa_uninit(ctx
->vo
);
2563 #ifdef CONFIG_GL_WIN32
2568 #ifdef CONFIG_GL_X11
2570 vo_x11_uninit(ctx
->vo
);
2573 #ifdef CONFIG_GL_SDL