2 * Copyright 2018 Collabora Ltd.
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
24 #include "nir_to_spirv.h"
25 #include "spirv_builder.h"
28 #include "pipe/p_state.h"
29 #include "util/u_memory.h"
30 #include "util/hash_table.h"
33 struct spirv_builder builder
;
37 gl_shader_stage stage
;
41 SpvId image_types
[PIPE_MAX_SAMPLERS
];
42 SpvId samplers
[PIPE_MAX_SAMPLERS
];
43 unsigned samplers_used
: PIPE_MAX_SAMPLERS
;
44 SpvId entry_ifaces
[PIPE_MAX_SHADER_INPUTS
* 4 + PIPE_MAX_SHADER_OUTPUTS
* 4];
45 size_t num_entry_ifaces
;
53 struct hash_table
*vars
; /* nir_variable -> SpvId */
55 const SpvId
*block_ids
;
58 SpvId loop_break
, loop_cont
;
60 SpvId front_face_var
, instance_id_var
, vertex_id_var
;
64 get_fvec_constant(struct ntv_context
*ctx
, unsigned bit_size
,
65 unsigned num_components
, float value
);
68 get_uvec_constant(struct ntv_context
*ctx
, unsigned bit_size
,
69 unsigned num_components
, uint32_t value
);
72 get_ivec_constant(struct ntv_context
*ctx
, unsigned bit_size
,
73 unsigned num_components
, int32_t value
);
76 emit_unop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
, SpvId src
);
79 emit_binop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
,
80 SpvId src0
, SpvId src1
);
83 emit_triop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
,
84 SpvId src0
, SpvId src1
, SpvId src2
);
87 get_bvec_type(struct ntv_context
*ctx
, int num_components
)
89 SpvId bool_type
= spirv_builder_type_bool(&ctx
->builder
);
90 if (num_components
> 1)
91 return spirv_builder_type_vector(&ctx
->builder
, bool_type
,
94 assert(num_components
== 1);
99 block_label(struct ntv_context
*ctx
, nir_block
*block
)
101 assert(block
->index
< ctx
->num_blocks
);
102 return ctx
->block_ids
[block
->index
];
106 emit_float_const(struct ntv_context
*ctx
, int bit_size
, float value
)
108 assert(bit_size
== 32);
109 return spirv_builder_const_float(&ctx
->builder
, bit_size
, value
);
113 emit_uint_const(struct ntv_context
*ctx
, int bit_size
, uint32_t value
)
115 assert(bit_size
== 32);
116 return spirv_builder_const_uint(&ctx
->builder
, bit_size
, value
);
120 emit_int_const(struct ntv_context
*ctx
, int bit_size
, int32_t value
)
122 assert(bit_size
== 32);
123 return spirv_builder_const_int(&ctx
->builder
, bit_size
, value
);
127 get_fvec_type(struct ntv_context
*ctx
, unsigned bit_size
, unsigned num_components
)
129 assert(bit_size
== 32); // only 32-bit floats supported so far
131 SpvId float_type
= spirv_builder_type_float(&ctx
->builder
, bit_size
);
132 if (num_components
> 1)
133 return spirv_builder_type_vector(&ctx
->builder
, float_type
,
136 assert(num_components
== 1);
141 get_ivec_type(struct ntv_context
*ctx
, unsigned bit_size
, unsigned num_components
)
143 assert(bit_size
== 1 || bit_size
== 32); // only 32-bit ints supported so far
145 SpvId int_type
= spirv_builder_type_int(&ctx
->builder
, MAX2(bit_size
, 32));
146 if (num_components
> 1)
147 return spirv_builder_type_vector(&ctx
->builder
, int_type
,
150 assert(num_components
== 1);
155 get_uvec_type(struct ntv_context
*ctx
, unsigned bit_size
, unsigned num_components
)
157 assert(bit_size
== 1 || bit_size
== 32); // only 32-bit uints supported so far
159 SpvId uint_type
= spirv_builder_type_uint(&ctx
->builder
, MAX2(bit_size
, 32));
160 if (num_components
> 1)
161 return spirv_builder_type_vector(&ctx
->builder
, uint_type
,
164 assert(num_components
== 1);
169 get_dest_uvec_type(struct ntv_context
*ctx
, nir_dest
*dest
)
171 return get_uvec_type(ctx
, nir_dest_bit_size(*dest
),
172 nir_dest_num_components(*dest
));
176 get_glsl_basetype(struct ntv_context
*ctx
, enum glsl_base_type type
)
180 return spirv_builder_type_bool(&ctx
->builder
);
182 case GLSL_TYPE_FLOAT
:
183 return spirv_builder_type_float(&ctx
->builder
, 32);
186 return spirv_builder_type_int(&ctx
->builder
, 32);
189 return spirv_builder_type_uint(&ctx
->builder
, 32);
190 /* TODO: handle more types */
193 unreachable("unknown GLSL type");
198 get_glsl_type(struct ntv_context
*ctx
, const struct glsl_type
*type
)
201 if (glsl_type_is_scalar(type
))
202 return get_glsl_basetype(ctx
, glsl_get_base_type(type
));
204 if (glsl_type_is_vector(type
))
205 return spirv_builder_type_vector(&ctx
->builder
,
206 get_glsl_basetype(ctx
, glsl_get_base_type(type
)),
207 glsl_get_vector_elements(type
));
209 if (glsl_type_is_array(type
)) {
210 SpvId ret
= spirv_builder_type_array(&ctx
->builder
,
211 get_glsl_type(ctx
, glsl_get_array_element(type
)),
212 emit_uint_const(ctx
, 32, glsl_get_length(type
)));
213 uint32_t stride
= glsl_get_explicit_stride(type
);
215 spirv_builder_emit_array_stride(&ctx
->builder
, ret
, stride
);
220 unreachable("we shouldn't get here, I think...");
224 emit_input(struct ntv_context
*ctx
, struct nir_variable
*var
)
226 SpvId var_type
= get_glsl_type(ctx
, var
->type
);
227 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
228 SpvStorageClassInput
,
230 SpvId var_id
= spirv_builder_emit_var(&ctx
->builder
, pointer_type
,
231 SpvStorageClassInput
);
234 spirv_builder_emit_name(&ctx
->builder
, var_id
, var
->name
);
236 if (ctx
->stage
== MESA_SHADER_FRAGMENT
) {
237 if (var
->data
.location
>= VARYING_SLOT_VAR0
)
238 spirv_builder_emit_location(&ctx
->builder
, var_id
,
242 else if ((var
->data
.location
>= VARYING_SLOT_COL0
&&
243 var
->data
.location
<= VARYING_SLOT_TEX7
) ||
244 var
->data
.location
== VARYING_SLOT_BFC0
||
245 var
->data
.location
== VARYING_SLOT_BFC1
) {
246 spirv_builder_emit_location(&ctx
->builder
, var_id
,
249 switch (var
->data
.location
) {
250 case VARYING_SLOT_POS
:
251 spirv_builder_emit_builtin(&ctx
->builder
, var_id
, SpvBuiltInFragCoord
);
254 case VARYING_SLOT_PNTC
:
255 spirv_builder_emit_builtin(&ctx
->builder
, var_id
, SpvBuiltInPointCoord
);
259 debug_printf("unknown varying slot: %s\n", gl_varying_slot_name(var
->data
.location
));
260 unreachable("unexpected varying slot");
264 spirv_builder_emit_location(&ctx
->builder
, var_id
,
265 var
->data
.driver_location
);
268 if (var
->data
.location_frac
)
269 spirv_builder_emit_component(&ctx
->builder
, var_id
,
270 var
->data
.location_frac
);
272 if (var
->data
.interpolation
== INTERP_MODE_FLAT
)
273 spirv_builder_emit_decoration(&ctx
->builder
, var_id
, SpvDecorationFlat
);
275 _mesa_hash_table_insert(ctx
->vars
, var
, (void *)(intptr_t)var_id
);
277 assert(ctx
->num_entry_ifaces
< ARRAY_SIZE(ctx
->entry_ifaces
));
278 ctx
->entry_ifaces
[ctx
->num_entry_ifaces
++] = var_id
;
282 emit_output(struct ntv_context
*ctx
, struct nir_variable
*var
)
284 SpvId var_type
= get_glsl_type(ctx
, var
->type
);
285 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
286 SpvStorageClassOutput
,
288 SpvId var_id
= spirv_builder_emit_var(&ctx
->builder
, pointer_type
,
289 SpvStorageClassOutput
);
291 spirv_builder_emit_name(&ctx
->builder
, var_id
, var
->name
);
294 if (ctx
->stage
== MESA_SHADER_VERTEX
) {
295 if (var
->data
.location
>= VARYING_SLOT_VAR0
)
296 spirv_builder_emit_location(&ctx
->builder
, var_id
,
300 else if ((var
->data
.location
>= VARYING_SLOT_COL0
&&
301 var
->data
.location
<= VARYING_SLOT_TEX7
) ||
302 var
->data
.location
== VARYING_SLOT_BFC0
||
303 var
->data
.location
== VARYING_SLOT_BFC1
) {
304 spirv_builder_emit_location(&ctx
->builder
, var_id
,
307 switch (var
->data
.location
) {
308 case VARYING_SLOT_POS
:
309 spirv_builder_emit_builtin(&ctx
->builder
, var_id
, SpvBuiltInPosition
);
312 case VARYING_SLOT_PSIZ
:
313 spirv_builder_emit_builtin(&ctx
->builder
, var_id
, SpvBuiltInPointSize
);
316 case VARYING_SLOT_CLIP_DIST0
:
317 assert(glsl_type_is_array(var
->type
));
318 spirv_builder_emit_builtin(&ctx
->builder
, var_id
, SpvBuiltInClipDistance
);
322 debug_printf("unknown varying slot: %s\n", gl_varying_slot_name(var
->data
.location
));
323 unreachable("unexpected varying slot");
326 } else if (ctx
->stage
== MESA_SHADER_FRAGMENT
) {
327 if (var
->data
.location
>= FRAG_RESULT_DATA0
)
328 spirv_builder_emit_location(&ctx
->builder
, var_id
,
329 var
->data
.location
- FRAG_RESULT_DATA0
);
331 switch (var
->data
.location
) {
332 case FRAG_RESULT_COLOR
:
333 spirv_builder_emit_location(&ctx
->builder
, var_id
, 0);
334 spirv_builder_emit_index(&ctx
->builder
, var_id
, var
->data
.index
);
337 case FRAG_RESULT_DEPTH
:
338 spirv_builder_emit_builtin(&ctx
->builder
, var_id
, SpvBuiltInFragDepth
);
342 spirv_builder_emit_location(&ctx
->builder
, var_id
,
343 var
->data
.driver_location
);
348 if (var
->data
.location_frac
)
349 spirv_builder_emit_component(&ctx
->builder
, var_id
,
350 var
->data
.location_frac
);
352 _mesa_hash_table_insert(ctx
->vars
, var
, (void *)(intptr_t)var_id
);
354 assert(ctx
->num_entry_ifaces
< ARRAY_SIZE(ctx
->entry_ifaces
));
355 ctx
->entry_ifaces
[ctx
->num_entry_ifaces
++] = var_id
;
359 type_to_dim(enum glsl_sampler_dim gdim
, bool *is_ms
)
363 case GLSL_SAMPLER_DIM_1D
:
365 case GLSL_SAMPLER_DIM_2D
:
367 case GLSL_SAMPLER_DIM_3D
:
369 case GLSL_SAMPLER_DIM_CUBE
:
371 case GLSL_SAMPLER_DIM_RECT
:
373 case GLSL_SAMPLER_DIM_BUF
:
375 case GLSL_SAMPLER_DIM_EXTERNAL
:
376 return SpvDim2D
; /* seems dodgy... */
377 case GLSL_SAMPLER_DIM_MS
:
381 fprintf(stderr
, "unknown sampler type %d\n", gdim
);
388 zink_binding(gl_shader_stage stage
, VkDescriptorType type
, int index
)
390 if (stage
== MESA_SHADER_NONE
||
391 stage
>= MESA_SHADER_COMPUTE
) {
392 unreachable("not supported");
394 uint32_t stage_offset
= (uint32_t)stage
* (PIPE_MAX_CONSTANT_BUFFERS
+
395 PIPE_MAX_SHADER_SAMPLER_VIEWS
);
398 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
399 assert(index
< PIPE_MAX_CONSTANT_BUFFERS
);
400 return stage_offset
+ index
;
402 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
403 assert(index
< PIPE_MAX_SHADER_SAMPLER_VIEWS
);
404 return stage_offset
+ PIPE_MAX_CONSTANT_BUFFERS
+ index
;
407 unreachable("unexpected type");
413 emit_sampler(struct ntv_context
*ctx
, struct nir_variable
*var
)
415 const struct glsl_type
*type
= glsl_without_array(var
->type
);
418 SpvDim dimension
= type_to_dim(glsl_get_sampler_dim(type
), &is_ms
);
420 SpvId result_type
= get_glsl_basetype(ctx
, glsl_get_sampler_result_type(type
));
421 SpvId image_type
= spirv_builder_type_image(&ctx
->builder
, result_type
,
423 glsl_sampler_type_is_array(type
),
425 SpvImageFormatUnknown
);
427 SpvId sampled_type
= spirv_builder_type_sampled_image(&ctx
->builder
,
429 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
430 SpvStorageClassUniformConstant
,
433 if (glsl_type_is_array(var
->type
)) {
434 for (int i
= 0; i
< glsl_get_length(var
->type
); ++i
) {
435 SpvId var_id
= spirv_builder_emit_var(&ctx
->builder
, pointer_type
,
436 SpvStorageClassUniformConstant
);
439 char element_name
[100];
440 snprintf(element_name
, sizeof(element_name
), "%s_%d", var
->name
, i
);
441 spirv_builder_emit_name(&ctx
->builder
, var_id
, var
->name
);
444 int index
= var
->data
.binding
+ i
;
445 assert(!(ctx
->samplers_used
& (1 << index
)));
446 assert(!ctx
->image_types
[index
]);
447 ctx
->image_types
[index
] = image_type
;
448 ctx
->samplers
[index
] = var_id
;
449 ctx
->samplers_used
|= 1 << index
;
451 spirv_builder_emit_descriptor_set(&ctx
->builder
, var_id
,
452 var
->data
.descriptor_set
);
453 int binding
= zink_binding(ctx
->stage
,
454 VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
,
455 var
->data
.binding
+ i
);
456 spirv_builder_emit_binding(&ctx
->builder
, var_id
, binding
);
459 SpvId var_id
= spirv_builder_emit_var(&ctx
->builder
, pointer_type
,
460 SpvStorageClassUniformConstant
);
463 spirv_builder_emit_name(&ctx
->builder
, var_id
, var
->name
);
465 int index
= var
->data
.binding
;
466 assert(!(ctx
->samplers_used
& (1 << index
)));
467 assert(!ctx
->image_types
[index
]);
468 ctx
->image_types
[index
] = image_type
;
469 ctx
->samplers
[index
] = var_id
;
470 ctx
->samplers_used
|= 1 << index
;
472 spirv_builder_emit_descriptor_set(&ctx
->builder
, var_id
,
473 var
->data
.descriptor_set
);
474 int binding
= zink_binding(ctx
->stage
,
475 VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
,
477 spirv_builder_emit_binding(&ctx
->builder
, var_id
, binding
);
482 emit_ubo(struct ntv_context
*ctx
, struct nir_variable
*var
)
484 uint32_t size
= glsl_count_attribute_slots(var
->type
, false);
485 SpvId vec4_type
= get_uvec_type(ctx
, 32, 4);
486 SpvId array_length
= emit_uint_const(ctx
, 32, size
);
487 SpvId array_type
= spirv_builder_type_array(&ctx
->builder
, vec4_type
,
489 spirv_builder_emit_array_stride(&ctx
->builder
, array_type
, 16);
491 // wrap UBO-array in a struct
492 SpvId struct_type
= spirv_builder_type_struct(&ctx
->builder
, &array_type
, 1);
494 char struct_name
[100];
495 snprintf(struct_name
, sizeof(struct_name
), "struct_%s", var
->name
);
496 spirv_builder_emit_name(&ctx
->builder
, struct_type
, struct_name
);
499 spirv_builder_emit_decoration(&ctx
->builder
, struct_type
,
501 spirv_builder_emit_member_offset(&ctx
->builder
, struct_type
, 0, 0);
504 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
505 SpvStorageClassUniform
,
508 SpvId var_id
= spirv_builder_emit_var(&ctx
->builder
, pointer_type
,
509 SpvStorageClassUniform
);
511 spirv_builder_emit_name(&ctx
->builder
, var_id
, var
->name
);
513 assert(ctx
->num_ubos
< ARRAY_SIZE(ctx
->ubos
));
514 ctx
->ubos
[ctx
->num_ubos
++] = var_id
;
516 spirv_builder_emit_descriptor_set(&ctx
->builder
, var_id
,
517 var
->data
.descriptor_set
);
518 int binding
= zink_binding(ctx
->stage
,
519 VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
,
521 spirv_builder_emit_binding(&ctx
->builder
, var_id
, binding
);
525 emit_uniform(struct ntv_context
*ctx
, struct nir_variable
*var
)
527 if (var
->data
.mode
== nir_var_mem_ubo
)
530 assert(var
->data
.mode
== nir_var_uniform
);
531 if (glsl_type_is_sampler(glsl_without_array(var
->type
)))
532 emit_sampler(ctx
, var
);
537 get_src_ssa(struct ntv_context
*ctx
, const nir_ssa_def
*ssa
)
539 assert(ssa
->index
< ctx
->num_defs
);
540 assert(ctx
->defs
[ssa
->index
] != 0);
541 return ctx
->defs
[ssa
->index
];
545 get_var_from_reg(struct ntv_context
*ctx
, nir_register
*reg
)
547 assert(reg
->index
< ctx
->num_regs
);
548 assert(ctx
->regs
[reg
->index
] != 0);
549 return ctx
->regs
[reg
->index
];
553 get_src_reg(struct ntv_context
*ctx
, const nir_reg_src
*reg
)
556 assert(!reg
->indirect
);
557 assert(!reg
->base_offset
);
559 SpvId var
= get_var_from_reg(ctx
, reg
->reg
);
560 SpvId type
= get_uvec_type(ctx
, reg
->reg
->bit_size
, reg
->reg
->num_components
);
561 return spirv_builder_emit_load(&ctx
->builder
, type
, var
);
565 get_src(struct ntv_context
*ctx
, nir_src
*src
)
568 return get_src_ssa(ctx
, src
->ssa
);
570 return get_src_reg(ctx
, &src
->reg
);
574 get_alu_src_raw(struct ntv_context
*ctx
, nir_alu_instr
*alu
, unsigned src
)
576 assert(!alu
->src
[src
].negate
);
577 assert(!alu
->src
[src
].abs
);
579 SpvId def
= get_src(ctx
, &alu
->src
[src
].src
);
581 unsigned used_channels
= 0;
582 bool need_swizzle
= false;
583 for (unsigned i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; i
++) {
584 if (!nir_alu_instr_channel_used(alu
, src
, i
))
589 if (alu
->src
[src
].swizzle
[i
] != i
)
592 assert(used_channels
!= 0);
594 unsigned live_channels
= nir_src_num_components(alu
->src
[src
].src
);
595 if (used_channels
!= live_channels
)
601 int bit_size
= nir_src_bit_size(alu
->src
[src
].src
);
602 assert(bit_size
== 1 || bit_size
== 32);
604 SpvId raw_type
= spirv_builder_type_uint(&ctx
->builder
, MAX2(bit_size
, 32));
605 if (used_channels
== 1) {
606 uint32_t indices
[] = { alu
->src
[src
].swizzle
[0] };
607 return spirv_builder_emit_composite_extract(&ctx
->builder
, raw_type
,
609 ARRAY_SIZE(indices
));
610 } else if (live_channels
== 1) {
611 SpvId raw_vec_type
= spirv_builder_type_vector(&ctx
->builder
,
615 SpvId constituents
[NIR_MAX_VEC_COMPONENTS
];
616 for (unsigned i
= 0; i
< used_channels
; ++i
)
617 constituents
[i
] = def
;
619 return spirv_builder_emit_composite_construct(&ctx
->builder
,
624 SpvId raw_vec_type
= spirv_builder_type_vector(&ctx
->builder
,
628 uint32_t components
[NIR_MAX_VEC_COMPONENTS
];
629 size_t num_components
= 0;
630 for (unsigned i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; i
++) {
631 if (!nir_alu_instr_channel_used(alu
, src
, i
))
634 components
[num_components
++] = alu
->src
[src
].swizzle
[i
];
637 return spirv_builder_emit_vector_shuffle(&ctx
->builder
, raw_vec_type
,
638 def
, def
, components
,
644 store_ssa_def(struct ntv_context
*ctx
, nir_ssa_def
*ssa
, SpvId result
)
647 assert(ssa
->index
< ctx
->num_defs
);
648 ctx
->defs
[ssa
->index
] = result
;
652 emit_select(struct ntv_context
*ctx
, SpvId type
, SpvId cond
,
653 SpvId if_true
, SpvId if_false
)
655 return emit_triop(ctx
, SpvOpSelect
, type
, cond
, if_true
, if_false
);
659 bvec_to_uvec(struct ntv_context
*ctx
, SpvId value
, unsigned num_components
)
661 SpvId otype
= get_uvec_type(ctx
, 32, num_components
);
662 SpvId zero
= get_uvec_constant(ctx
, 32, num_components
, 0);
663 SpvId one
= get_uvec_constant(ctx
, 32, num_components
, UINT32_MAX
);
664 return emit_select(ctx
, otype
, value
, one
, zero
);
668 uvec_to_bvec(struct ntv_context
*ctx
, SpvId value
, unsigned num_components
)
670 SpvId type
= get_bvec_type(ctx
, num_components
);
671 SpvId zero
= get_uvec_constant(ctx
, 32, num_components
, 0);
672 return emit_binop(ctx
, SpvOpINotEqual
, type
, value
, zero
);
676 emit_bitcast(struct ntv_context
*ctx
, SpvId type
, SpvId value
)
678 return emit_unop(ctx
, SpvOpBitcast
, type
, value
);
682 bitcast_to_uvec(struct ntv_context
*ctx
, SpvId value
, unsigned bit_size
,
683 unsigned num_components
)
685 SpvId type
= get_uvec_type(ctx
, bit_size
, num_components
);
686 return emit_bitcast(ctx
, type
, value
);
690 bitcast_to_ivec(struct ntv_context
*ctx
, SpvId value
, unsigned bit_size
,
691 unsigned num_components
)
693 SpvId type
= get_ivec_type(ctx
, bit_size
, num_components
);
694 return emit_bitcast(ctx
, type
, value
);
698 bitcast_to_fvec(struct ntv_context
*ctx
, SpvId value
, unsigned bit_size
,
699 unsigned num_components
)
701 SpvId type
= get_fvec_type(ctx
, bit_size
, num_components
);
702 return emit_bitcast(ctx
, type
, value
);
706 store_reg_def(struct ntv_context
*ctx
, nir_reg_dest
*reg
, SpvId result
)
708 SpvId var
= get_var_from_reg(ctx
, reg
->reg
);
710 spirv_builder_emit_store(&ctx
->builder
, var
, result
);
714 store_dest_raw(struct ntv_context
*ctx
, nir_dest
*dest
, SpvId result
)
717 store_ssa_def(ctx
, &dest
->ssa
, result
);
719 store_reg_def(ctx
, &dest
->reg
, result
);
723 store_dest(struct ntv_context
*ctx
, nir_dest
*dest
, SpvId result
, nir_alu_type type
)
725 unsigned num_components
= nir_dest_num_components(*dest
);
726 unsigned bit_size
= nir_dest_bit_size(*dest
);
728 switch (nir_alu_type_get_base_type(type
)) {
730 assert(bit_size
== 1);
731 result
= bvec_to_uvec(ctx
, result
, num_components
);
735 break; /* nothing to do! */
739 result
= bitcast_to_uvec(ctx
, result
, bit_size
, num_components
);
743 unreachable("unsupported nir_alu_type");
746 store_dest_raw(ctx
, dest
, result
);
750 emit_unop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
, SpvId src
)
752 return spirv_builder_emit_unop(&ctx
->builder
, op
, type
, src
);
756 emit_binop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
,
757 SpvId src0
, SpvId src1
)
759 return spirv_builder_emit_binop(&ctx
->builder
, op
, type
, src0
, src1
);
763 emit_triop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
,
764 SpvId src0
, SpvId src1
, SpvId src2
)
766 return spirv_builder_emit_triop(&ctx
->builder
, op
, type
, src0
, src1
, src2
);
770 emit_builtin_unop(struct ntv_context
*ctx
, enum GLSLstd450 op
, SpvId type
,
773 SpvId args
[] = { src
};
774 return spirv_builder_emit_ext_inst(&ctx
->builder
, type
, ctx
->GLSL_std_450
,
775 op
, args
, ARRAY_SIZE(args
));
779 emit_builtin_binop(struct ntv_context
*ctx
, enum GLSLstd450 op
, SpvId type
,
780 SpvId src0
, SpvId src1
)
782 SpvId args
[] = { src0
, src1
};
783 return spirv_builder_emit_ext_inst(&ctx
->builder
, type
, ctx
->GLSL_std_450
,
784 op
, args
, ARRAY_SIZE(args
));
788 emit_builtin_triop(struct ntv_context
*ctx
, enum GLSLstd450 op
, SpvId type
,
789 SpvId src0
, SpvId src1
, SpvId src2
)
791 SpvId args
[] = { src0
, src1
, src2
};
792 return spirv_builder_emit_ext_inst(&ctx
->builder
, type
, ctx
->GLSL_std_450
,
793 op
, args
, ARRAY_SIZE(args
));
797 get_fvec_constant(struct ntv_context
*ctx
, unsigned bit_size
,
798 unsigned num_components
, float value
)
800 assert(bit_size
== 32);
802 SpvId result
= emit_float_const(ctx
, bit_size
, value
);
803 if (num_components
== 1)
806 assert(num_components
> 1);
807 SpvId components
[num_components
];
808 for (int i
= 0; i
< num_components
; i
++)
809 components
[i
] = result
;
811 SpvId type
= get_fvec_type(ctx
, bit_size
, num_components
);
812 return spirv_builder_const_composite(&ctx
->builder
, type
, components
,
817 get_uvec_constant(struct ntv_context
*ctx
, unsigned bit_size
,
818 unsigned num_components
, uint32_t value
)
820 assert(bit_size
== 32);
822 SpvId result
= emit_uint_const(ctx
, bit_size
, value
);
823 if (num_components
== 1)
826 assert(num_components
> 1);
827 SpvId components
[num_components
];
828 for (int i
= 0; i
< num_components
; i
++)
829 components
[i
] = result
;
831 SpvId type
= get_uvec_type(ctx
, bit_size
, num_components
);
832 return spirv_builder_const_composite(&ctx
->builder
, type
, components
,
837 get_ivec_constant(struct ntv_context
*ctx
, unsigned bit_size
,
838 unsigned num_components
, int32_t value
)
840 assert(bit_size
== 32);
842 SpvId result
= emit_int_const(ctx
, bit_size
, value
);
843 if (num_components
== 1)
846 assert(num_components
> 1);
847 SpvId components
[num_components
];
848 for (int i
= 0; i
< num_components
; i
++)
849 components
[i
] = result
;
851 SpvId type
= get_ivec_type(ctx
, bit_size
, num_components
);
852 return spirv_builder_const_composite(&ctx
->builder
, type
, components
,
856 static inline unsigned
857 alu_instr_src_components(const nir_alu_instr
*instr
, unsigned src
)
859 if (nir_op_infos
[instr
->op
].input_sizes
[src
] > 0)
860 return nir_op_infos
[instr
->op
].input_sizes
[src
];
862 if (instr
->dest
.dest
.is_ssa
)
863 return instr
->dest
.dest
.ssa
.num_components
;
865 return instr
->dest
.dest
.reg
.reg
->num_components
;
869 get_alu_src(struct ntv_context
*ctx
, nir_alu_instr
*alu
, unsigned src
)
871 SpvId raw_value
= get_alu_src_raw(ctx
, alu
, src
);
873 unsigned num_components
= alu_instr_src_components(alu
, src
);
874 unsigned bit_size
= nir_src_bit_size(alu
->src
[src
].src
);
875 nir_alu_type type
= nir_op_infos
[alu
->op
].input_types
[src
];
877 switch (nir_alu_type_get_base_type(type
)) {
879 assert(bit_size
== 1);
880 return uvec_to_bvec(ctx
, raw_value
, num_components
);
883 return bitcast_to_ivec(ctx
, raw_value
, bit_size
, num_components
);
889 return bitcast_to_fvec(ctx
, raw_value
, bit_size
, num_components
);
892 unreachable("unknown nir_alu_type");
897 store_alu_result(struct ntv_context
*ctx
, nir_alu_instr
*alu
, SpvId result
)
899 assert(!alu
->dest
.saturate
);
900 return store_dest(ctx
, &alu
->dest
.dest
, result
,
901 nir_op_infos
[alu
->op
].output_type
);
905 get_dest_type(struct ntv_context
*ctx
, nir_dest
*dest
, nir_alu_type type
)
907 unsigned num_components
= nir_dest_num_components(*dest
);
908 unsigned bit_size
= nir_dest_bit_size(*dest
);
910 switch (nir_alu_type_get_base_type(type
)) {
912 return get_bvec_type(ctx
, num_components
);
915 return get_ivec_type(ctx
, bit_size
, num_components
);
918 return get_uvec_type(ctx
, bit_size
, num_components
);
921 return get_fvec_type(ctx
, bit_size
, num_components
);
924 unreachable("unsupported nir_alu_type");
929 emit_alu(struct ntv_context
*ctx
, nir_alu_instr
*alu
)
931 SpvId src
[nir_op_infos
[alu
->op
].num_inputs
];
932 for (unsigned i
= 0; i
< nir_op_infos
[alu
->op
].num_inputs
; i
++)
933 src
[i
] = get_alu_src(ctx
, alu
, i
);
935 SpvId dest_type
= get_dest_type(ctx
, &alu
->dest
.dest
,
936 nir_op_infos
[alu
->op
].output_type
);
937 unsigned bit_size
= nir_dest_bit_size(alu
->dest
.dest
);
938 unsigned num_components
= nir_dest_num_components(alu
->dest
.dest
);
943 assert(nir_op_infos
[alu
->op
].num_inputs
== 1);
947 #define UNOP(nir_op, spirv_op) \
949 assert(nir_op_infos[alu->op].num_inputs == 1); \
950 result = emit_unop(ctx, spirv_op, dest_type, src[0]); \
953 UNOP(nir_op_ineg
, SpvOpSNegate
)
954 UNOP(nir_op_fneg
, SpvOpFNegate
)
955 UNOP(nir_op_fddx
, SpvOpDPdx
)
956 UNOP(nir_op_fddx_coarse
, SpvOpDPdxCoarse
)
957 UNOP(nir_op_fddx_fine
, SpvOpDPdxFine
)
958 UNOP(nir_op_fddy
, SpvOpDPdy
)
959 UNOP(nir_op_fddy_coarse
, SpvOpDPdyCoarse
)
960 UNOP(nir_op_fddy_fine
, SpvOpDPdyFine
)
961 UNOP(nir_op_f2i32
, SpvOpConvertFToS
)
962 UNOP(nir_op_f2u32
, SpvOpConvertFToU
)
963 UNOP(nir_op_i2f32
, SpvOpConvertSToF
)
964 UNOP(nir_op_u2f32
, SpvOpConvertUToF
)
965 UNOP(nir_op_inot
, SpvOpNot
)
969 assert(nir_op_infos
[alu
->op
].num_inputs
== 1);
970 result
= emit_select(ctx
, dest_type
, src
[0],
971 get_ivec_constant(ctx
, 32, num_components
, 1),
972 get_ivec_constant(ctx
, 32, num_components
, 0));
976 assert(nir_op_infos
[alu
->op
].num_inputs
== 1);
977 result
= emit_select(ctx
, dest_type
, src
[0],
978 get_fvec_constant(ctx
, 32, num_components
, 1),
979 get_fvec_constant(ctx
, 32, num_components
, 0));
982 #define BUILTIN_UNOP(nir_op, spirv_op) \
984 assert(nir_op_infos[alu->op].num_inputs == 1); \
985 result = emit_builtin_unop(ctx, spirv_op, dest_type, src[0]); \
988 BUILTIN_UNOP(nir_op_iabs
, GLSLstd450SAbs
)
989 BUILTIN_UNOP(nir_op_fabs
, GLSLstd450FAbs
)
990 BUILTIN_UNOP(nir_op_fsqrt
, GLSLstd450Sqrt
)
991 BUILTIN_UNOP(nir_op_frsq
, GLSLstd450InverseSqrt
)
992 BUILTIN_UNOP(nir_op_flog2
, GLSLstd450Log2
)
993 BUILTIN_UNOP(nir_op_fexp2
, GLSLstd450Exp2
)
994 BUILTIN_UNOP(nir_op_ffract
, GLSLstd450Fract
)
995 BUILTIN_UNOP(nir_op_ffloor
, GLSLstd450Floor
)
996 BUILTIN_UNOP(nir_op_fceil
, GLSLstd450Ceil
)
997 BUILTIN_UNOP(nir_op_ftrunc
, GLSLstd450Trunc
)
998 BUILTIN_UNOP(nir_op_fround_even
, GLSLstd450RoundEven
)
999 BUILTIN_UNOP(nir_op_fsign
, GLSLstd450FSign
)
1000 BUILTIN_UNOP(nir_op_fsin
, GLSLstd450Sin
)
1001 BUILTIN_UNOP(nir_op_fcos
, GLSLstd450Cos
)
1005 assert(nir_op_infos
[alu
->op
].num_inputs
== 1);
1006 result
= emit_binop(ctx
, SpvOpFDiv
, dest_type
,
1007 get_fvec_constant(ctx
, bit_size
, num_components
, 1),
1012 assert(nir_op_infos
[alu
->op
].num_inputs
== 1);
1013 result
= emit_binop(ctx
, SpvOpFOrdNotEqual
, dest_type
, src
[0],
1014 get_fvec_constant(ctx
,
1015 nir_src_bit_size(alu
->src
[0].src
),
1016 num_components
, 0));
1020 #define BINOP(nir_op, spirv_op) \
1022 assert(nir_op_infos[alu->op].num_inputs == 2); \
1023 result = emit_binop(ctx, spirv_op, dest_type, src[0], src[1]); \
1026 BINOP(nir_op_iadd
, SpvOpIAdd
)
1027 BINOP(nir_op_isub
, SpvOpISub
)
1028 BINOP(nir_op_imul
, SpvOpIMul
)
1029 BINOP(nir_op_idiv
, SpvOpSDiv
)
1030 BINOP(nir_op_udiv
, SpvOpUDiv
)
1031 BINOP(nir_op_umod
, SpvOpUMod
)
1032 BINOP(nir_op_fadd
, SpvOpFAdd
)
1033 BINOP(nir_op_fsub
, SpvOpFSub
)
1034 BINOP(nir_op_fmul
, SpvOpFMul
)
1035 BINOP(nir_op_fdiv
, SpvOpFDiv
)
1036 BINOP(nir_op_fmod
, SpvOpFMod
)
1037 BINOP(nir_op_ilt
, SpvOpSLessThan
)
1038 BINOP(nir_op_ige
, SpvOpSGreaterThanEqual
)
1039 BINOP(nir_op_ieq
, SpvOpIEqual
)
1040 BINOP(nir_op_ine
, SpvOpINotEqual
)
1041 BINOP(nir_op_uge
, SpvOpUGreaterThanEqual
)
1042 BINOP(nir_op_flt
, SpvOpFOrdLessThan
)
1043 BINOP(nir_op_fge
, SpvOpFOrdGreaterThanEqual
)
1044 BINOP(nir_op_feq
, SpvOpFOrdEqual
)
1045 BINOP(nir_op_fne
, SpvOpFOrdNotEqual
)
1046 BINOP(nir_op_ishl
, SpvOpShiftLeftLogical
)
1047 BINOP(nir_op_ishr
, SpvOpShiftRightArithmetic
)
1048 BINOP(nir_op_ushr
, SpvOpShiftRightLogical
)
1049 BINOP(nir_op_iand
, SpvOpBitwiseAnd
)
1050 BINOP(nir_op_ior
, SpvOpBitwiseOr
)
1053 #define BUILTIN_BINOP(nir_op, spirv_op) \
1055 assert(nir_op_infos[alu->op].num_inputs == 2); \
1056 result = emit_builtin_binop(ctx, spirv_op, dest_type, src[0], src[1]); \
1059 BUILTIN_BINOP(nir_op_fmin
, GLSLstd450FMin
)
1060 BUILTIN_BINOP(nir_op_fmax
, GLSLstd450FMax
)
1061 #undef BUILTIN_BINOP
1066 assert(nir_op_infos
[alu
->op
].num_inputs
== 2);
1067 result
= emit_binop(ctx
, SpvOpDot
, dest_type
, src
[0], src
[1]);
1071 unreachable("should already be lowered away");
1077 assert(nir_op_infos
[alu
->op
].num_inputs
== 2);
1078 int num_components
= nir_dest_num_components(alu
->dest
.dest
);
1079 SpvId bool_type
= get_bvec_type(ctx
, num_components
);
1081 SpvId zero
= emit_float_const(ctx
, bit_size
, 0.0f
);
1082 SpvId one
= emit_float_const(ctx
, bit_size
, 1.0f
);
1083 if (num_components
> 1) {
1084 SpvId zero_comps
[num_components
], one_comps
[num_components
];
1085 for (int i
= 0; i
< num_components
; i
++) {
1086 zero_comps
[i
] = zero
;
1090 zero
= spirv_builder_const_composite(&ctx
->builder
, dest_type
,
1091 zero_comps
, num_components
);
1092 one
= spirv_builder_const_composite(&ctx
->builder
, dest_type
,
1093 one_comps
, num_components
);
1098 case nir_op_seq
: op
= SpvOpFOrdEqual
; break;
1099 case nir_op_sne
: op
= SpvOpFOrdNotEqual
; break;
1100 case nir_op_slt
: op
= SpvOpFOrdLessThan
; break;
1101 case nir_op_sge
: op
= SpvOpFOrdGreaterThanEqual
; break;
1102 default: unreachable("unexpected op");
1105 result
= emit_binop(ctx
, op
, bool_type
, src
[0], src
[1]);
1106 result
= emit_select(ctx
, dest_type
, result
, one
, zero
);
1111 assert(nir_op_infos
[alu
->op
].num_inputs
== 3);
1112 result
= emit_builtin_triop(ctx
, GLSLstd450FMix
, dest_type
,
1113 src
[0], src
[1], src
[2]);
1117 result
= emit_binop(ctx
, SpvOpFOrdGreaterThan
,
1118 get_bvec_type(ctx
, num_components
),
1120 get_fvec_constant(ctx
,
1121 nir_src_bit_size(alu
->src
[0].src
),
1122 num_components
, 0));
1123 result
= emit_select(ctx
, dest_type
, result
, src
[1], src
[2]);
1127 assert(nir_op_infos
[alu
->op
].num_inputs
== 3);
1128 result
= emit_select(ctx
, dest_type
, src
[0], src
[1], src
[2]);
1131 case nir_op_bany_fnequal2
:
1132 case nir_op_bany_fnequal3
:
1133 case nir_op_bany_fnequal4
:
1134 assert(nir_op_infos
[alu
->op
].num_inputs
== 2);
1135 assert(alu_instr_src_components(alu
, 0) ==
1136 alu_instr_src_components(alu
, 1));
1137 result
= emit_binop(ctx
, SpvOpFOrdNotEqual
,
1138 get_bvec_type(ctx
, alu_instr_src_components(alu
, 0)),
1140 result
= emit_unop(ctx
, SpvOpAny
, dest_type
, result
);
1143 case nir_op_ball_fequal2
:
1144 case nir_op_ball_fequal3
:
1145 case nir_op_ball_fequal4
:
1146 assert(nir_op_infos
[alu
->op
].num_inputs
== 2);
1147 assert(alu_instr_src_components(alu
, 0) ==
1148 alu_instr_src_components(alu
, 1));
1149 result
= emit_binop(ctx
, SpvOpFOrdEqual
,
1150 get_bvec_type(ctx
, alu_instr_src_components(alu
, 0)),
1152 result
= emit_unop(ctx
, SpvOpAll
, dest_type
, result
);
1155 case nir_op_bany_inequal2
:
1156 case nir_op_bany_inequal3
:
1157 case nir_op_bany_inequal4
:
1158 assert(nir_op_infos
[alu
->op
].num_inputs
== 2);
1159 assert(alu_instr_src_components(alu
, 0) ==
1160 alu_instr_src_components(alu
, 1));
1161 result
= emit_binop(ctx
, SpvOpINotEqual
,
1162 get_bvec_type(ctx
, alu_instr_src_components(alu
, 0)),
1164 result
= emit_unop(ctx
, SpvOpAny
, dest_type
, result
);
1167 case nir_op_ball_iequal2
:
1168 case nir_op_ball_iequal3
:
1169 case nir_op_ball_iequal4
:
1170 assert(nir_op_infos
[alu
->op
].num_inputs
== 2);
1171 assert(alu_instr_src_components(alu
, 0) ==
1172 alu_instr_src_components(alu
, 1));
1173 result
= emit_binop(ctx
, SpvOpIEqual
,
1174 get_bvec_type(ctx
, alu_instr_src_components(alu
, 0)),
1176 result
= emit_unop(ctx
, SpvOpAll
, dest_type
, result
);
1182 int num_inputs
= nir_op_infos
[alu
->op
].num_inputs
;
1183 assert(2 <= num_inputs
&& num_inputs
<= 4);
1184 result
= spirv_builder_emit_composite_construct(&ctx
->builder
, dest_type
,
1190 fprintf(stderr
, "emit_alu: not implemented (%s)\n",
1191 nir_op_infos
[alu
->op
].name
);
1193 unreachable("unsupported opcode");
1197 store_alu_result(ctx
, alu
, result
);
1201 emit_load_const(struct ntv_context
*ctx
, nir_load_const_instr
*load_const
)
1203 unsigned bit_size
= load_const
->def
.bit_size
;
1204 unsigned num_components
= load_const
->def
.num_components
;
1207 if (num_components
> 1) {
1208 SpvId components
[num_components
];
1210 if (bit_size
== 1) {
1211 for (int i
= 0; i
< num_components
; i
++)
1212 components
[i
] = spirv_builder_const_bool(&ctx
->builder
,
1213 load_const
->value
[i
].b
);
1215 type
= get_bvec_type(ctx
, num_components
);
1217 for (int i
= 0; i
< num_components
; i
++)
1218 components
[i
] = emit_uint_const(ctx
, bit_size
,
1219 load_const
->value
[i
].u32
);
1221 type
= get_uvec_type(ctx
, bit_size
, num_components
);
1223 constant
= spirv_builder_const_composite(&ctx
->builder
, type
,
1224 components
, num_components
);
1226 assert(num_components
== 1);
1228 constant
= spirv_builder_const_bool(&ctx
->builder
,
1229 load_const
->value
[0].b
);
1231 constant
= emit_uint_const(ctx
, bit_size
, load_const
->value
[0].u32
);
1235 constant
= bvec_to_uvec(ctx
, constant
, num_components
);
1237 store_ssa_def(ctx
, &load_const
->def
, constant
);
1241 emit_load_ubo(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
1243 nir_const_value
*const_block_index
= nir_src_as_const_value(intr
->src
[0]);
1244 assert(const_block_index
); // no dynamic indexing for now
1245 assert(const_block_index
->u32
== 0); // we only support the default UBO for now
1247 nir_const_value
*const_offset
= nir_src_as_const_value(intr
->src
[1]);
1249 SpvId uvec4_type
= get_uvec_type(ctx
, 32, 4);
1250 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
1251 SpvStorageClassUniform
,
1254 unsigned idx
= const_offset
->u32
;
1255 SpvId member
= emit_uint_const(ctx
, 32, 0);
1256 SpvId offset
= emit_uint_const(ctx
, 32, idx
);
1257 SpvId offsets
[] = { member
, offset
};
1258 SpvId ptr
= spirv_builder_emit_access_chain(&ctx
->builder
, pointer_type
,
1259 ctx
->ubos
[0], offsets
,
1260 ARRAY_SIZE(offsets
));
1261 SpvId result
= spirv_builder_emit_load(&ctx
->builder
, uvec4_type
, ptr
);
1263 SpvId type
= get_dest_uvec_type(ctx
, &intr
->dest
);
1264 unsigned num_components
= nir_dest_num_components(intr
->dest
);
1265 if (num_components
== 1) {
1266 uint32_t components
[] = { 0 };
1267 result
= spirv_builder_emit_composite_extract(&ctx
->builder
,
1271 } else if (num_components
< 4) {
1272 SpvId constituents
[num_components
];
1273 SpvId uint_type
= spirv_builder_type_uint(&ctx
->builder
, 32);
1274 for (uint32_t i
= 0; i
< num_components
; ++i
)
1275 constituents
[i
] = spirv_builder_emit_composite_extract(&ctx
->builder
,
1280 result
= spirv_builder_emit_composite_construct(&ctx
->builder
,
1286 store_dest(ctx
, &intr
->dest
, result
, nir_type_uint
);
1288 unreachable("uniform-addressing not yet supported");
1292 emit_discard(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
1294 assert(ctx
->block_started
);
1295 spirv_builder_emit_kill(&ctx
->builder
);
1296 /* discard is weird in NIR, so let's just create an unreachable block after
1297 it and hope that the vulkan driver will DCE any instructinos in it. */
1298 spirv_builder_label(&ctx
->builder
, spirv_builder_new_id(&ctx
->builder
));
1302 emit_load_deref(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
1304 SpvId ptr
= get_src(ctx
, intr
->src
);
1306 nir_variable
*var
= nir_intrinsic_get_var(intr
, 0);
1307 SpvId result
= spirv_builder_emit_load(&ctx
->builder
,
1308 get_glsl_type(ctx
, var
->type
),
1310 unsigned num_components
= nir_dest_num_components(intr
->dest
);
1311 unsigned bit_size
= nir_dest_bit_size(intr
->dest
);
1312 result
= bitcast_to_uvec(ctx
, result
, bit_size
, num_components
);
1313 store_dest(ctx
, &intr
->dest
, result
, nir_type_uint
);
1317 emit_store_deref(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
1319 SpvId ptr
= get_src(ctx
, &intr
->src
[0]);
1320 SpvId src
= get_src(ctx
, &intr
->src
[1]);
1322 nir_variable
*var
= nir_intrinsic_get_var(intr
, 0);
1323 SpvId type
= get_glsl_type(ctx
, glsl_without_array(var
->type
));
1324 SpvId result
= emit_bitcast(ctx
, type
, src
);
1325 spirv_builder_emit_store(&ctx
->builder
, ptr
, result
);
1329 create_builtin_var(struct ntv_context
*ctx
, SpvId var_type
,
1330 SpvStorageClass storage_class
,
1331 const char *name
, SpvBuiltIn builtin
)
1333 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
1336 SpvId var
= spirv_builder_emit_var(&ctx
->builder
, pointer_type
,
1338 spirv_builder_emit_name(&ctx
->builder
, var
, name
);
1339 spirv_builder_emit_builtin(&ctx
->builder
, var
, builtin
);
1341 assert(ctx
->num_entry_ifaces
< ARRAY_SIZE(ctx
->entry_ifaces
));
1342 ctx
->entry_ifaces
[ctx
->num_entry_ifaces
++] = var
;
1347 emit_load_front_face(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
1349 SpvId var_type
= spirv_builder_type_bool(&ctx
->builder
);
1350 if (!ctx
->front_face_var
)
1351 ctx
->front_face_var
= create_builtin_var(ctx
, var_type
,
1352 SpvStorageClassInput
,
1354 SpvBuiltInFrontFacing
);
1356 SpvId result
= spirv_builder_emit_load(&ctx
->builder
, var_type
,
1357 ctx
->front_face_var
);
1358 assert(1 == nir_dest_num_components(intr
->dest
));
1359 store_dest(ctx
, &intr
->dest
, result
, nir_type_bool
);
1363 emit_load_instance_id(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
1365 SpvId var_type
= spirv_builder_type_uint(&ctx
->builder
, 32);
1366 if (!ctx
->instance_id_var
)
1367 ctx
->instance_id_var
= create_builtin_var(ctx
, var_type
,
1368 SpvStorageClassInput
,
1370 SpvBuiltInInstanceIndex
);
1372 SpvId result
= spirv_builder_emit_load(&ctx
->builder
, var_type
,
1373 ctx
->instance_id_var
);
1374 assert(1 == nir_dest_num_components(intr
->dest
));
1375 store_dest(ctx
, &intr
->dest
, result
, nir_type_uint
);
1379 emit_load_vertex_id(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
1381 SpvId var_type
= spirv_builder_type_uint(&ctx
->builder
, 32);
1382 if (!ctx
->vertex_id_var
)
1383 ctx
->vertex_id_var
= create_builtin_var(ctx
, var_type
,
1384 SpvStorageClassInput
,
1386 SpvBuiltInVertexIndex
);
1388 SpvId result
= spirv_builder_emit_load(&ctx
->builder
, var_type
,
1389 ctx
->vertex_id_var
);
1390 assert(1 == nir_dest_num_components(intr
->dest
));
1391 store_dest(ctx
, &intr
->dest
, result
, nir_type_uint
);
1395 emit_intrinsic(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
1397 switch (intr
->intrinsic
) {
1398 case nir_intrinsic_load_ubo
:
1399 emit_load_ubo(ctx
, intr
);
1402 case nir_intrinsic_discard
:
1403 emit_discard(ctx
, intr
);
1406 case nir_intrinsic_load_deref
:
1407 emit_load_deref(ctx
, intr
);
1410 case nir_intrinsic_store_deref
:
1411 emit_store_deref(ctx
, intr
);
1414 case nir_intrinsic_load_front_face
:
1415 emit_load_front_face(ctx
, intr
);
1418 case nir_intrinsic_load_instance_id
:
1419 emit_load_instance_id(ctx
, intr
);
1422 case nir_intrinsic_load_vertex_id
:
1423 emit_load_vertex_id(ctx
, intr
);
1427 fprintf(stderr
, "emit_intrinsic: not implemented (%s)\n",
1428 nir_intrinsic_infos
[intr
->intrinsic
].name
);
1429 unreachable("unsupported intrinsic");
1434 emit_undef(struct ntv_context
*ctx
, nir_ssa_undef_instr
*undef
)
1436 SpvId type
= get_uvec_type(ctx
, undef
->def
.bit_size
,
1437 undef
->def
.num_components
);
1439 store_ssa_def(ctx
, &undef
->def
,
1440 spirv_builder_emit_undef(&ctx
->builder
, type
));
1444 get_src_float(struct ntv_context
*ctx
, nir_src
*src
)
1446 SpvId def
= get_src(ctx
, src
);
1447 unsigned num_components
= nir_src_num_components(*src
);
1448 unsigned bit_size
= nir_src_bit_size(*src
);
1449 return bitcast_to_fvec(ctx
, def
, bit_size
, num_components
);
1453 get_src_int(struct ntv_context
*ctx
, nir_src
*src
)
1455 SpvId def
= get_src(ctx
, src
);
1456 unsigned num_components
= nir_src_num_components(*src
);
1457 unsigned bit_size
= nir_src_bit_size(*src
);
1458 return bitcast_to_ivec(ctx
, def
, bit_size
, num_components
);
1462 emit_tex(struct ntv_context
*ctx
, nir_tex_instr
*tex
)
1464 assert(tex
->op
== nir_texop_tex
||
1465 tex
->op
== nir_texop_txb
||
1466 tex
->op
== nir_texop_txl
||
1467 tex
->op
== nir_texop_txd
||
1468 tex
->op
== nir_texop_txf
||
1469 tex
->op
== nir_texop_txs
);
1470 assert(tex
->texture_index
== tex
->sampler_index
);
1472 SpvId coord
= 0, proj
= 0, bias
= 0, lod
= 0, dref
= 0, dx
= 0, dy
= 0,
1474 unsigned coord_components
= 0;
1475 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
1476 switch (tex
->src
[i
].src_type
) {
1477 case nir_tex_src_coord
:
1478 if (tex
->op
== nir_texop_txf
)
1479 coord
= get_src_int(ctx
, &tex
->src
[i
].src
);
1481 coord
= get_src_float(ctx
, &tex
->src
[i
].src
);
1482 coord_components
= nir_src_num_components(tex
->src
[i
].src
);
1485 case nir_tex_src_projector
:
1486 assert(nir_src_num_components(tex
->src
[i
].src
) == 1);
1487 proj
= get_src_float(ctx
, &tex
->src
[i
].src
);
1491 case nir_tex_src_offset
:
1492 offset
= get_src_int(ctx
, &tex
->src
[i
].src
);
1495 case nir_tex_src_bias
:
1496 assert(tex
->op
== nir_texop_txb
);
1497 bias
= get_src_float(ctx
, &tex
->src
[i
].src
);
1501 case nir_tex_src_lod
:
1502 assert(nir_src_num_components(tex
->src
[i
].src
) == 1);
1503 if (tex
->op
== nir_texop_txf
||
1504 tex
->op
== nir_texop_txs
)
1505 lod
= get_src_int(ctx
, &tex
->src
[i
].src
);
1507 lod
= get_src_float(ctx
, &tex
->src
[i
].src
);
1511 case nir_tex_src_comparator
:
1512 assert(nir_src_num_components(tex
->src
[i
].src
) == 1);
1513 dref
= get_src_float(ctx
, &tex
->src
[i
].src
);
1517 case nir_tex_src_ddx
:
1518 dx
= get_src_float(ctx
, &tex
->src
[i
].src
);
1522 case nir_tex_src_ddy
:
1523 dy
= get_src_float(ctx
, &tex
->src
[i
].src
);
1528 fprintf(stderr
, "texture source: %d\n", tex
->src
[i
].src_type
);
1529 unreachable("unknown texture source");
1533 if (lod
== 0 && ctx
->stage
!= MESA_SHADER_FRAGMENT
) {
1534 lod
= emit_float_const(ctx
, 32, 0.0f
);
1538 SpvId image_type
= ctx
->image_types
[tex
->texture_index
];
1539 SpvId sampled_type
= spirv_builder_type_sampled_image(&ctx
->builder
,
1542 assert(ctx
->samplers_used
& (1u << tex
->texture_index
));
1543 SpvId load
= spirv_builder_emit_load(&ctx
->builder
, sampled_type
,
1544 ctx
->samplers
[tex
->texture_index
]);
1546 SpvId dest_type
= get_dest_type(ctx
, &tex
->dest
, tex
->dest_type
);
1548 if (tex
->op
== nir_texop_txs
) {
1549 SpvId image
= spirv_builder_emit_image(&ctx
->builder
, image_type
, load
);
1550 SpvId result
= spirv_builder_emit_image_query_size(&ctx
->builder
,
1553 store_dest(ctx
, &tex
->dest
, result
, tex
->dest_type
);
1557 if (proj
&& coord_components
> 0) {
1558 SpvId constituents
[coord_components
+ 1];
1559 if (coord_components
== 1)
1560 constituents
[0] = coord
;
1562 assert(coord_components
> 1);
1563 SpvId float_type
= spirv_builder_type_float(&ctx
->builder
, 32);
1564 for (uint32_t i
= 0; i
< coord_components
; ++i
)
1565 constituents
[i
] = spirv_builder_emit_composite_extract(&ctx
->builder
,
1571 constituents
[coord_components
++] = proj
;
1573 SpvId vec_type
= get_fvec_type(ctx
, 32, coord_components
);
1574 coord
= spirv_builder_emit_composite_construct(&ctx
->builder
,
1580 SpvId actual_dest_type
= dest_type
;
1582 actual_dest_type
= spirv_builder_type_float(&ctx
->builder
, 32);
1585 if (tex
->op
== nir_texop_txf
) {
1586 SpvId image
= spirv_builder_emit_image(&ctx
->builder
, image_type
, load
);
1587 result
= spirv_builder_emit_image_fetch(&ctx
->builder
, dest_type
,
1590 result
= spirv_builder_emit_image_sample(&ctx
->builder
,
1591 actual_dest_type
, load
,
1594 lod
, bias
, dref
, dx
, dy
,
1598 spirv_builder_emit_decoration(&ctx
->builder
, result
,
1599 SpvDecorationRelaxedPrecision
);
1601 if (dref
&& nir_dest_num_components(tex
->dest
) > 1) {
1602 SpvId components
[4] = { result
, result
, result
, result
};
1603 result
= spirv_builder_emit_composite_construct(&ctx
->builder
,
1609 store_dest(ctx
, &tex
->dest
, result
, tex
->dest_type
);
1613 start_block(struct ntv_context
*ctx
, SpvId label
)
1615 /* terminate previous block if needed */
1616 if (ctx
->block_started
)
1617 spirv_builder_emit_branch(&ctx
->builder
, label
);
1619 /* start new block */
1620 spirv_builder_label(&ctx
->builder
, label
);
1621 ctx
->block_started
= true;
1625 branch(struct ntv_context
*ctx
, SpvId label
)
1627 assert(ctx
->block_started
);
1628 spirv_builder_emit_branch(&ctx
->builder
, label
);
1629 ctx
->block_started
= false;
1633 branch_conditional(struct ntv_context
*ctx
, SpvId condition
, SpvId then_id
,
1636 assert(ctx
->block_started
);
1637 spirv_builder_emit_branch_conditional(&ctx
->builder
, condition
,
1639 ctx
->block_started
= false;
1643 emit_jump(struct ntv_context
*ctx
, nir_jump_instr
*jump
)
1645 switch (jump
->type
) {
1646 case nir_jump_break
:
1647 assert(ctx
->loop_break
);
1648 branch(ctx
, ctx
->loop_break
);
1651 case nir_jump_continue
:
1652 assert(ctx
->loop_cont
);
1653 branch(ctx
, ctx
->loop_cont
);
1657 unreachable("Unsupported jump type\n");
1662 emit_deref_var(struct ntv_context
*ctx
, nir_deref_instr
*deref
)
1664 assert(deref
->deref_type
== nir_deref_type_var
);
1666 struct hash_entry
*he
= _mesa_hash_table_search(ctx
->vars
, deref
->var
);
1668 SpvId result
= (SpvId
)(intptr_t)he
->data
;
1669 /* uint is a bit of a lie here, it's really just an opaque type */
1670 store_dest(ctx
, &deref
->dest
, result
, nir_type_uint
);
1674 emit_deref_array(struct ntv_context
*ctx
, nir_deref_instr
*deref
)
1676 assert(deref
->deref_type
== nir_deref_type_array
);
1677 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
1679 SpvStorageClass storage_class
;
1680 switch (var
->data
.mode
) {
1681 case nir_var_shader_in
:
1682 storage_class
= SpvStorageClassInput
;
1685 case nir_var_shader_out
:
1686 storage_class
= SpvStorageClassOutput
;
1690 unreachable("Unsupported nir_variable_mode\n");
1693 SpvId index
= get_src(ctx
, &deref
->arr
.index
);
1695 SpvId ptr_type
= spirv_builder_type_pointer(&ctx
->builder
,
1697 get_glsl_type(ctx
, deref
->type
));
1699 SpvId result
= spirv_builder_emit_access_chain(&ctx
->builder
,
1701 get_src(ctx
, &deref
->parent
),
1703 /* uint is a bit of a lie here, it's really just an opaque type */
1704 store_dest(ctx
, &deref
->dest
, result
, nir_type_uint
);
1708 emit_deref(struct ntv_context
*ctx
, nir_deref_instr
*deref
)
1710 switch (deref
->deref_type
) {
1711 case nir_deref_type_var
:
1712 emit_deref_var(ctx
, deref
);
1715 case nir_deref_type_array
:
1716 emit_deref_array(ctx
, deref
);
1720 unreachable("unexpected deref_type");
1725 emit_block(struct ntv_context
*ctx
, struct nir_block
*block
)
1727 start_block(ctx
, block_label(ctx
, block
));
1728 nir_foreach_instr(instr
, block
) {
1729 switch (instr
->type
) {
1730 case nir_instr_type_alu
:
1731 emit_alu(ctx
, nir_instr_as_alu(instr
));
1733 case nir_instr_type_intrinsic
:
1734 emit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
1736 case nir_instr_type_load_const
:
1737 emit_load_const(ctx
, nir_instr_as_load_const(instr
));
1739 case nir_instr_type_ssa_undef
:
1740 emit_undef(ctx
, nir_instr_as_ssa_undef(instr
));
1742 case nir_instr_type_tex
:
1743 emit_tex(ctx
, nir_instr_as_tex(instr
));
1745 case nir_instr_type_phi
:
1746 unreachable("nir_instr_type_phi not supported");
1748 case nir_instr_type_jump
:
1749 emit_jump(ctx
, nir_instr_as_jump(instr
));
1751 case nir_instr_type_call
:
1752 unreachable("nir_instr_type_call not supported");
1754 case nir_instr_type_parallel_copy
:
1755 unreachable("nir_instr_type_parallel_copy not supported");
1757 case nir_instr_type_deref
:
1758 emit_deref(ctx
, nir_instr_as_deref(instr
));
1765 emit_cf_list(struct ntv_context
*ctx
, struct exec_list
*list
);
1768 get_src_bool(struct ntv_context
*ctx
, nir_src
*src
)
1770 SpvId def
= get_src(ctx
, src
);
1771 assert(nir_src_bit_size(*src
) == 1);
1772 unsigned num_components
= nir_src_num_components(*src
);
1773 return uvec_to_bvec(ctx
, def
, num_components
);
1777 emit_if(struct ntv_context
*ctx
, nir_if
*if_stmt
)
1779 SpvId condition
= get_src_bool(ctx
, &if_stmt
->condition
);
1781 SpvId header_id
= spirv_builder_new_id(&ctx
->builder
);
1782 SpvId then_id
= block_label(ctx
, nir_if_first_then_block(if_stmt
));
1783 SpvId endif_id
= spirv_builder_new_id(&ctx
->builder
);
1784 SpvId else_id
= endif_id
;
1786 bool has_else
= !exec_list_is_empty(&if_stmt
->else_list
);
1788 assert(nir_if_first_else_block(if_stmt
)->index
< ctx
->num_blocks
);
1789 else_id
= block_label(ctx
, nir_if_first_else_block(if_stmt
));
1792 /* create a header-block */
1793 start_block(ctx
, header_id
);
1794 spirv_builder_emit_selection_merge(&ctx
->builder
, endif_id
,
1795 SpvSelectionControlMaskNone
);
1796 branch_conditional(ctx
, condition
, then_id
, else_id
);
1798 emit_cf_list(ctx
, &if_stmt
->then_list
);
1801 if (ctx
->block_started
)
1802 branch(ctx
, endif_id
);
1804 emit_cf_list(ctx
, &if_stmt
->else_list
);
1807 start_block(ctx
, endif_id
);
1811 emit_loop(struct ntv_context
*ctx
, nir_loop
*loop
)
1813 SpvId header_id
= spirv_builder_new_id(&ctx
->builder
);
1814 SpvId begin_id
= block_label(ctx
, nir_loop_first_block(loop
));
1815 SpvId break_id
= spirv_builder_new_id(&ctx
->builder
);
1816 SpvId cont_id
= spirv_builder_new_id(&ctx
->builder
);
1818 /* create a header-block */
1819 start_block(ctx
, header_id
);
1820 spirv_builder_loop_merge(&ctx
->builder
, break_id
, cont_id
, SpvLoopControlMaskNone
);
1821 branch(ctx
, begin_id
);
1823 SpvId save_break
= ctx
->loop_break
;
1824 SpvId save_cont
= ctx
->loop_cont
;
1825 ctx
->loop_break
= break_id
;
1826 ctx
->loop_cont
= cont_id
;
1828 emit_cf_list(ctx
, &loop
->body
);
1830 ctx
->loop_break
= save_break
;
1831 ctx
->loop_cont
= save_cont
;
1833 branch(ctx
, cont_id
);
1834 start_block(ctx
, cont_id
);
1835 branch(ctx
, header_id
);
1837 start_block(ctx
, break_id
);
1841 emit_cf_list(struct ntv_context
*ctx
, struct exec_list
*list
)
1843 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
1844 switch (node
->type
) {
1845 case nir_cf_node_block
:
1846 emit_block(ctx
, nir_cf_node_as_block(node
));
1849 case nir_cf_node_if
:
1850 emit_if(ctx
, nir_cf_node_as_if(node
));
1853 case nir_cf_node_loop
:
1854 emit_loop(ctx
, nir_cf_node_as_loop(node
));
1857 case nir_cf_node_function
:
1858 unreachable("nir_cf_node_function not supported");
1864 struct spirv_shader
*
1865 nir_to_spirv(struct nir_shader
*s
)
1867 struct spirv_shader
*ret
= NULL
;
1869 struct ntv_context ctx
= {};
1871 switch (s
->info
.stage
) {
1872 case MESA_SHADER_VERTEX
:
1873 case MESA_SHADER_FRAGMENT
:
1874 case MESA_SHADER_COMPUTE
:
1875 spirv_builder_emit_cap(&ctx
.builder
, SpvCapabilityShader
);
1878 case MESA_SHADER_TESS_CTRL
:
1879 case MESA_SHADER_TESS_EVAL
:
1880 spirv_builder_emit_cap(&ctx
.builder
, SpvCapabilityTessellation
);
1883 case MESA_SHADER_GEOMETRY
:
1884 spirv_builder_emit_cap(&ctx
.builder
, SpvCapabilityGeometry
);
1888 unreachable("invalid stage");
1891 // TODO: only enable when needed
1892 if (s
->info
.stage
== MESA_SHADER_FRAGMENT
) {
1893 spirv_builder_emit_cap(&ctx
.builder
, SpvCapabilitySampled1D
);
1894 spirv_builder_emit_cap(&ctx
.builder
, SpvCapabilityImageQuery
);
1895 spirv_builder_emit_cap(&ctx
.builder
, SpvCapabilityDerivativeControl
);
1898 ctx
.stage
= s
->info
.stage
;
1899 ctx
.GLSL_std_450
= spirv_builder_import(&ctx
.builder
, "GLSL.std.450");
1900 spirv_builder_emit_source(&ctx
.builder
, SpvSourceLanguageGLSL
, 450);
1902 spirv_builder_emit_mem_model(&ctx
.builder
, SpvAddressingModelLogical
,
1903 SpvMemoryModelGLSL450
);
1905 SpvExecutionModel exec_model
;
1906 switch (s
->info
.stage
) {
1907 case MESA_SHADER_VERTEX
:
1908 exec_model
= SpvExecutionModelVertex
;
1910 case MESA_SHADER_TESS_CTRL
:
1911 exec_model
= SpvExecutionModelTessellationControl
;
1913 case MESA_SHADER_TESS_EVAL
:
1914 exec_model
= SpvExecutionModelTessellationEvaluation
;
1916 case MESA_SHADER_GEOMETRY
:
1917 exec_model
= SpvExecutionModelGeometry
;
1919 case MESA_SHADER_FRAGMENT
:
1920 exec_model
= SpvExecutionModelFragment
;
1922 case MESA_SHADER_COMPUTE
:
1923 exec_model
= SpvExecutionModelGLCompute
;
1926 unreachable("invalid stage");
1929 SpvId type_void
= spirv_builder_type_void(&ctx
.builder
);
1930 SpvId type_main
= spirv_builder_type_function(&ctx
.builder
, type_void
,
1932 SpvId entry_point
= spirv_builder_new_id(&ctx
.builder
);
1933 spirv_builder_emit_name(&ctx
.builder
, entry_point
, "main");
1935 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
1936 _mesa_key_pointer_equal
);
1938 nir_foreach_variable(var
, &s
->inputs
)
1939 emit_input(&ctx
, var
);
1941 nir_foreach_variable(var
, &s
->outputs
)
1942 emit_output(&ctx
, var
);
1944 nir_foreach_variable(var
, &s
->uniforms
)
1945 emit_uniform(&ctx
, var
);
1947 if (s
->info
.stage
== MESA_SHADER_FRAGMENT
) {
1948 spirv_builder_emit_exec_mode(&ctx
.builder
, entry_point
,
1949 SpvExecutionModeOriginUpperLeft
);
1950 if (s
->info
.outputs_written
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
))
1951 spirv_builder_emit_exec_mode(&ctx
.builder
, entry_point
,
1952 SpvExecutionModeDepthReplacing
);
1956 spirv_builder_function(&ctx
.builder
, entry_point
, type_void
,
1957 SpvFunctionControlMaskNone
,
1960 nir_function_impl
*entry
= nir_shader_get_entrypoint(s
);
1961 nir_metadata_require(entry
, nir_metadata_block_index
);
1963 ctx
.defs
= (SpvId
*)malloc(sizeof(SpvId
) * entry
->ssa_alloc
);
1966 ctx
.num_defs
= entry
->ssa_alloc
;
1968 nir_index_local_regs(entry
);
1969 ctx
.regs
= malloc(sizeof(SpvId
) * entry
->reg_alloc
);
1972 ctx
.num_regs
= entry
->reg_alloc
;
1974 SpvId
*block_ids
= (SpvId
*)malloc(sizeof(SpvId
) * entry
->num_blocks
);
1978 for (int i
= 0; i
< entry
->num_blocks
; ++i
)
1979 block_ids
[i
] = spirv_builder_new_id(&ctx
.builder
);
1981 ctx
.block_ids
= block_ids
;
1982 ctx
.num_blocks
= entry
->num_blocks
;
1984 /* emit a block only for the variable declarations */
1985 start_block(&ctx
, spirv_builder_new_id(&ctx
.builder
));
1986 foreach_list_typed(nir_register
, reg
, node
, &entry
->registers
) {
1987 SpvId type
= get_uvec_type(&ctx
, reg
->bit_size
, reg
->num_components
);
1988 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
.builder
,
1989 SpvStorageClassFunction
,
1991 SpvId var
= spirv_builder_emit_var(&ctx
.builder
, pointer_type
,
1992 SpvStorageClassFunction
);
1994 ctx
.regs
[reg
->index
] = var
;
1997 emit_cf_list(&ctx
, &entry
->body
);
2001 spirv_builder_return(&ctx
.builder
); // doesn't belong here, but whatevz
2002 spirv_builder_function_end(&ctx
.builder
);
2004 spirv_builder_emit_entry_point(&ctx
.builder
, exec_model
, entry_point
,
2005 "main", ctx
.entry_ifaces
,
2006 ctx
.num_entry_ifaces
);
2008 size_t num_words
= spirv_builder_get_num_words(&ctx
.builder
);
2010 ret
= CALLOC_STRUCT(spirv_shader
);
2014 ret
->words
= MALLOC(sizeof(uint32_t) * num_words
);
2018 ret
->num_words
= spirv_builder_get_words(&ctx
.builder
, ret
->words
, num_words
);
2019 assert(ret
->num_words
== num_words
);
2026 spirv_shader_delete(ret
);
2029 _mesa_hash_table_destroy(ctx
.vars
, NULL
);
2035 spirv_shader_delete(struct spirv_shader
*s
)