Skip to content

Commit

Permalink
gl: set uniform initial value from property
Browse files Browse the repository at this point in the history
  • Loading branch information
wang-bin committed Apr 23, 2016
1 parent 8e39194 commit d3d416c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 36 deletions.
9 changes: 3 additions & 6 deletions examples/glslfilter/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class WaveShader : public DynamicShaderObject {
WaveShader(QObject *parent = 0) : DynamicShaderObject(parent)
, t(0)
{
setProperty("u_t", 0);
setProperty("u_A", 0.06);
setProperty("u_omega", 5);
setHeader(QLatin1String(GLSL(
uniform float u_omega;
uniform float u_A;
Expand All @@ -54,12 +57,6 @@ class WaveShader : public DynamicShaderObject {
t+=2.0*M_PI/25.0;
setProperty("u_t", t);
}
private:
virtual void ready() {
setProperty("u_t", 0);
setProperty("u_A", 0.06);
setProperty("u_omega", 5);
}
};

int main(int argc, char** argv)
Expand Down
5 changes: 5 additions & 0 deletions src/QtAV/OpenGLTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ class Q_AV_EXPORT Uniform {
void set(const float* v, int count = 0);
void set(const unsigned* v, int count = 0);
void set(const int* v, int count = 0);
/*!
* \brief set
* \param v can be T and QVector<T>, T is the type supported by OpenGL
*/
void set(const QVariant& v);
/*!
* \brief setGL
* Call glUniformXXX to update uniform values that set by set(const T&, int) and mark dirty false. Currently only use OpenGL ES2 supported functions, i.e. uint, double types are not supported.
Expand Down
7 changes: 0 additions & 7 deletions src/QtAV/VideoShaderObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ class VideoShaderObjectPrivate;
* \brief The VideoShaderObject class
* User defined uniform names are bound to class meta properties (property signals are required)
* and object dynamic properties.
* Property value won't be updated if event loop is not ready or shader program is not compiled when calling setProperty().
* For example, calling setProperty() has no effect in constructor. The initial values can be set in ready().
* Property value type T is limited to float, int, unsigned(ES3.0) and QVector<T>
*/
class Q_AV_EXPORT VideoShaderObject : public QObject, public VideoShader
Expand All @@ -48,11 +46,6 @@ private Q_SLOTS:
void propertyChanged(int id);
private:
void programReady() Q_DECL_OVERRIDE Q_DECL_FINAL;
/*!
* \brief ready
* Called when shader program is ready. You can call setProperty("uniformName", uniformValue) here to set default uniform values.
*/
virtual void ready() {}
};

class DynamicShaderObjectPrivate;
Expand Down
25 changes: 25 additions & 0 deletions src/opengl/OpenGLTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,31 @@ void Uniform::set(const unsigned *v, int count)
dirty = set_uniform_value(data, v, count);
}

void Uniform::set(const QVariant &v)
{
if (tupleSize() > 1 || arraySize() > 1) {
if (isFloat()) {
set(v.value<QVector<float> >().data());
} else if (isInt() || isBool()) {
set(v.value<QVector<int> >().data());
} else if (isUInt()) {
set(v.value<QVector<unsigned> >().data());
} else if (type() == Uniform::Sampler) {

}
} else {
if (isFloat()) {
set(v.toFloat());
} else if (isInt() || isBool()) {
set(v.toInt());
} else if (isUInt()) {
set(v.toUInt());
} else if (type() == Uniform::Sampler) {

}
}
}

bool Uniform::setGL()
{
if (location < 0) {
Expand Down
29 changes: 6 additions & 23 deletions src/opengl/VideoShaderObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,27 +73,7 @@ void VideoShaderObject::propertyChanged(int id)
const int idx = id&0xffff;
Uniform &u = d.user_uniforms[st][idx];
const QVariant v = property(u.name.constData());
if (u.tupleSize() > 1 || u.arraySize() > 1) {
if (u.isFloat()) {
u.set(v.value<QVector<float> >().data());
} else if (u.isInt() || u.isBool()) {
u.set(v.value<QVector<int> >().data());
} else if (u.isUInt()) {
u.set(v.value<QVector<unsigned> >().data());
} else if (u.type() == Uniform::Sampler) {

}
} else {
if (u.isFloat()) {
u.set(v.toFloat());
} else if (u.isInt() || u.isBool()) {
u.set(v.toInt());
} else if (u.isUInt()) {
u.set(v.toUInt());
} else if (u.type() == Uniform::Sampler) {

}
}
u.set(v);
//if (u.dirty) update();
}

Expand All @@ -109,7 +89,8 @@ void VideoShaderObject::programReady()
const Uniform& u = uniforms[i];
const int idx = metaObject()->indexOfProperty(u.name.constData());
if (idx < 0) {
qDebug("VideoShaderObject has no property '%s'", u.name.constData());
qDebug("VideoShaderObject has no meta property '%s'. Setting initial value from dynamic property", u.name.constData());
const_cast<Uniform&>(u).set(property(u.name.constData()));
continue;
}
QMetaProperty mp = metaObject()->property(idx);
Expand All @@ -127,9 +108,11 @@ void VideoShaderObject::programReady()
#endif
connect(mapper, SIGNAL(mapped(int)), this, SLOT(propertyChanged(int)));
d.sigMap[st].append(mapper);
qDebug() << "set uniform property: " << u.name << property(u.name.constData());
propertyChanged(i|(st<<16)); // set the initial value
}
}
ready();
//ready();
}

class DynamicShaderObjectPrivate : public VideoShaderObjectPrivate {
Expand Down

0 comments on commit d3d416c

Please sign in to comment.