nir/spirv: Remove unneeded parameters from pointer_to_offset
[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 offset = vtn_pointer_to_offset(b, ptr, &index, NULL);
2177
2178 nir_intrinsic_op op = get_ssbo_nir_atomic_op(opcode);
2179
2180 atomic = nir_intrinsic_instr_create(b->nb.shader, op);
2181
2182 switch (opcode) {
2183 case SpvOpAtomicLoad:
2184 atomic->num_components = glsl_get_vector_elements(ptr->type->type);
2185 atomic->src[0] = nir_src_for_ssa(index);
2186 atomic->src[1] = nir_src_for_ssa(offset);
2187 break;
2188
2189 case SpvOpAtomicStore:
2190 atomic->num_components = glsl_get_vector_elements(ptr->type->type);
2191 nir_intrinsic_set_write_mask(atomic, (1 << atomic->num_components) - 1);
2192 atomic->src[0] = nir_src_for_ssa(vtn_ssa_value(b, w[4])->def);
2193 atomic->src[1] = nir_src_for_ssa(index);
2194 atomic->src[2] = nir_src_for_ssa(offset);
2195 break;
2196
2197 case SpvOpAtomicExchange:
2198 case SpvOpAtomicCompareExchange:
2199 case SpvOpAtomicCompareExchangeWeak:
2200 case SpvOpAtomicIIncrement:
2201 case SpvOpAtomicIDecrement:
2202 case SpvOpAtomicIAdd:
2203 case SpvOpAtomicISub:
2204 case SpvOpAtomicSMin:
2205 case SpvOpAtomicUMin:
2206 case SpvOpAtomicSMax:
2207 case SpvOpAtomicUMax:
2208 case SpvOpAtomicAnd:
2209 case SpvOpAtomicOr:
2210 case SpvOpAtomicXor:
2211 atomic->src[0] = nir_src_for_ssa(index);
2212 atomic->src[1] = nir_src_for_ssa(offset);
2213 fill_common_atomic_sources(b, opcode, w, &atomic->src[2]);
2214 break;
2215
2216 default:
2217 unreachable("Invalid SPIR-V atomic");
2218 }
2219 }
2220
2221 if (opcode != SpvOpAtomicStore) {
2222 struct vtn_type *type = vtn_value(b, w[1], vtn_value_type_type)->type;
2223
2224 nir_ssa_dest_init(&atomic->instr, &atomic->dest,
2225 glsl_get_vector_elements(type->type),
2226 glsl_get_bit_size(type->type), NULL);
2227
2228 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
2229 val->ssa = rzalloc(b, struct vtn_ssa_value);
2230 val->ssa->def = &atomic->dest.ssa;
2231 val->ssa->type = type->type;
2232 }
2233
2234 nir_builder_instr_insert(&b->nb, &atomic->instr);
2235 }
2236
2237 static nir_alu_instr *
2238 create_vec(nir_shader *shader, unsigned num_components, unsigned bit_size)
2239 {
2240 nir_op op;
2241 switch (num_components) {
2242 case 1: op = nir_op_fmov; break;
2243 case 2: op = nir_op_vec2; break;
2244 case 3: op = nir_op_vec3; break;
2245 case 4: op = nir_op_vec4; break;
2246 default: unreachable("bad vector size");
2247 }
2248
2249 nir_alu_instr *vec = nir_alu_instr_create(shader, op);
2250 nir_ssa_dest_init(&vec->instr, &vec->dest.dest, num_components,
2251 bit_size, NULL);
2252 vec->dest.write_mask = (1 << num_components) - 1;
2253
2254 return vec;
2255 }
2256
2257 struct vtn_ssa_value *
2258 vtn_ssa_transpose(struct vtn_builder *b, struct vtn_ssa_value *src)
2259 {
2260 if (src->transposed)
2261 return src->transposed;
2262
2263 struct vtn_ssa_value *dest =
2264 vtn_create_ssa_value(b, glsl_transposed_type(src->type));
2265
2266 for (unsigned i = 0; i < glsl_get_matrix_columns(dest->type); i++) {
2267 nir_alu_instr *vec = create_vec(b->shader,
2268 glsl_get_matrix_columns(src->type),
2269 glsl_get_bit_size(src->type));
2270 if (glsl_type_is_vector_or_scalar(src->type)) {
2271 vec->src[0].src = nir_src_for_ssa(src->def);
2272 vec->src[0].swizzle[0] = i;
2273 } else {
2274 for (unsigned j = 0; j < glsl_get_matrix_columns(src->type); j++) {
2275 vec->src[j].src = nir_src_for_ssa(src->elems[j]->def);
2276 vec->src[j].swizzle[0] = i;
2277 }
2278 }
2279 nir_builder_instr_insert(&b->nb, &vec->instr);
2280 dest->elems[i]->def = &vec->dest.dest.ssa;
2281 }
2282
2283 dest->transposed = src;
2284
2285 return dest;
2286 }
2287
2288 nir_ssa_def *
2289 vtn_vector_extract(struct vtn_builder *b, nir_ssa_def *src, unsigned index)
2290 {
2291 unsigned swiz[4] = { index };
2292 return nir_swizzle(&b->nb, src, swiz, 1, true);
2293 }
2294
2295 nir_ssa_def *
2296 vtn_vector_insert(struct vtn_builder *b, nir_ssa_def *src, nir_ssa_def *insert,
2297 unsigned index)
2298 {
2299 nir_alu_instr *vec = create_vec(b->shader, src->num_components,
2300 src->bit_size);
2301
2302 for (unsigned i = 0; i < src->num_components; i++) {
2303 if (i == index) {
2304 vec->src[i].src = nir_src_for_ssa(insert);
2305 } else {
2306 vec->src[i].src = nir_src_for_ssa(src);
2307 vec->src[i].swizzle[0] = i;
2308 }
2309 }
2310
2311 nir_builder_instr_insert(&b->nb, &vec->instr);
2312
2313 return &vec->dest.dest.ssa;
2314 }
2315
2316 nir_ssa_def *
2317 vtn_vector_extract_dynamic(struct vtn_builder *b, nir_ssa_def *src,
2318 nir_ssa_def *index)
2319 {
2320 nir_ssa_def *dest = vtn_vector_extract(b, src, 0);
2321 for (unsigned i = 1; i < src->num_components; i++)
2322 dest = nir_bcsel(&b->nb, nir_ieq(&b->nb, index, nir_imm_int(&b->nb, i)),
2323 vtn_vector_extract(b, src, i), dest);
2324
2325 return dest;
2326 }
2327
2328 nir_ssa_def *
2329 vtn_vector_insert_dynamic(struct vtn_builder *b, nir_ssa_def *src,
2330 nir_ssa_def *insert, nir_ssa_def *index)
2331 {
2332 nir_ssa_def *dest = vtn_vector_insert(b, src, insert, 0);
2333 for (unsigned i = 1; i < src->num_components; i++)
2334 dest = nir_bcsel(&b->nb, nir_ieq(&b->nb, index, nir_imm_int(&b->nb, i)),
2335 vtn_vector_insert(b, src, insert, i), dest);
2336
2337 return dest;
2338 }
2339
2340 static nir_ssa_def *
2341 vtn_vector_shuffle(struct vtn_builder *b, unsigned num_components,
2342 nir_ssa_def *src0, nir_ssa_def *src1,
2343 const uint32_t *indices)
2344 {
2345 nir_alu_instr *vec = create_vec(b->shader, num_components, src0->bit_size);
2346
2347 for (unsigned i = 0; i < num_components; i++) {
2348 uint32_t index = indices[i];
2349 if (index == 0xffffffff) {
2350 vec->src[i].src =
2351 nir_src_for_ssa(nir_ssa_undef(&b->nb, 1, src0->bit_size));
2352 } else if (index < src0->num_components) {
2353 vec->src[i].src = nir_src_for_ssa(src0);
2354 vec->src[i].swizzle[0] = index;
2355 } else {
2356 vec->src[i].src = nir_src_for_ssa(src1);
2357 vec->src[i].swizzle[0] = index - src0->num_components;
2358 }
2359 }
2360
2361 nir_builder_instr_insert(&b->nb, &vec->instr);
2362
2363 return &vec->dest.dest.ssa;
2364 }
2365
2366 /*
2367 * Concatentates a number of vectors/scalars together to produce a vector
2368 */
2369 static nir_ssa_def *
2370 vtn_vector_construct(struct vtn_builder *b, unsigned num_components,
2371 unsigned num_srcs, nir_ssa_def **srcs)
2372 {
2373 nir_alu_instr *vec = create_vec(b->shader, num_components,
2374 srcs[0]->bit_size);
2375
2376 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
2377 *
2378 * "When constructing a vector, there must be at least two Constituent
2379 * operands."
2380 */
2381 assert(num_srcs >= 2);
2382
2383 unsigned dest_idx = 0;
2384 for (unsigned i = 0; i < num_srcs; i++) {
2385 nir_ssa_def *src = srcs[i];
2386 assert(dest_idx + src->num_components <= num_components);
2387 for (unsigned j = 0; j < src->num_components; j++) {
2388 vec->src[dest_idx].src = nir_src_for_ssa(src);
2389 vec->src[dest_idx].swizzle[0] = j;
2390 dest_idx++;
2391 }
2392 }
2393
2394 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
2395 *
2396 * "When constructing a vector, the total number of components in all
2397 * the operands must equal the number of components in Result Type."
2398 */
2399 assert(dest_idx == num_components);
2400
2401 nir_builder_instr_insert(&b->nb, &vec->instr);
2402
2403 return &vec->dest.dest.ssa;
2404 }
2405
2406 static struct vtn_ssa_value *
2407 vtn_composite_copy(void *mem_ctx, struct vtn_ssa_value *src)
2408 {
2409 struct vtn_ssa_value *dest = rzalloc(mem_ctx, struct vtn_ssa_value);
2410 dest->type = src->type;
2411
2412 if (glsl_type_is_vector_or_scalar(src->type)) {
2413 dest->def = src->def;
2414 } else {
2415 unsigned elems = glsl_get_length(src->type);
2416
2417 dest->elems = ralloc_array(mem_ctx, struct vtn_ssa_value *, elems);
2418 for (unsigned i = 0; i < elems; i++)
2419 dest->elems[i] = vtn_composite_copy(mem_ctx, src->elems[i]);
2420 }
2421
2422 return dest;
2423 }
2424
2425 static struct vtn_ssa_value *
2426 vtn_composite_insert(struct vtn_builder *b, struct vtn_ssa_value *src,
2427 struct vtn_ssa_value *insert, const uint32_t *indices,
2428 unsigned num_indices)
2429 {
2430 struct vtn_ssa_value *dest = vtn_composite_copy(b, src);
2431
2432 struct vtn_ssa_value *cur = dest;
2433 unsigned i;
2434 for (i = 0; i < num_indices - 1; i++) {
2435 cur = cur->elems[indices[i]];
2436 }
2437
2438 if (glsl_type_is_vector_or_scalar(cur->type)) {
2439 /* According to the SPIR-V spec, OpCompositeInsert may work down to
2440 * the component granularity. In that case, the last index will be
2441 * the index to insert the scalar into the vector.
2442 */
2443
2444 cur->def = vtn_vector_insert(b, cur->def, insert->def, indices[i]);
2445 } else {
2446 cur->elems[indices[i]] = insert;
2447 }
2448
2449 return dest;
2450 }
2451
2452 static struct vtn_ssa_value *
2453 vtn_composite_extract(struct vtn_builder *b, struct vtn_ssa_value *src,
2454 const uint32_t *indices, unsigned num_indices)
2455 {
2456 struct vtn_ssa_value *cur = src;
2457 for (unsigned i = 0; i < num_indices; i++) {
2458 if (glsl_type_is_vector_or_scalar(cur->type)) {
2459 assert(i == num_indices - 1);
2460 /* According to the SPIR-V spec, OpCompositeExtract may work down to
2461 * the component granularity. The last index will be the index of the
2462 * vector to extract.
2463 */
2464
2465 struct vtn_ssa_value *ret = rzalloc(b, struct vtn_ssa_value);
2466 ret->type = glsl_scalar_type(glsl_get_base_type(cur->type));
2467 ret->def = vtn_vector_extract(b, cur->def, indices[i]);
2468 return ret;
2469 } else {
2470 cur = cur->elems[indices[i]];
2471 }
2472 }
2473
2474 return cur;
2475 }
2476
2477 static void
2478 vtn_handle_composite(struct vtn_builder *b, SpvOp opcode,
2479 const uint32_t *w, unsigned count)
2480 {
2481 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
2482 const struct glsl_type *type =
2483 vtn_value(b, w[1], vtn_value_type_type)->type->type;
2484 val->ssa = vtn_create_ssa_value(b, type);
2485
2486 switch (opcode) {
2487 case SpvOpVectorExtractDynamic:
2488 val->ssa->def = vtn_vector_extract_dynamic(b, vtn_ssa_value(b, w[3])->def,
2489 vtn_ssa_value(b, w[4])->def);
2490 break;
2491
2492 case SpvOpVectorInsertDynamic:
2493 val->ssa->def = vtn_vector_insert_dynamic(b, vtn_ssa_value(b, w[3])->def,
2494 vtn_ssa_value(b, w[4])->def,
2495 vtn_ssa_value(b, w[5])->def);
2496 break;
2497
2498 case SpvOpVectorShuffle:
2499 val->ssa->def = vtn_vector_shuffle(b, glsl_get_vector_elements(type),
2500 vtn_ssa_value(b, w[3])->def,
2501 vtn_ssa_value(b, w[4])->def,
2502 w + 5);
2503 break;
2504
2505 case SpvOpCompositeConstruct: {
2506 unsigned elems = count - 3;
2507 if (glsl_type_is_vector_or_scalar(type)) {
2508 nir_ssa_def *srcs[4];
2509 for (unsigned i = 0; i < elems; i++)
2510 srcs[i] = vtn_ssa_value(b, w[3 + i])->def;
2511 val->ssa->def =
2512 vtn_vector_construct(b, glsl_get_vector_elements(type),
2513 elems, srcs);
2514 } else {
2515 val->ssa->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
2516 for (unsigned i = 0; i < elems; i++)
2517 val->ssa->elems[i] = vtn_ssa_value(b, w[3 + i]);
2518 }
2519 break;
2520 }
2521 case SpvOpCompositeExtract:
2522 val->ssa = vtn_composite_extract(b, vtn_ssa_value(b, w[3]),
2523 w + 4, count - 4);
2524 break;
2525
2526 case SpvOpCompositeInsert:
2527 val->ssa = vtn_composite_insert(b, vtn_ssa_value(b, w[4]),
2528 vtn_ssa_value(b, w[3]),
2529 w + 5, count - 5);
2530 break;
2531
2532 case SpvOpCopyObject:
2533 val->ssa = vtn_composite_copy(b, vtn_ssa_value(b, w[3]));
2534 break;
2535
2536 default:
2537 unreachable("unknown composite operation");
2538 }
2539 }
2540
2541 static void
2542 vtn_handle_barrier(struct vtn_builder *b, SpvOp opcode,
2543 const uint32_t *w, unsigned count)
2544 {
2545 nir_intrinsic_op intrinsic_op;
2546 switch (opcode) {
2547 case SpvOpEmitVertex:
2548 case SpvOpEmitStreamVertex:
2549 intrinsic_op = nir_intrinsic_emit_vertex;
2550 break;
2551 case SpvOpEndPrimitive:
2552 case SpvOpEndStreamPrimitive:
2553 intrinsic_op = nir_intrinsic_end_primitive;
2554 break;
2555 case SpvOpMemoryBarrier:
2556 intrinsic_op = nir_intrinsic_memory_barrier;
2557 break;
2558 case SpvOpControlBarrier:
2559 intrinsic_op = nir_intrinsic_barrier;
2560 break;
2561 default:
2562 unreachable("unknown barrier instruction");
2563 }
2564
2565 nir_intrinsic_instr *intrin =
2566 nir_intrinsic_instr_create(b->shader, intrinsic_op);
2567
2568 if (opcode == SpvOpEmitStreamVertex || opcode == SpvOpEndStreamPrimitive)
2569 nir_intrinsic_set_stream_id(intrin, w[1]);
2570
2571 nir_builder_instr_insert(&b->nb, &intrin->instr);
2572 }
2573
2574 static unsigned
2575 gl_primitive_from_spv_execution_mode(SpvExecutionMode mode)
2576 {
2577 switch (mode) {
2578 case SpvExecutionModeInputPoints:
2579 case SpvExecutionModeOutputPoints:
2580 return 0; /* GL_POINTS */
2581 case SpvExecutionModeInputLines:
2582 return 1; /* GL_LINES */
2583 case SpvExecutionModeInputLinesAdjacency:
2584 return 0x000A; /* GL_LINE_STRIP_ADJACENCY_ARB */
2585 case SpvExecutionModeTriangles:
2586 return 4; /* GL_TRIANGLES */
2587 case SpvExecutionModeInputTrianglesAdjacency:
2588 return 0x000C; /* GL_TRIANGLES_ADJACENCY_ARB */
2589 case SpvExecutionModeQuads:
2590 return 7; /* GL_QUADS */
2591 case SpvExecutionModeIsolines:
2592 return 0x8E7A; /* GL_ISOLINES */
2593 case SpvExecutionModeOutputLineStrip:
2594 return 3; /* GL_LINE_STRIP */
2595 case SpvExecutionModeOutputTriangleStrip:
2596 return 5; /* GL_TRIANGLE_STRIP */
2597 default:
2598 assert(!"Invalid primitive type");
2599 return 4;
2600 }
2601 }
2602
2603 static unsigned
2604 vertices_in_from_spv_execution_mode(SpvExecutionMode mode)
2605 {
2606 switch (mode) {
2607 case SpvExecutionModeInputPoints:
2608 return 1;
2609 case SpvExecutionModeInputLines:
2610 return 2;
2611 case SpvExecutionModeInputLinesAdjacency:
2612 return 4;
2613 case SpvExecutionModeTriangles:
2614 return 3;
2615 case SpvExecutionModeInputTrianglesAdjacency:
2616 return 6;
2617 default:
2618 assert(!"Invalid GS input mode");
2619 return 0;
2620 }
2621 }
2622
2623 static gl_shader_stage
2624 stage_for_execution_model(SpvExecutionModel model)
2625 {
2626 switch (model) {
2627 case SpvExecutionModelVertex:
2628 return MESA_SHADER_VERTEX;
2629 case SpvExecutionModelTessellationControl:
2630 return MESA_SHADER_TESS_CTRL;
2631 case SpvExecutionModelTessellationEvaluation:
2632 return MESA_SHADER_TESS_EVAL;
2633 case SpvExecutionModelGeometry:
2634 return MESA_SHADER_GEOMETRY;
2635 case SpvExecutionModelFragment:
2636 return MESA_SHADER_FRAGMENT;
2637 case SpvExecutionModelGLCompute:
2638 return MESA_SHADER_COMPUTE;
2639 default:
2640 unreachable("Unsupported execution model");
2641 }
2642 }
2643
2644 #define spv_check_supported(name, cap) do { \
2645 if (!(b->ext && b->ext->name)) \
2646 vtn_warn("Unsupported SPIR-V capability: %s", \
2647 spirv_capability_to_string(cap)); \
2648 } while(0)
2649
2650 static bool
2651 vtn_handle_preamble_instruction(struct vtn_builder *b, SpvOp opcode,
2652 const uint32_t *w, unsigned count)
2653 {
2654 switch (opcode) {
2655 case SpvOpSource:
2656 case SpvOpSourceExtension:
2657 case SpvOpSourceContinued:
2658 case SpvOpExtension:
2659 /* Unhandled, but these are for debug so that's ok. */
2660 break;
2661
2662 case SpvOpCapability: {
2663 SpvCapability cap = w[1];
2664 switch (cap) {
2665 case SpvCapabilityMatrix:
2666 case SpvCapabilityShader:
2667 case SpvCapabilityGeometry:
2668 case SpvCapabilityGeometryPointSize:
2669 case SpvCapabilityUniformBufferArrayDynamicIndexing:
2670 case SpvCapabilitySampledImageArrayDynamicIndexing:
2671 case SpvCapabilityStorageBufferArrayDynamicIndexing:
2672 case SpvCapabilityStorageImageArrayDynamicIndexing:
2673 case SpvCapabilityImageRect:
2674 case SpvCapabilitySampledRect:
2675 case SpvCapabilitySampled1D:
2676 case SpvCapabilityImage1D:
2677 case SpvCapabilitySampledCubeArray:
2678 case SpvCapabilitySampledBuffer:
2679 case SpvCapabilityImageBuffer:
2680 case SpvCapabilityImageQuery:
2681 case SpvCapabilityDerivativeControl:
2682 case SpvCapabilityInterpolationFunction:
2683 case SpvCapabilityMultiViewport:
2684 case SpvCapabilitySampleRateShading:
2685 case SpvCapabilityClipDistance:
2686 case SpvCapabilityCullDistance:
2687 case SpvCapabilityInputAttachment:
2688 case SpvCapabilityImageGatherExtended:
2689 case SpvCapabilityStorageImageExtendedFormats:
2690 break;
2691
2692 case SpvCapabilityGeometryStreams:
2693 case SpvCapabilityLinkage:
2694 case SpvCapabilityVector16:
2695 case SpvCapabilityFloat16Buffer:
2696 case SpvCapabilityFloat16:
2697 case SpvCapabilityInt64Atomics:
2698 case SpvCapabilityAtomicStorage:
2699 case SpvCapabilityInt16:
2700 case SpvCapabilityStorageImageMultisample:
2701 case SpvCapabilityImageCubeArray:
2702 case SpvCapabilityInt8:
2703 case SpvCapabilitySparseResidency:
2704 case SpvCapabilityMinLod:
2705 case SpvCapabilityTransformFeedback:
2706 vtn_warn("Unsupported SPIR-V capability: %s",
2707 spirv_capability_to_string(cap));
2708 break;
2709
2710 case SpvCapabilityFloat64:
2711 spv_check_supported(float64, cap);
2712 break;
2713 case SpvCapabilityInt64:
2714 spv_check_supported(int64, cap);
2715 break;
2716
2717 case SpvCapabilityAddresses:
2718 case SpvCapabilityKernel:
2719 case SpvCapabilityImageBasic:
2720 case SpvCapabilityImageReadWrite:
2721 case SpvCapabilityImageMipmap:
2722 case SpvCapabilityPipes:
2723 case SpvCapabilityGroups:
2724 case SpvCapabilityDeviceEnqueue:
2725 case SpvCapabilityLiteralSampler:
2726 case SpvCapabilityGenericPointer:
2727 vtn_warn("Unsupported OpenCL-style SPIR-V capability: %s",
2728 spirv_capability_to_string(cap));
2729 break;
2730
2731 case SpvCapabilityImageMSArray:
2732 spv_check_supported(image_ms_array, cap);
2733 break;
2734
2735 case SpvCapabilityTessellation:
2736 case SpvCapabilityTessellationPointSize:
2737 spv_check_supported(tessellation, cap);
2738 break;
2739
2740 case SpvCapabilityDrawParameters:
2741 spv_check_supported(draw_parameters, cap);
2742 break;
2743
2744 case SpvCapabilityStorageImageReadWithoutFormat:
2745 spv_check_supported(image_read_without_format, cap);
2746 break;
2747
2748 case SpvCapabilityStorageImageWriteWithoutFormat:
2749 spv_check_supported(image_write_without_format, cap);
2750 break;
2751
2752 case SpvCapabilityMultiView:
2753 spv_check_supported(multiview, cap);
2754 break;
2755
2756 default:
2757 unreachable("Unhandled capability");
2758 }
2759 break;
2760 }
2761
2762 case SpvOpExtInstImport:
2763 vtn_handle_extension(b, opcode, w, count);
2764 break;
2765
2766 case SpvOpMemoryModel:
2767 assert(w[1] == SpvAddressingModelLogical);
2768 assert(w[2] == SpvMemoryModelGLSL450);
2769 break;
2770
2771 case SpvOpEntryPoint: {
2772 struct vtn_value *entry_point = &b->values[w[2]];
2773 /* Let this be a name label regardless */
2774 unsigned name_words;
2775 entry_point->name = vtn_string_literal(b, &w[3], count - 3, &name_words);
2776
2777 if (strcmp(entry_point->name, b->entry_point_name) != 0 ||
2778 stage_for_execution_model(w[1]) != b->entry_point_stage)
2779 break;
2780
2781 assert(b->entry_point == NULL);
2782 b->entry_point = entry_point;
2783 break;
2784 }
2785
2786 case SpvOpString:
2787 vtn_push_value(b, w[1], vtn_value_type_string)->str =
2788 vtn_string_literal(b, &w[2], count - 2, NULL);
2789 break;
2790
2791 case SpvOpName:
2792 b->values[w[1]].name = vtn_string_literal(b, &w[2], count - 2, NULL);
2793 break;
2794
2795 case SpvOpMemberName:
2796 /* TODO */
2797 break;
2798
2799 case SpvOpExecutionMode:
2800 case SpvOpDecorationGroup:
2801 case SpvOpDecorate:
2802 case SpvOpMemberDecorate:
2803 case SpvOpGroupDecorate:
2804 case SpvOpGroupMemberDecorate:
2805 vtn_handle_decoration(b, opcode, w, count);
2806 break;
2807
2808 default:
2809 return false; /* End of preamble */
2810 }
2811
2812 return true;
2813 }
2814
2815 static void
2816 vtn_handle_execution_mode(struct vtn_builder *b, struct vtn_value *entry_point,
2817 const struct vtn_decoration *mode, void *data)
2818 {
2819 assert(b->entry_point == entry_point);
2820
2821 switch(mode->exec_mode) {
2822 case SpvExecutionModeOriginUpperLeft:
2823 case SpvExecutionModeOriginLowerLeft:
2824 b->origin_upper_left =
2825 (mode->exec_mode == SpvExecutionModeOriginUpperLeft);
2826 break;
2827
2828 case SpvExecutionModeEarlyFragmentTests:
2829 assert(b->shader->stage == MESA_SHADER_FRAGMENT);
2830 b->shader->info.fs.early_fragment_tests = true;
2831 break;
2832
2833 case SpvExecutionModeInvocations:
2834 assert(b->shader->stage == MESA_SHADER_GEOMETRY);
2835 b->shader->info.gs.invocations = MAX2(1, mode->literals[0]);
2836 break;
2837
2838 case SpvExecutionModeDepthReplacing:
2839 assert(b->shader->stage == MESA_SHADER_FRAGMENT);
2840 b->shader->info.fs.depth_layout = FRAG_DEPTH_LAYOUT_ANY;
2841 break;
2842 case SpvExecutionModeDepthGreater:
2843 assert(b->shader->stage == MESA_SHADER_FRAGMENT);
2844 b->shader->info.fs.depth_layout = FRAG_DEPTH_LAYOUT_GREATER;
2845 break;
2846 case SpvExecutionModeDepthLess:
2847 assert(b->shader->stage == MESA_SHADER_FRAGMENT);
2848 b->shader->info.fs.depth_layout = FRAG_DEPTH_LAYOUT_LESS;
2849 break;
2850 case SpvExecutionModeDepthUnchanged:
2851 assert(b->shader->stage == MESA_SHADER_FRAGMENT);
2852 b->shader->info.fs.depth_layout = FRAG_DEPTH_LAYOUT_UNCHANGED;
2853 break;
2854
2855 case SpvExecutionModeLocalSize:
2856 assert(b->shader->stage == MESA_SHADER_COMPUTE);
2857 b->shader->info.cs.local_size[0] = mode->literals[0];
2858 b->shader->info.cs.local_size[1] = mode->literals[1];
2859 b->shader->info.cs.local_size[2] = mode->literals[2];
2860 break;
2861 case SpvExecutionModeLocalSizeHint:
2862 break; /* Nothing to do with this */
2863
2864 case SpvExecutionModeOutputVertices:
2865 if (b->shader->stage == MESA_SHADER_TESS_CTRL ||
2866 b->shader->stage == MESA_SHADER_TESS_EVAL) {
2867 b->shader->info.tess.tcs_vertices_out = mode->literals[0];
2868 } else {
2869 assert(b->shader->stage == MESA_SHADER_GEOMETRY);
2870 b->shader->info.gs.vertices_out = mode->literals[0];
2871 }
2872 break;
2873
2874 case SpvExecutionModeInputPoints:
2875 case SpvExecutionModeInputLines:
2876 case SpvExecutionModeInputLinesAdjacency:
2877 case SpvExecutionModeTriangles:
2878 case SpvExecutionModeInputTrianglesAdjacency:
2879 case SpvExecutionModeQuads:
2880 case SpvExecutionModeIsolines:
2881 if (b->shader->stage == MESA_SHADER_TESS_CTRL ||
2882 b->shader->stage == MESA_SHADER_TESS_EVAL) {
2883 b->shader->info.tess.primitive_mode =
2884 gl_primitive_from_spv_execution_mode(mode->exec_mode);
2885 } else {
2886 assert(b->shader->stage == MESA_SHADER_GEOMETRY);
2887 b->shader->info.gs.vertices_in =
2888 vertices_in_from_spv_execution_mode(mode->exec_mode);
2889 }
2890 break;
2891
2892 case SpvExecutionModeOutputPoints:
2893 case SpvExecutionModeOutputLineStrip:
2894 case SpvExecutionModeOutputTriangleStrip:
2895 assert(b->shader->stage == MESA_SHADER_GEOMETRY);
2896 b->shader->info.gs.output_primitive =
2897 gl_primitive_from_spv_execution_mode(mode->exec_mode);
2898 break;
2899
2900 case SpvExecutionModeSpacingEqual:
2901 assert(b->shader->stage == MESA_SHADER_TESS_CTRL ||
2902 b->shader->stage == MESA_SHADER_TESS_EVAL);
2903 b->shader->info.tess.spacing = TESS_SPACING_EQUAL;
2904 break;
2905 case SpvExecutionModeSpacingFractionalEven:
2906 assert(b->shader->stage == MESA_SHADER_TESS_CTRL ||
2907 b->shader->stage == MESA_SHADER_TESS_EVAL);
2908 b->shader->info.tess.spacing = TESS_SPACING_FRACTIONAL_EVEN;
2909 break;
2910 case SpvExecutionModeSpacingFractionalOdd:
2911 assert(b->shader->stage == MESA_SHADER_TESS_CTRL ||
2912 b->shader->stage == MESA_SHADER_TESS_EVAL);
2913 b->shader->info.tess.spacing = TESS_SPACING_FRACTIONAL_ODD;
2914 break;
2915 case SpvExecutionModeVertexOrderCw:
2916 assert(b->shader->stage == MESA_SHADER_TESS_CTRL ||
2917 b->shader->stage == MESA_SHADER_TESS_EVAL);
2918 /* Vulkan's notion of CCW seems to match the hardware backends,
2919 * but be the opposite of OpenGL. Currently NIR follows GL semantics,
2920 * so we set it backwards here.
2921 */
2922 b->shader->info.tess.ccw = true;
2923 break;
2924 case SpvExecutionModeVertexOrderCcw:
2925 assert(b->shader->stage == MESA_SHADER_TESS_CTRL ||
2926 b->shader->stage == MESA_SHADER_TESS_EVAL);
2927 /* Backwards; see above */
2928 b->shader->info.tess.ccw = false;
2929 break;
2930 case SpvExecutionModePointMode:
2931 assert(b->shader->stage == MESA_SHADER_TESS_CTRL ||
2932 b->shader->stage == MESA_SHADER_TESS_EVAL);
2933 b->shader->info.tess.point_mode = true;
2934 break;
2935
2936 case SpvExecutionModePixelCenterInteger:
2937 b->pixel_center_integer = true;
2938 break;
2939
2940 case SpvExecutionModeXfb:
2941 assert(!"Unhandled execution mode");
2942 break;
2943
2944 case SpvExecutionModeVecTypeHint:
2945 case SpvExecutionModeContractionOff:
2946 break; /* OpenCL */
2947
2948 default:
2949 unreachable("Unhandled execution mode");
2950 }
2951 }
2952
2953 static bool
2954 vtn_handle_variable_or_type_instruction(struct vtn_builder *b, SpvOp opcode,
2955 const uint32_t *w, unsigned count)
2956 {
2957 switch (opcode) {
2958 case SpvOpSource:
2959 case SpvOpSourceContinued:
2960 case SpvOpSourceExtension:
2961 case SpvOpExtension:
2962 case SpvOpCapability:
2963 case SpvOpExtInstImport:
2964 case SpvOpMemoryModel:
2965 case SpvOpEntryPoint:
2966 case SpvOpExecutionMode:
2967 case SpvOpString:
2968 case SpvOpName:
2969 case SpvOpMemberName:
2970 case SpvOpDecorationGroup:
2971 case SpvOpDecorate:
2972 case SpvOpMemberDecorate:
2973 case SpvOpGroupDecorate:
2974 case SpvOpGroupMemberDecorate:
2975 assert(!"Invalid opcode types and variables section");
2976 break;
2977
2978 case SpvOpTypeVoid:
2979 case SpvOpTypeBool:
2980 case SpvOpTypeInt:
2981 case SpvOpTypeFloat:
2982 case SpvOpTypeVector:
2983 case SpvOpTypeMatrix:
2984 case SpvOpTypeImage:
2985 case SpvOpTypeSampler:
2986 case SpvOpTypeSampledImage:
2987 case SpvOpTypeArray:
2988 case SpvOpTypeRuntimeArray:
2989 case SpvOpTypeStruct:
2990 case SpvOpTypeOpaque:
2991 case SpvOpTypePointer:
2992 case SpvOpTypeFunction:
2993 case SpvOpTypeEvent:
2994 case SpvOpTypeDeviceEvent:
2995 case SpvOpTypeReserveId:
2996 case SpvOpTypeQueue:
2997 case SpvOpTypePipe:
2998 vtn_handle_type(b, opcode, w, count);
2999 break;
3000
3001 case SpvOpConstantTrue:
3002 case SpvOpConstantFalse:
3003 case SpvOpConstant:
3004 case SpvOpConstantComposite:
3005 case SpvOpConstantSampler:
3006 case SpvOpConstantNull:
3007 case SpvOpSpecConstantTrue:
3008 case SpvOpSpecConstantFalse:
3009 case SpvOpSpecConstant:
3010 case SpvOpSpecConstantComposite:
3011 case SpvOpSpecConstantOp:
3012 vtn_handle_constant(b, opcode, w, count);
3013 break;
3014
3015 case SpvOpUndef:
3016 case SpvOpVariable:
3017 vtn_handle_variables(b, opcode, w, count);
3018 break;
3019
3020 default:
3021 return false; /* End of preamble */
3022 }
3023
3024 return true;
3025 }
3026
3027 static bool
3028 vtn_handle_body_instruction(struct vtn_builder *b, SpvOp opcode,
3029 const uint32_t *w, unsigned count)
3030 {
3031 switch (opcode) {
3032 case SpvOpLabel:
3033 break;
3034
3035 case SpvOpLoopMerge:
3036 case SpvOpSelectionMerge:
3037 /* This is handled by cfg pre-pass and walk_blocks */
3038 break;
3039
3040 case SpvOpUndef: {
3041 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_undef);
3042 val->type = vtn_value(b, w[1], vtn_value_type_type)->type;
3043 break;
3044 }
3045
3046 case SpvOpExtInst:
3047 vtn_handle_extension(b, opcode, w, count);
3048 break;
3049
3050 case SpvOpVariable:
3051 case SpvOpLoad:
3052 case SpvOpStore:
3053 case SpvOpCopyMemory:
3054 case SpvOpCopyMemorySized:
3055 case SpvOpAccessChain:
3056 case SpvOpInBoundsAccessChain:
3057 case SpvOpArrayLength:
3058 vtn_handle_variables(b, opcode, w, count);
3059 break;
3060
3061 case SpvOpFunctionCall:
3062 vtn_handle_function_call(b, opcode, w, count);
3063 break;
3064
3065 case SpvOpSampledImage:
3066 case SpvOpImage:
3067 case SpvOpImageSampleImplicitLod:
3068 case SpvOpImageSampleExplicitLod:
3069 case SpvOpImageSampleDrefImplicitLod:
3070 case SpvOpImageSampleDrefExplicitLod:
3071 case SpvOpImageSampleProjImplicitLod:
3072 case SpvOpImageSampleProjExplicitLod:
3073 case SpvOpImageSampleProjDrefImplicitLod:
3074 case SpvOpImageSampleProjDrefExplicitLod:
3075 case SpvOpImageFetch:
3076 case SpvOpImageGather:
3077 case SpvOpImageDrefGather:
3078 case SpvOpImageQuerySizeLod:
3079 case SpvOpImageQueryLod:
3080 case SpvOpImageQueryLevels:
3081 case SpvOpImageQuerySamples:
3082 vtn_handle_texture(b, opcode, w, count);
3083 break;
3084
3085 case SpvOpImageRead:
3086 case SpvOpImageWrite:
3087 case SpvOpImageTexelPointer:
3088 vtn_handle_image(b, opcode, w, count);
3089 break;
3090
3091 case SpvOpImageQuerySize: {
3092 struct vtn_pointer *image =
3093 vtn_value(b, w[3], vtn_value_type_pointer)->pointer;
3094 if (image->mode == vtn_variable_mode_image) {
3095 vtn_handle_image(b, opcode, w, count);
3096 } else {
3097 assert(image->mode == vtn_variable_mode_sampler);
3098 vtn_handle_texture(b, opcode, w, count);
3099 }
3100 break;
3101 }
3102
3103 case SpvOpAtomicLoad:
3104 case SpvOpAtomicExchange:
3105 case SpvOpAtomicCompareExchange:
3106 case SpvOpAtomicCompareExchangeWeak:
3107 case SpvOpAtomicIIncrement:
3108 case SpvOpAtomicIDecrement:
3109 case SpvOpAtomicIAdd:
3110 case SpvOpAtomicISub:
3111 case SpvOpAtomicSMin:
3112 case SpvOpAtomicUMin:
3113 case SpvOpAtomicSMax:
3114 case SpvOpAtomicUMax:
3115 case SpvOpAtomicAnd:
3116 case SpvOpAtomicOr:
3117 case SpvOpAtomicXor: {
3118 struct vtn_value *pointer = vtn_untyped_value(b, w[3]);
3119 if (pointer->value_type == vtn_value_type_image_pointer) {
3120 vtn_handle_image(b, opcode, w, count);
3121 } else {
3122 assert(pointer->value_type == vtn_value_type_pointer);
3123 vtn_handle_ssbo_or_shared_atomic(b, opcode, w, count);
3124 }
3125 break;
3126 }
3127
3128 case SpvOpAtomicStore: {
3129 struct vtn_value *pointer = vtn_untyped_value(b, w[1]);
3130 if (pointer->value_type == vtn_value_type_image_pointer) {
3131 vtn_handle_image(b, opcode, w, count);
3132 } else {
3133 assert(pointer->value_type == vtn_value_type_pointer);
3134 vtn_handle_ssbo_or_shared_atomic(b, opcode, w, count);
3135 }
3136 break;
3137 }
3138
3139 case SpvOpSNegate:
3140 case SpvOpFNegate:
3141 case SpvOpNot:
3142 case SpvOpAny:
3143 case SpvOpAll:
3144 case SpvOpConvertFToU:
3145 case SpvOpConvertFToS:
3146 case SpvOpConvertSToF:
3147 case SpvOpConvertUToF:
3148 case SpvOpUConvert:
3149 case SpvOpSConvert:
3150 case SpvOpFConvert:
3151 case SpvOpQuantizeToF16:
3152 case SpvOpConvertPtrToU:
3153 case SpvOpConvertUToPtr:
3154 case SpvOpPtrCastToGeneric:
3155 case SpvOpGenericCastToPtr:
3156 case SpvOpBitcast:
3157 case SpvOpIsNan:
3158 case SpvOpIsInf:
3159 case SpvOpIsFinite:
3160 case SpvOpIsNormal:
3161 case SpvOpSignBitSet:
3162 case SpvOpLessOrGreater:
3163 case SpvOpOrdered:
3164 case SpvOpUnordered:
3165 case SpvOpIAdd:
3166 case SpvOpFAdd:
3167 case SpvOpISub:
3168 case SpvOpFSub:
3169 case SpvOpIMul:
3170 case SpvOpFMul:
3171 case SpvOpUDiv:
3172 case SpvOpSDiv:
3173 case SpvOpFDiv:
3174 case SpvOpUMod:
3175 case SpvOpSRem:
3176 case SpvOpSMod:
3177 case SpvOpFRem:
3178 case SpvOpFMod:
3179 case SpvOpVectorTimesScalar:
3180 case SpvOpDot:
3181 case SpvOpIAddCarry:
3182 case SpvOpISubBorrow:
3183 case SpvOpUMulExtended:
3184 case SpvOpSMulExtended:
3185 case SpvOpShiftRightLogical:
3186 case SpvOpShiftRightArithmetic:
3187 case SpvOpShiftLeftLogical:
3188 case SpvOpLogicalEqual:
3189 case SpvOpLogicalNotEqual:
3190 case SpvOpLogicalOr:
3191 case SpvOpLogicalAnd:
3192 case SpvOpLogicalNot:
3193 case SpvOpBitwiseOr:
3194 case SpvOpBitwiseXor:
3195 case SpvOpBitwiseAnd:
3196 case SpvOpSelect:
3197 case SpvOpIEqual:
3198 case SpvOpFOrdEqual:
3199 case SpvOpFUnordEqual:
3200 case SpvOpINotEqual:
3201 case SpvOpFOrdNotEqual:
3202 case SpvOpFUnordNotEqual:
3203 case SpvOpULessThan:
3204 case SpvOpSLessThan:
3205 case SpvOpFOrdLessThan:
3206 case SpvOpFUnordLessThan:
3207 case SpvOpUGreaterThan:
3208 case SpvOpSGreaterThan:
3209 case SpvOpFOrdGreaterThan:
3210 case SpvOpFUnordGreaterThan:
3211 case SpvOpULessThanEqual:
3212 case SpvOpSLessThanEqual:
3213 case SpvOpFOrdLessThanEqual:
3214 case SpvOpFUnordLessThanEqual:
3215 case SpvOpUGreaterThanEqual:
3216 case SpvOpSGreaterThanEqual:
3217 case SpvOpFOrdGreaterThanEqual:
3218 case SpvOpFUnordGreaterThanEqual:
3219 case SpvOpDPdx:
3220 case SpvOpDPdy:
3221 case SpvOpFwidth:
3222 case SpvOpDPdxFine:
3223 case SpvOpDPdyFine:
3224 case SpvOpFwidthFine:
3225 case SpvOpDPdxCoarse:
3226 case SpvOpDPdyCoarse:
3227 case SpvOpFwidthCoarse:
3228 case SpvOpBitFieldInsert:
3229 case SpvOpBitFieldSExtract:
3230 case SpvOpBitFieldUExtract:
3231 case SpvOpBitReverse:
3232 case SpvOpBitCount:
3233 case SpvOpTranspose:
3234 case SpvOpOuterProduct:
3235 case SpvOpMatrixTimesScalar:
3236 case SpvOpVectorTimesMatrix:
3237 case SpvOpMatrixTimesVector:
3238 case SpvOpMatrixTimesMatrix:
3239 vtn_handle_alu(b, opcode, w, count);
3240 break;
3241
3242 case SpvOpVectorExtractDynamic:
3243 case SpvOpVectorInsertDynamic:
3244 case SpvOpVectorShuffle:
3245 case SpvOpCompositeConstruct:
3246 case SpvOpCompositeExtract:
3247 case SpvOpCompositeInsert:
3248 case SpvOpCopyObject:
3249 vtn_handle_composite(b, opcode, w, count);
3250 break;
3251
3252 case SpvOpEmitVertex:
3253 case SpvOpEndPrimitive:
3254 case SpvOpEmitStreamVertex:
3255 case SpvOpEndStreamPrimitive:
3256 case SpvOpControlBarrier:
3257 case SpvOpMemoryBarrier:
3258 vtn_handle_barrier(b, opcode, w, count);
3259 break;
3260
3261 default:
3262 unreachable("Unhandled opcode");
3263 }
3264
3265 return true;
3266 }
3267
3268 nir_function *
3269 spirv_to_nir(const uint32_t *words, size_t word_count,
3270 struct nir_spirv_specialization *spec, unsigned num_spec,
3271 gl_shader_stage stage, const char *entry_point_name,
3272 const struct nir_spirv_supported_extensions *ext,
3273 const nir_shader_compiler_options *options)
3274 {
3275 const uint32_t *word_end = words + word_count;
3276
3277 /* Handle the SPIR-V header (first 4 dwords) */
3278 assert(word_count > 5);
3279
3280 assert(words[0] == SpvMagicNumber);
3281 assert(words[1] >= 0x10000);
3282 /* words[2] == generator magic */
3283 unsigned value_id_bound = words[3];
3284 assert(words[4] == 0);
3285
3286 words+= 5;
3287
3288 /* Initialize the stn_builder object */
3289 struct vtn_builder *b = rzalloc(NULL, struct vtn_builder);
3290 b->value_id_bound = value_id_bound;
3291 b->values = rzalloc_array(b, struct vtn_value, value_id_bound);
3292 exec_list_make_empty(&b->functions);
3293 b->entry_point_stage = stage;
3294 b->entry_point_name = entry_point_name;
3295 b->ext = ext;
3296
3297 /* Handle all the preamble instructions */
3298 words = vtn_foreach_instruction(b, words, word_end,
3299 vtn_handle_preamble_instruction);
3300
3301 if (b->entry_point == NULL) {
3302 assert(!"Entry point not found");
3303 ralloc_free(b);
3304 return NULL;
3305 }
3306
3307 b->shader = nir_shader_create(NULL, stage, options, NULL);
3308
3309 /* Set shader info defaults */
3310 b->shader->info.gs.invocations = 1;
3311
3312 /* Parse execution modes */
3313 vtn_foreach_execution_mode(b, b->entry_point,
3314 vtn_handle_execution_mode, NULL);
3315
3316 b->specializations = spec;
3317 b->num_specializations = num_spec;
3318
3319 /* Handle all variable, type, and constant instructions */
3320 words = vtn_foreach_instruction(b, words, word_end,
3321 vtn_handle_variable_or_type_instruction);
3322
3323 vtn_build_cfg(b, words, word_end);
3324
3325 foreach_list_typed(struct vtn_function, func, node, &b->functions) {
3326 b->impl = func->impl;
3327 b->const_table = _mesa_hash_table_create(b, _mesa_hash_pointer,
3328 _mesa_key_pointer_equal);
3329
3330 vtn_function_emit(b, func, vtn_handle_body_instruction);
3331 }
3332
3333 assert(b->entry_point->value_type == vtn_value_type_function);
3334 nir_function *entry_point = b->entry_point->func->impl->function;
3335 assert(entry_point);
3336
3337 ralloc_free(b);
3338
3339 return entry_point;
3340 }