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
95 struct spirv_builder builder
;
99 gl_shader_stage stage
;
103 SpvId image_types
[PIPE_MAX_SAMPLERS
];
104 SpvId samplers
[PIPE_MAX_SAMPLERS
];
105 unsigned samplers_used
: PIPE_MAX_SAMPLERS
;
106 SpvId entry_ifaces
[PIPE_MAX_SHADER_INPUTS
* 4 + PIPE_MAX_SHADER_OUTPUTS
* 4];
107 size_t num_entry_ifaces
;
115 struct hash_table
*vars
; /* nir_variable -> SpvId */
116 struct hash_table
*so_outputs
; /* pipe_stream_output -> SpvId */
117 unsigned outputs
[VARYING_SLOT_MAX
];
118 const struct glsl_type
*so_output_gl_types
[VARYING_SLOT_MAX
];
119 SpvId so_output_types
[VARYING_SLOT_MAX
];
121 const SpvId
*block_ids
;
124 SpvId loop_break
, loop_cont
;
126 SpvId front_face_var
, instance_id_var
, vertex_id_var
;
128 bool seen_texcoord
[8]; //whether we've seen a VARYING_SLOT_TEX[n] this pass
133 get_fvec_constant(struct ntv_context
*ctx
, unsigned bit_size
,
134 unsigned num_components
, float value
);
137 get_uvec_constant(struct ntv_context
*ctx
, unsigned bit_size
,
138 unsigned num_components
, uint32_t value
);
141 get_ivec_constant(struct ntv_context
*ctx
, unsigned bit_size
,
142 unsigned num_components
, int32_t value
);
145 emit_unop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
, SpvId src
);
148 emit_binop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
,
149 SpvId src0
, SpvId src1
);
152 emit_triop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
,
153 SpvId src0
, SpvId src1
, SpvId src2
);
156 get_bvec_type(struct ntv_context
*ctx
, int num_components
)
158 SpvId bool_type
= spirv_builder_type_bool(&ctx
->builder
);
159 if (num_components
> 1)
160 return spirv_builder_type_vector(&ctx
->builder
, bool_type
,
163 assert(num_components
== 1);
168 block_label(struct ntv_context
*ctx
, nir_block
*block
)
170 assert(block
->index
< ctx
->num_blocks
);
171 return ctx
->block_ids
[block
->index
];
175 emit_float_const(struct ntv_context
*ctx
, int bit_size
, float value
)
177 assert(bit_size
== 32);
178 return spirv_builder_const_float(&ctx
->builder
, bit_size
, value
);
182 emit_uint_const(struct ntv_context
*ctx
, int bit_size
, uint32_t value
)
184 assert(bit_size
== 32);
185 return spirv_builder_const_uint(&ctx
->builder
, bit_size
, value
);
189 emit_int_const(struct ntv_context
*ctx
, int bit_size
, int32_t value
)
191 assert(bit_size
== 32);
192 return spirv_builder_const_int(&ctx
->builder
, bit_size
, value
);
196 get_fvec_type(struct ntv_context
*ctx
, unsigned bit_size
, unsigned num_components
)
198 assert(bit_size
== 32); // only 32-bit floats supported so far
200 SpvId float_type
= spirv_builder_type_float(&ctx
->builder
, bit_size
);
201 if (num_components
> 1)
202 return spirv_builder_type_vector(&ctx
->builder
, float_type
,
205 assert(num_components
== 1);
210 get_ivec_type(struct ntv_context
*ctx
, unsigned bit_size
, unsigned num_components
)
212 assert(bit_size
== 32); // only 32-bit ints supported so far
214 SpvId int_type
= spirv_builder_type_int(&ctx
->builder
, bit_size
);
215 if (num_components
> 1)
216 return spirv_builder_type_vector(&ctx
->builder
, int_type
,
219 assert(num_components
== 1);
224 get_uvec_type(struct ntv_context
*ctx
, unsigned bit_size
, unsigned num_components
)
226 assert(bit_size
== 32); // only 32-bit uints supported so far
228 SpvId uint_type
= spirv_builder_type_uint(&ctx
->builder
, bit_size
);
229 if (num_components
> 1)
230 return spirv_builder_type_vector(&ctx
->builder
, uint_type
,
233 assert(num_components
== 1);
238 get_dest_uvec_type(struct ntv_context
*ctx
, nir_dest
*dest
)
240 unsigned bit_size
= MAX2(nir_dest_bit_size(*dest
), 32);
241 return get_uvec_type(ctx
, bit_size
, nir_dest_num_components(*dest
));
245 get_glsl_basetype(struct ntv_context
*ctx
, enum glsl_base_type type
)
249 return spirv_builder_type_bool(&ctx
->builder
);
251 case GLSL_TYPE_FLOAT
:
252 return spirv_builder_type_float(&ctx
->builder
, 32);
255 return spirv_builder_type_int(&ctx
->builder
, 32);
258 return spirv_builder_type_uint(&ctx
->builder
, 32);
259 /* TODO: handle more types */
262 unreachable("unknown GLSL type");
267 get_glsl_type(struct ntv_context
*ctx
, const struct glsl_type
*type
)
270 if (glsl_type_is_scalar(type
))
271 return get_glsl_basetype(ctx
, glsl_get_base_type(type
));
273 if (glsl_type_is_vector(type
))
274 return spirv_builder_type_vector(&ctx
->builder
,
275 get_glsl_basetype(ctx
, glsl_get_base_type(type
)),
276 glsl_get_vector_elements(type
));
278 if (glsl_type_is_array(type
)) {
279 SpvId ret
= spirv_builder_type_array(&ctx
->builder
,
280 get_glsl_type(ctx
, glsl_get_array_element(type
)),
281 emit_uint_const(ctx
, 32, glsl_get_length(type
)));
282 uint32_t stride
= glsl_get_explicit_stride(type
);
284 spirv_builder_emit_array_stride(&ctx
->builder
, ret
, stride
);
289 unreachable("we shouldn't get here, I think...");
292 static inline unsigned
293 handle_slot(struct ntv_context
*ctx
, unsigned slot
)
295 unsigned orig
= slot
;
296 if (slot
< VARYING_SLOT_VAR0
) {
298 if (slot
>= VARYING_SLOT_TEX0
&& slot
<= VARYING_SLOT_TEX7
)
299 ctx
->seen_texcoord
[slot
- VARYING_SLOT_TEX0
] = true;
301 slot
= slot_pack_map
[slot
];
302 if (slot
== UINT_MAX
)
303 debug_printf("unhandled varying slot: %s\n", gl_varying_slot_name(orig
));
305 slot
-= VARYING_SLOT_VAR0
- NTV_MIN_RESERVED_SLOTS
;
306 assert(slot
<= VARYING_SLOT_VAR0
- 8 ||
307 !ctx
->seen_texcoord
[VARYING_SLOT_VAR0
- slot
- 1]);
310 assert(slot
< VARYING_SLOT_VAR0
);
314 #define HANDLE_EMIT_BUILTIN(SLOT, BUILTIN) \
315 case VARYING_SLOT_##SLOT: \
316 spirv_builder_emit_builtin(&ctx->builder, var_id, SpvBuiltIn##BUILTIN); \
321 emit_input(struct ntv_context
*ctx
, struct nir_variable
*var
)
323 SpvId var_type
= get_glsl_type(ctx
, var
->type
);
324 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
325 SpvStorageClassInput
,
327 SpvId var_id
= spirv_builder_emit_var(&ctx
->builder
, pointer_type
,
328 SpvStorageClassInput
);
331 spirv_builder_emit_name(&ctx
->builder
, var_id
, var
->name
);
333 if (ctx
->stage
== MESA_SHADER_FRAGMENT
) {
334 unsigned slot
= var
->data
.location
;
336 HANDLE_EMIT_BUILTIN(POS
, FragCoord
);
337 HANDLE_EMIT_BUILTIN(PNTC
, PointCoord
);
338 HANDLE_EMIT_BUILTIN(LAYER
, Layer
);
339 HANDLE_EMIT_BUILTIN(PRIMITIVE_ID
, PrimitiveId
);
340 HANDLE_EMIT_BUILTIN(CLIP_DIST0
, ClipDistance
);
341 HANDLE_EMIT_BUILTIN(CULL_DIST0
, CullDistance
);
342 HANDLE_EMIT_BUILTIN(VIEWPORT
, ViewportIndex
);
343 HANDLE_EMIT_BUILTIN(FACE
, FrontFacing
);
346 slot
= handle_slot(ctx
, slot
);
347 spirv_builder_emit_location(&ctx
->builder
, var_id
, slot
);
350 spirv_builder_emit_location(&ctx
->builder
, var_id
,
351 var
->data
.driver_location
);
354 if (var
->data
.location_frac
)
355 spirv_builder_emit_component(&ctx
->builder
, var_id
,
356 var
->data
.location_frac
);
358 if (var
->data
.interpolation
== INTERP_MODE_FLAT
)
359 spirv_builder_emit_decoration(&ctx
->builder
, var_id
, SpvDecorationFlat
);
361 _mesa_hash_table_insert(ctx
->vars
, var
, (void *)(intptr_t)var_id
);
363 assert(ctx
->num_entry_ifaces
< ARRAY_SIZE(ctx
->entry_ifaces
));
364 ctx
->entry_ifaces
[ctx
->num_entry_ifaces
++] = var_id
;
368 emit_output(struct ntv_context
*ctx
, struct nir_variable
*var
)
370 SpvId var_type
= get_glsl_type(ctx
, var
->type
);
371 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
372 SpvStorageClassOutput
,
374 SpvId var_id
= spirv_builder_emit_var(&ctx
->builder
, pointer_type
,
375 SpvStorageClassOutput
);
377 spirv_builder_emit_name(&ctx
->builder
, var_id
, var
->name
);
380 if (ctx
->stage
== MESA_SHADER_VERTEX
) {
381 unsigned slot
= var
->data
.location
;
383 HANDLE_EMIT_BUILTIN(POS
, Position
);
384 HANDLE_EMIT_BUILTIN(PSIZ
, PointSize
);
385 HANDLE_EMIT_BUILTIN(LAYER
, Layer
);
386 HANDLE_EMIT_BUILTIN(PRIMITIVE_ID
, PrimitiveId
);
387 HANDLE_EMIT_BUILTIN(CULL_DIST0
, CullDistance
);
388 HANDLE_EMIT_BUILTIN(VIEWPORT
, ViewportIndex
);
389 HANDLE_EMIT_BUILTIN(TESS_LEVEL_OUTER
, TessLevelOuter
);
390 HANDLE_EMIT_BUILTIN(TESS_LEVEL_INNER
, TessLevelInner
);
392 case VARYING_SLOT_CLIP_DIST0
:
393 assert(glsl_type_is_array(var
->type
));
394 spirv_builder_emit_builtin(&ctx
->builder
, var_id
, SpvBuiltInClipDistance
);
395 /* this can be as large as 2x vec4, which requires 2 slots */
396 ctx
->outputs
[VARYING_SLOT_CLIP_DIST1
] = var_id
;
397 ctx
->so_output_gl_types
[VARYING_SLOT_CLIP_DIST1
] = var
->type
;
398 ctx
->so_output_types
[VARYING_SLOT_CLIP_DIST1
] = var_type
;
402 slot
= handle_slot(ctx
, slot
);
403 spirv_builder_emit_location(&ctx
->builder
, var_id
, slot
);
405 ctx
->outputs
[var
->data
.location
] = var_id
;
406 ctx
->so_output_gl_types
[var
->data
.location
] = var
->type
;
407 ctx
->so_output_types
[var
->data
.location
] = var_type
;
408 } else if (ctx
->stage
== MESA_SHADER_FRAGMENT
) {
409 if (var
->data
.location
>= FRAG_RESULT_DATA0
) {
410 spirv_builder_emit_location(&ctx
->builder
, var_id
,
411 var
->data
.location
- FRAG_RESULT_DATA0
);
412 spirv_builder_emit_index(&ctx
->builder
, var_id
, var
->data
.index
);
414 switch (var
->data
.location
) {
415 case FRAG_RESULT_COLOR
:
416 unreachable("gl_FragColor should be lowered by now");
418 case FRAG_RESULT_DEPTH
:
419 spirv_builder_emit_builtin(&ctx
->builder
, var_id
, SpvBuiltInFragDepth
);
423 spirv_builder_emit_location(&ctx
->builder
, var_id
,
424 var
->data
.driver_location
);
425 spirv_builder_emit_index(&ctx
->builder
, var_id
, var
->data
.index
);
430 if (var
->data
.location_frac
)
431 spirv_builder_emit_component(&ctx
->builder
, var_id
,
432 var
->data
.location_frac
);
434 switch (var
->data
.interpolation
) {
435 case INTERP_MODE_NONE
:
436 case INTERP_MODE_SMOOTH
: /* XXX spirv doesn't seem to have anything for this */
438 case INTERP_MODE_FLAT
:
439 spirv_builder_emit_decoration(&ctx
->builder
, var_id
, SpvDecorationFlat
);
441 case INTERP_MODE_EXPLICIT
:
442 spirv_builder_emit_decoration(&ctx
->builder
, var_id
, SpvDecorationExplicitInterpAMD
);
444 case INTERP_MODE_NOPERSPECTIVE
:
445 spirv_builder_emit_decoration(&ctx
->builder
, var_id
, SpvDecorationNoPerspective
);
448 unreachable("unknown interpolation value");
451 _mesa_hash_table_insert(ctx
->vars
, var
, (void *)(intptr_t)var_id
);
453 assert(ctx
->num_entry_ifaces
< ARRAY_SIZE(ctx
->entry_ifaces
));
454 ctx
->entry_ifaces
[ctx
->num_entry_ifaces
++] = var_id
;
458 type_to_dim(enum glsl_sampler_dim gdim
, bool *is_ms
)
462 case GLSL_SAMPLER_DIM_1D
:
464 case GLSL_SAMPLER_DIM_2D
:
466 case GLSL_SAMPLER_DIM_3D
:
468 case GLSL_SAMPLER_DIM_CUBE
:
470 case GLSL_SAMPLER_DIM_RECT
:
472 case GLSL_SAMPLER_DIM_BUF
:
474 case GLSL_SAMPLER_DIM_EXTERNAL
:
475 return SpvDim2D
; /* seems dodgy... */
476 case GLSL_SAMPLER_DIM_MS
:
480 fprintf(stderr
, "unknown sampler type %d\n", gdim
);
487 zink_binding(gl_shader_stage stage
, VkDescriptorType type
, int index
)
489 if (stage
== MESA_SHADER_NONE
||
490 stage
>= MESA_SHADER_COMPUTE
) {
491 unreachable("not supported");
493 uint32_t stage_offset
= (uint32_t)stage
* (PIPE_MAX_CONSTANT_BUFFERS
+
494 PIPE_MAX_SHADER_SAMPLER_VIEWS
);
497 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
:
498 assert(index
< PIPE_MAX_CONSTANT_BUFFERS
);
499 return stage_offset
+ index
;
501 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
:
502 assert(index
< PIPE_MAX_SHADER_SAMPLER_VIEWS
);
503 return stage_offset
+ PIPE_MAX_CONSTANT_BUFFERS
+ index
;
506 unreachable("unexpected type");
512 emit_sampler(struct ntv_context
*ctx
, struct nir_variable
*var
)
514 const struct glsl_type
*type
= glsl_without_array(var
->type
);
517 SpvDim dimension
= type_to_dim(glsl_get_sampler_dim(type
), &is_ms
);
519 SpvId result_type
= get_glsl_basetype(ctx
, glsl_get_sampler_result_type(type
));
520 SpvId image_type
= spirv_builder_type_image(&ctx
->builder
, result_type
,
522 glsl_sampler_type_is_array(type
),
524 SpvImageFormatUnknown
);
526 SpvId sampled_type
= spirv_builder_type_sampled_image(&ctx
->builder
,
528 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
529 SpvStorageClassUniformConstant
,
532 if (glsl_type_is_array(var
->type
)) {
533 for (int i
= 0; i
< glsl_get_length(var
->type
); ++i
) {
534 SpvId var_id
= spirv_builder_emit_var(&ctx
->builder
, pointer_type
,
535 SpvStorageClassUniformConstant
);
538 char element_name
[100];
539 snprintf(element_name
, sizeof(element_name
), "%s_%d", var
->name
, i
);
540 spirv_builder_emit_name(&ctx
->builder
, var_id
, var
->name
);
543 int index
= var
->data
.binding
+ i
;
544 assert(!(ctx
->samplers_used
& (1 << index
)));
545 assert(!ctx
->image_types
[index
]);
546 ctx
->image_types
[index
] = image_type
;
547 ctx
->samplers
[index
] = var_id
;
548 ctx
->samplers_used
|= 1 << index
;
550 spirv_builder_emit_descriptor_set(&ctx
->builder
, var_id
,
551 var
->data
.descriptor_set
);
552 int binding
= zink_binding(ctx
->stage
,
553 VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
,
554 var
->data
.binding
+ i
);
555 spirv_builder_emit_binding(&ctx
->builder
, var_id
, binding
);
558 SpvId var_id
= spirv_builder_emit_var(&ctx
->builder
, pointer_type
,
559 SpvStorageClassUniformConstant
);
562 spirv_builder_emit_name(&ctx
->builder
, var_id
, var
->name
);
564 int index
= var
->data
.binding
;
565 assert(!(ctx
->samplers_used
& (1 << index
)));
566 assert(!ctx
->image_types
[index
]);
567 ctx
->image_types
[index
] = image_type
;
568 ctx
->samplers
[index
] = var_id
;
569 ctx
->samplers_used
|= 1 << index
;
571 spirv_builder_emit_descriptor_set(&ctx
->builder
, var_id
,
572 var
->data
.descriptor_set
);
573 int binding
= zink_binding(ctx
->stage
,
574 VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
,
576 spirv_builder_emit_binding(&ctx
->builder
, var_id
, binding
);
581 emit_ubo(struct ntv_context
*ctx
, struct nir_variable
*var
)
583 uint32_t size
= glsl_count_attribute_slots(var
->type
, false);
584 SpvId vec4_type
= get_uvec_type(ctx
, 32, 4);
585 SpvId array_length
= emit_uint_const(ctx
, 32, size
);
586 SpvId array_type
= spirv_builder_type_array(&ctx
->builder
, vec4_type
,
588 spirv_builder_emit_array_stride(&ctx
->builder
, array_type
, 16);
590 // wrap UBO-array in a struct
591 SpvId struct_type
= spirv_builder_type_struct(&ctx
->builder
, &array_type
, 1);
593 char struct_name
[100];
594 snprintf(struct_name
, sizeof(struct_name
), "struct_%s", var
->name
);
595 spirv_builder_emit_name(&ctx
->builder
, struct_type
, struct_name
);
598 spirv_builder_emit_decoration(&ctx
->builder
, struct_type
,
600 spirv_builder_emit_member_offset(&ctx
->builder
, struct_type
, 0, 0);
603 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
604 SpvStorageClassUniform
,
607 SpvId var_id
= spirv_builder_emit_var(&ctx
->builder
, pointer_type
,
608 SpvStorageClassUniform
);
610 spirv_builder_emit_name(&ctx
->builder
, var_id
, var
->name
);
612 assert(ctx
->num_ubos
< ARRAY_SIZE(ctx
->ubos
));
613 ctx
->ubos
[ctx
->num_ubos
++] = var_id
;
615 spirv_builder_emit_descriptor_set(&ctx
->builder
, var_id
,
616 var
->data
.descriptor_set
);
617 int binding
= zink_binding(ctx
->stage
,
618 VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
,
620 spirv_builder_emit_binding(&ctx
->builder
, var_id
, binding
);
624 emit_uniform(struct ntv_context
*ctx
, struct nir_variable
*var
)
626 if (var
->data
.mode
== nir_var_mem_ubo
)
629 assert(var
->data
.mode
== nir_var_uniform
);
630 if (glsl_type_is_sampler(glsl_without_array(var
->type
)))
631 emit_sampler(ctx
, var
);
636 get_vec_from_bit_size(struct ntv_context
*ctx
, uint32_t bit_size
, uint32_t num_components
)
639 return get_bvec_type(ctx
, num_components
);
641 return get_uvec_type(ctx
, bit_size
, num_components
);
642 unreachable("unhandled register bit size");
647 get_src_ssa(struct ntv_context
*ctx
, const nir_ssa_def
*ssa
)
649 assert(ssa
->index
< ctx
->num_defs
);
650 assert(ctx
->defs
[ssa
->index
] != 0);
651 return ctx
->defs
[ssa
->index
];
655 get_var_from_reg(struct ntv_context
*ctx
, nir_register
*reg
)
657 assert(reg
->index
< ctx
->num_regs
);
658 assert(ctx
->regs
[reg
->index
] != 0);
659 return ctx
->regs
[reg
->index
];
663 get_src_reg(struct ntv_context
*ctx
, const nir_reg_src
*reg
)
666 assert(!reg
->indirect
);
667 assert(!reg
->base_offset
);
669 SpvId var
= get_var_from_reg(ctx
, reg
->reg
);
670 SpvId type
= get_vec_from_bit_size(ctx
, reg
->reg
->bit_size
, reg
->reg
->num_components
);
671 return spirv_builder_emit_load(&ctx
->builder
, type
, var
);
675 get_src(struct ntv_context
*ctx
, nir_src
*src
)
678 return get_src_ssa(ctx
, src
->ssa
);
680 return get_src_reg(ctx
, &src
->reg
);
684 get_alu_src_raw(struct ntv_context
*ctx
, nir_alu_instr
*alu
, unsigned src
)
686 assert(!alu
->src
[src
].negate
);
687 assert(!alu
->src
[src
].abs
);
689 SpvId def
= get_src(ctx
, &alu
->src
[src
].src
);
691 unsigned used_channels
= 0;
692 bool need_swizzle
= false;
693 for (unsigned i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; i
++) {
694 if (!nir_alu_instr_channel_used(alu
, src
, i
))
699 if (alu
->src
[src
].swizzle
[i
] != i
)
702 assert(used_channels
!= 0);
704 unsigned live_channels
= nir_src_num_components(alu
->src
[src
].src
);
705 if (used_channels
!= live_channels
)
711 int bit_size
= nir_src_bit_size(alu
->src
[src
].src
);
712 assert(bit_size
== 1 || bit_size
== 32);
714 SpvId raw_type
= bit_size
== 1 ? spirv_builder_type_bool(&ctx
->builder
) :
715 spirv_builder_type_uint(&ctx
->builder
, bit_size
);
717 if (used_channels
== 1) {
718 uint32_t indices
[] = { alu
->src
[src
].swizzle
[0] };
719 return spirv_builder_emit_composite_extract(&ctx
->builder
, raw_type
,
721 ARRAY_SIZE(indices
));
722 } else if (live_channels
== 1) {
723 SpvId raw_vec_type
= spirv_builder_type_vector(&ctx
->builder
,
727 SpvId constituents
[NIR_MAX_VEC_COMPONENTS
] = {0};
728 for (unsigned i
= 0; i
< used_channels
; ++i
)
729 constituents
[i
] = def
;
731 return spirv_builder_emit_composite_construct(&ctx
->builder
,
736 SpvId raw_vec_type
= spirv_builder_type_vector(&ctx
->builder
,
740 uint32_t components
[NIR_MAX_VEC_COMPONENTS
] = {0};
741 size_t num_components
= 0;
742 for (unsigned i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; i
++) {
743 if (!nir_alu_instr_channel_used(alu
, src
, i
))
746 components
[num_components
++] = alu
->src
[src
].swizzle
[i
];
749 return spirv_builder_emit_vector_shuffle(&ctx
->builder
, raw_vec_type
,
750 def
, def
, components
,
756 store_ssa_def(struct ntv_context
*ctx
, nir_ssa_def
*ssa
, SpvId result
)
759 assert(ssa
->index
< ctx
->num_defs
);
760 ctx
->defs
[ssa
->index
] = result
;
764 emit_select(struct ntv_context
*ctx
, SpvId type
, SpvId cond
,
765 SpvId if_true
, SpvId if_false
)
767 return emit_triop(ctx
, SpvOpSelect
, type
, cond
, if_true
, if_false
);
771 uvec_to_bvec(struct ntv_context
*ctx
, SpvId value
, unsigned num_components
)
773 SpvId type
= get_bvec_type(ctx
, num_components
);
774 SpvId zero
= get_uvec_constant(ctx
, 32, num_components
, 0);
775 return emit_binop(ctx
, SpvOpINotEqual
, type
, value
, zero
);
779 emit_bitcast(struct ntv_context
*ctx
, SpvId type
, SpvId value
)
781 return emit_unop(ctx
, SpvOpBitcast
, type
, value
);
785 bitcast_to_uvec(struct ntv_context
*ctx
, SpvId value
, unsigned bit_size
,
786 unsigned num_components
)
788 SpvId type
= get_uvec_type(ctx
, bit_size
, num_components
);
789 return emit_bitcast(ctx
, type
, value
);
793 bitcast_to_ivec(struct ntv_context
*ctx
, SpvId value
, unsigned bit_size
,
794 unsigned num_components
)
796 SpvId type
= get_ivec_type(ctx
, bit_size
, num_components
);
797 return emit_bitcast(ctx
, type
, value
);
801 bitcast_to_fvec(struct ntv_context
*ctx
, SpvId value
, unsigned bit_size
,
802 unsigned num_components
)
804 SpvId type
= get_fvec_type(ctx
, bit_size
, num_components
);
805 return emit_bitcast(ctx
, type
, value
);
809 store_reg_def(struct ntv_context
*ctx
, nir_reg_dest
*reg
, SpvId result
)
811 SpvId var
= get_var_from_reg(ctx
, reg
->reg
);
813 spirv_builder_emit_store(&ctx
->builder
, var
, result
);
817 store_dest_raw(struct ntv_context
*ctx
, nir_dest
*dest
, SpvId result
)
820 store_ssa_def(ctx
, &dest
->ssa
, result
);
822 store_reg_def(ctx
, &dest
->reg
, result
);
826 store_dest(struct ntv_context
*ctx
, nir_dest
*dest
, SpvId result
, nir_alu_type type
)
828 unsigned num_components
= nir_dest_num_components(*dest
);
829 unsigned bit_size
= nir_dest_bit_size(*dest
);
832 switch (nir_alu_type_get_base_type(type
)) {
834 assert("bool should have bit-size 1");
837 break; /* nothing to do! */
841 result
= bitcast_to_uvec(ctx
, result
, bit_size
, num_components
);
845 unreachable("unsupported nir_alu_type");
849 store_dest_raw(ctx
, dest
, result
);
854 emit_unop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
, SpvId src
)
856 return spirv_builder_emit_unop(&ctx
->builder
, op
, type
, src
);
859 /* return the intended xfb output vec type based on base type and vector size */
861 get_output_type(struct ntv_context
*ctx
, unsigned register_index
, unsigned num_components
)
863 const struct glsl_type
*out_type
= ctx
->so_output_gl_types
[register_index
];
864 enum glsl_base_type base_type
= glsl_get_base_type(out_type
);
865 if (base_type
== GLSL_TYPE_ARRAY
)
866 base_type
= glsl_get_base_type(glsl_without_array(out_type
));
870 return get_bvec_type(ctx
, num_components
);
872 case GLSL_TYPE_FLOAT
:
873 return get_fvec_type(ctx
, 32, num_components
);
876 return get_ivec_type(ctx
, 32, num_components
);
879 return get_uvec_type(ctx
, 32, num_components
);
884 unreachable("unknown type");
888 /* for streamout create new outputs, as streamout can be done on individual components,
889 from complete outputs, so we just can't use the created packed outputs */
891 emit_so_info(struct ntv_context
*ctx
, unsigned max_output_location
,
892 const struct zink_so_info
*so_info
)
894 for (unsigned i
= 0; i
< so_info
->so_info
.num_outputs
; i
++) {
895 struct pipe_stream_output so_output
= so_info
->so_info
.output
[i
];
896 unsigned slot
= so_info
->so_info_slots
[i
];
897 SpvId out_type
= get_output_type(ctx
, slot
, so_output
.num_components
);
898 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
899 SpvStorageClassOutput
,
901 SpvId var_id
= spirv_builder_emit_var(&ctx
->builder
, pointer_type
,
902 SpvStorageClassOutput
);
905 snprintf(name
, 10, "xfb%d", i
);
906 spirv_builder_emit_name(&ctx
->builder
, var_id
, name
);
907 spirv_builder_emit_offset(&ctx
->builder
, var_id
, (so_output
.dst_offset
* 4));
908 spirv_builder_emit_xfb_buffer(&ctx
->builder
, var_id
, so_output
.output_buffer
);
909 spirv_builder_emit_xfb_stride(&ctx
->builder
, var_id
, so_info
->so_info
.stride
[so_output
.output_buffer
] * 4);
911 /* output location is incremented by VARYING_SLOT_VAR0 for non-builtins in vtn,
912 * so we need to ensure that the new xfb location slot doesn't conflict with any previously-emitted
915 * if there's no previous outputs that take up user slots (VAR0+) then we can start right after the
916 * glsl builtin reserved slots, otherwise we start just after the adjusted user output slot
918 uint32_t location
= NTV_MIN_RESERVED_SLOTS
+ i
;
919 if (max_output_location
>= VARYING_SLOT_VAR0
)
920 location
= max_output_location
- VARYING_SLOT_VAR0
+ 1 + i
;
921 assert(location
< VARYING_SLOT_VAR0
);
922 assert(location
<= VARYING_SLOT_VAR0
- 8 ||
923 !ctx
->seen_texcoord
[VARYING_SLOT_VAR0
- location
- 1]);
924 spirv_builder_emit_location(&ctx
->builder
, var_id
, location
);
926 /* note: gl_ClipDistance[4] can the 0-indexed member of VARYING_SLOT_CLIP_DIST1 here,
927 * so this is still the 0 component
929 if (so_output
.start_component
)
930 spirv_builder_emit_component(&ctx
->builder
, var_id
, so_output
.start_component
);
932 uint32_t *key
= ralloc_size(ctx
->mem_ctx
, sizeof(uint32_t));
933 *key
= (uint32_t)so_output
.register_index
<< 2 | so_output
.start_component
;
934 _mesa_hash_table_insert(ctx
->so_outputs
, key
, (void *)(intptr_t)var_id
);
936 assert(ctx
->num_entry_ifaces
< ARRAY_SIZE(ctx
->entry_ifaces
));
937 ctx
->entry_ifaces
[ctx
->num_entry_ifaces
++] = var_id
;
942 emit_so_outputs(struct ntv_context
*ctx
,
943 const struct zink_so_info
*so_info
)
945 SpvId loaded_outputs
[VARYING_SLOT_MAX
] = {};
946 for (unsigned i
= 0; i
< so_info
->so_info
.num_outputs
; i
++) {
947 uint32_t components
[NIR_MAX_VEC_COMPONENTS
];
948 unsigned slot
= so_info
->so_info_slots
[i
];
949 struct pipe_stream_output so_output
= so_info
->so_info
.output
[i
];
950 uint32_t so_key
= (uint32_t) so_output
.register_index
<< 2 | so_output
.start_component
;
951 struct hash_entry
*he
= _mesa_hash_table_search(ctx
->so_outputs
, &so_key
);
953 SpvId so_output_var_id
= (SpvId
)(intptr_t)he
->data
;
955 SpvId type
= get_output_type(ctx
, slot
, so_output
.num_components
);
956 SpvId output
= ctx
->outputs
[slot
];
957 SpvId output_type
= ctx
->so_output_types
[slot
];
958 const struct glsl_type
*out_type
= ctx
->so_output_gl_types
[slot
];
960 if (!loaded_outputs
[slot
])
961 loaded_outputs
[slot
] = spirv_builder_emit_load(&ctx
->builder
, output_type
, output
);
962 SpvId src
= loaded_outputs
[slot
];
966 for (unsigned c
= 0; c
< so_output
.num_components
; c
++) {
967 components
[c
] = so_output
.start_component
+ c
;
968 /* this is the second half of a 2 * vec4 array */
969 if (ctx
->stage
== MESA_SHADER_VERTEX
&& slot
== VARYING_SLOT_CLIP_DIST1
)
973 /* if we're emitting a scalar or the type we're emitting matches the output's original type and we're
974 * emitting the same number of components, then we can skip any sort of conversion here
976 if (glsl_type_is_scalar(out_type
) || (type
== output_type
&& glsl_get_length(out_type
) == so_output
.num_components
))
979 /* OpCompositeExtract can only extract scalars for our use here */
980 if (so_output
.num_components
== 1) {
981 result
= spirv_builder_emit_composite_extract(&ctx
->builder
, type
, src
, components
, so_output
.num_components
);
982 } else if (glsl_type_is_vector(out_type
)) {
983 /* OpVectorShuffle can select vector members into a differently-sized vector */
984 result
= spirv_builder_emit_vector_shuffle(&ctx
->builder
, type
,
986 components
, so_output
.num_components
);
987 result
= emit_unop(ctx
, SpvOpBitcast
, type
, result
);
989 /* for arrays, we need to manually extract each desired member
990 * and re-pack them into the desired output type
992 for (unsigned c
= 0; c
< so_output
.num_components
; c
++) {
993 uint32_t member
[] = { so_output
.start_component
+ c
};
994 SpvId base_type
= get_glsl_type(ctx
, glsl_without_array(out_type
));
996 if (ctx
->stage
== MESA_SHADER_VERTEX
&& slot
== VARYING_SLOT_CLIP_DIST1
)
998 components
[c
] = spirv_builder_emit_composite_extract(&ctx
->builder
, base_type
, src
, member
, 1);
1000 result
= spirv_builder_emit_composite_construct(&ctx
->builder
, type
, components
, so_output
.num_components
);
1004 spirv_builder_emit_store(&ctx
->builder
, so_output_var_id
, result
);
1009 emit_binop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
,
1010 SpvId src0
, SpvId src1
)
1012 return spirv_builder_emit_binop(&ctx
->builder
, op
, type
, src0
, src1
);
1016 emit_triop(struct ntv_context
*ctx
, SpvOp op
, SpvId type
,
1017 SpvId src0
, SpvId src1
, SpvId src2
)
1019 return spirv_builder_emit_triop(&ctx
->builder
, op
, type
, src0
, src1
, src2
);
1023 emit_builtin_unop(struct ntv_context
*ctx
, enum GLSLstd450 op
, SpvId type
,
1026 SpvId args
[] = { src
};
1027 return spirv_builder_emit_ext_inst(&ctx
->builder
, type
, ctx
->GLSL_std_450
,
1028 op
, args
, ARRAY_SIZE(args
));
1032 emit_builtin_binop(struct ntv_context
*ctx
, enum GLSLstd450 op
, SpvId type
,
1033 SpvId src0
, SpvId src1
)
1035 SpvId args
[] = { src0
, src1
};
1036 return spirv_builder_emit_ext_inst(&ctx
->builder
, type
, ctx
->GLSL_std_450
,
1037 op
, args
, ARRAY_SIZE(args
));
1041 emit_builtin_triop(struct ntv_context
*ctx
, enum GLSLstd450 op
, SpvId type
,
1042 SpvId src0
, SpvId src1
, SpvId src2
)
1044 SpvId args
[] = { src0
, src1
, src2
};
1045 return spirv_builder_emit_ext_inst(&ctx
->builder
, type
, ctx
->GLSL_std_450
,
1046 op
, args
, ARRAY_SIZE(args
));
1050 get_fvec_constant(struct ntv_context
*ctx
, unsigned bit_size
,
1051 unsigned num_components
, float value
)
1053 assert(bit_size
== 32);
1055 SpvId result
= emit_float_const(ctx
, bit_size
, value
);
1056 if (num_components
== 1)
1059 assert(num_components
> 1);
1060 SpvId components
[num_components
];
1061 for (int i
= 0; i
< num_components
; i
++)
1062 components
[i
] = result
;
1064 SpvId type
= get_fvec_type(ctx
, bit_size
, num_components
);
1065 return spirv_builder_const_composite(&ctx
->builder
, type
, components
,
1070 get_uvec_constant(struct ntv_context
*ctx
, unsigned bit_size
,
1071 unsigned num_components
, uint32_t value
)
1073 assert(bit_size
== 32);
1075 SpvId result
= emit_uint_const(ctx
, bit_size
, value
);
1076 if (num_components
== 1)
1079 assert(num_components
> 1);
1080 SpvId components
[num_components
];
1081 for (int i
= 0; i
< num_components
; i
++)
1082 components
[i
] = result
;
1084 SpvId type
= get_uvec_type(ctx
, bit_size
, num_components
);
1085 return spirv_builder_const_composite(&ctx
->builder
, type
, components
,
1090 get_ivec_constant(struct ntv_context
*ctx
, unsigned bit_size
,
1091 unsigned num_components
, int32_t value
)
1093 assert(bit_size
== 32);
1095 SpvId result
= emit_int_const(ctx
, bit_size
, value
);
1096 if (num_components
== 1)
1099 assert(num_components
> 1);
1100 SpvId components
[num_components
];
1101 for (int i
= 0; i
< num_components
; i
++)
1102 components
[i
] = result
;
1104 SpvId type
= get_ivec_type(ctx
, bit_size
, num_components
);
1105 return spirv_builder_const_composite(&ctx
->builder
, type
, components
,
1109 static inline unsigned
1110 alu_instr_src_components(const nir_alu_instr
*instr
, unsigned src
)
1112 if (nir_op_infos
[instr
->op
].input_sizes
[src
] > 0)
1113 return nir_op_infos
[instr
->op
].input_sizes
[src
];
1115 if (instr
->dest
.dest
.is_ssa
)
1116 return instr
->dest
.dest
.ssa
.num_components
;
1118 return instr
->dest
.dest
.reg
.reg
->num_components
;
1122 get_alu_src(struct ntv_context
*ctx
, nir_alu_instr
*alu
, unsigned src
)
1124 SpvId raw_value
= get_alu_src_raw(ctx
, alu
, src
);
1126 unsigned num_components
= alu_instr_src_components(alu
, src
);
1127 unsigned bit_size
= nir_src_bit_size(alu
->src
[src
].src
);
1128 nir_alu_type type
= nir_op_infos
[alu
->op
].input_types
[src
];
1133 switch (nir_alu_type_get_base_type(type
)) {
1135 unreachable("bool should have bit-size 1");
1138 return bitcast_to_ivec(ctx
, raw_value
, bit_size
, num_components
);
1143 case nir_type_float
:
1144 return bitcast_to_fvec(ctx
, raw_value
, bit_size
, num_components
);
1147 unreachable("unknown nir_alu_type");
1153 store_alu_result(struct ntv_context
*ctx
, nir_alu_instr
*alu
, SpvId result
)
1155 assert(!alu
->dest
.saturate
);
1156 return store_dest(ctx
, &alu
->dest
.dest
, result
,
1157 nir_op_infos
[alu
->op
].output_type
);
1161 get_dest_type(struct ntv_context
*ctx
, nir_dest
*dest
, nir_alu_type type
)
1163 unsigned num_components
= nir_dest_num_components(*dest
);
1164 unsigned bit_size
= nir_dest_bit_size(*dest
);
1167 return get_bvec_type(ctx
, num_components
);
1169 switch (nir_alu_type_get_base_type(type
)) {
1171 unreachable("bool should have bit-size 1");
1174 return get_ivec_type(ctx
, bit_size
, num_components
);
1177 return get_uvec_type(ctx
, bit_size
, num_components
);
1179 case nir_type_float
:
1180 return get_fvec_type(ctx
, bit_size
, num_components
);
1183 unreachable("unsupported nir_alu_type");
1188 emit_alu(struct ntv_context
*ctx
, nir_alu_instr
*alu
)
1190 SpvId src
[nir_op_infos
[alu
->op
].num_inputs
];
1191 unsigned in_bit_sizes
[nir_op_infos
[alu
->op
].num_inputs
];
1192 for (unsigned i
= 0; i
< nir_op_infos
[alu
->op
].num_inputs
; i
++) {
1193 src
[i
] = get_alu_src(ctx
, alu
, i
);
1194 in_bit_sizes
[i
] = nir_src_bit_size(alu
->src
[i
].src
);
1197 SpvId dest_type
= get_dest_type(ctx
, &alu
->dest
.dest
,
1198 nir_op_infos
[alu
->op
].output_type
);
1199 unsigned bit_size
= nir_dest_bit_size(alu
->dest
.dest
);
1200 unsigned num_components
= nir_dest_num_components(alu
->dest
.dest
);
1205 assert(nir_op_infos
[alu
->op
].num_inputs
== 1);
1209 #define UNOP(nir_op, spirv_op) \
1211 assert(nir_op_infos[alu->op].num_inputs == 1); \
1212 result = emit_unop(ctx, spirv_op, dest_type, src[0]); \
1215 UNOP(nir_op_ineg
, SpvOpSNegate
)
1216 UNOP(nir_op_fneg
, SpvOpFNegate
)
1217 UNOP(nir_op_fddx
, SpvOpDPdx
)
1218 UNOP(nir_op_fddx_coarse
, SpvOpDPdxCoarse
)
1219 UNOP(nir_op_fddx_fine
, SpvOpDPdxFine
)
1220 UNOP(nir_op_fddy
, SpvOpDPdy
)
1221 UNOP(nir_op_fddy_coarse
, SpvOpDPdyCoarse
)
1222 UNOP(nir_op_fddy_fine
, SpvOpDPdyFine
)
1223 UNOP(nir_op_f2i32
, SpvOpConvertFToS
)
1224 UNOP(nir_op_f2u32
, SpvOpConvertFToU
)
1225 UNOP(nir_op_i2f32
, SpvOpConvertSToF
)
1226 UNOP(nir_op_u2f32
, SpvOpConvertUToF
)
1227 UNOP(nir_op_bitfield_reverse
, SpvOpBitReverse
)
1232 result
= emit_unop(ctx
, SpvOpLogicalNot
, dest_type
, src
[0]);
1234 result
= emit_unop(ctx
, SpvOpNot
, dest_type
, src
[0]);
1238 assert(nir_op_infos
[alu
->op
].num_inputs
== 1);
1239 result
= emit_select(ctx
, dest_type
, src
[0],
1240 get_ivec_constant(ctx
, 32, num_components
, 1),
1241 get_ivec_constant(ctx
, 32, num_components
, 0));
1245 assert(nir_op_infos
[alu
->op
].num_inputs
== 1);
1246 result
= emit_select(ctx
, dest_type
, src
[0],
1247 get_fvec_constant(ctx
, 32, num_components
, 1),
1248 get_fvec_constant(ctx
, 32, num_components
, 0));
1251 #define BUILTIN_UNOP(nir_op, spirv_op) \
1253 assert(nir_op_infos[alu->op].num_inputs == 1); \
1254 result = emit_builtin_unop(ctx, spirv_op, dest_type, src[0]); \
1257 BUILTIN_UNOP(nir_op_iabs
, GLSLstd450SAbs
)
1258 BUILTIN_UNOP(nir_op_fabs
, GLSLstd450FAbs
)
1259 BUILTIN_UNOP(nir_op_fsqrt
, GLSLstd450Sqrt
)
1260 BUILTIN_UNOP(nir_op_frsq
, GLSLstd450InverseSqrt
)
1261 BUILTIN_UNOP(nir_op_flog2
, GLSLstd450Log2
)
1262 BUILTIN_UNOP(nir_op_fexp2
, GLSLstd450Exp2
)
1263 BUILTIN_UNOP(nir_op_ffract
, GLSLstd450Fract
)
1264 BUILTIN_UNOP(nir_op_ffloor
, GLSLstd450Floor
)
1265 BUILTIN_UNOP(nir_op_fceil
, GLSLstd450Ceil
)
1266 BUILTIN_UNOP(nir_op_ftrunc
, GLSLstd450Trunc
)
1267 BUILTIN_UNOP(nir_op_fround_even
, GLSLstd450RoundEven
)
1268 BUILTIN_UNOP(nir_op_fsign
, GLSLstd450FSign
)
1269 BUILTIN_UNOP(nir_op_isign
, GLSLstd450SSign
)
1270 BUILTIN_UNOP(nir_op_fsin
, GLSLstd450Sin
)
1271 BUILTIN_UNOP(nir_op_fcos
, GLSLstd450Cos
)
1275 assert(nir_op_infos
[alu
->op
].num_inputs
== 1);
1276 result
= emit_binop(ctx
, SpvOpFDiv
, dest_type
,
1277 get_fvec_constant(ctx
, bit_size
, num_components
, 1),
1282 assert(nir_op_infos
[alu
->op
].num_inputs
== 1);
1283 result
= emit_binop(ctx
, SpvOpFOrdNotEqual
, dest_type
, src
[0],
1284 get_fvec_constant(ctx
,
1285 nir_src_bit_size(alu
->src
[0].src
),
1286 num_components
, 0));
1289 assert(nir_op_infos
[alu
->op
].num_inputs
== 1);
1290 result
= emit_binop(ctx
, SpvOpINotEqual
, dest_type
, src
[0],
1291 get_ivec_constant(ctx
,
1292 nir_src_bit_size(alu
->src
[0].src
),
1293 num_components
, 0));
1297 #define BINOP(nir_op, spirv_op) \
1299 assert(nir_op_infos[alu->op].num_inputs == 2); \
1300 result = emit_binop(ctx, spirv_op, dest_type, src[0], src[1]); \
1303 BINOP(nir_op_iadd
, SpvOpIAdd
)
1304 BINOP(nir_op_isub
, SpvOpISub
)
1305 BINOP(nir_op_imul
, SpvOpIMul
)
1306 BINOP(nir_op_idiv
, SpvOpSDiv
)
1307 BINOP(nir_op_udiv
, SpvOpUDiv
)
1308 BINOP(nir_op_umod
, SpvOpUMod
)
1309 BINOP(nir_op_fadd
, SpvOpFAdd
)
1310 BINOP(nir_op_fsub
, SpvOpFSub
)
1311 BINOP(nir_op_fmul
, SpvOpFMul
)
1312 BINOP(nir_op_fdiv
, SpvOpFDiv
)
1313 BINOP(nir_op_fmod
, SpvOpFMod
)
1314 BINOP(nir_op_ilt
, SpvOpSLessThan
)
1315 BINOP(nir_op_ige
, SpvOpSGreaterThanEqual
)
1316 BINOP(nir_op_ult
, SpvOpULessThan
)
1317 BINOP(nir_op_uge
, SpvOpUGreaterThanEqual
)
1318 BINOP(nir_op_flt
, SpvOpFOrdLessThan
)
1319 BINOP(nir_op_fge
, SpvOpFOrdGreaterThanEqual
)
1320 BINOP(nir_op_feq
, SpvOpFOrdEqual
)
1321 BINOP(nir_op_fneu
, SpvOpFUnordNotEqual
)
1322 BINOP(nir_op_ishl
, SpvOpShiftLeftLogical
)
1323 BINOP(nir_op_ishr
, SpvOpShiftRightArithmetic
)
1324 BINOP(nir_op_ushr
, SpvOpShiftRightLogical
)
1325 BINOP(nir_op_ixor
, SpvOpBitwiseXor
)
1328 #define BINOP_LOG(nir_op, spv_op, spv_log_op) \
1330 assert(nir_op_infos[alu->op].num_inputs == 2); \
1331 if (nir_src_bit_size(alu->src[0].src) == 1) \
1332 result = emit_binop(ctx, spv_log_op, dest_type, src[0], src[1]); \
1334 result = emit_binop(ctx, spv_op, dest_type, src[0], src[1]); \
1337 BINOP_LOG(nir_op_iand
, SpvOpBitwiseAnd
, SpvOpLogicalAnd
)
1338 BINOP_LOG(nir_op_ior
, SpvOpBitwiseOr
, SpvOpLogicalOr
)
1339 BINOP_LOG(nir_op_ieq
, SpvOpIEqual
, SpvOpLogicalEqual
)
1340 BINOP_LOG(nir_op_ine
, SpvOpINotEqual
, SpvOpLogicalNotEqual
)
1343 #define BUILTIN_BINOP(nir_op, spirv_op) \
1345 assert(nir_op_infos[alu->op].num_inputs == 2); \
1346 result = emit_builtin_binop(ctx, spirv_op, dest_type, src[0], src[1]); \
1349 BUILTIN_BINOP(nir_op_fmin
, GLSLstd450FMin
)
1350 BUILTIN_BINOP(nir_op_fmax
, GLSLstd450FMax
)
1351 BUILTIN_BINOP(nir_op_imin
, GLSLstd450SMin
)
1352 BUILTIN_BINOP(nir_op_imax
, GLSLstd450SMax
)
1353 BUILTIN_BINOP(nir_op_umin
, GLSLstd450UMin
)
1354 BUILTIN_BINOP(nir_op_umax
, GLSLstd450UMax
)
1355 #undef BUILTIN_BINOP
1360 assert(nir_op_infos
[alu
->op
].num_inputs
== 2);
1361 result
= emit_binop(ctx
, SpvOpDot
, dest_type
, src
[0], src
[1]);
1365 unreachable("should already be lowered away");
1371 assert(nir_op_infos
[alu
->op
].num_inputs
== 2);
1372 int num_components
= nir_dest_num_components(alu
->dest
.dest
);
1373 SpvId bool_type
= get_bvec_type(ctx
, num_components
);
1375 SpvId zero
= emit_float_const(ctx
, bit_size
, 0.0f
);
1376 SpvId one
= emit_float_const(ctx
, bit_size
, 1.0f
);
1377 if (num_components
> 1) {
1378 SpvId zero_comps
[num_components
], one_comps
[num_components
];
1379 for (int i
= 0; i
< num_components
; i
++) {
1380 zero_comps
[i
] = zero
;
1384 zero
= spirv_builder_const_composite(&ctx
->builder
, dest_type
,
1385 zero_comps
, num_components
);
1386 one
= spirv_builder_const_composite(&ctx
->builder
, dest_type
,
1387 one_comps
, num_components
);
1392 case nir_op_seq
: op
= SpvOpFOrdEqual
; break;
1393 case nir_op_sne
: op
= SpvOpFOrdNotEqual
; break;
1394 case nir_op_slt
: op
= SpvOpFOrdLessThan
; break;
1395 case nir_op_sge
: op
= SpvOpFOrdGreaterThanEqual
; break;
1396 default: unreachable("unexpected op");
1399 result
= emit_binop(ctx
, op
, bool_type
, src
[0], src
[1]);
1400 result
= emit_select(ctx
, dest_type
, result
, one
, zero
);
1405 assert(nir_op_infos
[alu
->op
].num_inputs
== 3);
1406 result
= emit_builtin_triop(ctx
, GLSLstd450FMix
, dest_type
,
1407 src
[0], src
[1], src
[2]);
1411 result
= emit_binop(ctx
, SpvOpFOrdGreaterThan
,
1412 get_bvec_type(ctx
, num_components
),
1414 get_fvec_constant(ctx
,
1415 nir_src_bit_size(alu
->src
[0].src
),
1416 num_components
, 0));
1417 result
= emit_select(ctx
, dest_type
, result
, src
[1], src
[2]);
1421 assert(nir_op_infos
[alu
->op
].num_inputs
== 3);
1422 result
= emit_select(ctx
, dest_type
, src
[0], src
[1], src
[2]);
1425 case nir_op_bany_fnequal2
:
1426 case nir_op_bany_fnequal3
:
1427 case nir_op_bany_fnequal4
: {
1428 assert(nir_op_infos
[alu
->op
].num_inputs
== 2);
1429 assert(alu_instr_src_components(alu
, 0) ==
1430 alu_instr_src_components(alu
, 1));
1431 assert(in_bit_sizes
[0] == in_bit_sizes
[1]);
1432 /* The type of Operand 1 and Operand 2 must be a scalar or vector of floating-point type. */
1433 SpvOp op
= in_bit_sizes
[0] == 1 ? SpvOpLogicalNotEqual
: SpvOpFOrdNotEqual
;
1434 result
= emit_binop(ctx
, op
,
1435 get_bvec_type(ctx
, alu_instr_src_components(alu
, 0)),
1437 result
= emit_unop(ctx
, SpvOpAny
, dest_type
, result
);
1441 case nir_op_ball_fequal2
:
1442 case nir_op_ball_fequal3
:
1443 case nir_op_ball_fequal4
: {
1444 assert(nir_op_infos
[alu
->op
].num_inputs
== 2);
1445 assert(alu_instr_src_components(alu
, 0) ==
1446 alu_instr_src_components(alu
, 1));
1447 assert(in_bit_sizes
[0] == in_bit_sizes
[1]);
1448 /* The type of Operand 1 and Operand 2 must be a scalar or vector of floating-point type. */
1449 SpvOp op
= in_bit_sizes
[0] == 1 ? SpvOpLogicalEqual
: SpvOpFOrdEqual
;
1450 result
= emit_binop(ctx
, op
,
1451 get_bvec_type(ctx
, alu_instr_src_components(alu
, 0)),
1453 result
= emit_unop(ctx
, SpvOpAll
, dest_type
, result
);
1457 case nir_op_bany_inequal2
:
1458 case nir_op_bany_inequal3
:
1459 case nir_op_bany_inequal4
: {
1460 assert(nir_op_infos
[alu
->op
].num_inputs
== 2);
1461 assert(alu_instr_src_components(alu
, 0) ==
1462 alu_instr_src_components(alu
, 1));
1463 assert(in_bit_sizes
[0] == in_bit_sizes
[1]);
1464 /* The type of Operand 1 and Operand 2 must be a scalar or vector of integer type. */
1465 SpvOp op
= in_bit_sizes
[0] == 1 ? SpvOpLogicalNotEqual
: SpvOpINotEqual
;
1466 result
= emit_binop(ctx
, op
,
1467 get_bvec_type(ctx
, alu_instr_src_components(alu
, 0)),
1469 result
= emit_unop(ctx
, SpvOpAny
, dest_type
, result
);
1473 case nir_op_ball_iequal2
:
1474 case nir_op_ball_iequal3
:
1475 case nir_op_ball_iequal4
: {
1476 assert(nir_op_infos
[alu
->op
].num_inputs
== 2);
1477 assert(alu_instr_src_components(alu
, 0) ==
1478 alu_instr_src_components(alu
, 1));
1479 assert(in_bit_sizes
[0] == in_bit_sizes
[1]);
1480 /* The type of Operand 1 and Operand 2 must be a scalar or vector of integer type. */
1481 SpvOp op
= in_bit_sizes
[0] == 1 ? SpvOpLogicalEqual
: SpvOpIEqual
;
1482 result
= emit_binop(ctx
, op
,
1483 get_bvec_type(ctx
, alu_instr_src_components(alu
, 0)),
1485 result
= emit_unop(ctx
, SpvOpAll
, dest_type
, result
);
1492 int num_inputs
= nir_op_infos
[alu
->op
].num_inputs
;
1493 assert(2 <= num_inputs
&& num_inputs
<= 4);
1494 result
= spirv_builder_emit_composite_construct(&ctx
->builder
, dest_type
,
1500 fprintf(stderr
, "emit_alu: not implemented (%s)\n",
1501 nir_op_infos
[alu
->op
].name
);
1503 unreachable("unsupported opcode");
1507 store_alu_result(ctx
, alu
, result
);
1511 emit_load_const(struct ntv_context
*ctx
, nir_load_const_instr
*load_const
)
1513 unsigned bit_size
= load_const
->def
.bit_size
;
1514 unsigned num_components
= load_const
->def
.num_components
;
1517 if (num_components
> 1) {
1518 SpvId components
[num_components
];
1519 SpvId type
= get_vec_from_bit_size(ctx
, bit_size
, num_components
);
1520 if (bit_size
== 1) {
1521 for (int i
= 0; i
< num_components
; i
++)
1522 components
[i
] = spirv_builder_const_bool(&ctx
->builder
,
1523 load_const
->value
[i
].b
);
1526 for (int i
= 0; i
< num_components
; i
++)
1527 components
[i
] = emit_uint_const(ctx
, bit_size
,
1528 load_const
->value
[i
].u32
);
1531 constant
= spirv_builder_const_composite(&ctx
->builder
, type
,
1532 components
, num_components
);
1534 assert(num_components
== 1);
1536 constant
= spirv_builder_const_bool(&ctx
->builder
,
1537 load_const
->value
[0].b
);
1539 constant
= emit_uint_const(ctx
, bit_size
, load_const
->value
[0].u32
);
1542 store_ssa_def(ctx
, &load_const
->def
, constant
);
1546 emit_load_ubo(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
1548 nir_const_value
*const_block_index
= nir_src_as_const_value(intr
->src
[0]);
1549 assert(const_block_index
); // no dynamic indexing for now
1550 assert(const_block_index
->u32
== 0); // we only support the default UBO for now
1552 nir_const_value
*const_offset
= nir_src_as_const_value(intr
->src
[1]);
1554 SpvId uvec4_type
= get_uvec_type(ctx
, 32, 4);
1555 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
1556 SpvStorageClassUniform
,
1559 unsigned idx
= const_offset
->u32
;
1560 SpvId member
= emit_uint_const(ctx
, 32, 0);
1561 SpvId offset
= emit_uint_const(ctx
, 32, idx
);
1562 SpvId offsets
[] = { member
, offset
};
1563 SpvId ptr
= spirv_builder_emit_access_chain(&ctx
->builder
, pointer_type
,
1564 ctx
->ubos
[0], offsets
,
1565 ARRAY_SIZE(offsets
));
1566 SpvId result
= spirv_builder_emit_load(&ctx
->builder
, uvec4_type
, ptr
);
1568 SpvId type
= get_dest_uvec_type(ctx
, &intr
->dest
);
1569 unsigned num_components
= nir_dest_num_components(intr
->dest
);
1570 if (num_components
== 1) {
1571 uint32_t components
[] = { 0 };
1572 result
= spirv_builder_emit_composite_extract(&ctx
->builder
,
1576 } else if (num_components
< 4) {
1577 SpvId constituents
[num_components
];
1578 SpvId uint_type
= spirv_builder_type_uint(&ctx
->builder
, 32);
1579 for (uint32_t i
= 0; i
< num_components
; ++i
)
1580 constituents
[i
] = spirv_builder_emit_composite_extract(&ctx
->builder
,
1585 result
= spirv_builder_emit_composite_construct(&ctx
->builder
,
1591 if (nir_dest_bit_size(intr
->dest
) == 1)
1592 result
= uvec_to_bvec(ctx
, result
, num_components
);
1594 store_dest(ctx
, &intr
->dest
, result
, nir_type_uint
);
1596 unreachable("uniform-addressing not yet supported");
1600 emit_discard(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
1602 assert(ctx
->block_started
);
1603 spirv_builder_emit_kill(&ctx
->builder
);
1604 /* discard is weird in NIR, so let's just create an unreachable block after
1605 it and hope that the vulkan driver will DCE any instructinos in it. */
1606 spirv_builder_label(&ctx
->builder
, spirv_builder_new_id(&ctx
->builder
));
1610 emit_load_deref(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
1612 SpvId ptr
= get_src(ctx
, intr
->src
);
1614 SpvId result
= spirv_builder_emit_load(&ctx
->builder
,
1615 get_glsl_type(ctx
, nir_src_as_deref(intr
->src
[0])->type
),
1617 unsigned num_components
= nir_dest_num_components(intr
->dest
);
1618 unsigned bit_size
= nir_dest_bit_size(intr
->dest
);
1619 result
= bitcast_to_uvec(ctx
, result
, bit_size
, num_components
);
1620 store_dest(ctx
, &intr
->dest
, result
, nir_type_uint
);
1624 emit_store_deref(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
1626 SpvId ptr
= get_src(ctx
, &intr
->src
[0]);
1627 SpvId src
= get_src(ctx
, &intr
->src
[1]);
1629 SpvId type
= get_glsl_type(ctx
, nir_src_as_deref(intr
->src
[0])->type
);
1630 SpvId result
= emit_bitcast(ctx
, type
, src
);
1631 spirv_builder_emit_store(&ctx
->builder
, ptr
, result
);
1635 create_builtin_var(struct ntv_context
*ctx
, SpvId var_type
,
1636 SpvStorageClass storage_class
,
1637 const char *name
, SpvBuiltIn builtin
)
1639 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
->builder
,
1642 SpvId var
= spirv_builder_emit_var(&ctx
->builder
, pointer_type
,
1644 spirv_builder_emit_name(&ctx
->builder
, var
, name
);
1645 spirv_builder_emit_builtin(&ctx
->builder
, var
, builtin
);
1647 assert(ctx
->num_entry_ifaces
< ARRAY_SIZE(ctx
->entry_ifaces
));
1648 ctx
->entry_ifaces
[ctx
->num_entry_ifaces
++] = var
;
1653 emit_load_front_face(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
1655 SpvId var_type
= spirv_builder_type_bool(&ctx
->builder
);
1656 if (!ctx
->front_face_var
)
1657 ctx
->front_face_var
= create_builtin_var(ctx
, var_type
,
1658 SpvStorageClassInput
,
1660 SpvBuiltInFrontFacing
);
1662 SpvId result
= spirv_builder_emit_load(&ctx
->builder
, var_type
,
1663 ctx
->front_face_var
);
1664 assert(1 == nir_dest_num_components(intr
->dest
));
1665 store_dest(ctx
, &intr
->dest
, result
, nir_type_bool
);
1669 emit_load_instance_id(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
1671 SpvId var_type
= spirv_builder_type_uint(&ctx
->builder
, 32);
1672 if (!ctx
->instance_id_var
)
1673 ctx
->instance_id_var
= create_builtin_var(ctx
, var_type
,
1674 SpvStorageClassInput
,
1676 SpvBuiltInInstanceIndex
);
1678 SpvId result
= spirv_builder_emit_load(&ctx
->builder
, var_type
,
1679 ctx
->instance_id_var
);
1680 assert(1 == nir_dest_num_components(intr
->dest
));
1681 store_dest(ctx
, &intr
->dest
, result
, nir_type_uint
);
1685 emit_load_vertex_id(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
1687 SpvId var_type
= spirv_builder_type_uint(&ctx
->builder
, 32);
1688 if (!ctx
->vertex_id_var
)
1689 ctx
->vertex_id_var
= create_builtin_var(ctx
, var_type
,
1690 SpvStorageClassInput
,
1692 SpvBuiltInVertexIndex
);
1694 SpvId result
= spirv_builder_emit_load(&ctx
->builder
, var_type
,
1695 ctx
->vertex_id_var
);
1696 assert(1 == nir_dest_num_components(intr
->dest
));
1697 store_dest(ctx
, &intr
->dest
, result
, nir_type_uint
);
1701 emit_intrinsic(struct ntv_context
*ctx
, nir_intrinsic_instr
*intr
)
1703 switch (intr
->intrinsic
) {
1704 case nir_intrinsic_load_ubo
:
1705 emit_load_ubo(ctx
, intr
);
1708 case nir_intrinsic_discard
:
1709 emit_discard(ctx
, intr
);
1712 case nir_intrinsic_load_deref
:
1713 emit_load_deref(ctx
, intr
);
1716 case nir_intrinsic_store_deref
:
1717 emit_store_deref(ctx
, intr
);
1720 case nir_intrinsic_load_front_face
:
1721 emit_load_front_face(ctx
, intr
);
1724 case nir_intrinsic_load_instance_id
:
1725 emit_load_instance_id(ctx
, intr
);
1728 case nir_intrinsic_load_vertex_id
:
1729 emit_load_vertex_id(ctx
, intr
);
1733 fprintf(stderr
, "emit_intrinsic: not implemented (%s)\n",
1734 nir_intrinsic_infos
[intr
->intrinsic
].name
);
1735 unreachable("unsupported intrinsic");
1740 emit_undef(struct ntv_context
*ctx
, nir_ssa_undef_instr
*undef
)
1742 SpvId type
= get_uvec_type(ctx
, undef
->def
.bit_size
,
1743 undef
->def
.num_components
);
1745 store_ssa_def(ctx
, &undef
->def
,
1746 spirv_builder_emit_undef(&ctx
->builder
, type
));
1750 get_src_float(struct ntv_context
*ctx
, nir_src
*src
)
1752 SpvId def
= get_src(ctx
, src
);
1753 unsigned num_components
= nir_src_num_components(*src
);
1754 unsigned bit_size
= nir_src_bit_size(*src
);
1755 return bitcast_to_fvec(ctx
, def
, bit_size
, num_components
);
1759 get_src_int(struct ntv_context
*ctx
, nir_src
*src
)
1761 SpvId def
= get_src(ctx
, src
);
1762 unsigned num_components
= nir_src_num_components(*src
);
1763 unsigned bit_size
= nir_src_bit_size(*src
);
1764 return bitcast_to_ivec(ctx
, def
, bit_size
, num_components
);
1768 tex_instr_is_lod_allowed(nir_tex_instr
*tex
)
1770 /* This can only be used with an OpTypeImage that has a Dim operand of 1D, 2D, 3D, or Cube
1771 * - SPIR-V: 3.14. Image Operands
1774 return (tex
->sampler_dim
== GLSL_SAMPLER_DIM_1D
||
1775 tex
->sampler_dim
== GLSL_SAMPLER_DIM_2D
||
1776 tex
->sampler_dim
== GLSL_SAMPLER_DIM_3D
||
1777 tex
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
);
1781 pad_coord_vector(struct ntv_context
*ctx
, SpvId orig
, unsigned old_size
, unsigned new_size
)
1783 SpvId int_type
= spirv_builder_type_int(&ctx
->builder
, 32);
1784 SpvId type
= get_ivec_type(ctx
, 32, new_size
);
1785 SpvId constituents
[NIR_MAX_VEC_COMPONENTS
] = {0};
1786 SpvId zero
= emit_int_const(ctx
, 32, 0);
1787 assert(new_size
< NIR_MAX_VEC_COMPONENTS
);
1790 constituents
[0] = orig
;
1792 for (unsigned i
= 0; i
< old_size
; i
++)
1793 constituents
[i
] = spirv_builder_emit_vector_extract(&ctx
->builder
, int_type
, orig
, i
);
1796 for (unsigned i
= old_size
; i
< new_size
; i
++)
1797 constituents
[i
] = zero
;
1799 return spirv_builder_emit_composite_construct(&ctx
->builder
, type
,
1800 constituents
, new_size
);
1804 emit_tex(struct ntv_context
*ctx
, nir_tex_instr
*tex
)
1806 assert(tex
->op
== nir_texop_tex
||
1807 tex
->op
== nir_texop_txb
||
1808 tex
->op
== nir_texop_txl
||
1809 tex
->op
== nir_texop_txd
||
1810 tex
->op
== nir_texop_txf
||
1811 tex
->op
== nir_texop_txf_ms
||
1812 tex
->op
== nir_texop_txs
);
1813 assert(tex
->texture_index
== tex
->sampler_index
);
1815 SpvId coord
= 0, proj
= 0, bias
= 0, lod
= 0, dref
= 0, dx
= 0, dy
= 0,
1816 offset
= 0, sample
= 0;
1817 unsigned coord_components
= 0, coord_bitsize
= 0, offset_components
= 0;
1818 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
1819 switch (tex
->src
[i
].src_type
) {
1820 case nir_tex_src_coord
:
1821 if (tex
->op
== nir_texop_txf
||
1822 tex
->op
== nir_texop_txf_ms
)
1823 coord
= get_src_int(ctx
, &tex
->src
[i
].src
);
1825 coord
= get_src_float(ctx
, &tex
->src
[i
].src
);
1826 coord_components
= nir_src_num_components(tex
->src
[i
].src
);
1827 coord_bitsize
= nir_src_bit_size(tex
->src
[i
].src
);
1830 case nir_tex_src_projector
:
1831 assert(nir_src_num_components(tex
->src
[i
].src
) == 1);
1832 proj
= get_src_float(ctx
, &tex
->src
[i
].src
);
1836 case nir_tex_src_offset
:
1837 offset
= get_src_int(ctx
, &tex
->src
[i
].src
);
1838 offset_components
= nir_src_num_components(tex
->src
[i
].src
);
1841 case nir_tex_src_bias
:
1842 assert(tex
->op
== nir_texop_txb
);
1843 bias
= get_src_float(ctx
, &tex
->src
[i
].src
);
1847 case nir_tex_src_lod
:
1848 assert(nir_src_num_components(tex
->src
[i
].src
) == 1);
1849 if (tex
->op
== nir_texop_txf
||
1850 tex
->op
== nir_texop_txf_ms
||
1851 tex
->op
== nir_texop_txs
)
1852 lod
= get_src_int(ctx
, &tex
->src
[i
].src
);
1854 lod
= get_src_float(ctx
, &tex
->src
[i
].src
);
1858 case nir_tex_src_ms_index
:
1859 assert(nir_src_num_components(tex
->src
[i
].src
) == 1);
1860 sample
= get_src_int(ctx
, &tex
->src
[i
].src
);
1863 case nir_tex_src_comparator
:
1864 assert(nir_src_num_components(tex
->src
[i
].src
) == 1);
1865 dref
= get_src_float(ctx
, &tex
->src
[i
].src
);
1869 case nir_tex_src_ddx
:
1870 dx
= get_src_float(ctx
, &tex
->src
[i
].src
);
1874 case nir_tex_src_ddy
:
1875 dy
= get_src_float(ctx
, &tex
->src
[i
].src
);
1880 fprintf(stderr
, "texture source: %d\n", tex
->src
[i
].src_type
);
1881 unreachable("unknown texture source");
1885 if (lod
== 0 && ctx
->stage
!= MESA_SHADER_FRAGMENT
) {
1886 lod
= emit_float_const(ctx
, 32, 0.0f
);
1890 SpvId image_type
= ctx
->image_types
[tex
->texture_index
];
1891 SpvId sampled_type
= spirv_builder_type_sampled_image(&ctx
->builder
,
1894 assert(ctx
->samplers_used
& (1u << tex
->texture_index
));
1895 SpvId load
= spirv_builder_emit_load(&ctx
->builder
, sampled_type
,
1896 ctx
->samplers
[tex
->texture_index
]);
1898 SpvId dest_type
= get_dest_type(ctx
, &tex
->dest
, tex
->dest_type
);
1900 if (!tex_instr_is_lod_allowed(tex
))
1902 if (tex
->op
== nir_texop_txs
) {
1903 SpvId image
= spirv_builder_emit_image(&ctx
->builder
, image_type
, load
);
1904 SpvId result
= spirv_builder_emit_image_query_size(&ctx
->builder
,
1907 store_dest(ctx
, &tex
->dest
, result
, tex
->dest_type
);
1911 if (proj
&& coord_components
> 0) {
1912 SpvId constituents
[coord_components
+ 1];
1913 if (coord_components
== 1)
1914 constituents
[0] = coord
;
1916 assert(coord_components
> 1);
1917 SpvId float_type
= spirv_builder_type_float(&ctx
->builder
, 32);
1918 for (uint32_t i
= 0; i
< coord_components
; ++i
)
1919 constituents
[i
] = spirv_builder_emit_composite_extract(&ctx
->builder
,
1925 constituents
[coord_components
++] = proj
;
1927 SpvId vec_type
= get_fvec_type(ctx
, 32, coord_components
);
1928 coord
= spirv_builder_emit_composite_construct(&ctx
->builder
,
1934 SpvId actual_dest_type
= dest_type
;
1936 actual_dest_type
= spirv_builder_type_float(&ctx
->builder
, 32);
1939 if (tex
->op
== nir_texop_txf
||
1940 tex
->op
== nir_texop_txf_ms
) {
1941 SpvId image
= spirv_builder_emit_image(&ctx
->builder
, image_type
, load
);
1943 /* SPIRV requires matched length vectors for OpIAdd, so if a shader
1944 * uses vecs of differing sizes we need to make a new vec padded with zeroes
1945 * to mimic how GLSL does this implicitly
1947 if (offset_components
> coord_components
)
1948 coord
= pad_coord_vector(ctx
, coord
, coord_components
, offset_components
);
1949 else if (coord_components
> offset_components
)
1950 offset
= pad_coord_vector(ctx
, offset
, offset_components
, coord_components
);
1951 coord
= emit_binop(ctx
, SpvOpIAdd
,
1952 get_ivec_type(ctx
, coord_bitsize
, coord_components
),
1955 result
= spirv_builder_emit_image_fetch(&ctx
->builder
, dest_type
,
1956 image
, coord
, lod
, sample
);
1958 result
= spirv_builder_emit_image_sample(&ctx
->builder
,
1959 actual_dest_type
, load
,
1962 lod
, bias
, dref
, dx
, dy
,
1966 spirv_builder_emit_decoration(&ctx
->builder
, result
,
1967 SpvDecorationRelaxedPrecision
);
1969 if (dref
&& nir_dest_num_components(tex
->dest
) > 1) {
1970 SpvId components
[4] = { result
, result
, result
, result
};
1971 result
= spirv_builder_emit_composite_construct(&ctx
->builder
,
1977 store_dest(ctx
, &tex
->dest
, result
, tex
->dest_type
);
1981 start_block(struct ntv_context
*ctx
, SpvId label
)
1983 /* terminate previous block if needed */
1984 if (ctx
->block_started
)
1985 spirv_builder_emit_branch(&ctx
->builder
, label
);
1987 /* start new block */
1988 spirv_builder_label(&ctx
->builder
, label
);
1989 ctx
->block_started
= true;
1993 branch(struct ntv_context
*ctx
, SpvId label
)
1995 assert(ctx
->block_started
);
1996 spirv_builder_emit_branch(&ctx
->builder
, label
);
1997 ctx
->block_started
= false;
2001 branch_conditional(struct ntv_context
*ctx
, SpvId condition
, SpvId then_id
,
2004 assert(ctx
->block_started
);
2005 spirv_builder_emit_branch_conditional(&ctx
->builder
, condition
,
2007 ctx
->block_started
= false;
2011 emit_jump(struct ntv_context
*ctx
, nir_jump_instr
*jump
)
2013 switch (jump
->type
) {
2014 case nir_jump_break
:
2015 assert(ctx
->loop_break
);
2016 branch(ctx
, ctx
->loop_break
);
2019 case nir_jump_continue
:
2020 assert(ctx
->loop_cont
);
2021 branch(ctx
, ctx
->loop_cont
);
2025 unreachable("Unsupported jump type\n");
2030 emit_deref_var(struct ntv_context
*ctx
, nir_deref_instr
*deref
)
2032 assert(deref
->deref_type
== nir_deref_type_var
);
2034 struct hash_entry
*he
= _mesa_hash_table_search(ctx
->vars
, deref
->var
);
2036 SpvId result
= (SpvId
)(intptr_t)he
->data
;
2037 store_dest_raw(ctx
, &deref
->dest
, result
);
2041 emit_deref_array(struct ntv_context
*ctx
, nir_deref_instr
*deref
)
2043 assert(deref
->deref_type
== nir_deref_type_array
);
2044 nir_variable
*var
= nir_deref_instr_get_variable(deref
);
2046 SpvStorageClass storage_class
;
2047 switch (var
->data
.mode
) {
2048 case nir_var_shader_in
:
2049 storage_class
= SpvStorageClassInput
;
2052 case nir_var_shader_out
:
2053 storage_class
= SpvStorageClassOutput
;
2057 unreachable("Unsupported nir_variable_mode\n");
2060 SpvId index
= get_src(ctx
, &deref
->arr
.index
);
2062 SpvId ptr_type
= spirv_builder_type_pointer(&ctx
->builder
,
2064 get_glsl_type(ctx
, deref
->type
));
2066 SpvId result
= spirv_builder_emit_access_chain(&ctx
->builder
,
2068 get_src(ctx
, &deref
->parent
),
2070 /* uint is a bit of a lie here, it's really just an opaque type */
2071 store_dest(ctx
, &deref
->dest
, result
, nir_type_uint
);
2075 emit_deref(struct ntv_context
*ctx
, nir_deref_instr
*deref
)
2077 switch (deref
->deref_type
) {
2078 case nir_deref_type_var
:
2079 emit_deref_var(ctx
, deref
);
2082 case nir_deref_type_array
:
2083 emit_deref_array(ctx
, deref
);
2087 unreachable("unexpected deref_type");
2092 emit_block(struct ntv_context
*ctx
, struct nir_block
*block
)
2094 start_block(ctx
, block_label(ctx
, block
));
2095 nir_foreach_instr(instr
, block
) {
2096 switch (instr
->type
) {
2097 case nir_instr_type_alu
:
2098 emit_alu(ctx
, nir_instr_as_alu(instr
));
2100 case nir_instr_type_intrinsic
:
2101 emit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
2103 case nir_instr_type_load_const
:
2104 emit_load_const(ctx
, nir_instr_as_load_const(instr
));
2106 case nir_instr_type_ssa_undef
:
2107 emit_undef(ctx
, nir_instr_as_ssa_undef(instr
));
2109 case nir_instr_type_tex
:
2110 emit_tex(ctx
, nir_instr_as_tex(instr
));
2112 case nir_instr_type_phi
:
2113 unreachable("nir_instr_type_phi not supported");
2115 case nir_instr_type_jump
:
2116 emit_jump(ctx
, nir_instr_as_jump(instr
));
2118 case nir_instr_type_call
:
2119 unreachable("nir_instr_type_call not supported");
2121 case nir_instr_type_parallel_copy
:
2122 unreachable("nir_instr_type_parallel_copy not supported");
2124 case nir_instr_type_deref
:
2125 emit_deref(ctx
, nir_instr_as_deref(instr
));
2132 emit_cf_list(struct ntv_context
*ctx
, struct exec_list
*list
);
2135 get_src_bool(struct ntv_context
*ctx
, nir_src
*src
)
2137 assert(nir_src_bit_size(*src
) == 1);
2138 return get_src(ctx
, src
);
2142 emit_if(struct ntv_context
*ctx
, nir_if
*if_stmt
)
2144 SpvId condition
= get_src_bool(ctx
, &if_stmt
->condition
);
2146 SpvId header_id
= spirv_builder_new_id(&ctx
->builder
);
2147 SpvId then_id
= block_label(ctx
, nir_if_first_then_block(if_stmt
));
2148 SpvId endif_id
= spirv_builder_new_id(&ctx
->builder
);
2149 SpvId else_id
= endif_id
;
2151 bool has_else
= !exec_list_is_empty(&if_stmt
->else_list
);
2153 assert(nir_if_first_else_block(if_stmt
)->index
< ctx
->num_blocks
);
2154 else_id
= block_label(ctx
, nir_if_first_else_block(if_stmt
));
2157 /* create a header-block */
2158 start_block(ctx
, header_id
);
2159 spirv_builder_emit_selection_merge(&ctx
->builder
, endif_id
,
2160 SpvSelectionControlMaskNone
);
2161 branch_conditional(ctx
, condition
, then_id
, else_id
);
2163 emit_cf_list(ctx
, &if_stmt
->then_list
);
2166 if (ctx
->block_started
)
2167 branch(ctx
, endif_id
);
2169 emit_cf_list(ctx
, &if_stmt
->else_list
);
2172 start_block(ctx
, endif_id
);
2176 emit_loop(struct ntv_context
*ctx
, nir_loop
*loop
)
2178 SpvId header_id
= spirv_builder_new_id(&ctx
->builder
);
2179 SpvId begin_id
= block_label(ctx
, nir_loop_first_block(loop
));
2180 SpvId break_id
= spirv_builder_new_id(&ctx
->builder
);
2181 SpvId cont_id
= spirv_builder_new_id(&ctx
->builder
);
2183 /* create a header-block */
2184 start_block(ctx
, header_id
);
2185 spirv_builder_loop_merge(&ctx
->builder
, break_id
, cont_id
, SpvLoopControlMaskNone
);
2186 branch(ctx
, begin_id
);
2188 SpvId save_break
= ctx
->loop_break
;
2189 SpvId save_cont
= ctx
->loop_cont
;
2190 ctx
->loop_break
= break_id
;
2191 ctx
->loop_cont
= cont_id
;
2193 emit_cf_list(ctx
, &loop
->body
);
2195 ctx
->loop_break
= save_break
;
2196 ctx
->loop_cont
= save_cont
;
2198 /* loop->body may have already ended our block */
2199 if (ctx
->block_started
)
2200 branch(ctx
, cont_id
);
2201 start_block(ctx
, cont_id
);
2202 branch(ctx
, header_id
);
2204 start_block(ctx
, break_id
);
2208 emit_cf_list(struct ntv_context
*ctx
, struct exec_list
*list
)
2210 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
2211 switch (node
->type
) {
2212 case nir_cf_node_block
:
2213 emit_block(ctx
, nir_cf_node_as_block(node
));
2216 case nir_cf_node_if
:
2217 emit_if(ctx
, nir_cf_node_as_if(node
));
2220 case nir_cf_node_loop
:
2221 emit_loop(ctx
, nir_cf_node_as_loop(node
));
2224 case nir_cf_node_function
:
2225 unreachable("nir_cf_node_function not supported");
2231 struct spirv_shader
*
2232 nir_to_spirv(struct nir_shader
*s
, const struct zink_so_info
*so_info
)
2234 struct spirv_shader
*ret
= NULL
;
2236 struct ntv_context ctx
= {};
2237 ctx
.mem_ctx
= ralloc_context(NULL
);
2238 ctx
.builder
.mem_ctx
= ctx
.mem_ctx
;
2240 switch (s
->info
.stage
) {
2241 case MESA_SHADER_VERTEX
:
2242 case MESA_SHADER_FRAGMENT
:
2243 case MESA_SHADER_COMPUTE
:
2244 spirv_builder_emit_cap(&ctx
.builder
, SpvCapabilityShader
);
2247 case MESA_SHADER_TESS_CTRL
:
2248 case MESA_SHADER_TESS_EVAL
:
2249 spirv_builder_emit_cap(&ctx
.builder
, SpvCapabilityTessellation
);
2252 case MESA_SHADER_GEOMETRY
:
2253 spirv_builder_emit_cap(&ctx
.builder
, SpvCapabilityGeometry
);
2257 unreachable("invalid stage");
2260 // TODO: only enable when needed
2261 if (s
->info
.stage
== MESA_SHADER_FRAGMENT
) {
2262 spirv_builder_emit_cap(&ctx
.builder
, SpvCapabilitySampled1D
);
2263 spirv_builder_emit_cap(&ctx
.builder
, SpvCapabilityImageQuery
);
2264 spirv_builder_emit_cap(&ctx
.builder
, SpvCapabilityDerivativeControl
);
2267 ctx
.stage
= s
->info
.stage
;
2268 ctx
.GLSL_std_450
= spirv_builder_import(&ctx
.builder
, "GLSL.std.450");
2269 spirv_builder_emit_source(&ctx
.builder
, SpvSourceLanguageGLSL
, 450);
2271 spirv_builder_emit_mem_model(&ctx
.builder
, SpvAddressingModelLogical
,
2272 SpvMemoryModelGLSL450
);
2274 SpvExecutionModel exec_model
;
2275 switch (s
->info
.stage
) {
2276 case MESA_SHADER_VERTEX
:
2277 exec_model
= SpvExecutionModelVertex
;
2279 case MESA_SHADER_TESS_CTRL
:
2280 exec_model
= SpvExecutionModelTessellationControl
;
2282 case MESA_SHADER_TESS_EVAL
:
2283 exec_model
= SpvExecutionModelTessellationEvaluation
;
2285 case MESA_SHADER_GEOMETRY
:
2286 exec_model
= SpvExecutionModelGeometry
;
2288 case MESA_SHADER_FRAGMENT
:
2289 exec_model
= SpvExecutionModelFragment
;
2291 case MESA_SHADER_COMPUTE
:
2292 exec_model
= SpvExecutionModelGLCompute
;
2295 unreachable("invalid stage");
2298 SpvId type_void
= spirv_builder_type_void(&ctx
.builder
);
2299 SpvId type_main
= spirv_builder_type_function(&ctx
.builder
, type_void
,
2301 SpvId entry_point
= spirv_builder_new_id(&ctx
.builder
);
2302 spirv_builder_emit_name(&ctx
.builder
, entry_point
, "main");
2304 ctx
.vars
= _mesa_hash_table_create(ctx
.mem_ctx
, _mesa_hash_pointer
,
2305 _mesa_key_pointer_equal
);
2307 ctx
.so_outputs
= _mesa_hash_table_create(ctx
.mem_ctx
, _mesa_hash_u32
,
2308 _mesa_key_u32_equal
);
2310 nir_foreach_shader_in_variable(var
, s
)
2311 emit_input(&ctx
, var
);
2313 nir_foreach_shader_out_variable(var
, s
)
2314 emit_output(&ctx
, var
);
2317 emit_so_info(&ctx
, util_last_bit64(s
->info
.outputs_written
), so_info
);
2318 nir_foreach_variable_with_modes(var
, s
, nir_var_uniform
|
2321 emit_uniform(&ctx
, var
);
2323 if (s
->info
.stage
== MESA_SHADER_FRAGMENT
) {
2324 spirv_builder_emit_exec_mode(&ctx
.builder
, entry_point
,
2325 SpvExecutionModeOriginUpperLeft
);
2326 if (s
->info
.outputs_written
& BITFIELD64_BIT(FRAG_RESULT_DEPTH
))
2327 spirv_builder_emit_exec_mode(&ctx
.builder
, entry_point
,
2328 SpvExecutionModeDepthReplacing
);
2331 if (so_info
&& so_info
->so_info
.num_outputs
) {
2332 spirv_builder_emit_cap(&ctx
.builder
, SpvCapabilityTransformFeedback
);
2333 spirv_builder_emit_exec_mode(&ctx
.builder
, entry_point
,
2334 SpvExecutionModeXfb
);
2337 spirv_builder_function(&ctx
.builder
, entry_point
, type_void
,
2338 SpvFunctionControlMaskNone
,
2341 nir_function_impl
*entry
= nir_shader_get_entrypoint(s
);
2342 nir_metadata_require(entry
, nir_metadata_block_index
);
2344 ctx
.defs
= ralloc_array_size(ctx
.mem_ctx
,
2345 sizeof(SpvId
), entry
->ssa_alloc
);
2348 ctx
.num_defs
= entry
->ssa_alloc
;
2350 nir_index_local_regs(entry
);
2351 ctx
.regs
= ralloc_array_size(ctx
.mem_ctx
,
2352 sizeof(SpvId
), entry
->reg_alloc
);
2355 ctx
.num_regs
= entry
->reg_alloc
;
2357 SpvId
*block_ids
= ralloc_array_size(ctx
.mem_ctx
,
2358 sizeof(SpvId
), entry
->num_blocks
);
2362 for (int i
= 0; i
< entry
->num_blocks
; ++i
)
2363 block_ids
[i
] = spirv_builder_new_id(&ctx
.builder
);
2365 ctx
.block_ids
= block_ids
;
2366 ctx
.num_blocks
= entry
->num_blocks
;
2368 /* emit a block only for the variable declarations */
2369 start_block(&ctx
, spirv_builder_new_id(&ctx
.builder
));
2370 foreach_list_typed(nir_register
, reg
, node
, &entry
->registers
) {
2371 SpvId type
= get_vec_from_bit_size(&ctx
, reg
->bit_size
, reg
->num_components
);
2372 SpvId pointer_type
= spirv_builder_type_pointer(&ctx
.builder
,
2373 SpvStorageClassFunction
,
2375 SpvId var
= spirv_builder_emit_var(&ctx
.builder
, pointer_type
,
2376 SpvStorageClassFunction
);
2378 ctx
.regs
[reg
->index
] = var
;
2381 emit_cf_list(&ctx
, &entry
->body
);
2384 emit_so_outputs(&ctx
, so_info
);
2386 spirv_builder_return(&ctx
.builder
); // doesn't belong here, but whatevz
2387 spirv_builder_function_end(&ctx
.builder
);
2389 spirv_builder_emit_entry_point(&ctx
.builder
, exec_model
, entry_point
,
2390 "main", ctx
.entry_ifaces
,
2391 ctx
.num_entry_ifaces
);
2393 size_t num_words
= spirv_builder_get_num_words(&ctx
.builder
);
2395 ret
= CALLOC_STRUCT(spirv_shader
);
2399 ret
->words
= MALLOC(sizeof(uint32_t) * num_words
);
2403 ret
->num_words
= spirv_builder_get_words(&ctx
.builder
, ret
->words
, num_words
);
2404 assert(ret
->num_words
== num_words
);
2406 ralloc_free(ctx
.mem_ctx
);
2411 ralloc_free(ctx
.mem_ctx
);
2414 spirv_shader_delete(ret
);
2420 spirv_shader_delete(struct spirv_shader
*s
)