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