]> git.sesse.net Git - vlc/blob - modules/video_output/opengl.c
OpenGL: Addition of XYZ to RGB conversion in GLSL
[vlc] / modules / video_output / opengl.c
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
6  *
7  * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
8  *          Ilkka Ollakka <ileoo@videolan.org>
9  *          Rémi Denis-Courmont
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>
13  *
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.
18  *
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.
23  *
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  *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_picture_pool.h>
34 #include <vlc_subpicture.h>
35 #include <vlc_opengl.h>
36
37 #include "opengl.h"
38
39 #ifndef GL_CLAMP_TO_EDGE
40 # define GL_CLAMP_TO_EDGE 0x812F
41 #endif
42
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>
66 #endif
67 #endif
68
69 #if USE_OPENGL_ES
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)
77 #else
78 #   define SUPPORTS_FIXED_PIPELINE
79 #   define GL_MAX_TEXTURE_IMAGE_UNITS GL_MAX_TEXTURE_UNITS
80 #endif
81 #else
82 #   define GLSL_VERSION "120"
83 #   define VLCGL_TEXTURE_COUNT 1
84 #   define VLCGL_PICTURE_MAX 128
85 #   define PRECISION ""
86 #   define SUPPORTS_SHADERS
87 #   define SUPPORTS_FIXED_PIPELINE
88 #endif
89
90 static const vlc_fourcc_t gl_subpicture_chromas[] = {
91     VLC_CODEC_RGBA,
92     0
93 };
94
95 typedef struct {
96     GLuint   texture;
97     unsigned format;
98     unsigned type;
99     unsigned width;
100     unsigned height;
101
102     float    alpha;
103
104     float    top;
105     float    left;
106     float    bottom;
107     float    right;
108
109     float    tex_width;
110     float    tex_height;
111 } gl_region_t;
112
113 struct vout_display_opengl_t {
114
115     vlc_gl_t   *gl;
116
117     video_format_t fmt;
118     const vlc_chroma_description_t *chroma;
119
120     int        tex_target;
121     int        tex_format;
122     int        tex_internal;
123     int        tex_type;
124
125     int        tex_width[PICTURE_PLANE_MAX];
126     int        tex_height[PICTURE_PLANE_MAX];
127
128     GLuint     texture[VLCGL_TEXTURE_COUNT][PICTURE_PLANE_MAX];
129
130     int         region_count;
131     gl_region_t *region;
132
133
134     picture_pool_t *pool;
135
136     /* index 0 for normal and 1 for subtitle overlay */
137     GLuint     program[2];
138     GLint      shader[3]; //3. is for the common vertex shader
139     int        local_count;
140     GLfloat    local_value[16];
141
142     /* Shader variables commands*/
143 #ifdef SUPPORTS_SHADERS
144     PFNGLGETUNIFORMLOCATIONPROC      GetUniformLocation;
145     PFNGLGETATTRIBLOCATIONPROC       GetAttribLocation;
146     PFNGLVERTEXATTRIBPOINTERPROC     VertexAttribPointer;
147     PFNGLENABLEVERTEXATTRIBARRAYPROC EnableVertexAttribArray;
148
149     PFNGLUNIFORM4FVPROC   Uniform4fv;
150     PFNGLUNIFORM4FPROC    Uniform4f;
151     PFNGLUNIFORM1IPROC    Uniform1i;
152
153     /* Shader command */
154     PFNGLCREATESHADERPROC CreateShader;
155     PFNGLSHADERSOURCEPROC ShaderSource;
156     PFNGLCOMPILESHADERPROC CompileShader;
157     PFNGLDELETESHADERPROC   DeleteShader;
158
159     PFNGLCREATEPROGRAMPROC CreateProgram;
160     PFNGLLINKPROGRAMPROC   LinkProgram;
161     PFNGLUSEPROGRAMPROC    UseProgram;
162     PFNGLDELETEPROGRAMPROC DeleteProgram;
163
164     PFNGLATTACHSHADERPROC  AttachShader;
165
166     /* Shader log commands */
167     PFNGLGETPROGRAMIVPROC  GetProgramiv;
168     PFNGLGETPROGRAMINFOLOGPROC GetProgramInfoLog;
169     PFNGLGETSHADERIVPROC   GetShaderiv;
170     PFNGLGETSHADERINFOLOGPROC GetShaderInfoLog;
171 #endif
172
173
174     /* multitexture */
175     bool use_multitexture;
176
177     /* Non-power-of-2 texture size support */
178     bool supports_npot;
179
180     uint8_t *texture_temp_buf;
181     int      texture_temp_buf_size;
182 };
183
184 static inline int GetAlignedSize(unsigned size)
185 {
186     /* Return the smallest larger or equal power of 2 */
187     unsigned align = 1 << (8 * sizeof (unsigned) - clz(size));
188     return ((align >> 1) == size) ? size : align;
189 }
190
191 #if !USE_OPENGL_ES
192 static bool IsLuminance16Supported(int target)
193 {
194     GLuint texture;
195
196     glGenTextures(1, &texture);
197     glBindTexture(target, texture);
198     glTexImage2D(target, 0, GL_LUMINANCE16,
199                  64, 64, 0, GL_LUMINANCE, GL_UNSIGNED_SHORT, NULL);
200     GLint size = 0;
201     glGetTexLevelParameteriv(target, 0, GL_TEXTURE_LUMINANCE_SIZE, &size);
202
203     glDeleteTextures(1, &texture);
204
205     return size == 16;
206 }
207 #endif
208
209 #ifdef SUPPORTS_SHADERS
210 static void BuildVertexShader(vout_display_opengl_t *vgl,
211                               GLint *shader)
212 {
213     /* Basic vertex shader */
214     const char *vertexShader =
215         "#version " GLSL_VERSION "\n"
216         PRECISION
217         "varying vec4 TexCoord0,TexCoord1, TexCoord2;"
218         "attribute vec4 MultiTexCoord0,MultiTexCoord1,MultiTexCoord2;"
219         "attribute vec4 VertexPosition;"
220         "void main() {"
221         " TexCoord0 = MultiTexCoord0;"
222         " TexCoord1 = MultiTexCoord1;"
223         " TexCoord2 = MultiTexCoord2;"
224         " gl_Position = VertexPosition;"
225         "}";
226
227     *shader = vgl->CreateShader(GL_VERTEX_SHADER);
228     vgl->ShaderSource(*shader, 1, &vertexShader, NULL);
229     vgl->CompileShader(*shader);
230 }
231
232 static void BuildYUVFragmentShader(vout_display_opengl_t *vgl,
233                                    GLint *shader,
234                                    int *local_count,
235                                    GLfloat *local_value,
236                                    const video_format_t *fmt,
237                                    float yuv_range_correction)
238
239 {
240     /* [R/G/B][Y U V O] from TV range to full range
241      * XXX we could also do hue/brightness/constrast/gamma
242      * by simply changing the coefficients
243      */
244     const float matrix_bt601_tv2full[12] = {
245         1.164383561643836,  0.0000,             1.596026785714286, -0.874202217873451 ,
246         1.164383561643836, -0.391762290094914, -0.812967647237771,  0.531667823499146 ,
247         1.164383561643836,  2.017232142857142,  0.0000,            -1.085630789302022 ,
248     };
249     const float matrix_bt709_tv2full[12] = {
250         1.164383561643836,  0.0000,             1.792741071428571, -0.972945075016308 ,
251         1.164383561643836, -0.21324861427373,  -0.532909328559444,  0.301482665475862 ,
252         1.164383561643836,  2.112401785714286,  0.0000,            -1.133402217873451 ,
253     };
254     const float (*matrix) = fmt->i_height > 576 ? matrix_bt709_tv2full
255                                                 : matrix_bt601_tv2full;
256
257     /* Basic linear YUV -> RGB conversion using bilinear interpolation */
258     const char *template_glsl_yuv =
259         "#version " GLSL_VERSION "\n"
260         PRECISION
261         "uniform sampler2D Texture0;"
262         "uniform sampler2D Texture1;"
263         "uniform sampler2D Texture2;"
264         "uniform vec4      Coefficient[4];"
265         "varying vec4      TexCoord0,TexCoord1,TexCoord2;"
266
267         "void main(void) {"
268         " vec4 x,y,z,result;"
269         " x  = texture2D(Texture0, TexCoord0.st);"
270         " %c = texture2D(Texture1, TexCoord1.st);"
271         " %c = texture2D(Texture2, TexCoord2.st);"
272
273         " result = x * Coefficient[0] + Coefficient[3];"
274         " result = (y * Coefficient[1]) + result;"
275         " result = (z * Coefficient[2]) + result;"
276         " gl_FragColor = result;"
277         "}";
278     bool swap_uv = fmt->i_chroma == VLC_CODEC_YV12 ||
279                    fmt->i_chroma == VLC_CODEC_YV9;
280
281     char *code;
282     if (asprintf(&code, template_glsl_yuv,
283                  swap_uv ? 'z' : 'y',
284                  swap_uv ? 'y' : 'z') < 0)
285         code = NULL;
286
287     for (int i = 0; i < 4; i++) {
288         float correction = i < 3 ? yuv_range_correction : 1.0;
289         /* We place coefficient values for coefficient[4] in one array from matrix values.
290            Notice that we fill values from top down instead of left to right.*/
291         for (int j = 0; j < 4; j++)
292             local_value[*local_count + i*4+j] = j < 3 ? correction * matrix[j*4+i]
293                                                       : 0.0 ;
294     }
295     (*local_count) += 4;
296
297
298     *shader = vgl->CreateShader(GL_FRAGMENT_SHADER);
299     vgl->ShaderSource(*shader, 1, (const char **)&code, NULL);
300     vgl->CompileShader(*shader);
301
302     free(code);
303 }
304
305 #if 0
306 static void BuildRGBFragmentShader(vout_display_opengl_t *vgl,
307                                    GLint *shader)
308 {
309     // Simple shader for RGB
310     const char *code =
311         "#version " GLSL_VERSION "\n"
312         PRECISION
313         "uniform sampler2D Texture[3];"
314         "varying vec4 TexCoord0,TexCoord1,TexCoord2;"
315         "void main()"
316         "{ "
317         "  gl_FragColor = texture2D(Texture[0], TexCoord0.st);"
318         "}";
319     *shader = vgl->CreateShader(GL_FRAGMENT_SHADER);
320     vgl->ShaderSource(*shader, 1, &code, NULL);
321     vgl->CompileShader(*shader);
322 }
323 #endif
324
325 static void BuildRGBAFragmentShader(vout_display_opengl_t *vgl,
326                                    GLint *shader)
327 {
328     // Simple shader for RGBA
329     const char *code =
330         "#version " GLSL_VERSION "\n"
331         PRECISION
332         "uniform sampler2D Texture;"
333         "uniform vec4 FillColor;"
334         "varying vec4 TexCoord0;"
335         "void main()"
336         "{ "
337         "  gl_FragColor = texture2D(Texture, TexCoord0.st) * FillColor;"
338         "}";
339     *shader = vgl->CreateShader(GL_FRAGMENT_SHADER);
340     vgl->ShaderSource(*shader, 1, &code, NULL);
341     vgl->CompileShader(*shader);
342 }
343
344 static void BuildXYZFragmentShader(vout_display_opengl_t *vgl,
345                                    GLint *shader)
346 {
347     /* Shader for XYZ to RGB correction
348      * 3 steps :
349      *  - XYZ gamma correction
350      *  - XYZ to RGB matrix conversion
351      *  - reverse RGB gamma correction
352      */
353       const char *code =
354         "#version " GLSL_VERSION "\n"
355         PRECISION
356         "uniform sampler2D Texture0;"
357         "uniform vec4 xyz_gamma = vec4(2.6);"
358         "uniform vec4 rgb_gamma = vec4(1.0/2.2);"
359         // WARN: matrix Is filled column by column (not row !)
360         "uniform mat4 matrix_xyz_rgb = mat4("
361         "    3.240454 , -0.9692660, 0.0556434, 0.0,"
362         "   -1.5371385,  1.8760108, -0.2040259, 0.0,"
363         "    -0.4985314, 0.0415560, 1.0572252,  0.0,"
364         "    0.0,      0.0,         0.0,        1.0 "
365         " );"
366
367         "varying vec4 TexCoord0;"
368         "void main()"
369         "{ "
370         " vec4 v_in, v_out;"
371         " v_in  = texture2D(Texture0, TexCoord0.st);"
372         " v_in = pow(v_in, xyz_gamma);"
373         " v_out = matrix_xyz_rgb * v_in ;"
374         " v_out = pow(v_out, rgb_gamma) ;"
375         " v_out = clamp(v_out, 0.0, 1.0) ;"
376         " gl_FragColor = v_out;"
377         "}";
378     *shader = vgl->CreateShader(GL_FRAGMENT_SHADER);
379     vgl->ShaderSource(*shader, 1, &code, NULL);
380     vgl->CompileShader(*shader);
381 }
382
383 #endif
384
385 vout_display_opengl_t *vout_display_opengl_New(video_format_t *fmt,
386                                                const vlc_fourcc_t **subpicture_chromas,
387                                                vlc_gl_t *gl)
388 {
389     vout_display_opengl_t *vgl = calloc(1, sizeof(*vgl));
390     if (!vgl)
391         return NULL;
392
393     vgl->gl = gl;
394     if (vlc_gl_Lock(vgl->gl)) {
395         free(vgl);
396         return NULL;
397     }
398
399     if (vgl->gl->getProcAddress == NULL) {
400         fprintf(stderr, "getProcAddress not implemented, bailing out\n");
401         free(vgl);
402         return NULL;
403     }
404
405     const char *extensions = (const char *)glGetString(GL_EXTENSIONS);
406 #if !USE_OPENGL_ES
407     const unsigned char *ogl_version = glGetString(GL_VERSION);
408     bool supports_shaders = strverscmp((const char *)ogl_version, "2.0") >= 0;
409 #else
410     bool supports_shaders = false;
411 #endif
412
413 #if USE_OPENGL_ES == 2
414     vgl->CreateShader  = glCreateShader;
415     vgl->ShaderSource  = glShaderSource;
416     vgl->CompileShader = glCompileShader;
417     vgl->AttachShader  = glAttachShader;
418
419     vgl->GetProgramiv  = glGetProgramiv;
420     vgl->GetShaderiv   = glGetShaderiv;
421     vgl->GetProgramInfoLog  = glGetProgramInfoLog;
422     vgl->GetShaderInfoLog   = glGetShaderInfoLog;
423
424     vgl->DeleteShader  = glDeleteShader;
425
426     vgl->GetUniformLocation = glGetUniformLocation;
427     vgl->GetAttribLocation  = glGetAttribLocation;
428     vgl->VertexAttribPointer= glVertexAttribPointer;
429     vgl->EnableVertexAttribArray = glEnableVertexAttribArray;
430     vgl->Uniform4fv    = glUniform4fv;
431     vgl->Uniform4f     = glUniform4f;
432     vgl->Uniform1i     = glUniform1i;
433
434     vgl->CreateProgram = glCreateProgram;
435     vgl->LinkProgram   = glLinkProgram;
436     vgl->UseProgram    = glUseProgram;
437     vgl->DeleteProgram = glDeleteProgram;
438     supports_shaders = true;
439 #elif defined(SUPPORTS_SHADERS)
440     vgl->CreateShader  = (PFNGLCREATESHADERPROC)vlc_gl_GetProcAddress(vgl->gl, "glCreateShader");
441     vgl->ShaderSource  = (PFNGLSHADERSOURCEPROC)vlc_gl_GetProcAddress(vgl->gl, "glShaderSource");
442     vgl->CompileShader = (PFNGLCOMPILESHADERPROC)vlc_gl_GetProcAddress(vgl->gl, "glCompileShader");
443     vgl->AttachShader  = (PFNGLATTACHSHADERPROC)vlc_gl_GetProcAddress(vgl->gl, "glAttachShader");
444
445     vgl->GetProgramiv  = (PFNGLGETPROGRAMIVPROC)vlc_gl_GetProcAddress(vgl->gl, "glGetProgramiv");
446     vgl->GetShaderiv   = (PFNGLGETSHADERIVPROC)vlc_gl_GetProcAddress(vgl->gl, "glGetShaderiv");
447     vgl->GetProgramInfoLog  = (PFNGLGETPROGRAMINFOLOGPROC)vlc_gl_GetProcAddress(vgl->gl, "glGetProgramInfoLog");
448     vgl->GetShaderInfoLog   = (PFNGLGETSHADERINFOLOGPROC)vlc_gl_GetProcAddress(vgl->gl, "glGetShaderInfoLog");
449
450     vgl->DeleteShader  = (PFNGLDELETESHADERPROC)vlc_gl_GetProcAddress(vgl->gl, "glDeleteShader");
451
452     vgl->GetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC)vlc_gl_GetProcAddress(vgl->gl, "glGetUniformLocation");
453     vgl->GetAttribLocation  = (PFNGLGETATTRIBLOCATIONPROC)vlc_gl_GetProcAddress(vgl->gl, "glGetAttribLocation");
454     vgl->VertexAttribPointer= (PFNGLVERTEXATTRIBPOINTERPROC)vlc_gl_GetProcAddress(vgl->gl, "glVertexAttribPointer");
455     vgl->EnableVertexAttribArray = (PFNGLENABLEVERTEXATTRIBARRAYPROC)vlc_gl_GetProcAddress(vgl->gl, "glEnableVertexAttribArray");
456     vgl->Uniform4fv    = (PFNGLUNIFORM4FVPROC)vlc_gl_GetProcAddress(vgl->gl,"glUniform4fv");
457     vgl->Uniform4f     = (PFNGLUNIFORM4FPROC)vlc_gl_GetProcAddress(vgl->gl,"glUniform4f");
458     vgl->Uniform1i     = (PFNGLUNIFORM1IPROC)vlc_gl_GetProcAddress(vgl->gl,"glUniform1i");
459
460     vgl->CreateProgram = (PFNGLCREATEPROGRAMPROC)vlc_gl_GetProcAddress(vgl->gl, "glCreateProgram");
461     vgl->LinkProgram   = (PFNGLLINKPROGRAMPROC)vlc_gl_GetProcAddress(vgl->gl, "glLinkProgram");
462     vgl->UseProgram    = (PFNGLUSEPROGRAMPROC)vlc_gl_GetProcAddress(vgl->gl, "glUseProgram");
463     vgl->DeleteProgram = (PFNGLDELETEPROGRAMPROC)vlc_gl_GetProcAddress(vgl->gl, "glDeleteProgram");
464
465     if (!vgl->CreateShader || !vgl->ShaderSource || !vgl->CreateProgram)
466         supports_shaders = false;
467 #endif
468
469     vgl->supports_npot = HasExtension(extensions, "GL_ARB_texture_non_power_of_two") ||
470                          HasExtension(extensions, "GL_APPLE_texture_2D_limited_npot");
471
472 #if USE_OPENGL_ES == 2
473     /* OpenGL ES 2 includes support for non-power of 2 textures by specification
474      * so checks for extensions are bound to fail. Check for OpenGL ES version instead. */
475     vgl->supports_npot = true;
476 #endif
477
478     GLint max_texture_units = 0;
479     glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &max_texture_units);
480
481 #ifdef __APPLE__
482 #if USE_OPENGL_ES
483     /* work-around an iOS 6 bug */
484     if (kCFCoreFoundationVersionNumber >= 786.)
485         max_texture_units = 8;
486     supports_shaders = true;
487 #endif
488 #endif
489
490     /* Initialize with default chroma */
491     vgl->fmt = *fmt;
492     vgl->fmt.i_chroma = VLC_CODEC_RGB32;
493 #   if defined(WORDS_BIGENDIAN)
494     vgl->fmt.i_rmask  = 0xff000000;
495     vgl->fmt.i_gmask  = 0x00ff0000;
496     vgl->fmt.i_bmask  = 0x0000ff00;
497 #   else
498     vgl->fmt.i_rmask  = 0x000000ff;
499     vgl->fmt.i_gmask  = 0x0000ff00;
500     vgl->fmt.i_bmask  = 0x00ff0000;
501 #   endif
502     vgl->tex_target   = GL_TEXTURE_2D;
503     vgl->tex_format   = GL_RGBA;
504     vgl->tex_internal = GL_RGBA;
505     vgl->tex_type     = GL_UNSIGNED_BYTE;
506     /* Use YUV if possible and needed */
507     bool need_fs_yuv = false;
508     bool need_fs_xyz = false;
509     bool need_fs_rgba = USE_OPENGL_ES == 2;
510     float yuv_range_correction = 1.0;
511
512     if (max_texture_units >= 3 && supports_shaders && vlc_fourcc_IsYUV(fmt->i_chroma)) {
513         const vlc_fourcc_t *list = vlc_fourcc_GetYUVFallback(fmt->i_chroma);
514         while (*list) {
515             const vlc_chroma_description_t *dsc = vlc_fourcc_GetChromaDescription(*list);
516             if (dsc && dsc->plane_count == 3 && dsc->pixel_size == 1) {
517                 need_fs_yuv       = true;
518                 vgl->fmt          = *fmt;
519                 vgl->fmt.i_chroma = *list;
520                 vgl->tex_format   = GL_LUMINANCE;
521                 vgl->tex_internal = GL_LUMINANCE;
522                 vgl->tex_type     = GL_UNSIGNED_BYTE;
523                 yuv_range_correction = 1.0;
524                 break;
525 #if !USE_OPENGL_ES
526             } else if (dsc && dsc->plane_count == 3 && dsc->pixel_size == 2 &&
527                        IsLuminance16Supported(vgl->tex_target)) {
528                 need_fs_yuv       = true;
529                 vgl->fmt          = *fmt;
530                 vgl->fmt.i_chroma = *list;
531                 vgl->tex_format   = GL_LUMINANCE;
532                 vgl->tex_internal = GL_LUMINANCE16;
533                 vgl->tex_type     = GL_UNSIGNED_SHORT;
534                 yuv_range_correction = (float)((1 << 16) - 1) / ((1 << dsc->pixel_bits) - 1);
535                 break;
536 #endif
537             }
538             list++;
539         }
540     }
541
542     if (fmt->i_chroma == VLC_CODEC_XYZ12) {
543         vlc_fourcc_GetChromaDescription(fmt->i_chroma);
544         need_fs_xyz       = true;
545         vgl->fmt          = *fmt;
546         vgl->fmt.i_chroma = VLC_CODEC_XYZ12;
547         vgl->tex_format   = GL_RGB;
548         vgl->tex_internal = GL_RGB;
549         vgl->tex_type     = GL_UNSIGNED_SHORT;
550     }
551     vgl->chroma = vlc_fourcc_GetChromaDescription(vgl->fmt.i_chroma);
552     vgl->use_multitexture = vgl->chroma->plane_count > 1;
553
554     /* Texture size */
555     for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
556         int w = vgl->fmt.i_width  * vgl->chroma->p[j].w.num / vgl->chroma->p[j].w.den;
557         int h = vgl->fmt.i_height * vgl->chroma->p[j].h.num / vgl->chroma->p[j].h.den;
558         if (vgl->supports_npot) {
559             vgl->tex_width[j]  = w;
560             vgl->tex_height[j] = h;
561         } else {
562             vgl->tex_width[j]  = GetAlignedSize(w);
563             vgl->tex_height[j] = GetAlignedSize(h);
564         }
565     }
566
567     /* Build program if needed */
568     vgl->program[0] =
569     vgl->program[1] = 0;
570     vgl->shader[0] =
571     vgl->shader[1] =
572     vgl->shader[2] = -1;
573     vgl->local_count = 0;
574     if (supports_shaders && (need_fs_yuv || need_fs_xyz|| need_fs_rgba)) {
575 #ifdef SUPPORTS_SHADERS
576         if (need_fs_xyz)
577             BuildXYZFragmentShader(vgl, &vgl->shader[0]);
578         else
579             BuildYUVFragmentShader(vgl, &vgl->shader[0], &vgl->local_count,
580                                 vgl->local_value, fmt, yuv_range_correction);
581
582         BuildRGBAFragmentShader(vgl, &vgl->shader[1]);
583         BuildVertexShader(vgl, &vgl->shader[2]);
584
585         /* Check shaders messages */
586         for (unsigned j = 0; j < 3; j++) {
587             int infoLength;
588             vgl->GetShaderiv(vgl->shader[j], GL_INFO_LOG_LENGTH, &infoLength);
589             if (infoLength <= 1)
590                 continue;
591
592             char *infolog = malloc(infoLength);
593             int charsWritten;
594             vgl->GetShaderInfoLog(vgl->shader[j], infoLength, &charsWritten, infolog);
595             fprintf(stderr, "shader %d: %s\n", j, infolog);
596             free(infolog);
597         }
598
599         vgl->program[0] = vgl->CreateProgram();
600         vgl->AttachShader(vgl->program[0], vgl->shader[0]);
601         vgl->AttachShader(vgl->program[0], vgl->shader[2]);
602         vgl->LinkProgram(vgl->program[0]);
603
604         vgl->program[1] = vgl->CreateProgram();
605         vgl->AttachShader(vgl->program[1], vgl->shader[1]);
606         vgl->AttachShader(vgl->program[1], vgl->shader[2]);
607         vgl->LinkProgram(vgl->program[1]);
608
609         /* Check program messages */
610         for (GLuint i = 0; i < 2; i++) {
611             int infoLength = 0;
612             vgl->GetProgramiv(vgl->program[i], GL_INFO_LOG_LENGTH, &infoLength);
613             if (infoLength <= 1)
614                 continue;
615             char *infolog = malloc(infoLength);
616             int charsWritten;
617             vgl->GetProgramInfoLog(vgl->program[i], infoLength, &charsWritten, infolog);
618             fprintf(stderr, "shader program %d: %s\n", i, infolog);
619             free(infolog);
620
621             /* If there is some message, better to check linking is ok */
622             GLint link_status = GL_TRUE;
623             vgl->GetProgramiv(vgl->program[i], GL_LINK_STATUS, &link_status);
624             if (link_status == GL_FALSE) {
625                 fprintf(stderr, "Unable to use program %d\n", i);
626                 free(vgl);
627                 return NULL;
628             }
629         }
630 #else
631         (void)yuv_range_correction;
632 #endif
633     }
634
635     /* */
636     glDisable(GL_BLEND);
637     glDisable(GL_DEPTH_TEST);
638     glDepthMask(GL_FALSE);
639     glDisable(GL_CULL_FACE);
640     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
641     glClear(GL_COLOR_BUFFER_BIT);
642
643     vlc_gl_Unlock(vgl->gl);
644
645     /* */
646     for (int i = 0; i < VLCGL_TEXTURE_COUNT; i++) {
647         for (int j = 0; j < PICTURE_PLANE_MAX; j++)
648             vgl->texture[i][j] = 0;
649     }
650     vgl->region_count = 0;
651     vgl->region = NULL;
652     vgl->pool = NULL;
653
654     *fmt = vgl->fmt;
655     if (subpicture_chromas) {
656         *subpicture_chromas = gl_subpicture_chromas;
657     }
658     return vgl;
659 }
660
661 void vout_display_opengl_Delete(vout_display_opengl_t *vgl)
662 {
663     /* */
664     if (!vlc_gl_Lock(vgl->gl)) {
665         glFinish();
666         glFlush();
667         for (int i = 0; i < VLCGL_TEXTURE_COUNT; i++)
668             glDeleteTextures(vgl->chroma->plane_count, vgl->texture[i]);
669         for (int i = 0; i < vgl->region_count; i++) {
670             if (vgl->region[i].texture)
671                 glDeleteTextures(1, &vgl->region[i].texture);
672         }
673         free(vgl->region);
674
675 #ifdef SUPPORTS_SHADERS
676         if (vgl->program[0]) {
677             for (int i = 0; i < 2; i++)
678                 vgl->DeleteProgram(vgl->program[i]);
679             for (int i = 0; i < 3; i++)
680                 vgl->DeleteShader(vgl->shader[i]);
681         }
682 #endif
683
684         free(vgl->texture_temp_buf);
685         vlc_gl_Unlock(vgl->gl);
686     }
687     if (vgl->pool)
688         picture_pool_Delete(vgl->pool);
689     free(vgl);
690 }
691
692 picture_pool_t *vout_display_opengl_GetPool(vout_display_opengl_t *vgl, unsigned requested_count)
693 {
694     if (vgl->pool)
695         return vgl->pool;
696
697     /* Allocate our pictures */
698     picture_t *picture[VLCGL_PICTURE_MAX] = {NULL, };
699     unsigned count;
700
701     for (count = 0; count < __MIN(VLCGL_PICTURE_MAX, requested_count); count++) {
702         picture[count] = picture_NewFromFormat(&vgl->fmt);
703         if (!picture[count])
704             break;
705     }
706     if (count <= 0)
707         return NULL;
708
709     /* Wrap the pictures into a pool */
710     picture_pool_configuration_t cfg;
711     memset(&cfg, 0, sizeof(cfg));
712     cfg.picture_count = count;
713     cfg.picture       = picture;
714     vgl->pool = picture_pool_NewExtended(&cfg);
715     if (!vgl->pool)
716         goto error;
717
718     /* Allocates our textures */
719     if (vlc_gl_Lock(vgl->gl))
720         return vgl->pool;
721
722     for (int i = 0; i < VLCGL_TEXTURE_COUNT; i++) {
723         glGenTextures(vgl->chroma->plane_count, vgl->texture[i]);
724         for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
725             if (vgl->use_multitexture) {
726                 glActiveTexture(GL_TEXTURE0 + j);
727                 glClientActiveTexture(GL_TEXTURE0 + j);
728             }
729             glBindTexture(vgl->tex_target, vgl->texture[i][j]);
730
731 #if !USE_OPENGL_ES
732             /* Set the texture parameters */
733             glTexParameterf(vgl->tex_target, GL_TEXTURE_PRIORITY, 1.0);
734             glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
735 #endif
736
737             glTexParameteri(vgl->tex_target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
738             glTexParameteri(vgl->tex_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
739             glTexParameteri(vgl->tex_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
740             glTexParameteri(vgl->tex_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
741
742             /* Call glTexImage2D only once, and use glTexSubImage2D later */
743             glTexImage2D(vgl->tex_target, 0,
744                          vgl->tex_internal, vgl->tex_width[j], vgl->tex_height[j],
745                          0, vgl->tex_format, vgl->tex_type, NULL);
746         }
747     }
748
749     vlc_gl_Unlock(vgl->gl);
750
751     return vgl->pool;
752
753 error:
754     for (unsigned i = 0; i < count; i++)
755         picture_Release(picture[i]);
756     return NULL;
757 }
758
759 #define ALIGN(x, y) (((x) + ((y) - 1)) & ~((y) - 1))
760 static void Upload(vout_display_opengl_t *vgl, int in_width, int in_height,
761                    int in_full_width, int in_full_height,
762                    int w_num, int w_den, int h_num, int h_den,
763                    int pitch, int pixel_pitch,
764                    int full_upload, const uint8_t *pixels,
765                    int tex_target, int tex_format, int tex_type)
766 {
767     int width       =       in_width * w_num / w_den;
768     int full_width  =  in_full_width * w_num / w_den;
769     int height      =      in_height * h_num / h_den;
770     int full_height = in_full_height * h_num / h_den;
771     // This unpack alignment is the default, but setting it just in case.
772     glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
773 #ifndef GL_UNPACK_ROW_LENGTH
774     int dst_width = full_upload ? full_width : width;
775     int dst_pitch = ALIGN(dst_width * pixel_pitch, 4);
776     if ( pitch != dst_pitch )
777     {
778         int buf_size = dst_pitch * full_height * pixel_pitch;
779         const uint8_t *source = pixels;
780         uint8_t *destination;
781         if( !vgl->texture_temp_buf || vgl->texture_temp_buf_size < buf_size )
782         {
783             free( vgl->texture_temp_buf );
784             vgl->texture_temp_buf = xmalloc( buf_size );
785             vgl->texture_temp_buf_size = buf_size;
786         }
787         destination = vgl->texture_temp_buf;
788
789         for( int h = 0; h < height ; h++ )
790         {
791             memcpy( destination, source, width * pixel_pitch );
792             source += pitch;
793             destination += dst_pitch;
794         }
795         if (full_upload)
796             glTexImage2D( tex_target, 0, tex_format,
797                           full_width, full_height,
798                           0, tex_format, tex_type, vgl->texture_temp_buf );
799         else
800             glTexSubImage2D( tex_target, 0,
801                              0, 0,
802                              width, height,
803                              tex_format, tex_type, vgl->texture_temp_buf );
804     } else {
805 #else
806     (void) width;
807     (void) height;
808     (void) vgl;
809     {
810         glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch / pixel_pitch);
811 #endif
812         if (full_upload)
813             glTexImage2D(tex_target, 0, tex_format,
814                          full_width, full_height,
815                          0, tex_format, tex_type, pixels);
816         else
817             glTexSubImage2D(tex_target, 0,
818                             0, 0,
819                             width, height,
820                             tex_format, tex_type, pixels);
821     }
822 }
823
824 int vout_display_opengl_Prepare(vout_display_opengl_t *vgl,
825                                 picture_t *picture, subpicture_t *subpicture)
826 {
827     if (vlc_gl_Lock(vgl->gl))
828         return VLC_EGENERIC;
829
830     /* Update the texture */
831     for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
832         if (vgl->use_multitexture) {
833             glActiveTexture(GL_TEXTURE0 + j);
834             glClientActiveTexture(GL_TEXTURE0 + j);
835         }
836         glBindTexture(vgl->tex_target, vgl->texture[0][j]);
837
838         Upload(vgl, picture->format.i_visible_width, vgl->fmt.i_visible_height,
839                vgl->fmt.i_width, vgl->fmt.i_height,
840                vgl->chroma->p[j].w.num, vgl->chroma->p[j].w.den, vgl->chroma->p[j].h.num, vgl->chroma->p[j].h.den,
841                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);
842     }
843
844     int         last_count = vgl->region_count;
845     gl_region_t *last = vgl->region;
846
847     vgl->region_count = 0;
848     vgl->region       = NULL;
849
850     if (subpicture) {
851
852         int count = 0;
853         for (subpicture_region_t *r = subpicture->p_region; r; r = r->p_next)
854             count++;
855
856         vgl->region_count = count;
857         vgl->region       = calloc(count, sizeof(*vgl->region));
858
859         if (vgl->use_multitexture) {
860             glActiveTexture(GL_TEXTURE0 + 0);
861             glClientActiveTexture(GL_TEXTURE0 + 0);
862         }
863         int i = 0;
864         for (subpicture_region_t *r = subpicture->p_region; r; r = r->p_next, i++) {
865             gl_region_t *glr = &vgl->region[i];
866
867             glr->format = GL_RGBA;
868             glr->type   = GL_UNSIGNED_BYTE;
869             glr->width  = r->fmt.i_visible_width;
870             glr->height = r->fmt.i_visible_height;
871             if (!vgl->supports_npot) {
872                 glr->width  = GetAlignedSize(glr->width);
873                 glr->height = GetAlignedSize(glr->height);
874                 glr->tex_width  = (float) r->fmt.i_visible_width  / glr->width;
875                 glr->tex_height = (float) r->fmt.i_visible_height / glr->height;
876             } else {
877                 glr->tex_width  = 1.0;
878                 glr->tex_height = 1.0;
879             }
880             glr->alpha  = (float)subpicture->i_alpha * r->i_alpha / 255 / 255;
881             glr->left   =  2.0 * (r->i_x                          ) / subpicture->i_original_picture_width  - 1.0;
882             glr->top    = -2.0 * (r->i_y                          ) / subpicture->i_original_picture_height + 1.0;
883             glr->right  =  2.0 * (r->i_x + r->fmt.i_visible_width ) / subpicture->i_original_picture_width  - 1.0;
884             glr->bottom = -2.0 * (r->i_y + r->fmt.i_visible_height) / subpicture->i_original_picture_height + 1.0;
885
886             glr->texture = 0;
887             for (int j = 0; j < last_count; j++) {
888                 if (last[j].texture &&
889                     last[j].width  == glr->width &&
890                     last[j].height == glr->height &&
891                     last[j].format == glr->format &&
892                     last[j].type   == glr->type) {
893                     glr->texture = last[j].texture;
894                     memset(&last[j], 0, sizeof(last[j]));
895                     break;
896                 }
897             }
898
899             const int pixels_offset = r->fmt.i_y_offset * r->p_picture->p->i_pitch +
900                                       r->fmt.i_x_offset * r->p_picture->p->i_pixel_pitch;
901             if (glr->texture) {
902                 glBindTexture(GL_TEXTURE_2D, glr->texture);
903                 Upload(vgl, r->fmt.i_visible_width, r->fmt.i_visible_height, glr->width, glr->height, 1, 1, 1, 1,
904                        r->p_picture->p->i_pitch, r->p_picture->p->i_pixel_pitch, 0,
905                        &r->p_picture->p->p_pixels[pixels_offset], GL_TEXTURE_2D, glr->format, glr->type);
906             } else {
907                 glGenTextures(1, &glr->texture);
908                 glBindTexture(GL_TEXTURE_2D, glr->texture);
909 #if !USE_OPENGL_ES
910                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_PRIORITY, 1.0);
911                 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
912 #endif
913                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
914                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
915                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
916                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
917                 Upload(vgl, r->fmt.i_visible_width, r->fmt.i_visible_height, glr->width, glr->height, 1, 1, 1, 1,
918                        r->p_picture->p->i_pitch, r->p_picture->p->i_pixel_pitch, 1,
919                        &r->p_picture->p->p_pixels[pixels_offset], GL_TEXTURE_2D, glr->format, glr->type);
920             }
921         }
922     }
923     for (int i = 0; i < last_count; i++) {
924         if (last[i].texture)
925             glDeleteTextures(1, &last[i].texture);
926     }
927     free(last);
928
929     vlc_gl_Unlock(vgl->gl);
930     VLC_UNUSED(subpicture);
931     return VLC_SUCCESS;
932 }
933
934 #ifdef SUPPORTS_FIXED_PIPELINE
935 static void DrawWithoutShaders(vout_display_opengl_t *vgl,
936                                float *left, float *top, float *right, float *bottom)
937 {
938     static const GLfloat vertexCoord[] = {
939         -1.0f, -1.0f,
940          1.0f, -1.0f,
941         -1.0f,  1.0f,
942          1.0f,  1.0f,
943     };
944
945     const GLfloat textureCoord[] = {
946         left[0],  bottom[0],
947         right[0], bottom[0],
948         left[0],  top[0],
949         right[0], top[0]
950     };
951
952     glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
953     glEnable(vgl->tex_target);
954     glActiveTexture(GL_TEXTURE0 + 0);
955     glClientActiveTexture(GL_TEXTURE0 + 0);
956
957     glBindTexture(vgl->tex_target, vgl->texture[0][0]);
958
959     glEnableClientState(GL_VERTEX_ARRAY);
960     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
961
962     glTexCoordPointer(2, GL_FLOAT, 0, textureCoord);
963     glVertexPointer(2, GL_FLOAT, 0, vertexCoord);
964
965     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
966
967     glDisableClientState(GL_TEXTURE_COORD_ARRAY);
968     glDisableClientState(GL_VERTEX_ARRAY);
969     glDisable(vgl->tex_target);
970 }
971 #endif
972
973 #ifdef SUPPORTS_SHADERS
974 static void DrawWithShaders(vout_display_opengl_t *vgl,
975                             float *left, float *top, float *right, float *bottom,
976                             int program)
977 {
978     vgl->UseProgram(vgl->program[program]);
979     if (program == 0) {
980         if (vgl->chroma->plane_count == 3) {
981             vgl->Uniform4fv(vgl->GetUniformLocation(vgl->program[0], "Coefficient"), 4, vgl->local_value);
982             vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[0], "Texture0"), 0);
983             vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[0], "Texture1"), 1);
984             vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[0], "Texture2"), 2);
985         }
986         else if (vgl->chroma->plane_count == 1) {
987             vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[0], "Texture0"), 0);
988         }
989     } else {
990         vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[1], "Texture0"), 0);
991         vgl->Uniform4f(vgl->GetUniformLocation(vgl->program[1], "FillColor"), 1.0f, 1.0f, 1.0f, 1.0f);
992     }
993
994     static const GLfloat vertexCoord[] = {
995         -1.0,  1.0,
996         -1.0, -1.0,
997          1.0,  1.0,
998          1.0, -1.0,
999     };
1000
1001     for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
1002         const GLfloat textureCoord[] = {
1003             left[j],  top[j],
1004             left[j],  bottom[j],
1005             right[j], top[j],
1006             right[j], bottom[j],
1007         };
1008         glActiveTexture(GL_TEXTURE0+j);
1009         glClientActiveTexture(GL_TEXTURE0+j);
1010         glBindTexture(vgl->tex_target, vgl->texture[0][j]);
1011
1012         char attribute[20];
1013         snprintf(attribute, sizeof(attribute), "MultiTexCoord%1d", j);
1014         vgl->EnableVertexAttribArray(vgl->GetAttribLocation(vgl->program[program], attribute));
1015         vgl->VertexAttribPointer(vgl->GetAttribLocation(vgl->program[program], attribute), 2, GL_FLOAT, 0, 0, textureCoord);
1016     }
1017     glActiveTexture(GL_TEXTURE0 + 0);
1018     glClientActiveTexture(GL_TEXTURE0 + 0);
1019     vgl->EnableVertexAttribArray(vgl->GetAttribLocation(vgl->program[program], "VertexPosition"));
1020     vgl->VertexAttribPointer(vgl->GetAttribLocation(vgl->program[program], "VertexPosition"), 2, GL_FLOAT, 0, 0, vertexCoord);
1021
1022     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1023 }
1024 #endif
1025
1026 int vout_display_opengl_Display(vout_display_opengl_t *vgl,
1027                                 const video_format_t *source)
1028 {
1029     if (vlc_gl_Lock(vgl->gl))
1030         return VLC_EGENERIC;
1031
1032     /* Why drawing here and not in Render()? Because this way, the
1033        OpenGL providers can call vout_display_opengl_Display to force redraw.i
1034        Currently, the OS X provider uses it to get a smooth window resizing */
1035     glClear(GL_COLOR_BUFFER_BIT);
1036
1037     /* Draw the picture */
1038     float left[PICTURE_PLANE_MAX];
1039     float top[PICTURE_PLANE_MAX];
1040     float right[PICTURE_PLANE_MAX];
1041     float bottom[PICTURE_PLANE_MAX];
1042     for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
1043         /* glTexCoord works differently with GL_TEXTURE_2D and
1044            GL_TEXTURE_RECTANGLE_EXT */
1045         float scale_w, scale_h;
1046
1047         if (vgl->tex_target == GL_TEXTURE_2D) {
1048             scale_w = (float)vgl->chroma->p[j].w.num / vgl->chroma->p[j].w.den / vgl->tex_width[j];
1049             scale_h = (float)vgl->chroma->p[j].h.num / vgl->chroma->p[j].h.den / vgl->tex_height[j];
1050
1051         } else {
1052             scale_w = 1.0;
1053             scale_h = 1.0;
1054         }
1055         left[j]   = (source->i_x_offset +                       0 ) * scale_w;
1056         top[j]    = (source->i_y_offset +                       0 ) * scale_h;
1057         right[j]  = (source->i_x_offset + source->i_visible_width ) * scale_w;
1058         bottom[j] = (source->i_y_offset + source->i_visible_height) * scale_h;
1059     }
1060
1061 #ifdef SUPPORTS_SHADERS
1062     if (vgl->program[0] && (vgl->chroma->plane_count == 3 || vgl->chroma->plane_count == 1))
1063         DrawWithShaders(vgl, left, top, right, bottom, 0);
1064     else if (vgl->program[1] && vgl->chroma->plane_count == 1)
1065         DrawWithShaders(vgl, left, top, right, bottom, 1);
1066     else
1067 #endif
1068     {
1069 #ifdef SUPPORTS_FIXED_PIPELINE
1070         DrawWithoutShaders(vgl, left, top, right, bottom);
1071 #endif
1072     }
1073
1074     /* Draw the subpictures */
1075     if (vgl->program[1]) {
1076 #ifdef SUPPORTS_SHADERS
1077         // Change the program for overlays
1078         vgl->UseProgram(vgl->program[1]);
1079         vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[1], "Texture"), 0);
1080 #endif
1081     }
1082
1083 #ifdef SUPPORTS_FIXED_PIPELINE
1084     glEnable(GL_TEXTURE_2D);
1085 #endif
1086     glEnable(GL_BLEND);
1087     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1088
1089     glActiveTexture(GL_TEXTURE0 + 0);
1090     glClientActiveTexture(GL_TEXTURE0 + 0);
1091     for (int i = 0; i < vgl->region_count; i++) {
1092         gl_region_t *glr = &vgl->region[i];
1093         const GLfloat vertexCoord[] = {
1094             glr->left,  glr->top,
1095             glr->left,  glr->bottom,
1096             glr->right, glr->top,
1097             glr->right, glr->bottom,
1098         };
1099         const GLfloat textureCoord[] = {
1100             0.0, 0.0,
1101             0.0, glr->tex_height,
1102             glr->tex_width, 0.0,
1103             glr->tex_width, glr->tex_height,
1104         };
1105
1106         glBindTexture(GL_TEXTURE_2D, glr->texture);
1107         if (vgl->program[1]) {
1108 #ifdef SUPPORTS_SHADERS
1109             vgl->Uniform4f(vgl->GetUniformLocation(vgl->program[1], "FillColor"), 1.0f, 1.0f, 1.0f, glr->alpha);
1110             vgl->EnableVertexAttribArray(vgl->GetAttribLocation(vgl->program[1], "MultiTexCoord0"));
1111             vgl->VertexAttribPointer(vgl->GetAttribLocation(vgl->program[1], "MultiTexCoord0"), 2, GL_FLOAT, 0, 0, textureCoord);
1112             vgl->EnableVertexAttribArray(vgl->GetAttribLocation(vgl->program[1], "VertexPosition"));
1113             vgl->VertexAttribPointer(vgl->GetAttribLocation(vgl->program[1], "VertexPosition"), 2, GL_FLOAT, 0, 0, vertexCoord);
1114 #endif
1115         } else {
1116 #ifdef SUPPORTS_FIXED_PIPELINE
1117             glEnableClientState(GL_VERTEX_ARRAY);
1118             glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1119             glColor4f(1.0f, 1.0f, 1.0f, glr->alpha);
1120             glTexCoordPointer(2, GL_FLOAT, 0, textureCoord);
1121             glVertexPointer(2, GL_FLOAT, 0, vertexCoord);
1122 #endif
1123         }
1124
1125         glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1126
1127         if (!vgl->program[1]) {
1128 #ifdef SUPPORTS_FIXED_PIPELINE
1129             glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1130             glDisableClientState(GL_VERTEX_ARRAY);
1131 #endif
1132         }
1133     }
1134     glDisable(GL_BLEND);
1135 #ifdef SUPPORTS_FIXED_PIPELINE
1136     glDisable(GL_TEXTURE_2D);
1137 #endif
1138
1139     /* Display */
1140     vlc_gl_Swap(vgl->gl);
1141
1142     vlc_gl_Unlock(vgl->gl);
1143     return VLC_SUCCESS;
1144 }
1145