]> git.sesse.net Git - vlc/blob - modules/video_output/opengl.c
opengl: allocate the texture size using the visible dimensions of the format.
[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 #if defined(_WIN32)
174     PFNGLACTIVETEXTUREPROC  ActiveTexture;
175     PFNGLCLIENTACTIVETEXTUREPROC  ClientActiveTexture;
176 #endif
177
178
179     /* multitexture */
180     bool use_multitexture;
181
182     /* Non-power-of-2 texture size support */
183     bool supports_npot;
184
185     uint8_t *texture_temp_buf;
186     int      texture_temp_buf_size;
187 };
188
189 static inline int GetAlignedSize(unsigned size)
190 {
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;
194 }
195
196 #if !USE_OPENGL_ES
197 static bool IsLuminance16Supported(int target)
198 {
199     GLuint texture;
200
201     glGenTextures(1, &texture);
202     glBindTexture(target, texture);
203     glTexImage2D(target, 0, GL_LUMINANCE16,
204                  64, 64, 0, GL_LUMINANCE, GL_UNSIGNED_SHORT, NULL);
205     GLint size = 0;
206     glGetTexLevelParameteriv(target, 0, GL_TEXTURE_LUMINANCE_SIZE, &size);
207
208     glDeleteTextures(1, &texture);
209
210     return size == 16;
211 }
212 #endif
213
214 #ifdef SUPPORTS_SHADERS
215 static void BuildVertexShader(vout_display_opengl_t *vgl,
216                               GLint *shader)
217 {
218     /* Basic vertex shader */
219     const char *vertexShader =
220         "#version " GLSL_VERSION "\n"
221         PRECISION
222         "varying vec4 TexCoord0,TexCoord1, TexCoord2;"
223         "attribute vec4 MultiTexCoord0,MultiTexCoord1,MultiTexCoord2;"
224         "attribute vec4 VertexPosition;"
225         "void main() {"
226         " TexCoord0 = MultiTexCoord0;"
227         " TexCoord1 = MultiTexCoord1;"
228         " TexCoord2 = MultiTexCoord2;"
229         " gl_Position = VertexPosition;"
230         "}";
231
232     *shader = vgl->CreateShader(GL_VERTEX_SHADER);
233     vgl->ShaderSource(*shader, 1, &vertexShader, NULL);
234     vgl->CompileShader(*shader);
235 }
236
237 static void BuildYUVFragmentShader(vout_display_opengl_t *vgl,
238                                    GLint *shader,
239                                    int *local_count,
240                                    GLfloat *local_value,
241                                    const video_format_t *fmt,
242                                    float yuv_range_correction)
243
244 {
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
248      */
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 ,
253     };
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 ,
258     };
259     const float (*matrix) = fmt->i_height > 576 ? matrix_bt709_tv2full
260                                                 : matrix_bt601_tv2full;
261
262     /* Basic linear YUV -> RGB conversion using bilinear interpolation */
263     const char *template_glsl_yuv =
264         "#version " GLSL_VERSION "\n"
265         PRECISION
266         "uniform sampler2D Texture0;"
267         "uniform sampler2D Texture1;"
268         "uniform sampler2D Texture2;"
269         "uniform vec4      Coefficient[4];"
270         "varying vec4      TexCoord0,TexCoord1,TexCoord2;"
271
272         "void main(void) {"
273         " vec4 x,y,z,result;"
274         " x  = texture2D(Texture0, TexCoord0.st);"
275         " %c = texture2D(Texture1, TexCoord1.st);"
276         " %c = texture2D(Texture2, TexCoord2.st);"
277
278         " result = x * Coefficient[0] + Coefficient[3];"
279         " result = (y * Coefficient[1]) + result;"
280         " result = (z * Coefficient[2]) + result;"
281         " gl_FragColor = result;"
282         "}";
283     bool swap_uv = fmt->i_chroma == VLC_CODEC_YV12 ||
284                    fmt->i_chroma == VLC_CODEC_YV9;
285
286     char *code;
287     if (asprintf(&code, template_glsl_yuv,
288                  swap_uv ? 'z' : 'y',
289                  swap_uv ? 'y' : 'z') < 0)
290         code = NULL;
291
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]
298                                                       : 0.0 ;
299     }
300     (*local_count) += 4;
301
302
303     *shader = vgl->CreateShader(GL_FRAGMENT_SHADER);
304     vgl->ShaderSource(*shader, 1, (const char **)&code, NULL);
305     vgl->CompileShader(*shader);
306
307     free(code);
308 }
309
310 #if 0
311 static void BuildRGBFragmentShader(vout_display_opengl_t *vgl,
312                                    GLint *shader)
313 {
314     // Simple shader for RGB
315     const char *code =
316         "#version " GLSL_VERSION "\n"
317         PRECISION
318         "uniform sampler2D Texture[3];"
319         "varying vec4 TexCoord0,TexCoord1,TexCoord2;"
320         "void main()"
321         "{ "
322         "  gl_FragColor = texture2D(Texture[0], TexCoord0.st);"
323         "}";
324     *shader = vgl->CreateShader(GL_FRAGMENT_SHADER);
325     vgl->ShaderSource(*shader, 1, &code, NULL);
326     vgl->CompileShader(*shader);
327 }
328 #endif
329
330 static void BuildRGBAFragmentShader(vout_display_opengl_t *vgl,
331                                    GLint *shader)
332 {
333     // Simple shader for RGBA
334     const char *code =
335         "#version " GLSL_VERSION "\n"
336         PRECISION
337         "uniform sampler2D Texture;"
338         "uniform vec4 FillColor;"
339         "varying vec4 TexCoord0;"
340         "void main()"
341         "{ "
342         "  gl_FragColor = texture2D(Texture, TexCoord0.st) * FillColor;"
343         "}";
344     *shader = vgl->CreateShader(GL_FRAGMENT_SHADER);
345     vgl->ShaderSource(*shader, 1, &code, NULL);
346     vgl->CompileShader(*shader);
347 }
348
349 static void BuildXYZFragmentShader(vout_display_opengl_t *vgl,
350                                    GLint *shader)
351 {
352     /* Shader for XYZ to RGB correction
353      * 3 steps :
354      *  - XYZ gamma correction
355      *  - XYZ to RGB matrix conversion
356      *  - reverse RGB gamma correction
357      */
358       const char *code =
359         "#version " GLSL_VERSION "\n"
360         PRECISION
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 "
370         " );"
371
372         "varying vec4 TexCoord0;"
373         "void main()"
374         "{ "
375         " vec4 v_in, v_out;"
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;"
382         "}";
383     *shader = vgl->CreateShader(GL_FRAGMENT_SHADER);
384     vgl->ShaderSource(*shader, 1, &code, NULL);
385     vgl->CompileShader(*shader);
386 }
387
388 #endif
389
390 vout_display_opengl_t *vout_display_opengl_New(video_format_t *fmt,
391                                                const vlc_fourcc_t **subpicture_chromas,
392                                                vlc_gl_t *gl)
393 {
394     vout_display_opengl_t *vgl = calloc(1, sizeof(*vgl));
395     if (!vgl)
396         return NULL;
397
398     vgl->gl = gl;
399     if (vlc_gl_Lock(vgl->gl)) {
400         free(vgl);
401         return NULL;
402     }
403
404     if (vgl->gl->getProcAddress == NULL) {
405         fprintf(stderr, "getProcAddress not implemented, bailing out\n");
406         free(vgl);
407         return NULL;
408     }
409
410     const char *extensions = (const char *)glGetString(GL_EXTENSIONS);
411 #if !USE_OPENGL_ES
412     const unsigned char *ogl_version = glGetString(GL_VERSION);
413     bool supports_shaders = strverscmp((const char *)ogl_version, "2.0") >= 0;
414 #else
415     bool supports_shaders = false;
416 #endif
417
418 #if USE_OPENGL_ES == 2
419     vgl->CreateShader  = glCreateShader;
420     vgl->ShaderSource  = glShaderSource;
421     vgl->CompileShader = glCompileShader;
422     vgl->AttachShader  = glAttachShader;
423
424     vgl->GetProgramiv  = glGetProgramiv;
425     vgl->GetShaderiv   = glGetShaderiv;
426     vgl->GetProgramInfoLog  = glGetProgramInfoLog;
427     vgl->GetShaderInfoLog   = glGetShaderInfoLog;
428
429     vgl->DeleteShader  = glDeleteShader;
430
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;
438
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");
449
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");
454
455     vgl->DeleteShader  = (PFNGLDELETESHADERPROC)vlc_gl_GetProcAddress(vgl->gl, "glDeleteShader");
456
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");
464
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");
469
470     if (!vgl->CreateShader || !vgl->ShaderSource || !vgl->CreateProgram)
471         supports_shaders = false;
472 #endif
473
474 #if defined(_WIN32)
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
479 #endif
480
481     vgl->supports_npot = HasExtension(extensions, "GL_ARB_texture_non_power_of_two") ||
482                          HasExtension(extensions, "GL_APPLE_texture_2D_limited_npot");
483
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;
488 #endif
489
490     GLint max_texture_units = 0;
491     glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &max_texture_units);
492
493 #ifdef __APPLE__
494 #if USE_OPENGL_ES
495     /* work-around an iOS 6 bug */
496     if (kCFCoreFoundationVersionNumber >= 786.)
497         max_texture_units = 8;
498     supports_shaders = true;
499 #endif
500 #endif
501
502     /* Initialize with default chroma */
503     vgl->fmt = *fmt;
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;
509 #   else
510     vgl->fmt.i_rmask  = 0x000000ff;
511     vgl->fmt.i_gmask  = 0x0000ff00;
512     vgl->fmt.i_bmask  = 0x00ff0000;
513 #   endif
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;
523
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);
526         while (*list) {
527             const vlc_chroma_description_t *dsc = vlc_fourcc_GetChromaDescription(*list);
528             if (dsc && dsc->plane_count == 3 && dsc->pixel_size == 1) {
529                 need_fs_yuv       = true;
530                 vgl->fmt          = *fmt;
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;
536                 break;
537 #if !USE_OPENGL_ES
538             } else if (dsc && dsc->plane_count == 3 && dsc->pixel_size == 2 &&
539                        IsLuminance16Supported(vgl->tex_target)) {
540                 need_fs_yuv       = true;
541                 vgl->fmt          = *fmt;
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);
547                 break;
548 #endif
549             }
550             list++;
551         }
552     }
553
554     if (fmt->i_chroma == VLC_CODEC_XYZ12) {
555         vlc_fourcc_GetChromaDescription(fmt->i_chroma);
556         need_fs_xyz       = true;
557         vgl->fmt          = *fmt;
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;
562     }
563     vgl->chroma = vlc_fourcc_GetChromaDescription(vgl->fmt.i_chroma);
564     vgl->use_multitexture = vgl->chroma->plane_count > 1;
565
566     /* Texture size */
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;
573         } else {
574             vgl->tex_width[j]  = GetAlignedSize(w);
575             vgl->tex_height[j] = GetAlignedSize(h);
576         }
577     }
578
579     /* Build program if needed */
580     vgl->program[0] =
581     vgl->program[1] = 0;
582     vgl->shader[0] =
583     vgl->shader[1] =
584     vgl->shader[2] = -1;
585     vgl->local_count = 0;
586     if (supports_shaders && (need_fs_yuv || need_fs_xyz|| need_fs_rgba)) {
587 #ifdef SUPPORTS_SHADERS
588         if (need_fs_xyz)
589             BuildXYZFragmentShader(vgl, &vgl->shader[0]);
590         else
591             BuildYUVFragmentShader(vgl, &vgl->shader[0], &vgl->local_count,
592                                 vgl->local_value, fmt, yuv_range_correction);
593
594         BuildRGBAFragmentShader(vgl, &vgl->shader[1]);
595         BuildVertexShader(vgl, &vgl->shader[2]);
596
597         /* Check shaders messages */
598         for (unsigned j = 0; j < 3; j++) {
599             int infoLength;
600             vgl->GetShaderiv(vgl->shader[j], GL_INFO_LOG_LENGTH, &infoLength);
601             if (infoLength <= 1)
602                 continue;
603
604             char *infolog = malloc(infoLength);
605             int charsWritten;
606             vgl->GetShaderInfoLog(vgl->shader[j], infoLength, &charsWritten, infolog);
607             fprintf(stderr, "shader %d: %s\n", j, infolog);
608             free(infolog);
609         }
610
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]);
615
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]);
620
621         /* Check program messages */
622         for (GLuint i = 0; i < 2; i++) {
623             int infoLength = 0;
624             vgl->GetProgramiv(vgl->program[i], GL_INFO_LOG_LENGTH, &infoLength);
625             if (infoLength <= 1)
626                 continue;
627             char *infolog = malloc(infoLength);
628             int charsWritten;
629             vgl->GetProgramInfoLog(vgl->program[i], infoLength, &charsWritten, infolog);
630             fprintf(stderr, "shader program %d: %s\n", i, infolog);
631             free(infolog);
632
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);
638                 free(vgl);
639                 return NULL;
640             }
641         }
642 #else
643         (void)yuv_range_correction;
644 #endif
645     }
646
647     /* */
648     glDisable(GL_BLEND);
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);
654
655     vlc_gl_Unlock(vgl->gl);
656
657     /* */
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;
661     }
662     vgl->region_count = 0;
663     vgl->region = NULL;
664     vgl->pool = NULL;
665
666     *fmt = vgl->fmt;
667     if (subpicture_chromas) {
668         *subpicture_chromas = gl_subpicture_chromas;
669     }
670     return vgl;
671 }
672
673 void vout_display_opengl_Delete(vout_display_opengl_t *vgl)
674 {
675     /* */
676     if (!vlc_gl_Lock(vgl->gl)) {
677         glFinish();
678         glFlush();
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);
684         }
685         free(vgl->region);
686
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]);
693         }
694 #endif
695
696         free(vgl->texture_temp_buf);
697         vlc_gl_Unlock(vgl->gl);
698     }
699     if (vgl->pool)
700         picture_pool_Delete(vgl->pool);
701     free(vgl);
702 }
703
704 picture_pool_t *vout_display_opengl_GetPool(vout_display_opengl_t *vgl, unsigned requested_count)
705 {
706     if (vgl->pool)
707         return vgl->pool;
708
709     /* Allocate our pictures */
710     picture_t *picture[VLCGL_PICTURE_MAX] = {NULL, };
711     unsigned count;
712
713     for (count = 0; count < __MIN(VLCGL_PICTURE_MAX, requested_count); count++) {
714         picture[count] = picture_NewFromFormat(&vgl->fmt);
715         if (!picture[count])
716             break;
717     }
718     if (count <= 0)
719         return NULL;
720
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);
727     if (!vgl->pool)
728         goto error;
729
730     /* Allocates our textures */
731     if (vlc_gl_Lock(vgl->gl))
732         return vgl->pool;
733
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);
740             }
741             glBindTexture(vgl->tex_target, vgl->texture[i][j]);
742
743 #if !USE_OPENGL_ES
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);
747 #endif
748
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);
753
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);
758         }
759     }
760
761     vlc_gl_Unlock(vgl->gl);
762
763     return vgl->pool;
764
765 error:
766     for (unsigned i = 0; i < count; i++)
767         picture_Release(picture[i]);
768     return NULL;
769 }
770
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)
778 {
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 )
789     {
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 )
794         {
795             free( vgl->texture_temp_buf );
796             vgl->texture_temp_buf = xmalloc( buf_size );
797             vgl->texture_temp_buf_size = buf_size;
798         }
799         destination = vgl->texture_temp_buf;
800
801         for( int h = 0; h < height ; h++ )
802         {
803             memcpy( destination, source, width * pixel_pitch );
804             source += pitch;
805             destination += dst_pitch;
806         }
807         if (full_upload)
808             glTexImage2D( tex_target, 0, tex_format,
809                           full_width, full_height,
810                           0, tex_format, tex_type, vgl->texture_temp_buf );
811         else
812             glTexSubImage2D( tex_target, 0,
813                              0, 0,
814                              width, height,
815                              tex_format, tex_type, vgl->texture_temp_buf );
816     } else {
817 #else
818     (void) width;
819     (void) height;
820     (void) vgl;
821     {
822         glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch / pixel_pitch);
823 #endif
824         if (full_upload)
825             glTexImage2D(tex_target, 0, tex_format,
826                          full_width, full_height,
827                          0, tex_format, tex_type, pixels);
828         else
829             glTexSubImage2D(tex_target, 0,
830                             0, 0,
831                             width, height,
832                             tex_format, tex_type, pixels);
833     }
834 }
835
836 int vout_display_opengl_Prepare(vout_display_opengl_t *vgl,
837                                 picture_t *picture, subpicture_t *subpicture)
838 {
839     if (vlc_gl_Lock(vgl->gl))
840         return VLC_EGENERIC;
841
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);
847         }
848         glBindTexture(vgl->tex_target, vgl->texture[0][j]);
849
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);
854     }
855
856     int         last_count = vgl->region_count;
857     gl_region_t *last = vgl->region;
858
859     vgl->region_count = 0;
860     vgl->region       = NULL;
861
862     if (subpicture) {
863
864         int count = 0;
865         for (subpicture_region_t *r = subpicture->p_region; r; r = r->p_next)
866             count++;
867
868         vgl->region_count = count;
869         vgl->region       = calloc(count, sizeof(*vgl->region));
870
871         if (vgl->use_multitexture) {
872             glActiveTexture(GL_TEXTURE0 + 0);
873             glClientActiveTexture(GL_TEXTURE0 + 0);
874         }
875         int i = 0;
876         for (subpicture_region_t *r = subpicture->p_region; r; r = r->p_next, i++) {
877             gl_region_t *glr = &vgl->region[i];
878
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;
888             } else {
889                 glr->tex_width  = 1.0;
890                 glr->tex_height = 1.0;
891             }
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;
897
898             glr->texture = 0;
899             for (int j = 0; j < last_count; j++) {
900                 if (last[j].texture &&
901                     last[j].width  == glr->width &&
902                     last[j].height == glr->height &&
903                     last[j].format == glr->format &&
904                     last[j].type   == glr->type) {
905                     glr->texture = last[j].texture;
906                     memset(&last[j], 0, sizeof(last[j]));
907                     break;
908                 }
909             }
910
911             const int pixels_offset = r->fmt.i_y_offset * r->p_picture->p->i_pitch +
912                                       r->fmt.i_x_offset * r->p_picture->p->i_pixel_pitch;
913             if (glr->texture) {
914                 glBindTexture(GL_TEXTURE_2D, glr->texture);
915                 Upload(vgl, r->fmt.i_visible_width, r->fmt.i_visible_height, glr->width, glr->height, 1, 1, 1, 1,
916                        r->p_picture->p->i_pitch, r->p_picture->p->i_pixel_pitch, 0,
917                        &r->p_picture->p->p_pixels[pixels_offset], GL_TEXTURE_2D, glr->format, glr->type);
918             } else {
919                 glGenTextures(1, &glr->texture);
920                 glBindTexture(GL_TEXTURE_2D, glr->texture);
921 #if !USE_OPENGL_ES
922                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_PRIORITY, 1.0);
923                 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
924 #endif
925                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
926                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
927                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
928                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
929                 Upload(vgl, r->fmt.i_visible_width, r->fmt.i_visible_height, glr->width, glr->height, 1, 1, 1, 1,
930                        r->p_picture->p->i_pitch, r->p_picture->p->i_pixel_pitch, 1,
931                        &r->p_picture->p->p_pixels[pixels_offset], GL_TEXTURE_2D, glr->format, glr->type);
932             }
933         }
934     }
935     for (int i = 0; i < last_count; i++) {
936         if (last[i].texture)
937             glDeleteTextures(1, &last[i].texture);
938     }
939     free(last);
940
941     vlc_gl_Unlock(vgl->gl);
942     VLC_UNUSED(subpicture);
943     return VLC_SUCCESS;
944 }
945
946 #ifdef SUPPORTS_FIXED_PIPELINE
947 static void DrawWithoutShaders(vout_display_opengl_t *vgl,
948                                float *left, float *top, float *right, float *bottom)
949 {
950     static const GLfloat vertexCoord[] = {
951         -1.0f, -1.0f,
952          1.0f, -1.0f,
953         -1.0f,  1.0f,
954          1.0f,  1.0f,
955     };
956
957     const GLfloat textureCoord[] = {
958         left[0],  bottom[0],
959         right[0], bottom[0],
960         left[0],  top[0],
961         right[0], top[0]
962     };
963
964     glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
965     glEnable(vgl->tex_target);
966     glActiveTexture(GL_TEXTURE0 + 0);
967     glClientActiveTexture(GL_TEXTURE0 + 0);
968
969     glBindTexture(vgl->tex_target, vgl->texture[0][0]);
970
971     glEnableClientState(GL_VERTEX_ARRAY);
972     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
973
974     glTexCoordPointer(2, GL_FLOAT, 0, textureCoord);
975     glVertexPointer(2, GL_FLOAT, 0, vertexCoord);
976
977     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
978
979     glDisableClientState(GL_TEXTURE_COORD_ARRAY);
980     glDisableClientState(GL_VERTEX_ARRAY);
981     glDisable(vgl->tex_target);
982 }
983 #endif
984
985 #ifdef SUPPORTS_SHADERS
986 static void DrawWithShaders(vout_display_opengl_t *vgl,
987                             float *left, float *top, float *right, float *bottom,
988                             int program)
989 {
990     vgl->UseProgram(vgl->program[program]);
991     if (program == 0) {
992         if (vgl->chroma->plane_count == 3) {
993             vgl->Uniform4fv(vgl->GetUniformLocation(vgl->program[0], "Coefficient"), 4, vgl->local_value);
994             vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[0], "Texture0"), 0);
995             vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[0], "Texture1"), 1);
996             vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[0], "Texture2"), 2);
997         }
998         else if (vgl->chroma->plane_count == 1) {
999             vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[0], "Texture0"), 0);
1000         }
1001     } else {
1002         vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[1], "Texture0"), 0);
1003         vgl->Uniform4f(vgl->GetUniformLocation(vgl->program[1], "FillColor"), 1.0f, 1.0f, 1.0f, 1.0f);
1004     }
1005
1006     static const GLfloat vertexCoord[] = {
1007         -1.0,  1.0,
1008         -1.0, -1.0,
1009          1.0,  1.0,
1010          1.0, -1.0,
1011     };
1012
1013     for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
1014         const GLfloat textureCoord[] = {
1015             left[j],  top[j],
1016             left[j],  bottom[j],
1017             right[j], top[j],
1018             right[j], bottom[j],
1019         };
1020         glActiveTexture(GL_TEXTURE0+j);
1021         glClientActiveTexture(GL_TEXTURE0+j);
1022         glBindTexture(vgl->tex_target, vgl->texture[0][j]);
1023
1024         char attribute[20];
1025         snprintf(attribute, sizeof(attribute), "MultiTexCoord%1d", j);
1026         vgl->EnableVertexAttribArray(vgl->GetAttribLocation(vgl->program[program], attribute));
1027         vgl->VertexAttribPointer(vgl->GetAttribLocation(vgl->program[program], attribute), 2, GL_FLOAT, 0, 0, textureCoord);
1028     }
1029     glActiveTexture(GL_TEXTURE0 + 0);
1030     glClientActiveTexture(GL_TEXTURE0 + 0);
1031     vgl->EnableVertexAttribArray(vgl->GetAttribLocation(vgl->program[program], "VertexPosition"));
1032     vgl->VertexAttribPointer(vgl->GetAttribLocation(vgl->program[program], "VertexPosition"), 2, GL_FLOAT, 0, 0, vertexCoord);
1033
1034     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1035 }
1036 #endif
1037
1038 int vout_display_opengl_Display(vout_display_opengl_t *vgl,
1039                                 const video_format_t *source)
1040 {
1041     if (vlc_gl_Lock(vgl->gl))
1042         return VLC_EGENERIC;
1043
1044     /* Why drawing here and not in Render()? Because this way, the
1045        OpenGL providers can call vout_display_opengl_Display to force redraw.i
1046        Currently, the OS X provider uses it to get a smooth window resizing */
1047     glClear(GL_COLOR_BUFFER_BIT);
1048
1049     /* Draw the picture */
1050     float left[PICTURE_PLANE_MAX];
1051     float top[PICTURE_PLANE_MAX];
1052     float right[PICTURE_PLANE_MAX];
1053     float bottom[PICTURE_PLANE_MAX];
1054     for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
1055         /* glTexCoord works differently with GL_TEXTURE_2D and
1056            GL_TEXTURE_RECTANGLE_EXT */
1057         float scale_w, scale_h;
1058
1059         if (vgl->tex_target == GL_TEXTURE_2D) {
1060             scale_w = (float)vgl->chroma->p[j].w.num / vgl->chroma->p[j].w.den / vgl->tex_width[j];
1061             scale_h = (float)vgl->chroma->p[j].h.num / vgl->chroma->p[j].h.den / vgl->tex_height[j];
1062
1063         } else {
1064             scale_w = 1.0;
1065             scale_h = 1.0;
1066         }
1067         left[j]   = (source->i_x_offset +                       0 ) * scale_w;
1068         top[j]    = (source->i_y_offset +                       0 ) * scale_h;
1069         right[j]  = (source->i_x_offset + source->i_visible_width ) * scale_w;
1070         bottom[j] = (source->i_y_offset + source->i_visible_height) * scale_h;
1071     }
1072
1073 #ifdef SUPPORTS_SHADERS
1074     if (vgl->program[0] && (vgl->chroma->plane_count == 3 || vgl->chroma->plane_count == 1))
1075         DrawWithShaders(vgl, left, top, right, bottom, 0);
1076     else if (vgl->program[1] && vgl->chroma->plane_count == 1)
1077         DrawWithShaders(vgl, left, top, right, bottom, 1);
1078     else
1079 #endif
1080     {
1081 #ifdef SUPPORTS_FIXED_PIPELINE
1082         DrawWithoutShaders(vgl, left, top, right, bottom);
1083 #endif
1084     }
1085
1086     /* Draw the subpictures */
1087     if (vgl->program[1]) {
1088 #ifdef SUPPORTS_SHADERS
1089         // Change the program for overlays
1090         vgl->UseProgram(vgl->program[1]);
1091         vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[1], "Texture"), 0);
1092 #endif
1093     }
1094
1095 #ifdef SUPPORTS_FIXED_PIPELINE
1096     glEnable(GL_TEXTURE_2D);
1097 #endif
1098     glEnable(GL_BLEND);
1099     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1100
1101     glActiveTexture(GL_TEXTURE0 + 0);
1102     glClientActiveTexture(GL_TEXTURE0 + 0);
1103     for (int i = 0; i < vgl->region_count; i++) {
1104         gl_region_t *glr = &vgl->region[i];
1105         const GLfloat vertexCoord[] = {
1106             glr->left,  glr->top,
1107             glr->left,  glr->bottom,
1108             glr->right, glr->top,
1109             glr->right, glr->bottom,
1110         };
1111         const GLfloat textureCoord[] = {
1112             0.0, 0.0,
1113             0.0, glr->tex_height,
1114             glr->tex_width, 0.0,
1115             glr->tex_width, glr->tex_height,
1116         };
1117
1118         glBindTexture(GL_TEXTURE_2D, glr->texture);
1119         if (vgl->program[1]) {
1120 #ifdef SUPPORTS_SHADERS
1121             vgl->Uniform4f(vgl->GetUniformLocation(vgl->program[1], "FillColor"), 1.0f, 1.0f, 1.0f, glr->alpha);
1122             vgl->EnableVertexAttribArray(vgl->GetAttribLocation(vgl->program[1], "MultiTexCoord0"));
1123             vgl->VertexAttribPointer(vgl->GetAttribLocation(vgl->program[1], "MultiTexCoord0"), 2, GL_FLOAT, 0, 0, textureCoord);
1124             vgl->EnableVertexAttribArray(vgl->GetAttribLocation(vgl->program[1], "VertexPosition"));
1125             vgl->VertexAttribPointer(vgl->GetAttribLocation(vgl->program[1], "VertexPosition"), 2, GL_FLOAT, 0, 0, vertexCoord);
1126 #endif
1127         } else {
1128 #ifdef SUPPORTS_FIXED_PIPELINE
1129             glEnableClientState(GL_VERTEX_ARRAY);
1130             glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1131             glColor4f(1.0f, 1.0f, 1.0f, glr->alpha);
1132             glTexCoordPointer(2, GL_FLOAT, 0, textureCoord);
1133             glVertexPointer(2, GL_FLOAT, 0, vertexCoord);
1134 #endif
1135         }
1136
1137         glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1138
1139         if (!vgl->program[1]) {
1140 #ifdef SUPPORTS_FIXED_PIPELINE
1141             glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1142             glDisableClientState(GL_VERTEX_ARRAY);
1143 #endif
1144         }
1145     }
1146     glDisable(GL_BLEND);
1147 #ifdef SUPPORTS_FIXED_PIPELINE
1148     glDisable(GL_TEXTURE_2D);
1149 #endif
1150
1151     /* Display */
1152     vlc_gl_Swap(vgl->gl);
1153
1154     vlc_gl_Unlock(vgl->gl);
1155     return VLC_SUCCESS;
1156 }
1157