OSDN Git Service

Detect GLSL vs GLSL/ES based on mime type
authorRhys Weatherley <rhys.weatherley@nokia.com>
Wed, 1 Dec 2010 00:04:56 +0000 (10:04 +1000)
committerRhys Weatherley <rhys.weatherley@nokia.com>
Wed, 1 Dec 2010 00:25:23 +0000 (10:25 +1000)
*.vert and *.frag are now for desktop shaders
*.vsh and *.fsh are now for ES shaders
File/New gives the user the choice which to create

src/plugins/glsleditor/GLSLEditor.mimetypes.xml
src/plugins/glsleditor/glsleditor.cpp
src/plugins/glsleditor/glsleditor.h
src/plugins/glsleditor/glsleditorconstants.h
src/plugins/glsleditor/glsleditorfactory.cpp
src/plugins/glsleditor/glsleditorplugin.cpp
src/plugins/glsleditor/glsleditorplugin.h
src/plugins/glsleditor/glslfilewizard.cpp
src/plugins/glsleditor/glslfilewizard.h

index 5d63307..9d2eb6c 100644 (file)
         <sub-class-of type="text/x-glsl"/>
         <comment>GLSL Fragment Shader file</comment>
         <glob pattern="*.frag"/>
+    </mime-type>
+
+    <mime-type type="text/x-glsl-es-frag">
+        <sub-class-of type="text/x-glsl"/>
+        <comment>GLSL/ES Fragment Shader file</comment>
         <glob pattern="*.fsh"/>
     </mime-type>
 
     <mime-type type="text/x-glsl-vert">
         <sub-class-of type="text/x-glsl"/>
-        <comment>GLSL Fragment Shader file</comment>
+        <comment>GLSL Vertex Shader file</comment>
         <glob pattern="*.vert"/>
+    </mime-type>
+
+    <mime-type type="text/x-glsl-es-vert">
+        <sub-class-of type="text/x-glsl"/>
+        <comment>GLSL/ES Vertex Shader file</comment>
         <glob pattern="*.vsh"/>
     </mime-type>
 
index 760636d..1350512 100644 (file)
@@ -316,8 +316,12 @@ void GLSLTextEditor::updateDocumentNow()
 {
     m_updateDocumentTimer->stop();
 
-    int variant = Lexer::Variant_GLSL_Qt; // ### hardcoded
+    int variant = 0;
 
+    if (isDesktopShader())
+        variant |= Lexer::Variant_GLSL_120;
+    else
+        variant |= Lexer::Variant_GLSL_Qt;
     if (isVertexShader())
         variant |= Lexer::Variant_VertexShader;
     if (isFragmentShader())
@@ -336,11 +340,11 @@ void GLSLTextEditor::updateDocumentNow()
         Semantic sem;
         Scope *globalScope = engine->newNamespace();
         doc->_globalScope = globalScope;
-        sem.translationUnit(plugin->shaderInit()->ast, globalScope, plugin->shaderInit()->engine);
+        sem.translationUnit(plugin->shaderInit(variant)->ast, globalScope, plugin->shaderInit(variant)->engine);
         if (variant & Lexer::Variant_VertexShader)
-            sem.translationUnit(plugin->vertexShaderInit()->ast, globalScope, plugin->vertexShaderInit()->engine);
+            sem.translationUnit(plugin->vertexShaderInit(variant)->ast, globalScope, plugin->vertexShaderInit(variant)->engine);
         if (variant & Lexer::Variant_FragmentShader)
-            sem.translationUnit(plugin->fragmentShaderInit()->ast, globalScope, plugin->fragmentShaderInit()->engine);
+            sem.translationUnit(plugin->fragmentShaderInit(variant)->ast, globalScope, plugin->fragmentShaderInit(variant)->engine);
         sem.translationUnit(ast, globalScope, engine);
 
         CreateRanges createRanges(document(), doc);
@@ -380,14 +384,28 @@ void GLSLTextEditor::updateDocumentNow()
     }
 }
 
+bool GLSLTextEditor::isDesktopShader() const
+{
+    return mimeType() == QLatin1String("text/x-glsl-vert") ||
+           mimeType() == QLatin1String("text/x-glsl-frag") ||
+           mimeType() == QLatin1String("text/x-glsl") || // Could be either.
+           mimeType() == QLatin1String("application/x-glsl");
+}
+
 bool GLSLTextEditor::isVertexShader() const
 {
-    return mimeType() == QLatin1String("text/x-glsl-vert");
+    return mimeType() == QLatin1String("text/x-glsl-vert") ||
+           mimeType() == QLatin1String("text/x-glsl-es-vert") ||
+           mimeType() == QLatin1String("text/x-glsl") || // Could be either.
+           mimeType() == QLatin1String("application/x-glsl");
 }
 
 bool GLSLTextEditor::isFragmentShader() const
 {
-    return mimeType() == QLatin1String("text/x-glsl-frag");
+    return mimeType() == QLatin1String("text/x-glsl-frag") ||
+           mimeType() == QLatin1String("text/x-glsl-es-frag") ||
+           mimeType() == QLatin1String("text/x-glsl") || // Could be either.
+           mimeType() == QLatin1String("application/x-glsl");
 }
 
 Document::Ptr GLSLTextEditor::glslDocument() const
