Merge commit '8b0fb1c152fe191768953aa8c77b89034a377f83' into vulkan
[mesa.git] / src / compiler / nir / 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 nir_ssa_undef_instr *undef =
42 nir_ssa_undef_instr_create(b->shader, num_components);
43
44 nir_instr_insert_before_cf_list(&b->impl->body, &undef->instr);
45 val->def = &undef->def;
46 } else {
47 unsigned elems = glsl_get_length(val->type);
48 val->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
49 if (glsl_type_is_matrix(type)) {
50 const struct glsl_type *elem_type =
51 glsl_vector_type(glsl_get_base_type(type),
52 glsl_get_vector_elements(type));
53
54 for (unsigned i = 0; i < elems; i++)
55 val->elems[i] = vtn_undef_ssa_value(b, elem_type);
56 } else if (glsl_type_is_array(type)) {
57 const struct glsl_type *elem_type = glsl_get_array_element(type);
58 for (unsigned i = 0; i < elems; i++)
59 val->elems[i] = vtn_undef_ssa_value(b, elem_type);
60 } else {
61 for (unsigned i = 0; i < elems; i++) {
62 const struct glsl_type *elem_type = glsl_get_struct_field(type, i);
63 val->elems[i] = vtn_undef_ssa_value(b, elem_type);
64 }
65 }
66 }
67
68 return val;
69 }
70
71 static struct vtn_ssa_value *
72 vtn_const_ssa_value(struct vtn_builder *b, nir_constant *constant,
73 const struct glsl_type *type)
74 {
75 struct hash_entry *entry = _mesa_hash_table_search(b->const_table, constant);
76
77 if (entry)
78 return entry->data;
79
80 struct vtn_ssa_value *val = rzalloc(b, struct vtn_ssa_value);
81 val->type = type;
82
83 switch (glsl_get_base_type(type)) {
84 case GLSL_TYPE_INT:
85 case GLSL_TYPE_UINT:
86 case GLSL_TYPE_BOOL:
87 case GLSL_TYPE_FLOAT:
88 case GLSL_TYPE_DOUBLE:
89 if (glsl_type_is_vector_or_scalar(type)) {
90 unsigned num_components = glsl_get_vector_elements(val->type);
91 nir_load_const_instr *load =
92 nir_load_const_instr_create(b->shader, num_components);
93
94 for (unsigned i = 0; i < num_components; i++)
95 load->value.u[i] = constant->value.u[i];
96
97 nir_instr_insert_before_cf_list(&b->impl->body, &load->instr);
98 val->def = &load->def;
99 } else {
100 assert(glsl_type_is_matrix(type));
101 unsigned rows = glsl_get_vector_elements(val->type);
102 unsigned columns = glsl_get_matrix_columns(val->type);
103 val->elems = ralloc_array(b, struct vtn_ssa_value *, columns);
104
105 for (unsigned i = 0; i < columns; i++) {
106 struct vtn_ssa_value *col_val = rzalloc(b, struct vtn_ssa_value);
107 col_val->type = glsl_get_column_type(val->type);
108 nir_load_const_instr *load =
109 nir_load_const_instr_create(b->shader, rows);
110
111 for (unsigned j = 0; j < rows; j++)
112 load->value.u[j] = constant->value.u[rows * i + j];
113
114 nir_instr_insert_before_cf_list(&b->impl->body, &load->instr);
115 col_val->def = &load->def;
116
117 val->elems[i] = col_val;
118 }
119 }
120 break;
121
122 case GLSL_TYPE_ARRAY: {
123 unsigned elems = glsl_get_length(val->type);
124 val->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
125 const struct glsl_type *elem_type = glsl_get_array_element(val->type);
126 for (unsigned i = 0; i < elems; i++)
127 val->elems[i] = vtn_const_ssa_value(b, constant->elements[i],
128 elem_type);
129 break;
130 }
131
132 case GLSL_TYPE_STRUCT: {
133 unsigned elems = glsl_get_length(val->type);
134 val->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
135 for (unsigned i = 0; i < elems; i++) {
136 const struct glsl_type *elem_type =
137 glsl_get_struct_field(val->type, i);
138 val->elems[i] = vtn_const_ssa_value(b, constant->elements[i],
139 elem_type);
140 }
141 break;
142 }
143
144 default:
145 unreachable("bad constant type");
146 }
147
148 return val;
149 }
150
151 struct vtn_ssa_value *
152 vtn_ssa_value(struct vtn_builder *b, uint32_t value_id)
153 {
154 struct vtn_value *val = vtn_untyped_value(b, value_id);
155 switch (val->value_type) {
156 case vtn_value_type_undef:
157 return vtn_undef_ssa_value(b, val->type->type);
158
159 case vtn_value_type_constant:
160 return vtn_const_ssa_value(b, val->constant, val->const_type);
161
162 case vtn_value_type_ssa:
163 return val->ssa;
164
165 case vtn_value_type_access_chain:
166 /* This is needed for function parameters */
167 return vtn_variable_load(b, val->access_chain);
168
169 default:
170 unreachable("Invalid type for an SSA value");
171 }
172 }
173
174 static char *
175 vtn_string_literal(struct vtn_builder *b, const uint32_t *words,
176 unsigned word_count, unsigned *words_used)
177 {
178 char *dup = ralloc_strndup(b, (char *)words, word_count * sizeof(*words));
179 if (words_used) {
180 /* Ammount of space taken by the string (including the null) */
181 unsigned len = strlen(dup) + 1;
182 *words_used = DIV_ROUND_UP(len, sizeof(*words));
183 }
184 return dup;
185 }
186
187 const uint32_t *
188 vtn_foreach_instruction(struct vtn_builder *b, const uint32_t *start,
189 const uint32_t *end, vtn_instruction_handler handler)
190 {
191 b->file = NULL;
192 b->line = -1;
193 b->col = -1;
194
195 const uint32_t *w = start;
196 while (w < end) {
197 SpvOp opcode = w[0] & SpvOpCodeMask;
198 unsigned count = w[0] >> SpvWordCountShift;
199 assert(count >= 1 && w + count <= end);
200
201 switch (opcode) {
202 case SpvOpNop:
203 break; /* Do nothing */
204
205 case SpvOpLine:
206 b->file = vtn_value(b, w[1], vtn_value_type_string)->str;
207 b->line = w[2];
208 b->col = w[3];
209 break;
210
211 case SpvOpNoLine:
212 b->file = NULL;
213 b->line = -1;
214 b->col = -1;
215 break;
216
217 default:
218 if (!handler(b, opcode, w, count))
219 return w;
220 break;
221 }
222
223 w += count;
224 }
225 assert(w == end);
226 return w;
227 }
228
229 static void
230 vtn_handle_extension(struct vtn_builder *b, SpvOp opcode,
231 const uint32_t *w, unsigned count)
232 {
233 switch (opcode) {
234 case SpvOpExtInstImport: {
235 struct vtn_value *val = vtn_push_value(b, w[1], vtn_value_type_extension);
236 if (strcmp((const char *)&w[2], "GLSL.std.450") == 0) {
237 val->ext_handler = vtn_handle_glsl450_instruction;
238 } else {
239 assert(!"Unsupported extension");
240 }
241 break;
242 }
243
244 case SpvOpExtInst: {
245 struct vtn_value *val = vtn_value(b, w[3], vtn_value_type_extension);
246 bool handled = val->ext_handler(b, w[4], w, count);
247 (void)handled;
248 assert(handled);
249 break;
250 }
251
252 default:
253 unreachable("Unhandled opcode");
254 }
255 }
256
257 static void
258 _foreach_decoration_helper(struct vtn_builder *b,
259 struct vtn_value *base_value,
260 int parent_member,
261 struct vtn_value *value,
262 vtn_decoration_foreach_cb cb, void *data)
263 {
264 for (struct vtn_decoration *dec = value->decoration; dec; dec = dec->next) {
265 int member;
266 if (dec->scope == VTN_DEC_DECORATION) {
267 member = parent_member;
268 } else if (dec->scope >= VTN_DEC_STRUCT_MEMBER0) {
269 assert(parent_member == -1);
270 member = dec->scope - VTN_DEC_STRUCT_MEMBER0;
271 } else {
272 /* Not a decoration */
273 continue;
274 }
275
276 if (dec->group) {
277 assert(dec->group->value_type == vtn_value_type_decoration_group);
278 _foreach_decoration_helper(b, base_value, member, dec->group,
279 cb, data);
280 } else {
281 cb(b, base_value, member, dec, data);
282 }
283 }
284 }
285
286 /** Iterates (recursively if needed) over all of the decorations on a value
287 *
288 * This function iterates over all of the decorations applied to a given
289 * value. If it encounters a decoration group, it recurses into the group
290 * and iterates over all of those decorations as well.
291 */
292 void
293 vtn_foreach_decoration(struct vtn_builder *b, struct vtn_value *value,
294 vtn_decoration_foreach_cb cb, void *data)
295 {
296 _foreach_decoration_helper(b, value, -1, value, cb, data);
297 }
298
299 void
300 vtn_foreach_execution_mode(struct vtn_builder *b, struct vtn_value *value,
301 vtn_execution_mode_foreach_cb cb, void *data)
302 {
303 for (struct vtn_decoration *dec = value->decoration; dec; dec = dec->next) {
304 if (dec->scope != VTN_DEC_EXECUTION_MODE)
305 continue;
306
307 assert(dec->group == NULL);
308 cb(b, value, dec, data);
309 }
310 }
311
312 static void
313 vtn_handle_decoration(struct vtn_builder *b, SpvOp opcode,
314 const uint32_t *w, unsigned count)
315 {
316 const uint32_t *w_end = w + count;
317 const uint32_t target = w[1];
318 w += 2;
319
320 switch (opcode) {
321 case SpvOpDecorationGroup:
322 vtn_push_value(b, target, vtn_value_type_decoration_group);
323 break;
324
325 case SpvOpDecorate:
326 case SpvOpMemberDecorate:
327 case SpvOpExecutionMode: {
328 struct vtn_value *val = &b->values[target];
329
330 struct vtn_decoration *dec = rzalloc(b, struct vtn_decoration);
331 switch (opcode) {
332 case SpvOpDecorate:
333 dec->scope = VTN_DEC_DECORATION;
334 break;
335 case SpvOpMemberDecorate:
336 dec->scope = VTN_DEC_STRUCT_MEMBER0 + *(w++);
337 break;
338 case SpvOpExecutionMode:
339 dec->scope = VTN_DEC_EXECUTION_MODE;
340 break;
341 default:
342 unreachable("Invalid decoration opcode");
343 }
344 dec->decoration = *(w++);
345 dec->literals = w;
346
347 /* Link into the list */
348 dec->next = val->decoration;
349 val->decoration = dec;
350 break;
351 }
352
353 case SpvOpGroupMemberDecorate:
354 case SpvOpGroupDecorate: {
355 struct vtn_value *group =
356 vtn_value(b, target, vtn_value_type_decoration_group);
357
358 for (; w < w_end; w++) {
359 struct vtn_value *val = vtn_untyped_value(b, *w);
360 struct vtn_decoration *dec = rzalloc(b, struct vtn_decoration);
361
362 dec->group = group;
363 if (opcode == SpvOpGroupDecorate) {
364 dec->scope = VTN_DEC_DECORATION;
365 } else {
366 dec->scope = VTN_DEC_STRUCT_MEMBER0 + *(w++);
367 }
368
369 /* Link into the list */
370 dec->next = val->decoration;
371 val->decoration = dec;
372 }
373 break;
374 }
375
376 default:
377 unreachable("Unhandled opcode");
378 }
379 }
380
381 struct member_decoration_ctx {
382 struct glsl_struct_field *fields;
383 struct vtn_type *type;
384 };
385
386 /* does a shallow copy of a vtn_type */
387
388 static struct vtn_type *
389 vtn_type_copy(struct vtn_builder *b, struct vtn_type *src)
390 {
391 struct vtn_type *dest = ralloc(b, struct vtn_type);
392 dest->type = src->type;
393 dest->is_builtin = src->is_builtin;
394 if (src->is_builtin)
395 dest->builtin = src->builtin;
396
397 if (!glsl_type_is_scalar(src->type)) {
398 switch (glsl_get_base_type(src->type)) {
399 case GLSL_TYPE_INT:
400 case GLSL_TYPE_UINT:
401 case GLSL_TYPE_BOOL:
402 case GLSL_TYPE_FLOAT:
403 case GLSL_TYPE_DOUBLE:
404 case GLSL_TYPE_ARRAY:
405 dest->row_major = src->row_major;
406 dest->stride = src->stride;
407 dest->array_element = src->array_element;
408 break;
409
410 case GLSL_TYPE_STRUCT: {
411 unsigned elems = glsl_get_length(src->type);
412
413 dest->members = ralloc_array(b, struct vtn_type *, elems);
414 memcpy(dest->members, src->members, elems * sizeof(struct vtn_type *));
415
416 dest->offsets = ralloc_array(b, unsigned, elems);
417 memcpy(dest->offsets, src->offsets, elems * sizeof(unsigned));
418 break;
419 }
420
421 default:
422 unreachable("unhandled type");
423 }
424 }
425
426 return dest;
427 }
428
429 static struct vtn_type *
430 mutable_matrix_member(struct vtn_builder *b, struct vtn_type *type, int member)
431 {
432 type->members[member] = vtn_type_copy(b, type->members[member]);
433 type = type->members[member];
434
435 /* We may have an array of matrices.... Oh, joy! */
436 while (glsl_type_is_array(type->type)) {
437 type->array_element = vtn_type_copy(b, type->array_element);
438 type = type->array_element;
439 }
440
441 assert(glsl_type_is_matrix(type->type));
442
443 return type;
444 }
445
446 static void
447 struct_member_decoration_cb(struct vtn_builder *b,
448 struct vtn_value *val, int member,
449 const struct vtn_decoration *dec, void *void_ctx)
450 {
451 struct member_decoration_ctx *ctx = void_ctx;
452
453 if (member < 0)
454 return;
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 .fields = fields,
675 .type = val->type
676 };
677
678 vtn_foreach_decoration(b, val, struct_member_decoration_cb, &ctx);
679
680 const char *name = val->name ? val->name : "struct";
681
682 val->type->type = glsl_struct_type(fields, num_fields, name);
683 break;
684 }
685
686 case SpvOpTypeFunction: {
687 const struct glsl_type *return_type =
688 vtn_value(b, w[2], vtn_value_type_type)->type->type;
689 NIR_VLA(struct glsl_function_param, params, count - 3);
690 for (unsigned i = 0; i < count - 3; i++) {
691 params[i].type = vtn_value(b, w[i + 3], vtn_value_type_type)->type->type;
692
693 /* FIXME: */
694 params[i].in = true;
695 params[i].out = true;
696 }
697 val->type->type = glsl_function_type(return_type, params, count - 3);
698 break;
699 }
700
701 case SpvOpTypePointer:
702 /* FIXME: For now, we'll just do the really lame thing and return
703 * the same type. The validator should ensure that the proper number
704 * of dereferences happen
705 */
706 val->type = vtn_value(b, w[3], vtn_value_type_type)->type;
707 break;
708
709 case SpvOpTypeImage: {
710 const struct glsl_type *sampled_type =
711 vtn_value(b, w[2], vtn_value_type_type)->type->type;
712
713 assert(glsl_type_is_vector_or_scalar(sampled_type));
714
715 enum glsl_sampler_dim dim;
716 switch ((SpvDim)w[3]) {
717 case SpvDim1D: dim = GLSL_SAMPLER_DIM_1D; break;
718 case SpvDim2D: dim = GLSL_SAMPLER_DIM_2D; break;
719 case SpvDim3D: dim = GLSL_SAMPLER_DIM_3D; break;
720 case SpvDimCube: dim = GLSL_SAMPLER_DIM_CUBE; break;
721 case SpvDimRect: dim = GLSL_SAMPLER_DIM_RECT; break;
722 case SpvDimBuffer: dim = GLSL_SAMPLER_DIM_BUF; break;
723 default:
724 unreachable("Invalid SPIR-V Sampler dimension");
725 }
726
727 bool is_shadow = w[4];
728 bool is_array = w[5];
729 bool multisampled = w[6];
730 unsigned sampled = w[7];
731 SpvImageFormat format = w[8];
732
733 if (count > 9)
734 val->type->access_qualifier = w[9];
735 else
736 val->type->access_qualifier = SpvAccessQualifierReadWrite;
737
738 assert(!multisampled && "FIXME: Handl multi-sampled textures");
739
740 val->type->image_format = translate_image_format(format);
741
742 if (sampled == 1) {
743 val->type->type = glsl_sampler_type(dim, is_shadow, is_array,
744 glsl_get_base_type(sampled_type));
745 } else if (sampled == 2) {
746 assert(format);
747 assert(!is_shadow);
748 val->type->type = glsl_image_type(dim, is_array,
749 glsl_get_base_type(sampled_type));
750 } else {
751 assert(!"We need to know if the image will be sampled");
752 }
753 break;
754 }
755
756 case SpvOpTypeSampledImage:
757 val->type = vtn_value(b, w[2], vtn_value_type_type)->type;
758 break;
759
760 case SpvOpTypeSampler:
761 /* The actual sampler type here doesn't really matter. It gets
762 * thrown away the moment you combine it with an image. What really
763 * matters is that it's a sampler type as opposed to an integer type
764 * so the backend knows what to do.
765 *
766 * TODO: Eventually we should consider adding a "bare sampler" type
767 * to glsl_types.
768 */
769 val->type->type = glsl_sampler_type(GLSL_SAMPLER_DIM_2D, false, false,
770 GLSL_TYPE_FLOAT);
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
1036 nir_const_value src[3];
1037 assert(count <= 7);
1038 for (unsigned i = 0; i < count - 4; i++) {
1039 nir_constant *c =
1040 vtn_value(b, w[4 + i], vtn_value_type_constant)->constant;
1041
1042 unsigned j = swap ? 1 - i : i;
1043 for (unsigned k = 0; k < num_components; k++)
1044 src[j].u[k] = c->value.u[k];
1045 }
1046
1047 nir_const_value res = nir_eval_const_opcode(op, num_components, src);
1048
1049 for (unsigned k = 0; k < num_components; k++)
1050 val->constant->value.u[k] = res.u[k];
1051
1052 return;
1053 } /* default */
1054 }
1055 }
1056
1057 case SpvOpConstantNull:
1058 val->constant = vtn_null_constant(b, val->const_type);
1059 break;
1060
1061 case SpvOpConstantSampler:
1062 assert(!"OpConstantSampler requires Kernel Capability");
1063 break;
1064
1065 default:
1066 unreachable("Unhandled opcode");
1067 }
1068 }
1069
1070 static void
1071 vtn_handle_function_call(struct vtn_builder *b, SpvOp opcode,
1072 const uint32_t *w, unsigned count)
1073 {
1074 struct nir_function *callee =
1075 vtn_value(b, w[3], vtn_value_type_function)->func->impl->function;
1076
1077 nir_call_instr *call = nir_call_instr_create(b->nb.shader, callee);
1078 for (unsigned i = 0; i < call->num_params; i++) {
1079 unsigned arg_id = w[4 + i];
1080 struct vtn_value *arg = vtn_untyped_value(b, arg_id);
1081 if (arg->value_type == vtn_value_type_access_chain) {
1082 nir_deref_var *d = vtn_access_chain_to_deref(b, arg->access_chain);
1083 call->params[i] = nir_deref_as_var(nir_copy_deref(call, &d->deref));
1084 } else {
1085 struct vtn_ssa_value *arg_ssa = vtn_ssa_value(b, arg_id);
1086
1087 /* Make a temporary to store the argument in */
1088 nir_variable *tmp =
1089 nir_local_variable_create(b->impl, arg_ssa->type, "arg_tmp");
1090 call->params[i] = nir_deref_var_create(call, tmp);
1091
1092 vtn_local_store(b, arg_ssa, call->params[i]);
1093 }
1094 }
1095
1096 nir_variable *out_tmp = NULL;
1097 if (!glsl_type_is_void(callee->return_type)) {
1098 out_tmp = nir_local_variable_create(b->impl, callee->return_type,
1099 "out_tmp");
1100 call->return_deref = nir_deref_var_create(call, out_tmp);
1101 }
1102
1103 nir_builder_instr_insert(&b->nb, &call->instr);
1104
1105 if (glsl_type_is_void(callee->return_type)) {
1106 vtn_push_value(b, w[2], vtn_value_type_undef);
1107 } else {
1108 struct vtn_value *retval = vtn_push_value(b, w[2], vtn_value_type_ssa);
1109 retval->ssa = vtn_local_load(b, call->return_deref);
1110 }
1111 }
1112
1113 struct vtn_ssa_value *
1114 vtn_create_ssa_value(struct vtn_builder *b, const struct glsl_type *type)
1115 {
1116 struct vtn_ssa_value *val = rzalloc(b, struct vtn_ssa_value);
1117 val->type = type;
1118
1119 if (!glsl_type_is_vector_or_scalar(type)) {
1120 unsigned elems = glsl_get_length(type);
1121 val->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
1122 for (unsigned i = 0; i < elems; i++) {
1123 const struct glsl_type *child_type;
1124
1125 switch (glsl_get_base_type(type)) {
1126 case GLSL_TYPE_INT:
1127 case GLSL_TYPE_UINT:
1128 case GLSL_TYPE_BOOL:
1129 case GLSL_TYPE_FLOAT:
1130 case GLSL_TYPE_DOUBLE:
1131 child_type = glsl_get_column_type(type);
1132 break;
1133 case GLSL_TYPE_ARRAY:
1134 child_type = glsl_get_array_element(type);
1135 break;
1136 case GLSL_TYPE_STRUCT:
1137 child_type = glsl_get_struct_field(type, i);
1138 break;
1139 default:
1140 unreachable("unkown base type");
1141 }
1142
1143 val->elems[i] = vtn_create_ssa_value(b, child_type);
1144 }
1145 }
1146
1147 return val;
1148 }
1149
1150 static nir_tex_src
1151 vtn_tex_src(struct vtn_builder *b, unsigned index, nir_tex_src_type type)
1152 {
1153 nir_tex_src src;
1154 src.src = nir_src_for_ssa(vtn_ssa_value(b, index)->def);
1155 src.src_type = type;
1156 return src;
1157 }
1158
1159 static void
1160 vtn_handle_texture(struct vtn_builder *b, SpvOp opcode,
1161 const uint32_t *w, unsigned count)
1162 {
1163 if (opcode == SpvOpSampledImage) {
1164 struct vtn_value *val =
1165 vtn_push_value(b, w[2], vtn_value_type_sampled_image);
1166 val->sampled_image = ralloc(b, struct vtn_sampled_image);
1167 val->sampled_image->image =
1168 vtn_value(b, w[3], vtn_value_type_access_chain)->access_chain;
1169 val->sampled_image->sampler =
1170 vtn_value(b, w[4], vtn_value_type_access_chain)->access_chain;
1171 return;
1172 } else if (opcode == SpvOpImage) {
1173 struct vtn_value *val =
1174 vtn_push_value(b, w[2], vtn_value_type_access_chain);
1175 struct vtn_value *src_val = vtn_untyped_value(b, w[3]);
1176 if (src_val->value_type == vtn_value_type_sampled_image) {
1177 val->access_chain = src_val->sampled_image->image;
1178 } else {
1179 assert(src_val->value_type == vtn_value_type_access_chain);
1180 val->access_chain = src_val->access_chain;
1181 }
1182 return;
1183 }
1184
1185 struct vtn_type *ret_type = vtn_value(b, w[1], vtn_value_type_type)->type;
1186 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
1187
1188 struct vtn_sampled_image sampled;
1189 struct vtn_value *sampled_val = vtn_untyped_value(b, w[3]);
1190 if (sampled_val->value_type == vtn_value_type_sampled_image) {
1191 sampled = *sampled_val->sampled_image;
1192 } else {
1193 assert(sampled_val->value_type == vtn_value_type_access_chain);
1194 sampled.image = NULL;
1195 sampled.sampler = sampled_val->access_chain;
1196 }
1197
1198 nir_tex_src srcs[8]; /* 8 should be enough */
1199 nir_tex_src *p = srcs;
1200
1201 unsigned idx = 4;
1202
1203 bool has_coord = false;
1204 switch (opcode) {
1205 case SpvOpImageSampleImplicitLod:
1206 case SpvOpImageSampleExplicitLod:
1207 case SpvOpImageSampleDrefImplicitLod:
1208 case SpvOpImageSampleDrefExplicitLod:
1209 case SpvOpImageSampleProjImplicitLod:
1210 case SpvOpImageSampleProjExplicitLod:
1211 case SpvOpImageSampleProjDrefImplicitLod:
1212 case SpvOpImageSampleProjDrefExplicitLod:
1213 case SpvOpImageFetch:
1214 case SpvOpImageGather:
1215 case SpvOpImageDrefGather:
1216 case SpvOpImageQueryLod: {
1217 /* All these types have the coordinate as their first real argument */
1218 struct vtn_ssa_value *coord = vtn_ssa_value(b, w[idx++]);
1219 has_coord = true;
1220 p->src = nir_src_for_ssa(coord->def);
1221 p->src_type = nir_tex_src_coord;
1222 p++;
1223 break;
1224 }
1225
1226 default:
1227 break;
1228 }
1229
1230 /* These all have an explicit depth value as their next source */
1231 switch (opcode) {
1232 case SpvOpImageSampleDrefImplicitLod:
1233 case SpvOpImageSampleDrefExplicitLod:
1234 case SpvOpImageSampleProjDrefImplicitLod:
1235 case SpvOpImageSampleProjDrefExplicitLod:
1236 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_comparitor);
1237 break;
1238 default:
1239 break;
1240 }
1241
1242 /* For OpImageQuerySizeLod, we always have an LOD */
1243 if (opcode == SpvOpImageQuerySizeLod)
1244 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_lod);
1245
1246 /* Figure out the base texture operation */
1247 nir_texop texop;
1248 switch (opcode) {
1249 case SpvOpImageSampleImplicitLod:
1250 case SpvOpImageSampleDrefImplicitLod:
1251 case SpvOpImageSampleProjImplicitLod:
1252 case SpvOpImageSampleProjDrefImplicitLod:
1253 texop = nir_texop_tex;
1254 break;
1255
1256 case SpvOpImageSampleExplicitLod:
1257 case SpvOpImageSampleDrefExplicitLod:
1258 case SpvOpImageSampleProjExplicitLod:
1259 case SpvOpImageSampleProjDrefExplicitLod:
1260 texop = nir_texop_txl;
1261 break;
1262
1263 case SpvOpImageFetch:
1264 texop = nir_texop_txf;
1265 break;
1266
1267 case SpvOpImageGather:
1268 case SpvOpImageDrefGather:
1269 texop = nir_texop_tg4;
1270 break;
1271
1272 case SpvOpImageQuerySizeLod:
1273 case SpvOpImageQuerySize:
1274 texop = nir_texop_txs;
1275 break;
1276
1277 case SpvOpImageQueryLod:
1278 texop = nir_texop_lod;
1279 break;
1280
1281 case SpvOpImageQueryLevels:
1282 texop = nir_texop_query_levels;
1283 break;
1284
1285 case SpvOpImageQuerySamples:
1286 default:
1287 unreachable("Unhandled opcode");
1288 }
1289
1290 nir_constant *const_offset = NULL;
1291
1292 /* Now we need to handle some number of optional arguments */
1293 if (idx < count) {
1294 uint32_t operands = w[idx++];
1295
1296 if (operands & SpvImageOperandsBiasMask) {
1297 assert(texop == nir_texop_tex);
1298 texop = nir_texop_txb;
1299 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_bias);
1300 }
1301
1302 if (operands & SpvImageOperandsLodMask) {
1303 assert(texop == nir_texop_txl || texop == nir_texop_txf ||
1304 texop == nir_texop_txs);
1305 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_lod);
1306 }
1307
1308 if (operands & SpvImageOperandsGradMask) {
1309 assert(texop == nir_texop_tex);
1310 texop = nir_texop_txd;
1311 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_ddx);
1312 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_ddy);
1313 }
1314
1315 if (operands & SpvImageOperandsConstOffsetMask) {
1316 const_offset =
1317 vtn_value(b, w[idx++], vtn_value_type_constant)->constant;
1318 }
1319
1320 if (operands & SpvImageOperandsOffsetMask)
1321 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_offset);
1322
1323 if (operands & SpvImageOperandsConstOffsetsMask)
1324 assert(!"Constant offsets to texture gather not yet implemented");
1325
1326 if (operands & SpvImageOperandsSampleMask) {
1327 assert(texop == nir_texop_txf);
1328 texop = nir_texop_txf_ms;
1329 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_ms_index);
1330 }
1331 }
1332 /* We should have now consumed exactly all of the arguments */
1333 assert(idx == count);
1334
1335 nir_tex_instr *instr = nir_tex_instr_create(b->shader, p - srcs);
1336 instr->op = texop;
1337
1338 memcpy(instr->src, srcs, instr->num_srcs * sizeof(*instr->src));
1339
1340 const struct glsl_type *image_type;
1341 if (sampled.image) {
1342 image_type = sampled.image->var->var->interface_type;
1343 } else {
1344 image_type = sampled.sampler->var->var->interface_type;
1345 }
1346
1347 instr->sampler_dim = glsl_get_sampler_dim(image_type);
1348 instr->is_array = glsl_sampler_type_is_array(image_type);
1349 instr->is_shadow = glsl_sampler_type_is_shadow(image_type);
1350 instr->is_new_style_shadow = instr->is_shadow;
1351
1352 if (const_offset) {
1353 for (unsigned i = 0; i < 4; i++)
1354 instr->const_offset[i] = const_offset->value.u[i];
1355 }
1356
1357 if (has_coord) {
1358 switch (instr->sampler_dim) {
1359 case GLSL_SAMPLER_DIM_1D:
1360 case GLSL_SAMPLER_DIM_BUF:
1361 instr->coord_components = 1;
1362 break;
1363 case GLSL_SAMPLER_DIM_2D:
1364 case GLSL_SAMPLER_DIM_RECT:
1365 instr->coord_components = 2;
1366 break;
1367 case GLSL_SAMPLER_DIM_3D:
1368 case GLSL_SAMPLER_DIM_CUBE:
1369 case GLSL_SAMPLER_DIM_MS:
1370 instr->coord_components = 3;
1371 break;
1372 default:
1373 assert("Invalid sampler type");
1374 }
1375
1376 if (instr->is_array)
1377 instr->coord_components++;
1378 } else {
1379 instr->coord_components = 0;
1380 }
1381
1382 switch (glsl_get_sampler_result_type(image_type)) {
1383 case GLSL_TYPE_FLOAT: instr->dest_type = nir_type_float; break;
1384 case GLSL_TYPE_INT: instr->dest_type = nir_type_int; break;
1385 case GLSL_TYPE_UINT: instr->dest_type = nir_type_uint; break;
1386 case GLSL_TYPE_BOOL: instr->dest_type = nir_type_bool; break;
1387 default:
1388 unreachable("Invalid base type for sampler result");
1389 }
1390
1391 nir_deref_var *sampler = vtn_access_chain_to_deref(b, sampled.sampler);
1392 instr->sampler = nir_deref_as_var(nir_copy_deref(instr, &sampler->deref));
1393 if (sampled.image) {
1394 nir_deref_var *image = vtn_access_chain_to_deref(b, sampled.image);
1395 instr->texture = nir_deref_as_var(nir_copy_deref(instr, &image->deref));
1396 } else {
1397 instr->texture = NULL;
1398 }
1399
1400 nir_ssa_dest_init(&instr->instr, &instr->dest,
1401 nir_tex_instr_dest_size(instr), NULL);
1402
1403 assert(glsl_get_vector_elements(ret_type->type) ==
1404 nir_tex_instr_dest_size(instr));
1405
1406 val->ssa = vtn_create_ssa_value(b, ret_type->type);
1407 val->ssa->def = &instr->dest.ssa;
1408
1409 nir_builder_instr_insert(&b->nb, &instr->instr);
1410 }
1411
1412 static nir_ssa_def *
1413 get_image_coord(struct vtn_builder *b, uint32_t value)
1414 {
1415 struct vtn_ssa_value *coord = vtn_ssa_value(b, value);
1416
1417 /* The image_load_store intrinsics assume a 4-dim coordinate */
1418 unsigned dim = glsl_get_vector_elements(coord->type);
1419 unsigned swizzle[4];
1420 for (unsigned i = 0; i < 4; i++)
1421 swizzle[i] = MIN2(i, dim - 1);
1422
1423 return nir_swizzle(&b->nb, coord->def, swizzle, 4, false);
1424 }
1425
1426 static void
1427 vtn_handle_image(struct vtn_builder *b, SpvOp opcode,
1428 const uint32_t *w, unsigned count)
1429 {
1430 /* Just get this one out of the way */
1431 if (opcode == SpvOpImageTexelPointer) {
1432 struct vtn_value *val =
1433 vtn_push_value(b, w[2], vtn_value_type_image_pointer);
1434 val->image = ralloc(b, struct vtn_image_pointer);
1435
1436 val->image->image =
1437 vtn_value(b, w[3], vtn_value_type_access_chain)->access_chain;
1438 val->image->coord = get_image_coord(b, w[4]);
1439 val->image->sample = vtn_ssa_value(b, w[5])->def;
1440 return;
1441 }
1442
1443 struct vtn_image_pointer image;
1444
1445 switch (opcode) {
1446 case SpvOpAtomicExchange:
1447 case SpvOpAtomicCompareExchange:
1448 case SpvOpAtomicCompareExchangeWeak:
1449 case SpvOpAtomicIIncrement:
1450 case SpvOpAtomicIDecrement:
1451 case SpvOpAtomicIAdd:
1452 case SpvOpAtomicISub:
1453 case SpvOpAtomicSMin:
1454 case SpvOpAtomicUMin:
1455 case SpvOpAtomicSMax:
1456 case SpvOpAtomicUMax:
1457 case SpvOpAtomicAnd:
1458 case SpvOpAtomicOr:
1459 case SpvOpAtomicXor:
1460 image = *vtn_value(b, w[3], vtn_value_type_image_pointer)->image;
1461 break;
1462
1463 case SpvOpImageQuerySize:
1464 image.image =
1465 vtn_value(b, w[3], vtn_value_type_access_chain)->access_chain;
1466 image.coord = NULL;
1467 image.sample = NULL;
1468 break;
1469
1470 case SpvOpImageRead:
1471 image.image =
1472 vtn_value(b, w[3], vtn_value_type_access_chain)->access_chain;
1473 image.coord = get_image_coord(b, w[4]);
1474
1475 if (count > 5 && (w[5] & SpvImageOperandsSampleMask)) {
1476 assert(w[5] == SpvImageOperandsSampleMask);
1477 image.sample = vtn_ssa_value(b, w[6])->def;
1478 } else {
1479 image.sample = nir_ssa_undef(&b->nb, 1);
1480 }
1481 break;
1482
1483 case SpvOpImageWrite:
1484 image.image =
1485 vtn_value(b, w[1], vtn_value_type_access_chain)->access_chain;
1486 image.coord = get_image_coord(b, w[2]);
1487
1488 /* texel = w[3] */
1489
1490 if (count > 4 && (w[4] & SpvImageOperandsSampleMask)) {
1491 assert(w[4] == SpvImageOperandsSampleMask);
1492 image.sample = vtn_ssa_value(b, w[5])->def;
1493 } else {
1494 image.sample = nir_ssa_undef(&b->nb, 1);
1495 }
1496 break;
1497
1498 default:
1499 unreachable("Invalid image opcode");
1500 }
1501
1502 nir_intrinsic_op op;
1503 switch (opcode) {
1504 #define OP(S, N) case SpvOp##S: op = nir_intrinsic_image_##N; break;
1505 OP(ImageQuerySize, size)
1506 OP(ImageRead, load)
1507 OP(ImageWrite, store)
1508 OP(AtomicExchange, atomic_exchange)
1509 OP(AtomicCompareExchange, atomic_comp_swap)
1510 OP(AtomicIIncrement, atomic_add)
1511 OP(AtomicIDecrement, atomic_add)
1512 OP(AtomicIAdd, atomic_add)
1513 OP(AtomicISub, atomic_add)
1514 OP(AtomicSMin, atomic_min)
1515 OP(AtomicUMin, atomic_min)
1516 OP(AtomicSMax, atomic_max)
1517 OP(AtomicUMax, atomic_max)
1518 OP(AtomicAnd, atomic_and)
1519 OP(AtomicOr, atomic_or)
1520 OP(AtomicXor, atomic_xor)
1521 #undef OP
1522 default:
1523 unreachable("Invalid image opcode");
1524 }
1525
1526 nir_intrinsic_instr *intrin = nir_intrinsic_instr_create(b->shader, op);
1527
1528 nir_deref_var *image_deref = vtn_access_chain_to_deref(b, image.image);
1529 intrin->variables[0] =
1530 nir_deref_as_var(nir_copy_deref(&intrin->instr, &image_deref->deref));
1531
1532 /* ImageQuerySize doesn't take any extra parameters */
1533 if (opcode != SpvOpImageQuerySize) {
1534 /* The image coordinate is always 4 components but we may not have that
1535 * many. Swizzle to compensate.
1536 */
1537 unsigned swiz[4];
1538 for (unsigned i = 0; i < 4; i++)
1539 swiz[i] = i < image.coord->num_components ? i : 0;
1540 intrin->src[0] = nir_src_for_ssa(nir_swizzle(&b->nb, image.coord,
1541 swiz, 4, false));
1542 intrin->src[1] = nir_src_for_ssa(image.sample);
1543 }
1544
1545 switch (opcode) {
1546 case SpvOpImageQuerySize:
1547 case SpvOpImageRead:
1548 break;
1549 case SpvOpImageWrite:
1550 intrin->src[2] = nir_src_for_ssa(vtn_ssa_value(b, w[3])->def);
1551 break;
1552 case SpvOpAtomicIIncrement:
1553 intrin->src[2] = nir_src_for_ssa(nir_imm_int(&b->nb, 1));
1554 break;
1555 case SpvOpAtomicIDecrement:
1556 intrin->src[2] = nir_src_for_ssa(nir_imm_int(&b->nb, -1));
1557 break;
1558
1559 case SpvOpAtomicExchange:
1560 case SpvOpAtomicIAdd:
1561 case SpvOpAtomicSMin:
1562 case SpvOpAtomicUMin:
1563 case SpvOpAtomicSMax:
1564 case SpvOpAtomicUMax:
1565 case SpvOpAtomicAnd:
1566 case SpvOpAtomicOr:
1567 case SpvOpAtomicXor:
1568 intrin->src[2] = nir_src_for_ssa(vtn_ssa_value(b, w[6])->def);
1569 break;
1570
1571 case SpvOpAtomicCompareExchange:
1572 intrin->src[2] = nir_src_for_ssa(vtn_ssa_value(b, w[7])->def);
1573 intrin->src[3] = nir_src_for_ssa(vtn_ssa_value(b, w[6])->def);
1574 break;
1575
1576 case SpvOpAtomicISub:
1577 intrin->src[2] = nir_src_for_ssa(nir_ineg(&b->nb, vtn_ssa_value(b, w[6])->def));
1578 break;
1579
1580 default:
1581 unreachable("Invalid image opcode");
1582 }
1583
1584 if (opcode != SpvOpImageWrite) {
1585 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
1586 struct vtn_type *type = vtn_value(b, w[1], vtn_value_type_type)->type;
1587 nir_ssa_dest_init(&intrin->instr, &intrin->dest, 4, NULL);
1588
1589 nir_builder_instr_insert(&b->nb, &intrin->instr);
1590
1591 /* The image intrinsics always return 4 channels but we may not want
1592 * that many. Emit a mov to trim it down.
1593 */
1594 unsigned swiz[4] = {0, 1, 2, 3};
1595 val->ssa = vtn_create_ssa_value(b, type->type);
1596 val->ssa->def = nir_swizzle(&b->nb, &intrin->dest.ssa, swiz,
1597 glsl_get_vector_elements(type->type), false);
1598 } else {
1599 nir_builder_instr_insert(&b->nb, &intrin->instr);
1600 }
1601 }
1602
1603 static nir_intrinsic_op
1604 get_ssbo_nir_atomic_op(SpvOp opcode)
1605 {
1606 switch (opcode) {
1607 #define OP(S, N) case SpvOp##S: return nir_intrinsic_ssbo_##N;
1608 OP(AtomicExchange, atomic_exchange)
1609 OP(AtomicCompareExchange, atomic_comp_swap)
1610 OP(AtomicIIncrement, atomic_add)
1611 OP(AtomicIDecrement, atomic_add)
1612 OP(AtomicIAdd, atomic_add)
1613 OP(AtomicISub, atomic_add)
1614 OP(AtomicSMin, atomic_imin)
1615 OP(AtomicUMin, atomic_umin)
1616 OP(AtomicSMax, atomic_imax)
1617 OP(AtomicUMax, atomic_umax)
1618 OP(AtomicAnd, atomic_and)
1619 OP(AtomicOr, atomic_or)
1620 OP(AtomicXor, atomic_xor)
1621 #undef OP
1622 default:
1623 unreachable("Invalid SSBO atomic");
1624 }
1625 }
1626
1627 static nir_intrinsic_op
1628 get_shared_nir_atomic_op(SpvOp opcode)
1629 {
1630 switch (opcode) {
1631 #define OP(S, N) case SpvOp##S: return nir_intrinsic_var_##N;
1632 OP(AtomicExchange, atomic_exchange)
1633 OP(AtomicCompareExchange, atomic_comp_swap)
1634 OP(AtomicIIncrement, atomic_add)
1635 OP(AtomicIDecrement, atomic_add)
1636 OP(AtomicIAdd, atomic_add)
1637 OP(AtomicISub, atomic_add)
1638 OP(AtomicSMin, atomic_imin)
1639 OP(AtomicUMin, atomic_umin)
1640 OP(AtomicSMax, atomic_imax)
1641 OP(AtomicUMax, atomic_umax)
1642 OP(AtomicAnd, atomic_and)
1643 OP(AtomicOr, atomic_or)
1644 OP(AtomicXor, atomic_xor)
1645 #undef OP
1646 default:
1647 unreachable("Invalid shared atomic");
1648 }
1649 }
1650
1651 static void
1652 fill_common_atomic_sources(struct vtn_builder *b, SpvOp opcode,
1653 const uint32_t *w, nir_src *src)
1654 {
1655 switch (opcode) {
1656 case SpvOpAtomicIIncrement:
1657 src[0] = nir_src_for_ssa(nir_imm_int(&b->nb, 1));
1658 break;
1659
1660 case SpvOpAtomicIDecrement:
1661 src[0] = nir_src_for_ssa(nir_imm_int(&b->nb, -1));
1662 break;
1663
1664 case SpvOpAtomicISub:
1665 src[0] =
1666 nir_src_for_ssa(nir_ineg(&b->nb, vtn_ssa_value(b, w[6])->def));
1667 break;
1668
1669 case SpvOpAtomicCompareExchange:
1670 src[0] = nir_src_for_ssa(vtn_ssa_value(b, w[7])->def);
1671 src[1] = nir_src_for_ssa(vtn_ssa_value(b, w[8])->def);
1672 break;
1673 /* Fall through */
1674
1675 case SpvOpAtomicExchange:
1676 case SpvOpAtomicIAdd:
1677 case SpvOpAtomicSMin:
1678 case SpvOpAtomicUMin:
1679 case SpvOpAtomicSMax:
1680 case SpvOpAtomicUMax:
1681 case SpvOpAtomicAnd:
1682 case SpvOpAtomicOr:
1683 case SpvOpAtomicXor:
1684 src[0] = nir_src_for_ssa(vtn_ssa_value(b, w[6])->def);
1685 break;
1686
1687 default:
1688 unreachable("Invalid SPIR-V atomic");
1689 }
1690 }
1691
1692 static void
1693 vtn_handle_ssbo_or_shared_atomic(struct vtn_builder *b, SpvOp opcode,
1694 const uint32_t *w, unsigned count)
1695 {
1696 struct vtn_access_chain *chain =
1697 vtn_value(b, w[3], vtn_value_type_access_chain)->access_chain;
1698 nir_intrinsic_instr *atomic;
1699
1700 /*
1701 SpvScope scope = w[4];
1702 SpvMemorySemanticsMask semantics = w[5];
1703 */
1704
1705 if (chain->var->mode == vtn_variable_mode_workgroup) {
1706 nir_deref *deref = &vtn_access_chain_to_deref(b, chain)->deref;
1707 nir_intrinsic_op op = get_shared_nir_atomic_op(opcode);
1708 atomic = nir_intrinsic_instr_create(b->nb.shader, op);
1709 atomic->variables[0] = nir_deref_as_var(nir_copy_deref(atomic, deref));
1710 fill_common_atomic_sources(b, opcode, w, &atomic->src[0]);
1711 } else {
1712 assert(chain->var->mode == vtn_variable_mode_ssbo);
1713 struct vtn_type *type;
1714 nir_ssa_def *offset, *index;
1715 offset = vtn_access_chain_to_offset(b, chain, &index, &type, NULL, false);
1716
1717 nir_intrinsic_op op = get_ssbo_nir_atomic_op(opcode);
1718
1719 atomic = nir_intrinsic_instr_create(b->nb.shader, op);
1720 atomic->src[0] = nir_src_for_ssa(index);
1721 atomic->src[1] = nir_src_for_ssa(offset);
1722 fill_common_atomic_sources(b, opcode, w, &atomic->src[2]);
1723 }
1724
1725 nir_ssa_dest_init(&atomic->instr, &atomic->dest, 1, NULL);
1726
1727 struct vtn_type *type = vtn_value(b, w[1], vtn_value_type_type)->type;
1728 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
1729 val->ssa = rzalloc(b, struct vtn_ssa_value);
1730 val->ssa->def = &atomic->dest.ssa;
1731 val->ssa->type = type->type;
1732
1733 nir_builder_instr_insert(&b->nb, &atomic->instr);
1734 }
1735
1736 static nir_alu_instr *
1737 create_vec(nir_shader *shader, unsigned num_components)
1738 {
1739 nir_op op;
1740 switch (num_components) {
1741 case 1: op = nir_op_fmov; break;
1742 case 2: op = nir_op_vec2; break;
1743 case 3: op = nir_op_vec3; break;
1744 case 4: op = nir_op_vec4; break;
1745 default: unreachable("bad vector size");
1746 }
1747
1748 nir_alu_instr *vec = nir_alu_instr_create(shader, op);
1749 nir_ssa_dest_init(&vec->instr, &vec->dest.dest, num_components, NULL);
1750 vec->dest.write_mask = (1 << num_components) - 1;
1751
1752 return vec;
1753 }
1754
1755 struct vtn_ssa_value *
1756 vtn_ssa_transpose(struct vtn_builder *b, struct vtn_ssa_value *src)
1757 {
1758 if (src->transposed)
1759 return src->transposed;
1760
1761 struct vtn_ssa_value *dest =
1762 vtn_create_ssa_value(b, glsl_transposed_type(src->type));
1763
1764 for (unsigned i = 0; i < glsl_get_matrix_columns(dest->type); i++) {
1765 nir_alu_instr *vec = create_vec(b->shader,
1766 glsl_get_matrix_columns(src->type));
1767 if (glsl_type_is_vector_or_scalar(src->type)) {
1768 vec->src[0].src = nir_src_for_ssa(src->def);
1769 vec->src[0].swizzle[0] = i;
1770 } else {
1771 for (unsigned j = 0; j < glsl_get_matrix_columns(src->type); j++) {
1772 vec->src[j].src = nir_src_for_ssa(src->elems[j]->def);
1773 vec->src[j].swizzle[0] = i;
1774 }
1775 }
1776 nir_builder_instr_insert(&b->nb, &vec->instr);
1777 dest->elems[i]->def = &vec->dest.dest.ssa;
1778 }
1779
1780 dest->transposed = src;
1781
1782 return dest;
1783 }
1784
1785 nir_ssa_def *
1786 vtn_vector_extract(struct vtn_builder *b, nir_ssa_def *src, unsigned index)
1787 {
1788 unsigned swiz[4] = { index };
1789 return nir_swizzle(&b->nb, src, swiz, 1, true);
1790 }
1791
1792 nir_ssa_def *
1793 vtn_vector_insert(struct vtn_builder *b, nir_ssa_def *src, nir_ssa_def *insert,
1794 unsigned index)
1795 {
1796 nir_alu_instr *vec = create_vec(b->shader, src->num_components);
1797
1798 for (unsigned i = 0; i < src->num_components; i++) {
1799 if (i == index) {
1800 vec->src[i].src = nir_src_for_ssa(insert);
1801 } else {
1802 vec->src[i].src = nir_src_for_ssa(src);
1803 vec->src[i].swizzle[0] = i;
1804 }
1805 }
1806
1807 nir_builder_instr_insert(&b->nb, &vec->instr);
1808
1809 return &vec->dest.dest.ssa;
1810 }
1811
1812 nir_ssa_def *
1813 vtn_vector_extract_dynamic(struct vtn_builder *b, nir_ssa_def *src,
1814 nir_ssa_def *index)
1815 {
1816 nir_ssa_def *dest = vtn_vector_extract(b, src, 0);
1817 for (unsigned i = 1; i < src->num_components; i++)
1818 dest = nir_bcsel(&b->nb, nir_ieq(&b->nb, index, nir_imm_int(&b->nb, i)),
1819 vtn_vector_extract(b, src, i), dest);
1820
1821 return dest;
1822 }
1823
1824 nir_ssa_def *
1825 vtn_vector_insert_dynamic(struct vtn_builder *b, nir_ssa_def *src,
1826 nir_ssa_def *insert, nir_ssa_def *index)
1827 {
1828 nir_ssa_def *dest = vtn_vector_insert(b, src, insert, 0);
1829 for (unsigned i = 1; i < src->num_components; i++)
1830 dest = nir_bcsel(&b->nb, nir_ieq(&b->nb, index, nir_imm_int(&b->nb, i)),
1831 vtn_vector_insert(b, src, insert, i), dest);
1832
1833 return dest;
1834 }
1835
1836 static nir_ssa_def *
1837 vtn_vector_shuffle(struct vtn_builder *b, unsigned num_components,
1838 nir_ssa_def *src0, nir_ssa_def *src1,
1839 const uint32_t *indices)
1840 {
1841 nir_alu_instr *vec = create_vec(b->shader, num_components);
1842
1843 nir_ssa_undef_instr *undef = nir_ssa_undef_instr_create(b->shader, 1);
1844 nir_builder_instr_insert(&b->nb, &undef->instr);
1845
1846 for (unsigned i = 0; i < num_components; i++) {
1847 uint32_t index = indices[i];
1848 if (index == 0xffffffff) {
1849 vec->src[i].src = nir_src_for_ssa(&undef->def);
1850 } else if (index < src0->num_components) {
1851 vec->src[i].src = nir_src_for_ssa(src0);
1852 vec->src[i].swizzle[0] = index;
1853 } else {
1854 vec->src[i].src = nir_src_for_ssa(src1);
1855 vec->src[i].swizzle[0] = index - src0->num_components;
1856 }
1857 }
1858
1859 nir_builder_instr_insert(&b->nb, &vec->instr);
1860
1861 return &vec->dest.dest.ssa;
1862 }
1863
1864 /*
1865 * Concatentates a number of vectors/scalars together to produce a vector
1866 */
1867 static nir_ssa_def *
1868 vtn_vector_construct(struct vtn_builder *b, unsigned num_components,
1869 unsigned num_srcs, nir_ssa_def **srcs)
1870 {
1871 nir_alu_instr *vec = create_vec(b->shader, num_components);
1872
1873 unsigned dest_idx = 0;
1874 for (unsigned i = 0; i < num_srcs; i++) {
1875 nir_ssa_def *src = srcs[i];
1876 for (unsigned j = 0; j < src->num_components; j++) {
1877 vec->src[dest_idx].src = nir_src_for_ssa(src);
1878 vec->src[dest_idx].swizzle[0] = j;
1879 dest_idx++;
1880 }
1881 }
1882
1883 nir_builder_instr_insert(&b->nb, &vec->instr);
1884
1885 return &vec->dest.dest.ssa;
1886 }
1887
1888 static struct vtn_ssa_value *
1889 vtn_composite_copy(void *mem_ctx, struct vtn_ssa_value *src)
1890 {
1891 struct vtn_ssa_value *dest = rzalloc(mem_ctx, struct vtn_ssa_value);
1892 dest->type = src->type;
1893
1894 if (glsl_type_is_vector_or_scalar(src->type)) {
1895 dest->def = src->def;
1896 } else {
1897 unsigned elems = glsl_get_length(src->type);
1898
1899 dest->elems = ralloc_array(mem_ctx, struct vtn_ssa_value *, elems);
1900 for (unsigned i = 0; i < elems; i++)
1901 dest->elems[i] = vtn_composite_copy(mem_ctx, src->elems[i]);
1902 }
1903
1904 return dest;
1905 }
1906
1907 static struct vtn_ssa_value *
1908 vtn_composite_insert(struct vtn_builder *b, struct vtn_ssa_value *src,
1909 struct vtn_ssa_value *insert, const uint32_t *indices,
1910 unsigned num_indices)
1911 {
1912 struct vtn_ssa_value *dest = vtn_composite_copy(b, src);
1913
1914 struct vtn_ssa_value *cur = dest;
1915 unsigned i;
1916 for (i = 0; i < num_indices - 1; i++) {
1917 cur = cur->elems[indices[i]];
1918 }
1919
1920 if (glsl_type_is_vector_or_scalar(cur->type)) {
1921 /* According to the SPIR-V spec, OpCompositeInsert may work down to
1922 * the component granularity. In that case, the last index will be
1923 * the index to insert the scalar into the vector.
1924 */
1925
1926 cur->def = vtn_vector_insert(b, cur->def, insert->def, indices[i]);
1927 } else {
1928 cur->elems[indices[i]] = insert;
1929 }
1930
1931 return dest;
1932 }
1933
1934 static struct vtn_ssa_value *
1935 vtn_composite_extract(struct vtn_builder *b, struct vtn_ssa_value *src,
1936 const uint32_t *indices, unsigned num_indices)
1937 {
1938 struct vtn_ssa_value *cur = src;
1939 for (unsigned i = 0; i < num_indices; i++) {
1940 if (glsl_type_is_vector_or_scalar(cur->type)) {
1941 assert(i == num_indices - 1);
1942 /* According to the SPIR-V spec, OpCompositeExtract may work down to
1943 * the component granularity. The last index will be the index of the
1944 * vector to extract.
1945 */
1946
1947 struct vtn_ssa_value *ret = rzalloc(b, struct vtn_ssa_value);
1948 ret->type = glsl_scalar_type(glsl_get_base_type(cur->type));
1949 ret->def = vtn_vector_extract(b, cur->def, indices[i]);
1950 return ret;
1951 } else {
1952 cur = cur->elems[indices[i]];
1953 }
1954 }
1955
1956 return cur;
1957 }
1958
1959 static void
1960 vtn_handle_composite(struct vtn_builder *b, SpvOp opcode,
1961 const uint32_t *w, unsigned count)
1962 {
1963 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
1964 const struct glsl_type *type =
1965 vtn_value(b, w[1], vtn_value_type_type)->type->type;
1966 val->ssa = vtn_create_ssa_value(b, type);
1967
1968 switch (opcode) {
1969 case SpvOpVectorExtractDynamic:
1970 val->ssa->def = vtn_vector_extract_dynamic(b, vtn_ssa_value(b, w[3])->def,
1971 vtn_ssa_value(b, w[4])->def);
1972 break;
1973
1974 case SpvOpVectorInsertDynamic:
1975 val->ssa->def = vtn_vector_insert_dynamic(b, vtn_ssa_value(b, w[3])->def,
1976 vtn_ssa_value(b, w[4])->def,
1977 vtn_ssa_value(b, w[5])->def);
1978 break;
1979
1980 case SpvOpVectorShuffle:
1981 val->ssa->def = vtn_vector_shuffle(b, glsl_get_vector_elements(type),
1982 vtn_ssa_value(b, w[3])->def,
1983 vtn_ssa_value(b, w[4])->def,
1984 w + 5);
1985 break;
1986
1987 case SpvOpCompositeConstruct: {
1988 unsigned elems = count - 3;
1989 if (glsl_type_is_vector_or_scalar(type)) {
1990 nir_ssa_def *srcs[4];
1991 for (unsigned i = 0; i < elems; i++)
1992 srcs[i] = vtn_ssa_value(b, w[3 + i])->def;
1993 val->ssa->def =
1994 vtn_vector_construct(b, glsl_get_vector_elements(type),
1995 elems, srcs);
1996 } else {
1997 val->ssa->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
1998 for (unsigned i = 0; i < elems; i++)
1999 val->ssa->elems[i] = vtn_ssa_value(b, w[3 + i]);
2000 }
2001 break;
2002 }
2003 case SpvOpCompositeExtract:
2004 val->ssa = vtn_composite_extract(b, vtn_ssa_value(b, w[3]),
2005 w + 4, count - 4);
2006 break;
2007
2008 case SpvOpCompositeInsert:
2009 val->ssa = vtn_composite_insert(b, vtn_ssa_value(b, w[4]),
2010 vtn_ssa_value(b, w[3]),
2011 w + 5, count - 5);
2012 break;
2013
2014 case SpvOpCopyObject:
2015 val->ssa = vtn_composite_copy(b, vtn_ssa_value(b, w[3]));
2016 break;
2017
2018 default:
2019 unreachable("unknown composite operation");
2020 }
2021 }
2022
2023 static void
2024 vtn_handle_barrier(struct vtn_builder *b, SpvOp opcode,
2025 const uint32_t *w, unsigned count)
2026 {
2027 nir_intrinsic_op intrinsic_op;
2028 switch (opcode) {
2029 case SpvOpEmitVertex:
2030 case SpvOpEmitStreamVertex:
2031 intrinsic_op = nir_intrinsic_emit_vertex;
2032 break;
2033 case SpvOpEndPrimitive:
2034 case SpvOpEndStreamPrimitive:
2035 intrinsic_op = nir_intrinsic_end_primitive;
2036 break;
2037 case SpvOpMemoryBarrier:
2038 intrinsic_op = nir_intrinsic_memory_barrier;
2039 break;
2040 case SpvOpControlBarrier:
2041 intrinsic_op = nir_intrinsic_barrier;
2042 break;
2043 default:
2044 unreachable("unknown barrier instruction");
2045 }
2046
2047 nir_intrinsic_instr *intrin =
2048 nir_intrinsic_instr_create(b->shader, intrinsic_op);
2049
2050 if (opcode == SpvOpEmitStreamVertex || opcode == SpvOpEndStreamPrimitive)
2051 intrin->const_index[0] = w[1];
2052
2053 nir_builder_instr_insert(&b->nb, &intrin->instr);
2054 }
2055
2056 static unsigned
2057 gl_primitive_from_spv_execution_mode(SpvExecutionMode mode)
2058 {
2059 switch (mode) {
2060 case SpvExecutionModeInputPoints:
2061 case SpvExecutionModeOutputPoints:
2062 return 0; /* GL_POINTS */
2063 case SpvExecutionModeInputLines:
2064 return 1; /* GL_LINES */
2065 case SpvExecutionModeInputLinesAdjacency:
2066 return 0x000A; /* GL_LINE_STRIP_ADJACENCY_ARB */
2067 case SpvExecutionModeTriangles:
2068 return 4; /* GL_TRIANGLES */
2069 case SpvExecutionModeInputTrianglesAdjacency:
2070 return 0x000C; /* GL_TRIANGLES_ADJACENCY_ARB */
2071 case SpvExecutionModeQuads:
2072 return 7; /* GL_QUADS */
2073 case SpvExecutionModeIsolines:
2074 return 0x8E7A; /* GL_ISOLINES */
2075 case SpvExecutionModeOutputLineStrip:
2076 return 3; /* GL_LINE_STRIP */
2077 case SpvExecutionModeOutputTriangleStrip:
2078 return 5; /* GL_TRIANGLE_STRIP */
2079 default:
2080 assert(!"Invalid primitive type");
2081 return 4;
2082 }
2083 }
2084
2085 static unsigned
2086 vertices_in_from_spv_execution_mode(SpvExecutionMode mode)
2087 {
2088 switch (mode) {
2089 case SpvExecutionModeInputPoints:
2090 return 1;
2091 case SpvExecutionModeInputLines:
2092 return 2;
2093 case SpvExecutionModeInputLinesAdjacency:
2094 return 4;
2095 case SpvExecutionModeTriangles:
2096 return 3;
2097 case SpvExecutionModeInputTrianglesAdjacency:
2098 return 6;
2099 default:
2100 assert(!"Invalid GS input mode");
2101 return 0;
2102 }
2103 }
2104
2105 static gl_shader_stage
2106 stage_for_execution_model(SpvExecutionModel model)
2107 {
2108 switch (model) {
2109 case SpvExecutionModelVertex:
2110 return MESA_SHADER_VERTEX;
2111 case SpvExecutionModelTessellationControl:
2112 return MESA_SHADER_TESS_CTRL;
2113 case SpvExecutionModelTessellationEvaluation:
2114 return MESA_SHADER_TESS_EVAL;
2115 case SpvExecutionModelGeometry:
2116 return MESA_SHADER_GEOMETRY;
2117 case SpvExecutionModelFragment:
2118 return MESA_SHADER_FRAGMENT;
2119 case SpvExecutionModelGLCompute:
2120 return MESA_SHADER_COMPUTE;
2121 default:
2122 unreachable("Unsupported execution model");
2123 }
2124 }
2125
2126 static bool
2127 vtn_handle_preamble_instruction(struct vtn_builder *b, SpvOp opcode,
2128 const uint32_t *w, unsigned count)
2129 {
2130 switch (opcode) {
2131 case SpvOpSource:
2132 case SpvOpSourceExtension:
2133 case SpvOpSourceContinued:
2134 case SpvOpExtension:
2135 /* Unhandled, but these are for debug so that's ok. */
2136 break;
2137
2138 case SpvOpCapability:
2139 switch ((SpvCapability)w[1]) {
2140 case SpvCapabilityMatrix:
2141 case SpvCapabilityShader:
2142 case SpvCapabilityGeometry:
2143 break;
2144 default:
2145 assert(!"Unsupported capability");
2146 }
2147 break;
2148
2149 case SpvOpExtInstImport:
2150 vtn_handle_extension(b, opcode, w, count);
2151 break;
2152
2153 case SpvOpMemoryModel:
2154 assert(w[1] == SpvAddressingModelLogical);
2155 assert(w[2] == SpvMemoryModelGLSL450);
2156 break;
2157
2158 case SpvOpEntryPoint: {
2159 struct vtn_value *entry_point = &b->values[w[2]];
2160 /* Let this be a name label regardless */
2161 unsigned name_words;
2162 entry_point->name = vtn_string_literal(b, &w[3], count - 3, &name_words);
2163
2164 if (strcmp(entry_point->name, b->entry_point_name) != 0 ||
2165 stage_for_execution_model(w[1]) != b->entry_point_stage)
2166 break;
2167
2168 assert(b->entry_point == NULL);
2169 b->entry_point = entry_point;
2170 break;
2171 }
2172
2173 case SpvOpString:
2174 vtn_push_value(b, w[1], vtn_value_type_string)->str =
2175 vtn_string_literal(b, &w[2], count - 2, NULL);
2176 break;
2177
2178 case SpvOpName:
2179 b->values[w[1]].name = vtn_string_literal(b, &w[2], count - 2, NULL);
2180 break;
2181
2182 case SpvOpMemberName:
2183 /* TODO */
2184 break;
2185
2186 case SpvOpExecutionMode:
2187 case SpvOpDecorationGroup:
2188 case SpvOpDecorate:
2189 case SpvOpMemberDecorate:
2190 case SpvOpGroupDecorate:
2191 case SpvOpGroupMemberDecorate:
2192 vtn_handle_decoration(b, opcode, w, count);
2193 break;
2194
2195 default:
2196 return false; /* End of preamble */
2197 }
2198
2199 return true;
2200 }
2201
2202 static void
2203 vtn_handle_execution_mode(struct vtn_builder *b, struct vtn_value *entry_point,
2204 const struct vtn_decoration *mode, void *data)
2205 {
2206 assert(b->entry_point == entry_point);
2207
2208 switch(mode->exec_mode) {
2209 case SpvExecutionModeOriginUpperLeft:
2210 case SpvExecutionModeOriginLowerLeft:
2211 b->origin_upper_left =
2212 (mode->exec_mode == SpvExecutionModeOriginUpperLeft);
2213 break;
2214
2215 case SpvExecutionModeEarlyFragmentTests:
2216 assert(b->shader->stage == MESA_SHADER_FRAGMENT);
2217 b->shader->info.fs.early_fragment_tests = true;
2218 break;
2219
2220 case SpvExecutionModeInvocations:
2221 assert(b->shader->stage == MESA_SHADER_GEOMETRY);
2222 b->shader->info.gs.invocations = MAX2(1, mode->literals[0]);
2223 break;
2224
2225 case SpvExecutionModeDepthReplacing:
2226 assert(b->shader->stage == MESA_SHADER_FRAGMENT);
2227 b->shader->info.fs.depth_layout = FRAG_DEPTH_LAYOUT_ANY;
2228 break;
2229 case SpvExecutionModeDepthGreater:
2230 assert(b->shader->stage == MESA_SHADER_FRAGMENT);
2231 b->shader->info.fs.depth_layout = FRAG_DEPTH_LAYOUT_GREATER;
2232 break;
2233 case SpvExecutionModeDepthLess:
2234 assert(b->shader->stage == MESA_SHADER_FRAGMENT);
2235 b->shader->info.fs.depth_layout = FRAG_DEPTH_LAYOUT_LESS;
2236 break;
2237 case SpvExecutionModeDepthUnchanged:
2238 assert(b->shader->stage == MESA_SHADER_FRAGMENT);
2239 b->shader->info.fs.depth_layout = FRAG_DEPTH_LAYOUT_UNCHANGED;
2240 break;
2241
2242 case SpvExecutionModeLocalSize:
2243 assert(b->shader->stage == MESA_SHADER_COMPUTE);
2244 b->shader->info.cs.local_size[0] = mode->literals[0];
2245 b->shader->info.cs.local_size[1] = mode->literals[1];
2246 b->shader->info.cs.local_size[2] = mode->literals[2];
2247 break;
2248 case SpvExecutionModeLocalSizeHint:
2249 break; /* Nothing do do with this */
2250
2251 case SpvExecutionModeOutputVertices:
2252 assert(b->shader->stage == MESA_SHADER_GEOMETRY);
2253 b->shader->info.gs.vertices_out = mode->literals[0];
2254 break;
2255
2256 case SpvExecutionModeInputPoints:
2257 case SpvExecutionModeInputLines:
2258 case SpvExecutionModeInputLinesAdjacency:
2259 case SpvExecutionModeTriangles:
2260 case SpvExecutionModeInputTrianglesAdjacency:
2261 case SpvExecutionModeQuads:
2262 case SpvExecutionModeIsolines:
2263 if (b->shader->stage == MESA_SHADER_GEOMETRY) {
2264 b->shader->info.gs.vertices_in =
2265 vertices_in_from_spv_execution_mode(mode->exec_mode);
2266 } else {
2267 assert(!"Tesselation shaders not yet supported");
2268 }
2269 break;
2270
2271 case SpvExecutionModeOutputPoints:
2272 case SpvExecutionModeOutputLineStrip:
2273 case SpvExecutionModeOutputTriangleStrip:
2274 assert(b->shader->stage == MESA_SHADER_GEOMETRY);
2275 b->shader->info.gs.output_primitive =
2276 gl_primitive_from_spv_execution_mode(mode->exec_mode);
2277 break;
2278
2279 case SpvExecutionModeSpacingEqual:
2280 case SpvExecutionModeSpacingFractionalEven:
2281 case SpvExecutionModeSpacingFractionalOdd:
2282 case SpvExecutionModeVertexOrderCw:
2283 case SpvExecutionModeVertexOrderCcw:
2284 case SpvExecutionModePointMode:
2285 assert(!"TODO: Add tessellation metadata");
2286 break;
2287
2288 case SpvExecutionModePixelCenterInteger:
2289 case SpvExecutionModeXfb:
2290 assert(!"Unhandled execution mode");
2291 break;
2292
2293 case SpvExecutionModeVecTypeHint:
2294 case SpvExecutionModeContractionOff:
2295 break; /* OpenCL */
2296 }
2297 }
2298
2299 static bool
2300 vtn_handle_variable_or_type_instruction(struct vtn_builder *b, SpvOp opcode,
2301 const uint32_t *w, unsigned count)
2302 {
2303 switch (opcode) {
2304 case SpvOpSource:
2305 case SpvOpSourceContinued:
2306 case SpvOpSourceExtension:
2307 case SpvOpExtension:
2308 case SpvOpCapability:
2309 case SpvOpExtInstImport:
2310 case SpvOpMemoryModel:
2311 case SpvOpEntryPoint:
2312 case SpvOpExecutionMode:
2313 case SpvOpString:
2314 case SpvOpName:
2315 case SpvOpMemberName:
2316 case SpvOpDecorationGroup:
2317 case SpvOpDecorate:
2318 case SpvOpMemberDecorate:
2319 case SpvOpGroupDecorate:
2320 case SpvOpGroupMemberDecorate:
2321 assert(!"Invalid opcode types and variables section");
2322 break;
2323
2324 case SpvOpTypeVoid:
2325 case SpvOpTypeBool:
2326 case SpvOpTypeInt:
2327 case SpvOpTypeFloat:
2328 case SpvOpTypeVector:
2329 case SpvOpTypeMatrix:
2330 case SpvOpTypeImage:
2331 case SpvOpTypeSampler:
2332 case SpvOpTypeSampledImage:
2333 case SpvOpTypeArray:
2334 case SpvOpTypeRuntimeArray:
2335 case SpvOpTypeStruct:
2336 case SpvOpTypeOpaque:
2337 case SpvOpTypePointer:
2338 case SpvOpTypeFunction:
2339 case SpvOpTypeEvent:
2340 case SpvOpTypeDeviceEvent:
2341 case SpvOpTypeReserveId:
2342 case SpvOpTypeQueue:
2343 case SpvOpTypePipe:
2344 vtn_handle_type(b, opcode, w, count);
2345 break;
2346
2347 case SpvOpConstantTrue:
2348 case SpvOpConstantFalse:
2349 case SpvOpConstant:
2350 case SpvOpConstantComposite:
2351 case SpvOpConstantSampler:
2352 case SpvOpConstantNull:
2353 case SpvOpSpecConstantTrue:
2354 case SpvOpSpecConstantFalse:
2355 case SpvOpSpecConstant:
2356 case SpvOpSpecConstantComposite:
2357 case SpvOpSpecConstantOp:
2358 vtn_handle_constant(b, opcode, w, count);
2359 break;
2360
2361 case SpvOpVariable:
2362 vtn_handle_variables(b, opcode, w, count);
2363 break;
2364
2365 default:
2366 return false; /* End of preamble */
2367 }
2368
2369 return true;
2370 }
2371
2372 static bool
2373 vtn_handle_body_instruction(struct vtn_builder *b, SpvOp opcode,
2374 const uint32_t *w, unsigned count)
2375 {
2376 switch (opcode) {
2377 case SpvOpLabel:
2378 break;
2379
2380 case SpvOpLoopMerge:
2381 case SpvOpSelectionMerge:
2382 /* This is handled by cfg pre-pass and walk_blocks */
2383 break;
2384
2385 case SpvOpUndef: {
2386 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_undef);
2387 val->type = vtn_value(b, w[1], vtn_value_type_type)->type;
2388 break;
2389 }
2390
2391 case SpvOpExtInst:
2392 vtn_handle_extension(b, opcode, w, count);
2393 break;
2394
2395 case SpvOpVariable:
2396 case SpvOpLoad:
2397 case SpvOpStore:
2398 case SpvOpCopyMemory:
2399 case SpvOpCopyMemorySized:
2400 case SpvOpAccessChain:
2401 case SpvOpInBoundsAccessChain:
2402 case SpvOpArrayLength:
2403 vtn_handle_variables(b, opcode, w, count);
2404 break;
2405
2406 case SpvOpFunctionCall:
2407 vtn_handle_function_call(b, opcode, w, count);
2408 break;
2409
2410 case SpvOpSampledImage:
2411 case SpvOpImage:
2412 case SpvOpImageSampleImplicitLod:
2413 case SpvOpImageSampleExplicitLod:
2414 case SpvOpImageSampleDrefImplicitLod:
2415 case SpvOpImageSampleDrefExplicitLod:
2416 case SpvOpImageSampleProjImplicitLod:
2417 case SpvOpImageSampleProjExplicitLod:
2418 case SpvOpImageSampleProjDrefImplicitLod:
2419 case SpvOpImageSampleProjDrefExplicitLod:
2420 case SpvOpImageFetch:
2421 case SpvOpImageGather:
2422 case SpvOpImageDrefGather:
2423 case SpvOpImageQuerySizeLod:
2424 case SpvOpImageQueryLod:
2425 case SpvOpImageQueryLevels:
2426 case SpvOpImageQuerySamples:
2427 vtn_handle_texture(b, opcode, w, count);
2428 break;
2429
2430 case SpvOpImageRead:
2431 case SpvOpImageWrite:
2432 case SpvOpImageTexelPointer:
2433 vtn_handle_image(b, opcode, w, count);
2434 break;
2435
2436 case SpvOpImageQuerySize: {
2437 struct vtn_access_chain *image =
2438 vtn_value(b, w[3], vtn_value_type_access_chain)->access_chain;
2439 if (glsl_type_is_image(image->var->var->interface_type)) {
2440 vtn_handle_image(b, opcode, w, count);
2441 } else {
2442 vtn_handle_texture(b, opcode, w, count);
2443 }
2444 break;
2445 }
2446
2447 case SpvOpAtomicExchange:
2448 case SpvOpAtomicCompareExchange:
2449 case SpvOpAtomicCompareExchangeWeak:
2450 case SpvOpAtomicIIncrement:
2451 case SpvOpAtomicIDecrement:
2452 case SpvOpAtomicIAdd:
2453 case SpvOpAtomicISub:
2454 case SpvOpAtomicSMin:
2455 case SpvOpAtomicUMin:
2456 case SpvOpAtomicSMax:
2457 case SpvOpAtomicUMax:
2458 case SpvOpAtomicAnd:
2459 case SpvOpAtomicOr:
2460 case SpvOpAtomicXor: {
2461 struct vtn_value *pointer = vtn_untyped_value(b, w[3]);
2462 if (pointer->value_type == vtn_value_type_image_pointer) {
2463 vtn_handle_image(b, opcode, w, count);
2464 } else {
2465 assert(pointer->value_type == vtn_value_type_access_chain);
2466 vtn_handle_ssbo_or_shared_atomic(b, opcode, w, count);
2467 }
2468 break;
2469 }
2470
2471 case SpvOpSNegate:
2472 case SpvOpFNegate:
2473 case SpvOpNot:
2474 case SpvOpAny:
2475 case SpvOpAll:
2476 case SpvOpConvertFToU:
2477 case SpvOpConvertFToS:
2478 case SpvOpConvertSToF:
2479 case SpvOpConvertUToF:
2480 case SpvOpUConvert:
2481 case SpvOpSConvert:
2482 case SpvOpFConvert:
2483 case SpvOpQuantizeToF16:
2484 case SpvOpConvertPtrToU:
2485 case SpvOpConvertUToPtr:
2486 case SpvOpPtrCastToGeneric:
2487 case SpvOpGenericCastToPtr:
2488 case SpvOpBitcast:
2489 case SpvOpIsNan:
2490 case SpvOpIsInf:
2491 case SpvOpIsFinite:
2492 case SpvOpIsNormal:
2493 case SpvOpSignBitSet:
2494 case SpvOpLessOrGreater:
2495 case SpvOpOrdered:
2496 case SpvOpUnordered:
2497 case SpvOpIAdd:
2498 case SpvOpFAdd:
2499 case SpvOpISub:
2500 case SpvOpFSub:
2501 case SpvOpIMul:
2502 case SpvOpFMul:
2503 case SpvOpUDiv:
2504 case SpvOpSDiv:
2505 case SpvOpFDiv:
2506 case SpvOpUMod:
2507 case SpvOpSRem:
2508 case SpvOpSMod:
2509 case SpvOpFRem:
2510 case SpvOpFMod:
2511 case SpvOpVectorTimesScalar:
2512 case SpvOpDot:
2513 case SpvOpIAddCarry:
2514 case SpvOpISubBorrow:
2515 case SpvOpUMulExtended:
2516 case SpvOpSMulExtended:
2517 case SpvOpShiftRightLogical:
2518 case SpvOpShiftRightArithmetic:
2519 case SpvOpShiftLeftLogical:
2520 case SpvOpLogicalEqual:
2521 case SpvOpLogicalNotEqual:
2522 case SpvOpLogicalOr:
2523 case SpvOpLogicalAnd:
2524 case SpvOpLogicalNot:
2525 case SpvOpBitwiseOr:
2526 case SpvOpBitwiseXor:
2527 case SpvOpBitwiseAnd:
2528 case SpvOpSelect:
2529 case SpvOpIEqual:
2530 case SpvOpFOrdEqual:
2531 case SpvOpFUnordEqual:
2532 case SpvOpINotEqual:
2533 case SpvOpFOrdNotEqual:
2534 case SpvOpFUnordNotEqual:
2535 case SpvOpULessThan:
2536 case SpvOpSLessThan:
2537 case SpvOpFOrdLessThan:
2538 case SpvOpFUnordLessThan:
2539 case SpvOpUGreaterThan:
2540 case SpvOpSGreaterThan:
2541 case SpvOpFOrdGreaterThan:
2542 case SpvOpFUnordGreaterThan:
2543 case SpvOpULessThanEqual:
2544 case SpvOpSLessThanEqual:
2545 case SpvOpFOrdLessThanEqual:
2546 case SpvOpFUnordLessThanEqual:
2547 case SpvOpUGreaterThanEqual:
2548 case SpvOpSGreaterThanEqual:
2549 case SpvOpFOrdGreaterThanEqual:
2550 case SpvOpFUnordGreaterThanEqual:
2551 case SpvOpDPdx:
2552 case SpvOpDPdy:
2553 case SpvOpFwidth:
2554 case SpvOpDPdxFine:
2555 case SpvOpDPdyFine:
2556 case SpvOpFwidthFine:
2557 case SpvOpDPdxCoarse:
2558 case SpvOpDPdyCoarse:
2559 case SpvOpFwidthCoarse:
2560 case SpvOpBitFieldInsert:
2561 case SpvOpBitFieldSExtract:
2562 case SpvOpBitFieldUExtract:
2563 case SpvOpBitReverse:
2564 case SpvOpBitCount:
2565 case SpvOpTranspose:
2566 case SpvOpOuterProduct:
2567 case SpvOpMatrixTimesScalar:
2568 case SpvOpVectorTimesMatrix:
2569 case SpvOpMatrixTimesVector:
2570 case SpvOpMatrixTimesMatrix:
2571 vtn_handle_alu(b, opcode, w, count);
2572 break;
2573
2574 case SpvOpVectorExtractDynamic:
2575 case SpvOpVectorInsertDynamic:
2576 case SpvOpVectorShuffle:
2577 case SpvOpCompositeConstruct:
2578 case SpvOpCompositeExtract:
2579 case SpvOpCompositeInsert:
2580 case SpvOpCopyObject:
2581 vtn_handle_composite(b, opcode, w, count);
2582 break;
2583
2584 case SpvOpEmitVertex:
2585 case SpvOpEndPrimitive:
2586 case SpvOpEmitStreamVertex:
2587 case SpvOpEndStreamPrimitive:
2588 case SpvOpControlBarrier:
2589 case SpvOpMemoryBarrier:
2590 vtn_handle_barrier(b, opcode, w, count);
2591 break;
2592
2593 default:
2594 unreachable("Unhandled opcode");
2595 }
2596
2597 return true;
2598 }
2599
2600 nir_function *
2601 spirv_to_nir(const uint32_t *words, size_t word_count,
2602 struct nir_spirv_specialization *spec, unsigned num_spec,
2603 gl_shader_stage stage, const char *entry_point_name,
2604 const nir_shader_compiler_options *options)
2605 {
2606 const uint32_t *word_end = words + word_count;
2607
2608 /* Handle the SPIR-V header (first 4 dwords) */
2609 assert(word_count > 5);
2610
2611 assert(words[0] == SpvMagicNumber);
2612 assert(words[1] >= 0x10000);
2613 /* words[2] == generator magic */
2614 unsigned value_id_bound = words[3];
2615 assert(words[4] == 0);
2616
2617 words+= 5;
2618
2619 /* Initialize the stn_builder object */
2620 struct vtn_builder *b = rzalloc(NULL, struct vtn_builder);
2621 b->value_id_bound = value_id_bound;
2622 b->values = rzalloc_array(b, struct vtn_value, value_id_bound);
2623 exec_list_make_empty(&b->functions);
2624 b->entry_point_stage = stage;
2625 b->entry_point_name = entry_point_name;
2626
2627 /* Handle all the preamble instructions */
2628 words = vtn_foreach_instruction(b, words, word_end,
2629 vtn_handle_preamble_instruction);
2630
2631 if (b->entry_point == NULL) {
2632 assert(!"Entry point not found");
2633 ralloc_free(b);
2634 return NULL;
2635 }
2636
2637 b->shader = nir_shader_create(NULL, stage, options);
2638
2639 /* Parse execution modes */
2640 vtn_foreach_execution_mode(b, b->entry_point,
2641 vtn_handle_execution_mode, NULL);
2642
2643 b->specializations = spec;
2644 b->num_specializations = num_spec;
2645
2646 /* Handle all variable, type, and constant instructions */
2647 words = vtn_foreach_instruction(b, words, word_end,
2648 vtn_handle_variable_or_type_instruction);
2649
2650 vtn_build_cfg(b, words, word_end);
2651
2652 foreach_list_typed(struct vtn_function, func, node, &b->functions) {
2653 b->impl = func->impl;
2654 b->const_table = _mesa_hash_table_create(b, _mesa_hash_pointer,
2655 _mesa_key_pointer_equal);
2656
2657 vtn_function_emit(b, func, vtn_handle_body_instruction);
2658 }
2659
2660 assert(b->entry_point->value_type == vtn_value_type_function);
2661 nir_function *entry_point = b->entry_point->func->impl->function;
2662 assert(entry_point);
2663
2664 ralloc_free(b);
2665
2666 return entry_point;
2667 }