Detect GLSL vs GLSL/ES based on mime type

*.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
This commit is contained in:
Rhys Weatherley
2010-12-01 10:04:56 +10:00
parent 1b3016c571
commit 85aa32dfca
9 changed files with 126 additions and 32 deletions

View File

@@ -13,13 +13,23 @@
<sub-class-of type="text/x-glsl"/> <sub-class-of type="text/x-glsl"/>
<comment>GLSL Fragment Shader file</comment> <comment>GLSL Fragment Shader file</comment>
<glob pattern="*.frag"/> <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"/> <glob pattern="*.fsh"/>
</mime-type> </mime-type>
<mime-type type="text/x-glsl-vert"> <mime-type type="text/x-glsl-vert">
<sub-class-of type="text/x-glsl"/> <sub-class-of type="text/x-glsl"/>
<comment>GLSL Fragment Shader file</comment> <comment>GLSL Vertex Shader file</comment>
<glob pattern="*.vert"/> <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"/> <glob pattern="*.vsh"/>
</mime-type> </mime-type>

View File

@@ -316,8 +316,12 @@ void GLSLTextEditor::updateDocumentNow()
{ {
m_updateDocumentTimer->stop(); 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()) if (isVertexShader())
variant |= Lexer::Variant_VertexShader; variant |= Lexer::Variant_VertexShader;
if (isFragmentShader()) if (isFragmentShader())
@@ -336,11 +340,11 @@ void GLSLTextEditor::updateDocumentNow()
Semantic sem; Semantic sem;
Scope *globalScope = engine->newNamespace(); Scope *globalScope = engine->newNamespace();
doc->_globalScope = globalScope; 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) 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) 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); sem.translationUnit(ast, globalScope, engine);
CreateRanges createRanges(document(), doc); 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 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 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 Document::Ptr GLSLTextEditor::glslDocument() const

View File

@@ -97,6 +97,7 @@ public:
QSet<QString> identifiers() const; QSet<QString> identifiers() const;
bool isDesktopShader() const;
bool isVertexShader() const; bool isVertexShader() const;
bool isFragmentShader() const; bool isFragmentShader() const;

View File

@@ -50,6 +50,10 @@ const char * const TASK_INDEX = "GLSLEditor.TaskIndex";
const char * const TASK_SEARCH = "GLSLEditor.TaskSearch"; const char * const TASK_SEARCH = "GLSLEditor.TaskSearch";
const char * const GLSL_MIMETYPE = "application/x-glsl"; 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_CATEGORY_GLSL = "U.GLSL";
const char * const WIZARD_TR_CATEGORY_GLSL = QT_TRANSLATE_NOOP("GLSLEditor", "GLSL"); const char * const WIZARD_TR_CATEGORY_GLSL = QT_TRANSLATE_NOOP("GLSLEditor", "GLSL");

View File

@@ -55,6 +55,10 @@ GLSLEditorFactory::GLSLEditorFactory(QObject *parent)
{ {
m_mimeTypes m_mimeTypes
<< QLatin1String(GLSLEditor::Constants::GLSL_MIMETYPE) << 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)
; ;
} }

View File

@@ -173,6 +173,14 @@ bool GLSLEditorPlugin::initialize(const QStringList & /*arguments*/, QString *er
Core::MimeDatabase *mimeDatabase = Core::ICore::instance()->mimeDatabase(); Core::MimeDatabase *mimeDatabase = Core::ICore::instance()->mimeDatabase();
iconProvider->registerIconOverlayForMimeType(QIcon(QLatin1String(":/glsleditor/images/glslfile.png")), iconProvider->registerIconOverlayForMimeType(QIcon(QLatin1String(":/glsleditor/images/glslfile.png")),
mimeDatabase->findByType(QLatin1String(GLSLEditor::Constants::GLSL_MIMETYPE))); 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); Core::BaseFileWizardParameters fragWizardParameters(Core::IWizard::FileWizard);
fragWizardParameters.setCategory(QLatin1String(Constants::WIZARD_CATEGORY_GLSL)); 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 " "Language (GLSL/ES). Fragment shaders generate the final "
"pixel colors for triangles, points, and lines rendered " "pixel colors for triangles, points, and lines rendered "
"with OpenGL.")); "with OpenGL."));
fragWizardParameters.setDisplayName(tr("Fragment shader")); fragWizardParameters.setDisplayName(tr("Fragment shader (OpenGL/ES 2.0)"));
fragWizardParameters.setId(QLatin1String("F.GLSL")); 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); Core::BaseFileWizardParameters vertWizardParameters(Core::IWizard::FileWizard);
vertWizardParameters.setCategory(QLatin1String(Constants::WIZARD_CATEGORY_GLSL)); 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 " "Language (GLSL/ES). Vertex shaders transform the "
"positions, normals, and texture co-ordinates of " "positions, normals, and texture co-ordinates of "
"triangles, points, and lines rendered with OpenGL.")); "triangles, points, and lines rendered with OpenGL."));
vertWizardParameters.setDisplayName(tr("Vertex shader")); vertWizardParameters.setDisplayName(tr("Vertex shader (OpenGL/ES 2.0)"));
vertWizardParameters.setId(QLatin1String("V.GLSL")); vertWizardParameters.setId(QLatin1String("G.GLSL"));
addAutoReleasedObject(new GLSLFileWizard(vertWizardParameters, GLSLFileWizard::VertexShader, core)); 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; return true;
} }
@@ -263,22 +289,27 @@ void GLSLEditorPlugin::parseGlslFile(const QString &fileName, InitFile *initFile
initFile->ast = parser.parse(); initFile->ast = parser.parse();
} }
const GLSLEditorPlugin::InitFile *GLSLEditorPlugin::fragmentShaderInit() const const GLSLEditorPlugin::InitFile *GLSLEditorPlugin::fragmentShaderInit(int variant) const
{ {
// TODO: select the correct language variant if (variant & GLSL::Lexer::Variant_GLSL_120)
//return &m_glsl_120_frag; return &m_glsl_120_frag;
else
return &m_glsl_es_100_frag; 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; if (variant & GLSL::Lexer::Variant_GLSL_120)
return &m_glsl_120_vert;
else
return &m_glsl_es_100_vert; 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; if (variant & GLSL::Lexer::Variant_GLSL_120)
return &m_glsl_120_common;
else
return &m_glsl_es_100_common; return &m_glsl_es_100_common;
} }

