1 /*****************************************************************************
2 * opengl.c: OpenGL and OpenGL ES output common code
3 *****************************************************************************
4 * Copyright (C) 2004-2013 VLC authors and VideoLAN
5 * Copyright (C) 2009, 2011 Laurent Aimar
7 * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
8 * Ilkka Ollakka <ileoo@videolan.org>
10 * Adrien Maglo <magsoft at videolan dot org>
11 * Felix Paul Kühne <fkuehne at videolan dot org>
12 * Pierre d'Herbemont <pdherbemont at videolan dot org>
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU Lesser General Public License as published by
16 * the Free Software Foundation; either version 2.1 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public License
25 * along with this program; if not, write to the Free Software Foundation,
26 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
27 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_picture_pool.h>
34 #include <vlc_subpicture.h>
35 #include <vlc_opengl.h>
39 #ifndef GL_CLAMP_TO_EDGE
40 # define GL_CLAMP_TO_EDGE 0x812F
43 #if USE_OPENGL_ES == 2 || defined(__APPLE__)
44 # define PFNGLGETPROGRAMIVPROC typeof(glGetProgramiv)*
45 # define PFNGLGETPROGRAMINFOLOGPROC typeof(glGetProgramInfoLog)*
46 # define PFNGLGETSHADERIVPROC typeof(glGetShaderiv)*
47 # define PFNGLGETSHADERINFOLOGPROC typeof(glGetShaderInfoLog)*
48 # define PFNGLGETUNIFORMLOCATIONPROC typeof(glGetUniformLocation)*
49 # define PFNGLGETATTRIBLOCATIONPROC typeof(glGetAttribLocation)*
50 # define PFNGLVERTEXATTRIBPOINTERPROC typeof(glVertexAttribPointer)*
51 # define PFNGLENABLEVERTEXATTRIBARRAYPROC typeof(glEnableVertexAttribArray)*
52 # define PFNGLUNIFORM4FVPROC typeof(glUniform4fv)*
53 # define PFNGLUNIFORM4FPROC typeof(glUniform4f)*
54 # define PFNGLUNIFORM1IPROC typeof(glUniform1i)*
55 # define PFNGLCREATESHADERPROC typeof(glCreateShader)*
56 # define PFNGLSHADERSOURCEPROC typeof(glShaderSource)*
57 # define PFNGLCOMPILESHADERPROC typeof(glCompileShader)*
58 # define PFNGLDELETESHADERPROC typeof(glDeleteShader)*
59 # define PFNGLCREATEPROGRAMPROC typeof(glCreateProgram)*
60 # define PFNGLLINKPROGRAMPROC typeof(glLinkProgram)*
61 # define PFNGLUSEPROGRAMPROC typeof(glUseProgram)*
62 # define PFNGLDELETEPROGRAMPROC typeof(glDeleteProgram)*
63 # define PFNGLATTACHSHADERPROC typeof(glAttachShader)*
64 #if defined(__APPLE__) && USE_OPENGL_ES
65 # import <CoreFoundation/CoreFoundation.h>
70 # define GLSL_VERSION "100"
71 # define VLCGL_TEXTURE_COUNT 1
72 # define VLCGL_PICTURE_MAX 1
73 # define PRECISION "precision highp float;"
74 #if USE_OPENGL_ES == 2
75 # define SUPPORTS_SHADERS
76 # define glClientActiveTexture(x)
78 # define SUPPORTS_FIXED_PIPELINE
79 # define GL_MAX_TEXTURE_IMAGE_UNITS GL_MAX_TEXTURE_UNITS
82 # define GLSL_VERSION "120"
83 # define VLCGL_TEXTURE_COUNT 1
84 # define VLCGL_PICTURE_MAX 128
86 # define SUPPORTS_SHADERS
87 # define SUPPORTS_FIXED_PIPELINE
90 static const vlc_fourcc_t gl_subpicture_chromas[] = {
113 struct vout_display_opengl_t {
118 const vlc_chroma_description_t *chroma;
125 int tex_width[PICTURE_PLANE_MAX];
126 int tex_height[PICTURE_PLANE_MAX];
128 GLuint texture[VLCGL_TEXTURE_COUNT][PICTURE_PLANE_MAX];
134 picture_pool_t *pool;
136 /* index 0 for normal and 1 for subtitle overlay */
138 GLint shader[3]; //3. is for the common vertex shader
140 GLfloat local_value[16];
142 /* Shader variables commands*/
143 #ifdef SUPPORTS_SHADERS
144 PFNGLGETUNIFORMLOCATIONPROC GetUniformLocation;
145 PFNGLGETATTRIBLOCATIONPROC GetAttribLocation;
146 PFNGLVERTEXATTRIBPOINTERPROC VertexAttribPointer;
147 PFNGLENABLEVERTEXATTRIBARRAYPROC EnableVertexAttribArray;
149 PFNGLUNIFORM4FVPROC Uniform4fv;
150 PFNGLUNIFORM4FPROC Uniform4f;
151 PFNGLUNIFORM1IPROC Uniform1i;
154 PFNGLCREATESHADERPROC CreateShader;
155 PFNGLSHADERSOURCEPROC ShaderSource;
156 PFNGLCOMPILESHADERPROC CompileShader;
157 PFNGLDELETESHADERPROC DeleteShader;
159 PFNGLCREATEPROGRAMPROC CreateProgram;
160 PFNGLLINKPROGRAMPROC LinkProgram;
161 PFNGLUSEPROGRAMPROC UseProgram;
162 PFNGLDELETEPROGRAMPROC DeleteProgram;
164 PFNGLATTACHSHADERPROC AttachShader;
166 /* Shader log commands */
167 PFNGLGETPROGRAMIVPROC GetProgramiv;
168 PFNGLGETPROGRAMINFOLOGPROC GetProgramInfoLog;
169 PFNGLGETSHADERIVPROC GetShaderiv;
170 PFNGLGETSHADERINFOLOGPROC GetShaderInfoLog;
174 PFNGLACTIVETEXTUREPROC ActiveTexture;
175 PFNGLCLIENTACTIVETEXTUREPROC ClientActiveTexture;
180 bool use_multitexture;
182 /* Non-power-of-2 texture size support */
185 uint8_t *texture_temp_buf;
186 int texture_temp_buf_size;
189 static inline int GetAlignedSize(unsigned size)
191 /* Return the smallest larger or equal power of 2 */
192 unsigned align = 1 << (8 * sizeof (unsigned) - clz(size));
193 return ((align >> 1) == size) ? size : align;
197 static bool IsLuminance16Supported(int target)
201 glGenTextures(1, &texture);
202 glBindTexture(target, texture);
203 glTexImage2D(target, 0, GL_LUMINANCE16,
204 64, 64, 0, GL_LUMINANCE, GL_UNSIGNED_SHORT, NULL);
206 glGetTexLevelParameteriv(target, 0, GL_TEXTURE_LUMINANCE_SIZE, &size);
208 glDeleteTextures(1, &texture);
214 #ifdef SUPPORTS_SHADERS
215 static void BuildVertexShader(vout_display_opengl_t *vgl,
218 /* Basic vertex shader */
219 const char *vertexShader =
220 "#version " GLSL_VERSION "\n"
222 "varying vec4 TexCoord0,TexCoord1, TexCoord2;"
223 "attribute vec4 MultiTexCoord0,MultiTexCoord1,MultiTexCoord2;"
224 "attribute vec4 VertexPosition;"
226 " TexCoord0 = MultiTexCoord0;"
227 " TexCoord1 = MultiTexCoord1;"
228 " TexCoord2 = MultiTexCoord2;"
229 " gl_Position = VertexPosition;"
232 *shader = vgl->CreateShader(GL_VERTEX_SHADER);
233 vgl->ShaderSource(*shader, 1, &vertexShader, NULL);
234 vgl->CompileShader(*shader);
237 static void BuildYUVFragmentShader(vout_display_opengl_t *vgl,
240 GLfloat *local_value,
241 const video_format_t *fmt,
242 float yuv_range_correction)
245 /* [R/G/B][Y U V O] from TV range to full range
246 * XXX we could also do hue/brightness/constrast/gamma
247 * by simply changing the coefficients
249 const float matrix_bt601_tv2full[12] = {
250 1.164383561643836, 0.0000, 1.596026785714286, -0.874202217873451 ,
251 1.164383561643836, -0.391762290094914, -0.812967647237771, 0.531667823499146 ,
252 1.164383561643836, 2.017232142857142, 0.0000, -1.085630789302022 ,
254 const float matrix_bt709_tv2full[12] = {
255 1.164383561643836, 0.0000, 1.792741071428571, -0.972945075016308 ,
256 1.164383561643836, -0.21324861427373, -0.532909328559444, 0.301482665475862 ,
257 1.164383561643836, 2.112401785714286, 0.0000, -1.133402217873451 ,
259 const float (*matrix) = fmt->i_height > 576 ? matrix_bt709_tv2full
260 : matrix_bt601_tv2full;
262 /* Basic linear YUV -> RGB conversion using bilinear interpolation */
263 const char *template_glsl_yuv =
264 "#version " GLSL_VERSION "\n"
266 "uniform sampler2D Texture0;"
267 "uniform sampler2D Texture1;"
268 "uniform sampler2D Texture2;"
269 "uniform vec4 Coefficient[4];"
270 "varying vec4 TexCoord0,TexCoord1,TexCoord2;"
273 " vec4 x,y,z,result;"
274 " x = texture2D(Texture0, TexCoord0.st);"
275 " %c = texture2D(Texture1, TexCoord1.st);"
276 " %c = texture2D(Texture2, TexCoord2.st);"
278 " result = x * Coefficient[0] + Coefficient[3];"
279 " result = (y * Coefficient[1]) + result;"
280 " result = (z * Coefficient[2]) + result;"
281 " gl_FragColor = result;"
283 bool swap_uv = fmt->i_chroma == VLC_CODEC_YV12 ||
284 fmt->i_chroma == VLC_CODEC_YV9;
287 if (asprintf(&code, template_glsl_yuv,
289 swap_uv ? 'y' : 'z') < 0)
292 for (int i = 0; i < 4; i++) {
293 float correction = i < 3 ? yuv_range_correction : 1.0;
294 /* We place coefficient values for coefficient[4] in one array from matrix values.
295 Notice that we fill values from top down instead of left to right.*/
296 for (int j = 0; j < 4; j++)
297 local_value[*local_count + i*4+j] = j < 3 ? correction * matrix[j*4+i]
303 *shader = vgl->CreateShader(GL_FRAGMENT_SHADER);
304 vgl->ShaderSource(*shader, 1, (const char **)&code, NULL);
305 vgl->CompileShader(*shader);
311 static void BuildRGBFragmentShader(vout_display_opengl_t *vgl,
314 // Simple shader for RGB
316 "#version " GLSL_VERSION "\n"
318 "uniform sampler2D Texture[3];"
319 "varying vec4 TexCoord0,TexCoord1,TexCoord2;"
322 " gl_FragColor = texture2D(Texture[0], TexCoord0.st);"
324 *shader = vgl->CreateShader(GL_FRAGMENT_SHADER);
325 vgl->ShaderSource(*shader, 1, &code, NULL);
326 vgl->CompileShader(*shader);
330 static void BuildRGBAFragmentShader(vout_display_opengl_t *vgl,
333 // Simple shader for RGBA
335 "#version " GLSL_VERSION "\n"
337 "uniform sampler2D Texture;"
338 "uniform vec4 FillColor;"
339 "varying vec4 TexCoord0;"
342 " gl_FragColor = texture2D(Texture, TexCoord0.st) * FillColor;"
344 *shader = vgl->CreateShader(GL_FRAGMENT_SHADER);
345 vgl->ShaderSource(*shader, 1, &code, NULL);
346 vgl->CompileShader(*shader);
349 static void BuildXYZFragmentShader(vout_display_opengl_t *vgl,
352 /* Shader for XYZ to RGB correction
354 * - XYZ gamma correction
355 * - XYZ to RGB matrix conversion
356 * - reverse RGB gamma correction
359 "#version " GLSL_VERSION "\n"
361 "uniform sampler2D Texture0;"
362 "uniform vec4 xyz_gamma = vec4(2.6);"
363 "uniform vec4 rgb_gamma = vec4(1.0/2.2);"
364 // WARN: matrix Is filled column by column (not row !)
365 "uniform mat4 matrix_xyz_rgb = mat4("
366 " 3.240454 , -0.9692660, 0.0556434, 0.0,"
367 " -1.5371385, 1.8760108, -0.2040259, 0.0,"
368 " -0.4985314, 0.0415560, 1.0572252, 0.0,"
369 " 0.0, 0.0, 0.0, 1.0 "
372 "varying vec4 TexCoord0;"
376 " v_in = texture2D(Texture0, TexCoord0.st);"
377 " v_in = pow(v_in, xyz_gamma);"
378 " v_out = matrix_xyz_rgb * v_in ;"
379 " v_out = pow(v_out, rgb_gamma) ;"
380 " v_out = clamp(v_out, 0.0, 1.0) ;"
381 " gl_FragColor = v_out;"
383 *shader = vgl->CreateShader(GL_FRAGMENT_SHADER);
384 vgl->ShaderSource(*shader, 1, &code, NULL);
385 vgl->CompileShader(*shader);
390 vout_display_opengl_t *vout_display_opengl_New(video_format_t *fmt,
391 const vlc_fourcc_t **subpicture_chromas,
394 vout_display_opengl_t *vgl = calloc(1, sizeof(*vgl));
399 if (vlc_gl_Lock(vgl->gl)) {
404 if (vgl->gl->getProcAddress == NULL) {
405 fprintf(stderr, "getProcAddress not implemented, bailing out\n");
410 const char *extensions = (const char *)glGetString(GL_EXTENSIONS);
412 const unsigned char *ogl_version = glGetString(GL_VERSION);
413 bool supports_shaders = strverscmp((const char *)ogl_version, "2.0") >= 0;
415 bool supports_shaders = false;
418 #if USE_OPENGL_ES == 2
419 vgl->CreateShader = glCreateShader;
420 vgl->ShaderSource = glShaderSource;
421 vgl->CompileShader = glCompileShader;
422 vgl->AttachShader = glAttachShader;
424 vgl->GetProgramiv = glGetProgramiv;
425 vgl->GetShaderiv = glGetShaderiv;
426 vgl->GetProgramInfoLog = glGetProgramInfoLog;
427 vgl->GetShaderInfoLog = glGetShaderInfoLog;
429 vgl->DeleteShader = glDeleteShader;
431 vgl->GetUniformLocation = glGetUniformLocation;
432 vgl->GetAttribLocation = glGetAttribLocation;
433 vgl->VertexAttribPointer= glVertexAttribPointer;
434 vgl->EnableVertexAttribArray = glEnableVertexAttribArray;
435 vgl->Uniform4fv = glUniform4fv;
436 vgl->Uniform4f = glUniform4f;
437 vgl->Uniform1i = glUniform1i;
439 vgl->CreateProgram = glCreateProgram;
440 vgl->LinkProgram = glLinkProgram;
441 vgl->UseProgram = glUseProgram;
442 vgl->DeleteProgram = glDeleteProgram;
443 supports_shaders = true;
444 #elif defined(SUPPORTS_SHADERS)
445 vgl->CreateShader = (PFNGLCREATESHADERPROC)vlc_gl_GetProcAddress(vgl->gl, "glCreateShader");
446 vgl->ShaderSource = (PFNGLSHADERSOURCEPROC)vlc_gl_GetProcAddress(vgl->gl, "glShaderSource");
447 vgl->CompileShader = (PFNGLCOMPILESHADERPROC)vlc_gl_GetProcAddress(vgl->gl, "glCompileShader");
448 vgl->AttachShader = (PFNGLATTACHSHADERPROC)vlc_gl_GetProcAddress(vgl->gl, "glAttachShader");
450 vgl->GetProgramiv = (PFNGLGETPROGRAMIVPROC)vlc_gl_GetProcAddress(vgl->gl, "glGetProgramiv");
451 vgl->GetShaderiv = (PFNGLGETSHADERIVPROC)vlc_gl_GetProcAddress(vgl->gl, "glGetShaderiv");
452 vgl->GetProgramInfoLog = (PFNGLGETPROGRAMINFOLOGPROC)vlc_gl_GetProcAddress(vgl->gl, "glGetProgramInfoLog");
453 vgl->GetShaderInfoLog = (PFNGLGETSHADERINFOLOGPROC)vlc_gl_GetProcAddress(vgl->gl, "glGetShaderInfoLog");
455 vgl->DeleteShader = (PFNGLDELETESHADERPROC)vlc_gl_GetProcAddress(vgl->gl, "glDeleteShader");
457 vgl->GetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC)vlc_gl_GetProcAddress(vgl->gl, "glGetUniformLocation");
458 vgl->GetAttribLocation = (PFNGLGETATTRIBLOCATIONPROC)vlc_gl_GetProcAddress(vgl->gl, "glGetAttribLocation");
459 vgl->VertexAttribPointer= (PFNGLVERTEXATTRIBPOINTERPROC)vlc_gl_GetProcAddress(vgl->gl, "glVertexAttribPointer");
460 vgl->EnableVertexAttribArray = (PFNGLENABLEVERTEXATTRIBARRAYPROC)vlc_gl_GetProcAddress(vgl->gl, "glEnableVertexAttribArray");
461 vgl->Uniform4fv = (PFNGLUNIFORM4FVPROC)vlc_gl_GetProcAddress(vgl->gl,"glUniform4fv");
462 vgl->Uniform4f = (PFNGLUNIFORM4FPROC)vlc_gl_GetProcAddress(vgl->gl,"glUniform4f");
463 vgl->Uniform1i = (PFNGLUNIFORM1IPROC)vlc_gl_GetProcAddress(vgl->gl,"glUniform1i");
465 vgl->CreateProgram = (PFNGLCREATEPROGRAMPROC)vlc_gl_GetProcAddress(vgl->gl, "glCreateProgram");
466 vgl->LinkProgram = (PFNGLLINKPROGRAMPROC)vlc_gl_GetProcAddress(vgl->gl, "glLinkProgram");
467 vgl->UseProgram = (PFNGLUSEPROGRAMPROC)vlc_gl_GetProcAddress(vgl->gl, "glUseProgram");
468 vgl->DeleteProgram = (PFNGLDELETEPROGRAMPROC)vlc_gl_GetProcAddress(vgl->gl, "glDeleteProgram");
470 if (!vgl->CreateShader || !vgl->ShaderSource || !vgl->CreateProgram)
471 supports_shaders = false;
475 vgl->ActiveTexture = (PFNGLACTIVETEXTUREPROC)vlc_gl_GetProcAddress(vgl->gl, "glActiveTexture");
476 vgl->ClientActiveTexture = (PFNGLCLIENTACTIVETEXTUREPROC)vlc_gl_GetProcAddress(vgl->gl, "glClientActiveTexture");
477 # define glActiveTexture vgl->ActiveTexture
478 # define glClientActiveTexture vgl->ClientActiveTexture
481 vgl->supports_npot = HasExtension(extensions, "GL_ARB_texture_non_power_of_two") ||
482 HasExtension(extensions, "GL_APPLE_texture_2D_limited_npot");
484 #if USE_OPENGL_ES == 2
485 /* OpenGL ES 2 includes support for non-power of 2 textures by specification
486 * so checks for extensions are bound to fail. Check for OpenGL ES version instead. */
487 vgl->supports_npot = true;
490 GLint max_texture_units = 0;
491 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &max_texture_units);
495 /* work-around an iOS 6 bug */
496 if (kCFCoreFoundationVersionNumber >= 786.)
497 max_texture_units = 8;
498 supports_shaders = true;
502 /* Initialize with default chroma */
504 vgl->fmt.i_chroma = VLC_CODEC_RGB32;
505 # if defined(WORDS_BIGENDIAN)
506 vgl->fmt.i_rmask = 0xff000000;
507 vgl->fmt.i_gmask = 0x00ff0000;
508 vgl->fmt.i_bmask = 0x0000ff00;
510 vgl->fmt.i_rmask = 0x000000ff;
511 vgl->fmt.i_gmask = 0x0000ff00;
512 vgl->fmt.i_bmask = 0x00ff0000;
514 vgl->tex_target = GL_TEXTURE_2D;
515 vgl->tex_format = GL_RGBA;
516 vgl->tex_internal = GL_RGBA;
517 vgl->tex_type = GL_UNSIGNED_BYTE;
518 /* Use YUV if possible and needed */
519 bool need_fs_yuv = false;
520 bool need_fs_xyz = false;
521 bool need_fs_rgba = USE_OPENGL_ES == 2;
522 float yuv_range_correction = 1.0;
524 if (max_texture_units >= 3 && supports_shaders && vlc_fourcc_IsYUV(fmt->i_chroma)) {
525 const vlc_fourcc_t *list = vlc_fourcc_GetYUVFallback(fmt->i_chroma);
527 const vlc_chroma_description_t *dsc = vlc_fourcc_GetChromaDescription(*list);
528 if (dsc && dsc->plane_count == 3 && dsc->pixel_size == 1) {
531 vgl->fmt.i_chroma = *list;
532 vgl->tex_format = GL_LUMINANCE;
533 vgl->tex_internal = GL_LUMINANCE;
534 vgl->tex_type = GL_UNSIGNED_BYTE;
535 yuv_range_correction = 1.0;
538 } else if (dsc && dsc->plane_count == 3 && dsc->pixel_size == 2 &&
539 IsLuminance16Supported(vgl->tex_target)) {
542 vgl->fmt.i_chroma = *list;
543 vgl->tex_format = GL_LUMINANCE;
544 vgl->tex_internal = GL_LUMINANCE16;
545 vgl->tex_type = GL_UNSIGNED_SHORT;
546 yuv_range_correction = (float)((1 << 16) - 1) / ((1 << dsc->pixel_bits) - 1);
554 if (fmt->i_chroma == VLC_CODEC_XYZ12) {
555 vlc_fourcc_GetChromaDescription(fmt->i_chroma);
558 vgl->fmt.i_chroma = VLC_CODEC_XYZ12;
559 vgl->tex_format = GL_RGB;
560 vgl->tex_internal = GL_RGB;
561 vgl->tex_type = GL_UNSIGNED_SHORT;
563 vgl->chroma = vlc_fourcc_GetChromaDescription(vgl->fmt.i_chroma);
564 vgl->use_multitexture = vgl->chroma->plane_count > 1;
567 for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
568 int w = vgl->fmt.i_visible_width * vgl->chroma->p[j].w.num / vgl->chroma->p[j].w.den;
569 int h = vgl->fmt.i_visible_height * vgl->chroma->p[j].h.num / vgl->chroma->p[j].h.den;
570 if (vgl->supports_npot) {
571 vgl->tex_width[j] = w;
572 vgl->tex_height[j] = h;
574 vgl->tex_width[j] = GetAlignedSize(w);
575 vgl->tex_height[j] = GetAlignedSize(h);
579 /* Build program if needed */
585 vgl->local_count = 0;
586 if (supports_shaders && (need_fs_yuv || need_fs_xyz|| need_fs_rgba)) {
587 #ifdef SUPPORTS_SHADERS
589 BuildXYZFragmentShader(vgl, &vgl->shader[0]);
591 BuildYUVFragmentShader(vgl, &vgl->shader[0], &vgl->local_count,
592 vgl->local_value, fmt, yuv_range_correction);
594 BuildRGBAFragmentShader(vgl, &vgl->shader[1]);
595 BuildVertexShader(vgl, &vgl->shader[2]);
597 /* Check shaders messages */
598 for (unsigned j = 0; j < 3; j++) {
600 vgl->GetShaderiv(vgl->shader[j], GL_INFO_LOG_LENGTH, &infoLength);
604 char *infolog = malloc(infoLength);
606 vgl->GetShaderInfoLog(vgl->shader[j], infoLength, &charsWritten, infolog);
607 fprintf(stderr, "shader %d: %s\n", j, infolog);
611 vgl->program[0] = vgl->CreateProgram();
612 vgl->AttachShader(vgl->program[0], vgl->shader[0]);
613 vgl->AttachShader(vgl->program[0], vgl->shader[2]);
614 vgl->LinkProgram(vgl->program[0]);
616 vgl->program[1] = vgl->CreateProgram();
617 vgl->AttachShader(vgl->program[1], vgl->shader[1]);
618 vgl->AttachShader(vgl->program[1], vgl->shader[2]);
619 vgl->LinkProgram(vgl->program[1]);
621 /* Check program messages */
622 for (GLuint i = 0; i < 2; i++) {
624 vgl->GetProgramiv(vgl->program[i], GL_INFO_LOG_LENGTH, &infoLength);
627 char *infolog = malloc(infoLength);
629 vgl->GetProgramInfoLog(vgl->program[i], infoLength, &charsWritten, infolog);
630 fprintf(stderr, "shader program %d: %s\n", i, infolog);
633 /* If there is some message, better to check linking is ok */
634 GLint link_status = GL_TRUE;
635 vgl->GetProgramiv(vgl->program[i], GL_LINK_STATUS, &link_status);
636 if (link_status == GL_FALSE) {
637 fprintf(stderr, "Unable to use program %d\n", i);
643 (void)yuv_range_correction;
649 glDisable(GL_DEPTH_TEST);
650 glDepthMask(GL_FALSE);
651 glDisable(GL_CULL_FACE);
652 glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
653 glClear(GL_COLOR_BUFFER_BIT);
655 vlc_gl_Unlock(vgl->gl);
658 for (int i = 0; i < VLCGL_TEXTURE_COUNT; i++) {
659 for (int j = 0; j < PICTURE_PLANE_MAX; j++)
660 vgl->texture[i][j] = 0;
662 vgl->region_count = 0;
667 if (subpicture_chromas) {
668 *subpicture_chromas = gl_subpicture_chromas;
673 void vout_display_opengl_Delete(vout_display_opengl_t *vgl)
676 if (!vlc_gl_Lock(vgl->gl)) {
679 for (int i = 0; i < VLCGL_TEXTURE_COUNT; i++)
680 glDeleteTextures(vgl->chroma->plane_count, vgl->texture[i]);
681 for (int i = 0; i < vgl->region_count; i++) {
682 if (vgl->region[i].texture)
683 glDeleteTextures(1, &vgl->region[i].texture);
687 #ifdef SUPPORTS_SHADERS
688 if (vgl->program[0]) {
689 for (int i = 0; i < 2; i++)
690 vgl->DeleteProgram(vgl->program[i]);
691 for (int i = 0; i < 3; i++)
692 vgl->DeleteShader(vgl->shader[i]);
696 free(vgl->texture_temp_buf);
697 vlc_gl_Unlock(vgl->gl);
700 picture_pool_Delete(vgl->pool);
704 picture_pool_t *vout_display_opengl_GetPool(vout_display_opengl_t *vgl, unsigned requested_count)
709 /* Allocate our pictures */
710 picture_t *picture[VLCGL_PICTURE_MAX] = {NULL, };
713 for (count = 0; count < __MIN(VLCGL_PICTURE_MAX, requested_count); count++) {
714 picture[count] = picture_NewFromFormat(&vgl->fmt);
721 /* Wrap the pictures into a pool */
722 picture_pool_configuration_t cfg;
723 memset(&cfg, 0, sizeof(cfg));
724 cfg.picture_count = count;
725 cfg.picture = picture;
726 vgl->pool = picture_pool_NewExtended(&cfg);
730 /* Allocates our textures */
731 if (vlc_gl_Lock(vgl->gl))
734 for (int i = 0; i < VLCGL_TEXTURE_COUNT; i++) {
735 glGenTextures(vgl->chroma->plane_count, vgl->texture[i]);
736 for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
737 if (vgl->use_multitexture) {
738 glActiveTexture(GL_TEXTURE0 + j);
739 glClientActiveTexture(GL_TEXTURE0 + j);
741 glBindTexture(vgl->tex_target, vgl->texture[i][j]);
744 /* Set the texture parameters */
745 glTexParameterf(vgl->tex_target, GL_TEXTURE_PRIORITY, 1.0);
746 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
749 glTexParameteri(vgl->tex_target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
750 glTexParameteri(vgl->tex_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
751 glTexParameteri(vgl->tex_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
752 glTexParameteri(vgl->tex_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
754 /* Call glTexImage2D only once, and use glTexSubImage2D later */
755 glTexImage2D(vgl->tex_target, 0,
756 vgl->tex_internal, vgl->tex_width[j], vgl->tex_height[j],
757 0, vgl->tex_format, vgl->tex_type, NULL);
761 vlc_gl_Unlock(vgl->gl);
766 for (unsigned i = 0; i < count; i++)
767 picture_Release(picture[i]);
771 #define ALIGN(x, y) (((x) + ((y) - 1)) & ~((y) - 1))
772 static void Upload(vout_display_opengl_t *vgl, int in_width, int in_height,
773 int in_full_width, int in_full_height,
774 int w_num, int w_den, int h_num, int h_den,
775 int pitch, int pixel_pitch,
776 int full_upload, const uint8_t *pixels,
777 int tex_target, int tex_format, int tex_type)
779 int width = in_width * w_num / w_den;
780 int full_width = in_full_width * w_num / w_den;
781 int height = in_height * h_num / h_den;
782 int full_height = in_full_height * h_num / h_den;
783 // This unpack alignment is the default, but setting it just in case.
784 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
785 #ifndef GL_UNPACK_ROW_LENGTH
786 int dst_width = full_upload ? full_width : width;
787 int dst_pitch = ALIGN(dst_width * pixel_pitch, 4);
788 if ( pitch != dst_pitch )
790 int buf_size = dst_pitch * full_height * pixel_pitch;
791 const uint8_t *source = pixels;
792 uint8_t *destination;
793 if( !vgl->texture_temp_buf || vgl->texture_temp_buf_size < buf_size )
795 free( vgl->texture_temp_buf );
796 vgl->texture_temp_buf = xmalloc( buf_size );
797 vgl->texture_temp_buf_size = buf_size;
799 destination = vgl->texture_temp_buf;
801 for( int h = 0; h < height ; h++ )
803 memcpy( destination, source, width * pixel_pitch );
805 destination += dst_pitch;
808 glTexImage2D( tex_target, 0, tex_format,
809 full_width, full_height,
810 0, tex_format, tex_type, vgl->texture_temp_buf );
812 glTexSubImage2D( tex_target, 0,
815 tex_format, tex_type, vgl->texture_temp_buf );
822 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch / pixel_pitch);
825 glTexImage2D(tex_target, 0, tex_format,
826 full_width, full_height,
827 0, tex_format, tex_type, pixels);
829 glTexSubImage2D(tex_target, 0,
832 tex_format, tex_type, pixels);
836 int vout_display_opengl_Prepare(vout_display_opengl_t *vgl,
837 picture_t *picture, subpicture_t *subpicture)
839 if (vlc_gl_Lock(vgl->gl))
842 /* Update the texture */
843 for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
844 if (vgl->use_multitexture) {
845 glActiveTexture(GL_TEXTURE0 + j);
846 glClientActiveTexture(GL_TEXTURE0 + j);
848 glBindTexture(vgl->tex_target, vgl->texture[0][j]);
850 Upload(vgl, picture->format.i_visible_width, vgl->fmt.i_visible_height,
851 vgl->fmt.i_width, vgl->fmt.i_height,
852 vgl->chroma->p[j].w.num, vgl->chroma->p[j].w.den, vgl->chroma->p[j].h.num, vgl->chroma->p[j].h.den,
853 picture->p[j].i_pitch, picture->p[j].i_pixel_pitch, 0, picture->p[j].p_pixels, vgl->tex_target, vgl->tex_format, vgl->tex_type);
856 int last_count = vgl->region_count;
857 gl_region_t *last = vgl->region;
859 vgl->region_count = 0;
865 for (subpicture_region_t *r = subpicture->p_region; r; r = r->p_next)
868 vgl->region_count = count;
869 vgl->region = calloc(count, sizeof(*vgl->region));
871 if (vgl->use_multitexture) {
872 glActiveTexture(GL_TEXTURE0 + 0);
873 glClientActiveTexture(GL_TEXTURE0 + 0);
876 for (subpicture_region_t *r = subpicture->p_region; r; r = r->p_next, i++) {
877 gl_region_t *glr = &vgl->region[i];
879 glr->format = GL_RGBA;
880 glr->type = GL_UNSIGNED_BYTE;
881 glr->width = r->fmt.i_visible_width;
882 glr->height = r->fmt.i_visible_height;
883 if (!vgl->supports_npot) {
884 glr->width = GetAlignedSize(glr->width);
885 glr->height = GetAlignedSize(glr->height);
886 glr->tex_width = (float) r->fmt.i_visible_width / glr->width;
887 glr->tex_height = (float) r->fmt.i_visible_height / glr->height;
889 glr->tex_width = 1.0;
890 glr->tex_height = 1.0;
892 glr->alpha = (float)subpicture->i_alpha * r->i_alpha / 255 / 255;
893 glr->left = 2.0 * (r->i_x ) / subpicture->i_original_picture_width - 1.0;
894 glr->top = -2.0 * (r->i_y ) / subpicture->i_original_picture_height + 1.0;
895 glr->right = 2.0 * (r->i_x + r->fmt.i_visible_width ) / subpicture->i_original_picture_width - 1.0;
896 glr->bottom = -2.0 * (r->i_y + r->fmt.i_visible_height) / subpicture->i_original_picture_height + 1.0;
899 /* Try to recycle the textures allocated by the previous
900 call to this function. */
901 for (int j = 0; j < last_count; j++) {
902 if (last[j].texture &&
903 last[j].width == glr->width &&
904 last[j].height == glr->height &&
905 last[j].format == glr->format &&
906 last[j].type == glr->type) {
907 glr->texture = last[j].texture;
908 memset(&last[j], 0, sizeof(last[j]));
913 const int pixels_offset = r->fmt.i_y_offset * r->p_picture->p->i_pitch +
914 r->fmt.i_x_offset * r->p_picture->p->i_pixel_pitch;
916 /* A texture was successfully recycled, reuse it. */
917 glBindTexture(GL_TEXTURE_2D, glr->texture);
918 Upload(vgl, r->fmt.i_visible_width, r->fmt.i_visible_height, glr->width, glr->height, 1, 1, 1, 1,
919 r->p_picture->p->i_pitch, r->p_picture->p->i_pixel_pitch, 0,
920 &r->p_picture->p->p_pixels[pixels_offset], GL_TEXTURE_2D, glr->format, glr->type);
922 /* Could not recycle a previous texture, generate a new one. */
923 glGenTextures(1, &glr->texture);
924 glBindTexture(GL_TEXTURE_2D, glr->texture);
926 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_PRIORITY, 1.0);
927 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
929 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
930 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
931 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
932 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
933 Upload(vgl, r->fmt.i_visible_width, r->fmt.i_visible_height, glr->width, glr->height, 1, 1, 1, 1,
934 r->p_picture->p->i_pitch, r->p_picture->p->i_pixel_pitch, 1,
935 &r->p_picture->p->p_pixels[pixels_offset], GL_TEXTURE_2D, glr->format, glr->type);
939 for (int i = 0; i < last_count; i++) {
941 glDeleteTextures(1, &last[i].texture);
945 vlc_gl_Unlock(vgl->gl);
946 VLC_UNUSED(subpicture);
950 #ifdef SUPPORTS_FIXED_PIPELINE
951 static void DrawWithoutShaders(vout_display_opengl_t *vgl,
952 float *left, float *top, float *right, float *bottom)
954 static const GLfloat vertexCoord[] = {
961 const GLfloat textureCoord[] = {
968 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
969 glEnable(vgl->tex_target);
970 glActiveTexture(GL_TEXTURE0 + 0);
971 glClientActiveTexture(GL_TEXTURE0 + 0);
973 glBindTexture(vgl->tex_target, vgl->texture[0][0]);
975 glEnableClientState(GL_VERTEX_ARRAY);
976 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
978 glTexCoordPointer(2, GL_FLOAT, 0, textureCoord);
979 glVertexPointer(2, GL_FLOAT, 0, vertexCoord);
981 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
983 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
984 glDisableClientState(GL_VERTEX_ARRAY);
985 glDisable(vgl->tex_target);
989 #ifdef SUPPORTS_SHADERS
990 static void DrawWithShaders(vout_display_opengl_t *vgl,
991 float *left, float *top, float *right, float *bottom,
994 vgl->UseProgram(vgl->program[program]);
996 if (vgl->chroma->plane_count == 3) {
997 vgl->Uniform4fv(vgl->GetUniformLocation(vgl->program[0], "Coefficient"), 4, vgl->local_value);
998 vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[0], "Texture0"), 0);
999 vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[0], "Texture1"), 1);
1000 vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[0], "Texture2"), 2);
1002 else if (vgl->chroma->plane_count == 1) {
1003 vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[0], "Texture0"), 0);
1006 vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[1], "Texture0"), 0);
1007 vgl->Uniform4f(vgl->GetUniformLocation(vgl->program[1], "FillColor"), 1.0f, 1.0f, 1.0f, 1.0f);
1010 static const GLfloat vertexCoord[] = {
1017 for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
1018 const GLfloat textureCoord[] = {
1022 right[j], bottom[j],
1024 glActiveTexture(GL_TEXTURE0+j);
1025 glClientActiveTexture(GL_TEXTURE0+j);
1026 glBindTexture(vgl->tex_target, vgl->texture[0][j]);
1029 snprintf(attribute, sizeof(attribute), "MultiTexCoord%1d", j);
1030 vgl->EnableVertexAttribArray(vgl->GetAttribLocation(vgl->program[program], attribute));
1031 vgl->VertexAttribPointer(vgl->GetAttribLocation(vgl->program[program], attribute), 2, GL_FLOAT, 0, 0, textureCoord);
1033 glActiveTexture(GL_TEXTURE0 + 0);
1034 glClientActiveTexture(GL_TEXTURE0 + 0);
1035 vgl->EnableVertexAttribArray(vgl->GetAttribLocation(vgl->program[program], "VertexPosition"));
1036 vgl->VertexAttribPointer(vgl->GetAttribLocation(vgl->program[program], "VertexPosition"), 2, GL_FLOAT, 0, 0, vertexCoord);
1038 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1042 int vout_display_opengl_Display(vout_display_opengl_t *vgl,
1043 const video_format_t *source)
1045 if (vlc_gl_Lock(vgl->gl))
1046 return VLC_EGENERIC;
1048 /* Why drawing here and not in Render()? Because this way, the
1049 OpenGL providers can call vout_display_opengl_Display to force redraw.i
1050 Currently, the OS X provider uses it to get a smooth window resizing */
1051 glClear(GL_COLOR_BUFFER_BIT);
1053 /* Draw the picture */
1054 float left[PICTURE_PLANE_MAX];
1055 float top[PICTURE_PLANE_MAX];
1056 float right[PICTURE_PLANE_MAX];
1057 float bottom[PICTURE_PLANE_MAX];
1058 for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
1059 /* glTexCoord works differently with GL_TEXTURE_2D and
1060 GL_TEXTURE_RECTANGLE_EXT */
1061 float scale_w, scale_h;
1063 if (vgl->tex_target == GL_TEXTURE_2D) {
1064 scale_w = (float)vgl->chroma->p[j].w.num / vgl->chroma->p[j].w.den / vgl->tex_width[j];
1065 scale_h = (float)vgl->chroma->p[j].h.num / vgl->chroma->p[j].h.den / vgl->tex_height[j];
1071 /* Warning: if NPOT is not supported a larger texture is
1072 allocated. This will cause right and bottom coordinates to
1073 land on the edge of two texels with the texels to the
1074 right/bottom uninitialized by the call to
1075 glTexSubImage2D. This might cause a green line to appear on
1076 the right/bottom of the display.
1077 There are two possible solutions:
1078 - Manually mirror the edges of the texture.
1079 - Add a "-1" when computing right and bottom, however the
1080 last row/column might not be displayed at all.
1082 left[j] = (source->i_x_offset + 0 ) * scale_w;
1083 top[j] = (source->i_y_offset + 0 ) * scale_h;
1084 right[j] = (source->i_x_offset + source->i_visible_width ) * scale_w;
1085 bottom[j] = (source->i_y_offset + source->i_visible_height) * scale_h;
1088 #ifdef SUPPORTS_SHADERS
1089 if (vgl->program[0] && (vgl->chroma->plane_count == 3 || vgl->chroma->plane_count == 1))
1090 DrawWithShaders(vgl, left, top, right, bottom, 0);
1091 else if (vgl->program[1] && vgl->chroma->plane_count == 1)
1092 DrawWithShaders(vgl, left, top, right, bottom, 1);
1096 #ifdef SUPPORTS_FIXED_PIPELINE
1097 DrawWithoutShaders(vgl, left, top, right, bottom);
1101 /* Draw the subpictures */
1102 if (vgl->program[1]) {
1103 #ifdef SUPPORTS_SHADERS
1104 // Change the program for overlays
1105 vgl->UseProgram(vgl->program[1]);
1106 vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[1], "Texture"), 0);
1110 #ifdef SUPPORTS_FIXED_PIPELINE
1111 glEnable(GL_TEXTURE_2D);
1114 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1116 glActiveTexture(GL_TEXTURE0 + 0);
1117 glClientActiveTexture(GL_TEXTURE0 + 0);
1118 for (int i = 0; i < vgl->region_count; i++) {
1119 gl_region_t *glr = &vgl->region[i];
1120 const GLfloat vertexCoord[] = {
1121 glr->left, glr->top,
1122 glr->left, glr->bottom,
1123 glr->right, glr->top,
1124 glr->right, glr->bottom,
1126 const GLfloat textureCoord[] = {
1128 0.0, glr->tex_height,
1129 glr->tex_width, 0.0,
1130 glr->tex_width, glr->tex_height,
1133 glBindTexture(GL_TEXTURE_2D, glr->texture);
1134 if (vgl->program[1]) {
1135 #ifdef SUPPORTS_SHADERS
1136 vgl->Uniform4f(vgl->GetUniformLocation(vgl->program[1], "FillColor"), 1.0f, 1.0f, 1.0f, glr->alpha);
1137 vgl->EnableVertexAttribArray(vgl->GetAttribLocation(vgl->program[1], "MultiTexCoord0"));
1138 vgl->VertexAttribPointer(vgl->GetAttribLocation(vgl->program[1], "MultiTexCoord0"), 2, GL_FLOAT, 0, 0, textureCoord);
1139 vgl->EnableVertexAttribArray(vgl->GetAttribLocation(vgl->program[1], "VertexPosition"));
1140 vgl->VertexAttribPointer(vgl->GetAttribLocation(vgl->program[1], "VertexPosition"), 2, GL_FLOAT, 0, 0, vertexCoord);
1143 #ifdef SUPPORTS_FIXED_PIPELINE
1144 glEnableClientState(GL_VERTEX_ARRAY);
1145 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1146 glColor4f(1.0f, 1.0f, 1.0f, glr->alpha);
1147 glTexCoordPointer(2, GL_FLOAT, 0, textureCoord);
1148 glVertexPointer(2, GL_FLOAT, 0, vertexCoord);
1152 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1154 if (!vgl->program[1]) {
1155 #ifdef SUPPORTS_FIXED_PIPELINE
1156 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1157 glDisableClientState(GL_VERTEX_ARRAY);
1161 glDisable(GL_BLEND);
1162 #ifdef SUPPORTS_FIXED_PIPELINE
1163 glDisable(GL_TEXTURE_2D);
1167 vlc_gl_Swap(vgl->gl);
1169 vlc_gl_Unlock(vgl->gl);