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