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 emit_float_const(struct ntv_context
*ctx
, int bit_size
, float value
)
102 assert(bit_size
== 32);
103 return spirv_builder_const_float(&ctx
->builder
, bit_size
, value
);
107 emit_uint_const(struct ntv_context
*ctx
, int bit_size
, uint32_t value
)
109 assert(bit_size
== 32);
110 return spirv_builder_const_uint(&ctx
->builder
, bit_size
, value
);
114 get_fvec_type(struct ntv_context
*ctx
, unsigned bit_size
, unsigned num_components
)
116 assert(bit_size
== 32); // only 32-bit floats supported so far
118 SpvId float_type
= spirv_builder_type_float(&ctx
->builder
, bit_size
);
119 if (num_components
> 1)
120 return spirv_builder_type_vector(&ctx
->builder
, float_type
,
123 assert(num_components
== 1);
128 get_ivec_type(struct ntv_context
*ctx
, unsigned bit_size
, unsigned num_components
)
130 assert(bit_size
== 32); // only 32-bit ints supported so far
132 SpvId int_type
= spirv_builder_type_int(&ctx
->builder
, bit_size
);
133 if (num_components
> 1)
134 return spirv_builder_type_vector(&ctx
->builder
, int_type
,
137 assert(num_components
== 1);
142 get_uvec_type(struct ntv_context
*ctx
, unsigned bit_size
, unsigned num_components
)
144 assert(bit_size
== 32); // only 32-bit uints supported so far
146 SpvId uint_type
= spirv_builder_type_uint(&ctx
->builder
, bit_size
);
147 if (num_components
> 1)
148 return spirv_builder_type_vector(&ctx
->builder
, uint_type
,
151 assert(num_components
== 1);
156 get_dest_uvec_type(struct ntv_context
*ctx
, nir_dest
*dest
)
158 return get_uvec_type(ctx
, nir_dest_bit_size(*dest
),
159 nir_dest_num_components(*dest
));
163 get_glsl_basetype(struct ntv_context
*ctx
, enum glsl_base_type type
)
166 case GLSL_TYPE_FLOAT
:
167 return spirv_builder_type_float(&ctx
->builder
, 32);
170 return spirv_builder_type_int(&ctx
->builder
, 32);
173 return spirv_builder_type_uint(&ctx
->builder
, 32);
174 /* TODO: handle more types */
177 unreachable("unknown GLSL type");
182 get_glsl_type(struct ntv_context
*ctx
, const struct glsl_type
*type
)
185 if (glsl_type_is_scalar(type
))
186 return get_glsl_basetype(ctx
, glsl_get_base_type(type
));
188 if (glsl_type_is_vector(type
))
189 return spirv_builder_type_vector(&ctx
->builder
,
190 get_glsl_basetype(ctx
, glsl_get_base_type(type
)),
191 glsl_get_vector_elements(type
));
193 if (glsl_type_is_array(type
)) {
194 SpvId ret
= spirv_builder_type_array(&ctx
->builder
,
195 get_glsl_type(ctx
, glsl_get_array_element(type
)),
196 emit_uint_const(ctx
, 32, glsl_get_length(type
)));
197 uint32_t stride
= glsl_get_explicit_stride(type
);
199 spirv_builder_emit_array_stride(&ctx
->builder
, ret
, stride
);
204 unreachable("we shouldn't get here, I think...");
208 emit_input(struct ntv_context
*ctx
, struct nir_variable
*var
)
210 SpvId var_type
= get_glsl_type(ctx
, var
->type
);
211 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
212 SpvStorageClassInput
,
214 SpvId var_id
= spirv_builder_emit_var(&ctx
->builder
, pointer_type
,
215 SpvStorageClassInput
);
218 spirv_builder_emit_name(&ctx
->builder
, var_id
, var
->name
);
220 if (ctx
->stage
== MESA_SHADER_FRAGMENT
) {
221 if (var
->data
.location
>= VARYING_SLOT_VAR0
||
222 (var
->data
.location
>= VARYING_SLOT_COL0
&&
223 var
->data
.location
<= VARYING_SLOT_TEX7
)) {
224 spirv_builder_emit_location(&ctx
->builder
, var_id
,
225 ctx
->var_location
++);
227 switch (var
->data
.location
) {
228 case VARYING_SLOT_POS
:
229 spirv_builder_emit_builtin(&ctx
->builder
, var_id
, SpvBuiltInFragCoord
);
232 case VARYING_SLOT_PNTC
:
233 spirv_builder_emit_builtin(&ctx
->builder
, var_id
, SpvBuiltInPointCoord
);
237 debug_printf("unknown varying slot: %s\n", gl_varying_slot_name(var
->data
.location
));
238 unreachable("unexpected varying slot");
242 spirv_builder_emit_location(&ctx
->builder
, var_id
,
243 var
->data
.driver_location
);
246 if (var
->data
.location_frac
)
247 spirv_builder_emit_component(&ctx
->builder
, var_id
,
248 var
->data
.location_frac
);
250 if (var
->data
.interpolation
== INTERP_MODE_FLAT
)
251 spirv_builder_emit_decoration(&ctx
->builder
, var_id
, SpvDecorationFlat
);
253 _mesa_hash_table_insert(ctx
->vars
, var
, (void *)(intptr_t)var_id
);
255 assert(ctx
->num_entry_ifaces
< ARRAY_SIZE(ctx
->entry_ifaces
));
256 ctx
->entry_ifaces
[ctx
->num_entry_ifaces
++] = var_id
;
260 emit_output(struct ntv_context
*ctx
, struct nir_variable
*var
)
262 SpvId var_type
= get_glsl_type(ctx
, var
->type
);
263 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
264 SpvStorageClassOutput
,
266 SpvId var_id
= spirv_builder_emit_var(&ctx
->builder
, pointer_type
,
267 SpvStorageClassOutput
);
269 spirv_builder_emit_name(&ctx
->builder
, var_id
, var
->name
);
272 if (ctx
->stage
== MESA_SHADER_VERTEX
) {
273 if (var
->data
.location
>= VARYING_SLOT_VAR0
||
274 (var
->data
.location
>= VARYING_SLOT_COL0
&&
275 var
->data
.location
<= VARYING_SLOT_TEX7
)) {
276 spirv_builder_emit_location(&ctx
->builder
, var_id
,
277 ctx
->var_location
++);
279 switch (var
->data
.location
) {
280 case VARYING_SLOT_POS
:
281 spirv_builder_emit_builtin(&ctx
->builder
, var_id
, SpvBuiltInPosition
);
284 case VARYING_SLOT_PSIZ
:
285 spirv_builder_emit_builtin(&ctx
->builder
, var_id
, SpvBuiltInPointSize
);
288 case VARYING_SLOT_CLIP_DIST0
:
289 assert(glsl_type_is_array(var
->type
));
290 spirv_builder_emit_builtin(&ctx
->builder
, var_id
, SpvBuiltInClipDistance
);
294 debug_printf("unknown varying slot: %s\n", gl_varying_slot_name(var
->data
.location
));
295 unreachable("unexpected varying slot");
298 } else if (ctx
->stage
== MESA_SHADER_FRAGMENT
) {
299 if (var
->data
.location
>= FRAG_RESULT_DATA0
)
300 spirv_builder_emit_location(&ctx
->builder
, var_id
,
301 var
->data
.location
- FRAG_RESULT_DATA0
);
303 switch (var
->data
.location
) {
304 case FRAG_RESULT_COLOR
:
305 spirv_builder_emit_location(&ctx
->builder
, var_id
, 0);
308 case FRAG_RESULT_DEPTH
:
309 spirv_builder_emit_builtin(&ctx
->builder
, var_id
, SpvBuiltInFragDepth
);
313 spirv_builder_emit_location(&ctx
->builder
, var_id
,
314 var
->data
.driver_location
);
319 if (var
->data
.location_frac
)
320 spirv_builder_emit_component(&ctx
->builder
, var_id
,
321 var
->data
.location_frac
);
323 _mesa_hash_table_insert(ctx
->vars
, var
, (void *)(intptr_t)var_id
);
325 assert(ctx
->num_entry_ifaces
< ARRAY_SIZE(ctx
->entry_ifaces
));
326 ctx
->entry_ifaces
[ctx
->num_entry_ifaces
++] = var_id
;
330 type_to_dim(enum glsl_sampler_dim gdim
, bool *is_ms
)
334 case GLSL_SAMPLER_DIM_1D
:
336 case GLSL_SAMPLER_DIM_2D
:
338 case GLSL_SAMPLER_DIM_RECT
:
340 case GLSL_SAMPLER_DIM_CUBE
:
342 case GLSL_SAMPLER_DIM_3D
:
344 case GLSL_SAMPLER_DIM_MS
:
348 fprintf(stderr
, "unknown sampler type %d\n", gdim
);
355 emit_sampler(struct ntv_context
*ctx
, struct nir_variable
*var
)
358 SpvDim dimension
= type_to_dim(glsl_get_sampler_dim(var
->type
), &is_ms
);
359 SpvId float_type
= spirv_builder_type_float(&ctx
->builder
, 32);
360 SpvId image_type
= spirv_builder_type_image(&ctx
->builder
, float_type
,
361 dimension
, false, glsl_sampler_type_is_array(var
->type
), is_ms
, 1,
362 SpvImageFormatUnknown
);
364 SpvId sampled_type
= spirv_builder_type_sampled_image(&ctx
->builder
,
366 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
367 SpvStorageClassUniformConstant
,
369 SpvId var_id
= spirv_builder_emit_var(&ctx
->builder
, pointer_type
,
370 SpvStorageClassUniformConstant
);
373 spirv_builder_emit_name(&ctx
->builder
, var_id
, var
->name
);
375 assert(ctx
->num_samplers
< ARRAY_SIZE(ctx
->samplers
));
376 ctx
->samplers
[ctx
->num_samplers
++] = var_id
;
378 spirv_builder_emit_descriptor_set(&ctx
->builder
, var_id
,
379 var
->data
.descriptor_set
);
380 spirv_builder_emit_binding(&ctx
->builder
, var_id
, var
->data
.binding
);
384 emit_ubo(struct ntv_context
*ctx
, struct nir_variable
*var
)
386 uint32_t size
= glsl_count_attribute_slots(var
->type
, false);
387 SpvId vec4_type
= get_uvec_type(ctx
, 32, 4);
388 SpvId array_length
= emit_uint_const(ctx
, 32, size
);
389 SpvId array_type
= spirv_builder_type_array(&ctx
->builder
, vec4_type
,
391 spirv_builder_emit_array_stride(&ctx
->builder
, array_type
, 16);
393 // wrap UBO-array in a struct
394 SpvId struct_type
= spirv_builder_type_struct(&ctx
->builder
, &array_type
, 1);
396 char struct_name
[100];
397 snprintf(struct_name
, sizeof(struct_name
), "struct_%s", var
->name
);
398 spirv_builder_emit_name(&ctx
->builder
, struct_type
, struct_name
);
401 spirv_builder_emit_decoration(&ctx
->builder
, struct_type
,
403 spirv_builder_emit_member_offset(&ctx
->builder
, struct_type
, 0, 0);
406 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
407 SpvStorageClassUniform
,
410 SpvId var_id
= spirv_builder_emit_var(&ctx
->builder
, pointer_type
,
411 SpvStorageClassUniform
);
413 spirv_builder_emit_name(&ctx
->builder
, var_id
, var
->name
);
415 assert(ctx
->num_ubos
< ARRAY_SIZE(ctx
->ubos
));
416 ctx
->ubos
[ctx
->num_ubos
++] = var_id
;
418 spirv_builder_emit_descriptor_set(&ctx
->builder
, var_id
,
419 var
->data
.descriptor_set
);
420 spirv_builder_emit_binding(&ctx
->builder
, var_id
, var
->data
.binding
);
424 emit_uniform(struct ntv_context
*ctx
, struct nir_variable
*var
)
426 if (var
->data
.mode
== nir_var_mem_ubo
)
429 assert(var
->data
.mode
== nir_var_uniform
);
430 if (glsl_type_is_sampler(var
->type
))
431 emit_sampler(ctx
, var
);
436 get_src_uint_ssa(struct ntv_context
*ctx
, const nir_ssa_def
*ssa
)
438 assert(ssa
->index
< ctx
->num_defs
);
439 assert(ctx
->defs
[ssa
->index
] != 0);
440 return ctx
->defs
[ssa
->index
];
444 get_var_from_reg(struct ntv_context
*ctx
, nir_register
*reg
)
446 assert(reg
->index
< ctx
->num_regs
);
447 assert(ctx
->regs
[reg
->index
] != 0);
448 return ctx
->regs
[reg
->index
];
452 get_src_uint_reg(struct ntv_context
*ctx
, const nir_reg_src
*reg
)
455 assert(!reg
->indirect
);
456 assert(!reg
->base_offset
);
458 SpvId var
= get_var_from_reg(ctx
, reg
->reg
);
459 SpvId type
= get_uvec_type(ctx
, reg
->reg
->bit_size
, reg
->reg
->num_components
);
460 return spirv_builder_emit_load(&ctx
->builder
, type
, var
);
464 get_src_uint(struct ntv_context
*ctx
, nir_src
*src
)
467 return get_src_uint_ssa(ctx
, src
->ssa
);
469 return get_src_uint_reg(ctx
, &src
->reg
);
473 get_alu_src_uint(struct ntv_context
*ctx
, nir_alu_instr
*alu
, unsigned src
)
475 assert(!alu
->src
[src
].negate
);
476 assert(!alu
->src
[src
].abs
);
478 SpvId def
= get_src_uint(ctx
, &alu
->src
[src
].src
);
480 unsigned used_channels
= 0;
481 bool need_swizzle
= false;
482 for (unsigned i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; i
++) {
483 if (!nir_alu_instr_channel_used(alu
, src
, i
))
488 if (alu
->src
[src
].swizzle
[i
] != i
)
491 assert(used_channels
!= 0);
493 unsigned live_channels
= nir_src_num_components(alu
->src
[src
].src
);
494 if (used_channels
!= live_channels
)
500 int bit_size
= nir_src_bit_size(alu
->src
[src
].src
);
501 assert(bit_size
== 32);
503 SpvId uint_type
= spirv_builder_type_uint(&ctx
->builder
, bit_size
);
504 if (used_channels
== 1) {
505 uint32_t indices
[] = { alu
->src
[src
].swizzle
[0] };
506 return spirv_builder_emit_composite_extract(&ctx
->builder
, uint_type
,
508 ARRAY_SIZE(indices
));
509 } else if (live_channels
== 1) {
510 SpvId uvec_type
= spirv_builder_type_vector(&ctx
->builder
, uint_type
,
513 SpvId constituents
[NIR_MAX_VEC_COMPONENTS
];
514 for (unsigned i
= 0; i
< used_channels
; ++i
)
515 constituents
[i
] = def
;
517 return spirv_builder_emit_composite_construct(&ctx
->builder
, uvec_type
,
521 SpvId uvec_type
= spirv_builder_type_vector(&ctx
->builder
, uint_type
,
524 uint32_t components
[NIR_MAX_VEC_COMPONENTS
];
525 size_t num_components
= 0;
526 for (unsigned i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; i
++) {
527 if (!nir_alu_instr_channel_used(alu
, src
, i
))
530 components
[num_components
++] = alu
->src
[src
].swizzle
[i
];
533 return spirv_builder_emit_vector_shuffle(&ctx
->builder
, uvec_type
,
534 def
, def
, components
, num_components
);
539 store_ssa_def_uint(struct ntv_context
*ctx
, nir_ssa_def
*ssa
, SpvId result
)
542 assert(ssa
->index
< ctx
->num_defs
);
543 ctx
->defs
[ssa
->index
] = result
;
547 emit_select(struct ntv_context
*ctx
, SpvId type
, SpvId cond
,
548 SpvId if_true
, SpvId if_false
)
550 return emit_triop(ctx
, SpvOpSelect
, type
, cond
, if_true
, if_false
);
554 bvec_to_uvec(struct ntv_context
*ctx
, SpvId value
, unsigned num_components
)
556 SpvId otype
= get_uvec_type(ctx
, 32, num_components
);
557 uint32_t zeros
[4] = { 0, 0, 0, 0 };
558 uint32_t ones
[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff };
559 SpvId zero
= get_uvec_constant(ctx
, 32, num_components
, zeros
);
560 SpvId one
= get_uvec_constant(ctx
, 32, num_components
, ones
);
561 return emit_select(ctx
, otype
, value
, one
, zero
);
565 uvec_to_bvec(struct ntv_context
*ctx
, SpvId value
, unsigned num_components
)
567 SpvId type
= get_bvec_type(ctx
, num_components
);
569 uint32_t zeros
[NIR_MAX_VEC_COMPONENTS
] = { 0 };
570 SpvId zero
= get_uvec_constant(ctx
, 32, num_components
, zeros
);
572 return emit_binop(ctx
, SpvOpINotEqual
, type
, value
, zero
);
576 emit_bitcast(struct ntv_context
*ctx
, SpvId type
, SpvId value
)
578 return emit_unop(ctx
, SpvOpBitcast
, type
, value
);
582 bitcast_to_uvec(struct ntv_context
*ctx
, SpvId value
, unsigned bit_size
,
583 unsigned num_components
)
585 SpvId type
= get_uvec_type(ctx
, bit_size
, num_components
);
586 return emit_bitcast(ctx
, type
, value
);
590 bitcast_to_ivec(struct ntv_context
*ctx
, SpvId value
, unsigned bit_size
,
591 unsigned num_components
)
593 SpvId type
= get_ivec_type(ctx
, bit_size
, num_components
);
594 return emit_bitcast(ctx
, type
, value
);
598 bitcast_to_fvec(struct ntv_context
*ctx
, SpvId value
, unsigned bit_size
,
599 unsigned num_components
)
601 SpvId type
= get_fvec_type(ctx
, bit_size
, num_components
);
602 return emit_bitcast(ctx
, type
, value
);
606 store_reg_def(struct ntv_context
*ctx
, nir_reg_dest
*reg
, SpvId result
)
608 SpvId var
= get_var_from_reg(ctx
, reg
->reg
);
610 spirv_builder_emit_store(&ctx
->builder
, var
, result
);
614 store_dest_uint(struct ntv_context
*ctx
, nir_dest
*dest
, SpvId result
)
617 store_ssa_def_uint(ctx
, &dest
->ssa
, result
);
619 store_reg_def(ctx
, &dest
->reg
, result
);
623 store_dest(struct ntv_context
*ctx
, nir_dest
*dest
, SpvId result
, nir_alu_type type
)
625 unsigned num_components
= nir_dest_num_components(*dest
);
626 unsigned bit_size
= nir_dest_bit_size(*dest
);
628 switch (nir_alu_type_get_base_type(type
)) {
630 assert(bit_size
== 1);
631 result
= bvec_to_uvec(ctx
, result
, num_components
);
635 break; /* nothing to do! */
639 result
= bitcast_to_uvec(ctx
, result
, bit_size
, num_components
);
643 unreachable("unsupported nir_alu_type");
646 store_dest_uint(ctx
, dest
, result
);
650 emit_unop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
, SpvId src
)
652 return spirv_builder_emit_unop(&ctx
->builder
, op
, type
, src
);
656 emit_binop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
,
657 SpvId src0
, SpvId src1
)
659 return spirv_builder_emit_binop(&ctx
->builder
, op
, type
, src0
, src1
);
663 emit_triop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
,
664 SpvId src0
, SpvId src1
, SpvId src2
)
666 return spirv_builder_emit_triop(&ctx
->builder
, op
, type
, src0
, src1
, src2
);
670 emit_builtin_unop(struct ntv_context
*ctx
, enum GLSLstd450 op
, SpvId type
,
673 SpvId args
[] = { src
};
674 return spirv_builder_emit_ext_inst(&ctx
->builder
, type
, ctx
->GLSL_std_450
,
675 op
, args
, ARRAY_SIZE(args
));
679 emit_builtin_binop(struct ntv_context
*ctx
, enum GLSLstd450 op
, SpvId type
,
680 SpvId src0
, SpvId src1
)
682 SpvId args
[] = { src0
, src1
};
683 return spirv_builder_emit_ext_inst(&ctx
->builder
, type
, ctx
->GLSL_std_450
,
684 op
, args
, ARRAY_SIZE(args
));
688 get_fvec_constant(struct ntv_context
*ctx
, int bit_size
, int num_components
,
689 const float values
[])
691 assert(bit_size
== 32);
693 if (num_components
> 1) {
694 SpvId components
[num_components
];
695 for (int i
= 0; i
< num_components
; i
++)
696 components
[i
] = emit_float_const(ctx
, bit_size
, values
[i
]);
698 SpvId type
= get_fvec_type(ctx
, bit_size
, num_components
);
699 return spirv_builder_const_composite(&ctx
->builder
, type
, components
,
703 assert(num_components
== 1);
704 return emit_float_const(ctx
, bit_size
, values
[0]);
708 get_uvec_constant(struct ntv_context
*ctx
, int bit_size
, int num_components
,
709 const uint32_t values
[])
711 assert(bit_size
== 32);
713 if (num_components
> 1) {
714 SpvId components
[num_components
];
715 for (int i
= 0; i
< num_components
; i
++)
716 components
[i
] = emit_uint_const(ctx
, bit_size
, values
[i
]);
718 SpvId type
= get_uvec_type(ctx
, bit_size
, num_components
);
719 return spirv_builder_const_composite(&ctx
->builder
, type
, components
,
723 assert(num_components
== 1);
724 return emit_uint_const(ctx
, bit_size
, values
[0]);
727 static inline unsigned
728 alu_instr_src_components(const nir_alu_instr
*instr
, unsigned src
)
730 if (nir_op_infos
[instr
->op
].input_sizes
[src
] > 0)
731 return nir_op_infos
[instr
->op
].input_sizes
[src
];
733 if (instr
->dest
.dest
.is_ssa
)
734 return instr
->dest
.dest
.ssa
.num_components
;
736 return instr
->dest
.dest
.reg
.reg
->num_components
;
740 get_alu_src(struct ntv_context
*ctx
, nir_alu_instr
*alu
, unsigned src
)
742 SpvId uint_value
= get_alu_src_uint(ctx
, alu
, src
);
744 unsigned num_components
= alu_instr_src_components(alu
, src
);
745 unsigned bit_size
= nir_src_bit_size(alu
->src
[src
].src
);
746 nir_alu_type type
= nir_op_infos
[alu
->op
].input_types
[src
];
748 switch (nir_alu_type_get_base_type(type
)) {
750 assert(bit_size
== 1);
751 return uvec_to_bvec(ctx
, uint_value
, num_components
);
754 return bitcast_to_ivec(ctx
, uint_value
, bit_size
, num_components
);
760 return bitcast_to_fvec(ctx
, uint_value
, bit_size
, num_components
);
763 unreachable("unknown nir_alu_type");
768 store_alu_result(struct ntv_context
*ctx
, nir_alu_instr
*alu
, SpvId result
)
770 assert(!alu
->dest
.saturate
);
771 return store_dest(ctx
, &alu
->dest
.dest
, result
, nir_op_infos
[alu
->op
].output_type
);
775 get_dest_type(struct ntv_context
*ctx
, nir_dest
*dest
, nir_alu_type type
)
777 unsigned num_components
= nir_dest_num_components(*dest
);
778 unsigned bit_size
= nir_dest_bit_size(*dest
);
780 switch (nir_alu_type_get_base_type(type
)) {
782 return get_bvec_type(ctx
, num_components
);
785 return get_ivec_type(ctx
, bit_size
, num_components
);
788 return get_uvec_type(ctx
, bit_size
, num_components
);
791 return get_fvec_type(ctx
, bit_size
, num_components
);
794 unreachable("unsupported nir_alu_type");
799 emit_alu(struct ntv_context
*ctx
, nir_alu_instr
*alu
)
801 SpvId src
[nir_op_infos
[alu
->op
].num_inputs
];
802 for (unsigned i
= 0; i
< nir_op_infos
[alu
->op
].num_inputs
; i
++)
803 src
[i
] = get_alu_src(ctx
, alu
, i
);
805 SpvId dest_type
= get_dest_type(ctx
, &alu
->dest
.dest
,
806 nir_op_infos
[alu
->op
].output_type
);
807 unsigned bit_size
= nir_dest_bit_size(alu
->dest
.dest
);
808 unsigned num_components
= nir_dest_num_components(alu
->dest
.dest
);
813 assert(nir_op_infos
[alu
->op
].num_inputs
== 1);
817 #define UNOP(nir_op, spirv_op) \
819 assert(nir_op_infos[alu->op].num_inputs == 1); \
820 result = emit_unop(ctx, spirv_op, dest_type, src[0]); \
823 UNOP(nir_op_ineg
, SpvOpSNegate
)
824 UNOP(nir_op_fneg
, SpvOpFNegate
)
825 UNOP(nir_op_fddx
, SpvOpDPdx
)
826 UNOP(nir_op_fddy
, SpvOpDPdy
)
827 UNOP(nir_op_f2i32
, SpvOpConvertFToS
)
828 UNOP(nir_op_f2u32
, SpvOpConvertFToU
)
829 UNOP(nir_op_i2f32
, SpvOpConvertSToF
)
830 UNOP(nir_op_u2f32
, SpvOpConvertUToF
)
831 UNOP(nir_op_inot
, SpvOpNot
)
835 assert(nir_op_infos
[alu
->op
].num_inputs
== 1);
836 result
= bvec_to_uvec(ctx
, src
[0], num_components
);
839 #define BUILTIN_UNOP(nir_op, spirv_op) \
841 assert(nir_op_infos[alu->op].num_inputs == 1); \
842 result = emit_builtin_unop(ctx, spirv_op, dest_type, src[0]); \
845 BUILTIN_UNOP(nir_op_fabs
, GLSLstd450FAbs
)
846 BUILTIN_UNOP(nir_op_fsqrt
, GLSLstd450Sqrt
)
847 BUILTIN_UNOP(nir_op_frsq
, GLSLstd450InverseSqrt
)
848 BUILTIN_UNOP(nir_op_flog2
, GLSLstd450Log2
)
849 BUILTIN_UNOP(nir_op_fexp2
, GLSLstd450Exp2
)
850 BUILTIN_UNOP(nir_op_ffract
, GLSLstd450Fract
)
851 BUILTIN_UNOP(nir_op_ffloor
, GLSLstd450Floor
)
852 BUILTIN_UNOP(nir_op_fceil
, GLSLstd450Ceil
)
853 BUILTIN_UNOP(nir_op_ftrunc
, GLSLstd450Trunc
)
854 BUILTIN_UNOP(nir_op_fround_even
, GLSLstd450RoundEven
)
855 BUILTIN_UNOP(nir_op_fsign
, GLSLstd450FSign
)
856 BUILTIN_UNOP(nir_op_fsin
, GLSLstd450Sin
)
857 BUILTIN_UNOP(nir_op_fcos
, GLSLstd450Cos
)
861 assert(nir_op_infos
[alu
->op
].num_inputs
== 1);
862 float one
[4] = { 1, 1, 1, 1 };
864 src
[0] = get_fvec_constant(ctx
, bit_size
, num_components
, one
);
865 result
= emit_binop(ctx
, SpvOpFDiv
, dest_type
, src
[0], src
[1]);
870 assert(nir_op_infos
[alu
->op
].num_inputs
== 1);
871 float values
[NIR_MAX_VEC_COMPONENTS
] = { 0 };
872 SpvId zero
= get_fvec_constant(ctx
, nir_src_bit_size(alu
->src
[0].src
),
873 num_components
, values
);
874 result
= emit_binop(ctx
, SpvOpFOrdNotEqual
, dest_type
, src
[0], zero
);
878 #define BINOP(nir_op, spirv_op) \
880 assert(nir_op_infos[alu->op].num_inputs == 2); \
881 result = emit_binop(ctx, spirv_op, dest_type, src[0], src[1]); \
884 BINOP(nir_op_iadd
, SpvOpIAdd
)
885 BINOP(nir_op_isub
, SpvOpISub
)
886 BINOP(nir_op_imul
, SpvOpIMul
)
887 BINOP(nir_op_idiv
, SpvOpSDiv
)
888 BINOP(nir_op_udiv
, SpvOpUDiv
)
889 BINOP(nir_op_fadd
, SpvOpFAdd
)
890 BINOP(nir_op_fsub
, SpvOpFSub
)
891 BINOP(nir_op_fmul
, SpvOpFMul
)
892 BINOP(nir_op_fdiv
, SpvOpFDiv
)
893 BINOP(nir_op_fmod
, SpvOpFMod
)
894 BINOP(nir_op_ilt
, SpvOpSLessThan
)
895 BINOP(nir_op_ige
, SpvOpSGreaterThanEqual
)
896 BINOP(nir_op_ieq
, SpvOpIEqual
)
897 BINOP(nir_op_ine
, SpvOpINotEqual
)
898 BINOP(nir_op_flt
, SpvOpFOrdLessThan
)
899 BINOP(nir_op_fge
, SpvOpFOrdGreaterThanEqual
)
900 BINOP(nir_op_feq
, SpvOpFOrdEqual
)
901 BINOP(nir_op_fne
, SpvOpFOrdNotEqual
)
902 BINOP(nir_op_ishl
, SpvOpShiftLeftLogical
)
903 BINOP(nir_op_ishr
, SpvOpShiftRightArithmetic
)
904 BINOP(nir_op_ushr
, SpvOpShiftRightLogical
)
905 BINOP(nir_op_iand
, SpvOpBitwiseAnd
)
906 BINOP(nir_op_ior
, SpvOpBitwiseOr
)
909 #define BUILTIN_BINOP(nir_op, spirv_op) \
911 assert(nir_op_infos[alu->op].num_inputs == 2); \
912 result = emit_builtin_binop(ctx, spirv_op, dest_type, src[0], src[1]); \
915 BUILTIN_BINOP(nir_op_fmin
, GLSLstd450FMin
)
916 BUILTIN_BINOP(nir_op_fmax
, GLSLstd450FMax
)
922 assert(nir_op_infos
[alu
->op
].num_inputs
== 2);
923 result
= emit_binop(ctx
, SpvOpDot
, dest_type
, src
[0], src
[1]);
930 assert(nir_op_infos
[alu
->op
].num_inputs
== 2);
931 int num_components
= nir_dest_num_components(alu
->dest
.dest
);
932 SpvId bool_type
= get_bvec_type(ctx
, num_components
);
934 SpvId zero
= emit_float_const(ctx
, bit_size
, 0.0f
);
935 SpvId one
= emit_float_const(ctx
, bit_size
, 1.0f
);
936 if (num_components
> 1) {
937 SpvId zero_comps
[num_components
], one_comps
[num_components
];
938 for (int i
= 0; i
< num_components
; i
++) {
939 zero_comps
[i
] = zero
;
943 zero
= spirv_builder_const_composite(&ctx
->builder
, dest_type
,
944 zero_comps
, num_components
);
945 one
= spirv_builder_const_composite(&ctx
->builder
, dest_type
,
946 one_comps
, num_components
);
951 case nir_op_seq
: op
= SpvOpFOrdEqual
; break;
952 case nir_op_sne
: op
= SpvOpFOrdNotEqual
; break;
953 case nir_op_slt
: op
= SpvOpFOrdLessThan
; break;
954 case nir_op_sge
: op
= SpvOpFOrdGreaterThanEqual
; break;
955 default: unreachable("unexpected op");
958 result
= emit_binop(ctx
, op
, bool_type
, src
[0], src
[1]);
959 result
= emit_select(ctx
, dest_type
, result
, one
, zero
);
964 assert(nir_op_infos
[alu
->op
].num_inputs
== 3);
965 int num_components
= nir_dest_num_components(alu
->dest
.dest
);
966 SpvId bool_type
= get_bvec_type(ctx
, num_components
);
968 float zero
[4] = { 0, 0, 0, 0 };
969 SpvId cmp
= get_fvec_constant(ctx
, nir_src_bit_size(alu
->src
[0].src
),
970 num_components
, zero
);
972 result
= emit_binop(ctx
, SpvOpFOrdGreaterThan
, bool_type
, src
[0], cmp
);
973 result
= emit_select(ctx
, dest_type
, result
, src
[1], src
[2]);
978 assert(nir_op_infos
[alu
->op
].num_inputs
== 3);
979 result
= emit_select(ctx
, dest_type
, src
[0], src
[1], src
[2]);
985 int num_inputs
= nir_op_infos
[alu
->op
].num_inputs
;
986 assert(2 <= num_inputs
&& num_inputs
<= 4);
987 result
= spirv_builder_emit_composite_construct(&ctx
->builder
, dest_type
,
993 fprintf(stderr
, "emit_alu: not implemented (%s)\n",
994 nir_op_infos
[alu
->op
].name
);
996 unreachable("unsupported opcode");
1000 store_alu_result(ctx
, alu
, result
);
1004 emit_load_const(struct ntv_context
*ctx
, nir_load_const_instr
*load_const
)
1006 uint32_t values
[NIR_MAX_VEC_COMPONENTS
];
1007 for (int i
= 0; i
< load_const
->def
.num_components
; ++i
)
1008 values
[i
] = load_const
->value
[i
].u32
;
1010 SpvId constant
= get_uvec_constant(ctx
, load_const
->def
.bit_size
,
1011 load_const
->def
.num_components
,
1013 store_ssa_def_uint(ctx
, &load_const
->def
, constant
);
1017 emit_load_ubo(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
1019 nir_const_value
*const_block_index
= nir_src_as_const_value(intr
->src
[0]);
1020 assert(const_block_index
); // no dynamic indexing for now
1021 assert(const_block_index
->u32
== 0); // we only support the default UBO for now
1023 nir_const_value
*const_offset
= nir_src_as_const_value(intr
->src
[1]);
1025 SpvId uvec4_type
= get_uvec_type(ctx
, 32, 4);
1026 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
1027 SpvStorageClassUniform
,
1030 unsigned idx
= const_offset
->u32
;
1031 SpvId member
= emit_uint_const(ctx
, 32, 0);
1032 SpvId offset
= emit_uint_const(ctx
, 32, idx
);
1033 SpvId offsets
[] = { member
, offset
};
1034 SpvId ptr
= spirv_builder_emit_access_chain(&ctx
->builder
, pointer_type
,
1035 ctx
->ubos
[0], offsets
,
1036 ARRAY_SIZE(offsets
));
1037 SpvId result
= spirv_builder_emit_load(&ctx
->builder
, uvec4_type
, ptr
);
1039 SpvId type
= get_dest_uvec_type(ctx
, &intr
->dest
);
1040 unsigned num_components
= nir_dest_num_components(intr
->dest
);
1041 if (num_components
== 1) {
1042 uint32_t components
[] = { 0 };
1043 result
= spirv_builder_emit_composite_extract(&ctx
->builder
,
1047 } else if (num_components
< 4) {
1048 SpvId constituents
[num_components
];
1049 SpvId uint_type
= spirv_builder_type_uint(&ctx
->builder
, 32);
1050 for (uint32_t i
= 0; i
< num_components
; ++i
)
1051 constituents
[i
] = spirv_builder_emit_composite_extract(&ctx
->builder
,
1056 result
= spirv_builder_emit_composite_construct(&ctx
->builder
,
1062 store_dest_uint(ctx
, &intr
->dest
, result
);
1064 unreachable("uniform-addressing not yet supported");
1068 emit_discard(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
1070 assert(ctx
->block_started
);
1071 spirv_builder_emit_kill(&ctx
->builder
);
1072 /* discard is weird in NIR, so let's just create an unreachable block after
1073 it and hope that the vulkan driver will DCE any instructinos in it. */
1074 spirv_builder_label(&ctx
->builder
, spirv_builder_new_id(&ctx
->builder
));
1078 emit_load_deref(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
1080 /* uint is a bit of a lie here; it's really just a pointer */
1081 SpvId ptr
= get_src_uint(ctx
, intr
->src
);
1083 nir_variable
*var
= nir_intrinsic_get_var(intr
, 0);
1084 SpvId result
= spirv_builder_emit_load(&ctx
->builder
,
1085 get_glsl_type(ctx
, var
->type
),
1087 unsigned num_components
= nir_dest_num_components(intr
->dest
);
1088 unsigned bit_size
= nir_dest_bit_size(intr
->dest
);
1089 result
= bitcast_to_uvec(ctx
, result
, bit_size
, num_components
);
1090 store_dest_uint(ctx
, &intr
->dest
, result
);
1094 emit_store_deref(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
1096 /* uint is a bit of a lie here; it's really just a pointer */
1097 SpvId ptr
= get_src_uint(ctx
, &intr
->src
[0]);
1098 SpvId src
= get_src_uint(ctx
, &intr
->src
[1]);
1100 nir_variable
*var
= nir_intrinsic_get_var(intr
, 0);
1101 SpvId type
= get_glsl_type(ctx
, glsl_without_array(var
->type
));
1102 SpvId result
= emit_bitcast(ctx
, type
, src
);
1103 spirv_builder_emit_store(&ctx
->builder
, ptr
, result
);
1107 emit_intrinsic(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
1109 switch (intr
->intrinsic
) {
1110 case nir_intrinsic_load_ubo
:
1111 emit_load_ubo(ctx
, intr
);
1114 case nir_intrinsic_discard
:
1115 emit_discard(ctx
, intr
);
1118 case nir_intrinsic_load_deref
:
1119 emit_load_deref(ctx
, intr
);
1122 case nir_intrinsic_store_deref
:
1123 emit_store_deref(ctx
, intr
);
1127 fprintf(stderr
, "emit_intrinsic: not implemented (%s)\n",
1128 nir_intrinsic_infos
[intr
->intrinsic
].name
);
1129 unreachable("unsupported intrinsic");
1134 emit_undef(struct ntv_context
*ctx
, nir_ssa_undef_instr
*undef
)
1136 SpvId type
= get_uvec_type(ctx
, undef
->def
.bit_size
,
1137 undef
->def
.num_components
);
1139 store_ssa_def_uint(ctx
, &undef
->def
,
1140 spirv_builder_emit_undef(&ctx
->builder
, type
));
1144 get_src_float(struct ntv_context
*ctx
, nir_src
*src
)
1146 SpvId def
= get_src_uint(ctx
, src
);
1147 unsigned num_components
= nir_src_num_components(*src
);
1148 unsigned bit_size
= nir_src_bit_size(*src
);
1149 return bitcast_to_fvec(ctx
, def
, bit_size
, num_components
);
1153 emit_tex(struct ntv_context
*ctx
, nir_tex_instr
*tex
)
1155 assert(tex
->op
== nir_texop_tex
||
1156 tex
->op
== nir_texop_txb
||
1157 tex
->op
== nir_texop_txl
);
1158 assert(nir_alu_type_get_base_type(tex
->dest_type
) == nir_type_float
);
1159 assert(tex
->texture_index
== tex
->sampler_index
);
1161 SpvId coord
= 0, proj
= 0, bias
= 0, lod
= 0, dref
= 0;
1162 unsigned coord_components
;
1163 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
1164 switch (tex
->src
[i
].src_type
) {
1165 case nir_tex_src_coord
:
1166 coord
= get_src_float(ctx
, &tex
->src
[i
].src
);
1167 coord_components
= nir_src_num_components(tex
->src
[i
].src
);
1170 case nir_tex_src_projector
:
1171 assert(nir_src_num_components(tex
->src
[i
].src
) == 1);
1172 proj
= get_src_float(ctx
, &tex
->src
[i
].src
);
1176 case nir_tex_src_bias
:
1177 assert(tex
->op
== nir_texop_txb
);
1178 bias
= get_src_float(ctx
, &tex
->src
[i
].src
);
1182 case nir_tex_src_lod
:
1183 assert(nir_src_num_components(tex
->src
[i
].src
) == 1);
1184 lod
= get_src_float(ctx
, &tex
->src
[i
].src
);
1188 case nir_tex_src_comparator
:
1189 assert(nir_src_num_components(tex
->src
[i
].src
) == 1);
1190 dref
= get_src_float(ctx
, &tex
->src
[i
].src
);
1195 fprintf(stderr
, "texture source: %d\n", tex
->src
[i
].src_type
);
1196 unreachable("unknown texture source");
1200 if (lod
== 0 && ctx
->stage
!= MESA_SHADER_FRAGMENT
) {
1201 lod
= emit_float_const(ctx
, 32, 0.0f
);
1206 SpvDim dimension
= type_to_dim(tex
->sampler_dim
, &is_ms
);
1207 SpvId float_type
= spirv_builder_type_float(&ctx
->builder
, 32);
1208 SpvId image_type
= spirv_builder_type_image(&ctx
->builder
, float_type
,
1209 dimension
, false, tex
->is_array
, is_ms
, 1,
1210 SpvImageFormatUnknown
);
1211 SpvId sampled_type
= spirv_builder_type_sampled_image(&ctx
->builder
,
1214 assert(tex
->texture_index
< ctx
->num_samplers
);
1215 SpvId load
= spirv_builder_emit_load(&ctx
->builder
, sampled_type
,
1216 ctx
->samplers
[tex
->texture_index
]);
1218 SpvId dest_type
= get_dest_type(ctx
, &tex
->dest
, tex
->dest_type
);
1221 SpvId constituents
[coord_components
+ 1];
1222 if (coord_components
== 1)
1223 constituents
[0] = coord
;
1225 assert(coord_components
> 1);
1226 SpvId float_type
= spirv_builder_type_float(&ctx
->builder
, 32);
1227 for (uint32_t i
= 0; i
< coord_components
; ++i
)
1228 constituents
[i
] = spirv_builder_emit_composite_extract(&ctx
->builder
,
1234 constituents
[coord_components
++] = proj
;
1236 SpvId vec_type
= get_fvec_type(ctx
, 32, coord_components
);
1237 coord
= spirv_builder_emit_composite_construct(&ctx
->builder
,
1243 SpvId actual_dest_type
= dest_type
;
1245 actual_dest_type
= float_type
;
1247 SpvId result
= spirv_builder_emit_image_sample(&ctx
->builder
,
1248 actual_dest_type
, load
,
1252 spirv_builder_emit_decoration(&ctx
->builder
, result
,
1253 SpvDecorationRelaxedPrecision
);
1256 SpvId components
[4] = { result
, result
, result
, result
};
1257 result
= spirv_builder_emit_composite_construct(&ctx
->builder
,
1263 store_dest(ctx
, &tex
->dest
, result
, tex
->dest_type
);
1267 start_block(struct ntv_context
*ctx
, SpvId label
)
1269 /* terminate previous block if needed */
1270 if (ctx
->block_started
)
1271 spirv_builder_emit_branch(&ctx
->builder
, label
);
1273 /* start new block */
1274 spirv_builder_label(&ctx
->builder
, label
);
1275 ctx
->block_started
= true;
1279 branch(struct ntv_context
*ctx
, SpvId label
)
1281 assert(ctx
->block_started
);
1282 spirv_builder_emit_branch(&ctx
->builder
, label
);
1283 ctx
->block_started
= false;
1287 branch_conditional(struct ntv_context
*ctx
, SpvId condition
, SpvId then_id
,
1290 assert(ctx
->block_started
);
1291 spirv_builder_emit_branch_conditional(&ctx
->builder
, condition
,
1293 ctx
->block_started
= false;
1297 emit_jump(struct ntv_context
*ctx
, nir_jump_instr
*jump
)
1299 switch (jump
->type
) {
1300 case nir_jump_break
:
1301 assert(ctx
->loop_break
);
1302 branch(ctx
, ctx
->loop_break
);
1305 case nir_jump_continue
:
1306 assert(ctx
->loop_cont
);
1307 branch(ctx
, ctx
->loop_cont
);
1311 unreachable("Unsupported jump type\n");
1316 emit_deref_var(struct ntv_context
*ctx
, nir_deref_instr
*deref
)
1318 assert(deref
->deref_type
== nir_deref_type_var
);
1320 struct hash_entry
*he
= _mesa_hash_table_search(ctx
->vars
, deref
->var
);
1322 SpvId result
= (SpvId
)(intptr_t)he
->data
;
1323 /* uint is a bit of a lie here, it's really just an opaque type */
1324 store_dest_uint(ctx
, &deref
->dest
, result
);
1328 emit_deref_array(struct ntv_context
*ctx
, nir_deref_instr
*deref
)
1330 assert(deref
->deref_type
== nir_deref_type_array
);
1331 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
1333 SpvStorageClass storage_class
;
1334 switch (var
->data
.mode
) {
1335 case nir_var_shader_in
:
1336 storage_class
= SpvStorageClassInput
;
1339 case nir_var_shader_out
:
1340 storage_class
= SpvStorageClassOutput
;
1344 unreachable("Unsupported nir_variable_mode\n");
1347 SpvId index
= get_src_uint(ctx
, &deref
->arr
.index
);
1349 SpvId ptr_type
= spirv_builder_type_pointer(&ctx
->builder
,
1351 get_glsl_type(ctx
, deref
->type
));
1353 SpvId result
= spirv_builder_emit_access_chain(&ctx
->builder
,
1355 get_src_uint(ctx
, &deref
->parent
),
1357 /* uint is a bit of a lie here, it's really just an opaque type */
1358 store_dest_uint(ctx
, &deref
->dest
, result
);
1362 emit_deref(struct ntv_context
*ctx
, nir_deref_instr
*deref
)
1364 switch (deref
->deref_type
) {
1365 case nir_deref_type_var
:
1366 emit_deref_var(ctx
, deref
);
1369 case nir_deref_type_array
:
1370 emit_deref_array(ctx
, deref
);
1374 unreachable("unexpected deref_type");
1379 emit_block(struct ntv_context
*ctx
, struct nir_block
*block
)
1381 start_block(ctx
, block_label(ctx
, block
));
1382 nir_foreach_instr(instr
, block
) {
1383 switch (instr
->type
) {
1384 case nir_instr_type_alu
:
1385 emit_alu(ctx
, nir_instr_as_alu(instr
));
1387 case nir_instr_type_intrinsic
:
1388 emit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
1390 case nir_instr_type_load_const
:
1391 emit_load_const(ctx
, nir_instr_as_load_const(instr
));
1393 case nir_instr_type_ssa_undef
:
1394 emit_undef(ctx
, nir_instr_as_ssa_undef(instr
));
1396 case nir_instr_type_tex
:
1397 emit_tex(ctx
, nir_instr_as_tex(instr
));
1399 case nir_instr_type_phi
:
1400 unreachable("nir_instr_type_phi not supported");
1402 case nir_instr_type_jump
:
1403 emit_jump(ctx
, nir_instr_as_jump(instr
));
1405 case nir_instr_type_call
:
1406 unreachable("nir_instr_type_call not supported");
1408 case nir_instr_type_parallel_copy
:
1409 unreachable("nir_instr_type_parallel_copy not supported");
1411 case nir_instr_type_deref
:
1412 emit_deref(ctx
, nir_instr_as_deref(instr
));
1419 emit_cf_list(struct ntv_context
*ctx
, struct exec_list
*list
);
1422 get_src_bool(struct ntv_context
*ctx
, nir_src
*src
)
1424 SpvId def
= get_src_uint(ctx
, src
);
1425 assert(nir_src_bit_size(*src
) == 32);
1426 unsigned num_components
= nir_src_num_components(*src
);
1427 return uvec_to_bvec(ctx
, def
, num_components
);
1431 emit_if(struct ntv_context
*ctx
, nir_if
*if_stmt
)
1433 SpvId condition
= get_src_bool(ctx
, &if_stmt
->condition
);
1435 SpvId header_id
= spirv_builder_new_id(&ctx
->builder
);
1436 SpvId then_id
= block_label(ctx
, nir_if_first_then_block(if_stmt
));
1437 SpvId endif_id
= spirv_builder_new_id(&ctx
->builder
);
1438 SpvId else_id
= endif_id
;
1440 bool has_else
= !exec_list_is_empty(&if_stmt
->else_list
);
1442 assert(nir_if_first_else_block(if_stmt
)->index
< ctx
->num_blocks
);
1443 else_id
= block_label(ctx
, nir_if_first_else_block(if_stmt
));
1446 /* create a header-block */
1447 start_block(ctx
, header_id
);
1448 spirv_builder_emit_selection_merge(&ctx
->builder
, endif_id
,
1449 SpvSelectionControlMaskNone
);
1450 branch_conditional(ctx
, condition
, then_id
, else_id
);
1452 emit_cf_list(ctx
, &if_stmt
->then_list
);
1455 if (ctx
->block_started
)
1456 branch(ctx
, endif_id
);
1458 emit_cf_list(ctx
, &if_stmt
->else_list
);
1461 start_block(ctx
, endif_id
);
1465 emit_loop(struct ntv_context
*ctx
, nir_loop
*loop
)
1467 SpvId header_id
= spirv_builder_new_id(&ctx
->builder
);
1468 SpvId begin_id
= block_label(ctx
, nir_loop_first_block(loop
));
1469 SpvId break_id
= spirv_builder_new_id(&ctx
->builder
);
1470 SpvId cont_id
= spirv_builder_new_id(&ctx
->builder
);
1472 /* create a header-block */
1473 start_block(ctx
, header_id
);
1474 spirv_builder_loop_merge(&ctx
->builder
, break_id
, cont_id
, SpvLoopControlMaskNone
);
1475 branch(ctx
, begin_id
);
1477 SpvId save_break
= ctx
->loop_break
;
1478 SpvId save_cont
= ctx
->loop_cont
;
1479 ctx
->loop_break
= break_id
;
1480 ctx
->loop_cont
= cont_id
;
1482 emit_cf_list(ctx
, &loop
->body
);
1484 ctx
->loop_break
= save_break
;
1485 ctx
->loop_cont
= save_cont
;
1487 branch(ctx
, cont_id
);
1488 start_block(ctx
, cont_id
);
1489 branch(ctx
, header_id
);
1491 start_block(ctx
, break_id
);
1495 emit_cf_list(struct ntv_context
*ctx
, struct exec_list
*list
)
1497 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
1498 switch (node
->type
) {
1499 case nir_cf_node_block
:
1500 emit_block(ctx
, nir_cf_node_as_block(node
));
1503 case nir_cf_node_if
:
1504 emit_if(ctx
, nir_cf_node_as_if(node
));
1507 case nir_cf_node_loop
:
1508 emit_loop(ctx
, nir_cf_node_as_loop(node
));
1511 case nir_cf_node_function
:
1512 unreachable("nir_cf_node_function not supported");
1518 struct spirv_shader
*
1519 nir_to_spirv(struct nir_shader
*s
)
1521 struct spirv_shader
*ret
= NULL
;
1523 struct ntv_context ctx
= {};
1525 switch (s
->info
.stage
) {
1526 case MESA_SHADER_VERTEX
:
1527 case MESA_SHADER_FRAGMENT
:
1528 case MESA_SHADER_COMPUTE
:
1529 spirv_builder_emit_cap(&ctx
.builder
, SpvCapabilityShader
);
1532 case MESA_SHADER_TESS_CTRL
:
1533 case MESA_SHADER_TESS_EVAL
:
1534 spirv_builder_emit_cap(&ctx
.builder
, SpvCapabilityTessellation
);
1537 case MESA_SHADER_GEOMETRY
:
1538 spirv_builder_emit_cap(&ctx
.builder
, SpvCapabilityGeometry
);
1542 unreachable("invalid stage");
1545 // TODO: only enable when needed
1546 if (s
->info
.stage
== MESA_SHADER_FRAGMENT
)
1547 spirv_builder_emit_cap(&ctx
.builder
, SpvCapabilitySampled1D
);
1549 ctx
.stage
= s
->info
.stage
;
1550 ctx
.GLSL_std_450
= spirv_builder_import(&ctx
.builder
, "GLSL.std.450");
1551 spirv_builder_emit_source(&ctx
.builder
, SpvSourceLanguageGLSL
, 450);
1553 spirv_builder_emit_mem_model(&ctx
.builder
, SpvAddressingModelLogical
,
1554 SpvMemoryModelGLSL450
);
1556 SpvExecutionModel exec_model
;
1557 switch (s
->info
.stage
) {
1558 case MESA_SHADER_VERTEX
:
1559 exec_model
= SpvExecutionModelVertex
;
1561 case MESA_SHADER_TESS_CTRL
:
1562 exec_model
= SpvExecutionModelTessellationControl
;
1564 case MESA_SHADER_TESS_EVAL
:
1565 exec_model
= SpvExecutionModelTessellationEvaluation
;
1567 case MESA_SHADER_GEOMETRY
:
1568 exec_model
= SpvExecutionModelGeometry
;
1570 case MESA_SHADER_FRAGMENT
:
1571 exec_model
= SpvExecutionModelFragment
;
1573 case MESA_SHADER_COMPUTE
:
1574 exec_model
= SpvExecutionModelGLCompute
;
1577 unreachable("invalid stage");
1580 SpvId type_void
= spirv_builder_type_void(&ctx
.builder
);
1581 SpvId type_main
= spirv_builder_type_function(&ctx
.builder
, type_void
,
1583 SpvId entry_point
= spirv_builder_new_id(&ctx
.builder
);
1584 spirv_builder_emit_name(&ctx
.builder
, entry_point
, "main");
1586 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
1587 _mesa_key_pointer_equal
);
1589 nir_foreach_variable(var
, &s
->inputs
)
1590 emit_input(&ctx
, var
);
1592 nir_foreach_variable(var
, &s
->outputs
)
1593 emit_output(&ctx
, var
);
1595 nir_foreach_variable(var
, &s
->uniforms
)
1596 emit_uniform(&ctx
, var
);
1598 spirv_builder_emit_entry_point(&ctx
.builder
, exec_model
, entry_point
,
1599 "main", ctx
.entry_ifaces
,
1600 ctx
.num_entry_ifaces
);
1601 if (s
->info
.stage
== MESA_SHADER_FRAGMENT
) {
1602 spirv_builder_emit_exec_mode(&ctx
.builder
, entry_point
,
1603 SpvExecutionModeOriginUpperLeft
);
1604 if (s
->info
.outputs_written
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
))
1605 spirv_builder_emit_exec_mode(&ctx
.builder
, entry_point
,
1606 SpvExecutionModeDepthReplacing
);
1610 spirv_builder_function(&ctx
.builder
, entry_point
, type_void
,
1611 SpvFunctionControlMaskNone
,
1614 nir_function_impl
*entry
= nir_shader_get_entrypoint(s
);
1615 nir_metadata_require(entry
, nir_metadata_block_index
);
1617 ctx
.defs
= (SpvId
*)malloc(sizeof(SpvId
) * entry
->ssa_alloc
);
1620 ctx
.num_defs
= entry
->ssa_alloc
;
1622 nir_index_local_regs(entry
);
1623 ctx
.regs
= malloc(sizeof(SpvId
) * entry
->reg_alloc
);
1626 ctx
.num_regs
= entry
->reg_alloc
;
1628 SpvId
*block_ids
= (SpvId
*)malloc(sizeof(SpvId
) * entry
->num_blocks
);
1632 for (int i
= 0; i
< entry
->num_blocks
; ++i
)
1633 block_ids
[i
] = spirv_builder_new_id(&ctx
.builder
);
1635 ctx
.block_ids
= block_ids
;
1636 ctx
.num_blocks
= entry
->num_blocks
;
1638 /* emit a block only for the variable declarations */
1639 start_block(&ctx
, spirv_builder_new_id(&ctx
.builder
));
1640 foreach_list_typed(nir_register
, reg
, node
, &entry
->registers
) {
1641 SpvId type
= get_uvec_type(&ctx
, reg
->bit_size
, reg
->num_components
);
1642 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
.builder
,
1643 SpvStorageClassFunction
,
1645 SpvId var
= spirv_builder_emit_var(&ctx
.builder
, pointer_type
,
1646 SpvStorageClassFunction
);
1648 ctx
.regs
[reg
->index
] = var
;
1651 emit_cf_list(&ctx
, &entry
->body
);
1655 spirv_builder_return(&ctx
.builder
); // doesn't belong here, but whatevz
1656 spirv_builder_function_end(&ctx
.builder
);
1658 size_t num_words
= spirv_builder_get_num_words(&ctx
.builder
);
1660 ret
= CALLOC_STRUCT(spirv_shader
);
1664 ret
->words
= MALLOC(sizeof(uint32_t) * num_words
);
1668 ret
->num_words
= spirv_builder_get_words(&ctx
.builder
, ret
->words
, num_words
);
1669 assert(ret
->num_words
== num_words
);
1676 spirv_shader_delete(ret
);
1679 _mesa_hash_table_destroy(ctx
.vars
, NULL
);
1685 spirv_shader_delete(struct spirv_shader
*s
)