Skip to content

Commit

Permalink
gl: set uniform values only when they are changed
Browse files Browse the repository at this point in the history
  • Loading branch information
wang-bin committed Mar 15, 2016
1 parent 7fd58d9 commit 7efee68
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
3 changes: 1 addition & 2 deletions src/OpenGLVideo.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/******************************************************************************
QtAV: Media play library based on Qt and FFmpeg
QtAV: Multimedia framework based on Qt and FFmpeg
Copyright (C) 2012-2016 Wang Bin <[email protected]>
* This file is part of QtAV (from 2014)
Expand Down Expand Up @@ -370,7 +370,6 @@ void OpenGLVideo::render(const QRectF &target, const QRectF& roi, const QMatrix4
if (!shader)
shader = d.manager->prepareMaterial(d.material, mt); //TODO: print shader type name if changed. prepareMaterial(,sample_code, pp_code)
shader->update(d.material);
shader->program()->setUniformValue(shader->opacityLocation(), (GLfloat)1.0);
shader->program()->setUniformValue(shader->matrixLocation(), transform*d.matrix);
// uniform end. attribute begin
d.bindAttributes(shader, target, roi);
Expand Down
2 changes: 1 addition & 1 deletion src/QtAV/OpenGLVideo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/******************************************************************************
QtAV: Media play library based on Qt and FFmpeg
QtAV: Multimedia framework based on Qt and FFmpeg
Copyright (C) 2012-2016 Wang Bin <[email protected]>
* This file is part of QtAV (from 2014)
Expand Down
2 changes: 2 additions & 0 deletions src/QtAV/private/VideoShader_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class Q_AV_PRIVATE_EXPORT VideoShaderPrivate : public DPtrPrivate<VideoShader>
VideoShaderPrivate()
: owns_program(false)
, rebuild_program(false)
, update_builtin_uniforms(true)
, program(0)
, u_Matrix(-1)
, u_colorMatrix(-1)
Expand All @@ -73,6 +74,7 @@ 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
bool rebuild_program;
bool update_builtin_uniforms; //builtin uniforms are static, set the values once is enough if no change
QOpenGLShaderProgram *program;
int u_Matrix;
int u_colorMatrix;
Expand Down
10 changes: 7 additions & 3 deletions src/VideoShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ void VideoShader::initialize(QOpenGLShaderProgram *shaderProgram)
}
}
d.rebuild_program = false;
d.update_builtin_uniforms = true;
}

int VideoShader::textureLocationCount() const
Expand Down Expand Up @@ -461,19 +462,23 @@ bool VideoShader::update(VideoMaterial *material)
setVideoFormat(fmt);
// uniforms begin
program()->bind(); //glUseProgram(id). for glUniform
setUserUniformValues();
if (!d.update_builtin_uniforms)
return true;
d.update_builtin_uniforms = false;
// all texture ids should be binded when renderering even for packed plane!
const int nb_planes = fmt.planeCount(); //number of texture id
// TODO: sample2D array
for (int i = 0; i < nb_planes; ++i) {
// use glUniform1i to swap planes. swap uv: i => (3-i)%3
// TODO: in shader, use uniform sample2D u_Texture[], and use glUniform1iv(u_Texture, 3, {...})
program()->setUniformValue(textureLocation(i), (GLint)i);
}
if (nb_planes < textureLocationCount()) {
for (int i = nb_planes; i < textureLocationCount(); ++i) {
program()->setUniformValue(textureLocation(i), (GLint)(nb_planes - 1));
}
}
//qDebug() << "color mat " << material->colorMatrix();
program()->setUniformValue(opacityLocation(), (GLfloat)1.0);
program()->setUniformValue(colorMatrixLocation(), material->colorMatrix());
if (d_func().u_to8 >= 0)
program()->setUniformValue(d_func().u_to8, material->vectorTo8bit());
Expand All @@ -482,7 +487,6 @@ bool VideoShader::update(VideoMaterial *material)
//program()->setUniformValue(matrixLocation(), material->matrix()); //what about sgnode? state.combindMatrix()?
if (texelSizeLocation() >= 0)
program()->setUniformValueArray(texelSizeLocation(), material->texelSize().constData(), nb_planes);
setUserUniformValues();
// uniform end. attribute begins
return true;
}
Expand Down

0 comments on commit 7efee68

Please sign in to comment.