a03d08e06eb4a6a1753805c8091cc12835092e66
[mesa.git] / src / gallium / drivers / zink / nir_to_spirv / spirv_builder.c
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 #include "spirv_builder.h"
25
26 #include "util/macros.h"
27 #include "util/u_bitcast.h"
28 #include "util/u_memory.h"
29 #include "util/hash_table.h"
30 #define XXH_INLINE_ALL
31 #include "util/xxhash.h"
32
33 #include <stdbool.h>
34 #include <inttypes.h>
35 #include <string.h>
36
37 static bool
38 spirv_buffer_grow(struct spirv_buffer *b, size_t needed)
39 {
40 size_t new_room = MAX3(64, (b->room * 3) / 2, needed);
41
42 uint32_t *new_words = realloc(b->words, new_room * sizeof(uint32_t));
43 if (!new_words)
44 return false;
45
46 b->words = new_words;
47 b->room = new_room;
48 return true;
49 }
50
51 static inline bool
52 spirv_buffer_prepare(struct spirv_buffer *b, size_t needed)
53 {
54 needed += b->num_words;
55 if (b->room >= b->num_words + needed)
56 return true;
57
58 return spirv_buffer_grow(b, needed);
59 }
60
61 static inline void
62 spirv_buffer_emit_word(struct spirv_buffer *b, uint32_t word)
63 {
64 assert(b->num_words < b->room);
65 b->words[b->num_words++] = word;
66 }
67
68 static int
69 spirv_buffer_emit_string(struct spirv_buffer *b, const char *str)
70 {
71 int pos = 0;
72 uint32_t word = 0;
73 while (str[pos] != '\0') {
74 word |= str[pos] << (8 * (pos % 4));
75 if (++pos % 4 == 0) {
76 spirv_buffer_prepare(b, 1);
77 spirv_buffer_emit_word(b, word);
78 word = 0;
79 }
80 }
81
82 spirv_buffer_prepare(b, 1);
83 spirv_buffer_emit_word(b, word);
84
85 return 1 + pos / 4;
86 }
87
88 void
89 spirv_builder_emit_cap(struct spirv_builder *b, SpvCapability cap)
90 {
91 spirv_buffer_prepare(&b->capabilities, 2);
92 spirv_buffer_emit_word(&b->capabilities, SpvOpCapability | (2 << 16));
93 spirv_buffer_emit_word(&b->capabilities, cap);
94 }
95
96 void
97 spirv_builder_emit_source(struct spirv_builder *b, SpvSourceLanguage lang,
98 uint32_t version)
99 {
100 spirv_buffer_prepare(&b->debug_names, 3);
101 spirv_buffer_emit_word(&b->debug_names, SpvOpSource | (3 << 16));
102 spirv_buffer_emit_word(&b->debug_names, lang);
103 spirv_buffer_emit_word(&b->debug_names, version);
104 }
105
106 void
107 spirv_builder_emit_mem_model(struct spirv_builder *b,
108 SpvAddressingModel addr_model,
109 SpvMemoryModel mem_model)
110 {
111 spirv_buffer_prepare(&b->memory_model, 3);
112 spirv_buffer_emit_word(&b->memory_model, SpvOpMemoryModel | (3 << 16));
113 spirv_buffer_emit_word(&b->memory_model, addr_model);
114 spirv_buffer_emit_word(&b->memory_model, mem_model);
115 }
116
117 void
118 spirv_builder_emit_entry_point(struct spirv_builder *b,
119 SpvExecutionModel exec_model, SpvId entry_point,
120 const char *name, const SpvId interfaces[],
121 size_t num_interfaces)
122 {
123 size_t pos = b->entry_points.num_words;
124 spirv_buffer_prepare(&b->entry_points, 3);
125 spirv_buffer_emit_word(&b->entry_points, SpvOpEntryPoint);
126 spirv_buffer_emit_word(&b->entry_points, exec_model);
127 spirv_buffer_emit_word(&b->entry_points, entry_point);
128 int len = spirv_buffer_emit_string(&b->entry_points, name);
129 b->entry_points.words[pos] |= (3 + len + num_interfaces) << 16;
130 spirv_buffer_prepare(&b->entry_points, num_interfaces);
131 for (int i = 0; i < num_interfaces; ++i)
132 spirv_buffer_emit_word(&b->entry_points, interfaces[i]);
133 }
134
135 void
136 spirv_builder_emit_exec_mode(struct spirv_builder *b, SpvId entry_point,
137 SpvExecutionMode exec_mode)
138 {
139 spirv_buffer_prepare(&b->exec_modes, 3);
140 spirv_buffer_emit_word(&b->exec_modes, SpvOpExecutionMode | (3 << 16));
141 spirv_buffer_emit_word(&b->exec_modes, entry_point);
142 spirv_buffer_emit_word(&b->exec_modes, exec_mode);
143 }
144
145 void
146 spirv_builder_emit_name(struct spirv_builder *b, SpvId target,
147 const char *name)
148 {
149 size_t pos = b->debug_names.num_words;
150 spirv_buffer_prepare(&b->debug_names, 2);
151 spirv_buffer_emit_word(&b->debug_names, SpvOpName);
152 spirv_buffer_emit_word(&b->debug_names, target);
153 int len = spirv_buffer_emit_string(&b->debug_names, name);
154 b->debug_names.words[pos] |= (2 + len) << 16;
155 }
156
157 static void
158 emit_decoration(struct spirv_builder *b, SpvId target,
159 SpvDecoration decoration, const uint32_t extra_operands[],
160 size_t num_extra_operands)
161 {
162 int words = 3 + num_extra_operands;
163 spirv_buffer_prepare(&b->decorations, words);
164 spirv_buffer_emit_word(&b->decorations, SpvOpDecorate | (words << 16));
165 spirv_buffer_emit_word(&b->decorations, target);
166 spirv_buffer_emit_word(&b->decorations, decoration);
167 for (int i = 0; i < num_extra_operands; ++i)
168 spirv_buffer_emit_word(&b->decorations, extra_operands[i]);
169 }
170
171 void
172 spirv_builder_emit_decoration(struct spirv_builder *b, SpvId target,
173 SpvDecoration decoration)
174 {
175 emit_decoration(b, target, decoration, NULL, 0);
176 }
177
178 void
179 spirv_builder_emit_location(struct spirv_builder *b, SpvId target,
180 uint32_t location)
181 {
182 uint32_t args[] = { location };
183 emit_decoration(b, target, SpvDecorationLocation, args, ARRAY_SIZE(args));
184 }
185
186 void
187 spirv_builder_emit_component(struct spirv_builder *b, SpvId target,
188 uint32_t component)
189 {
190 uint32_t args[] = { component };
191 emit_decoration(b, target, SpvDecorationComponent, args, ARRAY_SIZE(args));
192 }
193
194 void
195 spirv_builder_emit_builtin(struct spirv_builder *b, SpvId target,
196 SpvBuiltIn builtin)
197 {
198 uint32_t args[] = { builtin };
199 emit_decoration(b, target, SpvDecorationBuiltIn, args, ARRAY_SIZE(args));
200 }
201
202 void
203 spirv_builder_emit_descriptor_set(struct spirv_builder *b, SpvId target,
204 uint32_t descriptor_set)
205 {
206 uint32_t args[] = { descriptor_set };
207 emit_decoration(b, target, SpvDecorationDescriptorSet, args,
208 ARRAY_SIZE(args));
209 }
210
211 void
212 spirv_builder_emit_binding(struct spirv_builder *b, SpvId target,
213 uint32_t binding)
214 {
215 uint32_t args[] = { binding };
216 emit_decoration(b, target, SpvDecorationBinding, args, ARRAY_SIZE(args));
217 }
218
219 void
220 spirv_builder_emit_array_stride(struct spirv_builder *b, SpvId target,
221 uint32_t stride)
222 {
223 uint32_t args[] = { stride };
224 emit_decoration(b, target, SpvDecorationArrayStride, args, ARRAY_SIZE(args));
225 }
226
227 void
228 spirv_builder_emit_offset(struct spirv_builder *b, SpvId target,
229 uint32_t offset)
230 {
231 uint32_t args[] = { offset };
232 emit_decoration(b, target, SpvDecorationOffset, args, ARRAY_SIZE(args));
233 }
234
235 void
236 spirv_builder_emit_xfb_buffer(struct spirv_builder *b, SpvId target,
237 uint32_t buffer)
238 {
239 uint32_t args[] = { buffer };
240 emit_decoration(b, target, SpvDecorationXfbBuffer, args, ARRAY_SIZE(args));
241 }
242
243 void
244 spirv_builder_emit_xfb_stride(struct spirv_builder *b, SpvId target,
245 uint32_t stride)
246 {
247 uint32_t args[] = { stride };
248 emit_decoration(b, target, SpvDecorationXfbStride, args, ARRAY_SIZE(args));
249 }
250
251 void
252 spirv_builder_emit_index(struct spirv_builder *b, SpvId target, int index)
253 {
254 uint32_t args[] = { index };
255 emit_decoration(b, target, SpvDecorationIndex, args, ARRAY_SIZE(args));
256 }
257
258 static void
259 emit_member_decoration(struct spirv_builder *b, SpvId target, uint32_t member,
260 SpvDecoration decoration, const uint32_t extra_operands[],
261 size_t num_extra_operands)
262 {
263 int words = 4 + num_extra_operands;
264 spirv_buffer_prepare(&b->decorations, words);
265 spirv_buffer_emit_word(&b->decorations,
266 SpvOpMemberDecorate | (words << 16));
267 spirv_buffer_emit_word(&b->decorations, target);
268 spirv_buffer_emit_word(&b->decorations, member);
269 spirv_buffer_emit_word(&b->decorations, decoration);
270 for (int i = 0; i < num_extra_operands; ++i)
271 spirv_buffer_emit_word(&b->decorations, extra_operands[i]);
272 }
273
274 void
275 spirv_builder_emit_member_offset(struct spirv_builder *b, SpvId target,
276 uint32_t member, uint32_t offset)
277 {
278 uint32_t args[] = { offset };
279 emit_member_decoration(b, target, member, SpvDecorationOffset,
280 args, ARRAY_SIZE(args));
281 }
282
283 SpvId
284 spirv_builder_emit_undef(struct spirv_builder *b, SpvId result_type)
285 {
286 SpvId result = spirv_builder_new_id(b);
287 spirv_buffer_prepare(&b->instructions, 3);
288 spirv_buffer_emit_word(&b->instructions, SpvOpUndef | (3 << 16));
289 spirv_buffer_emit_word(&b->instructions, result_type);
290 spirv_buffer_emit_word(&b->instructions, result);
291 return result;
292 }
293
294 void
295 spirv_builder_function(struct spirv_builder *b, SpvId result,
296 SpvId return_type,
297 SpvFunctionControlMask function_control,
298 SpvId function_type)
299 {
300 spirv_buffer_prepare(&b->instructions, 5);
301 spirv_buffer_emit_word(&b->instructions, SpvOpFunction | (5 << 16));
302 spirv_buffer_emit_word(&b->instructions, return_type);
303 spirv_buffer_emit_word(&b->instructions, result);
304 spirv_buffer_emit_word(&b->instructions, function_control);
305 spirv_buffer_emit_word(&b->instructions, function_type);
306 }
307
308 void
309 spirv_builder_function_end(struct spirv_builder *b)
310 {
311 spirv_buffer_prepare(&b->instructions, 1);
312 spirv_buffer_emit_word(&b->instructions, SpvOpFunctionEnd | (1 << 16));
313 }
314
315 void
316 spirv_builder_label(struct spirv_builder *b, SpvId label)
317 {
318 spirv_buffer_prepare(&b->instructions, 2);
319 spirv_buffer_emit_word(&b->instructions, SpvOpLabel | (2 << 16));
320 spirv_buffer_emit_word(&b->instructions, label);
321 }
322
323 void
324 spirv_builder_return(struct spirv_builder *b)
325 {
326 spirv_buffer_prepare(&b->instructions, 1);
327 spirv_buffer_emit_word(&b->instructions, SpvOpReturn | (1 << 16));
328 }
329
330 SpvId
331 spirv_builder_emit_load(struct spirv_builder *b, SpvId result_type,
332 SpvId pointer)
333 {
334 return spirv_builder_emit_unop(b, SpvOpLoad, result_type, pointer);
335 }
336
337 void
338 spirv_builder_emit_store(struct spirv_builder *b, SpvId pointer, SpvId object)
339 {
340 spirv_buffer_prepare(&b->instructions, 3);
341 spirv_buffer_emit_word(&b->instructions, SpvOpStore | (3 << 16));
342 spirv_buffer_emit_word(&b->instructions, pointer);
343 spirv_buffer_emit_word(&b->instructions, object);
344 }
345
346 SpvId
347 spirv_builder_emit_access_chain(struct spirv_builder *b, SpvId result_type,
348 SpvId base, const SpvId indexes[],
349 size_t num_indexes)
350 {
351 SpvId result = spirv_builder_new_id(b);
352
353 int words = 4 + num_indexes;
354 spirv_buffer_prepare(&b->instructions, words);
355 spirv_buffer_emit_word(&b->instructions, SpvOpAccessChain | (words << 16));
356 spirv_buffer_emit_word(&b->instructions, result_type);
357 spirv_buffer_emit_word(&b->instructions, result);
358 spirv_buffer_emit_word(&b->instructions, base);
359 for (int i = 0; i < num_indexes; ++i)
360 spirv_buffer_emit_word(&b->instructions, indexes[i]);
361 return result;
362 }
363
364
365 SpvId
366 spirv_builder_emit_unop(struct spirv_builder *b, SpvOp op, SpvId result_type,
367 SpvId operand)
368 {
369 SpvId result = spirv_builder_new_id(b);
370 spirv_buffer_prepare(&b->instructions, 4);
371 spirv_buffer_emit_word(&b->instructions, op | (4 << 16));
372 spirv_buffer_emit_word(&b->instructions, result_type);
373 spirv_buffer_emit_word(&b->instructions, result);
374 spirv_buffer_emit_word(&b->instructions, operand);
375 return result;
376 }
377
378 SpvId
379 spirv_builder_emit_binop(struct spirv_builder *b, SpvOp op, SpvId result_type,
380 SpvId operand0, SpvId operand1)
381 {
382 SpvId result = spirv_builder_new_id(b);
383 spirv_buffer_prepare(&b->instructions, 5);
384 spirv_buffer_emit_word(&b->instructions, op | (5 << 16));
385 spirv_buffer_emit_word(&b->instructions, result_type);
386 spirv_buffer_emit_word(&b->instructions, result);
387 spirv_buffer_emit_word(&b->instructions, operand0);
388 spirv_buffer_emit_word(&b->instructions, operand1);
389 return result;
390 }
391
392 SpvId
393 spirv_builder_emit_triop(struct spirv_builder *b, SpvOp op, SpvId result_type,
394 SpvId operand0, SpvId operand1, SpvId operand2)
395 {
396 SpvId result = spirv_builder_new_id(b);
397 spirv_buffer_prepare(&b->instructions, 6);
398 spirv_buffer_emit_word(&b->instructions, op | (6 << 16));
399 spirv_buffer_emit_word(&b->instructions, result_type);
400 spirv_buffer_emit_word(&b->instructions, result);
401 spirv_buffer_emit_word(&b->instructions, operand0);
402 spirv_buffer_emit_word(&b->instructions, operand1);
403 spirv_buffer_emit_word(&b->instructions, operand2);
404 return result;
405 }
406
407 SpvId
408 spirv_builder_emit_composite_extract(struct spirv_builder *b, SpvId result_type,
409 SpvId composite, const uint32_t indexes[],
410 size_t num_indexes)
411 {
412 SpvId result = spirv_builder_new_id(b);
413
414 assert(num_indexes > 0);
415 int words = 4 + num_indexes;
416 spirv_buffer_prepare(&b->instructions, words);
417 spirv_buffer_emit_word(&b->instructions,
418 SpvOpCompositeExtract | (words << 16));
419 spirv_buffer_emit_word(&b->instructions, result_type);
420 spirv_buffer_emit_word(&b->instructions, result);
421 spirv_buffer_emit_word(&b->instructions, composite);
422 for (int i = 0; i < num_indexes; ++i)
423 spirv_buffer_emit_word(&b->instructions, indexes[i]);
424 return result;
425 }
426
427 SpvId
428 spirv_builder_emit_composite_construct(struct spirv_builder *b,
429 SpvId result_type,
430 const SpvId constituents[],
431 size_t num_constituents)
432 {
433 SpvId result = spirv_builder_new_id(b);
434
435 assert(num_constituents > 0);
436 int words = 3 + num_constituents;
437 spirv_buffer_prepare(&b->instructions, words);
438 spirv_buffer_emit_word(&b->instructions,
439 SpvOpCompositeConstruct | (words << 16));
440 spirv_buffer_emit_word(&b->instructions, result_type);
441 spirv_buffer_emit_word(&b->instructions, result);
442 for (int i = 0; i < num_constituents; ++i)
443 spirv_buffer_emit_word(&b->instructions, constituents[i]);
444 return result;
445 }
446
447 SpvId
448 spirv_builder_emit_vector_shuffle(struct spirv_builder *b, SpvId result_type,
449 SpvId vector_1, SpvId vector_2,
450 const uint32_t components[],
451 size_t num_components)
452 {
453 SpvId result = spirv_builder_new_id(b);
454
455 assert(num_components > 0);
456 int words = 5 + num_components;
457 spirv_buffer_prepare(&b->instructions, words);
458 spirv_buffer_emit_word(&b->instructions, SpvOpVectorShuffle | (words << 16));
459 spirv_buffer_emit_word(&b->instructions, result_type);
460 spirv_buffer_emit_word(&b->instructions, result);
461 spirv_buffer_emit_word(&b->instructions, vector_1);
462 spirv_buffer_emit_word(&b->instructions, vector_2);
463 for (int i = 0; i < num_components; ++i)
464 spirv_buffer_emit_word(&b->instructions, components[i]);
465 return result;
466 }
467
468 SpvId
469 spirv_builder_emit_vector_extract(struct spirv_builder *b, SpvId result_type,
470 SpvId vector_1,
471 uint32_t component)
472 {
473 SpvId result = spirv_builder_new_id(b);
474
475 int words = 5;
476 spirv_buffer_prepare(&b->instructions, words);
477 spirv_buffer_emit_word(&b->instructions, SpvOpVectorExtractDynamic | (words << 16));
478 spirv_buffer_emit_word(&b->instructions, result_type);
479 spirv_buffer_emit_word(&b->instructions, result);
480 spirv_buffer_emit_word(&b->instructions, vector_1);
481 spirv_buffer_emit_word(&b->instructions, spirv_builder_const_uint(b, 32, component));
482 return result;
483 }
484
485 SpvId
486 spirv_builder_emit_vector_insert(struct spirv_builder *b, SpvId result_type,
487 SpvId vector_1,
488 SpvId component,
489 uint32_t index)
490 {
491 SpvId result = spirv_builder_new_id(b);
492
493 int words = 6;
494 spirv_buffer_prepare(&b->instructions, words);
495 spirv_buffer_emit_word(&b->instructions, SpvOpVectorInsertDynamic | (words << 16));
496 spirv_buffer_emit_word(&b->instructions, result_type);
497 spirv_buffer_emit_word(&b->instructions, result);
498 spirv_buffer_emit_word(&b->instructions, vector_1);
499 spirv_buffer_emit_word(&b->instructions, component);
500 spirv_buffer_emit_word(&b->instructions, spirv_builder_const_uint(b, 32, index));
501 return result;
502 }
503
504 void
505 spirv_builder_emit_branch(struct spirv_builder *b, SpvId label)
506 {
507 spirv_buffer_prepare(&b->instructions, 2);
508 spirv_buffer_emit_word(&b->instructions, SpvOpBranch | (2 << 16));
509 spirv_buffer_emit_word(&b->instructions, label);
510 }
511
512 void
513 spirv_builder_emit_selection_merge(struct spirv_builder *b, SpvId merge_block,
514 SpvSelectionControlMask selection_control)
515 {
516 spirv_buffer_prepare(&b->instructions, 3);
517 spirv_buffer_emit_word(&b->instructions, SpvOpSelectionMerge | (3 << 16));
518 spirv_buffer_emit_word(&b->instructions, merge_block);
519 spirv_buffer_emit_word(&b->instructions, selection_control);
520 }
521
522 void
523 spirv_builder_loop_merge(struct spirv_builder *b, SpvId merge_block,
524 SpvId cont_target, SpvLoopControlMask loop_control)
525 {
526 spirv_buffer_prepare(&b->instructions, 4);
527 spirv_buffer_emit_word(&b->instructions, SpvOpLoopMerge | (4 << 16));
528 spirv_buffer_emit_word(&b->instructions, merge_block);
529 spirv_buffer_emit_word(&b->instructions, cont_target);
530 spirv_buffer_emit_word(&b->instructions, loop_control);
531 }
532
533 void
534 spirv_builder_emit_branch_conditional(struct spirv_builder *b, SpvId condition,
535 SpvId true_label, SpvId false_label)
536 {
537 spirv_buffer_prepare(&b->instructions, 4);
538 spirv_buffer_emit_word(&b->instructions, SpvOpBranchConditional | (4 << 16));
539 spirv_buffer_emit_word(&b->instructions, condition);
540 spirv_buffer_emit_word(&b->instructions, true_label);
541 spirv_buffer_emit_word(&b->instructions, false_label);
542 }
543
544 SpvId
545 spirv_builder_emit_phi(struct spirv_builder *b, SpvId result_type,
546 size_t num_vars, size_t *position)
547 {
548 SpvId result = spirv_builder_new_id(b);
549
550 assert(num_vars > 0);
551 int words = 3 + 2 * num_vars;
552 spirv_buffer_prepare(&b->instructions, words);
553 spirv_buffer_emit_word(&b->instructions, SpvOpPhi | (words << 16));
554 spirv_buffer_emit_word(&b->instructions, result_type);
555 spirv_buffer_emit_word(&b->instructions, result);
556 *position = b->instructions.num_words;
557 for (int i = 0; i < 2 * num_vars; ++i)
558 spirv_buffer_emit_word(&b->instructions, 0);
559 return result;
560 }
561
562 void
563 spirv_builder_set_phi_operand(struct spirv_builder *b, size_t position,
564 size_t index, SpvId variable, SpvId parent)
565 {
566 b->instructions.words[position + index * 2 + 0] = variable;
567 b->instructions.words[position + index * 2 + 1] = parent;
568 }
569
570 void
571 spirv_builder_emit_kill(struct spirv_builder *b)
572 {
573 spirv_buffer_prepare(&b->instructions, 1);
574 spirv_buffer_emit_word(&b->instructions, SpvOpKill | (1 << 16));
575 }
576
577 SpvId
578 spirv_builder_emit_image_sample(struct spirv_builder *b,
579 SpvId result_type,
580 SpvId sampled_image,
581 SpvId coordinate,
582 bool proj,
583 SpvId lod,
584 SpvId bias,
585 SpvId dref,
586 SpvId dx,
587 SpvId dy,
588 SpvId offset)
589 {
590 SpvId result = spirv_builder_new_id(b);
591
592 int opcode = SpvOpImageSampleImplicitLod;
593 int operands = 5;
594 if (proj)
595 opcode += SpvOpImageSampleProjImplicitLod - SpvOpImageSampleImplicitLod;
596 if (lod || (dx && dy))
597 opcode += SpvOpImageSampleExplicitLod - SpvOpImageSampleImplicitLod;
598 if (dref) {
599 opcode += SpvOpImageSampleDrefImplicitLod - SpvOpImageSampleImplicitLod;
600 operands++;
601 }
602
603 SpvImageOperandsMask operand_mask = SpvImageOperandsMaskNone;
604 SpvId extra_operands[5];
605 int num_extra_operands = 0;
606 if (bias) {
607 extra_operands[++num_extra_operands] = bias;
608 operand_mask |= SpvImageOperandsBiasMask;
609 }
610 if (lod) {
611 extra_operands[++num_extra_operands] = lod;
612 operand_mask |= SpvImageOperandsLodMask;
613 } else if (dx && dy) {
614 extra_operands[++num_extra_operands] = dx;
615 extra_operands[++num_extra_operands] = dy;
616 operand_mask |= SpvImageOperandsGradMask;
617 }
618 if (offset) {
619 extra_operands[++num_extra_operands] = offset;
620 operand_mask |= SpvImageOperandsOffsetMask;
621 }
622
623 /* finalize num_extra_operands / extra_operands */
624 if (num_extra_operands > 0) {
625 extra_operands[0] = operand_mask;
626 num_extra_operands++;
627 }
628
629 spirv_buffer_prepare(&b->instructions, operands + num_extra_operands);
630 spirv_buffer_emit_word(&b->instructions, opcode | ((operands + num_extra_operands) << 16));
631 spirv_buffer_emit_word(&b->instructions, result_type);
632 spirv_buffer_emit_word(&b->instructions, result);
633 spirv_buffer_emit_word(&b->instructions, sampled_image);
634 spirv_buffer_emit_word(&b->instructions, coordinate);
635 if (dref)
636 spirv_buffer_emit_word(&b->instructions, dref);
637 for (int i = 0; i < num_extra_operands; ++i)
638 spirv_buffer_emit_word(&b->instructions, extra_operands[i]);
639 return result;
640 }
641
642 SpvId
643 spirv_builder_emit_image(struct spirv_builder *b, SpvId result_type,
644 SpvId sampled_image)
645 {
646 SpvId result = spirv_builder_new_id(b);
647 spirv_buffer_prepare(&b->instructions, 4);
648 spirv_buffer_emit_word(&b->instructions, SpvOpImage | (4 << 16));
649 spirv_buffer_emit_word(&b->instructions, result_type);
650 spirv_buffer_emit_word(&b->instructions, result);
651 spirv_buffer_emit_word(&b->instructions, sampled_image);
652 return result;
653 }
654
655 SpvId
656 spirv_builder_emit_image_fetch(struct spirv_builder *b,
657 SpvId result_type,
658 SpvId image,
659 SpvId coordinate,
660 SpvId lod,
661 SpvId sample)
662 {
663 SpvId result = spirv_builder_new_id(b);
664
665 SpvImageOperandsMask operand_mask = SpvImageOperandsMaskNone;
666 SpvId extra_operands[3];
667 int num_extra_operands = 0;
668 if (lod) {
669 extra_operands[++num_extra_operands] = lod;
670 operand_mask |= SpvImageOperandsLodMask;
671 }
672 if (sample) {
673 extra_operands[++num_extra_operands] = sample;
674 operand_mask |= SpvImageOperandsSampleMask;
675 }
676
677 /* finalize num_extra_operands / extra_operands */
678 if (num_extra_operands > 0) {
679 extra_operands[0] = operand_mask;
680 num_extra_operands++;
681 }
682
683 spirv_buffer_prepare(&b->instructions, 5 + num_extra_operands);
684 spirv_buffer_emit_word(&b->instructions, SpvOpImageFetch |
685 ((5 + num_extra_operands) << 16));
686 spirv_buffer_emit_word(&b->instructions, result_type);
687 spirv_buffer_emit_word(&b->instructions, result);
688 spirv_buffer_emit_word(&b->instructions, image);
689 spirv_buffer_emit_word(&b->instructions, coordinate);
690 for (int i = 0; i < num_extra_operands; ++i)
691 spirv_buffer_emit_word(&b->instructions, extra_operands[i]);
692 return result;
693 }
694
695 SpvId
696 spirv_builder_emit_image_query_size(struct spirv_builder *b,
697 SpvId result_type,
698 SpvId image,
699 SpvId lod)
700 {
701 int opcode = SpvOpImageQuerySize;
702 int words = 4;
703 if (lod) {
704 words++;
705 opcode = SpvOpImageQuerySizeLod;
706 }
707
708 SpvId result = spirv_builder_new_id(b);
709 spirv_buffer_prepare(&b->instructions, words);
710 spirv_buffer_emit_word(&b->instructions, opcode | (words << 16));
711 spirv_buffer_emit_word(&b->instructions, result_type);
712 spirv_buffer_emit_word(&b->instructions, result);
713 spirv_buffer_emit_word(&b->instructions, image);
714
715 if (lod)
716 spirv_buffer_emit_word(&b->instructions, lod);
717
718 return result;
719 }
720
721 SpvId
722 spirv_builder_emit_ext_inst(struct spirv_builder *b, SpvId result_type,
723 SpvId set, uint32_t instruction,
724 const SpvId *args, size_t num_args)
725 {
726 SpvId result = spirv_builder_new_id(b);
727
728 int words = 5 + num_args;
729 spirv_buffer_prepare(&b->instructions, words);
730 spirv_buffer_emit_word(&b->instructions, SpvOpExtInst | (words << 16));
731 spirv_buffer_emit_word(&b->instructions, result_type);
732 spirv_buffer_emit_word(&b->instructions, result);
733 spirv_buffer_emit_word(&b->instructions, set);
734 spirv_buffer_emit_word(&b->instructions, instruction);
735 for (int i = 0; i < num_args; ++i)
736 spirv_buffer_emit_word(&b->instructions, args[i]);
737 return result;
738 }
739
740 struct spirv_type {
741 SpvOp op;
742 uint32_t args[8];
743 size_t num_args;
744
745 SpvId type;
746 };
747
748 static uint32_t
749 non_aggregate_type_hash(const void *arg)
750 {
751 const struct spirv_type *type = arg;
752
753 uint32_t hash = 0;
754 hash = XXH32(&type->op, sizeof(type->op), hash);
755 hash = XXH32(type->args, sizeof(uint32_t) * type->num_args, hash);
756 return hash;
757 }
758
759 static bool
760 non_aggregate_type_equals(const void *a, const void *b)
761 {
762 const struct spirv_type *ta = a, *tb = b;
763
764 if (ta->op != tb->op)
765 return false;
766
767 assert(ta->num_args == tb->num_args);
768 return memcmp(ta->args, tb->args, sizeof(uint32_t) * ta->num_args) == 0;
769 }
770
771 static SpvId
772 get_type_def(struct spirv_builder *b, SpvOp op, const uint32_t args[],
773 size_t num_args)
774 {
775 /* According to the SPIR-V specification:
776 *
777 * "Two different type <id>s form, by definition, two different types. It
778 * is valid to declare multiple aggregate type <id>s having the same
779 * opcode and operands. This is to allow multiple instances of aggregate
780 * types with the same structure to be decorated differently. (Different
781 * decorations are not required; two different aggregate type <id>s are
782 * allowed to have identical declarations and decorations, and will still
783 * be two different types.) Non-aggregate types are different: It is
784 * invalid to declare multiple type <id>s for the same scalar, vector, or
785 * matrix type. That is, non-aggregate type declarations must all have
786 * different opcodes or operands. (Note that non-aggregate types cannot
787 * be decorated in ways that affect their type.)"
788 *
789 * ..so, we need to prevent the same non-aggregate type to be re-defined
790 * with a new <id>. We do this by putting the definitions in a hash-map, so
791 * we can easily look up and reuse them.
792 */
793
794 struct spirv_type key;
795 assert(num_args <= ARRAY_SIZE(key.args));
796 key.op = op;
797 memcpy(&key.args, args, sizeof(uint32_t) * num_args);
798 key.num_args = num_args;
799
800 struct hash_entry *entry;
801 if (b->types) {
802 entry = _mesa_hash_table_search(b->types, &key);
803 if (entry)
804 return ((struct spirv_type *)entry->data)->type;
805 } else {
806 b->types = _mesa_hash_table_create(NULL, non_aggregate_type_hash,
807 non_aggregate_type_equals);
808 assert(b->types);
809 }
810
811 struct spirv_type *type = CALLOC_STRUCT(spirv_type);
812 if (!type)
813 return 0;
814
815 type->op = op;
816 memcpy(&type->args, args, sizeof(uint32_t) * num_args);
817 type->num_args = num_args;
818
819 type->type = spirv_builder_new_id(b);
820 spirv_buffer_prepare(&b->types_const_defs, 2 + num_args);
821 spirv_buffer_emit_word(&b->types_const_defs, op | ((2 + num_args) << 16));
822 spirv_buffer_emit_word(&b->types_const_defs, type->type);
823 for (int i = 0; i < num_args; ++i)
824 spirv_buffer_emit_word(&b->types_const_defs, args[i]);
825
826 entry = _mesa_hash_table_insert(b->types, type, type);
827 assert(entry);
828
829 return ((struct spirv_type *)entry->data)->type;
830 }
831
832 SpvId
833 spirv_builder_type_void(struct spirv_builder *b)
834 {
835 return get_type_def(b, SpvOpTypeVoid, NULL, 0);
836 }
837
838 SpvId
839 spirv_builder_type_bool(struct spirv_builder *b)
840 {
841 return get_type_def(b, SpvOpTypeBool, NULL, 0);
842 }
843
844 SpvId
845 spirv_builder_type_int(struct spirv_builder *b, unsigned width)
846 {
847 uint32_t args[] = { width, 1 };
848 return get_type_def(b, SpvOpTypeInt, args, ARRAY_SIZE(args));
849 }
850
851 SpvId
852 spirv_builder_type_uint(struct spirv_builder *b, unsigned width)
853 {
854 uint32_t args[] = { width, 0 };
855 return get_type_def(b, SpvOpTypeInt, args, ARRAY_SIZE(args));
856 }
857
858 SpvId
859 spirv_builder_type_float(struct spirv_builder *b, unsigned width)
860 {
861 uint32_t args[] = { width };
862 return get_type_def(b, SpvOpTypeFloat, args, ARRAY_SIZE(args));
863 }
864
865 SpvId
866 spirv_builder_type_image(struct spirv_builder *b, SpvId sampled_type,
867 SpvDim dim, bool depth, bool arrayed, bool ms,
868 unsigned sampled, SpvImageFormat image_format)
869 {
870 assert(sampled < 3);
871 uint32_t args[] = {
872 sampled_type, dim, depth ? 1 : 0, arrayed ? 1 : 0, ms ? 1 : 0, sampled,
873 image_format
874 };
875 return get_type_def(b, SpvOpTypeImage, args, ARRAY_SIZE(args));
876 }
877
878 SpvId
879 spirv_builder_type_sampled_image(struct spirv_builder *b, SpvId image_type)
880 {
881 uint32_t args[] = { image_type };
882 return get_type_def(b, SpvOpTypeSampledImage, args, ARRAY_SIZE(args));
883 }
884
885 SpvId
886 spirv_builder_type_pointer(struct spirv_builder *b,
887 SpvStorageClass storage_class, SpvId type)
888 {
889 uint32_t args[] = { storage_class, type };
890 return get_type_def(b, SpvOpTypePointer, args, ARRAY_SIZE(args));
891 }
892
893 SpvId
894 spirv_builder_type_vector(struct spirv_builder *b, SpvId component_type,
895 unsigned component_count)
896 {
897 assert(component_count > 1);
898 uint32_t args[] = { component_type, component_count };
899 return get_type_def(b, SpvOpTypeVector, args, ARRAY_SIZE(args));
900 }
901
902 SpvId
903 spirv_builder_type_array(struct spirv_builder *b, SpvId component_type,
904 SpvId length)
905 {
906 SpvId type = spirv_builder_new_id(b);
907 spirv_buffer_prepare(&b->types_const_defs, 4);
908 spirv_buffer_emit_word(&b->types_const_defs, SpvOpTypeArray | (4 << 16));
909 spirv_buffer_emit_word(&b->types_const_defs, type);
910 spirv_buffer_emit_word(&b->types_const_defs, component_type);
911 spirv_buffer_emit_word(&b->types_const_defs, length);
912 return type;
913 }
914
915 SpvId
916 spirv_builder_type_struct(struct spirv_builder *b, const SpvId member_types[],
917 size_t num_member_types)
918 {
919 int words = 2 + num_member_types;
920 SpvId type = spirv_builder_new_id(b);
921 spirv_buffer_prepare(&b->types_const_defs, words);
922 spirv_buffer_emit_word(&b->types_const_defs, SpvOpTypeStruct | (words << 16));
923 spirv_buffer_emit_word(&b->types_const_defs, type);
924 for (int i = 0; i < num_member_types; ++i)
925 spirv_buffer_emit_word(&b->types_const_defs, member_types[i]);
926 return type;
927 }
928
929 SpvId
930 spirv_builder_type_function(struct spirv_builder *b, SpvId return_type,
931 const SpvId parameter_types[],
932 size_t num_parameter_types)
933 {
934 int words = 3 + num_parameter_types;
935 SpvId type = spirv_builder_new_id(b);
936 spirv_buffer_prepare(&b->types_const_defs, words);
937 spirv_buffer_emit_word(&b->types_const_defs, SpvOpTypeFunction | (words << 16));
938 spirv_buffer_emit_word(&b->types_const_defs, type);
939 spirv_buffer_emit_word(&b->types_const_defs, return_type);
940 for (int i = 0; i < num_parameter_types; ++i)
941 spirv_buffer_emit_word(&b->types_const_defs, parameter_types[i]);
942 return type;
943 }
944
945 struct spirv_const {
946 SpvOp op, type;
947 uint32_t args[8];
948 size_t num_args;
949
950 SpvId result;
951 };
952
953 static uint32_t
954 const_hash(const void *arg)
955 {
956 const struct spirv_const *key = arg;
957
958 uint32_t hash = 0;
959 hash = XXH32(&key->op, sizeof(key->op), hash);
960 hash = XXH32(&key->type, sizeof(key->type), hash);
961 hash = XXH32(key->args, sizeof(uint32_t) * key->num_args, hash);
962 return hash;
963 }
964
965 static bool
966 const_equals(const void *a, const void *b)
967 {
968 const struct spirv_const *ca = a, *cb = b;
969
970 if (ca->op != cb->op ||
971 ca->type != cb->type)
972 return false;
973
974 assert(ca->num_args == cb->num_args);
975 return memcmp(ca->args, cb->args, sizeof(uint32_t) * ca->num_args) == 0;
976 }
977
978 static SpvId
979 get_const_def(struct spirv_builder *b, SpvOp op, SpvId type,
980 const uint32_t args[], size_t num_args)
981 {
982 struct spirv_const key;
983 assert(num_args <= ARRAY_SIZE(key.args));
984 key.op = op;
985 key.type = type;
986 memcpy(&key.args, args, sizeof(uint32_t) * num_args);
987 key.num_args = num_args;
988
989 struct hash_entry *entry;
990 if (b->consts) {
991 entry = _mesa_hash_table_search(b->consts, &key);
992 if (entry)
993 return ((struct spirv_const *)entry->data)->result;
994 } else {
995 b->consts = _mesa_hash_table_create(NULL, const_hash, const_equals);
996 assert(b->consts);
997 }
998
999 struct spirv_const *cnst = CALLOC_STRUCT(spirv_const);
1000 if (!cnst)
1001 return 0;
1002
1003 cnst->op = op;
1004 cnst->type = type;
1005 memcpy(&cnst->args, args, sizeof(uint32_t) * num_args);
1006 cnst->num_args = num_args;
1007
1008 cnst->result = spirv_builder_new_id(b);
1009 spirv_buffer_prepare(&b->types_const_defs, 3 + num_args);
1010 spirv_buffer_emit_word(&b->types_const_defs, op | ((3 + num_args) << 16));
1011 spirv_buffer_emit_word(&b->types_const_defs, type);
1012 spirv_buffer_emit_word(&b->types_const_defs, cnst->result);
1013 for (int i = 0; i < num_args; ++i)
1014 spirv_buffer_emit_word(&b->types_const_defs, args[i]);
1015
1016 entry = _mesa_hash_table_insert(b->consts, cnst, cnst);
1017 assert(entry);
1018
1019 return ((struct spirv_const *)entry->data)->result;
1020 }
1021
1022 SpvId
1023 spirv_builder_const_bool(struct spirv_builder *b, bool val)
1024 {
1025 return get_const_def(b, val ? SpvOpConstantTrue : SpvOpConstantFalse,
1026 spirv_builder_type_bool(b), NULL, 0);
1027 }
1028
1029 SpvId
1030 spirv_builder_const_int(struct spirv_builder *b, int width, int32_t val)
1031 {
1032 assert(width <= 32);
1033 uint32_t args[] = { val };
1034 return get_const_def(b, SpvOpConstant, spirv_builder_type_int(b, width),
1035 args, ARRAY_SIZE(args));
1036 }
1037
1038 SpvId
1039 spirv_builder_const_uint(struct spirv_builder *b, int width, uint32_t val)
1040 {
1041 assert(width <= 32);
1042 uint32_t args[] = { val };
1043 return get_const_def(b, SpvOpConstant, spirv_builder_type_uint(b, width),
1044 args, ARRAY_SIZE(args));
1045 }
1046
1047 SpvId
1048 spirv_builder_const_float(struct spirv_builder *b, int width, float val)
1049 {
1050 assert(width <= 32);
1051 uint32_t args[] = { u_bitcast_f2u(val) };
1052 return get_const_def(b, SpvOpConstant, spirv_builder_type_float(b, width),
1053 args, ARRAY_SIZE(args));
1054 }
1055
1056 SpvId
1057 spirv_builder_const_composite(struct spirv_builder *b, SpvId result_type,
1058 const SpvId constituents[],
1059 size_t num_constituents)
1060 {
1061 return get_const_def(b, SpvOpConstantComposite, result_type,
1062 (const uint32_t *)constituents,
1063 num_constituents);
1064 }
1065
1066 SpvId
1067 spirv_builder_emit_var(struct spirv_builder *b, SpvId type,
1068 SpvStorageClass storage_class)
1069 {
1070 assert(storage_class != SpvStorageClassGeneric);
1071 struct spirv_buffer *buf = storage_class != SpvStorageClassFunction ?
1072 &b->types_const_defs : &b->instructions;
1073
1074 SpvId ret = spirv_builder_new_id(b);
1075 spirv_buffer_prepare(buf, 4);
1076 spirv_buffer_emit_word(buf, SpvOpVariable | (4 << 16));
1077 spirv_buffer_emit_word(buf, type);
1078 spirv_buffer_emit_word(buf, ret);
1079 spirv_buffer_emit_word(buf, storage_class);
1080 return ret;
1081 }
1082
1083 SpvId
1084 spirv_builder_import(struct spirv_builder *b, const char *name)
1085 {
1086 SpvId result = spirv_builder_new_id(b);
1087 size_t pos = b->imports.num_words;
1088 spirv_buffer_prepare(&b->imports, 2);
1089 spirv_buffer_emit_word(&b->imports, SpvOpExtInstImport);
1090 spirv_buffer_emit_word(&b->imports, result);
1091 int len = spirv_buffer_emit_string(&b->imports, name);
1092 b->imports.words[pos] |= (2 + len) << 16;
1093 return result;
1094 }
1095
1096 size_t
1097 spirv_builder_get_num_words(struct spirv_builder *b)
1098 {
1099 const size_t header_size = 5;
1100 return header_size +
1101 b->capabilities.num_words +
1102 b->imports.num_words +
1103 b->memory_model.num_words +
1104 b->entry_points.num_words +
1105 b->exec_modes.num_words +
1106 b->debug_names.num_words +
1107 b->decorations.num_words +
1108 b->types_const_defs.num_words +
1109 b->instructions.num_words;
1110 }
1111
1112 size_t
1113 spirv_builder_get_words(struct spirv_builder *b, uint32_t *words,
1114 size_t num_words)
1115 {
1116 assert(num_words >= spirv_builder_get_num_words(b));
1117
1118 size_t written = 0;
1119 words[written++] = SpvMagicNumber;
1120 words[written++] = 0x00010000;
1121 words[written++] = 0;
1122 words[written++] = b->prev_id + 1;
1123 words[written++] = 0;
1124
1125 const struct spirv_buffer *buffers[] = {
1126 &b->capabilities,
1127 &b->imports,
1128 &b->memory_model,
1129 &b->entry_points,
1130 &b->exec_modes,
1131 &b->debug_names,
1132 &b->decorations,
1133 &b->types_const_defs,
1134 &b->instructions
1135 };
1136
1137 for (int i = 0; i < ARRAY_SIZE(buffers); ++i) {
1138 const struct spirv_buffer *buffer = buffers[i];
1139 for (int j = 0; j < buffer->num_words; ++j)
1140 words[written++] = buffer->words[j];
1141 }
1142
1143 assert(written == spirv_builder_get_num_words(b));
1144 return written;
1145 }