From 426f6aa672b94d8324321bc6c6d4f1a358eebedc Mon Sep 17 00:00:00 2001 From: Sean Harmer Date: Sat, 26 Oct 2013 18:48:56 +0100 Subject: 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 --- src/particles/qquickcustomparticle.cpp | 53 ++++++++-------------------------- 1 file changed, 12 insertions(+), 41 deletions(-) (limited to 'src/particles/qquickcustomparticle.cpp') diff --git a/src/particles/qquickcustomparticle.cpp b/src/particles/qquickcustomparticle.cpp index 9bba3ebb18..71d58ae391 100644 --- a/src/particles/qquickcustomparticle.cpp +++ b/src/particles/qquickcustomparticle.cpp @@ -41,47 +41,11 @@ #include "qquickcustomparticle_p.h" #include +#include #include QT_BEGIN_NAMESPACE -//Includes comments because the code isn't self explanatory -static const char qt_particles_template_vertex_code[] = - "attribute highp vec2 qt_ParticlePos;\n" - "attribute highp vec2 qt_ParticleTex;\n" - "attribute highp vec4 qt_ParticleData; // x = time, y = lifeSpan, z = size, w = endSize\n" - "attribute highp vec4 qt_ParticleVec; // x,y = constant velocity, z,w = acceleration\n" - "attribute highp float qt_ParticleR;\n" - "uniform highp mat4 qt_Matrix;\n" - "uniform highp float qt_Timestamp;\n" - "varying highp vec2 qt_TexCoord0;\n" - "void defaultMain() {\n" - " qt_TexCoord0 = qt_ParticleTex;\n" - " highp float size = qt_ParticleData.z;\n" - " highp float endSize = qt_ParticleData.w;\n" - " highp float t = (qt_Timestamp - qt_ParticleData.x) / qt_ParticleData.y;\n" - " highp float currentSize = mix(size, endSize, t * t);\n" - " if (t < 0. || t > 1.)\n" - " currentSize = 0.;\n" - " highp vec2 pos = qt_ParticlePos\n" - " - currentSize / 2. + currentSize * qt_ParticleTex // adjust size\n" - " + qt_ParticleVec.xy * t * qt_ParticleData.y // apply velocity vector..\n" - " + 0.5 * qt_ParticleVec.zw * pow(t * qt_ParticleData.y, 2.);\n" - " gl_Position = qt_Matrix * vec4(pos.x, pos.y, 0, 1);\n" - "}"; -static const char qt_particles_default_vertex_code[] = - "void main() { \n" - " defaultMain(); \n" - "}"; - -static const char qt_particles_default_fragment_code[] = - "uniform sampler2D source; \n" - "varying highp vec2 qt_TexCoord0; \n" - "uniform lowp float qt_Opacity; \n" - "void main() { \n" - " gl_FragColor = texture2D(source, qt_TexCoord0) * qt_Opacity; \n" - "}"; - static QSGGeometry::Attribute PlainParticle_Attributes[] = { QSGGeometry::Attribute::create(0, 2, GL_FLOAT, true), // Position QSGGeometry::Attribute::create(1, 2, GL_FLOAT), // TexCoord @@ -309,11 +273,18 @@ QQuickShaderEffectNode *QQuickCustomParticle::prepareNextFrame(QQuickShaderEffec Q_ASSERT(material); Key s = m_common.source; - if (s.sourceCode[Key::FragmentShader].isEmpty()) - s.sourceCode[Key::FragmentShader] = qt_particles_default_fragment_code; + QSGShaderSourceBuilder builder; + if (s.sourceCode[Key::FragmentShader].isEmpty()) { + builder.appendSourceFile(QStringLiteral(":/particles/shaders/customparticle.frag")); + s.sourceCode[Key::FragmentShader] = builder.source(); + builder.clear(); + } + + builder.appendSourceFile(QStringLiteral(":/particles/shaders/customparticletemplate.vert")); if (s.sourceCode[Key::VertexShader].isEmpty()) - s.sourceCode[Key::VertexShader] = qt_particles_default_vertex_code; - s.sourceCode[Key::VertexShader] = qt_particles_template_vertex_code + s.sourceCode[Key::VertexShader]; + builder.appendSourceFile(QStringLiteral(":/particles/shaders/customparticle.vert")); + s.sourceCode[Key::VertexShader] = builder.source() + s.sourceCode[Key::VertexShader]; + s.className = metaObject()->className(); material->setProgramSource(s); -- cgit v1.2.3