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"
32 struct spirv_builder builder
;
36 gl_shader_stage stage
;
37 SpvId inputs
[PIPE_MAX_SHADER_INPUTS
][4];
38 SpvId input_types
[PIPE_MAX_SHADER_INPUTS
][4];
39 SpvId outputs
[PIPE_MAX_SHADER_OUTPUTS
][4];
40 SpvId output_types
[PIPE_MAX_SHADER_OUTPUTS
][4];
44 SpvId samplers
[PIPE_MAX_SAMPLERS
];
46 SpvId entry_ifaces
[PIPE_MAX_SHADER_INPUTS
* 4 + PIPE_MAX_SHADER_OUTPUTS
* 4];
47 size_t num_entry_ifaces
;
52 struct hash_table
*vars
;
56 get_fvec_constant(struct ntv_context
*ctx
, int bit_size
, int num_components
,
57 const float values
[]);
60 get_uvec_constant(struct ntv_context
*ctx
, int bit_size
, int num_components
,
61 const uint32_t values
[]);
64 emit_unop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
, SpvId src
);
67 emit_binop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
,
68 SpvId src0
, SpvId src1
);
71 emit_triop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
,
72 SpvId src0
, SpvId src1
, SpvId src2
);
75 get_bvec_type(struct ntv_context
*ctx
, int num_components
)
77 SpvId bool_type
= spirv_builder_type_bool(&ctx
->builder
);
78 if (num_components
> 1)
79 return spirv_builder_type_vector(&ctx
->builder
, bool_type
,
82 assert(num_components
== 1);
87 get_fvec_type(struct ntv_context
*ctx
, unsigned bit_size
, unsigned num_components
)
89 assert(bit_size
== 32); // only 32-bit floats supported so far
91 SpvId float_type
= spirv_builder_type_float(&ctx
->builder
, bit_size
);
92 if (num_components
> 1)
93 return spirv_builder_type_vector(&ctx
->builder
, float_type
,
96 assert(num_components
== 1);
101 get_ivec_type(struct ntv_context
*ctx
, unsigned bit_size
, unsigned num_components
)
103 assert(bit_size
== 32); // only 32-bit ints supported so far
105 SpvId int_type
= spirv_builder_type_int(&ctx
->builder
, bit_size
);
106 if (num_components
> 1)
107 return spirv_builder_type_vector(&ctx
->builder
, int_type
,
110 assert(num_components
== 1);
115 get_uvec_type(struct ntv_context
*ctx
, unsigned bit_size
, unsigned num_components
)
117 assert(bit_size
== 32); // only 32-bit uints supported so far
119 SpvId uint_type
= spirv_builder_type_uint(&ctx
->builder
, bit_size
);
120 if (num_components
> 1)
121 return spirv_builder_type_vector(&ctx
->builder
, uint_type
,
124 assert(num_components
== 1);
129 get_dest_uvec_type(struct ntv_context
*ctx
, nir_dest
*dest
)
131 return get_uvec_type(ctx
, nir_dest_bit_size(*dest
),
132 nir_dest_num_components(*dest
));
136 get_glsl_basetype(struct ntv_context
*ctx
, enum glsl_base_type type
)
139 case GLSL_TYPE_FLOAT
:
140 return spirv_builder_type_float(&ctx
->builder
, 32);
143 return spirv_builder_type_int(&ctx
->builder
, 32);
146 return spirv_builder_type_uint(&ctx
->builder
, 32);
147 /* TODO: handle more types */
150 unreachable("unknown GLSL type");
155 get_glsl_type(struct ntv_context
*ctx
, const struct glsl_type
*type
)
158 if (glsl_type_is_scalar(type
))
159 return get_glsl_basetype(ctx
, glsl_get_base_type(type
));
161 if (glsl_type_is_vector(type
))
162 return spirv_builder_type_vector(&ctx
->builder
,
163 get_glsl_basetype(ctx
, glsl_get_base_type(type
)),
164 glsl_get_vector_elements(type
));
166 unreachable("we shouldn't get here, I think...");
170 emit_input(struct ntv_context
*ctx
, struct nir_variable
*var
)
172 SpvId vec_type
= get_glsl_type(ctx
, var
->type
);
173 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
174 SpvStorageClassInput
,
176 SpvId var_id
= spirv_builder_emit_var(&ctx
->builder
, pointer_type
,
177 SpvStorageClassInput
);
180 spirv_builder_emit_name(&ctx
->builder
, var_id
, var
->name
);
182 if (ctx
->stage
== MESA_SHADER_FRAGMENT
) {
183 switch (var
->data
.location
) {
184 case VARYING_SLOT_POS
:
185 spirv_builder_emit_builtin(&ctx
->builder
, var_id
, SpvBuiltInFragCoord
);
188 case VARYING_SLOT_PNTC
:
189 spirv_builder_emit_builtin(&ctx
->builder
, var_id
, SpvBuiltInPointCoord
);
193 spirv_builder_emit_location(&ctx
->builder
, var_id
,
194 var
->data
.driver_location
);
198 spirv_builder_emit_location(&ctx
->builder
, var_id
,
199 var
->data
.driver_location
);
202 if (var
->data
.location_frac
)
203 spirv_builder_emit_component(&ctx
->builder
, var_id
,
204 var
->data
.location_frac
);
206 if (var
->data
.interpolation
== INTERP_MODE_FLAT
)
207 spirv_builder_emit_decoration(&ctx
->builder
, var_id
, SpvDecorationFlat
);
209 assert(var
->data
.driver_location
< PIPE_MAX_SHADER_INPUTS
);
210 assert(var
->data
.location_frac
< 4);
211 assert(ctx
->inputs
[var
->data
.driver_location
][var
->data
.location_frac
] == 0);
212 ctx
->inputs
[var
->data
.driver_location
][var
->data
.location_frac
] = var_id
;
213 ctx
->input_types
[var
->data
.driver_location
][var
->data
.location_frac
] = vec_type
;
215 assert(ctx
->num_entry_ifaces
< ARRAY_SIZE(ctx
->entry_ifaces
));
216 ctx
->entry_ifaces
[ctx
->num_entry_ifaces
++] = var_id
;
220 emit_output(struct ntv_context
*ctx
, struct nir_variable
*var
)
222 SpvId vec_type
= get_glsl_type(ctx
, var
->type
);
223 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
224 SpvStorageClassOutput
,
226 SpvId var_id
= spirv_builder_emit_var(&ctx
->builder
, pointer_type
,
227 SpvStorageClassOutput
);
229 spirv_builder_emit_name(&ctx
->builder
, var_id
, var
->name
);
232 if (ctx
->stage
== MESA_SHADER_VERTEX
) {
233 switch (var
->data
.location
) {
234 case VARYING_SLOT_POS
:
235 spirv_builder_emit_builtin(&ctx
->builder
, var_id
, SpvBuiltInPosition
);
238 case VARYING_SLOT_PSIZ
:
239 spirv_builder_emit_builtin(&ctx
->builder
, var_id
, SpvBuiltInPointSize
);
243 spirv_builder_emit_location(&ctx
->builder
, var_id
,
244 var
->data
.driver_location
- 1);
246 } else if (ctx
->stage
== MESA_SHADER_FRAGMENT
) {
247 switch (var
->data
.location
) {
248 case FRAG_RESULT_DEPTH
:
249 spirv_builder_emit_builtin(&ctx
->builder
, var_id
, SpvBuiltInFragDepth
);
253 spirv_builder_emit_location(&ctx
->builder
, var_id
,
254 var
->data
.driver_location
);
258 if (var
->data
.location_frac
)
259 spirv_builder_emit_component(&ctx
->builder
, var_id
,
260 var
->data
.location_frac
);
262 assert(var
->data
.driver_location
< PIPE_MAX_SHADER_INPUTS
);
263 assert(var
->data
.location_frac
< 4);
264 assert(ctx
->outputs
[var
->data
.driver_location
][var
->data
.location_frac
] == 0);
265 ctx
->outputs
[var
->data
.driver_location
][var
->data
.location_frac
] = var_id
;
266 ctx
->output_types
[var
->data
.driver_location
][var
->data
.location_frac
] = vec_type
;
268 assert(ctx
->num_entry_ifaces
< ARRAY_SIZE(ctx
->entry_ifaces
));
269 ctx
->entry_ifaces
[ctx
->num_entry_ifaces
++] = var_id
;
273 type_to_dim(enum glsl_sampler_dim gdim
, bool *is_ms
)
277 case GLSL_SAMPLER_DIM_1D
:
279 case GLSL_SAMPLER_DIM_2D
:
281 case GLSL_SAMPLER_DIM_RECT
:
283 case GLSL_SAMPLER_DIM_CUBE
:
285 case GLSL_SAMPLER_DIM_3D
:
287 case GLSL_SAMPLER_DIM_MS
:
291 fprintf(stderr
, "unknown sampler type %d\n", gdim
);
298 emit_sampler(struct ntv_context
*ctx
, struct nir_variable
*var
)
301 SpvDim dimension
= type_to_dim(glsl_get_sampler_dim(var
->type
), &is_ms
);
302 SpvId float_type
= spirv_builder_type_float(&ctx
->builder
, 32);
303 SpvId image_type
= spirv_builder_type_image(&ctx
->builder
, float_type
,
304 dimension
, false, glsl_sampler_type_is_array(var
->type
), is_ms
, 1,
305 SpvImageFormatUnknown
);
307 SpvId sampled_type
= spirv_builder_type_sampled_image(&ctx
->builder
,
309 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
310 SpvStorageClassUniformConstant
,
312 SpvId var_id
= spirv_builder_emit_var(&ctx
->builder
, pointer_type
,
313 SpvStorageClassUniformConstant
);
316 spirv_builder_emit_name(&ctx
->builder
, var_id
, var
->name
);
318 assert(ctx
->num_samplers
< ARRAY_SIZE(ctx
->samplers
));
319 ctx
->samplers
[ctx
->num_samplers
++] = var_id
;
321 spirv_builder_emit_descriptor_set(&ctx
->builder
, var_id
,
322 var
->data
.descriptor_set
);
323 spirv_builder_emit_binding(&ctx
->builder
, var_id
, var
->data
.binding
);
327 emit_ubo(struct ntv_context
*ctx
, struct nir_variable
*var
)
329 uint32_t size
= glsl_count_attribute_slots(var
->type
, false);
330 SpvId vec4_type
= get_uvec_type(ctx
, 32, 4);
331 SpvId array_length
= spirv_builder_const_uint(&ctx
->builder
, 32, size
);
332 SpvId array_type
= spirv_builder_type_array(&ctx
->builder
, vec4_type
,
334 spirv_builder_emit_array_stride(&ctx
->builder
, array_type
, 16);
336 // wrap UBO-array in a struct
337 SpvId struct_type
= spirv_builder_type_struct(&ctx
->builder
, &array_type
, 1);
339 char struct_name
[100];
340 snprintf(struct_name
, sizeof(struct_name
), "struct_%s", var
->name
);
341 spirv_builder_emit_name(&ctx
->builder
, struct_type
, struct_name
);
344 spirv_builder_emit_decoration(&ctx
->builder
, struct_type
,
346 spirv_builder_emit_member_offset(&ctx
->builder
, struct_type
, 0, 0);
349 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
350 SpvStorageClassUniform
,
353 SpvId var_id
= spirv_builder_emit_var(&ctx
->builder
, pointer_type
,
354 SpvStorageClassUniform
);
356 spirv_builder_emit_name(&ctx
->builder
, var_id
, var
->name
);
358 assert(ctx
->num_ubos
< ARRAY_SIZE(ctx
->ubos
));
359 ctx
->ubos
[ctx
->num_ubos
++] = var_id
;
361 spirv_builder_emit_descriptor_set(&ctx
->builder
, var_id
,
362 var
->data
.descriptor_set
);
363 spirv_builder_emit_binding(&ctx
->builder
, var_id
, var
->data
.binding
);
367 emit_uniform(struct ntv_context
*ctx
, struct nir_variable
*var
)
369 if (glsl_type_is_sampler(var
->type
))
370 emit_sampler(ctx
, var
);
371 else if (var
->interface_type
)
376 get_src_uint_ssa(struct ntv_context
*ctx
, const nir_ssa_def
*ssa
)
378 assert(ssa
->index
< ctx
->num_defs
);
379 assert(ctx
->defs
[ssa
->index
] != 0);
380 return ctx
->defs
[ssa
->index
];
384 get_var_from_reg(struct ntv_context
*ctx
, nir_register
*reg
)
386 struct hash_entry
*he
= _mesa_hash_table_search(ctx
->vars
, reg
);
388 SpvId type
= get_uvec_type(ctx
, reg
->bit_size
, reg
->num_components
);
389 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
390 SpvStorageClassFunction
,
393 SpvId var
= spirv_builder_emit_var(&ctx
->builder
, pointer_type
,
394 SpvStorageClassFunction
);
396 he
= _mesa_hash_table_insert(ctx
->vars
, reg
, (void *)(intptr_t)var
);
398 return (SpvId
)(intptr_t)he
->data
;
402 get_src_uint_reg(struct ntv_context
*ctx
, const nir_reg_src
*reg
)
405 assert(!reg
->indirect
);
406 assert(!reg
->base_offset
);
408 SpvId var
= get_var_from_reg(ctx
, reg
->reg
);
409 SpvId type
= get_uvec_type(ctx
, reg
->reg
->bit_size
, reg
->reg
->num_components
);
410 return spirv_builder_emit_load(&ctx
->builder
, type
, var
);
414 get_src_uint(struct ntv_context
*ctx
, nir_src
*src
)
417 return get_src_uint_ssa(ctx
, src
->ssa
);
419 return get_src_uint_reg(ctx
, &src
->reg
);
423 get_alu_src_uint(struct ntv_context
*ctx
, nir_alu_instr
*alu
, unsigned src
)
425 assert(!alu
->src
[src
].negate
);
426 assert(!alu
->src
[src
].abs
);
428 SpvId def
= get_src_uint(ctx
, &alu
->src
[src
].src
);
430 unsigned used_channels
= 0;
431 bool need_swizzle
= false;
432 for (unsigned i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; i
++) {
433 if (!nir_alu_instr_channel_used(alu
, src
, i
))
438 if (alu
->src
[src
].swizzle
[i
] != i
)
441 assert(used_channels
!= 0);
443 unsigned live_channels
= nir_src_num_components(alu
->src
[src
].src
);
444 if (used_channels
!= live_channels
)
450 int bit_size
= nir_src_bit_size(alu
->src
[src
].src
);
452 SpvId uint_type
= spirv_builder_type_uint(&ctx
->builder
, bit_size
);
453 if (used_channels
== 1) {
454 uint32_t indices
[] = { alu
->src
[src
].swizzle
[0] };
455 return spirv_builder_emit_composite_extract(&ctx
->builder
, uint_type
,
457 ARRAY_SIZE(indices
));
458 } else if (live_channels
== 1) {
459 SpvId uvec_type
= spirv_builder_type_vector(&ctx
->builder
, uint_type
,
462 SpvId constituents
[NIR_MAX_VEC_COMPONENTS
];
463 for (unsigned i
= 0; i
< used_channels
; ++i
)
464 constituents
[i
] = def
;
466 return spirv_builder_emit_composite_construct(&ctx
->builder
, uvec_type
,
470 SpvId uvec_type
= spirv_builder_type_vector(&ctx
->builder
, uint_type
,
473 uint32_t components
[NIR_MAX_VEC_COMPONENTS
];
474 size_t num_components
= 0;
475 for (unsigned i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; i
++) {
476 if (!nir_alu_instr_channel_used(alu
, src
, i
))
479 components
[num_components
++] = alu
->src
[src
].swizzle
[i
];
482 return spirv_builder_emit_vector_shuffle(&ctx
->builder
, uvec_type
,
483 def
, def
, components
, num_components
);
488 store_ssa_def_uint(struct ntv_context
*ctx
, nir_ssa_def
*ssa
, SpvId result
)
491 assert(ssa
->index
< ctx
->num_defs
);
492 ctx
->defs
[ssa
->index
] = result
;
496 bvec_to_uvec(struct ntv_context
*ctx
, SpvId value
, unsigned num_components
)
498 SpvId otype
= get_uvec_type(ctx
, 32, num_components
);
499 uint32_t zeros
[4] = { 0, 0, 0, 0 };
500 uint32_t ones
[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff };
501 SpvId zero
= get_uvec_constant(ctx
, 32, num_components
, zeros
);
502 SpvId one
= get_uvec_constant(ctx
, 32, num_components
, ones
);
503 return emit_triop(ctx
, SpvOpSelect
, otype
, value
, one
, zero
);
507 uvec_to_bvec(struct ntv_context
*ctx
, SpvId value
, unsigned num_components
)
509 SpvId type
= get_bvec_type(ctx
, num_components
);
511 uint32_t zeros
[NIR_MAX_VEC_COMPONENTS
] = { 0 };
512 SpvId zero
= get_uvec_constant(ctx
, 32, num_components
, zeros
);
514 return emit_binop(ctx
, SpvOpINotEqual
, type
, value
, zero
);
518 bitcast_to_uvec(struct ntv_context
*ctx
, SpvId value
, unsigned bit_size
,
519 unsigned num_components
)
521 SpvId type
= get_uvec_type(ctx
, bit_size
, num_components
);
522 return emit_unop(ctx
, SpvOpBitcast
, type
, value
);
526 bitcast_to_ivec(struct ntv_context
*ctx
, SpvId value
, unsigned bit_size
,
527 unsigned num_components
)
529 SpvId type
= get_ivec_type(ctx
, bit_size
, num_components
);
530 return emit_unop(ctx
, SpvOpBitcast
, type
, value
);
534 bitcast_to_fvec(struct ntv_context
*ctx
, SpvId value
, unsigned bit_size
,
535 unsigned num_components
)
537 SpvId type
= get_fvec_type(ctx
, bit_size
, num_components
);
538 return emit_unop(ctx
, SpvOpBitcast
, type
, value
);
542 store_reg_def(struct ntv_context
*ctx
, nir_reg_dest
*reg
, SpvId result
)
544 SpvId var
= get_var_from_reg(ctx
, reg
->reg
);
546 spirv_builder_emit_store(&ctx
->builder
, var
, result
);
550 store_dest_uint(struct ntv_context
*ctx
, nir_dest
*dest
, SpvId result
)
553 store_ssa_def_uint(ctx
, &dest
->ssa
, result
);
555 store_reg_def(ctx
, &dest
->reg
, result
);
559 store_dest(struct ntv_context
*ctx
, nir_dest
*dest
, SpvId result
, nir_alu_type type
)
561 unsigned num_components
= nir_dest_num_components(*dest
);
562 unsigned bit_size
= nir_dest_bit_size(*dest
);
564 switch (nir_alu_type_get_base_type(type
)) {
566 assert(bit_size
== 1);
567 result
= bvec_to_uvec(ctx
, result
, num_components
);
571 break; /* nothing to do! */
575 result
= bitcast_to_uvec(ctx
, result
, bit_size
, num_components
);
579 unreachable("unsupported nir_alu_type");
582 store_dest_uint(ctx
, dest
, result
);
586 emit_unop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
, SpvId src
)
588 return spirv_builder_emit_unop(&ctx
->builder
, op
, type
, src
);
592 emit_binop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
,
593 SpvId src0
, SpvId src1
)
595 return spirv_builder_emit_binop(&ctx
->builder
, op
, type
, src0
, src1
);
599 emit_triop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
,
600 SpvId src0
, SpvId src1
, SpvId src2
)
602 return spirv_builder_emit_triop(&ctx
->builder
, op
, type
, src0
, src1
, src2
);
606 emit_builtin_unop(struct ntv_context
*ctx
, enum GLSLstd450 op
, SpvId type
,
609 SpvId args
[] = { src
};
610 return spirv_builder_emit_ext_inst(&ctx
->builder
, type
, ctx
->GLSL_std_450
,
611 op
, args
, ARRAY_SIZE(args
));
615 emit_builtin_binop(struct ntv_context
*ctx
, enum GLSLstd450 op
, SpvId type
,
616 SpvId src0
, SpvId src1
)
618 SpvId args
[] = { src0
, src1
};
619 return spirv_builder_emit_ext_inst(&ctx
->builder
, type
, ctx
->GLSL_std_450
,
620 op
, args
, ARRAY_SIZE(args
));
624 get_fvec_constant(struct ntv_context
*ctx
, int bit_size
, int num_components
,
625 const float values
[])
627 assert(bit_size
== 32);
629 if (num_components
> 1) {
630 SpvId components
[num_components
];
631 for (int i
= 0; i
< num_components
; i
++)
632 components
[i
] = spirv_builder_const_float(&ctx
->builder
, bit_size
,
635 SpvId type
= get_fvec_type(ctx
, bit_size
, num_components
);
636 return spirv_builder_const_composite(&ctx
->builder
, type
, components
,
640 assert(num_components
== 1);
641 return spirv_builder_const_float(&ctx
->builder
, bit_size
, values
[0]);
645 get_uvec_constant(struct ntv_context
*ctx
, int bit_size
, int num_components
,
646 const uint32_t values
[])
648 assert(bit_size
== 32);
650 if (num_components
> 1) {
651 SpvId components
[num_components
];
652 for (int i
= 0; i
< num_components
; i
++)
653 components
[i
] = spirv_builder_const_uint(&ctx
->builder
, bit_size
,
656 SpvId type
= get_uvec_type(ctx
, bit_size
, num_components
);
657 return spirv_builder_const_composite(&ctx
->builder
, type
, components
,
661 assert(num_components
== 1);
662 return spirv_builder_const_uint(&ctx
->builder
, bit_size
, values
[0]);
665 static inline unsigned
666 alu_instr_src_components(const nir_alu_instr
*instr
, unsigned src
)
668 if (nir_op_infos
[instr
->op
].input_sizes
[src
] > 0)
669 return nir_op_infos
[instr
->op
].input_sizes
[src
];
671 if (instr
->dest
.dest
.is_ssa
)
672 return instr
->dest
.dest
.ssa
.num_components
;
674 return instr
->dest
.dest
.reg
.reg
->num_components
;
678 get_alu_src(struct ntv_context
*ctx
, nir_alu_instr
*alu
, unsigned src
)
680 SpvId uint_value
= get_alu_src_uint(ctx
, alu
, src
);
682 unsigned num_components
= alu_instr_src_components(alu
, src
);
683 unsigned bit_size
= nir_src_bit_size(alu
->src
[src
].src
);
684 nir_alu_type type
= nir_op_infos
[alu
->op
].input_types
[src
];
686 switch (nir_alu_type_get_base_type(type
)) {
688 assert(bit_size
== 1);
689 return uvec_to_bvec(ctx
, uint_value
, num_components
);
692 return bitcast_to_ivec(ctx
, uint_value
, bit_size
, num_components
);
698 return bitcast_to_fvec(ctx
, uint_value
, bit_size
, num_components
);
701 unreachable("unknown nir_alu_type");
706 store_alu_result(struct ntv_context
*ctx
, nir_alu_instr
*alu
, SpvId result
)
708 assert(!alu
->dest
.saturate
);
709 return store_dest(ctx
, &alu
->dest
.dest
, result
, nir_op_infos
[alu
->op
].output_type
);
713 get_dest_type(struct ntv_context
*ctx
, nir_dest
*dest
, nir_alu_type type
)
715 unsigned num_components
= nir_dest_num_components(*dest
);
716 unsigned bit_size
= nir_dest_bit_size(*dest
);
718 switch (nir_alu_type_get_base_type(type
)) {
720 return get_bvec_type(ctx
, num_components
);
723 return get_ivec_type(ctx
, bit_size
, num_components
);
726 return get_uvec_type(ctx
, bit_size
, num_components
);
729 return get_fvec_type(ctx
, bit_size
, num_components
);
732 unreachable("unsupported nir_alu_type");
737 emit_alu(struct ntv_context
*ctx
, nir_alu_instr
*alu
)
739 SpvId src
[nir_op_infos
[alu
->op
].num_inputs
];
740 for (unsigned i
= 0; i
< nir_op_infos
[alu
->op
].num_inputs
; i
++)
741 src
[i
] = get_alu_src(ctx
, alu
, i
);
743 SpvId dest_type
= get_dest_type(ctx
, &alu
->dest
.dest
,
744 nir_op_infos
[alu
->op
].output_type
);
745 unsigned bit_size
= nir_dest_bit_size(alu
->dest
.dest
);
746 unsigned num_components
= nir_dest_num_components(alu
->dest
.dest
);
751 assert(nir_op_infos
[alu
->op
].num_inputs
== 1);
755 #define UNOP(nir_op, spirv_op) \
757 assert(nir_op_infos[alu->op].num_inputs == 1); \
758 result = emit_unop(ctx, spirv_op, dest_type, src[0]); \
761 #define BUILTIN_UNOP(nir_op, spirv_op) \
763 assert(nir_op_infos[alu->op].num_inputs == 1); \
764 result = emit_builtin_unop(ctx, spirv_op, dest_type, src[0]); \
767 UNOP(nir_op_fneg
, SpvOpFNegate
)
768 UNOP(nir_op_fddx
, SpvOpDPdx
)
769 UNOP(nir_op_fddy
, SpvOpDPdy
)
771 BUILTIN_UNOP(nir_op_fabs
, GLSLstd450FAbs
)
772 BUILTIN_UNOP(nir_op_fsqrt
, GLSLstd450Sqrt
)
773 BUILTIN_UNOP(nir_op_frsq
, GLSLstd450InverseSqrt
)
774 BUILTIN_UNOP(nir_op_flog2
, GLSLstd450Log2
)
775 BUILTIN_UNOP(nir_op_fexp2
, GLSLstd450Exp2
)
776 BUILTIN_UNOP(nir_op_ffract
, GLSLstd450Fract
)
777 BUILTIN_UNOP(nir_op_ffloor
, GLSLstd450Floor
)
778 BUILTIN_UNOP(nir_op_fceil
, GLSLstd450Ceil
)
779 BUILTIN_UNOP(nir_op_ftrunc
, GLSLstd450Trunc
)
780 BUILTIN_UNOP(nir_op_fround_even
, GLSLstd450RoundEven
)
781 BUILTIN_UNOP(nir_op_fsign
, GLSLstd450FSign
)
782 BUILTIN_UNOP(nir_op_fsin
, GLSLstd450Sin
)
783 BUILTIN_UNOP(nir_op_fcos
, GLSLstd450Cos
)
786 assert(nir_op_infos
[alu
->op
].num_inputs
== 1);
787 float one
[4] = { 1, 1, 1, 1 };
789 src
[0] = get_fvec_constant(ctx
, bit_size
, num_components
, one
);
790 result
= emit_binop(ctx
, SpvOpFDiv
, dest_type
, src
[0], src
[1]);
797 #define BINOP(nir_op, spirv_op) \
799 assert(nir_op_infos[alu->op].num_inputs == 2); \
800 result = emit_binop(ctx, spirv_op, dest_type, src[0], src[1]); \
803 #define BUILTIN_BINOP(nir_op, spirv_op) \
805 assert(nir_op_infos[alu->op].num_inputs == 2); \
806 result = emit_builtin_binop(ctx, spirv_op, dest_type, src[0], src[1]); \
809 BINOP(nir_op_iadd
, SpvOpIAdd
)
810 BINOP(nir_op_isub
, SpvOpISub
)
811 BINOP(nir_op_imul
, SpvOpIMul
)
812 BINOP(nir_op_fadd
, SpvOpFAdd
)
813 BINOP(nir_op_fsub
, SpvOpFSub
)
814 BINOP(nir_op_fmul
, SpvOpFMul
)
815 BINOP(nir_op_flt
, SpvOpFUnordLessThan
)
816 BINOP(nir_op_fge
, SpvOpFUnordGreaterThanEqual
)
818 BUILTIN_BINOP(nir_op_fmin
, GLSLstd450FMin
)
819 BUILTIN_BINOP(nir_op_fmax
, GLSLstd450FMax
)
827 assert(nir_op_infos
[alu
->op
].num_inputs
== 2);
828 result
= emit_binop(ctx
, SpvOpDot
, dest_type
, src
[0], src
[1]);
835 assert(nir_op_infos
[alu
->op
].num_inputs
== 2);
836 int num_components
= nir_dest_num_components(alu
->dest
.dest
);
837 SpvId bool_type
= get_bvec_type(ctx
, num_components
);
839 SpvId zero
= spirv_builder_const_float(&ctx
->builder
, 32, 0.0f
);
840 SpvId one
= spirv_builder_const_float(&ctx
->builder
, 32, 1.0f
);
841 if (num_components
> 1) {
842 SpvId zero_comps
[num_components
], one_comps
[num_components
];
843 for (int i
= 0; i
< num_components
; i
++) {
844 zero_comps
[i
] = zero
;
848 zero
= spirv_builder_const_composite(&ctx
->builder
, dest_type
,
849 zero_comps
, num_components
);
850 one
= spirv_builder_const_composite(&ctx
->builder
, dest_type
,
851 one_comps
, num_components
);
856 case nir_op_seq
: op
= SpvOpFOrdEqual
; break;
857 case nir_op_sne
: op
= SpvOpFOrdNotEqual
; break;
858 case nir_op_slt
: op
= SpvOpFOrdLessThan
; break;
859 case nir_op_sge
: op
= SpvOpFOrdGreaterThanEqual
; break;
860 default: unreachable("unexpected op");
863 result
= emit_binop(ctx
, op
, bool_type
, src
[0], src
[1]);
864 result
= emit_triop(ctx
, SpvOpSelect
, dest_type
, result
, one
, zero
);
869 assert(nir_op_infos
[alu
->op
].num_inputs
== 3);
870 int num_components
= nir_dest_num_components(alu
->dest
.dest
);
871 SpvId bool_type
= get_bvec_type(ctx
, num_components
);
873 float zero
[4] = { 0, 0, 0, 0 };
874 SpvId cmp
= get_fvec_constant(ctx
, nir_src_bit_size(alu
->src
[0].src
),
875 num_components
, zero
);
877 result
= emit_binop(ctx
, SpvOpFOrdGreaterThan
, bool_type
, src
[0], cmp
);
878 result
= emit_triop(ctx
, SpvOpSelect
, dest_type
, result
, src
[1], src
[2]);
885 int num_inputs
= nir_op_infos
[alu
->op
].num_inputs
;
886 assert(2 <= num_inputs
&& num_inputs
<= 4);
887 result
= spirv_builder_emit_composite_construct(&ctx
->builder
, dest_type
,
893 fprintf(stderr
, "emit_alu: not implemented (%s)\n",
894 nir_op_infos
[alu
->op
].name
);
896 unreachable("unsupported opcode");
900 store_alu_result(ctx
, alu
, result
);
904 emit_load_const(struct ntv_context
*ctx
, nir_load_const_instr
*load_const
)
906 uint32_t values
[NIR_MAX_VEC_COMPONENTS
];
907 for (int i
= 0; i
< load_const
->def
.num_components
; ++i
)
908 values
[i
] = load_const
->value
[i
].u32
;
910 SpvId constant
= get_uvec_constant(ctx
, load_const
->def
.bit_size
,
911 load_const
->def
.num_components
,
913 store_ssa_def_uint(ctx
, &load_const
->def
, constant
);
917 emit_load_input(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
919 nir_const_value
*const_offset
= nir_src_as_const_value(intr
->src
[0]);
921 int driver_location
= (int)nir_intrinsic_base(intr
) + const_offset
->u32
;
922 assert(driver_location
< PIPE_MAX_SHADER_INPUTS
);
923 int location_frac
= nir_intrinsic_component(intr
);
924 assert(location_frac
< 4);
926 SpvId ptr
= ctx
->inputs
[driver_location
][location_frac
];
927 SpvId type
= ctx
->input_types
[driver_location
][location_frac
];
930 SpvId result
= spirv_builder_emit_load(&ctx
->builder
, type
, ptr
);
932 unsigned num_components
= nir_dest_num_components(intr
->dest
);
933 unsigned bit_size
= nir_dest_bit_size(intr
->dest
);
934 result
= bitcast_to_uvec(ctx
, result
, bit_size
, num_components
);
936 store_dest_uint(ctx
, &intr
->dest
, result
);
938 unreachable("input-addressing not yet supported");
942 emit_load_ubo(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
944 nir_const_value
*const_block_index
= nir_src_as_const_value(intr
->src
[0]);
945 assert(const_block_index
); // no dynamic indexing for now
946 assert(const_block_index
->u32
== 0); // we only support the default UBO for now
948 nir_const_value
*const_offset
= nir_src_as_const_value(intr
->src
[1]);
950 SpvId uvec4_type
= get_uvec_type(ctx
, 32, 4);
951 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
952 SpvStorageClassUniform
,
955 unsigned idx
= const_offset
->u32
;
956 SpvId member
= spirv_builder_const_uint(&ctx
->builder
, 32, 0);
957 SpvId offset
= spirv_builder_const_uint(&ctx
->builder
, 32, idx
);
958 SpvId offsets
[] = { member
, offset
};
959 SpvId ptr
= spirv_builder_emit_access_chain(&ctx
->builder
, pointer_type
,
960 ctx
->ubos
[0], offsets
,
961 ARRAY_SIZE(offsets
));
962 SpvId result
= spirv_builder_emit_load(&ctx
->builder
, uvec4_type
, ptr
);
964 SpvId type
= get_dest_uvec_type(ctx
, &intr
->dest
);
965 unsigned num_components
= nir_dest_num_components(intr
->dest
);
966 if (num_components
== 1) {
967 uint32_t components
[] = { 0 };
968 result
= spirv_builder_emit_composite_extract(&ctx
->builder
,
972 } else if (num_components
< 4) {
973 SpvId constituents
[num_components
];
974 SpvId uint_type
= spirv_builder_type_uint(&ctx
->builder
, 32);
975 for (uint32_t i
= 0; i
< num_components
; ++i
)
976 constituents
[i
] = spirv_builder_emit_composite_extract(&ctx
->builder
,
981 result
= spirv_builder_emit_composite_construct(&ctx
->builder
,
987 store_dest_uint(ctx
, &intr
->dest
, result
);
989 unreachable("uniform-addressing not yet supported");
993 emit_store_output(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
995 nir_const_value
*const_offset
= nir_src_as_const_value(intr
->src
[1]);
997 int driver_location
= (int)nir_intrinsic_base(intr
) + const_offset
->u32
;
998 assert(driver_location
< PIPE_MAX_SHADER_OUTPUTS
);
999 int location_frac
= nir_intrinsic_component(intr
);
1000 assert(location_frac
< 4);
1002 SpvId ptr
= ctx
->outputs
[driver_location
][location_frac
];
1005 SpvId src
= get_src_uint(ctx
, &intr
->src
[0]);
1006 SpvId spirv_type
= ctx
->output_types
[driver_location
][location_frac
];
1007 SpvId result
= emit_unop(ctx
, SpvOpBitcast
, spirv_type
, src
);
1008 spirv_builder_emit_store(&ctx
->builder
, ptr
, result
);
1010 unreachable("output-addressing not yet supported");
1014 emit_intrinsic(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
1016 switch (intr
->intrinsic
) {
1017 case nir_intrinsic_load_input
:
1018 emit_load_input(ctx
, intr
);
1021 case nir_intrinsic_load_ubo
:
1022 emit_load_ubo(ctx
, intr
);
1025 case nir_intrinsic_store_output
:
1026 emit_store_output(ctx
, intr
);
1030 fprintf(stderr
, "emit_intrinsic: not implemented (%s)\n",
1031 nir_intrinsic_infos
[intr
->intrinsic
].name
);
1032 unreachable("unsupported intrinsic");
1037 emit_undef(struct ntv_context
*ctx
, nir_ssa_undef_instr
*undef
)
1039 SpvId type
= get_fvec_type(ctx
, undef
->def
.bit_size
,
1040 undef
->def
.num_components
);
1042 store_ssa_def_uint(ctx
, &undef
->def
,
1043 spirv_builder_emit_undef(&ctx
->builder
, type
));
1047 get_src_float(struct ntv_context
*ctx
, nir_src
*src
)
1049 SpvId def
= get_src_uint(ctx
, src
);
1050 unsigned num_components
= nir_src_num_components(*src
);
1051 unsigned bit_size
= nir_src_bit_size(*src
);
1052 return bitcast_to_fvec(ctx
, def
, bit_size
, num_components
);
1056 emit_tex(struct ntv_context
*ctx
, nir_tex_instr
*tex
)
1058 assert(tex
->op
== nir_texop_tex
);
1059 assert(nir_alu_type_get_base_type(tex
->dest_type
) == nir_type_float
);
1060 assert(tex
->texture_index
== tex
->sampler_index
);
1062 bool has_proj
= false;
1063 SpvId coord
= 0, proj
;
1064 unsigned coord_components
;
1065 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
1066 switch (tex
->src
[i
].src_type
) {
1067 case nir_tex_src_coord
:
1068 coord
= get_src_float(ctx
, &tex
->src
[i
].src
);
1069 coord_components
= nir_src_num_components(tex
->src
[i
].src
);
1072 case nir_tex_src_projector
:
1074 proj
= get_src_float(ctx
, &tex
->src
[i
].src
);
1075 assert(nir_src_num_components(tex
->src
[i
].src
) == 1);
1079 fprintf(stderr
, "texture source: %d\n", tex
->src
[i
].src_type
);
1080 unreachable("unknown texture source");
1085 SpvDim dimension
= type_to_dim(tex
->sampler_dim
, &is_ms
);
1086 SpvId float_type
= spirv_builder_type_float(&ctx
->builder
, 32);
1087 SpvId image_type
= spirv_builder_type_image(&ctx
->builder
, float_type
,
1088 dimension
, false, tex
->is_array
, is_ms
, 1,
1089 SpvImageFormatUnknown
);
1090 SpvId sampled_type
= spirv_builder_type_sampled_image(&ctx
->builder
,
1093 assert(tex
->texture_index
< ctx
->num_samplers
);
1094 SpvId load
= spirv_builder_emit_load(&ctx
->builder
, sampled_type
,
1095 ctx
->samplers
[tex
->texture_index
]);
1097 SpvId dest_type
= get_dest_type(ctx
, &tex
->dest
, tex
->dest_type
);
1101 SpvId constituents
[coord_components
+ 1];
1102 SpvId float_type
= spirv_builder_type_float(&ctx
->builder
, 32);
1103 for (uint32_t i
= 0; i
< coord_components
; ++i
)
1104 constituents
[i
] = spirv_builder_emit_composite_extract(&ctx
->builder
,
1109 constituents
[coord_components
++] = proj
;
1111 SpvId vec_type
= get_fvec_type(ctx
, 32, coord_components
);
1112 SpvId merged
= spirv_builder_emit_composite_construct(&ctx
->builder
,
1117 result
= spirv_builder_emit_image_sample_proj_implicit_lod(&ctx
->builder
,
1122 result
= spirv_builder_emit_image_sample_implicit_lod(&ctx
->builder
,
1125 spirv_builder_emit_decoration(&ctx
->builder
, result
,
1126 SpvDecorationRelaxedPrecision
);
1128 store_dest(ctx
, &tex
->dest
, result
, tex
->dest_type
);
1132 emit_block(struct ntv_context
*ctx
, struct nir_block
*block
)
1134 nir_foreach_instr(instr
, block
) {
1135 switch (instr
->type
) {
1136 case nir_instr_type_alu
:
1137 emit_alu(ctx
, nir_instr_as_alu(instr
));
1139 case nir_instr_type_intrinsic
:
1140 emit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
1142 case nir_instr_type_load_const
:
1143 emit_load_const(ctx
, nir_instr_as_load_const(instr
));
1145 case nir_instr_type_ssa_undef
:
1146 emit_undef(ctx
, nir_instr_as_ssa_undef(instr
));
1148 case nir_instr_type_tex
:
1149 emit_tex(ctx
, nir_instr_as_tex(instr
));
1151 case nir_instr_type_phi
:
1152 unreachable("nir_instr_type_phi not supported");
1154 case nir_instr_type_jump
:
1155 unreachable("nir_instr_type_jump not supported");
1157 case nir_instr_type_call
:
1158 unreachable("nir_instr_type_call not supported");
1160 case nir_instr_type_parallel_copy
:
1161 unreachable("nir_instr_type_parallel_copy not supported");
1163 case nir_instr_type_deref
:
1164 unreachable("nir_instr_type_deref not supported");
1171 emit_cf_list(struct ntv_context
*ctx
, struct exec_list
*list
)
1173 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
1174 switch (node
->type
) {
1175 case nir_cf_node_block
:
1176 emit_block(ctx
, nir_cf_node_as_block(node
));
1179 case nir_cf_node_if
:
1180 unreachable("nir_cf_node_if not supported");
1183 case nir_cf_node_loop
:
1184 unreachable("nir_cf_node_loop not supported");
1187 case nir_cf_node_function
:
1188 unreachable("nir_cf_node_function not supported");
1194 struct spirv_shader
*
1195 nir_to_spirv(struct nir_shader
*s
)
1197 struct spirv_shader
*ret
= NULL
;
1199 struct ntv_context ctx
= {};
1201 switch (s
->info
.stage
) {
1202 case MESA_SHADER_VERTEX
:
1203 case MESA_SHADER_FRAGMENT
:
1204 case MESA_SHADER_COMPUTE
:
1205 spirv_builder_emit_cap(&ctx
.builder
, SpvCapabilityShader
);
1208 case MESA_SHADER_TESS_CTRL
:
1209 case MESA_SHADER_TESS_EVAL
:
1210 spirv_builder_emit_cap(&ctx
.builder
, SpvCapabilityTessellation
);
1213 case MESA_SHADER_GEOMETRY
:
1214 spirv_builder_emit_cap(&ctx
.builder
, SpvCapabilityGeometry
);
1218 unreachable("invalid stage");
1221 ctx
.stage
= s
->info
.stage
;
1222 ctx
.GLSL_std_450
= spirv_builder_import(&ctx
.builder
, "GLSL.std.450");
1223 spirv_builder_emit_source(&ctx
.builder
, SpvSourceLanguageGLSL
, 450);
1225 spirv_builder_emit_mem_model(&ctx
.builder
, SpvAddressingModelLogical
,
1226 SpvMemoryModelGLSL450
);
1228 SpvExecutionModel exec_model
;
1229 switch (s
->info
.stage
) {
1230 case MESA_SHADER_VERTEX
:
1231 exec_model
= SpvExecutionModelVertex
;
1233 case MESA_SHADER_TESS_CTRL
:
1234 exec_model
= SpvExecutionModelTessellationControl
;
1236 case MESA_SHADER_TESS_EVAL
:
1237 exec_model
= SpvExecutionModelTessellationEvaluation
;
1239 case MESA_SHADER_GEOMETRY
:
1240 exec_model
= SpvExecutionModelGeometry
;
1242 case MESA_SHADER_FRAGMENT
:
1243 exec_model
= SpvExecutionModelFragment
;
1245 case MESA_SHADER_COMPUTE
:
1246 exec_model
= SpvExecutionModelGLCompute
;
1249 unreachable("invalid stage");
1252 SpvId type_void
= spirv_builder_type_void(&ctx
.builder
);
1253 SpvId type_main
= spirv_builder_type_function(&ctx
.builder
, type_void
,
1255 SpvId entry_point
= spirv_builder_new_id(&ctx
.builder
);
1256 SpvId label
= spirv_builder_new_id(&ctx
.builder
);
1257 spirv_builder_emit_name(&ctx
.builder
, entry_point
, "main");
1259 nir_foreach_variable(var
, &s
->inputs
)
1260 emit_input(&ctx
, var
);
1262 nir_foreach_variable(var
, &s
->outputs
)
1263 emit_output(&ctx
, var
);
1265 nir_foreach_variable(var
, &s
->uniforms
)
1266 emit_uniform(&ctx
, var
);
1268 spirv_builder_emit_entry_point(&ctx
.builder
, exec_model
, entry_point
,
1269 "main", ctx
.entry_ifaces
,
1270 ctx
.num_entry_ifaces
);
1271 if (s
->info
.stage
== MESA_SHADER_FRAGMENT
)
1272 spirv_builder_emit_exec_mode(&ctx
.builder
, entry_point
,
1273 SpvExecutionModeOriginUpperLeft
);
1276 spirv_builder_function(&ctx
.builder
, entry_point
, type_void
,
1277 SpvFunctionControlMaskNone
,
1279 spirv_builder_label(&ctx
.builder
, label
);
1281 nir_function_impl
*entry
= nir_shader_get_entrypoint(s
);
1283 ctx
.defs
= (SpvId
*)malloc(sizeof(SpvId
) * entry
->ssa_alloc
);
1286 ctx
.num_defs
= entry
->ssa_alloc
;
1288 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
1289 _mesa_key_pointer_equal
);
1293 emit_cf_list(&ctx
, &entry
->body
);
1296 spirv_builder_return(&ctx
.builder
); // doesn't belong here, but whatevz
1297 spirv_builder_function_end(&ctx
.builder
);
1299 size_t num_words
= spirv_builder_get_num_words(&ctx
.builder
);
1301 ret
= CALLOC_STRUCT(spirv_shader
);
1305 ret
->words
= MALLOC(sizeof(uint32_t) * num_words
);
1309 ret
->num_words
= spirv_builder_get_words(&ctx
.builder
, ret
->words
, num_words
);
1310 assert(ret
->num_words
== num_words
);
1317 spirv_shader_delete(ret
);
1323 spirv_shader_delete(struct spirv_shader
*s
)