2 * Copyright © 2015 Intel Corporation
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:
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
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
24 * Jason Ekstrand (jason@jlekstrand.net)
28 #include "vtn_private.h"
29 #include "nir/nir_vla.h"
30 #include "nir/nir_control_flow.h"
31 #include "nir/nir_constant_expressions.h"
32 #include "spirv_info.h"
37 vtn_log(struct vtn_builder *b, enum nir_spirv_debug_level level,
38 size_t spirv_offset, const char *message)
40 if (b->options->debug.func) {
41 b->options->debug.func(b->options->debug.private_data,
42 level, spirv_offset, message);
46 if (level >= NIR_SPIRV_DEBUG_LEVEL_WARNING)
47 fprintf(stderr, "%s\n", message);
52 vtn_logf(struct vtn_builder *b, enum nir_spirv_debug_level level,
53 size_t spirv_offset, const char *fmt, ...)
59 msg = ralloc_vasprintf(NULL, fmt, args);
62 vtn_log(b, level, spirv_offset, msg);
68 vtn_log_err(struct vtn_builder *b,
69 enum nir_spirv_debug_level level, const char *prefix,
70 const char *file, unsigned line,
71 const char *fmt, va_list args)
75 msg = ralloc_strdup(NULL, prefix);
78 ralloc_asprintf_append(&msg, " In file %s:%u\n", file, line);
81 ralloc_asprintf_append(&msg, " ");
83 ralloc_vasprintf_append(&msg, fmt, args);
85 ralloc_asprintf_append(&msg, "\n %zu bytes into the SPIR-V binary",
89 ralloc_asprintf_append(&msg,
90 "\n in SPIR-V source file %s, line %d, col %d",
91 b->file, b->line, b->col);
94 vtn_log(b, level, b->spirv_offset, msg);
100 vtn_dump_shader(struct vtn_builder *b, const char *path, const char *prefix)
105 int len = snprintf(filename, sizeof(filename), "%s/%s-%d.spirv",
106 path, prefix, idx++);
107 if (len < 0 || len >= sizeof(filename))
110 FILE *f = fopen(filename, "w");
114 fwrite(b->spirv, sizeof(*b->spirv), b->spirv_word_count, f);
117 vtn_info("SPIR-V shader dumped to %s", filename);
121 _vtn_warn(struct vtn_builder *b, const char *file, unsigned line,
122 const char *fmt, ...)
127 vtn_log_err(b, NIR_SPIRV_DEBUG_LEVEL_WARNING, "SPIR-V WARNING:\n",
128 file, line, fmt, args);
133 _vtn_fail(struct vtn_builder *b, const char *file, unsigned line,
134 const char *fmt, ...)
139 vtn_log_err(b, NIR_SPIRV_DEBUG_LEVEL_ERROR, "SPIR-V parsing FAILED:\n",
140 file, line, fmt, args);
143 const char *dump_path = getenv("MESA_SPIRV_FAIL_DUMP_PATH");
145 vtn_dump_shader(b, dump_path, "fail");
147 longjmp(b->fail_jump, 1);
150 struct spec_constant_value {
158 static struct vtn_ssa_value *
159 vtn_undef_ssa_value(struct vtn_builder *b, const struct glsl_type *type)
161 struct vtn_ssa_value *val = rzalloc(b, struct vtn_ssa_value);
164 if (glsl_type_is_vector_or_scalar(type)) {
165 unsigned num_components = glsl_get_vector_elements(val->type);
166 unsigned bit_size = glsl_get_bit_size(val->type);
167 val->def = nir_ssa_undef(&b->nb, num_components, bit_size);
169 unsigned elems = glsl_get_length(val->type);
170 val->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
171 if (glsl_type_is_matrix(type)) {
172 const struct glsl_type *elem_type =
173 glsl_vector_type(glsl_get_base_type(type),
174 glsl_get_vector_elements(type));
176 for (unsigned i = 0; i < elems; i++)
177 val->elems[i] = vtn_undef_ssa_value(b, elem_type);
178 } else if (glsl_type_is_array(type)) {
179 const struct glsl_type *elem_type = glsl_get_array_element(type);
180 for (unsigned i = 0; i < elems; i++)
181 val->elems[i] = vtn_undef_ssa_value(b, elem_type);
183 for (unsigned i = 0; i < elems; i++) {
184 const struct glsl_type *elem_type = glsl_get_struct_field(type, i);
185 val->elems[i] = vtn_undef_ssa_value(b, elem_type);
193 static struct vtn_ssa_value *
194 vtn_const_ssa_value(struct vtn_builder *b, nir_constant *constant,
195 const struct glsl_type *type)
197 struct hash_entry *entry = _mesa_hash_table_search(b->const_table, constant);
202 struct vtn_ssa_value *val = rzalloc(b, struct vtn_ssa_value);
205 switch (glsl_get_base_type(type)) {
208 case GLSL_TYPE_INT16:
209 case GLSL_TYPE_UINT16:
210 case GLSL_TYPE_INT64:
211 case GLSL_TYPE_UINT64:
213 case GLSL_TYPE_FLOAT:
214 case GLSL_TYPE_FLOAT16:
215 case GLSL_TYPE_DOUBLE: {
216 int bit_size = glsl_get_bit_size(type);
217 if (glsl_type_is_vector_or_scalar(type)) {
218 unsigned num_components = glsl_get_vector_elements(val->type);
219 nir_load_const_instr *load =
220 nir_load_const_instr_create(b->shader, num_components, bit_size);
222 load->value = constant->values[0];
224 nir_instr_insert_before_cf_list(&b->nb.impl->body, &load->instr);
225 val->def = &load->def;
227 assert(glsl_type_is_matrix(type));
228 unsigned rows = glsl_get_vector_elements(val->type);
229 unsigned columns = glsl_get_matrix_columns(val->type);
230 val->elems = ralloc_array(b, struct vtn_ssa_value *, columns);
232 for (unsigned i = 0; i < columns; i++) {
233 struct vtn_ssa_value *col_val = rzalloc(b, struct vtn_ssa_value);
234 col_val->type = glsl_get_column_type(val->type);
235 nir_load_const_instr *load =
236 nir_load_const_instr_create(b->shader, rows, bit_size);
238 load->value = constant->values[i];
240 nir_instr_insert_before_cf_list(&b->nb.impl->body, &load->instr);
241 col_val->def = &load->def;
243 val->elems[i] = col_val;
249 case GLSL_TYPE_ARRAY: {
250 unsigned elems = glsl_get_length(val->type);
251 val->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
252 const struct glsl_type *elem_type = glsl_get_array_element(val->type);
253 for (unsigned i = 0; i < elems; i++)
254 val->elems[i] = vtn_const_ssa_value(b, constant->elements[i],
259 case GLSL_TYPE_STRUCT: {
260 unsigned elems = glsl_get_length(val->type);
261 val->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
262 for (unsigned i = 0; i < elems; i++) {
263 const struct glsl_type *elem_type =
264 glsl_get_struct_field(val->type, i);
265 val->elems[i] = vtn_const_ssa_value(b, constant->elements[i],
272 vtn_fail("bad constant type");
278 struct vtn_ssa_value *
279 vtn_ssa_value(struct vtn_builder *b, uint32_t value_id)
281 struct vtn_value *val = vtn_untyped_value(b, value_id);
282 switch (val->value_type) {
283 case vtn_value_type_undef:
284 return vtn_undef_ssa_value(b, val->type->type);
286 case vtn_value_type_constant:
287 return vtn_const_ssa_value(b, val->constant, val->type->type);
289 case vtn_value_type_ssa:
292 case vtn_value_type_pointer:
293 vtn_assert(val->pointer->ptr_type && val->pointer->ptr_type->type);
294 struct vtn_ssa_value *ssa =
295 vtn_create_ssa_value(b, val->pointer->ptr_type->type);
296 ssa->def = vtn_pointer_to_ssa(b, val->pointer);
300 vtn_fail("Invalid type for an SSA value");
305 vtn_string_literal(struct vtn_builder *b, const uint32_t *words,
306 unsigned word_count, unsigned *words_used)
308 char *dup = ralloc_strndup(b, (char *)words, word_count * sizeof(*words));
310 /* Ammount of space taken by the string (including the null) */
311 unsigned len = strlen(dup) + 1;
312 *words_used = DIV_ROUND_UP(len, sizeof(*words));
318 vtn_foreach_instruction(struct vtn_builder *b, const uint32_t *start,
319 const uint32_t *end, vtn_instruction_handler handler)
325 const uint32_t *w = start;
327 SpvOp opcode = w[0] & SpvOpCodeMask;
328 unsigned count = w[0] >> SpvWordCountShift;
329 vtn_assert(count >= 1 && w + count <= end);
331 b->spirv_offset = (uint8_t *)w - (uint8_t *)b->spirv;
335 break; /* Do nothing */
338 b->file = vtn_value(b, w[1], vtn_value_type_string)->str;
350 if (!handler(b, opcode, w, count))
368 vtn_handle_extension(struct vtn_builder *b, SpvOp opcode,
369 const uint32_t *w, unsigned count)
372 case SpvOpExtInstImport: {
373 struct vtn_value *val = vtn_push_value(b, w[1], vtn_value_type_extension);
374 if (strcmp((const char *)&w[2], "GLSL.std.450") == 0) {
375 val->ext_handler = vtn_handle_glsl450_instruction;
376 } else if ((strcmp((const char *)&w[2], "SPV_AMD_gcn_shader") == 0)
377 && (b->options && b->options->exts.AMD_gcn_shader)) {
378 val->ext_handler = vtn_handle_amd_gcn_shader_instruction;
380 vtn_fail("Unsupported extension");
386 struct vtn_value *val = vtn_value(b, w[3], vtn_value_type_extension);
387 bool handled = val->ext_handler(b, w[4], w, count);
393 vtn_fail("Unhandled opcode");
398 _foreach_decoration_helper(struct vtn_builder *b,
399 struct vtn_value *base_value,
401 struct vtn_value *value,
402 vtn_decoration_foreach_cb cb, void *data)
404 for (struct vtn_decoration *dec = value->decoration; dec; dec = dec->next) {
406 if (dec->scope == VTN_DEC_DECORATION) {
407 member = parent_member;
408 } else if (dec->scope >= VTN_DEC_STRUCT_MEMBER0) {
409 vtn_fail_if(value->value_type != vtn_value_type_type ||
410 value->type->base_type != vtn_base_type_struct,
411 "OpMemberDecorate and OpGroupMemberDecorate are only "
412 "allowed on OpTypeStruct");
413 /* This means we haven't recursed yet */
414 assert(value == base_value);
416 member = dec->scope - VTN_DEC_STRUCT_MEMBER0;
418 vtn_fail_if(member >= base_value->type->length,
419 "OpMemberDecorate specifies member %d but the "
420 "OpTypeStruct has only %u members",
421 member, base_value->type->length);
423 /* Not a decoration */
424 assert(dec->scope == VTN_DEC_EXECUTION_MODE);
429 assert(dec->group->value_type == vtn_value_type_decoration_group);
430 _foreach_decoration_helper(b, base_value, member, dec->group,
433 cb(b, base_value, member, dec, data);
438 /** Iterates (recursively if needed) over all of the decorations on a value
440 * This function iterates over all of the decorations applied to a given
441 * value. If it encounters a decoration group, it recurses into the group
442 * and iterates over all of those decorations as well.
445 vtn_foreach_decoration(struct vtn_builder *b, struct vtn_value *value,
446 vtn_decoration_foreach_cb cb, void *data)
448 _foreach_decoration_helper(b, value, -1, value, cb, data);
452 vtn_foreach_execution_mode(struct vtn_builder *b, struct vtn_value *value,
453 vtn_execution_mode_foreach_cb cb, void *data)
455 for (struct vtn_decoration *dec = value->decoration; dec; dec = dec->next) {
456 if (dec->scope != VTN_DEC_EXECUTION_MODE)
459 assert(dec->group == NULL);
460 cb(b, value, dec, data);
465 vtn_handle_decoration(struct vtn_builder *b, SpvOp opcode,
466 const uint32_t *w, unsigned count)
468 const uint32_t *w_end = w + count;
469 const uint32_t target = w[1];
473 case SpvOpDecorationGroup:
474 vtn_push_value(b, target, vtn_value_type_decoration_group);
478 case SpvOpMemberDecorate:
479 case SpvOpExecutionMode: {
480 struct vtn_value *val = vtn_untyped_value(b, target);
482 struct vtn_decoration *dec = rzalloc(b, struct vtn_decoration);
485 dec->scope = VTN_DEC_DECORATION;
487 case SpvOpMemberDecorate:
488 dec->scope = VTN_DEC_STRUCT_MEMBER0 + *(w++);
489 vtn_fail_if(dec->scope < VTN_DEC_STRUCT_MEMBER0, /* overflow */
490 "Member argument of OpMemberDecorate too large");
492 case SpvOpExecutionMode:
493 dec->scope = VTN_DEC_EXECUTION_MODE;
496 unreachable("Invalid decoration opcode");
498 dec->decoration = *(w++);
501 /* Link into the list */
502 dec->next = val->decoration;
503 val->decoration = dec;
507 case SpvOpGroupMemberDecorate:
508 case SpvOpGroupDecorate: {
509 struct vtn_value *group =
510 vtn_value(b, target, vtn_value_type_decoration_group);
512 for (; w < w_end; w++) {
513 struct vtn_value *val = vtn_untyped_value(b, *w);
514 struct vtn_decoration *dec = rzalloc(b, struct vtn_decoration);
517 if (opcode == SpvOpGroupDecorate) {
518 dec->scope = VTN_DEC_DECORATION;
520 dec->scope = VTN_DEC_STRUCT_MEMBER0 + *(++w);
521 vtn_fail_if(dec->scope < 0, /* Check for overflow */
522 "Member argument of OpGroupMemberDecorate too large");
525 /* Link into the list */
526 dec->next = val->decoration;
527 val->decoration = dec;
533 unreachable("Unhandled opcode");
537 struct member_decoration_ctx {
539 struct glsl_struct_field *fields;
540 struct vtn_type *type;
543 /** Returns true if two types are "compatible", i.e. you can do an OpLoad,
544 * OpStore, or OpCopyMemory between them without breaking anything.
545 * Technically, the SPIR-V rules require the exact same type ID but this lets
546 * us internally be a bit looser.
549 vtn_types_compatible(struct vtn_builder *b,
550 struct vtn_type *t1, struct vtn_type *t2)
552 if (t1->id == t2->id)
555 if (t1->base_type != t2->base_type)
558 switch (t1->base_type) {
559 case vtn_base_type_void:
560 case vtn_base_type_scalar:
561 case vtn_base_type_vector:
562 case vtn_base_type_matrix:
563 case vtn_base_type_image:
564 case vtn_base_type_sampler:
565 case vtn_base_type_sampled_image:
566 return t1->type == t2->type;
568 case vtn_base_type_array:
569 return t1->length == t2->length &&
570 vtn_types_compatible(b, t1->array_element, t2->array_element);
572 case vtn_base_type_pointer:
573 return vtn_types_compatible(b, t1->deref, t2->deref);
575 case vtn_base_type_struct:
576 if (t1->length != t2->length)
579 for (unsigned i = 0; i < t1->length; i++) {
580 if (!vtn_types_compatible(b, t1->members[i], t2->members[i]))
585 case vtn_base_type_function:
586 /* This case shouldn't get hit since you can't copy around function
587 * types. Just require them to be identical.
592 vtn_fail("Invalid base type");
595 /* does a shallow copy of a vtn_type */
597 static struct vtn_type *
598 vtn_type_copy(struct vtn_builder *b, struct vtn_type *src)
600 struct vtn_type *dest = ralloc(b, struct vtn_type);
603 switch (src->base_type) {
604 case vtn_base_type_void:
605 case vtn_base_type_scalar:
606 case vtn_base_type_vector:
607 case vtn_base_type_matrix:
608 case vtn_base_type_array:
609 case vtn_base_type_pointer:
610 case vtn_base_type_image:
611 case vtn_base_type_sampler:
612 case vtn_base_type_sampled_image:
613 /* Nothing more to do */
616 case vtn_base_type_struct:
617 dest->members = ralloc_array(b, struct vtn_type *, src->length);
618 memcpy(dest->members, src->members,
619 src->length * sizeof(src->members[0]));
621 dest->offsets = ralloc_array(b, unsigned, src->length);
622 memcpy(dest->offsets, src->offsets,
623 src->length * sizeof(src->offsets[0]));
626 case vtn_base_type_function:
627 dest->params = ralloc_array(b, struct vtn_type *, src->length);
628 memcpy(dest->params, src->params, src->length * sizeof(src->params[0]));
635 static struct vtn_type *
636 mutable_matrix_member(struct vtn_builder *b, struct vtn_type *type, int member)
638 type->members[member] = vtn_type_copy(b, type->members[member]);
639 type = type->members[member];
641 /* We may have an array of matrices.... Oh, joy! */
642 while (glsl_type_is_array(type->type)) {
643 type->array_element = vtn_type_copy(b, type->array_element);
644 type = type->array_element;
647 vtn_assert(glsl_type_is_matrix(type->type));
653 struct_member_decoration_cb(struct vtn_builder *b,
654 struct vtn_value *val, int member,
655 const struct vtn_decoration *dec, void *void_ctx)
657 struct member_decoration_ctx *ctx = void_ctx;
662 assert(member < ctx->num_fields);
664 switch (dec->decoration) {
665 case SpvDecorationNonWritable:
666 case SpvDecorationNonReadable:
667 case SpvDecorationRelaxedPrecision:
668 case SpvDecorationVolatile:
669 case SpvDecorationCoherent:
670 case SpvDecorationUniform:
671 break; /* FIXME: Do nothing with this for now. */
672 case SpvDecorationNoPerspective:
673 ctx->fields[member].interpolation = INTERP_MODE_NOPERSPECTIVE;
675 case SpvDecorationFlat:
676 ctx->fields[member].interpolation = INTERP_MODE_FLAT;
678 case SpvDecorationCentroid:
679 ctx->fields[member].centroid = true;
681 case SpvDecorationSample:
682 ctx->fields[member].sample = true;
684 case SpvDecorationStream:
685 /* Vulkan only allows one GS stream */
686 vtn_assert(dec->literals[0] == 0);
688 case SpvDecorationLocation:
689 ctx->fields[member].location = dec->literals[0];
691 case SpvDecorationComponent:
692 break; /* FIXME: What should we do with these? */
693 case SpvDecorationBuiltIn:
694 ctx->type->members[member] = vtn_type_copy(b, ctx->type->members[member]);
695 ctx->type->members[member]->is_builtin = true;
696 ctx->type->members[member]->builtin = dec->literals[0];
697 ctx->type->builtin_block = true;
699 case SpvDecorationOffset:
700 ctx->type->offsets[member] = dec->literals[0];
702 case SpvDecorationMatrixStride:
703 /* Handled as a second pass */
705 case SpvDecorationColMajor:
706 break; /* Nothing to do here. Column-major is the default. */
707 case SpvDecorationRowMajor:
708 mutable_matrix_member(b, ctx->type, member)->row_major = true;
711 case SpvDecorationPatch:
714 case SpvDecorationSpecId:
715 case SpvDecorationBlock:
716 case SpvDecorationBufferBlock:
717 case SpvDecorationArrayStride:
718 case SpvDecorationGLSLShared:
719 case SpvDecorationGLSLPacked:
720 case SpvDecorationInvariant:
721 case SpvDecorationRestrict:
722 case SpvDecorationAliased:
723 case SpvDecorationConstant:
724 case SpvDecorationIndex:
725 case SpvDecorationBinding:
726 case SpvDecorationDescriptorSet:
727 case SpvDecorationLinkageAttributes:
728 case SpvDecorationNoContraction:
729 case SpvDecorationInputAttachmentIndex:
730 vtn_warn("Decoration not allowed on struct members: %s",
731 spirv_decoration_to_string(dec->decoration));
734 case SpvDecorationXfbBuffer:
735 case SpvDecorationXfbStride:
736 vtn_warn("Vulkan does not have transform feedback");
739 case SpvDecorationCPacked:
740 case SpvDecorationSaturatedConversion:
741 case SpvDecorationFuncParamAttr:
742 case SpvDecorationFPRoundingMode:
743 case SpvDecorationFPFastMathMode:
744 case SpvDecorationAlignment:
745 vtn_warn("Decoration only allowed for CL-style kernels: %s",
746 spirv_decoration_to_string(dec->decoration));
750 vtn_fail("Unhandled decoration");
754 /* Matrix strides are handled as a separate pass because we need to know
755 * whether the matrix is row-major or not first.
758 struct_member_matrix_stride_cb(struct vtn_builder *b,
759 struct vtn_value *val, int member,
760 const struct vtn_decoration *dec,
763 if (dec->decoration != SpvDecorationMatrixStride)
766 vtn_fail_if(member < 0,
767 "The MatrixStride decoration is only allowed on members "
770 struct member_decoration_ctx *ctx = void_ctx;
772 struct vtn_type *mat_type = mutable_matrix_member(b, ctx->type, member);
773 if (mat_type->row_major) {
774 mat_type->array_element = vtn_type_copy(b, mat_type->array_element);
775 mat_type->stride = mat_type->array_element->stride;
776 mat_type->array_element->stride = dec->literals[0];
778 vtn_assert(mat_type->array_element->stride > 0);
779 mat_type->stride = dec->literals[0];
784 type_decoration_cb(struct vtn_builder *b,
785 struct vtn_value *val, int member,
786 const struct vtn_decoration *dec, void *ctx)
788 struct vtn_type *type = val->type;
791 /* This should have been handled by OpTypeStruct */
792 assert(val->type->base_type == vtn_base_type_struct);
793 assert(member >= 0 && member < val->type->length);
797 switch (dec->decoration) {
798 case SpvDecorationArrayStride:
799 vtn_assert(type->base_type == vtn_base_type_matrix ||
800 type->base_type == vtn_base_type_array ||
801 type->base_type == vtn_base_type_pointer);
802 type->stride = dec->literals[0];
804 case SpvDecorationBlock:
805 vtn_assert(type->base_type == vtn_base_type_struct);
808 case SpvDecorationBufferBlock:
809 vtn_assert(type->base_type == vtn_base_type_struct);
810 type->buffer_block = true;
812 case SpvDecorationGLSLShared:
813 case SpvDecorationGLSLPacked:
814 /* Ignore these, since we get explicit offsets anyways */
817 case SpvDecorationRowMajor:
818 case SpvDecorationColMajor:
819 case SpvDecorationMatrixStride:
820 case SpvDecorationBuiltIn:
821 case SpvDecorationNoPerspective:
822 case SpvDecorationFlat:
823 case SpvDecorationPatch:
824 case SpvDecorationCentroid:
825 case SpvDecorationSample:
826 case SpvDecorationVolatile:
827 case SpvDecorationCoherent:
828 case SpvDecorationNonWritable:
829 case SpvDecorationNonReadable:
830 case SpvDecorationUniform:
831 case SpvDecorationStream:
832 case SpvDecorationLocation:
833 case SpvDecorationComponent:
834 case SpvDecorationOffset:
835 case SpvDecorationXfbBuffer:
836 case SpvDecorationXfbStride:
837 vtn_warn("Decoration only allowed for struct members: %s",
838 spirv_decoration_to_string(dec->decoration));
841 case SpvDecorationRelaxedPrecision:
842 case SpvDecorationSpecId:
843 case SpvDecorationInvariant:
844 case SpvDecorationRestrict:
845 case SpvDecorationAliased:
846 case SpvDecorationConstant:
847 case SpvDecorationIndex:
848 case SpvDecorationBinding:
849 case SpvDecorationDescriptorSet:
850 case SpvDecorationLinkageAttributes:
851 case SpvDecorationNoContraction:
852 case SpvDecorationInputAttachmentIndex:
853 vtn_warn("Decoration not allowed on types: %s",
854 spirv_decoration_to_string(dec->decoration));
857 case SpvDecorationCPacked:
858 case SpvDecorationSaturatedConversion:
859 case SpvDecorationFuncParamAttr:
860 case SpvDecorationFPRoundingMode:
861 case SpvDecorationFPFastMathMode:
862 case SpvDecorationAlignment:
863 vtn_warn("Decoration only allowed for CL-style kernels: %s",
864 spirv_decoration_to_string(dec->decoration));
868 vtn_fail("Unhandled decoration");
873 translate_image_format(struct vtn_builder *b, SpvImageFormat format)
876 case SpvImageFormatUnknown: return 0; /* GL_NONE */
877 case SpvImageFormatRgba32f: return 0x8814; /* GL_RGBA32F */
878 case SpvImageFormatRgba16f: return 0x881A; /* GL_RGBA16F */
879 case SpvImageFormatR32f: return 0x822E; /* GL_R32F */
880 case SpvImageFormatRgba8: return 0x8058; /* GL_RGBA8 */
881 case SpvImageFormatRgba8Snorm: return 0x8F97; /* GL_RGBA8_SNORM */
882 case SpvImageFormatRg32f: return 0x8230; /* GL_RG32F */
883 case SpvImageFormatRg16f: return 0x822F; /* GL_RG16F */
884 case SpvImageFormatR11fG11fB10f: return 0x8C3A; /* GL_R11F_G11F_B10F */
885 case SpvImageFormatR16f: return 0x822D; /* GL_R16F */
886 case SpvImageFormatRgba16: return 0x805B; /* GL_RGBA16 */
887 case SpvImageFormatRgb10A2: return 0x8059; /* GL_RGB10_A2 */
888 case SpvImageFormatRg16: return 0x822C; /* GL_RG16 */
889 case SpvImageFormatRg8: return 0x822B; /* GL_RG8 */
890 case SpvImageFormatR16: return 0x822A; /* GL_R16 */
891 case SpvImageFormatR8: return 0x8229; /* GL_R8 */
892 case SpvImageFormatRgba16Snorm: return 0x8F9B; /* GL_RGBA16_SNORM */
893 case SpvImageFormatRg16Snorm: return 0x8F99; /* GL_RG16_SNORM */
894 case SpvImageFormatRg8Snorm: return 0x8F95; /* GL_RG8_SNORM */
895 case SpvImageFormatR16Snorm: return 0x8F98; /* GL_R16_SNORM */
896 case SpvImageFormatR8Snorm: return 0x8F94; /* GL_R8_SNORM */
897 case SpvImageFormatRgba32i: return 0x8D82; /* GL_RGBA32I */
898 case SpvImageFormatRgba16i: return 0x8D88; /* GL_RGBA16I */
899 case SpvImageFormatRgba8i: return 0x8D8E; /* GL_RGBA8I */
900 case SpvImageFormatR32i: return 0x8235; /* GL_R32I */
901 case SpvImageFormatRg32i: return 0x823B; /* GL_RG32I */
902 case SpvImageFormatRg16i: return 0x8239; /* GL_RG16I */
903 case SpvImageFormatRg8i: return 0x8237; /* GL_RG8I */
904 case SpvImageFormatR16i: return 0x8233; /* GL_R16I */
905 case SpvImageFormatR8i: return 0x8231; /* GL_R8I */
906 case SpvImageFormatRgba32ui: return 0x8D70; /* GL_RGBA32UI */
907 case SpvImageFormatRgba16ui: return 0x8D76; /* GL_RGBA16UI */
908 case SpvImageFormatRgba8ui: return 0x8D7C; /* GL_RGBA8UI */
909 case SpvImageFormatR32ui: return 0x8236; /* GL_R32UI */
910 case SpvImageFormatRgb10a2ui: return 0x906F; /* GL_RGB10_A2UI */
911 case SpvImageFormatRg32ui: return 0x823C; /* GL_RG32UI */
912 case SpvImageFormatRg16ui: return 0x823A; /* GL_RG16UI */
913 case SpvImageFormatRg8ui: return 0x8238; /* GL_RG8UI */
914 case SpvImageFormatR16ui: return 0x8234; /* GL_R16UI */
915 case SpvImageFormatR8ui: return 0x8232; /* GL_R8UI */
917 vtn_fail("Invalid image format");
921 static struct vtn_type *
922 vtn_type_layout_std430(struct vtn_builder *b, struct vtn_type *type,
923 uint32_t *size_out, uint32_t *align_out)
925 switch (type->base_type) {
926 case vtn_base_type_scalar: {
927 uint32_t comp_size = glsl_get_bit_size(type->type) / 8;
928 *size_out = comp_size;
929 *align_out = comp_size;
933 case vtn_base_type_vector: {
934 uint32_t comp_size = glsl_get_bit_size(type->type) / 8;
935 assert(type->length > 0 && type->length <= 4);
936 unsigned align_comps = type->length == 3 ? 4 : type->length;
937 *size_out = comp_size * type->length,
938 *align_out = comp_size * align_comps;
942 case vtn_base_type_matrix:
943 case vtn_base_type_array: {
944 /* We're going to add an array stride */
945 type = vtn_type_copy(b, type);
946 uint32_t elem_size, elem_align;
947 type->array_element = vtn_type_layout_std430(b, type->array_element,
948 &elem_size, &elem_align);
949 type->stride = vtn_align_u32(elem_size, elem_align);
950 *size_out = type->stride * type->length;
951 *align_out = elem_align;
955 case vtn_base_type_struct: {
956 /* We're going to add member offsets */
957 type = vtn_type_copy(b, type);
960 for (unsigned i = 0; i < type->length; i++) {
961 uint32_t mem_size, mem_align;
962 type->members[i] = vtn_type_layout_std430(b, type->members[i],
963 &mem_size, &mem_align);
964 offset = vtn_align_u32(offset, mem_align);
965 type->offsets[i] = offset;
967 align = MAX2(align, mem_align);
975 unreachable("Invalid SPIR-V type for std430");
980 vtn_handle_type(struct vtn_builder *b, SpvOp opcode,
981 const uint32_t *w, unsigned count)
983 struct vtn_value *val = vtn_push_value(b, w[1], vtn_value_type_type);
985 val->type = rzalloc(b, struct vtn_type);
986 val->type->id = w[1];
990 val->type->base_type = vtn_base_type_void;
991 val->type->type = glsl_void_type();
994 val->type->base_type = vtn_base_type_scalar;
995 val->type->type = glsl_bool_type();
996 val->type->length = 1;
1000 const bool signedness = w[3];
1001 val->type->base_type = vtn_base_type_scalar;
1004 val->type->type = (signedness ? glsl_int64_t_type() : glsl_uint64_t_type());
1007 val->type->type = (signedness ? glsl_int_type() : glsl_uint_type());
1010 val->type->type = (signedness ? glsl_int16_t_type() : glsl_uint16_t_type());
1013 vtn_fail("Invalid int bit size");
1015 val->type->length = 1;
1019 case SpvOpTypeFloat: {
1020 int bit_size = w[2];
1021 val->type->base_type = vtn_base_type_scalar;
1024 val->type->type = glsl_float16_t_type();
1027 val->type->type = glsl_float_type();
1030 val->type->type = glsl_double_type();
1033 vtn_fail("Invalid float bit size");
1035 val->type->length = 1;
1039 case SpvOpTypeVector: {
1040 struct vtn_type *base = vtn_value(b, w[2], vtn_value_type_type)->type;
1041 unsigned elems = w[3];
1043 vtn_fail_if(base->base_type != vtn_base_type_scalar,
1044 "Base type for OpTypeVector must be a scalar");
1045 vtn_fail_if(elems < 2 || elems > 4,
1046 "Invalid component count for OpTypeVector");
1048 val->type->base_type = vtn_base_type_vector;
1049 val->type->type = glsl_vector_type(glsl_get_base_type(base->type), elems);
1050 val->type->length = elems;
1051 val->type->stride = glsl_get_bit_size(base->type) / 8;
1052 val->type->array_element = base;
1056 case SpvOpTypeMatrix: {
1057 struct vtn_type *base = vtn_value(b, w[2], vtn_value_type_type)->type;
1058 unsigned columns = w[3];
1060 vtn_fail_if(base->base_type != vtn_base_type_vector,
1061 "Base type for OpTypeMatrix must be a vector");
1062 vtn_fail_if(columns < 2 || columns > 4,
1063 "Invalid column count for OpTypeMatrix");
1065 val->type->base_type = vtn_base_type_matrix;
1066 val->type->type = glsl_matrix_type(glsl_get_base_type(base->type),
1067 glsl_get_vector_elements(base->type),
1069 vtn_fail_if(glsl_type_is_error(val->type->type),
1070 "Unsupported base type for OpTypeMatrix");
1071 assert(!glsl_type_is_error(val->type->type));
1072 val->type->length = columns;
1073 val->type->array_element = base;
1074 val->type->row_major = false;
1075 val->type->stride = 0;
1079 case SpvOpTypeRuntimeArray:
1080 case SpvOpTypeArray: {
1081 struct vtn_type *array_element =
1082 vtn_value(b, w[2], vtn_value_type_type)->type;
1084 if (opcode == SpvOpTypeRuntimeArray) {
1085 /* A length of 0 is used to denote unsized arrays */
1086 val->type->length = 0;
1089 vtn_value(b, w[3], vtn_value_type_constant)->constant->values[0].u32[0];
1092 val->type->base_type = vtn_base_type_array;
1093 val->type->type = glsl_array_type(array_element->type, val->type->length);
1094 val->type->array_element = array_element;
1095 val->type->stride = 0;
1099 case SpvOpTypeStruct: {
1100 unsigned num_fields = count - 2;
1101 val->type->base_type = vtn_base_type_struct;
1102 val->type->length = num_fields;
1103 val->type->members = ralloc_array(b, struct vtn_type *, num_fields);
1104 val->type->offsets = ralloc_array(b, unsigned, num_fields);
1106 NIR_VLA(struct glsl_struct_field, fields, count);
1107 for (unsigned i = 0; i < num_fields; i++) {
1108 val->type->members[i] =
1109 vtn_value(b, w[i + 2], vtn_value_type_type)->type;
1110 fields[i] = (struct glsl_struct_field) {
1111 .type = val->type->members[i]->type,
1112 .name = ralloc_asprintf(b, "field%d", i),
1117 struct member_decoration_ctx ctx = {
1118 .num_fields = num_fields,
1123 vtn_foreach_decoration(b, val, struct_member_decoration_cb, &ctx);
1124 vtn_foreach_decoration(b, val, struct_member_matrix_stride_cb, &ctx);
1126 const char *name = val->name ? val->name : "struct";
1128 val->type->type = glsl_struct_type(fields, num_fields, name);
1132 case SpvOpTypeFunction: {
1133 val->type->base_type = vtn_base_type_function;
1134 val->type->type = NULL;
1136 val->type->return_type = vtn_value(b, w[2], vtn_value_type_type)->type;
1138 const unsigned num_params = count - 3;
1139 val->type->length = num_params;
1140 val->type->params = ralloc_array(b, struct vtn_type *, num_params);
1141 for (unsigned i = 0; i < count - 3; i++) {
1142 val->type->params[i] =
1143 vtn_value(b, w[i + 3], vtn_value_type_type)->type;
1148 case SpvOpTypePointer: {
1149 SpvStorageClass storage_class = w[2];
1150 struct vtn_type *deref_type =
1151 vtn_value(b, w[3], vtn_value_type_type)->type;
1153 val->type->base_type = vtn_base_type_pointer;
1154 val->type->storage_class = storage_class;
1155 val->type->deref = deref_type;
1157 if (storage_class == SpvStorageClassUniform ||
1158 storage_class == SpvStorageClassStorageBuffer) {
1159 /* These can actually be stored to nir_variables and used as SSA
1160 * values so they need a real glsl_type.
1162 val->type->type = glsl_vector_type(GLSL_TYPE_UINT, 2);
1165 if (storage_class == SpvStorageClassWorkgroup &&
1166 b->options->lower_workgroup_access_to_offsets) {
1167 uint32_t size, align;
1168 val->type->deref = vtn_type_layout_std430(b, val->type->deref,
1170 val->type->length = size;
1171 val->type->align = align;
1172 /* These can actually be stored to nir_variables and used as SSA
1173 * values so they need a real glsl_type.
1175 val->type->type = glsl_uint_type();
1180 case SpvOpTypeImage: {
1181 val->type->base_type = vtn_base_type_image;
1183 const struct vtn_type *sampled_type =
1184 vtn_value(b, w[2], vtn_value_type_type)->type;
1186 vtn_fail_if(sampled_type->base_type != vtn_base_type_scalar ||
1187 glsl_get_bit_size(sampled_type->type) != 32,
1188 "Sampled type of OpTypeImage must be a 32-bit scalar");
1190 enum glsl_sampler_dim dim;
1191 switch ((SpvDim)w[3]) {
1192 case SpvDim1D: dim = GLSL_SAMPLER_DIM_1D; break;
1193 case SpvDim2D: dim = GLSL_SAMPLER_DIM_2D; break;
1194 case SpvDim3D: dim = GLSL_SAMPLER_DIM_3D; break;
1195 case SpvDimCube: dim = GLSL_SAMPLER_DIM_CUBE; break;
1196 case SpvDimRect: dim = GLSL_SAMPLER_DIM_RECT; break;
1197 case SpvDimBuffer: dim = GLSL_SAMPLER_DIM_BUF; break;
1198 case SpvDimSubpassData: dim = GLSL_SAMPLER_DIM_SUBPASS; break;
1200 vtn_fail("Invalid SPIR-V image dimensionality");
1203 bool is_shadow = w[4];
1204 bool is_array = w[5];
1205 bool multisampled = w[6];
1206 unsigned sampled = w[7];
1207 SpvImageFormat format = w[8];
1210 val->type->access_qualifier = w[9];
1212 val->type->access_qualifier = SpvAccessQualifierReadWrite;
1215 if (dim == GLSL_SAMPLER_DIM_2D)
1216 dim = GLSL_SAMPLER_DIM_MS;
1217 else if (dim == GLSL_SAMPLER_DIM_SUBPASS)
1218 dim = GLSL_SAMPLER_DIM_SUBPASS_MS;
1220 vtn_fail("Unsupported multisampled image type");
1223 val->type->image_format = translate_image_format(b, format);
1225 enum glsl_base_type sampled_base_type =
1226 glsl_get_base_type(sampled_type->type);
1228 val->type->sampled = true;
1229 val->type->type = glsl_sampler_type(dim, is_shadow, is_array,
1231 } else if (sampled == 2) {
1232 vtn_assert(!is_shadow);
1233 val->type->sampled = false;
1234 val->type->type = glsl_image_type(dim, is_array, sampled_base_type);
1236 vtn_fail("We need to know if the image will be sampled");
1241 case SpvOpTypeSampledImage:
1242 val->type->base_type = vtn_base_type_sampled_image;
1243 val->type->image = vtn_value(b, w[2], vtn_value_type_type)->type;
1244 val->type->type = val->type->image->type;
1247 case SpvOpTypeSampler:
1248 /* The actual sampler type here doesn't really matter. It gets
1249 * thrown away the moment you combine it with an image. What really
1250 * matters is that it's a sampler type as opposed to an integer type
1251 * so the backend knows what to do.
1253 val->type->base_type = vtn_base_type_sampler;
1254 val->type->type = glsl_bare_sampler_type();
1257 case SpvOpTypeOpaque:
1258 case SpvOpTypeEvent:
1259 case SpvOpTypeDeviceEvent:
1260 case SpvOpTypeReserveId:
1261 case SpvOpTypeQueue:
1264 vtn_fail("Unhandled opcode");
1267 vtn_foreach_decoration(b, val, type_decoration_cb, NULL);
1270 static nir_constant *
1271 vtn_null_constant(struct vtn_builder *b, const struct glsl_type *type)
1273 nir_constant *c = rzalloc(b, nir_constant);
1275 /* For pointers and other typeless things, we have to return something but
1276 * it doesn't matter what.
1281 switch (glsl_get_base_type(type)) {
1283 case GLSL_TYPE_UINT:
1284 case GLSL_TYPE_INT16:
1285 case GLSL_TYPE_UINT16:
1286 case GLSL_TYPE_INT64:
1287 case GLSL_TYPE_UINT64:
1288 case GLSL_TYPE_BOOL:
1289 case GLSL_TYPE_FLOAT:
1290 case GLSL_TYPE_FLOAT16:
1291 case GLSL_TYPE_DOUBLE:
1292 /* Nothing to do here. It's already initialized to zero */
1295 case GLSL_TYPE_ARRAY:
1296 vtn_assert(glsl_get_length(type) > 0);
1297 c->num_elements = glsl_get_length(type);
1298 c->elements = ralloc_array(b, nir_constant *, c->num_elements);
1300 c->elements[0] = vtn_null_constant(b, glsl_get_array_element(type));
1301 for (unsigned i = 1; i < c->num_elements; i++)
1302 c->elements[i] = c->elements[0];
1305 case GLSL_TYPE_STRUCT:
1306 c->num_elements = glsl_get_length(type);
1307 c->elements = ralloc_array(b, nir_constant *, c->num_elements);
1309 for (unsigned i = 0; i < c->num_elements; i++) {
1310 c->elements[i] = vtn_null_constant(b, glsl_get_struct_field(type, i));
1315 vtn_fail("Invalid type for null constant");
1322 spec_constant_decoration_cb(struct vtn_builder *b, struct vtn_value *v,
1323 int member, const struct vtn_decoration *dec,
1326 vtn_assert(member == -1);
1327 if (dec->decoration != SpvDecorationSpecId)
1330 struct spec_constant_value *const_value = data;
1332 for (unsigned i = 0; i < b->num_specializations; i++) {
1333 if (b->specializations[i].id == dec->literals[0]) {
1334 if (const_value->is_double)
1335 const_value->data64 = b->specializations[i].data64;
1337 const_value->data32 = b->specializations[i].data32;
1344 get_specialization(struct vtn_builder *b, struct vtn_value *val,
1345 uint32_t const_value)
1347 struct spec_constant_value data;
1348 data.is_double = false;
1349 data.data32 = const_value;
1350 vtn_foreach_decoration(b, val, spec_constant_decoration_cb, &data);
1355 get_specialization64(struct vtn_builder *b, struct vtn_value *val,
1356 uint64_t const_value)
1358 struct spec_constant_value data;
1359 data.is_double = true;
1360 data.data64 = const_value;
1361 vtn_foreach_decoration(b, val, spec_constant_decoration_cb, &data);
1366 handle_workgroup_size_decoration_cb(struct vtn_builder *b,
1367 struct vtn_value *val,
1369 const struct vtn_decoration *dec,
1372 vtn_assert(member == -1);
1373 if (dec->decoration != SpvDecorationBuiltIn ||
1374 dec->literals[0] != SpvBuiltInWorkgroupSize)
1377 vtn_assert(val->type->type == glsl_vector_type(GLSL_TYPE_UINT, 3));
1379 b->shader->info.cs.local_size[0] = val->constant->values[0].u32[0];
1380 b->shader->info.cs.local_size[1] = val->constant->values[0].u32[1];
1381 b->shader->info.cs.local_size[2] = val->constant->values[0].u32[2];
1385 vtn_handle_constant(struct vtn_builder *b, SpvOp opcode,
1386 const uint32_t *w, unsigned count)
1388 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_constant);
1389 val->constant = rzalloc(b, nir_constant);
1391 case SpvOpConstantTrue:
1392 case SpvOpConstantFalse:
1393 case SpvOpSpecConstantTrue:
1394 case SpvOpSpecConstantFalse: {
1395 vtn_fail_if(val->type->type != glsl_bool_type(),
1396 "Result type of %s must be OpTypeBool",
1397 spirv_op_to_string(opcode));
1399 uint32_t int_val = (opcode == SpvOpConstantTrue ||
1400 opcode == SpvOpSpecConstantTrue);
1402 if (opcode == SpvOpSpecConstantTrue ||
1403 opcode == SpvOpSpecConstantFalse)
1404 int_val = get_specialization(b, val, int_val);
1406 val->constant->values[0].u32[0] = int_val ? NIR_TRUE : NIR_FALSE;
1410 case SpvOpConstant: {
1411 vtn_fail_if(val->type->base_type != vtn_base_type_scalar,
1412 "Result type of %s must be a scalar",
1413 spirv_op_to_string(opcode));
1414 int bit_size = glsl_get_bit_size(val->type->type);
1417 val->constant->values->u64[0] = vtn_u64_literal(&w[3]);
1420 val->constant->values->u32[0] = w[3];
1423 val->constant->values->u16[0] = w[3];
1426 vtn_fail("Unsupported SpvOpConstant bit size");
1431 case SpvOpSpecConstant: {
1432 vtn_fail_if(val->type->base_type != vtn_base_type_scalar,
1433 "Result type of %s must be a scalar",
1434 spirv_op_to_string(opcode));
1435 int bit_size = glsl_get_bit_size(val->type->type);
1438 val->constant->values[0].u64[0] =
1439 get_specialization64(b, val, vtn_u64_literal(&w[3]));
1442 val->constant->values[0].u32[0] = get_specialization(b, val, w[3]);
1445 val->constant->values[0].u16[0] = get_specialization(b, val, w[3]);
1448 vtn_fail("Unsupported SpvOpSpecConstant bit size");
1453 case SpvOpSpecConstantComposite:
1454 case SpvOpConstantComposite: {
1455 unsigned elem_count = count - 3;
1456 vtn_fail_if(elem_count != val->type->length,
1457 "%s has %u constituents, expected %u",
1458 spirv_op_to_string(opcode), elem_count, val->type->length);
1460 nir_constant **elems = ralloc_array(b, nir_constant *, elem_count);
1461 for (unsigned i = 0; i < elem_count; i++)
1462 elems[i] = vtn_value(b, w[i + 3], vtn_value_type_constant)->constant;
1464 switch (val->type->base_type) {
1465 case vtn_base_type_vector: {
1466 assert(glsl_type_is_vector(val->type->type));
1467 int bit_size = glsl_get_bit_size(val->type->type);
1468 for (unsigned i = 0; i < elem_count; i++) {
1471 val->constant->values[0].u64[i] = elems[i]->values[0].u64[0];
1474 val->constant->values[0].u32[i] = elems[i]->values[0].u32[0];
1477 val->constant->values[0].u16[i] = elems[i]->values[0].u16[0];
1480 vtn_fail("Invalid SpvOpConstantComposite bit size");
1486 case vtn_base_type_matrix:
1487 assert(glsl_type_is_matrix(val->type->type));
1488 for (unsigned i = 0; i < elem_count; i++)
1489 val->constant->values[i] = elems[i]->values[0];
1492 case vtn_base_type_struct:
1493 case vtn_base_type_array:
1494 ralloc_steal(val->constant, elems);
1495 val->constant->num_elements = elem_count;
1496 val->constant->elements = elems;
1500 vtn_fail("Result type of %s must be a composite type",
1501 spirv_op_to_string(opcode));
1506 case SpvOpSpecConstantOp: {
1507 SpvOp opcode = get_specialization(b, val, w[3]);
1509 case SpvOpVectorShuffle: {
1510 struct vtn_value *v0 = &b->values[w[4]];
1511 struct vtn_value *v1 = &b->values[w[5]];
1513 vtn_assert(v0->value_type == vtn_value_type_constant ||
1514 v0->value_type == vtn_value_type_undef);
1515 vtn_assert(v1->value_type == vtn_value_type_constant ||
1516 v1->value_type == vtn_value_type_undef);
1518 unsigned len0 = glsl_get_vector_elements(v0->type->type);
1519 unsigned len1 = glsl_get_vector_elements(v1->type->type);
1521 vtn_assert(len0 + len1 < 16);
1523 unsigned bit_size = glsl_get_bit_size(val->type->type);
1524 unsigned bit_size0 = glsl_get_bit_size(v0->type->type);
1525 unsigned bit_size1 = glsl_get_bit_size(v1->type->type);
1527 vtn_assert(bit_size == bit_size0 && bit_size == bit_size1);
1528 (void)bit_size0; (void)bit_size1;
1530 if (bit_size == 64) {
1532 if (v0->value_type == vtn_value_type_constant) {
1533 for (unsigned i = 0; i < len0; i++)
1534 u64[i] = v0->constant->values[0].u64[i];
1536 if (v1->value_type == vtn_value_type_constant) {
1537 for (unsigned i = 0; i < len1; i++)
1538 u64[len0 + i] = v1->constant->values[0].u64[i];
1541 for (unsigned i = 0, j = 0; i < count - 6; i++, j++) {
1542 uint32_t comp = w[i + 6];
1543 /* If component is not used, set the value to a known constant
1544 * to detect if it is wrongly used.
1546 if (comp == (uint32_t)-1)
1547 val->constant->values[0].u64[j] = 0xdeadbeefdeadbeef;
1549 val->constant->values[0].u64[j] = u64[comp];
1552 /* This is for both 32-bit and 16-bit values */
1554 if (v0->value_type == vtn_value_type_constant) {
1555 for (unsigned i = 0; i < len0; i++)
1556 u32[i] = v0->constant->values[0].u32[i];
1558 if (v1->value_type == vtn_value_type_constant) {
1559 for (unsigned i = 0; i < len1; i++)
1560 u32[len0 + i] = v1->constant->values[0].u32[i];
1563 for (unsigned i = 0, j = 0; i < count - 6; i++, j++) {
1564 uint32_t comp = w[i + 6];
1565 /* If component is not used, set the value to a known constant
1566 * to detect if it is wrongly used.
1568 if (comp == (uint32_t)-1)
1569 val->constant->values[0].u32[j] = 0xdeadbeef;
1571 val->constant->values[0].u32[j] = u32[comp];
1577 case SpvOpCompositeExtract:
1578 case SpvOpCompositeInsert: {
1579 struct vtn_value *comp;
1580 unsigned deref_start;
1581 struct nir_constant **c;
1582 if (opcode == SpvOpCompositeExtract) {
1583 comp = vtn_value(b, w[4], vtn_value_type_constant);
1585 c = &comp->constant;
1587 comp = vtn_value(b, w[5], vtn_value_type_constant);
1589 val->constant = nir_constant_clone(comp->constant,
1596 const struct vtn_type *type = comp->type;
1597 for (unsigned i = deref_start; i < count; i++) {
1598 vtn_fail_if(w[i] > type->length,
1599 "%uth index of %s is %u but the type has only "
1600 "%u elements", i - deref_start,
1601 spirv_op_to_string(opcode), w[i], type->length);
1603 switch (type->base_type) {
1604 case vtn_base_type_vector:
1606 type = type->array_element;
1609 case vtn_base_type_matrix:
1610 assert(col == 0 && elem == -1);
1613 type = type->array_element;
1616 case vtn_base_type_array:
1617 c = &(*c)->elements[w[i]];
1618 type = type->array_element;
1621 case vtn_base_type_struct:
1622 c = &(*c)->elements[w[i]];
1623 type = type->members[w[i]];
1627 vtn_fail("%s must only index into composite types",
1628 spirv_op_to_string(opcode));
1632 if (opcode == SpvOpCompositeExtract) {
1636 unsigned num_components = type->length;
1637 unsigned bit_size = glsl_get_bit_size(type->type);
1638 for (unsigned i = 0; i < num_components; i++)
1641 val->constant->values[0].u64[i] = (*c)->values[col].u64[elem + i];
1644 val->constant->values[0].u32[i] = (*c)->values[col].u32[elem + i];
1647 val->constant->values[0].u16[i] = (*c)->values[col].u16[elem + i];
1650 vtn_fail("Invalid SpvOpCompositeExtract bit size");
1654 struct vtn_value *insert =
1655 vtn_value(b, w[4], vtn_value_type_constant);
1656 vtn_assert(insert->type == type);
1658 *c = insert->constant;
1660 unsigned num_components = type->length;
1661 unsigned bit_size = glsl_get_bit_size(type->type);
1662 for (unsigned i = 0; i < num_components; i++)
1665 (*c)->values[col].u64[elem + i] = insert->constant->values[0].u64[i];
1668 (*c)->values[col].u32[elem + i] = insert->constant->values[0].u32[i];
1671 (*c)->values[col].u16[elem + i] = insert->constant->values[0].u16[i];
1674 vtn_fail("Invalid SpvOpCompositeInsert bit size");
1683 nir_alu_type dst_alu_type = nir_get_nir_type_for_glsl_type(val->type->type);
1684 nir_alu_type src_alu_type = dst_alu_type;
1685 unsigned num_components = glsl_get_vector_elements(val->type->type);
1688 vtn_assert(count <= 7);
1693 /* We have a source in a conversion */
1695 nir_get_nir_type_for_glsl_type(
1696 vtn_value(b, w[4], vtn_value_type_constant)->type->type);
1697 /* We use the bitsize of the conversion source to evaluate the opcode later */
1698 bit_size = glsl_get_bit_size(
1699 vtn_value(b, w[4], vtn_value_type_constant)->type->type);
1702 bit_size = glsl_get_bit_size(val->type->type);
1705 nir_op op = vtn_nir_alu_op_for_spirv_opcode(b, opcode, &swap,
1708 nir_const_value src[4];
1710 for (unsigned i = 0; i < count - 4; i++) {
1712 vtn_value(b, w[4 + i], vtn_value_type_constant)->constant;
1714 unsigned j = swap ? 1 - i : i;
1715 src[j] = c->values[0];
1718 val->constant->values[0] =
1719 nir_eval_const_opcode(op, num_components, bit_size, src);
1726 case SpvOpConstantNull:
1727 val->constant = vtn_null_constant(b, val->type->type);
1730 case SpvOpConstantSampler:
1731 vtn_fail("OpConstantSampler requires Kernel Capability");
1735 vtn_fail("Unhandled opcode");
1738 /* Now that we have the value, update the workgroup size if needed */
1739 vtn_foreach_decoration(b, val, handle_workgroup_size_decoration_cb, NULL);
1743 vtn_handle_function_call(struct vtn_builder *b, SpvOp opcode,
1744 const uint32_t *w, unsigned count)
1746 struct vtn_type *res_type = vtn_value(b, w[1], vtn_value_type_type)->type;
1747 struct vtn_function *vtn_callee =
1748 vtn_value(b, w[3], vtn_value_type_function)->func;
1749 struct nir_function *callee = vtn_callee->impl->function;
1751 vtn_callee->referenced = true;
1753 nir_call_instr *call = nir_call_instr_create(b->nb.shader, callee);
1754 for (unsigned i = 0; i < call->num_params; i++) {
1755 unsigned arg_id = w[4 + i];
1756 struct vtn_value *arg = vtn_untyped_value(b, arg_id);
1757 if (arg->value_type == vtn_value_type_pointer &&
1758 arg->pointer->ptr_type->type == NULL) {
1759 nir_deref_var *d = vtn_pointer_to_deref(b, arg->pointer);
1760 call->params[i] = nir_deref_var_clone(d, call);
1762 struct vtn_ssa_value *arg_ssa = vtn_ssa_value(b, arg_id);
1764 /* Make a temporary to store the argument in */
1766 nir_local_variable_create(b->nb.impl, arg_ssa->type, "arg_tmp");
1767 call->params[i] = nir_deref_var_create(call, tmp);
1769 vtn_local_store(b, arg_ssa, call->params[i]);
1773 nir_variable *out_tmp = NULL;
1774 vtn_assert(res_type->type == callee->return_type);
1775 if (!glsl_type_is_void(callee->return_type)) {
1776 out_tmp = nir_local_variable_create(b->nb.impl, callee->return_type,
1778 call->return_deref = nir_deref_var_create(call, out_tmp);
1781 nir_builder_instr_insert(&b->nb, &call->instr);
1783 if (glsl_type_is_void(callee->return_type)) {
1784 vtn_push_value(b, w[2], vtn_value_type_undef);
1786 vtn_push_ssa(b, w[2], res_type, vtn_local_load(b, call->return_deref));
1790 struct vtn_ssa_value *
1791 vtn_create_ssa_value(struct vtn_builder *b, const struct glsl_type *type)
1793 struct vtn_ssa_value *val = rzalloc(b, struct vtn_ssa_value);
1796 if (!glsl_type_is_vector_or_scalar(type)) {
1797 unsigned elems = glsl_get_length(type);
1798 val->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
1799 for (unsigned i = 0; i < elems; i++) {
1800 const struct glsl_type *child_type;
1802 switch (glsl_get_base_type(type)) {
1804 case GLSL_TYPE_UINT:
1805 case GLSL_TYPE_INT16:
1806 case GLSL_TYPE_UINT16:
1807 case GLSL_TYPE_INT64:
1808 case GLSL_TYPE_UINT64:
1809 case GLSL_TYPE_BOOL:
1810 case GLSL_TYPE_FLOAT:
1811 case GLSL_TYPE_FLOAT16:
1812 case GLSL_TYPE_DOUBLE:
1813 child_type = glsl_get_column_type(type);
1815 case GLSL_TYPE_ARRAY:
1816 child_type = glsl_get_array_element(type);
1818 case GLSL_TYPE_STRUCT:
1819 child_type = glsl_get_struct_field(type, i);
1822 vtn_fail("unkown base type");
1825 val->elems[i] = vtn_create_ssa_value(b, child_type);
1833 vtn_tex_src(struct vtn_builder *b, unsigned index, nir_tex_src_type type)
1836 src.src = nir_src_for_ssa(vtn_ssa_value(b, index)->def);
1837 src.src_type = type;
1842 vtn_handle_texture(struct vtn_builder *b, SpvOp opcode,
1843 const uint32_t *w, unsigned count)
1845 if (opcode == SpvOpSampledImage) {
1846 struct vtn_value *val =
1847 vtn_push_value(b, w[2], vtn_value_type_sampled_image);
1848 val->sampled_image = ralloc(b, struct vtn_sampled_image);
1849 val->sampled_image->type =
1850 vtn_value(b, w[1], vtn_value_type_type)->type;
1851 val->sampled_image->image =
1852 vtn_value(b, w[3], vtn_value_type_pointer)->pointer;
1853 val->sampled_image->sampler =
1854 vtn_value(b, w[4], vtn_value_type_pointer)->pointer;
1856 } else if (opcode == SpvOpImage) {
1857 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_pointer);
1858 struct vtn_value *src_val = vtn_untyped_value(b, w[3]);
1859 if (src_val->value_type == vtn_value_type_sampled_image) {
1860 val->pointer = src_val->sampled_image->image;
1862 vtn_assert(src_val->value_type == vtn_value_type_pointer);
1863 val->pointer = src_val->pointer;
1868 struct vtn_type *ret_type = vtn_value(b, w[1], vtn_value_type_type)->type;
1869 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
1871 struct vtn_sampled_image sampled;
1872 struct vtn_value *sampled_val = vtn_untyped_value(b, w[3]);
1873 if (sampled_val->value_type == vtn_value_type_sampled_image) {
1874 sampled = *sampled_val->sampled_image;
1876 vtn_assert(sampled_val->value_type == vtn_value_type_pointer);
1877 sampled.type = sampled_val->pointer->type;
1878 sampled.image = NULL;
1879 sampled.sampler = sampled_val->pointer;
1882 const struct glsl_type *image_type = sampled.type->type;
1883 const enum glsl_sampler_dim sampler_dim = glsl_get_sampler_dim(image_type);
1884 const bool is_array = glsl_sampler_type_is_array(image_type);
1885 const bool is_shadow = glsl_sampler_type_is_shadow(image_type);
1887 /* Figure out the base texture operation */
1890 case SpvOpImageSampleImplicitLod:
1891 case SpvOpImageSampleDrefImplicitLod:
1892 case SpvOpImageSampleProjImplicitLod:
1893 case SpvOpImageSampleProjDrefImplicitLod:
1894 texop = nir_texop_tex;
1897 case SpvOpImageSampleExplicitLod:
1898 case SpvOpImageSampleDrefExplicitLod:
1899 case SpvOpImageSampleProjExplicitLod:
1900 case SpvOpImageSampleProjDrefExplicitLod:
1901 texop = nir_texop_txl;
1904 case SpvOpImageFetch:
1905 if (glsl_get_sampler_dim(image_type) == GLSL_SAMPLER_DIM_MS) {
1906 texop = nir_texop_txf_ms;
1908 texop = nir_texop_txf;
1912 case SpvOpImageGather:
1913 case SpvOpImageDrefGather:
1914 texop = nir_texop_tg4;
1917 case SpvOpImageQuerySizeLod:
1918 case SpvOpImageQuerySize:
1919 texop = nir_texop_txs;
1922 case SpvOpImageQueryLod:
1923 texop = nir_texop_lod;
1926 case SpvOpImageQueryLevels:
1927 texop = nir_texop_query_levels;
1930 case SpvOpImageQuerySamples:
1931 texop = nir_texop_texture_samples;
1935 vtn_fail("Unhandled opcode");
1938 nir_tex_src srcs[8]; /* 8 should be enough */
1939 nir_tex_src *p = srcs;
1943 struct nir_ssa_def *coord;
1944 unsigned coord_components;
1946 case SpvOpImageSampleImplicitLod:
1947 case SpvOpImageSampleExplicitLod:
1948 case SpvOpImageSampleDrefImplicitLod:
1949 case SpvOpImageSampleDrefExplicitLod:
1950 case SpvOpImageSampleProjImplicitLod:
1951 case SpvOpImageSampleProjExplicitLod:
1952 case SpvOpImageSampleProjDrefImplicitLod:
1953 case SpvOpImageSampleProjDrefExplicitLod:
1954 case SpvOpImageFetch:
1955 case SpvOpImageGather:
1956 case SpvOpImageDrefGather:
1957 case SpvOpImageQueryLod: {
1958 /* All these types have the coordinate as their first real argument */
1959 switch (sampler_dim) {
1960 case GLSL_SAMPLER_DIM_1D:
1961 case GLSL_SAMPLER_DIM_BUF:
1962 coord_components = 1;
1964 case GLSL_SAMPLER_DIM_2D:
1965 case GLSL_SAMPLER_DIM_RECT:
1966 case GLSL_SAMPLER_DIM_MS:
1967 coord_components = 2;
1969 case GLSL_SAMPLER_DIM_3D:
1970 case GLSL_SAMPLER_DIM_CUBE:
1971 coord_components = 3;
1974 vtn_fail("Invalid sampler type");
1977 if (is_array && texop != nir_texop_lod)
1980 coord = vtn_ssa_value(b, w[idx++])->def;
1981 p->src = nir_src_for_ssa(nir_channels(&b->nb, coord,
1982 (1 << coord_components) - 1));
1983 p->src_type = nir_tex_src_coord;
1990 coord_components = 0;
1995 case SpvOpImageSampleProjImplicitLod:
1996 case SpvOpImageSampleProjExplicitLod:
1997 case SpvOpImageSampleProjDrefImplicitLod:
1998 case SpvOpImageSampleProjDrefExplicitLod:
1999 /* These have the projector as the last coordinate component */
2000 p->src = nir_src_for_ssa(nir_channel(&b->nb, coord, coord_components));
2001 p->src_type = nir_tex_src_projector;
2009 unsigned gather_component = 0;
2011 case SpvOpImageSampleDrefImplicitLod:
2012 case SpvOpImageSampleDrefExplicitLod:
2013 case SpvOpImageSampleProjDrefImplicitLod:
2014 case SpvOpImageSampleProjDrefExplicitLod:
2015 case SpvOpImageDrefGather:
2016 /* These all have an explicit depth value as their next source */
2017 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_comparator);
2020 case SpvOpImageGather:
2021 /* This has a component as its next source */
2023 vtn_value(b, w[idx++], vtn_value_type_constant)->constant->values[0].u32[0];
2030 /* For OpImageQuerySizeLod, we always have an LOD */
2031 if (opcode == SpvOpImageQuerySizeLod)
2032 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_lod);
2034 /* Now we need to handle some number of optional arguments */
2035 const struct vtn_ssa_value *gather_offsets = NULL;
2037 uint32_t operands = w[idx++];
2039 if (operands & SpvImageOperandsBiasMask) {
2040 vtn_assert(texop == nir_texop_tex);
2041 texop = nir_texop_txb;
2042 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_bias);
2045 if (operands & SpvImageOperandsLodMask) {
2046 vtn_assert(texop == nir_texop_txl || texop == nir_texop_txf ||
2047 texop == nir_texop_txs);
2048 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_lod);
2051 if (operands & SpvImageOperandsGradMask) {
2052 vtn_assert(texop == nir_texop_txl);
2053 texop = nir_texop_txd;
2054 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_ddx);
2055 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_ddy);
2058 if (operands & SpvImageOperandsOffsetMask ||
2059 operands & SpvImageOperandsConstOffsetMask)
2060 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_offset);
2062 if (operands & SpvImageOperandsConstOffsetsMask) {
2063 gather_offsets = vtn_ssa_value(b, w[idx++]);
2064 (*p++) = (nir_tex_src){};
2067 if (operands & SpvImageOperandsSampleMask) {
2068 vtn_assert(texop == nir_texop_txf_ms);
2069 texop = nir_texop_txf_ms;
2070 (*p++) = vtn_tex_src(b, w[idx++], nir_tex_src_ms_index);
2073 /* We should have now consumed exactly all of the arguments */
2074 vtn_assert(idx == count);
2076 nir_tex_instr *instr = nir_tex_instr_create(b->shader, p - srcs);
2079 memcpy(instr->src, srcs, instr->num_srcs * sizeof(*instr->src));
2081 instr->coord_components = coord_components;
2082 instr->sampler_dim = sampler_dim;
2083 instr->is_array = is_array;
2084 instr->is_shadow = is_shadow;
2085 instr->is_new_style_shadow =
2086 is_shadow && glsl_get_components(ret_type->type) == 1;
2087 instr->component = gather_component;
2089 switch (glsl_get_sampler_result_type(image_type)) {
2090 case GLSL_TYPE_FLOAT: instr->dest_type = nir_type_float; break;
2091 case GLSL_TYPE_INT: instr->dest_type = nir_type_int; break;
2092 case GLSL_TYPE_UINT: instr->dest_type = nir_type_uint; break;
2093 case GLSL_TYPE_BOOL: instr->dest_type = nir_type_bool; break;
2095 vtn_fail("Invalid base type for sampler result");
2098 nir_deref_var *sampler = vtn_pointer_to_deref(b, sampled.sampler);
2099 nir_deref_var *texture;
2100 if (sampled.image) {
2101 nir_deref_var *image = vtn_pointer_to_deref(b, sampled.image);
2107 instr->texture = nir_deref_var_clone(texture, instr);
2109 switch (instr->op) {
2115 /* These operations require a sampler */
2116 instr->sampler = nir_deref_var_clone(sampler, instr);
2119 case nir_texop_txf_ms:
2122 case nir_texop_query_levels:
2123 case nir_texop_texture_samples:
2124 case nir_texop_samples_identical:
2126 instr->sampler = NULL;
2128 case nir_texop_txf_ms_mcs:
2129 vtn_fail("unexpected nir_texop_txf_ms_mcs");
2132 nir_ssa_dest_init(&instr->instr, &instr->dest,
2133 nir_tex_instr_dest_size(instr), 32, NULL);
2135 vtn_assert(glsl_get_vector_elements(ret_type->type) ==
2136 nir_tex_instr_dest_size(instr));
2139 nir_instr *instruction;
2140 if (gather_offsets) {
2141 vtn_assert(glsl_get_base_type(gather_offsets->type) == GLSL_TYPE_ARRAY);
2142 vtn_assert(glsl_get_length(gather_offsets->type) == 4);
2143 nir_tex_instr *instrs[4] = {instr, NULL, NULL, NULL};
2145 /* Copy the current instruction 4x */
2146 for (uint32_t i = 1; i < 4; i++) {
2147 instrs[i] = nir_tex_instr_create(b->shader, instr->num_srcs);
2148 instrs[i]->op = instr->op;
2149 instrs[i]->coord_components = instr->coord_components;
2150 instrs[i]->sampler_dim = instr->sampler_dim;
2151 instrs[i]->is_array = instr->is_array;
2152 instrs[i]->is_shadow = instr->is_shadow;
2153 instrs[i]->is_new_style_shadow = instr->is_new_style_shadow;
2154 instrs[i]->component = instr->component;
2155 instrs[i]->dest_type = instr->dest_type;
2156 instrs[i]->texture = nir_deref_var_clone(texture, instrs[i]);
2157 instrs[i]->sampler = NULL;
2159 memcpy(instrs[i]->src, srcs, instr->num_srcs * sizeof(*instr->src));
2161 nir_ssa_dest_init(&instrs[i]->instr, &instrs[i]->dest,
2162 nir_tex_instr_dest_size(instr), 32, NULL);
2165 /* Fill in the last argument with the offset from the passed in offsets
2166 * and insert the instruction into the stream.
2168 for (uint32_t i = 0; i < 4; i++) {
2170 src.src = nir_src_for_ssa(gather_offsets->elems[i]->def);
2171 src.src_type = nir_tex_src_offset;
2172 instrs[i]->src[instrs[i]->num_srcs - 1] = src;
2173 nir_builder_instr_insert(&b->nb, &instrs[i]->instr);
2176 /* Combine the results of the 4 instructions by taking their .w
2179 nir_alu_instr *vec4 = nir_alu_instr_create(b->shader, nir_op_vec4);
2180 nir_ssa_dest_init(&vec4->instr, &vec4->dest.dest, 4, 32, NULL);
2181 vec4->dest.write_mask = 0xf;
2182 for (uint32_t i = 0; i < 4; i++) {
2183 vec4->src[i].src = nir_src_for_ssa(&instrs[i]->dest.ssa);
2184 vec4->src[i].swizzle[0] = 3;
2186 def = &vec4->dest.dest.ssa;
2187 instruction = &vec4->instr;
2189 def = &instr->dest.ssa;
2190 instruction = &instr->instr;
2193 val->ssa = vtn_create_ssa_value(b, ret_type->type);
2194 val->ssa->def = def;
2196 nir_builder_instr_insert(&b->nb, instruction);
2200 fill_common_atomic_sources(struct vtn_builder *b, SpvOp opcode,
2201 const uint32_t *w, nir_src *src)
2204 case SpvOpAtomicIIncrement:
2205 src[0] = nir_src_for_ssa(nir_imm_int(&b->nb, 1));
2208 case SpvOpAtomicIDecrement:
2209 src[0] = nir_src_for_ssa(nir_imm_int(&b->nb, -1));
2212 case SpvOpAtomicISub:
2214 nir_src_for_ssa(nir_ineg(&b->nb, vtn_ssa_value(b, w[6])->def));
2217 case SpvOpAtomicCompareExchange:
2218 src[0] = nir_src_for_ssa(vtn_ssa_value(b, w[8])->def);
2219 src[1] = nir_src_for_ssa(vtn_ssa_value(b, w[7])->def);
2222 case SpvOpAtomicExchange:
2223 case SpvOpAtomicIAdd:
2224 case SpvOpAtomicSMin:
2225 case SpvOpAtomicUMin:
2226 case SpvOpAtomicSMax:
2227 case SpvOpAtomicUMax:
2228 case SpvOpAtomicAnd:
2230 case SpvOpAtomicXor:
2231 src[0] = nir_src_for_ssa(vtn_ssa_value(b, w[6])->def);
2235 vtn_fail("Invalid SPIR-V atomic");
2239 static nir_ssa_def *
2240 get_image_coord(struct vtn_builder *b, uint32_t value)
2242 struct vtn_ssa_value *coord = vtn_ssa_value(b, value);
2244 /* The image_load_store intrinsics assume a 4-dim coordinate */
2245 unsigned dim = glsl_get_vector_elements(coord->type);
2246 unsigned swizzle[4];
2247 for (unsigned i = 0; i < 4; i++)
2248 swizzle[i] = MIN2(i, dim - 1);
2250 return nir_swizzle(&b->nb, coord->def, swizzle, 4, false);
2254 vtn_handle_image(struct vtn_builder *b, SpvOp opcode,
2255 const uint32_t *w, unsigned count)
2257 /* Just get this one out of the way */
2258 if (opcode == SpvOpImageTexelPointer) {
2259 struct vtn_value *val =
2260 vtn_push_value(b, w[2], vtn_value_type_image_pointer);
2261 val->image = ralloc(b, struct vtn_image_pointer);
2263 val->image->image = vtn_value(b, w[3], vtn_value_type_pointer)->pointer;
2264 val->image->coord = get_image_coord(b, w[4]);
2265 val->image->sample = vtn_ssa_value(b, w[5])->def;
2269 struct vtn_image_pointer image;
2272 case SpvOpAtomicExchange:
2273 case SpvOpAtomicCompareExchange:
2274 case SpvOpAtomicCompareExchangeWeak:
2275 case SpvOpAtomicIIncrement:
2276 case SpvOpAtomicIDecrement:
2277 case SpvOpAtomicIAdd:
2278 case SpvOpAtomicISub:
2279 case SpvOpAtomicLoad:
2280 case SpvOpAtomicSMin:
2281 case SpvOpAtomicUMin:
2282 case SpvOpAtomicSMax:
2283 case SpvOpAtomicUMax:
2284 case SpvOpAtomicAnd:
2286 case SpvOpAtomicXor:
2287 image = *vtn_value(b, w[3], vtn_value_type_image_pointer)->image;
2290 case SpvOpAtomicStore:
2291 image = *vtn_value(b, w[1], vtn_value_type_image_pointer)->image;
2294 case SpvOpImageQuerySize:
2295 image.image = vtn_value(b, w[3], vtn_value_type_pointer)->pointer;
2297 image.sample = NULL;
2300 case SpvOpImageRead:
2301 image.image = vtn_value(b, w[3], vtn_value_type_pointer)->pointer;
2302 image.coord = get_image_coord(b, w[4]);
2304 if (count > 5 && (w[5] & SpvImageOperandsSampleMask)) {
2305 vtn_assert(w[5] == SpvImageOperandsSampleMask);
2306 image.sample = vtn_ssa_value(b, w[6])->def;
2308 image.sample = nir_ssa_undef(&b->nb, 1, 32);
2312 case SpvOpImageWrite:
2313 image.image = vtn_value(b, w[1], vtn_value_type_pointer)->pointer;
2314 image.coord = get_image_coord(b, w[2]);
2318 if (count > 4 && (w[4] & SpvImageOperandsSampleMask)) {
2319 vtn_assert(w[4] == SpvImageOperandsSampleMask);
2320 image.sample = vtn_ssa_value(b, w[5])->def;
2322 image.sample = nir_ssa_undef(&b->nb, 1, 32);
2327 vtn_fail("Invalid image opcode");
2330 nir_intrinsic_op op;
2332 #define OP(S, N) case SpvOp##S: op = nir_intrinsic_image_##N; break;
2333 OP(ImageQuerySize, size)
2335 OP(ImageWrite, store)
2336 OP(AtomicLoad, load)
2337 OP(AtomicStore, store)
2338 OP(AtomicExchange, atomic_exchange)
2339 OP(AtomicCompareExchange, atomic_comp_swap)
2340 OP(AtomicIIncrement, atomic_add)
2341 OP(AtomicIDecrement, atomic_add)
2342 OP(AtomicIAdd, atomic_add)
2343 OP(AtomicISub, atomic_add)
2344 OP(AtomicSMin, atomic_min)
2345 OP(AtomicUMin, atomic_min)
2346 OP(AtomicSMax, atomic_max)
2347 OP(AtomicUMax, atomic_max)
2348 OP(AtomicAnd, atomic_and)
2349 OP(AtomicOr, atomic_or)
2350 OP(AtomicXor, atomic_xor)
2353 vtn_fail("Invalid image opcode");
2356 nir_intrinsic_instr *intrin = nir_intrinsic_instr_create(b->shader, op);
2358 nir_deref_var *image_deref = vtn_pointer_to_deref(b, image.image);
2359 intrin->variables[0] = nir_deref_var_clone(image_deref, intrin);
2361 /* ImageQuerySize doesn't take any extra parameters */
2362 if (opcode != SpvOpImageQuerySize) {
2363 /* The image coordinate is always 4 components but we may not have that
2364 * many. Swizzle to compensate.
2367 for (unsigned i = 0; i < 4; i++)
2368 swiz[i] = i < image.coord->num_components ? i : 0;
2369 intrin->src[0] = nir_src_for_ssa(nir_swizzle(&b->nb, image.coord,
2371 intrin->src[1] = nir_src_for_ssa(image.sample);
2375 case SpvOpAtomicLoad:
2376 case SpvOpImageQuerySize:
2377 case SpvOpImageRead:
2379 case SpvOpAtomicStore:
2380 intrin->src[2] = nir_src_for_ssa(vtn_ssa_value(b, w[4])->def);
2382 case SpvOpImageWrite:
2383 intrin->src[2] = nir_src_for_ssa(vtn_ssa_value(b, w[3])->def);
2386 case SpvOpAtomicCompareExchange:
2387 case SpvOpAtomicIIncrement:
2388 case SpvOpAtomicIDecrement:
2389 case SpvOpAtomicExchange:
2390 case SpvOpAtomicIAdd:
2391 case SpvOpAtomicISub:
2392 case SpvOpAtomicSMin:
2393 case SpvOpAtomicUMin:
2394 case SpvOpAtomicSMax:
2395 case SpvOpAtomicUMax:
2396 case SpvOpAtomicAnd:
2398 case SpvOpAtomicXor:
2399 fill_common_atomic_sources(b, opcode, w, &intrin->src[2]);
2403 vtn_fail("Invalid image opcode");
2406 if (opcode != SpvOpImageWrite) {
2407 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
2408 struct vtn_type *type = vtn_value(b, w[1], vtn_value_type_type)->type;
2410 unsigned dest_components =
2411 nir_intrinsic_infos[intrin->intrinsic].dest_components;
2412 if (intrin->intrinsic == nir_intrinsic_image_size) {
2413 dest_components = intrin->num_components =
2414 glsl_get_vector_elements(type->type);
2417 nir_ssa_dest_init(&intrin->instr, &intrin->dest,
2418 dest_components, 32, NULL);
2420 nir_builder_instr_insert(&b->nb, &intrin->instr);
2422 val->ssa = vtn_create_ssa_value(b, type->type);
2423 val->ssa->def = &intrin->dest.ssa;
2425 nir_builder_instr_insert(&b->nb, &intrin->instr);
2429 static nir_intrinsic_op
2430 get_ssbo_nir_atomic_op(struct vtn_builder *b, SpvOp opcode)
2433 case SpvOpAtomicLoad: return nir_intrinsic_load_ssbo;
2434 case SpvOpAtomicStore: return nir_intrinsic_store_ssbo;
2435 #define OP(S, N) case SpvOp##S: return nir_intrinsic_ssbo_##N;
2436 OP(AtomicExchange, atomic_exchange)
2437 OP(AtomicCompareExchange, atomic_comp_swap)
2438 OP(AtomicIIncrement, atomic_add)
2439 OP(AtomicIDecrement, atomic_add)
2440 OP(AtomicIAdd, atomic_add)
2441 OP(AtomicISub, atomic_add)
2442 OP(AtomicSMin, atomic_imin)
2443 OP(AtomicUMin, atomic_umin)
2444 OP(AtomicSMax, atomic_imax)
2445 OP(AtomicUMax, atomic_umax)
2446 OP(AtomicAnd, atomic_and)
2447 OP(AtomicOr, atomic_or)
2448 OP(AtomicXor, atomic_xor)
2451 vtn_fail("Invalid SSBO atomic");
2455 static nir_intrinsic_op
2456 get_shared_nir_atomic_op(struct vtn_builder *b, SpvOp opcode)
2459 case SpvOpAtomicLoad: return nir_intrinsic_load_shared;
2460 case SpvOpAtomicStore: return nir_intrinsic_store_shared;
2461 #define OP(S, N) case SpvOp##S: return nir_intrinsic_shared_##N;
2462 OP(AtomicExchange, atomic_exchange)
2463 OP(AtomicCompareExchange, atomic_comp_swap)
2464 OP(AtomicIIncrement, atomic_add)
2465 OP(AtomicIDecrement, atomic_add)
2466 OP(AtomicIAdd, atomic_add)
2467 OP(AtomicISub, atomic_add)
2468 OP(AtomicSMin, atomic_imin)
2469 OP(AtomicUMin, atomic_umin)
2470 OP(AtomicSMax, atomic_imax)
2471 OP(AtomicUMax, atomic_umax)
2472 OP(AtomicAnd, atomic_and)
2473 OP(AtomicOr, atomic_or)
2474 OP(AtomicXor, atomic_xor)
2477 vtn_fail("Invalid shared atomic");
2481 static nir_intrinsic_op
2482 get_var_nir_atomic_op(struct vtn_builder *b, SpvOp opcode)
2485 case SpvOpAtomicLoad: return nir_intrinsic_load_var;
2486 case SpvOpAtomicStore: return nir_intrinsic_store_var;
2487 #define OP(S, N) case SpvOp##S: return nir_intrinsic_var_##N;
2488 OP(AtomicExchange, atomic_exchange)
2489 OP(AtomicCompareExchange, atomic_comp_swap)
2490 OP(AtomicIIncrement, atomic_add)
2491 OP(AtomicIDecrement, atomic_add)
2492 OP(AtomicIAdd, atomic_add)
2493 OP(AtomicISub, atomic_add)
2494 OP(AtomicSMin, atomic_imin)
2495 OP(AtomicUMin, atomic_umin)
2496 OP(AtomicSMax, atomic_imax)
2497 OP(AtomicUMax, atomic_umax)
2498 OP(AtomicAnd, atomic_and)
2499 OP(AtomicOr, atomic_or)
2500 OP(AtomicXor, atomic_xor)
2503 vtn_fail("Invalid shared atomic");
2508 vtn_handle_ssbo_or_shared_atomic(struct vtn_builder *b, SpvOp opcode,
2509 const uint32_t *w, unsigned count)
2511 struct vtn_pointer *ptr;
2512 nir_intrinsic_instr *atomic;
2515 case SpvOpAtomicLoad:
2516 case SpvOpAtomicExchange:
2517 case SpvOpAtomicCompareExchange:
2518 case SpvOpAtomicCompareExchangeWeak:
2519 case SpvOpAtomicIIncrement:
2520 case SpvOpAtomicIDecrement:
2521 case SpvOpAtomicIAdd:
2522 case SpvOpAtomicISub:
2523 case SpvOpAtomicSMin:
2524 case SpvOpAtomicUMin:
2525 case SpvOpAtomicSMax:
2526 case SpvOpAtomicUMax:
2527 case SpvOpAtomicAnd:
2529 case SpvOpAtomicXor:
2530 ptr = vtn_value(b, w[3], vtn_value_type_pointer)->pointer;
2533 case SpvOpAtomicStore:
2534 ptr = vtn_value(b, w[1], vtn_value_type_pointer)->pointer;
2538 vtn_fail("Invalid SPIR-V atomic");
2542 SpvScope scope = w[4];
2543 SpvMemorySemanticsMask semantics = w[5];
2546 if (ptr->mode == vtn_variable_mode_workgroup &&
2547 !b->options->lower_workgroup_access_to_offsets) {
2548 nir_deref_var *deref = vtn_pointer_to_deref(b, ptr);
2549 const struct glsl_type *deref_type = nir_deref_tail(&deref->deref)->type;
2550 nir_intrinsic_op op = get_var_nir_atomic_op(b, opcode);
2551 atomic = nir_intrinsic_instr_create(b->nb.shader, op);
2552 atomic->variables[0] = nir_deref_var_clone(deref, atomic);
2555 case SpvOpAtomicLoad:
2556 atomic->num_components = glsl_get_vector_elements(deref_type);
2559 case SpvOpAtomicStore:
2560 atomic->num_components = glsl_get_vector_elements(deref_type);
2561 nir_intrinsic_set_write_mask(atomic, (1 << atomic->num_components) - 1);
2562 atomic->src[0] = nir_src_for_ssa(vtn_ssa_value(b, w[4])->def);
2565 case SpvOpAtomicExchange:
2566 case SpvOpAtomicCompareExchange:
2567 case SpvOpAtomicCompareExchangeWeak:
2568 case SpvOpAtomicIIncrement:
2569 case SpvOpAtomicIDecrement:
2570 case SpvOpAtomicIAdd:
2571 case SpvOpAtomicISub:
2572 case SpvOpAtomicSMin:
2573 case SpvOpAtomicUMin:
2574 case SpvOpAtomicSMax:
2575 case SpvOpAtomicUMax:
2576 case SpvOpAtomicAnd:
2578 case SpvOpAtomicXor:
2579 fill_common_atomic_sources(b, opcode, w, &atomic->src[0]);
2583 vtn_fail("Invalid SPIR-V atomic");
2587 nir_ssa_def *offset, *index;
2588 offset = vtn_pointer_to_offset(b, ptr, &index, NULL);
2590 nir_intrinsic_op op;
2591 if (ptr->mode == vtn_variable_mode_ssbo) {
2592 op = get_ssbo_nir_atomic_op(b, opcode);
2594 vtn_assert(ptr->mode == vtn_variable_mode_workgroup &&
2595 b->options->lower_workgroup_access_to_offsets);
2596 op = get_shared_nir_atomic_op(b, opcode);
2599 atomic = nir_intrinsic_instr_create(b->nb.shader, op);
2603 case SpvOpAtomicLoad:
2604 atomic->num_components = glsl_get_vector_elements(ptr->type->type);
2605 if (ptr->mode == vtn_variable_mode_ssbo)
2606 atomic->src[src++] = nir_src_for_ssa(index);
2607 atomic->src[src++] = nir_src_for_ssa(offset);
2610 case SpvOpAtomicStore:
2611 atomic->num_components = glsl_get_vector_elements(ptr->type->type);
2612 nir_intrinsic_set_write_mask(atomic, (1 << atomic->num_components) - 1);
2613 atomic->src[src++] = nir_src_for_ssa(vtn_ssa_value(b, w[4])->def);
2614 if (ptr->mode == vtn_variable_mode_ssbo)
2615 atomic->src[src++] = nir_src_for_ssa(index);
2616 atomic->src[src++] = nir_src_for_ssa(offset);
2619 case SpvOpAtomicExchange:
2620 case SpvOpAtomicCompareExchange:
2621 case SpvOpAtomicCompareExchangeWeak:
2622 case SpvOpAtomicIIncrement:
2623 case SpvOpAtomicIDecrement:
2624 case SpvOpAtomicIAdd:
2625 case SpvOpAtomicISub:
2626 case SpvOpAtomicSMin:
2627 case SpvOpAtomicUMin:
2628 case SpvOpAtomicSMax:
2629 case SpvOpAtomicUMax:
2630 case SpvOpAtomicAnd:
2632 case SpvOpAtomicXor:
2633 if (ptr->mode == vtn_variable_mode_ssbo)
2634 atomic->src[src++] = nir_src_for_ssa(index);
2635 atomic->src[src++] = nir_src_for_ssa(offset);
2636 fill_common_atomic_sources(b, opcode, w, &atomic->src[src]);
2640 vtn_fail("Invalid SPIR-V atomic");
2644 if (opcode != SpvOpAtomicStore) {
2645 struct vtn_type *type = vtn_value(b, w[1], vtn_value_type_type)->type;
2647 nir_ssa_dest_init(&atomic->instr, &atomic->dest,
2648 glsl_get_vector_elements(type->type),
2649 glsl_get_bit_size(type->type), NULL);
2651 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
2652 val->ssa = rzalloc(b, struct vtn_ssa_value);
2653 val->ssa->def = &atomic->dest.ssa;
2654 val->ssa->type = type->type;
2657 nir_builder_instr_insert(&b->nb, &atomic->instr);
2660 static nir_alu_instr *
2661 create_vec(struct vtn_builder *b, unsigned num_components, unsigned bit_size)
2664 switch (num_components) {
2665 case 1: op = nir_op_fmov; break;
2666 case 2: op = nir_op_vec2; break;
2667 case 3: op = nir_op_vec3; break;
2668 case 4: op = nir_op_vec4; break;
2669 default: vtn_fail("bad vector size");
2672 nir_alu_instr *vec = nir_alu_instr_create(b->shader, op);
2673 nir_ssa_dest_init(&vec->instr, &vec->dest.dest, num_components,
2675 vec->dest.write_mask = (1 << num_components) - 1;
2680 struct vtn_ssa_value *
2681 vtn_ssa_transpose(struct vtn_builder *b, struct vtn_ssa_value *src)
2683 if (src->transposed)
2684 return src->transposed;
2686 struct vtn_ssa_value *dest =
2687 vtn_create_ssa_value(b, glsl_transposed_type(src->type));
2689 for (unsigned i = 0; i < glsl_get_matrix_columns(dest->type); i++) {
2690 nir_alu_instr *vec = create_vec(b, glsl_get_matrix_columns(src->type),
2691 glsl_get_bit_size(src->type));
2692 if (glsl_type_is_vector_or_scalar(src->type)) {
2693 vec->src[0].src = nir_src_for_ssa(src->def);
2694 vec->src[0].swizzle[0] = i;
2696 for (unsigned j = 0; j < glsl_get_matrix_columns(src->type); j++) {
2697 vec->src[j].src = nir_src_for_ssa(src->elems[j]->def);
2698 vec->src[j].swizzle[0] = i;
2701 nir_builder_instr_insert(&b->nb, &vec->instr);
2702 dest->elems[i]->def = &vec->dest.dest.ssa;
2705 dest->transposed = src;
2711 vtn_vector_extract(struct vtn_builder *b, nir_ssa_def *src, unsigned index)
2713 unsigned swiz[4] = { index };
2714 return nir_swizzle(&b->nb, src, swiz, 1, true);
2718 vtn_vector_insert(struct vtn_builder *b, nir_ssa_def *src, nir_ssa_def *insert,
2721 nir_alu_instr *vec = create_vec(b, src->num_components,
2724 for (unsigned i = 0; i < src->num_components; i++) {
2726 vec->src[i].src = nir_src_for_ssa(insert);
2728 vec->src[i].src = nir_src_for_ssa(src);
2729 vec->src[i].swizzle[0] = i;
2733 nir_builder_instr_insert(&b->nb, &vec->instr);
2735 return &vec->dest.dest.ssa;
2739 vtn_vector_extract_dynamic(struct vtn_builder *b, nir_ssa_def *src,
2742 nir_ssa_def *dest = vtn_vector_extract(b, src, 0);
2743 for (unsigned i = 1; i < src->num_components; i++)
2744 dest = nir_bcsel(&b->nb, nir_ieq(&b->nb, index, nir_imm_int(&b->nb, i)),
2745 vtn_vector_extract(b, src, i), dest);
2751 vtn_vector_insert_dynamic(struct vtn_builder *b, nir_ssa_def *src,
2752 nir_ssa_def *insert, nir_ssa_def *index)
2754 nir_ssa_def *dest = vtn_vector_insert(b, src, insert, 0);
2755 for (unsigned i = 1; i < src->num_components; i++)
2756 dest = nir_bcsel(&b->nb, nir_ieq(&b->nb, index, nir_imm_int(&b->nb, i)),
2757 vtn_vector_insert(b, src, insert, i), dest);
2762 static nir_ssa_def *
2763 vtn_vector_shuffle(struct vtn_builder *b, unsigned num_components,
2764 nir_ssa_def *src0, nir_ssa_def *src1,
2765 const uint32_t *indices)
2767 nir_alu_instr *vec = create_vec(b, num_components, src0->bit_size);
2769 for (unsigned i = 0; i < num_components; i++) {
2770 uint32_t index = indices[i];
2771 if (index == 0xffffffff) {
2773 nir_src_for_ssa(nir_ssa_undef(&b->nb, 1, src0->bit_size));
2774 } else if (index < src0->num_components) {
2775 vec->src[i].src = nir_src_for_ssa(src0);
2776 vec->src[i].swizzle[0] = index;
2778 vec->src[i].src = nir_src_for_ssa(src1);
2779 vec->src[i].swizzle[0] = index - src0->num_components;
2783 nir_builder_instr_insert(&b->nb, &vec->instr);
2785 return &vec->dest.dest.ssa;
2789 * Concatentates a number of vectors/scalars together to produce a vector
2791 static nir_ssa_def *
2792 vtn_vector_construct(struct vtn_builder *b, unsigned num_components,
2793 unsigned num_srcs, nir_ssa_def **srcs)
2795 nir_alu_instr *vec = create_vec(b, num_components, srcs[0]->bit_size);
2797 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
2799 * "When constructing a vector, there must be at least two Constituent
2802 vtn_assert(num_srcs >= 2);
2804 unsigned dest_idx = 0;
2805 for (unsigned i = 0; i < num_srcs; i++) {
2806 nir_ssa_def *src = srcs[i];
2807 vtn_assert(dest_idx + src->num_components <= num_components);
2808 for (unsigned j = 0; j < src->num_components; j++) {
2809 vec->src[dest_idx].src = nir_src_for_ssa(src);
2810 vec->src[dest_idx].swizzle[0] = j;
2815 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
2817 * "When constructing a vector, the total number of components in all
2818 * the operands must equal the number of components in Result Type."
2820 vtn_assert(dest_idx == num_components);
2822 nir_builder_instr_insert(&b->nb, &vec->instr);
2824 return &vec->dest.dest.ssa;
2827 static struct vtn_ssa_value *
2828 vtn_composite_copy(void *mem_ctx, struct vtn_ssa_value *src)
2830 struct vtn_ssa_value *dest = rzalloc(mem_ctx, struct vtn_ssa_value);
2831 dest->type = src->type;
2833 if (glsl_type_is_vector_or_scalar(src->type)) {
2834 dest->def = src->def;
2836 unsigned elems = glsl_get_length(src->type);
2838 dest->elems = ralloc_array(mem_ctx, struct vtn_ssa_value *, elems);
2839 for (unsigned i = 0; i < elems; i++)
2840 dest->elems[i] = vtn_composite_copy(mem_ctx, src->elems[i]);
2846 static struct vtn_ssa_value *
2847 vtn_composite_insert(struct vtn_builder *b, struct vtn_ssa_value *src,
2848 struct vtn_ssa_value *insert, const uint32_t *indices,
2849 unsigned num_indices)
2851 struct vtn_ssa_value *dest = vtn_composite_copy(b, src);
2853 struct vtn_ssa_value *cur = dest;
2855 for (i = 0; i < num_indices - 1; i++) {
2856 cur = cur->elems[indices[i]];
2859 if (glsl_type_is_vector_or_scalar(cur->type)) {
2860 /* According to the SPIR-V spec, OpCompositeInsert may work down to
2861 * the component granularity. In that case, the last index will be
2862 * the index to insert the scalar into the vector.
2865 cur->def = vtn_vector_insert(b, cur->def, insert->def, indices[i]);
2867 cur->elems[indices[i]] = insert;
2873 static struct vtn_ssa_value *
2874 vtn_composite_extract(struct vtn_builder *b, struct vtn_ssa_value *src,
2875 const uint32_t *indices, unsigned num_indices)
2877 struct vtn_ssa_value *cur = src;
2878 for (unsigned i = 0; i < num_indices; i++) {
2879 if (glsl_type_is_vector_or_scalar(cur->type)) {
2880 vtn_assert(i == num_indices - 1);
2881 /* According to the SPIR-V spec, OpCompositeExtract may work down to
2882 * the component granularity. The last index will be the index of the
2883 * vector to extract.
2886 struct vtn_ssa_value *ret = rzalloc(b, struct vtn_ssa_value);
2887 ret->type = glsl_scalar_type(glsl_get_base_type(cur->type));
2888 ret->def = vtn_vector_extract(b, cur->def, indices[i]);
2891 cur = cur->elems[indices[i]];
2899 vtn_handle_composite(struct vtn_builder *b, SpvOp opcode,
2900 const uint32_t *w, unsigned count)
2902 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_ssa);
2903 const struct glsl_type *type =
2904 vtn_value(b, w[1], vtn_value_type_type)->type->type;
2905 val->ssa = vtn_create_ssa_value(b, type);
2908 case SpvOpVectorExtractDynamic:
2909 val->ssa->def = vtn_vector_extract_dynamic(b, vtn_ssa_value(b, w[3])->def,
2910 vtn_ssa_value(b, w[4])->def);
2913 case SpvOpVectorInsertDynamic:
2914 val->ssa->def = vtn_vector_insert_dynamic(b, vtn_ssa_value(b, w[3])->def,
2915 vtn_ssa_value(b, w[4])->def,
2916 vtn_ssa_value(b, w[5])->def);
2919 case SpvOpVectorShuffle:
2920 val->ssa->def = vtn_vector_shuffle(b, glsl_get_vector_elements(type),
2921 vtn_ssa_value(b, w[3])->def,
2922 vtn_ssa_value(b, w[4])->def,
2926 case SpvOpCompositeConstruct: {
2927 unsigned elems = count - 3;
2928 if (glsl_type_is_vector_or_scalar(type)) {
2929 nir_ssa_def *srcs[4];
2930 for (unsigned i = 0; i < elems; i++)
2931 srcs[i] = vtn_ssa_value(b, w[3 + i])->def;
2933 vtn_vector_construct(b, glsl_get_vector_elements(type),
2936 val->ssa->elems = ralloc_array(b, struct vtn_ssa_value *, elems);
2937 for (unsigned i = 0; i < elems; i++)
2938 val->ssa->elems[i] = vtn_ssa_value(b, w[3 + i]);
2942 case SpvOpCompositeExtract:
2943 val->ssa = vtn_composite_extract(b, vtn_ssa_value(b, w[3]),
2947 case SpvOpCompositeInsert:
2948 val->ssa = vtn_composite_insert(b, vtn_ssa_value(b, w[4]),
2949 vtn_ssa_value(b, w[3]),
2953 case SpvOpCopyObject:
2954 val->ssa = vtn_composite_copy(b, vtn_ssa_value(b, w[3]));
2958 vtn_fail("unknown composite operation");
2963 vtn_emit_barrier(struct vtn_builder *b, nir_intrinsic_op op)
2965 nir_intrinsic_instr *intrin = nir_intrinsic_instr_create(b->shader, op);
2966 nir_builder_instr_insert(&b->nb, &intrin->instr);
2970 vtn_emit_memory_barrier(struct vtn_builder *b, SpvScope scope,
2971 SpvMemorySemanticsMask semantics)
2973 static const SpvMemorySemanticsMask all_memory_semantics =
2974 SpvMemorySemanticsUniformMemoryMask |
2975 SpvMemorySemanticsWorkgroupMemoryMask |
2976 SpvMemorySemanticsAtomicCounterMemoryMask |
2977 SpvMemorySemanticsImageMemoryMask;
2979 /* If we're not actually doing a memory barrier, bail */
2980 if (!(semantics & all_memory_semantics))
2983 /* GL and Vulkan don't have these */
2984 vtn_assert(scope != SpvScopeCrossDevice);
2986 if (scope == SpvScopeSubgroup)
2987 return; /* Nothing to do here */
2989 if (scope == SpvScopeWorkgroup) {
2990 vtn_emit_barrier(b, nir_intrinsic_group_memory_barrier);
2994 /* There's only two scopes thing left */
2995 vtn_assert(scope == SpvScopeInvocation || scope == SpvScopeDevice);
2997 if ((semantics & all_memory_semantics) == all_memory_semantics) {
2998 vtn_emit_barrier(b, nir_intrinsic_memory_barrier);
3002 /* Issue a bunch of more specific barriers */
3003 uint32_t bits = semantics;
3005 SpvMemorySemanticsMask semantic = 1 << u_bit_scan(&bits);
3007 case SpvMemorySemanticsUniformMemoryMask:
3008 vtn_emit_barrier(b, nir_intrinsic_memory_barrier_buffer);
3010 case SpvMemorySemanticsWorkgroupMemoryMask:
3011 vtn_emit_barrier(b, nir_intrinsic_memory_barrier_shared);
3013 case SpvMemorySemanticsAtomicCounterMemoryMask:
3014 vtn_emit_barrier(b, nir_intrinsic_memory_barrier_atomic_counter);
3016 case SpvMemorySemanticsImageMemoryMask:
3017 vtn_emit_barrier(b, nir_intrinsic_memory_barrier_image);
3026 vtn_handle_barrier(struct vtn_builder *b, SpvOp opcode,
3027 const uint32_t *w, unsigned count)
3030 case SpvOpEmitVertex:
3031 case SpvOpEmitStreamVertex:
3032 case SpvOpEndPrimitive:
3033 case SpvOpEndStreamPrimitive: {
3034 nir_intrinsic_op intrinsic_op;
3036 case SpvOpEmitVertex:
3037 case SpvOpEmitStreamVertex:
3038 intrinsic_op = nir_intrinsic_emit_vertex;
3040 case SpvOpEndPrimitive:
3041 case SpvOpEndStreamPrimitive:
3042 intrinsic_op = nir_intrinsic_end_primitive;
3045 unreachable("Invalid opcode");
3048 nir_intrinsic_instr *intrin =
3049 nir_intrinsic_instr_create(b->shader, intrinsic_op);
3052 case SpvOpEmitStreamVertex:
3053 case SpvOpEndStreamPrimitive:
3054 nir_intrinsic_set_stream_id(intrin, w[1]);
3060 nir_builder_instr_insert(&b->nb, &intrin->instr);
3064 case SpvOpMemoryBarrier: {
3065 SpvScope scope = vtn_constant_value(b, w[1])->values[0].u32[0];
3066 SpvMemorySemanticsMask semantics =
3067 vtn_constant_value(b, w[2])->values[0].u32[0];
3068 vtn_emit_memory_barrier(b, scope, semantics);
3072 case SpvOpControlBarrier: {
3073 SpvScope execution_scope =
3074 vtn_constant_value(b, w[1])->values[0].u32[0];
3075 if (execution_scope == SpvScopeWorkgroup)
3076 vtn_emit_barrier(b, nir_intrinsic_barrier);
3078 SpvScope memory_scope =
3079 vtn_constant_value(b, w[2])->values[0].u32[0];
3080 SpvMemorySemanticsMask memory_semantics =
3081 vtn_constant_value(b, w[3])->values[0].u32[0];
3082 vtn_emit_memory_barrier(b, memory_scope, memory_semantics);
3087 unreachable("unknown barrier instruction");
3092 gl_primitive_from_spv_execution_mode(struct vtn_builder *b,
3093 SpvExecutionMode mode)
3096 case SpvExecutionModeInputPoints:
3097 case SpvExecutionModeOutputPoints:
3098 return 0; /* GL_POINTS */
3099 case SpvExecutionModeInputLines:
3100 return 1; /* GL_LINES */
3101 case SpvExecutionModeInputLinesAdjacency:
3102 return 0x000A; /* GL_LINE_STRIP_ADJACENCY_ARB */
3103 case SpvExecutionModeTriangles:
3104 return 4; /* GL_TRIANGLES */
3105 case SpvExecutionModeInputTrianglesAdjacency:
3106 return 0x000C; /* GL_TRIANGLES_ADJACENCY_ARB */
3107 case SpvExecutionModeQuads:
3108 return 7; /* GL_QUADS */
3109 case SpvExecutionModeIsolines:
3110 return 0x8E7A; /* GL_ISOLINES */
3111 case SpvExecutionModeOutputLineStrip:
3112 return 3; /* GL_LINE_STRIP */
3113 case SpvExecutionModeOutputTriangleStrip:
3114 return 5; /* GL_TRIANGLE_STRIP */
3116 vtn_fail("Invalid primitive type");
3121 vertices_in_from_spv_execution_mode(struct vtn_builder *b,
3122 SpvExecutionMode mode)
3125 case SpvExecutionModeInputPoints:
3127 case SpvExecutionModeInputLines:
3129 case SpvExecutionModeInputLinesAdjacency:
3131 case SpvExecutionModeTriangles:
3133 case SpvExecutionModeInputTrianglesAdjacency:
3136 vtn_fail("Invalid GS input mode");
3140 static gl_shader_stage
3141 stage_for_execution_model(struct vtn_builder *b, SpvExecutionModel model)
3144 case SpvExecutionModelVertex:
3145 return MESA_SHADER_VERTEX;
3146 case SpvExecutionModelTessellationControl:
3147 return MESA_SHADER_TESS_CTRL;
3148 case SpvExecutionModelTessellationEvaluation:
3149 return MESA_SHADER_TESS_EVAL;
3150 case SpvExecutionModelGeometry:
3151 return MESA_SHADER_GEOMETRY;
3152 case SpvExecutionModelFragment:
3153 return MESA_SHADER_FRAGMENT;
3154 case SpvExecutionModelGLCompute:
3155 return MESA_SHADER_COMPUTE;
3157 vtn_fail("Unsupported execution model");
3161 #define spv_check_supported(name, cap) do { \
3162 if (!(b->options && b->options->caps.name)) \
3163 vtn_warn("Unsupported SPIR-V capability: %s", \
3164 spirv_capability_to_string(cap)); \
3168 vtn_handle_preamble_instruction(struct vtn_builder *b, SpvOp opcode,
3169 const uint32_t *w, unsigned count)
3176 case SpvSourceLanguageUnknown: lang = "unknown"; break;
3177 case SpvSourceLanguageESSL: lang = "ESSL"; break;
3178 case SpvSourceLanguageGLSL: lang = "GLSL"; break;
3179 case SpvSourceLanguageOpenCL_C: lang = "OpenCL C"; break;
3180 case SpvSourceLanguageOpenCL_CPP: lang = "OpenCL C++"; break;
3181 case SpvSourceLanguageHLSL: lang = "HLSL"; break;
3184 uint32_t version = w[2];
3187 (count > 3) ? vtn_value(b, w[3], vtn_value_type_string)->str : "";
3189 vtn_info("Parsing SPIR-V from %s %u source file %s", lang, version, file);
3193 case SpvOpSourceExtension:
3194 case SpvOpSourceContinued:
3195 case SpvOpExtension:
3196 case SpvOpModuleProcessed:
3197 /* Unhandled, but these are for debug so that's ok. */
3200 case SpvOpCapability: {
3201 SpvCapability cap = w[1];
3203 case SpvCapabilityMatrix:
3204 case SpvCapabilityShader:
3205 case SpvCapabilityGeometry:
3206 case SpvCapabilityGeometryPointSize:
3207 case SpvCapabilityUniformBufferArrayDynamicIndexing:
3208 case SpvCapabilitySampledImageArrayDynamicIndexing:
3209 case SpvCapabilityStorageBufferArrayDynamicIndexing:
3210 case SpvCapabilityStorageImageArrayDynamicIndexing:
3211 case SpvCapabilityImageRect:
3212 case SpvCapabilitySampledRect:
3213 case SpvCapabilitySampled1D:
3214 case SpvCapabilityImage1D:
3215 case SpvCapabilitySampledCubeArray:
3216 case SpvCapabilityImageCubeArray:
3217 case SpvCapabilitySampledBuffer:
3218 case SpvCapabilityImageBuffer:
3219 case SpvCapabilityImageQuery:
3220 case SpvCapabilityDerivativeControl:
3221 case SpvCapabilityInterpolationFunction:
3222 case SpvCapabilityMultiViewport:
3223 case SpvCapabilitySampleRateShading:
3224 case SpvCapabilityClipDistance:
3225 case SpvCapabilityCullDistance:
3226 case SpvCapabilityInputAttachment:
3227 case SpvCapabilityImageGatherExtended:
3228 case SpvCapabilityStorageImageExtendedFormats:
3231 case SpvCapabilityGeometryStreams:
3232 case SpvCapabilityLinkage:
3233 case SpvCapabilityVector16:
3234 case SpvCapabilityFloat16Buffer:
3235 case SpvCapabilityFloat16:
3236 case SpvCapabilityInt64Atomics:
3237 case SpvCapabilityAtomicStorage:
3238 case SpvCapabilityInt16:
3239 case SpvCapabilityStorageImageMultisample:
3240 case SpvCapabilityInt8:
3241 case SpvCapabilitySparseResidency:
3242 case SpvCapabilityMinLod:
3243 case SpvCapabilityTransformFeedback:
3244 vtn_warn("Unsupported SPIR-V capability: %s",
3245 spirv_capability_to_string(cap));
3248 case SpvCapabilityFloat64:
3249 spv_check_supported(float64, cap);
3251 case SpvCapabilityInt64:
3252 spv_check_supported(int64, cap);
3255 case SpvCapabilityAddresses:
3256 case SpvCapabilityKernel:
3257 case SpvCapabilityImageBasic:
3258 case SpvCapabilityImageReadWrite:
3259 case SpvCapabilityImageMipmap:
3260 case SpvCapabilityPipes:
3261 case SpvCapabilityGroups:
3262 case SpvCapabilityDeviceEnqueue:
3263 case SpvCapabilityLiteralSampler:
3264 case SpvCapabilityGenericPointer:
3265 vtn_warn("Unsupported OpenCL-style SPIR-V capability: %s",
3266 spirv_capability_to_string(cap));
3269 case SpvCapabilityImageMSArray:
3270 spv_check_supported(image_ms_array, cap);
3273 case SpvCapabilityTessellation:
3274 case SpvCapabilityTessellationPointSize:
3275 spv_check_supported(tessellation, cap);
3278 case SpvCapabilityDrawParameters:
3279 spv_check_supported(draw_parameters, cap);
3282 case SpvCapabilityStorageImageReadWithoutFormat:
3283 spv_check_supported(image_read_without_format, cap);
3286 case SpvCapabilityStorageImageWriteWithoutFormat:
3287 spv_check_supported(image_write_without_format, cap);
3290 case SpvCapabilityDeviceGroup:
3291 spv_check_supported(device_group, cap);
3294 case SpvCapabilityMultiView:
3295 spv_check_supported(multiview, cap);
3298 case SpvCapabilityGroupNonUniform:
3299 spv_check_supported(subgroup_basic, cap);
3302 case SpvCapabilityGroupNonUniformVote:
3303 spv_check_supported(subgroup_vote, cap);
3306 case SpvCapabilitySubgroupBallotKHR:
3307 case SpvCapabilityGroupNonUniformBallot:
3308 spv_check_supported(subgroup_ballot, cap);
3311 case SpvCapabilityGroupNonUniformShuffle:
3312 case SpvCapabilityGroupNonUniformShuffleRelative:
3313 spv_check_supported(subgroup_shuffle, cap);
3316 case SpvCapabilityGroupNonUniformQuad:
3317 spv_check_supported(subgroup_quad, cap);
3319 case SpvCapabilityGroupNonUniformArithmetic:
3320 case SpvCapabilityGroupNonUniformClustered:
3321 spv_check_supported(subgroup_arithmetic, cap);
3323 case SpvCapabilityVariablePointersStorageBuffer:
3324 case SpvCapabilityVariablePointers:
3325 spv_check_supported(variable_pointers, cap);
3328 case SpvCapabilityStorageUniformBufferBlock16:
3329 case SpvCapabilityStorageUniform16:
3330 case SpvCapabilityStoragePushConstant16:
3331 case SpvCapabilityStorageInputOutput16:
3332 spv_check_supported(storage_16bit, cap);
3335 case SpvCapabilityShaderViewportIndexLayerEXT:
3336 spv_check_supported(shader_viewport_index_layer, cap);
3340 vtn_fail("Unhandled capability");
3345 case SpvOpExtInstImport:
3346 vtn_handle_extension(b, opcode, w, count);
3349 case SpvOpMemoryModel:
3350 vtn_assert(w[1] == SpvAddressingModelLogical);
3351 vtn_assert(w[2] == SpvMemoryModelSimple ||
3352 w[2] == SpvMemoryModelGLSL450);
3355 case SpvOpEntryPoint: {
3356 struct vtn_value *entry_point = &b->values[w[2]];
3357 /* Let this be a name label regardless */
3358 unsigned name_words;
3359 entry_point->name = vtn_string_literal(b, &w[3], count - 3, &name_words);
3361 if (strcmp(entry_point->name, b->entry_point_name) != 0 ||
3362 stage_for_execution_model(b, w[1]) != b->entry_point_stage)
3365 vtn_assert(b->entry_point == NULL);
3366 b->entry_point = entry_point;
3371 vtn_push_value(b, w[1], vtn_value_type_string)->str =
3372 vtn_string_literal(b, &w[2], count - 2, NULL);
3376 b->values[w[1]].name = vtn_string_literal(b, &w[2], count - 2, NULL);
3379 case SpvOpMemberName:
3383 case SpvOpExecutionMode:
3384 case SpvOpDecorationGroup:
3386 case SpvOpMemberDecorate:
3387 case SpvOpGroupDecorate:
3388 case SpvOpGroupMemberDecorate:
3389 vtn_handle_decoration(b, opcode, w, count);
3393 return false; /* End of preamble */
3400 vtn_handle_execution_mode(struct vtn_builder *b, struct vtn_value *entry_point,
3401 const struct vtn_decoration *mode, void *data)
3403 vtn_assert(b->entry_point == entry_point);
3405 switch(mode->exec_mode) {
3406 case SpvExecutionModeOriginUpperLeft:
3407 case SpvExecutionModeOriginLowerLeft:
3408 b->origin_upper_left =
3409 (mode->exec_mode == SpvExecutionModeOriginUpperLeft);
3412 case SpvExecutionModeEarlyFragmentTests:
3413 vtn_assert(b->shader->info.stage == MESA_SHADER_FRAGMENT);
3414 b->shader->info.fs.early_fragment_tests = true;
3417 case SpvExecutionModeInvocations:
3418 vtn_assert(b->shader->info.stage == MESA_SHADER_GEOMETRY);
3419 b->shader->info.gs.invocations = MAX2(1, mode->literals[0]);
3422 case SpvExecutionModeDepthReplacing:
3423 vtn_assert(b->shader->info.stage == MESA_SHADER_FRAGMENT);
3424 b->shader->info.fs.depth_layout = FRAG_DEPTH_LAYOUT_ANY;
3426 case SpvExecutionModeDepthGreater:
3427 vtn_assert(b->shader->info.stage == MESA_SHADER_FRAGMENT);
3428 b->shader->info.fs.depth_layout = FRAG_DEPTH_LAYOUT_GREATER;
3430 case SpvExecutionModeDepthLess:
3431 vtn_assert(b->shader->info.stage == MESA_SHADER_FRAGMENT);
3432 b->shader->info.fs.depth_layout = FRAG_DEPTH_LAYOUT_LESS;
3434 case SpvExecutionModeDepthUnchanged:
3435 vtn_assert(b->shader->info.stage == MESA_SHADER_FRAGMENT);
3436 b->shader->info.fs.depth_layout = FRAG_DEPTH_LAYOUT_UNCHANGED;
3439 case SpvExecutionModeLocalSize:
3440 vtn_assert(b->shader->info.stage == MESA_SHADER_COMPUTE);
3441 b->shader->info.cs.local_size[0] = mode->literals[0];
3442 b->shader->info.cs.local_size[1] = mode->literals[1];
3443 b->shader->info.cs.local_size[2] = mode->literals[2];
3445 case SpvExecutionModeLocalSizeHint:
3446 break; /* Nothing to do with this */
3448 case SpvExecutionModeOutputVertices:
3449 if (b->shader->info.stage == MESA_SHADER_TESS_CTRL ||
3450 b->shader->info.stage == MESA_SHADER_TESS_EVAL) {
3451 b->shader->info.tess.tcs_vertices_out = mode->literals[0];
3453 vtn_assert(b->shader->info.stage == MESA_SHADER_GEOMETRY);
3454 b->shader->info.gs.vertices_out = mode->literals[0];
3458 case SpvExecutionModeInputPoints:
3459 case SpvExecutionModeInputLines:
3460 case SpvExecutionModeInputLinesAdjacency:
3461 case SpvExecutionModeTriangles:
3462 case SpvExecutionModeInputTrianglesAdjacency:
3463 case SpvExecutionModeQuads:
3464 case SpvExecutionModeIsolines:
3465 if (b->shader->info.stage == MESA_SHADER_TESS_CTRL ||
3466 b->shader->info.stage == MESA_SHADER_TESS_EVAL) {
3467 b->shader->info.tess.primitive_mode =
3468 gl_primitive_from_spv_execution_mode(b, mode->exec_mode);
3470 vtn_assert(b->shader->info.stage == MESA_SHADER_GEOMETRY);
3471 b->shader->info.gs.vertices_in =
3472 vertices_in_from_spv_execution_mode(b, mode->exec_mode);
3476 case SpvExecutionModeOutputPoints:
3477 case SpvExecutionModeOutputLineStrip:
3478 case SpvExecutionModeOutputTriangleStrip:
3479 vtn_assert(b->shader->info.stage == MESA_SHADER_GEOMETRY);
3480 b->shader->info.gs.output_primitive =
3481 gl_primitive_from_spv_execution_mode(b, mode->exec_mode);
3484 case SpvExecutionModeSpacingEqual:
3485 vtn_assert(b->shader->info.stage == MESA_SHADER_TESS_CTRL ||
3486 b->shader->info.stage == MESA_SHADER_TESS_EVAL);
3487 b->shader->info.tess.spacing = TESS_SPACING_EQUAL;
3489 case SpvExecutionModeSpacingFractionalEven:
3490 vtn_assert(b->shader->info.stage == MESA_SHADER_TESS_CTRL ||
3491 b->shader->info.stage == MESA_SHADER_TESS_EVAL);
3492 b->shader->info.tess.spacing = TESS_SPACING_FRACTIONAL_EVEN;
3494 case SpvExecutionModeSpacingFractionalOdd:
3495 vtn_assert(b->shader->info.stage == MESA_SHADER_TESS_CTRL ||
3496 b->shader->info.stage == MESA_SHADER_TESS_EVAL);
3497 b->shader->info.tess.spacing = TESS_SPACING_FRACTIONAL_ODD;
3499 case SpvExecutionModeVertexOrderCw:
3500 vtn_assert(b->shader->info.stage == MESA_SHADER_TESS_CTRL ||
3501 b->shader->info.stage == MESA_SHADER_TESS_EVAL);
3502 b->shader->info.tess.ccw = false;
3504 case SpvExecutionModeVertexOrderCcw:
3505 vtn_assert(b->shader->info.stage == MESA_SHADER_TESS_CTRL ||
3506 b->shader->info.stage == MESA_SHADER_TESS_EVAL);
3507 b->shader->info.tess.ccw = true;
3509 case SpvExecutionModePointMode:
3510 vtn_assert(b->shader->info.stage == MESA_SHADER_TESS_CTRL ||
3511 b->shader->info.stage == MESA_SHADER_TESS_EVAL);
3512 b->shader->info.tess.point_mode = true;
3515 case SpvExecutionModePixelCenterInteger:
3516 b->pixel_center_integer = true;
3519 case SpvExecutionModeXfb:
3520 vtn_fail("Unhandled execution mode");
3523 case SpvExecutionModeVecTypeHint:
3524 case SpvExecutionModeContractionOff:
3528 vtn_fail("Unhandled execution mode");
3533 vtn_handle_variable_or_type_instruction(struct vtn_builder *b, SpvOp opcode,
3534 const uint32_t *w, unsigned count)
3536 vtn_set_instruction_result_type(b, opcode, w, count);
3540 case SpvOpSourceContinued:
3541 case SpvOpSourceExtension:
3542 case SpvOpExtension:
3543 case SpvOpCapability:
3544 case SpvOpExtInstImport:
3545 case SpvOpMemoryModel:
3546 case SpvOpEntryPoint:
3547 case SpvOpExecutionMode:
3550 case SpvOpMemberName:
3551 case SpvOpDecorationGroup:
3553 case SpvOpMemberDecorate:
3554 case SpvOpGroupDecorate:
3555 case SpvOpGroupMemberDecorate:
3556 vtn_fail("Invalid opcode types and variables section");
3562 case SpvOpTypeFloat:
3563 case SpvOpTypeVector:
3564 case SpvOpTypeMatrix:
3565 case SpvOpTypeImage:
3566 case SpvOpTypeSampler:
3567 case SpvOpTypeSampledImage:
3568 case SpvOpTypeArray:
3569 case SpvOpTypeRuntimeArray:
3570 case SpvOpTypeStruct:
3571 case SpvOpTypeOpaque:
3572 case SpvOpTypePointer:
3573 case SpvOpTypeFunction:
3574 case SpvOpTypeEvent:
3575 case SpvOpTypeDeviceEvent:
3576 case SpvOpTypeReserveId:
3577 case SpvOpTypeQueue:
3579 vtn_handle_type(b, opcode, w, count);
3582 case SpvOpConstantTrue:
3583 case SpvOpConstantFalse:
3585 case SpvOpConstantComposite:
3586 case SpvOpConstantSampler:
3587 case SpvOpConstantNull:
3588 case SpvOpSpecConstantTrue:
3589 case SpvOpSpecConstantFalse:
3590 case SpvOpSpecConstant:
3591 case SpvOpSpecConstantComposite:
3592 case SpvOpSpecConstantOp:
3593 vtn_handle_constant(b, opcode, w, count);
3598 vtn_handle_variables(b, opcode, w, count);
3602 return false; /* End of preamble */
3609 vtn_handle_body_instruction(struct vtn_builder *b, SpvOp opcode,
3610 const uint32_t *w, unsigned count)
3616 case SpvOpLoopMerge:
3617 case SpvOpSelectionMerge:
3618 /* This is handled by cfg pre-pass and walk_blocks */
3622 struct vtn_value *val = vtn_push_value(b, w[2], vtn_value_type_undef);
3623 val->type = vtn_value(b, w[1], vtn_value_type_type)->type;
3628 vtn_handle_extension(b, opcode, w, count);
3634 case SpvOpCopyMemory:
3635 case SpvOpCopyMemorySized:
3636 case SpvOpAccessChain:
3637 case SpvOpPtrAccessChain:
3638 case SpvOpInBoundsAccessChain:
3639 case SpvOpArrayLength:
3640 vtn_handle_variables(b, opcode, w, count);
3643 case SpvOpFunctionCall:
3644 vtn_handle_function_call(b, opcode, w, count);
3647 case SpvOpSampledImage:
3649 case SpvOpImageSampleImplicitLod:
3650 case SpvOpImageSampleExplicitLod:
3651 case SpvOpImageSampleDrefImplicitLod:
3652 case SpvOpImageSampleDrefExplicitLod:
3653 case SpvOpImageSampleProjImplicitLod:
3654 case SpvOpImageSampleProjExplicitLod:
3655 case SpvOpImageSampleProjDrefImplicitLod:
3656 case SpvOpImageSampleProjDrefExplicitLod:
3657 case SpvOpImageFetch:
3658 case SpvOpImageGather:
3659 case SpvOpImageDrefGather:
3660 case SpvOpImageQuerySizeLod:
3661 case SpvOpImageQueryLod:
3662 case SpvOpImageQueryLevels:
3663 case SpvOpImageQuerySamples:
3664 vtn_handle_texture(b, opcode, w, count);
3667 case SpvOpImageRead:
3668 case SpvOpImageWrite:
3669 case SpvOpImageTexelPointer:
3670 vtn_handle_image(b, opcode, w, count);
3673 case SpvOpImageQuerySize: {
3674 struct vtn_pointer *image =
3675 vtn_value(b, w[3], vtn_value_type_pointer)->pointer;
3676 if (image->mode == vtn_variable_mode_image) {
3677 vtn_handle_image(b, opcode, w, count);
3679 vtn_assert(image->mode == vtn_variable_mode_sampler);
3680 vtn_handle_texture(b, opcode, w, count);
3685 case SpvOpAtomicLoad:
3686 case SpvOpAtomicExchange:
3687 case SpvOpAtomicCompareExchange:
3688 case SpvOpAtomicCompareExchangeWeak:
3689 case SpvOpAtomicIIncrement:
3690 case SpvOpAtomicIDecrement:
3691 case SpvOpAtomicIAdd:
3692 case SpvOpAtomicISub:
3693 case SpvOpAtomicSMin:
3694 case SpvOpAtomicUMin:
3695 case SpvOpAtomicSMax:
3696 case SpvOpAtomicUMax:
3697 case SpvOpAtomicAnd:
3699 case SpvOpAtomicXor: {
3700 struct vtn_value *pointer = vtn_untyped_value(b, w[3]);
3701 if (pointer->value_type == vtn_value_type_image_pointer) {
3702 vtn_handle_image(b, opcode, w, count);
3704 vtn_assert(pointer->value_type == vtn_value_type_pointer);
3705 vtn_handle_ssbo_or_shared_atomic(b, opcode, w, count);
3710 case SpvOpAtomicStore: {
3711 struct vtn_value *pointer = vtn_untyped_value(b, w[1]);
3712 if (pointer->value_type == vtn_value_type_image_pointer) {
3713 vtn_handle_image(b, opcode, w, count);
3715 vtn_assert(pointer->value_type == vtn_value_type_pointer);
3716 vtn_handle_ssbo_or_shared_atomic(b, opcode, w, count);
3722 /* Handle OpSelect up-front here because it needs to be able to handle
3723 * pointers and not just regular vectors and scalars.
3725 struct vtn_value *res_val = vtn_untyped_value(b, w[2]);
3726 struct vtn_value *sel_val = vtn_untyped_value(b, w[3]);
3727 struct vtn_value *obj1_val = vtn_untyped_value(b, w[4]);
3728 struct vtn_value *obj2_val = vtn_untyped_value(b, w[5]);
3730 const struct glsl_type *sel_type;
3731 switch (res_val->type->base_type) {
3732 case vtn_base_type_scalar:
3733 sel_type = glsl_bool_type();
3735 case vtn_base_type_vector:
3736 sel_type = glsl_vector_type(GLSL_TYPE_BOOL, res_val->type->length);
3738 case vtn_base_type_pointer:
3739 /* We need to have actual storage for pointer types */
3740 vtn_fail_if(res_val->type->type == NULL,
3741 "Invalid pointer result type for OpSelect");
3742 sel_type = glsl_bool_type();
3745 vtn_fail("Result type of OpSelect must be a scalar, vector, or pointer");
3748 if (unlikely(sel_val->type->type != sel_type)) {
3749 if (sel_val->type->type == glsl_bool_type()) {
3750 /* This case is illegal but some older versions of GLSLang produce
3751 * it. The GLSLang issue was fixed on March 30, 2017:
3753 * https://github.com/KhronosGroup/glslang/issues/809
3755 * Unfortunately, there are applications in the wild which are
3756 * shipping with this bug so it isn't nice to fail on them so we
3757 * throw a warning instead. It's not actually a problem for us as
3758 * nir_builder will just splat the condition out which is most
3759 * likely what the client wanted anyway.
3761 vtn_warn("Condition type of OpSelect must have the same number "
3762 "of components as Result Type");
3764 vtn_fail("Condition type of OpSelect must be a scalar or vector "
3765 "of Boolean type. It must have the same number of "
3766 "components as Result Type");
3770 vtn_fail_if(obj1_val->type != res_val->type ||
3771 obj2_val->type != res_val->type,
3772 "Object types must match the result type in OpSelect");
3774 struct vtn_type *res_type = vtn_value(b, w[1], vtn_value_type_type)->type;
3775 struct vtn_ssa_value *ssa = vtn_create_ssa_value(b, res_type->type);
3776 ssa->def = nir_bcsel(&b->nb, vtn_ssa_value(b, w[3])->def,
3777 vtn_ssa_value(b, w[4])->def,
3778 vtn_ssa_value(b, w[5])->def);
3779 vtn_push_ssa(b, w[2], res_type, ssa);
3788 case SpvOpConvertFToU:
3789 case SpvOpConvertFToS:
3790 case SpvOpConvertSToF:
3791 case SpvOpConvertUToF:
3795 case SpvOpQuantizeToF16:
3796 case SpvOpConvertPtrToU:
3797 case SpvOpConvertUToPtr:
3798 case SpvOpPtrCastToGeneric:
3799 case SpvOpGenericCastToPtr:
3805 case SpvOpSignBitSet:
3806 case SpvOpLessOrGreater:
3808 case SpvOpUnordered:
3823 case SpvOpVectorTimesScalar:
3825 case SpvOpIAddCarry:
3826 case SpvOpISubBorrow:
3827 case SpvOpUMulExtended:
3828 case SpvOpSMulExtended:
3829 case SpvOpShiftRightLogical:
3830 case SpvOpShiftRightArithmetic:
3831 case SpvOpShiftLeftLogical:
3832 case SpvOpLogicalEqual:
3833 case SpvOpLogicalNotEqual:
3834 case SpvOpLogicalOr:
3835 case SpvOpLogicalAnd:
3836 case SpvOpLogicalNot:
3837 case SpvOpBitwiseOr:
3838 case SpvOpBitwiseXor:
3839 case SpvOpBitwiseAnd:
3841 case SpvOpFOrdEqual:
3842 case SpvOpFUnordEqual:
3843 case SpvOpINotEqual:
3844 case SpvOpFOrdNotEqual:
3845 case SpvOpFUnordNotEqual:
3846 case SpvOpULessThan:
3847 case SpvOpSLessThan:
3848 case SpvOpFOrdLessThan:
3849 case SpvOpFUnordLessThan:
3850 case SpvOpUGreaterThan:
3851 case SpvOpSGreaterThan:
3852 case SpvOpFOrdGreaterThan:
3853 case SpvOpFUnordGreaterThan:
3854 case SpvOpULessThanEqual:
3855 case SpvOpSLessThanEqual:
3856 case SpvOpFOrdLessThanEqual:
3857 case SpvOpFUnordLessThanEqual:
3858 case SpvOpUGreaterThanEqual:
3859 case SpvOpSGreaterThanEqual:
3860 case SpvOpFOrdGreaterThanEqual:
3861 case SpvOpFUnordGreaterThanEqual:
3867 case SpvOpFwidthFine:
3868 case SpvOpDPdxCoarse:
3869 case SpvOpDPdyCoarse:
3870 case SpvOpFwidthCoarse:
3871 case SpvOpBitFieldInsert:
3872 case SpvOpBitFieldSExtract:
3873 case SpvOpBitFieldUExtract:
3874 case SpvOpBitReverse:
3876 case SpvOpTranspose:
3877 case SpvOpOuterProduct:
3878 case SpvOpMatrixTimesScalar:
3879 case SpvOpVectorTimesMatrix:
3880 case SpvOpMatrixTimesVector:
3881 case SpvOpMatrixTimesMatrix:
3882 vtn_handle_alu(b, opcode, w, count);
3885 case SpvOpVectorExtractDynamic:
3886 case SpvOpVectorInsertDynamic:
3887 case SpvOpVectorShuffle:
3888 case SpvOpCompositeConstruct:
3889 case SpvOpCompositeExtract:
3890 case SpvOpCompositeInsert:
3891 case SpvOpCopyObject:
3892 vtn_handle_composite(b, opcode, w, count);
3895 case SpvOpEmitVertex:
3896 case SpvOpEndPrimitive:
3897 case SpvOpEmitStreamVertex:
3898 case SpvOpEndStreamPrimitive:
3899 case SpvOpControlBarrier:
3900 case SpvOpMemoryBarrier:
3901 vtn_handle_barrier(b, opcode, w, count);
3904 case SpvOpGroupNonUniformElect:
3905 case SpvOpGroupNonUniformAll:
3906 case SpvOpGroupNonUniformAny:
3907 case SpvOpGroupNonUniformAllEqual:
3908 case SpvOpGroupNonUniformBroadcast:
3909 case SpvOpGroupNonUniformBroadcastFirst:
3910 case SpvOpGroupNonUniformBallot:
3911 case SpvOpGroupNonUniformInverseBallot:
3912 case SpvOpGroupNonUniformBallotBitExtract:
3913 case SpvOpGroupNonUniformBallotBitCount:
3914 case SpvOpGroupNonUniformBallotFindLSB:
3915 case SpvOpGroupNonUniformBallotFindMSB:
3916 case SpvOpGroupNonUniformShuffle:
3917 case SpvOpGroupNonUniformShuffleXor:
3918 case SpvOpGroupNonUniformShuffleUp:
3919 case SpvOpGroupNonUniformShuffleDown:
3920 case SpvOpGroupNonUniformIAdd:
3921 case SpvOpGroupNonUniformFAdd:
3922 case SpvOpGroupNonUniformIMul:
3923 case SpvOpGroupNonUniformFMul:
3924 case SpvOpGroupNonUniformSMin:
3925 case SpvOpGroupNonUniformUMin:
3926 case SpvOpGroupNonUniformFMin:
3927 case SpvOpGroupNonUniformSMax:
3928 case SpvOpGroupNonUniformUMax:
3929 case SpvOpGroupNonUniformFMax:
3930 case SpvOpGroupNonUniformBitwiseAnd:
3931 case SpvOpGroupNonUniformBitwiseOr:
3932 case SpvOpGroupNonUniformBitwiseXor:
3933 case SpvOpGroupNonUniformLogicalAnd:
3934 case SpvOpGroupNonUniformLogicalOr:
3935 case SpvOpGroupNonUniformLogicalXor:
3936 case SpvOpGroupNonUniformQuadBroadcast:
3937 case SpvOpGroupNonUniformQuadSwap:
3938 vtn_handle_subgroup(b, opcode, w, count);
3942 vtn_fail("Unhandled opcode");
3949 spirv_to_nir(const uint32_t *words, size_t word_count,
3950 struct nir_spirv_specialization *spec, unsigned num_spec,
3951 gl_shader_stage stage, const char *entry_point_name,
3952 const struct spirv_to_nir_options *options,
3953 const nir_shader_compiler_options *nir_options)
3955 /* Initialize the stn_builder object */
3956 struct vtn_builder *b = rzalloc(NULL, struct vtn_builder);
3958 b->spirv_word_count = word_count;
3962 exec_list_make_empty(&b->functions);
3963 b->entry_point_stage = stage;
3964 b->entry_point_name = entry_point_name;
3965 b->options = options;
3967 /* See also _vtn_fail() */
3968 if (setjmp(b->fail_jump)) {
3973 const uint32_t *word_end = words + word_count;
3975 /* Handle the SPIR-V header (first 4 dwords) */
3976 vtn_assert(word_count > 5);
3978 vtn_assert(words[0] == SpvMagicNumber);
3979 vtn_assert(words[1] >= 0x10000);
3980 /* words[2] == generator magic */
3981 unsigned value_id_bound = words[3];
3982 vtn_assert(words[4] == 0);
3986 b->value_id_bound = value_id_bound;
3987 b->values = rzalloc_array(b, struct vtn_value, value_id_bound);
3989 /* Handle all the preamble instructions */
3990 words = vtn_foreach_instruction(b, words, word_end,
3991 vtn_handle_preamble_instruction);
3993 if (b->entry_point == NULL) {
3994 vtn_fail("Entry point not found");
3999 b->shader = nir_shader_create(b, stage, nir_options, NULL);
4001 /* Set shader info defaults */
4002 b->shader->info.gs.invocations = 1;
4004 /* Parse execution modes */
4005 vtn_foreach_execution_mode(b, b->entry_point,
4006 vtn_handle_execution_mode, NULL);
4008 b->specializations = spec;
4009 b->num_specializations = num_spec;
4011 /* Handle all variable, type, and constant instructions */
4012 words = vtn_foreach_instruction(b, words, word_end,
4013 vtn_handle_variable_or_type_instruction);
4015 /* Set types on all vtn_values */
4016 vtn_foreach_instruction(b, words, word_end, vtn_set_instruction_result_type);
4018 vtn_build_cfg(b, words, word_end);
4020 assert(b->entry_point->value_type == vtn_value_type_function);
4021 b->entry_point->func->referenced = true;
4026 foreach_list_typed(struct vtn_function, func, node, &b->functions) {
4027 if (func->referenced && !func->emitted) {
4028 b->const_table = _mesa_hash_table_create(b, _mesa_hash_pointer,
4029 _mesa_key_pointer_equal);
4031 vtn_function_emit(b, func, vtn_handle_body_instruction);
4037 vtn_assert(b->entry_point->value_type == vtn_value_type_function);
4038 nir_function *entry_point = b->entry_point->func->impl->function;
4039 vtn_assert(entry_point);
4041 /* Unparent the shader from the vtn_builder before we delete the builder */
4042 ralloc_steal(NULL, b->shader);