Merge branch 'nir-spirv' into vulkan
[mesa.git] / src / glsl / nir / 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 "spirv_to_nir_private.h"
29 #include "nir_vla.h"
30 #include "nir_control_flow.h"
31
32 static struct vtn_ssa_value *
33 vtn_const_ssa_value(struct vtn_builder *b, nir_constant *constant,
34 const struct glsl_type *type)
35 {
36 struct hash_entry *entry = _mesa_hash_table_search(b->const_table, constant);
37
38 if (entry)
39 return entry->data;
40
41 struct vtn_ssa_value *val = rzalloc(b, struct vtn_ssa_value);
42 val->type = type;
43
44 switch (glsl_get_base_type(type)) {
45 case GLSL_TYPE_INT:
46 case GLSL_TYPE_UINT:
47 case GLSL_TYPE_BOOL:
48 case GLSL_TYPE_FLOAT:
49 case GLSL_TYPE_DOUBLE:
50 if (glsl_type_is_vector_or_scalar(type)) {
51 unsigned num_components = glsl_get_vector_elements(val->type);
52 nir_load_const_instr *load =
53 nir_load_const_instr_create(b->shader, num_components);
54
55 for (unsigned i = 0; i < num_components; i++)
56 load->value.u[i] = constant->value.u[i];
57
58 nir_instr_insert_before_cf_list(&b->impl->body, &load->instr);
59 val->def = &load->def;
60 } else {
61 assert(glsl_type_is_matrix(type));
62 unsigned rows = glsl_get_vector_elements(val->type);
63 unsigned columns = glsl_get_matrix_columns(val->type);
64 val->elems = ralloc_array(b, struct vtn_ssa_value *, columns);
65
66 for (unsigned i = 0; i < columns; i++) {
67 struct vtn_ssa_value *col_val = rzalloc(b, struct vtn_ssa_value);
68 col_val->type = glsl_get_column_type(val->type);
69 nir_load_const_instr *load =
70 nir_load_const_instr_create(b->shader, rows);
71
72 for (unsigned j = 0; j < rows; j++)
73 load->value.u[j] = constant->value.u[rows * i + j];
74
75 nir_instr_insert_before_cf_list(&b->impl->body, &load->instr);
76 col_val->def = &load->def;
77
78 val->elems[i] = col_val;
79 }
80 }
81 break;
82
83 case GLSL_TYPE_ARRAY: {
84 unsigned elems = glsl_get_length(val->type);
85 val->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
86 const struct glsl_type *elem_type = glsl_get_array_element(val->type);
87 for (unsigned i = 0; i < elems; i++)
88 val->elems[i] = vtn_const_ssa_value(b, constant->elements[i],
89 elem_type);
90 break;
91 }
92
93 case GLSL_TYPE_STRUCT: {
94 unsigned elems = glsl_get_length(val->type);
95 val->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
96 for (unsigned i = 0; i < elems; i++) {
97 const struct glsl_type *elem_type =
98 glsl_get_struct_field(val->type, i);
99 val->elems[i] = vtn_const_ssa_value(b, constant->elements[i],
100 elem_type);
101 }
102 break;
103 }
104
105 default:
106 unreachable("bad constant type");
107 }
108
109 return val;
110 }
111
112 struct vtn_ssa_value *
113 vtn_ssa_value(struct vtn_builder *b, uint32_t value_id)
114 {
115 struct vtn_value *val = vtn_untyped_value(b, value_id);
116 switch (val->value_type) {
117 case vtn_value_type_constant:
118 return vtn_const_ssa_value(b, val->constant, val->const_type);
119
120 case vtn_value_type_ssa:
121 return val->ssa;
122 default:
123 unreachable("Invalid type for an SSA value");
124 }
125 }
126
127 static char *
128 vtn_string_literal(struct vtn_builder *b, const uint32_t *words,
129 unsigned word_count)
130 {
131 return ralloc_strndup(b, (char *)words, word_count * sizeof(*words));
132 }
133
134 static const uint32_t *
135 vtn_foreach_instruction(struct vtn_builder *b, const uint32_t *start,
136 const uint32_t *end, vtn_instruction_handler handler)
137 {
138 const uint32_t *w = start;
139 while (w < end) {
140 SpvOp opcode = w[0] & SpvOpCodeMask;
141 unsigned count = w[0] >> SpvWordCountShift;
142 assert(count >= 1 && w + count <= end);
143
144 if (!handler(b, opcode, w, count))
145 return w;
146
147 w += count;
148 }
149 assert(w == end);
150 return w;
151 }
152
153 static void
154 vtn_handle_extension(struct vtn_builder *b, SpvOp opcode,
155 const uint32_t *w, unsigned count)
156 {
157 switch (opcode) {
158 case SpvOpExtInstImport: {
159 struct vtn_value *val = vtn_push_value(b, w[1], vtn_value_type_extension);
160 if (strcmp((const char *)&w[2], "GLSL.std.450") == 0) {
161 val->ext_handler = vtn_handle_glsl450_instruction;
162 } else {
163 assert(!"Unsupported extension");
164 }
165 break;
166 }
167
168 case SpvOpExtInst: {
169 struct vtn_value *val = vtn_value(b, w[3], vtn_value_type_extension);
170 bool handled = val->ext_handler(b, w[4], w, count);
171 (void)handled;
172 assert(handled);
173 break;
174 }
175
176 default:
177 unreachable("Unhandled opcode");
178 }
179 }
180
181 static void
182 _foreach_decoration_helper(struct vtn_builder *b,
183 struct vtn_value *base_value,
184 int member,
185 struct vtn_value *value,
186 vtn_decoration_foreach_cb cb, void *data)
187 {
188 int new_member = member;
189
190 for (struct vtn_decoration *dec = value->decoration; dec; dec = dec->next) {
191 if (dec->member >= 0) {
192 assert(member == -1);
193 new_member = dec->member;
194 }
195
196 if (dec->group) {
197 assert(dec->group->value_type == vtn_value_type_decoration_group);
198 _foreach_decoration_helper(b, base_value, new_member, dec->group,
199 cb, data);
200 } else {
201 cb(b, base_value, new_member, dec, data);
202 }
203 }
204 }
205
206 /** Iterates (recursively if needed) over all of the decorations on a value
207 *
208 * This function iterates over all of the decorations applied to a given
209 * value. If it encounters a decoration group, it recurses into the group
210 * and iterates over all of those decorations as well.
211 */
212 void
213 vtn_foreach_decoration(struct vtn_builder *b, struct vtn_value *value,
214 vtn_decoration_foreach_cb cb, void *data)
215 {
216 _foreach_decoration_helper(b, value, -1, value, cb, data);
217 }
218
219 static void
220 vtn_handle_decoration(struct vtn_builder *b, SpvOp opcode,
221 const uint32_t *w, unsigned count)
222 {
223 const uint32_t *w_end = w + count;
224 const uint32_t target = w[1];
225 w += 2;
226
227 int member = -1;
228 switch (opcode) {
229 case SpvOpDecorationGroup:
230 vtn_push_value(b, target, vtn_value_type_undef);
231 break;
232
233 case SpvOpMemberDecorate:
234 member = *(w++);
235 /* fallthrough */
236 case SpvOpDecorate: {
237 struct vtn_value *val = &b->values[target];
238
239 struct vtn_decoration *dec = rzalloc(b, struct vtn_decoration);
240 dec->member = member;
241 dec->decoration = *(w++);
242 dec->literals = w;
243
244 /* Link into the list */
245 dec->next = val->decoration;
246 val->decoration = dec;
247 break;
248 }
249
250 case SpvOpGroupMemberDecorate:
251 member = *(w++);
252 /* fallthrough */
253 case SpvOpGroupDecorate: {
254 struct vtn_value *group = &b->values[target];
255 assert(group->value_type == vtn_value_type_decoration_group);
256
257 for (; w < w_end; w++) {
258 struct vtn_value *val = &b->values[*w];
259 struct vtn_decoration *dec = rzalloc(b, struct vtn_decoration);
260 dec->member = member;
261 dec->group = group;
262
263 /* Link into the list */
264 dec->next = val->decoration;
265 val->decoration = dec;
266 }
267 break;
268 }
269
270 default:
271 unreachable("Unhandled opcode");
272 }
273 }
274
275 struct member_decoration_ctx {
276 struct glsl_struct_field *fields;
277 struct vtn_type *type;
278 };
279
280 /* does a shallow copy of a vtn_type */
281
282 static struct vtn_type *
283 vtn_type_copy(struct vtn_builder *b, struct vtn_type *src)
284 {
285 struct vtn_type *dest = ralloc(b, struct vtn_type);
286 dest->type = src->type;
287 dest->is_builtin = src->is_builtin;
288 if (src->is_builtin)
289 dest->builtin = src->builtin;
290
291 if (!glsl_type_is_vector_or_scalar(src->type)) {
292 switch (glsl_get_base_type(src->type)) {
293 case GLSL_TYPE_ARRAY:
294 dest->array_element = src->array_element;
295 dest->stride = src->stride;
296 break;
297
298 case GLSL_TYPE_INT:
299 case GLSL_TYPE_UINT:
300 case GLSL_TYPE_BOOL:
301 case GLSL_TYPE_FLOAT:
302 case GLSL_TYPE_DOUBLE:
303 /* matrices */
304 dest->row_major = src->row_major;
305 dest->stride = src->stride;
306 break;
307
308 case GLSL_TYPE_STRUCT: {
309 unsigned elems = glsl_get_length(src->type);
310
311 dest->members = ralloc_array(b, struct vtn_type *, elems);
312 memcpy(dest->members, src->members, elems * sizeof(struct vtn_type *));
313
314 dest->offsets = ralloc_array(b, unsigned, elems);
315 memcpy(dest->offsets, src->offsets, elems * sizeof(unsigned));
316 break;
317 }
318
319 default:
320 unreachable("unhandled type");
321 }
322 }
323
324 return dest;
325 }
326
327 static void
328 struct_member_decoration_cb(struct vtn_builder *b,
329 struct vtn_value *val, int member,
330 const struct vtn_decoration *dec, void *void_ctx)
331 {
332 struct member_decoration_ctx *ctx = void_ctx;
333
334 if (member < 0)
335 return;
336
337 switch (dec->decoration) {
338 case SpvDecorationRelaxedPrecision:
339 break; /* FIXME: Do nothing with this for now. */
340 case SpvDecorationSmooth:
341 ctx->fields[member].interpolation = INTERP_QUALIFIER_SMOOTH;
342 break;
343 case SpvDecorationNoperspective:
344 ctx->fields[member].interpolation = INTERP_QUALIFIER_NOPERSPECTIVE;
345 break;
346 case SpvDecorationFlat:
347 ctx->fields[member].interpolation = INTERP_QUALIFIER_FLAT;
348 break;
349 case SpvDecorationCentroid:
350 ctx->fields[member].centroid = true;
351 break;
352 case SpvDecorationSample:
353 ctx->fields[member].sample = true;
354 break;
355 case SpvDecorationLocation:
356 ctx->fields[member].location = dec->literals[0];
357 break;
358 case SpvDecorationBuiltIn:
359 ctx->type->members[member] = vtn_type_copy(b,
360 ctx->type->members[member]);
361 ctx->type->members[member]->is_builtin = true;
362 ctx->type->members[member]->builtin = dec->literals[0];
363 ctx->type->builtin_block = true;
364 break;
365 case SpvDecorationOffset:
366 ctx->type->offsets[member] = dec->literals[0];
367 break;
368 case SpvDecorationMatrixStride:
369 ctx->type->members[member]->stride = dec->literals[0];
370 break;
371 case SpvDecorationColMajor:
372 break; /* Nothing to do here. Column-major is the default. */
373 default:
374 unreachable("Unhandled member decoration");
375 }
376 }
377
378 static void
379 type_decoration_cb(struct vtn_builder *b,
380 struct vtn_value *val, int member,
381 const struct vtn_decoration *dec, void *ctx)
382 {
383 struct vtn_type *type = val->type;
384
385 if (member != -1)
386 return;
387
388 switch (dec->decoration) {
389 case SpvDecorationArrayStride:
390 type->stride = dec->literals[0];
391 break;
392 case SpvDecorationBlock:
393 type->block = true;
394 break;
395 case SpvDecorationBufferBlock:
396 type->buffer_block = true;
397 break;
398 case SpvDecorationGLSLShared:
399 case SpvDecorationGLSLPacked:
400 /* Ignore these, since we get explicit offsets anyways */
401 break;
402
403 default:
404 unreachable("Unhandled type decoration");
405 }
406 }
407
408 static void
409 vtn_handle_type(struct vtn_builder *b, SpvOp opcode,
410 const uint32_t *w, unsigned count)
411 {
412 struct vtn_value *val = vtn_push_value(b, w[1], vtn_value_type_type);
413
414 val->type = rzalloc(b, struct vtn_type);
415 val->type->is_builtin = false;
416
417 switch (opcode) {
418 case SpvOpTypeVoid:
419 val->type->type = glsl_void_type();
420 break;
421 case SpvOpTypeBool:
422 val->type->type = glsl_bool_type();
423 break;
424 case SpvOpTypeInt:
425 val->type->type = glsl_int_type();
426 break;
427 case SpvOpTypeFloat:
428 val->type->type = glsl_float_type();
429 break;
430
431 case SpvOpTypeVector: {
432 const struct glsl_type *base =
433 vtn_value(b, w[2], vtn_value_type_type)->type->type;
434 unsigned elems = w[3];
435
436 assert(glsl_type_is_scalar(base));
437 val->type->type = glsl_vector_type(glsl_get_base_type(base), elems);
438 break;
439 }
440
441 case SpvOpTypeMatrix: {
442 struct vtn_type *base =
443 vtn_value(b, w[2], vtn_value_type_type)->type;
444 unsigned columns = w[3];
445
446 assert(glsl_type_is_vector(base->type));
447 val->type->type = glsl_matrix_type(glsl_get_base_type(base->type),
448 glsl_get_vector_elements(base->type),
449 columns);
450 val->type->array_element = base;
451 val->type->row_major = false;
452 val->type->stride = 0;
453 break;
454 }
455
456 case SpvOpTypeArray: {
457 struct vtn_type *array_element =
458 vtn_value(b, w[2], vtn_value_type_type)->type;
459 val->type->type = glsl_array_type(array_element->type, w[3]);
460 val->type->array_element = array_element;
461 val->type->stride = 0;
462 break;
463 }
464
465 case SpvOpTypeStruct: {
466 unsigned num_fields = count - 2;
467 val->type->members = ralloc_array(b, struct vtn_type *, num_fields);
468 val->type->offsets = ralloc_array(b, unsigned, num_fields);
469
470 NIR_VLA(struct glsl_struct_field, fields, count);
471 for (unsigned i = 0; i < num_fields; i++) {
472 /* TODO: Handle decorators */
473 val->type->members[i] =
474 vtn_value(b, w[i + 2], vtn_value_type_type)->type;
475 fields[i].type = val->type->members[i]->type;
476 fields[i].name = ralloc_asprintf(b, "field%d", i);
477 fields[i].location = -1;
478 fields[i].interpolation = 0;
479 fields[i].centroid = 0;
480 fields[i].sample = 0;
481 fields[i].matrix_layout = 2;
482 fields[i].stream = -1;
483 }
484
485 struct member_decoration_ctx ctx = {
486 .fields = fields,
487 .type = val->type
488 };
489
490 vtn_foreach_decoration(b, val, struct_member_decoration_cb, &ctx);
491
492 const char *name = val->name ? val->name : "struct";
493
494 val->type->type = glsl_struct_type(fields, num_fields, name);
495 break;
496 }
497
498 case SpvOpTypeFunction: {
499 const struct glsl_type *return_type =
500 vtn_value(b, w[2], vtn_value_type_type)->type->type;
501 NIR_VLA(struct glsl_function_param, params, count - 3);
502 for (unsigned i = 0; i < count - 3; i++) {
503 params[i].type = vtn_value(b, w[i + 3], vtn_value_type_type)->type->type;
504
505 /* FIXME: */
506 params[i].in = true;
507 params[i].out = true;
508 }
509 val->type->type = glsl_function_type(return_type, params, count - 3);
510 break;
511 }
512
513 case SpvOpTypePointer:
514 /* FIXME: For now, we'll just do the really lame thing and return
515 * the same type. The validator should ensure that the proper number
516 * of dereferences happen
517 */
518 val->type = vtn_value(b, w[3], vtn_value_type_type)->type;
519 break;
520
521 case SpvOpTypeImage: {
522 const struct glsl_type *sampled_type =
523 vtn_value(b, w[2], vtn_value_type_type)->type->type;
524
525 assert(glsl_type_is_vector_or_scalar(sampled_type));
526
527 enum glsl_sampler_dim dim;
528 switch ((SpvDim)w[3]) {
529 case SpvDim1D: dim = GLSL_SAMPLER_DIM_1D; break;
530 case SpvDim2D: dim = GLSL_SAMPLER_DIM_2D; break;
531 case SpvDim3D: dim = GLSL_SAMPLER_DIM_3D; break;
532 case SpvDimCube: dim = GLSL_SAMPLER_DIM_CUBE; break;
533 case SpvDimRect: dim = GLSL_SAMPLER_DIM_RECT; break;
534 case SpvDimBuffer: dim = GLSL_SAMPLER_DIM_BUF; break;
535 default:
536 unreachable("Invalid SPIR-V Sampler dimension");
537 }
538
539 bool is_shadow = w[4];
540 bool is_array = w[5];
541
542 assert(w[6] == 0 && "FIXME: Handl multi-sampled textures");
543 assert(w[7] == 1 && "FIXME: Add support for non-sampled images");
544
545 val->type->type = glsl_sampler_type(dim, is_shadow, is_array,
546 glsl_get_base_type(sampled_type));
547 break;
548 }
549
550 case SpvOpTypeSampledImage:
551 val->type = vtn_value(b, w[2], vtn_value_type_type)->type;
552 break;
553
554 case SpvOpTypeRuntimeArray:
555 case SpvOpTypeOpaque:
556 case SpvOpTypeEvent:
557 case SpvOpTypeDeviceEvent:
558 case SpvOpTypeReserveId:
559 case SpvOpTypeQueue:
560 case SpvOpTypePipe:
561 default:
562 unreachable("Unhandled opcode");
563 }
564
565 vtn_foreach_decoration(b, val, type_decoration_cb, NULL);
566 }
567
568 static void
569 vtn_handle_constant(struct vtn_builder *b, SpvOp opcode,
570 const uint32_t *w, unsigned count)
571 {
572 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_constant);
573 val->const_type = vtn_value(b, w[1], vtn_value_type_type)->type->type;
574 val->constant = ralloc(b, nir_constant);
575 switch (opcode) {
576 case SpvOpConstantTrue:
577 assert(val->const_type == glsl_bool_type());
578 val->constant->value.u[0] = NIR_TRUE;
579 break;
580 case SpvOpConstantFalse:
581 assert(val->const_type == glsl_bool_type());
582 val->constant->value.u[0] = NIR_FALSE;
583 break;
584 case SpvOpConstant:
585 assert(glsl_type_is_scalar(val->const_type));
586 val->constant->value.u[0] = w[3];
587 break;
588 case SpvOpConstantComposite: {
589 unsigned elem_count = count - 3;
590 nir_constant **elems = ralloc_array(b, nir_constant *, elem_count);
591 for (unsigned i = 0; i < elem_count; i++)
592 elems[i] = vtn_value(b, w[i + 3], vtn_value_type_constant)->constant;
593
594 switch (glsl_get_base_type(val->const_type)) {
595 case GLSL_TYPE_UINT:
596 case GLSL_TYPE_INT:
597 case GLSL_TYPE_FLOAT:
598 case GLSL_TYPE_BOOL:
599 if (glsl_type_is_matrix(val->const_type)) {
600 unsigned rows = glsl_get_vector_elements(val->const_type);
601 assert(glsl_get_matrix_columns(val->const_type) == elem_count);
602 for (unsigned i = 0; i < elem_count; i++)
603 for (unsigned j = 0; j < rows; j++)
604 val->constant->value.u[rows * i + j] = elems[i]->value.u[j];
605 } else {
606 assert(glsl_type_is_vector(val->const_type));
607 assert(glsl_get_vector_elements(val->const_type) == elem_count);
608 for (unsigned i = 0; i < elem_count; i++)
609 val->constant->value.u[i] = elems[i]->value.u[0];
610 }
611 ralloc_free(elems);
612 break;
613
614 case GLSL_TYPE_STRUCT:
615 case GLSL_TYPE_ARRAY:
616 ralloc_steal(val->constant, elems);
617 val->constant->elements = elems;
618 break;
619
620 default:
621 unreachable("Unsupported type for constants");
622 }
623 break;
624 }
625
626 default:
627 unreachable("Unhandled opcode");
628 }
629 }
630
631 static void
632 vtn_get_builtin_location(SpvBuiltIn builtin, int *location,
633 nir_variable_mode *mode)
634 {
635 switch (builtin) {
636 case SpvBuiltInPosition:
637 *location = VARYING_SLOT_POS;
638 *mode = nir_var_shader_out;
639 break;
640 case SpvBuiltInPointSize:
641 *location = VARYING_SLOT_PSIZ;
642 *mode = nir_var_shader_out;
643 break;
644 case SpvBuiltInClipDistance:
645 *location = VARYING_SLOT_CLIP_DIST0; /* XXX CLIP_DIST1? */
646 *mode = nir_var_shader_in;
647 break;
648 case SpvBuiltInCullDistance:
649 /* XXX figure this out */
650 unreachable("unhandled builtin");
651 case SpvBuiltInVertexId:
652 /* Vulkan defines VertexID to be zero-based and reserves the new
653 * builtin keyword VertexIndex to indicate the non-zero-based value.
654 */
655 *location = SYSTEM_VALUE_VERTEX_ID_ZERO_BASE;
656 *mode = nir_var_system_value;
657 break;
658 case SpvBuiltInInstanceId:
659 *location = SYSTEM_VALUE_INSTANCE_ID;
660 *mode = nir_var_system_value;
661 break;
662 case SpvBuiltInPrimitiveId:
663 *location = VARYING_SLOT_PRIMITIVE_ID;
664 *mode = nir_var_shader_out;
665 break;
666 case SpvBuiltInInvocationId:
667 *location = SYSTEM_VALUE_INVOCATION_ID;
668 *mode = nir_var_system_value;
669 break;
670 case SpvBuiltInLayer:
671 *location = VARYING_SLOT_LAYER;
672 *mode = nir_var_shader_out;
673 break;
674 case SpvBuiltInTessLevelOuter:
675 case SpvBuiltInTessLevelInner:
676 case SpvBuiltInTessCoord:
677 case SpvBuiltInPatchVertices:
678 unreachable("no tessellation support");
679 case SpvBuiltInFragCoord:
680 *location = VARYING_SLOT_POS;
681 *mode = nir_var_shader_in;
682 break;
683 case SpvBuiltInPointCoord:
684 *location = VARYING_SLOT_PNTC;
685 *mode = nir_var_shader_out;
686 break;
687 case SpvBuiltInFrontFacing:
688 *location = VARYING_SLOT_FACE;
689 *mode = nir_var_shader_out;
690 break;
691 case SpvBuiltInSampleId:
692 *location = SYSTEM_VALUE_SAMPLE_ID;
693 *mode = nir_var_shader_in;
694 break;
695 case SpvBuiltInSamplePosition:
696 *location = SYSTEM_VALUE_SAMPLE_POS;
697 *mode = nir_var_shader_in;
698 break;
699 case SpvBuiltInSampleMask:
700 *location = SYSTEM_VALUE_SAMPLE_MASK_IN; /* XXX out? */
701 *mode = nir_var_shader_in;
702 break;
703 case SpvBuiltInFragColor:
704 *location = FRAG_RESULT_COLOR;
705 *mode = nir_var_shader_out;
706 break;
707 case SpvBuiltInFragDepth:
708 *location = FRAG_RESULT_DEPTH;
709 *mode = nir_var_shader_out;
710 break;
711 case SpvBuiltInHelperInvocation:
712 unreachable("unsupported builtin"); /* XXX */
713 break;
714 case SpvBuiltInNumWorkgroups:
715 case SpvBuiltInWorkgroupSize:
716 /* these are constants, need to be handled specially */
717 unreachable("unsupported builtin");
718 case SpvBuiltInWorkgroupId:
719 case SpvBuiltInLocalInvocationId:
720 case SpvBuiltInGlobalInvocationId:
721 case SpvBuiltInLocalInvocationIndex:
722 unreachable("no compute shader support");
723 default:
724 unreachable("unsupported builtin");
725 }
726 }
727
728 static void
729 var_decoration_cb(struct vtn_builder *b, struct vtn_value *val, int member,
730 const struct vtn_decoration *dec, void *void_var)
731 {
732 assert(val->value_type == vtn_value_type_deref);
733 assert(val->deref->deref.child == NULL);
734 assert(val->deref->var == void_var);
735
736 nir_variable *var = void_var;
737 switch (dec->decoration) {
738 case SpvDecorationRelaxedPrecision:
739 break; /* FIXME: Do nothing with this for now. */
740 case SpvDecorationSmooth:
741 var->data.interpolation = INTERP_QUALIFIER_SMOOTH;
742 break;
743 case SpvDecorationNoperspective:
744 var->data.interpolation = INTERP_QUALIFIER_NOPERSPECTIVE;
745 break;
746 case SpvDecorationFlat:
747 var->data.interpolation = INTERP_QUALIFIER_FLAT;
748 break;
749 case SpvDecorationCentroid:
750 var->data.centroid = true;
751 break;
752 case SpvDecorationSample:
753 var->data.sample = true;
754 break;
755 case SpvDecorationInvariant:
756 var->data.invariant = true;
757 break;
758 case SpvDecorationConstant:
759 assert(var->constant_initializer != NULL);
760 var->data.read_only = true;
761 break;
762 case SpvDecorationNonwritable:
763 var->data.read_only = true;
764 break;
765 case SpvDecorationLocation:
766 var->data.explicit_location = true;
767 var->data.location = dec->literals[0];
768 break;
769 case SpvDecorationComponent:
770 var->data.location_frac = dec->literals[0];
771 break;
772 case SpvDecorationIndex:
773 var->data.explicit_index = true;
774 var->data.index = dec->literals[0];
775 break;
776 case SpvDecorationBinding:
777 var->data.explicit_binding = true;
778 var->data.binding = dec->literals[0];
779 break;
780 case SpvDecorationDescriptorSet:
781 var->data.descriptor_set = dec->literals[0];
782 break;
783 case SpvDecorationBuiltIn: {
784 nir_variable_mode mode;
785 vtn_get_builtin_location(dec->literals[0], &var->data.location,
786 &mode);
787 var->data.mode = mode;
788 if (mode == nir_var_shader_in || mode == nir_var_system_value)
789 var->data.read_only = true;
790 b->builtins[dec->literals[0]] = var;
791 break;
792 }
793 case SpvDecorationNoStaticUse:
794 /* This can safely be ignored */
795 break;
796 case SpvDecorationRowMajor:
797 case SpvDecorationColMajor:
798 case SpvDecorationGLSLShared:
799 case SpvDecorationPatch:
800 case SpvDecorationRestrict:
801 case SpvDecorationAliased:
802 case SpvDecorationVolatile:
803 case SpvDecorationCoherent:
804 case SpvDecorationNonreadable:
805 case SpvDecorationUniform:
806 /* This is really nice but we have no use for it right now. */
807 case SpvDecorationCPacked:
808 case SpvDecorationSaturatedConversion:
809 case SpvDecorationStream:
810 case SpvDecorationOffset:
811 case SpvDecorationXfbBuffer:
812 case SpvDecorationFuncParamAttr:
813 case SpvDecorationFPRoundingMode:
814 case SpvDecorationFPFastMathMode:
815 case SpvDecorationLinkageAttributes:
816 case SpvDecorationSpecId:
817 break;
818 default:
819 unreachable("Unhandled variable decoration");
820 }
821 }
822
823 static nir_variable *
824 get_builtin_variable(struct vtn_builder *b,
825 const struct glsl_type *type,
826 SpvBuiltIn builtin)
827 {
828 nir_variable *var = b->builtins[builtin];
829
830 if (!var) {
831 var = ralloc(b->shader, nir_variable);
832 var->type = type;
833
834 nir_variable_mode mode;
835 vtn_get_builtin_location(builtin, &var->data.location, &mode);
836 var->data.mode = mode;
837 var->name = ralloc_strdup(var, "builtin");
838
839 switch (mode) {
840 case nir_var_shader_in:
841 exec_list_push_tail(&b->shader->inputs, &var->node);
842 break;
843 case nir_var_shader_out:
844 exec_list_push_tail(&b->shader->outputs, &var->node);
845 break;
846 case nir_var_system_value:
847 exec_list_push_tail(&b->shader->system_values, &var->node);
848 break;
849 default:
850 unreachable("bad builtin mode");
851 }
852
853 b->builtins[builtin] = var;
854 }
855
856 return var;
857 }
858
859 static void
860 vtn_builtin_load(struct vtn_builder *b,
861 struct vtn_ssa_value *val,
862 SpvBuiltIn builtin)
863 {
864 assert(glsl_type_is_vector_or_scalar(val->type));
865
866 nir_variable *var = get_builtin_variable(b, val->type, builtin);
867
868 nir_intrinsic_instr *load =
869 nir_intrinsic_instr_create(b->shader, nir_intrinsic_load_var);
870 nir_ssa_dest_init(&load->instr, &load->dest,
871 glsl_get_vector_elements(val->type), NULL);
872
873 load->variables[0] = nir_deref_var_create(load, var);
874 load->num_components = glsl_get_vector_elements(val->type);
875 nir_builder_instr_insert(&b->nb, &load->instr);
876 val->def = &load->dest.ssa;
877 }
878
879 static void
880 vtn_builtin_store(struct vtn_builder *b,
881 struct vtn_ssa_value *val,
882 SpvBuiltIn builtin)
883 {
884 assert(glsl_type_is_vector_or_scalar(val->type));
885
886 nir_variable *var = get_builtin_variable(b, val->type, builtin);
887
888 nir_intrinsic_instr *store =
889 nir_intrinsic_instr_create(b->shader, nir_intrinsic_store_var);
890
891 store->variables[0] = nir_deref_var_create(store, var);
892 store->num_components = glsl_get_vector_elements(val->type);
893 store->src[0] = nir_src_for_ssa(val->def);
894 nir_builder_instr_insert(&b->nb, &store->instr);
895 }
896
897 static struct vtn_ssa_value *
898 _vtn_variable_load(struct vtn_builder *b,
899 nir_deref_var *src_deref, struct vtn_type *src_type,
900 nir_deref *src_deref_tail)
901 {
902 struct vtn_ssa_value *val = rzalloc(b, struct vtn_ssa_value);
903 val->type = src_deref_tail->type;
904
905 if (src_type->is_builtin) {
906 vtn_builtin_load(b, val, src_type->builtin);
907 return val;
908 }
909
910 /* The deref tail may contain a deref to select a component of a vector (in
911 * other words, it might not be an actual tail) so we have to save it away
912 * here since we overwrite it later.
913 */
914 nir_deref *old_child = src_deref_tail->child;
915
916 if (glsl_type_is_vector_or_scalar(val->type)) {
917 nir_intrinsic_instr *load =
918 nir_intrinsic_instr_create(b->shader, nir_intrinsic_load_var);
919 load->variables[0] =
920 nir_deref_as_var(nir_copy_deref(load, &src_deref->deref));
921 load->num_components = glsl_get_vector_elements(val->type);
922 nir_ssa_dest_init(&load->instr, &load->dest, load->num_components, NULL);
923
924 nir_builder_instr_insert(&b->nb, &load->instr);
925
926 if (src_deref->var->data.mode == nir_var_uniform &&
927 glsl_get_base_type(val->type) == GLSL_TYPE_BOOL) {
928 /* Uniform boolean loads need to be fixed up since they're defined
929 * to be zero/nonzero rather than NIR_FALSE/NIR_TRUE.
930 */
931 val->def = nir_ine(&b->nb, &load->dest.ssa, nir_imm_int(&b->nb, 0));
932 } else {
933 val->def = &load->dest.ssa;
934 }
935 } else if (glsl_get_base_type(val->type) == GLSL_TYPE_ARRAY ||
936 glsl_type_is_matrix(val->type)) {
937 unsigned elems = glsl_get_length(val->type);
938 val->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
939
940 nir_deref_array *deref = nir_deref_array_create(b);
941 deref->deref_array_type = nir_deref_array_type_direct;
942 deref->deref.type = glsl_get_array_element(val->type);
943 src_deref_tail->child = &deref->deref;
944 for (unsigned i = 0; i < elems; i++) {
945 deref->base_offset = i;
946 val->elems[i] = _vtn_variable_load(b, src_deref,
947 src_type->array_element,
948 &deref->deref);
949 }
950 } else {
951 assert(glsl_get_base_type(val->type) == GLSL_TYPE_STRUCT);
952 unsigned elems = glsl_get_length(val->type);
953 val->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
954
955 nir_deref_struct *deref = nir_deref_struct_create(b, 0);
956 src_deref_tail->child = &deref->deref;
957 for (unsigned i = 0; i < elems; i++) {
958 deref->index = i;
959 deref->deref.type = glsl_get_struct_field(val->type, i);
960 val->elems[i] = _vtn_variable_load(b, src_deref,
961 src_type->members[i],
962 &deref->deref);
963 }
964 }
965
966 src_deref_tail->child = old_child;
967
968 return val;
969 }
970
971 static void
972 _vtn_variable_store(struct vtn_builder *b, struct vtn_type *dest_type,
973 nir_deref_var *dest_deref, nir_deref *dest_deref_tail,
974 struct vtn_ssa_value *src)
975 {
976 if (dest_type->is_builtin) {
977 vtn_builtin_store(b, src, dest_type->builtin);
978 return;
979 }
980
981 nir_deref *old_child = dest_deref_tail->child;
982
983 if (glsl_type_is_vector_or_scalar(src->type)) {
984 nir_intrinsic_instr *store =
985 nir_intrinsic_instr_create(b->shader, nir_intrinsic_store_var);
986 store->variables[0] =
987 nir_deref_as_var(nir_copy_deref(store, &dest_deref->deref));
988 store->num_components = glsl_get_vector_elements(src->type);
989 store->src[0] = nir_src_for_ssa(src->def);
990
991 nir_builder_instr_insert(&b->nb, &store->instr);
992 } else if (glsl_get_base_type(src->type) == GLSL_TYPE_ARRAY ||
993 glsl_type_is_matrix(src->type)) {
994 unsigned elems = glsl_get_length(src->type);
995
996 nir_deref_array *deref = nir_deref_array_create(b);
997 deref->deref_array_type = nir_deref_array_type_direct;
998 deref->deref.type = glsl_get_array_element(src->type);
999 dest_deref_tail->child = &deref->deref;
1000 for (unsigned i = 0; i < elems; i++) {
1001 deref->base_offset = i;
1002 _vtn_variable_store(b, dest_type->array_element, dest_deref,
1003 &deref->deref, src->elems[i]);
1004 }
1005 } else {
1006 assert(glsl_get_base_type(src->type) == GLSL_TYPE_STRUCT);
1007 unsigned elems = glsl_get_length(src->type);
1008
1009 nir_deref_struct *deref = nir_deref_struct_create(b, 0);
1010 dest_deref_tail->child = &deref->deref;
1011 for (unsigned i = 0; i < elems; i++) {
1012 deref->index = i;
1013 deref->deref.type = glsl_get_struct_field(src->type, i);
1014 _vtn_variable_store(b, dest_type->members[i], dest_deref,
1015 &deref->deref, src->elems[i]);
1016 }
1017 }
1018
1019 dest_deref_tail->child = old_child;
1020 }
1021
1022 static struct vtn_ssa_value *
1023 _vtn_block_load(struct vtn_builder *b, nir_intrinsic_op op,
1024 unsigned set, nir_ssa_def *binding,
1025 unsigned offset, nir_ssa_def *indirect,
1026 struct vtn_type *type)
1027 {
1028 struct vtn_ssa_value *val = ralloc(b, struct vtn_ssa_value);
1029 val->type = type->type;
1030 val->transposed = NULL;
1031 if (glsl_type_is_vector_or_scalar(type->type)) {
1032 nir_intrinsic_instr *load = nir_intrinsic_instr_create(b->shader, op);
1033 load->num_components = glsl_get_vector_elements(type->type);
1034 load->const_index[0] = set;
1035 load->src[0] = nir_src_for_ssa(binding);
1036 load->const_index[1] = offset;
1037 if (indirect)
1038 load->src[1] = nir_src_for_ssa(indirect);
1039 nir_ssa_dest_init(&load->instr, &load->dest, load->num_components, NULL);
1040 nir_builder_instr_insert(&b->nb, &load->instr);
1041 val->def = &load->dest.ssa;
1042 } else {
1043 unsigned elems = glsl_get_length(type->type);
1044 val->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
1045 if (glsl_type_is_struct(type->type)) {
1046 for (unsigned i = 0; i < elems; i++) {
1047 val->elems[i] = _vtn_block_load(b, op, set, binding,
1048 offset + type->offsets[i],
1049 indirect, type->members[i]);
1050 }
1051 } else {
1052 for (unsigned i = 0; i < elems; i++) {
1053 val->elems[i] = _vtn_block_load(b, op, set, binding,
1054 offset + i * type->stride,
1055 indirect, type->array_element);
1056 }
1057 }
1058 }
1059
1060 return val;
1061 }
1062
1063 static struct vtn_ssa_value *
1064 vtn_block_load(struct vtn_builder *b, nir_deref_var *src,
1065 struct vtn_type *type, nir_deref *src_tail)
1066 {
1067 unsigned set = src->var->data.descriptor_set;
1068
1069 nir_ssa_def *binding = nir_imm_int(&b->nb, src->var->data.binding);
1070 nir_deref *deref = &src->deref;
1071
1072 /* The block variable may be an array, in which case the array index adds
1073 * an offset to the binding. Figure out that index now.
1074 */
1075
1076 if (deref->child->deref_type == nir_deref_type_array) {
1077 deref = deref->child;
1078 type = type->array_element;
1079 nir_deref_array *deref_array = nir_deref_as_array(deref);
1080 if (deref_array->deref_array_type == nir_deref_array_type_direct) {
1081 binding = nir_imm_int(&b->nb, src->var->data.binding +
1082 deref_array->base_offset);
1083 } else {
1084 binding = nir_iadd(&b->nb, binding, deref_array->indirect.ssa);
1085 }
1086 }
1087
1088 unsigned offset = 0;
1089 nir_ssa_def *indirect = NULL;
1090 while (deref != src_tail) {
1091 deref = deref->child;
1092 switch (deref->deref_type) {
1093 case nir_deref_type_array: {
1094 nir_deref_array *deref_array = nir_deref_as_array(deref);
1095 if (deref_array->deref_array_type == nir_deref_array_type_direct) {
1096 offset += type->stride * deref_array->base_offset;
1097 } else {
1098 nir_ssa_def *offset = nir_imul(&b->nb, deref_array->indirect.ssa,
1099 nir_imm_int(&b->nb, type->stride));
1100 indirect = indirect ? nir_iadd(&b->nb, indirect, offset) : offset;
1101 }
1102 type = type->array_element;
1103 break;
1104 }
1105
1106 case nir_deref_type_struct: {
1107 nir_deref_struct *deref_struct = nir_deref_as_struct(deref);
1108 offset += type->offsets[deref_struct->index];
1109 type = type->members[deref_struct->index];
1110 break;
1111 }
1112
1113 default:
1114 unreachable("unknown deref type");
1115 }
1116 }
1117
1118 /* TODO SSBO's */
1119 nir_intrinsic_op op = indirect ? nir_intrinsic_load_ubo_indirect
1120 : nir_intrinsic_load_ubo;
1121
1122 return _vtn_block_load(b, op, set, binding, offset, indirect, type);
1123 }
1124
1125 /*
1126 * Gets the NIR-level deref tail, which may have as a child an array deref
1127 * selecting which component due to OpAccessChain supporting per-component
1128 * indexing in SPIR-V.
1129 */
1130
1131 static nir_deref *
1132 get_deref_tail(nir_deref_var *deref)
1133 {
1134 nir_deref *cur = &deref->deref;
1135 while (!glsl_type_is_vector_or_scalar(cur->type) && cur->child)
1136 cur = cur->child;
1137
1138 return cur;
1139 }
1140
1141 static nir_ssa_def *vtn_vector_extract(struct vtn_builder *b,
1142 nir_ssa_def *src, unsigned index);
1143
1144 static nir_ssa_def *vtn_vector_extract_dynamic(struct vtn_builder *b,
1145 nir_ssa_def *src,
1146 nir_ssa_def *index);
1147
1148 static struct vtn_ssa_value *
1149 vtn_variable_load(struct vtn_builder *b, nir_deref_var *src,
1150 struct vtn_type *src_type)
1151 {
1152 nir_deref *src_tail = get_deref_tail(src);
1153
1154 struct vtn_ssa_value *val;
1155 if (src->var->interface_type && src->var->data.mode == nir_var_uniform)
1156 val = vtn_block_load(b, src, src_type, src_tail);
1157 else
1158 val = _vtn_variable_load(b, src, src_type, src_tail);
1159
1160 if (src_tail->child) {
1161 nir_deref_array *vec_deref = nir_deref_as_array(src_tail->child);
1162 assert(vec_deref->deref.child == NULL);
1163 val->type = vec_deref->deref.type;
1164 if (vec_deref->deref_array_type == nir_deref_array_type_direct)
1165 val->def = vtn_vector_extract(b, val->def, vec_deref->base_offset);
1166 else
1167 val->def = vtn_vector_extract_dynamic(b, val->def,
1168 vec_deref->indirect.ssa);
1169 }
1170
1171 return val;
1172 }
1173
1174 static nir_ssa_def * vtn_vector_insert(struct vtn_builder *b,
1175 nir_ssa_def *src, nir_ssa_def *insert,
1176 unsigned index);
1177
1178 static nir_ssa_def * vtn_vector_insert_dynamic(struct vtn_builder *b,
1179 nir_ssa_def *src,
1180 nir_ssa_def *insert,
1181 nir_ssa_def *index);
1182 static void
1183 vtn_variable_store(struct vtn_builder *b, struct vtn_ssa_value *src,
1184 nir_deref_var *dest, struct vtn_type *dest_type)
1185 {
1186 nir_deref *dest_tail = get_deref_tail(dest);
1187 if (dest_tail->child) {
1188 struct vtn_ssa_value *val = _vtn_variable_load(b, dest, dest_type,
1189 dest_tail);
1190 nir_deref_array *deref = nir_deref_as_array(dest_tail->child);
1191 assert(deref->deref.child == NULL);
1192 if (deref->deref_array_type == nir_deref_array_type_direct)
1193 val->def = vtn_vector_insert(b, val->def, src->def,
1194 deref->base_offset);
1195 else
1196 val->def = vtn_vector_insert_dynamic(b, val->def, src->def,
1197 deref->indirect.ssa);
1198 _vtn_variable_store(b, dest_type, dest, dest_tail, val);
1199 } else {
1200 _vtn_variable_store(b, dest_type, dest, dest_tail, src);
1201 }
1202 }
1203
1204 static void
1205 vtn_variable_copy(struct vtn_builder *b, nir_deref_var *src,
1206 nir_deref_var *dest, struct vtn_type *type)
1207 {
1208 nir_deref *src_tail = get_deref_tail(src);
1209
1210 if (src_tail->child || src->var->interface_type) {
1211 assert(get_deref_tail(dest)->child);
1212 struct vtn_ssa_value *val = vtn_variable_load(b, src, type);
1213 vtn_variable_store(b, val, dest, type);
1214 } else {
1215 nir_intrinsic_instr *copy =
1216 nir_intrinsic_instr_create(b->shader, nir_intrinsic_copy_var);
1217 copy->variables[0] = nir_deref_as_var(nir_copy_deref(copy, &dest->deref));
1218 copy->variables[1] = nir_deref_as_var(nir_copy_deref(copy, &src->deref));
1219
1220 nir_builder_instr_insert(&b->nb, &copy->instr);
1221 }
1222 }
1223
1224 static void
1225 vtn_handle_variables(struct vtn_builder *b, SpvOp opcode,
1226 const uint32_t *w, unsigned count)
1227 {
1228 switch (opcode) {
1229 case SpvOpVariable: {
1230 struct vtn_type *type =
1231 vtn_value(b, w[1], vtn_value_type_type)->type;
1232 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_deref);
1233
1234 nir_variable *var = ralloc(b->shader, nir_variable);
1235
1236 var->type = type->type;
1237 var->name = ralloc_strdup(var, val->name);
1238
1239 bool builtin_block = false;
1240 if (type->block) {
1241 var->interface_type = type->type;
1242 builtin_block = type->builtin_block;
1243 } else if (glsl_type_is_array(type->type) &&
1244 (type->array_element->block ||
1245 type->array_element->buffer_block)) {
1246 var->interface_type = type->array_element->type;
1247 builtin_block = type->array_element->builtin_block;
1248 } else {
1249 var->interface_type = NULL;
1250 }
1251
1252 switch ((SpvStorageClass)w[3]) {
1253 case SpvStorageClassUniform:
1254 case SpvStorageClassUniformConstant:
1255 var->data.mode = nir_var_uniform;
1256 var->data.read_only = true;
1257 break;
1258 case SpvStorageClassInput:
1259 var->data.mode = nir_var_shader_in;
1260 var->data.read_only = true;
1261 break;
1262 case SpvStorageClassOutput:
1263 var->data.mode = nir_var_shader_out;
1264 break;
1265 case SpvStorageClassPrivateGlobal:
1266 var->data.mode = nir_var_global;
1267 break;
1268 case SpvStorageClassFunction:
1269 var->data.mode = nir_var_local;
1270 break;
1271 case SpvStorageClassWorkgroupLocal:
1272 case SpvStorageClassWorkgroupGlobal:
1273 case SpvStorageClassGeneric:
1274 case SpvStorageClassAtomicCounter:
1275 default:
1276 unreachable("Unhandled variable storage class");
1277 }
1278
1279 if (count > 4) {
1280 assert(count == 5);
1281 var->constant_initializer =
1282 vtn_value(b, w[4], vtn_value_type_constant)->constant;
1283 }
1284
1285 val->deref = nir_deref_var_create(b, var);
1286 val->deref_type = type;
1287
1288 if (b->execution_model == SpvExecutionModelFragment &&
1289 var->data.mode == nir_var_shader_out) {
1290 var->data.location += FRAG_RESULT_DATA0;
1291 } else if (b->execution_model == SpvExecutionModelVertex &&
1292 var->data.mode == nir_var_shader_in) {
1293 var->data.location += VERT_ATTRIB_GENERIC0;
1294 } else if (var->data.mode == nir_var_shader_in ||
1295 var->data.mode == nir_var_shader_out) {
1296 var->data.location += VARYING_SLOT_VAR0;
1297 }
1298
1299 /* We handle decorations last because decorations might cause us to
1300 * over-write other things such as the variable's location and we want
1301 * those changes to stick.
1302 */
1303 vtn_foreach_decoration(b, val, var_decoration_cb, var);
1304
1305 /* If this was a uniform block, then we're not going to actually use the
1306 * variable (we're only going to use it to compute offsets), so don't
1307 * declare it in the shader.
1308 */
1309 if (var->data.mode == nir_var_uniform && var->interface_type)
1310 break;
1311
1312 /* Builtin blocks are lowered to individual variables during SPIR-V ->
1313 * NIR, so don't declare them either.
1314 */
1315 if (builtin_block)
1316 break;
1317
1318 switch (var->data.mode) {
1319 case nir_var_shader_in:
1320 exec_list_push_tail(&b->shader->inputs, &var->node);
1321 break;
1322 case nir_var_shader_out:
1323 exec_list_push_tail(&b->shader->outputs, &var->node);
1324 break;
1325 case nir_var_global:
1326 exec_list_push_tail(&b->shader->globals, &var->node);
1327 break;
1328 case nir_var_local:
1329 exec_list_push_tail(&b->impl->locals, &var->node);
1330 break;
1331 case nir_var_uniform:
1332 exec_list_push_tail(&b->shader->uniforms, &var->node);
1333 break;
1334 case nir_var_system_value:
1335 exec_list_push_tail(&b->shader->system_values, &var->node);
1336 break;
1337 }
1338 break;
1339 }
1340
1341 case SpvOpAccessChain:
1342 case SpvOpInBoundsAccessChain: {
1343 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_deref);
1344 nir_deref_var *base = vtn_value(b, w[3], vtn_value_type_deref)->deref;
1345 val->deref = nir_deref_as_var(nir_copy_deref(b, &base->deref));
1346 struct vtn_type *deref_type = vtn_value(b, w[3], vtn_value_type_deref)->deref_type;
1347
1348 nir_deref *tail = &val->deref->deref;
1349 while (tail->child)
1350 tail = tail->child;
1351
1352 for (unsigned i = 0; i < count - 4; i++) {
1353 assert(w[i + 4] < b->value_id_bound);
1354 struct vtn_value *idx_val = &b->values[w[i + 4]];
1355
1356 enum glsl_base_type base_type = glsl_get_base_type(tail->type);
1357 switch (base_type) {
1358 case GLSL_TYPE_UINT:
1359 case GLSL_TYPE_INT:
1360 case GLSL_TYPE_FLOAT:
1361 case GLSL_TYPE_DOUBLE:
1362 case GLSL_TYPE_BOOL:
1363 case GLSL_TYPE_ARRAY: {
1364 nir_deref_array *deref_arr = nir_deref_array_create(b);
1365 if (base_type == GLSL_TYPE_ARRAY ||
1366 glsl_type_is_matrix(tail->type)) {
1367 deref_type = deref_type->array_element;
1368 } else {
1369 assert(glsl_type_is_vector(tail->type));
1370 deref_type = ralloc(b, struct vtn_type);
1371 deref_type->type = glsl_scalar_type(base_type);
1372 }
1373
1374 deref_arr->deref.type = deref_type->type;
1375
1376 if (idx_val->value_type == vtn_value_type_constant) {
1377 unsigned idx = idx_val->constant->value.u[0];
1378 deref_arr->deref_array_type = nir_deref_array_type_direct;
1379 deref_arr->base_offset = idx;
1380 } else {
1381 assert(idx_val->value_type == vtn_value_type_ssa);
1382 deref_arr->deref_array_type = nir_deref_array_type_indirect;
1383 deref_arr->base_offset = 0;
1384 deref_arr->indirect =
1385 nir_src_for_ssa(vtn_ssa_value(b, w[1])->def);
1386 }
1387 tail->child = &deref_arr->deref;
1388 break;
1389 }
1390
1391 case GLSL_TYPE_STRUCT: {
1392 assert(idx_val->value_type == vtn_value_type_constant);
1393 unsigned idx = idx_val->constant->value.u[0];
1394 deref_type = deref_type->members[idx];
1395 nir_deref_struct *deref_struct = nir_deref_struct_create(b, idx);
1396 deref_struct->deref.type = deref_type->type;
1397 tail->child = &deref_struct->deref;
1398 break;
1399 }
1400 default:
1401 unreachable("Invalid type for deref");
1402 }
1403 tail = tail->child;
1404 }
1405
1406 /* For uniform blocks, we don't resolve the access chain until we
1407 * actually access the variable, so we need to keep around the original
1408 * type of the variable.
1409 */
1410 if (base->var->interface_type && base->var->data.mode == nir_var_uniform)
1411 val->deref_type = vtn_value(b, w[3], vtn_value_type_deref)->deref_type;
1412 else
1413 val->deref_type = deref_type;
1414
1415
1416 break;
1417 }
1418
1419 case SpvOpCopyMemory: {
1420 nir_deref_var *dest = vtn_value(b, w[1], vtn_value_type_deref)->deref;
1421 nir_deref_var *src = vtn_value(b, w[2], vtn_value_type_deref)->deref;
1422 struct vtn_type *type =
1423 vtn_value(b, w[1], vtn_value_type_deref)->deref_type;
1424
1425 vtn_variable_copy(b, src, dest, type);
1426 break;
1427 }
1428
1429 case SpvOpLoad: {
1430 nir_deref_var *src = vtn_value(b, w[3], vtn_value_type_deref)->deref;
1431 struct vtn_type *src_type =
1432 vtn_value(b, w[3], vtn_value_type_deref)->deref_type;
1433
1434 if (glsl_get_base_type(src_type->type) == GLSL_TYPE_SAMPLER) {
1435 vtn_push_value(b, w[2], vtn_value_type_deref)->deref = src;
1436 return;
1437 }
1438
1439 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
1440 val->ssa = vtn_variable_load(b, src, src_type);
1441 break;
1442 }
1443
1444 case SpvOpStore: {
1445 nir_deref_var *dest = vtn_value(b, w[1], vtn_value_type_deref)->deref;
1446 struct vtn_type *dest_type =
1447 vtn_value(b, w[1], vtn_value_type_deref)->deref_type;
1448 struct vtn_ssa_value *src = vtn_ssa_value(b, w[2]);
1449 vtn_variable_store(b, src, dest, dest_type);
1450 break;
1451 }
1452
1453 case SpvOpCopyMemorySized:
1454 case SpvOpArrayLength:
1455 case SpvOpImageTexelPointer:
1456 default:
1457 unreachable("Unhandled opcode");
1458 }
1459 }
1460
1461 static void
1462 vtn_handle_function_call(struct vtn_builder *b, SpvOp opcode,
1463 const uint32_t *w, unsigned count)
1464 {
1465 unreachable("Unhandled opcode");
1466 }
1467
1468 static struct vtn_ssa_value *
1469 vtn_create_ssa_value(struct vtn_builder *b, const struct glsl_type *type)
1470 {
1471 struct vtn_ssa_value *val = rzalloc(b, struct vtn_ssa_value);
1472 val->type = type;
1473
1474 if (!glsl_type_is_vector_or_scalar(type)) {
1475 unsigned elems = glsl_get_length(type);
1476 val->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
1477 for (unsigned i = 0; i < elems; i++) {
1478 const struct glsl_type *child_type;
1479
1480 switch (glsl_get_base_type(type)) {
1481 case GLSL_TYPE_INT:
1482 case GLSL_TYPE_UINT:
1483 case GLSL_TYPE_BOOL:
1484 case GLSL_TYPE_FLOAT:
1485 case GLSL_TYPE_DOUBLE:
1486 child_type = glsl_get_column_type(type);
1487 break;
1488 case GLSL_TYPE_ARRAY:
1489 child_type = glsl_get_array_element(type);
1490 break;
1491 case GLSL_TYPE_STRUCT:
1492 child_type = glsl_get_struct_field(type, i);
1493 break;
1494 default:
1495 unreachable("unkown base type");
1496 }
1497
1498 val->elems[i] = vtn_create_ssa_value(b, child_type);
1499 }
1500 }
1501
1502 return val;
1503 }
1504
1505 static nir_tex_src
1506 vtn_tex_src(struct vtn_builder *b, unsigned index, nir_tex_src_type type)
1507 {
1508 nir_tex_src src;
1509 src.src = nir_src_for_ssa(vtn_value(b, index, vtn_value_type_ssa)->ssa->def);
1510 src.src_type = type;
1511 return src;
1512 }
1513
1514 static void
1515 vtn_handle_texture(struct vtn_builder *b, SpvOp opcode,
1516 const uint32_t *w, unsigned count)
1517 {
1518 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
1519 nir_deref_var *sampler = vtn_value(b, w[3], vtn_value_type_deref)->deref;
1520
1521 nir_tex_src srcs[8]; /* 8 should be enough */
1522 nir_tex_src *p = srcs;
1523
1524 unsigned idx = 4;
1525
1526 unsigned coord_components = 0;
1527 switch (opcode) {
1528 case SpvOpImageSampleImplicitLod:
1529 case SpvOpImageSampleExplicitLod:
1530 case SpvOpImageSampleDrefImplicitLod:
1531 case SpvOpImageSampleDrefExplicitLod:
1532 case SpvOpImageSampleProjImplicitLod:
1533 case SpvOpImageSampleProjExplicitLod:
1534 case SpvOpImageSampleProjDrefImplicitLod:
1535 case SpvOpImageSampleProjDrefExplicitLod:
1536 case SpvOpImageFetch:
1537 case SpvOpImageGather:
1538 case SpvOpImageDrefGather:
1539 case SpvOpImageQueryLod: {
1540 /* All these types have the coordinate as their first real argument */
1541 struct vtn_ssa_value *coord = vtn_ssa_value(b, w[idx++]);
1542 coord_components = glsl_get_vector_elements(coord->type);
1543 p->src = nir_src_for_ssa(coord->def);
1544 p->src_type = nir_tex_src_coord;
1545 p++;
1546 break;
1547 }
1548
1549 default:
1550 break;
1551 }
1552
1553 nir_texop texop;
1554 switch (opcode) {
1555 case SpvOpImageSampleImplicitLod:
1556 texop = nir_texop_tex;
1557 break;
1558
1559 case SpvOpImageSampleExplicitLod:
1560 case SpvOpImageSampleDrefImplicitLod:
1561 case SpvOpImageSampleDrefExplicitLod:
1562 case SpvOpImageSampleProjImplicitLod:
1563 case SpvOpImageSampleProjExplicitLod:
1564 case SpvOpImageSampleProjDrefImplicitLod:
1565 case SpvOpImageSampleProjDrefExplicitLod:
1566 case SpvOpImageFetch:
1567 case SpvOpImageGather:
1568 case SpvOpImageDrefGather:
1569 case SpvOpImageQuerySizeLod:
1570 case SpvOpImageQuerySize:
1571 case SpvOpImageQueryLod:
1572 case SpvOpImageQueryLevels:
1573 case SpvOpImageQuerySamples:
1574 default:
1575 unreachable("Unhandled opcode");
1576 }
1577
1578 /* From now on, the remaining sources are "Optional Image Operands." */
1579 if (idx < count) {
1580 /* XXX handle these (bias, lod, etc.) */
1581 assert(0);
1582 }
1583
1584
1585 nir_tex_instr *instr = nir_tex_instr_create(b->shader, p - srcs);
1586
1587 const struct glsl_type *sampler_type = nir_deref_tail(&sampler->deref)->type;
1588 instr->sampler_dim = glsl_get_sampler_dim(sampler_type);
1589
1590 switch (glsl_get_sampler_result_type(sampler_type)) {
1591 case GLSL_TYPE_FLOAT: instr->dest_type = nir_type_float; break;
1592 case GLSL_TYPE_INT: instr->dest_type = nir_type_int; break;
1593 case GLSL_TYPE_UINT: instr->dest_type = nir_type_unsigned; break;
1594 case GLSL_TYPE_BOOL: instr->dest_type = nir_type_bool; break;
1595 default:
1596 unreachable("Invalid base type for sampler result");
1597 }
1598
1599 instr->op = texop;
1600 memcpy(instr->src, srcs, instr->num_srcs * sizeof(*instr->src));
1601 instr->coord_components = coord_components;
1602 instr->is_array = glsl_sampler_type_is_array(sampler_type);
1603 instr->is_shadow = glsl_sampler_type_is_shadow(sampler_type);
1604
1605 instr->sampler = nir_deref_as_var(nir_copy_deref(instr, &sampler->deref));
1606
1607 nir_ssa_dest_init(&instr->instr, &instr->dest, 4, NULL);
1608 val->ssa = vtn_create_ssa_value(b, glsl_vector_type(GLSL_TYPE_FLOAT, 4));
1609 val->ssa->def = &instr->dest.ssa;
1610
1611 nir_builder_instr_insert(&b->nb, &instr->instr);
1612 }
1613
1614
1615 static nir_alu_instr *
1616 create_vec(void *mem_ctx, unsigned num_components)
1617 {
1618 nir_op op;
1619 switch (num_components) {
1620 case 1: op = nir_op_fmov; break;
1621 case 2: op = nir_op_vec2; break;
1622 case 3: op = nir_op_vec3; break;
1623 case 4: op = nir_op_vec4; break;
1624 default: unreachable("bad vector size");
1625 }
1626
1627 nir_alu_instr *vec = nir_alu_instr_create(mem_ctx, op);
1628 nir_ssa_dest_init(&vec->instr, &vec->dest.dest, num_components, NULL);
1629 vec->dest.write_mask = (1 << num_components) - 1;
1630
1631 return vec;
1632 }
1633
1634 static struct vtn_ssa_value *
1635 vtn_transpose(struct vtn_builder *b, struct vtn_ssa_value *src)
1636 {
1637 if (src->transposed)
1638 return src->transposed;
1639
1640 struct vtn_ssa_value *dest =
1641 vtn_create_ssa_value(b, glsl_transposed_type(src->type));
1642
1643 for (unsigned i = 0; i < glsl_get_matrix_columns(dest->type); i++) {
1644 nir_alu_instr *vec = create_vec(b, glsl_get_matrix_columns(src->type));
1645 if (glsl_type_is_vector_or_scalar(src->type)) {
1646 vec->src[0].src = nir_src_for_ssa(src->def);
1647 vec->src[0].swizzle[0] = i;
1648 } else {
1649 for (unsigned j = 0; j < glsl_get_matrix_columns(src->type); j++) {
1650 vec->src[j].src = nir_src_for_ssa(src->elems[j]->def);
1651 vec->src[j].swizzle[0] = i;
1652 }
1653 }
1654 nir_builder_instr_insert(&b->nb, &vec->instr);
1655 dest->elems[i]->def = &vec->dest.dest.ssa;
1656 }
1657
1658 dest->transposed = src;
1659
1660 return dest;
1661 }
1662
1663 /*
1664 * Normally, column vectors in SPIR-V correspond to a single NIR SSA
1665 * definition. But for matrix multiplies, we want to do one routine for
1666 * multiplying a matrix by a matrix and then pretend that vectors are matrices
1667 * with one column. So we "wrap" these things, and unwrap the result before we
1668 * send it off.
1669 */
1670
1671 static struct vtn_ssa_value *
1672 vtn_wrap_matrix(struct vtn_builder *b, struct vtn_ssa_value *val)
1673 {
1674 if (val == NULL)
1675 return NULL;
1676
1677 if (glsl_type_is_matrix(val->type))
1678 return val;
1679
1680 struct vtn_ssa_value *dest = rzalloc(b, struct vtn_ssa_value);
1681 dest->type = val->type;
1682 dest->elems = ralloc_array(b, struct vtn_ssa_value *, 1);
1683 dest->elems[0] = val;
1684
1685 return dest;
1686 }
1687
1688 static struct vtn_ssa_value *
1689 vtn_unwrap_matrix(struct vtn_ssa_value *val)
1690 {
1691 if (glsl_type_is_matrix(val->type))
1692 return val;
1693
1694 return val->elems[0];
1695 }
1696
1697 static struct vtn_ssa_value *
1698 vtn_matrix_multiply(struct vtn_builder *b,
1699 struct vtn_ssa_value *_src0, struct vtn_ssa_value *_src1)
1700 {
1701
1702 struct vtn_ssa_value *src0 = vtn_wrap_matrix(b, _src0);
1703 struct vtn_ssa_value *src1 = vtn_wrap_matrix(b, _src1);
1704 struct vtn_ssa_value *src0_transpose = vtn_wrap_matrix(b, _src0->transposed);
1705 struct vtn_ssa_value *src1_transpose = vtn_wrap_matrix(b, _src1->transposed);
1706
1707 unsigned src0_rows = glsl_get_vector_elements(src0->type);
1708 unsigned src0_columns = glsl_get_matrix_columns(src0->type);
1709 unsigned src1_columns = glsl_get_matrix_columns(src1->type);
1710
1711 struct vtn_ssa_value *dest =
1712 vtn_create_ssa_value(b, glsl_matrix_type(glsl_get_base_type(src0->type),
1713 src0_rows, src1_columns));
1714
1715 dest = vtn_wrap_matrix(b, dest);
1716
1717 bool transpose_result = false;
1718 if (src0_transpose && src1_transpose) {
1719 /* transpose(A) * transpose(B) = transpose(B * A) */
1720 src1 = src0_transpose;
1721 src0 = src1_transpose;
1722 src0_transpose = NULL;
1723 src1_transpose = NULL;
1724 transpose_result = true;
1725 }
1726
1727 if (src0_transpose && !src1_transpose &&
1728 glsl_get_base_type(src0->type) == GLSL_TYPE_FLOAT) {
1729 /* We already have the rows of src0 and the columns of src1 available,
1730 * so we can just take the dot product of each row with each column to
1731 * get the result.
1732 */
1733
1734 for (unsigned i = 0; i < src1_columns; i++) {
1735 nir_alu_instr *vec = create_vec(b, src0_rows);
1736 for (unsigned j = 0; j < src0_rows; j++) {
1737 vec->src[j].src =
1738 nir_src_for_ssa(nir_fdot(&b->nb, src0_transpose->elems[j]->def,
1739 src1->elems[i]->def));
1740 }
1741
1742 nir_builder_instr_insert(&b->nb, &vec->instr);
1743 dest->elems[i]->def = &vec->dest.dest.ssa;
1744 }
1745 } else {
1746 /* We don't handle the case where src1 is transposed but not src0, since
1747 * the general case only uses individual components of src1 so the
1748 * optimizer should chew through the transpose we emitted for src1.
1749 */
1750
1751 for (unsigned i = 0; i < src1_columns; i++) {
1752 /* dest[i] = sum(src0[j] * src1[i][j] for all j) */
1753 dest->elems[i]->def =
1754 nir_fmul(&b->nb, src0->elems[0]->def,
1755 vtn_vector_extract(b, src1->elems[i]->def, 0));
1756 for (unsigned j = 1; j < src0_columns; j++) {
1757 dest->elems[i]->def =
1758 nir_fadd(&b->nb, dest->elems[i]->def,
1759 nir_fmul(&b->nb, src0->elems[j]->def,
1760 vtn_vector_extract(b,
1761 src1->elems[i]->def, j)));
1762 }
1763 }
1764 }
1765
1766 dest = vtn_unwrap_matrix(dest);
1767
1768 if (transpose_result)
1769 dest = vtn_transpose(b, dest);
1770
1771 return dest;
1772 }
1773
1774 static struct vtn_ssa_value *
1775 vtn_mat_times_scalar(struct vtn_builder *b,
1776 struct vtn_ssa_value *mat,
1777 nir_ssa_def *scalar)
1778 {
1779 struct vtn_ssa_value *dest = vtn_create_ssa_value(b, mat->type);
1780 for (unsigned i = 0; i < glsl_get_matrix_columns(mat->type); i++) {
1781 if (glsl_get_base_type(mat->type) == GLSL_TYPE_FLOAT)
1782 dest->elems[i]->def = nir_fmul(&b->nb, mat->elems[i]->def, scalar);
1783 else
1784 dest->elems[i]->def = nir_imul(&b->nb, mat->elems[i]->def, scalar);
1785 }
1786
1787 return dest;
1788 }
1789
1790 static void
1791 vtn_handle_matrix_alu(struct vtn_builder *b, SpvOp opcode,
1792 const uint32_t *w, unsigned count)
1793 {
1794 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
1795
1796 switch (opcode) {
1797 case SpvOpTranspose: {
1798 struct vtn_ssa_value *src = vtn_ssa_value(b, w[3]);
1799 val->ssa = vtn_transpose(b, src);
1800 break;
1801 }
1802
1803 case SpvOpOuterProduct: {
1804 struct vtn_ssa_value *src0 = vtn_ssa_value(b, w[3]);
1805 struct vtn_ssa_value *src1 = vtn_ssa_value(b, w[4]);
1806
1807 val->ssa = vtn_matrix_multiply(b, src0, vtn_transpose(b, src1));
1808 break;
1809 }
1810
1811 case SpvOpMatrixTimesScalar: {
1812 struct vtn_ssa_value *mat = vtn_ssa_value(b, w[3]);
1813 struct vtn_ssa_value *scalar = vtn_ssa_value(b, w[4]);
1814
1815 if (mat->transposed) {
1816 val->ssa = vtn_transpose(b, vtn_mat_times_scalar(b, mat->transposed,
1817 scalar->def));
1818 } else {
1819 val->ssa = vtn_mat_times_scalar(b, mat, scalar->def);
1820 }
1821 break;
1822 }
1823
1824 case SpvOpVectorTimesMatrix:
1825 case SpvOpMatrixTimesVector:
1826 case SpvOpMatrixTimesMatrix: {
1827 struct vtn_ssa_value *src0 = vtn_ssa_value(b, w[3]);
1828 struct vtn_ssa_value *src1 = vtn_ssa_value(b, w[4]);
1829
1830 val->ssa = vtn_matrix_multiply(b, src0, src1);
1831 break;
1832 }
1833
1834 default: unreachable("unknown matrix opcode");
1835 }
1836 }
1837
1838 static void
1839 vtn_handle_alu(struct vtn_builder *b, SpvOp opcode,
1840 const uint32_t *w, unsigned count)
1841 {
1842 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
1843 const struct glsl_type *type =
1844 vtn_value(b, w[1], vtn_value_type_type)->type->type;
1845 val->ssa = vtn_create_ssa_value(b, type);
1846
1847 /* Collect the various SSA sources */
1848 unsigned num_inputs = count - 3;
1849 nir_ssa_def *src[4];
1850 for (unsigned i = 0; i < num_inputs; i++)
1851 src[i] = vtn_ssa_value(b, w[i + 3])->def;
1852
1853 /* Indicates that the first two arguments should be swapped. This is
1854 * used for implementing greater-than and less-than-or-equal.
1855 */
1856 bool swap = false;
1857
1858 nir_op op;
1859 switch (opcode) {
1860 /* Basic ALU operations */
1861 case SpvOpSNegate: op = nir_op_ineg; break;
1862 case SpvOpFNegate: op = nir_op_fneg; break;
1863 case SpvOpNot: op = nir_op_inot; break;
1864
1865 case SpvOpAny:
1866 switch (src[0]->num_components) {
1867 case 1: op = nir_op_imov; break;
1868 case 2: op = nir_op_bany2; break;
1869 case 3: op = nir_op_bany3; break;
1870 case 4: op = nir_op_bany4; break;
1871 }
1872 break;
1873
1874 case SpvOpAll:
1875 switch (src[0]->num_components) {
1876 case 1: op = nir_op_imov; break;
1877 case 2: op = nir_op_ball2; break;
1878 case 3: op = nir_op_ball3; break;
1879 case 4: op = nir_op_ball4; break;
1880 }
1881 break;
1882
1883 case SpvOpIAdd: op = nir_op_iadd; break;
1884 case SpvOpFAdd: op = nir_op_fadd; break;
1885 case SpvOpISub: op = nir_op_isub; break;
1886 case SpvOpFSub: op = nir_op_fsub; break;
1887 case SpvOpIMul: op = nir_op_imul; break;
1888 case SpvOpFMul: op = nir_op_fmul; break;
1889 case SpvOpUDiv: op = nir_op_udiv; break;
1890 case SpvOpSDiv: op = nir_op_idiv; break;
1891 case SpvOpFDiv: op = nir_op_fdiv; break;
1892 case SpvOpUMod: op = nir_op_umod; break;
1893 case SpvOpSMod: op = nir_op_umod; break; /* FIXME? */
1894 case SpvOpFMod: op = nir_op_fmod; break;
1895
1896 case SpvOpDot:
1897 assert(src[0]->num_components == src[1]->num_components);
1898 switch (src[0]->num_components) {
1899 case 1: op = nir_op_fmul; break;
1900 case 2: op = nir_op_fdot2; break;
1901 case 3: op = nir_op_fdot3; break;
1902 case 4: op = nir_op_fdot4; break;
1903 }
1904 break;
1905
1906 case SpvOpShiftRightLogical: op = nir_op_ushr; break;
1907 case SpvOpShiftRightArithmetic: op = nir_op_ishr; break;
1908 case SpvOpShiftLeftLogical: op = nir_op_ishl; break;
1909 case SpvOpLogicalOr: op = nir_op_ior; break;
1910 case SpvOpLogicalEqual: op = nir_op_ieq; break;
1911 case SpvOpLogicalNotEqual: op = nir_op_ine; break;
1912 case SpvOpLogicalAnd: op = nir_op_iand; break;
1913 case SpvOpBitwiseOr: op = nir_op_ior; break;
1914 case SpvOpBitwiseXor: op = nir_op_ixor; break;
1915 case SpvOpBitwiseAnd: op = nir_op_iand; break;
1916 case SpvOpSelect: op = nir_op_bcsel; break;
1917 case SpvOpIEqual: op = nir_op_ieq; break;
1918
1919 /* Comparisons: (TODO: How do we want to handled ordered/unordered?) */
1920 case SpvOpFOrdEqual: op = nir_op_feq; break;
1921 case SpvOpFUnordEqual: op = nir_op_feq; break;
1922 case SpvOpINotEqual: op = nir_op_ine; break;
1923 case SpvOpFOrdNotEqual: op = nir_op_fne; break;
1924 case SpvOpFUnordNotEqual: op = nir_op_fne; break;
1925 case SpvOpULessThan: op = nir_op_ult; break;
1926 case SpvOpSLessThan: op = nir_op_ilt; break;
1927 case SpvOpFOrdLessThan: op = nir_op_flt; break;
1928 case SpvOpFUnordLessThan: op = nir_op_flt; break;
1929 case SpvOpUGreaterThan: op = nir_op_ult; swap = true; break;
1930 case SpvOpSGreaterThan: op = nir_op_ilt; swap = true; break;
1931 case SpvOpFOrdGreaterThan: op = nir_op_flt; swap = true; break;
1932 case SpvOpFUnordGreaterThan: op = nir_op_flt; swap = true; break;
1933 case SpvOpULessThanEqual: op = nir_op_uge; swap = true; break;
1934 case SpvOpSLessThanEqual: op = nir_op_ige; swap = true; break;
1935 case SpvOpFOrdLessThanEqual: op = nir_op_fge; swap = true; break;
1936 case SpvOpFUnordLessThanEqual: op = nir_op_fge; swap = true; break;
1937 case SpvOpUGreaterThanEqual: op = nir_op_uge; break;
1938 case SpvOpSGreaterThanEqual: op = nir_op_ige; break;
1939 case SpvOpFOrdGreaterThanEqual: op = nir_op_fge; break;
1940 case SpvOpFUnordGreaterThanEqual:op = nir_op_fge; break;
1941
1942 /* Conversions: */
1943 case SpvOpConvertFToU: op = nir_op_f2u; break;
1944 case SpvOpConvertFToS: op = nir_op_f2i; break;
1945 case SpvOpConvertSToF: op = nir_op_i2f; break;
1946 case SpvOpConvertUToF: op = nir_op_u2f; break;
1947 case SpvOpBitcast: op = nir_op_imov; break;
1948 case SpvOpUConvert:
1949 case SpvOpSConvert:
1950 op = nir_op_imov; /* TODO: NIR is 32-bit only; these are no-ops. */
1951 break;
1952 case SpvOpFConvert:
1953 op = nir_op_fmov;
1954 break;
1955
1956 /* Derivatives: */
1957 case SpvOpDPdx: op = nir_op_fddx; break;
1958 case SpvOpDPdy: op = nir_op_fddy; break;
1959 case SpvOpDPdxFine: op = nir_op_fddx_fine; break;
1960 case SpvOpDPdyFine: op = nir_op_fddy_fine; break;
1961 case SpvOpDPdxCoarse: op = nir_op_fddx_coarse; break;
1962 case SpvOpDPdyCoarse: op = nir_op_fddy_coarse; break;
1963 case SpvOpFwidth:
1964 val->ssa->def = nir_fadd(&b->nb,
1965 nir_fabs(&b->nb, nir_fddx(&b->nb, src[0])),
1966 nir_fabs(&b->nb, nir_fddx(&b->nb, src[1])));
1967 return;
1968 case SpvOpFwidthFine:
1969 val->ssa->def = nir_fadd(&b->nb,
1970 nir_fabs(&b->nb, nir_fddx_fine(&b->nb, src[0])),
1971 nir_fabs(&b->nb, nir_fddx_fine(&b->nb, src[1])));
1972 return;
1973 case SpvOpFwidthCoarse:
1974 val->ssa->def = nir_fadd(&b->nb,
1975 nir_fabs(&b->nb, nir_fddx_coarse(&b->nb, src[0])),
1976 nir_fabs(&b->nb, nir_fddx_coarse(&b->nb, src[1])));
1977 return;
1978
1979 case SpvOpVectorTimesScalar:
1980 /* The builder will take care of splatting for us. */
1981 val->ssa->def = nir_fmul(&b->nb, src[0], src[1]);
1982 return;
1983
1984 case SpvOpSRem:
1985 case SpvOpFRem:
1986 unreachable("No NIR equivalent");
1987
1988 case SpvOpIsNan:
1989 case SpvOpIsInf:
1990 case SpvOpIsFinite:
1991 case SpvOpIsNormal:
1992 case SpvOpSignBitSet:
1993 case SpvOpLessOrGreater:
1994 case SpvOpOrdered:
1995 case SpvOpUnordered:
1996 default:
1997 unreachable("Unhandled opcode");
1998 }
1999
2000 if (swap) {
2001 nir_ssa_def *tmp = src[0];
2002 src[0] = src[1];
2003 src[1] = tmp;
2004 }
2005
2006 nir_alu_instr *instr = nir_alu_instr_create(b->shader, op);
2007 nir_ssa_dest_init(&instr->instr, &instr->dest.dest,
2008 glsl_get_vector_elements(type), val->name);
2009 instr->dest.write_mask = (1 << glsl_get_vector_elements(type)) - 1;
2010 val->ssa->def = &instr->dest.dest.ssa;
2011
2012 for (unsigned i = 0; i < nir_op_infos[op].num_inputs; i++)
2013 instr->src[i].src = nir_src_for_ssa(src[i]);
2014
2015 nir_builder_instr_insert(&b->nb, &instr->instr);
2016 }
2017
2018 static nir_ssa_def *
2019 vtn_vector_extract(struct vtn_builder *b, nir_ssa_def *src, unsigned index)
2020 {
2021 unsigned swiz[4] = { index };
2022 return nir_swizzle(&b->nb, src, swiz, 1, true);
2023 }
2024
2025
2026 static nir_ssa_def *
2027 vtn_vector_insert(struct vtn_builder *b, nir_ssa_def *src, nir_ssa_def *insert,
2028 unsigned index)
2029 {
2030 nir_alu_instr *vec = create_vec(b->shader, src->num_components);
2031
2032 for (unsigned i = 0; i < src->num_components; i++) {
2033 if (i == index) {
2034 vec->src[i].src = nir_src_for_ssa(insert);
2035 } else {
2036 vec->src[i].src = nir_src_for_ssa(src);
2037 vec->src[i].swizzle[0] = i;
2038 }
2039 }
2040
2041 nir_builder_instr_insert(&b->nb, &vec->instr);
2042
2043 return &vec->dest.dest.ssa;
2044 }
2045
2046 static nir_ssa_def *
2047 vtn_vector_extract_dynamic(struct vtn_builder *b, nir_ssa_def *src,
2048 nir_ssa_def *index)
2049 {
2050 nir_ssa_def *dest = vtn_vector_extract(b, src, 0);
2051 for (unsigned i = 1; i < src->num_components; i++)
2052 dest = nir_bcsel(&b->nb, nir_ieq(&b->nb, index, nir_imm_int(&b->nb, i)),
2053 vtn_vector_extract(b, src, i), dest);
2054
2055 return dest;
2056 }
2057
2058 static nir_ssa_def *
2059 vtn_vector_insert_dynamic(struct vtn_builder *b, nir_ssa_def *src,
2060 nir_ssa_def *insert, nir_ssa_def *index)
2061 {
2062 nir_ssa_def *dest = vtn_vector_insert(b, src, insert, 0);
2063 for (unsigned i = 1; i < src->num_components; i++)
2064 dest = nir_bcsel(&b->nb, nir_ieq(&b->nb, index, nir_imm_int(&b->nb, i)),
2065 vtn_vector_insert(b, src, insert, i), dest);
2066
2067 return dest;
2068 }
2069
2070 static nir_ssa_def *
2071 vtn_vector_shuffle(struct vtn_builder *b, unsigned num_components,
2072 nir_ssa_def *src0, nir_ssa_def *src1,
2073 const uint32_t *indices)
2074 {
2075 nir_alu_instr *vec = create_vec(b->shader, num_components);
2076
2077 nir_ssa_undef_instr *undef = nir_ssa_undef_instr_create(b->shader, 1);
2078 nir_builder_instr_insert(&b->nb, &undef->instr);
2079
2080 for (unsigned i = 0; i < num_components; i++) {
2081 uint32_t index = indices[i];
2082 if (index == 0xffffffff) {
2083 vec->src[i].src = nir_src_for_ssa(&undef->def);
2084 } else if (index < src0->num_components) {
2085 vec->src[i].src = nir_src_for_ssa(src0);
2086 vec->src[i].swizzle[0] = index;
2087 } else {
2088 vec->src[i].src = nir_src_for_ssa(src1);
2089 vec->src[i].swizzle[0] = index - src0->num_components;
2090 }
2091 }
2092
2093 nir_builder_instr_insert(&b->nb, &vec->instr);
2094
2095 return &vec->dest.dest.ssa;
2096 }
2097
2098 /*
2099 * Concatentates a number of vectors/scalars together to produce a vector
2100 */
2101 static nir_ssa_def *
2102 vtn_vector_construct(struct vtn_builder *b, unsigned num_components,
2103 unsigned num_srcs, nir_ssa_def **srcs)
2104 {
2105 nir_alu_instr *vec = create_vec(b->shader, num_components);
2106
2107 unsigned dest_idx = 0;
2108 for (unsigned i = 0; i < num_srcs; i++) {
2109 nir_ssa_def *src = srcs[i];
2110 for (unsigned j = 0; j < src->num_components; j++) {
2111 vec->src[dest_idx].src = nir_src_for_ssa(src);
2112 vec->src[dest_idx].swizzle[0] = j;
2113 dest_idx++;
2114 }
2115 }
2116
2117 nir_builder_instr_insert(&b->nb, &vec->instr);
2118
2119 return &vec->dest.dest.ssa;
2120 }
2121
2122 static struct vtn_ssa_value *
2123 vtn_composite_copy(void *mem_ctx, struct vtn_ssa_value *src)
2124 {
2125 struct vtn_ssa_value *dest = rzalloc(mem_ctx, struct vtn_ssa_value);
2126 dest->type = src->type;
2127
2128 if (glsl_type_is_vector_or_scalar(src->type)) {
2129 dest->def = src->def;
2130 } else {
2131 unsigned elems = glsl_get_length(src->type);
2132
2133 dest->elems = ralloc_array(mem_ctx, struct vtn_ssa_value *, elems);
2134 for (unsigned i = 0; i < elems; i++)
2135 dest->elems[i] = vtn_composite_copy(mem_ctx, src->elems[i]);
2136 }
2137
2138 return dest;
2139 }
2140
2141 static struct vtn_ssa_value *
2142 vtn_composite_insert(struct vtn_builder *b, struct vtn_ssa_value *src,
2143 struct vtn_ssa_value *insert, const uint32_t *indices,
2144 unsigned num_indices)
2145 {
2146 struct vtn_ssa_value *dest = vtn_composite_copy(b, src);
2147
2148 struct vtn_ssa_value *cur = dest;
2149 unsigned i;
2150 for (i = 0; i < num_indices - 1; i++) {
2151 cur = cur->elems[indices[i]];
2152 }
2153
2154 if (glsl_type_is_vector_or_scalar(cur->type)) {
2155 /* According to the SPIR-V spec, OpCompositeInsert may work down to
2156 * the component granularity. In that case, the last index will be
2157 * the index to insert the scalar into the vector.
2158 */
2159
2160 cur->def = vtn_vector_insert(b, cur->def, insert->def, indices[i]);
2161 } else {
2162 cur->elems[indices[i]] = insert;
2163 }
2164
2165 return dest;
2166 }
2167
2168 static struct vtn_ssa_value *
2169 vtn_composite_extract(struct vtn_builder *b, struct vtn_ssa_value *src,
2170 const uint32_t *indices, unsigned num_indices)
2171 {
2172 struct vtn_ssa_value *cur = src;
2173 for (unsigned i = 0; i < num_indices; i++) {
2174 if (glsl_type_is_vector_or_scalar(cur->type)) {
2175 assert(i == num_indices - 1);
2176 /* According to the SPIR-V spec, OpCompositeExtract may work down to
2177 * the component granularity. The last index will be the index of the
2178 * vector to extract.
2179 */
2180
2181 struct vtn_ssa_value *ret = rzalloc(b, struct vtn_ssa_value);
2182 ret->type = glsl_scalar_type(glsl_get_base_type(cur->type));
2183 ret->def = vtn_vector_extract(b, cur->def, indices[i]);
2184 return ret;
2185 }
2186 }
2187
2188 return cur;
2189 }
2190
2191 static void
2192 vtn_handle_composite(struct vtn_builder *b, SpvOp opcode,
2193 const uint32_t *w, unsigned count)
2194 {
2195 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
2196 const struct glsl_type *type =
2197 vtn_value(b, w[1], vtn_value_type_type)->type->type;
2198 val->ssa = vtn_create_ssa_value(b, type);
2199
2200 switch (opcode) {
2201 case SpvOpVectorExtractDynamic:
2202 val->ssa->def = vtn_vector_extract_dynamic(b, vtn_ssa_value(b, w[3])->def,
2203 vtn_ssa_value(b, w[4])->def);
2204 break;
2205
2206 case SpvOpVectorInsertDynamic:
2207 val->ssa->def = vtn_vector_insert_dynamic(b, vtn_ssa_value(b, w[3])->def,
2208 vtn_ssa_value(b, w[4])->def,
2209 vtn_ssa_value(b, w[5])->def);
2210 break;
2211
2212 case SpvOpVectorShuffle:
2213 val->ssa->def = vtn_vector_shuffle(b, glsl_get_vector_elements(type),
2214 vtn_ssa_value(b, w[3])->def,
2215 vtn_ssa_value(b, w[4])->def,
2216 w + 5);
2217 break;
2218
2219 case SpvOpCompositeConstruct: {
2220 unsigned elems = count - 3;
2221 if (glsl_type_is_vector_or_scalar(type)) {
2222 nir_ssa_def *srcs[4];
2223 for (unsigned i = 0; i < elems; i++)
2224 srcs[i] = vtn_ssa_value(b, w[3 + i])->def;
2225 val->ssa->def =
2226 vtn_vector_construct(b, glsl_get_vector_elements(type),
2227 elems, srcs);
2228 } else {
2229 val->ssa->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
2230 for (unsigned i = 0; i < elems; i++)
2231 val->ssa->elems[i] = vtn_ssa_value(b, w[3 + i]);
2232 }
2233 break;
2234 }
2235 case SpvOpCompositeExtract:
2236 val->ssa = vtn_composite_extract(b, vtn_ssa_value(b, w[3]),
2237 w + 4, count - 4);
2238 break;
2239
2240 case SpvOpCompositeInsert:
2241 val->ssa = vtn_composite_insert(b, vtn_ssa_value(b, w[4]),
2242 vtn_ssa_value(b, w[3]),
2243 w + 5, count - 5);
2244 break;
2245
2246 case SpvOpCopyObject:
2247 val->ssa = vtn_composite_copy(b, vtn_ssa_value(b, w[3]));
2248 break;
2249
2250 default:
2251 unreachable("unknown composite operation");
2252 }
2253 }
2254
2255 static void
2256 vtn_phi_node_init(struct vtn_builder *b, struct vtn_ssa_value *val)
2257 {
2258 if (glsl_type_is_vector_or_scalar(val->type)) {
2259 nir_phi_instr *phi = nir_phi_instr_create(b->shader);
2260 nir_ssa_dest_init(&phi->instr, &phi->dest,
2261 glsl_get_vector_elements(val->type), NULL);
2262 exec_list_make_empty(&phi->srcs);
2263 nir_builder_instr_insert(&b->nb, &phi->instr);
2264 val->def = &phi->dest.ssa;
2265 } else {
2266 unsigned elems = glsl_get_length(val->type);
2267 for (unsigned i = 0; i < elems; i++)
2268 vtn_phi_node_init(b, val->elems[i]);
2269 }
2270 }
2271
2272 static struct vtn_ssa_value *
2273 vtn_phi_node_create(struct vtn_builder *b, const struct glsl_type *type)
2274 {
2275 struct vtn_ssa_value *val = vtn_create_ssa_value(b, type);
2276 vtn_phi_node_init(b, val);
2277 return val;
2278 }
2279
2280 static void
2281 vtn_handle_phi_first_pass(struct vtn_builder *b, const uint32_t *w)
2282 {
2283 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
2284 const struct glsl_type *type =
2285 vtn_value(b, w[1], vtn_value_type_type)->type->type;
2286 val->ssa = vtn_phi_node_create(b, type);
2287 }
2288
2289 static void
2290 vtn_phi_node_add_src(struct vtn_ssa_value *phi, const nir_block *pred,
2291 struct vtn_ssa_value *val)
2292 {
2293 assert(phi->type == val->type);
2294 if (glsl_type_is_vector_or_scalar(phi->type)) {
2295 nir_phi_instr *phi_instr = nir_instr_as_phi(phi->def->parent_instr);
2296 nir_phi_src *src = ralloc(phi_instr, nir_phi_src);
2297 src->pred = (nir_block *) pred;
2298 src->src = nir_src_for_ssa(val->def);
2299 exec_list_push_tail(&phi_instr->srcs, &src->node);
2300 } else {
2301 unsigned elems = glsl_get_length(phi->type);
2302 for (unsigned i = 0; i < elems; i++)
2303 vtn_phi_node_add_src(phi->elems[i], pred, val->elems[i]);
2304 }
2305 }
2306
2307 static struct vtn_ssa_value *
2308 vtn_get_phi_node_src(struct vtn_builder *b, nir_block *block,
2309 const struct glsl_type *type, const uint32_t *w,
2310 unsigned count)
2311 {
2312 struct hash_entry *entry = _mesa_hash_table_search(b->block_table, block);
2313 if (entry) {
2314 struct vtn_block *spv_block = entry->data;
2315 for (unsigned off = 4; off < count; off += 2) {
2316 if (spv_block == vtn_value(b, w[off], vtn_value_type_block)->block) {
2317 return vtn_ssa_value(b, w[off - 1]);
2318 }
2319 }
2320 }
2321
2322 b->nb.cursor = nir_before_block(block);
2323 struct vtn_ssa_value *phi = vtn_phi_node_create(b, type);
2324
2325 struct set_entry *entry2;
2326 set_foreach(block->predecessors, entry2) {
2327 nir_block *pred = (nir_block *) entry2->key;
2328 struct vtn_ssa_value *val = vtn_get_phi_node_src(b, pred, type, w,
2329 count);
2330 vtn_phi_node_add_src(phi, pred, val);
2331 }
2332
2333 return phi;
2334 }
2335
2336 static bool
2337 vtn_handle_phi_second_pass(struct vtn_builder *b, SpvOp opcode,
2338 const uint32_t *w, unsigned count)
2339 {
2340 if (opcode == SpvOpLabel) {
2341 b->block = vtn_value(b, w[1], vtn_value_type_block)->block;
2342 return true;
2343 }
2344
2345 if (opcode != SpvOpPhi)
2346 return true;
2347
2348 struct vtn_ssa_value *phi = vtn_value(b, w[2], vtn_value_type_ssa)->ssa;
2349
2350 struct set_entry *entry;
2351 set_foreach(b->block->block->predecessors, entry) {
2352 nir_block *pred = (nir_block *) entry->key;
2353
2354 struct vtn_ssa_value *val = vtn_get_phi_node_src(b, pred, phi->type, w,
2355 count);
2356 vtn_phi_node_add_src(phi, pred, val);
2357 }
2358
2359 return true;
2360 }
2361
2362 static bool
2363 vtn_handle_preamble_instruction(struct vtn_builder *b, SpvOp opcode,
2364 const uint32_t *w, unsigned count)
2365 {
2366 switch (opcode) {
2367 case SpvOpSource:
2368 case SpvOpSourceExtension:
2369 case SpvOpExtension:
2370 /* Unhandled, but these are for debug so that's ok. */
2371 break;
2372
2373 case SpvOpCapability:
2374 /*
2375 * TODO properly handle these and give a real error if asking for too
2376 * much.
2377 */
2378 assert(w[1] == SpvCapabilityMatrix ||
2379 w[1] == SpvCapabilityShader);
2380 break;
2381
2382 case SpvOpExtInstImport:
2383 vtn_handle_extension(b, opcode, w, count);
2384 break;
2385
2386 case SpvOpMemoryModel:
2387 assert(w[1] == SpvAddressingModelLogical);
2388 assert(w[2] == SpvMemoryModelGLSL450);
2389 break;
2390
2391 case SpvOpEntryPoint:
2392 assert(b->entry_point == NULL);
2393 b->entry_point = &b->values[w[2]];
2394 b->execution_model = w[1];
2395 break;
2396
2397 case SpvOpExecutionMode:
2398 /* TODO */
2399 break;
2400
2401 case SpvOpString:
2402 vtn_push_value(b, w[1], vtn_value_type_string)->str =
2403 vtn_string_literal(b, &w[2], count - 2);
2404 break;
2405
2406 case SpvOpName:
2407 b->values[w[1]].name = vtn_string_literal(b, &w[2], count - 2);
2408 break;
2409
2410 case SpvOpMemberName:
2411 /* TODO */
2412 break;
2413
2414 case SpvOpLine:
2415 break; /* Ignored for now */
2416
2417 case SpvOpDecorationGroup:
2418 case SpvOpDecorate:
2419 case SpvOpMemberDecorate:
2420 case SpvOpGroupDecorate:
2421 case SpvOpGroupMemberDecorate:
2422 vtn_handle_decoration(b, opcode, w, count);
2423 break;
2424
2425 case SpvOpTypeVoid:
2426 case SpvOpTypeBool:
2427 case SpvOpTypeInt:
2428 case SpvOpTypeFloat:
2429 case SpvOpTypeVector:
2430 case SpvOpTypeMatrix:
2431 case SpvOpTypeImage:
2432 case SpvOpTypeSampler:
2433 case SpvOpTypeSampledImage:
2434 case SpvOpTypeArray:
2435 case SpvOpTypeRuntimeArray:
2436 case SpvOpTypeStruct:
2437 case SpvOpTypeOpaque:
2438 case SpvOpTypePointer:
2439 case SpvOpTypeFunction:
2440 case SpvOpTypeEvent:
2441 case SpvOpTypeDeviceEvent:
2442 case SpvOpTypeReserveId:
2443 case SpvOpTypeQueue:
2444 case SpvOpTypePipe:
2445 vtn_handle_type(b, opcode, w, count);
2446 break;
2447
2448 case SpvOpConstantTrue:
2449 case SpvOpConstantFalse:
2450 case SpvOpConstant:
2451 case SpvOpConstantComposite:
2452 case SpvOpConstantSampler:
2453 case SpvOpSpecConstantTrue:
2454 case SpvOpSpecConstantFalse:
2455 case SpvOpSpecConstant:
2456 case SpvOpSpecConstantComposite:
2457 vtn_handle_constant(b, opcode, w, count);
2458 break;
2459
2460 case SpvOpVariable:
2461 vtn_handle_variables(b, opcode, w, count);
2462 break;
2463
2464 default:
2465 return false; /* End of preamble */
2466 }
2467
2468 return true;
2469 }
2470
2471 static bool
2472 vtn_handle_first_cfg_pass_instruction(struct vtn_builder *b, SpvOp opcode,
2473 const uint32_t *w, unsigned count)
2474 {
2475 switch (opcode) {
2476 case SpvOpFunction: {
2477 assert(b->func == NULL);
2478 b->func = rzalloc(b, struct vtn_function);
2479
2480 const struct glsl_type *result_type =
2481 vtn_value(b, w[1], vtn_value_type_type)->type->type;
2482 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_function);
2483 const struct glsl_type *func_type =
2484 vtn_value(b, w[4], vtn_value_type_type)->type->type;
2485
2486 assert(glsl_get_function_return_type(func_type) == result_type);
2487
2488 nir_function *func =
2489 nir_function_create(b->shader, ralloc_strdup(b->shader, val->name));
2490
2491 nir_function_overload *overload = nir_function_overload_create(func);
2492 overload->num_params = glsl_get_length(func_type);
2493 overload->params = ralloc_array(overload, nir_parameter,
2494 overload->num_params);
2495 for (unsigned i = 0; i < overload->num_params; i++) {
2496 const struct glsl_function_param *param =
2497 glsl_get_function_param(func_type, i);
2498 overload->params[i].type = param->type;
2499 if (param->in) {
2500 if (param->out) {
2501 overload->params[i].param_type = nir_parameter_inout;
2502 } else {
2503 overload->params[i].param_type = nir_parameter_in;
2504 }
2505 } else {
2506 if (param->out) {
2507 overload->params[i].param_type = nir_parameter_out;
2508 } else {
2509 assert(!"Parameter is neither in nor out");
2510 }
2511 }
2512 }
2513 b->func->overload = overload;
2514 break;
2515 }
2516
2517 case SpvOpFunctionEnd:
2518 b->func->end = w;
2519 b->func = NULL;
2520 break;
2521
2522 case SpvOpFunctionParameter:
2523 break; /* Does nothing */
2524
2525 case SpvOpLabel: {
2526 assert(b->block == NULL);
2527 b->block = rzalloc(b, struct vtn_block);
2528 b->block->label = w;
2529 vtn_push_value(b, w[1], vtn_value_type_block)->block = b->block;
2530
2531 if (b->func->start_block == NULL) {
2532 /* This is the first block encountered for this function. In this
2533 * case, we set the start block and add it to the list of
2534 * implemented functions that we'll walk later.
2535 */
2536 b->func->start_block = b->block;
2537 exec_list_push_tail(&b->functions, &b->func->node);
2538 }
2539 break;
2540 }
2541
2542 case SpvOpBranch:
2543 case SpvOpBranchConditional:
2544 case SpvOpSwitch:
2545 case SpvOpKill:
2546 case SpvOpReturn:
2547 case SpvOpReturnValue:
2548 case SpvOpUnreachable:
2549 assert(b->block);
2550 b->block->branch = w;
2551 b->block = NULL;
2552 break;
2553
2554 case SpvOpSelectionMerge:
2555 case SpvOpLoopMerge:
2556 assert(b->block && b->block->merge_op == SpvOpNop);
2557 b->block->merge_op = opcode;
2558 b->block->merge_block_id = w[1];
2559 break;
2560
2561 default:
2562 /* Continue on as per normal */
2563 return true;
2564 }
2565
2566 return true;
2567 }
2568
2569 static bool
2570 vtn_handle_body_instruction(struct vtn_builder *b, SpvOp opcode,
2571 const uint32_t *w, unsigned count)
2572 {
2573 switch (opcode) {
2574 case SpvOpLabel: {
2575 struct vtn_block *block = vtn_value(b, w[1], vtn_value_type_block)->block;
2576 assert(block->block == NULL);
2577
2578 block->block = nir_cursor_current_block(b->nb.cursor);
2579 break;
2580 }
2581
2582 case SpvOpLoopMerge:
2583 case SpvOpSelectionMerge:
2584 /* This is handled by cfg pre-pass and walk_blocks */
2585 break;
2586
2587 case SpvOpUndef:
2588 vtn_push_value(b, w[2], vtn_value_type_undef);
2589 break;
2590
2591 case SpvOpExtInst:
2592 vtn_handle_extension(b, opcode, w, count);
2593 break;
2594
2595 case SpvOpVariable:
2596 case SpvOpLoad:
2597 case SpvOpStore:
2598 case SpvOpCopyMemory:
2599 case SpvOpCopyMemorySized:
2600 case SpvOpAccessChain:
2601 case SpvOpInBoundsAccessChain:
2602 case SpvOpArrayLength:
2603 case SpvOpImageTexelPointer:
2604 vtn_handle_variables(b, opcode, w, count);
2605 break;
2606
2607 case SpvOpFunctionCall:
2608 vtn_handle_function_call(b, opcode, w, count);
2609 break;
2610
2611 case SpvOpImageSampleImplicitLod:
2612 case SpvOpImageSampleExplicitLod:
2613 case SpvOpImageSampleDrefImplicitLod:
2614 case SpvOpImageSampleDrefExplicitLod:
2615 case SpvOpImageSampleProjImplicitLod:
2616 case SpvOpImageSampleProjExplicitLod:
2617 case SpvOpImageSampleProjDrefImplicitLod:
2618 case SpvOpImageSampleProjDrefExplicitLod:
2619 case SpvOpImageFetch:
2620 case SpvOpImageGather:
2621 case SpvOpImageDrefGather:
2622 case SpvOpImageQuerySizeLod:
2623 case SpvOpImageQuerySize:
2624 case SpvOpImageQueryLod:
2625 case SpvOpImageQueryLevels:
2626 case SpvOpImageQuerySamples:
2627 vtn_handle_texture(b, opcode, w, count);
2628 break;
2629
2630 case SpvOpSNegate:
2631 case SpvOpFNegate:
2632 case SpvOpNot:
2633 case SpvOpAny:
2634 case SpvOpAll:
2635 case SpvOpConvertFToU:
2636 case SpvOpConvertFToS:
2637 case SpvOpConvertSToF:
2638 case SpvOpConvertUToF:
2639 case SpvOpUConvert:
2640 case SpvOpSConvert:
2641 case SpvOpFConvert:
2642 case SpvOpConvertPtrToU:
2643 case SpvOpConvertUToPtr:
2644 case SpvOpPtrCastToGeneric:
2645 case SpvOpGenericCastToPtr:
2646 case SpvOpBitcast:
2647 case SpvOpIsNan:
2648 case SpvOpIsInf:
2649 case SpvOpIsFinite:
2650 case SpvOpIsNormal:
2651 case SpvOpSignBitSet:
2652 case SpvOpLessOrGreater:
2653 case SpvOpOrdered:
2654 case SpvOpUnordered:
2655 case SpvOpIAdd:
2656 case SpvOpFAdd:
2657 case SpvOpISub:
2658 case SpvOpFSub:
2659 case SpvOpIMul:
2660 case SpvOpFMul:
2661 case SpvOpUDiv:
2662 case SpvOpSDiv:
2663 case SpvOpFDiv:
2664 case SpvOpUMod:
2665 case SpvOpSRem:
2666 case SpvOpSMod:
2667 case SpvOpFRem:
2668 case SpvOpFMod:
2669 case SpvOpVectorTimesScalar:
2670 case SpvOpDot:
2671 case SpvOpShiftRightLogical:
2672 case SpvOpShiftRightArithmetic:
2673 case SpvOpShiftLeftLogical:
2674 case SpvOpLogicalOr:
2675 case SpvOpLogicalEqual:
2676 case SpvOpLogicalNotEqual:
2677 case SpvOpLogicalAnd:
2678 case SpvOpBitwiseOr:
2679 case SpvOpBitwiseXor:
2680 case SpvOpBitwiseAnd:
2681 case SpvOpSelect:
2682 case SpvOpIEqual:
2683 case SpvOpFOrdEqual:
2684 case SpvOpFUnordEqual:
2685 case SpvOpINotEqual:
2686 case SpvOpFOrdNotEqual:
2687 case SpvOpFUnordNotEqual:
2688 case SpvOpULessThan:
2689 case SpvOpSLessThan:
2690 case SpvOpFOrdLessThan:
2691 case SpvOpFUnordLessThan:
2692 case SpvOpUGreaterThan:
2693 case SpvOpSGreaterThan:
2694 case SpvOpFOrdGreaterThan:
2695 case SpvOpFUnordGreaterThan:
2696 case SpvOpULessThanEqual:
2697 case SpvOpSLessThanEqual:
2698 case SpvOpFOrdLessThanEqual:
2699 case SpvOpFUnordLessThanEqual:
2700 case SpvOpUGreaterThanEqual:
2701 case SpvOpSGreaterThanEqual:
2702 case SpvOpFOrdGreaterThanEqual:
2703 case SpvOpFUnordGreaterThanEqual:
2704 case SpvOpDPdx:
2705 case SpvOpDPdy:
2706 case SpvOpFwidth:
2707 case SpvOpDPdxFine:
2708 case SpvOpDPdyFine:
2709 case SpvOpFwidthFine:
2710 case SpvOpDPdxCoarse:
2711 case SpvOpDPdyCoarse:
2712 case SpvOpFwidthCoarse:
2713 vtn_handle_alu(b, opcode, w, count);
2714 break;
2715
2716 case SpvOpTranspose:
2717 case SpvOpOuterProduct:
2718 case SpvOpMatrixTimesScalar:
2719 case SpvOpVectorTimesMatrix:
2720 case SpvOpMatrixTimesVector:
2721 case SpvOpMatrixTimesMatrix:
2722 vtn_handle_matrix_alu(b, opcode, w, count);
2723 break;
2724
2725 case SpvOpVectorExtractDynamic:
2726 case SpvOpVectorInsertDynamic:
2727 case SpvOpVectorShuffle:
2728 case SpvOpCompositeConstruct:
2729 case SpvOpCompositeExtract:
2730 case SpvOpCompositeInsert:
2731 case SpvOpCopyObject:
2732 vtn_handle_composite(b, opcode, w, count);
2733 break;
2734
2735 case SpvOpPhi:
2736 vtn_handle_phi_first_pass(b, w);
2737 break;
2738
2739 default:
2740 unreachable("Unhandled opcode");
2741 }
2742
2743 return true;
2744 }
2745
2746 static void
2747 vtn_walk_blocks(struct vtn_builder *b, struct vtn_block *start,
2748 struct vtn_block *break_block, struct vtn_block *cont_block,
2749 struct vtn_block *end_block)
2750 {
2751 struct vtn_block *block = start;
2752 while (block != end_block) {
2753 if (block->merge_op == SpvOpLoopMerge) {
2754 /* This is the jump into a loop. */
2755 struct vtn_block *new_cont_block = block;
2756 struct vtn_block *new_break_block =
2757 vtn_value(b, block->merge_block_id, vtn_value_type_block)->block;
2758
2759 nir_loop *loop = nir_loop_create(b->shader);
2760 nir_cf_node_insert(b->nb.cursor, &loop->cf_node);
2761
2762 /* Reset the merge_op to prerevent infinite recursion */
2763 block->merge_op = SpvOpNop;
2764
2765 b->nb.cursor = nir_after_cf_list(&loop->body);
2766 vtn_walk_blocks(b, block, new_break_block, new_cont_block, NULL);
2767
2768 b->nb.cursor = nir_after_cf_node(&loop->cf_node);
2769 block = new_break_block;
2770 continue;
2771 }
2772
2773 const uint32_t *w = block->branch;
2774 SpvOp branch_op = w[0] & SpvOpCodeMask;
2775
2776 b->block = block;
2777 vtn_foreach_instruction(b, block->label, block->branch,
2778 vtn_handle_body_instruction);
2779
2780 nir_block *cur_block = nir_cursor_current_block(b->nb.cursor);
2781 assert(cur_block == block->block);
2782 _mesa_hash_table_insert(b->block_table, cur_block, block);
2783
2784 switch (branch_op) {
2785 case SpvOpBranch: {
2786 struct vtn_block *branch_block =
2787 vtn_value(b, w[1], vtn_value_type_block)->block;
2788
2789 if (branch_block == break_block) {
2790 nir_jump_instr *jump = nir_jump_instr_create(b->shader,
2791 nir_jump_break);
2792 nir_builder_instr_insert(&b->nb, &jump->instr);
2793
2794 return;
2795 } else if (branch_block == cont_block) {
2796 nir_jump_instr *jump = nir_jump_instr_create(b->shader,
2797 nir_jump_continue);
2798 nir_builder_instr_insert(&b->nb, &jump->instr);
2799
2800 return;
2801 } else if (branch_block == end_block) {
2802 /* We're branching to the merge block of an if, since for loops
2803 * and functions end_block == NULL, so we're done here.
2804 */
2805 return;
2806 } else {
2807 /* We're branching to another block, and according to the rules,
2808 * we can only branch to another block with one predecessor (so
2809 * we're the only one jumping to it) so we can just process it
2810 * next.
2811 */
2812 block = branch_block;
2813 continue;
2814 }
2815 }
2816
2817 case SpvOpBranchConditional: {
2818 /* Gather up the branch blocks */
2819 struct vtn_block *then_block =
2820 vtn_value(b, w[2], vtn_value_type_block)->block;
2821 struct vtn_block *else_block =
2822 vtn_value(b, w[3], vtn_value_type_block)->block;
2823
2824 nir_if *if_stmt = nir_if_create(b->shader);
2825 if_stmt->condition = nir_src_for_ssa(vtn_ssa_value(b, w[1])->def);
2826 nir_cf_node_insert(b->nb.cursor, &if_stmt->cf_node);
2827
2828 if (then_block == break_block) {
2829 nir_jump_instr *jump = nir_jump_instr_create(b->shader,
2830 nir_jump_break);
2831 nir_instr_insert_after_cf_list(&if_stmt->then_list,
2832 &jump->instr);
2833 block = else_block;
2834 } else if (else_block == break_block) {
2835 nir_jump_instr *jump = nir_jump_instr_create(b->shader,
2836 nir_jump_break);
2837 nir_instr_insert_after_cf_list(&if_stmt->else_list,
2838 &jump->instr);
2839 block = then_block;
2840 } else if (then_block == cont_block) {
2841 nir_jump_instr *jump = nir_jump_instr_create(b->shader,
2842 nir_jump_continue);
2843 nir_instr_insert_after_cf_list(&if_stmt->then_list,
2844 &jump->instr);
2845 block = else_block;
2846 } else if (else_block == cont_block) {
2847 nir_jump_instr *jump = nir_jump_instr_create(b->shader,
2848 nir_jump_continue);
2849 nir_instr_insert_after_cf_list(&if_stmt->else_list,
2850 &jump->instr);
2851 block = then_block;
2852 } else {
2853 /* According to the rules we're branching to two blocks that don't
2854 * have any other predecessors, so we can handle this as a
2855 * conventional if.
2856 */
2857 assert(block->merge_op == SpvOpSelectionMerge);
2858 struct vtn_block *merge_block =
2859 vtn_value(b, block->merge_block_id, vtn_value_type_block)->block;
2860
2861 b->nb.cursor = nir_after_cf_list(&if_stmt->then_list);
2862 vtn_walk_blocks(b, then_block, break_block, cont_block, merge_block);
2863
2864 b->nb.cursor = nir_after_cf_list(&if_stmt->else_list);
2865 vtn_walk_blocks(b, else_block, break_block, cont_block, merge_block);
2866
2867 b->nb.cursor = nir_after_cf_node(&if_stmt->cf_node);
2868 block = merge_block;
2869 continue;
2870 }
2871
2872 /* If we got here then we inserted a predicated break or continue
2873 * above and we need to handle the other case. We already set
2874 * `block` above to indicate what block to visit after the
2875 * predicated break.
2876 */
2877
2878 /* It's possible that the other branch is also a break/continue.
2879 * If it is, we handle that here.
2880 */
2881 if (block == break_block) {
2882 nir_jump_instr *jump = nir_jump_instr_create(b->shader,
2883 nir_jump_break);
2884 nir_builder_instr_insert(&b->nb, &jump->instr);
2885
2886 return;
2887 } else if (block == cont_block) {
2888 nir_jump_instr *jump = nir_jump_instr_create(b->shader,
2889 nir_jump_continue);
2890 nir_builder_instr_insert(&b->nb, &jump->instr);
2891
2892 return;
2893 }
2894
2895 /* If we got here then there was a predicated break/continue but
2896 * the other half of the if has stuff in it. `block` was already
2897 * set above so there is nothing left for us to do.
2898 */
2899 continue;
2900 }
2901
2902 case SpvOpReturn: {
2903 nir_jump_instr *jump = nir_jump_instr_create(b->shader,
2904 nir_jump_return);
2905 nir_builder_instr_insert(&b->nb, &jump->instr);
2906 return;
2907 }
2908
2909 case SpvOpKill: {
2910 nir_intrinsic_instr *discard =
2911 nir_intrinsic_instr_create(b->shader, nir_intrinsic_discard);
2912 nir_builder_instr_insert(&b->nb, &discard->instr);
2913 return;
2914 }
2915
2916 case SpvOpSwitch:
2917 case SpvOpReturnValue:
2918 case SpvOpUnreachable:
2919 default:
2920 unreachable("Unhandled opcode");
2921 }
2922 }
2923 }
2924
2925 nir_shader *
2926 spirv_to_nir(const uint32_t *words, size_t word_count,
2927 gl_shader_stage stage,
2928 const nir_shader_compiler_options *options)
2929 {
2930 const uint32_t *word_end = words + word_count;
2931
2932 /* Handle the SPIR-V header (first 4 dwords) */
2933 assert(word_count > 5);
2934
2935 assert(words[0] == SpvMagicNumber);
2936 assert(words[1] == 99);
2937 /* words[2] == generator magic */
2938 unsigned value_id_bound = words[3];
2939 assert(words[4] == 0);
2940
2941 words+= 5;
2942
2943 nir_shader *shader = nir_shader_create(NULL, stage, options);
2944
2945 /* Initialize the stn_builder object */
2946 struct vtn_builder *b = rzalloc(NULL, struct vtn_builder);
2947 b->shader = shader;
2948 b->value_id_bound = value_id_bound;
2949 b->values = rzalloc_array(b, struct vtn_value, value_id_bound);
2950 exec_list_make_empty(&b->functions);
2951
2952 /* Handle all the preamble instructions */
2953 words = vtn_foreach_instruction(b, words, word_end,
2954 vtn_handle_preamble_instruction);
2955
2956 /* Do a very quick CFG analysis pass */
2957 vtn_foreach_instruction(b, words, word_end,
2958 vtn_handle_first_cfg_pass_instruction);
2959
2960 foreach_list_typed(struct vtn_function, func, node, &b->functions) {
2961 b->impl = nir_function_impl_create(func->overload);
2962 b->const_table = _mesa_hash_table_create(b, _mesa_hash_pointer,
2963 _mesa_key_pointer_equal);
2964 b->block_table = _mesa_hash_table_create(b, _mesa_hash_pointer,
2965 _mesa_key_pointer_equal);
2966 nir_builder_init(&b->nb, b->impl);
2967 b->nb.cursor = nir_after_cf_list(&b->impl->body);
2968 vtn_walk_blocks(b, func->start_block, NULL, NULL, NULL);
2969 vtn_foreach_instruction(b, func->start_block->label, func->end,
2970 vtn_handle_phi_second_pass);
2971 }
2972
2973 /* Because we can still have output reads in NIR, we need to lower
2974 * outputs to temporaries before we are truely finished.
2975 */
2976 nir_lower_outputs_to_temporaries(shader);
2977
2978 ralloc_free(b);
2979
2980 return shader;
2981 }