]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/spirv.h
Reset staging buffers in TransferQueue once the transfers are done
[libs/gl.git] / source / glsl / spirv.h
1 #ifndef MSP_GL_SL_SPIRV_H_
2 #define MSP_GL_SL_SPIRV_H_
3
4 #include <map>
5 #include <string>
6 #include <vector>
7 #include "spirvconstants.h"
8 #include "spirvwriter.h"
9 #include "visitor.h"
10
11 namespace Msp {
12 namespace GL {
13 namespace SL {
14
15 /** Creates SPIR-V binary from a module. */
16 class SpirVGenerator: private NodeVisitor
17 {
18 private:
19         typedef SpirVCapability Capability;
20         typedef SpirVStorageClass StorageClass;
21         typedef SpirVOpcode Opcode;
22         typedef SpirVDecoration Decoration;
23         typedef SpirVBuiltin BuiltinSemantic;
24         typedef SpirVContent::Word Word;
25         typedef SpirVWriter::Id Id;
26
27         struct BuiltinFunctionInfo
28         {
29                 char function[22];
30                 char arg_types[5];
31                 char extension[13];
32                 Word opcode;
33                 std::uint8_t arg_order[4];
34                 Word capability;
35                 void (SpirVGenerator::*handler)(FunctionCall &, const std::vector<Id> &);
36         };
37
38         struct Declaration
39         {
40                 Id id;
41                 Id type_id;
42
43                 Declaration(Id i, Id t): id(i), type_id(t) { }
44         };
45
46         struct TypeKey
47         {
48                 Id type_id;
49                 unsigned detail;
50
51                 TypeKey(Id i, unsigned d): type_id(i), detail(d) { }
52                 TypeKey(BasicTypeDeclaration::Kind, bool);
53
54                 bool operator<(const TypeKey &) const;
55         };
56
57         struct ConstantKey
58         {
59                 Id type_id;
60                 union
61                 {
62                         int int_value;
63                         float float_value;
64                 };
65
66                 ConstantKey(Id t, int i): type_id(t), int_value(i) { }
67                 ConstantKey(Id t, unsigned u): type_id(t), int_value(u) { }
68                 ConstantKey(Id t, float f): type_id(t), float_value(f) { }
69
70                 bool operator<(const ConstantKey &) const;
71         };
72
73         Features features;
74         Stage *stage = 0;
75         FunctionDeclaration *current_function = 0;
76         std::vector<const InterfaceLayout *> interface_layouts;
77         SpirVContent content;
78         SpirVWriter writer;
79         std::set<Capability> used_capabilities;
80         std::map<std::string, Id> imported_extension_ids;
81         std::map<Node *, Declaration> declared_ids;
82         std::map<std::string, Id> declared_uniform_ids;
83         std::map<unsigned, Id> declared_spec_ids;
84         std::map<TypeKey, Id> standard_type_ids;
85         std::map<Id, Id> image_type_ids;
86         std::map<TypeKey, Id> array_type_ids;
87         std::map<TypeKey, Id> pointer_type_ids;
88         std::map<std::string, Id> function_type_ids;
89         std::map<ConstantKey, Id> constant_ids;
90         std::map<const VariableDeclaration *, Id> variable_load_ids;
91         Id next_id = 1;
92         Id r_expression_result_id = 0;
93         bool r_constant_result = false;
94         bool constant_expression = false;
95         bool spec_constant = false;
96         bool reachable = false;
97         bool composite_access = false;
98         Id r_composite_base_id = 0;
99         Node *r_composite_base = 0;
100         std::vector<unsigned> r_composite_chain;
101         Id assignment_source_id = 0;
102         Id loop_merge_block_id = 0;
103         Id loop_continue_target_id = 0;
104
105         static const BuiltinFunctionInfo builtin_functions[];
106
107 public:
108         SpirVGenerator();
109
110         void apply(Module &, const Features &);
111         const std::vector<Word> &get_code() const { return content.code; }
112
113 private:
114         static StorageClass get_interface_storage(const std::string &, bool);
115         static SpirVBuiltin get_builtin_semantic(const std::string &);
116         void use_capability(Capability);
117         Id import_extension(const std::string &);
118         Id get_id(Node &) const;
119         Id allocate_id(Node &, Id);
120         Id allocate_forward_id(Node &);
121         Id write_constant(Id, Word, bool);
122         static ConstantKey get_constant_key(Id, const Variant &value);
123         Id get_constant_id(Id, const Variant &value);
124         Id get_vector_constant_id(Id, unsigned, Id);
125         Id get_standard_type_id(BasicTypeDeclaration::Kind, unsigned, bool = true);
126         bool is_scalar_type(Id, BasicTypeDeclaration::Kind) const;
127         Id get_array_type_id(TypeDeclaration &, Id, bool);
128         Id get_pointer_type_id(Id, StorageClass);
129         Id get_variable_type_id(const VariableDeclaration &);
130         Id get_load_id(VariableDeclaration &);
131         void prune_loads(Id);
132         Id begin_expression(Opcode, Id, unsigned = 0);
133         void end_expression(Opcode);
134         Id write_expression(Opcode, Id, Id);
135         Id write_expression(Opcode, Id, Id, Id);
136         void write_deconstruct(Id, Id, Id *, unsigned);
137         Id write_construct(Id, const Id *, unsigned);
138
139         virtual void visit(Block &);
140         virtual void visit(Literal &);
141         virtual void visit(VariableReference &);
142         void generate_composite_access(TypeDeclaration &);
143         void visit_composite(Expression &, unsigned, TypeDeclaration &);
144         void visit_isolated(Expression &);
145         virtual void visit(MemberAccess &);
146         virtual void visit(Swizzle &);
147         virtual void visit(UnaryExpression &);
148         virtual void visit(BinaryExpression &);
149         virtual void visit(Assignment &);
150         virtual void visit(TernaryExpression &);
151         virtual void visit(FunctionCall &);
152         void visit_constructor(FunctionCall &, const std::vector<Id> &, bool);
153         void visit_builtin_matrix_comp_mult(FunctionCall &, const std::vector<Id> &);
154         void visit_builtin_texture_query(FunctionCall &, const std::vector<Id> &);
155         void visit_builtin_texture(FunctionCall &, const std::vector<Id> &);
156         void visit_builtin_texel_fetch(FunctionCall &, const std::vector<Id> &);
157         void visit_builtin_interpolate(FunctionCall &, const std::vector<Id> &);
158         virtual void visit(ExpressionStatement &);
159         virtual void visit(InterfaceLayout &);
160         bool check_duplicate_type(TypeDeclaration &);
161         bool check_standard_type(BasicTypeDeclaration &);
162         virtual void visit(BasicTypeDeclaration &);
163         virtual void visit(ImageTypeDeclaration &);
164         virtual void visit(StructDeclaration &);
165         virtual void visit(VariableDeclaration &);
166         void visit_entry_point(FunctionDeclaration &, Id);
167         virtual void visit(FunctionDeclaration &);
168         virtual void visit(Conditional &);
169         virtual void visit(Iteration &);
170         virtual void visit(Return &);
171         virtual void visit(Jump &);
172 };
173
174 } // namespace SL
175 } // namespace GL
176 } // namespace Msp
177
178 #endif