• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 void
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 
288 SpvId
289 spirv_builder_emit_image(struct spirv_builder *b, SpvId result_type,
290                          SpvId sampled_image);
291 
292 SpvId
293 spirv_builder_emit_image_texel_pointer(struct spirv_builder *b,
294                                        SpvId result_type,
295                                        SpvId image,
296                                        SpvId coordinate,
297                                        SpvId sample);
298 
299 SpvId
300 spirv_builder_emit_image_read(struct spirv_builder *b,
301                               SpvId result_type,
302                               SpvId image,
303                               SpvId coordinate,
304                               SpvId lod,
305                               SpvId sample,
306                               SpvId offset);
307 
308 void
309 spirv_builder_emit_image_write(struct spirv_builder *b,
310                                SpvId image,
311                                SpvId coordinate,
312                                SpvId texel,
313                                SpvId lod,
314                                SpvId sample,
315                                SpvId offset);
316 
317 SpvId
318 spirv_builder_emit_image_fetch(struct spirv_builder *b,
319                                SpvId result_type,
320                                SpvId image,
321                                SpvId coordinate,
322                                SpvId lod,
323                                SpvId sample,
324                                SpvId const_offset,
325                                SpvId offset);
326 SpvId
327 spirv_builder_emit_image_gather(struct spirv_builder *b,
328                                SpvId result_type,
329                                SpvId image,
330                                SpvId coordinate,
331                                SpvId component,
332                                SpvId lod,
333                                SpvId sample,
334                                SpvId const_offset,
335                                SpvId offset,
336                                SpvId dref);
337 
338 SpvId
339 spirv_builder_emit_image_query_size(struct spirv_builder *b,
340                                     SpvId result_type,
341                                     SpvId image,
342                                     SpvId lod);
343 
344 SpvId
345 spirv_builder_emit_image_query_levels(struct spirv_builder *b,
346                                     SpvId result_type,
347                                     SpvId image);
348 
349 SpvId
350 spirv_builder_emit_image_query_lod(struct spirv_builder *b,
351                                     SpvId result_type,
352                                     SpvId image,
353                                     SpvId coords);
354 
355 SpvId
356 spirv_builder_emit_ext_inst(struct spirv_builder *b, SpvId result_type,
357                             SpvId set, uint32_t instruction,
358                             const SpvId args[], size_t num_args);
359 
360 SpvId
361 spirv_builder_type_void(struct spirv_builder *b);
362 
363 SpvId
364 spirv_builder_type_bool(struct spirv_builder *b);
365 
366 SpvId
367 spirv_builder_type_int(struct spirv_builder *b, unsigned width);
368 
369 SpvId
370 spirv_builder_type_uint(struct spirv_builder *b, unsigned width);
371 
372 SpvId
373 spirv_builder_type_float(struct spirv_builder *b, unsigned width);
374 
375 SpvId
376 spirv_builder_type_image(struct spirv_builder *b, SpvId sampled_type,
377                          SpvDim dim, bool depth, bool arrayed, bool ms,
378                          unsigned sampled, SpvImageFormat image_format);
379 
380 SpvId
381 spirv_builder_type_sampled_image(struct spirv_builder *b, SpvId image_type);
382 
383 SpvId
384 spirv_builder_type_pointer(struct spirv_builder *b,
385                            SpvStorageClass storage_class, SpvId type);
386 
387 SpvId
388 spirv_builder_type_vector(struct spirv_builder *b, SpvId component_type,
389                           unsigned component_count);
390 
391 SpvId
392 spirv_builder_type_matrix(struct spirv_builder *b, SpvId component_type,
393                           unsigned component_count);
394 
395 SpvId
396 spirv_builder_type_runtime_array(struct spirv_builder *b, SpvId component_type);
397 
398 SpvId
399 spirv_builder_type_array(struct spirv_builder *b, SpvId component_type,
400                          SpvId length);
401 
402 SpvId
403 spirv_builder_type_struct(struct spirv_builder *b, const SpvId member_types[],
404                           size_t num_member_types);
405 
406 SpvId
407 spirv_builder_type_function(struct spirv_builder *b, SpvId return_type,
408                             const SpvId parameter_types[],
409                             size_t num_parameter_types);
410 
411 SpvId
412 spirv_builder_const_bool(struct spirv_builder *b, bool val);
413 
414 SpvId
415 spirv_builder_const_int(struct spirv_builder *b, int width, int64_t val);
416 
417 SpvId
418 spirv_builder_const_uint(struct spirv_builder *b, int width, uint64_t val);
419 
420 SpvId
421 spirv_builder_spec_const_uint(struct spirv_builder *b, int width);
422 
423 SpvId
424 spirv_builder_const_float(struct spirv_builder *b, int width, double val);
425 
426 SpvId
427 spirv_builder_const_composite(struct spirv_builder *b, SpvId result_type,
428                               const SpvId constituents[],
429                               size_t num_constituents);
430 
431 SpvId
432 spirv_builder_spec_const_composite(struct spirv_builder *b, SpvId result_type,
433                                    const SpvId constituents[],
434                                    size_t num_constituents);
435 
436 SpvId
437 spirv_builder_emit_var(struct spirv_builder *b, SpvId type,
438                        SpvStorageClass storage_class);
439 
440 void
441 spirv_builder_emit_memory_barrier(struct spirv_builder *b, SpvScope scope, SpvMemorySemanticsMask semantics);
442 
443 void
444 spirv_builder_emit_control_barrier(struct spirv_builder *b, SpvScope scope, SpvScope mem_scope, SpvMemorySemanticsMask semantics);
445 
446 SpvId
447 spirv_builder_import(struct spirv_builder *b, const char *name);
448 
449 size_t
450 spirv_builder_get_num_words(struct spirv_builder *b);
451 
452 size_t
453 spirv_builder_get_words(struct spirv_builder *b, uint32_t *words,
454                         size_t num_words, uint32_t spirv_version);
455 
456 void
457 spirv_builder_emit_vertex(struct spirv_builder *b, uint32_t stream);
458 void
459 spirv_builder_end_primitive(struct spirv_builder *b, uint32_t stream);
460 #endif
461