nir/spirv: Beef up the type system a bit
[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 "spirv_info.h"
33
34 struct spec_constant_value {
35 bool is_double;
36 union {
37 uint32_t data32;
38 uint64_t data64;
39 };
40 };
41
42 void
43 _vtn_warn(const char *file, int line, const char *msg, ...)
44 {
45 char *formatted;
46 va_list args;
47
48 va_start(args, msg);
49 formatted = ralloc_vasprintf(NULL, msg, args);
50 va_end(args);
51
52 fprintf(stderr, "%s:%d WARNING: %s\n", file, line, formatted);
53
54 ralloc_free(formatted);
55 }
56
57 static struct vtn_ssa_value *
58 vtn_undef_ssa_value(struct vtn_builder *b, const struct glsl_type *type)
59 {
60 struct vtn_ssa_value *val = rzalloc(b, struct vtn_ssa_value);
61 val->type = type;
62
63 if (glsl_type_is_vector_or_scalar(type)) {
64 unsigned num_components = glsl_get_vector_elements(val->type);
65 unsigned bit_size = glsl_get_bit_size(val->type);
66 val->def = nir_ssa_undef(&b->nb, num_components, bit_size);
67 } else {
68 unsigned elems = glsl_get_length(val->type);
69 val->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
70 if (glsl_type_is_matrix(type)) {
71 const struct glsl_type *elem_type =
72 glsl_vector_type(glsl_get_base_type(type),
73 glsl_get_vector_elements(type));
74
75 for (unsigned i = 0; i < elems; i++)
76 val->elems[i] = vtn_undef_ssa_value(b, elem_type);
77 } else if (glsl_type_is_array(type)) {
78 const struct glsl_type *elem_type = glsl_get_array_element(type);
79 for (unsigned i = 0; i < elems; i++)
80 val->elems[i] = vtn_undef_ssa_value(b, elem_type);
81 } else {
82 for (unsigned i = 0; i < elems; i++) {
83 const struct glsl_type *elem_type = glsl_get_struct_field(type, i);
84 val->elems[i] = vtn_undef_ssa_value(b, elem_type);
85 }
86 }
87 }
88
89 return val;
90 }
91
92 static struct vtn_ssa_value *
93 vtn_const_ssa_value(struct vtn_builder *b, nir_constant *constant,
94 const struct glsl_type *type)
95 {
96 struct hash_entry *entry = _mesa_hash_table_search(b->const_table, constant);
97
98 if (entry)
99 return entry->data;
100
101 struct vtn_ssa_value *val = rzalloc(b, struct vtn_ssa_value);
102 val->type = type;
103
104 switch (glsl_get_base_type(type)) {
105 case GLSL_TYPE_INT:
106 case GLSL_TYPE_UINT:
107 case GLSL_TYPE_INT64:
108 case GLSL_TYPE_UINT64:
109 case GLSL_TYPE_BOOL:
110 case GLSL_TYPE_FLOAT:
111 case GLSL_TYPE_DOUBLE: {
112 int bit_size = glsl_get_bit_size(type);
113 if (glsl_type_is_vector_or_scalar(type)) {
114 unsigned num_components = glsl_get_vector_elements(val->type);
115 nir_load_const_instr *load =
116 nir_load_const_instr_create(b->shader, num_components, bit_size);
117
118 load->value = constant->values[0];
119
120 nir_instr_insert_before_cf_list(&b->impl->body, &load->instr);
121 val->def = &load->def;
122 } else {
123 assert(glsl_type_is_matrix(type));
124 unsigned rows = glsl_get_vector_elements(val->type);
125 unsigned columns = glsl_get_matrix_columns(val->type);
126 val->elems = ralloc_array(b, struct vtn_ssa_value *, columns);
127
128 for (unsigned i = 0; i < columns; i++) {
129 struct vtn_ssa_value *col_val = rzalloc(b, struct vtn_ssa_value);
130 col_val->type = glsl_get_column_type(val->type);
131 nir_load_const_instr *load =
132 nir_load_const_instr_create(b->shader, rows, bit_size);
133
134 load->value = constant->values[i];
135
136 nir_instr_insert_before_cf_list(&b->impl->body, &load->instr);
137 col_val->def = &load->def;
138
139 val->elems[i] = col_val;
140 }
141 }
142 break;
143 }
144
145 case GLSL_TYPE_ARRAY: {
146 unsigned elems = glsl_get_length(val->type);
147 val->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
148 const struct glsl_type *elem_type = glsl_get_array_element(val->type);
149 for (unsigned i = 0; i < elems; i++)
150 val->elems[i] = vtn_const_ssa_value(b, constant->elements[i],
151 elem_type);
152 break;
153 }
154
155 case GLSL_TYPE_STRUCT: {
156 unsigned elems = glsl_get_length(val->type);
157 val->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
158 for (unsigned i = 0; i < elems; i++) {
159 const struct glsl_type *elem_type =
160 glsl_get_struct_field(val->type, i);
161 val->elems[i] = vtn_const_ssa_value(b, constant->elements[i],
162 elem_type);
163 }
164 break;
165 }
166
167 default:
168 unreachable("bad constant type");
169 }
170
171 return val;
172 }
173
174 struct vtn_ssa_value *
175 vtn_ssa_value(struct vtn_builder *b, uint32_t value_id)
176 {
177 struct vtn_value *val = vtn_untyped_value(b, value_id);
178 switch (val->value_type) {
179 case vtn_value_type_undef:
180 return vtn_undef_ssa_value(b, val->type->type);
181
182 case vtn_value_type_constant:
183 return vtn_const_ssa_value(b, val->constant, val->const_type);
184
185 case vtn_value_type_ssa:
186 return val->ssa;
187
188 case vtn_value_type_pointer:
189 /* This is needed for function parameters */
190 return vtn_variable_load(b, val->pointer);
191
192 default:
193 unreachable("Invalid type for an SSA value");
194 }
195 }
196
197 static char *
198 vtn_string_literal(struct vtn_builder *b, const uint32_t *words,
199 unsigned word_count, unsigned *words_used)
200 {
201 char *dup = ralloc_strndup(b, (char *)words, word_count * sizeof(*words));
202 if (words_used) {
203 /* Ammount of space taken by the string (including the null) */
204 unsigned len = strlen(dup) + 1;
205 *words_used = DIV_ROUND_UP(len, sizeof(*words));
206 }
207 return dup;
208 }
209
210 const uint32_t *
211 vtn_foreach_instruction(struct vtn_builder *b, const uint32_t *start,
212 const uint32_t *end, vtn_instruction_handler handler)
213 {
214 b->file = NULL;
215 b->line = -1;
216 b->col = -1;
217
218 const uint32_t *w = start;
219 while (w < end) {
220 SpvOp opcode = w[0] & SpvOpCodeMask;
221 unsigned count = w[0] >> SpvWordCountShift;
222 assert(count >= 1 && w + count <= end);
223
224 switch (opcode) {
225 case SpvOpNop:
226 break; /* Do nothing */
227
228 case SpvOpLine:
229 b->file = vtn_value(b, w[1], vtn_value_type_string)->str;
230 b->line = w[2];
231 b->col = w[3];
232 break;
233
234 case SpvOpNoLine:
235 b->file = NULL;
236 b->line = -1;
237 b->col = -1;
238 break;
239
240 default:
241 if (!handler(b, opcode, w, count))
242 return w;
243 break;
244 }
245
246 w += count;
247 }
248 assert(w == end);
249 return w;
250 }
251
252 static void
253 vtn_handle_extension(struct vtn_builder *b, SpvOp opcode,
254 const uint32_t *w, unsigned count)
255 {
256 switch (opcode) {
257 case SpvOpExtInstImport: {
258 struct vtn_value *val = vtn_push_value(b, w[1], vtn_value_type_extension);
259 if (strcmp((const char *)&w[2], "GLSL.std.450") == 0) {
260 val->ext_handler = vtn_handle_glsl450_instruction;
261 } else {
262 assert(!"Unsupported extension");
263 }
264 break;
265 }
266
267 case SpvOpExtInst: {
268 struct vtn_value *val = vtn_value(b, w[3], vtn_value_type_extension);
269 bool handled = val->ext_handler(b, w[4], w, count);
270 (void)handled;
271 assert(handled);
272 break;
273 }
274
275 default:
276 unreachable("Unhandled opcode");
277 }
278 }
279
280 static void
281 _foreach_decoration_helper(struct vtn_builder *b,
282 struct vtn_value *base_value,
283 int parent_member,
284 struct vtn_value *value,
285 vtn_decoration_foreach_cb cb, void *data)
286 {
287 for (struct vtn_decoration *dec = value->decoration; dec; dec = dec->next) {
288 int member;
289 if (dec->scope == VTN_DEC_DECORATION) {
290 member = parent_member;
291 } else if (dec->scope >= VTN_DEC_STRUCT_MEMBER0) {
292 assert(parent_member == -1);
293 member = dec->scope - VTN_DEC_STRUCT_MEMBER0;
294 } else {
295 /* Not a decoration */
296 continue;
297 }
298
299 if (dec->group) {
300 assert(dec->group->value_type == vtn_value_type_decoration_group);
301 _foreach_decoration_helper(b, base_value, member, dec->group,
302 cb, data);
303 } else {
304 cb(b, base_value, member, dec, data);
305 }
306 }
307 }
308
309 /** Iterates (recursively if needed) over all of the decorations on a value
310 *
311 * This function iterates over all of the decorations applied to a given
312 * value. If it encounters a decoration group, it recurses into the group
313 * and iterates over all of those decorations as well.
314 */
315 void
316 vtn_foreach_decoration(struct vtn_builder *b, struct vtn_value *value,
317 vtn_decoration_foreach_cb cb, void *data)
318 {
319 _foreach_decoration_helper(b, value, -1, value, cb, data);
320 }
321
322 void
323 vtn_foreach_execution_mode(struct vtn_builder *b, struct vtn_value *value,
324 vtn_execution_mode_foreach_cb cb, void *data)
325 {
326 for (struct vtn_decoration *dec = value->decoration; dec; dec = dec->next) {
327 if (dec->scope != VTN_DEC_EXECUTION_MODE)
328 continue;
329
330 assert(dec->group == NULL);
331 cb(b, value, dec, data);
332 }
333 }
334
335 static void
336 vtn_handle_decoration(struct vtn_builder *b, SpvOp opcode,
337 const uint32_t *w, unsigned count)
338 {
339 const uint32_t *w_end = w + count;
340 const uint32_t target = w[1];
341 w += 2;
342
343 switch (opcode) {
344 case SpvOpDecorationGroup:
345 vtn_push_value(b, target, vtn_value_type_decoration_group);
346 break;
347
348 case SpvOpDecorate:
349 case SpvOpMemberDecorate:
350 case SpvOpExecutionMode: {
351 struct vtn_value *val = &b->values[target];
352
353 struct vtn_decoration *dec = rzalloc(b, struct vtn_decoration);
354 switch (opcode) {
355 case SpvOpDecorate:
356 dec->scope = VTN_DEC_DECORATION;
357 break;
358 case SpvOpMemberDecorate:
359 dec->scope = VTN_DEC_STRUCT_MEMBER0 + *(w++);
360 break;
361 case SpvOpExecutionMode:
362 dec->scope = VTN_DEC_EXECUTION_MODE;
363 break;
364 default:
365 unreachable("Invalid decoration opcode");
366 }
367 dec->decoration = *(w++);
368 dec->literals = w;
369
370 /* Link into the list */
371 dec->next = val->decoration;
372 val->decoration = dec;
373 break;
374 }
375
376 case SpvOpGroupMemberDecorate:
377 case SpvOpGroupDecorate: {
378 struct vtn_value *group =
379 vtn_value(b, target, vtn_value_type_decoration_group);
380
381 for (; w < w_end; w++) {
382 struct vtn_value *val = vtn_untyped_value(b, *w);
383 struct vtn_decoration *dec = rzalloc(b, struct vtn_decoration);
384
385 dec->group = group;
386 if (opcode == SpvOpGroupDecorate) {
387 dec->scope = VTN_DEC_DECORATION;
388 } else {
389 dec->scope = VTN_DEC_STRUCT_MEMBER0 + *(++w);
390 }
391
392 /* Link into the list */
393 dec->next = val->decoration;
394 val->decoration = dec;
395 }
396 break;
397 }
398
399 default:
400 unreachable("Unhandled opcode");
401 }
402 }
403
404 struct member_decoration_ctx {
405 unsigned num_fields;
406 struct glsl_struct_field *fields;
407 struct vtn_type *type;
408 };
409
410 /* does a shallow copy of a vtn_type */
411
412 static struct vtn_type *
413 vtn_type_copy(struct vtn_builder *b, struct vtn_type *src)
414 {
415 struct vtn_type *dest = ralloc(b, struct vtn_type);
416 *dest = *src;
417
418 switch (src->base_type) {
419 case vtn_base_type_void:
420 case vtn_base_type_scalar:
421 case vtn_base_type_vector:
422 case vtn_base_type_matrix:
423 case vtn_base_type_array:
424 case vtn_base_type_image:
425 case vtn_base_type_sampler:
426 case vtn_base_type_function:
427 /* Nothing more to do */
428 break;
429
430 case vtn_base_type_struct:
431 dest->members = ralloc_array(b, struct vtn_type *, src->length);
432 memcpy(dest->members, src->members,
433 src->length * sizeof(src->members[0]));
434
435 dest->offsets = ralloc_array(b, unsigned, src->length);
436 memcpy(dest->offsets, src->offsets,
437 src->length * sizeof(src->offsets[0]));
438 break;
439 }
440
441 return dest;
442 }
443
444 static struct vtn_type *
445 mutable_matrix_member(struct vtn_builder *b, struct vtn_type *type, int member)
446 {
447 type->members[member] = vtn_type_copy(b, type->members[member]);
448 type = type->members[member];
449
450 /* We may have an array of matrices.... Oh, joy! */
451 while (glsl_type_is_array(type->type)) {
452 type->array_element = vtn_type_copy(b, type->array_element);
453 type = type->array_element;
454 }
455
456 assert(glsl_type_is_matrix(type->type));
457
458 return type;
459 }
460
461 static void
462 struct_member_decoration_cb(struct vtn_builder *b,
463 struct vtn_value *val, int member,
464 const struct vtn_decoration *dec, void *void_ctx)
465 {
466 struct member_decoration_ctx *ctx = void_ctx;
467
468 if (member < 0)
469 return;
470
471 assert(member < ctx->num_fields);
472
473 switch (dec->decoration) {
474 case SpvDecorationNonWritable:
475 case SpvDecorationNonReadable:
476 case SpvDecorationRelaxedPrecision:
477 case SpvDecorationVolatile:
478 case SpvDecorationCoherent:
479 case SpvDecorationUniform:
480 break; /* FIXME: Do nothing with this for now. */
481 case SpvDecorationNoPerspective:
482 ctx->fields[member].interpolation = INTERP_MODE_NOPERSPECTIVE;
483 break;
484 case SpvDecorationFlat:
485 ctx->fields[member].interpolation = INTERP_MODE_FLAT;
486 break;
487 case SpvDecorationCentroid:
488 ctx->fields[member].centroid = true;
489 break;
490 case SpvDecorationSample:
491 ctx->fields[member].sample = true;
492 break;
493 case SpvDecorationStream:
494 /* Vulkan only allows one GS stream */
495 assert(dec->literals[0] == 0);
496 break;
497 case SpvDecorationLocation:
498 ctx->fields[member].location = dec->literals[0];
499 break;
500 case SpvDecorationComponent:
501 break; /* FIXME: What should we do with these? */
502 case SpvDecorationBuiltIn:
503 ctx->type->members[member] = vtn_type_copy(b, ctx->type->members[member]);
504 ctx->type->members[member]->is_builtin = true;
505 ctx->type->members[member]->builtin = dec->literals[0];
506 ctx->type->builtin_block = true;
507 break;
508 case SpvDecorationOffset:
509 ctx->type->offsets[member] = dec->literals[0];
510 break;
511 case SpvDecorationMatrixStride:
512 /* Handled as a second pass */
513 break;
514 case SpvDecorationColMajor:
515 break; /* Nothing to do here. Column-major is the default. */
516 case SpvDecorationRowMajor:
517 mutable_matrix_member(b, ctx->type, member)->row_major = true;
518 break;
519
520 case SpvDecorationPatch:
521 break;
522
523 case SpvDecorationSpecId:
524 case SpvDecorationBlock:
525 case SpvDecorationBufferBlock:
526 case SpvDecorationArrayStride:
527 case SpvDecorationGLSLShared:
528 case SpvDecorationGLSLPacked:
529 case SpvDecorationInvariant:
530 case SpvDecorationRestrict:
531 case SpvDecorationAliased:
532 case SpvDecorationConstant:
533 case SpvDecorationIndex:
534 case SpvDecorationBinding:
535 case SpvDecorationDescriptorSet:
536 case SpvDecorationLinkageAttributes:
537 case SpvDecorationNoContraction:
538 case SpvDecorationInputAttachmentIndex:
539 vtn_warn("Decoration not allowed on struct members: %s",
540 spirv_decoration_to_string(dec->decoration));
541 break;
542
543 case SpvDecorationXfbBuffer:
544 case SpvDecorationXfbStride:
545 vtn_warn("Vulkan does not have transform feedback");
546 break;
547
548 case SpvDecorationCPacked:
549 case SpvDecorationSaturatedConversion:
550 case SpvDecorationFuncParamAttr:
551 case SpvDecorationFPRoundingMode:
552 case SpvDecorationFPFastMathMode:
553 case SpvDecorationAlignment:
554 vtn_warn("Decoration only allowed for CL-style kernels: %s",
555 spirv_decoration_to_string(dec->decoration));
556 break;
557
558 default:
559 unreachable("Unhandled decoration");
560 }
561 }
562
563 /* Matrix strides are handled as a separate pass because we need to know
564 * whether the matrix is row-major or not first.
565 */
566 static void
567 struct_member_matrix_stride_cb(struct vtn_builder *b,
568 struct vtn_value *val, int member,
569 const struct vtn_decoration *dec,
570 void *void_ctx)
571 {
572 if (dec->decoration != SpvDecorationMatrixStride)
573 return;
574 assert(member >= 0);
575
576 struct member_decoration_ctx *ctx = void_ctx;
577
578 struct vtn_type *mat_type = mutable_matrix_member(b, ctx->type, member);
579 if (mat_type->row_major) {
580 mat_type->array_element = vtn_type_copy(b, mat_type->array_element);
581 mat_type->stride = mat_type->array_element->stride;
582 mat_type->array_element->stride = dec->literals[0];
583 } else {
584 assert(mat_type->array_element->stride > 0);
585 mat_type->stride = dec->literals[0];
586 }
587 }
588
589 static void
590 type_decoration_cb(struct vtn_builder *b,
591 struct vtn_value *val, int member,
592 const struct vtn_decoration *dec, void *ctx)
593 {
594 struct vtn_type *type = val->type;
595
596 if (member != -1)
597 return;
598
599 switch (dec->decoration) {
600 case SpvDecorationArrayStride:
601 type->stride = dec->literals[0];
602 break;
603 case SpvDecorationBlock:
604 type->block = true;
605 break;
606 case SpvDecorationBufferBlock:
607 type->buffer_block = true;
608 break;
609 case SpvDecorationGLSLShared:
610 case SpvDecorationGLSLPacked:
611 /* Ignore these, since we get explicit offsets anyways */
612 break;
613
614 case SpvDecorationRowMajor:
615 case SpvDecorationColMajor:
616 case SpvDecorationMatrixStride:
617 case SpvDecorationBuiltIn:
618 case SpvDecorationNoPerspective:
619 case SpvDecorationFlat:
620 case SpvDecorationPatch:
621 case SpvDecorationCentroid:
622 case SpvDecorationSample:
623 case SpvDecorationVolatile:
624 case SpvDecorationCoherent:
625 case SpvDecorationNonWritable:
626 case SpvDecorationNonReadable:
627 case SpvDecorationUniform:
628 case SpvDecorationStream:
629 case SpvDecorationLocation:
630 case SpvDecorationComponent:
631 case SpvDecorationOffset:
632 case SpvDecorationXfbBuffer:
633 case SpvDecorationXfbStride:
634 vtn_warn("Decoration only allowed for struct members: %s",
635 spirv_decoration_to_string(dec->decoration));
636 break;
637
638 case SpvDecorationRelaxedPrecision:
639 case SpvDecorationSpecId:
640 case SpvDecorationInvariant:
641 case SpvDecorationRestrict:
642 case SpvDecorationAliased:
643 case SpvDecorationConstant:
644 case SpvDecorationIndex:
645 case SpvDecorationBinding:
646 case SpvDecorationDescriptorSet:
647 case SpvDecorationLinkageAttributes:
648 case SpvDecorationNoContraction:
649 case SpvDecorationInputAttachmentIndex:
650 vtn_warn("Decoration not allowed on types: %s",
651 spirv_decoration_to_string(dec->decoration));
652 break;
653
654 case SpvDecorationCPacked:
655 case SpvDecorationSaturatedConversion:
656 case SpvDecorationFuncParamAttr:
657 case SpvDecorationFPRoundingMode:
658 case SpvDecorationFPFastMathMode:
659 case SpvDecorationAlignment:
660 vtn_warn("Decoration only allowed for CL-style kernels: %s",
661 spirv_decoration_to_string(dec->decoration));
662 break;
663
664 default:
665 unreachable("Unhandled decoration");
666 }
667 }
668
669 static unsigned
670 translate_image_format(SpvImageFormat format)
671 {
672 switch (format) {
673 case SpvImageFormatUnknown: return 0; /* GL_NONE */
674 case SpvImageFormatRgba32f: return 0x8814; /* GL_RGBA32F */
675 case SpvImageFormatRgba16f: return 0x881A; /* GL_RGBA16F */
676 case SpvImageFormatR32f: return 0x822E; /* GL_R32F */
677 case SpvImageFormatRgba8: return 0x8058; /* GL_RGBA8 */
678 case SpvImageFormatRgba8Snorm: return 0x8F97; /* GL_RGBA8_SNORM */
679 case SpvImageFormatRg32f: return 0x8230; /* GL_RG32F */
680 case SpvImageFormatRg16f: return 0x822F; /* GL_RG16F */
681 case SpvImageFormatR11fG11fB10f: return 0x8C3A; /* GL_R11F_G11F_B10F */
682 case SpvImageFormatR16f: return 0x822D; /* GL_R16F */
683 case SpvImageFormatRgba16: return 0x805B; /* GL_RGBA16 */
684 case SpvImageFormatRgb10A2: return 0x8059; /* GL_RGB10_A2 */
685 case SpvImageFormatRg16: return 0x822C; /* GL_RG16 */
686 case SpvImageFormatRg8: return 0x822B; /* GL_RG8 */
687 case SpvImageFormatR16: return 0x822A; /* GL_R16 */
688 case SpvImageFormatR8: return 0x8229; /* GL_R8 */
689 case SpvImageFormatRgba16Snorm: return 0x8F9B; /* GL_RGBA16_SNORM */
690 case SpvImageFormatRg16Snorm: return 0x8F99; /* GL_RG16_SNORM */
691 case SpvImageFormatRg8Snorm: return 0x8F95; /* GL_RG8_SNORM */
692 case SpvImageFormatR16Snorm: return 0x8F98; /* GL_R16_SNORM */
693 case SpvImageFormatR8Snorm: return 0x8F94; /* GL_R8_SNORM */
694 case SpvImageFormatRgba32i: return 0x8D82; /* GL_RGBA32I */
695 case SpvImageFormatRgba16i: return 0x8D88; /* GL_RGBA16I */
696 case SpvImageFormatRgba8i: return 0x8D8E; /* GL_RGBA8I */
697 case SpvImageFormatR32i: return 0x8235; /* GL_R32I */
698 case SpvImageFormatRg32i: return 0x823B; /* GL_RG32I */
699 case SpvImageFormatRg16i: return 0x8239; /* GL_RG16I */
700 case SpvImageFormatRg8i: return 0x8237; /* GL_RG8I */
701 case SpvImageFormatR16i: return 0x8233; /* GL_R16I */
702 case SpvImageFormatR8i: return 0x8231; /* GL_R8I */
703 case SpvImageFormatRgba32ui: return 0x8D70; /* GL_RGBA32UI */
704 case SpvImageFormatRgba16ui: return 0x8D76; /* GL_RGBA16UI */
705 case SpvImageFormatRgba8ui: return 0x8D7C; /* GL_RGBA8UI */
706 case SpvImageFormatR32ui: return 0x8236; /* GL_R32UI */
707 case SpvImageFormatRgb10a2ui: return 0x906F; /* GL_RGB10_A2UI */
708 case SpvImageFormatRg32ui: return 0x823C; /* GL_RG32UI */
709 case SpvImageFormatRg16ui: return 0x823A; /* GL_RG16UI */
710 case SpvImageFormatRg8ui: return 0x8238; /* GL_RG8UI */
711 case SpvImageFormatR16ui: return 0x823A; /* GL_RG16UI */
712 case SpvImageFormatR8ui: return 0x8232; /* GL_R8UI */
713 default:
714 assert(!"Invalid image format");
715 return 0;
716 }
717 }
718
719 static void
720 vtn_handle_type(struct vtn_builder *b, SpvOp opcode,
721 const uint32_t *w, unsigned count)
722 {
723 struct vtn_value *val = vtn_push_value(b, w[1], vtn_value_type_type);
724
725 val->type = rzalloc(b, struct vtn_type);
726 val->type->val = val;
727
728 switch (opcode) {
729 case SpvOpTypeVoid:
730 val->type->base_type = vtn_base_type_void;
731 val->type->type = glsl_void_type();
732 break;
733 case SpvOpTypeBool:
734 val->type->base_type = vtn_base_type_scalar;
735 val->type->type = glsl_bool_type();
736 break;
737 case SpvOpTypeInt: {
738 int bit_size = w[2];
739 const bool signedness = w[3];
740 val->type->base_type = vtn_base_type_scalar;
741 if (bit_size == 64)
742 val->type->type = (signedness ? glsl_int64_t_type() : glsl_uint64_t_type());
743 else
744 val->type->type = (signedness ? glsl_int_type() : glsl_uint_type());
745 break;
746 }
747 case SpvOpTypeFloat: {
748 int bit_size = w[2];
749 val->type->base_type = vtn_base_type_scalar;
750 val->type->type = bit_size == 64 ? glsl_double_type() : glsl_float_type();
751 break;
752 }
753
754 case SpvOpTypeVector: {
755 struct vtn_type *base = vtn_value(b, w[2], vtn_value_type_type)->type;
756 unsigned elems = w[3];
757
758 assert(glsl_type_is_scalar(base->type));
759 val->type->base_type = vtn_base_type_vector;
760 val->type->type = glsl_vector_type(glsl_get_base_type(base->type), elems);
761
762 /* Vectors implicitly have sizeof(base_type) stride. For now, this
763 * is always 4 bytes. This will have to change if we want to start
764 * supporting doubles or half-floats.
765 */
766 val->type->stride = glsl_get_bit_size(base->type) / 8;
767 val->type->array_element = base;
768 break;
769 }
770
771 case SpvOpTypeMatrix: {
772 struct vtn_type *base = vtn_value(b, w[2], vtn_value_type_type)->type;
773 unsigned columns = w[3];
774
775 assert(glsl_type_is_vector(base->type));
776 val->type->base_type = vtn_base_type_matrix;
777 val->type->type = glsl_matrix_type(glsl_get_base_type(base->type),
778 glsl_get_vector_elements(base->type),
779 columns);
780 assert(!glsl_type_is_error(val->type->type));
781 val->type->length = columns;
782 val->type->array_element = base;
783 val->type->row_major = false;
784 val->type->stride = 0;
785 break;
786 }
787
788 case SpvOpTypeRuntimeArray:
789 case SpvOpTypeArray: {
790 struct vtn_type *array_element =
791 vtn_value(b, w[2], vtn_value_type_type)->type;
792
793 if (opcode == SpvOpTypeRuntimeArray) {
794 /* A length of 0 is used to denote unsized arrays */
795 val->type->length = 0;
796 } else {
797 val->type->length =
798 vtn_value(b, w[3], vtn_value_type_constant)->constant->values[0].u32[0];
799 }
800
801 val->type->base_type = vtn_base_type_array;
802 val->type->type = glsl_array_type(array_element->type, val->type->length);
803 val->type->array_element = array_element;
804 val->type->stride = 0;
805 break;
806 }
807
808 case SpvOpTypeStruct: {
809 unsigned num_fields = count - 2;
810 val->type->base_type = vtn_base_type_struct;
811 val->type->length = num_fields;
812 val->type->members = ralloc_array(b, struct vtn_type *, num_fields);
813 val->type->offsets = ralloc_array(b, unsigned, num_fields);
814
815 NIR_VLA(struct glsl_struct_field, fields, count);
816 for (unsigned i = 0; i < num_fields; i++) {
817 val->type->members[i] =
818 vtn_value(b, w[i + 2], vtn_value_type_type)->type;
819 fields[i] = (struct glsl_struct_field) {
820 .type = val->type->members[i]->type,
821 .name = ralloc_asprintf(b, "field%d", i),
822 .location = -1,
823 };
824 }
825
826 struct member_decoration_ctx ctx = {
827 .num_fields = num_fields,
828 .fields = fields,
829 .type = val->type
830 };
831
832 vtn_foreach_decoration(b, val, struct_member_decoration_cb, &ctx);
833 vtn_foreach_decoration(b, val, struct_member_matrix_stride_cb, &ctx);
834
835 const char *name = val->name ? val->name : "struct";
836
837 val->type->type = glsl_struct_type(fields, num_fields, name);
838 break;
839 }
840
841 case SpvOpTypeFunction: {
842 val->type->base_type = vtn_base_type_function;
843
844 const struct glsl_type *return_type =
845 vtn_value(b, w[2], vtn_value_type_type)->type->type;
846 NIR_VLA(struct glsl_function_param, params, count - 3);
847 for (unsigned i = 0; i < count - 3; i++) {
848 params[i].type = vtn_value(b, w[i + 3], vtn_value_type_type)->type->type;
849
850 /* FIXME: */
851 params[i].in = true;
852 params[i].out = true;
853 }
854 val->type->type = glsl_function_type(return_type, params, count - 3);
855 break;
856 }
857
858 case SpvOpTypePointer:
859 /* FIXME: For now, we'll just do the really lame thing and return
860 * the same type. The validator should ensure that the proper number
861 * of dereferences happen
862 */
863 val->type = vtn_value(b, w[3], vtn_value_type_type)->type;
864 break;
865
866 case SpvOpTypeImage: {
867 val->type->base_type = vtn_base_type_image;
868
869 const struct glsl_type *sampled_type =
870 vtn_value(b, w[2], vtn_value_type_type)->type->type;
871
872 assert(glsl_type_is_vector_or_scalar(sampled_type));
873
874 enum glsl_sampler_dim dim;
875 switch ((SpvDim)w[3]) {
876 case SpvDim1D: dim = GLSL_SAMPLER_DIM_1D; break;
877 case SpvDim2D: dim = GLSL_SAMPLER_DIM_2D; break;
878 case SpvDim3D: dim = GLSL_SAMPLER_DIM_3D; break;
879 case SpvDimCube: dim = GLSL_SAMPLER_DIM_CUBE; break;
880 case SpvDimRect: dim = GLSL_SAMPLER_DIM_RECT; break;
881 case SpvDimBuffer: dim = GLSL_SAMPLER_DIM_BUF; break;
882 case SpvDimSubpassData: dim = GLSL_SAMPLER_DIM_SUBPASS; break;
883 default:
884 unreachable("Invalid SPIR-V Sampler dimension");
885 }
886
887 bool is_shadow = w[4];
888 bool is_array = w[5];
889 bool multisampled = w[6];
890 unsigned sampled = w[7];
891 SpvImageFormat format = w[8];
892
893 if (count > 9)
894 val->type->access_qualifier = w[9];
895 else
896 val->type->access_qualifier = SpvAccessQualifierReadWrite;
897
898 if (multisampled) {
899 if (dim == GLSL_SAMPLER_DIM_2D)
900 dim = GLSL_SAMPLER_DIM_MS;
901 else if (dim == GLSL_SAMPLER_DIM_SUBPASS)
902 dim = GLSL_SAMPLER_DIM_SUBPASS_MS;
903 else
904 assert(!"Unsupported multisampled image type");
905 }
906
907 val->type->image_format = translate_image_format(format);
908
909 if (sampled == 1) {
910 val->type->sampled = true;
911 val->type->type = glsl_sampler_type(dim, is_shadow, is_array,
912 glsl_get_base_type(sampled_type));
913 } else if (sampled == 2) {
914 assert(!is_shadow);
915 val->type->sampled = false;
916 val->type->type = glsl_image_type(dim, is_array,
917 glsl_get_base_type(sampled_type));
918 } else {
919 assert(!"We need to know if the image will be sampled");
920 }
921 break;
922 }
923
924 case SpvOpTypeSampledImage:
925 val->type = vtn_value(b, w[2], vtn_value_type_type)->type;
926 break;
927
928 case SpvOpTypeSampler:
929 /* The actual sampler type here doesn't really matter. It gets
930 * thrown away the moment you combine it with an image. What really
931 * matters is that it's a sampler type as opposed to an integer type
932 * so the backend knows what to do.
933 */
934 val->type->base_type = vtn_base_type_sampler;
935 val->type->type = glsl_bare_sampler_type();
936 break;
937
938 case SpvOpTypeOpaque:
939 case SpvOpTypeEvent:
940 case SpvOpTypeDeviceEvent:
941 case SpvOpTypeReserveId:
942 case SpvOpTypeQueue:
943 case SpvOpTypePipe:
944 default:
945 unreachable("Unhandled opcode");
946 }
947
948 vtn_foreach_decoration(b, val, type_decoration_cb, NULL);
949 }
950
951 static nir_constant *
952 vtn_null_constant(struct vtn_builder *b, const struct glsl_type *type)
953 {
954 nir_constant *c = rzalloc(b, nir_constant);
955
956 switch (glsl_get_base_type(type)) {
957 case GLSL_TYPE_INT:
958 case GLSL_TYPE_UINT:
959 case GLSL_TYPE_INT64:
960 case GLSL_TYPE_UINT64:
961 case GLSL_TYPE_BOOL:
962 case GLSL_TYPE_FLOAT:
963 case GLSL_TYPE_DOUBLE:
964 /* Nothing to do here. It's already initialized to zero */
965 break;
966
967 case GLSL_TYPE_ARRAY:
968 assert(glsl_get_length(type) > 0);
969 c->num_elements = glsl_get_length(type);
970 c->elements = ralloc_array(b, nir_constant *, c->num_elements);
971
972 c->elements[0] = vtn_null_constant(b, glsl_get_array_element(type));
973 for (unsigned i = 1; i < c->num_elements; i++)
974 c->elements[i] = c->elements[0];
975 break;
976
977 case GLSL_TYPE_STRUCT:
978 c->num_elements = glsl_get_length(type);
979 c->elements = ralloc_array(b, nir_constant *, c->num_elements);
980
981 for (unsigned i = 0; i < c->num_elements; i++) {
982 c->elements[i] = vtn_null_constant(b, glsl_get_struct_field(type, i));
983 }
984 break;
985
986 default:
987 unreachable("Invalid type for null constant");
988 }
989
990 return c;
991 }
992
993 static void
994 spec_constant_decoration_cb(struct vtn_builder *b, struct vtn_value *v,
995 int member, const struct vtn_decoration *dec,
996 void *data)
997 {
998 assert(member == -1);
999 if (dec->decoration != SpvDecorationSpecId)
1000 return;
1001
1002 struct spec_constant_value *const_value = data;
1003
1004 for (unsigned i = 0; i < b->num_specializations; i++) {
1005 if (b->specializations[i].id == dec->literals[0]) {
1006 if (const_value->is_double)
1007 const_value->data64 = b->specializations[i].data64;
1008 else
1009 const_value->data32 = b->specializations[i].data32;
1010 return;
1011 }
1012 }
1013 }
1014
1015 static uint32_t
1016 get_specialization(struct vtn_builder *b, struct vtn_value *val,
1017 uint32_t const_value)
1018 {
1019 struct spec_constant_value data;
1020 data.is_double = false;
1021 data.data32 = const_value;
1022 vtn_foreach_decoration(b, val, spec_constant_decoration_cb, &data);
1023 return data.data32;
1024 }
1025
1026 static uint64_t
1027 get_specialization64(struct vtn_builder *b, struct vtn_value *val,
1028 uint64_t const_value)
1029 {
1030 struct spec_constant_value data;
1031 data.is_double = true;
1032 data.data64 = const_value;
1033 vtn_foreach_decoration(b, val, spec_constant_decoration_cb, &data);
1034 return data.data64;
1035 }
1036
1037 static void
1038 handle_workgroup_size_decoration_cb(struct vtn_builder *b,
1039 struct vtn_value *val,
1040 int member,
1041 const struct vtn_decoration *dec,
1042 void *data)
1043 {
1044 assert(member == -1);
1045 if (dec->decoration != SpvDecorationBuiltIn ||
1046 dec->literals[0] != SpvBuiltInWorkgroupSize)
1047 return;
1048
1049 assert(val->const_type == glsl_vector_type(GLSL_TYPE_UINT, 3));
1050
1051 b->shader->info.cs.local_size[0] = val->constant->values[0].u32[0];
1052 b->shader->info.cs.local_size[1] = val->constant->values[0].u32[1];
1053 b->shader->info.cs.local_size[2] = val->constant->values[0].u32[2];
1054 }
1055
1056 static void
1057 vtn_handle_constant(struct vtn_builder *b, SpvOp opcode,
1058 const uint32_t *w, unsigned count)
1059 {
1060 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_constant);
1061 val->const_type = vtn_value(b, w[1], vtn_value_type_type)->type->type;
1062 val->constant = rzalloc(b, nir_constant);
1063 switch (opcode) {
1064 case SpvOpConstantTrue:
1065 assert(val->const_type == glsl_bool_type());
1066 val->constant->values[0].u32[0] = NIR_TRUE;
1067 break;
1068 case SpvOpConstantFalse:
1069 assert(val->const_type == glsl_bool_type());
1070 val->constant->values[0].u32[0] = NIR_FALSE;
1071 break;
1072
1073 case SpvOpSpecConstantTrue:
1074 case SpvOpSpecConstantFalse: {
1075 assert(val->const_type == glsl_bool_type());
1076 uint32_t int_val =
1077 get_specialization(b, val, (opcode == SpvOpSpecConstantTrue));
1078 val->constant->values[0].u32[0] = int_val ? NIR_TRUE : NIR_FALSE;
1079 break;
1080 }
1081
1082 case SpvOpConstant: {
1083 assert(glsl_type_is_scalar(val->const_type));
1084 int bit_size = glsl_get_bit_size(val->const_type);
1085 if (bit_size == 64) {
1086 val->constant->values->u32[0] = w[3];
1087 val->constant->values->u32[1] = w[4];
1088 } else {
1089 assert(bit_size == 32);
1090 val->constant->values->u32[0] = w[3];
1091 }
1092 break;
1093 }
1094 case SpvOpSpecConstant: {
1095 assert(glsl_type_is_scalar(val->const_type));
1096 val->constant->values[0].u32[0] = get_specialization(b, val, w[3]);
1097 int bit_size = glsl_get_bit_size(val->const_type);
1098 if (bit_size == 64)
1099 val->constant->values[0].u64[0] =
1100 get_specialization64(b, val, vtn_u64_literal(&w[3]));
1101 else
1102 val->constant->values[0].u32[0] = get_specialization(b, val, w[3]);
1103 break;
1104 }
1105 case SpvOpSpecConstantComposite:
1106 case SpvOpConstantComposite: {
1107 unsigned elem_count = count - 3;
1108 nir_constant **elems = ralloc_array(b, nir_constant *, elem_count);
1109 for (unsigned i = 0; i < elem_count; i++)
1110 elems[i] = vtn_value(b, w[i + 3], vtn_value_type_constant)->constant;
1111
1112 switch (glsl_get_base_type(val->const_type)) {
1113 case GLSL_TYPE_UINT:
1114 case GLSL_TYPE_INT:
1115 case GLSL_TYPE_UINT64:
1116 case GLSL_TYPE_INT64:
1117 case GLSL_TYPE_FLOAT:
1118 case GLSL_TYPE_BOOL:
1119 case GLSL_TYPE_DOUBLE: {
1120 int bit_size = glsl_get_bit_size(val->const_type);
1121 if (glsl_type_is_matrix(val->const_type)) {
1122 assert(glsl_get_matrix_columns(val->const_type) == elem_count);
1123 for (unsigned i = 0; i < elem_count; i++)
1124 val->constant->values[i] = elems[i]->values[0];
1125 } else {
1126 assert(glsl_type_is_vector(val->const_type));
1127 assert(glsl_get_vector_elements(val->const_type) == elem_count);
1128 for (unsigned i = 0; i < elem_count; i++) {
1129 if (bit_size == 64) {
1130 val->constant->values[0].u64[i] = elems[i]->values[0].u64[0];
1131 } else {
1132 assert(bit_size == 32);
1133 val->constant->values[0].u32[i] = elems[i]->values[0].u32[0];
1134 }
1135 }
1136 }
1137 ralloc_free(elems);
1138 break;
1139 }
1140 case GLSL_TYPE_STRUCT:
1141 case GLSL_TYPE_ARRAY:
1142 ralloc_steal(val->constant, elems);
1143 val->constant->num_elements = elem_count;
1144 val->constant->elements = elems;
1145 break;
1146
1147 default:
1148 unreachable("Unsupported type for constants");
1149 }
1150 break;
1151 }
1152
1153 case SpvOpSpecConstantOp: {
1154 SpvOp opcode = get_specialization(b, val, w[3]);
1155 switch (opcode) {
1156 case SpvOpVectorShuffle: {
1157 struct vtn_value *v0 = &b->values[w[4]];
1158 struct vtn_value *v1 = &b->values[w[5]];
1159
1160 assert(v0->value_type == vtn_value_type_constant ||
1161 v0->value_type == vtn_value_type_undef);
1162 assert(v1->value_type == vtn_value_type_constant ||
1163 v1->value_type == vtn_value_type_undef);
1164
1165 unsigned len0 = v0->value_type == vtn_value_type_constant ?
1166 glsl_get_vector_elements(v0->const_type) :
1167 glsl_get_vector_elements(v0->type->type);
1168 unsigned len1 = v1->value_type == vtn_value_type_constant ?
1169 glsl_get_vector_elements(v1->const_type) :
1170 glsl_get_vector_elements(v1->type->type);
1171
1172 assert(len0 + len1 < 16);
1173
1174 unsigned bit_size = glsl_get_bit_size(val->const_type);
1175 unsigned bit_size0 = v0->value_type == vtn_value_type_constant ?
1176 glsl_get_bit_size(v0->const_type) :
1177 glsl_get_bit_size(v0->type->type);
1178 unsigned bit_size1 = v1->value_type == vtn_value_type_constant ?
1179 glsl_get_bit_size(v1->const_type) :
1180 glsl_get_bit_size(v1->type->type);
1181
1182 assert(bit_size == bit_size0 && bit_size == bit_size1);
1183 (void)bit_size0; (void)bit_size1;
1184
1185 if (bit_size == 64) {
1186 uint64_t u64[8];
1187 if (v0->value_type == vtn_value_type_constant) {
1188 for (unsigned i = 0; i < len0; i++)
1189 u64[i] = v0->constant->values[0].u64[i];
1190 }
1191 if (v1->value_type == vtn_value_type_constant) {
1192 for (unsigned i = 0; i < len1; i++)
1193 u64[len0 + i] = v1->constant->values[0].u64[i];
1194 }
1195
1196 for (unsigned i = 0, j = 0; i < count - 6; i++, j++) {
1197 uint32_t comp = w[i + 6];
1198 /* If component is not used, set the value to a known constant
1199 * to detect if it is wrongly used.
1200 */
1201 if (comp == (uint32_t)-1)
1202 val->constant->values[0].u64[j] = 0xdeadbeefdeadbeef;
1203 else
1204 val->constant->values[0].u64[j] = u64[comp];
1205 }
1206 } else {
1207 uint32_t u32[8];
1208 if (v0->value_type == vtn_value_type_constant) {
1209 for (unsigned i = 0; i < len0; i++)
1210 u32[i] = v0->constant->values[0].u32[i];
1211 }
1212 if (v1->value_type == vtn_value_type_constant) {
1213 for (unsigned i = 0; i < len1; i++)
1214 u32[len0 + i] = v1->constant->values[0].u32[i];
1215 }
1216
1217 for (unsigned i = 0, j = 0; i < count - 6; i++, j++) {
1218 uint32_t comp = w[i + 6];
1219 /* If component is not used, set the value to a known constant
1220 * to detect if it is wrongly used.
1221 */
1222 if (comp == (uint32_t)-1)
1223 val->constant->values[0].u32[j] = 0xdeadbeef;
1224 else
1225 val->constant->values[0].u32[j] = u32[comp];
1226 }
1227 }
1228 break;
1229 }
1230
1231 case SpvOpCompositeExtract:
1232 case SpvOpCompositeInsert: {
1233 struct vtn_value *comp;
1234 unsigned deref_start;
1235 struct nir_constant **c;
1236 if (opcode == SpvOpCompositeExtract) {
1237 comp = vtn_value(b, w[4], vtn_value_type_constant);
1238 deref_start = 5;
1239 c = &comp->constant;
1240 } else {
1241 comp = vtn_value(b, w[5], vtn_value_type_constant);
1242 deref_start = 6;
1243 val->constant = nir_constant_clone(comp->constant,
1244 (nir_variable *)b);
1245 c = &val->constant;
1246 }
1247
1248 int elem = -1;
1249 int col = 0;
1250 const struct glsl_type *type = comp->const_type;
1251 for (unsigned i = deref_start; i < count; i++) {
1252 switch (glsl_get_base_type(type)) {
1253 case GLSL_TYPE_UINT:
1254 case GLSL_TYPE_INT:
1255 case GLSL_TYPE_UINT64:
1256 case GLSL_TYPE_INT64:
1257 case GLSL_TYPE_FLOAT:
1258 case GLSL_TYPE_DOUBLE:
1259 case GLSL_TYPE_BOOL:
1260 /* If we hit this granularity, we're picking off an element */
1261 if (glsl_type_is_matrix(type)) {
1262 assert(col == 0 && elem == -1);
1263 col = w[i];
1264 elem = 0;
1265 type = glsl_get_column_type(type);
1266 } else {
1267 assert(elem <= 0 && glsl_type_is_vector(type));
1268 elem = w[i];
1269 type = glsl_scalar_type(glsl_get_base_type(type));
1270 }
1271 continue;
1272
1273 case GLSL_TYPE_ARRAY:
1274 c = &(*c)->elements[w[i]];
1275 type = glsl_get_array_element(type);
1276 continue;
1277
1278 case GLSL_TYPE_STRUCT:
1279 c = &(*c)->elements[w[i]];
1280 type = glsl_get_struct_field(type, w[i]);
1281 continue;
1282
1283 default:
1284 unreachable("Invalid constant type");
1285 }
1286 }
1287
1288 if (opcode == SpvOpCompositeExtract) {
1289 if (elem == -1) {
1290 val->constant = *c;
1291 } else {
1292 unsigned num_components = glsl_get_vector_elements(type);
1293 unsigned bit_size = glsl_get_bit_size(type);
1294 for (unsigned i = 0; i < num_components; i++)
1295 if (bit_size == 64) {
1296 val->constant->values[0].u64[i] = (*c)->values[col].u64[elem + i];
1297 } else {
1298 assert(bit_size == 32);
1299 val->constant->values[0].u32[i] = (*c)->values[col].u32[elem + i];
1300 }
1301 }
1302 } else {
1303 struct vtn_value *insert =
1304 vtn_value(b, w[4], vtn_value_type_constant);
1305 assert(insert->const_type == type);
1306 if (elem == -1) {
1307 *c = insert->constant;
1308 } else {
1309 unsigned num_components = glsl_get_vector_elements(type);
1310 unsigned bit_size = glsl_get_bit_size(type);
1311 for (unsigned i = 0; i < num_components; i++)
1312 if (bit_size == 64) {
1313 (*c)->values[col].u64[elem + i] = insert->constant->values[0].u64[i];
1314 } else {
1315 assert(bit_size == 32);
1316 (*c)->values[col].u32[elem + i] = insert->constant->values[0].u32[i];
1317 }
1318 }
1319 }
1320 break;
1321 }
1322
1323 default: {
1324 bool swap;
1325 nir_alu_type dst_alu_type = nir_get_nir_type_for_glsl_type(val->const_type);
1326 nir_alu_type src_alu_type = dst_alu_type;
1327 nir_op op = vtn_nir_alu_op_for_spirv_opcode(opcode, &swap, src_alu_type, dst_alu_type);
1328
1329 unsigned num_components = glsl_get_vector_elements(val->const_type);
1330 unsigned bit_size =
1331 glsl_get_bit_size(val->const_type);
1332
1333 nir_const_value src[4];
1334 assert(count <= 7);
1335 for (unsigned i = 0; i < count - 4; i++) {
1336 nir_constant *c =
1337 vtn_value(b, w[4 + i], vtn_value_type_constant)->constant;
1338
1339 unsigned j = swap ? 1 - i : i;
1340 assert(bit_size == 32);
1341 src[j] = c->values[0];
1342 }
1343
1344 val->constant->values[0] =
1345 nir_eval_const_opcode(op, num_components, bit_size, src);
1346 break;
1347 } /* default */
1348 }
1349 break;
1350 }
1351
1352 case SpvOpConstantNull:
1353 val->constant = vtn_null_constant(b, val->const_type);
1354 break;
1355
1356 case SpvOpConstantSampler:
1357 assert(!"OpConstantSampler requires Kernel Capability");
1358 break;
1359
1360 default:
1361 unreachable("Unhandled opcode");
1362 }
1363
1364 /* Now that we have the value, update the workgroup size if needed */
1365 vtn_foreach_decoration(b, val, handle_workgroup_size_decoration_cb, NULL);
1366 }
1367
1368 static void
1369 vtn_handle_function_call(struct vtn_builder *b, SpvOp opcode,
1370 const uint32_t *w, unsigned count)
1371 {
1372 struct nir_function *callee =
1373 vtn_value(b, w[3], vtn_value_type_function)->func->impl->function;
1374
1375 nir_call_instr *call = nir_call_instr_create(b->nb.shader, callee);
1376 for (unsigned i = 0; i < call->num_params; i++) {
1377 unsigned arg_id = w[4 + i];
1378 struct vtn_value *arg = vtn_untyped_value(b, arg_id);
1379 if (arg->value_type == vtn_value_type_pointer) {
1380 nir_deref_var *d = vtn_pointer_to_deref(b, arg->pointer);
1381 call->params[i] = nir_deref_var_clone(d, call);
1382 } else {
1383 struct vtn_ssa_value *arg_ssa = vtn_ssa_value(b, arg_id);
1384
1385 /* Make a temporary to store the argument in */
1386 nir_variable *tmp =
1387 nir_local_variable_create(b->impl, arg_ssa->type, "arg_tmp");
1388 call->params[i] = nir_deref_var_create(call, tmp);
1389
1390 vtn_local_store(b, arg_ssa, call->params[i]);
1391 }
1392 }
1393
1394 nir_variable *out_tmp = NULL;
1395 if (!glsl_type_is_void(callee->return_type)) {
1396 out_tmp = nir_local_variable_create(b->impl, callee->return_type,
1397 "out_tmp");
1398 call->return_deref = nir_deref_var_create(call, out_tmp);
1399 }
1400
1401 nir_builder_instr_insert(&b->nb, &call->instr);
1402
1403 if (glsl_type_is_void(callee->return_type)) {
1404 vtn_push_value(b, w[2], vtn_value_type_undef);
1405 } else {
1406 struct vtn_value *retval = vtn_push_value(b, w[2], vtn_value_type_ssa);
1407 retval->ssa = vtn_local_load(b, call->return_deref);
1408 }
1409 }
1410
1411 struct vtn_ssa_value *
1412 vtn_create_ssa_value(struct vtn_builder *b, const struct glsl_type *type)
1413 {
1414 struct vtn_ssa_value *val = rzalloc(b, struct vtn_ssa_value);
1415 val->type = type;
1416
1417 if (!glsl_type_is_vector_or_scalar(type)) {
1418 unsigned elems = glsl_get_length(type);
1419 val->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
1420 for (unsigned i = 0; i < elems; i++) {
1421 const struct glsl_type *child_type;
1422
1423 switch (glsl_get_base_type(type)) {
1424 case GLSL_TYPE_INT:
1425 case GLSL_TYPE_UINT:
1426 case GLSL_TYPE_INT64:
1427 case GLSL_TYPE_UINT64:
1428 case GLSL_TYPE_BOOL:
1429 case GLSL_TYPE_FLOAT:
1430 case GLSL_TYPE_DOUBLE:
1431 child_type = glsl_get_column_type(type);
1432 break;
1433 case GLSL_TYPE_ARRAY:
1434 child_type = glsl_get_array_element(type);
1435 break;
1436 case GLSL_TYPE_STRUCT:
1437 child_type = glsl_get_struct_field(type, i);
1438 break;
1439 default:
1440 unreachable("unkown base type");
1441 }
1442
1443 val->elems[i] = vtn_create_ssa_value(b, child_type);
1444 }
1445 }
1446
1447 return val;
1448 }
1449
1450 static nir_tex_src
1451 vtn_tex_src(struct vtn_builder *b, unsigned index, nir_tex_src_type type)
1452 {
1453 nir_tex_src src;
1454 src.src = nir_src_for_ssa(vtn_ssa_value(b, index)->def);
1455 src.src_type = type;
1456 return src;
1457 }
1458
1459 static void
1460 vtn_handle_texture(struct vtn_builder *b, SpvOp opcode,
1461 const uint32_t *w, unsigned count)
1462 {
1463 if (opcode == SpvOpSampledImage) {
1464 struct vtn_value *val =
1465 vtn_push_value(b, w[2], vtn_value_type_sampled_image);
1466 val->sampled_image = ralloc(b, struct vtn_sampled_image);
1467 val->sampled_image->image =
1468 vtn_value(b, w[3], vtn_value_type_pointer)->pointer;
1469 val->sampled_image->sampler =
1470 vtn_value(b, w[4], vtn_value_type_pointer)->pointer;
1471 return;
1472 } else if (opcode == SpvOpImage) {
1473 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_pointer);
1474 struct vtn_value *src_val = vtn_untyped_value(b, w[3]);
1475 if (src_val->value_type == vtn_value_type_sampled_image) {
1476 val->pointer = src_val->sampled_image->image;
1477 } else {
1478 assert(src_val->value_type == vtn_value_type_pointer);
1479 val->pointer = src_val->pointer;
1480 }
1481 return;
1482 }
1483
1484 struct vtn_type *ret_type = vtn_value(b, w[1], vtn_value_type_type)->type;
1485 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
1486
1487 struct vtn_sampled_image sampled;
1488 struct vtn_value *sampled_val = vtn_untyped_value(b, w[3]);
1489 if (sampled_val->value_type == vtn_value_type_sampled_image) {
1490 sampled = *sampled_val->sampled_image;
1491 } else {
1492 assert(sampled_val->value_type == vtn_value_type_pointer);
1493 sampled.image = NULL;
1494 sampled.sampler = sampled_val->pointer;
1495 }
1496
1497 const struct glsl_type *image_type;
1498 if (sampled.image) {
1499 image_type = sampled.image->var->var->interface_type;
1500 } else {
1501 image_type = sampled.sampler->var->var->interface_type;
1502 }
1503 const enum glsl_sampler_dim sampler_dim = glsl_get_sampler_dim(image_type);
1504 const bool is_array = glsl_sampler_type_is_array(image_type);
1505 const bool is_shadow = glsl_sampler_type_is_shadow(image_type);
1506
1507 /* Figure out the base texture operation */
1508 nir_texop texop;
1509 switch (opcode) {
1510 case SpvOpImageSampleImplicitLod:
1511 case SpvOpImageSampleDrefImplicitLod:
1512 case SpvOpImageSampleProjImplicitLod:
1513 case SpvOpImageSampleProjDrefImplicitLod:
1514 texop = nir_texop_tex;
1515 break;
1516
1517 case SpvOpImageSampleExplicitLod:
1518 case SpvOpImageSampleDrefExplicitLod:
1519 case SpvOpImageSampleProjExplicitLod:
1520 case SpvOpImageSampleProjDrefExplicitLod:
1521 texop = nir_texop_txl;
1522 break;
1523
1524 case SpvOpImageFetch:
1525 if (glsl_get_sampler_dim(image_type) == GLSL_SAMPLER_DIM_MS) {
1526 texop = nir_texop_txf_ms;
1527 } else {
1528 texop = nir_texop_txf;
1529 }
1530 break;
1531
1532 case SpvOpImageGather:
1533 case SpvOpImageDrefGather:
1534 texop = nir_texop_tg4;
1535 break;
1536
1537 case SpvOpImageQuerySizeLod:
1538 case SpvOpImageQuerySize:
1539 texop = nir_texop_txs;
1540 break;
1541
1542 case SpvOpImageQueryLod:
1543 texop = nir_texop_lod;
1544 break;
1545
1546 case SpvOpImageQueryLevels:
1547 texop = nir_texop_query_levels;
1548 break;
1549
1550 case SpvOpImageQuerySamples:
1551 texop = nir_texop_texture_samples;
1552 break;
1553
1554 default:
1555 unreachable("Unhandled opcode");
1556 }
1557
1558 nir_tex_src srcs[8]; /* 8 should be enough */
1559 nir_tex_src *p = srcs;
1560
1561 unsigned idx = 4;
1562
1563 struct nir_ssa_def *coord;
1564 unsigned coord_components;
1565 switch (opcode) {
1566 case SpvOpImageSampleImplicitLod:
1567 case SpvOpImageSampleExplicitLod:
1568 case SpvOpImageSampleDrefImplicitLod:
1569 case SpvOpImageSampleDrefExplicitLod:
1570 case SpvOpImageSampleProjImplicitLod:
1571 case SpvOpImageSampleProjExplicitLod:
1572 case SpvOpImageSampleProjDrefImplicitLod:
1573 case SpvOpImageSampleProjDrefExplicitLod:
1574 case SpvOpImageFetch:
1575 case SpvOpImageGather:
1576 case SpvOpImageDrefGather:
1577 case SpvOpImageQueryLod: {
1578 /* All these types have the coordinate as their first real argument */
1579 switch (sampler_dim) {
1580 case GLSL_SAMPLER_DIM_1D:
1581 case GLSL_SAMPLER_DIM_BUF:
1582 coord_components = 1;
1583 break;
1584 case GLSL_SAMPLER_DIM_2D:
1585 case GLSL_SAMPLER_DIM_RECT:
1586 case GLSL_SAMPLER_DIM_MS:
1587 coord_components = 2;
1588 break;
1589 case GLSL_SAMPLER_DIM_3D:
1590 case GLSL_SAMPLER_DIM_CUBE:
1591 coord_components = 3;
1592 break;
1593 default:
1594 unreachable("Invalid sampler type");
1595 }
1596
1597 if (is_array && texop != nir_texop_lod)
1598 coord_components++;
1599
1600 coord = vtn_ssa_value(b, w[idx++])->def;
1601 p->src = nir_src_for_ssa(nir_channels(&b->nb, coord,
1602 (1 << coord_components) - 1));
1603 p->src_type = nir_tex_src_coord;
1604 p++;
1605 break;
1606 }
1607
1608 default:
1609 coord = NULL;
1610 coord_components = 0;
1611 break;
1612 }
1613
1614 switch (opcode) {
1615 case SpvOpImageSampleProjImplicitLod:
1616 case SpvOpImageSampleProjExplicitLod:
1617 case SpvOpImageSampleProjDrefImplicitLod:
1618 case SpvOpImageSampleProjDrefExplicitLod:
1619 /* These have the projector as the last coordinate component */
1620 p->src = nir_src_for_ssa(nir_channel(&b->nb, coord, coord_components));
1621 p->src_type = nir_tex_src_projector;
1622 p++;
1623 break;
1624
1625 default:
1626 break;
1627 }
1628
1629 unsigned gather_component = 0;
1630 switch (opcode) {
1631 case SpvOpImageSampleDrefImplicitLod:
1632 case SpvOpImageSampleDrefExplicitLod:
1633 case SpvOpImageSampleProjDrefImplicitLod:
1634 case SpvOpImageSampleProjDrefExplicitLod:
1635 case SpvOpImageDrefGather:
1636 /* These all have an explicit depth value as their next source */
1637 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_comparator);
1638 break;
1639
1640 case SpvOpImageGather:
1641 /* This has a component as its next source */
1642 gather_component =
1643 vtn_value(b, w[idx++], vtn_value_type_constant)->constant->values[0].u32[0];
1644 break;
1645
1646 default:
1647 break;
1648 }
1649
1650 /* For OpImageQuerySizeLod, we always have an LOD */
1651 if (opcode == SpvOpImageQuerySizeLod)
1652 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_lod);
1653
1654 /* Now we need to handle some number of optional arguments */
1655 const struct vtn_ssa_value *gather_offsets = NULL;
1656 if (idx < count) {
1657 uint32_t operands = w[idx++];
1658
1659 if (operands & SpvImageOperandsBiasMask) {
1660 assert(texop == nir_texop_tex);
1661 texop = nir_texop_txb;
1662 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_bias);
1663 }
1664
1665 if (operands & SpvImageOperandsLodMask) {
1666 assert(texop == nir_texop_txl || texop == nir_texop_txf ||
1667 texop == nir_texop_txs);
1668 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_lod);
1669 }
1670
1671 if (operands & SpvImageOperandsGradMask) {
1672 assert(texop == nir_texop_txl);
1673 texop = nir_texop_txd;
1674 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_ddx);
1675 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_ddy);
1676 }
1677
1678 if (operands & SpvImageOperandsOffsetMask ||
1679 operands & SpvImageOperandsConstOffsetMask)
1680 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_offset);
1681
1682 if (operands & SpvImageOperandsConstOffsetsMask) {
1683 gather_offsets = vtn_ssa_value(b, w[idx++]);
1684 (*p++) = (nir_tex_src){};
1685 }
1686
1687 if (operands & SpvImageOperandsSampleMask) {
1688 assert(texop == nir_texop_txf_ms);
1689 texop = nir_texop_txf_ms;
1690 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_ms_index);
1691 }
1692 }
1693 /* We should have now consumed exactly all of the arguments */
1694 assert(idx == count);
1695
1696 nir_tex_instr *instr = nir_tex_instr_create(b->shader, p - srcs);
1697 instr->op = texop;
1698
1699 memcpy(instr->src, srcs, instr->num_srcs * sizeof(*instr->src));
1700
1701 instr->coord_components = coord_components;
1702 instr->sampler_dim = sampler_dim;
1703 instr->is_array = is_array;
1704 instr->is_shadow = is_shadow;
1705 instr->is_new_style_shadow =
1706 is_shadow && glsl_get_components(ret_type->type) == 1;
1707 instr->component = gather_component;
1708
1709 switch (glsl_get_sampler_result_type(image_type)) {
1710 case GLSL_TYPE_FLOAT: instr->dest_type = nir_type_float; break;
1711 case GLSL_TYPE_INT: instr->dest_type = nir_type_int; break;
1712 case GLSL_TYPE_UINT: instr->dest_type = nir_type_uint; break;
1713 case GLSL_TYPE_BOOL: instr->dest_type = nir_type_bool; break;
1714 default:
1715 unreachable("Invalid base type for sampler result");
1716 }
1717
1718 nir_deref_var *sampler = vtn_pointer_to_deref(b, sampled.sampler);
1719 nir_deref_var *texture;
1720 if (sampled.image) {
1721 nir_deref_var *image = vtn_pointer_to_deref(b, sampled.image);
1722 texture = image;
1723 } else {
1724 texture = sampler;
1725 }
1726
1727 instr->texture = nir_deref_var_clone(texture, instr);
1728
1729 switch (instr->op) {
1730 case nir_texop_tex:
1731 case nir_texop_txb:
1732 case nir_texop_txl:
1733 case nir_texop_txd:
1734 /* These operations require a sampler */
1735 instr->sampler = nir_deref_var_clone(sampler, instr);
1736 break;
1737 case nir_texop_txf:
1738 case nir_texop_txf_ms:
1739 case nir_texop_txs:
1740 case nir_texop_lod:
1741 case nir_texop_tg4:
1742 case nir_texop_query_levels:
1743 case nir_texop_texture_samples:
1744 case nir_texop_samples_identical:
1745 /* These don't */
1746 instr->sampler = NULL;
1747 break;
1748 case nir_texop_txf_ms_mcs:
1749 unreachable("unexpected nir_texop_txf_ms_mcs");
1750 }
1751
1752 nir_ssa_dest_init(&instr->instr, &instr->dest,
1753 nir_tex_instr_dest_size(instr), 32, NULL);
1754
1755 assert(glsl_get_vector_elements(ret_type->type) ==
1756 nir_tex_instr_dest_size(instr));
1757
1758 nir_ssa_def *def;
1759 nir_instr *instruction;
1760 if (gather_offsets) {
1761 assert(glsl_get_base_type(gather_offsets->type) == GLSL_TYPE_ARRAY);
1762 assert(glsl_get_length(gather_offsets->type) == 4);
1763 nir_tex_instr *instrs[4] = {instr, NULL, NULL, NULL};
1764
1765 /* Copy the current instruction 4x */
1766 for (uint32_t i = 1; i < 4; i++) {
1767 instrs[i] = nir_tex_instr_create(b->shader, instr->num_srcs);
1768 instrs[i]->op = instr->op;
1769 instrs[i]->coord_components = instr->coord_components;
1770 instrs[i]->sampler_dim = instr->sampler_dim;
1771 instrs[i]->is_array = instr->is_array;
1772 instrs[i]->is_shadow = instr->is_shadow;
1773 instrs[i]->is_new_style_shadow = instr->is_new_style_shadow;
1774 instrs[i]->component = instr->component;
1775 instrs[i]->dest_type = instr->dest_type;
1776 instrs[i]->texture = nir_deref_var_clone(texture, instrs[i]);
1777 instrs[i]->sampler = NULL;
1778
1779 memcpy(instrs[i]->src, srcs, instr->num_srcs * sizeof(*instr->src));
1780
1781 nir_ssa_dest_init(&instrs[i]->instr, &instrs[i]->dest,
1782 nir_tex_instr_dest_size(instr), 32, NULL);
1783 }
1784
1785 /* Fill in the last argument with the offset from the passed in offsets
1786 * and insert the instruction into the stream.
1787 */
1788 for (uint32_t i = 0; i < 4; i++) {
1789 nir_tex_src src;
1790 src.src = nir_src_for_ssa(gather_offsets->elems[i]->def);
1791 src.src_type = nir_tex_src_offset;
1792 instrs[i]->src[instrs[i]->num_srcs - 1] = src;
1793 nir_builder_instr_insert(&b->nb, &instrs[i]->instr);
1794 }
1795
1796 /* Combine the results of the 4 instructions by taking their .w
1797 * components
1798 */
1799 nir_alu_instr *vec4 = nir_alu_instr_create(b->shader, nir_op_vec4);
1800 nir_ssa_dest_init(&vec4->instr, &vec4->dest.dest, 4, 32, NULL);
1801 vec4->dest.write_mask = 0xf;
1802 for (uint32_t i = 0; i < 4; i++) {
1803 vec4->src[i].src = nir_src_for_ssa(&instrs[i]->dest.ssa);
1804 vec4->src[i].swizzle[0] = 3;
1805 }
1806 def = &vec4->dest.dest.ssa;
1807 instruction = &vec4->instr;
1808 } else {
1809 def = &instr->dest.ssa;
1810 instruction = &instr->instr;
1811 }
1812
1813 val->ssa = vtn_create_ssa_value(b, ret_type->type);
1814 val->ssa->def = def;
1815
1816 nir_builder_instr_insert(&b->nb, instruction);
1817 }
1818
1819 static void
1820 fill_common_atomic_sources(struct vtn_builder *b, SpvOp opcode,
1821 const uint32_t *w, nir_src *src)
1822 {
1823 switch (opcode) {
1824 case SpvOpAtomicIIncrement:
1825 src[0] = nir_src_for_ssa(nir_imm_int(&b->nb, 1));
1826 break;
1827
1828 case SpvOpAtomicIDecrement:
1829 src[0] = nir_src_for_ssa(nir_imm_int(&b->nb, -1));
1830 break;
1831
1832 case SpvOpAtomicISub:
1833 src[0] =
1834 nir_src_for_ssa(nir_ineg(&b->nb, vtn_ssa_value(b, w[6])->def));
1835 break;
1836
1837 case SpvOpAtomicCompareExchange:
1838 src[0] = nir_src_for_ssa(vtn_ssa_value(b, w[8])->def);
1839 src[1] = nir_src_for_ssa(vtn_ssa_value(b, w[7])->def);
1840 break;
1841
1842 case SpvOpAtomicExchange:
1843 case SpvOpAtomicIAdd:
1844 case SpvOpAtomicSMin:
1845 case SpvOpAtomicUMin:
1846 case SpvOpAtomicSMax:
1847 case SpvOpAtomicUMax:
1848 case SpvOpAtomicAnd:
1849 case SpvOpAtomicOr:
1850 case SpvOpAtomicXor:
1851 src[0] = nir_src_for_ssa(vtn_ssa_value(b, w[6])->def);
1852 break;
1853
1854 default:
1855 unreachable("Invalid SPIR-V atomic");
1856 }
1857 }
1858
1859 static nir_ssa_def *
1860 get_image_coord(struct vtn_builder *b, uint32_t value)
1861 {
1862 struct vtn_ssa_value *coord = vtn_ssa_value(b, value);
1863
1864 /* The image_load_store intrinsics assume a 4-dim coordinate */
1865 unsigned dim = glsl_get_vector_elements(coord->type);
1866 unsigned swizzle[4];
1867 for (unsigned i = 0; i < 4; i++)
1868 swizzle[i] = MIN2(i, dim - 1);
1869
1870 return nir_swizzle(&b->nb, coord->def, swizzle, 4, false);
1871 }
1872
1873 static void
1874 vtn_handle_image(struct vtn_builder *b, SpvOp opcode,
1875 const uint32_t *w, unsigned count)
1876 {
1877 /* Just get this one out of the way */
1878 if (opcode == SpvOpImageTexelPointer) {
1879 struct vtn_value *val =
1880 vtn_push_value(b, w[2], vtn_value_type_image_pointer);
1881 val->image = ralloc(b, struct vtn_image_pointer);
1882
1883 val->image->image = vtn_value(b, w[3], vtn_value_type_pointer)->pointer;
1884 val->image->coord = get_image_coord(b, w[4]);
1885 val->image->sample = vtn_ssa_value(b, w[5])->def;
1886 return;
1887 }
1888
1889 struct vtn_image_pointer image;
1890
1891 switch (opcode) {
1892 case SpvOpAtomicExchange:
1893 case SpvOpAtomicCompareExchange:
1894 case SpvOpAtomicCompareExchangeWeak:
1895 case SpvOpAtomicIIncrement:
1896 case SpvOpAtomicIDecrement:
1897 case SpvOpAtomicIAdd:
1898 case SpvOpAtomicISub:
1899 case SpvOpAtomicLoad:
1900 case SpvOpAtomicSMin:
1901 case SpvOpAtomicUMin:
1902 case SpvOpAtomicSMax:
1903 case SpvOpAtomicUMax:
1904 case SpvOpAtomicAnd:
1905 case SpvOpAtomicOr:
1906 case SpvOpAtomicXor:
1907 image = *vtn_value(b, w[3], vtn_value_type_image_pointer)->image;
1908 break;
1909
1910 case SpvOpAtomicStore:
1911 image = *vtn_value(b, w[1], vtn_value_type_image_pointer)->image;
1912 break;
1913
1914 case SpvOpImageQuerySize:
1915 image.image = vtn_value(b, w[3], vtn_value_type_pointer)->pointer;
1916 image.coord = NULL;
1917 image.sample = NULL;
1918 break;
1919
1920 case SpvOpImageRead:
1921 image.image = vtn_value(b, w[3], vtn_value_type_pointer)->pointer;
1922 image.coord = get_image_coord(b, w[4]);
1923
1924 if (count > 5 && (w[5] & SpvImageOperandsSampleMask)) {
1925 assert(w[5] == SpvImageOperandsSampleMask);
1926 image.sample = vtn_ssa_value(b, w[6])->def;
1927 } else {
1928 image.sample = nir_ssa_undef(&b->nb, 1, 32);
1929 }
1930 break;
1931
1932 case SpvOpImageWrite:
1933 image.image = vtn_value(b, w[1], vtn_value_type_pointer)->pointer;
1934 image.coord = get_image_coord(b, w[2]);
1935
1936 /* texel = w[3] */
1937
1938 if (count > 4 && (w[4] & SpvImageOperandsSampleMask)) {
1939 assert(w[4] == SpvImageOperandsSampleMask);
1940 image.sample = vtn_ssa_value(b, w[5])->def;
1941 } else {
1942 image.sample = nir_ssa_undef(&b->nb, 1, 32);
1943 }
1944 break;
1945
1946 default:
1947 unreachable("Invalid image opcode");
1948 }
1949
1950 nir_intrinsic_op op;
1951 switch (opcode) {
1952 #define OP(S, N) case SpvOp##S: op = nir_intrinsic_image_##N; break;
1953 OP(ImageQuerySize, size)
1954 OP(ImageRead, load)
1955 OP(ImageWrite, store)
1956 OP(AtomicLoad, load)
1957 OP(AtomicStore, store)
1958 OP(AtomicExchange, atomic_exchange)
1959 OP(AtomicCompareExchange, atomic_comp_swap)
1960 OP(AtomicIIncrement, atomic_add)
1961 OP(AtomicIDecrement, atomic_add)
1962 OP(AtomicIAdd, atomic_add)
1963 OP(AtomicISub, atomic_add)
1964 OP(AtomicSMin, atomic_min)
1965 OP(AtomicUMin, atomic_min)
1966 OP(AtomicSMax, atomic_max)
1967 OP(AtomicUMax, atomic_max)
1968 OP(AtomicAnd, atomic_and)
1969 OP(AtomicOr, atomic_or)
1970 OP(AtomicXor, atomic_xor)
1971 #undef OP
1972 default:
1973 unreachable("Invalid image opcode");
1974 }
1975
1976 nir_intrinsic_instr *intrin = nir_intrinsic_instr_create(b->shader, op);
1977
1978 nir_deref_var *image_deref = vtn_pointer_to_deref(b, image.image);
1979 intrin->variables[0] = nir_deref_var_clone(image_deref, intrin);
1980
1981 /* ImageQuerySize doesn't take any extra parameters */
1982 if (opcode != SpvOpImageQuerySize) {
1983 /* The image coordinate is always 4 components but we may not have that
1984 * many. Swizzle to compensate.
1985 */
1986 unsigned swiz[4];
1987 for (unsigned i = 0; i < 4; i++)
1988 swiz[i] = i < image.coord->num_components ? i : 0;
1989 intrin->src[0] = nir_src_for_ssa(nir_swizzle(&b->nb, image.coord,
1990 swiz, 4, false));
1991 intrin->src[1] = nir_src_for_ssa(image.sample);
1992 }
1993
1994 switch (opcode) {
1995 case SpvOpAtomicLoad:
1996 case SpvOpImageQuerySize:
1997 case SpvOpImageRead:
1998 break;
1999 case SpvOpAtomicStore:
2000 intrin->src[2] = nir_src_for_ssa(vtn_ssa_value(b, w[4])->def);
2001 break;
2002 case SpvOpImageWrite:
2003 intrin->src[2] = nir_src_for_ssa(vtn_ssa_value(b, w[3])->def);
2004 break;
2005
2006 case SpvOpAtomicIIncrement:
2007 case SpvOpAtomicIDecrement:
2008 case SpvOpAtomicExchange:
2009 case SpvOpAtomicIAdd:
2010 case SpvOpAtomicSMin:
2011 case SpvOpAtomicUMin:
2012 case SpvOpAtomicSMax:
2013 case SpvOpAtomicUMax:
2014 case SpvOpAtomicAnd:
2015 case SpvOpAtomicOr:
2016 case SpvOpAtomicXor:
2017 fill_common_atomic_sources(b, opcode, w, &intrin->src[2]);
2018 break;
2019
2020 default:
2021 unreachable("Invalid image opcode");
2022 }
2023
2024 if (opcode != SpvOpImageWrite) {
2025 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
2026 struct vtn_type *type = vtn_value(b, w[1], vtn_value_type_type)->type;
2027
2028 unsigned dest_components =
2029 nir_intrinsic_infos[intrin->intrinsic].dest_components;
2030 if (intrin->intrinsic == nir_intrinsic_image_size) {
2031 dest_components = intrin->num_components =
2032 glsl_get_vector_elements(type->type);
2033 }
2034
2035 nir_ssa_dest_init(&intrin->instr, &intrin->dest,
2036 dest_components, 32, NULL);
2037
2038 nir_builder_instr_insert(&b->nb, &intrin->instr);
2039
2040 val->ssa = vtn_create_ssa_value(b, type->type);
2041 val->ssa->def = &intrin->dest.ssa;
2042 } else {
2043 nir_builder_instr_insert(&b->nb, &intrin->instr);
2044 }
2045 }
2046
2047 static nir_intrinsic_op
2048 get_ssbo_nir_atomic_op(SpvOp opcode)
2049 {
2050 switch (opcode) {
2051 case SpvOpAtomicLoad: return nir_intrinsic_load_ssbo;
2052 case SpvOpAtomicStore: return nir_intrinsic_store_ssbo;
2053 #define OP(S, N) case SpvOp##S: return nir_intrinsic_ssbo_##N;
2054 OP(AtomicExchange, atomic_exchange)
2055 OP(AtomicCompareExchange, atomic_comp_swap)
2056 OP(AtomicIIncrement, atomic_add)
2057 OP(AtomicIDecrement, atomic_add)
2058 OP(AtomicIAdd, atomic_add)
2059 OP(AtomicISub, atomic_add)
2060 OP(AtomicSMin, atomic_imin)
2061 OP(AtomicUMin, atomic_umin)
2062 OP(AtomicSMax, atomic_imax)
2063 OP(AtomicUMax, atomic_umax)
2064 OP(AtomicAnd, atomic_and)
2065 OP(AtomicOr, atomic_or)
2066 OP(AtomicXor, atomic_xor)
2067 #undef OP
2068 default:
2069 unreachable("Invalid SSBO atomic");
2070 }
2071 }
2072
2073 static nir_intrinsic_op
2074 get_shared_nir_atomic_op(SpvOp opcode)
2075 {
2076 switch (opcode) {
2077 case SpvOpAtomicLoad: return nir_intrinsic_load_var;
2078 case SpvOpAtomicStore: return nir_intrinsic_store_var;
2079 #define OP(S, N) case SpvOp##S: return nir_intrinsic_var_##N;
2080 OP(AtomicExchange, atomic_exchange)
2081 OP(AtomicCompareExchange, atomic_comp_swap)
2082 OP(AtomicIIncrement, atomic_add)
2083 OP(AtomicIDecrement, atomic_add)
2084 OP(AtomicIAdd, atomic_add)
2085 OP(AtomicISub, atomic_add)
2086 OP(AtomicSMin, atomic_imin)
2087 OP(AtomicUMin, atomic_umin)
2088 OP(AtomicSMax, atomic_imax)
2089 OP(AtomicUMax, atomic_umax)
2090 OP(AtomicAnd, atomic_and)
2091 OP(AtomicOr, atomic_or)
2092 OP(AtomicXor, atomic_xor)
2093 #undef OP
2094 default:
2095 unreachable("Invalid shared atomic");
2096 }
2097 }
2098
2099 static void
2100 vtn_handle_ssbo_or_shared_atomic(struct vtn_builder *b, SpvOp opcode,
2101 const uint32_t *w, unsigned count)
2102 {
2103 struct vtn_pointer *ptr;
2104 nir_intrinsic_instr *atomic;
2105
2106 switch (opcode) {
2107 case SpvOpAtomicLoad:
2108 case SpvOpAtomicExchange:
2109 case SpvOpAtomicCompareExchange:
2110 case SpvOpAtomicCompareExchangeWeak:
2111 case SpvOpAtomicIIncrement:
2112 case SpvOpAtomicIDecrement:
2113 case SpvOpAtomicIAdd:
2114 case SpvOpAtomicISub:
2115 case SpvOpAtomicSMin:
2116 case SpvOpAtomicUMin:
2117 case SpvOpAtomicSMax:
2118 case SpvOpAtomicUMax:
2119 case SpvOpAtomicAnd:
2120 case SpvOpAtomicOr:
2121 case SpvOpAtomicXor:
2122 ptr = vtn_value(b, w[3], vtn_value_type_pointer)->pointer;
2123 break;
2124
2125 case SpvOpAtomicStore:
2126 ptr = vtn_value(b, w[1], vtn_value_type_pointer)->pointer;
2127 break;
2128
2129 default:
2130 unreachable("Invalid SPIR-V atomic");
2131 }
2132
2133 /*
2134 SpvScope scope = w[4];
2135 SpvMemorySemanticsMask semantics = w[5];
2136 */
2137
2138 if (ptr->mode == vtn_variable_mode_workgroup) {
2139 nir_deref_var *deref = vtn_pointer_to_deref(b, ptr);
2140 const struct glsl_type *deref_type = nir_deref_tail(&deref->deref)->type;
2141 nir_intrinsic_op op = get_shared_nir_atomic_op(opcode);
2142 atomic = nir_intrinsic_instr_create(b->nb.shader, op);
2143 atomic->variables[0] = nir_deref_var_clone(deref, atomic);
2144
2145 switch (opcode) {
2146 case SpvOpAtomicLoad:
2147 atomic->num_components = glsl_get_vector_elements(deref_type);
2148 break;
2149
2150 case SpvOpAtomicStore:
2151 atomic->num_components = glsl_get_vector_elements(deref_type);
2152 nir_intrinsic_set_write_mask(atomic, (1 << atomic->num_components) - 1);
2153 atomic->src[0] = nir_src_for_ssa(vtn_ssa_value(b, w[4])->def);
2154 break;
2155
2156 case SpvOpAtomicExchange:
2157 case SpvOpAtomicCompareExchange:
2158 case SpvOpAtomicCompareExchangeWeak:
2159 case SpvOpAtomicIIncrement:
2160 case SpvOpAtomicIDecrement:
2161 case SpvOpAtomicIAdd:
2162 case SpvOpAtomicISub:
2163 case SpvOpAtomicSMin:
2164 case SpvOpAtomicUMin:
2165 case SpvOpAtomicSMax:
2166 case SpvOpAtomicUMax:
2167 case SpvOpAtomicAnd:
2168 case SpvOpAtomicOr:
2169 case SpvOpAtomicXor:
2170 fill_common_atomic_sources(b, opcode, w, &atomic->src[0]);
2171 break;
2172
2173 default:
2174 unreachable("Invalid SPIR-V atomic");
2175
2176 }
2177 } else {
2178 assert(ptr->mode == vtn_variable_mode_ssbo);
2179 nir_ssa_def *offset, *index;
2180 offset = vtn_pointer_to_offset(b, ptr, &index, NULL);
2181
2182 nir_intrinsic_op op = get_ssbo_nir_atomic_op(opcode);
2183
2184 atomic = nir_intrinsic_instr_create(b->nb.shader, op);
2185
2186 switch (opcode) {
2187 case SpvOpAtomicLoad:
2188 atomic->num_components = glsl_get_vector_elements(ptr->type->type);
2189 atomic->src[0] = nir_src_for_ssa(index);
2190 atomic->src[1] = nir_src_for_ssa(offset);
2191 break;
2192
2193 case SpvOpAtomicStore:
2194 atomic->num_components = glsl_get_vector_elements(ptr->type->type);
2195 nir_intrinsic_set_write_mask(atomic, (1 << atomic->num_components) - 1);
2196 atomic->src[0] = nir_src_for_ssa(vtn_ssa_value(b, w[4])->def);
2197 atomic->src[1] = nir_src_for_ssa(index);
2198 atomic->src[2] = nir_src_for_ssa(offset);
2199 break;
2200
2201 case SpvOpAtomicExchange:
2202 case SpvOpAtomicCompareExchange:
2203 case SpvOpAtomicCompareExchangeWeak:
2204 case SpvOpAtomicIIncrement:
2205 case SpvOpAtomicIDecrement:
2206 case SpvOpAtomicIAdd:
2207 case SpvOpAtomicISub:
2208 case SpvOpAtomicSMin:
2209 case SpvOpAtomicUMin:
2210 case SpvOpAtomicSMax:
2211 case SpvOpAtomicUMax:
2212 case SpvOpAtomicAnd:
2213 case SpvOpAtomicOr:
2214 case SpvOpAtomicXor:
2215 atomic->src[0] = nir_src_for_ssa(index);
2216 atomic->src[1] = nir_src_for_ssa(offset);
2217 fill_common_atomic_sources(b, opcode, w, &atomic->src[2]);
2218 break;
2219
2220 default:
2221 unreachable("Invalid SPIR-V atomic");
2222 }
2223 }
2224
2225 if (opcode != SpvOpAtomicStore) {
2226 struct vtn_type *type = vtn_value(b, w[1], vtn_value_type_type)->type;
2227
2228 nir_ssa_dest_init(&atomic->instr, &atomic->dest,
2229 glsl_get_vector_elements(type->type),
2230 glsl_get_bit_size(type->type), NULL);
2231
2232 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
2233 val->ssa = rzalloc(b, struct vtn_ssa_value);
2234 val->ssa->def = &atomic->dest.ssa;
2235 val->ssa->type = type->type;
2236 }
2237
2238 nir_builder_instr_insert(&b->nb, &atomic->instr);
2239 }
2240
2241 static nir_alu_instr *
2242 create_vec(nir_shader *shader, unsigned num_components, unsigned bit_size)
2243 {
2244 nir_op op;
2245 switch (num_components) {
2246 case 1: op = nir_op_fmov; break;
2247 case 2: op = nir_op_vec2; break;
2248 case 3: op = nir_op_vec3; break;
2249 case 4: op = nir_op_vec4; break;
2250 default: unreachable("bad vector size");
2251 }
2252
2253 nir_alu_instr *vec = nir_alu_instr_create(shader, op);
2254 nir_ssa_dest_init(&vec->instr, &vec->dest.dest, num_components,
2255 bit_size, NULL);
2256 vec->dest.write_mask = (1 << num_components) - 1;
2257
2258 return vec;
2259 }
2260
2261 struct vtn_ssa_value *
2262 vtn_ssa_transpose(struct vtn_builder *b, struct vtn_ssa_value *src)
2263 {
2264 if (src->transposed)
2265 return src->transposed;
2266
2267 struct vtn_ssa_value *dest =
2268 vtn_create_ssa_value(b, glsl_transposed_type(src->type));
2269
2270 for (unsigned i = 0; i < glsl_get_matrix_columns(dest->type); i++) {
2271 nir_alu_instr *vec = create_vec(b->shader,
2272 glsl_get_matrix_columns(src->type),
2273 glsl_get_bit_size(src->type));
2274 if (glsl_type_is_vector_or_scalar(src->type)) {
2275 vec->src[0].src = nir_src_for_ssa(src->def);
2276 vec->src[0].swizzle[0] = i;
2277 } else {
2278 for (unsigned j = 0; j < glsl_get_matrix_columns(src->type); j++) {
2279 vec->src[j].src = nir_src_for_ssa(src->elems[j]->def);
2280 vec->src[j].swizzle[0] = i;
2281 }
2282 }
2283 nir_builder_instr_insert(&b->nb, &vec->instr);
2284 dest->elems[i]->def = &vec->dest.dest.ssa;
2285 }
2286
2287 dest->transposed = src;
2288
2289 return dest;
2290 }
2291
2292 nir_ssa_def *
2293 vtn_vector_extract(struct vtn_builder *b, nir_ssa_def *src, unsigned index)
2294 {
2295 unsigned swiz[4] = { index };
2296 return nir_swizzle(&b->nb, src, swiz, 1, true);
2297 }
2298
2299 nir_ssa_def *
2300 vtn_vector_insert(struct vtn_builder *b, nir_ssa_def *src, nir_ssa_def *insert,
2301 unsigned index)
2302 {
2303 nir_alu_instr *vec = create_vec(b->shader, src->num_components,
2304 src->bit_size);
2305
2306 for (unsigned i = 0; i < src->num_components; i++) {
2307 if (i == index) {
2308 vec->src[i].src = nir_src_for_ssa(insert);
2309 } else {
2310 vec->src[i].src = nir_src_for_ssa(src);
2311 vec->src[i].swizzle[0] = i;
2312 }
2313 }
2314
2315 nir_builder_instr_insert(&b->nb, &vec->instr);
2316
2317 return &vec->dest.dest.ssa;
2318 }
2319
2320 nir_ssa_def *
2321 vtn_vector_extract_dynamic(struct vtn_builder *b, nir_ssa_def *src,
2322 nir_ssa_def *index)
2323 {
2324 nir_ssa_def *dest = vtn_vector_extract(b, src, 0);
2325 for (unsigned i = 1; i < src->num_components; i++)
2326 dest = nir_bcsel(&b->nb, nir_ieq(&b->nb, index, nir_imm_int(&b->nb, i)),
2327 vtn_vector_extract(b, src, i), dest);
2328
2329 return dest;
2330 }
2331
2332 nir_ssa_def *
2333 vtn_vector_insert_dynamic(struct vtn_builder *b, nir_ssa_def *src,
2334 nir_ssa_def *insert, nir_ssa_def *index)
2335 {
2336 nir_ssa_def *dest = vtn_vector_insert(b, src, insert, 0);
2337 for (unsigned i = 1; i < src->num_components; i++)
2338 dest = nir_bcsel(&b->nb, nir_ieq(&b->nb, index, nir_imm_int(&b->nb, i)),
2339 vtn_vector_insert(b, src, insert, i), dest);
2340
2341 return dest;
2342 }
2343
2344 static nir_ssa_def *
2345 vtn_vector_shuffle(struct vtn_builder *b, unsigned num_components,
2346 nir_ssa_def *src0, nir_ssa_def *src1,
2347 const uint32_t *indices)
2348 {
2349 nir_alu_instr *vec = create_vec(b->shader, num_components, src0->bit_size);
2350
2351 for (unsigned i = 0; i < num_components; i++) {
2352 uint32_t index = indices[i];
2353 if (index == 0xffffffff) {
2354 vec->src[i].src =
2355 nir_src_for_ssa(nir_ssa_undef(&b->nb, 1, src0->bit_size));
2356 } else if (index < src0->num_components) {
2357 vec->src[i].src = nir_src_for_ssa(src0);
2358 vec->src[i].swizzle[0] = index;
2359 } else {
2360 vec->src[i].src = nir_src_for_ssa(src1);
2361 vec->src[i].swizzle[0] = index - src0->num_components;
2362 }
2363 }
2364
2365 nir_builder_instr_insert(&b->nb, &vec->instr);
2366
2367 return &vec->dest.dest.ssa;
2368 }
2369
2370 /*
2371 * Concatentates a number of vectors/scalars together to produce a vector
2372 */
2373 static nir_ssa_def *
2374 vtn_vector_construct(struct vtn_builder *b, unsigned num_components,
2375 unsigned num_srcs, nir_ssa_def **srcs)
2376 {
2377 nir_alu_instr *vec = create_vec(b->shader, num_components,
2378 srcs[0]->bit_size);
2379
2380 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
2381 *
2382 * "When constructing a vector, there must be at least two Constituent
2383 * operands."
2384 */
2385 assert(num_srcs >= 2);
2386
2387 unsigned dest_idx = 0;
2388 for (unsigned i = 0; i < num_srcs; i++) {
2389 nir_ssa_def *src = srcs[i];
2390 assert(dest_idx + src->num_components <= num_components);
2391 for (unsigned j = 0; j < src->num_components; j++) {
2392 vec->src[dest_idx].src = nir_src_for_ssa(src);
2393 vec->src[dest_idx].swizzle[0] = j;
2394 dest_idx++;
2395 }
2396 }
2397
2398 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
2399 *
2400 * "When constructing a vector, the total number of components in all
2401 * the operands must equal the number of components in Result Type."
2402 */
2403 assert(dest_idx == num_components);
2404
2405 nir_builder_instr_insert(&b->nb, &vec->instr);
2406
2407 return &vec->dest.dest.ssa;
2408 }
2409
2410 static struct vtn_ssa_value *
2411 vtn_composite_copy(void *mem_ctx, struct vtn_ssa_value *src)
2412 {
2413 struct vtn_ssa_value *dest = rzalloc(mem_ctx, struct vtn_ssa_value);
2414 dest->type = src->type;
2415
2416 if (glsl_type_is_vector_or_scalar(src->type)) {
2417 dest->def = src->def;
2418 } else {
2419 unsigned elems = glsl_get_length(src->type);
2420
2421 dest->elems = ralloc_array(mem_ctx, struct vtn_ssa_value *, elems);
2422 for (unsigned i = 0; i < elems; i++)
2423 dest->elems[i] = vtn_composite_copy(mem_ctx, src->elems[i]);
2424 }
2425
2426 return dest;
2427 }
2428
2429 static struct vtn_ssa_value *
2430 vtn_composite_insert(struct vtn_builder *b, struct vtn_ssa_value *src,
2431 struct vtn_ssa_value *insert, const uint32_t *indices,
2432 unsigned num_indices)
2433 {
2434 struct vtn_ssa_value *dest = vtn_composite_copy(b, src);
2435
2436 struct vtn_ssa_value *cur = dest;
2437 unsigned i;
2438 for (i = 0; i < num_indices - 1; i++) {
2439 cur = cur->elems[indices[i]];
2440 }
2441
2442 if (glsl_type_is_vector_or_scalar(cur->type)) {
2443 /* According to the SPIR-V spec, OpCompositeInsert may work down to
2444 * the component granularity. In that case, the last index will be
2445 * the index to insert the scalar into the vector.
2446 */
2447
2448 cur->def = vtn_vector_insert(b, cur->def, insert->def, indices[i]);
2449 } else {
2450 cur->elems[indices[i]] = insert;
2451 }
2452
2453 return dest;
2454 }
2455
2456 static struct vtn_ssa_value *
2457 vtn_composite_extract(struct vtn_builder *b, struct vtn_ssa_value *src,
2458 const uint32_t *indices, unsigned num_indices)
2459 {
2460 struct vtn_ssa_value *cur = src;
2461 for (unsigned i = 0; i < num_indices; i++) {
2462 if (glsl_type_is_vector_or_scalar(cur->type)) {
2463 assert(i == num_indices - 1);
2464 /* According to the SPIR-V spec, OpCompositeExtract may work down to
2465 * the component granularity. The last index will be the index of the
2466 * vector to extract.
2467 */
2468
2469 struct vtn_ssa_value *ret = rzalloc(b, struct vtn_ssa_value);
2470 ret->type = glsl_scalar_type(glsl_get_base_type(cur->type));
2471 ret->def = vtn_vector_extract(b, cur->def, indices[i]);
2472 return ret;
2473 } else {
2474 cur = cur->elems[indices[i]];
2475 }
2476 }
2477
2478 return cur;
2479 }
2480
2481 static void
2482 vtn_handle_composite(struct vtn_builder *b, SpvOp opcode,
2483 const uint32_t *w, unsigned count)
2484 {
2485 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
2486 const struct glsl_type *type =
2487 vtn_value(b, w[1], vtn_value_type_type)->type->type;
2488 val->ssa = vtn_create_ssa_value(b, type);
2489
2490 switch (opcode) {
2491 case SpvOpVectorExtractDynamic:
2492 val->ssa->def = vtn_vector_extract_dynamic(b, vtn_ssa_value(b, w[3])->def,
2493 vtn_ssa_value(b, w[4])->def);
2494 break;
2495
2496 case SpvOpVectorInsertDynamic:
2497 val->ssa->def = vtn_vector_insert_dynamic(b, vtn_ssa_value(b, w[3])->def,
2498 vtn_ssa_value(b, w[4])->def,
2499 vtn_ssa_value(b, w[5])->def);
2500 break;
2501
2502 case SpvOpVectorShuffle:
2503 val->ssa->def = vtn_vector_shuffle(b, glsl_get_vector_elements(type),
2504 vtn_ssa_value(b, w[3])->def,
2505 vtn_ssa_value(b, w[4])->def,
2506 w + 5);
2507 break;
2508
2509 case SpvOpCompositeConstruct: {
2510 unsigned elems = count - 3;
2511 if (glsl_type_is_vector_or_scalar(type)) {
2512 nir_ssa_def *srcs[4];
2513 for (unsigned i = 0; i < elems; i++)
2514 srcs[i] = vtn_ssa_value(b, w[3 + i])->def;
2515 val->ssa->def =
2516 vtn_vector_construct(b, glsl_get_vector_elements(type),
2517 elems, srcs);
2518 } else {
2519 val->ssa->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
2520 for (unsigned i = 0; i < elems; i++)
2521 val->ssa->elems[i] = vtn_ssa_value(b, w[3 + i]);
2522 }
2523 break;
2524 }
2525 case SpvOpCompositeExtract:
2526 val->ssa = vtn_composite_extract(b, vtn_ssa_value(b, w[3]),
2527 w + 4, count - 4);
2528 break;
2529
2530 case SpvOpCompositeInsert:
2531 val->ssa = vtn_composite_insert(b, vtn_ssa_value(b, w[4]),
2532 vtn_ssa_value(b, w[3]),
2533 w + 5, count - 5);
2534 break;
2535
2536 case SpvOpCopyObject:
2537 val->ssa = vtn_composite_copy(b, vtn_ssa_value(b, w[3]));
2538 break;
2539
2540 default:
2541 unreachable("unknown composite operation");
2542 }
2543 }
2544
2545 static void
2546 vtn_handle_barrier(struct vtn_builder *b, SpvOp opcode,
2547 const uint32_t *w, unsigned count)
2548 {
2549 nir_intrinsic_op intrinsic_op;
2550 switch (opcode) {
2551 case SpvOpEmitVertex:
2552 case SpvOpEmitStreamVertex:
2553 intrinsic_op = nir_intrinsic_emit_vertex;
2554 break;
2555 case SpvOpEndPrimitive:
2556 case SpvOpEndStreamPrimitive:
2557 intrinsic_op = nir_intrinsic_end_primitive;
2558 break;
2559 case SpvOpMemoryBarrier:
2560 intrinsic_op = nir_intrinsic_memory_barrier;
2561 break;
2562 case SpvOpControlBarrier:
2563 intrinsic_op = nir_intrinsic_barrier;
2564 break;
2565 default:
2566 unreachable("unknown barrier instruction");
2567 }
2568
2569 nir_intrinsic_instr *intrin =
2570 nir_intrinsic_instr_create(b->shader, intrinsic_op);
2571
2572 if (opcode == SpvOpEmitStreamVertex || opcode == SpvOpEndStreamPrimitive)
2573 nir_intrinsic_set_stream_id(intrin, w[1]);
2574
2575 nir_builder_instr_insert(&b->nb, &intrin->instr);
2576 }
2577
2578 static unsigned
2579 gl_primitive_from_spv_execution_mode(SpvExecutionMode mode)
2580 {
2581 switch (mode) {
2582 case SpvExecutionModeInputPoints:
2583 case SpvExecutionModeOutputPoints:
2584 return 0; /* GL_POINTS */
2585 case SpvExecutionModeInputLines:
2586 return 1; /* GL_LINES */
2587 case SpvExecutionModeInputLinesAdjacency:
2588 return 0x000A; /* GL_LINE_STRIP_ADJACENCY_ARB */
2589 case SpvExecutionModeTriangles:
2590 return 4; /* GL_TRIANGLES */
2591 case SpvExecutionModeInputTrianglesAdjacency:
2592 return 0x000C; /* GL_TRIANGLES_ADJACENCY_ARB */
2593 case SpvExecutionModeQuads:
2594 return 7; /* GL_QUADS */
2595 case SpvExecutionModeIsolines:
2596 return 0x8E7A; /* GL_ISOLINES */
2597 case SpvExecutionModeOutputLineStrip:
2598 return 3; /* GL_LINE_STRIP */
2599 case SpvExecutionModeOutputTriangleStrip:
2600 return 5; /* GL_TRIANGLE_STRIP */
2601 default:
2602 assert(!"Invalid primitive type");
2603 return 4;
2604 }
2605 }
2606
2607 static unsigned
2608 vertices_in_from_spv_execution_mode(SpvExecutionMode mode)
2609 {
2610 switch (mode) {
2611 case SpvExecutionModeInputPoints:
2612 return 1;
2613 case SpvExecutionModeInputLines:
2614 return 2;
2615 case SpvExecutionModeInputLinesAdjacency:
2616 return 4;
2617 case SpvExecutionModeTriangles:
2618 return 3;
2619 case SpvExecutionModeInputTrianglesAdjacency:
2620 return 6;
2621 default:
2622 assert(!"Invalid GS input mode");
2623 return 0;
2624 }
2625 }
2626
2627 static gl_shader_stage
2628 stage_for_execution_model(SpvExecutionModel model)
2629 {
2630 switch (model) {
2631 case SpvExecutionModelVertex:
2632 return MESA_SHADER_VERTEX;
2633 case SpvExecutionModelTessellationControl:
2634 return MESA_SHADER_TESS_CTRL;
2635 case SpvExecutionModelTessellationEvaluation:
2636 return MESA_SHADER_TESS_EVAL;
2637 case SpvExecutionModelGeometry:
2638 return MESA_SHADER_GEOMETRY;
2639 case SpvExecutionModelFragment:
2640 return MESA_SHADER_FRAGMENT;
2641 case SpvExecutionModelGLCompute:
2642 return MESA_SHADER_COMPUTE;
2643 default:
2644 unreachable("Unsupported execution model");
2645 }
2646 }
2647
2648 #define spv_check_supported(name, cap) do { \
2649 if (!(b->ext && b->ext->name)) \
2650 vtn_warn("Unsupported SPIR-V capability: %s", \
2651 spirv_capability_to_string(cap)); \
2652 } while(0)
2653
2654 static bool
2655 vtn_handle_preamble_instruction(struct vtn_builder *b, SpvOp opcode,
2656 const uint32_t *w, unsigned count)
2657 {
2658 switch (opcode) {
2659 case SpvOpSource:
2660 case SpvOpSourceExtension:
2661 case SpvOpSourceContinued:
2662 case SpvOpExtension:
2663 /* Unhandled, but these are for debug so that's ok. */
2664 break;
2665
2666 case SpvOpCapability: {
2667 SpvCapability cap = w[1];
2668 switch (cap) {
2669 case SpvCapabilityMatrix:
2670 case SpvCapabilityShader:
2671 case SpvCapabilityGeometry:
2672 case SpvCapabilityGeometryPointSize:
2673 case SpvCapabilityUniformBufferArrayDynamicIndexing:
2674 case SpvCapabilitySampledImageArrayDynamicIndexing:
2675 case SpvCapabilityStorageBufferArrayDynamicIndexing:
2676 case SpvCapabilityStorageImageArrayDynamicIndexing:
2677 case SpvCapabilityImageRect:
2678 case SpvCapabilitySampledRect:
2679 case SpvCapabilitySampled1D:
2680 case SpvCapabilityImage1D:
2681 case SpvCapabilitySampledCubeArray:
2682 case SpvCapabilitySampledBuffer:
2683 case SpvCapabilityImageBuffer:
2684 case SpvCapabilityImageQuery:
2685 case SpvCapabilityDerivativeControl:
2686 case SpvCapabilityInterpolationFunction:
2687 case SpvCapabilityMultiViewport:
2688 case SpvCapabilitySampleRateShading:
2689 case SpvCapabilityClipDistance:
2690 case SpvCapabilityCullDistance:
2691 case SpvCapabilityInputAttachment:
2692 case SpvCapabilityImageGatherExtended:
2693 case SpvCapabilityStorageImageExtendedFormats:
2694 break;
2695
2696 case SpvCapabilityGeometryStreams:
2697 case SpvCapabilityLinkage:
2698 case SpvCapabilityVector16:
2699 case SpvCapabilityFloat16Buffer:
2700 case SpvCapabilityFloat16:
2701 case SpvCapabilityInt64Atomics:
2702 case SpvCapabilityAtomicStorage:
2703 case SpvCapabilityInt16:
2704 case SpvCapabilityStorageImageMultisample:
2705 case SpvCapabilityImageCubeArray:
2706 case SpvCapabilityInt8:
2707 case SpvCapabilitySparseResidency:
2708 case SpvCapabilityMinLod:
2709 case SpvCapabilityTransformFeedback:
2710 vtn_warn("Unsupported SPIR-V capability: %s",
2711 spirv_capability_to_string(cap));
2712 break;
2713
2714 case SpvCapabilityFloat64:
2715 spv_check_supported(float64, cap);
2716 break;
2717 case SpvCapabilityInt64:
2718 spv_check_supported(int64, cap);
2719 break;
2720
2721 case SpvCapabilityAddresses:
2722 case SpvCapabilityKernel:
2723 case SpvCapabilityImageBasic:
2724 case SpvCapabilityImageReadWrite:
2725 case SpvCapabilityImageMipmap:
2726 case SpvCapabilityPipes:
2727 case SpvCapabilityGroups:
2728 case SpvCapabilityDeviceEnqueue:
2729 case SpvCapabilityLiteralSampler:
2730 case SpvCapabilityGenericPointer:
2731 vtn_warn("Unsupported OpenCL-style SPIR-V capability: %s",
2732 spirv_capability_to_string(cap));
2733 break;
2734
2735 case SpvCapabilityImageMSArray:
2736 spv_check_supported(image_ms_array, cap);
2737 break;
2738
2739 case SpvCapabilityTessellation:
2740 case SpvCapabilityTessellationPointSize:
2741 spv_check_supported(tessellation, cap);
2742 break;
2743
2744 case SpvCapabilityDrawParameters:
2745 spv_check_supported(draw_parameters, cap);
2746 break;
2747
2748 case SpvCapabilityStorageImageReadWithoutFormat:
2749 spv_check_supported(image_read_without_format, cap);
2750 break;
2751
2752 case SpvCapabilityStorageImageWriteWithoutFormat:
2753 spv_check_supported(image_write_without_format, cap);
2754 break;
2755
2756 case SpvCapabilityMultiView:
2757 spv_check_supported(multiview, cap);
2758 break;
2759
2760 default:
2761 unreachable("Unhandled capability");
2762 }
2763 break;
2764 }
2765
2766 case SpvOpExtInstImport:
2767 vtn_handle_extension(b, opcode, w, count);
2768 break;
2769
2770 case SpvOpMemoryModel:
2771 assert(w[1] == SpvAddressingModelLogical);
2772 assert(w[2] == SpvMemoryModelGLSL450);
2773 break;
2774
2775 case SpvOpEntryPoint: {
2776 struct vtn_value *entry_point = &b->values[w[2]];
2777 /* Let this be a name label regardless */
2778 unsigned name_words;
2779 entry_point->name = vtn_string_literal(b, &w[3], count - 3, &name_words);
2780
2781 if (strcmp(entry_point->name, b->entry_point_name) != 0 ||
2782 stage_for_execution_model(w[1]) != b->entry_point_stage)
2783 break;
2784
2785 assert(b->entry_point == NULL);
2786 b->entry_point = entry_point;
2787 break;
2788 }
2789
2790 case SpvOpString:
2791 vtn_push_value(b, w[1], vtn_value_type_string)->str =
2792 vtn_string_literal(b, &w[2], count - 2, NULL);
2793 break;
2794
2795 case SpvOpName:
2796 b->values[w[1]].name = vtn_string_literal(b, &w[2], count - 2, NULL);
2797 break;
2798
2799 case SpvOpMemberName:
2800 /* TODO */
2801 break;
2802
2803 case SpvOpExecutionMode:
2804 case SpvOpDecorationGroup:
2805 case SpvOpDecorate:
2806 case SpvOpMemberDecorate:
2807 case SpvOpGroupDecorate:
2808 case SpvOpGroupMemberDecorate:
2809 vtn_handle_decoration(b, opcode, w, count);
2810 break;
2811
2812 default:
2813 return false; /* End of preamble */
2814 }
2815
2816 return true;
2817 }
2818
2819 static void
2820 vtn_handle_execution_mode(struct vtn_builder *b, struct vtn_value *entry_point,
2821 const struct vtn_decoration *mode, void *data)
2822 {
2823 assert(b->entry_point == entry_point);
2824
2825 switch(mode->exec_mode) {
2826 case SpvExecutionModeOriginUpperLeft:
2827 case SpvExecutionModeOriginLowerLeft:
2828 b->origin_upper_left =
2829 (mode->exec_mode == SpvExecutionModeOriginUpperLeft);
2830 break;
2831
2832 case SpvExecutionModeEarlyFragmentTests:
2833 assert(b->shader->stage == MESA_SHADER_FRAGMENT);
2834 b->shader->info.fs.early_fragment_tests = true;
2835 break;
2836
2837 case SpvExecutionModeInvocations:
2838 assert(b->shader->stage == MESA_SHADER_GEOMETRY);
2839 b->shader->info.gs.invocations = MAX2(1, mode->literals[0]);
2840 break;
2841
2842 case SpvExecutionModeDepthReplacing:
2843 assert(b->shader->stage == MESA_SHADER_FRAGMENT);
2844 b->shader->info.fs.depth_layout = FRAG_DEPTH_LAYOUT_ANY;
2845 break;
2846 case SpvExecutionModeDepthGreater:
2847 assert(b->shader->stage == MESA_SHADER_FRAGMENT);
2848 b->shader->info.fs.depth_layout = FRAG_DEPTH_LAYOUT_GREATER;
2849 break;
2850 case SpvExecutionModeDepthLess:
2851 assert(b->shader->stage == MESA_SHADER_FRAGMENT);
2852 b->shader->info.fs.depth_layout = FRAG_DEPTH_LAYOUT_LESS;
2853 break;
2854 case SpvExecutionModeDepthUnchanged:
2855 assert(b->shader->stage == MESA_SHADER_FRAGMENT);
2856 b->shader->info.fs.depth_layout = FRAG_DEPTH_LAYOUT_UNCHANGED;
2857 break;
2858
2859 case SpvExecutionModeLocalSize:
2860 assert(b->shader->stage == MESA_SHADER_COMPUTE);
2861 b->shader->info.cs.local_size[0] = mode->literals[0];
2862 b->shader->info.cs.local_size[1] = mode->literals[1];
2863 b->shader->info.cs.local_size[2] = mode->literals[2];
2864 break;
2865 case SpvExecutionModeLocalSizeHint:
2866 break; /* Nothing to do with this */
2867
2868 case SpvExecutionModeOutputVertices:
2869 if (b->shader->stage == MESA_SHADER_TESS_CTRL ||
2870 b->shader->stage == MESA_SHADER_TESS_EVAL) {
2871 b->shader->info.tess.tcs_vertices_out = mode->literals[0];
2872 } else {
2873 assert(b->shader->stage == MESA_SHADER_GEOMETRY);
2874 b->shader->info.gs.vertices_out = mode->literals[0];
2875 }
2876 break;
2877
2878 case SpvExecutionModeInputPoints:
2879 case SpvExecutionModeInputLines:
2880 case SpvExecutionModeInputLinesAdjacency:
2881 case SpvExecutionModeTriangles:
2882 case SpvExecutionModeInputTrianglesAdjacency:
2883 case SpvExecutionModeQuads:
2884 case SpvExecutionModeIsolines:
2885 if (b->shader->stage == MESA_SHADER_TESS_CTRL ||
2886 b->shader->stage == MESA_SHADER_TESS_EVAL) {
2887 b->shader->info.tess.primitive_mode =
2888 gl_primitive_from_spv_execution_mode(mode->exec_mode);
2889 } else {
2890 assert(b->shader->stage == MESA_SHADER_GEOMETRY);
2891 b->shader->info.gs.vertices_in =
2892 vertices_in_from_spv_execution_mode(mode->exec_mode);
2893 }
2894 break;
2895
2896 case SpvExecutionModeOutputPoints:
2897 case SpvExecutionModeOutputLineStrip:
2898 case SpvExecutionModeOutputTriangleStrip:
2899 assert(b->shader->stage == MESA_SHADER_GEOMETRY);
2900 b->shader->info.gs.output_primitive =
2901 gl_primitive_from_spv_execution_mode(mode->exec_mode);
2902 break;
2903
2904 case SpvExecutionModeSpacingEqual:
2905 assert(b->shader->stage == MESA_SHADER_TESS_CTRL ||
2906 b->shader->stage == MESA_SHADER_TESS_EVAL);
2907 b->shader->info.tess.spacing = TESS_SPACING_EQUAL;
2908 break;
2909 case SpvExecutionModeSpacingFractionalEven:
2910 assert(b->shader->stage == MESA_SHADER_TESS_CTRL ||
2911 b->shader->stage == MESA_SHADER_TESS_EVAL);
2912 b->shader->info.tess.spacing = TESS_SPACING_FRACTIONAL_EVEN;
2913 break;
2914 case SpvExecutionModeSpacingFractionalOdd:
2915 assert(b->shader->stage == MESA_SHADER_TESS_CTRL ||
2916 b->shader->stage == MESA_SHADER_TESS_EVAL);
2917 b->shader->info.tess.spacing = TESS_SPACING_FRACTIONAL_ODD;
2918 break;
2919 case SpvExecutionModeVertexOrderCw:
2920 assert(b->shader->stage == MESA_SHADER_TESS_CTRL ||
2921 b->shader->stage == MESA_SHADER_TESS_EVAL);
2922 /* Vulkan's notion of CCW seems to match the hardware backends,
2923 * but be the opposite of OpenGL. Currently NIR follows GL semantics,
2924 * so we set it backwards here.
2925 */
2926 b->shader->info.tess.ccw = true;
2927 break;
2928 case SpvExecutionModeVertexOrderCcw:
2929 assert(b->shader->stage == MESA_SHADER_TESS_CTRL ||
2930 b->shader->stage == MESA_SHADER_TESS_EVAL);
2931 /* Backwards; see above */
2932 b->shader->info.tess.ccw = false;
2933 break;
2934 case SpvExecutionModePointMode:
2935 assert(b->shader->stage == MESA_SHADER_TESS_CTRL ||
2936 b->shader->stage == MESA_SHADER_TESS_EVAL);
2937 b->shader->info.tess.point_mode = true;
2938 break;
2939
2940 case SpvExecutionModePixelCenterInteger:
2941 b->pixel_center_integer = true;
2942 break;
2943
2944 case SpvExecutionModeXfb:
2945 assert(!"Unhandled execution mode");
2946 break;
2947
2948 case SpvExecutionModeVecTypeHint:
2949 case SpvExecutionModeContractionOff:
2950 break; /* OpenCL */
2951
2952 default:
2953 unreachable("Unhandled execution mode");
2954 }
2955 }
2956
2957 static bool
2958 vtn_handle_variable_or_type_instruction(struct vtn_builder *b, SpvOp opcode,
2959 const uint32_t *w, unsigned count)
2960 {
2961 switch (opcode) {
2962 case SpvOpSource:
2963 case SpvOpSourceContinued:
2964 case SpvOpSourceExtension:
2965 case SpvOpExtension:
2966 case SpvOpCapability:
2967 case SpvOpExtInstImport:
2968 case SpvOpMemoryModel:
2969 case SpvOpEntryPoint:
2970 case SpvOpExecutionMode:
2971 case SpvOpString:
2972 case SpvOpName:
2973 case SpvOpMemberName:
2974 case SpvOpDecorationGroup:
2975 case SpvOpDecorate:
2976 case SpvOpMemberDecorate:
2977 case SpvOpGroupDecorate:
2978 case SpvOpGroupMemberDecorate:
2979 assert(!"Invalid opcode types and variables section");
2980 break;
2981
2982 case SpvOpTypeVoid:
2983 case SpvOpTypeBool:
2984 case SpvOpTypeInt:
2985 case SpvOpTypeFloat:
2986 case SpvOpTypeVector:
2987 case SpvOpTypeMatrix:
2988 case SpvOpTypeImage:
2989 case SpvOpTypeSampler:
2990 case SpvOpTypeSampledImage:
2991 case SpvOpTypeArray:
2992 case SpvOpTypeRuntimeArray:
2993 case SpvOpTypeStruct:
2994 case SpvOpTypeOpaque:
2995 case SpvOpTypePointer:
2996 case SpvOpTypeFunction:
2997 case SpvOpTypeEvent:
2998 case SpvOpTypeDeviceEvent:
2999 case SpvOpTypeReserveId:
3000 case SpvOpTypeQueue:
3001 case SpvOpTypePipe:
3002 vtn_handle_type(b, opcode, w, count);
3003 break;
3004
3005 case SpvOpConstantTrue:
3006 case SpvOpConstantFalse:
3007 case SpvOpConstant:
3008 case SpvOpConstantComposite:
3009 case SpvOpConstantSampler:
3010 case SpvOpConstantNull:
3011 case SpvOpSpecConstantTrue:
3012 case SpvOpSpecConstantFalse:
3013 case SpvOpSpecConstant:
3014 case SpvOpSpecConstantComposite:
3015 case SpvOpSpecConstantOp:
3016 vtn_handle_constant(b, opcode, w, count);
3017 break;
3018
3019 case SpvOpUndef:
3020 case SpvOpVariable:
3021 vtn_handle_variables(b, opcode, w, count);
3022 break;
3023
3024 default:
3025 return false; /* End of preamble */
3026 }
3027
3028 return true;
3029 }
3030
3031 static bool
3032 vtn_handle_body_instruction(struct vtn_builder *b, SpvOp opcode,
3033 const uint32_t *w, unsigned count)
3034 {
3035 switch (opcode) {
3036 case SpvOpLabel:
3037 break;
3038
3039 case SpvOpLoopMerge:
3040 case SpvOpSelectionMerge:
3041 /* This is handled by cfg pre-pass and walk_blocks */
3042 break;
3043
3044 case SpvOpUndef: {
3045 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_undef);
3046 val->type = vtn_value(b, w[1], vtn_value_type_type)->type;
3047 break;
3048 }
3049
3050 case SpvOpExtInst:
3051 vtn_handle_extension(b, opcode, w, count);
3052 break;
3053
3054 case SpvOpVariable:
3055 case SpvOpLoad:
3056 case SpvOpStore:
3057 case SpvOpCopyMemory:
3058 case SpvOpCopyMemorySized:
3059 case SpvOpAccessChain:
3060 case SpvOpInBoundsAccessChain:
3061 case SpvOpArrayLength:
3062 vtn_handle_variables(b, opcode, w, count);
3063 break;
3064
3065 case SpvOpFunctionCall:
3066 vtn_handle_function_call(b, opcode, w, count);
3067 break;
3068
3069 case SpvOpSampledImage:
3070 case SpvOpImage:
3071 case SpvOpImageSampleImplicitLod:
3072 case SpvOpImageSampleExplicitLod:
3073 case SpvOpImageSampleDrefImplicitLod:
3074 case SpvOpImageSampleDrefExplicitLod:
3075 case SpvOpImageSampleProjImplicitLod:
3076 case SpvOpImageSampleProjExplicitLod:
3077 case SpvOpImageSampleProjDrefImplicitLod:
3078 case SpvOpImageSampleProjDrefExplicitLod:
3079 case SpvOpImageFetch:
3080 case SpvOpImageGather:
3081 case SpvOpImageDrefGather:
3082 case SpvOpImageQuerySizeLod:
3083 case SpvOpImageQueryLod:
3084 case SpvOpImageQueryLevels:
3085 case SpvOpImageQuerySamples:
3086 vtn_handle_texture(b, opcode, w, count);
3087 break;
3088
3089 case SpvOpImageRead:
3090 case SpvOpImageWrite:
3091 case SpvOpImageTexelPointer:
3092 vtn_handle_image(b, opcode, w, count);
3093 break;
3094
3095 case SpvOpImageQuerySize: {
3096 struct vtn_pointer *image =
3097 vtn_value(b, w[3], vtn_value_type_pointer)->pointer;
3098 if (image->mode == vtn_variable_mode_image) {
3099 vtn_handle_image(b, opcode, w, count);
3100 } else {
3101 assert(image->mode == vtn_variable_mode_sampler);
3102 vtn_handle_texture(b, opcode, w, count);
3103 }
3104 break;
3105 }
3106
3107 case SpvOpAtomicLoad:
3108 case SpvOpAtomicExchange:
3109 case SpvOpAtomicCompareExchange:
3110 case SpvOpAtomicCompareExchangeWeak:
3111 case SpvOpAtomicIIncrement:
3112 case SpvOpAtomicIDecrement:
3113 case SpvOpAtomicIAdd:
3114 case SpvOpAtomicISub:
3115 case SpvOpAtomicSMin:
3116 case SpvOpAtomicUMin:
3117 case SpvOpAtomicSMax:
3118 case SpvOpAtomicUMax:
3119 case SpvOpAtomicAnd:
3120 case SpvOpAtomicOr:
3121 case SpvOpAtomicXor: {
3122 struct vtn_value *pointer = vtn_untyped_value(b, w[3]);
3123 if (pointer->value_type == vtn_value_type_image_pointer) {
3124 vtn_handle_image(b, opcode, w, count);
3125 } else {
3126 assert(pointer->value_type == vtn_value_type_pointer);
3127 vtn_handle_ssbo_or_shared_atomic(b, opcode, w, count);
3128 }
3129 break;
3130 }
3131
3132 case SpvOpAtomicStore: {
3133 struct vtn_value *pointer = vtn_untyped_value(b, w[1]);
3134 if (pointer->value_type == vtn_value_type_image_pointer) {
3135 vtn_handle_image(b, opcode, w, count);
3136 } else {
3137 assert(pointer->value_type == vtn_value_type_pointer);
3138 vtn_handle_ssbo_or_shared_atomic(b, opcode, w, count);
3139 }
3140 break;
3141 }
3142
3143 case SpvOpSNegate:
3144 case SpvOpFNegate:
3145 case SpvOpNot:
3146 case SpvOpAny:
3147 case SpvOpAll:
3148 case SpvOpConvertFToU:
3149 case SpvOpConvertFToS:
3150 case SpvOpConvertSToF:
3151 case SpvOpConvertUToF:
3152 case SpvOpUConvert:
3153 case SpvOpSConvert:
3154 case SpvOpFConvert:
3155 case SpvOpQuantizeToF16:
3156 case SpvOpConvertPtrToU:
3157 case SpvOpConvertUToPtr:
3158 case SpvOpPtrCastToGeneric:
3159 case SpvOpGenericCastToPtr:
3160 case SpvOpBitcast:
3161 case SpvOpIsNan:
3162 case SpvOpIsInf:
3163 case SpvOpIsFinite:
3164 case SpvOpIsNormal:
3165 case SpvOpSignBitSet:
3166 case SpvOpLessOrGreater:
3167 case SpvOpOrdered:
3168 case SpvOpUnordered:
3169 case SpvOpIAdd:
3170 case SpvOpFAdd:
3171 case SpvOpISub:
3172 case SpvOpFSub:
3173 case SpvOpIMul:
3174 case SpvOpFMul:
3175 case SpvOpUDiv:
3176 case SpvOpSDiv:
3177 case SpvOpFDiv:
3178 case SpvOpUMod:
3179 case SpvOpSRem:
3180 case SpvOpSMod:
3181 case SpvOpFRem:
3182 case SpvOpFMod:
3183 case SpvOpVectorTimesScalar:
3184 case SpvOpDot:
3185 case SpvOpIAddCarry:
3186 case SpvOpISubBorrow:
3187 case SpvOpUMulExtended:
3188 case SpvOpSMulExtended:
3189 case SpvOpShiftRightLogical:
3190 case SpvOpShiftRightArithmetic:
3191 case SpvOpShiftLeftLogical:
3192 case SpvOpLogicalEqual:
3193 case SpvOpLogicalNotEqual:
3194 case SpvOpLogicalOr:
3195 case SpvOpLogicalAnd:
3196 case SpvOpLogicalNot:
3197 case SpvOpBitwiseOr:
3198 case SpvOpBitwiseXor:
3199 case SpvOpBitwiseAnd:
3200 case SpvOpSelect:
3201 case SpvOpIEqual:
3202 case SpvOpFOrdEqual:
3203 case SpvOpFUnordEqual:
3204 case SpvOpINotEqual:
3205 case SpvOpFOrdNotEqual:
3206 case SpvOpFUnordNotEqual:
3207 case SpvOpULessThan:
3208 case SpvOpSLessThan:
3209 case SpvOpFOrdLessThan:
3210 case SpvOpFUnordLessThan:
3211 case SpvOpUGreaterThan:
3212 case SpvOpSGreaterThan:
3213 case SpvOpFOrdGreaterThan:
3214 case SpvOpFUnordGreaterThan:
3215 case SpvOpULessThanEqual:
3216 case SpvOpSLessThanEqual:
3217 case SpvOpFOrdLessThanEqual:
3218 case SpvOpFUnordLessThanEqual:
3219 case SpvOpUGreaterThanEqual:
3220 case SpvOpSGreaterThanEqual:
3221 case SpvOpFOrdGreaterThanEqual:
3222 case SpvOpFUnordGreaterThanEqual:
3223 case SpvOpDPdx:
3224 case SpvOpDPdy:
3225 case SpvOpFwidth:
3226 case SpvOpDPdxFine:
3227 case SpvOpDPdyFine:
3228 case SpvOpFwidthFine:
3229 case SpvOpDPdxCoarse:
3230 case SpvOpDPdyCoarse:
3231 case SpvOpFwidthCoarse:
3232 case SpvOpBitFieldInsert:
3233 case SpvOpBitFieldSExtract:
3234 case SpvOpBitFieldUExtract:
3235 case SpvOpBitReverse:
3236 case SpvOpBitCount:
3237 case SpvOpTranspose:
3238 case SpvOpOuterProduct:
3239 case SpvOpMatrixTimesScalar:
3240 case SpvOpVectorTimesMatrix:
3241 case SpvOpMatrixTimesVector:
3242 case SpvOpMatrixTimesMatrix:
3243 vtn_handle_alu(b, opcode, w, count);
3244 break;
3245
3246 case SpvOpVectorExtractDynamic:
3247 case SpvOpVectorInsertDynamic:
3248 case SpvOpVectorShuffle:
3249 case SpvOpCompositeConstruct:
3250 case SpvOpCompositeExtract:
3251 case SpvOpCompositeInsert:
3252 case SpvOpCopyObject:
3253 vtn_handle_composite(b, opcode, w, count);
3254 break;
3255
3256 case SpvOpEmitVertex:
3257 case SpvOpEndPrimitive:
3258 case SpvOpEmitStreamVertex:
3259 case SpvOpEndStreamPrimitive:
3260 case SpvOpControlBarrier:
3261 case SpvOpMemoryBarrier:
3262 vtn_handle_barrier(b, opcode, w, count);
3263 break;
3264
3265 default:
3266 unreachable("Unhandled opcode");
3267 }
3268
3269 return true;
3270 }
3271
3272 nir_function *
3273 spirv_to_nir(const uint32_t *words, size_t word_count,
3274 struct nir_spirv_specialization *spec, unsigned num_spec,
3275 gl_shader_stage stage, const char *entry_point_name,
3276 const struct nir_spirv_supported_extensions *ext,
3277 const nir_shader_compiler_options *options)
3278 {
3279 const uint32_t *word_end = words + word_count;
3280
3281 /* Handle the SPIR-V header (first 4 dwords) */
3282 assert(word_count > 5);
3283
3284 assert(words[0] == SpvMagicNumber);
3285 assert(words[1] >= 0x10000);
3286 /* words[2] == generator magic */
3287 unsigned value_id_bound = words[3];
3288 assert(words[4] == 0);
3289
3290 words+= 5;
3291
3292 /* Initialize the stn_builder object */
3293 struct vtn_builder *b = rzalloc(NULL, struct vtn_builder);
3294 b->value_id_bound = value_id_bound;
3295 b->values = rzalloc_array(b, struct vtn_value, value_id_bound);
3296 exec_list_make_empty(&b->functions);
3297 b->entry_point_stage = stage;
3298 b->entry_point_name = entry_point_name;
3299 b->ext = ext;
3300
3301 /* Handle all the preamble instructions */
3302 words = vtn_foreach_instruction(b, words, word_end,
3303 vtn_handle_preamble_instruction);
3304
3305 if (b->entry_point == NULL) {
3306 assert(!"Entry point not found");
3307 ralloc_free(b);
3308 return NULL;
3309 }
3310
3311 b->shader = nir_shader_create(NULL, stage, options, NULL);
3312
3313 /* Set shader info defaults */
3314 b->shader->info.gs.invocations = 1;
3315
3316 /* Parse execution modes */
3317 vtn_foreach_execution_mode(b, b->entry_point,
3318 vtn_handle_execution_mode, NULL);
3319
3320 b->specializations = spec;
3321 b->num_specializations = num_spec;
3322
3323 /* Handle all variable, type, and constant instructions */
3324 words = vtn_foreach_instruction(b, words, word_end,
3325 vtn_handle_variable_or_type_instruction);
3326
3327 vtn_build_cfg(b, words, word_end);
3328
3329 foreach_list_typed(struct vtn_function, func, node, &b->functions) {
3330 b->impl = func->impl;
3331 b->const_table = _mesa_hash_table_create(b, _mesa_hash_pointer,
3332 _mesa_key_pointer_equal);
3333
3334 vtn_function_emit(b, func, vtn_handle_body_instruction);
3335 }
3336
3337 assert(b->entry_point->value_type == vtn_value_type_function);
3338 nir_function *entry_point = b->entry_point->func->impl->function;
3339 assert(entry_point);
3340
3341 ralloc_free(b);
3342
3343 return entry_point;
3344 }