index c5e822e..cc58263 100644 (file)
@@ -97,6 +97,7 @@ public:
 
     QSet<QString> identifiers() const;
 
+    bool isDesktopShader() const;
     bool isVertexShader() const;
     bool isFragmentShader() const;
 
index 0574872..e9d1f04 100644 (file)
@@ -50,6 +50,10 @@ const char * const TASK_INDEX = "GLSLEditor.TaskIndex";
 const char * const TASK_SEARCH = "GLSLEditor.TaskSearch";
 
 const char * const GLSL_MIMETYPE = "application/x-glsl";
+const char * const GLSL_MIMETYPE_VERT = "text/x-glsl-vert";
+const char * const GLSL_MIMETYPE_FRAG = "text/x-glsl-frag";
+const char * const GLSL_MIMETYPE_VERT_ES = "text/x-glsl-es-vert";
+const char * const GLSL_MIMETYPE_FRAG_ES = "text/x-glsl-es-frag";
 
 const char * const WIZARD_CATEGORY_GLSL = "U.GLSL";
 const char * const WIZARD_TR_CATEGORY_GLSL = QT_TRANSLATE_NOOP("GLSLEditor", "GLSL");
index 216dbf5..06cd4ed 100644 (file)
@@ -55,6 +55,10 @@ GLSLEditorFactory::GLSLEditorFactory(QObject *parent)
 {
     m_mimeTypes
             << QLatin1String(GLSLEditor::Constants::GLSL_MIMETYPE)
+            << QLatin1String(GLSLEditor::Constants::GLSL_MIMETYPE_VERT)
+            << QLatin1String(GLSLEditor::Constants::GLSL_MIMETYPE_FRAG)
+            << QLatin1String(GLSLEditor::Constants::GLSL_MIMETYPE_VERT_ES)
+            << QLatin1String(GLSLEditor::Constants::GLSL_MIMETYPE_FRAG_ES)
             ;
 }
 
index 825b245..6c47328 100644 (file)
@@ -173,6 +173,14 @@ bool GLSLEditorPlugin::initialize(const QStringList & /*arguments*/, QString *er
     Core::MimeDatabase *mimeDatabase = Core::ICore::instance()->mimeDatabase();
     iconProvider->registerIconOverlayForMimeType(QIcon(QLatin1String(":/glsleditor/images/glslfile.png")),
                                                  mimeDatabase->findByType(QLatin1String(GLSLEditor::Constants::GLSL_MIMETYPE)));
+    iconProvider->registerIconOverlayForMimeType(QIcon(QLatin1String(":/glsleditor/images/glslfile.png")),
+                                                 mimeDatabase->findByType(QLatin1String(GLSLEditor::Constants::GLSL_MIMETYPE_VERT)));
+    iconProvider->registerIconOverlayForMimeType(QIcon(QLatin1String(":/glsleditor/images/glslfile.png")),
+                                                 mimeDatabase->findByType(QLatin1String(GLSLEditor::Constants::GLSL_MIMETYPE_FRAG)));
+    iconProvider->registerIconOverlayForMimeType(QIcon(QLatin1String(":/glsleditor/images/glslfile.png")),
+                                                 mimeDatabase->findByType(QLatin1String(GLSLEditor::Constants::GLSL_MIMETYPE_VERT_ES)));
+    iconProvider->registerIconOverlayForMimeType(QIcon(QLatin1String(":/glsleditor/images/glslfile.png")),
+                                                 mimeDatabase->findByType(QLatin1String(GLSLEditor::Constants::GLSL_MIMETYPE_FRAG_ES)));
 
     Core::BaseFileWizardParameters fragWizardParameters(Core::IWizard::FileWizard);
     fragWizardParameters.setCategory(QLatin1String(Constants::WIZARD_CATEGORY_GLSL));
@@ -182,9 +190,9 @@ bool GLSLEditorPlugin::initialize(const QStringList & /*arguments*/, QString *er
             "Language (GLSL/ES).  Fragment shaders generate the final "
             "pixel colors for triangles, points, and lines rendered "
             "with OpenGL."));
