From cd3ae498bfac3cf98f6386f17596d1ec5ed08110 Mon Sep 17 00:00:00 2001 From: Alan Alpert Date: Fri, 19 Aug 2011 14:56:19 +1000 Subject: Use point sprites Only for the simpler particles, at least for now. Change-Id: If777cf301894553849b78060aa23980aee7fb807 Reviewed-on: http://codereview.qt.nokia.com/3240 Reviewed-by: Qt Sanity Bot Reviewed-by: Gunnar Sletta --- .../defaultshaders/coloredfragment.shader | 3 +- .../particles/defaultshaders/coloredvertex.shader | 6 +- .../particles/defaultshaders/simplefragment.shader | 3 +- .../particles/defaultshaders/simplevertex.shader | 6 +- src/declarative/particles/qsgimageparticle.cpp | 107 ++++++++++++++------- src/declarative/particles/qsgimageparticle_p.h | 4 - 6 files changed, 77 insertions(+), 52 deletions(-) diff --git a/src/declarative/particles/defaultshaders/coloredfragment.shader b/src/declarative/particles/defaultshaders/coloredfragment.shader index 383888b5c7..4ae2643598 100644 --- a/src/declarative/particles/defaultshaders/coloredfragment.shader +++ b/src/declarative/particles/defaultshaders/coloredfragment.shader @@ -1,10 +1,9 @@ uniform sampler2D texture; uniform lowp float qt_Opacity; -varying highp vec2 fTex; varying lowp vec4 fColor; void main() { - gl_FragColor = (texture2D(texture, fTex)) * fColor * qt_Opacity; + gl_FragColor = (texture2D(texture, gl_PointCoord)) * fColor * qt_Opacity; } diff --git a/src/declarative/particles/defaultshaders/coloredvertex.shader b/src/declarative/particles/defaultshaders/coloredvertex.shader index d6498f277f..cdbb130dd9 100644 --- a/src/declarative/particles/defaultshaders/coloredvertex.shader +++ b/src/declarative/particles/defaultshaders/coloredvertex.shader @@ -1,5 +1,4 @@ attribute highp vec2 vPos; -attribute highp vec2 vTex; attribute highp vec4 vData; // x = time, y = lifeSpan, z = size, w = endSize attribute highp vec4 vVec; // x,y = constant speed, z,w = acceleration attribute lowp vec4 vColor; @@ -7,11 +6,9 @@ attribute lowp vec4 vColor; uniform highp mat4 qt_Matrix; uniform highp float timestamp; -varying highp vec2 fTex; varying lowp vec4 fColor; void main() { - fTex = vTex; highp float size = vData.z; highp float endSize = vData.w; @@ -22,8 +19,9 @@ void main() { if (t < 0. || t > 1.) currentSize = 0.; + gl_PointSize = currentSize; + highp vec2 pos = vPos - - currentSize / 2. + currentSize * vTex // adjust size + vVec.xy * t * vData.y // apply speed vector.. + 0.5 * vVec.zw * pow(t * vData.y, 2.); diff --git a/src/declarative/particles/defaultshaders/simplefragment.shader b/src/declarative/particles/defaultshaders/simplefragment.shader index 0b0adaa32b..9b445da52f 100644 --- a/src/declarative/particles/defaultshaders/simplefragment.shader +++ b/src/declarative/particles/defaultshaders/simplefragment.shader @@ -1,9 +1,8 @@ uniform sampler2D texture; uniform lowp float qt_Opacity; -varying highp vec2 fTex; varying lowp float fFade; void main() { - gl_FragColor = texture2D(texture, fTex) * (fFade * qt_Opacity); + gl_FragColor = texture2D(texture, gl_PointCoord) * (fFade * qt_Opacity); } diff --git a/src/declarative/particles/defaultshaders/simplevertex.shader b/src/declarative/particles/defaultshaders/simplevertex.shader index b7542e660e..1c07363505 100644 --- a/src/declarative/particles/defaultshaders/simplevertex.shader +++ b/src/declarative/particles/defaultshaders/simplevertex.shader @@ -1,16 +1,13 @@ attribute highp vec2 vPos; -attribute highp vec2 vTex; attribute highp vec4 vData; // x = time, y = lifeSpan, z = size, w = endSize attribute highp vec4 vVec; // x,y = constant speed, z,w = acceleration uniform highp mat4 qt_Matrix; uniform highp float timestamp; -varying highp vec2 fTex; varying lowp float fFade; void main() { - fTex = vTex; highp float size = vData.z; highp float endSize = vData.w; @@ -21,8 +18,9 @@ void main() { if (t < 0. || t > 1.) currentSize = 0.; + gl_PointSize = currentSize; + highp vec2 pos = vPos - - currentSize / 2. + currentSize * vTex // adjust size + vVec.xy * t * vData.y // apply speed vector.. + 0.5 * vVec.zw * pow(t * vData.y, 2.); diff --git a/src/declarative/particles/qsgimageparticle.cpp b/src/declarative/particles/qsgimageparticle.cpp index 08d78af5ac..7a4654112a 100644 --- a/src/declarative/particles/qsgimageparticle.cpp +++ b/src/declarative/particles/qsgimageparticle.cpp @@ -287,9 +287,24 @@ public: const char *vertexShader() const { return m_vertex_code.constData(); } const char *fragmentShader() const { return m_fragment_code.constData(); } + void activate() { + QSGSimpleMaterialShader::activate(); +#ifndef QT_OPENGL_ES_2 + glEnable(GL_POINT_SPRITE); + glEnable(GL_VERTEX_PROGRAM_POINT_SIZE); +#endif + } + + void deactivate() { + QSGSimpleMaterialShader::deactivate(); +#ifndef QT_OPENGL_ES_2 + glDisable(GL_POINT_SPRITE); + glDisable(GL_VERTEX_PROGRAM_POINT_SIZE); +#endif + } + QList attributes() const { - return QList() << "vPos" << "vTex" << "vData" << "vVec" - << "vColor"; + return QList() << "vPos" << "vData" << "vVec" << "vColor"; } void initialize() { @@ -336,8 +351,24 @@ public: const char *vertexShader() const { return m_vertex_code.constData(); } const char *fragmentShader() const { return m_fragment_code.constData(); } + void activate() { + QSGSimpleMaterialShader::activate(); +#ifndef QT_OPENGL_ES_2 + glEnable(GL_POINT_SPRITE); + glEnable(GL_VERTEX_PROGRAM_POINT_SIZE); +#endif + } + + void deactivate() { + QSGSimpleMaterialShader::deactivate(); +#ifndef QT_OPENGL_ES_2 + glDisable(GL_POINT_SPRITE); + glDisable(GL_VERTEX_PROGRAM_POINT_SIZE); +#endif + } + QList attributes() const { - return QList() << "vPos" << "vTex" << "vData" << "vVec"; + return QList() << "vPos" << "vData" << "vVec"; } void initialize() { @@ -679,30 +710,28 @@ void QSGImageParticle::createEngine() static QSGGeometry::Attribute SimpleParticle_Attributes[] = { { 0, 2, GL_FLOAT }, // Position - { 1, 2, GL_FLOAT }, // TexCoord - { 2, 4, GL_FLOAT }, // Data - { 3, 4, GL_FLOAT } // Vectors + { 1, 4, GL_FLOAT }, // Data + { 2, 4, GL_FLOAT } // Vectors }; static QSGGeometry::AttributeSet SimpleParticle_AttributeSet = { - 4, // Attribute Count - (2 + 2 + 4 + 4 ) * sizeof(float), + 3, // Attribute Count + ( 2 + 4 + 4 ) * sizeof(float), SimpleParticle_Attributes }; static QSGGeometry::Attribute ColoredParticle_Attributes[] = { { 0, 2, GL_FLOAT }, // Position - { 1, 2, GL_FLOAT }, // TexCoord - { 2, 4, GL_FLOAT }, // Data - { 3, 4, GL_FLOAT }, // Vectors - { 4, 4, GL_UNSIGNED_BYTE }, // Colors + { 1, 4, GL_FLOAT }, // Data + { 2, 4, GL_FLOAT }, // Vectors + { 3, 4, GL_UNSIGNED_BYTE }, // Colors }; static QSGGeometry::AttributeSet ColoredParticle_AttributeSet = { - 5, // Attribute Count - (2 + 2 + 4 + 4) * sizeof(float) + 4 * sizeof(uchar), + 4, // Attribute Count + ( 2 + 4 + 4 ) * sizeof(float) + 4 * sizeof(uchar), ColoredParticle_Attributes }; @@ -855,12 +884,20 @@ QSGGeometryNode* QSGImageParticle::buildParticleNodes() else if (perfLevel == Deformable) g = new QSGGeometry(DeformableParticle_AttributeSet, vCount, iCount); else if (perfLevel == Colored) - g = new QSGGeometry(ColoredParticle_AttributeSet, vCount, iCount); + g = new QSGGeometry(ColoredParticle_AttributeSet, count, 0); else //Simple - g = new QSGGeometry(SimpleParticle_AttributeSet, vCount, iCount); + g = new QSGGeometry(SimpleParticle_AttributeSet, count, 0); node->setGeometry(g); - g->setDrawingMode(GL_TRIANGLES); + if (perfLevel <= Colored){ + g->setDrawingMode(GL_POINTS); + if (m_debugMode){ + GLfloat pointSizeRange[2]; + glGetFloatv(GL_ALIASED_POINT_SIZE_RANGE, pointSizeRange); + qDebug() << "Using point sprites, GL_ALIASED_POINT_SIZE_RANGE " <setDrawingMode(GL_TRIANGLES); for (int p=0; p < count; ++p) commit(gIdx, p);//commit sets geometry for the node, has its own perfLevel switch @@ -871,21 +908,19 @@ QSGGeometryNode* QSGImageParticle::buildParticleNodes() initTexCoords((DeformableVertex*)g->vertexData(), vCount); else if (perfLevel == Deformable) initTexCoords((DeformableVertex*)g->vertexData(), vCount); - else if (perfLevel == Colored) - initTexCoords((ColoredVertex*)g->vertexData(), vCount); - else //Simple - initTexCoords((SimpleVertex*)g->vertexData(), vCount); - - quint16 *indices = g->indexDataAsUShort(); - for (int i=0; i < count; ++i) { - int o = i * 4; - indices[0] = o; - indices[1] = o + 1; - indices[2] = o + 2; - indices[3] = o + 1; - indices[4] = o + 3; - indices[5] = o + 2; - indices += 6; + + if (perfLevel > Colored){ + quint16 *indices = g->indexDataAsUShort(); + for (int i=0; i < count; ++i) { + int o = i * 4; + indices[0] = o; + indices[1] = o + 1; + indices[2] = o + 2; + indices[3] = o + 1; + indices[4] = o + 3; + indices[5] = o + 2; + indices += 6; + } } } @@ -1114,8 +1149,8 @@ void QSGImageParticle::commit(int gIdx, int pIdx) } break; case Colored: - coloredVertices += pIdx*4; - for (int i=0; i<4; i++){ + coloredVertices += pIdx*1; + for (int i=0; i<1; i++){ coloredVertices[i].x = datum->x - m_systemOffset.x(); coloredVertices[i].y = datum->y - m_systemOffset.y(); coloredVertices[i].t = datum->t; @@ -1133,8 +1168,8 @@ void QSGImageParticle::commit(int gIdx, int pIdx) } break; case Simple: - simpleVertices += pIdx*4; - for (int i=0; i<4; i++){ + simpleVertices += pIdx*1; + for (int i=0; i<1; i++){ simpleVertices[i].x = datum->x - m_systemOffset.x(); simpleVertices[i].y = datum->y - m_systemOffset.y(); simpleVertices[i].t = datum->t; diff --git a/src/declarative/particles/qsgimageparticle_p.h b/src/declarative/particles/qsgimageparticle_p.h index f8a57a51b3..ff2155692e 100644 --- a/src/declarative/particles/qsgimageparticle_p.h +++ b/src/declarative/particles/qsgimageparticle_p.h @@ -61,8 +61,6 @@ class QSGSpriteEngine; struct SimpleVertex { float x; float y; - float tx; - float ty; float t; float lifeSpan; float size; @@ -76,8 +74,6 @@ struct SimpleVertex { struct ColoredVertex { float x; float y; - float tx; - float ty; float t; float lifeSpan; float size; -- cgit v1.2.3