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