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