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;
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 /** Returns the mapping of source indices to filenames. Can be used to
116 translate error messages. */
117 const SourceMap &get_source_map() const;
119 /** Returns a textual representation of the syntax tree for a shader stage.
120 Intended for debugging purposes. */
121 std::string get_stage_debug(Stage::Type) const;
123 /** Returns diagnostics from compilation. The output is intended to be
125 std::string get_diagnostics() const;
128 /** Appends a module to the target, processing any imports found in it. */
129 void append_module(const Module &, ModuleCache &);
131 /** Appends a single stage to the matching stage of the target. */
132 void append_stage(const Stage &);
134 /// Imports a module by name and appends it to the target. */
135 void import(ModuleCache &, const std::string &);
137 /** Generates any implicitly defines syntactic structures and resolves
139 void generate(Stage &);
142 bool resolve(Stage &, unsigned &, unsigned);
144 /** Resolves various references between nodes. Flags can be specified to
145 request resolving particular aspects. Resolving may ripple into other
146 aspects as necessary. */
147 void resolve(Stage &, unsigned = RESOLVE_ALL);
149 /** Runs validators on a stage. Diagnostic messages are recorded in the
150 stage for later inspection. */
151 void validate(Stage &);
153 /** Checks a stage's recorded diagnostics for errors. If any are found,
155 bool check_errors(Stage &);
157 static bool diagnostic_line_order(const Diagnostic &, const Diagnostic &);
159 /** Applies optimizations to a stage. The return value indicates which
160 stage should be optimized next. */
161 OptimizeResult optimize(Stage &);
163 /** Performs final adjustments on a stage after compilation. */
164 void finalize(Stage &, Mode);
166 static void inject_block(Block &, const Block &);