aboutsummaryrefslogtreecommitdiffstats
path: root/src/particles/shaders
diff options
context:
space:
mode:
authorSean Harmer <sean.harmer@kdab.com>2013-10-26 18:48:56 +0100
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-10-31 12:54:28 +0100
commit426f6aa672b94d8324321bc6c6d4f1a358eebedc (patch)
tree8af3e5b0aa2fdcdb7655672669f5ae277151cc3d /src/particles/shaders
parent34c85bb56c92316a6ce1c79d25f9653fec14791c (diff)
Refactor shaders into seprate GLSL source files
The default implementation of QSGShaderMaterial::vertexShader() and fragmentShader() now loads the GLSL source from a list of source files that can be specified via the setShaderSourceFile() or setShaderSourceFiles() functions. Multiple shader source files for each shader stage are supported. Each source file will be read in the order specified and concatenated together before being compiled. The other places where Qt Quick 2 loads shader source code have been adapted to use the new QSGShaderSourceBuilder, which is also used internally by QSGMaterial. This puts Qt Quick 2 into a better state ready to support OpenGL core profile and to load different shaders based upon OpenGL version, profile, GPU vendor, platform, etc. Change-Id: I1a66213c2ce788413168eb48c7bc5317e61988a2 Reviewed-by: Gunnar Sletta <gunnar.sletta@digia.com>
Diffstat (limited to 'src/particles/shaders')
-rw-r--r--src/particles/shaders/customparticle.frag9
-rw-r--r--src/particles/shaders/customparticle.vert4
-rw-r--r--src/particles/shaders/customparticletemplate.vert26
-rw-r--r--src/particles/shaders/imageparticle.frag40
-rw-r--r--src/particles/shaders/imageparticle.vert143
5 files changed, 222 insertions, 0 deletions
diff --git a/src/particles/shaders/customparticle.frag b/src/particles/shaders/customparticle.frag
new file mode 100644
index 0000000000..c1c15ecb0c
--- /dev/null
+++ b/src/particles/shaders/customparticle.frag
@@ -0,0 +1,9 @@
+varying highp vec2 qt_TexCoord0;
+
+uniform sampler2D source;
+uniform lowp float qt_Opacity;
+
+void main()
+{
+ gl_FragColor = texture2D(source, qt_TexCoord0) * qt_Opacity;
+} \ No newline at end of file
diff --git a/src/particles/shaders/customparticle.vert b/src/particles/shaders/customparticle.vert
new file mode 100644
index 0000000000..b99f73ea53
--- /dev/null
+++ b/src/particles/shaders/customparticle.vert
@@ -0,0 +1,4 @@
+void main()
+{
+ defaultMain();
+} \ No newline at end of file
diff --git a/src/particles/shaders/customparticletemplate.vert b/src/particles/shaders/customparticletemplate.vert
new file mode 100644
index 0000000000..c482c4207b
--- /dev/null
+++ b/src/particles/shaders/customparticletemplate.vert
@@ -0,0 +1,26 @@
+attribute highp vec2 qt_ParticlePos;
+attribute highp vec2 qt_ParticleTex;
+attribute highp vec4 qt_ParticleData; // x = time, y = lifeSpan, z = size, w = endSize
+attribute highp vec4 qt_ParticleVec; // x,y = constant velocity, z,w = acceleration
+attribute highp float qt_ParticleR;
+
+uniform highp mat4 qt_Matrix;
+uniform highp float qt_Timestamp;
+
+varying highp vec2 qt_TexCoord0;
+
+void defaultMain()
+{
+ qt_TexCoord0 = qt_ParticleTex;
+ highp float size = qt_ParticleData.z;
+ highp float endSize = qt_ParticleData.w;
+ highp float t = (qt_Timestamp - qt_ParticleData.x) / qt_ParticleData.y;
+ highp float currentSize = mix(size, endSize, t * t);
+ if (t < 0. || t > 1.)
+ currentSize = 0.;
+ highp vec2 pos = qt_ParticlePos
+ - currentSize / 2. + currentSize * qt_ParticleTex // adjust size
+ + qt_ParticleVec.xy * t * qt_ParticleData.y // apply velocity vector..
+ + 0.5 * qt_ParticleVec.zw * pow(t * qt_ParticleData.y, 2.);
+ gl_Position = qt_Matrix * vec4(pos.x, pos.y, 0, 1);
+}
diff --git a/src/particles/shaders/imageparticle.frag b/src/particles/shaders/imageparticle.frag
new file mode 100644
index 0000000000..699b90babf
--- /dev/null
+++ b/src/particles/shaders/imageparticle.frag
@@ -0,0 +1,40 @@
+uniform sampler2D _qt_texture;
+uniform lowp float qt_Opacity;
+
+#if defined(SPRITE)
+varying highp vec4 fTexS;
+#elif defined(DEFORM)
+varying highp vec2 fTex;
+#endif
+
+#if defined(COLOR)
+varying lowp vec4 fColor;
+#else
+varying lowp float fFade;
+#endif
+
+#if defined(TABLE)
+varying lowp vec2 tt;
+uniform sampler2D colortable;
+#endif
+
+void main()
+{
+#if defined(SPRITE)
+ gl_FragColor = mix(texture2D(_qt_texture, fTexS.xy), texture2D(_qt_texture, fTexS.zw), tt.y)
+ * fColor
+ * texture2D(colortable, tt)
+ * qt_Opacity;
+#elif defined(TABLE)
+ gl_FragColor = texture2D(_qt_texture, fTex)
+ * fColor
+ * texture2D(colortable, tt)
+ * qt_Opacity;
+#elif defined(DEFORM)
+ gl_FragColor = (texture2D(_qt_texture, fTex)) * fColor * qt_Opacity;
+#elif defined(COLOR)
+ gl_FragColor = (texture2D(_qt_texture, gl_PointCoord)) * fColor * qt_Opacity;
+#else
+ gl_FragColor = texture2D(_qt_texture, gl_PointCoord) * (fFade * qt_Opacity);
+#endif
+} \ No newline at end of file
diff --git a/src/particles/shaders/imageparticle.vert b/src/particles/shaders/imageparticle.vert
new file mode 100644
index 0000000000..9e607a7477
--- /dev/null
+++ b/src/particles/shaders/imageparticle.vert
@@ -0,0 +1,143 @@
+#if defined(DEFORM)
+attribute highp vec4 vPosTex;
+#else
+attribute highp vec2 vPos;
+#endif
+
+attribute highp vec4 vData; // x = time, y = lifeSpan, z = size, w = endSize
+attribute highp vec4 vVec; // x,y = constant velocity, z,w = acceleration
+uniform highp float entry;
+
+#if defined(COLOR)
+attribute highp vec4 vColor;
+#endif
+
+#if defined(DEFORM)
+attribute highp vec4 vDeformVec; // x,y x unit vector; z,w = y unit vector
+attribute highp vec3 vRotation; // x = radians of rotation, y = rotation velocity, z = bool autoRotate
+#endif
+
+#if defined(SPRITE)
+attribute highp vec3 vAnimData; // w,h(premultiplied of anim), interpolation progress
+attribute highp vec4 vAnimPos; // x,y, x,y (two frames for interpolation)
+#endif
+
+uniform highp mat4 qt_Matrix;
+uniform highp float timestamp;
+
+#if defined(TABLE)
+varying lowp vec2 tt;//y is progress if Sprite mode
+uniform highp float sizetable[64];
+uniform highp float opacitytable[64];
+#endif
+
+#if defined(SPRITE)
+varying highp vec4 fTexS;
+#elif defined(DEFORM)
+varying highp vec2 fTex;
+#endif
+
+#if defined(COLOR)
+varying lowp vec4 fColor;
+#else
+varying lowp float fFade;
+#endif
+
+
+void main()
+{
+ highp float t = (timestamp - vData.x) / vData.y;
+ if (t < 0. || t > 1.) {
+#if defined(DEFORM)
+ gl_Position = qt_Matrix * vec4(vPosTex.x, vPosTex.y, 0., 1.);
+#else
+ gl_PointSize = 0.;
+#endif
+ } else {
+#if defined(SPRITE)
+ tt.y = vAnimData.z;
+
+ // Calculate frame location in texture
+ fTexS.xy = vAnimPos.xy + vPosTex.zw * vAnimData.xy;
+
+ // Next frame is also passed, for interpolation
+ fTexS.zw = vAnimPos.zw + vPosTex.zw * vAnimData.xy;
+
+#elif defined(DEFORM)
+ fTex = vPosTex.zw;
+#endif
+ highp float currentSize = mix(vData.z, vData.w, t * t);
+#if defined (Q_OS_BLACKBERRY)
+ highp float fade = 1.;
+#else
+ lowp float fade = 1.;
+#endif
+ highp float fadeIn = min(t * 10., 1.);
+ highp float fadeOut = 1. - clamp((t - 0.75) * 4.,0., 1.);
+
+#if defined(TABLE)
+ currentSize = currentSize * sizetable[int(floor(t*64.))];
+ fade = fade * opacitytable[int(floor(t*64.))];
+#endif
+
+ if (entry == 1.)
+ fade = fade * fadeIn * fadeOut;
+ else if (entry == 2.)
+ currentSize = currentSize * fadeIn * fadeOut;
+
+ if (currentSize <= 0.) {
+#if defined(DEFORM)
+ gl_Position = qt_Matrix * vec4(vPosTex.x, vPosTex.y, 0., 1.);
+#else
+ gl_PointSize = 0.;
+#endif
+ } else {
+ if (currentSize < 3.) // Sizes too small look jittery as they move
+ currentSize = 3.;
+
+ highp vec2 pos;
+#if defined(DEFORM)
+ highp float rotation = vRotation.x + vRotation.y * t * vData.y;
+ if (vRotation.z == 1.0){
+ highp vec2 curVel = vVec.zw * t * vData.y + vVec.xy;
+ if (length(curVel) > 0.)
+ rotation += atan(curVel.y, curVel.x);
+ }
+ highp vec2 trigCalcs = vec2(cos(rotation), sin(rotation));
+ highp vec4 deform = vDeformVec * currentSize * (vPosTex.zzww - 0.5);
+ highp vec4 rotatedDeform = deform.xxzz * trigCalcs.xyxy;
+ rotatedDeform = rotatedDeform + (deform.yyww * trigCalcs.yxyx * vec4(-1.,1.,-1.,1.));
+ /* The readable version:
+ highp vec2 xDeform = vDeformVec.xy * currentSize * (vTex.x-0.5);
+ highp vec2 yDeform = vDeformVec.zw * currentSize * (vTex.y-0.5);
+ highp vec2 xRotatedDeform;
+ xRotatedDeform.x = trigCalcs.x*xDeform.x - trigCalcs.y*xDeform.y;
+ xRotatedDeform.y = trigCalcs.y*xDeform.x + trigCalcs.x*xDeform.y;
+ highp vec2 yRotatedDeform;
+ yRotatedDeform.x = trigCalcs.x*yDeform.x - trigCalcs.y*yDeform.y;
+ yRotatedDeform.y = trigCalcs.y*yDeform.x + trigCalcs.x*yDeform.y;
+ */
+ pos = vPosTex.xy
+ + rotatedDeform.xy
+ + rotatedDeform.zw
+ + vVec.xy * t * vData.y // apply velocity
+ + 0.5 * vVec.zw * pow(t * vData.y, 2.); // apply acceleration
+#else
+ pos = vPos
+ + vVec.xy * t * vData.y // apply velocity vector..
+ + 0.5 * vVec.zw * pow(t * vData.y, 2.);
+ gl_PointSize = currentSize;
+#endif
+ gl_Position = qt_Matrix * vec4(pos.x, pos.y, 0, 1);
+
+#if defined(COLOR)
+ fColor = vColor * fade;
+#else
+ fFade = fade;
+#endif
+#if defined(TABLE)
+ tt.x = t;
+#endif
+ }
+ }
+} \ No newline at end of file