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