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 "gl_common.h"
36 #include "libavutil/common.h"
38 void (GLAPIENTRY
*Begin
)(GLenum
);
39 void (GLAPIENTRY
*End
)(void);
40 void (GLAPIENTRY
*Viewport
)(GLint
, GLint
, GLsizei
, GLsizei
);
41 void (GLAPIENTRY
*MatrixMode
)(GLenum
);
42 void (GLAPIENTRY
*LoadIdentity
)(void);
43 void (GLAPIENTRY
*Scaled
)(double, double, double);
44 void (GLAPIENTRY
*Ortho
)(double, double, double, double, double, double);
45 void (GLAPIENTRY
*PushMatrix
)(void);
46 void (GLAPIENTRY
*PopMatrix
)(void);
47 void (GLAPIENTRY
*Clear
)(GLbitfield
);
48 GLuint (GLAPIENTRY
*GenLists
)(GLsizei
);
49 void (GLAPIENTRY
*DeleteLists
)(GLuint
, GLsizei
);
50 void (GLAPIENTRY
*NewList
)(GLuint
, GLenum
);
51 void (GLAPIENTRY
*EndList
)(void);
52 void (GLAPIENTRY
*CallList
)(GLuint
);
53 void (GLAPIENTRY
*CallLists
)(GLsizei
, GLenum
, const GLvoid
*);
54 void (GLAPIENTRY
*GenTextures
)(GLsizei
, GLuint
*);
55 void (GLAPIENTRY
*DeleteTextures
)(GLsizei
, const GLuint
*);
56 void (GLAPIENTRY
*TexEnvi
)(GLenum
, GLenum
, GLint
);
57 void (GLAPIENTRY
*Color4ub
)(GLubyte
, GLubyte
, GLubyte
, GLubyte
);
58 void (GLAPIENTRY
*Color3f
)(GLfloat
, GLfloat
, GLfloat
);
59 void (GLAPIENTRY
*ClearColor
)(GLclampf
, GLclampf
, GLclampf
, GLclampf
);
60 void (GLAPIENTRY
*Enable
)(GLenum
);
61 void (GLAPIENTRY
*Disable
)(GLenum
);
62 const GLubyte
*(GLAPIENTRY
*GetString
)(GLenum
);
63 void (GLAPIENTRY
*DrawBuffer
)(GLenum
);
64 void (GLAPIENTRY
*DepthMask
)(GLboolean
);
65 void (GLAPIENTRY
*BlendFunc
)(GLenum
, GLenum
);
66 void (GLAPIENTRY
*Flush
)(void);
67 void (GLAPIENTRY
*Finish
)(void);
68 void (GLAPIENTRY
*PixelStorei
)(GLenum
, GLint
);
69 void (GLAPIENTRY
*TexImage1D
)(GLenum
, GLint
, GLint
, GLsizei
, GLint
, GLenum
, GLenum
, const GLvoid
*);
70 void (GLAPIENTRY
*TexImage2D
)(GLenum
, GLint
, GLint
, GLsizei
, GLsizei
, GLint
, GLenum
, GLenum
, const GLvoid
*);
71 void (GLAPIENTRY
*TexSubImage2D
)(GLenum
, GLint
, GLint
, GLint
, GLsizei
, GLsizei
, GLenum
, GLenum
, const GLvoid
*);
72 void (GLAPIENTRY
*TexParameteri
)(GLenum
, GLenum
, GLint
);
73 void (GLAPIENTRY
*TexParameterf
)(GLenum
, GLenum
, GLfloat
);
74 void (GLAPIENTRY
*TexParameterfv
)(GLenum
, GLenum
, const GLfloat
*);
75 void (GLAPIENTRY
*TexCoord2f
)(GLfloat
, GLfloat
);
76 void (GLAPIENTRY
*Vertex2f
)(GLfloat
, GLfloat
);
77 void (GLAPIENTRY
*GetIntegerv
)(GLenum
, GLint
*);
80 * \defgroup glextfunctions OpenGL extension functions
82 * the pointers to these functions are acquired when the OpenGL
86 void (GLAPIENTRY
*GenBuffers
)(GLsizei
, GLuint
*);
87 void (GLAPIENTRY
*DeleteBuffers
)(GLsizei
, const GLuint
*);
88 void (GLAPIENTRY
*BindBuffer
)(GLenum
, GLuint
);
89 GLvoid
* (GLAPIENTRY
*MapBuffer
)(GLenum
, GLenum
);
90 GLboolean (GLAPIENTRY
*UnmapBuffer
)(GLenum
);
91 void (GLAPIENTRY
*BufferData
)(GLenum
, intptr_t, const GLvoid
*, GLenum
);
92 void (GLAPIENTRY
*CombinerParameterfv
)(GLenum
, const GLfloat
*);
93 void (GLAPIENTRY
*CombinerParameteri
)(GLenum
, GLint
);
94 void (GLAPIENTRY
*CombinerInput
)(GLenum
, GLenum
, GLenum
, GLenum
, GLenum
,
96 void (GLAPIENTRY
*CombinerOutput
)(GLenum
, GLenum
, GLenum
, GLenum
, GLenum
,
97 GLenum
, GLenum
, GLboolean
, GLboolean
,
99 void (GLAPIENTRY
*BeginFragmentShader
)(void);
100 void (GLAPIENTRY
*EndFragmentShader
)(void);
101 void (GLAPIENTRY
*SampleMap
)(GLuint
, GLuint
, GLenum
);
102 void (GLAPIENTRY
*ColorFragmentOp2
)(GLenum
, GLuint
, GLuint
, GLuint
, GLuint
,
103 GLuint
, GLuint
, GLuint
, GLuint
, GLuint
);
104 void (GLAPIENTRY
*ColorFragmentOp3
)(GLenum
, GLuint
, GLuint
, GLuint
, GLuint
,
105 GLuint
, GLuint
, GLuint
, GLuint
, GLuint
,
106 GLuint
, GLuint
, GLuint
);
107 void (GLAPIENTRY
*SetFragmentShaderConstant
)(GLuint
, const GLfloat
*);
108 void (GLAPIENTRY
*ActiveTexture
)(GLenum
);
109 void (GLAPIENTRY
*BindTexture
)(GLenum
, GLuint
);
110 void (GLAPIENTRY
*MultiTexCoord2f
)(GLenum
, GLfloat
, GLfloat
);
111 void (GLAPIENTRY
*GenPrograms
)(GLsizei
, GLuint
*);
112 void (GLAPIENTRY
*DeletePrograms
)(GLsizei
, const GLuint
*);
113 void (GLAPIENTRY
*BindProgram
)(GLenum
, GLuint
);
114 void (GLAPIENTRY
*ProgramString
)(GLenum
, GLenum
, GLsizei
, const GLvoid
*);
115 void (GLAPIENTRY
*GetProgramiv
)(GLenum
, GLenum
, GLint
*);
116 void (GLAPIENTRY
*ProgramEnvParameter4f
)(GLenum
, GLuint
, GLfloat
, GLfloat
,
118 int (GLAPIENTRY
*SwapInterval
)(int);
119 void (GLAPIENTRY
*TexImage3D
)(GLenum
, GLint
, GLenum
, GLsizei
, GLsizei
, GLsizei
,
120 GLint
, GLenum
, GLenum
, const GLvoid
*);
121 void* (GLAPIENTRY
*AllocateMemoryMESA
)(void *, int, size_t, float, float, float);
122 void (GLAPIENTRY
*FreeMemoryMESA
)(void *, int, void *);
123 /** \} */ // end of glextfunctions group
125 //! \defgroup glgeneral OpenGL general helper functions
127 //! \defgroup glcontext OpenGL context management helper functions
129 //! \defgroup gltexture OpenGL texture handling helper functions
131 //! \defgroup glconversion OpenGL conversion helper functions
133 static GLint hqtexfmt
;
136 * \brief adjusts the GL_UNPACK_ALIGNMENT to fit the stride.
137 * \param stride number of bytes per line for which alignment should fit.
140 void glAdjustAlignment(int stride
) {
144 else if (stride
% 4 == 0)
146 else if (stride
% 2 == 0)
150 PixelStorei(GL_UNPACK_ALIGNMENT
, gl_alignment
);
153 struct gl_name_map_struct
{
159 #define MAP(a) {a, #a}
160 //! mapping table for the glValName function
161 static const struct gl_name_map_struct gl_name_map
[] = {
163 MAP(GL_R3_G3_B2
), MAP(GL_RGB4
), MAP(GL_RGB5
), MAP(GL_RGB8
),
164 MAP(GL_RGB10
), MAP(GL_RGB12
), MAP(GL_RGB16
), MAP(GL_RGBA2
),
165 MAP(GL_RGBA4
), MAP(GL_RGB5_A1
), MAP(GL_RGBA8
), MAP(GL_RGB10_A2
),
166 MAP(GL_RGBA12
), MAP(GL_RGBA16
), MAP(GL_LUMINANCE8
),
169 MAP(GL_RGB
), MAP(GL_RGBA
), MAP(GL_RED
), MAP(GL_GREEN
), MAP(GL_BLUE
),
170 MAP(GL_ALPHA
), MAP(GL_LUMINANCE
), MAP(GL_LUMINANCE_ALPHA
),
173 MAP(GL_BGR
), MAP(GL_BGRA
),
176 MAP(GL_BYTE
), MAP(GL_UNSIGNED_BYTE
), MAP(GL_SHORT
), MAP(GL_UNSIGNED_SHORT
),
177 MAP(GL_INT
), MAP(GL_UNSIGNED_INT
), MAP(GL_FLOAT
), MAP(GL_DOUBLE
),
178 MAP(GL_2_BYTES
), MAP(GL_3_BYTES
), MAP(GL_4_BYTES
),
180 MAP(GL_UNSIGNED_BYTE_3_3_2
), MAP(GL_UNSIGNED_BYTE_2_3_3_REV
),
181 MAP(GL_UNSIGNED_SHORT_5_6_5
), MAP(GL_UNSIGNED_SHORT_5_6_5_REV
),
182 MAP(GL_UNSIGNED_SHORT_4_4_4_4
), MAP(GL_UNSIGNED_SHORT_4_4_4_4_REV
),
183 MAP(GL_UNSIGNED_SHORT_5_5_5_1
), MAP(GL_UNSIGNED_SHORT_1_5_5_5_REV
),
184 MAP(GL_UNSIGNED_INT_8_8_8_8
), MAP(GL_UNSIGNED_INT_8_8_8_8_REV
),
185 MAP(GL_UNSIGNED_INT_10_10_10_2
), MAP(GL_UNSIGNED_INT_2_10_10_10_REV
),
191 * \brief return the name of an OpenGL constant
192 * \param value the constant
193 * \return name of the constant or "Unknown format!"
196 const char *glValName(GLint value
)
200 while (gl_name_map
[i
].name
) {
201 if (gl_name_map
[i
].value
== value
)
202 return gl_name_map
[i
].name
;
205 return "Unknown format!";
208 //! always return this format as internal texture format in glFindFormat
209 #define TEXTUREFORMAT_ALWAYS GL_RGB8
210 #undef TEXTUREFORMAT_ALWAYS
213 * \brief find the OpenGL settings coresponding to format.
215 * All parameters may be NULL.
216 * \param fmt MPlayer format to analyze.
217 * \param bpp [OUT] bits per pixel of that format.
218 * \param gl_texfmt [OUT] internal texture format that fits the
219 * image format, not necessarily the best for performance.
220 * \param gl_format [OUT] OpenGL format for this image format.
221 * \param gl_type [OUT] OpenGL type for this image format.
222 * \return 1 if format is supported by OpenGL, 0 if not.
225 int glFindFormat(uint32_t fmt
, int *bpp
, GLint
*gl_texfmt
,
226 GLenum
*gl_format
, GLenum
*gl_type
)
232 if (!bpp
) bpp
= &dummy1
;
233 if (!gl_texfmt
) gl_texfmt
= &dummy3
;
234 if (!gl_format
) gl_format
= &dummy2
;
235 if (!gl_type
) gl_type
= &dummy2
;
237 *bpp
= IMGFMT_IS_BGR(fmt
)?IMGFMT_BGR_DEPTH(fmt
):IMGFMT_RGB_DEPTH(fmt
);
242 *gl_type
= GL_UNSIGNED_SHORT
;
246 *gl_type
= GL_UNSIGNED_BYTE
;
250 *gl_format
= GL_RGBA
;
251 *gl_type
= GL_UNSIGNED_BYTE
;
254 supported
= 0; // no native YV12 support
259 *gl_format
= GL_LUMINANCE
;
260 *gl_type
= GL_UNSIGNED_BYTE
;
264 *gl_texfmt
= GL_YCBCR_MESA
;
266 *gl_format
= GL_YCBCR_MESA
;
267 *gl_type
= fmt
== IMGFMT_UYVY
? GL_UNSIGNED_SHORT_8_8
: GL_UNSIGNED_SHORT_8_8_REV
;
270 // we do not support palettized formats, although the format the
271 // swscale produces works
274 gl_type
= GL_UNSIGNED_BYTE_2_3_3_REV
;
278 *gl_format
= GL_RGBA
;
279 *gl_type
= GL_UNSIGNED_SHORT_1_5_5_5_REV
;
283 *gl_type
= GL_UNSIGNED_SHORT_5_6_5_REV
;
287 // special case as red and blue have a differen number of bits.
288 // GL_BGR and GL_UNSIGNED_BYTE_3_3_2 isn't supported at least
289 // by nVidia drivers, and in addition would give more bits to
290 // blue than to red, which isn't wanted
292 gl_type
= GL_UNSIGNED_BYTE_3_3_2
;
296 *gl_format
= GL_BGRA
;
297 *gl_type
= GL_UNSIGNED_SHORT_1_5_5_5_REV
;
301 *gl_type
= GL_UNSIGNED_SHORT_5_6_5
;
305 *gl_type
= GL_UNSIGNED_BYTE
;
309 *gl_format
= GL_BGRA
;
310 *gl_type
= GL_UNSIGNED_BYTE
;
314 *gl_format
= GL_RGBA
;
315 *gl_type
= GL_UNSIGNED_BYTE
;
318 #ifdef TEXTUREFORMAT_ALWAYS
319 *gl_texfmt
= TEXTUREFORMAT_ALWAYS
;
324 static void *setNull(const GLubyte
*s
) {
331 const char *funcnames
[7];
335 #define DEF_FUNC_DESC(name) {&name, NULL, {"gl"#name, NULL}, gl ##name}
336 static const extfunc_desc_t extfuncs
[] = {
337 // these aren't extension functions but we query them anyway to allow
338 // different "backends" with one binary
339 DEF_FUNC_DESC(Begin
),
341 DEF_FUNC_DESC(Viewport
),
342 DEF_FUNC_DESC(MatrixMode
),
343 DEF_FUNC_DESC(LoadIdentity
),
344 DEF_FUNC_DESC(Scaled
),
345 DEF_FUNC_DESC(Ortho
),
346 DEF_FUNC_DESC(PushMatrix
),
347 DEF_FUNC_DESC(PopMatrix
),
348 DEF_FUNC_DESC(Clear
),
349 DEF_FUNC_DESC(GenLists
),
350 DEF_FUNC_DESC(DeleteLists
),
351 DEF_FUNC_DESC(NewList
),
352 DEF_FUNC_DESC(EndList
),
353 DEF_FUNC_DESC(CallList
),
354 DEF_FUNC_DESC(CallLists
),
355 DEF_FUNC_DESC(GenTextures
),
356 DEF_FUNC_DESC(DeleteTextures
),
357 DEF_FUNC_DESC(TexEnvi
),
358 DEF_FUNC_DESC(Color4ub
),
359 DEF_FUNC_DESC(Color3f
),
360 DEF_FUNC_DESC(ClearColor
),
361 DEF_FUNC_DESC(Enable
),
362 DEF_FUNC_DESC(Disable
),
363 DEF_FUNC_DESC(DrawBuffer
),
364 DEF_FUNC_DESC(DepthMask
),
365 DEF_FUNC_DESC(BlendFunc
),
366 DEF_FUNC_DESC(Flush
),
367 DEF_FUNC_DESC(Finish
),
368 DEF_FUNC_DESC(PixelStorei
),
369 DEF_FUNC_DESC(TexImage1D
),
370 DEF_FUNC_DESC(TexImage2D
),
371 DEF_FUNC_DESC(TexSubImage2D
),
372 DEF_FUNC_DESC(TexParameteri
),
373 DEF_FUNC_DESC(TexParameterf
),
374 DEF_FUNC_DESC(TexParameterfv
),
375 DEF_FUNC_DESC(TexCoord2f
),
376 DEF_FUNC_DESC(Vertex2f
),
377 DEF_FUNC_DESC(GetIntegerv
),
379 // here start the real extensions
380 {&GenBuffers
, NULL
, {"glGenBuffers", "glGenBuffersARB", NULL
}},
381 {&DeleteBuffers
, NULL
, {"glDeleteBuffers", "glDeleteBuffersARB", NULL
}},
382 {&BindBuffer
, NULL
, {"glBindBuffer", "glBindBufferARB", NULL
}},
383 {&MapBuffer
, NULL
, {"glMapBuffer", "glMapBufferARB", NULL
}},
384 {&UnmapBuffer
, NULL
, {"glUnmapBuffer", "glUnmapBufferARB", NULL
}},
385 {&BufferData
, NULL
, {"glBufferData", "glBufferDataARB", NULL
}},
386 {&CombinerParameterfv
, "NV_register_combiners", {"glCombinerParameterfv", "glCombinerParameterfvNV", NULL
}},
387 {&CombinerParameteri
, "NV_register_combiners", {"glCombinerParameteri", "glCombinerParameteriNV", NULL
}},
388 {&CombinerInput
, "NV_register_combiners", {"glCombinerInput", "glCombinerInputNV", NULL
}},
389 {&CombinerOutput
, "NV_register_combiners", {"glCombinerOutput", "glCombinerOutputNV", NULL
}},
390 {&BeginFragmentShader
, "ATI_fragment_shader", {"glBeginFragmentShaderATI", NULL
}},
391 {&EndFragmentShader
, "ATI_fragment_shader", {"glEndFragmentShaderATI", NULL
}},
392 {&SampleMap
, "ATI_fragment_shader", {"glSampleMapATI", NULL
}},
393 {&ColorFragmentOp2
, "ATI_fragment_shader", {"glColorFragmentOp2ATI", NULL
}},
394 {&ColorFragmentOp3
, "ATI_fragment_shader", {"glColorFragmentOp3ATI", NULL
}},
395 {&SetFragmentShaderConstant
, "ATI_fragment_shader", {"glSetFragmentShaderConstantATI", NULL
}},
396 {&ActiveTexture
, NULL
, {"glActiveTexture", "glActiveTextureARB", NULL
}},
397 {&BindTexture
, NULL
, {"glBindTexture", "glBindTextureARB", "glBindTextureEXT", NULL
}},
398 {&MultiTexCoord2f
, NULL
, {"glMultiTexCoord2f", "glMultiTexCoord2fARB", NULL
}},
399 {&GenPrograms
, "_program", {"glGenProgramsARB", NULL
}},
400 {&DeletePrograms
, "_program", {"glDeleteProgramsARB", NULL
}},
401 {&BindProgram
, "_program", {"glBindProgramARB", NULL
}},
402 {&ProgramString
, "_program", {"glProgramStringARB", NULL
}},
403 {&GetProgramiv
, "_program", {"glGetProgramivARB", NULL
}},
404 {&ProgramEnvParameter4f
, "_program", {"glProgramEnvParameter4fARB", NULL
}},
405 {&SwapInterval
, "_swap_control", {"glXSwapInterval", "glXSwapIntervalEXT", "glXSwapIntervalSGI", "wglSwapInterval", "wglSwapIntervalEXT", "wglSwapIntervalSGI", NULL
}},
406 {&TexImage3D
, NULL
, {"glTexImage3D", NULL
}},
407 {&AllocateMemoryMESA
, "GLX_MESA_allocate_memory", {"glXAllocateMemoryMESA", NULL
}},
408 {&FreeMemoryMESA
, "GLX_MESA_allocate_memory", {"glXFreeMemoryMESA", NULL
}},
413 * \brief find the function pointers of some useful OpenGL extensions
414 * \param getProcAddress function to resolve function names, may be NULL
415 * \param ext2 an extra extension string
417 static void getFunctions(void *(*getProcAddress
)(const GLubyte
*),
419 const extfunc_desc_t
*dsc
;
420 const char *extensions
;
424 getProcAddress
= setNull
;
426 // special case, we need glGetString before starting to find the other functions
427 GetString
= getProcAddress("glGetString");
429 GetString
= glGetString
;
431 extensions
= (const char *)GetString(GL_EXTENSIONS
);
432 if (!extensions
) extensions
= "";
433 if (!ext2
) ext2
= "";
434 allexts
= malloc(strlen(extensions
) + strlen(ext2
) + 2);
435 strcpy(allexts
, extensions
);
436 strcat(allexts
, " ");
437 strcat(allexts
, ext2
);
438 mp_msg(MSGT_VO
, MSGL_DBG2
, "OpenGL extensions string:\n%s\n", allexts
);
439 for (dsc
= extfuncs
; dsc
->funcptr
; dsc
++) {
442 if (!dsc
->extstr
|| strstr(allexts
, dsc
->extstr
)) {
443 for (i
= 0; !ptr
&& dsc
->funcnames
[i
]; i
++)
444 ptr
= getProcAddress((const GLubyte
*)dsc
->funcnames
[i
]);
448 *(void **)dsc
->funcptr
= ptr
;
450 if (strstr(allexts
, "_texture_float"))
451 hqtexfmt
= GL_RGB32F
;
452 else if (strstr(allexts
, "NV_float_buffer"))
453 hqtexfmt
= GL_FLOAT_RGB32_NV
;
460 * \brief create a texture and set some defaults
461 * \param target texture taget, usually GL_TEXTURE_2D
462 * \param fmt internal texture format
463 * \param format texture host data format
464 * \param type texture host data type
465 * \param filter filter used for scaling, e.g. GL_LINEAR
466 * \param w texture width
467 * \param h texture height
468 * \param val luminance value to fill texture with
471 void glCreateClearTex(GLenum target
, GLenum fmt
, GLenum format
, GLenum type
, GLint filter
,
472 int w
, int h
, unsigned char val
) {
473 GLfloat fval
= (GLfloat
)val
/ 255.0;
474 GLfloat border
[4] = {fval
, fval
, fval
, fval
};
475 int stride
= w
* glFmt2bpp(format
, type
);
478 init
= malloc(stride
* h
);
479 memset(init
, val
, stride
* h
);
480 glAdjustAlignment(stride
);
481 PixelStorei(GL_UNPACK_ROW_LENGTH
, w
);
482 TexImage2D(target
, 0, fmt
, w
, h
, 0, format
, type
, init
);
483 TexParameterf(target
, GL_TEXTURE_PRIORITY
, 1.0);
484 TexParameteri(target
, GL_TEXTURE_MIN_FILTER
, filter
);
485 TexParameteri(target
, GL_TEXTURE_MAG_FILTER
, filter
);
486 TexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
487 TexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
488 // Border texels should not be used with CLAMP_TO_EDGE
489 // We set a sane default anyway.
490 TexParameterfv(target
, GL_TEXTURE_BORDER_COLOR
, border
);
495 * \brief skips whitespace and comments
496 * \param f file to read from
498 static void ppm_skip(FILE *f
) {
506 } while (c
!= EOF
&& (isspace(c
) || comment
));
511 #define MAXDIM (16 * 1024)
514 * \brief creates a texture from a PPM file
515 * \param target texture taget, usually GL_TEXTURE_2D
516 * \param fmt internal texture format, 0 for default
517 * \param filter filter used for scaling, e.g. GL_LINEAR
518 * \param f file to read PPM from
519 * \param width [out] width of texture
520 * \param height [out] height of texture
521 * \param maxval [out] maxval value from PPM file
522 * \return 0 on error, 1 otherwise
525 int glCreatePPMTex(GLenum target
, GLenum fmt
, GLint filter
,
526 FILE *f
, int *width
, int *height
, int *maxval
) {
527 unsigned w
, h
, m
, val
, bpp
;
531 if (fgetc(f
) != 'P' || fgetc(f
) != '6')
534 if (fscanf(f
, "%u", &w
) != 1)
537 if (fscanf(f
, "%u", &h
) != 1)
540 if (fscanf(f
, "%u", &m
) != 1)
545 if (w
> MAXDIM
|| h
> MAXDIM
)
547 bpp
= (m
> 255) ? 6 : 3;
548 data
= malloc(w
* h
* bpp
);
549 if (fread(data
, w
* bpp
, h
, f
) != h
)
552 fmt
= (m
> 255) ? hqtexfmt
: 3;
553 if (fmt
== GL_FLOAT_RGB32_NV
&& target
!= GL_TEXTURE_RECTANGLE
)
556 type
= m
> 255 ? GL_UNSIGNED_SHORT
: GL_UNSIGNED_BYTE
;
557 glCreateClearTex(target
, fmt
, GL_RGB
, type
, filter
, w
, h
, 0);
558 glUploadTex(target
, GL_RGB
, type
,
559 data
, w
* bpp
, 0, 0, w
, h
, 0);
561 if (width
) *width
= w
;
562 if (height
) *height
= h
;
563 if (maxval
) *maxval
= m
;
568 * \brief return the number of bytes per pixel for the given format
569 * \param format OpenGL format
570 * \param type OpenGL type
571 * \return bytes per pixel
574 * Does not handle all possible variants, just those used by MPlayer
576 int glFmt2bpp(GLenum format
, GLenum type
) {
577 int component_size
= 0;
579 case GL_UNSIGNED_BYTE_3_3_2
:
580 case GL_UNSIGNED_BYTE_2_3_3_REV
:
582 case GL_UNSIGNED_SHORT_5_5_5_1
:
583 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
584 case GL_UNSIGNED_SHORT_5_6_5
:
585 case GL_UNSIGNED_SHORT_5_6_5_REV
:
587 case GL_UNSIGNED_BYTE
:
590 case GL_UNSIGNED_SHORT
:
597 return component_size
;
602 return 3 * component_size
;
605 return 4 * component_size
;
611 * \brief upload a texture, handling things like stride and slices
612 * \param target texture target, usually GL_TEXTURE_2D
613 * \param format OpenGL format of data
614 * \param type OpenGL type of data
615 * \param dataptr data to upload
616 * \param stride data stride
617 * \param x x offset in texture
618 * \param y y offset in texture
619 * \param w width of the texture part to upload
620 * \param h height of the texture part to upload
621 * \param slice height of an upload slice, 0 for all at once
624 void glUploadTex(GLenum target
, GLenum format
, GLenum type
,
625 const void *dataptr
, int stride
,
626 int x
, int y
, int w
, int h
, int slice
) {
627 const uint8_t *data
= dataptr
;
629 if (w
<= 0 || h
<= 0) return;
633 data
+= (h
- 1) * stride
;
636 // this is not always correct, but should work for MPlayer
637 glAdjustAlignment(stride
);
638 PixelStorei(GL_UNPACK_ROW_LENGTH
, stride
/ glFmt2bpp(format
, type
));
639 for (; y
+ slice
<= y_max
; y
+= slice
) {
640 TexSubImage2D(target
, 0, x
, y
, w
, slice
, format
, type
, data
);
641 data
+= stride
* slice
;
644 TexSubImage2D(target
, 0, x
, y
, w
, y_max
- y
, format
, type
, data
);
647 static void fillUVcoeff(GLfloat
*ucoef
, GLfloat
*vcoef
,
648 float uvcos
, float uvsin
) {
650 ucoef
[0] = 0 * uvcos
+ 1.403 * uvsin
;
651 vcoef
[0] = 0 * uvsin
+ 1.403 * uvcos
;
652 ucoef
[1] = -0.344 * uvcos
+ -0.714 * uvsin
;
653 vcoef
[1] = -0.344 * uvsin
+ -0.714 * uvcos
;
654 ucoef
[2] = 1.770 * uvcos
+ 0 * uvsin
;
655 vcoef
[2] = 1.770 * uvsin
+ 0 * uvcos
;
658 // Coefficients (probably) must be in [0, 1] range, whereas they originally
659 // are in [-2, 2] range, so here comes the trick:
660 // First put them in the [-0.5, 0.5] range, then add 0.5.
661 // This can be undone with the HALF_BIAS and SCALE_BY_FOUR arguments
662 // for CombinerInput and CombinerOutput (or the respective ATI variants)
663 for (i
= 0; i
< 4; i
++) {
664 ucoef
[i
] = ucoef
[i
] * 0.25 + 0.5;
665 vcoef
[i
] = vcoef
[i
] * 0.25 + 0.5;
670 * \brief Setup register combiners for YUV to RGB conversion.
671 * \param uvcos used for saturation and hue adjustment
672 * \param uvsin used for saturation and hue adjustment
674 static void glSetupYUVCombiners(float uvcos
, float uvsin
) {
678 if (!CombinerInput
|| !CombinerOutput
||
679 !CombinerParameterfv
|| !CombinerParameteri
) {
680 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Combiner functions missing!\n");
683 GetIntegerv(GL_MAX_GENERAL_COMBINERS_NV
, &i
);
685 mp_msg(MSGT_VO
, MSGL_ERR
,
686 "[gl] 2 general combiners needed for YUV combiner support (found %i)\n", i
);
687 GetIntegerv(GL_MAX_TEXTURE_UNITS
, &i
);
689 mp_msg(MSGT_VO
, MSGL_ERR
,
690 "[gl] 3 texture units needed for YUV combiner support (found %i)\n", i
);
691 fillUVcoeff(ucoef
, vcoef
, uvcos
, uvsin
);
692 CombinerParameterfv(GL_CONSTANT_COLOR0_NV
, ucoef
);
693 CombinerParameterfv(GL_CONSTANT_COLOR1_NV
, vcoef
);
695 // UV first, like this green component cannot overflow
696 CombinerInput(GL_COMBINER0_NV
, GL_RGB
, GL_VARIABLE_A_NV
,
697 GL_TEXTURE1
, GL_HALF_BIAS_NORMAL_NV
, GL_RGB
);
698 CombinerInput(GL_COMBINER0_NV
, GL_RGB
, GL_VARIABLE_B_NV
,
699 GL_CONSTANT_COLOR0_NV
, GL_HALF_BIAS_NORMAL_NV
, GL_RGB
);
700 CombinerInput(GL_COMBINER0_NV
, GL_RGB
, GL_VARIABLE_C_NV
,
701 GL_TEXTURE2
, GL_HALF_BIAS_NORMAL_NV
, GL_RGB
);
702 CombinerInput(GL_COMBINER0_NV
, GL_RGB
, GL_VARIABLE_D_NV
,
703 GL_CONSTANT_COLOR1_NV
, GL_HALF_BIAS_NORMAL_NV
, GL_RGB
);
704 CombinerOutput(GL_COMBINER0_NV
, GL_RGB
, GL_DISCARD_NV
, GL_DISCARD_NV
,
705 GL_SPARE0_NV
, GL_SCALE_BY_FOUR_NV
, GL_NONE
, GL_FALSE
,
709 CombinerInput(GL_COMBINER1_NV
, GL_RGB
, GL_VARIABLE_A_NV
, GL_SPARE0_NV
,
710 GL_SIGNED_IDENTITY_NV
, GL_RGB
);
711 CombinerInput(GL_COMBINER1_NV
, GL_RGB
, GL_VARIABLE_B_NV
, GL_ZERO
,
712 GL_UNSIGNED_INVERT_NV
, GL_RGB
);
713 CombinerInput(GL_COMBINER1_NV
, GL_RGB
, GL_VARIABLE_C_NV
,
714 GL_TEXTURE0
, GL_SIGNED_IDENTITY_NV
, GL_RGB
);
715 CombinerInput(GL_COMBINER1_NV
, GL_RGB
, GL_VARIABLE_D_NV
, GL_ZERO
,
716 GL_UNSIGNED_INVERT_NV
, GL_RGB
);
717 CombinerOutput(GL_COMBINER1_NV
, GL_RGB
, GL_DISCARD_NV
, GL_DISCARD_NV
,
718 GL_SPARE0_NV
, GL_NONE
, GL_NONE
, GL_FALSE
,
721 // leave final combiner stage in default mode
722 CombinerParameteri(GL_NUM_GENERAL_COMBINERS_NV
, 2);
726 * \brief Setup ATI version of register combiners for YUV to RGB conversion.
727 * \param uvcos used for saturation and hue adjustment
728 * \param uvsin used for saturation and hue adjustment
730 * ATI called this fragment shader, but the name is confusing in the
731 * light of a very different OpenGL 2.0 extension with the same name
733 static void glSetupYUVCombinersATI(float uvcos
, float uvsin
) {
737 if (!BeginFragmentShader
|| !EndFragmentShader
||
738 !SetFragmentShaderConstant
|| !SampleMap
||
739 !ColorFragmentOp2
|| !ColorFragmentOp3
) {
740 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Combiner (ATI) functions missing!\n");
743 GetIntegerv(GL_NUM_FRAGMENT_REGISTERS_ATI
, &i
);
745 mp_msg(MSGT_VO
, MSGL_ERR
,
746 "[gl] 3 registers needed for YUV combiner (ATI) support (found %i)\n", i
);
747 GetIntegerv (GL_MAX_TEXTURE_UNITS
, &i
);
749 mp_msg(MSGT_VO
, MSGL_ERR
,
750 "[gl] 3 texture units needed for YUV combiner (ATI) support (found %i)\n", i
);
751 fillUVcoeff(ucoef
, vcoef
, uvcos
, uvsin
);
752 BeginFragmentShader();
753 SetFragmentShaderConstant(GL_CON_0_ATI
, ucoef
);
754 SetFragmentShaderConstant(GL_CON_1_ATI
, vcoef
);
755 SampleMap(GL_REG_0_ATI
, GL_TEXTURE0
, GL_SWIZZLE_STR_ATI
);
756 SampleMap(GL_REG_1_ATI
, GL_TEXTURE1
, GL_SWIZZLE_STR_ATI
);
757 SampleMap(GL_REG_2_ATI
, GL_TEXTURE2
, GL_SWIZZLE_STR_ATI
);
758 // UV first, like this green component cannot overflow
759 ColorFragmentOp2(GL_MUL_ATI
, GL_REG_1_ATI
, GL_NONE
, GL_NONE
,
760 GL_REG_1_ATI
, GL_NONE
, GL_BIAS_BIT_ATI
,
761 GL_CON_0_ATI
, GL_NONE
, GL_BIAS_BIT_ATI
);
762 ColorFragmentOp3(GL_MAD_ATI
, GL_REG_2_ATI
, GL_NONE
, GL_4X_BIT_ATI
,
763 GL_REG_2_ATI
, GL_NONE
, GL_BIAS_BIT_ATI
,
764 GL_CON_1_ATI
, GL_NONE
, GL_BIAS_BIT_ATI
,
765 GL_REG_1_ATI
, GL_NONE
, GL_NONE
);
766 ColorFragmentOp2(GL_ADD_ATI
, GL_REG_0_ATI
, GL_NONE
, GL_NONE
,
767 GL_REG_0_ATI
, GL_NONE
, GL_NONE
,
768 GL_REG_2_ATI
, GL_NONE
, GL_NONE
);
773 * \brief helper function for gen_spline_lookup_tex
774 * \param x subpixel-position ((0,1) range) to calculate weights for
775 * \param dst where to store transformed weights, must provide space for 4 GLfloats
777 * calculates the weights and stores them after appropriate transformation
778 * for the scaler fragment program.
780 static void store_weights(float x
, GLfloat
*dst
) {
781 float w0
= (((-1 * x
+ 3) * x
- 3) * x
+ 1) / 6;
782 float w1
= ((( 3 * x
- 6) * x
+ 0) * x
+ 4) / 6;
783 float w2
= (((-3 * x
+ 3) * x
+ 3) * x
+ 1) / 6;
784 float w3
= ((( 1 * x
+ 0) * x
+ 0) * x
+ 0) / 6;
785 *dst
++ = 1 + x
- w1
/ (w0
+ w1
);
786 *dst
++ = 1 - x
+ w3
/ (w2
+ w3
);
791 //! to avoid artefacts this should be rather large
792 #define LOOKUP_BSPLINE_RES (2 * 1024)
794 * \brief creates the 1D lookup texture needed for fast higher-order filtering
795 * \param unit texture unit to attach texture to
797 static void gen_spline_lookup_tex(GLenum unit
) {
798 GLfloat
*tex
= calloc(4 * LOOKUP_BSPLINE_RES
, sizeof(*tex
));
801 for (i
= 0; i
< LOOKUP_BSPLINE_RES
; i
++) {
802 float x
= (float)(i
+ 0.5) / LOOKUP_BSPLINE_RES
;
803 store_weights(x
, tp
);
806 store_weights(0, tex
);
807 store_weights(1, &tex
[4 * (LOOKUP_BSPLINE_RES
- 1)]);
809 TexImage1D(GL_TEXTURE_1D
, 0, GL_RGBA16
, LOOKUP_BSPLINE_RES
, 0, GL_RGBA
, GL_FLOAT
, tex
);
810 TexParameterf(GL_TEXTURE_1D
, GL_TEXTURE_PRIORITY
, 1.0);
811 TexParameteri(GL_TEXTURE_1D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
812 TexParameteri(GL_TEXTURE_1D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
813 TexParameteri(GL_TEXTURE_1D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
814 ActiveTexture(GL_TEXTURE0
);
818 static const char *bilin_filt_template
=
819 "TEX yuv.%c, fragment.texcoord[%c], texture[%c], %s;";
821 #define BICUB_FILT_MAIN(textype) \
822 /* first y-interpolation */ \
823 "ADD coord, fragment.texcoord[%c].xyxy, cdelta.xyxw;" \
824 "ADD coord2, fragment.texcoord[%c].xyxy, cdelta.zyzw;" \
825 "TEX a.r, coord.xyxy, texture[%c], "textype";" \
826 "TEX a.g, coord.zwzw, texture[%c], "textype";" \
827 /* second y-interpolation */ \
828 "TEX b.r, coord2.xyxy, texture[%c], "textype";" \
829 "TEX b.g, coord2.zwzw, texture[%c], "textype";" \
830 "LRP a.b, parmy.b, a.rrrr, a.gggg;" \
831 "LRP a.a, parmy.b, b.rrrr, b.gggg;" \
832 /* x-interpolation */ \
833 "LRP yuv.%c, parmx.b, a.bbbb, a.aaaa;"
835 static const char *bicub_filt_template_2D
=
836 "MAD coord.xy, fragment.texcoord[%c], {%f, %f}, {0.5, 0.5};"
837 "TEX parmx, coord.x, texture[%c], 1D;"
838 "MUL cdelta.xz, parmx.rrgg, {-%f, 0, %f, 0};"
839 "TEX parmy, coord.y, texture[%c], 1D;"
840 "MUL cdelta.yw, parmy.rrgg, {0, -%f, 0, %f};"
841 BICUB_FILT_MAIN("2D");
843 static const char *bicub_filt_template_RECT
=
844 "ADD coord, fragment.texcoord[%c], {0.5, 0.5};"
845 "TEX parmx, coord.x, texture[%c], 1D;"
846 "MUL cdelta.xz, parmx.rrgg, {-1, 0, 1, 0};"
847 "TEX parmy, coord.y, texture[%c], 1D;"
848 "MUL cdelta.yw, parmy.rrgg, {0, -1, 0, 1};"
849 BICUB_FILT_MAIN("RECT");
851 #define CALCWEIGHTS(t, s) \
852 "MAD "t", {-0.5, 0.1666, 0.3333, -0.3333}, "s", {1, 0, -0.5, 0.5};" \
853 "MAD "t", "t", "s", {0, 0, -0.5, 0.5};" \
854 "MAD "t", "t", "s", {-0.6666, 0, 0.8333, 0.1666};" \
857 "MAD "t".xy, "t".xyxy, a.xyxy, {1, 1, 0, 0};" \
858 "ADD "t".x, "t".xxxx, "s";" \
859 "SUB "t".y, "t".yyyy, "s";"
861 static const char *bicub_notex_filt_template_2D
=
862 "MAD coord.xy, fragment.texcoord[%c], {%f, %f}, {0.5, 0.5};"
863 "FRC coord.xy, coord.xyxy;"
864 CALCWEIGHTS("parmx", "coord.xxxx")
865 "MUL cdelta.xz, parmx.rrgg, {-%f, 0, %f, 0};"
866 CALCWEIGHTS("parmy", "coord.yyyy")
867 "MUL cdelta.yw, parmy.rrgg, {0, -%f, 0, %f};"
868 BICUB_FILT_MAIN("2D");
870 static const char *bicub_notex_filt_template_RECT
=
871 "ADD coord, fragment.texcoord[%c], {0.5, 0.5};"
872 "FRC coord.xy, coord.xyxy;"
873 CALCWEIGHTS("parmx", "coord.xxxx")
874 "MUL cdelta.xz, parmx.rrgg, {-1, 0, 1, 0};"
875 CALCWEIGHTS("parmy", "coord.yyyy")
876 "MUL cdelta.yw, parmy.rrgg, {0, -1, 0, 1};"
877 BICUB_FILT_MAIN("RECT");
879 #define BICUB_X_FILT_MAIN(textype) \
880 "ADD coord.xy, fragment.texcoord[%c].xyxy, cdelta.xyxy;" \
881 "ADD coord2.xy, fragment.texcoord[%c].xyxy, cdelta.zyzy;" \
882 "TEX a.r, coord, texture[%c], "textype";" \
883 "TEX b.r, coord2, texture[%c], "textype";" \
884 /* x-interpolation */ \
885 "LRP yuv.%c, parmx.b, a.rrrr, b.rrrr;"
887 static const char *bicub_x_filt_template_2D
=
888 "MAD coord.x, fragment.texcoord[%c], {%f}, {0.5};"
889 "TEX parmx, coord, texture[%c], 1D;"
890 "MUL cdelta.xyz, parmx.rrgg, {-%f, 0, %f};"
891 BICUB_X_FILT_MAIN("2D");
893 static const char *bicub_x_filt_template_RECT
=
894 "ADD coord.x, fragment.texcoord[%c], {0.5};"
895 "TEX parmx, coord, texture[%c], 1D;"
896 "MUL cdelta.xyz, parmx.rrgg, {-1, 0, 1};"
897 BICUB_X_FILT_MAIN("RECT");
899 static const char *unsharp_filt_template
=
900 "PARAM dcoord%c = {%f, %f, %f, %f};"
901 "ADD coord, fragment.texcoord[%c].xyxy, dcoord%c;"
902 "SUB coord2, fragment.texcoord[%c].xyxy, dcoord%c;"
903 "TEX a.r, fragment.texcoord[%c], texture[%c], %s;"
904 "TEX b.r, coord.xyxy, texture[%c], %s;"
905 "TEX b.g, coord.zwzw, texture[%c], %s;"
907 "TEX b.b, coord2.xyxy, texture[%c], %s;"
908 "TEX b.g, coord2.zwzw, texture[%c], %s;"
909 "DP3 b, b, {0.25, 0.25, 0.25};"
911 "MAD yuv.%c, b.r, {%f}, a.r;";
913 static const char *unsharp_filt_template2
=
914 "PARAM dcoord%c = {%f, %f, %f, %f};"
915 "PARAM dcoord2%c = {%f, 0, 0, %f};"
916 "ADD coord, fragment.texcoord[%c].xyxy, dcoord%c;"
917 "SUB coord2, fragment.texcoord[%c].xyxy, dcoord%c;"
918 "TEX a.r, fragment.texcoord[%c], texture[%c], %s;"
919 "TEX b.r, coord.xyxy, texture[%c], %s;"
920 "TEX b.g, coord.zwzw, texture[%c], %s;"
922 "TEX b.b, coord2.xyxy, texture[%c], %s;"
923 "TEX b.g, coord2.zwzw, texture[%c], %s;"
926 "ADD coord, fragment.texcoord[%c].xyxy, dcoord2%c;"
927 "SUB coord2, fragment.texcoord[%c].xyxy, dcoord2%c;"
928 "TEX b.r, coord.xyxy, texture[%c], %s;"
929 "TEX b.g, coord.zwzw, texture[%c], %s;"
931 "TEX b.b, coord2.xyxy, texture[%c], %s;"
932 "TEX b.g, coord2.zwzw, texture[%c], %s;"
933 "DP4 b.r, b, {-0.1171875, -0.1171875, -0.1171875, -0.09765625};"
934 "MAD b.r, a.r, {0.859375}, b.r;"
935 "MAD yuv.%c, b.r, {%f}, a.r;";
937 static const char *yuv_prog_template
=
938 "PARAM ycoef = {%.4f, %.4f, %.4f};"
939 "PARAM ucoef = {%.4f, %.4f, %.4f};"
940 "PARAM vcoef = {%.4f, %.4f, %.4f};"
941 "PARAM offsets = {%.4f, %.4f, %.4f};"
943 "MAD res.rgb, yuv.rrrr, ycoef, offsets;"
944 "MAD res.rgb, yuv.gggg, ucoef, res;"
945 "MAD result.color.rgb, yuv.bbbb, vcoef, res;"
948 static const char *yuv_pow_prog_template
=
949 "PARAM ycoef = {%.4f, %.4f, %.4f};"
950 "PARAM ucoef = {%.4f, %.4f, %.4f};"
951 "PARAM vcoef = {%.4f, %.4f, %.4f};"
952 "PARAM offsets = {%.4f, %.4f, %.4f};"
953 "PARAM gamma = {%.4f, %.4f, %.4f};"
955 "MAD res.rgb, yuv.rrrr, ycoef, offsets;"
956 "MAD res.rgb, yuv.gggg, ucoef, res;"
957 "MAD_SAT res.rgb, yuv.bbbb, vcoef, res;"
958 "POW result.color.r, res.r, gamma.r;"
959 "POW result.color.g, res.g, gamma.g;"
960 "POW result.color.b, res.b, gamma.b;"
963 static const char *yuv_lookup_prog_template
=
964 "PARAM ycoef = {%.4f, %.4f, %.4f, 0};"
965 "PARAM ucoef = {%.4f, %.4f, %.4f, 0};"
966 "PARAM vcoef = {%.4f, %.4f, %.4f, 0};"
967 "PARAM offsets = {%.4f, %.4f, %.4f, 0.125};"
969 "MAD res, yuv.rrrr, ycoef, offsets;"
970 "MAD res.rgb, yuv.gggg, ucoef, res;"
971 "MAD res.rgb, yuv.bbbb, vcoef, res;"
972 "TEX result.color.r, res.raaa, texture[%c], 2D;"
973 "ADD res.a, res.a, 0.25;"
974 "TEX result.color.g, res.gaaa, texture[%c], 2D;"
975 "ADD res.a, res.a, 0.25;"
976 "TEX result.color.b, res.baaa, texture[%c], 2D;"
979 static const char *yuv_lookup3d_prog_template
=
980 "TEX result.color, yuv, texture[%c], 3D;"
984 * \brief creates and initializes helper textures needed for scaling texture read
985 * \param scaler scaler type to create texture for
986 * \param texu contains next free texture unit number
987 * \param texs texture unit ids for the scaler are stored in this array
989 static void create_scaler_textures(int scaler
, int *texu
, char *texs
) {
991 case YUV_SCALER_BILIN
:
992 case YUV_SCALER_BICUB_NOTEX
:
993 case YUV_SCALER_UNSHARP
:
994 case YUV_SCALER_UNSHARP2
:
996 case YUV_SCALER_BICUB
:
997 case YUV_SCALER_BICUB_X
:
999 gen_spline_lookup_tex(GL_TEXTURE0
+ texs
[0]);
1003 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] unknown scaler type %i\n", scaler
);
1007 static void gen_gamma_map(unsigned char *map
, int size
, float gamma
);
1017 static void get_yuv2rgb_coeffs(gl_conversion_params_t
*params
, float yuv2rgb
[3][4]) {
1018 float uvcos
= params
->saturation
* cos(params
->hue
);
1019 float uvsin
= params
->saturation
* sin(params
->hue
);
1021 float uv_coeffs
[3][2] = {
1026 for (i
= 0; i
< 3; i
++) {
1027 yuv2rgb
[i
][COL_C
] = params
->brightness
;
1028 yuv2rgb
[i
][COL_Y
] = 1.164 * params
->contrast
;
1029 yuv2rgb
[i
][COL_C
] += (-16 / 255.0) * yuv2rgb
[i
][COL_Y
];
1030 yuv2rgb
[i
][COL_U
] = uv_coeffs
[i
][0] * uvcos
+ uv_coeffs
[i
][1] * uvsin
;
1031 yuv2rgb
[i
][COL_C
] += (-128 / 255.0) * yuv2rgb
[i
][COL_U
];
1032 yuv2rgb
[i
][COL_V
] = uv_coeffs
[i
][0] * uvsin
+ uv_coeffs
[i
][1] * uvcos
;
1033 yuv2rgb
[i
][COL_C
] += (-128 / 255.0) * yuv2rgb
[i
][COL_V
];
1034 // this "centers" contrast control so that e.g. a contrast of 0
1035 // leads to a grey image, not a black one
1036 yuv2rgb
[i
][COL_C
] += 0.5 - params
->contrast
/ 2.0;
1040 //! size of gamma map use to avoid slow exp function in gen_yuv2rgb_map
1041 #define GMAP_SIZE (1024)
1043 * \brief generate a 3D YUV -> RGB map
1044 * \param params struct containing parameters like brightness, gamma, ...
1045 * \param map where to store map. Must provide space for (size + 2)^3 elements
1046 * \param size size of the map, excluding border
1048 static void gen_yuv2rgb_map(gl_conversion_params_t
*params
, unsigned char *map
, int size
) {
1050 float step
= 1.0 / size
;
1052 float yuv2rgb
[3][4];
1053 unsigned char gmaps
[3][GMAP_SIZE
];
1054 gen_gamma_map(gmaps
[0], GMAP_SIZE
, params
->rgamma
);
1055 gen_gamma_map(gmaps
[1], GMAP_SIZE
, params
->ggamma
);
1056 gen_gamma_map(gmaps
[2], GMAP_SIZE
, params
->bgamma
);
1057 get_yuv2rgb_coeffs(params
, yuv2rgb
);
1058 for (i
= 0; i
< 3; i
++)
1059 for (j
= 0; j
< 4; j
++)
1060 yuv2rgb
[i
][j
] *= GMAP_SIZE
- 1;
1062 for (i
= -1; i
<= size
; i
++) {
1064 for (j
= -1; j
<= size
; j
++) {
1066 for (k
= -1; k
<= size
; k
++) {
1067 for (l
= 0; l
< 3; l
++) {
1068 float rgb
= yuv2rgb
[l
][COL_Y
] * y
+ yuv2rgb
[l
][COL_U
] * u
+ yuv2rgb
[l
][COL_V
] * v
+ yuv2rgb
[l
][COL_C
];
1069 *map
++ = gmaps
[l
][av_clip(rgb
, 0, GMAP_SIZE
- 1)];
1071 y
+= (k
== -1 || k
== size
- 1) ? step
/ 2 : step
;
1073 u
+= (j
== -1 || j
== size
- 1) ? step
/ 2 : step
;
1075 v
+= (i
== -1 || i
== size
- 1) ? step
/ 2 : step
;
1079 //! resolution of texture for gamma lookup table
1080 #define LOOKUP_RES 512
1081 //! resolution for 3D yuv->rgb conversion lookup table
1082 #define LOOKUP_3DRES 32
1084 * \brief creates and initializes helper textures needed for yuv conversion
1085 * \param params struct containing parameters like brightness, gamma, ...
1086 * \param texu contains next free texture unit number
1087 * \param texs texture unit ids for the conversion are stored in this array
1089 static void create_conv_textures(gl_conversion_params_t
*params
, int *texu
, char *texs
) {
1090 unsigned char *lookup_data
= NULL
;
1091 int conv
= YUV_CONVERSION(params
->type
);
1093 case YUV_CONVERSION_FRAGMENT
:
1094 case YUV_CONVERSION_FRAGMENT_POW
:
1096 case YUV_CONVERSION_FRAGMENT_LOOKUP
:
1097 texs
[0] = (*texu
)++;
1098 ActiveTexture(GL_TEXTURE0
+ texs
[0]);
1099 lookup_data
= malloc(4 * LOOKUP_RES
);
1100 gen_gamma_map(lookup_data
, LOOKUP_RES
, params
->rgamma
);
1101 gen_gamma_map(&lookup_data
[LOOKUP_RES
], LOOKUP_RES
, params
->ggamma
);
1102 gen_gamma_map(&lookup_data
[2 * LOOKUP_RES
], LOOKUP_RES
, params
->bgamma
);
1103 glCreateClearTex(GL_TEXTURE_2D
, GL_LUMINANCE8
, GL_LUMINANCE
, GL_UNSIGNED_BYTE
, GL_LINEAR
,
1105 glUploadTex(GL_TEXTURE_2D
, GL_LUMINANCE
, GL_UNSIGNED_BYTE
, lookup_data
,
1106 LOOKUP_RES
, 0, 0, LOOKUP_RES
, 4, 0);
1107 ActiveTexture(GL_TEXTURE0
);
1110 case YUV_CONVERSION_FRAGMENT_LOOKUP3D
:
1112 int sz
= LOOKUP_3DRES
+ 2; // texture size including borders
1114 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] Missing 3D texture function!\n");
1117 texs
[0] = (*texu
)++;
1118 ActiveTexture(GL_TEXTURE0
+ texs
[0]);
1119 lookup_data
= malloc(3 * sz
* sz
* sz
);
1120 gen_yuv2rgb_map(params
, lookup_data
, LOOKUP_3DRES
);
1121 glAdjustAlignment(sz
);
1122 PixelStorei(GL_UNPACK_ROW_LENGTH
, 0);
1123 TexImage3D(GL_TEXTURE_3D
, 0, 3, sz
, sz
, sz
, 1,
1124 GL_RGB
, GL_UNSIGNED_BYTE
, lookup_data
);
1125 TexParameterf(GL_TEXTURE_3D
, GL_TEXTURE_PRIORITY
, 1.0);
1126 TexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
1127 TexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
1128 TexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_WRAP_S
, GL_CLAMP
);
1129 TexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_WRAP_T
, GL_CLAMP
);
1130 TexParameteri(GL_TEXTURE_3D
, GL_TEXTURE_WRAP_R
, GL_CLAMP
);
1131 ActiveTexture(GL_TEXTURE0
);
1136 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] unknown conversion type %i\n", conv
);
1143 * \brief adds a scaling texture read at the current fragment program position
1144 * \param scaler type of scaler to insert
1145 * \param prog_pos current position in fragment program
1146 * \param remain how many bytes remain in the buffer given by prog_pos
1147 * \param texs array containing the texture unit identifiers for this scaler
1148 * \param in_tex texture unit the scaler should read from
1149 * \param out_comp component of the yuv variable the scaler stores the result in
1150 * \param rect if rectangular (pixel) adressing should be used for in_tex
1151 * \param texw width of the in_tex texture
1152 * \param texh height of the in_tex texture
1153 * \param strength strength of filter effect if the scaler does some kind of filtering
1155 static void add_scaler(int scaler
, char **prog_pos
, int *remain
, char *texs
,
1156 char in_tex
, char out_comp
, int rect
, int texw
, int texh
,
1158 const char *ttype
= rect
? "RECT" : "2D";
1159 const float ptw
= rect
? 1.0 : 1.0 / texw
;
1160 const float pth
= rect
? 1.0 : 1.0 / texh
;
1162 case YUV_SCALER_BILIN
:
1163 snprintf(*prog_pos
, *remain
, bilin_filt_template
, out_comp
, in_tex
,
1166 case YUV_SCALER_BICUB
:
1168 snprintf(*prog_pos
, *remain
, bicub_filt_template_RECT
,
1169 in_tex
, texs
[0], texs
[0],
1170 in_tex
, in_tex
, in_tex
, in_tex
, in_tex
, in_tex
, out_comp
);
1172 snprintf(*prog_pos
, *remain
, bicub_filt_template_2D
,
1173 in_tex
, (float)texw
, (float)texh
,
1174 texs
[0], ptw
, ptw
, texs
[0], pth
, pth
,
1175 in_tex
, in_tex
, in_tex
, in_tex
, in_tex
, in_tex
, out_comp
);
1177 case YUV_SCALER_BICUB_X
:
1179 snprintf(*prog_pos
, *remain
, bicub_x_filt_template_RECT
,
1181 in_tex
, in_tex
, in_tex
, in_tex
, out_comp
);
1183 snprintf(*prog_pos
, *remain
, bicub_x_filt_template_2D
,
1184 in_tex
, (float)texw
,
1186 in_tex
, in_tex
, in_tex
, in_tex
, out_comp
);
1188 case YUV_SCALER_BICUB_NOTEX
:
1190 snprintf(*prog_pos
, *remain
, bicub_notex_filt_template_RECT
,
1192 in_tex
, in_tex
, in_tex
, in_tex
, in_tex
, in_tex
, out_comp
);
1194 snprintf(*prog_pos
, *remain
, bicub_notex_filt_template_2D
,
1195 in_tex
, (float)texw
, (float)texh
, ptw
, ptw
, pth
, pth
,
1196 in_tex
, in_tex
, in_tex
, in_tex
, in_tex
, in_tex
, out_comp
);
1198 case YUV_SCALER_UNSHARP
:
1199 snprintf(*prog_pos
, *remain
, unsharp_filt_template
,
1200 out_comp
, 0.5 * ptw
, 0.5 * pth
, 0.5 * ptw
, -0.5 * pth
,
1201 in_tex
, out_comp
, in_tex
, out_comp
, in_tex
,
1202 in_tex
, ttype
, in_tex
, ttype
, in_tex
, ttype
, in_tex
, ttype
,
1203 in_tex
, ttype
, out_comp
, strength
);
1205 case YUV_SCALER_UNSHARP2
:
1206 snprintf(*prog_pos
, *remain
, unsharp_filt_template2
,
1207 out_comp
, 1.2 * ptw
, 1.2 * pth
, 1.2 * ptw
, -1.2 * pth
,
1208 out_comp
, 1.5 * ptw
, 1.5 * pth
,
1209 in_tex
, out_comp
, in_tex
, out_comp
, in_tex
,
1210 in_tex
, ttype
, in_tex
, ttype
, in_tex
, ttype
, in_tex
, ttype
,
1211 in_tex
, ttype
, in_tex
, out_comp
, in_tex
, out_comp
,
1212 in_tex
, ttype
, in_tex
, ttype
, in_tex
, ttype
,
1213 in_tex
, ttype
, out_comp
, strength
);
1216 *remain
-= strlen(*prog_pos
);
1217 *prog_pos
+= strlen(*prog_pos
);
1220 static const struct {
1225 {"instructions", 0x88A0, 0x88A1},
1226 {"native instructions", 0x88A2, 0x88A3},
1227 {"temporaries", 0x88A4, 0x88A5},
1228 {"native temporaries", 0x88A6, 0x88A7},
1229 {"parameters", 0x88A8, 0x88A9},
1230 {"native parameters", 0x88AA, 0x88AB},
1231 {"attribs", 0x88AC, 0x88AD},
1232 {"native attribs", 0x88AE, 0x88AF},
1233 {"ALU instructions", 0x8805, 0x880B},
1234 {"TEX instructions", 0x8806, 0x880C},
1235 {"TEX indirections", 0x8807, 0x880D},
1236 {"native ALU instructions", 0x8808, 0x880E},
1237 {"native TEX instructions", 0x8809, 0x880F},
1238 {"native TEX indirections", 0x880A, 0x8810},
1243 * \brief load the specified GPU Program
1244 * \param target program target to load into, only GL_FRAGMENT_PROGRAM is tested
1245 * \param prog program string
1246 * \return 1 on success, 0 otherwise
1248 int loadGPUProgram(GLenum target
, char *prog
) {
1250 GLint cur
= 0, max
= 0, err
= 0;
1251 if (!ProgramString
) {
1252 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] Missing GPU program function\n");
1255 ProgramString(target
, GL_PROGRAM_FORMAT_ASCII
, strlen(prog
), prog
);
1256 GetIntegerv(GL_PROGRAM_ERROR_POSITION
, &err
);
1258 mp_msg(MSGT_VO
, MSGL_ERR
,
1259 "[gl] Error compiling fragment program, make sure your card supports\n"
1260 "[gl] GL_ARB_fragment_program (use glxinfo to check).\n"
1261 "[gl] Error message:\n %s at %.10s\n",
1262 GetString(GL_PROGRAM_ERROR_STRING
), &prog
[err
]);
1265 if (!GetProgramiv
|| !mp_msg_test(MSGT_VO
, MSGL_DBG2
))
1267 mp_msg(MSGT_VO
, MSGL_V
, "[gl] Program statistics:\n");
1268 for (i
= 0; progstats
[i
].name
; i
++) {
1269 GetProgramiv(target
, progstats
[i
].cur
, &cur
);
1270 GetProgramiv(target
, progstats
[i
].max
, &max
);
1271 mp_msg(MSGT_VO
, MSGL_V
, "[gl] %s: %i/%i\n", progstats
[i
].name
, cur
, max
);
1276 #define MAX_PROGSZ (1024*1024)
1279 * \brief setup a fragment program that will do YUV->RGB conversion
1280 * \param parms struct containing parameters like conversion and scaler type,
1283 static void glSetupYUVFragprog(gl_conversion_params_t
*params
) {
1284 int type
= params
->type
;
1285 int texw
= params
->texw
;
1286 int texh
= params
->texh
;
1287 int rect
= params
->target
== GL_TEXTURE_RECTANGLE
;
1288 static const char prog_hdr
[] =
1290 "OPTION ARB_precision_hint_fastest;"
1291 // all scaler variables must go here so they aren't defined
1292 // multiple times when the same scaler is used more than once
1293 "TEMP coord, coord2, cdelta, parmx, parmy, a, b, yuv;";
1295 char *yuv_prog
, *prog_pos
;
1297 char lum_scale_texs
[1];
1298 char chrom_scale_texs
[1];
1301 // this is the conversion matrix, with y, u, v factors
1302 // for red, green, blue and the constant offsets
1303 float yuv2rgb
[3][4];
1304 create_conv_textures(params
, &cur_texu
, conv_texs
);
1305 create_scaler_textures(YUV_LUM_SCALER(type
), &cur_texu
, lum_scale_texs
);
1306 if (YUV_CHROM_SCALER(type
) == YUV_LUM_SCALER(type
))
1307 memcpy(chrom_scale_texs
, lum_scale_texs
, sizeof(chrom_scale_texs
));
1309 create_scaler_textures(YUV_CHROM_SCALER(type
), &cur_texu
, chrom_scale_texs
);
1310 GetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS
, &i
);
1312 mp_msg(MSGT_VO
, MSGL_ERR
,
1313 "[gl] %i texture units needed for this type of YUV fragment support (found %i)\n",
1315 if (!ProgramString
) {
1316 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] ProgramString function missing!\n");
1319 yuv_prog
= malloc(MAX_PROGSZ
);
1320 strcpy(yuv_prog
, prog_hdr
);
1321 prog_pos
= yuv_prog
+ sizeof(prog_hdr
) - 1;
1322 prog_remain
= MAX_PROGSZ
- sizeof(prog_hdr
);
1323 add_scaler(YUV_LUM_SCALER(type
), &prog_pos
, &prog_remain
, lum_scale_texs
,
1324 '0', 'r', rect
, texw
, texh
, params
->filter_strength
);
1325 add_scaler(YUV_CHROM_SCALER(type
), &prog_pos
, &prog_remain
, chrom_scale_texs
,
1326 '1', 'g', rect
, texw
/ 2, texh
/ 2, params
->filter_strength
);
1327 add_scaler(YUV_CHROM_SCALER(type
), &prog_pos
, &prog_remain
, chrom_scale_texs
,
1328 '2', 'b', rect
, texw
/ 2, texh
/ 2, params
->filter_strength
);
1329 get_yuv2rgb_coeffs(params
, yuv2rgb
);
1330 switch (YUV_CONVERSION(type
)) {
1331 case YUV_CONVERSION_FRAGMENT
:
1332 snprintf(prog_pos
, prog_remain
, yuv_prog_template
,
1333 yuv2rgb
[ROW_R
][COL_Y
], yuv2rgb
[ROW_G
][COL_Y
], yuv2rgb
[ROW_B
][COL_Y
],
1334 yuv2rgb
[ROW_R
][COL_U
], yuv2rgb
[ROW_G
][COL_U
], yuv2rgb
[ROW_B
][COL_U
],
1335 yuv2rgb
[ROW_R
][COL_V
], yuv2rgb
[ROW_G
][COL_V
], yuv2rgb
[ROW_B
][COL_V
],
1336 yuv2rgb
[ROW_R
][COL_C
], yuv2rgb
[ROW_G
][COL_C
], yuv2rgb
[ROW_B
][COL_C
]);
1338 case YUV_CONVERSION_FRAGMENT_POW
:
1339 snprintf(prog_pos
, prog_remain
, yuv_pow_prog_template
,
1340 yuv2rgb
[ROW_R
][COL_Y
], yuv2rgb
[ROW_G
][COL_Y
], yuv2rgb
[ROW_B
][COL_Y
],
1341 yuv2rgb
[ROW_R
][COL_U
], yuv2rgb
[ROW_G
][COL_U
], yuv2rgb
[ROW_B
][COL_U
],
1342 yuv2rgb
[ROW_R
][COL_V
], yuv2rgb
[ROW_G
][COL_V
], yuv2rgb
[ROW_B
][COL_V
],
1343 yuv2rgb
[ROW_R
][COL_C
], yuv2rgb
[ROW_G
][COL_C
], yuv2rgb
[ROW_B
][COL_C
],
1344 (float)1.0 / params
->rgamma
, (float)1.0 / params
->bgamma
, (float)1.0 / params
->bgamma
);
1346 case YUV_CONVERSION_FRAGMENT_LOOKUP
:
1347 snprintf(prog_pos
, prog_remain
, yuv_lookup_prog_template
,
1348 yuv2rgb
[ROW_R
][COL_Y
], yuv2rgb
[ROW_G
][COL_Y
], yuv2rgb
[ROW_B
][COL_Y
],
1349 yuv2rgb
[ROW_R
][COL_U
], yuv2rgb
[ROW_G
][COL_U
], yuv2rgb
[ROW_B
][COL_U
],
1350 yuv2rgb
[ROW_R
][COL_V
], yuv2rgb
[ROW_G
][COL_V
], yuv2rgb
[ROW_B
][COL_V
],
1351 yuv2rgb
[ROW_R
][COL_C
], yuv2rgb
[ROW_G
][COL_C
], yuv2rgb
[ROW_B
][COL_C
],
1352 conv_texs
[0], conv_texs
[0], conv_texs
[0]);
1354 case YUV_CONVERSION_FRAGMENT_LOOKUP3D
:
1355 snprintf(prog_pos
, prog_remain
, yuv_lookup3d_prog_template
, conv_texs
[0]);
1358 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] unknown conversion type %i\n", YUV_CONVERSION(type
));
1361 mp_msg(MSGT_VO
, MSGL_DBG2
, "[gl] generated fragment program:\n%s\n", yuv_prog
);
1362 loadGPUProgram(GL_FRAGMENT_PROGRAM
, yuv_prog
);
1367 * \brief little helper function to create a lookup table for gamma
1368 * \param map buffer to create map into
1369 * \param size size of buffer
1370 * \param gamma gamma value
1372 static void gen_gamma_map(unsigned char *map
, int size
, float gamma
) {
1375 for (i
= 0; i
< size
; i
++)
1376 map
[i
] = 255 * i
/ (size
- 1);
1379 gamma
= 1.0 / gamma
;
1380 for (i
= 0; i
< size
; i
++) {
1381 float tmp
= (float)i
/ (size
- 1.0);
1382 tmp
= pow(tmp
, gamma
);
1383 if (tmp
> 1.0) tmp
= 1.0;
1384 if (tmp
< 0.0) tmp
= 0.0;
1390 * \brief setup YUV->RGB conversion
1391 * \param parms struct containing parameters like conversion and scaler type,
1393 * \ingroup glconversion
1395 void glSetupYUVConversion(gl_conversion_params_t
*params
) {
1396 float uvcos
= params
->saturation
* cos(params
->hue
);
1397 float uvsin
= params
->saturation
* sin(params
->hue
);
1398 switch (YUV_CONVERSION(params
->type
)) {
1399 case YUV_CONVERSION_COMBINERS
:
1400 glSetupYUVCombiners(uvcos
, uvsin
);
1402 case YUV_CONVERSION_COMBINERS_ATI
:
1403 glSetupYUVCombinersATI(uvcos
, uvsin
);
1405 case YUV_CONVERSION_FRAGMENT_LOOKUP
:
1406 case YUV_CONVERSION_FRAGMENT_LOOKUP3D
:
1407 case YUV_CONVERSION_FRAGMENT
:
1408 case YUV_CONVERSION_FRAGMENT_POW
:
1409 glSetupYUVFragprog(params
);
1411 case YUV_CONVERSION_NONE
:
1414 mp_msg(MSGT_VO
, MSGL_ERR
, "[gl] unknown conversion type %i\n", YUV_CONVERSION(params
->type
));
1419 * \brief enable the specified YUV conversion
1420 * \param target texture target for Y, U and V textures (e.g. GL_TEXTURE_2D)
1421 * \param type type of YUV conversion
1422 * \ingroup glconversion
1424 void glEnableYUVConversion(GLenum target
, int type
) {
1425 switch (YUV_CONVERSION(type
)) {
1426 case YUV_CONVERSION_COMBINERS
:
1427 ActiveTexture(GL_TEXTURE1
);
1429 ActiveTexture(GL_TEXTURE2
);
1431 ActiveTexture(GL_TEXTURE0
);
1432 Enable(GL_REGISTER_COMBINERS_NV
);
1434 case YUV_CONVERSION_COMBINERS_ATI
:
1435 ActiveTexture(GL_TEXTURE1
);
1437 ActiveTexture(GL_TEXTURE2
);
1439 ActiveTexture(GL_TEXTURE0
);
1440 Enable(GL_FRAGMENT_SHADER_ATI
);
1442 case YUV_CONVERSION_FRAGMENT_LOOKUP3D
:
1443 case YUV_CONVERSION_FRAGMENT_LOOKUP
:
1444 case YUV_CONVERSION_FRAGMENT_POW
:
1445 case YUV_CONVERSION_FRAGMENT
:
1446 case YUV_CONVERSION_NONE
:
1447 Enable(GL_FRAGMENT_PROGRAM
);
1453 * \brief disable the specified YUV conversion
1454 * \param target texture target for Y, U and V textures (e.g. GL_TEXTURE_2D)
1455 * \param type type of YUV conversion
1456 * \ingroup glconversion
1458 void glDisableYUVConversion(GLenum target
, int type
) {
1459 switch (YUV_CONVERSION(type
)) {
1460 case YUV_CONVERSION_COMBINERS
:
1461 ActiveTexture(GL_TEXTURE1
);
1463 ActiveTexture(GL_TEXTURE2
);
1465 ActiveTexture(GL_TEXTURE0
);
1466 Disable(GL_REGISTER_COMBINERS_NV
);
1468 case YUV_CONVERSION_COMBINERS_ATI
:
1469 ActiveTexture(GL_TEXTURE1
);
1471 ActiveTexture(GL_TEXTURE2
);
1473 ActiveTexture(GL_TEXTURE0
);
1474 Disable(GL_FRAGMENT_SHADER_ATI
);
1476 case YUV_CONVERSION_FRAGMENT_LOOKUP3D
:
1477 case YUV_CONVERSION_FRAGMENT_LOOKUP
:
1478 case YUV_CONVERSION_FRAGMENT_POW
:
1479 case YUV_CONVERSION_FRAGMENT
:
1480 case YUV_CONVERSION_NONE
:
1481 Disable(GL_FRAGMENT_PROGRAM
);
1487 * \brief draw a texture part at given 2D coordinates
1488 * \param x screen top coordinate
1489 * \param y screen left coordinate
1490 * \param w screen width coordinate
1491 * \param h screen height coordinate
1492 * \param tx texture top coordinate in pixels
1493 * \param ty texture left coordinate in pixels
1494 * \param tw texture part width in pixels
1495 * \param th texture part height in pixels
1496 * \param sx width of texture in pixels
1497 * \param sy height of texture in pixels
1498 * \param rect_tex whether this texture uses texture_rectangle extension
1499 * \param is_yv12 if set, also draw the textures from units 1 and 2
1500 * \param flip flip the texture upside down
1501 * \ingroup gltexture
1503 void glDrawTex(GLfloat x
, GLfloat y
, GLfloat w
, GLfloat h
,
1504 GLfloat tx
, GLfloat ty
, GLfloat tw
, GLfloat th
,
1505 int sx
, int sy
, int rect_tex
, int is_yv12
, int flip
) {
1506 GLfloat tx2
= tx
/ 2, ty2
= ty
/ 2, tw2
= tw
/ 2, th2
= th
/ 2;
1508 tx
/= sx
; ty
/= sy
; tw
/= sx
; th
/= sy
;
1509 tx2
= tx
, ty2
= ty
, tw2
= tw
, th2
= th
;
1518 MultiTexCoord2f(GL_TEXTURE1
, tx2
, ty2
);
1519 MultiTexCoord2f(GL_TEXTURE2
, tx2
, ty2
);
1522 TexCoord2f(tx
, ty
+ th
);
1524 MultiTexCoord2f(GL_TEXTURE1
, tx2
, ty2
+ th2
);
1525 MultiTexCoord2f(GL_TEXTURE2
, tx2
, ty2
+ th2
);
1528 TexCoord2f(tx
+ tw
, ty
+ th
);
1530 MultiTexCoord2f(GL_TEXTURE1
, tx2
+ tw2
, ty2
+ th2
);
1531 MultiTexCoord2f(GL_TEXTURE2
, tx2
+ tw2
, ty2
+ th2
);
1533 Vertex2f(x
+ w
, y
+ h
);
1534 TexCoord2f(tx
+ tw
, ty
);
1536 MultiTexCoord2f(GL_TEXTURE1
, tx2
+ tw2
, ty2
);
1537 MultiTexCoord2f(GL_TEXTURE2
, tx2
+ tw2
, ty2
);
1543 #ifdef CONFIG_GL_WIN32
1544 #include "w32_common.h"
1546 * \brief little helper since wglGetProcAddress definition does not fit our
1548 * \param procName name of function to look up
1549 * \return function pointer returned by wglGetProcAddress
1551 static void *w32gpa(const GLubyte
*procName
) {
1553 void *res
= wglGetProcAddress(procName
);
1554 if (res
) return res
;
1555 oglmod
= GetModuleHandle("opengl32.dll");
1556 return GetProcAddress(oglmod
, procName
);
1559 static int setGlWindow_w32(MPGLContext
*ctx
)
1561 HWND win
= vo_w32_window
;
1562 int *vinfo
= &ctx
->vinfo
.w32
;
1563 HGLRC
*context
= &ctx
->context
.w32
;
1565 HDC windc
= vo_w32_get_dc(win
);
1566 HGLRC new_context
= 0;
1567 int keep_context
= 0;
1568 int res
= SET_WINDOW_FAILED
;
1570 // should only be needed when keeping context, but not doing glFinish
1571 // can cause flickering even when we do not keep it.
1574 new_vinfo
= GetPixelFormat(windc
);
1575 if (*context
&& *vinfo
&& new_vinfo
&& *vinfo
== new_vinfo
) {
1576 // we can keep the wglContext
1577 new_context
= *context
;
1581 new_context
= wglCreateContext(windc
);
1583 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Could not create GL context!\n");
1589 if (!wglMakeCurrent(windc
, new_context
)) {
1590 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Could not set GL context!\n");
1591 if (!keep_context
) {
1592 wglDeleteContext(new_context
);
1598 vo_w32_window
= win
;
1601 GetClientRect(win
, &rect
);
1602 vo_dwidth
= rect
.right
;
1603 vo_dheight
= rect
.bottom
;
1605 if (!keep_context
) {
1607 wglDeleteContext(*context
);
1608 *context
= new_context
;
1610 getFunctions(w32gpa
, NULL
);
1612 // and inform that reinit is neccessary
1613 res
= SET_WINDOW_REINIT
;
1615 res
= SET_WINDOW_OK
;
1618 vo_w32_release_dc(win
, windc
);
1622 static void releaseGlContext_w32(MPGLContext
*ctx
) {
1623 int *vinfo
= &ctx
->vinfo
.w32
;
1624 HGLRC
*context
= &ctx
->context
.w32
;
1627 wglMakeCurrent(0, 0);
1628 wglDeleteContext(*context
);
1633 static void swapGlBuffers_w32(MPGLContext
*ctx
) {
1634 HDC vo_hdc
= vo_w32_get_dc(vo_w32_window
);
1635 SwapBuffers(vo_hdc
);
1636 vo_w32_release_dc(vo_w32_window
, vo_hdc
);
1639 #ifdef CONFIG_GL_X11
1643 #include "x11_common.h"
1645 * \brief find address of a linked function
1646 * \param s name of function to find
1647 * \return address of function or NULL if not found
1649 static void *getdladdr(const char *s
) {
1652 void *handle
= dlopen(NULL
, RTLD_LAZY
);
1655 ret
= dlsym(handle
, s
);
1662 * \brief Returns the XVisualInfo associated with Window win.
1663 * \param win Window whose XVisualInfo is returne.
1664 * \return XVisualInfo of the window. Caller must use XFree to free it.
1666 static XVisualInfo
*getWindowVisualInfo(Window win
) {
1667 XWindowAttributes xw_attr
;
1668 XVisualInfo vinfo_template
;
1670 XGetWindowAttributes(mDisplay
, win
, &xw_attr
);
1671 vinfo_template
.visualid
= XVisualIDFromVisual(xw_attr
.visual
);
1672 return XGetVisualInfo(mDisplay
, VisualIDMask
, &vinfo_template
, &tmp
);
1675 static void appendstr(char **dst
, const char *str
)
1681 newsize
= strlen(*dst
) + 1 + strlen(str
) + 1;
1682 newstr
= realloc(*dst
, newsize
);
1691 * \brief Changes the window in which video is displayed.
1692 * If possible only transfers the context to the new window, otherwise
1693 * creates a new one, which must be initialized by the caller.
1694 * \param vinfo Currently used visual.
1695 * \param context Currently used context.
1696 * \param win window that should be used for drawing.
1697 * \return one of SET_WINDOW_FAILED, SET_WINDOW_OK or SET_WINDOW_REINIT.
1698 * In case of SET_WINDOW_REINIT the context could not be transfered
1699 * and the caller must initialize it correctly.
1700 * \ingroup glcontext
1702 static int setGlWindow_x11(MPGLContext
*ctx
)
1704 XVisualInfo
**vinfo
= &ctx
->vinfo
.x11
;
1705 GLXContext
*context
= &ctx
->context
.x11
;
1706 Window win
= vo_window
;
1707 XVisualInfo
*new_vinfo
;
1708 GLXContext new_context
= NULL
;
1709 int keep_context
= 0;
1711 // should only be needed when keeping context, but not doing glFinish
1712 // can cause flickering even when we do not keep it.
1715 new_vinfo
= getWindowVisualInfo(win
);
1716 if (*context
&& *vinfo
&& new_vinfo
&&
1717 (*vinfo
)->visualid
== new_vinfo
->visualid
) {
1718 // we can keep the GLXContext
1719 new_context
= *context
;
1725 new_context
= glXCreateContext(mDisplay
, new_vinfo
, NULL
, True
);
1727 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Could not create GLX context!\n");
1729 return SET_WINDOW_FAILED
;
1734 if (!glXMakeCurrent(mDisplay
, vo_window
, new_context
)) {
1735 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Could not set GLX context!\n");
1736 if (!keep_context
) {
1737 glXDestroyContext(mDisplay
, new_context
);
1740 return SET_WINDOW_FAILED
;
1745 vo_x11_update_geometry();
1746 if (!keep_context
) {
1747 void *(*getProcAddress
)(const GLubyte
*);
1748 const char *(*glXExtStr
)(Display
*, int);
1749 char *glxstr
= strdup("");
1751 glXDestroyContext(mDisplay
, *context
);
1752 *context
= new_context
;
1756 getProcAddress
= getdladdr("glXGetProcAddress");
1757 if (!getProcAddress
)
1758 getProcAddress
= getdladdr("glXGetProcAddressARB");
1759 if (!getProcAddress
)
1760 getProcAddress
= (void *)getdladdr
;
1761 glXExtStr
= getdladdr("glXQueryExtensionsString");
1763 appendstr(&glxstr
, glXExtStr(mDisplay
, DefaultScreen(mDisplay
)));
1764 glXExtStr
= getdladdr("glXGetClientString");
1766 appendstr(&glxstr
, glXExtStr(mDisplay
, GLX_EXTENSIONS
));
1767 glXExtStr
= getdladdr("glXGetServerString");
1769 appendstr(&glxstr
, glXExtStr(mDisplay
, GLX_EXTENSIONS
));
1771 getFunctions(getProcAddress
, glxstr
);
1774 // and inform that reinit is neccessary
1775 return SET_WINDOW_REINIT
;
1777 return SET_WINDOW_OK
;
1781 * \brief free the VisualInfo and GLXContext of an OpenGL context.
1782 * \ingroup glcontext
1784 static void releaseGlContext_x11(MPGLContext
*ctx
) {
1785 XVisualInfo
**vinfo
= &ctx
->vinfo
.x11
;
1786 GLXContext
*context
= &ctx
->context
.x11
;
1793 glXMakeCurrent(mDisplay
, None
, NULL
);
1794 glXDestroyContext(mDisplay
, *context
);
1799 static void swapGlBuffers_x11(MPGLContext
*ctx
) {
1800 glXSwapBuffers(mDisplay
, vo_window
);
1803 static int x11_check_events(void) {
1804 return vo_x11_check_events(mDisplay
);
1808 int init_mpglcontext(MPGLContext
*ctx
, enum MPGLType type
) {
1809 memset(ctx
, 0, sizeof(*ctx
));
1811 switch (ctx
->type
) {
1812 #ifdef CONFIG_GL_WIN32
1814 ctx
->setGlWindow
= setGlWindow_w32
;
1815 ctx
->releaseGlContext
= releaseGlContext_w32
;
1816 ctx
->swapGlBuffers
= swapGlBuffers_w32
;
1817 ctx
->update_xinerama_info
= w32_update_xinerama_info
;
1818 ctx
->border
= vo_w32_border
;
1819 ctx
->check_events
= vo_w32_check_events
;
1820 ctx
->fullscreen
= vo_w32_fullscreen
;
1821 ctx
->ontop
= vo_w32_ontop
;
1822 return vo_w32_init();
1824 #ifdef CONFIG_GL_X11
1826 ctx
->setGlWindow
= setGlWindow_x11
;
1827 ctx
->releaseGlContext
= releaseGlContext_x11
;
1828 ctx
->swapGlBuffers
= swapGlBuffers_x11
;
1829 ctx
->update_xinerama_info
= update_xinerama_info
;
1830 ctx
->border
= vo_x11_border
;
1831 ctx
->check_events
= x11_check_events
;
1832 ctx
->fullscreen
= vo_x11_fullscreen
;
1833 ctx
->ontop
= vo_x11_ontop
;
1841 void uninit_mpglcontext(MPGLContext
*ctx
) {
1842 ctx
->releaseGlContext(ctx
);
1843 switch (ctx
->type
) {
1844 #ifdef CONFIG_GL_WIN32
1849 #ifdef CONFIG_GL_X11
1855 memset(ctx
, 0, sizeof(*ctx
));