Revert "radv: keep track of whether NGG is used for GS on GFX10"
[mesa.git] / src / amd / vulkan / radv_nir_to_llvm.c
1 /*
2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
4 *
5 * based in part on anv driver which is:
6 * Copyright © 2015 Intel Corporation
7 *
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:
14 *
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
17 * Software.
18 *
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
25 * IN THE SOFTWARE.
26 */
27
28 #include "radv_private.h"
29 #include "radv_shader.h"
30 #include "radv_shader_helper.h"
31 #include "nir/nir.h"
32
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>
37
38 #include "sid.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"
45
46 #define RADEON_LLVM_MAX_INPUTS (VARYING_SLOT_VAR31 + 1)
47
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;
53
54 unsigned max_workgroup_size;
55 LLVMContextRef context;
56 LLVMValueRef main_function;
57
58 LLVMValueRef descriptor_sets[RADV_UD_MAX_SETS];
59 LLVMValueRef ring_offsets;
60
61 LLVMValueRef vertex_buffers;
62 LLVMValueRef rel_auto_id;
63 LLVMValueRef vs_prim_id;
64 LLVMValueRef es2gs_offset;
65
66 LLVMValueRef oc_lds;
67 LLVMValueRef merged_wave_info;
68 LLVMValueRef tess_factor_offset;
69 LLVMValueRef tes_rel_patch_id;
70 LLVMValueRef tes_u;
71 LLVMValueRef tes_v;
72
73 /* HW GS */
74 /* On gfx10:
75 * - bits 0..10: ordered_wave_id
76 * - bits 12..20: number of vertices in group
77 * - bits 22..30: number of primitives in group
78 */
79 LLVMValueRef gs_tg_info;
80 LLVMValueRef gs2vs_offset;
81 LLVMValueRef gs_wave_id;
82 LLVMValueRef gs_vtx_offset[6];
83
84 LLVMValueRef esgs_ring;
85 LLVMValueRef gsvs_ring[4];
86 LLVMValueRef hs_ring_tess_offchip;
87 LLVMValueRef hs_ring_tess_factor;
88
89 LLVMValueRef persp_sample, persp_center, persp_centroid;
90 LLVMValueRef linear_sample, linear_center, linear_centroid;
91
92 /* Streamout */
93 LLVMValueRef streamout_buffers;
94 LLVMValueRef streamout_write_idx;
95 LLVMValueRef streamout_config;
96 LLVMValueRef streamout_offset[4];
97
98 gl_shader_stage stage;
99
100 LLVMValueRef inputs[RADEON_LLVM_MAX_INPUTS * 4];
101 uint64_t float16_shaded_mask;
102
103 uint64_t input_mask;
104 uint64_t output_mask;
105
106 bool is_gs_copy_shader;
107 LLVMValueRef gs_next_vertex[4];
108 unsigned gs_max_out_vertices;
109
110 unsigned tes_primitive_mode;
111
112 uint32_t tcs_patch_outputs_read;
113 uint64_t tcs_outputs_read;
114 uint32_t tcs_vertices_per_patch;
115 uint32_t tcs_num_inputs;
116 uint32_t tcs_num_patches;
117 uint32_t max_gsvs_emit_size;
118 uint32_t gsvs_vertex_size;
119 };
120
121 enum radeon_llvm_calling_convention {
122 RADEON_LLVM_AMDGPU_VS = 87,
123 RADEON_LLVM_AMDGPU_GS = 88,
124 RADEON_LLVM_AMDGPU_PS = 89,
125 RADEON_LLVM_AMDGPU_CS = 90,
126 RADEON_LLVM_AMDGPU_HS = 93,
127 };
128
129 static inline struct radv_shader_context *
130 radv_shader_context_from_abi(struct ac_shader_abi *abi)
131 {
132 struct radv_shader_context *ctx = NULL;
133 return container_of(abi, ctx, abi);
134 }
135
136 struct ac_build_if_state
137 {
138 struct radv_shader_context *ctx;
139 LLVMValueRef condition;
140 LLVMBasicBlockRef entry_block;
141 LLVMBasicBlockRef true_block;
142 LLVMBasicBlockRef false_block;
143 LLVMBasicBlockRef merge_block;
144 };
145
146 static LLVMBasicBlockRef
147 ac_build_insert_new_block(struct radv_shader_context *ctx, const char *name)
148 {
149 LLVMBasicBlockRef current_block;
150 LLVMBasicBlockRef next_block;
151 LLVMBasicBlockRef new_block;
152
153 /* get current basic block */
154 current_block = LLVMGetInsertBlock(ctx->ac.builder);
155
156 /* chqeck if there's another block after this one */
157 next_block = LLVMGetNextBasicBlock(current_block);
158 if (next_block) {
159 /* insert the new block before the next block */
160 new_block = LLVMInsertBasicBlockInContext(ctx->context, next_block, name);
161 }
162 else {
163 /* append new block after current block */
164 LLVMValueRef function = LLVMGetBasicBlockParent(current_block);
165 new_block = LLVMAppendBasicBlockInContext(ctx->context, function, name);
166 }
167 return new_block;
168 }
169
170 static void
171 ac_nir_build_if(struct ac_build_if_state *ifthen,
172 struct radv_shader_context *ctx,
173 LLVMValueRef condition)
174 {
175 LLVMBasicBlockRef block = LLVMGetInsertBlock(ctx->ac.builder);
176
177 memset(ifthen, 0, sizeof *ifthen);
178 ifthen->ctx = ctx;
179 ifthen->condition = condition;
180 ifthen->entry_block = block;
181
182 /* create endif/merge basic block for the phi functions */
183 ifthen->merge_block = ac_build_insert_new_block(ctx, "endif-block");
184
185 /* create/insert true_block before merge_block */
186 ifthen->true_block =
187 LLVMInsertBasicBlockInContext(ctx->context,
188 ifthen->merge_block,
189 "if-true-block");
190
191 /* successive code goes into the true block */
192 LLVMPositionBuilderAtEnd(ctx->ac.builder, ifthen->true_block);
193 }
194
195 /**
196 * End a conditional.
197 */
198 static void
199 ac_nir_build_endif(struct ac_build_if_state *ifthen)
200 {
201 LLVMBuilderRef builder = ifthen->ctx->ac.builder;
202
203 /* Insert branch to the merge block from current block */
204 LLVMBuildBr(builder, ifthen->merge_block);
205
206 /*
207 * Now patch in the various branch instructions.
208 */
209
210 /* Insert the conditional branch instruction at the end of entry_block */
211 LLVMPositionBuilderAtEnd(builder, ifthen->entry_block);
212 if (ifthen->false_block) {
213 /* we have an else clause */
214 LLVMBuildCondBr(builder, ifthen->condition,
215 ifthen->true_block, ifthen->false_block);
216 }
217 else {
218 /* no else clause */
219 LLVMBuildCondBr(builder, ifthen->condition,
220 ifthen->true_block, ifthen->merge_block);
221 }
222
223 /* Resume building code at end of the ifthen->merge_block */
224 LLVMPositionBuilderAtEnd(builder, ifthen->merge_block);
225 }
226
227
228 static LLVMValueRef get_rel_patch_id(struct radv_shader_context *ctx)
229 {
230 switch (ctx->stage) {
231 case MESA_SHADER_TESS_CTRL:
232 return ac_unpack_param(&ctx->ac, ctx->abi.tcs_rel_ids, 0, 8);
233 case MESA_SHADER_TESS_EVAL:
234 return ctx->tes_rel_patch_id;
235 break;
236 default:
237 unreachable("Illegal stage");
238 }
239 }
240
241 static unsigned
242 get_tcs_num_patches(struct radv_shader_context *ctx)
243 {
244 unsigned num_tcs_input_cp = ctx->options->key.tcs.input_vertices;
245 unsigned num_tcs_output_cp = ctx->tcs_vertices_per_patch;
246 uint32_t input_vertex_size = ctx->tcs_num_inputs * 16;
247 uint32_t input_patch_size = ctx->options->key.tcs.input_vertices * input_vertex_size;
248 uint32_t num_tcs_outputs = util_last_bit64(ctx->shader_info->info.tcs.outputs_written);
249 uint32_t num_tcs_patch_outputs = util_last_bit64(ctx->shader_info->info.tcs.patch_outputs_written);
250 uint32_t output_vertex_size = num_tcs_outputs * 16;
251 uint32_t pervertex_output_patch_size = ctx->tcs_vertices_per_patch * output_vertex_size;
252 uint32_t output_patch_size = pervertex_output_patch_size + num_tcs_patch_outputs * 16;
253 unsigned num_patches;
254 unsigned hardware_lds_size;
255
256 /* Ensure that we only need one wave per SIMD so we don't need to check
257 * resource usage. Also ensures that the number of tcs in and out
258 * vertices per threadgroup are at most 256.
259 */
260 num_patches = 64 / MAX2(num_tcs_input_cp, num_tcs_output_cp) * 4;
261 /* Make sure that the data fits in LDS. This assumes the shaders only
262 * use LDS for the inputs and outputs.
263 */
264 hardware_lds_size = 32768;
265
266 /* Looks like STONEY hangs if we use more than 32 KiB LDS in a single
267 * threadgroup, even though there is more than 32 KiB LDS.
268 *
269 * Test: dEQP-VK.tessellation.shader_input_output.barrier
270 */
271 if (ctx->options->chip_class >= GFX7 && ctx->options->family != CHIP_STONEY)
272 hardware_lds_size = 65536;
273
274 num_patches = MIN2(num_patches, hardware_lds_size / (input_patch_size + output_patch_size));
275 /* Make sure the output data fits in the offchip buffer */
276 num_patches = MIN2(num_patches, (ctx->options->tess_offchip_block_dw_size * 4) / output_patch_size);
277 /* Not necessary for correctness, but improves performance. The
278 * specific value is taken from the proprietary driver.
279 */
280 num_patches = MIN2(num_patches, 40);
281
282 /* GFX6 bug workaround - limit LS-HS threadgroups to only one wave. */
283 if (ctx->options->chip_class == GFX6) {
284 unsigned one_wave = 64 / MAX2(num_tcs_input_cp, num_tcs_output_cp);
285 num_patches = MIN2(num_patches, one_wave);
286 }
287 return num_patches;
288 }
289
290 static unsigned
291 calculate_tess_lds_size(struct radv_shader_context *ctx)
292 {
293 unsigned num_tcs_input_cp = ctx->options->key.tcs.input_vertices;
294 unsigned num_tcs_output_cp;
295 unsigned num_tcs_outputs, num_tcs_patch_outputs;
296 unsigned input_vertex_size, output_vertex_size;
297 unsigned input_patch_size, output_patch_size;
298 unsigned pervertex_output_patch_size;
299 unsigned output_patch0_offset;
300 unsigned num_patches;
301 unsigned lds_size;
302
303 num_tcs_output_cp = ctx->tcs_vertices_per_patch;
304 num_tcs_outputs = util_last_bit64(ctx->shader_info->info.tcs.outputs_written);
305 num_tcs_patch_outputs = util_last_bit64(ctx->shader_info->info.tcs.patch_outputs_written);
306
307 input_vertex_size = ctx->tcs_num_inputs * 16;
308 output_vertex_size = num_tcs_outputs * 16;
309
310 input_patch_size = num_tcs_input_cp * input_vertex_size;
311
312 pervertex_output_patch_size = num_tcs_output_cp * output_vertex_size;
313 output_patch_size = pervertex_output_patch_size + num_tcs_patch_outputs * 16;
314
315 num_patches = ctx->tcs_num_patches;
316 output_patch0_offset = input_patch_size * num_patches;
317
318 lds_size = output_patch0_offset + output_patch_size * num_patches;
319 return lds_size;
320 }
321
322 /* Tessellation shaders pass outputs to the next shader using LDS.
323 *
324 * LS outputs = TCS inputs
325 * TCS outputs = TES inputs
326 *
327 * The LDS layout is:
328 * - TCS inputs for patch 0
329 * - TCS inputs for patch 1
330 * - TCS inputs for patch 2 = get_tcs_in_current_patch_offset (if RelPatchID==2)
331 * - ...
332 * - TCS outputs for patch 0 = get_tcs_out_patch0_offset
333 * - Per-patch TCS outputs for patch 0 = get_tcs_out_patch0_patch_data_offset
334 * - TCS outputs for patch 1
335 * - Per-patch TCS outputs for patch 1
336 * - TCS outputs for patch 2 = get_tcs_out_current_patch_offset (if RelPatchID==2)
337 * - Per-patch TCS outputs for patch 2 = get_tcs_out_current_patch_data_offset (if RelPatchID==2)
338 * - ...
339 *
340 * All three shaders VS(LS), TCS, TES share the same LDS space.
341 */
342 static LLVMValueRef
343 get_tcs_in_patch_stride(struct radv_shader_context *ctx)
344 {
345 assert (ctx->stage == MESA_SHADER_TESS_CTRL);
346 uint32_t input_vertex_size = ctx->tcs_num_inputs * 16;
347 uint32_t input_patch_size = ctx->options->key.tcs.input_vertices * input_vertex_size;
348
349 input_patch_size /= 4;
350 return LLVMConstInt(ctx->ac.i32, input_patch_size, false);
351 }
352
353 static LLVMValueRef
354 get_tcs_out_patch_stride(struct radv_shader_context *ctx)
355 {
356 uint32_t num_tcs_outputs = util_last_bit64(ctx->shader_info->info.tcs.outputs_written);
357 uint32_t num_tcs_patch_outputs = util_last_bit64(ctx->shader_info->info.tcs.patch_outputs_written);
358 uint32_t output_vertex_size = num_tcs_outputs * 16;
359 uint32_t pervertex_output_patch_size = ctx->tcs_vertices_per_patch * output_vertex_size;
360 uint32_t output_patch_size = pervertex_output_patch_size + num_tcs_patch_outputs * 16;
361 output_patch_size /= 4;
362 return LLVMConstInt(ctx->ac.i32, output_patch_size, false);
363 }
364
365 static LLVMValueRef
366 get_tcs_out_vertex_stride(struct radv_shader_context *ctx)
367 {
368 uint32_t num_tcs_outputs = util_last_bit64(ctx->shader_info->info.tcs.outputs_written);
369 uint32_t output_vertex_size = num_tcs_outputs * 16;
370 output_vertex_size /= 4;
371 return LLVMConstInt(ctx->ac.i32, output_vertex_size, false);
372 }
373
374 static LLVMValueRef
375 get_tcs_out_patch0_offset(struct radv_shader_context *ctx)
376 {
377 assert (ctx->stage == MESA_SHADER_TESS_CTRL);
378 uint32_t input_vertex_size = ctx->tcs_num_inputs * 16;
379 uint32_t input_patch_size = ctx->options->key.tcs.input_vertices * input_vertex_size;
380 uint32_t output_patch0_offset = input_patch_size;
381 unsigned num_patches = ctx->tcs_num_patches;
382
383 output_patch0_offset *= num_patches;
384 output_patch0_offset /= 4;
385 return LLVMConstInt(ctx->ac.i32, output_patch0_offset, false);
386 }
387
388 static LLVMValueRef
389 get_tcs_out_patch0_patch_data_offset(struct radv_shader_context *ctx)
390 {
391 assert (ctx->stage == MESA_SHADER_TESS_CTRL);
392 uint32_t input_vertex_size = ctx->tcs_num_inputs * 16;
393 uint32_t input_patch_size = ctx->options->key.tcs.input_vertices * input_vertex_size;
394 uint32_t output_patch0_offset = input_patch_size;
395
396 uint32_t num_tcs_outputs = util_last_bit64(ctx->shader_info->info.tcs.outputs_written);
397 uint32_t output_vertex_size = num_tcs_outputs * 16;
398 uint32_t pervertex_output_patch_size = ctx->tcs_vertices_per_patch * output_vertex_size;
399 unsigned num_patches = ctx->tcs_num_patches;
400
401 output_patch0_offset *= num_patches;
402 output_patch0_offset += pervertex_output_patch_size;
403 output_patch0_offset /= 4;
404 return LLVMConstInt(ctx->ac.i32, output_patch0_offset, false);
405 }
406
407 static LLVMValueRef
408 get_tcs_in_current_patch_offset(struct radv_shader_context *ctx)
409 {
410 LLVMValueRef patch_stride = get_tcs_in_patch_stride(ctx);
411 LLVMValueRef rel_patch_id = get_rel_patch_id(ctx);
412
413 return LLVMBuildMul(ctx->ac.builder, patch_stride, rel_patch_id, "");
414 }
415
416 static LLVMValueRef
417 get_tcs_out_current_patch_offset(struct radv_shader_context *ctx)
418 {
419 LLVMValueRef patch0_offset = get_tcs_out_patch0_offset(ctx);
420 LLVMValueRef patch_stride = get_tcs_out_patch_stride(ctx);
421 LLVMValueRef rel_patch_id = get_rel_patch_id(ctx);
422
423 return ac_build_imad(&ctx->ac, patch_stride, rel_patch_id,
424 patch0_offset);
425 }
426
427 static LLVMValueRef
428 get_tcs_out_current_patch_data_offset(struct radv_shader_context *ctx)
429 {
430 LLVMValueRef patch0_patch_data_offset =
431 get_tcs_out_patch0_patch_data_offset(ctx);
432 LLVMValueRef patch_stride = get_tcs_out_patch_stride(ctx);
433 LLVMValueRef rel_patch_id = get_rel_patch_id(ctx);
434
435 return ac_build_imad(&ctx->ac, patch_stride, rel_patch_id,
436 patch0_patch_data_offset);
437 }
438
439 #define MAX_ARGS 64
440 struct arg_info {
441 LLVMTypeRef types[MAX_ARGS];
442 LLVMValueRef *assign[MAX_ARGS];
443 uint8_t count;
444 uint8_t sgpr_count;
445 uint8_t num_sgprs_used;
446 uint8_t num_vgprs_used;
447 };
448
449 enum ac_arg_regfile {
450 ARG_SGPR,
451 ARG_VGPR,
452 };
453
454 static void
455 add_arg(struct arg_info *info, enum ac_arg_regfile regfile, LLVMTypeRef type,
456 LLVMValueRef *param_ptr)
457 {
458 assert(info->count < MAX_ARGS);
459
460 info->assign[info->count] = param_ptr;
461 info->types[info->count] = type;
462 info->count++;
463
464 if (regfile == ARG_SGPR) {
465 info->num_sgprs_used += ac_get_type_size(type) / 4;
466 info->sgpr_count++;
467 } else {
468 assert(regfile == ARG_VGPR);
469 info->num_vgprs_used += ac_get_type_size(type) / 4;
470 }
471 }
472
473 static void assign_arguments(LLVMValueRef main_function,
474 struct arg_info *info)
475 {
476 unsigned i;
477 for (i = 0; i < info->count; i++) {
478 if (info->assign[i])
479 *info->assign[i] = LLVMGetParam(main_function, i);
480 }
481 }
482
483 static LLVMValueRef
484 create_llvm_function(LLVMContextRef ctx, LLVMModuleRef module,
485 LLVMBuilderRef builder, LLVMTypeRef *return_types,
486 unsigned num_return_elems,
487 struct arg_info *args,
488 unsigned max_workgroup_size,
489 const struct radv_nir_compiler_options *options)
490 {
491 LLVMTypeRef main_function_type, ret_type;
492 LLVMBasicBlockRef main_function_body;
493
494 if (num_return_elems)
495 ret_type = LLVMStructTypeInContext(ctx, return_types,
496 num_return_elems, true);
497 else
498 ret_type = LLVMVoidTypeInContext(ctx);
499
500 /* Setup the function */
501 main_function_type =
502 LLVMFunctionType(ret_type, args->types, args->count, 0);
503 LLVMValueRef main_function =
504 LLVMAddFunction(module, "main", main_function_type);
505 main_function_body =
506 LLVMAppendBasicBlockInContext(ctx, main_function, "main_body");
507 LLVMPositionBuilderAtEnd(builder, main_function_body);
508
509 LLVMSetFunctionCallConv(main_function, RADEON_LLVM_AMDGPU_CS);
510 for (unsigned i = 0; i < args->sgpr_count; ++i) {
511 LLVMValueRef P = LLVMGetParam(main_function, i);
512
513 ac_add_function_attr(ctx, main_function, i + 1, AC_FUNC_ATTR_INREG);
514
515 if (LLVMGetTypeKind(LLVMTypeOf(P)) == LLVMPointerTypeKind) {
516 ac_add_function_attr(ctx, main_function, i + 1, AC_FUNC_ATTR_NOALIAS);
517 ac_add_attr_dereferenceable(P, UINT64_MAX);
518 }
519 }
520
521 if (options->address32_hi) {
522 ac_llvm_add_target_dep_function_attr(main_function,
523 "amdgpu-32bit-address-high-bits",
524 options->address32_hi);
525 }
526
527 ac_llvm_set_workgroup_size(main_function, max_workgroup_size);
528
529 if (options->unsafe_math) {
530 /* These were copied from some LLVM test. */
531 LLVMAddTargetDependentFunctionAttr(main_function,
532 "less-precise-fpmad",
533 "true");
534 LLVMAddTargetDependentFunctionAttr(main_function,
535 "no-infs-fp-math",
536 "true");
537 LLVMAddTargetDependentFunctionAttr(main_function,
538 "no-nans-fp-math",
539 "true");
540 LLVMAddTargetDependentFunctionAttr(main_function,
541 "unsafe-fp-math",
542 "true");
543 LLVMAddTargetDependentFunctionAttr(main_function,
544 "no-signed-zeros-fp-math",
545 "true");
546 }
547 return main_function;
548 }
549
550
551 static void
552 set_loc(struct radv_userdata_info *ud_info, uint8_t *sgpr_idx,
553 uint8_t num_sgprs)
554 {
555 ud_info->sgpr_idx = *sgpr_idx;
556 ud_info->num_sgprs = num_sgprs;
557 *sgpr_idx += num_sgprs;
558 }
559
560 static void
561 set_loc_shader(struct radv_shader_context *ctx, int idx, uint8_t *sgpr_idx,
562 uint8_t num_sgprs)
563 {
564 struct radv_userdata_info *ud_info =
565 &ctx->shader_info->user_sgprs_locs.shader_data[idx];
566 assert(ud_info);
567
568 set_loc(ud_info, sgpr_idx, num_sgprs);
569 }
570
571 static void
572 set_loc_shader_ptr(struct radv_shader_context *ctx, int idx, uint8_t *sgpr_idx)
573 {
574 bool use_32bit_pointers = idx != AC_UD_SCRATCH_RING_OFFSETS;
575
576 set_loc_shader(ctx, idx, sgpr_idx, use_32bit_pointers ? 1 : 2);
577 }
578
579 static void
580 set_loc_desc(struct radv_shader_context *ctx, int idx, uint8_t *sgpr_idx)
581 {
582 struct radv_userdata_locations *locs =
583 &ctx->shader_info->user_sgprs_locs;
584 struct radv_userdata_info *ud_info = &locs->descriptor_sets[idx];
585 assert(ud_info);
586
587 set_loc(ud_info, sgpr_idx, 1);
588
589 locs->descriptor_sets_enabled |= 1 << idx;
590 }
591
592 struct user_sgpr_info {
593 bool need_ring_offsets;
594 bool indirect_all_descriptor_sets;
595 uint8_t remaining_sgprs;
596 };
597
598 static bool needs_view_index_sgpr(struct radv_shader_context *ctx,
599 gl_shader_stage stage)
600 {
601 switch (stage) {
602 case MESA_SHADER_VERTEX:
603 if (ctx->shader_info->info.needs_multiview_view_index ||
604 (!ctx->options->key.vs.out.as_es && !ctx->options->key.vs.out.as_ls && ctx->options->key.has_multiview_view_index))
605 return true;
606 break;
607 case MESA_SHADER_TESS_EVAL:
608 if (ctx->shader_info->info.needs_multiview_view_index || (!ctx->options->key.tes.out.as_es && ctx->options->key.has_multiview_view_index))
609 return true;
610 break;
611 case MESA_SHADER_GEOMETRY:
612 case MESA_SHADER_TESS_CTRL:
613 if (ctx->shader_info->info.needs_multiview_view_index)
614 return true;
615 break;
616 default:
617 break;
618 }
619 return false;
620 }
621
622 static uint8_t
623 count_vs_user_sgprs(struct radv_shader_context *ctx)
624 {
625 uint8_t count = 0;
626
627 if (ctx->shader_info->info.vs.has_vertex_buffers)
628 count++;
629 count += ctx->shader_info->info.vs.needs_draw_id ? 3 : 2;
630
631 return count;
632 }
633
634 static void allocate_inline_push_consts(struct radv_shader_context *ctx,
635 struct user_sgpr_info *user_sgpr_info)
636 {
637 uint8_t remaining_sgprs = user_sgpr_info->remaining_sgprs;
638
639 /* Only supported if shaders use push constants. */
640 if (ctx->shader_info->info.min_push_constant_used == UINT8_MAX)
641 return;
642
643 /* Only supported if shaders don't have indirect push constants. */
644 if (ctx->shader_info->info.has_indirect_push_constants)
645 return;
646
647 /* Only supported for 32-bit push constants. */
648 if (!ctx->shader_info->info.has_only_32bit_push_constants)
649 return;
650
651 uint8_t num_push_consts =
652 (ctx->shader_info->info.max_push_constant_used -
653 ctx->shader_info->info.min_push_constant_used) / 4;
654
655 /* Check if the number of user SGPRs is large enough. */
656 if (num_push_consts < remaining_sgprs) {
657 ctx->shader_info->info.num_inline_push_consts = num_push_consts;
658 } else {
659 ctx->shader_info->info.num_inline_push_consts = remaining_sgprs;
660 }
661
662 /* Clamp to the maximum number of allowed inlined push constants. */
663 if (ctx->shader_info->info.num_inline_push_consts > AC_MAX_INLINE_PUSH_CONSTS)
664 ctx->shader_info->info.num_inline_push_consts = AC_MAX_INLINE_PUSH_CONSTS;
665
666 if (ctx->shader_info->info.num_inline_push_consts == num_push_consts &&
667 !ctx->shader_info->info.loads_dynamic_offsets) {
668 /* Disable the default push constants path if all constants are
669 * inlined and if shaders don't use dynamic descriptors.
670 */
671 ctx->shader_info->info.loads_push_constants = false;
672 }
673
674 ctx->shader_info->info.base_inline_push_consts =
675 ctx->shader_info->info.min_push_constant_used / 4;
676 }
677
678 static void allocate_user_sgprs(struct radv_shader_context *ctx,
679 gl_shader_stage stage,
680 bool has_previous_stage,
681 gl_shader_stage previous_stage,
682 bool needs_view_index,
683 struct user_sgpr_info *user_sgpr_info)
684 {
685 uint8_t user_sgpr_count = 0;
686
687 memset(user_sgpr_info, 0, sizeof(struct user_sgpr_info));
688
689 /* until we sort out scratch/global buffers always assign ring offsets for gs/vs/es */
690 if (stage == MESA_SHADER_GEOMETRY ||
691 stage == MESA_SHADER_VERTEX ||
692 stage == MESA_SHADER_TESS_CTRL ||
693 stage == MESA_SHADER_TESS_EVAL ||
694 ctx->is_gs_copy_shader)
695 user_sgpr_info->need_ring_offsets = true;
696
697 if (stage == MESA_SHADER_FRAGMENT &&
698 ctx->shader_info->info.ps.needs_sample_positions)
699 user_sgpr_info->need_ring_offsets = true;
700
701 /* 2 user sgprs will nearly always be allocated for scratch/rings */
702 if (ctx->options->supports_spill || user_sgpr_info->need_ring_offsets) {
703 user_sgpr_count += 2;
704 }
705
706 switch (stage) {
707 case MESA_SHADER_COMPUTE:
708 if (ctx->shader_info->info.cs.uses_grid_size)
709 user_sgpr_count += 3;
710 break;
711 case MESA_SHADER_FRAGMENT:
712 user_sgpr_count += ctx->shader_info->info.ps.needs_sample_positions;
713 break;
714 case MESA_SHADER_VERTEX:
715 if (!ctx->is_gs_copy_shader)
716 user_sgpr_count += count_vs_user_sgprs(ctx);
717 break;
718 case MESA_SHADER_TESS_CTRL:
719 if (has_previous_stage) {
720 if (previous_stage == MESA_SHADER_VERTEX)
721 user_sgpr_count += count_vs_user_sgprs(ctx);
722 }
723 break;
724 case MESA_SHADER_TESS_EVAL:
725 break;
726 case MESA_SHADER_GEOMETRY:
727 if (has_previous_stage) {
728 if (previous_stage == MESA_SHADER_VERTEX) {
729 user_sgpr_count += count_vs_user_sgprs(ctx);
730 }
731 }
732 break;
733 default:
734 break;
735 }
736
737 if (needs_view_index)
738 user_sgpr_count++;
739
740 if (ctx->shader_info->info.loads_push_constants)
741 user_sgpr_count++;
742
743 if (ctx->streamout_buffers)
744 user_sgpr_count++;
745
746 uint32_t available_sgprs = ctx->options->chip_class >= GFX9 && stage != MESA_SHADER_COMPUTE ? 32 : 16;
747 uint32_t remaining_sgprs = available_sgprs - user_sgpr_count;
748 uint32_t num_desc_set =
749 util_bitcount(ctx->shader_info->info.desc_set_used_mask);
750
751 if (remaining_sgprs < num_desc_set) {
752 user_sgpr_info->indirect_all_descriptor_sets = true;
753 user_sgpr_info->remaining_sgprs = remaining_sgprs - 1;
754 } else {
755 user_sgpr_info->remaining_sgprs = remaining_sgprs - num_desc_set;
756 }
757
758 allocate_inline_push_consts(ctx, user_sgpr_info);
759 }
760
761 static void
762 declare_global_input_sgprs(struct radv_shader_context *ctx,
763 const struct user_sgpr_info *user_sgpr_info,
764 struct arg_info *args,
765 LLVMValueRef *desc_sets)
766 {
767 LLVMTypeRef type = ac_array_in_const32_addr_space(ctx->ac.i8);
768
769 /* 1 for each descriptor set */
770 if (!user_sgpr_info->indirect_all_descriptor_sets) {
771 uint32_t mask = ctx->shader_info->info.desc_set_used_mask;
772
773 while (mask) {
774 int i = u_bit_scan(&mask);
775
776 add_arg(args, ARG_SGPR, type, &ctx->descriptor_sets[i]);
777 }
778 } else {
779 add_arg(args, ARG_SGPR, ac_array_in_const32_addr_space(type),
780 desc_sets);
781 }
782
783 if (ctx->shader_info->info.loads_push_constants) {
784 /* 1 for push constants and dynamic descriptors */
785 add_arg(args, ARG_SGPR, type, &ctx->abi.push_constants);
786 }
787
788 for (unsigned i = 0; i < ctx->shader_info->info.num_inline_push_consts; i++) {
789 add_arg(args, ARG_SGPR, ctx->ac.i32,
790 &ctx->abi.inline_push_consts[i]);
791 }
792 ctx->abi.num_inline_push_consts = ctx->shader_info->info.num_inline_push_consts;
793 ctx->abi.base_inline_push_consts = ctx->shader_info->info.base_inline_push_consts;
794
795 if (ctx->shader_info->info.so.num_outputs) {
796 add_arg(args, ARG_SGPR,
797 ac_array_in_const32_addr_space(ctx->ac.v4i32),
798 &ctx->streamout_buffers);
799 }
800 }
801
802 static void
803 declare_vs_specific_input_sgprs(struct radv_shader_context *ctx,
804 gl_shader_stage stage,
805 bool has_previous_stage,
806 gl_shader_stage previous_stage,
807 struct arg_info *args)
808 {
809 if (!ctx->is_gs_copy_shader &&
810 (stage == MESA_SHADER_VERTEX ||
811 (has_previous_stage && previous_stage == MESA_SHADER_VERTEX))) {
812 if (ctx->shader_info->info.vs.has_vertex_buffers) {
813 add_arg(args, ARG_SGPR,
814 ac_array_in_const32_addr_space(ctx->ac.v4i32),
815 &ctx->vertex_buffers);
816 }
817 add_arg(args, ARG_SGPR, ctx->ac.i32, &ctx->abi.base_vertex);
818 add_arg(args, ARG_SGPR, ctx->ac.i32, &ctx->abi.start_instance);
819 if (ctx->shader_info->info.vs.needs_draw_id) {
820 add_arg(args, ARG_SGPR, ctx->ac.i32, &ctx->abi.draw_id);
821 }
822 }
823 }
824
825 static void
826 declare_vs_input_vgprs(struct radv_shader_context *ctx, struct arg_info *args)
827 {
828 add_arg(args, ARG_VGPR, ctx->ac.i32, &ctx->abi.vertex_id);
829 if (!ctx->is_gs_copy_shader) {
830 if (ctx->options->key.vs.out.as_ls) {
831 add_arg(args, ARG_VGPR, ctx->ac.i32, &ctx->rel_auto_id);
832 if (ctx->ac.chip_class >= GFX10) {
833 add_arg(args, ARG_VGPR, ctx->ac.i32, NULL); /* user vgpr */
834 add_arg(args, ARG_VGPR, ctx->ac.i32, &ctx->abi.instance_id);
835 } else {
836 add_arg(args, ARG_VGPR, ctx->ac.i32, &ctx->abi.instance_id);
837 add_arg(args, ARG_VGPR, ctx->ac.i32, NULL); /* unused */
838 }
839 } else {
840 if (ctx->ac.chip_class >= GFX10) {
841 add_arg(args, ARG_VGPR, ctx->ac.i32, NULL); /* user vgpr */
842 add_arg(args, ARG_VGPR, ctx->ac.i32, NULL); /* user vgpr */
843 add_arg(args, ARG_VGPR, ctx->ac.i32, &ctx->abi.instance_id);
844 } else {
845 add_arg(args, ARG_VGPR, ctx->ac.i32, &ctx->abi.instance_id);
846 add_arg(args, ARG_VGPR, ctx->ac.i32, &ctx->vs_prim_id);
847 add_arg(args, ARG_VGPR, ctx->ac.i32, NULL); /* unused */
848 }
849 }
850 }
851 }
852
853 static void
854 declare_streamout_sgprs(struct radv_shader_context *ctx, gl_shader_stage stage,
855 struct arg_info *args)
856 {
857 int i;
858
859 if (ctx->ac.chip_class >= GFX10)
860 return;
861
862 /* Streamout SGPRs. */
863 if (ctx->shader_info->info.so.num_outputs) {
864 assert(stage == MESA_SHADER_VERTEX ||
865 stage == MESA_SHADER_TESS_EVAL);
866
867 if (stage != MESA_SHADER_TESS_EVAL) {
868 add_arg(args, ARG_SGPR, ctx->ac.i32, &ctx->streamout_config);
869 } else {
870 args->assign[args->count - 1] = &ctx->streamout_config;
871 args->types[args->count - 1] = ctx->ac.i32;
872 }
873
874 add_arg(args, ARG_SGPR, ctx->ac.i32, &ctx->streamout_write_idx);
875 }
876
877 /* A streamout buffer offset is loaded if the stride is non-zero. */
878 for (i = 0; i < 4; i++) {
879 if (!ctx->shader_info->info.so.strides[i])
880 continue;
881
882 add_arg(args, ARG_SGPR, ctx->ac.i32, &ctx->streamout_offset[i]);
883 }
884 }
885
886 static void
887 declare_tes_input_vgprs(struct radv_shader_context *ctx, struct arg_info *args)
888 {
889 add_arg(args, ARG_VGPR, ctx->ac.f32, &ctx->tes_u);
890 add_arg(args, ARG_VGPR, ctx->ac.f32, &ctx->tes_v);
891 add_arg(args, ARG_VGPR, ctx->ac.i32, &ctx->tes_rel_patch_id);
892 add_arg(args, ARG_VGPR, ctx->ac.i32, &ctx->abi.tes_patch_id);
893 }
894
895 static void
896 set_global_input_locs(struct radv_shader_context *ctx,
897 const struct user_sgpr_info *user_sgpr_info,
898 LLVMValueRef desc_sets, uint8_t *user_sgpr_idx)
899 {
900 uint32_t mask = ctx->shader_info->info.desc_set_used_mask;
901
902 if (!user_sgpr_info->indirect_all_descriptor_sets) {
903 while (mask) {
904 int i = u_bit_scan(&mask);
905
906 set_loc_desc(ctx, i, user_sgpr_idx);
907 }
908 } else {
909 set_loc_shader_ptr(ctx, AC_UD_INDIRECT_DESCRIPTOR_SETS,
910 user_sgpr_idx);
911
912 while (mask) {
913 int i = u_bit_scan(&mask);
914
915 ctx->descriptor_sets[i] =
916 ac_build_load_to_sgpr(&ctx->ac, desc_sets,
917 LLVMConstInt(ctx->ac.i32, i, false));
918
919 }
920
921 ctx->shader_info->need_indirect_descriptor_sets = true;
922 }
923
924 if (ctx->shader_info->info.loads_push_constants) {
925 set_loc_shader_ptr(ctx, AC_UD_PUSH_CONSTANTS, user_sgpr_idx);
926 }
927
928 if (ctx->shader_info->info.num_inline_push_consts) {
929 set_loc_shader(ctx, AC_UD_INLINE_PUSH_CONSTANTS, user_sgpr_idx,
930 ctx->shader_info->info.num_inline_push_consts);
931 }
932
933 if (ctx->streamout_buffers) {
934 set_loc_shader_ptr(ctx, AC_UD_STREAMOUT_BUFFERS,
935 user_sgpr_idx);
936 }
937 }
938
939 static void
940 set_vs_specific_input_locs(struct radv_shader_context *ctx,
941 gl_shader_stage stage, bool has_previous_stage,
942 gl_shader_stage previous_stage,
943 uint8_t *user_sgpr_idx)
944 {
945 if (!ctx->is_gs_copy_shader &&
946 (stage == MESA_SHADER_VERTEX ||
947 (has_previous_stage && previous_stage == MESA_SHADER_VERTEX))) {
948 if (ctx->shader_info->info.vs.has_vertex_buffers) {
949 set_loc_shader_ptr(ctx, AC_UD_VS_VERTEX_BUFFERS,
950 user_sgpr_idx);
951 }
952
953 unsigned vs_num = 2;
954 if (ctx->shader_info->info.vs.needs_draw_id)
955 vs_num++;
956
957 set_loc_shader(ctx, AC_UD_VS_BASE_VERTEX_START_INSTANCE,
958 user_sgpr_idx, vs_num);
959 }
960 }
961
962 static void set_llvm_calling_convention(LLVMValueRef func,
963 gl_shader_stage stage)
964 {
965 enum radeon_llvm_calling_convention calling_conv;
966
967 switch (stage) {
968 case MESA_SHADER_VERTEX:
969 case MESA_SHADER_TESS_EVAL:
970 calling_conv = RADEON_LLVM_AMDGPU_VS;
971 break;
972 case MESA_SHADER_GEOMETRY:
973 calling_conv = RADEON_LLVM_AMDGPU_GS;
974 break;
975 case MESA_SHADER_TESS_CTRL:
976 calling_conv = RADEON_LLVM_AMDGPU_HS;
977 break;
978 case MESA_SHADER_FRAGMENT:
979 calling_conv = RADEON_LLVM_AMDGPU_PS;
980 break;
981 case MESA_SHADER_COMPUTE:
982 calling_conv = RADEON_LLVM_AMDGPU_CS;
983 break;
984 default:
985 unreachable("Unhandle shader type");
986 }
987
988 LLVMSetFunctionCallConv(func, calling_conv);
989 }
990
991 /* Returns whether the stage is a stage that can be directly before the GS */
992 static bool is_pre_gs_stage(gl_shader_stage stage)
993 {
994 return stage == MESA_SHADER_VERTEX || stage == MESA_SHADER_TESS_EVAL;
995 }
996
997 static void create_function(struct radv_shader_context *ctx,
998 gl_shader_stage stage,
999 bool has_previous_stage,
1000 gl_shader_stage previous_stage)
1001 {
1002 uint8_t user_sgpr_idx;
1003 struct user_sgpr_info user_sgpr_info;
1004 struct arg_info args = {};
1005 LLVMValueRef desc_sets;
1006 bool needs_view_index = needs_view_index_sgpr(ctx, stage);
1007
1008 if (ctx->ac.chip_class >= GFX10) {
1009 if (is_pre_gs_stage(stage) && ctx->options->key.vs.out.as_ngg) {
1010 /* On GFX10, VS is merged into GS for NGG. */
1011 previous_stage = stage;
1012 stage = MESA_SHADER_GEOMETRY;
1013 has_previous_stage = true;
1014 }
1015 }
1016
1017 allocate_user_sgprs(ctx, stage, has_previous_stage,
1018 previous_stage, needs_view_index, &user_sgpr_info);
1019
1020 if (user_sgpr_info.need_ring_offsets && !ctx->options->supports_spill) {
1021 add_arg(&args, ARG_SGPR, ac_array_in_const_addr_space(ctx->ac.v4i32),
1022 &ctx->ring_offsets);
1023 }
1024
1025 switch (stage) {
1026 case MESA_SHADER_COMPUTE:
1027 declare_global_input_sgprs(ctx, &user_sgpr_info, &args,
1028 &desc_sets);
1029
1030 if (ctx->shader_info->info.cs.uses_grid_size) {
1031 add_arg(&args, ARG_SGPR, ctx->ac.v3i32,
1032 &ctx->abi.num_work_groups);
1033 }
1034
1035 for (int i = 0; i < 3; i++) {
1036 ctx->abi.workgroup_ids[i] = NULL;
1037 if (ctx->shader_info->info.cs.uses_block_id[i]) {
1038 add_arg(&args, ARG_SGPR, ctx->ac.i32,
1039 &ctx->abi.workgroup_ids[i]);
1040 }
1041 }
1042
1043 if (ctx->shader_info->info.cs.uses_local_invocation_idx)
1044 add_arg(&args, ARG_SGPR, ctx->ac.i32, &ctx->abi.tg_size);
1045 add_arg(&args, ARG_VGPR, ctx->ac.v3i32,
1046 &ctx->abi.local_invocation_ids);
1047 break;
1048 case MESA_SHADER_VERTEX:
1049 declare_global_input_sgprs(ctx, &user_sgpr_info, &args,
1050 &desc_sets);
1051
1052 declare_vs_specific_input_sgprs(ctx, stage, has_previous_stage,
1053 previous_stage, &args);
1054
1055 if (needs_view_index)
1056 add_arg(&args, ARG_SGPR, ctx->ac.i32,
1057 &ctx->abi.view_index);
1058 if (ctx->options->key.vs.out.as_es) {
1059 add_arg(&args, ARG_SGPR, ctx->ac.i32,
1060 &ctx->es2gs_offset);
1061 } else if (ctx->options->key.vs.out.as_ls) {
1062 /* no extra parameters */
1063 } else {
1064 declare_streamout_sgprs(ctx, stage, &args);
1065 }
1066
1067 declare_vs_input_vgprs(ctx, &args);
1068 break;
1069 case MESA_SHADER_TESS_CTRL:
1070 if (has_previous_stage) {
1071 // First 6 system regs
1072 add_arg(&args, ARG_SGPR, ctx->ac.i32, &ctx->oc_lds);
1073 add_arg(&args, ARG_SGPR, ctx->ac.i32,
1074 &ctx->merged_wave_info);
1075 add_arg(&args, ARG_SGPR, ctx->ac.i32,
1076 &ctx->tess_factor_offset);
1077
1078 add_arg(&args, ARG_SGPR, ctx->ac.i32, NULL); // scratch offset
1079 add_arg(&args, ARG_SGPR, ctx->ac.i32, NULL); // unknown
1080 add_arg(&args, ARG_SGPR, ctx->ac.i32, NULL); // unknown
1081
1082 declare_global_input_sgprs(ctx, &user_sgpr_info, &args,
1083 &desc_sets);
1084
1085 declare_vs_specific_input_sgprs(ctx, stage,
1086 has_previous_stage,
1087 previous_stage, &args);
1088
1089 if (needs_view_index)
1090 add_arg(&args, ARG_SGPR, ctx->ac.i32,
1091 &ctx->abi.view_index);
1092
1093 add_arg(&args, ARG_VGPR, ctx->ac.i32,
1094 &ctx->abi.tcs_patch_id);
1095 add_arg(&args, ARG_VGPR, ctx->ac.i32,
1096 &ctx->abi.tcs_rel_ids);
1097
1098 declare_vs_input_vgprs(ctx, &args);
1099 } else {
1100 declare_global_input_sgprs(ctx, &user_sgpr_info, &args,
1101 &desc_sets);
1102
1103 if (needs_view_index)
1104 add_arg(&args, ARG_SGPR, ctx->ac.i32,
1105 &ctx->abi.view_index);
1106
1107 add_arg(&args, ARG_SGPR, ctx->ac.i32, &ctx->oc_lds);
1108 add_arg(&args, ARG_SGPR, ctx->ac.i32,
1109 &ctx->tess_factor_offset);
1110 add_arg(&args, ARG_VGPR, ctx->ac.i32,
1111 &ctx->abi.tcs_patch_id);
1112 add_arg(&args, ARG_VGPR, ctx->ac.i32,
1113 &ctx->abi.tcs_rel_ids);
1114 }
1115 break;
1116 case MESA_SHADER_TESS_EVAL:
1117 declare_global_input_sgprs(ctx, &user_sgpr_info, &args,
1118 &desc_sets);
1119
1120 if (needs_view_index)
1121 add_arg(&args, ARG_SGPR, ctx->ac.i32,
1122 &ctx->abi.view_index);
1123
1124 if (ctx->options->key.tes.out.as_es) {
1125 add_arg(&args, ARG_SGPR, ctx->ac.i32, &ctx->oc_lds);
1126 add_arg(&args, ARG_SGPR, ctx->ac.i32, NULL);
1127 add_arg(&args, ARG_SGPR, ctx->ac.i32,
1128 &ctx->es2gs_offset);
1129 } else {
1130 add_arg(&args, ARG_SGPR, ctx->ac.i32, NULL);
1131 declare_streamout_sgprs(ctx, stage, &args);
1132 add_arg(&args, ARG_SGPR, ctx->ac.i32, &ctx->oc_lds);
1133 }
1134 declare_tes_input_vgprs(ctx, &args);
1135 break;
1136 case MESA_SHADER_GEOMETRY:
1137 if (has_previous_stage) {
1138 // First 6 system regs
1139 if (ctx->options->key.vs.out.as_ngg) {
1140 add_arg(&args, ARG_SGPR, ctx->ac.i32,
1141 &ctx->gs_tg_info);
1142 } else {
1143 add_arg(&args, ARG_SGPR, ctx->ac.i32,
1144 &ctx->gs2vs_offset);
1145 }
1146
1147 add_arg(&args, ARG_SGPR, ctx->ac.i32,
1148 &ctx->merged_wave_info);
1149 add_arg(&args, ARG_SGPR, ctx->ac.i32, &ctx->oc_lds);
1150
1151 add_arg(&args, ARG_SGPR, ctx->ac.i32, NULL); // scratch offset
1152 add_arg(&args, ARG_SGPR, ctx->ac.i32, NULL); // unknown
1153 add_arg(&args, ARG_SGPR, ctx->ac.i32, NULL); // unknown
1154
1155 declare_global_input_sgprs(ctx, &user_sgpr_info, &args,
1156 &desc_sets);
1157
1158 if (previous_stage != MESA_SHADER_TESS_EVAL) {
1159 declare_vs_specific_input_sgprs(ctx, stage,
1160 has_previous_stage,
1161 previous_stage,
1162 &args);
1163 }
1164
1165 if (needs_view_index)
1166 add_arg(&args, ARG_SGPR, ctx->ac.i32,
1167 &ctx->abi.view_index);
1168
1169 add_arg(&args, ARG_VGPR, ctx->ac.i32,
1170 &ctx->gs_vtx_offset[0]);
1171 add_arg(&args, ARG_VGPR, ctx->ac.i32,
1172 &ctx->gs_vtx_offset[2]);
1173 add_arg(&args, ARG_VGPR, ctx->ac.i32,
1174 &ctx->abi.gs_prim_id);
1175 add_arg(&args, ARG_VGPR, ctx->ac.i32,
1176 &ctx->abi.gs_invocation_id);
1177 add_arg(&args, ARG_VGPR, ctx->ac.i32,
1178 &ctx->gs_vtx_offset[4]);
1179
1180 if (previous_stage == MESA_SHADER_VERTEX) {
1181 declare_vs_input_vgprs(ctx, &args);
1182 } else {
1183 declare_tes_input_vgprs(ctx, &args);
1184 }
1185 } else {
1186 declare_global_input_sgprs(ctx, &user_sgpr_info, &args,
1187 &desc_sets);
1188
1189 if (needs_view_index)
1190 add_arg(&args, ARG_SGPR, ctx->ac.i32,
1191 &ctx->abi.view_index);
1192
1193 add_arg(&args, ARG_SGPR, ctx->ac.i32, &ctx->gs2vs_offset);
1194 add_arg(&args, ARG_SGPR, ctx->ac.i32, &ctx->gs_wave_id);
1195 add_arg(&args, ARG_VGPR, ctx->ac.i32,
1196 &ctx->gs_vtx_offset[0]);
1197 add_arg(&args, ARG_VGPR, ctx->ac.i32,
1198 &ctx->gs_vtx_offset[1]);
1199 add_arg(&args, ARG_VGPR, ctx->ac.i32,
1200 &ctx->abi.gs_prim_id);
1201 add_arg(&args, ARG_VGPR, ctx->ac.i32,
1202 &ctx->gs_vtx_offset[2]);
1203 add_arg(&args, ARG_VGPR, ctx->ac.i32,
1204 &ctx->gs_vtx_offset[3]);
1205 add_arg(&args, ARG_VGPR, ctx->ac.i32,
1206 &ctx->gs_vtx_offset[4]);
1207 add_arg(&args, ARG_VGPR, ctx->ac.i32,
1208 &ctx->gs_vtx_offset[5]);
1209 add_arg(&args, ARG_VGPR, ctx->ac.i32,
1210 &ctx->abi.gs_invocation_id);
1211 }
1212 break;
1213 case MESA_SHADER_FRAGMENT:
1214 declare_global_input_sgprs(ctx, &user_sgpr_info, &args,
1215 &desc_sets);
1216
1217 add_arg(&args, ARG_SGPR, ctx->ac.i32, &ctx->abi.prim_mask);
1218 add_arg(&args, ARG_VGPR, ctx->ac.v2i32, &ctx->persp_sample);
1219 add_arg(&args, ARG_VGPR, ctx->ac.v2i32, &ctx->persp_center);
1220 add_arg(&args, ARG_VGPR, ctx->ac.v2i32, &ctx->persp_centroid);
1221 add_arg(&args, ARG_VGPR, ctx->ac.v3i32, NULL); /* persp pull model */
1222 add_arg(&args, ARG_VGPR, ctx->ac.v2i32, &ctx->linear_sample);
1223 add_arg(&args, ARG_VGPR, ctx->ac.v2i32, &ctx->linear_center);
1224 add_arg(&args, ARG_VGPR, ctx->ac.v2i32, &ctx->linear_centroid);
1225 add_arg(&args, ARG_VGPR, ctx->ac.f32, NULL); /* line stipple tex */
1226 add_arg(&args, ARG_VGPR, ctx->ac.f32, &ctx->abi.frag_pos[0]);
1227 add_arg(&args, ARG_VGPR, ctx->ac.f32, &ctx->abi.frag_pos[1]);
1228 add_arg(&args, ARG_VGPR, ctx->ac.f32, &ctx->abi.frag_pos[2]);
1229 add_arg(&args, ARG_VGPR, ctx->ac.f32, &ctx->abi.frag_pos[3]);
1230 add_arg(&args, ARG_VGPR, ctx->ac.i32, &ctx->abi.front_face);
1231 add_arg(&args, ARG_VGPR, ctx->ac.i32, &ctx->abi.ancillary);
1232 add_arg(&args, ARG_VGPR, ctx->ac.i32, &ctx->abi.sample_coverage);
1233 add_arg(&args, ARG_VGPR, ctx->ac.i32, NULL); /* fixed pt */
1234 break;
1235 default:
1236 unreachable("Shader stage not implemented");
1237 }
1238
1239 ctx->main_function = create_llvm_function(
1240 ctx->context, ctx->ac.module, ctx->ac.builder, NULL, 0, &args,
1241 ctx->max_workgroup_size, ctx->options);
1242 set_llvm_calling_convention(ctx->main_function, stage);
1243
1244
1245 ctx->shader_info->num_input_vgprs = 0;
1246 ctx->shader_info->num_input_sgprs = ctx->options->supports_spill ? 2 : 0;
1247
1248 ctx->shader_info->num_input_sgprs += args.num_sgprs_used;
1249
1250 if (ctx->stage != MESA_SHADER_FRAGMENT)
1251 ctx->shader_info->num_input_vgprs = args.num_vgprs_used;
1252
1253 assign_arguments(ctx->main_function, &args);
1254
1255 user_sgpr_idx = 0;
1256
1257 if (ctx->options->supports_spill || user_sgpr_info.need_ring_offsets) {
1258 set_loc_shader_ptr(ctx, AC_UD_SCRATCH_RING_OFFSETS,
1259 &user_sgpr_idx);
1260 if (ctx->options->supports_spill) {
1261 ctx->ring_offsets = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.implicit.buffer.ptr",
1262 LLVMPointerType(ctx->ac.i8, AC_ADDR_SPACE_CONST),
1263 NULL, 0, AC_FUNC_ATTR_READNONE);
1264 ctx->ring_offsets = LLVMBuildBitCast(ctx->ac.builder, ctx->ring_offsets,
1265 ac_array_in_const_addr_space(ctx->ac.v4i32), "");
1266 }
1267 }
1268
1269 /* For merged shaders the user SGPRs start at 8, with 8 system SGPRs in front (including
1270 * the rw_buffers at s0/s1. With user SGPR0 = s8, lets restart the count from 0 */
1271 if (has_previous_stage)
1272 user_sgpr_idx = 0;
1273
1274 set_global_input_locs(ctx, &user_sgpr_info, desc_sets, &user_sgpr_idx);
1275
1276 switch (stage) {
1277 case MESA_SHADER_COMPUTE:
1278 if (ctx->shader_info->info.cs.uses_grid_size) {
1279 set_loc_shader(ctx, AC_UD_CS_GRID_SIZE,
1280 &user_sgpr_idx, 3);
1281 }
1282 break;
1283 case MESA_SHADER_VERTEX:
1284 set_vs_specific_input_locs(ctx, stage, has_previous_stage,
1285 previous_stage, &user_sgpr_idx);
1286 if (ctx->abi.view_index)
1287 set_loc_shader(ctx, AC_UD_VIEW_INDEX, &user_sgpr_idx, 1);
1288 break;
1289 case MESA_SHADER_TESS_CTRL:
1290 set_vs_specific_input_locs(ctx, stage, has_previous_stage,
1291 previous_stage, &user_sgpr_idx);
1292 if (ctx->abi.view_index)
1293 set_loc_shader(ctx, AC_UD_VIEW_INDEX, &user_sgpr_idx, 1);
1294 break;
1295 case MESA_SHADER_TESS_EVAL:
1296 if (ctx->abi.view_index)
1297 set_loc_shader(ctx, AC_UD_VIEW_INDEX, &user_sgpr_idx, 1);
1298 break;
1299 case MESA_SHADER_GEOMETRY:
1300 if (has_previous_stage) {
1301 if (previous_stage == MESA_SHADER_VERTEX)
1302 set_vs_specific_input_locs(ctx, stage,
1303 has_previous_stage,
1304 previous_stage,
1305 &user_sgpr_idx);
1306 }
1307 if (ctx->abi.view_index)
1308 set_loc_shader(ctx, AC_UD_VIEW_INDEX, &user_sgpr_idx, 1);
1309 break;
1310 case MESA_SHADER_FRAGMENT:
1311 break;
1312 default:
1313 unreachable("Shader stage not implemented");
1314 }
1315
1316 if (stage == MESA_SHADER_TESS_CTRL ||
1317 (stage == MESA_SHADER_VERTEX && ctx->options->key.vs.out.as_ls) ||
1318 /* GFX9 has the ESGS ring buffer in LDS. */
1319 (stage == MESA_SHADER_GEOMETRY && has_previous_stage)) {
1320 ac_declare_lds_as_pointer(&ctx->ac);
1321 }
1322
1323 ctx->shader_info->num_user_sgprs = user_sgpr_idx;
1324 }
1325
1326
1327 static LLVMValueRef
1328 radv_load_resource(struct ac_shader_abi *abi, LLVMValueRef index,
1329 unsigned desc_set, unsigned binding)
1330 {
1331 struct radv_shader_context *ctx = radv_shader_context_from_abi(abi);
1332 LLVMValueRef desc_ptr = ctx->descriptor_sets[desc_set];
1333 struct radv_pipeline_layout *pipeline_layout = ctx->options->layout;
1334 struct radv_descriptor_set_layout *layout = pipeline_layout->set[desc_set].layout;
1335 unsigned base_offset = layout->binding[binding].offset;
1336 LLVMValueRef offset, stride;
1337
1338 if (layout->binding[binding].type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
1339 layout->binding[binding].type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
1340 unsigned idx = pipeline_layout->set[desc_set].dynamic_offset_start +
1341 layout->binding[binding].dynamic_offset_offset;
1342 desc_ptr = ctx->abi.push_constants;
1343 base_offset = pipeline_layout->push_constant_size + 16 * idx;
1344 stride = LLVMConstInt(ctx->ac.i32, 16, false);
1345 } else
1346 stride = LLVMConstInt(ctx->ac.i32, layout->binding[binding].size, false);
1347
1348 offset = LLVMConstInt(ctx->ac.i32, base_offset, false);
1349
1350 if (layout->binding[binding].type != VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
1351 offset = ac_build_imad(&ctx->ac, index, stride, offset);
1352 }
1353
1354 desc_ptr = LLVMBuildGEP(ctx->ac.builder, desc_ptr, &offset, 1, "");
1355 desc_ptr = ac_cast_ptr(&ctx->ac, desc_ptr, ctx->ac.v4i32);
1356 LLVMSetMetadata(desc_ptr, ctx->ac.uniform_md_kind, ctx->ac.empty_md);
1357
1358 if (layout->binding[binding].type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {
1359 uint32_t desc_type = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
1360 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
1361 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
1362 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W) |
1363 S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
1364 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
1365
1366 LLVMValueRef desc_components[4] = {
1367 LLVMBuildPtrToInt(ctx->ac.builder, desc_ptr, ctx->ac.intptr, ""),
1368 LLVMConstInt(ctx->ac.i32, S_008F04_BASE_ADDRESS_HI(ctx->options->address32_hi), false),
1369 /* High limit to support variable sizes. */
1370 LLVMConstInt(ctx->ac.i32, 0xffffffff, false),
1371 LLVMConstInt(ctx->ac.i32, desc_type, false),
1372 };
1373
1374 return ac_build_gather_values(&ctx->ac, desc_components, 4);
1375 }
1376
1377 return desc_ptr;
1378 }
1379
1380
1381 /* The offchip buffer layout for TCS->TES is
1382 *
1383 * - attribute 0 of patch 0 vertex 0
1384 * - attribute 0 of patch 0 vertex 1
1385 * - attribute 0 of patch 0 vertex 2
1386 * ...
1387 * - attribute 0 of patch 1 vertex 0
1388 * - attribute 0 of patch 1 vertex 1
1389 * ...
1390 * - attribute 1 of patch 0 vertex 0
1391 * - attribute 1 of patch 0 vertex 1
1392 * ...
1393 * - per patch attribute 0 of patch 0
1394 * - per patch attribute 0 of patch 1
1395 * ...
1396 *
1397 * Note that every attribute has 4 components.
1398 */
1399 static LLVMValueRef get_non_vertex_index_offset(struct radv_shader_context *ctx)
1400 {
1401 uint32_t num_patches = ctx->tcs_num_patches;
1402 uint32_t num_tcs_outputs;
1403 if (ctx->stage == MESA_SHADER_TESS_CTRL)
1404 num_tcs_outputs = util_last_bit64(ctx->shader_info->info.tcs.outputs_written);
1405 else
1406 num_tcs_outputs = ctx->options->key.tes.tcs_num_outputs;
1407
1408 uint32_t output_vertex_size = num_tcs_outputs * 16;
1409 uint32_t pervertex_output_patch_size = ctx->tcs_vertices_per_patch * output_vertex_size;
1410
1411 return LLVMConstInt(ctx->ac.i32, pervertex_output_patch_size * num_patches, false);
1412 }
1413
1414 static LLVMValueRef calc_param_stride(struct radv_shader_context *ctx,
1415 LLVMValueRef vertex_index)
1416 {
1417 LLVMValueRef param_stride;
1418 if (vertex_index)
1419 param_stride = LLVMConstInt(ctx->ac.i32, ctx->tcs_vertices_per_patch * ctx->tcs_num_patches, false);
1420 else
1421 param_stride = LLVMConstInt(ctx->ac.i32, ctx->tcs_num_patches, false);
1422 return param_stride;
1423 }
1424
1425 static LLVMValueRef get_tcs_tes_buffer_address(struct radv_shader_context *ctx,
1426 LLVMValueRef vertex_index,
1427 LLVMValueRef param_index)
1428 {
1429 LLVMValueRef base_addr;
1430 LLVMValueRef param_stride, constant16;
1431 LLVMValueRef rel_patch_id = get_rel_patch_id(ctx);
1432 LLVMValueRef vertices_per_patch = LLVMConstInt(ctx->ac.i32, ctx->tcs_vertices_per_patch, false);
1433 constant16 = LLVMConstInt(ctx->ac.i32, 16, false);
1434 param_stride = calc_param_stride(ctx, vertex_index);
1435 if (vertex_index) {
1436 base_addr = ac_build_imad(&ctx->ac, rel_patch_id,
1437 vertices_per_patch, vertex_index);
1438 } else {
1439 base_addr = rel_patch_id;
1440 }
1441
1442 base_addr = LLVMBuildAdd(ctx->ac.builder, base_addr,
1443 LLVMBuildMul(ctx->ac.builder, param_index,
1444 param_stride, ""), "");
1445
1446 base_addr = LLVMBuildMul(ctx->ac.builder, base_addr, constant16, "");
1447
1448 if (!vertex_index) {
1449 LLVMValueRef patch_data_offset = get_non_vertex_index_offset(ctx);
1450
1451 base_addr = LLVMBuildAdd(ctx->ac.builder, base_addr,
1452 patch_data_offset, "");
1453 }
1454 return base_addr;
1455 }
1456
1457 static LLVMValueRef get_tcs_tes_buffer_address_params(struct radv_shader_context *ctx,
1458 unsigned param,
1459 unsigned const_index,
1460 bool is_compact,
1461 LLVMValueRef vertex_index,
1462 LLVMValueRef indir_index)
1463 {
1464 LLVMValueRef param_index;
1465
1466 if (indir_index)
1467 param_index = LLVMBuildAdd(ctx->ac.builder, LLVMConstInt(ctx->ac.i32, param, false),
1468 indir_index, "");
1469 else {
1470 if (const_index && !is_compact)
1471 param += const_index;
1472 param_index = LLVMConstInt(ctx->ac.i32, param, false);
1473 }
1474 return get_tcs_tes_buffer_address(ctx, vertex_index, param_index);
1475 }
1476
1477 static LLVMValueRef
1478 get_dw_address(struct radv_shader_context *ctx,
1479 LLVMValueRef dw_addr,
1480 unsigned param,
1481 unsigned const_index,
1482 bool compact_const_index,
1483 LLVMValueRef vertex_index,
1484 LLVMValueRef stride,
1485 LLVMValueRef indir_index)
1486
1487 {
1488
1489 if (vertex_index) {
1490 dw_addr = LLVMBuildAdd(ctx->ac.builder, dw_addr,
1491 LLVMBuildMul(ctx->ac.builder,
1492 vertex_index,
1493 stride, ""), "");
1494 }
1495
1496 if (indir_index)
1497 dw_addr = LLVMBuildAdd(ctx->ac.builder, dw_addr,
1498 LLVMBuildMul(ctx->ac.builder, indir_index,
1499 LLVMConstInt(ctx->ac.i32, 4, false), ""), "");
1500 else if (const_index && !compact_const_index)
1501 dw_addr = LLVMBuildAdd(ctx->ac.builder, dw_addr,
1502 LLVMConstInt(ctx->ac.i32, const_index * 4, false), "");
1503
1504 dw_addr = LLVMBuildAdd(ctx->ac.builder, dw_addr,
1505 LLVMConstInt(ctx->ac.i32, param * 4, false), "");
1506
1507 if (const_index && compact_const_index)
1508 dw_addr = LLVMBuildAdd(ctx->ac.builder, dw_addr,
1509 LLVMConstInt(ctx->ac.i32, const_index, false), "");
1510 return dw_addr;
1511 }
1512
1513 static LLVMValueRef
1514 load_tcs_varyings(struct ac_shader_abi *abi,
1515 LLVMTypeRef type,
1516 LLVMValueRef vertex_index,
1517 LLVMValueRef indir_index,
1518 unsigned const_index,
1519 unsigned location,
1520 unsigned driver_location,
1521 unsigned component,
1522 unsigned num_components,
1523 bool is_patch,
1524 bool is_compact,
1525 bool load_input)
1526 {
1527 struct radv_shader_context *ctx = radv_shader_context_from_abi(abi);
1528 LLVMValueRef dw_addr, stride;
1529 LLVMValueRef value[4], result;
1530 unsigned param = shader_io_get_unique_index(location);
1531
1532 if (load_input) {
1533 uint32_t input_vertex_size = (ctx->tcs_num_inputs * 16) / 4;
1534 stride = LLVMConstInt(ctx->ac.i32, input_vertex_size, false);
1535 dw_addr = get_tcs_in_current_patch_offset(ctx);
1536 } else {
1537 if (!is_patch) {
1538 stride = get_tcs_out_vertex_stride(ctx);
1539 dw_addr = get_tcs_out_current_patch_offset(ctx);
1540 } else {
1541 dw_addr = get_tcs_out_current_patch_data_offset(ctx);
1542 stride = NULL;
1543 }
1544 }
1545
1546 dw_addr = get_dw_address(ctx, dw_addr, param, const_index, is_compact, vertex_index, stride,
1547 indir_index);
1548
1549 for (unsigned i = 0; i < num_components + component; i++) {
1550 value[i] = ac_lds_load(&ctx->ac, dw_addr);
1551 dw_addr = LLVMBuildAdd(ctx->ac.builder, dw_addr,
1552 ctx->ac.i32_1, "");
1553 }
1554 result = ac_build_varying_gather_values(&ctx->ac, value, num_components, component);
1555 return result;
1556 }
1557
1558 static void
1559 store_tcs_output(struct ac_shader_abi *abi,
1560 const nir_variable *var,
1561 LLVMValueRef vertex_index,
1562 LLVMValueRef param_index,
1563 unsigned const_index,
1564 LLVMValueRef src,
1565 unsigned writemask)
1566 {
1567 struct radv_shader_context *ctx = radv_shader_context_from_abi(abi);
1568 const unsigned location = var->data.location;
1569 unsigned component = var->data.location_frac;
1570 const bool is_patch = var->data.patch;
1571 const bool is_compact = var->data.compact;
1572 LLVMValueRef dw_addr;
1573 LLVMValueRef stride = NULL;
1574 LLVMValueRef buf_addr = NULL;
1575 unsigned param;
1576 bool store_lds = true;
1577
1578 if (is_patch) {
1579 if (!(ctx->tcs_patch_outputs_read & (1U << (location - VARYING_SLOT_PATCH0))))
1580 store_lds = false;
1581 } else {
1582 if (!(ctx->tcs_outputs_read & (1ULL << location)))
1583 store_lds = false;
1584 }
1585
1586 param = shader_io_get_unique_index(location);
1587 if ((location == VARYING_SLOT_CLIP_DIST0 || location == VARYING_SLOT_CLIP_DIST1) && is_compact) {
1588 const_index += component;
1589 component = 0;
1590
1591 if (const_index >= 4) {
1592 const_index -= 4;
1593 param++;
1594 }
1595 }
1596
1597 if (!is_patch) {
1598 stride = get_tcs_out_vertex_stride(ctx);
1599 dw_addr = get_tcs_out_current_patch_offset(ctx);
1600 } else {
1601 dw_addr = get_tcs_out_current_patch_data_offset(ctx);
1602 }
1603
1604 dw_addr = get_dw_address(ctx, dw_addr, param, const_index, is_compact, vertex_index, stride,
1605 param_index);
1606 buf_addr = get_tcs_tes_buffer_address_params(ctx, param, const_index, is_compact,
1607 vertex_index, param_index);
1608
1609 bool is_tess_factor = false;
1610 if (location == VARYING_SLOT_TESS_LEVEL_INNER ||
1611 location == VARYING_SLOT_TESS_LEVEL_OUTER)
1612 is_tess_factor = true;
1613
1614 unsigned base = is_compact ? const_index : 0;
1615 for (unsigned chan = 0; chan < 8; chan++) {
1616 if (!(writemask & (1 << chan)))
1617 continue;
1618 LLVMValueRef value = ac_llvm_extract_elem(&ctx->ac, src, chan - component);
1619 value = ac_to_integer(&ctx->ac, value);
1620 value = LLVMBuildZExtOrBitCast(ctx->ac.builder, value, ctx->ac.i32, "");
1621
1622 if (store_lds || is_tess_factor) {
1623 LLVMValueRef dw_addr_chan =
1624 LLVMBuildAdd(ctx->ac.builder, dw_addr,
1625 LLVMConstInt(ctx->ac.i32, chan, false), "");
1626 ac_lds_store(&ctx->ac, dw_addr_chan, value);
1627 }
1628
1629 if (!is_tess_factor && writemask != 0xF)
1630 ac_build_buffer_store_dword(&ctx->ac, ctx->hs_ring_tess_offchip, value, 1,
1631 buf_addr, ctx->oc_lds,
1632 4 * (base + chan), ac_glc, false);
1633 }
1634
1635 if (writemask == 0xF) {
1636 ac_build_buffer_store_dword(&ctx->ac, ctx->hs_ring_tess_offchip, src, 4,
1637 buf_addr, ctx->oc_lds,
1638 (base * 4), ac_glc, false);
1639 }
1640 }
1641
1642 static LLVMValueRef
1643 load_tes_input(struct ac_shader_abi *abi,
1644 LLVMTypeRef type,
1645 LLVMValueRef vertex_index,
1646 LLVMValueRef param_index,
1647 unsigned const_index,
1648 unsigned location,
1649 unsigned driver_location,
1650 unsigned component,
1651 unsigned num_components,
1652 bool is_patch,
1653 bool is_compact,
1654 bool load_input)
1655 {
1656 struct radv_shader_context *ctx = radv_shader_context_from_abi(abi);
1657 LLVMValueRef buf_addr;
1658 LLVMValueRef result;
1659 unsigned param = shader_io_get_unique_index(location);
1660
1661 if ((location == VARYING_SLOT_CLIP_DIST0 || location == VARYING_SLOT_CLIP_DIST1) && is_compact) {
1662 const_index += component;
1663 component = 0;
1664 if (const_index >= 4) {
1665 const_index -= 4;
1666 param++;
1667 }
1668 }
1669
1670 buf_addr = get_tcs_tes_buffer_address_params(ctx, param, const_index,
1671 is_compact, vertex_index, param_index);
1672
1673 LLVMValueRef comp_offset = LLVMConstInt(ctx->ac.i32, component * 4, false);
1674 buf_addr = LLVMBuildAdd(ctx->ac.builder, buf_addr, comp_offset, "");
1675
1676 result = ac_build_buffer_load(&ctx->ac, ctx->hs_ring_tess_offchip, num_components, NULL,
1677 buf_addr, ctx->oc_lds, is_compact ? (4 * const_index) : 0, ac_glc, true, false);
1678 result = ac_trim_vector(&ctx->ac, result, num_components);
1679 return result;
1680 }
1681
1682 static LLVMValueRef
1683 load_gs_input(struct ac_shader_abi *abi,
1684 unsigned location,
1685 unsigned driver_location,
1686 unsigned component,
1687 unsigned num_components,
1688 unsigned vertex_index,
1689 unsigned const_index,
1690 LLVMTypeRef type)
1691 {
1692 struct radv_shader_context *ctx = radv_shader_context_from_abi(abi);
1693 LLVMValueRef vtx_offset;
1694 unsigned param, vtx_offset_param;
1695 LLVMValueRef value[4], result;
1696
1697 vtx_offset_param = vertex_index;
1698 assert(vtx_offset_param < 6);
1699 vtx_offset = LLVMBuildMul(ctx->ac.builder, ctx->gs_vtx_offset[vtx_offset_param],
1700 LLVMConstInt(ctx->ac.i32, 4, false), "");
1701
1702 param = shader_io_get_unique_index(location);
1703
1704 for (unsigned i = component; i < num_components + component; i++) {
1705 if (ctx->ac.chip_class >= GFX9) {
1706 LLVMValueRef dw_addr = ctx->gs_vtx_offset[vtx_offset_param];
1707 dw_addr = LLVMBuildAdd(ctx->ac.builder, dw_addr,
1708 LLVMConstInt(ctx->ac.i32, param * 4 + i + const_index, 0), "");
1709 value[i] = ac_lds_load(&ctx->ac, dw_addr);
1710 } else {
1711 LLVMValueRef soffset =
1712 LLVMConstInt(ctx->ac.i32,
1713 (param * 4 + i + const_index) * 256,
1714 false);
1715
1716 value[i] = ac_build_buffer_load(&ctx->ac,
1717 ctx->esgs_ring, 1,
1718 ctx->ac.i32_0,
1719 vtx_offset, soffset,
1720 0, ac_glc, true, false);
1721 }
1722
1723 if (ac_get_type_size(type) == 2) {
1724 value[i] = LLVMBuildBitCast(ctx->ac.builder, value[i], ctx->ac.i32, "");
1725 value[i] = LLVMBuildTrunc(ctx->ac.builder, value[i], ctx->ac.i16, "");
1726 }
1727 value[i] = LLVMBuildBitCast(ctx->ac.builder, value[i], type, "");
1728 }
1729 result = ac_build_varying_gather_values(&ctx->ac, value, num_components, component);
1730 result = ac_to_integer(&ctx->ac, result);
1731 return result;
1732 }
1733
1734
1735 static void radv_emit_kill(struct ac_shader_abi *abi, LLVMValueRef visible)
1736 {
1737 struct radv_shader_context *ctx = radv_shader_context_from_abi(abi);
1738 ac_build_kill_if_false(&ctx->ac, visible);
1739 }
1740
1741 static LLVMValueRef lookup_interp_param(struct ac_shader_abi *abi,
1742 enum glsl_interp_mode interp, unsigned location)
1743 {
1744 struct radv_shader_context *ctx = radv_shader_context_from_abi(abi);
1745
1746 switch (interp) {
1747 case INTERP_MODE_FLAT:
1748 default:
1749 return NULL;
1750 case INTERP_MODE_SMOOTH:
1751 case INTERP_MODE_NONE:
1752 if (location == INTERP_CENTER)
1753 return ctx->persp_center;
1754 else if (location == INTERP_CENTROID)
1755 return ctx->persp_centroid;
1756 else if (location == INTERP_SAMPLE)
1757 return ctx->persp_sample;
1758 break;
1759 case INTERP_MODE_NOPERSPECTIVE:
1760 if (location == INTERP_CENTER)
1761 return ctx->linear_center;
1762 else if (location == INTERP_CENTROID)
1763 return ctx->linear_centroid;
1764 else if (location == INTERP_SAMPLE)
1765 return ctx->linear_sample;
1766 break;
1767 }
1768 return NULL;
1769 }
1770
1771 static uint32_t
1772 radv_get_sample_pos_offset(uint32_t num_samples)
1773 {
1774 uint32_t sample_pos_offset = 0;
1775
1776 switch (num_samples) {
1777 case 2:
1778 sample_pos_offset = 1;
1779 break;
1780 case 4:
1781 sample_pos_offset = 3;
1782 break;
1783 case 8:
1784 sample_pos_offset = 7;
1785 break;
1786 default:
1787 break;
1788 }
1789 return sample_pos_offset;
1790 }
1791
1792 static LLVMValueRef load_sample_position(struct ac_shader_abi *abi,
1793 LLVMValueRef sample_id)
1794 {
1795 struct radv_shader_context *ctx = radv_shader_context_from_abi(abi);
1796
1797 LLVMValueRef result;
1798 LLVMValueRef index = LLVMConstInt(ctx->ac.i32, RING_PS_SAMPLE_POSITIONS, false);
1799 LLVMValueRef ptr = LLVMBuildGEP(ctx->ac.builder, ctx->ring_offsets, &index, 1, "");
1800
1801 ptr = LLVMBuildBitCast(ctx->ac.builder, ptr,
1802 ac_array_in_const_addr_space(ctx->ac.v2f32), "");
1803
1804 uint32_t sample_pos_offset =
1805 radv_get_sample_pos_offset(ctx->options->key.fs.num_samples);
1806
1807 sample_id =
1808 LLVMBuildAdd(ctx->ac.builder, sample_id,
1809 LLVMConstInt(ctx->ac.i32, sample_pos_offset, false), "");
1810 result = ac_build_load_invariant(&ctx->ac, ptr, sample_id);
1811
1812 return result;
1813 }
1814
1815
1816 static LLVMValueRef load_sample_mask_in(struct ac_shader_abi *abi)
1817 {
1818 struct radv_shader_context *ctx = radv_shader_context_from_abi(abi);
1819 uint8_t log2_ps_iter_samples;
1820
1821 if (ctx->shader_info->info.ps.force_persample) {
1822 log2_ps_iter_samples =
1823 util_logbase2(ctx->options->key.fs.num_samples);
1824 } else {
1825 log2_ps_iter_samples = ctx->options->key.fs.log2_ps_iter_samples;
1826 }
1827
1828 /* The bit pattern matches that used by fixed function fragment
1829 * processing. */
1830 static const uint16_t ps_iter_masks[] = {
1831 0xffff, /* not used */
1832 0x5555,
1833 0x1111,
1834 0x0101,
1835 0x0001,
1836 };
1837 assert(log2_ps_iter_samples < ARRAY_SIZE(ps_iter_masks));
1838
1839 uint32_t ps_iter_mask = ps_iter_masks[log2_ps_iter_samples];
1840
1841 LLVMValueRef result, sample_id;
1842 sample_id = ac_unpack_param(&ctx->ac, abi->ancillary, 8, 4);
1843 sample_id = LLVMBuildShl(ctx->ac.builder, LLVMConstInt(ctx->ac.i32, ps_iter_mask, false), sample_id, "");
1844 result = LLVMBuildAnd(ctx->ac.builder, sample_id, abi->sample_coverage, "");
1845 return result;
1846 }
1847
1848
1849 static void
1850 visit_emit_vertex(struct ac_shader_abi *abi, unsigned stream, LLVMValueRef *addrs)
1851 {
1852 LLVMValueRef gs_next_vertex;
1853 LLVMValueRef can_emit;
1854 unsigned offset = 0;
1855 struct radv_shader_context *ctx = radv_shader_context_from_abi(abi);
1856
1857 /* Write vertex attribute values to GSVS ring */
1858 gs_next_vertex = LLVMBuildLoad(ctx->ac.builder,
1859 ctx->gs_next_vertex[stream],
1860 "");
1861
1862 /* If this thread has already emitted the declared maximum number of
1863 * vertices, kill it: excessive vertex emissions are not supposed to
1864 * have any effect, and GS threads have no externally observable
1865 * effects other than emitting vertices.
1866 */
1867 can_emit = LLVMBuildICmp(ctx->ac.builder, LLVMIntULT, gs_next_vertex,
1868 LLVMConstInt(ctx->ac.i32, ctx->gs_max_out_vertices, false), "");
1869 ac_build_kill_if_false(&ctx->ac, can_emit);
1870
1871 for (unsigned i = 0; i < AC_LLVM_MAX_OUTPUTS; ++i) {
1872 unsigned output_usage_mask =
1873 ctx->shader_info->info.gs.output_usage_mask[i];
1874 uint8_t output_stream =
1875 ctx->shader_info->info.gs.output_streams[i];
1876 LLVMValueRef *out_ptr = &addrs[i * 4];
1877 int length = util_last_bit(output_usage_mask);
1878
1879 if (!(ctx->output_mask & (1ull << i)) ||
1880 output_stream != stream)
1881 continue;
1882
1883 for (unsigned j = 0; j < length; j++) {
1884 if (!(output_usage_mask & (1 << j)))
1885 continue;
1886
1887 LLVMValueRef out_val = LLVMBuildLoad(ctx->ac.builder,
1888 out_ptr[j], "");
1889 LLVMValueRef voffset =
1890 LLVMConstInt(ctx->ac.i32, offset *
1891 ctx->gs_max_out_vertices, false);
1892
1893 offset++;
1894
1895 voffset = LLVMBuildAdd(ctx->ac.builder, voffset, gs_next_vertex, "");
1896 voffset = LLVMBuildMul(ctx->ac.builder, voffset, LLVMConstInt(ctx->ac.i32, 4, false), "");
1897
1898 out_val = ac_to_integer(&ctx->ac, out_val);
1899 out_val = LLVMBuildZExtOrBitCast(ctx->ac.builder, out_val, ctx->ac.i32, "");
1900
1901 ac_build_buffer_store_dword(&ctx->ac,
1902 ctx->gsvs_ring[stream],
1903 out_val, 1,
1904 voffset, ctx->gs2vs_offset, 0,
1905 ac_glc | ac_slc, true);
1906 }
1907 }
1908
1909 gs_next_vertex = LLVMBuildAdd(ctx->ac.builder, gs_next_vertex,
1910 ctx->ac.i32_1, "");
1911 LLVMBuildStore(ctx->ac.builder, gs_next_vertex, ctx->gs_next_vertex[stream]);
1912
1913 ac_build_sendmsg(&ctx->ac,
1914 AC_SENDMSG_GS_OP_EMIT | AC_SENDMSG_GS | (stream << 8),
1915 ctx->gs_wave_id);
1916 }
1917
1918 static void
1919 visit_end_primitive(struct ac_shader_abi *abi, unsigned stream)
1920 {
1921 struct radv_shader_context *ctx = radv_shader_context_from_abi(abi);
1922 ac_build_sendmsg(&ctx->ac, AC_SENDMSG_GS_OP_CUT | AC_SENDMSG_GS | (stream << 8), ctx->gs_wave_id);
1923 }
1924
1925 static LLVMValueRef
1926 load_tess_coord(struct ac_shader_abi *abi)
1927 {
1928 struct radv_shader_context *ctx = radv_shader_context_from_abi(abi);
1929
1930 LLVMValueRef coord[4] = {
1931 ctx->tes_u,
1932 ctx->tes_v,
1933 ctx->ac.f32_0,
1934 ctx->ac.f32_0,
1935 };
1936
1937 if (ctx->tes_primitive_mode == GL_TRIANGLES)
1938 coord[2] = LLVMBuildFSub(ctx->ac.builder, ctx->ac.f32_1,
1939 LLVMBuildFAdd(ctx->ac.builder, coord[0], coord[1], ""), "");
1940
1941 return ac_build_gather_values(&ctx->ac, coord, 3);
1942 }
1943
1944 static LLVMValueRef
1945 load_patch_vertices_in(struct ac_shader_abi *abi)
1946 {
1947 struct radv_shader_context *ctx = radv_shader_context_from_abi(abi);
1948 return LLVMConstInt(ctx->ac.i32, ctx->options->key.tcs.input_vertices, false);
1949 }
1950
1951
1952 static LLVMValueRef radv_load_base_vertex(struct ac_shader_abi *abi)
1953 {
1954 return abi->base_vertex;
1955 }
1956
1957 static LLVMValueRef radv_load_ssbo(struct ac_shader_abi *abi,
1958 LLVMValueRef buffer_ptr, bool write)
1959 {
1960 struct radv_shader_context *ctx = radv_shader_context_from_abi(abi);
1961 LLVMValueRef result;
1962
1963 LLVMSetMetadata(buffer_ptr, ctx->ac.uniform_md_kind, ctx->ac.empty_md);
1964
1965 result = LLVMBuildLoad(ctx->ac.builder, buffer_ptr, "");
1966 LLVMSetMetadata(result, ctx->ac.invariant_load_md_kind, ctx->ac.empty_md);
1967
1968 return result;
1969 }
1970
1971 static LLVMValueRef radv_load_ubo(struct ac_shader_abi *abi, LLVMValueRef buffer_ptr)
1972 {
1973 struct radv_shader_context *ctx = radv_shader_context_from_abi(abi);
1974 LLVMValueRef result;
1975
1976 if (LLVMGetTypeKind(LLVMTypeOf(buffer_ptr)) != LLVMPointerTypeKind) {
1977 /* Do not load the descriptor for inlined uniform blocks. */
1978 return buffer_ptr;
1979 }
1980
1981 LLVMSetMetadata(buffer_ptr, ctx->ac.uniform_md_kind, ctx->ac.empty_md);
1982
1983 result = LLVMBuildLoad(ctx->ac.builder, buffer_ptr, "");
1984 LLVMSetMetadata(result, ctx->ac.invariant_load_md_kind, ctx->ac.empty_md);
1985
1986 return result;
1987 }
1988
1989 static LLVMValueRef radv_get_sampler_desc(struct ac_shader_abi *abi,
1990 unsigned descriptor_set,
1991 unsigned base_index,
1992 unsigned constant_index,
1993 LLVMValueRef index,
1994 enum ac_descriptor_type desc_type,
1995 bool image, bool write,
1996 bool bindless)
1997 {
1998 struct radv_shader_context *ctx = radv_shader_context_from_abi(abi);
1999 LLVMValueRef list = ctx->descriptor_sets[descriptor_set];
2000 struct radv_descriptor_set_layout *layout = ctx->options->layout->set[descriptor_set].layout;
2001 struct radv_descriptor_set_binding_layout *binding = layout->binding + base_index;
2002 unsigned offset = binding->offset;
2003 unsigned stride = binding->size;
2004 unsigned type_size;
2005 LLVMBuilderRef builder = ctx->ac.builder;
2006 LLVMTypeRef type;
2007
2008 assert(base_index < layout->binding_count);
2009
2010 switch (desc_type) {
2011 case AC_DESC_IMAGE:
2012 type = ctx->ac.v8i32;
2013 type_size = 32;
2014 break;
2015 case AC_DESC_FMASK:
2016 type = ctx->ac.v8i32;
2017 offset += 32;
2018 type_size = 32;
2019 break;
2020 case AC_DESC_SAMPLER:
2021 type = ctx->ac.v4i32;
2022 if (binding->type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER) {
2023 offset += radv_combined_image_descriptor_sampler_offset(binding);
2024 }
2025
2026 type_size = 16;
2027 break;
2028 case AC_DESC_BUFFER:
2029 type = ctx->ac.v4i32;
2030 type_size = 16;
2031 break;
2032 case AC_DESC_PLANE_0:
2033 case AC_DESC_PLANE_1:
2034 case AC_DESC_PLANE_2:
2035 type = ctx->ac.v8i32;
2036 type_size = 32;
2037 offset += 32 * (desc_type - AC_DESC_PLANE_0);
2038 break;
2039 default:
2040 unreachable("invalid desc_type\n");
2041 }
2042
2043 offset += constant_index * stride;
2044
2045 if (desc_type == AC_DESC_SAMPLER && binding->immutable_samplers_offset &&
2046 (!index || binding->immutable_samplers_equal)) {
2047 if (binding->immutable_samplers_equal)
2048 constant_index = 0;
2049
2050 const uint32_t *samplers = radv_immutable_samplers(layout, binding);
2051
2052 LLVMValueRef constants[] = {
2053 LLVMConstInt(ctx->ac.i32, samplers[constant_index * 4 + 0], 0),
2054 LLVMConstInt(ctx->ac.i32, samplers[constant_index * 4 + 1], 0),
2055 LLVMConstInt(ctx->ac.i32, samplers[constant_index * 4 + 2], 0),
2056 LLVMConstInt(ctx->ac.i32, samplers[constant_index * 4 + 3], 0),
2057 };
2058 return ac_build_gather_values(&ctx->ac, constants, 4);
2059 }
2060
2061 assert(stride % type_size == 0);
2062
2063 LLVMValueRef adjusted_index = index;
2064 if (!adjusted_index)
2065 adjusted_index = ctx->ac.i32_0;
2066
2067 adjusted_index = LLVMBuildMul(builder, adjusted_index, LLVMConstInt(ctx->ac.i32, stride / type_size, 0), "");
2068
2069 LLVMValueRef val_offset = LLVMConstInt(ctx->ac.i32, offset, 0);
2070 list = LLVMBuildGEP(builder, list, &val_offset, 1, "");
2071 list = LLVMBuildPointerCast(builder, list,
2072 ac_array_in_const32_addr_space(type), "");
2073
2074 LLVMValueRef descriptor = ac_build_load_to_sgpr(&ctx->ac, list, adjusted_index);
2075
2076 /* 3 plane formats always have same size and format for plane 1 & 2, so
2077 * use the tail from plane 1 so that we can store only the first 16 bytes
2078 * of the last plane. */
2079 if (desc_type == AC_DESC_PLANE_2) {
2080 LLVMValueRef descriptor2 = radv_get_sampler_desc(abi, descriptor_set, base_index, constant_index, index, AC_DESC_PLANE_1,image, write, bindless);
2081
2082 LLVMValueRef components[8];
2083 for (unsigned i = 0; i < 4; ++i)
2084 components[i] = ac_llvm_extract_elem(&ctx->ac, descriptor, i);
2085
2086 for (unsigned i = 4; i < 8; ++i)
2087 components[i] = ac_llvm_extract_elem(&ctx->ac, descriptor2, i);
2088 descriptor = ac_build_gather_values(&ctx->ac, components, 8);
2089 }
2090
2091 return descriptor;
2092 }
2093
2094 /* For 2_10_10_10 formats the alpha is handled as unsigned by pre-vega HW.
2095 * so we may need to fix it up. */
2096 static LLVMValueRef
2097 adjust_vertex_fetch_alpha(struct radv_shader_context *ctx,
2098 unsigned adjustment,
2099 LLVMValueRef alpha)
2100 {
2101 if (adjustment == RADV_ALPHA_ADJUST_NONE)
2102 return alpha;
2103
2104 LLVMValueRef c30 = LLVMConstInt(ctx->ac.i32, 30, 0);
2105
2106 alpha = LLVMBuildBitCast(ctx->ac.builder, alpha, ctx->ac.f32, "");
2107
2108 if (adjustment == RADV_ALPHA_ADJUST_SSCALED)
2109 alpha = LLVMBuildFPToUI(ctx->ac.builder, alpha, ctx->ac.i32, "");
2110 else
2111 alpha = ac_to_integer(&ctx->ac, alpha);
2112
2113 /* For the integer-like cases, do a natural sign extension.
2114 *
2115 * For the SNORM case, the values are 0.0, 0.333, 0.666, 1.0
2116 * and happen to contain 0, 1, 2, 3 as the two LSBs of the
2117 * exponent.
2118 */
2119 alpha = LLVMBuildShl(ctx->ac.builder, alpha,
2120 adjustment == RADV_ALPHA_ADJUST_SNORM ?
2121 LLVMConstInt(ctx->ac.i32, 7, 0) : c30, "");
2122 alpha = LLVMBuildAShr(ctx->ac.builder, alpha, c30, "");
2123
2124 /* Convert back to the right type. */
2125 if (adjustment == RADV_ALPHA_ADJUST_SNORM) {
2126 LLVMValueRef clamp;
2127 LLVMValueRef neg_one = LLVMConstReal(ctx->ac.f32, -1.0);
2128 alpha = LLVMBuildSIToFP(ctx->ac.builder, alpha, ctx->ac.f32, "");
2129 clamp = LLVMBuildFCmp(ctx->ac.builder, LLVMRealULT, alpha, neg_one, "");
2130 alpha = LLVMBuildSelect(ctx->ac.builder, clamp, neg_one, alpha, "");
2131 } else if (adjustment == RADV_ALPHA_ADJUST_SSCALED) {
2132 alpha = LLVMBuildSIToFP(ctx->ac.builder, alpha, ctx->ac.f32, "");
2133 }
2134
2135 return LLVMBuildBitCast(ctx->ac.builder, alpha, ctx->ac.i32, "");
2136 }
2137
2138 static unsigned
2139 get_num_channels_from_data_format(unsigned data_format)
2140 {
2141 switch (data_format) {
2142 case V_008F0C_BUF_DATA_FORMAT_8:
2143 case V_008F0C_BUF_DATA_FORMAT_16:
2144 case V_008F0C_BUF_DATA_FORMAT_32:
2145 return 1;
2146 case V_008F0C_BUF_DATA_FORMAT_8_8:
2147 case V_008F0C_BUF_DATA_FORMAT_16_16:
2148 case V_008F0C_BUF_DATA_FORMAT_32_32:
2149 return 2;
2150 case V_008F0C_BUF_DATA_FORMAT_10_11_11:
2151 case V_008F0C_BUF_DATA_FORMAT_11_11_10:
2152 case V_008F0C_BUF_DATA_FORMAT_32_32_32:
2153 return 3;
2154 case V_008F0C_BUF_DATA_FORMAT_8_8_8_8:
2155 case V_008F0C_BUF_DATA_FORMAT_10_10_10_2:
2156 case V_008F0C_BUF_DATA_FORMAT_2_10_10_10:
2157 case V_008F0C_BUF_DATA_FORMAT_16_16_16_16:
2158 case V_008F0C_BUF_DATA_FORMAT_32_32_32_32:
2159 return 4;
2160 default:
2161 break;
2162 }
2163
2164 return 4;
2165 }
2166
2167 static LLVMValueRef
2168 radv_fixup_vertex_input_fetches(struct radv_shader_context *ctx,
2169 LLVMValueRef value,
2170 unsigned num_channels,
2171 bool is_float)
2172 {
2173 LLVMValueRef zero = is_float ? ctx->ac.f32_0 : ctx->ac.i32_0;
2174 LLVMValueRef one = is_float ? ctx->ac.f32_1 : ctx->ac.i32_1;
2175 LLVMValueRef chan[4];
2176
2177 if (LLVMGetTypeKind(LLVMTypeOf(value)) == LLVMVectorTypeKind) {
2178 unsigned vec_size = LLVMGetVectorSize(LLVMTypeOf(value));
2179
2180 if (num_channels == 4 && num_channels == vec_size)
2181 return value;
2182
2183 num_channels = MIN2(num_channels, vec_size);
2184
2185 for (unsigned i = 0; i < num_channels; i++)
2186 chan[i] = ac_llvm_extract_elem(&ctx->ac, value, i);
2187 } else {
2188 if (num_channels) {
2189 assert(num_channels == 1);
2190 chan[0] = value;
2191 }
2192 }
2193
2194 for (unsigned i = num_channels; i < 4; i++) {
2195 chan[i] = i == 3 ? one : zero;
2196 chan[i] = ac_to_integer(&ctx->ac, chan[i]);
2197 }
2198
2199 return ac_build_gather_values(&ctx->ac, chan, 4);
2200 }
2201
2202 static void
2203 handle_vs_input_decl(struct radv_shader_context *ctx,
2204 struct nir_variable *variable)
2205 {
2206 LLVMValueRef t_list_ptr = ctx->vertex_buffers;
2207 LLVMValueRef t_offset;
2208 LLVMValueRef t_list;
2209 LLVMValueRef input;
2210 LLVMValueRef buffer_index;
2211 unsigned attrib_count = glsl_count_attribute_slots(variable->type, true);
2212 uint8_t input_usage_mask =
2213 ctx->shader_info->info.vs.input_usage_mask[variable->data.location];
2214 unsigned num_input_channels = util_last_bit(input_usage_mask);
2215
2216 variable->data.driver_location = variable->data.location * 4;
2217
2218 enum glsl_base_type type = glsl_get_base_type(variable->type);
2219 for (unsigned i = 0; i < attrib_count; ++i) {
2220 LLVMValueRef output[4];
2221 unsigned attrib_index = variable->data.location + i - VERT_ATTRIB_GENERIC0;
2222 unsigned attrib_format = ctx->options->key.vs.vertex_attribute_formats[attrib_index];
2223 unsigned data_format = attrib_format & 0x0f;
2224 unsigned num_format = (attrib_format >> 4) & 0x07;
2225 bool is_float = num_format != V_008F0C_BUF_NUM_FORMAT_UINT &&
2226 num_format != V_008F0C_BUF_NUM_FORMAT_SINT;
2227
2228 if (ctx->options->key.vs.instance_rate_inputs & (1u << attrib_index)) {
2229 uint32_t divisor = ctx->options->key.vs.instance_rate_divisors[attrib_index];
2230
2231 if (divisor) {
2232 buffer_index = ctx->abi.instance_id;
2233
2234 if (divisor != 1) {
2235 buffer_index = LLVMBuildUDiv(ctx->ac.builder, buffer_index,
2236 LLVMConstInt(ctx->ac.i32, divisor, 0), "");
2237 }
2238 } else {
2239 buffer_index = ctx->ac.i32_0;
2240 }
2241
2242 buffer_index = LLVMBuildAdd(ctx->ac.builder, ctx->abi.start_instance, buffer_index, "");
2243 } else
2244 buffer_index = LLVMBuildAdd(ctx->ac.builder, ctx->abi.vertex_id,
2245 ctx->abi.base_vertex, "");
2246
2247 /* Adjust the number of channels to load based on the vertex
2248 * attribute format.
2249 */
2250 unsigned num_format_channels = get_num_channels_from_data_format(data_format);
2251 unsigned num_channels = MIN2(num_input_channels, num_format_channels);
2252 unsigned attrib_binding = ctx->options->key.vs.vertex_attribute_bindings[attrib_index];
2253 unsigned attrib_offset = ctx->options->key.vs.vertex_attribute_offsets[attrib_index];
2254 unsigned attrib_stride = ctx->options->key.vs.vertex_attribute_strides[attrib_index];
2255
2256 if (ctx->options->key.vs.post_shuffle & (1 << attrib_index)) {
2257 /* Always load, at least, 3 channels for formats that
2258 * need to be shuffled because X<->Z.
2259 */
2260 num_channels = MAX2(num_channels, 3);
2261 }
2262
2263 if (attrib_stride != 0 && attrib_offset > attrib_stride) {
2264 LLVMValueRef buffer_offset =
2265 LLVMConstInt(ctx->ac.i32,
2266 attrib_offset / attrib_stride, false);
2267
2268 buffer_index = LLVMBuildAdd(ctx->ac.builder,
2269 buffer_index,
2270 buffer_offset, "");
2271
2272 attrib_offset = attrib_offset % attrib_stride;
2273 }
2274
2275 t_offset = LLVMConstInt(ctx->ac.i32, attrib_binding, false);
2276 t_list = ac_build_load_to_sgpr(&ctx->ac, t_list_ptr, t_offset);
2277
2278 input = ac_build_struct_tbuffer_load(&ctx->ac, t_list,
2279 buffer_index,
2280 LLVMConstInt(ctx->ac.i32, attrib_offset, false),
2281 ctx->ac.i32_0, ctx->ac.i32_0,
2282 num_channels,
2283 data_format, num_format, 0, true);
2284
2285 if (ctx->options->key.vs.post_shuffle & (1 << attrib_index)) {
2286 LLVMValueRef c[4];
2287 c[0] = ac_llvm_extract_elem(&ctx->ac, input, 2);
2288 c[1] = ac_llvm_extract_elem(&ctx->ac, input, 1);
2289 c[2] = ac_llvm_extract_elem(&ctx->ac, input, 0);
2290 c[3] = ac_llvm_extract_elem(&ctx->ac, input, 3);
2291
2292 input = ac_build_gather_values(&ctx->ac, c, 4);
2293 }
2294
2295 input = radv_fixup_vertex_input_fetches(ctx, input, num_channels,
2296 is_float);
2297
2298 for (unsigned chan = 0; chan < 4; chan++) {
2299 LLVMValueRef llvm_chan = LLVMConstInt(ctx->ac.i32, chan, false);
2300 output[chan] = LLVMBuildExtractElement(ctx->ac.builder, input, llvm_chan, "");
2301 if (type == GLSL_TYPE_FLOAT16) {
2302 output[chan] = LLVMBuildBitCast(ctx->ac.builder, output[chan], ctx->ac.f32, "");
2303 output[chan] = LLVMBuildFPTrunc(ctx->ac.builder, output[chan], ctx->ac.f16, "");
2304 }
2305 }
2306
2307 unsigned alpha_adjust = (ctx->options->key.vs.alpha_adjust >> (attrib_index * 2)) & 3;
2308 output[3] = adjust_vertex_fetch_alpha(ctx, alpha_adjust, output[3]);
2309
2310 for (unsigned chan = 0; chan < 4; chan++) {
2311 output[chan] = ac_to_integer(&ctx->ac, output[chan]);
2312 if (type == GLSL_TYPE_UINT16 || type == GLSL_TYPE_INT16)
2313 output[chan] = LLVMBuildTrunc(ctx->ac.builder, output[chan], ctx->ac.i16, "");
2314
2315 ctx->inputs[ac_llvm_reg_index_soa(variable->data.location + i, chan)] = output[chan];
2316 }
2317 }
2318 }
2319
2320 static void
2321 handle_vs_inputs(struct radv_shader_context *ctx,
2322 struct nir_shader *nir) {
2323 nir_foreach_variable(variable, &nir->inputs)
2324 handle_vs_input_decl(ctx, variable);
2325 }
2326
2327 static void
2328 prepare_interp_optimize(struct radv_shader_context *ctx,
2329 struct nir_shader *nir)
2330 {
2331 bool uses_center = false;
2332 bool uses_centroid = false;
2333 nir_foreach_variable(variable, &nir->inputs) {
2334 if (glsl_get_base_type(glsl_without_array(variable->type)) != GLSL_TYPE_FLOAT ||
2335 variable->data.sample)
2336 continue;
2337
2338 if (variable->data.centroid)
2339 uses_centroid = true;
2340 else
2341 uses_center = true;
2342 }
2343
2344 if (uses_center && uses_centroid) {
2345 LLVMValueRef sel = LLVMBuildICmp(ctx->ac.builder, LLVMIntSLT, ctx->abi.prim_mask, ctx->ac.i32_0, "");
2346 ctx->persp_centroid = LLVMBuildSelect(ctx->ac.builder, sel, ctx->persp_center, ctx->persp_centroid, "");
2347 ctx->linear_centroid = LLVMBuildSelect(ctx->ac.builder, sel, ctx->linear_center, ctx->linear_centroid, "");
2348 }
2349 }
2350
2351 static void
2352 scan_shader_output_decl(struct radv_shader_context *ctx,
2353 struct nir_variable *variable,
2354 struct nir_shader *shader,
2355 gl_shader_stage stage)
2356 {
2357 int idx = variable->data.location + variable->data.index;
2358 unsigned attrib_count = glsl_count_attribute_slots(variable->type, false);
2359 uint64_t mask_attribs;
2360
2361 variable->data.driver_location = idx * 4;
2362
2363 /* tess ctrl has it's own load/store paths for outputs */
2364 if (stage == MESA_SHADER_TESS_CTRL)
2365 return;
2366
2367 if (variable->data.compact) {
2368 unsigned component_count = variable->data.location_frac +
2369 glsl_get_length(variable->type);
2370 attrib_count = (component_count + 3) / 4;
2371 }
2372
2373 mask_attribs = ((1ull << attrib_count) - 1) << idx;
2374 if (stage == MESA_SHADER_VERTEX ||
2375 stage == MESA_SHADER_TESS_EVAL ||
2376 stage == MESA_SHADER_GEOMETRY) {
2377 if (idx == VARYING_SLOT_CLIP_DIST0) {
2378 if (stage == MESA_SHADER_VERTEX) {
2379 ctx->shader_info->vs.outinfo.clip_dist_mask = (1 << shader->info.clip_distance_array_size) - 1;
2380 ctx->shader_info->vs.outinfo.cull_dist_mask = (1 << shader->info.cull_distance_array_size) - 1;
2381 ctx->shader_info->vs.outinfo.cull_dist_mask <<= shader->info.clip_distance_array_size;
2382 }
2383 if (stage == MESA_SHADER_TESS_EVAL) {
2384 ctx->shader_info->tes.outinfo.clip_dist_mask = (1 << shader->info.clip_distance_array_size) - 1;
2385 ctx->shader_info->tes.outinfo.cull_dist_mask = (1 << shader->info.cull_distance_array_size) - 1;
2386 ctx->shader_info->tes.outinfo.cull_dist_mask <<= shader->info.clip_distance_array_size;
2387 }
2388 }
2389 }
2390
2391 ctx->output_mask |= mask_attribs;
2392 }
2393
2394
2395 /* Initialize arguments for the shader export intrinsic */
2396 static void
2397 si_llvm_init_export_args(struct radv_shader_context *ctx,
2398 LLVMValueRef *values,
2399 unsigned enabled_channels,
2400 unsigned target,
2401 struct ac_export_args *args)
2402 {
2403 /* Specify the channels that are enabled. */
2404 args->enabled_channels = enabled_channels;
2405
2406 /* Specify whether the EXEC mask represents the valid mask */
2407 args->valid_mask = 0;
2408
2409 /* Specify whether this is the last export */
2410 args->done = 0;
2411
2412 /* Specify the target we are exporting */
2413 args->target = target;
2414
2415 args->compr = false;
2416 args->out[0] = LLVMGetUndef(ctx->ac.f32);
2417 args->out[1] = LLVMGetUndef(ctx->ac.f32);
2418 args->out[2] = LLVMGetUndef(ctx->ac.f32);
2419 args->out[3] = LLVMGetUndef(ctx->ac.f32);
2420
2421 if (!values)
2422 return;
2423
2424 bool is_16bit = ac_get_type_size(LLVMTypeOf(values[0])) == 2;
2425 if (ctx->stage == MESA_SHADER_FRAGMENT) {
2426 unsigned index = target - V_008DFC_SQ_EXP_MRT;
2427 unsigned col_format = (ctx->options->key.fs.col_format >> (4 * index)) & 0xf;
2428 bool is_int8 = (ctx->options->key.fs.is_int8 >> index) & 1;
2429 bool is_int10 = (ctx->options->key.fs.is_int10 >> index) & 1;
2430 unsigned chan;
2431
2432 LLVMValueRef (*packf)(struct ac_llvm_context *ctx, LLVMValueRef args[2]) = NULL;
2433 LLVMValueRef (*packi)(struct ac_llvm_context *ctx, LLVMValueRef args[2],
2434 unsigned bits, bool hi) = NULL;
2435
2436 switch(col_format) {
2437 case V_028714_SPI_SHADER_ZERO:
2438 args->enabled_channels = 0; /* writemask */
2439 args->target = V_008DFC_SQ_EXP_NULL;
2440 break;
2441
2442 case V_028714_SPI_SHADER_32_R:
2443 args->enabled_channels = 1;
2444 args->out[0] = values[0];
2445 break;
2446
2447 case V_028714_SPI_SHADER_32_GR:
2448 args->enabled_channels = 0x3;
2449 args->out[0] = values[0];
2450 args->out[1] = values[1];
2451 break;
2452
2453 case V_028714_SPI_SHADER_32_AR:
2454 if (ctx->ac.chip_class >= GFX10) {
2455 args->enabled_channels = 0x3;
2456 args->out[0] = values[0];
2457 args->out[1] = values[3];
2458 } else {
2459 args->enabled_channels = 0x9;
2460 args->out[0] = values[0];
2461 args->out[3] = values[3];
2462 }
2463 break;
2464
2465 case V_028714_SPI_SHADER_FP16_ABGR:
2466 args->enabled_channels = 0x5;
2467 packf = ac_build_cvt_pkrtz_f16;
2468 if (is_16bit) {
2469 for (unsigned chan = 0; chan < 4; chan++)
2470 values[chan] = LLVMBuildFPExt(ctx->ac.builder,
2471 values[chan],
2472 ctx->ac.f32, "");
2473 }
2474 break;
2475
2476 case V_028714_SPI_SHADER_UNORM16_ABGR:
2477 args->enabled_channels = 0x5;
2478 packf = ac_build_cvt_pknorm_u16;
2479 break;
2480
2481 case V_028714_SPI_SHADER_SNORM16_ABGR:
2482 args->enabled_channels = 0x5;
2483 packf = ac_build_cvt_pknorm_i16;
2484 break;
2485
2486 case V_028714_SPI_SHADER_UINT16_ABGR:
2487 args->enabled_channels = 0x5;
2488 packi = ac_build_cvt_pk_u16;
2489 if (is_16bit) {
2490 for (unsigned chan = 0; chan < 4; chan++)
2491 values[chan] = LLVMBuildZExt(ctx->ac.builder,
2492 ac_to_integer(&ctx->ac, values[chan]),
2493 ctx->ac.i32, "");
2494 }
2495 break;
2496
2497 case V_028714_SPI_SHADER_SINT16_ABGR:
2498 args->enabled_channels = 0x5;
2499 packi = ac_build_cvt_pk_i16;
2500 if (is_16bit) {
2501 for (unsigned chan = 0; chan < 4; chan++)
2502 values[chan] = LLVMBuildSExt(ctx->ac.builder,
2503 ac_to_integer(&ctx->ac, values[chan]),
2504 ctx->ac.i32, "");
2505 }
2506 break;
2507
2508 default:
2509 case V_028714_SPI_SHADER_32_ABGR:
2510 memcpy(&args->out[0], values, sizeof(values[0]) * 4);
2511 break;
2512 }
2513
2514 /* Pack f16 or norm_i16/u16. */
2515 if (packf) {
2516 for (chan = 0; chan < 2; chan++) {
2517 LLVMValueRef pack_args[2] = {
2518 values[2 * chan],
2519 values[2 * chan + 1]
2520 };
2521 LLVMValueRef packed;
2522
2523 packed = packf(&ctx->ac, pack_args);
2524 args->out[chan] = ac_to_float(&ctx->ac, packed);
2525 }
2526 args->compr = 1; /* COMPR flag */
2527 }
2528
2529 /* Pack i16/u16. */
2530 if (packi) {
2531 for (chan = 0; chan < 2; chan++) {
2532 LLVMValueRef pack_args[2] = {
2533 ac_to_integer(&ctx->ac, values[2 * chan]),
2534 ac_to_integer(&ctx->ac, values[2 * chan + 1])
2535 };
2536 LLVMValueRef packed;
2537
2538 packed = packi(&ctx->ac, pack_args,
2539 is_int8 ? 8 : is_int10 ? 10 : 16,
2540 chan == 1);
2541 args->out[chan] = ac_to_float(&ctx->ac, packed);
2542 }
2543 args->compr = 1; /* COMPR flag */
2544 }
2545 return;
2546 }
2547
2548 if (is_16bit) {
2549 for (unsigned chan = 0; chan < 4; chan++) {
2550 values[chan] = LLVMBuildBitCast(ctx->ac.builder, values[chan], ctx->ac.i16, "");
2551 args->out[chan] = LLVMBuildZExt(ctx->ac.builder, values[chan], ctx->ac.i32, "");
2552 }
2553 } else
2554 memcpy(&args->out[0], values, sizeof(values[0]) * 4);
2555
2556 for (unsigned i = 0; i < 4; ++i)
2557 args->out[i] = ac_to_float(&ctx->ac, args->out[i]);
2558 }
2559
2560 static void
2561 radv_export_param(struct radv_shader_context *ctx, unsigned index,
2562 LLVMValueRef *values, unsigned enabled_channels)
2563 {
2564 struct ac_export_args args;
2565
2566 si_llvm_init_export_args(ctx, values, enabled_channels,
2567 V_008DFC_SQ_EXP_PARAM + index, &args);
2568 ac_build_export(&ctx->ac, &args);
2569 }
2570
2571 static LLVMValueRef
2572 radv_load_output(struct radv_shader_context *ctx, unsigned index, unsigned chan)
2573 {
2574 LLVMValueRef output =
2575 ctx->abi.outputs[ac_llvm_reg_index_soa(index, chan)];
2576
2577 return LLVMBuildLoad(ctx->ac.builder, output, "");
2578 }
2579
2580 static void
2581 radv_emit_stream_output(struct radv_shader_context *ctx,
2582 LLVMValueRef const *so_buffers,
2583 LLVMValueRef const *so_write_offsets,
2584 const struct radv_stream_output *output)
2585 {
2586 unsigned num_comps = util_bitcount(output->component_mask);
2587 unsigned loc = output->location;
2588 unsigned buf = output->buffer;
2589 unsigned offset = output->offset;
2590 unsigned start;
2591 LLVMValueRef out[4];
2592
2593 assert(num_comps && num_comps <= 4);
2594 if (!num_comps || num_comps > 4)
2595 return;
2596
2597 /* Get the first component. */
2598 start = ffs(output->component_mask) - 1;
2599
2600 /* Load the output as int. */
2601 for (int i = 0; i < num_comps; i++) {
2602 out[i] = ac_to_integer(&ctx->ac,
2603 radv_load_output(ctx, loc, start + i));
2604 }
2605
2606 /* Pack the output. */
2607 LLVMValueRef vdata = NULL;
2608
2609 switch (num_comps) {
2610 case 1: /* as i32 */
2611 vdata = out[0];
2612 break;
2613 case 2: /* as v2i32 */
2614 case 3: /* as v4i32 (aligned to 4) */
2615 out[3] = LLVMGetUndef(ctx->ac.i32);
2616 /* fall through */
2617 case 4: /* as v4i32 */
2618 vdata = ac_build_gather_values(&ctx->ac, out,
2619 !ac_has_vec3_support(ctx->ac.chip_class, false) ?
2620 util_next_power_of_two(num_comps) :
2621 num_comps);
2622 break;
2623 }
2624
2625 ac_build_buffer_store_dword(&ctx->ac, so_buffers[buf],
2626 vdata, num_comps, so_write_offsets[buf],
2627 ctx->ac.i32_0, offset,
2628 ac_glc | ac_slc, false);
2629 }
2630
2631 static void
2632 radv_emit_streamout(struct radv_shader_context *ctx, unsigned stream)
2633 {
2634 struct ac_build_if_state if_ctx;
2635 int i;
2636
2637 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
2638 assert(ctx->streamout_config);
2639 LLVMValueRef so_vtx_count =
2640 ac_build_bfe(&ctx->ac, ctx->streamout_config,
2641 LLVMConstInt(ctx->ac.i32, 16, false),
2642 LLVMConstInt(ctx->ac.i32, 7, false), false);
2643
2644 LLVMValueRef tid = ac_get_thread_id(&ctx->ac);
2645
2646 /* can_emit = tid < so_vtx_count; */
2647 LLVMValueRef can_emit = LLVMBuildICmp(ctx->ac.builder, LLVMIntULT,
2648 tid, so_vtx_count, "");
2649
2650 /* Emit the streamout code conditionally. This actually avoids
2651 * out-of-bounds buffer access. The hw tells us via the SGPR
2652 * (so_vtx_count) which threads are allowed to emit streamout data.
2653 */
2654 ac_nir_build_if(&if_ctx, ctx, can_emit);
2655 {
2656 /* The buffer offset is computed as follows:
2657 * ByteOffset = streamout_offset[buffer_id]*4 +
2658 * (streamout_write_index + thread_id)*stride[buffer_id] +
2659 * attrib_offset
2660 */
2661 LLVMValueRef so_write_index = ctx->streamout_write_idx;
2662
2663 /* Compute (streamout_write_index + thread_id). */
2664 so_write_index =
2665 LLVMBuildAdd(ctx->ac.builder, so_write_index, tid, "");
2666
2667 /* Load the descriptor and compute the write offset for each
2668 * enabled buffer.
2669 */
2670 LLVMValueRef so_write_offset[4] = {};
2671 LLVMValueRef so_buffers[4] = {};
2672 LLVMValueRef buf_ptr = ctx->streamout_buffers;
2673
2674 for (i = 0; i < 4; i++) {
2675 uint16_t stride = ctx->shader_info->info.so.strides[i];
2676
2677 if (!stride)
2678 continue;
2679
2680 LLVMValueRef offset =
2681 LLVMConstInt(ctx->ac.i32, i, false);
2682
2683 so_buffers[i] = ac_build_load_to_sgpr(&ctx->ac,
2684 buf_ptr, offset);
2685
2686 LLVMValueRef so_offset = ctx->streamout_offset[i];
2687
2688 so_offset = LLVMBuildMul(ctx->ac.builder, so_offset,
2689 LLVMConstInt(ctx->ac.i32, 4, false), "");
2690
2691 so_write_offset[i] =
2692 ac_build_imad(&ctx->ac, so_write_index,
2693 LLVMConstInt(ctx->ac.i32,
2694 stride * 4, false),
2695 so_offset);
2696 }
2697
2698 /* Write streamout data. */
2699 for (i = 0; i < ctx->shader_info->info.so.num_outputs; i++) {
2700 struct radv_stream_output *output =
2701 &ctx->shader_info->info.so.outputs[i];
2702
2703 if (stream != output->stream)
2704 continue;
2705
2706 radv_emit_stream_output(ctx, so_buffers,
2707 so_write_offset, output);
2708 }
2709 }
2710 ac_nir_build_endif(&if_ctx);
2711 }
2712
2713 static void
2714 handle_vs_outputs_post(struct radv_shader_context *ctx,
2715 bool export_prim_id, bool export_layer_id,
2716 bool export_clip_dists,
2717 struct radv_vs_output_info *outinfo)
2718 {
2719 uint32_t param_count = 0;
2720 unsigned target;
2721 unsigned pos_idx, num_pos_exports = 0;
2722 struct ac_export_args args, pos_args[4] = {};
2723 LLVMValueRef psize_value = NULL, layer_value = NULL, viewport_index_value = NULL;
2724 int i;
2725
2726 if (ctx->options->key.has_multiview_view_index) {
2727 LLVMValueRef* tmp_out = &ctx->abi.outputs[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER, 0)];
2728 if(!*tmp_out) {
2729 for(unsigned i = 0; i < 4; ++i)
2730 ctx->abi.outputs[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER, i)] =
2731 ac_build_alloca_undef(&ctx->ac, ctx->ac.f32, "");
2732 }
2733
2734 LLVMBuildStore(ctx->ac.builder, ac_to_float(&ctx->ac, ctx->abi.view_index), *tmp_out);
2735 ctx->output_mask |= 1ull << VARYING_SLOT_LAYER;
2736 }
2737
2738 memset(outinfo->vs_output_param_offset, AC_EXP_PARAM_UNDEFINED,
2739 sizeof(outinfo->vs_output_param_offset));
2740
2741 for(unsigned location = VARYING_SLOT_CLIP_DIST0; location <= VARYING_SLOT_CLIP_DIST1; ++location) {
2742 if (ctx->output_mask & (1ull << location)) {
2743 unsigned output_usage_mask, length;
2744 LLVMValueRef slots[4];
2745 unsigned j;
2746
2747 if (ctx->stage == MESA_SHADER_VERTEX &&
2748 !ctx->is_gs_copy_shader) {
2749 output_usage_mask =
2750 ctx->shader_info->info.vs.output_usage_mask[location];
2751 } else if (ctx->stage == MESA_SHADER_TESS_EVAL) {
2752 output_usage_mask =
2753 ctx->shader_info->info.tes.output_usage_mask[location];
2754 } else {
2755 assert(ctx->is_gs_copy_shader);
2756 output_usage_mask =
2757 ctx->shader_info->info.gs.output_usage_mask[location];
2758 }
2759
2760 length = util_last_bit(output_usage_mask);
2761
2762 for (j = 0; j < length; j++)
2763 slots[j] = ac_to_float(&ctx->ac, radv_load_output(ctx, location, j));
2764
2765 for (i = length; i < 4; i++)
2766 slots[i] = LLVMGetUndef(ctx->ac.f32);
2767
2768 target = V_008DFC_SQ_EXP_POS + 2 + (location - VARYING_SLOT_CLIP_DIST0);
2769 si_llvm_init_export_args(ctx, &slots[0], 0xf, target, &args);
2770 memcpy(&pos_args[target - V_008DFC_SQ_EXP_POS],
2771 &args, sizeof(args));
2772
2773 if (export_clip_dists) {
2774 /* Export the clip/cull distances values to the next stage. */
2775 radv_export_param(ctx, param_count, &slots[0], 0xf);
2776 outinfo->vs_output_param_offset[location] = param_count++;
2777 }
2778 }
2779 }
2780
2781 LLVMValueRef pos_values[4] = {ctx->ac.f32_0, ctx->ac.f32_0, ctx->ac.f32_0, ctx->ac.f32_1};
2782 if (ctx->output_mask & (1ull << VARYING_SLOT_POS)) {
2783 for (unsigned j = 0; j < 4; j++)
2784 pos_values[j] = radv_load_output(ctx, VARYING_SLOT_POS, j);
2785 }
2786 si_llvm_init_export_args(ctx, pos_values, 0xf, V_008DFC_SQ_EXP_POS, &pos_args[0]);
2787
2788 if (ctx->output_mask & (1ull << VARYING_SLOT_PSIZ)) {
2789 outinfo->writes_pointsize = true;
2790 psize_value = radv_load_output(ctx, VARYING_SLOT_PSIZ, 0);
2791 }
2792
2793 if (ctx->output_mask & (1ull << VARYING_SLOT_LAYER)) {
2794 outinfo->writes_layer = true;
2795 layer_value = radv_load_output(ctx, VARYING_SLOT_LAYER, 0);
2796 }
2797
2798 if (ctx->output_mask & (1ull << VARYING_SLOT_VIEWPORT)) {
2799 outinfo->writes_viewport_index = true;
2800 viewport_index_value = radv_load_output(ctx, VARYING_SLOT_VIEWPORT, 0);
2801 }
2802
2803 if (ctx->shader_info->info.so.num_outputs &&
2804 !ctx->is_gs_copy_shader) {
2805 /* The GS copy shader emission already emits streamout. */
2806 radv_emit_streamout(ctx, 0);
2807 }
2808
2809 if (outinfo->writes_pointsize ||
2810 outinfo->writes_layer ||
2811 outinfo->writes_viewport_index) {
2812 pos_args[1].enabled_channels = ((outinfo->writes_pointsize == true ? 1 : 0) |
2813 (outinfo->writes_layer == true ? 4 : 0));
2814 pos_args[1].valid_mask = 0;
2815 pos_args[1].done = 0;
2816 pos_args[1].target = V_008DFC_SQ_EXP_POS + 1;
2817 pos_args[1].compr = 0;
2818 pos_args[1].out[0] = ctx->ac.f32_0; /* X */
2819 pos_args[1].out[1] = ctx->ac.f32_0; /* Y */
2820 pos_args[1].out[2] = ctx->ac.f32_0; /* Z */
2821 pos_args[1].out[3] = ctx->ac.f32_0; /* W */
2822
2823 if (outinfo->writes_pointsize == true)
2824 pos_args[1].out[0] = psize_value;
2825 if (outinfo->writes_layer == true)
2826 pos_args[1].out[2] = layer_value;
2827 if (outinfo->writes_viewport_index == true) {
2828 if (ctx->options->chip_class >= GFX9) {
2829 /* GFX9 has the layer in out.z[10:0] and the viewport
2830 * index in out.z[19:16].
2831 */
2832 LLVMValueRef v = viewport_index_value;
2833 v = ac_to_integer(&ctx->ac, v);
2834 v = LLVMBuildShl(ctx->ac.builder, v,
2835 LLVMConstInt(ctx->ac.i32, 16, false),
2836 "");
2837 v = LLVMBuildOr(ctx->ac.builder, v,
2838 ac_to_integer(&ctx->ac, pos_args[1].out[2]), "");
2839
2840 pos_args[1].out[2] = ac_to_float(&ctx->ac, v);
2841 pos_args[1].enabled_channels |= 1 << 2;
2842 } else {
2843 pos_args[1].out[3] = viewport_index_value;
2844 pos_args[1].enabled_channels |= 1 << 3;
2845 }
2846 }
2847 }
2848 for (i = 0; i < 4; i++) {
2849 if (pos_args[i].out[0])
2850 num_pos_exports++;
2851 }
2852
2853 /* Navi10-14 skip POS0 exports if EXEC=0 and DONE=0, causing a hang.
2854 * Setting valid_mask=1 prevents it and has no other effect.
2855 */
2856 if (ctx->ac.family == CHIP_NAVI10 ||
2857 ctx->ac.family == CHIP_NAVI12 ||
2858 ctx->ac.family == CHIP_NAVI14)
2859 pos_args[0].valid_mask = 1;
2860
2861 pos_idx = 0;
2862 for (i = 0; i < 4; i++) {
2863 if (!pos_args[i].out[0])
2864 continue;
2865
2866 /* Specify the target we are exporting */
2867 pos_args[i].target = V_008DFC_SQ_EXP_POS + pos_idx++;
2868 if (pos_idx == num_pos_exports)
2869 pos_args[i].done = 1;
2870 ac_build_export(&ctx->ac, &pos_args[i]);
2871 }
2872
2873 for (unsigned i = 0; i < AC_LLVM_MAX_OUTPUTS; ++i) {
2874 LLVMValueRef values[4];
2875 if (!(ctx->output_mask & (1ull << i)))
2876 continue;
2877
2878 if (i != VARYING_SLOT_LAYER &&
2879 i != VARYING_SLOT_PRIMITIVE_ID &&
2880 i < VARYING_SLOT_VAR0)
2881 continue;
2882
2883 for (unsigned j = 0; j < 4; j++)
2884 values[j] = ac_to_float(&ctx->ac, radv_load_output(ctx, i, j));
2885
2886 unsigned output_usage_mask;
2887
2888 if (ctx->stage == MESA_SHADER_VERTEX &&
2889 !ctx->is_gs_copy_shader) {
2890 output_usage_mask =
2891 ctx->shader_info->info.vs.output_usage_mask[i];
2892 } else if (ctx->stage == MESA_SHADER_TESS_EVAL) {
2893 output_usage_mask =
2894 ctx->shader_info->info.tes.output_usage_mask[i];
2895 } else {
2896 assert(ctx->is_gs_copy_shader);
2897 output_usage_mask =
2898 ctx->shader_info->info.gs.output_usage_mask[i];
2899 }
2900
2901 radv_export_param(ctx, param_count, values, output_usage_mask);
2902
2903 outinfo->vs_output_param_offset[i] = param_count++;
2904 }
2905
2906 if (export_prim_id) {
2907 LLVMValueRef values[4];
2908
2909 values[0] = ctx->vs_prim_id;
2910 for (unsigned j = 1; j < 4; j++)
2911 values[j] = ctx->ac.f32_0;
2912
2913 radv_export_param(ctx, param_count, values, 0x1);
2914
2915 outinfo->vs_output_param_offset[VARYING_SLOT_PRIMITIVE_ID] = param_count++;
2916 outinfo->export_prim_id = true;
2917 }
2918
2919 if (export_layer_id && layer_value) {
2920 LLVMValueRef values[4];
2921
2922 values[0] = layer_value;
2923 for (unsigned j = 1; j < 4; j++)
2924 values[j] = ctx->ac.f32_0;
2925
2926 radv_export_param(ctx, param_count, values, 0x1);
2927
2928 outinfo->vs_output_param_offset[VARYING_SLOT_LAYER] = param_count++;
2929 }
2930
2931 outinfo->pos_exports = num_pos_exports;
2932 outinfo->param_exports = param_count;
2933 }
2934
2935 static void
2936 handle_es_outputs_post(struct radv_shader_context *ctx,
2937 struct radv_es_output_info *outinfo)
2938 {
2939 int j;
2940 uint64_t max_output_written = 0;
2941 LLVMValueRef lds_base = NULL;
2942
2943 for (unsigned i = 0; i < AC_LLVM_MAX_OUTPUTS; ++i) {
2944 int param_index;
2945
2946 if (!(ctx->output_mask & (1ull << i)))
2947 continue;
2948
2949 param_index = shader_io_get_unique_index(i);
2950
2951 max_output_written = MAX2(param_index, max_output_written);
2952 }
2953
2954 outinfo->esgs_itemsize = (max_output_written + 1) * 16;
2955
2956 if (ctx->ac.chip_class >= GFX9) {
2957 unsigned itemsize_dw = outinfo->esgs_itemsize / 4;
2958 LLVMValueRef vertex_idx = ac_get_thread_id(&ctx->ac);
2959 LLVMValueRef wave_idx = ac_unpack_param(&ctx->ac, ctx->merged_wave_info, 24, 4);
2960 vertex_idx = LLVMBuildOr(ctx->ac.builder, vertex_idx,
2961 LLVMBuildMul(ctx->ac.builder, wave_idx,
2962 LLVMConstInt(ctx->ac.i32, 64, false), ""), "");
2963 lds_base = LLVMBuildMul(ctx->ac.builder, vertex_idx,
2964 LLVMConstInt(ctx->ac.i32, itemsize_dw, 0), "");
2965 }
2966
2967 for (unsigned i = 0; i < AC_LLVM_MAX_OUTPUTS; ++i) {
2968 LLVMValueRef dw_addr = NULL;
2969 LLVMValueRef *out_ptr = &ctx->abi.outputs[i * 4];
2970 unsigned output_usage_mask;
2971 int param_index;
2972
2973 if (!(ctx->output_mask & (1ull << i)))
2974 continue;
2975
2976 if (ctx->stage == MESA_SHADER_VERTEX) {
2977 output_usage_mask =
2978 ctx->shader_info->info.vs.output_usage_mask[i];
2979 } else {
2980 assert(ctx->stage == MESA_SHADER_TESS_EVAL);
2981 output_usage_mask =
2982 ctx->shader_info->info.tes.output_usage_mask[i];
2983 }
2984
2985 param_index = shader_io_get_unique_index(i);
2986
2987 if (lds_base) {
2988 dw_addr = LLVMBuildAdd(ctx->ac.builder, lds_base,
2989 LLVMConstInt(ctx->ac.i32, param_index * 4, false),
2990 "");
2991 }
2992
2993 for (j = 0; j < 4; j++) {
2994 if (!(output_usage_mask & (1 << j)))
2995 continue;
2996
2997 LLVMValueRef out_val = LLVMBuildLoad(ctx->ac.builder, out_ptr[j], "");
2998 out_val = ac_to_integer(&ctx->ac, out_val);
2999 out_val = LLVMBuildZExtOrBitCast(ctx->ac.builder, out_val, ctx->ac.i32, "");
3000
3001 if (ctx->ac.chip_class >= GFX9) {
3002 LLVMValueRef dw_addr_offset =
3003 LLVMBuildAdd(ctx->ac.builder, dw_addr,
3004 LLVMConstInt(ctx->ac.i32,
3005 j, false), "");
3006
3007 ac_lds_store(&ctx->ac, dw_addr_offset, out_val);
3008 } else {
3009 ac_build_buffer_store_dword(&ctx->ac,
3010 ctx->esgs_ring,
3011 out_val, 1,
3012 NULL, ctx->es2gs_offset,
3013 (4 * param_index + j) * 4,
3014 ac_glc | ac_slc, true);
3015 }
3016 }
3017 }
3018 }
3019
3020 static void
3021 handle_ls_outputs_post(struct radv_shader_context *ctx)
3022 {
3023 LLVMValueRef vertex_id = ctx->rel_auto_id;
3024 uint32_t num_tcs_inputs = util_last_bit64(ctx->shader_info->info.vs.ls_outputs_written);
3025 LLVMValueRef vertex_dw_stride = LLVMConstInt(ctx->ac.i32, num_tcs_inputs * 4, false);
3026 LLVMValueRef base_dw_addr = LLVMBuildMul(ctx->ac.builder, vertex_id,
3027 vertex_dw_stride, "");
3028
3029 for (unsigned i = 0; i < AC_LLVM_MAX_OUTPUTS; ++i) {
3030 LLVMValueRef *out_ptr = &ctx->abi.outputs[i * 4];
3031
3032 if (!(ctx->output_mask & (1ull << i)))
3033 continue;
3034
3035 int param = shader_io_get_unique_index(i);
3036 LLVMValueRef dw_addr = LLVMBuildAdd(ctx->ac.builder, base_dw_addr,
3037 LLVMConstInt(ctx->ac.i32, param * 4, false),
3038 "");
3039 for (unsigned j = 0; j < 4; j++) {
3040 LLVMValueRef value = LLVMBuildLoad(ctx->ac.builder, out_ptr[j], "");
3041 value = ac_to_integer(&ctx->ac, value);
3042 value = LLVMBuildZExtOrBitCast(ctx->ac.builder, value, ctx->ac.i32, "");
3043 ac_lds_store(&ctx->ac, dw_addr, value);
3044 dw_addr = LLVMBuildAdd(ctx->ac.builder, dw_addr, ctx->ac.i32_1, "");
3045 }
3046 }
3047 }
3048
3049 static LLVMValueRef get_wave_id_in_tg(struct radv_shader_context *ctx)
3050 {
3051 return ac_unpack_param(&ctx->ac, ctx->merged_wave_info, 24, 4);
3052 }
3053
3054 static LLVMValueRef ngg_get_vtx_cnt(struct radv_shader_context *ctx)
3055 {
3056 return ac_build_bfe(&ctx->ac, ctx->gs_tg_info,
3057 LLVMConstInt(ctx->ac.i32, 12, false),
3058 LLVMConstInt(ctx->ac.i32, 9, false),
3059 false);
3060 }
3061
3062 static LLVMValueRef ngg_get_prim_cnt(struct radv_shader_context *ctx)
3063 {
3064 return ac_build_bfe(&ctx->ac, ctx->gs_tg_info,
3065 LLVMConstInt(ctx->ac.i32, 22, false),
3066 LLVMConstInt(ctx->ac.i32, 9, false),
3067 false);
3068 }
3069
3070 /* Send GS Alloc Req message from the first wave of the group to SPI.
3071 * Message payload is:
3072 * - bits 0..10: vertices in group
3073 * - bits 12..22: primitives in group
3074 */
3075 static void build_sendmsg_gs_alloc_req(struct radv_shader_context *ctx,
3076 LLVMValueRef vtx_cnt,
3077 LLVMValueRef prim_cnt)
3078 {
3079 LLVMBuilderRef builder = ctx->ac.builder;
3080 LLVMValueRef tmp;
3081
3082 tmp = LLVMBuildICmp(builder, LLVMIntEQ, get_wave_id_in_tg(ctx), ctx->ac.i32_0, "");
3083 ac_build_ifcc(&ctx->ac, tmp, 5020);
3084
3085 tmp = LLVMBuildShl(builder, prim_cnt, LLVMConstInt(ctx->ac.i32, 12, false),"");
3086 tmp = LLVMBuildOr(builder, tmp, vtx_cnt, "");
3087 ac_build_sendmsg(&ctx->ac, AC_SENDMSG_GS_ALLOC_REQ, tmp);
3088
3089 ac_build_endif(&ctx->ac, 5020);
3090 }
3091
3092 struct ngg_prim {
3093 unsigned num_vertices;
3094 LLVMValueRef isnull;
3095 LLVMValueRef index[3];
3096 LLVMValueRef edgeflag[3];
3097 };
3098
3099 static void build_export_prim(struct radv_shader_context *ctx,
3100 const struct ngg_prim *prim)
3101 {
3102 LLVMBuilderRef builder = ctx->ac.builder;
3103 struct ac_export_args args;
3104 LLVMValueRef tmp;
3105
3106 tmp = LLVMBuildZExt(builder, prim->isnull, ctx->ac.i32, "");
3107 args.out[0] = LLVMBuildShl(builder, tmp, LLVMConstInt(ctx->ac.i32, 31, false), "");
3108
3109 for (unsigned i = 0; i < prim->num_vertices; ++i) {
3110 tmp = LLVMBuildShl(builder, prim->index[i],
3111 LLVMConstInt(ctx->ac.i32, 10 * i, false), "");
3112 args.out[0] = LLVMBuildOr(builder, args.out[0], tmp, "");
3113 tmp = LLVMBuildZExt(builder, prim->edgeflag[i], ctx->ac.i32, "");
3114 tmp = LLVMBuildShl(builder, tmp,
3115 LLVMConstInt(ctx->ac.i32, 10 * i + 9, false), "");
3116 args.out[0] = LLVMBuildOr(builder, args.out[0], tmp, "");
3117 }
3118
3119 args.out[0] = LLVMBuildBitCast(builder, args.out[0], ctx->ac.f32, "");
3120 args.out[1] = LLVMGetUndef(ctx->ac.f32);
3121 args.out[2] = LLVMGetUndef(ctx->ac.f32);
3122 args.out[3] = LLVMGetUndef(ctx->ac.f32);
3123
3124 args.target = V_008DFC_SQ_EXP_PRIM;
3125 args.enabled_channels = 1;
3126 args.done = true;
3127 args.valid_mask = false;
3128 args.compr = false;
3129
3130 ac_build_export(&ctx->ac, &args);
3131 }
3132
3133 static void
3134 handle_ngg_outputs_post(struct radv_shader_context *ctx)
3135 {
3136 LLVMBuilderRef builder = ctx->ac.builder;
3137 struct ac_build_if_state if_state;
3138 unsigned num_vertices = 3;
3139 LLVMValueRef tmp;
3140
3141 assert((ctx->stage == MESA_SHADER_VERTEX ||
3142 ctx->stage == MESA_SHADER_TESS_EVAL) && !ctx->is_gs_copy_shader);
3143
3144 LLVMValueRef prims_in_wave = ac_unpack_param(&ctx->ac, ctx->merged_wave_info, 8, 8);
3145 LLVMValueRef vtx_in_wave = ac_unpack_param(&ctx->ac, ctx->merged_wave_info, 0, 8);
3146 LLVMValueRef is_gs_thread = LLVMBuildICmp(builder, LLVMIntULT,
3147 ac_get_thread_id(&ctx->ac), prims_in_wave, "");
3148 LLVMValueRef is_es_thread = LLVMBuildICmp(builder, LLVMIntULT,
3149 ac_get_thread_id(&ctx->ac), vtx_in_wave, "");
3150 LLVMValueRef vtxindex[] = {
3151 ac_unpack_param(&ctx->ac, ctx->gs_vtx_offset[0], 0, 16),
3152 ac_unpack_param(&ctx->ac, ctx->gs_vtx_offset[0], 16, 16),
3153 ac_unpack_param(&ctx->ac, ctx->gs_vtx_offset[2], 0, 16),
3154 };
3155
3156 /* TODO: streamout */
3157
3158 /* TODO: VS primitive ID */
3159 if (ctx->options->key.vs.out.export_prim_id)
3160 assert(0);
3161
3162 /* TODO: primitive culling */
3163
3164 build_sendmsg_gs_alloc_req(ctx, ngg_get_vtx_cnt(ctx), ngg_get_prim_cnt(ctx));
3165
3166 /* TODO: streamout queries */
3167 /* Export primitive data to the index buffer. Format is:
3168 * - bits 0..8: index 0
3169 * - bit 9: edge flag 0
3170 * - bits 10..18: index 1
3171 * - bit 19: edge flag 1
3172 * - bits 20..28: index 2
3173 * - bit 29: edge flag 2
3174 * - bit 31: null primitive (skip)
3175 *
3176 * For the first version, we will always build up all three indices
3177 * independent of the primitive type. The additional garbage data
3178 * shouldn't hurt.
3179 *
3180 * TODO: culling depends on the primitive type, so can have some
3181 * interaction here.
3182 */
3183 ac_nir_build_if(&if_state, ctx, is_gs_thread);
3184 {
3185 struct ngg_prim prim = {};
3186
3187 prim.num_vertices = num_vertices;
3188 prim.isnull = ctx->ac.i1false;
3189 memcpy(prim.index, vtxindex, sizeof(vtxindex[0]) * 3);
3190
3191 for (unsigned i = 0; i < num_vertices; ++i) {
3192 tmp = LLVMBuildLShr(builder, ctx->abi.gs_invocation_id,
3193 LLVMConstInt(ctx->ac.i32, 8 + i, false), "");
3194 prim.edgeflag[i] = LLVMBuildTrunc(builder, tmp, ctx->ac.i1, "");
3195 }
3196
3197 build_export_prim(ctx, &prim);
3198 }
3199 ac_nir_build_endif(&if_state);
3200
3201 /* Export per-vertex data (positions and parameters). */
3202 ac_nir_build_if(&if_state, ctx, is_es_thread);
3203 {
3204 handle_vs_outputs_post(ctx, ctx->options->key.vs.out.export_prim_id,
3205 ctx->options->key.vs.out.export_layer_id,
3206 ctx->options->key.vs.out.export_clip_dists,
3207 ctx->stage == MESA_SHADER_TESS_EVAL ? &ctx->shader_info->tes.outinfo : &ctx->shader_info->vs.outinfo);
3208 }
3209 ac_nir_build_endif(&if_state);
3210 }
3211
3212 static void
3213 write_tess_factors(struct radv_shader_context *ctx)
3214 {
3215 unsigned stride, outer_comps, inner_comps;
3216 struct ac_build_if_state if_ctx, inner_if_ctx;
3217 LLVMValueRef invocation_id = ac_unpack_param(&ctx->ac, ctx->abi.tcs_rel_ids, 8, 5);
3218 LLVMValueRef rel_patch_id = ac_unpack_param(&ctx->ac, ctx->abi.tcs_rel_ids, 0, 8);
3219 unsigned tess_inner_index = 0, tess_outer_index;
3220 LLVMValueRef lds_base, lds_inner = NULL, lds_outer, byteoffset, buffer;
3221 LLVMValueRef out[6], vec0, vec1, tf_base, inner[4], outer[4];
3222 int i;
3223 ac_emit_barrier(&ctx->ac, ctx->stage);
3224
3225 switch (ctx->options->key.tcs.primitive_mode) {
3226 case GL_ISOLINES:
3227 stride = 2;
3228 outer_comps = 2;
3229 inner_comps = 0;
3230 break;
3231 case GL_TRIANGLES:
3232 stride = 4;
3233 outer_comps = 3;
3234 inner_comps = 1;
3235 break;
3236 case GL_QUADS:
3237 stride = 6;
3238 outer_comps = 4;
3239 inner_comps = 2;
3240 break;
3241 default:
3242 return;
3243 }
3244
3245 ac_nir_build_if(&if_ctx, ctx,
3246 LLVMBuildICmp(ctx->ac.builder, LLVMIntEQ,
3247 invocation_id, ctx->ac.i32_0, ""));
3248
3249 lds_base = get_tcs_out_current_patch_data_offset(ctx);
3250
3251 if (inner_comps) {
3252 tess_inner_index = shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER);
3253 lds_inner = LLVMBuildAdd(ctx->ac.builder, lds_base,
3254 LLVMConstInt(ctx->ac.i32, tess_inner_index * 4, false), "");
3255 }
3256
3257 tess_outer_index = shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER);
3258 lds_outer = LLVMBuildAdd(ctx->ac.builder, lds_base,
3259 LLVMConstInt(ctx->ac.i32, tess_outer_index * 4, false), "");
3260
3261 for (i = 0; i < 4; i++) {
3262 inner[i] = LLVMGetUndef(ctx->ac.i32);
3263 outer[i] = LLVMGetUndef(ctx->ac.i32);
3264 }
3265
3266 // LINES reversal
3267 if (ctx->options->key.tcs.primitive_mode == GL_ISOLINES) {
3268 outer[0] = out[1] = ac_lds_load(&ctx->ac, lds_outer);
3269 lds_outer = LLVMBuildAdd(ctx->ac.builder, lds_outer,
3270 ctx->ac.i32_1, "");
3271 outer[1] = out[0] = ac_lds_load(&ctx->ac, lds_outer);
3272 } else {
3273 for (i = 0; i < outer_comps; i++) {
3274 outer[i] = out[i] =
3275 ac_lds_load(&ctx->ac, lds_outer);
3276 lds_outer = LLVMBuildAdd(ctx->ac.builder, lds_outer,
3277 ctx->ac.i32_1, "");
3278 }
3279 for (i = 0; i < inner_comps; i++) {
3280 inner[i] = out[outer_comps+i] =
3281 ac_lds_load(&ctx->ac, lds_inner);
3282 lds_inner = LLVMBuildAdd(ctx->ac.builder, lds_inner,
3283 ctx->ac.i32_1, "");
3284 }
3285 }
3286
3287 /* Convert the outputs to vectors for stores. */
3288 vec0 = ac_build_gather_values(&ctx->ac, out, MIN2(stride, 4));
3289 vec1 = NULL;
3290
3291 if (stride > 4)
3292 vec1 = ac_build_gather_values(&ctx->ac, out + 4, stride - 4);
3293
3294
3295 buffer = ctx->hs_ring_tess_factor;
3296 tf_base = ctx->tess_factor_offset;
3297 byteoffset = LLVMBuildMul(ctx->ac.builder, rel_patch_id,
3298 LLVMConstInt(ctx->ac.i32, 4 * stride, false), "");
3299 unsigned tf_offset = 0;
3300
3301 if (ctx->options->chip_class <= GFX8) {
3302 ac_nir_build_if(&inner_if_ctx, ctx,
3303 LLVMBuildICmp(ctx->ac.builder, LLVMIntEQ,
3304 rel_patch_id, ctx->ac.i32_0, ""));
3305
3306 /* Store the dynamic HS control word. */
3307 ac_build_buffer_store_dword(&ctx->ac, buffer,
3308 LLVMConstInt(ctx->ac.i32, 0x80000000, false),
3309 1, ctx->ac.i32_0, tf_base,
3310 0, ac_glc, false);
3311 tf_offset += 4;
3312
3313 ac_nir_build_endif(&inner_if_ctx);
3314 }
3315
3316 /* Store the tessellation factors. */
3317 ac_build_buffer_store_dword(&ctx->ac, buffer, vec0,
3318 MIN2(stride, 4), byteoffset, tf_base,
3319 tf_offset, ac_glc, false);
3320 if (vec1)
3321 ac_build_buffer_store_dword(&ctx->ac, buffer, vec1,
3322 stride - 4, byteoffset, tf_base,
3323 16 + tf_offset, ac_glc, false);
3324
3325 //store to offchip for TES to read - only if TES reads them
3326 if (ctx->options->key.tcs.tes_reads_tess_factors) {
3327 LLVMValueRef inner_vec, outer_vec, tf_outer_offset;
3328 LLVMValueRef tf_inner_offset;
3329 unsigned param_outer, param_inner;
3330
3331 param_outer = shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER);
3332 tf_outer_offset = get_tcs_tes_buffer_address(ctx, NULL,
3333 LLVMConstInt(ctx->ac.i32, param_outer, 0));
3334
3335 outer_vec = ac_build_gather_values(&ctx->ac, outer,
3336 util_next_power_of_two(outer_comps));
3337
3338 ac_build_buffer_store_dword(&ctx->ac, ctx->hs_ring_tess_offchip, outer_vec,
3339 outer_comps, tf_outer_offset,
3340 ctx->oc_lds, 0, ac_glc, false);
3341 if (inner_comps) {
3342 param_inner = shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER);
3343 tf_inner_offset = get_tcs_tes_buffer_address(ctx, NULL,
3344 LLVMConstInt(ctx->ac.i32, param_inner, 0));
3345
3346 inner_vec = inner_comps == 1 ? inner[0] :
3347 ac_build_gather_values(&ctx->ac, inner, inner_comps);
3348 ac_build_buffer_store_dword(&ctx->ac, ctx->hs_ring_tess_offchip, inner_vec,
3349 inner_comps, tf_inner_offset,
3350 ctx->oc_lds, 0, ac_glc, false);
3351 }
3352 }
3353 ac_nir_build_endif(&if_ctx);
3354 }
3355
3356 static void
3357 handle_tcs_outputs_post(struct radv_shader_context *ctx)
3358 {
3359 write_tess_factors(ctx);
3360 }
3361
3362 static bool
3363 si_export_mrt_color(struct radv_shader_context *ctx,
3364 LLVMValueRef *color, unsigned index,
3365 struct ac_export_args *args)
3366 {
3367 /* Export */
3368 si_llvm_init_export_args(ctx, color, 0xf,
3369 V_008DFC_SQ_EXP_MRT + index, args);
3370 if (!args->enabled_channels)
3371 return false; /* unnecessary NULL export */
3372
3373 return true;
3374 }
3375
3376 static void
3377 radv_export_mrt_z(struct radv_shader_context *ctx,
3378 LLVMValueRef depth, LLVMValueRef stencil,
3379 LLVMValueRef samplemask)
3380 {
3381 struct ac_export_args args;
3382
3383 ac_export_mrt_z(&ctx->ac, depth, stencil, samplemask, &args);
3384
3385 ac_build_export(&ctx->ac, &args);
3386 }
3387
3388 static void
3389 handle_fs_outputs_post(struct radv_shader_context *ctx)
3390 {
3391 unsigned index = 0;
3392 LLVMValueRef depth = NULL, stencil = NULL, samplemask = NULL;
3393 struct ac_export_args color_args[8];
3394
3395 for (unsigned i = 0; i < AC_LLVM_MAX_OUTPUTS; ++i) {
3396 LLVMValueRef values[4];
3397
3398 if (!(ctx->output_mask & (1ull << i)))
3399 continue;
3400
3401 if (i < FRAG_RESULT_DATA0)
3402 continue;
3403
3404 for (unsigned j = 0; j < 4; j++)
3405 values[j] = ac_to_float(&ctx->ac,
3406 radv_load_output(ctx, i, j));
3407
3408 bool ret = si_export_mrt_color(ctx, values,
3409 i - FRAG_RESULT_DATA0,
3410 &color_args[index]);
3411 if (ret)
3412 index++;
3413 }
3414
3415 /* Process depth, stencil, samplemask. */
3416 if (ctx->shader_info->info.ps.writes_z) {
3417 depth = ac_to_float(&ctx->ac,
3418 radv_load_output(ctx, FRAG_RESULT_DEPTH, 0));
3419 }
3420 if (ctx->shader_info->info.ps.writes_stencil) {
3421 stencil = ac_to_float(&ctx->ac,
3422 radv_load_output(ctx, FRAG_RESULT_STENCIL, 0));
3423 }
3424 if (ctx->shader_info->info.ps.writes_sample_mask) {
3425 samplemask = ac_to_float(&ctx->ac,
3426 radv_load_output(ctx, FRAG_RESULT_SAMPLE_MASK, 0));
3427 }
3428
3429 /* Set the DONE bit on last non-null color export only if Z isn't
3430 * exported.
3431 */
3432 if (index > 0 &&
3433 !ctx->shader_info->info.ps.writes_z &&
3434 !ctx->shader_info->info.ps.writes_stencil &&
3435 !ctx->shader_info->info.ps.writes_sample_mask) {
3436 unsigned last = index - 1;
3437
3438 color_args[last].valid_mask = 1; /* whether the EXEC mask is valid */
3439 color_args[last].done = 1; /* DONE bit */
3440 }
3441
3442 /* Export PS outputs. */
3443 for (unsigned i = 0; i < index; i++)
3444 ac_build_export(&ctx->ac, &color_args[i]);
3445
3446 if (depth || stencil || samplemask)
3447 radv_export_mrt_z(ctx, depth, stencil, samplemask);
3448 else if (!index)
3449 ac_build_export_null(&ctx->ac);
3450 }
3451
3452 static void
3453 emit_gs_epilogue(struct radv_shader_context *ctx)
3454 {
3455 ac_build_sendmsg(&ctx->ac, AC_SENDMSG_GS_OP_NOP | AC_SENDMSG_GS_DONE, ctx->gs_wave_id);
3456 }
3457
3458 static void
3459 handle_shader_outputs_post(struct ac_shader_abi *abi, unsigned max_outputs,
3460 LLVMValueRef *addrs)
3461 {
3462 struct radv_shader_context *ctx = radv_shader_context_from_abi(abi);
3463
3464 switch (ctx->stage) {
3465 case MESA_SHADER_VERTEX:
3466 if (ctx->options->key.vs.out.as_ls)
3467 handle_ls_outputs_post(ctx);
3468 else if (ctx->options->key.vs.out.as_ngg)
3469 break; /* handled outside of the shader body */
3470 else if (ctx->options->key.vs.out.as_es)
3471 handle_es_outputs_post(ctx, &ctx->shader_info->vs.es_info);
3472 else
3473 handle_vs_outputs_post(ctx, ctx->options->key.vs.out.export_prim_id,
3474 ctx->options->key.vs.out.export_layer_id,
3475 ctx->options->key.vs.out.export_clip_dists,
3476 &ctx->shader_info->vs.outinfo);
3477 break;
3478 case MESA_SHADER_FRAGMENT:
3479 handle_fs_outputs_post(ctx);
3480 break;
3481 case MESA_SHADER_GEOMETRY:
3482 emit_gs_epilogue(ctx);
3483 break;
3484 case MESA_SHADER_TESS_CTRL:
3485 handle_tcs_outputs_post(ctx);
3486 break;
3487 case MESA_SHADER_TESS_EVAL:
3488 if (ctx->options->key.tes.out.as_ngg)
3489 break; /* handled outside of the shader body */
3490 else if (ctx->options->key.tes.out.as_es)
3491 handle_es_outputs_post(ctx, &ctx->shader_info->tes.es_info);
3492 else
3493 handle_vs_outputs_post(ctx, ctx->options->key.tes.out.export_prim_id,
3494 ctx->options->key.tes.out.export_layer_id,
3495 ctx->options->key.tes.out.export_clip_dists,
3496 &ctx->shader_info->tes.outinfo);
3497 break;
3498 default:
3499 break;
3500 }
3501 }
3502
3503 static void ac_llvm_finalize_module(struct radv_shader_context *ctx,
3504 LLVMPassManagerRef passmgr,
3505 const struct radv_nir_compiler_options *options)
3506 {
3507 LLVMRunPassManager(passmgr, ctx->ac.module);
3508 LLVMDisposeBuilder(ctx->ac.builder);
3509
3510 ac_llvm_context_dispose(&ctx->ac);
3511 }
3512
3513 static void
3514 ac_nir_eliminate_const_vs_outputs(struct radv_shader_context *ctx)
3515 {
3516 struct radv_vs_output_info *outinfo;
3517
3518 switch (ctx->stage) {
3519 case MESA_SHADER_FRAGMENT:
3520 case MESA_SHADER_COMPUTE:
3521 case MESA_SHADER_TESS_CTRL:
3522 case MESA_SHADER_GEOMETRY:
3523 return;
3524 case MESA_SHADER_VERTEX:
3525 if (ctx->options->key.vs.out.as_ls ||
3526 ctx->options->key.vs.out.as_es)
3527 return;
3528 outinfo = &ctx->shader_info->vs.outinfo;
3529 break;
3530 case MESA_SHADER_TESS_EVAL:
3531 if (ctx->options->key.vs.out.as_es)
3532 return;
3533 outinfo = &ctx->shader_info->tes.outinfo;
3534 break;
3535 default:
3536 unreachable("Unhandled shader type");
3537 }
3538
3539 ac_optimize_vs_outputs(&ctx->ac,
3540 ctx->main_function,
3541 outinfo->vs_output_param_offset,
3542 VARYING_SLOT_MAX,
3543 &outinfo->param_exports);
3544 }
3545
3546 static void
3547 ac_setup_rings(struct radv_shader_context *ctx)
3548 {
3549 if (ctx->options->chip_class <= GFX8 &&
3550 (ctx->stage == MESA_SHADER_GEOMETRY ||
3551 ctx->options->key.vs.out.as_es || ctx->options->key.tes.out.as_es)) {
3552 unsigned ring = ctx->stage == MESA_SHADER_GEOMETRY ? RING_ESGS_GS
3553 : RING_ESGS_VS;
3554 LLVMValueRef offset = LLVMConstInt(ctx->ac.i32, ring, false);
3555
3556 ctx->esgs_ring = ac_build_load_to_sgpr(&ctx->ac,
3557 ctx->ring_offsets,
3558 offset);
3559 }
3560
3561 if (ctx->is_gs_copy_shader) {
3562 ctx->gsvs_ring[0] =
3563 ac_build_load_to_sgpr(&ctx->ac, ctx->ring_offsets,
3564 LLVMConstInt(ctx->ac.i32,
3565 RING_GSVS_VS, false));
3566 }
3567
3568 if (ctx->stage == MESA_SHADER_GEOMETRY) {
3569 /* The conceptual layout of the GSVS ring is
3570 * v0c0 .. vLv0 v0c1 .. vLc1 ..
3571 * but the real memory layout is swizzled across
3572 * threads:
3573 * t0v0c0 .. t15v0c0 t0v1c0 .. t15v1c0 ... t15vLcL
3574 * t16v0c0 ..
3575 * Override the buffer descriptor accordingly.
3576 */
3577 LLVMTypeRef v2i64 = LLVMVectorType(ctx->ac.i64, 2);
3578 uint64_t stream_offset = 0;
3579 unsigned num_records = 64;
3580 LLVMValueRef base_ring;
3581
3582 base_ring =
3583 ac_build_load_to_sgpr(&ctx->ac, ctx->ring_offsets,
3584 LLVMConstInt(ctx->ac.i32,
3585 RING_GSVS_GS, false));
3586
3587 for (unsigned stream = 0; stream < 4; stream++) {
3588 unsigned num_components, stride;
3589 LLVMValueRef ring, tmp;
3590
3591 num_components =
3592 ctx->shader_info->info.gs.num_stream_output_components[stream];
3593
3594 if (!num_components)
3595 continue;
3596
3597 stride = 4 * num_components * ctx->gs_max_out_vertices;
3598
3599 /* Limit on the stride field for <= GFX7. */
3600 assert(stride < (1 << 14));
3601
3602 ring = LLVMBuildBitCast(ctx->ac.builder,
3603 base_ring, v2i64, "");
3604 tmp = LLVMBuildExtractElement(ctx->ac.builder,
3605 ring, ctx->ac.i32_0, "");
3606 tmp = LLVMBuildAdd(ctx->ac.builder, tmp,
3607 LLVMConstInt(ctx->ac.i64,
3608 stream_offset, 0), "");
3609 ring = LLVMBuildInsertElement(ctx->ac.builder,
3610 ring, tmp, ctx->ac.i32_0, "");
3611
3612 stream_offset += stride * 64;
3613
3614 ring = LLVMBuildBitCast(ctx->ac.builder, ring,
3615 ctx->ac.v4i32, "");
3616
3617 tmp = LLVMBuildExtractElement(ctx->ac.builder, ring,
3618 ctx->ac.i32_1, "");
3619 tmp = LLVMBuildOr(ctx->ac.builder, tmp,
3620 LLVMConstInt(ctx->ac.i32,
3621 S_008F04_STRIDE(stride), false), "");
3622 ring = LLVMBuildInsertElement(ctx->ac.builder, ring, tmp,
3623 ctx->ac.i32_1, "");
3624
3625 ring = LLVMBuildInsertElement(ctx->ac.builder, ring,
3626 LLVMConstInt(ctx->ac.i32,
3627 num_records, false),
3628 LLVMConstInt(ctx->ac.i32, 2, false), "");
3629
3630 ctx->gsvs_ring[stream] = ring;
3631 }
3632 }
3633
3634 if (ctx->stage == MESA_SHADER_TESS_CTRL ||
3635 ctx->stage == MESA_SHADER_TESS_EVAL) {
3636 ctx->hs_ring_tess_offchip = ac_build_load_to_sgpr(&ctx->ac, ctx->ring_offsets, LLVMConstInt(ctx->ac.i32, RING_HS_TESS_OFFCHIP, false));
3637 ctx->hs_ring_tess_factor = ac_build_load_to_sgpr(&ctx->ac, ctx->ring_offsets, LLVMConstInt(ctx->ac.i32, RING_HS_TESS_FACTOR, false));
3638 }
3639 }
3640
3641 unsigned
3642 radv_nir_get_max_workgroup_size(enum chip_class chip_class,
3643 const struct nir_shader *nir)
3644 {
3645 switch (nir->info.stage) {
3646 case MESA_SHADER_TESS_CTRL:
3647 return chip_class >= GFX7 ? 128 : 64;
3648 case MESA_SHADER_GEOMETRY:
3649 return chip_class >= GFX9 ? 128 : 64;
3650 case MESA_SHADER_COMPUTE:
3651 break;
3652 default:
3653 return 0;
3654 }
3655
3656 unsigned max_workgroup_size = nir->info.cs.local_size[0] *
3657 nir->info.cs.local_size[1] *
3658 nir->info.cs.local_size[2];
3659 return max_workgroup_size;
3660 }
3661
3662 /* Fixup the HW not emitting the TCS regs if there are no HS threads. */
3663 static void ac_nir_fixup_ls_hs_input_vgprs(struct radv_shader_context *ctx)
3664 {
3665 LLVMValueRef count = ac_unpack_param(&ctx->ac, ctx->merged_wave_info, 8, 8);
3666 LLVMValueRef hs_empty = LLVMBuildICmp(ctx->ac.builder, LLVMIntEQ, count,
3667 ctx->ac.i32_0, "");
3668 ctx->abi.instance_id = LLVMBuildSelect(ctx->ac.builder, hs_empty, ctx->rel_auto_id, ctx->abi.instance_id, "");
3669 ctx->rel_auto_id = LLVMBuildSelect(ctx->ac.builder, hs_empty, ctx->abi.tcs_rel_ids, ctx->rel_auto_id, "");
3670 ctx->abi.vertex_id = LLVMBuildSelect(ctx->ac.builder, hs_empty, ctx->abi.tcs_patch_id, ctx->abi.vertex_id, "");
3671 }
3672
3673 static void prepare_gs_input_vgprs(struct radv_shader_context *ctx)
3674 {
3675 for(int i = 5; i >= 0; --i) {
3676 ctx->gs_vtx_offset[i] = ac_unpack_param(&ctx->ac, ctx->gs_vtx_offset[i & ~1],
3677 (i & 1) * 16, 16);
3678 }
3679
3680 ctx->gs_wave_id = ac_unpack_param(&ctx->ac, ctx->merged_wave_info, 16, 8);
3681 }
3682
3683
3684 static
3685 LLVMModuleRef ac_translate_nir_to_llvm(struct ac_llvm_compiler *ac_llvm,
3686 struct nir_shader *const *shaders,
3687 int shader_count,
3688 struct radv_shader_variant_info *shader_info,
3689 const struct radv_nir_compiler_options *options)
3690 {
3691 struct radv_shader_context ctx = {0};
3692 unsigned i;
3693 ctx.options = options;
3694 ctx.shader_info = shader_info;
3695
3696 ac_llvm_context_init(&ctx.ac, options->chip_class, options->family);
3697 ctx.context = ctx.ac.context;
3698 ctx.ac.module = ac_create_module(ac_llvm->tm, ctx.context);
3699
3700 enum ac_float_mode float_mode =
3701 options->unsafe_math ? AC_FLOAT_MODE_UNSAFE_FP_MATH :
3702 AC_FLOAT_MODE_DEFAULT;
3703
3704 ctx.ac.builder = ac_create_builder(ctx.context, float_mode);
3705
3706 radv_nir_shader_info_init(&shader_info->info);
3707
3708 for(int i = 0; i < shader_count; ++i)
3709 radv_nir_shader_info_pass(shaders[i], options, &shader_info->info);
3710
3711 for (i = 0; i < RADV_UD_MAX_SETS; i++)
3712 shader_info->user_sgprs_locs.descriptor_sets[i].sgpr_idx = -1;
3713 for (i = 0; i < AC_UD_MAX_UD; i++)
3714 shader_info->user_sgprs_locs.shader_data[i].sgpr_idx = -1;
3715
3716 ctx.max_workgroup_size = 0;
3717 for (int i = 0; i < shader_count; ++i) {
3718 ctx.max_workgroup_size = MAX2(ctx.max_workgroup_size,
3719 radv_nir_get_max_workgroup_size(ctx.options->chip_class,
3720 shaders[i]));
3721 }
3722
3723 if (ctx.ac.chip_class >= GFX10) {
3724 if (is_pre_gs_stage(shaders[0]->info.stage) &&
3725 options->key.vs.out.as_ngg) {
3726 ctx.max_workgroup_size = 128;
3727 }
3728 }
3729
3730 create_function(&ctx, shaders[shader_count - 1]->info.stage, shader_count >= 2,
3731 shader_count >= 2 ? shaders[shader_count - 2]->info.stage : MESA_SHADER_VERTEX);
3732
3733 ctx.abi.inputs = &ctx.inputs[0];
3734 ctx.abi.emit_outputs = handle_shader_outputs_post;
3735 ctx.abi.emit_vertex = visit_emit_vertex;
3736 ctx.abi.load_ubo = radv_load_ubo;
3737 ctx.abi.load_ssbo = radv_load_ssbo;
3738 ctx.abi.load_sampler_desc = radv_get_sampler_desc;
3739 ctx.abi.load_resource = radv_load_resource;
3740 ctx.abi.clamp_shadow_reference = false;
3741 ctx.abi.gfx9_stride_size_workaround = ctx.ac.chip_class == GFX9 && HAVE_LLVM < 0x800;
3742
3743 /* Because the new raw/struct atomic intrinsics are buggy with LLVM 8,
3744 * we fallback to the old intrinsics for atomic buffer image operations
3745 * and thus we need to apply the indexing workaround...
3746 */
3747 ctx.abi.gfx9_stride_size_workaround_for_atomic = ctx.ac.chip_class == GFX9 && HAVE_LLVM < 0x900;
3748
3749 bool is_ngg = is_pre_gs_stage(shaders[0]->info.stage) && ctx.options->key.vs.out.as_ngg;
3750 if (shader_count >= 2 || is_ngg)
3751 ac_init_exec_full_mask(&ctx.ac);
3752
3753 if ((ctx.ac.family == CHIP_VEGA10 ||
3754 ctx.ac.family == CHIP_RAVEN) &&
3755 shaders[shader_count - 1]->info.stage == MESA_SHADER_TESS_CTRL)
3756 ac_nir_fixup_ls_hs_input_vgprs(&ctx);
3757
3758 for(int i = 0; i < shader_count; ++i) {
3759 ctx.stage = shaders[i]->info.stage;
3760 ctx.output_mask = 0;
3761
3762 if (shaders[i]->info.stage == MESA_SHADER_GEOMETRY) {
3763 for (int i = 0; i < 4; i++) {
3764 ctx.gs_next_vertex[i] =
3765 ac_build_alloca(&ctx.ac, ctx.ac.i32, "");
3766 }
3767 ctx.gs_max_out_vertices = shaders[i]->info.gs.vertices_out;
3768 ctx.abi.load_inputs = load_gs_input;
3769 ctx.abi.emit_primitive = visit_end_primitive;
3770 } else if (shaders[i]->info.stage == MESA_SHADER_TESS_CTRL) {
3771 ctx.tcs_outputs_read = shaders[i]->info.outputs_read;
3772 ctx.tcs_patch_outputs_read = shaders[i]->info.patch_outputs_read;
3773 ctx.abi.load_tess_varyings = load_tcs_varyings;
3774 ctx.abi.load_patch_vertices_in = load_patch_vertices_in;
3775 ctx.abi.store_tcs_outputs = store_tcs_output;
3776 ctx.tcs_vertices_per_patch = shaders[i]->info.tess.tcs_vertices_out;
3777 if (shader_count == 1)
3778 ctx.tcs_num_inputs = ctx.options->key.tcs.num_inputs;
3779 else
3780 ctx.tcs_num_inputs = util_last_bit64(shader_info->info.vs.ls_outputs_written);
3781 ctx.tcs_num_patches = get_tcs_num_patches(&ctx);
3782 } else if (shaders[i]->info.stage == MESA_SHADER_TESS_EVAL) {
3783 ctx.tes_primitive_mode = shaders[i]->info.tess.primitive_mode;
3784 ctx.abi.load_tess_varyings = load_tes_input;
3785 ctx.abi.load_tess_coord = load_tess_coord;
3786 ctx.abi.load_patch_vertices_in = load_patch_vertices_in;
3787 ctx.tcs_vertices_per_patch = shaders[i]->info.tess.tcs_vertices_out;
3788 ctx.tcs_num_patches = ctx.options->key.tes.num_patches;
3789 } else if (shaders[i]->info.stage == MESA_SHADER_VERTEX) {
3790 ctx.abi.load_base_vertex = radv_load_base_vertex;
3791 } else if (shaders[i]->info.stage == MESA_SHADER_FRAGMENT) {
3792 shader_info->fs.can_discard = shaders[i]->info.fs.uses_discard;
3793 ctx.abi.lookup_interp_param = lookup_interp_param;
3794 ctx.abi.load_sample_position = load_sample_position;
3795 ctx.abi.load_sample_mask_in = load_sample_mask_in;
3796 ctx.abi.emit_kill = radv_emit_kill;
3797 }
3798
3799 if (i)
3800 ac_emit_barrier(&ctx.ac, ctx.stage);
3801
3802 nir_foreach_variable(variable, &shaders[i]->outputs)
3803 scan_shader_output_decl(&ctx, variable, shaders[i], shaders[i]->info.stage);
3804
3805 if (shaders[i]->info.stage == MESA_SHADER_GEOMETRY) {
3806 unsigned addclip = shaders[i]->info.clip_distance_array_size +
3807 shaders[i]->info.cull_distance_array_size > 4;
3808 ctx.gsvs_vertex_size = (util_bitcount64(ctx.output_mask) + addclip) * 16;
3809 ctx.max_gsvs_emit_size = ctx.gsvs_vertex_size *
3810 shaders[i]->info.gs.vertices_out;
3811 }
3812
3813 ac_setup_rings(&ctx);
3814
3815 LLVMBasicBlockRef merge_block;
3816 if (shader_count >= 2 || is_ngg) {
3817 LLVMValueRef fn = LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx.ac.builder));
3818 LLVMBasicBlockRef then_block = LLVMAppendBasicBlockInContext(ctx.ac.context, fn, "");
3819 merge_block = LLVMAppendBasicBlockInContext(ctx.ac.context, fn, "");
3820
3821 LLVMValueRef count = ac_unpack_param(&ctx.ac, ctx.merged_wave_info, 8 * i, 8);
3822 LLVMValueRef thread_id = ac_get_thread_id(&ctx.ac);
3823 LLVMValueRef cond = LLVMBuildICmp(ctx.ac.builder, LLVMIntULT,
3824 thread_id, count, "");
3825 LLVMBuildCondBr(ctx.ac.builder, cond, then_block, merge_block);
3826
3827 LLVMPositionBuilderAtEnd(ctx.ac.builder, then_block);
3828 }
3829
3830 if (shaders[i]->info.stage == MESA_SHADER_FRAGMENT)
3831 prepare_interp_optimize(&ctx, shaders[i]);
3832 else if(shaders[i]->info.stage == MESA_SHADER_VERTEX)
3833 handle_vs_inputs(&ctx, shaders[i]);
3834 else if(shader_count >= 2 && shaders[i]->info.stage == MESA_SHADER_GEOMETRY)
3835 prepare_gs_input_vgprs(&ctx);
3836
3837 ac_nir_translate(&ctx.ac, &ctx.abi, shaders[i]);
3838
3839 if (shader_count >= 2 || is_ngg) {
3840 LLVMBuildBr(ctx.ac.builder, merge_block);
3841 LLVMPositionBuilderAtEnd(ctx.ac.builder, merge_block);
3842 }
3843
3844 /* This needs to be outside the if wrapping the shader body, as sometimes
3845 * the HW generates waves with 0 es/vs threads. */
3846 if (is_pre_gs_stage(shaders[i]->info.stage) &&
3847 ctx.options->key.vs.out.as_ngg &&
3848 i == shader_count - 1) {
3849 handle_ngg_outputs_post(&ctx);
3850 }
3851
3852 if (shaders[i]->info.stage == MESA_SHADER_GEOMETRY) {
3853 shader_info->gs.gsvs_vertex_size = ctx.gsvs_vertex_size;
3854 shader_info->gs.max_gsvs_emit_size = ctx.max_gsvs_emit_size;
3855 } else if (shaders[i]->info.stage == MESA_SHADER_TESS_CTRL) {
3856 shader_info->tcs.num_patches = ctx.tcs_num_patches;
3857 shader_info->tcs.lds_size = calculate_tess_lds_size(&ctx);
3858 }
3859 }
3860
3861 LLVMBuildRetVoid(ctx.ac.builder);
3862
3863 if (options->dump_preoptir)
3864 ac_dump_module(ctx.ac.module);
3865
3866 ac_llvm_finalize_module(&ctx, ac_llvm->passmgr, options);
3867
3868 if (shader_count == 1)
3869 ac_nir_eliminate_const_vs_outputs(&ctx);
3870
3871 if (options->dump_shader) {
3872 ctx.shader_info->private_mem_vgprs =
3873 ac_count_scratch_private_memory(ctx.main_function);
3874 }
3875
3876 return ctx.ac.module;
3877 }
3878
3879 static void ac_diagnostic_handler(LLVMDiagnosticInfoRef di, void *context)
3880 {
3881 unsigned *retval = (unsigned *)context;
3882 LLVMDiagnosticSeverity severity = LLVMGetDiagInfoSeverity(di);
3883 char *description = LLVMGetDiagInfoDescription(di);
3884
3885 if (severity == LLVMDSError) {
3886 *retval = 1;
3887 fprintf(stderr, "LLVM triggered Diagnostic Handler: %s\n",
3888 description);
3889 }
3890
3891 LLVMDisposeMessage(description);
3892 }
3893
3894 static unsigned radv_llvm_compile(LLVMModuleRef M,
3895 char **pelf_buffer, size_t *pelf_size,
3896 struct ac_llvm_compiler *ac_llvm)
3897 {
3898 unsigned retval = 0;
3899 LLVMContextRef llvm_ctx;
3900
3901 /* Setup Diagnostic Handler*/
3902 llvm_ctx = LLVMGetModuleContext(M);
3903
3904 LLVMContextSetDiagnosticHandler(llvm_ctx, ac_diagnostic_handler,
3905 &retval);
3906
3907 /* Compile IR*/
3908 if (!radv_compile_to_elf(ac_llvm, M, pelf_buffer, pelf_size))
3909 retval = 1;
3910 return retval;
3911 }
3912
3913 static void ac_compile_llvm_module(struct ac_llvm_compiler *ac_llvm,
3914 LLVMModuleRef llvm_module,
3915 struct radv_shader_binary **rbinary,
3916 struct radv_shader_variant_info *shader_info,
3917 gl_shader_stage stage,
3918 const struct radv_nir_compiler_options *options)
3919 {
3920 char *elf_buffer = NULL;
3921 size_t elf_size = 0;
3922 char *llvm_ir_string = NULL;
3923 if (options->dump_shader)
3924 ac_dump_module(llvm_module);
3925
3926 if (options->record_llvm_ir) {
3927 char *llvm_ir = LLVMPrintModuleToString(llvm_module);
3928 llvm_ir_string = strdup(llvm_ir);
3929 LLVMDisposeMessage(llvm_ir);
3930 }
3931
3932 int v = radv_llvm_compile(llvm_module, &elf_buffer, &elf_size, ac_llvm);
3933 if (v) {
3934 fprintf(stderr, "compile failed\n");
3935 }
3936
3937 LLVMContextRef ctx = LLVMGetModuleContext(llvm_module);
3938 LLVMDisposeModule(llvm_module);
3939 LLVMContextDispose(ctx);
3940
3941 size_t llvm_ir_size = llvm_ir_string ? strlen(llvm_ir_string) : 0;
3942 size_t alloc_size = sizeof(struct radv_shader_binary_rtld) + elf_size + llvm_ir_size + 1;
3943 struct radv_shader_binary_rtld *rbin = calloc(1, alloc_size);
3944 memcpy(rbin->data, elf_buffer, elf_size);
3945 if (llvm_ir_string)
3946 memcpy(rbin->data + elf_size, llvm_ir_string, llvm_ir_size + 1);
3947
3948 rbin->base.type = RADV_BINARY_TYPE_RTLD;
3949 rbin->base.stage = stage;
3950 rbin->base.total_size = alloc_size;
3951 rbin->elf_size = elf_size;
3952 rbin->llvm_ir_size = llvm_ir_size;
3953 *rbinary = &rbin->base;
3954
3955 free(llvm_ir_string);
3956 free(elf_buffer);
3957 }
3958
3959 static void
3960 ac_fill_shader_info(struct radv_shader_variant_info *shader_info, struct nir_shader *nir, const struct radv_nir_compiler_options *options)
3961 {
3962 switch (nir->info.stage) {
3963 case MESA_SHADER_COMPUTE:
3964 for (int i = 0; i < 3; ++i)
3965 shader_info->cs.block_size[i] = nir->info.cs.local_size[i];
3966 break;
3967 case MESA_SHADER_FRAGMENT:
3968 shader_info->fs.early_fragment_test = nir->info.fs.early_fragment_tests;
3969 break;
3970 case MESA_SHADER_GEOMETRY:
3971 shader_info->gs.vertices_in = nir->info.gs.vertices_in;
3972 shader_info->gs.vertices_out = nir->info.gs.vertices_out;
3973 shader_info->gs.output_prim = nir->info.gs.output_primitive;
3974 shader_info->gs.invocations = nir->info.gs.invocations;
3975 break;
3976 case MESA_SHADER_TESS_EVAL:
3977 shader_info->tes.primitive_mode = nir->info.tess.primitive_mode;
3978 shader_info->tes.spacing = nir->info.tess.spacing;
3979 shader_info->tes.ccw = nir->info.tess.ccw;
3980 shader_info->tes.point_mode = nir->info.tess.point_mode;
3981 shader_info->tes.as_es = options->key.tes.out.as_es;
3982 shader_info->tes.export_prim_id = options->key.tes.out.export_prim_id;
3983 shader_info->is_ngg = options->key.tes.out.as_ngg;
3984 break;
3985 case MESA_SHADER_TESS_CTRL:
3986 shader_info->tcs.tcs_vertices_out = nir->info.tess.tcs_vertices_out;
3987 break;
3988 case MESA_SHADER_VERTEX:
3989 shader_info->vs.as_es = options->key.vs.out.as_es;
3990 shader_info->vs.as_ls = options->key.vs.out.as_ls;
3991 shader_info->vs.export_prim_id = options->key.vs.out.export_prim_id;
3992 shader_info->is_ngg = options->key.vs.out.as_ngg;
3993 break;
3994 default:
3995 break;
3996 }
3997 }
3998
3999 void
4000 radv_compile_nir_shader(struct ac_llvm_compiler *ac_llvm,
4001 struct radv_shader_binary **rbinary,
4002 struct radv_shader_variant_info *shader_info,
4003 struct nir_shader *const *nir,
4004 int nir_count,
4005 const struct radv_nir_compiler_options *options)
4006 {
4007
4008 LLVMModuleRef llvm_module;
4009
4010 llvm_module = ac_translate_nir_to_llvm(ac_llvm, nir, nir_count, shader_info,
4011 options);
4012
4013 ac_compile_llvm_module(ac_llvm, llvm_module, rbinary, shader_info,
4014 nir[nir_count - 1]->info.stage, options);
4015
4016 for (int i = 0; i < nir_count; ++i)
4017 ac_fill_shader_info(shader_info, nir[i], options);
4018
4019 /* Determine the ES type (VS or TES) for the GS on GFX9. */
4020 if (options->chip_class >= GFX9) {
4021 if (nir_count == 2 &&
4022 nir[1]->info.stage == MESA_SHADER_GEOMETRY) {
4023 shader_info->gs.es_type = nir[0]->info.stage;
4024 }
4025 }
4026 }
4027
4028 static void
4029 ac_gs_copy_shader_emit(struct radv_shader_context *ctx)
4030 {
4031 LLVMValueRef vtx_offset =
4032 LLVMBuildMul(ctx->ac.builder, ctx->abi.vertex_id,
4033 LLVMConstInt(ctx->ac.i32, 4, false), "");
4034 LLVMValueRef stream_id;
4035
4036 /* Fetch the vertex stream ID. */
4037 if (ctx->shader_info->info.so.num_outputs) {
4038 stream_id =
4039 ac_unpack_param(&ctx->ac, ctx->streamout_config, 24, 2);
4040 } else {
4041 stream_id = ctx->ac.i32_0;
4042 }
4043
4044 LLVMBasicBlockRef end_bb;
4045 LLVMValueRef switch_inst;
4046
4047 end_bb = LLVMAppendBasicBlockInContext(ctx->ac.context,
4048 ctx->main_function, "end");
4049 switch_inst = LLVMBuildSwitch(ctx->ac.builder, stream_id, end_bb, 4);
4050
4051 for (unsigned stream = 0; stream < 4; stream++) {
4052 unsigned num_components =
4053 ctx->shader_info->info.gs.num_stream_output_components[stream];
4054 LLVMBasicBlockRef bb;
4055 unsigned offset;
4056
4057 if (!num_components)
4058 continue;
4059
4060 if (stream > 0 && !ctx->shader_info->info.so.num_outputs)
4061 continue;
4062
4063 bb = LLVMInsertBasicBlockInContext(ctx->ac.context, end_bb, "out");
4064 LLVMAddCase(switch_inst, LLVMConstInt(ctx->ac.i32, stream, 0), bb);
4065 LLVMPositionBuilderAtEnd(ctx->ac.builder, bb);
4066
4067 offset = 0;
4068 for (unsigned i = 0; i < AC_LLVM_MAX_OUTPUTS; ++i) {
4069 unsigned output_usage_mask =
4070 ctx->shader_info->info.gs.output_usage_mask[i];
4071 unsigned output_stream =
4072 ctx->shader_info->info.gs.output_streams[i];
4073 int length = util_last_bit(output_usage_mask);
4074
4075 if (!(ctx->output_mask & (1ull << i)) ||
4076 output_stream != stream)
4077 continue;
4078
4079 for (unsigned j = 0; j < length; j++) {
4080 LLVMValueRef value, soffset;
4081
4082 if (!(output_usage_mask & (1 << j)))
4083 continue;
4084
4085 soffset = LLVMConstInt(ctx->ac.i32,
4086 offset *
4087 ctx->gs_max_out_vertices * 16 * 4, false);
4088
4089 offset++;
4090
4091 value = ac_build_buffer_load(&ctx->ac,
4092 ctx->gsvs_ring[0],
4093 1, ctx->ac.i32_0,
4094 vtx_offset, soffset,
4095 0, ac_glc | ac_slc, true, false);
4096
4097 LLVMTypeRef type = LLVMGetAllocatedType(ctx->abi.outputs[ac_llvm_reg_index_soa(i, j)]);
4098 if (ac_get_type_size(type) == 2) {
4099 value = LLVMBuildBitCast(ctx->ac.builder, value, ctx->ac.i32, "");
4100 value = LLVMBuildTrunc(ctx->ac.builder, value, ctx->ac.i16, "");
4101 }
4102
4103 LLVMBuildStore(ctx->ac.builder,
4104 ac_to_float(&ctx->ac, value), ctx->abi.outputs[ac_llvm_reg_index_soa(i, j)]);
4105 }
4106 }
4107
4108 if (ctx->shader_info->info.so.num_outputs)
4109 radv_emit_streamout(ctx, stream);
4110
4111 if (stream == 0) {
4112 handle_vs_outputs_post(ctx, false, false, true,
4113 &ctx->shader_info->vs.outinfo);
4114 }
4115
4116 LLVMBuildBr(ctx->ac.builder, end_bb);
4117 }
4118
4119 LLVMPositionBuilderAtEnd(ctx->ac.builder, end_bb);
4120 }
4121
4122 void
4123 radv_compile_gs_copy_shader(struct ac_llvm_compiler *ac_llvm,
4124 struct nir_shader *geom_shader,
4125 struct radv_shader_binary **rbinary,
4126 struct radv_shader_variant_info *shader_info,
4127 const struct radv_nir_compiler_options *options)
4128 {
4129 struct radv_shader_context ctx = {0};
4130 ctx.options = options;
4131 ctx.shader_info = shader_info;
4132
4133 ac_llvm_context_init(&ctx.ac, options->chip_class, options->family);
4134 ctx.context = ctx.ac.context;
4135 ctx.ac.module = ac_create_module(ac_llvm->tm, ctx.context);
4136
4137 ctx.is_gs_copy_shader = true;
4138
4139 enum ac_float_mode float_mode =
4140 options->unsafe_math ? AC_FLOAT_MODE_UNSAFE_FP_MATH :
4141 AC_FLOAT_MODE_DEFAULT;
4142
4143 ctx.ac.builder = ac_create_builder(ctx.context, float_mode);
4144 ctx.stage = MESA_SHADER_VERTEX;
4145
4146 radv_nir_shader_info_pass(geom_shader, options, &shader_info->info);
4147
4148 create_function(&ctx, MESA_SHADER_VERTEX, false, MESA_SHADER_VERTEX);
4149
4150 ctx.gs_max_out_vertices = geom_shader->info.gs.vertices_out;
4151 ac_setup_rings(&ctx);
4152
4153 nir_foreach_variable(variable, &geom_shader->outputs) {
4154 scan_shader_output_decl(&ctx, variable, geom_shader, MESA_SHADER_VERTEX);
4155 ac_handle_shader_output_decl(&ctx.ac, &ctx.abi, geom_shader,
4156 variable, MESA_SHADER_VERTEX);
4157 }
4158
4159 ac_gs_copy_shader_emit(&ctx);
4160
4161 LLVMBuildRetVoid(ctx.ac.builder);
4162
4163 ac_llvm_finalize_module(&ctx, ac_llvm->passmgr, options);
4164
4165 ac_compile_llvm_module(ac_llvm, ctx.ac.module, rbinary, shader_info,
4166 MESA_SHADER_VERTEX, options);
4167 (*rbinary)->is_gs_copy_shader = true;
4168
4169 }