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