-    fragWizardParameters.setDisplayName(tr("Fragment shader"));
+    fragWizardParameters.setDisplayName(tr("Fragment shader (OpenGL/ES 2.0)"));
     fragWizardParameters.setId(QLatin1String("F.GLSL"));
-    addAutoReleasedObject(new GLSLFileWizard(fragWizardParameters, GLSLFileWizard::FragmentShader, core));
+    addAutoReleasedObject(new GLSLFileWizard(fragWizardParameters, GLSLFileWizard::FragmentShaderES, core));
 
     Core::BaseFileWizardParameters vertWizardParameters(Core::IWizard::FileWizard);
     vertWizardParameters.setCategory(QLatin1String(Constants::WIZARD_CATEGORY_GLSL));
@@ -194,9 +202,27 @@ bool GLSLEditorPlugin::initialize(const QStringList & /*arguments*/, QString *er
             "Language (GLSL/ES).  Vertex shaders transform the "
             "positions, normals, and texture co-ordinates of "
             "triangles, points, and lines rendered with OpenGL."));
-    vertWizardParameters.setDisplayName(tr("Vertex shader"));
-    vertWizardParameters.setId(QLatin1String("V.GLSL"));
-    addAutoReleasedObject(new GLSLFileWizard(vertWizardParameters, GLSLFileWizard::VertexShader, core));
+    vertWizardParameters.setDisplayName(tr("Vertex shader (OpenGL/ES 2.0)"));
+    vertWizardParameters.setId(QLatin1String("G.GLSL"));
+    addAutoReleasedObject(new GLSLFileWizard(vertWizardParameters, GLSLFileWizard::VertexShaderES, core));
+
+    fragWizardParameters.setDescription
+        (tr("Creates a fragment shader in the Desktop OpenGL Shading "
+            "Language (GLSL).  Fragment shaders generate the final "
+            "pixel colors for triangles, points, and lines rendered "
+            "with OpenGL."));
+    fragWizardParameters.setDisplayName(tr("Fragment shader (Desktop OpenGL)"));
+    fragWizardParameters.setId(QLatin1String("J.GLSL"));
+    addAutoReleasedObject(new GLSLFileWizard(fragWizardParameters, GLSLFileWizard::FragmentShaderDesktop, core));
+
+    vertWizardParameters.setDescription
+        (tr("Creates a vertex shader in the Desktop OpenGL Shading "
+            "Language (GLSL).  Vertex shaders transform the "
+            "positions, normals, and texture co-ordinates of "
+            "triangles, points, and lines rendered with OpenGL."));
+    vertWizardParameters.setDisplayName(tr("Vertex shader (Desktop OpenGL)"));
+    vertWizardParameters.setId(QLatin1String("K.GLSL"));
+    addAutoReleasedObject(new GLSLFileWizard(vertWizardParameters, GLSLFileWizard::VertexShaderDesktop, core));
 
     return true;
 }
@@ -263,23 +289,28 @@ void GLSLEditorPlugin::parseGlslFile(const QString &fileName, InitFile *initFile
     initFile->ast = parser.parse();
 }
 
-const GLSLEditorPlugin::InitFile *GLSLEditorPlugin::fragmentShaderInit() const
+const GLSLEditorPlugin::InitFile *GLSLEditorPlugin::fragmentShaderInit(int variant) const
 {
-    // TODO: select the correct language variant
-    //return &m_glsl_120_frag;
-    return &m_glsl_es_100_frag;
+    if (variant & GLSL::Lexer::Variant_GLSL_120)
+        return &m_glsl_120_frag;
+    else
+        return &m_glsl_es_100_frag;
 }
 
-const GLSLEditorPlugin::InitFile *GLSLEditorPlugin::vertexShaderInit() const
+const GLSLEditorPlugin::InitFile *GLSLEditorPlugin::vertexShaderInit(int variant) const
 {
-    //return &m_glsl_120_vert;
-    return &m_glsl_es_100_vert;
+    if (variant & GLSL::Lexer::Variant_GLSL_120)
+        return &m_glsl_120_vert;
+    else
+        return &m_glsl_es_100_vert;
 }
 
-const GLSLEditorPlugin::InitFile *GLSLEditorPlugin::shaderInit() const
+const GLSLEditorPlugin::InitFile *GLSLEditorPlugin::shaderInit(int variant) const
 {
-    //return &m_glsl_120_common;
-    return &m_glsl_es_100_common;
+    if (variant & GLSL::Lexer::Variant_GLSL_120)
+        return &m_glsl_120_common;
+    else
+        return &m_glsl_es_100_common;
 }
 
 Q_EXPORT_PLUGIN(GLSLEditorPlugin)
index 6561d64..d9ddbfb 100644 (file)
@@ -94,9 +94,9 @@ public:
         ~InitFile();
     };
 
