From: Mikko Rasa Date: Sun, 21 Feb 2021 23:12:25 +0000 (+0200) Subject: Add a standalone GLSL compiler frontend X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=b30777959d8e2ab2caf489e32f40390f60a75fcb;p=libs%2Fgl.git Add a standalone GLSL compiler frontend This makes debugging the compiler easier and will eventually allow shaders to be compiled into SPIR-V during build. --- diff --git a/Build b/Build index 19831c3e..fb571b0c 100644 --- a/Build +++ b/Build @@ -94,6 +94,12 @@ package "mspgl" use "mspgl"; }; + program "glslcompiler" + { + source "tools/glslcompiler.cpp"; + use "mspgl"; + }; + source_tarball { source "License.txt"; diff --git a/source/glsl/compiler.cpp b/source/glsl/compiler.cpp index 4a7a19e2..ca6c5805 100644 --- a/source/glsl/compiler.cpp +++ b/source/glsl/compiler.cpp @@ -3,6 +3,7 @@ #include #include "compatibility.h" #include "compiler.h" +#include "debug.h" #include "error.h" #include "generate.h" #include "optimize.h" @@ -132,6 +133,14 @@ const SourceMap &Compiler::get_source_map() const return module->source_map; } +string Compiler::get_stage_debug(Stage::Type stage_type) const +{ + for(list::iterator i=module->stages.begin(); i!=module->stages.end(); ++i) + if(i->type==stage_type) + return DumpTree().apply(*i); + throw key_error(Stage::get_stage_name(stage_type)); +} + void Compiler::append_module(Module &mod, DataFile::Collection *res) { module->source_map.merge_from(mod.source_map); diff --git a/source/glsl/compiler.h b/source/glsl/compiler.h index 8340aa6d..4eaff213 100644 --- a/source/glsl/compiler.h +++ b/source/glsl/compiler.h @@ -44,8 +44,10 @@ public: const std::map &get_vertex_attributes() const; const std::map &get_fragment_outputs() const; const SourceMap &get_source_map() const; -private: + std::string get_stage_debug(Stage::Type) const; + +private: void append_module(Module &, DataFile::Collection *); void append_stage(Stage &); void import(DataFile::Collection *, const std::string &); diff --git a/source/glsl/features.cpp b/source/glsl/features.cpp index d1c8101d..11b6c942 100644 --- a/source/glsl/features.cpp +++ b/source/glsl/features.cpp @@ -32,6 +32,19 @@ Features Features::from_context() return features; } +Features Features::all() +{ + Features features; + features.gl_api = OPENGL; + features.glsl_version = Version(4, 60); + features.arb_explicit_attrib_location = true; + features.arb_gpu_shader5 = true; + features.arb_uniform_buffer_object = true; + features.ext_gpu_shader4 = true; + features.ext_texture_array = true; + return features; +} + } // namespace SL } // namespace GL } // namespace Msp diff --git a/source/glsl/features.h b/source/glsl/features.h index 8f7a3e7c..8d32f8be 100644 --- a/source/glsl/features.h +++ b/source/glsl/features.h @@ -20,6 +20,7 @@ struct Features Features(); static Features from_context(); + static Features all(); }; } // namespace SL diff --git a/tools/glslcompiler.cpp b/tools/glslcompiler.cpp new file mode 100644 index 00000000..74b37ef6 --- /dev/null +++ b/tools/glslcompiler.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +#include + +class GlslCompiler: public Msp::RegisteredApplication +{ +private: + std::string source_fn; + bool parse_only; + bool combined; + Msp::GL::SL::Stage::Type stage; + bool dump_ast; + +public: + GlslCompiler(int, char **); + + virtual int main(); +}; + +using namespace std; +using namespace Msp; + +GlslCompiler::GlslCompiler(int argc, char **argv): + parse_only(false), + combined(false), + stage(GL::SL::Stage::SHARED), + dump_ast(false) +{ + string stage_str; + + GetOpt getopt; + getopt.add_option('c', "combined", combined, GetOpt::NO_ARG).set_help("Output combined GLSL"); + getopt.add_option('a', "dump-ast", dump_ast, GetOpt::NO_ARG).set_help("Dump AST for debugging"); + getopt.add_option('p', "parse_only", parse_only, GetOpt::NO_ARG).set_help("Only parse the loaded source, don't compile"); + getopt.add_option('s', "stage", stage_str, GetOpt::REQUIRED_ARG).set_help("Output GLSL for STAGE", "STAGE"); + getopt.add_argument("source", source_fn, GetOpt::REQUIRED_ARG).set_help("GLSL file to compile"); + getopt(argc, argv); + + if(stage_str=="vertex") + stage = GL::SL::Stage::VERTEX; + else if(stage_str=="geometry") + stage = GL::SL::Stage::GEOMETRY; + else if(stage_str=="fragment") + stage = GL::SL::Stage::FRAGMENT; + else if(!dump_ast) + combined = true; +} + +int GlslCompiler::main() +{ + GL::SL::Compiler compiler(GL::SL::Features::all()); + IO::File file(source_fn); + compiler.load_source(file, source_fn); + if(!parse_only) + compiler.compile(GL::SL::Compiler::PROGRAM); + + if(dump_ast) + { + vector stages = compiler.get_stages(); + for(vector::const_iterator i=stages.begin(); i!=stages.end(); ++i) + IO::print("%s\n", compiler.get_stage_debug(*i)); + } + + if(combined) + IO::print("%s\n", compiler.get_combined_glsl()); + else if(stage!=GL::SL::Stage::SHARED) + IO::print("%s\n", compiler.get_stage_glsl(stage)); + + return 0; +}