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", 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
);
301 getProcAddress
= setNull
;
302 for (dsc
= extfuncs
; dsc
->funcptr
; dsc
++) {
305 if (!dsc
->extstr
|| strstr(allexts
, dsc
->extstr
)) {
306 for (i
= 0; !ptr
&& dsc
->funcnames
[i
]; i
++)
307 ptr
= getProcAddress((const GLubyte
*)dsc
->funcnames
[i
]);
309 *(dsc
->funcptr
) = ptr
;
315 * \brief create a texture and set some defaults
316 * \param target texture taget, usually GL_TEXTURE_2D
317 * \param fmt internal texture format
318 * \param filter filter used for scaling, e.g. GL_LINEAR
319 * \param w texture width
320 * \param h texture height
321 * \param val luminance value to fill texture with
324 void glCreateClearTex(GLenum target
, GLenum fmt
, GLint filter
,
325 int w
, int h
, unsigned char val
) {
326 GLfloat fval
= (GLfloat
)val
/ 255.0;
327 GLfloat border
[4] = {fval
, fval
, fval
, fval
};
328 GLenum clrfmt
= (fmt
== GL_ALPHA
) ? GL_ALPHA
: GL_LUMINANCE
;
329 char *init
= (char *)malloc(w
* h
);
330 memset(init
, val
, w
* h
);
331 glAdjustAlignment(w
);
332 glPixelStorei(GL_UNPACK_ROW_LENGTH
, w
);
333 glTexImage2D(target
, 0, fmt
, w
, h
, 0, clrfmt
, GL_UNSIGNED_BYTE
, init
);
334 glTexParameterf(target
, GL_TEXTURE_PRIORITY
, 1.0);
335 glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
, filter
);
336 glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, filter
);
337 glTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
338 glTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
339 // Border texels should not be used with CLAMP_TO_EDGE
340 // We set a sane default anyway.
341 glTexParameterfv(target
, GL_TEXTURE_BORDER_COLOR
, border
);
346 * \brief skips whitespace and comments
347 * \param f file to read from
349 static void ppm_skip(FILE *f
) {
357 } while (c
!= EOF
&& (isspace(c
) || comment
));
363 * \brief creates a texture from a PPM file
364 * \param target texture taget, usually GL_TEXTURE_2D
365 * \param fmt internal texture format
366 * \param filter filter used for scaling, e.g. GL_LINEAR
367 * \param f file to read PPM from
368 * \param width [out] width of texture
369 * \param height [out] height of texture
370 * \param maxval [out] maxval value from PPM file
371 * \return 0 on error, 1 otherwise
374 int glCreatePPMTex(GLenum target
, GLenum fmt
, GLint filter
,
375 FILE *f
, int *width
, int *height
, int *maxval
) {
379 if (fgetc(f
) != 'P' || fgetc(f
) != '6')
382 if (fscanf(f
, "%i", &w
) != 1)
385 if (fscanf(f
, "%i", &h
) != 1)
388 if (fscanf(f
, "%i", &m
) != 1)
393 data
= (char *)malloc(w
* h
* 3);
394 if (fread(data
, w
* 3, h
, f
) != h
)
396 glCreateClearTex(target
, fmt
, filter
, w
, h
, 0);
397 glUploadTex(target
, GL_RGB
, GL_UNSIGNED_BYTE
, data
, w
* 3, 0, 0, w
, h
, 0);
399 if (width
) *width
= w
;
400 if (height
) *height
= h
;
401 if (maxval
) *maxval
= m
;
406 * \brief return the number of bytes per pixel for the given format
407 * \param format OpenGL format
408 * \param type OpenGL type
409 * \return bytes per pixel
412 * Does not handle all possible variants, just those used by MPlayer
414 int glFmt2bpp(GLenum format
, GLenum type
) {
416 case GL_UNSIGNED_BYTE_3_3_2
:
417 case GL_UNSIGNED_BYTE_2_3_3_REV
:
419 case GL_UNSIGNED_SHORT_5_5_5_1
:
420 case GL_UNSIGNED_SHORT_1_5_5_5_REV
:
421 case GL_UNSIGNED_SHORT_5_6_5
:
422 case GL_UNSIGNED_SHORT_5_6_5_REV
:
425 if (type
!= GL_UNSIGNED_BYTE
)
426 return 0; //not implemented
442 * \brief upload a texture, handling things like stride and slices
443 * \param target texture target, usually GL_TEXTURE_2D
444 * \param format OpenGL format of data
445 * \param type OpenGL type of data
446 * \param data data to upload
447 * \param stride data stride
448 * \param x x offset in texture
449 * \param y y offset in texture
450 * \param w width of the texture part to upload
451 * \param h height of the texture part to upload
452 * \param slice height of an upload slice, 0 for all at once
455 void glUploadTex(GLenum target
, GLenum format
, GLenum type
,
456 const void *data
, int stride
,
457 int x
, int y
, int w
, int h
, int slice
) {
459 if (w
<= 0 || h
<= 0) return;
466 // this is not always correct, but should work for MPlayer
467 glAdjustAlignment(stride
);
468 glPixelStorei(GL_UNPACK_ROW_LENGTH
, stride
/ glFmt2bpp(format
, type
));
469 for (; y
+ slice
<= y_max
; y
+= slice
) {
470 glTexSubImage2D(target
, 0, x
, y
, w
, slice
, format
, type
, data
);
471 data
+= stride
* slice
;
474 glTexSubImage2D(target
, 0, x
, y
, w
, y_max
- y
, format
, type
, data
);
477 static void fillUVcoeff(GLfloat
*ucoef
, GLfloat
*vcoef
,
478 float uvcos
, float uvsin
) {
480 ucoef
[0] = 0 * uvcos
+ 1.403 * uvsin
;
481 vcoef
[0] = 0 * uvsin
+ 1.403 * uvcos
;
482 ucoef
[1] = -0.344 * uvcos
+ -0.714 * uvsin
;
483 vcoef
[1] = -0.344 * uvsin
+ -0.714 * uvcos
;
484 ucoef
[2] = 1.770 * uvcos
+ 0 * uvsin
;
485 vcoef
[2] = 1.770 * uvsin
+ 0 * uvcos
;
488 // Coefficients (probably) must be in [0, 1] range, whereas they originally
489 // are in [-2, 2] range, so here comes the trick:
490 // First put them in the [-0.5, 0.5] range, then add 0.5.
491 // This can be undone with the HALF_BIAS and SCALE_BY_FOUR arguments
492 // for CombinerInput and CombinerOutput (or the respective ATI variants)
493 for (i
= 0; i
< 4; i
++) {
494 ucoef
[i
] = ucoef
[i
] * 0.25 + 0.5;
495 vcoef
[i
] = vcoef
[i
] * 0.25 + 0.5;
500 * \brief Setup register combiners for YUV to RGB conversion.
501 * \param uvcos used for saturation and hue adjustment
502 * \param uvsin used for saturation and hue adjustment
504 static void glSetupYUVCombiners(float uvcos
, float uvsin
) {
508 glGetIntegerv(GL_MAX_GENERAL_COMBINERS_NV
, &i
);
510 mp_msg(MSGT_VO
, MSGL_ERR
,
511 "[gl] 2 general combiners needed for YUV combiner support (found %i)\n", i
);
512 glGetIntegerv (GL_MAX_TEXTURE_UNITS
, &i
);
514 mp_msg(MSGT_VO
, MSGL_ERR
,
515 "[gl] 3 texture units needed for YUV combiner support (found %i)\n", i
);
516 if (!CombinerInput
|| !CombinerOutput
||
517 !CombinerParameterfv
|| !CombinerParameteri
) {
518 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Combiner functions missing!\n");
521 fillUVcoeff(ucoef
, vcoef
, uvcos
, uvsin
);
522 CombinerParameterfv(GL_CONSTANT_COLOR0_NV
, ucoef
);
523 CombinerParameterfv(GL_CONSTANT_COLOR1_NV
, vcoef
);
525 // UV first, like this green component cannot overflow
526 CombinerInput(GL_COMBINER0_NV
, GL_RGB
, GL_VARIABLE_A_NV
,
527 GL_TEXTURE1
, GL_HALF_BIAS_NORMAL_NV
, GL_RGB
);
528 CombinerInput(GL_COMBINER0_NV
, GL_RGB
, GL_VARIABLE_B_NV
,
529 GL_CONSTANT_COLOR0_NV
, GL_HALF_BIAS_NORMAL_NV
, GL_RGB
);
530 CombinerInput(GL_COMBINER0_NV
, GL_RGB
, GL_VARIABLE_C_NV
,
531 GL_TEXTURE2
, GL_HALF_BIAS_NORMAL_NV
, GL_RGB
);
532 CombinerInput(GL_COMBINER0_NV
, GL_RGB
, GL_VARIABLE_D_NV
,
533 GL_CONSTANT_COLOR1_NV
, GL_HALF_BIAS_NORMAL_NV
, GL_RGB
);
534 CombinerOutput(GL_COMBINER0_NV
, GL_RGB
, GL_DISCARD_NV
, GL_DISCARD_NV
,
535 GL_SPARE0_NV
, GL_SCALE_BY_FOUR_NV
, GL_NONE
, GL_FALSE
,
539 CombinerInput(GL_COMBINER1_NV
, GL_RGB
, GL_VARIABLE_A_NV
, GL_SPARE0_NV
,
540 GL_SIGNED_IDENTITY_NV
, GL_RGB
);
541 CombinerInput(GL_COMBINER1_NV
, GL_RGB
, GL_VARIABLE_B_NV
, GL_ZERO
,
542 GL_UNSIGNED_INVERT_NV
, GL_RGB
);
543 CombinerInput(GL_COMBINER1_NV
, GL_RGB
, GL_VARIABLE_C_NV
,
544 GL_TEXTURE0
, GL_SIGNED_IDENTITY_NV
, GL_RGB
);
545 CombinerInput(GL_COMBINER1_NV
, GL_RGB
, GL_VARIABLE_D_NV
, GL_ZERO
,
546 GL_UNSIGNED_INVERT_NV
, GL_RGB
);
547 CombinerOutput(GL_COMBINER1_NV
, GL_RGB
, GL_DISCARD_NV
, GL_DISCARD_NV
,
548 GL_SPARE0_NV
, GL_NONE
, GL_NONE
, GL_FALSE
,
551 // leave final combiner stage in default mode
552 CombinerParameteri(GL_NUM_GENERAL_COMBINERS_NV
, 2);
556 * \brief Setup ATI version of register combiners for YUV to RGB conversion.
557 * \param uvcos used for saturation and hue adjustment
558 * \param uvsin used for saturation and hue adjustment
560 * ATI called this fragment shader, but the name is confusing in the
561 * light of a very different OpenGL 2.0 extension with the same name
563 static void glSetupYUVCombinersATI(float uvcos
, float uvsin
) {
567 glGetIntegerv(GL_NUM_FRAGMENT_REGISTERS_ATI
, &i
);
569 mp_msg(MSGT_VO
, MSGL_ERR
,
570 "[gl] 3 registers needed for YUV combiner (ATI) support (found %i)\n", i
);
571 glGetIntegerv (GL_MAX_TEXTURE_UNITS
, &i
);
573 mp_msg(MSGT_VO
, MSGL_ERR
,
574 "[gl] 3 texture units needed for YUV combiner (ATI) support (found %i)\n", i
);
575 if (!BeginFragmentShader
|| !EndFragmentShader
||
576 !SetFragmentShaderConstant
|| !SampleMap
||
577 !ColorFragmentOp2
|| !ColorFragmentOp3
) {
578 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Combiner (ATI) functions missing!\n");
581 fillUVcoeff(ucoef
, vcoef
, uvcos
, uvsin
);
582 BeginFragmentShader();
583 SetFragmentShaderConstant(GL_CON_0_ATI
, ucoef
);
584 SetFragmentShaderConstant(GL_CON_1_ATI
, vcoef
);
585 SampleMap(GL_REG_0_ATI
, GL_TEXTURE0
, GL_SWIZZLE_STR_ATI
);
586 SampleMap(GL_REG_1_ATI
, GL_TEXTURE1
, GL_SWIZZLE_STR_ATI
);
587 SampleMap(GL_REG_2_ATI
, GL_TEXTURE2
, GL_SWIZZLE_STR_ATI
);
588 // UV first, like this green component cannot overflow
589 ColorFragmentOp2(GL_MUL_ATI
, GL_REG_1_ATI
, GL_NONE
, GL_NONE
,
590 GL_REG_1_ATI
, GL_NONE
, GL_BIAS_BIT_ATI
,
591 GL_CON_0_ATI
, GL_NONE
, GL_BIAS_BIT_ATI
);
592 ColorFragmentOp3(GL_MAD_ATI
, GL_REG_2_ATI
, GL_NONE
, GL_4X_BIT_ATI
,
593 GL_REG_2_ATI
, GL_NONE
, GL_BIAS_BIT_ATI
,
594 GL_CON_1_ATI
, GL_NONE
, GL_BIAS_BIT_ATI
,
595 GL_REG_1_ATI
, GL_NONE
, GL_NONE
);
596 ColorFragmentOp2(GL_ADD_ATI
, GL_REG_0_ATI
, GL_NONE
, GL_NONE
,
597 GL_REG_0_ATI
, GL_NONE
, GL_NONE
,
598 GL_REG_2_ATI
, GL_NONE
, GL_NONE
);
602 static const char *yuv_prog_template
=
604 "OPTION ARB_precision_hint_fastest;"
605 "PARAM ycoef = {%.4f, %.4f, %.4f};"
606 "PARAM ucoef = {%.4f, %.4f, %.4f};"
607 "PARAM vcoef = {%.4f, %.4f, %.4f};"
608 "PARAM offsets = {%.4f, %.4f, %.4f};"
609 "PARAM gamma = {%.4f, %.4f, %.4f};"
611 "TEX y, fragment.texcoord[0], texture[0], %s;"
612 "MAD res, y, ycoef, offsets;"
613 "TEX u, fragment.texcoord[1], texture[1], %s;"
614 "MAD res, u, ucoef, res;"
615 "TEX v, fragment.texcoord[2], texture[2], %s;"
616 "MAD result.color, v, vcoef, res;"
619 static const char *yuv_pow_prog_template
=
621 "OPTION ARB_precision_hint_fastest;"
622 "PARAM ycoef = {%.4f, %.4f, %.4f};"
623 "PARAM ucoef = {%.4f, %.4f, %.4f};"
624 "PARAM vcoef = {%.4f, %.4f, %.4f};"
625 "PARAM offsets = {%.4f, %.4f, %.4f};"
626 "PARAM gamma = {%.4f, %.4f, %.4f};"
628 "TEX y, fragment.texcoord[0], texture[0], %s;"
629 "MAD res, y, ycoef, offsets;"
630 "TEX u, fragment.texcoord[1], texture[1], %s;"
631 "MAD res, u, ucoef, res;"
632 "TEX v, fragment.texcoord[2], texture[2], %s;"
633 "MAD_SAT res, v, vcoef, res;"
634 "POW result.color.r, res.r, gamma.r;"
635 "POW result.color.g, res.g, gamma.g;"
636 "POW result.color.b, res.b, gamma.b;"
639 static const char *yuv_lookup_prog_template
=
641 "OPTION ARB_precision_hint_fastest;"
642 "PARAM ycoef = {%.4f, %.4f, %.4f, 0};"
643 "PARAM ucoef = {%.4f, %.4f, %.4f, 0};"
644 "PARAM vcoef = {%.4f, %.4f, %.4f, 0};"
645 "PARAM offsets = {%.4f, %.4f, %.4f, 0.125};"
646 "PARAM gamma = {%.4f, %.4f, %.4f};"
648 "TEX y, fragment.texcoord[0], texture[0], %s;"
649 "MAD res, y, ycoef, offsets;"
650 "TEX u, fragment.texcoord[1], texture[1], %s;"
651 "MAD res, u, ucoef, res;"
652 "TEX v, fragment.texcoord[2], texture[2], %s;"
653 "MAD res, v, vcoef, res;"
654 "TEX result.color.r, res.raaa, texture[3], 2D;"
655 "ADD res.a, res.a, 0.25;"
656 "TEX result.color.g, res.gaaa, texture[3], 2D;"
657 "ADD res.a, res.a, 0.25;"
658 "TEX result.color.b, res.baaa, texture[3], 2D;"
662 * \brief setup a fragment program that will do YUV->RGB conversion
663 * \param brightness brightness adjustment offset
664 * \param contrast contrast adjustment factor
665 * \param uvcos used for saturation and hue adjustment
666 * \param uvsin used for saturation and hue adjustment
667 * \param lookup use fragment program that uses texture unit 4 to
668 * do additional conversion via lookup.
670 static void glSetupYUVFragprog(float brightness
, float contrast
,
671 float uvcos
, float uvsin
, float rgamma
,
672 float ggamma
, float bgamma
, int type
, int rect
) {
674 const char *prog_template
= yuv_prog_template
;
675 char *tex_type
= rect
? "RECT" : "2D";
678 // this is the conversion matrix, with y, u, v factors
679 // for red, green, blue and the constant offsets
680 float ry
, ru
, rv
, rc
;
681 float gy
, gu
, gv
, gc
;
682 float by
, bu
, bv
, bc
;
684 case YUV_CONVERSION_FRAGMENT_POW
:
685 prog_template
= yuv_pow_prog_template
;
687 case YUV_CONVERSION_FRAGMENT_LOOKUP
:
688 prog_template
= yuv_lookup_prog_template
;
692 glGetIntegerv (GL_MAX_TEXTURE_UNITS
, &i
);
694 mp_msg(MSGT_VO
, MSGL_ERR
,
695 "[gl] 3 texture units needed for YUV fragment support (found %i)\n", i
);
697 mp_msg(MSGT_VO
, MSGL_ERR
,
698 "[gl] 4 texture units needed for YUV fragment support with lookup (found %i)\n", i
);
699 if (!ProgramString
) {
700 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] ProgramString function missing!\n");
703 ry
= 1.164 * contrast
;
704 gy
= 1.164 * contrast
;
705 by
= 1.164 * contrast
;
706 ru
= 0 * uvcos
+ 1.596 * uvsin
;
707 rv
= 0 * uvsin
+ 1.596 * uvcos
;
708 gu
= -0.391 * uvcos
+ -0.813 * uvsin
;
709 gv
= -0.391 * uvsin
+ -0.813 * uvcos
;
710 bu
= 2.018 * uvcos
+ 0 * uvsin
;
711 bv
= 2.018 * uvsin
+ 0 * uvcos
;
712 rc
= (-16 * ry
+ (-128) * ru
+ (-128) * rv
) / 255.0 + brightness
;
713 gc
= (-16 * gy
+ (-128) * gu
+ (-128) * gv
) / 255.0 + brightness
;
714 bc
= (-16 * by
+ (-128) * bu
+ (-128) * bv
) / 255.0 + brightness
;
715 // these "center" contrast control so that e.g. a contrast of 0
716 // leads to a grey image, not a black one
717 rc
+= 0.5 - contrast
/ 2.0;
718 gc
+= 0.5 - contrast
/ 2.0;
719 bc
+= 0.5 - contrast
/ 2.0;
720 rgamma
= 1.0 / rgamma
;
721 ggamma
= 1.0 / ggamma
;
722 bgamma
= 1.0 / bgamma
;
723 snprintf(yuv_prog
, 1000, prog_template
, ry
, gy
, by
, ru
, gu
, bu
, rv
, gv
, bv
,
724 rc
, gc
, bc
, rgamma
, bgamma
, bgamma
, tex_type
, tex_type
, tex_type
);
725 ProgramString(GL_FRAGMENT_PROGRAM
, GL_PROGRAM_FORMAT_ASCII
,
726 strlen(yuv_prog
), yuv_prog
);
727 glGetIntegerv(GL_PROGRAM_ERROR_POSITION
, &i
);
729 mp_msg(MSGT_VO
, MSGL_ERR
,
730 "[gl] Error compiling fragment program, make sure your card supports\n"
731 "GL_ARB_fragment_program (use glxinfo to check).%.10s\n", &yuv_prog
[i
]);
735 * \brief little helper function to create a lookup table for gamma
736 * \param map buffer to create map into
737 * \param size size of buffer
738 * \param gamma gamma value
740 static void gen_gamma_map(unsigned char *map
, int size
, float gamma
) {
743 for (i
= 0; i
< size
; i
++) {
744 float tmp
= (float)i
/ (size
- 1.0);
745 tmp
= pow(tmp
, gamma
);
746 if (tmp
> 1.0) tmp
= 1.0;
747 if (tmp
< 0.0) tmp
= 0.0;
752 //! resolution of texture for gamma lookup table
753 #define LOOKUP_RES 512
756 * \brief setup YUV->RGB conversion
757 * \param target texture target for Y, U and V textures (e.g. GL_TEXTURE_2D)
758 * \param type YUV conversion type
759 * \param brightness brightness adjustment offset
760 * \param contrast contrast adjustment factor
761 * \param hue hue adjustment angle
762 * \param saturation saturation adjustment factor
763 * \param rgamma gamma value for red channel
764 * \param ggamma gamma value for green channel
765 * \param bgamma gamma value for blue channel
766 * \ingroup glconversion
768 void glSetupYUVConversion(GLenum target
, int type
,
769 float brightness
, float contrast
,
770 float hue
, float saturation
,
771 float rgamma
, float ggamma
, float bgamma
) {
772 float uvcos
= saturation
* cos(hue
);
773 float uvsin
= saturation
* sin(hue
);
775 case YUV_CONVERSION_COMBINERS
:
776 glSetupYUVCombiners(uvcos
, uvsin
);
778 case YUV_CONVERSION_COMBINERS_ATI
:
779 glSetupYUVCombinersATI(uvcos
, uvsin
);
781 case YUV_CONVERSION_FRAGMENT_LOOKUP
:
783 unsigned char lookup_data
[4 * LOOKUP_RES
];
784 gen_gamma_map(lookup_data
, LOOKUP_RES
, rgamma
);
785 gen_gamma_map(&lookup_data
[LOOKUP_RES
], LOOKUP_RES
, ggamma
);
786 gen_gamma_map(&lookup_data
[2 * LOOKUP_RES
], LOOKUP_RES
, bgamma
);
787 ActiveTexture(GL_TEXTURE3
);
788 glCreateClearTex(GL_TEXTURE_2D
, GL_LUMINANCE8
, GL_LINEAR
,
790 glUploadTex(GL_TEXTURE_2D
, GL_LUMINANCE
, GL_UNSIGNED_BYTE
, lookup_data
,
791 LOOKUP_RES
, 0, 0, LOOKUP_RES
, 4, 0);
792 ActiveTexture(GL_TEXTURE0
);
794 case YUV_CONVERSION_FRAGMENT
:
795 case YUV_CONVERSION_FRAGMENT_POW
:
796 glSetupYUVFragprog(brightness
, contrast
, uvcos
, uvsin
,
797 rgamma
, ggamma
, bgamma
, type
,
798 target
== GL_TEXTURE_RECTANGLE
);
804 * \brief enable the specified YUV conversion
805 * \param target texture target for Y, U and V textures (e.g. GL_TEXTURE_2D)
806 * \param type type of YUV conversion
807 * \ingroup glconversion
809 void inline glEnableYUVConversion(GLenum target
, int type
) {
810 if (type
<= 0) return;
812 case YUV_CONVERSION_COMBINERS
:
813 ActiveTexture(GL_TEXTURE1
);
815 ActiveTexture(GL_TEXTURE2
);
817 ActiveTexture(GL_TEXTURE0
);
818 glEnable(GL_REGISTER_COMBINERS_NV
);
820 case YUV_CONVERSION_COMBINERS_ATI
:
821 ActiveTexture(GL_TEXTURE1
);
823 ActiveTexture(GL_TEXTURE2
);
825 ActiveTexture(GL_TEXTURE0
);
826 glEnable(GL_FRAGMENT_SHADER_ATI
);
828 case YUV_CONVERSION_FRAGMENT_LOOKUP
:
829 case YUV_CONVERSION_FRAGMENT_POW
:
830 case YUV_CONVERSION_FRAGMENT
:
831 glEnable(GL_FRAGMENT_PROGRAM
);
837 * \brief disable the specified YUV conversion
838 * \param target texture target for Y, U and V textures (e.g. GL_TEXTURE_2D)
839 * \param type type of YUV conversion
840 * \ingroup glconversion
842 void inline glDisableYUVConversion(GLenum target
, int type
) {
843 if (type
<= 0) return;
845 case YUV_CONVERSION_COMBINERS
:
846 ActiveTexture(GL_TEXTURE1
);
848 ActiveTexture(GL_TEXTURE2
);
850 ActiveTexture(GL_TEXTURE0
);
851 glDisable(GL_REGISTER_COMBINERS_NV
);
853 case YUV_CONVERSION_COMBINERS_ATI
:
854 ActiveTexture(GL_TEXTURE1
);
856 ActiveTexture(GL_TEXTURE2
);
858 ActiveTexture(GL_TEXTURE0
);
859 glDisable(GL_FRAGMENT_SHADER_ATI
);
861 case YUV_CONVERSION_FRAGMENT_LOOKUP
:
862 case YUV_CONVERSION_FRAGMENT_POW
:
863 case YUV_CONVERSION_FRAGMENT
:
864 glDisable(GL_FRAGMENT_PROGRAM
);
870 * \brief draw a texture part at given 2D coordinates
871 * \param x screen top coordinate
872 * \param y screen left coordinate
873 * \param w screen width coordinate
874 * \param h screen height coordinate
875 * \param tx texture top coordinate in pixels
876 * \param ty texture left coordinate in pixels
877 * \param tw texture part width in pixels
878 * \param th texture part height in pixels
879 * \param sx width of texture in pixels
880 * \param sy height of texture in pixels
881 * \param rect_tex whether this texture uses texture_rectangle extension
882 * \param is_yv12 if set, also draw the textures from units 1 and 2
883 * \param flip flip the texture upside down
886 void glDrawTex(GLfloat x
, GLfloat y
, GLfloat w
, GLfloat h
,
887 GLfloat tx
, GLfloat ty
, GLfloat tw
, GLfloat th
,
888 int sx
, int sy
, int rect_tex
, int is_yv12
, int flip
) {
889 GLfloat tx2
= tx
/ 2, ty2
= ty
/ 2, tw2
= tw
/ 2, th2
= th
/ 2;
891 tx
/= sx
; ty
/= sy
; tw
/= sx
; th
/= sy
;
892 tx2
= tx
, ty2
= ty
, tw2
= tw
, th2
= th
;
899 glTexCoord2f(tx
, ty
);
901 MultiTexCoord2f(GL_TEXTURE1
, tx2
, ty2
);
902 MultiTexCoord2f(GL_TEXTURE2
, tx2
, ty2
);
905 glTexCoord2f(tx
, ty
+ th
);
907 MultiTexCoord2f(GL_TEXTURE1
, tx2
, ty2
+ th2
);
908 MultiTexCoord2f(GL_TEXTURE2
, tx2
, ty2
+ th2
);
910 glVertex2f(x
, y
+ h
);
911 glTexCoord2f(tx
+ tw
, ty
+ th
);
913 MultiTexCoord2f(GL_TEXTURE1
, tx2
+ tw2
, ty2
+ th2
);
914 MultiTexCoord2f(GL_TEXTURE2
, tx2
+ tw2
, ty2
+ th2
);
916 glVertex2f(x
+ w
, y
+ h
);
917 glTexCoord2f(tx
+ tw
, ty
);
919 MultiTexCoord2f(GL_TEXTURE1
, tx2
+ tw2
, ty2
);
920 MultiTexCoord2f(GL_TEXTURE2
, tx2
+ tw2
, ty2
);
922 glVertex2f(x
+ w
, y
);
927 #include "w32_common.h"
929 * \brief little helper since wglGetProcAddress definition does not fit our
931 * \param procName name of function to look up
932 * \return function pointer returned by wglGetProcAddress
934 static void *w32gpa(const GLubyte
*procName
) {
935 return wglGetProcAddress(procName
);
938 int setGlWindow(int *vinfo
, HGLRC
*context
, HWND win
)
941 HDC windc
= GetDC(win
);
942 HGLRC new_context
= 0;
943 int keep_context
= 0;
945 // should only be needed when keeping context, but not doing glFinish
946 // can cause flickering even when we do not keep it.
949 new_vinfo
= GetPixelFormat(windc
);
950 if (*context
&& *vinfo
&& new_vinfo
&& *vinfo
== new_vinfo
) {
951 // we can keep the wglContext
952 new_context
= *context
;
956 new_context
= wglCreateContext(windc
);
958 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Could not create GL context!\n");
959 return SET_WINDOW_FAILED
;
964 if (!wglMakeCurrent(windc
, new_context
)) {
965 mp_msg (MSGT_VO
, MSGL_FATAL
, "[gl] Could not set GL context!\n");
967 wglDeleteContext(new_context
);
969 return SET_WINDOW_FAILED
;
977 GetClientRect(win
, &rect
);
978 vo_dwidth
= rect
.right
;
979 vo_dheight
= rect
.bottom
;
983 wglDeleteContext(*context
);
984 *context
= new_context
;
986 getFunctions(w32gpa
, NULL
);
988 // and inform that reinit is neccessary
989 return SET_WINDOW_REINIT
;
991 return SET_WINDOW_OK
;
994 void releaseGlContext(int *vinfo
, HGLRC
*context
) {
997 wglMakeCurrent(0, 0);
998 wglDeleteContext(*context
);
1003 void swapGlBuffers() {
1004 SwapBuffers(vo_hdc
);
1010 #include "x11_common.h"
1012 * \brief find address of a linked function
1013 * \param s name of function to find
1014 * \return address of function or NULL if not found
1018 static void *getdladdr(const char *s
) {
1020 #if defined(__sun) || defined(__sgi)
1021 static void *handle
= NULL
;
1023 handle
= dlopen(NULL
, RTLD_LAZY
);
1024 return dlsym(handle
, s
);
1034 * \brief Returns the XVisualInfo associated with Window win.
1035 * \param win Window whose XVisualInfo is returne.
1036 * \return XVisualInfo of the window. Caller must use XFree to free it.
1038 static XVisualInfo
*getWindowVisualInfo(Window win
) {
1039 XWindowAttributes xw_attr
;
1040 XVisualInfo vinfo_template
;
1042 XGetWindowAttributes(mDisplay
, win
, &xw_attr
);
1043 vinfo_template
.visualid
= XVisualIDFromVisual(xw_attr
.visual
);
1044 return XGetVisualInfo(mDisplay
, VisualIDMask
, &vinfo_template
, &tmp
);
1048 * \brief Changes the window in which video is displayed.
1049 * If possible only transfers the context to the new window, otherwise
1050 * creates a new one, which must be initialized by the caller.
1051 * \param vinfo Currently used visual.
1052 * \param context Currently used context.
1053 * \param win window that should be used for drawing.
1054 * \return one of SET_WINDOW_FAILED, SET_WINDOW_OK or SET_WINDOW_REINIT.
1055 * In case of SET_WINDOW_REINIT the context could not be transfered
1056 * and the caller must initialize it correctly.
1057 * \ingroup glcontext
1059 int setGlWindow(XVisualInfo
**vinfo
, GLXContext
*context
, Window win
)
1061 XVisualInfo
*new_vinfo
;
1062 GLXContext new_context
= NULL
;
1063 int keep_context
= 0;
1065 // should only be needed when keeping context, but not doing glFinish
1066 // can cause flickering even when we do not keep it.
1069 new_vinfo
= getWindowVisualInfo(win
);
1070 if (*context
&& *vinfo
&& new_vinfo
&&
1071 (*vinfo
)->visualid
== new_vinfo
->visualid
) {
1072 // we can keep the GLXContext
1073 new_context
= *context
;
1079 new_context
= glXCreateContext(mDisplay
, new_vinfo
, NULL
, True
);
1081 mp_msg(MSGT_VO
, MSGL_FATAL
, "[gl] Could not create GLX context!\n");
1083 return SET_WINDOW_FAILED
;
1088 if (!glXMakeCurrent(mDisplay
, vo_window
, new_context
)) {
1089 mp_msg (MSGT_VO
, MSGL_FATAL
, "[gl] Could not set GLX context!\n");
1090 if (!keep_context
) {
1091 glXDestroyContext (mDisplay
, new_context
);
1094 return SET_WINDOW_FAILED
;
1103 XGetGeometry(mDisplay
, vo_window
, &root
, &tmp
, &tmp
,
1104 (unsigned *)&vo_dwidth
, (unsigned *)&vo_dheight
, &utmp
, &utmp
);
1106 if (!keep_context
) {
1107 void *(*getProcAddress
)(const GLubyte
*);
1108 const char *(*glXExtStr
)(Display
*, int);
1110 glXDestroyContext(mDisplay
, *context
);
1111 *context
= new_context
;
1115 getProcAddress
= getdladdr("glXGetProcAddress");
1116 if (!getProcAddress
)
1117 getProcAddress
= getdladdr("glXGetProcAddressARB");
1118 if (!getProcAddress
)
1119 getProcAddress
= (void *)getdladdr
;
1120 glXExtStr
= getdladdr("glXQueryExtensionsString");
1121 getFunctions(getProcAddress
, !glXExtStr
? NULL
:
1122 glXExtStr(mDisplay
, DefaultScreen(mDisplay
)));
1124 // and inform that reinit is neccessary
1125 return SET_WINDOW_REINIT
;
1127 return SET_WINDOW_OK
;
1131 * \brief free the VisualInfo and GLXContext of an OpenGL context.
1132 * \ingroup glcontext
1134 void releaseGlContext(XVisualInfo
**vinfo
, GLXContext
*context
) {
1141 glXMakeCurrent(mDisplay
, None
, NULL
);
1142 glXDestroyContext(mDisplay
, *context
);
1147 void swapGlBuffers(void) {
1148 glXSwapBuffers(mDisplay
, vo_window
);