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