xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/zink/nir_to_spirv/spirv_builder.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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 spirv_buffer local_vars;
57    struct hash_table *types;
58    struct hash_table *consts;
59 
60    struct spirv_buffer instructions;
61    SpvId prev_id;
62    unsigned local_vars_begin;
63 };
64 
65 struct spriv_tex_src {
66    SpvId src;
67    SpvId cl_sampler;
68    SpvId coord;
69    SpvId proj;
70    SpvId bias;
71    SpvId lod;
72    SpvId dref;
73    SpvId dx;
74    SpvId dy;
75    SpvId const_offset;
76    SpvId offset;
77    SpvId sample;
78    SpvId tex_offset;
79    SpvId min_lod;
80    bool sparse;
81 };
82 
83 static inline SpvId
spirv_builder_new_id(struct spirv_builder * b)84 spirv_builder_new_id(struct spirv_builder *b)
85 {
86    return ++b->prev_id;
87 }
88 
89 void
90 spirv_builder_emit_cap(struct spirv_builder *b, SpvCapability cap);
91 
92 void
93 spirv_builder_emit_extension(struct spirv_builder *b, const char *ext);
94 
95 void
96 spirv_builder_emit_source(struct spirv_builder *b, SpvSourceLanguage lang,
97                           uint32_t version);
98 
99 void
100 spirv_builder_emit_mem_model(struct spirv_builder *b,
101                              SpvAddressingModel addr_model,
102                              SpvMemoryModel mem_model);
103 
104 void
105 spirv_builder_emit_name(struct spirv_builder *b, SpvId target,
106                         const char *name);
107 
108 void
109 spirv_builder_emit_decoration(struct spirv_builder *b, SpvId target,
110                               SpvDecoration decoration);
111 
112 void
113 spirv_builder_emit_rounding_mode(struct spirv_builder *b, SpvId target,
114                                  SpvFPRoundingMode rounding);
115 
116 void
117 spirv_builder_emit_input_attachment_index(struct spirv_builder *b, SpvId target, uint32_t id);
118 
119 void
120 spirv_builder_emit_specid(struct spirv_builder *b, SpvId target, uint32_t id);
121 
122 void
123 spirv_builder_emit_location(struct spirv_builder *b, SpvId target,
124                             uint32_t location);
125 
126 void
127 spirv_builder_emit_component(struct spirv_builder *b, SpvId target,
128                              uint32_t component);
129 
130 void
131 spirv_builder_emit_builtin(struct spirv_builder *b, SpvId target,
132                            SpvBuiltIn builtin);
133 
134 void
135 spirv_builder_emit_index(struct spirv_builder *b, SpvId target, int index);
136 
137 void
138 spirv_builder_emit_stream(struct spirv_builder *b, SpvId target, int stream);
139 
140 void
141 spirv_builder_emit_descriptor_set(struct spirv_builder *b, SpvId target,
142                                   uint32_t descriptor_set);
143 
144 void
145 spirv_builder_emit_binding(struct spirv_builder *b, SpvId target,
146                            uint32_t binding);
147 
148 void
149 spirv_builder_emit_array_stride(struct spirv_builder *b, SpvId target,
150                                 uint32_t stride);
151 
152 void
153 spirv_builder_emit_offset(struct spirv_builder *b, SpvId target,
154                           uint32_t offset);
155 
156 void
157 spirv_builder_emit_xfb_buffer(struct spirv_builder *b, SpvId target,
158                               uint32_t buffer);
159 
160 void
161 spirv_builder_emit_xfb_stride(struct spirv_builder *b, SpvId target,
162                               uint32_t stride);
163 
164 void
165 spirv_builder_emit_member_offset(struct spirv_builder *b, SpvId target,
166                                  uint32_t member, uint32_t offset);
167 
168 void
169 spirv_builder_emit_entry_point(struct spirv_builder *b,
170                                SpvExecutionModel exec_model, SpvId entry_point,
171                                const char *name, const SpvId interfaces[],
172                                size_t num_interfaces);
173 uint32_t
174 spirv_builder_emit_exec_mode_literal(struct spirv_builder *b, SpvId entry_point,
175                                      SpvExecutionMode exec_mode, uint32_t param);
176 void
177 spirv_builder_emit_exec_mode_literal3(struct spirv_builder *b, SpvId entry_point,
178                                      SpvExecutionMode exec_mode, uint32_t param[3]);
179 void
180 spirv_builder_emit_exec_mode_id3(struct spirv_builder *b, SpvId entry_point,
181                                  SpvExecutionMode exec_mode, SpvId param[3]);
182 void
183 spirv_builder_emit_exec_mode(struct spirv_builder *b, SpvId entry_point,
184                              SpvExecutionMode exec_mode);
185 
186 void
187 spirv_builder_function(struct spirv_builder *b, SpvId result,
188                        SpvId return_type,
189                        SpvFunctionControlMask function_control,
190                        SpvId function_type);
191 
192 void
193 spirv_builder_function_end(struct spirv_builder *b);
194 
195 void
196 spirv_builder_label(struct spirv_builder *b, SpvId label);
197 
198 void
199 spirv_builder_return(struct spirv_builder *b);
200 
201 SpvId
202 spirv_builder_emit_undef(struct spirv_builder *b, SpvId result_type);
203 
204 SpvId
205 spirv_builder_emit_load(struct spirv_builder *b, SpvId result_type,
206                         SpvId pointer);
207 
208 SpvId
209 spirv_builder_emit_load_aligned(struct spirv_builder *b, SpvId result_type, SpvId pointer, unsigned alignment, bool coherent);
210 void
211 spirv_builder_emit_atomic_store(struct spirv_builder *b, SpvId pointer, SpvScope scope,
212                                 SpvMemorySemanticsMask semantics, SpvId object);
213 
214 void
215 spirv_builder_emit_store(struct spirv_builder *b, SpvId pointer, SpvId object);
216 void
217 spirv_builder_emit_store_aligned(struct spirv_builder *b, SpvId pointer, SpvId object, unsigned alignment, bool coherent);
218 
219 SpvId
220 spirv_builder_emit_access_chain(struct spirv_builder *b, SpvId result_type,
221                                 SpvId base, const SpvId indexes[],
222                                 size_t num_indexes);
223 
224 void
225 spirv_builder_emit_interlock(struct spirv_builder *b, bool end);
226 
227 SpvId
228 spirv_builder_emit_unop_const(struct spirv_builder *b, SpvOp op, SpvId result_type, uint64_t operand);
229 
230 SpvId
231 spirv_builder_emit_unop(struct spirv_builder *b, SpvOp op, SpvId result_type,
232                         SpvId operand);
233 
234 SpvId
235 spirv_builder_emit_binop(struct spirv_builder *b, SpvOp op, SpvId result_type,
236                          SpvId operand0, SpvId operand1);
237 
238 SpvId
239 spirv_builder_emit_triop(struct spirv_builder *b, SpvOp op, SpvId result_type,
240                          SpvId operand0, SpvId operand1, SpvId operand2);
241 
242 SpvId
243 spirv_builder_emit_quadop(struct spirv_builder *b, SpvOp op, SpvId result_type,
244                          SpvId operand0, SpvId operand1, SpvId operand2, SpvId operand3);
245 
246 SpvId
247 spirv_builder_emit_hexop(struct spirv_builder *b, SpvOp op, SpvId result_type,
248                          SpvId operand0, SpvId operand1, SpvId operand2, SpvId operand3,
249                          SpvId operand4, SpvId operand5);
250 
251 SpvId
252 spirv_builder_emit_composite_extract(struct spirv_builder *b, SpvId result_type,
253                                      SpvId composite, const uint32_t indexes[],
254                                      size_t num_indexes);
255 
256 SpvId
257 spirv_builder_emit_composite_construct(struct spirv_builder *b,
258                                        SpvId result_type,
259                                        const SpvId constituents[],
260                                        size_t num_constituents);
261 
262 SpvId
263 spirv_builder_emit_vector_shuffle(struct spirv_builder *b, SpvId result_type,
264                                   SpvId vector_1, SpvId vector_2,
265                                   const uint32_t components[],
266                                   size_t num_components);
267 SpvId
268 spirv_builder_emit_vector_extract(struct spirv_builder *b, SpvId result_type,
269                                   SpvId vector_1,
270                                   uint32_t component);
271 SpvId
272 spirv_builder_emit_vector_insert(struct spirv_builder *b, SpvId result_type,
273                                   SpvId vector_1,
274                                   SpvId component,
275                                   uint32_t index);
276 void
277 spirv_builder_emit_branch(struct spirv_builder *b, SpvId label);
278 
279 void
280 spirv_builder_emit_selection_merge(struct spirv_builder *b, SpvId merge_block,
281                                    SpvSelectionControlMask selection_control);
282 
283 void
284 spirv_builder_loop_merge(struct spirv_builder *b, SpvId merge_block,
285                          SpvId cont_target, SpvLoopControlMask loop_control);
286 
287 void
288 spirv_builder_emit_branch_conditional(struct spirv_builder *b, SpvId condition,
289                                       SpvId true_label, SpvId false_label);
290 
291 SpvId
292 spirv_builder_emit_phi(struct spirv_builder *b, SpvId result_type,
293                        size_t num_vars, size_t *position);
294 
295 void
296 spirv_builder_set_phi_operand(struct spirv_builder *b, size_t position,
297                               size_t index, SpvId variable, SpvId parent);
298 
299 void
300 spirv_builder_emit_kill(struct spirv_builder *b);
301 
302 void
303 spirv_builder_emit_terminate(struct spirv_builder *b);
304 
305 void
306 spirv_builder_emit_demote(struct spirv_builder *b);
307 
308 SpvId
309 spirv_is_helper_invocation(struct spirv_builder *b);
310 
311 SpvId
312 spirv_builder_emit_vote(struct spirv_builder *b, SpvOp op, SpvId src);
313 
314 SpvId
315 spirv_builder_emit_image_sample(struct spirv_builder *b,
316                                 SpvId result_type,
317                                 SpvId sampled_image,
318                                 const struct spriv_tex_src *src);
319 
320 SpvId
321 spirv_builder_emit_image(struct spirv_builder *b, SpvId result_type,
322                          SpvId sampled_image);
323 
324 SpvId
325 spirv_builder_emit_image_texel_pointer(struct spirv_builder *b,
326                                        SpvId result_type,
327                                        SpvId image,
328                                        SpvId coordinate,
329                                        SpvId sample);
330 
331 SpvId
332 spirv_builder_emit_image_read(struct spirv_builder *b,
333                               SpvId result_type,
334                               SpvId image,
335                               SpvId coordinate,
336                               SpvId lod,
337                               SpvId sample,
338                               SpvId offset,
339                               bool sparse);
340 
341 void
342 spirv_builder_emit_image_write(struct spirv_builder *b,
343                                SpvId image,
344                                SpvId coordinate,
345                                SpvId texel,
346                                SpvId lod,
347                                SpvId sample,
348                                SpvId offset);
349 
350 SpvId
351 spirv_builder_emit_image_fetch(struct spirv_builder *b,
352                                SpvId result_type,
353                                SpvId image,
354                                const struct spriv_tex_src *src);
355 SpvId
356 spirv_builder_emit_image_gather(struct spirv_builder *b,
357                                SpvId result_type,
358                                SpvId image,
359                                const struct spriv_tex_src *src,
360                                SpvId component);
361 
362 SpvId
363 spirv_builder_emit_image_query_size(struct spirv_builder *b,
364                                     SpvId result_type,
365                                     SpvId image,
366                                     SpvId lod);
367 
368 SpvId
369 spirv_builder_emit_image_query_levels(struct spirv_builder *b,
370                                     SpvId result_type,
371                                     SpvId image);
372 
373 SpvId
374 spirv_builder_emit_image_query_lod(struct spirv_builder *b,
375                                     SpvId result_type,
376                                     SpvId image,
377                                     SpvId coords);
378 
379 SpvId
380 spirv_builder_emit_ext_inst(struct spirv_builder *b, SpvId result_type,
381                             SpvId set, uint32_t instruction,
382                             const SpvId args[], size_t num_args);
383 
384 SpvId
385 spirv_builder_type_void(struct spirv_builder *b);
386 
387 SpvId
388 spirv_builder_type_bool(struct spirv_builder *b);
389 
390 SpvId
391 spirv_builder_type_int(struct spirv_builder *b, unsigned width);
392 
393 SpvId
394 spirv_builder_type_uint(struct spirv_builder *b, unsigned width);
395 
396 SpvId
397 spirv_builder_type_float(struct spirv_builder *b, unsigned width);
398 
399 SpvId
400 spirv_builder_type_image(struct spirv_builder *b, SpvId sampled_type,
401                          SpvDim dim, bool depth, bool arrayed, bool ms,
402                          unsigned sampled, SpvImageFormat image_format);
403 
404 SpvId
405 spirv_builder_type_sampled_image(struct spirv_builder *b, SpvId image_type);
406 SpvId
407 spirv_builder_type_sampler(struct spirv_builder *b);
408 SpvId
409 spirv_builder_emit_sampled_image(struct spirv_builder *b, SpvId result_type, SpvId image, SpvId sampler);
410 
411 SpvId
412 spirv_builder_type_pointer(struct spirv_builder *b,
413                            SpvStorageClass storage_class, SpvId type);
414 
415 SpvId
416 spirv_builder_type_vector(struct spirv_builder *b, SpvId component_type,
417                           unsigned component_count);
418 
419 SpvId
420 spirv_builder_type_matrix(struct spirv_builder *b, SpvId component_type,
421                           unsigned component_count);
422 
423 SpvId
424 spirv_builder_type_runtime_array(struct spirv_builder *b, SpvId component_type);
425 
426 SpvId
427 spirv_builder_type_array(struct spirv_builder *b, SpvId component_type,
428                          SpvId length);
429 
430 SpvId
431 spirv_builder_type_struct(struct spirv_builder *b, const SpvId member_types[],
432                           size_t num_member_types);
433 
434 SpvId
435 spirv_builder_type_function(struct spirv_builder *b, SpvId return_type,
436                             const SpvId parameter_types[],
437                             size_t num_parameter_types);
438 
439 SpvId
440 spirv_builder_function_call(struct spirv_builder *b, SpvId result_type,
441                             SpvId function, const SpvId arguments[],
442                             size_t num_arguments);
443 
444 SpvId
445 spirv_builder_const_bool(struct spirv_builder *b, bool val);
446 
447 SpvId
448 spirv_builder_const_int(struct spirv_builder *b, int width, int64_t val);
449 
450 SpvId
451 spirv_builder_const_uint(struct spirv_builder *b, int width, uint64_t val);
452 
453 SpvId
454 spirv_builder_spec_const_uint(struct spirv_builder *b, int width);
455 
456 SpvId
457 spirv_builder_const_float(struct spirv_builder *b, int width, double val);
458 
459 SpvId
460 spirv_builder_const_composite(struct spirv_builder *b, SpvId result_type,
461                               const SpvId constituents[],
462                               size_t num_constituents);
463 
464 SpvId
465 spirv_builder_spec_const_composite(struct spirv_builder *b, SpvId result_type,
466                                    const SpvId constituents[],
467                                    size_t num_constituents);
468 
469 SpvId
470 spirv_builder_emit_var(struct spirv_builder *b, SpvId type,
471                        SpvStorageClass storage_class);
472 
473 void
474 spirv_builder_emit_memory_barrier(struct spirv_builder *b, SpvScope scope, SpvMemorySemanticsMask semantics);
475 
476 void
477 spirv_builder_emit_control_barrier(struct spirv_builder *b, SpvScope scope, SpvScope mem_scope, SpvMemorySemanticsMask semantics);
478 
479 SpvId
480 spirv_builder_import(struct spirv_builder *b, const char *name);
481 
482 size_t
483 spirv_builder_get_num_words(struct spirv_builder *b);
484 
485 size_t
486 spirv_builder_get_words(struct spirv_builder *b, uint32_t *words,
487                         size_t num_words, uint32_t spirv_version,
488                         uint32_t *tcs_vertices_out_word);
489 
490 void
491 spirv_builder_emit_vertex(struct spirv_builder *b, uint32_t stream, bool multistream);
492 void
493 spirv_builder_end_primitive(struct spirv_builder *b, uint32_t stream, bool multistream);
494 void
495 spirv_builder_begin_local_vars(struct spirv_builder *b);
496 #endif
497