2 * Copyright © 2015 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Jason Ekstrand (jason@jlekstrand.net)
28 #include "vtn_private.h"
29 #include "nir/nir_vla.h"
30 #include "nir/nir_control_flow.h"
31 #include "nir/nir_constant_expressions.h"
32 #include "nir/nir_deref.h"
33 #include "spirv_info.h"
35 #include "util/format/u_format.h"
36 #include "util/u_math.h"
41 vtn_log(struct vtn_builder
*b
, enum nir_spirv_debug_level level
,
42 size_t spirv_offset
, const char *message
)
44 if (b
->options
->debug
.func
) {
45 b
->options
->debug
.func(b
->options
->debug
.private_data
,
46 level
, spirv_offset
, message
);
50 if (level
>= NIR_SPIRV_DEBUG_LEVEL_WARNING
)
51 fprintf(stderr
, "%s\n", message
);
56 vtn_logf(struct vtn_builder
*b
, enum nir_spirv_debug_level level
,
57 size_t spirv_offset
, const char *fmt
, ...)
63 msg
= ralloc_vasprintf(NULL
, fmt
, args
);
66 vtn_log(b
, level
, spirv_offset
, msg
);
72 vtn_log_err(struct vtn_builder
*b
,
73 enum nir_spirv_debug_level level
, const char *prefix
,
74 const char *file
, unsigned line
,
75 const char *fmt
, va_list args
)
79 msg
= ralloc_strdup(NULL
, prefix
);
82 ralloc_asprintf_append(&msg
, " In file %s:%u\n", file
, line
);
85 ralloc_asprintf_append(&msg
, " ");
87 ralloc_vasprintf_append(&msg
, fmt
, args
);
89 ralloc_asprintf_append(&msg
, "\n %zu bytes into the SPIR-V binary",
93 ralloc_asprintf_append(&msg
,
94 "\n in SPIR-V source file %s, line %d, col %d",
95 b
->file
, b
->line
, b
->col
);
98 vtn_log(b
, level
, b
->spirv_offset
, msg
);
104 vtn_dump_shader(struct vtn_builder
*b
, const char *path
, const char *prefix
)
109 int len
= snprintf(filename
, sizeof(filename
), "%s/%s-%d.spirv",
110 path
, prefix
, idx
++);
111 if (len
< 0 || len
>= sizeof(filename
))
114 FILE *f
= fopen(filename
, "w");
118 fwrite(b
->spirv
, sizeof(*b
->spirv
), b
->spirv_word_count
, f
);
121 vtn_info("SPIR-V shader dumped to %s", filename
);
125 _vtn_warn(struct vtn_builder
*b
, const char *file
, unsigned line
,
126 const char *fmt
, ...)
131 vtn_log_err(b
, NIR_SPIRV_DEBUG_LEVEL_WARNING
, "SPIR-V WARNING:\n",
132 file
, line
, fmt
, args
);
137 _vtn_err(struct vtn_builder
*b
, const char *file
, unsigned line
,
138 const char *fmt
, ...)
143 vtn_log_err(b
, NIR_SPIRV_DEBUG_LEVEL_ERROR
, "SPIR-V ERROR:\n",
144 file
, line
, fmt
, args
);
149 _vtn_fail(struct vtn_builder
*b
, const char *file
, unsigned line
,
150 const char *fmt
, ...)
155 vtn_log_err(b
, NIR_SPIRV_DEBUG_LEVEL_ERROR
, "SPIR-V parsing FAILED:\n",
156 file
, line
, fmt
, args
);
159 const char *dump_path
= getenv("MESA_SPIRV_FAIL_DUMP_PATH");
161 vtn_dump_shader(b
, dump_path
, "fail");
163 longjmp(b
->fail_jump
, 1);
166 static struct vtn_ssa_value
*
167 vtn_undef_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
169 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
170 val
->type
= glsl_get_bare_type(type
);
172 if (glsl_type_is_vector_or_scalar(type
)) {
173 unsigned num_components
= glsl_get_vector_elements(val
->type
);
174 unsigned bit_size
= glsl_get_bit_size(val
->type
);
175 val
->def
= nir_ssa_undef(&b
->nb
, num_components
, bit_size
);
177 unsigned elems
= glsl_get_length(val
->type
);
178 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
179 if (glsl_type_is_array_or_matrix(type
)) {
180 const struct glsl_type
*elem_type
= glsl_get_array_element(type
);
181 for (unsigned i
= 0; i
< elems
; i
++)
182 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
184 vtn_assert(glsl_type_is_struct_or_ifc(type
));
185 for (unsigned i
= 0; i
< elems
; i
++) {
186 const struct glsl_type
*elem_type
= glsl_get_struct_field(type
, i
);
187 val
->elems
[i
] = vtn_undef_ssa_value(b
, elem_type
);
195 static struct vtn_ssa_value
*
196 vtn_const_ssa_value(struct vtn_builder
*b
, nir_constant
*constant
,
197 const struct glsl_type
*type
)
199 struct hash_entry
*entry
= _mesa_hash_table_search(b
->const_table
, constant
);
204 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
205 val
->type
= glsl_get_bare_type(type
);
207 if (glsl_type_is_vector_or_scalar(type
)) {
208 unsigned num_components
= glsl_get_vector_elements(val
->type
);
209 unsigned bit_size
= glsl_get_bit_size(type
);
210 nir_load_const_instr
*load
=
211 nir_load_const_instr_create(b
->shader
, num_components
, bit_size
);
213 memcpy(load
->value
, constant
->values
,
214 sizeof(nir_const_value
) * num_components
);
216 nir_instr_insert_before_cf_list(&b
->nb
.impl
->body
, &load
->instr
);
217 val
->def
= &load
->def
;
219 unsigned elems
= glsl_get_length(val
->type
);
220 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
221 if (glsl_type_is_array_or_matrix(type
)) {
222 const struct glsl_type
*elem_type
= glsl_get_array_element(type
);
223 for (unsigned i
= 0; i
< elems
; i
++) {
224 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
228 vtn_assert(glsl_type_is_struct_or_ifc(type
));
229 for (unsigned i
= 0; i
< elems
; i
++) {
230 const struct glsl_type
*elem_type
= glsl_get_struct_field(type
, i
);
231 val
->elems
[i
] = vtn_const_ssa_value(b
, constant
->elements
[i
],
240 struct vtn_ssa_value
*
241 vtn_ssa_value(struct vtn_builder
*b
, uint32_t value_id
)
243 struct vtn_value
*val
= vtn_untyped_value(b
, value_id
);
244 switch (val
->value_type
) {
245 case vtn_value_type_undef
:
246 return vtn_undef_ssa_value(b
, val
->type
->type
);
248 case vtn_value_type_constant
:
249 return vtn_const_ssa_value(b
, val
->constant
, val
->type
->type
);
251 case vtn_value_type_ssa
:
254 case vtn_value_type_pointer
:
255 vtn_assert(val
->pointer
->ptr_type
&& val
->pointer
->ptr_type
->type
);
256 struct vtn_ssa_value
*ssa
=
257 vtn_create_ssa_value(b
, val
->pointer
->ptr_type
->type
);
258 ssa
->def
= vtn_pointer_to_ssa(b
, val
->pointer
);
262 vtn_fail("Invalid type for an SSA value");
267 vtn_push_ssa_value(struct vtn_builder
*b
, uint32_t value_id
,
268 struct vtn_ssa_value
*ssa
)
270 struct vtn_type
*type
= vtn_get_value_type(b
, value_id
);
272 /* See vtn_create_ssa_value */
273 vtn_fail_if(ssa
->type
!= glsl_get_bare_type(type
->type
),
274 "Type mismatch for SPIR-V SSA value");
276 struct vtn_value
*val
;
277 if (type
->base_type
== vtn_base_type_pointer
) {
278 val
= vtn_push_pointer(b
, value_id
, vtn_pointer_from_ssa(b
, ssa
->def
, type
));
280 /* Don't trip the value_type_ssa check in vtn_push_value */
281 val
= vtn_push_value(b
, value_id
, vtn_value_type_invalid
);
282 val
->value_type
= vtn_value_type_ssa
;
290 vtn_get_nir_ssa(struct vtn_builder
*b
, uint32_t value_id
)
292 struct vtn_ssa_value
*ssa
= vtn_ssa_value(b
, value_id
);
293 vtn_fail_if(!glsl_type_is_vector_or_scalar(ssa
->type
),
294 "Expected a vector or scalar type");
299 vtn_push_nir_ssa(struct vtn_builder
*b
, uint32_t value_id
, nir_ssa_def
*def
)
301 /* Types for all SPIR-V SSA values are set as part of a pre-pass so the
302 * type will be valid by the time we get here.
304 struct vtn_type
*type
= vtn_get_value_type(b
, value_id
);
305 vtn_fail_if(def
->num_components
!= glsl_get_vector_elements(type
->type
) ||
306 def
->bit_size
!= glsl_get_bit_size(type
->type
),
307 "Mismatch between NIR and SPIR-V type.");
308 struct vtn_ssa_value
*ssa
= vtn_create_ssa_value(b
, type
->type
);
310 return vtn_push_ssa_value(b
, value_id
, ssa
);
313 static nir_deref_instr
*
314 vtn_get_image(struct vtn_builder
*b
, uint32_t value_id
)
316 struct vtn_type
*type
= vtn_get_value_type(b
, value_id
);
317 vtn_assert(type
->base_type
== vtn_base_type_image
);
318 return nir_build_deref_cast(&b
->nb
, vtn_get_nir_ssa(b
, value_id
),
319 nir_var_uniform
, type
->glsl_image
, 0);
323 vtn_push_image(struct vtn_builder
*b
, uint32_t value_id
,
324 nir_deref_instr
*deref
)
326 struct vtn_type
*type
= vtn_get_value_type(b
, value_id
);
327 vtn_assert(type
->base_type
== vtn_base_type_image
);
328 vtn_push_nir_ssa(b
, value_id
, &deref
->dest
.ssa
);
331 static nir_deref_instr
*
332 vtn_get_sampler(struct vtn_builder
*b
, uint32_t value_id
)
334 struct vtn_type
*type
= vtn_get_value_type(b
, value_id
);
335 vtn_assert(type
->base_type
== vtn_base_type_sampler
);
336 return nir_build_deref_cast(&b
->nb
, vtn_get_nir_ssa(b
, value_id
),
337 nir_var_uniform
, glsl_bare_sampler_type(), 0);
341 vtn_sampled_image_to_nir_ssa(struct vtn_builder
*b
,
342 struct vtn_sampled_image si
)
344 return nir_vec2(&b
->nb
, &si
.image
->dest
.ssa
, &si
.sampler
->dest
.ssa
);
348 vtn_push_sampled_image(struct vtn_builder
*b
, uint32_t value_id
,
349 struct vtn_sampled_image si
)
351 struct vtn_type
*type
= vtn_get_value_type(b
, value_id
);
352 vtn_assert(type
->base_type
== vtn_base_type_sampled_image
);
353 vtn_push_nir_ssa(b
, value_id
, vtn_sampled_image_to_nir_ssa(b
, si
));
356 static struct vtn_sampled_image
357 vtn_get_sampled_image(struct vtn_builder
*b
, uint32_t value_id
)
359 struct vtn_type
*type
= vtn_get_value_type(b
, value_id
);
360 vtn_assert(type
->base_type
== vtn_base_type_sampled_image
);
361 nir_ssa_def
*si_vec2
= vtn_get_nir_ssa(b
, value_id
);
363 struct vtn_sampled_image si
= { NULL
, };
364 si
.image
= nir_build_deref_cast(&b
->nb
, nir_channel(&b
->nb
, si_vec2
, 0),
366 type
->image
->glsl_image
, 0);
367 si
.sampler
= nir_build_deref_cast(&b
->nb
, nir_channel(&b
->nb
, si_vec2
, 1),
369 glsl_bare_sampler_type(), 0);
374 vtn_string_literal(struct vtn_builder
*b
, const uint32_t *words
,
375 unsigned word_count
, unsigned *words_used
)
377 char *dup
= ralloc_strndup(b
, (char *)words
, word_count
* sizeof(*words
));
379 /* Ammount of space taken by the string (including the null) */
380 unsigned len
= strlen(dup
) + 1;
381 *words_used
= DIV_ROUND_UP(len
, sizeof(*words
));
387 vtn_foreach_instruction(struct vtn_builder
*b
, const uint32_t *start
,
388 const uint32_t *end
, vtn_instruction_handler handler
)
394 const uint32_t *w
= start
;
396 SpvOp opcode
= w
[0] & SpvOpCodeMask
;
397 unsigned count
= w
[0] >> SpvWordCountShift
;
398 vtn_assert(count
>= 1 && w
+ count
<= end
);
400 b
->spirv_offset
= (uint8_t *)w
- (uint8_t *)b
->spirv
;
404 break; /* Do nothing */
407 b
->file
= vtn_value(b
, w
[1], vtn_value_type_string
)->str
;
419 if (!handler(b
, opcode
, w
, count
))
437 vtn_handle_non_semantic_instruction(struct vtn_builder
*b
, SpvOp ext_opcode
,
438 const uint32_t *w
, unsigned count
)
445 vtn_handle_extension(struct vtn_builder
*b
, SpvOp opcode
,
446 const uint32_t *w
, unsigned count
)
448 const char *ext
= (const char *)&w
[2];
450 case SpvOpExtInstImport
: {
451 struct vtn_value
*val
= vtn_push_value(b
, w
[1], vtn_value_type_extension
);
452 if (strcmp(ext
, "GLSL.std.450") == 0) {
453 val
->ext_handler
= vtn_handle_glsl450_instruction
;
454 } else if ((strcmp(ext
, "SPV_AMD_gcn_shader") == 0)
455 && (b
->options
&& b
->options
->caps
.amd_gcn_shader
)) {
456 val
->ext_handler
= vtn_handle_amd_gcn_shader_instruction
;
457 } else if ((strcmp(ext
, "SPV_AMD_shader_ballot") == 0)
458 && (b
->options
&& b
->options
->caps
.amd_shader_ballot
)) {
459 val
->ext_handler
= vtn_handle_amd_shader_ballot_instruction
;
460 } else if ((strcmp(ext
, "SPV_AMD_shader_trinary_minmax") == 0)
461 && (b
->options
&& b
->options
->caps
.amd_trinary_minmax
)) {
462 val
->ext_handler
= vtn_handle_amd_shader_trinary_minmax_instruction
;
463 } else if ((strcmp(ext
, "SPV_AMD_shader_explicit_vertex_parameter") == 0)
464 && (b
->options
&& b
->options
->caps
.amd_shader_explicit_vertex_parameter
)) {
465 val
->ext_handler
= vtn_handle_amd_shader_explicit_vertex_parameter_instruction
;
466 } else if (strcmp(ext
, "OpenCL.std") == 0) {
467 val
->ext_handler
= vtn_handle_opencl_instruction
;
468 } else if (strstr(ext
, "NonSemantic.") == ext
) {
469 val
->ext_handler
= vtn_handle_non_semantic_instruction
;
471 vtn_fail("Unsupported extension: %s", ext
);
477 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
478 bool handled
= val
->ext_handler(b
, w
[4], w
, count
);
484 vtn_fail_with_opcode("Unhandled opcode", opcode
);
489 _foreach_decoration_helper(struct vtn_builder
*b
,
490 struct vtn_value
*base_value
,
492 struct vtn_value
*value
,
493 vtn_decoration_foreach_cb cb
, void *data
)
495 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
497 if (dec
->scope
== VTN_DEC_DECORATION
) {
498 member
= parent_member
;
499 } else if (dec
->scope
>= VTN_DEC_STRUCT_MEMBER0
) {
500 vtn_fail_if(value
->value_type
!= vtn_value_type_type
||
501 value
->type
->base_type
!= vtn_base_type_struct
,
502 "OpMemberDecorate and OpGroupMemberDecorate are only "
503 "allowed on OpTypeStruct");
504 /* This means we haven't recursed yet */
505 assert(value
== base_value
);
507 member
= dec
->scope
- VTN_DEC_STRUCT_MEMBER0
;
509 vtn_fail_if(member
>= base_value
->type
->length
,
510 "OpMemberDecorate specifies member %d but the "
511 "OpTypeStruct has only %u members",
512 member
, base_value
->type
->length
);
514 /* Not a decoration */
515 assert(dec
->scope
== VTN_DEC_EXECUTION_MODE
);
520 assert(dec
->group
->value_type
== vtn_value_type_decoration_group
);
521 _foreach_decoration_helper(b
, base_value
, member
, dec
->group
,
524 cb(b
, base_value
, member
, dec
, data
);
529 /** Iterates (recursively if needed) over all of the decorations on a value
531 * This function iterates over all of the decorations applied to a given
532 * value. If it encounters a decoration group, it recurses into the group
533 * and iterates over all of those decorations as well.
536 vtn_foreach_decoration(struct vtn_builder
*b
, struct vtn_value
*value
,
537 vtn_decoration_foreach_cb cb
, void *data
)
539 _foreach_decoration_helper(b
, value
, -1, value
, cb
, data
);
543 vtn_foreach_execution_mode(struct vtn_builder
*b
, struct vtn_value
*value
,
544 vtn_execution_mode_foreach_cb cb
, void *data
)
546 for (struct vtn_decoration
*dec
= value
->decoration
; dec
; dec
= dec
->next
) {
547 if (dec
->scope
!= VTN_DEC_EXECUTION_MODE
)
550 assert(dec
->group
== NULL
);
551 cb(b
, value
, dec
, data
);
556 vtn_handle_decoration(struct vtn_builder
*b
, SpvOp opcode
,
557 const uint32_t *w
, unsigned count
)
559 const uint32_t *w_end
= w
+ count
;
560 const uint32_t target
= w
[1];
564 case SpvOpDecorationGroup
:
565 vtn_push_value(b
, target
, vtn_value_type_decoration_group
);
569 case SpvOpDecorateId
:
570 case SpvOpMemberDecorate
:
571 case SpvOpDecorateString
:
572 case SpvOpMemberDecorateString
:
573 case SpvOpExecutionMode
:
574 case SpvOpExecutionModeId
: {
575 struct vtn_value
*val
= vtn_untyped_value(b
, target
);
577 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
580 case SpvOpDecorateId
:
581 case SpvOpDecorateString
:
582 dec
->scope
= VTN_DEC_DECORATION
;
584 case SpvOpMemberDecorate
:
585 case SpvOpMemberDecorateString
:
586 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(w
++);
587 vtn_fail_if(dec
->scope
< VTN_DEC_STRUCT_MEMBER0
, /* overflow */
588 "Member argument of OpMemberDecorate too large");
590 case SpvOpExecutionMode
:
591 case SpvOpExecutionModeId
:
592 dec
->scope
= VTN_DEC_EXECUTION_MODE
;
595 unreachable("Invalid decoration opcode");
597 dec
->decoration
= *(w
++);
600 /* Link into the list */
601 dec
->next
= val
->decoration
;
602 val
->decoration
= dec
;
606 case SpvOpGroupMemberDecorate
:
607 case SpvOpGroupDecorate
: {
608 struct vtn_value
*group
=
609 vtn_value(b
, target
, vtn_value_type_decoration_group
);
611 for (; w
< w_end
; w
++) {
612 struct vtn_value
*val
= vtn_untyped_value(b
, *w
);
613 struct vtn_decoration
*dec
= rzalloc(b
, struct vtn_decoration
);
616 if (opcode
== SpvOpGroupDecorate
) {
617 dec
->scope
= VTN_DEC_DECORATION
;
619 dec
->scope
= VTN_DEC_STRUCT_MEMBER0
+ *(++w
);
620 vtn_fail_if(dec
->scope
< 0, /* Check for overflow */
621 "Member argument of OpGroupMemberDecorate too large");
624 /* Link into the list */
625 dec
->next
= val
->decoration
;
626 val
->decoration
= dec
;
632 unreachable("Unhandled opcode");
636 struct member_decoration_ctx
{
638 struct glsl_struct_field
*fields
;
639 struct vtn_type
*type
;
643 * Returns true if the given type contains a struct decorated Block or
647 vtn_type_contains_block(struct vtn_builder
*b
, struct vtn_type
*type
)
649 switch (type
->base_type
) {
650 case vtn_base_type_array
:
651 return vtn_type_contains_block(b
, type
->array_element
);
652 case vtn_base_type_struct
:
653 if (type
->block
|| type
->buffer_block
)
655 for (unsigned i
= 0; i
< type
->length
; i
++) {
656 if (vtn_type_contains_block(b
, type
->members
[i
]))
665 /** Returns true if two types are "compatible", i.e. you can do an OpLoad,
666 * OpStore, or OpCopyMemory between them without breaking anything.
667 * Technically, the SPIR-V rules require the exact same type ID but this lets
668 * us internally be a bit looser.
671 vtn_types_compatible(struct vtn_builder
*b
,
672 struct vtn_type
*t1
, struct vtn_type
*t2
)
674 if (t1
->id
== t2
->id
)
677 if (t1
->base_type
!= t2
->base_type
)
680 switch (t1
->base_type
) {
681 case vtn_base_type_void
:
682 case vtn_base_type_scalar
:
683 case vtn_base_type_vector
:
684 case vtn_base_type_matrix
:
685 case vtn_base_type_image
:
686 case vtn_base_type_sampler
:
687 case vtn_base_type_sampled_image
:
688 return t1
->type
== t2
->type
;
690 case vtn_base_type_array
:
691 return t1
->length
== t2
->length
&&
692 vtn_types_compatible(b
, t1
->array_element
, t2
->array_element
);
694 case vtn_base_type_pointer
:
695 return vtn_types_compatible(b
, t1
->deref
, t2
->deref
);
697 case vtn_base_type_struct
:
698 if (t1
->length
!= t2
->length
)
701 for (unsigned i
= 0; i
< t1
->length
; i
++) {
702 if (!vtn_types_compatible(b
, t1
->members
[i
], t2
->members
[i
]))
707 case vtn_base_type_function
:
708 /* This case shouldn't get hit since you can't copy around function
709 * types. Just require them to be identical.
714 vtn_fail("Invalid base type");
718 vtn_type_without_array(struct vtn_type
*type
)
720 while (type
->base_type
== vtn_base_type_array
)
721 type
= type
->array_element
;
725 /* does a shallow copy of a vtn_type */
727 static struct vtn_type
*
728 vtn_type_copy(struct vtn_builder
*b
, struct vtn_type
*src
)
730 struct vtn_type
*dest
= ralloc(b
, struct vtn_type
);
733 switch (src
->base_type
) {
734 case vtn_base_type_void
:
735 case vtn_base_type_scalar
:
736 case vtn_base_type_vector
:
737 case vtn_base_type_matrix
:
738 case vtn_base_type_array
:
739 case vtn_base_type_pointer
:
740 case vtn_base_type_image
:
741 case vtn_base_type_sampler
:
742 case vtn_base_type_sampled_image
:
743 /* Nothing more to do */
746 case vtn_base_type_struct
:
747 dest
->members
= ralloc_array(b
, struct vtn_type
*, src
->length
);
748 memcpy(dest
->members
, src
->members
,
749 src
->length
* sizeof(src
->members
[0]));
751 dest
->offsets
= ralloc_array(b
, unsigned, src
->length
);
752 memcpy(dest
->offsets
, src
->offsets
,
753 src
->length
* sizeof(src
->offsets
[0]));
756 case vtn_base_type_function
:
757 dest
->params
= ralloc_array(b
, struct vtn_type
*, src
->length
);
758 memcpy(dest
->params
, src
->params
, src
->length
* sizeof(src
->params
[0]));
765 static const struct glsl_type
*
766 wrap_type_in_array(const struct glsl_type
*type
,
767 const struct glsl_type
*array_type
)
769 if (!glsl_type_is_array(array_type
))
772 const struct glsl_type
*elem_type
=
773 wrap_type_in_array(type
, glsl_get_array_element(array_type
));
774 return glsl_array_type(elem_type
, glsl_get_length(array_type
),
775 glsl_get_explicit_stride(array_type
));
779 vtn_type_needs_explicit_layout(struct vtn_builder
*b
, enum vtn_variable_mode mode
)
781 /* For OpenCL we never want to strip the info from the types, and it makes
782 * type comparisons easier in later stages.
784 if (b
->options
->environment
== NIR_SPIRV_OPENCL
)
788 case vtn_variable_mode_input
:
789 case vtn_variable_mode_output
:
790 /* Layout decorations kept because we need offsets for XFB arrays of
793 return b
->shader
->info
.has_transform_feedback_varyings
;
795 case vtn_variable_mode_ssbo
:
796 case vtn_variable_mode_phys_ssbo
:
797 case vtn_variable_mode_ubo
:
805 const struct glsl_type
*
806 vtn_type_get_nir_type(struct vtn_builder
*b
, struct vtn_type
*type
,
807 enum vtn_variable_mode mode
)
809 if (mode
== vtn_variable_mode_atomic_counter
) {
810 vtn_fail_if(glsl_without_array(type
->type
) != glsl_uint_type(),
811 "Variables in the AtomicCounter storage class should be "
812 "(possibly arrays of arrays of) uint.");
813 return wrap_type_in_array(glsl_atomic_uint_type(), type
->type
);
816 if (mode
== vtn_variable_mode_uniform
) {
817 switch (type
->base_type
) {
818 case vtn_base_type_array
: {
819 const struct glsl_type
*elem_type
=
820 vtn_type_get_nir_type(b
, type
->array_element
, mode
);
822 return glsl_array_type(elem_type
, type
->length
,
823 glsl_get_explicit_stride(type
->type
));
826 case vtn_base_type_struct
: {
827 bool need_new_struct
= false;
828 const uint32_t num_fields
= type
->length
;
829 NIR_VLA(struct glsl_struct_field
, fields
, num_fields
);
830 for (unsigned i
= 0; i
< num_fields
; i
++) {
831 fields
[i
] = *glsl_get_struct_field_data(type
->type
, i
);
832 const struct glsl_type
*field_nir_type
=
833 vtn_type_get_nir_type(b
, type
->members
[i
], mode
);
834 if (fields
[i
].type
!= field_nir_type
) {
835 fields
[i
].type
= field_nir_type
;
836 need_new_struct
= true;
839 if (need_new_struct
) {
840 if (glsl_type_is_interface(type
->type
)) {
841 return glsl_interface_type(fields
, num_fields
,
842 /* packing */ 0, false,
843 glsl_get_type_name(type
->type
));
845 return glsl_struct_type(fields
, num_fields
,
846 glsl_get_type_name(type
->type
),
847 glsl_struct_type_is_packed(type
->type
));
850 /* No changes, just pass it on */
855 case vtn_base_type_image
:
856 return type
->glsl_image
;
858 case vtn_base_type_sampler
:
859 return glsl_bare_sampler_type();
861 case vtn_base_type_sampled_image
:
862 return type
->image
->glsl_image
;
869 /* Layout decorations are allowed but ignored in certain conditions,
870 * to allow SPIR-V generators perform type deduplication. Discard
871 * unnecessary ones when passing to NIR.
873 if (!vtn_type_needs_explicit_layout(b
, mode
))
874 return glsl_get_bare_type(type
->type
);
879 static struct vtn_type
*
880 mutable_matrix_member(struct vtn_builder
*b
, struct vtn_type
*type
, int member
)
882 type
->members
[member
] = vtn_type_copy(b
, type
->members
[member
]);
883 type
= type
->members
[member
];
885 /* We may have an array of matrices.... Oh, joy! */
886 while (glsl_type_is_array(type
->type
)) {
887 type
->array_element
= vtn_type_copy(b
, type
->array_element
);
888 type
= type
->array_element
;
891 vtn_assert(glsl_type_is_matrix(type
->type
));
897 vtn_handle_access_qualifier(struct vtn_builder
*b
, struct vtn_type
*type
,
898 int member
, enum gl_access_qualifier access
)
900 type
->members
[member
] = vtn_type_copy(b
, type
->members
[member
]);
901 type
= type
->members
[member
];
903 type
->access
|= access
;
907 array_stride_decoration_cb(struct vtn_builder
*b
,
908 struct vtn_value
*val
, int member
,
909 const struct vtn_decoration
*dec
, void *void_ctx
)
911 struct vtn_type
*type
= val
->type
;
913 if (dec
->decoration
== SpvDecorationArrayStride
) {
914 if (vtn_type_contains_block(b
, type
)) {
915 vtn_warn("The ArrayStride decoration cannot be applied to an array "
916 "type which contains a structure type decorated Block "
918 /* Ignore the decoration */
920 vtn_fail_if(dec
->operands
[0] == 0, "ArrayStride must be non-zero");
921 type
->stride
= dec
->operands
[0];
927 struct_member_decoration_cb(struct vtn_builder
*b
,
928 UNUSED
struct vtn_value
*val
, int member
,
929 const struct vtn_decoration
*dec
, void *void_ctx
)
931 struct member_decoration_ctx
*ctx
= void_ctx
;
936 assert(member
< ctx
->num_fields
);
938 switch (dec
->decoration
) {
939 case SpvDecorationRelaxedPrecision
:
940 case SpvDecorationUniform
:
941 case SpvDecorationUniformId
:
942 break; /* FIXME: Do nothing with this for now. */
943 case SpvDecorationNonWritable
:
944 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_NON_WRITEABLE
);
946 case SpvDecorationNonReadable
:
947 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_NON_READABLE
);
949 case SpvDecorationVolatile
:
950 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_VOLATILE
);
952 case SpvDecorationCoherent
:
953 vtn_handle_access_qualifier(b
, ctx
->type
, member
, ACCESS_COHERENT
);
955 case SpvDecorationNoPerspective
:
956 ctx
->fields
[member
].interpolation
= INTERP_MODE_NOPERSPECTIVE
;
958 case SpvDecorationFlat
:
959 ctx
->fields
[member
].interpolation
= INTERP_MODE_FLAT
;
961 case SpvDecorationExplicitInterpAMD
:
962 ctx
->fields
[member
].interpolation
= INTERP_MODE_EXPLICIT
;
964 case SpvDecorationCentroid
:
965 ctx
->fields
[member
].centroid
= true;
967 case SpvDecorationSample
:
968 ctx
->fields
[member
].sample
= true;
970 case SpvDecorationStream
:
971 /* This is handled later by var_decoration_cb in vtn_variables.c */
973 case SpvDecorationLocation
:
974 ctx
->fields
[member
].location
= dec
->operands
[0];
976 case SpvDecorationComponent
:
977 break; /* FIXME: What should we do with these? */
978 case SpvDecorationBuiltIn
:
979 ctx
->type
->members
[member
] = vtn_type_copy(b
, ctx
->type
->members
[member
]);
980 ctx
->type
->members
[member
]->is_builtin
= true;
981 ctx
->type
->members
[member
]->builtin
= dec
->operands
[0];
982 ctx
->type
->builtin_block
= true;
984 case SpvDecorationOffset
:
985 ctx
->type
->offsets
[member
] = dec
->operands
[0];
986 ctx
->fields
[member
].offset
= dec
->operands
[0];
988 case SpvDecorationMatrixStride
:
989 /* Handled as a second pass */
991 case SpvDecorationColMajor
:
992 break; /* Nothing to do here. Column-major is the default. */
993 case SpvDecorationRowMajor
:
994 mutable_matrix_member(b
, ctx
->type
, member
)->row_major
= true;
997 case SpvDecorationPatch
:
1000 case SpvDecorationSpecId
:
1001 case SpvDecorationBlock
:
1002 case SpvDecorationBufferBlock
:
1003 case SpvDecorationArrayStride
:
1004 case SpvDecorationGLSLShared
:
1005 case SpvDecorationGLSLPacked
:
1006 case SpvDecorationInvariant
:
1007 case SpvDecorationRestrict
:
1008 case SpvDecorationAliased
:
1009 case SpvDecorationConstant
:
1010 case SpvDecorationIndex
:
1011 case SpvDecorationBinding
:
1012 case SpvDecorationDescriptorSet
:
1013 case SpvDecorationLinkageAttributes
:
1014 case SpvDecorationNoContraction
:
1015 case SpvDecorationInputAttachmentIndex
:
1016 vtn_warn("Decoration not allowed on struct members: %s",
1017 spirv_decoration_to_string(dec
->decoration
));
1020 case SpvDecorationXfbBuffer
:
1021 case SpvDecorationXfbStride
:
1022 /* This is handled later by var_decoration_cb in vtn_variables.c */
1025 case SpvDecorationCPacked
:
1026 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
)
1027 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1028 spirv_decoration_to_string(dec
->decoration
));
1030 ctx
->type
->packed
= true;
1033 case SpvDecorationSaturatedConversion
:
1034 case SpvDecorationFuncParamAttr
:
1035 case SpvDecorationFPRoundingMode
:
1036 case SpvDecorationFPFastMathMode
:
1037 case SpvDecorationAlignment
:
1038 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
) {
1039 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1040 spirv_decoration_to_string(dec
->decoration
));
1044 case SpvDecorationUserSemantic
:
1045 case SpvDecorationUserTypeGOOGLE
:
1046 /* User semantic decorations can safely be ignored by the driver. */
1050 vtn_fail_with_decoration("Unhandled decoration", dec
->decoration
);
1054 /** Chases the array type all the way down to the tail and rewrites the
1055 * glsl_types to be based off the tail's glsl_type.
1058 vtn_array_type_rewrite_glsl_type(struct vtn_type
*type
)
1060 if (type
->base_type
!= vtn_base_type_array
)
1063 vtn_array_type_rewrite_glsl_type(type
->array_element
);
1065 type
->type
= glsl_array_type(type
->array_element
->type
,
1066 type
->length
, type
->stride
);
1069 /* Matrix strides are handled as a separate pass because we need to know
1070 * whether the matrix is row-major or not first.
1073 struct_member_matrix_stride_cb(struct vtn_builder
*b
,
1074 UNUSED
struct vtn_value
*val
, int member
,
1075 const struct vtn_decoration
*dec
,
1078 if (dec
->decoration
!= SpvDecorationMatrixStride
)
1081 vtn_fail_if(member
< 0,
1082 "The MatrixStride decoration is only allowed on members "
1084 vtn_fail_if(dec
->operands
[0] == 0, "MatrixStride must be non-zero");
1086 struct member_decoration_ctx
*ctx
= void_ctx
;
1088 struct vtn_type
*mat_type
= mutable_matrix_member(b
, ctx
->type
, member
);
1089 if (mat_type
->row_major
) {
1090 mat_type
->array_element
= vtn_type_copy(b
, mat_type
->array_element
);
1091 mat_type
->stride
= mat_type
->array_element
->stride
;
1092 mat_type
->array_element
->stride
= dec
->operands
[0];
1094 mat_type
->type
= glsl_explicit_matrix_type(mat_type
->type
,
1095 dec
->operands
[0], true);
1096 mat_type
->array_element
->type
= glsl_get_column_type(mat_type
->type
);
1098 vtn_assert(mat_type
->array_element
->stride
> 0);
1099 mat_type
->stride
= dec
->operands
[0];
1101 mat_type
->type
= glsl_explicit_matrix_type(mat_type
->type
,
1102 dec
->operands
[0], false);
1105 /* Now that we've replaced the glsl_type with a properly strided matrix
1106 * type, rewrite the member type so that it's an array of the proper kind
1109 vtn_array_type_rewrite_glsl_type(ctx
->type
->members
[member
]);
1110 ctx
->fields
[member
].type
= ctx
->type
->members
[member
]->type
;
1114 struct_block_decoration_cb(struct vtn_builder
*b
,
1115 struct vtn_value
*val
, int member
,
1116 const struct vtn_decoration
*dec
, void *ctx
)
1121 struct vtn_type
*type
= val
->type
;
1122 if (dec
->decoration
== SpvDecorationBlock
)
1124 else if (dec
->decoration
== SpvDecorationBufferBlock
)
1125 type
->buffer_block
= true;
1129 type_decoration_cb(struct vtn_builder
*b
,
1130 struct vtn_value
*val
, int member
,
1131 const struct vtn_decoration
*dec
, UNUSED
void *ctx
)
1133 struct vtn_type
*type
= val
->type
;
1136 /* This should have been handled by OpTypeStruct */
1137 assert(val
->type
->base_type
== vtn_base_type_struct
);
1138 assert(member
>= 0 && member
< val
->type
->length
);
1142 switch (dec
->decoration
) {
1143 case SpvDecorationArrayStride
:
1144 vtn_assert(type
->base_type
== vtn_base_type_array
||
1145 type
->base_type
== vtn_base_type_pointer
);
1147 case SpvDecorationBlock
:
1148 vtn_assert(type
->base_type
== vtn_base_type_struct
);
1149 vtn_assert(type
->block
);
1151 case SpvDecorationBufferBlock
:
1152 vtn_assert(type
->base_type
== vtn_base_type_struct
);
1153 vtn_assert(type
->buffer_block
);
1155 case SpvDecorationGLSLShared
:
1156 case SpvDecorationGLSLPacked
:
1157 /* Ignore these, since we get explicit offsets anyways */
1160 case SpvDecorationRowMajor
:
1161 case SpvDecorationColMajor
:
1162 case SpvDecorationMatrixStride
:
1163 case SpvDecorationBuiltIn
:
1164 case SpvDecorationNoPerspective
:
1165 case SpvDecorationFlat
:
1166 case SpvDecorationPatch
:
1167 case SpvDecorationCentroid
:
1168 case SpvDecorationSample
:
1169 case SpvDecorationExplicitInterpAMD
:
1170 case SpvDecorationVolatile
:
1171 case SpvDecorationCoherent
:
1172 case SpvDecorationNonWritable
:
1173 case SpvDecorationNonReadable
:
1174 case SpvDecorationUniform
:
1175 case SpvDecorationUniformId
:
1176 case SpvDecorationLocation
:
1177 case SpvDecorationComponent
:
1178 case SpvDecorationOffset
:
1179 case SpvDecorationXfbBuffer
:
1180 case SpvDecorationXfbStride
:
1181 case SpvDecorationUserSemantic
:
1182 vtn_warn("Decoration only allowed for struct members: %s",
1183 spirv_decoration_to_string(dec
->decoration
));
1186 case SpvDecorationStream
:
1187 /* We don't need to do anything here, as stream is filled up when
1188 * aplying the decoration to a variable, just check that if it is not a
1189 * struct member, it should be a struct.
1191 vtn_assert(type
->base_type
== vtn_base_type_struct
);
1194 case SpvDecorationRelaxedPrecision
:
1195 case SpvDecorationSpecId
:
1196 case SpvDecorationInvariant
:
1197 case SpvDecorationRestrict
:
1198 case SpvDecorationAliased
:
1199 case SpvDecorationConstant
:
1200 case SpvDecorationIndex
:
1201 case SpvDecorationBinding
:
1202 case SpvDecorationDescriptorSet
:
1203 case SpvDecorationLinkageAttributes
:
1204 case SpvDecorationNoContraction
:
1205 case SpvDecorationInputAttachmentIndex
:
1206 vtn_warn("Decoration not allowed on types: %s",
1207 spirv_decoration_to_string(dec
->decoration
));
1210 case SpvDecorationCPacked
:
1211 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
)
1212 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1213 spirv_decoration_to_string(dec
->decoration
));
1215 type
->packed
= true;
1218 case SpvDecorationSaturatedConversion
:
1219 case SpvDecorationFuncParamAttr
:
1220 case SpvDecorationFPRoundingMode
:
1221 case SpvDecorationFPFastMathMode
:
1222 case SpvDecorationAlignment
:
1223 vtn_warn("Decoration only allowed for CL-style kernels: %s",
1224 spirv_decoration_to_string(dec
->decoration
));
1227 case SpvDecorationUserTypeGOOGLE
:
1228 /* User semantic decorations can safely be ignored by the driver. */
1232 vtn_fail_with_decoration("Unhandled decoration", dec
->decoration
);
1237 translate_image_format(struct vtn_builder
*b
, SpvImageFormat format
)
1240 case SpvImageFormatUnknown
: return PIPE_FORMAT_NONE
;
1241 case SpvImageFormatRgba32f
: return PIPE_FORMAT_R32G32B32A32_FLOAT
;
1242 case SpvImageFormatRgba16f
: return PIPE_FORMAT_R16G16B16A16_FLOAT
;
1243 case SpvImageFormatR32f
: return PIPE_FORMAT_R32_FLOAT
;
1244 case SpvImageFormatRgba8
: return PIPE_FORMAT_R8G8B8A8_UNORM
;
1245 case SpvImageFormatRgba8Snorm
: return PIPE_FORMAT_R8G8B8A8_SNORM
;
1246 case SpvImageFormatRg32f
: return PIPE_FORMAT_R32G32_FLOAT
;
1247 case SpvImageFormatRg16f
: return PIPE_FORMAT_R16G16_FLOAT
;
1248 case SpvImageFormatR11fG11fB10f
: return PIPE_FORMAT_R11G11B10_FLOAT
;
1249 case SpvImageFormatR16f
: return PIPE_FORMAT_R16_FLOAT
;
1250 case SpvImageFormatRgba16
: return PIPE_FORMAT_R16G16B16A16_UNORM
;
1251 case SpvImageFormatRgb10A2
: return PIPE_FORMAT_R10G10B10A2_UNORM
;
1252 case SpvImageFormatRg16
: return PIPE_FORMAT_R16G16_UNORM
;
1253 case SpvImageFormatRg8
: return PIPE_FORMAT_R8G8_UNORM
;
1254 case SpvImageFormatR16
: return PIPE_FORMAT_R16_UNORM
;
1255 case SpvImageFormatR8
: return PIPE_FORMAT_R8_UNORM
;
1256 case SpvImageFormatRgba16Snorm
: return PIPE_FORMAT_R16G16B16A16_SNORM
;
1257 case SpvImageFormatRg16Snorm
: return PIPE_FORMAT_R16G16_SNORM
;
1258 case SpvImageFormatRg8Snorm
: return PIPE_FORMAT_R8G8_SNORM
;
1259 case SpvImageFormatR16Snorm
: return PIPE_FORMAT_R16_SNORM
;
1260 case SpvImageFormatR8Snorm
: return PIPE_FORMAT_R8_SNORM
;
1261 case SpvImageFormatRgba32i
: return PIPE_FORMAT_R32G32B32A32_SINT
;
1262 case SpvImageFormatRgba16i
: return PIPE_FORMAT_R16G16B16A16_SINT
;
1263 case SpvImageFormatRgba8i
: return PIPE_FORMAT_R8G8B8A8_SINT
;
1264 case SpvImageFormatR32i
: return PIPE_FORMAT_R32_SINT
;
1265 case SpvImageFormatRg32i
: return PIPE_FORMAT_R32G32_SINT
;
1266 case SpvImageFormatRg16i
: return PIPE_FORMAT_R16G16_SINT
;
1267 case SpvImageFormatRg8i
: return PIPE_FORMAT_R8G8_SINT
;
1268 case SpvImageFormatR16i
: return PIPE_FORMAT_R16_SINT
;
1269 case SpvImageFormatR8i
: return PIPE_FORMAT_R8_SINT
;
1270 case SpvImageFormatRgba32ui
: return PIPE_FORMAT_R32G32B32A32_UINT
;
1271 case SpvImageFormatRgba16ui
: return PIPE_FORMAT_R16G16B16A16_UINT
;
1272 case SpvImageFormatRgba8ui
: return PIPE_FORMAT_R8G8B8A8_UINT
;
1273 case SpvImageFormatR32ui
: return PIPE_FORMAT_R32_UINT
;
1274 case SpvImageFormatRgb10a2ui
: return PIPE_FORMAT_R10G10B10A2_UINT
;
1275 case SpvImageFormatRg32ui
: return PIPE_FORMAT_R32G32_UINT
;
1276 case SpvImageFormatRg16ui
: return PIPE_FORMAT_R16G16_UINT
;
1277 case SpvImageFormatRg8ui
: return PIPE_FORMAT_R8G8_UINT
;
1278 case SpvImageFormatR16ui
: return PIPE_FORMAT_R16_UINT
;
1279 case SpvImageFormatR8ui
: return PIPE_FORMAT_R8_UINT
;
1281 vtn_fail("Invalid image format: %s (%u)",
1282 spirv_imageformat_to_string(format
), format
);
1287 vtn_handle_type(struct vtn_builder
*b
, SpvOp opcode
,
1288 const uint32_t *w
, unsigned count
)
1290 struct vtn_value
*val
= NULL
;
1292 /* In order to properly handle forward declarations, we have to defer
1293 * allocation for pointer types.
1295 if (opcode
!= SpvOpTypePointer
&& opcode
!= SpvOpTypeForwardPointer
) {
1296 val
= vtn_push_value(b
, w
[1], vtn_value_type_type
);
1297 vtn_fail_if(val
->type
!= NULL
,
1298 "Only pointers can have forward declarations");
1299 val
->type
= rzalloc(b
, struct vtn_type
);
1300 val
->type
->id
= w
[1];
1305 val
->type
->base_type
= vtn_base_type_void
;
1306 val
->type
->type
= glsl_void_type();
1309 val
->type
->base_type
= vtn_base_type_scalar
;
1310 val
->type
->type
= glsl_bool_type();
1311 val
->type
->length
= 1;
1313 case SpvOpTypeInt
: {
1314 int bit_size
= w
[2];
1315 const bool signedness
= w
[3];
1316 val
->type
->base_type
= vtn_base_type_scalar
;
1319 val
->type
->type
= (signedness
? glsl_int64_t_type() : glsl_uint64_t_type());
1322 val
->type
->type
= (signedness
? glsl_int_type() : glsl_uint_type());
1325 val
->type
->type
= (signedness
? glsl_int16_t_type() : glsl_uint16_t_type());
1328 val
->type
->type
= (signedness
? glsl_int8_t_type() : glsl_uint8_t_type());
1331 vtn_fail("Invalid int bit size: %u", bit_size
);
1333 val
->type
->length
= 1;
1337 case SpvOpTypeFloat
: {
1338 int bit_size
= w
[2];
1339 val
->type
->base_type
= vtn_base_type_scalar
;
1342 val
->type
->type
= glsl_float16_t_type();
1345 val
->type
->type
= glsl_float_type();
1348 val
->type
->type
= glsl_double_type();
1351 vtn_fail("Invalid float bit size: %u", bit_size
);
1353 val
->type
->length
= 1;
1357 case SpvOpTypeVector
: {
1358 struct vtn_type
*base
= vtn_get_type(b
, w
[2]);
1359 unsigned elems
= w
[3];
1361 vtn_fail_if(base
->base_type
!= vtn_base_type_scalar
,
1362 "Base type for OpTypeVector must be a scalar");
1363 vtn_fail_if((elems
< 2 || elems
> 4) && (elems
!= 8) && (elems
!= 16),
1364 "Invalid component count for OpTypeVector");
1366 val
->type
->base_type
= vtn_base_type_vector
;
1367 val
->type
->type
= glsl_vector_type(glsl_get_base_type(base
->type
), elems
);
1368 val
->type
->length
= elems
;
1369 val
->type
->stride
= glsl_type_is_boolean(val
->type
->type
)
1370 ? 4 : glsl_get_bit_size(base
->type
) / 8;
1371 val
->type
->array_element
= base
;
1375 case SpvOpTypeMatrix
: {
1376 struct vtn_type
*base
= vtn_get_type(b
, w
[2]);
1377 unsigned columns
= w
[3];
1379 vtn_fail_if(base
->base_type
!= vtn_base_type_vector
,
1380 "Base type for OpTypeMatrix must be a vector");
1381 vtn_fail_if(columns
< 2 || columns
> 4,
1382 "Invalid column count for OpTypeMatrix");
1384 val
->type
->base_type
= vtn_base_type_matrix
;
1385 val
->type
->type
= glsl_matrix_type(glsl_get_base_type(base
->type
),
1386 glsl_get_vector_elements(base
->type
),
1388 vtn_fail_if(glsl_type_is_error(val
->type
->type
),
1389 "Unsupported base type for OpTypeMatrix");
1390 assert(!glsl_type_is_error(val
->type
->type
));
1391 val
->type
->length
= columns
;
1392 val
->type
->array_element
= base
;
1393 val
->type
->row_major
= false;
1394 val
->type
->stride
= 0;
1398 case SpvOpTypeRuntimeArray
:
1399 case SpvOpTypeArray
: {
1400 struct vtn_type
*array_element
= vtn_get_type(b
, w
[2]);
1402 if (opcode
== SpvOpTypeRuntimeArray
) {
1403 /* A length of 0 is used to denote unsized arrays */
1404 val
->type
->length
= 0;
1406 val
->type
->length
= vtn_constant_uint(b
, w
[3]);
1409 val
->type
->base_type
= vtn_base_type_array
;
1410 val
->type
->array_element
= array_element
;
1411 if (b
->shader
->info
.stage
== MESA_SHADER_KERNEL
)
1412 val
->type
->stride
= glsl_get_cl_size(array_element
->type
);
1414 vtn_foreach_decoration(b
, val
, array_stride_decoration_cb
, NULL
);
1415 val
->type
->type
= glsl_array_type(array_element
->type
, val
->type
->length
,
1420 case SpvOpTypeStruct
: {
1421 unsigned num_fields
= count
- 2;
1422 val
->type
->base_type
= vtn_base_type_struct
;
1423 val
->type
->length
= num_fields
;
1424 val
->type
->members
= ralloc_array(b
, struct vtn_type
*, num_fields
);
1425 val
->type
->offsets
= ralloc_array(b
, unsigned, num_fields
);
1426 val
->type
->packed
= false;
1428 NIR_VLA(struct glsl_struct_field
, fields
, count
);
1429 for (unsigned i
= 0; i
< num_fields
; i
++) {
1430 val
->type
->members
[i
] = vtn_get_type(b
, w
[i
+ 2]);
1431 fields
[i
] = (struct glsl_struct_field
) {
1432 .type
= val
->type
->members
[i
]->type
,
1433 .name
= ralloc_asprintf(b
, "field%d", i
),
1439 if (b
->shader
->info
.stage
== MESA_SHADER_KERNEL
) {
1440 unsigned offset
= 0;
1441 for (unsigned i
= 0; i
< num_fields
; i
++) {
1442 offset
= align(offset
, glsl_get_cl_alignment(fields
[i
].type
));
1443 fields
[i
].offset
= offset
;
1444 offset
+= glsl_get_cl_size(fields
[i
].type
);
1448 struct member_decoration_ctx ctx
= {
1449 .num_fields
= num_fields
,
1454 vtn_foreach_decoration(b
, val
, struct_member_decoration_cb
, &ctx
);
1455 vtn_foreach_decoration(b
, val
, struct_member_matrix_stride_cb
, &ctx
);
1457 vtn_foreach_decoration(b
, val
, struct_block_decoration_cb
, NULL
);
1459 const char *name
= val
->name
;
1461 if (val
->type
->block
|| val
->type
->buffer_block
) {
1462 /* Packing will be ignored since types coming from SPIR-V are
1463 * explicitly laid out.
1465 val
->type
->type
= glsl_interface_type(fields
, num_fields
,
1466 /* packing */ 0, false,
1467 name
? name
: "block");
1469 val
->type
->type
= glsl_struct_type(fields
, num_fields
,
1470 name
? name
: "struct", false);
1475 case SpvOpTypeFunction
: {
1476 val
->type
->base_type
= vtn_base_type_function
;
1477 val
->type
->type
= NULL
;
1479 val
->type
->return_type
= vtn_get_type(b
, w
[2]);
1481 const unsigned num_params
= count
- 3;
1482 val
->type
->length
= num_params
;
1483 val
->type
->params
= ralloc_array(b
, struct vtn_type
*, num_params
);
1484 for (unsigned i
= 0; i
< count
- 3; i
++) {
1485 val
->type
->params
[i
] = vtn_get_type(b
, w
[i
+ 3]);
1490 case SpvOpTypePointer
:
1491 case SpvOpTypeForwardPointer
: {
1492 /* We can't blindly push the value because it might be a forward
1495 val
= vtn_untyped_value(b
, w
[1]);
1497 SpvStorageClass storage_class
= w
[2];
1499 if (val
->value_type
== vtn_value_type_invalid
) {
1500 val
->value_type
= vtn_value_type_type
;
1501 val
->type
= rzalloc(b
, struct vtn_type
);
1502 val
->type
->id
= w
[1];
1503 val
->type
->base_type
= vtn_base_type_pointer
;
1504 val
->type
->storage_class
= storage_class
;
1506 /* These can actually be stored to nir_variables and used as SSA
1507 * values so they need a real glsl_type.
1509 enum vtn_variable_mode mode
= vtn_storage_class_to_mode(
1510 b
, storage_class
, NULL
, NULL
);
1511 val
->type
->type
= nir_address_format_to_glsl_type(
1512 vtn_mode_to_address_format(b
, mode
));
1514 vtn_fail_if(val
->type
->storage_class
!= storage_class
,
1515 "The storage classes of an OpTypePointer and any "
1516 "OpTypeForwardPointers that provide forward "
1517 "declarations of it must match.");
1520 if (opcode
== SpvOpTypePointer
) {
1521 vtn_fail_if(val
->type
->deref
!= NULL
,
1522 "While OpTypeForwardPointer can be used to provide a "
1523 "forward declaration of a pointer, OpTypePointer can "
1524 "only be used once for a given id.");
1526 val
->type
->deref
= vtn_get_type(b
, w
[3]);
1528 /* Only certain storage classes use ArrayStride. The others (in
1529 * particular Workgroup) are expected to be laid out by the driver.
1531 switch (storage_class
) {
1532 case SpvStorageClassUniform
:
1533 case SpvStorageClassPushConstant
:
1534 case SpvStorageClassStorageBuffer
:
1535 case SpvStorageClassPhysicalStorageBuffer
:
1536 vtn_foreach_decoration(b
, val
, array_stride_decoration_cb
, NULL
);
1539 /* Nothing to do. */
1543 if (b
->physical_ptrs
) {
1544 switch (storage_class
) {
1545 case SpvStorageClassFunction
:
1546 case SpvStorageClassWorkgroup
:
1547 case SpvStorageClassCrossWorkgroup
:
1548 case SpvStorageClassUniformConstant
:
1549 val
->type
->stride
= align(glsl_get_cl_size(val
->type
->deref
->type
),
1550 glsl_get_cl_alignment(val
->type
->deref
->type
));
1560 case SpvOpTypeImage
: {
1561 val
->type
->base_type
= vtn_base_type_image
;
1563 /* Images are represented in NIR as a scalar SSA value that is the
1564 * result of a deref instruction. An OpLoad on an OpTypeImage pointer
1565 * from UniformConstant memory just takes the NIR deref from the pointer
1566 * and turns it into an SSA value.
1568 val
->type
->type
= nir_address_format_to_glsl_type(
1569 vtn_mode_to_address_format(b
, vtn_variable_mode_function
));
1571 const struct vtn_type
*sampled_type
= vtn_get_type(b
, w
[2]);
1572 if (b
->shader
->info
.stage
== MESA_SHADER_KERNEL
) {
1573 vtn_fail_if(sampled_type
->base_type
!= vtn_base_type_void
,
1574 "Sampled type of OpTypeImage must be void for kernels");
1576 vtn_fail_if(sampled_type
->base_type
!= vtn_base_type_scalar
||
1577 glsl_get_bit_size(sampled_type
->type
) != 32,
1578 "Sampled type of OpTypeImage must be a 32-bit scalar");
1581 enum glsl_sampler_dim dim
;
1582 switch ((SpvDim
)w
[3]) {
1583 case SpvDim1D
: dim
= GLSL_SAMPLER_DIM_1D
; break;
1584 case SpvDim2D
: dim
= GLSL_SAMPLER_DIM_2D
; break;
1585 case SpvDim3D
: dim
= GLSL_SAMPLER_DIM_3D
; break;
1586 case SpvDimCube
: dim
= GLSL_SAMPLER_DIM_CUBE
; break;
1587 case SpvDimRect
: dim
= GLSL_SAMPLER_DIM_RECT
; break;
1588 case SpvDimBuffer
: dim
= GLSL_SAMPLER_DIM_BUF
; break;
1589 case SpvDimSubpassData
: dim
= GLSL_SAMPLER_DIM_SUBPASS
; break;
1591 vtn_fail("Invalid SPIR-V image dimensionality: %s (%u)",
1592 spirv_dim_to_string((SpvDim
)w
[3]), w
[3]);
1595 /* w[4]: as per Vulkan spec "Validation Rules within a Module",
1596 * The “Depth” operand of OpTypeImage is ignored.
1598 bool is_array
= w
[5];
1599 bool multisampled
= w
[6];
1600 unsigned sampled
= w
[7];
1601 SpvImageFormat format
= w
[8];
1604 val
->type
->access_qualifier
= w
[9];
1605 else if (b
->shader
->info
.stage
== MESA_SHADER_KERNEL
)
1606 /* Per the CL C spec: If no qualifier is provided, read_only is assumed. */
1607 val
->type
->access_qualifier
= SpvAccessQualifierReadOnly
;
1609 val
->type
->access_qualifier
= SpvAccessQualifierReadWrite
;
1612 if (dim
== GLSL_SAMPLER_DIM_2D
)
1613 dim
= GLSL_SAMPLER_DIM_MS
;
1614 else if (dim
== GLSL_SAMPLER_DIM_SUBPASS
)
1615 dim
= GLSL_SAMPLER_DIM_SUBPASS_MS
;
1617 vtn_fail("Unsupported multisampled image type");
1620 val
->type
->image_format
= translate_image_format(b
, format
);
1622 enum glsl_base_type sampled_base_type
=
1623 glsl_get_base_type(sampled_type
->type
);
1625 val
->type
->glsl_image
= glsl_sampler_type(dim
, false, is_array
,
1627 } else if (sampled
== 2) {
1628 val
->type
->glsl_image
= glsl_image_type(dim
, is_array
,
1630 } else if (b
->shader
->info
.stage
== MESA_SHADER_KERNEL
) {
1631 val
->type
->glsl_image
= glsl_image_type(dim
, is_array
,
1634 vtn_fail("We need to know if the image will be sampled");
1639 case SpvOpTypeSampledImage
: {
1640 val
->type
->base_type
= vtn_base_type_sampled_image
;
1641 val
->type
->image
= vtn_get_type(b
, w
[2]);
1643 /* Sampled images are represented NIR as a vec2 SSA value where each
1644 * component is the result of a deref instruction. The first component
1645 * is the image and the second is the sampler. An OpLoad on an
1646 * OpTypeSampledImage pointer from UniformConstant memory just takes
1647 * the NIR deref from the pointer and duplicates it to both vector
1650 nir_address_format addr_format
=
1651 vtn_mode_to_address_format(b
, vtn_variable_mode_function
);
1652 assert(nir_address_format_num_components(addr_format
) == 1);
1653 unsigned bit_size
= nir_address_format_bit_size(addr_format
);
1654 assert(bit_size
== 32 || bit_size
== 64);
1656 enum glsl_base_type base_type
=
1657 bit_size
== 32 ? GLSL_TYPE_UINT
: GLSL_TYPE_UINT64
;
1658 val
->type
->type
= glsl_vector_type(base_type
, 2);
1662 case SpvOpTypeSampler
:
1663 val
->type
->base_type
= vtn_base_type_sampler
;
1665 /* Samplers are represented in NIR as a scalar SSA value that is the
1666 * result of a deref instruction. An OpLoad on an OpTypeSampler pointer
1667 * from UniformConstant memory just takes the NIR deref from the pointer
1668 * and turns it into an SSA value.
1670 val
->type
->type
= nir_address_format_to_glsl_type(
1671 vtn_mode_to_address_format(b
, vtn_variable_mode_function
));
1674 case SpvOpTypeOpaque
:
1675 case SpvOpTypeEvent
:
1676 case SpvOpTypeDeviceEvent
:
1677 case SpvOpTypeReserveId
:
1678 case SpvOpTypeQueue
:
1681 vtn_fail_with_opcode("Unhandled opcode", opcode
);
1684 vtn_foreach_decoration(b
, val
, type_decoration_cb
, NULL
);
1686 if (val
->type
->base_type
== vtn_base_type_struct
&&
1687 (val
->type
->block
|| val
->type
->buffer_block
)) {
1688 for (unsigned i
= 0; i
< val
->type
->length
; i
++) {
1689 vtn_fail_if(vtn_type_contains_block(b
, val
->type
->members
[i
]),
1690 "Block and BufferBlock decorations cannot decorate a "
1691 "structure type that is nested at any level inside "
1692 "another structure type decorated with Block or "
1698 static nir_constant
*
1699 vtn_null_constant(struct vtn_builder
*b
, struct vtn_type
*type
)
1701 nir_constant
*c
= rzalloc(b
, nir_constant
);
1703 switch (type
->base_type
) {
1704 case vtn_base_type_scalar
:
1705 case vtn_base_type_vector
:
1706 /* Nothing to do here. It's already initialized to zero */
1709 case vtn_base_type_pointer
: {
1710 enum vtn_variable_mode mode
= vtn_storage_class_to_mode(
1711 b
, type
->storage_class
, type
->deref
, NULL
);
1712 nir_address_format addr_format
= vtn_mode_to_address_format(b
, mode
);
1714 const nir_const_value
*null_value
= nir_address_format_null_value(addr_format
);
1715 memcpy(c
->values
, null_value
,
1716 sizeof(nir_const_value
) * nir_address_format_num_components(addr_format
));
1720 case vtn_base_type_void
:
1721 case vtn_base_type_image
:
1722 case vtn_base_type_sampler
:
1723 case vtn_base_type_sampled_image
:
1724 case vtn_base_type_function
:
1725 /* For those we have to return something but it doesn't matter what. */
1728 case vtn_base_type_matrix
:
1729 case vtn_base_type_array
:
1730 vtn_assert(type
->length
> 0);
1731 c
->num_elements
= type
->length
;
1732 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
1734 c
->elements
[0] = vtn_null_constant(b
, type
->array_element
);
1735 for (unsigned i
= 1; i
< c
->num_elements
; i
++)
1736 c
->elements
[i
] = c
->elements
[0];
1739 case vtn_base_type_struct
:
1740 c
->num_elements
= type
->length
;
1741 c
->elements
= ralloc_array(b
, nir_constant
*, c
->num_elements
);
1742 for (unsigned i
= 0; i
< c
->num_elements
; i
++)
1743 c
->elements
[i
] = vtn_null_constant(b
, type
->members
[i
]);
1747 vtn_fail("Invalid type for null constant");
1754 spec_constant_decoration_cb(struct vtn_builder
*b
, UNUSED
struct vtn_value
*val
,
1755 ASSERTED
int member
,
1756 const struct vtn_decoration
*dec
, void *data
)
1758 vtn_assert(member
== -1);
1759 if (dec
->decoration
!= SpvDecorationSpecId
)
1762 nir_const_value
*value
= data
;
1763 for (unsigned i
= 0; i
< b
->num_specializations
; i
++) {
1764 if (b
->specializations
[i
].id
== dec
->operands
[0]) {
1765 *value
= b
->specializations
[i
].value
;
1772 handle_workgroup_size_decoration_cb(struct vtn_builder
*b
,
1773 struct vtn_value
*val
,
1774 ASSERTED
int member
,
1775 const struct vtn_decoration
*dec
,
1778 vtn_assert(member
== -1);
1779 if (dec
->decoration
!= SpvDecorationBuiltIn
||
1780 dec
->operands
[0] != SpvBuiltInWorkgroupSize
)
1783 vtn_assert(val
->type
->type
== glsl_vector_type(GLSL_TYPE_UINT
, 3));
1784 b
->workgroup_size_builtin
= val
;
1788 vtn_handle_constant(struct vtn_builder
*b
, SpvOp opcode
,
1789 const uint32_t *w
, unsigned count
)
1791 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_constant
);
1792 val
->constant
= rzalloc(b
, nir_constant
);
1794 case SpvOpConstantTrue
:
1795 case SpvOpConstantFalse
:
1796 case SpvOpSpecConstantTrue
:
1797 case SpvOpSpecConstantFalse
: {
1798 vtn_fail_if(val
->type
->type
!= glsl_bool_type(),
1799 "Result type of %s must be OpTypeBool",
1800 spirv_op_to_string(opcode
));
1802 bool bval
= (opcode
== SpvOpConstantTrue
||
1803 opcode
== SpvOpSpecConstantTrue
);
1805 nir_const_value u32val
= nir_const_value_for_uint(bval
, 32);
1807 if (opcode
== SpvOpSpecConstantTrue
||
1808 opcode
== SpvOpSpecConstantFalse
)
1809 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &u32val
);
1811 val
->constant
->values
[0].b
= u32val
.u32
!= 0;
1816 case SpvOpSpecConstant
: {
1817 vtn_fail_if(val
->type
->base_type
!= vtn_base_type_scalar
,
1818 "Result type of %s must be a scalar",
1819 spirv_op_to_string(opcode
));
1820 int bit_size
= glsl_get_bit_size(val
->type
->type
);
1823 val
->constant
->values
[0].u64
= vtn_u64_literal(&w
[3]);
1826 val
->constant
->values
[0].u32
= w
[3];
1829 val
->constant
->values
[0].u16
= w
[3];
1832 val
->constant
->values
[0].u8
= w
[3];
1835 vtn_fail("Unsupported SpvOpConstant bit size: %u", bit_size
);
1838 if (opcode
== SpvOpSpecConstant
)
1839 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
,
1840 &val
->constant
->values
[0]);
1844 case SpvOpSpecConstantComposite
:
1845 case SpvOpConstantComposite
: {
1846 unsigned elem_count
= count
- 3;
1847 vtn_fail_if(elem_count
!= val
->type
->length
,
1848 "%s has %u constituents, expected %u",
1849 spirv_op_to_string(opcode
), elem_count
, val
->type
->length
);
1851 nir_constant
**elems
= ralloc_array(b
, nir_constant
*, elem_count
);
1852 for (unsigned i
= 0; i
< elem_count
; i
++) {
1853 struct vtn_value
*val
= vtn_untyped_value(b
, w
[i
+ 3]);
1855 if (val
->value_type
== vtn_value_type_constant
) {
1856 elems
[i
] = val
->constant
;
1858 vtn_fail_if(val
->value_type
!= vtn_value_type_undef
,
1859 "only constants or undefs allowed for "
1860 "SpvOpConstantComposite");
1861 /* to make it easier, just insert a NULL constant for now */
1862 elems
[i
] = vtn_null_constant(b
, val
->type
);
1866 switch (val
->type
->base_type
) {
1867 case vtn_base_type_vector
: {
1868 assert(glsl_type_is_vector(val
->type
->type
));
1869 for (unsigned i
= 0; i
< elem_count
; i
++)
1870 val
->constant
->values
[i
] = elems
[i
]->values
[0];
1874 case vtn_base_type_matrix
:
1875 case vtn_base_type_struct
:
1876 case vtn_base_type_array
:
1877 ralloc_steal(val
->constant
, elems
);
1878 val
->constant
->num_elements
= elem_count
;
1879 val
->constant
->elements
= elems
;
1883 vtn_fail("Result type of %s must be a composite type",
1884 spirv_op_to_string(opcode
));
1889 case SpvOpSpecConstantOp
: {
1890 nir_const_value u32op
= nir_const_value_for_uint(w
[3], 32);
1891 vtn_foreach_decoration(b
, val
, spec_constant_decoration_cb
, &u32op
);
1892 SpvOp opcode
= u32op
.u32
;
1894 case SpvOpVectorShuffle
: {
1895 struct vtn_value
*v0
= &b
->values
[w
[4]];
1896 struct vtn_value
*v1
= &b
->values
[w
[5]];
1898 vtn_assert(v0
->value_type
== vtn_value_type_constant
||
1899 v0
->value_type
== vtn_value_type_undef
);
1900 vtn_assert(v1
->value_type
== vtn_value_type_constant
||
1901 v1
->value_type
== vtn_value_type_undef
);
1903 unsigned len0
= glsl_get_vector_elements(v0
->type
->type
);
1904 unsigned len1
= glsl_get_vector_elements(v1
->type
->type
);
1906 vtn_assert(len0
+ len1
< 16);
1908 unsigned bit_size
= glsl_get_bit_size(val
->type
->type
);
1909 unsigned bit_size0
= glsl_get_bit_size(v0
->type
->type
);
1910 unsigned bit_size1
= glsl_get_bit_size(v1
->type
->type
);
1912 vtn_assert(bit_size
== bit_size0
&& bit_size
== bit_size1
);
1913 (void)bit_size0
; (void)bit_size1
;
1915 nir_const_value undef
= { .u64
= 0xdeadbeefdeadbeef };
1916 nir_const_value combined
[NIR_MAX_VEC_COMPONENTS
* 2];
1918 if (v0
->value_type
== vtn_value_type_constant
) {
1919 for (unsigned i
= 0; i
< len0
; i
++)
1920 combined
[i
] = v0
->constant
->values
[i
];
1922 if (v1
->value_type
== vtn_value_type_constant
) {
1923 for (unsigned i
= 0; i
< len1
; i
++)
1924 combined
[len0
+ i
] = v1
->constant
->values
[i
];
1927 for (unsigned i
= 0, j
= 0; i
< count
- 6; i
++, j
++) {
1928 uint32_t comp
= w
[i
+ 6];
1929 if (comp
== (uint32_t)-1) {
1930 /* If component is not used, set the value to a known constant
1931 * to detect if it is wrongly used.
1933 val
->constant
->values
[j
] = undef
;
1935 vtn_fail_if(comp
>= len0
+ len1
,
1936 "All Component literals must either be FFFFFFFF "
1937 "or in [0, N - 1] (inclusive).");
1938 val
->constant
->values
[j
] = combined
[comp
];
1944 case SpvOpCompositeExtract
:
1945 case SpvOpCompositeInsert
: {
1946 struct vtn_value
*comp
;
1947 unsigned deref_start
;
1948 struct nir_constant
**c
;
1949 if (opcode
== SpvOpCompositeExtract
) {
1950 comp
= vtn_value(b
, w
[4], vtn_value_type_constant
);
1952 c
= &comp
->constant
;
1954 comp
= vtn_value(b
, w
[5], vtn_value_type_constant
);
1956 val
->constant
= nir_constant_clone(comp
->constant
,
1962 const struct vtn_type
*type
= comp
->type
;
1963 for (unsigned i
= deref_start
; i
< count
; i
++) {
1964 vtn_fail_if(w
[i
] > type
->length
,
1965 "%uth index of %s is %u but the type has only "
1966 "%u elements", i
- deref_start
,
1967 spirv_op_to_string(opcode
), w
[i
], type
->length
);
1969 switch (type
->base_type
) {
1970 case vtn_base_type_vector
:
1972 type
= type
->array_element
;
1975 case vtn_base_type_matrix
:
1976 case vtn_base_type_array
:
1977 c
= &(*c
)->elements
[w
[i
]];
1978 type
= type
->array_element
;
1981 case vtn_base_type_struct
:
1982 c
= &(*c
)->elements
[w
[i
]];
1983 type
= type
->members
[w
[i
]];
1987 vtn_fail("%s must only index into composite types",
1988 spirv_op_to_string(opcode
));
1992 if (opcode
== SpvOpCompositeExtract
) {
1996 unsigned num_components
= type
->length
;
1997 for (unsigned i
= 0; i
< num_components
; i
++)
1998 val
->constant
->values
[i
] = (*c
)->values
[elem
+ i
];
2001 struct vtn_value
*insert
=
2002 vtn_value(b
, w
[4], vtn_value_type_constant
);
2003 vtn_assert(insert
->type
== type
);
2005 *c
= insert
->constant
;
2007 unsigned num_components
= type
->length
;
2008 for (unsigned i
= 0; i
< num_components
; i
++)
2009 (*c
)->values
[elem
+ i
] = insert
->constant
->values
[i
];
2017 nir_alu_type dst_alu_type
= nir_get_nir_type_for_glsl_type(val
->type
->type
);
2018 nir_alu_type src_alu_type
= dst_alu_type
;
2019 unsigned num_components
= glsl_get_vector_elements(val
->type
->type
);
2022 vtn_assert(count
<= 7);
2028 /* We have a source in a conversion */
2030 nir_get_nir_type_for_glsl_type(vtn_get_value_type(b
, w
[4])->type
);
2031 /* We use the bitsize of the conversion source to evaluate the opcode later */
2032 bit_size
= glsl_get_bit_size(vtn_get_value_type(b
, w
[4])->type
);
2035 bit_size
= glsl_get_bit_size(val
->type
->type
);
2038 nir_op op
= vtn_nir_alu_op_for_spirv_opcode(b
, opcode
, &swap
,
2039 nir_alu_type_get_type_size(src_alu_type
),
2040 nir_alu_type_get_type_size(dst_alu_type
));
2041 nir_const_value src
[3][NIR_MAX_VEC_COMPONENTS
];
2043 for (unsigned i
= 0; i
< count
- 4; i
++) {
2044 struct vtn_value
*src_val
=
2045 vtn_value(b
, w
[4 + i
], vtn_value_type_constant
);
2047 /* If this is an unsized source, pull the bit size from the
2048 * source; otherwise, we'll use the bit size from the destination.
2050 if (!nir_alu_type_get_type_size(nir_op_infos
[op
].input_types
[i
]))
2051 bit_size
= glsl_get_bit_size(src_val
->type
->type
);
2053 unsigned src_comps
= nir_op_infos
[op
].input_sizes
[i
] ?
2054 nir_op_infos
[op
].input_sizes
[i
] :
2057 unsigned j
= swap
? 1 - i
: i
;
2058 for (unsigned c
= 0; c
< src_comps
; c
++)
2059 src
[j
][c
] = src_val
->constant
->values
[c
];
2062 /* fix up fixed size sources */
2069 for (unsigned i
= 0; i
< num_components
; ++i
) {
2071 case 64: src
[1][i
].u32
= src
[1][i
].u64
; break;
2072 case 16: src
[1][i
].u32
= src
[1][i
].u16
; break;
2073 case 8: src
[1][i
].u32
= src
[1][i
].u8
; break;
2082 nir_const_value
*srcs
[3] = {
2083 src
[0], src
[1], src
[2],
2085 nir_eval_const_opcode(op
, val
->constant
->values
,
2086 num_components
, bit_size
, srcs
,
2087 b
->shader
->info
.float_controls_execution_mode
);
2094 case SpvOpConstantNull
:
2095 val
->constant
= vtn_null_constant(b
, val
->type
);
2098 case SpvOpConstantSampler
:
2099 vtn_fail("OpConstantSampler requires Kernel Capability");
2103 vtn_fail_with_opcode("Unhandled opcode", opcode
);
2106 /* Now that we have the value, update the workgroup size if needed */
2107 vtn_foreach_decoration(b
, val
, handle_workgroup_size_decoration_cb
, NULL
);
2110 SpvMemorySemanticsMask
2111 vtn_storage_class_to_memory_semantics(SpvStorageClass sc
)
2114 case SpvStorageClassStorageBuffer
:
2115 case SpvStorageClassPhysicalStorageBuffer
:
2116 return SpvMemorySemanticsUniformMemoryMask
;
2117 case SpvStorageClassWorkgroup
:
2118 return SpvMemorySemanticsWorkgroupMemoryMask
;
2120 return SpvMemorySemanticsMaskNone
;
2125 vtn_split_barrier_semantics(struct vtn_builder
*b
,
2126 SpvMemorySemanticsMask semantics
,
2127 SpvMemorySemanticsMask
*before
,
2128 SpvMemorySemanticsMask
*after
)
2130 /* For memory semantics embedded in operations, we split them into up to
2131 * two barriers, to be added before and after the operation. This is less
2132 * strict than if we propagated until the final backend stage, but still
2133 * result in correct execution.
2135 * A further improvement could be pipe this information (and use!) into the
2136 * next compiler layers, at the expense of making the handling of barriers
2140 *before
= SpvMemorySemanticsMaskNone
;
2141 *after
= SpvMemorySemanticsMaskNone
;
2143 SpvMemorySemanticsMask order_semantics
=
2144 semantics
& (SpvMemorySemanticsAcquireMask
|
2145 SpvMemorySemanticsReleaseMask
|
2146 SpvMemorySemanticsAcquireReleaseMask
|
2147 SpvMemorySemanticsSequentiallyConsistentMask
);
2149 if (util_bitcount(order_semantics
) > 1) {
2150 /* Old GLSLang versions incorrectly set all the ordering bits. This was
2151 * fixed in c51287d744fb6e7e9ccc09f6f8451e6c64b1dad6 of glslang repo,
2152 * and it is in GLSLang since revision "SPIRV99.1321" (from Jul-2016).
2154 vtn_warn("Multiple memory ordering semantics specified, "
2155 "assuming AcquireRelease.");
2156 order_semantics
= SpvMemorySemanticsAcquireReleaseMask
;
2159 const SpvMemorySemanticsMask av_vis_semantics
=
2160 semantics
& (SpvMemorySemanticsMakeAvailableMask
|
2161 SpvMemorySemanticsMakeVisibleMask
);
2163 const SpvMemorySemanticsMask storage_semantics
=
2164 semantics
& (SpvMemorySemanticsUniformMemoryMask
|
2165 SpvMemorySemanticsSubgroupMemoryMask
|
2166 SpvMemorySemanticsWorkgroupMemoryMask
|
2167 SpvMemorySemanticsCrossWorkgroupMemoryMask
|
2168 SpvMemorySemanticsAtomicCounterMemoryMask
|
2169 SpvMemorySemanticsImageMemoryMask
|
2170 SpvMemorySemanticsOutputMemoryMask
);
2172 const SpvMemorySemanticsMask other_semantics
=
2173 semantics
& ~(order_semantics
| av_vis_semantics
| storage_semantics
);
2175 if (other_semantics
)
2176 vtn_warn("Ignoring unhandled memory semantics: %u\n", other_semantics
);
2178 /* SequentiallyConsistent is treated as AcquireRelease. */
2180 /* The RELEASE barrier happens BEFORE the operation, and it is usually
2181 * associated with a Store. All the write operations with a matching
2182 * semantics will not be reordered after the Store.
2184 if (order_semantics
& (SpvMemorySemanticsReleaseMask
|
2185 SpvMemorySemanticsAcquireReleaseMask
|
2186 SpvMemorySemanticsSequentiallyConsistentMask
)) {
2187 *before
|= SpvMemorySemanticsReleaseMask
| storage_semantics
;
2190 /* The ACQUIRE barrier happens AFTER the operation, and it is usually
2191 * associated with a Load. All the operations with a matching semantics
2192 * will not be reordered before the Load.
2194 if (order_semantics
& (SpvMemorySemanticsAcquireMask
|
2195 SpvMemorySemanticsAcquireReleaseMask
|
2196 SpvMemorySemanticsSequentiallyConsistentMask
)) {
2197 *after
|= SpvMemorySemanticsAcquireMask
| storage_semantics
;
2200 if (av_vis_semantics
& SpvMemorySemanticsMakeVisibleMask
)
2201 *before
|= SpvMemorySemanticsMakeVisibleMask
| storage_semantics
;
2203 if (av_vis_semantics
& SpvMemorySemanticsMakeAvailableMask
)
2204 *after
|= SpvMemorySemanticsMakeAvailableMask
| storage_semantics
;
2207 static nir_memory_semantics
2208 vtn_mem_semantics_to_nir_mem_semantics(struct vtn_builder
*b
,
2209 SpvMemorySemanticsMask semantics
)
2211 nir_memory_semantics nir_semantics
= 0;
2213 SpvMemorySemanticsMask order_semantics
=
2214 semantics
& (SpvMemorySemanticsAcquireMask
|
2215 SpvMemorySemanticsReleaseMask
|
2216 SpvMemorySemanticsAcquireReleaseMask
|
2217 SpvMemorySemanticsSequentiallyConsistentMask
);
2219 if (util_bitcount(order_semantics
) > 1) {
2220 /* Old GLSLang versions incorrectly set all the ordering bits. This was
2221 * fixed in c51287d744fb6e7e9ccc09f6f8451e6c64b1dad6 of glslang repo,
2222 * and it is in GLSLang since revision "SPIRV99.1321" (from Jul-2016).
2224 vtn_warn("Multiple memory ordering semantics bits specified, "
2225 "assuming AcquireRelease.");
2226 order_semantics
= SpvMemorySemanticsAcquireReleaseMask
;
2229 switch (order_semantics
) {
2231 /* Not an ordering barrier. */
2234 case SpvMemorySemanticsAcquireMask
:
2235 nir_semantics
= NIR_MEMORY_ACQUIRE
;
2238 case SpvMemorySemanticsReleaseMask
:
2239 nir_semantics
= NIR_MEMORY_RELEASE
;
2242 case SpvMemorySemanticsSequentiallyConsistentMask
:
2243 /* Fall through. Treated as AcquireRelease in Vulkan. */
2244 case SpvMemorySemanticsAcquireReleaseMask
:
2245 nir_semantics
= NIR_MEMORY_ACQUIRE
| NIR_MEMORY_RELEASE
;
2249 unreachable("Invalid memory order semantics");
2252 if (semantics
& SpvMemorySemanticsMakeAvailableMask
) {
2253 vtn_fail_if(!b
->options
->caps
.vk_memory_model
,
2254 "To use MakeAvailable memory semantics the VulkanMemoryModel "
2255 "capability must be declared.");
2256 nir_semantics
|= NIR_MEMORY_MAKE_AVAILABLE
;
2259 if (semantics
& SpvMemorySemanticsMakeVisibleMask
) {
2260 vtn_fail_if(!b
->options
->caps
.vk_memory_model
,
2261 "To use MakeVisible memory semantics the VulkanMemoryModel "
2262 "capability must be declared.");
2263 nir_semantics
|= NIR_MEMORY_MAKE_VISIBLE
;
2266 return nir_semantics
;
2269 static nir_variable_mode
2270 vtn_mem_sematics_to_nir_var_modes(struct vtn_builder
*b
,
2271 SpvMemorySemanticsMask semantics
)
2273 /* Vulkan Environment for SPIR-V says "SubgroupMemory, CrossWorkgroupMemory,
2274 * and AtomicCounterMemory are ignored".
2276 semantics
&= ~(SpvMemorySemanticsSubgroupMemoryMask
|
2277 SpvMemorySemanticsCrossWorkgroupMemoryMask
|
2278 SpvMemorySemanticsAtomicCounterMemoryMask
);
2280 /* TODO: Consider adding nir_var_mem_image mode to NIR so it can be used
2281 * for SpvMemorySemanticsImageMemoryMask.
2284 nir_variable_mode modes
= 0;
2285 if (semantics
& (SpvMemorySemanticsUniformMemoryMask
|
2286 SpvMemorySemanticsImageMemoryMask
)) {
2287 modes
|= nir_var_uniform
|
2292 if (semantics
& SpvMemorySemanticsWorkgroupMemoryMask
)
2293 modes
|= nir_var_mem_shared
;
2294 if (semantics
& SpvMemorySemanticsOutputMemoryMask
) {
2295 modes
|= nir_var_shader_out
;
2302 vtn_scope_to_nir_scope(struct vtn_builder
*b
, SpvScope scope
)
2304 nir_scope nir_scope
;
2306 case SpvScopeDevice
:
2307 vtn_fail_if(b
->options
->caps
.vk_memory_model
&&
2308 !b
->options
->caps
.vk_memory_model_device_scope
,
2309 "If the Vulkan memory model is declared and any instruction "
2310 "uses Device scope, the VulkanMemoryModelDeviceScope "
2311 "capability must be declared.");
2312 nir_scope
= NIR_SCOPE_DEVICE
;
2315 case SpvScopeQueueFamily
:
2316 vtn_fail_if(!b
->options
->caps
.vk_memory_model
,
2317 "To use Queue Family scope, the VulkanMemoryModel capability "
2318 "must be declared.");
2319 nir_scope
= NIR_SCOPE_QUEUE_FAMILY
;
2322 case SpvScopeWorkgroup
:
2323 nir_scope
= NIR_SCOPE_WORKGROUP
;
2326 case SpvScopeSubgroup
:
2327 nir_scope
= NIR_SCOPE_SUBGROUP
;
2330 case SpvScopeInvocation
:
2331 nir_scope
= NIR_SCOPE_INVOCATION
;
2335 vtn_fail("Invalid memory scope");
2342 vtn_emit_scoped_control_barrier(struct vtn_builder
*b
, SpvScope exec_scope
,
2344 SpvMemorySemanticsMask semantics
)
2346 nir_memory_semantics nir_semantics
=
2347 vtn_mem_semantics_to_nir_mem_semantics(b
, semantics
);
2348 nir_variable_mode modes
= vtn_mem_sematics_to_nir_var_modes(b
, semantics
);
2349 nir_scope nir_exec_scope
= vtn_scope_to_nir_scope(b
, exec_scope
);
2351 /* Memory semantics is optional for OpControlBarrier. */
2352 nir_scope nir_mem_scope
;
2353 if (nir_semantics
== 0 || modes
== 0)
2354 nir_mem_scope
= NIR_SCOPE_NONE
;
2356 nir_mem_scope
= vtn_scope_to_nir_scope(b
, mem_scope
);
2358 nir_scoped_barrier(&b
->nb
, nir_exec_scope
, nir_mem_scope
, nir_semantics
, modes
);
2362 vtn_emit_scoped_memory_barrier(struct vtn_builder
*b
, SpvScope scope
,
2363 SpvMemorySemanticsMask semantics
)
2365 nir_variable_mode modes
= vtn_mem_sematics_to_nir_var_modes(b
, semantics
);
2366 nir_memory_semantics nir_semantics
=
2367 vtn_mem_semantics_to_nir_mem_semantics(b
, semantics
);
2369 /* No barrier to add. */
2370 if (nir_semantics
== 0 || modes
== 0)
2373 nir_scope nir_mem_scope
= vtn_scope_to_nir_scope(b
, scope
);
2374 nir_scoped_barrier(&b
->nb
, NIR_SCOPE_NONE
, nir_mem_scope
, nir_semantics
, modes
);
2377 struct vtn_ssa_value
*
2378 vtn_create_ssa_value(struct vtn_builder
*b
, const struct glsl_type
*type
)
2380 /* Always use bare types for SSA values for a couple of reasons:
2382 * 1. Code which emits deref chains should never listen to the explicit
2383 * layout information on the SSA value if any exists. If we've
2384 * accidentally been relying on this, we want to find those bugs.
2386 * 2. We want to be able to quickly check that an SSA value being assigned
2387 * to a SPIR-V value has the right type. Using bare types everywhere
2388 * ensures that we can pointer-compare.
2390 struct vtn_ssa_value
*val
= rzalloc(b
, struct vtn_ssa_value
);
2391 val
->type
= glsl_get_bare_type(type
);
2394 if (!glsl_type_is_vector_or_scalar(type
)) {
2395 unsigned elems
= glsl_get_length(val
->type
);
2396 val
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
2397 if (glsl_type_is_array_or_matrix(type
)) {
2398 const struct glsl_type
*elem_type
= glsl_get_array_element(type
);
2399 for (unsigned i
= 0; i
< elems
; i
++)
2400 val
->elems
[i
] = vtn_create_ssa_value(b
, elem_type
);
2402 vtn_assert(glsl_type_is_struct_or_ifc(type
));
2403 for (unsigned i
= 0; i
< elems
; i
++) {
2404 const struct glsl_type
*elem_type
= glsl_get_struct_field(type
, i
);
2405 val
->elems
[i
] = vtn_create_ssa_value(b
, elem_type
);
2414 vtn_tex_src(struct vtn_builder
*b
, unsigned index
, nir_tex_src_type type
)
2417 src
.src
= nir_src_for_ssa(vtn_get_nir_ssa(b
, index
));
2418 src
.src_type
= type
;
2423 image_operand_arg(struct vtn_builder
*b
, const uint32_t *w
, uint32_t count
,
2424 uint32_t mask_idx
, SpvImageOperandsMask op
)
2426 static const SpvImageOperandsMask ops_with_arg
=
2427 SpvImageOperandsBiasMask
|
2428 SpvImageOperandsLodMask
|
2429 SpvImageOperandsGradMask
|
2430 SpvImageOperandsConstOffsetMask
|
2431 SpvImageOperandsOffsetMask
|
2432 SpvImageOperandsConstOffsetsMask
|
2433 SpvImageOperandsSampleMask
|
2434 SpvImageOperandsMinLodMask
|
2435 SpvImageOperandsMakeTexelAvailableMask
|
2436 SpvImageOperandsMakeTexelVisibleMask
;
2438 assert(util_bitcount(op
) == 1);
2439 assert(w
[mask_idx
] & op
);
2440 assert(op
& ops_with_arg
);
2442 uint32_t idx
= util_bitcount(w
[mask_idx
] & (op
- 1) & ops_with_arg
) + 1;
2444 /* Adjust indices for operands with two arguments. */
2445 static const SpvImageOperandsMask ops_with_two_args
=
2446 SpvImageOperandsGradMask
;
2447 idx
+= util_bitcount(w
[mask_idx
] & (op
- 1) & ops_with_two_args
);
2451 vtn_fail_if(idx
+ (op
& ops_with_two_args
? 1 : 0) >= count
,
2452 "Image op claims to have %s but does not enough "
2453 "following operands", spirv_imageoperands_to_string(op
));
2459 non_uniform_decoration_cb(struct vtn_builder
*b
,
2460 struct vtn_value
*val
, int member
,
2461 const struct vtn_decoration
*dec
, void *void_ctx
)
2463 enum gl_access_qualifier
*access
= void_ctx
;
2464 switch (dec
->decoration
) {
2465 case SpvDecorationNonUniformEXT
:
2466 *access
|= ACCESS_NON_UNIFORM
;
2475 vtn_handle_texture(struct vtn_builder
*b
, SpvOp opcode
,
2476 const uint32_t *w
, unsigned count
)
2478 struct vtn_type
*ret_type
= vtn_get_type(b
, w
[1]);
2480 if (opcode
== SpvOpSampledImage
) {
2481 struct vtn_sampled_image si
= {
2482 .image
= vtn_get_image(b
, w
[3]),
2483 .sampler
= vtn_get_sampler(b
, w
[4]),
2485 vtn_push_sampled_image(b
, w
[2], si
);
2487 } else if (opcode
== SpvOpImage
) {
2488 struct vtn_sampled_image si
= vtn_get_sampled_image(b
, w
[3]);
2489 vtn_push_image(b
, w
[2], si
.image
);
2493 nir_deref_instr
*image
= NULL
, *sampler
= NULL
;
2494 struct vtn_value
*sampled_val
= vtn_untyped_value(b
, w
[3]);
2495 if (sampled_val
->type
->base_type
== vtn_base_type_sampled_image
) {
2496 struct vtn_sampled_image si
= vtn_get_sampled_image(b
, w
[3]);
2498 sampler
= si
.sampler
;
2500 image
= vtn_get_image(b
, w
[3]);
2503 const enum glsl_sampler_dim sampler_dim
= glsl_get_sampler_dim(image
->type
);
2504 const bool is_array
= glsl_sampler_type_is_array(image
->type
);
2505 nir_alu_type dest_type
= nir_type_invalid
;
2507 /* Figure out the base texture operation */
2510 case SpvOpImageSampleImplicitLod
:
2511 case SpvOpImageSampleDrefImplicitLod
:
2512 case SpvOpImageSampleProjImplicitLod
:
2513 case SpvOpImageSampleProjDrefImplicitLod
:
2514 texop
= nir_texop_tex
;
2517 case SpvOpImageSampleExplicitLod
:
2518 case SpvOpImageSampleDrefExplicitLod
:
2519 case SpvOpImageSampleProjExplicitLod
:
2520 case SpvOpImageSampleProjDrefExplicitLod
:
2521 texop
= nir_texop_txl
;
2524 case SpvOpImageFetch
:
2525 if (sampler_dim
== GLSL_SAMPLER_DIM_MS
) {
2526 texop
= nir_texop_txf_ms
;
2528 texop
= nir_texop_txf
;
2532 case SpvOpImageGather
:
2533 case SpvOpImageDrefGather
:
2534 texop
= nir_texop_tg4
;
2537 case SpvOpImageQuerySizeLod
:
2538 case SpvOpImageQuerySize
:
2539 texop
= nir_texop_txs
;
2540 dest_type
= nir_type_int
;
2543 case SpvOpImageQueryLod
:
2544 texop
= nir_texop_lod
;
2545 dest_type
= nir_type_float
;
2548 case SpvOpImageQueryLevels
:
2549 texop
= nir_texop_query_levels
;
2550 dest_type
= nir_type_int
;
2553 case SpvOpImageQuerySamples
:
2554 texop
= nir_texop_texture_samples
;
2555 dest_type
= nir_type_int
;
2558 case SpvOpFragmentFetchAMD
:
2559 texop
= nir_texop_fragment_fetch
;
2562 case SpvOpFragmentMaskFetchAMD
:
2563 texop
= nir_texop_fragment_mask_fetch
;
2567 vtn_fail_with_opcode("Unhandled opcode", opcode
);
2570 nir_tex_src srcs
[10]; /* 10 should be enough */
2571 nir_tex_src
*p
= srcs
;
2573 p
->src
= nir_src_for_ssa(&image
->dest
.ssa
);
2574 p
->src_type
= nir_tex_src_texture_deref
;
2584 vtn_fail_if(sampler
== NULL
,
2585 "%s requires an image of type OpTypeSampledImage",
2586 spirv_op_to_string(opcode
));
2587 p
->src
= nir_src_for_ssa(&sampler
->dest
.ssa
);
2588 p
->src_type
= nir_tex_src_sampler_deref
;
2592 case nir_texop_txf_ms
:
2594 case nir_texop_query_levels
:
2595 case nir_texop_texture_samples
:
2596 case nir_texop_samples_identical
:
2597 case nir_texop_fragment_fetch
:
2598 case nir_texop_fragment_mask_fetch
:
2601 case nir_texop_txf_ms_fb
:
2602 vtn_fail("unexpected nir_texop_txf_ms_fb");
2604 case nir_texop_txf_ms_mcs
:
2605 vtn_fail("unexpected nir_texop_txf_ms_mcs");
2606 case nir_texop_tex_prefetch
:
2607 vtn_fail("unexpected nir_texop_tex_prefetch");
2612 struct nir_ssa_def
*coord
;
2613 unsigned coord_components
;
2615 case SpvOpImageSampleImplicitLod
:
2616 case SpvOpImageSampleExplicitLod
:
2617 case SpvOpImageSampleDrefImplicitLod
:
2618 case SpvOpImageSampleDrefExplicitLod
:
2619 case SpvOpImageSampleProjImplicitLod
:
2620 case SpvOpImageSampleProjExplicitLod
:
2621 case SpvOpImageSampleProjDrefImplicitLod
:
2622 case SpvOpImageSampleProjDrefExplicitLod
:
2623 case SpvOpImageFetch
:
2624 case SpvOpImageGather
:
2625 case SpvOpImageDrefGather
:
2626 case SpvOpImageQueryLod
:
2627 case SpvOpFragmentFetchAMD
:
2628 case SpvOpFragmentMaskFetchAMD
: {
2629 /* All these types have the coordinate as their first real argument */
2630 coord_components
= glsl_get_sampler_dim_coordinate_components(sampler_dim
);
2632 if (is_array
&& texop
!= nir_texop_lod
)
2635 struct vtn_ssa_value
*coord_val
= vtn_ssa_value(b
, w
[idx
++]);
2636 coord
= coord_val
->def
;
2637 p
->src
= nir_src_for_ssa(nir_channels(&b
->nb
, coord
,
2638 (1 << coord_components
) - 1));
2640 /* OpenCL allows integer sampling coordinates */
2641 if (glsl_type_is_integer(coord_val
->type
) &&
2642 opcode
== SpvOpImageSampleExplicitLod
) {
2643 vtn_fail_if(b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
,
2644 "Unless the Kernel capability is being used, the coordinate parameter "
2645 "OpImageSampleExplicitLod must be floating point.");
2647 p
->src
= nir_src_for_ssa(nir_i2f32(&b
->nb
, p
->src
.ssa
));
2650 p
->src_type
= nir_tex_src_coord
;
2657 coord_components
= 0;
2662 case SpvOpImageSampleProjImplicitLod
:
2663 case SpvOpImageSampleProjExplicitLod
:
2664 case SpvOpImageSampleProjDrefImplicitLod
:
2665 case SpvOpImageSampleProjDrefExplicitLod
:
2666 /* These have the projector as the last coordinate component */
2667 p
->src
= nir_src_for_ssa(nir_channel(&b
->nb
, coord
, coord_components
));
2668 p
->src_type
= nir_tex_src_projector
;
2676 bool is_shadow
= false;
2677 unsigned gather_component
= 0;
2679 case SpvOpImageSampleDrefImplicitLod
:
2680 case SpvOpImageSampleDrefExplicitLod
:
2681 case SpvOpImageSampleProjDrefImplicitLod
:
2682 case SpvOpImageSampleProjDrefExplicitLod
:
2683 case SpvOpImageDrefGather
:
2684 /* These all have an explicit depth value as their next source */
2686 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_comparator
);
2689 case SpvOpImageGather
:
2690 /* This has a component as its next source */
2691 gather_component
= vtn_constant_uint(b
, w
[idx
++]);
2698 /* For OpImageQuerySizeLod, we always have an LOD */
2699 if (opcode
== SpvOpImageQuerySizeLod
)
2700 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_lod
);
2702 /* For OpFragmentFetchAMD, we always have a multisample index */
2703 if (opcode
== SpvOpFragmentFetchAMD
)
2704 (*p
++) = vtn_tex_src(b
, w
[idx
++], nir_tex_src_ms_index
);
2706 /* Now we need to handle some number of optional arguments */
2707 struct vtn_value
*gather_offsets
= NULL
;
2709 uint32_t operands
= w
[idx
];
2711 if (operands
& SpvImageOperandsBiasMask
) {
2712 vtn_assert(texop
== nir_texop_tex
||
2713 texop
== nir_texop_tg4
);
2714 if (texop
== nir_texop_tex
)
2715 texop
= nir_texop_txb
;
2716 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2717 SpvImageOperandsBiasMask
);
2718 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_bias
);
2721 if (operands
& SpvImageOperandsLodMask
) {
2722 vtn_assert(texop
== nir_texop_txl
|| texop
== nir_texop_txf
||
2723 texop
== nir_texop_txs
|| texop
== nir_texop_tg4
);
2724 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2725 SpvImageOperandsLodMask
);
2726 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_lod
);
2729 if (operands
& SpvImageOperandsGradMask
) {
2730 vtn_assert(texop
== nir_texop_txl
);
2731 texop
= nir_texop_txd
;
2732 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2733 SpvImageOperandsGradMask
);
2734 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_ddx
);
2735 (*p
++) = vtn_tex_src(b
, w
[arg
+ 1], nir_tex_src_ddy
);
2738 vtn_fail_if(util_bitcount(operands
& (SpvImageOperandsConstOffsetsMask
|
2739 SpvImageOperandsOffsetMask
|
2740 SpvImageOperandsConstOffsetMask
)) > 1,
2741 "At most one of the ConstOffset, Offset, and ConstOffsets "
2742 "image operands can be used on a given instruction.");
2744 if (operands
& SpvImageOperandsOffsetMask
) {
2745 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2746 SpvImageOperandsOffsetMask
);
2747 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_offset
);
2750 if (operands
& SpvImageOperandsConstOffsetMask
) {
2751 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2752 SpvImageOperandsConstOffsetMask
);
2753 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_offset
);
2756 if (operands
& SpvImageOperandsConstOffsetsMask
) {
2757 vtn_assert(texop
== nir_texop_tg4
);
2758 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2759 SpvImageOperandsConstOffsetsMask
);
2760 gather_offsets
= vtn_value(b
, w
[arg
], vtn_value_type_constant
);
2763 if (operands
& SpvImageOperandsSampleMask
) {
2764 vtn_assert(texop
== nir_texop_txf_ms
);
2765 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2766 SpvImageOperandsSampleMask
);
2767 texop
= nir_texop_txf_ms
;
2768 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_ms_index
);
2771 if (operands
& SpvImageOperandsMinLodMask
) {
2772 vtn_assert(texop
== nir_texop_tex
||
2773 texop
== nir_texop_txb
||
2774 texop
== nir_texop_txd
);
2775 uint32_t arg
= image_operand_arg(b
, w
, count
, idx
,
2776 SpvImageOperandsMinLodMask
);
2777 (*p
++) = vtn_tex_src(b
, w
[arg
], nir_tex_src_min_lod
);
2781 nir_tex_instr
*instr
= nir_tex_instr_create(b
->shader
, p
- srcs
);
2784 memcpy(instr
->src
, srcs
, instr
->num_srcs
* sizeof(*instr
->src
));
2786 instr
->coord_components
= coord_components
;
2787 instr
->sampler_dim
= sampler_dim
;
2788 instr
->is_array
= is_array
;
2789 instr
->is_shadow
= is_shadow
;
2790 instr
->is_new_style_shadow
=
2791 is_shadow
&& glsl_get_components(ret_type
->type
) == 1;
2792 instr
->component
= gather_component
;
2794 /* The Vulkan spec says:
2796 * "If an instruction loads from or stores to a resource (including
2797 * atomics and image instructions) and the resource descriptor being
2798 * accessed is not dynamically uniform, then the operand corresponding
2799 * to that resource (e.g. the pointer or sampled image operand) must be
2800 * decorated with NonUniform."
2802 * It's very careful to specify that the exact operand must be decorated
2803 * NonUniform. The SPIR-V parser is not expected to chase through long
2804 * chains to find the NonUniform decoration. It's either right there or we
2805 * can assume it doesn't exist.
2807 enum gl_access_qualifier access
= 0;
2808 vtn_foreach_decoration(b
, sampled_val
, non_uniform_decoration_cb
, &access
);
2810 if (image
&& (access
& ACCESS_NON_UNIFORM
))
2811 instr
->texture_non_uniform
= true;
2813 if (sampler
&& (access
& ACCESS_NON_UNIFORM
))
2814 instr
->sampler_non_uniform
= true;
2816 /* for non-query ops, get dest_type from SPIR-V return type */
2817 if (dest_type
== nir_type_invalid
) {
2818 /* the return type should match the image type, unless the image type is
2819 * VOID (CL image), in which case the return type dictates the sampler
2821 enum glsl_base_type sampler_base
=
2822 glsl_get_sampler_result_type(image
->type
);
2823 enum glsl_base_type ret_base
= glsl_get_base_type(ret_type
->type
);
2824 vtn_fail_if(sampler_base
!= ret_base
&& sampler_base
!= GLSL_TYPE_VOID
,
2825 "SPIR-V return type mismatches image type. This is only valid "
2826 "for untyped images (OpenCL).");
2828 case GLSL_TYPE_FLOAT
: dest_type
= nir_type_float
; break;
2829 case GLSL_TYPE_INT
: dest_type
= nir_type_int
; break;
2830 case GLSL_TYPE_UINT
: dest_type
= nir_type_uint
; break;
2831 case GLSL_TYPE_BOOL
: dest_type
= nir_type_bool
; break;
2833 vtn_fail("Invalid base type for sampler result");
2837 instr
->dest_type
= dest_type
;
2839 nir_ssa_dest_init(&instr
->instr
, &instr
->dest
,
2840 nir_tex_instr_dest_size(instr
), 32, NULL
);
2842 vtn_assert(glsl_get_vector_elements(ret_type
->type
) ==
2843 nir_tex_instr_dest_size(instr
));
2845 if (gather_offsets
) {
2846 vtn_fail_if(gather_offsets
->type
->base_type
!= vtn_base_type_array
||
2847 gather_offsets
->type
->length
!= 4,
2848 "ConstOffsets must be an array of size four of vectors "
2849 "of two integer components");
2851 struct vtn_type
*vec_type
= gather_offsets
->type
->array_element
;
2852 vtn_fail_if(vec_type
->base_type
!= vtn_base_type_vector
||
2853 vec_type
->length
!= 2 ||
2854 !glsl_type_is_integer(vec_type
->type
),
2855 "ConstOffsets must be an array of size four of vectors "
2856 "of two integer components");
2858 unsigned bit_size
= glsl_get_bit_size(vec_type
->type
);
2859 for (uint32_t i
= 0; i
< 4; i
++) {
2860 const nir_const_value
*cvec
=
2861 gather_offsets
->constant
->elements
[i
]->values
;
2862 for (uint32_t j
= 0; j
< 2; j
++) {
2864 case 8: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i8
; break;
2865 case 16: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i16
; break;
2866 case 32: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i32
; break;
2867 case 64: instr
->tg4_offsets
[i
][j
] = cvec
[j
].i64
; break;
2869 vtn_fail("Unsupported bit size: %u", bit_size
);
2875 nir_builder_instr_insert(&b
->nb
, &instr
->instr
);
2877 vtn_push_nir_ssa(b
, w
[2], &instr
->dest
.ssa
);
2881 fill_common_atomic_sources(struct vtn_builder
*b
, SpvOp opcode
,
2882 const uint32_t *w
, nir_src
*src
)
2885 case SpvOpAtomicIIncrement
:
2886 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, 1));
2889 case SpvOpAtomicIDecrement
:
2890 src
[0] = nir_src_for_ssa(nir_imm_int(&b
->nb
, -1));
2893 case SpvOpAtomicISub
:
2895 nir_src_for_ssa(nir_ineg(&b
->nb
, vtn_get_nir_ssa(b
, w
[6])));
2898 case SpvOpAtomicCompareExchange
:
2899 case SpvOpAtomicCompareExchangeWeak
:
2900 src
[0] = nir_src_for_ssa(vtn_get_nir_ssa(b
, w
[8]));
2901 src
[1] = nir_src_for_ssa(vtn_get_nir_ssa(b
, w
[7]));
2904 case SpvOpAtomicExchange
:
2905 case SpvOpAtomicIAdd
:
2906 case SpvOpAtomicSMin
:
2907 case SpvOpAtomicUMin
:
2908 case SpvOpAtomicSMax
:
2909 case SpvOpAtomicUMax
:
2910 case SpvOpAtomicAnd
:
2912 case SpvOpAtomicXor
:
2913 case SpvOpAtomicFAddEXT
:
2914 src
[0] = nir_src_for_ssa(vtn_get_nir_ssa(b
, w
[6]));
2918 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
2922 static nir_ssa_def
*
2923 get_image_coord(struct vtn_builder
*b
, uint32_t value
)
2925 nir_ssa_def
*coord
= vtn_get_nir_ssa(b
, value
);
2927 /* The image_load_store intrinsics assume a 4-dim coordinate */
2928 unsigned swizzle
[4];
2929 for (unsigned i
= 0; i
< 4; i
++)
2930 swizzle
[i
] = MIN2(i
, coord
->num_components
- 1);
2932 return nir_swizzle(&b
->nb
, coord
, swizzle
, 4);
2935 static nir_ssa_def
*
2936 expand_to_vec4(nir_builder
*b
, nir_ssa_def
*value
)
2938 if (value
->num_components
== 4)
2942 for (unsigned i
= 0; i
< 4; i
++)
2943 swiz
[i
] = i
< value
->num_components
? i
: 0;
2944 return nir_swizzle(b
, value
, swiz
, 4);
2948 vtn_handle_image(struct vtn_builder
*b
, SpvOp opcode
,
2949 const uint32_t *w
, unsigned count
)
2951 /* Just get this one out of the way */
2952 if (opcode
== SpvOpImageTexelPointer
) {
2953 struct vtn_value
*val
=
2954 vtn_push_value(b
, w
[2], vtn_value_type_image_pointer
);
2955 val
->image
= ralloc(b
, struct vtn_image_pointer
);
2957 val
->image
->image
= vtn_nir_deref(b
, w
[3]);
2958 val
->image
->coord
= get_image_coord(b
, w
[4]);
2959 val
->image
->sample
= vtn_get_nir_ssa(b
, w
[5]);
2960 val
->image
->lod
= nir_imm_int(&b
->nb
, 0);
2964 struct vtn_image_pointer image
;
2965 SpvScope scope
= SpvScopeInvocation
;
2966 SpvMemorySemanticsMask semantics
= 0;
2968 enum gl_access_qualifier access
= 0;
2970 struct vtn_value
*res_val
;
2972 case SpvOpAtomicExchange
:
2973 case SpvOpAtomicCompareExchange
:
2974 case SpvOpAtomicCompareExchangeWeak
:
2975 case SpvOpAtomicIIncrement
:
2976 case SpvOpAtomicIDecrement
:
2977 case SpvOpAtomicIAdd
:
2978 case SpvOpAtomicISub
:
2979 case SpvOpAtomicLoad
:
2980 case SpvOpAtomicSMin
:
2981 case SpvOpAtomicUMin
:
2982 case SpvOpAtomicSMax
:
2983 case SpvOpAtomicUMax
:
2984 case SpvOpAtomicAnd
:
2986 case SpvOpAtomicXor
:
2987 case SpvOpAtomicFAddEXT
:
2988 res_val
= vtn_value(b
, w
[3], vtn_value_type_image_pointer
);
2989 image
= *res_val
->image
;
2990 scope
= vtn_constant_uint(b
, w
[4]);
2991 semantics
= vtn_constant_uint(b
, w
[5]);
2992 access
|= ACCESS_COHERENT
;
2995 case SpvOpAtomicStore
:
2996 res_val
= vtn_value(b
, w
[1], vtn_value_type_image_pointer
);
2997 image
= *res_val
->image
;
2998 scope
= vtn_constant_uint(b
, w
[2]);
2999 semantics
= vtn_constant_uint(b
, w
[3]);
3000 access
|= ACCESS_COHERENT
;
3003 case SpvOpImageQuerySizeLod
:
3004 res_val
= vtn_untyped_value(b
, w
[3]);
3005 image
.image
= vtn_get_image(b
, w
[3]);
3007 image
.sample
= NULL
;
3008 image
.lod
= vtn_ssa_value(b
, w
[4])->def
;
3011 case SpvOpImageQuerySize
:
3012 res_val
= vtn_untyped_value(b
, w
[3]);
3013 image
.image
= vtn_get_image(b
, w
[3]);
3015 image
.sample
= NULL
;
3019 case SpvOpImageQueryFormat
:
3020 case SpvOpImageQueryOrder
:
3021 res_val
= vtn_untyped_value(b
, w
[3]);
3022 image
.image
= vtn_get_image(b
, w
[3]);
3024 image
.sample
= NULL
;
3028 case SpvOpImageRead
: {
3029 res_val
= vtn_untyped_value(b
, w
[3]);
3030 image
.image
= vtn_get_image(b
, w
[3]);
3031 image
.coord
= get_image_coord(b
, w
[4]);
3033 const SpvImageOperandsMask operands
=
3034 count
> 5 ? w
[5] : SpvImageOperandsMaskNone
;
3036 if (operands
& SpvImageOperandsSampleMask
) {
3037 uint32_t arg
= image_operand_arg(b
, w
, count
, 5,
3038 SpvImageOperandsSampleMask
);
3039 image
.sample
= vtn_get_nir_ssa(b
, w
[arg
]);
3041 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
3044 if (operands
& SpvImageOperandsMakeTexelVisibleMask
) {
3045 vtn_fail_if((operands
& SpvImageOperandsNonPrivateTexelMask
) == 0,
3046 "MakeTexelVisible requires NonPrivateTexel to also be set.");
3047 uint32_t arg
= image_operand_arg(b
, w
, count
, 5,
3048 SpvImageOperandsMakeTexelVisibleMask
);
3049 semantics
= SpvMemorySemanticsMakeVisibleMask
;
3050 scope
= vtn_constant_uint(b
, w
[arg
]);
3053 if (operands
& SpvImageOperandsLodMask
) {
3054 uint32_t arg
= image_operand_arg(b
, w
, count
, 5,
3055 SpvImageOperandsLodMask
);
3056 image
.lod
= vtn_get_nir_ssa(b
, w
[arg
]);
3058 image
.lod
= nir_imm_int(&b
->nb
, 0);
3061 /* TODO: Volatile. */
3066 case SpvOpImageWrite
: {
3067 res_val
= vtn_untyped_value(b
, w
[1]);
3068 image
.image
= vtn_get_image(b
, w
[1]);
3069 image
.coord
= get_image_coord(b
, w
[2]);
3073 const SpvImageOperandsMask operands
=
3074 count
> 4 ? w
[4] : SpvImageOperandsMaskNone
;
3076 if (operands
& SpvImageOperandsSampleMask
) {
3077 uint32_t arg
= image_operand_arg(b
, w
, count
, 4,
3078 SpvImageOperandsSampleMask
);
3079 image
.sample
= vtn_get_nir_ssa(b
, w
[arg
]);
3081 image
.sample
= nir_ssa_undef(&b
->nb
, 1, 32);
3084 if (operands
& SpvImageOperandsMakeTexelAvailableMask
) {
3085 vtn_fail_if((operands
& SpvImageOperandsNonPrivateTexelMask
) == 0,
3086 "MakeTexelAvailable requires NonPrivateTexel to also be set.");
3087 uint32_t arg
= image_operand_arg(b
, w
, count
, 4,
3088 SpvImageOperandsMakeTexelAvailableMask
);
3089 semantics
= SpvMemorySemanticsMakeAvailableMask
;
3090 scope
= vtn_constant_uint(b
, w
[arg
]);
3093 if (operands
& SpvImageOperandsLodMask
) {
3094 uint32_t arg
= image_operand_arg(b
, w
, count
, 4,
3095 SpvImageOperandsLodMask
);
3096 image
.lod
= vtn_get_nir_ssa(b
, w
[arg
]);
3098 image
.lod
= nir_imm_int(&b
->nb
, 0);
3101 /* TODO: Volatile. */
3107 vtn_fail_with_opcode("Invalid image opcode", opcode
);
3110 nir_intrinsic_op op
;
3112 #define OP(S, N) case SpvOp##S: op = nir_intrinsic_image_deref_##N; break;
3113 OP(ImageQuerySize
, size
)
3114 OP(ImageQuerySizeLod
, size
)
3116 OP(ImageWrite
, store
)
3117 OP(AtomicLoad
, load
)
3118 OP(AtomicStore
, store
)
3119 OP(AtomicExchange
, atomic_exchange
)
3120 OP(AtomicCompareExchange
, atomic_comp_swap
)
3121 OP(AtomicCompareExchangeWeak
, atomic_comp_swap
)
3122 OP(AtomicIIncrement
, atomic_add
)
3123 OP(AtomicIDecrement
, atomic_add
)
3124 OP(AtomicIAdd
, atomic_add
)
3125 OP(AtomicISub
, atomic_add
)
3126 OP(AtomicSMin
, atomic_imin
)
3127 OP(AtomicUMin
, atomic_umin
)
3128 OP(AtomicSMax
, atomic_imax
)
3129 OP(AtomicUMax
, atomic_umax
)
3130 OP(AtomicAnd
, atomic_and
)
3131 OP(AtomicOr
, atomic_or
)
3132 OP(AtomicXor
, atomic_xor
)
3133 OP(AtomicFAddEXT
, atomic_fadd
)
3134 OP(ImageQueryFormat
, format
)
3135 OP(ImageQueryOrder
, order
)
3138 vtn_fail_with_opcode("Invalid image opcode", opcode
);
3141 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
3143 intrin
->src
[0] = nir_src_for_ssa(&image
.image
->dest
.ssa
);
3146 case SpvOpImageQuerySize
:
3147 case SpvOpImageQuerySizeLod
:
3148 case SpvOpImageQueryFormat
:
3149 case SpvOpImageQueryOrder
:
3152 /* The image coordinate is always 4 components but we may not have that
3153 * many. Swizzle to compensate.
3155 intrin
->src
[1] = nir_src_for_ssa(expand_to_vec4(&b
->nb
, image
.coord
));
3156 intrin
->src
[2] = nir_src_for_ssa(image
.sample
);
3160 /* The Vulkan spec says:
3162 * "If an instruction loads from or stores to a resource (including
3163 * atomics and image instructions) and the resource descriptor being
3164 * accessed is not dynamically uniform, then the operand corresponding
3165 * to that resource (e.g. the pointer or sampled image operand) must be
3166 * decorated with NonUniform."
3168 * It's very careful to specify that the exact operand must be decorated
3169 * NonUniform. The SPIR-V parser is not expected to chase through long
3170 * chains to find the NonUniform decoration. It's either right there or we
3171 * can assume it doesn't exist.
3173 vtn_foreach_decoration(b
, res_val
, non_uniform_decoration_cb
, &access
);
3174 nir_intrinsic_set_access(intrin
, access
);
3177 case SpvOpImageQueryFormat
:
3178 case SpvOpImageQueryOrder
:
3179 /* No additional sources */
3181 case SpvOpImageQuerySize
:
3182 intrin
->src
[1] = nir_src_for_ssa(nir_imm_int(&b
->nb
, 0));
3184 case SpvOpImageQuerySizeLod
:
3185 intrin
->src
[1] = nir_src_for_ssa(image
.lod
);
3187 case SpvOpAtomicLoad
:
3188 case SpvOpImageRead
:
3189 /* Only OpImageRead can support a lod parameter if
3190 * SPV_AMD_shader_image_load_store_lod is used but the current NIR
3191 * intrinsics definition for atomics requires us to set it for
3194 intrin
->src
[3] = nir_src_for_ssa(image
.lod
);
3196 case SpvOpAtomicStore
:
3197 case SpvOpImageWrite
: {
3198 const uint32_t value_id
= opcode
== SpvOpAtomicStore
? w
[4] : w
[3];
3199 struct vtn_ssa_value
*value
= vtn_ssa_value(b
, value_id
);
3200 /* nir_intrinsic_image_deref_store always takes a vec4 value */
3201 assert(op
== nir_intrinsic_image_deref_store
);
3202 intrin
->num_components
= 4;
3203 intrin
->src
[3] = nir_src_for_ssa(expand_to_vec4(&b
->nb
, value
->def
));
3204 /* Only OpImageWrite can support a lod parameter if
3205 * SPV_AMD_shader_image_load_store_lod is used but the current NIR
3206 * intrinsics definition for atomics requires us to set it for
3209 intrin
->src
[4] = nir_src_for_ssa(image
.lod
);
3211 if (opcode
== SpvOpImageWrite
)
3212 nir_intrinsic_set_type(intrin
, nir_get_nir_type_for_glsl_type(value
->type
));
3216 case SpvOpAtomicCompareExchange
:
3217 case SpvOpAtomicCompareExchangeWeak
:
3218 case SpvOpAtomicIIncrement
:
3219 case SpvOpAtomicIDecrement
:
3220 case SpvOpAtomicExchange
:
3221 case SpvOpAtomicIAdd
:
3222 case SpvOpAtomicISub
:
3223 case SpvOpAtomicSMin
:
3224 case SpvOpAtomicUMin
:
3225 case SpvOpAtomicSMax
:
3226 case SpvOpAtomicUMax
:
3227 case SpvOpAtomicAnd
:
3229 case SpvOpAtomicXor
:
3230 case SpvOpAtomicFAddEXT
:
3231 fill_common_atomic_sources(b
, opcode
, w
, &intrin
->src
[3]);
3235 vtn_fail_with_opcode("Invalid image opcode", opcode
);
3238 /* Image operations implicitly have the Image storage memory semantics. */
3239 semantics
|= SpvMemorySemanticsImageMemoryMask
;
3241 SpvMemorySemanticsMask before_semantics
;
3242 SpvMemorySemanticsMask after_semantics
;
3243 vtn_split_barrier_semantics(b
, semantics
, &before_semantics
, &after_semantics
);
3245 if (before_semantics
)
3246 vtn_emit_memory_barrier(b
, scope
, before_semantics
);
3248 if (opcode
!= SpvOpImageWrite
&& opcode
!= SpvOpAtomicStore
) {
3249 struct vtn_type
*type
= vtn_get_type(b
, w
[1]);
3251 unsigned dest_components
= glsl_get_vector_elements(type
->type
);
3252 if (nir_intrinsic_infos
[op
].dest_components
== 0)
3253 intrin
->num_components
= dest_components
;
3255 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
,
3256 nir_intrinsic_dest_components(intrin
), 32, NULL
);
3258 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3260 nir_ssa_def
*result
= &intrin
->dest
.ssa
;
3261 if (nir_intrinsic_dest_components(intrin
) != dest_components
)
3262 result
= nir_channels(&b
->nb
, result
, (1 << dest_components
) - 1);
3264 vtn_push_nir_ssa(b
, w
[2], result
);
3266 if (opcode
== SpvOpImageRead
)
3267 nir_intrinsic_set_type(intrin
, nir_get_nir_type_for_glsl_type(type
->type
));
3269 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3272 if (after_semantics
)
3273 vtn_emit_memory_barrier(b
, scope
, after_semantics
);
3276 static nir_intrinsic_op
3277 get_ssbo_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
3280 case SpvOpAtomicLoad
: return nir_intrinsic_load_ssbo
;
3281 case SpvOpAtomicStore
: return nir_intrinsic_store_ssbo
;
3282 #define OP(S, N) case SpvOp##S: return nir_intrinsic_ssbo_##N;
3283 OP(AtomicExchange
, atomic_exchange
)
3284 OP(AtomicCompareExchange
, atomic_comp_swap
)
3285 OP(AtomicCompareExchangeWeak
, atomic_comp_swap
)
3286 OP(AtomicIIncrement
, atomic_add
)
3287 OP(AtomicIDecrement
, atomic_add
)
3288 OP(AtomicIAdd
, atomic_add
)
3289 OP(AtomicISub
, atomic_add
)
3290 OP(AtomicSMin
, atomic_imin
)
3291 OP(AtomicUMin
, atomic_umin
)
3292 OP(AtomicSMax
, atomic_imax
)
3293 OP(AtomicUMax
, atomic_umax
)
3294 OP(AtomicAnd
, atomic_and
)
3295 OP(AtomicOr
, atomic_or
)
3296 OP(AtomicXor
, atomic_xor
)
3297 OP(AtomicFAddEXT
, atomic_fadd
)
3300 vtn_fail_with_opcode("Invalid SSBO atomic", opcode
);
3304 static nir_intrinsic_op
3305 get_uniform_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
3308 #define OP(S, N) case SpvOp##S: return nir_intrinsic_atomic_counter_ ##N;
3309 OP(AtomicLoad
, read_deref
)
3310 OP(AtomicExchange
, exchange
)
3311 OP(AtomicCompareExchange
, comp_swap
)
3312 OP(AtomicCompareExchangeWeak
, comp_swap
)
3313 OP(AtomicIIncrement
, inc_deref
)
3314 OP(AtomicIDecrement
, post_dec_deref
)
3315 OP(AtomicIAdd
, add_deref
)
3316 OP(AtomicISub
, add_deref
)
3317 OP(AtomicUMin
, min_deref
)
3318 OP(AtomicUMax
, max_deref
)
3319 OP(AtomicAnd
, and_deref
)
3320 OP(AtomicOr
, or_deref
)
3321 OP(AtomicXor
, xor_deref
)
3324 /* We left the following out: AtomicStore, AtomicSMin and
3325 * AtomicSmax. Right now there are not nir intrinsics for them. At this
3326 * moment Atomic Counter support is needed for ARB_spirv support, so is
3327 * only need to support GLSL Atomic Counters that are uints and don't
3328 * allow direct storage.
3330 vtn_fail("Invalid uniform atomic");
3334 static nir_intrinsic_op
3335 get_deref_nir_atomic_op(struct vtn_builder
*b
, SpvOp opcode
)
3338 case SpvOpAtomicLoad
: return nir_intrinsic_load_deref
;
3339 case SpvOpAtomicStore
: return nir_intrinsic_store_deref
;
3340 #define OP(S, N) case SpvOp##S: return nir_intrinsic_deref_##N;
3341 OP(AtomicExchange
, atomic_exchange
)
3342 OP(AtomicCompareExchange
, atomic_comp_swap
)
3343 OP(AtomicCompareExchangeWeak
, atomic_comp_swap
)
3344 OP(AtomicIIncrement
, atomic_add
)
3345 OP(AtomicIDecrement
, atomic_add
)
3346 OP(AtomicIAdd
, atomic_add
)
3347 OP(AtomicISub
, atomic_add
)
3348 OP(AtomicSMin
, atomic_imin
)
3349 OP(AtomicUMin
, atomic_umin
)
3350 OP(AtomicSMax
, atomic_imax
)
3351 OP(AtomicUMax
, atomic_umax
)
3352 OP(AtomicAnd
, atomic_and
)
3353 OP(AtomicOr
, atomic_or
)
3354 OP(AtomicXor
, atomic_xor
)
3355 OP(AtomicFAddEXT
, atomic_fadd
)
3358 vtn_fail_with_opcode("Invalid shared atomic", opcode
);
3363 * Handles shared atomics, ssbo atomics and atomic counters.
3366 vtn_handle_atomics(struct vtn_builder
*b
, SpvOp opcode
,
3367 const uint32_t *w
, UNUSED
unsigned count
)
3369 struct vtn_pointer
*ptr
;
3370 nir_intrinsic_instr
*atomic
;
3372 SpvScope scope
= SpvScopeInvocation
;
3373 SpvMemorySemanticsMask semantics
= 0;
3376 case SpvOpAtomicLoad
:
3377 case SpvOpAtomicExchange
:
3378 case SpvOpAtomicCompareExchange
:
3379 case SpvOpAtomicCompareExchangeWeak
:
3380 case SpvOpAtomicIIncrement
:
3381 case SpvOpAtomicIDecrement
:
3382 case SpvOpAtomicIAdd
:
3383 case SpvOpAtomicISub
:
3384 case SpvOpAtomicSMin
:
3385 case SpvOpAtomicUMin
:
3386 case SpvOpAtomicSMax
:
3387 case SpvOpAtomicUMax
:
3388 case SpvOpAtomicAnd
:
3390 case SpvOpAtomicXor
:
3391 case SpvOpAtomicFAddEXT
:
3392 ptr
= vtn_value(b
, w
[3], vtn_value_type_pointer
)->pointer
;
3393 scope
= vtn_constant_uint(b
, w
[4]);
3394 semantics
= vtn_constant_uint(b
, w
[5]);
3397 case SpvOpAtomicStore
:
3398 ptr
= vtn_value(b
, w
[1], vtn_value_type_pointer
)->pointer
;
3399 scope
= vtn_constant_uint(b
, w
[2]);
3400 semantics
= vtn_constant_uint(b
, w
[3]);
3404 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
3407 /* uniform as "atomic counter uniform" */
3408 if (ptr
->mode
== vtn_variable_mode_atomic_counter
) {
3409 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
3410 nir_intrinsic_op op
= get_uniform_nir_atomic_op(b
, opcode
);
3411 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
3412 atomic
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
3414 /* SSBO needs to initialize index/offset. In this case we don't need to,
3415 * as that info is already stored on the ptr->var->var nir_variable (see
3416 * vtn_create_variable)
3420 case SpvOpAtomicLoad
:
3421 case SpvOpAtomicExchange
:
3422 case SpvOpAtomicCompareExchange
:
3423 case SpvOpAtomicCompareExchangeWeak
:
3424 case SpvOpAtomicIIncrement
:
3425 case SpvOpAtomicIDecrement
:
3426 case SpvOpAtomicIAdd
:
3427 case SpvOpAtomicISub
:
3428 case SpvOpAtomicSMin
:
3429 case SpvOpAtomicUMin
:
3430 case SpvOpAtomicSMax
:
3431 case SpvOpAtomicUMax
:
3432 case SpvOpAtomicAnd
:
3434 case SpvOpAtomicXor
:
3435 /* Nothing: we don't need to call fill_common_atomic_sources here, as
3436 * atomic counter uniforms doesn't have sources
3441 unreachable("Invalid SPIR-V atomic");
3444 } else if (vtn_pointer_uses_ssa_offset(b
, ptr
)) {
3445 nir_ssa_def
*offset
, *index
;
3446 offset
= vtn_pointer_to_offset(b
, ptr
, &index
);
3448 assert(ptr
->mode
== vtn_variable_mode_ssbo
);
3450 nir_intrinsic_op op
= get_ssbo_nir_atomic_op(b
, opcode
);
3451 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
3453 nir_intrinsic_set_access(atomic
, ACCESS_COHERENT
);
3457 case SpvOpAtomicLoad
:
3458 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
3459 nir_intrinsic_set_align(atomic
, 4, 0);
3460 if (ptr
->mode
== vtn_variable_mode_ssbo
)
3461 atomic
->src
[src
++] = nir_src_for_ssa(index
);
3462 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
3465 case SpvOpAtomicStore
:
3466 atomic
->num_components
= glsl_get_vector_elements(ptr
->type
->type
);
3467 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
3468 nir_intrinsic_set_align(atomic
, 4, 0);
3469 atomic
->src
[src
++] = nir_src_for_ssa(vtn_get_nir_ssa(b
, w
[4]));
3470 if (ptr
->mode
== vtn_variable_mode_ssbo
)
3471 atomic
->src
[src
++] = nir_src_for_ssa(index
);
3472 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
3475 case SpvOpAtomicExchange
:
3476 case SpvOpAtomicCompareExchange
:
3477 case SpvOpAtomicCompareExchangeWeak
:
3478 case SpvOpAtomicIIncrement
:
3479 case SpvOpAtomicIDecrement
:
3480 case SpvOpAtomicIAdd
:
3481 case SpvOpAtomicISub
:
3482 case SpvOpAtomicSMin
:
3483 case SpvOpAtomicUMin
:
3484 case SpvOpAtomicSMax
:
3485 case SpvOpAtomicUMax
:
3486 case SpvOpAtomicAnd
:
3488 case SpvOpAtomicXor
:
3489 case SpvOpAtomicFAddEXT
:
3490 if (ptr
->mode
== vtn_variable_mode_ssbo
)
3491 atomic
->src
[src
++] = nir_src_for_ssa(index
);
3492 atomic
->src
[src
++] = nir_src_for_ssa(offset
);
3493 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[src
]);
3497 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
3500 nir_deref_instr
*deref
= vtn_pointer_to_deref(b
, ptr
);
3501 const struct glsl_type
*deref_type
= deref
->type
;
3502 nir_intrinsic_op op
= get_deref_nir_atomic_op(b
, opcode
);
3503 atomic
= nir_intrinsic_instr_create(b
->nb
.shader
, op
);
3504 atomic
->src
[0] = nir_src_for_ssa(&deref
->dest
.ssa
);
3506 if (ptr
->mode
!= vtn_variable_mode_workgroup
)
3507 nir_intrinsic_set_access(atomic
, ACCESS_COHERENT
);
3510 case SpvOpAtomicLoad
:
3511 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
3514 case SpvOpAtomicStore
:
3515 atomic
->num_components
= glsl_get_vector_elements(deref_type
);
3516 nir_intrinsic_set_write_mask(atomic
, (1 << atomic
->num_components
) - 1);
3517 atomic
->src
[1] = nir_src_for_ssa(vtn_get_nir_ssa(b
, w
[4]));
3520 case SpvOpAtomicExchange
:
3521 case SpvOpAtomicCompareExchange
:
3522 case SpvOpAtomicCompareExchangeWeak
:
3523 case SpvOpAtomicIIncrement
:
3524 case SpvOpAtomicIDecrement
:
3525 case SpvOpAtomicIAdd
:
3526 case SpvOpAtomicISub
:
3527 case SpvOpAtomicSMin
:
3528 case SpvOpAtomicUMin
:
3529 case SpvOpAtomicSMax
:
3530 case SpvOpAtomicUMax
:
3531 case SpvOpAtomicAnd
:
3533 case SpvOpAtomicXor
:
3534 case SpvOpAtomicFAddEXT
:
3535 fill_common_atomic_sources(b
, opcode
, w
, &atomic
->src
[1]);
3539 vtn_fail_with_opcode("Invalid SPIR-V atomic", opcode
);
3543 /* Atomic ordering operations will implicitly apply to the atomic operation
3544 * storage class, so include that too.
3546 semantics
|= vtn_storage_class_to_memory_semantics(ptr
->ptr_type
->storage_class
);
3548 SpvMemorySemanticsMask before_semantics
;
3549 SpvMemorySemanticsMask after_semantics
;
3550 vtn_split_barrier_semantics(b
, semantics
, &before_semantics
, &after_semantics
);
3552 if (before_semantics
)
3553 vtn_emit_memory_barrier(b
, scope
, before_semantics
);
3555 if (opcode
!= SpvOpAtomicStore
) {
3556 struct vtn_type
*type
= vtn_get_type(b
, w
[1]);
3558 nir_ssa_dest_init(&atomic
->instr
, &atomic
->dest
,
3559 glsl_get_vector_elements(type
->type
),
3560 glsl_get_bit_size(type
->type
), NULL
);
3562 vtn_push_nir_ssa(b
, w
[2], &atomic
->dest
.ssa
);
3565 nir_builder_instr_insert(&b
->nb
, &atomic
->instr
);
3567 if (after_semantics
)
3568 vtn_emit_memory_barrier(b
, scope
, after_semantics
);
3571 static nir_alu_instr
*
3572 create_vec(struct vtn_builder
*b
, unsigned num_components
, unsigned bit_size
)
3574 nir_op op
= nir_op_vec(num_components
);
3575 nir_alu_instr
*vec
= nir_alu_instr_create(b
->shader
, op
);
3576 nir_ssa_dest_init(&vec
->instr
, &vec
->dest
.dest
, num_components
,
3578 vec
->dest
.write_mask
= (1 << num_components
) - 1;
3583 struct vtn_ssa_value
*
3584 vtn_ssa_transpose(struct vtn_builder
*b
, struct vtn_ssa_value
*src
)
3586 if (src
->transposed
)
3587 return src
->transposed
;
3589 struct vtn_ssa_value
*dest
=
3590 vtn_create_ssa_value(b
, glsl_transposed_type(src
->type
));
3592 for (unsigned i
= 0; i
< glsl_get_matrix_columns(dest
->type
); i
++) {
3593 nir_alu_instr
*vec
= create_vec(b
, glsl_get_matrix_columns(src
->type
),
3594 glsl_get_bit_size(src
->type
));
3595 if (glsl_type_is_vector_or_scalar(src
->type
)) {
3596 vec
->src
[0].src
= nir_src_for_ssa(src
->def
);
3597 vec
->src
[0].swizzle
[0] = i
;
3599 for (unsigned j
= 0; j
< glsl_get_matrix_columns(src
->type
); j
++) {
3600 vec
->src
[j
].src
= nir_src_for_ssa(src
->elems
[j
]->def
);
3601 vec
->src
[j
].swizzle
[0] = i
;
3604 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3605 dest
->elems
[i
]->def
= &vec
->dest
.dest
.ssa
;
3608 dest
->transposed
= src
;
3613 static nir_ssa_def
*
3614 vtn_vector_shuffle(struct vtn_builder
*b
, unsigned num_components
,
3615 nir_ssa_def
*src0
, nir_ssa_def
*src1
,
3616 const uint32_t *indices
)
3618 nir_alu_instr
*vec
= create_vec(b
, num_components
, src0
->bit_size
);
3620 for (unsigned i
= 0; i
< num_components
; i
++) {
3621 uint32_t index
= indices
[i
];
3622 if (index
== 0xffffffff) {
3624 nir_src_for_ssa(nir_ssa_undef(&b
->nb
, 1, src0
->bit_size
));
3625 } else if (index
< src0
->num_components
) {
3626 vec
->src
[i
].src
= nir_src_for_ssa(src0
);
3627 vec
->src
[i
].swizzle
[0] = index
;
3629 vec
->src
[i
].src
= nir_src_for_ssa(src1
);
3630 vec
->src
[i
].swizzle
[0] = index
- src0
->num_components
;
3634 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3636 return &vec
->dest
.dest
.ssa
;
3640 * Concatentates a number of vectors/scalars together to produce a vector
3642 static nir_ssa_def
*
3643 vtn_vector_construct(struct vtn_builder
*b
, unsigned num_components
,
3644 unsigned num_srcs
, nir_ssa_def
**srcs
)
3646 nir_alu_instr
*vec
= create_vec(b
, num_components
, srcs
[0]->bit_size
);
3648 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
3650 * "When constructing a vector, there must be at least two Constituent
3653 vtn_assert(num_srcs
>= 2);
3655 unsigned dest_idx
= 0;
3656 for (unsigned i
= 0; i
< num_srcs
; i
++) {
3657 nir_ssa_def
*src
= srcs
[i
];
3658 vtn_assert(dest_idx
+ src
->num_components
<= num_components
);
3659 for (unsigned j
= 0; j
< src
->num_components
; j
++) {
3660 vec
->src
[dest_idx
].src
= nir_src_for_ssa(src
);
3661 vec
->src
[dest_idx
].swizzle
[0] = j
;
3666 /* From the SPIR-V 1.1 spec for OpCompositeConstruct:
3668 * "When constructing a vector, the total number of components in all
3669 * the operands must equal the number of components in Result Type."
3671 vtn_assert(dest_idx
== num_components
);
3673 nir_builder_instr_insert(&b
->nb
, &vec
->instr
);
3675 return &vec
->dest
.dest
.ssa
;
3678 static struct vtn_ssa_value
*
3679 vtn_composite_copy(void *mem_ctx
, struct vtn_ssa_value
*src
)
3681 struct vtn_ssa_value
*dest
= rzalloc(mem_ctx
, struct vtn_ssa_value
);
3682 dest
->type
= src
->type
;
3684 if (glsl_type_is_vector_or_scalar(src
->type
)) {
3685 dest
->def
= src
->def
;
3687 unsigned elems
= glsl_get_length(src
->type
);
3689 dest
->elems
= ralloc_array(mem_ctx
, struct vtn_ssa_value
*, elems
);
3690 for (unsigned i
= 0; i
< elems
; i
++)
3691 dest
->elems
[i
] = vtn_composite_copy(mem_ctx
, src
->elems
[i
]);
3697 static struct vtn_ssa_value
*
3698 vtn_composite_insert(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
3699 struct vtn_ssa_value
*insert
, const uint32_t *indices
,
3700 unsigned num_indices
)
3702 struct vtn_ssa_value
*dest
= vtn_composite_copy(b
, src
);
3704 struct vtn_ssa_value
*cur
= dest
;
3706 for (i
= 0; i
< num_indices
- 1; i
++) {
3707 /* If we got a vector here, that means the next index will be trying to
3708 * dereference a scalar.
3710 vtn_fail_if(glsl_type_is_vector_or_scalar(cur
->type
),
3711 "OpCompositeInsert has too many indices.");
3712 vtn_fail_if(indices
[i
] >= glsl_get_length(cur
->type
),
3713 "All indices in an OpCompositeInsert must be in-bounds");
3714 cur
= cur
->elems
[indices
[i
]];
3717 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
3718 vtn_fail_if(indices
[i
] >= glsl_get_vector_elements(cur
->type
),
3719 "All indices in an OpCompositeInsert must be in-bounds");
3721 /* According to the SPIR-V spec, OpCompositeInsert may work down to
3722 * the component granularity. In that case, the last index will be
3723 * the index to insert the scalar into the vector.
3726 cur
->def
= nir_vector_insert_imm(&b
->nb
, cur
->def
, insert
->def
, indices
[i
]);
3728 vtn_fail_if(indices
[i
] >= glsl_get_length(cur
->type
),
3729 "All indices in an OpCompositeInsert must be in-bounds");
3730 cur
->elems
[indices
[i
]] = insert
;
3736 static struct vtn_ssa_value
*
3737 vtn_composite_extract(struct vtn_builder
*b
, struct vtn_ssa_value
*src
,
3738 const uint32_t *indices
, unsigned num_indices
)
3740 struct vtn_ssa_value
*cur
= src
;
3741 for (unsigned i
= 0; i
< num_indices
; i
++) {
3742 if (glsl_type_is_vector_or_scalar(cur
->type
)) {
3743 vtn_assert(i
== num_indices
- 1);
3744 vtn_fail_if(indices
[i
] >= glsl_get_vector_elements(cur
->type
),
3745 "All indices in an OpCompositeExtract must be in-bounds");
3747 /* According to the SPIR-V spec, OpCompositeExtract may work down to
3748 * the component granularity. The last index will be the index of the
3749 * vector to extract.
3752 const struct glsl_type
*scalar_type
=
3753 glsl_scalar_type(glsl_get_base_type(cur
->type
));
3754 struct vtn_ssa_value
*ret
= vtn_create_ssa_value(b
, scalar_type
);
3755 ret
->def
= nir_channel(&b
->nb
, cur
->def
, indices
[i
]);
3758 vtn_fail_if(indices
[i
] >= glsl_get_length(cur
->type
),
3759 "All indices in an OpCompositeExtract must be in-bounds");
3760 cur
= cur
->elems
[indices
[i
]];
3768 vtn_handle_composite(struct vtn_builder
*b
, SpvOp opcode
,
3769 const uint32_t *w
, unsigned count
)
3771 struct vtn_type
*type
= vtn_get_type(b
, w
[1]);
3772 struct vtn_ssa_value
*ssa
= vtn_create_ssa_value(b
, type
->type
);
3775 case SpvOpVectorExtractDynamic
:
3776 ssa
->def
= nir_vector_extract(&b
->nb
, vtn_get_nir_ssa(b
, w
[3]),
3777 vtn_get_nir_ssa(b
, w
[4]));
3780 case SpvOpVectorInsertDynamic
:
3781 ssa
->def
= nir_vector_insert(&b
->nb
, vtn_get_nir_ssa(b
, w
[3]),
3782 vtn_get_nir_ssa(b
, w
[4]),
3783 vtn_get_nir_ssa(b
, w
[5]));
3786 case SpvOpVectorShuffle
:
3787 ssa
->def
= vtn_vector_shuffle(b
, glsl_get_vector_elements(type
->type
),
3788 vtn_get_nir_ssa(b
, w
[3]),
3789 vtn_get_nir_ssa(b
, w
[4]),
3793 case SpvOpCompositeConstruct
: {
3794 unsigned elems
= count
- 3;
3796 if (glsl_type_is_vector_or_scalar(type
->type
)) {
3797 nir_ssa_def
*srcs
[NIR_MAX_VEC_COMPONENTS
];
3798 for (unsigned i
= 0; i
< elems
; i
++)
3799 srcs
[i
] = vtn_get_nir_ssa(b
, w
[3 + i
]);
3801 vtn_vector_construct(b
, glsl_get_vector_elements(type
->type
),
3804 ssa
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
3805 for (unsigned i
= 0; i
< elems
; i
++)
3806 ssa
->elems
[i
] = vtn_ssa_value(b
, w
[3 + i
]);
3810 case SpvOpCompositeExtract
:
3811 ssa
= vtn_composite_extract(b
, vtn_ssa_value(b
, w
[3]),
3815 case SpvOpCompositeInsert
:
3816 ssa
= vtn_composite_insert(b
, vtn_ssa_value(b
, w
[4]),
3817 vtn_ssa_value(b
, w
[3]),
3821 case SpvOpCopyLogical
:
3822 ssa
= vtn_composite_copy(b
, vtn_ssa_value(b
, w
[3]));
3824 case SpvOpCopyObject
:
3825 vtn_copy_value(b
, w
[3], w
[2]);
3829 vtn_fail_with_opcode("unknown composite operation", opcode
);
3832 vtn_push_ssa_value(b
, w
[2], ssa
);
3836 vtn_emit_barrier(struct vtn_builder
*b
, nir_intrinsic_op op
)
3838 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(b
->shader
, op
);
3839 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3843 vtn_emit_memory_barrier(struct vtn_builder
*b
, SpvScope scope
,
3844 SpvMemorySemanticsMask semantics
)
3846 if (b
->shader
->options
->use_scoped_barrier
) {
3847 vtn_emit_scoped_memory_barrier(b
, scope
, semantics
);
3851 static const SpvMemorySemanticsMask all_memory_semantics
=
3852 SpvMemorySemanticsUniformMemoryMask
|
3853 SpvMemorySemanticsWorkgroupMemoryMask
|
3854 SpvMemorySemanticsAtomicCounterMemoryMask
|
3855 SpvMemorySemanticsImageMemoryMask
|
3856 SpvMemorySemanticsOutputMemoryMask
;
3858 /* If we're not actually doing a memory barrier, bail */
3859 if (!(semantics
& all_memory_semantics
))
3862 /* GL and Vulkan don't have these */
3863 vtn_assert(scope
!= SpvScopeCrossDevice
);
3865 if (scope
== SpvScopeSubgroup
)
3866 return; /* Nothing to do here */
3868 if (scope
== SpvScopeWorkgroup
) {
3869 vtn_emit_barrier(b
, nir_intrinsic_group_memory_barrier
);
3873 /* There's only two scopes thing left */
3874 vtn_assert(scope
== SpvScopeInvocation
|| scope
== SpvScopeDevice
);
3876 /* Map the GLSL memoryBarrier() construct and any barriers with more than one
3877 * semantic to the corresponding NIR one.
3879 if (util_bitcount(semantics
& all_memory_semantics
) > 1) {
3880 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier
);
3881 if (semantics
& SpvMemorySemanticsOutputMemoryMask
) {
3882 /* GLSL memoryBarrier() (and the corresponding NIR one) doesn't include
3883 * TCS outputs, so we have to emit it's own intrinsic for that. We
3884 * then need to emit another memory_barrier to prevent moving
3885 * non-output operations to before the tcs_patch barrier.
3887 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_tcs_patch
);
3888 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier
);
3893 /* Issue a more specific barrier */
3894 switch (semantics
& all_memory_semantics
) {
3895 case SpvMemorySemanticsUniformMemoryMask
:
3896 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_buffer
);
3898 case SpvMemorySemanticsWorkgroupMemoryMask
:
3899 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_shared
);
3901 case SpvMemorySemanticsAtomicCounterMemoryMask
:
3902 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_atomic_counter
);
3904 case SpvMemorySemanticsImageMemoryMask
:
3905 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_image
);
3907 case SpvMemorySemanticsOutputMemoryMask
:
3908 if (b
->nb
.shader
->info
.stage
== MESA_SHADER_TESS_CTRL
)
3909 vtn_emit_barrier(b
, nir_intrinsic_memory_barrier_tcs_patch
);
3917 vtn_handle_barrier(struct vtn_builder
*b
, SpvOp opcode
,
3918 const uint32_t *w
, UNUSED
unsigned count
)
3921 case SpvOpEmitVertex
:
3922 case SpvOpEmitStreamVertex
:
3923 case SpvOpEndPrimitive
:
3924 case SpvOpEndStreamPrimitive
: {
3925 nir_intrinsic_op intrinsic_op
;
3927 case SpvOpEmitVertex
:
3928 case SpvOpEmitStreamVertex
:
3929 intrinsic_op
= nir_intrinsic_emit_vertex
;
3931 case SpvOpEndPrimitive
:
3932 case SpvOpEndStreamPrimitive
:
3933 intrinsic_op
= nir_intrinsic_end_primitive
;
3936 unreachable("Invalid opcode");
3939 nir_intrinsic_instr
*intrin
=
3940 nir_intrinsic_instr_create(b
->shader
, intrinsic_op
);
3943 case SpvOpEmitStreamVertex
:
3944 case SpvOpEndStreamPrimitive
: {
3945 unsigned stream
= vtn_constant_uint(b
, w
[1]);
3946 nir_intrinsic_set_stream_id(intrin
, stream
);
3954 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
3958 case SpvOpMemoryBarrier
: {
3959 SpvScope scope
= vtn_constant_uint(b
, w
[1]);
3960 SpvMemorySemanticsMask semantics
= vtn_constant_uint(b
, w
[2]);
3961 vtn_emit_memory_barrier(b
, scope
, semantics
);
3965 case SpvOpControlBarrier
: {
3966 SpvScope execution_scope
= vtn_constant_uint(b
, w
[1]);
3967 SpvScope memory_scope
= vtn_constant_uint(b
, w
[2]);
3968 SpvMemorySemanticsMask memory_semantics
= vtn_constant_uint(b
, w
[3]);
3970 /* GLSLang, prior to commit 8297936dd6eb3, emitted OpControlBarrier with
3971 * memory semantics of None for GLSL barrier().
3972 * And before that, prior to c3f1cdfa, emitted the OpControlBarrier with
3973 * Device instead of Workgroup for execution scope.
3975 if (b
->wa_glslang_cs_barrier
&&
3976 b
->nb
.shader
->info
.stage
== MESA_SHADER_COMPUTE
&&
3977 (execution_scope
== SpvScopeWorkgroup
||
3978 execution_scope
== SpvScopeDevice
) &&
3979 memory_semantics
== SpvMemorySemanticsMaskNone
) {
3980 execution_scope
= SpvScopeWorkgroup
;
3981 memory_scope
= SpvScopeWorkgroup
;
3982 memory_semantics
= SpvMemorySemanticsAcquireReleaseMask
|
3983 SpvMemorySemanticsWorkgroupMemoryMask
;
3986 /* From the SPIR-V spec:
3988 * "When used with the TessellationControl execution model, it also
3989 * implicitly synchronizes the Output Storage Class: Writes to Output
3990 * variables performed by any invocation executed prior to a
3991 * OpControlBarrier will be visible to any other invocation after
3992 * return from that OpControlBarrier."
3994 if (b
->nb
.shader
->info
.stage
== MESA_SHADER_TESS_CTRL
) {
3995 memory_semantics
&= ~(SpvMemorySemanticsAcquireMask
|
3996 SpvMemorySemanticsReleaseMask
|
3997 SpvMemorySemanticsAcquireReleaseMask
|
3998 SpvMemorySemanticsSequentiallyConsistentMask
);
3999 memory_semantics
|= SpvMemorySemanticsAcquireReleaseMask
|
4000 SpvMemorySemanticsOutputMemoryMask
;
4003 if (b
->shader
->options
->use_scoped_barrier
) {
4004 vtn_emit_scoped_control_barrier(b
, execution_scope
, memory_scope
,
4007 vtn_emit_memory_barrier(b
, memory_scope
, memory_semantics
);
4009 if (execution_scope
== SpvScopeWorkgroup
)
4010 vtn_emit_barrier(b
, nir_intrinsic_control_barrier
);
4016 unreachable("unknown barrier instruction");
4021 gl_primitive_from_spv_execution_mode(struct vtn_builder
*b
,
4022 SpvExecutionMode mode
)
4025 case SpvExecutionModeInputPoints
:
4026 case SpvExecutionModeOutputPoints
:
4027 return 0; /* GL_POINTS */
4028 case SpvExecutionModeInputLines
:
4029 return 1; /* GL_LINES */
4030 case SpvExecutionModeInputLinesAdjacency
:
4031 return 0x000A; /* GL_LINE_STRIP_ADJACENCY_ARB */
4032 case SpvExecutionModeTriangles
:
4033 return 4; /* GL_TRIANGLES */
4034 case SpvExecutionModeInputTrianglesAdjacency
:
4035 return 0x000C; /* GL_TRIANGLES_ADJACENCY_ARB */
4036 case SpvExecutionModeQuads
:
4037 return 7; /* GL_QUADS */
4038 case SpvExecutionModeIsolines
:
4039 return 0x8E7A; /* GL_ISOLINES */
4040 case SpvExecutionModeOutputLineStrip
:
4041 return 3; /* GL_LINE_STRIP */
4042 case SpvExecutionModeOutputTriangleStrip
:
4043 return 5; /* GL_TRIANGLE_STRIP */
4045 vtn_fail("Invalid primitive type: %s (%u)",
4046 spirv_executionmode_to_string(mode
), mode
);
4051 vertices_in_from_spv_execution_mode(struct vtn_builder
*b
,
4052 SpvExecutionMode mode
)
4055 case SpvExecutionModeInputPoints
:
4057 case SpvExecutionModeInputLines
:
4059 case SpvExecutionModeInputLinesAdjacency
:
4061 case SpvExecutionModeTriangles
:
4063 case SpvExecutionModeInputTrianglesAdjacency
:
4066 vtn_fail("Invalid GS input mode: %s (%u)",
4067 spirv_executionmode_to_string(mode
), mode
);
4071 static gl_shader_stage
4072 stage_for_execution_model(struct vtn_builder
*b
, SpvExecutionModel model
)
4075 case SpvExecutionModelVertex
:
4076 return MESA_SHADER_VERTEX
;
4077 case SpvExecutionModelTessellationControl
:
4078 return MESA_SHADER_TESS_CTRL
;
4079 case SpvExecutionModelTessellationEvaluation
:
4080 return MESA_SHADER_TESS_EVAL
;
4081 case SpvExecutionModelGeometry
:
4082 return MESA_SHADER_GEOMETRY
;
4083 case SpvExecutionModelFragment
:
4084 return MESA_SHADER_FRAGMENT
;
4085 case SpvExecutionModelGLCompute
:
4086 return MESA_SHADER_COMPUTE
;
4087 case SpvExecutionModelKernel
:
4088 return MESA_SHADER_KERNEL
;
4090 vtn_fail("Unsupported execution model: %s (%u)",
4091 spirv_executionmodel_to_string(model
), model
);
4095 #define spv_check_supported(name, cap) do { \
4096 if (!(b->options && b->options->caps.name)) \
4097 vtn_warn("Unsupported SPIR-V capability: %s (%u)", \
4098 spirv_capability_to_string(cap), cap); \
4103 vtn_handle_entry_point(struct vtn_builder
*b
, const uint32_t *w
,
4106 struct vtn_value
*entry_point
= &b
->values
[w
[2]];
4107 /* Let this be a name label regardless */
4108 unsigned name_words
;
4109 entry_point
->name
= vtn_string_literal(b
, &w
[3], count
- 3, &name_words
);
4111 if (strcmp(entry_point
->name
, b
->entry_point_name
) != 0 ||
4112 stage_for_execution_model(b
, w
[1]) != b
->entry_point_stage
)
4115 vtn_assert(b
->entry_point
== NULL
);
4116 b
->entry_point
= entry_point
;
4120 vtn_handle_preamble_instruction(struct vtn_builder
*b
, SpvOp opcode
,
4121 const uint32_t *w
, unsigned count
)
4128 case SpvSourceLanguageUnknown
: lang
= "unknown"; break;
4129 case SpvSourceLanguageESSL
: lang
= "ESSL"; break;
4130 case SpvSourceLanguageGLSL
: lang
= "GLSL"; break;
4131 case SpvSourceLanguageOpenCL_C
: lang
= "OpenCL C"; break;
4132 case SpvSourceLanguageOpenCL_CPP
: lang
= "OpenCL C++"; break;
4133 case SpvSourceLanguageHLSL
: lang
= "HLSL"; break;
4136 uint32_t version
= w
[2];
4139 (count
> 3) ? vtn_value(b
, w
[3], vtn_value_type_string
)->str
: "";
4141 vtn_info("Parsing SPIR-V from %s %u source file %s", lang
, version
, file
);
4145 case SpvOpSourceExtension
:
4146 case SpvOpSourceContinued
:
4147 case SpvOpExtension
:
4148 case SpvOpModuleProcessed
:
4149 /* Unhandled, but these are for debug so that's ok. */
4152 case SpvOpCapability
: {
4153 SpvCapability cap
= w
[1];
4155 case SpvCapabilityMatrix
:
4156 case SpvCapabilityShader
:
4157 case SpvCapabilityGeometry
:
4158 case SpvCapabilityGeometryPointSize
:
4159 case SpvCapabilityUniformBufferArrayDynamicIndexing
:
4160 case SpvCapabilitySampledImageArrayDynamicIndexing
:
4161 case SpvCapabilityStorageBufferArrayDynamicIndexing
:
4162 case SpvCapabilityStorageImageArrayDynamicIndexing
:
4163 case SpvCapabilityImageRect
:
4164 case SpvCapabilitySampledRect
:
4165 case SpvCapabilitySampled1D
:
4166 case SpvCapabilityImage1D
:
4167 case SpvCapabilitySampledCubeArray
:
4168 case SpvCapabilityImageCubeArray
:
4169 case SpvCapabilitySampledBuffer
:
4170 case SpvCapabilityImageBuffer
:
4171 case SpvCapabilityImageQuery
:
4172 case SpvCapabilityDerivativeControl
:
4173 case SpvCapabilityInterpolationFunction
:
4174 case SpvCapabilityMultiViewport
:
4175 case SpvCapabilitySampleRateShading
:
4176 case SpvCapabilityClipDistance
:
4177 case SpvCapabilityCullDistance
:
4178 case SpvCapabilityInputAttachment
:
4179 case SpvCapabilityImageGatherExtended
:
4180 case SpvCapabilityStorageImageExtendedFormats
:
4181 case SpvCapabilityVector16
:
4184 case SpvCapabilityLinkage
:
4185 case SpvCapabilityFloat16Buffer
:
4186 case SpvCapabilitySparseResidency
:
4187 vtn_warn("Unsupported SPIR-V capability: %s",
4188 spirv_capability_to_string(cap
));
4191 case SpvCapabilityMinLod
:
4192 spv_check_supported(min_lod
, cap
);
4195 case SpvCapabilityAtomicStorage
:
4196 spv_check_supported(atomic_storage
, cap
);
4199 case SpvCapabilityFloat64
:
4200 spv_check_supported(float64
, cap
);
4202 case SpvCapabilityInt64
:
4203 spv_check_supported(int64
, cap
);
4205 case SpvCapabilityInt16
:
4206 spv_check_supported(int16
, cap
);
4208 case SpvCapabilityInt8
:
4209 spv_check_supported(int8
, cap
);
4212 case SpvCapabilityTransformFeedback
:
4213 spv_check_supported(transform_feedback
, cap
);
4216 case SpvCapabilityGeometryStreams
:
4217 spv_check_supported(geometry_streams
, cap
);
4220 case SpvCapabilityInt64Atomics
:
4221 spv_check_supported(int64_atomics
, cap
);
4224 case SpvCapabilityStorageImageMultisample
:
4225 spv_check_supported(storage_image_ms
, cap
);
4228 case SpvCapabilityAddresses
:
4229 spv_check_supported(address
, cap
);
4232 case SpvCapabilityKernel
:
4233 spv_check_supported(kernel
, cap
);
4236 case SpvCapabilityImageBasic
:
4237 spv_check_supported(kernel_image
, cap
);
4240 case SpvCapabilityImageReadWrite
:
4241 case SpvCapabilityImageMipmap
:
4242 case SpvCapabilityPipes
:
4243 case SpvCapabilityDeviceEnqueue
:
4244 case SpvCapabilityLiteralSampler
:
4245 case SpvCapabilityGenericPointer
:
4246 vtn_warn("Unsupported OpenCL-style SPIR-V capability: %s",
4247 spirv_capability_to_string(cap
));
4250 case SpvCapabilityImageMSArray
:
4251 spv_check_supported(image_ms_array
, cap
);
4254 case SpvCapabilityTessellation
:
4255 case SpvCapabilityTessellationPointSize
:
4256 spv_check_supported(tessellation
, cap
);
4259 case SpvCapabilityDrawParameters
:
4260 spv_check_supported(draw_parameters
, cap
);
4263 case SpvCapabilityStorageImageReadWithoutFormat
:
4264 spv_check_supported(image_read_without_format
, cap
);
4267 case SpvCapabilityStorageImageWriteWithoutFormat
:
4268 spv_check_supported(image_write_without_format
, cap
);
4271 case SpvCapabilityDeviceGroup
:
4272 spv_check_supported(device_group
, cap
);
4275 case SpvCapabilityMultiView
:
4276 spv_check_supported(multiview
, cap
);
4279 case SpvCapabilityGroupNonUniform
:
4280 spv_check_supported(subgroup_basic
, cap
);
4283 case SpvCapabilitySubgroupVoteKHR
:
4284 case SpvCapabilityGroupNonUniformVote
:
4285 spv_check_supported(subgroup_vote
, cap
);
4288 case SpvCapabilitySubgroupBallotKHR
:
4289 case SpvCapabilityGroupNonUniformBallot
:
4290 spv_check_supported(subgroup_ballot
, cap
);
4293 case SpvCapabilityGroupNonUniformShuffle
:
4294 case SpvCapabilityGroupNonUniformShuffleRelative
:
4295 spv_check_supported(subgroup_shuffle
, cap
);
4298 case SpvCapabilityGroupNonUniformQuad
:
4299 spv_check_supported(subgroup_quad
, cap
);
4302 case SpvCapabilityGroupNonUniformArithmetic
:
4303 case SpvCapabilityGroupNonUniformClustered
:
4304 spv_check_supported(subgroup_arithmetic
, cap
);
4307 case SpvCapabilityGroups
:
4308 spv_check_supported(amd_shader_ballot
, cap
);
4311 case SpvCapabilityVariablePointersStorageBuffer
:
4312 case SpvCapabilityVariablePointers
:
4313 spv_check_supported(variable_pointers
, cap
);
4314 b
->variable_pointers
= true;
4317 case SpvCapabilityStorageUniformBufferBlock16
:
4318 case SpvCapabilityStorageUniform16
:
4319 case SpvCapabilityStoragePushConstant16
:
4320 case SpvCapabilityStorageInputOutput16
:
4321 spv_check_supported(storage_16bit
, cap
);
4324 case SpvCapabilityShaderLayer
:
4325 case SpvCapabilityShaderViewportIndex
:
4326 case SpvCapabilityShaderViewportIndexLayerEXT
:
4327 spv_check_supported(shader_viewport_index_layer
, cap
);
4330 case SpvCapabilityStorageBuffer8BitAccess
:
4331 case SpvCapabilityUniformAndStorageBuffer8BitAccess
:
4332 case SpvCapabilityStoragePushConstant8
:
4333 spv_check_supported(storage_8bit
, cap
);
4336 case SpvCapabilityShaderNonUniformEXT
:
4337 spv_check_supported(descriptor_indexing
, cap
);
4340 case SpvCapabilityInputAttachmentArrayDynamicIndexingEXT
:
4341 case SpvCapabilityUniformTexelBufferArrayDynamicIndexingEXT
:
4342 case SpvCapabilityStorageTexelBufferArrayDynamicIndexingEXT
:
4343 spv_check_supported(descriptor_array_dynamic_indexing
, cap
);
4346 case SpvCapabilityUniformBufferArrayNonUniformIndexingEXT
:
4347 case SpvCapabilitySampledImageArrayNonUniformIndexingEXT
:
4348 case SpvCapabilityStorageBufferArrayNonUniformIndexingEXT
:
4349 case SpvCapabilityStorageImageArrayNonUniformIndexingEXT
:
4350 case SpvCapabilityInputAttachmentArrayNonUniformIndexingEXT
:
4351 case SpvCapabilityUniformTexelBufferArrayNonUniformIndexingEXT
:
4352 case SpvCapabilityStorageTexelBufferArrayNonUniformIndexingEXT
:
4353 spv_check_supported(descriptor_array_non_uniform_indexing
, cap
);
4356 case SpvCapabilityRuntimeDescriptorArrayEXT
:
4357 spv_check_supported(runtime_descriptor_array
, cap
);
4360 case SpvCapabilityStencilExportEXT
:
4361 spv_check_supported(stencil_export
, cap
);
4364 case SpvCapabilitySampleMaskPostDepthCoverage
:
4365 spv_check_supported(post_depth_coverage
, cap
);
4368 case SpvCapabilityDenormFlushToZero
:
4369 case SpvCapabilityDenormPreserve
:
4370 case SpvCapabilitySignedZeroInfNanPreserve
:
4371 case SpvCapabilityRoundingModeRTE
:
4372 case SpvCapabilityRoundingModeRTZ
:
4373 spv_check_supported(float_controls
, cap
);
4376 case SpvCapabilityPhysicalStorageBufferAddresses
:
4377 spv_check_supported(physical_storage_buffer_address
, cap
);
4380 case SpvCapabilityComputeDerivativeGroupQuadsNV
:
4381 case SpvCapabilityComputeDerivativeGroupLinearNV
:
4382 spv_check_supported(derivative_group
, cap
);
4385 case SpvCapabilityFloat16
:
4386 spv_check_supported(float16
, cap
);
4389 case SpvCapabilityFragmentShaderSampleInterlockEXT
:
4390 spv_check_supported(fragment_shader_sample_interlock
, cap
);
4393 case SpvCapabilityFragmentShaderPixelInterlockEXT
:
4394 spv_check_supported(fragment_shader_pixel_interlock
, cap
);
4397 case SpvCapabilityDemoteToHelperInvocationEXT
:
4398 spv_check_supported(demote_to_helper_invocation
, cap
);
4401 case SpvCapabilityShaderClockKHR
:
4402 spv_check_supported(shader_clock
, cap
);
4405 case SpvCapabilityVulkanMemoryModel
:
4406 spv_check_supported(vk_memory_model
, cap
);
4409 case SpvCapabilityVulkanMemoryModelDeviceScope
:
4410 spv_check_supported(vk_memory_model_device_scope
, cap
);
4413 case SpvCapabilityImageReadWriteLodAMD
:
4414 spv_check_supported(amd_image_read_write_lod
, cap
);
4417 case SpvCapabilityIntegerFunctions2INTEL
:
4418 spv_check_supported(integer_functions2
, cap
);
4421 case SpvCapabilityFragmentMaskAMD
:
4422 spv_check_supported(amd_fragment_mask
, cap
);
4425 case SpvCapabilityImageGatherBiasLodAMD
:
4426 spv_check_supported(amd_image_gather_bias_lod
, cap
);
4429 case SpvCapabilityAtomicFloat32AddEXT
:
4430 spv_check_supported(float32_atomic_add
, cap
);
4433 case SpvCapabilityAtomicFloat64AddEXT
:
4434 spv_check_supported(float64_atomic_add
, cap
);
4438 vtn_fail("Unhandled capability: %s (%u)",
4439 spirv_capability_to_string(cap
), cap
);
4444 case SpvOpExtInstImport
:
4445 vtn_handle_extension(b
, opcode
, w
, count
);
4448 case SpvOpMemoryModel
:
4450 case SpvAddressingModelPhysical32
:
4451 vtn_fail_if(b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
,
4452 "AddressingModelPhysical32 only supported for kernels");
4453 b
->shader
->info
.cs
.ptr_size
= 32;
4454 b
->physical_ptrs
= true;
4455 assert(nir_address_format_bit_size(b
->options
->global_addr_format
) == 32);
4456 assert(nir_address_format_num_components(b
->options
->global_addr_format
) == 1);
4457 assert(nir_address_format_bit_size(b
->options
->shared_addr_format
) == 32);
4458 assert(nir_address_format_num_components(b
->options
->shared_addr_format
) == 1);
4459 if (!b
->options
->constant_as_global
) {
4460 assert(nir_address_format_bit_size(b
->options
->ubo_addr_format
) == 32);
4461 assert(nir_address_format_num_components(b
->options
->ubo_addr_format
) == 1);
4464 case SpvAddressingModelPhysical64
:
4465 vtn_fail_if(b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
,
4466 "AddressingModelPhysical64 only supported for kernels");
4467 b
->shader
->info
.cs
.ptr_size
= 64;
4468 b
->physical_ptrs
= true;
4469 assert(nir_address_format_bit_size(b
->options
->global_addr_format
) == 64);
4470 assert(nir_address_format_num_components(b
->options
->global_addr_format
) == 1);
4471 assert(nir_address_format_bit_size(b
->options
->shared_addr_format
) == 64);
4472 assert(nir_address_format_num_components(b
->options
->shared_addr_format
) == 1);
4473 if (!b
->options
->constant_as_global
) {
4474 assert(nir_address_format_bit_size(b
->options
->ubo_addr_format
) == 64);
4475 assert(nir_address_format_num_components(b
->options
->ubo_addr_format
) == 1);
4478 case SpvAddressingModelLogical
:
4479 vtn_fail_if(b
->shader
->info
.stage
== MESA_SHADER_KERNEL
,
4480 "AddressingModelLogical only supported for shaders");
4481 b
->physical_ptrs
= false;
4483 case SpvAddressingModelPhysicalStorageBuffer64
:
4484 vtn_fail_if(!b
->options
||
4485 !b
->options
->caps
.physical_storage_buffer_address
,
4486 "AddressingModelPhysicalStorageBuffer64 not supported");
4489 vtn_fail("Unknown addressing model: %s (%u)",
4490 spirv_addressingmodel_to_string(w
[1]), w
[1]);
4494 b
->mem_model
= w
[2];
4496 case SpvMemoryModelSimple
:
4497 case SpvMemoryModelGLSL450
:
4498 case SpvMemoryModelOpenCL
:
4500 case SpvMemoryModelVulkan
:
4501 vtn_fail_if(!b
->options
->caps
.vk_memory_model
,
4502 "Vulkan memory model is unsupported by this driver");
4505 vtn_fail("Unsupported memory model: %s",
4506 spirv_memorymodel_to_string(w
[2]));
4511 case SpvOpEntryPoint
:
4512 vtn_handle_entry_point(b
, w
, count
);
4516 vtn_push_value(b
, w
[1], vtn_value_type_string
)->str
=
4517 vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
4521 b
->values
[w
[1]].name
= vtn_string_literal(b
, &w
[2], count
- 2, NULL
);
4524 case SpvOpMemberName
:
4528 case SpvOpExecutionMode
:
4529 case SpvOpExecutionModeId
:
4530 case SpvOpDecorationGroup
:
4532 case SpvOpDecorateId
:
4533 case SpvOpMemberDecorate
:
4534 case SpvOpGroupDecorate
:
4535 case SpvOpGroupMemberDecorate
:
4536 case SpvOpDecorateString
:
4537 case SpvOpMemberDecorateString
:
4538 vtn_handle_decoration(b
, opcode
, w
, count
);
4541 case SpvOpExtInst
: {
4542 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
4543 if (val
->ext_handler
== vtn_handle_non_semantic_instruction
) {
4544 /* NonSemantic extended instructions are acceptable in preamble. */
4545 vtn_handle_non_semantic_instruction(b
, w
[4], w
, count
);
4548 return false; /* End of preamble. */
4553 return false; /* End of preamble */
4560 vtn_handle_execution_mode(struct vtn_builder
*b
, struct vtn_value
*entry_point
,
4561 const struct vtn_decoration
*mode
, UNUSED
void *data
)
4563 vtn_assert(b
->entry_point
== entry_point
);
4565 switch(mode
->exec_mode
) {
4566 case SpvExecutionModeOriginUpperLeft
:
4567 case SpvExecutionModeOriginLowerLeft
:
4568 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4569 b
->shader
->info
.fs
.origin_upper_left
=
4570 (mode
->exec_mode
== SpvExecutionModeOriginUpperLeft
);
4573 case SpvExecutionModeEarlyFragmentTests
:
4574 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4575 b
->shader
->info
.fs
.early_fragment_tests
= true;
4578 case SpvExecutionModePostDepthCoverage
:
4579 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4580 b
->shader
->info
.fs
.post_depth_coverage
= true;
4583 case SpvExecutionModeInvocations
:
4584 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4585 b
->shader
->info
.gs
.invocations
= MAX2(1, mode
->operands
[0]);
4588 case SpvExecutionModeDepthReplacing
:
4589 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4590 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_ANY
;
4592 case SpvExecutionModeDepthGreater
:
4593 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4594 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_GREATER
;
4596 case SpvExecutionModeDepthLess
:
4597 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4598 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_LESS
;
4600 case SpvExecutionModeDepthUnchanged
:
4601 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4602 b
->shader
->info
.fs
.depth_layout
= FRAG_DEPTH_LAYOUT_UNCHANGED
;
4605 case SpvExecutionModeLocalSize
:
4606 vtn_assert(gl_shader_stage_is_compute(b
->shader
->info
.stage
));
4607 b
->shader
->info
.cs
.local_size
[0] = mode
->operands
[0];
4608 b
->shader
->info
.cs
.local_size
[1] = mode
->operands
[1];
4609 b
->shader
->info
.cs
.local_size
[2] = mode
->operands
[2];
4612 case SpvExecutionModeLocalSizeHint
:
4613 break; /* Nothing to do with this */
4615 case SpvExecutionModeOutputVertices
:
4616 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4617 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
4618 b
->shader
->info
.tess
.tcs_vertices_out
= mode
->operands
[0];
4620 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4621 b
->shader
->info
.gs
.vertices_out
= mode
->operands
[0];
4625 case SpvExecutionModeInputPoints
:
4626 case SpvExecutionModeInputLines
:
4627 case SpvExecutionModeInputLinesAdjacency
:
4628 case SpvExecutionModeTriangles
:
4629 case SpvExecutionModeInputTrianglesAdjacency
:
4630 case SpvExecutionModeQuads
:
4631 case SpvExecutionModeIsolines
:
4632 if (b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4633 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
) {
4634 b
->shader
->info
.tess
.primitive_mode
=
4635 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
4637 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4638 b
->shader
->info
.gs
.vertices_in
=
4639 vertices_in_from_spv_execution_mode(b
, mode
->exec_mode
);
4640 b
->shader
->info
.gs
.input_primitive
=
4641 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
4645 case SpvExecutionModeOutputPoints
:
4646 case SpvExecutionModeOutputLineStrip
:
4647 case SpvExecutionModeOutputTriangleStrip
:
4648 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_GEOMETRY
);
4649 b
->shader
->info
.gs
.output_primitive
=
4650 gl_primitive_from_spv_execution_mode(b
, mode
->exec_mode
);
4653 case SpvExecutionModeSpacingEqual
:
4654 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4655 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4656 b
->shader
->info
.tess
.spacing
= TESS_SPACING_EQUAL
;
4658 case SpvExecutionModeSpacingFractionalEven
:
4659 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4660 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4661 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_EVEN
;
4663 case SpvExecutionModeSpacingFractionalOdd
:
4664 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4665 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4666 b
->shader
->info
.tess
.spacing
= TESS_SPACING_FRACTIONAL_ODD
;
4668 case SpvExecutionModeVertexOrderCw
:
4669 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4670 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4671 b
->shader
->info
.tess
.ccw
= false;
4673 case SpvExecutionModeVertexOrderCcw
:
4674 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4675 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4676 b
->shader
->info
.tess
.ccw
= true;
4678 case SpvExecutionModePointMode
:
4679 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_TESS_CTRL
||
4680 b
->shader
->info
.stage
== MESA_SHADER_TESS_EVAL
);
4681 b
->shader
->info
.tess
.point_mode
= true;
4684 case SpvExecutionModePixelCenterInteger
:
4685 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4686 b
->shader
->info
.fs
.pixel_center_integer
= true;
4689 case SpvExecutionModeXfb
:
4690 b
->shader
->info
.has_transform_feedback_varyings
= true;
4693 case SpvExecutionModeVecTypeHint
:
4696 case SpvExecutionModeContractionOff
:
4697 if (b
->shader
->info
.stage
!= MESA_SHADER_KERNEL
)
4698 vtn_warn("ExectionMode only allowed for CL-style kernels: %s",
4699 spirv_executionmode_to_string(mode
->exec_mode
));
4704 case SpvExecutionModeStencilRefReplacingEXT
:
4705 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4708 case SpvExecutionModeDerivativeGroupQuadsNV
:
4709 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_COMPUTE
);
4710 b
->shader
->info
.cs
.derivative_group
= DERIVATIVE_GROUP_QUADS
;
4713 case SpvExecutionModeDerivativeGroupLinearNV
:
4714 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_COMPUTE
);
4715 b
->shader
->info
.cs
.derivative_group
= DERIVATIVE_GROUP_LINEAR
;
4718 case SpvExecutionModePixelInterlockOrderedEXT
:
4719 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4720 b
->shader
->info
.fs
.pixel_interlock_ordered
= true;
4723 case SpvExecutionModePixelInterlockUnorderedEXT
:
4724 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4725 b
->shader
->info
.fs
.pixel_interlock_unordered
= true;
4728 case SpvExecutionModeSampleInterlockOrderedEXT
:
4729 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4730 b
->shader
->info
.fs
.sample_interlock_ordered
= true;
4733 case SpvExecutionModeSampleInterlockUnorderedEXT
:
4734 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_FRAGMENT
);
4735 b
->shader
->info
.fs
.sample_interlock_unordered
= true;
4738 case SpvExecutionModeDenormPreserve
:
4739 case SpvExecutionModeDenormFlushToZero
:
4740 case SpvExecutionModeSignedZeroInfNanPreserve
:
4741 case SpvExecutionModeRoundingModeRTE
:
4742 case SpvExecutionModeRoundingModeRTZ
: {
4743 unsigned execution_mode
= 0;
4744 switch (mode
->exec_mode
) {
4745 case SpvExecutionModeDenormPreserve
:
4746 switch (mode
->operands
[0]) {
4747 case 16: execution_mode
= FLOAT_CONTROLS_DENORM_PRESERVE_FP16
; break;
4748 case 32: execution_mode
= FLOAT_CONTROLS_DENORM_PRESERVE_FP32
; break;
4749 case 64: execution_mode
= FLOAT_CONTROLS_DENORM_PRESERVE_FP64
; break;
4750 default: vtn_fail("Floating point type not supported");
4753 case SpvExecutionModeDenormFlushToZero
:
4754 switch (mode
->operands
[0]) {
4755 case 16: execution_mode
= FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16
; break;
4756 case 32: execution_mode
= FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32
; break;
4757 case 64: execution_mode
= FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP64
; break;
4758 default: vtn_fail("Floating point type not supported");
4761 case SpvExecutionModeSignedZeroInfNanPreserve
:
4762 switch (mode
->operands
[0]) {
4763 case 16: execution_mode
= FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP16
; break;
4764 case 32: execution_mode
= FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP32
; break;
4765 case 64: execution_mode
= FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP64
; break;
4766 default: vtn_fail("Floating point type not supported");
4769 case SpvExecutionModeRoundingModeRTE
:
4770 switch (mode
->operands
[0]) {
4771 case 16: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16
; break;
4772 case 32: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32
; break;
4773 case 64: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64
; break;
4774 default: vtn_fail("Floating point type not supported");
4777 case SpvExecutionModeRoundingModeRTZ
:
4778 switch (mode
->operands
[0]) {
4779 case 16: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16
; break;
4780 case 32: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32
; break;
4781 case 64: execution_mode
= FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64
; break;
4782 default: vtn_fail("Floating point type not supported");
4789 b
->shader
->info
.float_controls_execution_mode
|= execution_mode
;
4793 case SpvExecutionModeLocalSizeId
:
4794 case SpvExecutionModeLocalSizeHintId
:
4795 /* Handled later by vtn_handle_execution_mode_id(). */
4799 vtn_fail("Unhandled execution mode: %s (%u)",
4800 spirv_executionmode_to_string(mode
->exec_mode
),
4806 vtn_handle_execution_mode_id(struct vtn_builder
*b
, struct vtn_value
*entry_point
,
4807 const struct vtn_decoration
*mode
, UNUSED
void *data
)
4810 vtn_assert(b
->entry_point
== entry_point
);
4812 switch (mode
->exec_mode
) {
4813 case SpvExecutionModeLocalSizeId
:
4814 b
->shader
->info
.cs
.local_size
[0] = vtn_constant_uint(b
, mode
->operands
[0]);
4815 b
->shader
->info
.cs
.local_size
[1] = vtn_constant_uint(b
, mode
->operands
[1]);
4816 b
->shader
->info
.cs
.local_size
[2] = vtn_constant_uint(b
, mode
->operands
[2]);
4819 case SpvExecutionModeLocalSizeHintId
:
4820 /* Nothing to do with this hint. */
4824 /* Nothing to do. Literal execution modes already handled by
4825 * vtn_handle_execution_mode(). */
4831 vtn_handle_variable_or_type_instruction(struct vtn_builder
*b
, SpvOp opcode
,
4832 const uint32_t *w
, unsigned count
)
4834 vtn_set_instruction_result_type(b
, opcode
, w
, count
);
4838 case SpvOpSourceContinued
:
4839 case SpvOpSourceExtension
:
4840 case SpvOpExtension
:
4841 case SpvOpCapability
:
4842 case SpvOpExtInstImport
:
4843 case SpvOpMemoryModel
:
4844 case SpvOpEntryPoint
:
4845 case SpvOpExecutionMode
:
4848 case SpvOpMemberName
:
4849 case SpvOpDecorationGroup
:
4851 case SpvOpDecorateId
:
4852 case SpvOpMemberDecorate
:
4853 case SpvOpGroupDecorate
:
4854 case SpvOpGroupMemberDecorate
:
4855 case SpvOpDecorateString
:
4856 case SpvOpMemberDecorateString
:
4857 vtn_fail("Invalid opcode types and variables section");
4863 case SpvOpTypeFloat
:
4864 case SpvOpTypeVector
:
4865 case SpvOpTypeMatrix
:
4866 case SpvOpTypeImage
:
4867 case SpvOpTypeSampler
:
4868 case SpvOpTypeSampledImage
:
4869 case SpvOpTypeArray
:
4870 case SpvOpTypeRuntimeArray
:
4871 case SpvOpTypeStruct
:
4872 case SpvOpTypeOpaque
:
4873 case SpvOpTypePointer
:
4874 case SpvOpTypeForwardPointer
:
4875 case SpvOpTypeFunction
:
4876 case SpvOpTypeEvent
:
4877 case SpvOpTypeDeviceEvent
:
4878 case SpvOpTypeReserveId
:
4879 case SpvOpTypeQueue
:
4881 vtn_handle_type(b
, opcode
, w
, count
);
4884 case SpvOpConstantTrue
:
4885 case SpvOpConstantFalse
:
4887 case SpvOpConstantComposite
:
4888 case SpvOpConstantSampler
:
4889 case SpvOpConstantNull
:
4890 case SpvOpSpecConstantTrue
:
4891 case SpvOpSpecConstantFalse
:
4892 case SpvOpSpecConstant
:
4893 case SpvOpSpecConstantComposite
:
4894 case SpvOpSpecConstantOp
:
4895 vtn_handle_constant(b
, opcode
, w
, count
);
4900 vtn_handle_variables(b
, opcode
, w
, count
);
4903 case SpvOpExtInst
: {
4904 struct vtn_value
*val
= vtn_value(b
, w
[3], vtn_value_type_extension
);
4905 /* NonSemantic extended instructions are acceptable in preamble, others
4906 * will indicate the end of preamble.
4908 return val
->ext_handler
== vtn_handle_non_semantic_instruction
;
4912 return false; /* End of preamble */
4918 static struct vtn_ssa_value
*
4919 vtn_nir_select(struct vtn_builder
*b
, struct vtn_ssa_value
*src0
,
4920 struct vtn_ssa_value
*src1
, struct vtn_ssa_value
*src2
)
4922 struct vtn_ssa_value
*dest
= rzalloc(b
, struct vtn_ssa_value
);
4923 dest
->type
= src1
->type
;
4925 if (glsl_type_is_vector_or_scalar(src1
->type
)) {
4926 dest
->def
= nir_bcsel(&b
->nb
, src0
->def
, src1
->def
, src2
->def
);
4928 unsigned elems
= glsl_get_length(src1
->type
);
4930 dest
->elems
= ralloc_array(b
, struct vtn_ssa_value
*, elems
);
4931 for (unsigned i
= 0; i
< elems
; i
++) {
4932 dest
->elems
[i
] = vtn_nir_select(b
, src0
,
4933 src1
->elems
[i
], src2
->elems
[i
]);
4941 vtn_handle_select(struct vtn_builder
*b
, SpvOp opcode
,
4942 const uint32_t *w
, unsigned count
)
4944 /* Handle OpSelect up-front here because it needs to be able to handle
4945 * pointers and not just regular vectors and scalars.
4947 struct vtn_value
*res_val
= vtn_untyped_value(b
, w
[2]);
4948 struct vtn_value
*cond_val
= vtn_untyped_value(b
, w
[3]);
4949 struct vtn_value
*obj1_val
= vtn_untyped_value(b
, w
[4]);
4950 struct vtn_value
*obj2_val
= vtn_untyped_value(b
, w
[5]);
4952 vtn_fail_if(obj1_val
->type
!= res_val
->type
||
4953 obj2_val
->type
!= res_val
->type
,
4954 "Object types must match the result type in OpSelect");
4956 vtn_fail_if((cond_val
->type
->base_type
!= vtn_base_type_scalar
&&
4957 cond_val
->type
->base_type
!= vtn_base_type_vector
) ||
4958 !glsl_type_is_boolean(cond_val
->type
->type
),
4959 "OpSelect must have either a vector of booleans or "
4960 "a boolean as Condition type");
4962 vtn_fail_if(cond_val
->type
->base_type
== vtn_base_type_vector
&&
4963 (res_val
->type
->base_type
!= vtn_base_type_vector
||
4964 res_val
->type
->length
!= cond_val
->type
->length
),
4965 "When Condition type in OpSelect is a vector, the Result "
4966 "type must be a vector of the same length");
4968 switch (res_val
->type
->base_type
) {
4969 case vtn_base_type_scalar
:
4970 case vtn_base_type_vector
:
4971 case vtn_base_type_matrix
:
4972 case vtn_base_type_array
:
4973 case vtn_base_type_struct
:
4976 case vtn_base_type_pointer
:
4977 /* We need to have actual storage for pointer types. */
4978 vtn_fail_if(res_val
->type
->type
== NULL
,
4979 "Invalid pointer result type for OpSelect");
4982 vtn_fail("Result type of OpSelect must be a scalar, composite, or pointer");
4985 vtn_push_ssa_value(b
, w
[2],
4986 vtn_nir_select(b
, vtn_ssa_value(b
, w
[3]),
4987 vtn_ssa_value(b
, w
[4]),
4988 vtn_ssa_value(b
, w
[5])));
4992 vtn_handle_ptr(struct vtn_builder
*b
, SpvOp opcode
,
4993 const uint32_t *w
, unsigned count
)
4995 struct vtn_type
*type1
= vtn_get_value_type(b
, w
[3]);
4996 struct vtn_type
*type2
= vtn_get_value_type(b
, w
[4]);
4997 vtn_fail_if(type1
->base_type
!= vtn_base_type_pointer
||
4998 type2
->base_type
!= vtn_base_type_pointer
,
4999 "%s operands must have pointer types",
5000 spirv_op_to_string(opcode
));
5001 vtn_fail_if(type1
->storage_class
!= type2
->storage_class
,
5002 "%s operands must have the same storage class",
5003 spirv_op_to_string(opcode
));
5005 struct vtn_type
*vtn_type
= vtn_get_type(b
, w
[1]);
5006 const struct glsl_type
*type
= vtn_type
->type
;
5008 nir_address_format addr_format
= vtn_mode_to_address_format(
5009 b
, vtn_storage_class_to_mode(b
, type1
->storage_class
, NULL
, NULL
));
5014 case SpvOpPtrDiff
: {
5015 /* OpPtrDiff returns the difference in number of elements (not byte offset). */
5016 unsigned elem_size
, elem_align
;
5017 glsl_get_natural_size_align_bytes(type1
->deref
->type
,
5018 &elem_size
, &elem_align
);
5020 def
= nir_build_addr_isub(&b
->nb
,
5021 vtn_get_nir_ssa(b
, w
[3]),
5022 vtn_get_nir_ssa(b
, w
[4]),
5024 def
= nir_idiv(&b
->nb
, def
, nir_imm_intN_t(&b
->nb
, elem_size
, def
->bit_size
));
5025 def
= nir_i2i(&b
->nb
, def
, glsl_get_bit_size(type
));
5030 case SpvOpPtrNotEqual
: {
5031 def
= nir_build_addr_ieq(&b
->nb
,
5032 vtn_get_nir_ssa(b
, w
[3]),
5033 vtn_get_nir_ssa(b
, w
[4]),
5035 if (opcode
== SpvOpPtrNotEqual
)
5036 def
= nir_inot(&b
->nb
, def
);
5041 unreachable("Invalid ptr operation");
5044 vtn_push_nir_ssa(b
, w
[2], def
);
5048 vtn_handle_body_instruction(struct vtn_builder
*b
, SpvOp opcode
,
5049 const uint32_t *w
, unsigned count
)
5055 case SpvOpLoopMerge
:
5056 case SpvOpSelectionMerge
:
5057 /* This is handled by cfg pre-pass and walk_blocks */
5061 struct vtn_value
*val
= vtn_push_value(b
, w
[2], vtn_value_type_undef
);
5062 val
->type
= vtn_get_type(b
, w
[1]);
5067 vtn_handle_extension(b
, opcode
, w
, count
);
5073 case SpvOpCopyMemory
:
5074 case SpvOpCopyMemorySized
:
5075 case SpvOpAccessChain
:
5076 case SpvOpPtrAccessChain
:
5077 case SpvOpInBoundsAccessChain
:
5078 case SpvOpInBoundsPtrAccessChain
:
5079 case SpvOpArrayLength
:
5080 case SpvOpConvertPtrToU
:
5081 case SpvOpConvertUToPtr
:
5082 vtn_handle_variables(b
, opcode
, w
, count
);
5085 case SpvOpFunctionCall
:
5086 vtn_handle_function_call(b
, opcode
, w
, count
);
5089 case SpvOpSampledImage
:
5091 case SpvOpImageSampleImplicitLod
:
5092 case SpvOpImageSampleExplicitLod
:
5093 case SpvOpImageSampleDrefImplicitLod
:
5094 case SpvOpImageSampleDrefExplicitLod
:
5095 case SpvOpImageSampleProjImplicitLod
:
5096 case SpvOpImageSampleProjExplicitLod
:
5097 case SpvOpImageSampleProjDrefImplicitLod
:
5098 case SpvOpImageSampleProjDrefExplicitLod
:
5099 case SpvOpImageFetch
:
5100 case SpvOpImageGather
:
5101 case SpvOpImageDrefGather
:
5102 case SpvOpImageQueryLod
:
5103 case SpvOpImageQueryLevels
:
5104 case SpvOpImageQuerySamples
:
5105 vtn_handle_texture(b
, opcode
, w
, count
);
5108 case SpvOpImageRead
:
5109 case SpvOpImageWrite
:
5110 case SpvOpImageTexelPointer
:
5111 case SpvOpImageQueryFormat
:
5112 case SpvOpImageQueryOrder
:
5113 vtn_handle_image(b
, opcode
, w
, count
);
5116 case SpvOpImageQuerySizeLod
:
5117 case SpvOpImageQuerySize
: {
5118 struct vtn_type
*image_type
= vtn_get_value_type(b
, w
[3]);
5119 vtn_assert(image_type
->base_type
== vtn_base_type_image
);
5120 if (glsl_type_is_image(image_type
->glsl_image
)) {
5121 vtn_handle_image(b
, opcode
, w
, count
);
5123 vtn_assert(glsl_type_is_sampler(image_type
->glsl_image
));
5124 vtn_handle_texture(b
, opcode
, w
, count
);
5129 case SpvOpFragmentMaskFetchAMD
:
5130 case SpvOpFragmentFetchAMD
:
5131 vtn_handle_texture(b
, opcode
, w
, count
);
5134 case SpvOpAtomicLoad
:
5135 case SpvOpAtomicExchange
:
5136 case SpvOpAtomicCompareExchange
:
5137 case SpvOpAtomicCompareExchangeWeak
:
5138 case SpvOpAtomicIIncrement
:
5139 case SpvOpAtomicIDecrement
:
5140 case SpvOpAtomicIAdd
:
5141 case SpvOpAtomicISub
:
5142 case SpvOpAtomicSMin
:
5143 case SpvOpAtomicUMin
:
5144 case SpvOpAtomicSMax
:
5145 case SpvOpAtomicUMax
:
5146 case SpvOpAtomicAnd
:
5148 case SpvOpAtomicXor
:
5149 case SpvOpAtomicFAddEXT
: {
5150 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[3]);
5151 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
5152 vtn_handle_image(b
, opcode
, w
, count
);
5154 vtn_assert(pointer
->value_type
== vtn_value_type_pointer
);
5155 vtn_handle_atomics(b
, opcode
, w
, count
);
5160 case SpvOpAtomicStore
: {
5161 struct vtn_value
*pointer
= vtn_untyped_value(b
, w
[1]);
5162 if (pointer
->value_type
== vtn_value_type_image_pointer
) {
5163 vtn_handle_image(b
, opcode
, w
, count
);
5165 vtn_assert(pointer
->value_type
== vtn_value_type_pointer
);
5166 vtn_handle_atomics(b
, opcode
, w
, count
);
5172 vtn_handle_select(b
, opcode
, w
, count
);
5180 case SpvOpConvertFToU
:
5181 case SpvOpConvertFToS
:
5182 case SpvOpConvertSToF
:
5183 case SpvOpConvertUToF
:
5187 case SpvOpQuantizeToF16
:
5188 case SpvOpPtrCastToGeneric
:
5189 case SpvOpGenericCastToPtr
:
5194 case SpvOpSignBitSet
:
5195 case SpvOpLessOrGreater
:
5197 case SpvOpUnordered
:
5212 case SpvOpVectorTimesScalar
:
5214 case SpvOpIAddCarry
:
5215 case SpvOpISubBorrow
:
5216 case SpvOpUMulExtended
:
5217 case SpvOpSMulExtended
:
5218 case SpvOpShiftRightLogical
:
5219 case SpvOpShiftRightArithmetic
:
5220 case SpvOpShiftLeftLogical
:
5221 case SpvOpLogicalEqual
:
5222 case SpvOpLogicalNotEqual
:
5223 case SpvOpLogicalOr
:
5224 case SpvOpLogicalAnd
:
5225 case SpvOpLogicalNot
:
5226 case SpvOpBitwiseOr
:
5227 case SpvOpBitwiseXor
:
5228 case SpvOpBitwiseAnd
:
5230 case SpvOpFOrdEqual
:
5231 case SpvOpFUnordEqual
:
5232 case SpvOpINotEqual
:
5233 case SpvOpFOrdNotEqual
:
5234 case SpvOpFUnordNotEqual
:
5235 case SpvOpULessThan
:
5236 case SpvOpSLessThan
:
5237 case SpvOpFOrdLessThan
:
5238 case SpvOpFUnordLessThan
:
5239 case SpvOpUGreaterThan
:
5240 case SpvOpSGreaterThan
:
5241 case SpvOpFOrdGreaterThan
:
5242 case SpvOpFUnordGreaterThan
:
5243 case SpvOpULessThanEqual
:
5244 case SpvOpSLessThanEqual
:
5245 case SpvOpFOrdLessThanEqual
:
5246 case SpvOpFUnordLessThanEqual
:
5247 case SpvOpUGreaterThanEqual
:
5248 case SpvOpSGreaterThanEqual
:
5249 case SpvOpFOrdGreaterThanEqual
:
5250 case SpvOpFUnordGreaterThanEqual
:
5256 case SpvOpFwidthFine
:
5257 case SpvOpDPdxCoarse
:
5258 case SpvOpDPdyCoarse
:
5259 case SpvOpFwidthCoarse
:
5260 case SpvOpBitFieldInsert
:
5261 case SpvOpBitFieldSExtract
:
5262 case SpvOpBitFieldUExtract
:
5263 case SpvOpBitReverse
:
5265 case SpvOpTranspose
:
5266 case SpvOpOuterProduct
:
5267 case SpvOpMatrixTimesScalar
:
5268 case SpvOpVectorTimesMatrix
:
5269 case SpvOpMatrixTimesVector
:
5270 case SpvOpMatrixTimesMatrix
:
5271 case SpvOpUCountLeadingZerosINTEL
:
5272 case SpvOpUCountTrailingZerosINTEL
:
5273 case SpvOpAbsISubINTEL
:
5274 case SpvOpAbsUSubINTEL
:
5275 case SpvOpIAddSatINTEL
:
5276 case SpvOpUAddSatINTEL
:
5277 case SpvOpIAverageINTEL
:
5278 case SpvOpUAverageINTEL
:
5279 case SpvOpIAverageRoundedINTEL
:
5280 case SpvOpUAverageRoundedINTEL
:
5281 case SpvOpISubSatINTEL
:
5282 case SpvOpUSubSatINTEL
:
5283 case SpvOpIMul32x16INTEL
:
5284 case SpvOpUMul32x16INTEL
:
5285 vtn_handle_alu(b
, opcode
, w
, count
);
5289 vtn_handle_bitcast(b
, w
, count
);
5292 case SpvOpVectorExtractDynamic
:
5293 case SpvOpVectorInsertDynamic
:
5294 case SpvOpVectorShuffle
:
5295 case SpvOpCompositeConstruct
:
5296 case SpvOpCompositeExtract
:
5297 case SpvOpCompositeInsert
:
5298 case SpvOpCopyLogical
:
5299 case SpvOpCopyObject
:
5300 vtn_handle_composite(b
, opcode
, w
, count
);
5303 case SpvOpEmitVertex
:
5304 case SpvOpEndPrimitive
:
5305 case SpvOpEmitStreamVertex
:
5306 case SpvOpEndStreamPrimitive
:
5307 case SpvOpControlBarrier
:
5308 case SpvOpMemoryBarrier
:
5309 vtn_handle_barrier(b
, opcode
, w
, count
);
5312 case SpvOpGroupNonUniformElect
:
5313 case SpvOpGroupNonUniformAll
:
5314 case SpvOpGroupNonUniformAny
:
5315 case SpvOpGroupNonUniformAllEqual
:
5316 case SpvOpGroupNonUniformBroadcast
:
5317 case SpvOpGroupNonUniformBroadcastFirst
:
5318 case SpvOpGroupNonUniformBallot
:
5319 case SpvOpGroupNonUniformInverseBallot
:
5320 case SpvOpGroupNonUniformBallotBitExtract
:
5321 case SpvOpGroupNonUniformBallotBitCount
:
5322 case SpvOpGroupNonUniformBallotFindLSB
:
5323 case SpvOpGroupNonUniformBallotFindMSB
:
5324 case SpvOpGroupNonUniformShuffle
:
5325 case SpvOpGroupNonUniformShuffleXor
:
5326 case SpvOpGroupNonUniformShuffleUp
:
5327 case SpvOpGroupNonUniformShuffleDown
:
5328 case SpvOpGroupNonUniformIAdd
:
5329 case SpvOpGroupNonUniformFAdd
:
5330 case SpvOpGroupNonUniformIMul
:
5331 case SpvOpGroupNonUniformFMul
:
5332 case SpvOpGroupNonUniformSMin
:
5333 case SpvOpGroupNonUniformUMin
:
5334 case SpvOpGroupNonUniformFMin
:
5335 case SpvOpGroupNonUniformSMax
:
5336 case SpvOpGroupNonUniformUMax
:
5337 case SpvOpGroupNonUniformFMax
:
5338 case SpvOpGroupNonUniformBitwiseAnd
:
5339 case SpvOpGroupNonUniformBitwiseOr
:
5340 case SpvOpGroupNonUniformBitwiseXor
:
5341 case SpvOpGroupNonUniformLogicalAnd
:
5342 case SpvOpGroupNonUniformLogicalOr
:
5343 case SpvOpGroupNonUniformLogicalXor
:
5344 case SpvOpGroupNonUniformQuadBroadcast
:
5345 case SpvOpGroupNonUniformQuadSwap
:
5348 case SpvOpGroupBroadcast
:
5349 case SpvOpGroupIAdd
:
5350 case SpvOpGroupFAdd
:
5351 case SpvOpGroupFMin
:
5352 case SpvOpGroupUMin
:
5353 case SpvOpGroupSMin
:
5354 case SpvOpGroupFMax
:
5355 case SpvOpGroupUMax
:
5356 case SpvOpGroupSMax
:
5357 case SpvOpSubgroupBallotKHR
:
5358 case SpvOpSubgroupFirstInvocationKHR
:
5359 case SpvOpSubgroupReadInvocationKHR
:
5360 case SpvOpSubgroupAllKHR
:
5361 case SpvOpSubgroupAnyKHR
:
5362 case SpvOpSubgroupAllEqualKHR
:
5363 case SpvOpGroupIAddNonUniformAMD
:
5364 case SpvOpGroupFAddNonUniformAMD
:
5365 case SpvOpGroupFMinNonUniformAMD
:
5366 case SpvOpGroupUMinNonUniformAMD
:
5367 case SpvOpGroupSMinNonUniformAMD
:
5368 case SpvOpGroupFMaxNonUniformAMD
:
5369 case SpvOpGroupUMaxNonUniformAMD
:
5370 case SpvOpGroupSMaxNonUniformAMD
:
5371 vtn_handle_subgroup(b
, opcode
, w
, count
);
5376 case SpvOpPtrNotEqual
:
5377 vtn_handle_ptr(b
, opcode
, w
, count
);
5380 case SpvOpBeginInvocationInterlockEXT
:
5381 vtn_emit_barrier(b
, nir_intrinsic_begin_invocation_interlock
);
5384 case SpvOpEndInvocationInterlockEXT
:
5385 vtn_emit_barrier(b
, nir_intrinsic_end_invocation_interlock
);
5388 case SpvOpDemoteToHelperInvocationEXT
: {
5389 nir_intrinsic_instr
*intrin
=
5390 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_demote
);
5391 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
5395 case SpvOpIsHelperInvocationEXT
: {
5396 nir_intrinsic_instr
*intrin
=
5397 nir_intrinsic_instr_create(b
->shader
, nir_intrinsic_is_helper_invocation
);
5398 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
, 1, 1, NULL
);
5399 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
5401 vtn_push_nir_ssa(b
, w
[2], &intrin
->dest
.ssa
);
5405 case SpvOpReadClockKHR
: {
5406 SpvScope scope
= vtn_constant_uint(b
, w
[3]);
5407 nir_scope nir_scope
;
5410 case SpvScopeDevice
:
5411 nir_scope
= NIR_SCOPE_DEVICE
;
5413 case SpvScopeSubgroup
:
5414 nir_scope
= NIR_SCOPE_SUBGROUP
;
5417 vtn_fail("invalid read clock scope");
5420 /* Operation supports two result types: uvec2 and uint64_t. The NIR
5421 * intrinsic gives uvec2, so pack the result for the other case.
5423 nir_intrinsic_instr
*intrin
=
5424 nir_intrinsic_instr_create(b
->nb
.shader
, nir_intrinsic_shader_clock
);
5425 nir_ssa_dest_init(&intrin
->instr
, &intrin
->dest
, 2, 32, NULL
);
5426 nir_intrinsic_set_memory_scope(intrin
, nir_scope
);
5427 nir_builder_instr_insert(&b
->nb
, &intrin
->instr
);
5429 struct vtn_type
*type
= vtn_get_type(b
, w
[1]);
5430 const struct glsl_type
*dest_type
= type
->type
;
5431 nir_ssa_def
*result
;
5433 if (glsl_type_is_vector(dest_type
)) {
5434 assert(dest_type
== glsl_vector_type(GLSL_TYPE_UINT
, 2));
5435 result
= &intrin
->dest
.ssa
;
5437 assert(glsl_type_is_scalar(dest_type
));
5438 assert(glsl_get_base_type(dest_type
) == GLSL_TYPE_UINT64
);
5439 result
= nir_pack_64_2x32(&b
->nb
, &intrin
->dest
.ssa
);
5442 vtn_push_nir_ssa(b
, w
[2], result
);
5446 case SpvOpLifetimeStart
:
5447 case SpvOpLifetimeStop
:
5451 vtn_fail_with_opcode("Unhandled opcode", opcode
);
5458 vtn_create_builder(const uint32_t *words
, size_t word_count
,
5459 gl_shader_stage stage
, const char *entry_point_name
,
5460 const struct spirv_to_nir_options
*options
)
5462 /* Initialize the vtn_builder object */
5463 struct vtn_builder
*b
= rzalloc(NULL
, struct vtn_builder
);
5464 struct spirv_to_nir_options
*dup_options
=
5465 ralloc(b
, struct spirv_to_nir_options
);
5466 *dup_options
= *options
;
5469 b
->spirv_word_count
= word_count
;
5473 list_inithead(&b
->functions
);
5474 b
->entry_point_stage
= stage
;
5475 b
->entry_point_name
= entry_point_name
;
5476 b
->options
= dup_options
;
5479 * Handle the SPIR-V header (first 5 dwords).
5480 * Can't use vtx_assert() as the setjmp(3) target isn't initialized yet.
5482 if (word_count
<= 5)
5485 if (words
[0] != SpvMagicNumber
) {
5486 vtn_err("words[0] was 0x%x, want 0x%x", words
[0], SpvMagicNumber
);
5489 if (words
[1] < 0x10000) {
5490 vtn_err("words[1] was 0x%x, want >= 0x10000", words
[1]);
5494 uint16_t generator_id
= words
[2] >> 16;
5495 uint16_t generator_version
= words
[2];
5497 /* In GLSLang commit 8297936dd6eb3, their handling of barrier() was fixed
5498 * to provide correct memory semantics on compute shader barrier()
5499 * commands. Prior to that, we need to fix them up ourselves. This
5500 * GLSLang fix caused them to bump to generator version 3.
5502 b
->wa_glslang_cs_barrier
= (generator_id
== 8 && generator_version
< 3);
5504 /* words[2] == generator magic */
5505 unsigned value_id_bound
= words
[3];
5506 if (words
[4] != 0) {
5507 vtn_err("words[4] was %u, want 0", words
[4]);
5511 b
->value_id_bound
= value_id_bound
;
5512 b
->values
= rzalloc_array(b
, struct vtn_value
, value_id_bound
);
5520 static nir_function
*
5521 vtn_emit_kernel_entry_point_wrapper(struct vtn_builder
*b
,
5522 nir_function
*entry_point
)
5524 vtn_assert(entry_point
== b
->entry_point
->func
->impl
->function
);
5525 vtn_fail_if(!entry_point
->name
, "entry points are required to have a name");
5526 const char *func_name
=
5527 ralloc_asprintf(b
->shader
, "__wrapped_%s", entry_point
->name
);
5529 /* we shouldn't have any inputs yet */
5530 vtn_assert(!entry_point
->shader
->num_inputs
);
5531 vtn_assert(b
->shader
->info
.stage
== MESA_SHADER_KERNEL
);
5533 nir_function
*main_entry_point
= nir_function_create(b
->shader
, func_name
);
5534 main_entry_point
->impl
= nir_function_impl_create(main_entry_point
);
5535 nir_builder_init(&b
->nb
, main_entry_point
->impl
);
5536 b
->nb
.cursor
= nir_after_cf_list(&main_entry_point
->impl
->body
);
5537 b
->func_param_idx
= 0;
5539 nir_call_instr
*call
= nir_call_instr_create(b
->nb
.shader
, entry_point
);
5541 for (unsigned i
= 0; i
< entry_point
->num_params
; ++i
) {
5542 struct vtn_type
*param_type
= b
->entry_point
->func
->type
->params
[i
];
5544 /* consider all pointers to function memory to be parameters passed
5547 bool is_by_val
= param_type
->base_type
== vtn_base_type_pointer
&&
5548 param_type
->storage_class
== SpvStorageClassFunction
;
5550 /* input variable */
5551 nir_variable
*in_var
= rzalloc(b
->nb
.shader
, nir_variable
);
5552 in_var
->data
.mode
= nir_var_uniform
;
5553 in_var
->data
.read_only
= true;
5554 in_var
->data
.location
= i
;
5555 if (param_type
->base_type
== vtn_base_type_image
) {
5556 in_var
->data
.access
= 0;
5557 if (param_type
->access_qualifier
& SpvAccessQualifierReadOnly
)
5558 in_var
->data
.access
|= ACCESS_NON_WRITEABLE
;
5559 if (param_type
->access_qualifier
& SpvAccessQualifierWriteOnly
)
5560 in_var
->data
.access
|= ACCESS_NON_READABLE
;
5564 in_var
->type
= param_type
->deref
->type
;
5565 else if (param_type
->base_type
== vtn_base_type_image
)
5566 in_var
->type
= param_type
->glsl_image
;
5567 else if (param_type
->base_type
== vtn_base_type_sampler
)
5568 in_var
->type
= glsl_bare_sampler_type();
5570 in_var
->type
= param_type
->type
;
5572 nir_shader_add_variable(b
->nb
.shader
, in_var
);
5573 b
->nb
.shader
->num_inputs
++;
5575 /* we have to copy the entire variable into function memory */
5577 nir_variable
*copy_var
=
5578 nir_local_variable_create(main_entry_point
->impl
, in_var
->type
,
5580 nir_copy_var(&b
->nb
, copy_var
, in_var
);
5582 nir_src_for_ssa(&nir_build_deref_var(&b
->nb
, copy_var
)->dest
.ssa
);
5583 } else if (param_type
->base_type
== vtn_base_type_image
||
5584 param_type
->base_type
== vtn_base_type_sampler
) {
5585 /* Don't load the var, just pass a deref of it */
5586 call
->params
[i
] = nir_src_for_ssa(&nir_build_deref_var(&b
->nb
, in_var
)->dest
.ssa
);
5588 call
->params
[i
] = nir_src_for_ssa(nir_load_var(&b
->nb
, in_var
));
5592 nir_builder_instr_insert(&b
->nb
, &call
->instr
);
5594 return main_entry_point
;
5598 spirv_to_nir(const uint32_t *words
, size_t word_count
,
5599 struct nir_spirv_specialization
*spec
, unsigned num_spec
,
5600 gl_shader_stage stage
, const char *entry_point_name
,
5601 const struct spirv_to_nir_options
*options
,
5602 const nir_shader_compiler_options
*nir_options
)
5605 const uint32_t *word_end
= words
+ word_count
;
5607 struct vtn_builder
*b
= vtn_create_builder(words
, word_count
,
5608 stage
, entry_point_name
,
5614 /* See also _vtn_fail() */
5615 if (setjmp(b
->fail_jump
)) {
5620 /* Skip the SPIR-V header, handled at vtn_create_builder */
5623 b
->shader
= nir_shader_create(b
, stage
, nir_options
, NULL
);
5625 /* Handle all the preamble instructions */
5626 words
= vtn_foreach_instruction(b
, words
, word_end
,
5627 vtn_handle_preamble_instruction
);
5629 if (b
->entry_point
== NULL
) {
5630 vtn_fail("Entry point not found");
5635 /* Ensure a sane address mode is being used for function temps */
5636 assert(nir_address_format_bit_size(b
->options
->temp_addr_format
) == nir_get_ptr_bitsize(b
->shader
));
5637 assert(nir_address_format_num_components(b
->options
->temp_addr_format
) == 1);
5639 /* Set shader info defaults */
5640 if (stage
== MESA_SHADER_GEOMETRY
)
5641 b
->shader
->info
.gs
.invocations
= 1;
5643 /* Parse execution modes. */
5644 vtn_foreach_execution_mode(b
, b
->entry_point
,
5645 vtn_handle_execution_mode
, NULL
);
5647 b
->specializations
= spec
;
5648 b
->num_specializations
= num_spec
;
5650 /* Handle all variable, type, and constant instructions */
5651 words
= vtn_foreach_instruction(b
, words
, word_end
,
5652 vtn_handle_variable_or_type_instruction
);
5654 /* Parse execution modes that depend on IDs. Must happen after we have
5657 vtn_foreach_execution_mode(b
, b
->entry_point
,
5658 vtn_handle_execution_mode_id
, NULL
);
5660 if (b
->workgroup_size_builtin
) {
5661 vtn_assert(b
->workgroup_size_builtin
->type
->type
==
5662 glsl_vector_type(GLSL_TYPE_UINT
, 3));
5664 nir_const_value
*const_size
=
5665 b
->workgroup_size_builtin
->constant
->values
;
5667 b
->shader
->info
.cs
.local_size
[0] = const_size
[0].u32
;
5668 b
->shader
->info
.cs
.local_size
[1] = const_size
[1].u32
;
5669 b
->shader
->info
.cs
.local_size
[2] = const_size
[2].u32
;
5672 /* Set types on all vtn_values */
5673 vtn_foreach_instruction(b
, words
, word_end
, vtn_set_instruction_result_type
);
5675 vtn_build_cfg(b
, words
, word_end
);
5677 assert(b
->entry_point
->value_type
== vtn_value_type_function
);
5678 b
->entry_point
->func
->referenced
= true;
5683 vtn_foreach_cf_node(node
, &b
->functions
) {
5684 struct vtn_function
*func
= vtn_cf_node_as_function(node
);
5685 if (func
->referenced
&& !func
->emitted
) {
5686 b
->const_table
= _mesa_pointer_hash_table_create(b
);
5688 vtn_function_emit(b
, func
, vtn_handle_body_instruction
);
5694 vtn_assert(b
->entry_point
->value_type
== vtn_value_type_function
);
5695 nir_function
*entry_point
= b
->entry_point
->func
->impl
->function
;
5696 vtn_assert(entry_point
);
5698 /* post process entry_points with input params */
5699 if (entry_point
->num_params
&& b
->shader
->info
.stage
== MESA_SHADER_KERNEL
)
5700 entry_point
= vtn_emit_kernel_entry_point_wrapper(b
, entry_point
);
5702 /* structurize the CFG */
5703 nir_lower_goto_ifs(b
->shader
);
5705 entry_point
->is_entrypoint
= true;
5707 /* When multiple shader stages exist in the same SPIR-V module, we
5708 * generate input and output variables for every stage, in the same
5709 * NIR program. These dead variables can be invalid NIR. For example,
5710 * TCS outputs must be per-vertex arrays (or decorated 'patch'), while
5711 * VS output variables wouldn't be.
5713 * To ensure we have valid NIR, we eliminate any dead inputs and outputs
5714 * right away. In order to do so, we must lower any constant initializers
5715 * on outputs so nir_remove_dead_variables sees that they're written to.
5717 nir_lower_variable_initializers(b
->shader
, nir_var_shader_out
);
5718 nir_remove_dead_variables(b
->shader
,
5719 nir_var_shader_in
| nir_var_shader_out
, NULL
);
5721 /* We sometimes generate bogus derefs that, while never used, give the
5722 * validator a bit of heartburn. Run dead code to get rid of them.
5724 nir_opt_dce(b
->shader
);
5726 /* Unparent the shader from the vtn_builder before we delete the builder */
5727 ralloc_steal(NULL
, b
->shader
);
5729 nir_shader
*shader
= b
->shader
;