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