Commit 0cd050a9 authored by Ilkka Ollakka's avatar Ilkka Ollakka

Opengl: Rework opengl to use glsl instead of arb extension for yuv->rgb

This doesn't yet fully work with opengl es side. If you notice any regressions
please notify it.
parent a106407a
...@@ -36,16 +36,6 @@ ...@@ -36,16 +36,6 @@
#include "opengl.h" #include "opengl.h"
// Define USE_OPENGL_ES to the GL ES Version you want to select // Define USE_OPENGL_ES to the GL ES Version you want to select
#ifdef __APPLE__
# define PFNGLGENPROGRAMSARBPROC typeof(glGenProgramsARB)*
# define PFNGLBINDPROGRAMARBPROC typeof(glBindProgramARB)*
# define PFNGLPROGRAMSTRINGARBPROC typeof(glProgramStringARB)*
# define PFNGLDELETEPROGRAMSARBPROC typeof(glDeleteProgramsARB)*
# define PFNGLPROGRAMLOCALPARAMETER4FVARBPROC typeof(glProgramLocalParameter4fvARB)*
# define PFNGLACTIVETEXTUREPROC typeof(glActiveTexture)*
# define PFNGLCLIENTACTIVETEXTUREPROC typeof(glClientActiveTexture)*
#endif
/* RV16 */ /* RV16 */
#ifndef GL_UNSIGNED_SHORT_5_6_5 #ifndef GL_UNSIGNED_SHORT_5_6_5
# define GL_UNSIGNED_SHORT_5_6_5 0x8363 # define GL_UNSIGNED_SHORT_5_6_5 0x8363
...@@ -54,6 +44,34 @@ ...@@ -54,6 +44,34 @@
# define GL_CLAMP_TO_EDGE 0x812F # define GL_CLAMP_TO_EDGE 0x812F
#endif #endif
#ifdef __APPLE__
# define PFNGLGENBUFFERSPROC typeof(glGenBuffers)*
# define PFNGLBINDBUFFERPROC typeof(glBindBuffer)*
# define PFNGLDELETEBUFFERSPROC typeof(glDeleteBuffers)*
# define PFNGLBUFFERSUBDATAPROC typeof(glBufferSubData)*
# define PFNGLBUFFERDATAPROC typeof(glBufferData)*
# define PFNGLGETPROGRAMIVPROC typeof(glGetProgramiv)*
# define PFNGLGETPROGRAMINFOLOGPROC typeof(glGetProgramInfoLog)*
# define PFNGLGETSHADERIVPROC typeof(glGetShaderiv)*
# define PFNGLGETSHADERINFOLOGPROC typeof(glGetShaderInfoLog)*
# define PFNGLGETUNIFORMLOCATIONPROC typeof(glGetUniformLocation)*
# define PFNGLGETATTRIBLOCATIONPROC typeof(glGetAttribLocation)*
# define PFNGLUNIFORM4FVPROC typeof(glUniform4fv)*
# define PFNGLUNIFORM4FPROC typeof(glUniform4f)*
# define PFNGLUNIFORM3IPROC typeof(glUniform3i)*
# define PFNGLUNIFORM1IPROC typeof(glUniform1i)*
# define PFNGLCREATESHADERPROC typeof(glCreateShader)*
# define PFNGLSHADERSOURCEPROC typeof(glShaderSource)*
# define PFNGLCOMPILESHADERPROC typeof(glCompileShader)*
# define PFNGLDETACHSHADERPROC typeof(glDetachShader)*
# define PFNGLDELETESHADERPROC typeof(glDeleteShader)*
# define PFNGLCREATEPROGRAMPROC typeof(glCreateProgram)*
# define PFNGLLINKPROGRAMPROC typeof(glLinkProgram)*
# define PFNGLUSEPROGRAMPROC typeof(glUseProgram)*
# define PFNGLDELETEPROGRAMPROC typeof(glDeleteProgram)*
# define PFNGLATTACHSHADERPROC typeof(glAttachShader)*
#endif
#if USE_OPENGL_ES #if USE_OPENGL_ES
# define VLCGL_TEXTURE_COUNT 1 # define VLCGL_TEXTURE_COUNT 1
# define VLCGL_PICTURE_MAX 1 # define VLCGL_PICTURE_MAX 1
...@@ -83,6 +101,7 @@ typedef struct { ...@@ -83,6 +101,7 @@ typedef struct {
} gl_region_t; } gl_region_t;
struct vout_display_opengl_t { struct vout_display_opengl_t {
vlc_gl_t *gl; vlc_gl_t *gl;
video_format_t fmt; video_format_t fmt;
...@@ -104,21 +123,55 @@ struct vout_display_opengl_t { ...@@ -104,21 +123,55 @@ struct vout_display_opengl_t {
picture_pool_t *pool; picture_pool_t *pool;
GLuint program; /* index 0 for normal and 1 for subtitle overlay */
GLuint program[2];
GLint shader[3]; //3. is for the common vertex shader
int local_count; int local_count;
GLfloat local_value[16][4]; GLfloat local_value[16];
/* Buffer commands */
PFNGLGENBUFFERSPROC GenBuffers;
PFNGLBINDBUFFERPROC BindBuffer;
PFNGLDELETEBUFFERSPROC DeleteBuffers;
PFNGLBUFFERSUBDATAPROC BufferSubData;
PFNGLBUFFERDATAPROC BufferData;
/* Shader variables commands*/
PFNGLGETUNIFORMLOCATIONPROC GetUniformLocation;
PFNGLGETATTRIBLOCATIONPROC GetAttribLocation;
PFNGLUNIFORM4FVPROC Uniform4fv;
PFNGLUNIFORM4FPROC Uniform4f;
PFNGLUNIFORM3IPROC Uniform3i;
PFNGLUNIFORM1IPROC Uniform1i;
/* Shader command */
PFNGLCREATESHADERPROC CreateShader;
PFNGLSHADERSOURCEPROC ShaderSource;
PFNGLCOMPILESHADERPROC CompileShader;
PFNGLDETACHSHADERPROC DetachShader;
PFNGLDELETESHADERPROC DeleteShader;
PFNGLCREATEPROGRAMPROC CreateProgram;
PFNGLLINKPROGRAMPROC LinkProgram;
PFNGLUSEPROGRAMPROC UseProgram;
PFNGLDELETEPROGRAMPROC DeleteProgram;
PFNGLATTACHSHADERPROC AttachShader;
/* Shader log commands */
PFNGLGETPROGRAMIVPROC GetProgramiv;
PFNGLGETPROGRAMINFOLOGPROC GetProgramInfoLog;
PFNGLGETSHADERIVPROC GetShaderiv;
PFNGLGETSHADERINFOLOGPROC GetShaderInfoLog;
/* fragment_program */
PFNGLGENPROGRAMSARBPROC GenProgramsARB;
PFNGLBINDPROGRAMARBPROC BindProgramARB;
PFNGLPROGRAMSTRINGARBPROC ProgramStringARB;
PFNGLDELETEPROGRAMSARBPROC DeleteProgramsARB;
PFNGLPROGRAMLOCALPARAMETER4FVARBPROC ProgramLocalParameter4fvARB;
/* multitexture */ /* multitexture */
bool use_multitexture;
PFNGLACTIVETEXTUREPROC ActiveTexture; PFNGLACTIVETEXTUREPROC ActiveTexture;
PFNGLCLIENTACTIVETEXTUREPROC ClientActiveTexture; PFNGLCLIENTACTIVETEXTUREPROC ClientActiveTexture;
bool use_multitexture;
}; };
static inline int GetAlignedSize(unsigned size) static inline int GetAlignedSize(unsigned size)
...@@ -128,6 +181,7 @@ static inline int GetAlignedSize(unsigned size) ...@@ -128,6 +181,7 @@ static inline int GetAlignedSize(unsigned size)
return ((align >> 1) == size) ? size : align; return ((align >> 1) == size) ? size : align;
} }
#if !USE_OPENGL_ES
static bool IsLuminance16Supported(int target) static bool IsLuminance16Supported(int target)
{ {
#if defined(MACOS_OPENGL) #if defined(MACOS_OPENGL)
...@@ -161,6 +215,7 @@ static bool IsLuminance16Supported(int target) ...@@ -161,6 +215,7 @@ static bool IsLuminance16Supported(int target)
return size == 16; return size == 16;
} }
#endif
vout_display_opengl_t *vout_display_opengl_New(video_format_t *fmt, vout_display_opengl_t *vout_display_opengl_New(video_format_t *fmt,
const vlc_fourcc_t **subpicture_chromas, const vlc_fourcc_t **subpicture_chromas,
...@@ -176,46 +231,17 @@ vout_display_opengl_t *vout_display_opengl_New(video_format_t *fmt, ...@@ -176,46 +231,17 @@ vout_display_opengl_t *vout_display_opengl_New(video_format_t *fmt,
return NULL; return NULL;
} }
const char *extensions = (const char *)glGetString(GL_EXTENSIONS); if( vgl->gl->getProcAddress == NULL )
{
/* Load extensions */ fprintf(stderr, "getProcAddress not implemented, bailing out\n");
bool supports_fp = false; free( vgl );
if (HasExtension(extensions, "GL_ARB_fragment_program")) { return NULL;
#if !defined(MACOS_OPENGL)
vgl->GenProgramsARB = (PFNGLGENPROGRAMSARBPROC)vlc_gl_GetProcAddress(vgl->gl, "glGenProgramsARB");
vgl->BindProgramARB = (PFNGLBINDPROGRAMARBPROC)vlc_gl_GetProcAddress(vgl->gl, "glBindProgramARB");
vgl->ProgramStringARB = (PFNGLPROGRAMSTRINGARBPROC)vlc_gl_GetProcAddress(vgl->gl, "glProgramStringARB");
vgl->DeleteProgramsARB = (PFNGLDELETEPROGRAMSARBPROC)vlc_gl_GetProcAddress(vgl->gl, "glDeleteProgramsARB");
vgl->ProgramLocalParameter4fvARB = (PFNGLPROGRAMLOCALPARAMETER4FVARBPROC)vlc_gl_GetProcAddress(vgl->gl, "glProgramLocalParameter4fvARB");
#else
vgl->GenProgramsARB = glGenProgramsARB;
vgl->BindProgramARB = glBindProgramARB;
vgl->ProgramStringARB = glProgramStringARB;
vgl->DeleteProgramsARB = glDeleteProgramsARB;
vgl->ProgramLocalParameter4fvARB = glProgramLocalParameter4fvARB;
#endif
supports_fp = vgl->GenProgramsARB &&
vgl->BindProgramARB &&
vgl->ProgramStringARB &&
vgl->DeleteProgramsARB &&
vgl->ProgramLocalParameter4fvARB;
} }
bool supports_multitexture = false;
bool supports_fp = true;
GLint max_texture_units = 0; GLint max_texture_units = 0;
if (HasExtension(extensions, "GL_ARB_multitexture")) { glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &max_texture_units);
#if !defined(MACOS_OPENGL)
vgl->ActiveTexture = (PFNGLACTIVETEXTUREPROC)vlc_gl_GetProcAddress(vgl->gl, "glActiveTexture");
vgl->ClientActiveTexture = (PFNGLCLIENTACTIVETEXTUREPROC)vlc_gl_GetProcAddress(vgl->gl, "glClientActiveTexture");
#else
vgl->ActiveTexture = glActiveTexture;
vgl->ClientActiveTexture = glClientActiveTexture;
#endif
supports_multitexture = vgl->ActiveTexture &&
vgl->ClientActiveTexture;
if (supports_multitexture)
glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &max_texture_units);
}
/* Initialize with default chroma */ /* Initialize with default chroma */
vgl->fmt = *fmt; vgl->fmt = *fmt;
...@@ -253,7 +279,7 @@ vout_display_opengl_t *vout_display_opengl_New(video_format_t *fmt, ...@@ -253,7 +279,7 @@ vout_display_opengl_t *vout_display_opengl_New(video_format_t *fmt,
/* Use YUV if possible and needed */ /* Use YUV if possible and needed */
bool need_fs_yuv = false; bool need_fs_yuv = false;
float yuv_range_correction = 1.0; float yuv_range_correction = 1.0;
if (supports_fp && supports_multitexture && max_texture_units >= 3 && if ( max_texture_units >= 3 &&
vlc_fourcc_IsYUV(fmt->i_chroma) && !vlc_fourcc_IsYUV(vgl->fmt.i_chroma)) { vlc_fourcc_IsYUV(fmt->i_chroma) && !vlc_fourcc_IsYUV(vgl->fmt.i_chroma)) {
const vlc_fourcc_t *list = vlc_fourcc_GetYUVFallback(fmt->i_chroma); const vlc_fourcc_t *list = vlc_fourcc_GetYUVFallback(fmt->i_chroma);
while (*list) { while (*list) {
...@@ -267,6 +293,7 @@ vout_display_opengl_t *vout_display_opengl_New(video_format_t *fmt, ...@@ -267,6 +293,7 @@ vout_display_opengl_t *vout_display_opengl_New(video_format_t *fmt,
vgl->tex_type = GL_UNSIGNED_BYTE; vgl->tex_type = GL_UNSIGNED_BYTE;
yuv_range_correction = 1.0; yuv_range_correction = 1.0;
break; break;
#if !USE_OPENGL_ES
} else if (dsc && dsc->plane_count == 3 && dsc->pixel_size == 2 && } else if (dsc && dsc->plane_count == 3 && dsc->pixel_size == 2 &&
IsLuminance16Supported(vgl->tex_target)) { IsLuminance16Supported(vgl->tex_target)) {
need_fs_yuv = true; need_fs_yuv = true;
...@@ -277,6 +304,7 @@ vout_display_opengl_t *vout_display_opengl_New(video_format_t *fmt, ...@@ -277,6 +304,7 @@ vout_display_opengl_t *vout_display_opengl_New(video_format_t *fmt,
vgl->tex_type = GL_UNSIGNED_SHORT; vgl->tex_type = GL_UNSIGNED_SHORT;
yuv_range_correction = (float)((1 << 16) - 1) / ((1 << dsc->pixel_bits) - 1); yuv_range_correction = (float)((1 << 16) - 1) / ((1 << dsc->pixel_bits) - 1);
break; break;
#endif
} }
list++; list++;
} }
...@@ -294,108 +322,202 @@ vout_display_opengl_t *vout_display_opengl_New(video_format_t *fmt, ...@@ -294,108 +322,202 @@ vout_display_opengl_t *vout_display_opengl_New(video_format_t *fmt,
} }
#endif #endif
vgl->GenBuffers = (PFNGLGENBUFFERSPROC)vlc_gl_GetProcAddress(vgl->gl, "glGenBuffers");
vgl->BindBuffer = (PFNGLBINDBUFFERPROC)vlc_gl_GetProcAddress(vgl->gl, "glBindBuffer");
vgl->BufferData = (PFNGLBUFFERDATAPROC)vlc_gl_GetProcAddress(vgl->gl, "glBufferData");
vgl->BufferSubData = (PFNGLBUFFERSUBDATAPROC)vlc_gl_GetProcAddress(vgl->gl, "glBufferSubData");
vgl->DeleteBuffers = (PFNGLDELETEBUFFERSPROC)vlc_gl_GetProcAddress(vgl->gl, "glDeleteBuffers");
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");
vgl->AttachShader = (PFNGLATTACHSHADERPROC)vlc_gl_GetProcAddress(vgl->gl, "glAttachShader");
vgl->GetProgramiv = (PFNGLGETPROGRAMIVPROC)vlc_gl_GetProcAddress(vgl->gl, "glGetProgramiv");
vgl->GetProgramInfoLog = (PFNGLGETPROGRAMINFOLOGPROC)vlc_gl_GetProcAddress(vgl->gl, "glGetProgramInfoLog");
vgl->GetShaderiv = (PFNGLGETSHADERIVPROC)vlc_gl_GetProcAddress(vgl->gl, "glGetShaderiv");
vgl->GetShaderInfoLog = (PFNGLGETSHADERINFOLOGPROC)vlc_gl_GetProcAddress(vgl->gl, "glGetShaderInfoLog");
vgl->DetachShader = (PFNGLDETACHSHADERPROC)vlc_gl_GetProcAddress(vgl->gl, "glDetachShader");
vgl->DeleteShader = (PFNGLDELETESHADERPROC)vlc_gl_GetProcAddress(vgl->gl, "glDeleteShader");
vgl->GetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC)vlc_gl_GetProcAddress(vgl->gl, "glGetUniformLocation");
vgl->GetAttribLocation = (PFNGLGETATTRIBLOCATIONPROC)vlc_gl_GetProcAddress(vgl->gl, "glGetAttribLocation");
vgl->Uniform4fv = (PFNGLUNIFORM4FVPROC)vlc_gl_GetProcAddress(vgl->gl,"glUniform4fv");
vgl->Uniform4f = (PFNGLUNIFORM4FPROC)vlc_gl_GetProcAddress(vgl->gl,"glUniform4f");
vgl->Uniform3i = (PFNGLUNIFORM3IPROC)vlc_gl_GetProcAddress(vgl->gl,"glUniform3i");
vgl->Uniform1i = (PFNGLUNIFORM1IPROC)vlc_gl_GetProcAddress(vgl->gl,"glUniform1i");
vgl->CreateProgram = (PFNGLCREATEPROGRAMPROC)vlc_gl_GetProcAddress(vgl->gl, "glCreateProgram");
vgl->LinkProgram = (PFNGLLINKPROGRAMPROC)vlc_gl_GetProcAddress(vgl->gl, "glLinkProgram");
vgl->UseProgram = (PFNGLUSEPROGRAMPROC)vlc_gl_GetProcAddress(vgl->gl, "glUseProgram");
vgl->DeleteProgram = (PFNGLDELETEPROGRAMPROC)vlc_gl_GetProcAddress(vgl->gl, "glDeleteProgram");
vgl->ActiveTexture = (PFNGLACTIVETEXTUREPROC)vlc_gl_GetProcAddress(vgl->gl, "glActiveTexture");
vgl->ClientActiveTexture = (PFNGLCLIENTACTIVETEXTUREPROC)vlc_gl_GetProcAddress(vgl->gl, "glClientActiveTexture");
vgl->chroma = vlc_fourcc_GetChromaDescription(vgl->fmt.i_chroma); vgl->chroma = vlc_fourcc_GetChromaDescription(vgl->fmt.i_chroma);
vgl->use_multitexture = vgl->chroma->plane_count > 1; vgl->use_multitexture = vgl->chroma->plane_count > 1;
bool supports_npot = false; if( !vgl->CreateShader || !vgl->ShaderSource || !vgl->CreateProgram )
#if USE_OPENGL_ES == 2 {
supports_npot = true; fprintf(stderr, "Looks like you don't have all the opengl we need, only %s, giving up\n", glGetString(GL_VERSION));
#else free( vgl );
supports_npot |= HasExtension(extensions, "GL_APPLE_texture_2D_limited_npot") || return NULL;
HasExtension(extensions, "GL_ARB_texture_non_power_of_two"); }
#endif
/* Texture size */ /* Texture size */
for (unsigned j = 0; j < vgl->chroma->plane_count; j++) { for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
int w = vgl->fmt.i_width * vgl->chroma->p[j].w.num / vgl->chroma->p[j].w.den; int w = vgl->fmt.i_width * vgl->chroma->p[j].w.num / vgl->chroma->p[j].w.den;
int h = vgl->fmt.i_height * vgl->chroma->p[j].h.num / vgl->chroma->p[j].h.den; int h = vgl->fmt.i_height * vgl->chroma->p[j].h.num / vgl->chroma->p[j].h.den;
if (supports_npot) {
vgl->tex_width[j] = w; vgl->tex_width[j] = w;
vgl->tex_height[j] = h; vgl->tex_height[j] = h;
} }
else {
/* A texture must have a size aligned on a power of 2 */
vgl->tex_width[j] = GetAlignedSize(w);
vgl->tex_height[j] = GetAlignedSize(h);
}
}
/* Build fragment program if needed */ /* Build fragment program if needed */
vgl->program = 0; vgl->program[0] = 0;
vgl->program[1] = 0;
vgl->local_count = 0; vgl->local_count = 0;
vgl->shader[0] = vgl->shader[1] = vgl->shader[2] = -1;
if (supports_fp) { if (supports_fp) {
char *code = NULL; char *code = NULL;
if (need_fs_yuv) {
/* [R/G/B][Y U V O] from TV range to full range /* [R/G/B][Y U V O] from TV range to full range
* XXX we could also do hue/brightness/constrast/gamma * XXX we could also do hue/brightness/constrast/gamma
* by simply changing the coefficients * by simply changing the coefficients
*/ */
const float matrix_bt601_tv2full[3][4] = { const float matrix_bt601_tv2full[12] = {
{ 1.164383561643836, 0.0000, 1.596026785714286, -0.874202217873451 }, 1.164383561643836, 0.0000, 1.596026785714286, -0.874202217873451 ,
{ 1.164383561643836, -0.391762290094914, -0.812967647237771, 0.531667823499146 }, 1.164383561643836, -0.391762290094914, -0.812967647237771, 0.531667823499146 ,
{ 1.164383561643836, 2.017232142857142, 0.0000, -1.085630789302022 }, 1.164383561643836, 2.017232142857142, 0.0000, -1.085630789302022 ,
}; };
const float matrix_bt709_tv2full[3][4] = { const float matrix_bt709_tv2full[12] = {
{ 1.164383561643836, 0.0000, 1.792741071428571, -0.972945075016308 }, 1.164383561643836, 0.0000, 1.792741071428571, -0.972945075016308 ,
{ 1.164383561643836, -0.21324861427373, -0.532909328559444, 0.301482665475862 }, 1.164383561643836, -0.21324861427373, -0.532909328559444, 0.301482665475862 ,
{ 1.164383561643836, 2.112401785714286, 0.0000, -1.133402217873451 }, 1.164383561643836, 2.112401785714286, 0.0000, -1.133402217873451 ,
}; };
const float (*matrix)[4] = fmt->i_height > 576 ? matrix_bt709_tv2full const float (*matrix) = fmt->i_height > 576 ? matrix_bt709_tv2full
: matrix_bt601_tv2full; : matrix_bt601_tv2full;
/* Basic linear YUV -> RGB conversion using bilinear interpolation */ /* Basic linear YUV -> RGB conversion using bilinear interpolation */
const char *template_yuv = const char *template_glsl_yuv =
"!!ARBfp1.0" "#version 120\n"
"OPTION ARB_precision_hint_fastest;" "uniform sampler2D Texture0;"
"uniform sampler2D Texture1;"
"TEMP src;" "uniform sampler2D Texture2;"
"TEX src.x, fragment.texcoord[0], texture[0], 2D;" "uniform vec4 coefficient[4];"
"TEX src.%c, fragment.texcoord[1], texture[1], 2D;"
"TEX src.%c, fragment.texcoord[2], texture[2], 2D;" "void main(void) {"
" vec4 x,y,z,result;"
"PARAM coefficient[4] = { program.local[0..3] };" " x = texture2D(Texture0, gl_TexCoord[0].st);"
" %c = texture2D(Texture1, gl_TexCoord[1].st);"
"TEMP tmp;" " %c = texture2D(Texture2, gl_TexCoord[2].st);"
"MAD tmp.rgb, src.xxxx, coefficient[0], coefficient[3];"
"MAD tmp.rgb, src.yyyy, coefficient[1], tmp;" " result = x * coefficient[0] + coefficient[3];"
"MAD result.color.rgb, src.zzzz, coefficient[2], tmp;" " result = (y * coefficient[1]) + result;"
"END"; " result = (z * coefficient[2]) + result;"
" gl_FragColor = result;"
"}";
bool swap_uv = vgl->fmt.i_chroma == VLC_CODEC_YV12 || bool swap_uv = vgl->fmt.i_chroma == VLC_CODEC_YV12 ||
vgl->fmt.i_chroma == VLC_CODEC_YV9; vgl->fmt.i_chroma == VLC_CODEC_YV9;
if (asprintf(&code, template_yuv, if (asprintf(&code, template_glsl_yuv,
swap_uv ? 'z' : 'y', swap_uv ? 'z' : 'y',
swap_uv ? 'y' : 'z') < 0) swap_uv ? 'y' : 'z') < 0)
code = NULL; code = NULL;
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
float correction = i < 3 ? yuv_range_correction : 1.0; float correction = i < 3 ? yuv_range_correction : 1.0;
for (int j = 0; j < 4; j++) { /* We place coefficient values for coefficient[4] in one array from matrix values.
vgl->local_value[vgl->local_count + i][j] = j < 3 ? correction * matrix[j][i] : 0.0; Notice that we fill values from top down instead of left to right.*/
} for( int j = 0; j < 4; j++ )
vgl->local_value[vgl->local_count + i*4+j] = j < 3 ? correction * matrix[j*4+i] : 0.0 ;
} }
vgl->local_count += 4; vgl->local_count += 4;
// Basic vertex shader that we use in both cases
const char *vertexShader =
"#version 120\n"
"void main() {"
" gl_TexCoord[0] = gl_MultiTexCoord0;"
" gl_TexCoord[1] = gl_MultiTexCoord1;"
" gl_TexCoord[2] = gl_MultiTexCoord2;"
" gl_Position = ftransform(); }";
// Dummy shader for text overlay
const char *helloShader =
"#version 120\n"
"uniform sampler2D Texture0;"
"uniform vec4 fillColor;"
"void main()"
"{ "
" gl_FragColor = texture2D(Texture0, gl_TexCoord[0].st)*fillColor;}";
vgl->shader[2] = vgl->CreateShader( GL_VERTEX_SHADER );
vgl->ShaderSource( vgl->shader[2], 1, (const GLchar **)&vertexShader, NULL);
vgl->CompileShader( vgl->shader[2] );
/* Create 'dummy' shader that handles subpicture overlay for now*/
vgl->shader[1] = vgl->CreateShader( GL_FRAGMENT_SHADER );
vgl->ShaderSource( vgl->shader[1], 1, &helloShader, NULL);
vgl->CompileShader( vgl->shader[1] );
vgl->program[1] = vgl->CreateProgram();
vgl->AttachShader( vgl->program[1], vgl->shader[1]);
vgl->AttachShader( vgl->program[1], vgl->shader[2]);
vgl->LinkProgram( vgl->program[1] );
// Create shader from code
vgl->shader[0] = vgl->CreateShader( GL_FRAGMENT_SHADER );
vgl->program[0] = vgl->CreateProgram();
if( need_fs_yuv )
{
vgl->ShaderSource( vgl->shader[0], 1, (const GLchar **)&code, NULL );
vgl->CompileShader( vgl->shader[0]);
vgl->AttachShader( vgl->program[0], vgl->shader[0] );
} else {
/* Use simpler shader if we don't need to to yuv -> rgb,
for example when input is allready rgb (.bmp image).*/
vgl->AttachShader( vgl->program[0], vgl->shader[1] );
} }
if (code) { vgl->AttachShader( vgl->program[0], vgl->shader[2]);
// Here you have shaders
vgl->GenProgramsARB(1, &vgl->program); vgl->LinkProgram( vgl->program[0] );
vgl->BindProgramARB(GL_FRAGMENT_PROGRAM_ARB, vgl->program);
vgl->ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB,
GL_PROGRAM_FORMAT_ASCII_ARB,
strlen(code), (const GLbyte*)code);
if (glGetError() == GL_INVALID_OPERATION) {
/* FIXME if the program was needed for YUV, the video will be broken */
#if 0
GLint position;
glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &position);
const char *msg = (const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB);
fprintf(stderr, "GL_INVALID_OPERATION: error at %d: %s\n", position, msg);
#endif
vgl->DeleteProgramsARB(1, &vgl->program);
vgl->program = 0;
}
free(code); free(code);
for( GLuint i = 0; i < 2; i++ )
{
int infoLength = 0;
int charsWritten = 0;
char *infolog;
vgl->GetProgramiv( vgl->program[i], GL_INFO_LOG_LENGTH, &infoLength );
if( infoLength > 1 )
{
/* If there is some message, better to check linking is ok */
GLint link_status = GL_TRUE;
vgl->GetProgramiv( vgl->program[i], GL_LINK_STATUS, &link_status );
infolog = (char *)malloc(infoLength);
vgl->GetProgramInfoLog( vgl->program[i], infoLength, &charsWritten, infolog );
fprintf(stderr, "shader program %d:%s %d\n",i,infolog,infoLength);
free(infolog);
/* Check shaders messages too */
for( GLuint j = 0; j < 2; j++ )
{
vgl->GetShaderiv( vgl->shader[j], GL_INFO_LOG_LENGTH, &infoLength );
if( infoLength > 1 )
{
infolog = (char *)malloc(infoLength);
vgl->GetShaderInfoLog( vgl->shader[j], infoLength, &charsWritten, infolog );
fprintf(stderr, "shader %d: %s\n",j,infolog );
free( infolog );
}
}
if( link_status == GL_FALSE )
{
msg_Err( vgl->gl, "Unable to use program %d", i );
free( vgl );
return NULL;
}
}
} }
} }
...@@ -420,11 +542,7 @@ vout_display_opengl_t *vout_display_opengl_New(video_format_t *fmt, ...@@ -420,11 +542,7 @@ vout_display_opengl_t *vout_display_opengl_New(video_format_t *fmt,
*fmt = vgl->fmt; *fmt = vgl->fmt;
if (subpicture_chromas) { if (subpicture_chromas) {
*subpicture_chromas = NULL;
#if !USE_OPENGL_ES
if (supports_npot)
*subpicture_chromas = gl_subpicture_chromas; *subpicture_chromas = gl_subpicture_chromas;
#endif
} }
return vgl; return vgl;
} }
...@@ -444,8 +562,13 @@ void vout_display_opengl_Delete(vout_display_opengl_t *vgl) ...@@ -444,8 +562,13 @@ void vout_display_opengl_Delete(vout_display_opengl_t *vgl)
} }
free(vgl->region); free(vgl->region);
if (vgl->program) if (vgl->program[0])
vgl->DeleteProgramsARB(1, &vgl->program); {
for( int i = 0; i < 2; i++ )
vgl->DeleteProgram( vgl->program[i] );
for( int i = 0; i < 3; i++ )
vgl->DeleteShader( vgl->shader[i] );
}
vlc_gl_Unlock(vgl->gl); vlc_gl_Unlock(vgl->gl);
} }
...@@ -488,7 +611,10 @@ picture_pool_t *vout_display_opengl_GetPool(vout_display_opengl_t *vgl, unsigned ...@@ -488,7 +611,10 @@ picture_pool_t *vout_display_opengl_GetPool(vout_display_opengl_t *vgl, unsigned
glGenTextures(vgl->chroma->plane_count, vgl->texture[i]); glGenTextures(vgl->chroma->plane_count, vgl->texture[i]);
for (unsigned j = 0; j < vgl->chroma->plane_count; j++) { for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
if (vgl->use_multitexture) if (vgl->use_multitexture)
{
vgl->ActiveTexture(GL_TEXTURE0 + j); vgl->ActiveTexture(GL_TEXTURE0 + j);
vgl->ClientActiveTexture(GL_TEXTURE0 + j);
}
glBindTexture(vgl->tex_target, vgl->texture[i][j]); glBindTexture(vgl->tex_target, vgl->texture[i][j]);
#if !USE_OPENGL_ES #if !USE_OPENGL_ES
...@@ -544,7 +670,10 @@ int vout_display_opengl_Prepare(vout_display_opengl_t *vgl, ...@@ -544,7 +670,10 @@ int vout_display_opengl_Prepare(vout_display_opengl_t *vgl,
/* Update the texture */ /* Update the texture */
for (unsigned j = 0; j < vgl->chroma->plane_count; j++) { for (unsigned j = 0; j < vgl->chroma->plane_count; j++) {
if (vgl->use_multitexture) if (vgl->use_multitexture)
{
vgl->ActiveTexture(GL_TEXTURE0 + j); vgl->ActiveTexture(GL_TEXTURE0 + j);
vgl->ClientActiveTexture(GL_TEXTURE0 + j);
}
glBindTexture(vgl->tex_target, vgl->texture[0][j]); glBindTexture(vgl->tex_target, vgl->texture[0][j]);
glPixelStorei(GL_UNPACK_ROW_LENGTH, picture->p[j].i_pitch / picture->p[j].i_pixel_pitch); glPixelStorei(GL_UNPACK_ROW_LENGTH, picture->p[j].i_pitch / picture->p[j].i_pixel_pitch);
glTexSubImage2D(vgl->tex_target, 0, glTexSubImage2D(vgl->tex_target, 0,
...@@ -570,7 +699,10 @@ int vout_display_opengl_Prepare(vout_display_opengl_t *vgl, ...@@ -570,7 +699,10 @@ int vout_display_opengl_Prepare(vout_display_opengl_t *vgl,
vgl->region = calloc(count, sizeof(*vgl->region)); vgl->region = calloc(count, sizeof(*vgl->region));
if (vgl->use_multitexture) if (vgl->use_multitexture)
{
vgl->ActiveTexture(GL_TEXTURE0 + 0); vgl->ActiveTexture(GL_TEXTURE0 + 0);
vgl->ClientActiveTexture(GL_TEXTURE0 + 0);
}
int i = 0; int i = 0;
for (subpicture_region_t *r = subpicture->p_region; r; r = r->p_next, i++) { for (subpicture_region_t *r = subpicture->p_region; r; r = r->p_next, i++) {
gl_region_t *glr = &vgl->region[i]; gl_region_t *glr = &vgl->region[i];
...@@ -610,8 +742,10 @@ int vout_display_opengl_Prepare(vout_display_opengl_t *vgl, ...@@ -610,8 +742,10 @@ int vout_display_opengl_Prepare(vout_display_opengl_t *vgl,
} else { } else {
glGenTextures(1, &glr->texture); glGenTextures(1, &glr->texture);
glBindTexture(GL_TEXTURE_2D, glr->texture); glBindTexture(GL_TEXTURE_2D, glr->texture);
#if !USE_OPENGL_ES
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_PRIORITY, 1.0); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_PRIORITY, 1.0);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
#endif
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 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_S, GL_CLAMP_TO_EDGE);
...@@ -670,13 +804,11 @@ int vout_display_opengl_Display(vout_display_opengl_t *vgl, ...@@ -670,13 +804,11 @@ int vout_display_opengl_Display(vout_display_opengl_t *vgl,
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
if (vgl->program) { vgl->UseProgram(vgl->program[0]);
glEnable(GL_FRAGMENT_PROGRAM_ARB); vgl->Uniform4fv( vgl->GetUniformLocation( vgl->program[0], "coefficient" ), 4, vgl->local_value);
for (int i = 0; i < vgl->local_count; i++) vgl->Uniform1i( vgl->GetUniformLocation( vgl->program[0], "Texture0" ), 0);
vgl->ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, i, vgl->local_value[i]); vgl->Uniform1i( vgl->GetUniformLocation( vgl->program[0], "Texture1" ), 1);
} else { vgl->Uniform1i( vgl->GetUniformLocation( vgl->program[0], "Texture2" ), 2);
glEnable(vgl->tex_target);
}
#if USE_OPENGL_ES #if USE_OPENGL_ES
static const GLfloat vertexCoord[] = { static const GLfloat vertexCoord[] = {
...@@ -722,10 +854,13 @@ int vout_display_opengl_Display(vout_display_opengl_t *vgl, ...@@ -722,10 +854,13 @@ int vout_display_opengl_Display(vout_display_opengl_t *vgl,
}; };
vgl->ActiveTexture( GL_TEXTURE0+j); vgl->ActiveTexture( GL_TEXTURE0+j);
vgl->ClientActiveTexture( GL_TEXTURE0+j); vgl->ClientActiveTexture( GL_TEXTURE0+j);
glEnable(vgl->tex_target);
glEnableClientState(GL_TEXTURE_COORD_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glBindTexture(vgl->tex_target, vgl->texture[0][j]); glBindTexture(vgl->tex_target, vgl->texture[0][j]);
glTexCoordPointer(2, GL_FLOAT, 0, texCoord); glTexCoordPointer(2, GL_FLOAT, 0, texCoord);
} }
vgl->ActiveTexture(GL_TEXTURE0 + 0);
vgl->ClientActiveTexture(GL_TEXTURE0 + 0);
glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, vertexCoord); glVertexPointer(2, GL_FLOAT, 0, vertexCoord);
glDrawArrays( GL_TRIANGLE_STRIP, 0, 4); glDrawArrays( GL_TRIANGLE_STRIP, 0, 4);
...@@ -740,13 +875,8 @@ int vout_display_opengl_Display(vout_display_opengl_t *vgl, ...@@ -740,13 +875,8 @@ int vout_display_opengl_Display(vout_display_opengl_t *vgl,
if (vgl->program)
glDisable(GL_FRAGMENT_PROGRAM_ARB);
else
glDisable(vgl->tex_target);
if (vgl->use_multitexture)
vgl->ActiveTexture(GL_TEXTURE0 + 0); vgl->ActiveTexture(GL_TEXTURE0 + 0);
vgl->ClientActiveTexture(GL_TEXTURE0 + 0);
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND); glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
...@@ -757,6 +887,11 @@ int vout_display_opengl_Display(vout_display_opengl_t *vgl, ...@@ -757,6 +887,11 @@ int vout_display_opengl_Display(vout_display_opengl_t *vgl,
1.0, 0.0, 1.0, 0.0,
1.0, 1.0, 1.0, 1.0,
}; };
vgl->UseProgram(vgl->program[1]);
vgl->Uniform1i( vgl->GetUniformLocation( vgl->program[1], "Texture0" ), 0);
vgl->ActiveTexture(GL_TEXTURE0 + 0);
vgl->ClientActiveTexture(GL_TEXTURE0 + 0);
for (int i = 0; i < vgl->region_count; i++) { for (int i = 0; i < vgl->region_count; i++) {
gl_region_t *glr = &vgl->region[i]; gl_region_t *glr = &vgl->region[i];
const GLfloat vertexCoord[] = { const GLfloat vertexCoord[] = {
...@@ -765,7 +900,7 @@ int vout_display_opengl_Display(vout_display_opengl_t *vgl, ...@@ -765,7 +900,7 @@ int vout_display_opengl_Display(vout_display_opengl_t *vgl,
glr->right, glr->top, glr->right, glr->top,
glr->right,glr->bottom, glr->right,glr->bottom,
}; };
glColor4f(1.0f, 1.0f, 1.0f, glr->alpha); vgl->Uniform4f( vgl->GetUniformLocation( vgl->program[1], "fillColor"), 1.0f, 1.0f, 1.0f, glr->alpha);
glEnableClientState(GL_TEXTURE_COORD_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glBindTexture(GL_TEXTURE_2D, glr->texture); glBindTexture(GL_TEXTURE_2D, glr->texture);
......
...@@ -34,6 +34,13 @@ ...@@ -34,6 +34,13 @@
# define USE_OPENGL_ES 0 # define USE_OPENGL_ES 0
#endif #endif
#ifdef __APPLE__
# include "TargetConditionals.h"
# if defined(TARGET_OS_IPHONE) || defined(TARGET_IPHONE_SIMULATOR)
# define USE_OPENGL_ES 1
# endif
#endif
#if !defined (__APPLE__) #if !defined (__APPLE__)
# if USE_OPENGL_ES == 2 # if USE_OPENGL_ES == 2
# include <GLES2/gl2.h> # include <GLES2/gl2.h>
...@@ -42,14 +49,16 @@ ...@@ -42,14 +49,16 @@
# else # else
# ifdef WIN32 # ifdef WIN32
# include <GL/glew.h> # include <GL/glew.h>
# else
# include <GL/glx.h>
# endif # endif
# include <GL/gl.h> # include <GL/gl.h>
# endif # endif
#else #else
# if USE_OPENGL_ES == 2 # if USE_OPENGL_ES
# include <OpenGLES/ES2/gl.h>
# elif USE_OPENGL_ES == 1
# include <OpenGLES/ES1/gl.h> # include <OpenGLES/ES1/gl.h>
# include <OpenGLES/ES2/gl.h>
# include <OpenGLES/ES2/glext.h>
# else # else
# define MACOS_OPENGL # define MACOS_OPENGL
# include <OpenGL/gl.h> # include <OpenGL/gl.h>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment