Merge svn changes up to r28087
[mplayer.git] / libvo / gl_common.c
blob0df3e2e74ae8863dd2acd326579daee589de7b7e
1 /*
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.
25 /**
26 * \file gl_common.c
27 * \brief OpenGL helper functions used by vo_gl.c and vo_gl2.c
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <ctype.h>
34 #include <math.h>
35 #include "old_vo_defines.h"
36 #include "gl_common.h"
37 #include "libavutil/common.h"
39 /**
40 * \defgroup glextfunctions OpenGL extension functions
42 * the pointers to these functions are acquired when the OpenGL
43 * context is created
44 * \{
46 void (APIENTRY *GenBuffers)(GLsizei, GLuint *);
47 void (APIENTRY *DeleteBuffers)(GLsizei, const GLuint *);
48 void (APIENTRY *BindBuffer)(GLenum, GLuint);
49 GLvoid* (APIENTRY *MapBuffer)(GLenum, GLenum);
50 GLboolean (APIENTRY *UnmapBuffer)(GLenum);
51 void (APIENTRY *BufferData)(GLenum, intptr_t, const GLvoid *, GLenum);
52 void (APIENTRY *CombinerParameterfv)(GLenum, const GLfloat *);
53 void (APIENTRY *CombinerParameteri)(GLenum, GLint);
54 void (APIENTRY *CombinerInput)(GLenum, GLenum, GLenum, GLenum, GLenum,
55 GLenum);
56 void (APIENTRY *CombinerOutput)(GLenum, GLenum, GLenum, GLenum, GLenum,
57 GLenum, GLenum, GLboolean, GLboolean,
58 GLboolean);
59 void (APIENTRY *BeginFragmentShader)(void);
60 void (APIENTRY *EndFragmentShader)(void);
61 void (APIENTRY *SampleMap)(GLuint, GLuint, GLenum);
62 void (APIENTRY *ColorFragmentOp2)(GLenum, GLuint, GLuint, GLuint, GLuint,
63 GLuint, GLuint, GLuint, GLuint, GLuint);
64 void (APIENTRY *ColorFragmentOp3)(GLenum, GLuint, GLuint, GLuint, GLuint,
65 GLuint, GLuint, GLuint, GLuint, GLuint,
66 GLuint, GLuint, GLuint);
67 void (APIENTRY *SetFragmentShaderConstant)(GLuint, const GLfloat *);
68 void (APIENTRY *ActiveTexture)(GLenum);
69 void (APIENTRY *BindTexture)(GLenum, GLuint);
70 void (APIENTRY *MultiTexCoord2f)(GLenum, GLfloat, GLfloat);
71 void (APIENTRY *GenPrograms)(GLsizei, GLuint *);
72 void (APIENTRY *DeletePrograms)(GLsizei, const GLuint *);
73 void (APIENTRY *BindProgram)(GLenum, GLuint);
74 void (APIENTRY *ProgramString)(GLenum, GLenum, GLsizei, const GLvoid *);
75 void (APIENTRY *GetProgramiv)(GLenum, GLenum, GLint *);
76 void (APIENTRY *ProgramEnvParameter4f)(GLenum, GLuint, GLfloat, GLfloat,
77 GLfloat, GLfloat);
78 int (APIENTRY *SwapInterval)(int);
79 void (APIENTRY *TexImage3D)(GLenum, GLint, GLenum, GLsizei, GLsizei, GLsizei,
80 GLint, GLenum, GLenum, const GLvoid *);
81 /** \} */ // end of glextfunctions group
83 //! \defgroup glgeneral OpenGL general helper functions
85 //! \defgroup glcontext OpenGL context management helper functions
87 //! \defgroup gltexture OpenGL texture handling helper functions
89 //! \defgroup glconversion OpenGL conversion helper functions
91 static GLint hqtexfmt;
93 /**
94 * \brief adjusts the GL_UNPACK_ALIGNMENT to fit the stride.
95 * \param stride number of bytes per line for which alignment should fit.
96 * \ingroup glgeneral
98 void glAdjustAlignment(int stride) {
99 GLint gl_alignment;
100 if (stride % 8 == 0)
101 gl_alignment=8;
102 else if (stride % 4 == 0)
103 gl_alignment=4;
104 else if (stride % 2 == 0)
105 gl_alignment=2;
106 else
107 gl_alignment=1;
108 glPixelStorei (GL_UNPACK_ALIGNMENT, gl_alignment);
111 struct gl_name_map_struct {
112 GLint value;
113 const char *name;
116 #undef MAP
117 #define MAP(a) {a, #a}
118 //! mapping table for the glValName function
119 static const struct gl_name_map_struct gl_name_map[] = {
120 // internal format
121 MAP(GL_R3_G3_B2), MAP(GL_RGB4), MAP(GL_RGB5), MAP(GL_RGB8),
122 MAP(GL_RGB10), MAP(GL_RGB12), MAP(GL_RGB16), MAP(GL_RGBA2),
123 MAP(GL_RGBA4), MAP(GL_RGB5_A1), MAP(GL_RGBA8), MAP(GL_RGB10_A2),
124 MAP(GL_RGBA12), MAP(GL_RGBA16), MAP(GL_LUMINANCE8),
126 // format
127 MAP(GL_RGB), MAP(GL_RGBA), MAP(GL_RED), MAP(GL_GREEN), MAP(GL_BLUE),
128 MAP(GL_ALPHA), MAP(GL_LUMINANCE), MAP(GL_LUMINANCE_ALPHA),
129 MAP(GL_COLOR_INDEX),
130 // rest 1.2 only
131 MAP(GL_BGR), MAP(GL_BGRA),
133 //type
134 MAP(GL_BYTE), MAP(GL_UNSIGNED_BYTE), MAP(GL_SHORT), MAP(GL_UNSIGNED_SHORT),
135 MAP(GL_INT), MAP(GL_UNSIGNED_INT), MAP(GL_FLOAT), MAP(GL_DOUBLE),
136 MAP(GL_2_BYTES), MAP(GL_3_BYTES), MAP(GL_4_BYTES),
137 // rest 1.2 only
138 MAP(GL_UNSIGNED_BYTE_3_3_2), MAP(GL_UNSIGNED_BYTE_2_3_3_REV),
139 MAP(GL_UNSIGNED_SHORT_5_6_5), MAP(GL_UNSIGNED_SHORT_5_6_5_REV),
140 MAP(GL_UNSIGNED_SHORT_4_4_4_4), MAP(GL_UNSIGNED_SHORT_4_4_4_4_REV),
141 MAP(GL_UNSIGNED_SHORT_5_5_5_1), MAP(GL_UNSIGNED_SHORT_1_5_5_5_REV),
142 MAP(GL_UNSIGNED_INT_8_8_8_8), MAP(GL_UNSIGNED_INT_8_8_8_8_REV),
143 MAP(GL_UNSIGNED_INT_10_10_10_2), MAP(GL_UNSIGNED_INT_2_10_10_10_REV),
144 {0, 0}
146 #undef MAP
149 * \brief return the name of an OpenGL constant
150 * \param value the constant
151 * \return name of the constant or "Unknown format!"
152 * \ingroup glgeneral
154 const char *glValName(GLint value)
156 int i = 0;
158 while (gl_name_map[i].name) {
159 if (gl_name_map[i].value == value)
160 return gl_name_map[i].name;
161 i++;
163 return "Unknown format!";
166 //! always return this format as internal texture format in glFindFormat
167 #define TEXTUREFORMAT_ALWAYS GL_RGB8
168 #undef TEXTUREFORMAT_ALWAYS
171 * \brief find the OpenGL settings coresponding to format.
173 * All parameters may be NULL.
174 * \param fmt MPlayer format to analyze.
175 * \param bpp [OUT] bits per pixel of that format.
176 * \param gl_texfmt [OUT] internal texture format that fits the
177 * image format, not necessarily the best for performance.
178 * \param gl_format [OUT] OpenGL format for this image format.
179 * \param gl_type [OUT] OpenGL type for this image format.
180 * \return 1 if format is supported by OpenGL, 0 if not.
181 * \ingroup gltexture
183 int glFindFormat(uint32_t fmt, int *bpp, GLint *gl_texfmt,
184 GLenum *gl_format, GLenum *gl_type)
186 int supported = 1;
187 int dummy1;
188 GLenum dummy2;
189 GLint dummy3;
190 if (!bpp) bpp = &dummy1;
191 if (!gl_texfmt) gl_texfmt = &dummy3;
192 if (!gl_format) gl_format = &dummy2;
193 if (!gl_type) gl_type = &dummy2;
195 *bpp = IMGFMT_IS_BGR(fmt)?IMGFMT_BGR_DEPTH(fmt):IMGFMT_RGB_DEPTH(fmt);
196 *gl_texfmt = 3;
197 switch (fmt) {
198 case IMGFMT_RGB24:
199 *gl_format = GL_RGB;
200 *gl_type = GL_UNSIGNED_BYTE;
201 break;
202 case IMGFMT_RGBA:
203 *gl_texfmt = 4;
204 *gl_format = GL_RGBA;
205 *gl_type = GL_UNSIGNED_BYTE;
206 break;
207 case IMGFMT_YV12:
208 supported = 0; // no native YV12 support
209 case IMGFMT_Y800:
210 case IMGFMT_Y8:
211 *gl_texfmt = 1;
212 *bpp = 8;
213 *gl_format = GL_LUMINANCE;
214 *gl_type = GL_UNSIGNED_BYTE;
215 break;
216 #if 0
217 // we do not support palettized formats, although the format the
218 // swscale produces works
219 case IMGFMT_RGB8:
220 gl_format = GL_RGB;
221 gl_type = GL_UNSIGNED_BYTE_2_3_3_REV;
222 break;
223 #endif
224 case IMGFMT_RGB15:
225 *gl_format = GL_RGBA;
226 *gl_type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
227 break;
228 case IMGFMT_RGB16:
229 *gl_format = GL_RGB;
230 *gl_type = GL_UNSIGNED_SHORT_5_6_5_REV;
231 break;
232 #if 0
233 case IMGFMT_BGR8:
234 // special case as red and blue have a differen number of bits.
235 // GL_BGR and GL_UNSIGNED_BYTE_3_3_2 isn't supported at least
236 // by nVidia drivers, and in addition would give more bits to
237 // blue than to red, which isn't wanted
238 gl_format = GL_RGB;
239 gl_type = GL_UNSIGNED_BYTE_3_3_2;
240 break;
241 #endif
242 case IMGFMT_BGR15:
243 *gl_format = GL_BGRA;
244 *gl_type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
245 break;
246 case IMGFMT_BGR16:
247 *gl_format = GL_RGB;
248 *gl_type = GL_UNSIGNED_SHORT_5_6_5;
249 break;
250 case IMGFMT_BGR24:
251 *gl_format = GL_BGR;
252 *gl_type = GL_UNSIGNED_BYTE;
253 break;
254 case IMGFMT_BGRA:
255 *gl_texfmt = 4;
256 *gl_format = GL_BGRA;
257 *gl_type = GL_UNSIGNED_BYTE;
258 break;
259 default:
260 *gl_texfmt = 4;
261 *gl_format = GL_RGBA;
262 *gl_type = GL_UNSIGNED_BYTE;
263 supported = 0;
265 #ifdef TEXTUREFORMAT_ALWAYS
266 *gl_texfmt = TEXTUREFORMAT_ALWAYS;
267 #endif
268 return supported;
271 static void *setNull(const GLubyte *s) {
272 return NULL;
275 typedef struct {
276 void *funcptr;
277 const char *extstr;
278 const char *funcnames[7];
279 } extfunc_desc_t;
281 static const extfunc_desc_t extfuncs[] = {
282 {&GenBuffers, NULL, {"glGenBuffers", "glGenBuffersARB", NULL}},
283 {&DeleteBuffers, NULL, {"glDeleteBuffers", "glDeleteBuffersARB", NULL}},
284 {&BindBuffer, NULL, {"glBindBuffer", "glBindBufferARB", NULL}},
285 {&MapBuffer, NULL, {"glMapBuffer", "glMapBufferARB", NULL}},
286 {&UnmapBuffer, NULL, {"glUnmapBuffer", "glUnmapBufferARB", NULL}},
287 {&BufferData, NULL, {"glBufferData", "glBufferDataARB", NULL}},
288 {&CombinerParameterfv, "NV_register_combiners", {"glCombinerParameterfv", "glCombinerParameterfvNV", NULL}},
289 {&CombinerParameteri, "NV_register_combiners", {"glCombinerParameteri", "glCombinerParameteriNV", NULL}},
290 {&CombinerInput, "NV_register_combiners", {"glCombinerInput", "glCombinerInputNV", NULL}},
291 {&CombinerOutput, "NV_register_combiners", {"glCombinerOutput", "glCombinerOutputNV", NULL}},
292 {&BeginFragmentShader, "ATI_fragment_shader", {"glBeginFragmentShaderATI", NULL}},
293 {&EndFragmentShader, "ATI_fragment_shader", {"glEndFragmentShaderATI", NULL}},
294 {&SampleMap, "ATI_fragment_shader", {"glSampleMapATI", NULL}},
295 {&ColorFragmentOp2, "ATI_fragment_shader", {"glColorFragmentOp2ATI", NULL}},
296 {&ColorFragmentOp3, "ATI_fragment_shader", {"glColorFragmentOp3ATI", NULL}},
297 {&SetFragmentShaderConstant, "ATI_fragment_shader", {"glSetFragmentShaderConstantATI", NULL}},
298 {&ActiveTexture, NULL, {"glActiveTexture", "glActiveTextureARB", NULL}},
299 {&BindTexture, NULL, {"glBindTexture", "glBindTextureARB", "glBindTextureEXT", NULL}},
300 {&MultiTexCoord2f, NULL, {"glMultiTexCoord2f", "glMultiTexCoord2fARB", NULL}},
301 {&GenPrograms, "_program", {"glGenProgramsARB", NULL}},
302 {&DeletePrograms, "_program", {"glDeleteProgramsARB", NULL}},
303 {&BindProgram, "_program", {"glBindProgramARB", NULL}},
304 {&ProgramString, "_program", {"glProgramStringARB", NULL}},
305 {&GetProgramiv, "_program", {"glGetProgramivARB", NULL}},
306 {&ProgramEnvParameter4f, "_program", {"glProgramEnvParameter4fARB", NULL}},
307 {&SwapInterval, "_swap_control", {"glXSwapInterval", "glXSwapIntervalEXT", "glXSwapIntervalSGI", "wglSwapInterval", "wglSwapIntervalEXT", "wglSwapIntervalSGI", NULL}},
308 {&TexImage3D, NULL, {"glTexImage3D", NULL}},
309 {NULL}
313 * \brief find the function pointers of some useful OpenGL extensions
314 * \param getProcAddress function to resolve function names, may be NULL
315 * \param ext2 an extra extension string
317 static void getFunctions(void *(*getProcAddress)(const GLubyte *),
318 const char *ext2) {
319 const extfunc_desc_t *dsc;
320 const char *extensions = (const char *)glGetString(GL_EXTENSIONS);
321 char *allexts;
322 if (!extensions) extensions = "";
323 if (!ext2) ext2 = "";
324 allexts = malloc(strlen(extensions) + strlen(ext2) + 2);
325 strcpy(allexts, extensions);
326 strcat(allexts, " ");
327 strcat(allexts, ext2);
328 mp_msg(MSGT_VO, MSGL_V, "OpenGL extensions string:\n%s\n", allexts);
329 if (!getProcAddress)
330 getProcAddress = setNull;
331 for (dsc = extfuncs; dsc->funcptr; dsc++) {
332 void *ptr = NULL;
333 int i;
334 if (!dsc->extstr || strstr(allexts, dsc->extstr)) {
335 for (i = 0; !ptr && dsc->funcnames[i]; i++)
336 ptr = getProcAddress((const GLubyte *)dsc->funcnames[i]);
338 *(void **)dsc->funcptr = ptr;
340 if (strstr(allexts, "_texture_float"))
341 hqtexfmt = GL_RGB32F;
342 else if (strstr(allexts, "NV_float_buffer"))
343 hqtexfmt = GL_FLOAT_RGB32_NV;
344 else
345 hqtexfmt = GL_RGB16;
346 free(allexts);
350 * \brief create a texture and set some defaults
351 * \param target texture taget, usually GL_TEXTURE_2D
352 * \param fmt internal texture format
353 * \param format texture host data format
354 * \param type texture host data type
355 * \param filter filter used for scaling, e.g. GL_LINEAR
356 * \param w texture width
357 * \param h texture height
358 * \param val luminance value to fill texture with
359 * \ingroup gltexture
361 void glCreateClearTex(GLenum target, GLenum fmt, GLenum format, GLenum type, GLint filter,
362 int w, int h, unsigned char val) {
363 GLfloat fval = (GLfloat)val / 255.0;
364 GLfloat border[4] = {fval, fval, fval, fval};
365 int stride = w * glFmt2bpp(format, type);
366 char *init;
367 if (!stride) return;
368 init = malloc(stride * h);
369 memset(init, val, stride * h);
370 glAdjustAlignment(stride);
371 glPixelStorei(GL_UNPACK_ROW_LENGTH, w);
372 glTexImage2D(target, 0, fmt, w, h, 0, format, type, init);
373 glTexParameterf(target, GL_TEXTURE_PRIORITY, 1.0);
374 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
375 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
376 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
377 glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
378 // Border texels should not be used with CLAMP_TO_EDGE
379 // We set a sane default anyway.
380 glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, border);
381 free(init);
385 * \brief skips whitespace and comments
386 * \param f file to read from
388 static void ppm_skip(FILE *f) {
389 int c, comment = 0;
390 do {
391 c = fgetc(f);
392 if (c == '#')
393 comment = 1;
394 if (c == '\n')
395 comment = 0;
396 } while (c != EOF && (isspace(c) || comment));
397 if (c != EOF)
398 ungetc(c, f);
401 #define MAXDIM (16 * 1024)
404 * \brief creates a texture from a PPM file
405 * \param target texture taget, usually GL_TEXTURE_2D
406 * \param fmt internal texture format, 0 for default
407 * \param filter filter used for scaling, e.g. GL_LINEAR
408 * \param f file to read PPM from
409 * \param width [out] width of texture
410 * \param height [out] height of texture
411 * \param maxval [out] maxval value from PPM file
412 * \return 0 on error, 1 otherwise
413 * \ingroup gltexture
415 int glCreatePPMTex(GLenum target, GLenum fmt, GLint filter,
416 FILE *f, int *width, int *height, int *maxval) {
417 unsigned w, h, m, val, bpp;
418 char *data;
419 GLenum type;
420 ppm_skip(f);
421 if (fgetc(f) != 'P' || fgetc(f) != '6')
422 return 0;
423 ppm_skip(f);
424 if (fscanf(f, "%u", &w) != 1)
425 return 0;
426 ppm_skip(f);
427 if (fscanf(f, "%u", &h) != 1)
428 return 0;
429 ppm_skip(f);
430 if (fscanf(f, "%u", &m) != 1)
431 return 0;
432 val = fgetc(f);
433 if (!isspace(val))
434 return 0;
435 if (w > MAXDIM || h > MAXDIM)
436 return 0;
437 bpp = (m > 255) ? 6 : 3;
438 data = malloc(w * h * bpp);
439 if (fread(data, w * bpp, h, f) != h)
440 return 0;
441 if (!fmt) {
442 fmt = (m > 255) ? hqtexfmt : 3;
443 if (fmt == GL_FLOAT_RGB32_NV && target != GL_TEXTURE_RECTANGLE)
444 fmt = GL_RGB16;
446 type = m > 255 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_BYTE;
447 glCreateClearTex(target, fmt, GL_RGB, type, filter, w, h, 0);
448 glUploadTex(target, GL_RGB, type,
449 data, w * bpp, 0, 0, w, h, 0);
450 free(data);
451 if (width) *width = w;
452 if (height) *height = h;
453 if (maxval) *maxval = m;
454 return 1;
458 * \brief return the number of bytes per pixel for the given format
459 * \param format OpenGL format
460 * \param type OpenGL type
461 * \return bytes per pixel
462 * \ingroup glgeneral
464 * Does not handle all possible variants, just those used by MPlayer
466 int glFmt2bpp(GLenum format, GLenum type) {
467 int component_size = 0;
468 switch (type) {
469 case GL_UNSIGNED_BYTE_3_3_2:
470 case GL_UNSIGNED_BYTE_2_3_3_REV:
471 return 1;
472 case GL_UNSIGNED_SHORT_5_5_5_1:
473 case GL_UNSIGNED_SHORT_1_5_5_5_REV:
474 case GL_UNSIGNED_SHORT_5_6_5:
475 case GL_UNSIGNED_SHORT_5_6_5_REV:
476 return 2;
477 case GL_UNSIGNED_BYTE:
478 component_size = 1;
479 break;
480 case GL_UNSIGNED_SHORT:
481 component_size = 2;
482 break;
484 switch (format) {
485 case GL_LUMINANCE:
486 case GL_ALPHA:
487 return component_size;
488 case GL_RGB:
489 case GL_BGR:
490 return 3 * component_size;
491 case GL_RGBA:
492 case GL_BGRA:
493 return 4 * component_size;
495 return 0; // unknown
499 * \brief upload a texture, handling things like stride and slices
500 * \param target texture target, usually GL_TEXTURE_2D
501 * \param format OpenGL format of data
502 * \param type OpenGL type of data
503 * \param dataptr data to upload
504 * \param stride data stride
505 * \param x x offset in texture
506 * \param y y offset in texture
507 * \param w width of the texture part to upload
508 * \param h height of the texture part to upload
509 * \param slice height of an upload slice, 0 for all at once
510 * \ingroup gltexture
512 void glUploadTex(GLenum target, GLenum format, GLenum type,
513 const void *dataptr, int stride,
514 int x, int y, int w, int h, int slice) {
515 const uint8_t *data = dataptr;
516 int y_max = y + h;
517 if (w <= 0 || h <= 0) return;
518 if (slice <= 0)
519 slice = h;
520 if (stride < 0) {
521 data += (h - 1) * stride;
522 stride = -stride;
524 // this is not always correct, but should work for MPlayer
525 glAdjustAlignment(stride);
526 glPixelStorei(GL_UNPACK_ROW_LENGTH, stride / glFmt2bpp(format, type));
527 for (; y + slice <= y_max; y += slice) {
528 glTexSubImage2D(target, 0, x, y, w, slice, format, type, data);
529 data += stride * slice;
531 if (y < y_max)
532 glTexSubImage2D(target, 0, x, y, w, y_max - y, format, type, data);
535 static void fillUVcoeff(GLfloat *ucoef, GLfloat *vcoef,
536 float uvcos, float uvsin) {
537 int i;
538 ucoef[0] = 0 * uvcos + 1.403 * uvsin;
539 vcoef[0] = 0 * uvsin + 1.403 * uvcos;
540 ucoef[1] = -0.344 * uvcos + -0.714 * uvsin;
541 vcoef[1] = -0.344 * uvsin + -0.714 * uvcos;
542 ucoef[2] = 1.770 * uvcos + 0 * uvsin;
543 vcoef[2] = 1.770 * uvsin + 0 * uvcos;
544 ucoef[3] = 0;
545 vcoef[3] = 0;
546 // Coefficients (probably) must be in [0, 1] range, whereas they originally
547 // are in [-2, 2] range, so here comes the trick:
548 // First put them in the [-0.5, 0.5] range, then add 0.5.
549 // This can be undone with the HALF_BIAS and SCALE_BY_FOUR arguments
550 // for CombinerInput and CombinerOutput (or the respective ATI variants)
551 for (i = 0; i < 4; i++) {
552 ucoef[i] = ucoef[i] * 0.25 + 0.5;
553 vcoef[i] = vcoef[i] * 0.25 + 0.5;
558 * \brief Setup register combiners for YUV to RGB conversion.
559 * \param uvcos used for saturation and hue adjustment
560 * \param uvsin used for saturation and hue adjustment
562 static void glSetupYUVCombiners(float uvcos, float uvsin) {
563 GLfloat ucoef[4];
564 GLfloat vcoef[4];
565 GLint i;
566 if (!CombinerInput || !CombinerOutput ||
567 !CombinerParameterfv || !CombinerParameteri) {
568 mp_msg(MSGT_VO, MSGL_FATAL, "[gl] Combiner functions missing!\n");
569 return;
571 glGetIntegerv(GL_MAX_GENERAL_COMBINERS_NV, &i);
572 if (i < 2)
573 mp_msg(MSGT_VO, MSGL_ERR,
574 "[gl] 2 general combiners needed for YUV combiner support (found %i)\n", i);
575 glGetIntegerv(GL_MAX_TEXTURE_UNITS, &i);
576 if (i < 3)
577 mp_msg(MSGT_VO, MSGL_ERR,
578 "[gl] 3 texture units needed for YUV combiner support (found %i)\n", i);
579 fillUVcoeff(ucoef, vcoef, uvcos, uvsin);
580 CombinerParameterfv(GL_CONSTANT_COLOR0_NV, ucoef);
581 CombinerParameterfv(GL_CONSTANT_COLOR1_NV, vcoef);
583 // UV first, like this green component cannot overflow
584 CombinerInput(GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_A_NV,
585 GL_TEXTURE1, GL_HALF_BIAS_NORMAL_NV, GL_RGB);
586 CombinerInput(GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_B_NV,
587 GL_CONSTANT_COLOR0_NV, GL_HALF_BIAS_NORMAL_NV, GL_RGB);
588 CombinerInput(GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_C_NV,
589 GL_TEXTURE2, GL_HALF_BIAS_NORMAL_NV, GL_RGB);
590 CombinerInput(GL_COMBINER0_NV, GL_RGB, GL_VARIABLE_D_NV,
591 GL_CONSTANT_COLOR1_NV, GL_HALF_BIAS_NORMAL_NV, GL_RGB);
592 CombinerOutput(GL_COMBINER0_NV, GL_RGB, GL_DISCARD_NV, GL_DISCARD_NV,
593 GL_SPARE0_NV, GL_SCALE_BY_FOUR_NV, GL_NONE, GL_FALSE,
594 GL_FALSE, GL_FALSE);
596 // stage 2
597 CombinerInput(GL_COMBINER1_NV, GL_RGB, GL_VARIABLE_A_NV, GL_SPARE0_NV,
598 GL_SIGNED_IDENTITY_NV, GL_RGB);
599 CombinerInput(GL_COMBINER1_NV, GL_RGB, GL_VARIABLE_B_NV, GL_ZERO,
600 GL_UNSIGNED_INVERT_NV, GL_RGB);
601 CombinerInput(GL_COMBINER1_NV, GL_RGB, GL_VARIABLE_C_NV,
602 GL_TEXTURE0, GL_SIGNED_IDENTITY_NV, GL_RGB);
603 CombinerInput(GL_COMBINER1_NV, GL_RGB, GL_VARIABLE_D_NV, GL_ZERO,
604 GL_UNSIGNED_INVERT_NV, GL_RGB);
605 CombinerOutput(GL_COMBINER1_NV, GL_RGB, GL_DISCARD_NV, GL_DISCARD_NV,
606 GL_SPARE0_NV, GL_NONE, GL_NONE, GL_FALSE,
607 GL_FALSE, GL_FALSE);
609 // leave final combiner stage in default mode
610 CombinerParameteri(GL_NUM_GENERAL_COMBINERS_NV, 2);
614 * \brief Setup ATI version of register combiners for YUV to RGB conversion.
615 * \param uvcos used for saturation and hue adjustment
616 * \param uvsin used for saturation and hue adjustment
618 * ATI called this fragment shader, but the name is confusing in the
619 * light of a very different OpenGL 2.0 extension with the same name
621 static void glSetupYUVCombinersATI(float uvcos, float uvsin) {
622 GLfloat ucoef[4];
623 GLfloat vcoef[4];
624 GLint i;
625 if (!BeginFragmentShader || !EndFragmentShader ||
626 !SetFragmentShaderConstant || !SampleMap ||
627 !ColorFragmentOp2 || !ColorFragmentOp3) {
628 mp_msg(MSGT_VO, MSGL_FATAL, "[gl] Combiner (ATI) functions missing!\n");
629 return;
631 glGetIntegerv(GL_NUM_FRAGMENT_REGISTERS_ATI, &i);
632 if (i < 3)
633 mp_msg(MSGT_VO, MSGL_ERR,
634 "[gl] 3 registers needed for YUV combiner (ATI) support (found %i)\n", i);
635 glGetIntegerv (GL_MAX_TEXTURE_UNITS, &i);
636 if (i < 3)
637 mp_msg(MSGT_VO, MSGL_ERR,
638 "[gl] 3 texture units needed for YUV combiner (ATI) support (found %i)\n", i);
639 fillUVcoeff(ucoef, vcoef, uvcos, uvsin);
640 BeginFragmentShader();
641 SetFragmentShaderConstant(GL_CON_0_ATI, ucoef);
642 SetFragmentShaderConstant(GL_CON_1_ATI, vcoef);
643 SampleMap(GL_REG_0_ATI, GL_TEXTURE0, GL_SWIZZLE_STR_ATI);
644 SampleMap(GL_REG_1_ATI, GL_TEXTURE1, GL_SWIZZLE_STR_ATI);
645 SampleMap(GL_REG_2_ATI, GL_TEXTURE2, GL_SWIZZLE_STR_ATI);
646 // UV first, like this green component cannot overflow
647 ColorFragmentOp2(GL_MUL_ATI, GL_REG_1_ATI, GL_NONE, GL_NONE,
648 GL_REG_1_ATI, GL_NONE, GL_BIAS_BIT_ATI,
649 GL_CON_0_ATI, GL_NONE, GL_BIAS_BIT_ATI);
650 ColorFragmentOp3(GL_MAD_ATI, GL_REG_2_ATI, GL_NONE, GL_4X_BIT_ATI,
651 GL_REG_2_ATI, GL_NONE, GL_BIAS_BIT_ATI,
652 GL_CON_1_ATI, GL_NONE, GL_BIAS_BIT_ATI,
653 GL_REG_1_ATI, GL_NONE, GL_NONE);
654 ColorFragmentOp2(GL_ADD_ATI, GL_REG_0_ATI, GL_NONE, GL_NONE,
655 GL_REG_0_ATI, GL_NONE, GL_NONE,
656 GL_REG_2_ATI, GL_NONE, GL_NONE);
657 EndFragmentShader();
661 * \brief helper function for gen_spline_lookup_tex
662 * \param x subpixel-position ((0,1) range) to calculate weights for
663 * \param dst where to store transformed weights, must provide space for 4 GLfloats
665 * calculates the weights and stores them after appropriate transformation
666 * for the scaler fragment program.
668 static void store_weights(float x, GLfloat *dst) {
669 float w0 = (((-1 * x + 3) * x - 3) * x + 1) / 6;
670 float w1 = ((( 3 * x - 6) * x + 0) * x + 4) / 6;
671 float w2 = (((-3 * x + 3) * x + 3) * x + 1) / 6;
672 float w3 = ((( 1 * x + 0) * x + 0) * x + 0) / 6;
673 *dst++ = 1 + x - w1 / (w0 + w1);
674 *dst++ = 1 - x + w3 / (w2 + w3);
675 *dst++ = w0 + w1;
676 *dst++ = 0;
679 //! to avoid artefacts this should be rather large
680 #define LOOKUP_BSPLINE_RES (2 * 1024)
682 * \brief creates the 1D lookup texture needed for fast higher-order filtering
683 * \param unit texture unit to attach texture to
685 static void gen_spline_lookup_tex(GLenum unit) {
686 GLfloat tex[4 * LOOKUP_BSPLINE_RES];
687 GLfloat *tp = tex;
688 int i;
689 for (i = 0; i < LOOKUP_BSPLINE_RES; i++) {
690 float x = (float)(i + 0.5) / LOOKUP_BSPLINE_RES;
691 store_weights(x, tp);
692 tp += 4;
694 store_weights(0, tex);
695 store_weights(1, &tex[4 * (LOOKUP_BSPLINE_RES - 1)]);
696 ActiveTexture(unit);
697 glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA16, LOOKUP_BSPLINE_RES, 0, GL_RGBA, GL_FLOAT, tex);
698 glTexParameterf(GL_TEXTURE_1D, GL_TEXTURE_PRIORITY, 1.0);
699 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
700 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
701 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT);
702 ActiveTexture(GL_TEXTURE0);
705 static const char *bilin_filt_template =
706 "TEX yuv.%c, fragment.texcoord[%c], texture[%c], %s;";
708 #define BICUB_FILT_MAIN(textype) \
709 /* first y-interpolation */ \
710 "ADD coord, fragment.texcoord[%c].xyxy, cdelta.xyxw;" \
711 "ADD coord2, fragment.texcoord[%c].xyxy, cdelta.zyzw;" \
712 "TEX a.r, coord.xyxy, texture[%c], "textype";" \
713 "TEX a.g, coord.zwzw, texture[%c], "textype";" \
714 /* second y-interpolation */ \
715 "TEX b.r, coord2.xyxy, texture[%c], "textype";" \
716 "TEX b.g, coord2.zwzw, texture[%c], "textype";" \
717 "LRP a.b, parmy.b, a.rrrr, a.gggg;" \
718 "LRP a.a, parmy.b, b.rrrr, b.gggg;" \
719 /* x-interpolation */ \
720 "LRP yuv.%c, parmx.b, a.bbbb, a.aaaa;"
722 static const char *bicub_filt_template_2D =
723 "MAD coord.xy, fragment.texcoord[%c], {%f, %f}, {0.5, 0.5};"
724 "TEX parmx, coord.x, texture[%c], 1D;"
725 "MUL cdelta.xz, parmx.rrgg, {-%f, 0, %f, 0};"
726 "TEX parmy, coord.y, texture[%c], 1D;"
727 "MUL cdelta.yw, parmy.rrgg, {0, -%f, 0, %f};"
728 BICUB_FILT_MAIN("2D");
730 static const char *bicub_filt_template_RECT =
731 "ADD coord, fragment.texcoord[%c], {0.5, 0.5};"
732 "TEX parmx, coord.x, texture[%c], 1D;"
733 "MUL cdelta.xz, parmx.rrgg, {-1, 0, 1, 0};"
734 "TEX parmy, coord.y, texture[%c], 1D;"
735 "MUL cdelta.yw, parmy.rrgg, {0, -1, 0, 1};"
736 BICUB_FILT_MAIN("RECT");
738 #define CALCWEIGHTS(t, s) \
739 "MAD "t", {-0.5, 0.1666, 0.3333, -0.3333}, "s", {1, 0, -0.5, 0.5};" \
740 "MAD "t", "t", "s", {0, 0, -0.5, 0.5};" \
741 "MAD "t", "t", "s", {-0.6666, 0, 0.8333, 0.1666};" \
742 "RCP a.x, "t".z;" \
743 "RCP a.y, "t".w;" \
744 "MAD "t".xy, "t".xyxy, a.xyxy, {1, 1, 0, 0};" \
745 "ADD "t".x, "t".xxxx, "s";" \
746 "SUB "t".y, "t".yyyy, "s";"
748 static const char *bicub_notex_filt_template_2D =
749 "MAD coord.xy, fragment.texcoord[%c], {%f, %f}, {0.5, 0.5};"
750 "FRC coord.xy, coord.xyxy;"
751 CALCWEIGHTS("parmx", "coord.xxxx")
752 "MUL cdelta.xz, parmx.rrgg, {-%f, 0, %f, 0};"
753 CALCWEIGHTS("parmy", "coord.yyyy")
754 "MUL cdelta.yw, parmy.rrgg, {0, -%f, 0, %f};"
755 BICUB_FILT_MAIN("2D");
757 static const char *bicub_notex_filt_template_RECT =
758 "ADD coord, fragment.texcoord[%c], {0.5, 0.5};"
759 "FRC coord.xy, coord.xyxy;"
760 CALCWEIGHTS("parmx", "coord.xxxx")
761 "MUL cdelta.xz, parmx.rrgg, {-1, 0, 1, 0};"
762 CALCWEIGHTS("parmy", "coord.yyyy")
763 "MUL cdelta.yw, parmy.rrgg, {0, -1, 0, 1};"
764 BICUB_FILT_MAIN("RECT");
766 #define BICUB_X_FILT_MAIN(textype) \
767 "ADD coord.xy, fragment.texcoord[%c].xyxy, cdelta.xyxy;" \
768 "ADD coord2.xy, fragment.texcoord[%c].xyxy, cdelta.zyzy;" \
769 "TEX a.r, coord, texture[%c], "textype";" \
770 "TEX b.r, coord2, texture[%c], "textype";" \
771 /* x-interpolation */ \
772 "LRP yuv.%c, parmx.b, a.rrrr, b.rrrr;"
774 static const char *bicub_x_filt_template_2D =
775 "MAD coord.x, fragment.texcoord[%c], {%f}, {0.5};"
776 "TEX parmx, coord, texture[%c], 1D;"
777 "MUL cdelta.xyz, parmx.rrgg, {-%f, 0, %f};"
778 BICUB_X_FILT_MAIN("2D");
780 static const char *bicub_x_filt_template_RECT =
781 "ADD coord.x, fragment.texcoord[%c], {0.5};"
782 "TEX parmx, coord, texture[%c], 1D;"
783 "MUL cdelta.xyz, parmx.rrgg, {-1, 0, 1};"
784 BICUB_X_FILT_MAIN("RECT");
786 static const char *unsharp_filt_template =
787 "PARAM dcoord%c = {%f, %f, %f, %f};"
788 "ADD coord, fragment.texcoord[%c].xyxy, dcoord%c;"
789 "SUB coord2, fragment.texcoord[%c].xyxy, dcoord%c;"
790 "TEX a.r, fragment.texcoord[%c], texture[%c], %s;"
791 "TEX b.r, coord.xyxy, texture[%c], %s;"
792 "TEX b.g, coord.zwzw, texture[%c], %s;"
793 "ADD b.r, b.r, b.g;"
794 "TEX b.b, coord2.xyxy, texture[%c], %s;"
795 "TEX b.g, coord2.zwzw, texture[%c], %s;"
796 "DP3 b, b, {0.25, 0.25, 0.25};"
797 "SUB b.r, a.r, b.r;"
798 "MAD yuv.%c, b.r, {%f}, a.r;";
800 static const char *unsharp_filt_template2 =
801 "PARAM dcoord%c = {%f, %f, %f, %f};"
802 "PARAM dcoord2%c = {%f, 0, 0, %f};"
803 "ADD coord, fragment.texcoord[%c].xyxy, dcoord%c;"
804 "SUB coord2, fragment.texcoord[%c].xyxy, dcoord%c;"
805 "TEX a.r, fragment.texcoord[%c], texture[%c], %s;"
806 "TEX b.r, coord.xyxy, texture[%c], %s;"
807 "TEX b.g, coord.zwzw, texture[%c], %s;"
808 "ADD b.r, b.r, b.g;"
809 "TEX b.b, coord2.xyxy, texture[%c], %s;"
810 "TEX b.g, coord2.zwzw, texture[%c], %s;"
811 "ADD b.r, b.r, b.b;"
812 "ADD b.a, b.r, b.g;"
813 "ADD coord, fragment.texcoord[%c].xyxy, dcoord2%c;"
814 "SUB coord2, fragment.texcoord[%c].xyxy, dcoord2%c;"
815 "TEX b.r, coord.xyxy, texture[%c], %s;"
816 "TEX b.g, coord.zwzw, texture[%c], %s;"
817 "ADD b.r, b.r, b.g;"
818 "TEX b.b, coord2.xyxy, texture[%c], %s;"
819 "TEX b.g, coord2.zwzw, texture[%c], %s;"
820 "DP4 b.r, b, {-0.1171875, -0.1171875, -0.1171875, -0.09765625};"
821 "MAD b.r, a.r, {0.859375}, b.r;"
822 "MAD yuv.%c, b.r, {%f}, a.r;";
824 static const char *yuv_prog_template =
825 "PARAM ycoef = {%.4f, %.4f, %.4f};"
826 "PARAM ucoef = {%.4f, %.4f, %.4f};"
827 "PARAM vcoef = {%.4f, %.4f, %.4f};"
828 "PARAM offsets = {%.4f, %.4f, %.4f};"
829 "TEMP res;"
830 "MAD res.rgb, yuv.rrrr, ycoef, offsets;"
831 "MAD res.rgb, yuv.gggg, ucoef, res;"
832 "MAD result.color.rgb, yuv.bbbb, vcoef, res;"
833 "END";
835 static const char *yuv_pow_prog_template =
836 "PARAM ycoef = {%.4f, %.4f, %.4f};"
837 "PARAM ucoef = {%.4f, %.4f, %.4f};"
838 "PARAM vcoef = {%.4f, %.4f, %.4f};"
839 "PARAM offsets = {%.4f, %.4f, %.4f};"
840 "PARAM gamma = {%.4f, %.4f, %.4f};"
841 "TEMP res;"
842 "MAD res.rgb, yuv.rrrr, ycoef, offsets;"
843 "MAD res.rgb, yuv.gggg, ucoef, res;"
844 "MAD_SAT res.rgb, yuv.bbbb, vcoef, res;"
845 "POW result.color.r, res.r, gamma.r;"
846 "POW result.color.g, res.g, gamma.g;"
847 "POW result.color.b, res.b, gamma.b;"
848 "END";
850 static const char *yuv_lookup_prog_template =
851 "PARAM ycoef = {%.4f, %.4f, %.4f, 0};"
852 "PARAM ucoef = {%.4f, %.4f, %.4f, 0};"
853 "PARAM vcoef = {%.4f, %.4f, %.4f, 0};"
854 "PARAM offsets = {%.4f, %.4f, %.4f, 0.125};"
855 "TEMP res;"
856 "MAD res, yuv.rrrr, ycoef, offsets;"
857 "MAD res.rgb, yuv.gggg, ucoef, res;"
858 "MAD res.rgb, yuv.bbbb, vcoef, res;"
859 "TEX result.color.r, res.raaa, texture[%c], 2D;"
860 "ADD res.a, res.a, 0.25;"
861 "TEX result.color.g, res.gaaa, texture[%c], 2D;"
862 "ADD res.a, res.a, 0.25;"
863 "TEX result.color.b, res.baaa, texture[%c], 2D;"
864 "END";
866 static const char *yuv_lookup3d_prog_template =
867 "TEX result.color, yuv, texture[%c], 3D;"
868 "END";
871 * \brief creates and initializes helper textures needed for scaling texture read
872 * \param scaler scaler type to create texture for
873 * \param texu contains next free texture unit number
874 * \param texs texture unit ids for the scaler are stored in this array
876 static void create_scaler_textures(int scaler, int *texu, char *texs) {
877 switch (scaler) {
878 case YUV_SCALER_BILIN:
879 case YUV_SCALER_BICUB_NOTEX:
880 case YUV_SCALER_UNSHARP:
881 case YUV_SCALER_UNSHARP2:
882 break;
883 case YUV_SCALER_BICUB:
884 case YUV_SCALER_BICUB_X:
885 texs[0] = (*texu)++;
886 gen_spline_lookup_tex(GL_TEXTURE0 + texs[0]);
887 texs[0] += '0';
888 break;
889 default:
890 mp_msg(MSGT_VO, MSGL_ERR, "[gl] unknown scaler type %i\n", scaler);
894 static void gen_gamma_map(unsigned char *map, int size, float gamma);
896 #define ROW_R 0
897 #define ROW_G 1
898 #define ROW_B 2
899 #define COL_Y 0
900 #define COL_U 1
901 #define COL_V 2
902 #define COL_C 3
904 static void get_yuv2rgb_coeffs(gl_conversion_params_t *params, float yuv2rgb[3][4]) {
905 float uvcos = params->saturation * cos(params->hue);
906 float uvsin = params->saturation * sin(params->hue);
907 int i;
908 float uv_coeffs[3][2] = {
909 { 0.000, 1.596},
910 {-0.391, -0.813},
911 { 2.018, 0.000}
913 for (i = 0; i < 3; i++) {
914 yuv2rgb[i][COL_C] = params->brightness;
915 yuv2rgb[i][COL_Y] = 1.164 * params->contrast;
916 yuv2rgb[i][COL_C] += (-16 / 255.0) * yuv2rgb[i][COL_Y];
917 yuv2rgb[i][COL_U] = uv_coeffs[i][0] * uvcos + uv_coeffs[i][1] * uvsin;
918 yuv2rgb[i][COL_C] += (-128 / 255.0) * yuv2rgb[i][COL_U];
919 yuv2rgb[i][COL_V] = uv_coeffs[i][0] * uvsin + uv_coeffs[i][1] * uvcos;
920 yuv2rgb[i][COL_C] += (-128 / 255.0) * yuv2rgb[i][COL_V];
921 // this "centers" contrast control so that e.g. a contrast of 0
922 // leads to a grey image, not a black one
923 yuv2rgb[i][COL_C] += 0.5 - params->contrast / 2.0;
927 //! size of gamma map use to avoid slow exp function in gen_yuv2rgb_map
928 #define GMAP_SIZE (1024)
930 * \brief generate a 3D YUV -> RGB map
931 * \param params struct containing parameters like brightness, gamma, ...
932 * \param map where to store map. Must provide space for (size + 2)^3 elements
933 * \param size size of the map, excluding border
935 static void gen_yuv2rgb_map(gl_conversion_params_t *params, unsigned char *map, int size) {
936 int i, j, k, l;
937 float step = 1.0 / size;
938 float y, u, v;
939 float yuv2rgb[3][4];
940 unsigned char gmaps[3][GMAP_SIZE];
941 gen_gamma_map(gmaps[0], GMAP_SIZE, params->rgamma);
942 gen_gamma_map(gmaps[1], GMAP_SIZE, params->ggamma);
943 gen_gamma_map(gmaps[2], GMAP_SIZE, params->bgamma);
944 get_yuv2rgb_coeffs(params, yuv2rgb);
945 for (i = 0; i < 3; i++)
946 for (j = 0; j < 4; j++)
947 yuv2rgb[i][j] *= GMAP_SIZE - 1;
948 v = 0;
949 for (i = -1; i <= size; i++) {
950 u = 0;
951 for (j = -1; j <= size; j++) {
952 y = 0;
953 for (k = -1; k <= size; k++) {
954 for (l = 0; l < 3; l++) {
955 float rgb = yuv2rgb[l][COL_Y] * y + yuv2rgb[l][COL_U] * u + yuv2rgb[l][COL_V] * v + yuv2rgb[l][COL_C];
956 *map++ = gmaps[l][av_clip(rgb, 0, GMAP_SIZE - 1)];
958 y += (k == -1 || k == size - 1) ? step / 2 : step;
960 u += (j == -1 || j == size - 1) ? step / 2 : step;
962 v += (i == -1 || i == size - 1) ? step / 2 : step;
966 //! resolution of texture for gamma lookup table
967 #define LOOKUP_RES 512
968 //! resolution for 3D yuv->rgb conversion lookup table
969 #define LOOKUP_3DRES 32
971 * \brief creates and initializes helper textures needed for yuv conversion
972 * \param params struct containing parameters like brightness, gamma, ...
973 * \param texu contains next free texture unit number
974 * \param texs texture unit ids for the conversion are stored in this array
976 static void create_conv_textures(gl_conversion_params_t *params, int *texu, char *texs) {
977 unsigned char *lookup_data = NULL;
978 int conv = YUV_CONVERSION(params->type);
979 switch (conv) {
980 case YUV_CONVERSION_FRAGMENT:
981 case YUV_CONVERSION_FRAGMENT_POW:
982 break;
983 case YUV_CONVERSION_FRAGMENT_LOOKUP:
984 texs[0] = (*texu)++;
985 ActiveTexture(GL_TEXTURE0 + texs[0]);
986 lookup_data = malloc(4 * LOOKUP_RES);
987 gen_gamma_map(lookup_data, LOOKUP_RES, params->rgamma);
988 gen_gamma_map(&lookup_data[LOOKUP_RES], LOOKUP_RES, params->ggamma);
989 gen_gamma_map(&lookup_data[2 * LOOKUP_RES], LOOKUP_RES, params->bgamma);
990 glCreateClearTex(GL_TEXTURE_2D, GL_LUMINANCE8, GL_LUMINANCE, GL_UNSIGNED_BYTE, GL_LINEAR,
991 LOOKUP_RES, 4, 0);
992 glUploadTex(GL_TEXTURE_2D, GL_LUMINANCE, GL_UNSIGNED_BYTE, lookup_data,
993 LOOKUP_RES, 0, 0, LOOKUP_RES, 4, 0);
994 ActiveTexture(GL_TEXTURE0);
995 texs[0] += '0';
996 break;
997 case YUV_CONVERSION_FRAGMENT_LOOKUP3D:
999 int sz = LOOKUP_3DRES + 2; // texture size including borders
1000 if (!TexImage3D) {
1001 mp_msg(MSGT_VO, MSGL_ERR, "[gl] Missing 3D texture function!\n");
1002 break;
1004 texs[0] = (*texu)++;
1005 ActiveTexture(GL_TEXTURE0 + texs[0]);
1006 lookup_data = malloc(3 * sz * sz * sz);
1007 gen_yuv2rgb_map(params, lookup_data, LOOKUP_3DRES);
1008 glAdjustAlignment(sz);
1009 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
1010 TexImage3D(GL_TEXTURE_3D, 0, 3, sz, sz, sz, 1,
1011 GL_RGB, GL_UNSIGNED_BYTE, lookup_data);
1012 glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_PRIORITY, 1.0);
1013 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1014 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1015 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP);
1016 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP);
1017 glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP);
1018 ActiveTexture(GL_TEXTURE0);
1019 texs[0] += '0';
1021 break;
1022 default:
1023 mp_msg(MSGT_VO, MSGL_ERR, "[gl] unknown conversion type %i\n", conv);
1025 if (lookup_data)
1026 free(lookup_data);
1030 * \brief adds a scaling texture read at the current fragment program position
1031 * \param scaler type of scaler to insert
1032 * \param prog_pos current position in fragment program
1033 * \param remain how many bytes remain in the buffer given by prog_pos
1034 * \param texs array containing the texture unit identifiers for this scaler
1035 * \param in_tex texture unit the scaler should read from
1036 * \param out_comp component of the yuv variable the scaler stores the result in
1037 * \param rect if rectangular (pixel) adressing should be used for in_tex
1038 * \param texw width of the in_tex texture
1039 * \param texh height of the in_tex texture
1040 * \param strength strength of filter effect if the scaler does some kind of filtering
1042 static void add_scaler(int scaler, char **prog_pos, int *remain, char *texs,
1043 char in_tex, char out_comp, int rect, int texw, int texh,
1044 double strength) {
1045 const char *ttype = rect ? "RECT" : "2D";
1046 const float ptw = rect ? 1.0 : 1.0 / texw;
1047 const float pth = rect ? 1.0 : 1.0 / texh;
1048 switch (scaler) {
1049 case YUV_SCALER_BILIN:
1050 snprintf(*prog_pos, *remain, bilin_filt_template, out_comp, in_tex,
1051 in_tex, ttype);
1052 break;
1053 case YUV_SCALER_BICUB:
1054 if (rect)
1055 snprintf(*prog_pos, *remain, bicub_filt_template_RECT,
1056 in_tex, texs[0], texs[0],
1057 in_tex, in_tex, in_tex, in_tex, in_tex, in_tex, out_comp);
1058 else
1059 snprintf(*prog_pos, *remain, bicub_filt_template_2D,
1060 in_tex, (float)texw, (float)texh,
1061 texs[0], ptw, ptw, texs[0], pth, pth,
1062 in_tex, in_tex, in_tex, in_tex, in_tex, in_tex, out_comp);
1063 break;
1064 case YUV_SCALER_BICUB_X:
1065 if (rect)
1066 snprintf(*prog_pos, *remain, bicub_x_filt_template_RECT,
1067 in_tex, texs[0],
1068 in_tex, in_tex, in_tex, in_tex, out_comp);
1069 else
1070 snprintf(*prog_pos, *remain, bicub_x_filt_template_2D,
1071 in_tex, (float)texw,
1072 texs[0], ptw, ptw,
1073 in_tex, in_tex, in_tex, in_tex, out_comp);
1074 break;
1075 case YUV_SCALER_BICUB_NOTEX:
1076 if (rect)
1077 snprintf(*prog_pos, *remain, bicub_notex_filt_template_RECT,
1078 in_tex,
1079 in_tex, in_tex, in_tex, in_tex, in_tex, in_tex, out_comp);
1080 else
1081 snprintf(*prog_pos, *remain, bicub_notex_filt_template_2D,
1082 in_tex, (float)texw, (float)texh, ptw, ptw, pth, pth,
1083 in_tex, in_tex, in_tex, in_tex, in_tex, in_tex, out_comp);
1084 break;
1085 case YUV_SCALER_UNSHARP:
1086 snprintf(*prog_pos, *remain, unsharp_filt_template,
1087 out_comp, 0.5 * ptw, 0.5 * pth, 0.5 * ptw, -0.5 * pth,
1088 in_tex, out_comp, in_tex, out_comp, in_tex,
1089 in_tex, ttype, in_tex, ttype, in_tex, ttype, in_tex, ttype,
1090 in_tex, ttype, out_comp, strength);
1091 break;
1092 case YUV_SCALER_UNSHARP2:
1093 snprintf(*prog_pos, *remain, unsharp_filt_template2,
1094 out_comp, 1.2 * ptw, 1.2 * pth, 1.2 * ptw, -1.2 * pth,
1095 out_comp, 1.5 * ptw, 1.5 * pth,
1096 in_tex, out_comp, in_tex, out_comp, in_tex,
1097 in_tex, ttype, in_tex, ttype, in_tex, ttype, in_tex, ttype,
1098 in_tex, ttype, in_tex, out_comp, in_tex, out_comp,
1099 in_tex, ttype, in_tex, ttype, in_tex, ttype,
1100 in_tex, ttype, out_comp, strength);
1101 break;
1103 *remain -= strlen(*prog_pos);
1104 *prog_pos += strlen(*prog_pos);
1107 static const struct {
1108 const char *name;
1109 GLenum cur;
1110 GLenum max;
1111 } progstats[] = {
1112 {"instructions", 0x88A0, 0x88A1},
1113 {"native instructions", 0x88A2, 0x88A3},
1114 {"temporaries", 0x88A4, 0x88A5},
1115 {"native temporaries", 0x88A6, 0x88A7},
1116 {"parameters", 0x88A8, 0x88A9},
1117 {"native parameters", 0x88AA, 0x88AB},
1118 {"attribs", 0x88AC, 0x88AD},
1119 {"native attribs", 0x88AE, 0x88AF},
1120 {"ALU instructions", 0x8805, 0x880B},
1121 {"TEX instructions", 0x8806, 0x880C},
1122 {"TEX indirections", 0x8807, 0x880D},
1123 {"native ALU instructions", 0x8808, 0x880E},
1124 {"native TEX instructions", 0x8809, 0x880F},
1125 {"native TEX indirections", 0x880A, 0x8810},
1126 {NULL, 0, 0}
1130 * \brief load the specified GPU Program
1131 * \param target program target to load into, only GL_FRAGMENT_PROGRAM is tested
1132 * \param prog program string
1133 * \return 1 on success, 0 otherwise
1135 int loadGPUProgram(GLenum target, char *prog) {
1136 int i;
1137 GLint cur = 0, max = 0, err = 0;
1138 if (!ProgramString) {
1139 mp_msg(MSGT_VO, MSGL_ERR, "[gl] Missing GPU program function\n");
1140 return 0;
1142 ProgramString(target, GL_PROGRAM_FORMAT_ASCII, strlen(prog), prog);
1143 glGetIntegerv(GL_PROGRAM_ERROR_POSITION, &err);
1144 if (err != -1) {
1145 mp_msg(MSGT_VO, MSGL_ERR,
1146 "[gl] Error compiling fragment program, make sure your card supports\n"
1147 "[gl] GL_ARB_fragment_program (use glxinfo to check).\n"
1148 "[gl] Error message:\n %s at %.10s\n",
1149 glGetString(GL_PROGRAM_ERROR_STRING), &prog[err]);
1150 return 0;
1152 if (!GetProgramiv || !mp_msg_test(MSGT_VO, MSGL_V))
1153 return 1;
1154 mp_msg(MSGT_VO, MSGL_V, "[gl] Program statistics:\n");
1155 for (i = 0; progstats[i].name; i++) {
1156 GetProgramiv(target, progstats[i].cur, &cur);
1157 GetProgramiv(target, progstats[i].max, &max);
1158 mp_msg(MSGT_VO, MSGL_V, "[gl] %s: %i/%i\n", progstats[i].name, cur, max);
1160 return 1;
1163 #define MAX_PROGSZ (1024*1024)
1166 * \brief setup a fragment program that will do YUV->RGB conversion
1167 * \param parms struct containing parameters like conversion and scaler type,
1168 * brightness, ...
1170 static void glSetupYUVFragprog(gl_conversion_params_t *params) {
1171 int type = params->type;
1172 int texw = params->texw;
1173 int texh = params->texh;
1174 int rect = params->target == GL_TEXTURE_RECTANGLE;
1175 static const char prog_hdr[] =
1176 "!!ARBfp1.0\n"
1177 "OPTION ARB_precision_hint_fastest;"
1178 // all scaler variables must go here so they aren't defined
1179 // multiple times when the same scaler is used more than once
1180 "TEMP coord, coord2, cdelta, parmx, parmy, a, b, yuv;";
1181 int prog_remain;
1182 char *yuv_prog, *prog_pos;
1183 int cur_texu = 3;
1184 char lum_scale_texs[1];
1185 char chrom_scale_texs[1];
1186 char conv_texs[1];
1187 GLint i;
1188 // this is the conversion matrix, with y, u, v factors
1189 // for red, green, blue and the constant offsets
1190 float yuv2rgb[3][4];
1191 create_conv_textures(params, &cur_texu, conv_texs);
1192 create_scaler_textures(YUV_LUM_SCALER(type), &cur_texu, lum_scale_texs);
1193 if (YUV_CHROM_SCALER(type) == YUV_LUM_SCALER(type))
1194 memcpy(chrom_scale_texs, lum_scale_texs, sizeof(chrom_scale_texs));
1195 else
1196 create_scaler_textures(YUV_CHROM_SCALER(type), &cur_texu, chrom_scale_texs);
1197 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &i);
1198 if (i < cur_texu)
1199 mp_msg(MSGT_VO, MSGL_ERR,
1200 "[gl] %i texture units needed for this type of YUV fragment support (found %i)\n",
1201 cur_texu, i);
1202 if (!ProgramString) {
1203 mp_msg(MSGT_VO, MSGL_FATAL, "[gl] ProgramString function missing!\n");
1204 return;
1206 yuv_prog = malloc(MAX_PROGSZ);
1207 strcpy(yuv_prog, prog_hdr);
1208 prog_pos = yuv_prog + sizeof(prog_hdr) - 1;
1209 prog_remain = MAX_PROGSZ - sizeof(prog_hdr);
1210 add_scaler(YUV_LUM_SCALER(type), &prog_pos, &prog_remain, lum_scale_texs,
1211 '0', 'r', rect, texw, texh, params->filter_strength);
1212 add_scaler(YUV_CHROM_SCALER(type), &prog_pos, &prog_remain, chrom_scale_texs,
1213 '1', 'g', rect, texw / 2, texh / 2, params->filter_strength);
1214 add_scaler(YUV_CHROM_SCALER(type), &prog_pos, &prog_remain, chrom_scale_texs,
1215 '2', 'b', rect, texw / 2, texh / 2, params->filter_strength);
1216 get_yuv2rgb_coeffs(params, yuv2rgb);
1217 switch (YUV_CONVERSION(type)) {
1218 case YUV_CONVERSION_FRAGMENT:
1219 snprintf(prog_pos, prog_remain, yuv_prog_template,
1220 yuv2rgb[ROW_R][COL_Y], yuv2rgb[ROW_G][COL_Y], yuv2rgb[ROW_B][COL_Y],
1221 yuv2rgb[ROW_R][COL_U], yuv2rgb[ROW_G][COL_U], yuv2rgb[ROW_B][COL_U],
1222 yuv2rgb[ROW_R][COL_V], yuv2rgb[ROW_G][COL_V], yuv2rgb[ROW_B][COL_V],
1223 yuv2rgb[ROW_R][COL_C], yuv2rgb[ROW_G][COL_C], yuv2rgb[ROW_B][COL_C]);
1224 break;
1225 case YUV_CONVERSION_FRAGMENT_POW:
1226 snprintf(prog_pos, prog_remain, yuv_pow_prog_template,
1227 yuv2rgb[ROW_R][COL_Y], yuv2rgb[ROW_G][COL_Y], yuv2rgb[ROW_B][COL_Y],
1228 yuv2rgb[ROW_R][COL_U], yuv2rgb[ROW_G][COL_U], yuv2rgb[ROW_B][COL_U],
1229 yuv2rgb[ROW_R][COL_V], yuv2rgb[ROW_G][COL_V], yuv2rgb[ROW_B][COL_V],
1230 yuv2rgb[ROW_R][COL_C], yuv2rgb[ROW_G][COL_C], yuv2rgb[ROW_B][COL_C],
1231 (float)1.0 / params->rgamma, (float)1.0 / params->bgamma, (float)1.0 / params->bgamma);
1232 break;
1233 case YUV_CONVERSION_FRAGMENT_LOOKUP:
1234 snprintf(prog_pos, prog_remain, yuv_lookup_prog_template,
1235 yuv2rgb[ROW_R][COL_Y], yuv2rgb[ROW_G][COL_Y], yuv2rgb[ROW_B][COL_Y],
1236 yuv2rgb[ROW_R][COL_U], yuv2rgb[ROW_G][COL_U], yuv2rgb[ROW_B][COL_U],
1237 yuv2rgb[ROW_R][COL_V], yuv2rgb[ROW_G][COL_V], yuv2rgb[ROW_B][COL_V],
1238 yuv2rgb[ROW_R][COL_C], yuv2rgb[ROW_G][COL_C], yuv2rgb[ROW_B][COL_C],
1239 conv_texs[0], conv_texs[0], conv_texs[0]);
1240 break;
1241 case YUV_CONVERSION_FRAGMENT_LOOKUP3D:
1242 snprintf(prog_pos, prog_remain, yuv_lookup3d_prog_template, conv_texs[0]);
1243 break;
1244 default:
1245 mp_msg(MSGT_VO, MSGL_ERR, "[gl] unknown conversion type %i\n", YUV_CONVERSION(type));
1246 break;
1248 mp_msg(MSGT_VO, MSGL_V, "[gl] generated fragment program:\n%s\n", yuv_prog);
1249 loadGPUProgram(GL_FRAGMENT_PROGRAM, yuv_prog);
1250 free(yuv_prog);
1254 * \brief little helper function to create a lookup table for gamma
1255 * \param map buffer to create map into
1256 * \param size size of buffer
1257 * \param gamma gamma value
1259 static void gen_gamma_map(unsigned char *map, int size, float gamma) {
1260 int i;
1261 if (gamma == 1.0) {
1262 for (i = 0; i < size; i++)
1263 map[i] = 255 * i / (size - 1);
1264 return;
1266 gamma = 1.0 / gamma;
1267 for (i = 0; i < size; i++) {
1268 float tmp = (float)i / (size - 1.0);
1269 tmp = pow(tmp, gamma);
1270 if (tmp > 1.0) tmp = 1.0;
1271 if (tmp < 0.0) tmp = 0.0;
1272 map[i] = 255 * tmp;
1277 * \brief setup YUV->RGB conversion
1278 * \param parms struct containing parameters like conversion and scaler type,
1279 * brightness, ...
1280 * \ingroup glconversion
1282 void glSetupYUVConversion(gl_conversion_params_t *params) {
1283 float uvcos = params->saturation * cos(params->hue);
1284 float uvsin = params->saturation * sin(params->hue);
1285 switch (YUV_CONVERSION(params->type)) {
1286 case YUV_CONVERSION_COMBINERS:
1287 glSetupYUVCombiners(uvcos, uvsin);
1288 break;
1289 case YUV_CONVERSION_COMBINERS_ATI:
1290 glSetupYUVCombinersATI(uvcos, uvsin);
1291 break;
1292 case YUV_CONVERSION_FRAGMENT_LOOKUP:
1293 case YUV_CONVERSION_FRAGMENT_LOOKUP3D:
1294 case YUV_CONVERSION_FRAGMENT:
1295 case YUV_CONVERSION_FRAGMENT_POW:
1296 glSetupYUVFragprog(params);
1297 break;
1298 default:
1299 mp_msg(MSGT_VO, MSGL_ERR, "[gl] unknown conversion type %i\n", YUV_CONVERSION(params->type));
1304 * \brief enable the specified YUV conversion
1305 * \param target texture target for Y, U and V textures (e.g. GL_TEXTURE_2D)
1306 * \param type type of YUV conversion
1307 * \ingroup glconversion
1309 void glEnableYUVConversion(GLenum target, int type) {
1310 if (type <= 0) return;
1311 switch (YUV_CONVERSION(type)) {
1312 case YUV_CONVERSION_COMBINERS:
1313 ActiveTexture(GL_TEXTURE1);
1314 glEnable(target);
1315 ActiveTexture(GL_TEXTURE2);
1316 glEnable(target);
1317 ActiveTexture(GL_TEXTURE0);
1318 glEnable(GL_REGISTER_COMBINERS_NV);
1319 break;
1320 case YUV_CONVERSION_COMBINERS_ATI:
1321 ActiveTexture(GL_TEXTURE1);
1322 glEnable(target);
1323 ActiveTexture(GL_TEXTURE2);
1324 glEnable(target);
1325 ActiveTexture(GL_TEXTURE0);
1326 glEnable(GL_FRAGMENT_SHADER_ATI);
1327 break;
1328 case YUV_CONVERSION_FRAGMENT_LOOKUP3D:
1329 case YUV_CONVERSION_FRAGMENT_LOOKUP:
1330 case YUV_CONVERSION_FRAGMENT_POW:
1331 case YUV_CONVERSION_FRAGMENT:
1332 glEnable(GL_FRAGMENT_PROGRAM);
1333 break;
1338 * \brief disable the specified YUV conversion
1339 * \param target texture target for Y, U and V textures (e.g. GL_TEXTURE_2D)
1340 * \param type type of YUV conversion
1341 * \ingroup glconversion
1343 void glDisableYUVConversion(GLenum target, int type) {
1344 if (type <= 0) return;
1345 switch (YUV_CONVERSION(type)) {
1346 case YUV_CONVERSION_COMBINERS:
1347 ActiveTexture(GL_TEXTURE1);
1348 glDisable(target);
1349 ActiveTexture(GL_TEXTURE2);
1350 glDisable(target);
1351 ActiveTexture(GL_TEXTURE0);
1352 glDisable(GL_REGISTER_COMBINERS_NV);
1353 break;
1354 case YUV_CONVERSION_COMBINERS_ATI:
1355 ActiveTexture(GL_TEXTURE1);
1356 glDisable(target);
1357 ActiveTexture(GL_TEXTURE2);
1358 glDisable(target);
1359 ActiveTexture(GL_TEXTURE0);
1360 glDisable(GL_FRAGMENT_SHADER_ATI);
1361 break;
1362 case YUV_CONVERSION_FRAGMENT_LOOKUP3D:
1363 case YUV_CONVERSION_FRAGMENT_LOOKUP:
1364 case YUV_CONVERSION_FRAGMENT_POW:
1365 case YUV_CONVERSION_FRAGMENT:
1366 glDisable(GL_FRAGMENT_PROGRAM);
1367 break;
1372 * \brief draw a texture part at given 2D coordinates
1373 * \param x screen top coordinate
1374 * \param y screen left coordinate
1375 * \param w screen width coordinate
1376 * \param h screen height coordinate
1377 * \param tx texture top coordinate in pixels
1378 * \param ty texture left coordinate in pixels
1379 * \param tw texture part width in pixels
1380 * \param th texture part height in pixels
1381 * \param sx width of texture in pixels
1382 * \param sy height of texture in pixels
1383 * \param rect_tex whether this texture uses texture_rectangle extension
1384 * \param is_yv12 if set, also draw the textures from units 1 and 2
1385 * \param flip flip the texture upside down
1386 * \ingroup gltexture
1388 void glDrawTex(GLfloat x, GLfloat y, GLfloat w, GLfloat h,
1389 GLfloat tx, GLfloat ty, GLfloat tw, GLfloat th,
1390 int sx, int sy, int rect_tex, int is_yv12, int flip) {
1391 GLfloat tx2 = tx / 2, ty2 = ty / 2, tw2 = tw / 2, th2 = th / 2;
1392 if (!rect_tex) {
1393 tx /= sx; ty /= sy; tw /= sx; th /= sy;
1394 tx2 = tx, ty2 = ty, tw2 = tw, th2 = th;
1396 if (flip) {
1397 y += h;
1398 h = -h;
1400 glBegin(GL_QUADS);
1401 glTexCoord2f(tx, ty);
1402 if (is_yv12) {
1403 MultiTexCoord2f(GL_TEXTURE1, tx2, ty2);
1404 MultiTexCoord2f(GL_TEXTURE2, tx2, ty2);
1406 glVertex2f(x, y);
1407 glTexCoord2f(tx, ty + th);
1408 if (is_yv12) {
1409 MultiTexCoord2f(GL_TEXTURE1, tx2, ty2 + th2);
1410 MultiTexCoord2f(GL_TEXTURE2, tx2, ty2 + th2);
1412 glVertex2f(x, y + h);
1413 glTexCoord2f(tx + tw, ty + th);
1414 if (is_yv12) {
1415 MultiTexCoord2f(GL_TEXTURE1, tx2 + tw2, ty2 + th2);
1416 MultiTexCoord2f(GL_TEXTURE2, tx2 + tw2, ty2 + th2);
1418 glVertex2f(x + w, y + h);
1419 glTexCoord2f(tx + tw, ty);
1420 if (is_yv12) {
1421 MultiTexCoord2f(GL_TEXTURE1, tx2 + tw2, ty2);
1422 MultiTexCoord2f(GL_TEXTURE2, tx2 + tw2, ty2);
1424 glVertex2f(x + w, y);
1425 glEnd();
1428 #ifdef GL_WIN32
1429 #include "w32_common.h"
1431 * \brief little helper since wglGetProcAddress definition does not fit our
1432 * getProcAddress
1433 * \param procName name of function to look up
1434 * \return function pointer returned by wglGetProcAddress
1436 static void *w32gpa(const GLubyte *procName) {
1437 HMODULE oglmod;
1438 void *res = wglGetProcAddress(procName);
1439 if (res) return res;
1440 oglmod = GetModuleHandle("opengl32.dll");
1441 return GetProcAddress(oglmod, procName);
1444 int setGlWindow(int *vinfo, HGLRC *context, HWND win)
1446 int new_vinfo;
1447 HDC windc = GetDC(win);
1448 HGLRC new_context = 0;
1449 int keep_context = 0;
1450 int res = SET_WINDOW_FAILED;
1452 // should only be needed when keeping context, but not doing glFinish
1453 // can cause flickering even when we do not keep it.
1454 if (*context)
1455 glFinish();
1456 new_vinfo = GetPixelFormat(windc);
1457 if (*context && *vinfo && new_vinfo && *vinfo == new_vinfo) {
1458 // we can keep the wglContext
1459 new_context = *context;
1460 keep_context = 1;
1461 } else {
1462 // create a context
1463 new_context = wglCreateContext(windc);
1464 if (!new_context) {
1465 mp_msg(MSGT_VO, MSGL_FATAL, "[gl] Could not create GL context!\n");
1466 goto out;
1470 // set context
1471 if (!wglMakeCurrent(windc, new_context)) {
1472 mp_msg(MSGT_VO, MSGL_FATAL, "[gl] Could not set GL context!\n");
1473 if (!keep_context) {
1474 wglDeleteContext(new_context);
1476 goto out;
1479 // set new values
1480 vo_w32_window = win;
1482 RECT rect;
1483 GetClientRect(win, &rect);
1484 vo_dwidth = rect.right;
1485 vo_dheight = rect.bottom;
1487 if (!keep_context) {
1488 if (*context)
1489 wglDeleteContext(*context);
1490 *context = new_context;
1491 *vinfo = new_vinfo;
1492 getFunctions(w32gpa, NULL);
1494 // and inform that reinit is neccessary
1495 res = SET_WINDOW_REINIT;
1496 } else
1497 res = SET_WINDOW_OK;
1499 out:
1500 ReleaseDC(win, windc);
1501 return res;
1504 void releaseGlContext(int *vinfo, HGLRC *context) {
1505 *vinfo = 0;
1506 if (*context) {
1507 wglMakeCurrent(0, 0);
1508 wglDeleteContext(*context);
1510 *context = 0;
1513 void swapGlBuffers() {
1514 HDC vo_hdc = GetDC(vo_w32_window);
1515 SwapBuffers(vo_hdc);
1516 ReleaseDC(vo_w32_window, vo_hdc);
1518 #else
1519 #ifdef HAVE_LIBDL
1520 #include <dlfcn.h>
1521 #endif
1522 #include "x11_common.h"
1524 * \brief find address of a linked function
1525 * \param s name of function to find
1526 * \return address of function or NULL if not found
1528 * Copied from xine
1530 static void *getdladdr(const char *s) {
1531 #ifdef HAVE_LIBDL
1532 #if defined(__sun) || defined(__sgi)
1533 static void *handle = NULL;
1534 if (!handle)
1535 handle = dlopen(NULL, RTLD_LAZY);
1536 return dlsym(handle, s);
1537 #else
1538 return dlsym(0, s);
1539 #endif
1540 #else
1541 return NULL;
1542 #endif
1546 * \brief Returns the XVisualInfo associated with Window win.
1547 * \param win Window whose XVisualInfo is returne.
1548 * \return XVisualInfo of the window. Caller must use XFree to free it.
1550 static XVisualInfo *getWindowVisualInfo(Window win) {
1551 XWindowAttributes xw_attr;
1552 XVisualInfo vinfo_template;
1553 int tmp;
1554 XGetWindowAttributes(mDisplay, win, &xw_attr);
1555 vinfo_template.visualid = XVisualIDFromVisual(xw_attr.visual);
1556 return XGetVisualInfo(mDisplay, VisualIDMask, &vinfo_template, &tmp);
1560 * \brief Changes the window in which video is displayed.
1561 * If possible only transfers the context to the new window, otherwise
1562 * creates a new one, which must be initialized by the caller.
1563 * \param vinfo Currently used visual.
1564 * \param context Currently used context.
1565 * \param win window that should be used for drawing.
1566 * \return one of SET_WINDOW_FAILED, SET_WINDOW_OK or SET_WINDOW_REINIT.
1567 * In case of SET_WINDOW_REINIT the context could not be transfered
1568 * and the caller must initialize it correctly.
1569 * \ingroup glcontext
1571 int setGlWindow(XVisualInfo **vinfo, GLXContext *context, Window win)
1573 XVisualInfo *new_vinfo;
1574 GLXContext new_context = NULL;
1575 int keep_context = 0;
1577 // should only be needed when keeping context, but not doing glFinish
1578 // can cause flickering even when we do not keep it.
1579 if (*context)
1580 glFinish();
1581 new_vinfo = getWindowVisualInfo(win);
1582 if (*context && *vinfo && new_vinfo &&
1583 (*vinfo)->visualid == new_vinfo->visualid) {
1584 // we can keep the GLXContext
1585 new_context = *context;
1586 XFree(new_vinfo);
1587 new_vinfo = *vinfo;
1588 keep_context = 1;
1589 } else {
1590 // create a context
1591 new_context = glXCreateContext(mDisplay, new_vinfo, NULL, True);
1592 if (!new_context) {
1593 mp_msg(MSGT_VO, MSGL_FATAL, "[gl] Could not create GLX context!\n");
1594 XFree(new_vinfo);
1595 return SET_WINDOW_FAILED;
1599 // set context
1600 if (!glXMakeCurrent(mDisplay, vo_window, new_context)) {
1601 mp_msg(MSGT_VO, MSGL_FATAL, "[gl] Could not set GLX context!\n");
1602 if (!keep_context) {
1603 glXDestroyContext(mDisplay, new_context);
1604 XFree(new_vinfo);
1606 return SET_WINDOW_FAILED;
1609 // set new values
1610 vo_window = win;
1611 vo_x11_update_geometry();
1612 if (!keep_context) {
1613 void *(*getProcAddress)(const GLubyte *);
1614 const char *(*glXExtStr)(Display *, int);
1615 if (*context)
1616 glXDestroyContext(mDisplay, *context);
1617 *context = new_context;
1618 if (*vinfo)
1619 XFree(*vinfo);
1620 *vinfo = new_vinfo;
1621 getProcAddress = getdladdr("glXGetProcAddress");
1622 if (!getProcAddress)
1623 getProcAddress = getdladdr("glXGetProcAddressARB");
1624 if (!getProcAddress)
1625 getProcAddress = (void *)getdladdr;
1626 glXExtStr = getdladdr("glXQueryExtensionsString");
1627 getFunctions(getProcAddress, !glXExtStr ? NULL :
1628 glXExtStr(mDisplay, DefaultScreen(mDisplay)));
1630 // and inform that reinit is neccessary
1631 return SET_WINDOW_REINIT;
1633 return SET_WINDOW_OK;
1637 * \brief free the VisualInfo and GLXContext of an OpenGL context.
1638 * \ingroup glcontext
1640 void releaseGlContext(XVisualInfo **vinfo, GLXContext *context) {
1641 if (*vinfo)
1642 XFree(*vinfo);
1643 *vinfo = NULL;
1644 if (*context)
1646 glFinish();
1647 glXMakeCurrent(mDisplay, None, NULL);
1648 glXDestroyContext(mDisplay, *context);
1650 *context = 0;
1653 void swapGlBuffers(void) {
1654 glXSwapBuffers(mDisplay, vo_window);
1656 #endif