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