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