2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
5 * based in part on anv driver which is:
6 * Copyright © 2015 Intel Corporation
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28 #include "radv_private.h"
29 #include "radv_shader.h"
30 #include "radv_shader_helper.h"
33 #include <llvm-c/Core.h>
34 #include <llvm-c/TargetMachine.h>
35 #include <llvm-c/Transforms/Scalar.h>
36 #include <llvm-c/Transforms/Utils.h>
39 #include "ac_binary.h"
40 #include "ac_llvm_util.h"
41 #include "ac_llvm_build.h"
42 #include "ac_shader_abi.h"
43 #include "ac_shader_util.h"
44 #include "ac_exp_param.h"
46 #define RADEON_LLVM_MAX_INPUTS (VARYING_SLOT_VAR31 + 1)
48 struct radv_shader_context
{
49 struct ac_llvm_context ac
;
50 const struct radv_nir_compiler_options
*options
;
51 struct radv_shader_variant_info
*shader_info
;
52 struct ac_shader_abi abi
;
54 unsigned max_workgroup_size
;
55 LLVMContextRef context
;
56 LLVMValueRef main_function
;
58 LLVMValueRef descriptor_sets
[RADV_UD_MAX_SETS
];
59 LLVMValueRef ring_offsets
;
61 LLVMValueRef vertex_buffers
;
62 LLVMValueRef rel_auto_id
;
63 LLVMValueRef vs_prim_id
;
64 LLVMValueRef es2gs_offset
;
67 LLVMValueRef merged_wave_info
;
68 LLVMValueRef tess_factor_offset
;
69 LLVMValueRef tes_rel_patch_id
;
73 LLVMValueRef gs2vs_offset
;
74 LLVMValueRef gs_wave_id
;
75 LLVMValueRef gs_vtx_offset
[6];
77 LLVMValueRef esgs_ring
;
78 LLVMValueRef gsvs_ring
[4];
79 LLVMValueRef hs_ring_tess_offchip
;
80 LLVMValueRef hs_ring_tess_factor
;
82 LLVMValueRef persp_sample
, persp_center
, persp_centroid
;
83 LLVMValueRef linear_sample
, linear_center
, linear_centroid
;
86 LLVMValueRef streamout_buffers
;
87 LLVMValueRef streamout_write_idx
;
88 LLVMValueRef streamout_config
;
89 LLVMValueRef streamout_offset
[4];
91 gl_shader_stage stage
;
93 LLVMValueRef inputs
[RADEON_LLVM_MAX_INPUTS
* 4];
94 uint64_t float16_shaded_mask
;
99 bool is_gs_copy_shader
;
100 LLVMValueRef gs_next_vertex
[4];
101 unsigned gs_max_out_vertices
;
103 unsigned tes_primitive_mode
;
105 uint32_t tcs_patch_outputs_read
;
106 uint64_t tcs_outputs_read
;
107 uint32_t tcs_vertices_per_patch
;
108 uint32_t tcs_num_inputs
;
109 uint32_t tcs_num_patches
;
110 uint32_t max_gsvs_emit_size
;
111 uint32_t gsvs_vertex_size
;
114 enum radeon_llvm_calling_convention
{
115 RADEON_LLVM_AMDGPU_VS
= 87,
116 RADEON_LLVM_AMDGPU_GS
= 88,
117 RADEON_LLVM_AMDGPU_PS
= 89,
118 RADEON_LLVM_AMDGPU_CS
= 90,
119 RADEON_LLVM_AMDGPU_HS
= 93,
122 static inline struct radv_shader_context
*
123 radv_shader_context_from_abi(struct ac_shader_abi
*abi
)
125 struct radv_shader_context
*ctx
= NULL
;
126 return container_of(abi
, ctx
, abi
);
129 struct ac_build_if_state
131 struct radv_shader_context
*ctx
;
132 LLVMValueRef condition
;
133 LLVMBasicBlockRef entry_block
;
134 LLVMBasicBlockRef true_block
;
135 LLVMBasicBlockRef false_block
;
136 LLVMBasicBlockRef merge_block
;
139 static LLVMBasicBlockRef
140 ac_build_insert_new_block(struct radv_shader_context
*ctx
, const char *name
)
142 LLVMBasicBlockRef current_block
;
143 LLVMBasicBlockRef next_block
;
144 LLVMBasicBlockRef new_block
;
146 /* get current basic block */
147 current_block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
149 /* chqeck if there's another block after this one */
150 next_block
= LLVMGetNextBasicBlock(current_block
);
152 /* insert the new block before the next block */
153 new_block
= LLVMInsertBasicBlockInContext(ctx
->context
, next_block
, name
);
156 /* append new block after current block */
157 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
158 new_block
= LLVMAppendBasicBlockInContext(ctx
->context
, function
, name
);
164 ac_nir_build_if(struct ac_build_if_state
*ifthen
,
165 struct radv_shader_context
*ctx
,
166 LLVMValueRef condition
)
168 LLVMBasicBlockRef block
= LLVMGetInsertBlock(ctx
->ac
.builder
);
170 memset(ifthen
, 0, sizeof *ifthen
);
172 ifthen
->condition
= condition
;
173 ifthen
->entry_block
= block
;
175 /* create endif/merge basic block for the phi functions */
176 ifthen
->merge_block
= ac_build_insert_new_block(ctx
, "endif-block");
178 /* create/insert true_block before merge_block */
180 LLVMInsertBasicBlockInContext(ctx
->context
,
184 /* successive code goes into the true block */
185 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, ifthen
->true_block
);
192 ac_nir_build_endif(struct ac_build_if_state
*ifthen
)
194 LLVMBuilderRef builder
= ifthen
->ctx
->ac
.builder
;
196 /* Insert branch to the merge block from current block */
197 LLVMBuildBr(builder
, ifthen
->merge_block
);
200 * Now patch in the various branch instructions.
203 /* Insert the conditional branch instruction at the end of entry_block */
204 LLVMPositionBuilderAtEnd(builder
, ifthen
->entry_block
);
205 if (ifthen
->false_block
) {
206 /* we have an else clause */
207 LLVMBuildCondBr(builder
, ifthen
->condition
,
208 ifthen
->true_block
, ifthen
->false_block
);
212 LLVMBuildCondBr(builder
, ifthen
->condition
,
213 ifthen
->true_block
, ifthen
->merge_block
);
216 /* Resume building code at end of the ifthen->merge_block */
217 LLVMPositionBuilderAtEnd(builder
, ifthen
->merge_block
);
221 static LLVMValueRef
get_rel_patch_id(struct radv_shader_context
*ctx
)
223 switch (ctx
->stage
) {
224 case MESA_SHADER_TESS_CTRL
:
225 return ac_unpack_param(&ctx
->ac
, ctx
->abi
.tcs_rel_ids
, 0, 8);
226 case MESA_SHADER_TESS_EVAL
:
227 return ctx
->tes_rel_patch_id
;
230 unreachable("Illegal stage");
235 get_tcs_num_patches(struct radv_shader_context
*ctx
)
237 unsigned num_tcs_input_cp
= ctx
->options
->key
.tcs
.input_vertices
;
238 unsigned num_tcs_output_cp
= ctx
->tcs_vertices_per_patch
;
239 uint32_t input_vertex_size
= ctx
->tcs_num_inputs
* 16;
240 uint32_t input_patch_size
= ctx
->options
->key
.tcs
.input_vertices
* input_vertex_size
;
241 uint32_t num_tcs_outputs
= util_last_bit64(ctx
->shader_info
->info
.tcs
.outputs_written
);
242 uint32_t num_tcs_patch_outputs
= util_last_bit64(ctx
->shader_info
->info
.tcs
.patch_outputs_written
);
243 uint32_t output_vertex_size
= num_tcs_outputs
* 16;
244 uint32_t pervertex_output_patch_size
= ctx
->tcs_vertices_per_patch
* output_vertex_size
;
245 uint32_t output_patch_size
= pervertex_output_patch_size
+ num_tcs_patch_outputs
* 16;
246 unsigned num_patches
;
247 unsigned hardware_lds_size
;
249 /* Ensure that we only need one wave per SIMD so we don't need to check
250 * resource usage. Also ensures that the number of tcs in and out
251 * vertices per threadgroup are at most 256.
253 num_patches
= 64 / MAX2(num_tcs_input_cp
, num_tcs_output_cp
) * 4;
254 /* Make sure that the data fits in LDS. This assumes the shaders only
255 * use LDS for the inputs and outputs.
257 hardware_lds_size
= 32768;
259 /* Looks like STONEY hangs if we use more than 32 KiB LDS in a single
260 * threadgroup, even though there is more than 32 KiB LDS.
262 * Test: dEQP-VK.tessellation.shader_input_output.barrier
264 if (ctx
->options
->chip_class
>= GFX7
&& ctx
->options
->family
!= CHIP_STONEY
)
265 hardware_lds_size
= 65536;
267 num_patches
= MIN2(num_patches
, hardware_lds_size
/ (input_patch_size
+ output_patch_size
));
268 /* Make sure the output data fits in the offchip buffer */
269 num_patches
= MIN2(num_patches
, (ctx
->options
->tess_offchip_block_dw_size
* 4) / output_patch_size
);
270 /* Not necessary for correctness, but improves performance. The
271 * specific value is taken from the proprietary driver.
273 num_patches
= MIN2(num_patches
, 40);
275 /* GFX6 bug workaround - limit LS-HS threadgroups to only one wave. */
276 if (ctx
->options
->chip_class
== GFX6
) {
277 unsigned one_wave
= 64 / MAX2(num_tcs_input_cp
, num_tcs_output_cp
);
278 num_patches
= MIN2(num_patches
, one_wave
);
284 calculate_tess_lds_size(struct radv_shader_context
*ctx
)
286 unsigned num_tcs_input_cp
= ctx
->options
->key
.tcs
.input_vertices
;
287 unsigned num_tcs_output_cp
;
288 unsigned num_tcs_outputs
, num_tcs_patch_outputs
;
289 unsigned input_vertex_size
, output_vertex_size
;
290 unsigned input_patch_size
, output_patch_size
;
291 unsigned pervertex_output_patch_size
;
292 unsigned output_patch0_offset
;
293 unsigned num_patches
;
296 num_tcs_output_cp
= ctx
->tcs_vertices_per_patch
;
297 num_tcs_outputs
= util_last_bit64(ctx
->shader_info
->info
.tcs
.outputs_written
);
298 num_tcs_patch_outputs
= util_last_bit64(ctx
->shader_info
->info
.tcs
.patch_outputs_written
);
300 input_vertex_size
= ctx
->tcs_num_inputs
* 16;
301 output_vertex_size
= num_tcs_outputs
* 16;
303 input_patch_size
= num_tcs_input_cp
* input_vertex_size
;
305 pervertex_output_patch_size
= num_tcs_output_cp
* output_vertex_size
;
306 output_patch_size
= pervertex_output_patch_size
+ num_tcs_patch_outputs
* 16;
308 num_patches
= ctx
->tcs_num_patches
;
309 output_patch0_offset
= input_patch_size
* num_patches
;
311 lds_size
= output_patch0_offset
+ output_patch_size
* num_patches
;
315 /* Tessellation shaders pass outputs to the next shader using LDS.
317 * LS outputs = TCS inputs
318 * TCS outputs = TES inputs
321 * - TCS inputs for patch 0
322 * - TCS inputs for patch 1
323 * - TCS inputs for patch 2 = get_tcs_in_current_patch_offset (if RelPatchID==2)
325 * - TCS outputs for patch 0 = get_tcs_out_patch0_offset
326 * - Per-patch TCS outputs for patch 0 = get_tcs_out_patch0_patch_data_offset
327 * - TCS outputs for patch 1
328 * - Per-patch TCS outputs for patch 1
329 * - TCS outputs for patch 2 = get_tcs_out_current_patch_offset (if RelPatchID==2)
330 * - Per-patch TCS outputs for patch 2 = get_tcs_out_current_patch_data_offset (if RelPatchID==2)
333 * All three shaders VS(LS), TCS, TES share the same LDS space.
336 get_tcs_in_patch_stride(struct radv_shader_context
*ctx
)
338 assert (ctx
->stage
== MESA_SHADER_TESS_CTRL
);
339 uint32_t input_vertex_size
= ctx
->tcs_num_inputs
* 16;
340 uint32_t input_patch_size
= ctx
->options
->key
.tcs
.input_vertices
* input_vertex_size
;
342 input_patch_size
/= 4;
343 return LLVMConstInt(ctx
->ac
.i32
, input_patch_size
, false);
347 get_tcs_out_patch_stride(struct radv_shader_context
*ctx
)
349 uint32_t num_tcs_outputs
= util_last_bit64(ctx
->shader_info
->info
.tcs
.outputs_written
);
350 uint32_t num_tcs_patch_outputs
= util_last_bit64(ctx
->shader_info
->info
.tcs
.patch_outputs_written
);
351 uint32_t output_vertex_size
= num_tcs_outputs
* 16;
352 uint32_t pervertex_output_patch_size
= ctx
->tcs_vertices_per_patch
* output_vertex_size
;
353 uint32_t output_patch_size
= pervertex_output_patch_size
+ num_tcs_patch_outputs
* 16;
354 output_patch_size
/= 4;
355 return LLVMConstInt(ctx
->ac
.i32
, output_patch_size
, false);
359 get_tcs_out_vertex_stride(struct radv_shader_context
*ctx
)
361 uint32_t num_tcs_outputs
= util_last_bit64(ctx
->shader_info
->info
.tcs
.outputs_written
);
362 uint32_t output_vertex_size
= num_tcs_outputs
* 16;
363 output_vertex_size
/= 4;
364 return LLVMConstInt(ctx
->ac
.i32
, output_vertex_size
, false);
368 get_tcs_out_patch0_offset(struct radv_shader_context
*ctx
)
370 assert (ctx
->stage
== MESA_SHADER_TESS_CTRL
);
371 uint32_t input_vertex_size
= ctx
->tcs_num_inputs
* 16;
372 uint32_t input_patch_size
= ctx
->options
->key
.tcs
.input_vertices
* input_vertex_size
;
373 uint32_t output_patch0_offset
= input_patch_size
;
374 unsigned num_patches
= ctx
->tcs_num_patches
;
376 output_patch0_offset
*= num_patches
;
377 output_patch0_offset
/= 4;
378 return LLVMConstInt(ctx
->ac
.i32
, output_patch0_offset
, false);
382 get_tcs_out_patch0_patch_data_offset(struct radv_shader_context
*ctx
)
384 assert (ctx
->stage
== MESA_SHADER_TESS_CTRL
);
385 uint32_t input_vertex_size
= ctx
->tcs_num_inputs
* 16;
386 uint32_t input_patch_size
= ctx
->options
->key
.tcs
.input_vertices
* input_vertex_size
;
387 uint32_t output_patch0_offset
= input_patch_size
;
389 uint32_t num_tcs_outputs
= util_last_bit64(ctx
->shader_info
->info
.tcs
.outputs_written
);
390 uint32_t output_vertex_size
= num_tcs_outputs
* 16;
391 uint32_t pervertex_output_patch_size
= ctx
->tcs_vertices_per_patch
* output_vertex_size
;
392 unsigned num_patches
= ctx
->tcs_num_patches
;
394 output_patch0_offset
*= num_patches
;
395 output_patch0_offset
+= pervertex_output_patch_size
;
396 output_patch0_offset
/= 4;
397 return LLVMConstInt(ctx
->ac
.i32
, output_patch0_offset
, false);
401 get_tcs_in_current_patch_offset(struct radv_shader_context
*ctx
)
403 LLVMValueRef patch_stride
= get_tcs_in_patch_stride(ctx
);
404 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
406 return LLVMBuildMul(ctx
->ac
.builder
, patch_stride
, rel_patch_id
, "");
410 get_tcs_out_current_patch_offset(struct radv_shader_context
*ctx
)
412 LLVMValueRef patch0_offset
= get_tcs_out_patch0_offset(ctx
);
413 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
414 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
416 return ac_build_imad(&ctx
->ac
, patch_stride
, rel_patch_id
,
421 get_tcs_out_current_patch_data_offset(struct radv_shader_context
*ctx
)
423 LLVMValueRef patch0_patch_data_offset
=
424 get_tcs_out_patch0_patch_data_offset(ctx
);
425 LLVMValueRef patch_stride
= get_tcs_out_patch_stride(ctx
);
426 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
428 return ac_build_imad(&ctx
->ac
, patch_stride
, rel_patch_id
,
429 patch0_patch_data_offset
);
434 LLVMTypeRef types
[MAX_ARGS
];
435 LLVMValueRef
*assign
[MAX_ARGS
];
438 uint8_t num_sgprs_used
;
439 uint8_t num_vgprs_used
;
442 enum ac_arg_regfile
{
448 add_arg(struct arg_info
*info
, enum ac_arg_regfile regfile
, LLVMTypeRef type
,
449 LLVMValueRef
*param_ptr
)
451 assert(info
->count
< MAX_ARGS
);
453 info
->assign
[info
->count
] = param_ptr
;
454 info
->types
[info
->count
] = type
;
457 if (regfile
== ARG_SGPR
) {
458 info
->num_sgprs_used
+= ac_get_type_size(type
) / 4;
461 assert(regfile
== ARG_VGPR
);
462 info
->num_vgprs_used
+= ac_get_type_size(type
) / 4;
466 static void assign_arguments(LLVMValueRef main_function
,
467 struct arg_info
*info
)
470 for (i
= 0; i
< info
->count
; i
++) {
472 *info
->assign
[i
] = LLVMGetParam(main_function
, i
);
477 create_llvm_function(LLVMContextRef ctx
, LLVMModuleRef module
,
478 LLVMBuilderRef builder
, LLVMTypeRef
*return_types
,
479 unsigned num_return_elems
,
480 struct arg_info
*args
,
481 unsigned max_workgroup_size
,
482 const struct radv_nir_compiler_options
*options
)
484 LLVMTypeRef main_function_type
, ret_type
;
485 LLVMBasicBlockRef main_function_body
;
487 if (num_return_elems
)
488 ret_type
= LLVMStructTypeInContext(ctx
, return_types
,
489 num_return_elems
, true);
491 ret_type
= LLVMVoidTypeInContext(ctx
);
493 /* Setup the function */
495 LLVMFunctionType(ret_type
, args
->types
, args
->count
, 0);
496 LLVMValueRef main_function
=
497 LLVMAddFunction(module
, "main", main_function_type
);
499 LLVMAppendBasicBlockInContext(ctx
, main_function
, "main_body");
500 LLVMPositionBuilderAtEnd(builder
, main_function_body
);
502 LLVMSetFunctionCallConv(main_function
, RADEON_LLVM_AMDGPU_CS
);
503 for (unsigned i
= 0; i
< args
->sgpr_count
; ++i
) {
504 LLVMValueRef P
= LLVMGetParam(main_function
, i
);
506 ac_add_function_attr(ctx
, main_function
, i
+ 1, AC_FUNC_ATTR_INREG
);
508 if (LLVMGetTypeKind(LLVMTypeOf(P
)) == LLVMPointerTypeKind
) {
509 ac_add_function_attr(ctx
, main_function
, i
+ 1, AC_FUNC_ATTR_NOALIAS
);
510 ac_add_attr_dereferenceable(P
, UINT64_MAX
);
514 if (options
->address32_hi
) {
515 ac_llvm_add_target_dep_function_attr(main_function
,
516 "amdgpu-32bit-address-high-bits",
517 options
->address32_hi
);
520 ac_llvm_set_workgroup_size(main_function
, max_workgroup_size
);
522 if (options
->unsafe_math
) {
523 /* These were copied from some LLVM test. */
524 LLVMAddTargetDependentFunctionAttr(main_function
,
525 "less-precise-fpmad",
527 LLVMAddTargetDependentFunctionAttr(main_function
,
530 LLVMAddTargetDependentFunctionAttr(main_function
,
533 LLVMAddTargetDependentFunctionAttr(main_function
,
536 LLVMAddTargetDependentFunctionAttr(main_function
,
537 "no-signed-zeros-fp-math",
540 return main_function
;
545 set_loc(struct radv_userdata_info
*ud_info
, uint8_t *sgpr_idx
,
548 ud_info
->sgpr_idx
= *sgpr_idx
;
549 ud_info
->num_sgprs
= num_sgprs
;
550 *sgpr_idx
+= num_sgprs
;
554 set_loc_shader(struct radv_shader_context
*ctx
, int idx
, uint8_t *sgpr_idx
,
557 struct radv_userdata_info
*ud_info
=
558 &ctx
->shader_info
->user_sgprs_locs
.shader_data
[idx
];
561 set_loc(ud_info
, sgpr_idx
, num_sgprs
);
565 set_loc_shader_ptr(struct radv_shader_context
*ctx
, int idx
, uint8_t *sgpr_idx
)
567 bool use_32bit_pointers
= idx
!= AC_UD_SCRATCH_RING_OFFSETS
;
569 set_loc_shader(ctx
, idx
, sgpr_idx
, use_32bit_pointers
? 1 : 2);
573 set_loc_desc(struct radv_shader_context
*ctx
, int idx
, uint8_t *sgpr_idx
)
575 struct radv_userdata_locations
*locs
=
576 &ctx
->shader_info
->user_sgprs_locs
;
577 struct radv_userdata_info
*ud_info
= &locs
->descriptor_sets
[idx
];
580 set_loc(ud_info
, sgpr_idx
, 1);
582 locs
->descriptor_sets_enabled
|= 1 << idx
;
585 struct user_sgpr_info
{
586 bool need_ring_offsets
;
587 bool indirect_all_descriptor_sets
;
588 uint8_t remaining_sgprs
;
591 static bool needs_view_index_sgpr(struct radv_shader_context
*ctx
,
592 gl_shader_stage stage
)
595 case MESA_SHADER_VERTEX
:
596 if (ctx
->shader_info
->info
.needs_multiview_view_index
||
597 (!ctx
->options
->key
.vs
.as_es
&& !ctx
->options
->key
.vs
.as_ls
&& ctx
->options
->key
.has_multiview_view_index
))
600 case MESA_SHADER_TESS_EVAL
:
601 if (ctx
->shader_info
->info
.needs_multiview_view_index
|| (!ctx
->options
->key
.tes
.as_es
&& ctx
->options
->key
.has_multiview_view_index
))
604 case MESA_SHADER_GEOMETRY
:
605 case MESA_SHADER_TESS_CTRL
:
606 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
616 count_vs_user_sgprs(struct radv_shader_context
*ctx
)
620 if (ctx
->shader_info
->info
.vs
.has_vertex_buffers
)
622 count
+= ctx
->shader_info
->info
.vs
.needs_draw_id
? 3 : 2;
627 static void allocate_inline_push_consts(struct radv_shader_context
*ctx
,
628 struct user_sgpr_info
*user_sgpr_info
)
630 uint8_t remaining_sgprs
= user_sgpr_info
->remaining_sgprs
;
632 /* Only supported if shaders use push constants. */
633 if (ctx
->shader_info
->info
.min_push_constant_used
== UINT8_MAX
)
636 /* Only supported if shaders don't have indirect push constants. */
637 if (ctx
->shader_info
->info
.has_indirect_push_constants
)
640 /* Only supported for 32-bit push constants. */
641 if (!ctx
->shader_info
->info
.has_only_32bit_push_constants
)
644 uint8_t num_push_consts
=
645 (ctx
->shader_info
->info
.max_push_constant_used
-
646 ctx
->shader_info
->info
.min_push_constant_used
) / 4;
648 /* Check if the number of user SGPRs is large enough. */
649 if (num_push_consts
< remaining_sgprs
) {
650 ctx
->shader_info
->info
.num_inline_push_consts
= num_push_consts
;
652 ctx
->shader_info
->info
.num_inline_push_consts
= remaining_sgprs
;
655 /* Clamp to the maximum number of allowed inlined push constants. */
656 if (ctx
->shader_info
->info
.num_inline_push_consts
> AC_MAX_INLINE_PUSH_CONSTS
)
657 ctx
->shader_info
->info
.num_inline_push_consts
= AC_MAX_INLINE_PUSH_CONSTS
;
659 if (ctx
->shader_info
->info
.num_inline_push_consts
== num_push_consts
&&
660 !ctx
->shader_info
->info
.loads_dynamic_offsets
) {
661 /* Disable the default push constants path if all constants are
662 * inlined and if shaders don't use dynamic descriptors.
664 ctx
->shader_info
->info
.loads_push_constants
= false;
667 ctx
->shader_info
->info
.base_inline_push_consts
=
668 ctx
->shader_info
->info
.min_push_constant_used
/ 4;
671 static void allocate_user_sgprs(struct radv_shader_context
*ctx
,
672 gl_shader_stage stage
,
673 bool has_previous_stage
,
674 gl_shader_stage previous_stage
,
675 bool needs_view_index
,
676 struct user_sgpr_info
*user_sgpr_info
)
678 uint8_t user_sgpr_count
= 0;
680 memset(user_sgpr_info
, 0, sizeof(struct user_sgpr_info
));
682 /* until we sort out scratch/global buffers always assign ring offsets for gs/vs/es */
683 if (stage
== MESA_SHADER_GEOMETRY
||
684 stage
== MESA_SHADER_VERTEX
||
685 stage
== MESA_SHADER_TESS_CTRL
||
686 stage
== MESA_SHADER_TESS_EVAL
||
687 ctx
->is_gs_copy_shader
)
688 user_sgpr_info
->need_ring_offsets
= true;
690 if (stage
== MESA_SHADER_FRAGMENT
&&
691 ctx
->shader_info
->info
.ps
.needs_sample_positions
)
692 user_sgpr_info
->need_ring_offsets
= true;
694 /* 2 user sgprs will nearly always be allocated for scratch/rings */
695 if (ctx
->options
->supports_spill
|| user_sgpr_info
->need_ring_offsets
) {
696 user_sgpr_count
+= 2;
700 case MESA_SHADER_COMPUTE
:
701 if (ctx
->shader_info
->info
.cs
.uses_grid_size
)
702 user_sgpr_count
+= 3;
704 case MESA_SHADER_FRAGMENT
:
705 user_sgpr_count
+= ctx
->shader_info
->info
.ps
.needs_sample_positions
;
707 case MESA_SHADER_VERTEX
:
708 if (!ctx
->is_gs_copy_shader
)
709 user_sgpr_count
+= count_vs_user_sgprs(ctx
);
711 case MESA_SHADER_TESS_CTRL
:
712 if (has_previous_stage
) {
713 if (previous_stage
== MESA_SHADER_VERTEX
)
714 user_sgpr_count
+= count_vs_user_sgprs(ctx
);
717 case MESA_SHADER_TESS_EVAL
:
719 case MESA_SHADER_GEOMETRY
:
720 if (has_previous_stage
) {
721 if (previous_stage
== MESA_SHADER_VERTEX
) {
722 user_sgpr_count
+= count_vs_user_sgprs(ctx
);
730 if (needs_view_index
)
733 if (ctx
->shader_info
->info
.loads_push_constants
)
736 if (ctx
->streamout_buffers
)
739 uint32_t available_sgprs
= ctx
->options
->chip_class
>= GFX9
&& stage
!= MESA_SHADER_COMPUTE
? 32 : 16;
740 uint32_t remaining_sgprs
= available_sgprs
- user_sgpr_count
;
741 uint32_t num_desc_set
=
742 util_bitcount(ctx
->shader_info
->info
.desc_set_used_mask
);
744 if (remaining_sgprs
< num_desc_set
) {
745 user_sgpr_info
->indirect_all_descriptor_sets
= true;
746 user_sgpr_info
->remaining_sgprs
= remaining_sgprs
- 1;
748 user_sgpr_info
->remaining_sgprs
= remaining_sgprs
- num_desc_set
;
751 allocate_inline_push_consts(ctx
, user_sgpr_info
);
755 declare_global_input_sgprs(struct radv_shader_context
*ctx
,
756 const struct user_sgpr_info
*user_sgpr_info
,
757 struct arg_info
*args
,
758 LLVMValueRef
*desc_sets
)
760 LLVMTypeRef type
= ac_array_in_const32_addr_space(ctx
->ac
.i8
);
762 /* 1 for each descriptor set */
763 if (!user_sgpr_info
->indirect_all_descriptor_sets
) {
764 uint32_t mask
= ctx
->shader_info
->info
.desc_set_used_mask
;
767 int i
= u_bit_scan(&mask
);
769 add_arg(args
, ARG_SGPR
, type
, &ctx
->descriptor_sets
[i
]);
772 add_arg(args
, ARG_SGPR
, ac_array_in_const32_addr_space(type
),
776 if (ctx
->shader_info
->info
.loads_push_constants
) {
777 /* 1 for push constants and dynamic descriptors */
778 add_arg(args
, ARG_SGPR
, type
, &ctx
->abi
.push_constants
);
781 for (unsigned i
= 0; i
< ctx
->shader_info
->info
.num_inline_push_consts
; i
++) {
782 add_arg(args
, ARG_SGPR
, ctx
->ac
.i32
,
783 &ctx
->abi
.inline_push_consts
[i
]);
785 ctx
->abi
.num_inline_push_consts
= ctx
->shader_info
->info
.num_inline_push_consts
;
786 ctx
->abi
.base_inline_push_consts
= ctx
->shader_info
->info
.base_inline_push_consts
;
788 if (ctx
->shader_info
->info
.so
.num_outputs
) {
789 add_arg(args
, ARG_SGPR
,
790 ac_array_in_const32_addr_space(ctx
->ac
.v4i32
),
791 &ctx
->streamout_buffers
);
796 declare_vs_specific_input_sgprs(struct radv_shader_context
*ctx
,
797 gl_shader_stage stage
,
798 bool has_previous_stage
,
799 gl_shader_stage previous_stage
,
800 struct arg_info
*args
)
802 if (!ctx
->is_gs_copy_shader
&&
803 (stage
== MESA_SHADER_VERTEX
||
804 (has_previous_stage
&& previous_stage
== MESA_SHADER_VERTEX
))) {
805 if (ctx
->shader_info
->info
.vs
.has_vertex_buffers
) {
806 add_arg(args
, ARG_SGPR
,
807 ac_array_in_const32_addr_space(ctx
->ac
.v4i32
),
808 &ctx
->vertex_buffers
);
810 add_arg(args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->abi
.base_vertex
);
811 add_arg(args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->abi
.start_instance
);
812 if (ctx
->shader_info
->info
.vs
.needs_draw_id
) {
813 add_arg(args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->abi
.draw_id
);
819 declare_vs_input_vgprs(struct radv_shader_context
*ctx
, struct arg_info
*args
)
821 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.vertex_id
);
822 if (!ctx
->is_gs_copy_shader
) {
823 if (ctx
->options
->key
.vs
.as_ls
) {
824 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->rel_auto_id
);
825 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.instance_id
);
827 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.instance_id
);
828 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->vs_prim_id
);
830 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, NULL
); /* unused */
835 declare_streamout_sgprs(struct radv_shader_context
*ctx
, gl_shader_stage stage
,
836 struct arg_info
*args
)
840 /* Streamout SGPRs. */
841 if (ctx
->shader_info
->info
.so
.num_outputs
) {
842 assert(stage
== MESA_SHADER_VERTEX
||
843 stage
== MESA_SHADER_TESS_EVAL
);
845 if (stage
!= MESA_SHADER_TESS_EVAL
) {
846 add_arg(args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->streamout_config
);
848 args
->assign
[args
->count
- 1] = &ctx
->streamout_config
;
849 args
->types
[args
->count
- 1] = ctx
->ac
.i32
;
852 add_arg(args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->streamout_write_idx
);
855 /* A streamout buffer offset is loaded if the stride is non-zero. */
856 for (i
= 0; i
< 4; i
++) {
857 if (!ctx
->shader_info
->info
.so
.strides
[i
])
860 add_arg(args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->streamout_offset
[i
]);
865 declare_tes_input_vgprs(struct radv_shader_context
*ctx
, struct arg_info
*args
)
867 add_arg(args
, ARG_VGPR
, ctx
->ac
.f32
, &ctx
->tes_u
);
868 add_arg(args
, ARG_VGPR
, ctx
->ac
.f32
, &ctx
->tes_v
);
869 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->tes_rel_patch_id
);
870 add_arg(args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.tes_patch_id
);
874 set_global_input_locs(struct radv_shader_context
*ctx
,
875 const struct user_sgpr_info
*user_sgpr_info
,
876 LLVMValueRef desc_sets
, uint8_t *user_sgpr_idx
)
878 uint32_t mask
= ctx
->shader_info
->info
.desc_set_used_mask
;
880 if (!user_sgpr_info
->indirect_all_descriptor_sets
) {
882 int i
= u_bit_scan(&mask
);
884 set_loc_desc(ctx
, i
, user_sgpr_idx
);
887 set_loc_shader_ptr(ctx
, AC_UD_INDIRECT_DESCRIPTOR_SETS
,
891 int i
= u_bit_scan(&mask
);
893 ctx
->descriptor_sets
[i
] =
894 ac_build_load_to_sgpr(&ctx
->ac
, desc_sets
,
895 LLVMConstInt(ctx
->ac
.i32
, i
, false));
899 ctx
->shader_info
->need_indirect_descriptor_sets
= true;
902 if (ctx
->shader_info
->info
.loads_push_constants
) {
903 set_loc_shader_ptr(ctx
, AC_UD_PUSH_CONSTANTS
, user_sgpr_idx
);
906 if (ctx
->shader_info
->info
.num_inline_push_consts
) {
907 set_loc_shader(ctx
, AC_UD_INLINE_PUSH_CONSTANTS
, user_sgpr_idx
,
908 ctx
->shader_info
->info
.num_inline_push_consts
);
911 if (ctx
->streamout_buffers
) {
912 set_loc_shader_ptr(ctx
, AC_UD_STREAMOUT_BUFFERS
,
918 set_vs_specific_input_locs(struct radv_shader_context
*ctx
,
919 gl_shader_stage stage
, bool has_previous_stage
,
920 gl_shader_stage previous_stage
,
921 uint8_t *user_sgpr_idx
)
923 if (!ctx
->is_gs_copy_shader
&&
924 (stage
== MESA_SHADER_VERTEX
||
925 (has_previous_stage
&& previous_stage
== MESA_SHADER_VERTEX
))) {
926 if (ctx
->shader_info
->info
.vs
.has_vertex_buffers
) {
927 set_loc_shader_ptr(ctx
, AC_UD_VS_VERTEX_BUFFERS
,
932 if (ctx
->shader_info
->info
.vs
.needs_draw_id
)
935 set_loc_shader(ctx
, AC_UD_VS_BASE_VERTEX_START_INSTANCE
,
936 user_sgpr_idx
, vs_num
);
940 static void set_llvm_calling_convention(LLVMValueRef func
,
941 gl_shader_stage stage
)
943 enum radeon_llvm_calling_convention calling_conv
;
946 case MESA_SHADER_VERTEX
:
947 case MESA_SHADER_TESS_EVAL
:
948 calling_conv
= RADEON_LLVM_AMDGPU_VS
;
950 case MESA_SHADER_GEOMETRY
:
951 calling_conv
= RADEON_LLVM_AMDGPU_GS
;
953 case MESA_SHADER_TESS_CTRL
:
954 calling_conv
= RADEON_LLVM_AMDGPU_HS
;
956 case MESA_SHADER_FRAGMENT
:
957 calling_conv
= RADEON_LLVM_AMDGPU_PS
;
959 case MESA_SHADER_COMPUTE
:
960 calling_conv
= RADEON_LLVM_AMDGPU_CS
;
963 unreachable("Unhandle shader type");
966 LLVMSetFunctionCallConv(func
, calling_conv
);
969 static void create_function(struct radv_shader_context
*ctx
,
970 gl_shader_stage stage
,
971 bool has_previous_stage
,
972 gl_shader_stage previous_stage
)
974 uint8_t user_sgpr_idx
;
975 struct user_sgpr_info user_sgpr_info
;
976 struct arg_info args
= {};
977 LLVMValueRef desc_sets
;
978 bool needs_view_index
= needs_view_index_sgpr(ctx
, stage
);
979 allocate_user_sgprs(ctx
, stage
, has_previous_stage
,
980 previous_stage
, needs_view_index
, &user_sgpr_info
);
982 if (user_sgpr_info
.need_ring_offsets
&& !ctx
->options
->supports_spill
) {
983 add_arg(&args
, ARG_SGPR
, ac_array_in_const_addr_space(ctx
->ac
.v4i32
),
988 case MESA_SHADER_COMPUTE
:
989 declare_global_input_sgprs(ctx
, &user_sgpr_info
, &args
,
992 if (ctx
->shader_info
->info
.cs
.uses_grid_size
) {
993 add_arg(&args
, ARG_SGPR
, ctx
->ac
.v3i32
,
994 &ctx
->abi
.num_work_groups
);
997 for (int i
= 0; i
< 3; i
++) {
998 ctx
->abi
.workgroup_ids
[i
] = NULL
;
999 if (ctx
->shader_info
->info
.cs
.uses_block_id
[i
]) {
1000 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
1001 &ctx
->abi
.workgroup_ids
[i
]);
1005 if (ctx
->shader_info
->info
.cs
.uses_local_invocation_idx
)
1006 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->abi
.tg_size
);
1007 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v3i32
,
1008 &ctx
->abi
.local_invocation_ids
);
1010 case MESA_SHADER_VERTEX
:
1011 declare_global_input_sgprs(ctx
, &user_sgpr_info
, &args
,
1014 declare_vs_specific_input_sgprs(ctx
, stage
, has_previous_stage
,
1015 previous_stage
, &args
);
1017 if (needs_view_index
)
1018 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
1019 &ctx
->abi
.view_index
);
1020 if (ctx
->options
->key
.vs
.as_es
) {
1021 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
1022 &ctx
->es2gs_offset
);
1023 } else if (ctx
->options
->key
.vs
.as_ls
) {
1024 /* no extra parameters */
1026 declare_streamout_sgprs(ctx
, stage
, &args
);
1029 declare_vs_input_vgprs(ctx
, &args
);
1031 case MESA_SHADER_TESS_CTRL
:
1032 if (has_previous_stage
) {
1033 // First 6 system regs
1034 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->oc_lds
);
1035 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
1036 &ctx
->merged_wave_info
);
1037 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
1038 &ctx
->tess_factor_offset
);
1040 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
); // scratch offset
1041 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
); // unknown
1042 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
); // unknown
1044 declare_global_input_sgprs(ctx
, &user_sgpr_info
, &args
,
1047 declare_vs_specific_input_sgprs(ctx
, stage
,
1049 previous_stage
, &args
);
1051 if (needs_view_index
)
1052 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
1053 &ctx
->abi
.view_index
);
1055 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
1056 &ctx
->abi
.tcs_patch_id
);
1057 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
1058 &ctx
->abi
.tcs_rel_ids
);
1060 declare_vs_input_vgprs(ctx
, &args
);
1062 declare_global_input_sgprs(ctx
, &user_sgpr_info
, &args
,
1065 if (needs_view_index
)
1066 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
1067 &ctx
->abi
.view_index
);
1069 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->oc_lds
);
1070 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
1071 &ctx
->tess_factor_offset
);
1072 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
1073 &ctx
->abi
.tcs_patch_id
);
1074 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
1075 &ctx
->abi
.tcs_rel_ids
);
1078 case MESA_SHADER_TESS_EVAL
:
1079 declare_global_input_sgprs(ctx
, &user_sgpr_info
, &args
,
1082 if (needs_view_index
)
1083 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
1084 &ctx
->abi
.view_index
);
1086 if (ctx
->options
->key
.tes
.as_es
) {
1087 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->oc_lds
);
1088 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
);
1089 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
1090 &ctx
->es2gs_offset
);
1092 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
);
1093 declare_streamout_sgprs(ctx
, stage
, &args
);
1094 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->oc_lds
);
1096 declare_tes_input_vgprs(ctx
, &args
);
1098 case MESA_SHADER_GEOMETRY
:
1099 if (has_previous_stage
) {
1100 // First 6 system regs
1101 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
1102 &ctx
->gs2vs_offset
);
1103 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
1104 &ctx
->merged_wave_info
);
1105 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->oc_lds
);
1107 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
); // scratch offset
1108 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
); // unknown
1109 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, NULL
); // unknown
1111 declare_global_input_sgprs(ctx
, &user_sgpr_info
, &args
,
1114 if (previous_stage
!= MESA_SHADER_TESS_EVAL
) {
1115 declare_vs_specific_input_sgprs(ctx
, stage
,
1121 if (needs_view_index
)
1122 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
1123 &ctx
->abi
.view_index
);
1125 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
1126 &ctx
->gs_vtx_offset
[0]);
1127 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
1128 &ctx
->gs_vtx_offset
[2]);
1129 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
1130 &ctx
->abi
.gs_prim_id
);
1131 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
1132 &ctx
->abi
.gs_invocation_id
);
1133 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
1134 &ctx
->gs_vtx_offset
[4]);
1136 if (previous_stage
== MESA_SHADER_VERTEX
) {
1137 declare_vs_input_vgprs(ctx
, &args
);
1139 declare_tes_input_vgprs(ctx
, &args
);
1142 declare_global_input_sgprs(ctx
, &user_sgpr_info
, &args
,
1145 if (needs_view_index
)
1146 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
,
1147 &ctx
->abi
.view_index
);
1149 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->gs2vs_offset
);
1150 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->gs_wave_id
);
1151 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
1152 &ctx
->gs_vtx_offset
[0]);
1153 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
1154 &ctx
->gs_vtx_offset
[1]);
1155 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
1156 &ctx
->abi
.gs_prim_id
);
1157 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
1158 &ctx
->gs_vtx_offset
[2]);
1159 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
1160 &ctx
->gs_vtx_offset
[3]);
1161 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
1162 &ctx
->gs_vtx_offset
[4]);
1163 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
1164 &ctx
->gs_vtx_offset
[5]);
1165 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
,
1166 &ctx
->abi
.gs_invocation_id
);
1169 case MESA_SHADER_FRAGMENT
:
1170 declare_global_input_sgprs(ctx
, &user_sgpr_info
, &args
,
1173 add_arg(&args
, ARG_SGPR
, ctx
->ac
.i32
, &ctx
->abi
.prim_mask
);
1174 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v2i32
, &ctx
->persp_sample
);
1175 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v2i32
, &ctx
->persp_center
);
1176 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v2i32
, &ctx
->persp_centroid
);
1177 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v3i32
, NULL
); /* persp pull model */
1178 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v2i32
, &ctx
->linear_sample
);
1179 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v2i32
, &ctx
->linear_center
);
1180 add_arg(&args
, ARG_VGPR
, ctx
->ac
.v2i32
, &ctx
->linear_centroid
);
1181 add_arg(&args
, ARG_VGPR
, ctx
->ac
.f32
, NULL
); /* line stipple tex */
1182 add_arg(&args
, ARG_VGPR
, ctx
->ac
.f32
, &ctx
->abi
.frag_pos
[0]);
1183 add_arg(&args
, ARG_VGPR
, ctx
->ac
.f32
, &ctx
->abi
.frag_pos
[1]);
1184 add_arg(&args
, ARG_VGPR
, ctx
->ac
.f32
, &ctx
->abi
.frag_pos
[2]);
1185 add_arg(&args
, ARG_VGPR
, ctx
->ac
.f32
, &ctx
->abi
.frag_pos
[3]);
1186 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.front_face
);
1187 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.ancillary
);
1188 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
, &ctx
->abi
.sample_coverage
);
1189 add_arg(&args
, ARG_VGPR
, ctx
->ac
.i32
, NULL
); /* fixed pt */
1192 unreachable("Shader stage not implemented");
1195 ctx
->main_function
= create_llvm_function(
1196 ctx
->context
, ctx
->ac
.module
, ctx
->ac
.builder
, NULL
, 0, &args
,
1197 ctx
->max_workgroup_size
, ctx
->options
);
1198 set_llvm_calling_convention(ctx
->main_function
, stage
);
1201 ctx
->shader_info
->num_input_vgprs
= 0;
1202 ctx
->shader_info
->num_input_sgprs
= ctx
->options
->supports_spill
? 2 : 0;
1204 ctx
->shader_info
->num_input_sgprs
+= args
.num_sgprs_used
;
1206 if (ctx
->stage
!= MESA_SHADER_FRAGMENT
)
1207 ctx
->shader_info
->num_input_vgprs
= args
.num_vgprs_used
;
1209 assign_arguments(ctx
->main_function
, &args
);
1213 if (ctx
->options
->supports_spill
|| user_sgpr_info
.need_ring_offsets
) {
1214 set_loc_shader_ptr(ctx
, AC_UD_SCRATCH_RING_OFFSETS
,
1216 if (ctx
->options
->supports_spill
) {
1217 ctx
->ring_offsets
= ac_build_intrinsic(&ctx
->ac
, "llvm.amdgcn.implicit.buffer.ptr",
1218 LLVMPointerType(ctx
->ac
.i8
, AC_ADDR_SPACE_CONST
),
1219 NULL
, 0, AC_FUNC_ATTR_READNONE
);
1220 ctx
->ring_offsets
= LLVMBuildBitCast(ctx
->ac
.builder
, ctx
->ring_offsets
,
1221 ac_array_in_const_addr_space(ctx
->ac
.v4i32
), "");
1225 /* For merged shaders the user SGPRs start at 8, with 8 system SGPRs in front (including
1226 * the rw_buffers at s0/s1. With user SGPR0 = s8, lets restart the count from 0 */
1227 if (has_previous_stage
)
1230 set_global_input_locs(ctx
, &user_sgpr_info
, desc_sets
, &user_sgpr_idx
);
1233 case MESA_SHADER_COMPUTE
:
1234 if (ctx
->shader_info
->info
.cs
.uses_grid_size
) {
1235 set_loc_shader(ctx
, AC_UD_CS_GRID_SIZE
,
1239 case MESA_SHADER_VERTEX
:
1240 set_vs_specific_input_locs(ctx
, stage
, has_previous_stage
,
1241 previous_stage
, &user_sgpr_idx
);
1242 if (ctx
->abi
.view_index
)
1243 set_loc_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
1245 case MESA_SHADER_TESS_CTRL
:
1246 set_vs_specific_input_locs(ctx
, stage
, has_previous_stage
,
1247 previous_stage
, &user_sgpr_idx
);
1248 if (ctx
->abi
.view_index
)
1249 set_loc_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
1251 case MESA_SHADER_TESS_EVAL
:
1252 if (ctx
->abi
.view_index
)
1253 set_loc_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
1255 case MESA_SHADER_GEOMETRY
:
1256 if (has_previous_stage
) {
1257 if (previous_stage
== MESA_SHADER_VERTEX
)
1258 set_vs_specific_input_locs(ctx
, stage
,
1263 if (ctx
->abi
.view_index
)
1264 set_loc_shader(ctx
, AC_UD_VIEW_INDEX
, &user_sgpr_idx
, 1);
1266 case MESA_SHADER_FRAGMENT
:
1269 unreachable("Shader stage not implemented");
1272 if (stage
== MESA_SHADER_TESS_CTRL
||
1273 (stage
== MESA_SHADER_VERTEX
&& ctx
->options
->key
.vs
.as_ls
) ||
1274 /* GFX9 has the ESGS ring buffer in LDS. */
1275 (stage
== MESA_SHADER_GEOMETRY
&& has_previous_stage
)) {
1276 ac_declare_lds_as_pointer(&ctx
->ac
);
1279 ctx
->shader_info
->num_user_sgprs
= user_sgpr_idx
;
1284 radv_load_resource(struct ac_shader_abi
*abi
, LLVMValueRef index
,
1285 unsigned desc_set
, unsigned binding
)
1287 struct radv_shader_context
*ctx
= radv_shader_context_from_abi(abi
);
1288 LLVMValueRef desc_ptr
= ctx
->descriptor_sets
[desc_set
];
1289 struct radv_pipeline_layout
*pipeline_layout
= ctx
->options
->layout
;
1290 struct radv_descriptor_set_layout
*layout
= pipeline_layout
->set
[desc_set
].layout
;
1291 unsigned base_offset
= layout
->binding
[binding
].offset
;
1292 LLVMValueRef offset
, stride
;
1294 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
||
1295 layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC
) {
1296 unsigned idx
= pipeline_layout
->set
[desc_set
].dynamic_offset_start
+
1297 layout
->binding
[binding
].dynamic_offset_offset
;
1298 desc_ptr
= ctx
->abi
.push_constants
;
1299 base_offset
= pipeline_layout
->push_constant_size
+ 16 * idx
;
1300 stride
= LLVMConstInt(ctx
->ac
.i32
, 16, false);
1302 stride
= LLVMConstInt(ctx
->ac
.i32
, layout
->binding
[binding
].size
, false);
1304 offset
= LLVMConstInt(ctx
->ac
.i32
, base_offset
, false);
1306 if (layout
->binding
[binding
].type
!= VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
) {
1307 offset
= ac_build_imad(&ctx
->ac
, index
, stride
, offset
);
1310 desc_ptr
= LLVMBuildGEP(ctx
->ac
.builder
, desc_ptr
, &offset
, 1, "");
1311 desc_ptr
= ac_cast_ptr(&ctx
->ac
, desc_ptr
, ctx
->ac
.v4i32
);
1312 LLVMSetMetadata(desc_ptr
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
1314 if (layout
->binding
[binding
].type
== VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT
) {
1315 uint32_t desc_type
= S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X
) |
1316 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y
) |
1317 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z
) |
1318 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W
) |
1319 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT
) |
1320 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32
);
1322 LLVMValueRef desc_components
[4] = {
1323 LLVMBuildPtrToInt(ctx
->ac
.builder
, desc_ptr
, ctx
->ac
.intptr
, ""),
1324 LLVMConstInt(ctx
->ac
.i32
, S_008F04_BASE_ADDRESS_HI(ctx
->options
->address32_hi
), false),
1325 /* High limit to support variable sizes. */
1326 LLVMConstInt(ctx
->ac
.i32
, 0xffffffff, false),
1327 LLVMConstInt(ctx
->ac
.i32
, desc_type
, false),
1330 return ac_build_gather_values(&ctx
->ac
, desc_components
, 4);
1337 /* The offchip buffer layout for TCS->TES is
1339 * - attribute 0 of patch 0 vertex 0
1340 * - attribute 0 of patch 0 vertex 1
1341 * - attribute 0 of patch 0 vertex 2
1343 * - attribute 0 of patch 1 vertex 0
1344 * - attribute 0 of patch 1 vertex 1
1346 * - attribute 1 of patch 0 vertex 0
1347 * - attribute 1 of patch 0 vertex 1
1349 * - per patch attribute 0 of patch 0
1350 * - per patch attribute 0 of patch 1
1353 * Note that every attribute has 4 components.
1355 static LLVMValueRef
get_non_vertex_index_offset(struct radv_shader_context
*ctx
)
1357 uint32_t num_patches
= ctx
->tcs_num_patches
;
1358 uint32_t num_tcs_outputs
;
1359 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
)
1360 num_tcs_outputs
= util_last_bit64(ctx
->shader_info
->info
.tcs
.outputs_written
);
1362 num_tcs_outputs
= ctx
->options
->key
.tes
.tcs_num_outputs
;
1364 uint32_t output_vertex_size
= num_tcs_outputs
* 16;
1365 uint32_t pervertex_output_patch_size
= ctx
->tcs_vertices_per_patch
* output_vertex_size
;
1367 return LLVMConstInt(ctx
->ac
.i32
, pervertex_output_patch_size
* num_patches
, false);
1370 static LLVMValueRef
calc_param_stride(struct radv_shader_context
*ctx
,
1371 LLVMValueRef vertex_index
)
1373 LLVMValueRef param_stride
;
1375 param_stride
= LLVMConstInt(ctx
->ac
.i32
, ctx
->tcs_vertices_per_patch
* ctx
->tcs_num_patches
, false);
1377 param_stride
= LLVMConstInt(ctx
->ac
.i32
, ctx
->tcs_num_patches
, false);
1378 return param_stride
;
1381 static LLVMValueRef
get_tcs_tes_buffer_address(struct radv_shader_context
*ctx
,
1382 LLVMValueRef vertex_index
,
1383 LLVMValueRef param_index
)
1385 LLVMValueRef base_addr
;
1386 LLVMValueRef param_stride
, constant16
;
1387 LLVMValueRef rel_patch_id
= get_rel_patch_id(ctx
);
1388 LLVMValueRef vertices_per_patch
= LLVMConstInt(ctx
->ac
.i32
, ctx
->tcs_vertices_per_patch
, false);
1389 constant16
= LLVMConstInt(ctx
->ac
.i32
, 16, false);
1390 param_stride
= calc_param_stride(ctx
, vertex_index
);
1392 base_addr
= ac_build_imad(&ctx
->ac
, rel_patch_id
,
1393 vertices_per_patch
, vertex_index
);
1395 base_addr
= rel_patch_id
;
1398 base_addr
= LLVMBuildAdd(ctx
->ac
.builder
, base_addr
,
1399 LLVMBuildMul(ctx
->ac
.builder
, param_index
,
1400 param_stride
, ""), "");
1402 base_addr
= LLVMBuildMul(ctx
->ac
.builder
, base_addr
, constant16
, "");
1404 if (!vertex_index
) {
1405 LLVMValueRef patch_data_offset
= get_non_vertex_index_offset(ctx
);
1407 base_addr
= LLVMBuildAdd(ctx
->ac
.builder
, base_addr
,
1408 patch_data_offset
, "");
1413 static LLVMValueRef
get_tcs_tes_buffer_address_params(struct radv_shader_context
*ctx
,
1415 unsigned const_index
,
1417 LLVMValueRef vertex_index
,
1418 LLVMValueRef indir_index
)
1420 LLVMValueRef param_index
;
1423 param_index
= LLVMBuildAdd(ctx
->ac
.builder
, LLVMConstInt(ctx
->ac
.i32
, param
, false),
1426 if (const_index
&& !is_compact
)
1427 param
+= const_index
;
1428 param_index
= LLVMConstInt(ctx
->ac
.i32
, param
, false);
1430 return get_tcs_tes_buffer_address(ctx
, vertex_index
, param_index
);
1434 get_dw_address(struct radv_shader_context
*ctx
,
1435 LLVMValueRef dw_addr
,
1437 unsigned const_index
,
1438 bool compact_const_index
,
1439 LLVMValueRef vertex_index
,
1440 LLVMValueRef stride
,
1441 LLVMValueRef indir_index
)
1446 dw_addr
= LLVMBuildAdd(ctx
->ac
.builder
, dw_addr
,
1447 LLVMBuildMul(ctx
->ac
.builder
,
1453 dw_addr
= LLVMBuildAdd(ctx
->ac
.builder
, dw_addr
,
1454 LLVMBuildMul(ctx
->ac
.builder
, indir_index
,
1455 LLVMConstInt(ctx
->ac
.i32
, 4, false), ""), "");
1456 else if (const_index
&& !compact_const_index
)
1457 dw_addr
= LLVMBuildAdd(ctx
->ac
.builder
, dw_addr
,
1458 LLVMConstInt(ctx
->ac
.i32
, const_index
* 4, false), "");
1460 dw_addr
= LLVMBuildAdd(ctx
->ac
.builder
, dw_addr
,
1461 LLVMConstInt(ctx
->ac
.i32
, param
* 4, false), "");
1463 if (const_index
&& compact_const_index
)
1464 dw_addr
= LLVMBuildAdd(ctx
->ac
.builder
, dw_addr
,
1465 LLVMConstInt(ctx
->ac
.i32
, const_index
, false), "");
1470 load_tcs_varyings(struct ac_shader_abi
*abi
,
1472 LLVMValueRef vertex_index
,
1473 LLVMValueRef indir_index
,
1474 unsigned const_index
,
1476 unsigned driver_location
,
1478 unsigned num_components
,
1483 struct radv_shader_context
*ctx
= radv_shader_context_from_abi(abi
);
1484 LLVMValueRef dw_addr
, stride
;
1485 LLVMValueRef value
[4], result
;
1486 unsigned param
= shader_io_get_unique_index(location
);
1489 uint32_t input_vertex_size
= (ctx
->tcs_num_inputs
* 16) / 4;
1490 stride
= LLVMConstInt(ctx
->ac
.i32
, input_vertex_size
, false);
1491 dw_addr
= get_tcs_in_current_patch_offset(ctx
);
1494 stride
= get_tcs_out_vertex_stride(ctx
);
1495 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
1497 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
1502 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
1505 for (unsigned i
= 0; i
< num_components
+ component
; i
++) {
1506 value
[i
] = ac_lds_load(&ctx
->ac
, dw_addr
);
1507 dw_addr
= LLVMBuildAdd(ctx
->ac
.builder
, dw_addr
,
1510 result
= ac_build_varying_gather_values(&ctx
->ac
, value
, num_components
, component
);
1515 store_tcs_output(struct ac_shader_abi
*abi
,
1516 const nir_variable
*var
,
1517 LLVMValueRef vertex_index
,
1518 LLVMValueRef param_index
,
1519 unsigned const_index
,
1523 struct radv_shader_context
*ctx
= radv_shader_context_from_abi(abi
);
1524 const unsigned location
= var
->data
.location
;
1525 unsigned component
= var
->data
.location_frac
;
1526 const bool is_patch
= var
->data
.patch
;
1527 const bool is_compact
= var
->data
.compact
;
1528 LLVMValueRef dw_addr
;
1529 LLVMValueRef stride
= NULL
;
1530 LLVMValueRef buf_addr
= NULL
;
1532 bool store_lds
= true;
1535 if (!(ctx
->tcs_patch_outputs_read
& (1U << (location
- VARYING_SLOT_PATCH0
))))
1538 if (!(ctx
->tcs_outputs_read
& (1ULL << location
)))
1542 param
= shader_io_get_unique_index(location
);
1543 if ((location
== VARYING_SLOT_CLIP_DIST0
|| location
== VARYING_SLOT_CLIP_DIST1
) && is_compact
) {
1544 const_index
+= component
;
1547 if (const_index
>= 4) {
1554 stride
= get_tcs_out_vertex_stride(ctx
);
1555 dw_addr
= get_tcs_out_current_patch_offset(ctx
);
1557 dw_addr
= get_tcs_out_current_patch_data_offset(ctx
);
1560 dw_addr
= get_dw_address(ctx
, dw_addr
, param
, const_index
, is_compact
, vertex_index
, stride
,
1562 buf_addr
= get_tcs_tes_buffer_address_params(ctx
, param
, const_index
, is_compact
,
1563 vertex_index
, param_index
);
1565 bool is_tess_factor
= false;
1566 if (location
== VARYING_SLOT_TESS_LEVEL_INNER
||
1567 location
== VARYING_SLOT_TESS_LEVEL_OUTER
)
1568 is_tess_factor
= true;
1570 unsigned base
= is_compact
? const_index
: 0;
1571 for (unsigned chan
= 0; chan
< 8; chan
++) {
1572 if (!(writemask
& (1 << chan
)))
1574 LLVMValueRef value
= ac_llvm_extract_elem(&ctx
->ac
, src
, chan
- component
);
1575 value
= ac_to_integer(&ctx
->ac
, value
);
1576 value
= LLVMBuildZExtOrBitCast(ctx
->ac
.builder
, value
, ctx
->ac
.i32
, "");
1578 if (store_lds
|| is_tess_factor
) {
1579 LLVMValueRef dw_addr_chan
=
1580 LLVMBuildAdd(ctx
->ac
.builder
, dw_addr
,
1581 LLVMConstInt(ctx
->ac
.i32
, chan
, false), "");
1582 ac_lds_store(&ctx
->ac
, dw_addr_chan
, value
);
1585 if (!is_tess_factor
&& writemask
!= 0xF)
1586 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, value
, 1,
1587 buf_addr
, ctx
->oc_lds
,
1588 4 * (base
+ chan
), ac_glc
, false);
1591 if (writemask
== 0xF) {
1592 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, src
, 4,
1593 buf_addr
, ctx
->oc_lds
,
1594 (base
* 4), ac_glc
, false);
1599 load_tes_input(struct ac_shader_abi
*abi
,
1601 LLVMValueRef vertex_index
,
1602 LLVMValueRef param_index
,
1603 unsigned const_index
,
1605 unsigned driver_location
,
1607 unsigned num_components
,
1612 struct radv_shader_context
*ctx
= radv_shader_context_from_abi(abi
);
1613 LLVMValueRef buf_addr
;
1614 LLVMValueRef result
;
1615 unsigned param
= shader_io_get_unique_index(location
);
1617 if ((location
== VARYING_SLOT_CLIP_DIST0
|| location
== VARYING_SLOT_CLIP_DIST1
) && is_compact
) {
1618 const_index
+= component
;
1620 if (const_index
>= 4) {
1626 buf_addr
= get_tcs_tes_buffer_address_params(ctx
, param
, const_index
,
1627 is_compact
, vertex_index
, param_index
);
1629 LLVMValueRef comp_offset
= LLVMConstInt(ctx
->ac
.i32
, component
* 4, false);
1630 buf_addr
= LLVMBuildAdd(ctx
->ac
.builder
, buf_addr
, comp_offset
, "");
1632 result
= ac_build_buffer_load(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, num_components
, NULL
,
1633 buf_addr
, ctx
->oc_lds
, is_compact
? (4 * const_index
) : 0, ac_glc
, true, false);
1634 result
= ac_trim_vector(&ctx
->ac
, result
, num_components
);
1639 load_gs_input(struct ac_shader_abi
*abi
,
1641 unsigned driver_location
,
1643 unsigned num_components
,
1644 unsigned vertex_index
,
1645 unsigned const_index
,
1648 struct radv_shader_context
*ctx
= radv_shader_context_from_abi(abi
);
1649 LLVMValueRef vtx_offset
;
1650 unsigned param
, vtx_offset_param
;
1651 LLVMValueRef value
[4], result
;
1653 vtx_offset_param
= vertex_index
;
1654 assert(vtx_offset_param
< 6);
1655 vtx_offset
= LLVMBuildMul(ctx
->ac
.builder
, ctx
->gs_vtx_offset
[vtx_offset_param
],
1656 LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
1658 param
= shader_io_get_unique_index(location
);
1660 for (unsigned i
= component
; i
< num_components
+ component
; i
++) {
1661 if (ctx
->ac
.chip_class
>= GFX9
) {
1662 LLVMValueRef dw_addr
= ctx
->gs_vtx_offset
[vtx_offset_param
];
1663 dw_addr
= LLVMBuildAdd(ctx
->ac
.builder
, dw_addr
,
1664 LLVMConstInt(ctx
->ac
.i32
, param
* 4 + i
+ const_index
, 0), "");
1665 value
[i
] = ac_lds_load(&ctx
->ac
, dw_addr
);
1667 LLVMValueRef soffset
=
1668 LLVMConstInt(ctx
->ac
.i32
,
1669 (param
* 4 + i
+ const_index
) * 256,
1672 value
[i
] = ac_build_buffer_load(&ctx
->ac
,
1675 vtx_offset
, soffset
,
1676 0, ac_glc
, true, false);
1679 if (ac_get_type_size(type
) == 2) {
1680 value
[i
] = LLVMBuildBitCast(ctx
->ac
.builder
, value
[i
], ctx
->ac
.i32
, "");
1681 value
[i
] = LLVMBuildTrunc(ctx
->ac
.builder
, value
[i
], ctx
->ac
.i16
, "");
1683 value
[i
] = LLVMBuildBitCast(ctx
->ac
.builder
, value
[i
], type
, "");
1685 result
= ac_build_varying_gather_values(&ctx
->ac
, value
, num_components
, component
);
1686 result
= ac_to_integer(&ctx
->ac
, result
);
1691 static void radv_emit_kill(struct ac_shader_abi
*abi
, LLVMValueRef visible
)
1693 struct radv_shader_context
*ctx
= radv_shader_context_from_abi(abi
);
1694 ac_build_kill_if_false(&ctx
->ac
, visible
);
1697 static LLVMValueRef
lookup_interp_param(struct ac_shader_abi
*abi
,
1698 enum glsl_interp_mode interp
, unsigned location
)
1700 struct radv_shader_context
*ctx
= radv_shader_context_from_abi(abi
);
1703 case INTERP_MODE_FLAT
:
1706 case INTERP_MODE_SMOOTH
:
1707 case INTERP_MODE_NONE
:
1708 if (location
== INTERP_CENTER
)
1709 return ctx
->persp_center
;
1710 else if (location
== INTERP_CENTROID
)
1711 return ctx
->persp_centroid
;
1712 else if (location
== INTERP_SAMPLE
)
1713 return ctx
->persp_sample
;
1715 case INTERP_MODE_NOPERSPECTIVE
:
1716 if (location
== INTERP_CENTER
)
1717 return ctx
->linear_center
;
1718 else if (location
== INTERP_CENTROID
)
1719 return ctx
->linear_centroid
;
1720 else if (location
== INTERP_SAMPLE
)
1721 return ctx
->linear_sample
;
1728 radv_get_sample_pos_offset(uint32_t num_samples
)
1730 uint32_t sample_pos_offset
= 0;
1732 switch (num_samples
) {
1734 sample_pos_offset
= 1;
1737 sample_pos_offset
= 3;
1740 sample_pos_offset
= 7;
1745 return sample_pos_offset
;
1748 static LLVMValueRef
load_sample_position(struct ac_shader_abi
*abi
,
1749 LLVMValueRef sample_id
)
1751 struct radv_shader_context
*ctx
= radv_shader_context_from_abi(abi
);
1753 LLVMValueRef result
;
1754 LLVMValueRef index
= LLVMConstInt(ctx
->ac
.i32
, RING_PS_SAMPLE_POSITIONS
, false);
1755 LLVMValueRef ptr
= LLVMBuildGEP(ctx
->ac
.builder
, ctx
->ring_offsets
, &index
, 1, "");
1757 ptr
= LLVMBuildBitCast(ctx
->ac
.builder
, ptr
,
1758 ac_array_in_const_addr_space(ctx
->ac
.v2f32
), "");
1760 uint32_t sample_pos_offset
=
1761 radv_get_sample_pos_offset(ctx
->options
->key
.fs
.num_samples
);
1764 LLVMBuildAdd(ctx
->ac
.builder
, sample_id
,
1765 LLVMConstInt(ctx
->ac
.i32
, sample_pos_offset
, false), "");
1766 result
= ac_build_load_invariant(&ctx
->ac
, ptr
, sample_id
);
1772 static LLVMValueRef
load_sample_mask_in(struct ac_shader_abi
*abi
)
1774 struct radv_shader_context
*ctx
= radv_shader_context_from_abi(abi
);
1775 uint8_t log2_ps_iter_samples
;
1777 if (ctx
->shader_info
->info
.ps
.force_persample
) {
1778 log2_ps_iter_samples
=
1779 util_logbase2(ctx
->options
->key
.fs
.num_samples
);
1781 log2_ps_iter_samples
= ctx
->options
->key
.fs
.log2_ps_iter_samples
;
1784 /* The bit pattern matches that used by fixed function fragment
1786 static const uint16_t ps_iter_masks
[] = {
1787 0xffff, /* not used */
1793 assert(log2_ps_iter_samples
< ARRAY_SIZE(ps_iter_masks
));
1795 uint32_t ps_iter_mask
= ps_iter_masks
[log2_ps_iter_samples
];
1797 LLVMValueRef result
, sample_id
;
1798 sample_id
= ac_unpack_param(&ctx
->ac
, abi
->ancillary
, 8, 4);
1799 sample_id
= LLVMBuildShl(ctx
->ac
.builder
, LLVMConstInt(ctx
->ac
.i32
, ps_iter_mask
, false), sample_id
, "");
1800 result
= LLVMBuildAnd(ctx
->ac
.builder
, sample_id
, abi
->sample_coverage
, "");
1806 visit_emit_vertex(struct ac_shader_abi
*abi
, unsigned stream
, LLVMValueRef
*addrs
)
1808 LLVMValueRef gs_next_vertex
;
1809 LLVMValueRef can_emit
;
1810 unsigned offset
= 0;
1811 struct radv_shader_context
*ctx
= radv_shader_context_from_abi(abi
);
1813 /* Write vertex attribute values to GSVS ring */
1814 gs_next_vertex
= LLVMBuildLoad(ctx
->ac
.builder
,
1815 ctx
->gs_next_vertex
[stream
],
1818 /* If this thread has already emitted the declared maximum number of
1819 * vertices, kill it: excessive vertex emissions are not supposed to
1820 * have any effect, and GS threads have no externally observable
1821 * effects other than emitting vertices.
1823 can_emit
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntULT
, gs_next_vertex
,
1824 LLVMConstInt(ctx
->ac
.i32
, ctx
->gs_max_out_vertices
, false), "");
1825 ac_build_kill_if_false(&ctx
->ac
, can_emit
);
1827 for (unsigned i
= 0; i
< AC_LLVM_MAX_OUTPUTS
; ++i
) {
1828 unsigned output_usage_mask
=
1829 ctx
->shader_info
->info
.gs
.output_usage_mask
[i
];
1830 uint8_t output_stream
=
1831 ctx
->shader_info
->info
.gs
.output_streams
[i
];
1832 LLVMValueRef
*out_ptr
= &addrs
[i
* 4];
1833 int length
= util_last_bit(output_usage_mask
);
1835 if (!(ctx
->output_mask
& (1ull << i
)) ||
1836 output_stream
!= stream
)
1839 for (unsigned j
= 0; j
< length
; j
++) {
1840 if (!(output_usage_mask
& (1 << j
)))
1843 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->ac
.builder
,
1845 LLVMValueRef voffset
=
1846 LLVMConstInt(ctx
->ac
.i32
, offset
*
1847 ctx
->gs_max_out_vertices
, false);
1851 voffset
= LLVMBuildAdd(ctx
->ac
.builder
, voffset
, gs_next_vertex
, "");
1852 voffset
= LLVMBuildMul(ctx
->ac
.builder
, voffset
, LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
1854 out_val
= ac_to_integer(&ctx
->ac
, out_val
);
1855 out_val
= LLVMBuildZExtOrBitCast(ctx
->ac
.builder
, out_val
, ctx
->ac
.i32
, "");
1857 ac_build_buffer_store_dword(&ctx
->ac
,
1858 ctx
->gsvs_ring
[stream
],
1860 voffset
, ctx
->gs2vs_offset
, 0,
1861 ac_glc
| ac_slc
, true);
1865 gs_next_vertex
= LLVMBuildAdd(ctx
->ac
.builder
, gs_next_vertex
,
1867 LLVMBuildStore(ctx
->ac
.builder
, gs_next_vertex
, ctx
->gs_next_vertex
[stream
]);
1869 ac_build_sendmsg(&ctx
->ac
,
1870 AC_SENDMSG_GS_OP_EMIT
| AC_SENDMSG_GS
| (stream
<< 8),
1875 visit_end_primitive(struct ac_shader_abi
*abi
, unsigned stream
)
1877 struct radv_shader_context
*ctx
= radv_shader_context_from_abi(abi
);
1878 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_CUT
| AC_SENDMSG_GS
| (stream
<< 8), ctx
->gs_wave_id
);
1882 load_tess_coord(struct ac_shader_abi
*abi
)
1884 struct radv_shader_context
*ctx
= radv_shader_context_from_abi(abi
);
1886 LLVMValueRef coord
[4] = {
1893 if (ctx
->tes_primitive_mode
== GL_TRIANGLES
)
1894 coord
[2] = LLVMBuildFSub(ctx
->ac
.builder
, ctx
->ac
.f32_1
,
1895 LLVMBuildFAdd(ctx
->ac
.builder
, coord
[0], coord
[1], ""), "");
1897 return ac_build_gather_values(&ctx
->ac
, coord
, 3);
1901 load_patch_vertices_in(struct ac_shader_abi
*abi
)
1903 struct radv_shader_context
*ctx
= radv_shader_context_from_abi(abi
);
1904 return LLVMConstInt(ctx
->ac
.i32
, ctx
->options
->key
.tcs
.input_vertices
, false);
1908 static LLVMValueRef
radv_load_base_vertex(struct ac_shader_abi
*abi
)
1910 return abi
->base_vertex
;
1913 static LLVMValueRef
radv_load_ssbo(struct ac_shader_abi
*abi
,
1914 LLVMValueRef buffer_ptr
, bool write
)
1916 struct radv_shader_context
*ctx
= radv_shader_context_from_abi(abi
);
1917 LLVMValueRef result
;
1919 LLVMSetMetadata(buffer_ptr
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
1921 result
= LLVMBuildLoad(ctx
->ac
.builder
, buffer_ptr
, "");
1922 LLVMSetMetadata(result
, ctx
->ac
.invariant_load_md_kind
, ctx
->ac
.empty_md
);
1927 static LLVMValueRef
radv_load_ubo(struct ac_shader_abi
*abi
, LLVMValueRef buffer_ptr
)
1929 struct radv_shader_context
*ctx
= radv_shader_context_from_abi(abi
);
1930 LLVMValueRef result
;
1932 if (LLVMGetTypeKind(LLVMTypeOf(buffer_ptr
)) != LLVMPointerTypeKind
) {
1933 /* Do not load the descriptor for inlined uniform blocks. */
1937 LLVMSetMetadata(buffer_ptr
, ctx
->ac
.uniform_md_kind
, ctx
->ac
.empty_md
);
1939 result
= LLVMBuildLoad(ctx
->ac
.builder
, buffer_ptr
, "");
1940 LLVMSetMetadata(result
, ctx
->ac
.invariant_load_md_kind
, ctx
->ac
.empty_md
);
1945 static LLVMValueRef
radv_get_sampler_desc(struct ac_shader_abi
*abi
,
1946 unsigned descriptor_set
,
1947 unsigned base_index
,
1948 unsigned constant_index
,
1950 enum ac_descriptor_type desc_type
,
1951 bool image
, bool write
,
1954 struct radv_shader_context
*ctx
= radv_shader_context_from_abi(abi
);
1955 LLVMValueRef list
= ctx
->descriptor_sets
[descriptor_set
];
1956 struct radv_descriptor_set_layout
*layout
= ctx
->options
->layout
->set
[descriptor_set
].layout
;
1957 struct radv_descriptor_set_binding_layout
*binding
= layout
->binding
+ base_index
;
1958 unsigned offset
= binding
->offset
;
1959 unsigned stride
= binding
->size
;
1961 LLVMBuilderRef builder
= ctx
->ac
.builder
;
1964 assert(base_index
< layout
->binding_count
);
1966 switch (desc_type
) {
1968 type
= ctx
->ac
.v8i32
;
1972 type
= ctx
->ac
.v8i32
;
1976 case AC_DESC_SAMPLER
:
1977 type
= ctx
->ac
.v4i32
;
1978 if (binding
->type
== VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
) {
1979 offset
+= radv_combined_image_descriptor_sampler_offset(binding
);
1984 case AC_DESC_BUFFER
:
1985 type
= ctx
->ac
.v4i32
;
1988 case AC_DESC_PLANE_0
:
1989 case AC_DESC_PLANE_1
:
1990 case AC_DESC_PLANE_2
:
1991 type
= ctx
->ac
.v8i32
;
1993 offset
+= 32 * (desc_type
- AC_DESC_PLANE_0
);
1996 unreachable("invalid desc_type\n");
1999 offset
+= constant_index
* stride
;
2001 if (desc_type
== AC_DESC_SAMPLER
&& binding
->immutable_samplers_offset
&&
2002 (!index
|| binding
->immutable_samplers_equal
)) {
2003 if (binding
->immutable_samplers_equal
)
2006 const uint32_t *samplers
= radv_immutable_samplers(layout
, binding
);
2008 LLVMValueRef constants
[] = {
2009 LLVMConstInt(ctx
->ac
.i32
, samplers
[constant_index
* 4 + 0], 0),
2010 LLVMConstInt(ctx
->ac
.i32
, samplers
[constant_index
* 4 + 1], 0),
2011 LLVMConstInt(ctx
->ac
.i32
, samplers
[constant_index
* 4 + 2], 0),
2012 LLVMConstInt(ctx
->ac
.i32
, samplers
[constant_index
* 4 + 3], 0),
2014 return ac_build_gather_values(&ctx
->ac
, constants
, 4);
2017 assert(stride
% type_size
== 0);
2019 LLVMValueRef adjusted_index
= index
;
2020 if (!adjusted_index
)
2021 adjusted_index
= ctx
->ac
.i32_0
;
2023 adjusted_index
= LLVMBuildMul(builder
, adjusted_index
, LLVMConstInt(ctx
->ac
.i32
, stride
/ type_size
, 0), "");
2025 LLVMValueRef val_offset
= LLVMConstInt(ctx
->ac
.i32
, offset
, 0);
2026 list
= LLVMBuildGEP(builder
, list
, &val_offset
, 1, "");
2027 list
= LLVMBuildPointerCast(builder
, list
,
2028 ac_array_in_const32_addr_space(type
), "");
2030 LLVMValueRef descriptor
= ac_build_load_to_sgpr(&ctx
->ac
, list
, adjusted_index
);
2032 /* 3 plane formats always have same size and format for plane 1 & 2, so
2033 * use the tail from plane 1 so that we can store only the first 16 bytes
2034 * of the last plane. */
2035 if (desc_type
== AC_DESC_PLANE_2
) {
2036 LLVMValueRef descriptor2
= radv_get_sampler_desc(abi
, descriptor_set
, base_index
, constant_index
, index
, AC_DESC_PLANE_1
,image
, write
, bindless
);
2038 LLVMValueRef components
[8];
2039 for (unsigned i
= 0; i
< 4; ++i
)
2040 components
[i
] = ac_llvm_extract_elem(&ctx
->ac
, descriptor
, i
);
2042 for (unsigned i
= 4; i
< 8; ++i
)
2043 components
[i
] = ac_llvm_extract_elem(&ctx
->ac
, descriptor2
, i
);
2044 descriptor
= ac_build_gather_values(&ctx
->ac
, components
, 8);
2050 /* For 2_10_10_10 formats the alpha is handled as unsigned by pre-vega HW.
2051 * so we may need to fix it up. */
2053 adjust_vertex_fetch_alpha(struct radv_shader_context
*ctx
,
2054 unsigned adjustment
,
2057 if (adjustment
== RADV_ALPHA_ADJUST_NONE
)
2060 LLVMValueRef c30
= LLVMConstInt(ctx
->ac
.i32
, 30, 0);
2062 alpha
= LLVMBuildBitCast(ctx
->ac
.builder
, alpha
, ctx
->ac
.f32
, "");
2064 if (adjustment
== RADV_ALPHA_ADJUST_SSCALED
)
2065 alpha
= LLVMBuildFPToUI(ctx
->ac
.builder
, alpha
, ctx
->ac
.i32
, "");
2067 alpha
= ac_to_integer(&ctx
->ac
, alpha
);
2069 /* For the integer-like cases, do a natural sign extension.
2071 * For the SNORM case, the values are 0.0, 0.333, 0.666, 1.0
2072 * and happen to contain 0, 1, 2, 3 as the two LSBs of the
2075 alpha
= LLVMBuildShl(ctx
->ac
.builder
, alpha
,
2076 adjustment
== RADV_ALPHA_ADJUST_SNORM
?
2077 LLVMConstInt(ctx
->ac
.i32
, 7, 0) : c30
, "");
2078 alpha
= LLVMBuildAShr(ctx
->ac
.builder
, alpha
, c30
, "");
2080 /* Convert back to the right type. */
2081 if (adjustment
== RADV_ALPHA_ADJUST_SNORM
) {
2083 LLVMValueRef neg_one
= LLVMConstReal(ctx
->ac
.f32
, -1.0);
2084 alpha
= LLVMBuildSIToFP(ctx
->ac
.builder
, alpha
, ctx
->ac
.f32
, "");
2085 clamp
= LLVMBuildFCmp(ctx
->ac
.builder
, LLVMRealULT
, alpha
, neg_one
, "");
2086 alpha
= LLVMBuildSelect(ctx
->ac
.builder
, clamp
, neg_one
, alpha
, "");
2087 } else if (adjustment
== RADV_ALPHA_ADJUST_SSCALED
) {
2088 alpha
= LLVMBuildSIToFP(ctx
->ac
.builder
, alpha
, ctx
->ac
.f32
, "");
2091 return LLVMBuildBitCast(ctx
->ac
.builder
, alpha
, ctx
->ac
.i32
, "");
2095 get_num_channels_from_data_format(unsigned data_format
)
2097 switch (data_format
) {
2098 case V_008F0C_BUF_DATA_FORMAT_8
:
2099 case V_008F0C_BUF_DATA_FORMAT_16
:
2100 case V_008F0C_BUF_DATA_FORMAT_32
:
2102 case V_008F0C_BUF_DATA_FORMAT_8_8
:
2103 case V_008F0C_BUF_DATA_FORMAT_16_16
:
2104 case V_008F0C_BUF_DATA_FORMAT_32_32
:
2106 case V_008F0C_BUF_DATA_FORMAT_10_11_11
:
2107 case V_008F0C_BUF_DATA_FORMAT_11_11_10
:
2108 case V_008F0C_BUF_DATA_FORMAT_32_32_32
:
2110 case V_008F0C_BUF_DATA_FORMAT_8_8_8_8
:
2111 case V_008F0C_BUF_DATA_FORMAT_10_10_10_2
:
2112 case V_008F0C_BUF_DATA_FORMAT_2_10_10_10
:
2113 case V_008F0C_BUF_DATA_FORMAT_16_16_16_16
:
2114 case V_008F0C_BUF_DATA_FORMAT_32_32_32_32
:
2124 radv_fixup_vertex_input_fetches(struct radv_shader_context
*ctx
,
2126 unsigned num_channels
,
2129 LLVMValueRef zero
= is_float
? ctx
->ac
.f32_0
: ctx
->ac
.i32_0
;
2130 LLVMValueRef one
= is_float
? ctx
->ac
.f32_1
: ctx
->ac
.i32_1
;
2131 LLVMValueRef chan
[4];
2133 if (LLVMGetTypeKind(LLVMTypeOf(value
)) == LLVMVectorTypeKind
) {
2134 unsigned vec_size
= LLVMGetVectorSize(LLVMTypeOf(value
));
2136 if (num_channels
== 4 && num_channels
== vec_size
)
2139 num_channels
= MIN2(num_channels
, vec_size
);
2141 for (unsigned i
= 0; i
< num_channels
; i
++)
2142 chan
[i
] = ac_llvm_extract_elem(&ctx
->ac
, value
, i
);
2145 assert(num_channels
== 1);
2150 for (unsigned i
= num_channels
; i
< 4; i
++) {
2151 chan
[i
] = i
== 3 ? one
: zero
;
2152 chan
[i
] = ac_to_integer(&ctx
->ac
, chan
[i
]);
2155 return ac_build_gather_values(&ctx
->ac
, chan
, 4);
2159 handle_vs_input_decl(struct radv_shader_context
*ctx
,
2160 struct nir_variable
*variable
)
2162 LLVMValueRef t_list_ptr
= ctx
->vertex_buffers
;
2163 LLVMValueRef t_offset
;
2164 LLVMValueRef t_list
;
2166 LLVMValueRef buffer_index
;
2167 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, true);
2168 uint8_t input_usage_mask
=
2169 ctx
->shader_info
->info
.vs
.input_usage_mask
[variable
->data
.location
];
2170 unsigned num_input_channels
= util_last_bit(input_usage_mask
);
2172 variable
->data
.driver_location
= variable
->data
.location
* 4;
2174 enum glsl_base_type type
= glsl_get_base_type(variable
->type
);
2175 for (unsigned i
= 0; i
< attrib_count
; ++i
) {
2176 LLVMValueRef output
[4];
2177 unsigned attrib_index
= variable
->data
.location
+ i
- VERT_ATTRIB_GENERIC0
;
2178 unsigned attrib_format
= ctx
->options
->key
.vs
.vertex_attribute_formats
[attrib_index
];
2179 unsigned data_format
= attrib_format
& 0x0f;
2180 unsigned num_format
= (attrib_format
>> 4) & 0x07;
2181 bool is_float
= num_format
!= V_008F0C_BUF_NUM_FORMAT_UINT
&&
2182 num_format
!= V_008F0C_BUF_NUM_FORMAT_SINT
;
2184 if (ctx
->options
->key
.vs
.instance_rate_inputs
& (1u << attrib_index
)) {
2185 uint32_t divisor
= ctx
->options
->key
.vs
.instance_rate_divisors
[attrib_index
];
2188 buffer_index
= ctx
->abi
.instance_id
;
2191 buffer_index
= LLVMBuildUDiv(ctx
->ac
.builder
, buffer_index
,
2192 LLVMConstInt(ctx
->ac
.i32
, divisor
, 0), "");
2195 buffer_index
= ctx
->ac
.i32_0
;
2198 buffer_index
= LLVMBuildAdd(ctx
->ac
.builder
, ctx
->abi
.start_instance
, buffer_index
, "");
2200 buffer_index
= LLVMBuildAdd(ctx
->ac
.builder
, ctx
->abi
.vertex_id
,
2201 ctx
->abi
.base_vertex
, "");
2203 /* Adjust the number of channels to load based on the vertex
2206 unsigned num_format_channels
= get_num_channels_from_data_format(data_format
);
2207 unsigned num_channels
= MIN2(num_input_channels
, num_format_channels
);
2208 unsigned attrib_binding
= ctx
->options
->key
.vs
.vertex_attribute_bindings
[attrib_index
];
2209 unsigned attrib_offset
= ctx
->options
->key
.vs
.vertex_attribute_offsets
[attrib_index
];
2210 unsigned attrib_stride
= ctx
->options
->key
.vs
.vertex_attribute_strides
[attrib_index
];
2212 if (ctx
->options
->key
.vs
.post_shuffle
& (1 << attrib_index
)) {
2213 /* Always load, at least, 3 channels for formats that
2214 * need to be shuffled because X<->Z.
2216 num_channels
= MAX2(num_channels
, 3);
2219 if (attrib_stride
!= 0 && attrib_offset
> attrib_stride
) {
2220 LLVMValueRef buffer_offset
=
2221 LLVMConstInt(ctx
->ac
.i32
,
2222 attrib_offset
/ attrib_stride
, false);
2224 buffer_index
= LLVMBuildAdd(ctx
->ac
.builder
,
2228 attrib_offset
= attrib_offset
% attrib_stride
;
2231 t_offset
= LLVMConstInt(ctx
->ac
.i32
, attrib_binding
, false);
2232 t_list
= ac_build_load_to_sgpr(&ctx
->ac
, t_list_ptr
, t_offset
);
2234 input
= ac_build_struct_tbuffer_load(&ctx
->ac
, t_list
,
2236 LLVMConstInt(ctx
->ac
.i32
, attrib_offset
, false),
2237 ctx
->ac
.i32_0
, ctx
->ac
.i32_0
,
2239 data_format
, num_format
, 0, true);
2241 if (ctx
->options
->key
.vs
.post_shuffle
& (1 << attrib_index
)) {
2243 c
[0] = ac_llvm_extract_elem(&ctx
->ac
, input
, 2);
2244 c
[1] = ac_llvm_extract_elem(&ctx
->ac
, input
, 1);
2245 c
[2] = ac_llvm_extract_elem(&ctx
->ac
, input
, 0);
2246 c
[3] = ac_llvm_extract_elem(&ctx
->ac
, input
, 3);
2248 input
= ac_build_gather_values(&ctx
->ac
, c
, 4);
2251 input
= radv_fixup_vertex_input_fetches(ctx
, input
, num_channels
,
2254 for (unsigned chan
= 0; chan
< 4; chan
++) {
2255 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
2256 output
[chan
] = LLVMBuildExtractElement(ctx
->ac
.builder
, input
, llvm_chan
, "");
2257 if (type
== GLSL_TYPE_FLOAT16
) {
2258 output
[chan
] = LLVMBuildBitCast(ctx
->ac
.builder
, output
[chan
], ctx
->ac
.f32
, "");
2259 output
[chan
] = LLVMBuildFPTrunc(ctx
->ac
.builder
, output
[chan
], ctx
->ac
.f16
, "");
2263 unsigned alpha_adjust
= (ctx
->options
->key
.vs
.alpha_adjust
>> (attrib_index
* 2)) & 3;
2264 output
[3] = adjust_vertex_fetch_alpha(ctx
, alpha_adjust
, output
[3]);
2266 for (unsigned chan
= 0; chan
< 4; chan
++) {
2267 output
[chan
] = ac_to_integer(&ctx
->ac
, output
[chan
]);
2268 if (type
== GLSL_TYPE_UINT16
|| type
== GLSL_TYPE_INT16
)
2269 output
[chan
] = LLVMBuildTrunc(ctx
->ac
.builder
, output
[chan
], ctx
->ac
.i16
, "");
2271 ctx
->inputs
[ac_llvm_reg_index_soa(variable
->data
.location
+ i
, chan
)] = output
[chan
];
2276 static void interp_fs_input(struct radv_shader_context
*ctx
,
2278 LLVMValueRef interp_param
,
2279 LLVMValueRef prim_mask
,
2281 LLVMValueRef result
[4])
2283 LLVMValueRef attr_number
;
2286 bool interp
= !LLVMIsUndef(interp_param
);
2288 attr_number
= LLVMConstInt(ctx
->ac
.i32
, attr
, false);
2290 /* fs.constant returns the param from the middle vertex, so it's not
2291 * really useful for flat shading. It's meant to be used for custom
2292 * interpolation (but the intrinsic can't fetch from the other two
2295 * Luckily, it doesn't matter, because we rely on the FLAT_SHADE state
2296 * to do the right thing. The only reason we use fs.constant is that
2297 * fs.interp cannot be used on integers, because they can be equal
2301 interp_param
= LLVMBuildBitCast(ctx
->ac
.builder
, interp_param
,
2304 i
= LLVMBuildExtractElement(ctx
->ac
.builder
, interp_param
,
2306 j
= LLVMBuildExtractElement(ctx
->ac
.builder
, interp_param
,
2310 for (chan
= 0; chan
< 4; chan
++) {
2311 LLVMValueRef llvm_chan
= LLVMConstInt(ctx
->ac
.i32
, chan
, false);
2313 if (interp
&& float16
) {
2314 result
[chan
] = ac_build_fs_interp_f16(&ctx
->ac
,
2318 } else if (interp
) {
2319 result
[chan
] = ac_build_fs_interp(&ctx
->ac
,
2324 result
[chan
] = ac_build_fs_interp_mov(&ctx
->ac
,
2325 LLVMConstInt(ctx
->ac
.i32
, 2, false),
2329 result
[chan
] = LLVMBuildBitCast(ctx
->ac
.builder
, result
[chan
], ctx
->ac
.i32
, "");
2330 result
[chan
] = LLVMBuildTruncOrBitCast(ctx
->ac
.builder
, result
[chan
], float16
? ctx
->ac
.i16
: ctx
->ac
.i32
, "");
2335 static void mark_16bit_fs_input(struct radv_shader_context
*ctx
,
2336 const struct glsl_type
*type
,
2339 if (glsl_type_is_scalar(type
) || glsl_type_is_vector(type
) || glsl_type_is_matrix(type
)) {
2340 unsigned attrib_count
= glsl_count_attribute_slots(type
, false);
2341 if (glsl_type_is_16bit(type
)) {
2342 ctx
->float16_shaded_mask
|= ((1ull << attrib_count
) - 1) << location
;
2344 } else if (glsl_type_is_array(type
)) {
2345 unsigned stride
= glsl_count_attribute_slots(glsl_get_array_element(type
), false);
2346 for (unsigned i
= 0; i
< glsl_get_length(type
); ++i
) {
2347 mark_16bit_fs_input(ctx
, glsl_get_array_element(type
), location
+ i
* stride
);
2350 assert(glsl_type_is_struct_or_ifc(type
));
2351 for (unsigned i
= 0; i
< glsl_get_length(type
); i
++) {
2352 mark_16bit_fs_input(ctx
, glsl_get_struct_field(type
, i
), location
);
2353 location
+= glsl_count_attribute_slots(glsl_get_struct_field(type
, i
), false);
2359 handle_fs_input_decl(struct radv_shader_context
*ctx
,
2360 struct nir_variable
*variable
)
2362 int idx
= variable
->data
.location
;
2363 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
2364 LLVMValueRef interp
= NULL
;
2367 variable
->data
.driver_location
= idx
* 4;
2370 if (variable
->data
.compact
) {
2371 unsigned component_count
= variable
->data
.location_frac
+
2372 glsl_get_length(variable
->type
);
2373 attrib_count
= (component_count
+ 3) / 4;
2375 mark_16bit_fs_input(ctx
, variable
->type
, idx
);
2377 mask
= ((1ull << attrib_count
) - 1) << variable
->data
.location
;
2379 if (glsl_get_base_type(glsl_without_array(variable
->type
)) == GLSL_TYPE_FLOAT
||
2380 glsl_get_base_type(glsl_without_array(variable
->type
)) == GLSL_TYPE_FLOAT16
||
2381 glsl_get_base_type(glsl_without_array(variable
->type
)) == GLSL_TYPE_STRUCT
) {
2382 unsigned interp_type
;
2383 if (variable
->data
.sample
)
2384 interp_type
= INTERP_SAMPLE
;
2385 else if (variable
->data
.centroid
)
2386 interp_type
= INTERP_CENTROID
;
2388 interp_type
= INTERP_CENTER
;
2390 interp
= lookup_interp_param(&ctx
->abi
, variable
->data
.interpolation
, interp_type
);
2393 interp
= LLVMGetUndef(ctx
->ac
.i32
);
2395 for (unsigned i
= 0; i
< attrib_count
; ++i
)
2396 ctx
->inputs
[ac_llvm_reg_index_soa(idx
+ i
, 0)] = interp
;
2398 ctx
->input_mask
|= mask
;
2402 handle_vs_inputs(struct radv_shader_context
*ctx
,
2403 struct nir_shader
*nir
) {
2404 nir_foreach_variable(variable
, &nir
->inputs
)
2405 handle_vs_input_decl(ctx
, variable
);
2409 prepare_interp_optimize(struct radv_shader_context
*ctx
,
2410 struct nir_shader
*nir
)
2412 bool uses_center
= false;
2413 bool uses_centroid
= false;
2414 nir_foreach_variable(variable
, &nir
->inputs
) {
2415 if (glsl_get_base_type(glsl_without_array(variable
->type
)) != GLSL_TYPE_FLOAT
||
2416 variable
->data
.sample
)
2419 if (variable
->data
.centroid
)
2420 uses_centroid
= true;
2425 if (uses_center
&& uses_centroid
) {
2426 LLVMValueRef sel
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntSLT
, ctx
->abi
.prim_mask
, ctx
->ac
.i32_0
, "");
2427 ctx
->persp_centroid
= LLVMBuildSelect(ctx
->ac
.builder
, sel
, ctx
->persp_center
, ctx
->persp_centroid
, "");
2428 ctx
->linear_centroid
= LLVMBuildSelect(ctx
->ac
.builder
, sel
, ctx
->linear_center
, ctx
->linear_centroid
, "");
2433 handle_fs_inputs(struct radv_shader_context
*ctx
,
2434 struct nir_shader
*nir
)
2436 prepare_interp_optimize(ctx
, nir
);
2438 nir_foreach_variable(variable
, &nir
->inputs
)
2439 handle_fs_input_decl(ctx
, variable
);
2443 if (ctx
->shader_info
->info
.ps
.uses_input_attachments
||
2444 ctx
->shader_info
->info
.needs_multiview_view_index
) {
2445 ctx
->input_mask
|= 1ull << VARYING_SLOT_LAYER
;
2446 ctx
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)] = LLVMGetUndef(ctx
->ac
.i32
);
2449 for (unsigned i
= 0; i
< RADEON_LLVM_MAX_INPUTS
; ++i
) {
2450 LLVMValueRef interp_param
;
2451 LLVMValueRef
*inputs
= ctx
->inputs
+ac_llvm_reg_index_soa(i
, 0);
2453 if (!(ctx
->input_mask
& (1ull << i
)))
2456 if (i
>= VARYING_SLOT_VAR0
|| i
== VARYING_SLOT_PNTC
||
2457 i
== VARYING_SLOT_PRIMITIVE_ID
|| i
== VARYING_SLOT_LAYER
) {
2458 interp_param
= *inputs
;
2459 bool float16
= (ctx
->float16_shaded_mask
>> i
) & 1;
2460 interp_fs_input(ctx
, index
, interp_param
, ctx
->abi
.prim_mask
, float16
,
2463 if (LLVMIsUndef(interp_param
))
2464 ctx
->shader_info
->fs
.flat_shaded_mask
|= 1u << index
;
2466 ctx
->shader_info
->fs
.float16_shaded_mask
|= 1u << index
;
2467 if (i
>= VARYING_SLOT_VAR0
)
2468 ctx
->abi
.fs_input_attr_indices
[i
- VARYING_SLOT_VAR0
] = index
;
2470 } else if (i
== VARYING_SLOT_CLIP_DIST0
) {
2471 int length
= ctx
->shader_info
->info
.ps
.num_input_clips_culls
;
2473 for (unsigned j
= 0; j
< length
; j
+= 4) {
2474 inputs
= ctx
->inputs
+ ac_llvm_reg_index_soa(i
, j
);
2476 interp_param
= *inputs
;
2477 interp_fs_input(ctx
, index
, interp_param
,
2478 ctx
->abi
.prim_mask
, false, inputs
);
2481 } else if (i
== VARYING_SLOT_POS
) {
2482 for(int i
= 0; i
< 3; ++i
)
2483 inputs
[i
] = ctx
->abi
.frag_pos
[i
];
2485 inputs
[3] = ac_build_fdiv(&ctx
->ac
, ctx
->ac
.f32_1
,
2486 ctx
->abi
.frag_pos
[3]);
2489 ctx
->shader_info
->fs
.num_interp
= index
;
2490 ctx
->shader_info
->fs
.input_mask
= ctx
->input_mask
>> VARYING_SLOT_VAR0
;
2492 if (ctx
->shader_info
->info
.needs_multiview_view_index
)
2493 ctx
->abi
.view_index
= ctx
->inputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)];
2497 scan_shader_output_decl(struct radv_shader_context
*ctx
,
2498 struct nir_variable
*variable
,
2499 struct nir_shader
*shader
,
2500 gl_shader_stage stage
)
2502 int idx
= variable
->data
.location
+ variable
->data
.index
;
2503 unsigned attrib_count
= glsl_count_attribute_slots(variable
->type
, false);
2504 uint64_t mask_attribs
;
2506 variable
->data
.driver_location
= idx
* 4;
2508 /* tess ctrl has it's own load/store paths for outputs */
2509 if (stage
== MESA_SHADER_TESS_CTRL
)
2512 if (variable
->data
.compact
) {
2513 unsigned component_count
= variable
->data
.location_frac
+
2514 glsl_get_length(variable
->type
);
2515 attrib_count
= (component_count
+ 3) / 4;
2518 mask_attribs
= ((1ull << attrib_count
) - 1) << idx
;
2519 if (stage
== MESA_SHADER_VERTEX
||
2520 stage
== MESA_SHADER_TESS_EVAL
||
2521 stage
== MESA_SHADER_GEOMETRY
) {
2522 if (idx
== VARYING_SLOT_CLIP_DIST0
) {
2523 if (stage
== MESA_SHADER_VERTEX
) {
2524 ctx
->shader_info
->vs
.outinfo
.clip_dist_mask
= (1 << shader
->info
.clip_distance_array_size
) - 1;
2525 ctx
->shader_info
->vs
.outinfo
.cull_dist_mask
= (1 << shader
->info
.cull_distance_array_size
) - 1;
2526 ctx
->shader_info
->vs
.outinfo
.cull_dist_mask
<<= shader
->info
.clip_distance_array_size
;
2528 if (stage
== MESA_SHADER_TESS_EVAL
) {
2529 ctx
->shader_info
->tes
.outinfo
.clip_dist_mask
= (1 << shader
->info
.clip_distance_array_size
) - 1;
2530 ctx
->shader_info
->tes
.outinfo
.cull_dist_mask
= (1 << shader
->info
.cull_distance_array_size
) - 1;
2531 ctx
->shader_info
->tes
.outinfo
.cull_dist_mask
<<= shader
->info
.clip_distance_array_size
;
2536 ctx
->output_mask
|= mask_attribs
;
2540 /* Initialize arguments for the shader export intrinsic */
2542 si_llvm_init_export_args(struct radv_shader_context
*ctx
,
2543 LLVMValueRef
*values
,
2544 unsigned enabled_channels
,
2546 struct ac_export_args
*args
)
2548 /* Specify the channels that are enabled. */
2549 args
->enabled_channels
= enabled_channels
;
2551 /* Specify whether the EXEC mask represents the valid mask */
2552 args
->valid_mask
= 0;
2554 /* Specify whether this is the last export */
2557 /* Specify the target we are exporting */
2558 args
->target
= target
;
2560 args
->compr
= false;
2561 args
->out
[0] = LLVMGetUndef(ctx
->ac
.f32
);
2562 args
->out
[1] = LLVMGetUndef(ctx
->ac
.f32
);
2563 args
->out
[2] = LLVMGetUndef(ctx
->ac
.f32
);
2564 args
->out
[3] = LLVMGetUndef(ctx
->ac
.f32
);
2569 bool is_16bit
= ac_get_type_size(LLVMTypeOf(values
[0])) == 2;
2570 if (ctx
->stage
== MESA_SHADER_FRAGMENT
) {
2571 unsigned index
= target
- V_008DFC_SQ_EXP_MRT
;
2572 unsigned col_format
= (ctx
->options
->key
.fs
.col_format
>> (4 * index
)) & 0xf;
2573 bool is_int8
= (ctx
->options
->key
.fs
.is_int8
>> index
) & 1;
2574 bool is_int10
= (ctx
->options
->key
.fs
.is_int10
>> index
) & 1;
2577 LLVMValueRef (*packf
)(struct ac_llvm_context
*ctx
, LLVMValueRef args
[2]) = NULL
;
2578 LLVMValueRef (*packi
)(struct ac_llvm_context
*ctx
, LLVMValueRef args
[2],
2579 unsigned bits
, bool hi
) = NULL
;
2581 switch(col_format
) {
2582 case V_028714_SPI_SHADER_ZERO
:
2583 args
->enabled_channels
= 0; /* writemask */
2584 args
->target
= V_008DFC_SQ_EXP_NULL
;
2587 case V_028714_SPI_SHADER_32_R
:
2588 args
->enabled_channels
= 1;
2589 args
->out
[0] = values
[0];
2592 case V_028714_SPI_SHADER_32_GR
:
2593 args
->enabled_channels
= 0x3;
2594 args
->out
[0] = values
[0];
2595 args
->out
[1] = values
[1];
2598 case V_028714_SPI_SHADER_32_AR
:
2599 args
->enabled_channels
= 0x9;
2600 args
->out
[0] = values
[0];
2601 args
->out
[3] = values
[3];
2604 case V_028714_SPI_SHADER_FP16_ABGR
:
2605 args
->enabled_channels
= 0x5;
2606 packf
= ac_build_cvt_pkrtz_f16
;
2608 for (unsigned chan
= 0; chan
< 4; chan
++)
2609 values
[chan
] = LLVMBuildFPExt(ctx
->ac
.builder
,
2615 case V_028714_SPI_SHADER_UNORM16_ABGR
:
2616 args
->enabled_channels
= 0x5;
2617 packf
= ac_build_cvt_pknorm_u16
;
2620 case V_028714_SPI_SHADER_SNORM16_ABGR
:
2621 args
->enabled_channels
= 0x5;
2622 packf
= ac_build_cvt_pknorm_i16
;
2625 case V_028714_SPI_SHADER_UINT16_ABGR
:
2626 args
->enabled_channels
= 0x5;
2627 packi
= ac_build_cvt_pk_u16
;
2629 for (unsigned chan
= 0; chan
< 4; chan
++)
2630 values
[chan
] = LLVMBuildZExt(ctx
->ac
.builder
,
2631 ac_to_integer(&ctx
->ac
, values
[chan
]),
2636 case V_028714_SPI_SHADER_SINT16_ABGR
:
2637 args
->enabled_channels
= 0x5;
2638 packi
= ac_build_cvt_pk_i16
;
2640 for (unsigned chan
= 0; chan
< 4; chan
++)
2641 values
[chan
] = LLVMBuildSExt(ctx
->ac
.builder
,
2642 ac_to_integer(&ctx
->ac
, values
[chan
]),
2648 case V_028714_SPI_SHADER_32_ABGR
:
2649 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
2653 /* Pack f16 or norm_i16/u16. */
2655 for (chan
= 0; chan
< 2; chan
++) {
2656 LLVMValueRef pack_args
[2] = {
2658 values
[2 * chan
+ 1]
2660 LLVMValueRef packed
;
2662 packed
= packf(&ctx
->ac
, pack_args
);
2663 args
->out
[chan
] = ac_to_float(&ctx
->ac
, packed
);
2665 args
->compr
= 1; /* COMPR flag */
2670 for (chan
= 0; chan
< 2; chan
++) {
2671 LLVMValueRef pack_args
[2] = {
2672 ac_to_integer(&ctx
->ac
, values
[2 * chan
]),
2673 ac_to_integer(&ctx
->ac
, values
[2 * chan
+ 1])
2675 LLVMValueRef packed
;
2677 packed
= packi(&ctx
->ac
, pack_args
,
2678 is_int8
? 8 : is_int10
? 10 : 16,
2680 args
->out
[chan
] = ac_to_float(&ctx
->ac
, packed
);
2682 args
->compr
= 1; /* COMPR flag */
2688 for (unsigned chan
= 0; chan
< 4; chan
++) {
2689 values
[chan
] = LLVMBuildBitCast(ctx
->ac
.builder
, values
[chan
], ctx
->ac
.i16
, "");
2690 args
->out
[chan
] = LLVMBuildZExt(ctx
->ac
.builder
, values
[chan
], ctx
->ac
.i32
, "");
2693 memcpy(&args
->out
[0], values
, sizeof(values
[0]) * 4);
2695 for (unsigned i
= 0; i
< 4; ++i
)
2696 args
->out
[i
] = ac_to_float(&ctx
->ac
, args
->out
[i
]);
2700 radv_export_param(struct radv_shader_context
*ctx
, unsigned index
,
2701 LLVMValueRef
*values
, unsigned enabled_channels
)
2703 struct ac_export_args args
;
2705 si_llvm_init_export_args(ctx
, values
, enabled_channels
,
2706 V_008DFC_SQ_EXP_PARAM
+ index
, &args
);
2707 ac_build_export(&ctx
->ac
, &args
);
2711 radv_load_output(struct radv_shader_context
*ctx
, unsigned index
, unsigned chan
)
2713 LLVMValueRef output
=
2714 ctx
->abi
.outputs
[ac_llvm_reg_index_soa(index
, chan
)];
2716 return LLVMBuildLoad(ctx
->ac
.builder
, output
, "");
2720 radv_emit_stream_output(struct radv_shader_context
*ctx
,
2721 LLVMValueRef
const *so_buffers
,
2722 LLVMValueRef
const *so_write_offsets
,
2723 const struct radv_stream_output
*output
)
2725 unsigned num_comps
= util_bitcount(output
->component_mask
);
2726 unsigned loc
= output
->location
;
2727 unsigned buf
= output
->buffer
;
2728 unsigned offset
= output
->offset
;
2730 LLVMValueRef out
[4];
2732 assert(num_comps
&& num_comps
<= 4);
2733 if (!num_comps
|| num_comps
> 4)
2736 /* Get the first component. */
2737 start
= ffs(output
->component_mask
) - 1;
2739 /* Load the output as int. */
2740 for (int i
= 0; i
< num_comps
; i
++) {
2741 out
[i
] = ac_to_integer(&ctx
->ac
,
2742 radv_load_output(ctx
, loc
, start
+ i
));
2745 /* Pack the output. */
2746 LLVMValueRef vdata
= NULL
;
2748 switch (num_comps
) {
2749 case 1: /* as i32 */
2752 case 2: /* as v2i32 */
2753 case 3: /* as v4i32 (aligned to 4) */
2754 out
[3] = LLVMGetUndef(ctx
->ac
.i32
);
2756 case 4: /* as v4i32 */
2757 vdata
= ac_build_gather_values(&ctx
->ac
, out
,
2758 !ac_has_vec3_support(ctx
->ac
.chip_class
, false) ?
2759 util_next_power_of_two(num_comps
) :
2764 ac_build_buffer_store_dword(&ctx
->ac
, so_buffers
[buf
],
2765 vdata
, num_comps
, so_write_offsets
[buf
],
2766 ctx
->ac
.i32_0
, offset
,
2767 ac_glc
| ac_slc
, false);
2771 radv_emit_streamout(struct radv_shader_context
*ctx
, unsigned stream
)
2773 struct ac_build_if_state if_ctx
;
2776 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
2777 assert(ctx
->streamout_config
);
2778 LLVMValueRef so_vtx_count
=
2779 ac_build_bfe(&ctx
->ac
, ctx
->streamout_config
,
2780 LLVMConstInt(ctx
->ac
.i32
, 16, false),
2781 LLVMConstInt(ctx
->ac
.i32
, 7, false), false);
2783 LLVMValueRef tid
= ac_get_thread_id(&ctx
->ac
);
2785 /* can_emit = tid < so_vtx_count; */
2786 LLVMValueRef can_emit
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntULT
,
2787 tid
, so_vtx_count
, "");
2789 /* Emit the streamout code conditionally. This actually avoids
2790 * out-of-bounds buffer access. The hw tells us via the SGPR
2791 * (so_vtx_count) which threads are allowed to emit streamout data.
2793 ac_nir_build_if(&if_ctx
, ctx
, can_emit
);
2795 /* The buffer offset is computed as follows:
2796 * ByteOffset = streamout_offset[buffer_id]*4 +
2797 * (streamout_write_index + thread_id)*stride[buffer_id] +
2800 LLVMValueRef so_write_index
= ctx
->streamout_write_idx
;
2802 /* Compute (streamout_write_index + thread_id). */
2804 LLVMBuildAdd(ctx
->ac
.builder
, so_write_index
, tid
, "");
2806 /* Load the descriptor and compute the write offset for each
2809 LLVMValueRef so_write_offset
[4] = {};
2810 LLVMValueRef so_buffers
[4] = {};
2811 LLVMValueRef buf_ptr
= ctx
->streamout_buffers
;
2813 for (i
= 0; i
< 4; i
++) {
2814 uint16_t stride
= ctx
->shader_info
->info
.so
.strides
[i
];
2819 LLVMValueRef offset
=
2820 LLVMConstInt(ctx
->ac
.i32
, i
, false);
2822 so_buffers
[i
] = ac_build_load_to_sgpr(&ctx
->ac
,
2825 LLVMValueRef so_offset
= ctx
->streamout_offset
[i
];
2827 so_offset
= LLVMBuildMul(ctx
->ac
.builder
, so_offset
,
2828 LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
2830 so_write_offset
[i
] =
2831 ac_build_imad(&ctx
->ac
, so_write_index
,
2832 LLVMConstInt(ctx
->ac
.i32
,
2837 /* Write streamout data. */
2838 for (i
= 0; i
< ctx
->shader_info
->info
.so
.num_outputs
; i
++) {
2839 struct radv_stream_output
*output
=
2840 &ctx
->shader_info
->info
.so
.outputs
[i
];
2842 if (stream
!= output
->stream
)
2845 radv_emit_stream_output(ctx
, so_buffers
,
2846 so_write_offset
, output
);
2849 ac_nir_build_endif(&if_ctx
);
2853 handle_vs_outputs_post(struct radv_shader_context
*ctx
,
2854 bool export_prim_id
, bool export_layer_id
,
2855 bool export_clip_dists
,
2856 struct radv_vs_output_info
*outinfo
)
2858 uint32_t param_count
= 0;
2860 unsigned pos_idx
, num_pos_exports
= 0;
2861 struct ac_export_args args
, pos_args
[4] = {};
2862 LLVMValueRef psize_value
= NULL
, layer_value
= NULL
, viewport_index_value
= NULL
;
2865 if (ctx
->options
->key
.has_multiview_view_index
) {
2866 LLVMValueRef
* tmp_out
= &ctx
->abi
.outputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, 0)];
2868 for(unsigned i
= 0; i
< 4; ++i
)
2869 ctx
->abi
.outputs
[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER
, i
)] =
2870 ac_build_alloca_undef(&ctx
->ac
, ctx
->ac
.f32
, "");
2873 LLVMBuildStore(ctx
->ac
.builder
, ac_to_float(&ctx
->ac
, ctx
->abi
.view_index
), *tmp_out
);
2874 ctx
->output_mask
|= 1ull << VARYING_SLOT_LAYER
;
2877 memset(outinfo
->vs_output_param_offset
, AC_EXP_PARAM_UNDEFINED
,
2878 sizeof(outinfo
->vs_output_param_offset
));
2880 for(unsigned location
= VARYING_SLOT_CLIP_DIST0
; location
<= VARYING_SLOT_CLIP_DIST1
; ++location
) {
2881 if (ctx
->output_mask
& (1ull << location
)) {
2882 unsigned output_usage_mask
, length
;
2883 LLVMValueRef slots
[4];
2886 if (ctx
->stage
== MESA_SHADER_VERTEX
&&
2887 !ctx
->is_gs_copy_shader
) {
2889 ctx
->shader_info
->info
.vs
.output_usage_mask
[location
];
2890 } else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
2892 ctx
->shader_info
->info
.tes
.output_usage_mask
[location
];
2894 assert(ctx
->is_gs_copy_shader
);
2896 ctx
->shader_info
->info
.gs
.output_usage_mask
[location
];
2899 length
= util_last_bit(output_usage_mask
);
2901 for (j
= 0; j
< length
; j
++)
2902 slots
[j
] = ac_to_float(&ctx
->ac
, radv_load_output(ctx
, location
, j
));
2904 for (i
= length
; i
< 4; i
++)
2905 slots
[i
] = LLVMGetUndef(ctx
->ac
.f32
);
2907 target
= V_008DFC_SQ_EXP_POS
+ 2 + (location
- VARYING_SLOT_CLIP_DIST0
);
2908 si_llvm_init_export_args(ctx
, &slots
[0], 0xf, target
, &args
);
2909 memcpy(&pos_args
[target
- V_008DFC_SQ_EXP_POS
],
2910 &args
, sizeof(args
));
2912 if (export_clip_dists
) {
2913 /* Export the clip/cull distances values to the next stage. */
2914 radv_export_param(ctx
, param_count
, &slots
[0], 0xf);
2915 outinfo
->vs_output_param_offset
[location
] = param_count
++;
2920 LLVMValueRef pos_values
[4] = {ctx
->ac
.f32_0
, ctx
->ac
.f32_0
, ctx
->ac
.f32_0
, ctx
->ac
.f32_1
};
2921 if (ctx
->output_mask
& (1ull << VARYING_SLOT_POS
)) {
2922 for (unsigned j
= 0; j
< 4; j
++)
2923 pos_values
[j
] = radv_load_output(ctx
, VARYING_SLOT_POS
, j
);
2925 si_llvm_init_export_args(ctx
, pos_values
, 0xf, V_008DFC_SQ_EXP_POS
, &pos_args
[0]);
2927 if (ctx
->output_mask
& (1ull << VARYING_SLOT_PSIZ
)) {
2928 outinfo
->writes_pointsize
= true;
2929 psize_value
= radv_load_output(ctx
, VARYING_SLOT_PSIZ
, 0);
2932 if (ctx
->output_mask
& (1ull << VARYING_SLOT_LAYER
)) {
2933 outinfo
->writes_layer
= true;
2934 layer_value
= radv_load_output(ctx
, VARYING_SLOT_LAYER
, 0);
2937 if (ctx
->output_mask
& (1ull << VARYING_SLOT_VIEWPORT
)) {
2938 outinfo
->writes_viewport_index
= true;
2939 viewport_index_value
= radv_load_output(ctx
, VARYING_SLOT_VIEWPORT
, 0);
2942 if (ctx
->shader_info
->info
.so
.num_outputs
&&
2943 !ctx
->is_gs_copy_shader
) {
2944 /* The GS copy shader emission already emits streamout. */
2945 radv_emit_streamout(ctx
, 0);
2948 if (outinfo
->writes_pointsize
||
2949 outinfo
->writes_layer
||
2950 outinfo
->writes_viewport_index
) {
2951 pos_args
[1].enabled_channels
= ((outinfo
->writes_pointsize
== true ? 1 : 0) |
2952 (outinfo
->writes_layer
== true ? 4 : 0));
2953 pos_args
[1].valid_mask
= 0;
2954 pos_args
[1].done
= 0;
2955 pos_args
[1].target
= V_008DFC_SQ_EXP_POS
+ 1;
2956 pos_args
[1].compr
= 0;
2957 pos_args
[1].out
[0] = ctx
->ac
.f32_0
; /* X */
2958 pos_args
[1].out
[1] = ctx
->ac
.f32_0
; /* Y */
2959 pos_args
[1].out
[2] = ctx
->ac
.f32_0
; /* Z */
2960 pos_args
[1].out
[3] = ctx
->ac
.f32_0
; /* W */
2962 if (outinfo
->writes_pointsize
== true)
2963 pos_args
[1].out
[0] = psize_value
;
2964 if (outinfo
->writes_layer
== true)
2965 pos_args
[1].out
[2] = layer_value
;
2966 if (outinfo
->writes_viewport_index
== true) {
2967 if (ctx
->options
->chip_class
>= GFX9
) {
2968 /* GFX9 has the layer in out.z[10:0] and the viewport
2969 * index in out.z[19:16].
2971 LLVMValueRef v
= viewport_index_value
;
2972 v
= ac_to_integer(&ctx
->ac
, v
);
2973 v
= LLVMBuildShl(ctx
->ac
.builder
, v
,
2974 LLVMConstInt(ctx
->ac
.i32
, 16, false),
2976 v
= LLVMBuildOr(ctx
->ac
.builder
, v
,
2977 ac_to_integer(&ctx
->ac
, pos_args
[1].out
[2]), "");
2979 pos_args
[1].out
[2] = ac_to_float(&ctx
->ac
, v
);
2980 pos_args
[1].enabled_channels
|= 1 << 2;
2982 pos_args
[1].out
[3] = viewport_index_value
;
2983 pos_args
[1].enabled_channels
|= 1 << 3;
2987 for (i
= 0; i
< 4; i
++) {
2988 if (pos_args
[i
].out
[0])
2993 for (i
= 0; i
< 4; i
++) {
2994 if (!pos_args
[i
].out
[0])
2997 /* Specify the target we are exporting */
2998 pos_args
[i
].target
= V_008DFC_SQ_EXP_POS
+ pos_idx
++;
2999 if (pos_idx
== num_pos_exports
)
3000 pos_args
[i
].done
= 1;
3001 ac_build_export(&ctx
->ac
, &pos_args
[i
]);
3004 for (unsigned i
= 0; i
< AC_LLVM_MAX_OUTPUTS
; ++i
) {
3005 LLVMValueRef values
[4];
3006 if (!(ctx
->output_mask
& (1ull << i
)))
3009 if (i
!= VARYING_SLOT_LAYER
&&
3010 i
!= VARYING_SLOT_PRIMITIVE_ID
&&
3011 i
< VARYING_SLOT_VAR0
)
3014 for (unsigned j
= 0; j
< 4; j
++)
3015 values
[j
] = ac_to_float(&ctx
->ac
, radv_load_output(ctx
, i
, j
));
3017 unsigned output_usage_mask
;
3019 if (ctx
->stage
== MESA_SHADER_VERTEX
&&
3020 !ctx
->is_gs_copy_shader
) {
3022 ctx
->shader_info
->info
.vs
.output_usage_mask
[i
];
3023 } else if (ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
3025 ctx
->shader_info
->info
.tes
.output_usage_mask
[i
];
3027 assert(ctx
->is_gs_copy_shader
);
3029 ctx
->shader_info
->info
.gs
.output_usage_mask
[i
];
3032 radv_export_param(ctx
, param_count
, values
, output_usage_mask
);
3034 outinfo
->vs_output_param_offset
[i
] = param_count
++;
3037 if (export_prim_id
) {
3038 LLVMValueRef values
[4];
3040 values
[0] = ctx
->vs_prim_id
;
3041 for (unsigned j
= 1; j
< 4; j
++)
3042 values
[j
] = ctx
->ac
.f32_0
;
3044 radv_export_param(ctx
, param_count
, values
, 0x1);
3046 outinfo
->vs_output_param_offset
[VARYING_SLOT_PRIMITIVE_ID
] = param_count
++;
3047 outinfo
->export_prim_id
= true;
3050 if (export_layer_id
&& layer_value
) {
3051 LLVMValueRef values
[4];
3053 values
[0] = layer_value
;
3054 for (unsigned j
= 1; j
< 4; j
++)
3055 values
[j
] = ctx
->ac
.f32_0
;
3057 radv_export_param(ctx
, param_count
, values
, 0x1);
3059 outinfo
->vs_output_param_offset
[VARYING_SLOT_LAYER
] = param_count
++;
3062 outinfo
->pos_exports
= num_pos_exports
;
3063 outinfo
->param_exports
= param_count
;
3067 handle_es_outputs_post(struct radv_shader_context
*ctx
,
3068 struct radv_es_output_info
*outinfo
)
3071 uint64_t max_output_written
= 0;
3072 LLVMValueRef lds_base
= NULL
;
3074 for (unsigned i
= 0; i
< AC_LLVM_MAX_OUTPUTS
; ++i
) {
3077 if (!(ctx
->output_mask
& (1ull << i
)))
3080 param_index
= shader_io_get_unique_index(i
);
3082 max_output_written
= MAX2(param_index
, max_output_written
);
3085 outinfo
->esgs_itemsize
= (max_output_written
+ 1) * 16;
3087 if (ctx
->ac
.chip_class
>= GFX9
) {
3088 unsigned itemsize_dw
= outinfo
->esgs_itemsize
/ 4;
3089 LLVMValueRef vertex_idx
= ac_get_thread_id(&ctx
->ac
);
3090 LLVMValueRef wave_idx
= ac_unpack_param(&ctx
->ac
, ctx
->merged_wave_info
, 24, 4);
3091 vertex_idx
= LLVMBuildOr(ctx
->ac
.builder
, vertex_idx
,
3092 LLVMBuildMul(ctx
->ac
.builder
, wave_idx
,
3093 LLVMConstInt(ctx
->ac
.i32
, 64, false), ""), "");
3094 lds_base
= LLVMBuildMul(ctx
->ac
.builder
, vertex_idx
,
3095 LLVMConstInt(ctx
->ac
.i32
, itemsize_dw
, 0), "");
3098 for (unsigned i
= 0; i
< AC_LLVM_MAX_OUTPUTS
; ++i
) {
3099 LLVMValueRef dw_addr
= NULL
;
3100 LLVMValueRef
*out_ptr
= &ctx
->abi
.outputs
[i
* 4];
3101 unsigned output_usage_mask
;
3104 if (!(ctx
->output_mask
& (1ull << i
)))
3107 if (ctx
->stage
== MESA_SHADER_VERTEX
) {
3109 ctx
->shader_info
->info
.vs
.output_usage_mask
[i
];
3111 assert(ctx
->stage
== MESA_SHADER_TESS_EVAL
);
3113 ctx
->shader_info
->info
.tes
.output_usage_mask
[i
];
3116 param_index
= shader_io_get_unique_index(i
);
3119 dw_addr
= LLVMBuildAdd(ctx
->ac
.builder
, lds_base
,
3120 LLVMConstInt(ctx
->ac
.i32
, param_index
* 4, false),
3124 for (j
= 0; j
< 4; j
++) {
3125 if (!(output_usage_mask
& (1 << j
)))
3128 LLVMValueRef out_val
= LLVMBuildLoad(ctx
->ac
.builder
, out_ptr
[j
], "");
3129 out_val
= ac_to_integer(&ctx
->ac
, out_val
);
3130 out_val
= LLVMBuildZExtOrBitCast(ctx
->ac
.builder
, out_val
, ctx
->ac
.i32
, "");
3132 if (ctx
->ac
.chip_class
>= GFX9
) {
3133 LLVMValueRef dw_addr_offset
=
3134 LLVMBuildAdd(ctx
->ac
.builder
, dw_addr
,
3135 LLVMConstInt(ctx
->ac
.i32
,
3138 ac_lds_store(&ctx
->ac
, dw_addr_offset
, out_val
);
3140 ac_build_buffer_store_dword(&ctx
->ac
,
3143 NULL
, ctx
->es2gs_offset
,
3144 (4 * param_index
+ j
) * 4,
3145 ac_glc
| ac_slc
, true);
3152 handle_ls_outputs_post(struct radv_shader_context
*ctx
)
3154 LLVMValueRef vertex_id
= ctx
->rel_auto_id
;
3155 uint32_t num_tcs_inputs
= util_last_bit64(ctx
->shader_info
->info
.vs
.ls_outputs_written
);
3156 LLVMValueRef vertex_dw_stride
= LLVMConstInt(ctx
->ac
.i32
, num_tcs_inputs
* 4, false);
3157 LLVMValueRef base_dw_addr
= LLVMBuildMul(ctx
->ac
.builder
, vertex_id
,
3158 vertex_dw_stride
, "");
3160 for (unsigned i
= 0; i
< AC_LLVM_MAX_OUTPUTS
; ++i
) {
3161 LLVMValueRef
*out_ptr
= &ctx
->abi
.outputs
[i
* 4];
3163 if (!(ctx
->output_mask
& (1ull << i
)))
3166 int param
= shader_io_get_unique_index(i
);
3167 LLVMValueRef dw_addr
= LLVMBuildAdd(ctx
->ac
.builder
, base_dw_addr
,
3168 LLVMConstInt(ctx
->ac
.i32
, param
* 4, false),
3170 for (unsigned j
= 0; j
< 4; j
++) {
3171 LLVMValueRef value
= LLVMBuildLoad(ctx
->ac
.builder
, out_ptr
[j
], "");
3172 value
= ac_to_integer(&ctx
->ac
, value
);
3173 value
= LLVMBuildZExtOrBitCast(ctx
->ac
.builder
, value
, ctx
->ac
.i32
, "");
3174 ac_lds_store(&ctx
->ac
, dw_addr
, value
);
3175 dw_addr
= LLVMBuildAdd(ctx
->ac
.builder
, dw_addr
, ctx
->ac
.i32_1
, "");
3181 write_tess_factors(struct radv_shader_context
*ctx
)
3183 unsigned stride
, outer_comps
, inner_comps
;
3184 struct ac_build_if_state if_ctx
, inner_if_ctx
;
3185 LLVMValueRef invocation_id
= ac_unpack_param(&ctx
->ac
, ctx
->abi
.tcs_rel_ids
, 8, 5);
3186 LLVMValueRef rel_patch_id
= ac_unpack_param(&ctx
->ac
, ctx
->abi
.tcs_rel_ids
, 0, 8);
3187 unsigned tess_inner_index
= 0, tess_outer_index
;
3188 LLVMValueRef lds_base
, lds_inner
= NULL
, lds_outer
, byteoffset
, buffer
;
3189 LLVMValueRef out
[6], vec0
, vec1
, tf_base
, inner
[4], outer
[4];
3191 ac_emit_barrier(&ctx
->ac
, ctx
->stage
);
3193 switch (ctx
->options
->key
.tcs
.primitive_mode
) {
3213 ac_nir_build_if(&if_ctx
, ctx
,
3214 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
3215 invocation_id
, ctx
->ac
.i32_0
, ""));
3217 lds_base
= get_tcs_out_current_patch_data_offset(ctx
);
3220 tess_inner_index
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER
);
3221 lds_inner
= LLVMBuildAdd(ctx
->ac
.builder
, lds_base
,
3222 LLVMConstInt(ctx
->ac
.i32
, tess_inner_index
* 4, false), "");
3225 tess_outer_index
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER
);
3226 lds_outer
= LLVMBuildAdd(ctx
->ac
.builder
, lds_base
,
3227 LLVMConstInt(ctx
->ac
.i32
, tess_outer_index
* 4, false), "");
3229 for (i
= 0; i
< 4; i
++) {
3230 inner
[i
] = LLVMGetUndef(ctx
->ac
.i32
);
3231 outer
[i
] = LLVMGetUndef(ctx
->ac
.i32
);
3235 if (ctx
->options
->key
.tcs
.primitive_mode
== GL_ISOLINES
) {
3236 outer
[0] = out
[1] = ac_lds_load(&ctx
->ac
, lds_outer
);
3237 lds_outer
= LLVMBuildAdd(ctx
->ac
.builder
, lds_outer
,
3239 outer
[1] = out
[0] = ac_lds_load(&ctx
->ac
, lds_outer
);
3241 for (i
= 0; i
< outer_comps
; i
++) {
3243 ac_lds_load(&ctx
->ac
, lds_outer
);
3244 lds_outer
= LLVMBuildAdd(ctx
->ac
.builder
, lds_outer
,
3247 for (i
= 0; i
< inner_comps
; i
++) {
3248 inner
[i
] = out
[outer_comps
+i
] =
3249 ac_lds_load(&ctx
->ac
, lds_inner
);
3250 lds_inner
= LLVMBuildAdd(ctx
->ac
.builder
, lds_inner
,
3255 /* Convert the outputs to vectors for stores. */
3256 vec0
= ac_build_gather_values(&ctx
->ac
, out
, MIN2(stride
, 4));
3260 vec1
= ac_build_gather_values(&ctx
->ac
, out
+ 4, stride
- 4);
3263 buffer
= ctx
->hs_ring_tess_factor
;
3264 tf_base
= ctx
->tess_factor_offset
;
3265 byteoffset
= LLVMBuildMul(ctx
->ac
.builder
, rel_patch_id
,
3266 LLVMConstInt(ctx
->ac
.i32
, 4 * stride
, false), "");
3267 unsigned tf_offset
= 0;
3269 if (ctx
->options
->chip_class
<= GFX8
) {
3270 ac_nir_build_if(&inner_if_ctx
, ctx
,
3271 LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
,
3272 rel_patch_id
, ctx
->ac
.i32_0
, ""));
3274 /* Store the dynamic HS control word. */
3275 ac_build_buffer_store_dword(&ctx
->ac
, buffer
,
3276 LLVMConstInt(ctx
->ac
.i32
, 0x80000000, false),
3277 1, ctx
->ac
.i32_0
, tf_base
,
3281 ac_nir_build_endif(&inner_if_ctx
);
3284 /* Store the tessellation factors. */
3285 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec0
,
3286 MIN2(stride
, 4), byteoffset
, tf_base
,
3287 tf_offset
, ac_glc
, false);
3289 ac_build_buffer_store_dword(&ctx
->ac
, buffer
, vec1
,
3290 stride
- 4, byteoffset
, tf_base
,
3291 16 + tf_offset
, ac_glc
, false);
3293 //store to offchip for TES to read - only if TES reads them
3294 if (ctx
->options
->key
.tcs
.tes_reads_tess_factors
) {
3295 LLVMValueRef inner_vec
, outer_vec
, tf_outer_offset
;
3296 LLVMValueRef tf_inner_offset
;
3297 unsigned param_outer
, param_inner
;
3299 param_outer
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER
);
3300 tf_outer_offset
= get_tcs_tes_buffer_address(ctx
, NULL
,
3301 LLVMConstInt(ctx
->ac
.i32
, param_outer
, 0));
3303 outer_vec
= ac_build_gather_values(&ctx
->ac
, outer
,
3304 util_next_power_of_two(outer_comps
));
3306 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, outer_vec
,
3307 outer_comps
, tf_outer_offset
,
3308 ctx
->oc_lds
, 0, ac_glc
, false);
3310 param_inner
= shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER
);
3311 tf_inner_offset
= get_tcs_tes_buffer_address(ctx
, NULL
,
3312 LLVMConstInt(ctx
->ac
.i32
, param_inner
, 0));
3314 inner_vec
= inner_comps
== 1 ? inner
[0] :
3315 ac_build_gather_values(&ctx
->ac
, inner
, inner_comps
);
3316 ac_build_buffer_store_dword(&ctx
->ac
, ctx
->hs_ring_tess_offchip
, inner_vec
,
3317 inner_comps
, tf_inner_offset
,
3318 ctx
->oc_lds
, 0, ac_glc
, false);
3321 ac_nir_build_endif(&if_ctx
);
3325 handle_tcs_outputs_post(struct radv_shader_context
*ctx
)
3327 write_tess_factors(ctx
);
3331 si_export_mrt_color(struct radv_shader_context
*ctx
,
3332 LLVMValueRef
*color
, unsigned index
,
3333 struct ac_export_args
*args
)
3336 si_llvm_init_export_args(ctx
, color
, 0xf,
3337 V_008DFC_SQ_EXP_MRT
+ index
, args
);
3338 if (!args
->enabled_channels
)
3339 return false; /* unnecessary NULL export */
3345 radv_export_mrt_z(struct radv_shader_context
*ctx
,
3346 LLVMValueRef depth
, LLVMValueRef stencil
,
3347 LLVMValueRef samplemask
)
3349 struct ac_export_args args
;
3351 ac_export_mrt_z(&ctx
->ac
, depth
, stencil
, samplemask
, &args
);
3353 ac_build_export(&ctx
->ac
, &args
);
3357 handle_fs_outputs_post(struct radv_shader_context
*ctx
)
3360 LLVMValueRef depth
= NULL
, stencil
= NULL
, samplemask
= NULL
;
3361 struct ac_export_args color_args
[8];
3363 for (unsigned i
= 0; i
< AC_LLVM_MAX_OUTPUTS
; ++i
) {
3364 LLVMValueRef values
[4];
3366 if (!(ctx
->output_mask
& (1ull << i
)))
3369 if (i
< FRAG_RESULT_DATA0
)
3372 for (unsigned j
= 0; j
< 4; j
++)
3373 values
[j
] = ac_to_float(&ctx
->ac
,
3374 radv_load_output(ctx
, i
, j
));
3376 bool ret
= si_export_mrt_color(ctx
, values
,
3377 i
- FRAG_RESULT_DATA0
,
3378 &color_args
[index
]);
3383 /* Process depth, stencil, samplemask. */
3384 if (ctx
->shader_info
->info
.ps
.writes_z
) {
3385 depth
= ac_to_float(&ctx
->ac
,
3386 radv_load_output(ctx
, FRAG_RESULT_DEPTH
, 0));
3388 if (ctx
->shader_info
->info
.ps
.writes_stencil
) {
3389 stencil
= ac_to_float(&ctx
->ac
,
3390 radv_load_output(ctx
, FRAG_RESULT_STENCIL
, 0));
3392 if (ctx
->shader_info
->info
.ps
.writes_sample_mask
) {
3393 samplemask
= ac_to_float(&ctx
->ac
,
3394 radv_load_output(ctx
, FRAG_RESULT_SAMPLE_MASK
, 0));
3397 /* Set the DONE bit on last non-null color export only if Z isn't
3401 !ctx
->shader_info
->info
.ps
.writes_z
&&
3402 !ctx
->shader_info
->info
.ps
.writes_stencil
&&
3403 !ctx
->shader_info
->info
.ps
.writes_sample_mask
) {
3404 unsigned last
= index
- 1;
3406 color_args
[last
].valid_mask
= 1; /* whether the EXEC mask is valid */
3407 color_args
[last
].done
= 1; /* DONE bit */
3410 /* Export PS outputs. */
3411 for (unsigned i
= 0; i
< index
; i
++)
3412 ac_build_export(&ctx
->ac
, &color_args
[i
]);
3414 if (depth
|| stencil
|| samplemask
)
3415 radv_export_mrt_z(ctx
, depth
, stencil
, samplemask
);
3417 ac_build_export_null(&ctx
->ac
);
3421 emit_gs_epilogue(struct radv_shader_context
*ctx
)
3423 ac_build_sendmsg(&ctx
->ac
, AC_SENDMSG_GS_OP_NOP
| AC_SENDMSG_GS_DONE
, ctx
->gs_wave_id
);
3427 handle_shader_outputs_post(struct ac_shader_abi
*abi
, unsigned max_outputs
,
3428 LLVMValueRef
*addrs
)
3430 struct radv_shader_context
*ctx
= radv_shader_context_from_abi(abi
);
3432 switch (ctx
->stage
) {
3433 case MESA_SHADER_VERTEX
:
3434 if (ctx
->options
->key
.vs
.as_ls
)
3435 handle_ls_outputs_post(ctx
);
3436 else if (ctx
->options
->key
.vs
.as_es
)
3437 handle_es_outputs_post(ctx
, &ctx
->shader_info
->vs
.es_info
);
3439 handle_vs_outputs_post(ctx
, ctx
->options
->key
.vs
.export_prim_id
,
3440 ctx
->options
->key
.vs
.export_layer_id
,
3441 ctx
->options
->key
.vs
.export_clip_dists
,
3442 &ctx
->shader_info
->vs
.outinfo
);
3444 case MESA_SHADER_FRAGMENT
:
3445 handle_fs_outputs_post(ctx
);
3447 case MESA_SHADER_GEOMETRY
:
3448 emit_gs_epilogue(ctx
);
3450 case MESA_SHADER_TESS_CTRL
:
3451 handle_tcs_outputs_post(ctx
);
3453 case MESA_SHADER_TESS_EVAL
:
3454 if (ctx
->options
->key
.tes
.as_es
)
3455 handle_es_outputs_post(ctx
, &ctx
->shader_info
->tes
.es_info
);
3457 handle_vs_outputs_post(ctx
, ctx
->options
->key
.tes
.export_prim_id
,
3458 ctx
->options
->key
.tes
.export_layer_id
,
3459 ctx
->options
->key
.tes
.export_clip_dists
,
3460 &ctx
->shader_info
->tes
.outinfo
);
3467 static void ac_llvm_finalize_module(struct radv_shader_context
*ctx
,
3468 LLVMPassManagerRef passmgr
,
3469 const struct radv_nir_compiler_options
*options
)
3471 LLVMRunPassManager(passmgr
, ctx
->ac
.module
);
3472 LLVMDisposeBuilder(ctx
->ac
.builder
);
3474 ac_llvm_context_dispose(&ctx
->ac
);
3478 ac_nir_eliminate_const_vs_outputs(struct radv_shader_context
*ctx
)
3480 struct radv_vs_output_info
*outinfo
;
3482 switch (ctx
->stage
) {
3483 case MESA_SHADER_FRAGMENT
:
3484 case MESA_SHADER_COMPUTE
:
3485 case MESA_SHADER_TESS_CTRL
:
3486 case MESA_SHADER_GEOMETRY
:
3488 case MESA_SHADER_VERTEX
:
3489 if (ctx
->options
->key
.vs
.as_ls
||
3490 ctx
->options
->key
.vs
.as_es
)
3492 outinfo
= &ctx
->shader_info
->vs
.outinfo
;
3494 case MESA_SHADER_TESS_EVAL
:
3495 if (ctx
->options
->key
.vs
.as_es
)
3497 outinfo
= &ctx
->shader_info
->tes
.outinfo
;
3500 unreachable("Unhandled shader type");
3503 ac_optimize_vs_outputs(&ctx
->ac
,
3505 outinfo
->vs_output_param_offset
,
3507 &outinfo
->param_exports
);
3511 ac_setup_rings(struct radv_shader_context
*ctx
)
3513 if (ctx
->options
->chip_class
<= GFX8
&&
3514 (ctx
->stage
== MESA_SHADER_GEOMETRY
||
3515 ctx
->options
->key
.vs
.as_es
|| ctx
->options
->key
.tes
.as_es
)) {
3516 unsigned ring
= ctx
->stage
== MESA_SHADER_GEOMETRY
? RING_ESGS_GS
3518 LLVMValueRef offset
= LLVMConstInt(ctx
->ac
.i32
, ring
, false);
3520 ctx
->esgs_ring
= ac_build_load_to_sgpr(&ctx
->ac
,
3525 if (ctx
->is_gs_copy_shader
) {
3527 ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
,
3528 LLVMConstInt(ctx
->ac
.i32
,
3529 RING_GSVS_VS
, false));
3532 if (ctx
->stage
== MESA_SHADER_GEOMETRY
) {
3533 /* The conceptual layout of the GSVS ring is
3534 * v0c0 .. vLv0 v0c1 .. vLc1 ..
3535 * but the real memory layout is swizzled across
3537 * t0v0c0 .. t15v0c0 t0v1c0 .. t15v1c0 ... t15vLcL
3539 * Override the buffer descriptor accordingly.
3541 LLVMTypeRef v2i64
= LLVMVectorType(ctx
->ac
.i64
, 2);
3542 uint64_t stream_offset
= 0;
3543 unsigned num_records
= 64;
3544 LLVMValueRef base_ring
;
3547 ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
,
3548 LLVMConstInt(ctx
->ac
.i32
,
3549 RING_GSVS_GS
, false));
3551 for (unsigned stream
= 0; stream
< 4; stream
++) {
3552 unsigned num_components
, stride
;
3553 LLVMValueRef ring
, tmp
;
3556 ctx
->shader_info
->info
.gs
.num_stream_output_components
[stream
];
3558 if (!num_components
)
3561 stride
= 4 * num_components
* ctx
->gs_max_out_vertices
;
3563 /* Limit on the stride field for <= GFX7. */
3564 assert(stride
< (1 << 14));
3566 ring
= LLVMBuildBitCast(ctx
->ac
.builder
,
3567 base_ring
, v2i64
, "");
3568 tmp
= LLVMBuildExtractElement(ctx
->ac
.builder
,
3569 ring
, ctx
->ac
.i32_0
, "");
3570 tmp
= LLVMBuildAdd(ctx
->ac
.builder
, tmp
,
3571 LLVMConstInt(ctx
->ac
.i64
,
3572 stream_offset
, 0), "");
3573 ring
= LLVMBuildInsertElement(ctx
->ac
.builder
,
3574 ring
, tmp
, ctx
->ac
.i32_0
, "");
3576 stream_offset
+= stride
* 64;
3578 ring
= LLVMBuildBitCast(ctx
->ac
.builder
, ring
,
3581 tmp
= LLVMBuildExtractElement(ctx
->ac
.builder
, ring
,
3583 tmp
= LLVMBuildOr(ctx
->ac
.builder
, tmp
,
3584 LLVMConstInt(ctx
->ac
.i32
,
3585 S_008F04_STRIDE(stride
), false), "");
3586 ring
= LLVMBuildInsertElement(ctx
->ac
.builder
, ring
, tmp
,
3589 ring
= LLVMBuildInsertElement(ctx
->ac
.builder
, ring
,
3590 LLVMConstInt(ctx
->ac
.i32
,
3591 num_records
, false),
3592 LLVMConstInt(ctx
->ac
.i32
, 2, false), "");
3594 ctx
->gsvs_ring
[stream
] = ring
;
3598 if (ctx
->stage
== MESA_SHADER_TESS_CTRL
||
3599 ctx
->stage
== MESA_SHADER_TESS_EVAL
) {
3600 ctx
->hs_ring_tess_offchip
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_HS_TESS_OFFCHIP
, false));
3601 ctx
->hs_ring_tess_factor
= ac_build_load_to_sgpr(&ctx
->ac
, ctx
->ring_offsets
, LLVMConstInt(ctx
->ac
.i32
, RING_HS_TESS_FACTOR
, false));
3606 radv_nir_get_max_workgroup_size(enum chip_class chip_class
,
3607 const struct nir_shader
*nir
)
3609 switch (nir
->info
.stage
) {
3610 case MESA_SHADER_TESS_CTRL
:
3611 return chip_class
>= GFX7
? 128 : 64;
3612 case MESA_SHADER_GEOMETRY
:
3613 return chip_class
>= GFX9
? 128 : 64;
3614 case MESA_SHADER_COMPUTE
:
3620 unsigned max_workgroup_size
= nir
->info
.cs
.local_size
[0] *
3621 nir
->info
.cs
.local_size
[1] *
3622 nir
->info
.cs
.local_size
[2];
3623 return max_workgroup_size
;
3626 /* Fixup the HW not emitting the TCS regs if there are no HS threads. */
3627 static void ac_nir_fixup_ls_hs_input_vgprs(struct radv_shader_context
*ctx
)
3629 LLVMValueRef count
= ac_unpack_param(&ctx
->ac
, ctx
->merged_wave_info
, 8, 8);
3630 LLVMValueRef hs_empty
= LLVMBuildICmp(ctx
->ac
.builder
, LLVMIntEQ
, count
,
3632 ctx
->abi
.instance_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->rel_auto_id
, ctx
->abi
.instance_id
, "");
3633 ctx
->rel_auto_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->abi
.tcs_rel_ids
, ctx
->rel_auto_id
, "");
3634 ctx
->abi
.vertex_id
= LLVMBuildSelect(ctx
->ac
.builder
, hs_empty
, ctx
->abi
.tcs_patch_id
, ctx
->abi
.vertex_id
, "");
3637 static void prepare_gs_input_vgprs(struct radv_shader_context
*ctx
)
3639 for(int i
= 5; i
>= 0; --i
) {
3640 ctx
->gs_vtx_offset
[i
] = ac_unpack_param(&ctx
->ac
, ctx
->gs_vtx_offset
[i
& ~1],
3644 ctx
->gs_wave_id
= ac_unpack_param(&ctx
->ac
, ctx
->merged_wave_info
, 16, 8);
3649 LLVMModuleRef
ac_translate_nir_to_llvm(struct ac_llvm_compiler
*ac_llvm
,
3650 struct nir_shader
*const *shaders
,
3652 struct radv_shader_variant_info
*shader_info
,
3653 const struct radv_nir_compiler_options
*options
)
3655 struct radv_shader_context ctx
= {0};
3657 ctx
.options
= options
;
3658 ctx
.shader_info
= shader_info
;
3660 ac_llvm_context_init(&ctx
.ac
, options
->chip_class
, options
->family
);
3661 ctx
.context
= ctx
.ac
.context
;
3662 ctx
.ac
.module
= ac_create_module(ac_llvm
->tm
, ctx
.context
);
3664 enum ac_float_mode float_mode
=
3665 options
->unsafe_math
? AC_FLOAT_MODE_UNSAFE_FP_MATH
:
3666 AC_FLOAT_MODE_DEFAULT
;
3668 ctx
.ac
.builder
= ac_create_builder(ctx
.context
, float_mode
);
3670 memset(shader_info
, 0, sizeof(*shader_info
));
3672 radv_nir_shader_info_init(&shader_info
->info
);
3674 for(int i
= 0; i
< shader_count
; ++i
)
3675 radv_nir_shader_info_pass(shaders
[i
], options
, &shader_info
->info
);
3677 for (i
= 0; i
< RADV_UD_MAX_SETS
; i
++)
3678 shader_info
->user_sgprs_locs
.descriptor_sets
[i
].sgpr_idx
= -1;
3679 for (i
= 0; i
< AC_UD_MAX_UD
; i
++)
3680 shader_info
->user_sgprs_locs
.shader_data
[i
].sgpr_idx
= -1;
3682 ctx
.max_workgroup_size
= 0;
3683 for (int i
= 0; i
< shader_count
; ++i
) {
3684 ctx
.max_workgroup_size
= MAX2(ctx
.max_workgroup_size
,
3685 radv_nir_get_max_workgroup_size(ctx
.options
->chip_class
,
3689 create_function(&ctx
, shaders
[shader_count
- 1]->info
.stage
, shader_count
>= 2,
3690 shader_count
>= 2 ? shaders
[shader_count
- 2]->info
.stage
: MESA_SHADER_VERTEX
);
3692 ctx
.abi
.inputs
= &ctx
.inputs
[0];
3693 ctx
.abi
.emit_outputs
= handle_shader_outputs_post
;
3694 ctx
.abi
.emit_vertex
= visit_emit_vertex
;
3695 ctx
.abi
.load_ubo
= radv_load_ubo
;
3696 ctx
.abi
.load_ssbo
= radv_load_ssbo
;
3697 ctx
.abi
.load_sampler_desc
= radv_get_sampler_desc
;
3698 ctx
.abi
.load_resource
= radv_load_resource
;
3699 ctx
.abi
.clamp_shadow_reference
= false;
3700 ctx
.abi
.gfx9_stride_size_workaround
= ctx
.ac
.chip_class
== GFX9
&& HAVE_LLVM
< 0x800;
3702 /* Because the new raw/struct atomic intrinsics are buggy with LLVM 8,
3703 * we fallback to the old intrinsics for atomic buffer image operations
3704 * and thus we need to apply the indexing workaround...
3706 ctx
.abi
.gfx9_stride_size_workaround_for_atomic
= ctx
.ac
.chip_class
== GFX9
&& HAVE_LLVM
< 0x900;
3708 if (shader_count
>= 2)
3709 ac_init_exec_full_mask(&ctx
.ac
);
3711 if ((ctx
.ac
.family
== CHIP_VEGA10
||
3712 ctx
.ac
.family
== CHIP_RAVEN
) &&
3713 shaders
[shader_count
- 1]->info
.stage
== MESA_SHADER_TESS_CTRL
)
3714 ac_nir_fixup_ls_hs_input_vgprs(&ctx
);
3716 for(int i
= 0; i
< shader_count
; ++i
) {
3717 ctx
.stage
= shaders
[i
]->info
.stage
;
3718 ctx
.output_mask
= 0;
3720 if (shaders
[i
]->info
.stage
== MESA_SHADER_GEOMETRY
) {
3721 for (int i
= 0; i
< 4; i
++) {
3722 ctx
.gs_next_vertex
[i
] =
3723 ac_build_alloca(&ctx
.ac
, ctx
.ac
.i32
, "");
3725 ctx
.gs_max_out_vertices
= shaders
[i
]->info
.gs
.vertices_out
;
3726 ctx
.abi
.load_inputs
= load_gs_input
;
3727 ctx
.abi
.emit_primitive
= visit_end_primitive
;
3728 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_TESS_CTRL
) {
3729 ctx
.tcs_outputs_read
= shaders
[i
]->info
.outputs_read
;
3730 ctx
.tcs_patch_outputs_read
= shaders
[i
]->info
.patch_outputs_read
;
3731 ctx
.abi
.load_tess_varyings
= load_tcs_varyings
;
3732 ctx
.abi
.load_patch_vertices_in
= load_patch_vertices_in
;
3733 ctx
.abi
.store_tcs_outputs
= store_tcs_output
;
3734 ctx
.tcs_vertices_per_patch
= shaders
[i
]->info
.tess
.tcs_vertices_out
;
3735 if (shader_count
== 1)
3736 ctx
.tcs_num_inputs
= ctx
.options
->key
.tcs
.num_inputs
;
3738 ctx
.tcs_num_inputs
= util_last_bit64(shader_info
->info
.vs
.ls_outputs_written
);
3739 ctx
.tcs_num_patches
= get_tcs_num_patches(&ctx
);
3740 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_TESS_EVAL
) {
3741 ctx
.tes_primitive_mode
= shaders
[i
]->info
.tess
.primitive_mode
;
3742 ctx
.abi
.load_tess_varyings
= load_tes_input
;
3743 ctx
.abi
.load_tess_coord
= load_tess_coord
;
3744 ctx
.abi
.load_patch_vertices_in
= load_patch_vertices_in
;
3745 ctx
.tcs_vertices_per_patch
= shaders
[i
]->info
.tess
.tcs_vertices_out
;
3746 ctx
.tcs_num_patches
= ctx
.options
->key
.tes
.num_patches
;
3747 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_VERTEX
) {
3748 ctx
.abi
.load_base_vertex
= radv_load_base_vertex
;
3749 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_FRAGMENT
) {
3750 shader_info
->fs
.can_discard
= shaders
[i
]->info
.fs
.uses_discard
;
3751 ctx
.abi
.lookup_interp_param
= lookup_interp_param
;
3752 ctx
.abi
.load_sample_position
= load_sample_position
;
3753 ctx
.abi
.load_sample_mask_in
= load_sample_mask_in
;
3754 ctx
.abi
.emit_kill
= radv_emit_kill
;
3758 ac_emit_barrier(&ctx
.ac
, ctx
.stage
);
3760 nir_foreach_variable(variable
, &shaders
[i
]->outputs
)
3761 scan_shader_output_decl(&ctx
, variable
, shaders
[i
], shaders
[i
]->info
.stage
);
3763 if (shaders
[i
]->info
.stage
== MESA_SHADER_GEOMETRY
) {
3764 unsigned addclip
= shaders
[i
]->info
.clip_distance_array_size
+
3765 shaders
[i
]->info
.cull_distance_array_size
> 4;
3766 ctx
.gsvs_vertex_size
= (util_bitcount64(ctx
.output_mask
) + addclip
) * 16;
3767 ctx
.max_gsvs_emit_size
= ctx
.gsvs_vertex_size
*
3768 shaders
[i
]->info
.gs
.vertices_out
;
3771 ac_setup_rings(&ctx
);
3773 LLVMBasicBlockRef merge_block
;
3774 if (shader_count
>= 2) {
3775 LLVMValueRef fn
= LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx
.ac
.builder
));
3776 LLVMBasicBlockRef then_block
= LLVMAppendBasicBlockInContext(ctx
.ac
.context
, fn
, "");
3777 merge_block
= LLVMAppendBasicBlockInContext(ctx
.ac
.context
, fn
, "");
3779 LLVMValueRef count
= ac_unpack_param(&ctx
.ac
, ctx
.merged_wave_info
, 8 * i
, 8);
3780 LLVMValueRef thread_id
= ac_get_thread_id(&ctx
.ac
);
3781 LLVMValueRef cond
= LLVMBuildICmp(ctx
.ac
.builder
, LLVMIntULT
,
3782 thread_id
, count
, "");
3783 LLVMBuildCondBr(ctx
.ac
.builder
, cond
, then_block
, merge_block
);
3785 LLVMPositionBuilderAtEnd(ctx
.ac
.builder
, then_block
);
3788 if (shaders
[i
]->info
.stage
== MESA_SHADER_FRAGMENT
)
3789 handle_fs_inputs(&ctx
, shaders
[i
]);
3790 else if(shaders
[i
]->info
.stage
== MESA_SHADER_VERTEX
)
3791 handle_vs_inputs(&ctx
, shaders
[i
]);
3792 else if(shader_count
>= 2 && shaders
[i
]->info
.stage
== MESA_SHADER_GEOMETRY
)
3793 prepare_gs_input_vgprs(&ctx
);
3795 ac_nir_translate(&ctx
.ac
, &ctx
.abi
, shaders
[i
]);
3797 if (shader_count
>= 2) {
3798 LLVMBuildBr(ctx
.ac
.builder
, merge_block
);
3799 LLVMPositionBuilderAtEnd(ctx
.ac
.builder
, merge_block
);
3802 if (shaders
[i
]->info
.stage
== MESA_SHADER_GEOMETRY
) {
3803 shader_info
->gs
.gsvs_vertex_size
= ctx
.gsvs_vertex_size
;
3804 shader_info
->gs
.max_gsvs_emit_size
= ctx
.max_gsvs_emit_size
;
3805 } else if (shaders
[i
]->info
.stage
== MESA_SHADER_TESS_CTRL
) {
3806 shader_info
->tcs
.num_patches
= ctx
.tcs_num_patches
;
3807 shader_info
->tcs
.lds_size
= calculate_tess_lds_size(&ctx
);
3811 LLVMBuildRetVoid(ctx
.ac
.builder
);
3813 if (options
->dump_preoptir
)
3814 ac_dump_module(ctx
.ac
.module
);
3816 ac_llvm_finalize_module(&ctx
, ac_llvm
->passmgr
, options
);
3818 if (shader_count
== 1)
3819 ac_nir_eliminate_const_vs_outputs(&ctx
);
3821 if (options
->dump_shader
) {
3822 ctx
.shader_info
->private_mem_vgprs
=
3823 ac_count_scratch_private_memory(ctx
.main_function
);
3826 return ctx
.ac
.module
;
3829 static void ac_diagnostic_handler(LLVMDiagnosticInfoRef di
, void *context
)
3831 unsigned *retval
= (unsigned *)context
;
3832 LLVMDiagnosticSeverity severity
= LLVMGetDiagInfoSeverity(di
);
3833 char *description
= LLVMGetDiagInfoDescription(di
);
3835 if (severity
== LLVMDSError
) {
3837 fprintf(stderr
, "LLVM triggered Diagnostic Handler: %s\n",
3841 LLVMDisposeMessage(description
);
3844 static unsigned radv_llvm_compile(LLVMModuleRef M
,
3845 char **pelf_buffer
, size_t *pelf_size
,
3846 struct ac_llvm_compiler
*ac_llvm
)
3848 unsigned retval
= 0;
3849 LLVMContextRef llvm_ctx
;
3851 /* Setup Diagnostic Handler*/
3852 llvm_ctx
= LLVMGetModuleContext(M
);
3854 LLVMContextSetDiagnosticHandler(llvm_ctx
, ac_diagnostic_handler
,
3858 if (!radv_compile_to_elf(ac_llvm
, M
, pelf_buffer
, pelf_size
))
3863 static void ac_compile_llvm_module(struct ac_llvm_compiler
*ac_llvm
,
3864 LLVMModuleRef llvm_module
,
3865 struct radv_shader_binary
**rbinary
,
3866 struct radv_shader_variant_info
*shader_info
,
3867 gl_shader_stage stage
,
3868 const struct radv_nir_compiler_options
*options
)
3870 char *elf_buffer
= NULL
;
3871 size_t elf_size
= 0;
3872 char *llvm_ir_string
= NULL
;
3873 if (options
->dump_shader
)
3874 ac_dump_module(llvm_module
);
3876 if (options
->record_llvm_ir
) {
3877 char *llvm_ir
= LLVMPrintModuleToString(llvm_module
);
3878 llvm_ir_string
= strdup(llvm_ir
);
3879 LLVMDisposeMessage(llvm_ir
);
3882 int v
= radv_llvm_compile(llvm_module
, &elf_buffer
, &elf_size
, ac_llvm
);
3884 fprintf(stderr
, "compile failed\n");
3887 LLVMContextRef ctx
= LLVMGetModuleContext(llvm_module
);
3888 LLVMDisposeModule(llvm_module
);
3889 LLVMContextDispose(ctx
);
3891 size_t llvm_ir_size
= llvm_ir_string
? strlen(llvm_ir_string
) : 0;
3892 size_t alloc_size
= sizeof(struct radv_shader_binary_rtld
) + elf_size
+ llvm_ir_size
+ 1;
3893 struct radv_shader_binary_rtld
*rbin
= calloc(1, alloc_size
);
3894 memcpy(rbin
->data
, elf_buffer
, elf_size
);
3896 memcpy(rbin
->data
+ elf_size
, llvm_ir_string
, llvm_ir_size
+ 1);
3898 rbin
->base
.type
= RADV_BINARY_TYPE_RTLD
;
3899 rbin
->base
.stage
= stage
;
3900 rbin
->base
.total_size
= alloc_size
;
3901 rbin
->elf_size
= elf_size
;
3902 rbin
->llvm_ir_size
= llvm_ir_size
;
3903 *rbinary
= &rbin
->base
;
3905 free(llvm_ir_string
);
3910 ac_fill_shader_info(struct radv_shader_variant_info
*shader_info
, struct nir_shader
*nir
, const struct radv_nir_compiler_options
*options
)
3912 switch (nir
->info
.stage
) {
3913 case MESA_SHADER_COMPUTE
:
3914 for (int i
= 0; i
< 3; ++i
)
3915 shader_info
->cs
.block_size
[i
] = nir
->info
.cs
.local_size
[i
];
3917 case MESA_SHADER_FRAGMENT
:
3918 shader_info
->fs
.early_fragment_test
= nir
->info
.fs
.early_fragment_tests
;
3920 case MESA_SHADER_GEOMETRY
:
3921 shader_info
->gs
.vertices_in
= nir
->info
.gs
.vertices_in
;
3922 shader_info
->gs
.vertices_out
= nir
->info
.gs
.vertices_out
;
3923 shader_info
->gs
.output_prim
= nir
->info
.gs
.output_primitive
;
3924 shader_info
->gs
.invocations
= nir
->info
.gs
.invocations
;
3926 case MESA_SHADER_TESS_EVAL
:
3927 shader_info
->tes
.primitive_mode
= nir
->info
.tess
.primitive_mode
;
3928 shader_info
->tes
.spacing
= nir
->info
.tess
.spacing
;
3929 shader_info
->tes
.ccw
= nir
->info
.tess
.ccw
;
3930 shader_info
->tes
.point_mode
= nir
->info
.tess
.point_mode
;
3931 shader_info
->tes
.as_es
= options
->key
.tes
.as_es
;
3932 shader_info
->tes
.export_prim_id
= options
->key
.tes
.export_prim_id
;
3934 case MESA_SHADER_TESS_CTRL
:
3935 shader_info
->tcs
.tcs_vertices_out
= nir
->info
.tess
.tcs_vertices_out
;
3937 case MESA_SHADER_VERTEX
:
3938 shader_info
->vs
.as_es
= options
->key
.vs
.as_es
;
3939 shader_info
->vs
.as_ls
= options
->key
.vs
.as_ls
;
3940 shader_info
->vs
.export_prim_id
= options
->key
.vs
.export_prim_id
;
3948 radv_compile_nir_shader(struct ac_llvm_compiler
*ac_llvm
,
3949 struct radv_shader_binary
**rbinary
,
3950 struct radv_shader_variant_info
*shader_info
,
3951 struct nir_shader
*const *nir
,
3953 const struct radv_nir_compiler_options
*options
)
3956 LLVMModuleRef llvm_module
;
3958 llvm_module
= ac_translate_nir_to_llvm(ac_llvm
, nir
, nir_count
, shader_info
,
3961 ac_compile_llvm_module(ac_llvm
, llvm_module
, rbinary
, shader_info
,
3962 nir
[nir_count
- 1]->info
.stage
, options
);
3964 for (int i
= 0; i
< nir_count
; ++i
)
3965 ac_fill_shader_info(shader_info
, nir
[i
], options
);
3967 /* Determine the ES type (VS or TES) for the GS on GFX9. */
3968 if (options
->chip_class
== GFX9
) {
3969 if (nir_count
== 2 &&
3970 nir
[1]->info
.stage
== MESA_SHADER_GEOMETRY
) {
3971 shader_info
->gs
.es_type
= nir
[0]->info
.stage
;
3977 ac_gs_copy_shader_emit(struct radv_shader_context
*ctx
)
3979 LLVMValueRef vtx_offset
=
3980 LLVMBuildMul(ctx
->ac
.builder
, ctx
->abi
.vertex_id
,
3981 LLVMConstInt(ctx
->ac
.i32
, 4, false), "");
3982 LLVMValueRef stream_id
;
3984 /* Fetch the vertex stream ID. */
3985 if (ctx
->shader_info
->info
.so
.num_outputs
) {
3987 ac_unpack_param(&ctx
->ac
, ctx
->streamout_config
, 24, 2);
3989 stream_id
= ctx
->ac
.i32_0
;
3992 LLVMBasicBlockRef end_bb
;
3993 LLVMValueRef switch_inst
;
3995 end_bb
= LLVMAppendBasicBlockInContext(ctx
->ac
.context
,
3996 ctx
->main_function
, "end");
3997 switch_inst
= LLVMBuildSwitch(ctx
->ac
.builder
, stream_id
, end_bb
, 4);
3999 for (unsigned stream
= 0; stream
< 4; stream
++) {
4000 unsigned num_components
=
4001 ctx
->shader_info
->info
.gs
.num_stream_output_components
[stream
];
4002 LLVMBasicBlockRef bb
;
4005 if (!num_components
)
4008 if (stream
> 0 && !ctx
->shader_info
->info
.so
.num_outputs
)
4011 bb
= LLVMInsertBasicBlockInContext(ctx
->ac
.context
, end_bb
, "out");
4012 LLVMAddCase(switch_inst
, LLVMConstInt(ctx
->ac
.i32
, stream
, 0), bb
);
4013 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, bb
);
4016 for (unsigned i
= 0; i
< AC_LLVM_MAX_OUTPUTS
; ++i
) {
4017 unsigned output_usage_mask
=
4018 ctx
->shader_info
->info
.gs
.output_usage_mask
[i
];
4019 unsigned output_stream
=
4020 ctx
->shader_info
->info
.gs
.output_streams
[i
];
4021 int length
= util_last_bit(output_usage_mask
);
4023 if (!(ctx
->output_mask
& (1ull << i
)) ||
4024 output_stream
!= stream
)
4027 for (unsigned j
= 0; j
< length
; j
++) {
4028 LLVMValueRef value
, soffset
;
4030 if (!(output_usage_mask
& (1 << j
)))
4033 soffset
= LLVMConstInt(ctx
->ac
.i32
,
4035 ctx
->gs_max_out_vertices
* 16 * 4, false);
4039 value
= ac_build_buffer_load(&ctx
->ac
,
4042 vtx_offset
, soffset
,
4043 0, ac_glc
| ac_slc
, true, false);
4045 LLVMTypeRef type
= LLVMGetAllocatedType(ctx
->abi
.outputs
[ac_llvm_reg_index_soa(i
, j
)]);
4046 if (ac_get_type_size(type
) == 2) {
4047 value
= LLVMBuildBitCast(ctx
->ac
.builder
, value
, ctx
->ac
.i32
, "");
4048 value
= LLVMBuildTrunc(ctx
->ac
.builder
, value
, ctx
->ac
.i16
, "");
4051 LLVMBuildStore(ctx
->ac
.builder
,
4052 ac_to_float(&ctx
->ac
, value
), ctx
->abi
.outputs
[ac_llvm_reg_index_soa(i
, j
)]);
4056 if (ctx
->shader_info
->info
.so
.num_outputs
)
4057 radv_emit_streamout(ctx
, stream
);
4060 handle_vs_outputs_post(ctx
, false, false, true,
4061 &ctx
->shader_info
->vs
.outinfo
);
4064 LLVMBuildBr(ctx
->ac
.builder
, end_bb
);
4067 LLVMPositionBuilderAtEnd(ctx
->ac
.builder
, end_bb
);
4071 radv_compile_gs_copy_shader(struct ac_llvm_compiler
*ac_llvm
,
4072 struct nir_shader
*geom_shader
,
4073 struct radv_shader_binary
**rbinary
,
4074 struct radv_shader_variant_info
*shader_info
,
4075 const struct radv_nir_compiler_options
*options
)
4077 struct radv_shader_context ctx
= {0};
4078 ctx
.options
= options
;
4079 ctx
.shader_info
= shader_info
;
4081 ac_llvm_context_init(&ctx
.ac
, options
->chip_class
, options
->family
);
4082 ctx
.context
= ctx
.ac
.context
;
4083 ctx
.ac
.module
= ac_create_module(ac_llvm
->tm
, ctx
.context
);
4085 ctx
.is_gs_copy_shader
= true;
4087 enum ac_float_mode float_mode
=
4088 options
->unsafe_math
? AC_FLOAT_MODE_UNSAFE_FP_MATH
:
4089 AC_FLOAT_MODE_DEFAULT
;
4091 ctx
.ac
.builder
= ac_create_builder(ctx
.context
, float_mode
);
4092 ctx
.stage
= MESA_SHADER_VERTEX
;
4094 radv_nir_shader_info_pass(geom_shader
, options
, &shader_info
->info
);
4096 create_function(&ctx
, MESA_SHADER_VERTEX
, false, MESA_SHADER_VERTEX
);
4098 ctx
.gs_max_out_vertices
= geom_shader
->info
.gs
.vertices_out
;
4099 ac_setup_rings(&ctx
);
4101 nir_foreach_variable(variable
, &geom_shader
->outputs
) {
4102 scan_shader_output_decl(&ctx
, variable
, geom_shader
, MESA_SHADER_VERTEX
);
4103 ac_handle_shader_output_decl(&ctx
.ac
, &ctx
.abi
, geom_shader
,
4104 variable
, MESA_SHADER_VERTEX
);
4107 ac_gs_copy_shader_emit(&ctx
);
4109 LLVMBuildRetVoid(ctx
.ac
.builder
);
4111 ac_llvm_finalize_module(&ctx
, ac_llvm
->passmgr
, options
);
4113 ac_compile_llvm_module(ac_llvm
, ctx
.ac
.module
, rbinary
, shader_info
,
4114 MESA_SHADER_VERTEX
, options
);
4115 (*rbinary
)->is_gs_copy_shader
= true;