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
;
42 SpvId samplers
[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
;
62 get_fvec_constant(struct ntv_context
*ctx
, int bit_size
, int num_components
,
63 const float values
[]);
66 get_uvec_constant(struct ntv_context
*ctx
, int bit_size
, int num_components
,
67 const uint32_t values
[]);
70 emit_unop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
, SpvId src
);
73 emit_binop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
,
74 SpvId src0
, SpvId src1
);
77 emit_triop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
,
78 SpvId src0
, SpvId src1
, SpvId src2
);
81 get_bvec_type(struct ntv_context
*ctx
, int num_components
)
83 SpvId bool_type
= spirv_builder_type_bool(&ctx
->builder
);
84 if (num_components
> 1)
85 return spirv_builder_type_vector(&ctx
->builder
, bool_type
,
88 assert(num_components
== 1);
93 block_label(struct ntv_context
*ctx
, nir_block
*block
)
95 assert(block
->index
< ctx
->num_blocks
);
96 return ctx
->block_ids
[block
->index
];
100 get_fvec_type(struct ntv_context
*ctx
, unsigned bit_size
, unsigned num_components
)
102 assert(bit_size
== 32); // only 32-bit floats supported so far
104 SpvId float_type
= spirv_builder_type_float(&ctx
->builder
, bit_size
);
105 if (num_components
> 1)
106 return spirv_builder_type_vector(&ctx
->builder
, float_type
,
109 assert(num_components
== 1);
114 get_ivec_type(struct ntv_context
*ctx
, unsigned bit_size
, unsigned num_components
)
116 assert(bit_size
== 32); // only 32-bit ints supported so far
118 SpvId int_type
= spirv_builder_type_int(&ctx
->builder
, bit_size
);
119 if (num_components
> 1)
120 return spirv_builder_type_vector(&ctx
->builder
, int_type
,
123 assert(num_components
== 1);
128 get_uvec_type(struct ntv_context
*ctx
, unsigned bit_size
, unsigned num_components
)
130 assert(bit_size
== 32); // only 32-bit uints supported so far
132 SpvId uint_type
= spirv_builder_type_uint(&ctx
->builder
, bit_size
);
133 if (num_components
> 1)
134 return spirv_builder_type_vector(&ctx
->builder
, uint_type
,
137 assert(num_components
== 1);
142 get_dest_uvec_type(struct ntv_context
*ctx
, nir_dest
*dest
)
144 return get_uvec_type(ctx
, nir_dest_bit_size(*dest
),
145 nir_dest_num_components(*dest
));
149 get_glsl_basetype(struct ntv_context
*ctx
, enum glsl_base_type type
)
152 case GLSL_TYPE_FLOAT
:
153 return spirv_builder_type_float(&ctx
->builder
, 32);
156 return spirv_builder_type_int(&ctx
->builder
, 32);
159 return spirv_builder_type_uint(&ctx
->builder
, 32);
160 /* TODO: handle more types */
163 unreachable("unknown GLSL type");
168 get_glsl_type(struct ntv_context
*ctx
, const struct glsl_type
*type
)
171 if (glsl_type_is_scalar(type
))
172 return get_glsl_basetype(ctx
, glsl_get_base_type(type
));
174 if (glsl_type_is_vector(type
))
175 return spirv_builder_type_vector(&ctx
->builder
,
176 get_glsl_basetype(ctx
, glsl_get_base_type(type
)),
177 glsl_get_vector_elements(type
));
179 unreachable("we shouldn't get here, I think...");
183 emit_input(struct ntv_context
*ctx
, struct nir_variable
*var
)
185 SpvId var_type
= get_glsl_type(ctx
, var
->type
);
186 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
187 SpvStorageClassInput
,
189 SpvId var_id
= spirv_builder_emit_var(&ctx
->builder
, pointer_type
,
190 SpvStorageClassInput
);
193 spirv_builder_emit_name(&ctx
->builder
, var_id
, var
->name
);
195 if (ctx
->stage
== MESA_SHADER_FRAGMENT
) {
196 if (var
->data
.location
>= VARYING_SLOT_VAR0
||
197 (var
->data
.location
>= VARYING_SLOT_COL0
&&
198 var
->data
.location
<= VARYING_SLOT_TEX7
)) {
199 spirv_builder_emit_location(&ctx
->builder
, var_id
,
200 ctx
->var_location
++);
202 switch (var
->data
.location
) {
203 case VARYING_SLOT_POS
:
204 spirv_builder_emit_builtin(&ctx
->builder
, var_id
, SpvBuiltInFragCoord
);
207 case VARYING_SLOT_PNTC
:
208 spirv_builder_emit_builtin(&ctx
->builder
, var_id
, SpvBuiltInPointCoord
);
212 unreachable("unknown varying slot");
216 spirv_builder_emit_location(&ctx
->builder
, var_id
,
217 var
->data
.driver_location
);
220 if (var
->data
.location_frac
)
221 spirv_builder_emit_component(&ctx
->builder
, var_id
,
222 var
->data
.location_frac
);
224 if (var
->data
.interpolation
== INTERP_MODE_FLAT
)
225 spirv_builder_emit_decoration(&ctx
->builder
, var_id
, SpvDecorationFlat
);
227 _mesa_hash_table_insert(ctx
->vars
, var
, (void *)(intptr_t)var_id
);
229 assert(ctx
->num_entry_ifaces
< ARRAY_SIZE(ctx
->entry_ifaces
));
230 ctx
->entry_ifaces
[ctx
->num_entry_ifaces
++] = var_id
;
234 emit_output(struct ntv_context
*ctx
, struct nir_variable
*var
)
236 SpvId var_type
= get_glsl_type(ctx
, var
->type
);
237 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
238 SpvStorageClassOutput
,
240 SpvId var_id
= spirv_builder_emit_var(&ctx
->builder
, pointer_type
,
241 SpvStorageClassOutput
);
243 spirv_builder_emit_name(&ctx
->builder
, var_id
, var
->name
);
246 if (ctx
->stage
== MESA_SHADER_VERTEX
) {
247 if (var
->data
.location
>= VARYING_SLOT_VAR0
||
248 (var
->data
.location
>= VARYING_SLOT_COL0
&&
249 var
->data
.location
<= VARYING_SLOT_TEX7
)) {
250 spirv_builder_emit_location(&ctx
->builder
, var_id
,
251 ctx
->var_location
++);
253 switch (var
->data
.location
) {
254 case VARYING_SLOT_POS
:
255 spirv_builder_emit_builtin(&ctx
->builder
, var_id
, SpvBuiltInPosition
);
258 case VARYING_SLOT_PSIZ
:
259 spirv_builder_emit_builtin(&ctx
->builder
, var_id
, SpvBuiltInPointSize
);
263 unreachable("unknown varying slot");
266 } else if (ctx
->stage
== MESA_SHADER_FRAGMENT
) {
267 switch (var
->data
.location
) {
268 case FRAG_RESULT_DEPTH
:
269 spirv_builder_emit_builtin(&ctx
->builder
, var_id
, SpvBuiltInFragDepth
);
273 spirv_builder_emit_location(&ctx
->builder
, var_id
,
274 var
->data
.driver_location
);
278 if (var
->data
.location_frac
)
279 spirv_builder_emit_component(&ctx
->builder
, var_id
,
280 var
->data
.location_frac
);
282 _mesa_hash_table_insert(ctx
->vars
, var
, (void *)(intptr_t)var_id
);
284 assert(ctx
->num_entry_ifaces
< ARRAY_SIZE(ctx
->entry_ifaces
));
285 ctx
->entry_ifaces
[ctx
->num_entry_ifaces
++] = var_id
;
289 type_to_dim(enum glsl_sampler_dim gdim
, bool *is_ms
)
293 case GLSL_SAMPLER_DIM_1D
:
295 case GLSL_SAMPLER_DIM_2D
:
297 case GLSL_SAMPLER_DIM_RECT
:
299 case GLSL_SAMPLER_DIM_CUBE
:
301 case GLSL_SAMPLER_DIM_3D
:
303 case GLSL_SAMPLER_DIM_MS
:
307 fprintf(stderr
, "unknown sampler type %d\n", gdim
);
314 emit_sampler(struct ntv_context
*ctx
, struct nir_variable
*var
)
317 SpvDim dimension
= type_to_dim(glsl_get_sampler_dim(var
->type
), &is_ms
);
318 SpvId float_type
= spirv_builder_type_float(&ctx
->builder
, 32);
319 SpvId image_type
= spirv_builder_type_image(&ctx
->builder
, float_type
,
320 dimension
, false, glsl_sampler_type_is_array(var
->type
), is_ms
, 1,
321 SpvImageFormatUnknown
);
323 SpvId sampled_type
= spirv_builder_type_sampled_image(&ctx
->builder
,
325 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
326 SpvStorageClassUniformConstant
,
328 SpvId var_id
= spirv_builder_emit_var(&ctx
->builder
, pointer_type
,
329 SpvStorageClassUniformConstant
);
332 spirv_builder_emit_name(&ctx
->builder
, var_id
, var
->name
);
334 assert(ctx
->num_samplers
< ARRAY_SIZE(ctx
->samplers
));
335 ctx
->samplers
[ctx
->num_samplers
++] = var_id
;
337 spirv_builder_emit_descriptor_set(&ctx
->builder
, var_id
,
338 var
->data
.descriptor_set
);
339 spirv_builder_emit_binding(&ctx
->builder
, var_id
, var
->data
.binding
);
343 emit_ubo(struct ntv_context
*ctx
, struct nir_variable
*var
)
345 uint32_t size
= glsl_count_attribute_slots(var
->type
, false);
346 SpvId vec4_type
= get_uvec_type(ctx
, 32, 4);
347 SpvId array_length
= spirv_builder_const_uint(&ctx
->builder
, 32, size
);
348 SpvId array_type
= spirv_builder_type_array(&ctx
->builder
, vec4_type
,
350 spirv_builder_emit_array_stride(&ctx
->builder
, array_type
, 16);
352 // wrap UBO-array in a struct
353 SpvId struct_type
= spirv_builder_type_struct(&ctx
->builder
, &array_type
, 1);
355 char struct_name
[100];
356 snprintf(struct_name
, sizeof(struct_name
), "struct_%s", var
->name
);
357 spirv_builder_emit_name(&ctx
->builder
, struct_type
, struct_name
);
360 spirv_builder_emit_decoration(&ctx
->builder
, struct_type
,
362 spirv_builder_emit_member_offset(&ctx
->builder
, struct_type
, 0, 0);
365 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
366 SpvStorageClassUniform
,
369 SpvId var_id
= spirv_builder_emit_var(&ctx
->builder
, pointer_type
,
370 SpvStorageClassUniform
);
372 spirv_builder_emit_name(&ctx
->builder
, var_id
, var
->name
);
374 assert(ctx
->num_ubos
< ARRAY_SIZE(ctx
->ubos
));
375 ctx
->ubos
[ctx
->num_ubos
++] = var_id
;
377 spirv_builder_emit_descriptor_set(&ctx
->builder
, var_id
,
378 var
->data
.descriptor_set
);
379 spirv_builder_emit_binding(&ctx
->builder
, var_id
, var
->data
.binding
);
383 emit_uniform(struct ntv_context
*ctx
, struct nir_variable
*var
)
385 if (glsl_type_is_sampler(var
->type
))
386 emit_sampler(ctx
, var
);
387 else if (var
->interface_type
)
392 get_src_uint_ssa(struct ntv_context
*ctx
, const nir_ssa_def
*ssa
)
394 assert(ssa
->index
< ctx
->num_defs
);
395 assert(ctx
->defs
[ssa
->index
] != 0);
396 return ctx
->defs
[ssa
->index
];
400 get_var_from_reg(struct ntv_context
*ctx
, nir_register
*reg
)
402 assert(reg
->index
< ctx
->num_regs
);
403 assert(ctx
->regs
[reg
->index
] != 0);
404 return ctx
->regs
[reg
->index
];
408 get_src_uint_reg(struct ntv_context
*ctx
, const nir_reg_src
*reg
)
411 assert(!reg
->indirect
);
412 assert(!reg
->base_offset
);
414 SpvId var
= get_var_from_reg(ctx
, reg
->reg
);
415 SpvId type
= get_uvec_type(ctx
, reg
->reg
->bit_size
, reg
->reg
->num_components
);
416 return spirv_builder_emit_load(&ctx
->builder
, type
, var
);
420 get_src_uint(struct ntv_context
*ctx
, nir_src
*src
)
423 return get_src_uint_ssa(ctx
, src
->ssa
);
425 return get_src_uint_reg(ctx
, &src
->reg
);
429 get_alu_src_uint(struct ntv_context
*ctx
, nir_alu_instr
*alu
, unsigned src
)
431 assert(!alu
->src
[src
].negate
);
432 assert(!alu
->src
[src
].abs
);
434 SpvId def
= get_src_uint(ctx
, &alu
->src
[src
].src
);
436 unsigned used_channels
= 0;
437 bool need_swizzle
= false;
438 for (unsigned i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; i
++) {
439 if (!nir_alu_instr_channel_used(alu
, src
, i
))
444 if (alu
->src
[src
].swizzle
[i
] != i
)
447 assert(used_channels
!= 0);
449 unsigned live_channels
= nir_src_num_components(alu
->src
[src
].src
);
450 if (used_channels
!= live_channels
)
456 int bit_size
= nir_src_bit_size(alu
->src
[src
].src
);
458 SpvId uint_type
= spirv_builder_type_uint(&ctx
->builder
, bit_size
);
459 if (used_channels
== 1) {
460 uint32_t indices
[] = { alu
->src
[src
].swizzle
[0] };
461 return spirv_builder_emit_composite_extract(&ctx
->builder
, uint_type
,
463 ARRAY_SIZE(indices
));
464 } else if (live_channels
== 1) {
465 SpvId uvec_type
= spirv_builder_type_vector(&ctx
->builder
, uint_type
,
468 SpvId constituents
[NIR_MAX_VEC_COMPONENTS
];
469 for (unsigned i
= 0; i
< used_channels
; ++i
)
470 constituents
[i
] = def
;
472 return spirv_builder_emit_composite_construct(&ctx
->builder
, uvec_type
,
476 SpvId uvec_type
= spirv_builder_type_vector(&ctx
->builder
, uint_type
,
479 uint32_t components
[NIR_MAX_VEC_COMPONENTS
];
480 size_t num_components
= 0;
481 for (unsigned i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; i
++) {
482 if (!nir_alu_instr_channel_used(alu
, src
, i
))
485 components
[num_components
++] = alu
->src
[src
].swizzle
[i
];
488 return spirv_builder_emit_vector_shuffle(&ctx
->builder
, uvec_type
,
489 def
, def
, components
, num_components
);
494 store_ssa_def_uint(struct ntv_context
*ctx
, nir_ssa_def
*ssa
, SpvId result
)
497 assert(ssa
->index
< ctx
->num_defs
);
498 ctx
->defs
[ssa
->index
] = result
;
502 bvec_to_uvec(struct ntv_context
*ctx
, SpvId value
, unsigned num_components
)
504 SpvId otype
= get_uvec_type(ctx
, 32, num_components
);
505 uint32_t zeros
[4] = { 0, 0, 0, 0 };
506 uint32_t ones
[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff };
507 SpvId zero
= get_uvec_constant(ctx
, 32, num_components
, zeros
);
508 SpvId one
= get_uvec_constant(ctx
, 32, num_components
, ones
);
509 return emit_triop(ctx
, SpvOpSelect
, otype
, value
, one
, zero
);
513 uvec_to_bvec(struct ntv_context
*ctx
, SpvId value
, unsigned num_components
)
515 SpvId type
= get_bvec_type(ctx
, num_components
);
517 uint32_t zeros
[NIR_MAX_VEC_COMPONENTS
] = { 0 };
518 SpvId zero
= get_uvec_constant(ctx
, 32, num_components
, zeros
);
520 return emit_binop(ctx
, SpvOpINotEqual
, type
, value
, zero
);
524 bitcast_to_uvec(struct ntv_context
*ctx
, SpvId value
, unsigned bit_size
,
525 unsigned num_components
)
527 SpvId type
= get_uvec_type(ctx
, bit_size
, num_components
);
528 return emit_unop(ctx
, SpvOpBitcast
, type
, value
);
532 bitcast_to_ivec(struct ntv_context
*ctx
, SpvId value
, unsigned bit_size
,
533 unsigned num_components
)
535 SpvId type
= get_ivec_type(ctx
, bit_size
, num_components
);
536 return emit_unop(ctx
, SpvOpBitcast
, type
, value
);
540 bitcast_to_fvec(struct ntv_context
*ctx
, SpvId value
, unsigned bit_size
,
541 unsigned num_components
)
543 SpvId type
= get_fvec_type(ctx
, bit_size
, num_components
);
544 return emit_unop(ctx
, SpvOpBitcast
, type
, value
);
548 store_reg_def(struct ntv_context
*ctx
, nir_reg_dest
*reg
, SpvId result
)
550 SpvId var
= get_var_from_reg(ctx
, reg
->reg
);
552 spirv_builder_emit_store(&ctx
->builder
, var
, result
);
556 store_dest_uint(struct ntv_context
*ctx
, nir_dest
*dest
, SpvId result
)
559 store_ssa_def_uint(ctx
, &dest
->ssa
, result
);
561 store_reg_def(ctx
, &dest
->reg
, result
);
565 store_dest(struct ntv_context
*ctx
, nir_dest
*dest
, SpvId result
, nir_alu_type type
)
567 unsigned num_components
= nir_dest_num_components(*dest
);
568 unsigned bit_size
= nir_dest_bit_size(*dest
);
570 switch (nir_alu_type_get_base_type(type
)) {
572 assert(bit_size
== 1);
573 result
= bvec_to_uvec(ctx
, result
, num_components
);
577 break; /* nothing to do! */
581 result
= bitcast_to_uvec(ctx
, result
, bit_size
, num_components
);
585 unreachable("unsupported nir_alu_type");
588 store_dest_uint(ctx
, dest
, result
);
592 emit_unop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
, SpvId src
)
594 return spirv_builder_emit_unop(&ctx
->builder
, op
, type
, src
);
598 emit_binop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
,
599 SpvId src0
, SpvId src1
)
601 return spirv_builder_emit_binop(&ctx
->builder
, op
, type
, src0
, src1
);
605 emit_triop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
,
606 SpvId src0
, SpvId src1
, SpvId src2
)
608 return spirv_builder_emit_triop(&ctx
->builder
, op
, type
, src0
, src1
, src2
);
612 emit_builtin_unop(struct ntv_context
*ctx
, enum GLSLstd450 op
, SpvId type
,
615 SpvId args
[] = { src
};
616 return spirv_builder_emit_ext_inst(&ctx
->builder
, type
, ctx
->GLSL_std_450
,
617 op
, args
, ARRAY_SIZE(args
));
621 emit_builtin_binop(struct ntv_context
*ctx
, enum GLSLstd450 op
, SpvId type
,
622 SpvId src0
, SpvId src1
)
624 SpvId args
[] = { src0
, src1
};
625 return spirv_builder_emit_ext_inst(&ctx
->builder
, type
, ctx
->GLSL_std_450
,
626 op
, args
, ARRAY_SIZE(args
));
630 get_fvec_constant(struct ntv_context
*ctx
, int bit_size
, int num_components
,
631 const float values
[])
633 assert(bit_size
== 32);
635 if (num_components
> 1) {
636 SpvId components
[num_components
];
637 for (int i
= 0; i
< num_components
; i
++)
638 components
[i
] = spirv_builder_const_float(&ctx
->builder
, bit_size
,
641 SpvId type
= get_fvec_type(ctx
, bit_size
, num_components
);
642 return spirv_builder_const_composite(&ctx
->builder
, type
, components
,
646 assert(num_components
== 1);
647 return spirv_builder_const_float(&ctx
->builder
, bit_size
, values
[0]);
651 get_uvec_constant(struct ntv_context
*ctx
, int bit_size
, int num_components
,
652 const uint32_t values
[])
654 assert(bit_size
== 32);
656 if (num_components
> 1) {
657 SpvId components
[num_components
];
658 for (int i
= 0; i
< num_components
; i
++)
659 components
[i
] = spirv_builder_const_uint(&ctx
->builder
, bit_size
,
662 SpvId type
= get_uvec_type(ctx
, bit_size
, num_components
);
663 return spirv_builder_const_composite(&ctx
->builder
, type
, components
,
667 assert(num_components
== 1);
668 return spirv_builder_const_uint(&ctx
->builder
, bit_size
, values
[0]);
671 static inline unsigned
672 alu_instr_src_components(const nir_alu_instr
*instr
, unsigned src
)
674 if (nir_op_infos
[instr
->op
].input_sizes
[src
] > 0)
675 return nir_op_infos
[instr
->op
].input_sizes
[src
];
677 if (instr
->dest
.dest
.is_ssa
)
678 return instr
->dest
.dest
.ssa
.num_components
;
680 return instr
->dest
.dest
.reg
.reg
->num_components
;
684 get_alu_src(struct ntv_context
*ctx
, nir_alu_instr
*alu
, unsigned src
)
686 SpvId uint_value
= get_alu_src_uint(ctx
, alu
, src
);
688 unsigned num_components
= alu_instr_src_components(alu
, src
);
689 unsigned bit_size
= nir_src_bit_size(alu
->src
[src
].src
);
690 nir_alu_type type
= nir_op_infos
[alu
->op
].input_types
[src
];
692 switch (nir_alu_type_get_base_type(type
)) {
694 assert(bit_size
== 1);
695 return uvec_to_bvec(ctx
, uint_value
, num_components
);
698 return bitcast_to_ivec(ctx
, uint_value
, bit_size
, num_components
);
704 return bitcast_to_fvec(ctx
, uint_value
, bit_size
, num_components
);
707 unreachable("unknown nir_alu_type");
712 store_alu_result(struct ntv_context
*ctx
, nir_alu_instr
*alu
, SpvId result
)
714 assert(!alu
->dest
.saturate
);
715 return store_dest(ctx
, &alu
->dest
.dest
, result
, nir_op_infos
[alu
->op
].output_type
);
719 get_dest_type(struct ntv_context
*ctx
, nir_dest
*dest
, nir_alu_type type
)
721 unsigned num_components
= nir_dest_num_components(*dest
);
722 unsigned bit_size
= nir_dest_bit_size(*dest
);
724 switch (nir_alu_type_get_base_type(type
)) {
726 return get_bvec_type(ctx
, num_components
);
729 return get_ivec_type(ctx
, bit_size
, num_components
);
732 return get_uvec_type(ctx
, bit_size
, num_components
);
735 return get_fvec_type(ctx
, bit_size
, num_components
);
738 unreachable("unsupported nir_alu_type");
743 emit_alu(struct ntv_context
*ctx
, nir_alu_instr
*alu
)
745 SpvId src
[nir_op_infos
[alu
->op
].num_inputs
];
746 for (unsigned i
= 0; i
< nir_op_infos
[alu
->op
].num_inputs
; i
++)
747 src
[i
] = get_alu_src(ctx
, alu
, i
);
749 SpvId dest_type
= get_dest_type(ctx
, &alu
->dest
.dest
,
750 nir_op_infos
[alu
->op
].output_type
);
751 unsigned bit_size
= nir_dest_bit_size(alu
->dest
.dest
);
752 unsigned num_components
= nir_dest_num_components(alu
->dest
.dest
);
757 assert(nir_op_infos
[alu
->op
].num_inputs
== 1);
761 #define UNOP(nir_op, spirv_op) \
763 assert(nir_op_infos[alu->op].num_inputs == 1); \
764 result = emit_unop(ctx, spirv_op, dest_type, src[0]); \
767 #define BUILTIN_UNOP(nir_op, spirv_op) \
769 assert(nir_op_infos[alu->op].num_inputs == 1); \
770 result = emit_builtin_unop(ctx, spirv_op, dest_type, src[0]); \
773 UNOP(nir_op_fneg
, SpvOpFNegate
)
774 UNOP(nir_op_fddx
, SpvOpDPdx
)
775 UNOP(nir_op_fddy
, SpvOpDPdy
)
777 BUILTIN_UNOP(nir_op_fabs
, GLSLstd450FAbs
)
778 BUILTIN_UNOP(nir_op_fsqrt
, GLSLstd450Sqrt
)
779 BUILTIN_UNOP(nir_op_frsq
, GLSLstd450InverseSqrt
)
780 BUILTIN_UNOP(nir_op_flog2
, GLSLstd450Log2
)
781 BUILTIN_UNOP(nir_op_fexp2
, GLSLstd450Exp2
)
782 BUILTIN_UNOP(nir_op_ffract
, GLSLstd450Fract
)
783 BUILTIN_UNOP(nir_op_ffloor
, GLSLstd450Floor
)
784 BUILTIN_UNOP(nir_op_fceil
, GLSLstd450Ceil
)
785 BUILTIN_UNOP(nir_op_ftrunc
, GLSLstd450Trunc
)
786 BUILTIN_UNOP(nir_op_fround_even
, GLSLstd450RoundEven
)
787 BUILTIN_UNOP(nir_op_fsign
, GLSLstd450FSign
)
788 BUILTIN_UNOP(nir_op_fsin
, GLSLstd450Sin
)
789 BUILTIN_UNOP(nir_op_fcos
, GLSLstd450Cos
)
792 assert(nir_op_infos
[alu
->op
].num_inputs
== 1);
793 float one
[4] = { 1, 1, 1, 1 };
795 src
[0] = get_fvec_constant(ctx
, bit_size
, num_components
, one
);
796 result
= emit_binop(ctx
, SpvOpFDiv
, dest_type
, src
[0], src
[1]);
803 #define BINOP(nir_op, spirv_op) \
805 assert(nir_op_infos[alu->op].num_inputs == 2); \
806 result = emit_binop(ctx, spirv_op, dest_type, src[0], src[1]); \
809 #define BUILTIN_BINOP(nir_op, spirv_op) \
811 assert(nir_op_infos[alu->op].num_inputs == 2); \
812 result = emit_builtin_binop(ctx, spirv_op, dest_type, src[0], src[1]); \
815 BINOP(nir_op_iadd
, SpvOpIAdd
)
816 BINOP(nir_op_isub
, SpvOpISub
)
817 BINOP(nir_op_imul
, SpvOpIMul
)
818 BINOP(nir_op_fadd
, SpvOpFAdd
)
819 BINOP(nir_op_fsub
, SpvOpFSub
)
820 BINOP(nir_op_fmul
, SpvOpFMul
)
821 BINOP(nir_op_fmod
, SpvOpFMod
)
822 BINOP(nir_op_flt
, SpvOpFUnordLessThan
)
823 BINOP(nir_op_fge
, SpvOpFUnordGreaterThanEqual
)
825 BUILTIN_BINOP(nir_op_fmin
, GLSLstd450FMin
)
826 BUILTIN_BINOP(nir_op_fmax
, GLSLstd450FMax
)
834 assert(nir_op_infos
[alu
->op
].num_inputs
== 2);
835 result
= emit_binop(ctx
, SpvOpDot
, dest_type
, src
[0], src
[1]);
842 assert(nir_op_infos
[alu
->op
].num_inputs
== 2);
843 int num_components
= nir_dest_num_components(alu
->dest
.dest
);
844 SpvId bool_type
= get_bvec_type(ctx
, num_components
);
846 SpvId zero
= spirv_builder_const_float(&ctx
->builder
, 32, 0.0f
);
847 SpvId one
= spirv_builder_const_float(&ctx
->builder
, 32, 1.0f
);
848 if (num_components
> 1) {
849 SpvId zero_comps
[num_components
], one_comps
[num_components
];
850 for (int i
= 0; i
< num_components
; i
++) {
851 zero_comps
[i
] = zero
;
855 zero
= spirv_builder_const_composite(&ctx
->builder
, dest_type
,
856 zero_comps
, num_components
);
857 one
= spirv_builder_const_composite(&ctx
->builder
, dest_type
,
858 one_comps
, num_components
);
863 case nir_op_seq
: op
= SpvOpFOrdEqual
; break;
864 case nir_op_sne
: op
= SpvOpFOrdNotEqual
; break;
865 case nir_op_slt
: op
= SpvOpFOrdLessThan
; break;
866 case nir_op_sge
: op
= SpvOpFOrdGreaterThanEqual
; break;
867 default: unreachable("unexpected op");
870 result
= emit_binop(ctx
, op
, bool_type
, src
[0], src
[1]);
871 result
= emit_triop(ctx
, SpvOpSelect
, dest_type
, result
, one
, zero
);
876 assert(nir_op_infos
[alu
->op
].num_inputs
== 3);
877 int num_components
= nir_dest_num_components(alu
->dest
.dest
);
878 SpvId bool_type
= get_bvec_type(ctx
, num_components
);
880 float zero
[4] = { 0, 0, 0, 0 };
881 SpvId cmp
= get_fvec_constant(ctx
, nir_src_bit_size(alu
->src
[0].src
),
882 num_components
, zero
);
884 result
= emit_binop(ctx
, SpvOpFOrdGreaterThan
, bool_type
, src
[0], cmp
);
885 result
= emit_triop(ctx
, SpvOpSelect
, dest_type
, result
, src
[1], src
[2]);
892 int num_inputs
= nir_op_infos
[alu
->op
].num_inputs
;
893 assert(2 <= num_inputs
&& num_inputs
<= 4);
894 result
= spirv_builder_emit_composite_construct(&ctx
->builder
, dest_type
,
900 fprintf(stderr
, "emit_alu: not implemented (%s)\n",
901 nir_op_infos
[alu
->op
].name
);
903 unreachable("unsupported opcode");
907 store_alu_result(ctx
, alu
, result
);
911 emit_load_const(struct ntv_context
*ctx
, nir_load_const_instr
*load_const
)
913 uint32_t values
[NIR_MAX_VEC_COMPONENTS
];
914 for (int i
= 0; i
< load_const
->def
.num_components
; ++i
)
915 values
[i
] = load_const
->value
[i
].u32
;
917 SpvId constant
= get_uvec_constant(ctx
, load_const
->def
.bit_size
,
918 load_const
->def
.num_components
,
920 store_ssa_def_uint(ctx
, &load_const
->def
, constant
);
924 emit_load_ubo(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
926 nir_const_value
*const_block_index
= nir_src_as_const_value(intr
->src
[0]);
927 assert(const_block_index
); // no dynamic indexing for now
928 assert(const_block_index
->u32
== 0); // we only support the default UBO for now
930 nir_const_value
*const_offset
= nir_src_as_const_value(intr
->src
[1]);
932 SpvId uvec4_type
= get_uvec_type(ctx
, 32, 4);
933 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
934 SpvStorageClassUniform
,
937 unsigned idx
= const_offset
->u32
;
938 SpvId member
= spirv_builder_const_uint(&ctx
->builder
, 32, 0);
939 SpvId offset
= spirv_builder_const_uint(&ctx
->builder
, 32, idx
);
940 SpvId offsets
[] = { member
, offset
};
941 SpvId ptr
= spirv_builder_emit_access_chain(&ctx
->builder
, pointer_type
,
942 ctx
->ubos
[0], offsets
,
943 ARRAY_SIZE(offsets
));
944 SpvId result
= spirv_builder_emit_load(&ctx
->builder
, uvec4_type
, ptr
);
946 SpvId type
= get_dest_uvec_type(ctx
, &intr
->dest
);
947 unsigned num_components
= nir_dest_num_components(intr
->dest
);
948 if (num_components
== 1) {
949 uint32_t components
[] = { 0 };
950 result
= spirv_builder_emit_composite_extract(&ctx
->builder
,
954 } else if (num_components
< 4) {
955 SpvId constituents
[num_components
];
956 SpvId uint_type
= spirv_builder_type_uint(&ctx
->builder
, 32);
957 for (uint32_t i
= 0; i
< num_components
; ++i
)
958 constituents
[i
] = spirv_builder_emit_composite_extract(&ctx
->builder
,
963 result
= spirv_builder_emit_composite_construct(&ctx
->builder
,
969 store_dest_uint(ctx
, &intr
->dest
, result
);
971 unreachable("uniform-addressing not yet supported");
975 emit_discard(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
977 assert(ctx
->block_started
);
978 spirv_builder_emit_kill(&ctx
->builder
);
979 /* discard is weird in NIR, so let's just create an unreachable block after
980 it and hope that the vulkan driver will DCE any instructinos in it. */
981 spirv_builder_label(&ctx
->builder
, spirv_builder_new_id(&ctx
->builder
));
985 emit_load_deref(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
987 nir_variable
*var
= nir_intrinsic_get_var(intr
, 0);
988 struct hash_entry
*he
= _mesa_hash_table_search(ctx
->vars
, var
);
990 SpvId ptr
= (SpvId
)(intptr_t)he
->data
;
992 // SpvId ptr = get_src_uint(ctx, intr->src); /* uint is a bit of a lie here; it's really just a pointer */
993 SpvId result
= spirv_builder_emit_load(&ctx
->builder
,
994 get_glsl_type(ctx
, var
->type
),
996 unsigned num_components
= nir_dest_num_components(intr
->dest
);
997 unsigned bit_size
= nir_dest_bit_size(intr
->dest
);
998 result
= bitcast_to_uvec(ctx
, result
, bit_size
, num_components
);
999 store_dest_uint(ctx
, &intr
->dest
, result
);
1003 emit_store_deref(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
1005 nir_variable
*var
= nir_intrinsic_get_var(intr
, 0);
1006 struct hash_entry
*he
= _mesa_hash_table_search(ctx
->vars
, var
);
1008 SpvId ptr
= (SpvId
)(intptr_t)he
->data
;
1009 // SpvId ptr = get_src_uint(ctx, &intr->src[0]); /* uint is a bit of a lie here; it's really just a pointer */
1011 SpvId src
= get_src_uint(ctx
, &intr
->src
[1]);
1012 SpvId result
= emit_unop(ctx
, SpvOpBitcast
, get_glsl_type(ctx
, var
->type
),
1014 spirv_builder_emit_store(&ctx
->builder
, ptr
, result
);
1018 emit_intrinsic(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
1020 switch (intr
->intrinsic
) {
1021 case nir_intrinsic_load_ubo
:
1022 emit_load_ubo(ctx
, intr
);
1025 case nir_intrinsic_discard
:
1026 emit_discard(ctx
, intr
);
1029 case nir_intrinsic_load_deref
:
1030 emit_load_deref(ctx
, intr
);
1033 case nir_intrinsic_store_deref
:
1034 emit_store_deref(ctx
, intr
);
1038 fprintf(stderr
, "emit_intrinsic: not implemented (%s)\n",
1039 nir_intrinsic_infos
[intr
->intrinsic
].name
);
1040 unreachable("unsupported intrinsic");
1045 emit_undef(struct ntv_context
*ctx
, nir_ssa_undef_instr
*undef
)
1047 SpvId type
= get_uvec_type(ctx
, undef
->def
.bit_size
,
1048 undef
->def
.num_components
);
1050 store_ssa_def_uint(ctx
, &undef
->def
,
1051 spirv_builder_emit_undef(&ctx
->builder
, type
));
1055 get_src_float(struct ntv_context
*ctx
, nir_src
*src
)
1057 SpvId def
= get_src_uint(ctx
, src
);
1058 unsigned num_components
= nir_src_num_components(*src
);
1059 unsigned bit_size
= nir_src_bit_size(*src
);
1060 return bitcast_to_fvec(ctx
, def
, bit_size
, num_components
);
1064 emit_tex(struct ntv_context
*ctx
, nir_tex_instr
*tex
)
1066 assert(tex
->op
== nir_texop_tex
);
1067 assert(nir_alu_type_get_base_type(tex
->dest_type
) == nir_type_float
);
1068 assert(tex
->texture_index
== tex
->sampler_index
);
1070 bool has_proj
= false, has_lod
= false;
1071 SpvId coord
= 0, proj
, lod
;
1072 unsigned coord_components
;
1073 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
1074 switch (tex
->src
[i
].src_type
) {
1075 case nir_tex_src_coord
:
1076 coord
= get_src_float(ctx
, &tex
->src
[i
].src
);
1077 coord_components
= nir_src_num_components(tex
->src
[i
].src
);
1080 case nir_tex_src_projector
:
1082 proj
= get_src_float(ctx
, &tex
->src
[i
].src
);
1083 assert(nir_src_num_components(tex
->src
[i
].src
) == 1);
1086 case nir_tex_src_lod
:
1088 lod
= get_src_float(ctx
, &tex
->src
[i
].src
);
1089 assert(nir_src_num_components(tex
->src
[i
].src
) == 1);
1093 fprintf(stderr
, "texture source: %d\n", tex
->src
[i
].src_type
);
1094 unreachable("unknown texture source");
1098 if (!has_lod
&& ctx
->stage
!= MESA_SHADER_FRAGMENT
) {
1100 lod
= spirv_builder_const_float(&ctx
->builder
, 32, 0);
1104 SpvDim dimension
= type_to_dim(tex
->sampler_dim
, &is_ms
);
1105 SpvId float_type
= spirv_builder_type_float(&ctx
->builder
, 32);
1106 SpvId image_type
= spirv_builder_type_image(&ctx
->builder
, float_type
,
1107 dimension
, false, tex
->is_array
, is_ms
, 1,
1108 SpvImageFormatUnknown
);
1109 SpvId sampled_type
= spirv_builder_type_sampled_image(&ctx
->builder
,
1112 assert(tex
->texture_index
< ctx
->num_samplers
);
1113 SpvId load
= spirv_builder_emit_load(&ctx
->builder
, sampled_type
,
1114 ctx
->samplers
[tex
->texture_index
]);
1116 SpvId dest_type
= get_dest_type(ctx
, &tex
->dest
, tex
->dest_type
);
1120 SpvId constituents
[coord_components
+ 1];
1121 SpvId float_type
= spirv_builder_type_float(&ctx
->builder
, 32);
1122 for (uint32_t i
= 0; i
< coord_components
; ++i
)
1123 constituents
[i
] = spirv_builder_emit_composite_extract(&ctx
->builder
,
1128 constituents
[coord_components
++] = proj
;
1130 SpvId vec_type
= get_fvec_type(ctx
, 32, coord_components
);
1131 SpvId merged
= spirv_builder_emit_composite_construct(&ctx
->builder
,
1137 result
= spirv_builder_emit_image_sample_proj_explicit_lod(&ctx
->builder
,
1143 result
= spirv_builder_emit_image_sample_proj_implicit_lod(&ctx
->builder
,
1149 result
= spirv_builder_emit_image_sample_explicit_lod(&ctx
->builder
,
1154 result
= spirv_builder_emit_image_sample_implicit_lod(&ctx
->builder
,
1159 spirv_builder_emit_decoration(&ctx
->builder
, result
,
1160 SpvDecorationRelaxedPrecision
);
1162 store_dest(ctx
, &tex
->dest
, result
, tex
->dest_type
);
1166 start_block(struct ntv_context
*ctx
, SpvId label
)
1168 /* terminate previous block if needed */
1169 if (ctx
->block_started
)
1170 spirv_builder_emit_branch(&ctx
->builder
, label
);
1172 /* start new block */
1173 spirv_builder_label(&ctx
->builder
, label
);
1174 ctx
->block_started
= true;
1178 branch(struct ntv_context
*ctx
, SpvId label
)
1180 assert(ctx
->block_started
);
1181 spirv_builder_emit_branch(&ctx
->builder
, label
);
1182 ctx
->block_started
= false;
1186 branch_conditional(struct ntv_context
*ctx
, SpvId condition
, SpvId then_id
,
1189 assert(ctx
->block_started
);
1190 spirv_builder_emit_branch_conditional(&ctx
->builder
, condition
,
1192 ctx
->block_started
= false;
1196 emit_jump(struct ntv_context
*ctx
, nir_jump_instr
*jump
)
1198 switch (jump
->type
) {
1199 case nir_jump_break
:
1200 assert(ctx
->loop_break
);
1201 branch(ctx
, ctx
->loop_break
);
1204 case nir_jump_continue
:
1205 assert(ctx
->loop_cont
);
1206 branch(ctx
, ctx
->loop_cont
);
1210 unreachable("Unsupported jump type\n");
1215 emit_deref(struct ntv_context
*ctx
, nir_deref_instr
*deref
)
1217 assert(deref
->deref_type
== nir_deref_type_var
);
1219 SpvStorageClass storage_class
;
1220 switch (deref
->var
->data
.mode
) {
1221 case nir_var_shader_in
:
1222 storage_class
= SpvStorageClassInput
;
1225 case nir_var_shader_out
:
1226 storage_class
= SpvStorageClassOutput
;
1230 unreachable("Unsupported nir_variable_mode\n");
1233 struct hash_entry
*he
= _mesa_hash_table_search(ctx
->vars
, deref
->var
);
1236 SpvId ptr_type
= spirv_builder_type_pointer(&ctx
->builder
,
1238 get_glsl_type(ctx
, deref
->type
));
1240 SpvId result
= spirv_builder_emit_access_chain(&ctx
->builder
,
1242 (SpvId
)(intptr_t)he
->data
,
1244 /* uint is a bit of a lie here, it's really just an opaque type */
1245 store_dest_uint(ctx
, &deref
->dest
, result
);
1249 emit_block(struct ntv_context
*ctx
, struct nir_block
*block
)
1251 start_block(ctx
, block_label(ctx
, block
));
1252 nir_foreach_instr(instr
, block
) {
1253 switch (instr
->type
) {
1254 case nir_instr_type_alu
:
1255 emit_alu(ctx
, nir_instr_as_alu(instr
));
1257 case nir_instr_type_intrinsic
:
1258 emit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
1260 case nir_instr_type_load_const
:
1261 emit_load_const(ctx
, nir_instr_as_load_const(instr
));
1263 case nir_instr_type_ssa_undef
:
1264 emit_undef(ctx
, nir_instr_as_ssa_undef(instr
));
1266 case nir_instr_type_tex
:
1267 emit_tex(ctx
, nir_instr_as_tex(instr
));
1269 case nir_instr_type_phi
:
1270 unreachable("nir_instr_type_phi not supported");
1272 case nir_instr_type_jump
:
1273 emit_jump(ctx
, nir_instr_as_jump(instr
));
1275 case nir_instr_type_call
:
1276 unreachable("nir_instr_type_call not supported");
1278 case nir_instr_type_parallel_copy
:
1279 unreachable("nir_instr_type_parallel_copy not supported");
1281 case nir_instr_type_deref
:
1282 /* these are handled in emit_{load,store}_deref */
1283 /* emit_deref(ctx, nir_instr_as_deref(instr)); */
1290 emit_cf_list(struct ntv_context
*ctx
, struct exec_list
*list
);
1293 get_src_bool(struct ntv_context
*ctx
, nir_src
*src
)
1295 SpvId def
= get_src_uint(ctx
, src
);
1296 assert(nir_src_bit_size(*src
) == 32);
1297 unsigned num_components
= nir_src_num_components(*src
);
1298 return uvec_to_bvec(ctx
, def
, num_components
);
1302 emit_if(struct ntv_context
*ctx
, nir_if
*if_stmt
)
1304 SpvId condition
= get_src_bool(ctx
, &if_stmt
->condition
);
1306 SpvId header_id
= spirv_builder_new_id(&ctx
->builder
);
1307 SpvId then_id
= block_label(ctx
, nir_if_first_then_block(if_stmt
));
1308 SpvId endif_id
= spirv_builder_new_id(&ctx
->builder
);
1309 SpvId else_id
= endif_id
;
1311 bool has_else
= !exec_list_is_empty(&if_stmt
->else_list
);
1313 assert(nir_if_first_else_block(if_stmt
)->index
< ctx
->num_blocks
);
1314 else_id
= block_label(ctx
, nir_if_first_else_block(if_stmt
));
1317 /* create a header-block */
1318 start_block(ctx
, header_id
);
1319 spirv_builder_emit_selection_merge(&ctx
->builder
, endif_id
,
1320 SpvSelectionControlMaskNone
);
1321 branch_conditional(ctx
, condition
, then_id
, else_id
);
1323 emit_cf_list(ctx
, &if_stmt
->then_list
);
1326 if (ctx
->block_started
)
1327 branch(ctx
, endif_id
);
1329 emit_cf_list(ctx
, &if_stmt
->else_list
);
1332 start_block(ctx
, endif_id
);
1336 emit_loop(struct ntv_context
*ctx
, nir_loop
*loop
)
1338 SpvId header_id
= spirv_builder_new_id(&ctx
->builder
);
1339 SpvId begin_id
= block_label(ctx
, nir_loop_first_block(loop
));
1340 SpvId break_id
= spirv_builder_new_id(&ctx
->builder
);
1341 SpvId cont_id
= spirv_builder_new_id(&ctx
->builder
);
1343 /* create a header-block */
1344 start_block(ctx
, header_id
);
1345 spirv_builder_loop_merge(&ctx
->builder
, break_id
, cont_id
, SpvLoopControlMaskNone
);
1346 branch(ctx
, begin_id
);
1348 SpvId save_break
= ctx
->loop_break
;
1349 SpvId save_cont
= ctx
->loop_cont
;
1350 ctx
->loop_break
= break_id
;
1351 ctx
->loop_cont
= cont_id
;
1353 emit_cf_list(ctx
, &loop
->body
);
1355 ctx
->loop_break
= save_break
;
1356 ctx
->loop_cont
= save_cont
;
1358 branch(ctx
, cont_id
);
1359 start_block(ctx
, cont_id
);
1360 branch(ctx
, header_id
);
1362 start_block(ctx
, break_id
);
1366 emit_cf_list(struct ntv_context
*ctx
, struct exec_list
*list
)
1368 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
1369 switch (node
->type
) {
1370 case nir_cf_node_block
:
1371 emit_block(ctx
, nir_cf_node_as_block(node
));
1374 case nir_cf_node_if
:
1375 emit_if(ctx
, nir_cf_node_as_if(node
));
1378 case nir_cf_node_loop
:
1379 emit_loop(ctx
, nir_cf_node_as_loop(node
));
1382 case nir_cf_node_function
:
1383 unreachable("nir_cf_node_function not supported");
1389 struct spirv_shader
*
1390 nir_to_spirv(struct nir_shader
*s
)
1392 struct spirv_shader
*ret
= NULL
;
1394 struct ntv_context ctx
= {};
1396 switch (s
->info
.stage
) {
1397 case MESA_SHADER_VERTEX
:
1398 case MESA_SHADER_FRAGMENT
:
1399 case MESA_SHADER_COMPUTE
:
1400 spirv_builder_emit_cap(&ctx
.builder
, SpvCapabilityShader
);
1403 case MESA_SHADER_TESS_CTRL
:
1404 case MESA_SHADER_TESS_EVAL
:
1405 spirv_builder_emit_cap(&ctx
.builder
, SpvCapabilityTessellation
);
1408 case MESA_SHADER_GEOMETRY
:
1409 spirv_builder_emit_cap(&ctx
.builder
, SpvCapabilityGeometry
);
1413 unreachable("invalid stage");
1416 ctx
.stage
= s
->info
.stage
;
1417 ctx
.GLSL_std_450
= spirv_builder_import(&ctx
.builder
, "GLSL.std.450");
1418 spirv_builder_emit_source(&ctx
.builder
, SpvSourceLanguageGLSL
, 450);
1420 spirv_builder_emit_mem_model(&ctx
.builder
, SpvAddressingModelLogical
,
1421 SpvMemoryModelGLSL450
);
1423 SpvExecutionModel exec_model
;
1424 switch (s
->info
.stage
) {
1425 case MESA_SHADER_VERTEX
:
1426 exec_model
= SpvExecutionModelVertex
;
1428 case MESA_SHADER_TESS_CTRL
:
1429 exec_model
= SpvExecutionModelTessellationControl
;
1431 case MESA_SHADER_TESS_EVAL
:
1432 exec_model
= SpvExecutionModelTessellationEvaluation
;
1434 case MESA_SHADER_GEOMETRY
:
1435 exec_model
= SpvExecutionModelGeometry
;
1437 case MESA_SHADER_FRAGMENT
:
1438 exec_model
= SpvExecutionModelFragment
;
1440 case MESA_SHADER_COMPUTE
:
1441 exec_model
= SpvExecutionModelGLCompute
;
1444 unreachable("invalid stage");
1447 SpvId type_void
= spirv_builder_type_void(&ctx
.builder
);
1448 SpvId type_main
= spirv_builder_type_function(&ctx
.builder
, type_void
,
1450 SpvId entry_point
= spirv_builder_new_id(&ctx
.builder
);
1451 spirv_builder_emit_name(&ctx
.builder
, entry_point
, "main");
1453 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
1454 _mesa_key_pointer_equal
);
1456 nir_foreach_variable(var
, &s
->inputs
)
1457 emit_input(&ctx
, var
);
1459 nir_foreach_variable(var
, &s
->outputs
)
1460 emit_output(&ctx
, var
);
1462 nir_foreach_variable(var
, &s
->uniforms
)
1463 emit_uniform(&ctx
, var
);
1465 spirv_builder_emit_entry_point(&ctx
.builder
, exec_model
, entry_point
,
1466 "main", ctx
.entry_ifaces
,
1467 ctx
.num_entry_ifaces
);
1468 if (s
->info
.stage
== MESA_SHADER_FRAGMENT
)
1469 spirv_builder_emit_exec_mode(&ctx
.builder
, entry_point
,
1470 SpvExecutionModeOriginUpperLeft
);
1473 spirv_builder_function(&ctx
.builder
, entry_point
, type_void
,
1474 SpvFunctionControlMaskNone
,
1477 nir_function_impl
*entry
= nir_shader_get_entrypoint(s
);
1478 nir_metadata_require(entry
, nir_metadata_block_index
);
1480 ctx
.defs
= (SpvId
*)malloc(sizeof(SpvId
) * entry
->ssa_alloc
);
1483 ctx
.num_defs
= entry
->ssa_alloc
;
1485 nir_index_local_regs(entry
);
1486 ctx
.regs
= malloc(sizeof(SpvId
) * entry
->reg_alloc
);
1489 ctx
.num_regs
= entry
->reg_alloc
;
1491 SpvId
*block_ids
= (SpvId
*)malloc(sizeof(SpvId
) * entry
->num_blocks
);
1495 for (int i
= 0; i
< entry
->num_blocks
; ++i
)
1496 block_ids
[i
] = spirv_builder_new_id(&ctx
.builder
);
1498 ctx
.block_ids
= block_ids
;
1499 ctx
.num_blocks
= entry
->num_blocks
;
1501 /* emit a block only for the variable declarations */
1502 start_block(&ctx
, spirv_builder_new_id(&ctx
.builder
));
1503 foreach_list_typed(nir_register
, reg
, node
, &entry
->registers
) {
1504 SpvId type
= get_uvec_type(&ctx
, reg
->bit_size
, reg
->num_components
);
1505 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
.builder
,
1506 SpvStorageClassFunction
,
1508 SpvId var
= spirv_builder_emit_var(&ctx
.builder
, pointer_type
,
1509 SpvStorageClassFunction
);
1511 ctx
.regs
[reg
->index
] = var
;
1514 emit_cf_list(&ctx
, &entry
->body
);
1518 spirv_builder_return(&ctx
.builder
); // doesn't belong here, but whatevz
1519 spirv_builder_function_end(&ctx
.builder
);
1521 size_t num_words
= spirv_builder_get_num_words(&ctx
.builder
);
1523 ret
= CALLOC_STRUCT(spirv_shader
);
1527 ret
->words
= MALLOC(sizeof(uint32_t) * num_words
);
1531 ret
->num_words
= spirv_builder_get_words(&ctx
.builder
, ret
->words
, num_words
);
1532 assert(ret
->num_words
== num_words
);
1539 spirv_shader_delete(ret
);
1542 _mesa_hash_table_destroy(ctx
.vars
, NULL
);
1548 spirv_shader_delete(struct spirv_shader
*s
)