Commit 1bf87c67 authored by Roberto Raggi's avatar Roberto Raggi
Browse files

Initialize the shaders

parent 6b301687
...@@ -76,6 +76,7 @@ class OverloadSet; ...@@ -76,6 +76,7 @@ class OverloadSet;
class Namespace; class Namespace;
class AST; class AST;
class TranslationUnitAST;
template <typename T> class List; template <typename T> class List;
} }
......
...@@ -36,8 +36,8 @@ ...@@ -36,8 +36,8 @@
using namespace GLSL; using namespace GLSL;
Semantic::Semantic(Engine *engine) Semantic::Semantic()
: _engine(engine) : _engine(0)
, _scope(0) , _scope(0)
, _type(0) , _type(0)
{ {
...@@ -47,6 +47,13 @@ Semantic::~Semantic() ...@@ -47,6 +47,13 @@ Semantic::~Semantic()
{ {
} }
Engine *Semantic::switchEngine(Engine *engine)
{
Engine *previousEngine = _engine;
_engine = engine;
return previousEngine;
}
Scope *Semantic::switchScope(Scope *scope) Scope *Semantic::switchScope(Scope *scope)
{ {
Scope *previousScope = _scope; Scope *previousScope = _scope;
...@@ -82,9 +89,9 @@ void Semantic::declaration(DeclarationAST *ast) ...@@ -82,9 +89,9 @@ void Semantic::declaration(DeclarationAST *ast)
accept(ast); accept(ast);
} }
Scope *Semantic::translationUnit(TranslationUnitAST *ast) void Semantic::translationUnit(TranslationUnitAST *ast, Scope *globalScope, Engine *engine)
{ {
Namespace *globalScope = _engine->newNamespace(); Engine *previousEngine = switchEngine(engine);
Scope *previousScope = switchScope(globalScope); Scope *previousScope = switchScope(globalScope);
if (ast) { if (ast) {
for (List<DeclarationAST *> *it = ast->declarations; it; it = it->next) { for (List<DeclarationAST *> *it = ast->declarations; it; it = it->next) {
...@@ -92,7 +99,8 @@ Scope *Semantic::translationUnit(TranslationUnitAST *ast) ...@@ -92,7 +99,8 @@ Scope *Semantic::translationUnit(TranslationUnitAST *ast)
declaration(decl); declaration(decl);
} }
} }
return switchScope(previousScope); (void) switchScope(previousScope);
(void) switchEngine(previousEngine);
} }
Semantic::ExprResult Semantic::functionIdentifier(FunctionIdentifierAST *ast) Semantic::ExprResult Semantic::functionIdentifier(FunctionIdentifierAST *ast)
...@@ -106,7 +114,7 @@ Semantic::ExprResult Semantic::functionIdentifier(FunctionIdentifierAST *ast) ...@@ -106,7 +114,7 @@ Semantic::ExprResult Semantic::functionIdentifier(FunctionIdentifierAST *ast)
else else
_engine->error(ast->lineno, QString("`%1' cannot be used as a function").arg(*ast->name)); _engine->error(ast->lineno, QString("`%1' cannot be used as a function").arg(*ast->name));
} else { } else {
// ### _engine->error(ast->lineno, QString("`%1' was not declared in this scope").arg(*ast->name)); _engine->error(ast->lineno, QString("`%1' was not declared in this scope").arg(*ast->name));
} }
} else if (ast->type) { } else if (ast->type) {
const Type *ty = type(ast->type); const Type *ty = type(ast->type);
......
...@@ -36,7 +36,7 @@ namespace GLSL { ...@@ -36,7 +36,7 @@ namespace GLSL {
class GLSL_EXPORT Semantic: protected Visitor class GLSL_EXPORT Semantic: protected Visitor
{ {
public: public:
Semantic(Engine *engine); Semantic();
virtual ~Semantic(); virtual ~Semantic();
struct ExprResult { struct ExprResult {
...@@ -50,18 +50,20 @@ public: ...@@ -50,18 +50,20 @@ public:
bool isConstant; bool isConstant;
}; };
void translationUnit(TranslationUnitAST *ast, Scope *globalScope, Engine *engine);
protected:
Engine *switchEngine(Engine *engine);
Scope *switchScope(Scope *scope);
ExprResult expression(ExpressionAST *ast); ExprResult expression(ExpressionAST *ast);
void statement(StatementAST *ast); void statement(StatementAST *ast);
const Type *type(TypeAST *ast); const Type *type(TypeAST *ast);
void declaration(DeclarationAST *ast); void declaration(DeclarationAST *ast);
Scope *translationUnit(TranslationUnitAST *ast);
ExprResult functionIdentifier(FunctionIdentifierAST *ast); ExprResult functionIdentifier(FunctionIdentifierAST *ast);
Symbol *field(StructTypeAST::Field *ast); Symbol *field(StructTypeAST::Field *ast);
void parameterDeclaration(ParameterDeclarationAST *ast, Function *fun); void parameterDeclaration(ParameterDeclarationAST *ast, Function *fun);
protected:
Scope *switchScope(Scope *scope);
virtual bool visit(TranslationUnitAST *ast); virtual bool visit(TranslationUnitAST *ast);
virtual bool visit(FunctionIdentifierAST *ast); virtual bool visit(FunctionIdentifierAST *ast);
virtual bool visit(StructTypeAST::Field *ast); virtual bool visit(StructTypeAST::Field *ast);
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include <glsllexer.h> #include <glsllexer.h>
#include <glslastdump.h> #include <glslastdump.h>
#include <glslsemantic.h> #include <glslsemantic.h>
#include <glslsymbols.h>
#include <glsltypes.h>
#include <QtCore/QTextStream> #include <QtCore/QTextStream>
...@@ -82,8 +84,9 @@ int main(int argc, char *argv[]) ...@@ -82,8 +84,9 @@ int main(int argc, char *argv[])
ASTDump dump(qout); ASTDump dump(qout);
dump(ast); dump(ast);
Semantic sem(&engine); Semantic sem;
sem.translationUnit(ast); Scope *globalScope = engine.newNamespace();
sem.translationUnit(ast, globalScope, &engine);
delete source; delete source;
delete ast; delete ast;
......
...@@ -39,6 +39,7 @@ ...@@ -39,6 +39,7 @@
#include <glsl/glslparser.h> #include <glsl/glslparser.h>
#include <glsl/glslengine.h> #include <glsl/glslengine.h>
#include <glsl/glslsemantic.h> #include <glsl/glslsemantic.h>
#include <glsl/glslsymbols.h>
#include <coreplugin/actionmanager/actionmanager.h> #include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/actioncontainer.h> #include <coreplugin/actionmanager/actioncontainer.h>
...@@ -270,9 +271,14 @@ void GLSLTextEditor::updateDocumentNow() ...@@ -270,9 +271,14 @@ void GLSLTextEditor::updateDocumentNow()
Parser parser(&engine, preprocessedCode.constData(), preprocessedCode.size(), variant); Parser parser(&engine, preprocessedCode.constData(), preprocessedCode.size(), variant);
TranslationUnitAST *ast = parser.parse(); TranslationUnitAST *ast = parser.parse();
Semantic sem(&engine); GLSLEditorPlugin *plugin = GLSLEditorPlugin::instance();
Scope *globalScope = sem.translationUnit(ast);
Q_UNUSED(globalScope); Semantic sem;
Scope *globalScope = engine.newNamespace();
sem.translationUnit(plugin->shaderInit()->ast, globalScope, plugin->shaderInit()->engine);
sem.translationUnit(plugin->vertexShaderInit()->ast, globalScope, plugin->vertexShaderInit()->engine);
sem.translationUnit(plugin->fragmentShaderInit()->ast, globalScope, plugin->fragmentShaderInit()->engine);
sem.translationUnit(ast, globalScope, &engine);
QTextCharFormat errorFormat; QTextCharFormat errorFormat;
errorFormat.setUnderlineStyle(QTextCharFormat::WaveUnderline); errorFormat.setUnderlineStyle(QTextCharFormat::WaveUnderline);
......
...@@ -55,6 +55,10 @@ ...@@ -55,6 +55,10 @@
#include <texteditor/completionsupport.h> #include <texteditor/completionsupport.h>
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <glsl/glslengine.h>
#include <glsl/glslparser.h>
#include <glsl/glsllexer.h>
#include <QtCore/QtPlugin> #include <QtCore/QtPlugin>
#include <QtCore/QDebug> #include <QtCore/QDebug>
#include <QtCore/QSettings> #include <QtCore/QSettings>
...@@ -70,6 +74,11 @@ using namespace GLSLEditor::Constants; ...@@ -70,6 +74,11 @@ using namespace GLSLEditor::Constants;
GLSLEditorPlugin *GLSLEditorPlugin::m_instance = 0; GLSLEditorPlugin *GLSLEditorPlugin::m_instance = 0;
GLSLEditorPlugin::InitFile::~InitFile()
{
delete engine;
}
GLSLEditorPlugin::GLSLEditorPlugin() : GLSLEditorPlugin::GLSLEditorPlugin() :
m_editor(0), m_editor(0),
m_actionHandler(0) m_actionHandler(0)
...@@ -102,9 +111,10 @@ bool GLSLEditorPlugin::initialize(const QStringList & /*arguments*/, QString *er ...@@ -102,9 +111,10 @@ bool GLSLEditorPlugin::initialize(const QStringList & /*arguments*/, QString *er
if (!core->mimeDatabase()->addMimeTypes(QLatin1String(":/glsleditor/GLSLEditor.mimetypes.xml"), error_message)) if (!core->mimeDatabase()->addMimeTypes(QLatin1String(":/glsleditor/GLSLEditor.mimetypes.xml"), error_message))
return false; return false;
m_glsl_120_frag = glslFile(QLatin1String("glsl_120.frag")); parseGlslFile(QLatin1String("glsl_120.frag"), &m_glsl_120_frag);
m_glsl_120_vert = glslFile(QLatin1String("glsl_120.vert")); parseGlslFile(QLatin1String("glsl_120.vert"), &m_glsl_120_vert);
m_glsl_120_common = glslFile(QLatin1String("glsl_120_common.glsl")); parseGlslFile(QLatin1String("glsl_120_common.glsl"), &m_glsl_120_common);
// m_modelManager = new ModelManager(this); // m_modelManager = new ModelManager(this);
// addAutoReleasedObject(m_modelManager); // addAutoReleasedObject(m_modelManager);
...@@ -239,19 +249,31 @@ QByteArray GLSLEditorPlugin::glslFile(const QString &fileName) ...@@ -239,19 +249,31 @@ QByteArray GLSLEditorPlugin::glslFile(const QString &fileName)
return QByteArray(); return QByteArray();
} }
QByteArray GLSLEditorPlugin::fragmentShaderInit() const void GLSLEditorPlugin::parseGlslFile(const QString &fileName, InitFile *initFile)
{
const int variant = GLSL::Lexer::Variant_GLSL_Qt | // ### hardcoded
GLSL::Lexer::Variant_VertexShader |
GLSL::Lexer::Variant_FragmentShader;
const QByteArray code = glslFile(fileName);
initFile->engine = new GLSL::Engine();
GLSL::Parser parser(initFile->engine, code.constData(), code.size(), variant);
initFile->ast = parser.parse();
}
const GLSLEditorPlugin::InitFile *GLSLEditorPlugin::fragmentShaderInit() const
{ {
return m_glsl_120_frag; return &m_glsl_120_frag;
} }
QByteArray GLSLEditorPlugin::vertexShaderInit() const const GLSLEditorPlugin::InitFile *GLSLEditorPlugin::vertexShaderInit() const
{ {
return m_glsl_120_vert; return &m_glsl_120_vert;
} }
QByteArray GLSLEditorPlugin::shaderInit() const const GLSLEditorPlugin::InitFile *GLSLEditorPlugin::shaderInit() const
{ {
return m_glsl_120_common; return &m_glsl_120_common;
} }
Q_EXPORT_PLUGIN(GLSLEditorPlugin) Q_EXPORT_PLUGIN(GLSLEditorPlugin)
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
#include <extensionsystem/iplugin.h> #include <extensionsystem/iplugin.h>
#include <coreplugin/icontext.h> #include <coreplugin/icontext.h>
#include <QtCore/QPointer> #include <QtCore/QPointer>
#include <glsl/glsl.h>
QT_FORWARD_DECLARE_CLASS(QAction) QT_FORWARD_DECLARE_CLASS(QAction)
QT_FORWARD_DECLARE_CLASS(QTimer) QT_FORWARD_DECLARE_CLASS(QTimer)
...@@ -83,12 +84,23 @@ public: ...@@ -83,12 +84,23 @@ public:
void initializeEditor(GLSLEditor::GLSLTextEditor *editor); void initializeEditor(GLSLEditor::GLSLTextEditor *editor);
QByteArray fragmentShaderInit() const; struct InitFile {
QByteArray vertexShaderInit() const; GLSL::Engine *engine;
QByteArray shaderInit() const; GLSL::TranslationUnitAST *ast;
InitFile(GLSL::Engine *engine = 0, GLSL::TranslationUnitAST *ast = 0)
: engine(engine), ast(ast) {}
~InitFile();
};
const InitFile *fragmentShaderInit() const;
const InitFile *vertexShaderInit() const;
const InitFile *shaderInit() const;
private: private:
QByteArray glslFile(const QString &fileName); QByteArray glslFile(const QString &fileName);
void parseGlslFile(const QString &fileName, InitFile *initFile);
Core::Command *addToolAction(QAction *a, Core::ActionManager *am, Core::Context &context, const QString &name, Core::Command *addToolAction(QAction *a, Core::ActionManager *am, Core::Context &context, const QString &name,
Core::ActionContainer *c1, const QString &keySequence); Core::ActionContainer *c1, const QString &keySequence);
...@@ -99,9 +111,10 @@ private: ...@@ -99,9 +111,10 @@ private:
TextEditor::TextEditorActionHandler *m_actionHandler; TextEditor::TextEditorActionHandler *m_actionHandler;
QPointer<TextEditor::ITextEditable> m_currentTextEditable; QPointer<TextEditor::ITextEditable> m_currentTextEditable;
QByteArray m_glsl_120_frag;
QByteArray m_glsl_120_vert; InitFile m_glsl_120_frag;
QByteArray m_glsl_120_common; InitFile m_glsl_120_vert;
InitFile m_glsl_120_common;
}; };
} // namespace Internal } // namespace Internal
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment