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