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