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