2 * Copyright 2018 Collabora Ltd.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
24 #include "nir_to_spirv.h"
25 #include "spirv_builder.h"
28 #include "pipe/p_state.h"
29 #include "util/u_memory.h"
30 #include "util/hash_table.h"
33 struct spirv_builder builder
;
37 gl_shader_stage stage
;
41 SpvId samplers
[PIPE_MAX_SAMPLERS
];
43 SpvId entry_ifaces
[PIPE_MAX_SHADER_INPUTS
* 4 + PIPE_MAX_SHADER_OUTPUTS
* 4];
44 size_t num_entry_ifaces
;
52 struct hash_table
*vars
; /* nir_variable -> SpvId */
54 const SpvId
*block_ids
;
57 SpvId loop_break
, loop_cont
;
63 get_fvec_constant(struct ntv_context
*ctx
, unsigned bit_size
,
64 unsigned num_components
, float value
);
67 get_uvec_constant(struct ntv_context
*ctx
, unsigned bit_size
,
68 unsigned num_components
, uint32_t value
);
71 get_ivec_constant(struct ntv_context
*ctx
, unsigned bit_size
,
72 unsigned num_components
, int32_t value
);
75 emit_unop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
, SpvId src
);
78 emit_binop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
,
79 SpvId src0
, SpvId src1
);
82 emit_triop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
,
83 SpvId src0
, SpvId src1
, SpvId src2
);
86 get_bvec_type(struct ntv_context
*ctx
, int num_components
)
88 SpvId bool_type
= spirv_builder_type_bool(&ctx
->builder
);
89 if (num_components
> 1)
90 return spirv_builder_type_vector(&ctx
->builder
, bool_type
,
93 assert(num_components
== 1);
98 block_label(struct ntv_context
*ctx
, nir_block
*block
)
100 assert(block
->index
< ctx
->num_blocks
);
101 return ctx
->block_ids
[block
->index
];
105 emit_float_const(struct ntv_context
*ctx
, int bit_size
, float value
)
107 assert(bit_size
== 32);
108 return spirv_builder_const_float(&ctx
->builder
, bit_size
, value
);
112 emit_uint_const(struct ntv_context
*ctx
, int bit_size
, uint32_t value
)
114 assert(bit_size
== 32);
115 return spirv_builder_const_uint(&ctx
->builder
, bit_size
, value
);
119 emit_int_const(struct ntv_context
*ctx
, int bit_size
, int32_t value
)
121 assert(bit_size
== 32);
122 return spirv_builder_const_int(&ctx
->builder
, bit_size
, value
);
126 get_fvec_type(struct ntv_context
*ctx
, unsigned bit_size
, unsigned num_components
)
128 assert(bit_size
== 32); // only 32-bit floats supported so far
130 SpvId float_type
= spirv_builder_type_float(&ctx
->builder
, bit_size
);
131 if (num_components
> 1)
132 return spirv_builder_type_vector(&ctx
->builder
, float_type
,
135 assert(num_components
== 1);
140 get_ivec_type(struct ntv_context
*ctx
, unsigned bit_size
, unsigned num_components
)
142 assert(bit_size
== 1 || bit_size
== 32); // only 32-bit ints supported so far
144 SpvId int_type
= spirv_builder_type_int(&ctx
->builder
, MAX2(bit_size
, 32));
145 if (num_components
> 1)
146 return spirv_builder_type_vector(&ctx
->builder
, int_type
,
149 assert(num_components
== 1);
154 get_uvec_type(struct ntv_context
*ctx
, unsigned bit_size
, unsigned num_components
)
156 assert(bit_size
== 1 || bit_size
== 32); // only 32-bit uints supported so far
158 SpvId uint_type
= spirv_builder_type_uint(&ctx
->builder
, MAX2(bit_size
, 32));
159 if (num_components
> 1)
160 return spirv_builder_type_vector(&ctx
->builder
, uint_type
,
163 assert(num_components
== 1);
168 get_dest_uvec_type(struct ntv_context
*ctx
, nir_dest
*dest
)
170 return get_uvec_type(ctx
, nir_dest_bit_size(*dest
),
171 nir_dest_num_components(*dest
));
175 get_glsl_basetype(struct ntv_context
*ctx
, enum glsl_base_type type
)
179 return spirv_builder_type_bool(&ctx
->builder
);
181 case GLSL_TYPE_FLOAT
:
182 return spirv_builder_type_float(&ctx
->builder
, 32);
185 return spirv_builder_type_int(&ctx
->builder
, 32);
188 return spirv_builder_type_uint(&ctx
->builder
, 32);
189 /* TODO: handle more types */
192 unreachable("unknown GLSL type");
197 get_glsl_type(struct ntv_context
*ctx
, const struct glsl_type
*type
)
200 if (glsl_type_is_scalar(type
))
201 return get_glsl_basetype(ctx
, glsl_get_base_type(type
));
203 if (glsl_type_is_vector(type
))
204 return spirv_builder_type_vector(&ctx
->builder
,
205 get_glsl_basetype(ctx
, glsl_get_base_type(type
)),
206 glsl_get_vector_elements(type
));
208 if (glsl_type_is_array(type
)) {
209 SpvId ret
= spirv_builder_type_array(&ctx
->builder
,
210 get_glsl_type(ctx
, glsl_get_array_element(type
)),
211 emit_uint_const(ctx
, 32, glsl_get_length(type
)));
212 uint32_t stride
= glsl_get_explicit_stride(type
);
214 spirv_builder_emit_array_stride(&ctx
->builder
, ret
, stride
);
219 unreachable("we shouldn't get here, I think...");
223 emit_input(struct ntv_context
*ctx
, struct nir_variable
*var
)
225 SpvId var_type
= get_glsl_type(ctx
, var
->type
);
226 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
227 SpvStorageClassInput
,
229 SpvId var_id
= spirv_builder_emit_var(&ctx
->builder
, pointer_type
,
230 SpvStorageClassInput
);
233 spirv_builder_emit_name(&ctx
->builder
, var_id
, var
->name
);
235 if (ctx
->stage
== MESA_SHADER_FRAGMENT
) {
236 if (var
->data
.location
>= VARYING_SLOT_VAR0
)
237 spirv_builder_emit_location(&ctx
->builder
, var_id
,
241 else if ((var
->data
.location
>= VARYING_SLOT_COL0
&&
242 var
->data
.location
<= VARYING_SLOT_TEX7
) ||
243 var
->data
.location
== VARYING_SLOT_BFC0
||
244 var
->data
.location
== VARYING_SLOT_BFC1
) {
245 spirv_builder_emit_location(&ctx
->builder
, var_id
,
248 switch (var
->data
.location
) {
249 case VARYING_SLOT_POS
:
250 spirv_builder_emit_builtin(&ctx
->builder
, var_id
, SpvBuiltInFragCoord
);
253 case VARYING_SLOT_PNTC
:
254 spirv_builder_emit_builtin(&ctx
->builder
, var_id
, SpvBuiltInPointCoord
);
258 debug_printf("unknown varying slot: %s\n", gl_varying_slot_name(var
->data
.location
));
259 unreachable("unexpected varying slot");
263 spirv_builder_emit_location(&ctx
->builder
, var_id
,
264 var
->data
.driver_location
);
267 if (var
->data
.location_frac
)
268 spirv_builder_emit_component(&ctx
->builder
, var_id
,
269 var
->data
.location_frac
);
271 if (var
->data
.interpolation
== INTERP_MODE_FLAT
)
272 spirv_builder_emit_decoration(&ctx
->builder
, var_id
, SpvDecorationFlat
);
274 _mesa_hash_table_insert(ctx
->vars
, var
, (void *)(intptr_t)var_id
);
276 assert(ctx
->num_entry_ifaces
< ARRAY_SIZE(ctx
->entry_ifaces
));
277 ctx
->entry_ifaces
[ctx
->num_entry_ifaces
++] = var_id
;
281 emit_output(struct ntv_context
*ctx
, struct nir_variable
*var
)
283 SpvId var_type
= get_glsl_type(ctx
, var
->type
);
284 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
285 SpvStorageClassOutput
,
287 SpvId var_id
= spirv_builder_emit_var(&ctx
->builder
, pointer_type
,
288 SpvStorageClassOutput
);
290 spirv_builder_emit_name(&ctx
->builder
, var_id
, var
->name
);
293 if (ctx
->stage
== MESA_SHADER_VERTEX
) {
294 if (var
->data
.location
>= VARYING_SLOT_VAR0
)
295 spirv_builder_emit_location(&ctx
->builder
, var_id
,
299 else if ((var
->data
.location
>= VARYING_SLOT_COL0
&&
300 var
->data
.location
<= VARYING_SLOT_TEX7
) ||
301 var
->data
.location
== VARYING_SLOT_BFC0
||
302 var
->data
.location
== VARYING_SLOT_BFC1
) {
303 spirv_builder_emit_location(&ctx
->builder
, var_id
,
306 switch (var
->data
.location
) {
307 case VARYING_SLOT_POS
:
308 spirv_builder_emit_builtin(&ctx
->builder
, var_id
, SpvBuiltInPosition
);
311 case VARYING_SLOT_PSIZ
:
312 spirv_builder_emit_builtin(&ctx
->builder
, var_id
, SpvBuiltInPointSize
);
315 case VARYING_SLOT_CLIP_DIST0
:
316 assert(glsl_type_is_array(var
->type
));
317 spirv_builder_emit_builtin(&ctx
->builder
, var_id
, SpvBuiltInClipDistance
);
321 debug_printf("unknown varying slot: %s\n", gl_varying_slot_name(var
->data
.location
));
322 unreachable("unexpected varying slot");
325 } else if (ctx
->stage
== MESA_SHADER_FRAGMENT
) {
326 if (var
->data
.location
>= FRAG_RESULT_DATA0
)
327 spirv_builder_emit_location(&ctx
->builder
, var_id
,
328 var
->data
.location
- FRAG_RESULT_DATA0
);
330 switch (var
->data
.location
) {
331 case FRAG_RESULT_COLOR
:
332 spirv_builder_emit_location(&ctx
->builder
, var_id
, 0);
335 case FRAG_RESULT_DEPTH
:
336 spirv_builder_emit_builtin(&ctx
->builder
, var_id
, SpvBuiltInFragDepth
);
340 spirv_builder_emit_location(&ctx
->builder
, var_id
,
341 var
->data
.driver_location
);
346 if (var
->data
.location_frac
)
347 spirv_builder_emit_component(&ctx
->builder
, var_id
,
348 var
->data
.location_frac
);
350 _mesa_hash_table_insert(ctx
->vars
, var
, (void *)(intptr_t)var_id
);
352 assert(ctx
->num_entry_ifaces
< ARRAY_SIZE(ctx
->entry_ifaces
));
353 ctx
->entry_ifaces
[ctx
->num_entry_ifaces
++] = var_id
;
357 type_to_dim(enum glsl_sampler_dim gdim
, bool *is_ms
)
361 case GLSL_SAMPLER_DIM_1D
:
363 case GLSL_SAMPLER_DIM_2D
:
365 case GLSL_SAMPLER_DIM_3D
:
367 case GLSL_SAMPLER_DIM_CUBE
:
369 case GLSL_SAMPLER_DIM_RECT
:
371 case GLSL_SAMPLER_DIM_BUF
:
373 case GLSL_SAMPLER_DIM_EXTERNAL
:
374 return SpvDim2D
; /* seems dodgy... */
375 case GLSL_SAMPLER_DIM_MS
:
379 fprintf(stderr
, "unknown sampler type %d\n", gdim
);
386 emit_sampler(struct ntv_context
*ctx
, struct nir_variable
*var
)
389 SpvDim dimension
= type_to_dim(glsl_get_sampler_dim(var
->type
), &is_ms
);
390 SpvId float_type
= spirv_builder_type_float(&ctx
->builder
, 32);
391 SpvId image_type
= spirv_builder_type_image(&ctx
->builder
, float_type
,
392 dimension
, false, glsl_sampler_type_is_array(var
->type
), is_ms
, 1,
393 SpvImageFormatUnknown
);
395 SpvId sampled_type
= spirv_builder_type_sampled_image(&ctx
->builder
,
397 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
398 SpvStorageClassUniformConstant
,
400 SpvId var_id
= spirv_builder_emit_var(&ctx
->builder
, pointer_type
,
401 SpvStorageClassUniformConstant
);
404 spirv_builder_emit_name(&ctx
->builder
, var_id
, var
->name
);
406 assert(ctx
->num_samplers
< ARRAY_SIZE(ctx
->samplers
));
407 ctx
->samplers
[ctx
->num_samplers
++] = var_id
;
409 spirv_builder_emit_descriptor_set(&ctx
->builder
, var_id
,
410 var
->data
.descriptor_set
);
411 spirv_builder_emit_binding(&ctx
->builder
, var_id
, var
->data
.binding
);
415 emit_ubo(struct ntv_context
*ctx
, struct nir_variable
*var
)
417 uint32_t size
= glsl_count_attribute_slots(var
->type
, false);
418 SpvId vec4_type
= get_uvec_type(ctx
, 32, 4);
419 SpvId array_length
= emit_uint_const(ctx
, 32, size
);
420 SpvId array_type
= spirv_builder_type_array(&ctx
->builder
, vec4_type
,
422 spirv_builder_emit_array_stride(&ctx
->builder
, array_type
, 16);
424 // wrap UBO-array in a struct
425 SpvId struct_type
= spirv_builder_type_struct(&ctx
->builder
, &array_type
, 1);
427 char struct_name
[100];
428 snprintf(struct_name
, sizeof(struct_name
), "struct_%s", var
->name
);
429 spirv_builder_emit_name(&ctx
->builder
, struct_type
, struct_name
);
432 spirv_builder_emit_decoration(&ctx
->builder
, struct_type
,
434 spirv_builder_emit_member_offset(&ctx
->builder
, struct_type
, 0, 0);
437 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
438 SpvStorageClassUniform
,
441 SpvId var_id
= spirv_builder_emit_var(&ctx
->builder
, pointer_type
,
442 SpvStorageClassUniform
);
444 spirv_builder_emit_name(&ctx
->builder
, var_id
, var
->name
);
446 assert(ctx
->num_ubos
< ARRAY_SIZE(ctx
->ubos
));
447 ctx
->ubos
[ctx
->num_ubos
++] = var_id
;
449 spirv_builder_emit_descriptor_set(&ctx
->builder
, var_id
,
450 var
->data
.descriptor_set
);
451 spirv_builder_emit_binding(&ctx
->builder
, var_id
, var
->data
.binding
);
455 emit_uniform(struct ntv_context
*ctx
, struct nir_variable
*var
)
457 if (var
->data
.mode
== nir_var_mem_ubo
)
460 assert(var
->data
.mode
== nir_var_uniform
);
461 if (glsl_type_is_sampler(var
->type
))
462 emit_sampler(ctx
, var
);
467 get_src_uint_ssa(struct ntv_context
*ctx
, const nir_ssa_def
*ssa
)
469 assert(ssa
->index
< ctx
->num_defs
);
470 assert(ctx
->defs
[ssa
->index
] != 0);
471 return ctx
->defs
[ssa
->index
];
475 get_var_from_reg(struct ntv_context
*ctx
, nir_register
*reg
)
477 assert(reg
->index
< ctx
->num_regs
);
478 assert(ctx
->regs
[reg
->index
] != 0);
479 return ctx
->regs
[reg
->index
];
483 get_src_uint_reg(struct ntv_context
*ctx
, const nir_reg_src
*reg
)
486 assert(!reg
->indirect
);
487 assert(!reg
->base_offset
);
489 SpvId var
= get_var_from_reg(ctx
, reg
->reg
);
490 SpvId type
= get_uvec_type(ctx
, reg
->reg
->bit_size
, reg
->reg
->num_components
);
491 return spirv_builder_emit_load(&ctx
->builder
, type
, var
);
495 get_src_uint(struct ntv_context
*ctx
, nir_src
*src
)
498 return get_src_uint_ssa(ctx
, src
->ssa
);
500 return get_src_uint_reg(ctx
, &src
->reg
);
504 get_alu_src_uint(struct ntv_context
*ctx
, nir_alu_instr
*alu
, unsigned src
)
506 assert(!alu
->src
[src
].negate
);
507 assert(!alu
->src
[src
].abs
);
509 SpvId def
= get_src_uint(ctx
, &alu
->src
[src
].src
);
511 unsigned used_channels
= 0;
512 bool need_swizzle
= false;
513 for (unsigned i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; i
++) {
514 if (!nir_alu_instr_channel_used(alu
, src
, i
))
519 if (alu
->src
[src
].swizzle
[i
] != i
)
522 assert(used_channels
!= 0);
524 unsigned live_channels
= nir_src_num_components(alu
->src
[src
].src
);
525 if (used_channels
!= live_channels
)
531 int bit_size
= nir_src_bit_size(alu
->src
[src
].src
);
532 assert(bit_size
== 1 || bit_size
== 32);
534 SpvId uint_type
= spirv_builder_type_uint(&ctx
->builder
, MAX2(bit_size
, 32));
535 if (used_channels
== 1) {
536 uint32_t indices
[] = { alu
->src
[src
].swizzle
[0] };
537 return spirv_builder_emit_composite_extract(&ctx
->builder
, uint_type
,
539 ARRAY_SIZE(indices
));
540 } else if (live_channels
== 1) {
541 SpvId uvec_type
= spirv_builder_type_vector(&ctx
->builder
, uint_type
,
544 SpvId constituents
[NIR_MAX_VEC_COMPONENTS
];
545 for (unsigned i
= 0; i
< used_channels
; ++i
)
546 constituents
[i
] = def
;
548 return spirv_builder_emit_composite_construct(&ctx
->builder
, uvec_type
,
552 SpvId uvec_type
= spirv_builder_type_vector(&ctx
->builder
, uint_type
,
555 uint32_t components
[NIR_MAX_VEC_COMPONENTS
];
556 size_t num_components
= 0;
557 for (unsigned i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; i
++) {
558 if (!nir_alu_instr_channel_used(alu
, src
, i
))
561 components
[num_components
++] = alu
->src
[src
].swizzle
[i
];
564 return spirv_builder_emit_vector_shuffle(&ctx
->builder
, uvec_type
,
565 def
, def
, components
, num_components
);
570 store_ssa_def_uint(struct ntv_context
*ctx
, nir_ssa_def
*ssa
, SpvId result
)
573 assert(ssa
->index
< ctx
->num_defs
);
574 ctx
->defs
[ssa
->index
] = result
;
578 emit_select(struct ntv_context
*ctx
, SpvId type
, SpvId cond
,
579 SpvId if_true
, SpvId if_false
)
581 return emit_triop(ctx
, SpvOpSelect
, type
, cond
, if_true
, if_false
);
585 bvec_to_uvec(struct ntv_context
*ctx
, SpvId value
, unsigned num_components
)
587 SpvId otype
= get_uvec_type(ctx
, 32, num_components
);
588 SpvId zero
= get_uvec_constant(ctx
, 32, num_components
, 0);
589 SpvId one
= get_uvec_constant(ctx
, 32, num_components
, UINT32_MAX
);
590 return emit_select(ctx
, otype
, value
, one
, zero
);
594 uvec_to_bvec(struct ntv_context
*ctx
, SpvId value
, unsigned num_components
)
596 SpvId type
= get_bvec_type(ctx
, num_components
);
597 SpvId zero
= get_uvec_constant(ctx
, 32, num_components
, 0);
598 return emit_binop(ctx
, SpvOpINotEqual
, type
, value
, zero
);
602 emit_bitcast(struct ntv_context
*ctx
, SpvId type
, SpvId value
)
604 return emit_unop(ctx
, SpvOpBitcast
, type
, value
);
608 bitcast_to_uvec(struct ntv_context
*ctx
, SpvId value
, unsigned bit_size
,
609 unsigned num_components
)
611 SpvId type
= get_uvec_type(ctx
, bit_size
, num_components
);
612 return emit_bitcast(ctx
, type
, value
);
616 bitcast_to_ivec(struct ntv_context
*ctx
, SpvId value
, unsigned bit_size
,
617 unsigned num_components
)
619 SpvId type
= get_ivec_type(ctx
, bit_size
, num_components
);
620 return emit_bitcast(ctx
, type
, value
);
624 bitcast_to_fvec(struct ntv_context
*ctx
, SpvId value
, unsigned bit_size
,
625 unsigned num_components
)
627 SpvId type
= get_fvec_type(ctx
, bit_size
, num_components
);
628 return emit_bitcast(ctx
, type
, value
);
632 store_reg_def(struct ntv_context
*ctx
, nir_reg_dest
*reg
, SpvId result
)
634 SpvId var
= get_var_from_reg(ctx
, reg
->reg
);
636 spirv_builder_emit_store(&ctx
->builder
, var
, result
);
640 store_dest_uint(struct ntv_context
*ctx
, nir_dest
*dest
, SpvId result
)
643 store_ssa_def_uint(ctx
, &dest
->ssa
, result
);
645 store_reg_def(ctx
, &dest
->reg
, result
);
649 store_dest(struct ntv_context
*ctx
, nir_dest
*dest
, SpvId result
, nir_alu_type type
)
651 unsigned num_components
= nir_dest_num_components(*dest
);
652 unsigned bit_size
= nir_dest_bit_size(*dest
);
654 switch (nir_alu_type_get_base_type(type
)) {
656 assert(bit_size
== 1);
657 result
= bvec_to_uvec(ctx
, result
, num_components
);
661 break; /* nothing to do! */
665 result
= bitcast_to_uvec(ctx
, result
, bit_size
, num_components
);
669 unreachable("unsupported nir_alu_type");
672 store_dest_uint(ctx
, dest
, result
);
676 emit_unop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
, SpvId src
)
678 return spirv_builder_emit_unop(&ctx
->builder
, op
, type
, src
);
682 emit_binop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
,
683 SpvId src0
, SpvId src1
)
685 return spirv_builder_emit_binop(&ctx
->builder
, op
, type
, src0
, src1
);
689 emit_triop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
,
690 SpvId src0
, SpvId src1
, SpvId src2
)
692 return spirv_builder_emit_triop(&ctx
->builder
, op
, type
, src0
, src1
, src2
);
696 emit_builtin_unop(struct ntv_context
*ctx
, enum GLSLstd450 op
, SpvId type
,
699 SpvId args
[] = { src
};
700 return spirv_builder_emit_ext_inst(&ctx
->builder
, type
, ctx
->GLSL_std_450
,
701 op
, args
, ARRAY_SIZE(args
));
705 emit_builtin_binop(struct ntv_context
*ctx
, enum GLSLstd450 op
, SpvId type
,
706 SpvId src0
, SpvId src1
)
708 SpvId args
[] = { src0
, src1
};
709 return spirv_builder_emit_ext_inst(&ctx
->builder
, type
, ctx
->GLSL_std_450
,
710 op
, args
, ARRAY_SIZE(args
));
714 emit_builtin_triop(struct ntv_context
*ctx
, enum GLSLstd450 op
, SpvId type
,
715 SpvId src0
, SpvId src1
, SpvId src2
)
717 SpvId args
[] = { src0
, src1
, src2
};
718 return spirv_builder_emit_ext_inst(&ctx
->builder
, type
, ctx
->GLSL_std_450
,
719 op
, args
, ARRAY_SIZE(args
));
723 get_fvec_constant(struct ntv_context
*ctx
, unsigned bit_size
,
724 unsigned num_components
, float value
)
726 assert(bit_size
== 32);
728 SpvId result
= emit_float_const(ctx
, bit_size
, value
);
729 if (num_components
== 1)
732 assert(num_components
> 1);
733 SpvId components
[num_components
];
734 for (int i
= 0; i
< num_components
; i
++)
735 components
[i
] = result
;
737 SpvId type
= get_fvec_type(ctx
, bit_size
, num_components
);
738 return spirv_builder_const_composite(&ctx
->builder
, type
, components
,
743 get_uvec_constant(struct ntv_context
*ctx
, unsigned bit_size
,
744 unsigned num_components
, uint32_t value
)
746 assert(bit_size
== 32);
748 SpvId result
= emit_uint_const(ctx
, bit_size
, value
);
749 if (num_components
== 1)
752 assert(num_components
> 1);
753 SpvId components
[num_components
];
754 for (int i
= 0; i
< num_components
; i
++)
755 components
[i
] = result
;
757 SpvId type
= get_uvec_type(ctx
, bit_size
, num_components
);
758 return spirv_builder_const_composite(&ctx
->builder
, type
, components
,
763 get_ivec_constant(struct ntv_context
*ctx
, unsigned bit_size
,
764 unsigned num_components
, int32_t value
)
766 assert(bit_size
== 32);
768 SpvId result
= emit_int_const(ctx
, bit_size
, value
);
769 if (num_components
== 1)
772 assert(num_components
> 1);
773 SpvId components
[num_components
];
774 for (int i
= 0; i
< num_components
; i
++)
775 components
[i
] = result
;
777 SpvId type
= get_ivec_type(ctx
, bit_size
, num_components
);
778 return spirv_builder_const_composite(&ctx
->builder
, type
, components
,
782 static inline unsigned
783 alu_instr_src_components(const nir_alu_instr
*instr
, unsigned src
)
785 if (nir_op_infos
[instr
->op
].input_sizes
[src
] > 0)
786 return nir_op_infos
[instr
->op
].input_sizes
[src
];
788 if (instr
->dest
.dest
.is_ssa
)
789 return instr
->dest
.dest
.ssa
.num_components
;
791 return instr
->dest
.dest
.reg
.reg
->num_components
;
795 get_alu_src(struct ntv_context
*ctx
, nir_alu_instr
*alu
, unsigned src
)
797 SpvId uint_value
= get_alu_src_uint(ctx
, alu
, src
);
799 unsigned num_components
= alu_instr_src_components(alu
, src
);
800 unsigned bit_size
= nir_src_bit_size(alu
->src
[src
].src
);
801 nir_alu_type type
= nir_op_infos
[alu
->op
].input_types
[src
];
803 switch (nir_alu_type_get_base_type(type
)) {
805 assert(bit_size
== 1);
806 return uvec_to_bvec(ctx
, uint_value
, num_components
);
809 return bitcast_to_ivec(ctx
, uint_value
, bit_size
, num_components
);
815 return bitcast_to_fvec(ctx
, uint_value
, bit_size
, num_components
);
818 unreachable("unknown nir_alu_type");
823 store_alu_result(struct ntv_context
*ctx
, nir_alu_instr
*alu
, SpvId result
)
825 assert(!alu
->dest
.saturate
);
826 return store_dest(ctx
, &alu
->dest
.dest
, result
, nir_op_infos
[alu
->op
].output_type
);
830 get_dest_type(struct ntv_context
*ctx
, nir_dest
*dest
, nir_alu_type type
)
832 unsigned num_components
= nir_dest_num_components(*dest
);
833 unsigned bit_size
= nir_dest_bit_size(*dest
);
835 switch (nir_alu_type_get_base_type(type
)) {
837 return get_bvec_type(ctx
, num_components
);
840 return get_ivec_type(ctx
, bit_size
, num_components
);
843 return get_uvec_type(ctx
, bit_size
, num_components
);
846 return get_fvec_type(ctx
, bit_size
, num_components
);
849 unreachable("unsupported nir_alu_type");
854 emit_alu(struct ntv_context
*ctx
, nir_alu_instr
*alu
)
856 SpvId src
[nir_op_infos
[alu
->op
].num_inputs
];
857 for (unsigned i
= 0; i
< nir_op_infos
[alu
->op
].num_inputs
; i
++)
858 src
[i
] = get_alu_src(ctx
, alu
, i
);
860 SpvId dest_type
= get_dest_type(ctx
, &alu
->dest
.dest
,
861 nir_op_infos
[alu
->op
].output_type
);
862 unsigned bit_size
= nir_dest_bit_size(alu
->dest
.dest
);
863 unsigned num_components
= nir_dest_num_components(alu
->dest
.dest
);
868 assert(nir_op_infos
[alu
->op
].num_inputs
== 1);
872 #define UNOP(nir_op, spirv_op) \
874 assert(nir_op_infos[alu->op].num_inputs == 1); \
875 result = emit_unop(ctx, spirv_op, dest_type, src[0]); \
878 UNOP(nir_op_ineg
, SpvOpSNegate
)
879 UNOP(nir_op_fneg
, SpvOpFNegate
)
880 UNOP(nir_op_fddx
, SpvOpDPdx
)
881 UNOP(nir_op_fddy
, SpvOpDPdy
)
882 UNOP(nir_op_f2i32
, SpvOpConvertFToS
)
883 UNOP(nir_op_f2u32
, SpvOpConvertFToU
)
884 UNOP(nir_op_i2f32
, SpvOpConvertSToF
)
885 UNOP(nir_op_u2f32
, SpvOpConvertUToF
)
886 UNOP(nir_op_inot
, SpvOpNot
)
890 assert(nir_op_infos
[alu
->op
].num_inputs
== 1);
891 result
= emit_select(ctx
, dest_type
, src
[0],
892 get_ivec_constant(ctx
, 32, num_components
, 1),
893 get_ivec_constant(ctx
, 32, num_components
, 0));
897 assert(nir_op_infos
[alu
->op
].num_inputs
== 1);
898 result
= emit_select(ctx
, dest_type
, src
[0],
899 get_fvec_constant(ctx
, 32, num_components
, 1),
900 get_fvec_constant(ctx
, 32, num_components
, 0));
903 #define BUILTIN_UNOP(nir_op, spirv_op) \
905 assert(nir_op_infos[alu->op].num_inputs == 1); \
906 result = emit_builtin_unop(ctx, spirv_op, dest_type, src[0]); \
909 BUILTIN_UNOP(nir_op_fabs
, GLSLstd450FAbs
)
910 BUILTIN_UNOP(nir_op_fsqrt
, GLSLstd450Sqrt
)
911 BUILTIN_UNOP(nir_op_frsq
, GLSLstd450InverseSqrt
)
912 BUILTIN_UNOP(nir_op_flog2
, GLSLstd450Log2
)
913 BUILTIN_UNOP(nir_op_fexp2
, GLSLstd450Exp2
)
914 BUILTIN_UNOP(nir_op_ffract
, GLSLstd450Fract
)
915 BUILTIN_UNOP(nir_op_ffloor
, GLSLstd450Floor
)
916 BUILTIN_UNOP(nir_op_fceil
, GLSLstd450Ceil
)
917 BUILTIN_UNOP(nir_op_ftrunc
, GLSLstd450Trunc
)
918 BUILTIN_UNOP(nir_op_fround_even
, GLSLstd450RoundEven
)
919 BUILTIN_UNOP(nir_op_fsign
, GLSLstd450FSign
)
920 BUILTIN_UNOP(nir_op_fsin
, GLSLstd450Sin
)
921 BUILTIN_UNOP(nir_op_fcos
, GLSLstd450Cos
)
925 assert(nir_op_infos
[alu
->op
].num_inputs
== 1);
926 result
= emit_binop(ctx
, SpvOpFDiv
, dest_type
,
927 get_fvec_constant(ctx
, bit_size
, num_components
, 1),
932 assert(nir_op_infos
[alu
->op
].num_inputs
== 1);
933 result
= emit_binop(ctx
, SpvOpFOrdNotEqual
, dest_type
, src
[0],
934 get_fvec_constant(ctx
,
935 nir_src_bit_size(alu
->src
[0].src
),
940 #define BINOP(nir_op, spirv_op) \
942 assert(nir_op_infos[alu->op].num_inputs == 2); \
943 result = emit_binop(ctx, spirv_op, dest_type, src[0], src[1]); \
946 BINOP(nir_op_iadd
, SpvOpIAdd
)
947 BINOP(nir_op_isub
, SpvOpISub
)
948 BINOP(nir_op_imul
, SpvOpIMul
)
949 BINOP(nir_op_idiv
, SpvOpSDiv
)
950 BINOP(nir_op_udiv
, SpvOpUDiv
)
951 BINOP(nir_op_fadd
, SpvOpFAdd
)
952 BINOP(nir_op_fsub
, SpvOpFSub
)
953 BINOP(nir_op_fmul
, SpvOpFMul
)
954 BINOP(nir_op_fdiv
, SpvOpFDiv
)
955 BINOP(nir_op_fmod
, SpvOpFMod
)
956 BINOP(nir_op_ilt
, SpvOpSLessThan
)
957 BINOP(nir_op_ige
, SpvOpSGreaterThanEqual
)
958 BINOP(nir_op_ieq
, SpvOpIEqual
)
959 BINOP(nir_op_ine
, SpvOpINotEqual
)
960 BINOP(nir_op_flt
, SpvOpFOrdLessThan
)
961 BINOP(nir_op_fge
, SpvOpFOrdGreaterThanEqual
)
962 BINOP(nir_op_feq
, SpvOpFOrdEqual
)
963 BINOP(nir_op_fne
, SpvOpFOrdNotEqual
)
964 BINOP(nir_op_ishl
, SpvOpShiftLeftLogical
)
965 BINOP(nir_op_ishr
, SpvOpShiftRightArithmetic
)
966 BINOP(nir_op_ushr
, SpvOpShiftRightLogical
)
967 BINOP(nir_op_iand
, SpvOpBitwiseAnd
)
968 BINOP(nir_op_ior
, SpvOpBitwiseOr
)
971 #define BUILTIN_BINOP(nir_op, spirv_op) \
973 assert(nir_op_infos[alu->op].num_inputs == 2); \
974 result = emit_builtin_binop(ctx, spirv_op, dest_type, src[0], src[1]); \
977 BUILTIN_BINOP(nir_op_fmin
, GLSLstd450FMin
)
978 BUILTIN_BINOP(nir_op_fmax
, GLSLstd450FMax
)
984 assert(nir_op_infos
[alu
->op
].num_inputs
== 2);
985 result
= emit_binop(ctx
, SpvOpDot
, dest_type
, src
[0], src
[1]);
992 assert(nir_op_infos
[alu
->op
].num_inputs
== 2);
993 int num_components
= nir_dest_num_components(alu
->dest
.dest
);
994 SpvId bool_type
= get_bvec_type(ctx
, num_components
);
996 SpvId zero
= emit_float_const(ctx
, bit_size
, 0.0f
);
997 SpvId one
= emit_float_const(ctx
, bit_size
, 1.0f
);
998 if (num_components
> 1) {
999 SpvId zero_comps
[num_components
], one_comps
[num_components
];
1000 for (int i
= 0; i
< num_components
; i
++) {
1001 zero_comps
[i
] = zero
;
1005 zero
= spirv_builder_const_composite(&ctx
->builder
, dest_type
,
1006 zero_comps
, num_components
);
1007 one
= spirv_builder_const_composite(&ctx
->builder
, dest_type
,
1008 one_comps
, num_components
);
1013 case nir_op_seq
: op
= SpvOpFOrdEqual
; break;
1014 case nir_op_sne
: op
= SpvOpFOrdNotEqual
; break;
1015 case nir_op_slt
: op
= SpvOpFOrdLessThan
; break;
1016 case nir_op_sge
: op
= SpvOpFOrdGreaterThanEqual
; break;
1017 default: unreachable("unexpected op");
1020 result
= emit_binop(ctx
, op
, bool_type
, src
[0], src
[1]);
1021 result
= emit_select(ctx
, dest_type
, result
, one
, zero
);
1026 assert(nir_op_infos
[alu
->op
].num_inputs
== 3);
1027 result
= emit_builtin_triop(ctx
, GLSLstd450FMix
, dest_type
,
1028 src
[0], src
[1], src
[2]);
1032 result
= emit_binop(ctx
, SpvOpFOrdGreaterThan
,
1033 get_bvec_type(ctx
, num_components
),
1035 get_fvec_constant(ctx
,
1036 nir_src_bit_size(alu
->src
[0].src
),
1037 num_components
, 0));
1038 result
= emit_select(ctx
, dest_type
, result
, src
[1], src
[2]);
1042 assert(nir_op_infos
[alu
->op
].num_inputs
== 3);
1043 result
= emit_select(ctx
, dest_type
, src
[0], src
[1], src
[2]);
1046 case nir_op_bany_fnequal2
:
1047 case nir_op_bany_fnequal3
:
1048 case nir_op_bany_fnequal4
:
1049 assert(nir_op_infos
[alu
->op
].num_inputs
== 2);
1050 assert(alu_instr_src_components(alu
, 0) ==
1051 alu_instr_src_components(alu
, 1));
1052 result
= emit_binop(ctx
, SpvOpFOrdNotEqual
,
1053 get_bvec_type(ctx
, alu_instr_src_components(alu
, 0)),
1055 result
= emit_unop(ctx
, SpvOpAny
, dest_type
, result
);
1058 case nir_op_ball_fequal2
:
1059 case nir_op_ball_fequal3
:
1060 case nir_op_ball_fequal4
:
1061 assert(nir_op_infos
[alu
->op
].num_inputs
== 2);
1062 assert(alu_instr_src_components(alu
, 0) ==
1063 alu_instr_src_components(alu
, 1));
1064 result
= emit_binop(ctx
, SpvOpFOrdEqual
,
1065 get_bvec_type(ctx
, alu_instr_src_components(alu
, 0)),
1067 result
= emit_unop(ctx
, SpvOpAll
, dest_type
, result
);
1070 case nir_op_bany_inequal2
:
1071 case nir_op_bany_inequal3
:
1072 case nir_op_bany_inequal4
:
1073 assert(nir_op_infos
[alu
->op
].num_inputs
== 2);
1074 assert(alu_instr_src_components(alu
, 0) ==
1075 alu_instr_src_components(alu
, 1));
1076 result
= emit_binop(ctx
, SpvOpINotEqual
,
1077 get_bvec_type(ctx
, alu_instr_src_components(alu
, 0)),
1079 result
= emit_unop(ctx
, SpvOpAny
, dest_type
, result
);
1082 case nir_op_ball_iequal2
:
1083 case nir_op_ball_iequal3
:
1084 case nir_op_ball_iequal4
:
1085 assert(nir_op_infos
[alu
->op
].num_inputs
== 2);
1086 assert(alu_instr_src_components(alu
, 0) ==
1087 alu_instr_src_components(alu
, 1));
1088 result
= emit_binop(ctx
, SpvOpIEqual
,
1089 get_bvec_type(ctx
, alu_instr_src_components(alu
, 0)),
1091 result
= emit_unop(ctx
, SpvOpAll
, dest_type
, result
);
1097 int num_inputs
= nir_op_infos
[alu
->op
].num_inputs
;
1098 assert(2 <= num_inputs
&& num_inputs
<= 4);
1099 result
= spirv_builder_emit_composite_construct(&ctx
->builder
, dest_type
,
1105 fprintf(stderr
, "emit_alu: not implemented (%s)\n",
1106 nir_op_infos
[alu
->op
].name
);
1108 unreachable("unsupported opcode");
1112 store_alu_result(ctx
, alu
, result
);
1116 emit_load_const(struct ntv_context
*ctx
, nir_load_const_instr
*load_const
)
1118 unsigned bit_size
= load_const
->def
.bit_size
;
1119 unsigned num_components
= load_const
->def
.num_components
;
1122 if (num_components
> 1) {
1123 SpvId components
[num_components
];
1125 if (bit_size
== 1) {
1126 for (int i
= 0; i
< num_components
; i
++)
1127 components
[i
] = spirv_builder_const_bool(&ctx
->builder
,
1128 load_const
->value
[i
].b
);
1130 type
= get_bvec_type(ctx
, num_components
);
1132 for (int i
= 0; i
< num_components
; i
++)
1133 components
[i
] = emit_uint_const(ctx
, bit_size
,
1134 load_const
->value
[i
].u32
);
1136 type
= get_uvec_type(ctx
, bit_size
, num_components
);
1138 constant
= spirv_builder_const_composite(&ctx
->builder
, type
,
1139 components
, num_components
);
1141 assert(num_components
== 1);
1143 constant
= spirv_builder_const_bool(&ctx
->builder
,
1144 load_const
->value
[0].b
);
1146 constant
= emit_uint_const(ctx
, bit_size
, load_const
->value
[0].u32
);
1150 constant
= bvec_to_uvec(ctx
, constant
, num_components
);
1152 store_ssa_def_uint(ctx
, &load_const
->def
, constant
);
1156 emit_load_ubo(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
1158 nir_const_value
*const_block_index
= nir_src_as_const_value(intr
->src
[0]);
1159 assert(const_block_index
); // no dynamic indexing for now
1160 assert(const_block_index
->u32
== 0); // we only support the default UBO for now
1162 nir_const_value
*const_offset
= nir_src_as_const_value(intr
->src
[1]);
1164 SpvId uvec4_type
= get_uvec_type(ctx
, 32, 4);
1165 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
1166 SpvStorageClassUniform
,
1169 unsigned idx
= const_offset
->u32
;
1170 SpvId member
= emit_uint_const(ctx
, 32, 0);
1171 SpvId offset
= emit_uint_const(ctx
, 32, idx
);
1172 SpvId offsets
[] = { member
, offset
};
1173 SpvId ptr
= spirv_builder_emit_access_chain(&ctx
->builder
, pointer_type
,
1174 ctx
->ubos
[0], offsets
,
1175 ARRAY_SIZE(offsets
));
1176 SpvId result
= spirv_builder_emit_load(&ctx
->builder
, uvec4_type
, ptr
);
1178 SpvId type
= get_dest_uvec_type(ctx
, &intr
->dest
);
1179 unsigned num_components
= nir_dest_num_components(intr
->dest
);
1180 if (num_components
== 1) {
1181 uint32_t components
[] = { 0 };
1182 result
= spirv_builder_emit_composite_extract(&ctx
->builder
,
1186 } else if (num_components
< 4) {
1187 SpvId constituents
[num_components
];
1188 SpvId uint_type
= spirv_builder_type_uint(&ctx
->builder
, 32);
1189 for (uint32_t i
= 0; i
< num_components
; ++i
)
1190 constituents
[i
] = spirv_builder_emit_composite_extract(&ctx
->builder
,
1195 result
= spirv_builder_emit_composite_construct(&ctx
->builder
,
1201 store_dest_uint(ctx
, &intr
->dest
, result
);
1203 unreachable("uniform-addressing not yet supported");
1207 emit_discard(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
1209 assert(ctx
->block_started
);
1210 spirv_builder_emit_kill(&ctx
->builder
);
1211 /* discard is weird in NIR, so let's just create an unreachable block after
1212 it and hope that the vulkan driver will DCE any instructinos in it. */
1213 spirv_builder_label(&ctx
->builder
, spirv_builder_new_id(&ctx
->builder
));
1217 emit_load_deref(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
1219 /* uint is a bit of a lie here; it's really just a pointer */
1220 SpvId ptr
= get_src_uint(ctx
, intr
->src
);
1222 nir_variable
*var
= nir_intrinsic_get_var(intr
, 0);
1223 SpvId result
= spirv_builder_emit_load(&ctx
->builder
,
1224 get_glsl_type(ctx
, var
->type
),
1226 unsigned num_components
= nir_dest_num_components(intr
->dest
);
1227 unsigned bit_size
= nir_dest_bit_size(intr
->dest
);
1228 result
= bitcast_to_uvec(ctx
, result
, bit_size
, num_components
);
1229 store_dest_uint(ctx
, &intr
->dest
, result
);
1233 emit_store_deref(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
1235 /* uint is a bit of a lie here; it's really just a pointer */
1236 SpvId ptr
= get_src_uint(ctx
, &intr
->src
[0]);
1237 SpvId src
= get_src_uint(ctx
, &intr
->src
[1]);
1239 nir_variable
*var
= nir_intrinsic_get_var(intr
, 0);
1240 SpvId type
= get_glsl_type(ctx
, glsl_without_array(var
->type
));
1241 SpvId result
= emit_bitcast(ctx
, type
, src
);
1242 spirv_builder_emit_store(&ctx
->builder
, ptr
, result
);
1246 emit_load_front_face(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
1248 SpvId var_type
= get_glsl_type(ctx
, glsl_bool_type());
1249 if (!ctx
->front_face_var
) {
1250 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
1251 SpvStorageClassInput
,
1253 ctx
->front_face_var
= spirv_builder_emit_var(&ctx
->builder
,
1255 SpvStorageClassInput
);
1256 spirv_builder_emit_name(&ctx
->builder
, ctx
->front_face_var
,
1258 spirv_builder_emit_builtin(&ctx
->builder
, ctx
->front_face_var
,
1259 SpvBuiltInFrontFacing
);
1261 assert(ctx
->num_entry_ifaces
< ARRAY_SIZE(ctx
->entry_ifaces
));
1262 ctx
->entry_ifaces
[ctx
->num_entry_ifaces
++] = ctx
->front_face_var
;
1265 SpvId result
= spirv_builder_emit_load(&ctx
->builder
, var_type
,
1266 ctx
->front_face_var
);
1267 assert(1 == nir_dest_num_components(intr
->dest
));
1268 result
= bvec_to_uvec(ctx
, result
, 1);
1269 store_dest_uint(ctx
, &intr
->dest
, result
);
1273 emit_intrinsic(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
1275 switch (intr
->intrinsic
) {
1276 case nir_intrinsic_load_ubo
:
1277 emit_load_ubo(ctx
, intr
);
1280 case nir_intrinsic_discard
:
1281 emit_discard(ctx
, intr
);
1284 case nir_intrinsic_load_deref
:
1285 emit_load_deref(ctx
, intr
);
1288 case nir_intrinsic_store_deref
:
1289 emit_store_deref(ctx
, intr
);
1292 case nir_intrinsic_load_front_face
:
1293 emit_load_front_face(ctx
, intr
);
1297 fprintf(stderr
, "emit_intrinsic: not implemented (%s)\n",
1298 nir_intrinsic_infos
[intr
->intrinsic
].name
);
1299 unreachable("unsupported intrinsic");
1304 emit_undef(struct ntv_context
*ctx
, nir_ssa_undef_instr
*undef
)
1306 SpvId type
= get_uvec_type(ctx
, undef
->def
.bit_size
,
1307 undef
->def
.num_components
);
1309 store_ssa_def_uint(ctx
, &undef
->def
,
1310 spirv_builder_emit_undef(&ctx
->builder
, type
));
1314 get_src_float(struct ntv_context
*ctx
, nir_src
*src
)
1316 SpvId def
= get_src_uint(ctx
, src
);
1317 unsigned num_components
= nir_src_num_components(*src
);
1318 unsigned bit_size
= nir_src_bit_size(*src
);
1319 return bitcast_to_fvec(ctx
, def
, bit_size
, num_components
);
1323 get_src_int(struct ntv_context
*ctx
, nir_src
*src
)
1325 SpvId def
= get_src_uint(ctx
, src
);
1326 unsigned num_components
= nir_src_num_components(*src
);
1327 unsigned bit_size
= nir_src_bit_size(*src
);
1328 return bitcast_to_ivec(ctx
, def
, bit_size
, num_components
);
1332 emit_tex(struct ntv_context
*ctx
, nir_tex_instr
*tex
)
1334 assert(tex
->op
== nir_texop_tex
||
1335 tex
->op
== nir_texop_txb
||
1336 tex
->op
== nir_texop_txl
||
1337 tex
->op
== nir_texop_txd
||
1338 tex
->op
== nir_texop_txf
);
1339 assert(nir_alu_type_get_base_type(tex
->dest_type
) == nir_type_float
);
1340 assert(tex
->texture_index
== tex
->sampler_index
);
1342 SpvId coord
= 0, proj
= 0, bias
= 0, lod
= 0, dref
= 0, dx
= 0, dy
= 0;
1343 unsigned coord_components
= 0;
1344 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
1345 switch (tex
->src
[i
].src_type
) {
1346 case nir_tex_src_coord
:
1347 if (tex
->op
== nir_texop_txf
)
1348 coord
= get_src_int(ctx
, &tex
->src
[i
].src
);
1350 coord
= get_src_float(ctx
, &tex
->src
[i
].src
);
1351 coord_components
= nir_src_num_components(tex
->src
[i
].src
);
1354 case nir_tex_src_projector
:
1355 assert(nir_src_num_components(tex
->src
[i
].src
) == 1);
1356 proj
= get_src_float(ctx
, &tex
->src
[i
].src
);
1360 case nir_tex_src_bias
:
1361 assert(tex
->op
== nir_texop_txb
);
1362 bias
= get_src_float(ctx
, &tex
->src
[i
].src
);
1366 case nir_tex_src_lod
:
1367 assert(nir_src_num_components(tex
->src
[i
].src
) == 1);
1368 if (tex
->op
== nir_texop_txf
)
1369 lod
= get_src_int(ctx
, &tex
->src
[i
].src
);
1371 lod
= get_src_float(ctx
, &tex
->src
[i
].src
);
1375 case nir_tex_src_comparator
:
1376 assert(nir_src_num_components(tex
->src
[i
].src
) == 1);
1377 dref
= get_src_float(ctx
, &tex
->src
[i
].src
);
1381 case nir_tex_src_ddx
:
1382 dx
= get_src_float(ctx
, &tex
->src
[i
].src
);
1386 case nir_tex_src_ddy
:
1387 dy
= get_src_float(ctx
, &tex
->src
[i
].src
);
1392 fprintf(stderr
, "texture source: %d\n", tex
->src
[i
].src_type
);
1393 unreachable("unknown texture source");
1397 if (lod
== 0 && ctx
->stage
!= MESA_SHADER_FRAGMENT
) {
1398 lod
= emit_float_const(ctx
, 32, 0.0f
);
1403 SpvDim dimension
= type_to_dim(tex
->sampler_dim
, &is_ms
);
1404 SpvId float_type
= spirv_builder_type_float(&ctx
->builder
, 32);
1405 SpvId image_type
= spirv_builder_type_image(&ctx
->builder
, float_type
,
1406 dimension
, false, tex
->is_array
, is_ms
, 1,
1407 SpvImageFormatUnknown
);
1408 SpvId sampled_type
= spirv_builder_type_sampled_image(&ctx
->builder
,
1411 assert(tex
->texture_index
< ctx
->num_samplers
);
1412 SpvId load
= spirv_builder_emit_load(&ctx
->builder
, sampled_type
,
1413 ctx
->samplers
[tex
->texture_index
]);
1415 SpvId dest_type
= get_dest_type(ctx
, &tex
->dest
, tex
->dest_type
);
1418 SpvId constituents
[coord_components
+ 1];
1419 if (coord_components
== 1)
1420 constituents
[0] = coord
;
1422 assert(coord_components
> 1);
1423 SpvId float_type
= spirv_builder_type_float(&ctx
->builder
, 32);
1424 for (uint32_t i
= 0; i
< coord_components
; ++i
)
1425 constituents
[i
] = spirv_builder_emit_composite_extract(&ctx
->builder
,
1431 constituents
[coord_components
++] = proj
;
1433 SpvId vec_type
= get_fvec_type(ctx
, 32, coord_components
);
1434 coord
= spirv_builder_emit_composite_construct(&ctx
->builder
,
1440 SpvId actual_dest_type
= dest_type
;
1442 actual_dest_type
= float_type
;
1445 if (tex
->op
== nir_texop_txf
) {
1446 SpvId image
= spirv_builder_emit_image(&ctx
->builder
, image_type
, load
);
1447 result
= spirv_builder_emit_image_fetch(&ctx
->builder
, dest_type
,
1450 result
= spirv_builder_emit_image_sample(&ctx
->builder
,
1451 actual_dest_type
, load
,
1454 lod
, bias
, dref
, dx
, dy
);
1457 spirv_builder_emit_decoration(&ctx
->builder
, result
,
1458 SpvDecorationRelaxedPrecision
);
1461 SpvId components
[4] = { result
, result
, result
, result
};
1462 result
= spirv_builder_emit_composite_construct(&ctx
->builder
,
1468 store_dest(ctx
, &tex
->dest
, result
, tex
->dest_type
);
1472 start_block(struct ntv_context
*ctx
, SpvId label
)
1474 /* terminate previous block if needed */
1475 if (ctx
->block_started
)
1476 spirv_builder_emit_branch(&ctx
->builder
, label
);
1478 /* start new block */
1479 spirv_builder_label(&ctx
->builder
, label
);
1480 ctx
->block_started
= true;
1484 branch(struct ntv_context
*ctx
, SpvId label
)
1486 assert(ctx
->block_started
);
1487 spirv_builder_emit_branch(&ctx
->builder
, label
);
1488 ctx
->block_started
= false;
1492 branch_conditional(struct ntv_context
*ctx
, SpvId condition
, SpvId then_id
,
1495 assert(ctx
->block_started
);
1496 spirv_builder_emit_branch_conditional(&ctx
->builder
, condition
,
1498 ctx
->block_started
= false;
1502 emit_jump(struct ntv_context
*ctx
, nir_jump_instr
*jump
)
1504 switch (jump
->type
) {
1505 case nir_jump_break
:
1506 assert(ctx
->loop_break
);
1507 branch(ctx
, ctx
->loop_break
);
1510 case nir_jump_continue
:
1511 assert(ctx
->loop_cont
);
1512 branch(ctx
, ctx
->loop_cont
);
1516 unreachable("Unsupported jump type\n");
1521 emit_deref_var(struct ntv_context
*ctx
, nir_deref_instr
*deref
)
1523 assert(deref
->deref_type
== nir_deref_type_var
);
1525 struct hash_entry
*he
= _mesa_hash_table_search(ctx
->vars
, deref
->var
);
1527 SpvId result
= (SpvId
)(intptr_t)he
->data
;
1528 /* uint is a bit of a lie here, it's really just an opaque type */
1529 store_dest_uint(ctx
, &deref
->dest
, result
);
1533 emit_deref_array(struct ntv_context
*ctx
, nir_deref_instr
*deref
)
1535 assert(deref
->deref_type
== nir_deref_type_array
);
1536 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
1538 SpvStorageClass storage_class
;
1539 switch (var
->data
.mode
) {
1540 case nir_var_shader_in
:
1541 storage_class
= SpvStorageClassInput
;
1544 case nir_var_shader_out
:
1545 storage_class
= SpvStorageClassOutput
;
1549 unreachable("Unsupported nir_variable_mode\n");
1552 SpvId index
= get_src_uint(ctx
, &deref
->arr
.index
);
1554 SpvId ptr_type
= spirv_builder_type_pointer(&ctx
->builder
,
1556 get_glsl_type(ctx
, deref
->type
));
1558 SpvId result
= spirv_builder_emit_access_chain(&ctx
->builder
,
1560 get_src_uint(ctx
, &deref
->parent
),
1562 /* uint is a bit of a lie here, it's really just an opaque type */
1563 store_dest_uint(ctx
, &deref
->dest
, result
);
1567 emit_deref(struct ntv_context
*ctx
, nir_deref_instr
*deref
)
1569 switch (deref
->deref_type
) {
1570 case nir_deref_type_var
:
1571 emit_deref_var(ctx
, deref
);
1574 case nir_deref_type_array
:
1575 emit_deref_array(ctx
, deref
);
1579 unreachable("unexpected deref_type");
1584 emit_block(struct ntv_context
*ctx
, struct nir_block
*block
)
1586 start_block(ctx
, block_label(ctx
, block
));
1587 nir_foreach_instr(instr
, block
) {
1588 switch (instr
->type
) {
1589 case nir_instr_type_alu
:
1590 emit_alu(ctx
, nir_instr_as_alu(instr
));
1592 case nir_instr_type_intrinsic
:
1593 emit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
1595 case nir_instr_type_load_const
:
1596 emit_load_const(ctx
, nir_instr_as_load_const(instr
));
1598 case nir_instr_type_ssa_undef
:
1599 emit_undef(ctx
, nir_instr_as_ssa_undef(instr
));
1601 case nir_instr_type_tex
:
1602 emit_tex(ctx
, nir_instr_as_tex(instr
));
1604 case nir_instr_type_phi
:
1605 unreachable("nir_instr_type_phi not supported");
1607 case nir_instr_type_jump
:
1608 emit_jump(ctx
, nir_instr_as_jump(instr
));
1610 case nir_instr_type_call
:
1611 unreachable("nir_instr_type_call not supported");
1613 case nir_instr_type_parallel_copy
:
1614 unreachable("nir_instr_type_parallel_copy not supported");
1616 case nir_instr_type_deref
:
1617 emit_deref(ctx
, nir_instr_as_deref(instr
));
1624 emit_cf_list(struct ntv_context
*ctx
, struct exec_list
*list
);
1627 get_src_bool(struct ntv_context
*ctx
, nir_src
*src
)
1629 SpvId def
= get_src_uint(ctx
, src
);
1630 assert(nir_src_bit_size(*src
) == 1);
1631 unsigned num_components
= nir_src_num_components(*src
);
1632 return uvec_to_bvec(ctx
, def
, num_components
);
1636 emit_if(struct ntv_context
*ctx
, nir_if
*if_stmt
)
1638 SpvId condition
= get_src_bool(ctx
, &if_stmt
->condition
);
1640 SpvId header_id
= spirv_builder_new_id(&ctx
->builder
);
1641 SpvId then_id
= block_label(ctx
, nir_if_first_then_block(if_stmt
));
1642 SpvId endif_id
= spirv_builder_new_id(&ctx
->builder
);
1643 SpvId else_id
= endif_id
;
1645 bool has_else
= !exec_list_is_empty(&if_stmt
->else_list
);
1647 assert(nir_if_first_else_block(if_stmt
)->index
< ctx
->num_blocks
);
1648 else_id
= block_label(ctx
, nir_if_first_else_block(if_stmt
));
1651 /* create a header-block */
1652 start_block(ctx
, header_id
);
1653 spirv_builder_emit_selection_merge(&ctx
->builder
, endif_id
,
1654 SpvSelectionControlMaskNone
);
1655 branch_conditional(ctx
, condition
, then_id
, else_id
);
1657 emit_cf_list(ctx
, &if_stmt
->then_list
);
1660 if (ctx
->block_started
)
1661 branch(ctx
, endif_id
);
1663 emit_cf_list(ctx
, &if_stmt
->else_list
);
1666 start_block(ctx
, endif_id
);
1670 emit_loop(struct ntv_context
*ctx
, nir_loop
*loop
)
1672 SpvId header_id
= spirv_builder_new_id(&ctx
->builder
);
1673 SpvId begin_id
= block_label(ctx
, nir_loop_first_block(loop
));
1674 SpvId break_id
= spirv_builder_new_id(&ctx
->builder
);
1675 SpvId cont_id
= spirv_builder_new_id(&ctx
->builder
);
1677 /* create a header-block */
1678 start_block(ctx
, header_id
);
1679 spirv_builder_loop_merge(&ctx
->builder
, break_id
, cont_id
, SpvLoopControlMaskNone
);
1680 branch(ctx
, begin_id
);
1682 SpvId save_break
= ctx
->loop_break
;
1683 SpvId save_cont
= ctx
->loop_cont
;
1684 ctx
->loop_break
= break_id
;
1685 ctx
->loop_cont
= cont_id
;
1687 emit_cf_list(ctx
, &loop
->body
);
1689 ctx
->loop_break
= save_break
;
1690 ctx
->loop_cont
= save_cont
;
1692 branch(ctx
, cont_id
);
1693 start_block(ctx
, cont_id
);
1694 branch(ctx
, header_id
);
1696 start_block(ctx
, break_id
);
1700 emit_cf_list(struct ntv_context
*ctx
, struct exec_list
*list
)
1702 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
1703 switch (node
->type
) {
1704 case nir_cf_node_block
:
1705 emit_block(ctx
, nir_cf_node_as_block(node
));
1708 case nir_cf_node_if
:
1709 emit_if(ctx
, nir_cf_node_as_if(node
));
1712 case nir_cf_node_loop
:
1713 emit_loop(ctx
, nir_cf_node_as_loop(node
));
1716 case nir_cf_node_function
:
1717 unreachable("nir_cf_node_function not supported");
1723 struct spirv_shader
*
1724 nir_to_spirv(struct nir_shader
*s
)
1726 struct spirv_shader
*ret
= NULL
;
1728 struct ntv_context ctx
= {};
1730 switch (s
->info
.stage
) {
1731 case MESA_SHADER_VERTEX
:
1732 case MESA_SHADER_FRAGMENT
:
1733 case MESA_SHADER_COMPUTE
:
1734 spirv_builder_emit_cap(&ctx
.builder
, SpvCapabilityShader
);
1737 case MESA_SHADER_TESS_CTRL
:
1738 case MESA_SHADER_TESS_EVAL
:
1739 spirv_builder_emit_cap(&ctx
.builder
, SpvCapabilityTessellation
);
1742 case MESA_SHADER_GEOMETRY
:
1743 spirv_builder_emit_cap(&ctx
.builder
, SpvCapabilityGeometry
);
1747 unreachable("invalid stage");
1750 // TODO: only enable when needed
1751 if (s
->info
.stage
== MESA_SHADER_FRAGMENT
)
1752 spirv_builder_emit_cap(&ctx
.builder
, SpvCapabilitySampled1D
);
1754 ctx
.stage
= s
->info
.stage
;
1755 ctx
.GLSL_std_450
= spirv_builder_import(&ctx
.builder
, "GLSL.std.450");
1756 spirv_builder_emit_source(&ctx
.builder
, SpvSourceLanguageGLSL
, 450);
1758 spirv_builder_emit_mem_model(&ctx
.builder
, SpvAddressingModelLogical
,
1759 SpvMemoryModelGLSL450
);
1761 SpvExecutionModel exec_model
;
1762 switch (s
->info
.stage
) {
1763 case MESA_SHADER_VERTEX
:
1764 exec_model
= SpvExecutionModelVertex
;
1766 case MESA_SHADER_TESS_CTRL
:
1767 exec_model
= SpvExecutionModelTessellationControl
;
1769 case MESA_SHADER_TESS_EVAL
:
1770 exec_model
= SpvExecutionModelTessellationEvaluation
;
1772 case MESA_SHADER_GEOMETRY
:
1773 exec_model
= SpvExecutionModelGeometry
;
1775 case MESA_SHADER_FRAGMENT
:
1776 exec_model
= SpvExecutionModelFragment
;
1778 case MESA_SHADER_COMPUTE
:
1779 exec_model
= SpvExecutionModelGLCompute
;
1782 unreachable("invalid stage");
1785 SpvId type_void
= spirv_builder_type_void(&ctx
.builder
);
1786 SpvId type_main
= spirv_builder_type_function(&ctx
.builder
, type_void
,
1788 SpvId entry_point
= spirv_builder_new_id(&ctx
.builder
);
1789 spirv_builder_emit_name(&ctx
.builder
, entry_point
, "main");
1791 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
1792 _mesa_key_pointer_equal
);
1794 nir_foreach_variable(var
, &s
->inputs
)
1795 emit_input(&ctx
, var
);
1797 nir_foreach_variable(var
, &s
->outputs
)
1798 emit_output(&ctx
, var
);
1800 nir_foreach_variable(var
, &s
->uniforms
)
1801 emit_uniform(&ctx
, var
);
1803 if (s
->info
.stage
== MESA_SHADER_FRAGMENT
) {
1804 spirv_builder_emit_exec_mode(&ctx
.builder
, entry_point
,
1805 SpvExecutionModeOriginUpperLeft
);
1806 if (s
->info
.outputs_written
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
))
1807 spirv_builder_emit_exec_mode(&ctx
.builder
, entry_point
,
1808 SpvExecutionModeDepthReplacing
);
1812 spirv_builder_function(&ctx
.builder
, entry_point
, type_void
,
1813 SpvFunctionControlMaskNone
,
1816 nir_function_impl
*entry
= nir_shader_get_entrypoint(s
);
1817 nir_metadata_require(entry
, nir_metadata_block_index
);
1819 ctx
.defs
= (SpvId
*)malloc(sizeof(SpvId
) * entry
->ssa_alloc
);
1822 ctx
.num_defs
= entry
->ssa_alloc
;
1824 nir_index_local_regs(entry
);
1825 ctx
.regs
= malloc(sizeof(SpvId
) * entry
->reg_alloc
);
1828 ctx
.num_regs
= entry
->reg_alloc
;
1830 SpvId
*block_ids
= (SpvId
*)malloc(sizeof(SpvId
) * entry
->num_blocks
);
1834 for (int i
= 0; i
< entry
->num_blocks
; ++i
)
1835 block_ids
[i
] = spirv_builder_new_id(&ctx
.builder
);
1837 ctx
.block_ids
= block_ids
;
1838 ctx
.num_blocks
= entry
->num_blocks
;
1840 /* emit a block only for the variable declarations */
1841 start_block(&ctx
, spirv_builder_new_id(&ctx
.builder
));
1842 foreach_list_typed(nir_register
, reg
, node
, &entry
->registers
) {
1843 SpvId type
= get_uvec_type(&ctx
, reg
->bit_size
, reg
->num_components
);
1844 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
.builder
,
1845 SpvStorageClassFunction
,
1847 SpvId var
= spirv_builder_emit_var(&ctx
.builder
, pointer_type
,
1848 SpvStorageClassFunction
);
1850 ctx
.regs
[reg
->index
] = var
;
1853 emit_cf_list(&ctx
, &entry
->body
);
1857 spirv_builder_return(&ctx
.builder
); // doesn't belong here, but whatevz
1858 spirv_builder_function_end(&ctx
.builder
);
1860 spirv_builder_emit_entry_point(&ctx
.builder
, exec_model
, entry_point
,
1861 "main", ctx
.entry_ifaces
,
1862 ctx
.num_entry_ifaces
);
1864 size_t num_words
= spirv_builder_get_num_words(&ctx
.builder
);
1866 ret
= CALLOC_STRUCT(spirv_shader
);
1870 ret
->words
= MALLOC(sizeof(uint32_t) * num_words
);
1874 ret
->num_words
= spirv_builder_get_words(&ctx
.builder
, ret
->words
, num_words
);
1875 assert(ret
->num_words
== num_words
);
1882 spirv_shader_delete(ret
);
1885 _mesa_hash_table_destroy(ctx
.vars
, NULL
);
1891 spirv_shader_delete(struct spirv_shader
*s
)