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