• 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 
36 struct spirv_buffer {
37    uint32_t *words;
38    size_t num_words, room;
39 };
40 
41 struct spirv_builder {
42    void *mem_ctx;
43 
44    struct spirv_buffer capabilities;
45    struct spirv_buffer extensions;
46    struct spirv_buffer imports;
47    struct spirv_buffer memory_model;
48    struct spirv_buffer entry_points;
49    struct spirv_buffer exec_modes;
50    struct spirv_buffer debug_names;
51    struct spirv_buffer decorations;
52 
53    struct spirv_buffer types_const_defs;
54    struct hash_table *types;
55    struct hash_table *consts;
56 
57    struct spirv_buffer instructions;
58    SpvId prev_id;
59 };
60 
61 static inline SpvId
spirv_builder_new_id(struct spirv_builder * b)62 spirv_builder_new_id(struct spirv_builder *b)
63 {
64    return ++b->prev_id;
65 }
66 
67 void
68 spirv_builder_emit_cap(struct spirv_builder *b, SpvCapability cap);
69 
70 void
71 spirv_builder_emit_extension(struct spirv_builder *b, const char *ext);
72 
73 void
74 spirv_builder_emit_source(struct spirv_builder *b, SpvSourceLanguage lang,
75                           uint32_t version);
76 
77 void
78 spirv_builder_emit_mem_model(struct spirv_builder *b,
79                              SpvAddressingModel addr_model,
80                              SpvMemoryModel mem_model);
81 
82 void
83 spirv_builder_emit_name(struct spirv_builder *b, SpvId target,
84                         const char *name);
85 
86 void
87 spirv_builder_emit_decoration(struct spirv_builder *b, SpvId target,
88                               SpvDecoration decoration);
89 
90 void
91 spirv_builder_emit_location(struct spirv_builder *b, SpvId target,
92                             uint32_t location);
93 
94 void
95 spirv_builder_emit_component(struct spirv_builder *b, SpvId target,
96                              uint32_t component);
97 
98 void
99 spirv_builder_emit_builtin(struct spirv_builder *b, SpvId target,
100                            SpvBuiltIn builtin);
101 
102 void
103 spirv_builder_emit_index(struct spirv_builder *b, SpvId target, int index);
104 
105 void
106 spirv_builder_emit_descriptor_set(struct spirv_builder *b, SpvId target,
107                                   uint32_t descriptor_set);
108 
109 void
110 spirv_builder_emit_binding(struct spirv_builder *b, SpvId target,
111                            uint32_t binding);
112 
113 void
114 spirv_builder_emit_array_stride(struct spirv_builder *b, SpvId target,
115                                 uint32_t stride);
116 
117 void
118 spirv_builder_emit_offset(struct spirv_builder *b, SpvId target,
119                           uint32_t offset);
120 
121 void
122 spirv_builder_emit_xfb_buffer(struct spirv_builder *b, SpvId target,
123                               uint32_t buffer);
124 
125 void
126 spirv_builder_emit_xfb_stride(struct spirv_builder *b, SpvId target,
127                               uint32_t stride);
128 
129 void
130 spirv_builder_emit_member_offset(struct spirv_builder *b, SpvId target,
131                                  uint32_t member, uint32_t offset);
132 
133 void
134 spirv_builder_emit_entry_point(struct spirv_builder *b,
135                                SpvExecutionModel exec_model, SpvId entry_point,
136                                const char *name, const SpvId interfaces[],
137                                size_t num_interfaces);
138 void
139 spirv_builder_emit_exec_mode_literal(struct spirv_builder *b, SpvId entry_point,
140                                      SpvExecutionMode exec_mode, uint32_t param);
141 void
142 spirv_builder_emit_exec_mode(struct spirv_builder *b, SpvId entry_point,
143                              SpvExecutionMode exec_mode);
144 
145 void
146 spirv_builder_function(struct spirv_builder *b, SpvId result,
147                        SpvId return_type,
148                        SpvFunctionControlMask function_control,
149                        SpvId function_type);
150 
151 void
152 spirv_builder_function_end(struct spirv_builder *b);
153 
154 void
155 spirv_builder_label(struct spirv_builder *b, SpvId label);
156 
157 void
158 spirv_builder_return(struct spirv_builder *b);
159 
160 SpvId
161 spirv_builder_emit_undef(struct spirv_builder *b, SpvId result_type);
162 
163 SpvId
164 spirv_builder_emit_load(struct spirv_builder *b, SpvId result_type,
165                         SpvId pointer);
166 
167 void
168 spirv_builder_emit_store(struct spirv_builder *b, SpvId pointer, SpvId object);
169 
170 SpvId
171 spirv_builder_emit_access_chain(struct spirv_builder *b, SpvId result_type,
172                                 SpvId base, const SpvId indexes[],
173                                 size_t num_indexes);
174 
175 SpvId
176 spirv_builder_emit_unop(struct spirv_builder *b, SpvOp op, SpvId result_type,
177                         SpvId operand);
178 
179 SpvId
180 spirv_builder_emit_binop(struct spirv_builder *b, SpvOp op, SpvId result_type,
181                          SpvId operand0, SpvId operand1);
182 
183 SpvId
184 spirv_builder_emit_triop(struct spirv_builder *b, SpvOp op, SpvId result_type,
185                          SpvId operand0, SpvId operand1, SpvId operand2);
186 
187 SpvId
188 spirv_builder_emit_quadop(struct spirv_builder *b, SpvOp op, SpvId result_type,
189                          SpvId operand0, SpvId operand1, SpvId operand2, SpvId operand3);
190 
191 SpvId
192 spirv_builder_emit_composite_extract(struct spirv_builder *b, SpvId result_type,
193                                      SpvId composite, const uint32_t indexes[],
194                                      size_t num_indexes);
195 
196 SpvId
197 spirv_builder_emit_composite_construct(struct spirv_builder *b,
198                                        SpvId result_type,
199                                        const SpvId constituents[],
200                                        size_t num_constituents);
201 
202 SpvId
203 spirv_builder_emit_vector_shuffle(struct spirv_builder *b, SpvId result_type,
204                                   SpvId vector_1, SpvId vector_2,
205                                   const uint32_t components[],
206                                   size_t num_components);
207 SpvId
208 spirv_builder_emit_vector_extract(struct spirv_builder *b, SpvId result_type,
209                                   SpvId vector_1,
210                                   uint32_t component);
211 SpvId
212 spirv_builder_emit_vector_insert(struct spirv_builder *b, SpvId result_type,
213                                   SpvId vector_1,
214                                   SpvId component,
215                                   uint32_t index);
216 void
217 spirv_builder_emit_branch(struct spirv_builder *b, SpvId label);
218 
219 void
220 spirv_builder_emit_selection_merge(struct spirv_builder *b, SpvId merge_block,
221                                    SpvSelectionControlMask selection_control);
222 
223 void
224 spirv_builder_loop_merge(struct spirv_builder *b, SpvId merge_block,
225                          SpvId cont_target, SpvLoopControlMask loop_control);
226 
227 void
228 spirv_builder_emit_branch_conditional(struct spirv_builder *b, SpvId condition,
229                                       SpvId true_label, SpvId false_label);
230 
231 SpvId
232 spirv_builder_emit_phi(struct spirv_builder *b, SpvId result_type,
233                        size_t num_vars, size_t *position);
234 
235 void
236 spirv_builder_set_phi_operand(struct spirv_builder *b, size_t position,
237                               size_t index, SpvId variable, SpvId parent);
238 
239 void
240 spirv_builder_emit_kill(struct spirv_builder *b);
241 
242 
243 SpvId
244 spirv_builder_emit_image_sample(struct spirv_builder *b,
245                                 SpvId result_type,
246                                 SpvId sampled_image,
247                                 SpvId coordinate,
248                                 bool proj,
249                                 SpvId lod,
250                                 SpvId bias,
251                                 SpvId dref,
252                                 SpvId dx,
253                                 SpvId dy,
254                                 SpvId offset);
255 
256 SpvId
257 spirv_builder_emit_image(struct spirv_builder *b, SpvId result_type,
258                          SpvId sampled_image);
259 
260 SpvId
261 spirv_builder_emit_image_fetch(struct spirv_builder *b,
262                                SpvId result_type,
263                                SpvId image,
264                                SpvId coordinate,
265                                SpvId lod,
266                                SpvId sample);
267 
268 SpvId
269 spirv_builder_emit_image_query_size(struct spirv_builder *b,
270                                     SpvId result_type,
271                                     SpvId image,
272                                     SpvId lod);
273 
274 SpvId
275 spirv_builder_emit_image_query_lod(struct spirv_builder *b,
276                                     SpvId result_type,
277                                     SpvId image,
278                                     SpvId coords);
279 
280 SpvId
281 spirv_builder_emit_ext_inst(struct spirv_builder *b, SpvId result_type,
282                             SpvId set, uint32_t instruction,
283                             const SpvId args[], size_t num_args);
284 
285 SpvId
286 spirv_builder_type_void(struct spirv_builder *b);
287 
288 SpvId
289 spirv_builder_type_bool(struct spirv_builder *b);
290 
291 SpvId
292 spirv_builder_type_int(struct spirv_builder *b, unsigned width);
293 
294 SpvId
295 spirv_builder_type_uint(struct spirv_builder *b, unsigned width);
296 
297 SpvId
298 spirv_builder_type_float(struct spirv_builder *b, unsigned width);
299 
300 SpvId
301 spirv_builder_type_image(struct spirv_builder *b, SpvId sampled_type,
302                          SpvDim dim, bool depth, bool arrayed, bool ms,
303                          unsigned sampled, SpvImageFormat image_format);
304 
305 SpvId
306 spirv_builder_type_sampled_image(struct spirv_builder *b, SpvId image_type);
307 
308 SpvId
309 spirv_builder_type_pointer(struct spirv_builder *b,
310                            SpvStorageClass storage_class, SpvId type);
311 
312 SpvId
313 spirv_builder_type_vector(struct spirv_builder *b, SpvId component_type,
314                           unsigned component_count);
315 
316 SpvId
317 spirv_builder_type_matrix(struct spirv_builder *b, SpvId component_type,
318                           unsigned component_count);
319 
320 SpvId
321 spirv_builder_type_array(struct spirv_builder *b, SpvId component_type,
322                          SpvId length);
323 
324 SpvId
325 spirv_builder_type_struct(struct spirv_builder *b, const SpvId member_types[],
326                           size_t num_member_types);
327 
328 SpvId
329 spirv_builder_type_function(struct spirv_builder *b, SpvId return_type,
330                             const SpvId parameter_types[],
331                             size_t num_parameter_types);
332 
333 SpvId
334 spirv_builder_const_bool(struct spirv_builder *b, bool val);
335 
336 SpvId
337 spirv_builder_const_int(struct spirv_builder *b, int width, int32_t val);
338 
339 SpvId
340 spirv_builder_const_uint(struct spirv_builder *b, int width, uint32_t val);
341 
342 SpvId
343 spirv_builder_const_float(struct spirv_builder *b, int width, float val);
344 
345 SpvId
346 spirv_builder_const_composite(struct spirv_builder *b, SpvId result_type,
347                               const SpvId constituents[],
348                               size_t num_constituents);
349 
350 SpvId
351 spirv_builder_emit_var(struct spirv_builder *b, SpvId type,
352                        SpvStorageClass storage_class);
353 
354 void
355 spirv_builder_emit_memory_barrier(struct spirv_builder *b, SpvScope scope, SpvMemorySemanticsMask semantics);
356 
357 void
358 spirv_builder_emit_control_barrier(struct spirv_builder *b, SpvScope scope, SpvScope mem_scope, SpvMemorySemanticsMask semantics);
359 
360 SpvId
361 spirv_builder_import(struct spirv_builder *b, const char *name);
362 
363 size_t
364 spirv_builder_get_num_words(struct spirv_builder *b);
365 
366 size_t
367 spirv_builder_get_words(struct spirv_builder *b, uint32_t *words,
368                         size_t num_words);
369 
370 void
371 spirv_builder_emit_vertex(struct spirv_builder *b);
372 void
373 spirv_builder_end_primitive(struct spirv_builder *b);
374 #endif
375