1 /*
2 * Copyright 2018 Collabora Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24 #ifndef SPIRV_BUILDER_H
25 #define SPIRV_BUILDER_H
26
27 #include "compiler/spirv/spirv.h"
28 #include "compiler/spirv/GLSL.std.450.h"
29
30 #include <stdbool.h>
31 #include <stdint.h>
32 #include <stdlib.h>
33
34 struct hash_table;
35 struct set;
36
37 struct spirv_buffer {
38 uint32_t *words;
39 size_t num_words, room;
40 };
41
42 struct spirv_builder {
43 void *mem_ctx;
44
45 struct set *caps;
46
47 struct spirv_buffer extensions;
48 struct spirv_buffer imports;
49 struct spirv_buffer memory_model;
50 struct spirv_buffer entry_points;
51 struct spirv_buffer exec_modes;
52 struct spirv_buffer debug_names;
53 struct spirv_buffer decorations;
54
55 struct spirv_buffer types_const_defs;
56 struct hash_table *types;
57 struct hash_table *consts;
58
59 struct spirv_buffer instructions;
60 SpvId prev_id;
61 };
62
63 static inline SpvId
spirv_builder_new_id(struct spirv_builder *b)64 spirv_builder_new_id(struct spirv_builder *b)
65 {
66 return ++b->prev_id;
67 }
68
69 void
70 spirv_builder_emit_cap(struct spirv_builder *b, SpvCapability cap);
71
72 void
73 spirv_builder_emit_extension(struct spirv_builder *b, const char *ext);
74
75 void
76 spirv_builder_emit_source(struct spirv_builder *b, SpvSourceLanguage lang,
77 uint32_t version);
78
79 void
80 spirv_builder_emit_mem_model(struct spirv_builder *b,
81 SpvAddressingModel addr_model,
82 SpvMemoryModel mem_model);
83
84 void
85 spirv_builder_emit_name(struct spirv_builder *b, SpvId target,
86 const char *name);
87
88 void
89 spirv_builder_emit_decoration(struct spirv_builder *b, SpvId target,
90 SpvDecoration decoration);
91
92 void
93 spirv_builder_emit_input_attachment_index(struct spirv_builder *b, SpvId target, uint32_t id);
94
95 void
96 spirv_builder_emit_specid(struct spirv_builder *b, SpvId target, uint32_t id);
97
98 void
99 spirv_builder_emit_location(struct spirv_builder *b, SpvId target,
100 uint32_t location);
101
102 void
103 spirv_builder_emit_component(struct spirv_builder *b, SpvId target,
104 uint32_t component);
105
106 void
107 spirv_builder_emit_builtin(struct spirv_builder *b, SpvId target,
108 SpvBuiltIn builtin);
109
110 void
111 spirv_builder_emit_index(struct spirv_builder *b, SpvId target, int index);
112
113 void
114 spirv_builder_emit_stream(struct spirv_builder *b, SpvId target, int stream);
115
116 void
117 spirv_builder_emit_descriptor_set(struct spirv_builder *b, SpvId target,
118 uint32_t descriptor_set);
119
120 void
121 spirv_builder_emit_binding(struct spirv_builder *b, SpvId target,
122 uint32_t binding);
123
124 void
125 spirv_builder_emit_array_stride(struct spirv_builder *b, SpvId target,
126 uint32_t stride);
127
128 void
129 spirv_builder_emit_offset(struct spirv_builder *b, SpvId target,
130 uint32_t offset);
131
132 void
133 spirv_builder_emit_xfb_buffer(struct spirv_builder *b, SpvId target,
134 uint32_t buffer);
135
136 void
137 spirv_builder_emit_xfb_stride(struct spirv_builder *b, SpvId target,
138 uint32_t stride);
139
140 void
141 spirv_builder_emit_member_offset(struct spirv_builder *b, SpvId target,
142 uint32_t member, uint32_t offset);
143
144 void
145 spirv_builder_emit_entry_point(struct spirv_builder *b,
146 SpvExecutionModel exec_model, SpvId entry_point,
147 const char *name, const SpvId interfaces[],
148 size_t num_interfaces);
149 uint32_t
150 spirv_builder_emit_exec_mode_literal(struct spirv_builder *b, SpvId entry_point,
151 SpvExecutionMode exec_mode, uint32_t param);
152 void
153 spirv_builder_emit_exec_mode_literal3(struct spirv_builder *b, SpvId entry_point,
154 SpvExecutionMode exec_mode, uint32_t param[3]);
155 void
156 spirv_builder_emit_exec_mode(struct spirv_builder *b, SpvId entry_point,
157 SpvExecutionMode exec_mode);
158
159 void
160 spirv_builder_function(struct spirv_builder *b, SpvId result,
161 SpvId return_type,
162 SpvFunctionControlMask function_control,
163 SpvId function_type);
164
165 void
166 spirv_builder_function_end(struct spirv_builder *b);
167
168 void
169 spirv_builder_label(struct spirv_builder *b, SpvId label);
170
171 void
172 spirv_builder_return(struct spirv_builder *b);
173
174 SpvId
175 spirv_builder_emit_undef(struct spirv_builder *b, SpvId result_type);
176
177 SpvId
178 spirv_builder_emit_load(struct spirv_builder *b, SpvId result_type,
179 SpvId pointer);
180
181 void
182 spirv_builder_emit_atomic_store(struct spirv_builder *b, SpvId pointer, SpvScope scope,
183 SpvMemorySemanticsMask semantics, SpvId object);
184
185 void
186 spirv_builder_emit_store(struct spirv_builder *b, SpvId pointer, SpvId object);
187
188 SpvId
189 spirv_builder_emit_access_chain(struct spirv_builder *b, SpvId result_type,
190 SpvId base, const SpvId indexes[],
191 size_t num_indexes);
192
193 void
194 spirv_builder_emit_interlock(struct spirv_builder *b, bool end);
195
196 SpvId
197 spirv_builder_emit_unop_const(struct spirv_builder *b, SpvOp op, SpvId result_type, uint64_t operand);
198
199 SpvId
200 spirv_builder_emit_unop(struct spirv_builder *b, SpvOp op, SpvId result_type,
201 SpvId operand);
202
203 SpvId
204 spirv_builder_emit_binop(struct spirv_builder *b, SpvOp op, SpvId result_type,
205 SpvId operand0, SpvId operand1);
206
207 SpvId
208 spirv_builder_emit_triop(struct spirv_builder *b, SpvOp op, SpvId result_type,
209 SpvId operand0, SpvId operand1, SpvId operand2);
210
211 SpvId
212 spirv_builder_emit_quadop(struct spirv_builder *b, SpvOp op, SpvId result_type,
213 SpvId operand0, SpvId operand1, SpvId operand2, SpvId operand3);
214
215 SpvId
216 spirv_builder_emit_hexop(struct spirv_builder *b, SpvOp op, SpvId result_type,
217 SpvId operand0, SpvId operand1, SpvId operand2, SpvId operand3,
218 SpvId operand4, SpvId operand5);
219
220 SpvId
221 spirv_builder_emit_composite_extract(struct spirv_builder *b, SpvId result_type,
222 SpvId composite, const uint32_t indexes[],
223 size_t num_indexes);
224
225 SpvId
226 spirv_builder_emit_composite_construct(struct spirv_builder *b,
227 SpvId result_type,
228 const SpvId constituents[],
229 size_t num_constituents);
230
231 SpvId
232 spirv_builder_emit_vector_shuffle(struct spirv_builder *b, SpvId result_type,
233 SpvId vector_1, SpvId vector_2,
234 const uint32_t components[],
235 size_t num_components);
236 SpvId
237 spirv_builder_emit_vector_extract(struct spirv_builder *b, SpvId result_type,
238 SpvId vector_1,
239 uint32_t component);
240 SpvId
241 spirv_builder_emit_vector_insert(struct spirv_builder *b, SpvId result_type,
242 SpvId vector_1,
243 SpvId component,
244 uint32_t index);
245 void
246 spirv_builder_emit_branch(struct spirv_builder *b, SpvId label);
247
248 void
249 spirv_builder_emit_selection_merge(struct spirv_builder *b, SpvId merge_block,
250 SpvSelectionControlMask selection_control);
251
252 void
253 spirv_builder_loop_merge(struct spirv_builder *b, SpvId merge_block,
254 SpvId cont_target, SpvLoopControlMask loop_control);
255
256 void
257 spirv_builder_emit_branch_conditional(struct spirv_builder *b, SpvId condition,
258 SpvId true_label, SpvId false_label);
259
260 SpvId
261 spirv_builder_emit_phi(struct spirv_builder *b, SpvId result_type,
262 size_t num_vars, size_t *position);
263
264 void
265 spirv_builder_set_phi_operand(struct spirv_builder *b, size_t position,
266 size_t index, SpvId variable, SpvId parent);
267
268 void
269 spirv_builder_emit_kill(struct spirv_builder *b);
270
271 SpvId
272 spirv_builder_emit_vote(struct spirv_builder *b, SpvOp op, SpvId src);
273
274 SpvId
275 spirv_builder_emit_image_sample(struct spirv_builder *b,
276 SpvId result_type,
277 SpvId sampled_image,
278 SpvId coordinate,
279 bool proj,
280 SpvId lod,
281 SpvId bias,
282 SpvId dref,
283 SpvId dx,
284 SpvId dy,
285 SpvId const_offset,
286 SpvId offset,
287 SpvId min_lod,
288 bool sparse);
289
290 SpvId
291 spirv_builder_emit_image(struct spirv_builder *b, SpvId result_type,
292 SpvId sampled_image);
293
294 SpvId
295 spirv_builder_emit_image_texel_pointer(struct spirv_builder *b,
296 SpvId result_type,
297 SpvId image,
298 SpvId coordinate,
299 SpvId sample);
300
301 SpvId
302 spirv_builder_emit_image_read(struct spirv_builder *b,
303 SpvId result_type,
304 SpvId image,
305 SpvId coordinate,
306 SpvId lod,
307 SpvId sample,
308 SpvId offset,
309 bool sparse);
310
311 void
312 spirv_builder_emit_image_write(struct spirv_builder *b,
313 SpvId image,
314 SpvId coordinate,
315 SpvId texel,
316 SpvId lod,
317 SpvId sample,
318 SpvId offset);
319
320 SpvId
321 spirv_builder_emit_image_fetch(struct spirv_builder *b,
322 SpvId result_type,
323 SpvId image,
324 SpvId coordinate,
325 SpvId lod,
326 SpvId sample,
327 SpvId const_offset,
328 SpvId offset,
329 bool sparse);
330 SpvId
331 spirv_builder_emit_image_gather(struct spirv_builder *b,
332 SpvId result_type,
333 SpvId image,
334 SpvId coordinate,
335 SpvId component,
336 SpvId lod,
337 SpvId sample,
338 SpvId const_offset,
339 SpvId offset,
340 SpvId dref,
341 bool sparse);
342
343 SpvId
344 spirv_builder_emit_image_query_size(struct spirv_builder *b,
345 SpvId result_type,
346 SpvId image,
347 SpvId lod);
348
349 SpvId
350 spirv_builder_emit_image_query_levels(struct spirv_builder *b,
351 SpvId result_type,
352 SpvId image);
353
354 SpvId
355 spirv_builder_emit_image_query_lod(struct spirv_builder *b,
356 SpvId result_type,
357 SpvId image,
358 SpvId coords);
359
360 SpvId
361 spirv_builder_emit_ext_inst(struct spirv_builder *b, SpvId result_type,
362 SpvId set, uint32_t instruction,
363 const SpvId args[], size_t num_args);
364
365 SpvId
366 spirv_builder_type_void(struct spirv_builder *b);
367
368 SpvId
369 spirv_builder_type_bool(struct spirv_builder *b);
370
371 SpvId
372 spirv_builder_type_int(struct spirv_builder *b, unsigned width);
373
374 SpvId
375 spirv_builder_type_uint(struct spirv_builder *b, unsigned width);
376
377 SpvId
378 spirv_builder_type_float(struct spirv_builder *b, unsigned width);
379
380 SpvId
381 spirv_builder_type_image(struct spirv_builder *b, SpvId sampled_type,
382 SpvDim dim, bool depth, bool arrayed, bool ms,
383 unsigned sampled, SpvImageFormat image_format);
384
385 SpvId
386 spirv_builder_type_sampled_image(struct spirv_builder *b, SpvId image_type);
387
388 SpvId
389 spirv_builder_type_pointer(struct spirv_builder *b,
390 SpvStorageClass storage_class, SpvId type);
391
392 SpvId
393 spirv_builder_type_vector(struct spirv_builder *b, SpvId component_type,
394 unsigned component_count);
395
396 SpvId
397 spirv_builder_type_matrix(struct spirv_builder *b, SpvId component_type,
398 unsigned component_count);
399
400 SpvId
401 spirv_builder_type_runtime_array(struct spirv_builder *b, SpvId component_type);
402
403 SpvId
404 spirv_builder_type_array(struct spirv_builder *b, SpvId component_type,
405 SpvId length);
406
407 SpvId
408 spirv_builder_type_struct(struct spirv_builder *b, const SpvId member_types[],
409 size_t num_member_types);
410
411 SpvId
412 spirv_builder_type_function(struct spirv_builder *b, SpvId return_type,
413 const SpvId parameter_types[],
414 size_t num_parameter_types);
415
416 SpvId
417 spirv_builder_function_call(struct spirv_builder *b, SpvId result_type,
418 SpvId function, const SpvId arguments[],
419 size_t num_arguments);
420
421 SpvId
422 spirv_builder_const_bool(struct spirv_builder *b, bool val);
423
424 SpvId
425 spirv_builder_const_int(struct spirv_builder *b, int width, int64_t val);
426
427 SpvId
428 spirv_builder_const_uint(struct spirv_builder *b, int width, uint64_t val);
429
430 SpvId
431 spirv_builder_spec_const_uint(struct spirv_builder *b, int width);
432
433 SpvId
434 spirv_builder_const_float(struct spirv_builder *b, int width, double val);
435
436 SpvId
437 spirv_builder_const_composite(struct spirv_builder *b, SpvId result_type,
438 const SpvId constituents[],
439 size_t num_constituents);
440
441 SpvId
442 spirv_builder_spec_const_composite(struct spirv_builder *b, SpvId result_type,
443 const SpvId constituents[],
444 size_t num_constituents);
445
446 SpvId
447 spirv_builder_emit_var(struct spirv_builder *b, SpvId type,
448 SpvStorageClass storage_class);
449
450 void
451 spirv_builder_emit_memory_barrier(struct spirv_builder *b, SpvScope scope, SpvMemorySemanticsMask semantics);
452
453 void
454 spirv_builder_emit_control_barrier(struct spirv_builder *b, SpvScope scope, SpvScope mem_scope, SpvMemorySemanticsMask semantics);
455
456 SpvId
457 spirv_builder_import(struct spirv_builder *b, const char *name);
458
459 size_t
460 spirv_builder_get_num_words(struct spirv_builder *b);
461
462 size_t
463 spirv_builder_get_words(struct spirv_builder *b, uint32_t *words,
464 size_t num_words, uint32_t spirv_version,
465 uint32_t *tcs_vertices_out_word);
466
467 void
468 spirv_builder_emit_vertex(struct spirv_builder *b, uint32_t stream, bool multistream);
469 void
470 spirv_builder_end_primitive(struct spirv_builder *b, uint32_t stream, bool multistream);
471 #endif
472