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"
32 /* this consistently maps slots to a zero-indexed value to avoid wasting slots */
33 static unsigned slot_pack_map
[] = {
34 /* Position is builtin */
35 [VARYING_SLOT_POS
] = UINT_MAX
,
36 [VARYING_SLOT_COL0
] = 0, /* input/output */
37 [VARYING_SLOT_COL1
] = 1, /* input/output */
38 [VARYING_SLOT_FOGC
] = 2, /* input/output */
39 /* TEX0-7 are deprecated, so we put them at the end of the range and hope nobody uses them all */
40 [VARYING_SLOT_TEX0
] = VARYING_SLOT_VAR0
- 1, /* input/output */
41 [VARYING_SLOT_TEX1
] = VARYING_SLOT_VAR0
- 2,
42 [VARYING_SLOT_TEX2
] = VARYING_SLOT_VAR0
- 3,
43 [VARYING_SLOT_TEX3
] = VARYING_SLOT_VAR0
- 4,
44 [VARYING_SLOT_TEX4
] = VARYING_SLOT_VAR0
- 5,
45 [VARYING_SLOT_TEX5
] = VARYING_SLOT_VAR0
- 6,
46 [VARYING_SLOT_TEX6
] = VARYING_SLOT_VAR0
- 7,
47 [VARYING_SLOT_TEX7
] = VARYING_SLOT_VAR0
- 8,
49 /* PointSize is builtin */
50 [VARYING_SLOT_PSIZ
] = UINT_MAX
,
52 [VARYING_SLOT_BFC0
] = 3, /* output only */
53 [VARYING_SLOT_BFC1
] = 4, /* output only */
54 [VARYING_SLOT_EDGE
] = 5, /* output only */
55 [VARYING_SLOT_CLIP_VERTEX
] = 6, /* output only */
57 /* ClipDistance is builtin */
58 [VARYING_SLOT_CLIP_DIST0
] = UINT_MAX
,
59 [VARYING_SLOT_CLIP_DIST1
] = UINT_MAX
,
61 /* CullDistance is builtin */
62 [VARYING_SLOT_CULL_DIST0
] = UINT_MAX
, /* input/output */
63 [VARYING_SLOT_CULL_DIST1
] = UINT_MAX
, /* never actually used */
65 /* PrimitiveId is builtin */
66 [VARYING_SLOT_PRIMITIVE_ID
] = UINT_MAX
,
68 /* Layer is builtin */
69 [VARYING_SLOT_LAYER
] = UINT_MAX
, /* input/output */
71 /* ViewportIndex is builtin */
72 [VARYING_SLOT_VIEWPORT
] = UINT_MAX
, /* input/output */
74 /* FrontFacing is builtin */
75 [VARYING_SLOT_FACE
] = UINT_MAX
,
77 /* PointCoord is builtin */
78 [VARYING_SLOT_PNTC
] = UINT_MAX
, /* input only */
80 /* TessLevelOuter is builtin */
81 [VARYING_SLOT_TESS_LEVEL_OUTER
] = UINT_MAX
,
82 /* TessLevelInner is builtin */
83 [VARYING_SLOT_TESS_LEVEL_INNER
] = UINT_MAX
,
85 [VARYING_SLOT_BOUNDING_BOX0
] = 7, /* Only appears as TCS output. */
86 [VARYING_SLOT_BOUNDING_BOX1
] = 8, /* Only appears as TCS output. */
87 [VARYING_SLOT_VIEW_INDEX
] = 9, /* input/output */
88 [VARYING_SLOT_VIEWPORT_MASK
] = 10, /* output only */
90 #define NTV_MIN_RESERVED_SLOTS 11
93 struct spirv_builder builder
;
97 gl_shader_stage stage
;
101 SpvId image_types
[PIPE_MAX_SAMPLERS
];
102 SpvId samplers
[PIPE_MAX_SAMPLERS
];
103 unsigned samplers_used
: PIPE_MAX_SAMPLERS
;
104 SpvId entry_ifaces
[PIPE_MAX_SHADER_INPUTS
* 4 + PIPE_MAX_SHADER_OUTPUTS
* 4];
105 size_t num_entry_ifaces
;
113 struct hash_table
*vars
; /* nir_variable -> SpvId */
114 struct hash_table
*so_outputs
; /* pipe_stream_output -> SpvId */
115 unsigned outputs
[VARYING_SLOT_MAX
];
116 const struct glsl_type
*so_output_gl_types
[VARYING_SLOT_MAX
];
117 SpvId so_output_types
[VARYING_SLOT_MAX
];
119 const SpvId
*block_ids
;
122 SpvId loop_break
, loop_cont
;
124 SpvId front_face_var
, instance_id_var
, vertex_id_var
;
126 bool seen_texcoord
[8]; //whether we've seen a VARYING_SLOT_TEX[n] this pass
131 get_fvec_constant(struct ntv_context
*ctx
, unsigned bit_size
,
132 unsigned num_components
, float value
);
135 get_uvec_constant(struct ntv_context
*ctx
, unsigned bit_size
,
136 unsigned num_components
, uint32_t value
);
139 get_ivec_constant(struct ntv_context
*ctx
, unsigned bit_size
,
140 unsigned num_components
, int32_t value
);
143 emit_unop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
, SpvId src
);
146 emit_binop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
,
147 SpvId src0
, SpvId src1
);
150 emit_triop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
,
151 SpvId src0
, SpvId src1
, SpvId src2
);
154 get_bvec_type(struct ntv_context
*ctx
, int num_components
)
156 SpvId bool_type
= spirv_builder_type_bool(&ctx
->builder
);
157 if (num_components
> 1)
158 return spirv_builder_type_vector(&ctx
->builder
, bool_type
,
161 assert(num_components
== 1);
166 block_label(struct ntv_context
*ctx
, nir_block
*block
)
168 assert(block
->index
< ctx
->num_blocks
);
169 return ctx
->block_ids
[block
->index
];
173 emit_float_const(struct ntv_context
*ctx
, int bit_size
, float value
)
175 assert(bit_size
== 32);
176 return spirv_builder_const_float(&ctx
->builder
, bit_size
, value
);
180 emit_uint_const(struct ntv_context
*ctx
, int bit_size
, uint32_t value
)
182 assert(bit_size
== 32);
183 return spirv_builder_const_uint(&ctx
->builder
, bit_size
, value
);
187 emit_int_const(struct ntv_context
*ctx
, int bit_size
, int32_t value
)
189 assert(bit_size
== 32);
190 return spirv_builder_const_int(&ctx
->builder
, bit_size
, value
);
194 get_fvec_type(struct ntv_context
*ctx
, unsigned bit_size
, unsigned num_components
)
196 assert(bit_size
== 32); // only 32-bit floats supported so far
198 SpvId float_type
= spirv_builder_type_float(&ctx
->builder
, bit_size
);
199 if (num_components
> 1)
200 return spirv_builder_type_vector(&ctx
->builder
, float_type
,
203 assert(num_components
== 1);
208 get_ivec_type(struct ntv_context
*ctx
, unsigned bit_size
, unsigned num_components
)
210 assert(bit_size
== 32); // only 32-bit ints supported so far
212 SpvId int_type
= spirv_builder_type_int(&ctx
->builder
, bit_size
);
213 if (num_components
> 1)
214 return spirv_builder_type_vector(&ctx
->builder
, int_type
,
217 assert(num_components
== 1);
222 get_uvec_type(struct ntv_context
*ctx
, unsigned bit_size
, unsigned num_components
)
224 assert(bit_size
== 32); // only 32-bit uints supported so far
226 SpvId uint_type
= spirv_builder_type_uint(&ctx
->builder
, bit_size
);
227 if (num_components
> 1)
228 return spirv_builder_type_vector(&ctx
->builder
, uint_type
,
231 assert(num_components
== 1);
236 get_dest_uvec_type(struct ntv_context
*ctx
, nir_dest
*dest
)
238 unsigned bit_size
= MAX2(nir_dest_bit_size(*dest
), 32);
239 return get_uvec_type(ctx
, bit_size
, nir_dest_num_components(*dest
));
243 get_glsl_basetype(struct ntv_context
*ctx
, enum glsl_base_type type
)
247 return spirv_builder_type_bool(&ctx
->builder
);
249 case GLSL_TYPE_FLOAT
:
250 return spirv_builder_type_float(&ctx
->builder
, 32);
253 return spirv_builder_type_int(&ctx
->builder
, 32);
256 return spirv_builder_type_uint(&ctx
->builder
, 32);
257 /* TODO: handle more types */
260 unreachable("unknown GLSL type");
265 get_glsl_type(struct ntv_context
*ctx
, const struct glsl_type
*type
)
268 if (glsl_type_is_scalar(type
))
269 return get_glsl_basetype(ctx
, glsl_get_base_type(type
));
271 if (glsl_type_is_vector(type
))
272 return spirv_builder_type_vector(&ctx
->builder
,
273 get_glsl_basetype(ctx
, glsl_get_base_type(type
)),
274 glsl_get_vector_elements(type
));
276 if (glsl_type_is_array(type
)) {
277 SpvId ret
= spirv_builder_type_array(&ctx
->builder
,
278 get_glsl_type(ctx
, glsl_get_array_element(type
)),
279 emit_uint_const(ctx
, 32, glsl_get_length(type
)));
280 uint32_t stride
= glsl_get_explicit_stride(type
);
282 spirv_builder_emit_array_stride(&ctx
->builder
, ret
, stride
);
287 unreachable("we shouldn't get here, I think...");
290 static inline unsigned
291 handle_slot(struct ntv_context
*ctx
, unsigned slot
)
293 unsigned orig
= slot
;
294 if (slot
< VARYING_SLOT_VAR0
) {
296 if (slot
>= VARYING_SLOT_TEX0
&& slot
<= VARYING_SLOT_TEX7
)
297 ctx
->seen_texcoord
[slot
- VARYING_SLOT_TEX0
] = true;
299 slot
= slot_pack_map
[slot
];
300 if (slot
== UINT_MAX
)
301 debug_printf("unhandled varying slot: %s\n", gl_varying_slot_name(orig
));
303 slot
-= VARYING_SLOT_VAR0
- NTV_MIN_RESERVED_SLOTS
;
304 assert(slot
<= VARYING_SLOT_VAR0
- 8 ||
305 !ctx
->seen_texcoord
[VARYING_SLOT_VAR0
- slot
- 1]);
308 assert(slot
< VARYING_SLOT_VAR0
);
312 #define HANDLE_EMIT_BUILTIN(SLOT, BUILTIN) \
313 case VARYING_SLOT_##SLOT: \
314 spirv_builder_emit_builtin(&ctx->builder, var_id, SpvBuiltIn##BUILTIN); \
319 emit_input(struct ntv_context
*ctx
, struct nir_variable
*var
)
321 SpvId var_type
= get_glsl_type(ctx
, var
->type
);
322 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
323 SpvStorageClassInput
,
325 SpvId var_id
= spirv_builder_emit_var(&ctx
->builder
, pointer_type
,
326 SpvStorageClassInput
);
329 spirv_builder_emit_name(&ctx
->builder
, var_id
, var
->name
);
331 if (ctx
->stage
== MESA_SHADER_FRAGMENT
) {
332 unsigned slot
= var
->data
.location
;
334 HANDLE_EMIT_BUILTIN(POS
, FragCoord
);
335 HANDLE_EMIT_BUILTIN(PNTC
, PointCoord
);
336 HANDLE_EMIT_BUILTIN(LAYER
, Layer
);
337 HANDLE_EMIT_BUILTIN(PRIMITIVE_ID
, PrimitiveId
);
338 HANDLE_EMIT_BUILTIN(CLIP_DIST0
, ClipDistance
);
339 HANDLE_EMIT_BUILTIN(CULL_DIST0
, CullDistance
);
340 HANDLE_EMIT_BUILTIN(VIEWPORT
, ViewportIndex
);
341 HANDLE_EMIT_BUILTIN(FACE
, FrontFacing
);
344 slot
= handle_slot(ctx
, slot
);
345 spirv_builder_emit_location(&ctx
->builder
, var_id
, slot
);
348 spirv_builder_emit_location(&ctx
->builder
, var_id
,
349 var
->data
.driver_location
);
352 if (var
->data
.location_frac
)
353 spirv_builder_emit_component(&ctx
->builder
, var_id
,
354 var
->data
.location_frac
);
356 if (var
->data
.interpolation
== INTERP_MODE_FLAT
)
357 spirv_builder_emit_decoration(&ctx
->builder
, var_id
, SpvDecorationFlat
);
359 _mesa_hash_table_insert(ctx
->vars
, var
, (void *)(intptr_t)var_id
);
361 assert(ctx
->num_entry_ifaces
< ARRAY_SIZE(ctx
->entry_ifaces
));
362 ctx
->entry_ifaces
[ctx
->num_entry_ifaces
++] = var_id
;
366 emit_output(struct ntv_context
*ctx
, struct nir_variable
*var
)
368 SpvId var_type
= get_glsl_type(ctx
, var
->type
);
369 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
370 SpvStorageClassOutput
,
372 SpvId var_id
= spirv_builder_emit_var(&ctx
->builder
, pointer_type
,
373 SpvStorageClassOutput
);
375 spirv_builder_emit_name(&ctx
->builder
, var_id
, var
->name
);
378 if (ctx
->stage
== MESA_SHADER_VERTEX
) {
379 unsigned slot
= var
->data
.location
;
381 HANDLE_EMIT_BUILTIN(POS
, Position
);
382 HANDLE_EMIT_BUILTIN(PSIZ
, PointSize
);
383 HANDLE_EMIT_BUILTIN(LAYER
, Layer
);
384 HANDLE_EMIT_BUILTIN(PRIMITIVE_ID
, PrimitiveId
);
385 HANDLE_EMIT_BUILTIN(CULL_DIST0
, CullDistance
);
386 HANDLE_EMIT_BUILTIN(VIEWPORT
, ViewportIndex
);
387 HANDLE_EMIT_BUILTIN(TESS_LEVEL_OUTER
, TessLevelOuter
);
388 HANDLE_EMIT_BUILTIN(TESS_LEVEL_INNER
, TessLevelInner
);
390 case VARYING_SLOT_CLIP_DIST0
:
391 assert(glsl_type_is_array(var
->type
));
392 spirv_builder_emit_builtin(&ctx
->builder
, var_id
, SpvBuiltInClipDistance
);
393 /* this can be as large as 2x vec4, which requires 2 slots */
394 ctx
->outputs
[VARYING_SLOT_CLIP_DIST1
] = var_id
;
395 ctx
->so_output_gl_types
[VARYING_SLOT_CLIP_DIST1
] = var
->type
;
396 ctx
->so_output_types
[VARYING_SLOT_CLIP_DIST1
] = var_type
;
400 slot
= handle_slot(ctx
, slot
);
401 spirv_builder_emit_location(&ctx
->builder
, var_id
, slot
);
403 ctx
->outputs
[var
->data
.location
] = var_id
;
404 ctx
->so_output_gl_types
[var
->data
.location
] = var
->type
;
405 ctx
->so_output_types
[var
->data
.location
] = var_type
;
406 } else if (ctx
->stage
== MESA_SHADER_FRAGMENT
) {
407 if (var
->data
.location
>= FRAG_RESULT_DATA0
) {
408 spirv_builder_emit_location(&ctx
->builder
, var_id
,
409 var
->data
.location
- FRAG_RESULT_DATA0
);
410 spirv_builder_emit_index(&ctx
->builder
, var_id
, var
->data
.index
);
412 switch (var
->data
.location
) {
413 case FRAG_RESULT_COLOR
:
414 unreachable("gl_FragColor should be lowered by now");
416 case FRAG_RESULT_DEPTH
:
417 spirv_builder_emit_builtin(&ctx
->builder
, var_id
, SpvBuiltInFragDepth
);
421 spirv_builder_emit_location(&ctx
->builder
, var_id
,
422 var
->data
.driver_location
);
423 spirv_builder_emit_index(&ctx
->builder
, var_id
, var
->data
.index
);
428 if (var
->data
.location_frac
)
429 spirv_builder_emit_component(&ctx
->builder
, var_id
,
430 var
->data
.location_frac
);
432 switch (var
->data
.interpolation
) {
433 case INTERP_MODE_NONE
:
434 case INTERP_MODE_SMOOTH
: /* XXX spirv doesn't seem to have anything for this */
436 case INTERP_MODE_FLAT
:
437 spirv_builder_emit_decoration(&ctx
->builder
, var_id
, SpvDecorationFlat
);
439 case INTERP_MODE_EXPLICIT
:
440 spirv_builder_emit_decoration(&ctx
->builder
, var_id
, SpvDecorationExplicitInterpAMD
);
442 case INTERP_MODE_NOPERSPECTIVE
:
443 spirv_builder_emit_decoration(&ctx
->builder
, var_id
, SpvDecorationNoPerspective
);
446 unreachable("unknown interpolation value");
449 _mesa_hash_table_insert(ctx
->vars
, var
, (void *)(intptr_t)var_id
);
451 assert(ctx
->num_entry_ifaces
< ARRAY_SIZE(ctx
->entry_ifaces
));
452 ctx
->entry_ifaces
[ctx
->num_entry_ifaces
++] = var_id
;
456 type_to_dim(enum glsl_sampler_dim gdim
, bool *is_ms
)
460 case GLSL_SAMPLER_DIM_1D
:
462 case GLSL_SAMPLER_DIM_2D
:
464 case GLSL_SAMPLER_DIM_3D
:
466 case GLSL_SAMPLER_DIM_CUBE
:
468 case GLSL_SAMPLER_DIM_RECT
:
470 case GLSL_SAMPLER_DIM_BUF
:
472 case GLSL_SAMPLER_DIM_EXTERNAL
:
473 return SpvDim2D
; /* seems dodgy... */
474 case GLSL_SAMPLER_DIM_MS
:
478 fprintf(stderr
, "unknown sampler type %d\n", gdim
);
485 zink_binding(gl_shader_stage stage
, VkDescriptorType type
, int index
)
487 if (stage
== MESA_SHADER_NONE
||
488 stage
>= MESA_SHADER_COMPUTE
) {
489 unreachable("not supported");
491 uint32_t stage_offset
= (uint32_t)stage
* (PIPE_MAX_CONSTANT_BUFFERS
+
492 PIPE_MAX_SHADER_SAMPLER_VIEWS
);
495 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
496 assert(index
< PIPE_MAX_CONSTANT_BUFFERS
);
497 return stage_offset
+ index
;
499 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
500 assert(index
< PIPE_MAX_SHADER_SAMPLER_VIEWS
);
501 return stage_offset
+ PIPE_MAX_CONSTANT_BUFFERS
+ index
;
504 unreachable("unexpected type");
510 emit_sampler(struct ntv_context
*ctx
, struct nir_variable
*var
)
512 const struct glsl_type
*type
= glsl_without_array(var
->type
);
515 SpvDim dimension
= type_to_dim(glsl_get_sampler_dim(type
), &is_ms
);
517 SpvId result_type
= get_glsl_basetype(ctx
, glsl_get_sampler_result_type(type
));
518 SpvId image_type
= spirv_builder_type_image(&ctx
->builder
, result_type
,
520 glsl_sampler_type_is_array(type
),
522 SpvImageFormatUnknown
);
524 SpvId sampled_type
= spirv_builder_type_sampled_image(&ctx
->builder
,
526 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
527 SpvStorageClassUniformConstant
,
530 if (glsl_type_is_array(var
->type
)) {
531 for (int i
= 0; i
< glsl_get_length(var
->type
); ++i
) {
532 SpvId var_id
= spirv_builder_emit_var(&ctx
->builder
, pointer_type
,
533 SpvStorageClassUniformConstant
);
536 char element_name
[100];
537 snprintf(element_name
, sizeof(element_name
), "%s_%d", var
->name
, i
);
538 spirv_builder_emit_name(&ctx
->builder
, var_id
, var
->name
);
541 int index
= var
->data
.binding
+ i
;
542 assert(!(ctx
->samplers_used
& (1 << index
)));
543 assert(!ctx
->image_types
[index
]);
544 ctx
->image_types
[index
] = image_type
;
545 ctx
->samplers
[index
] = var_id
;
546 ctx
->samplers_used
|= 1 << index
;
548 spirv_builder_emit_descriptor_set(&ctx
->builder
, var_id
,
549 var
->data
.descriptor_set
);
550 int binding
= zink_binding(ctx
->stage
,
551 VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
,
552 var
->data
.binding
+ i
);
553 spirv_builder_emit_binding(&ctx
->builder
, var_id
, binding
);
556 SpvId var_id
= spirv_builder_emit_var(&ctx
->builder
, pointer_type
,
557 SpvStorageClassUniformConstant
);
560 spirv_builder_emit_name(&ctx
->builder
, var_id
, var
->name
);
562 int index
= var
->data
.binding
;
563 assert(!(ctx
->samplers_used
& (1 << index
)));
564 assert(!ctx
->image_types
[index
]);
565 ctx
->image_types
[index
] = image_type
;
566 ctx
->samplers
[index
] = var_id
;
567 ctx
->samplers_used
|= 1 << index
;
569 spirv_builder_emit_descriptor_set(&ctx
->builder
, var_id
,
570 var
->data
.descriptor_set
);
571 int binding
= zink_binding(ctx
->stage
,
572 VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
,
574 spirv_builder_emit_binding(&ctx
->builder
, var_id
, binding
);
579 emit_ubo(struct ntv_context
*ctx
, struct nir_variable
*var
)
581 uint32_t size
= glsl_count_attribute_slots(var
->type
, false);
582 SpvId vec4_type
= get_uvec_type(ctx
, 32, 4);
583 SpvId array_length
= emit_uint_const(ctx
, 32, size
);
584 SpvId array_type
= spirv_builder_type_array(&ctx
->builder
, vec4_type
,
586 spirv_builder_emit_array_stride(&ctx
->builder
, array_type
, 16);
588 // wrap UBO-array in a struct
589 SpvId struct_type
= spirv_builder_type_struct(&ctx
->builder
, &array_type
, 1);
591 char struct_name
[100];
592 snprintf(struct_name
, sizeof(struct_name
), "struct_%s", var
->name
);
593 spirv_builder_emit_name(&ctx
->builder
, struct_type
, struct_name
);
596 spirv_builder_emit_decoration(&ctx
->builder
, struct_type
,
598 spirv_builder_emit_member_offset(&ctx
->builder
, struct_type
, 0, 0);
601 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
602 SpvStorageClassUniform
,
605 SpvId var_id
= spirv_builder_emit_var(&ctx
->builder
, pointer_type
,
606 SpvStorageClassUniform
);
608 spirv_builder_emit_name(&ctx
->builder
, var_id
, var
->name
);
610 assert(ctx
->num_ubos
< ARRAY_SIZE(ctx
->ubos
));
611 ctx
->ubos
[ctx
->num_ubos
++] = var_id
;
613 spirv_builder_emit_descriptor_set(&ctx
->builder
, var_id
,
614 var
->data
.descriptor_set
);
615 int binding
= zink_binding(ctx
->stage
,
616 VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
,
618 spirv_builder_emit_binding(&ctx
->builder
, var_id
, binding
);
622 emit_uniform(struct ntv_context
*ctx
, struct nir_variable
*var
)
624 if (var
->data
.mode
== nir_var_mem_ubo
)
627 assert(var
->data
.mode
== nir_var_uniform
);
628 if (glsl_type_is_sampler(glsl_without_array(var
->type
)))
629 emit_sampler(ctx
, var
);
634 get_src_ssa(struct ntv_context
*ctx
, const nir_ssa_def
*ssa
)
636 assert(ssa
->index
< ctx
->num_defs
);
637 assert(ctx
->defs
[ssa
->index
] != 0);
638 return ctx
->defs
[ssa
->index
];
642 get_var_from_reg(struct ntv_context
*ctx
, nir_register
*reg
)
644 assert(reg
->index
< ctx
->num_regs
);
645 assert(ctx
->regs
[reg
->index
] != 0);
646 return ctx
->regs
[reg
->index
];
650 get_src_reg(struct ntv_context
*ctx
, const nir_reg_src
*reg
)
653 assert(!reg
->indirect
);
654 assert(!reg
->base_offset
);
656 SpvId var
= get_var_from_reg(ctx
, reg
->reg
);
657 SpvId type
= get_uvec_type(ctx
, reg
->reg
->bit_size
, reg
->reg
->num_components
);
658 return spirv_builder_emit_load(&ctx
->builder
, type
, var
);
662 get_src(struct ntv_context
*ctx
, nir_src
*src
)
665 return get_src_ssa(ctx
, src
->ssa
);
667 return get_src_reg(ctx
, &src
->reg
);
671 get_alu_src_raw(struct ntv_context
*ctx
, nir_alu_instr
*alu
, unsigned src
)
673 assert(!alu
->src
[src
].negate
);
674 assert(!alu
->src
[src
].abs
);
676 SpvId def
= get_src(ctx
, &alu
->src
[src
].src
);
678 unsigned used_channels
= 0;
679 bool need_swizzle
= false;
680 for (unsigned i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; i
++) {
681 if (!nir_alu_instr_channel_used(alu
, src
, i
))
686 if (alu
->src
[src
].swizzle
[i
] != i
)
689 assert(used_channels
!= 0);
691 unsigned live_channels
= nir_src_num_components(alu
->src
[src
].src
);
692 if (used_channels
!= live_channels
)
698 int bit_size
= nir_src_bit_size(alu
->src
[src
].src
);
699 assert(bit_size
== 1 || bit_size
== 32);
701 SpvId raw_type
= bit_size
== 1 ? spirv_builder_type_bool(&ctx
->builder
) :
702 spirv_builder_type_uint(&ctx
->builder
, bit_size
);
704 if (used_channels
== 1) {
705 uint32_t indices
[] = { alu
->src
[src
].swizzle
[0] };
706 return spirv_builder_emit_composite_extract(&ctx
->builder
, raw_type
,
708 ARRAY_SIZE(indices
));
709 } else if (live_channels
== 1) {
710 SpvId raw_vec_type
= spirv_builder_type_vector(&ctx
->builder
,
714 SpvId constituents
[NIR_MAX_VEC_COMPONENTS
] = {0};
715 for (unsigned i
= 0; i
< used_channels
; ++i
)
716 constituents
[i
] = def
;
718 return spirv_builder_emit_composite_construct(&ctx
->builder
,
723 SpvId raw_vec_type
= spirv_builder_type_vector(&ctx
->builder
,
727 uint32_t components
[NIR_MAX_VEC_COMPONENTS
] = {0};
728 size_t num_components
= 0;
729 for (unsigned i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; i
++) {
730 if (!nir_alu_instr_channel_used(alu
, src
, i
))
733 components
[num_components
++] = alu
->src
[src
].swizzle
[i
];
736 return spirv_builder_emit_vector_shuffle(&ctx
->builder
, raw_vec_type
,
737 def
, def
, components
,
743 store_ssa_def(struct ntv_context
*ctx
, nir_ssa_def
*ssa
, SpvId result
)
746 assert(ssa
->index
< ctx
->num_defs
);
747 ctx
->defs
[ssa
->index
] = result
;
751 emit_select(struct ntv_context
*ctx
, SpvId type
, SpvId cond
,
752 SpvId if_true
, SpvId if_false
)
754 return emit_triop(ctx
, SpvOpSelect
, type
, cond
, if_true
, if_false
);
758 uvec_to_bvec(struct ntv_context
*ctx
, SpvId value
, unsigned num_components
)
760 SpvId type
= get_bvec_type(ctx
, num_components
);
761 SpvId zero
= get_uvec_constant(ctx
, 32, num_components
, 0);
762 return emit_binop(ctx
, SpvOpINotEqual
, type
, value
, zero
);
766 emit_bitcast(struct ntv_context
*ctx
, SpvId type
, SpvId value
)
768 return emit_unop(ctx
, SpvOpBitcast
, type
, value
);
772 bitcast_to_uvec(struct ntv_context
*ctx
, SpvId value
, unsigned bit_size
,
773 unsigned num_components
)
775 SpvId type
= get_uvec_type(ctx
, bit_size
, num_components
);
776 return emit_bitcast(ctx
, type
, value
);
780 bitcast_to_ivec(struct ntv_context
*ctx
, SpvId value
, unsigned bit_size
,
781 unsigned num_components
)
783 SpvId type
= get_ivec_type(ctx
, bit_size
, num_components
);
784 return emit_bitcast(ctx
, type
, value
);
788 bitcast_to_fvec(struct ntv_context
*ctx
, SpvId value
, unsigned bit_size
,
789 unsigned num_components
)
791 SpvId type
= get_fvec_type(ctx
, bit_size
, num_components
);
792 return emit_bitcast(ctx
, type
, value
);
796 store_reg_def(struct ntv_context
*ctx
, nir_reg_dest
*reg
, SpvId result
)
798 SpvId var
= get_var_from_reg(ctx
, reg
->reg
);
800 spirv_builder_emit_store(&ctx
->builder
, var
, result
);
804 store_dest_raw(struct ntv_context
*ctx
, nir_dest
*dest
, SpvId result
)
807 store_ssa_def(ctx
, &dest
->ssa
, result
);
809 store_reg_def(ctx
, &dest
->reg
, result
);
813 store_dest(struct ntv_context
*ctx
, nir_dest
*dest
, SpvId result
, nir_alu_type type
)
815 unsigned num_components
= nir_dest_num_components(*dest
);
816 unsigned bit_size
= nir_dest_bit_size(*dest
);
819 switch (nir_alu_type_get_base_type(type
)) {
821 assert("bool should have bit-size 1");
824 break; /* nothing to do! */
828 result
= bitcast_to_uvec(ctx
, result
, bit_size
, num_components
);
832 unreachable("unsupported nir_alu_type");
836 store_dest_raw(ctx
, dest
, result
);
841 emit_unop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
, SpvId src
)
843 return spirv_builder_emit_unop(&ctx
->builder
, op
, type
, src
);
846 /* return the intended xfb output vec type based on base type and vector size */
848 get_output_type(struct ntv_context
*ctx
, unsigned register_index
, unsigned num_components
)
850 const struct glsl_type
*out_type
= ctx
->so_output_gl_types
[register_index
];
851 enum glsl_base_type base_type
= glsl_get_base_type(out_type
);
852 if (base_type
== GLSL_TYPE_ARRAY
)
853 base_type
= glsl_get_base_type(glsl_without_array(out_type
));
857 return get_bvec_type(ctx
, num_components
);
859 case GLSL_TYPE_FLOAT
:
860 return get_fvec_type(ctx
, 32, num_components
);
863 return get_ivec_type(ctx
, 32, num_components
);
866 return get_uvec_type(ctx
, 32, num_components
);
871 unreachable("unknown type");
875 /* for streamout create new outputs, as streamout can be done on individual components,
876 from complete outputs, so we just can't use the created packed outputs */
878 emit_so_info(struct ntv_context
*ctx
, unsigned max_output_location
,
879 const struct pipe_stream_output_info
*so_info
, struct pipe_stream_output_info
*local_so_info
)
881 for (unsigned i
= 0; i
< local_so_info
->num_outputs
; i
++) {
882 struct pipe_stream_output so_output
= local_so_info
->output
[i
];
883 SpvId out_type
= get_output_type(ctx
, so_output
.register_index
, so_output
.num_components
);
884 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
885 SpvStorageClassOutput
,
887 SpvId var_id
= spirv_builder_emit_var(&ctx
->builder
, pointer_type
,
888 SpvStorageClassOutput
);
891 snprintf(name
, 10, "xfb%d", i
);
892 spirv_builder_emit_name(&ctx
->builder
, var_id
, name
);
893 spirv_builder_emit_offset(&ctx
->builder
, var_id
, (so_output
.dst_offset
* 4));
894 spirv_builder_emit_xfb_buffer(&ctx
->builder
, var_id
, so_output
.output_buffer
);
895 spirv_builder_emit_xfb_stride(&ctx
->builder
, var_id
, so_info
->stride
[so_output
.output_buffer
] * 4);
897 /* output location is incremented by VARYING_SLOT_VAR0 for non-builtins in vtn,
898 * so we need to ensure that the new xfb location slot doesn't conflict with any previously-emitted
901 * if there's no previous outputs that take up user slots (VAR0+) then we can start right after the
902 * glsl builtin reserved slots, otherwise we start just after the adjusted user output slot
904 uint32_t location
= NTV_MIN_RESERVED_SLOTS
+ i
;
905 if (max_output_location
>= VARYING_SLOT_VAR0
)
906 location
= max_output_location
- VARYING_SLOT_VAR0
+ 1 + i
;
907 assert(location
< VARYING_SLOT_VAR0
);
908 assert(location
<= VARYING_SLOT_VAR0
- 8 ||
909 !ctx
->seen_texcoord
[VARYING_SLOT_VAR0
- location
- 1]);
910 spirv_builder_emit_location(&ctx
->builder
, var_id
, location
);
912 /* note: gl_ClipDistance[4] can the 0-indexed member of VARYING_SLOT_CLIP_DIST1 here,
913 * so this is still the 0 component
915 if (so_output
.start_component
)
916 spirv_builder_emit_component(&ctx
->builder
, var_id
, so_output
.start_component
);
918 uint32_t *key
= ralloc_size(NULL
, sizeof(uint32_t));
919 *key
= (uint32_t)so_output
.register_index
<< 2 | so_output
.start_component
;
920 _mesa_hash_table_insert(ctx
->so_outputs
, key
, (void *)(intptr_t)var_id
);
922 assert(ctx
->num_entry_ifaces
< ARRAY_SIZE(ctx
->entry_ifaces
));
923 ctx
->entry_ifaces
[ctx
->num_entry_ifaces
++] = var_id
;
928 emit_so_outputs(struct ntv_context
*ctx
,
929 const struct pipe_stream_output_info
*so_info
, struct pipe_stream_output_info
*local_so_info
)
931 SpvId loaded_outputs
[VARYING_SLOT_MAX
] = {};
932 for (unsigned i
= 0; i
< local_so_info
->num_outputs
; i
++) {
933 uint32_t components
[NIR_MAX_VEC_COMPONENTS
];
934 struct pipe_stream_output so_output
= local_so_info
->output
[i
];
935 uint32_t so_key
= (uint32_t) so_output
.register_index
<< 2 | so_output
.start_component
;
936 struct hash_entry
*he
= _mesa_hash_table_search(ctx
->so_outputs
, &so_key
);
938 SpvId so_output_var_id
= (SpvId
)(intptr_t)he
->data
;
940 SpvId type
= get_output_type(ctx
, so_output
.register_index
, so_output
.num_components
);
941 SpvId output
= ctx
->outputs
[so_output
.register_index
];
942 SpvId output_type
= ctx
->so_output_types
[so_output
.register_index
];
943 const struct glsl_type
*out_type
= ctx
->so_output_gl_types
[so_output
.register_index
];
945 if (!loaded_outputs
[so_output
.register_index
])
946 loaded_outputs
[so_output
.register_index
] = spirv_builder_emit_load(&ctx
->builder
, output_type
, output
);
947 SpvId src
= loaded_outputs
[so_output
.register_index
];
951 for (unsigned c
= 0; c
< so_output
.num_components
; c
++) {
952 components
[c
] = so_output
.start_component
+ c
;
953 /* this is the second half of a 2 * vec4 array */
954 if (ctx
->stage
== MESA_SHADER_VERTEX
&& so_output
.register_index
== VARYING_SLOT_CLIP_DIST1
)
958 /* if we're emitting a scalar or the type we're emitting matches the output's original type and we're
959 * emitting the same number of components, then we can skip any sort of conversion here
961 if (glsl_type_is_scalar(out_type
) || (type
== output_type
&& glsl_get_length(out_type
) == so_output
.num_components
))
964 /* OpCompositeExtract can only extract scalars for our use here */
965 if (so_output
.num_components
== 1) {
966 result
= spirv_builder_emit_composite_extract(&ctx
->builder
, type
, src
, components
, so_output
.num_components
);
967 } else if (glsl_type_is_vector(out_type
)) {
968 /* OpVectorShuffle can select vector members into a differently-sized vector */
969 result
= spirv_builder_emit_vector_shuffle(&ctx
->builder
, type
,
971 components
, so_output
.num_components
);
972 result
= emit_unop(ctx
, SpvOpBitcast
, type
, result
);
974 /* for arrays, we need to manually extract each desired member
975 * and re-pack them into the desired output type
977 for (unsigned c
= 0; c
< so_output
.num_components
; c
++) {
978 uint32_t member
[] = { so_output
.start_component
+ c
};
979 SpvId base_type
= get_glsl_type(ctx
, glsl_without_array(out_type
));
981 if (ctx
->stage
== MESA_SHADER_VERTEX
&& so_output
.register_index
== VARYING_SLOT_CLIP_DIST1
)
983 components
[c
] = spirv_builder_emit_composite_extract(&ctx
->builder
, base_type
, src
, member
, 1);
985 result
= spirv_builder_emit_composite_construct(&ctx
->builder
, type
, components
, so_output
.num_components
);
989 spirv_builder_emit_store(&ctx
->builder
, so_output_var_id
, result
);
994 emit_binop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
,
995 SpvId src0
, SpvId src1
)
997 return spirv_builder_emit_binop(&ctx
->builder
, op
, type
, src0
, src1
);
1001 emit_triop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
,
1002 SpvId src0
, SpvId src1
, SpvId src2
)
1004 return spirv_builder_emit_triop(&ctx
->builder
, op
, type
, src0
, src1
, src2
);
1008 emit_builtin_unop(struct ntv_context
*ctx
, enum GLSLstd450 op
, SpvId type
,
1011 SpvId args
[] = { src
};
1012 return spirv_builder_emit_ext_inst(&ctx
->builder
, type
, ctx
->GLSL_std_450
,
1013 op
, args
, ARRAY_SIZE(args
));
1017 emit_builtin_binop(struct ntv_context
*ctx
, enum GLSLstd450 op
, SpvId type
,
1018 SpvId src0
, SpvId src1
)
1020 SpvId args
[] = { src0
, src1
};
1021 return spirv_builder_emit_ext_inst(&ctx
->builder
, type
, ctx
->GLSL_std_450
,
1022 op
, args
, ARRAY_SIZE(args
));
1026 emit_builtin_triop(struct ntv_context
*ctx
, enum GLSLstd450 op
, SpvId type
,
1027 SpvId src0
, SpvId src1
, SpvId src2
)
1029 SpvId args
[] = { src0
, src1
, src2
};
1030 return spirv_builder_emit_ext_inst(&ctx
->builder
, type
, ctx
->GLSL_std_450
,
1031 op
, args
, ARRAY_SIZE(args
));
1035 get_fvec_constant(struct ntv_context
*ctx
, unsigned bit_size
,
1036 unsigned num_components
, float value
)
1038 assert(bit_size
== 32);
1040 SpvId result
= emit_float_const(ctx
, bit_size
, value
);
1041 if (num_components
== 1)
1044 assert(num_components
> 1);
1045 SpvId components
[num_components
];
1046 for (int i
= 0; i
< num_components
; i
++)
1047 components
[i
] = result
;
1049 SpvId type
= get_fvec_type(ctx
, bit_size
, num_components
);
1050 return spirv_builder_const_composite(&ctx
->builder
, type
, components
,
1055 get_uvec_constant(struct ntv_context
*ctx
, unsigned bit_size
,
1056 unsigned num_components
, uint32_t value
)
1058 assert(bit_size
== 32);
1060 SpvId result
= emit_uint_const(ctx
, bit_size
, value
);
1061 if (num_components
== 1)
1064 assert(num_components
> 1);
1065 SpvId components
[num_components
];
1066 for (int i
= 0; i
< num_components
; i
++)
1067 components
[i
] = result
;
1069 SpvId type
= get_uvec_type(ctx
, bit_size
, num_components
);
1070 return spirv_builder_const_composite(&ctx
->builder
, type
, components
,
1075 get_ivec_constant(struct ntv_context
*ctx
, unsigned bit_size
,
1076 unsigned num_components
, int32_t value
)
1078 assert(bit_size
== 32);
1080 SpvId result
= emit_int_const(ctx
, bit_size
, value
);
1081 if (num_components
== 1)
1084 assert(num_components
> 1);
1085 SpvId components
[num_components
];
1086 for (int i
= 0; i
< num_components
; i
++)
1087 components
[i
] = result
;
1089 SpvId type
= get_ivec_type(ctx
, bit_size
, num_components
);
1090 return spirv_builder_const_composite(&ctx
->builder
, type
, components
,
1094 static inline unsigned
1095 alu_instr_src_components(const nir_alu_instr
*instr
, unsigned src
)
1097 if (nir_op_infos
[instr
->op
].input_sizes
[src
] > 0)
1098 return nir_op_infos
[instr
->op
].input_sizes
[src
];
1100 if (instr
->dest
.dest
.is_ssa
)
1101 return instr
->dest
.dest
.ssa
.num_components
;
1103 return instr
->dest
.dest
.reg
.reg
->num_components
;
1107 get_alu_src(struct ntv_context
*ctx
, nir_alu_instr
*alu
, unsigned src
)
1109 SpvId raw_value
= get_alu_src_raw(ctx
, alu
, src
);
1111 unsigned num_components
= alu_instr_src_components(alu
, src
);
1112 unsigned bit_size
= nir_src_bit_size(alu
->src
[src
].src
);
1113 nir_alu_type type
= nir_op_infos
[alu
->op
].input_types
[src
];
1118 switch (nir_alu_type_get_base_type(type
)) {
1120 unreachable("bool should have bit-size 1");
1123 return bitcast_to_ivec(ctx
, raw_value
, bit_size
, num_components
);
1128 case nir_type_float
:
1129 return bitcast_to_fvec(ctx
, raw_value
, bit_size
, num_components
);
1132 unreachable("unknown nir_alu_type");
1138 store_alu_result(struct ntv_context
*ctx
, nir_alu_instr
*alu
, SpvId result
)
1140 assert(!alu
->dest
.saturate
);
1141 return store_dest(ctx
, &alu
->dest
.dest
, result
,
1142 nir_op_infos
[alu
->op
].output_type
);
1146 get_dest_type(struct ntv_context
*ctx
, nir_dest
*dest
, nir_alu_type type
)
1148 unsigned num_components
= nir_dest_num_components(*dest
);
1149 unsigned bit_size
= nir_dest_bit_size(*dest
);
1152 return get_bvec_type(ctx
, num_components
);
1154 switch (nir_alu_type_get_base_type(type
)) {
1156 unreachable("bool should have bit-size 1");
1159 return get_ivec_type(ctx
, bit_size
, num_components
);
1162 return get_uvec_type(ctx
, bit_size
, num_components
);
1164 case nir_type_float
:
1165 return get_fvec_type(ctx
, bit_size
, num_components
);
1168 unreachable("unsupported nir_alu_type");
1173 emit_alu(struct ntv_context
*ctx
, nir_alu_instr
*alu
)
1175 SpvId src
[nir_op_infos
[alu
->op
].num_inputs
];
1176 unsigned in_bit_sizes
[nir_op_infos
[alu
->op
].num_inputs
];
1177 for (unsigned i
= 0; i
< nir_op_infos
[alu
->op
].num_inputs
; i
++) {
1178 src
[i
] = get_alu_src(ctx
, alu
, i
);
1179 in_bit_sizes
[i
] = nir_src_bit_size(alu
->src
[i
].src
);
1182 SpvId dest_type
= get_dest_type(ctx
, &alu
->dest
.dest
,
1183 nir_op_infos
[alu
->op
].output_type
);
1184 unsigned bit_size
= nir_dest_bit_size(alu
->dest
.dest
);
1185 unsigned num_components
= nir_dest_num_components(alu
->dest
.dest
);
1190 assert(nir_op_infos
[alu
->op
].num_inputs
== 1);
1194 #define UNOP(nir_op, spirv_op) \
1196 assert(nir_op_infos[alu->op].num_inputs == 1); \
1197 result = emit_unop(ctx, spirv_op, dest_type, src[0]); \
1200 UNOP(nir_op_ineg
, SpvOpSNegate
)
1201 UNOP(nir_op_fneg
, SpvOpFNegate
)
1202 UNOP(nir_op_fddx
, SpvOpDPdx
)
1203 UNOP(nir_op_fddx_coarse
, SpvOpDPdxCoarse
)
1204 UNOP(nir_op_fddx_fine
, SpvOpDPdxFine
)
1205 UNOP(nir_op_fddy
, SpvOpDPdy
)
1206 UNOP(nir_op_fddy_coarse
, SpvOpDPdyCoarse
)
1207 UNOP(nir_op_fddy_fine
, SpvOpDPdyFine
)
1208 UNOP(nir_op_f2i32
, SpvOpConvertFToS
)
1209 UNOP(nir_op_f2u32
, SpvOpConvertFToU
)
1210 UNOP(nir_op_i2f32
, SpvOpConvertSToF
)
1211 UNOP(nir_op_u2f32
, SpvOpConvertUToF
)
1212 UNOP(nir_op_bitfield_reverse
, SpvOpBitReverse
)
1217 result
= emit_unop(ctx
, SpvOpLogicalNot
, dest_type
, src
[0]);
1219 result
= emit_unop(ctx
, SpvOpNot
, dest_type
, src
[0]);
1223 assert(nir_op_infos
[alu
->op
].num_inputs
== 1);
1224 result
= emit_select(ctx
, dest_type
, src
[0],
1225 get_ivec_constant(ctx
, 32, num_components
, 1),
1226 get_ivec_constant(ctx
, 32, num_components
, 0));
1230 assert(nir_op_infos
[alu
->op
].num_inputs
== 1);
1231 result
= emit_select(ctx
, dest_type
, src
[0],
1232 get_fvec_constant(ctx
, 32, num_components
, 1),
1233 get_fvec_constant(ctx
, 32, num_components
, 0));
1236 #define BUILTIN_UNOP(nir_op, spirv_op) \
1238 assert(nir_op_infos[alu->op].num_inputs == 1); \
1239 result = emit_builtin_unop(ctx, spirv_op, dest_type, src[0]); \
1242 BUILTIN_UNOP(nir_op_iabs
, GLSLstd450SAbs
)
1243 BUILTIN_UNOP(nir_op_fabs
, GLSLstd450FAbs
)
1244 BUILTIN_UNOP(nir_op_fsqrt
, GLSLstd450Sqrt
)
1245 BUILTIN_UNOP(nir_op_frsq
, GLSLstd450InverseSqrt
)
1246 BUILTIN_UNOP(nir_op_flog2
, GLSLstd450Log2
)
1247 BUILTIN_UNOP(nir_op_fexp2
, GLSLstd450Exp2
)
1248 BUILTIN_UNOP(nir_op_ffract
, GLSLstd450Fract
)
1249 BUILTIN_UNOP(nir_op_ffloor
, GLSLstd450Floor
)
1250 BUILTIN_UNOP(nir_op_fceil
, GLSLstd450Ceil
)
1251 BUILTIN_UNOP(nir_op_ftrunc
, GLSLstd450Trunc
)
1252 BUILTIN_UNOP(nir_op_fround_even
, GLSLstd450RoundEven
)
1253 BUILTIN_UNOP(nir_op_fsign
, GLSLstd450FSign
)
1254 BUILTIN_UNOP(nir_op_isign
, GLSLstd450SSign
)
1255 BUILTIN_UNOP(nir_op_fsin
, GLSLstd450Sin
)
1256 BUILTIN_UNOP(nir_op_fcos
, GLSLstd450Cos
)
1260 assert(nir_op_infos
[alu
->op
].num_inputs
== 1);
1261 result
= emit_binop(ctx
, SpvOpFDiv
, dest_type
,
1262 get_fvec_constant(ctx
, bit_size
, num_components
, 1),
1267 assert(nir_op_infos
[alu
->op
].num_inputs
== 1);
1268 result
= emit_binop(ctx
, SpvOpFOrdNotEqual
, dest_type
, src
[0],
1269 get_fvec_constant(ctx
,
1270 nir_src_bit_size(alu
->src
[0].src
),
1271 num_components
, 0));
1274 assert(nir_op_infos
[alu
->op
].num_inputs
== 1);
1275 result
= emit_binop(ctx
, SpvOpINotEqual
, dest_type
, src
[0],
1276 get_ivec_constant(ctx
,
1277 nir_src_bit_size(alu
->src
[0].src
),
1278 num_components
, 0));
1282 #define BINOP(nir_op, spirv_op) \
1284 assert(nir_op_infos[alu->op].num_inputs == 2); \
1285 result = emit_binop(ctx, spirv_op, dest_type, src[0], src[1]); \
1288 BINOP(nir_op_iadd
, SpvOpIAdd
)
1289 BINOP(nir_op_isub
, SpvOpISub
)
1290 BINOP(nir_op_imul
, SpvOpIMul
)
1291 BINOP(nir_op_idiv
, SpvOpSDiv
)
1292 BINOP(nir_op_udiv
, SpvOpUDiv
)
1293 BINOP(nir_op_umod
, SpvOpUMod
)
1294 BINOP(nir_op_fadd
, SpvOpFAdd
)
1295 BINOP(nir_op_fsub
, SpvOpFSub
)
1296 BINOP(nir_op_fmul
, SpvOpFMul
)
1297 BINOP(nir_op_fdiv
, SpvOpFDiv
)
1298 BINOP(nir_op_fmod
, SpvOpFMod
)
1299 BINOP(nir_op_ilt
, SpvOpSLessThan
)
1300 BINOP(nir_op_ige
, SpvOpSGreaterThanEqual
)
1301 BINOP(nir_op_ult
, SpvOpULessThan
)
1302 BINOP(nir_op_uge
, SpvOpUGreaterThanEqual
)
1303 BINOP(nir_op_flt
, SpvOpFOrdLessThan
)
1304 BINOP(nir_op_fge
, SpvOpFOrdGreaterThanEqual
)
1305 BINOP(nir_op_feq
, SpvOpFOrdEqual
)
1306 BINOP(nir_op_fne
, SpvOpFUnordNotEqual
)
1307 BINOP(nir_op_ishl
, SpvOpShiftLeftLogical
)
1308 BINOP(nir_op_ishr
, SpvOpShiftRightArithmetic
)
1309 BINOP(nir_op_ushr
, SpvOpShiftRightLogical
)
1310 BINOP(nir_op_ixor
, SpvOpBitwiseXor
)
1313 #define BINOP_LOG(nir_op, spv_op, spv_log_op) \
1315 assert(nir_op_infos[alu->op].num_inputs == 2); \
1316 if (nir_src_bit_size(alu->src[0].src) == 1) \
1317 result = emit_binop(ctx, spv_log_op, dest_type, src[0], src[1]); \
1319 result = emit_binop(ctx, spv_op, dest_type, src[0], src[1]); \
1322 BINOP_LOG(nir_op_iand
, SpvOpBitwiseAnd
, SpvOpLogicalAnd
)
1323 BINOP_LOG(nir_op_ior
, SpvOpBitwiseOr
, SpvOpLogicalOr
)
1324 BINOP_LOG(nir_op_ieq
, SpvOpIEqual
, SpvOpLogicalEqual
)
1325 BINOP_LOG(nir_op_ine
, SpvOpINotEqual
, SpvOpLogicalNotEqual
)
1328 #define BUILTIN_BINOP(nir_op, spirv_op) \
1330 assert(nir_op_infos[alu->op].num_inputs == 2); \
1331 result = emit_builtin_binop(ctx, spirv_op, dest_type, src[0], src[1]); \
1334 BUILTIN_BINOP(nir_op_fmin
, GLSLstd450FMin
)
1335 BUILTIN_BINOP(nir_op_fmax
, GLSLstd450FMax
)
1336 BUILTIN_BINOP(nir_op_imin
, GLSLstd450SMin
)
1337 BUILTIN_BINOP(nir_op_imax
, GLSLstd450SMax
)
1338 BUILTIN_BINOP(nir_op_umin
, GLSLstd450UMin
)
1339 BUILTIN_BINOP(nir_op_umax
, GLSLstd450UMax
)
1340 #undef BUILTIN_BINOP
1345 assert(nir_op_infos
[alu
->op
].num_inputs
== 2);
1346 result
= emit_binop(ctx
, SpvOpDot
, dest_type
, src
[0], src
[1]);
1350 unreachable("should already be lowered away");
1356 assert(nir_op_infos
[alu
->op
].num_inputs
== 2);
1357 int num_components
= nir_dest_num_components(alu
->dest
.dest
);
1358 SpvId bool_type
= get_bvec_type(ctx
, num_components
);
1360 SpvId zero
= emit_float_const(ctx
, bit_size
, 0.0f
);
1361 SpvId one
= emit_float_const(ctx
, bit_size
, 1.0f
);
1362 if (num_components
> 1) {
1363 SpvId zero_comps
[num_components
], one_comps
[num_components
];
1364 for (int i
= 0; i
< num_components
; i
++) {
1365 zero_comps
[i
] = zero
;
1369 zero
= spirv_builder_const_composite(&ctx
->builder
, dest_type
,
1370 zero_comps
, num_components
);
1371 one
= spirv_builder_const_composite(&ctx
->builder
, dest_type
,
1372 one_comps
, num_components
);
1377 case nir_op_seq
: op
= SpvOpFOrdEqual
; break;
1378 case nir_op_sne
: op
= SpvOpFOrdNotEqual
; break;
1379 case nir_op_slt
: op
= SpvOpFOrdLessThan
; break;
1380 case nir_op_sge
: op
= SpvOpFOrdGreaterThanEqual
; break;
1381 default: unreachable("unexpected op");
1384 result
= emit_binop(ctx
, op
, bool_type
, src
[0], src
[1]);
1385 result
= emit_select(ctx
, dest_type
, result
, one
, zero
);
1390 assert(nir_op_infos
[alu
->op
].num_inputs
== 3);
1391 result
= emit_builtin_triop(ctx
, GLSLstd450FMix
, dest_type
,
1392 src
[0], src
[1], src
[2]);
1396 result
= emit_binop(ctx
, SpvOpFOrdGreaterThan
,
1397 get_bvec_type(ctx
, num_components
),
1399 get_fvec_constant(ctx
,
1400 nir_src_bit_size(alu
->src
[0].src
),
1401 num_components
, 0));
1402 result
= emit_select(ctx
, dest_type
, result
, src
[1], src
[2]);
1406 assert(nir_op_infos
[alu
->op
].num_inputs
== 3);
1407 result
= emit_select(ctx
, dest_type
, src
[0], src
[1], src
[2]);
1410 case nir_op_bany_fnequal2
:
1411 case nir_op_bany_fnequal3
:
1412 case nir_op_bany_fnequal4
: {
1413 assert(nir_op_infos
[alu
->op
].num_inputs
== 2);
1414 assert(alu_instr_src_components(alu
, 0) ==
1415 alu_instr_src_components(alu
, 1));
1416 assert(in_bit_sizes
[0] == in_bit_sizes
[1]);
1417 /* The type of Operand 1 and Operand 2 must be a scalar or vector of floating-point type. */
1418 SpvOp op
= in_bit_sizes
[0] == 1 ? SpvOpLogicalNotEqual
: SpvOpFOrdNotEqual
;
1419 result
= emit_binop(ctx
, op
,
1420 get_bvec_type(ctx
, alu_instr_src_components(alu
, 0)),
1422 result
= emit_unop(ctx
, SpvOpAny
, dest_type
, result
);
1426 case nir_op_ball_fequal2
:
1427 case nir_op_ball_fequal3
:
1428 case nir_op_ball_fequal4
: {
1429 assert(nir_op_infos
[alu
->op
].num_inputs
== 2);
1430 assert(alu_instr_src_components(alu
, 0) ==
1431 alu_instr_src_components(alu
, 1));
1432 assert(in_bit_sizes
[0] == in_bit_sizes
[1]);
1433 /* The type of Operand 1 and Operand 2 must be a scalar or vector of floating-point type. */
1434 SpvOp op
= in_bit_sizes
[0] == 1 ? SpvOpLogicalEqual
: SpvOpFOrdEqual
;
1435 result
= emit_binop(ctx
, op
,
1436 get_bvec_type(ctx
, alu_instr_src_components(alu
, 0)),
1438 result
= emit_unop(ctx
, SpvOpAll
, dest_type
, result
);
1442 case nir_op_bany_inequal2
:
1443 case nir_op_bany_inequal3
:
1444 case nir_op_bany_inequal4
: {
1445 assert(nir_op_infos
[alu
->op
].num_inputs
== 2);
1446 assert(alu_instr_src_components(alu
, 0) ==
1447 alu_instr_src_components(alu
, 1));
1448 assert(in_bit_sizes
[0] == in_bit_sizes
[1]);
1449 /* The type of Operand 1 and Operand 2 must be a scalar or vector of integer type. */
1450 SpvOp op
= in_bit_sizes
[0] == 1 ? SpvOpLogicalNotEqual
: SpvOpINotEqual
;
1451 result
= emit_binop(ctx
, op
,
1452 get_bvec_type(ctx
, alu_instr_src_components(alu
, 0)),
1454 result
= emit_unop(ctx
, SpvOpAny
, dest_type
, result
);
1458 case nir_op_ball_iequal2
:
1459 case nir_op_ball_iequal3
:
1460 case nir_op_ball_iequal4
: {
1461 assert(nir_op_infos
[alu
->op
].num_inputs
== 2);
1462 assert(alu_instr_src_components(alu
, 0) ==
1463 alu_instr_src_components(alu
, 1));
1464 assert(in_bit_sizes
[0] == in_bit_sizes
[1]);
1465 /* The type of Operand 1 and Operand 2 must be a scalar or vector of integer type. */
1466 SpvOp op
= in_bit_sizes
[0] == 1 ? SpvOpLogicalEqual
: SpvOpIEqual
;
1467 result
= emit_binop(ctx
, op
,
1468 get_bvec_type(ctx
, alu_instr_src_components(alu
, 0)),
1470 result
= emit_unop(ctx
, SpvOpAll
, dest_type
, result
);
1477 int num_inputs
= nir_op_infos
[alu
->op
].num_inputs
;
1478 assert(2 <= num_inputs
&& num_inputs
<= 4);
1479 result
= spirv_builder_emit_composite_construct(&ctx
->builder
, dest_type
,
1485 fprintf(stderr
, "emit_alu: not implemented (%s)\n",
1486 nir_op_infos
[alu
->op
].name
);
1488 unreachable("unsupported opcode");
1492 store_alu_result(ctx
, alu
, result
);
1496 emit_load_const(struct ntv_context
*ctx
, nir_load_const_instr
*load_const
)
1498 unsigned bit_size
= load_const
->def
.bit_size
;
1499 unsigned num_components
= load_const
->def
.num_components
;
1502 if (num_components
> 1) {
1503 SpvId components
[num_components
];
1505 if (bit_size
== 1) {
1506 for (int i
= 0; i
< num_components
; i
++)
1507 components
[i
] = spirv_builder_const_bool(&ctx
->builder
,
1508 load_const
->value
[i
].b
);
1510 type
= get_bvec_type(ctx
, num_components
);
1512 for (int i
= 0; i
< num_components
; i
++)
1513 components
[i
] = emit_uint_const(ctx
, bit_size
,
1514 load_const
->value
[i
].u32
);
1516 type
= get_uvec_type(ctx
, bit_size
, num_components
);
1518 constant
= spirv_builder_const_composite(&ctx
->builder
, type
,
1519 components
, num_components
);
1521 assert(num_components
== 1);
1523 constant
= spirv_builder_const_bool(&ctx
->builder
,
1524 load_const
->value
[0].b
);
1526 constant
= emit_uint_const(ctx
, bit_size
, load_const
->value
[0].u32
);
1529 store_ssa_def(ctx
, &load_const
->def
, constant
);
1533 emit_load_ubo(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
1535 nir_const_value
*const_block_index
= nir_src_as_const_value(intr
->src
[0]);
1536 assert(const_block_index
); // no dynamic indexing for now
1537 assert(const_block_index
->u32
== 0); // we only support the default UBO for now
1539 nir_const_value
*const_offset
= nir_src_as_const_value(intr
->src
[1]);
1541 SpvId uvec4_type
= get_uvec_type(ctx
, 32, 4);
1542 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
1543 SpvStorageClassUniform
,
1546 unsigned idx
= const_offset
->u32
;
1547 SpvId member
= emit_uint_const(ctx
, 32, 0);
1548 SpvId offset
= emit_uint_const(ctx
, 32, idx
);
1549 SpvId offsets
[] = { member
, offset
};
1550 SpvId ptr
= spirv_builder_emit_access_chain(&ctx
->builder
, pointer_type
,
1551 ctx
->ubos
[0], offsets
,
1552 ARRAY_SIZE(offsets
));
1553 SpvId result
= spirv_builder_emit_load(&ctx
->builder
, uvec4_type
, ptr
);
1555 SpvId type
= get_dest_uvec_type(ctx
, &intr
->dest
);
1556 unsigned num_components
= nir_dest_num_components(intr
->dest
);
1557 if (num_components
== 1) {
1558 uint32_t components
[] = { 0 };
1559 result
= spirv_builder_emit_composite_extract(&ctx
->builder
,
1563 } else if (num_components
< 4) {
1564 SpvId constituents
[num_components
];
1565 SpvId uint_type
= spirv_builder_type_uint(&ctx
->builder
, 32);
1566 for (uint32_t i
= 0; i
< num_components
; ++i
)
1567 constituents
[i
] = spirv_builder_emit_composite_extract(&ctx
->builder
,
1572 result
= spirv_builder_emit_composite_construct(&ctx
->builder
,
1578 if (nir_dest_bit_size(intr
->dest
) == 1)
1579 result
= uvec_to_bvec(ctx
, result
, num_components
);
1581 store_dest(ctx
, &intr
->dest
, result
, nir_type_uint
);
1583 unreachable("uniform-addressing not yet supported");
1587 emit_discard(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
1589 assert(ctx
->block_started
);
1590 spirv_builder_emit_kill(&ctx
->builder
);
1591 /* discard is weird in NIR, so let's just create an unreachable block after
1592 it and hope that the vulkan driver will DCE any instructinos in it. */
1593 spirv_builder_label(&ctx
->builder
, spirv_builder_new_id(&ctx
->builder
));
1597 emit_load_deref(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
1599 SpvId ptr
= get_src(ctx
, intr
->src
);
1601 SpvId result
= spirv_builder_emit_load(&ctx
->builder
,
1602 get_glsl_type(ctx
, nir_src_as_deref(intr
->src
[0])->type
),
1604 unsigned num_components
= nir_dest_num_components(intr
->dest
);
1605 unsigned bit_size
= nir_dest_bit_size(intr
->dest
);
1606 result
= bitcast_to_uvec(ctx
, result
, bit_size
, num_components
);
1607 store_dest(ctx
, &intr
->dest
, result
, nir_type_uint
);
1611 emit_store_deref(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
1613 SpvId ptr
= get_src(ctx
, &intr
->src
[0]);
1614 SpvId src
= get_src(ctx
, &intr
->src
[1]);
1616 SpvId type
= get_glsl_type(ctx
, nir_src_as_deref(intr
->src
[0])->type
);
1617 SpvId result
= emit_bitcast(ctx
, type
, src
);
1618 spirv_builder_emit_store(&ctx
->builder
, ptr
, result
);
1622 create_builtin_var(struct ntv_context
*ctx
, SpvId var_type
,
1623 SpvStorageClass storage_class
,
1624 const char *name
, SpvBuiltIn builtin
)
1626 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
1629 SpvId var
= spirv_builder_emit_var(&ctx
->builder
, pointer_type
,
1631 spirv_builder_emit_name(&ctx
->builder
, var
, name
);
1632 spirv_builder_emit_builtin(&ctx
->builder
, var
, builtin
);
1634 assert(ctx
->num_entry_ifaces
< ARRAY_SIZE(ctx
->entry_ifaces
));
1635 ctx
->entry_ifaces
[ctx
->num_entry_ifaces
++] = var
;
1640 emit_load_front_face(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
1642 SpvId var_type
= spirv_builder_type_bool(&ctx
->builder
);
1643 if (!ctx
->front_face_var
)
1644 ctx
->front_face_var
= create_builtin_var(ctx
, var_type
,
1645 SpvStorageClassInput
,
1647 SpvBuiltInFrontFacing
);
1649 SpvId result
= spirv_builder_emit_load(&ctx
->builder
, var_type
,
1650 ctx
->front_face_var
);
1651 assert(1 == nir_dest_num_components(intr
->dest
));
1652 store_dest(ctx
, &intr
->dest
, result
, nir_type_bool
);
1656 emit_load_instance_id(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
1658 SpvId var_type
= spirv_builder_type_uint(&ctx
->builder
, 32);
1659 if (!ctx
->instance_id_var
)
1660 ctx
->instance_id_var
= create_builtin_var(ctx
, var_type
,
1661 SpvStorageClassInput
,
1663 SpvBuiltInInstanceIndex
);
1665 SpvId result
= spirv_builder_emit_load(&ctx
->builder
, var_type
,
1666 ctx
->instance_id_var
);
1667 assert(1 == nir_dest_num_components(intr
->dest
));
1668 store_dest(ctx
, &intr
->dest
, result
, nir_type_uint
);
1672 emit_load_vertex_id(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
1674 SpvId var_type
= spirv_builder_type_uint(&ctx
->builder
, 32);
1675 if (!ctx
->vertex_id_var
)
1676 ctx
->vertex_id_var
= create_builtin_var(ctx
, var_type
,
1677 SpvStorageClassInput
,
1679 SpvBuiltInVertexIndex
);
1681 SpvId result
= spirv_builder_emit_load(&ctx
->builder
, var_type
,
1682 ctx
->vertex_id_var
);
1683 assert(1 == nir_dest_num_components(intr
->dest
));
1684 store_dest(ctx
, &intr
->dest
, result
, nir_type_uint
);
1688 emit_intrinsic(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
1690 switch (intr
->intrinsic
) {
1691 case nir_intrinsic_load_ubo
:
1692 emit_load_ubo(ctx
, intr
);
1695 case nir_intrinsic_discard
:
1696 emit_discard(ctx
, intr
);
1699 case nir_intrinsic_load_deref
:
1700 emit_load_deref(ctx
, intr
);
1703 case nir_intrinsic_store_deref
:
1704 emit_store_deref(ctx
, intr
);
1707 case nir_intrinsic_load_front_face
:
1708 emit_load_front_face(ctx
, intr
);
1711 case nir_intrinsic_load_instance_id
:
1712 emit_load_instance_id(ctx
, intr
);
1715 case nir_intrinsic_load_vertex_id
:
1716 emit_load_vertex_id(ctx
, intr
);
1720 fprintf(stderr
, "emit_intrinsic: not implemented (%s)\n",
1721 nir_intrinsic_infos
[intr
->intrinsic
].name
);
1722 unreachable("unsupported intrinsic");
1727 emit_undef(struct ntv_context
*ctx
, nir_ssa_undef_instr
*undef
)
1729 SpvId type
= get_uvec_type(ctx
, undef
->def
.bit_size
,
1730 undef
->def
.num_components
);
1732 store_ssa_def(ctx
, &undef
->def
,
1733 spirv_builder_emit_undef(&ctx
->builder
, type
));
1737 get_src_float(struct ntv_context
*ctx
, nir_src
*src
)
1739 SpvId def
= get_src(ctx
, src
);
1740 unsigned num_components
= nir_src_num_components(*src
);
1741 unsigned bit_size
= nir_src_bit_size(*src
);
1742 return bitcast_to_fvec(ctx
, def
, bit_size
, num_components
);
1746 get_src_int(struct ntv_context
*ctx
, nir_src
*src
)
1748 SpvId def
= get_src(ctx
, src
);
1749 unsigned num_components
= nir_src_num_components(*src
);
1750 unsigned bit_size
= nir_src_bit_size(*src
);
1751 return bitcast_to_ivec(ctx
, def
, bit_size
, num_components
);
1755 emit_tex(struct ntv_context
*ctx
, nir_tex_instr
*tex
)
1757 assert(tex
->op
== nir_texop_tex
||
1758 tex
->op
== nir_texop_txb
||
1759 tex
->op
== nir_texop_txl
||
1760 tex
->op
== nir_texop_txd
||
1761 tex
->op
== nir_texop_txf
||
1762 tex
->op
== nir_texop_txf_ms
||
1763 tex
->op
== nir_texop_txs
);
1764 assert(tex
->texture_index
== tex
->sampler_index
);
1766 SpvId coord
= 0, proj
= 0, bias
= 0, lod
= 0, dref
= 0, dx
= 0, dy
= 0,
1767 offset
= 0, sample
= 0;
1768 unsigned coord_components
= 0;
1769 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
1770 switch (tex
->src
[i
].src_type
) {
1771 case nir_tex_src_coord
:
1772 if (tex
->op
== nir_texop_txf
||
1773 tex
->op
== nir_texop_txf_ms
)
1774 coord
= get_src_int(ctx
, &tex
->src
[i
].src
);
1776 coord
= get_src_float(ctx
, &tex
->src
[i
].src
);
1777 coord_components
= nir_src_num_components(tex
->src
[i
].src
);
1780 case nir_tex_src_projector
:
1781 assert(nir_src_num_components(tex
->src
[i
].src
) == 1);
1782 proj
= get_src_float(ctx
, &tex
->src
[i
].src
);
1786 case nir_tex_src_offset
:
1787 offset
= get_src_int(ctx
, &tex
->src
[i
].src
);
1790 case nir_tex_src_bias
:
1791 assert(tex
->op
== nir_texop_txb
);
1792 bias
= get_src_float(ctx
, &tex
->src
[i
].src
);
1796 case nir_tex_src_lod
:
1797 assert(nir_src_num_components(tex
->src
[i
].src
) == 1);
1798 if (tex
->op
== nir_texop_txf
||
1799 tex
->op
== nir_texop_txf_ms
||
1800 tex
->op
== nir_texop_txs
)
1801 lod
= get_src_int(ctx
, &tex
->src
[i
].src
);
1803 lod
= get_src_float(ctx
, &tex
->src
[i
].src
);
1807 case nir_tex_src_ms_index
:
1808 assert(nir_src_num_components(tex
->src
[i
].src
) == 1);
1809 sample
= get_src_int(ctx
, &tex
->src
[i
].src
);
1812 case nir_tex_src_comparator
:
1813 assert(nir_src_num_components(tex
->src
[i
].src
) == 1);
1814 dref
= get_src_float(ctx
, &tex
->src
[i
].src
);
1818 case nir_tex_src_ddx
:
1819 dx
= get_src_float(ctx
, &tex
->src
[i
].src
);
1823 case nir_tex_src_ddy
:
1824 dy
= get_src_float(ctx
, &tex
->src
[i
].src
);
1829 fprintf(stderr
, "texture source: %d\n", tex
->src
[i
].src_type
);
1830 unreachable("unknown texture source");
1834 if (lod
== 0 && ctx
->stage
!= MESA_SHADER_FRAGMENT
) {
1835 lod
= emit_float_const(ctx
, 32, 0.0f
);
1839 SpvId image_type
= ctx
->image_types
[tex
->texture_index
];
1840 SpvId sampled_type
= spirv_builder_type_sampled_image(&ctx
->builder
,
1843 assert(ctx
->samplers_used
& (1u << tex
->texture_index
));
1844 SpvId load
= spirv_builder_emit_load(&ctx
->builder
, sampled_type
,
1845 ctx
->samplers
[tex
->texture_index
]);
1847 SpvId dest_type
= get_dest_type(ctx
, &tex
->dest
, tex
->dest_type
);
1849 if (tex
->op
== nir_texop_txs
) {
1850 SpvId image
= spirv_builder_emit_image(&ctx
->builder
, image_type
, load
);
1851 SpvId result
= spirv_builder_emit_image_query_size(&ctx
->builder
,
1854 store_dest(ctx
, &tex
->dest
, result
, tex
->dest_type
);
1858 if (proj
&& coord_components
> 0) {
1859 SpvId constituents
[coord_components
+ 1];
1860 if (coord_components
== 1)
1861 constituents
[0] = coord
;
1863 assert(coord_components
> 1);
1864 SpvId float_type
= spirv_builder_type_float(&ctx
->builder
, 32);
1865 for (uint32_t i
= 0; i
< coord_components
; ++i
)
1866 constituents
[i
] = spirv_builder_emit_composite_extract(&ctx
->builder
,
1872 constituents
[coord_components
++] = proj
;
1874 SpvId vec_type
= get_fvec_type(ctx
, 32, coord_components
);
1875 coord
= spirv_builder_emit_composite_construct(&ctx
->builder
,
1881 SpvId actual_dest_type
= dest_type
;
1883 actual_dest_type
= spirv_builder_type_float(&ctx
->builder
, 32);
1886 if (tex
->op
== nir_texop_txf
||
1887 tex
->op
== nir_texop_txf_ms
) {
1888 SpvId image
= spirv_builder_emit_image(&ctx
->builder
, image_type
, load
);
1889 result
= spirv_builder_emit_image_fetch(&ctx
->builder
, dest_type
,
1890 image
, coord
, lod
, sample
);
1892 result
= spirv_builder_emit_image_sample(&ctx
->builder
,
1893 actual_dest_type
, load
,
1896 lod
, bias
, dref
, dx
, dy
,
1900 spirv_builder_emit_decoration(&ctx
->builder
, result
,
1901 SpvDecorationRelaxedPrecision
);
1903 if (dref
&& nir_dest_num_components(tex
->dest
) > 1) {
1904 SpvId components
[4] = { result
, result
, result
, result
};
1905 result
= spirv_builder_emit_composite_construct(&ctx
->builder
,
1911 store_dest(ctx
, &tex
->dest
, result
, tex
->dest_type
);
1915 start_block(struct ntv_context
*ctx
, SpvId label
)
1917 /* terminate previous block if needed */
1918 if (ctx
->block_started
)
1919 spirv_builder_emit_branch(&ctx
->builder
, label
);
1921 /* start new block */
1922 spirv_builder_label(&ctx
->builder
, label
);
1923 ctx
->block_started
= true;
1927 branch(struct ntv_context
*ctx
, SpvId label
)
1929 assert(ctx
->block_started
);
1930 spirv_builder_emit_branch(&ctx
->builder
, label
);
1931 ctx
->block_started
= false;
1935 branch_conditional(struct ntv_context
*ctx
, SpvId condition
, SpvId then_id
,
1938 assert(ctx
->block_started
);
1939 spirv_builder_emit_branch_conditional(&ctx
->builder
, condition
,
1941 ctx
->block_started
= false;
1945 emit_jump(struct ntv_context
*ctx
, nir_jump_instr
*jump
)
1947 switch (jump
->type
) {
1948 case nir_jump_break
:
1949 assert(ctx
->loop_break
);
1950 branch(ctx
, ctx
->loop_break
);
1953 case nir_jump_continue
:
1954 assert(ctx
->loop_cont
);
1955 branch(ctx
, ctx
->loop_cont
);
1959 unreachable("Unsupported jump type\n");
1964 emit_deref_var(struct ntv_context
*ctx
, nir_deref_instr
*deref
)
1966 assert(deref
->deref_type
== nir_deref_type_var
);
1968 struct hash_entry
*he
= _mesa_hash_table_search(ctx
->vars
, deref
->var
);
1970 SpvId result
= (SpvId
)(intptr_t)he
->data
;
1971 store_dest_raw(ctx
, &deref
->dest
, result
);
1975 emit_deref_array(struct ntv_context
*ctx
, nir_deref_instr
*deref
)
1977 assert(deref
->deref_type
== nir_deref_type_array
);
1978 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
1980 SpvStorageClass storage_class
;
1981 switch (var
->data
.mode
) {
1982 case nir_var_shader_in
:
1983 storage_class
= SpvStorageClassInput
;
1986 case nir_var_shader_out
:
1987 storage_class
= SpvStorageClassOutput
;
1991 unreachable("Unsupported nir_variable_mode\n");
1994 SpvId index
= get_src(ctx
, &deref
->arr
.index
);
1996 SpvId ptr_type
= spirv_builder_type_pointer(&ctx
->builder
,
1998 get_glsl_type(ctx
, deref
->type
));
2000 SpvId result
= spirv_builder_emit_access_chain(&ctx
->builder
,
2002 get_src(ctx
, &deref
->parent
),
2004 /* uint is a bit of a lie here, it's really just an opaque type */
2005 store_dest(ctx
, &deref
->dest
, result
, nir_type_uint
);
2009 emit_deref(struct ntv_context
*ctx
, nir_deref_instr
*deref
)
2011 switch (deref
->deref_type
) {
2012 case nir_deref_type_var
:
2013 emit_deref_var(ctx
, deref
);
2016 case nir_deref_type_array
:
2017 emit_deref_array(ctx
, deref
);
2021 unreachable("unexpected deref_type");
2026 emit_block(struct ntv_context
*ctx
, struct nir_block
*block
)
2028 start_block(ctx
, block_label(ctx
, block
));
2029 nir_foreach_instr(instr
, block
) {
2030 switch (instr
->type
) {
2031 case nir_instr_type_alu
:
2032 emit_alu(ctx
, nir_instr_as_alu(instr
));
2034 case nir_instr_type_intrinsic
:
2035 emit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
2037 case nir_instr_type_load_const
:
2038 emit_load_const(ctx
, nir_instr_as_load_const(instr
));
2040 case nir_instr_type_ssa_undef
:
2041 emit_undef(ctx
, nir_instr_as_ssa_undef(instr
));
2043 case nir_instr_type_tex
:
2044 emit_tex(ctx
, nir_instr_as_tex(instr
));
2046 case nir_instr_type_phi
:
2047 unreachable("nir_instr_type_phi not supported");
2049 case nir_instr_type_jump
:
2050 emit_jump(ctx
, nir_instr_as_jump(instr
));
2052 case nir_instr_type_call
:
2053 unreachable("nir_instr_type_call not supported");
2055 case nir_instr_type_parallel_copy
:
2056 unreachable("nir_instr_type_parallel_copy not supported");
2058 case nir_instr_type_deref
:
2059 emit_deref(ctx
, nir_instr_as_deref(instr
));
2066 emit_cf_list(struct ntv_context
*ctx
, struct exec_list
*list
);
2069 get_src_bool(struct ntv_context
*ctx
, nir_src
*src
)
2071 assert(nir_src_bit_size(*src
) == 1);
2072 return get_src(ctx
, src
);
2076 emit_if(struct ntv_context
*ctx
, nir_if
*if_stmt
)
2078 SpvId condition
= get_src_bool(ctx
, &if_stmt
->condition
);
2080 SpvId header_id
= spirv_builder_new_id(&ctx
->builder
);
2081 SpvId then_id
= block_label(ctx
, nir_if_first_then_block(if_stmt
));
2082 SpvId endif_id
= spirv_builder_new_id(&ctx
->builder
);
2083 SpvId else_id
= endif_id
;
2085 bool has_else
= !exec_list_is_empty(&if_stmt
->else_list
);
2087 assert(nir_if_first_else_block(if_stmt
)->index
< ctx
->num_blocks
);
2088 else_id
= block_label(ctx
, nir_if_first_else_block(if_stmt
));
2091 /* create a header-block */
2092 start_block(ctx
, header_id
);
2093 spirv_builder_emit_selection_merge(&ctx
->builder
, endif_id
,
2094 SpvSelectionControlMaskNone
);
2095 branch_conditional(ctx
, condition
, then_id
, else_id
);
2097 emit_cf_list(ctx
, &if_stmt
->then_list
);
2100 if (ctx
->block_started
)
2101 branch(ctx
, endif_id
);
2103 emit_cf_list(ctx
, &if_stmt
->else_list
);
2106 start_block(ctx
, endif_id
);
2110 emit_loop(struct ntv_context
*ctx
, nir_loop
*loop
)
2112 SpvId header_id
= spirv_builder_new_id(&ctx
->builder
);
2113 SpvId begin_id
= block_label(ctx
, nir_loop_first_block(loop
));
2114 SpvId break_id
= spirv_builder_new_id(&ctx
->builder
);
2115 SpvId cont_id
= spirv_builder_new_id(&ctx
->builder
);
2117 /* create a header-block */
2118 start_block(ctx
, header_id
);
2119 spirv_builder_loop_merge(&ctx
->builder
, break_id
, cont_id
, SpvLoopControlMaskNone
);
2120 branch(ctx
, begin_id
);
2122 SpvId save_break
= ctx
->loop_break
;
2123 SpvId save_cont
= ctx
->loop_cont
;
2124 ctx
->loop_break
= break_id
;
2125 ctx
->loop_cont
= cont_id
;
2127 emit_cf_list(ctx
, &loop
->body
);
2129 ctx
->loop_break
= save_break
;
2130 ctx
->loop_cont
= save_cont
;
2132 /* loop->body may have already ended our block */
2133 if (ctx
->block_started
)
2134 branch(ctx
, cont_id
);
2135 start_block(ctx
, cont_id
);
2136 branch(ctx
, header_id
);
2138 start_block(ctx
, break_id
);
2142 emit_cf_list(struct ntv_context
*ctx
, struct exec_list
*list
)
2144 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
2145 switch (node
->type
) {
2146 case nir_cf_node_block
:
2147 emit_block(ctx
, nir_cf_node_as_block(node
));
2150 case nir_cf_node_if
:
2151 emit_if(ctx
, nir_cf_node_as_if(node
));
2154 case nir_cf_node_loop
:
2155 emit_loop(ctx
, nir_cf_node_as_loop(node
));
2158 case nir_cf_node_function
:
2159 unreachable("nir_cf_node_function not supported");
2165 struct spirv_shader
*
2166 nir_to_spirv(struct nir_shader
*s
, const struct pipe_stream_output_info
*so_info
, struct pipe_stream_output_info
*local_so_info
)
2168 struct spirv_shader
*ret
= NULL
;
2170 struct ntv_context ctx
= {};
2172 switch (s
->info
.stage
) {
2173 case MESA_SHADER_VERTEX
:
2174 case MESA_SHADER_FRAGMENT
:
2175 case MESA_SHADER_COMPUTE
:
2176 spirv_builder_emit_cap(&ctx
.builder
, SpvCapabilityShader
);
2179 case MESA_SHADER_TESS_CTRL
:
2180 case MESA_SHADER_TESS_EVAL
:
2181 spirv_builder_emit_cap(&ctx
.builder
, SpvCapabilityTessellation
);
2184 case MESA_SHADER_GEOMETRY
:
2185 spirv_builder_emit_cap(&ctx
.builder
, SpvCapabilityGeometry
);
2189 unreachable("invalid stage");
2192 // TODO: only enable when needed
2193 if (s
->info
.stage
== MESA_SHADER_FRAGMENT
) {
2194 spirv_builder_emit_cap(&ctx
.builder
, SpvCapabilitySampled1D
);
2195 spirv_builder_emit_cap(&ctx
.builder
, SpvCapabilityImageQuery
);
2196 spirv_builder_emit_cap(&ctx
.builder
, SpvCapabilityDerivativeControl
);
2199 ctx
.stage
= s
->info
.stage
;
2200 ctx
.GLSL_std_450
= spirv_builder_import(&ctx
.builder
, "GLSL.std.450");
2201 spirv_builder_emit_source(&ctx
.builder
, SpvSourceLanguageGLSL
, 450);
2203 spirv_builder_emit_mem_model(&ctx
.builder
, SpvAddressingModelLogical
,
2204 SpvMemoryModelGLSL450
);
2206 SpvExecutionModel exec_model
;
2207 switch (s
->info
.stage
) {
2208 case MESA_SHADER_VERTEX
:
2209 exec_model
= SpvExecutionModelVertex
;
2211 case MESA_SHADER_TESS_CTRL
:
2212 exec_model
= SpvExecutionModelTessellationControl
;
2214 case MESA_SHADER_TESS_EVAL
:
2215 exec_model
= SpvExecutionModelTessellationEvaluation
;
2217 case MESA_SHADER_GEOMETRY
:
2218 exec_model
= SpvExecutionModelGeometry
;
2220 case MESA_SHADER_FRAGMENT
:
2221 exec_model
= SpvExecutionModelFragment
;
2223 case MESA_SHADER_COMPUTE
:
2224 exec_model
= SpvExecutionModelGLCompute
;
2227 unreachable("invalid stage");
2230 SpvId type_void
= spirv_builder_type_void(&ctx
.builder
);
2231 SpvId type_main
= spirv_builder_type_function(&ctx
.builder
, type_void
,
2233 SpvId entry_point
= spirv_builder_new_id(&ctx
.builder
);
2234 spirv_builder_emit_name(&ctx
.builder
, entry_point
, "main");
2236 ctx
.vars
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
2237 _mesa_key_pointer_equal
);
2239 ctx
.so_outputs
= _mesa_hash_table_create(NULL
, _mesa_hash_u32
,
2240 _mesa_key_u32_equal
);
2242 nir_foreach_variable(var
, &s
->inputs
)
2243 emit_input(&ctx
, var
);
2245 nir_foreach_variable(var
, &s
->outputs
)
2246 emit_output(&ctx
, var
);
2249 emit_so_info(&ctx
, util_last_bit64(s
->info
.outputs_written
), so_info
, local_so_info
);
2250 nir_foreach_variable(var
, &s
->uniforms
)
2251 emit_uniform(&ctx
, var
);
2253 if (s
->info
.stage
== MESA_SHADER_FRAGMENT
) {
2254 spirv_builder_emit_exec_mode(&ctx
.builder
, entry_point
,
2255 SpvExecutionModeOriginUpperLeft
);
2256 if (s
->info
.outputs_written
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
))
2257 spirv_builder_emit_exec_mode(&ctx
.builder
, entry_point
,
2258 SpvExecutionModeDepthReplacing
);
2261 if (so_info
&& so_info
->num_outputs
) {
2262 spirv_builder_emit_cap(&ctx
.builder
, SpvCapabilityTransformFeedback
);
2263 spirv_builder_emit_exec_mode(&ctx
.builder
, entry_point
,
2264 SpvExecutionModeXfb
);
2267 spirv_builder_function(&ctx
.builder
, entry_point
, type_void
,
2268 SpvFunctionControlMaskNone
,
2271 nir_function_impl
*entry
= nir_shader_get_entrypoint(s
);
2272 nir_metadata_require(entry
, nir_metadata_block_index
);
2274 ctx
.defs
= (SpvId
*)malloc(sizeof(SpvId
) * entry
->ssa_alloc
);
2277 ctx
.num_defs
= entry
->ssa_alloc
;
2279 nir_index_local_regs(entry
);
2280 ctx
.regs
= malloc(sizeof(SpvId
) * entry
->reg_alloc
);
2283 ctx
.num_regs
= entry
->reg_alloc
;
2285 SpvId
*block_ids
= (SpvId
*)malloc(sizeof(SpvId
) * entry
->num_blocks
);
2289 for (int i
= 0; i
< entry
->num_blocks
; ++i
)
2290 block_ids
[i
] = spirv_builder_new_id(&ctx
.builder
);
2292 ctx
.block_ids
= block_ids
;
2293 ctx
.num_blocks
= entry
->num_blocks
;
2295 /* emit a block only for the variable declarations */
2296 start_block(&ctx
, spirv_builder_new_id(&ctx
.builder
));
2297 foreach_list_typed(nir_register
, reg
, node
, &entry
->registers
) {
2298 SpvId type
= get_uvec_type(&ctx
, reg
->bit_size
, reg
->num_components
);
2299 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
.builder
,
2300 SpvStorageClassFunction
,
2302 SpvId var
= spirv_builder_emit_var(&ctx
.builder
, pointer_type
,
2303 SpvStorageClassFunction
);
2305 ctx
.regs
[reg
->index
] = var
;
2308 emit_cf_list(&ctx
, &entry
->body
);
2313 emit_so_outputs(&ctx
, so_info
, local_so_info
);
2315 spirv_builder_return(&ctx
.builder
); // doesn't belong here, but whatevz
2316 spirv_builder_function_end(&ctx
.builder
);
2318 spirv_builder_emit_entry_point(&ctx
.builder
, exec_model
, entry_point
,
2319 "main", ctx
.entry_ifaces
,
2320 ctx
.num_entry_ifaces
);
2322 size_t num_words
= spirv_builder_get_num_words(&ctx
.builder
);
2324 ret
= CALLOC_STRUCT(spirv_shader
);
2328 ret
->words
= MALLOC(sizeof(uint32_t) * num_words
);
2332 ret
->num_words
= spirv_builder_get_words(&ctx
.builder
, ret
->words
, num_words
);
2333 assert(ret
->num_words
== num_words
);
2340 spirv_shader_delete(ret
);
2343 _mesa_hash_table_destroy(ctx
.vars
, NULL
);
2346 _mesa_hash_table_destroy(ctx
.so_outputs
, NULL
);
2352 spirv_shader_delete(struct spirv_shader
*s
)