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