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