1 #ifndef MSP_GL_SL_COMPILER_H_
2 #define MSP_GL_SL_COMPILER_H_
5 #include <msp/datafile/collection.h>
6 #include <msp/io/base.h>
36 RESOLVE_VARIABLES = 4,
37 RESOLVE_EXPRESSIONS = 8,
38 RESOLVE_FUNCTIONS = 16,
44 std::vector<std::string> imported_names;
45 bool compiled = false;
46 bool specialized = false;
47 std::map<std::string, int> spec_values;
50 /** Creates a compiler targeting a specific set of features. */
51 Compiler(const Features &);
59 /** Sets the source code to be compiled. Only builtin imports are
61 void set_source(const std::string &, const std::string & = "<string>");
63 /** Loads source code from an I/O object. If a collection is used, imports
64 can be fetched from it. */
65 void load_source(IO::Base &, DataFile::Collection * = 0, const std::string & = "<file>");
67 /** Loads source code from an I/O object. Only builtin imports are
69 void load_source(IO::Base &, const std::string &);
71 /** Specializes the shader. All specialization constants are considered
72 specialized, even if they do not appear in the map. */
73 void specialize(const std::map<std::string, int> &);
75 /** Compiles the shader. */
78 /** Returns combined GLSL source for all shader stages. The result is
79 suitable for feeding back to the compiler. */
80 std::string get_combined_glsl() const;
82 /** Returns a list of compiled stage types. */
83 std::vector<Stage::Type> get_stages() const;
85 /** Returns GLSL source for a single shader stage. The result is standard
86 GLSL suitable for OpenGL or an external GLSL compiler. */
87 std::string get_stage_glsl(Stage::Type) const;
89 /** Returns a combined SPIR-V binary for all shader stages. The result is
90 suitable for use with OpenGL or Vulkan. */
91 std::vector<std::uint32_t> get_combined_spirv() const;
93 /** Returns a map of vertex attribute locations. If the target GLSL version
94 supports interface layouts, the map is empty (locations are included in the
96 const std::map<std::string, unsigned> &get_vertex_attributes() const;
98 /** Returns a map of fragment output locations. If the target GLSL version
99 supports interface layouts, the map is empty (locations are included in the
101 const std::map<std::string, unsigned> &get_fragment_outputs() const;
103 /** Returns a map of texture bindings. If the target GLSL version supports
104 bindings, the map is empty (bindings are included in the GLSL source). */
105 const std::map<std::string, unsigned> &get_texture_bindings() const;
107 /** Returns a map of uniform block bindings. If the target GLSL version
108 supports bindings, the map is empty (bindings are included in the GLSL
110 const std::map<std::string, unsigned> &get_uniform_block_bindings() const;
112 unsigned get_n_clip_distances() const;
114 /** Returns the mapping of source indices to filenames. Can be used to
115 translate error messages. */
116 const SourceMap &get_source_map() const;
118 /** Returns a textual representation of the syntax tree for a shader stage.
119 Intended for debugging purposes. */
120 std::string get_stage_debug(Stage::Type) const;
122 /** Returns diagnostics from compilation. The output is intended to be
124 std::string get_diagnostics() const;
127 /** Appends a module to the target, processing any imports found in it. */
128 void append_module(const Module &, ModuleCache &);
130 /** Appends a single stage to the matching stage of the target. */
131 void append_stage(const Stage &);
133 /// Imports a module by name and appends it to the target. */
134 void import(ModuleCache &, const std::string &);
136 /** Generates any implicitly defines syntactic structures and resolves
138 void generate(Stage &);
141 bool resolve(Stage &, unsigned &, unsigned);
143 /** Resolves various references between nodes. Flags can be specified to
144 request resolving particular aspects. Resolving may ripple into other
145 aspects as necessary. */
146 void resolve(Stage &, unsigned = RESOLVE_ALL);
148 /** Runs validators on a stage. Diagnostic messages are recorded in the
149 stage for later inspection. */
150 void validate(Stage &);
152 /** Checks a stage's recorded diagnostics for errors. If any are found,
154 bool check_errors(Stage &);
156 static bool diagnostic_line_order(const Diagnostic &, const Diagnostic &);
158 /** Applies optimizations to a stage. The return value indicates which
159 stage should be optimized next. */
160 OptimizeResult optimize(Stage &);
162 /** Performs final adjustments on a stage after compilation. */
163 void finalize(Stage &, Mode);
165 static void inject_block(Block &, const Block &);