3 * \brief OpenGL helper functions used by vo_gl.c and vo_gl2.c
5 * Common OpenGL routines.
6 * Copyleft (C) Reimar Döffinger <Reimar.Doeffinger@stud.uni-karlsruhe.de>, 2005
7 * Licensend under the GNU GPL v2.
8 * Special thanks go to the xine team and Matthias Hopf, whose video_out_opengl.c
9 * gave me lots of good ideas.
16 #include "gl_common.h"
19 * \defgroup glextfunctions OpenGL extension functions
21 * the pointers to these functions are acquired when the OpenGL
25 void (APIENTRY
*GenBuffers
)(GLsizei
, GLuint
*);
26 void (APIENTRY
*DeleteBuffers
)(GLsizei
, const GLuint
*);
27 void (APIENTRY
*BindBuffer
)(GLenum
, GLuint
);
28 GLvoid
* (APIENTRY
*MapBuffer
)(GLenum
, GLenum
);
29 GLboolean (APIENTRY
*UnmapBuffer
)(GLenum
);
30 void (APIENTRY
*BufferData
)(GLenum
, intptr_t, const GLvoid
*, GLenum
);
31 void (APIENTRY
*CombinerParameterfv
)(GLenum
, const GLfloat
*);
32 void (APIENTRY
*CombinerParameteri
)(GLenum
, GLint
);
33 void (APIENTRY
*CombinerInput
)(GLenum
, GLenum
, GLenum
, GLenum
, GLenum
,
35 void (APIENTRY
*CombinerOutput
)(GLenum
, GLenum
, GLenum
, GLenum
, GLenum
,
36 GLenum
, GLenum
, GLboolean
, GLboolean
,
38 void (APIENTRY
*BeginFragmentShader
)(void);
39 void (APIENTRY
*EndFragmentShader
)(void);
40 void (APIENTRY
*SampleMap
)(GLuint
, GLuint
, GLenum
);
41 void (APIENTRY
*ColorFragmentOp2
)(GLenum
, GLuint
, GLuint
, GLuint
, GLuint
,
42 GLuint
, GLuint
, GLuint
, GLuint
, GLuint
);
43 void (APIENTRY
*ColorFragmentOp3
)(GLenum
, GLuint
, GLuint
, GLuint
, GLuint
,
44 GLuint
, GLuint
, GLuint
, GLuint
, GLuint
,
45 GLuint
, GLuint
, GLuint
);
46 void (APIENTRY
*SetFragmentShaderConstant
)(GLuint
, const GLfloat
*);
47 void (APIENTRY
*ActiveTexture
)(GLenum
);
48 void (APIENTRY
*BindTexture
)(GLenum
, GLuint
);
49 void (APIENTRY
*MultiTexCoord2f
)(GLenum
, GLfloat
, GLfloat
);
50 void (APIENTRY
*GenPrograms
)(GLsizei
, GLuint
*);
51 void (APIENTRY
*DeletePrograms
)(GLsizei
, const GLuint
*);
52 void (APIENTRY
*BindProgram
)(GLenum
, GLuint
);
53 void (APIENTRY
*ProgramString
)(GLenum
, GLenum
, GLsizei
, const GLvoid
*);
54 void (APIENTRY
*ProgramEnvParameter4f
)(GLenum
, GLuint
, GLfloat
, GLfloat
,
56 int (APIENTRY
*SwapInterval
)(int);
57 /** \} */ // end of glextfunctions group
59 //! \defgroup glgeneral OpenGL general helper functions
61 //! \defgroup glcontext OpenGL context management helper functions
63 //! \defgroup gltexture OpenGL texture handling helper functions
65 //! \defgroup glconversion OpenGL conversion helper functions
68 * \brief adjusts the GL_UNPACK_ALIGNMENT to fit the stride.
69 * \param stride number of bytes per line for which alignment should fit.
72 void glAdjustAlignment(int stride
) {
76 else if (stride
% 4 == 0)
78 else if (stride
% 2 == 0)
82 glPixelStorei (GL_UNPACK_ALIGNMENT
, gl_alignment
);
85 struct gl_name_map_struct
{
91 #define MAP(a) {a, #a}
92 //! mapping table for the glValName function
93 static const struct gl_name_map_struct gl_name_map
[] = {
95 MAP(GL_R3_G3_B2
), MAP(GL_RGB4
), MAP(GL_RGB5
), MAP(GL_RGB8
),
96 MAP(GL_RGB10
), MAP(GL_RGB12
), MAP(GL_RGB16
), MAP(GL_RGBA2
),
97 MAP(GL_RGBA4
), MAP(GL_RGB5_A1
), MAP(GL_RGBA8
), MAP(GL_RGB10_A2
),
98 MAP(GL_RGBA12
), MAP(GL_RGBA16
), MAP(GL_LUMINANCE8
),
101 MAP(GL_RGB
), MAP(GL_RGBA
), MAP(GL_RED
), MAP(GL_GREEN
), MAP(GL_BLUE
),
102 MAP(GL_ALPHA
), MAP(GL_LUMINANCE
), MAP(GL_LUMINANCE_ALPHA
),
105 MAP(GL_BGR
), MAP(GL_BGRA
),
108 MAP(GL_BYTE
), MAP(GL_UNSIGNED_BYTE
), MAP(GL_SHORT
), MAP(GL_UNSIGNED_SHORT
),
109 MAP(GL_INT
), MAP(GL_UNSIGNED_INT
), MAP(GL_FLOAT
), MAP(GL_DOUBLE
),
110 MAP(GL_2_BYTES
), MAP(GL_3_BYTES
), MAP(GL_4_BYTES
),
112 MAP(GL_UNSIGNED_BYTE_3_3_2
), MAP(GL_UNSIGNED_BYTE_2_3_3_REV
),
113 MAP(GL_UNSIGNED_SHORT_5_6_5
), MAP(GL_UNSIGNED_SHORT_5_6_5_REV
),
114 MAP(GL_UNSIGNED_SHORT_4_4_4_4
), MAP(GL_UNSIGNED_SHORT_4_4_4_4_REV
),
115 MAP(GL_UNSIGNED_SHORT_5_5_5_1
), MAP(GL_UNSIGNED_SHORT_1_5_5_5_REV
),
116 MAP(GL_UNSIGNED_INT_8_8_8_8
), MAP(GL_UNSIGNED_INT_8_8_8_8_REV
),
117 MAP(GL_UNSIGNED_INT_10_10_10_2
), MAP(GL_UNSIGNED_INT_2_10_10_10_REV
),
123 * \brief return the name of an OpenGL constant
124 * \param value the constant
125 * \return name of the constant or "Unknown format!"
128 const char *glValName(GLint value
)
132 while (gl_name_map
[i
].name
) {
133 if (gl_name_map
[i
].value
== value
)
134 return gl_name_map
[i
].name
;
137 return "Unknown format!";
140 //! always return this format as internal texture format in glFindFormat
141 #define TEXTUREFORMAT_ALWAYS GL_RGB8
142 #undef TEXTUREFORMAT_ALWAYS
145 * \brief find the OpenGL settings coresponding to format.
147 * All parameters may be NULL.
148 * \param fmt MPlayer format to analyze.
149 * \param bpp [OUT] bits per pixel of that format.
150 * \param gl_texfmt [OUT] internal texture format that fits the
151 * image format, not necessarily the best for performance.
152 * \param gl_format [OUT] OpenGL format for this image format.
153 * \param gl_type [OUT] OpenGL type for this image format.
154 * \return 1 if format is supported by OpenGL, 0 if not.
157 int glFindFormat(uint32_t fmt
, int *bpp
, GLint
*gl_texfmt
,
158 GLenum
*gl_format
, GLenum
*gl_type
)
164 if (bpp
== NULL
) bpp
= &dummy1
;
165 if (gl_texfmt
== NULL
) gl_texfmt
= &dummy3
;
166 if (gl_format
== NULL
) gl_format
= &dummy2
;
167 if (gl_type
== NULL
) gl_type
= &dummy2
;
169 *bpp
= IMGFMT_IS_BGR(fmt
)?IMGFMT_BGR_DEPTH(fmt
):IMGFMT_RGB_DEPTH(fmt
);
174 *gl_type
= GL_UNSIGNED_BYTE
;
178 *gl_format
= GL_RGBA
;
179 *gl_type
= GL_UNSIGNED_BYTE
;
182 supported
= 0; // no native YV12 support
187 *gl_format
= GL_LUMINANCE
;
188 *gl_type
= GL_UNSIGNED_BYTE
;
191 // we do not support palettized formats, although the format the
192 // swscale produces works
195 gl_type
= GL_UNSIGNED_BYTE_2_3_3_REV
;
199 *gl_format
= GL_RGBA
;
200 *gl_type
= GL_UNSIGNED_SHORT_1_5_5_5_REV
;
204 *gl_type
= GL_UNSIGNED_SHORT_5_6_5_REV
;
208 // special case as red and blue have a differen number of bits.
209 // GL_BGR and GL_UNSIGNED_BYTE_3_3_2 isn't supported at least
210 // by nVidia drivers, and in addition would give more bits to
211 // blue than to red, which isn't wanted
213 gl_type
= GL_UNSIGNED_BYTE_3_3_2
;
217 *gl_format
= GL_BGRA
;
218 *gl_type
= GL_UNSIGNED_SHORT_1_5_5_5_REV
;
222 *gl_type
= GL_UNSIGNED_SHORT_5_6_5
;
226 *gl_type
= GL_UNSIGNED_BYTE
;
230 *gl_format
= GL_BGRA
;
231 *gl_type
= GL_UNSIGNED_BYTE
;
235 *gl_format
= GL_RGBA
;
236 *gl_type
= GL_UNSIGNED_BYTE
;
239 #ifdef TEXTUREFORMAT_ALWAYS
240 *gl_texfmt
= TEXTUREFORMAT_ALWAYS
;
245 static void *setNull(const GLubyte
*s
) {
255 static const extfunc_desc_t extfuncs
[] = {
256 {(void **)&GenBuffers
, NULL
, {"glGenBuffers", "glGenBuffersARB", NULL
}},
257 {(void **)&DeleteBuffers
, NULL
, {"glDeleteBuffers", "glDeleteBuffersARB", NULL
}},
258 {(void **)&BindBuffer
, NULL
, {"glBindBuffer", "glBindBufferARB", NULL
}},
259 {(void **)&MapBuffer
, NULL
, {"glMapBuffer", "glMapBufferARB", NULL
}},
260 {(void **)&UnmapBuffer
, NULL
, {"glUnmapBuffer", "glUnmapBufferARB", NULL
}},
261 {(void **)&BufferData
, NULL
, {"glBufferData", "glBufferDataARB", NULL
}},
262 {(void **)&CombinerParameterfv
, NULL
, {"glCombinerParameterfv", "glCombinerParameterfvNV", NULL
}},
263 {(void **)&CombinerParameteri
, NULL
, {"glCombinerParameteri", "glCombinerParameteriNV", NULL
}},
264 {(void **)&CombinerInput
, NULL
, {"glCombinerInput", "glCombinerInputNV", NULL
}},
265 {(void **)&CombinerOutput
, NULL
, {"glCombinerOutput", "glCombinerOutputNV", NULL
}},
266 {(void **)&BeginFragmentShader
, NULL
, {"glBeginFragmentShaderATI", NULL
}},
267 {(void **)&EndFragmentShader
, NULL
, {"glEndFragmentShaderATI", NULL
}},
268 {(void **)&SampleMap
, NULL
, {"glSampleMapATI", NULL
}},
269 {(void **)&ColorFragmentOp2
, NULL
, {"glColorFragmentOp2ATI", NULL
}},
270 {(void **)&ColorFragmentOp3
, NULL
, {"glColorFragmentOp3ATI", NULL
}},
271 {(void **)&SetFragmentShaderConstant
, NULL
, {"glSetFragmentShaderConstantATI", NULL
}},
272 {(void **)&ActiveTexture
, NULL
, {"glActiveTexture", "glActiveTextureARB", NULL
}},
273 {(void **)&BindTexture
, NULL
, {"glBindTexture", "glBindTextureARB", "glBindTextureEXT", NULL
}},
274 {(void **)&MultiTexCoord2f
, NULL
, {"glMultiTexCoord2f", "glMultiTexCoord2fARB", NULL
}},
275 {(void **)&GenPrograms
, NULL
, {"glGenPrograms", "glGenProgramsARB", "glGenProgramsNV", NULL
}},
276 {(void **)&DeletePrograms
, NULL
, {"glDeletePrograms", "glDeleteProgramsARB", "glDeleteProgramsNV", NULL
}},
277 {(void **)&BindProgram
, NULL
, {"glBindProgram", "glBindProgramARB", "glBindProgramNV", NULL
}},
278 {(void **)&ProgramString
, NULL
, {"glProgramString", "glProgramStringARB", "glProgramStringNV", NULL
}},
279 {(void **)&ProgramEnvParameter4f
, NULL
, {"glProgramEnvParameter4f", "glProgramEnvParameter4fARB", "glProgramEnvParameter4fNV", NULL
}},
280 {(void **)&SwapInterval
, "_swap_control", {"glXSwapInterval", "glXSwapIntervalEXT", "glXSwapIntervalSGI", "wglSwapInterval", "wglSwapIntervalEXT", "wglSwapIntervalSGI", NULL
}},
285 * \brief find the function pointers of some useful OpenGL extensions
286 * \param getProcAddress function to resolve function names, may be NULL
287 * \param ext2 an extra extension string
289 static void getFunctions(void *(*getProcAddress
)(const GLubyte
*),
291 const extfunc_desc_t
*dsc
;
292 const char *extensions
= (const char *)glGetString(GL_EXTENSIONS
);
294 if (!extensions
) extensions
= "";
295 if (!ext2
) ext2
= "";
296 allexts
= (char *)malloc(strlen(extensions
) + strlen(ext2
) + 2);
297 strcpy(allexts
, extensions
);
298 strcat(allexts
, " ");
299 strcat(allexts
, ext2
);
300 mp_msg(MSGT_VO
, MSGL_V
, "OpenGL extensions string:\n%s\n", allexts
);
302 getProcAddress
= setNull
;
303 for (dsc
= extfuncs
; dsc
->funcptr
; dsc
++) {
306 if (!dsc
->extstr
|| strstr(allexts
, dsc
->extstr
)) {
307 for (i
= 0; !ptr
&& dsc
->funcnames
[i
]; i
++)
308 ptr
= getProcAddress((const GLubyte
*)dsc
->funcnames
[i
]);
310 *(dsc
->funcptr
) = ptr
;
316 * \brief create a texture and set some defaults
317 * \param target texture taget, usually GL_TEXTURE_2D
318 * \param fmt internal texture format
319 * \param filter filter used for scaling, e.g. GL_LINEAR
320 * \param w texture width
321 * \param h texture height
322 * \param val luminance value to fill texture with
325 void glCreateClearTex(GLenum target
, GLenum fmt
, GLint filter
,
326 int w
, int h
, unsigned char val
) {
327 GLfloat fval
= (GLfloat
)val
/ 255.0;
328 GLfloat border
[4] = {fval
, fval
, fval
, fval
};
329 GLenum clrfmt
= (fmt
== GL_ALPHA
) ? GL_ALPHA
: GL_LUMINANCE
;
330 char *init
= (char *)malloc(w
* h
);
331 memset(init
, val
, w
* h
);
332 glAdjustAlignment(w
);
333 glPixelStorei(GL_UNPACK_ROW_LENGTH
, w
);
334 glTexImage2D(target
, 0, fmt
, w
, h
, 0, clrfmt
, GL_UNSIGNED_BYTE
, init
);
335 glTexParameterf(target
, GL_TEXTURE_PRIORITY
, 1.0);
336 glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
, filter
);
337 glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, filter
);
338 glTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
339 glTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
340 // Border texels should not be used with CLAMP_TO_EDGE
341 // We set a sane default anyway.
342 glTexParameterfv(target
, GL_TEXTURE_BORDER_COLOR
, border
);
347 * \brief skips whitespace and comments
348 * \param f file to read from
350 static void ppm_skip(FILE *f
) {
358 } while (c
!= EOF
&& (isspace(c
) || comment
));
363 #define MAXDIM (16 * 1024)
366 * \brief creates a texture from a PPM file
367 * \param target texture taget, usually GL_TEXTURE_2D
368 * \param fmt internal texture format
369 * \param filter filter used for scaling, e.g. GL_LINEAR
370 * \param f file to read PPM from
371 * \param width [out] width of texture
372 * \param height [out] height of texture
373 * \param maxval [out] maxval value from PPM file
374 * \return 0 on error, 1 otherwise
377 int glCreatePPMTex(GLenum target
, GLenum fmt
, GLint filter
,
378 FILE *f
, int *width
, int *height
, int *maxval
) {
379 unsigned w
, h
, m
, val
;
382 if (fgetc(f
) != 'P' || fgetc(f
) != '6')
385 if (fscanf(f
, "%u", &w
) != 1)
388 if (fscanf(f
, "%u", &h
) != 1)
391 if (fscanf(f
, "%u", &m
) != 1)
396 if (w
> MAXDIM
|| h
> MAXDIM
)
398 data
= (char *)malloc(w
* h
* 3);
399 if (fread(data
, w
* 3, h
, f
) != h
)
401 glCreateClearTex(target
, fmt
, filter
, w
, h
, 0);
402 glUploadTex(target
, GL_RGB
, GL_UNSIGNED_BYTE
, data
, w
* 3, 0, 0, w
, h
, 0);
404 if (width
) *width
= w
;
405 if (height
) *height
= h
;
406 if (maxval
) *maxval
= m
;
411 * \brief return the number of bytes per pixel for the given format
412 * \param format OpenGL format
413 * \param type OpenGL type
414 * \return bytes per pixel
417 * Does not handle all possible variants, just those used by MPlayer
419 int glFmt2bpp(GLenum format
, GLenum type
) {
421 case GL_UNSIGNED_BYTE_3_3_2
:
422 case GL_UNSIGNED_BYTE_2_3_3_REV
:
424 case GL_UNSIGNED_SHORT_5_5_5_1
:
425 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
426 case GL_UNSIGNED_SHORT_5_6_5
:
427 case GL_UNSIGNED_SHORT_5_6_5_REV
:
430 if (type
!= GL_UNSIGNED_BYTE
)
431 return 0; //not implemented
447 * \brief upload a texture, handling things like stride and slices
448 * \param target texture target, usually GL_TEXTURE_2D
449 * \param format OpenGL format of data
450 * \param type OpenGL type of data
451 * \param data data to upload
452 * \param stride data stride
453 * \param x x offset in texture
454 * \param y y offset in texture
455 * \param w width of the texture part to upload
456 * \param h height of the texture part to upload
457 * \param slice height of an upload slice, 0 for all at once
460 void glUploadTex(GLenum target
, GLenum format
, GLenum type
,
461 const void *data
, int stride
,
462 int x
, int y
, int w
, int h
, int slice
) {
464 if (w
<= 0 || h
<= 0) return;
471 // this is not always correct, but should work for MPlayer
472 glAdjustAlignment(stride
);
473 glPixelStorei(GL_UNPACK_ROW_LENGTH
, stride
/ glFmt2bpp(format
, type
));
474 for (; y
+ slice
<= y_max
; y
+= slice
) {
475 glTexSubImage2D(target
, 0, x
, y
, w
, slice
, format
, type
, data
);
476 data
+= stride
* slice
;
479 glTexSubImage2D(target
, 0, x
, y
, w
, y_max
- y
, format
, type
, data
);
482 static void fillUVcoeff(GLfloat
*ucoef
, GLfloat
*vcoef
,
483 float uvcos
, float uvsin
) {
485 ucoef
[0] = 0 * uvcos
+ 1.403 * uvsin
;
486 vcoef
[0] = 0 * uvsin
+ 1.403 * uvcos
;
487 ucoef
[1] = -0.344 * uvcos
+ -0.714 * uvsin
;
488 vcoef
[1] = -0.344 * uvsin
+ -0.714 * uvcos
;
489 ucoef
[2] = 1.770 * uvcos
+ 0 * uvsin
;
490 vcoef
[2] = 1.770 * uvsin
+ 0 * uvcos
;
493 // Coefficients (probably) must be in [0, 1] range, whereas they originally
494 // are in [-2, 2] range, so here comes the trick:
495 // First put them in the [-0.5, 0.5] range, then add 0.5.
496 // This can be undone with the HALF_BIAS and SCALE_BY_FOUR arguments
497 // for CombinerInput and CombinerOutput (or the respective ATI variants)
498 for (i
= 0; i
< 4; i
++) {
499 ucoef
[i
] = ucoef
[i
] * 0.25 + 0.5;
500 vcoef
[i
] = vcoef
[i
] * 0.25 + 0.5;
505 * \brief Setup register combiners for YUV to RGB conversion.
506 * \param uvcos used for saturation and hue adjustment
507 * \param uvsin used for saturation and hue adjustment
509 static void glSetupYUVCombiners(float uvcos
, float uvsin
) {
513 glGetIntegerv(GL_MAX_GENERAL_COMBINERS_NV
, &i
);
515 mp_msg(MSGT_VO
, MSGL_ERR
,
516 "[gl] 2 general combiners needed for YUV combiner support (found %i)\n", i
);
517 glGetIntegerv (GL_MAX_TEXTURE_UNITS
, &i
);
519 mp_msg(MSGT_VO
, MSGL_ERR
,
520 "[gl] 3 texture units needed for YUV combiner support (found %i)\n", i
);
521 if (!CombinerInput
|| !CombinerOutput
||
522 !CombinerParameterfv
|| !CombinerParameteri
) {
523 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Combiner functions missing!\n");
526 fillUVcoeff(ucoef
, vcoef
, uvcos
, uvsin
);
527 CombinerParameterfv(GL_CONSTANT_COLOR0_NV
, ucoef
);
528 CombinerParameterfv(GL_CONSTANT_COLOR1_NV
, vcoef
);
530 // UV first, like this green component cannot overflow
531 CombinerInput(GL_COMBINER0_NV
, GL_RGB
, GL_VARIABLE_A_NV
,
532 GL_TEXTURE1
, GL_HALF_BIAS_NORMAL_NV
, GL_RGB
);
533 CombinerInput(GL_COMBINER0_NV
, GL_RGB
, GL_VARIABLE_B_NV
,
534 GL_CONSTANT_COLOR0_NV
, GL_HALF_BIAS_NORMAL_NV
, GL_RGB
);
535 CombinerInput(GL_COMBINER0_NV
, GL_RGB
, GL_VARIABLE_C_NV
,
536 GL_TEXTURE2
, GL_HALF_BIAS_NORMAL_NV
, GL_RGB
);
537 CombinerInput(GL_COMBINER0_NV
, GL_RGB
, GL_VARIABLE_D_NV
,
538 GL_CONSTANT_COLOR1_NV
, GL_HALF_BIAS_NORMAL_NV
, GL_RGB
);
539 CombinerOutput(GL_COMBINER0_NV
, GL_RGB
, GL_DISCARD_NV
, GL_DISCARD_NV
,
540 GL_SPARE0_NV
, GL_SCALE_BY_FOUR_NV
, GL_NONE
, GL_FALSE
,
544 CombinerInput(GL_COMBINER1_NV
, GL_RGB
, GL_VARIABLE_A_NV
, GL_SPARE0_NV
,
545 GL_SIGNED_IDENTITY_NV
, GL_RGB
);
546 CombinerInput(GL_COMBINER1_NV
, GL_RGB
, GL_VARIABLE_B_NV
, GL_ZERO
,
547 GL_UNSIGNED_INVERT_NV
, GL_RGB
);
548 CombinerInput(GL_COMBINER1_NV
, GL_RGB
, GL_VARIABLE_C_NV
,
549 GL_TEXTURE0
, GL_SIGNED_IDENTITY_NV
, GL_RGB
);
550 CombinerInput(GL_COMBINER1_NV
, GL_RGB
, GL_VARIABLE_D_NV
, GL_ZERO
,
551 GL_UNSIGNED_INVERT_NV
, GL_RGB
);
552 CombinerOutput(GL_COMBINER1_NV
, GL_RGB
, GL_DISCARD_NV
, GL_DISCARD_NV
,
553 GL_SPARE0_NV
, GL_NONE
, GL_NONE
, GL_FALSE
,
556 // leave final combiner stage in default mode
557 CombinerParameteri(GL_NUM_GENERAL_COMBINERS_NV
, 2);
561 * \brief Setup ATI version of register combiners for YUV to RGB conversion.
562 * \param uvcos used for saturation and hue adjustment
563 * \param uvsin used for saturation and hue adjustment
565 * ATI called this fragment shader, but the name is confusing in the
566 * light of a very different OpenGL 2.0 extension with the same name
568 static void glSetupYUVCombinersATI(float uvcos
, float uvsin
) {
572 glGetIntegerv(GL_NUM_FRAGMENT_REGISTERS_ATI
, &i
);
574 mp_msg(MSGT_VO
, MSGL_ERR
,
575 "[gl] 3 registers needed for YUV combiner (ATI) support (found %i)\n", i
);
576 glGetIntegerv (GL_MAX_TEXTURE_UNITS
, &i
);
578 mp_msg(MSGT_VO
, MSGL_ERR
,
579 "[gl] 3 texture units needed for YUV combiner (ATI) support (found %i)\n", i
);
580 if (!BeginFragmentShader
|| !EndFragmentShader
||
581 !SetFragmentShaderConstant
|| !SampleMap
||
582 !ColorFragmentOp2
|| !ColorFragmentOp3
) {
583 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Combiner (ATI) functions missing!\n");
586 fillUVcoeff(ucoef
, vcoef
, uvcos
, uvsin
);
587 BeginFragmentShader();
588 SetFragmentShaderConstant(GL_CON_0_ATI
, ucoef
);
589 SetFragmentShaderConstant(GL_CON_1_ATI
, vcoef
);
590 SampleMap(GL_REG_0_ATI
, GL_TEXTURE0
, GL_SWIZZLE_STR_ATI
);
591 SampleMap(GL_REG_1_ATI
, GL_TEXTURE1
, GL_SWIZZLE_STR_ATI
);
592 SampleMap(GL_REG_2_ATI
, GL_TEXTURE2
, GL_SWIZZLE_STR_ATI
);
593 // UV first, like this green component cannot overflow
594 ColorFragmentOp2(GL_MUL_ATI
, GL_REG_1_ATI
, GL_NONE
, GL_NONE
,
595 GL_REG_1_ATI
, GL_NONE
, GL_BIAS_BIT_ATI
,
596 GL_CON_0_ATI
, GL_NONE
, GL_BIAS_BIT_ATI
);
597 ColorFragmentOp3(GL_MAD_ATI
, GL_REG_2_ATI
, GL_NONE
, GL_4X_BIT_ATI
,
598 GL_REG_2_ATI
, GL_NONE
, GL_BIAS_BIT_ATI
,
599 GL_CON_1_ATI
, GL_NONE
, GL_BIAS_BIT_ATI
,
600 GL_REG_1_ATI
, GL_NONE
, GL_NONE
);
601 ColorFragmentOp2(GL_ADD_ATI
, GL_REG_0_ATI
, GL_NONE
, GL_NONE
,
602 GL_REG_0_ATI
, GL_NONE
, GL_NONE
,
603 GL_REG_2_ATI
, GL_NONE
, GL_NONE
);
607 static const char *yuv_prog_template
=
609 "OPTION ARB_precision_hint_fastest;"
610 "PARAM ycoef = {%.4f, %.4f, %.4f};"
611 "PARAM ucoef = {%.4f, %.4f, %.4f};"
612 "PARAM vcoef = {%.4f, %.4f, %.4f};"
613 "PARAM offsets = {%.4f, %.4f, %.4f};"
614 "PARAM gamma = {%.4f, %.4f, %.4f};"
616 "TEX y, fragment.texcoord[0], texture[0], %s;"
617 "MAD res, y, ycoef, offsets;"
618 "TEX u, fragment.texcoord[1], texture[1], %s;"
619 "MAD res, u, ucoef, res;"
620 "TEX v, fragment.texcoord[2], texture[2], %s;"
621 "MAD result.color, v, vcoef, res;"
624 static const char *yuv_pow_prog_template
=
626 "OPTION ARB_precision_hint_fastest;"
627 "PARAM ycoef = {%.4f, %.4f, %.4f};"
628 "PARAM ucoef = {%.4f, %.4f, %.4f};"
629 "PARAM vcoef = {%.4f, %.4f, %.4f};"
630 "PARAM offsets = {%.4f, %.4f, %.4f};"
631 "PARAM gamma = {%.4f, %.4f, %.4f};"
633 "TEX y, fragment.texcoord[0], texture[0], %s;"
634 "MAD res, y, ycoef, offsets;"
635 "TEX u, fragment.texcoord[1], texture[1], %s;"
636 "MAD res, u, ucoef, res;"
637 "TEX v, fragment.texcoord[2], texture[2], %s;"
638 "MAD_SAT res, v, vcoef, res;"
639 "POW result.color.r, res.r, gamma.r;"
640 "POW result.color.g, res.g, gamma.g;"
641 "POW result.color.b, res.b, gamma.b;"
644 static const char *yuv_lookup_prog_template
=
646 "OPTION ARB_precision_hint_fastest;"
647 "PARAM ycoef = {%.4f, %.4f, %.4f, 0};"
648 "PARAM ucoef = {%.4f, %.4f, %.4f, 0};"
649 "PARAM vcoef = {%.4f, %.4f, %.4f, 0};"
650 "PARAM offsets = {%.4f, %.4f, %.4f, 0.125};"
651 "PARAM gamma = {%.4f, %.4f, %.4f};"
653 "TEX y, fragment.texcoord[0], texture[0], %s;"
654 "MAD res, y, ycoef, offsets;"
655 "TEX u, fragment.texcoord[1], texture[1], %s;"
656 "MAD res, u, ucoef, res;"
657 "TEX v, fragment.texcoord[2], texture[2], %s;"
658 "MAD res, v, vcoef, res;"
659 "TEX result.color.r, res.raaa, texture[3], 2D;"
660 "ADD res.a, res.a, 0.25;"
661 "TEX result.color.g, res.gaaa, texture[3], 2D;"
662 "ADD res.a, res.a, 0.25;"
663 "TEX result.color.b, res.baaa, texture[3], 2D;"
667 * \brief setup a fragment program that will do YUV->RGB conversion
668 * \param brightness brightness adjustment offset
669 * \param contrast contrast adjustment factor
670 * \param uvcos used for saturation and hue adjustment
671 * \param uvsin used for saturation and hue adjustment
672 * \param lookup use fragment program that uses texture unit 4 to
673 * do additional conversion via lookup.
675 static void glSetupYUVFragprog(float brightness
, float contrast
,
676 float uvcos
, float uvsin
, float rgamma
,
677 float ggamma
, float bgamma
, int type
, int rect
) {
679 const char *prog_template
= yuv_prog_template
;
680 char *tex_type
= rect
? "RECT" : "2D";
683 // this is the conversion matrix, with y, u, v factors
684 // for red, green, blue and the constant offsets
685 float ry
, ru
, rv
, rc
;
686 float gy
, gu
, gv
, gc
;
687 float by
, bu
, bv
, bc
;
689 case YUV_CONVERSION_FRAGMENT_POW
:
690 prog_template
= yuv_pow_prog_template
;
692 case YUV_CONVERSION_FRAGMENT_LOOKUP
:
693 prog_template
= yuv_lookup_prog_template
;
697 glGetIntegerv (GL_MAX_TEXTURE_UNITS
, &i
);
699 mp_msg(MSGT_VO
, MSGL_ERR
,
700 "[gl] 3 texture units needed for YUV fragment support (found %i)\n", i
);
702 mp_msg(MSGT_VO
, MSGL_ERR
,
703 "[gl] 4 texture units needed for YUV fragment support with lookup (found %i)\n", i
);
704 if (!ProgramString
) {
705 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] ProgramString function missing!\n");
708 ry
= 1.164 * contrast
;
709 gy
= 1.164 * contrast
;
710 by
= 1.164 * contrast
;
711 ru
= 0 * uvcos
+ 1.596 * uvsin
;
712 rv
= 0 * uvsin
+ 1.596 * uvcos
;
713 gu
= -0.391 * uvcos
+ -0.813 * uvsin
;
714 gv
= -0.391 * uvsin
+ -0.813 * uvcos
;
715 bu
= 2.018 * uvcos
+ 0 * uvsin
;
716 bv
= 2.018 * uvsin
+ 0 * uvcos
;
717 rc
= (-16 * ry
+ (-128) * ru
+ (-128) * rv
) / 255.0 + brightness
;
718 gc
= (-16 * gy
+ (-128) * gu
+ (-128) * gv
) / 255.0 + brightness
;
719 bc
= (-16 * by
+ (-128) * bu
+ (-128) * bv
) / 255.0 + brightness
;
720 // these "center" contrast control so that e.g. a contrast of 0
721 // leads to a grey image, not a black one
722 rc
+= 0.5 - contrast
/ 2.0;
723 gc
+= 0.5 - contrast
/ 2.0;
724 bc
+= 0.5 - contrast
/ 2.0;
725 rgamma
= 1.0 / rgamma
;
726 ggamma
= 1.0 / ggamma
;
727 bgamma
= 1.0 / bgamma
;
728 snprintf(yuv_prog
, 1000, prog_template
, ry
, gy
, by
, ru
, gu
, bu
, rv
, gv
, bv
,
729 rc
, gc
, bc
, rgamma
, bgamma
, bgamma
, tex_type
, tex_type
, tex_type
);
730 ProgramString(GL_FRAGMENT_PROGRAM
, GL_PROGRAM_FORMAT_ASCII
,
731 strlen(yuv_prog
), yuv_prog
);
732 glGetIntegerv(GL_PROGRAM_ERROR_POSITION
, &i
);
734 mp_msg(MSGT_VO
, MSGL_ERR
,
735 "[gl] Error compiling fragment program, make sure your card supports\n"
736 "GL_ARB_fragment_program (use glxinfo to check).%.10s\n", &yuv_prog
[i
]);
740 * \brief little helper function to create a lookup table for gamma
741 * \param map buffer to create map into
742 * \param size size of buffer
743 * \param gamma gamma value
745 static void gen_gamma_map(unsigned char *map
, int size
, float gamma
) {
748 for (i
= 0; i
< size
; i
++) {
749 float tmp
= (float)i
/ (size
- 1.0);
750 tmp
= pow(tmp
, gamma
);
751 if (tmp
> 1.0) tmp
= 1.0;
752 if (tmp
< 0.0) tmp
= 0.0;
757 //! resolution of texture for gamma lookup table
758 #define LOOKUP_RES 512
761 * \brief setup YUV->RGB conversion
762 * \param target texture target for Y, U and V textures (e.g. GL_TEXTURE_2D)
763 * \param type YUV conversion type
764 * \param brightness brightness adjustment offset
765 * \param contrast contrast adjustment factor
766 * \param hue hue adjustment angle
767 * \param saturation saturation adjustment factor
768 * \param rgamma gamma value for red channel
769 * \param ggamma gamma value for green channel
770 * \param bgamma gamma value for blue channel
771 * \ingroup glconversion
773 void glSetupYUVConversion(GLenum target
, int type
,
774 float brightness
, float contrast
,
775 float hue
, float saturation
,
776 float rgamma
, float ggamma
, float bgamma
) {
777 float uvcos
= saturation
* cos(hue
);
778 float uvsin
= saturation
* sin(hue
);
780 case YUV_CONVERSION_COMBINERS
:
781 glSetupYUVCombiners(uvcos
, uvsin
);
783 case YUV_CONVERSION_COMBINERS_ATI
:
784 glSetupYUVCombinersATI(uvcos
, uvsin
);
786 case YUV_CONVERSION_FRAGMENT_LOOKUP
:
788 unsigned char lookup_data
[4 * LOOKUP_RES
];
789 gen_gamma_map(lookup_data
, LOOKUP_RES
, rgamma
);
790 gen_gamma_map(&lookup_data
[LOOKUP_RES
], LOOKUP_RES
, ggamma
);
791 gen_gamma_map(&lookup_data
[2 * LOOKUP_RES
], LOOKUP_RES
, bgamma
);
792 ActiveTexture(GL_TEXTURE3
);
793 glCreateClearTex(GL_TEXTURE_2D
, GL_LUMINANCE8
, GL_LINEAR
,
795 glUploadTex(GL_TEXTURE_2D
, GL_LUMINANCE
, GL_UNSIGNED_BYTE
, lookup_data
,
796 LOOKUP_RES
, 0, 0, LOOKUP_RES
, 4, 0);
797 ActiveTexture(GL_TEXTURE0
);
799 case YUV_CONVERSION_FRAGMENT
:
800 case YUV_CONVERSION_FRAGMENT_POW
:
801 glSetupYUVFragprog(brightness
, contrast
, uvcos
, uvsin
,
802 rgamma
, ggamma
, bgamma
, type
,
803 target
== GL_TEXTURE_RECTANGLE
);
809 * \brief enable the specified YUV conversion
810 * \param target texture target for Y, U and V textures (e.g. GL_TEXTURE_2D)
811 * \param type type of YUV conversion
812 * \ingroup glconversion
814 void glEnableYUVConversion(GLenum target
, int type
) {
815 if (type
<= 0) return;
817 case YUV_CONVERSION_COMBINERS
:
818 ActiveTexture(GL_TEXTURE1
);
820 ActiveTexture(GL_TEXTURE2
);
822 ActiveTexture(GL_TEXTURE0
);
823 glEnable(GL_REGISTER_COMBINERS_NV
);
825 case YUV_CONVERSION_COMBINERS_ATI
:
826 ActiveTexture(GL_TEXTURE1
);
828 ActiveTexture(GL_TEXTURE2
);
830 ActiveTexture(GL_TEXTURE0
);
831 glEnable(GL_FRAGMENT_SHADER_ATI
);
833 case YUV_CONVERSION_FRAGMENT_LOOKUP
:
834 case YUV_CONVERSION_FRAGMENT_POW
:
835 case YUV_CONVERSION_FRAGMENT
:
836 glEnable(GL_FRAGMENT_PROGRAM
);
842 * \brief disable the specified YUV conversion
843 * \param target texture target for Y, U and V textures (e.g. GL_TEXTURE_2D)
844 * \param type type of YUV conversion
845 * \ingroup glconversion
847 void glDisableYUVConversion(GLenum target
, int type
) {
848 if (type
<= 0) return;
850 case YUV_CONVERSION_COMBINERS
:
851 ActiveTexture(GL_TEXTURE1
);
853 ActiveTexture(GL_TEXTURE2
);
855 ActiveTexture(GL_TEXTURE0
);
856 glDisable(GL_REGISTER_COMBINERS_NV
);
858 case YUV_CONVERSION_COMBINERS_ATI
:
859 ActiveTexture(GL_TEXTURE1
);
861 ActiveTexture(GL_TEXTURE2
);
863 ActiveTexture(GL_TEXTURE0
);
864 glDisable(GL_FRAGMENT_SHADER_ATI
);
866 case YUV_CONVERSION_FRAGMENT_LOOKUP
:
867 case YUV_CONVERSION_FRAGMENT_POW
:
868 case YUV_CONVERSION_FRAGMENT
:
869 glDisable(GL_FRAGMENT_PROGRAM
);
875 * \brief draw a texture part at given 2D coordinates
876 * \param x screen top coordinate
877 * \param y screen left coordinate
878 * \param w screen width coordinate
879 * \param h screen height coordinate
880 * \param tx texture top coordinate in pixels
881 * \param ty texture left coordinate in pixels
882 * \param tw texture part width in pixels
883 * \param th texture part height in pixels
884 * \param sx width of texture in pixels
885 * \param sy height of texture in pixels
886 * \param rect_tex whether this texture uses texture_rectangle extension
887 * \param is_yv12 if set, also draw the textures from units 1 and 2
888 * \param flip flip the texture upside down
891 void glDrawTex(GLfloat x
, GLfloat y
, GLfloat w
, GLfloat h
,
892 GLfloat tx
, GLfloat ty
, GLfloat tw
, GLfloat th
,
893 int sx
, int sy
, int rect_tex
, int is_yv12
, int flip
) {
894 GLfloat tx2
= tx
/ 2, ty2
= ty
/ 2, tw2
= tw
/ 2, th2
= th
/ 2;
896 tx
/= sx
; ty
/= sy
; tw
/= sx
; th
/= sy
;
897 tx2
= tx
, ty2
= ty
, tw2
= tw
, th2
= th
;
904 glTexCoord2f(tx
, ty
);
906 MultiTexCoord2f(GL_TEXTURE1
, tx2
, ty2
);
907 MultiTexCoord2f(GL_TEXTURE2
, tx2
, ty2
);
910 glTexCoord2f(tx
, ty
+ th
);
912 MultiTexCoord2f(GL_TEXTURE1
, tx2
, ty2
+ th2
);
913 MultiTexCoord2f(GL_TEXTURE2
, tx2
, ty2
+ th2
);
915 glVertex2f(x
, y
+ h
);
916 glTexCoord2f(tx
+ tw
, ty
+ th
);
918 MultiTexCoord2f(GL_TEXTURE1
, tx2
+ tw2
, ty2
+ th2
);
919 MultiTexCoord2f(GL_TEXTURE2
, tx2
+ tw2
, ty2
+ th2
);
921 glVertex2f(x
+ w
, y
+ h
);
922 glTexCoord2f(tx
+ tw
, ty
);
924 MultiTexCoord2f(GL_TEXTURE1
, tx2
+ tw2
, ty2
);
925 MultiTexCoord2f(GL_TEXTURE2
, tx2
+ tw2
, ty2
);
927 glVertex2f(x
+ w
, y
);
932 #include "w32_common.h"
934 * \brief little helper since wglGetProcAddress definition does not fit our
936 * \param procName name of function to look up
937 * \return function pointer returned by wglGetProcAddress
939 static void *w32gpa(const GLubyte
*procName
) {
940 return wglGetProcAddress(procName
);
943 int setGlWindow(int *vinfo
, HGLRC
*context
, HWND win
)
946 HDC windc
= GetDC(win
);
947 HGLRC new_context
= 0;
948 int keep_context
= 0;
950 // should only be needed when keeping context, but not doing glFinish
951 // can cause flickering even when we do not keep it.
954 new_vinfo
= GetPixelFormat(windc
);
955 if (*context
&& *vinfo
&& new_vinfo
&& *vinfo
== new_vinfo
) {
956 // we can keep the wglContext
957 new_context
= *context
;
961 new_context
= wglCreateContext(windc
);
963 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Could not create GL context!\n");
964 return SET_WINDOW_FAILED
;
969 if (!wglMakeCurrent(windc
, new_context
)) {
970 mp_msg (MSGT_VO
, MSGL_FATAL
, "[gl] Could not set GL context!\n");
972 wglDeleteContext(new_context
);
974 return SET_WINDOW_FAILED
;
982 GetClientRect(win
, &rect
);
983 vo_dwidth
= rect
.right
;
984 vo_dheight
= rect
.bottom
;
988 wglDeleteContext(*context
);
989 *context
= new_context
;
991 getFunctions(w32gpa
, NULL
);
993 // and inform that reinit is neccessary
994 return SET_WINDOW_REINIT
;
996 return SET_WINDOW_OK
;
999 void releaseGlContext(int *vinfo
, HGLRC
*context
) {
1002 wglMakeCurrent(0, 0);
1003 wglDeleteContext(*context
);
1008 void swapGlBuffers() {
1009 SwapBuffers(vo_hdc
);
1015 #include "x11_common.h"
1017 * \brief find address of a linked function
1018 * \param s name of function to find
1019 * \return address of function or NULL if not found
1023 static void *getdladdr(const char *s
) {
1025 #if defined(__sun) || defined(__sgi)
1026 static void *handle
= NULL
;
1028 handle
= dlopen(NULL
, RTLD_LAZY
);
1029 return dlsym(handle
, s
);
1039 * \brief Returns the XVisualInfo associated with Window win.
1040 * \param win Window whose XVisualInfo is returne.
1041 * \return XVisualInfo of the window. Caller must use XFree to free it.
1043 static XVisualInfo
*getWindowVisualInfo(Window win
) {
1044 XWindowAttributes xw_attr
;
1045 XVisualInfo vinfo_template
;
1047 XGetWindowAttributes(mDisplay
, win
, &xw_attr
);
1048 vinfo_template
.visualid
= XVisualIDFromVisual(xw_attr
.visual
);
1049 return XGetVisualInfo(mDisplay
, VisualIDMask
, &vinfo_template
, &tmp
);
1053 * \brief Changes the window in which video is displayed.
1054 * If possible only transfers the context to the new window, otherwise
1055 * creates a new one, which must be initialized by the caller.
1056 * \param vinfo Currently used visual.
1057 * \param context Currently used context.
1058 * \param win window that should be used for drawing.
1059 * \return one of SET_WINDOW_FAILED, SET_WINDOW_OK or SET_WINDOW_REINIT.
1060 * In case of SET_WINDOW_REINIT the context could not be transfered
1061 * and the caller must initialize it correctly.
1062 * \ingroup glcontext
1064 int setGlWindow(XVisualInfo
**vinfo
, GLXContext
*context
, Window win
)
1066 XVisualInfo
*new_vinfo
;
1067 GLXContext new_context
= NULL
;
1068 int keep_context
= 0;
1070 // should only be needed when keeping context, but not doing glFinish
1071 // can cause flickering even when we do not keep it.
1074 new_vinfo
= getWindowVisualInfo(win
);
1075 if (*context
&& *vinfo
&& new_vinfo
&&
1076 (*vinfo
)->visualid
== new_vinfo
->visualid
) {
1077 // we can keep the GLXContext
1078 new_context
= *context
;
1084 new_context
= glXCreateContext(mDisplay
, new_vinfo
, NULL
, True
);
1086 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Could not create GLX context!\n");
1088 return SET_WINDOW_FAILED
;
1093 if (!glXMakeCurrent(mDisplay
, vo_window
, new_context
)) {
1094 mp_msg (MSGT_VO
, MSGL_FATAL
, "[gl] Could not set GLX context!\n");
1095 if (!keep_context
) {
1096 glXDestroyContext (mDisplay
, new_context
);
1099 return SET_WINDOW_FAILED
;
1108 XGetGeometry(mDisplay
, vo_window
, &root
, &tmp
, &tmp
,
1109 (unsigned *)&vo_dwidth
, (unsigned *)&vo_dheight
, &utmp
, &utmp
);
1111 if (!keep_context
) {
1112 void *(*getProcAddress
)(const GLubyte
*);
1113 const char *(*glXExtStr
)(Display
*, int);
1115 glXDestroyContext(mDisplay
, *context
);
1116 *context
= new_context
;
1120 getProcAddress
= getdladdr("glXGetProcAddress");
1121 if (!getProcAddress
)
1122 getProcAddress
= getdladdr("glXGetProcAddressARB");
1123 if (!getProcAddress
)
1124 getProcAddress
= (void *)getdladdr
;
1125 glXExtStr
= getdladdr("glXQueryExtensionsString");
1126 getFunctions(getProcAddress
, !glXExtStr
? NULL
:
1127 glXExtStr(mDisplay
, DefaultScreen(mDisplay
)));
1129 // and inform that reinit is neccessary
1130 return SET_WINDOW_REINIT
;
1132 return SET_WINDOW_OK
;
1136 * \brief free the VisualInfo and GLXContext of an OpenGL context.
1137 * \ingroup glcontext
1139 void releaseGlContext(XVisualInfo
**vinfo
, GLXContext
*context
) {
1146 glXMakeCurrent(mDisplay
, None
, NULL
);
1147 glXDestroyContext(mDisplay
, *context
);
1152 void swapGlBuffers(void) {
1153 glXSwapBuffers(mDisplay
, vo_window
);