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"
39 #if UTIL_ARCH_BIG_ENDIAN
44 vtn_log(struct vtn_builder
*b
, enum nir_spirv_debug_level level
,
45 size_t spirv_offset
, const char *message
)
47 if (b
->options
->debug
.func
) {
48 b
->options
->debug
.func(b
->options
->debug
.private_data
,
49 level
, spirv_offset
, message
);
53 if (level
>= NIR_SPIRV_DEBUG_LEVEL_WARNING
)
54 fprintf(stderr
, "%s\n", message
);
59 vtn_logf(struct vtn_builder
*b
, enum nir_spirv_debug_level level
,
60 size_t spirv_offset
, const char *fmt
, ...)
66 msg
= ralloc_vasprintf(NULL
, fmt
, args
);
69 vtn_log(b
, level
, spirv_offset
, msg
);
75 vtn_log_err(struct vtn_builder
*b
,
76 enum nir_spirv_debug_level level
, const char *prefix
,
77 const char *file
, unsigned line
,
78 const char *fmt
, va_list args
)
82 msg
= ralloc_strdup(NULL
, prefix
);
85 ralloc_asprintf_append(&msg
, " In file %s:%u\n", file
, line
);
88 ralloc_asprintf_append(&msg
, " ");
90 ralloc_vasprintf_append(&msg
, fmt
, args
);
92 ralloc_asprintf_append(&msg
, "\n %zu bytes into the SPIR-V binary",
96 ralloc_asprintf_append(&msg
,
97 "\n in SPIR-V source file %s, line %d, col %d",
98 b
->file
, b
->line
, b
->col
);
101 vtn_log(b
, level
, b
->spirv_offset
, msg
);
107 vtn_dump_shader(struct vtn_builder
*b
, const char *path
, const char *prefix
)
112 int len
= snprintf(filename
, sizeof(filename
), "%s/%s-%d.spirv",
113 path
, prefix
, idx
++);
114 if (len
< 0 || len
>= sizeof(filename
))
117 FILE *f
= fopen(filename
, "w");
121 fwrite(b
->spirv
, sizeof(*b
->spirv
), b
->spirv_word_count
, f
);
124 vtn_info("SPIR-V shader dumped to %s", filename
);
128 _vtn_warn(struct vtn_builder
*b
, const char *file
, unsigned line
,
129 const char *fmt
, ...)
134 vtn_log_err(b
, NIR_SPIRV_DEBUG_LEVEL_WARNING
, "SPIR-V WARNING:\n",
135 file
, line
, fmt
, args
);
140 _vtn_err(struct vtn_builder
*b
, const char *file
, unsigned line
,
141 const char *fmt
, ...)
146 vtn_log_err(b
, NIR_SPIRV_DEBUG_LEVEL_ERROR
, "SPIR-V ERROR:\n",
147 file
, line
, fmt
, args
);
152 _vtn_fail(struct vtn_builder
*b
, const char *file
, unsigned line
,
153 const char *fmt
, ...)
158 vtn_log_err(b
, NIR_SPIRV_DEBUG_LEVEL_ERROR
, "SPIR-V parsing FAILED:\n",
159 file
, line
, fmt
, args
);
162 const char *dump_path
= getenv("MESA_SPIRV_FAIL_DUMP_PATH");
164 vtn_dump_shader(b
, dump_path
, "fail");
166 longjmp(b
->fail_jump
, 1);
169 static struct vtn_ssa_value
*
170 vtn_undef_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
172 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
173 val
->type
= glsl_get_bare_type(type
);
175 if (glsl_type_is_vector_or_scalar(type
)) {
176 unsigned num_components
= glsl_get_vector_elements(val
->type
);
177 unsigned bit_size
= glsl_get_bit_size(val
->type
);
178 val
->def
= nir_ssa_undef(&b
->nb
, num_components
, bit_size
);
180 unsigned elems
= glsl_get_length(val
->type
);
181 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
182 if (glsl_type_is_array_or_matrix(type
)) {
183 const struct glsl_type
*elem_type
= glsl_get_array_element(type
);
184 for (unsigned i
= 0; i
< elems
; i
++)
185 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
187 vtn_assert(glsl_type_is_struct_or_ifc(type
));
188 for (unsigned i
= 0; i
< elems
; i
++) {
189 const struct glsl_type
*elem_type
= glsl_get_struct_field(type
, i
);
190 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
198 static struct vtn_ssa_value
*
199 vtn_const_ssa_value(struct vtn_builder
*b
, nir_constant
*constant
,
200 const struct glsl_type
*type
)
202 struct hash_entry
*entry
= _mesa_hash_table_search(b
->const_table
, constant
);
207 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
208 val
->type
= glsl_get_bare_type(type
);
210 if (glsl_type_is_vector_or_scalar(type
)) {
211 unsigned num_components
= glsl_get_vector_elements(val
->type
);
212 unsigned bit_size
= glsl_get_bit_size(type
);
213 nir_load_const_instr
*load
=
214 nir_load_const_instr_create(b
->shader
, num_components
, bit_size
);
216 memcpy(load
->value
, constant
->values
,
217 sizeof(nir_const_value
) * num_components
);
219 nir_instr_insert_before_cf_list(&b
->nb
.impl
->body
, &load
->instr
);
220 val
->def
= &load
->def
;
222 unsigned elems
= glsl_get_length(val
->type
);
223 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
224 if (glsl_type_is_array_or_matrix(type
)) {
225 const struct glsl_type
*elem_type
= glsl_get_array_element(type
);
226 for (unsigned i
= 0; i
< elems
; i
++) {
227 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
231 vtn_assert(glsl_type_is_struct_or_ifc(type
));
232 for (unsigned i
= 0; i
< elems
; i
++) {
233 const struct glsl_type
*elem_type
= glsl_get_struct_field(type
, i
);
234 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
243 struct vtn_ssa_value
*
244 vtn_ssa_value(struct vtn_builder
*b
, uint32_t value_id
)
246 struct vtn_value
*val
= vtn_untyped_value(b
, value_id
);
247 switch (val
->value_type
) {
248 case vtn_value_type_undef
:
249 return vtn_undef_ssa_value(b
, val
->type
->type
);
251 case vtn_value_type_constant
:
252 return vtn_const_ssa_value(b
, val
->constant
, val
->type
->type
);
254 case vtn_value_type_ssa
:
257 case vtn_value_type_pointer
:
258 vtn_assert(val
->pointer
->ptr_type
&& val
->pointer
->ptr_type
->type
);
259 struct vtn_ssa_value
*ssa
=
260 vtn_create_ssa_value(b
, val
->pointer
->ptr_type
->type
);
261 ssa
->def
= vtn_pointer_to_ssa(b
, val
->pointer
);
265 vtn_fail("Invalid type for an SSA value");
270 vtn_push_ssa_value(struct vtn_builder
*b
, uint32_t value_id
,
271 struct vtn_ssa_value
*ssa
)
273 struct vtn_type
*type
= vtn_get_value_type(b
, value_id
);
275 /* See vtn_create_ssa_value */
276 vtn_fail_if(ssa
->type
!= glsl_get_bare_type(type
->type
),
277 "Type mismatch for SPIR-V SSA value");
279 struct vtn_value
*val
;
280 if (type
->base_type
== vtn_base_type_pointer
) {
281 val
= vtn_push_pointer(b
, value_id
, vtn_pointer_from_ssa(b
, ssa
->def
, type
));
283 /* Don't trip the value_type_ssa check in vtn_push_value */
284 val
= vtn_push_value(b
, value_id
, vtn_value_type_invalid
);
285 val
->value_type
= vtn_value_type_ssa
;
293 vtn_get_nir_ssa(struct vtn_builder
*b
, uint32_t value_id
)
295 struct vtn_ssa_value
*ssa
= vtn_ssa_value(b
, value_id
);
296 vtn_fail_if(!glsl_type_is_vector_or_scalar(ssa
->type
),
297 "Expected a vector or scalar type");
302 vtn_push_nir_ssa(struct vtn_builder
*b
, uint32_t value_id
, nir_ssa_def
*def
)
304 /* Types for all SPIR-V SSA values are set as part of a pre-pass so the
305 * type will be valid by the time we get here.
307 struct vtn_type
*type
= vtn_get_value_type(b
, value_id
);
308 vtn_fail_if(def
->num_components
!= glsl_get_vector_elements(type
->type
) ||
309 def
->bit_size
!= glsl_get_bit_size(type
->type
),
310 "Mismatch between NIR and SPIR-V type.");
311 struct vtn_ssa_value
*ssa
= vtn_create_ssa_value(b
, type
->type
);
313 return vtn_push_ssa_value(b
, value_id
, ssa
);
316 static nir_deref_instr
*
317 vtn_get_image(struct vtn_builder
*b
, uint32_t value_id
)
319 struct vtn_type
*type
= vtn_get_value_type(b
, value_id
);
320 vtn_assert(type
->base_type
== vtn_base_type_image
);
321 return nir_build_deref_cast(&b
->nb
, vtn_get_nir_ssa(b
, value_id
),
322 nir_var_uniform
, type
->glsl_image
, 0);
326 vtn_push_image(struct vtn_builder
*b
, uint32_t value_id
,
327 nir_deref_instr
*deref
, bool propagate_non_uniform
)
329 struct vtn_type
*type
= vtn_get_value_type(b
, value_id
);
330 vtn_assert(type
->base_type
== vtn_base_type_image
);
331 struct vtn_value
*value
= vtn_push_nir_ssa(b
, value_id
, &deref
->dest
.ssa
);
332 value
->propagated_non_uniform
= propagate_non_uniform
;
335 static nir_deref_instr
*
336 vtn_get_sampler(struct vtn_builder
*b
, uint32_t value_id
)
338 struct vtn_type
*type
= vtn_get_value_type(b
, value_id
);
339 vtn_assert(type
->base_type
== vtn_base_type_sampler
);
340 return nir_build_deref_cast(&b
->nb
, vtn_get_nir_ssa(b
, value_id
),
341 nir_var_uniform
, glsl_bare_sampler_type(), 0);
345 vtn_sampled_image_to_nir_ssa(struct vtn_builder
*b
,
346 struct vtn_sampled_image si
)
348 return nir_vec2(&b
->nb
, &si
.image
->dest
.ssa
, &si
.sampler
->dest
.ssa
);
352 vtn_push_sampled_image(struct vtn_builder
*b
, uint32_t value_id
,
353 struct vtn_sampled_image si
, bool propagate_non_uniform
)
355 struct vtn_type
*type
= vtn_get_value_type(b
, value_id
);
356 vtn_assert(type
->base_type
== vtn_base_type_sampled_image
);
357 struct vtn_value
*value
= vtn_push_nir_ssa(b
, value_id
,
358 vtn_sampled_image_to_nir_ssa(b
, si
));
359 value
->propagated_non_uniform
= propagate_non_uniform
;
362 static struct vtn_sampled_image
363 vtn_get_sampled_image(struct vtn_builder
*b
, uint32_t value_id
)
365 struct vtn_type
*type
= vtn_get_value_type(b
, value_id
);
366 vtn_assert(type
->base_type
== vtn_base_type_sampled_image
);
367 nir_ssa_def
*si_vec2
= vtn_get_nir_ssa(b
, value_id
);
369 struct vtn_sampled_image si
= { NULL
, };
370 si
.image
= nir_build_deref_cast(&b
->nb
, nir_channel(&b
->nb
, si_vec2
, 0),
372 type
->image
->glsl_image
, 0);
373 si
.sampler
= nir_build_deref_cast(&b
->nb
, nir_channel(&b
->nb
, si_vec2
, 1),
375 glsl_bare_sampler_type(), 0);
380 vtn_string_literal(struct vtn_builder
*b
, const uint32_t *words
,
381 unsigned word_count
, unsigned *words_used
)
383 /* From the SPIR-V spec:
385 * "A string is interpreted as a nul-terminated stream of characters.
386 * The character set is Unicode in the UTF-8 encoding scheme. The UTF-8
387 * octets (8-bit bytes) are packed four per word, following the
388 * little-endian convention (i.e., the first octet is in the
389 * lowest-order 8 bits of the word). The final word contains the
390 * string’s nul-termination character (0), and all contents past the
391 * end of the string in the final word are padded with 0."
393 * On big-endian, we need to byte-swap.
395 #if UTIL_ARCH_BIG_ENDIAN
397 uint32_t *copy
= ralloc_array(b
, uint32_t, word_count
);
398 for (unsigned i
= 0; i
< word_count
; i
++)
399 copy
[i
] = bswap_32(words
[i
]);
404 const char *str
= (char *)words
;
405 const char *end
= memchr(str
, 0, word_count
* 4);
406 vtn_fail_if(end
== NULL
, "String is not null-terminated");
409 *words_used
= DIV_ROUND_UP(end
- str
+ 1, sizeof(*words
));
415 vtn_foreach_instruction(struct vtn_builder
*b
, const uint32_t *start
,
416 const uint32_t *end
, vtn_instruction_handler handler
)
422 const uint32_t *w
= start
;
424 SpvOp opcode
= w
[0] & SpvOpCodeMask
;
425 unsigned count
= w
[0] >> SpvWordCountShift
;
426 vtn_assert(count
>= 1 && w
+ count
<= end
);
428 b
->spirv_offset
= (uint8_t *)w
- (uint8_t *)b
->spirv
;
432 break; /* Do nothing */
435 b
->file
= vtn_value(b
, w
[1], vtn_value_type_string
)->str
;
447 if (!handler(b
, opcode
, w
, count
))
465 vtn_handle_non_semantic_instruction(struct vtn_builder
*b
, SpvOp ext_opcode
,
466 const uint32_t *w
, unsigned count
)
473 vtn_handle_extension(struct vtn_builder
*b
, SpvOp opcode
,
474 const uint32_t *w
, unsigned count
)
477 case SpvOpExtInstImport
: {
478 struct vtn_value
*val
= vtn_push_value(b
, w
[1], vtn_value_type_extension
);
479 const char *ext
= vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
480 if (strcmp(ext
, "GLSL.std.450") == 0) {
481 val
->ext_handler
= vtn_handle_glsl450_instruction
;
482 } else if ((strcmp(ext
, "SPV_AMD_gcn_shader") == 0)
483 && (b
->options
&& b
->options
->caps
.amd_gcn_shader
)) {
484 val
->ext_handler
= vtn_handle_amd_gcn_shader_instruction
;
485 } else if ((strcmp(ext
, "SPV_AMD_shader_ballot") == 0)
486 && (b
->options
&& b
->options
->caps
.amd_shader_ballot
)) {
487 val
->ext_handler
= vtn_handle_amd_shader_ballot_instruction
;
488 } else if ((strcmp(ext
, "SPV_AMD_shader_trinary_minmax") == 0)
489 && (b
->options
&& b
->options
->caps
.amd_trinary_minmax
)) {
490 val
->ext_handler
= vtn_handle_amd_shader_trinary_minmax_instruction
;
491 } else if ((strcmp(ext
, "SPV_AMD_shader_explicit_vertex_parameter") == 0)
492 && (b
->options
&& b
->options
->caps
.amd_shader_explicit_vertex_parameter
)) {
493 val
->ext_handler
= vtn_handle_amd_shader_explicit_vertex_parameter_instruction
;
494 } else if (strcmp(ext
, "OpenCL.std") == 0) {
495 val
->ext_handler
= vtn_handle_opencl_instruction
;
496 } else if (strstr(ext
, "NonSemantic.") == ext
) {
497 val
->ext_handler
= vtn_handle_non_semantic_instruction
;
499 vtn_fail("Unsupported extension: %s", ext
);
505 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
506 bool handled
= val
->ext_handler(b
, w
[4], w
, count
);
512 vtn_fail_with_opcode("Unhandled opcode", opcode
);
517 _foreach_decoration_helper(struct vtn_builder
*b
,
518 struct vtn_value
*base_value
,
520 struct vtn_value
*value
,
521 vtn_decoration_foreach_cb cb
, void *data
)
523 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
525 if (dec
->scope
== VTN_DEC_DECORATION
) {
526 member
= parent_member
;
527 } else if (dec
->scope
>= VTN_DEC_STRUCT_MEMBER0
) {
528 vtn_fail_if(value
->value_type
!= vtn_value_type_type
||
529 value
->type
->base_type
!= vtn_base_type_struct
,
530 "OpMemberDecorate and OpGroupMemberDecorate are only "
531 "allowed on OpTypeStruct");
532 /* This means we haven't recursed yet */
533 assert(value
== base_value
);
535 member
= dec
->scope
- VTN_DEC_STRUCT_MEMBER0
;
537 vtn_fail_if(member
>= base_value
->type
->length
,
538 "OpMemberDecorate specifies member %d but the "
539 "OpTypeStruct has only %u members",
540 member
, base_value
->type
->length
);
542 /* Not a decoration */
543 assert(dec
->scope
== VTN_DEC_EXECUTION_MODE
);
548 assert(dec
->group
->value_type
== vtn_value_type_decoration_group
);
549 _foreach_decoration_helper(b
, base_value
, member
, dec
->group
,
552 cb(b
, base_value
, member
, dec
, data
);
557 /** Iterates (recursively if needed) over all of the decorations on a value
559 * This function iterates over all of the decorations applied to a given
560 * value. If it encounters a decoration group, it recurses into the group
561 * and iterates over all of those decorations as well.
564 vtn_foreach_decoration(struct vtn_builder
*b
, struct vtn_value
*value
,
565 vtn_decoration_foreach_cb cb
, void *data
)
567 _foreach_decoration_helper(b
, value
, -1, value
, cb
, data
);
571 vtn_foreach_execution_mode(struct vtn_builder
*b
, struct vtn_value
*value
,
572 vtn_execution_mode_foreach_cb cb
, void *data
)
574 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
575 if (dec
->scope
!= VTN_DEC_EXECUTION_MODE
)
578 assert(dec
->group
== NULL
);
579 cb(b
, value
, dec
, data
);
584 vtn_handle_decoration(struct vtn_builder
*b
, SpvOp opcode
,
585 const uint32_t *w
, unsigned count
)
587 const uint32_t *w_end
= w
+ count
;
588 const uint32_t target
= w
[1];
592 case SpvOpDecorationGroup
:
593 vtn_push_value(b
, target
, vtn_value_type_decoration_group
);
597 case SpvOpDecorateId
:
598 case SpvOpMemberDecorate
:
599 case SpvOpDecorateString
:
600 case SpvOpMemberDecorateString
:
601 case SpvOpExecutionMode
:
602 case SpvOpExecutionModeId
: {
603 struct vtn_value
*val
= vtn_untyped_value(b
, target
);
605 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
608 case SpvOpDecorateId
:
609 case SpvOpDecorateString
:
610 dec
->scope
= VTN_DEC_DECORATION
;
612 case SpvOpMemberDecorate
:
613 case SpvOpMemberDecorateString
:
614 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(w
++);
615 vtn_fail_if(dec
->scope
< VTN_DEC_STRUCT_MEMBER0
, /* overflow */
616 "Member argument of OpMemberDecorate too large");
618 case SpvOpExecutionMode
:
619 case SpvOpExecutionModeId
:
620 dec
->scope
= VTN_DEC_EXECUTION_MODE
;
623 unreachable("Invalid decoration opcode");
625 dec
->decoration
= *(w
++);
628 /* Link into the list */
629 dec
->next
= val
->decoration
;
630 val
->decoration
= dec
;
634 case SpvOpGroupMemberDecorate
:
635 case SpvOpGroupDecorate
: {
636 struct vtn_value
*group
=
637 vtn_value(b
, target
, vtn_value_type_decoration_group
);
639 for (; w
< w_end
; w
++) {
640 struct vtn_value
*val
= vtn_untyped_value(b
, *w
);
641 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
644 if (opcode
== SpvOpGroupDecorate
) {
645 dec
->scope
= VTN_DEC_DECORATION
;
647 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(++w
);
648 vtn_fail_if(dec
->scope
< 0, /* Check for overflow */
649 "Member argument of OpGroupMemberDecorate too large");
652 /* Link into the list */
653 dec
->next
= val
->decoration
;
654 val
->decoration
= dec
;
660 unreachable("Unhandled opcode");
664 struct member_decoration_ctx
{
666 struct glsl_struct_field
*fields
;
667 struct vtn_type
*type
;
671 * Returns true if the given type contains a struct decorated Block or
675 vtn_type_contains_block(struct vtn_builder
*b
, struct vtn_type
*type
)
677 switch (type
->base_type
) {
678 case vtn_base_type_array
:
679 return vtn_type_contains_block(b
, type
->array_element
);
680 case vtn_base_type_struct
:
681 if (type
->block
|| type
->buffer_block
)
683 for (unsigned i
= 0; i
< type
->length
; i
++) {
684 if (vtn_type_contains_block(b
, type
->members
[i
]))
693 /** Returns true if two types are "compatible", i.e. you can do an OpLoad,
694 * OpStore, or OpCopyMemory between them without breaking anything.
695 * Technically, the SPIR-V rules require the exact same type ID but this lets
696 * us internally be a bit looser.
699 vtn_types_compatible(struct vtn_builder
*b
,
700 struct vtn_type
*t1
, struct vtn_type
*t2
)
702 if (t1
->id
== t2
->id
)
705 if (t1
->base_type
!= t2
->base_type
)
708 switch (t1
->base_type
) {
709 case vtn_base_type_void
:
710 case vtn_base_type_scalar
:
711 case vtn_base_type_vector
:
712 case vtn_base_type_matrix
:
713 case vtn_base_type_image
:
714 case vtn_base_type_sampler
:
715 case vtn_base_type_sampled_image
:
716 return t1
->type
== t2
->type
;
718 case vtn_base_type_array
:
719 return t1
->length
== t2
->length
&&
720 vtn_types_compatible(b
, t1
->array_element
, t2
->array_element
);
722 case vtn_base_type_pointer
:
723 return vtn_types_compatible(b
, t1
->deref
, t2
->deref
);
725 case vtn_base_type_struct
:
726 if (t1
->length
!= t2
->length
)
729 for (unsigned i
= 0; i
< t1
->length
; i
++) {
730 if (!vtn_types_compatible(b
, t1
->members
[i
], t2
->members
[i
]))
735 case vtn_base_type_function
:
736 /* This case shouldn't get hit since you can't copy around function
737 * types. Just require them to be identical.
742 vtn_fail("Invalid base type");
746 vtn_type_without_array(struct vtn_type
*type
)
748 while (type
->base_type
== vtn_base_type_array
)
749 type
= type
->array_element
;
753 /* does a shallow copy of a vtn_type */
755 static struct vtn_type
*
756 vtn_type_copy(struct vtn_builder
*b
, struct vtn_type
*src
)
758 struct vtn_type
*dest
= ralloc(b
, struct vtn_type
);
761 switch (src
->base_type
) {
762 case vtn_base_type_void
:
763 case vtn_base_type_scalar
:
764 case vtn_base_type_vector
:
765 case vtn_base_type_matrix
:
766 case vtn_base_type_array
:
767 case vtn_base_type_pointer
:
768 case vtn_base_type_image
:
769 case vtn_base_type_sampler
:
770 case vtn_base_type_sampled_image
:
771 /* Nothing more to do */
774 case vtn_base_type_struct
:
775 dest
->members
= ralloc_array(b
, struct vtn_type
*, src
->length
);
776 memcpy(dest
->members
, src
->members
,
777 src
->length
* sizeof(src
->members
[0]));
779 dest
->offsets
= ralloc_array(b
, unsigned, src
->length
);
780 memcpy(dest
->offsets
, src
->offsets
,
781 src
->length
* sizeof(src
->offsets
[0]));
784 case vtn_base_type_function
:
785 dest
->params
= ralloc_array(b
, struct vtn_type
*, src
->length
);
786 memcpy(dest
->params
, src
->params
, src
->length
* sizeof(src
->params
[0]));
793 static const struct glsl_type
*
794 wrap_type_in_array(const struct glsl_type
*type
,
795 const struct glsl_type
*array_type
)
797 if (!glsl_type_is_array(array_type
))
800 const struct glsl_type
*elem_type
=
801 wrap_type_in_array(type
, glsl_get_array_element(array_type
));
802 return glsl_array_type(elem_type
, glsl_get_length(array_type
),
803 glsl_get_explicit_stride(array_type
));
807 vtn_type_needs_explicit_layout(struct vtn_builder
*b
, enum vtn_variable_mode mode
)
809 /* For OpenCL we never want to strip the info from the types, and it makes
810 * type comparisons easier in later stages.
812 if (b
->options
->environment
== NIR_SPIRV_OPENCL
)
816 case vtn_variable_mode_input
:
817 case vtn_variable_mode_output
:
818 /* Layout decorations kept because we need offsets for XFB arrays of
821 return b
->shader
->info
.has_transform_feedback_varyings
;
823 case vtn_variable_mode_ssbo
:
824 case vtn_variable_mode_phys_ssbo
:
825 case vtn_variable_mode_ubo
:
833 const struct glsl_type
*
834 vtn_type_get_nir_type(struct vtn_builder
*b
, struct vtn_type
*type
,
835 enum vtn_variable_mode mode
)
837 if (mode
== vtn_variable_mode_atomic_counter
) {
838 vtn_fail_if(glsl_without_array(type
->type
) != glsl_uint_type(),
839 "Variables in the AtomicCounter storage class should be "
840 "(possibly arrays of arrays of) uint.");
841 return wrap_type_in_array(glsl_atomic_uint_type(), type
->type
);
844 if (mode
== vtn_variable_mode_uniform
) {
845 switch (type
->base_type
) {
846 case vtn_base_type_array
: {
847 const struct glsl_type
*elem_type
=
848 vtn_type_get_nir_type(b
, type
->array_element
, mode
);
850 return glsl_array_type(elem_type
, type
->length
,
851 glsl_get_explicit_stride(type
->type
));
854 case vtn_base_type_struct
: {
855 bool need_new_struct
= false;
856 const uint32_t num_fields
= type
->length
;
857 NIR_VLA(struct glsl_struct_field
, fields
, num_fields
);
858 for (unsigned i
= 0; i
< num_fields
; i
++) {
859 fields
[i
] = *glsl_get_struct_field_data(type
->type
, i
);
860 const struct glsl_type
*field_nir_type
=
861 vtn_type_get_nir_type(b
, type
->members
[i
], mode
);
862 if (fields
[i
].type
!= field_nir_type
) {
863 fields
[i
].type
= field_nir_type
;
864 need_new_struct
= true;
867 if (need_new_struct
) {
868 if (glsl_type_is_interface(type
->type
)) {
869 return glsl_interface_type(fields
, num_fields
,
870 /* packing */ 0, false,
871 glsl_get_type_name(type
->type
));
873 return glsl_struct_type(fields
, num_fields
,
874 glsl_get_type_name(type
->type
),
875 glsl_struct_type_is_packed(type
->type
));
878 /* No changes, just pass it on */
883 case vtn_base_type_image
:
884 return type
->glsl_image
;
886 case vtn_base_type_sampler
:
887 return glsl_bare_sampler_type();
889 case vtn_base_type_sampled_image
:
890 return type
->image
->glsl_image
;
897 /* Layout decorations are allowed but ignored in certain conditions,
898 * to allow SPIR-V generators perform type deduplication. Discard
899 * unnecessary ones when passing to NIR.
901 if (!vtn_type_needs_explicit_layout(b
, mode
))
902 return glsl_get_bare_type(type
->type
);
907 static struct vtn_type
*
908 mutable_matrix_member(struct vtn_builder
*b
, struct vtn_type
*type
, int member
)
910 type
->members
[member
] = vtn_type_copy(b
, type
->members
[member
]);
911 type
= type
->members
[member
];
913 /* We may have an array of matrices.... Oh, joy! */
914 while (glsl_type_is_array(type
->type
)) {
915 type
->array_element
= vtn_type_copy(b
, type
->array_element
);
916 type
= type
->array_element
;
919 vtn_assert(glsl_type_is_matrix(type
->type
));
925 vtn_handle_access_qualifier(struct vtn_builder
*b
, struct vtn_type
*type
,
926 int member
, enum gl_access_qualifier access
)
928 type
->members
[member
] = vtn_type_copy(b
, type
->members
[member
]);
929 type
= type
->members
[member
];
931 type
->access
|= access
;
935 array_stride_decoration_cb(struct vtn_builder
*b
,
936 struct vtn_value
*val
, int member
,
937 const struct vtn_decoration
*dec
, void *void_ctx
)
939 struct vtn_type
*type
= val
->type
;
941 if (dec
->decoration
== SpvDecorationArrayStride
) {
942 if (vtn_type_contains_block(b
, type
)) {
943 vtn_warn("The ArrayStride decoration cannot be applied to an array "
944 "type which contains a structure type decorated Block "
946 /* Ignore the decoration */
948 vtn_fail_if(dec
->operands
[0] == 0, "ArrayStride must be non-zero");
949 type
->stride
= dec
->operands
[0];
955 struct_member_decoration_cb(struct vtn_builder
*b
,
956 UNUSED
struct vtn_value
*val
, int member
,
957 const struct vtn_decoration
*dec
, void *void_ctx
)
959 struct member_decoration_ctx
*ctx
= void_ctx
;
964 assert(member
< ctx
->num_fields
);
966 switch (dec
->decoration
) {
967 case SpvDecorationRelaxedPrecision
:
968 case SpvDecorationUniform
:
969 case SpvDecorationUniformId
:
970 break; /* FIXME: Do nothing with this for now. */
971 case SpvDecorationNonWritable
:
972 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_NON_WRITEABLE
);
974 case SpvDecorationNonReadable
:
975 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_NON_READABLE
);
977 case SpvDecorationVolatile
:
978 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_VOLATILE
);
980 case SpvDecorationCoherent
:
981 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_COHERENT
);
983 case SpvDecorationNoPerspective
:
984 ctx
->fields
[member
].interpolation
= INTERP_MODE_NOPERSPECTIVE
;
986 case SpvDecorationFlat
:
987 ctx
->fields
[member
].interpolation
= INTERP_MODE_FLAT
;
989 case SpvDecorationExplicitInterpAMD
:
990 ctx
->fields
[member
].interpolation
= INTERP_MODE_EXPLICIT
;
992 case SpvDecorationCentroid
:
993 ctx
->fields
[member
].centroid
= true;
995 case SpvDecorationSample
:
996 ctx
->fields
[member
].sample
= true;
998 case SpvDecorationStream
:
999 /* This is handled later by var_decoration_cb in vtn_variables.c */
1001 case SpvDecorationLocation
:
1002 ctx
->fields
[member
].location
= dec
->operands
[0];
1004 case SpvDecorationComponent
:
1005 break; /* FIXME: What should we do with these? */
1006 case SpvDecorationBuiltIn
:
1007 ctx
->type
->members
[member
] = vtn_type_copy(b
, ctx
->type
->members
[member
]);
1008 ctx
->type
->members
[member
]->is_builtin
= true;
1009 ctx
->type
->members
[member
]->builtin
= dec
->operands
[0];
1010 ctx
->type
->builtin_block
= true;
1012 case SpvDecorationOffset
:
1013 ctx
->type
->offsets
[member
] = dec
->operands
[0];
1014 ctx
->fields
[member
].offset
= dec
->operands
[0];
1016 case SpvDecorationMatrixStride
:
1017 /* Handled as a second pass */
1019 case SpvDecorationColMajor
:
1020 break; /* Nothing to do here. Column-major is the default. */
1021 case SpvDecorationRowMajor
:
1022 mutable_matrix_member(b
, ctx
->type
, member
)->row_major
= true;
1025 case SpvDecorationPatch
:
1028 case SpvDecorationSpecId
:
1029 case SpvDecorationBlock
:
1030 case SpvDecorationBufferBlock
:
1031 case SpvDecorationArrayStride
:
1032 case SpvDecorationGLSLShared
:
1033 case SpvDecorationGLSLPacked
:
1034 case SpvDecorationInvariant
:
1035 case SpvDecorationRestrict
:
1036 case SpvDecorationAliased
:
1037 case SpvDecorationConstant
:
1038 case SpvDecorationIndex
:
1039 case SpvDecorationBinding
:
1040 case SpvDecorationDescriptorSet
:
1041 case SpvDecorationLinkageAttributes
:
1042 case SpvDecorationNoContraction
:
1043 case SpvDecorationInputAttachmentIndex
:
1044 vtn_warn("Decoration not allowed on struct members: %s",
1045 spirv_decoration_to_string(dec
->decoration
));
1048 case SpvDecorationXfbBuffer
:
1049 case SpvDecorationXfbStride
:
1050 /* This is handled later by var_decoration_cb in vtn_variables.c */
1053 case SpvDecorationCPacked
:
1054 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
)
1055 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1056 spirv_decoration_to_string(dec
->decoration
));
1058 ctx
->type
->packed
= true;
1061 case SpvDecorationSaturatedConversion
:
1062 case SpvDecorationFuncParamAttr
:
1063 case SpvDecorationFPRoundingMode
:
1064 case SpvDecorationFPFastMathMode
:
1065 case SpvDecorationAlignment
:
1066 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
) {
1067 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1068 spirv_decoration_to_string(dec
->decoration
));
1072 case SpvDecorationUserSemantic
:
1073 case SpvDecorationUserTypeGOOGLE
:
1074 /* User semantic decorations can safely be ignored by the driver. */
1078 vtn_fail_with_decoration("Unhandled decoration", dec
->decoration
);
1082 /** Chases the array type all the way down to the tail and rewrites the
1083 * glsl_types to be based off the tail's glsl_type.
1086 vtn_array_type_rewrite_glsl_type(struct vtn_type
*type
)
1088 if (type
->base_type
!= vtn_base_type_array
)
1091 vtn_array_type_rewrite_glsl_type(type
->array_element
);
1093 type
->type
= glsl_array_type(type
->array_element
->type
,
1094 type
->length
, type
->stride
);
1097 /* Matrix strides are handled as a separate pass because we need to know
1098 * whether the matrix is row-major or not first.
1101 struct_member_matrix_stride_cb(struct vtn_builder
*b
,
1102 UNUSED
struct vtn_value
*val
, int member
,
1103 const struct vtn_decoration
*dec
,
1106 if (dec
->decoration
!= SpvDecorationMatrixStride
)
1109 vtn_fail_if(member
< 0,
1110 "The MatrixStride decoration is only allowed on members "
1112 vtn_fail_if(dec
->operands
[0] == 0, "MatrixStride must be non-zero");
1114 struct member_decoration_ctx
*ctx
= void_ctx
;
1116 struct vtn_type
*mat_type
= mutable_matrix_member(b
, ctx
->type
, member
);
1117 if (mat_type
->row_major
) {
1118 mat_type
->array_element
= vtn_type_copy(b
, mat_type
->array_element
);
1119 mat_type
->stride
= mat_type
->array_element
->stride
;
1120 mat_type
->array_element
->stride
= dec
->operands
[0];
1122 mat_type
->type
= glsl_explicit_matrix_type(mat_type
->type
,
1123 dec
->operands
[0], true);
1124 mat_type
->array_element
->type
= glsl_get_column_type(mat_type
->type
);
1126 vtn_assert(mat_type
->array_element
->stride
> 0);
1127 mat_type
->stride
= dec
->operands
[0];
1129 mat_type
->type
= glsl_explicit_matrix_type(mat_type
->type
,
1130 dec
->operands
[0], false);
1133 /* Now that we've replaced the glsl_type with a properly strided matrix
1134 * type, rewrite the member type so that it's an array of the proper kind
1137 vtn_array_type_rewrite_glsl_type(ctx
->type
->members
[member
]);
1138 ctx
->fields
[member
].type
= ctx
->type
->members
[member
]->type
;
1142 struct_block_decoration_cb(struct vtn_builder
*b
,
1143 struct vtn_value
*val
, int member
,
1144 const struct vtn_decoration
*dec
, void *ctx
)
1149 struct vtn_type
*type
= val
->type
;
1150 if (dec
->decoration
== SpvDecorationBlock
)
1152 else if (dec
->decoration
== SpvDecorationBufferBlock
)
1153 type
->buffer_block
= true;
1157 type_decoration_cb(struct vtn_builder
*b
,
1158 struct vtn_value
*val
, int member
,
1159 const struct vtn_decoration
*dec
, UNUSED
void *ctx
)
1161 struct vtn_type
*type
= val
->type
;
1164 /* This should have been handled by OpTypeStruct */
1165 assert(val
->type
->base_type
== vtn_base_type_struct
);
1166 assert(member
>= 0 && member
< val
->type
->length
);
1170 switch (dec
->decoration
) {
1171 case SpvDecorationArrayStride
:
1172 vtn_assert(type
->base_type
== vtn_base_type_array
||
1173 type
->base_type
== vtn_base_type_pointer
);
1175 case SpvDecorationBlock
:
1176 vtn_assert(type
->base_type
== vtn_base_type_struct
);
1177 vtn_assert(type
->block
);
1179 case SpvDecorationBufferBlock
:
1180 vtn_assert(type
->base_type
== vtn_base_type_struct
);
1181 vtn_assert(type
->buffer_block
);
1183 case SpvDecorationGLSLShared
:
1184 case SpvDecorationGLSLPacked
:
1185 /* Ignore these, since we get explicit offsets anyways */
1188 case SpvDecorationRowMajor
:
1189 case SpvDecorationColMajor
:
1190 case SpvDecorationMatrixStride
:
1191 case SpvDecorationBuiltIn
:
1192 case SpvDecorationNoPerspective
:
1193 case SpvDecorationFlat
:
1194 case SpvDecorationPatch
:
1195 case SpvDecorationCentroid
:
1196 case SpvDecorationSample
:
1197 case SpvDecorationExplicitInterpAMD
:
1198 case SpvDecorationVolatile
:
1199 case SpvDecorationCoherent
:
1200 case SpvDecorationNonWritable
:
1201 case SpvDecorationNonReadable
:
1202 case SpvDecorationUniform
:
1203 case SpvDecorationUniformId
:
1204 case SpvDecorationLocation
:
1205 case SpvDecorationComponent
:
1206 case SpvDecorationOffset
:
1207 case SpvDecorationXfbBuffer
:
1208 case SpvDecorationXfbStride
:
1209 case SpvDecorationUserSemantic
:
1210 vtn_warn("Decoration only allowed for struct members: %s",
1211 spirv_decoration_to_string(dec
->decoration
));
1214 case SpvDecorationStream
:
1215 /* We don't need to do anything here, as stream is filled up when
1216 * aplying the decoration to a variable, just check that if it is not a
1217 * struct member, it should be a struct.
1219 vtn_assert(type
->base_type
== vtn_base_type_struct
);
1222 case SpvDecorationRelaxedPrecision
:
1223 case SpvDecorationSpecId
:
1224 case SpvDecorationInvariant
:
1225 case SpvDecorationRestrict
:
1226 case SpvDecorationAliased
:
1227 case SpvDecorationConstant
:
1228 case SpvDecorationIndex
:
1229 case SpvDecorationBinding
:
1230 case SpvDecorationDescriptorSet
:
1231 case SpvDecorationLinkageAttributes
:
1232 case SpvDecorationNoContraction
:
1233 case SpvDecorationInputAttachmentIndex
:
1234 vtn_warn("Decoration not allowed on types: %s",
1235 spirv_decoration_to_string(dec
->decoration
));
1238 case SpvDecorationCPacked
:
1239 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
)
1240 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1241 spirv_decoration_to_string(dec
->decoration
));
1243 type
->packed
= true;
1246 case SpvDecorationSaturatedConversion
:
1247 case SpvDecorationFuncParamAttr
:
1248 case SpvDecorationFPRoundingMode
:
1249 case SpvDecorationFPFastMathMode
:
1250 case SpvDecorationAlignment
:
1251 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1252 spirv_decoration_to_string(dec
->decoration
));
1255 case SpvDecorationUserTypeGOOGLE
:
1256 /* User semantic decorations can safely be ignored by the driver. */
1260 vtn_fail_with_decoration("Unhandled decoration", dec
->decoration
);
1265 translate_image_format(struct vtn_builder
*b
, SpvImageFormat format
)
1268 case SpvImageFormatUnknown
: return PIPE_FORMAT_NONE
;
1269 case SpvImageFormatRgba32f
: return PIPE_FORMAT_R32G32B32A32_FLOAT
;
1270 case SpvImageFormatRgba16f
: return PIPE_FORMAT_R16G16B16A16_FLOAT
;
1271 case SpvImageFormatR32f
: return PIPE_FORMAT_R32_FLOAT
;
1272 case SpvImageFormatRgba8
: return PIPE_FORMAT_R8G8B8A8_UNORM
;
1273 case SpvImageFormatRgba8Snorm
: return PIPE_FORMAT_R8G8B8A8_SNORM
;
1274 case SpvImageFormatRg32f
: return PIPE_FORMAT_R32G32_FLOAT
;
1275 case SpvImageFormatRg16f
: return PIPE_FORMAT_R16G16_FLOAT
;
1276 case SpvImageFormatR11fG11fB10f
: return PIPE_FORMAT_R11G11B10_FLOAT
;
1277 case SpvImageFormatR16f
: return PIPE_FORMAT_R16_FLOAT
;
1278 case SpvImageFormatRgba16
: return PIPE_FORMAT_R16G16B16A16_UNORM
;
1279 case SpvImageFormatRgb10A2
: return PIPE_FORMAT_R10G10B10A2_UNORM
;
1280 case SpvImageFormatRg16
: return PIPE_FORMAT_R16G16_UNORM
;
1281 case SpvImageFormatRg8
: return PIPE_FORMAT_R8G8_UNORM
;
1282 case SpvImageFormatR16
: return PIPE_FORMAT_R16_UNORM
;
1283 case SpvImageFormatR8
: return PIPE_FORMAT_R8_UNORM
;
1284 case SpvImageFormatRgba16Snorm
: return PIPE_FORMAT_R16G16B16A16_SNORM
;
1285 case SpvImageFormatRg16Snorm
: return PIPE_FORMAT_R16G16_SNORM
;
1286 case SpvImageFormatRg8Snorm
: return PIPE_FORMAT_R8G8_SNORM
;
1287 case SpvImageFormatR16Snorm
: return PIPE_FORMAT_R16_SNORM
;
1288 case SpvImageFormatR8Snorm
: return PIPE_FORMAT_R8_SNORM
;
1289 case SpvImageFormatRgba32i
: return PIPE_FORMAT_R32G32B32A32_SINT
;
1290 case SpvImageFormatRgba16i
: return PIPE_FORMAT_R16G16B16A16_SINT
;
1291 case SpvImageFormatRgba8i
: return PIPE_FORMAT_R8G8B8A8_SINT
;
1292 case SpvImageFormatR32i
: return PIPE_FORMAT_R32_SINT
;
1293 case SpvImageFormatRg32i
: return PIPE_FORMAT_R32G32_SINT
;
1294 case SpvImageFormatRg16i
: return PIPE_FORMAT_R16G16_SINT
;
1295 case SpvImageFormatRg8i
: return PIPE_FORMAT_R8G8_SINT
;
1296 case SpvImageFormatR16i
: return PIPE_FORMAT_R16_SINT
;
1297 case SpvImageFormatR8i
: return PIPE_FORMAT_R8_SINT
;
1298 case SpvImageFormatRgba32ui
: return PIPE_FORMAT_R32G32B32A32_UINT
;
1299 case SpvImageFormatRgba16ui
: return PIPE_FORMAT_R16G16B16A16_UINT
;
1300 case SpvImageFormatRgba8ui
: return PIPE_FORMAT_R8G8B8A8_UINT
;
1301 case SpvImageFormatR32ui
: return PIPE_FORMAT_R32_UINT
;
1302 case SpvImageFormatRgb10a2ui
: return PIPE_FORMAT_R10G10B10A2_UINT
;
1303 case SpvImageFormatRg32ui
: return PIPE_FORMAT_R32G32_UINT
;
1304 case SpvImageFormatRg16ui
: return PIPE_FORMAT_R16G16_UINT
;
1305 case SpvImageFormatRg8ui
: return PIPE_FORMAT_R8G8_UINT
;
1306 case SpvImageFormatR16ui
: return PIPE_FORMAT_R16_UINT
;
1307 case SpvImageFormatR8ui
: return PIPE_FORMAT_R8_UINT
;
1309 vtn_fail("Invalid image format: %s (%u)",
1310 spirv_imageformat_to_string(format
), format
);
1315 vtn_handle_type(struct vtn_builder
*b
, SpvOp opcode
,
1316 const uint32_t *w
, unsigned count
)
1318 struct vtn_value
*val
= NULL
;
1320 /* In order to properly handle forward declarations, we have to defer
1321 * allocation for pointer types.
1323 if (opcode
!= SpvOpTypePointer
&& opcode
!= SpvOpTypeForwardPointer
) {
1324 val
= vtn_push_value(b
, w
[1], vtn_value_type_type
);
1325 vtn_fail_if(val
->type
!= NULL
,
1326 "Only pointers can have forward declarations");
1327 val
->type
= rzalloc(b
, struct vtn_type
);
1328 val
->type
->id
= w
[1];
1333 val
->type
->base_type
= vtn_base_type_void
;
1334 val
->type
->type
= glsl_void_type();
1337 val
->type
->base_type
= vtn_base_type_scalar
;
1338 val
->type
->type
= glsl_bool_type();
1339 val
->type
->length
= 1;
1341 case SpvOpTypeInt
: {
1342 int bit_size
= w
[2];
1343 const bool signedness
= w
[3];
1344 val
->type
->base_type
= vtn_base_type_scalar
;
1347 val
->type
->type
= (signedness
? glsl_int64_t_type() : glsl_uint64_t_type());
1350 val
->type
->type
= (signedness
? glsl_int_type() : glsl_uint_type());
1353 val
->type
->type
= (signedness
? glsl_int16_t_type() : glsl_uint16_t_type());
1356 val
->type
->type
= (signedness
? glsl_int8_t_type() : glsl_uint8_t_type());
1359 vtn_fail("Invalid int bit size: %u", bit_size
);
1361 val
->type
->length
= 1;
1365 case SpvOpTypeFloat
: {
1366 int bit_size
= w
[2];
1367 val
->type
->base_type
= vtn_base_type_scalar
;
1370 val
->type
->type
= glsl_float16_t_type();
1373 val
->type
->type
= glsl_float_type();
1376 val
->type
->type
= glsl_double_type();
1379 vtn_fail("Invalid float bit size: %u", bit_size
);
1381 val
->type
->length
= 1;
1385 case SpvOpTypeVector
: {
1386 struct vtn_type
*base
= vtn_get_type(b
, w
[2]);
1387 unsigned elems
= w
[3];
1389 vtn_fail_if(base
->base_type
!= vtn_base_type_scalar
,
1390 "Base type for OpTypeVector must be a scalar");
1391 vtn_fail_if((elems
< 2 || elems
> 4) && (elems
!= 8) && (elems
!= 16),
1392 "Invalid component count for OpTypeVector");
1394 val
->type
->base_type
= vtn_base_type_vector
;
1395 val
->type
->type
= glsl_vector_type(glsl_get_base_type(base
->type
), elems
);
1396 val
->type
->length
= elems
;
1397 val
->type
->stride
= glsl_type_is_boolean(val
->type
->type
)
1398 ? 4 : glsl_get_bit_size(base
->type
) / 8;
1399 val
->type
->array_element
= base
;
1403 case SpvOpTypeMatrix
: {
1404 struct vtn_type
*base
= vtn_get_type(b
, w
[2]);
1405 unsigned columns
= w
[3];
1407 vtn_fail_if(base
->base_type
!= vtn_base_type_vector
,
1408 "Base type for OpTypeMatrix must be a vector");
1409 vtn_fail_if(columns
< 2 || columns
> 4,
1410 "Invalid column count for OpTypeMatrix");
1412 val
->type
->base_type
= vtn_base_type_matrix
;
1413 val
->type
->type
= glsl_matrix_type(glsl_get_base_type(base
->type
),
1414 glsl_get_vector_elements(base
->type
),
1416 vtn_fail_if(glsl_type_is_error(val
->type
->type
),
1417 "Unsupported base type for OpTypeMatrix");
1418 assert(!glsl_type_is_error(val
->type
->type
));
1419 val
->type
->length
= columns
;
1420 val
->type
->array_element
= base
;
1421 val
->type
->row_major
= false;
1422 val
->type
->stride
= 0;
1426 case SpvOpTypeRuntimeArray
:
1427 case SpvOpTypeArray
: {
1428 struct vtn_type
*array_element
= vtn_get_type(b
, w
[2]);
1430 if (opcode
== SpvOpTypeRuntimeArray
) {
1431 /* A length of 0 is used to denote unsized arrays */
1432 val
->type
->length
= 0;
1434 val
->type
->length
= vtn_constant_uint(b
, w
[3]);
1437 val
->type
->base_type
= vtn_base_type_array
;
1438 val
->type
->array_element
= array_element
;
1439 if (b
->shader
->info
.stage
== MESA_SHADER_KERNEL
)
1440 val
->type
->stride
= glsl_get_cl_size(array_element
->type
);
1442 vtn_foreach_decoration(b
, val
, array_stride_decoration_cb
, NULL
);
1443 val
->type
->type
= glsl_array_type(array_element
->type
, val
->type
->length
,
1448 case SpvOpTypeStruct
: {
1449 unsigned num_fields
= count
- 2;
1450 val
->type
->base_type
= vtn_base_type_struct
;
1451 val
->type
->length
= num_fields
;
1452 val
->type
->members
= ralloc_array(b
, struct vtn_type
*, num_fields
);
1453 val
->type
->offsets
= ralloc_array(b
, unsigned, num_fields
);
1454 val
->type
->packed
= false;
1456 NIR_VLA(struct glsl_struct_field
, fields
, count
);
1457 for (unsigned i
= 0; i
< num_fields
; i
++) {
1458 val
->type
->members
[i
] = vtn_get_type(b
, w
[i
+ 2]);
1459 fields
[i
] = (struct glsl_struct_field
) {
1460 .type
= val
->type
->members
[i
]->type
,
1461 .name
= ralloc_asprintf(b
, "field%d", i
),
1467 if (b
->shader
->info
.stage
== MESA_SHADER_KERNEL
) {
1468 unsigned offset
= 0;
1469 for (unsigned i
= 0; i
< num_fields
; i
++) {
1470 offset
= align(offset
, glsl_get_cl_alignment(fields
[i
].type
));
1471 fields
[i
].offset
= offset
;
1472 offset
+= glsl_get_cl_size(fields
[i
].type
);
1476 struct member_decoration_ctx ctx
= {
1477 .num_fields
= num_fields
,
1482 vtn_foreach_decoration(b
, val
, struct_member_decoration_cb
, &ctx
);
1483 vtn_foreach_decoration(b
, val
, struct_member_matrix_stride_cb
, &ctx
);
1485 vtn_foreach_decoration(b
, val
, struct_block_decoration_cb
, NULL
);
1487 const char *name
= val
->name
;
1489 if (val
->type
->block
|| val
->type
->buffer_block
) {
1490 /* Packing will be ignored since types coming from SPIR-V are
1491 * explicitly laid out.
1493 val
->type
->type
= glsl_interface_type(fields
, num_fields
,
1494 /* packing */ 0, false,
1495 name
? name
: "block");
1497 val
->type
->type
= glsl_struct_type(fields
, num_fields
,
1498 name
? name
: "struct", false);
1503 case SpvOpTypeFunction
: {
1504 val
->type
->base_type
= vtn_base_type_function
;
1505 val
->type
->type
= NULL
;
1507 val
->type
->return_type
= vtn_get_type(b
, w
[2]);
1509 const unsigned num_params
= count
- 3;
1510 val
->type
->length
= num_params
;
1511 val
->type
->params
= ralloc_array(b
, struct vtn_type
*, num_params
);
1512 for (unsigned i
= 0; i
< count
- 3; i
++) {
1513 val
->type
->params
[i
] = vtn_get_type(b
, w
[i
+ 3]);
1518 case SpvOpTypePointer
:
1519 case SpvOpTypeForwardPointer
: {
1520 /* We can't blindly push the value because it might be a forward
1523 val
= vtn_untyped_value(b
, w
[1]);
1525 SpvStorageClass storage_class
= w
[2];
1527 if (val
->value_type
== vtn_value_type_invalid
) {
1528 val
->value_type
= vtn_value_type_type
;
1529 val
->type
= rzalloc(b
, struct vtn_type
);
1530 val
->type
->id
= w
[1];
1531 val
->type
->base_type
= vtn_base_type_pointer
;
1532 val
->type
->storage_class
= storage_class
;
1534 /* These can actually be stored to nir_variables and used as SSA
1535 * values so they need a real glsl_type.
1537 enum vtn_variable_mode mode
= vtn_storage_class_to_mode(
1538 b
, storage_class
, NULL
, NULL
);
1539 val
->type
->type
= nir_address_format_to_glsl_type(
1540 vtn_mode_to_address_format(b
, mode
));
1542 vtn_fail_if(val
->type
->storage_class
!= storage_class
,
1543 "The storage classes of an OpTypePointer and any "
1544 "OpTypeForwardPointers that provide forward "
1545 "declarations of it must match.");
1548 if (opcode
== SpvOpTypePointer
) {
1549 vtn_fail_if(val
->type
->deref
!= NULL
,
1550 "While OpTypeForwardPointer can be used to provide a "
1551 "forward declaration of a pointer, OpTypePointer can "
1552 "only be used once for a given id.");
1554 val
->type
->deref
= vtn_get_type(b
, w
[3]);
1556 /* Only certain storage classes use ArrayStride. The others (in
1557 * particular Workgroup) are expected to be laid out by the driver.
1559 switch (storage_class
) {
1560 case SpvStorageClassUniform
:
1561 case SpvStorageClassPushConstant
:
1562 case SpvStorageClassStorageBuffer
:
1563 case SpvStorageClassPhysicalStorageBuffer
:
1564 vtn_foreach_decoration(b
, val
, array_stride_decoration_cb
, NULL
);
1567 /* Nothing to do. */
1571 if (b
->physical_ptrs
) {
1572 switch (storage_class
) {
1573 case SpvStorageClassFunction
:
1574 case SpvStorageClassWorkgroup
:
1575 case SpvStorageClassCrossWorkgroup
:
1576 case SpvStorageClassUniformConstant
:
1577 val
->type
->stride
= align(glsl_get_cl_size(val
->type
->deref
->type
),
1578 glsl_get_cl_alignment(val
->type
->deref
->type
));
1588 case SpvOpTypeImage
: {
1589 val
->type
->base_type
= vtn_base_type_image
;
1591 /* Images are represented in NIR as a scalar SSA value that is the
1592 * result of a deref instruction. An OpLoad on an OpTypeImage pointer
1593 * from UniformConstant memory just takes the NIR deref from the pointer
1594 * and turns it into an SSA value.
1596 val
->type
->type
= nir_address_format_to_glsl_type(
1597 vtn_mode_to_address_format(b
, vtn_variable_mode_function
));
1599 const struct vtn_type
*sampled_type
= vtn_get_type(b
, w
[2]);
1600 if (b
->shader
->info
.stage
== MESA_SHADER_KERNEL
) {
1601 vtn_fail_if(sampled_type
->base_type
!= vtn_base_type_void
,
1602 "Sampled type of OpTypeImage must be void for kernels");
1604 vtn_fail_if(sampled_type
->base_type
!= vtn_base_type_scalar
||
1605 glsl_get_bit_size(sampled_type
->type
) != 32,
1606 "Sampled type of OpTypeImage must be a 32-bit scalar");
1609 enum glsl_sampler_dim dim
;
1610 switch ((SpvDim
)w
[3]) {
1611 case SpvDim1D
: dim
= GLSL_SAMPLER_DIM_1D
; break;
1612 case SpvDim2D
: dim
= GLSL_SAMPLER_DIM_2D
; break;
1613 case SpvDim3D
: dim
= GLSL_SAMPLER_DIM_3D
; break;
1614 case SpvDimCube
: dim
= GLSL_SAMPLER_DIM_CUBE
; break;
1615 case SpvDimRect
: dim
= GLSL_SAMPLER_DIM_RECT
; break;
1616 case SpvDimBuffer
: dim
= GLSL_SAMPLER_DIM_BUF
; break;
1617 case SpvDimSubpassData
: dim
= GLSL_SAMPLER_DIM_SUBPASS
; break;
1619 vtn_fail("Invalid SPIR-V image dimensionality: %s (%u)",
1620 spirv_dim_to_string((SpvDim
)w
[3]), w
[3]);
1623 /* w[4]: as per Vulkan spec "Validation Rules within a Module",
1624 * The “Depth” operand of OpTypeImage is ignored.
1626 bool is_array
= w
[5];
1627 bool multisampled
= w
[6];
1628 unsigned sampled
= w
[7];
1629 SpvImageFormat format
= w
[8];
1632 val
->type
->access_qualifier
= w
[9];
1633 else if (b
->shader
->info
.stage
== MESA_SHADER_KERNEL
)
1634 /* Per the CL C spec: If no qualifier is provided, read_only is assumed. */
1635 val
->type
->access_qualifier
= SpvAccessQualifierReadOnly
;
1637 val
->type
->access_qualifier
= SpvAccessQualifierReadWrite
;
1640 if (dim
== GLSL_SAMPLER_DIM_2D
)
1641 dim
= GLSL_SAMPLER_DIM_MS
;
1642 else if (dim
== GLSL_SAMPLER_DIM_SUBPASS
)
1643 dim
= GLSL_SAMPLER_DIM_SUBPASS_MS
;
1645 vtn_fail("Unsupported multisampled image type");
1648 val
->type
->image_format
= translate_image_format(b
, format
);
1650 enum glsl_base_type sampled_base_type
=
1651 glsl_get_base_type(sampled_type
->type
);
1653 val
->type
->glsl_image
= glsl_sampler_type(dim
, false, is_array
,
1655 } else if (sampled
== 2) {
1656 val
->type
->glsl_image
= glsl_image_type(dim
, is_array
,
1658 } else if (b
->shader
->info
.stage
== MESA_SHADER_KERNEL
) {
1659 val
->type
->glsl_image
= glsl_image_type(dim
, is_array
,
1662 vtn_fail("We need to know if the image will be sampled");
1667 case SpvOpTypeSampledImage
: {
1668 val
->type
->base_type
= vtn_base_type_sampled_image
;
1669 val
->type
->image
= vtn_get_type(b
, w
[2]);
1671 /* Sampled images are represented NIR as a vec2 SSA value where each
1672 * component is the result of a deref instruction. The first component
1673 * is the image and the second is the sampler. An OpLoad on an
1674 * OpTypeSampledImage pointer from UniformConstant memory just takes
1675 * the NIR deref from the pointer and duplicates it to both vector
1678 nir_address_format addr_format
=
1679 vtn_mode_to_address_format(b
, vtn_variable_mode_function
);
1680 assert(nir_address_format_num_components(addr_format
) == 1);
1681 unsigned bit_size
= nir_address_format_bit_size(addr_format
);
1682 assert(bit_size
== 32 || bit_size
== 64);
1684 enum glsl_base_type base_type
=
1685 bit_size
== 32 ? GLSL_TYPE_UINT
: GLSL_TYPE_UINT64
;
1686 val
->type
->type
= glsl_vector_type(base_type
, 2);
1690 case SpvOpTypeSampler
:
1691 val
->type
->base_type
= vtn_base_type_sampler
;
1693 /* Samplers are represented in NIR as a scalar SSA value that is the
1694 * result of a deref instruction. An OpLoad on an OpTypeSampler pointer
1695 * from UniformConstant memory just takes the NIR deref from the pointer
1696 * and turns it into an SSA value.
1698 val
->type
->type
= nir_address_format_to_glsl_type(
1699 vtn_mode_to_address_format(b
, vtn_variable_mode_function
));
1702 case SpvOpTypeOpaque
:
1703 case SpvOpTypeEvent
:
1704 case SpvOpTypeDeviceEvent
:
1705 case SpvOpTypeReserveId
:
1706 case SpvOpTypeQueue
:
1709 vtn_fail_with_opcode("Unhandled opcode", opcode
);
1712 vtn_foreach_decoration(b
, val
, type_decoration_cb
, NULL
);
1714 if (val
->type
->base_type
== vtn_base_type_struct
&&
1715 (val
->type
->block
|| val
->type
->buffer_block
)) {
1716 for (unsigned i
= 0; i
< val
->type
->length
; i
++) {
1717 vtn_fail_if(vtn_type_contains_block(b
, val
->type
->members
[i
]),
1718 "Block and BufferBlock decorations cannot decorate a "
1719 "structure type that is nested at any level inside "
1720 "another structure type decorated with Block or "
1726 static nir_constant
*
1727 vtn_null_constant(struct vtn_builder
*b
, struct vtn_type
*type
)
1729 nir_constant
*c
= rzalloc(b
, nir_constant
);
1731 switch (type
->base_type
) {
1732 case vtn_base_type_scalar
:
1733 case vtn_base_type_vector
:
1734 /* Nothing to do here. It's already initialized to zero */
1737 case vtn_base_type_pointer
: {
1738 enum vtn_variable_mode mode
= vtn_storage_class_to_mode(
1739 b
, type
->storage_class
, type
->deref
, NULL
);
1740 nir_address_format addr_format
= vtn_mode_to_address_format(b
, mode
);
1742 const nir_const_value
*null_value
= nir_address_format_null_value(addr_format
);
1743 memcpy(c
->values
, null_value
,
1744 sizeof(nir_const_value
) * nir_address_format_num_components(addr_format
));
1748 case vtn_base_type_void
:
1749 case vtn_base_type_image
:
1750 case vtn_base_type_sampler
:
1751 case vtn_base_type_sampled_image
:
1752 case vtn_base_type_function
:
1753 /* For those we have to return something but it doesn't matter what. */
1756 case vtn_base_type_matrix
:
1757 case vtn_base_type_array
:
1758 vtn_assert(type
->length
> 0);
1759 c
->num_elements
= type
->length
;
1760 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
1762 c
->elements
[0] = vtn_null_constant(b
, type
->array_element
);
1763 for (unsigned i
= 1; i
< c
->num_elements
; i
++)
1764 c
->elements
[i
] = c
->elements
[0];
1767 case vtn_base_type_struct
:
1768 c
->num_elements
= type
->length
;
1769 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
1770 for (unsigned i
= 0; i
< c
->num_elements
; i
++)
1771 c
->elements
[i
] = vtn_null_constant(b
, type
->members
[i
]);
1775 vtn_fail("Invalid type for null constant");
1782 spec_constant_decoration_cb(struct vtn_builder
*b
, UNUSED
struct vtn_value
*val
,
1783 ASSERTED
int member
,
1784 const struct vtn_decoration
*dec
, void *data
)
1786 vtn_assert(member
== -1);
1787 if (dec
->decoration
!= SpvDecorationSpecId
)
1790 nir_const_value
*value
= data
;
1791 for (unsigned i
= 0; i
< b
->num_specializations
; i
++) {
1792 if (b
->specializations
[i
].id
== dec
->operands
[0]) {
1793 *value
= b
->specializations
[i
].value
;
1800 handle_workgroup_size_decoration_cb(struct vtn_builder
*b
,
1801 struct vtn_value
*val
,
1802 ASSERTED
int member
,
1803 const struct vtn_decoration
*dec
,
1806 vtn_assert(member
== -1);
1807 if (dec
->decoration
!= SpvDecorationBuiltIn
||
1808 dec
->operands
[0] != SpvBuiltInWorkgroupSize
)
1811 vtn_assert(val
->type
->type
== glsl_vector_type(GLSL_TYPE_UINT
, 3));
1812 b
->workgroup_size_builtin
= val
;
1816 vtn_handle_constant(struct vtn_builder
*b
, SpvOp opcode
,
1817 const uint32_t *w
, unsigned count
)
1819 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_constant
);
1820 val
->constant
= rzalloc(b
, nir_constant
);
1822 case SpvOpConstantTrue
:
1823 case SpvOpConstantFalse
:
1824 case SpvOpSpecConstantTrue
:
1825 case SpvOpSpecConstantFalse
: {
1826 vtn_fail_if(val
->type
->type
!= glsl_bool_type(),
1827 "Result type of %s must be OpTypeBool",
1828 spirv_op_to_string(opcode
));
1830 bool bval
= (opcode
== SpvOpConstantTrue
||
1831 opcode
== SpvOpSpecConstantTrue
);
1833 nir_const_value u32val
= nir_const_value_for_uint(bval
, 32);
1835 if (opcode
== SpvOpSpecConstantTrue
||
1836 opcode
== SpvOpSpecConstantFalse
)
1837 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &u32val
);
1839 val
->constant
->values
[0].b
= u32val
.u32
!= 0;
1844 case SpvOpSpecConstant
: {
1845 vtn_fail_if(val
->type
->base_type
!= vtn_base_type_scalar
,
1846 "Result type of %s must be a scalar",
1847 spirv_op_to_string(opcode
));
1848 int bit_size
= glsl_get_bit_size(val
->type
->type
);
1851 val
->constant
->values
[0].u64
= vtn_u64_literal(&w
[3]);
1854 val
->constant
->values
[0].u32
= w
[3];
1857 val
->constant
->values
[0].u16
= w
[3];
1860 val
->constant
->values
[0].u8
= w
[3];
1863 vtn_fail("Unsupported SpvOpConstant bit size: %u", bit_size
);
1866 if (opcode
== SpvOpSpecConstant
)
1867 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
,
1868 &val
->constant
->values
[0]);
1872 case SpvOpSpecConstantComposite
:
1873 case SpvOpConstantComposite
: {
1874 unsigned elem_count
= count
- 3;
1875 vtn_fail_if(elem_count
!= val
->type
->length
,
1876 "%s has %u constituents, expected %u",
1877 spirv_op_to_string(opcode
), elem_count
, val
->type
->length
);
1879 nir_constant
**elems
= ralloc_array(b
, nir_constant
*, elem_count
);
1880 for (unsigned i
= 0; i
< elem_count
; i
++) {
1881 struct vtn_value
*val
= vtn_untyped_value(b
, w
[i
+ 3]);
1883 if (val
->value_type
== vtn_value_type_constant
) {
1884 elems
[i
] = val
->constant
;
1886 vtn_fail_if(val
->value_type
!= vtn_value_type_undef
,
1887 "only constants or undefs allowed for "
1888 "SpvOpConstantComposite");
1889 /* to make it easier, just insert a NULL constant for now */
1890 elems
[i
] = vtn_null_constant(b
, val
->type
);
1894 switch (val
->type
->base_type
) {
1895 case vtn_base_type_vector
: {
1896 assert(glsl_type_is_vector(val
->type
->type
));
1897 for (unsigned i
= 0; i
< elem_count
; i
++)
1898 val
->constant
->values
[i
] = elems
[i
]->values
[0];
1902 case vtn_base_type_matrix
:
1903 case vtn_base_type_struct
:
1904 case vtn_base_type_array
:
1905 ralloc_steal(val
->constant
, elems
);
1906 val
->constant
->num_elements
= elem_count
;
1907 val
->constant
->elements
= elems
;
1911 vtn_fail("Result type of %s must be a composite type",
1912 spirv_op_to_string(opcode
));
1917 case SpvOpSpecConstantOp
: {
1918 nir_const_value u32op
= nir_const_value_for_uint(w
[3], 32);
1919 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &u32op
);
1920 SpvOp opcode
= u32op
.u32
;
1922 case SpvOpVectorShuffle
: {
1923 struct vtn_value
*v0
= &b
->values
[w
[4]];
1924 struct vtn_value
*v1
= &b
->values
[w
[5]];
1926 vtn_assert(v0
->value_type
== vtn_value_type_constant
||
1927 v0
->value_type
== vtn_value_type_undef
);
1928 vtn_assert(v1
->value_type
== vtn_value_type_constant
||
1929 v1
->value_type
== vtn_value_type_undef
);
1931 unsigned len0
= glsl_get_vector_elements(v0
->type
->type
);
1932 unsigned len1
= glsl_get_vector_elements(v1
->type
->type
);
1934 vtn_assert(len0
+ len1
< 16);
1936 unsigned bit_size
= glsl_get_bit_size(val
->type
->type
);
1937 unsigned bit_size0
= glsl_get_bit_size(v0
->type
->type
);
1938 unsigned bit_size1
= glsl_get_bit_size(v1
->type
->type
);
1940 vtn_assert(bit_size
== bit_size0
&& bit_size
== bit_size1
);
1941 (void)bit_size0
; (void)bit_size1
;
1943 nir_const_value undef
= { .u64
= 0xdeadbeefdeadbeef };
1944 nir_const_value combined
[NIR_MAX_VEC_COMPONENTS
* 2];
1946 if (v0
->value_type
== vtn_value_type_constant
) {
1947 for (unsigned i
= 0; i
< len0
; i
++)
1948 combined
[i
] = v0
->constant
->values
[i
];
1950 if (v1
->value_type
== vtn_value_type_constant
) {
1951 for (unsigned i
= 0; i
< len1
; i
++)
1952 combined
[len0
+ i
] = v1
->constant
->values
[i
];
1955 for (unsigned i
= 0, j
= 0; i
< count
- 6; i
++, j
++) {
1956 uint32_t comp
= w
[i
+ 6];
1957 if (comp
== (uint32_t)-1) {
1958 /* If component is not used, set the value to a known constant
1959 * to detect if it is wrongly used.
1961 val
->constant
->values
[j
] = undef
;
1963 vtn_fail_if(comp
>= len0
+ len1
,
1964 "All Component literals must either be FFFFFFFF "
1965 "or in [0, N - 1] (inclusive).");
1966 val
->constant
->values
[j
] = combined
[comp
];
1972 case SpvOpCompositeExtract
:
1973 case SpvOpCompositeInsert
: {
1974 struct vtn_value
*comp
;
1975 unsigned deref_start
;
1976 struct nir_constant
**c
;
1977 if (opcode
== SpvOpCompositeExtract
) {
1978 comp
= vtn_value(b
, w
[4], vtn_value_type_constant
);
1980 c
= &comp
->constant
;
1982 comp
= vtn_value(b
, w
[5], vtn_value_type_constant
);
1984 val
->constant
= nir_constant_clone(comp
->constant
,
1990 const struct vtn_type
*type
= comp
->type
;
1991 for (unsigned i
= deref_start
; i
< count
; i
++) {
1992 vtn_fail_if(w
[i
] > type
->length
,
1993 "%uth index of %s is %u but the type has only "
1994 "%u elements", i
- deref_start
,
1995 spirv_op_to_string(opcode
), w
[i
], type
->length
);
1997 switch (type
->base_type
) {
1998 case vtn_base_type_vector
:
2000 type
= type
->array_element
;
2003 case vtn_base_type_matrix
:
2004 case vtn_base_type_array
:
2005 c
= &(*c
)->elements
[w
[i
]];
2006 type
= type
->array_element
;
2009 case vtn_base_type_struct
:
2010 c
= &(*c
)->elements
[w
[i
]];
2011 type
= type
->members
[w
[i
]];
2015 vtn_fail("%s must only index into composite types",
2016 spirv_op_to_string(opcode
));
2020 if (opcode
== SpvOpCompositeExtract
) {
2024 unsigned num_components
= type
->length
;
2025 for (unsigned i
= 0; i
< num_components
; i
++)
2026 val
->constant
->values
[i
] = (*c
)->values
[elem
+ i
];
2029 struct vtn_value
*insert
=
2030 vtn_value(b
, w
[4], vtn_value_type_constant
);
2031 vtn_assert(insert
->type
== type
);
2033 *c
= insert
->constant
;
2035 unsigned num_components
= type
->length
;
2036 for (unsigned i
= 0; i
< num_components
; i
++)
2037 (*c
)->values
[elem
+ i
] = insert
->constant
->values
[i
];
2045 nir_alu_type dst_alu_type
= nir_get_nir_type_for_glsl_type(val
->type
->type
);
2046 nir_alu_type src_alu_type
= dst_alu_type
;
2047 unsigned num_components
= glsl_get_vector_elements(val
->type
->type
);
2050 vtn_assert(count
<= 7);
2056 /* We have a source in a conversion */
2058 nir_get_nir_type_for_glsl_type(vtn_get_value_type(b
, w
[4])->type
);
2059 /* We use the bitsize of the conversion source to evaluate the opcode later */
2060 bit_size
= glsl_get_bit_size(vtn_get_value_type(b
, w
[4])->type
);
2063 bit_size
= glsl_get_bit_size(val
->type
->type
);
2066 nir_op op
= vtn_nir_alu_op_for_spirv_opcode(b
, opcode
, &swap
,
2067 nir_alu_type_get_type_size(src_alu_type
),
2068 nir_alu_type_get_type_size(dst_alu_type
));
2069 nir_const_value src
[3][NIR_MAX_VEC_COMPONENTS
];
2071 for (unsigned i
= 0; i
< count
- 4; i
++) {
2072 struct vtn_value
*src_val
=
2073 vtn_value(b
, w
[4 + i
], vtn_value_type_constant
);
2075 /* If this is an unsized source, pull the bit size from the
2076 * source; otherwise, we'll use the bit size from the destination.
2078 if (!nir_alu_type_get_type_size(nir_op_infos
[op
].input_types
[i
]))
2079 bit_size
= glsl_get_bit_size(src_val
->type
->type
);
2081 unsigned src_comps
= nir_op_infos
[op
].input_sizes
[i
] ?
2082 nir_op_infos
[op
].input_sizes
[i
] :
2085 unsigned j
= swap
? 1 - i
: i
;
2086 for (unsigned c
= 0; c
< src_comps
; c
++)
2087 src
[j
][c
] = src_val
->constant
->values
[c
];
2090 /* fix up fixed size sources */
2097 for (unsigned i
= 0; i
< num_components
; ++i
) {
2099 case 64: src
[1][i
].u32
= src
[1][i
].u64
; break;
2100 case 16: src
[1][i
].u32
= src
[1][i
].u16
; break;
2101 case 8: src
[1][i
].u32
= src
[1][i
].u8
; break;
2110 nir_const_value
*srcs
[3] = {
2111 src
[0], src
[1], src
[2],
2113 nir_eval_const_opcode(op
, val
->constant
->values
,
2114 num_components
, bit_size
, srcs
,
2115 b
->shader
->info
.float_controls_execution_mode
);
2122 case SpvOpConstantNull
:
2123 val
->constant
= vtn_null_constant(b
, val
->type
);
2127 vtn_fail_with_opcode("Unhandled opcode", opcode
);
2130 /* Now that we have the value, update the workgroup size if needed */
2131 vtn_foreach_decoration(b
, val
, handle_workgroup_size_decoration_cb
, NULL
);
2135 vtn_split_barrier_semantics(struct vtn_builder
*b
,
2136 SpvMemorySemanticsMask semantics
,
2137 SpvMemorySemanticsMask
*before
,
2138 SpvMemorySemanticsMask
*after
)
2140 /* For memory semantics embedded in operations, we split them into up to
2141 * two barriers, to be added before and after the operation. This is less
2142 * strict than if we propagated until the final backend stage, but still
2143 * result in correct execution.
2145 * A further improvement could be pipe this information (and use!) into the
2146 * next compiler layers, at the expense of making the handling of barriers
2150 *before
= SpvMemorySemanticsMaskNone
;
2151 *after
= SpvMemorySemanticsMaskNone
;
2153 SpvMemorySemanticsMask order_semantics
=
2154 semantics
& (SpvMemorySemanticsAcquireMask
|
2155 SpvMemorySemanticsReleaseMask
|
2156 SpvMemorySemanticsAcquireReleaseMask
|
2157 SpvMemorySemanticsSequentiallyConsistentMask
);
2159 if (util_bitcount(order_semantics
) > 1) {
2160 /* Old GLSLang versions incorrectly set all the ordering bits. This was
2161 * fixed in c51287d744fb6e7e9ccc09f6f8451e6c64b1dad6 of glslang repo,
2162 * and it is in GLSLang since revision "SPIRV99.1321" (from Jul-2016).
2164 vtn_warn("Multiple memory ordering semantics specified, "
2165 "assuming AcquireRelease.");
2166 order_semantics
= SpvMemorySemanticsAcquireReleaseMask
;
2169 const SpvMemorySemanticsMask av_vis_semantics
=
2170 semantics
& (SpvMemorySemanticsMakeAvailableMask
|
2171 SpvMemorySemanticsMakeVisibleMask
);
2173 const SpvMemorySemanticsMask storage_semantics
=
2174 semantics
& (SpvMemorySemanticsUniformMemoryMask
|
2175 SpvMemorySemanticsSubgroupMemoryMask
|
2176 SpvMemorySemanticsWorkgroupMemoryMask
|
2177 SpvMemorySemanticsCrossWorkgroupMemoryMask
|
2178 SpvMemorySemanticsAtomicCounterMemoryMask
|
2179 SpvMemorySemanticsImageMemoryMask
|
2180 SpvMemorySemanticsOutputMemoryMask
);
2182 const SpvMemorySemanticsMask other_semantics
=
2183 semantics
& ~(order_semantics
| av_vis_semantics
| storage_semantics
|
2184 SpvMemorySemanticsVolatileMask
);
2186 if (other_semantics
)
2187 vtn_warn("Ignoring unhandled memory semantics: %u\n", other_semantics
);
2189 /* SequentiallyConsistent is treated as AcquireRelease. */
2191 /* The RELEASE barrier happens BEFORE the operation, and it is usually
2192 * associated with a Store. All the write operations with a matching
2193 * semantics will not be reordered after the Store.
2195 if (order_semantics
& (SpvMemorySemanticsReleaseMask
|
2196 SpvMemorySemanticsAcquireReleaseMask
|
2197 SpvMemorySemanticsSequentiallyConsistentMask
)) {
2198 *before
|= SpvMemorySemanticsReleaseMask
| storage_semantics
;
2201 /* The ACQUIRE barrier happens AFTER the operation, and it is usually
2202 * associated with a Load. All the operations with a matching semantics
2203 * will not be reordered before the Load.
2205 if (order_semantics
& (SpvMemorySemanticsAcquireMask
|
2206 SpvMemorySemanticsAcquireReleaseMask
|
2207 SpvMemorySemanticsSequentiallyConsistentMask
)) {
2208 *after
|= SpvMemorySemanticsAcquireMask
| storage_semantics
;
2211 if (av_vis_semantics
& SpvMemorySemanticsMakeVisibleMask
)
2212 *before
|= SpvMemorySemanticsMakeVisibleMask
| storage_semantics
;
2214 if (av_vis_semantics
& SpvMemorySemanticsMakeAvailableMask
)
2215 *after
|= SpvMemorySemanticsMakeAvailableMask
| storage_semantics
;
2218 static nir_memory_semantics
2219 vtn_mem_semantics_to_nir_mem_semantics(struct vtn_builder
*b
,
2220 SpvMemorySemanticsMask semantics
)
2222 nir_memory_semantics nir_semantics
= 0;
2224 SpvMemorySemanticsMask order_semantics
=
2225 semantics
& (SpvMemorySemanticsAcquireMask
|
2226 SpvMemorySemanticsReleaseMask
|
2227 SpvMemorySemanticsAcquireReleaseMask
|
2228 SpvMemorySemanticsSequentiallyConsistentMask
);
2230 if (util_bitcount(order_semantics
) > 1) {
2231 /* Old GLSLang versions incorrectly set all the ordering bits. This was
2232 * fixed in c51287d744fb6e7e9ccc09f6f8451e6c64b1dad6 of glslang repo,
2233 * and it is in GLSLang since revision "SPIRV99.1321" (from Jul-2016).
2235 vtn_warn("Multiple memory ordering semantics bits specified, "
2236 "assuming AcquireRelease.");
2237 order_semantics
= SpvMemorySemanticsAcquireReleaseMask
;
2240 switch (order_semantics
) {
2242 /* Not an ordering barrier. */
2245 case SpvMemorySemanticsAcquireMask
:
2246 nir_semantics
= NIR_MEMORY_ACQUIRE
;
2249 case SpvMemorySemanticsReleaseMask
:
2250 nir_semantics
= NIR_MEMORY_RELEASE
;
2253 case SpvMemorySemanticsSequentiallyConsistentMask
:
2254 /* Fall through. Treated as AcquireRelease in Vulkan. */
2255 case SpvMemorySemanticsAcquireReleaseMask
:
2256 nir_semantics
= NIR_MEMORY_ACQUIRE
| NIR_MEMORY_RELEASE
;
2260 unreachable("Invalid memory order semantics");
2263 if (semantics
& SpvMemorySemanticsMakeAvailableMask
) {
2264 vtn_fail_if(!b
->options
->caps
.vk_memory_model
,
2265 "To use MakeAvailable memory semantics the VulkanMemoryModel "
2266 "capability must be declared.");
2267 nir_semantics
|= NIR_MEMORY_MAKE_AVAILABLE
;
2270 if (semantics
& SpvMemorySemanticsMakeVisibleMask
) {
2271 vtn_fail_if(!b
->options
->caps
.vk_memory_model
,
2272 "To use MakeVisible memory semantics the VulkanMemoryModel "
2273 "capability must be declared.");
2274 nir_semantics
|= NIR_MEMORY_MAKE_VISIBLE
;
2277 return nir_semantics
;
2280 static nir_variable_mode
2281 vtn_mem_sematics_to_nir_var_modes(struct vtn_builder
*b
,
2282 SpvMemorySemanticsMask semantics
)
2284 /* Vulkan Environment for SPIR-V says "SubgroupMemory, CrossWorkgroupMemory,
2285 * and AtomicCounterMemory are ignored".
2287 semantics
&= ~(SpvMemorySemanticsSubgroupMemoryMask
|
2288 SpvMemorySemanticsCrossWorkgroupMemoryMask
|
2289 SpvMemorySemanticsAtomicCounterMemoryMask
);
2291 /* TODO: Consider adding nir_var_mem_image mode to NIR so it can be used
2292 * for SpvMemorySemanticsImageMemoryMask.
2295 nir_variable_mode modes
= 0;
2296 if (semantics
& (SpvMemorySemanticsUniformMemoryMask
|
2297 SpvMemorySemanticsImageMemoryMask
)) {
2298 modes
|= nir_var_uniform
|
2303 if (semantics
& SpvMemorySemanticsWorkgroupMemoryMask
)
2304 modes
|= nir_var_mem_shared
;
2305 if (semantics
& SpvMemorySemanticsOutputMemoryMask
) {
2306 modes
|= nir_var_shader_out
;
2313 vtn_scope_to_nir_scope(struct vtn_builder
*b
, SpvScope scope
)
2315 nir_scope nir_scope
;
2317 case SpvScopeDevice
:
2318 vtn_fail_if(b
->options
->caps
.vk_memory_model
&&
2319 !b
->options
->caps
.vk_memory_model_device_scope
,
2320 "If the Vulkan memory model is declared and any instruction "
2321 "uses Device scope, the VulkanMemoryModelDeviceScope "
2322 "capability must be declared.");
2323 nir_scope
= NIR_SCOPE_DEVICE
;
2326 case SpvScopeQueueFamily
:
2327 vtn_fail_if(!b
->options
->caps
.vk_memory_model
,
2328 "To use Queue Family scope, the VulkanMemoryModel capability "
2329 "must be declared.");
2330 nir_scope
= NIR_SCOPE_QUEUE_FAMILY
;
2333 case SpvScopeWorkgroup
:
2334 nir_scope
= NIR_SCOPE_WORKGROUP
;
2337 case SpvScopeSubgroup
:
2338 nir_scope
= NIR_SCOPE_SUBGROUP
;
2341 case SpvScopeInvocation
:
2342 nir_scope
= NIR_SCOPE_INVOCATION
;
2346 vtn_fail("Invalid memory scope");
2353 vtn_emit_scoped_control_barrier(struct vtn_builder
*b
, SpvScope exec_scope
,
2355 SpvMemorySemanticsMask semantics
)
2357 nir_memory_semantics nir_semantics
=
2358 vtn_mem_semantics_to_nir_mem_semantics(b
, semantics
);
2359 nir_variable_mode modes
= vtn_mem_sematics_to_nir_var_modes(b
, semantics
);
2360 nir_scope nir_exec_scope
= vtn_scope_to_nir_scope(b
, exec_scope
);
2362 /* Memory semantics is optional for OpControlBarrier. */
2363 nir_scope nir_mem_scope
;
2364 if (nir_semantics
== 0 || modes
== 0)
2365 nir_mem_scope
= NIR_SCOPE_NONE
;
2367 nir_mem_scope
= vtn_scope_to_nir_scope(b
, mem_scope
);
2369 nir_scoped_barrier(&b
->nb
, nir_exec_scope
, nir_mem_scope
, nir_semantics
, modes
);
2373 vtn_emit_scoped_memory_barrier(struct vtn_builder
*b
, SpvScope scope
,
2374 SpvMemorySemanticsMask semantics
)
2376 nir_variable_mode modes
= vtn_mem_sematics_to_nir_var_modes(b
, semantics
);
2377 nir_memory_semantics nir_semantics
=
2378 vtn_mem_semantics_to_nir_mem_semantics(b
, semantics
);
2380 /* No barrier to add. */
2381 if (nir_semantics
== 0 || modes
== 0)
2384 nir_scope nir_mem_scope
= vtn_scope_to_nir_scope(b
, scope
);
2385 nir_scoped_barrier(&b
->nb
, NIR_SCOPE_NONE
, nir_mem_scope
, nir_semantics
, modes
);
2388 struct vtn_ssa_value
*
2389 vtn_create_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
2391 /* Always use bare types for SSA values for a couple of reasons:
2393 * 1. Code which emits deref chains should never listen to the explicit
2394 * layout information on the SSA value if any exists. If we've
2395 * accidentally been relying on this, we want to find those bugs.
2397 * 2. We want to be able to quickly check that an SSA value being assigned
2398 * to a SPIR-V value has the right type. Using bare types everywhere
2399 * ensures that we can pointer-compare.
2401 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
2402 val
->type
= glsl_get_bare_type(type
);
2405 if (!glsl_type_is_vector_or_scalar(type
)) {
2406 unsigned elems
= glsl_get_length(val
->type
);
2407 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
2408 if (glsl_type_is_array_or_matrix(type
)) {
2409 const struct glsl_type
*elem_type
= glsl_get_array_element(type
);
2410 for (unsigned i
= 0; i
< elems
; i
++)
2411 val
->elems
[i
] = vtn_create_ssa_value(b
, elem_type
);
2413 vtn_assert(glsl_type_is_struct_or_ifc(type
));
2414 for (unsigned i
= 0; i
< elems
; i
++) {
2415 const struct glsl_type
*elem_type
= glsl_get_struct_field(type
, i
);
2416 val
->elems
[i
] = vtn_create_ssa_value(b
, elem_type
);
2425 vtn_tex_src(struct vtn_builder
*b
, unsigned index
, nir_tex_src_type type
)
2428 src
.src
= nir_src_for_ssa(vtn_get_nir_ssa(b
, index
));
2429 src
.src_type
= type
;
2434 image_operand_arg(struct vtn_builder
*b
, const uint32_t *w
, uint32_t count
,
2435 uint32_t mask_idx
, SpvImageOperandsMask op
)
2437 static const SpvImageOperandsMask ops_with_arg
=
2438 SpvImageOperandsBiasMask
|
2439 SpvImageOperandsLodMask
|
2440 SpvImageOperandsGradMask
|
2441 SpvImageOperandsConstOffsetMask
|
2442 SpvImageOperandsOffsetMask
|
2443 SpvImageOperandsConstOffsetsMask
|
2444 SpvImageOperandsSampleMask
|
2445 SpvImageOperandsMinLodMask
|
2446 SpvImageOperandsMakeTexelAvailableMask
|
2447 SpvImageOperandsMakeTexelVisibleMask
;
2449 assert(util_bitcount(op
) == 1);
2450 assert(w
[mask_idx
] & op
);
2451 assert(op
& ops_with_arg
);
2453 uint32_t idx
= util_bitcount(w
[mask_idx
] & (op
- 1) & ops_with_arg
) + 1;
2455 /* Adjust indices for operands with two arguments. */
2456 static const SpvImageOperandsMask ops_with_two_args
=
2457 SpvImageOperandsGradMask
;
2458 idx
+= util_bitcount(w
[mask_idx
] & (op
- 1) & ops_with_two_args
);
2462 vtn_fail_if(idx
+ (op
& ops_with_two_args
? 1 : 0) >= count
,
2463 "Image op claims to have %s but does not enough "
2464 "following operands", spirv_imageoperands_to_string(op
));
2470 non_uniform_decoration_cb(struct vtn_builder
*b
,
2471 struct vtn_value
*val
, int member
,
2472 const struct vtn_decoration
*dec
, void *void_ctx
)
2474 enum gl_access_qualifier
*access
= void_ctx
;
2475 switch (dec
->decoration
) {
2476 case SpvDecorationNonUniformEXT
:
2477 *access
|= ACCESS_NON_UNIFORM
;
2486 vtn_handle_texture(struct vtn_builder
*b
, SpvOp opcode
,
2487 const uint32_t *w
, unsigned count
)
2489 struct vtn_type
*ret_type
= vtn_get_type(b
, w
[1]);
2491 if (opcode
== SpvOpSampledImage
) {
2492 struct vtn_sampled_image si
= {
2493 .image
= vtn_get_image(b
, w
[3]),
2494 .sampler
= vtn_get_sampler(b
, w
[4]),
2497 enum gl_access_qualifier access
= 0;
2498 vtn_foreach_decoration(b
, vtn_untyped_value(b
, w
[3]),
2499 non_uniform_decoration_cb
, &access
);
2500 vtn_foreach_decoration(b
, vtn_untyped_value(b
, w
[4]),
2501 non_uniform_decoration_cb
, &access
);
2503 vtn_push_sampled_image(b
, w
[2], si
, access
& ACCESS_NON_UNIFORM
);
2505 } else if (opcode
== SpvOpImage
) {
2506 struct vtn_sampled_image si
= vtn_get_sampled_image(b
, w
[3]);
2508 enum gl_access_qualifier access
= 0;
2509 vtn_foreach_decoration(b
, vtn_untyped_value(b
, w
[3]),
2510 non_uniform_decoration_cb
, &access
);
2512 vtn_push_image(b
, w
[2], si
.image
, access
& ACCESS_NON_UNIFORM
);
2516 nir_deref_instr
*image
= NULL
, *sampler
= NULL
;
2517 struct vtn_value
*sampled_val
= vtn_untyped_value(b
, w
[3]);
2518 if (sampled_val
->type
->base_type
== vtn_base_type_sampled_image
) {
2519 struct vtn_sampled_image si
= vtn_get_sampled_image(b
, w
[3]);
2521 sampler
= si
.sampler
;
2523 image
= vtn_get_image(b
, w
[3]);
2526 const enum glsl_sampler_dim sampler_dim
= glsl_get_sampler_dim(image
->type
);
2527 const bool is_array
= glsl_sampler_type_is_array(image
->type
);
2528 nir_alu_type dest_type
= nir_type_invalid
;
2530 /* Figure out the base texture operation */
2533 case SpvOpImageSampleImplicitLod
:
2534 case SpvOpImageSampleDrefImplicitLod
:
2535 case SpvOpImageSampleProjImplicitLod
:
2536 case SpvOpImageSampleProjDrefImplicitLod
:
2537 texop
= nir_texop_tex
;
2540 case SpvOpImageSampleExplicitLod
:
2541 case SpvOpImageSampleDrefExplicitLod
:
2542 case SpvOpImageSampleProjExplicitLod
:
2543 case SpvOpImageSampleProjDrefExplicitLod
:
2544 texop
= nir_texop_txl
;
2547 case SpvOpImageFetch
:
2548 if (sampler_dim
== GLSL_SAMPLER_DIM_MS
) {
2549 texop
= nir_texop_txf_ms
;
2551 texop
= nir_texop_txf
;
2555 case SpvOpImageGather
:
2556 case SpvOpImageDrefGather
:
2557 texop
= nir_texop_tg4
;
2560 case SpvOpImageQuerySizeLod
:
2561 case SpvOpImageQuerySize
:
2562 texop
= nir_texop_txs
;
2563 dest_type
= nir_type_int
;
2566 case SpvOpImageQueryLod
:
2567 texop
= nir_texop_lod
;
2568 dest_type
= nir_type_float
;
2571 case SpvOpImageQueryLevels
:
2572 texop
= nir_texop_query_levels
;
2573 dest_type
= nir_type_int
;
2576 case SpvOpImageQuerySamples
:
2577 texop
= nir_texop_texture_samples
;
2578 dest_type
= nir_type_int
;
2581 case SpvOpFragmentFetchAMD
:
2582 texop
= nir_texop_fragment_fetch
;
2585 case SpvOpFragmentMaskFetchAMD
:
2586 texop
= nir_texop_fragment_mask_fetch
;
2587 dest_type
= nir_type_uint
;
2591 vtn_fail_with_opcode("Unhandled opcode", opcode
);
2594 nir_tex_src srcs
[10]; /* 10 should be enough */
2595 nir_tex_src
*p
= srcs
;
2597 p
->src
= nir_src_for_ssa(&image
->dest
.ssa
);
2598 p
->src_type
= nir_tex_src_texture_deref
;
2608 vtn_fail_if(sampler
== NULL
,
2609 "%s requires an image of type OpTypeSampledImage",
2610 spirv_op_to_string(opcode
));
2611 p
->src
= nir_src_for_ssa(&sampler
->dest
.ssa
);
2612 p
->src_type
= nir_tex_src_sampler_deref
;
2616 case nir_texop_txf_ms
:
2618 case nir_texop_query_levels
:
2619 case nir_texop_texture_samples
:
2620 case nir_texop_samples_identical
:
2621 case nir_texop_fragment_fetch
:
2622 case nir_texop_fragment_mask_fetch
:
2625 case nir_texop_txf_ms_fb
:
2626 vtn_fail("unexpected nir_texop_txf_ms_fb");
2628 case nir_texop_txf_ms_mcs
:
2629 vtn_fail("unexpected nir_texop_txf_ms_mcs");
2630 case nir_texop_tex_prefetch
:
2631 vtn_fail("unexpected nir_texop_tex_prefetch");
2636 struct nir_ssa_def
*coord
;
2637 unsigned coord_components
;
2639 case SpvOpImageSampleImplicitLod
:
2640 case SpvOpImageSampleExplicitLod
:
2641 case SpvOpImageSampleDrefImplicitLod
:
2642 case SpvOpImageSampleDrefExplicitLod
:
2643 case SpvOpImageSampleProjImplicitLod
:
2644 case SpvOpImageSampleProjExplicitLod
:
2645 case SpvOpImageSampleProjDrefImplicitLod
:
2646 case SpvOpImageSampleProjDrefExplicitLod
:
2647 case SpvOpImageFetch
:
2648 case SpvOpImageGather
:
2649 case SpvOpImageDrefGather
:
2650 case SpvOpImageQueryLod
:
2651 case SpvOpFragmentFetchAMD
:
2652 case SpvOpFragmentMaskFetchAMD
: {
2653 /* All these types have the coordinate as their first real argument */
2654 coord_components
= glsl_get_sampler_dim_coordinate_components(sampler_dim
);
2656 if (is_array
&& texop
!= nir_texop_lod
)
2659 struct vtn_ssa_value
*coord_val
= vtn_ssa_value(b
, w
[idx
++]);
2660 coord
= coord_val
->def
;
2661 p
->src
= nir_src_for_ssa(nir_channels(&b
->nb
, coord
,
2662 (1 << coord_components
) - 1));
2664 /* OpenCL allows integer sampling coordinates */
2665 if (glsl_type_is_integer(coord_val
->type
) &&
2666 opcode
== SpvOpImageSampleExplicitLod
) {
2667 vtn_fail_if(b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
,
2668 "Unless the Kernel capability is being used, the coordinate parameter "
2669 "OpImageSampleExplicitLod must be floating point.");
2671 p
->src
= nir_src_for_ssa(nir_i2f32(&b
->nb
, p
->src
.ssa
));
2674 p
->src_type
= nir_tex_src_coord
;
2681 coord_components
= 0;
2686 case SpvOpImageSampleProjImplicitLod
:
2687 case SpvOpImageSampleProjExplicitLod
:
2688 case SpvOpImageSampleProjDrefImplicitLod
:
2689 case SpvOpImageSampleProjDrefExplicitLod
:
2690 /* These have the projector as the last coordinate component */
2691 p
->src
= nir_src_for_ssa(nir_channel(&b
->nb
, coord
, coord_components
));
2692 p
->src_type
= nir_tex_src_projector
;
2700 bool is_shadow
= false;
2701 unsigned gather_component
= 0;
2703 case SpvOpImageSampleDrefImplicitLod
:
2704 case SpvOpImageSampleDrefExplicitLod
:
2705 case SpvOpImageSampleProjDrefImplicitLod
:
2706 case SpvOpImageSampleProjDrefExplicitLod
:
2707 case SpvOpImageDrefGather
:
2708 /* These all have an explicit depth value as their next source */
2710 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_comparator
);
2713 case SpvOpImageGather
:
2714 /* This has a component as its next source */
2715 gather_component
= vtn_constant_uint(b
, w
[idx
++]);
2722 /* For OpImageQuerySizeLod, we always have an LOD */
2723 if (opcode
== SpvOpImageQuerySizeLod
)
2724 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
2726 /* For OpFragmentFetchAMD, we always have a multisample index */
2727 if (opcode
== SpvOpFragmentFetchAMD
)
2728 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ms_index
);
2730 /* Now we need to handle some number of optional arguments */
2731 struct vtn_value
*gather_offsets
= NULL
;
2733 uint32_t operands
= w
[idx
];
2735 if (operands
& SpvImageOperandsBiasMask
) {
2736 vtn_assert(texop
== nir_texop_tex
||
2737 texop
== nir_texop_tg4
);
2738 if (texop
== nir_texop_tex
)
2739 texop
= nir_texop_txb
;
2740 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2741 SpvImageOperandsBiasMask
);
2742 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_bias
);
2745 if (operands
& SpvImageOperandsLodMask
) {
2746 vtn_assert(texop
== nir_texop_txl
|| texop
== nir_texop_txf
||
2747 texop
== nir_texop_txs
|| texop
== nir_texop_tg4
);
2748 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2749 SpvImageOperandsLodMask
);
2750 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_lod
);
2753 if (operands
& SpvImageOperandsGradMask
) {
2754 vtn_assert(texop
== nir_texop_txl
);
2755 texop
= nir_texop_txd
;
2756 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2757 SpvImageOperandsGradMask
);
2758 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_ddx
);
2759 (*p
++) = vtn_tex_src(b
, w
[arg
+ 1], nir_tex_src_ddy
);
2762 vtn_fail_if(util_bitcount(operands
& (SpvImageOperandsConstOffsetsMask
|
2763 SpvImageOperandsOffsetMask
|
2764 SpvImageOperandsConstOffsetMask
)) > 1,
2765 "At most one of the ConstOffset, Offset, and ConstOffsets "
2766 "image operands can be used on a given instruction.");
2768 if (operands
& SpvImageOperandsOffsetMask
) {
2769 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2770 SpvImageOperandsOffsetMask
);
2771 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_offset
);
2774 if (operands
& SpvImageOperandsConstOffsetMask
) {
2775 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2776 SpvImageOperandsConstOffsetMask
);
2777 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_offset
);
2780 if (operands
& SpvImageOperandsConstOffsetsMask
) {
2781 vtn_assert(texop
== nir_texop_tg4
);
2782 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2783 SpvImageOperandsConstOffsetsMask
);
2784 gather_offsets
= vtn_value(b
, w
[arg
], vtn_value_type_constant
);
2787 if (operands
& SpvImageOperandsSampleMask
) {
2788 vtn_assert(texop
== nir_texop_txf_ms
);
2789 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2790 SpvImageOperandsSampleMask
);
2791 texop
= nir_texop_txf_ms
;
2792 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_ms_index
);
2795 if (operands
& SpvImageOperandsMinLodMask
) {
2796 vtn_assert(texop
== nir_texop_tex
||
2797 texop
== nir_texop_txb
||
2798 texop
== nir_texop_txd
);
2799 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2800 SpvImageOperandsMinLodMask
);
2801 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_min_lod
);
2805 nir_tex_instr
*instr
= nir_tex_instr_create(b
->shader
, p
- srcs
);
2808 memcpy(instr
->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
2810 instr
->coord_components
= coord_components
;
2811 instr
->sampler_dim
= sampler_dim
;
2812 instr
->is_array
= is_array
;
2813 instr
->is_shadow
= is_shadow
;
2814 instr
->is_new_style_shadow
=
2815 is_shadow
&& glsl_get_components(ret_type
->type
) == 1;
2816 instr
->component
= gather_component
;
2818 /* The Vulkan spec says:
2820 * "If an instruction loads from or stores to a resource (including
2821 * atomics and image instructions) and the resource descriptor being
2822 * accessed is not dynamically uniform, then the operand corresponding
2823 * to that resource (e.g. the pointer or sampled image operand) must be
2824 * decorated with NonUniform."
2826 * It's very careful to specify that the exact operand must be decorated
2827 * NonUniform. The SPIR-V parser is not expected to chase through long
2828 * chains to find the NonUniform decoration. It's either right there or we
2829 * can assume it doesn't exist.
2831 enum gl_access_qualifier access
= 0;
2832 vtn_foreach_decoration(b
, sampled_val
, non_uniform_decoration_cb
, &access
);
2834 if (sampled_val
->propagated_non_uniform
)
2835 access
|= ACCESS_NON_UNIFORM
;
2837 if (image
&& (access
& ACCESS_NON_UNIFORM
))
2838 instr
->texture_non_uniform
= true;
2840 if (sampler
&& (access
& ACCESS_NON_UNIFORM
))
2841 instr
->sampler_non_uniform
= true;
2843 /* for non-query ops, get dest_type from SPIR-V return type */
2844 if (dest_type
== nir_type_invalid
) {
2845 /* the return type should match the image type, unless the image type is
2846 * VOID (CL image), in which case the return type dictates the sampler
2848 enum glsl_base_type sampler_base
=
2849 glsl_get_sampler_result_type(image
->type
);
2850 enum glsl_base_type ret_base
= glsl_get_base_type(ret_type
->type
);
2851 vtn_fail_if(sampler_base
!= ret_base
&& sampler_base
!= GLSL_TYPE_VOID
,
2852 "SPIR-V return type mismatches image type. This is only valid "
2853 "for untyped images (OpenCL).");
2855 case GLSL_TYPE_FLOAT
: dest_type
= nir_type_float
; break;
2856 case GLSL_TYPE_INT
: dest_type
= nir_type_int
; break;
2857 case GLSL_TYPE_UINT
: dest_type
= nir_type_uint
; break;
2858 case GLSL_TYPE_BOOL
: dest_type
= nir_type_bool
; break;
2860 vtn_fail("Invalid base type for sampler result");
2864 instr
->dest_type
= dest_type
;
2866 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
2867 nir_tex_instr_dest_size(instr
), 32, NULL
);
2869 vtn_assert(glsl_get_vector_elements(ret_type
->type
) ==
2870 nir_tex_instr_dest_size(instr
));
2872 if (gather_offsets
) {
2873 vtn_fail_if(gather_offsets
->type
->base_type
!= vtn_base_type_array
||
2874 gather_offsets
->type
->length
!= 4,
2875 "ConstOffsets must be an array of size four of vectors "
2876 "of two integer components");
2878 struct vtn_type
*vec_type
= gather_offsets
->type
->array_element
;
2879 vtn_fail_if(vec_type
->base_type
!= vtn_base_type_vector
||
2880 vec_type
->length
!= 2 ||
2881 !glsl_type_is_integer(vec_type
->type
),
2882 "ConstOffsets must be an array of size four of vectors "
2883 "of two integer components");
2885 unsigned bit_size
= glsl_get_bit_size(vec_type
->type
);
2886 for (uint32_t i
= 0; i
< 4; i
++) {
2887 const nir_const_value
*cvec
=
2888 gather_offsets
->constant
->elements
[i
]->values
;
2889 for (uint32_t j
= 0; j
< 2; j
++) {
2891 case 8: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i8
; break;
2892 case 16: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i16
; break;
2893 case 32: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i32
; break;
2894 case 64: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i64
; break;
2896 vtn_fail("Unsupported bit size: %u", bit_size
);
2902 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
2904 vtn_push_nir_ssa(b
, w
[2], &instr
->dest
.ssa
);
2908 fill_common_atomic_sources(struct vtn_builder
*b
, SpvOp opcode
,
2909 const uint32_t *w
, nir_src
*src
)
2911 const struct glsl_type
*type
= vtn_get_type(b
, w
[1])->type
;
2912 unsigned bit_size
= glsl_get_bit_size(type
);
2915 case SpvOpAtomicIIncrement
:
2916 src
[0] = nir_src_for_ssa(nir_imm_intN_t(&b
->nb
, 1, bit_size
));
2919 case SpvOpAtomicIDecrement
:
2920 src
[0] = nir_src_for_ssa(nir_imm_intN_t(&b
->nb
, -1, bit_size
));
2923 case SpvOpAtomicISub
:
2925 nir_src_for_ssa(nir_ineg(&b
->nb
, vtn_get_nir_ssa(b
, w
[6])));
2928 case SpvOpAtomicCompareExchange
:
2929 case SpvOpAtomicCompareExchangeWeak
:
2930 src
[0] = nir_src_for_ssa(vtn_get_nir_ssa(b
, w
[8]));
2931 src
[1] = nir_src_for_ssa(vtn_get_nir_ssa(b
, w
[7]));
2934 case SpvOpAtomicExchange
:
2935 case SpvOpAtomicIAdd
:
2936 case SpvOpAtomicSMin
:
2937 case SpvOpAtomicUMin
:
2938 case SpvOpAtomicSMax
:
2939 case SpvOpAtomicUMax
:
2940 case SpvOpAtomicAnd
:
2942 case SpvOpAtomicXor
:
2943 case SpvOpAtomicFAddEXT
:
2944 src
[0] = nir_src_for_ssa(vtn_get_nir_ssa(b
, w
[6]));
2948 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
2952 static nir_ssa_def
*
2953 get_image_coord(struct vtn_builder
*b
, uint32_t value
)
2955 nir_ssa_def
*coord
= vtn_get_nir_ssa(b
, value
);
2957 /* The image_load_store intrinsics assume a 4-dim coordinate */
2958 unsigned swizzle
[4];
2959 for (unsigned i
= 0; i
< 4; i
++)
2960 swizzle
[i
] = MIN2(i
, coord
->num_components
- 1);
2962 return nir_swizzle(&b
->nb
, coord
, swizzle
, 4);
2965 static nir_ssa_def
*
2966 expand_to_vec4(nir_builder
*b
, nir_ssa_def
*value
)
2968 if (value
->num_components
== 4)
2972 for (unsigned i
= 0; i
< 4; i
++)
2973 swiz
[i
] = i
< value
->num_components
? i
: 0;
2974 return nir_swizzle(b
, value
, swiz
, 4);
2978 vtn_handle_image(struct vtn_builder
*b
, SpvOp opcode
,
2979 const uint32_t *w
, unsigned count
)
2981 /* Just get this one out of the way */
2982 if (opcode
== SpvOpImageTexelPointer
) {
2983 struct vtn_value
*val
=
2984 vtn_push_value(b
, w
[2], vtn_value_type_image_pointer
);
2985 val
->image
= ralloc(b
, struct vtn_image_pointer
);
2987 val
->image
->image
= vtn_nir_deref(b
, w
[3]);
2988 val
->image
->coord
= get_image_coord(b
, w
[4]);
2989 val
->image
->sample
= vtn_get_nir_ssa(b
, w
[5]);
2990 val
->image
->lod
= nir_imm_int(&b
->nb
, 0);
2994 struct vtn_image_pointer image
;
2995 SpvScope scope
= SpvScopeInvocation
;
2996 SpvMemorySemanticsMask semantics
= 0;
2998 enum gl_access_qualifier access
= 0;
3000 struct vtn_value
*res_val
;
3002 case SpvOpAtomicExchange
:
3003 case SpvOpAtomicCompareExchange
:
3004 case SpvOpAtomicCompareExchangeWeak
:
3005 case SpvOpAtomicIIncrement
:
3006 case SpvOpAtomicIDecrement
:
3007 case SpvOpAtomicIAdd
:
3008 case SpvOpAtomicISub
:
3009 case SpvOpAtomicLoad
:
3010 case SpvOpAtomicSMin
:
3011 case SpvOpAtomicUMin
:
3012 case SpvOpAtomicSMax
:
3013 case SpvOpAtomicUMax
:
3014 case SpvOpAtomicAnd
:
3016 case SpvOpAtomicXor
:
3017 case SpvOpAtomicFAddEXT
:
3018 res_val
= vtn_value(b
, w
[3], vtn_value_type_image_pointer
);
3019 image
= *res_val
->image
;
3020 scope
= vtn_constant_uint(b
, w
[4]);
3021 semantics
= vtn_constant_uint(b
, w
[5]);
3022 access
|= ACCESS_COHERENT
;
3025 case SpvOpAtomicStore
:
3026 res_val
= vtn_value(b
, w
[1], vtn_value_type_image_pointer
);
3027 image
= *res_val
->image
;
3028 scope
= vtn_constant_uint(b
, w
[2]);
3029 semantics
= vtn_constant_uint(b
, w
[3]);
3030 access
|= ACCESS_COHERENT
;
3033 case SpvOpImageQuerySizeLod
:
3034 res_val
= vtn_untyped_value(b
, w
[3]);
3035 image
.image
= vtn_get_image(b
, w
[3]);
3037 image
.sample
= NULL
;
3038 image
.lod
= vtn_ssa_value(b
, w
[4])->def
;
3041 case SpvOpImageQuerySize
:
3042 res_val
= vtn_untyped_value(b
, w
[3]);
3043 image
.image
= vtn_get_image(b
, w
[3]);
3045 image
.sample
= NULL
;
3049 case SpvOpImageQueryFormat
:
3050 case SpvOpImageQueryOrder
:
3051 res_val
= vtn_untyped_value(b
, w
[3]);
3052 image
.image
= vtn_get_image(b
, w
[3]);
3054 image
.sample
= NULL
;
3058 case SpvOpImageRead
: {
3059 res_val
= vtn_untyped_value(b
, w
[3]);
3060 image
.image
= vtn_get_image(b
, w
[3]);
3061 image
.coord
= get_image_coord(b
, w
[4]);
3063 const SpvImageOperandsMask operands
=
3064 count
> 5 ? w
[5] : SpvImageOperandsMaskNone
;
3066 if (operands
& SpvImageOperandsSampleMask
) {
3067 uint32_t arg
= image_operand_arg(b
, w
, count
, 5,
3068 SpvImageOperandsSampleMask
);
3069 image
.sample
= vtn_get_nir_ssa(b
, w
[arg
]);
3071 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
3074 if (operands
& SpvImageOperandsMakeTexelVisibleMask
) {
3075 vtn_fail_if((operands
& SpvImageOperandsNonPrivateTexelMask
) == 0,
3076 "MakeTexelVisible requires NonPrivateTexel to also be set.");
3077 uint32_t arg
= image_operand_arg(b
, w
, count
, 5,
3078 SpvImageOperandsMakeTexelVisibleMask
);
3079 semantics
= SpvMemorySemanticsMakeVisibleMask
;
3080 scope
= vtn_constant_uint(b
, w
[arg
]);
3083 if (operands
& SpvImageOperandsLodMask
) {
3084 uint32_t arg
= image_operand_arg(b
, w
, count
, 5,
3085 SpvImageOperandsLodMask
);
3086 image
.lod
= vtn_get_nir_ssa(b
, w
[arg
]);
3088 image
.lod
= nir_imm_int(&b
->nb
, 0);
3091 if (operands
& SpvImageOperandsVolatileTexelMask
)
3092 access
|= ACCESS_VOLATILE
;
3097 case SpvOpImageWrite
: {
3098 res_val
= vtn_untyped_value(b
, w
[1]);
3099 image
.image
= vtn_get_image(b
, w
[1]);
3100 image
.coord
= get_image_coord(b
, w
[2]);
3104 const SpvImageOperandsMask operands
=
3105 count
> 4 ? w
[4] : SpvImageOperandsMaskNone
;
3107 if (operands
& SpvImageOperandsSampleMask
) {
3108 uint32_t arg
= image_operand_arg(b
, w
, count
, 4,
3109 SpvImageOperandsSampleMask
);
3110 image
.sample
= vtn_get_nir_ssa(b
, w
[arg
]);
3112 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
3115 if (operands
& SpvImageOperandsMakeTexelAvailableMask
) {
3116 vtn_fail_if((operands
& SpvImageOperandsNonPrivateTexelMask
) == 0,
3117 "MakeTexelAvailable requires NonPrivateTexel to also be set.");
3118 uint32_t arg
= image_operand_arg(b
, w
, count
, 4,
3119 SpvImageOperandsMakeTexelAvailableMask
);
3120 semantics
= SpvMemorySemanticsMakeAvailableMask
;
3121 scope
= vtn_constant_uint(b
, w
[arg
]);
3124 if (operands
& SpvImageOperandsLodMask
) {
3125 uint32_t arg
= image_operand_arg(b
, w
, count
, 4,
3126 SpvImageOperandsLodMask
);
3127 image
.lod
= vtn_get_nir_ssa(b
, w
[arg
]);
3129 image
.lod
= nir_imm_int(&b
->nb
, 0);
3132 if (operands
& SpvImageOperandsVolatileTexelMask
)
3133 access
|= ACCESS_VOLATILE
;
3139 vtn_fail_with_opcode("Invalid image opcode", opcode
);
3142 if (semantics
& SpvMemorySemanticsVolatileMask
)
3143 access
|= ACCESS_VOLATILE
;
3145 nir_intrinsic_op op
;
3147 #define OP(S, N) case SpvOp##S: op = nir_intrinsic_image_deref_##N; break;
3148 OP(ImageQuerySize
, size
)
3149 OP(ImageQuerySizeLod
, size
)
3151 OP(ImageWrite
, store
)
3152 OP(AtomicLoad
, load
)
3153 OP(AtomicStore
, store
)
3154 OP(AtomicExchange
, atomic_exchange
)
3155 OP(AtomicCompareExchange
, atomic_comp_swap
)
3156 OP(AtomicCompareExchangeWeak
, atomic_comp_swap
)
3157 OP(AtomicIIncrement
, atomic_add
)
3158 OP(AtomicIDecrement
, atomic_add
)
3159 OP(AtomicIAdd
, atomic_add
)
3160 OP(AtomicISub
, atomic_add
)
3161 OP(AtomicSMin
, atomic_imin
)
3162 OP(AtomicUMin
, atomic_umin
)
3163 OP(AtomicSMax
, atomic_imax
)
3164 OP(AtomicUMax
, atomic_umax
)
3165 OP(AtomicAnd
, atomic_and
)
3166 OP(AtomicOr
, atomic_or
)
3167 OP(AtomicXor
, atomic_xor
)
3168 OP(AtomicFAddEXT
, atomic_fadd
)
3169 OP(ImageQueryFormat
, format
)
3170 OP(ImageQueryOrder
, order
)
3173 vtn_fail_with_opcode("Invalid image opcode", opcode
);
3176 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
3178 intrin
->src
[0] = nir_src_for_ssa(&image
.image
->dest
.ssa
);
3181 case SpvOpImageQuerySize
:
3182 case SpvOpImageQuerySizeLod
:
3183 case SpvOpImageQueryFormat
:
3184 case SpvOpImageQueryOrder
:
3187 /* The image coordinate is always 4 components but we may not have that
3188 * many. Swizzle to compensate.
3190 intrin
->src
[1] = nir_src_for_ssa(expand_to_vec4(&b
->nb
, image
.coord
));
3191 intrin
->src
[2] = nir_src_for_ssa(image
.sample
);
3195 /* The Vulkan spec says:
3197 * "If an instruction loads from or stores to a resource (including
3198 * atomics and image instructions) and the resource descriptor being
3199 * accessed is not dynamically uniform, then the operand corresponding
3200 * to that resource (e.g. the pointer or sampled image operand) must be
3201 * decorated with NonUniform."
3203 * It's very careful to specify that the exact operand must be decorated
3204 * NonUniform. The SPIR-V parser is not expected to chase through long
3205 * chains to find the NonUniform decoration. It's either right there or we
3206 * can assume it doesn't exist.
3208 vtn_foreach_decoration(b
, res_val
, non_uniform_decoration_cb
, &access
);
3209 nir_intrinsic_set_access(intrin
, access
);
3212 case SpvOpImageQueryFormat
:
3213 case SpvOpImageQueryOrder
:
3214 /* No additional sources */
3216 case SpvOpImageQuerySize
:
3217 intrin
->src
[1] = nir_src_for_ssa(nir_imm_int(&b
->nb
, 0));
3219 case SpvOpImageQuerySizeLod
:
3220 intrin
->src
[1] = nir_src_for_ssa(image
.lod
);
3222 case SpvOpAtomicLoad
:
3223 case SpvOpImageRead
:
3224 /* Only OpImageRead can support a lod parameter if
3225 * SPV_AMD_shader_image_load_store_lod is used but the current NIR
3226 * intrinsics definition for atomics requires us to set it for
3229 intrin
->src
[3] = nir_src_for_ssa(image
.lod
);
3231 case SpvOpAtomicStore
:
3232 case SpvOpImageWrite
: {
3233 const uint32_t value_id
= opcode
== SpvOpAtomicStore
? w
[4] : w
[3];
3234 struct vtn_ssa_value
*value
= vtn_ssa_value(b
, value_id
);
3235 /* nir_intrinsic_image_deref_store always takes a vec4 value */
3236 assert(op
== nir_intrinsic_image_deref_store
);
3237 intrin
->num_components
= 4;
3238 intrin
->src
[3] = nir_src_for_ssa(expand_to_vec4(&b
->nb
, value
->def
));
3239 /* Only OpImageWrite can support a lod parameter if
3240 * SPV_AMD_shader_image_load_store_lod is used but the current NIR
3241 * intrinsics definition for atomics requires us to set it for
3244 intrin
->src
[4] = nir_src_for_ssa(image
.lod
);
3246 if (opcode
== SpvOpImageWrite
)
3247 nir_intrinsic_set_type(intrin
, nir_get_nir_type_for_glsl_type(value
->type
));
3251 case SpvOpAtomicCompareExchange
:
3252 case SpvOpAtomicCompareExchangeWeak
:
3253 case SpvOpAtomicIIncrement
:
3254 case SpvOpAtomicIDecrement
:
3255 case SpvOpAtomicExchange
:
3256 case SpvOpAtomicIAdd
:
3257 case SpvOpAtomicISub
:
3258 case SpvOpAtomicSMin
:
3259 case SpvOpAtomicUMin
:
3260 case SpvOpAtomicSMax
:
3261 case SpvOpAtomicUMax
:
3262 case SpvOpAtomicAnd
:
3264 case SpvOpAtomicXor
:
3265 case SpvOpAtomicFAddEXT
:
3266 fill_common_atomic_sources(b
, opcode
, w
, &intrin
->src
[3]);
3270 vtn_fail_with_opcode("Invalid image opcode", opcode
);
3273 /* Image operations implicitly have the Image storage memory semantics. */
3274 semantics
|= SpvMemorySemanticsImageMemoryMask
;
3276 SpvMemorySemanticsMask before_semantics
;
3277 SpvMemorySemanticsMask after_semantics
;
3278 vtn_split_barrier_semantics(b
, semantics
, &before_semantics
, &after_semantics
);
3280 if (before_semantics
)
3281 vtn_emit_memory_barrier(b
, scope
, before_semantics
);
3283 if (opcode
!= SpvOpImageWrite
&& opcode
!= SpvOpAtomicStore
) {
3284 struct vtn_type
*type
= vtn_get_type(b
, w
[1]);
3286 unsigned dest_components
= glsl_get_vector_elements(type
->type
);
3287 if (nir_intrinsic_infos
[op
].dest_components
== 0)
3288 intrin
->num_components
= dest_components
;
3290 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
,
3291 nir_intrinsic_dest_components(intrin
), 32, NULL
);
3293 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3295 nir_ssa_def
*result
= &intrin
->dest
.ssa
;
3296 if (nir_intrinsic_dest_components(intrin
) != dest_components
)
3297 result
= nir_channels(&b
->nb
, result
, (1 << dest_components
) - 1);
3299 vtn_push_nir_ssa(b
, w
[2], result
);
3301 if (opcode
== SpvOpImageRead
)
3302 nir_intrinsic_set_type(intrin
, nir_get_nir_type_for_glsl_type(type
->type
));
3304 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3307 if (after_semantics
)
3308 vtn_emit_memory_barrier(b
, scope
, after_semantics
);
3311 static nir_intrinsic_op
3312 get_ssbo_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
3315 case SpvOpAtomicLoad
: return nir_intrinsic_load_ssbo
;
3316 case SpvOpAtomicStore
: return nir_intrinsic_store_ssbo
;
3317 #define OP(S, N) case SpvOp##S: return nir_intrinsic_ssbo_##N;
3318 OP(AtomicExchange
, atomic_exchange
)
3319 OP(AtomicCompareExchange
, atomic_comp_swap
)
3320 OP(AtomicCompareExchangeWeak
, atomic_comp_swap
)
3321 OP(AtomicIIncrement
, atomic_add
)
3322 OP(AtomicIDecrement
, atomic_add
)
3323 OP(AtomicIAdd
, atomic_add
)
3324 OP(AtomicISub
, atomic_add
)
3325 OP(AtomicSMin
, atomic_imin
)
3326 OP(AtomicUMin
, atomic_umin
)
3327 OP(AtomicSMax
, atomic_imax
)
3328 OP(AtomicUMax
, atomic_umax
)
3329 OP(AtomicAnd
, atomic_and
)
3330 OP(AtomicOr
, atomic_or
)
3331 OP(AtomicXor
, atomic_xor
)
3332 OP(AtomicFAddEXT
, atomic_fadd
)
3335 vtn_fail_with_opcode("Invalid SSBO atomic", opcode
);
3339 static nir_intrinsic_op
3340 get_uniform_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
3343 #define OP(S, N) case SpvOp##S: return nir_intrinsic_atomic_counter_ ##N;
3344 OP(AtomicLoad
, read_deref
)
3345 OP(AtomicExchange
, exchange
)
3346 OP(AtomicCompareExchange
, comp_swap
)
3347 OP(AtomicCompareExchangeWeak
, comp_swap
)
3348 OP(AtomicIIncrement
, inc_deref
)
3349 OP(AtomicIDecrement
, post_dec_deref
)
3350 OP(AtomicIAdd
, add_deref
)
3351 OP(AtomicISub
, add_deref
)
3352 OP(AtomicUMin
, min_deref
)
3353 OP(AtomicUMax
, max_deref
)
3354 OP(AtomicAnd
, and_deref
)
3355 OP(AtomicOr
, or_deref
)
3356 OP(AtomicXor
, xor_deref
)
3359 /* We left the following out: AtomicStore, AtomicSMin and
3360 * AtomicSmax. Right now there are not nir intrinsics for them. At this
3361 * moment Atomic Counter support is needed for ARB_spirv support, so is
3362 * only need to support GLSL Atomic Counters that are uints and don't
3363 * allow direct storage.
3365 vtn_fail("Invalid uniform atomic");
3369 static nir_intrinsic_op
3370 get_deref_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
3373 case SpvOpAtomicLoad
: return nir_intrinsic_load_deref
;
3374 case SpvOpAtomicStore
: return nir_intrinsic_store_deref
;
3375 #define OP(S, N) case SpvOp##S: return nir_intrinsic_deref_##N;
3376 OP(AtomicExchange
, atomic_exchange
)
3377 OP(AtomicCompareExchange
, atomic_comp_swap
)
3378 OP(AtomicCompareExchangeWeak
, atomic_comp_swap
)
3379 OP(AtomicIIncrement
, atomic_add
)
3380 OP(AtomicIDecrement
, atomic_add
)
3381 OP(AtomicIAdd
, atomic_add
)
3382 OP(AtomicISub
, atomic_add
)
3383 OP(AtomicSMin
, atomic_imin
)
3384 OP(AtomicUMin
, atomic_umin
)
3385 OP(AtomicSMax
, atomic_imax
)
3386 OP(AtomicUMax
, atomic_umax
)
3387 OP(AtomicAnd
, atomic_and
)
3388 OP(AtomicOr
, atomic_or
)
3389 OP(AtomicXor
, atomic_xor
)
3390 OP(AtomicFAddEXT
, atomic_fadd
)
3393 vtn_fail_with_opcode("Invalid shared atomic", opcode
);
3398 * Handles shared atomics, ssbo atomics and atomic counters.
3401 vtn_handle_atomics(struct vtn_builder
*b
, SpvOp opcode
,
3402 const uint32_t *w
, UNUSED
unsigned count
)
3404 struct vtn_pointer
*ptr
;
3405 nir_intrinsic_instr
*atomic
;
3407 SpvScope scope
= SpvScopeInvocation
;
3408 SpvMemorySemanticsMask semantics
= 0;
3409 enum gl_access_qualifier access
= 0;
3412 case SpvOpAtomicLoad
:
3413 case SpvOpAtomicExchange
:
3414 case SpvOpAtomicCompareExchange
:
3415 case SpvOpAtomicCompareExchangeWeak
:
3416 case SpvOpAtomicIIncrement
:
3417 case SpvOpAtomicIDecrement
:
3418 case SpvOpAtomicIAdd
:
3419 case SpvOpAtomicISub
:
3420 case SpvOpAtomicSMin
:
3421 case SpvOpAtomicUMin
:
3422 case SpvOpAtomicSMax
:
3423 case SpvOpAtomicUMax
:
3424 case SpvOpAtomicAnd
:
3426 case SpvOpAtomicXor
:
3427 case SpvOpAtomicFAddEXT
:
3428 ptr
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
3429 scope
= vtn_constant_uint(b
, w
[4]);
3430 semantics
= vtn_constant_uint(b
, w
[5]);
3433 case SpvOpAtomicStore
:
3434 ptr
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
3435 scope
= vtn_constant_uint(b
, w
[2]);
3436 semantics
= vtn_constant_uint(b
, w
[3]);
3440 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
3443 if (semantics
& SpvMemorySemanticsVolatileMask
)
3444 access
|= ACCESS_VOLATILE
;
3446 /* uniform as "atomic counter uniform" */
3447 if (ptr
->mode
== vtn_variable_mode_atomic_counter
) {
3448 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
3449 nir_intrinsic_op op
= get_uniform_nir_atomic_op(b
, opcode
);
3450 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
3451 atomic
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
3453 /* SSBO needs to initialize index/offset. In this case we don't need to,
3454 * as that info is already stored on the ptr->var->var nir_variable (see
3455 * vtn_create_variable)
3459 case SpvOpAtomicLoad
:
3460 case SpvOpAtomicExchange
:
3461 case SpvOpAtomicCompareExchange
:
3462 case SpvOpAtomicCompareExchangeWeak
:
3463 case SpvOpAtomicIIncrement
:
3464 case SpvOpAtomicIDecrement
:
3465 case SpvOpAtomicIAdd
:
3466 case SpvOpAtomicISub
:
3467 case SpvOpAtomicSMin
:
3468 case SpvOpAtomicUMin
:
3469 case SpvOpAtomicSMax
:
3470 case SpvOpAtomicUMax
:
3471 case SpvOpAtomicAnd
:
3473 case SpvOpAtomicXor
:
3474 /* Nothing: we don't need to call fill_common_atomic_sources here, as
3475 * atomic counter uniforms doesn't have sources
3480 unreachable("Invalid SPIR-V atomic");
3483 } else if (vtn_pointer_uses_ssa_offset(b
, ptr
)) {
3484 nir_ssa_def
*offset
, *index
;
3485 offset
= vtn_pointer_to_offset(b
, ptr
, &index
);
3487 assert(ptr
->mode
== vtn_variable_mode_ssbo
);
3489 nir_intrinsic_op op
= get_ssbo_nir_atomic_op(b
, opcode
);
3490 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
3492 nir_intrinsic_set_access(atomic
, access
| ACCESS_COHERENT
);
3496 case SpvOpAtomicLoad
:
3497 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
3498 nir_intrinsic_set_align(atomic
, 4, 0);
3499 if (ptr
->mode
== vtn_variable_mode_ssbo
)
3500 atomic
->src
[src
++] = nir_src_for_ssa(index
);
3501 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
3504 case SpvOpAtomicStore
:
3505 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
3506 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
3507 nir_intrinsic_set_align(atomic
, 4, 0);
3508 atomic
->src
[src
++] = nir_src_for_ssa(vtn_get_nir_ssa(b
, w
[4]));
3509 if (ptr
->mode
== vtn_variable_mode_ssbo
)
3510 atomic
->src
[src
++] = nir_src_for_ssa(index
);
3511 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
3514 case SpvOpAtomicExchange
:
3515 case SpvOpAtomicCompareExchange
:
3516 case SpvOpAtomicCompareExchangeWeak
:
3517 case SpvOpAtomicIIncrement
:
3518 case SpvOpAtomicIDecrement
:
3519 case SpvOpAtomicIAdd
:
3520 case SpvOpAtomicISub
:
3521 case SpvOpAtomicSMin
:
3522 case SpvOpAtomicUMin
:
3523 case SpvOpAtomicSMax
:
3524 case SpvOpAtomicUMax
:
3525 case SpvOpAtomicAnd
:
3527 case SpvOpAtomicXor
:
3528 case SpvOpAtomicFAddEXT
:
3529 if (ptr
->mode
== vtn_variable_mode_ssbo
)
3530 atomic
->src
[src
++] = nir_src_for_ssa(index
);
3531 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
3532 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[src
]);
3536 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
3539 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
3540 const struct glsl_type
*deref_type
= deref
->type
;
3541 nir_intrinsic_op op
= get_deref_nir_atomic_op(b
, opcode
);
3542 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
3543 atomic
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
3545 if (ptr
->mode
!= vtn_variable_mode_workgroup
)
3546 access
|= ACCESS_COHERENT
;
3548 nir_intrinsic_set_access(atomic
, access
);
3551 case SpvOpAtomicLoad
:
3552 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
3555 case SpvOpAtomicStore
:
3556 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
3557 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
3558 atomic
->src
[1] = nir_src_for_ssa(vtn_get_nir_ssa(b
, w
[4]));
3561 case SpvOpAtomicExchange
:
3562 case SpvOpAtomicCompareExchange
:
3563 case SpvOpAtomicCompareExchangeWeak
:
3564 case SpvOpAtomicIIncrement
:
3565 case SpvOpAtomicIDecrement
:
3566 case SpvOpAtomicIAdd
:
3567 case SpvOpAtomicISub
:
3568 case SpvOpAtomicSMin
:
3569 case SpvOpAtomicUMin
:
3570 case SpvOpAtomicSMax
:
3571 case SpvOpAtomicUMax
:
3572 case SpvOpAtomicAnd
:
3574 case SpvOpAtomicXor
:
3575 case SpvOpAtomicFAddEXT
:
3576 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[1]);
3580 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
3584 /* Atomic ordering operations will implicitly apply to the atomic operation
3585 * storage class, so include that too.
3587 semantics
|= vtn_mode_to_memory_semantics(ptr
->mode
);
3589 SpvMemorySemanticsMask before_semantics
;
3590 SpvMemorySemanticsMask after_semantics
;
3591 vtn_split_barrier_semantics(b
, semantics
, &before_semantics
, &after_semantics
);
3593 if (before_semantics
)
3594 vtn_emit_memory_barrier(b
, scope
, before_semantics
);
3596 if (opcode
!= SpvOpAtomicStore
) {
3597 struct vtn_type
*type
= vtn_get_type(b
, w
[1]);
3599 nir_ssa_dest_init(&atomic
->instr
, &atomic
->dest
,
3600 glsl_get_vector_elements(type
->type
),
3601 glsl_get_bit_size(type
->type
), NULL
);
3603 vtn_push_nir_ssa(b
, w
[2], &atomic
->dest
.ssa
);
3606 nir_builder_instr_insert(&b
->nb
, &atomic
->instr
);
3608 if (after_semantics
)
3609 vtn_emit_memory_barrier(b
, scope
, after_semantics
);
3612 static nir_alu_instr
*
3613 create_vec(struct vtn_builder
*b
, unsigned num_components
, unsigned bit_size
)
3615 nir_op op
= nir_op_vec(num_components
);
3616 nir_alu_instr
*vec
= nir_alu_instr_create(b
->shader
, op
);
3617 nir_ssa_dest_init(&vec
->instr
, &vec
->dest
.dest
, num_components
,
3619 vec
->dest
.write_mask
= (1 << num_components
) - 1;
3624 struct vtn_ssa_value
*
3625 vtn_ssa_transpose(struct vtn_builder
*b
, struct vtn_ssa_value
*src
)
3627 if (src
->transposed
)
3628 return src
->transposed
;
3630 struct vtn_ssa_value
*dest
=
3631 vtn_create_ssa_value(b
, glsl_transposed_type(src
->type
));
3633 for (unsigned i
= 0; i
< glsl_get_matrix_columns(dest
->type
); i
++) {
3634 nir_alu_instr
*vec
= create_vec(b
, glsl_get_matrix_columns(src
->type
),
3635 glsl_get_bit_size(src
->type
));
3636 if (glsl_type_is_vector_or_scalar(src
->type
)) {
3637 vec
->src
[0].src
= nir_src_for_ssa(src
->def
);
3638 vec
->src
[0].swizzle
[0] = i
;
3640 for (unsigned j
= 0; j
< glsl_get_matrix_columns(src
->type
); j
++) {
3641 vec
->src
[j
].src
= nir_src_for_ssa(src
->elems
[j
]->def
);
3642 vec
->src
[j
].swizzle
[0] = i
;
3645 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3646 dest
->elems
[i
]->def
= &vec
->dest
.dest
.ssa
;
3649 dest
->transposed
= src
;
3654 static nir_ssa_def
*
3655 vtn_vector_shuffle(struct vtn_builder
*b
, unsigned num_components
,
3656 nir_ssa_def
*src0
, nir_ssa_def
*src1
,
3657 const uint32_t *indices
)
3659 nir_alu_instr
*vec
= create_vec(b
, num_components
, src0
->bit_size
);
3661 for (unsigned i
= 0; i
< num_components
; i
++) {
3662 uint32_t index
= indices
[i
];
3663 if (index
== 0xffffffff) {
3665 nir_src_for_ssa(nir_ssa_undef(&b
->nb
, 1, src0
->bit_size
));
3666 } else if (index
< src0
->num_components
) {
3667 vec
->src
[i
].src
= nir_src_for_ssa(src0
);
3668 vec
->src
[i
].swizzle
[0] = index
;
3670 vec
->src
[i
].src
= nir_src_for_ssa(src1
);
3671 vec
->src
[i
].swizzle
[0] = index
- src0
->num_components
;
3675 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3677 return &vec
->dest
.dest
.ssa
;
3681 * Concatentates a number of vectors/scalars together to produce a vector
3683 static nir_ssa_def
*
3684 vtn_vector_construct(struct vtn_builder
*b
, unsigned num_components
,
3685 unsigned num_srcs
, nir_ssa_def
**srcs
)
3687 nir_alu_instr
*vec
= create_vec(b
, num_components
, srcs
[0]->bit_size
);
3689 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
3691 * "When constructing a vector, there must be at least two Constituent
3694 vtn_assert(num_srcs
>= 2);
3696 unsigned dest_idx
= 0;
3697 for (unsigned i
= 0; i
< num_srcs
; i
++) {
3698 nir_ssa_def
*src
= srcs
[i
];
3699 vtn_assert(dest_idx
+ src
->num_components
<= num_components
);
3700 for (unsigned j
= 0; j
< src
->num_components
; j
++) {
3701 vec
->src
[dest_idx
].src
= nir_src_for_ssa(src
);
3702 vec
->src
[dest_idx
].swizzle
[0] = j
;
3707 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
3709 * "When constructing a vector, the total number of components in all
3710 * the operands must equal the number of components in Result Type."
3712 vtn_assert(dest_idx
== num_components
);
3714 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3716 return &vec
->dest
.dest
.ssa
;
3719 static struct vtn_ssa_value
*
3720 vtn_composite_copy(void *mem_ctx
, struct vtn_ssa_value
*src
)
3722 struct vtn_ssa_value
*dest
= rzalloc(mem_ctx
, struct vtn_ssa_value
);
3723 dest
->type
= src
->type
;
3725 if (glsl_type_is_vector_or_scalar(src
->type
)) {
3726 dest
->def
= src
->def
;
3728 unsigned elems
= glsl_get_length(src
->type
);
3730 dest
->elems
= ralloc_array(mem_ctx
, struct vtn_ssa_value
*, elems
);
3731 for (unsigned i
= 0; i
< elems
; i
++)
3732 dest
->elems
[i
] = vtn_composite_copy(mem_ctx
, src
->elems
[i
]);
3738 static struct vtn_ssa_value
*
3739 vtn_composite_insert(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
3740 struct vtn_ssa_value
*insert
, const uint32_t *indices
,
3741 unsigned num_indices
)
3743 struct vtn_ssa_value
*dest
= vtn_composite_copy(b
, src
);
3745 struct vtn_ssa_value
*cur
= dest
;
3747 for (i
= 0; i
< num_indices
- 1; i
++) {
3748 /* If we got a vector here, that means the next index will be trying to
3749 * dereference a scalar.
3751 vtn_fail_if(glsl_type_is_vector_or_scalar(cur
->type
),
3752 "OpCompositeInsert has too many indices.");
3753 vtn_fail_if(indices
[i
] >= glsl_get_length(cur
->type
),
3754 "All indices in an OpCompositeInsert must be in-bounds");
3755 cur
= cur
->elems
[indices
[i
]];
3758 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
3759 vtn_fail_if(indices
[i
] >= glsl_get_vector_elements(cur
->type
),
3760 "All indices in an OpCompositeInsert must be in-bounds");
3762 /* According to the SPIR-V spec, OpCompositeInsert may work down to
3763 * the component granularity. In that case, the last index will be
3764 * the index to insert the scalar into the vector.
3767 cur
->def
= nir_vector_insert_imm(&b
->nb
, cur
->def
, insert
->def
, indices
[i
]);
3769 vtn_fail_if(indices
[i
] >= glsl_get_length(cur
->type
),
3770 "All indices in an OpCompositeInsert must be in-bounds");
3771 cur
->elems
[indices
[i
]] = insert
;
3777 static struct vtn_ssa_value
*
3778 vtn_composite_extract(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
3779 const uint32_t *indices
, unsigned num_indices
)
3781 struct vtn_ssa_value
*cur
= src
;
3782 for (unsigned i
= 0; i
< num_indices
; i
++) {
3783 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
3784 vtn_assert(i
== num_indices
- 1);
3785 vtn_fail_if(indices
[i
] >= glsl_get_vector_elements(cur
->type
),
3786 "All indices in an OpCompositeExtract must be in-bounds");
3788 /* According to the SPIR-V spec, OpCompositeExtract may work down to
3789 * the component granularity. The last index will be the index of the
3790 * vector to extract.
3793 const struct glsl_type
*scalar_type
=
3794 glsl_scalar_type(glsl_get_base_type(cur
->type
));
3795 struct vtn_ssa_value
*ret
= vtn_create_ssa_value(b
, scalar_type
);
3796 ret
->def
= nir_channel(&b
->nb
, cur
->def
, indices
[i
]);
3799 vtn_fail_if(indices
[i
] >= glsl_get_length(cur
->type
),
3800 "All indices in an OpCompositeExtract must be in-bounds");
3801 cur
= cur
->elems
[indices
[i
]];
3809 vtn_handle_composite(struct vtn_builder
*b
, SpvOp opcode
,
3810 const uint32_t *w
, unsigned count
)
3812 struct vtn_type
*type
= vtn_get_type(b
, w
[1]);
3813 struct vtn_ssa_value
*ssa
= vtn_create_ssa_value(b
, type
->type
);
3816 case SpvOpVectorExtractDynamic
:
3817 ssa
->def
= nir_vector_extract(&b
->nb
, vtn_get_nir_ssa(b
, w
[3]),
3818 vtn_get_nir_ssa(b
, w
[4]));
3821 case SpvOpVectorInsertDynamic
:
3822 ssa
->def
= nir_vector_insert(&b
->nb
, vtn_get_nir_ssa(b
, w
[3]),
3823 vtn_get_nir_ssa(b
, w
[4]),
3824 vtn_get_nir_ssa(b
, w
[5]));
3827 case SpvOpVectorShuffle
:
3828 ssa
->def
= vtn_vector_shuffle(b
, glsl_get_vector_elements(type
->type
),
3829 vtn_get_nir_ssa(b
, w
[3]),
3830 vtn_get_nir_ssa(b
, w
[4]),
3834 case SpvOpCompositeConstruct
: {
3835 unsigned elems
= count
- 3;
3837 if (glsl_type_is_vector_or_scalar(type
->type
)) {
3838 nir_ssa_def
*srcs
[NIR_MAX_VEC_COMPONENTS
];
3839 for (unsigned i
= 0; i
< elems
; i
++)
3840 srcs
[i
] = vtn_get_nir_ssa(b
, w
[3 + i
]);
3842 vtn_vector_construct(b
, glsl_get_vector_elements(type
->type
),
3845 ssa
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
3846 for (unsigned i
= 0; i
< elems
; i
++)
3847 ssa
->elems
[i
] = vtn_ssa_value(b
, w
[3 + i
]);
3851 case SpvOpCompositeExtract
:
3852 ssa
= vtn_composite_extract(b
, vtn_ssa_value(b
, w
[3]),
3856 case SpvOpCompositeInsert
:
3857 ssa
= vtn_composite_insert(b
, vtn_ssa_value(b
, w
[4]),
3858 vtn_ssa_value(b
, w
[3]),
3862 case SpvOpCopyLogical
:
3863 ssa
= vtn_composite_copy(b
, vtn_ssa_value(b
, w
[3]));
3865 case SpvOpCopyObject
:
3866 vtn_copy_value(b
, w
[3], w
[2]);
3870 vtn_fail_with_opcode("unknown composite operation", opcode
);
3873 vtn_push_ssa_value(b
, w
[2], ssa
);
3877 vtn_emit_barrier(struct vtn_builder
*b
, nir_intrinsic_op op
)
3879 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
3880 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3884 vtn_emit_memory_barrier(struct vtn_builder
*b
, SpvScope scope
,
3885 SpvMemorySemanticsMask semantics
)
3887 if (b
->shader
->options
->use_scoped_barrier
) {
3888 vtn_emit_scoped_memory_barrier(b
, scope
, semantics
);
3892 static const SpvMemorySemanticsMask all_memory_semantics
=
3893 SpvMemorySemanticsUniformMemoryMask
|
3894 SpvMemorySemanticsWorkgroupMemoryMask
|
3895 SpvMemorySemanticsAtomicCounterMemoryMask
|
3896 SpvMemorySemanticsImageMemoryMask
|
3897 SpvMemorySemanticsOutputMemoryMask
;
3899 /* If we're not actually doing a memory barrier, bail */
3900 if (!(semantics
& all_memory_semantics
))
3903 /* GL and Vulkan don't have these */
3904 vtn_assert(scope
!= SpvScopeCrossDevice
);
3906 if (scope
== SpvScopeSubgroup
)
3907 return; /* Nothing to do here */
3909 if (scope
== SpvScopeWorkgroup
) {
3910 vtn_emit_barrier(b
, nir_intrinsic_group_memory_barrier
);
3914 /* There's only two scopes thing left */
3915 vtn_assert(scope
== SpvScopeInvocation
|| scope
== SpvScopeDevice
);
3917 /* Map the GLSL memoryBarrier() construct and any barriers with more than one
3918 * semantic to the corresponding NIR one.
3920 if (util_bitcount(semantics
& all_memory_semantics
) > 1) {
3921 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier
);
3922 if (semantics
& SpvMemorySemanticsOutputMemoryMask
) {
3923 /* GLSL memoryBarrier() (and the corresponding NIR one) doesn't include
3924 * TCS outputs, so we have to emit it's own intrinsic for that. We
3925 * then need to emit another memory_barrier to prevent moving
3926 * non-output operations to before the tcs_patch barrier.
3928 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_tcs_patch
);
3929 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier
);
3934 /* Issue a more specific barrier */
3935 switch (semantics
& all_memory_semantics
) {
3936 case SpvMemorySemanticsUniformMemoryMask
:
3937 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_buffer
);
3939 case SpvMemorySemanticsWorkgroupMemoryMask
:
3940 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_shared
);
3942 case SpvMemorySemanticsAtomicCounterMemoryMask
:
3943 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_atomic_counter
);
3945 case SpvMemorySemanticsImageMemoryMask
:
3946 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_image
);
3948 case SpvMemorySemanticsOutputMemoryMask
:
3949 if (b
->nb
.shader
->info
.stage
== MESA_SHADER_TESS_CTRL
)
3950 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_tcs_patch
);
3958 vtn_handle_barrier(struct vtn_builder
*b
, SpvOp opcode
,
3959 const uint32_t *w
, UNUSED
unsigned count
)
3962 case SpvOpEmitVertex
:
3963 case SpvOpEmitStreamVertex
:
3964 case SpvOpEndPrimitive
:
3965 case SpvOpEndStreamPrimitive
: {
3966 nir_intrinsic_op intrinsic_op
;
3968 case SpvOpEmitVertex
:
3969 case SpvOpEmitStreamVertex
:
3970 intrinsic_op
= nir_intrinsic_emit_vertex
;
3972 case SpvOpEndPrimitive
:
3973 case SpvOpEndStreamPrimitive
:
3974 intrinsic_op
= nir_intrinsic_end_primitive
;
3977 unreachable("Invalid opcode");
3980 nir_intrinsic_instr
*intrin
=
3981 nir_intrinsic_instr_create(b
->shader
, intrinsic_op
);
3984 case SpvOpEmitStreamVertex
:
3985 case SpvOpEndStreamPrimitive
: {
3986 unsigned stream
= vtn_constant_uint(b
, w
[1]);
3987 nir_intrinsic_set_stream_id(intrin
, stream
);
3995 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3999 case SpvOpMemoryBarrier
: {
4000 SpvScope scope
= vtn_constant_uint(b
, w
[1]);
4001 SpvMemorySemanticsMask semantics
= vtn_constant_uint(b
, w
[2]);
4002 vtn_emit_memory_barrier(b
, scope
, semantics
);
4006 case SpvOpControlBarrier
: {
4007 SpvScope execution_scope
= vtn_constant_uint(b
, w
[1]);
4008 SpvScope memory_scope
= vtn_constant_uint(b
, w
[2]);
4009 SpvMemorySemanticsMask memory_semantics
= vtn_constant_uint(b
, w
[3]);
4011 /* GLSLang, prior to commit 8297936dd6eb3, emitted OpControlBarrier with
4012 * memory semantics of None for GLSL barrier().
4013 * And before that, prior to c3f1cdfa, emitted the OpControlBarrier with
4014 * Device instead of Workgroup for execution scope.
4016 if (b
->wa_glslang_cs_barrier
&&
4017 b
->nb
.shader
->info
.stage
== MESA_SHADER_COMPUTE
&&
4018 (execution_scope
== SpvScopeWorkgroup
||
4019 execution_scope
== SpvScopeDevice
) &&
4020 memory_semantics
== SpvMemorySemanticsMaskNone
) {
4021 execution_scope
= SpvScopeWorkgroup
;
4022 memory_scope
= SpvScopeWorkgroup
;
4023 memory_semantics
= SpvMemorySemanticsAcquireReleaseMask
|
4024 SpvMemorySemanticsWorkgroupMemoryMask
;
4027 /* From the SPIR-V spec:
4029 * "When used with the TessellationControl execution model, it also
4030 * implicitly synchronizes the Output Storage Class: Writes to Output
4031 * variables performed by any invocation executed prior to a
4032 * OpControlBarrier will be visible to any other invocation after
4033 * return from that OpControlBarrier."
4035 if (b
->nb
.shader
->info
.stage
== MESA_SHADER_TESS_CTRL
) {
4036 memory_semantics
&= ~(SpvMemorySemanticsAcquireMask
|
4037 SpvMemorySemanticsReleaseMask
|
4038 SpvMemorySemanticsAcquireReleaseMask
|
4039 SpvMemorySemanticsSequentiallyConsistentMask
);
4040 memory_semantics
|= SpvMemorySemanticsAcquireReleaseMask
|
4041 SpvMemorySemanticsOutputMemoryMask
;
4044 if (b
->shader
->options
->use_scoped_barrier
) {
4045 vtn_emit_scoped_control_barrier(b
, execution_scope
, memory_scope
,
4048 vtn_emit_memory_barrier(b
, memory_scope
, memory_semantics
);
4050 if (execution_scope
== SpvScopeWorkgroup
)
4051 vtn_emit_barrier(b
, nir_intrinsic_control_barrier
);
4057 unreachable("unknown barrier instruction");
4062 gl_primitive_from_spv_execution_mode(struct vtn_builder
*b
,
4063 SpvExecutionMode mode
)
4066 case SpvExecutionModeInputPoints
:
4067 case SpvExecutionModeOutputPoints
:
4068 return 0; /* GL_POINTS */
4069 case SpvExecutionModeInputLines
:
4070 return 1; /* GL_LINES */
4071 case SpvExecutionModeInputLinesAdjacency
:
4072 return 0x000A; /* GL_LINE_STRIP_ADJACENCY_ARB */
4073 case SpvExecutionModeTriangles
:
4074 return 4; /* GL_TRIANGLES */
4075 case SpvExecutionModeInputTrianglesAdjacency
:
4076 return 0x000C; /* GL_TRIANGLES_ADJACENCY_ARB */
4077 case SpvExecutionModeQuads
:
4078 return 7; /* GL_QUADS */
4079 case SpvExecutionModeIsolines
:
4080 return 0x8E7A; /* GL_ISOLINES */
4081 case SpvExecutionModeOutputLineStrip
:
4082 return 3; /* GL_LINE_STRIP */
4083 case SpvExecutionModeOutputTriangleStrip
:
4084 return 5; /* GL_TRIANGLE_STRIP */
4086 vtn_fail("Invalid primitive type: %s (%u)",
4087 spirv_executionmode_to_string(mode
), mode
);
4092 vertices_in_from_spv_execution_mode(struct vtn_builder
*b
,
4093 SpvExecutionMode mode
)
4096 case SpvExecutionModeInputPoints
:
4098 case SpvExecutionModeInputLines
:
4100 case SpvExecutionModeInputLinesAdjacency
:
4102 case SpvExecutionModeTriangles
:
4104 case SpvExecutionModeInputTrianglesAdjacency
:
4107 vtn_fail("Invalid GS input mode: %s (%u)",
4108 spirv_executionmode_to_string(mode
), mode
);
4112 static gl_shader_stage
4113 stage_for_execution_model(struct vtn_builder
*b
, SpvExecutionModel model
)
4116 case SpvExecutionModelVertex
:
4117 return MESA_SHADER_VERTEX
;
4118 case SpvExecutionModelTessellationControl
:
4119 return MESA_SHADER_TESS_CTRL
;
4120 case SpvExecutionModelTessellationEvaluation
:
4121 return MESA_SHADER_TESS_EVAL
;
4122 case SpvExecutionModelGeometry
:
4123 return MESA_SHADER_GEOMETRY
;
4124 case SpvExecutionModelFragment
:
4125 return MESA_SHADER_FRAGMENT
;
4126 case SpvExecutionModelGLCompute
:
4127 return MESA_SHADER_COMPUTE
;
4128 case SpvExecutionModelKernel
:
4129 return MESA_SHADER_KERNEL
;
4131 vtn_fail("Unsupported execution model: %s (%u)",
4132 spirv_executionmodel_to_string(model
), model
);
4136 #define spv_check_supported(name, cap) do { \
4137 if (!(b->options && b->options->caps.name)) \
4138 vtn_warn("Unsupported SPIR-V capability: %s (%u)", \
4139 spirv_capability_to_string(cap), cap); \
4144 vtn_handle_entry_point(struct vtn_builder
*b
, const uint32_t *w
,
4147 struct vtn_value
*entry_point
= &b
->values
[w
[2]];
4148 /* Let this be a name label regardless */
4149 unsigned name_words
;
4150 entry_point
->name
= vtn_string_literal(b
, &w
[3], count
- 3, &name_words
);
4152 if (strcmp(entry_point
->name
, b
->entry_point_name
) != 0 ||
4153 stage_for_execution_model(b
, w
[1]) != b
->entry_point_stage
)
4156 vtn_assert(b
->entry_point
== NULL
);
4157 b
->entry_point
= entry_point
;
4161 vtn_handle_preamble_instruction(struct vtn_builder
*b
, SpvOp opcode
,
4162 const uint32_t *w
, unsigned count
)
4169 case SpvSourceLanguageUnknown
: lang
= "unknown"; break;
4170 case SpvSourceLanguageESSL
: lang
= "ESSL"; break;
4171 case SpvSourceLanguageGLSL
: lang
= "GLSL"; break;
4172 case SpvSourceLanguageOpenCL_C
: lang
= "OpenCL C"; break;
4173 case SpvSourceLanguageOpenCL_CPP
: lang
= "OpenCL C++"; break;
4174 case SpvSourceLanguageHLSL
: lang
= "HLSL"; break;
4177 uint32_t version
= w
[2];
4180 (count
> 3) ? vtn_value(b
, w
[3], vtn_value_type_string
)->str
: "";
4182 vtn_info("Parsing SPIR-V from %s %u source file %s", lang
, version
, file
);
4186 case SpvOpSourceExtension
:
4187 case SpvOpSourceContinued
:
4188 case SpvOpExtension
:
4189 case SpvOpModuleProcessed
:
4190 /* Unhandled, but these are for debug so that's ok. */
4193 case SpvOpCapability
: {
4194 SpvCapability cap
= w
[1];
4196 case SpvCapabilityMatrix
:
4197 case SpvCapabilityShader
:
4198 case SpvCapabilityGeometry
:
4199 case SpvCapabilityGeometryPointSize
:
4200 case SpvCapabilityUniformBufferArrayDynamicIndexing
:
4201 case SpvCapabilitySampledImageArrayDynamicIndexing
:
4202 case SpvCapabilityStorageBufferArrayDynamicIndexing
:
4203 case SpvCapabilityStorageImageArrayDynamicIndexing
:
4204 case SpvCapabilityImageRect
:
4205 case SpvCapabilitySampledRect
:
4206 case SpvCapabilitySampled1D
:
4207 case SpvCapabilityImage1D
:
4208 case SpvCapabilitySampledCubeArray
:
4209 case SpvCapabilityImageCubeArray
:
4210 case SpvCapabilitySampledBuffer
:
4211 case SpvCapabilityImageBuffer
:
4212 case SpvCapabilityImageQuery
:
4213 case SpvCapabilityDerivativeControl
:
4214 case SpvCapabilityInterpolationFunction
:
4215 case SpvCapabilityMultiViewport
:
4216 case SpvCapabilitySampleRateShading
:
4217 case SpvCapabilityClipDistance
:
4218 case SpvCapabilityCullDistance
:
4219 case SpvCapabilityInputAttachment
:
4220 case SpvCapabilityImageGatherExtended
:
4221 case SpvCapabilityStorageImageExtendedFormats
:
4222 case SpvCapabilityVector16
:
4225 case SpvCapabilityLinkage
:
4226 case SpvCapabilityFloat16Buffer
:
4227 case SpvCapabilitySparseResidency
:
4228 vtn_warn("Unsupported SPIR-V capability: %s",
4229 spirv_capability_to_string(cap
));
4232 case SpvCapabilityMinLod
:
4233 spv_check_supported(min_lod
, cap
);
4236 case SpvCapabilityAtomicStorage
:
4237 spv_check_supported(atomic_storage
, cap
);
4240 case SpvCapabilityFloat64
:
4241 spv_check_supported(float64
, cap
);
4243 case SpvCapabilityInt64
:
4244 spv_check_supported(int64
, cap
);
4246 case SpvCapabilityInt16
:
4247 spv_check_supported(int16
, cap
);
4249 case SpvCapabilityInt8
:
4250 spv_check_supported(int8
, cap
);
4253 case SpvCapabilityTransformFeedback
:
4254 spv_check_supported(transform_feedback
, cap
);
4257 case SpvCapabilityGeometryStreams
:
4258 spv_check_supported(geometry_streams
, cap
);
4261 case SpvCapabilityInt64Atomics
:
4262 spv_check_supported(int64_atomics
, cap
);
4265 case SpvCapabilityStorageImageMultisample
:
4266 spv_check_supported(storage_image_ms
, cap
);
4269 case SpvCapabilityAddresses
:
4270 spv_check_supported(address
, cap
);
4273 case SpvCapabilityKernel
:
4274 spv_check_supported(kernel
, cap
);
4277 case SpvCapabilityImageBasic
:
4278 spv_check_supported(kernel_image
, cap
);
4281 case SpvCapabilityLiteralSampler
:
4282 spv_check_supported(literal_sampler
, cap
);
4285 case SpvCapabilityImageReadWrite
:
4286 case SpvCapabilityImageMipmap
:
4287 case SpvCapabilityPipes
:
4288 case SpvCapabilityDeviceEnqueue
:
4289 case SpvCapabilityGenericPointer
:
4290 vtn_warn("Unsupported OpenCL-style SPIR-V capability: %s",
4291 spirv_capability_to_string(cap
));
4294 case SpvCapabilityImageMSArray
:
4295 spv_check_supported(image_ms_array
, cap
);
4298 case SpvCapabilityTessellation
:
4299 case SpvCapabilityTessellationPointSize
:
4300 spv_check_supported(tessellation
, cap
);
4303 case SpvCapabilityDrawParameters
:
4304 spv_check_supported(draw_parameters
, cap
);
4307 case SpvCapabilityStorageImageReadWithoutFormat
:
4308 spv_check_supported(image_read_without_format
, cap
);
4311 case SpvCapabilityStorageImageWriteWithoutFormat
:
4312 spv_check_supported(image_write_without_format
, cap
);
4315 case SpvCapabilityDeviceGroup
:
4316 spv_check_supported(device_group
, cap
);
4319 case SpvCapabilityMultiView
:
4320 spv_check_supported(multiview
, cap
);
4323 case SpvCapabilityGroupNonUniform
:
4324 spv_check_supported(subgroup_basic
, cap
);
4327 case SpvCapabilitySubgroupVoteKHR
:
4328 case SpvCapabilityGroupNonUniformVote
:
4329 spv_check_supported(subgroup_vote
, cap
);
4332 case SpvCapabilitySubgroupBallotKHR
:
4333 case SpvCapabilityGroupNonUniformBallot
:
4334 spv_check_supported(subgroup_ballot
, cap
);
4337 case SpvCapabilityGroupNonUniformShuffle
:
4338 case SpvCapabilityGroupNonUniformShuffleRelative
:
4339 spv_check_supported(subgroup_shuffle
, cap
);
4342 case SpvCapabilityGroupNonUniformQuad
:
4343 spv_check_supported(subgroup_quad
, cap
);
4346 case SpvCapabilityGroupNonUniformArithmetic
:
4347 case SpvCapabilityGroupNonUniformClustered
:
4348 spv_check_supported(subgroup_arithmetic
, cap
);
4351 case SpvCapabilityGroups
:
4352 spv_check_supported(amd_shader_ballot
, cap
);
4355 case SpvCapabilityVariablePointersStorageBuffer
:
4356 case SpvCapabilityVariablePointers
:
4357 spv_check_supported(variable_pointers
, cap
);
4358 b
->variable_pointers
= true;
4361 case SpvCapabilityStorageUniformBufferBlock16
:
4362 case SpvCapabilityStorageUniform16
:
4363 case SpvCapabilityStoragePushConstant16
:
4364 case SpvCapabilityStorageInputOutput16
:
4365 spv_check_supported(storage_16bit
, cap
);
4368 case SpvCapabilityShaderLayer
:
4369 case SpvCapabilityShaderViewportIndex
:
4370 case SpvCapabilityShaderViewportIndexLayerEXT
:
4371 spv_check_supported(shader_viewport_index_layer
, cap
);
4374 case SpvCapabilityStorageBuffer8BitAccess
:
4375 case SpvCapabilityUniformAndStorageBuffer8BitAccess
:
4376 case SpvCapabilityStoragePushConstant8
:
4377 spv_check_supported(storage_8bit
, cap
);
4380 case SpvCapabilityShaderNonUniformEXT
:
4381 spv_check_supported(descriptor_indexing
, cap
);
4384 case SpvCapabilityInputAttachmentArrayDynamicIndexingEXT
:
4385 case SpvCapabilityUniformTexelBufferArrayDynamicIndexingEXT
:
4386 case SpvCapabilityStorageTexelBufferArrayDynamicIndexingEXT
:
4387 spv_check_supported(descriptor_array_dynamic_indexing
, cap
);
4390 case SpvCapabilityUniformBufferArrayNonUniformIndexingEXT
:
4391 case SpvCapabilitySampledImageArrayNonUniformIndexingEXT
:
4392 case SpvCapabilityStorageBufferArrayNonUniformIndexingEXT
:
4393 case SpvCapabilityStorageImageArrayNonUniformIndexingEXT
:
4394 case SpvCapabilityInputAttachmentArrayNonUniformIndexingEXT
:
4395 case SpvCapabilityUniformTexelBufferArrayNonUniformIndexingEXT
:
4396 case SpvCapabilityStorageTexelBufferArrayNonUniformIndexingEXT
:
4397 spv_check_supported(descriptor_array_non_uniform_indexing
, cap
);
4400 case SpvCapabilityRuntimeDescriptorArrayEXT
:
4401 spv_check_supported(runtime_descriptor_array
, cap
);
4404 case SpvCapabilityStencilExportEXT
:
4405 spv_check_supported(stencil_export
, cap
);
4408 case SpvCapabilitySampleMaskPostDepthCoverage
:
4409 spv_check_supported(post_depth_coverage
, cap
);
4412 case SpvCapabilityDenormFlushToZero
:
4413 case SpvCapabilityDenormPreserve
:
4414 case SpvCapabilitySignedZeroInfNanPreserve
:
4415 case SpvCapabilityRoundingModeRTE
:
4416 case SpvCapabilityRoundingModeRTZ
:
4417 spv_check_supported(float_controls
, cap
);
4420 case SpvCapabilityPhysicalStorageBufferAddresses
:
4421 spv_check_supported(physical_storage_buffer_address
, cap
);
4424 case SpvCapabilityComputeDerivativeGroupQuadsNV
:
4425 case SpvCapabilityComputeDerivativeGroupLinearNV
:
4426 spv_check_supported(derivative_group
, cap
);
4429 case SpvCapabilityFloat16
:
4430 spv_check_supported(float16
, cap
);
4433 case SpvCapabilityFragmentShaderSampleInterlockEXT
:
4434 spv_check_supported(fragment_shader_sample_interlock
, cap
);
4437 case SpvCapabilityFragmentShaderPixelInterlockEXT
:
4438 spv_check_supported(fragment_shader_pixel_interlock
, cap
);
4441 case SpvCapabilityDemoteToHelperInvocationEXT
:
4442 spv_check_supported(demote_to_helper_invocation
, cap
);
4445 case SpvCapabilityShaderClockKHR
:
4446 spv_check_supported(shader_clock
, cap
);
4449 case SpvCapabilityVulkanMemoryModel
:
4450 spv_check_supported(vk_memory_model
, cap
);
4453 case SpvCapabilityVulkanMemoryModelDeviceScope
:
4454 spv_check_supported(vk_memory_model_device_scope
, cap
);
4457 case SpvCapabilityImageReadWriteLodAMD
:
4458 spv_check_supported(amd_image_read_write_lod
, cap
);
4461 case SpvCapabilityIntegerFunctions2INTEL
:
4462 spv_check_supported(integer_functions2
, cap
);
4465 case SpvCapabilityFragmentMaskAMD
:
4466 spv_check_supported(amd_fragment_mask
, cap
);
4469 case SpvCapabilityImageGatherBiasLodAMD
:
4470 spv_check_supported(amd_image_gather_bias_lod
, cap
);
4473 case SpvCapabilityAtomicFloat32AddEXT
:
4474 spv_check_supported(float32_atomic_add
, cap
);
4477 case SpvCapabilityAtomicFloat64AddEXT
:
4478 spv_check_supported(float64_atomic_add
, cap
);
4482 vtn_fail("Unhandled capability: %s (%u)",
4483 spirv_capability_to_string(cap
), cap
);
4488 case SpvOpExtInstImport
:
4489 vtn_handle_extension(b
, opcode
, w
, count
);
4492 case SpvOpMemoryModel
:
4494 case SpvAddressingModelPhysical32
:
4495 vtn_fail_if(b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
,
4496 "AddressingModelPhysical32 only supported for kernels");
4497 b
->shader
->info
.cs
.ptr_size
= 32;
4498 b
->physical_ptrs
= true;
4499 assert(nir_address_format_bit_size(b
->options
->global_addr_format
) == 32);
4500 assert(nir_address_format_num_components(b
->options
->global_addr_format
) == 1);
4501 assert(nir_address_format_bit_size(b
->options
->shared_addr_format
) == 32);
4502 assert(nir_address_format_num_components(b
->options
->shared_addr_format
) == 1);
4503 assert(nir_address_format_bit_size(b
->options
->constant_addr_format
) == 32);
4504 assert(nir_address_format_num_components(b
->options
->constant_addr_format
) == 1);
4506 case SpvAddressingModelPhysical64
:
4507 vtn_fail_if(b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
,
4508 "AddressingModelPhysical64 only supported for kernels");
4509 b
->shader
->info
.cs
.ptr_size
= 64;
4510 b
->physical_ptrs
= true;
4511 assert(nir_address_format_bit_size(b
->options
->global_addr_format
) == 64);
4512 assert(nir_address_format_num_components(b
->options
->global_addr_format
) == 1);
4513 assert(nir_address_format_bit_size(b
->options
->shared_addr_format
) == 64);
4514 assert(nir_address_format_num_components(b
->options
->shared_addr_format
) == 1);
4515 assert(nir_address_format_bit_size(b
->options
->constant_addr_format
) == 64);
4516 assert(nir_address_format_num_components(b
->options
->constant_addr_format
) == 1);
4518 case SpvAddressingModelLogical
:
4519 vtn_fail_if(b
->shader
->info
.stage
== MESA_SHADER_KERNEL
,
4520 "AddressingModelLogical only supported for shaders");
4521 b
->physical_ptrs
= false;
4523 case SpvAddressingModelPhysicalStorageBuffer64
:
4524 vtn_fail_if(!b
->options
||
4525 !b
->options
->caps
.physical_storage_buffer_address
,
4526 "AddressingModelPhysicalStorageBuffer64 not supported");
4529 vtn_fail("Unknown addressing model: %s (%u)",
4530 spirv_addressingmodel_to_string(w
[1]), w
[1]);
4534 b
->mem_model
= w
[2];
4536 case SpvMemoryModelSimple
:
4537 case SpvMemoryModelGLSL450
:
4538 case SpvMemoryModelOpenCL
:
4540 case SpvMemoryModelVulkan
:
4541 vtn_fail_if(!b
->options
->caps
.vk_memory_model
,
4542 "Vulkan memory model is unsupported by this driver");
4545 vtn_fail("Unsupported memory model: %s",
4546 spirv_memorymodel_to_string(w
[2]));
4551 case SpvOpEntryPoint
:
4552 vtn_handle_entry_point(b
, w
, count
);
4556 vtn_push_value(b
, w
[1], vtn_value_type_string
)->str
=
4557 vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
4561 b
->values
[w
[1]].name
= vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
4564 case SpvOpMemberName
:
4568 case SpvOpExecutionMode
:
4569 case SpvOpExecutionModeId
:
4570 case SpvOpDecorationGroup
:
4572 case SpvOpDecorateId
:
4573 case SpvOpMemberDecorate
:
4574 case SpvOpGroupDecorate
:
4575 case SpvOpGroupMemberDecorate
:
4576 case SpvOpDecorateString
:
4577 case SpvOpMemberDecorateString
:
4578 vtn_handle_decoration(b
, opcode
, w
, count
);
4581 case SpvOpExtInst
: {
4582 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
4583 if (val
->ext_handler
== vtn_handle_non_semantic_instruction
) {
4584 /* NonSemantic extended instructions are acceptable in preamble. */
4585 vtn_handle_non_semantic_instruction(b
, w
[4], w
, count
);
4588 return false; /* End of preamble. */
4593 return false; /* End of preamble */
4600 vtn_handle_execution_mode(struct vtn_builder
*b
, struct vtn_value
*entry_point
,
4601 const struct vtn_decoration
*mode
, UNUSED
void *data
)
4603 vtn_assert(b
->entry_point
== entry_point
);
4605 switch(mode
->exec_mode
) {
4606 case SpvExecutionModeOriginUpperLeft
:
4607 case SpvExecutionModeOriginLowerLeft
:
4608 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4609 b
->shader
->info
.fs
.origin_upper_left
=
4610 (mode
->exec_mode
== SpvExecutionModeOriginUpperLeft
);
4613 case SpvExecutionModeEarlyFragmentTests
:
4614 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4615 b
->shader
->info
.fs
.early_fragment_tests
= true;
4618 case SpvExecutionModePostDepthCoverage
:
4619 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4620 b
->shader
->info
.fs
.post_depth_coverage
= true;
4623 case SpvExecutionModeInvocations
:
4624 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4625 b
->shader
->info
.gs
.invocations
= MAX2(1, mode
->operands
[0]);
4628 case SpvExecutionModeDepthReplacing
:
4629 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4630 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_ANY
;
4632 case SpvExecutionModeDepthGreater
:
4633 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4634 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_GREATER
;
4636 case SpvExecutionModeDepthLess
:
4637 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4638 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_LESS
;
4640 case SpvExecutionModeDepthUnchanged
:
4641 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4642 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
4645 case SpvExecutionModeLocalSize
:
4646 vtn_assert(gl_shader_stage_is_compute(b
->shader
->info
.stage
));
4647 b
->shader
->info
.cs
.local_size
[0] = mode
->operands
[0];
4648 b
->shader
->info
.cs
.local_size
[1] = mode
->operands
[1];
4649 b
->shader
->info
.cs
.local_size
[2] = mode
->operands
[2];
4652 case SpvExecutionModeLocalSizeHint
:
4653 break; /* Nothing to do with this */
4655 case SpvExecutionModeOutputVertices
:
4656 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4657 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
4658 b
->shader
->info
.tess
.tcs_vertices_out
= mode
->operands
[0];
4660 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4661 b
->shader
->info
.gs
.vertices_out
= mode
->operands
[0];
4665 case SpvExecutionModeInputPoints
:
4666 case SpvExecutionModeInputLines
:
4667 case SpvExecutionModeInputLinesAdjacency
:
4668 case SpvExecutionModeTriangles
:
4669 case SpvExecutionModeInputTrianglesAdjacency
:
4670 case SpvExecutionModeQuads
:
4671 case SpvExecutionModeIsolines
:
4672 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4673 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
4674 b
->shader
->info
.tess
.primitive_mode
=
4675 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
4677 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4678 b
->shader
->info
.gs
.vertices_in
=
4679 vertices_in_from_spv_execution_mode(b
, mode
->exec_mode
);
4680 b
->shader
->info
.gs
.input_primitive
=
4681 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
4685 case SpvExecutionModeOutputPoints
:
4686 case SpvExecutionModeOutputLineStrip
:
4687 case SpvExecutionModeOutputTriangleStrip
:
4688 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4689 b
->shader
->info
.gs
.output_primitive
=
4690 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
4693 case SpvExecutionModeSpacingEqual
:
4694 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4695 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4696 b
->shader
->info
.tess
.spacing
= TESS_SPACING_EQUAL
;
4698 case SpvExecutionModeSpacingFractionalEven
:
4699 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4700 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4701 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_EVEN
;
4703 case SpvExecutionModeSpacingFractionalOdd
:
4704 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4705 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4706 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_ODD
;
4708 case SpvExecutionModeVertexOrderCw
:
4709 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4710 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4711 b
->shader
->info
.tess
.ccw
= false;
4713 case SpvExecutionModeVertexOrderCcw
:
4714 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4715 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4716 b
->shader
->info
.tess
.ccw
= true;
4718 case SpvExecutionModePointMode
:
4719 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4720 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4721 b
->shader
->info
.tess
.point_mode
= true;
4724 case SpvExecutionModePixelCenterInteger
:
4725 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4726 b
->shader
->info
.fs
.pixel_center_integer
= true;
4729 case SpvExecutionModeXfb
:
4730 b
->shader
->info
.has_transform_feedback_varyings
= true;
4733 case SpvExecutionModeVecTypeHint
:
4736 case SpvExecutionModeContractionOff
:
4737 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
)
4738 vtn_warn("ExectionMode only allowed for CL-style kernels: %s",
4739 spirv_executionmode_to_string(mode
->exec_mode
));
4744 case SpvExecutionModeStencilRefReplacingEXT
:
4745 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4748 case SpvExecutionModeDerivativeGroupQuadsNV
:
4749 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_COMPUTE
);
4750 b
->shader
->info
.cs
.derivative_group
= DERIVATIVE_GROUP_QUADS
;
4753 case SpvExecutionModeDerivativeGroupLinearNV
:
4754 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_COMPUTE
);
4755 b
->shader
->info
.cs
.derivative_group
= DERIVATIVE_GROUP_LINEAR
;
4758 case SpvExecutionModePixelInterlockOrderedEXT
:
4759 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4760 b
->shader
->info
.fs
.pixel_interlock_ordered
= true;
4763 case SpvExecutionModePixelInterlockUnorderedEXT
:
4764 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4765 b
->shader
->info
.fs
.pixel_interlock_unordered
= true;
4768 case SpvExecutionModeSampleInterlockOrderedEXT
:
4769 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4770 b
->shader
->info
.fs
.sample_interlock_ordered
= true;
4773 case SpvExecutionModeSampleInterlockUnorderedEXT
:
4774 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4775 b
->shader
->info
.fs
.sample_interlock_unordered
= true;
4778 case SpvExecutionModeDenormPreserve
:
4779 case SpvExecutionModeDenormFlushToZero
:
4780 case SpvExecutionModeSignedZeroInfNanPreserve
:
4781 case SpvExecutionModeRoundingModeRTE
:
4782 case SpvExecutionModeRoundingModeRTZ
: {
4783 unsigned execution_mode
= 0;
4784 switch (mode
->exec_mode
) {
4785 case SpvExecutionModeDenormPreserve
:
4786 switch (mode
->operands
[0]) {
4787 case 16: execution_mode
= FLOAT_CONTROLS_DENORM_PRESERVE_FP16
; break;
4788 case 32: execution_mode
= FLOAT_CONTROLS_DENORM_PRESERVE_FP32
; break;
4789 case 64: execution_mode
= FLOAT_CONTROLS_DENORM_PRESERVE_FP64
; break;
4790 default: vtn_fail("Floating point type not supported");
4793 case SpvExecutionModeDenormFlushToZero
:
4794 switch (mode
->operands
[0]) {
4795 case 16: execution_mode
= FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16
; break;
4796 case 32: execution_mode
= FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32
; break;
4797 case 64: execution_mode
= FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP64
; break;
4798 default: vtn_fail("Floating point type not supported");
4801 case SpvExecutionModeSignedZeroInfNanPreserve
:
4802 switch (mode
->operands
[0]) {
4803 case 16: execution_mode
= FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP16
; break;
4804 case 32: execution_mode
= FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP32
; break;
4805 case 64: execution_mode
= FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP64
; break;
4806 default: vtn_fail("Floating point type not supported");
4809 case SpvExecutionModeRoundingModeRTE
:
4810 switch (mode
->operands
[0]) {
4811 case 16: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16
; break;
4812 case 32: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32
; break;
4813 case 64: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64
; break;
4814 default: vtn_fail("Floating point type not supported");
4817 case SpvExecutionModeRoundingModeRTZ
:
4818 switch (mode
->operands
[0]) {
4819 case 16: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16
; break;
4820 case 32: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32
; break;
4821 case 64: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64
; break;
4822 default: vtn_fail("Floating point type not supported");
4829 b
->shader
->info
.float_controls_execution_mode
|= execution_mode
;
4833 case SpvExecutionModeLocalSizeId
:
4834 case SpvExecutionModeLocalSizeHintId
:
4835 /* Handled later by vtn_handle_execution_mode_id(). */
4839 vtn_fail("Unhandled execution mode: %s (%u)",
4840 spirv_executionmode_to_string(mode
->exec_mode
),
4846 vtn_handle_execution_mode_id(struct vtn_builder
*b
, struct vtn_value
*entry_point
,
4847 const struct vtn_decoration
*mode
, UNUSED
void *data
)
4850 vtn_assert(b
->entry_point
== entry_point
);
4852 switch (mode
->exec_mode
) {
4853 case SpvExecutionModeLocalSizeId
:
4854 b
->shader
->info
.cs
.local_size
[0] = vtn_constant_uint(b
, mode
->operands
[0]);
4855 b
->shader
->info
.cs
.local_size
[1] = vtn_constant_uint(b
, mode
->operands
[1]);
4856 b
->shader
->info
.cs
.local_size
[2] = vtn_constant_uint(b
, mode
->operands
[2]);
4859 case SpvExecutionModeLocalSizeHintId
:
4860 /* Nothing to do with this hint. */
4864 /* Nothing to do. Literal execution modes already handled by
4865 * vtn_handle_execution_mode(). */
4871 vtn_handle_variable_or_type_instruction(struct vtn_builder
*b
, SpvOp opcode
,
4872 const uint32_t *w
, unsigned count
)
4874 vtn_set_instruction_result_type(b
, opcode
, w
, count
);
4878 case SpvOpSourceContinued
:
4879 case SpvOpSourceExtension
:
4880 case SpvOpExtension
:
4881 case SpvOpCapability
:
4882 case SpvOpExtInstImport
:
4883 case SpvOpMemoryModel
:
4884 case SpvOpEntryPoint
:
4885 case SpvOpExecutionMode
:
4888 case SpvOpMemberName
:
4889 case SpvOpDecorationGroup
:
4891 case SpvOpDecorateId
:
4892 case SpvOpMemberDecorate
:
4893 case SpvOpGroupDecorate
:
4894 case SpvOpGroupMemberDecorate
:
4895 case SpvOpDecorateString
:
4896 case SpvOpMemberDecorateString
:
4897 vtn_fail("Invalid opcode types and variables section");
4903 case SpvOpTypeFloat
:
4904 case SpvOpTypeVector
:
4905 case SpvOpTypeMatrix
:
4906 case SpvOpTypeImage
:
4907 case SpvOpTypeSampler
:
4908 case SpvOpTypeSampledImage
:
4909 case SpvOpTypeArray
:
4910 case SpvOpTypeRuntimeArray
:
4911 case SpvOpTypeStruct
:
4912 case SpvOpTypeOpaque
:
4913 case SpvOpTypePointer
:
4914 case SpvOpTypeForwardPointer
:
4915 case SpvOpTypeFunction
:
4916 case SpvOpTypeEvent
:
4917 case SpvOpTypeDeviceEvent
:
4918 case SpvOpTypeReserveId
:
4919 case SpvOpTypeQueue
:
4921 vtn_handle_type(b
, opcode
, w
, count
);
4924 case SpvOpConstantTrue
:
4925 case SpvOpConstantFalse
:
4927 case SpvOpConstantComposite
:
4928 case SpvOpConstantNull
:
4929 case SpvOpSpecConstantTrue
:
4930 case SpvOpSpecConstantFalse
:
4931 case SpvOpSpecConstant
:
4932 case SpvOpSpecConstantComposite
:
4933 case SpvOpSpecConstantOp
:
4934 vtn_handle_constant(b
, opcode
, w
, count
);
4939 case SpvOpConstantSampler
:
4940 vtn_handle_variables(b
, opcode
, w
, count
);
4943 case SpvOpExtInst
: {
4944 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
4945 /* NonSemantic extended instructions are acceptable in preamble, others
4946 * will indicate the end of preamble.
4948 return val
->ext_handler
== vtn_handle_non_semantic_instruction
;
4952 return false; /* End of preamble */
4958 static struct vtn_ssa_value
*
4959 vtn_nir_select(struct vtn_builder
*b
, struct vtn_ssa_value
*src0
,
4960 struct vtn_ssa_value
*src1
, struct vtn_ssa_value
*src2
)
4962 struct vtn_ssa_value
*dest
= rzalloc(b
, struct vtn_ssa_value
);
4963 dest
->type
= src1
->type
;
4965 if (glsl_type_is_vector_or_scalar(src1
->type
)) {
4966 dest
->def
= nir_bcsel(&b
->nb
, src0
->def
, src1
->def
, src2
->def
);
4968 unsigned elems
= glsl_get_length(src1
->type
);
4970 dest
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
4971 for (unsigned i
= 0; i
< elems
; i
++) {
4972 dest
->elems
[i
] = vtn_nir_select(b
, src0
,
4973 src1
->elems
[i
], src2
->elems
[i
]);
4981 vtn_handle_select(struct vtn_builder
*b
, SpvOp opcode
,
4982 const uint32_t *w
, unsigned count
)
4984 /* Handle OpSelect up-front here because it needs to be able to handle
4985 * pointers and not just regular vectors and scalars.
4987 struct vtn_value
*res_val
= vtn_untyped_value(b
, w
[2]);
4988 struct vtn_value
*cond_val
= vtn_untyped_value(b
, w
[3]);
4989 struct vtn_value
*obj1_val
= vtn_untyped_value(b
, w
[4]);
4990 struct vtn_value
*obj2_val
= vtn_untyped_value(b
, w
[5]);
4992 vtn_fail_if(obj1_val
->type
!= res_val
->type
||
4993 obj2_val
->type
!= res_val
->type
,
4994 "Object types must match the result type in OpSelect");
4996 vtn_fail_if((cond_val
->type
->base_type
!= vtn_base_type_scalar
&&
4997 cond_val
->type
->base_type
!= vtn_base_type_vector
) ||
4998 !glsl_type_is_boolean(cond_val
->type
->type
),
4999 "OpSelect must have either a vector of booleans or "
5000 "a boolean as Condition type");
5002 vtn_fail_if(cond_val
->type
->base_type
== vtn_base_type_vector
&&
5003 (res_val
->type
->base_type
!= vtn_base_type_vector
||
5004 res_val
->type
->length
!= cond_val
->type
->length
),
5005 "When Condition type in OpSelect is a vector, the Result "
5006 "type must be a vector of the same length");
5008 switch (res_val
->type
->base_type
) {
5009 case vtn_base_type_scalar
:
5010 case vtn_base_type_vector
:
5011 case vtn_base_type_matrix
:
5012 case vtn_base_type_array
:
5013 case vtn_base_type_struct
:
5016 case vtn_base_type_pointer
:
5017 /* We need to have actual storage for pointer types. */
5018 vtn_fail_if(res_val
->type
->type
== NULL
,
5019 "Invalid pointer result type for OpSelect");
5022 vtn_fail("Result type of OpSelect must be a scalar, composite, or pointer");
5025 vtn_push_ssa_value(b
, w
[2],
5026 vtn_nir_select(b
, vtn_ssa_value(b
, w
[3]),
5027 vtn_ssa_value(b
, w
[4]),
5028 vtn_ssa_value(b
, w
[5])));
5032 vtn_handle_ptr(struct vtn_builder
*b
, SpvOp opcode
,
5033 const uint32_t *w
, unsigned count
)
5035 struct vtn_type
*type1
= vtn_get_value_type(b
, w
[3]);
5036 struct vtn_type
*type2
= vtn_get_value_type(b
, w
[4]);
5037 vtn_fail_if(type1
->base_type
!= vtn_base_type_pointer
||
5038 type2
->base_type
!= vtn_base_type_pointer
,
5039 "%s operands must have pointer types",
5040 spirv_op_to_string(opcode
));
5041 vtn_fail_if(type1
->storage_class
!= type2
->storage_class
,
5042 "%s operands must have the same storage class",
5043 spirv_op_to_string(opcode
));
5045 struct vtn_type
*vtn_type
= vtn_get_type(b
, w
[1]);
5046 const struct glsl_type
*type
= vtn_type
->type
;
5048 nir_address_format addr_format
= vtn_mode_to_address_format(
5049 b
, vtn_storage_class_to_mode(b
, type1
->storage_class
, NULL
, NULL
));
5054 case SpvOpPtrDiff
: {
5055 /* OpPtrDiff returns the difference in number of elements (not byte offset). */
5056 unsigned elem_size
, elem_align
;
5057 glsl_get_natural_size_align_bytes(type1
->deref
->type
,
5058 &elem_size
, &elem_align
);
5060 def
= nir_build_addr_isub(&b
->nb
,
5061 vtn_get_nir_ssa(b
, w
[3]),
5062 vtn_get_nir_ssa(b
, w
[4]),
5064 def
= nir_idiv(&b
->nb
, def
, nir_imm_intN_t(&b
->nb
, elem_size
, def
->bit_size
));
5065 def
= nir_i2i(&b
->nb
, def
, glsl_get_bit_size(type
));
5070 case SpvOpPtrNotEqual
: {
5071 def
= nir_build_addr_ieq(&b
->nb
,
5072 vtn_get_nir_ssa(b
, w
[3]),
5073 vtn_get_nir_ssa(b
, w
[4]),
5075 if (opcode
== SpvOpPtrNotEqual
)
5076 def
= nir_inot(&b
->nb
, def
);
5081 unreachable("Invalid ptr operation");
5084 vtn_push_nir_ssa(b
, w
[2], def
);
5088 vtn_handle_body_instruction(struct vtn_builder
*b
, SpvOp opcode
,
5089 const uint32_t *w
, unsigned count
)
5095 case SpvOpLoopMerge
:
5096 case SpvOpSelectionMerge
:
5097 /* This is handled by cfg pre-pass and walk_blocks */
5101 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
5102 val
->type
= vtn_get_type(b
, w
[1]);
5107 vtn_handle_extension(b
, opcode
, w
, count
);
5113 case SpvOpCopyMemory
:
5114 case SpvOpCopyMemorySized
:
5115 case SpvOpAccessChain
:
5116 case SpvOpPtrAccessChain
:
5117 case SpvOpInBoundsAccessChain
:
5118 case SpvOpInBoundsPtrAccessChain
:
5119 case SpvOpArrayLength
:
5120 case SpvOpConvertPtrToU
:
5121 case SpvOpConvertUToPtr
:
5122 vtn_handle_variables(b
, opcode
, w
, count
);
5125 case SpvOpFunctionCall
:
5126 vtn_handle_function_call(b
, opcode
, w
, count
);
5129 case SpvOpSampledImage
:
5131 case SpvOpImageSampleImplicitLod
:
5132 case SpvOpImageSampleExplicitLod
:
5133 case SpvOpImageSampleDrefImplicitLod
:
5134 case SpvOpImageSampleDrefExplicitLod
:
5135 case SpvOpImageSampleProjImplicitLod
:
5136 case SpvOpImageSampleProjExplicitLod
:
5137 case SpvOpImageSampleProjDrefImplicitLod
:
5138 case SpvOpImageSampleProjDrefExplicitLod
:
5139 case SpvOpImageFetch
:
5140 case SpvOpImageGather
:
5141 case SpvOpImageDrefGather
:
5142 case SpvOpImageQueryLod
:
5143 case SpvOpImageQueryLevels
:
5144 case SpvOpImageQuerySamples
:
5145 vtn_handle_texture(b
, opcode
, w
, count
);
5148 case SpvOpImageRead
:
5149 case SpvOpImageWrite
:
5150 case SpvOpImageTexelPointer
:
5151 case SpvOpImageQueryFormat
:
5152 case SpvOpImageQueryOrder
:
5153 vtn_handle_image(b
, opcode
, w
, count
);
5156 case SpvOpImageQuerySizeLod
:
5157 case SpvOpImageQuerySize
: {
5158 struct vtn_type
*image_type
= vtn_get_value_type(b
, w
[3]);
5159 vtn_assert(image_type
->base_type
== vtn_base_type_image
);
5160 if (glsl_type_is_image(image_type
->glsl_image
)) {
5161 vtn_handle_image(b
, opcode
, w
, count
);
5163 vtn_assert(glsl_type_is_sampler(image_type
->glsl_image
));
5164 vtn_handle_texture(b
, opcode
, w
, count
);
5169 case SpvOpFragmentMaskFetchAMD
:
5170 case SpvOpFragmentFetchAMD
:
5171 vtn_handle_texture(b
, opcode
, w
, count
);
5174 case SpvOpAtomicLoad
:
5175 case SpvOpAtomicExchange
:
5176 case SpvOpAtomicCompareExchange
:
5177 case SpvOpAtomicCompareExchangeWeak
:
5178 case SpvOpAtomicIIncrement
:
5179 case SpvOpAtomicIDecrement
:
5180 case SpvOpAtomicIAdd
:
5181 case SpvOpAtomicISub
:
5182 case SpvOpAtomicSMin
:
5183 case SpvOpAtomicUMin
:
5184 case SpvOpAtomicSMax
:
5185 case SpvOpAtomicUMax
:
5186 case SpvOpAtomicAnd
:
5188 case SpvOpAtomicXor
:
5189 case SpvOpAtomicFAddEXT
: {
5190 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[3]);
5191 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
5192 vtn_handle_image(b
, opcode
, w
, count
);
5194 vtn_assert(pointer
->value_type
== vtn_value_type_pointer
);
5195 vtn_handle_atomics(b
, opcode
, w
, count
);
5200 case SpvOpAtomicStore
: {
5201 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[1]);
5202 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
5203 vtn_handle_image(b
, opcode
, w
, count
);
5205 vtn_assert(pointer
->value_type
== vtn_value_type_pointer
);
5206 vtn_handle_atomics(b
, opcode
, w
, count
);
5212 vtn_handle_select(b
, opcode
, w
, count
);
5220 case SpvOpConvertFToU
:
5221 case SpvOpConvertFToS
:
5222 case SpvOpConvertSToF
:
5223 case SpvOpConvertUToF
:
5227 case SpvOpQuantizeToF16
:
5228 case SpvOpPtrCastToGeneric
:
5229 case SpvOpGenericCastToPtr
:
5234 case SpvOpSignBitSet
:
5235 case SpvOpLessOrGreater
:
5237 case SpvOpUnordered
:
5252 case SpvOpVectorTimesScalar
:
5254 case SpvOpIAddCarry
:
5255 case SpvOpISubBorrow
:
5256 case SpvOpUMulExtended
:
5257 case SpvOpSMulExtended
:
5258 case SpvOpShiftRightLogical
:
5259 case SpvOpShiftRightArithmetic
:
5260 case SpvOpShiftLeftLogical
:
5261 case SpvOpLogicalEqual
:
5262 case SpvOpLogicalNotEqual
:
5263 case SpvOpLogicalOr
:
5264 case SpvOpLogicalAnd
:
5265 case SpvOpLogicalNot
:
5266 case SpvOpBitwiseOr
:
5267 case SpvOpBitwiseXor
:
5268 case SpvOpBitwiseAnd
:
5270 case SpvOpFOrdEqual
:
5271 case SpvOpFUnordEqual
:
5272 case SpvOpINotEqual
:
5273 case SpvOpFOrdNotEqual
:
5274 case SpvOpFUnordNotEqual
:
5275 case SpvOpULessThan
:
5276 case SpvOpSLessThan
:
5277 case SpvOpFOrdLessThan
:
5278 case SpvOpFUnordLessThan
:
5279 case SpvOpUGreaterThan
:
5280 case SpvOpSGreaterThan
:
5281 case SpvOpFOrdGreaterThan
:
5282 case SpvOpFUnordGreaterThan
:
5283 case SpvOpULessThanEqual
:
5284 case SpvOpSLessThanEqual
:
5285 case SpvOpFOrdLessThanEqual
:
5286 case SpvOpFUnordLessThanEqual
:
5287 case SpvOpUGreaterThanEqual
:
5288 case SpvOpSGreaterThanEqual
:
5289 case SpvOpFOrdGreaterThanEqual
:
5290 case SpvOpFUnordGreaterThanEqual
:
5296 case SpvOpFwidthFine
:
5297 case SpvOpDPdxCoarse
:
5298 case SpvOpDPdyCoarse
:
5299 case SpvOpFwidthCoarse
:
5300 case SpvOpBitFieldInsert
:
5301 case SpvOpBitFieldSExtract
:
5302 case SpvOpBitFieldUExtract
:
5303 case SpvOpBitReverse
:
5305 case SpvOpTranspose
:
5306 case SpvOpOuterProduct
:
5307 case SpvOpMatrixTimesScalar
:
5308 case SpvOpVectorTimesMatrix
:
5309 case SpvOpMatrixTimesVector
:
5310 case SpvOpMatrixTimesMatrix
:
5311 case SpvOpUCountLeadingZerosINTEL
:
5312 case SpvOpUCountTrailingZerosINTEL
:
5313 case SpvOpAbsISubINTEL
:
5314 case SpvOpAbsUSubINTEL
:
5315 case SpvOpIAddSatINTEL
:
5316 case SpvOpUAddSatINTEL
:
5317 case SpvOpIAverageINTEL
:
5318 case SpvOpUAverageINTEL
:
5319 case SpvOpIAverageRoundedINTEL
:
5320 case SpvOpUAverageRoundedINTEL
:
5321 case SpvOpISubSatINTEL
:
5322 case SpvOpUSubSatINTEL
:
5323 case SpvOpIMul32x16INTEL
:
5324 case SpvOpUMul32x16INTEL
:
5325 vtn_handle_alu(b
, opcode
, w
, count
);
5329 vtn_handle_bitcast(b
, w
, count
);
5332 case SpvOpVectorExtractDynamic
:
5333 case SpvOpVectorInsertDynamic
:
5334 case SpvOpVectorShuffle
:
5335 case SpvOpCompositeConstruct
:
5336 case SpvOpCompositeExtract
:
5337 case SpvOpCompositeInsert
:
5338 case SpvOpCopyLogical
:
5339 case SpvOpCopyObject
:
5340 vtn_handle_composite(b
, opcode
, w
, count
);
5343 case SpvOpEmitVertex
:
5344 case SpvOpEndPrimitive
:
5345 case SpvOpEmitStreamVertex
:
5346 case SpvOpEndStreamPrimitive
:
5347 case SpvOpControlBarrier
:
5348 case SpvOpMemoryBarrier
:
5349 vtn_handle_barrier(b
, opcode
, w
, count
);
5352 case SpvOpGroupNonUniformElect
:
5353 case SpvOpGroupNonUniformAll
:
5354 case SpvOpGroupNonUniformAny
:
5355 case SpvOpGroupNonUniformAllEqual
:
5356 case SpvOpGroupNonUniformBroadcast
:
5357 case SpvOpGroupNonUniformBroadcastFirst
:
5358 case SpvOpGroupNonUniformBallot
:
5359 case SpvOpGroupNonUniformInverseBallot
:
5360 case SpvOpGroupNonUniformBallotBitExtract
:
5361 case SpvOpGroupNonUniformBallotBitCount
:
5362 case SpvOpGroupNonUniformBallotFindLSB
:
5363 case SpvOpGroupNonUniformBallotFindMSB
:
5364 case SpvOpGroupNonUniformShuffle
:
5365 case SpvOpGroupNonUniformShuffleXor
:
5366 case SpvOpGroupNonUniformShuffleUp
:
5367 case SpvOpGroupNonUniformShuffleDown
:
5368 case SpvOpGroupNonUniformIAdd
:
5369 case SpvOpGroupNonUniformFAdd
:
5370 case SpvOpGroupNonUniformIMul
:
5371 case SpvOpGroupNonUniformFMul
:
5372 case SpvOpGroupNonUniformSMin
:
5373 case SpvOpGroupNonUniformUMin
:
5374 case SpvOpGroupNonUniformFMin
:
5375 case SpvOpGroupNonUniformSMax
:
5376 case SpvOpGroupNonUniformUMax
:
5377 case SpvOpGroupNonUniformFMax
:
5378 case SpvOpGroupNonUniformBitwiseAnd
:
5379 case SpvOpGroupNonUniformBitwiseOr
:
5380 case SpvOpGroupNonUniformBitwiseXor
:
5381 case SpvOpGroupNonUniformLogicalAnd
:
5382 case SpvOpGroupNonUniformLogicalOr
:
5383 case SpvOpGroupNonUniformLogicalXor
:
5384 case SpvOpGroupNonUniformQuadBroadcast
:
5385 case SpvOpGroupNonUniformQuadSwap
:
5388 case SpvOpGroupBroadcast
:
5389 case SpvOpGroupIAdd
:
5390 case SpvOpGroupFAdd
:
5391 case SpvOpGroupFMin
:
5392 case SpvOpGroupUMin
:
5393 case SpvOpGroupSMin
:
5394 case SpvOpGroupFMax
:
5395 case SpvOpGroupUMax
:
5396 case SpvOpGroupSMax
:
5397 case SpvOpSubgroupBallotKHR
:
5398 case SpvOpSubgroupFirstInvocationKHR
:
5399 case SpvOpSubgroupReadInvocationKHR
:
5400 case SpvOpSubgroupAllKHR
:
5401 case SpvOpSubgroupAnyKHR
:
5402 case SpvOpSubgroupAllEqualKHR
:
5403 case SpvOpGroupIAddNonUniformAMD
:
5404 case SpvOpGroupFAddNonUniformAMD
:
5405 case SpvOpGroupFMinNonUniformAMD
:
5406 case SpvOpGroupUMinNonUniformAMD
:
5407 case SpvOpGroupSMinNonUniformAMD
:
5408 case SpvOpGroupFMaxNonUniformAMD
:
5409 case SpvOpGroupUMaxNonUniformAMD
:
5410 case SpvOpGroupSMaxNonUniformAMD
:
5411 vtn_handle_subgroup(b
, opcode
, w
, count
);
5416 case SpvOpPtrNotEqual
:
5417 vtn_handle_ptr(b
, opcode
, w
, count
);
5420 case SpvOpBeginInvocationInterlockEXT
:
5421 vtn_emit_barrier(b
, nir_intrinsic_begin_invocation_interlock
);
5424 case SpvOpEndInvocationInterlockEXT
:
5425 vtn_emit_barrier(b
, nir_intrinsic_end_invocation_interlock
);
5428 case SpvOpDemoteToHelperInvocationEXT
: {
5429 nir_intrinsic_instr
*intrin
=
5430 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_demote
);
5431 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
5435 case SpvOpIsHelperInvocationEXT
: {
5436 nir_intrinsic_instr
*intrin
=
5437 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_is_helper_invocation
);
5438 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
, 1, 1, NULL
);
5439 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
5441 vtn_push_nir_ssa(b
, w
[2], &intrin
->dest
.ssa
);
5445 case SpvOpReadClockKHR
: {
5446 SpvScope scope
= vtn_constant_uint(b
, w
[3]);
5447 nir_scope nir_scope
;
5450 case SpvScopeDevice
:
5451 nir_scope
= NIR_SCOPE_DEVICE
;
5453 case SpvScopeSubgroup
:
5454 nir_scope
= NIR_SCOPE_SUBGROUP
;
5457 vtn_fail("invalid read clock scope");
5460 /* Operation supports two result types: uvec2 and uint64_t. The NIR
5461 * intrinsic gives uvec2, so pack the result for the other case.
5463 nir_intrinsic_instr
*intrin
=
5464 nir_intrinsic_instr_create(b
->nb
.shader
, nir_intrinsic_shader_clock
);
5465 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
, 2, 32, NULL
);
5466 nir_intrinsic_set_memory_scope(intrin
, nir_scope
);
5467 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
5469 struct vtn_type
*type
= vtn_get_type(b
, w
[1]);
5470 const struct glsl_type
*dest_type
= type
->type
;
5471 nir_ssa_def
*result
;
5473 if (glsl_type_is_vector(dest_type
)) {
5474 assert(dest_type
== glsl_vector_type(GLSL_TYPE_UINT
, 2));
5475 result
= &intrin
->dest
.ssa
;
5477 assert(glsl_type_is_scalar(dest_type
));
5478 assert(glsl_get_base_type(dest_type
) == GLSL_TYPE_UINT64
);
5479 result
= nir_pack_64_2x32(&b
->nb
, &intrin
->dest
.ssa
);
5482 vtn_push_nir_ssa(b
, w
[2], result
);
5486 case SpvOpLifetimeStart
:
5487 case SpvOpLifetimeStop
:
5491 vtn_fail_with_opcode("Unhandled opcode", opcode
);
5498 vtn_create_builder(const uint32_t *words
, size_t word_count
,
5499 gl_shader_stage stage
, const char *entry_point_name
,
5500 const struct spirv_to_nir_options
*options
)
5502 /* Initialize the vtn_builder object */
5503 struct vtn_builder
*b
= rzalloc(NULL
, struct vtn_builder
);
5504 struct spirv_to_nir_options
*dup_options
=
5505 ralloc(b
, struct spirv_to_nir_options
);
5506 *dup_options
= *options
;
5509 b
->spirv_word_count
= word_count
;
5513 list_inithead(&b
->functions
);
5514 b
->entry_point_stage
= stage
;
5515 b
->entry_point_name
= entry_point_name
;
5516 b
->options
= dup_options
;
5519 * Handle the SPIR-V header (first 5 dwords).
5520 * Can't use vtx_assert() as the setjmp(3) target isn't initialized yet.
5522 if (word_count
<= 5)
5525 if (words
[0] != SpvMagicNumber
) {
5526 vtn_err("words[0] was 0x%x, want 0x%x", words
[0], SpvMagicNumber
);
5529 if (words
[1] < 0x10000) {
5530 vtn_err("words[1] was 0x%x, want >= 0x10000", words
[1]);
5534 uint16_t generator_id
= words
[2] >> 16;
5535 uint16_t generator_version
= words
[2];
5537 /* In GLSLang commit 8297936dd6eb3, their handling of barrier() was fixed
5538 * to provide correct memory semantics on compute shader barrier()
5539 * commands. Prior to that, we need to fix them up ourselves. This
5540 * GLSLang fix caused them to bump to generator version 3.
5542 b
->wa_glslang_cs_barrier
= (generator_id
== 8 && generator_version
< 3);
5544 /* words[2] == generator magic */
5545 unsigned value_id_bound
= words
[3];
5546 if (words
[4] != 0) {
5547 vtn_err("words[4] was %u, want 0", words
[4]);
5551 b
->value_id_bound
= value_id_bound
;
5552 b
->values
= rzalloc_array(b
, struct vtn_value
, value_id_bound
);
5560 static nir_function
*
5561 vtn_emit_kernel_entry_point_wrapper(struct vtn_builder
*b
,
5562 nir_function
*entry_point
)
5564 vtn_assert(entry_point
== b
->entry_point
->func
->impl
->function
);
5565 vtn_fail_if(!entry_point
->name
, "entry points are required to have a name");
5566 const char *func_name
=
5567 ralloc_asprintf(b
->shader
, "__wrapped_%s", entry_point
->name
);
5569 /* we shouldn't have any inputs yet */
5570 vtn_assert(!entry_point
->shader
->num_inputs
);
5571 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_KERNEL
);
5573 nir_function
*main_entry_point
= nir_function_create(b
->shader
, func_name
);
5574 main_entry_point
->impl
= nir_function_impl_create(main_entry_point
);
5575 nir_builder_init(&b
->nb
, main_entry_point
->impl
);
5576 b
->nb
.cursor
= nir_after_cf_list(&main_entry_point
->impl
->body
);
5577 b
->func_param_idx
= 0;
5579 nir_call_instr
*call
= nir_call_instr_create(b
->nb
.shader
, entry_point
);
5581 for (unsigned i
= 0; i
< entry_point
->num_params
; ++i
) {
5582 struct vtn_type
*param_type
= b
->entry_point
->func
->type
->params
[i
];
5584 /* consider all pointers to function memory to be parameters passed
5587 bool is_by_val
= param_type
->base_type
== vtn_base_type_pointer
&&
5588 param_type
->storage_class
== SpvStorageClassFunction
;
5590 /* input variable */
5591 nir_variable
*in_var
= rzalloc(b
->nb
.shader
, nir_variable
);
5592 in_var
->data
.mode
= nir_var_uniform
;
5593 in_var
->data
.read_only
= true;
5594 in_var
->data
.location
= i
;
5595 if (param_type
->base_type
== vtn_base_type_image
) {
5596 in_var
->data
.access
= 0;
5597 if (param_type
->access_qualifier
& SpvAccessQualifierReadOnly
)
5598 in_var
->data
.access
|= ACCESS_NON_WRITEABLE
;
5599 if (param_type
->access_qualifier
& SpvAccessQualifierWriteOnly
)
5600 in_var
->data
.access
|= ACCESS_NON_READABLE
;
5604 in_var
->type
= param_type
->deref
->type
;
5605 else if (param_type
->base_type
== vtn_base_type_image
)
5606 in_var
->type
= param_type
->glsl_image
;
5607 else if (param_type
->base_type
== vtn_base_type_sampler
)
5608 in_var
->type
= glsl_bare_sampler_type();
5610 in_var
->type
= param_type
->type
;
5612 nir_shader_add_variable(b
->nb
.shader
, in_var
);
5613 b
->nb
.shader
->num_inputs
++;
5615 /* we have to copy the entire variable into function memory */
5617 nir_variable
*copy_var
=
5618 nir_local_variable_create(main_entry_point
->impl
, in_var
->type
,
5620 nir_copy_var(&b
->nb
, copy_var
, in_var
);
5622 nir_src_for_ssa(&nir_build_deref_var(&b
->nb
, copy_var
)->dest
.ssa
);
5623 } else if (param_type
->base_type
== vtn_base_type_image
||
5624 param_type
->base_type
== vtn_base_type_sampler
) {
5625 /* Don't load the var, just pass a deref of it */
5626 call
->params
[i
] = nir_src_for_ssa(&nir_build_deref_var(&b
->nb
, in_var
)->dest
.ssa
);
5628 call
->params
[i
] = nir_src_for_ssa(nir_load_var(&b
->nb
, in_var
));
5632 nir_builder_instr_insert(&b
->nb
, &call
->instr
);
5634 return main_entry_point
;
5638 spirv_to_nir(const uint32_t *words
, size_t word_count
,
5639 struct nir_spirv_specialization
*spec
, unsigned num_spec
,
5640 gl_shader_stage stage
, const char *entry_point_name
,
5641 const struct spirv_to_nir_options
*options
,
5642 const nir_shader_compiler_options
*nir_options
)
5645 const uint32_t *word_end
= words
+ word_count
;
5647 struct vtn_builder
*b
= vtn_create_builder(words
, word_count
,
5648 stage
, entry_point_name
,
5654 /* See also _vtn_fail() */
5655 if (setjmp(b
->fail_jump
)) {
5660 /* Skip the SPIR-V header, handled at vtn_create_builder */
5663 b
->shader
= nir_shader_create(b
, stage
, nir_options
, NULL
);
5665 /* Handle all the preamble instructions */
5666 words
= vtn_foreach_instruction(b
, words
, word_end
,
5667 vtn_handle_preamble_instruction
);
5669 if (b
->entry_point
== NULL
) {
5670 vtn_fail("Entry point not found");
5675 /* Ensure a sane address mode is being used for function temps */
5676 assert(nir_address_format_bit_size(b
->options
->temp_addr_format
) == nir_get_ptr_bitsize(b
->shader
));
5677 assert(nir_address_format_num_components(b
->options
->temp_addr_format
) == 1);
5679 /* Set shader info defaults */
5680 if (stage
== MESA_SHADER_GEOMETRY
)
5681 b
->shader
->info
.gs
.invocations
= 1;
5683 /* Parse execution modes. */
5684 vtn_foreach_execution_mode(b
, b
->entry_point
,
5685 vtn_handle_execution_mode
, NULL
);
5687 b
->specializations
= spec
;
5688 b
->num_specializations
= num_spec
;
5690 /* Handle all variable, type, and constant instructions */
5691 words
= vtn_foreach_instruction(b
, words
, word_end
,
5692 vtn_handle_variable_or_type_instruction
);
5694 /* Parse execution modes that depend on IDs. Must happen after we have
5697 vtn_foreach_execution_mode(b
, b
->entry_point
,
5698 vtn_handle_execution_mode_id
, NULL
);
5700 if (b
->workgroup_size_builtin
) {
5701 vtn_assert(b
->workgroup_size_builtin
->type
->type
==
5702 glsl_vector_type(GLSL_TYPE_UINT
, 3));
5704 nir_const_value
*const_size
=
5705 b
->workgroup_size_builtin
->constant
->values
;
5707 b
->shader
->info
.cs
.local_size
[0] = const_size
[0].u32
;
5708 b
->shader
->info
.cs
.local_size
[1] = const_size
[1].u32
;
5709 b
->shader
->info
.cs
.local_size
[2] = const_size
[2].u32
;
5712 /* Set types on all vtn_values */
5713 vtn_foreach_instruction(b
, words
, word_end
, vtn_set_instruction_result_type
);
5715 vtn_build_cfg(b
, words
, word_end
);
5717 assert(b
->entry_point
->value_type
== vtn_value_type_function
);
5718 b
->entry_point
->func
->referenced
= true;
5723 vtn_foreach_cf_node(node
, &b
->functions
) {
5724 struct vtn_function
*func
= vtn_cf_node_as_function(node
);
5725 if (func
->referenced
&& !func
->emitted
) {
5726 b
->const_table
= _mesa_pointer_hash_table_create(b
);
5728 vtn_function_emit(b
, func
, vtn_handle_body_instruction
);
5734 vtn_assert(b
->entry_point
->value_type
== vtn_value_type_function
);
5735 nir_function
*entry_point
= b
->entry_point
->func
->impl
->function
;
5736 vtn_assert(entry_point
);
5738 /* post process entry_points with input params */
5739 if (entry_point
->num_params
&& b
->shader
->info
.stage
== MESA_SHADER_KERNEL
)
5740 entry_point
= vtn_emit_kernel_entry_point_wrapper(b
, entry_point
);
5742 /* structurize the CFG */
5743 nir_lower_goto_ifs(b
->shader
);
5745 entry_point
->is_entrypoint
= true;
5747 /* When multiple shader stages exist in the same SPIR-V module, we
5748 * generate input and output variables for every stage, in the same
5749 * NIR program. These dead variables can be invalid NIR. For example,
5750 * TCS outputs must be per-vertex arrays (or decorated 'patch'), while
5751 * VS output variables wouldn't be.
5753 * To ensure we have valid NIR, we eliminate any dead inputs and outputs
5754 * right away. In order to do so, we must lower any constant initializers
5755 * on outputs so nir_remove_dead_variables sees that they're written to.
5757 nir_lower_variable_initializers(b
->shader
, nir_var_shader_out
);
5758 nir_remove_dead_variables(b
->shader
,
5759 nir_var_shader_in
| nir_var_shader_out
, NULL
);
5761 /* We sometimes generate bogus derefs that, while never used, give the
5762 * validator a bit of heartburn. Run dead code to get rid of them.
5764 nir_opt_dce(b
->shader
);
5766 /* Unparent the shader from the vtn_builder before we delete the builder */
5767 ralloc_steal(NULL
, b
->shader
);
5769 nir_shader
*shader
= b
->shader
;