# define GL_CLAMP_TO_EDGE 0x812F
#endif
-#ifdef __APPLE__
+#if USE_OPENGL_ES == 2 || defined(__APPLE__)
# define PFNGLGETPROGRAMIVPROC typeof(glGetProgramiv)*
# define PFNGLGETPROGRAMINFOLOGPROC typeof(glGetProgramInfoLog)*
# define PFNGLGETSHADERIVPROC typeof(glGetShaderiv)*
# define PFNGLUSEPROGRAMPROC typeof(glUseProgram)*
# define PFNGLDELETEPROGRAMPROC typeof(glDeleteProgram)*
# define PFNGLATTACHSHADERPROC typeof(glAttachShader)*
-#if USE_OPENGL_ES
+#if defined(__APPLE__) && USE_OPENGL_ES
# import <CoreFoundation/CoreFoundation.h>
#endif
#endif
# define VLCGL_TEXTURE_COUNT 1
# define VLCGL_PICTURE_MAX 1
# define PRECISION "precision highp float;"
+#if USE_OPENGL_ES == 2
+# define SUPPORTS_SHADERS
+# define glClientActiveTexture(x)
+#else
+# define SUPPORTS_FIXED_PIPELINE
+# define GL_MAX_TEXTURE_IMAGE_UNITS GL_MAX_TEXTURE_UNITS
+#endif
#else
# define GLSL_VERSION "120"
# define VLCGL_TEXTURE_COUNT 1
# define VLCGL_PICTURE_MAX 128
# define PRECISION ""
+# define SUPPORTS_SHADERS
+# define SUPPORTS_FIXED_PIPELINE
#endif
static const vlc_fourcc_t gl_subpicture_chromas[] = {
float left;
float bottom;
float right;
+
+ float tex_width;
+ float tex_height;
} gl_region_t;
struct vout_display_opengl_t {
GLfloat local_value[16];
/* Shader variables commands*/
+#ifdef SUPPORTS_SHADERS
PFNGLGETUNIFORMLOCATIONPROC GetUniformLocation;
PFNGLGETATTRIBLOCATIONPROC GetAttribLocation;
PFNGLVERTEXATTRIBPOINTERPROC VertexAttribPointer;
PFNGLGETPROGRAMINFOLOGPROC GetProgramInfoLog;
PFNGLGETSHADERIVPROC GetShaderiv;
PFNGLGETSHADERINFOLOGPROC GetShaderInfoLog;
+#endif
/* multitexture */
/* Non-power-of-2 texture size support */
bool supports_npot;
+
+ uint8_t *texture_temp_buf;
+ int texture_temp_buf_size;
};
static inline int GetAlignedSize(unsigned size)
}
#endif
+#ifdef SUPPORTS_SHADERS
static void BuildVertexShader(vout_display_opengl_t *vgl,
GLint *shader)
{
vgl->ShaderSource(*shader, 1, &code, NULL);
vgl->CompileShader(*shader);
}
+#endif
vout_display_opengl_t *vout_display_opengl_New(video_format_t *fmt,
const vlc_fourcc_t **subpicture_chromas,
bool supports_shaders = false;
#endif
+#if USE_OPENGL_ES == 2
+ vgl->CreateShader = glCreateShader;
+ vgl->ShaderSource = glShaderSource;
+ vgl->CompileShader = glCompileShader;
+ vgl->AttachShader = glAttachShader;
+
+ vgl->GetProgramiv = glGetProgramiv;
+ vgl->GetShaderiv = glGetShaderiv;
+ vgl->GetProgramInfoLog = glGetProgramInfoLog;
+ vgl->GetShaderInfoLog = glGetShaderInfoLog;
+
+ vgl->DeleteShader = glDeleteShader;
+
+ vgl->GetUniformLocation = glGetUniformLocation;
+ vgl->GetAttribLocation = glGetAttribLocation;
+ vgl->VertexAttribPointer= glVertexAttribPointer;
+ vgl->EnableVertexAttribArray = glEnableVertexAttribArray;
+ vgl->Uniform4fv = glUniform4fv;
+ vgl->Uniform4f = glUniform4f;
+ vgl->Uniform1i = glUniform1i;
+
+ vgl->CreateProgram = glCreateProgram;
+ vgl->LinkProgram = glLinkProgram;
+ vgl->UseProgram = glUseProgram;
+ vgl->DeleteProgram = glDeleteProgram;
+ supports_shaders = true;
+#elif defined(SUPPORTS_SHADERS)
vgl->CreateShader = (PFNGLCREATESHADERPROC)vlc_gl_GetProcAddress(vgl->gl, "glCreateShader");
vgl->ShaderSource = (PFNGLSHADERSOURCEPROC)vlc_gl_GetProcAddress(vgl->gl, "glShaderSource");
vgl->CompileShader = (PFNGLCOMPILESHADERPROC)vlc_gl_GetProcAddress(vgl->gl, "glCompileShader");
if (!vgl->CreateShader || !vgl->ShaderSource || !vgl->CreateProgram)
supports_shaders = false;
+#endif
vgl->supports_npot = HasExtension(extensions, "GL_ARB_texture_non_power_of_two") ||
HasExtension(extensions, "GL_APPLE_texture_2D_limited_npot");
-#if USE_OPENGL_ES
+#if USE_OPENGL_ES == 2
/* OpenGL ES 2 includes support for non-power of 2 textures by specification
* so checks for extensions are bound to fail. Check for OpenGL ES version instead. */
- const unsigned char *ogl_version = glGetString(GL_VERSION);
- if (strverscmp((const char *)ogl_version, "2.0") >= 0)
- vgl->supports_npot = true;
+ vgl->supports_npot = true;
#endif
GLint max_texture_units = 0;
vgl->tex_type = GL_UNSIGNED_BYTE;
/* Use YUV if possible and needed */
bool need_fs_yuv = false;
+ bool need_fs_rgba = USE_OPENGL_ES == 2;
float yuv_range_correction = 1.0;
if (max_texture_units >= 3 && supports_shaders && vlc_fourcc_IsYUV(fmt->i_chroma)) {
vgl->shader[1] =
vgl->shader[2] = -1;
vgl->local_count = 0;
- if (supports_shaders && need_fs_yuv) {
+ if (supports_shaders && (need_fs_yuv || need_fs_rgba)) {
+#ifdef SUPPORTS_SHADERS
BuildYUVFragmentShader(vgl, &vgl->shader[0], &vgl->local_count,
vgl->local_value, fmt, yuv_range_correction);
BuildRGBAFragmentShader(vgl, &vgl->shader[1]);
return NULL;
}
}
+#else
+ (void)yuv_range_correction;
+#endif
}
/* */
}
free(vgl->region);
+#ifdef SUPPORTS_SHADERS
if (vgl->program[0]) {
for (int i = 0; i < 2; i++)
vgl->DeleteProgram(vgl->program[i]);
for (int i = 0; i < 3; i++)
vgl->DeleteShader(vgl->shader[i]);
}
+#endif
+ free(vgl->texture_temp_buf);
vlc_gl_Unlock(vgl->gl);
}
if (vgl->pool)
return NULL;
}
+#define ALIGN(x, y) (((x) + ((y) - 1)) & ~((y) - 1))
+static void Upload(vout_display_opengl_t *vgl, int in_width, int in_height,
+ int in_full_width, int in_full_height,
+ int w_num, int w_den, int h_num, int h_den,
+ int pitch, int pixel_pitch,
+ int full_upload, const uint8_t *pixels,
+ int tex_target, int tex_format, int tex_type)
+{
+ int width = in_width * w_num / w_den;
+ int full_width = in_full_width * w_num / w_den;
+ int height = in_height * h_num / h_den;
+ int full_height = in_full_height * h_num / h_den;
+ // This unpack alignment is the default, but setting it just in case.
+ glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
+#ifndef GL_UNPACK_ROW_LENGTH
+ int dst_width = full_upload ? full_width : width;
+ int dst_pitch = ALIGN(dst_width * pixel_pitch, 4);
+ if ( pitch != dst_pitch )
+ {
+ int buf_size = dst_pitch * full_height * pixel_pitch;
+ const uint8_t *source = pixels;
+ uint8_t *destination;
+ if( !vgl->texture_temp_buf || vgl->texture_temp_buf_size < buf_size )
+ {
+ free( vgl->texture_temp_buf );
+ vgl->texture_temp_buf = xmalloc( buf_size );
+ vgl->texture_temp_buf_size = buf_size;
+ }
+ destination = vgl->texture_temp_buf;
+
+ for( int h = 0; h < height ; h++ )
+ {
+ memcpy( destination, source, width * pixel_pitch );
+ source += pitch;
+ destination += dst_pitch;
+ }
+ if (full_upload)
+ glTexImage2D( tex_target, 0, tex_format,
+ full_width, full_height,
+ 0, tex_format, tex_type, vgl->texture_temp_buf );
+ else
+ glTexSubImage2D( tex_target, 0,
+ 0, 0,
+ width, height,
+ tex_format, tex_type, vgl->texture_temp_buf );
+ } else {
+#else
+ (void) width;
+ (void) height;
+ (void) vgl;
+ {
+ glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch / pixel_pitch);
+#endif
+ if (full_upload)
+ glTexImage2D(tex_target, 0, tex_format,
+ full_width, full_height,
+ 0, tex_format, tex_type, pixels);
+ else
+ glTexSubImage2D(tex_target, 0,
+ 0, 0,
+ width, height,
+ tex_format, tex_type, pixels);
+ }
+}
+
int vout_display_opengl_Prepare(vout_display_opengl_t *vgl,
picture_t *picture, subpicture_t *subpicture)
{
}
glBindTexture(vgl->tex_target, vgl->texture[0][j]);
-#ifndef GL_UNPACK_ROW_LENGTH
- if ( (picture->p[j].i_pitch / picture->p[j].i_pixel_pitch) != (unsigned int)
- ( picture->format.i_visible_width * vgl->chroma->p[j].w.num / vgl->chroma->p[j].w.den ) )
- {
- uint8_t *new_plane = malloc( picture->format.i_visible_width * vgl->fmt.i_visible_height * vgl->chroma->p[j].h.num / (vgl->chroma->p[j].h.den * vgl->chroma->p[j].w.den ) );
- uint8_t *destination = new_plane;
- const uint8_t *source = picture->p[j].p_pixels;
-
- for( unsigned height = 0; height < (vgl->fmt.i_visible_height * vgl->chroma->p[j].h.num / vgl->chroma->p[j].h.den) ; height++ )
- {
- memcpy( destination, source, picture->format.i_visible_width * vgl->chroma->p[j].w.num / vgl->chroma->p[j].w.den );
- source += picture->p[j].i_pitch / picture->p[j].i_pixel_pitch;
- destination += picture->format.i_visible_width * vgl->chroma->p[j].w.num / vgl->chroma->p[j].w.den;
- }
- glTexSubImage2D( vgl->tex_target, 0,
- 0, 0,
- picture->format.i_visible_width * vgl->chroma->p[j].w.num / vgl->chroma->p[j].w.den,
- vgl->fmt.i_height * vgl->chroma->p[j].h.num / vgl->chroma->p[j].h.den,
- vgl->tex_format, vgl->tex_type, new_plane );
- free( new_plane );
- } else {
-#else
- glPixelStorei(GL_UNPACK_ROW_LENGTH, picture->p[j].i_pitch / picture->p[j].i_pixel_pitch);
-#endif
- glTexSubImage2D(vgl->tex_target, 0,
- 0, 0,
- vgl->fmt.i_width * vgl->chroma->p[j].w.num / vgl->chroma->p[j].w.den,
- vgl->fmt.i_height * vgl->chroma->p[j].h.num / vgl->chroma->p[j].h.den,
- vgl->tex_format, vgl->tex_type, picture->p[j].p_pixels);
-#ifndef GL_UNPACK_ROW_LENGTH
- }
-#endif
+ Upload(vgl, picture->format.i_visible_width, vgl->fmt.i_visible_height,
+ vgl->fmt.i_width, vgl->fmt.i_height,
+ vgl->chroma->p[j].w.num, vgl->chroma->p[j].w.den, vgl->chroma->p[j].h.num, vgl->chroma->p[j].h.den,
+ 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);
}
int last_count = vgl->region_count;
if (!vgl->supports_npot) {
glr->width = GetAlignedSize(glr->width);
glr->height = GetAlignedSize(glr->height);
+ glr->tex_width = (float) r->fmt.i_visible_width / glr->width;
+ glr->tex_height = (float) r->fmt.i_visible_height / glr->height;
+ } else {
+ glr->tex_width = 1.0;
+ glr->tex_height = 1.0;
}
glr->alpha = (float)subpicture->i_alpha * r->i_alpha / 255 / 255;
glr->left = 2.0 * (r->i_x ) / subpicture->i_original_picture_width - 1.0;
r->fmt.i_x_offset * r->p_picture->p->i_pixel_pitch;
if (glr->texture) {
glBindTexture(GL_TEXTURE_2D, glr->texture);
- /* TODO set GL_UNPACK_ALIGNMENT */
-#ifdef GL_UNPACK_ROW_LENGTH
- glPixelStorei(GL_UNPACK_ROW_LENGTH, r->p_picture->p->i_pitch / r->p_picture->p->i_pixel_pitch);
-#endif
- glTexSubImage2D(GL_TEXTURE_2D, 0,
- 0, 0, glr->width, glr->height,
- glr->format, glr->type, &r->p_picture->p->p_pixels[pixels_offset]);
+ Upload(vgl, r->fmt.i_visible_width, r->fmt.i_visible_height, glr->width, glr->height, 1, 1, 1, 1,
+ r->p_picture->p->i_pitch, r->p_picture->p->i_pixel_pitch, 0,
+ &r->p_picture->p->p_pixels[pixels_offset], GL_TEXTURE_2D, glr->format, glr->type);
} else {
glGenTextures(1, &glr->texture);
glBindTexture(GL_TEXTURE_2D, glr->texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
- /* TODO set GL_UNPACK_ALIGNMENT */
-#ifdef GL_UNPACK_ROW_LENGTH
- glPixelStorei(GL_UNPACK_ROW_LENGTH, r->p_picture->p->i_pitch / r->p_picture->p->i_pixel_pitch);
-#endif
- glTexImage2D(GL_TEXTURE_2D, 0, glr->format,
- glr->width, glr->height, 0, glr->format, glr->type,
- &r->p_picture->p->p_pixels[pixels_offset]);
+ Upload(vgl, r->fmt.i_visible_width, r->fmt.i_visible_height, glr->width, glr->height, 1, 1, 1, 1,
+ r->p_picture->p->i_pitch, r->p_picture->p->i_pixel_pitch, 1,
+ &r->p_picture->p->p_pixels[pixels_offset], GL_TEXTURE_2D, glr->format, glr->type);
}
}
}
return VLC_SUCCESS;
}
+#ifdef SUPPORTS_FIXED_PIPELINE
static void DrawWithoutShaders(vout_display_opengl_t *vgl,
float *left, float *top, float *right, float *bottom)
{
right[0], top[0]
};
+ glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glEnable(vgl->tex_target);
glActiveTexture(GL_TEXTURE0 + 0);
glClientActiveTexture(GL_TEXTURE0 + 0);
glDisableClientState(GL_VERTEX_ARRAY);
glDisable(vgl->tex_target);
}
+#endif
+#ifdef SUPPORTS_SHADERS
static void DrawWithShaders(vout_display_opengl_t *vgl,
- float *left, float *top, float *right, float *bottom)
+ float *left, float *top, float *right, float *bottom,
+ int program)
{
- vgl->UseProgram(vgl->program[0]);
- vgl->Uniform4fv(vgl->GetUniformLocation(vgl->program[0], "Coefficient"), 4, vgl->local_value);
- vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[0], "Texture0"), 0);
- vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[0], "Texture1"), 1);
- vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[0], "Texture2"), 2);
+ vgl->UseProgram(vgl->program[program]);
+ if (program == 0) {
+ vgl->Uniform4fv(vgl->GetUniformLocation(vgl->program[0], "Coefficient"), 4, vgl->local_value);
+ vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[0], "Texture0"), 0);
+ vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[0], "Texture1"), 1);
+ vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[0], "Texture2"), 2);
+ } else {
+ vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[1], "Texture0"), 0);
+ vgl->Uniform4f(vgl->GetUniformLocation(vgl->program[1], "FillColor"), 1.0f, 1.0f, 1.0f, 1.0f);
+ }
static const GLfloat vertexCoord[] = {
-1.0, 1.0,
};
glActiveTexture(GL_TEXTURE0+j);
glClientActiveTexture(GL_TEXTURE0+j);
- glEnable(vgl->tex_target);
glBindTexture(vgl->tex_target, vgl->texture[0][j]);
char attribute[20];
snprintf(attribute, sizeof(attribute), "MultiTexCoord%1d", j);
- vgl->EnableVertexAttribArray(vgl->GetAttribLocation(vgl->program[0], attribute));
- vgl->VertexAttribPointer(vgl->GetAttribLocation(vgl->program[0], attribute), 2, GL_FLOAT, 0, 0, textureCoord);
+ vgl->EnableVertexAttribArray(vgl->GetAttribLocation(vgl->program[program], attribute));
+ vgl->VertexAttribPointer(vgl->GetAttribLocation(vgl->program[program], attribute), 2, GL_FLOAT, 0, 0, textureCoord);
}
glActiveTexture(GL_TEXTURE0 + 0);
glClientActiveTexture(GL_TEXTURE0 + 0);
- vgl->EnableVertexAttribArray(vgl->GetAttribLocation(vgl->program[0], "VertexPosition"));
- vgl->VertexAttribPointer(vgl->GetAttribLocation(vgl->program[0], "VertexPosition"), 2, GL_FLOAT, 0, 0, vertexCoord);
+ vgl->EnableVertexAttribArray(vgl->GetAttribLocation(vgl->program[program], "VertexPosition"));
+ vgl->VertexAttribPointer(vgl->GetAttribLocation(vgl->program[program], "VertexPosition"), 2, GL_FLOAT, 0, 0, vertexCoord);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
+#endif
int vout_display_opengl_Display(vout_display_opengl_t *vgl,
const video_format_t *source)
bottom[j] = (source->i_y_offset + source->i_visible_height) * scale_h;
}
- if (vgl->program[0])
- DrawWithShaders(vgl, left, top ,right, bottom);
+#ifdef SUPPORTS_SHADERS
+ if (vgl->program[0] && vgl->chroma->plane_count == 3)
+ DrawWithShaders(vgl, left, top, right, bottom, 0);
+ else if (vgl->program[1] && vgl->chroma->plane_count == 1)
+ DrawWithShaders(vgl, left, top, right, bottom, 1);
else
+#endif
+ {
+#ifdef SUPPORTS_FIXED_PIPELINE
DrawWithoutShaders(vgl, left, top, right, bottom);
+#endif
+ }
/* Draw the subpictures */
if (vgl->program[1]) {
+#ifdef SUPPORTS_SHADERS
// Change the program for overlays
vgl->UseProgram(vgl->program[1]);
vgl->Uniform1i(vgl->GetUniformLocation(vgl->program[1], "Texture"), 0);
+#endif
}
+#ifdef SUPPORTS_FIXED_PIPELINE
glEnable(GL_TEXTURE_2D);
+#endif
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glr->right, glr->top,
glr->right, glr->bottom,
};
- static const GLfloat textureCoord[] = {
+ const GLfloat textureCoord[] = {
0.0, 0.0,
- 0.0, 1.0,
- 1.0, 0.0,
- 1.0, 1.0,
+ 0.0, glr->tex_height,
+ glr->tex_width, 0.0,
+ glr->tex_width, glr->tex_height,
};
glBindTexture(GL_TEXTURE_2D, glr->texture);
if (vgl->program[1]) {
+#ifdef SUPPORTS_SHADERS
vgl->Uniform4f(vgl->GetUniformLocation(vgl->program[1], "FillColor"), 1.0f, 1.0f, 1.0f, glr->alpha);
vgl->EnableVertexAttribArray(vgl->GetAttribLocation(vgl->program[1], "MultiTexCoord0"));
vgl->VertexAttribPointer(vgl->GetAttribLocation(vgl->program[1], "MultiTexCoord0"), 2, GL_FLOAT, 0, 0, textureCoord);
vgl->EnableVertexAttribArray(vgl->GetAttribLocation(vgl->program[1], "VertexPosition"));
vgl->VertexAttribPointer(vgl->GetAttribLocation(vgl->program[1], "VertexPosition"), 2, GL_FLOAT, 0, 0, vertexCoord);
+#endif
} else {
+#ifdef SUPPORTS_FIXED_PIPELINE
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glColor4f(1.0f, 1.0f, 1.0f, glr->alpha);
glTexCoordPointer(2, GL_FLOAT, 0, textureCoord);
glVertexPointer(2, GL_FLOAT, 0, vertexCoord);
+#endif
}
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
if (!vgl->program[1]) {
+#ifdef SUPPORTS_FIXED_PIPELINE
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
+#endif
}
}
glDisable(GL_BLEND);
+#ifdef SUPPORTS_FIXED_PIPELINE
glDisable(GL_TEXTURE_2D);
+#endif
/* Display */
vlc_gl_Swap(vgl->gl);