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