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 using features from the current OpenGL context. */
53 /** Creates a compiler targeting a specific set of features. */
54 Compiler(const Features &);
62 /** Sets the source code to be compiled. Only builtin imports are
64 void set_source(const std::string &, const std::string & = "<string>");
66 /** Loads source code from an I/O object. If a collection is used, imports
67 can be fetched from it. */
68 void load_source(IO::Base &, DataFile::Collection * = 0, const std::string & = "<file>");
70 /** Loads source code from an I/O object. Only builtin imports are
72 void load_source(IO::Base &, const std::string &);
74 /** Specializes the shader. All specialization constants are considered
75 specialized, even if they do not appear in the map. */
76 void specialize(const std::map<std::string, int> &);
78 /** Compiles the shader. */
81 /** Returns combined GLSL source for all shader stages. The result is
82 suitable for feeding back to the compiler. */
83 std::string get_combined_glsl() const;
85 /** Returns a list of compiled stage types. */
86 std::vector<Stage::Type> get_stages() const;
88 /** Returns GLSL source for a single shader stage. The result is standard
89 GLSL suitable for OpenGL or an external GLSL compiler. */
90 std::string get_stage_glsl(Stage::Type) const;
92 /** Returns a combined SPIR-V binary for all shader stages. The result is
93 suitable for use with OpenGL or Vulkan. */
94 std::vector<std::uint32_t> get_combined_spirv() const;
96 /** Returns a map of vertex attribute locations. If the target GLSL version
97 supports interface layouts, the map is empty (locations are included in the
99 const std::map<std::string, unsigned> &get_vertex_attributes() const;
101 /** Returns a map of fragment output locations. If the target GLSL version
102 supports interface layouts, the map is empty (locations are included in the
104 const std::map<std::string, unsigned> &get_fragment_outputs() const;
106 /** Returns a map of texture bindings. If the target GLSL version supports
107 bindings, the map is empty (bindings are included in the GLSL source). */
108 const std::map<std::string, unsigned> &get_texture_bindings() const;
110 /** Returns a map of uniform block bindings. If the target GLSL version
111 supports bindings, the map is empty (bindings are included in the GLSL
113 const std::map<std::string, unsigned> &get_uniform_block_bindings() const;
115 unsigned get_n_clip_distances() const;
117 /** Returns the mapping of source indices to filenames. Can be used to
118 translate error messages. */
119 const SourceMap &get_source_map() const;
121 /** Returns a textual representation of the syntax tree for a shader stage.
122 Intended for debugging purposes. */
123 std::string get_stage_debug(Stage::Type) const;
125 /** Returns diagnostics from compilation. The output is intended to be
127 std::string get_diagnostics() const;
130 /** Appends a module to the target, processing any imports found in it. */
131 void append_module(const Module &, ModuleCache &);
133 /** Appends a single stage to the matching stage of the target. */
134 void append_stage(const Stage &);
136 /// Imports a module by name and appends it to the target. */
137 void import(ModuleCache &, const std::string &);
139 /** Generates any implicitly defines syntactic structures and resolves
141 void generate(Stage &);
144 bool resolve(Stage &, unsigned &, unsigned);
146 /** Resolves various references between nodes. Flags can be specified to
147 request resolving particular aspects. Resolving may ripple into other
148 aspects as necessary. */
149 void resolve(Stage &, unsigned = RESOLVE_ALL);
151 /** Runs validators on a stage. Diagnostic messages are recorded in the
152 stage for later inspection. */
153 void validate(Stage &);
155 /** Checks a stage's recorded diagnostics for errors. If any are found,
157 bool check_errors(Stage &);
159 static bool diagnostic_line_order(const Diagnostic &, const Diagnostic &);
161 /** Applies optimizations to a stage. The return value indicates which
162 stage should be optimized next. */
163 OptimizeResult optimize(Stage &);
165 /** Performs final adjustments on a stage after compilation. */
166 void finalize(Stage &, Mode);
168 static void inject_block(Block &, const Block &);