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 "nir/nir_deref.h"
33 #include "spirv_info.h"
35 #include "util/format/u_format.h"
36 #include "util/u_math.h"
41 vtn_log(struct vtn_builder
*b
, enum nir_spirv_debug_level level
,
42 size_t spirv_offset
, const char *message
)
44 if (b
->options
->debug
.func
) {
45 b
->options
->debug
.func(b
->options
->debug
.private_data
,
46 level
, spirv_offset
, message
);
50 if (level
>= NIR_SPIRV_DEBUG_LEVEL_WARNING
)
51 fprintf(stderr
, "%s\n", message
);
56 vtn_logf(struct vtn_builder
*b
, enum nir_spirv_debug_level level
,
57 size_t spirv_offset
, const char *fmt
, ...)
63 msg
= ralloc_vasprintf(NULL
, fmt
, args
);
66 vtn_log(b
, level
, spirv_offset
, msg
);
72 vtn_log_err(struct vtn_builder
*b
,
73 enum nir_spirv_debug_level level
, const char *prefix
,
74 const char *file
, unsigned line
,
75 const char *fmt
, va_list args
)
79 msg
= ralloc_strdup(NULL
, prefix
);
82 ralloc_asprintf_append(&msg
, " In file %s:%u\n", file
, line
);
85 ralloc_asprintf_append(&msg
, " ");
87 ralloc_vasprintf_append(&msg
, fmt
, args
);
89 ralloc_asprintf_append(&msg
, "\n %zu bytes into the SPIR-V binary",
93 ralloc_asprintf_append(&msg
,
94 "\n in SPIR-V source file %s, line %d, col %d",
95 b
->file
, b
->line
, b
->col
);
98 vtn_log(b
, level
, b
->spirv_offset
, msg
);
104 vtn_dump_shader(struct vtn_builder
*b
, const char *path
, const char *prefix
)
109 int len
= snprintf(filename
, sizeof(filename
), "%s/%s-%d.spirv",
110 path
, prefix
, idx
++);
111 if (len
< 0 || len
>= sizeof(filename
))
114 FILE *f
= fopen(filename
, "w");
118 fwrite(b
->spirv
, sizeof(*b
->spirv
), b
->spirv_word_count
, f
);
121 vtn_info("SPIR-V shader dumped to %s", filename
);
125 _vtn_warn(struct vtn_builder
*b
, const char *file
, unsigned line
,
126 const char *fmt
, ...)
131 vtn_log_err(b
, NIR_SPIRV_DEBUG_LEVEL_WARNING
, "SPIR-V WARNING:\n",
132 file
, line
, fmt
, args
);
137 _vtn_err(struct vtn_builder
*b
, const char *file
, unsigned line
,
138 const char *fmt
, ...)
143 vtn_log_err(b
, NIR_SPIRV_DEBUG_LEVEL_ERROR
, "SPIR-V ERROR:\n",
144 file
, line
, fmt
, args
);
149 _vtn_fail(struct vtn_builder
*b
, const char *file
, unsigned line
,
150 const char *fmt
, ...)
155 vtn_log_err(b
, NIR_SPIRV_DEBUG_LEVEL_ERROR
, "SPIR-V parsing FAILED:\n",
156 file
, line
, fmt
, args
);
159 const char *dump_path
= getenv("MESA_SPIRV_FAIL_DUMP_PATH");
161 vtn_dump_shader(b
, dump_path
, "fail");
163 longjmp(b
->fail_jump
, 1);
166 static struct vtn_ssa_value
*
167 vtn_undef_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
169 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
170 val
->type
= glsl_get_bare_type(type
);
172 if (glsl_type_is_vector_or_scalar(type
)) {
173 unsigned num_components
= glsl_get_vector_elements(val
->type
);
174 unsigned bit_size
= glsl_get_bit_size(val
->type
);
175 val
->def
= nir_ssa_undef(&b
->nb
, num_components
, bit_size
);
177 unsigned elems
= glsl_get_length(val
->type
);
178 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
179 if (glsl_type_is_array_or_matrix(type
)) {
180 const struct glsl_type
*elem_type
= glsl_get_array_element(type
);
181 for (unsigned i
= 0; i
< elems
; i
++)
182 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
184 vtn_assert(glsl_type_is_struct_or_ifc(type
));
185 for (unsigned i
= 0; i
< elems
; i
++) {
186 const struct glsl_type
*elem_type
= glsl_get_struct_field(type
, i
);
187 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
195 static struct vtn_ssa_value
*
196 vtn_const_ssa_value(struct vtn_builder
*b
, nir_constant
*constant
,
197 const struct glsl_type
*type
)
199 struct hash_entry
*entry
= _mesa_hash_table_search(b
->const_table
, constant
);
204 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
205 val
->type
= glsl_get_bare_type(type
);
207 if (glsl_type_is_vector_or_scalar(type
)) {
208 unsigned num_components
= glsl_get_vector_elements(val
->type
);
209 unsigned bit_size
= glsl_get_bit_size(type
);
210 nir_load_const_instr
*load
=
211 nir_load_const_instr_create(b
->shader
, num_components
, bit_size
);
213 memcpy(load
->value
, constant
->values
,
214 sizeof(nir_const_value
) * num_components
);
216 nir_instr_insert_before_cf_list(&b
->nb
.impl
->body
, &load
->instr
);
217 val
->def
= &load
->def
;
219 unsigned elems
= glsl_get_length(val
->type
);
220 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
221 if (glsl_type_is_array_or_matrix(type
)) {
222 const struct glsl_type
*elem_type
= glsl_get_array_element(type
);
223 for (unsigned i
= 0; i
< elems
; i
++) {
224 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
228 vtn_assert(glsl_type_is_struct_or_ifc(type
));
229 for (unsigned i
= 0; i
< elems
; i
++) {
230 const struct glsl_type
*elem_type
= glsl_get_struct_field(type
, i
);
231 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
240 struct vtn_ssa_value
*
241 vtn_ssa_value(struct vtn_builder
*b
, uint32_t value_id
)
243 struct vtn_value
*val
= vtn_untyped_value(b
, value_id
);
244 switch (val
->value_type
) {
245 case vtn_value_type_undef
:
246 return vtn_undef_ssa_value(b
, val
->type
->type
);
248 case vtn_value_type_constant
:
249 return vtn_const_ssa_value(b
, val
->constant
, val
->type
->type
);
251 case vtn_value_type_ssa
:
254 case vtn_value_type_pointer
:
255 vtn_assert(val
->pointer
->ptr_type
&& val
->pointer
->ptr_type
->type
);
256 struct vtn_ssa_value
*ssa
=
257 vtn_create_ssa_value(b
, val
->pointer
->ptr_type
->type
);
258 ssa
->def
= vtn_pointer_to_ssa(b
, val
->pointer
);
262 vtn_fail("Invalid type for an SSA value");
267 vtn_push_ssa_value(struct vtn_builder
*b
, uint32_t value_id
,
268 struct vtn_ssa_value
*ssa
)
270 struct vtn_type
*type
= vtn_get_value_type(b
, value_id
);
272 /* See vtn_create_ssa_value */
273 vtn_fail_if(ssa
->type
!= glsl_get_bare_type(type
->type
),
274 "Type mismatch for SPIR-V SSA value");
276 struct vtn_value
*val
;
277 if (type
->base_type
== vtn_base_type_pointer
) {
278 val
= vtn_push_pointer(b
, value_id
, vtn_pointer_from_ssa(b
, ssa
->def
, type
));
280 /* Don't trip the value_type_ssa check in vtn_push_value */
281 val
= vtn_push_value(b
, value_id
, vtn_value_type_invalid
);
282 val
->value_type
= vtn_value_type_ssa
;
290 vtn_get_nir_ssa(struct vtn_builder
*b
, uint32_t value_id
)
292 struct vtn_ssa_value
*ssa
= vtn_ssa_value(b
, value_id
);
293 vtn_fail_if(!glsl_type_is_vector_or_scalar(ssa
->type
),
294 "Expected a vector or scalar type");
299 vtn_push_nir_ssa(struct vtn_builder
*b
, uint32_t value_id
, nir_ssa_def
*def
)
301 /* Types for all SPIR-V SSA values are set as part of a pre-pass so the
302 * type will be valid by the time we get here.
304 struct vtn_type
*type
= vtn_get_value_type(b
, value_id
);
305 vtn_fail_if(def
->num_components
!= glsl_get_vector_elements(type
->type
) ||
306 def
->bit_size
!= glsl_get_bit_size(type
->type
),
307 "Mismatch between NIR and SPIR-V type.");
308 struct vtn_ssa_value
*ssa
= vtn_create_ssa_value(b
, type
->type
);
310 return vtn_push_ssa_value(b
, value_id
, ssa
);
313 static nir_deref_instr
*
314 vtn_get_image(struct vtn_builder
*b
, uint32_t value_id
)
316 struct vtn_type
*type
= vtn_get_value_type(b
, value_id
);
317 vtn_assert(type
->base_type
== vtn_base_type_image
);
318 return nir_build_deref_cast(&b
->nb
, vtn_get_nir_ssa(b
, value_id
),
319 nir_var_uniform
, type
->glsl_image
, 0);
323 vtn_push_image(struct vtn_builder
*b
, uint32_t value_id
,
324 nir_deref_instr
*deref
)
326 struct vtn_type
*type
= vtn_get_value_type(b
, value_id
);
327 vtn_assert(type
->base_type
== vtn_base_type_image
);
328 vtn_push_nir_ssa(b
, value_id
, &deref
->dest
.ssa
);
331 static nir_deref_instr
*
332 vtn_get_sampler(struct vtn_builder
*b
, uint32_t value_id
)
334 struct vtn_type
*type
= vtn_get_value_type(b
, value_id
);
335 vtn_assert(type
->base_type
== vtn_base_type_sampler
);
336 return nir_build_deref_cast(&b
->nb
, vtn_get_nir_ssa(b
, value_id
),
337 nir_var_uniform
, glsl_bare_sampler_type(), 0);
341 vtn_sampled_image_to_nir_ssa(struct vtn_builder
*b
,
342 struct vtn_sampled_image si
)
344 return nir_vec2(&b
->nb
, &si
.image
->dest
.ssa
, &si
.sampler
->dest
.ssa
);
348 vtn_push_sampled_image(struct vtn_builder
*b
, uint32_t value_id
,
349 struct vtn_sampled_image si
)
351 struct vtn_type
*type
= vtn_get_value_type(b
, value_id
);
352 vtn_assert(type
->base_type
== vtn_base_type_sampled_image
);
353 vtn_push_nir_ssa(b
, value_id
, vtn_sampled_image_to_nir_ssa(b
, si
));
356 static struct vtn_sampled_image
357 vtn_get_sampled_image(struct vtn_builder
*b
, uint32_t value_id
)
359 struct vtn_type
*type
= vtn_get_value_type(b
, value_id
);
360 vtn_assert(type
->base_type
== vtn_base_type_sampled_image
);
361 nir_ssa_def
*si_vec2
= vtn_get_nir_ssa(b
, value_id
);
363 struct vtn_sampled_image si
= { NULL
, };
364 si
.image
= nir_build_deref_cast(&b
->nb
, nir_channel(&b
->nb
, si_vec2
, 0),
366 type
->image
->glsl_image
, 0);
367 si
.sampler
= nir_build_deref_cast(&b
->nb
, nir_channel(&b
->nb
, si_vec2
, 1),
369 glsl_bare_sampler_type(), 0);
374 vtn_string_literal(struct vtn_builder
*b
, const uint32_t *words
,
375 unsigned word_count
, unsigned *words_used
)
377 char *dup
= ralloc_strndup(b
, (char *)words
, word_count
* sizeof(*words
));
379 /* Ammount of space taken by the string (including the null) */
380 unsigned len
= strlen(dup
) + 1;
381 *words_used
= DIV_ROUND_UP(len
, sizeof(*words
));
387 vtn_foreach_instruction(struct vtn_builder
*b
, const uint32_t *start
,
388 const uint32_t *end
, vtn_instruction_handler handler
)
394 const uint32_t *w
= start
;
396 SpvOp opcode
= w
[0] & SpvOpCodeMask
;
397 unsigned count
= w
[0] >> SpvWordCountShift
;
398 vtn_assert(count
>= 1 && w
+ count
<= end
);
400 b
->spirv_offset
= (uint8_t *)w
- (uint8_t *)b
->spirv
;
404 break; /* Do nothing */
407 b
->file
= vtn_value(b
, w
[1], vtn_value_type_string
)->str
;
419 if (!handler(b
, opcode
, w
, count
))
437 vtn_handle_non_semantic_instruction(struct vtn_builder
*b
, SpvOp ext_opcode
,
438 const uint32_t *w
, unsigned count
)
445 vtn_handle_extension(struct vtn_builder
*b
, SpvOp opcode
,
446 const uint32_t *w
, unsigned count
)
448 const char *ext
= (const char *)&w
[2];
450 case SpvOpExtInstImport
: {
451 struct vtn_value
*val
= vtn_push_value(b
, w
[1], vtn_value_type_extension
);
452 if (strcmp(ext
, "GLSL.std.450") == 0) {
453 val
->ext_handler
= vtn_handle_glsl450_instruction
;
454 } else if ((strcmp(ext
, "SPV_AMD_gcn_shader") == 0)
455 && (b
->options
&& b
->options
->caps
.amd_gcn_shader
)) {
456 val
->ext_handler
= vtn_handle_amd_gcn_shader_instruction
;
457 } else if ((strcmp(ext
, "SPV_AMD_shader_ballot") == 0)
458 && (b
->options
&& b
->options
->caps
.amd_shader_ballot
)) {
459 val
->ext_handler
= vtn_handle_amd_shader_ballot_instruction
;
460 } else if ((strcmp(ext
, "SPV_AMD_shader_trinary_minmax") == 0)
461 && (b
->options
&& b
->options
->caps
.amd_trinary_minmax
)) {
462 val
->ext_handler
= vtn_handle_amd_shader_trinary_minmax_instruction
;
463 } else if ((strcmp(ext
, "SPV_AMD_shader_explicit_vertex_parameter") == 0)
464 && (b
->options
&& b
->options
->caps
.amd_shader_explicit_vertex_parameter
)) {
465 val
->ext_handler
= vtn_handle_amd_shader_explicit_vertex_parameter_instruction
;
466 } else if (strcmp(ext
, "OpenCL.std") == 0) {
467 val
->ext_handler
= vtn_handle_opencl_instruction
;
468 } else if (strstr(ext
, "NonSemantic.") == ext
) {
469 val
->ext_handler
= vtn_handle_non_semantic_instruction
;
471 vtn_fail("Unsupported extension: %s", ext
);
477 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
478 bool handled
= val
->ext_handler(b
, w
[4], w
, count
);
484 vtn_fail_with_opcode("Unhandled opcode", opcode
);
489 _foreach_decoration_helper(struct vtn_builder
*b
,
490 struct vtn_value
*base_value
,
492 struct vtn_value
*value
,
493 vtn_decoration_foreach_cb cb
, void *data
)
495 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
497 if (dec
->scope
== VTN_DEC_DECORATION
) {
498 member
= parent_member
;
499 } else if (dec
->scope
>= VTN_DEC_STRUCT_MEMBER0
) {
500 vtn_fail_if(value
->value_type
!= vtn_value_type_type
||
501 value
->type
->base_type
!= vtn_base_type_struct
,
502 "OpMemberDecorate and OpGroupMemberDecorate are only "
503 "allowed on OpTypeStruct");
504 /* This means we haven't recursed yet */
505 assert(value
== base_value
);
507 member
= dec
->scope
- VTN_DEC_STRUCT_MEMBER0
;
509 vtn_fail_if(member
>= base_value
->type
->length
,
510 "OpMemberDecorate specifies member %d but the "
511 "OpTypeStruct has only %u members",
512 member
, base_value
->type
->length
);
514 /* Not a decoration */
515 assert(dec
->scope
== VTN_DEC_EXECUTION_MODE
);
520 assert(dec
->group
->value_type
== vtn_value_type_decoration_group
);
521 _foreach_decoration_helper(b
, base_value
, member
, dec
->group
,
524 cb(b
, base_value
, member
, dec
, data
);
529 /** Iterates (recursively if needed) over all of the decorations on a value
531 * This function iterates over all of the decorations applied to a given
532 * value. If it encounters a decoration group, it recurses into the group
533 * and iterates over all of those decorations as well.
536 vtn_foreach_decoration(struct vtn_builder
*b
, struct vtn_value
*value
,
537 vtn_decoration_foreach_cb cb
, void *data
)
539 _foreach_decoration_helper(b
, value
, -1, value
, cb
, data
);
543 vtn_foreach_execution_mode(struct vtn_builder
*b
, struct vtn_value
*value
,
544 vtn_execution_mode_foreach_cb cb
, void *data
)
546 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
547 if (dec
->scope
!= VTN_DEC_EXECUTION_MODE
)
550 assert(dec
->group
== NULL
);
551 cb(b
, value
, dec
, data
);
556 vtn_handle_decoration(struct vtn_builder
*b
, SpvOp opcode
,
557 const uint32_t *w
, unsigned count
)
559 const uint32_t *w_end
= w
+ count
;
560 const uint32_t target
= w
[1];
564 case SpvOpDecorationGroup
:
565 vtn_push_value(b
, target
, vtn_value_type_decoration_group
);
569 case SpvOpDecorateId
:
570 case SpvOpMemberDecorate
:
571 case SpvOpDecorateString
:
572 case SpvOpMemberDecorateString
:
573 case SpvOpExecutionMode
:
574 case SpvOpExecutionModeId
: {
575 struct vtn_value
*val
= vtn_untyped_value(b
, target
);
577 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
580 case SpvOpDecorateId
:
581 case SpvOpDecorateString
:
582 dec
->scope
= VTN_DEC_DECORATION
;
584 case SpvOpMemberDecorate
:
585 case SpvOpMemberDecorateString
:
586 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(w
++);
587 vtn_fail_if(dec
->scope
< VTN_DEC_STRUCT_MEMBER0
, /* overflow */
588 "Member argument of OpMemberDecorate too large");
590 case SpvOpExecutionMode
:
591 case SpvOpExecutionModeId
:
592 dec
->scope
= VTN_DEC_EXECUTION_MODE
;
595 unreachable("Invalid decoration opcode");
597 dec
->decoration
= *(w
++);
600 /* Link into the list */
601 dec
->next
= val
->decoration
;
602 val
->decoration
= dec
;
606 case SpvOpGroupMemberDecorate
:
607 case SpvOpGroupDecorate
: {
608 struct vtn_value
*group
=
609 vtn_value(b
, target
, vtn_value_type_decoration_group
);
611 for (; w
< w_end
; w
++) {
612 struct vtn_value
*val
= vtn_untyped_value(b
, *w
);
613 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
616 if (opcode
== SpvOpGroupDecorate
) {
617 dec
->scope
= VTN_DEC_DECORATION
;
619 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(++w
);
620 vtn_fail_if(dec
->scope
< 0, /* Check for overflow */
621 "Member argument of OpGroupMemberDecorate too large");
624 /* Link into the list */
625 dec
->next
= val
->decoration
;
626 val
->decoration
= dec
;
632 unreachable("Unhandled opcode");
636 struct member_decoration_ctx
{
638 struct glsl_struct_field
*fields
;
639 struct vtn_type
*type
;
643 * Returns true if the given type contains a struct decorated Block or
647 vtn_type_contains_block(struct vtn_builder
*b
, struct vtn_type
*type
)
649 switch (type
->base_type
) {
650 case vtn_base_type_array
:
651 return vtn_type_contains_block(b
, type
->array_element
);
652 case vtn_base_type_struct
:
653 if (type
->block
|| type
->buffer_block
)
655 for (unsigned i
= 0; i
< type
->length
; i
++) {
656 if (vtn_type_contains_block(b
, type
->members
[i
]))
665 /** Returns true if two types are "compatible", i.e. you can do an OpLoad,
666 * OpStore, or OpCopyMemory between them without breaking anything.
667 * Technically, the SPIR-V rules require the exact same type ID but this lets
668 * us internally be a bit looser.
671 vtn_types_compatible(struct vtn_builder
*b
,
672 struct vtn_type
*t1
, struct vtn_type
*t2
)
674 if (t1
->id
== t2
->id
)
677 if (t1
->base_type
!= t2
->base_type
)
680 switch (t1
->base_type
) {
681 case vtn_base_type_void
:
682 case vtn_base_type_scalar
:
683 case vtn_base_type_vector
:
684 case vtn_base_type_matrix
:
685 case vtn_base_type_image
:
686 case vtn_base_type_sampler
:
687 case vtn_base_type_sampled_image
:
688 return t1
->type
== t2
->type
;
690 case vtn_base_type_array
:
691 return t1
->length
== t2
->length
&&
692 vtn_types_compatible(b
, t1
->array_element
, t2
->array_element
);
694 case vtn_base_type_pointer
:
695 return vtn_types_compatible(b
, t1
->deref
, t2
->deref
);
697 case vtn_base_type_struct
:
698 if (t1
->length
!= t2
->length
)
701 for (unsigned i
= 0; i
< t1
->length
; i
++) {
702 if (!vtn_types_compatible(b
, t1
->members
[i
], t2
->members
[i
]))
707 case vtn_base_type_function
:
708 /* This case shouldn't get hit since you can't copy around function
709 * types. Just require them to be identical.
714 vtn_fail("Invalid base type");
718 vtn_type_without_array(struct vtn_type
*type
)
720 while (type
->base_type
== vtn_base_type_array
)
721 type
= type
->array_element
;
725 /* does a shallow copy of a vtn_type */
727 static struct vtn_type
*
728 vtn_type_copy(struct vtn_builder
*b
, struct vtn_type
*src
)
730 struct vtn_type
*dest
= ralloc(b
, struct vtn_type
);
733 switch (src
->base_type
) {
734 case vtn_base_type_void
:
735 case vtn_base_type_scalar
:
736 case vtn_base_type_vector
:
737 case vtn_base_type_matrix
:
738 case vtn_base_type_array
:
739 case vtn_base_type_pointer
:
740 case vtn_base_type_image
:
741 case vtn_base_type_sampler
:
742 case vtn_base_type_sampled_image
:
743 /* Nothing more to do */
746 case vtn_base_type_struct
:
747 dest
->members
= ralloc_array(b
, struct vtn_type
*, src
->length
);
748 memcpy(dest
->members
, src
->members
,
749 src
->length
* sizeof(src
->members
[0]));
751 dest
->offsets
= ralloc_array(b
, unsigned, src
->length
);
752 memcpy(dest
->offsets
, src
->offsets
,
753 src
->length
* sizeof(src
->offsets
[0]));
756 case vtn_base_type_function
:
757 dest
->params
= ralloc_array(b
, struct vtn_type
*, src
->length
);
758 memcpy(dest
->params
, src
->params
, src
->length
* sizeof(src
->params
[0]));
765 static const struct glsl_type
*
766 wrap_type_in_array(const struct glsl_type
*type
,
767 const struct glsl_type
*array_type
)
769 if (!glsl_type_is_array(array_type
))
772 const struct glsl_type
*elem_type
=
773 wrap_type_in_array(type
, glsl_get_array_element(array_type
));
774 return glsl_array_type(elem_type
, glsl_get_length(array_type
),
775 glsl_get_explicit_stride(array_type
));
778 const struct glsl_type
*
779 vtn_type_get_nir_type(struct vtn_builder
*b
, struct vtn_type
*type
,
780 enum vtn_variable_mode mode
)
782 if (mode
== vtn_variable_mode_atomic_counter
) {
783 vtn_fail_if(glsl_without_array(type
->type
) != glsl_uint_type(),
784 "Variables in the AtomicCounter storage class should be "
785 "(possibly arrays of arrays of) uint.");
786 return wrap_type_in_array(glsl_atomic_uint_type(), type
->type
);
789 if (mode
== vtn_variable_mode_uniform
) {
790 struct vtn_type
*tail
= vtn_type_without_array(type
);
791 if (tail
->base_type
== vtn_base_type_image
) {
792 return wrap_type_in_array(tail
->glsl_image
, type
->type
);
793 } else if (tail
->base_type
== vtn_base_type_sampler
) {
794 return wrap_type_in_array(glsl_bare_sampler_type(), type
->type
);
795 } else if (tail
->base_type
== vtn_base_type_sampled_image
) {
796 return wrap_type_in_array(tail
->image
->glsl_image
, type
->type
);
803 static struct vtn_type
*
804 mutable_matrix_member(struct vtn_builder
*b
, struct vtn_type
*type
, int member
)
806 type
->members
[member
] = vtn_type_copy(b
, type
->members
[member
]);
807 type
= type
->members
[member
];
809 /* We may have an array of matrices.... Oh, joy! */
810 while (glsl_type_is_array(type
->type
)) {
811 type
->array_element
= vtn_type_copy(b
, type
->array_element
);
812 type
= type
->array_element
;
815 vtn_assert(glsl_type_is_matrix(type
->type
));
821 vtn_handle_access_qualifier(struct vtn_builder
*b
, struct vtn_type
*type
,
822 int member
, enum gl_access_qualifier access
)
824 type
->members
[member
] = vtn_type_copy(b
, type
->members
[member
]);
825 type
= type
->members
[member
];
827 type
->access
|= access
;
831 array_stride_decoration_cb(struct vtn_builder
*b
,
832 struct vtn_value
*val
, int member
,
833 const struct vtn_decoration
*dec
, void *void_ctx
)
835 struct vtn_type
*type
= val
->type
;
837 if (dec
->decoration
== SpvDecorationArrayStride
) {
838 if (vtn_type_contains_block(b
, type
)) {
839 vtn_warn("The ArrayStride decoration cannot be applied to an array "
840 "type which contains a structure type decorated Block "
842 /* Ignore the decoration */
844 vtn_fail_if(dec
->operands
[0] == 0, "ArrayStride must be non-zero");
845 type
->stride
= dec
->operands
[0];
851 struct_member_decoration_cb(struct vtn_builder
*b
,
852 UNUSED
struct vtn_value
*val
, int member
,
853 const struct vtn_decoration
*dec
, void *void_ctx
)
855 struct member_decoration_ctx
*ctx
= void_ctx
;
860 assert(member
< ctx
->num_fields
);
862 switch (dec
->decoration
) {
863 case SpvDecorationRelaxedPrecision
:
864 case SpvDecorationUniform
:
865 case SpvDecorationUniformId
:
866 break; /* FIXME: Do nothing with this for now. */
867 case SpvDecorationNonWritable
:
868 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_NON_WRITEABLE
);
870 case SpvDecorationNonReadable
:
871 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_NON_READABLE
);
873 case SpvDecorationVolatile
:
874 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_VOLATILE
);
876 case SpvDecorationCoherent
:
877 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_COHERENT
);
879 case SpvDecorationNoPerspective
:
880 ctx
->fields
[member
].interpolation
= INTERP_MODE_NOPERSPECTIVE
;
882 case SpvDecorationFlat
:
883 ctx
->fields
[member
].interpolation
= INTERP_MODE_FLAT
;
885 case SpvDecorationExplicitInterpAMD
:
886 ctx
->fields
[member
].interpolation
= INTERP_MODE_EXPLICIT
;
888 case SpvDecorationCentroid
:
889 ctx
->fields
[member
].centroid
= true;
891 case SpvDecorationSample
:
892 ctx
->fields
[member
].sample
= true;
894 case SpvDecorationStream
:
895 /* This is handled later by var_decoration_cb in vtn_variables.c */
897 case SpvDecorationLocation
:
898 ctx
->fields
[member
].location
= dec
->operands
[0];
900 case SpvDecorationComponent
:
901 break; /* FIXME: What should we do with these? */
902 case SpvDecorationBuiltIn
:
903 ctx
->type
->members
[member
] = vtn_type_copy(b
, ctx
->type
->members
[member
]);
904 ctx
->type
->members
[member
]->is_builtin
= true;
905 ctx
->type
->members
[member
]->builtin
= dec
->operands
[0];
906 ctx
->type
->builtin_block
= true;
908 case SpvDecorationOffset
:
909 ctx
->type
->offsets
[member
] = dec
->operands
[0];
910 ctx
->fields
[member
].offset
= dec
->operands
[0];
912 case SpvDecorationMatrixStride
:
913 /* Handled as a second pass */
915 case SpvDecorationColMajor
:
916 break; /* Nothing to do here. Column-major is the default. */
917 case SpvDecorationRowMajor
:
918 mutable_matrix_member(b
, ctx
->type
, member
)->row_major
= true;
921 case SpvDecorationPatch
:
924 case SpvDecorationSpecId
:
925 case SpvDecorationBlock
:
926 case SpvDecorationBufferBlock
:
927 case SpvDecorationArrayStride
:
928 case SpvDecorationGLSLShared
:
929 case SpvDecorationGLSLPacked
:
930 case SpvDecorationInvariant
:
931 case SpvDecorationRestrict
:
932 case SpvDecorationAliased
:
933 case SpvDecorationConstant
:
934 case SpvDecorationIndex
:
935 case SpvDecorationBinding
:
936 case SpvDecorationDescriptorSet
:
937 case SpvDecorationLinkageAttributes
:
938 case SpvDecorationNoContraction
:
939 case SpvDecorationInputAttachmentIndex
:
940 vtn_warn("Decoration not allowed on struct members: %s",
941 spirv_decoration_to_string(dec
->decoration
));
944 case SpvDecorationXfbBuffer
:
945 case SpvDecorationXfbStride
:
946 /* This is handled later by var_decoration_cb in vtn_variables.c */
949 case SpvDecorationCPacked
:
950 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
)
951 vtn_warn("Decoration only allowed for CL-style kernels: %s",
952 spirv_decoration_to_string(dec
->decoration
));
954 ctx
->type
->packed
= true;
957 case SpvDecorationSaturatedConversion
:
958 case SpvDecorationFuncParamAttr
:
959 case SpvDecorationFPRoundingMode
:
960 case SpvDecorationFPFastMathMode
:
961 case SpvDecorationAlignment
:
962 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
) {
963 vtn_warn("Decoration only allowed for CL-style kernels: %s",
964 spirv_decoration_to_string(dec
->decoration
));
968 case SpvDecorationUserSemantic
:
969 case SpvDecorationUserTypeGOOGLE
:
970 /* User semantic decorations can safely be ignored by the driver. */
974 vtn_fail_with_decoration("Unhandled decoration", dec
->decoration
);
978 /** Chases the array type all the way down to the tail and rewrites the
979 * glsl_types to be based off the tail's glsl_type.
982 vtn_array_type_rewrite_glsl_type(struct vtn_type
*type
)
984 if (type
->base_type
!= vtn_base_type_array
)
987 vtn_array_type_rewrite_glsl_type(type
->array_element
);
989 type
->type
= glsl_array_type(type
->array_element
->type
,
990 type
->length
, type
->stride
);
993 /* Matrix strides are handled as a separate pass because we need to know
994 * whether the matrix is row-major or not first.
997 struct_member_matrix_stride_cb(struct vtn_builder
*b
,
998 UNUSED
struct vtn_value
*val
, int member
,
999 const struct vtn_decoration
*dec
,
1002 if (dec
->decoration
!= SpvDecorationMatrixStride
)
1005 vtn_fail_if(member
< 0,
1006 "The MatrixStride decoration is only allowed on members "
1008 vtn_fail_if(dec
->operands
[0] == 0, "MatrixStride must be non-zero");
1010 struct member_decoration_ctx
*ctx
= void_ctx
;
1012 struct vtn_type
*mat_type
= mutable_matrix_member(b
, ctx
->type
, member
);
1013 if (mat_type
->row_major
) {
1014 mat_type
->array_element
= vtn_type_copy(b
, mat_type
->array_element
);
1015 mat_type
->stride
= mat_type
->array_element
->stride
;
1016 mat_type
->array_element
->stride
= dec
->operands
[0];
1018 mat_type
->type
= glsl_explicit_matrix_type(mat_type
->type
,
1019 dec
->operands
[0], true);
1020 mat_type
->array_element
->type
= glsl_get_column_type(mat_type
->type
);
1022 vtn_assert(mat_type
->array_element
->stride
> 0);
1023 mat_type
->stride
= dec
->operands
[0];
1025 mat_type
->type
= glsl_explicit_matrix_type(mat_type
->type
,
1026 dec
->operands
[0], false);
1029 /* Now that we've replaced the glsl_type with a properly strided matrix
1030 * type, rewrite the member type so that it's an array of the proper kind
1033 vtn_array_type_rewrite_glsl_type(ctx
->type
->members
[member
]);
1034 ctx
->fields
[member
].type
= ctx
->type
->members
[member
]->type
;
1038 struct_block_decoration_cb(struct vtn_builder
*b
,
1039 struct vtn_value
*val
, int member
,
1040 const struct vtn_decoration
*dec
, void *ctx
)
1045 struct vtn_type
*type
= val
->type
;
1046 if (dec
->decoration
== SpvDecorationBlock
)
1048 else if (dec
->decoration
== SpvDecorationBufferBlock
)
1049 type
->buffer_block
= true;
1053 type_decoration_cb(struct vtn_builder
*b
,
1054 struct vtn_value
*val
, int member
,
1055 const struct vtn_decoration
*dec
, UNUSED
void *ctx
)
1057 struct vtn_type
*type
= val
->type
;
1060 /* This should have been handled by OpTypeStruct */
1061 assert(val
->type
->base_type
== vtn_base_type_struct
);
1062 assert(member
>= 0 && member
< val
->type
->length
);
1066 switch (dec
->decoration
) {
1067 case SpvDecorationArrayStride
:
1068 vtn_assert(type
->base_type
== vtn_base_type_array
||
1069 type
->base_type
== vtn_base_type_pointer
);
1071 case SpvDecorationBlock
:
1072 vtn_assert(type
->base_type
== vtn_base_type_struct
);
1073 vtn_assert(type
->block
);
1075 case SpvDecorationBufferBlock
:
1076 vtn_assert(type
->base_type
== vtn_base_type_struct
);
1077 vtn_assert(type
->buffer_block
);
1079 case SpvDecorationGLSLShared
:
1080 case SpvDecorationGLSLPacked
:
1081 /* Ignore these, since we get explicit offsets anyways */
1084 case SpvDecorationRowMajor
:
1085 case SpvDecorationColMajor
:
1086 case SpvDecorationMatrixStride
:
1087 case SpvDecorationBuiltIn
:
1088 case SpvDecorationNoPerspective
:
1089 case SpvDecorationFlat
:
1090 case SpvDecorationPatch
:
1091 case SpvDecorationCentroid
:
1092 case SpvDecorationSample
:
1093 case SpvDecorationExplicitInterpAMD
:
1094 case SpvDecorationVolatile
:
1095 case SpvDecorationCoherent
:
1096 case SpvDecorationNonWritable
:
1097 case SpvDecorationNonReadable
:
1098 case SpvDecorationUniform
:
1099 case SpvDecorationUniformId
:
1100 case SpvDecorationLocation
:
1101 case SpvDecorationComponent
:
1102 case SpvDecorationOffset
:
1103 case SpvDecorationXfbBuffer
:
1104 case SpvDecorationXfbStride
:
1105 case SpvDecorationUserSemantic
:
1106 vtn_warn("Decoration only allowed for struct members: %s",
1107 spirv_decoration_to_string(dec
->decoration
));
1110 case SpvDecorationStream
:
1111 /* We don't need to do anything here, as stream is filled up when
1112 * aplying the decoration to a variable, just check that if it is not a
1113 * struct member, it should be a struct.
1115 vtn_assert(type
->base_type
== vtn_base_type_struct
);
1118 case SpvDecorationRelaxedPrecision
:
1119 case SpvDecorationSpecId
:
1120 case SpvDecorationInvariant
:
1121 case SpvDecorationRestrict
:
1122 case SpvDecorationAliased
:
1123 case SpvDecorationConstant
:
1124 case SpvDecorationIndex
:
1125 case SpvDecorationBinding
:
1126 case SpvDecorationDescriptorSet
:
1127 case SpvDecorationLinkageAttributes
:
1128 case SpvDecorationNoContraction
:
1129 case SpvDecorationInputAttachmentIndex
:
1130 vtn_warn("Decoration not allowed on types: %s",
1131 spirv_decoration_to_string(dec
->decoration
));
1134 case SpvDecorationCPacked
:
1135 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
)
1136 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1137 spirv_decoration_to_string(dec
->decoration
));
1139 type
->packed
= true;
1142 case SpvDecorationSaturatedConversion
:
1143 case SpvDecorationFuncParamAttr
:
1144 case SpvDecorationFPRoundingMode
:
1145 case SpvDecorationFPFastMathMode
:
1146 case SpvDecorationAlignment
:
1147 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1148 spirv_decoration_to_string(dec
->decoration
));
1151 case SpvDecorationUserTypeGOOGLE
:
1152 /* User semantic decorations can safely be ignored by the driver. */
1156 vtn_fail_with_decoration("Unhandled decoration", dec
->decoration
);
1161 translate_image_format(struct vtn_builder
*b
, SpvImageFormat format
)
1164 case SpvImageFormatUnknown
: return PIPE_FORMAT_NONE
;
1165 case SpvImageFormatRgba32f
: return PIPE_FORMAT_R32G32B32A32_FLOAT
;
1166 case SpvImageFormatRgba16f
: return PIPE_FORMAT_R16G16B16A16_FLOAT
;
1167 case SpvImageFormatR32f
: return PIPE_FORMAT_R32_FLOAT
;
1168 case SpvImageFormatRgba8
: return PIPE_FORMAT_R8G8B8A8_UNORM
;
1169 case SpvImageFormatRgba8Snorm
: return PIPE_FORMAT_R8G8B8A8_SNORM
;
1170 case SpvImageFormatRg32f
: return PIPE_FORMAT_R32G32_FLOAT
;
1171 case SpvImageFormatRg16f
: return PIPE_FORMAT_R16G16_FLOAT
;
1172 case SpvImageFormatR11fG11fB10f
: return PIPE_FORMAT_R11G11B10_FLOAT
;
1173 case SpvImageFormatR16f
: return PIPE_FORMAT_R16_FLOAT
;
1174 case SpvImageFormatRgba16
: return PIPE_FORMAT_R16G16B16A16_UNORM
;
1175 case SpvImageFormatRgb10A2
: return PIPE_FORMAT_R10G10B10A2_UNORM
;
1176 case SpvImageFormatRg16
: return PIPE_FORMAT_R16G16_UNORM
;
1177 case SpvImageFormatRg8
: return PIPE_FORMAT_R8G8_UNORM
;
1178 case SpvImageFormatR16
: return PIPE_FORMAT_R16_UNORM
;
1179 case SpvImageFormatR8
: return PIPE_FORMAT_R8_UNORM
;
1180 case SpvImageFormatRgba16Snorm
: return PIPE_FORMAT_R16G16B16A16_SNORM
;
1181 case SpvImageFormatRg16Snorm
: return PIPE_FORMAT_R16G16_SNORM
;
1182 case SpvImageFormatRg8Snorm
: return PIPE_FORMAT_R8G8_SNORM
;
1183 case SpvImageFormatR16Snorm
: return PIPE_FORMAT_R16_SNORM
;
1184 case SpvImageFormatR8Snorm
: return PIPE_FORMAT_R8_SNORM
;
1185 case SpvImageFormatRgba32i
: return PIPE_FORMAT_R32G32B32A32_SINT
;
1186 case SpvImageFormatRgba16i
: return PIPE_FORMAT_R16G16B16A16_SINT
;
1187 case SpvImageFormatRgba8i
: return PIPE_FORMAT_R8G8B8A8_SINT
;
1188 case SpvImageFormatR32i
: return PIPE_FORMAT_R32_SINT
;
1189 case SpvImageFormatRg32i
: return PIPE_FORMAT_R32G32_SINT
;
1190 case SpvImageFormatRg16i
: return PIPE_FORMAT_R16G16_SINT
;
1191 case SpvImageFormatRg8i
: return PIPE_FORMAT_R8G8_SINT
;
1192 case SpvImageFormatR16i
: return PIPE_FORMAT_R16_SINT
;
1193 case SpvImageFormatR8i
: return PIPE_FORMAT_R8_SINT
;
1194 case SpvImageFormatRgba32ui
: return PIPE_FORMAT_R32G32B32A32_UINT
;
1195 case SpvImageFormatRgba16ui
: return PIPE_FORMAT_R16G16B16A16_UINT
;
1196 case SpvImageFormatRgba8ui
: return PIPE_FORMAT_R8G8B8A8_UINT
;
1197 case SpvImageFormatR32ui
: return PIPE_FORMAT_R32_UINT
;
1198 case SpvImageFormatRgb10a2ui
: return PIPE_FORMAT_R10G10B10A2_UINT
;
1199 case SpvImageFormatRg32ui
: return PIPE_FORMAT_R32G32_UINT
;
1200 case SpvImageFormatRg16ui
: return PIPE_FORMAT_R16G16_UINT
;
1201 case SpvImageFormatRg8ui
: return PIPE_FORMAT_R8G8_UINT
;
1202 case SpvImageFormatR16ui
: return PIPE_FORMAT_R16_UINT
;
1203 case SpvImageFormatR8ui
: return PIPE_FORMAT_R8_UINT
;
1205 vtn_fail("Invalid image format: %s (%u)",
1206 spirv_imageformat_to_string(format
), format
);
1211 vtn_handle_type(struct vtn_builder
*b
, SpvOp opcode
,
1212 const uint32_t *w
, unsigned count
)
1214 struct vtn_value
*val
= NULL
;
1216 /* In order to properly handle forward declarations, we have to defer
1217 * allocation for pointer types.
1219 if (opcode
!= SpvOpTypePointer
&& opcode
!= SpvOpTypeForwardPointer
) {
1220 val
= vtn_push_value(b
, w
[1], vtn_value_type_type
);
1221 vtn_fail_if(val
->type
!= NULL
,
1222 "Only pointers can have forward declarations");
1223 val
->type
= rzalloc(b
, struct vtn_type
);
1224 val
->type
->id
= w
[1];
1229 val
->type
->base_type
= vtn_base_type_void
;
1230 val
->type
->type
= glsl_void_type();
1233 val
->type
->base_type
= vtn_base_type_scalar
;
1234 val
->type
->type
= glsl_bool_type();
1235 val
->type
->length
= 1;
1237 case SpvOpTypeInt
: {
1238 int bit_size
= w
[2];
1239 const bool signedness
= w
[3];
1240 val
->type
->base_type
= vtn_base_type_scalar
;
1243 val
->type
->type
= (signedness
? glsl_int64_t_type() : glsl_uint64_t_type());
1246 val
->type
->type
= (signedness
? glsl_int_type() : glsl_uint_type());
1249 val
->type
->type
= (signedness
? glsl_int16_t_type() : glsl_uint16_t_type());
1252 val
->type
->type
= (signedness
? glsl_int8_t_type() : glsl_uint8_t_type());
1255 vtn_fail("Invalid int bit size: %u", bit_size
);
1257 val
->type
->length
= 1;
1261 case SpvOpTypeFloat
: {
1262 int bit_size
= w
[2];
1263 val
->type
->base_type
= vtn_base_type_scalar
;
1266 val
->type
->type
= glsl_float16_t_type();
1269 val
->type
->type
= glsl_float_type();
1272 val
->type
->type
= glsl_double_type();
1275 vtn_fail("Invalid float bit size: %u", bit_size
);
1277 val
->type
->length
= 1;
1281 case SpvOpTypeVector
: {
1282 struct vtn_type
*base
= vtn_get_type(b
, w
[2]);
1283 unsigned elems
= w
[3];
1285 vtn_fail_if(base
->base_type
!= vtn_base_type_scalar
,
1286 "Base type for OpTypeVector must be a scalar");
1287 vtn_fail_if((elems
< 2 || elems
> 4) && (elems
!= 8) && (elems
!= 16),
1288 "Invalid component count for OpTypeVector");
1290 val
->type
->base_type
= vtn_base_type_vector
;
1291 val
->type
->type
= glsl_vector_type(glsl_get_base_type(base
->type
), elems
);
1292 val
->type
->length
= elems
;
1293 val
->type
->stride
= glsl_type_is_boolean(val
->type
->type
)
1294 ? 4 : glsl_get_bit_size(base
->type
) / 8;
1295 val
->type
->array_element
= base
;
1299 case SpvOpTypeMatrix
: {
1300 struct vtn_type
*base
= vtn_get_type(b
, w
[2]);
1301 unsigned columns
= w
[3];
1303 vtn_fail_if(base
->base_type
!= vtn_base_type_vector
,
1304 "Base type for OpTypeMatrix must be a vector");
1305 vtn_fail_if(columns
< 2 || columns
> 4,
1306 "Invalid column count for OpTypeMatrix");
1308 val
->type
->base_type
= vtn_base_type_matrix
;
1309 val
->type
->type
= glsl_matrix_type(glsl_get_base_type(base
->type
),
1310 glsl_get_vector_elements(base
->type
),
1312 vtn_fail_if(glsl_type_is_error(val
->type
->type
),
1313 "Unsupported base type for OpTypeMatrix");
1314 assert(!glsl_type_is_error(val
->type
->type
));
1315 val
->type
->length
= columns
;
1316 val
->type
->array_element
= base
;
1317 val
->type
->row_major
= false;
1318 val
->type
->stride
= 0;
1322 case SpvOpTypeRuntimeArray
:
1323 case SpvOpTypeArray
: {
1324 struct vtn_type
*array_element
= vtn_get_type(b
, w
[2]);
1326 if (opcode
== SpvOpTypeRuntimeArray
) {
1327 /* A length of 0 is used to denote unsized arrays */
1328 val
->type
->length
= 0;
1330 val
->type
->length
= vtn_constant_uint(b
, w
[3]);
1333 val
->type
->base_type
= vtn_base_type_array
;
1334 val
->type
->array_element
= array_element
;
1335 if (b
->shader
->info
.stage
== MESA_SHADER_KERNEL
)
1336 val
->type
->stride
= glsl_get_cl_size(array_element
->type
);
1338 vtn_foreach_decoration(b
, val
, array_stride_decoration_cb
, NULL
);
1339 val
->type
->type
= glsl_array_type(array_element
->type
, val
->type
->length
,
1344 case SpvOpTypeStruct
: {
1345 unsigned num_fields
= count
- 2;
1346 val
->type
->base_type
= vtn_base_type_struct
;
1347 val
->type
->length
= num_fields
;
1348 val
->type
->members
= ralloc_array(b
, struct vtn_type
*, num_fields
);
1349 val
->type
->offsets
= ralloc_array(b
, unsigned, num_fields
);
1350 val
->type
->packed
= false;
1352 NIR_VLA(struct glsl_struct_field
, fields
, count
);
1353 for (unsigned i
= 0; i
< num_fields
; i
++) {
1354 val
->type
->members
[i
] = vtn_get_type(b
, w
[i
+ 2]);
1355 fields
[i
] = (struct glsl_struct_field
) {
1356 .type
= val
->type
->members
[i
]->type
,
1357 .name
= ralloc_asprintf(b
, "field%d", i
),
1363 if (b
->shader
->info
.stage
== MESA_SHADER_KERNEL
) {
1364 unsigned offset
= 0;
1365 for (unsigned i
= 0; i
< num_fields
; i
++) {
1366 offset
= align(offset
, glsl_get_cl_alignment(fields
[i
].type
));
1367 fields
[i
].offset
= offset
;
1368 offset
+= glsl_get_cl_size(fields
[i
].type
);
1372 struct member_decoration_ctx ctx
= {
1373 .num_fields
= num_fields
,
1378 vtn_foreach_decoration(b
, val
, struct_member_decoration_cb
, &ctx
);
1379 vtn_foreach_decoration(b
, val
, struct_member_matrix_stride_cb
, &ctx
);
1381 vtn_foreach_decoration(b
, val
, struct_block_decoration_cb
, NULL
);
1383 const char *name
= val
->name
;
1385 if (val
->type
->block
|| val
->type
->buffer_block
) {
1386 /* Packing will be ignored since types coming from SPIR-V are
1387 * explicitly laid out.
1389 val
->type
->type
= glsl_interface_type(fields
, num_fields
,
1390 /* packing */ 0, false,
1391 name
? name
: "block");
1393 val
->type
->type
= glsl_struct_type(fields
, num_fields
,
1394 name
? name
: "struct", false);
1399 case SpvOpTypeFunction
: {
1400 val
->type
->base_type
= vtn_base_type_function
;
1401 val
->type
->type
= NULL
;
1403 val
->type
->return_type
= vtn_get_type(b
, w
[2]);
1405 const unsigned num_params
= count
- 3;
1406 val
->type
->length
= num_params
;
1407 val
->type
->params
= ralloc_array(b
, struct vtn_type
*, num_params
);
1408 for (unsigned i
= 0; i
< count
- 3; i
++) {
1409 val
->type
->params
[i
] = vtn_get_type(b
, w
[i
+ 3]);
1414 case SpvOpTypePointer
:
1415 case SpvOpTypeForwardPointer
: {
1416 /* We can't blindly push the value because it might be a forward
1419 val
= vtn_untyped_value(b
, w
[1]);
1421 SpvStorageClass storage_class
= w
[2];
1423 if (val
->value_type
== vtn_value_type_invalid
) {
1424 val
->value_type
= vtn_value_type_type
;
1425 val
->type
= rzalloc(b
, struct vtn_type
);
1426 val
->type
->id
= w
[1];
1427 val
->type
->base_type
= vtn_base_type_pointer
;
1428 val
->type
->storage_class
= storage_class
;
1430 /* These can actually be stored to nir_variables and used as SSA
1431 * values so they need a real glsl_type.
1433 enum vtn_variable_mode mode
= vtn_storage_class_to_mode(
1434 b
, storage_class
, NULL
, NULL
);
1435 val
->type
->type
= nir_address_format_to_glsl_type(
1436 vtn_mode_to_address_format(b
, mode
));
1438 vtn_fail_if(val
->type
->storage_class
!= storage_class
,
1439 "The storage classes of an OpTypePointer and any "
1440 "OpTypeForwardPointers that provide forward "
1441 "declarations of it must match.");
1444 if (opcode
== SpvOpTypePointer
) {
1445 vtn_fail_if(val
->type
->deref
!= NULL
,
1446 "While OpTypeForwardPointer can be used to provide a "
1447 "forward declaration of a pointer, OpTypePointer can "
1448 "only be used once for a given id.");
1450 val
->type
->deref
= vtn_get_type(b
, w
[3]);
1452 /* Only certain storage classes use ArrayStride. The others (in
1453 * particular Workgroup) are expected to be laid out by the driver.
1455 switch (storage_class
) {
1456 case SpvStorageClassUniform
:
1457 case SpvStorageClassPushConstant
:
1458 case SpvStorageClassStorageBuffer
:
1459 case SpvStorageClassPhysicalStorageBuffer
:
1460 vtn_foreach_decoration(b
, val
, array_stride_decoration_cb
, NULL
);
1463 /* Nothing to do. */
1467 if (b
->physical_ptrs
) {
1468 switch (storage_class
) {
1469 case SpvStorageClassFunction
:
1470 case SpvStorageClassWorkgroup
:
1471 case SpvStorageClassCrossWorkgroup
:
1472 case SpvStorageClassUniformConstant
:
1473 val
->type
->stride
= align(glsl_get_cl_size(val
->type
->deref
->type
),
1474 glsl_get_cl_alignment(val
->type
->deref
->type
));
1484 case SpvOpTypeImage
: {
1485 val
->type
->base_type
= vtn_base_type_image
;
1487 /* Images are represented in NIR as a scalar SSA value that is the
1488 * result of a deref instruction. An OpLoad on an OpTypeImage pointer
1489 * from UniformConstant memory just takes the NIR deref from the pointer
1490 * and turns it into an SSA value.
1492 val
->type
->type
= nir_address_format_to_glsl_type(
1493 vtn_mode_to_address_format(b
, vtn_variable_mode_function
));
1495 const struct vtn_type
*sampled_type
= vtn_get_type(b
, w
[2]);
1496 vtn_fail_if(sampled_type
->base_type
!= vtn_base_type_scalar
||
1497 glsl_get_bit_size(sampled_type
->type
) != 32,
1498 "Sampled type of OpTypeImage must be a 32-bit scalar");
1500 enum glsl_sampler_dim dim
;
1501 switch ((SpvDim
)w
[3]) {
1502 case SpvDim1D
: dim
= GLSL_SAMPLER_DIM_1D
; break;
1503 case SpvDim2D
: dim
= GLSL_SAMPLER_DIM_2D
; break;
1504 case SpvDim3D
: dim
= GLSL_SAMPLER_DIM_3D
; break;
1505 case SpvDimCube
: dim
= GLSL_SAMPLER_DIM_CUBE
; break;
1506 case SpvDimRect
: dim
= GLSL_SAMPLER_DIM_RECT
; break;
1507 case SpvDimBuffer
: dim
= GLSL_SAMPLER_DIM_BUF
; break;
1508 case SpvDimSubpassData
: dim
= GLSL_SAMPLER_DIM_SUBPASS
; break;
1510 vtn_fail("Invalid SPIR-V image dimensionality: %s (%u)",
1511 spirv_dim_to_string((SpvDim
)w
[3]), w
[3]);
1514 /* w[4]: as per Vulkan spec "Validation Rules within a Module",
1515 * The “Depth” operand of OpTypeImage is ignored.
1517 bool is_array
= w
[5];
1518 bool multisampled
= w
[6];
1519 unsigned sampled
= w
[7];
1520 SpvImageFormat format
= w
[8];
1523 val
->type
->access_qualifier
= w
[9];
1525 val
->type
->access_qualifier
= SpvAccessQualifierReadWrite
;
1528 if (dim
== GLSL_SAMPLER_DIM_2D
)
1529 dim
= GLSL_SAMPLER_DIM_MS
;
1530 else if (dim
== GLSL_SAMPLER_DIM_SUBPASS
)
1531 dim
= GLSL_SAMPLER_DIM_SUBPASS_MS
;
1533 vtn_fail("Unsupported multisampled image type");
1536 val
->type
->image_format
= translate_image_format(b
, format
);
1538 enum glsl_base_type sampled_base_type
=
1539 glsl_get_base_type(sampled_type
->type
);
1541 val
->type
->glsl_image
= glsl_sampler_type(dim
, false, is_array
,
1543 } else if (sampled
== 2) {
1544 val
->type
->glsl_image
= glsl_image_type(dim
, is_array
,
1547 vtn_fail("We need to know if the image will be sampled");
1552 case SpvOpTypeSampledImage
: {
1553 val
->type
->base_type
= vtn_base_type_sampled_image
;
1554 val
->type
->image
= vtn_get_type(b
, w
[2]);
1556 /* Sampled images are represented NIR as a vec2 SSA value where each
1557 * component is the result of a deref instruction. The first component
1558 * is the image and the second is the sampler. An OpLoad on an
1559 * OpTypeSampledImage pointer from UniformConstant memory just takes
1560 * the NIR deref from the pointer and duplicates it to both vector
1563 nir_address_format addr_format
=
1564 vtn_mode_to_address_format(b
, vtn_variable_mode_function
);
1565 assert(nir_address_format_num_components(addr_format
) == 1);
1566 unsigned bit_size
= nir_address_format_bit_size(addr_format
);
1567 assert(bit_size
== 32 || bit_size
== 64);
1569 enum glsl_base_type base_type
=
1570 bit_size
== 32 ? GLSL_TYPE_UINT
: GLSL_TYPE_UINT64
;
1571 val
->type
->type
= glsl_vector_type(base_type
, 2);
1575 case SpvOpTypeSampler
:
1576 val
->type
->base_type
= vtn_base_type_sampler
;
1578 /* Samplers are represented in NIR as a scalar SSA value that is the
1579 * result of a deref instruction. An OpLoad on an OpTypeSampler pointer
1580 * from UniformConstant memory just takes the NIR deref from the pointer
1581 * and turns it into an SSA value.
1583 val
->type
->type
= nir_address_format_to_glsl_type(
1584 vtn_mode_to_address_format(b
, vtn_variable_mode_function
));
1587 case SpvOpTypeOpaque
:
1588 case SpvOpTypeEvent
:
1589 case SpvOpTypeDeviceEvent
:
1590 case SpvOpTypeReserveId
:
1591 case SpvOpTypeQueue
:
1594 vtn_fail_with_opcode("Unhandled opcode", opcode
);
1597 vtn_foreach_decoration(b
, val
, type_decoration_cb
, NULL
);
1599 if (val
->type
->base_type
== vtn_base_type_struct
&&
1600 (val
->type
->block
|| val
->type
->buffer_block
)) {
1601 for (unsigned i
= 0; i
< val
->type
->length
; i
++) {
1602 vtn_fail_if(vtn_type_contains_block(b
, val
->type
->members
[i
]),
1603 "Block and BufferBlock decorations cannot decorate a "
1604 "structure type that is nested at any level inside "
1605 "another structure type decorated with Block or "
1611 static nir_constant
*
1612 vtn_null_constant(struct vtn_builder
*b
, struct vtn_type
*type
)
1614 nir_constant
*c
= rzalloc(b
, nir_constant
);
1616 switch (type
->base_type
) {
1617 case vtn_base_type_scalar
:
1618 case vtn_base_type_vector
:
1619 /* Nothing to do here. It's already initialized to zero */
1622 case vtn_base_type_pointer
: {
1623 enum vtn_variable_mode mode
= vtn_storage_class_to_mode(
1624 b
, type
->storage_class
, type
->deref
, NULL
);
1625 nir_address_format addr_format
= vtn_mode_to_address_format(b
, mode
);
1627 const nir_const_value
*null_value
= nir_address_format_null_value(addr_format
);
1628 memcpy(c
->values
, null_value
,
1629 sizeof(nir_const_value
) * nir_address_format_num_components(addr_format
));
1633 case vtn_base_type_void
:
1634 case vtn_base_type_image
:
1635 case vtn_base_type_sampler
:
1636 case vtn_base_type_sampled_image
:
1637 case vtn_base_type_function
:
1638 /* For those we have to return something but it doesn't matter what. */
1641 case vtn_base_type_matrix
:
1642 case vtn_base_type_array
:
1643 vtn_assert(type
->length
> 0);
1644 c
->num_elements
= type
->length
;
1645 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
1647 c
->elements
[0] = vtn_null_constant(b
, type
->array_element
);
1648 for (unsigned i
= 1; i
< c
->num_elements
; i
++)
1649 c
->elements
[i
] = c
->elements
[0];
1652 case vtn_base_type_struct
:
1653 c
->num_elements
= type
->length
;
1654 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
1655 for (unsigned i
= 0; i
< c
->num_elements
; i
++)
1656 c
->elements
[i
] = vtn_null_constant(b
, type
->members
[i
]);
1660 vtn_fail("Invalid type for null constant");
1667 spec_constant_decoration_cb(struct vtn_builder
*b
, UNUSED
struct vtn_value
*val
,
1668 ASSERTED
int member
,
1669 const struct vtn_decoration
*dec
, void *data
)
1671 vtn_assert(member
== -1);
1672 if (dec
->decoration
!= SpvDecorationSpecId
)
1675 nir_const_value
*value
= data
;
1676 for (unsigned i
= 0; i
< b
->num_specializations
; i
++) {
1677 if (b
->specializations
[i
].id
== dec
->operands
[0]) {
1678 *value
= b
->specializations
[i
].value
;
1685 handle_workgroup_size_decoration_cb(struct vtn_builder
*b
,
1686 struct vtn_value
*val
,
1687 ASSERTED
int member
,
1688 const struct vtn_decoration
*dec
,
1691 vtn_assert(member
== -1);
1692 if (dec
->decoration
!= SpvDecorationBuiltIn
||
1693 dec
->operands
[0] != SpvBuiltInWorkgroupSize
)
1696 vtn_assert(val
->type
->type
== glsl_vector_type(GLSL_TYPE_UINT
, 3));
1697 b
->workgroup_size_builtin
= val
;
1701 vtn_handle_constant(struct vtn_builder
*b
, SpvOp opcode
,
1702 const uint32_t *w
, unsigned count
)
1704 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_constant
);
1705 val
->constant
= rzalloc(b
, nir_constant
);
1707 case SpvOpConstantTrue
:
1708 case SpvOpConstantFalse
:
1709 case SpvOpSpecConstantTrue
:
1710 case SpvOpSpecConstantFalse
: {
1711 vtn_fail_if(val
->type
->type
!= glsl_bool_type(),
1712 "Result type of %s must be OpTypeBool",
1713 spirv_op_to_string(opcode
));
1715 bool bval
= (opcode
== SpvOpConstantTrue
||
1716 opcode
== SpvOpSpecConstantTrue
);
1718 nir_const_value u32val
= nir_const_value_for_uint(bval
, 32);
1720 if (opcode
== SpvOpSpecConstantTrue
||
1721 opcode
== SpvOpSpecConstantFalse
)
1722 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &u32val
);
1724 val
->constant
->values
[0].b
= u32val
.u32
!= 0;
1729 case SpvOpSpecConstant
: {
1730 vtn_fail_if(val
->type
->base_type
!= vtn_base_type_scalar
,
1731 "Result type of %s must be a scalar",
1732 spirv_op_to_string(opcode
));
1733 int bit_size
= glsl_get_bit_size(val
->type
->type
);
1736 val
->constant
->values
[0].u64
= vtn_u64_literal(&w
[3]);
1739 val
->constant
->values
[0].u32
= w
[3];
1742 val
->constant
->values
[0].u16
= w
[3];
1745 val
->constant
->values
[0].u8
= w
[3];
1748 vtn_fail("Unsupported SpvOpConstant bit size: %u", bit_size
);
1751 if (opcode
== SpvOpSpecConstant
)
1752 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
,
1753 &val
->constant
->values
[0]);
1757 case SpvOpSpecConstantComposite
:
1758 case SpvOpConstantComposite
: {
1759 unsigned elem_count
= count
- 3;
1760 vtn_fail_if(elem_count
!= val
->type
->length
,
1761 "%s has %u constituents, expected %u",
1762 spirv_op_to_string(opcode
), elem_count
, val
->type
->length
);
1764 nir_constant
**elems
= ralloc_array(b
, nir_constant
*, elem_count
);
1765 for (unsigned i
= 0; i
< elem_count
; i
++) {
1766 struct vtn_value
*val
= vtn_untyped_value(b
, w
[i
+ 3]);
1768 if (val
->value_type
== vtn_value_type_constant
) {
1769 elems
[i
] = val
->constant
;
1771 vtn_fail_if(val
->value_type
!= vtn_value_type_undef
,
1772 "only constants or undefs allowed for "
1773 "SpvOpConstantComposite");
1774 /* to make it easier, just insert a NULL constant for now */
1775 elems
[i
] = vtn_null_constant(b
, val
->type
);
1779 switch (val
->type
->base_type
) {
1780 case vtn_base_type_vector
: {
1781 assert(glsl_type_is_vector(val
->type
->type
));
1782 for (unsigned i
= 0; i
< elem_count
; i
++)
1783 val
->constant
->values
[i
] = elems
[i
]->values
[0];
1787 case vtn_base_type_matrix
:
1788 case vtn_base_type_struct
:
1789 case vtn_base_type_array
:
1790 ralloc_steal(val
->constant
, elems
);
1791 val
->constant
->num_elements
= elem_count
;
1792 val
->constant
->elements
= elems
;
1796 vtn_fail("Result type of %s must be a composite type",
1797 spirv_op_to_string(opcode
));
1802 case SpvOpSpecConstantOp
: {
1803 nir_const_value u32op
= nir_const_value_for_uint(w
[3], 32);
1804 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &u32op
);
1805 SpvOp opcode
= u32op
.u32
;
1807 case SpvOpVectorShuffle
: {
1808 struct vtn_value
*v0
= &b
->values
[w
[4]];
1809 struct vtn_value
*v1
= &b
->values
[w
[5]];
1811 vtn_assert(v0
->value_type
== vtn_value_type_constant
||
1812 v0
->value_type
== vtn_value_type_undef
);
1813 vtn_assert(v1
->value_type
== vtn_value_type_constant
||
1814 v1
->value_type
== vtn_value_type_undef
);
1816 unsigned len0
= glsl_get_vector_elements(v0
->type
->type
);
1817 unsigned len1
= glsl_get_vector_elements(v1
->type
->type
);
1819 vtn_assert(len0
+ len1
< 16);
1821 unsigned bit_size
= glsl_get_bit_size(val
->type
->type
);
1822 unsigned bit_size0
= glsl_get_bit_size(v0
->type
->type
);
1823 unsigned bit_size1
= glsl_get_bit_size(v1
->type
->type
);
1825 vtn_assert(bit_size
== bit_size0
&& bit_size
== bit_size1
);
1826 (void)bit_size0
; (void)bit_size1
;
1828 nir_const_value undef
= { .u64
= 0xdeadbeefdeadbeef };
1829 nir_const_value combined
[NIR_MAX_VEC_COMPONENTS
* 2];
1831 if (v0
->value_type
== vtn_value_type_constant
) {
1832 for (unsigned i
= 0; i
< len0
; i
++)
1833 combined
[i
] = v0
->constant
->values
[i
];
1835 if (v1
->value_type
== vtn_value_type_constant
) {
1836 for (unsigned i
= 0; i
< len1
; i
++)
1837 combined
[len0
+ i
] = v1
->constant
->values
[i
];
1840 for (unsigned i
= 0, j
= 0; i
< count
- 6; i
++, j
++) {
1841 uint32_t comp
= w
[i
+ 6];
1842 if (comp
== (uint32_t)-1) {
1843 /* If component is not used, set the value to a known constant
1844 * to detect if it is wrongly used.
1846 val
->constant
->values
[j
] = undef
;
1848 vtn_fail_if(comp
>= len0
+ len1
,
1849 "All Component literals must either be FFFFFFFF "
1850 "or in [0, N - 1] (inclusive).");
1851 val
->constant
->values
[j
] = combined
[comp
];
1857 case SpvOpCompositeExtract
:
1858 case SpvOpCompositeInsert
: {
1859 struct vtn_value
*comp
;
1860 unsigned deref_start
;
1861 struct nir_constant
**c
;
1862 if (opcode
== SpvOpCompositeExtract
) {
1863 comp
= vtn_value(b
, w
[4], vtn_value_type_constant
);
1865 c
= &comp
->constant
;
1867 comp
= vtn_value(b
, w
[5], vtn_value_type_constant
);
1869 val
->constant
= nir_constant_clone(comp
->constant
,
1875 const struct vtn_type
*type
= comp
->type
;
1876 for (unsigned i
= deref_start
; i
< count
; i
++) {
1877 vtn_fail_if(w
[i
] > type
->length
,
1878 "%uth index of %s is %u but the type has only "
1879 "%u elements", i
- deref_start
,
1880 spirv_op_to_string(opcode
), w
[i
], type
->length
);
1882 switch (type
->base_type
) {
1883 case vtn_base_type_vector
:
1885 type
= type
->array_element
;
1888 case vtn_base_type_matrix
:
1889 case vtn_base_type_array
:
1890 c
= &(*c
)->elements
[w
[i
]];
1891 type
= type
->array_element
;
1894 case vtn_base_type_struct
:
1895 c
= &(*c
)->elements
[w
[i
]];
1896 type
= type
->members
[w
[i
]];
1900 vtn_fail("%s must only index into composite types",
1901 spirv_op_to_string(opcode
));
1905 if (opcode
== SpvOpCompositeExtract
) {
1909 unsigned num_components
= type
->length
;
1910 for (unsigned i
= 0; i
< num_components
; i
++)
1911 val
->constant
->values
[i
] = (*c
)->values
[elem
+ i
];
1914 struct vtn_value
*insert
=
1915 vtn_value(b
, w
[4], vtn_value_type_constant
);
1916 vtn_assert(insert
->type
== type
);
1918 *c
= insert
->constant
;
1920 unsigned num_components
= type
->length
;
1921 for (unsigned i
= 0; i
< num_components
; i
++)
1922 (*c
)->values
[elem
+ i
] = insert
->constant
->values
[i
];
1930 nir_alu_type dst_alu_type
= nir_get_nir_type_for_glsl_type(val
->type
->type
);
1931 nir_alu_type src_alu_type
= dst_alu_type
;
1932 unsigned num_components
= glsl_get_vector_elements(val
->type
->type
);
1935 vtn_assert(count
<= 7);
1941 /* We have a source in a conversion */
1943 nir_get_nir_type_for_glsl_type(vtn_get_value_type(b
, w
[4])->type
);
1944 /* We use the bitsize of the conversion source to evaluate the opcode later */
1945 bit_size
= glsl_get_bit_size(vtn_get_value_type(b
, w
[4])->type
);
1948 bit_size
= glsl_get_bit_size(val
->type
->type
);
1951 nir_op op
= vtn_nir_alu_op_for_spirv_opcode(b
, opcode
, &swap
,
1952 nir_alu_type_get_type_size(src_alu_type
),
1953 nir_alu_type_get_type_size(dst_alu_type
));
1954 nir_const_value src
[3][NIR_MAX_VEC_COMPONENTS
];
1956 for (unsigned i
= 0; i
< count
- 4; i
++) {
1957 struct vtn_value
*src_val
=
1958 vtn_value(b
, w
[4 + i
], vtn_value_type_constant
);
1960 /* If this is an unsized source, pull the bit size from the
1961 * source; otherwise, we'll use the bit size from the destination.
1963 if (!nir_alu_type_get_type_size(nir_op_infos
[op
].input_types
[i
]))
1964 bit_size
= glsl_get_bit_size(src_val
->type
->type
);
1966 unsigned src_comps
= nir_op_infos
[op
].input_sizes
[i
] ?
1967 nir_op_infos
[op
].input_sizes
[i
] :
1970 unsigned j
= swap
? 1 - i
: i
;
1971 for (unsigned c
= 0; c
< src_comps
; c
++)
1972 src
[j
][c
] = src_val
->constant
->values
[c
];
1975 /* fix up fixed size sources */
1982 for (unsigned i
= 0; i
< num_components
; ++i
) {
1984 case 64: src
[1][i
].u32
= src
[1][i
].u64
; break;
1985 case 16: src
[1][i
].u32
= src
[1][i
].u16
; break;
1986 case 8: src
[1][i
].u32
= src
[1][i
].u8
; break;
1995 nir_const_value
*srcs
[3] = {
1996 src
[0], src
[1], src
[2],
1998 nir_eval_const_opcode(op
, val
->constant
->values
,
1999 num_components
, bit_size
, srcs
,
2000 b
->shader
->info
.float_controls_execution_mode
);
2007 case SpvOpConstantNull
:
2008 val
->constant
= vtn_null_constant(b
, val
->type
);
2011 case SpvOpConstantSampler
:
2012 vtn_fail("OpConstantSampler requires Kernel Capability");
2016 vtn_fail_with_opcode("Unhandled opcode", opcode
);
2019 /* Now that we have the value, update the workgroup size if needed */
2020 vtn_foreach_decoration(b
, val
, handle_workgroup_size_decoration_cb
, NULL
);
2023 SpvMemorySemanticsMask
2024 vtn_storage_class_to_memory_semantics(SpvStorageClass sc
)
2027 case SpvStorageClassStorageBuffer
:
2028 case SpvStorageClassPhysicalStorageBuffer
:
2029 return SpvMemorySemanticsUniformMemoryMask
;
2030 case SpvStorageClassWorkgroup
:
2031 return SpvMemorySemanticsWorkgroupMemoryMask
;
2033 return SpvMemorySemanticsMaskNone
;
2038 vtn_split_barrier_semantics(struct vtn_builder
*b
,
2039 SpvMemorySemanticsMask semantics
,
2040 SpvMemorySemanticsMask
*before
,
2041 SpvMemorySemanticsMask
*after
)
2043 /* For memory semantics embedded in operations, we split them into up to
2044 * two barriers, to be added before and after the operation. This is less
2045 * strict than if we propagated until the final backend stage, but still
2046 * result in correct execution.
2048 * A further improvement could be pipe this information (and use!) into the
2049 * next compiler layers, at the expense of making the handling of barriers
2053 *before
= SpvMemorySemanticsMaskNone
;
2054 *after
= SpvMemorySemanticsMaskNone
;
2056 SpvMemorySemanticsMask order_semantics
=
2057 semantics
& (SpvMemorySemanticsAcquireMask
|
2058 SpvMemorySemanticsReleaseMask
|
2059 SpvMemorySemanticsAcquireReleaseMask
|
2060 SpvMemorySemanticsSequentiallyConsistentMask
);
2062 if (util_bitcount(order_semantics
) > 1) {
2063 /* Old GLSLang versions incorrectly set all the ordering bits. This was
2064 * fixed in c51287d744fb6e7e9ccc09f6f8451e6c64b1dad6 of glslang repo,
2065 * and it is in GLSLang since revision "SPIRV99.1321" (from Jul-2016).
2067 vtn_warn("Multiple memory ordering semantics specified, "
2068 "assuming AcquireRelease.");
2069 order_semantics
= SpvMemorySemanticsAcquireReleaseMask
;
2072 const SpvMemorySemanticsMask av_vis_semantics
=
2073 semantics
& (SpvMemorySemanticsMakeAvailableMask
|
2074 SpvMemorySemanticsMakeVisibleMask
);
2076 const SpvMemorySemanticsMask storage_semantics
=
2077 semantics
& (SpvMemorySemanticsUniformMemoryMask
|
2078 SpvMemorySemanticsSubgroupMemoryMask
|
2079 SpvMemorySemanticsWorkgroupMemoryMask
|
2080 SpvMemorySemanticsCrossWorkgroupMemoryMask
|
2081 SpvMemorySemanticsAtomicCounterMemoryMask
|
2082 SpvMemorySemanticsImageMemoryMask
|
2083 SpvMemorySemanticsOutputMemoryMask
);
2085 const SpvMemorySemanticsMask other_semantics
=
2086 semantics
& ~(order_semantics
| av_vis_semantics
| storage_semantics
);
2088 if (other_semantics
)
2089 vtn_warn("Ignoring unhandled memory semantics: %u\n", other_semantics
);
2091 /* SequentiallyConsistent is treated as AcquireRelease. */
2093 /* The RELEASE barrier happens BEFORE the operation, and it is usually
2094 * associated with a Store. All the write operations with a matching
2095 * semantics will not be reordered after the Store.
2097 if (order_semantics
& (SpvMemorySemanticsReleaseMask
|
2098 SpvMemorySemanticsAcquireReleaseMask
|
2099 SpvMemorySemanticsSequentiallyConsistentMask
)) {
2100 *before
|= SpvMemorySemanticsReleaseMask
| storage_semantics
;
2103 /* The ACQUIRE barrier happens AFTER the operation, and it is usually
2104 * associated with a Load. All the operations with a matching semantics
2105 * will not be reordered before the Load.
2107 if (order_semantics
& (SpvMemorySemanticsAcquireMask
|
2108 SpvMemorySemanticsAcquireReleaseMask
|
2109 SpvMemorySemanticsSequentiallyConsistentMask
)) {
2110 *after
|= SpvMemorySemanticsAcquireMask
| storage_semantics
;
2113 if (av_vis_semantics
& SpvMemorySemanticsMakeVisibleMask
)
2114 *before
|= SpvMemorySemanticsMakeVisibleMask
| storage_semantics
;
2116 if (av_vis_semantics
& SpvMemorySemanticsMakeAvailableMask
)
2117 *after
|= SpvMemorySemanticsMakeAvailableMask
| storage_semantics
;
2120 static nir_memory_semantics
2121 vtn_mem_semantics_to_nir_mem_semantics(struct vtn_builder
*b
,
2122 SpvMemorySemanticsMask semantics
)
2124 nir_memory_semantics nir_semantics
= 0;
2126 SpvMemorySemanticsMask order_semantics
=
2127 semantics
& (SpvMemorySemanticsAcquireMask
|
2128 SpvMemorySemanticsReleaseMask
|
2129 SpvMemorySemanticsAcquireReleaseMask
|
2130 SpvMemorySemanticsSequentiallyConsistentMask
);
2132 if (util_bitcount(order_semantics
) > 1) {
2133 /* Old GLSLang versions incorrectly set all the ordering bits. This was
2134 * fixed in c51287d744fb6e7e9ccc09f6f8451e6c64b1dad6 of glslang repo,
2135 * and it is in GLSLang since revision "SPIRV99.1321" (from Jul-2016).
2137 vtn_warn("Multiple memory ordering semantics bits specified, "
2138 "assuming AcquireRelease.");
2139 order_semantics
= SpvMemorySemanticsAcquireReleaseMask
;
2142 switch (order_semantics
) {
2144 /* Not an ordering barrier. */
2147 case SpvMemorySemanticsAcquireMask
:
2148 nir_semantics
= NIR_MEMORY_ACQUIRE
;
2151 case SpvMemorySemanticsReleaseMask
:
2152 nir_semantics
= NIR_MEMORY_RELEASE
;
2155 case SpvMemorySemanticsSequentiallyConsistentMask
:
2156 /* Fall through. Treated as AcquireRelease in Vulkan. */
2157 case SpvMemorySemanticsAcquireReleaseMask
:
2158 nir_semantics
= NIR_MEMORY_ACQUIRE
| NIR_MEMORY_RELEASE
;
2162 unreachable("Invalid memory order semantics");
2165 if (semantics
& SpvMemorySemanticsMakeAvailableMask
) {
2166 vtn_fail_if(!b
->options
->caps
.vk_memory_model
,
2167 "To use MakeAvailable memory semantics the VulkanMemoryModel "
2168 "capability must be declared.");
2169 nir_semantics
|= NIR_MEMORY_MAKE_AVAILABLE
;
2172 if (semantics
& SpvMemorySemanticsMakeVisibleMask
) {
2173 vtn_fail_if(!b
->options
->caps
.vk_memory_model
,
2174 "To use MakeVisible memory semantics the VulkanMemoryModel "
2175 "capability must be declared.");
2176 nir_semantics
|= NIR_MEMORY_MAKE_VISIBLE
;
2179 return nir_semantics
;
2182 static nir_variable_mode
2183 vtn_mem_sematics_to_nir_var_modes(struct vtn_builder
*b
,
2184 SpvMemorySemanticsMask semantics
)
2186 /* Vulkan Environment for SPIR-V says "SubgroupMemory, CrossWorkgroupMemory,
2187 * and AtomicCounterMemory are ignored".
2189 semantics
&= ~(SpvMemorySemanticsSubgroupMemoryMask
|
2190 SpvMemorySemanticsCrossWorkgroupMemoryMask
|
2191 SpvMemorySemanticsAtomicCounterMemoryMask
);
2193 /* TODO: Consider adding nir_var_mem_image mode to NIR so it can be used
2194 * for SpvMemorySemanticsImageMemoryMask.
2197 nir_variable_mode modes
= 0;
2198 if (semantics
& (SpvMemorySemanticsUniformMemoryMask
|
2199 SpvMemorySemanticsImageMemoryMask
)) {
2200 modes
|= nir_var_uniform
|
2205 if (semantics
& SpvMemorySemanticsWorkgroupMemoryMask
)
2206 modes
|= nir_var_mem_shared
;
2207 if (semantics
& SpvMemorySemanticsOutputMemoryMask
) {
2208 modes
|= nir_var_shader_out
;
2215 vtn_scope_to_nir_scope(struct vtn_builder
*b
, SpvScope scope
)
2217 nir_scope nir_scope
;
2219 case SpvScopeDevice
:
2220 vtn_fail_if(b
->options
->caps
.vk_memory_model
&&
2221 !b
->options
->caps
.vk_memory_model_device_scope
,
2222 "If the Vulkan memory model is declared and any instruction "
2223 "uses Device scope, the VulkanMemoryModelDeviceScope "
2224 "capability must be declared.");
2225 nir_scope
= NIR_SCOPE_DEVICE
;
2228 case SpvScopeQueueFamily
:
2229 vtn_fail_if(!b
->options
->caps
.vk_memory_model
,
2230 "To use Queue Family scope, the VulkanMemoryModel capability "
2231 "must be declared.");
2232 nir_scope
= NIR_SCOPE_QUEUE_FAMILY
;
2235 case SpvScopeWorkgroup
:
2236 nir_scope
= NIR_SCOPE_WORKGROUP
;
2239 case SpvScopeSubgroup
:
2240 nir_scope
= NIR_SCOPE_SUBGROUP
;
2243 case SpvScopeInvocation
:
2244 nir_scope
= NIR_SCOPE_INVOCATION
;
2248 vtn_fail("Invalid memory scope");
2255 vtn_emit_scoped_control_barrier(struct vtn_builder
*b
, SpvScope exec_scope
,
2257 SpvMemorySemanticsMask semantics
)
2259 nir_memory_semantics nir_semantics
=
2260 vtn_mem_semantics_to_nir_mem_semantics(b
, semantics
);
2261 nir_variable_mode modes
= vtn_mem_sematics_to_nir_var_modes(b
, semantics
);
2262 nir_scope nir_exec_scope
= vtn_scope_to_nir_scope(b
, exec_scope
);
2264 /* Memory semantics is optional for OpControlBarrier. */
2265 nir_scope nir_mem_scope
;
2266 if (nir_semantics
== 0 || modes
== 0)
2267 nir_mem_scope
= NIR_SCOPE_NONE
;
2269 nir_mem_scope
= vtn_scope_to_nir_scope(b
, mem_scope
);
2271 nir_scoped_barrier(&b
->nb
, nir_exec_scope
, nir_mem_scope
, nir_semantics
, modes
);
2275 vtn_emit_scoped_memory_barrier(struct vtn_builder
*b
, SpvScope scope
,
2276 SpvMemorySemanticsMask semantics
)
2278 nir_variable_mode modes
= vtn_mem_sematics_to_nir_var_modes(b
, semantics
);
2279 nir_memory_semantics nir_semantics
=
2280 vtn_mem_semantics_to_nir_mem_semantics(b
, semantics
);
2282 /* No barrier to add. */
2283 if (nir_semantics
== 0 || modes
== 0)
2286 nir_scope nir_mem_scope
= vtn_scope_to_nir_scope(b
, scope
);
2287 nir_scoped_barrier(&b
->nb
, NIR_SCOPE_NONE
, nir_mem_scope
, nir_semantics
, modes
);
2290 struct vtn_ssa_value
*
2291 vtn_create_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
2293 /* Always use bare types for SSA values for a couple of reasons:
2295 * 1. Code which emits deref chains should never listen to the explicit
2296 * layout information on the SSA value if any exists. If we've
2297 * accidentally been relying on this, we want to find those bugs.
2299 * 2. We want to be able to quickly check that an SSA value being assigned
2300 * to a SPIR-V value has the right type. Using bare types everywhere
2301 * ensures that we can pointer-compare.
2303 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
2304 val
->type
= glsl_get_bare_type(type
);
2307 if (!glsl_type_is_vector_or_scalar(type
)) {
2308 unsigned elems
= glsl_get_length(val
->type
);
2309 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
2310 if (glsl_type_is_array_or_matrix(type
)) {
2311 const struct glsl_type
*elem_type
= glsl_get_array_element(type
);
2312 for (unsigned i
= 0; i
< elems
; i
++)
2313 val
->elems
[i
] = vtn_create_ssa_value(b
, elem_type
);
2315 vtn_assert(glsl_type_is_struct_or_ifc(type
));
2316 for (unsigned i
= 0; i
< elems
; i
++) {
2317 const struct glsl_type
*elem_type
= glsl_get_struct_field(type
, i
);
2318 val
->elems
[i
] = vtn_create_ssa_value(b
, elem_type
);
2327 vtn_tex_src(struct vtn_builder
*b
, unsigned index
, nir_tex_src_type type
)
2330 src
.src
= nir_src_for_ssa(vtn_get_nir_ssa(b
, index
));
2331 src
.src_type
= type
;
2336 image_operand_arg(struct vtn_builder
*b
, const uint32_t *w
, uint32_t count
,
2337 uint32_t mask_idx
, SpvImageOperandsMask op
)
2339 static const SpvImageOperandsMask ops_with_arg
=
2340 SpvImageOperandsBiasMask
|
2341 SpvImageOperandsLodMask
|
2342 SpvImageOperandsGradMask
|
2343 SpvImageOperandsConstOffsetMask
|
2344 SpvImageOperandsOffsetMask
|
2345 SpvImageOperandsConstOffsetsMask
|
2346 SpvImageOperandsSampleMask
|
2347 SpvImageOperandsMinLodMask
|
2348 SpvImageOperandsMakeTexelAvailableMask
|
2349 SpvImageOperandsMakeTexelVisibleMask
;
2351 assert(util_bitcount(op
) == 1);
2352 assert(w
[mask_idx
] & op
);
2353 assert(op
& ops_with_arg
);
2355 uint32_t idx
= util_bitcount(w
[mask_idx
] & (op
- 1) & ops_with_arg
) + 1;
2357 /* Adjust indices for operands with two arguments. */
2358 static const SpvImageOperandsMask ops_with_two_args
=
2359 SpvImageOperandsGradMask
;
2360 idx
+= util_bitcount(w
[mask_idx
] & (op
- 1) & ops_with_two_args
);
2364 vtn_fail_if(idx
+ (op
& ops_with_two_args
? 1 : 0) >= count
,
2365 "Image op claims to have %s but does not enough "
2366 "following operands", spirv_imageoperands_to_string(op
));
2372 non_uniform_decoration_cb(struct vtn_builder
*b
,
2373 struct vtn_value
*val
, int member
,
2374 const struct vtn_decoration
*dec
, void *void_ctx
)
2376 enum gl_access_qualifier
*access
= void_ctx
;
2377 switch (dec
->decoration
) {
2378 case SpvDecorationNonUniformEXT
:
2379 *access
|= ACCESS_NON_UNIFORM
;
2388 vtn_handle_texture(struct vtn_builder
*b
, SpvOp opcode
,
2389 const uint32_t *w
, unsigned count
)
2391 struct vtn_type
*ret_type
= vtn_get_type(b
, w
[1]);
2393 if (opcode
== SpvOpSampledImage
) {
2394 struct vtn_sampled_image si
= {
2395 .image
= vtn_get_image(b
, w
[3]),
2396 .sampler
= vtn_get_sampler(b
, w
[4]),
2398 vtn_push_sampled_image(b
, w
[2], si
);
2400 } else if (opcode
== SpvOpImage
) {
2401 struct vtn_sampled_image si
= vtn_get_sampled_image(b
, w
[3]);
2402 vtn_push_image(b
, w
[2], si
.image
);
2406 nir_deref_instr
*image
= NULL
, *sampler
= NULL
;
2407 struct vtn_value
*sampled_val
= vtn_untyped_value(b
, w
[3]);
2408 if (sampled_val
->type
->base_type
== vtn_base_type_sampled_image
) {
2409 struct vtn_sampled_image si
= vtn_get_sampled_image(b
, w
[3]);
2411 sampler
= si
.sampler
;
2413 image
= vtn_get_image(b
, w
[3]);
2416 const enum glsl_sampler_dim sampler_dim
= glsl_get_sampler_dim(image
->type
);
2417 const bool is_array
= glsl_sampler_type_is_array(image
->type
);
2418 nir_alu_type dest_type
= nir_type_invalid
;
2420 /* Figure out the base texture operation */
2423 case SpvOpImageSampleImplicitLod
:
2424 case SpvOpImageSampleDrefImplicitLod
:
2425 case SpvOpImageSampleProjImplicitLod
:
2426 case SpvOpImageSampleProjDrefImplicitLod
:
2427 texop
= nir_texop_tex
;
2430 case SpvOpImageSampleExplicitLod
:
2431 case SpvOpImageSampleDrefExplicitLod
:
2432 case SpvOpImageSampleProjExplicitLod
:
2433 case SpvOpImageSampleProjDrefExplicitLod
:
2434 texop
= nir_texop_txl
;
2437 case SpvOpImageFetch
:
2438 if (sampler_dim
== GLSL_SAMPLER_DIM_MS
) {
2439 texop
= nir_texop_txf_ms
;
2441 texop
= nir_texop_txf
;
2445 case SpvOpImageGather
:
2446 case SpvOpImageDrefGather
:
2447 texop
= nir_texop_tg4
;
2450 case SpvOpImageQuerySizeLod
:
2451 case SpvOpImageQuerySize
:
2452 texop
= nir_texop_txs
;
2453 dest_type
= nir_type_int
;
2456 case SpvOpImageQueryLod
:
2457 texop
= nir_texop_lod
;
2458 dest_type
= nir_type_float
;
2461 case SpvOpImageQueryLevels
:
2462 texop
= nir_texop_query_levels
;
2463 dest_type
= nir_type_int
;
2466 case SpvOpImageQuerySamples
:
2467 texop
= nir_texop_texture_samples
;
2468 dest_type
= nir_type_int
;
2471 case SpvOpFragmentFetchAMD
:
2472 texop
= nir_texop_fragment_fetch
;
2475 case SpvOpFragmentMaskFetchAMD
:
2476 texop
= nir_texop_fragment_mask_fetch
;
2480 vtn_fail_with_opcode("Unhandled opcode", opcode
);
2483 nir_tex_src srcs
[10]; /* 10 should be enough */
2484 nir_tex_src
*p
= srcs
;
2486 p
->src
= nir_src_for_ssa(&image
->dest
.ssa
);
2487 p
->src_type
= nir_tex_src_texture_deref
;
2497 vtn_fail_if(sampler
== NULL
,
2498 "%s requires an image of type OpTypeSampledImage",
2499 spirv_op_to_string(opcode
));
2500 p
->src
= nir_src_for_ssa(&sampler
->dest
.ssa
);
2501 p
->src_type
= nir_tex_src_sampler_deref
;
2505 case nir_texop_txf_ms
:
2507 case nir_texop_query_levels
:
2508 case nir_texop_texture_samples
:
2509 case nir_texop_samples_identical
:
2510 case nir_texop_fragment_fetch
:
2511 case nir_texop_fragment_mask_fetch
:
2514 case nir_texop_txf_ms_fb
:
2515 vtn_fail("unexpected nir_texop_txf_ms_fb");
2517 case nir_texop_txf_ms_mcs
:
2518 vtn_fail("unexpected nir_texop_txf_ms_mcs");
2519 case nir_texop_tex_prefetch
:
2520 vtn_fail("unexpected nir_texop_tex_prefetch");
2525 struct nir_ssa_def
*coord
;
2526 unsigned coord_components
;
2528 case SpvOpImageSampleImplicitLod
:
2529 case SpvOpImageSampleExplicitLod
:
2530 case SpvOpImageSampleDrefImplicitLod
:
2531 case SpvOpImageSampleDrefExplicitLod
:
2532 case SpvOpImageSampleProjImplicitLod
:
2533 case SpvOpImageSampleProjExplicitLod
:
2534 case SpvOpImageSampleProjDrefImplicitLod
:
2535 case SpvOpImageSampleProjDrefExplicitLod
:
2536 case SpvOpImageFetch
:
2537 case SpvOpImageGather
:
2538 case SpvOpImageDrefGather
:
2539 case SpvOpImageQueryLod
:
2540 case SpvOpFragmentFetchAMD
:
2541 case SpvOpFragmentMaskFetchAMD
: {
2542 /* All these types have the coordinate as their first real argument */
2543 coord_components
= glsl_get_sampler_dim_coordinate_components(sampler_dim
);
2545 if (is_array
&& texop
!= nir_texop_lod
)
2548 coord
= vtn_get_nir_ssa(b
, w
[idx
++]);
2549 p
->src
= nir_src_for_ssa(nir_channels(&b
->nb
, coord
,
2550 (1 << coord_components
) - 1));
2551 p
->src_type
= nir_tex_src_coord
;
2558 coord_components
= 0;
2563 case SpvOpImageSampleProjImplicitLod
:
2564 case SpvOpImageSampleProjExplicitLod
:
2565 case SpvOpImageSampleProjDrefImplicitLod
:
2566 case SpvOpImageSampleProjDrefExplicitLod
:
2567 /* These have the projector as the last coordinate component */
2568 p
->src
= nir_src_for_ssa(nir_channel(&b
->nb
, coord
, coord_components
));
2569 p
->src_type
= nir_tex_src_projector
;
2577 bool is_shadow
= false;
2578 unsigned gather_component
= 0;
2580 case SpvOpImageSampleDrefImplicitLod
:
2581 case SpvOpImageSampleDrefExplicitLod
:
2582 case SpvOpImageSampleProjDrefImplicitLod
:
2583 case SpvOpImageSampleProjDrefExplicitLod
:
2584 case SpvOpImageDrefGather
:
2585 /* These all have an explicit depth value as their next source */
2587 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_comparator
);
2590 case SpvOpImageGather
:
2591 /* This has a component as its next source */
2592 gather_component
= vtn_constant_uint(b
, w
[idx
++]);
2599 /* For OpImageQuerySizeLod, we always have an LOD */
2600 if (opcode
== SpvOpImageQuerySizeLod
)
2601 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
2603 /* For OpFragmentFetchAMD, we always have a multisample index */
2604 if (opcode
== SpvOpFragmentFetchAMD
)
2605 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ms_index
);
2607 /* Now we need to handle some number of optional arguments */
2608 struct vtn_value
*gather_offsets
= NULL
;
2610 uint32_t operands
= w
[idx
];
2612 if (operands
& SpvImageOperandsBiasMask
) {
2613 vtn_assert(texop
== nir_texop_tex
||
2614 texop
== nir_texop_tg4
);
2615 if (texop
== nir_texop_tex
)
2616 texop
= nir_texop_txb
;
2617 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2618 SpvImageOperandsBiasMask
);
2619 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_bias
);
2622 if (operands
& SpvImageOperandsLodMask
) {
2623 vtn_assert(texop
== nir_texop_txl
|| texop
== nir_texop_txf
||
2624 texop
== nir_texop_txs
|| texop
== nir_texop_tg4
);
2625 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2626 SpvImageOperandsLodMask
);
2627 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_lod
);
2630 if (operands
& SpvImageOperandsGradMask
) {
2631 vtn_assert(texop
== nir_texop_txl
);
2632 texop
= nir_texop_txd
;
2633 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2634 SpvImageOperandsGradMask
);
2635 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_ddx
);
2636 (*p
++) = vtn_tex_src(b
, w
[arg
+ 1], nir_tex_src_ddy
);
2639 vtn_fail_if(util_bitcount(operands
& (SpvImageOperandsConstOffsetsMask
|
2640 SpvImageOperandsOffsetMask
|
2641 SpvImageOperandsConstOffsetMask
)) > 1,
2642 "At most one of the ConstOffset, Offset, and ConstOffsets "
2643 "image operands can be used on a given instruction.");
2645 if (operands
& SpvImageOperandsOffsetMask
) {
2646 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2647 SpvImageOperandsOffsetMask
);
2648 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_offset
);
2651 if (operands
& SpvImageOperandsConstOffsetMask
) {
2652 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2653 SpvImageOperandsConstOffsetMask
);
2654 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_offset
);
2657 if (operands
& SpvImageOperandsConstOffsetsMask
) {
2658 vtn_assert(texop
== nir_texop_tg4
);
2659 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2660 SpvImageOperandsConstOffsetsMask
);
2661 gather_offsets
= vtn_value(b
, w
[arg
], vtn_value_type_constant
);
2664 if (operands
& SpvImageOperandsSampleMask
) {
2665 vtn_assert(texop
== nir_texop_txf_ms
);
2666 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2667 SpvImageOperandsSampleMask
);
2668 texop
= nir_texop_txf_ms
;
2669 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_ms_index
);
2672 if (operands
& SpvImageOperandsMinLodMask
) {
2673 vtn_assert(texop
== nir_texop_tex
||
2674 texop
== nir_texop_txb
||
2675 texop
== nir_texop_txd
);
2676 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2677 SpvImageOperandsMinLodMask
);
2678 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_min_lod
);
2682 nir_tex_instr
*instr
= nir_tex_instr_create(b
->shader
, p
- srcs
);
2685 memcpy(instr
->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
2687 instr
->coord_components
= coord_components
;
2688 instr
->sampler_dim
= sampler_dim
;
2689 instr
->is_array
= is_array
;
2690 instr
->is_shadow
= is_shadow
;
2691 instr
->is_new_style_shadow
=
2692 is_shadow
&& glsl_get_components(ret_type
->type
) == 1;
2693 instr
->component
= gather_component
;
2695 /* The Vulkan spec says:
2697 * "If an instruction loads from or stores to a resource (including
2698 * atomics and image instructions) and the resource descriptor being
2699 * accessed is not dynamically uniform, then the operand corresponding
2700 * to that resource (e.g. the pointer or sampled image operand) must be
2701 * decorated with NonUniform."
2703 * It's very careful to specify that the exact operand must be decorated
2704 * NonUniform. The SPIR-V parser is not expected to chase through long
2705 * chains to find the NonUniform decoration. It's either right there or we
2706 * can assume it doesn't exist.
2708 enum gl_access_qualifier access
= 0;
2709 vtn_foreach_decoration(b
, sampled_val
, non_uniform_decoration_cb
, &access
);
2711 if (image
&& (access
& ACCESS_NON_UNIFORM
))
2712 instr
->texture_non_uniform
= true;
2714 if (sampler
&& (access
& ACCESS_NON_UNIFORM
))
2715 instr
->sampler_non_uniform
= true;
2717 /* for non-query ops, get dest_type from sampler type */
2718 if (dest_type
== nir_type_invalid
) {
2719 switch (glsl_get_sampler_result_type(image
->type
)) {
2720 case GLSL_TYPE_FLOAT
: dest_type
= nir_type_float
; break;
2721 case GLSL_TYPE_INT
: dest_type
= nir_type_int
; break;
2722 case GLSL_TYPE_UINT
: dest_type
= nir_type_uint
; break;
2723 case GLSL_TYPE_BOOL
: dest_type
= nir_type_bool
; break;
2725 vtn_fail("Invalid base type for sampler result");
2729 instr
->dest_type
= dest_type
;
2731 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
2732 nir_tex_instr_dest_size(instr
), 32, NULL
);
2734 vtn_assert(glsl_get_vector_elements(ret_type
->type
) ==
2735 nir_tex_instr_dest_size(instr
));
2737 if (gather_offsets
) {
2738 vtn_fail_if(gather_offsets
->type
->base_type
!= vtn_base_type_array
||
2739 gather_offsets
->type
->length
!= 4,
2740 "ConstOffsets must be an array of size four of vectors "
2741 "of two integer components");
2743 struct vtn_type
*vec_type
= gather_offsets
->type
->array_element
;
2744 vtn_fail_if(vec_type
->base_type
!= vtn_base_type_vector
||
2745 vec_type
->length
!= 2 ||
2746 !glsl_type_is_integer(vec_type
->type
),
2747 "ConstOffsets must be an array of size four of vectors "
2748 "of two integer components");
2750 unsigned bit_size
= glsl_get_bit_size(vec_type
->type
);
2751 for (uint32_t i
= 0; i
< 4; i
++) {
2752 const nir_const_value
*cvec
=
2753 gather_offsets
->constant
->elements
[i
]->values
;
2754 for (uint32_t j
= 0; j
< 2; j
++) {
2756 case 8: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i8
; break;
2757 case 16: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i16
; break;
2758 case 32: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i32
; break;
2759 case 64: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i64
; break;
2761 vtn_fail("Unsupported bit size: %u", bit_size
);
2767 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
2769 vtn_push_nir_ssa(b
, w
[2], &instr
->dest
.ssa
);
2773 fill_common_atomic_sources(struct vtn_builder
*b
, SpvOp opcode
,
2774 const uint32_t *w
, nir_src
*src
)
2777 case SpvOpAtomicIIncrement
:
2778 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, 1));
2781 case SpvOpAtomicIDecrement
:
2782 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, -1));
2785 case SpvOpAtomicISub
:
2787 nir_src_for_ssa(nir_ineg(&b
->nb
, vtn_get_nir_ssa(b
, w
[6])));
2790 case SpvOpAtomicCompareExchange
:
2791 case SpvOpAtomicCompareExchangeWeak
:
2792 src
[0] = nir_src_for_ssa(vtn_get_nir_ssa(b
, w
[8]));
2793 src
[1] = nir_src_for_ssa(vtn_get_nir_ssa(b
, w
[7]));
2796 case SpvOpAtomicExchange
:
2797 case SpvOpAtomicIAdd
:
2798 case SpvOpAtomicSMin
:
2799 case SpvOpAtomicUMin
:
2800 case SpvOpAtomicSMax
:
2801 case SpvOpAtomicUMax
:
2802 case SpvOpAtomicAnd
:
2804 case SpvOpAtomicXor
:
2805 case SpvOpAtomicFAddEXT
:
2806 src
[0] = nir_src_for_ssa(vtn_get_nir_ssa(b
, w
[6]));
2810 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
2814 static nir_ssa_def
*
2815 get_image_coord(struct vtn_builder
*b
, uint32_t value
)
2817 nir_ssa_def
*coord
= vtn_get_nir_ssa(b
, value
);
2819 /* The image_load_store intrinsics assume a 4-dim coordinate */
2820 unsigned swizzle
[4];
2821 for (unsigned i
= 0; i
< 4; i
++)
2822 swizzle
[i
] = MIN2(i
, coord
->num_components
- 1);
2824 return nir_swizzle(&b
->nb
, coord
, swizzle
, 4);
2827 static nir_ssa_def
*
2828 expand_to_vec4(nir_builder
*b
, nir_ssa_def
*value
)
2830 if (value
->num_components
== 4)
2834 for (unsigned i
= 0; i
< 4; i
++)
2835 swiz
[i
] = i
< value
->num_components
? i
: 0;
2836 return nir_swizzle(b
, value
, swiz
, 4);
2840 vtn_handle_image(struct vtn_builder
*b
, SpvOp opcode
,
2841 const uint32_t *w
, unsigned count
)
2843 /* Just get this one out of the way */
2844 if (opcode
== SpvOpImageTexelPointer
) {
2845 struct vtn_value
*val
=
2846 vtn_push_value(b
, w
[2], vtn_value_type_image_pointer
);
2847 val
->image
= ralloc(b
, struct vtn_image_pointer
);
2849 val
->image
->image
= vtn_nir_deref(b
, w
[3]);
2850 val
->image
->coord
= get_image_coord(b
, w
[4]);
2851 val
->image
->sample
= vtn_get_nir_ssa(b
, w
[5]);
2852 val
->image
->lod
= nir_imm_int(&b
->nb
, 0);
2856 struct vtn_image_pointer image
;
2857 SpvScope scope
= SpvScopeInvocation
;
2858 SpvMemorySemanticsMask semantics
= 0;
2860 struct vtn_value
*res_val
;
2862 case SpvOpAtomicExchange
:
2863 case SpvOpAtomicCompareExchange
:
2864 case SpvOpAtomicCompareExchangeWeak
:
2865 case SpvOpAtomicIIncrement
:
2866 case SpvOpAtomicIDecrement
:
2867 case SpvOpAtomicIAdd
:
2868 case SpvOpAtomicISub
:
2869 case SpvOpAtomicLoad
:
2870 case SpvOpAtomicSMin
:
2871 case SpvOpAtomicUMin
:
2872 case SpvOpAtomicSMax
:
2873 case SpvOpAtomicUMax
:
2874 case SpvOpAtomicAnd
:
2876 case SpvOpAtomicXor
:
2877 case SpvOpAtomicFAddEXT
:
2878 res_val
= vtn_value(b
, w
[3], vtn_value_type_image_pointer
);
2879 image
= *res_val
->image
;
2880 scope
= vtn_constant_uint(b
, w
[4]);
2881 semantics
= vtn_constant_uint(b
, w
[5]);
2884 case SpvOpAtomicStore
:
2885 res_val
= vtn_value(b
, w
[1], vtn_value_type_image_pointer
);
2886 image
= *res_val
->image
;
2887 scope
= vtn_constant_uint(b
, w
[2]);
2888 semantics
= vtn_constant_uint(b
, w
[3]);
2891 case SpvOpImageQuerySize
:
2892 res_val
= vtn_untyped_value(b
, w
[3]);
2893 image
.image
= vtn_get_image(b
, w
[3]);
2895 image
.sample
= NULL
;
2899 case SpvOpImageRead
: {
2900 res_val
= vtn_untyped_value(b
, w
[3]);
2901 image
.image
= vtn_get_image(b
, w
[3]);
2902 image
.coord
= get_image_coord(b
, w
[4]);
2904 const SpvImageOperandsMask operands
=
2905 count
> 5 ? w
[5] : SpvImageOperandsMaskNone
;
2907 if (operands
& SpvImageOperandsSampleMask
) {
2908 uint32_t arg
= image_operand_arg(b
, w
, count
, 5,
2909 SpvImageOperandsSampleMask
);
2910 image
.sample
= vtn_get_nir_ssa(b
, w
[arg
]);
2912 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
2915 if (operands
& SpvImageOperandsMakeTexelVisibleMask
) {
2916 vtn_fail_if((operands
& SpvImageOperandsNonPrivateTexelMask
) == 0,
2917 "MakeTexelVisible requires NonPrivateTexel to also be set.");
2918 uint32_t arg
= image_operand_arg(b
, w
, count
, 5,
2919 SpvImageOperandsMakeTexelVisibleMask
);
2920 semantics
= SpvMemorySemanticsMakeVisibleMask
;
2921 scope
= vtn_constant_uint(b
, w
[arg
]);
2924 if (operands
& SpvImageOperandsLodMask
) {
2925 uint32_t arg
= image_operand_arg(b
, w
, count
, 5,
2926 SpvImageOperandsLodMask
);
2927 image
.lod
= vtn_get_nir_ssa(b
, w
[arg
]);
2929 image
.lod
= nir_imm_int(&b
->nb
, 0);
2932 /* TODO: Volatile. */
2937 case SpvOpImageWrite
: {
2938 res_val
= vtn_untyped_value(b
, w
[1]);
2939 image
.image
= vtn_get_image(b
, w
[1]);
2940 image
.coord
= get_image_coord(b
, w
[2]);
2944 const SpvImageOperandsMask operands
=
2945 count
> 4 ? w
[4] : SpvImageOperandsMaskNone
;
2947 if (operands
& SpvImageOperandsSampleMask
) {
2948 uint32_t arg
= image_operand_arg(b
, w
, count
, 4,
2949 SpvImageOperandsSampleMask
);
2950 image
.sample
= vtn_get_nir_ssa(b
, w
[arg
]);
2952 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
2955 if (operands
& SpvImageOperandsMakeTexelAvailableMask
) {
2956 vtn_fail_if((operands
& SpvImageOperandsNonPrivateTexelMask
) == 0,
2957 "MakeTexelAvailable requires NonPrivateTexel to also be set.");
2958 uint32_t arg
= image_operand_arg(b
, w
, count
, 4,
2959 SpvImageOperandsMakeTexelAvailableMask
);
2960 semantics
= SpvMemorySemanticsMakeAvailableMask
;
2961 scope
= vtn_constant_uint(b
, w
[arg
]);
2964 if (operands
& SpvImageOperandsLodMask
) {
2965 uint32_t arg
= image_operand_arg(b
, w
, count
, 4,
2966 SpvImageOperandsLodMask
);
2967 image
.lod
= vtn_get_nir_ssa(b
, w
[arg
]);
2969 image
.lod
= nir_imm_int(&b
->nb
, 0);
2972 /* TODO: Volatile. */
2978 vtn_fail_with_opcode("Invalid image opcode", opcode
);
2981 nir_intrinsic_op op
;
2983 #define OP(S, N) case SpvOp##S: op = nir_intrinsic_image_deref_##N; break;
2984 OP(ImageQuerySize
, size
)
2986 OP(ImageWrite
, store
)
2987 OP(AtomicLoad
, load
)
2988 OP(AtomicStore
, store
)
2989 OP(AtomicExchange
, atomic_exchange
)
2990 OP(AtomicCompareExchange
, atomic_comp_swap
)
2991 OP(AtomicCompareExchangeWeak
, atomic_comp_swap
)
2992 OP(AtomicIIncrement
, atomic_add
)
2993 OP(AtomicIDecrement
, atomic_add
)
2994 OP(AtomicIAdd
, atomic_add
)
2995 OP(AtomicISub
, atomic_add
)
2996 OP(AtomicSMin
, atomic_imin
)
2997 OP(AtomicUMin
, atomic_umin
)
2998 OP(AtomicSMax
, atomic_imax
)
2999 OP(AtomicUMax
, atomic_umax
)
3000 OP(AtomicAnd
, atomic_and
)
3001 OP(AtomicOr
, atomic_or
)
3002 OP(AtomicXor
, atomic_xor
)
3003 OP(AtomicFAddEXT
, atomic_fadd
)
3006 vtn_fail_with_opcode("Invalid image opcode", opcode
);
3009 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
3011 intrin
->src
[0] = nir_src_for_ssa(&image
.image
->dest
.ssa
);
3013 /* ImageQuerySize doesn't take any extra parameters */
3014 if (opcode
!= SpvOpImageQuerySize
) {
3015 /* The image coordinate is always 4 components but we may not have that
3016 * many. Swizzle to compensate.
3018 intrin
->src
[1] = nir_src_for_ssa(expand_to_vec4(&b
->nb
, image
.coord
));
3019 intrin
->src
[2] = nir_src_for_ssa(image
.sample
);
3022 /* The Vulkan spec says:
3024 * "If an instruction loads from or stores to a resource (including
3025 * atomics and image instructions) and the resource descriptor being
3026 * accessed is not dynamically uniform, then the operand corresponding
3027 * to that resource (e.g. the pointer or sampled image operand) must be
3028 * decorated with NonUniform."
3030 * It's very careful to specify that the exact operand must be decorated
3031 * NonUniform. The SPIR-V parser is not expected to chase through long
3032 * chains to find the NonUniform decoration. It's either right there or we
3033 * can assume it doesn't exist.
3035 enum gl_access_qualifier access
= 0;
3036 vtn_foreach_decoration(b
, res_val
, non_uniform_decoration_cb
, &access
);
3037 nir_intrinsic_set_access(intrin
, access
);
3040 case SpvOpAtomicLoad
:
3041 case SpvOpImageQuerySize
:
3042 case SpvOpImageRead
:
3043 if (opcode
== SpvOpImageRead
|| opcode
== SpvOpAtomicLoad
) {
3044 /* Only OpImageRead can support a lod parameter if
3045 * SPV_AMD_shader_image_load_store_lod is used but the current NIR
3046 * intrinsics definition for atomics requires us to set it for
3049 intrin
->src
[3] = nir_src_for_ssa(image
.lod
);
3052 case SpvOpAtomicStore
:
3053 case SpvOpImageWrite
: {
3054 const uint32_t value_id
= opcode
== SpvOpAtomicStore
? w
[4] : w
[3];
3055 nir_ssa_def
*value
= vtn_get_nir_ssa(b
, value_id
);
3056 /* nir_intrinsic_image_deref_store always takes a vec4 value */
3057 assert(op
== nir_intrinsic_image_deref_store
);
3058 intrin
->num_components
= 4;
3059 intrin
->src
[3] = nir_src_for_ssa(expand_to_vec4(&b
->nb
, value
));
3060 /* Only OpImageWrite can support a lod parameter if
3061 * SPV_AMD_shader_image_load_store_lod is used but the current NIR
3062 * intrinsics definition for atomics requires us to set it for
3065 intrin
->src
[4] = nir_src_for_ssa(image
.lod
);
3069 case SpvOpAtomicCompareExchange
:
3070 case SpvOpAtomicCompareExchangeWeak
:
3071 case SpvOpAtomicIIncrement
:
3072 case SpvOpAtomicIDecrement
:
3073 case SpvOpAtomicExchange
:
3074 case SpvOpAtomicIAdd
:
3075 case SpvOpAtomicISub
:
3076 case SpvOpAtomicSMin
:
3077 case SpvOpAtomicUMin
:
3078 case SpvOpAtomicSMax
:
3079 case SpvOpAtomicUMax
:
3080 case SpvOpAtomicAnd
:
3082 case SpvOpAtomicXor
:
3083 case SpvOpAtomicFAddEXT
:
3084 fill_common_atomic_sources(b
, opcode
, w
, &intrin
->src
[3]);
3088 vtn_fail_with_opcode("Invalid image opcode", opcode
);
3091 /* Image operations implicitly have the Image storage memory semantics. */
3092 semantics
|= SpvMemorySemanticsImageMemoryMask
;
3094 SpvMemorySemanticsMask before_semantics
;
3095 SpvMemorySemanticsMask after_semantics
;
3096 vtn_split_barrier_semantics(b
, semantics
, &before_semantics
, &after_semantics
);
3098 if (before_semantics
)
3099 vtn_emit_memory_barrier(b
, scope
, before_semantics
);
3101 if (opcode
!= SpvOpImageWrite
&& opcode
!= SpvOpAtomicStore
) {
3102 struct vtn_type
*type
= vtn_get_type(b
, w
[1]);
3104 unsigned dest_components
= glsl_get_vector_elements(type
->type
);
3105 if (nir_intrinsic_infos
[op
].dest_components
== 0)
3106 intrin
->num_components
= dest_components
;
3108 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
,
3109 nir_intrinsic_dest_components(intrin
), 32, NULL
);
3111 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3113 nir_ssa_def
*result
= &intrin
->dest
.ssa
;
3114 if (nir_intrinsic_dest_components(intrin
) != dest_components
)
3115 result
= nir_channels(&b
->nb
, result
, (1 << dest_components
) - 1);
3117 vtn_push_nir_ssa(b
, w
[2], result
);
3119 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3122 if (after_semantics
)
3123 vtn_emit_memory_barrier(b
, scope
, after_semantics
);
3126 static nir_intrinsic_op
3127 get_ssbo_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
3130 case SpvOpAtomicLoad
: return nir_intrinsic_load_ssbo
;
3131 case SpvOpAtomicStore
: return nir_intrinsic_store_ssbo
;
3132 #define OP(S, N) case SpvOp##S: return nir_intrinsic_ssbo_##N;
3133 OP(AtomicExchange
, atomic_exchange
)
3134 OP(AtomicCompareExchange
, atomic_comp_swap
)
3135 OP(AtomicCompareExchangeWeak
, atomic_comp_swap
)
3136 OP(AtomicIIncrement
, atomic_add
)
3137 OP(AtomicIDecrement
, atomic_add
)
3138 OP(AtomicIAdd
, atomic_add
)
3139 OP(AtomicISub
, atomic_add
)
3140 OP(AtomicSMin
, atomic_imin
)
3141 OP(AtomicUMin
, atomic_umin
)
3142 OP(AtomicSMax
, atomic_imax
)
3143 OP(AtomicUMax
, atomic_umax
)
3144 OP(AtomicAnd
, atomic_and
)
3145 OP(AtomicOr
, atomic_or
)
3146 OP(AtomicXor
, atomic_xor
)
3147 OP(AtomicFAddEXT
, atomic_fadd
)
3150 vtn_fail_with_opcode("Invalid SSBO atomic", opcode
);
3154 static nir_intrinsic_op
3155 get_uniform_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
3158 #define OP(S, N) case SpvOp##S: return nir_intrinsic_atomic_counter_ ##N;
3159 OP(AtomicLoad
, read_deref
)
3160 OP(AtomicExchange
, exchange
)
3161 OP(AtomicCompareExchange
, comp_swap
)
3162 OP(AtomicCompareExchangeWeak
, comp_swap
)
3163 OP(AtomicIIncrement
, inc_deref
)
3164 OP(AtomicIDecrement
, post_dec_deref
)
3165 OP(AtomicIAdd
, add_deref
)
3166 OP(AtomicISub
, add_deref
)
3167 OP(AtomicUMin
, min_deref
)
3168 OP(AtomicUMax
, max_deref
)
3169 OP(AtomicAnd
, and_deref
)
3170 OP(AtomicOr
, or_deref
)
3171 OP(AtomicXor
, xor_deref
)
3174 /* We left the following out: AtomicStore, AtomicSMin and
3175 * AtomicSmax. Right now there are not nir intrinsics for them. At this
3176 * moment Atomic Counter support is needed for ARB_spirv support, so is
3177 * only need to support GLSL Atomic Counters that are uints and don't
3178 * allow direct storage.
3180 vtn_fail("Invalid uniform atomic");
3184 static nir_intrinsic_op
3185 get_deref_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
3188 case SpvOpAtomicLoad
: return nir_intrinsic_load_deref
;
3189 case SpvOpAtomicStore
: return nir_intrinsic_store_deref
;
3190 #define OP(S, N) case SpvOp##S: return nir_intrinsic_deref_##N;
3191 OP(AtomicExchange
, atomic_exchange
)
3192 OP(AtomicCompareExchange
, atomic_comp_swap
)
3193 OP(AtomicCompareExchangeWeak
, atomic_comp_swap
)
3194 OP(AtomicIIncrement
, atomic_add
)
3195 OP(AtomicIDecrement
, atomic_add
)
3196 OP(AtomicIAdd
, atomic_add
)
3197 OP(AtomicISub
, atomic_add
)
3198 OP(AtomicSMin
, atomic_imin
)
3199 OP(AtomicUMin
, atomic_umin
)
3200 OP(AtomicSMax
, atomic_imax
)
3201 OP(AtomicUMax
, atomic_umax
)
3202 OP(AtomicAnd
, atomic_and
)
3203 OP(AtomicOr
, atomic_or
)
3204 OP(AtomicXor
, atomic_xor
)
3205 OP(AtomicFAddEXT
, atomic_fadd
)
3208 vtn_fail_with_opcode("Invalid shared atomic", opcode
);
3213 * Handles shared atomics, ssbo atomics and atomic counters.
3216 vtn_handle_atomics(struct vtn_builder
*b
, SpvOp opcode
,
3217 const uint32_t *w
, UNUSED
unsigned count
)
3219 struct vtn_pointer
*ptr
;
3220 nir_intrinsic_instr
*atomic
;
3222 SpvScope scope
= SpvScopeInvocation
;
3223 SpvMemorySemanticsMask semantics
= 0;
3226 case SpvOpAtomicLoad
:
3227 case SpvOpAtomicExchange
:
3228 case SpvOpAtomicCompareExchange
:
3229 case SpvOpAtomicCompareExchangeWeak
:
3230 case SpvOpAtomicIIncrement
:
3231 case SpvOpAtomicIDecrement
:
3232 case SpvOpAtomicIAdd
:
3233 case SpvOpAtomicISub
:
3234 case SpvOpAtomicSMin
:
3235 case SpvOpAtomicUMin
:
3236 case SpvOpAtomicSMax
:
3237 case SpvOpAtomicUMax
:
3238 case SpvOpAtomicAnd
:
3240 case SpvOpAtomicXor
:
3241 case SpvOpAtomicFAddEXT
:
3242 ptr
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
3243 scope
= vtn_constant_uint(b
, w
[4]);
3244 semantics
= vtn_constant_uint(b
, w
[5]);
3247 case SpvOpAtomicStore
:
3248 ptr
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
3249 scope
= vtn_constant_uint(b
, w
[2]);
3250 semantics
= vtn_constant_uint(b
, w
[3]);
3254 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
3257 /* uniform as "atomic counter uniform" */
3258 if (ptr
->mode
== vtn_variable_mode_atomic_counter
) {
3259 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
3260 nir_intrinsic_op op
= get_uniform_nir_atomic_op(b
, opcode
);
3261 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
3262 atomic
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
3264 /* SSBO needs to initialize index/offset. In this case we don't need to,
3265 * as that info is already stored on the ptr->var->var nir_variable (see
3266 * vtn_create_variable)
3270 case SpvOpAtomicLoad
:
3271 case SpvOpAtomicExchange
:
3272 case SpvOpAtomicCompareExchange
:
3273 case SpvOpAtomicCompareExchangeWeak
:
3274 case SpvOpAtomicIIncrement
:
3275 case SpvOpAtomicIDecrement
:
3276 case SpvOpAtomicIAdd
:
3277 case SpvOpAtomicISub
:
3278 case SpvOpAtomicSMin
:
3279 case SpvOpAtomicUMin
:
3280 case SpvOpAtomicSMax
:
3281 case SpvOpAtomicUMax
:
3282 case SpvOpAtomicAnd
:
3284 case SpvOpAtomicXor
:
3285 /* Nothing: we don't need to call fill_common_atomic_sources here, as
3286 * atomic counter uniforms doesn't have sources
3291 unreachable("Invalid SPIR-V atomic");
3294 } else if (vtn_pointer_uses_ssa_offset(b
, ptr
)) {
3295 nir_ssa_def
*offset
, *index
;
3296 offset
= vtn_pointer_to_offset(b
, ptr
, &index
);
3298 assert(ptr
->mode
== vtn_variable_mode_ssbo
);
3300 nir_intrinsic_op op
= get_ssbo_nir_atomic_op(b
, opcode
);
3301 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
3305 case SpvOpAtomicLoad
:
3306 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
3307 nir_intrinsic_set_align(atomic
, 4, 0);
3308 if (ptr
->mode
== vtn_variable_mode_ssbo
)
3309 atomic
->src
[src
++] = nir_src_for_ssa(index
);
3310 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
3313 case SpvOpAtomicStore
:
3314 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
3315 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
3316 nir_intrinsic_set_align(atomic
, 4, 0);
3317 atomic
->src
[src
++] = nir_src_for_ssa(vtn_get_nir_ssa(b
, w
[4]));
3318 if (ptr
->mode
== vtn_variable_mode_ssbo
)
3319 atomic
->src
[src
++] = nir_src_for_ssa(index
);
3320 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
3323 case SpvOpAtomicExchange
:
3324 case SpvOpAtomicCompareExchange
:
3325 case SpvOpAtomicCompareExchangeWeak
:
3326 case SpvOpAtomicIIncrement
:
3327 case SpvOpAtomicIDecrement
:
3328 case SpvOpAtomicIAdd
:
3329 case SpvOpAtomicISub
:
3330 case SpvOpAtomicSMin
:
3331 case SpvOpAtomicUMin
:
3332 case SpvOpAtomicSMax
:
3333 case SpvOpAtomicUMax
:
3334 case SpvOpAtomicAnd
:
3336 case SpvOpAtomicXor
:
3337 case SpvOpAtomicFAddEXT
:
3338 if (ptr
->mode
== vtn_variable_mode_ssbo
)
3339 atomic
->src
[src
++] = nir_src_for_ssa(index
);
3340 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
3341 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[src
]);
3345 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
3348 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
3349 const struct glsl_type
*deref_type
= deref
->type
;
3350 nir_intrinsic_op op
= get_deref_nir_atomic_op(b
, opcode
);
3351 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
3352 atomic
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
3355 case SpvOpAtomicLoad
:
3356 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
3359 case SpvOpAtomicStore
:
3360 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
3361 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
3362 atomic
->src
[1] = nir_src_for_ssa(vtn_get_nir_ssa(b
, w
[4]));
3365 case SpvOpAtomicExchange
:
3366 case SpvOpAtomicCompareExchange
:
3367 case SpvOpAtomicCompareExchangeWeak
:
3368 case SpvOpAtomicIIncrement
:
3369 case SpvOpAtomicIDecrement
:
3370 case SpvOpAtomicIAdd
:
3371 case SpvOpAtomicISub
:
3372 case SpvOpAtomicSMin
:
3373 case SpvOpAtomicUMin
:
3374 case SpvOpAtomicSMax
:
3375 case SpvOpAtomicUMax
:
3376 case SpvOpAtomicAnd
:
3378 case SpvOpAtomicXor
:
3379 case SpvOpAtomicFAddEXT
:
3380 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[1]);
3384 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
3388 /* Atomic ordering operations will implicitly apply to the atomic operation
3389 * storage class, so include that too.
3391 semantics
|= vtn_storage_class_to_memory_semantics(ptr
->ptr_type
->storage_class
);
3393 SpvMemorySemanticsMask before_semantics
;
3394 SpvMemorySemanticsMask after_semantics
;
3395 vtn_split_barrier_semantics(b
, semantics
, &before_semantics
, &after_semantics
);
3397 if (before_semantics
)
3398 vtn_emit_memory_barrier(b
, scope
, before_semantics
);
3400 if (opcode
!= SpvOpAtomicStore
) {
3401 struct vtn_type
*type
= vtn_get_type(b
, w
[1]);
3403 nir_ssa_dest_init(&atomic
->instr
, &atomic
->dest
,
3404 glsl_get_vector_elements(type
->type
),
3405 glsl_get_bit_size(type
->type
), NULL
);
3407 vtn_push_nir_ssa(b
, w
[2], &atomic
->dest
.ssa
);
3410 nir_builder_instr_insert(&b
->nb
, &atomic
->instr
);
3412 if (after_semantics
)
3413 vtn_emit_memory_barrier(b
, scope
, after_semantics
);
3416 static nir_alu_instr
*
3417 create_vec(struct vtn_builder
*b
, unsigned num_components
, unsigned bit_size
)
3419 nir_op op
= nir_op_vec(num_components
);
3420 nir_alu_instr
*vec
= nir_alu_instr_create(b
->shader
, op
);
3421 nir_ssa_dest_init(&vec
->instr
, &vec
->dest
.dest
, num_components
,
3423 vec
->dest
.write_mask
= (1 << num_components
) - 1;
3428 struct vtn_ssa_value
*
3429 vtn_ssa_transpose(struct vtn_builder
*b
, struct vtn_ssa_value
*src
)
3431 if (src
->transposed
)
3432 return src
->transposed
;
3434 struct vtn_ssa_value
*dest
=
3435 vtn_create_ssa_value(b
, glsl_transposed_type(src
->type
));
3437 for (unsigned i
= 0; i
< glsl_get_matrix_columns(dest
->type
); i
++) {
3438 nir_alu_instr
*vec
= create_vec(b
, glsl_get_matrix_columns(src
->type
),
3439 glsl_get_bit_size(src
->type
));
3440 if (glsl_type_is_vector_or_scalar(src
->type
)) {
3441 vec
->src
[0].src
= nir_src_for_ssa(src
->def
);
3442 vec
->src
[0].swizzle
[0] = i
;
3444 for (unsigned j
= 0; j
< glsl_get_matrix_columns(src
->type
); j
++) {
3445 vec
->src
[j
].src
= nir_src_for_ssa(src
->elems
[j
]->def
);
3446 vec
->src
[j
].swizzle
[0] = i
;
3449 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3450 dest
->elems
[i
]->def
= &vec
->dest
.dest
.ssa
;
3453 dest
->transposed
= src
;
3458 static nir_ssa_def
*
3459 vtn_vector_shuffle(struct vtn_builder
*b
, unsigned num_components
,
3460 nir_ssa_def
*src0
, nir_ssa_def
*src1
,
3461 const uint32_t *indices
)
3463 nir_alu_instr
*vec
= create_vec(b
, num_components
, src0
->bit_size
);
3465 for (unsigned i
= 0; i
< num_components
; i
++) {
3466 uint32_t index
= indices
[i
];
3467 if (index
== 0xffffffff) {
3469 nir_src_for_ssa(nir_ssa_undef(&b
->nb
, 1, src0
->bit_size
));
3470 } else if (index
< src0
->num_components
) {
3471 vec
->src
[i
].src
= nir_src_for_ssa(src0
);
3472 vec
->src
[i
].swizzle
[0] = index
;
3474 vec
->src
[i
].src
= nir_src_for_ssa(src1
);
3475 vec
->src
[i
].swizzle
[0] = index
- src0
->num_components
;
3479 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3481 return &vec
->dest
.dest
.ssa
;
3485 * Concatentates a number of vectors/scalars together to produce a vector
3487 static nir_ssa_def
*
3488 vtn_vector_construct(struct vtn_builder
*b
, unsigned num_components
,
3489 unsigned num_srcs
, nir_ssa_def
**srcs
)
3491 nir_alu_instr
*vec
= create_vec(b
, num_components
, srcs
[0]->bit_size
);
3493 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
3495 * "When constructing a vector, there must be at least two Constituent
3498 vtn_assert(num_srcs
>= 2);
3500 unsigned dest_idx
= 0;
3501 for (unsigned i
= 0; i
< num_srcs
; i
++) {
3502 nir_ssa_def
*src
= srcs
[i
];
3503 vtn_assert(dest_idx
+ src
->num_components
<= num_components
);
3504 for (unsigned j
= 0; j
< src
->num_components
; j
++) {
3505 vec
->src
[dest_idx
].src
= nir_src_for_ssa(src
);
3506 vec
->src
[dest_idx
].swizzle
[0] = j
;
3511 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
3513 * "When constructing a vector, the total number of components in all
3514 * the operands must equal the number of components in Result Type."
3516 vtn_assert(dest_idx
== num_components
);
3518 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3520 return &vec
->dest
.dest
.ssa
;
3523 static struct vtn_ssa_value
*
3524 vtn_composite_copy(void *mem_ctx
, struct vtn_ssa_value
*src
)
3526 struct vtn_ssa_value
*dest
= rzalloc(mem_ctx
, struct vtn_ssa_value
);
3527 dest
->type
= src
->type
;
3529 if (glsl_type_is_vector_or_scalar(src
->type
)) {
3530 dest
->def
= src
->def
;
3532 unsigned elems
= glsl_get_length(src
->type
);
3534 dest
->elems
= ralloc_array(mem_ctx
, struct vtn_ssa_value
*, elems
);
3535 for (unsigned i
= 0; i
< elems
; i
++)
3536 dest
->elems
[i
] = vtn_composite_copy(mem_ctx
, src
->elems
[i
]);
3542 static struct vtn_ssa_value
*
3543 vtn_composite_insert(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
3544 struct vtn_ssa_value
*insert
, const uint32_t *indices
,
3545 unsigned num_indices
)
3547 struct vtn_ssa_value
*dest
= vtn_composite_copy(b
, src
);
3549 struct vtn_ssa_value
*cur
= dest
;
3551 for (i
= 0; i
< num_indices
- 1; i
++) {
3552 /* If we got a vector here, that means the next index will be trying to
3553 * dereference a scalar.
3555 vtn_fail_if(glsl_type_is_vector_or_scalar(cur
->type
),
3556 "OpCompositeInsert has too many indices.");
3557 vtn_fail_if(indices
[i
] >= glsl_get_length(cur
->type
),
3558 "All indices in an OpCompositeInsert must be in-bounds");
3559 cur
= cur
->elems
[indices
[i
]];
3562 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
3563 vtn_fail_if(indices
[i
] >= glsl_get_vector_elements(cur
->type
),
3564 "All indices in an OpCompositeInsert must be in-bounds");
3566 /* According to the SPIR-V spec, OpCompositeInsert may work down to
3567 * the component granularity. In that case, the last index will be
3568 * the index to insert the scalar into the vector.
3571 cur
->def
= nir_vector_insert_imm(&b
->nb
, cur
->def
, insert
->def
, indices
[i
]);
3573 vtn_fail_if(indices
[i
] >= glsl_get_length(cur
->type
),
3574 "All indices in an OpCompositeInsert must be in-bounds");
3575 cur
->elems
[indices
[i
]] = insert
;
3581 static struct vtn_ssa_value
*
3582 vtn_composite_extract(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
3583 const uint32_t *indices
, unsigned num_indices
)
3585 struct vtn_ssa_value
*cur
= src
;
3586 for (unsigned i
= 0; i
< num_indices
; i
++) {
3587 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
3588 vtn_assert(i
== num_indices
- 1);
3589 vtn_fail_if(indices
[i
] >= glsl_get_vector_elements(cur
->type
),
3590 "All indices in an OpCompositeExtract must be in-bounds");
3592 /* According to the SPIR-V spec, OpCompositeExtract may work down to
3593 * the component granularity. The last index will be the index of the
3594 * vector to extract.
3597 const struct glsl_type
*scalar_type
=
3598 glsl_scalar_type(glsl_get_base_type(cur
->type
));
3599 struct vtn_ssa_value
*ret
= vtn_create_ssa_value(b
, scalar_type
);
3600 ret
->def
= nir_channel(&b
->nb
, cur
->def
, indices
[i
]);
3603 vtn_fail_if(indices
[i
] >= glsl_get_length(cur
->type
),
3604 "All indices in an OpCompositeExtract must be in-bounds");
3605 cur
= cur
->elems
[indices
[i
]];
3613 vtn_handle_composite(struct vtn_builder
*b
, SpvOp opcode
,
3614 const uint32_t *w
, unsigned count
)
3616 struct vtn_type
*type
= vtn_get_type(b
, w
[1]);
3617 struct vtn_ssa_value
*ssa
= vtn_create_ssa_value(b
, type
->type
);
3620 case SpvOpVectorExtractDynamic
:
3621 ssa
->def
= nir_vector_extract(&b
->nb
, vtn_get_nir_ssa(b
, w
[3]),
3622 vtn_get_nir_ssa(b
, w
[4]));
3625 case SpvOpVectorInsertDynamic
:
3626 ssa
->def
= nir_vector_insert(&b
->nb
, vtn_get_nir_ssa(b
, w
[3]),
3627 vtn_get_nir_ssa(b
, w
[4]),
3628 vtn_get_nir_ssa(b
, w
[5]));
3631 case SpvOpVectorShuffle
:
3632 ssa
->def
= vtn_vector_shuffle(b
, glsl_get_vector_elements(type
->type
),
3633 vtn_get_nir_ssa(b
, w
[3]),
3634 vtn_get_nir_ssa(b
, w
[4]),
3638 case SpvOpCompositeConstruct
: {
3639 unsigned elems
= count
- 3;
3641 if (glsl_type_is_vector_or_scalar(type
->type
)) {
3642 nir_ssa_def
*srcs
[NIR_MAX_VEC_COMPONENTS
];
3643 for (unsigned i
= 0; i
< elems
; i
++)
3644 srcs
[i
] = vtn_get_nir_ssa(b
, w
[3 + i
]);
3646 vtn_vector_construct(b
, glsl_get_vector_elements(type
->type
),
3649 ssa
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
3650 for (unsigned i
= 0; i
< elems
; i
++)
3651 ssa
->elems
[i
] = vtn_ssa_value(b
, w
[3 + i
]);
3655 case SpvOpCompositeExtract
:
3656 ssa
= vtn_composite_extract(b
, vtn_ssa_value(b
, w
[3]),
3660 case SpvOpCompositeInsert
:
3661 ssa
= vtn_composite_insert(b
, vtn_ssa_value(b
, w
[4]),
3662 vtn_ssa_value(b
, w
[3]),
3666 case SpvOpCopyLogical
:
3667 ssa
= vtn_composite_copy(b
, vtn_ssa_value(b
, w
[3]));
3669 case SpvOpCopyObject
:
3670 vtn_copy_value(b
, w
[3], w
[2]);
3674 vtn_fail_with_opcode("unknown composite operation", opcode
);
3677 vtn_push_ssa_value(b
, w
[2], ssa
);
3681 vtn_emit_barrier(struct vtn_builder
*b
, nir_intrinsic_op op
)
3683 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
3684 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3688 vtn_emit_memory_barrier(struct vtn_builder
*b
, SpvScope scope
,
3689 SpvMemorySemanticsMask semantics
)
3691 if (b
->shader
->options
->use_scoped_barrier
) {
3692 vtn_emit_scoped_memory_barrier(b
, scope
, semantics
);
3696 static const SpvMemorySemanticsMask all_memory_semantics
=
3697 SpvMemorySemanticsUniformMemoryMask
|
3698 SpvMemorySemanticsWorkgroupMemoryMask
|
3699 SpvMemorySemanticsAtomicCounterMemoryMask
|
3700 SpvMemorySemanticsImageMemoryMask
|
3701 SpvMemorySemanticsOutputMemoryMask
;
3703 /* If we're not actually doing a memory barrier, bail */
3704 if (!(semantics
& all_memory_semantics
))
3707 /* GL and Vulkan don't have these */
3708 vtn_assert(scope
!= SpvScopeCrossDevice
);
3710 if (scope
== SpvScopeSubgroup
)
3711 return; /* Nothing to do here */
3713 if (scope
== SpvScopeWorkgroup
) {
3714 vtn_emit_barrier(b
, nir_intrinsic_group_memory_barrier
);
3718 /* There's only two scopes thing left */
3719 vtn_assert(scope
== SpvScopeInvocation
|| scope
== SpvScopeDevice
);
3721 /* Map the GLSL memoryBarrier() construct and any barriers with more than one
3722 * semantic to the corresponding NIR one.
3724 if (util_bitcount(semantics
& all_memory_semantics
) > 1) {
3725 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier
);
3726 if (semantics
& SpvMemorySemanticsOutputMemoryMask
) {
3727 /* GLSL memoryBarrier() (and the corresponding NIR one) doesn't include
3728 * TCS outputs, so we have to emit it's own intrinsic for that. We
3729 * then need to emit another memory_barrier to prevent moving
3730 * non-output operations to before the tcs_patch barrier.
3732 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_tcs_patch
);
3733 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier
);
3738 /* Issue a more specific barrier */
3739 switch (semantics
& all_memory_semantics
) {
3740 case SpvMemorySemanticsUniformMemoryMask
:
3741 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_buffer
);
3743 case SpvMemorySemanticsWorkgroupMemoryMask
:
3744 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_shared
);
3746 case SpvMemorySemanticsAtomicCounterMemoryMask
:
3747 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_atomic_counter
);
3749 case SpvMemorySemanticsImageMemoryMask
:
3750 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_image
);
3752 case SpvMemorySemanticsOutputMemoryMask
:
3753 if (b
->nb
.shader
->info
.stage
== MESA_SHADER_TESS_CTRL
)
3754 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_tcs_patch
);
3762 vtn_handle_barrier(struct vtn_builder
*b
, SpvOp opcode
,
3763 const uint32_t *w
, UNUSED
unsigned count
)
3766 case SpvOpEmitVertex
:
3767 case SpvOpEmitStreamVertex
:
3768 case SpvOpEndPrimitive
:
3769 case SpvOpEndStreamPrimitive
: {
3770 nir_intrinsic_op intrinsic_op
;
3772 case SpvOpEmitVertex
:
3773 case SpvOpEmitStreamVertex
:
3774 intrinsic_op
= nir_intrinsic_emit_vertex
;
3776 case SpvOpEndPrimitive
:
3777 case SpvOpEndStreamPrimitive
:
3778 intrinsic_op
= nir_intrinsic_end_primitive
;
3781 unreachable("Invalid opcode");
3784 nir_intrinsic_instr
*intrin
=
3785 nir_intrinsic_instr_create(b
->shader
, intrinsic_op
);
3788 case SpvOpEmitStreamVertex
:
3789 case SpvOpEndStreamPrimitive
: {
3790 unsigned stream
= vtn_constant_uint(b
, w
[1]);
3791 nir_intrinsic_set_stream_id(intrin
, stream
);
3799 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3803 case SpvOpMemoryBarrier
: {
3804 SpvScope scope
= vtn_constant_uint(b
, w
[1]);
3805 SpvMemorySemanticsMask semantics
= vtn_constant_uint(b
, w
[2]);
3806 vtn_emit_memory_barrier(b
, scope
, semantics
);
3810 case SpvOpControlBarrier
: {
3811 SpvScope execution_scope
= vtn_constant_uint(b
, w
[1]);
3812 SpvScope memory_scope
= vtn_constant_uint(b
, w
[2]);
3813 SpvMemorySemanticsMask memory_semantics
= vtn_constant_uint(b
, w
[3]);
3815 /* GLSLang, prior to commit 8297936dd6eb3, emitted OpControlBarrier with
3816 * memory semantics of None for GLSL barrier().
3817 * And before that, prior to c3f1cdfa, emitted the OpControlBarrier with
3818 * Device instead of Workgroup for execution scope.
3820 if (b
->wa_glslang_cs_barrier
&&
3821 b
->nb
.shader
->info
.stage
== MESA_SHADER_COMPUTE
&&
3822 (execution_scope
== SpvScopeWorkgroup
||
3823 execution_scope
== SpvScopeDevice
) &&
3824 memory_semantics
== SpvMemorySemanticsMaskNone
) {
3825 execution_scope
= SpvScopeWorkgroup
;
3826 memory_scope
= SpvScopeWorkgroup
;
3827 memory_semantics
= SpvMemorySemanticsAcquireReleaseMask
|
3828 SpvMemorySemanticsWorkgroupMemoryMask
;
3831 /* From the SPIR-V spec:
3833 * "When used with the TessellationControl execution model, it also
3834 * implicitly synchronizes the Output Storage Class: Writes to Output
3835 * variables performed by any invocation executed prior to a
3836 * OpControlBarrier will be visible to any other invocation after
3837 * return from that OpControlBarrier."
3839 if (b
->nb
.shader
->info
.stage
== MESA_SHADER_TESS_CTRL
) {
3840 memory_semantics
&= ~(SpvMemorySemanticsAcquireMask
|
3841 SpvMemorySemanticsReleaseMask
|
3842 SpvMemorySemanticsAcquireReleaseMask
|
3843 SpvMemorySemanticsSequentiallyConsistentMask
);
3844 memory_semantics
|= SpvMemorySemanticsAcquireReleaseMask
|
3845 SpvMemorySemanticsOutputMemoryMask
;
3848 if (b
->shader
->options
->use_scoped_barrier
) {
3849 vtn_emit_scoped_control_barrier(b
, execution_scope
, memory_scope
,
3852 vtn_emit_memory_barrier(b
, memory_scope
, memory_semantics
);
3854 if (execution_scope
== SpvScopeWorkgroup
)
3855 vtn_emit_barrier(b
, nir_intrinsic_control_barrier
);
3861 unreachable("unknown barrier instruction");
3866 gl_primitive_from_spv_execution_mode(struct vtn_builder
*b
,
3867 SpvExecutionMode mode
)
3870 case SpvExecutionModeInputPoints
:
3871 case SpvExecutionModeOutputPoints
:
3872 return 0; /* GL_POINTS */
3873 case SpvExecutionModeInputLines
:
3874 return 1; /* GL_LINES */
3875 case SpvExecutionModeInputLinesAdjacency
:
3876 return 0x000A; /* GL_LINE_STRIP_ADJACENCY_ARB */
3877 case SpvExecutionModeTriangles
:
3878 return 4; /* GL_TRIANGLES */
3879 case SpvExecutionModeInputTrianglesAdjacency
:
3880 return 0x000C; /* GL_TRIANGLES_ADJACENCY_ARB */
3881 case SpvExecutionModeQuads
:
3882 return 7; /* GL_QUADS */
3883 case SpvExecutionModeIsolines
:
3884 return 0x8E7A; /* GL_ISOLINES */
3885 case SpvExecutionModeOutputLineStrip
:
3886 return 3; /* GL_LINE_STRIP */
3887 case SpvExecutionModeOutputTriangleStrip
:
3888 return 5; /* GL_TRIANGLE_STRIP */
3890 vtn_fail("Invalid primitive type: %s (%u)",
3891 spirv_executionmode_to_string(mode
), mode
);
3896 vertices_in_from_spv_execution_mode(struct vtn_builder
*b
,
3897 SpvExecutionMode mode
)
3900 case SpvExecutionModeInputPoints
:
3902 case SpvExecutionModeInputLines
:
3904 case SpvExecutionModeInputLinesAdjacency
:
3906 case SpvExecutionModeTriangles
:
3908 case SpvExecutionModeInputTrianglesAdjacency
:
3911 vtn_fail("Invalid GS input mode: %s (%u)",
3912 spirv_executionmode_to_string(mode
), mode
);
3916 static gl_shader_stage
3917 stage_for_execution_model(struct vtn_builder
*b
, SpvExecutionModel model
)
3920 case SpvExecutionModelVertex
:
3921 return MESA_SHADER_VERTEX
;
3922 case SpvExecutionModelTessellationControl
:
3923 return MESA_SHADER_TESS_CTRL
;
3924 case SpvExecutionModelTessellationEvaluation
:
3925 return MESA_SHADER_TESS_EVAL
;
3926 case SpvExecutionModelGeometry
:
3927 return MESA_SHADER_GEOMETRY
;
3928 case SpvExecutionModelFragment
:
3929 return MESA_SHADER_FRAGMENT
;
3930 case SpvExecutionModelGLCompute
:
3931 return MESA_SHADER_COMPUTE
;
3932 case SpvExecutionModelKernel
:
3933 return MESA_SHADER_KERNEL
;
3935 vtn_fail("Unsupported execution model: %s (%u)",
3936 spirv_executionmodel_to_string(model
), model
);
3940 #define spv_check_supported(name, cap) do { \
3941 if (!(b->options && b->options->caps.name)) \
3942 vtn_warn("Unsupported SPIR-V capability: %s (%u)", \
3943 spirv_capability_to_string(cap), cap); \
3948 vtn_handle_entry_point(struct vtn_builder
*b
, const uint32_t *w
,
3951 struct vtn_value
*entry_point
= &b
->values
[w
[2]];
3952 /* Let this be a name label regardless */
3953 unsigned name_words
;
3954 entry_point
->name
= vtn_string_literal(b
, &w
[3], count
- 3, &name_words
);
3956 if (strcmp(entry_point
->name
, b
->entry_point_name
) != 0 ||
3957 stage_for_execution_model(b
, w
[1]) != b
->entry_point_stage
)
3960 vtn_assert(b
->entry_point
== NULL
);
3961 b
->entry_point
= entry_point
;
3965 vtn_handle_preamble_instruction(struct vtn_builder
*b
, SpvOp opcode
,
3966 const uint32_t *w
, unsigned count
)
3973 case SpvSourceLanguageUnknown
: lang
= "unknown"; break;
3974 case SpvSourceLanguageESSL
: lang
= "ESSL"; break;
3975 case SpvSourceLanguageGLSL
: lang
= "GLSL"; break;
3976 case SpvSourceLanguageOpenCL_C
: lang
= "OpenCL C"; break;
3977 case SpvSourceLanguageOpenCL_CPP
: lang
= "OpenCL C++"; break;
3978 case SpvSourceLanguageHLSL
: lang
= "HLSL"; break;
3981 uint32_t version
= w
[2];
3984 (count
> 3) ? vtn_value(b
, w
[3], vtn_value_type_string
)->str
: "";
3986 vtn_info("Parsing SPIR-V from %s %u source file %s", lang
, version
, file
);
3990 case SpvOpSourceExtension
:
3991 case SpvOpSourceContinued
:
3992 case SpvOpExtension
:
3993 case SpvOpModuleProcessed
:
3994 /* Unhandled, but these are for debug so that's ok. */
3997 case SpvOpCapability
: {
3998 SpvCapability cap
= w
[1];
4000 case SpvCapabilityMatrix
:
4001 case SpvCapabilityShader
:
4002 case SpvCapabilityGeometry
:
4003 case SpvCapabilityGeometryPointSize
:
4004 case SpvCapabilityUniformBufferArrayDynamicIndexing
:
4005 case SpvCapabilitySampledImageArrayDynamicIndexing
:
4006 case SpvCapabilityStorageBufferArrayDynamicIndexing
:
4007 case SpvCapabilityStorageImageArrayDynamicIndexing
:
4008 case SpvCapabilityImageRect
:
4009 case SpvCapabilitySampledRect
:
4010 case SpvCapabilitySampled1D
:
4011 case SpvCapabilityImage1D
:
4012 case SpvCapabilitySampledCubeArray
:
4013 case SpvCapabilityImageCubeArray
:
4014 case SpvCapabilitySampledBuffer
:
4015 case SpvCapabilityImageBuffer
:
4016 case SpvCapabilityImageQuery
:
4017 case SpvCapabilityDerivativeControl
:
4018 case SpvCapabilityInterpolationFunction
:
4019 case SpvCapabilityMultiViewport
:
4020 case SpvCapabilitySampleRateShading
:
4021 case SpvCapabilityClipDistance
:
4022 case SpvCapabilityCullDistance
:
4023 case SpvCapabilityInputAttachment
:
4024 case SpvCapabilityImageGatherExtended
:
4025 case SpvCapabilityStorageImageExtendedFormats
:
4026 case SpvCapabilityVector16
:
4029 case SpvCapabilityLinkage
:
4030 case SpvCapabilityFloat16Buffer
:
4031 case SpvCapabilitySparseResidency
:
4032 vtn_warn("Unsupported SPIR-V capability: %s",
4033 spirv_capability_to_string(cap
));
4036 case SpvCapabilityMinLod
:
4037 spv_check_supported(min_lod
, cap
);
4040 case SpvCapabilityAtomicStorage
:
4041 spv_check_supported(atomic_storage
, cap
);
4044 case SpvCapabilityFloat64
:
4045 spv_check_supported(float64
, cap
);
4047 case SpvCapabilityInt64
:
4048 spv_check_supported(int64
, cap
);
4050 case SpvCapabilityInt16
:
4051 spv_check_supported(int16
, cap
);
4053 case SpvCapabilityInt8
:
4054 spv_check_supported(int8
, cap
);
4057 case SpvCapabilityTransformFeedback
:
4058 spv_check_supported(transform_feedback
, cap
);
4061 case SpvCapabilityGeometryStreams
:
4062 spv_check_supported(geometry_streams
, cap
);
4065 case SpvCapabilityInt64Atomics
:
4066 spv_check_supported(int64_atomics
, cap
);
4069 case SpvCapabilityStorageImageMultisample
:
4070 spv_check_supported(storage_image_ms
, cap
);
4073 case SpvCapabilityAddresses
:
4074 spv_check_supported(address
, cap
);
4077 case SpvCapabilityKernel
:
4078 spv_check_supported(kernel
, cap
);
4081 case SpvCapabilityImageBasic
:
4082 case SpvCapabilityImageReadWrite
:
4083 case SpvCapabilityImageMipmap
:
4084 case SpvCapabilityPipes
:
4085 case SpvCapabilityDeviceEnqueue
:
4086 case SpvCapabilityLiteralSampler
:
4087 case SpvCapabilityGenericPointer
:
4088 vtn_warn("Unsupported OpenCL-style SPIR-V capability: %s",
4089 spirv_capability_to_string(cap
));
4092 case SpvCapabilityImageMSArray
:
4093 spv_check_supported(image_ms_array
, cap
);
4096 case SpvCapabilityTessellation
:
4097 case SpvCapabilityTessellationPointSize
:
4098 spv_check_supported(tessellation
, cap
);
4101 case SpvCapabilityDrawParameters
:
4102 spv_check_supported(draw_parameters
, cap
);
4105 case SpvCapabilityStorageImageReadWithoutFormat
:
4106 spv_check_supported(image_read_without_format
, cap
);
4109 case SpvCapabilityStorageImageWriteWithoutFormat
:
4110 spv_check_supported(image_write_without_format
, cap
);
4113 case SpvCapabilityDeviceGroup
:
4114 spv_check_supported(device_group
, cap
);
4117 case SpvCapabilityMultiView
:
4118 spv_check_supported(multiview
, cap
);
4121 case SpvCapabilityGroupNonUniform
:
4122 spv_check_supported(subgroup_basic
, cap
);
4125 case SpvCapabilitySubgroupVoteKHR
:
4126 case SpvCapabilityGroupNonUniformVote
:
4127 spv_check_supported(subgroup_vote
, cap
);
4130 case SpvCapabilitySubgroupBallotKHR
:
4131 case SpvCapabilityGroupNonUniformBallot
:
4132 spv_check_supported(subgroup_ballot
, cap
);
4135 case SpvCapabilityGroupNonUniformShuffle
:
4136 case SpvCapabilityGroupNonUniformShuffleRelative
:
4137 spv_check_supported(subgroup_shuffle
, cap
);
4140 case SpvCapabilityGroupNonUniformQuad
:
4141 spv_check_supported(subgroup_quad
, cap
);
4144 case SpvCapabilityGroupNonUniformArithmetic
:
4145 case SpvCapabilityGroupNonUniformClustered
:
4146 spv_check_supported(subgroup_arithmetic
, cap
);
4149 case SpvCapabilityGroups
:
4150 spv_check_supported(amd_shader_ballot
, cap
);
4153 case SpvCapabilityVariablePointersStorageBuffer
:
4154 case SpvCapabilityVariablePointers
:
4155 spv_check_supported(variable_pointers
, cap
);
4156 b
->variable_pointers
= true;
4159 case SpvCapabilityStorageUniformBufferBlock16
:
4160 case SpvCapabilityStorageUniform16
:
4161 case SpvCapabilityStoragePushConstant16
:
4162 case SpvCapabilityStorageInputOutput16
:
4163 spv_check_supported(storage_16bit
, cap
);
4166 case SpvCapabilityShaderLayer
:
4167 case SpvCapabilityShaderViewportIndex
:
4168 case SpvCapabilityShaderViewportIndexLayerEXT
:
4169 spv_check_supported(shader_viewport_index_layer
, cap
);
4172 case SpvCapabilityStorageBuffer8BitAccess
:
4173 case SpvCapabilityUniformAndStorageBuffer8BitAccess
:
4174 case SpvCapabilityStoragePushConstant8
:
4175 spv_check_supported(storage_8bit
, cap
);
4178 case SpvCapabilityShaderNonUniformEXT
:
4179 spv_check_supported(descriptor_indexing
, cap
);
4182 case SpvCapabilityInputAttachmentArrayDynamicIndexingEXT
:
4183 case SpvCapabilityUniformTexelBufferArrayDynamicIndexingEXT
:
4184 case SpvCapabilityStorageTexelBufferArrayDynamicIndexingEXT
:
4185 spv_check_supported(descriptor_array_dynamic_indexing
, cap
);
4188 case SpvCapabilityUniformBufferArrayNonUniformIndexingEXT
:
4189 case SpvCapabilitySampledImageArrayNonUniformIndexingEXT
:
4190 case SpvCapabilityStorageBufferArrayNonUniformIndexingEXT
:
4191 case SpvCapabilityStorageImageArrayNonUniformIndexingEXT
:
4192 case SpvCapabilityInputAttachmentArrayNonUniformIndexingEXT
:
4193 case SpvCapabilityUniformTexelBufferArrayNonUniformIndexingEXT
:
4194 case SpvCapabilityStorageTexelBufferArrayNonUniformIndexingEXT
:
4195 spv_check_supported(descriptor_array_non_uniform_indexing
, cap
);
4198 case SpvCapabilityRuntimeDescriptorArrayEXT
:
4199 spv_check_supported(runtime_descriptor_array
, cap
);
4202 case SpvCapabilityStencilExportEXT
:
4203 spv_check_supported(stencil_export
, cap
);
4206 case SpvCapabilitySampleMaskPostDepthCoverage
:
4207 spv_check_supported(post_depth_coverage
, cap
);
4210 case SpvCapabilityDenormFlushToZero
:
4211 case SpvCapabilityDenormPreserve
:
4212 case SpvCapabilitySignedZeroInfNanPreserve
:
4213 case SpvCapabilityRoundingModeRTE
:
4214 case SpvCapabilityRoundingModeRTZ
:
4215 spv_check_supported(float_controls
, cap
);
4218 case SpvCapabilityPhysicalStorageBufferAddresses
:
4219 spv_check_supported(physical_storage_buffer_address
, cap
);
4222 case SpvCapabilityComputeDerivativeGroupQuadsNV
:
4223 case SpvCapabilityComputeDerivativeGroupLinearNV
:
4224 spv_check_supported(derivative_group
, cap
);
4227 case SpvCapabilityFloat16
:
4228 spv_check_supported(float16
, cap
);
4231 case SpvCapabilityFragmentShaderSampleInterlockEXT
:
4232 spv_check_supported(fragment_shader_sample_interlock
, cap
);
4235 case SpvCapabilityFragmentShaderPixelInterlockEXT
:
4236 spv_check_supported(fragment_shader_pixel_interlock
, cap
);
4239 case SpvCapabilityDemoteToHelperInvocationEXT
:
4240 spv_check_supported(demote_to_helper_invocation
, cap
);
4243 case SpvCapabilityShaderClockKHR
:
4244 spv_check_supported(shader_clock
, cap
);
4247 case SpvCapabilityVulkanMemoryModel
:
4248 spv_check_supported(vk_memory_model
, cap
);
4251 case SpvCapabilityVulkanMemoryModelDeviceScope
:
4252 spv_check_supported(vk_memory_model_device_scope
, cap
);
4255 case SpvCapabilityImageReadWriteLodAMD
:
4256 spv_check_supported(amd_image_read_write_lod
, cap
);
4259 case SpvCapabilityIntegerFunctions2INTEL
:
4260 spv_check_supported(integer_functions2
, cap
);
4263 case SpvCapabilityFragmentMaskAMD
:
4264 spv_check_supported(amd_fragment_mask
, cap
);
4267 case SpvCapabilityImageGatherBiasLodAMD
:
4268 spv_check_supported(amd_image_gather_bias_lod
, cap
);
4271 case SpvCapabilityAtomicFloat32AddEXT
:
4272 spv_check_supported(float32_atomic_add
, cap
);
4275 case SpvCapabilityAtomicFloat64AddEXT
:
4276 spv_check_supported(float64_atomic_add
, cap
);
4280 vtn_fail("Unhandled capability: %s (%u)",
4281 spirv_capability_to_string(cap
), cap
);
4286 case SpvOpExtInstImport
:
4287 vtn_handle_extension(b
, opcode
, w
, count
);
4290 case SpvOpMemoryModel
:
4292 case SpvAddressingModelPhysical32
:
4293 vtn_fail_if(b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
,
4294 "AddressingModelPhysical32 only supported for kernels");
4295 b
->shader
->info
.cs
.ptr_size
= 32;
4296 b
->physical_ptrs
= true;
4297 b
->options
->shared_addr_format
= nir_address_format_32bit_global
;
4298 b
->options
->global_addr_format
= nir_address_format_32bit_global
;
4299 b
->options
->temp_addr_format
= nir_address_format_32bit_global
;
4301 case SpvAddressingModelPhysical64
:
4302 vtn_fail_if(b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
,
4303 "AddressingModelPhysical64 only supported for kernels");
4304 b
->shader
->info
.cs
.ptr_size
= 64;
4305 b
->physical_ptrs
= true;
4306 b
->options
->shared_addr_format
= nir_address_format_64bit_global
;
4307 b
->options
->global_addr_format
= nir_address_format_64bit_global
;
4308 b
->options
->temp_addr_format
= nir_address_format_64bit_global
;
4310 case SpvAddressingModelLogical
:
4311 vtn_fail_if(b
->shader
->info
.stage
== MESA_SHADER_KERNEL
,
4312 "AddressingModelLogical only supported for shaders");
4313 b
->physical_ptrs
= false;
4315 case SpvAddressingModelPhysicalStorageBuffer64
:
4316 vtn_fail_if(!b
->options
||
4317 !b
->options
->caps
.physical_storage_buffer_address
,
4318 "AddressingModelPhysicalStorageBuffer64 not supported");
4321 vtn_fail("Unknown addressing model: %s (%u)",
4322 spirv_addressingmodel_to_string(w
[1]), w
[1]);
4326 b
->mem_model
= w
[2];
4328 case SpvMemoryModelSimple
:
4329 case SpvMemoryModelGLSL450
:
4330 case SpvMemoryModelOpenCL
:
4332 case SpvMemoryModelVulkan
:
4333 vtn_fail_if(!b
->options
->caps
.vk_memory_model
,
4334 "Vulkan memory model is unsupported by this driver");
4337 vtn_fail("Unsupported memory model: %s",
4338 spirv_memorymodel_to_string(w
[2]));
4343 case SpvOpEntryPoint
:
4344 vtn_handle_entry_point(b
, w
, count
);
4348 vtn_push_value(b
, w
[1], vtn_value_type_string
)->str
=
4349 vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
4353 b
->values
[w
[1]].name
= vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
4356 case SpvOpMemberName
:
4360 case SpvOpExecutionMode
:
4361 case SpvOpExecutionModeId
:
4362 case SpvOpDecorationGroup
:
4364 case SpvOpDecorateId
:
4365 case SpvOpMemberDecorate
:
4366 case SpvOpGroupDecorate
:
4367 case SpvOpGroupMemberDecorate
:
4368 case SpvOpDecorateString
:
4369 case SpvOpMemberDecorateString
:
4370 vtn_handle_decoration(b
, opcode
, w
, count
);
4373 case SpvOpExtInst
: {
4374 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
4375 if (val
->ext_handler
== vtn_handle_non_semantic_instruction
) {
4376 /* NonSemantic extended instructions are acceptable in preamble. */
4377 vtn_handle_non_semantic_instruction(b
, w
[4], w
, count
);
4380 return false; /* End of preamble. */
4385 return false; /* End of preamble */
4392 vtn_handle_execution_mode(struct vtn_builder
*b
, struct vtn_value
*entry_point
,
4393 const struct vtn_decoration
*mode
, UNUSED
void *data
)
4395 vtn_assert(b
->entry_point
== entry_point
);
4397 switch(mode
->exec_mode
) {
4398 case SpvExecutionModeOriginUpperLeft
:
4399 case SpvExecutionModeOriginLowerLeft
:
4400 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4401 b
->shader
->info
.fs
.origin_upper_left
=
4402 (mode
->exec_mode
== SpvExecutionModeOriginUpperLeft
);
4405 case SpvExecutionModeEarlyFragmentTests
:
4406 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4407 b
->shader
->info
.fs
.early_fragment_tests
= true;
4410 case SpvExecutionModePostDepthCoverage
:
4411 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4412 b
->shader
->info
.fs
.post_depth_coverage
= true;
4415 case SpvExecutionModeInvocations
:
4416 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4417 b
->shader
->info
.gs
.invocations
= MAX2(1, mode
->operands
[0]);
4420 case SpvExecutionModeDepthReplacing
:
4421 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4422 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_ANY
;
4424 case SpvExecutionModeDepthGreater
:
4425 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4426 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_GREATER
;
4428 case SpvExecutionModeDepthLess
:
4429 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4430 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_LESS
;
4432 case SpvExecutionModeDepthUnchanged
:
4433 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4434 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
4437 case SpvExecutionModeLocalSize
:
4438 vtn_assert(gl_shader_stage_is_compute(b
->shader
->info
.stage
));
4439 b
->shader
->info
.cs
.local_size
[0] = mode
->operands
[0];
4440 b
->shader
->info
.cs
.local_size
[1] = mode
->operands
[1];
4441 b
->shader
->info
.cs
.local_size
[2] = mode
->operands
[2];
4444 case SpvExecutionModeLocalSizeId
:
4445 b
->shader
->info
.cs
.local_size
[0] = vtn_constant_uint(b
, mode
->operands
[0]);
4446 b
->shader
->info
.cs
.local_size
[1] = vtn_constant_uint(b
, mode
->operands
[1]);
4447 b
->shader
->info
.cs
.local_size
[2] = vtn_constant_uint(b
, mode
->operands
[2]);
4450 case SpvExecutionModeLocalSizeHint
:
4451 case SpvExecutionModeLocalSizeHintId
:
4452 break; /* Nothing to do with this */
4454 case SpvExecutionModeOutputVertices
:
4455 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4456 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
4457 b
->shader
->info
.tess
.tcs_vertices_out
= mode
->operands
[0];
4459 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4460 b
->shader
->info
.gs
.vertices_out
= mode
->operands
[0];
4464 case SpvExecutionModeInputPoints
:
4465 case SpvExecutionModeInputLines
:
4466 case SpvExecutionModeInputLinesAdjacency
:
4467 case SpvExecutionModeTriangles
:
4468 case SpvExecutionModeInputTrianglesAdjacency
:
4469 case SpvExecutionModeQuads
:
4470 case SpvExecutionModeIsolines
:
4471 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4472 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
4473 b
->shader
->info
.tess
.primitive_mode
=
4474 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
4476 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4477 b
->shader
->info
.gs
.vertices_in
=
4478 vertices_in_from_spv_execution_mode(b
, mode
->exec_mode
);
4479 b
->shader
->info
.gs
.input_primitive
=
4480 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
4484 case SpvExecutionModeOutputPoints
:
4485 case SpvExecutionModeOutputLineStrip
:
4486 case SpvExecutionModeOutputTriangleStrip
:
4487 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4488 b
->shader
->info
.gs
.output_primitive
=
4489 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
4492 case SpvExecutionModeSpacingEqual
:
4493 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4494 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4495 b
->shader
->info
.tess
.spacing
= TESS_SPACING_EQUAL
;
4497 case SpvExecutionModeSpacingFractionalEven
:
4498 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4499 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4500 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_EVEN
;
4502 case SpvExecutionModeSpacingFractionalOdd
:
4503 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4504 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4505 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_ODD
;
4507 case SpvExecutionModeVertexOrderCw
:
4508 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4509 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4510 b
->shader
->info
.tess
.ccw
= false;
4512 case SpvExecutionModeVertexOrderCcw
:
4513 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4514 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4515 b
->shader
->info
.tess
.ccw
= true;
4517 case SpvExecutionModePointMode
:
4518 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4519 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4520 b
->shader
->info
.tess
.point_mode
= true;
4523 case SpvExecutionModePixelCenterInteger
:
4524 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4525 b
->shader
->info
.fs
.pixel_center_integer
= true;
4528 case SpvExecutionModeXfb
:
4529 b
->shader
->info
.has_transform_feedback_varyings
= true;
4532 case SpvExecutionModeVecTypeHint
:
4535 case SpvExecutionModeContractionOff
:
4536 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
)
4537 vtn_warn("ExectionMode only allowed for CL-style kernels: %s",
4538 spirv_executionmode_to_string(mode
->exec_mode
));
4543 case SpvExecutionModeStencilRefReplacingEXT
:
4544 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4547 case SpvExecutionModeDerivativeGroupQuadsNV
:
4548 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_COMPUTE
);
4549 b
->shader
->info
.cs
.derivative_group
= DERIVATIVE_GROUP_QUADS
;
4552 case SpvExecutionModeDerivativeGroupLinearNV
:
4553 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_COMPUTE
);
4554 b
->shader
->info
.cs
.derivative_group
= DERIVATIVE_GROUP_LINEAR
;
4557 case SpvExecutionModePixelInterlockOrderedEXT
:
4558 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4559 b
->shader
->info
.fs
.pixel_interlock_ordered
= true;
4562 case SpvExecutionModePixelInterlockUnorderedEXT
:
4563 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4564 b
->shader
->info
.fs
.pixel_interlock_unordered
= true;
4567 case SpvExecutionModeSampleInterlockOrderedEXT
:
4568 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4569 b
->shader
->info
.fs
.sample_interlock_ordered
= true;
4572 case SpvExecutionModeSampleInterlockUnorderedEXT
:
4573 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4574 b
->shader
->info
.fs
.sample_interlock_unordered
= true;
4577 case SpvExecutionModeDenormPreserve
:
4578 case SpvExecutionModeDenormFlushToZero
:
4579 case SpvExecutionModeSignedZeroInfNanPreserve
:
4580 case SpvExecutionModeRoundingModeRTE
:
4581 case SpvExecutionModeRoundingModeRTZ
:
4582 /* Already handled in vtn_handle_rounding_mode_in_execution_mode() */
4586 vtn_fail("Unhandled execution mode: %s (%u)",
4587 spirv_executionmode_to_string(mode
->exec_mode
),
4593 vtn_handle_rounding_mode_in_execution_mode(struct vtn_builder
*b
, struct vtn_value
*entry_point
,
4594 const struct vtn_decoration
*mode
, void *data
)
4596 vtn_assert(b
->entry_point
== entry_point
);
4598 unsigned execution_mode
= 0;
4600 switch(mode
->exec_mode
) {
4601 case SpvExecutionModeDenormPreserve
:
4602 switch (mode
->operands
[0]) {
4603 case 16: execution_mode
= FLOAT_CONTROLS_DENORM_PRESERVE_FP16
; break;
4604 case 32: execution_mode
= FLOAT_CONTROLS_DENORM_PRESERVE_FP32
; break;
4605 case 64: execution_mode
= FLOAT_CONTROLS_DENORM_PRESERVE_FP64
; break;
4606 default: vtn_fail("Floating point type not supported");
4609 case SpvExecutionModeDenormFlushToZero
:
4610 switch (mode
->operands
[0]) {
4611 case 16: execution_mode
= FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16
; break;
4612 case 32: execution_mode
= FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32
; break;
4613 case 64: execution_mode
= FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP64
; break;
4614 default: vtn_fail("Floating point type not supported");
4617 case SpvExecutionModeSignedZeroInfNanPreserve
:
4618 switch (mode
->operands
[0]) {
4619 case 16: execution_mode
= FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP16
; break;
4620 case 32: execution_mode
= FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP32
; break;
4621 case 64: execution_mode
= FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP64
; break;
4622 default: vtn_fail("Floating point type not supported");
4625 case SpvExecutionModeRoundingModeRTE
:
4626 switch (mode
->operands
[0]) {
4627 case 16: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16
; break;
4628 case 32: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32
; break;
4629 case 64: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64
; break;
4630 default: vtn_fail("Floating point type not supported");
4633 case SpvExecutionModeRoundingModeRTZ
:
4634 switch (mode
->operands
[0]) {
4635 case 16: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16
; break;
4636 case 32: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32
; break;
4637 case 64: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64
; break;
4638 default: vtn_fail("Floating point type not supported");
4646 b
->shader
->info
.float_controls_execution_mode
|= execution_mode
;
4650 vtn_handle_variable_or_type_instruction(struct vtn_builder
*b
, SpvOp opcode
,
4651 const uint32_t *w
, unsigned count
)
4653 vtn_set_instruction_result_type(b
, opcode
, w
, count
);
4657 case SpvOpSourceContinued
:
4658 case SpvOpSourceExtension
:
4659 case SpvOpExtension
:
4660 case SpvOpCapability
:
4661 case SpvOpExtInstImport
:
4662 case SpvOpMemoryModel
:
4663 case SpvOpEntryPoint
:
4664 case SpvOpExecutionMode
:
4667 case SpvOpMemberName
:
4668 case SpvOpDecorationGroup
:
4670 case SpvOpDecorateId
:
4671 case SpvOpMemberDecorate
:
4672 case SpvOpGroupDecorate
:
4673 case SpvOpGroupMemberDecorate
:
4674 case SpvOpDecorateString
:
4675 case SpvOpMemberDecorateString
:
4676 vtn_fail("Invalid opcode types and variables section");
4682 case SpvOpTypeFloat
:
4683 case SpvOpTypeVector
:
4684 case SpvOpTypeMatrix
:
4685 case SpvOpTypeImage
:
4686 case SpvOpTypeSampler
:
4687 case SpvOpTypeSampledImage
:
4688 case SpvOpTypeArray
:
4689 case SpvOpTypeRuntimeArray
:
4690 case SpvOpTypeStruct
:
4691 case SpvOpTypeOpaque
:
4692 case SpvOpTypePointer
:
4693 case SpvOpTypeForwardPointer
:
4694 case SpvOpTypeFunction
:
4695 case SpvOpTypeEvent
:
4696 case SpvOpTypeDeviceEvent
:
4697 case SpvOpTypeReserveId
:
4698 case SpvOpTypeQueue
:
4700 vtn_handle_type(b
, opcode
, w
, count
);
4703 case SpvOpConstantTrue
:
4704 case SpvOpConstantFalse
:
4706 case SpvOpConstantComposite
:
4707 case SpvOpConstantSampler
:
4708 case SpvOpConstantNull
:
4709 case SpvOpSpecConstantTrue
:
4710 case SpvOpSpecConstantFalse
:
4711 case SpvOpSpecConstant
:
4712 case SpvOpSpecConstantComposite
:
4713 case SpvOpSpecConstantOp
:
4714 vtn_handle_constant(b
, opcode
, w
, count
);
4719 vtn_handle_variables(b
, opcode
, w
, count
);
4722 case SpvOpExtInst
: {
4723 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
4724 /* NonSemantic extended instructions are acceptable in preamble, others
4725 * will indicate the end of preamble.
4727 return val
->ext_handler
== vtn_handle_non_semantic_instruction
;
4731 return false; /* End of preamble */
4737 static struct vtn_ssa_value
*
4738 vtn_nir_select(struct vtn_builder
*b
, struct vtn_ssa_value
*src0
,
4739 struct vtn_ssa_value
*src1
, struct vtn_ssa_value
*src2
)
4741 struct vtn_ssa_value
*dest
= rzalloc(b
, struct vtn_ssa_value
);
4742 dest
->type
= src1
->type
;
4744 if (glsl_type_is_vector_or_scalar(src1
->type
)) {
4745 dest
->def
= nir_bcsel(&b
->nb
, src0
->def
, src1
->def
, src2
->def
);
4747 unsigned elems
= glsl_get_length(src1
->type
);
4749 dest
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
4750 for (unsigned i
= 0; i
< elems
; i
++) {
4751 dest
->elems
[i
] = vtn_nir_select(b
, src0
,
4752 src1
->elems
[i
], src2
->elems
[i
]);
4760 vtn_handle_select(struct vtn_builder
*b
, SpvOp opcode
,
4761 const uint32_t *w
, unsigned count
)
4763 /* Handle OpSelect up-front here because it needs to be able to handle
4764 * pointers and not just regular vectors and scalars.
4766 struct vtn_value
*res_val
= vtn_untyped_value(b
, w
[2]);
4767 struct vtn_value
*cond_val
= vtn_untyped_value(b
, w
[3]);
4768 struct vtn_value
*obj1_val
= vtn_untyped_value(b
, w
[4]);
4769 struct vtn_value
*obj2_val
= vtn_untyped_value(b
, w
[5]);
4771 vtn_fail_if(obj1_val
->type
!= res_val
->type
||
4772 obj2_val
->type
!= res_val
->type
,
4773 "Object types must match the result type in OpSelect");
4775 vtn_fail_if((cond_val
->type
->base_type
!= vtn_base_type_scalar
&&
4776 cond_val
->type
->base_type
!= vtn_base_type_vector
) ||
4777 !glsl_type_is_boolean(cond_val
->type
->type
),
4778 "OpSelect must have either a vector of booleans or "
4779 "a boolean as Condition type");
4781 vtn_fail_if(cond_val
->type
->base_type
== vtn_base_type_vector
&&
4782 (res_val
->type
->base_type
!= vtn_base_type_vector
||
4783 res_val
->type
->length
!= cond_val
->type
->length
),
4784 "When Condition type in OpSelect is a vector, the Result "
4785 "type must be a vector of the same length");
4787 switch (res_val
->type
->base_type
) {
4788 case vtn_base_type_scalar
:
4789 case vtn_base_type_vector
:
4790 case vtn_base_type_matrix
:
4791 case vtn_base_type_array
:
4792 case vtn_base_type_struct
:
4795 case vtn_base_type_pointer
:
4796 /* We need to have actual storage for pointer types. */
4797 vtn_fail_if(res_val
->type
->type
== NULL
,
4798 "Invalid pointer result type for OpSelect");
4801 vtn_fail("Result type of OpSelect must be a scalar, composite, or pointer");
4804 vtn_push_ssa_value(b
, w
[2],
4805 vtn_nir_select(b
, vtn_ssa_value(b
, w
[3]),
4806 vtn_ssa_value(b
, w
[4]),
4807 vtn_ssa_value(b
, w
[5])));
4811 vtn_handle_ptr(struct vtn_builder
*b
, SpvOp opcode
,
4812 const uint32_t *w
, unsigned count
)
4814 struct vtn_type
*type1
= vtn_get_value_type(b
, w
[3]);
4815 struct vtn_type
*type2
= vtn_get_value_type(b
, w
[4]);
4816 vtn_fail_if(type1
->base_type
!= vtn_base_type_pointer
||
4817 type2
->base_type
!= vtn_base_type_pointer
,
4818 "%s operands must have pointer types",
4819 spirv_op_to_string(opcode
));
4820 vtn_fail_if(type1
->storage_class
!= type2
->storage_class
,
4821 "%s operands must have the same storage class",
4822 spirv_op_to_string(opcode
));
4824 struct vtn_type
*vtn_type
= vtn_get_type(b
, w
[1]);
4825 const struct glsl_type
*type
= vtn_type
->type
;
4827 nir_address_format addr_format
= vtn_mode_to_address_format(
4828 b
, vtn_storage_class_to_mode(b
, type1
->storage_class
, NULL
, NULL
));
4833 case SpvOpPtrDiff
: {
4834 /* OpPtrDiff returns the difference in number of elements (not byte offset). */
4835 unsigned elem_size
, elem_align
;
4836 glsl_get_natural_size_align_bytes(type1
->deref
->type
,
4837 &elem_size
, &elem_align
);
4839 def
= nir_build_addr_isub(&b
->nb
,
4840 vtn_get_nir_ssa(b
, w
[3]),
4841 vtn_get_nir_ssa(b
, w
[4]),
4843 def
= nir_idiv(&b
->nb
, def
, nir_imm_intN_t(&b
->nb
, elem_size
, def
->bit_size
));
4844 def
= nir_i2i(&b
->nb
, def
, glsl_get_bit_size(type
));
4849 case SpvOpPtrNotEqual
: {
4850 def
= nir_build_addr_ieq(&b
->nb
,
4851 vtn_get_nir_ssa(b
, w
[3]),
4852 vtn_get_nir_ssa(b
, w
[4]),
4854 if (opcode
== SpvOpPtrNotEqual
)
4855 def
= nir_inot(&b
->nb
, def
);
4860 unreachable("Invalid ptr operation");
4863 vtn_push_nir_ssa(b
, w
[2], def
);
4867 vtn_handle_body_instruction(struct vtn_builder
*b
, SpvOp opcode
,
4868 const uint32_t *w
, unsigned count
)
4874 case SpvOpLoopMerge
:
4875 case SpvOpSelectionMerge
:
4876 /* This is handled by cfg pre-pass and walk_blocks */
4880 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
4881 val
->type
= vtn_get_type(b
, w
[1]);
4886 vtn_handle_extension(b
, opcode
, w
, count
);
4892 case SpvOpCopyMemory
:
4893 case SpvOpCopyMemorySized
:
4894 case SpvOpAccessChain
:
4895 case SpvOpPtrAccessChain
:
4896 case SpvOpInBoundsAccessChain
:
4897 case SpvOpInBoundsPtrAccessChain
:
4898 case SpvOpArrayLength
:
4899 case SpvOpConvertPtrToU
:
4900 case SpvOpConvertUToPtr
:
4901 vtn_handle_variables(b
, opcode
, w
, count
);
4904 case SpvOpFunctionCall
:
4905 vtn_handle_function_call(b
, opcode
, w
, count
);
4908 case SpvOpSampledImage
:
4910 case SpvOpImageSampleImplicitLod
:
4911 case SpvOpImageSampleExplicitLod
:
4912 case SpvOpImageSampleDrefImplicitLod
:
4913 case SpvOpImageSampleDrefExplicitLod
:
4914 case SpvOpImageSampleProjImplicitLod
:
4915 case SpvOpImageSampleProjExplicitLod
:
4916 case SpvOpImageSampleProjDrefImplicitLod
:
4917 case SpvOpImageSampleProjDrefExplicitLod
:
4918 case SpvOpImageFetch
:
4919 case SpvOpImageGather
:
4920 case SpvOpImageDrefGather
:
4921 case SpvOpImageQuerySizeLod
:
4922 case SpvOpImageQueryLod
:
4923 case SpvOpImageQueryLevels
:
4924 case SpvOpImageQuerySamples
:
4925 vtn_handle_texture(b
, opcode
, w
, count
);
4928 case SpvOpImageRead
:
4929 case SpvOpImageWrite
:
4930 case SpvOpImageTexelPointer
:
4931 vtn_handle_image(b
, opcode
, w
, count
);
4934 case SpvOpImageQuerySize
: {
4935 struct vtn_type
*image_type
= vtn_get_value_type(b
, w
[3]);
4936 vtn_assert(image_type
->base_type
== vtn_base_type_image
);
4937 if (glsl_type_is_image(image_type
->glsl_image
)) {
4938 vtn_handle_image(b
, opcode
, w
, count
);
4940 vtn_assert(glsl_type_is_sampler(image_type
->glsl_image
));
4941 vtn_handle_texture(b
, opcode
, w
, count
);
4946 case SpvOpFragmentMaskFetchAMD
:
4947 case SpvOpFragmentFetchAMD
:
4948 vtn_handle_texture(b
, opcode
, w
, count
);
4951 case SpvOpAtomicLoad
:
4952 case SpvOpAtomicExchange
:
4953 case SpvOpAtomicCompareExchange
:
4954 case SpvOpAtomicCompareExchangeWeak
:
4955 case SpvOpAtomicIIncrement
:
4956 case SpvOpAtomicIDecrement
:
4957 case SpvOpAtomicIAdd
:
4958 case SpvOpAtomicISub
:
4959 case SpvOpAtomicSMin
:
4960 case SpvOpAtomicUMin
:
4961 case SpvOpAtomicSMax
:
4962 case SpvOpAtomicUMax
:
4963 case SpvOpAtomicAnd
:
4965 case SpvOpAtomicXor
:
4966 case SpvOpAtomicFAddEXT
: {
4967 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[3]);
4968 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
4969 vtn_handle_image(b
, opcode
, w
, count
);
4971 vtn_assert(pointer
->value_type
== vtn_value_type_pointer
);
4972 vtn_handle_atomics(b
, opcode
, w
, count
);
4977 case SpvOpAtomicStore
: {
4978 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[1]);
4979 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
4980 vtn_handle_image(b
, opcode
, w
, count
);
4982 vtn_assert(pointer
->value_type
== vtn_value_type_pointer
);
4983 vtn_handle_atomics(b
, opcode
, w
, count
);
4989 vtn_handle_select(b
, opcode
, w
, count
);
4997 case SpvOpConvertFToU
:
4998 case SpvOpConvertFToS
:
4999 case SpvOpConvertSToF
:
5000 case SpvOpConvertUToF
:
5004 case SpvOpQuantizeToF16
:
5005 case SpvOpPtrCastToGeneric
:
5006 case SpvOpGenericCastToPtr
:
5011 case SpvOpSignBitSet
:
5012 case SpvOpLessOrGreater
:
5014 case SpvOpUnordered
:
5029 case SpvOpVectorTimesScalar
:
5031 case SpvOpIAddCarry
:
5032 case SpvOpISubBorrow
:
5033 case SpvOpUMulExtended
:
5034 case SpvOpSMulExtended
:
5035 case SpvOpShiftRightLogical
:
5036 case SpvOpShiftRightArithmetic
:
5037 case SpvOpShiftLeftLogical
:
5038 case SpvOpLogicalEqual
:
5039 case SpvOpLogicalNotEqual
:
5040 case SpvOpLogicalOr
:
5041 case SpvOpLogicalAnd
:
5042 case SpvOpLogicalNot
:
5043 case SpvOpBitwiseOr
:
5044 case SpvOpBitwiseXor
:
5045 case SpvOpBitwiseAnd
:
5047 case SpvOpFOrdEqual
:
5048 case SpvOpFUnordEqual
:
5049 case SpvOpINotEqual
:
5050 case SpvOpFOrdNotEqual
:
5051 case SpvOpFUnordNotEqual
:
5052 case SpvOpULessThan
:
5053 case SpvOpSLessThan
:
5054 case SpvOpFOrdLessThan
:
5055 case SpvOpFUnordLessThan
:
5056 case SpvOpUGreaterThan
:
5057 case SpvOpSGreaterThan
:
5058 case SpvOpFOrdGreaterThan
:
5059 case SpvOpFUnordGreaterThan
:
5060 case SpvOpULessThanEqual
:
5061 case SpvOpSLessThanEqual
:
5062 case SpvOpFOrdLessThanEqual
:
5063 case SpvOpFUnordLessThanEqual
:
5064 case SpvOpUGreaterThanEqual
:
5065 case SpvOpSGreaterThanEqual
:
5066 case SpvOpFOrdGreaterThanEqual
:
5067 case SpvOpFUnordGreaterThanEqual
:
5073 case SpvOpFwidthFine
:
5074 case SpvOpDPdxCoarse
:
5075 case SpvOpDPdyCoarse
:
5076 case SpvOpFwidthCoarse
:
5077 case SpvOpBitFieldInsert
:
5078 case SpvOpBitFieldSExtract
:
5079 case SpvOpBitFieldUExtract
:
5080 case SpvOpBitReverse
:
5082 case SpvOpTranspose
:
5083 case SpvOpOuterProduct
:
5084 case SpvOpMatrixTimesScalar
:
5085 case SpvOpVectorTimesMatrix
:
5086 case SpvOpMatrixTimesVector
:
5087 case SpvOpMatrixTimesMatrix
:
5088 case SpvOpUCountLeadingZerosINTEL
:
5089 case SpvOpUCountTrailingZerosINTEL
:
5090 case SpvOpAbsISubINTEL
:
5091 case SpvOpAbsUSubINTEL
:
5092 case SpvOpIAddSatINTEL
:
5093 case SpvOpUAddSatINTEL
:
5094 case SpvOpIAverageINTEL
:
5095 case SpvOpUAverageINTEL
:
5096 case SpvOpIAverageRoundedINTEL
:
5097 case SpvOpUAverageRoundedINTEL
:
5098 case SpvOpISubSatINTEL
:
5099 case SpvOpUSubSatINTEL
:
5100 case SpvOpIMul32x16INTEL
:
5101 case SpvOpUMul32x16INTEL
:
5102 vtn_handle_alu(b
, opcode
, w
, count
);
5106 vtn_handle_bitcast(b
, w
, count
);
5109 case SpvOpVectorExtractDynamic
:
5110 case SpvOpVectorInsertDynamic
:
5111 case SpvOpVectorShuffle
:
5112 case SpvOpCompositeConstruct
:
5113 case SpvOpCompositeExtract
:
5114 case SpvOpCompositeInsert
:
5115 case SpvOpCopyLogical
:
5116 case SpvOpCopyObject
:
5117 vtn_handle_composite(b
, opcode
, w
, count
);
5120 case SpvOpEmitVertex
:
5121 case SpvOpEndPrimitive
:
5122 case SpvOpEmitStreamVertex
:
5123 case SpvOpEndStreamPrimitive
:
5124 case SpvOpControlBarrier
:
5125 case SpvOpMemoryBarrier
:
5126 vtn_handle_barrier(b
, opcode
, w
, count
);
5129 case SpvOpGroupNonUniformElect
:
5130 case SpvOpGroupNonUniformAll
:
5131 case SpvOpGroupNonUniformAny
:
5132 case SpvOpGroupNonUniformAllEqual
:
5133 case SpvOpGroupNonUniformBroadcast
:
5134 case SpvOpGroupNonUniformBroadcastFirst
:
5135 case SpvOpGroupNonUniformBallot
:
5136 case SpvOpGroupNonUniformInverseBallot
:
5137 case SpvOpGroupNonUniformBallotBitExtract
:
5138 case SpvOpGroupNonUniformBallotBitCount
:
5139 case SpvOpGroupNonUniformBallotFindLSB
:
5140 case SpvOpGroupNonUniformBallotFindMSB
:
5141 case SpvOpGroupNonUniformShuffle
:
5142 case SpvOpGroupNonUniformShuffleXor
:
5143 case SpvOpGroupNonUniformShuffleUp
:
5144 case SpvOpGroupNonUniformShuffleDown
:
5145 case SpvOpGroupNonUniformIAdd
:
5146 case SpvOpGroupNonUniformFAdd
:
5147 case SpvOpGroupNonUniformIMul
:
5148 case SpvOpGroupNonUniformFMul
:
5149 case SpvOpGroupNonUniformSMin
:
5150 case SpvOpGroupNonUniformUMin
:
5151 case SpvOpGroupNonUniformFMin
:
5152 case SpvOpGroupNonUniformSMax
:
5153 case SpvOpGroupNonUniformUMax
:
5154 case SpvOpGroupNonUniformFMax
:
5155 case SpvOpGroupNonUniformBitwiseAnd
:
5156 case SpvOpGroupNonUniformBitwiseOr
:
5157 case SpvOpGroupNonUniformBitwiseXor
:
5158 case SpvOpGroupNonUniformLogicalAnd
:
5159 case SpvOpGroupNonUniformLogicalOr
:
5160 case SpvOpGroupNonUniformLogicalXor
:
5161 case SpvOpGroupNonUniformQuadBroadcast
:
5162 case SpvOpGroupNonUniformQuadSwap
:
5165 case SpvOpGroupBroadcast
:
5166 case SpvOpGroupIAdd
:
5167 case SpvOpGroupFAdd
:
5168 case SpvOpGroupFMin
:
5169 case SpvOpGroupUMin
:
5170 case SpvOpGroupSMin
:
5171 case SpvOpGroupFMax
:
5172 case SpvOpGroupUMax
:
5173 case SpvOpGroupSMax
:
5174 case SpvOpSubgroupBallotKHR
:
5175 case SpvOpSubgroupFirstInvocationKHR
:
5176 case SpvOpSubgroupReadInvocationKHR
:
5177 case SpvOpSubgroupAllKHR
:
5178 case SpvOpSubgroupAnyKHR
:
5179 case SpvOpSubgroupAllEqualKHR
:
5180 case SpvOpGroupIAddNonUniformAMD
:
5181 case SpvOpGroupFAddNonUniformAMD
:
5182 case SpvOpGroupFMinNonUniformAMD
:
5183 case SpvOpGroupUMinNonUniformAMD
:
5184 case SpvOpGroupSMinNonUniformAMD
:
5185 case SpvOpGroupFMaxNonUniformAMD
:
5186 case SpvOpGroupUMaxNonUniformAMD
:
5187 case SpvOpGroupSMaxNonUniformAMD
:
5188 vtn_handle_subgroup(b
, opcode
, w
, count
);
5193 case SpvOpPtrNotEqual
:
5194 vtn_handle_ptr(b
, opcode
, w
, count
);
5197 case SpvOpBeginInvocationInterlockEXT
:
5198 vtn_emit_barrier(b
, nir_intrinsic_begin_invocation_interlock
);
5201 case SpvOpEndInvocationInterlockEXT
:
5202 vtn_emit_barrier(b
, nir_intrinsic_end_invocation_interlock
);
5205 case SpvOpDemoteToHelperInvocationEXT
: {
5206 nir_intrinsic_instr
*intrin
=
5207 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_demote
);
5208 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
5212 case SpvOpIsHelperInvocationEXT
: {
5213 nir_intrinsic_instr
*intrin
=
5214 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_is_helper_invocation
);
5215 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
, 1, 1, NULL
);
5216 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
5218 vtn_push_nir_ssa(b
, w
[2], &intrin
->dest
.ssa
);
5222 case SpvOpReadClockKHR
: {
5223 SpvScope scope
= vtn_constant_uint(b
, w
[3]);
5224 nir_scope nir_scope
;
5227 case SpvScopeDevice
:
5228 nir_scope
= NIR_SCOPE_DEVICE
;
5230 case SpvScopeSubgroup
:
5231 nir_scope
= NIR_SCOPE_SUBGROUP
;
5234 vtn_fail("invalid read clock scope");
5237 /* Operation supports two result types: uvec2 and uint64_t. The NIR
5238 * intrinsic gives uvec2, so pack the result for the other case.
5240 nir_intrinsic_instr
*intrin
=
5241 nir_intrinsic_instr_create(b
->nb
.shader
, nir_intrinsic_shader_clock
);
5242 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
, 2, 32, NULL
);
5243 nir_intrinsic_set_memory_scope(intrin
, nir_scope
);
5244 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
5246 struct vtn_type
*type
= vtn_get_type(b
, w
[1]);
5247 const struct glsl_type
*dest_type
= type
->type
;
5248 nir_ssa_def
*result
;
5250 if (glsl_type_is_vector(dest_type
)) {
5251 assert(dest_type
== glsl_vector_type(GLSL_TYPE_UINT
, 2));
5252 result
= &intrin
->dest
.ssa
;
5254 assert(glsl_type_is_scalar(dest_type
));
5255 assert(glsl_get_base_type(dest_type
) == GLSL_TYPE_UINT64
);
5256 result
= nir_pack_64_2x32(&b
->nb
, &intrin
->dest
.ssa
);
5259 vtn_push_nir_ssa(b
, w
[2], result
);
5263 case SpvOpLifetimeStart
:
5264 case SpvOpLifetimeStop
:
5268 vtn_fail_with_opcode("Unhandled opcode", opcode
);
5275 vtn_create_builder(const uint32_t *words
, size_t word_count
,
5276 gl_shader_stage stage
, const char *entry_point_name
,
5277 const struct spirv_to_nir_options
*options
)
5279 /* Initialize the vtn_builder object */
5280 struct vtn_builder
*b
= rzalloc(NULL
, struct vtn_builder
);
5281 struct spirv_to_nir_options
*dup_options
=
5282 ralloc(b
, struct spirv_to_nir_options
);
5283 *dup_options
= *options
;
5286 b
->spirv_word_count
= word_count
;
5290 list_inithead(&b
->functions
);
5291 b
->entry_point_stage
= stage
;
5292 b
->entry_point_name
= entry_point_name
;
5293 b
->options
= dup_options
;
5296 * Handle the SPIR-V header (first 5 dwords).
5297 * Can't use vtx_assert() as the setjmp(3) target isn't initialized yet.
5299 if (word_count
<= 5)
5302 if (words
[0] != SpvMagicNumber
) {
5303 vtn_err("words[0] was 0x%x, want 0x%x", words
[0], SpvMagicNumber
);
5306 if (words
[1] < 0x10000) {
5307 vtn_err("words[1] was 0x%x, want >= 0x10000", words
[1]);
5311 uint16_t generator_id
= words
[2] >> 16;
5312 uint16_t generator_version
= words
[2];
5314 /* In GLSLang commit 8297936dd6eb3, their handling of barrier() was fixed
5315 * to provide correct memory semantics on compute shader barrier()
5316 * commands. Prior to that, we need to fix them up ourselves. This
5317 * GLSLang fix caused them to bump to generator version 3.
5319 b
->wa_glslang_cs_barrier
= (generator_id
== 8 && generator_version
< 3);
5321 /* words[2] == generator magic */
5322 unsigned value_id_bound
= words
[3];
5323 if (words
[4] != 0) {
5324 vtn_err("words[4] was %u, want 0", words
[4]);
5328 b
->value_id_bound
= value_id_bound
;
5329 b
->values
= rzalloc_array(b
, struct vtn_value
, value_id_bound
);
5337 static nir_function
*
5338 vtn_emit_kernel_entry_point_wrapper(struct vtn_builder
*b
,
5339 nir_function
*entry_point
)
5341 vtn_assert(entry_point
== b
->entry_point
->func
->impl
->function
);
5342 vtn_fail_if(!entry_point
->name
, "entry points are required to have a name");
5343 const char *func_name
=
5344 ralloc_asprintf(b
->shader
, "__wrapped_%s", entry_point
->name
);
5346 /* we shouldn't have any inputs yet */
5347 vtn_assert(!entry_point
->shader
->num_inputs
);
5348 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_KERNEL
);
5350 nir_function
*main_entry_point
= nir_function_create(b
->shader
, func_name
);
5351 main_entry_point
->impl
= nir_function_impl_create(main_entry_point
);
5352 nir_builder_init(&b
->nb
, main_entry_point
->impl
);
5353 b
->nb
.cursor
= nir_after_cf_list(&main_entry_point
->impl
->body
);
5354 b
->func_param_idx
= 0;
5356 nir_call_instr
*call
= nir_call_instr_create(b
->nb
.shader
, entry_point
);
5358 for (unsigned i
= 0; i
< entry_point
->num_params
; ++i
) {
5359 struct vtn_type
*param_type
= b
->entry_point
->func
->type
->params
[i
];
5361 /* consider all pointers to function memory to be parameters passed
5364 bool is_by_val
= param_type
->base_type
== vtn_base_type_pointer
&&
5365 param_type
->storage_class
== SpvStorageClassFunction
;
5367 /* input variable */
5368 nir_variable
*in_var
= rzalloc(b
->nb
.shader
, nir_variable
);
5369 in_var
->data
.mode
= nir_var_shader_in
;
5370 in_var
->data
.read_only
= true;
5371 in_var
->data
.location
= i
;
5374 in_var
->type
= param_type
->deref
->type
;
5376 in_var
->type
= param_type
->type
;
5378 nir_shader_add_variable(b
->nb
.shader
, in_var
);
5379 b
->nb
.shader
->num_inputs
++;
5381 /* we have to copy the entire variable into function memory */
5383 nir_variable
*copy_var
=
5384 nir_local_variable_create(main_entry_point
->impl
, in_var
->type
,
5386 nir_copy_var(&b
->nb
, copy_var
, in_var
);
5388 nir_src_for_ssa(&nir_build_deref_var(&b
->nb
, copy_var
)->dest
.ssa
);
5390 call
->params
[i
] = nir_src_for_ssa(nir_load_var(&b
->nb
, in_var
));
5394 nir_builder_instr_insert(&b
->nb
, &call
->instr
);
5396 return main_entry_point
;
5400 spirv_to_nir(const uint32_t *words
, size_t word_count
,
5401 struct nir_spirv_specialization
*spec
, unsigned num_spec
,
5402 gl_shader_stage stage
, const char *entry_point_name
,
5403 const struct spirv_to_nir_options
*options
,
5404 const nir_shader_compiler_options
*nir_options
)
5407 const uint32_t *word_end
= words
+ word_count
;
5409 struct vtn_builder
*b
= vtn_create_builder(words
, word_count
,
5410 stage
, entry_point_name
,
5416 /* See also _vtn_fail() */
5417 if (setjmp(b
->fail_jump
)) {
5422 /* Skip the SPIR-V header, handled at vtn_create_builder */
5425 b
->shader
= nir_shader_create(b
, stage
, nir_options
, NULL
);
5427 /* Handle all the preamble instructions */
5428 words
= vtn_foreach_instruction(b
, words
, word_end
,
5429 vtn_handle_preamble_instruction
);
5431 if (b
->entry_point
== NULL
) {
5432 vtn_fail("Entry point not found");
5437 /* Set shader info defaults */
5438 if (stage
== MESA_SHADER_GEOMETRY
)
5439 b
->shader
->info
.gs
.invocations
= 1;
5441 /* Parse rounding mode execution modes. This has to happen earlier than
5442 * other changes in the execution modes since they can affect, for example,
5443 * the result of the floating point constants.
5445 vtn_foreach_execution_mode(b
, b
->entry_point
,
5446 vtn_handle_rounding_mode_in_execution_mode
, NULL
);
5448 b
->specializations
= spec
;
5449 b
->num_specializations
= num_spec
;
5451 /* Handle all variable, type, and constant instructions */
5452 words
= vtn_foreach_instruction(b
, words
, word_end
,
5453 vtn_handle_variable_or_type_instruction
);
5455 /* Parse execution modes */
5456 vtn_foreach_execution_mode(b
, b
->entry_point
,
5457 vtn_handle_execution_mode
, NULL
);
5459 if (b
->workgroup_size_builtin
) {
5460 vtn_assert(b
->workgroup_size_builtin
->type
->type
==
5461 glsl_vector_type(GLSL_TYPE_UINT
, 3));
5463 nir_const_value
*const_size
=
5464 b
->workgroup_size_builtin
->constant
->values
;
5466 b
->shader
->info
.cs
.local_size
[0] = const_size
[0].u32
;
5467 b
->shader
->info
.cs
.local_size
[1] = const_size
[1].u32
;
5468 b
->shader
->info
.cs
.local_size
[2] = const_size
[2].u32
;
5471 /* Set types on all vtn_values */
5472 vtn_foreach_instruction(b
, words
, word_end
, vtn_set_instruction_result_type
);
5474 vtn_build_cfg(b
, words
, word_end
);
5476 assert(b
->entry_point
->value_type
== vtn_value_type_function
);
5477 b
->entry_point
->func
->referenced
= true;
5482 vtn_foreach_cf_node(node
, &b
->functions
) {
5483 struct vtn_function
*func
= vtn_cf_node_as_function(node
);
5484 if (func
->referenced
&& !func
->emitted
) {
5485 b
->const_table
= _mesa_pointer_hash_table_create(b
);
5487 vtn_function_emit(b
, func
, vtn_handle_body_instruction
);
5493 vtn_assert(b
->entry_point
->value_type
== vtn_value_type_function
);
5494 nir_function
*entry_point
= b
->entry_point
->func
->impl
->function
;
5495 vtn_assert(entry_point
);
5497 /* post process entry_points with input params */
5498 if (entry_point
->num_params
&& b
->shader
->info
.stage
== MESA_SHADER_KERNEL
)
5499 entry_point
= vtn_emit_kernel_entry_point_wrapper(b
, entry_point
);
5501 entry_point
->is_entrypoint
= true;
5503 /* When multiple shader stages exist in the same SPIR-V module, we
5504 * generate input and output variables for every stage, in the same
5505 * NIR program. These dead variables can be invalid NIR. For example,
5506 * TCS outputs must be per-vertex arrays (or decorated 'patch'), while
5507 * VS output variables wouldn't be.
5509 * To ensure we have valid NIR, we eliminate any dead inputs and outputs
5510 * right away. In order to do so, we must lower any constant initializers
5511 * on outputs so nir_remove_dead_variables sees that they're written to.
5513 nir_lower_variable_initializers(b
->shader
, nir_var_shader_out
);
5514 nir_remove_dead_variables(b
->shader
,
5515 nir_var_shader_in
| nir_var_shader_out
, NULL
);
5517 /* We sometimes generate bogus derefs that, while never used, give the
5518 * validator a bit of heartburn. Run dead code to get rid of them.
5520 nir_opt_dce(b
->shader
);
5522 /* Unparent the shader from the vtn_builder before we delete the builder */
5523 ralloc_steal(NULL
, b
->shader
);
5525 nir_shader
*shader
= b
->shader
;