2 * Copyright 2018 Collabora Ltd.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
24 #include "nir_to_spirv.h"
25 #include "spirv_builder.h"
28 #include "pipe/p_state.h"
29 #include "util/u_memory.h"
30 #include "util/hash_table.h"
33 struct spirv_builder builder
;
37 gl_shader_stage stage
;
42 SpvId samplers
[PIPE_MAX_SAMPLERS
];
44 SpvId entry_ifaces
[PIPE_MAX_SHADER_INPUTS
* 4 + PIPE_MAX_SHADER_OUTPUTS
* 4];
45 size_t num_entry_ifaces
;
53 struct hash_table
*vars
; /* nir_variable -> SpvId */
55 const SpvId
*block_ids
;
58 SpvId loop_break
, loop_cont
;
62 get_fvec_constant(struct ntv_context
*ctx
, int bit_size
, int num_components
,
63 const float values
[]);
66 get_uvec_constant(struct ntv_context
*ctx
, int bit_size
, int num_components
,
67 const uint32_t values
[]);
70 emit_unop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
, SpvId src
);
73 emit_binop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
,
74 SpvId src0
, SpvId src1
);
77 emit_triop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
,
78 SpvId src0
, SpvId src1
, SpvId src2
);
81 get_bvec_type(struct ntv_context
*ctx
, int num_components
)
83 SpvId bool_type
= spirv_builder_type_bool(&ctx
->builder
);
84 if (num_components
> 1)
85 return spirv_builder_type_vector(&ctx
->builder
, bool_type
,
88 assert(num_components
== 1);
93 block_label(struct ntv_context
*ctx
, nir_block
*block
)
95 assert(block
->index
< ctx
->num_blocks
);
96 return ctx
->block_ids
[block
->index
];
100 get_fvec_type(struct ntv_context
*ctx
, unsigned bit_size
, unsigned num_components
)
102 assert(bit_size
== 32); // only 32-bit floats supported so far
104 SpvId float_type
= spirv_builder_type_float(&ctx
->builder
, bit_size
);
105 if (num_components
> 1)
106 return spirv_builder_type_vector(&ctx
->builder
, float_type
,
109 assert(num_components
== 1);
114 get_ivec_type(struct ntv_context
*ctx
, unsigned bit_size
, unsigned num_components
)
116 assert(bit_size
== 32); // only 32-bit ints supported so far
118 SpvId int_type
= spirv_builder_type_int(&ctx
->builder
, bit_size
);
119 if (num_components
> 1)
120 return spirv_builder_type_vector(&ctx
->builder
, int_type
,
123 assert(num_components
== 1);
128 get_uvec_type(struct ntv_context
*ctx
, unsigned bit_size
, unsigned num_components
)
130 assert(bit_size
== 32); // only 32-bit uints supported so far
132 SpvId uint_type
= spirv_builder_type_uint(&ctx
->builder
, bit_size
);
133 if (num_components
> 1)
134 return spirv_builder_type_vector(&ctx
->builder
, uint_type
,
137 assert(num_components
== 1);
142 get_dest_uvec_type(struct ntv_context
*ctx
, nir_dest
*dest
)
144 return get_uvec_type(ctx
, nir_dest_bit_size(*dest
),
145 nir_dest_num_components(*dest
));
149 get_glsl_basetype(struct ntv_context
*ctx
, enum glsl_base_type type
)
152 case GLSL_TYPE_FLOAT
:
153 return spirv_builder_type_float(&ctx
->builder
, 32);
156 return spirv_builder_type_int(&ctx
->builder
, 32);
159 return spirv_builder_type_uint(&ctx
->builder
, 32);
160 /* TODO: handle more types */
163 unreachable("unknown GLSL type");
168 get_glsl_type(struct ntv_context
*ctx
, const struct glsl_type
*type
)
171 if (glsl_type_is_scalar(type
))
172 return get_glsl_basetype(ctx
, glsl_get_base_type(type
));
174 if (glsl_type_is_vector(type
))
175 return spirv_builder_type_vector(&ctx
->builder
,
176 get_glsl_basetype(ctx
, glsl_get_base_type(type
)),
177 glsl_get_vector_elements(type
));
179 if (glsl_type_is_array(type
)) {
180 SpvId ret
= spirv_builder_type_array(&ctx
->builder
,
181 get_glsl_type(ctx
, glsl_get_array_element(type
)),
182 spirv_builder_const_uint(&ctx
->builder
, 32, glsl_get_length(type
)));
183 uint32_t stride
= glsl_get_explicit_stride(type
);
185 spirv_builder_emit_array_stride(&ctx
->builder
, ret
, stride
);
190 unreachable("we shouldn't get here, I think...");
194 emit_input(struct ntv_context
*ctx
, struct nir_variable
*var
)
196 SpvId var_type
= get_glsl_type(ctx
, var
->type
);
197 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
198 SpvStorageClassInput
,
200 SpvId var_id
= spirv_builder_emit_var(&ctx
->builder
, pointer_type
,
201 SpvStorageClassInput
);
204 spirv_builder_emit_name(&ctx
->builder
, var_id
, var
->name
);
206 if (ctx
->stage
== MESA_SHADER_FRAGMENT
) {
207 if (var
->data
.location
>= VARYING_SLOT_VAR0
||
208 (var
->data
.location
>= VARYING_SLOT_COL0
&&
209 var
->data
.location
<= VARYING_SLOT_TEX7
)) {
210 spirv_builder_emit_location(&ctx
->builder
, var_id
,
211 ctx
->var_location
++);
213 switch (var
->data
.location
) {
214 case VARYING_SLOT_POS
:
215 spirv_builder_emit_builtin(&ctx
->builder
, var_id
, SpvBuiltInFragCoord
);
218 case VARYING_SLOT_PNTC
:
219 spirv_builder_emit_builtin(&ctx
->builder
, var_id
, SpvBuiltInPointCoord
);
223 debug_printf("unknown varying slot: %s\n", gl_varying_slot_name(var
->data
.location
));
224 unreachable("unexpected varying slot");
228 spirv_builder_emit_location(&ctx
->builder
, var_id
,
229 var
->data
.driver_location
);
232 if (var
->data
.location_frac
)
233 spirv_builder_emit_component(&ctx
->builder
, var_id
,
234 var
->data
.location_frac
);
236 if (var
->data
.interpolation
== INTERP_MODE_FLAT
)
237 spirv_builder_emit_decoration(&ctx
->builder
, var_id
, SpvDecorationFlat
);
239 _mesa_hash_table_insert(ctx
->vars
, var
, (void *)(intptr_t)var_id
);
241 assert(ctx
->num_entry_ifaces
< ARRAY_SIZE(ctx
->entry_ifaces
));
242 ctx
->entry_ifaces
[ctx
->num_entry_ifaces
++] = var_id
;
246 emit_output(struct ntv_context
*ctx
, struct nir_variable
*var
)
248 SpvId var_type
= get_glsl_type(ctx
, var
->type
);
249 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
250 SpvStorageClassOutput
,
252 SpvId var_id
= spirv_builder_emit_var(&ctx
->builder
, pointer_type
,
253 SpvStorageClassOutput
);
255 spirv_builder_emit_name(&ctx
->builder
, var_id
, var
->name
);
258 if (ctx
->stage
== MESA_SHADER_VERTEX
) {
259 if (var
->data
.location
>= VARYING_SLOT_VAR0
||
260 (var
->data
.location
>= VARYING_SLOT_COL0
&&
261 var
->data
.location
<= VARYING_SLOT_TEX7
)) {
262 spirv_builder_emit_location(&ctx
->builder
, var_id
,
263 ctx
->var_location
++);
265 switch (var
->data
.location
) {
266 case VARYING_SLOT_POS
:
267 spirv_builder_emit_builtin(&ctx
->builder
, var_id
, SpvBuiltInPosition
);
270 case VARYING_SLOT_PSIZ
:
271 spirv_builder_emit_builtin(&ctx
->builder
, var_id
, SpvBuiltInPointSize
);
274 case VARYING_SLOT_CLIP_DIST0
:
275 assert(glsl_type_is_array(var
->type
));
276 spirv_builder_emit_builtin(&ctx
->builder
, var_id
, SpvBuiltInClipDistance
);
280 debug_printf("unknown varying slot: %s\n", gl_varying_slot_name(var
->data
.location
));
281 unreachable("unexpected varying slot");
284 } else if (ctx
->stage
== MESA_SHADER_FRAGMENT
) {
285 if (var
->data
.location
>= FRAG_RESULT_DATA0
)
286 spirv_builder_emit_location(&ctx
->builder
, var_id
,
287 var
->data
.location
- FRAG_RESULT_DATA0
);
289 switch (var
->data
.location
) {
290 case FRAG_RESULT_COLOR
:
291 spirv_builder_emit_location(&ctx
->builder
, var_id
, 0);
294 case FRAG_RESULT_DEPTH
:
295 spirv_builder_emit_builtin(&ctx
->builder
, var_id
, SpvBuiltInFragDepth
);
299 spirv_builder_emit_location(&ctx
->builder
, var_id
,
300 var
->data
.driver_location
);
305 if (var
->data
.location_frac
)
306 spirv_builder_emit_component(&ctx
->builder
, var_id
,
307 var
->data
.location_frac
);
309 _mesa_hash_table_insert(ctx
->vars
, var
, (void *)(intptr_t)var_id
);
311 assert(ctx
->num_entry_ifaces
< ARRAY_SIZE(ctx
->entry_ifaces
));
312 ctx
->entry_ifaces
[ctx
->num_entry_ifaces
++] = var_id
;
316 type_to_dim(enum glsl_sampler_dim gdim
, bool *is_ms
)
320 case GLSL_SAMPLER_DIM_1D
:
322 case GLSL_SAMPLER_DIM_2D
:
324 case GLSL_SAMPLER_DIM_RECT
:
326 case GLSL_SAMPLER_DIM_CUBE
:
328 case GLSL_SAMPLER_DIM_3D
:
330 case GLSL_SAMPLER_DIM_MS
:
334 fprintf(stderr
, "unknown sampler type %d\n", gdim
);
341 emit_sampler(struct ntv_context
*ctx
, struct nir_variable
*var
)
344 SpvDim dimension
= type_to_dim(glsl_get_sampler_dim(var
->type
), &is_ms
);
345 SpvId float_type
= spirv_builder_type_float(&ctx
->builder
, 32);
346 SpvId image_type
= spirv_builder_type_image(&ctx
->builder
, float_type
,
347 dimension
, false, glsl_sampler_type_is_array(var
->type
), is_ms
, 1,
348 SpvImageFormatUnknown
);
350 SpvId sampled_type
= spirv_builder_type_sampled_image(&ctx
->builder
,
352 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
353 SpvStorageClassUniformConstant
,
355 SpvId var_id
= spirv_builder_emit_var(&ctx
->builder
, pointer_type
,
356 SpvStorageClassUniformConstant
);
359 spirv_builder_emit_name(&ctx
->builder
, var_id
, var
->name
);
361 assert(ctx
->num_samplers
< ARRAY_SIZE(ctx
->samplers
));
362 ctx
->samplers
[ctx
->num_samplers
++] = var_id
;
364 spirv_builder_emit_descriptor_set(&ctx
->builder
, var_id
,
365 var
->data
.descriptor_set
);
366 spirv_builder_emit_binding(&ctx
->builder
, var_id
, var
->data
.binding
);
370 emit_ubo(struct ntv_context
*ctx
, struct nir_variable
*var
)
372 uint32_t size
= glsl_count_attribute_slots(var
->type
, false);
373 SpvId vec4_type
= get_uvec_type(ctx
, 32, 4);
374 SpvId array_length
= spirv_builder_const_uint(&ctx
->builder
, 32, size
);
375 SpvId array_type
= spirv_builder_type_array(&ctx
->builder
, vec4_type
,
377 spirv_builder_emit_array_stride(&ctx
->builder
, array_type
, 16);
379 // wrap UBO-array in a struct
380 SpvId struct_type
= spirv_builder_type_struct(&ctx
->builder
, &array_type
, 1);
382 char struct_name
[100];
383 snprintf(struct_name
, sizeof(struct_name
), "struct_%s", var
->name
);
384 spirv_builder_emit_name(&ctx
->builder
, struct_type
, struct_name
);
387 spirv_builder_emit_decoration(&ctx
->builder
, struct_type
,
389 spirv_builder_emit_member_offset(&ctx
->builder
, struct_type
, 0, 0);
392 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
393 SpvStorageClassUniform
,
396 SpvId var_id
= spirv_builder_emit_var(&ctx
->builder
, pointer_type
,
397 SpvStorageClassUniform
);
399 spirv_builder_emit_name(&ctx
->builder
, var_id
, var
->name
);
401 assert(ctx
->num_ubos
< ARRAY_SIZE(ctx
->ubos
));
402 ctx
->ubos
[ctx
->num_ubos
++] = var_id
;
404 spirv_builder_emit_descriptor_set(&ctx
->builder
, var_id
,
405 var
->data
.descriptor_set
);
406 spirv_builder_emit_binding(&ctx
->builder
, var_id
, var
->data
.binding
);
410 emit_uniform(struct ntv_context
*ctx
, struct nir_variable
*var
)
412 if (var
->data
.mode
== nir_var_mem_ubo
)
415 assert(var
->data
.mode
== nir_var_uniform
);
416 if (glsl_type_is_sampler(var
->type
))
417 emit_sampler(ctx
, var
);
422 get_src_uint_ssa(struct ntv_context
*ctx
, const nir_ssa_def
*ssa
)
424 assert(ssa
->index
< ctx
->num_defs
);
425 assert(ctx
->defs
[ssa
->index
] != 0);
426 return ctx
->defs
[ssa
->index
];
430 get_var_from_reg(struct ntv_context
*ctx
, nir_register
*reg
)
432 assert(reg
->index
< ctx
->num_regs
);
433 assert(ctx
->regs
[reg
->index
] != 0);
434 return ctx
->regs
[reg
->index
];
438 get_src_uint_reg(struct ntv_context
*ctx
, const nir_reg_src
*reg
)
441 assert(!reg
->indirect
);
442 assert(!reg
->base_offset
);
444 SpvId var
= get_var_from_reg(ctx
, reg
->reg
);
445 SpvId type
= get_uvec_type(ctx
, reg
->reg
->bit_size
, reg
->reg
->num_components
);
446 return spirv_builder_emit_load(&ctx
->builder
, type
, var
);
450 get_src_uint(struct ntv_context
*ctx
, nir_src
*src
)
453 return get_src_uint_ssa(ctx
, src
->ssa
);
455 return get_src_uint_reg(ctx
, &src
->reg
);
459 get_alu_src_uint(struct ntv_context
*ctx
, nir_alu_instr
*alu
, unsigned src
)
461 assert(!alu
->src
[src
].negate
);
462 assert(!alu
->src
[src
].abs
);
464 SpvId def
= get_src_uint(ctx
, &alu
->src
[src
].src
);
466 unsigned used_channels
= 0;
467 bool need_swizzle
= false;
468 for (unsigned i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; i
++) {
469 if (!nir_alu_instr_channel_used(alu
, src
, i
))
474 if (alu
->src
[src
].swizzle
[i
] != i
)
477 assert(used_channels
!= 0);
479 unsigned live_channels
= nir_src_num_components(alu
->src
[src
].src
);
480 if (used_channels
!= live_channels
)
486 int bit_size
= nir_src_bit_size(alu
->src
[src
].src
);
487 assert(bit_size
== 32);
489 SpvId uint_type
= spirv_builder_type_uint(&ctx
->builder
, bit_size
);
490 if (used_channels
== 1) {
491 uint32_t indices
[] = { alu
->src
[src
].swizzle
[0] };
492 return spirv_builder_emit_composite_extract(&ctx
->builder
, uint_type
,
494 ARRAY_SIZE(indices
));
495 } else if (live_channels
== 1) {
496 SpvId uvec_type
= spirv_builder_type_vector(&ctx
->builder
, uint_type
,
499 SpvId constituents
[NIR_MAX_VEC_COMPONENTS
];
500 for (unsigned i
= 0; i
< used_channels
; ++i
)
501 constituents
[i
] = def
;
503 return spirv_builder_emit_composite_construct(&ctx
->builder
, uvec_type
,
507 SpvId uvec_type
= spirv_builder_type_vector(&ctx
->builder
, uint_type
,
510 uint32_t components
[NIR_MAX_VEC_COMPONENTS
];
511 size_t num_components
= 0;
512 for (unsigned i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; i
++) {
513 if (!nir_alu_instr_channel_used(alu
, src
, i
))
516 components
[num_components
++] = alu
->src
[src
].swizzle
[i
];
519 return spirv_builder_emit_vector_shuffle(&ctx
->builder
, uvec_type
,
520 def
, def
, components
, num_components
);
525 store_ssa_def_uint(struct ntv_context
*ctx
, nir_ssa_def
*ssa
, SpvId result
)
528 assert(ssa
->index
< ctx
->num_defs
);
529 ctx
->defs
[ssa
->index
] = result
;
533 bvec_to_uvec(struct ntv_context
*ctx
, SpvId value
, unsigned num_components
)
535 SpvId otype
= get_uvec_type(ctx
, 32, num_components
);
536 uint32_t zeros
[4] = { 0, 0, 0, 0 };
537 uint32_t ones
[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff };
538 SpvId zero
= get_uvec_constant(ctx
, 32, num_components
, zeros
);
539 SpvId one
= get_uvec_constant(ctx
, 32, num_components
, ones
);
540 return emit_triop(ctx
, SpvOpSelect
, otype
, value
, one
, zero
);
544 uvec_to_bvec(struct ntv_context
*ctx
, SpvId value
, unsigned num_components
)
546 SpvId type
= get_bvec_type(ctx
, num_components
);
548 uint32_t zeros
[NIR_MAX_VEC_COMPONENTS
] = { 0 };
549 SpvId zero
= get_uvec_constant(ctx
, 32, num_components
, zeros
);
551 return emit_binop(ctx
, SpvOpINotEqual
, type
, value
, zero
);
555 bitcast_to_uvec(struct ntv_context
*ctx
, SpvId value
, unsigned bit_size
,
556 unsigned num_components
)
558 SpvId type
= get_uvec_type(ctx
, bit_size
, num_components
);
559 return emit_unop(ctx
, SpvOpBitcast
, type
, value
);
563 bitcast_to_ivec(struct ntv_context
*ctx
, SpvId value
, unsigned bit_size
,
564 unsigned num_components
)
566 SpvId type
= get_ivec_type(ctx
, bit_size
, num_components
);
567 return emit_unop(ctx
, SpvOpBitcast
, type
, value
);
571 bitcast_to_fvec(struct ntv_context
*ctx
, SpvId value
, unsigned bit_size
,
572 unsigned num_components
)
574 SpvId type
= get_fvec_type(ctx
, bit_size
, num_components
);
575 return emit_unop(ctx
, SpvOpBitcast
, type
, value
);
579 store_reg_def(struct ntv_context
*ctx
, nir_reg_dest
*reg
, SpvId result
)
581 SpvId var
= get_var_from_reg(ctx
, reg
->reg
);
583 spirv_builder_emit_store(&ctx
->builder
, var
, result
);
587 store_dest_uint(struct ntv_context
*ctx
, nir_dest
*dest
, SpvId result
)
590 store_ssa_def_uint(ctx
, &dest
->ssa
, result
);
592 store_reg_def(ctx
, &dest
->reg
, result
);
596 store_dest(struct ntv_context
*ctx
, nir_dest
*dest
, SpvId result
, nir_alu_type type
)
598 unsigned num_components
= nir_dest_num_components(*dest
);
599 unsigned bit_size
= nir_dest_bit_size(*dest
);
601 switch (nir_alu_type_get_base_type(type
)) {
603 assert(bit_size
== 1);
604 result
= bvec_to_uvec(ctx
, result
, num_components
);
608 break; /* nothing to do! */
612 result
= bitcast_to_uvec(ctx
, result
, bit_size
, num_components
);
616 unreachable("unsupported nir_alu_type");
619 store_dest_uint(ctx
, dest
, result
);
623 emit_unop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
, SpvId src
)
625 return spirv_builder_emit_unop(&ctx
->builder
, op
, type
, src
);
629 emit_binop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
,
630 SpvId src0
, SpvId src1
)
632 return spirv_builder_emit_binop(&ctx
->builder
, op
, type
, src0
, src1
);
636 emit_triop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
,
637 SpvId src0
, SpvId src1
, SpvId src2
)
639 return spirv_builder_emit_triop(&ctx
->builder
, op
, type
, src0
, src1
, src2
);
643 emit_builtin_unop(struct ntv_context
*ctx
, enum GLSLstd450 op
, SpvId type
,
646 SpvId args
[] = { src
};
647 return spirv_builder_emit_ext_inst(&ctx
->builder
, type
, ctx
->GLSL_std_450
,
648 op
, args
, ARRAY_SIZE(args
));
652 emit_builtin_binop(struct ntv_context
*ctx
, enum GLSLstd450 op
, SpvId type
,
653 SpvId src0
, SpvId src1
)
655 SpvId args
[] = { src0
, src1
};
656 return spirv_builder_emit_ext_inst(&ctx
->builder
, type
, ctx
->GLSL_std_450
,
657 op
, args
, ARRAY_SIZE(args
));
661 get_fvec_constant(struct ntv_context
*ctx
, int bit_size
, int num_components
,
662 const float values
[])
664 assert(bit_size
== 32);
666 if (num_components
> 1) {
667 SpvId components
[num_components
];
668 for (int i
= 0; i
< num_components
; i
++)
669 components
[i
] = spirv_builder_const_float(&ctx
->builder
, bit_size
,
672 SpvId type
= get_fvec_type(ctx
, bit_size
, num_components
);
673 return spirv_builder_const_composite(&ctx
->builder
, type
, components
,
677 assert(num_components
== 1);
678 return spirv_builder_const_float(&ctx
->builder
, bit_size
, values
[0]);
682 get_uvec_constant(struct ntv_context
*ctx
, int bit_size
, int num_components
,
683 const uint32_t values
[])
685 assert(bit_size
== 32);
687 if (num_components
> 1) {
688 SpvId components
[num_components
];
689 for (int i
= 0; i
< num_components
; i
++)
690 components
[i
] = spirv_builder_const_uint(&ctx
->builder
, bit_size
,
693 SpvId type
= get_uvec_type(ctx
, bit_size
, num_components
);
694 return spirv_builder_const_composite(&ctx
->builder
, type
, components
,
698 assert(num_components
== 1);
699 return spirv_builder_const_uint(&ctx
->builder
, bit_size
, values
[0]);
702 static inline unsigned
703 alu_instr_src_components(const nir_alu_instr
*instr
, unsigned src
)
705 if (nir_op_infos
[instr
->op
].input_sizes
[src
] > 0)
706 return nir_op_infos
[instr
->op
].input_sizes
[src
];
708 if (instr
->dest
.dest
.is_ssa
)
709 return instr
->dest
.dest
.ssa
.num_components
;
711 return instr
->dest
.dest
.reg
.reg
->num_components
;
715 get_alu_src(struct ntv_context
*ctx
, nir_alu_instr
*alu
, unsigned src
)
717 SpvId uint_value
= get_alu_src_uint(ctx
, alu
, src
);
719 unsigned num_components
= alu_instr_src_components(alu
, src
);
720 unsigned bit_size
= nir_src_bit_size(alu
->src
[src
].src
);
721 nir_alu_type type
= nir_op_infos
[alu
->op
].input_types
[src
];
723 switch (nir_alu_type_get_base_type(type
)) {
725 assert(bit_size
== 1);
726 return uvec_to_bvec(ctx
, uint_value
, num_components
);
729 return bitcast_to_ivec(ctx
, uint_value
, bit_size
, num_components
);
735 return bitcast_to_fvec(ctx
, uint_value
, bit_size
, num_components
);
738 unreachable("unknown nir_alu_type");
743 store_alu_result(struct ntv_context
*ctx
, nir_alu_instr
*alu
, SpvId result
)
745 assert(!alu
->dest
.saturate
);
746 return store_dest(ctx
, &alu
->dest
.dest
, result
, nir_op_infos
[alu
->op
].output_type
);
750 get_dest_type(struct ntv_context
*ctx
, nir_dest
*dest
, nir_alu_type type
)
752 unsigned num_components
= nir_dest_num_components(*dest
);
753 unsigned bit_size
= nir_dest_bit_size(*dest
);
755 switch (nir_alu_type_get_base_type(type
)) {
757 return get_bvec_type(ctx
, num_components
);
760 return get_ivec_type(ctx
, bit_size
, num_components
);
763 return get_uvec_type(ctx
, bit_size
, num_components
);
766 return get_fvec_type(ctx
, bit_size
, num_components
);
769 unreachable("unsupported nir_alu_type");
774 emit_alu(struct ntv_context
*ctx
, nir_alu_instr
*alu
)
776 SpvId src
[nir_op_infos
[alu
->op
].num_inputs
];
777 for (unsigned i
= 0; i
< nir_op_infos
[alu
->op
].num_inputs
; i
++)
778 src
[i
] = get_alu_src(ctx
, alu
, i
);
780 SpvId dest_type
= get_dest_type(ctx
, &alu
->dest
.dest
,
781 nir_op_infos
[alu
->op
].output_type
);
782 unsigned bit_size
= nir_dest_bit_size(alu
->dest
.dest
);
783 unsigned num_components
= nir_dest_num_components(alu
->dest
.dest
);
788 assert(nir_op_infos
[alu
->op
].num_inputs
== 1);
792 #define UNOP(nir_op, spirv_op) \
794 assert(nir_op_infos[alu->op].num_inputs == 1); \
795 result = emit_unop(ctx, spirv_op, dest_type, src[0]); \
798 UNOP(nir_op_ineg
, SpvOpSNegate
)
799 UNOP(nir_op_fneg
, SpvOpFNegate
)
800 UNOP(nir_op_fddx
, SpvOpDPdx
)
801 UNOP(nir_op_fddy
, SpvOpDPdy
)
802 UNOP(nir_op_f2i32
, SpvOpConvertFToS
)
803 UNOP(nir_op_f2u32
, SpvOpConvertFToU
)
804 UNOP(nir_op_i2f32
, SpvOpConvertSToF
)
805 UNOP(nir_op_u2f32
, SpvOpConvertUToF
)
806 UNOP(nir_op_inot
, SpvOpNot
)
810 assert(nir_op_infos
[alu
->op
].num_inputs
== 1);
811 result
= bvec_to_uvec(ctx
, src
[0], num_components
);
814 #define BUILTIN_UNOP(nir_op, spirv_op) \
816 assert(nir_op_infos[alu->op].num_inputs == 1); \
817 result = emit_builtin_unop(ctx, spirv_op, dest_type, src[0]); \
820 BUILTIN_UNOP(nir_op_fabs
, GLSLstd450FAbs
)
821 BUILTIN_UNOP(nir_op_fsqrt
, GLSLstd450Sqrt
)
822 BUILTIN_UNOP(nir_op_frsq
, GLSLstd450InverseSqrt
)
823 BUILTIN_UNOP(nir_op_flog2
, GLSLstd450Log2
)
824 BUILTIN_UNOP(nir_op_fexp2
, GLSLstd450Exp2
)
825 BUILTIN_UNOP(nir_op_ffract
, GLSLstd450Fract
)
826 BUILTIN_UNOP(nir_op_ffloor
, GLSLstd450Floor
)
827 BUILTIN_UNOP(nir_op_fceil
, GLSLstd450Ceil
)
828 BUILTIN_UNOP(nir_op_ftrunc
, GLSLstd450Trunc
)
829 BUILTIN_UNOP(nir_op_fround_even
, GLSLstd450RoundEven
)
830 BUILTIN_UNOP(nir_op_fsign
, GLSLstd450FSign
)
831 BUILTIN_UNOP(nir_op_fsin
, GLSLstd450Sin
)
832 BUILTIN_UNOP(nir_op_fcos
, GLSLstd450Cos
)
836 assert(nir_op_infos
[alu
->op
].num_inputs
== 1);
837 float one
[4] = { 1, 1, 1, 1 };
839 src
[0] = get_fvec_constant(ctx
, bit_size
, num_components
, one
);
840 result
= emit_binop(ctx
, SpvOpFDiv
, dest_type
, src
[0], src
[1]);
845 assert(nir_op_infos
[alu
->op
].num_inputs
== 1);
846 float values
[NIR_MAX_VEC_COMPONENTS
] = { 0 };
847 SpvId zero
= get_fvec_constant(ctx
, nir_src_bit_size(alu
->src
[0].src
),
848 num_components
, values
);
849 result
= emit_binop(ctx
, SpvOpFOrdNotEqual
, dest_type
, src
[0], zero
);
853 #define BINOP(nir_op, spirv_op) \
855 assert(nir_op_infos[alu->op].num_inputs == 2); \
856 result = emit_binop(ctx, spirv_op, dest_type, src[0], src[1]); \
859 BINOP(nir_op_iadd
, SpvOpIAdd
)
860 BINOP(nir_op_isub
, SpvOpISub
)
861 BINOP(nir_op_imul
, SpvOpIMul
)
862 BINOP(nir_op_idiv
, SpvOpSDiv
)
863 BINOP(nir_op_udiv
, SpvOpUDiv
)
864 BINOP(nir_op_fadd
, SpvOpFAdd
)
865 BINOP(nir_op_fsub
, SpvOpFSub
)
866 BINOP(nir_op_fmul
, SpvOpFMul
)
867 BINOP(nir_op_fdiv
, SpvOpFDiv
)
868 BINOP(nir_op_fmod
, SpvOpFMod
)
869 BINOP(nir_op_ilt
, SpvOpSLessThan
)
870 BINOP(nir_op_ige
, SpvOpSGreaterThanEqual
)
871 BINOP(nir_op_ieq
, SpvOpIEqual
)
872 BINOP(nir_op_ine
, SpvOpINotEqual
)
873 BINOP(nir_op_flt
, SpvOpFOrdLessThan
)
874 BINOP(nir_op_fge
, SpvOpFOrdGreaterThanEqual
)
875 BINOP(nir_op_feq
, SpvOpFOrdEqual
)
876 BINOP(nir_op_fne
, SpvOpFOrdNotEqual
)
877 BINOP(nir_op_ishl
, SpvOpShiftLeftLogical
)
878 BINOP(nir_op_ishr
, SpvOpShiftRightArithmetic
)
879 BINOP(nir_op_ushr
, SpvOpShiftRightLogical
)
880 BINOP(nir_op_iand
, SpvOpBitwiseAnd
)
881 BINOP(nir_op_ior
, SpvOpBitwiseOr
)
884 #define BUILTIN_BINOP(nir_op, spirv_op) \
886 assert(nir_op_infos[alu->op].num_inputs == 2); \
887 result = emit_builtin_binop(ctx, spirv_op, dest_type, src[0], src[1]); \
890 BUILTIN_BINOP(nir_op_fmin
, GLSLstd450FMin
)
891 BUILTIN_BINOP(nir_op_fmax
, GLSLstd450FMax
)
897 assert(nir_op_infos
[alu
->op
].num_inputs
== 2);
898 result
= emit_binop(ctx
, SpvOpDot
, dest_type
, src
[0], src
[1]);
905 assert(nir_op_infos
[alu
->op
].num_inputs
== 2);
906 int num_components
= nir_dest_num_components(alu
->dest
.dest
);
907 SpvId bool_type
= get_bvec_type(ctx
, num_components
);
909 SpvId zero
= spirv_builder_const_float(&ctx
->builder
, 32, 0.0f
);
910 SpvId one
= spirv_builder_const_float(&ctx
->builder
, 32, 1.0f
);
911 if (num_components
> 1) {
912 SpvId zero_comps
[num_components
], one_comps
[num_components
];
913 for (int i
= 0; i
< num_components
; i
++) {
914 zero_comps
[i
] = zero
;
918 zero
= spirv_builder_const_composite(&ctx
->builder
, dest_type
,
919 zero_comps
, num_components
);
920 one
= spirv_builder_const_composite(&ctx
->builder
, dest_type
,
921 one_comps
, num_components
);
926 case nir_op_seq
: op
= SpvOpFOrdEqual
; break;
927 case nir_op_sne
: op
= SpvOpFOrdNotEqual
; break;
928 case nir_op_slt
: op
= SpvOpFOrdLessThan
; break;
929 case nir_op_sge
: op
= SpvOpFOrdGreaterThanEqual
; break;
930 default: unreachable("unexpected op");
933 result
= emit_binop(ctx
, op
, bool_type
, src
[0], src
[1]);
934 result
= emit_triop(ctx
, SpvOpSelect
, dest_type
, result
, one
, zero
);
939 assert(nir_op_infos
[alu
->op
].num_inputs
== 3);
940 int num_components
= nir_dest_num_components(alu
->dest
.dest
);
941 SpvId bool_type
= get_bvec_type(ctx
, num_components
);
943 float zero
[4] = { 0, 0, 0, 0 };
944 SpvId cmp
= get_fvec_constant(ctx
, nir_src_bit_size(alu
->src
[0].src
),
945 num_components
, zero
);
947 result
= emit_binop(ctx
, SpvOpFOrdGreaterThan
, bool_type
, src
[0], cmp
);
948 result
= emit_triop(ctx
, SpvOpSelect
, dest_type
, result
, src
[1], src
[2]);
953 assert(nir_op_infos
[alu
->op
].num_inputs
== 3);
954 result
= emit_triop(ctx
, SpvOpSelect
, dest_type
, src
[0], src
[1], src
[2]);
960 int num_inputs
= nir_op_infos
[alu
->op
].num_inputs
;
961 assert(2 <= num_inputs
&& num_inputs
<= 4);
962 result
= spirv_builder_emit_composite_construct(&ctx
->builder
, dest_type
,
968 fprintf(stderr
, "emit_alu: not implemented (%s)\n",
969 nir_op_infos
[alu
->op
].name
);
971 unreachable("unsupported opcode");
975 store_alu_result(ctx
, alu
, result
);
979 emit_load_const(struct ntv_context
*ctx
, nir_load_const_instr
*load_const
)
981 uint32_t values
[NIR_MAX_VEC_COMPONENTS
];
982 for (int i
= 0; i
< load_const
->def
.num_components
; ++i
)
983 values
[i
] = load_const
->value
[i
].u32
;
985 SpvId constant
= get_uvec_constant(ctx
, load_const
->def
.bit_size
,
986 load_const
->def
.num_components
,
988 store_ssa_def_uint(ctx
, &load_const
->def
, constant
);
992 emit_load_ubo(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
994 nir_const_value
*const_block_index
= nir_src_as_const_value(intr
->src
[0]);
995 assert(const_block_index
); // no dynamic indexing for now
996 assert(const_block_index
->u32
== 0); // we only support the default UBO for now
998 nir_const_value
*const_offset
= nir_src_as_const_value(intr
->src
[1]);
1000 SpvId uvec4_type
= get_uvec_type(ctx
, 32, 4);
1001 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
1002 SpvStorageClassUniform
,
1005 unsigned idx
= const_offset
->u32
;
1006 SpvId member
= spirv_builder_const_uint(&ctx
->builder
, 32, 0);
1007 SpvId offset
= spirv_builder_const_uint(&ctx
->builder
, 32, idx
);
1008 SpvId offsets
[] = { member
, offset
};
1009 SpvId ptr
= spirv_builder_emit_access_chain(&ctx
->builder
, pointer_type
,
1010 ctx
->ubos
[0], offsets
,
1011 ARRAY_SIZE(offsets
));
1012 SpvId result
= spirv_builder_emit_load(&ctx
->builder
, uvec4_type
, ptr
);
1014 SpvId type
= get_dest_uvec_type(ctx
, &intr
->dest
);
1015 unsigned num_components
= nir_dest_num_components(intr
->dest
);
1016 if (num_components
== 1) {
1017 uint32_t components
[] = { 0 };
1018 result
= spirv_builder_emit_composite_extract(&ctx
->builder
,
1022 } else if (num_components
< 4) {
1023 SpvId constituents
[num_components
];
1024 SpvId uint_type
= spirv_builder_type_uint(&ctx
->builder
, 32);
1025 for (uint32_t i
= 0; i
< num_components
; ++i
)
1026 constituents
[i
] = spirv_builder_emit_composite_extract(&ctx
->builder
,
1031 result
= spirv_builder_emit_composite_construct(&ctx
->builder
,
1037 store_dest_uint(ctx
, &intr
->dest
, result
);
1039 unreachable("uniform-addressing not yet supported");
1043 emit_discard(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
1045 assert(ctx
->block_started
);
1046 spirv_builder_emit_kill(&ctx
->builder
);
1047 /* discard is weird in NIR, so let's just create an unreachable block after
1048 it and hope that the vulkan driver will DCE any instructinos in it. */
1049 spirv_builder_label(&ctx
->builder
, spirv_builder_new_id(&ctx
->builder
));
1053 emit_load_deref(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
1055 /* uint is a bit of a lie here; it's really just a pointer */
1056 SpvId ptr
= get_src_uint(ctx
, intr
->src
);
1058 nir_variable
*var
= nir_intrinsic_get_var(intr
, 0);
1059 SpvId result
= spirv_builder_emit_load(&ctx
->builder
,
1060 get_glsl_type(ctx
, var
->type
),
1062 unsigned num_components
= nir_dest_num_components(intr
->dest
);
1063 unsigned bit_size
= nir_dest_bit_size(intr
->dest
);
1064 result
= bitcast_to_uvec(ctx
, result
, bit_size
, num_components
);
1065 store_dest_uint(ctx
, &intr
->dest
, result
);
1069 emit_store_deref(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
1071 /* uint is a bit of a lie here; it's really just a pointer */
1072 SpvId ptr
= get_src_uint(ctx
, &intr
->src
[0]);
1073 SpvId src
= get_src_uint(ctx
, &intr
->src
[1]);
1075 nir_variable
*var
= nir_intrinsic_get_var(intr
, 0);
1076 SpvId result
= emit_unop(ctx
, SpvOpBitcast
,
1077 get_glsl_type(ctx
, glsl_without_array(var
->type
)),
1079 spirv_builder_emit_store(&ctx
->builder
, ptr
, result
);
1083 emit_intrinsic(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
1085 switch (intr
->intrinsic
) {
1086 case nir_intrinsic_load_ubo
:
1087 emit_load_ubo(ctx
, intr
);
1090 case nir_intrinsic_discard
:
1091 emit_discard(ctx
, intr
);
1094 case nir_intrinsic_load_deref
:
1095 emit_load_deref(ctx
, intr
);
1098 case nir_intrinsic_store_deref
:
1099 emit_store_deref(ctx
, intr
);
1103 fprintf(stderr
, "emit_intrinsic: not implemented (%s)\n",
1104 nir_intrinsic_infos
[intr
->intrinsic
].name
);
1105 unreachable("unsupported intrinsic");
1110 emit_undef(struct ntv_context
*ctx
, nir_ssa_undef_instr
*undef
)
1112 SpvId type
= get_uvec_type(ctx
, undef
->def
.bit_size
,
1113 undef
->def
.num_components
);
1115 store_ssa_def_uint(ctx
, &undef
->def
,
1116 spirv_builder_emit_undef(&ctx
->builder
, type
));
1120 get_src_float(struct ntv_context
*ctx
, nir_src
*src
)
1122 SpvId def
= get_src_uint(ctx
, src
);
1123 unsigned num_components
= nir_src_num_components(*src
);
1124 unsigned bit_size
= nir_src_bit_size(*src
);
1125 return bitcast_to_fvec(ctx
, def
, bit_size
, num_components
);
1129 emit_tex(struct ntv_context
*ctx
, nir_tex_instr
*tex
)
1131 assert(tex
->op
== nir_texop_tex
||
1132 tex
->op
== nir_texop_txb
||
1133 tex
->op
== nir_texop_txl
);
1134 assert(nir_alu_type_get_base_type(tex
->dest_type
) == nir_type_float
);
1135 assert(tex
->texture_index
== tex
->sampler_index
);
1137 SpvId coord
= 0, proj
= 0, bias
= 0, lod
= 0, dref
= 0;
1138 unsigned coord_components
;
1139 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
1140 switch (tex
->src
[i
].src_type
) {
1141 case nir_tex_src_coord
:
1142 coord
= get_src_float(ctx
, &tex
->src
[i
].src
);
1143 coord_components
= nir_src_num_components(tex
->src
[i
].src
);
1146 case nir_tex_src_projector
:
1147 assert(nir_src_num_components(tex
->src
[i
].src
) == 1);
1148 proj
= get_src_float(ctx
, &tex
->src
[i
].src
);
1152 case nir_tex_src_bias
:
1153 assert(tex
->op
== nir_texop_txb
);
1154 bias
= get_src_float(ctx
, &tex
->src
[i
].src
);
1158 case nir_tex_src_lod
:
1159 assert(nir_src_num_components(tex
->src
[i
].src
) == 1);
1160 lod
= get_src_float(ctx
, &tex
->src
[i
].src
);
1164 case nir_tex_src_comparator
:
1165 assert(nir_src_num_components(tex
->src
[i
].src
) == 1);
1166 dref
= get_src_float(ctx
, &tex
->src
[i
].src
);
1171 fprintf(stderr
, "texture source: %d\n", tex
->src
[i
].src_type
);
1172 unreachable("unknown texture source");
1176 if (lod
== 0 && ctx
->stage
!= MESA_SHADER_FRAGMENT
) {
1177 lod
= spirv_builder_const_float(&ctx
->builder
, 32, 0);
1182 SpvDim dimension
= type_to_dim(tex
->sampler_dim
, &is_ms
);
1183 SpvId float_type
= spirv_builder_type_float(&ctx
->builder
, 32);
1184 SpvId image_type
= spirv_builder_type_image(&ctx
->builder
, float_type
,
1185 dimension
, false, tex
->is_array
, is_ms
, 1,
1186 SpvImageFormatUnknown
);
1187 SpvId sampled_type
= spirv_builder_type_sampled_image(&ctx
->builder
,
1190 assert(tex
->texture_index
< ctx
->num_samplers
);
1191 SpvId load
= spirv_builder_emit_load(&ctx
->builder
, sampled_type
,
1192 ctx
->samplers
[tex
->texture_index
]);
1194 SpvId dest_type
= get_dest_type(ctx
, &tex
->dest
, tex
->dest_type
);
1197 SpvId constituents
[coord_components
+ 1];
1198 if (coord_components
== 1)
1199 constituents
[0] = coord
;
1201 assert(coord_components
> 1);
1202 SpvId float_type
= spirv_builder_type_float(&ctx
->builder
, 32);
1203 for (uint32_t i
= 0; i
< coord_components
; ++i
)
1204 constituents
[i
] = spirv_builder_emit_composite_extract(&ctx
->builder
,
1210 constituents
[coord_components
++] = proj
;
1212 SpvId vec_type
= get_fvec_type(ctx
, 32, coord_components
);
1213 coord
= spirv_builder_emit_composite_construct(&ctx
->builder
,
1219 SpvId actual_dest_type
= dest_type
;
1221 actual_dest_type
= float_type
;
1223 SpvId result
= spirv_builder_emit_image_sample(&ctx
->builder
,
1224 actual_dest_type
, load
,
1228 spirv_builder_emit_decoration(&ctx
->builder
, result
,
1229 SpvDecorationRelaxedPrecision
);
1232 SpvId components
[4] = { result
, result
, result
, result
};
1233 result
= spirv_builder_emit_composite_construct(&ctx
->builder
,
1239 store_dest(ctx
, &tex
->dest
, result
, tex
->dest_type
);
1243 start_block(struct ntv_context
*ctx
, SpvId label
)
1245 /* terminate previous block if needed */
1246 if (ctx
->block_started
)
1247 spirv_builder_emit_branch(&ctx
->builder
, label
);
1249 /* start new block */
1250 spirv_builder_label(&ctx
->builder
, label
);
1251 ctx
->block_started
= true;
1255 branch(struct ntv_context
*ctx
, SpvId label
)
1257 assert(ctx
->block_started
);
1258 spirv_builder_emit_branch(&ctx
->builder
, label
);
1259 ctx
->block_started
= false;
1263 branch_conditional(struct ntv_context
*ctx
, SpvId condition
, SpvId then_id
,
1266 assert(ctx
->block_started
);
1267 spirv_builder_emit_branch_conditional(&ctx
->builder
, condition
,
1269 ctx
->block_started
= false;
1273 emit_jump(struct ntv_context
*ctx
, nir_jump_instr
*jump
)
1275 switch (jump
->type
) {
1276 case nir_jump_break
:
1277 assert(ctx
->loop_break
);
1278 branch(ctx
, ctx
->loop_break
);
1281 case nir_jump_continue
:
1282 assert(ctx
->loop_cont
);
1283 branch(ctx
, ctx
->loop_cont
);
1287 unreachable("Unsupported jump type\n");
1292 emit_deref_var(struct ntv_context
*ctx
, nir_deref_instr
*deref
)
1294 assert(deref
->deref_type
== nir_deref_type_var
);
1296 struct hash_entry
*he
= _mesa_hash_table_search(ctx
->vars
, deref
->var
);
1298 SpvId result
= (SpvId
)(intptr_t)he
->data
;
1299 /* uint is a bit of a lie here, it's really just an opaque type */
1300 store_dest_uint(ctx
, &deref
->dest
, result
);
1304 emit_deref_array(struct ntv_context
*ctx
, nir_deref_instr
*deref
)
1306 assert(deref
->deref_type
== nir_deref_type_array
);
1307 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
1309 SpvStorageClass storage_class
;
1310 switch (var
->data
.mode
) {
1311 case nir_var_shader_in
:
1312 storage_class
= SpvStorageClassInput
;
1315 case nir_var_shader_out
:
1316 storage_class
= SpvStorageClassOutput
;
1320 unreachable("Unsupported nir_variable_mode\n");
1323 SpvId index
= get_src_uint(ctx
, &deref
->arr
.index
);
1325 SpvId ptr_type
= spirv_builder_type_pointer(&ctx
->builder
,
1327 get_glsl_type(ctx
, deref
->type
));
1329 SpvId result
= spirv_builder_emit_access_chain(&ctx
->builder
,
1331 get_src_uint(ctx
, &deref
->parent
),
1333 /* uint is a bit of a lie here, it's really just an opaque type */
1334 store_dest_uint(ctx
, &deref
->dest
, result
);
1338 emit_deref(struct ntv_context
*ctx
, nir_deref_instr
*deref
)
1340 switch (deref
->deref_type
) {
1341 case nir_deref_type_var
:
1342 emit_deref_var(ctx
, deref
);
1345 case nir_deref_type_array
:
1346 emit_deref_array(ctx
, deref
);
1350 unreachable("unexpected deref_type");
1355 emit_block(struct ntv_context
*ctx
, struct nir_block
*block
)
1357 start_block(ctx
, block_label(ctx
, block
));
1358 nir_foreach_instr(instr
, block
) {
1359 switch (instr
->type
) {
1360 case nir_instr_type_alu
:
1361 emit_alu(ctx
, nir_instr_as_alu(instr
));
1363 case nir_instr_type_intrinsic
:
1364 emit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
1366 case nir_instr_type_load_const
:
1367 emit_load_const(ctx
, nir_instr_as_load_const(instr
));
1369 case nir_instr_type_ssa_undef
:
1370 emit_undef(ctx
, nir_instr_as_ssa_undef(instr
));
1372 case nir_instr_type_tex
:
1373 emit_tex(ctx
, nir_instr_as_tex(instr
));
1375 case nir_instr_type_phi
:
1376 unreachable("nir_instr_type_phi not supported");
1378 case nir_instr_type_jump
:
1379 emit_jump(ctx
, nir_instr_as_jump(instr
));
1381 case nir_instr_type_call
:
1382 unreachable("nir_instr_type_call not supported");
1384 case nir_instr_type_parallel_copy
:
1385 unreachable("nir_instr_type_parallel_copy not supported");
1387 case nir_instr_type_deref
:
1388 emit_deref(ctx
, nir_instr_as_deref(instr
));
1395 emit_cf_list(struct ntv_context
*ctx
, struct exec_list
*list
);
1398 get_src_bool(struct ntv_context
*ctx
, nir_src
*src
)
1400 SpvId def
= get_src_uint(ctx
, src
);
1401 assert(nir_src_bit_size(*src
) == 32);
1402 unsigned num_components
= nir_src_num_components(*src
);
1403 return uvec_to_bvec(ctx
, def
, num_components
);
1407 emit_if(struct ntv_context
*ctx
, nir_if
*if_stmt
)
1409 SpvId condition
= get_src_bool(ctx
, &if_stmt
->condition
);
1411 SpvId header_id
= spirv_builder_new_id(&ctx
->builder
);
1412 SpvId then_id
= block_label(ctx
, nir_if_first_then_block(if_stmt
));
1413 SpvId endif_id
= spirv_builder_new_id(&ctx
->builder
);
1414 SpvId else_id
= endif_id
;
1416 bool has_else
= !exec_list_is_empty(&if_stmt
->else_list
);
1418 assert(nir_if_first_else_block(if_stmt
)->index
< ctx
->num_blocks
);
1419 else_id
= block_label(ctx
, nir_if_first_else_block(if_stmt
));
1422 /* create a header-block */
1423 start_block(ctx
, header_id
);
1424 spirv_builder_emit_selection_merge(&ctx
->builder
, endif_id
,
1425 SpvSelectionControlMaskNone
);
1426 branch_conditional(ctx
, condition
, then_id
, else_id
);
1428 emit_cf_list(ctx
, &if_stmt
->then_list
);
1431 if (ctx
->block_started
)
1432 branch(ctx
, endif_id
);
1434 emit_cf_list(ctx
, &if_stmt
->else_list
);
1437 start_block(ctx
, endif_id
);
1441 emit_loop(struct ntv_context
*ctx
, nir_loop
*loop
)
1443 SpvId header_id
= spirv_builder_new_id(&ctx
->builder
);
1444 SpvId begin_id
= block_label(ctx
, nir_loop_first_block(loop
));
1445 SpvId break_id
= spirv_builder_new_id(&ctx
->builder
);
1446 SpvId cont_id
= spirv_builder_new_id(&ctx
->builder
);
1448 /* create a header-block */
1449 start_block(ctx
, header_id
);
1450 spirv_builder_loop_merge(&ctx
->builder
, break_id
, cont_id
, SpvLoopControlMaskNone
);
1451 branch(ctx
, begin_id
);
1453 SpvId save_break
= ctx
->loop_break
;
1454 SpvId save_cont
= ctx
->loop_cont
;
1455 ctx
->loop_break
= break_id
;
1456 ctx
->loop_cont
= cont_id
;
1458 emit_cf_list(ctx
, &loop
->body
);
1460 ctx
->loop_break
= save_break
;
1461 ctx
->loop_cont
= save_cont
;
1463 branch(ctx
, cont_id
);
1464 start_block(ctx
, cont_id
);
1465 branch(ctx
, header_id
);
1467 start_block(ctx
, break_id
);
1471 emit_cf_list(struct ntv_context
*ctx
, struct exec_list
*list
)
1473 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
1474 switch (node
->type
) {
1475 case nir_cf_node_block
:
1476 emit_block(ctx
, nir_cf_node_as_block(node
));
1479 case nir_cf_node_if
:
1480 emit_if(ctx
, nir_cf_node_as_if(node
));
1483 case nir_cf_node_loop
:
1484 emit_loop(ctx
, nir_cf_node_as_loop(node
));
1487 case nir_cf_node_function
:
1488 unreachable("nir_cf_node_function not supported");
1494 struct spirv_shader
*
1495 nir_to_spirv(struct nir_shader
*s
)
1497 struct spirv_shader
*ret
= NULL
;
1499 struct ntv_context ctx
= {};
1501 switch (s
->info
.stage
) {
1502 case MESA_SHADER_VERTEX
:
1503 case MESA_SHADER_FRAGMENT
:
1504 case MESA_SHADER_COMPUTE
:
1505 spirv_builder_emit_cap(&ctx
.builder
, SpvCapabilityShader
);
1508 case MESA_SHADER_TESS_CTRL
:
1509 case MESA_SHADER_TESS_EVAL
:
1510 spirv_builder_emit_cap(&ctx
.builder
, SpvCapabilityTessellation
);
1513 case MESA_SHADER_GEOMETRY
:
1514 spirv_builder_emit_cap(&ctx
.builder
, SpvCapabilityGeometry
);
1518 unreachable("invalid stage");
1521 // TODO: only enable when needed
1522 if (s
->info
.stage
== MESA_SHADER_FRAGMENT
)
1523 spirv_builder_emit_cap(&ctx
.builder
, SpvCapabilitySampled1D
);
1525 ctx
.stage
= s
->info
.stage
;
1526 ctx
.GLSL_std_450
= spirv_builder_import(&ctx
.builder
, "GLSL.std.450");
1527 spirv_builder_emit_source(&ctx
.builder
, SpvSourceLanguageGLSL
, 450);
1529 spirv_builder_emit_mem_model(&ctx
.builder
, SpvAddressingModelLogical
,
1530 SpvMemoryModelGLSL450
);
1532 SpvExecutionModel exec_model
;
1533 switch (s
->info
.stage
) {
1534 case MESA_SHADER_VERTEX
:
1535 exec_model
= SpvExecutionModelVertex
;
1537 case MESA_SHADER_TESS_CTRL
:
1538 exec_model
= SpvExecutionModelTessellationControl
;
1540 case MESA_SHADER_TESS_EVAL
:
1541 exec_model
= SpvExecutionModelTessellationEvaluation
;
1543 case MESA_SHADER_GEOMETRY
:
1544 exec_model
= SpvExecutionModelGeometry
;
1546 case MESA_SHADER_FRAGMENT
:
1547 exec_model
= SpvExecutionModelFragment
;
1549 case MESA_SHADER_COMPUTE
:
1550 exec_model
= SpvExecutionModelGLCompute
;
1553 unreachable("invalid stage");
1556 SpvId type_void
= spirv_builder_type_void(&ctx
.builder
);
1557 SpvId type_main
= spirv_builder_type_function(&ctx
.builder
, type_void
,
1559 SpvId entry_point
= spirv_builder_new_id(&ctx
.builder
);
1560 spirv_builder_emit_name(&ctx
.builder
, entry_point
, "main");
1562 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
1563 _mesa_key_pointer_equal
);
1565 nir_foreach_variable(var
, &s
->inputs
)
1566 emit_input(&ctx
, var
);
1568 nir_foreach_variable(var
, &s
->outputs
)
1569 emit_output(&ctx
, var
);
1571 nir_foreach_variable(var
, &s
->uniforms
)
1572 emit_uniform(&ctx
, var
);
1574 spirv_builder_emit_entry_point(&ctx
.builder
, exec_model
, entry_point
,
1575 "main", ctx
.entry_ifaces
,
1576 ctx
.num_entry_ifaces
);
1577 if (s
->info
.stage
== MESA_SHADER_FRAGMENT
) {
1578 spirv_builder_emit_exec_mode(&ctx
.builder
, entry_point
,
1579 SpvExecutionModeOriginUpperLeft
);
1580 if (s
->info
.outputs_written
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
))
1581 spirv_builder_emit_exec_mode(&ctx
.builder
, entry_point
,
1582 SpvExecutionModeDepthReplacing
);
1586 spirv_builder_function(&ctx
.builder
, entry_point
, type_void
,
1587 SpvFunctionControlMaskNone
,
1590 nir_function_impl
*entry
= nir_shader_get_entrypoint(s
);
1591 nir_metadata_require(entry
, nir_metadata_block_index
);
1593 ctx
.defs
= (SpvId
*)malloc(sizeof(SpvId
) * entry
->ssa_alloc
);
1596 ctx
.num_defs
= entry
->ssa_alloc
;
1598 nir_index_local_regs(entry
);
1599 ctx
.regs
= malloc(sizeof(SpvId
) * entry
->reg_alloc
);
1602 ctx
.num_regs
= entry
->reg_alloc
;
1604 SpvId
*block_ids
= (SpvId
*)malloc(sizeof(SpvId
) * entry
->num_blocks
);
1608 for (int i
= 0; i
< entry
->num_blocks
; ++i
)
1609 block_ids
[i
] = spirv_builder_new_id(&ctx
.builder
);
1611 ctx
.block_ids
= block_ids
;
1612 ctx
.num_blocks
= entry
->num_blocks
;
1614 /* emit a block only for the variable declarations */
1615 start_block(&ctx
, spirv_builder_new_id(&ctx
.builder
));
1616 foreach_list_typed(nir_register
, reg
, node
, &entry
->registers
) {
1617 SpvId type
= get_uvec_type(&ctx
, reg
->bit_size
, reg
->num_components
);
1618 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
.builder
,
1619 SpvStorageClassFunction
,
1621 SpvId var
= spirv_builder_emit_var(&ctx
.builder
, pointer_type
,
1622 SpvStorageClassFunction
);
1624 ctx
.regs
[reg
->index
] = var
;
1627 emit_cf_list(&ctx
, &entry
->body
);
1631 spirv_builder_return(&ctx
.builder
); // doesn't belong here, but whatevz
1632 spirv_builder_function_end(&ctx
.builder
);
1634 size_t num_words
= spirv_builder_get_num_words(&ctx
.builder
);
1636 ret
= CALLOC_STRUCT(spirv_shader
);
1640 ret
->words
= MALLOC(sizeof(uint32_t) * num_words
);
1644 ret
->num_words
= spirv_builder_get_words(&ctx
.builder
, ret
->words
, num_words
);
1645 assert(ret
->num_words
== num_words
);
1652 spirv_shader_delete(ret
);
1655 _mesa_hash_table_destroy(ctx
.vars
, NULL
);
1661 spirv_shader_delete(struct spirv_shader
*s
)