Use point sprites
authorAlan Alpert <alan.alpert@nokia.com>
Fri, 19 Aug 2011 04:56:19 +0000 (14:56 +1000)
committerQt by Nokia <qt-info@nokia.com>
Wed, 24 Aug 2011 02:23:40 +0000 (04:23 +0200)
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 <qt_sanity_bot@ovi.com>
Reviewed-by: Gunnar Sletta <gunnar.sletta@nokia.com>

src/declarative/particles/defaultshaders/coloredfragment.shader
src/declarative/particles/defaultshaders/coloredvertex.shader
src/declarative/particles/defaultshaders/simplefragment.shader
src/declarative/particles/defaultshaders/simplevertex.shader
src/declarative/particles/qsgimageparticle.cpp
src/declarative/particles/qsgimageparticle_p.h

index 383888b..4ae2643 100644 (file)
@@ -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;
 }
 
index d6498f2..cdbb130 100644 (file)
@@ -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.);
 
index 0b0adaa..9b445da 100644 (file)
@@ -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);
 }
index b7542e6..1c07363 100644 (file)
@@ -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.);
 
index 08d78af..7a46541 100644 (file)
@@ -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<ColoredMaterialData>::activate();
+#ifndef QT_OPENGL_ES_2
+        glEnable(GL_POINT_SPRITE);
+        glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
+#endif
+    }
+
+    void deactivate() {
+        QSGSimpleMaterialShader<ColoredMaterialData>::deactivate();
+#ifndef QT_OPENGL_ES_2
+        glDisable(GL_POINT_SPRITE);
+        glDisable(GL_VERTEX_PROGRAM_POINT_SIZE);
+#endif
+    }
+
     QList<QByteArray> attributes() const {
-        return QList<QByteArray>() << "vPos" << "vTex" << "vData" << "vVec"
-            << "vColor";
+        return QList<QByteArray>() << "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<SimpleMaterialData>::activate();
+#ifndef QT_OPENGL_ES_2
+        glEnable(GL_POINT_SPRITE);
+        glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
+#endif
+    }
+
+    void deactivate() {
+        QSGSimpleMaterialShader<SimpleMaterialData>::deactivate();
+#ifndef QT_OPENGL_ES_2
+        glDisable(GL_POINT_SPRITE);
+        glDisable(GL_VERTEX_PROGRAM_POINT_SIZE);
+#endif
+    }
+
     QList<QByteArray> attributes() const {
-        return QList<QByteArray>() << "vPos" << "vTex" << "vData" << "vVec";
+        return QList<QByteArray>() << "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 " <<pointSizeRange[0] << ":" << pointSizeRange[1];
+            }
+        }else
+            g->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>((DeformableVertex*)g->vertexData(), vCount);
         else if (perfLevel == Deformable)
             initTexCoords<DeformableVertex>((DeformableVertex*)g->vertexData(), vCount);
-        else if (perfLevel == Colored)
-            initTexCoords<ColoredVertex>((ColoredVertex*)g->vertexData(), vCount);
-        else //Simple
-            initTexCoords<SimpleVertex>((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;
index f8a57a5..ff21556 100644 (file)
@@ -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;