Skip to content

Commit

Permalink
gl: pass bpp as a uniform to shader
Browse files Browse the repository at this point in the history
  • Loading branch information
wang-bin committed Jun 29, 2014
1 parent d5d5f7a commit c535935
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/GLWidgetRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ bool GLWidgetRendererPrivate::prepareShaderProgram(const VideoFormat &fmt, Color
a_Position = glGetAttribLocation(program, "a_Position");
a_TexCoords = glGetAttribLocation(program, "a_TexCoords");
u_matrix = glGetUniformLocation(program, "u_MVP_matrix");
u_bpp = glGetUniformLocation(program, "u_bpp");
// fragment shader
u_colorMatrix = glGetUniformLocation(program, "u_colorMatrix");
#else
Expand All @@ -417,12 +418,14 @@ bool GLWidgetRendererPrivate::prepareShaderProgram(const VideoFormat &fmt, Color
a_Position = shader_program->attributeLocation("a_Position");
a_TexCoords = shader_program->attributeLocation("a_TexCoords");
u_matrix = shader_program->uniformLocation("u_MVP_matrix");
u_bpp = shader_program->uniformLocation("u_bpp");
// fragment shader
u_colorMatrix = shader_program->uniformLocation("u_colorMatrix");
#endif //NO_QGL_SHADER
qDebug("glGetAttribLocation(\"a_Position\") = %d\n", a_Position);
qDebug("glGetAttribLocation(\"a_TexCoords\") = %d\n", a_TexCoords);
qDebug("glGetUniformLocation(\"u_MVP_matrix\") = %d\n", u_matrix);
qDebug("glGetUniformLocation(\"u_bpp\") = %d\n", u_bpp);
qDebug("glGetUniformLocation(\"u_colorMatrix\") = %d\n", u_colorMatrix);

if (fmt.isRGB())
Expand Down Expand Up @@ -900,8 +903,10 @@ void GLWidgetRenderer::drawFrame()

#if NO_QGL_SHADER
glUniformMatrix4fv(d.u_colorMatrix, 1, GL_FALSE, mat);
glUniform1f(d.u_bpp, (GLfloat)d.video_format.bitsPerPixel(0));
#else
d.shader_program->setUniformValue(d.u_colorMatrix, d.colorTransform.matrixRef());
d.shader_program->setUniformValue(d.u_bpp, (GLfloat)d.video_format.bitsPerPixel(0));
#endif
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);

Expand Down
2 changes: 2 additions & 0 deletions src/QtAV/private/GLWidgetRenderer_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class Q_AV_EXPORT GLWidgetRendererPrivate : public VideoRendererPrivate
, a_Position(-1)
, a_TexCoords(-1)
, u_matrix(-1)
, u_bpp(-1)
, painter(0)
, video_format(VideoFormat::Format_Invalid)
{
Expand Down Expand Up @@ -195,6 +196,7 @@ class Q_AV_EXPORT GLWidgetRendererPrivate : public VideoRendererPrivate
QVector<GLint> u_Texture; //u_TextureN uniform. size is channel count
GLint u_matrix;
GLint u_colorMatrix;
GLuint u_bpp;

QPainter *painter;

Expand Down
6 changes: 5 additions & 1 deletion src/shaders/yuv_rgb.f.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ uniform sampler2D u_Texture0;
uniform sampler2D u_Texture1;
uniform sampler2D u_Texture2;
varying lowp vec2 v_TexCoords;
uniform float u_bpp;
uniform mat4 u_colorMatrix;

//http://en.wikipedia.org/wiki/YUV calculation used
Expand All @@ -60,7 +61,7 @@ const mat4 yuv2rgbMatrix = mat4(1, 1, 1, 0,
// 10, 16bit: http://msdn.microsoft.com/en-us/library/windows/desktop/bb970578%28v=vs.85%29.aspx
void main()
{
#if defined(YUV16BITS_LE_LUMINANCE_ALPHA) || defined(YUV16BITS_BE_LUMINANCE_ALPHA)
#if 0 //defined(YUV16BITS_LE_LUMINANCE_ALPHA) || defined(YUV16BITS_BE_LUMINANCE_ALPHA)
// FFmpeg supports 9, 10, 12, 14, 16 bits
#if defined(YUV9P)
const float range = 511.0;
Expand All @@ -73,6 +74,9 @@ void main()
#elif defined(YUV16P)
const float range = 65535.0;
#endif
#else
//http://stackoverflow.com/questions/22693169/opengl-es-2-0-glsl-compiling-fails-on-osx-when-using-const
float range = exp2(u_bpp) - 1.0; // why can not be const?
#endif //defined(YUV16BITS_LE_LUMINANCE_ALPHA)
// 10p in little endian: yyyyyyyy yy000000 => (L, L, L, A)
gl_FragColor = clamp(u_colorMatrix*yuv2rgbMatrix* vec4(
Expand Down

0 comments on commit c535935

Please sign in to comment.