1 /*****************************************************************************
2 * opengl.c: OpenGL and OpenGL ES output common code
3 *****************************************************************************
4 * Copyright (C) 2004-2013 VLC authors and VideoLAN
5 * Copyright (C) 2009, 2011 Laurent Aimar
7 * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
8 * Ilkka Ollakka <ileoo@videolan.org>
10 * Adrien Maglo <magsoft at videolan dot org>
11 * Felix Paul Kühne <fkuehne at videolan dot org>
12 * Pierre d'Herbemont <pdherbemont at videolan dot org>
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU Lesser General Public License as published by
16 * the Free Software Foundation; either version 2.1 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public License
25 * along with this program; if not, write to the Free Software Foundation,
26 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
27 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_picture_pool.h>
34 #include <vlc_subpicture.h>
35 #include <vlc_opengl.h>
36 #include <vlc_memory.h>
40 #ifndef GL_CLAMP_TO_EDGE
41 # define GL_CLAMP_TO_EDGE 0x812F
44 #if USE_OPENGL_ES == 2 || defined(__APPLE__)
45 # define PFNGLGETPROGRAMIVPROC typeof(glGetProgramiv)*
46 # define PFNGLGETPROGRAMINFOLOGPROC typeof(glGetProgramInfoLog)*
47 # define PFNGLGETSHADERIVPROC typeof(glGetShaderiv)*
48 # define PFNGLGETSHADERINFOLOGPROC typeof(glGetShaderInfoLog)*
49 # define PFNGLGETUNIFORMLOCATIONPROC typeof(glGetUniformLocation)*
50 # define PFNGLGETATTRIBLOCATIONPROC typeof(glGetAttribLocation)*
51 # define PFNGLVERTEXATTRIBPOINTERPROC typeof(glVertexAttribPointer)*
52 # define PFNGLENABLEVERTEXATTRIBARRAYPROC typeof(glEnableVertexAttribArray)*
53 # define PFNGLUNIFORMMATRIX4FVPROC typeof(glUniformMatrix4fv)*
54 # define PFNGLUNIFORM4FVPROC typeof(glUniform4fv)*
55 # define PFNGLUNIFORM4FPROC typeof(glUniform4f)*
56 # define PFNGLUNIFORM1IPROC typeof(glUniform1i)*
57 # define PFNGLCREATESHADERPROC typeof(glCreateShader)*
58 # define PFNGLSHADERSOURCEPROC typeof(glShaderSource)*
59 # define PFNGLCOMPILESHADERPROC typeof(glCompileShader)*
60 # define PFNGLDELETESHADERPROC typeof(glDeleteShader)*
61 # define PFNGLCREATEPROGRAMPROC typeof(glCreateProgram)*
62 # define PFNGLLINKPROGRAMPROC typeof(glLinkProgram)*
63 # define PFNGLUSEPROGRAMPROC typeof(glUseProgram)*
64 # define PFNGLDELETEPROGRAMPROC typeof(glDeleteProgram)*
65 # define PFNGLATTACHSHADERPROC typeof(glAttachShader)*
66 # define PFNGLGENBUFFERSPROC typeof(glGenBuffers)*
67 # define PFNGLBINDBUFFERPROC typeof(glBindBuffer)*
68 # define PFNGLBUFFERDATAPROC typeof(glBufferData)*
69 # define PFNGLDELETEBUFFERSPROC typeof(glDeleteBuffers)*
70 #if defined(__APPLE__) && USE_OPENGL_ES
71 # import <CoreFoundation/CoreFoundation.h>
76 # define GLSL_VERSION "100"
77 # define VLCGL_TEXTURE_COUNT 1
78 # define VLCGL_PICTURE_MAX 1
79 # define PRECISION "precision highp float;"
80 #if USE_OPENGL_ES == 2
81 # define SUPPORTS_SHADERS
82 # define glClientActiveTexture(x)
84 # define SUPPORTS_FIXED_PIPELINE
85 # define GL_MAX_TEXTURE_IMAGE_UNITS GL_MAX_TEXTURE_UNITS
88 # define GLSL_VERSION "120"
89 # define VLCGL_TEXTURE_COUNT 1
90 # define VLCGL_PICTURE_MAX 128
92 # define SUPPORTS_SHADERS
93 # define SUPPORTS_FIXED_PIPELINE
96 static const vlc_fourcc_t gl_subpicture_chromas[] = {
119 struct vout_display_opengl_t {
124 const vlc_chroma_description_t *chroma;
131 int tex_width[PICTURE_PLANE_MAX];
132 int tex_height[PICTURE_PLANE_MAX];
134 GLuint texture[VLCGL_TEXTURE_COUNT][PICTURE_PLANE_MAX];
140 picture_pool_t *pool;
142 /* index 0 for normal and 1 for subtitle overlay */
144 GLint shader[3]; //3. is for the common vertex shader
146 GLfloat local_value[16];
148 GLuint vertex_buffer_object;
149 GLuint texture_buffer_object[PICTURE_PLANE_MAX];
151 GLuint *subpicture_buffer_object;
152 int subpicture_buffer_object_count;
154 /* Shader variables commands*/
155 #ifdef SUPPORTS_SHADERS
156 PFNGLGETUNIFORMLOCATIONPROC GetUniformLocation;
157 PFNGLGETATTRIBLOCATIONPROC GetAttribLocation;
158 PFNGLVERTEXATTRIBPOINTERPROC VertexAttribPointer;
159 PFNGLENABLEVERTEXATTRIBARRAYPROC EnableVertexAttribArray;
161 PFNGLUNIFORMMATRIX4FVPROC UniformMatrix4fv;
162 PFNGLUNIFORM4FVPROC Uniform4fv;
163 PFNGLUNIFORM4FPROC Uniform4f;
164 PFNGLUNIFORM1IPROC Uniform1i;
167 PFNGLCREATESHADERPROC CreateShader;
168 PFNGLSHADERSOURCEPROC ShaderSource;
169 PFNGLCOMPILESHADERPROC CompileShader;
170 PFNGLDELETESHADERPROC DeleteShader;
172 PFNGLCREATEPROGRAMPROC CreateProgram;
173 PFNGLLINKPROGRAMPROC LinkProgram;
174 PFNGLUSEPROGRAMPROC UseProgram;
175 PFNGLDELETEPROGRAMPROC DeleteProgram;
177 PFNGLATTACHSHADERPROC AttachShader;
179 /* Shader log commands */
180 PFNGLGETPROGRAMIVPROC GetProgramiv;
181 PFNGLGETPROGRAMINFOLOGPROC GetProgramInfoLog;
182 PFNGLGETSHADERIVPROC GetShaderiv;
183 PFNGLGETSHADERINFOLOGPROC GetShaderInfoLog;
185 PFNGLGENBUFFERSPROC GenBuffers;
186 PFNGLBINDBUFFERPROC BindBuffer;
187 PFNGLBUFFERDATAPROC BufferData;
188 PFNGLDELETEBUFFERSPROC DeleteBuffers;
192 PFNGLACTIVETEXTUREPROC ActiveTexture;
193 PFNGLCLIENTACTIVETEXTUREPROC ClientActiveTexture;
198 bool use_multitexture;
200 /* Non-power-of-2 texture size support */
203 uint8_t *texture_temp_buf;
204 int texture_temp_buf_size;
207 static inline int GetAlignedSize(unsigned size)
209 /* Return the smallest larger or equal power of 2 */
210 unsigned align = 1 << (8 * sizeof (unsigned) - clz(size));
211 return ((align >> 1) == size) ? size : align;
215 static bool IsLuminance16Supported(int target)
219 glGenTextures(1, &texture);
220 glBindTexture(target, texture);
221 glTexImage2D(target, 0, GL_LUMINANCE16,
222 64, 64, 0, GL_LUMINANCE, GL_UNSIGNED_SHORT, NULL);
224 glGetTexLevelParameteriv(target, 0, GL_TEXTURE_LUMINANCE_SIZE, &size);
226 glDeleteTextures(1, &texture);
232 #ifdef SUPPORTS_SHADERS
233 static void BuildVertexShader(vout_display_opengl_t *vgl,
236 /* Basic vertex shader */
237 const char *vertexShader =
238 "#version " GLSL_VERSION "\n"
240 "varying vec4 TexCoord0,TexCoord1, TexCoord2;"
241 "attribute vec4 MultiTexCoord0,MultiTexCoord1,MultiTexCoord2;"
242 "attribute vec2 VertexPosition;"
243 "uniform mat4 RotationMatrix;"
245 " TexCoord0 = MultiTexCoord0;"
246 " TexCoord1 = MultiTexCoord1;"
247 " TexCoord2 = MultiTexCoord2;"
248 " gl_Position = RotationMatrix * vec4(VertexPosition, 0.0, 1.0);"
251 *shader = vgl->CreateShader(GL_VERTEX_SHADER);
252 vgl->ShaderSource(*shader, 1, &vertexShader, NULL);
253 vgl->CompileShader(*shader);
256 static void BuildYUVFragmentShader(vout_display_opengl_t *vgl,
259 GLfloat *local_value,
260 const video_format_t *fmt,
261 float yuv_range_correction)
264 /* [R/G/B][Y U V O] from TV range to full range
265 * XXX we could also do hue/brightness/constrast/gamma
266 * by simply changing the coefficients
268 const float matrix_bt601_tv2full[12] = {
269 1.164383561643836, 0.0000, 1.596026785714286, -0.874202217873451 ,
270 1.164383561643836, -0.391762290094914, -0.812967647237771, 0.531667823499146 ,
271 1.164383561643836, 2.017232142857142, 0.0000, -1.085630789302022 ,
273 const float matrix_bt709_tv2full[12] = {
274 1.164383561643836, 0.0000, 1.792741071428571, -0.972945075016308 ,
275 1.164383561643836, -0.21324861427373, -0.532909328559444, 0.301482665475862 ,
276 1.164383561643836, 2.112401785714286, 0.0000, -1.133402217873451 ,
278 const float (*matrix) = fmt->i_height > 576 ? matrix_bt709_tv2full
279 : matrix_bt601_tv2full;
281 /* Basic linear YUV -> RGB conversion using bilinear interpolation */
282 const char *template_glsl_yuv =
283 "#version " GLSL_VERSION "\n"
285 "uniform sampler2D Texture0;"
286 "uniform sampler2D Texture1;"
287 "uniform sampler2D Texture2;"
288 "uniform vec4 Coefficient[4];"
289 "varying vec4 TexCoord0,TexCoord1,TexCoord2;"
292 " vec4 x,y,z,result;"
293 " x = texture2D(Texture0, TexCoord0.st);"
294 " %c = texture2D(Texture1, TexCoord1.st);"
295 " %c = texture2D(Texture2, TexCoord2.st);"
297 " result = x * Coefficient[0] + Coefficient[3];"
298 " result = (y * Coefficient[1]) + result;"
299 " result = (z * Coefficient[2]) + result;"
300 " gl_FragColor = result;"
302 bool swap_uv = fmt->i_chroma == VLC_CODEC_YV12 ||
303 fmt->i_chroma == VLC_CODEC_YV9;
306 if (asprintf(&code, template_glsl_yuv,
308 swap_uv ? 'y' : 'z') < 0)
311 for (int i = 0; i < 4; i++) {
312 float correction = i < 3 ? yuv_range_correction : 1.0;
313 /* We place coefficient values for coefficient[4] in one array from matrix values.
314 Notice that we fill values from top down instead of left to right.*/
315 for (int j = 0; j < 4; j++)
316 local_value[*local_count + i*4+j] = j < 3 ? correction * matrix[j*4+i]
322 *shader = vgl->CreateShader(GL_FRAGMENT_SHADER);
323 vgl->ShaderSource(*shader, 1, (const char **)&code, NULL);
324 vgl->CompileShader(*shader);
330 static void BuildRGBFragmentShader(vout_display_opengl_t *vgl,
333 // Simple shader for RGB
335 "#version " GLSL_VERSION "\n"
337 "uniform sampler2D Texture[3];"
338 "varying vec4 TexCoord0,TexCoord1,TexCoord2;"
341 " gl_FragColor = texture2D(Texture[0], TexCoord0.st);"
343 *shader = vgl->CreateShader(GL_FRAGMENT_SHADER);
344 vgl->ShaderSource(*shader, 1, &code, NULL);
345 vgl->CompileShader(*shader);
349 static void BuildRGBAFragmentShader(vout_display_opengl_t *vgl,
352 // Simple shader for RGBA
354 "#version " GLSL_VERSION "\n"
356 "uniform sampler2D Texture;"
357 "uniform vec4 FillColor;"
358 "varying vec4 TexCoord0;"
361 " gl_FragColor = texture2D(Texture, TexCoord0.st) * FillColor;"
363 *shader = vgl->CreateShader(GL_FRAGMENT_SHADER);
364 vgl->ShaderSource(*shader, 1, &code, NULL);
365 vgl->CompileShader(*shader);
368 static void BuildXYZFragmentShader(vout_display_opengl_t *vgl,
371 /* Shader for XYZ to RGB correction
373 * - XYZ gamma correction
374 * - XYZ to RGB matrix conversion
375 * - reverse RGB gamma correction
378 "#version " GLSL_VERSION "\n"
380 "uniform sampler2D Texture0;"
381 "uniform vec4 xyz_gamma = vec4(2.6);"
382 "uniform vec4 rgb_gamma = vec4(1.0/2.2);"
383 // WARN: matrix Is filled column by column (not row !)
384 "uniform mat4 matrix_xyz_rgb = mat4("
385 " 3.240454 , -0.9692660, 0.0556434, 0.0,"
386 " -1.5371385, 1.8760108, -0.2040259, 0.0,"
387 " -0.4985314, 0.0415560, 1.0572252, 0.0,"
388 " 0.0, 0.0, 0.0, 1.0 "
391 "varying vec4 TexCoord0;"
395 " v_in = texture2D(Texture0, TexCoord0.st);"
396 " v_in = pow(v_in, xyz_gamma);"
397 " v_out = matrix_xyz_rgb * v_in ;"
398 " v_out = pow(v_out, rgb_gamma) ;"
399 " v_out = clamp(v_out, 0.0, 1.0) ;"
400 " gl_FragColor = v_out;"
402 *shader = vgl->CreateShader(GL_FRAGMENT_SHADER);
403 vgl->ShaderSource(*shader, 1, &code, NULL);
404 vgl->CompileShader(*shader);
409 vout_display_opengl_t *vout_display_opengl_New(video_format_t *fmt,
410 const vlc_fourcc_t **subpicture_chromas,
413 vout_display_opengl_t *vgl = calloc(1, sizeof(*vgl));
418 if (vlc_gl_Lock(vgl->gl)) {
423 if (vgl->gl->getProcAddress == NULL) {
424 fprintf(stderr, "getProcAddress not implemented, bailing out\n");
429 const char *extensions = (const char *)glGetString(GL_EXTENSIONS);
431 const unsigned char *ogl_version = glGetString(GL_VERSION);
432 bool supports_shaders = strverscmp((const char *)ogl_version, "2.0") >= 0;
434 bool supports_shaders = false;
437 #if USE_OPENGL_ES == 2
438 vgl->CreateShader = glCreateShader;
439 vgl->ShaderSource = glShaderSource;
440 vgl->CompileShader = glCompileShader;
441 vgl->AttachShader = glAttachShader;
443 vgl->GetProgramiv = glGetProgramiv;
444 vgl->GetShaderiv = glGetShaderiv;
445 vgl->GetProgramInfoLog = glGetProgramInfoLog;
446 vgl->GetShaderInfoLog = glGetShaderInfoLog;
448 vgl->DeleteShader = glDeleteShader;
450 vgl->GetUniformLocation = glGetUniformLocation;
451 vgl->GetAttribLocation = glGetAttribLocation;
452 vgl->VertexAttribPointer= glVertexAttribPointer;
453 vgl->EnableVertexAttribArray = glEnableVertexAttribArray;
454 vgl->UniformMatrix4fv = glUniformMatrix4fv;
455 vgl->Uniform4fv = glUniform4fv;
456 vgl->Uniform4f = glUniform4f;
457 vgl->Uniform1i = glUniform1i;
459 vgl->CreateProgram = glCreateProgram;
460 vgl->LinkProgram = glLinkProgram;
461 vgl->UseProgram = glUseProgram;
462 vgl->DeleteProgram = glDeleteProgram;
464 vgl->GenBuffers = glGenBuffers;
465 vgl->BindBuffer = glBindBuffer;
466 vgl->BufferData = glBufferData;
467 vgl->DeleteBuffers = glDeleteBuffers;
469 supports_shaders = true;
470 #elif defined(SUPPORTS_SHADERS)
471 vgl->CreateShader = (PFNGLCREATESHADERPROC)vlc_gl_GetProcAddress(vgl->gl, "glCreateShader");
472 vgl->ShaderSource = (PFNGLSHADERSOURCEPROC)vlc_gl_GetProcAddress(vgl->gl, "glShaderSource");
473 vgl->CompileShader = (PFNGLCOMPILESHADERPROC)vlc_gl_GetProcAddress(vgl->gl, "glCompileShader");
474 vgl->AttachShader = (PFNGLATTACHSHADERPROC)vlc_gl_GetProcAddress(vgl->gl, "glAttachShader");
476 vgl->GetProgramiv = (PFNGLGETPROGRAMIVPROC)vlc_gl_GetProcAddress(vgl->gl, "glGetProgramiv");
477 vgl->GetShaderiv = (PFNGLGETSHADERIVPROC)vlc_gl_GetProcAddress(vgl->gl, "glGetShaderiv");
478 vgl->GetProgramInfoLog = (PFNGLGETPROGRAMINFOLOGPROC)vlc_gl_GetProcAddress(vgl->gl, "glGetProgramInfoLog");
479 vgl->GetShaderInfoLog = (PFNGLGETSHADERINFOLOGPROC)vlc_gl_GetProcAddress(vgl->gl, "glGetShaderInfoLog");
481 vgl->DeleteShader = (PFNGLDELETESHADERPROC)vlc_gl_GetProcAddress(vgl->gl, "glDeleteShader");
483 vgl->GetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC)vlc_gl_GetProcAddress(vgl->gl, "glGetUniformLocation");
484 vgl->GetAttribLocation = (PFNGLGETATTRIBLOCATIONPROC)vlc_gl_GetProcAddress(vgl->gl, "glGetAttribLocation");
485 vgl->VertexAttribPointer= (PFNGLVERTEXATTRIBPOINTERPROC)vlc_gl_GetProcAddress(vgl->gl, "glVertexAttribPointer");
486 vgl->EnableVertexAttribArray = (PFNGLENABLEVERTEXATTRIBARRAYPROC)vlc_gl_GetProcAddress(vgl->gl, "glEnableVertexAttribArray");
487 vgl->UniformMatrix4fv = (PFNGLUNIFORMMATRIX4FVPROC)vlc_gl_GetProcAddress(vgl->gl,"glUniformMatrix4fv");
488 vgl->Uniform4fv = (PFNGLUNIFORM4FVPROC)vlc_gl_GetProcAddress(vgl->gl,"glUniform4fv");
489 vgl->Uniform4f = (PFNGLUNIFORM4FPROC)vlc_gl_GetProcAddress(vgl->gl,"glUniform4f");
490 vgl->Uniform1i = (PFNGLUNIFORM1IPROC)vlc_gl_GetProcAddress(vgl->gl,"glUniform1i");
492 vgl->CreateProgram = (PFNGLCREATEPROGRAMPROC)vlc_gl_GetProcAddress(vgl->gl, "glCreateProgram");
493 vgl->LinkProgram = (PFNGLLINKPROGRAMPROC)vlc_gl_GetProcAddress(vgl->gl, "glLinkProgram");
494 vgl->UseProgram = (PFNGLUSEPROGRAMPROC)vlc_gl_GetProcAddress(vgl->gl, "glUseProgram");
495 vgl->DeleteProgram = (PFNGLDELETEPROGRAMPROC)vlc_gl_GetProcAddress(vgl->gl, "glDeleteProgram");
497 vgl->GenBuffers = (PFNGLGENBUFFERSPROC)vlc_gl_GetProcAddress(vgl->gl, "glGenBuffers");
498 vgl->BindBuffer = (PFNGLBINDBUFFERPROC)vlc_gl_GetProcAddress(vgl->gl, "glBindBuffer");
499 vgl->BufferData = (PFNGLBUFFERDATAPROC)vlc_gl_GetProcAddress(vgl->gl, "glBufferData");
500 vgl->DeleteBuffers = (PFNGLDELETEBUFFERSPROC)vlc_gl_GetProcAddress(vgl->gl, "glDeleteBuffers");
502 if (!vgl->CreateShader || !vgl->ShaderSource || !vgl->CreateProgram)
503 supports_shaders = false;
507 vgl->ActiveTexture = (PFNGLACTIVETEXTUREPROC)vlc_gl_GetProcAddress(vgl->gl, "glActiveTexture");
508 vgl->ClientActiveTexture = (PFNGLCLIENTACTIVETEXTUREPROC)vlc_gl_GetProcAddress(vgl->gl, "glClientActiveTexture");
509 # define glActiveTexture vgl->ActiveTexture
510 # define glClientActiveTexture vgl->ClientActiveTexture
513 vgl->supports_npot = HasExtension(extensions, "GL_ARB_texture_non_power_of_two") ||
514 HasExtension(extensions, "GL_APPLE_texture_2D_limited_npot");
516 #if USE_OPENGL_ES == 2
517 /* OpenGL ES 2 includes support for non-power of 2 textures by specification
518 * so checks for extensions are bound to fail. Check for OpenGL ES version instead. */
519 vgl->supports_npot = true;
522 GLint max_texture_units = 0;
523 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &max_texture_units);
527 /* work-around an iOS 6 bug */
528 if (kCFCoreFoundationVersionNumber >= 786.)
529 max_texture_units = 8;
530 supports_shaders = true;
534 /* Initialize with default chroma */
536 vgl->fmt.i_chroma = VLC_CODEC_RGB32;
537 # if defined(WORDS_BIGENDIAN)
538 vgl->fmt.i_rmask = 0xff000000;
539 vgl->fmt.i_gmask = 0x00ff0000;
540 vgl->fmt.i_bmask = 0x0000ff00;
542 vgl->fmt.i_rmask = 0x000000ff;
543 vgl->fmt.i_gmask = 0x0000ff00;
544 vgl->fmt.i_bmask = 0x00ff0000;
546 vgl->tex_target = GL_TEXTURE_2D;
547 vgl->tex_format = GL_RGBA;
548 vgl->tex_internal = GL_RGBA;
549 vgl->tex_type = GL_UNSIGNED_BYTE;
550 /* Use YUV if possible and needed */
551 bool need_fs_yuv = false;
552 bool need_fs_xyz = false;
553 bool need_fs_rgba = USE_OPENGL_ES == 2;
554 float yuv_range_correction = 1.0;
556 if (max_texture_units >= 3 && supports_shaders && vlc_fourcc_IsYUV(fmt->i_chroma)) {
557 const vlc_fourcc_t *list = vlc_fourcc_GetYUVFallback(fmt->i_chroma);
559 const vlc_chroma_description_t *dsc = vlc_fourcc_GetChromaDescription(*list);
560 if (dsc && dsc->plane_count == 3 && dsc->pixel_size == 1) {
563 vgl->fmt.i_chroma = *list;
564 vgl->tex_format = GL_LUMINANCE;
565 vgl->tex_internal = GL_LUMINANCE;
566 vgl->tex_type = GL_UNSIGNED_BYTE;
567 yuv_range_correction = 1.0;
570 } else if (dsc && dsc->plane_count == 3 && dsc->pixel_size == 2 &&
571 IsLuminance16Supported(vgl->tex_target)) {
574 vgl->fmt.i_chroma = *list;
575 vgl->tex_format = GL_LUMINANCE;
576 vgl->tex_internal = GL_LUMINANCE16;
577 vgl->tex_type = GL_UNSIGNED_SHORT;
578 yuv_range_correction = (float)((1 << 16) - 1) / ((1 << dsc->pixel_bits) - 1);
586 if (fmt->i_chroma == VLC_CODEC_XYZ12) {
587 vlc_fourcc_GetChromaDescription(fmt->i_chroma);
590 vgl->fmt.i_chroma = VLC_CODEC_XYZ12;
591 vgl->tex_format = GL_RGB;
592 vgl->tex_internal = GL_RGB;
593 vgl->tex_type = GL_UNSIGNED_SHORT;
595 vgl->chroma = vlc_fourcc_GetChromaDescription(vgl->fmt.i_chroma);
596 vgl->use_multitexture = vgl->chroma->plane_count > 1;
599 for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
600 int w = vgl->fmt.i_visible_width * vgl->chroma->p[j].w.num / vgl->chroma->p[j].w.den;
601 int h = vgl->fmt.i_visible_height * vgl->chroma->p[j].h.num / vgl->chroma->p[j].h.den;
602 if (vgl->supports_npot) {
603 vgl->tex_width[j] = w;
604 vgl->tex_height[j] = h;
606 vgl->tex_width[j] = GetAlignedSize(w);
607 vgl->tex_height[j] = GetAlignedSize(h);
611 /* Build program if needed */
617 vgl->local_count = 0;
618 if (supports_shaders && (need_fs_yuv || need_fs_xyz|| need_fs_rgba)) {
619 #ifdef SUPPORTS_SHADERS
621 BuildXYZFragmentShader(vgl, &vgl->shader[0]);
623 BuildYUVFragmentShader(vgl, &vgl->shader[0], &vgl->local_count,
624 vgl->local_value, fmt, yuv_range_correction);
626 BuildRGBAFragmentShader(vgl, &vgl->shader[1]);
627 BuildVertexShader(vgl, &vgl->shader[2]);
629 /* Check shaders messages */
630 for (unsigned j = 0; j < 3; j++) {
632 vgl->GetShaderiv(vgl->shader[j], GL_INFO_LOG_LENGTH, &infoLength);
636 char *infolog = malloc(infoLength);
638 vgl->GetShaderInfoLog(vgl->shader[j], infoLength, &charsWritten, infolog);
639 fprintf(stderr, "shader %d: %s\n", j, infolog);
643 vgl->program[0] = vgl->CreateProgram();
644 vgl->AttachShader(vgl->program[0], vgl->shader[0]);
645 vgl->AttachShader(vgl->program[0], vgl->shader[2]);
646 vgl->LinkProgram(vgl->program[0]);
648 vgl->program[1] = vgl->CreateProgram();
649 vgl->AttachShader(vgl->program[1], vgl->shader[1]);
650 vgl->AttachShader(vgl->program[1], vgl->shader[2]);
651 vgl->LinkProgram(vgl->program[1]);
653 /* Check program messages */
654 for (GLuint i = 0; i < 2; i++) {
656 vgl->GetProgramiv(vgl->program[i], GL_INFO_LOG_LENGTH, &infoLength);
659 char *infolog = malloc(infoLength);
661 vgl->GetProgramInfoLog(vgl->program[i], infoLength, &charsWritten, infolog);
662 fprintf(stderr, "shader program %d: %s\n", i, infolog);
665 /* If there is some message, better to check linking is ok */
666 GLint link_status = GL_TRUE;
667 vgl->GetProgramiv(vgl->program[i], GL_LINK_STATUS, &link_status);
668 if (link_status == GL_FALSE) {
669 fprintf(stderr, "Unable to use program %d\n", i);
675 (void)yuv_range_correction;
681 glDisable(GL_DEPTH_TEST);
682 glDepthMask(GL_FALSE);
683 glDisable(GL_CULL_FACE);
684 glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
685 glClear(GL_COLOR_BUFFER_BIT);
687 #ifdef SUPPORTS_SHADERS
688 vgl->GenBuffers(1, &vgl->vertex_buffer_object);
689 vgl->GenBuffers(vgl->chroma->plane_count, vgl->texture_buffer_object);
691 /* Initial number of allocated buffer objects for subpictures, will grow dynamically. */
692 int subpicture_buffer_object_count = 8;
693 vgl->subpicture_buffer_object = malloc(subpicture_buffer_object_count * sizeof(GLuint));
694 if (!vgl->subpicture_buffer_object) {
695 vlc_gl_Unlock(vgl->gl);
696 vout_display_opengl_Delete(vgl);
699 vgl->subpicture_buffer_object_count = subpicture_buffer_object_count;
700 vgl->GenBuffers(vgl->subpicture_buffer_object_count, vgl->subpicture_buffer_object);
703 vlc_gl_Unlock(vgl->gl);
706 for (int i = 0; i < VLCGL_TEXTURE_COUNT; i++) {
707 for (int j = 0; j < PICTURE_PLANE_MAX; j++)
708 vgl->texture[i][j] = 0;
710 vgl->region_count = 0;
715 if (subpicture_chromas) {
716 *subpicture_chromas = gl_subpicture_chromas;
721 void vout_display_opengl_Delete(vout_display_opengl_t *vgl)
724 if (!vlc_gl_Lock(vgl->gl)) {
727 for (int i = 0; i < VLCGL_TEXTURE_COUNT; i++)
728 glDeleteTextures(vgl->chroma->plane_count, vgl->texture[i]);
729 for (int i = 0; i < vgl->region_count; i++) {
730 if (vgl->region[i].texture)
731 glDeleteTextures(1, &vgl->region[i].texture);
735 #ifdef SUPPORTS_SHADERS
736 if (vgl->program[0]) {
737 for (int i = 0; i < 2; i++)
738 vgl->DeleteProgram(vgl->program[i]);
739 for (int i = 0; i < 3; i++)
740 vgl->DeleteShader(vgl->shader[i]);
742 vgl->DeleteBuffers(1, &vgl->vertex_buffer_object);
743 vgl->DeleteBuffers(vgl->chroma->plane_count, vgl->texture_buffer_object);
744 if (vgl->subpicture_buffer_object_count > 0)
745 vgl->DeleteBuffers(vgl->subpicture_buffer_object_count, vgl->subpicture_buffer_object);
746 free(vgl->subpicture_buffer_object);
749 free(vgl->texture_temp_buf);
750 vlc_gl_Unlock(vgl->gl);
753 picture_pool_Delete(vgl->pool);
757 picture_pool_t *vout_display_opengl_GetPool(vout_display_opengl_t *vgl, unsigned requested_count)
762 /* Allocate our pictures */
763 picture_t *picture[VLCGL_PICTURE_MAX] = {NULL, };
766 for (count = 0; count < __MIN(VLCGL_PICTURE_MAX, requested_count); count++) {
767 picture[count] = picture_NewFromFormat(&vgl->fmt);
774 /* Wrap the pictures into a pool */
775 picture_pool_configuration_t cfg;
776 memset(&cfg, 0, sizeof(cfg));
777 cfg.picture_count = count;
778 cfg.picture = picture;
779 vgl->pool = picture_pool_NewExtended(&cfg);
783 /* Allocates our textures */
784 if (vlc_gl_Lock(vgl->gl))
787 for (int i = 0; i < VLCGL_TEXTURE_COUNT; i++) {
788 glGenTextures(vgl->chroma->plane_count, vgl->texture[i]);
789 for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
790 if (vgl->use_multitexture) {
791 glActiveTexture(GL_TEXTURE0 + j);
792 glClientActiveTexture(GL_TEXTURE0 + j);
794 glBindTexture(vgl->tex_target, vgl->texture[i][j]);
797 /* Set the texture parameters */
798 glTexParameterf(vgl->tex_target, GL_TEXTURE_PRIORITY, 1.0);
799 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
802 glTexParameteri(vgl->tex_target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
803 glTexParameteri(vgl->tex_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
804 glTexParameteri(vgl->tex_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
805 glTexParameteri(vgl->tex_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
807 /* Call glTexImage2D only once, and use glTexSubImage2D later */
808 glTexImage2D(vgl->tex_target, 0,
809 vgl->tex_internal, vgl->tex_width[j], vgl->tex_height[j],
810 0, vgl->tex_format, vgl->tex_type, NULL);
814 vlc_gl_Unlock(vgl->gl);
819 for (unsigned i = 0; i < count; i++)
820 picture_Release(picture[i]);
824 #define ALIGN(x, y) (((x) + ((y) - 1)) & ~((y) - 1))
825 static void Upload(vout_display_opengl_t *vgl, int in_width, int in_height,
826 int in_full_width, int in_full_height,
827 int w_num, int w_den, int h_num, int h_den,
828 int pitch, int pixel_pitch,
829 int full_upload, const uint8_t *pixels,
830 int tex_target, int tex_format, int tex_type)
832 int width = in_width * w_num / w_den;
833 int full_width = in_full_width * w_num / w_den;
834 int height = in_height * h_num / h_den;
835 int full_height = in_full_height * h_num / h_den;
836 // This unpack alignment is the default, but setting it just in case.
837 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
838 #ifndef GL_UNPACK_ROW_LENGTH
839 int dst_width = full_upload ? full_width : width;
840 int dst_pitch = ALIGN(dst_width * pixel_pitch, 4);
841 if ( pitch != dst_pitch )
843 int buf_size = dst_pitch * full_height * pixel_pitch;
844 const uint8_t *source = pixels;
845 uint8_t *destination;
846 if( !vgl->texture_temp_buf || vgl->texture_temp_buf_size < buf_size )
848 free( vgl->texture_temp_buf );
849 vgl->texture_temp_buf = xmalloc( buf_size );
850 vgl->texture_temp_buf_size = buf_size;
852 destination = vgl->texture_temp_buf;
854 for( int h = 0; h < height ; h++ )
856 memcpy( destination, source, width * pixel_pitch );
858 destination += dst_pitch;
861 glTexImage2D( tex_target, 0, tex_format,
862 full_width, full_height,
863 0, tex_format, tex_type, vgl->texture_temp_buf );
865 glTexSubImage2D( tex_target, 0,
868 tex_format, tex_type, vgl->texture_temp_buf );
875 glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch / pixel_pitch);
878 glTexImage2D(tex_target, 0, tex_format,
879 full_width, full_height,
880 0, tex_format, tex_type, pixels);
882 glTexSubImage2D(tex_target, 0,
885 tex_format, tex_type, pixels);
889 int vout_display_opengl_Prepare(vout_display_opengl_t *vgl,
890 picture_t *picture, subpicture_t *subpicture)
892 if (vlc_gl_Lock(vgl->gl))
895 /* Update the texture */
896 for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
897 if (vgl->use_multitexture) {
898 glActiveTexture(GL_TEXTURE0 + j);
899 glClientActiveTexture(GL_TEXTURE0 + j);
901 glBindTexture(vgl->tex_target, vgl->texture[0][j]);
903 Upload(vgl, picture->format.i_visible_width, vgl->fmt.i_visible_height,
904 vgl->fmt.i_width, vgl->fmt.i_height,
905 vgl->chroma->p[j].w.num, vgl->chroma->p[j].w.den, vgl->chroma->p[j].h.num, vgl->chroma->p[j].h.den,
906 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);
909 int last_count = vgl->region_count;
910 gl_region_t *last = vgl->region;
912 vgl->region_count = 0;
918 for (subpicture_region_t *r = subpicture->p_region; r; r = r->p_next)
921 vgl->region_count = count;
922 vgl->region = calloc(count, sizeof(*vgl->region));
924 if (vgl->use_multitexture) {
925 glActiveTexture(GL_TEXTURE0 + 0);
926 glClientActiveTexture(GL_TEXTURE0 + 0);
929 for (subpicture_region_t *r = subpicture->p_region; r; r = r->p_next, i++) {
930 gl_region_t *glr = &vgl->region[i];
932 glr->format = GL_RGBA;
933 glr->type = GL_UNSIGNED_BYTE;
934 glr->width = r->fmt.i_visible_width;
935 glr->height = r->fmt.i_visible_height;
936 if (!vgl->supports_npot) {
937 glr->width = GetAlignedSize(glr->width);
938 glr->height = GetAlignedSize(glr->height);
939 glr->tex_width = (float) r->fmt.i_visible_width / glr->width;
940 glr->tex_height = (float) r->fmt.i_visible_height / glr->height;
942 glr->tex_width = 1.0;
943 glr->tex_height = 1.0;
945 glr->alpha = (float)subpicture->i_alpha * r->i_alpha / 255 / 255;
946 glr->left = 2.0 * (r->i_x ) / subpicture->i_original_picture_width - 1.0;
947 glr->top = -2.0 * (r->i_y ) / subpicture->i_original_picture_height + 1.0;
948 glr->right = 2.0 * (r->i_x + r->fmt.i_visible_width ) / subpicture->i_original_picture_width - 1.0;
949 glr->bottom = -2.0 * (r->i_y + r->fmt.i_visible_height) / subpicture->i_original_picture_height + 1.0;
952 /* Try to recycle the textures allocated by the previous
953 call to this function. */
954 for (int j = 0; j < last_count; j++) {
955 if (last[j].texture &&
956 last[j].width == glr->width &&
957 last[j].height == glr->height &&
958 last[j].format == glr->format &&
959 last[j].type == glr->type) {
960 glr->texture = last[j].texture;
961 memset(&last[j], 0, sizeof(last[j]));
966 const int pixels_offset = r->fmt.i_y_offset * r->p_picture->p->i_pitch +
967 r->fmt.i_x_offset * r->p_picture->p->i_pixel_pitch;
969 /* A texture was successfully recycled, reuse it. */
970 glBindTexture(GL_TEXTURE_2D, glr->texture);
971 Upload(vgl, r->fmt.i_visible_width, r->fmt.i_visible_height, glr->width, glr->height, 1, 1, 1, 1,
972 r->p_picture->p->i_pitch, r->p_picture->p->i_pixel_pitch, 0,
973 &r->p_picture->p->p_pixels[pixels_offset], GL_TEXTURE_2D, glr->format, glr->type);
975 /* Could not recycle a previous texture, generate a new one. */
976 glGenTextures(1, &glr->texture);
977 glBindTexture(GL_TEXTURE_2D, glr->texture);
979 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_PRIORITY, 1.0);
980 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
982 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
983 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
984 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
985 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
986 Upload(vgl, r->fmt.i_visible_width, r->fmt.i_visible_height, glr->width, glr->height, 1, 1, 1, 1,
987 r->p_picture->p->i_pitch, r->p_picture->p->i_pixel_pitch, 1,
988 &r->p_picture->p->p_pixels[pixels_offset], GL_TEXTURE_2D, glr->format, glr->type);
992 for (int i = 0; i < last_count; i++) {
994 glDeleteTextures(1, &last[i].texture);
998 vlc_gl_Unlock(vgl->gl);
999 VLC_UNUSED(subpicture);
1003 static const GLfloat identity[] = {
1004 1.0f, 0.0f, 0.0f, 0.0f,
1005 0.0f, 1.0f, 0.0f, 0.0f,
1006 0.0f, 0.0f, 1.0f, 0.0f,
1007 0.0f, 0.0f, 0.0f, 1.0f
1010 static void orientationTransformMatrix(GLfloat matrix[static 16], video_orientation_t orientation) {
1012 memcpy(matrix, identity, sizeof(identity));
1014 const int k_cos_pi = -1;
1015 const int k_cos_pi_2 = 0;
1016 const int k_cos_n_pi_2 = 0;
1018 const int k_sin_pi = 0;
1019 const int k_sin_pi_2 = 1;
1020 const int k_sin_n_pi_2 = -1;
1022 bool rotate = false;
1023 int cos = 0, sin = 0;
1025 switch (orientation) {
1027 case ORIENT_ROTATED_90:
1032 case ORIENT_ROTATED_180:
1037 case ORIENT_ROTATED_270:
1042 case ORIENT_HFLIPPED:
1043 matrix[0 * 4 + 0] = -1;
1045 case ORIENT_VFLIPPED:
1046 matrix[1 * 4 + 1] = -1;
1048 case ORIENT_TRANSPOSED:
1049 matrix[0 * 4 + 0] = 0;
1050 matrix[0 * 4 + 1] = -1;
1051 matrix[1 * 4 + 0] = -1;
1052 matrix[1 * 4 + 1] = 0;
1054 case ORIENT_ANTI_TRANSPOSED:
1055 matrix[0 * 4 + 0] = 0;
1056 matrix[0 * 4 + 1] = 1;
1057 matrix[1 * 4 + 0] = 1;
1058 matrix[1 * 4 + 1] = 0;
1066 matrix[0 * 4 + 0] = cos;
1067 matrix[0 * 4 + 1] = -sin;
1068 matrix[1 * 4 + 0] = sin;
1069 matrix[1 * 4 + 1] = cos;
1073 #ifdef SUPPORTS_FIXED_PIPELINE
1074 static void DrawWithoutShaders(vout_display_opengl_t *vgl,
1075 float *left, float *top, float *right, float *bottom)
1077 static const GLfloat vertexCoord[] = {
1084 const GLfloat textureCoord[] = {
1086 right[0], bottom[0],
1091 GLfloat transformMatrix[16];
1092 orientationTransformMatrix(transformMatrix, vgl->fmt.orientation);
1095 glMatrixMode(GL_MODELVIEW);
1096 glLoadMatrixf(transformMatrix);
1098 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
1099 glEnable(vgl->tex_target);
1100 glActiveTexture(GL_TEXTURE0 + 0);
1101 glClientActiveTexture(GL_TEXTURE0 + 0);
1103 glBindTexture(vgl->tex_target, vgl->texture[0][0]);
1105 glEnableClientState(GL_VERTEX_ARRAY);
1106 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1108 glTexCoordPointer(2, GL_FLOAT, 0, textureCoord);
1109 glVertexPointer(2, GL_FLOAT, 0, vertexCoord);
1111 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1113 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1114 glDisableClientState(GL_VERTEX_ARRAY);
1115 glDisable(vgl->tex_target);
1122 #ifdef SUPPORTS_SHADERS
1123 static void DrawWithShaders(vout_display_opengl_t *vgl,
1124 float *left, float *top, float *right, float *bottom,
1127 vgl->UseProgram(vgl->program[program]);
1129 if (vgl->chroma->plane_count == 3) {
1130 vgl->Uniform4fv(vgl->GetUniformLocation(vgl->program[0], "Coefficient"), 4, vgl->local_value);
1131 vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[0], "Texture0"), 0);
1132 vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[0], "Texture1"), 1);
1133 vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[0], "Texture2"), 2);
1135 else if (vgl->chroma->plane_count == 1) {
1136 vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[0], "Texture0"), 0);
1139 vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[1], "Texture0"), 0);
1140 vgl->Uniform4f(vgl->GetUniformLocation(vgl->program[1], "FillColor"), 1.0f, 1.0f, 1.0f, 1.0f);
1143 static const GLfloat vertexCoord[] = {
1150 GLfloat transformMatrix[16];
1151 orientationTransformMatrix(transformMatrix, vgl->fmt.orientation);
1153 for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
1154 const GLfloat textureCoord[] = {
1158 right[j], bottom[j],
1160 glActiveTexture(GL_TEXTURE0+j);
1161 glClientActiveTexture(GL_TEXTURE0+j);
1162 glBindTexture(vgl->tex_target, vgl->texture[0][j]);
1164 vgl->BindBuffer(GL_ARRAY_BUFFER, vgl->texture_buffer_object[j]);
1165 vgl->BufferData(GL_ARRAY_BUFFER, sizeof(textureCoord), textureCoord, GL_STATIC_DRAW);
1168 snprintf(attribute, sizeof(attribute), "MultiTexCoord%1d", j);
1169 vgl->EnableVertexAttribArray(vgl->GetAttribLocation(vgl->program[program], attribute));
1170 vgl->VertexAttribPointer(vgl->GetAttribLocation(vgl->program[program], attribute), 2, GL_FLOAT, 0, 0, 0);
1172 glActiveTexture(GL_TEXTURE0 + 0);
1173 glClientActiveTexture(GL_TEXTURE0 + 0);
1175 vgl->BindBuffer(GL_ARRAY_BUFFER, vgl->vertex_buffer_object);
1176 vgl->BufferData(GL_ARRAY_BUFFER, sizeof(vertexCoord), vertexCoord, GL_STATIC_DRAW);
1177 vgl->EnableVertexAttribArray(vgl->GetAttribLocation(vgl->program[program], "VertexPosition"));
1178 vgl->VertexAttribPointer(vgl->GetAttribLocation(vgl->program[program], "VertexPosition"), 2, GL_FLOAT, 0, 0, 0);
1180 vgl->UniformMatrix4fv(vgl->GetUniformLocation(vgl->program[program], "RotationMatrix"), 1, GL_FALSE, transformMatrix);
1182 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1186 int vout_display_opengl_Display(vout_display_opengl_t *vgl,
1187 const video_format_t *source)
1189 if (vlc_gl_Lock(vgl->gl))
1190 return VLC_EGENERIC;
1192 /* Why drawing here and not in Render()? Because this way, the
1193 OpenGL providers can call vout_display_opengl_Display to force redraw.i
1194 Currently, the OS X provider uses it to get a smooth window resizing */
1195 glClear(GL_COLOR_BUFFER_BIT);
1197 /* Draw the picture */
1198 float left[PICTURE_PLANE_MAX];
1199 float top[PICTURE_PLANE_MAX];
1200 float right[PICTURE_PLANE_MAX];
1201 float bottom[PICTURE_PLANE_MAX];
1202 for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
1203 /* glTexCoord works differently with GL_TEXTURE_2D and
1204 GL_TEXTURE_RECTANGLE_EXT */
1205 float scale_w, scale_h;
1207 if (vgl->tex_target == GL_TEXTURE_2D) {
1208 scale_w = (float)vgl->chroma->p[j].w.num / vgl->chroma->p[j].w.den / vgl->tex_width[j];
1209 scale_h = (float)vgl->chroma->p[j].h.num / vgl->chroma->p[j].h.den / vgl->tex_height[j];
1215 /* Warning: if NPOT is not supported a larger texture is
1216 allocated. This will cause right and bottom coordinates to
1217 land on the edge of two texels with the texels to the
1218 right/bottom uninitialized by the call to
1219 glTexSubImage2D. This might cause a green line to appear on
1220 the right/bottom of the display.
1221 There are two possible solutions:
1222 - Manually mirror the edges of the texture.
1223 - Add a "-1" when computing right and bottom, however the
1224 last row/column might not be displayed at all.
1226 left[j] = (source->i_x_offset + 0 ) * scale_w;
1227 top[j] = (source->i_y_offset + 0 ) * scale_h;
1228 right[j] = (source->i_x_offset + source->i_visible_width ) * scale_w;
1229 bottom[j] = (source->i_y_offset + source->i_visible_height) * scale_h;
1232 #ifdef SUPPORTS_SHADERS
1233 if (vgl->program[0] && (vgl->chroma->plane_count == 3 || vgl->chroma->plane_count == 1))
1234 DrawWithShaders(vgl, left, top, right, bottom, 0);
1235 else if (vgl->program[1] && vgl->chroma->plane_count == 1)
1236 DrawWithShaders(vgl, left, top, right, bottom, 1);
1240 #ifdef SUPPORTS_FIXED_PIPELINE
1241 DrawWithoutShaders(vgl, left, top, right, bottom);
1245 /* Draw the subpictures */
1246 if (vgl->program[1]) {
1247 #ifdef SUPPORTS_SHADERS
1248 // Change the program for overlays
1249 vgl->UseProgram(vgl->program[1]);
1250 vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[1], "Texture"), 0);
1254 #ifdef SUPPORTS_FIXED_PIPELINE
1255 glEnable(GL_TEXTURE_2D);
1258 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1260 #ifdef SUPPORTS_SHADERS
1261 /* We need two buffer objects for each region: for vertex and texture coordinates. */
1262 if (2 * vgl->region_count > vgl->subpicture_buffer_object_count) {
1263 if (vgl->subpicture_buffer_object_count > 0)
1264 vgl->DeleteBuffers(vgl->subpicture_buffer_object_count, vgl->subpicture_buffer_object);
1265 vgl->subpicture_buffer_object_count = 0;
1267 int new_count = 2 * vgl->region_count;
1268 vgl->subpicture_buffer_object = realloc_or_free(vgl->subpicture_buffer_object, new_count * sizeof(GLuint));
1269 if (!vgl->subpicture_buffer_object) {
1270 vlc_gl_Unlock(vgl->gl);
1274 vgl->subpicture_buffer_object_count = new_count;
1275 vgl->GenBuffers(vgl->subpicture_buffer_object_count, vgl->subpicture_buffer_object);
1279 glActiveTexture(GL_TEXTURE0 + 0);
1280 glClientActiveTexture(GL_TEXTURE0 + 0);
1281 for (int i = 0; i < vgl->region_count; i++) {
1282 gl_region_t *glr = &vgl->region[i];
1283 const GLfloat vertexCoord[] = {
1284 glr->left, glr->top,
1285 glr->left, glr->bottom,
1286 glr->right, glr->top,
1287 glr->right, glr->bottom,
1289 const GLfloat textureCoord[] = {
1291 0.0, glr->tex_height,
1292 glr->tex_width, 0.0,
1293 glr->tex_width, glr->tex_height,
1296 glBindTexture(GL_TEXTURE_2D, glr->texture);
1297 if (vgl->program[1]) {
1298 #ifdef SUPPORTS_SHADERS
1299 vgl->Uniform4f(vgl->GetUniformLocation(vgl->program[1], "FillColor"), 1.0f, 1.0f, 1.0f, glr->alpha);
1301 vgl->BindBuffer(GL_ARRAY_BUFFER, vgl->subpicture_buffer_object[2 * i]);
1302 vgl->BufferData(GL_ARRAY_BUFFER, sizeof(textureCoord), textureCoord, GL_STATIC_DRAW);
1303 vgl->EnableVertexAttribArray(vgl->GetAttribLocation(vgl->program[1], "MultiTexCoord0"));
1304 vgl->VertexAttribPointer(vgl->GetAttribLocation(vgl->program[1], "MultiTexCoord0"), 2, GL_FLOAT, 0, 0, 0);
1306 vgl->BindBuffer(GL_ARRAY_BUFFER, vgl->subpicture_buffer_object[2 * i + 1]);
1307 vgl->BufferData(GL_ARRAY_BUFFER, sizeof(vertexCoord), vertexCoord, GL_STATIC_DRAW);
1308 vgl->EnableVertexAttribArray(vgl->GetAttribLocation(vgl->program[1], "VertexPosition"));
1309 vgl->VertexAttribPointer(vgl->GetAttribLocation(vgl->program[1], "VertexPosition"), 2, GL_FLOAT, 0, 0, 0);
1311 // Subpictures have the correct orientation:
1312 vgl->UniformMatrix4fv(vgl->GetUniformLocation(vgl->program[1], "RotationMatrix"), 1, GL_FALSE, identity);
1315 #ifdef SUPPORTS_FIXED_PIPELINE
1316 glEnableClientState(GL_VERTEX_ARRAY);
1317 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
1318 glColor4f(1.0f, 1.0f, 1.0f, glr->alpha);
1319 glTexCoordPointer(2, GL_FLOAT, 0, textureCoord);
1320 glVertexPointer(2, GL_FLOAT, 0, vertexCoord);
1324 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1326 if (!vgl->program[1]) {
1327 #ifdef SUPPORTS_FIXED_PIPELINE
1328 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1329 glDisableClientState(GL_VERTEX_ARRAY);
1333 glDisable(GL_BLEND);
1334 #ifdef SUPPORTS_FIXED_PIPELINE
1335 glDisable(GL_TEXTURE_2D);
1339 vlc_gl_Swap(vgl->gl);
1341 vlc_gl_Unlock(vgl->gl);