spirv: Add support for SPV_GOOGLE_decorate_string
[mesa.git] / src / compiler / spirv / spirv_to_nir.c
1 /*
2 * Copyright © 2015 Intel Corporation
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 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * 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 NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Jason Ekstrand (jason@jlekstrand.net)
25 *
26 */
27
28 #include "vtn_private.h"
29 #include "nir/nir_vla.h"
30 #include "nir/nir_control_flow.h"
31 #include "nir/nir_constant_expressions.h"
32 #include "nir/nir_deref.h"
33 #include "spirv_info.h"
34
35 #include <stdio.h>
36
37 void
38 vtn_log(struct vtn_builder *b, enum nir_spirv_debug_level level,
39 size_t spirv_offset, const char *message)
40 {
41 if (b->options->debug.func) {
42 b->options->debug.func(b->options->debug.private_data,
43 level, spirv_offset, message);
44 }
45
46 #ifndef NDEBUG
47 if (level >= NIR_SPIRV_DEBUG_LEVEL_WARNING)
48 fprintf(stderr, "%s\n", message);
49 #endif
50 }
51
52 void
53 vtn_logf(struct vtn_builder *b, enum nir_spirv_debug_level level,
54 size_t spirv_offset, const char *fmt, ...)
55 {
56 va_list args;
57 char *msg;
58
59 va_start(args, fmt);
60 msg = ralloc_vasprintf(NULL, fmt, args);
61 va_end(args);
62
63 vtn_log(b, level, spirv_offset, msg);
64
65 ralloc_free(msg);
66 }
67
68 static void
69 vtn_log_err(struct vtn_builder *b,
70 enum nir_spirv_debug_level level, const char *prefix,
71 const char *file, unsigned line,
72 const char *fmt, va_list args)
73 {
74 char *msg;
75
76 msg = ralloc_strdup(NULL, prefix);
77
78 #ifndef NDEBUG
79 ralloc_asprintf_append(&msg, " In file %s:%u\n", file, line);
80 #endif
81
82 ralloc_asprintf_append(&msg, " ");
83
84 ralloc_vasprintf_append(&msg, fmt, args);
85
86 ralloc_asprintf_append(&msg, "\n %zu bytes into the SPIR-V binary",
87 b->spirv_offset);
88
89 if (b->file) {
90 ralloc_asprintf_append(&msg,
91 "\n in SPIR-V source file %s, line %d, col %d",
92 b->file, b->line, b->col);
93 }
94
95 vtn_log(b, level, b->spirv_offset, msg);
96
97 ralloc_free(msg);
98 }
99
100 static void
101 vtn_dump_shader(struct vtn_builder *b, const char *path, const char *prefix)
102 {
103 static int idx = 0;
104
105 char filename[1024];
106 int len = snprintf(filename, sizeof(filename), "%s/%s-%d.spirv",
107 path, prefix, idx++);
108 if (len < 0 || len >= sizeof(filename))
109 return;
110
111 FILE *f = fopen(filename, "w");
112 if (f == NULL)
113 return;
114
115 fwrite(b->spirv, sizeof(*b->spirv), b->spirv_word_count, f);
116 fclose(f);
117
118 vtn_info("SPIR-V shader dumped to %s", filename);
119 }
120
121 void
122 _vtn_warn(struct vtn_builder *b, const char *file, unsigned line,
123 const char *fmt, ...)
124 {
125 va_list args;
126
127 va_start(args, fmt);
128 vtn_log_err(b, NIR_SPIRV_DEBUG_LEVEL_WARNING, "SPIR-V WARNING:\n",
129 file, line, fmt, args);
130 va_end(args);
131 }
132
133 void
134 _vtn_err(struct vtn_builder *b, const char *file, unsigned line,
135 const char *fmt, ...)
136 {
137 va_list args;
138
139 va_start(args, fmt);
140 vtn_log_err(b, NIR_SPIRV_DEBUG_LEVEL_ERROR, "SPIR-V ERROR:\n",
141 file, line, fmt, args);
142 va_end(args);
143 }
144
145 void
146 _vtn_fail(struct vtn_builder *b, const char *file, unsigned line,
147 const char *fmt, ...)
148 {
149 va_list args;
150
151 va_start(args, fmt);
152 vtn_log_err(b, NIR_SPIRV_DEBUG_LEVEL_ERROR, "SPIR-V parsing FAILED:\n",
153 file, line, fmt, args);
154 va_end(args);
155
156 const char *dump_path = getenv("MESA_SPIRV_FAIL_DUMP_PATH");
157 if (dump_path)
158 vtn_dump_shader(b, dump_path, "fail");
159
160 longjmp(b->fail_jump, 1);
161 }
162
163 struct spec_constant_value {
164 bool is_double;
165 union {
166 uint32_t data32;
167 uint64_t data64;
168 };
169 };
170
171 static struct vtn_ssa_value *
172 vtn_undef_ssa_value(struct vtn_builder *b, const struct glsl_type *type)
173 {
174 struct vtn_ssa_value *val = rzalloc(b, struct vtn_ssa_value);
175 val->type = type;
176
177 if (glsl_type_is_vector_or_scalar(type)) {
178 unsigned num_components = glsl_get_vector_elements(val->type);
179 unsigned bit_size = glsl_get_bit_size(val->type);
180 val->def = nir_ssa_undef(&b->nb, num_components, bit_size);
181 } else {
182 unsigned elems = glsl_get_length(val->type);
183 val->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
184 if (glsl_type_is_matrix(type)) {
185 const struct glsl_type *elem_type =
186 glsl_vector_type(glsl_get_base_type(type),
187 glsl_get_vector_elements(type));
188
189 for (unsigned i = 0; i < elems; i++)
190 val->elems[i] = vtn_undef_ssa_value(b, elem_type);
191 } else if (glsl_type_is_array(type)) {
192 const struct glsl_type *elem_type = glsl_get_array_element(type);
193 for (unsigned i = 0; i < elems; i++)
194 val->elems[i] = vtn_undef_ssa_value(b, elem_type);
195 } else {
196 for (unsigned i = 0; i < elems; i++) {
197 const struct glsl_type *elem_type = glsl_get_struct_field(type, i);
198 val->elems[i] = vtn_undef_ssa_value(b, elem_type);
199 }
200 }
201 }
202
203 return val;
204 }
205
206 static struct vtn_ssa_value *
207 vtn_const_ssa_value(struct vtn_builder *b, nir_constant *constant,
208 const struct glsl_type *type)
209 {
210 struct hash_entry *entry = _mesa_hash_table_search(b->const_table, constant);
211
212 if (entry)
213 return entry->data;
214
215 struct vtn_ssa_value *val = rzalloc(b, struct vtn_ssa_value);
216 val->type = type;
217
218 switch (glsl_get_base_type(type)) {
219 case GLSL_TYPE_INT:
220 case GLSL_TYPE_UINT:
221 case GLSL_TYPE_INT16:
222 case GLSL_TYPE_UINT16:
223 case GLSL_TYPE_UINT8:
224 case GLSL_TYPE_INT8:
225 case GLSL_TYPE_INT64:
226 case GLSL_TYPE_UINT64:
227 case GLSL_TYPE_BOOL:
228 case GLSL_TYPE_FLOAT:
229 case GLSL_TYPE_FLOAT16:
230 case GLSL_TYPE_DOUBLE: {
231 int bit_size = glsl_get_bit_size(type);
232 if (glsl_type_is_vector_or_scalar(type)) {
233 unsigned num_components = glsl_get_vector_elements(val->type);
234 nir_load_const_instr *load =
235 nir_load_const_instr_create(b->shader, num_components, bit_size);
236
237 load->value = constant->values[0];
238
239 nir_instr_insert_before_cf_list(&b->nb.impl->body, &load->instr);
240 val->def = &load->def;
241 } else {
242 assert(glsl_type_is_matrix(type));
243 unsigned rows = glsl_get_vector_elements(val->type);
244 unsigned columns = glsl_get_matrix_columns(val->type);
245 val->elems = ralloc_array(b, struct vtn_ssa_value *, columns);
246
247 for (unsigned i = 0; i < columns; i++) {
248 struct vtn_ssa_value *col_val = rzalloc(b, struct vtn_ssa_value);
249 col_val->type = glsl_get_column_type(val->type);
250 nir_load_const_instr *load =
251 nir_load_const_instr_create(b->shader, rows, bit_size);
252
253 load->value = constant->values[i];
254
255 nir_instr_insert_before_cf_list(&b->nb.impl->body, &load->instr);
256 col_val->def = &load->def;
257
258 val->elems[i] = col_val;
259 }
260 }
261 break;
262 }
263
264 case GLSL_TYPE_ARRAY: {
265 unsigned elems = glsl_get_length(val->type);
266 val->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
267 const struct glsl_type *elem_type = glsl_get_array_element(val->type);
268 for (unsigned i = 0; i < elems; i++)
269 val->elems[i] = vtn_const_ssa_value(b, constant->elements[i],
270 elem_type);
271 break;
272 }
273
274 case GLSL_TYPE_STRUCT: {
275 unsigned elems = glsl_get_length(val->type);
276 val->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
277 for (unsigned i = 0; i < elems; i++) {
278 const struct glsl_type *elem_type =
279 glsl_get_struct_field(val->type, i);
280 val->elems[i] = vtn_const_ssa_value(b, constant->elements[i],
281 elem_type);
282 }
283 break;
284 }
285
286 default:
287 vtn_fail("bad constant type");
288 }
289
290 return val;
291 }
292
293 struct vtn_ssa_value *
294 vtn_ssa_value(struct vtn_builder *b, uint32_t value_id)
295 {
296 struct vtn_value *val = vtn_untyped_value(b, value_id);
297 switch (val->value_type) {
298 case vtn_value_type_undef:
299 return vtn_undef_ssa_value(b, val->type->type);
300
301 case vtn_value_type_constant:
302 return vtn_const_ssa_value(b, val->constant, val->type->type);
303
304 case vtn_value_type_ssa:
305 return val->ssa;
306
307 case vtn_value_type_pointer:
308 vtn_assert(val->pointer->ptr_type && val->pointer->ptr_type->type);
309 struct vtn_ssa_value *ssa =
310 vtn_create_ssa_value(b, val->pointer->ptr_type->type);
311 ssa->def = vtn_pointer_to_ssa(b, val->pointer);
312 return ssa;
313
314 default:
315 vtn_fail("Invalid type for an SSA value");
316 }
317 }
318
319 static char *
320 vtn_string_literal(struct vtn_builder *b, const uint32_t *words,
321 unsigned word_count, unsigned *words_used)
322 {
323 char *dup = ralloc_strndup(b, (char *)words, word_count * sizeof(*words));
324 if (words_used) {
325 /* Ammount of space taken by the string (including the null) */
326 unsigned len = strlen(dup) + 1;
327 *words_used = DIV_ROUND_UP(len, sizeof(*words));
328 }
329 return dup;
330 }
331
332 const uint32_t *
333 vtn_foreach_instruction(struct vtn_builder *b, const uint32_t *start,
334 const uint32_t *end, vtn_instruction_handler handler)
335 {
336 b->file = NULL;
337 b->line = -1;
338 b->col = -1;
339
340 const uint32_t *w = start;
341 while (w < end) {
342 SpvOp opcode = w[0] & SpvOpCodeMask;
343 unsigned count = w[0] >> SpvWordCountShift;
344 vtn_assert(count >= 1 && w + count <= end);
345
346 b->spirv_offset = (uint8_t *)w - (uint8_t *)b->spirv;
347
348 switch (opcode) {
349 case SpvOpNop:
350 break; /* Do nothing */
351
352 case SpvOpLine:
353 b->file = vtn_value(b, w[1], vtn_value_type_string)->str;
354 b->line = w[2];
355 b->col = w[3];
356 break;
357
358 case SpvOpNoLine:
359 b->file = NULL;
360 b->line = -1;
361 b->col = -1;
362 break;
363
364 default:
365 if (!handler(b, opcode, w, count))
366 return w;
367 break;
368 }
369
370 w += count;
371 }
372
373 b->spirv_offset = 0;
374 b->file = NULL;
375 b->line = -1;
376 b->col = -1;
377
378 assert(w == end);
379 return w;
380 }
381
382 static void
383 vtn_handle_extension(struct vtn_builder *b, SpvOp opcode,
384 const uint32_t *w, unsigned count)
385 {
386 const char *ext = (const char *)&w[2];
387 switch (opcode) {
388 case SpvOpExtInstImport: {
389 struct vtn_value *val = vtn_push_value(b, w[1], vtn_value_type_extension);
390 if (strcmp(ext, "GLSL.std.450") == 0) {
391 val->ext_handler = vtn_handle_glsl450_instruction;
392 } else if ((strcmp(ext, "SPV_AMD_gcn_shader") == 0)
393 && (b->options && b->options->caps.gcn_shader)) {
394 val->ext_handler = vtn_handle_amd_gcn_shader_instruction;
395 } else if ((strcmp(ext, "SPV_AMD_shader_trinary_minmax") == 0)
396 && (b->options && b->options->caps.trinary_minmax)) {
397 val->ext_handler = vtn_handle_amd_shader_trinary_minmax_instruction;
398 } else {
399 vtn_fail("Unsupported extension: %s", ext);
400 }
401 break;
402 }
403
404 case SpvOpExtInst: {
405 struct vtn_value *val = vtn_value(b, w[3], vtn_value_type_extension);
406 bool handled = val->ext_handler(b, w[4], w, count);
407 vtn_assert(handled);
408 break;
409 }
410
411 default:
412 vtn_fail("Unhandled opcode");
413 }
414 }
415
416 static void
417 _foreach_decoration_helper(struct vtn_builder *b,
418 struct vtn_value *base_value,
419 int parent_member,
420 struct vtn_value *value,
421 vtn_decoration_foreach_cb cb, void *data)
422 {
423 for (struct vtn_decoration *dec = value->decoration; dec; dec = dec->next) {
424 int member;
425 if (dec->scope == VTN_DEC_DECORATION) {
426 member = parent_member;
427 } else if (dec->scope >= VTN_DEC_STRUCT_MEMBER0) {
428 vtn_fail_if(value->value_type != vtn_value_type_type ||
429 value->type->base_type != vtn_base_type_struct,
430 "OpMemberDecorate and OpGroupMemberDecorate are only "
431 "allowed on OpTypeStruct");
432 /* This means we haven't recursed yet */
433 assert(value == base_value);
434
435 member = dec->scope - VTN_DEC_STRUCT_MEMBER0;
436
437 vtn_fail_if(member >= base_value->type->length,
438 "OpMemberDecorate specifies member %d but the "
439 "OpTypeStruct has only %u members",
440 member, base_value->type->length);
441 } else {
442 /* Not a decoration */
443 assert(dec->scope == VTN_DEC_EXECUTION_MODE);
444 continue;
445 }
446
447 if (dec->group) {
448 assert(dec->group->value_type == vtn_value_type_decoration_group);
449 _foreach_decoration_helper(b, base_value, member, dec->group,
450 cb, data);
451 } else {
452 cb(b, base_value, member, dec, data);
453 }
454 }
455 }
456
457 /** Iterates (recursively if needed) over all of the decorations on a value
458 *
459 * This function iterates over all of the decorations applied to a given
460 * value. If it encounters a decoration group, it recurses into the group
461 * and iterates over all of those decorations as well.
462 */
463 void
464 vtn_foreach_decoration(struct vtn_builder *b, struct vtn_value *value,
465 vtn_decoration_foreach_cb cb, void *data)
466 {
467 _foreach_decoration_helper(b, value, -1, value, cb, data);
468 }
469
470 void
471 vtn_foreach_execution_mode(struct vtn_builder *b, struct vtn_value *value,
472 vtn_execution_mode_foreach_cb cb, void *data)
473 {
474 for (struct vtn_decoration *dec = value->decoration; dec; dec = dec->next) {
475 if (dec->scope != VTN_DEC_EXECUTION_MODE)
476 continue;
477
478 assert(dec->group == NULL);
479 cb(b, value, dec, data);
480 }
481 }
482
483 void
484 vtn_handle_decoration(struct vtn_builder *b, SpvOp opcode,
485 const uint32_t *w, unsigned count)
486 {
487 const uint32_t *w_end = w + count;
488 const uint32_t target = w[1];
489 w += 2;
490
491 switch (opcode) {
492 case SpvOpDecorationGroup:
493 vtn_push_value(b, target, vtn_value_type_decoration_group);
494 break;
495
496 case SpvOpDecorate:
497 case SpvOpMemberDecorate:
498 case SpvOpDecorateStringGOOGLE:
499 case SpvOpMemberDecorateStringGOOGLE:
500 case SpvOpExecutionMode: {
501 struct vtn_value *val = vtn_untyped_value(b, target);
502
503 struct vtn_decoration *dec = rzalloc(b, struct vtn_decoration);
504 switch (opcode) {
505 case SpvOpDecorate:
506 case SpvOpDecorateStringGOOGLE:
507 dec->scope = VTN_DEC_DECORATION;
508 break;
509 case SpvOpMemberDecorate:
510 case SpvOpMemberDecorateStringGOOGLE:
511 dec->scope = VTN_DEC_STRUCT_MEMBER0 + *(w++);
512 vtn_fail_if(dec->scope < VTN_DEC_STRUCT_MEMBER0, /* overflow */
513 "Member argument of OpMemberDecorate too large");
514 break;
515 case SpvOpExecutionMode:
516 dec->scope = VTN_DEC_EXECUTION_MODE;
517 break;
518 default:
519 unreachable("Invalid decoration opcode");
520 }
521 dec->decoration = *(w++);
522 dec->literals = w;
523
524 /* Link into the list */
525 dec->next = val->decoration;
526 val->decoration = dec;
527 break;
528 }
529
530 case SpvOpGroupMemberDecorate:
531 case SpvOpGroupDecorate: {
532 struct vtn_value *group =
533 vtn_value(b, target, vtn_value_type_decoration_group);
534
535 for (; w < w_end; w++) {
536 struct vtn_value *val = vtn_untyped_value(b, *w);
537 struct vtn_decoration *dec = rzalloc(b, struct vtn_decoration);
538
539 dec->group = group;
540 if (opcode == SpvOpGroupDecorate) {
541 dec->scope = VTN_DEC_DECORATION;
542 } else {
543 dec->scope = VTN_DEC_STRUCT_MEMBER0 + *(++w);
544 vtn_fail_if(dec->scope < 0, /* Check for overflow */
545 "Member argument of OpGroupMemberDecorate too large");
546 }
547
548 /* Link into the list */
549 dec->next = val->decoration;
550 val->decoration = dec;
551 }
552 break;
553 }
554
555 default:
556 unreachable("Unhandled opcode");
557 }
558 }
559
560 struct member_decoration_ctx {
561 unsigned num_fields;
562 struct glsl_struct_field *fields;
563 struct vtn_type *type;
564 };
565
566 /** Returns true if two types are "compatible", i.e. you can do an OpLoad,
567 * OpStore, or OpCopyMemory between them without breaking anything.
568 * Technically, the SPIR-V rules require the exact same type ID but this lets
569 * us internally be a bit looser.
570 */
571 bool
572 vtn_types_compatible(struct vtn_builder *b,
573 struct vtn_type *t1, struct vtn_type *t2)
574 {
575 if (t1->id == t2->id)
576 return true;
577
578 if (t1->base_type != t2->base_type)
579 return false;
580
581 switch (t1->base_type) {
582 case vtn_base_type_void:
583 case vtn_base_type_scalar:
584 case vtn_base_type_vector:
585 case vtn_base_type_matrix:
586 case vtn_base_type_image:
587 case vtn_base_type_sampler:
588 case vtn_base_type_sampled_image:
589 return t1->type == t2->type;
590
591 case vtn_base_type_array:
592 return t1->length == t2->length &&
593 vtn_types_compatible(b, t1->array_element, t2->array_element);
594
595 case vtn_base_type_pointer:
596 return vtn_types_compatible(b, t1->deref, t2->deref);
597
598 case vtn_base_type_struct:
599 if (t1->length != t2->length)
600 return false;
601
602 for (unsigned i = 0; i < t1->length; i++) {
603 if (!vtn_types_compatible(b, t1->members[i], t2->members[i]))
604 return false;
605 }
606 return true;
607
608 case vtn_base_type_function:
609 /* This case shouldn't get hit since you can't copy around function
610 * types. Just require them to be identical.
611 */
612 return false;
613 }
614
615 vtn_fail("Invalid base type");
616 }
617
618 /* does a shallow copy of a vtn_type */
619
620 static struct vtn_type *
621 vtn_type_copy(struct vtn_builder *b, struct vtn_type *src)
622 {
623 struct vtn_type *dest = ralloc(b, struct vtn_type);
624 *dest = *src;
625
626 switch (src->base_type) {
627 case vtn_base_type_void:
628 case vtn_base_type_scalar:
629 case vtn_base_type_vector:
630 case vtn_base_type_matrix:
631 case vtn_base_type_array:
632 case vtn_base_type_pointer:
633 case vtn_base_type_image:
634 case vtn_base_type_sampler:
635 case vtn_base_type_sampled_image:
636 /* Nothing more to do */
637 break;
638
639 case vtn_base_type_struct:
640 dest->members = ralloc_array(b, struct vtn_type *, src->length);
641 memcpy(dest->members, src->members,
642 src->length * sizeof(src->members[0]));
643
644 dest->offsets = ralloc_array(b, unsigned, src->length);
645 memcpy(dest->offsets, src->offsets,
646 src->length * sizeof(src->offsets[0]));
647 break;
648
649 case vtn_base_type_function:
650 dest->params = ralloc_array(b, struct vtn_type *, src->length);
651 memcpy(dest->params, src->params, src->length * sizeof(src->params[0]));
652 break;
653 }
654
655 return dest;
656 }
657
658 static struct vtn_type *
659 mutable_matrix_member(struct vtn_builder *b, struct vtn_type *type, int member)
660 {
661 type->members[member] = vtn_type_copy(b, type->members[member]);
662 type = type->members[member];
663
664 /* We may have an array of matrices.... Oh, joy! */
665 while (glsl_type_is_array(type->type)) {
666 type->array_element = vtn_type_copy(b, type->array_element);
667 type = type->array_element;
668 }
669
670 vtn_assert(glsl_type_is_matrix(type->type));
671
672 return type;
673 }
674
675 static void
676 vtn_handle_access_qualifier(struct vtn_builder *b, struct vtn_type *type,
677 int member, enum gl_access_qualifier access)
678 {
679 type->members[member] = vtn_type_copy(b, type->members[member]);
680 type = type->members[member];
681
682 type->access |= access;
683 }
684
685 static void
686 struct_member_decoration_cb(struct vtn_builder *b,
687 struct vtn_value *val, int member,
688 const struct vtn_decoration *dec, void *void_ctx)
689 {
690 struct member_decoration_ctx *ctx = void_ctx;
691
692 if (member < 0)
693 return;
694
695 assert(member < ctx->num_fields);
696
697 switch (dec->decoration) {
698 case SpvDecorationRelaxedPrecision:
699 case SpvDecorationUniform:
700 break; /* FIXME: Do nothing with this for now. */
701 case SpvDecorationNonWritable:
702 vtn_handle_access_qualifier(b, ctx->type, member, ACCESS_NON_WRITEABLE);
703 break;
704 case SpvDecorationNonReadable:
705 vtn_handle_access_qualifier(b, ctx->type, member, ACCESS_NON_READABLE);
706 break;
707 case SpvDecorationVolatile:
708 vtn_handle_access_qualifier(b, ctx->type, member, ACCESS_VOLATILE);
709 break;
710 case SpvDecorationCoherent:
711 vtn_handle_access_qualifier(b, ctx->type, member, ACCESS_COHERENT);
712 break;
713 case SpvDecorationNoPerspective:
714 ctx->fields[member].interpolation = INTERP_MODE_NOPERSPECTIVE;
715 break;
716 case SpvDecorationFlat:
717 ctx->fields[member].interpolation = INTERP_MODE_FLAT;
718 break;
719 case SpvDecorationCentroid:
720 ctx->fields[member].centroid = true;
721 break;
722 case SpvDecorationSample:
723 ctx->fields[member].sample = true;
724 break;
725 case SpvDecorationStream:
726 /* Vulkan only allows one GS stream */
727 vtn_assert(dec->literals[0] == 0);
728 break;
729 case SpvDecorationLocation:
730 ctx->fields[member].location = dec->literals[0];
731 break;
732 case SpvDecorationComponent:
733 break; /* FIXME: What should we do with these? */
734 case SpvDecorationBuiltIn:
735 ctx->type->members[member] = vtn_type_copy(b, ctx->type->members[member]);
736 ctx->type->members[member]->is_builtin = true;
737 ctx->type->members[member]->builtin = dec->literals[0];
738 ctx->type->builtin_block = true;
739 break;
740 case SpvDecorationOffset:
741 ctx->type->offsets[member] = dec->literals[0];
742 break;
743 case SpvDecorationMatrixStride:
744 /* Handled as a second pass */
745 break;
746 case SpvDecorationColMajor:
747 break; /* Nothing to do here. Column-major is the default. */
748 case SpvDecorationRowMajor:
749 mutable_matrix_member(b, ctx->type, member)->row_major = true;
750 break;
751
752 case SpvDecorationPatch:
753 break;
754
755 case SpvDecorationSpecId:
756 case SpvDecorationBlock:
757 case SpvDecorationBufferBlock:
758 case SpvDecorationArrayStride:
759 case SpvDecorationGLSLShared:
760 case SpvDecorationGLSLPacked:
761 case SpvDecorationInvariant:
762 case SpvDecorationRestrict:
763 case SpvDecorationAliased:
764 case SpvDecorationConstant:
765 case SpvDecorationIndex:
766 case SpvDecorationBinding:
767 case SpvDecorationDescriptorSet:
768 case SpvDecorationLinkageAttributes:
769 case SpvDecorationNoContraction:
770 case SpvDecorationInputAttachmentIndex:
771 vtn_warn("Decoration not allowed on struct members: %s",
772 spirv_decoration_to_string(dec->decoration));
773 break;
774
775 case SpvDecorationXfbBuffer:
776 case SpvDecorationXfbStride:
777 vtn_warn("Vulkan does not have transform feedback");
778 break;
779
780 case SpvDecorationCPacked:
781 case SpvDecorationSaturatedConversion:
782 case SpvDecorationFuncParamAttr:
783 case SpvDecorationFPRoundingMode:
784 case SpvDecorationFPFastMathMode:
785 case SpvDecorationAlignment:
786 vtn_warn("Decoration only allowed for CL-style kernels: %s",
787 spirv_decoration_to_string(dec->decoration));
788 break;
789
790 default:
791 vtn_fail("Unhandled decoration");
792 }
793 }
794
795 /* Matrix strides are handled as a separate pass because we need to know
796 * whether the matrix is row-major or not first.
797 */
798 static void
799 struct_member_matrix_stride_cb(struct vtn_builder *b,
800 struct vtn_value *val, int member,
801 const struct vtn_decoration *dec,
802 void *void_ctx)
803 {
804 if (dec->decoration != SpvDecorationMatrixStride)
805 return;
806
807 vtn_fail_if(member < 0,
808 "The MatrixStride decoration is only allowed on members "
809 "of OpTypeStruct");
810
811 struct member_decoration_ctx *ctx = void_ctx;
812
813 struct vtn_type *mat_type = mutable_matrix_member(b, ctx->type, member);
814 if (mat_type->row_major) {
815 mat_type->array_element = vtn_type_copy(b, mat_type->array_element);
816 mat_type->stride = mat_type->array_element->stride;
817 mat_type->array_element->stride = dec->literals[0];
818 } else {
819 vtn_assert(mat_type->array_element->stride > 0);
820 mat_type->stride = dec->literals[0];
821 }
822 }
823
824 static void
825 type_decoration_cb(struct vtn_builder *b,
826 struct vtn_value *val, int member,
827 const struct vtn_decoration *dec, void *ctx)
828 {
829 struct vtn_type *type = val->type;
830
831 if (member != -1) {
832 /* This should have been handled by OpTypeStruct */
833 assert(val->type->base_type == vtn_base_type_struct);
834 assert(member >= 0 && member < val->type->length);
835 return;
836 }
837
838 switch (dec->decoration) {
839 case SpvDecorationArrayStride:
840 vtn_assert(type->base_type == vtn_base_type_matrix ||
841 type->base_type == vtn_base_type_array ||
842 type->base_type == vtn_base_type_pointer);
843 type->stride = dec->literals[0];
844 break;
845 case SpvDecorationBlock:
846 vtn_assert(type->base_type == vtn_base_type_struct);
847 type->block = true;
848 break;
849 case SpvDecorationBufferBlock:
850 vtn_assert(type->base_type == vtn_base_type_struct);
851 type->buffer_block = true;
852 break;
853 case SpvDecorationGLSLShared:
854 case SpvDecorationGLSLPacked:
855 /* Ignore these, since we get explicit offsets anyways */
856 break;
857
858 case SpvDecorationRowMajor:
859 case SpvDecorationColMajor:
860 case SpvDecorationMatrixStride:
861 case SpvDecorationBuiltIn:
862 case SpvDecorationNoPerspective:
863 case SpvDecorationFlat:
864 case SpvDecorationPatch:
865 case SpvDecorationCentroid:
866 case SpvDecorationSample:
867 case SpvDecorationVolatile:
868 case SpvDecorationCoherent:
869 case SpvDecorationNonWritable:
870 case SpvDecorationNonReadable:
871 case SpvDecorationUniform:
872 case SpvDecorationLocation:
873 case SpvDecorationComponent:
874 case SpvDecorationOffset:
875 case SpvDecorationXfbBuffer:
876 case SpvDecorationXfbStride:
877 vtn_warn("Decoration only allowed for struct members: %s",
878 spirv_decoration_to_string(dec->decoration));
879 break;
880
881 case SpvDecorationStream:
882 /* We don't need to do anything here, as stream is filled up when
883 * aplying the decoration to a variable, just check that if it is not a
884 * struct member, it should be a struct.
885 */
886 vtn_assert(type->base_type == vtn_base_type_struct);
887 break;
888
889 case SpvDecorationRelaxedPrecision:
890 case SpvDecorationSpecId:
891 case SpvDecorationInvariant:
892 case SpvDecorationRestrict:
893 case SpvDecorationAliased:
894 case SpvDecorationConstant:
895 case SpvDecorationIndex:
896 case SpvDecorationBinding:
897 case SpvDecorationDescriptorSet:
898 case SpvDecorationLinkageAttributes:
899 case SpvDecorationNoContraction:
900 case SpvDecorationInputAttachmentIndex:
901 vtn_warn("Decoration not allowed on types: %s",
902 spirv_decoration_to_string(dec->decoration));
903 break;
904
905 case SpvDecorationCPacked:
906 case SpvDecorationSaturatedConversion:
907 case SpvDecorationFuncParamAttr:
908 case SpvDecorationFPRoundingMode:
909 case SpvDecorationFPFastMathMode:
910 case SpvDecorationAlignment:
911 vtn_warn("Decoration only allowed for CL-style kernels: %s",
912 spirv_decoration_to_string(dec->decoration));
913 break;
914
915 default:
916 vtn_fail("Unhandled decoration");
917 }
918 }
919
920 static unsigned
921 translate_image_format(struct vtn_builder *b, SpvImageFormat format)
922 {
923 switch (format) {
924 case SpvImageFormatUnknown: return 0; /* GL_NONE */
925 case SpvImageFormatRgba32f: return 0x8814; /* GL_RGBA32F */
926 case SpvImageFormatRgba16f: return 0x881A; /* GL_RGBA16F */
927 case SpvImageFormatR32f: return 0x822E; /* GL_R32F */
928 case SpvImageFormatRgba8: return 0x8058; /* GL_RGBA8 */
929 case SpvImageFormatRgba8Snorm: return 0x8F97; /* GL_RGBA8_SNORM */
930 case SpvImageFormatRg32f: return 0x8230; /* GL_RG32F */
931 case SpvImageFormatRg16f: return 0x822F; /* GL_RG16F */
932 case SpvImageFormatR11fG11fB10f: return 0x8C3A; /* GL_R11F_G11F_B10F */
933 case SpvImageFormatR16f: return 0x822D; /* GL_R16F */
934 case SpvImageFormatRgba16: return 0x805B; /* GL_RGBA16 */
935 case SpvImageFormatRgb10A2: return 0x8059; /* GL_RGB10_A2 */
936 case SpvImageFormatRg16: return 0x822C; /* GL_RG16 */
937 case SpvImageFormatRg8: return 0x822B; /* GL_RG8 */
938 case SpvImageFormatR16: return 0x822A; /* GL_R16 */
939 case SpvImageFormatR8: return 0x8229; /* GL_R8 */
940 case SpvImageFormatRgba16Snorm: return 0x8F9B; /* GL_RGBA16_SNORM */
941 case SpvImageFormatRg16Snorm: return 0x8F99; /* GL_RG16_SNORM */
942 case SpvImageFormatRg8Snorm: return 0x8F95; /* GL_RG8_SNORM */
943 case SpvImageFormatR16Snorm: return 0x8F98; /* GL_R16_SNORM */
944 case SpvImageFormatR8Snorm: return 0x8F94; /* GL_R8_SNORM */
945 case SpvImageFormatRgba32i: return 0x8D82; /* GL_RGBA32I */
946 case SpvImageFormatRgba16i: return 0x8D88; /* GL_RGBA16I */
947 case SpvImageFormatRgba8i: return 0x8D8E; /* GL_RGBA8I */
948 case SpvImageFormatR32i: return 0x8235; /* GL_R32I */
949 case SpvImageFormatRg32i: return 0x823B; /* GL_RG32I */
950 case SpvImageFormatRg16i: return 0x8239; /* GL_RG16I */
951 case SpvImageFormatRg8i: return 0x8237; /* GL_RG8I */
952 case SpvImageFormatR16i: return 0x8233; /* GL_R16I */
953 case SpvImageFormatR8i: return 0x8231; /* GL_R8I */
954 case SpvImageFormatRgba32ui: return 0x8D70; /* GL_RGBA32UI */
955 case SpvImageFormatRgba16ui: return 0x8D76; /* GL_RGBA16UI */
956 case SpvImageFormatRgba8ui: return 0x8D7C; /* GL_RGBA8UI */
957 case SpvImageFormatR32ui: return 0x8236; /* GL_R32UI */
958 case SpvImageFormatRgb10a2ui: return 0x906F; /* GL_RGB10_A2UI */
959 case SpvImageFormatRg32ui: return 0x823C; /* GL_RG32UI */
960 case SpvImageFormatRg16ui: return 0x823A; /* GL_RG16UI */
961 case SpvImageFormatRg8ui: return 0x8238; /* GL_RG8UI */
962 case SpvImageFormatR16ui: return 0x8234; /* GL_R16UI */
963 case SpvImageFormatR8ui: return 0x8232; /* GL_R8UI */
964 default:
965 vtn_fail("Invalid image format");
966 }
967 }
968
969 static struct vtn_type *
970 vtn_type_layout_std430(struct vtn_builder *b, struct vtn_type *type,
971 uint32_t *size_out, uint32_t *align_out)
972 {
973 switch (type->base_type) {
974 case vtn_base_type_scalar: {
975 uint32_t comp_size = glsl_get_bit_size(type->type) / 8;
976 *size_out = comp_size;
977 *align_out = comp_size;
978 return type;
979 }
980
981 case vtn_base_type_vector: {
982 uint32_t comp_size = glsl_get_bit_size(type->type) / 8;
983 unsigned align_comps = type->length == 3 ? 4 : type->length;
984 *size_out = comp_size * type->length,
985 *align_out = comp_size * align_comps;
986 return type;
987 }
988
989 case vtn_base_type_matrix:
990 case vtn_base_type_array: {
991 /* We're going to add an array stride */
992 type = vtn_type_copy(b, type);
993 uint32_t elem_size, elem_align;
994 type->array_element = vtn_type_layout_std430(b, type->array_element,
995 &elem_size, &elem_align);
996 type->stride = vtn_align_u32(elem_size, elem_align);
997 *size_out = type->stride * type->length;
998 *align_out = elem_align;
999 return type;
1000 }
1001
1002 case vtn_base_type_struct: {
1003 /* We're going to add member offsets */
1004 type = vtn_type_copy(b, type);
1005 uint32_t offset = 0;
1006 uint32_t align = 0;
1007 for (unsigned i = 0; i < type->length; i++) {
1008 uint32_t mem_size, mem_align;
1009 type->members[i] = vtn_type_layout_std430(b, type->members[i],
1010 &mem_size, &mem_align);
1011 offset = vtn_align_u32(offset, mem_align);
1012 type->offsets[i] = offset;
1013 offset += mem_size;
1014 align = MAX2(align, mem_align);
1015 }
1016 *size_out = offset;
1017 *align_out = align;
1018 return type;
1019 }
1020
1021 default:
1022 unreachable("Invalid SPIR-V type for std430");
1023 }
1024 }
1025
1026 static void
1027 vtn_handle_type(struct vtn_builder *b, SpvOp opcode,
1028 const uint32_t *w, unsigned count)
1029 {
1030 struct vtn_value *val = vtn_push_value(b, w[1], vtn_value_type_type);
1031
1032 val->type = rzalloc(b, struct vtn_type);
1033 val->type->id = w[1];
1034
1035 switch (opcode) {
1036 case SpvOpTypeVoid:
1037 val->type->base_type = vtn_base_type_void;
1038 val->type->type = glsl_void_type();
1039 break;
1040 case SpvOpTypeBool:
1041 val->type->base_type = vtn_base_type_scalar;
1042 val->type->type = glsl_bool_type();
1043 val->type->length = 1;
1044 break;
1045 case SpvOpTypeInt: {
1046 int bit_size = w[2];
1047 const bool signedness = w[3];
1048 val->type->base_type = vtn_base_type_scalar;
1049 switch (bit_size) {
1050 case 64:
1051 val->type->type = (signedness ? glsl_int64_t_type() : glsl_uint64_t_type());
1052 break;
1053 case 32:
1054 val->type->type = (signedness ? glsl_int_type() : glsl_uint_type());
1055 break;
1056 case 16:
1057 val->type->type = (signedness ? glsl_int16_t_type() : glsl_uint16_t_type());
1058 break;
1059 case 8:
1060 val->type->type = (signedness ? glsl_int8_t_type() : glsl_uint8_t_type());
1061 break;
1062 default:
1063 vtn_fail("Invalid int bit size");
1064 }
1065 val->type->length = 1;
1066 break;
1067 }
1068
1069 case SpvOpTypeFloat: {
1070 int bit_size = w[2];
1071 val->type->base_type = vtn_base_type_scalar;
1072 switch (bit_size) {
1073 case 16:
1074 val->type->type = glsl_float16_t_type();
1075 break;
1076 case 32:
1077 val->type->type = glsl_float_type();
1078 break;
1079 case 64:
1080 val->type->type = glsl_double_type();
1081 break;
1082 default:
1083 vtn_fail("Invalid float bit size");
1084 }
1085 val->type->length = 1;
1086 break;
1087 }
1088
1089 case SpvOpTypeVector: {
1090 struct vtn_type *base = vtn_value(b, w[2], vtn_value_type_type)->type;
1091 unsigned elems = w[3];
1092
1093 vtn_fail_if(base->base_type != vtn_base_type_scalar,
1094 "Base type for OpTypeVector must be a scalar");
1095 vtn_fail_if((elems < 2 || elems > 4) && (elems != 8) && (elems != 16),
1096 "Invalid component count for OpTypeVector");
1097
1098 val->type->base_type = vtn_base_type_vector;
1099 val->type->type = glsl_vector_type(glsl_get_base_type(base->type), elems);
1100 val->type->length = elems;
1101 val->type->stride = glsl_get_bit_size(base->type) / 8;
1102 val->type->array_element = base;
1103 break;
1104 }
1105
1106 case SpvOpTypeMatrix: {
1107 struct vtn_type *base = vtn_value(b, w[2], vtn_value_type_type)->type;
1108 unsigned columns = w[3];
1109
1110 vtn_fail_if(base->base_type != vtn_base_type_vector,
1111 "Base type for OpTypeMatrix must be a vector");
1112 vtn_fail_if(columns < 2 || columns > 4,
1113 "Invalid column count for OpTypeMatrix");
1114
1115 val->type->base_type = vtn_base_type_matrix;
1116 val->type->type = glsl_matrix_type(glsl_get_base_type(base->type),
1117 glsl_get_vector_elements(base->type),
1118 columns);
1119 vtn_fail_if(glsl_type_is_error(val->type->type),
1120 "Unsupported base type for OpTypeMatrix");
1121 assert(!glsl_type_is_error(val->type->type));
1122 val->type->length = columns;
1123 val->type->array_element = base;
1124 val->type->row_major = false;
1125 val->type->stride = 0;
1126 break;
1127 }
1128
1129 case SpvOpTypeRuntimeArray:
1130 case SpvOpTypeArray: {
1131 struct vtn_type *array_element =
1132 vtn_value(b, w[2], vtn_value_type_type)->type;
1133
1134 if (opcode == SpvOpTypeRuntimeArray) {
1135 /* A length of 0 is used to denote unsized arrays */
1136 val->type->length = 0;
1137 } else {
1138 val->type->length =
1139 vtn_value(b, w[3], vtn_value_type_constant)->constant->values[0].u32[0];
1140 }
1141
1142 val->type->base_type = vtn_base_type_array;
1143 val->type->type = glsl_array_type(array_element->type, val->type->length);
1144 val->type->array_element = array_element;
1145 val->type->stride = 0;
1146 break;
1147 }
1148
1149 case SpvOpTypeStruct: {
1150 unsigned num_fields = count - 2;
1151 val->type->base_type = vtn_base_type_struct;
1152 val->type->length = num_fields;
1153 val->type->members = ralloc_array(b, struct vtn_type *, num_fields);
1154 val->type->offsets = ralloc_array(b, unsigned, num_fields);
1155
1156 NIR_VLA(struct glsl_struct_field, fields, count);
1157 for (unsigned i = 0; i < num_fields; i++) {
1158 val->type->members[i] =
1159 vtn_value(b, w[i + 2], vtn_value_type_type)->type;
1160 fields[i] = (struct glsl_struct_field) {
1161 .type = val->type->members[i]->type,
1162 .name = ralloc_asprintf(b, "field%d", i),
1163 .location = -1,
1164 };
1165 }
1166
1167 struct member_decoration_ctx ctx = {
1168 .num_fields = num_fields,
1169 .fields = fields,
1170 .type = val->type
1171 };
1172
1173 vtn_foreach_decoration(b, val, struct_member_decoration_cb, &ctx);
1174 vtn_foreach_decoration(b, val, struct_member_matrix_stride_cb, &ctx);
1175
1176 const char *name = val->name ? val->name : "struct";
1177
1178 val->type->type = glsl_struct_type(fields, num_fields, name);
1179 break;
1180 }
1181
1182 case SpvOpTypeFunction: {
1183 val->type->base_type = vtn_base_type_function;
1184 val->type->type = NULL;
1185
1186 val->type->return_type = vtn_value(b, w[2], vtn_value_type_type)->type;
1187
1188 const unsigned num_params = count - 3;
1189 val->type->length = num_params;
1190 val->type->params = ralloc_array(b, struct vtn_type *, num_params);
1191 for (unsigned i = 0; i < count - 3; i++) {
1192 val->type->params[i] =
1193 vtn_value(b, w[i + 3], vtn_value_type_type)->type;
1194 }
1195 break;
1196 }
1197
1198 case SpvOpTypePointer: {
1199 SpvStorageClass storage_class = w[2];
1200 struct vtn_type *deref_type =
1201 vtn_value(b, w[3], vtn_value_type_type)->type;
1202
1203 val->type->base_type = vtn_base_type_pointer;
1204 val->type->storage_class = storage_class;
1205 val->type->deref = deref_type;
1206
1207 if (storage_class == SpvStorageClassUniform ||
1208 storage_class == SpvStorageClassStorageBuffer) {
1209 /* These can actually be stored to nir_variables and used as SSA
1210 * values so they need a real glsl_type.
1211 */
1212 val->type->type = glsl_vector_type(GLSL_TYPE_UINT, 2);
1213 }
1214
1215 if (storage_class == SpvStorageClassPushConstant) {
1216 /* These can actually be stored to nir_variables and used as SSA
1217 * values so they need a real glsl_type.
1218 */
1219 val->type->type = glsl_uint_type();
1220 }
1221
1222 if (storage_class == SpvStorageClassWorkgroup &&
1223 b->options->lower_workgroup_access_to_offsets) {
1224 uint32_t size, align;
1225 val->type->deref = vtn_type_layout_std430(b, val->type->deref,
1226 &size, &align);
1227 val->type->length = size;
1228 val->type->align = align;
1229 /* These can actually be stored to nir_variables and used as SSA
1230 * values so they need a real glsl_type.
1231 */
1232 val->type->type = glsl_uint_type();
1233 }
1234 break;
1235 }
1236
1237 case SpvOpTypeImage: {
1238 val->type->base_type = vtn_base_type_image;
1239
1240 const struct vtn_type *sampled_type =
1241 vtn_value(b, w[2], vtn_value_type_type)->type;
1242
1243 vtn_fail_if(sampled_type->base_type != vtn_base_type_scalar ||
1244 glsl_get_bit_size(sampled_type->type) != 32,
1245 "Sampled type of OpTypeImage must be a 32-bit scalar");
1246
1247 enum glsl_sampler_dim dim;
1248 switch ((SpvDim)w[3]) {
1249 case SpvDim1D: dim = GLSL_SAMPLER_DIM_1D; break;
1250 case SpvDim2D: dim = GLSL_SAMPLER_DIM_2D; break;
1251 case SpvDim3D: dim = GLSL_SAMPLER_DIM_3D; break;
1252 case SpvDimCube: dim = GLSL_SAMPLER_DIM_CUBE; break;
1253 case SpvDimRect: dim = GLSL_SAMPLER_DIM_RECT; break;
1254 case SpvDimBuffer: dim = GLSL_SAMPLER_DIM_BUF; break;
1255 case SpvDimSubpassData: dim = GLSL_SAMPLER_DIM_SUBPASS; break;
1256 default:
1257 vtn_fail("Invalid SPIR-V image dimensionality");
1258 }
1259
1260 bool is_shadow = w[4];
1261 bool is_array = w[5];
1262 bool multisampled = w[6];
1263 unsigned sampled = w[7];
1264 SpvImageFormat format = w[8];
1265
1266 if (count > 9)
1267 val->type->access_qualifier = w[9];
1268 else
1269 val->type->access_qualifier = SpvAccessQualifierReadWrite;
1270
1271 if (multisampled) {
1272 if (dim == GLSL_SAMPLER_DIM_2D)
1273 dim = GLSL_SAMPLER_DIM_MS;
1274 else if (dim == GLSL_SAMPLER_DIM_SUBPASS)
1275 dim = GLSL_SAMPLER_DIM_SUBPASS_MS;
1276 else
1277 vtn_fail("Unsupported multisampled image type");
1278 }
1279
1280 val->type->image_format = translate_image_format(b, format);
1281
1282 enum glsl_base_type sampled_base_type =
1283 glsl_get_base_type(sampled_type->type);
1284 if (sampled == 1) {
1285 val->type->sampled = true;
1286 val->type->type = glsl_sampler_type(dim, is_shadow, is_array,
1287 sampled_base_type);
1288 } else if (sampled == 2) {
1289 vtn_assert(!is_shadow);
1290 val->type->sampled = false;
1291 val->type->type = glsl_image_type(dim, is_array, sampled_base_type);
1292 } else {
1293 vtn_fail("We need to know if the image will be sampled");
1294 }
1295 break;
1296 }
1297
1298 case SpvOpTypeSampledImage:
1299 val->type->base_type = vtn_base_type_sampled_image;
1300 val->type->image = vtn_value(b, w[2], vtn_value_type_type)->type;
1301 val->type->type = val->type->image->type;
1302 break;
1303
1304 case SpvOpTypeSampler:
1305 /* The actual sampler type here doesn't really matter. It gets
1306 * thrown away the moment you combine it with an image. What really
1307 * matters is that it's a sampler type as opposed to an integer type
1308 * so the backend knows what to do.
1309 */
1310 val->type->base_type = vtn_base_type_sampler;
1311 val->type->type = glsl_bare_sampler_type();
1312 break;
1313
1314 case SpvOpTypeOpaque:
1315 case SpvOpTypeEvent:
1316 case SpvOpTypeDeviceEvent:
1317 case SpvOpTypeReserveId:
1318 case SpvOpTypeQueue:
1319 case SpvOpTypePipe:
1320 default:
1321 vtn_fail("Unhandled opcode");
1322 }
1323
1324 vtn_foreach_decoration(b, val, type_decoration_cb, NULL);
1325 }
1326
1327 static nir_constant *
1328 vtn_null_constant(struct vtn_builder *b, const struct glsl_type *type)
1329 {
1330 nir_constant *c = rzalloc(b, nir_constant);
1331
1332 /* For pointers and other typeless things, we have to return something but
1333 * it doesn't matter what.
1334 */
1335 if (!type)
1336 return c;
1337
1338 switch (glsl_get_base_type(type)) {
1339 case GLSL_TYPE_INT:
1340 case GLSL_TYPE_UINT:
1341 case GLSL_TYPE_INT16:
1342 case GLSL_TYPE_UINT16:
1343 case GLSL_TYPE_UINT8:
1344 case GLSL_TYPE_INT8:
1345 case GLSL_TYPE_INT64:
1346 case GLSL_TYPE_UINT64:
1347 case GLSL_TYPE_BOOL:
1348 case GLSL_TYPE_FLOAT:
1349 case GLSL_TYPE_FLOAT16:
1350 case GLSL_TYPE_DOUBLE:
1351 /* Nothing to do here. It's already initialized to zero */
1352 break;
1353
1354 case GLSL_TYPE_ARRAY:
1355 vtn_assert(glsl_get_length(type) > 0);
1356 c->num_elements = glsl_get_length(type);
1357 c->elements = ralloc_array(b, nir_constant *, c->num_elements);
1358
1359 c->elements[0] = vtn_null_constant(b, glsl_get_array_element(type));
1360 for (unsigned i = 1; i < c->num_elements; i++)
1361 c->elements[i] = c->elements[0];
1362 break;
1363
1364 case GLSL_TYPE_STRUCT:
1365 c->num_elements = glsl_get_length(type);
1366 c->elements = ralloc_array(b, nir_constant *, c->num_elements);
1367
1368 for (unsigned i = 0; i < c->num_elements; i++) {
1369 c->elements[i] = vtn_null_constant(b, glsl_get_struct_field(type, i));
1370 }
1371 break;
1372
1373 default:
1374 vtn_fail("Invalid type for null constant");
1375 }
1376
1377 return c;
1378 }
1379
1380 static void
1381 spec_constant_decoration_cb(struct vtn_builder *b, struct vtn_value *v,
1382 int member, const struct vtn_decoration *dec,
1383 void *data)
1384 {
1385 vtn_assert(member == -1);
1386 if (dec->decoration != SpvDecorationSpecId)
1387 return;
1388
1389 struct spec_constant_value *const_value = data;
1390
1391 for (unsigned i = 0; i < b->num_specializations; i++) {
1392 if (b->specializations[i].id == dec->literals[0]) {
1393 if (const_value->is_double)
1394 const_value->data64 = b->specializations[i].data64;
1395 else
1396 const_value->data32 = b->specializations[i].data32;
1397 return;
1398 }
1399 }
1400 }
1401
1402 static uint32_t
1403 get_specialization(struct vtn_builder *b, struct vtn_value *val,
1404 uint32_t const_value)
1405 {
1406 struct spec_constant_value data;
1407 data.is_double = false;
1408 data.data32 = const_value;
1409 vtn_foreach_decoration(b, val, spec_constant_decoration_cb, &data);
1410 return data.data32;
1411 }
1412
1413 static uint64_t
1414 get_specialization64(struct vtn_builder *b, struct vtn_value *val,
1415 uint64_t const_value)
1416 {
1417 struct spec_constant_value data;
1418 data.is_double = true;
1419 data.data64 = const_value;
1420 vtn_foreach_decoration(b, val, spec_constant_decoration_cb, &data);
1421 return data.data64;
1422 }
1423
1424 static void
1425 handle_workgroup_size_decoration_cb(struct vtn_builder *b,
1426 struct vtn_value *val,
1427 int member,
1428 const struct vtn_decoration *dec,
1429 void *data)
1430 {
1431 vtn_assert(member == -1);
1432 if (dec->decoration != SpvDecorationBuiltIn ||
1433 dec->literals[0] != SpvBuiltInWorkgroupSize)
1434 return;
1435
1436 vtn_assert(val->type->type == glsl_vector_type(GLSL_TYPE_UINT, 3));
1437
1438 b->shader->info.cs.local_size[0] = val->constant->values[0].u32[0];
1439 b->shader->info.cs.local_size[1] = val->constant->values[0].u32[1];
1440 b->shader->info.cs.local_size[2] = val->constant->values[0].u32[2];
1441 }
1442
1443 static void
1444 vtn_handle_constant(struct vtn_builder *b, SpvOp opcode,
1445 const uint32_t *w, unsigned count)
1446 {
1447 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_constant);
1448 val->constant = rzalloc(b, nir_constant);
1449 switch (opcode) {
1450 case SpvOpConstantTrue:
1451 case SpvOpConstantFalse:
1452 case SpvOpSpecConstantTrue:
1453 case SpvOpSpecConstantFalse: {
1454 vtn_fail_if(val->type->type != glsl_bool_type(),
1455 "Result type of %s must be OpTypeBool",
1456 spirv_op_to_string(opcode));
1457
1458 uint32_t int_val = (opcode == SpvOpConstantTrue ||
1459 opcode == SpvOpSpecConstantTrue);
1460
1461 if (opcode == SpvOpSpecConstantTrue ||
1462 opcode == SpvOpSpecConstantFalse)
1463 int_val = get_specialization(b, val, int_val);
1464
1465 val->constant->values[0].u32[0] = int_val ? NIR_TRUE : NIR_FALSE;
1466 break;
1467 }
1468
1469 case SpvOpConstant: {
1470 vtn_fail_if(val->type->base_type != vtn_base_type_scalar,
1471 "Result type of %s must be a scalar",
1472 spirv_op_to_string(opcode));
1473 int bit_size = glsl_get_bit_size(val->type->type);
1474 switch (bit_size) {
1475 case 64:
1476 val->constant->values->u64[0] = vtn_u64_literal(&w[3]);
1477 break;
1478 case 32:
1479 val->constant->values->u32[0] = w[3];
1480 break;
1481 case 16:
1482 val->constant->values->u16[0] = w[3];
1483 break;
1484 case 8:
1485 val->constant->values->u8[0] = w[3];
1486 break;
1487 default:
1488 vtn_fail("Unsupported SpvOpConstant bit size");
1489 }
1490 break;
1491 }
1492
1493 case SpvOpSpecConstant: {
1494 vtn_fail_if(val->type->base_type != vtn_base_type_scalar,
1495 "Result type of %s must be a scalar",
1496 spirv_op_to_string(opcode));
1497 int bit_size = glsl_get_bit_size(val->type->type);
1498 switch (bit_size) {
1499 case 64:
1500 val->constant->values[0].u64[0] =
1501 get_specialization64(b, val, vtn_u64_literal(&w[3]));
1502 break;
1503 case 32:
1504 val->constant->values[0].u32[0] = get_specialization(b, val, w[3]);
1505 break;
1506 case 16:
1507 val->constant->values[0].u16[0] = get_specialization(b, val, w[3]);
1508 break;
1509 case 8:
1510 val->constant->values[0].u8[0] = get_specialization(b, val, w[3]);
1511 break;
1512 default:
1513 vtn_fail("Unsupported SpvOpSpecConstant bit size");
1514 }
1515 break;
1516 }
1517
1518 case SpvOpSpecConstantComposite:
1519 case SpvOpConstantComposite: {
1520 unsigned elem_count = count - 3;
1521 vtn_fail_if(elem_count != val->type->length,
1522 "%s has %u constituents, expected %u",
1523 spirv_op_to_string(opcode), elem_count, val->type->length);
1524
1525 nir_constant **elems = ralloc_array(b, nir_constant *, elem_count);
1526 for (unsigned i = 0; i < elem_count; i++) {
1527 struct vtn_value *val = vtn_untyped_value(b, w[i + 3]);
1528
1529 if (val->value_type == vtn_value_type_constant) {
1530 elems[i] = val->constant;
1531 } else {
1532 vtn_fail_if(val->value_type != vtn_value_type_undef,
1533 "only constants or undefs allowed for "
1534 "SpvOpConstantComposite");
1535 /* to make it easier, just insert a NULL constant for now */
1536 elems[i] = vtn_null_constant(b, val->type->type);
1537 }
1538 }
1539
1540 switch (val->type->base_type) {
1541 case vtn_base_type_vector: {
1542 assert(glsl_type_is_vector(val->type->type));
1543 int bit_size = glsl_get_bit_size(val->type->type);
1544 for (unsigned i = 0; i < elem_count; i++) {
1545 switch (bit_size) {
1546 case 64:
1547 val->constant->values[0].u64[i] = elems[i]->values[0].u64[0];
1548 break;
1549 case 32:
1550 val->constant->values[0].u32[i] = elems[i]->values[0].u32[0];
1551 break;
1552 case 16:
1553 val->constant->values[0].u16[i] = elems[i]->values[0].u16[0];
1554 break;
1555 case 8:
1556 val->constant->values[0].u8[i] = elems[i]->values[0].u8[0];
1557 break;
1558 default:
1559 vtn_fail("Invalid SpvOpConstantComposite bit size");
1560 }
1561 }
1562 break;
1563 }
1564
1565 case vtn_base_type_matrix:
1566 assert(glsl_type_is_matrix(val->type->type));
1567 for (unsigned i = 0; i < elem_count; i++)
1568 val->constant->values[i] = elems[i]->values[0];
1569 break;
1570
1571 case vtn_base_type_struct:
1572 case vtn_base_type_array:
1573 ralloc_steal(val->constant, elems);
1574 val->constant->num_elements = elem_count;
1575 val->constant->elements = elems;
1576 break;
1577
1578 default:
1579 vtn_fail("Result type of %s must be a composite type",
1580 spirv_op_to_string(opcode));
1581 }
1582 break;
1583 }
1584
1585 case SpvOpSpecConstantOp: {
1586 SpvOp opcode = get_specialization(b, val, w[3]);
1587 switch (opcode) {
1588 case SpvOpVectorShuffle: {
1589 struct vtn_value *v0 = &b->values[w[4]];
1590 struct vtn_value *v1 = &b->values[w[5]];
1591
1592 vtn_assert(v0->value_type == vtn_value_type_constant ||
1593 v0->value_type == vtn_value_type_undef);
1594 vtn_assert(v1->value_type == vtn_value_type_constant ||
1595 v1->value_type == vtn_value_type_undef);
1596
1597 unsigned len0 = glsl_get_vector_elements(v0->type->type);
1598 unsigned len1 = glsl_get_vector_elements(v1->type->type);
1599
1600 vtn_assert(len0 + len1 < 16);
1601
1602 unsigned bit_size = glsl_get_bit_size(val->type->type);
1603 unsigned bit_size0 = glsl_get_bit_size(v0->type->type);
1604 unsigned bit_size1 = glsl_get_bit_size(v1->type->type);
1605
1606 vtn_assert(bit_size == bit_size0 && bit_size == bit_size1);
1607 (void)bit_size0; (void)bit_size1;
1608
1609 if (bit_size == 64) {
1610 uint64_t u64[8];
1611 if (v0->value_type == vtn_value_type_constant) {
1612 for (unsigned i = 0; i < len0; i++)
1613 u64[i] = v0->constant->values[0].u64[i];
1614 }
1615 if (v1->value_type == vtn_value_type_constant) {
1616 for (unsigned i = 0; i < len1; i++)
1617 u64[len0 + i] = v1->constant->values[0].u64[i];
1618 }
1619
1620 for (unsigned i = 0, j = 0; i < count - 6; i++, j++) {
1621 uint32_t comp = w[i + 6];
1622 /* If component is not used, set the value to a known constant
1623 * to detect if it is wrongly used.
1624 */
1625 if (comp == (uint32_t)-1)
1626 val->constant->values[0].u64[j] = 0xdeadbeefdeadbeef;
1627 else
1628 val->constant->values[0].u64[j] = u64[comp];
1629 }
1630 } else {
1631 /* This is for both 32-bit and 16-bit values */
1632 uint32_t u32[8];
1633 if (v0->value_type == vtn_value_type_constant) {
1634 for (unsigned i = 0; i < len0; i++)
1635 u32[i] = v0->constant->values[0].u32[i];
1636 }
1637 if (v1->value_type == vtn_value_type_constant) {
1638 for (unsigned i = 0; i < len1; i++)
1639 u32[len0 + i] = v1->constant->values[0].u32[i];
1640 }
1641
1642 for (unsigned i = 0, j = 0; i < count - 6; i++, j++) {
1643 uint32_t comp = w[i + 6];
1644 /* If component is not used, set the value to a known constant
1645 * to detect if it is wrongly used.
1646 */
1647 if (comp == (uint32_t)-1)
1648 val->constant->values[0].u32[j] = 0xdeadbeef;
1649 else
1650 val->constant->values[0].u32[j] = u32[comp];
1651 }
1652 }
1653 break;
1654 }
1655
1656 case SpvOpCompositeExtract:
1657 case SpvOpCompositeInsert: {
1658 struct vtn_value *comp;
1659 unsigned deref_start;
1660 struct nir_constant **c;
1661 if (opcode == SpvOpCompositeExtract) {
1662 comp = vtn_value(b, w[4], vtn_value_type_constant);
1663 deref_start = 5;
1664 c = &comp->constant;
1665 } else {
1666 comp = vtn_value(b, w[5], vtn_value_type_constant);
1667 deref_start = 6;
1668 val->constant = nir_constant_clone(comp->constant,
1669 (nir_variable *)b);
1670 c = &val->constant;
1671 }
1672
1673 int elem = -1;
1674 int col = 0;
1675 const struct vtn_type *type = comp->type;
1676 for (unsigned i = deref_start; i < count; i++) {
1677 vtn_fail_if(w[i] > type->length,
1678 "%uth index of %s is %u but the type has only "
1679 "%u elements", i - deref_start,
1680 spirv_op_to_string(opcode), w[i], type->length);
1681
1682 switch (type->base_type) {
1683 case vtn_base_type_vector:
1684 elem = w[i];
1685 type = type->array_element;
1686 break;
1687
1688 case vtn_base_type_matrix:
1689 assert(col == 0 && elem == -1);
1690 col = w[i];
1691 elem = 0;
1692 type = type->array_element;
1693 break;
1694
1695 case vtn_base_type_array:
1696 c = &(*c)->elements[w[i]];
1697 type = type->array_element;
1698 break;
1699
1700 case vtn_base_type_struct:
1701 c = &(*c)->elements[w[i]];
1702 type = type->members[w[i]];
1703 break;
1704
1705 default:
1706 vtn_fail("%s must only index into composite types",
1707 spirv_op_to_string(opcode));
1708 }
1709 }
1710
1711 if (opcode == SpvOpCompositeExtract) {
1712 if (elem == -1) {
1713 val->constant = *c;
1714 } else {
1715 unsigned num_components = type->length;
1716 unsigned bit_size = glsl_get_bit_size(type->type);
1717 for (unsigned i = 0; i < num_components; i++)
1718 switch(bit_size) {
1719 case 64:
1720 val->constant->values[0].u64[i] = (*c)->values[col].u64[elem + i];
1721 break;
1722 case 32:
1723 val->constant->values[0].u32[i] = (*c)->values[col].u32[elem + i];
1724 break;
1725 case 16:
1726 val->constant->values[0].u16[i] = (*c)->values[col].u16[elem + i];
1727 break;
1728 case 8:
1729 val->constant->values[0].u8[i] = (*c)->values[col].u8[elem + i];
1730 break;
1731 default:
1732 vtn_fail("Invalid SpvOpCompositeExtract bit size");
1733 }
1734 }
1735 } else {
1736 struct vtn_value *insert =
1737 vtn_value(b, w[4], vtn_value_type_constant);
1738 vtn_assert(insert->type == type);
1739 if (elem == -1) {
1740 *c = insert->constant;
1741 } else {
1742 unsigned num_components = type->length;
1743 unsigned bit_size = glsl_get_bit_size(type->type);
1744 for (unsigned i = 0; i < num_components; i++)
1745 switch (bit_size) {
1746 case 64:
1747 (*c)->values[col].u64[elem + i] = insert->constant->values[0].u64[i];
1748 break;
1749 case 32:
1750 (*c)->values[col].u32[elem + i] = insert->constant->values[0].u32[i];
1751 break;
1752 case 16:
1753 (*c)->values[col].u16[elem + i] = insert->constant->values[0].u16[i];
1754 break;
1755 case 8:
1756 (*c)->values[col].u8[elem + i] = insert->constant->values[0].u8[i];
1757 break;
1758 default:
1759 vtn_fail("Invalid SpvOpCompositeInsert bit size");
1760 }
1761 }
1762 }
1763 break;
1764 }
1765
1766 default: {
1767 bool swap;
1768 nir_alu_type dst_alu_type = nir_get_nir_type_for_glsl_type(val->type->type);
1769 nir_alu_type src_alu_type = dst_alu_type;
1770 unsigned num_components = glsl_get_vector_elements(val->type->type);
1771 unsigned bit_size;
1772
1773 vtn_assert(count <= 7);
1774
1775 switch (opcode) {
1776 case SpvOpSConvert:
1777 case SpvOpFConvert:
1778 /* We have a source in a conversion */
1779 src_alu_type =
1780 nir_get_nir_type_for_glsl_type(
1781 vtn_value(b, w[4], vtn_value_type_constant)->type->type);
1782 /* We use the bitsize of the conversion source to evaluate the opcode later */
1783 bit_size = glsl_get_bit_size(
1784 vtn_value(b, w[4], vtn_value_type_constant)->type->type);
1785 break;
1786 default:
1787 bit_size = glsl_get_bit_size(val->type->type);
1788 };
1789
1790 nir_op op = vtn_nir_alu_op_for_spirv_opcode(b, opcode, &swap,
1791 nir_alu_type_get_type_size(src_alu_type),
1792 nir_alu_type_get_type_size(dst_alu_type));
1793 nir_const_value src[4];
1794
1795 for (unsigned i = 0; i < count - 4; i++) {
1796 nir_constant *c =
1797 vtn_value(b, w[4 + i], vtn_value_type_constant)->constant;
1798
1799 unsigned j = swap ? 1 - i : i;
1800 src[j] = c->values[0];
1801 }
1802
1803 val->constant->values[0] =
1804 nir_eval_const_opcode(op, num_components, bit_size, src);
1805 break;
1806 } /* default */
1807 }
1808 break;
1809 }
1810
1811 case SpvOpConstantNull:
1812 val->constant = vtn_null_constant(b, val->type->type);
1813 break;
1814
1815 case SpvOpConstantSampler:
1816 vtn_fail("OpConstantSampler requires Kernel Capability");
1817 break;
1818
1819 default:
1820 vtn_fail("Unhandled opcode");
1821 }
1822
1823 /* Now that we have the value, update the workgroup size if needed */
1824 vtn_foreach_decoration(b, val, handle_workgroup_size_decoration_cb, NULL);
1825 }
1826
1827 struct vtn_ssa_value *
1828 vtn_create_ssa_value(struct vtn_builder *b, const struct glsl_type *type)
1829 {
1830 struct vtn_ssa_value *val = rzalloc(b, struct vtn_ssa_value);
1831 val->type = type;
1832
1833 if (!glsl_type_is_vector_or_scalar(type)) {
1834 unsigned elems = glsl_get_length(type);
1835 val->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
1836 for (unsigned i = 0; i < elems; i++) {
1837 const struct glsl_type *child_type;
1838
1839 switch (glsl_get_base_type(type)) {
1840 case GLSL_TYPE_INT:
1841 case GLSL_TYPE_UINT:
1842 case GLSL_TYPE_INT16:
1843 case GLSL_TYPE_UINT16:
1844 case GLSL_TYPE_UINT8:
1845 case GLSL_TYPE_INT8:
1846 case GLSL_TYPE_INT64:
1847 case GLSL_TYPE_UINT64:
1848 case GLSL_TYPE_BOOL:
1849 case GLSL_TYPE_FLOAT:
1850 case GLSL_TYPE_FLOAT16:
1851 case GLSL_TYPE_DOUBLE:
1852 child_type = glsl_get_column_type(type);
1853 break;
1854 case GLSL_TYPE_ARRAY:
1855 child_type = glsl_get_array_element(type);
1856 break;
1857 case GLSL_TYPE_STRUCT:
1858 child_type = glsl_get_struct_field(type, i);
1859 break;
1860 default:
1861 vtn_fail("unkown base type");
1862 }
1863
1864 val->elems[i] = vtn_create_ssa_value(b, child_type);
1865 }
1866 }
1867
1868 return val;
1869 }
1870
1871 static nir_tex_src
1872 vtn_tex_src(struct vtn_builder *b, unsigned index, nir_tex_src_type type)
1873 {
1874 nir_tex_src src;
1875 src.src = nir_src_for_ssa(vtn_ssa_value(b, index)->def);
1876 src.src_type = type;
1877 return src;
1878 }
1879
1880 static void
1881 vtn_handle_texture(struct vtn_builder *b, SpvOp opcode,
1882 const uint32_t *w, unsigned count)
1883 {
1884 if (opcode == SpvOpSampledImage) {
1885 struct vtn_value *val =
1886 vtn_push_value(b, w[2], vtn_value_type_sampled_image);
1887 val->sampled_image = ralloc(b, struct vtn_sampled_image);
1888 val->sampled_image->type =
1889 vtn_value(b, w[1], vtn_value_type_type)->type;
1890 val->sampled_image->image =
1891 vtn_value(b, w[3], vtn_value_type_pointer)->pointer;
1892 val->sampled_image->sampler =
1893 vtn_value(b, w[4], vtn_value_type_pointer)->pointer;
1894 return;
1895 } else if (opcode == SpvOpImage) {
1896 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_pointer);
1897 struct vtn_value *src_val = vtn_untyped_value(b, w[3]);
1898 if (src_val->value_type == vtn_value_type_sampled_image) {
1899 val->pointer = src_val->sampled_image->image;
1900 } else {
1901 vtn_assert(src_val->value_type == vtn_value_type_pointer);
1902 val->pointer = src_val->pointer;
1903 }
1904 return;
1905 }
1906
1907 struct vtn_type *ret_type = vtn_value(b, w[1], vtn_value_type_type)->type;
1908 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
1909
1910 struct vtn_sampled_image sampled;
1911 struct vtn_value *sampled_val = vtn_untyped_value(b, w[3]);
1912 if (sampled_val->value_type == vtn_value_type_sampled_image) {
1913 sampled = *sampled_val->sampled_image;
1914 } else {
1915 vtn_assert(sampled_val->value_type == vtn_value_type_pointer);
1916 sampled.type = sampled_val->pointer->type;
1917 sampled.image = NULL;
1918 sampled.sampler = sampled_val->pointer;
1919 }
1920
1921 const struct glsl_type *image_type = sampled.type->type;
1922 const enum glsl_sampler_dim sampler_dim = glsl_get_sampler_dim(image_type);
1923 const bool is_array = glsl_sampler_type_is_array(image_type);
1924
1925 /* Figure out the base texture operation */
1926 nir_texop texop;
1927 switch (opcode) {
1928 case SpvOpImageSampleImplicitLod:
1929 case SpvOpImageSampleDrefImplicitLod:
1930 case SpvOpImageSampleProjImplicitLod:
1931 case SpvOpImageSampleProjDrefImplicitLod:
1932 texop = nir_texop_tex;
1933 break;
1934
1935 case SpvOpImageSampleExplicitLod:
1936 case SpvOpImageSampleDrefExplicitLod:
1937 case SpvOpImageSampleProjExplicitLod:
1938 case SpvOpImageSampleProjDrefExplicitLod:
1939 texop = nir_texop_txl;
1940 break;
1941
1942 case SpvOpImageFetch:
1943 if (glsl_get_sampler_dim(image_type) == GLSL_SAMPLER_DIM_MS) {
1944 texop = nir_texop_txf_ms;
1945 } else {
1946 texop = nir_texop_txf;
1947 }
1948 break;
1949
1950 case SpvOpImageGather:
1951 case SpvOpImageDrefGather:
1952 texop = nir_texop_tg4;
1953 break;
1954
1955 case SpvOpImageQuerySizeLod:
1956 case SpvOpImageQuerySize:
1957 texop = nir_texop_txs;
1958 break;
1959
1960 case SpvOpImageQueryLod:
1961 texop = nir_texop_lod;
1962 break;
1963
1964 case SpvOpImageQueryLevels:
1965 texop = nir_texop_query_levels;
1966 break;
1967
1968 case SpvOpImageQuerySamples:
1969 texop = nir_texop_texture_samples;
1970 break;
1971
1972 default:
1973 vtn_fail("Unhandled opcode");
1974 }
1975
1976 nir_tex_src srcs[10]; /* 10 should be enough */
1977 nir_tex_src *p = srcs;
1978
1979 nir_deref_instr *sampler = vtn_pointer_to_deref(b, sampled.sampler);
1980 nir_deref_instr *texture =
1981 sampled.image ? vtn_pointer_to_deref(b, sampled.image) : sampler;
1982
1983 p->src = nir_src_for_ssa(&texture->dest.ssa);
1984 p->src_type = nir_tex_src_texture_deref;
1985 p++;
1986
1987 switch (texop) {
1988 case nir_texop_tex:
1989 case nir_texop_txb:
1990 case nir_texop_txl:
1991 case nir_texop_txd:
1992 case nir_texop_tg4:
1993 /* These operations require a sampler */
1994 p->src = nir_src_for_ssa(&sampler->dest.ssa);
1995 p->src_type = nir_tex_src_sampler_deref;
1996 p++;
1997 break;
1998 case nir_texop_txf:
1999 case nir_texop_txf_ms:
2000 case nir_texop_txs:
2001 case nir_texop_lod:
2002 case nir_texop_query_levels:
2003 case nir_texop_texture_samples:
2004 case nir_texop_samples_identical:
2005 /* These don't */
2006 break;
2007 case nir_texop_txf_ms_mcs:
2008 vtn_fail("unexpected nir_texop_txf_ms_mcs");
2009 }
2010
2011 unsigned idx = 4;
2012
2013 struct nir_ssa_def *coord;
2014 unsigned coord_components;
2015 switch (opcode) {
2016 case SpvOpImageSampleImplicitLod:
2017 case SpvOpImageSampleExplicitLod:
2018 case SpvOpImageSampleDrefImplicitLod:
2019 case SpvOpImageSampleDrefExplicitLod:
2020 case SpvOpImageSampleProjImplicitLod:
2021 case SpvOpImageSampleProjExplicitLod:
2022 case SpvOpImageSampleProjDrefImplicitLod:
2023 case SpvOpImageSampleProjDrefExplicitLod:
2024 case SpvOpImageFetch:
2025 case SpvOpImageGather:
2026 case SpvOpImageDrefGather:
2027 case SpvOpImageQueryLod: {
2028 /* All these types have the coordinate as their first real argument */
2029 switch (sampler_dim) {
2030 case GLSL_SAMPLER_DIM_1D:
2031 case GLSL_SAMPLER_DIM_BUF:
2032 coord_components = 1;
2033 break;
2034 case GLSL_SAMPLER_DIM_2D:
2035 case GLSL_SAMPLER_DIM_RECT:
2036 case GLSL_SAMPLER_DIM_MS:
2037 coord_components = 2;
2038 break;
2039 case GLSL_SAMPLER_DIM_3D:
2040 case GLSL_SAMPLER_DIM_CUBE:
2041 coord_components = 3;
2042 break;
2043 default:
2044 vtn_fail("Invalid sampler type");
2045 }
2046
2047 if (is_array && texop != nir_texop_lod)
2048 coord_components++;
2049
2050 coord = vtn_ssa_value(b, w[idx++])->def;
2051 p->src = nir_src_for_ssa(nir_channels(&b->nb, coord,
2052 (1 << coord_components) - 1));
2053 p->src_type = nir_tex_src_coord;
2054 p++;
2055 break;
2056 }
2057
2058 default:
2059 coord = NULL;
2060 coord_components = 0;
2061 break;
2062 }
2063
2064 switch (opcode) {
2065 case SpvOpImageSampleProjImplicitLod:
2066 case SpvOpImageSampleProjExplicitLod:
2067 case SpvOpImageSampleProjDrefImplicitLod:
2068 case SpvOpImageSampleProjDrefExplicitLod:
2069 /* These have the projector as the last coordinate component */
2070 p->src = nir_src_for_ssa(nir_channel(&b->nb, coord, coord_components));
2071 p->src_type = nir_tex_src_projector;
2072 p++;
2073 break;
2074
2075 default:
2076 break;
2077 }
2078
2079 bool is_shadow = false;
2080 unsigned gather_component = 0;
2081 switch (opcode) {
2082 case SpvOpImageSampleDrefImplicitLod:
2083 case SpvOpImageSampleDrefExplicitLod:
2084 case SpvOpImageSampleProjDrefImplicitLod:
2085 case SpvOpImageSampleProjDrefExplicitLod:
2086 case SpvOpImageDrefGather:
2087 /* These all have an explicit depth value as their next source */
2088 is_shadow = true;
2089 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_comparator);
2090 break;
2091
2092 case SpvOpImageGather:
2093 /* This has a component as its next source */
2094 gather_component =
2095 vtn_value(b, w[idx++], vtn_value_type_constant)->constant->values[0].u32[0];
2096 break;
2097
2098 default:
2099 break;
2100 }
2101
2102 /* For OpImageQuerySizeLod, we always have an LOD */
2103 if (opcode == SpvOpImageQuerySizeLod)
2104 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_lod);
2105
2106 /* Now we need to handle some number of optional arguments */
2107 const struct vtn_ssa_value *gather_offsets = NULL;
2108 if (idx < count) {
2109 uint32_t operands = w[idx++];
2110
2111 if (operands & SpvImageOperandsBiasMask) {
2112 vtn_assert(texop == nir_texop_tex);
2113 texop = nir_texop_txb;
2114 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_bias);
2115 }
2116
2117 if (operands & SpvImageOperandsLodMask) {
2118 vtn_assert(texop == nir_texop_txl || texop == nir_texop_txf ||
2119 texop == nir_texop_txs);
2120 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_lod);
2121 }
2122
2123 if (operands & SpvImageOperandsGradMask) {
2124 vtn_assert(texop == nir_texop_txl);
2125 texop = nir_texop_txd;
2126 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_ddx);
2127 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_ddy);
2128 }
2129
2130 if (operands & SpvImageOperandsOffsetMask ||
2131 operands & SpvImageOperandsConstOffsetMask)
2132 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_offset);
2133
2134 if (operands & SpvImageOperandsConstOffsetsMask) {
2135 nir_tex_src none = {0};
2136 gather_offsets = vtn_ssa_value(b, w[idx++]);
2137 (*p++) = none;
2138 }
2139
2140 if (operands & SpvImageOperandsSampleMask) {
2141 vtn_assert(texop == nir_texop_txf_ms);
2142 texop = nir_texop_txf_ms;
2143 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_ms_index);
2144 }
2145 }
2146 /* We should have now consumed exactly all of the arguments */
2147 vtn_assert(idx == count);
2148
2149 nir_tex_instr *instr = nir_tex_instr_create(b->shader, p - srcs);
2150 instr->op = texop;
2151
2152 memcpy(instr->src, srcs, instr->num_srcs * sizeof(*instr->src));
2153
2154 instr->coord_components = coord_components;
2155 instr->sampler_dim = sampler_dim;
2156 instr->is_array = is_array;
2157 instr->is_shadow = is_shadow;
2158 instr->is_new_style_shadow =
2159 is_shadow && glsl_get_components(ret_type->type) == 1;
2160 instr->component = gather_component;
2161
2162 switch (glsl_get_sampler_result_type(image_type)) {
2163 case GLSL_TYPE_FLOAT: instr->dest_type = nir_type_float; break;
2164 case GLSL_TYPE_INT: instr->dest_type = nir_type_int; break;
2165 case GLSL_TYPE_UINT: instr->dest_type = nir_type_uint; break;
2166 case GLSL_TYPE_BOOL: instr->dest_type = nir_type_bool; break;
2167 default:
2168 vtn_fail("Invalid base type for sampler result");
2169 }
2170
2171 nir_ssa_dest_init(&instr->instr, &instr->dest,
2172 nir_tex_instr_dest_size(instr), 32, NULL);
2173
2174 vtn_assert(glsl_get_vector_elements(ret_type->type) ==
2175 nir_tex_instr_dest_size(instr));
2176
2177 nir_ssa_def *def;
2178 nir_instr *instruction;
2179 if (gather_offsets) {
2180 vtn_assert(glsl_get_base_type(gather_offsets->type) == GLSL_TYPE_ARRAY);
2181 vtn_assert(glsl_get_length(gather_offsets->type) == 4);
2182 nir_tex_instr *instrs[4] = {instr, NULL, NULL, NULL};
2183
2184 /* Copy the current instruction 4x */
2185 for (uint32_t i = 1; i < 4; i++) {
2186 instrs[i] = nir_tex_instr_create(b->shader, instr->num_srcs);
2187 instrs[i]->op = instr->op;
2188 instrs[i]->coord_components = instr->coord_components;
2189 instrs[i]->sampler_dim = instr->sampler_dim;
2190 instrs[i]->is_array = instr->is_array;
2191 instrs[i]->is_shadow = instr->is_shadow;
2192 instrs[i]->is_new_style_shadow = instr->is_new_style_shadow;
2193 instrs[i]->component = instr->component;
2194 instrs[i]->dest_type = instr->dest_type;
2195
2196 memcpy(instrs[i]->src, srcs, instr->num_srcs * sizeof(*instr->src));
2197
2198 nir_ssa_dest_init(&instrs[i]->instr, &instrs[i]->dest,
2199 nir_tex_instr_dest_size(instr), 32, NULL);
2200 }
2201
2202 /* Fill in the last argument with the offset from the passed in offsets
2203 * and insert the instruction into the stream.
2204 */
2205 for (uint32_t i = 0; i < 4; i++) {
2206 nir_tex_src src;
2207 src.src = nir_src_for_ssa(gather_offsets->elems[i]->def);
2208 src.src_type = nir_tex_src_offset;
2209 instrs[i]->src[instrs[i]->num_srcs - 1] = src;
2210 nir_builder_instr_insert(&b->nb, &instrs[i]->instr);
2211 }
2212
2213 /* Combine the results of the 4 instructions by taking their .w
2214 * components
2215 */
2216 nir_alu_instr *vec4 = nir_alu_instr_create(b->shader, nir_op_vec4);
2217 nir_ssa_dest_init(&vec4->instr, &vec4->dest.dest, 4, 32, NULL);
2218 vec4->dest.write_mask = 0xf;
2219 for (uint32_t i = 0; i < 4; i++) {
2220 vec4->src[i].src = nir_src_for_ssa(&instrs[i]->dest.ssa);
2221 vec4->src[i].swizzle[0] = 3;
2222 }
2223 def = &vec4->dest.dest.ssa;
2224 instruction = &vec4->instr;
2225 } else {
2226 def = &instr->dest.ssa;
2227 instruction = &instr->instr;
2228 }
2229
2230 val->ssa = vtn_create_ssa_value(b, ret_type->type);
2231 val->ssa->def = def;
2232
2233 nir_builder_instr_insert(&b->nb, instruction);
2234 }
2235
2236 static void
2237 fill_common_atomic_sources(struct vtn_builder *b, SpvOp opcode,
2238 const uint32_t *w, nir_src *src)
2239 {
2240 switch (opcode) {
2241 case SpvOpAtomicIIncrement:
2242 src[0] = nir_src_for_ssa(nir_imm_int(&b->nb, 1));
2243 break;
2244
2245 case SpvOpAtomicIDecrement:
2246 src[0] = nir_src_for_ssa(nir_imm_int(&b->nb, -1));
2247 break;
2248
2249 case SpvOpAtomicISub:
2250 src[0] =
2251 nir_src_for_ssa(nir_ineg(&b->nb, vtn_ssa_value(b, w[6])->def));
2252 break;
2253
2254 case SpvOpAtomicCompareExchange:
2255 src[0] = nir_src_for_ssa(vtn_ssa_value(b, w[8])->def);
2256 src[1] = nir_src_for_ssa(vtn_ssa_value(b, w[7])->def);
2257 break;
2258
2259 case SpvOpAtomicExchange:
2260 case SpvOpAtomicIAdd:
2261 case SpvOpAtomicSMin:
2262 case SpvOpAtomicUMin:
2263 case SpvOpAtomicSMax:
2264 case SpvOpAtomicUMax:
2265 case SpvOpAtomicAnd:
2266 case SpvOpAtomicOr:
2267 case SpvOpAtomicXor:
2268 src[0] = nir_src_for_ssa(vtn_ssa_value(b, w[6])->def);
2269 break;
2270
2271 default:
2272 vtn_fail("Invalid SPIR-V atomic");
2273 }
2274 }
2275
2276 static nir_ssa_def *
2277 get_image_coord(struct vtn_builder *b, uint32_t value)
2278 {
2279 struct vtn_ssa_value *coord = vtn_ssa_value(b, value);
2280
2281 /* The image_load_store intrinsics assume a 4-dim coordinate */
2282 unsigned dim = glsl_get_vector_elements(coord->type);
2283 unsigned swizzle[4];
2284 for (unsigned i = 0; i < 4; i++)
2285 swizzle[i] = MIN2(i, dim - 1);
2286
2287 return nir_swizzle(&b->nb, coord->def, swizzle, 4, false);
2288 }
2289
2290 static nir_ssa_def *
2291 expand_to_vec4(nir_builder *b, nir_ssa_def *value)
2292 {
2293 if (value->num_components == 4)
2294 return value;
2295
2296 unsigned swiz[4];
2297 for (unsigned i = 0; i < 4; i++)
2298 swiz[i] = i < value->num_components ? i : 0;
2299 return nir_swizzle(b, value, swiz, 4, false);
2300 }
2301
2302 static void
2303 vtn_handle_image(struct vtn_builder *b, SpvOp opcode,
2304 const uint32_t *w, unsigned count)
2305 {
2306 /* Just get this one out of the way */
2307 if (opcode == SpvOpImageTexelPointer) {
2308 struct vtn_value *val =
2309 vtn_push_value(b, w[2], vtn_value_type_image_pointer);
2310 val->image = ralloc(b, struct vtn_image_pointer);
2311
2312 val->image->image = vtn_value(b, w[3], vtn_value_type_pointer)->pointer;
2313 val->image->coord = get_image_coord(b, w[4]);
2314 val->image->sample = vtn_ssa_value(b, w[5])->def;
2315 return;
2316 }
2317
2318 struct vtn_image_pointer image;
2319
2320 switch (opcode) {
2321 case SpvOpAtomicExchange:
2322 case SpvOpAtomicCompareExchange:
2323 case SpvOpAtomicCompareExchangeWeak:
2324 case SpvOpAtomicIIncrement:
2325 case SpvOpAtomicIDecrement:
2326 case SpvOpAtomicIAdd:
2327 case SpvOpAtomicISub:
2328 case SpvOpAtomicLoad:
2329 case SpvOpAtomicSMin:
2330 case SpvOpAtomicUMin:
2331 case SpvOpAtomicSMax:
2332 case SpvOpAtomicUMax:
2333 case SpvOpAtomicAnd:
2334 case SpvOpAtomicOr:
2335 case SpvOpAtomicXor:
2336 image = *vtn_value(b, w[3], vtn_value_type_image_pointer)->image;
2337 break;
2338
2339 case SpvOpAtomicStore:
2340 image = *vtn_value(b, w[1], vtn_value_type_image_pointer)->image;
2341 break;
2342
2343 case SpvOpImageQuerySize:
2344 image.image = vtn_value(b, w[3], vtn_value_type_pointer)->pointer;
2345 image.coord = NULL;
2346 image.sample = NULL;
2347 break;
2348
2349 case SpvOpImageRead:
2350 image.image = vtn_value(b, w[3], vtn_value_type_pointer)->pointer;
2351 image.coord = get_image_coord(b, w[4]);
2352
2353 if (count > 5 && (w[5] & SpvImageOperandsSampleMask)) {
2354 vtn_assert(w[5] == SpvImageOperandsSampleMask);
2355 image.sample = vtn_ssa_value(b, w[6])->def;
2356 } else {
2357 image.sample = nir_ssa_undef(&b->nb, 1, 32);
2358 }
2359 break;
2360
2361 case SpvOpImageWrite:
2362 image.image = vtn_value(b, w[1], vtn_value_type_pointer)->pointer;
2363 image.coord = get_image_coord(b, w[2]);
2364
2365 /* texel = w[3] */
2366
2367 if (count > 4 && (w[4] & SpvImageOperandsSampleMask)) {
2368 vtn_assert(w[4] == SpvImageOperandsSampleMask);
2369 image.sample = vtn_ssa_value(b, w[5])->def;
2370 } else {
2371 image.sample = nir_ssa_undef(&b->nb, 1, 32);
2372 }
2373 break;
2374
2375 default:
2376 vtn_fail("Invalid image opcode");
2377 }
2378
2379 nir_intrinsic_op op;
2380 switch (opcode) {
2381 #define OP(S, N) case SpvOp##S: op = nir_intrinsic_image_deref_##N; break;
2382 OP(ImageQuerySize, size)
2383 OP(ImageRead, load)
2384 OP(ImageWrite, store)
2385 OP(AtomicLoad, load)
2386 OP(AtomicStore, store)
2387 OP(AtomicExchange, atomic_exchange)
2388 OP(AtomicCompareExchange, atomic_comp_swap)
2389 OP(AtomicIIncrement, atomic_add)
2390 OP(AtomicIDecrement, atomic_add)
2391 OP(AtomicIAdd, atomic_add)
2392 OP(AtomicISub, atomic_add)
2393 OP(AtomicSMin, atomic_min)
2394 OP(AtomicUMin, atomic_min)
2395 OP(AtomicSMax, atomic_max)
2396 OP(AtomicUMax, atomic_max)
2397 OP(AtomicAnd, atomic_and)
2398 OP(AtomicOr, atomic_or)
2399 OP(AtomicXor, atomic_xor)
2400 #undef OP
2401 default:
2402 vtn_fail("Invalid image opcode");
2403 }
2404
2405 nir_intrinsic_instr *intrin = nir_intrinsic_instr_create(b->shader, op);
2406
2407 nir_deref_instr *image_deref = vtn_pointer_to_deref(b, image.image);
2408 intrin->src[0] = nir_src_for_ssa(&image_deref->dest.ssa);
2409
2410 /* ImageQuerySize doesn't take any extra parameters */
2411 if (opcode != SpvOpImageQuerySize) {
2412 /* The image coordinate is always 4 components but we may not have that
2413 * many. Swizzle to compensate.
2414 */
2415 intrin->src[1] = nir_src_for_ssa(expand_to_vec4(&b->nb, image.coord));
2416 intrin->src[2] = nir_src_for_ssa(image.sample);
2417 }
2418
2419 switch (opcode) {
2420 case SpvOpAtomicLoad:
2421 case SpvOpImageQuerySize:
2422 case SpvOpImageRead:
2423 break;
2424 case SpvOpAtomicStore:
2425 case SpvOpImageWrite: {
2426 const uint32_t value_id = opcode == SpvOpAtomicStore ? w[4] : w[3];
2427 nir_ssa_def *value = vtn_ssa_value(b, value_id)->def;
2428 /* nir_intrinsic_image_deref_store always takes a vec4 value */
2429 assert(op == nir_intrinsic_image_deref_store);
2430 intrin->num_components = 4;
2431 intrin->src[3] = nir_src_for_ssa(expand_to_vec4(&b->nb, value));
2432 break;
2433 }
2434
2435 case SpvOpAtomicCompareExchange:
2436 case SpvOpAtomicIIncrement:
2437 case SpvOpAtomicIDecrement:
2438 case SpvOpAtomicExchange:
2439 case SpvOpAtomicIAdd:
2440 case SpvOpAtomicISub:
2441 case SpvOpAtomicSMin:
2442 case SpvOpAtomicUMin:
2443 case SpvOpAtomicSMax:
2444 case SpvOpAtomicUMax:
2445 case SpvOpAtomicAnd:
2446 case SpvOpAtomicOr:
2447 case SpvOpAtomicXor:
2448 fill_common_atomic_sources(b, opcode, w, &intrin->src[3]);
2449 break;
2450
2451 default:
2452 vtn_fail("Invalid image opcode");
2453 }
2454
2455 if (opcode != SpvOpImageWrite && opcode != SpvOpAtomicStore) {
2456 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
2457 struct vtn_type *type = vtn_value(b, w[1], vtn_value_type_type)->type;
2458
2459 unsigned dest_components = glsl_get_vector_elements(type->type);
2460 intrin->num_components = nir_intrinsic_infos[op].dest_components;
2461 if (intrin->num_components == 0)
2462 intrin->num_components = dest_components;
2463
2464 nir_ssa_dest_init(&intrin->instr, &intrin->dest,
2465 intrin->num_components, 32, NULL);
2466
2467 nir_builder_instr_insert(&b->nb, &intrin->instr);
2468
2469 nir_ssa_def *result = &intrin->dest.ssa;
2470 if (intrin->num_components != dest_components)
2471 result = nir_channels(&b->nb, result, (1 << dest_components) - 1);
2472
2473 val->ssa = vtn_create_ssa_value(b, type->type);
2474 val->ssa->def = result;
2475 } else {
2476 nir_builder_instr_insert(&b->nb, &intrin->instr);
2477 }
2478 }
2479
2480 static nir_intrinsic_op
2481 get_ssbo_nir_atomic_op(struct vtn_builder *b, SpvOp opcode)
2482 {
2483 switch (opcode) {
2484 case SpvOpAtomicLoad: return nir_intrinsic_load_ssbo;
2485 case SpvOpAtomicStore: return nir_intrinsic_store_ssbo;
2486 #define OP(S, N) case SpvOp##S: return nir_intrinsic_ssbo_##N;
2487 OP(AtomicExchange, atomic_exchange)
2488 OP(AtomicCompareExchange, atomic_comp_swap)
2489 OP(AtomicIIncrement, atomic_add)
2490 OP(AtomicIDecrement, atomic_add)
2491 OP(AtomicIAdd, atomic_add)
2492 OP(AtomicISub, atomic_add)
2493 OP(AtomicSMin, atomic_imin)
2494 OP(AtomicUMin, atomic_umin)
2495 OP(AtomicSMax, atomic_imax)
2496 OP(AtomicUMax, atomic_umax)
2497 OP(AtomicAnd, atomic_and)
2498 OP(AtomicOr, atomic_or)
2499 OP(AtomicXor, atomic_xor)
2500 #undef OP
2501 default:
2502 vtn_fail("Invalid SSBO atomic");
2503 }
2504 }
2505
2506 static nir_intrinsic_op
2507 get_uniform_nir_atomic_op(struct vtn_builder *b, SpvOp opcode)
2508 {
2509 switch (opcode) {
2510 #define OP(S, N) case SpvOp##S: return nir_intrinsic_atomic_counter_ ##N;
2511 OP(AtomicLoad, read_deref)
2512 OP(AtomicExchange, exchange)
2513 OP(AtomicCompareExchange, comp_swap)
2514 OP(AtomicIIncrement, inc_deref)
2515 OP(AtomicIDecrement, post_dec_deref)
2516 OP(AtomicIAdd, add_deref)
2517 OP(AtomicISub, add_deref)
2518 OP(AtomicUMin, min_deref)
2519 OP(AtomicUMax, max_deref)
2520 OP(AtomicAnd, and_deref)
2521 OP(AtomicOr, or_deref)
2522 OP(AtomicXor, xor_deref)
2523 #undef OP
2524 default:
2525 /* We left the following out: AtomicStore, AtomicSMin and
2526 * AtomicSmax. Right now there are not nir intrinsics for them. At this
2527 * moment Atomic Counter support is needed for ARB_spirv support, so is
2528 * only need to support GLSL Atomic Counters that are uints and don't
2529 * allow direct storage.
2530 */
2531 unreachable("Invalid uniform atomic");
2532 }
2533 }
2534
2535 static nir_intrinsic_op
2536 get_shared_nir_atomic_op(struct vtn_builder *b, SpvOp opcode)
2537 {
2538 switch (opcode) {
2539 case SpvOpAtomicLoad: return nir_intrinsic_load_shared;
2540 case SpvOpAtomicStore: return nir_intrinsic_store_shared;
2541 #define OP(S, N) case SpvOp##S: return nir_intrinsic_shared_##N;
2542 OP(AtomicExchange, atomic_exchange)
2543 OP(AtomicCompareExchange, atomic_comp_swap)
2544 OP(AtomicIIncrement, atomic_add)
2545 OP(AtomicIDecrement, atomic_add)
2546 OP(AtomicIAdd, atomic_add)
2547 OP(AtomicISub, atomic_add)
2548 OP(AtomicSMin, atomic_imin)
2549 OP(AtomicUMin, atomic_umin)
2550 OP(AtomicSMax, atomic_imax)
2551 OP(AtomicUMax, atomic_umax)
2552 OP(AtomicAnd, atomic_and)
2553 OP(AtomicOr, atomic_or)
2554 OP(AtomicXor, atomic_xor)
2555 #undef OP
2556 default:
2557 vtn_fail("Invalid shared atomic");
2558 }
2559 }
2560
2561 static nir_intrinsic_op
2562 get_deref_nir_atomic_op(struct vtn_builder *b, SpvOp opcode)
2563 {
2564 switch (opcode) {
2565 case SpvOpAtomicLoad: return nir_intrinsic_load_deref;
2566 case SpvOpAtomicStore: return nir_intrinsic_store_deref;
2567 #define OP(S, N) case SpvOp##S: return nir_intrinsic_deref_##N;
2568 OP(AtomicExchange, atomic_exchange)
2569 OP(AtomicCompareExchange, atomic_comp_swap)
2570 OP(AtomicIIncrement, atomic_add)
2571 OP(AtomicIDecrement, atomic_add)
2572 OP(AtomicIAdd, atomic_add)
2573 OP(AtomicISub, atomic_add)
2574 OP(AtomicSMin, atomic_imin)
2575 OP(AtomicUMin, atomic_umin)
2576 OP(AtomicSMax, atomic_imax)
2577 OP(AtomicUMax, atomic_umax)
2578 OP(AtomicAnd, atomic_and)
2579 OP(AtomicOr, atomic_or)
2580 OP(AtomicXor, atomic_xor)
2581 #undef OP
2582 default:
2583 vtn_fail("Invalid shared atomic");
2584 }
2585 }
2586
2587 /*
2588 * Handles shared atomics, ssbo atomics and atomic counters.
2589 */
2590 static void
2591 vtn_handle_atomics(struct vtn_builder *b, SpvOp opcode,
2592 const uint32_t *w, unsigned count)
2593 {
2594 struct vtn_pointer *ptr;
2595 nir_intrinsic_instr *atomic;
2596
2597 switch (opcode) {
2598 case SpvOpAtomicLoad:
2599 case SpvOpAtomicExchange:
2600 case SpvOpAtomicCompareExchange:
2601 case SpvOpAtomicCompareExchangeWeak:
2602 case SpvOpAtomicIIncrement:
2603 case SpvOpAtomicIDecrement:
2604 case SpvOpAtomicIAdd:
2605 case SpvOpAtomicISub:
2606 case SpvOpAtomicSMin:
2607 case SpvOpAtomicUMin:
2608 case SpvOpAtomicSMax:
2609 case SpvOpAtomicUMax:
2610 case SpvOpAtomicAnd:
2611 case SpvOpAtomicOr:
2612 case SpvOpAtomicXor:
2613 ptr = vtn_value(b, w[3], vtn_value_type_pointer)->pointer;
2614 break;
2615
2616 case SpvOpAtomicStore:
2617 ptr = vtn_value(b, w[1], vtn_value_type_pointer)->pointer;
2618 break;
2619
2620 default:
2621 vtn_fail("Invalid SPIR-V atomic");
2622 }
2623
2624 /*
2625 SpvScope scope = w[4];
2626 SpvMemorySemanticsMask semantics = w[5];
2627 */
2628
2629 /* uniform as "atomic counter uniform" */
2630 if (ptr->mode == vtn_variable_mode_uniform) {
2631 nir_deref_instr *deref = vtn_pointer_to_deref(b, ptr);
2632 const struct glsl_type *deref_type = deref->type;
2633 nir_intrinsic_op op = get_uniform_nir_atomic_op(b, opcode);
2634 atomic = nir_intrinsic_instr_create(b->nb.shader, op);
2635 atomic->src[0] = nir_src_for_ssa(&deref->dest.ssa);
2636
2637 /* SSBO needs to initialize index/offset. In this case we don't need to,
2638 * as that info is already stored on the ptr->var->var nir_variable (see
2639 * vtn_create_variable)
2640 */
2641
2642 switch (opcode) {
2643 case SpvOpAtomicLoad:
2644 atomic->num_components = glsl_get_vector_elements(deref_type);
2645 break;
2646
2647 case SpvOpAtomicStore:
2648 atomic->num_components = glsl_get_vector_elements(deref_type);
2649 nir_intrinsic_set_write_mask(atomic, (1 << atomic->num_components) - 1);
2650 break;
2651
2652 case SpvOpAtomicExchange:
2653 case SpvOpAtomicCompareExchange:
2654 case SpvOpAtomicCompareExchangeWeak:
2655 case SpvOpAtomicIIncrement:
2656 case SpvOpAtomicIDecrement:
2657 case SpvOpAtomicIAdd:
2658 case SpvOpAtomicISub:
2659 case SpvOpAtomicSMin:
2660 case SpvOpAtomicUMin:
2661 case SpvOpAtomicSMax:
2662 case SpvOpAtomicUMax:
2663 case SpvOpAtomicAnd:
2664 case SpvOpAtomicOr:
2665 case SpvOpAtomicXor:
2666 /* Nothing: we don't need to call fill_common_atomic_sources here, as
2667 * atomic counter uniforms doesn't have sources
2668 */
2669 break;
2670
2671 default:
2672 unreachable("Invalid SPIR-V atomic");
2673
2674 }
2675 } else if (ptr->mode == vtn_variable_mode_workgroup &&
2676 !b->options->lower_workgroup_access_to_offsets) {
2677 nir_deref_instr *deref = vtn_pointer_to_deref(b, ptr);
2678 const struct glsl_type *deref_type = deref->type;
2679 nir_intrinsic_op op = get_deref_nir_atomic_op(b, opcode);
2680 atomic = nir_intrinsic_instr_create(b->nb.shader, op);
2681 atomic->src[0] = nir_src_for_ssa(&deref->dest.ssa);
2682
2683 switch (opcode) {
2684 case SpvOpAtomicLoad:
2685 atomic->num_components = glsl_get_vector_elements(deref_type);
2686 break;
2687
2688 case SpvOpAtomicStore:
2689 atomic->num_components = glsl_get_vector_elements(deref_type);
2690 nir_intrinsic_set_write_mask(atomic, (1 << atomic->num_components) - 1);
2691 atomic->src[1] = nir_src_for_ssa(vtn_ssa_value(b, w[4])->def);
2692 break;
2693
2694 case SpvOpAtomicExchange:
2695 case SpvOpAtomicCompareExchange:
2696 case SpvOpAtomicCompareExchangeWeak:
2697 case SpvOpAtomicIIncrement:
2698 case SpvOpAtomicIDecrement:
2699 case SpvOpAtomicIAdd:
2700 case SpvOpAtomicISub:
2701 case SpvOpAtomicSMin:
2702 case SpvOpAtomicUMin:
2703 case SpvOpAtomicSMax:
2704 case SpvOpAtomicUMax:
2705 case SpvOpAtomicAnd:
2706 case SpvOpAtomicOr:
2707 case SpvOpAtomicXor:
2708 fill_common_atomic_sources(b, opcode, w, &atomic->src[1]);
2709 break;
2710
2711 default:
2712 vtn_fail("Invalid SPIR-V atomic");
2713
2714 }
2715 } else {
2716 nir_ssa_def *offset, *index;
2717 offset = vtn_pointer_to_offset(b, ptr, &index);
2718
2719 nir_intrinsic_op op;
2720 if (ptr->mode == vtn_variable_mode_ssbo) {
2721 op = get_ssbo_nir_atomic_op(b, opcode);
2722 } else {
2723 vtn_assert(ptr->mode == vtn_variable_mode_workgroup &&
2724 b->options->lower_workgroup_access_to_offsets);
2725 op = get_shared_nir_atomic_op(b, opcode);
2726 }
2727
2728 atomic = nir_intrinsic_instr_create(b->nb.shader, op);
2729
2730 int src = 0;
2731 switch (opcode) {
2732 case SpvOpAtomicLoad:
2733 atomic->num_components = glsl_get_vector_elements(ptr->type->type);
2734 if (ptr->mode == vtn_variable_mode_ssbo)
2735 atomic->src[src++] = nir_src_for_ssa(index);
2736 atomic->src[src++] = nir_src_for_ssa(offset);
2737 break;
2738
2739 case SpvOpAtomicStore:
2740 atomic->num_components = glsl_get_vector_elements(ptr->type->type);
2741 nir_intrinsic_set_write_mask(atomic, (1 << atomic->num_components) - 1);
2742 atomic->src[src++] = nir_src_for_ssa(vtn_ssa_value(b, w[4])->def);
2743 if (ptr->mode == vtn_variable_mode_ssbo)
2744 atomic->src[src++] = nir_src_for_ssa(index);
2745 atomic->src[src++] = nir_src_for_ssa(offset);
2746 break;
2747
2748 case SpvOpAtomicExchange:
2749 case SpvOpAtomicCompareExchange:
2750 case SpvOpAtomicCompareExchangeWeak:
2751 case SpvOpAtomicIIncrement:
2752 case SpvOpAtomicIDecrement:
2753 case SpvOpAtomicIAdd:
2754 case SpvOpAtomicISub:
2755 case SpvOpAtomicSMin:
2756 case SpvOpAtomicUMin:
2757 case SpvOpAtomicSMax:
2758 case SpvOpAtomicUMax:
2759 case SpvOpAtomicAnd:
2760 case SpvOpAtomicOr:
2761 case SpvOpAtomicXor:
2762 if (ptr->mode == vtn_variable_mode_ssbo)
2763 atomic->src[src++] = nir_src_for_ssa(index);
2764 atomic->src[src++] = nir_src_for_ssa(offset);
2765 fill_common_atomic_sources(b, opcode, w, &atomic->src[src]);
2766 break;
2767
2768 default:
2769 vtn_fail("Invalid SPIR-V atomic");
2770 }
2771 }
2772
2773 if (opcode != SpvOpAtomicStore) {
2774 struct vtn_type *type = vtn_value(b, w[1], vtn_value_type_type)->type;
2775
2776 nir_ssa_dest_init(&atomic->instr, &atomic->dest,
2777 glsl_get_vector_elements(type->type),
2778 glsl_get_bit_size(type->type), NULL);
2779
2780 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
2781 val->ssa = rzalloc(b, struct vtn_ssa_value);
2782 val->ssa->def = &atomic->dest.ssa;
2783 val->ssa->type = type->type;
2784 }
2785
2786 nir_builder_instr_insert(&b->nb, &atomic->instr);
2787 }
2788
2789 static nir_alu_instr *
2790 create_vec(struct vtn_builder *b, unsigned num_components, unsigned bit_size)
2791 {
2792 nir_op op;
2793 switch (num_components) {
2794 case 1: op = nir_op_imov; break;
2795 case 2: op = nir_op_vec2; break;
2796 case 3: op = nir_op_vec3; break;
2797 case 4: op = nir_op_vec4; break;
2798 default: vtn_fail("bad vector size");
2799 }
2800
2801 nir_alu_instr *vec = nir_alu_instr_create(b->shader, op);
2802 nir_ssa_dest_init(&vec->instr, &vec->dest.dest, num_components,
2803 bit_size, NULL);
2804 vec->dest.write_mask = (1 << num_components) - 1;
2805
2806 return vec;
2807 }
2808
2809 struct vtn_ssa_value *
2810 vtn_ssa_transpose(struct vtn_builder *b, struct vtn_ssa_value *src)
2811 {
2812 if (src->transposed)
2813 return src->transposed;
2814
2815 struct vtn_ssa_value *dest =
2816 vtn_create_ssa_value(b, glsl_transposed_type(src->type));
2817
2818 for (unsigned i = 0; i < glsl_get_matrix_columns(dest->type); i++) {
2819 nir_alu_instr *vec = create_vec(b, glsl_get_matrix_columns(src->type),
2820 glsl_get_bit_size(src->type));
2821 if (glsl_type_is_vector_or_scalar(src->type)) {
2822 vec->src[0].src = nir_src_for_ssa(src->def);
2823 vec->src[0].swizzle[0] = i;
2824 } else {
2825 for (unsigned j = 0; j < glsl_get_matrix_columns(src->type); j++) {
2826 vec->src[j].src = nir_src_for_ssa(src->elems[j]->def);
2827 vec->src[j].swizzle[0] = i;
2828 }
2829 }
2830 nir_builder_instr_insert(&b->nb, &vec->instr);
2831 dest->elems[i]->def = &vec->dest.dest.ssa;
2832 }
2833
2834 dest->transposed = src;
2835
2836 return dest;
2837 }
2838
2839 nir_ssa_def *
2840 vtn_vector_extract(struct vtn_builder *b, nir_ssa_def *src, unsigned index)
2841 {
2842 return nir_channel(&b->nb, src, index);
2843 }
2844
2845 nir_ssa_def *
2846 vtn_vector_insert(struct vtn_builder *b, nir_ssa_def *src, nir_ssa_def *insert,
2847 unsigned index)
2848 {
2849 nir_alu_instr *vec = create_vec(b, src->num_components,
2850 src->bit_size);
2851
2852 for (unsigned i = 0; i < src->num_components; i++) {
2853 if (i == index) {
2854 vec->src[i].src = nir_src_for_ssa(insert);
2855 } else {
2856 vec->src[i].src = nir_src_for_ssa(src);
2857 vec->src[i].swizzle[0] = i;
2858 }
2859 }
2860
2861 nir_builder_instr_insert(&b->nb, &vec->instr);
2862
2863 return &vec->dest.dest.ssa;
2864 }
2865
2866 nir_ssa_def *
2867 vtn_vector_extract_dynamic(struct vtn_builder *b, nir_ssa_def *src,
2868 nir_ssa_def *index)
2869 {
2870 nir_ssa_def *dest = vtn_vector_extract(b, src, 0);
2871 for (unsigned i = 1; i < src->num_components; i++)
2872 dest = nir_bcsel(&b->nb, nir_ieq(&b->nb, index, nir_imm_int(&b->nb, i)),
2873 vtn_vector_extract(b, src, i), dest);
2874
2875 return dest;
2876 }
2877
2878 nir_ssa_def *
2879 vtn_vector_insert_dynamic(struct vtn_builder *b, nir_ssa_def *src,
2880 nir_ssa_def *insert, nir_ssa_def *index)
2881 {
2882 nir_ssa_def *dest = vtn_vector_insert(b, src, insert, 0);
2883 for (unsigned i = 1; i < src->num_components; i++)
2884 dest = nir_bcsel(&b->nb, nir_ieq(&b->nb, index, nir_imm_int(&b->nb, i)),
2885 vtn_vector_insert(b, src, insert, i), dest);
2886
2887 return dest;
2888 }
2889
2890 static nir_ssa_def *
2891 vtn_vector_shuffle(struct vtn_builder *b, unsigned num_components,
2892 nir_ssa_def *src0, nir_ssa_def *src1,
2893 const uint32_t *indices)
2894 {
2895 nir_alu_instr *vec = create_vec(b, num_components, src0->bit_size);
2896
2897 for (unsigned i = 0; i < num_components; i++) {
2898 uint32_t index = indices[i];
2899 if (index == 0xffffffff) {
2900 vec->src[i].src =
2901 nir_src_for_ssa(nir_ssa_undef(&b->nb, 1, src0->bit_size));
2902 } else if (index < src0->num_components) {
2903 vec->src[i].src = nir_src_for_ssa(src0);
2904 vec->src[i].swizzle[0] = index;
2905 } else {
2906 vec->src[i].src = nir_src_for_ssa(src1);
2907 vec->src[i].swizzle[0] = index - src0->num_components;
2908 }
2909 }
2910
2911 nir_builder_instr_insert(&b->nb, &vec->instr);
2912
2913 return &vec->dest.dest.ssa;
2914 }
2915
2916 /*
2917 * Concatentates a number of vectors/scalars together to produce a vector
2918 */
2919 static nir_ssa_def *
2920 vtn_vector_construct(struct vtn_builder *b, unsigned num_components,
2921 unsigned num_srcs, nir_ssa_def **srcs)
2922 {
2923 nir_alu_instr *vec = create_vec(b, num_components, srcs[0]->bit_size);
2924
2925 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
2926 *
2927 * "When constructing a vector, there must be at least two Constituent
2928 * operands."
2929 */
2930 vtn_assert(num_srcs >= 2);
2931
2932 unsigned dest_idx = 0;
2933 for (unsigned i = 0; i < num_srcs; i++) {
2934 nir_ssa_def *src = srcs[i];
2935 vtn_assert(dest_idx + src->num_components <= num_components);
2936 for (unsigned j = 0; j < src->num_components; j++) {
2937 vec->src[dest_idx].src = nir_src_for_ssa(src);
2938 vec->src[dest_idx].swizzle[0] = j;
2939 dest_idx++;
2940 }
2941 }
2942
2943 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
2944 *
2945 * "When constructing a vector, the total number of components in all
2946 * the operands must equal the number of components in Result Type."
2947 */
2948 vtn_assert(dest_idx == num_components);
2949
2950 nir_builder_instr_insert(&b->nb, &vec->instr);
2951
2952 return &vec->dest.dest.ssa;
2953 }
2954
2955 static struct vtn_ssa_value *
2956 vtn_composite_copy(void *mem_ctx, struct vtn_ssa_value *src)
2957 {
2958 struct vtn_ssa_value *dest = rzalloc(mem_ctx, struct vtn_ssa_value);
2959 dest->type = src->type;
2960
2961 if (glsl_type_is_vector_or_scalar(src->type)) {
2962 dest->def = src->def;
2963 } else {
2964 unsigned elems = glsl_get_length(src->type);
2965
2966 dest->elems = ralloc_array(mem_ctx, struct vtn_ssa_value *, elems);
2967 for (unsigned i = 0; i < elems; i++)
2968 dest->elems[i] = vtn_composite_copy(mem_ctx, src->elems[i]);
2969 }
2970
2971 return dest;
2972 }
2973
2974 static struct vtn_ssa_value *
2975 vtn_composite_insert(struct vtn_builder *b, struct vtn_ssa_value *src,
2976 struct vtn_ssa_value *insert, const uint32_t *indices,
2977 unsigned num_indices)
2978 {
2979 struct vtn_ssa_value *dest = vtn_composite_copy(b, src);
2980
2981 struct vtn_ssa_value *cur = dest;
2982 unsigned i;
2983 for (i = 0; i < num_indices - 1; i++) {
2984 cur = cur->elems[indices[i]];
2985 }
2986
2987 if (glsl_type_is_vector_or_scalar(cur->type)) {
2988 /* According to the SPIR-V spec, OpCompositeInsert may work down to
2989 * the component granularity. In that case, the last index will be
2990 * the index to insert the scalar into the vector.
2991 */
2992
2993 cur->def = vtn_vector_insert(b, cur->def, insert->def, indices[i]);
2994 } else {
2995 cur->elems[indices[i]] = insert;
2996 }
2997
2998 return dest;
2999 }
3000
3001 static struct vtn_ssa_value *
3002 vtn_composite_extract(struct vtn_builder *b, struct vtn_ssa_value *src,
3003 const uint32_t *indices, unsigned num_indices)
3004 {
3005 struct vtn_ssa_value *cur = src;
3006 for (unsigned i = 0; i < num_indices; i++) {
3007 if (glsl_type_is_vector_or_scalar(cur->type)) {
3008 vtn_assert(i == num_indices - 1);
3009 /* According to the SPIR-V spec, OpCompositeExtract may work down to
3010 * the component granularity. The last index will be the index of the
3011 * vector to extract.
3012 */
3013
3014 struct vtn_ssa_value *ret = rzalloc(b, struct vtn_ssa_value);
3015 ret->type = glsl_scalar_type(glsl_get_base_type(cur->type));
3016 ret->def = vtn_vector_extract(b, cur->def, indices[i]);
3017 return ret;
3018 } else {
3019 cur = cur->elems[indices[i]];
3020 }
3021 }
3022
3023 return cur;
3024 }
3025
3026 static void
3027 vtn_handle_composite(struct vtn_builder *b, SpvOp opcode,
3028 const uint32_t *w, unsigned count)
3029 {
3030 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
3031 const struct glsl_type *type =
3032 vtn_value(b, w[1], vtn_value_type_type)->type->type;
3033 val->ssa = vtn_create_ssa_value(b, type);
3034
3035 switch (opcode) {
3036 case SpvOpVectorExtractDynamic:
3037 val->ssa->def = vtn_vector_extract_dynamic(b, vtn_ssa_value(b, w[3])->def,
3038 vtn_ssa_value(b, w[4])->def);
3039 break;
3040
3041 case SpvOpVectorInsertDynamic:
3042 val->ssa->def = vtn_vector_insert_dynamic(b, vtn_ssa_value(b, w[3])->def,
3043 vtn_ssa_value(b, w[4])->def,
3044 vtn_ssa_value(b, w[5])->def);
3045 break;
3046
3047 case SpvOpVectorShuffle:
3048 val->ssa->def = vtn_vector_shuffle(b, glsl_get_vector_elements(type),
3049 vtn_ssa_value(b, w[3])->def,
3050 vtn_ssa_value(b, w[4])->def,
3051 w + 5);
3052 break;
3053
3054 case SpvOpCompositeConstruct: {
3055 unsigned elems = count - 3;
3056 assume(elems >= 1);
3057 if (glsl_type_is_vector_or_scalar(type)) {
3058 nir_ssa_def *srcs[NIR_MAX_VEC_COMPONENTS];
3059 for (unsigned i = 0; i < elems; i++)
3060 srcs[i] = vtn_ssa_value(b, w[3 + i])->def;
3061 val->ssa->def =
3062 vtn_vector_construct(b, glsl_get_vector_elements(type),
3063 elems, srcs);
3064 } else {
3065 val->ssa->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
3066 for (unsigned i = 0; i < elems; i++)
3067 val->ssa->elems[i] = vtn_ssa_value(b, w[3 + i]);
3068 }
3069 break;
3070 }
3071 case SpvOpCompositeExtract:
3072 val->ssa = vtn_composite_extract(b, vtn_ssa_value(b, w[3]),
3073 w + 4, count - 4);
3074 break;
3075
3076 case SpvOpCompositeInsert:
3077 val->ssa = vtn_composite_insert(b, vtn_ssa_value(b, w[4]),
3078 vtn_ssa_value(b, w[3]),
3079 w + 5, count - 5);
3080 break;
3081
3082 case SpvOpCopyObject:
3083 val->ssa = vtn_composite_copy(b, vtn_ssa_value(b, w[3]));
3084 break;
3085
3086 default:
3087 vtn_fail("unknown composite operation");
3088 }
3089 }
3090
3091 static void
3092 vtn_emit_barrier(struct vtn_builder *b, nir_intrinsic_op op)
3093 {
3094 nir_intrinsic_instr *intrin = nir_intrinsic_instr_create(b->shader, op);
3095 nir_builder_instr_insert(&b->nb, &intrin->instr);
3096 }
3097
3098 static void
3099 vtn_emit_memory_barrier(struct vtn_builder *b, SpvScope scope,
3100 SpvMemorySemanticsMask semantics)
3101 {
3102 static const SpvMemorySemanticsMask all_memory_semantics =
3103 SpvMemorySemanticsUniformMemoryMask |
3104 SpvMemorySemanticsWorkgroupMemoryMask |
3105 SpvMemorySemanticsAtomicCounterMemoryMask |
3106 SpvMemorySemanticsImageMemoryMask;
3107
3108 /* If we're not actually doing a memory barrier, bail */
3109 if (!(semantics & all_memory_semantics))
3110 return;
3111
3112 /* GL and Vulkan don't have these */
3113 vtn_assert(scope != SpvScopeCrossDevice);
3114
3115 if (scope == SpvScopeSubgroup)
3116 return; /* Nothing to do here */
3117
3118 if (scope == SpvScopeWorkgroup) {
3119 vtn_emit_barrier(b, nir_intrinsic_group_memory_barrier);
3120 return;
3121 }
3122
3123 /* There's only two scopes thing left */
3124 vtn_assert(scope == SpvScopeInvocation || scope == SpvScopeDevice);
3125
3126 if ((semantics & all_memory_semantics) == all_memory_semantics) {
3127 vtn_emit_barrier(b, nir_intrinsic_memory_barrier);
3128 return;
3129 }
3130
3131 /* Issue a bunch of more specific barriers */
3132 uint32_t bits = semantics;
3133 while (bits) {
3134 SpvMemorySemanticsMask semantic = 1 << u_bit_scan(&bits);
3135 switch (semantic) {
3136 case SpvMemorySemanticsUniformMemoryMask:
3137 vtn_emit_barrier(b, nir_intrinsic_memory_barrier_buffer);
3138 break;
3139 case SpvMemorySemanticsWorkgroupMemoryMask:
3140 vtn_emit_barrier(b, nir_intrinsic_memory_barrier_shared);
3141 break;
3142 case SpvMemorySemanticsAtomicCounterMemoryMask:
3143 vtn_emit_barrier(b, nir_intrinsic_memory_barrier_atomic_counter);
3144 break;
3145 case SpvMemorySemanticsImageMemoryMask:
3146 vtn_emit_barrier(b, nir_intrinsic_memory_barrier_image);
3147 break;
3148 default:
3149 break;;
3150 }
3151 }
3152 }
3153
3154 static void
3155 vtn_handle_barrier(struct vtn_builder *b, SpvOp opcode,
3156 const uint32_t *w, unsigned count)
3157 {
3158 switch (opcode) {
3159 case SpvOpEmitVertex:
3160 case SpvOpEmitStreamVertex:
3161 case SpvOpEndPrimitive:
3162 case SpvOpEndStreamPrimitive: {
3163 nir_intrinsic_op intrinsic_op;
3164 switch (opcode) {
3165 case SpvOpEmitVertex:
3166 case SpvOpEmitStreamVertex:
3167 intrinsic_op = nir_intrinsic_emit_vertex;
3168 break;
3169 case SpvOpEndPrimitive:
3170 case SpvOpEndStreamPrimitive:
3171 intrinsic_op = nir_intrinsic_end_primitive;
3172 break;
3173 default:
3174 unreachable("Invalid opcode");
3175 }
3176
3177 nir_intrinsic_instr *intrin =
3178 nir_intrinsic_instr_create(b->shader, intrinsic_op);
3179
3180 switch (opcode) {
3181 case SpvOpEmitStreamVertex:
3182 case SpvOpEndStreamPrimitive: {
3183 unsigned stream = vtn_constant_value(b, w[1])->values[0].u32[0];
3184 nir_intrinsic_set_stream_id(intrin, stream);
3185 break;
3186 }
3187
3188 default:
3189 break;
3190 }
3191
3192 nir_builder_instr_insert(&b->nb, &intrin->instr);
3193 break;
3194 }
3195
3196 case SpvOpMemoryBarrier: {
3197 SpvScope scope = vtn_constant_value(b, w[1])->values[0].u32[0];
3198 SpvMemorySemanticsMask semantics =
3199 vtn_constant_value(b, w[2])->values[0].u32[0];
3200 vtn_emit_memory_barrier(b, scope, semantics);
3201 return;
3202 }
3203
3204 case SpvOpControlBarrier: {
3205 SpvScope execution_scope =
3206 vtn_constant_value(b, w[1])->values[0].u32[0];
3207 if (execution_scope == SpvScopeWorkgroup)
3208 vtn_emit_barrier(b, nir_intrinsic_barrier);
3209
3210 SpvScope memory_scope =
3211 vtn_constant_value(b, w[2])->values[0].u32[0];
3212 SpvMemorySemanticsMask memory_semantics =
3213 vtn_constant_value(b, w[3])->values[0].u32[0];
3214 vtn_emit_memory_barrier(b, memory_scope, memory_semantics);
3215 break;
3216 }
3217
3218 default:
3219 unreachable("unknown barrier instruction");
3220 }
3221 }
3222
3223 static unsigned
3224 gl_primitive_from_spv_execution_mode(struct vtn_builder *b,
3225 SpvExecutionMode mode)
3226 {
3227 switch (mode) {
3228 case SpvExecutionModeInputPoints:
3229 case SpvExecutionModeOutputPoints:
3230 return 0; /* GL_POINTS */
3231 case SpvExecutionModeInputLines:
3232 return 1; /* GL_LINES */
3233 case SpvExecutionModeInputLinesAdjacency:
3234 return 0x000A; /* GL_LINE_STRIP_ADJACENCY_ARB */
3235 case SpvExecutionModeTriangles:
3236 return 4; /* GL_TRIANGLES */
3237 case SpvExecutionModeInputTrianglesAdjacency:
3238 return 0x000C; /* GL_TRIANGLES_ADJACENCY_ARB */
3239 case SpvExecutionModeQuads:
3240 return 7; /* GL_QUADS */
3241 case SpvExecutionModeIsolines:
3242 return 0x8E7A; /* GL_ISOLINES */
3243 case SpvExecutionModeOutputLineStrip:
3244 return 3; /* GL_LINE_STRIP */
3245 case SpvExecutionModeOutputTriangleStrip:
3246 return 5; /* GL_TRIANGLE_STRIP */
3247 default:
3248 vtn_fail("Invalid primitive type");
3249 }
3250 }
3251
3252 static unsigned
3253 vertices_in_from_spv_execution_mode(struct vtn_builder *b,
3254 SpvExecutionMode mode)
3255 {
3256 switch (mode) {
3257 case SpvExecutionModeInputPoints:
3258 return 1;
3259 case SpvExecutionModeInputLines:
3260 return 2;
3261 case SpvExecutionModeInputLinesAdjacency:
3262 return 4;
3263 case SpvExecutionModeTriangles:
3264 return 3;
3265 case SpvExecutionModeInputTrianglesAdjacency:
3266 return 6;
3267 default:
3268 vtn_fail("Invalid GS input mode");
3269 }
3270 }
3271
3272 static gl_shader_stage
3273 stage_for_execution_model(struct vtn_builder *b, SpvExecutionModel model)
3274 {
3275 switch (model) {
3276 case SpvExecutionModelVertex:
3277 return MESA_SHADER_VERTEX;
3278 case SpvExecutionModelTessellationControl:
3279 return MESA_SHADER_TESS_CTRL;
3280 case SpvExecutionModelTessellationEvaluation:
3281 return MESA_SHADER_TESS_EVAL;
3282 case SpvExecutionModelGeometry:
3283 return MESA_SHADER_GEOMETRY;
3284 case SpvExecutionModelFragment:
3285 return MESA_SHADER_FRAGMENT;
3286 case SpvExecutionModelGLCompute:
3287 return MESA_SHADER_COMPUTE;
3288 default:
3289 vtn_fail("Unsupported execution model");
3290 }
3291 }
3292
3293 #define spv_check_supported(name, cap) do { \
3294 if (!(b->options && b->options->caps.name)) \
3295 vtn_warn("Unsupported SPIR-V capability: %s", \
3296 spirv_capability_to_string(cap)); \
3297 } while(0)
3298
3299
3300 void
3301 vtn_handle_entry_point(struct vtn_builder *b, const uint32_t *w,
3302 unsigned count)
3303 {
3304 struct vtn_value *entry_point = &b->values[w[2]];
3305 /* Let this be a name label regardless */
3306 unsigned name_words;
3307 entry_point->name = vtn_string_literal(b, &w[3], count - 3, &name_words);
3308
3309 if (strcmp(entry_point->name, b->entry_point_name) != 0 ||
3310 stage_for_execution_model(b, w[1]) != b->entry_point_stage)
3311 return;
3312
3313 vtn_assert(b->entry_point == NULL);
3314 b->entry_point = entry_point;
3315 }
3316
3317 static bool
3318 vtn_handle_preamble_instruction(struct vtn_builder *b, SpvOp opcode,
3319 const uint32_t *w, unsigned count)
3320 {
3321 switch (opcode) {
3322 case SpvOpSource: {
3323 const char *lang;
3324 switch (w[1]) {
3325 default:
3326 case SpvSourceLanguageUnknown: lang = "unknown"; break;
3327 case SpvSourceLanguageESSL: lang = "ESSL"; break;
3328 case SpvSourceLanguageGLSL: lang = "GLSL"; break;
3329 case SpvSourceLanguageOpenCL_C: lang = "OpenCL C"; break;
3330 case SpvSourceLanguageOpenCL_CPP: lang = "OpenCL C++"; break;
3331 case SpvSourceLanguageHLSL: lang = "HLSL"; break;
3332 }
3333
3334 uint32_t version = w[2];
3335
3336 const char *file =
3337 (count > 3) ? vtn_value(b, w[3], vtn_value_type_string)->str : "";
3338
3339 vtn_info("Parsing SPIR-V from %s %u source file %s", lang, version, file);
3340 break;
3341 }
3342
3343 case SpvOpSourceExtension:
3344 case SpvOpSourceContinued:
3345 case SpvOpExtension:
3346 case SpvOpModuleProcessed:
3347 /* Unhandled, but these are for debug so that's ok. */
3348 break;
3349
3350 case SpvOpCapability: {
3351 SpvCapability cap = w[1];
3352 switch (cap) {
3353 case SpvCapabilityMatrix:
3354 case SpvCapabilityShader:
3355 case SpvCapabilityGeometry:
3356 case SpvCapabilityGeometryPointSize:
3357 case SpvCapabilityUniformBufferArrayDynamicIndexing:
3358 case SpvCapabilitySampledImageArrayDynamicIndexing:
3359 case SpvCapabilityStorageBufferArrayDynamicIndexing:
3360 case SpvCapabilityStorageImageArrayDynamicIndexing:
3361 case SpvCapabilityImageRect:
3362 case SpvCapabilitySampledRect:
3363 case SpvCapabilitySampled1D:
3364 case SpvCapabilityImage1D:
3365 case SpvCapabilitySampledCubeArray:
3366 case SpvCapabilityImageCubeArray:
3367 case SpvCapabilitySampledBuffer:
3368 case SpvCapabilityImageBuffer:
3369 case SpvCapabilityImageQuery:
3370 case SpvCapabilityDerivativeControl:
3371 case SpvCapabilityInterpolationFunction:
3372 case SpvCapabilityMultiViewport:
3373 case SpvCapabilitySampleRateShading:
3374 case SpvCapabilityClipDistance:
3375 case SpvCapabilityCullDistance:
3376 case SpvCapabilityInputAttachment:
3377 case SpvCapabilityImageGatherExtended:
3378 case SpvCapabilityStorageImageExtendedFormats:
3379 break;
3380
3381 case SpvCapabilityLinkage:
3382 case SpvCapabilityVector16:
3383 case SpvCapabilityFloat16Buffer:
3384 case SpvCapabilityFloat16:
3385 case SpvCapabilityInt64Atomics:
3386 case SpvCapabilityStorageImageMultisample:
3387 case SpvCapabilityInt8:
3388 case SpvCapabilitySparseResidency:
3389 case SpvCapabilityMinLod:
3390 vtn_warn("Unsupported SPIR-V capability: %s",
3391 spirv_capability_to_string(cap));
3392 break;
3393
3394 case SpvCapabilityAtomicStorage:
3395 spv_check_supported(atomic_storage, cap);
3396 break;
3397
3398 case SpvCapabilityFloat64:
3399 spv_check_supported(float64, cap);
3400 break;
3401 case SpvCapabilityInt64:
3402 spv_check_supported(int64, cap);
3403 break;
3404 case SpvCapabilityInt16:
3405 spv_check_supported(int16, cap);
3406 break;
3407
3408 case SpvCapabilityTransformFeedback:
3409 spv_check_supported(transform_feedback, cap);
3410 break;
3411
3412 case SpvCapabilityGeometryStreams:
3413 spv_check_supported(geometry_streams, cap);
3414 break;
3415
3416 case SpvCapabilityAddresses:
3417 case SpvCapabilityKernel:
3418 case SpvCapabilityImageBasic:
3419 case SpvCapabilityImageReadWrite:
3420 case SpvCapabilityImageMipmap:
3421 case SpvCapabilityPipes:
3422 case SpvCapabilityGroups:
3423 case SpvCapabilityDeviceEnqueue:
3424 case SpvCapabilityLiteralSampler:
3425 case SpvCapabilityGenericPointer:
3426 vtn_warn("Unsupported OpenCL-style SPIR-V capability: %s",
3427 spirv_capability_to_string(cap));
3428 break;
3429
3430 case SpvCapabilityImageMSArray:
3431 spv_check_supported(image_ms_array, cap);
3432 break;
3433
3434 case SpvCapabilityTessellation:
3435 case SpvCapabilityTessellationPointSize:
3436 spv_check_supported(tessellation, cap);
3437 break;
3438
3439 case SpvCapabilityDrawParameters:
3440 spv_check_supported(draw_parameters, cap);
3441 break;
3442
3443 case SpvCapabilityStorageImageReadWithoutFormat:
3444 spv_check_supported(image_read_without_format, cap);
3445 break;
3446
3447 case SpvCapabilityStorageImageWriteWithoutFormat:
3448 spv_check_supported(image_write_without_format, cap);
3449 break;
3450
3451 case SpvCapabilityDeviceGroup:
3452 spv_check_supported(device_group, cap);
3453 break;
3454
3455 case SpvCapabilityMultiView:
3456 spv_check_supported(multiview, cap);
3457 break;
3458
3459 case SpvCapabilityGroupNonUniform:
3460 spv_check_supported(subgroup_basic, cap);
3461 break;
3462
3463 case SpvCapabilityGroupNonUniformVote:
3464 spv_check_supported(subgroup_vote, cap);
3465 break;
3466
3467 case SpvCapabilitySubgroupBallotKHR:
3468 case SpvCapabilityGroupNonUniformBallot:
3469 spv_check_supported(subgroup_ballot, cap);
3470 break;
3471
3472 case SpvCapabilityGroupNonUniformShuffle:
3473 case SpvCapabilityGroupNonUniformShuffleRelative:
3474 spv_check_supported(subgroup_shuffle, cap);
3475 break;
3476
3477 case SpvCapabilityGroupNonUniformQuad:
3478 spv_check_supported(subgroup_quad, cap);
3479 break;
3480
3481 case SpvCapabilityGroupNonUniformArithmetic:
3482 case SpvCapabilityGroupNonUniformClustered:
3483 spv_check_supported(subgroup_arithmetic, cap);
3484 break;
3485
3486 case SpvCapabilityVariablePointersStorageBuffer:
3487 case SpvCapabilityVariablePointers:
3488 spv_check_supported(variable_pointers, cap);
3489 break;
3490
3491 case SpvCapabilityStorageUniformBufferBlock16:
3492 case SpvCapabilityStorageUniform16:
3493 case SpvCapabilityStoragePushConstant16:
3494 case SpvCapabilityStorageInputOutput16:
3495 spv_check_supported(storage_16bit, cap);
3496 break;
3497
3498 case SpvCapabilityShaderViewportIndexLayerEXT:
3499 spv_check_supported(shader_viewport_index_layer, cap);
3500 break;
3501
3502 case SpvCapabilityStorageBuffer8BitAccess:
3503 case SpvCapabilityUniformAndStorageBuffer8BitAccess:
3504 case SpvCapabilityStoragePushConstant8:
3505 spv_check_supported(storage_8bit, cap);
3506 break;
3507
3508 case SpvCapabilityInputAttachmentArrayDynamicIndexingEXT:
3509 case SpvCapabilityUniformTexelBufferArrayDynamicIndexingEXT:
3510 case SpvCapabilityStorageTexelBufferArrayDynamicIndexingEXT:
3511 spv_check_supported(descriptor_array_dynamic_indexing, cap);
3512 break;
3513
3514 case SpvCapabilityRuntimeDescriptorArrayEXT:
3515 spv_check_supported(runtime_descriptor_array, cap);
3516 break;
3517
3518 case SpvCapabilityStencilExportEXT:
3519 spv_check_supported(stencil_export, cap);
3520 break;
3521
3522 case SpvCapabilitySampleMaskPostDepthCoverage:
3523 spv_check_supported(post_depth_coverage, cap);
3524 break;
3525
3526 default:
3527 vtn_fail("Unhandled capability");
3528 }
3529 break;
3530 }
3531
3532 case SpvOpExtInstImport:
3533 vtn_handle_extension(b, opcode, w, count);
3534 break;
3535
3536 case SpvOpMemoryModel:
3537 vtn_assert(w[1] == SpvAddressingModelLogical);
3538 vtn_assert(w[2] == SpvMemoryModelSimple ||
3539 w[2] == SpvMemoryModelGLSL450);
3540 break;
3541
3542 case SpvOpEntryPoint:
3543 vtn_handle_entry_point(b, w, count);
3544 break;
3545
3546 case SpvOpString:
3547 vtn_push_value(b, w[1], vtn_value_type_string)->str =
3548 vtn_string_literal(b, &w[2], count - 2, NULL);
3549 break;
3550
3551 case SpvOpName:
3552 b->values[w[1]].name = vtn_string_literal(b, &w[2], count - 2, NULL);
3553 break;
3554
3555 case SpvOpMemberName:
3556 /* TODO */
3557 break;
3558
3559 case SpvOpExecutionMode:
3560 case SpvOpDecorationGroup:
3561 case SpvOpDecorate:
3562 case SpvOpMemberDecorate:
3563 case SpvOpGroupDecorate:
3564 case SpvOpGroupMemberDecorate:
3565 case SpvOpDecorateStringGOOGLE:
3566 case SpvOpMemberDecorateStringGOOGLE:
3567 vtn_handle_decoration(b, opcode, w, count);
3568 break;
3569
3570 default:
3571 return false; /* End of preamble */
3572 }
3573
3574 return true;
3575 }
3576
3577 static void
3578 vtn_handle_execution_mode(struct vtn_builder *b, struct vtn_value *entry_point,
3579 const struct vtn_decoration *mode, void *data)
3580 {
3581 vtn_assert(b->entry_point == entry_point);
3582
3583 switch(mode->exec_mode) {
3584 case SpvExecutionModeOriginUpperLeft:
3585 case SpvExecutionModeOriginLowerLeft:
3586 b->origin_upper_left =
3587 (mode->exec_mode == SpvExecutionModeOriginUpperLeft);
3588 break;
3589
3590 case SpvExecutionModeEarlyFragmentTests:
3591 vtn_assert(b->shader->info.stage == MESA_SHADER_FRAGMENT);
3592 b->shader->info.fs.early_fragment_tests = true;
3593 break;
3594
3595 case SpvExecutionModePostDepthCoverage:
3596 vtn_assert(b->shader->info.stage == MESA_SHADER_FRAGMENT);
3597 b->shader->info.fs.post_depth_coverage = true;
3598 break;
3599
3600 case SpvExecutionModeInvocations:
3601 vtn_assert(b->shader->info.stage == MESA_SHADER_GEOMETRY);
3602 b->shader->info.gs.invocations = MAX2(1, mode->literals[0]);
3603 break;
3604
3605 case SpvExecutionModeDepthReplacing:
3606 vtn_assert(b->shader->info.stage == MESA_SHADER_FRAGMENT);
3607 b->shader->info.fs.depth_layout = FRAG_DEPTH_LAYOUT_ANY;
3608 break;
3609 case SpvExecutionModeDepthGreater:
3610 vtn_assert(b->shader->info.stage == MESA_SHADER_FRAGMENT);
3611 b->shader->info.fs.depth_layout = FRAG_DEPTH_LAYOUT_GREATER;
3612 break;
3613 case SpvExecutionModeDepthLess:
3614 vtn_assert(b->shader->info.stage == MESA_SHADER_FRAGMENT);
3615 b->shader->info.fs.depth_layout = FRAG_DEPTH_LAYOUT_LESS;
3616 break;
3617 case SpvExecutionModeDepthUnchanged:
3618 vtn_assert(b->shader->info.stage == MESA_SHADER_FRAGMENT);
3619 b->shader->info.fs.depth_layout = FRAG_DEPTH_LAYOUT_UNCHANGED;
3620 break;
3621
3622 case SpvExecutionModeLocalSize:
3623 vtn_assert(b->shader->info.stage == MESA_SHADER_COMPUTE);
3624 b->shader->info.cs.local_size[0] = mode->literals[0];
3625 b->shader->info.cs.local_size[1] = mode->literals[1];
3626 b->shader->info.cs.local_size[2] = mode->literals[2];
3627 break;
3628 case SpvExecutionModeLocalSizeHint:
3629 break; /* Nothing to do with this */
3630
3631 case SpvExecutionModeOutputVertices:
3632 if (b->shader->info.stage == MESA_SHADER_TESS_CTRL ||
3633 b->shader->info.stage == MESA_SHADER_TESS_EVAL) {
3634 b->shader->info.tess.tcs_vertices_out = mode->literals[0];
3635 } else {
3636 vtn_assert(b->shader->info.stage == MESA_SHADER_GEOMETRY);
3637 b->shader->info.gs.vertices_out = mode->literals[0];
3638 }
3639 break;
3640
3641 case SpvExecutionModeInputPoints:
3642 case SpvExecutionModeInputLines:
3643 case SpvExecutionModeInputLinesAdjacency:
3644 case SpvExecutionModeTriangles:
3645 case SpvExecutionModeInputTrianglesAdjacency:
3646 case SpvExecutionModeQuads:
3647 case SpvExecutionModeIsolines:
3648 if (b->shader->info.stage == MESA_SHADER_TESS_CTRL ||
3649 b->shader->info.stage == MESA_SHADER_TESS_EVAL) {
3650 b->shader->info.tess.primitive_mode =
3651 gl_primitive_from_spv_execution_mode(b, mode->exec_mode);
3652 } else {
3653 vtn_assert(b->shader->info.stage == MESA_SHADER_GEOMETRY);
3654 b->shader->info.gs.vertices_in =
3655 vertices_in_from_spv_execution_mode(b, mode->exec_mode);
3656 b->shader->info.gs.input_primitive =
3657 gl_primitive_from_spv_execution_mode(b, mode->exec_mode);
3658 }
3659 break;
3660
3661 case SpvExecutionModeOutputPoints:
3662 case SpvExecutionModeOutputLineStrip:
3663 case SpvExecutionModeOutputTriangleStrip:
3664 vtn_assert(b->shader->info.stage == MESA_SHADER_GEOMETRY);
3665 b->shader->info.gs.output_primitive =
3666 gl_primitive_from_spv_execution_mode(b, mode->exec_mode);
3667 break;
3668
3669 case SpvExecutionModeSpacingEqual:
3670 vtn_assert(b->shader->info.stage == MESA_SHADER_TESS_CTRL ||
3671 b->shader->info.stage == MESA_SHADER_TESS_EVAL);
3672 b->shader->info.tess.spacing = TESS_SPACING_EQUAL;
3673 break;
3674 case SpvExecutionModeSpacingFractionalEven:
3675 vtn_assert(b->shader->info.stage == MESA_SHADER_TESS_CTRL ||
3676 b->shader->info.stage == MESA_SHADER_TESS_EVAL);
3677 b->shader->info.tess.spacing = TESS_SPACING_FRACTIONAL_EVEN;
3678 break;
3679 case SpvExecutionModeSpacingFractionalOdd:
3680 vtn_assert(b->shader->info.stage == MESA_SHADER_TESS_CTRL ||
3681 b->shader->info.stage == MESA_SHADER_TESS_EVAL);
3682 b->shader->info.tess.spacing = TESS_SPACING_FRACTIONAL_ODD;
3683 break;
3684 case SpvExecutionModeVertexOrderCw:
3685 vtn_assert(b->shader->info.stage == MESA_SHADER_TESS_CTRL ||
3686 b->shader->info.stage == MESA_SHADER_TESS_EVAL);
3687 b->shader->info.tess.ccw = false;
3688 break;
3689 case SpvExecutionModeVertexOrderCcw:
3690 vtn_assert(b->shader->info.stage == MESA_SHADER_TESS_CTRL ||
3691 b->shader->info.stage == MESA_SHADER_TESS_EVAL);
3692 b->shader->info.tess.ccw = true;
3693 break;
3694 case SpvExecutionModePointMode:
3695 vtn_assert(b->shader->info.stage == MESA_SHADER_TESS_CTRL ||
3696 b->shader->info.stage == MESA_SHADER_TESS_EVAL);
3697 b->shader->info.tess.point_mode = true;
3698 break;
3699
3700 case SpvExecutionModePixelCenterInteger:
3701 b->pixel_center_integer = true;
3702 break;
3703
3704 case SpvExecutionModeXfb:
3705 b->shader->info.has_transform_feedback_varyings = true;
3706 break;
3707
3708 case SpvExecutionModeVecTypeHint:
3709 case SpvExecutionModeContractionOff:
3710 break; /* OpenCL */
3711
3712 case SpvExecutionModeStencilRefReplacingEXT:
3713 vtn_assert(b->shader->info.stage == MESA_SHADER_FRAGMENT);
3714 break;
3715
3716 default:
3717 vtn_fail("Unhandled execution mode");
3718 }
3719 }
3720
3721 static bool
3722 vtn_handle_variable_or_type_instruction(struct vtn_builder *b, SpvOp opcode,
3723 const uint32_t *w, unsigned count)
3724 {
3725 vtn_set_instruction_result_type(b, opcode, w, count);
3726
3727 switch (opcode) {
3728 case SpvOpSource:
3729 case SpvOpSourceContinued:
3730 case SpvOpSourceExtension:
3731 case SpvOpExtension:
3732 case SpvOpCapability:
3733 case SpvOpExtInstImport:
3734 case SpvOpMemoryModel:
3735 case SpvOpEntryPoint:
3736 case SpvOpExecutionMode:
3737 case SpvOpString:
3738 case SpvOpName:
3739 case SpvOpMemberName:
3740 case SpvOpDecorationGroup:
3741 case SpvOpDecorate:
3742 case SpvOpMemberDecorate:
3743 case SpvOpGroupDecorate:
3744 case SpvOpGroupMemberDecorate:
3745 case SpvOpDecorateStringGOOGLE:
3746 case SpvOpMemberDecorateStringGOOGLE:
3747 vtn_fail("Invalid opcode types and variables section");
3748 break;
3749
3750 case SpvOpTypeVoid:
3751 case SpvOpTypeBool:
3752 case SpvOpTypeInt:
3753 case SpvOpTypeFloat:
3754 case SpvOpTypeVector:
3755 case SpvOpTypeMatrix:
3756 case SpvOpTypeImage:
3757 case SpvOpTypeSampler:
3758 case SpvOpTypeSampledImage:
3759 case SpvOpTypeArray:
3760 case SpvOpTypeRuntimeArray:
3761 case SpvOpTypeStruct:
3762 case SpvOpTypeOpaque:
3763 case SpvOpTypePointer:
3764 case SpvOpTypeFunction:
3765 case SpvOpTypeEvent:
3766 case SpvOpTypeDeviceEvent:
3767 case SpvOpTypeReserveId:
3768 case SpvOpTypeQueue:
3769 case SpvOpTypePipe:
3770 vtn_handle_type(b, opcode, w, count);
3771 break;
3772
3773 case SpvOpConstantTrue:
3774 case SpvOpConstantFalse:
3775 case SpvOpConstant:
3776 case SpvOpConstantComposite:
3777 case SpvOpConstantSampler:
3778 case SpvOpConstantNull:
3779 case SpvOpSpecConstantTrue:
3780 case SpvOpSpecConstantFalse:
3781 case SpvOpSpecConstant:
3782 case SpvOpSpecConstantComposite:
3783 case SpvOpSpecConstantOp:
3784 vtn_handle_constant(b, opcode, w, count);
3785 break;
3786
3787 case SpvOpUndef:
3788 case SpvOpVariable:
3789 vtn_handle_variables(b, opcode, w, count);
3790 break;
3791
3792 default:
3793 return false; /* End of preamble */
3794 }
3795
3796 return true;
3797 }
3798
3799 static bool
3800 vtn_handle_body_instruction(struct vtn_builder *b, SpvOp opcode,
3801 const uint32_t *w, unsigned count)
3802 {
3803 switch (opcode) {
3804 case SpvOpLabel:
3805 break;
3806
3807 case SpvOpLoopMerge:
3808 case SpvOpSelectionMerge:
3809 /* This is handled by cfg pre-pass and walk_blocks */
3810 break;
3811
3812 case SpvOpUndef: {
3813 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_undef);
3814 val->type = vtn_value(b, w[1], vtn_value_type_type)->type;
3815 break;
3816 }
3817
3818 case SpvOpExtInst:
3819 vtn_handle_extension(b, opcode, w, count);
3820 break;
3821
3822 case SpvOpVariable:
3823 case SpvOpLoad:
3824 case SpvOpStore:
3825 case SpvOpCopyMemory:
3826 case SpvOpCopyMemorySized:
3827 case SpvOpAccessChain:
3828 case SpvOpPtrAccessChain:
3829 case SpvOpInBoundsAccessChain:
3830 case SpvOpArrayLength:
3831 vtn_handle_variables(b, opcode, w, count);
3832 break;
3833
3834 case SpvOpFunctionCall:
3835 vtn_handle_function_call(b, opcode, w, count);
3836 break;
3837
3838 case SpvOpSampledImage:
3839 case SpvOpImage:
3840 case SpvOpImageSampleImplicitLod:
3841 case SpvOpImageSampleExplicitLod:
3842 case SpvOpImageSampleDrefImplicitLod:
3843 case SpvOpImageSampleDrefExplicitLod:
3844 case SpvOpImageSampleProjImplicitLod:
3845 case SpvOpImageSampleProjExplicitLod:
3846 case SpvOpImageSampleProjDrefImplicitLod:
3847 case SpvOpImageSampleProjDrefExplicitLod:
3848 case SpvOpImageFetch:
3849 case SpvOpImageGather:
3850 case SpvOpImageDrefGather:
3851 case SpvOpImageQuerySizeLod:
3852 case SpvOpImageQueryLod:
3853 case SpvOpImageQueryLevels:
3854 case SpvOpImageQuerySamples:
3855 vtn_handle_texture(b, opcode, w, count);
3856 break;
3857
3858 case SpvOpImageRead:
3859 case SpvOpImageWrite:
3860 case SpvOpImageTexelPointer:
3861 vtn_handle_image(b, opcode, w, count);
3862 break;
3863
3864 case SpvOpImageQuerySize: {
3865 struct vtn_pointer *image =
3866 vtn_value(b, w[3], vtn_value_type_pointer)->pointer;
3867 if (glsl_type_is_image(image->type->type)) {
3868 vtn_handle_image(b, opcode, w, count);
3869 } else {
3870 vtn_assert(glsl_type_is_sampler(image->type->type));
3871 vtn_handle_texture(b, opcode, w, count);
3872 }
3873 break;
3874 }
3875
3876 case SpvOpAtomicLoad:
3877 case SpvOpAtomicExchange:
3878 case SpvOpAtomicCompareExchange:
3879 case SpvOpAtomicCompareExchangeWeak:
3880 case SpvOpAtomicIIncrement:
3881 case SpvOpAtomicIDecrement:
3882 case SpvOpAtomicIAdd:
3883 case SpvOpAtomicISub:
3884 case SpvOpAtomicSMin:
3885 case SpvOpAtomicUMin:
3886 case SpvOpAtomicSMax:
3887 case SpvOpAtomicUMax:
3888 case SpvOpAtomicAnd:
3889 case SpvOpAtomicOr:
3890 case SpvOpAtomicXor: {
3891 struct vtn_value *pointer = vtn_untyped_value(b, w[3]);
3892 if (pointer->value_type == vtn_value_type_image_pointer) {
3893 vtn_handle_image(b, opcode, w, count);
3894 } else {
3895 vtn_assert(pointer->value_type == vtn_value_type_pointer);
3896 vtn_handle_atomics(b, opcode, w, count);
3897 }
3898 break;
3899 }
3900
3901 case SpvOpAtomicStore: {
3902 struct vtn_value *pointer = vtn_untyped_value(b, w[1]);
3903 if (pointer->value_type == vtn_value_type_image_pointer) {
3904 vtn_handle_image(b, opcode, w, count);
3905 } else {
3906 vtn_assert(pointer->value_type == vtn_value_type_pointer);
3907 vtn_handle_atomics(b, opcode, w, count);
3908 }
3909 break;
3910 }
3911
3912 case SpvOpSelect: {
3913 /* Handle OpSelect up-front here because it needs to be able to handle
3914 * pointers and not just regular vectors and scalars.
3915 */
3916 struct vtn_value *res_val = vtn_untyped_value(b, w[2]);
3917 struct vtn_value *sel_val = vtn_untyped_value(b, w[3]);
3918 struct vtn_value *obj1_val = vtn_untyped_value(b, w[4]);
3919 struct vtn_value *obj2_val = vtn_untyped_value(b, w[5]);
3920
3921 const struct glsl_type *sel_type;
3922 switch (res_val->type->base_type) {
3923 case vtn_base_type_scalar:
3924 sel_type = glsl_bool_type();
3925 break;
3926 case vtn_base_type_vector:
3927 sel_type = glsl_vector_type(GLSL_TYPE_BOOL, res_val->type->length);
3928 break;
3929 case vtn_base_type_pointer:
3930 /* We need to have actual storage for pointer types */
3931 vtn_fail_if(res_val->type->type == NULL,
3932 "Invalid pointer result type for OpSelect");
3933 sel_type = glsl_bool_type();
3934 break;
3935 default:
3936 vtn_fail("Result type of OpSelect must be a scalar, vector, or pointer");
3937 }
3938
3939 if (unlikely(sel_val->type->type != sel_type)) {
3940 if (sel_val->type->type == glsl_bool_type()) {
3941 /* This case is illegal but some older versions of GLSLang produce
3942 * it. The GLSLang issue was fixed on March 30, 2017:
3943 *
3944 * https://github.com/KhronosGroup/glslang/issues/809
3945 *
3946 * Unfortunately, there are applications in the wild which are
3947 * shipping with this bug so it isn't nice to fail on them so we
3948 * throw a warning instead. It's not actually a problem for us as
3949 * nir_builder will just splat the condition out which is most
3950 * likely what the client wanted anyway.
3951 */
3952 vtn_warn("Condition type of OpSelect must have the same number "
3953 "of components as Result Type");
3954 } else {
3955 vtn_fail("Condition type of OpSelect must be a scalar or vector "
3956 "of Boolean type. It must have the same number of "
3957 "components as Result Type");
3958 }
3959 }
3960
3961 vtn_fail_if(obj1_val->type != res_val->type ||
3962 obj2_val->type != res_val->type,
3963 "Object types must match the result type in OpSelect");
3964
3965 struct vtn_type *res_type = vtn_value(b, w[1], vtn_value_type_type)->type;
3966 struct vtn_ssa_value *ssa = vtn_create_ssa_value(b, res_type->type);
3967 ssa->def = nir_bcsel(&b->nb, vtn_ssa_value(b, w[3])->def,
3968 vtn_ssa_value(b, w[4])->def,
3969 vtn_ssa_value(b, w[5])->def);
3970 vtn_push_ssa(b, w[2], res_type, ssa);
3971 break;
3972 }
3973
3974 case SpvOpSNegate:
3975 case SpvOpFNegate:
3976 case SpvOpNot:
3977 case SpvOpAny:
3978 case SpvOpAll:
3979 case SpvOpConvertFToU:
3980 case SpvOpConvertFToS:
3981 case SpvOpConvertSToF:
3982 case SpvOpConvertUToF:
3983 case SpvOpUConvert:
3984 case SpvOpSConvert:
3985 case SpvOpFConvert:
3986 case SpvOpQuantizeToF16:
3987 case SpvOpConvertPtrToU:
3988 case SpvOpConvertUToPtr:
3989 case SpvOpPtrCastToGeneric:
3990 case SpvOpGenericCastToPtr:
3991 case SpvOpBitcast:
3992 case SpvOpIsNan:
3993 case SpvOpIsInf:
3994 case SpvOpIsFinite:
3995 case SpvOpIsNormal:
3996 case SpvOpSignBitSet:
3997 case SpvOpLessOrGreater:
3998 case SpvOpOrdered:
3999 case SpvOpUnordered:
4000 case SpvOpIAdd:
4001 case SpvOpFAdd:
4002 case SpvOpISub:
4003 case SpvOpFSub:
4004 case SpvOpIMul:
4005 case SpvOpFMul:
4006 case SpvOpUDiv:
4007 case SpvOpSDiv:
4008 case SpvOpFDiv:
4009 case SpvOpUMod:
4010 case SpvOpSRem:
4011 case SpvOpSMod:
4012 case SpvOpFRem:
4013 case SpvOpFMod:
4014 case SpvOpVectorTimesScalar:
4015 case SpvOpDot:
4016 case SpvOpIAddCarry:
4017 case SpvOpISubBorrow:
4018 case SpvOpUMulExtended:
4019 case SpvOpSMulExtended:
4020 case SpvOpShiftRightLogical:
4021 case SpvOpShiftRightArithmetic:
4022 case SpvOpShiftLeftLogical:
4023 case SpvOpLogicalEqual:
4024 case SpvOpLogicalNotEqual:
4025 case SpvOpLogicalOr:
4026 case SpvOpLogicalAnd:
4027 case SpvOpLogicalNot:
4028 case SpvOpBitwiseOr:
4029 case SpvOpBitwiseXor:
4030 case SpvOpBitwiseAnd:
4031 case SpvOpIEqual:
4032 case SpvOpFOrdEqual:
4033 case SpvOpFUnordEqual:
4034 case SpvOpINotEqual:
4035 case SpvOpFOrdNotEqual:
4036 case SpvOpFUnordNotEqual:
4037 case SpvOpULessThan:
4038 case SpvOpSLessThan:
4039 case SpvOpFOrdLessThan:
4040 case SpvOpFUnordLessThan:
4041 case SpvOpUGreaterThan:
4042 case SpvOpSGreaterThan:
4043 case SpvOpFOrdGreaterThan:
4044 case SpvOpFUnordGreaterThan:
4045 case SpvOpULessThanEqual:
4046 case SpvOpSLessThanEqual:
4047 case SpvOpFOrdLessThanEqual:
4048 case SpvOpFUnordLessThanEqual:
4049 case SpvOpUGreaterThanEqual:
4050 case SpvOpSGreaterThanEqual:
4051 case SpvOpFOrdGreaterThanEqual:
4052 case SpvOpFUnordGreaterThanEqual:
4053 case SpvOpDPdx:
4054 case SpvOpDPdy:
4055 case SpvOpFwidth:
4056 case SpvOpDPdxFine:
4057 case SpvOpDPdyFine:
4058 case SpvOpFwidthFine:
4059 case SpvOpDPdxCoarse:
4060 case SpvOpDPdyCoarse:
4061 case SpvOpFwidthCoarse:
4062 case SpvOpBitFieldInsert:
4063 case SpvOpBitFieldSExtract:
4064 case SpvOpBitFieldUExtract:
4065 case SpvOpBitReverse:
4066 case SpvOpBitCount:
4067 case SpvOpTranspose:
4068 case SpvOpOuterProduct:
4069 case SpvOpMatrixTimesScalar:
4070 case SpvOpVectorTimesMatrix:
4071 case SpvOpMatrixTimesVector:
4072 case SpvOpMatrixTimesMatrix:
4073 vtn_handle_alu(b, opcode, w, count);
4074 break;
4075
4076 case SpvOpVectorExtractDynamic:
4077 case SpvOpVectorInsertDynamic:
4078 case SpvOpVectorShuffle:
4079 case SpvOpCompositeConstruct:
4080 case SpvOpCompositeExtract:
4081 case SpvOpCompositeInsert:
4082 case SpvOpCopyObject:
4083 vtn_handle_composite(b, opcode, w, count);
4084 break;
4085
4086 case SpvOpEmitVertex:
4087 case SpvOpEndPrimitive:
4088 case SpvOpEmitStreamVertex:
4089 case SpvOpEndStreamPrimitive:
4090 case SpvOpControlBarrier:
4091 case SpvOpMemoryBarrier:
4092 vtn_handle_barrier(b, opcode, w, count);
4093 break;
4094
4095 case SpvOpGroupNonUniformElect:
4096 case SpvOpGroupNonUniformAll:
4097 case SpvOpGroupNonUniformAny:
4098 case SpvOpGroupNonUniformAllEqual:
4099 case SpvOpGroupNonUniformBroadcast:
4100 case SpvOpGroupNonUniformBroadcastFirst:
4101 case SpvOpGroupNonUniformBallot:
4102 case SpvOpGroupNonUniformInverseBallot:
4103 case SpvOpGroupNonUniformBallotBitExtract:
4104 case SpvOpGroupNonUniformBallotBitCount:
4105 case SpvOpGroupNonUniformBallotFindLSB:
4106 case SpvOpGroupNonUniformBallotFindMSB:
4107 case SpvOpGroupNonUniformShuffle:
4108 case SpvOpGroupNonUniformShuffleXor:
4109 case SpvOpGroupNonUniformShuffleUp:
4110 case SpvOpGroupNonUniformShuffleDown:
4111 case SpvOpGroupNonUniformIAdd:
4112 case SpvOpGroupNonUniformFAdd:
4113 case SpvOpGroupNonUniformIMul:
4114 case SpvOpGroupNonUniformFMul:
4115 case SpvOpGroupNonUniformSMin:
4116 case SpvOpGroupNonUniformUMin:
4117 case SpvOpGroupNonUniformFMin:
4118 case SpvOpGroupNonUniformSMax:
4119 case SpvOpGroupNonUniformUMax:
4120 case SpvOpGroupNonUniformFMax:
4121 case SpvOpGroupNonUniformBitwiseAnd:
4122 case SpvOpGroupNonUniformBitwiseOr:
4123 case SpvOpGroupNonUniformBitwiseXor:
4124 case SpvOpGroupNonUniformLogicalAnd:
4125 case SpvOpGroupNonUniformLogicalOr:
4126 case SpvOpGroupNonUniformLogicalXor:
4127 case SpvOpGroupNonUniformQuadBroadcast:
4128 case SpvOpGroupNonUniformQuadSwap:
4129 vtn_handle_subgroup(b, opcode, w, count);
4130 break;
4131
4132 default:
4133 vtn_fail("Unhandled opcode");
4134 }
4135
4136 return true;
4137 }
4138
4139 struct vtn_builder*
4140 vtn_create_builder(const uint32_t *words, size_t word_count,
4141 gl_shader_stage stage, const char *entry_point_name,
4142 const struct spirv_to_nir_options *options)
4143 {
4144 /* Initialize the vtn_builder object */
4145 struct vtn_builder *b = rzalloc(NULL, struct vtn_builder);
4146 b->spirv = words;
4147 b->spirv_word_count = word_count;
4148 b->file = NULL;
4149 b->line = -1;
4150 b->col = -1;
4151 exec_list_make_empty(&b->functions);
4152 b->entry_point_stage = stage;
4153 b->entry_point_name = entry_point_name;
4154 b->options = options;
4155
4156 /*
4157 * Handle the SPIR-V header (first 5 dwords).
4158 * Can't use vtx_assert() as the setjmp(3) target isn't initialized yet.
4159 */
4160 if (word_count <= 5)
4161 goto fail;
4162
4163 if (words[0] != SpvMagicNumber) {
4164 vtn_err("words[0] was 0x%x, want 0x%x", words[0], SpvMagicNumber);
4165 goto fail;
4166 }
4167 if (words[1] < 0x10000) {
4168 vtn_err("words[1] was 0x%x, want >= 0x10000", words[1]);
4169 goto fail;
4170 }
4171
4172 /* words[2] == generator magic */
4173 unsigned value_id_bound = words[3];
4174 if (words[4] != 0) {
4175 vtn_err("words[4] was %u, want 0", words[4]);
4176 goto fail;
4177 }
4178
4179 b->value_id_bound = value_id_bound;
4180 b->values = rzalloc_array(b, struct vtn_value, value_id_bound);
4181
4182 return b;
4183 fail:
4184 ralloc_free(b);
4185 return NULL;
4186 }
4187
4188 nir_function *
4189 spirv_to_nir(const uint32_t *words, size_t word_count,
4190 struct nir_spirv_specialization *spec, unsigned num_spec,
4191 gl_shader_stage stage, const char *entry_point_name,
4192 const struct spirv_to_nir_options *options,
4193 const nir_shader_compiler_options *nir_options)
4194
4195 {
4196 const uint32_t *word_end = words + word_count;
4197
4198 struct vtn_builder *b = vtn_create_builder(words, word_count,
4199 stage, entry_point_name,
4200 options);
4201
4202 if (b == NULL)
4203 return NULL;
4204
4205 /* See also _vtn_fail() */
4206 if (setjmp(b->fail_jump)) {
4207 ralloc_free(b);
4208 return NULL;
4209 }
4210
4211 /* Skip the SPIR-V header, handled at vtn_create_builder */
4212 words+= 5;
4213
4214 /* Handle all the preamble instructions */
4215 words = vtn_foreach_instruction(b, words, word_end,
4216 vtn_handle_preamble_instruction);
4217
4218 if (b->entry_point == NULL) {
4219 vtn_fail("Entry point not found");
4220 ralloc_free(b);
4221 return NULL;
4222 }
4223
4224 b->shader = nir_shader_create(b, stage, nir_options, NULL);
4225
4226 /* Set shader info defaults */
4227 b->shader->info.gs.invocations = 1;
4228
4229 /* Parse execution modes */
4230 vtn_foreach_execution_mode(b, b->entry_point,
4231 vtn_handle_execution_mode, NULL);
4232
4233 b->specializations = spec;
4234 b->num_specializations = num_spec;
4235
4236 /* Handle all variable, type, and constant instructions */
4237 words = vtn_foreach_instruction(b, words, word_end,
4238 vtn_handle_variable_or_type_instruction);
4239
4240 /* Set types on all vtn_values */
4241 vtn_foreach_instruction(b, words, word_end, vtn_set_instruction_result_type);
4242
4243 vtn_build_cfg(b, words, word_end);
4244
4245 assert(b->entry_point->value_type == vtn_value_type_function);
4246 b->entry_point->func->referenced = true;
4247
4248 bool progress;
4249 do {
4250 progress = false;
4251 foreach_list_typed(struct vtn_function, func, node, &b->functions) {
4252 if (func->referenced && !func->emitted) {
4253 b->const_table = _mesa_hash_table_create(b, _mesa_hash_pointer,
4254 _mesa_key_pointer_equal);
4255
4256 vtn_function_emit(b, func, vtn_handle_body_instruction);
4257 progress = true;
4258 }
4259 }
4260 } while (progress);
4261
4262 /* We sometimes generate bogus derefs that, while never used, give the
4263 * validator a bit of heartburn. Run dead code to get rid of them.
4264 */
4265 nir_opt_dce(b->shader);
4266
4267 vtn_assert(b->entry_point->value_type == vtn_value_type_function);
4268 nir_function *entry_point = b->entry_point->func->impl->function;
4269 vtn_assert(entry_point);
4270
4271 /* Unparent the shader from the vtn_builder before we delete the builder */
4272 ralloc_steal(NULL, b->shader);
4273
4274 ralloc_free(b);
4275
4276 return entry_point;
4277 }