1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
/*
* Copyright 2018 Collabora Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* on the rights to use, copy, modify, merge, publish, distribute, sub
* license, and/or sell copies of the Software, and to permit persons to whom
* the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
* THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
* USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef SPIRV_BUILDER_H
#define SPIRV_BUILDER_H
#include "compiler/spirv/spirv.h"
#include "compiler/spirv/GLSL.std.450.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
struct hash_table;
struct spirv_buffer {
uint32_t *words;
size_t num_words, room;
};
struct spirv_builder {
struct spirv_buffer capabilities;
struct spirv_buffer imports;
struct spirv_buffer memory_model;
struct spirv_buffer entry_points;
struct spirv_buffer exec_modes;
struct spirv_buffer debug_names;
struct spirv_buffer decorations;
struct spirv_buffer types_const_defs;
struct hash_table *types;
struct hash_table *consts;
struct spirv_buffer instructions;
SpvId prev_id;
};
static inline SpvId
spirv_builder_new_id(struct spirv_builder *b)
{
return ++b->prev_id;
}
void
spirv_builder_emit_cap(struct spirv_builder *b, SpvCapability cap);
void
spirv_builder_emit_source(struct spirv_builder *b, SpvSourceLanguage lang,
uint32_t version);
void
spirv_builder_emit_mem_model(struct spirv_builder *b,
SpvAddressingModel addr_model,
SpvMemoryModel mem_model);
void
spirv_builder_emit_name(struct spirv_builder *b, SpvId target,
const char *name);
void
spirv_builder_emit_decoration(struct spirv_builder *b, SpvId target,
SpvDecoration decoration);
void
spirv_builder_emit_location(struct spirv_builder *b, SpvId target,
uint32_t location);
void
spirv_builder_emit_component(struct spirv_builder *b, SpvId target,
uint32_t component);
void
spirv_builder_emit_builtin(struct spirv_builder *b, SpvId target,
SpvBuiltIn builtin);
void
spirv_builder_emit_descriptor_set(struct spirv_builder *b, SpvId target,
uint32_t descriptor_set);
void
spirv_builder_emit_binding(struct spirv_builder *b, SpvId target,
uint32_t binding);
void
spirv_builder_emit_array_stride(struct spirv_builder *b, SpvId target,
uint32_t stride);
void
spirv_builder_emit_member_offset(struct spirv_builder *b, SpvId target,
uint32_t member, uint32_t offset);
void
spirv_builder_emit_entry_point(struct spirv_builder *b,
SpvExecutionModel exec_model, SpvId entry_point,
const char *name, const SpvId interfaces[],
size_t num_interfaces);
void
spirv_builder_emit_exec_mode(struct spirv_builder *b, SpvId entry_point,
SpvExecutionMode exec_mode);
void
spirv_builder_function(struct spirv_builder *b, SpvId result,
SpvId return_type,
SpvFunctionControlMask function_control,
SpvId function_type);
void
spirv_builder_function_end(struct spirv_builder *b);
void
spirv_builder_label(struct spirv_builder *b, SpvId label);
void
spirv_builder_return(struct spirv_builder *b);
SpvId
spirv_builder_emit_undef(struct spirv_builder *b, SpvId result_type);
SpvId
spirv_builder_emit_load(struct spirv_builder *b, SpvId result_type,
SpvId pointer);
void
spirv_builder_emit_store(struct spirv_builder *b, SpvId pointer, SpvId object);
SpvId
spirv_builder_emit_access_chain(struct spirv_builder *b, SpvId result_type,
SpvId base, const SpvId indexes[],
size_t num_indexes);
SpvId
spirv_builder_emit_unop(struct spirv_builder *b, SpvOp op, SpvId result_type,
SpvId operand);
SpvId
spirv_builder_emit_binop(struct spirv_builder *b, SpvOp op, SpvId result_type,
SpvId operand0, SpvId operand1);
SpvId
spirv_builder_emit_triop(struct spirv_builder *b, SpvOp op, SpvId result_type,
SpvId operand0, SpvId operand1, SpvId operand2);
SpvId
spirv_builder_emit_composite_extract(struct spirv_builder *b, SpvId result_type,
SpvId composite, const uint32_t indexes[],
size_t num_indexes);
SpvId
spirv_builder_emit_composite_construct(struct spirv_builder *b,
SpvId result_type,
const SpvId constituents[],
size_t num_constituents);
SpvId
spirv_builder_emit_vector_shuffle(struct spirv_builder *b, SpvId result_type,
SpvId vector_1, SpvId vector_2,
const uint32_t components[],
size_t num_components);
void
spirv_builder_emit_branch(struct spirv_builder *b, SpvId label);
void
spirv_builder_emit_selection_merge(struct spirv_builder *b, SpvId merge_block,
SpvSelectionControlMask selection_control);
void
spirv_builder_loop_merge(struct spirv_builder *b, SpvId merge_block,
SpvId cont_target, SpvLoopControlMask loop_control);
void
spirv_builder_emit_branch_conditional(struct spirv_builder *b, SpvId condition,
SpvId true_label, SpvId false_label);
SpvId
spirv_builder_emit_phi(struct spirv_builder *b, SpvId result_type,
size_t num_vars, size_t *position);
void
spirv_builder_set_phi_operand(struct spirv_builder *b, size_t position,
size_t index, SpvId variable, SpvId parent);
void
spirv_builder_emit_kill(struct spirv_builder *b);
SpvId
spirv_builder_emit_image_sample(struct spirv_builder *b,
SpvId result_type,
SpvId sampled_image,
SpvId coordinate,
bool proj,
SpvId lod,
SpvId bias,
SpvId dref);
SpvId
spirv_builder_emit_ext_inst(struct spirv_builder *b, SpvId result_type,
SpvId set, uint32_t instruction,
const SpvId args[], size_t num_args);
SpvId
spirv_builder_type_void(struct spirv_builder *b);
SpvId
spirv_builder_type_bool(struct spirv_builder *b);
SpvId
spirv_builder_type_int(struct spirv_builder *b, unsigned width);
SpvId
spirv_builder_type_uint(struct spirv_builder *b, unsigned width);
SpvId
spirv_builder_type_float(struct spirv_builder *b, unsigned width);
SpvId
spirv_builder_type_image(struct spirv_builder *b, SpvId sampled_type,
SpvDim dim, bool depth, bool arrayed, bool ms,
unsigned sampled, SpvImageFormat image_format);
SpvId
spirv_builder_type_sampled_image(struct spirv_builder *b, SpvId image_type);
SpvId
spirv_builder_type_pointer(struct spirv_builder *b,
SpvStorageClass storage_class, SpvId type);
SpvId
spirv_builder_type_vector(struct spirv_builder *b, SpvId component_type,
unsigned component_count);
SpvId
spirv_builder_type_array(struct spirv_builder *b, SpvId component_type,
SpvId length);
SpvId
spirv_builder_type_struct(struct spirv_builder *b, const SpvId member_types[],
size_t num_member_types);
SpvId
spirv_builder_type_function(struct spirv_builder *b, SpvId return_type,
const SpvId parameter_types[],
size_t num_parameter_types);
SpvId
spirv_builder_const_bool(struct spirv_builder *b, bool val);
SpvId
spirv_builder_const_int(struct spirv_builder *b, int width, int32_t val);
SpvId
spirv_builder_const_uint(struct spirv_builder *b, int width, uint32_t val);
SpvId
spirv_builder_const_float(struct spirv_builder *b, int width, float val);
SpvId
spirv_builder_const_composite(struct spirv_builder *b, SpvId result_type,
const SpvId constituents[],
size_t num_constituents);
SpvId
spirv_builder_emit_var(struct spirv_builder *b, SpvId type,
SpvStorageClass storage_class);
SpvId
spirv_builder_import(struct spirv_builder *b, const char *name);
size_t
spirv_builder_get_num_words(struct spirv_builder *b);
size_t
spirv_builder_get_words(struct spirv_builder *b, uint32_t *words,
size_t num_words);
#endif
|