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