View File

@@ -94,9 +94,9 @@ public:
~InitFile(); ~InitFile();
}; };
const InitFile *fragmentShaderInit() const; const InitFile *fragmentShaderInit(int variant) const;
const InitFile *vertexShaderInit() const; const InitFile *vertexShaderInit(int variant) const;
const InitFile *shaderInit() const; const InitFile *shaderInit(int variant) const;
private: private:
QByteArray glslFile(const QString &fileName); QByteArray glslFile(const QString &fileName);

View File

@@ -67,7 +67,6 @@ Core::GeneratedFiles GLSLFileWizard::generateFiles(const QWizard *w,
const QString path = wizardDialog->path(); const QString path = wizardDialog->path();
const QString name = wizardDialog->fileName(); const QString name = wizardDialog->fileName();
const QString mimeType = QLatin1String(Constants::GLSL_MIMETYPE);
const QString fileName = Core::BaseFileWizard::buildFileName(path, name, preferredSuffix(m_shaderType)); const QString fileName = Core::BaseFileWizard::buildFileName(path, name, preferredSuffix(m_shaderType));
Core::GeneratedFile file(fileName); Core::GeneratedFile file(fileName);
@@ -82,7 +81,7 @@ QString GLSLFileWizard::fileContents(const QString &, ShaderType shaderType) con
QTextStream str(&contents); QTextStream str(&contents);
switch (shaderType) { switch (shaderType) {
case GLSLFileWizard::VertexShader: case GLSLFileWizard::VertexShaderES:
str << QLatin1String("attribute highp vec4 qgl_Vertex;\n") str << QLatin1String("attribute highp vec4 qgl_Vertex;\n")
<< QLatin1String("attribute highp vec4 qgl_TexCoord0;\n") << QLatin1String("attribute highp vec4 qgl_TexCoord0;\n")
<< QLatin1String("uniform highp mat4 qgl_ModelViewProjectionMatrix;\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(" qTexCoord0 = qgl_TexCoord0;\n")
<< QLatin1String("}\n"); << QLatin1String("}\n");
break; break;
case GLSLFileWizard::FragmentShader: case GLSLFileWizard::FragmentShaderES:
str << QLatin1String("uniform sampler2D qgl_Texture0;\n") str << QLatin1String("uniform sampler2D qgl_Texture0;\n")
<< QLatin1String("varying highp vec4 qTexCoord0;\n") << QLatin1String("varying highp vec4 qTexCoord0;\n")
<< QLatin1String("\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(" gl_FragColor = texture2D(qgl_Texture0, qTexCoord0.st);\n")
<< QLatin1String("}\n"); << QLatin1String("}\n");
break; 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; default: break;
} }
@@ -124,9 +144,13 @@ QWizard *GLSLFileWizard::createWizardDialog(QWidget *parent, const QString &defa
QString GLSLFileWizard::preferredSuffix(ShaderType shaderType) const QString GLSLFileWizard::preferredSuffix(ShaderType shaderType) const
{ {
switch (shaderType) { switch (shaderType) {
case GLSLFileWizard::VertexShader: case GLSLFileWizard::VertexShaderES:
return QLatin1String("vsh");
case GLSLFileWizard::FragmentShaderES:
return QLatin1String("fsh");
case GLSLFileWizard::VertexShaderDesktop:
return QLatin1String("vert"); return QLatin1String("vert");
case GLSLFileWizard::FragmentShader: case GLSLFileWizard::FragmentShaderDesktop:
return QLatin1String("frag"); return QLatin1String("frag");
default: default:
return QLatin1String("glsl"); return QLatin1String("glsl");

View File

@@ -43,8 +43,10 @@ public:
enum ShaderType enum ShaderType
{ {
VertexShader, VertexShaderES,
FragmentShader FragmentShaderES,
VertexShaderDesktop,
FragmentShaderDesktop
}; };
explicit GLSLFileWizard(const BaseFileWizardParameters &parameters, explicit GLSLFileWizard(const BaseFileWizardParameters &parameters,