Skip to content

Commit

Permalink
gl: compute less in glsl. less material types
Browse files Browse the repository at this point in the history
TODO: to support both RG and LA format, vec3 can be used
  • Loading branch information
wang-bin committed Sep 4, 2015
1 parent 514553a commit e377e4d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 52 deletions.
2 changes: 1 addition & 1 deletion src/QtAV/VideoShader.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ class Q_AV_EXPORT VideoShader
int textureLocation(int index) const;
int matrixLocation() const;
int colorMatrixLocation() const;
int bppLocation() const;
int opacityLocation() const;
int channelMapLocation() const;
VideoFormat videoFormat() const;
Expand Down Expand Up @@ -120,6 +119,7 @@ class Q_AV_EXPORT VideoMaterial
const QMatrix4x4& matrix() const;
const QMatrix4x4& channelMap() const;
int bpp() const; //1st plane
QVector2D vectorTo8bit() const;
int planeCount() const;
/*!
* \brief validTextureWidth
Expand Down
6 changes: 3 additions & 3 deletions src/QtAV/private/VideoShader_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Q_AV_PRIVATE_EXPORT VideoShaderPrivate : public DPtrPrivate<VideoShader>
, program(0)
, u_MVP_matrix(-1)
, u_colorMatrix(-1)
, u_bpp(-1)
, u_to8(-1)
, u_opacity(-1)
, u_c(-1)
, texture_target(GL_TEXTURE_2D)
Expand All @@ -71,10 +71,9 @@ class Q_AV_PRIVATE_EXPORT VideoShaderPrivate : public DPtrPrivate<VideoShader>

bool owns_program; // shader program is not created by this. e.g. scene graph create it's own program and we store it here
QOpenGLShaderProgram *program;
// TODO: compare with texture width uniform used in qtmm
int u_MVP_matrix;
int u_colorMatrix;
int u_bpp;
int u_to8;
int u_opacity;
int u_c;
QVector<int> u_Texture;
Expand Down Expand Up @@ -167,6 +166,7 @@ class VideoMaterialPrivate : public DPtrPrivate<VideoMaterial>
QMatrix4x4 matrix;
bool try_pbo;
QVector<QOpenGLBuffer> pbo;
QVector2D vec_to8; //TODO: vec3 to support both RG and LA (.rga, vec_to8)
QMatrix4x4 channel_map;
};

Expand Down
51 changes: 29 additions & 22 deletions src/VideoShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,8 @@ const char* VideoShader::fragmentShader() const
}
const bool has_alpha = d.video_format.hasAlpha();
if (d.video_format.isPlanar()) {
if (d.video_format.bytesPerPixel(0) == 2) {
if (d.video_format.isBigEndian())
frag.prepend("#define LA_16BITS_BE\n");
else
frag.prepend("#define LA_16BITS_LE\n");
if (d.video_format.bytesPerPixel(0) == 1) {
frag.prepend("#define CHANNEL_8BIT\n");
}
#if YUVA_DONE
if (has_alpha)
Expand Down Expand Up @@ -261,7 +258,7 @@ void VideoShader::initialize(QOpenGLShaderProgram *shaderProgram)
d.u_MVP_matrix = shaderProgram->uniformLocation("u_MVP_matrix");
// fragment shader
d.u_colorMatrix = shaderProgram->uniformLocation("u_colorMatrix");
d.u_bpp = shaderProgram->uniformLocation("u_bpp");
d.u_to8 = shaderProgram->uniformLocation("u_to8");
d.u_opacity = shaderProgram->uniformLocation("u_opacity");
d.u_c = shaderProgram->uniformLocation("u_c");
d.u_Texture.resize(textureLocationCount());
Expand All @@ -275,8 +272,8 @@ void VideoShader::initialize(QOpenGLShaderProgram *shaderProgram)
qDebug("glGetUniformLocation(\"u_opacity\") = %d", d.u_opacity);
if (d.u_c >= 0)
qDebug("glGetUniformLocation(\"u_c\") = %d", d.u_c);
if (d.u_bpp >= 0)
qDebug("glGetUniformLocation(\"u_bpp\") = %d", d.u_bpp);
if (d.u_to8 >= 0)
qDebug("glGetUniformLocation(\"u_to8\") = %d", d.u_to8);
}

int VideoShader::textureLocationCount() const
Expand Down Expand Up @@ -305,11 +302,6 @@ int VideoShader::colorMatrixLocation() const
return d_func().u_colorMatrix;
}

int VideoShader::bppLocation() const
{
return d_func().u_bpp;
}

int VideoShader::opacityLocation() const
{
return d_func().u_opacity;
Expand Down Expand Up @@ -376,8 +368,8 @@ bool VideoShader::update(VideoMaterial *material)
}
//qDebug() << "color mat " << material->colorMatrix();
program()->setUniformValue(colorMatrixLocation(), material->colorMatrix());
if (bppLocation() >= 0)
program()->setUniformValue(bppLocation(), (GLfloat)material->bpp());
if (d_func().u_to8 >= 0)
program()->setUniformValue(d_func().u_to8, material->vectorTo8bit());
if (channelMapLocation() >= 0)
program()->setUniformValue(channelMapLocation(), material->channelMap());
//program()->setUniformValue(matrixLocation(), material->matrix()); //what about sgnode? state.combindMatrix()?
Expand Down Expand Up @@ -451,7 +443,17 @@ void VideoMaterial::setCurrentFrame(const VideoFrame &frame)
}

const VideoFormat fmt(frame.format());
const int bpp_old = d.bpp;
d.bpp = fmt.bitsPerPixel(0);
if (d.bpp > 8 && d.bpp != bpp_old) {
const int range = (1 << d.bpp) - 1;
// FFmpeg supports 9, 10, 12, 14, 16 bits
// 10p in little endian: yyyyyyyy yy000000 => (L, L, L, A) //(yyyyyyyy, 000000yy)?
if (fmt.isBigEndian())
d.vec_to8 = QVector2D(256.0, 1.0)*255.0/(float)range;
else
d.vec_to8 = QVector2D(1.0, 256.0)*255.0/(float)range;
}
// http://forum.doom9.org/archive/index.php/t-160211.html
ColorSpace cs = frame.colorSpace();// ColorSpace_RGB;
if (cs == ColorSpace_Unknow) {
Expand Down Expand Up @@ -494,21 +496,21 @@ VideoShader* VideoMaterial::createShader() const

QString VideoMaterial::typeName(qint64 value)
{
return QString("gl material planar: %1, has alpha: %2, big endian: %3, 2d texture: %4, 8bit channel: %5")
.arg(!!(value&(1<<4)))
.arg(!!(value&(1<<3)))
.arg(!!(value&(1<<2)))
return QString("gl material 8bit channel: %1, planar: %2, has alpha: %3, 2d texture: %4")
.arg(!!(value&1))
.arg(!!(value&(1<<1)))
.arg(!!(value&1));
.arg(!!(value&(1<<2)))
.arg(!!(value&(1<<3)))
;
}

qint64 VideoMaterial::type() const
{
DPTR_D(const VideoMaterial);
const VideoFormat &fmt = d.video_format;
const bool tex_2d = d.target == GL_TEXTURE_2D;
// planar,alpha,be,2d,8bit
return (fmt.isPlanar()<<4)|(fmt.hasAlpha()<<3)|(fmt.isBigEndian()<<2)|(tex_2d<<1)|(fmt.bytesPerPixel(0) == 1);
// 2d,alpha,planar,8bit
return (tex_2d<<3)|(fmt.hasAlpha()<<2)|(fmt.isPlanar()<<1)|(fmt.bytesPerPixel(0) == 1);
}

bool VideoMaterial::bind()
Expand Down Expand Up @@ -633,6 +635,11 @@ int VideoMaterial::bpp() const
return d_func().bpp;
}

QVector2D VideoMaterial::vectorTo8bit() const
{
return d_func().vec_to8;
}

int VideoMaterial::planeCount() const
{
return d_func().frame.planeCount();
Expand Down
36 changes: 10 additions & 26 deletions src/shaders/planar.f.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,9 @@ varying lowp vec2 v_TexCoords3;
uniform float u_opacity;
uniform float u_bpp;
uniform mat4 u_colorMatrix;

#if defined(LA_16BITS_BE) || defined(LA_16BITS_LE)
#define LA_16BITS 1
#else
#define LA_16BITS 0
#ifndef CHANNEL_8BIT
uniform vec2 u_to8;
#endif
//#define LA_16BITS (defined(LA_16BITS_BE) || defined(LA_16BITS_LE)) // why may error?

#if defined(YUV_MAT_GLSL)
//http://en.wikipedia.org/wiki/YUV calculation used
//http://www.fourcc.org/fccyvrgb.php
Expand All @@ -84,36 +79,25 @@ 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()
{
// FFmpeg supports 9, 10, 12, 14, 16 bits
#if LA_16BITS
//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?
#if defined(LA_16BITS_LE)
vec2 t = vec2(1.0, 256.0)*255.0/range;
#else
vec2 t = vec2(256.0, 1.0)*255.0/range;
#endif
#endif //LA_16BITS
// 10p in little endian: yyyyyyyy yy000000 => (L, L, L, A)
gl_FragColor = clamp(u_colorMatrix
* vec4(
#if LA_16BITS
dot(texture2D(u_Texture0, v_TexCoords0).ra, t),
dot(texture2D(u_Texture1, v_TexCoords1).ra, t),
dot(texture2D(u_Texture2, v_TexCoords2).ra, t),
#ifndef CHANNEL_8BIT
dot(texture2D(u_Texture0, v_TexCoords0).ra, u_to8),
dot(texture2D(u_Texture1, v_TexCoords1).ra, u_to8),
dot(texture2D(u_Texture2, v_TexCoords2).ra, u_to8),
#else
// use r, g, a to work for both yv12 and nv12. idea from xbmc
texture2D(u_Texture0, v_TexCoords0).r,
texture2D(u_Texture1, v_TexCoords1).g,
texture2D(u_Texture2, v_TexCoords2).a,
#endif //LA_16BITS
#endif //CHANNEL_8BIT
1)
, 0.0, 1.0) * u_opacity;
#ifdef HAS_ALPHA
#if LA_16BITS
gl_FragColor.a *= dot(texture2D(u_Texture3, v_TexCoords3).ra, t); //GL_LUMINANCE_ALPHA
#ifndef CHANNEL_8BIT
gl_FragColor.a *= dot(texture2D(u_Texture3, v_TexCoords3).ra, u_to8); //GL_LUMINANCE_ALPHA
#else //8bit
gl_FragColor.a *= texture2D(u_Texture3, v_TexCoords3).a; //GL_ALPHA
#endif //LA_16BITS
#endif //CHANNEL_8BIT
#endif //HAS_ALPHA
}

0 comments on commit e377e4d

Please sign in to comment.