-    const InitFile *fragmentShaderInit() const;
-    const InitFile *vertexShaderInit() const;
-    const InitFile *shaderInit() const;
+    const InitFile *fragmentShaderInit(int variant) const;
+    const InitFile *vertexShaderInit(int variant) const;
+    const InitFile *shaderInit(int variant) const;
 
 private:
     QByteArray glslFile(const QString &fileName);
index 5dc020a..35759ba 100644 (file)
@@ -67,7 +67,6 @@ Core::GeneratedFiles GLSLFileWizard::generateFiles(const QWizard *w,
     const QString path = wizardDialog->path();
     const QString name = wizardDialog->fileName();
 
-    const QString mimeType = QLatin1String(Constants::GLSL_MIMETYPE);
     const QString fileName = Core::BaseFileWizard::buildFileName(path, name, preferredSuffix(m_shaderType));
 
     Core::GeneratedFile file(fileName);
@@ -82,7 +81,7 @@ QString GLSLFileWizard::fileContents(const QString &, ShaderType shaderType) con
     QTextStream str(&contents);
 
     switch (shaderType) {
-    case GLSLFileWizard::VertexShader:
+    case GLSLFileWizard::VertexShaderES:
         str << QLatin1String("attribute highp vec4 qgl_Vertex;\n")
             << QLatin1String("attribute highp vec4 qgl_TexCoord0;\n")
             << QLatin1String("uniform highp mat4 qgl_ModelViewProjectionMatrix;\n")
@@ -94,7 +93,7 @@ QString GLSLFileWizard::fileContents(const QString &, ShaderType shaderType) con
             << QLatin1String("    qTexCoord0 = qgl_TexCoord0;\n")
             << QLatin1String("}\n");
         break;
-    case GLSLFileWizard::FragmentShader:
+    case GLSLFileWizard::FragmentShaderES:
         str << QLatin1String("uniform sampler2D qgl_Texture0;\n")
             << QLatin1String("varying highp vec4 qTexCoord0;\n")
             << QLatin1String("\n")
@@ -103,6 +102,27 @@ QString GLSLFileWizard::fileContents(const QString &, ShaderType shaderType) con
             << QLatin1String("    gl_FragColor = texture2D(qgl_Texture0, qTexCoord0.st);\n")
             << QLatin1String("}\n");
         break;
+    case GLSLFileWizard::VertexShaderDesktop:
+        str << QLatin1String("attribute vec4 qgl_Vertex;\n")
+            << QLatin1String("attribute vec4 qgl_TexCoord0;\n")
+            << QLatin1String("uniform mat4 qgl_ModelViewProjectionMatrix;\n")
+            << QLatin1String("varying vec4 qTexCoord0;\n")
+            << QLatin1String("\n")
+            << QLatin1String("void main(void)\n")
+            << QLatin1String("{\n")
+            << QLatin1String("    gl_Position = qgl_ModelViewProjectionMatrix * qgl_Vertex;\n")
+            << QLatin1String("    qTexCoord0 = qgl_TexCoord0;\n")
+            << QLatin1String("}\n");
+        break;
+    case GLSLFileWizard::FragmentShaderDesktop:
+        str << QLatin1String("uniform sampler2D qgl_Texture0;\n")
+            << QLatin1String("varying vec4 qTexCoord0;\n")
+            << QLatin1String("\n")
+            << QLatin1String("void main(void)\n")
+            << QLatin1String("{\n")
+            << QLatin1String("    gl_FragColor = texture2D(qgl_Texture0, qTexCoord0.st);\n")
+            << QLatin1String("}\n");
+        break;
     default: break;
     }
 
@@ -124,9 +144,13 @@ QWizard *GLSLFileWizard::createWizardDialog(QWidget *parent, const QString &defa
 QString GLSLFileWizard::preferredSuffix(ShaderType shaderType) const
 {
     switch (shaderType) {
-    case GLSLFileWizard::VertexShader:
+    case GLSLFileWizard::VertexShaderES:
+        return QLatin1String("vsh");
+    case GLSLFileWizard::FragmentShaderES:
+        return QLatin1String("fsh");
+    case GLSLFileWizard::VertexShaderDesktop:
         return QLatin1String("vert");
-    case GLSLFileWizard::FragmentShader:
+    case GLSLFileWizard::FragmentShaderDesktop:
         return QLatin1String("frag");
     default:
         return QLatin1String("glsl");
index 9823dcc..2133ef2 100644 (file)
@@ -43,8 +43,10 @@ public:
 
     enum ShaderType
     {
-        VertexShader,
-        FragmentShader
+        VertexShaderES,
+        FragmentShaderES,
+        VertexShaderDesktop,
+        FragmentShaderDesktop
     };
 
     explicit GLSLFileWizard(const BaseFileWizardParameters &parameters,