radv/gfx10: export the PrimitiveID for ES stages (VS or TES)
[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;
2597
2598 if (ctx->vertexptr) {
2599 LLVMValueRef gep_idx[3] = {
2600 ctx->ac.i32_0, /* implicit C-style array */
2601 ctx->ac.i32_0, /* second value of struct */
2602 ctx->ac.i32_1, /* stream 1: source data index */
2603 };
2604
2605 gep_idx[2] = LLVMConstInt(ctx->ac.i32, ac_llvm_reg_index_soa(index, chan), false);
2606 output = LLVMBuildGEP(ctx->ac.builder, ctx->vertexptr, gep_idx, 3, "");
2607 } else {
2608 output = ctx->abi.outputs[ac_llvm_reg_index_soa(index, chan)];
2609 }
2610
2611 return LLVMBuildLoad(ctx->ac.builder, output, "");
2612 }
2613
2614 static void
2615 radv_emit_stream_output(struct radv_shader_context *ctx,
2616 LLVMValueRef const *so_buffers,
2617 LLVMValueRef const *so_write_offsets,
2618 const struct radv_stream_output *output)
2619 {
2620 unsigned num_comps = util_bitcount(output->component_mask);
2621 unsigned loc = output->location;
2622 unsigned buf = output->buffer;
2623 unsigned offset = output->offset;
2624 unsigned start;
2625 LLVMValueRef out[4];
2626
2627 assert(num_comps && num_comps <= 4);
2628 if (!num_comps || num_comps > 4)
2629 return;
2630
2631 /* Get the first component. */
2632 start = ffs(output->component_mask) - 1;
2633
2634 /* Load the output as int. */
2635 for (int i = 0; i < num_comps; i++) {
2636 out[i] = ac_to_integer(&ctx->ac,
2637 radv_load_output(ctx, loc, start + i));
2638 }
2639
2640 /* Pack the output. */
2641 LLVMValueRef vdata = NULL;
2642
2643 switch (num_comps) {
2644 case 1: /* as i32 */
2645 vdata = out[0];
2646 break;
2647 case 2: /* as v2i32 */
2648 case 3: /* as v4i32 (aligned to 4) */
2649 out[3] = LLVMGetUndef(ctx->ac.i32);
2650 /* fall through */
2651 case 4: /* as v4i32 */
2652 vdata = ac_build_gather_values(&ctx->ac, out,
2653 !ac_has_vec3_support(ctx->ac.chip_class, false) ?
2654 util_next_power_of_two(num_comps) :
2655 num_comps);
2656 break;
2657 }
2658
2659 ac_build_buffer_store_dword(&ctx->ac, so_buffers[buf],
2660 vdata, num_comps, so_write_offsets[buf],
2661 ctx->ac.i32_0, offset,
2662 ac_glc | ac_slc, false);
2663 }
2664
2665 static void
2666 radv_emit_streamout(struct radv_shader_context *ctx, unsigned stream)
2667 {
2668 struct ac_build_if_state if_ctx;
2669 int i;
2670
2671 /* Get bits [22:16], i.e. (so_param >> 16) & 127; */
2672 assert(ctx->streamout_config);
2673 LLVMValueRef so_vtx_count =
2674 ac_build_bfe(&ctx->ac, ctx->streamout_config,
2675 LLVMConstInt(ctx->ac.i32, 16, false),
2676 LLVMConstInt(ctx->ac.i32, 7, false), false);
2677
2678 LLVMValueRef tid = ac_get_thread_id(&ctx->ac);
2679
2680 /* can_emit = tid < so_vtx_count; */
2681 LLVMValueRef can_emit = LLVMBuildICmp(ctx->ac.builder, LLVMIntULT,
2682 tid, so_vtx_count, "");
2683
2684 /* Emit the streamout code conditionally. This actually avoids
2685 * out-of-bounds buffer access. The hw tells us via the SGPR
2686 * (so_vtx_count) which threads are allowed to emit streamout data.
2687 */
2688 ac_nir_build_if(&if_ctx, ctx, can_emit);
2689 {
2690 /* The buffer offset is computed as follows:
2691 * ByteOffset = streamout_offset[buffer_id]*4 +
2692 * (streamout_write_index + thread_id)*stride[buffer_id] +
2693 * attrib_offset
2694 */
2695 LLVMValueRef so_write_index = ctx->streamout_write_idx;
2696
2697 /* Compute (streamout_write_index + thread_id). */
2698 so_write_index =
2699 LLVMBuildAdd(ctx->ac.builder, so_write_index, tid, "");
2700
2701 /* Load the descriptor and compute the write offset for each
2702 * enabled buffer.
2703 */
2704 LLVMValueRef so_write_offset[4] = {};
2705 LLVMValueRef so_buffers[4] = {};
2706 LLVMValueRef buf_ptr = ctx->streamout_buffers;
2707
2708 for (i = 0; i < 4; i++) {
2709 uint16_t stride = ctx->shader_info->info.so.strides[i];
2710
2711 if (!stride)
2712 continue;
2713
2714 LLVMValueRef offset =
2715 LLVMConstInt(ctx->ac.i32, i, false);
2716
2717 so_buffers[i] = ac_build_load_to_sgpr(&ctx->ac,
2718 buf_ptr, offset);
2719
2720 LLVMValueRef so_offset = ctx->streamout_offset[i];
2721
2722 so_offset = LLVMBuildMul(ctx->ac.builder, so_offset,
2723 LLVMConstInt(ctx->ac.i32, 4, false), "");
2724
2725 so_write_offset[i] =
2726 ac_build_imad(&ctx->ac, so_write_index,
2727 LLVMConstInt(ctx->ac.i32,
2728 stride * 4, false),
2729 so_offset);
2730 }
2731
2732 /* Write streamout data. */
2733 for (i = 0; i < ctx->shader_info->info.so.num_outputs; i++) {
2734 struct radv_stream_output *output =
2735 &ctx->shader_info->info.so.outputs[i];
2736
2737 if (stream != output->stream)
2738 continue;
2739
2740 radv_emit_stream_output(ctx, so_buffers,
2741 so_write_offset, output);
2742 }
2743 }
2744 ac_nir_build_endif(&if_ctx);
2745 }
2746
2747 struct radv_shader_output_values {
2748 LLVMValueRef values[4];
2749 unsigned slot_name;
2750 unsigned slot_index;
2751 unsigned usage_mask;
2752 };
2753
2754 static void
2755 radv_build_param_exports(struct radv_shader_context *ctx,
2756 struct radv_shader_output_values *outputs,
2757 unsigned noutput,
2758 struct radv_vs_output_info *outinfo,
2759 bool export_clip_dists)
2760 {
2761 unsigned param_count = 0;
2762
2763 for (unsigned i = 0; i < noutput; i++) {
2764 unsigned slot_name = outputs[i].slot_name;
2765 unsigned usage_mask = outputs[i].usage_mask;
2766
2767 if (slot_name != VARYING_SLOT_LAYER &&
2768 slot_name != VARYING_SLOT_PRIMITIVE_ID &&
2769 slot_name != VARYING_SLOT_CLIP_DIST0 &&
2770 slot_name != VARYING_SLOT_CLIP_DIST1 &&
2771 slot_name < VARYING_SLOT_VAR0)
2772 continue;
2773
2774 if ((slot_name == VARYING_SLOT_CLIP_DIST0 ||
2775 slot_name == VARYING_SLOT_CLIP_DIST1) && !export_clip_dists)
2776 continue;
2777
2778 radv_export_param(ctx, param_count, outputs[i].values, usage_mask);
2779
2780 assert(i < ARRAY_SIZE(outinfo->vs_output_param_offset));
2781 outinfo->vs_output_param_offset[slot_name] = param_count++;
2782 }
2783
2784 outinfo->param_exports = param_count;
2785 }
2786
2787 /* Generate export instructions for hardware VS shader stage or NGG GS stage
2788 * (position and parameter data only).
2789 */
2790 static void
2791 radv_llvm_export_vs(struct radv_shader_context *ctx,
2792 struct radv_shader_output_values *outputs,
2793 unsigned noutput,
2794 struct radv_vs_output_info *outinfo,
2795 bool export_clip_dists)
2796 {
2797 LLVMValueRef psize_value = NULL, layer_value = NULL, viewport_value = NULL;
2798 struct ac_export_args pos_args[4] = {};
2799 unsigned pos_idx, index;
2800 int i;
2801
2802 /* Build position exports */
2803 for (i = 0; i < noutput; i++) {
2804 switch (outputs[i].slot_name) {
2805 case VARYING_SLOT_POS:
2806 si_llvm_init_export_args(ctx, outputs[i].values, 0xf,
2807 V_008DFC_SQ_EXP_POS, &pos_args[0]);
2808 break;
2809 case VARYING_SLOT_PSIZ:
2810 psize_value = outputs[i].values[0];
2811 break;
2812 case VARYING_SLOT_LAYER:
2813 layer_value = outputs[i].values[0];
2814 break;
2815 case VARYING_SLOT_VIEWPORT:
2816 viewport_value = outputs[i].values[0];
2817 break;
2818 case VARYING_SLOT_CLIP_DIST0:
2819 case VARYING_SLOT_CLIP_DIST1:
2820 index = 2 + outputs[i].slot_index;
2821 si_llvm_init_export_args(ctx, outputs[i].values, 0xf,
2822 V_008DFC_SQ_EXP_POS + index,
2823 &pos_args[index]);
2824 break;
2825 default:
2826 break;
2827 }
2828 }
2829
2830 /* We need to add the position output manually if it's missing. */
2831 if (!pos_args[0].out[0]) {
2832 pos_args[0].enabled_channels = 0xf; /* writemask */
2833 pos_args[0].valid_mask = 0; /* EXEC mask */
2834 pos_args[0].done = 0; /* last export? */
2835 pos_args[0].target = V_008DFC_SQ_EXP_POS;
2836 pos_args[0].compr = 0; /* COMPR flag */
2837 pos_args[0].out[0] = ctx->ac.f32_0; /* X */
2838 pos_args[0].out[1] = ctx->ac.f32_0; /* Y */
2839 pos_args[0].out[2] = ctx->ac.f32_0; /* Z */
2840 pos_args[0].out[3] = ctx->ac.f32_1; /* W */
2841 }
2842
2843 if (outinfo->writes_pointsize ||
2844 outinfo->writes_layer ||
2845 outinfo->writes_viewport_index) {
2846 pos_args[1].enabled_channels = ((outinfo->writes_pointsize == true ? 1 : 0) |
2847 (outinfo->writes_layer == true ? 4 : 0));
2848 pos_args[1].valid_mask = 0;
2849 pos_args[1].done = 0;
2850 pos_args[1].target = V_008DFC_SQ_EXP_POS + 1;
2851 pos_args[1].compr = 0;
2852 pos_args[1].out[0] = ctx->ac.f32_0; /* X */
2853 pos_args[1].out[1] = ctx->ac.f32_0; /* Y */
2854 pos_args[1].out[2] = ctx->ac.f32_0; /* Z */
2855 pos_args[1].out[3] = ctx->ac.f32_0; /* W */
2856
2857 if (outinfo->writes_pointsize == true)
2858 pos_args[1].out[0] = psize_value;
2859 if (outinfo->writes_layer == true)
2860 pos_args[1].out[2] = layer_value;
2861 if (outinfo->writes_viewport_index == true) {
2862 if (ctx->options->chip_class >= GFX9) {
2863 /* GFX9 has the layer in out.z[10:0] and the viewport
2864 * index in out.z[19:16].
2865 */
2866 LLVMValueRef v = viewport_value;
2867 v = ac_to_integer(&ctx->ac, v);
2868 v = LLVMBuildShl(ctx->ac.builder, v,
2869 LLVMConstInt(ctx->ac.i32, 16, false),
2870 "");
2871 v = LLVMBuildOr(ctx->ac.builder, v,
2872 ac_to_integer(&ctx->ac, pos_args[1].out[2]), "");
2873
2874 pos_args[1].out[2] = ac_to_float(&ctx->ac, v);
2875 pos_args[1].enabled_channels |= 1 << 2;
2876 } else {
2877 pos_args[1].out[3] = viewport_value;
2878 pos_args[1].enabled_channels |= 1 << 3;
2879 }
2880 }
2881 }
2882
2883 for (i = 0; i < 4; i++) {
2884 if (pos_args[i].out[0])
2885 outinfo->pos_exports++;
2886 }
2887
2888 /* Navi10-14 skip POS0 exports if EXEC=0 and DONE=0, causing a hang.
2889 * Setting valid_mask=1 prevents it and has no other effect.
2890 */
2891 if (ctx->ac.family == CHIP_NAVI10 ||
2892 ctx->ac.family == CHIP_NAVI12 ||
2893 ctx->ac.family == CHIP_NAVI14)
2894 pos_args[0].valid_mask = 1;
2895
2896 pos_idx = 0;
2897 for (i = 0; i < 4; i++) {
2898 if (!pos_args[i].out[0])
2899 continue;
2900
2901 /* Specify the target we are exporting */
2902 pos_args[i].target = V_008DFC_SQ_EXP_POS + pos_idx++;
2903
2904 if (pos_idx == outinfo->pos_exports)
2905 /* Specify that this is the last export */
2906 pos_args[i].done = 1;
2907
2908 ac_build_export(&ctx->ac, &pos_args[i]);
2909 }
2910
2911 /* Build parameter exports */
2912 radv_build_param_exports(ctx, outputs, noutput, outinfo, export_clip_dists);
2913 }
2914
2915 static void
2916 handle_vs_outputs_post(struct radv_shader_context *ctx,
2917 bool export_prim_id,
2918 bool export_clip_dists,
2919 struct radv_vs_output_info *outinfo)
2920 {
2921 struct radv_shader_output_values *outputs;
2922 unsigned noutput = 0;
2923
2924 if (ctx->options->key.has_multiview_view_index) {
2925 LLVMValueRef* tmp_out = &ctx->abi.outputs[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER, 0)];
2926 if(!*tmp_out) {
2927 for(unsigned i = 0; i < 4; ++i)
2928 ctx->abi.outputs[ac_llvm_reg_index_soa(VARYING_SLOT_LAYER, i)] =
2929 ac_build_alloca_undef(&ctx->ac, ctx->ac.f32, "");
2930 }
2931
2932 LLVMBuildStore(ctx->ac.builder, ac_to_float(&ctx->ac, ctx->abi.view_index), *tmp_out);
2933 ctx->output_mask |= 1ull << VARYING_SLOT_LAYER;
2934 }
2935
2936 memset(outinfo->vs_output_param_offset, AC_EXP_PARAM_UNDEFINED,
2937 sizeof(outinfo->vs_output_param_offset));
2938 outinfo->pos_exports = 0;
2939
2940 if (ctx->output_mask & (1ull << VARYING_SLOT_PSIZ)) {
2941 outinfo->writes_pointsize = true;
2942 }
2943
2944 if (ctx->output_mask & (1ull << VARYING_SLOT_LAYER)) {
2945 outinfo->writes_layer = true;
2946 }
2947
2948 if (ctx->output_mask & (1ull << VARYING_SLOT_VIEWPORT)) {
2949 outinfo->writes_viewport_index = true;
2950 }
2951
2952 if (ctx->shader_info->info.so.num_outputs &&
2953 !ctx->is_gs_copy_shader) {
2954 /* The GS copy shader emission already emits streamout. */
2955 radv_emit_streamout(ctx, 0);
2956 }
2957
2958 /* Allocate a temporary array for the output values. */
2959 unsigned num_outputs = util_bitcount64(ctx->output_mask) + export_prim_id;
2960 outputs = malloc(num_outputs * sizeof(outputs[0]));
2961
2962 for (unsigned i = 0; i < AC_LLVM_MAX_OUTPUTS; ++i) {
2963 if (!(ctx->output_mask & (1ull << i)))
2964 continue;
2965
2966 outputs[noutput].slot_name = i;
2967 outputs[noutput].slot_index = i == VARYING_SLOT_CLIP_DIST1;
2968
2969 if (ctx->stage == MESA_SHADER_VERTEX &&
2970 !ctx->is_gs_copy_shader) {
2971 outputs[noutput].usage_mask =
2972 ctx->shader_info->info.vs.output_usage_mask[i];
2973 } else if (ctx->stage == MESA_SHADER_TESS_EVAL) {
2974 outputs[noutput].usage_mask =
2975 ctx->shader_info->info.tes.output_usage_mask[i];
2976 } else {
2977 assert(ctx->is_gs_copy_shader || ctx->options->key.vs_common_out.as_ngg);
2978 outputs[noutput].usage_mask =
2979 ctx->shader_info->info.gs.output_usage_mask[i];
2980 }
2981
2982 for (unsigned j = 0; j < 4; j++) {
2983 outputs[noutput].values[j] =
2984 ac_to_float(&ctx->ac, radv_load_output(ctx, i, j));
2985 }
2986
2987 noutput++;
2988 }
2989
2990 /* Export PrimitiveID. */
2991 if (export_prim_id) {
2992 outinfo->export_prim_id = true;
2993
2994 outputs[noutput].slot_name = VARYING_SLOT_PRIMITIVE_ID;
2995 outputs[noutput].slot_index = 0;
2996 outputs[noutput].usage_mask = 0x1;
2997 outputs[noutput].values[0] = ctx->vs_prim_id;
2998 for (unsigned j = 1; j < 4; j++)
2999 outputs[noutput].values[j] = ctx->ac.f32_0;
3000 noutput++;
3001 }
3002
3003 radv_llvm_export_vs(ctx, outputs, noutput, outinfo, export_clip_dists);
3004
3005 free(outputs);
3006 }
3007
3008 static void
3009 handle_es_outputs_post(struct radv_shader_context *ctx,
3010 struct radv_es_output_info *outinfo)
3011 {
3012 int j;
3013 uint64_t max_output_written = 0;
3014 LLVMValueRef lds_base = NULL;
3015
3016 for (unsigned i = 0; i < AC_LLVM_MAX_OUTPUTS; ++i) {
3017 int param_index;
3018
3019 if (!(ctx->output_mask & (1ull << i)))
3020 continue;
3021
3022 param_index = shader_io_get_unique_index(i);
3023
3024 max_output_written = MAX2(param_index, max_output_written);
3025 }
3026
3027 outinfo->esgs_itemsize = (max_output_written + 1) * 16;
3028
3029 if (ctx->ac.chip_class >= GFX9) {
3030 unsigned itemsize_dw = outinfo->esgs_itemsize / 4;
3031 LLVMValueRef vertex_idx = ac_get_thread_id(&ctx->ac);
3032 LLVMValueRef wave_idx = ac_unpack_param(&ctx->ac, ctx->merged_wave_info, 24, 4);
3033 vertex_idx = LLVMBuildOr(ctx->ac.builder, vertex_idx,
3034 LLVMBuildMul(ctx->ac.builder, wave_idx,
3035 LLVMConstInt(ctx->ac.i32, 64, false), ""), "");
3036 lds_base = LLVMBuildMul(ctx->ac.builder, vertex_idx,
3037 LLVMConstInt(ctx->ac.i32, itemsize_dw, 0), "");
3038 }
3039
3040 for (unsigned i = 0; i < AC_LLVM_MAX_OUTPUTS; ++i) {
3041 LLVMValueRef dw_addr = NULL;
3042 LLVMValueRef *out_ptr = &ctx->abi.outputs[i * 4];
3043 unsigned output_usage_mask;
3044 int param_index;
3045
3046 if (!(ctx->output_mask & (1ull << i)))
3047 continue;
3048
3049 if (ctx->stage == MESA_SHADER_VERTEX) {
3050 output_usage_mask =
3051 ctx->shader_info->info.vs.output_usage_mask[i];
3052 } else {
3053 assert(ctx->stage == MESA_SHADER_TESS_EVAL);
3054 output_usage_mask =
3055 ctx->shader_info->info.tes.output_usage_mask[i];
3056 }
3057
3058 param_index = shader_io_get_unique_index(i);
3059
3060 if (lds_base) {
3061 dw_addr = LLVMBuildAdd(ctx->ac.builder, lds_base,
3062 LLVMConstInt(ctx->ac.i32, param_index * 4, false),
3063 "");
3064 }
3065
3066 for (j = 0; j < 4; j++) {
3067 if (!(output_usage_mask & (1 << j)))
3068 continue;
3069
3070 LLVMValueRef out_val = LLVMBuildLoad(ctx->ac.builder, out_ptr[j], "");
3071 out_val = ac_to_integer(&ctx->ac, out_val);
3072 out_val = LLVMBuildZExtOrBitCast(ctx->ac.builder, out_val, ctx->ac.i32, "");
3073
3074 if (ctx->ac.chip_class >= GFX9) {
3075 LLVMValueRef dw_addr_offset =
3076 LLVMBuildAdd(ctx->ac.builder, dw_addr,
3077 LLVMConstInt(ctx->ac.i32,
3078 j, false), "");
3079
3080 ac_lds_store(&ctx->ac, dw_addr_offset, out_val);
3081 } else {
3082 ac_build_buffer_store_dword(&ctx->ac,
3083 ctx->esgs_ring,
3084 out_val, 1,
3085 NULL, ctx->es2gs_offset,
3086 (4 * param_index + j) * 4,
3087 ac_glc | ac_slc, true);
3088 }
3089 }
3090 }
3091 }
3092
3093 static void
3094 handle_ls_outputs_post(struct radv_shader_context *ctx)
3095 {
3096 LLVMValueRef vertex_id = ctx->rel_auto_id;
3097 uint32_t num_tcs_inputs = util_last_bit64(ctx->shader_info->info.vs.ls_outputs_written);
3098 LLVMValueRef vertex_dw_stride = LLVMConstInt(ctx->ac.i32, num_tcs_inputs * 4, false);
3099 LLVMValueRef base_dw_addr = LLVMBuildMul(ctx->ac.builder, vertex_id,
3100 vertex_dw_stride, "");
3101
3102 for (unsigned i = 0; i < AC_LLVM_MAX_OUTPUTS; ++i) {
3103 LLVMValueRef *out_ptr = &ctx->abi.outputs[i * 4];
3104
3105 if (!(ctx->output_mask & (1ull << i)))
3106 continue;
3107
3108 int param = shader_io_get_unique_index(i);
3109 LLVMValueRef dw_addr = LLVMBuildAdd(ctx->ac.builder, base_dw_addr,
3110 LLVMConstInt(ctx->ac.i32, param * 4, false),
3111 "");
3112 for (unsigned j = 0; j < 4; j++) {
3113 LLVMValueRef value = LLVMBuildLoad(ctx->ac.builder, out_ptr[j], "");
3114 value = ac_to_integer(&ctx->ac, value);
3115 value = LLVMBuildZExtOrBitCast(ctx->ac.builder, value, ctx->ac.i32, "");
3116 ac_lds_store(&ctx->ac, dw_addr, value);
3117 dw_addr = LLVMBuildAdd(ctx->ac.builder, dw_addr, ctx->ac.i32_1, "");
3118 }
3119 }
3120 }
3121
3122 static LLVMValueRef get_wave_id_in_tg(struct radv_shader_context *ctx)
3123 {
3124 return ac_unpack_param(&ctx->ac, ctx->merged_wave_info, 24, 4);
3125 }
3126
3127 static LLVMValueRef get_tgsize(struct radv_shader_context *ctx)
3128 {
3129 return ac_unpack_param(&ctx->ac, ctx->merged_wave_info, 28, 4);
3130 }
3131
3132 static LLVMValueRef get_thread_id_in_tg(struct radv_shader_context *ctx)
3133 {
3134 LLVMBuilderRef builder = ctx->ac.builder;
3135 LLVMValueRef tmp;
3136 tmp = LLVMBuildMul(builder, get_wave_id_in_tg(ctx),
3137 LLVMConstInt(ctx->ac.i32, 64, false), "");
3138 return LLVMBuildAdd(builder, tmp, ac_get_thread_id(&ctx->ac), "");
3139 }
3140
3141 static LLVMValueRef ngg_get_vtx_cnt(struct radv_shader_context *ctx)
3142 {
3143 return ac_build_bfe(&ctx->ac, ctx->gs_tg_info,
3144 LLVMConstInt(ctx->ac.i32, 12, false),
3145 LLVMConstInt(ctx->ac.i32, 9, false),
3146 false);
3147 }
3148
3149 static LLVMValueRef ngg_get_prim_cnt(struct radv_shader_context *ctx)
3150 {
3151 return ac_build_bfe(&ctx->ac, ctx->gs_tg_info,
3152 LLVMConstInt(ctx->ac.i32, 22, false),
3153 LLVMConstInt(ctx->ac.i32, 9, false),
3154 false);
3155 }
3156
3157 static LLVMValueRef
3158 ngg_gs_get_vertex_storage(struct radv_shader_context *ctx)
3159 {
3160 unsigned num_outputs = util_bitcount64(ctx->output_mask);
3161
3162 LLVMTypeRef elements[2] = {
3163 LLVMArrayType(ctx->ac.i32, 4 * num_outputs),
3164 LLVMArrayType(ctx->ac.i8, 4),
3165 };
3166 LLVMTypeRef type = LLVMStructTypeInContext(ctx->ac.context, elements, 2, false);
3167 type = LLVMPointerType(LLVMArrayType(type, 0), AC_ADDR_SPACE_LDS);
3168 return LLVMBuildBitCast(ctx->ac.builder, ctx->gs_ngg_emit, type, "");
3169 }
3170
3171 /**
3172 * Return a pointer to the LDS storage reserved for the N'th vertex, where N
3173 * is in emit order; that is:
3174 * - during the epilogue, N is the threadidx (relative to the entire threadgroup)
3175 * - during vertex emit, i.e. while the API GS shader invocation is running,
3176 * N = threadidx * gs_max_out_vertices + emitidx
3177 *
3178 * Goals of the LDS memory layout:
3179 * 1. Eliminate bank conflicts on write for geometry shaders that have all emits
3180 * in uniform control flow
3181 * 2. Eliminate bank conflicts on read for export if, additionally, there is no
3182 * culling
3183 * 3. Agnostic to the number of waves (since we don't know it before compiling)
3184 * 4. Allow coalescing of LDS instructions (ds_write_b128 etc.)
3185 * 5. Avoid wasting memory.
3186 *
3187 * We use an AoS layout due to point 4 (this also helps point 3). In an AoS
3188 * layout, elimination of bank conflicts requires that each vertex occupy an
3189 * odd number of dwords. We use the additional dword to store the output stream
3190 * index as well as a flag to indicate whether this vertex ends a primitive
3191 * for rasterization.
3192 *
3193 * Swizzling is required to satisfy points 1 and 2 simultaneously.
3194 *
3195 * Vertices are stored in export order (gsthread * gs_max_out_vertices + emitidx).
3196 * Indices are swizzled in groups of 32, which ensures point 1 without
3197 * disturbing point 2.
3198 *
3199 * \return an LDS pointer to type {[N x i32], [4 x i8]}
3200 */
3201 static LLVMValueRef
3202 ngg_gs_vertex_ptr(struct radv_shader_context *ctx, LLVMValueRef vertexidx)
3203 {
3204 LLVMBuilderRef builder = ctx->ac.builder;
3205 LLVMValueRef storage = ngg_gs_get_vertex_storage(ctx);
3206
3207 /* gs_max_out_vertices = 2^(write_stride_2exp) * some odd number */
3208 unsigned write_stride_2exp = ffs(ctx->gs_max_out_vertices) - 1;
3209 if (write_stride_2exp) {
3210 LLVMValueRef row =
3211 LLVMBuildLShr(builder, vertexidx,
3212 LLVMConstInt(ctx->ac.i32, 5, false), "");
3213 LLVMValueRef swizzle =
3214 LLVMBuildAnd(builder, row,
3215 LLVMConstInt(ctx->ac.i32, (1u << write_stride_2exp) - 1,
3216 false), "");
3217 vertexidx = LLVMBuildXor(builder, vertexidx, swizzle, "");
3218 }
3219
3220 return ac_build_gep0(&ctx->ac, storage, vertexidx);
3221 }
3222
3223 static LLVMValueRef
3224 ngg_gs_emit_vertex_ptr(struct radv_shader_context *ctx, LLVMValueRef gsthread,
3225 LLVMValueRef emitidx)
3226 {
3227 LLVMBuilderRef builder = ctx->ac.builder;
3228 LLVMValueRef tmp;
3229
3230 tmp = LLVMConstInt(ctx->ac.i32, ctx->gs_max_out_vertices, false);
3231 tmp = LLVMBuildMul(builder, tmp, gsthread, "");
3232 const LLVMValueRef vertexidx = LLVMBuildAdd(builder, tmp, emitidx, "");
3233 return ngg_gs_vertex_ptr(ctx, vertexidx);
3234 }
3235
3236 /* Send GS Alloc Req message from the first wave of the group to SPI.
3237 * Message payload is:
3238 * - bits 0..10: vertices in group
3239 * - bits 12..22: primitives in group
3240 */
3241 static void build_sendmsg_gs_alloc_req(struct radv_shader_context *ctx,
3242 LLVMValueRef vtx_cnt,
3243 LLVMValueRef prim_cnt)
3244 {
3245 LLVMBuilderRef builder = ctx->ac.builder;
3246 LLVMValueRef tmp;
3247
3248 tmp = LLVMBuildICmp(builder, LLVMIntEQ, get_wave_id_in_tg(ctx), ctx->ac.i32_0, "");
3249 ac_build_ifcc(&ctx->ac, tmp, 5020);
3250
3251 tmp = LLVMBuildShl(builder, prim_cnt, LLVMConstInt(ctx->ac.i32, 12, false),"");
3252 tmp = LLVMBuildOr(builder, tmp, vtx_cnt, "");
3253 ac_build_sendmsg(&ctx->ac, AC_SENDMSG_GS_ALLOC_REQ, tmp);
3254
3255 ac_build_endif(&ctx->ac, 5020);
3256 }
3257
3258 struct ngg_prim {
3259 unsigned num_vertices;
3260 LLVMValueRef isnull;
3261 LLVMValueRef index[3];
3262 LLVMValueRef edgeflag[3];
3263 };
3264
3265 static void build_export_prim(struct radv_shader_context *ctx,
3266 const struct ngg_prim *prim)
3267 {
3268 LLVMBuilderRef builder = ctx->ac.builder;
3269 struct ac_export_args args;
3270 LLVMValueRef tmp;
3271
3272 tmp = LLVMBuildZExt(builder, prim->isnull, ctx->ac.i32, "");
3273 args.out[0] = LLVMBuildShl(builder, tmp, LLVMConstInt(ctx->ac.i32, 31, false), "");
3274
3275 for (unsigned i = 0; i < prim->num_vertices; ++i) {
3276 tmp = LLVMBuildShl(builder, prim->index[i],
3277 LLVMConstInt(ctx->ac.i32, 10 * i, false), "");
3278 args.out[0] = LLVMBuildOr(builder, args.out[0], tmp, "");
3279 tmp = LLVMBuildZExt(builder, prim->edgeflag[i], ctx->ac.i32, "");
3280 tmp = LLVMBuildShl(builder, tmp,
3281 LLVMConstInt(ctx->ac.i32, 10 * i + 9, false), "");
3282 args.out[0] = LLVMBuildOr(builder, args.out[0], tmp, "");
3283 }
3284
3285 args.out[0] = LLVMBuildBitCast(builder, args.out[0], ctx->ac.f32, "");
3286 args.out[1] = LLVMGetUndef(ctx->ac.f32);
3287 args.out[2] = LLVMGetUndef(ctx->ac.f32);
3288 args.out[3] = LLVMGetUndef(ctx->ac.f32);
3289
3290 args.target = V_008DFC_SQ_EXP_PRIM;
3291 args.enabled_channels = 1;
3292 args.done = true;
3293 args.valid_mask = false;
3294 args.compr = false;
3295
3296 ac_build_export(&ctx->ac, &args);
3297 }
3298
3299 static void
3300 handle_ngg_outputs_post(struct radv_shader_context *ctx)
3301 {
3302 LLVMBuilderRef builder = ctx->ac.builder;
3303 struct ac_build_if_state if_state;
3304 unsigned num_vertices = 3;
3305 LLVMValueRef tmp;
3306
3307 assert((ctx->stage == MESA_SHADER_VERTEX ||
3308 ctx->stage == MESA_SHADER_TESS_EVAL) && !ctx->is_gs_copy_shader);
3309
3310 LLVMValueRef prims_in_wave = ac_unpack_param(&ctx->ac, ctx->merged_wave_info, 8, 8);
3311 LLVMValueRef vtx_in_wave = ac_unpack_param(&ctx->ac, ctx->merged_wave_info, 0, 8);
3312 LLVMValueRef is_gs_thread = LLVMBuildICmp(builder, LLVMIntULT,
3313 ac_get_thread_id(&ctx->ac), prims_in_wave, "");
3314 LLVMValueRef is_es_thread = LLVMBuildICmp(builder, LLVMIntULT,
3315 ac_get_thread_id(&ctx->ac), vtx_in_wave, "");
3316 LLVMValueRef vtxindex[] = {
3317 ac_unpack_param(&ctx->ac, ctx->gs_vtx_offset[0], 0, 16),
3318 ac_unpack_param(&ctx->ac, ctx->gs_vtx_offset[0], 16, 16),
3319 ac_unpack_param(&ctx->ac, ctx->gs_vtx_offset[2], 0, 16),
3320 };
3321
3322 /* TODO: streamout */
3323
3324 /* Copy Primitive IDs from GS threads to the LDS address corresponding
3325 * to the ES thread of the provoking vertex.
3326 */
3327 if (ctx->stage == MESA_SHADER_VERTEX &&
3328 ctx->options->key.vs_common_out.export_prim_id) {
3329 /* TODO: streamout */
3330
3331 ac_build_ifcc(&ctx->ac, is_gs_thread, 5400);
3332 /* Extract the PROVOKING_VTX_INDEX field. */
3333 LLVMValueRef provoking_vtx_in_prim =
3334 LLVMConstInt(ctx->ac.i32, 0, false);
3335
3336 /* provoking_vtx_index = vtxindex[provoking_vtx_in_prim]; */
3337 LLVMValueRef indices = ac_build_gather_values(&ctx->ac, vtxindex, 3);
3338 LLVMValueRef provoking_vtx_index =
3339 LLVMBuildExtractElement(builder, indices, provoking_vtx_in_prim, "");
3340
3341 LLVMBuildStore(builder, ctx->abi.gs_prim_id,
3342 ac_build_gep0(&ctx->ac, ctx->esgs_ring, provoking_vtx_index));
3343 ac_build_endif(&ctx->ac, 5400);
3344 }
3345
3346 /* TODO: primitive culling */
3347
3348 build_sendmsg_gs_alloc_req(ctx, ngg_get_vtx_cnt(ctx), ngg_get_prim_cnt(ctx));
3349
3350 /* TODO: streamout queries */
3351 /* Export primitive data to the index buffer. Format is:
3352 * - bits 0..8: index 0
3353 * - bit 9: edge flag 0
3354 * - bits 10..18: index 1
3355 * - bit 19: edge flag 1
3356 * - bits 20..28: index 2
3357 * - bit 29: edge flag 2
3358 * - bit 31: null primitive (skip)
3359 *
3360 * For the first version, we will always build up all three indices
3361 * independent of the primitive type. The additional garbage data
3362 * shouldn't hurt.
3363 *
3364 * TODO: culling depends on the primitive type, so can have some
3365 * interaction here.
3366 */
3367 ac_nir_build_if(&if_state, ctx, is_gs_thread);
3368 {
3369 struct ngg_prim prim = {};
3370
3371 prim.num_vertices = num_vertices;
3372 prim.isnull = ctx->ac.i1false;
3373 memcpy(prim.index, vtxindex, sizeof(vtxindex[0]) * 3);
3374
3375 for (unsigned i = 0; i < num_vertices; ++i) {
3376 tmp = LLVMBuildLShr(builder, ctx->abi.gs_invocation_id,
3377 LLVMConstInt(ctx->ac.i32, 8 + i, false), "");
3378 prim.edgeflag[i] = LLVMBuildTrunc(builder, tmp, ctx->ac.i1, "");
3379 }
3380
3381 build_export_prim(ctx, &prim);
3382 }
3383 ac_nir_build_endif(&if_state);
3384
3385 /* Export per-vertex data (positions and parameters). */
3386 ac_nir_build_if(&if_state, ctx, is_es_thread);
3387 {
3388 struct radv_vs_output_info *outinfo =
3389 ctx->stage == MESA_SHADER_TESS_EVAL ? &ctx->shader_info->tes.outinfo : &ctx->shader_info->vs.outinfo;
3390
3391 /* Exporting the primitive ID is handled below. */
3392 /* TODO: use the new VS export path */
3393 handle_vs_outputs_post(ctx, false,
3394 ctx->options->key.vs_common_out.export_clip_dists,
3395 outinfo);
3396
3397 if (ctx->options->key.vs_common_out.export_prim_id) {
3398 unsigned param_count = outinfo->param_exports;
3399 LLVMValueRef values[4];
3400
3401 if (ctx->stage == MESA_SHADER_VERTEX) {
3402 /* Wait for GS stores to finish. */
3403 ac_build_s_barrier(&ctx->ac);
3404
3405 tmp = ac_build_gep0(&ctx->ac, ctx->esgs_ring,
3406 get_thread_id_in_tg(ctx));
3407 values[0] = LLVMBuildLoad(builder, tmp, "");
3408 } else {
3409 assert(ctx->stage == MESA_SHADER_TESS_EVAL);
3410 values[0] = ctx->abi.tes_patch_id;
3411 }
3412
3413 values[0] = ac_to_float(&ctx->ac, values[0]);
3414 for (unsigned j = 1; j < 4; j++)
3415 values[j] = ctx->ac.f32_0;
3416
3417 radv_export_param(ctx, param_count, values, 0x1);
3418
3419 outinfo->vs_output_param_offset[VARYING_SLOT_PRIMITIVE_ID] = param_count++;
3420 outinfo->export_prim_id = true;
3421 outinfo->param_exports = param_count;
3422 }
3423 }
3424 ac_nir_build_endif(&if_state);
3425 }
3426
3427 static void gfx10_ngg_gs_emit_prologue(struct radv_shader_context *ctx)
3428 {
3429 /* Zero out the part of LDS scratch that is used to accumulate the
3430 * per-stream generated primitive count.
3431 */
3432 LLVMBuilderRef builder = ctx->ac.builder;
3433 LLVMValueRef scratchptr = ctx->gs_ngg_scratch;
3434 LLVMValueRef tid = get_thread_id_in_tg(ctx);
3435 LLVMBasicBlockRef merge_block;
3436 LLVMValueRef cond;
3437
3438 LLVMValueRef fn = LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx->ac.builder));
3439 LLVMBasicBlockRef then_block = LLVMAppendBasicBlockInContext(ctx->ac.context, fn, "");
3440 merge_block = LLVMAppendBasicBlockInContext(ctx->ac.context, fn, "");
3441
3442 cond = LLVMBuildICmp(builder, LLVMIntULT, tid, LLVMConstInt(ctx->ac.i32, 4, false), "");
3443 LLVMBuildCondBr(ctx->ac.builder, cond, then_block, merge_block);
3444 LLVMPositionBuilderAtEnd(ctx->ac.builder, then_block);
3445
3446 LLVMValueRef ptr = ac_build_gep0(&ctx->ac, scratchptr, tid);
3447 LLVMBuildStore(builder, ctx->ac.i32_0, ptr);
3448
3449 LLVMBuildBr(ctx->ac.builder, merge_block);
3450 LLVMPositionBuilderAtEnd(ctx->ac.builder, merge_block);
3451
3452 ac_build_s_barrier(&ctx->ac);
3453 }
3454
3455 static void gfx10_ngg_gs_emit_epilogue_1(struct radv_shader_context *ctx)
3456 {
3457 LLVMBuilderRef builder = ctx->ac.builder;
3458 LLVMValueRef i8_0 = LLVMConstInt(ctx->ac.i8, 0, false);
3459 LLVMValueRef tmp;
3460
3461 /* Zero out remaining (non-emitted) primitive flags.
3462 *
3463 * Note: Alternatively, we could pass the relevant gs_next_vertex to
3464 * the emit threads via LDS. This is likely worse in the expected
3465 * typical case where each GS thread emits the full set of
3466 * vertices.
3467 */
3468 for (unsigned stream = 0; stream < 4; ++stream) {
3469 unsigned num_components;
3470
3471 num_components =
3472 ctx->shader_info->info.gs.num_stream_output_components[stream];
3473 if (!num_components)
3474 continue;
3475
3476 const LLVMValueRef gsthread = get_thread_id_in_tg(ctx);
3477
3478 ac_build_bgnloop(&ctx->ac, 5100);
3479
3480 const LLVMValueRef vertexidx =
3481 LLVMBuildLoad(builder, ctx->gs_next_vertex[stream], "");
3482 tmp = LLVMBuildICmp(builder, LLVMIntUGE, vertexidx,
3483 LLVMConstInt(ctx->ac.i32, ctx->gs_max_out_vertices, false), "");
3484 ac_build_ifcc(&ctx->ac, tmp, 5101);
3485 ac_build_break(&ctx->ac);
3486 ac_build_endif(&ctx->ac, 5101);
3487
3488 tmp = LLVMBuildAdd(builder, vertexidx, ctx->ac.i32_1, "");
3489 LLVMBuildStore(builder, tmp, ctx->gs_next_vertex[stream]);
3490
3491 tmp = ngg_gs_emit_vertex_ptr(ctx, gsthread, vertexidx);
3492 LLVMValueRef gep_idx[3] = {
3493 ctx->ac.i32_0, /* implied C-style array */
3494 ctx->ac.i32_1, /* second entry of struct */
3495 LLVMConstInt(ctx->ac.i32, stream, false),
3496 };
3497 tmp = LLVMBuildGEP(builder, tmp, gep_idx, 3, "");
3498 LLVMBuildStore(builder, i8_0, tmp);
3499
3500 ac_build_endloop(&ctx->ac, 5100);
3501 }
3502 }
3503
3504 static void gfx10_ngg_gs_emit_epilogue_2(struct radv_shader_context *ctx)
3505 {
3506 const unsigned verts_per_prim = si_conv_gl_prim_to_vertices(ctx->gs_output_prim);
3507 LLVMBuilderRef builder = ctx->ac.builder;
3508 LLVMValueRef tmp, tmp2;
3509
3510 ac_build_s_barrier(&ctx->ac);
3511
3512 const LLVMValueRef tid = get_thread_id_in_tg(ctx);
3513 LLVMValueRef num_emit_threads = ngg_get_prim_cnt(ctx);
3514
3515 /* TODO: streamout */
3516
3517 /* TODO: culling */
3518
3519 /* Determine vertex liveness. */
3520 LLVMValueRef vertliveptr = ac_build_alloca(&ctx->ac, ctx->ac.i1, "vertexlive");
3521
3522 tmp = LLVMBuildICmp(builder, LLVMIntULT, tid, num_emit_threads, "");
3523 ac_build_ifcc(&ctx->ac, tmp, 5120);
3524 {
3525 for (unsigned i = 0; i < verts_per_prim; ++i) {
3526 const LLVMValueRef primidx =
3527 LLVMBuildAdd(builder, tid,
3528 LLVMConstInt(ctx->ac.i32, i, false), "");
3529
3530 if (i > 0) {
3531 tmp = LLVMBuildICmp(builder, LLVMIntULT, primidx, num_emit_threads, "");
3532 ac_build_ifcc(&ctx->ac, tmp, 5121 + i);
3533 }
3534
3535 /* Load primitive liveness */
3536 tmp = ngg_gs_vertex_ptr(ctx, primidx);
3537 LLVMValueRef gep_idx[3] = {
3538 ctx->ac.i32_0, /* implicit C-style array */
3539 ctx->ac.i32_1, /* second value of struct */
3540 ctx->ac.i32_0, /* stream 0 */
3541 };
3542 tmp = LLVMBuildGEP(builder, tmp, gep_idx, 3, "");
3543 tmp = LLVMBuildLoad(builder, tmp, "");
3544 const LLVMValueRef primlive =
3545 LLVMBuildTrunc(builder, tmp, ctx->ac.i1, "");
3546
3547 tmp = LLVMBuildLoad(builder, vertliveptr, "");
3548 tmp = LLVMBuildOr(builder, tmp, primlive, ""),
3549 LLVMBuildStore(builder, tmp, vertliveptr);
3550
3551 if (i > 0)
3552 ac_build_endif(&ctx->ac, 5121 + i);
3553 }
3554 }
3555 ac_build_endif(&ctx->ac, 5120);
3556
3557 /* Inclusive scan addition across the current wave. */
3558 LLVMValueRef vertlive = LLVMBuildLoad(builder, vertliveptr, "");
3559 struct ac_wg_scan vertlive_scan = {};
3560 vertlive_scan.op = nir_op_iadd;
3561 vertlive_scan.enable_reduce = true;
3562 vertlive_scan.enable_exclusive = true;
3563 vertlive_scan.src = vertlive;
3564 vertlive_scan.scratch = ac_build_gep0(&ctx->ac, ctx->gs_ngg_scratch, ctx->ac.i32_0);
3565 vertlive_scan.waveidx = get_wave_id_in_tg(ctx);
3566 vertlive_scan.numwaves = get_tgsize(ctx);
3567 vertlive_scan.maxwaves = 8;
3568
3569 ac_build_wg_scan(&ctx->ac, &vertlive_scan);
3570
3571 /* Skip all exports (including index exports) when possible. At least on
3572 * early gfx10 revisions this is also to avoid hangs.
3573 */
3574 LLVMValueRef have_exports =
3575 LLVMBuildICmp(builder, LLVMIntNE, vertlive_scan.result_reduce, ctx->ac.i32_0, "");
3576 num_emit_threads =
3577 LLVMBuildSelect(builder, have_exports, num_emit_threads, ctx->ac.i32_0, "");
3578
3579 /* Allocate export space. Send this message as early as possible, to
3580 * hide the latency of the SQ <-> SPI roundtrip.
3581 *
3582 * Note: We could consider compacting primitives for export as well.
3583 * PA processes 1 non-null prim / clock, but it fetches 4 DW of
3584 * prim data per clock and skips null primitives at no additional
3585 * cost. So compacting primitives can only be beneficial when
3586 * there are 4 or more contiguous null primitives in the export
3587 * (in the common case of single-dword prim exports).
3588 */
3589 build_sendmsg_gs_alloc_req(ctx, vertlive_scan.result_reduce, num_emit_threads);
3590
3591 /* Setup the reverse vertex compaction permutation. We re-use stream 1
3592 * of the primitive liveness flags, relying on the fact that each
3593 * threadgroup can have at most 256 threads. */
3594 ac_build_ifcc(&ctx->ac, vertlive, 5130);
3595 {
3596 tmp = ngg_gs_vertex_ptr(ctx, vertlive_scan.result_exclusive);
3597 LLVMValueRef gep_idx[3] = {
3598 ctx->ac.i32_0, /* implicit C-style array */
3599 ctx->ac.i32_1, /* second value of struct */
3600 ctx->ac.i32_1, /* stream 1 */
3601 };
3602 tmp = LLVMBuildGEP(builder, tmp, gep_idx, 3, "");
3603 tmp2 = LLVMBuildTrunc(builder, tid, ctx->ac.i8, "");
3604 LLVMBuildStore(builder, tmp2, tmp);
3605 }
3606 ac_build_endif(&ctx->ac, 5130);
3607
3608 ac_build_s_barrier(&ctx->ac);
3609
3610 /* Export primitive data */
3611 tmp = LLVMBuildICmp(builder, LLVMIntULT, tid, num_emit_threads, "");
3612 ac_build_ifcc(&ctx->ac, tmp, 5140);
3613 {
3614 struct ngg_prim prim = {};
3615 prim.num_vertices = verts_per_prim;
3616
3617 tmp = ngg_gs_vertex_ptr(ctx, tid);
3618 LLVMValueRef gep_idx[3] = {
3619 ctx->ac.i32_0, /* implicit C-style array */
3620 ctx->ac.i32_1, /* second value of struct */
3621 ctx->ac.i32_0, /* primflag */
3622 };
3623 tmp = LLVMBuildGEP(builder, tmp, gep_idx, 3, "");
3624 tmp = LLVMBuildLoad(builder, tmp, "");
3625 prim.isnull = LLVMBuildICmp(builder, LLVMIntEQ, tmp,
3626 LLVMConstInt(ctx->ac.i8, 0, false), "");
3627
3628 for (unsigned i = 0; i < verts_per_prim; ++i) {
3629 prim.index[i] = LLVMBuildSub(builder, vertlive_scan.result_exclusive,
3630 LLVMConstInt(ctx->ac.i32, verts_per_prim - i - 1, false), "");
3631 prim.edgeflag[i] = ctx->ac.i1false;
3632 }
3633
3634 build_export_prim(ctx, &prim);
3635 }
3636 ac_build_endif(&ctx->ac, 5140);
3637
3638 /* Export position and parameter data */
3639 tmp = LLVMBuildICmp(builder, LLVMIntULT, tid, vertlive_scan.result_reduce, "");
3640 ac_build_ifcc(&ctx->ac, tmp, 5145);
3641 {
3642 struct radv_vs_output_info *outinfo = &ctx->shader_info->vs.outinfo;
3643 bool export_view_index = ctx->options->key.has_multiview_view_index;
3644 struct radv_shader_output_values *outputs;
3645 unsigned noutput = 0;
3646
3647 /* Allocate a temporary array for the output values. */
3648 unsigned num_outputs = util_bitcount64(ctx->output_mask) + export_view_index;
3649 outputs = calloc(num_outputs, sizeof(outputs[0]));
3650
3651 memset(outinfo->vs_output_param_offset, AC_EXP_PARAM_UNDEFINED,
3652 sizeof(outinfo->vs_output_param_offset));
3653 outinfo->pos_exports = 0;
3654
3655 tmp = ngg_gs_vertex_ptr(ctx, tid);
3656 LLVMValueRef gep_idx[3] = {
3657 ctx->ac.i32_0, /* implicit C-style array */
3658 ctx->ac.i32_1, /* second value of struct */
3659 ctx->ac.i32_1, /* stream 1: source data index */
3660 };
3661 tmp = LLVMBuildGEP(builder, tmp, gep_idx, 3, "");
3662 tmp = LLVMBuildLoad(builder, tmp, "");
3663 tmp = LLVMBuildZExt(builder, tmp, ctx->ac.i32, "");
3664 const LLVMValueRef vertexptr = ngg_gs_vertex_ptr(ctx, tmp);
3665
3666 if (ctx->output_mask & (1ull << VARYING_SLOT_PSIZ)) {
3667 outinfo->writes_pointsize = true;
3668 }
3669
3670 if (ctx->output_mask & (1ull << VARYING_SLOT_LAYER)) {
3671 outinfo->writes_layer = true;
3672 }
3673
3674 if (ctx->output_mask & (1ull << VARYING_SLOT_VIEWPORT)) {
3675 outinfo->writes_viewport_index = true;
3676 }
3677
3678 unsigned out_idx = 0;
3679 gep_idx[1] = ctx->ac.i32_0;
3680 for (unsigned i = 0; i < AC_LLVM_MAX_OUTPUTS; ++i) {
3681 if (!(ctx->output_mask & (1ull << i)))
3682 continue;
3683
3684 outputs[noutput].slot_name = i;
3685 outputs[noutput].slot_index = i == VARYING_SLOT_CLIP_DIST1;
3686
3687 outputs[noutput].usage_mask = ctx->shader_info->info.gs.output_usage_mask[i];
3688 int length = util_last_bit(outputs[noutput].usage_mask);
3689
3690 for (unsigned j = 0; j < length; j++, out_idx++) {
3691 gep_idx[2] = LLVMConstInt(ctx->ac.i32, out_idx, false);
3692 tmp = LLVMBuildGEP(builder, vertexptr, gep_idx, 3, "");
3693 tmp = LLVMBuildLoad(builder, tmp, "");
3694 outputs[noutput].values[j] = ac_to_float(&ctx->ac, tmp);
3695 }
3696
3697 for (unsigned j = length; j < 4; j++)
3698 outputs[noutput].values[j] = LLVMGetUndef(ctx->ac.f32);
3699
3700 noutput++;
3701 }
3702
3703 /* Export ViewIndex. */
3704 if (export_view_index) {
3705 outinfo->writes_layer = true;
3706
3707 outputs[noutput].slot_name = VARYING_SLOT_LAYER;
3708 outputs[noutput].slot_index = 0;
3709 outputs[noutput].usage_mask = 0x1;
3710 outputs[noutput].values[0] = ac_to_float(&ctx->ac, ctx->abi.view_index);
3711 for (unsigned j = 1; j < 4; j++)
3712 outputs[noutput].values[j] = ctx->ac.f32_0;
3713 noutput++;
3714 }
3715
3716 radv_llvm_export_vs(ctx, outputs, noutput, outinfo,
3717 ctx->options->key.vs_common_out.export_clip_dists);
3718 FREE(outputs);
3719 }
3720 ac_build_endif(&ctx->ac, 5145);
3721 }
3722
3723 static void gfx10_ngg_gs_emit_vertex(struct radv_shader_context *ctx,
3724 unsigned stream,
3725 LLVMValueRef *addrs)
3726 {
3727 LLVMBuilderRef builder = ctx->ac.builder;
3728 LLVMValueRef tmp;
3729 const LLVMValueRef vertexidx =
3730 LLVMBuildLoad(builder, ctx->gs_next_vertex[stream], "");
3731
3732 /* If this thread has already emitted the declared maximum number of
3733 * vertices, skip the write: excessive vertex emissions are not
3734 * supposed to have any effect.
3735 */
3736 const LLVMValueRef can_emit =
3737 LLVMBuildICmp(builder, LLVMIntULT, vertexidx,
3738 LLVMConstInt(ctx->ac.i32, ctx->gs_max_out_vertices, false), "");
3739 ac_build_kill_if_false(&ctx->ac, can_emit);
3740
3741 tmp = LLVMBuildAdd(builder, vertexidx, ctx->ac.i32_1, "");
3742 tmp = LLVMBuildSelect(builder, can_emit, tmp, vertexidx, "");
3743 LLVMBuildStore(builder, tmp, ctx->gs_next_vertex[stream]);
3744
3745 const LLVMValueRef vertexptr =
3746 ngg_gs_emit_vertex_ptr(ctx, get_thread_id_in_tg(ctx), vertexidx);
3747 unsigned out_idx = 0;
3748 for (unsigned i = 0; i < AC_LLVM_MAX_OUTPUTS; ++i) {
3749 unsigned output_usage_mask =
3750 ctx->shader_info->info.gs.output_usage_mask[i];
3751 uint8_t output_stream =
3752 ctx->shader_info->info.gs.output_streams[i];
3753 LLVMValueRef *out_ptr = &addrs[i * 4];
3754 int length = util_last_bit(output_usage_mask);
3755
3756 if (!(ctx->output_mask & (1ull << i)) ||
3757 output_stream != stream)
3758 continue;
3759
3760 for (unsigned j = 0; j < length; j++, out_idx++) {
3761 if (!(output_usage_mask & (1 << j)))
3762 continue;
3763
3764 LLVMValueRef out_val = LLVMBuildLoad(ctx->ac.builder,
3765 out_ptr[j], "");
3766 LLVMValueRef gep_idx[3] = {
3767 ctx->ac.i32_0, /* implied C-style array */
3768 ctx->ac.i32_0, /* first entry of struct */
3769 LLVMConstInt(ctx->ac.i32, out_idx, false),
3770 };
3771 LLVMValueRef ptr = LLVMBuildGEP(builder, vertexptr, gep_idx, 3, "");
3772
3773 out_val = ac_to_integer(&ctx->ac, out_val);
3774 LLVMBuildStore(builder, out_val, ptr);
3775 }
3776 }
3777 assert(out_idx * 4 <= ctx->gsvs_vertex_size);
3778
3779 /* Determine and store whether this vertex completed a primitive. */
3780 const LLVMValueRef curverts = LLVMBuildLoad(builder, ctx->gs_curprim_verts[stream], "");
3781
3782 tmp = LLVMConstInt(ctx->ac.i32, si_conv_gl_prim_to_vertices(ctx->gs_output_prim) - 1, false);
3783 const LLVMValueRef iscompleteprim =
3784 LLVMBuildICmp(builder, LLVMIntUGE, curverts, tmp, "");
3785
3786 tmp = LLVMBuildAdd(builder, curverts, ctx->ac.i32_1, "");
3787 LLVMBuildStore(builder, tmp, ctx->gs_curprim_verts[stream]);
3788
3789 LLVMValueRef gep_idx[3] = {
3790 ctx->ac.i32_0, /* implied C-style array */
3791 ctx->ac.i32_1, /* second struct entry */
3792 LLVMConstInt(ctx->ac.i32, stream, false),
3793 };
3794 const LLVMValueRef primflagptr =
3795 LLVMBuildGEP(builder, vertexptr, gep_idx, 3, "");
3796
3797 tmp = LLVMBuildZExt(builder, iscompleteprim, ctx->ac.i8, "");
3798 LLVMBuildStore(builder, tmp, primflagptr);
3799
3800 tmp = LLVMBuildLoad(builder, ctx->gs_generated_prims[stream], "");
3801 tmp = LLVMBuildAdd(builder, tmp, LLVMBuildZExt(builder, iscompleteprim, ctx->ac.i32, ""), "");
3802 LLVMBuildStore(builder, tmp, ctx->gs_generated_prims[stream]);
3803 }
3804
3805 static void
3806 write_tess_factors(struct radv_shader_context *ctx)
3807 {
3808 unsigned stride, outer_comps, inner_comps;
3809 struct ac_build_if_state if_ctx, inner_if_ctx;
3810 LLVMValueRef invocation_id = ac_unpack_param(&ctx->ac, ctx->abi.tcs_rel_ids, 8, 5);
3811 LLVMValueRef rel_patch_id = ac_unpack_param(&ctx->ac, ctx->abi.tcs_rel_ids, 0, 8);
3812 unsigned tess_inner_index = 0, tess_outer_index;
3813 LLVMValueRef lds_base, lds_inner = NULL, lds_outer, byteoffset, buffer;
3814 LLVMValueRef out[6], vec0, vec1, tf_base, inner[4], outer[4];
3815 int i;
3816 ac_emit_barrier(&ctx->ac, ctx->stage);
3817
3818 switch (ctx->options->key.tcs.primitive_mode) {
3819 case GL_ISOLINES:
3820 stride = 2;
3821 outer_comps = 2;
3822 inner_comps = 0;
3823 break;
3824 case GL_TRIANGLES:
3825 stride = 4;
3826 outer_comps = 3;
3827 inner_comps = 1;
3828 break;
3829 case GL_QUADS:
3830 stride = 6;
3831 outer_comps = 4;
3832 inner_comps = 2;
3833 break;
3834 default:
3835 return;
3836 }
3837
3838 ac_nir_build_if(&if_ctx, ctx,
3839 LLVMBuildICmp(ctx->ac.builder, LLVMIntEQ,
3840 invocation_id, ctx->ac.i32_0, ""));
3841
3842 lds_base = get_tcs_out_current_patch_data_offset(ctx);
3843
3844 if (inner_comps) {
3845 tess_inner_index = shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER);
3846 lds_inner = LLVMBuildAdd(ctx->ac.builder, lds_base,
3847 LLVMConstInt(ctx->ac.i32, tess_inner_index * 4, false), "");
3848 }
3849
3850 tess_outer_index = shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER);
3851 lds_outer = LLVMBuildAdd(ctx->ac.builder, lds_base,
3852 LLVMConstInt(ctx->ac.i32, tess_outer_index * 4, false), "");
3853
3854 for (i = 0; i < 4; i++) {
3855 inner[i] = LLVMGetUndef(ctx->ac.i32);
3856 outer[i] = LLVMGetUndef(ctx->ac.i32);
3857 }
3858
3859 // LINES reversal
3860 if (ctx->options->key.tcs.primitive_mode == GL_ISOLINES) {
3861 outer[0] = out[1] = ac_lds_load(&ctx->ac, lds_outer);
3862 lds_outer = LLVMBuildAdd(ctx->ac.builder, lds_outer,
3863 ctx->ac.i32_1, "");
3864 outer[1] = out[0] = ac_lds_load(&ctx->ac, lds_outer);
3865 } else {
3866 for (i = 0; i < outer_comps; i++) {
3867 outer[i] = out[i] =
3868 ac_lds_load(&ctx->ac, lds_outer);
3869 lds_outer = LLVMBuildAdd(ctx->ac.builder, lds_outer,
3870 ctx->ac.i32_1, "");
3871 }
3872 for (i = 0; i < inner_comps; i++) {
3873 inner[i] = out[outer_comps+i] =
3874 ac_lds_load(&ctx->ac, lds_inner);
3875 lds_inner = LLVMBuildAdd(ctx->ac.builder, lds_inner,
3876 ctx->ac.i32_1, "");
3877 }
3878 }
3879
3880 /* Convert the outputs to vectors for stores. */
3881 vec0 = ac_build_gather_values(&ctx->ac, out, MIN2(stride, 4));
3882 vec1 = NULL;
3883
3884 if (stride > 4)
3885 vec1 = ac_build_gather_values(&ctx->ac, out + 4, stride - 4);
3886
3887
3888 buffer = ctx->hs_ring_tess_factor;
3889 tf_base = ctx->tess_factor_offset;
3890 byteoffset = LLVMBuildMul(ctx->ac.builder, rel_patch_id,
3891 LLVMConstInt(ctx->ac.i32, 4 * stride, false), "");
3892 unsigned tf_offset = 0;
3893
3894 if (ctx->options->chip_class <= GFX8) {
3895 ac_nir_build_if(&inner_if_ctx, ctx,
3896 LLVMBuildICmp(ctx->ac.builder, LLVMIntEQ,
3897 rel_patch_id, ctx->ac.i32_0, ""));
3898
3899 /* Store the dynamic HS control word. */
3900 ac_build_buffer_store_dword(&ctx->ac, buffer,
3901 LLVMConstInt(ctx->ac.i32, 0x80000000, false),
3902 1, ctx->ac.i32_0, tf_base,
3903 0, ac_glc, false);
3904 tf_offset += 4;
3905
3906 ac_nir_build_endif(&inner_if_ctx);
3907 }
3908
3909 /* Store the tessellation factors. */
3910 ac_build_buffer_store_dword(&ctx->ac, buffer, vec0,
3911 MIN2(stride, 4), byteoffset, tf_base,
3912 tf_offset, ac_glc, false);
3913 if (vec1)
3914 ac_build_buffer_store_dword(&ctx->ac, buffer, vec1,
3915 stride - 4, byteoffset, tf_base,
3916 16 + tf_offset, ac_glc, false);
3917
3918 //store to offchip for TES to read - only if TES reads them
3919 if (ctx->options->key.tcs.tes_reads_tess_factors) {
3920 LLVMValueRef inner_vec, outer_vec, tf_outer_offset;
3921 LLVMValueRef tf_inner_offset;
3922 unsigned param_outer, param_inner;
3923
3924 param_outer = shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_OUTER);
3925 tf_outer_offset = get_tcs_tes_buffer_address(ctx, NULL,
3926 LLVMConstInt(ctx->ac.i32, param_outer, 0));
3927
3928 outer_vec = ac_build_gather_values(&ctx->ac, outer,
3929 util_next_power_of_two(outer_comps));
3930
3931 ac_build_buffer_store_dword(&ctx->ac, ctx->hs_ring_tess_offchip, outer_vec,
3932 outer_comps, tf_outer_offset,
3933 ctx->oc_lds, 0, ac_glc, false);
3934 if (inner_comps) {
3935 param_inner = shader_io_get_unique_index(VARYING_SLOT_TESS_LEVEL_INNER);
3936 tf_inner_offset = get_tcs_tes_buffer_address(ctx, NULL,
3937 LLVMConstInt(ctx->ac.i32, param_inner, 0));
3938
3939 inner_vec = inner_comps == 1 ? inner[0] :
3940 ac_build_gather_values(&ctx->ac, inner, inner_comps);
3941 ac_build_buffer_store_dword(&ctx->ac, ctx->hs_ring_tess_offchip, inner_vec,
3942 inner_comps, tf_inner_offset,
3943 ctx->oc_lds, 0, ac_glc, false);
3944 }
3945 }
3946 ac_nir_build_endif(&if_ctx);
3947 }
3948
3949 static void
3950 handle_tcs_outputs_post(struct radv_shader_context *ctx)
3951 {
3952 write_tess_factors(ctx);
3953 }
3954
3955 static bool
3956 si_export_mrt_color(struct radv_shader_context *ctx,
3957 LLVMValueRef *color, unsigned index,
3958 struct ac_export_args *args)
3959 {
3960 /* Export */
3961 si_llvm_init_export_args(ctx, color, 0xf,
3962 V_008DFC_SQ_EXP_MRT + index, args);
3963 if (!args->enabled_channels)
3964 return false; /* unnecessary NULL export */
3965
3966 return true;
3967 }
3968
3969 static void
3970 radv_export_mrt_z(struct radv_shader_context *ctx,
3971 LLVMValueRef depth, LLVMValueRef stencil,
3972 LLVMValueRef samplemask)
3973 {
3974 struct ac_export_args args;
3975
3976 ac_export_mrt_z(&ctx->ac, depth, stencil, samplemask, &args);
3977
3978 ac_build_export(&ctx->ac, &args);
3979 }
3980
3981 static void
3982 handle_fs_outputs_post(struct radv_shader_context *ctx)
3983 {
3984 unsigned index = 0;
3985 LLVMValueRef depth = NULL, stencil = NULL, samplemask = NULL;
3986 struct ac_export_args color_args[8];
3987
3988 for (unsigned i = 0; i < AC_LLVM_MAX_OUTPUTS; ++i) {
3989 LLVMValueRef values[4];
3990
3991 if (!(ctx->output_mask & (1ull << i)))
3992 continue;
3993
3994 if (i < FRAG_RESULT_DATA0)
3995 continue;
3996
3997 for (unsigned j = 0; j < 4; j++)
3998 values[j] = ac_to_float(&ctx->ac,
3999 radv_load_output(ctx, i, j));
4000
4001 bool ret = si_export_mrt_color(ctx, values,
4002 i - FRAG_RESULT_DATA0,
4003 &color_args[index]);
4004 if (ret)
4005 index++;
4006 }
4007
4008 /* Process depth, stencil, samplemask. */
4009 if (ctx->shader_info->info.ps.writes_z) {
4010 depth = ac_to_float(&ctx->ac,
4011 radv_load_output(ctx, FRAG_RESULT_DEPTH, 0));
4012 }
4013 if (ctx->shader_info->info.ps.writes_stencil) {
4014 stencil = ac_to_float(&ctx->ac,
4015 radv_load_output(ctx, FRAG_RESULT_STENCIL, 0));
4016 }
4017 if (ctx->shader_info->info.ps.writes_sample_mask) {
4018 samplemask = ac_to_float(&ctx->ac,
4019 radv_load_output(ctx, FRAG_RESULT_SAMPLE_MASK, 0));
4020 }
4021
4022 /* Set the DONE bit on last non-null color export only if Z isn't
4023 * exported.
4024 */
4025 if (index > 0 &&
4026 !ctx->shader_info->info.ps.writes_z &&
4027 !ctx->shader_info->info.ps.writes_stencil &&
4028 !ctx->shader_info->info.ps.writes_sample_mask) {
4029 unsigned last = index - 1;
4030
4031 color_args[last].valid_mask = 1; /* whether the EXEC mask is valid */
4032 color_args[last].done = 1; /* DONE bit */
4033 }
4034
4035 /* Export PS outputs. */
4036 for (unsigned i = 0; i < index; i++)
4037 ac_build_export(&ctx->ac, &color_args[i]);
4038
4039 if (depth || stencil || samplemask)
4040 radv_export_mrt_z(ctx, depth, stencil, samplemask);
4041 else if (!index)
4042 ac_build_export_null(&ctx->ac);
4043 }
4044
4045 static void
4046 emit_gs_epilogue(struct radv_shader_context *ctx)
4047 {
4048 if (ctx->options->key.vs_common_out.as_ngg) {
4049 gfx10_ngg_gs_emit_epilogue_1(ctx);
4050 return;
4051 }
4052
4053 if (ctx->ac.chip_class >= GFX10)
4054 LLVMBuildFence(ctx->ac.builder, LLVMAtomicOrderingRelease, false, "");
4055
4056 ac_build_sendmsg(&ctx->ac, AC_SENDMSG_GS_OP_NOP | AC_SENDMSG_GS_DONE, ctx->gs_wave_id);
4057 }
4058
4059 static void
4060 handle_shader_outputs_post(struct ac_shader_abi *abi, unsigned max_outputs,
4061 LLVMValueRef *addrs)
4062 {
4063 struct radv_shader_context *ctx = radv_shader_context_from_abi(abi);
4064
4065 switch (ctx->stage) {
4066 case MESA_SHADER_VERTEX:
4067 if (ctx->options->key.vs_common_out.as_ls)
4068 handle_ls_outputs_post(ctx);
4069 else if (ctx->options->key.vs_common_out.as_es)
4070 handle_es_outputs_post(ctx, &ctx->shader_info->vs.es_info);
4071 else if (ctx->options->key.vs_common_out.as_ngg)
4072 break; /* handled outside of the shader body */
4073 else
4074 handle_vs_outputs_post(ctx, ctx->options->key.vs_common_out.export_prim_id,
4075 ctx->options->key.vs_common_out.export_clip_dists,
4076 &ctx->shader_info->vs.outinfo);
4077 break;
4078 case MESA_SHADER_FRAGMENT:
4079 handle_fs_outputs_post(ctx);
4080 break;
4081 case MESA_SHADER_GEOMETRY:
4082 emit_gs_epilogue(ctx);
4083 break;
4084 case MESA_SHADER_TESS_CTRL:
4085 handle_tcs_outputs_post(ctx);
4086 break;
4087 case MESA_SHADER_TESS_EVAL:
4088 if (ctx->options->key.vs_common_out.as_ngg)
4089 break; /* handled outside of the shader body */
4090 else if (ctx->options->key.vs_common_out.as_es)
4091 handle_es_outputs_post(ctx, &ctx->shader_info->tes.es_info);
4092 else
4093 handle_vs_outputs_post(ctx, ctx->options->key.vs_common_out.export_prim_id,
4094 ctx->options->key.vs_common_out.export_clip_dists,
4095 &ctx->shader_info->tes.outinfo);
4096 break;
4097 default:
4098 break;
4099 }
4100 }
4101
4102 static void ac_llvm_finalize_module(struct radv_shader_context *ctx,
4103 LLVMPassManagerRef passmgr,
4104 const struct radv_nir_compiler_options *options)
4105 {
4106 LLVMRunPassManager(passmgr, ctx->ac.module);
4107 LLVMDisposeBuilder(ctx->ac.builder);
4108
4109 ac_llvm_context_dispose(&ctx->ac);
4110 }
4111
4112 static void
4113 ac_nir_eliminate_const_vs_outputs(struct radv_shader_context *ctx)
4114 {
4115 struct radv_vs_output_info *outinfo;
4116
4117 switch (ctx->stage) {
4118 case MESA_SHADER_FRAGMENT:
4119 case MESA_SHADER_COMPUTE:
4120 case MESA_SHADER_TESS_CTRL:
4121 case MESA_SHADER_GEOMETRY:
4122 return;
4123 case MESA_SHADER_VERTEX:
4124 if (ctx->options->key.vs_common_out.as_ls ||
4125 ctx->options->key.vs_common_out.as_es)
4126 return;
4127 outinfo = &ctx->shader_info->vs.outinfo;
4128 break;
4129 case MESA_SHADER_TESS_EVAL:
4130 if (ctx->options->key.vs_common_out.as_es)
4131 return;
4132 outinfo = &ctx->shader_info->tes.outinfo;
4133 break;
4134 default:
4135 unreachable("Unhandled shader type");
4136 }
4137
4138 ac_optimize_vs_outputs(&ctx->ac,
4139 ctx->main_function,
4140 outinfo->vs_output_param_offset,
4141 VARYING_SLOT_MAX,
4142 &outinfo->param_exports);
4143 }
4144
4145 static void
4146 ac_setup_rings(struct radv_shader_context *ctx)
4147 {
4148 if (ctx->options->chip_class <= GFX8 &&
4149 (ctx->stage == MESA_SHADER_GEOMETRY ||
4150 ctx->options->key.vs_common_out.as_es || ctx->options->key.vs_common_out.as_es)) {
4151 unsigned ring = ctx->stage == MESA_SHADER_GEOMETRY ? RING_ESGS_GS
4152 : RING_ESGS_VS;
4153 LLVMValueRef offset = LLVMConstInt(ctx->ac.i32, ring, false);
4154
4155 ctx->esgs_ring = ac_build_load_to_sgpr(&ctx->ac,
4156 ctx->ring_offsets,
4157 offset);
4158 }
4159
4160 if (ctx->is_gs_copy_shader) {
4161 ctx->gsvs_ring[0] =
4162 ac_build_load_to_sgpr(&ctx->ac, ctx->ring_offsets,
4163 LLVMConstInt(ctx->ac.i32,
4164 RING_GSVS_VS, false));
4165 }
4166
4167 if (ctx->stage == MESA_SHADER_GEOMETRY) {
4168 /* The conceptual layout of the GSVS ring is
4169 * v0c0 .. vLv0 v0c1 .. vLc1 ..
4170 * but the real memory layout is swizzled across
4171 * threads:
4172 * t0v0c0 .. t15v0c0 t0v1c0 .. t15v1c0 ... t15vLcL
4173 * t16v0c0 ..
4174 * Override the buffer descriptor accordingly.
4175 */
4176 LLVMTypeRef v2i64 = LLVMVectorType(ctx->ac.i64, 2);
4177 uint64_t stream_offset = 0;
4178 unsigned num_records = 64;
4179 LLVMValueRef base_ring;
4180
4181 base_ring =
4182 ac_build_load_to_sgpr(&ctx->ac, ctx->ring_offsets,
4183 LLVMConstInt(ctx->ac.i32,
4184 RING_GSVS_GS, false));
4185
4186 for (unsigned stream = 0; stream < 4; stream++) {
4187 unsigned num_components, stride;
4188 LLVMValueRef ring, tmp;
4189
4190 num_components =
4191 ctx->shader_info->info.gs.num_stream_output_components[stream];
4192
4193 if (!num_components)
4194 continue;
4195
4196 stride = 4 * num_components * ctx->gs_max_out_vertices;
4197
4198 /* Limit on the stride field for <= GFX7. */
4199 assert(stride < (1 << 14));
4200
4201 ring = LLVMBuildBitCast(ctx->ac.builder,
4202 base_ring, v2i64, "");
4203 tmp = LLVMBuildExtractElement(ctx->ac.builder,
4204 ring, ctx->ac.i32_0, "");
4205 tmp = LLVMBuildAdd(ctx->ac.builder, tmp,
4206 LLVMConstInt(ctx->ac.i64,
4207 stream_offset, 0), "");
4208 ring = LLVMBuildInsertElement(ctx->ac.builder,
4209 ring, tmp, ctx->ac.i32_0, "");
4210
4211 stream_offset += stride * 64;
4212
4213 ring = LLVMBuildBitCast(ctx->ac.builder, ring,
4214 ctx->ac.v4i32, "");
4215
4216 tmp = LLVMBuildExtractElement(ctx->ac.builder, ring,
4217 ctx->ac.i32_1, "");
4218 tmp = LLVMBuildOr(ctx->ac.builder, tmp,
4219 LLVMConstInt(ctx->ac.i32,
4220 S_008F04_STRIDE(stride), false), "");
4221 ring = LLVMBuildInsertElement(ctx->ac.builder, ring, tmp,
4222 ctx->ac.i32_1, "");
4223
4224 ring = LLVMBuildInsertElement(ctx->ac.builder, ring,
4225 LLVMConstInt(ctx->ac.i32,
4226 num_records, false),
4227 LLVMConstInt(ctx->ac.i32, 2, false), "");
4228
4229 ctx->gsvs_ring[stream] = ring;
4230 }
4231 }
4232
4233 if (ctx->stage == MESA_SHADER_TESS_CTRL ||
4234 ctx->stage == MESA_SHADER_TESS_EVAL) {
4235 ctx->hs_ring_tess_offchip = ac_build_load_to_sgpr(&ctx->ac, ctx->ring_offsets, LLVMConstInt(ctx->ac.i32, RING_HS_TESS_OFFCHIP, false));
4236 ctx->hs_ring_tess_factor = ac_build_load_to_sgpr(&ctx->ac, ctx->ring_offsets, LLVMConstInt(ctx->ac.i32, RING_HS_TESS_FACTOR, false));
4237 }
4238 }
4239
4240 unsigned
4241 radv_nir_get_max_workgroup_size(enum chip_class chip_class,
4242 const struct nir_shader *nir)
4243 {
4244 switch (nir->info.stage) {
4245 case MESA_SHADER_TESS_CTRL:
4246 return chip_class >= GFX7 ? 128 : 64;
4247 case MESA_SHADER_GEOMETRY:
4248 return chip_class >= GFX9 ? 128 : 64;
4249 case MESA_SHADER_COMPUTE:
4250 break;
4251 default:
4252 return 0;
4253 }
4254
4255 unsigned max_workgroup_size = nir->info.cs.local_size[0] *
4256 nir->info.cs.local_size[1] *
4257 nir->info.cs.local_size[2];
4258 return max_workgroup_size;
4259 }
4260
4261 /* Fixup the HW not emitting the TCS regs if there are no HS threads. */
4262 static void ac_nir_fixup_ls_hs_input_vgprs(struct radv_shader_context *ctx)
4263 {
4264 LLVMValueRef count = ac_unpack_param(&ctx->ac, ctx->merged_wave_info, 8, 8);
4265 LLVMValueRef hs_empty = LLVMBuildICmp(ctx->ac.builder, LLVMIntEQ, count,
4266 ctx->ac.i32_0, "");
4267 ctx->abi.instance_id = LLVMBuildSelect(ctx->ac.builder, hs_empty, ctx->rel_auto_id, ctx->abi.instance_id, "");
4268 ctx->rel_auto_id = LLVMBuildSelect(ctx->ac.builder, hs_empty, ctx->abi.tcs_rel_ids, ctx->rel_auto_id, "");
4269 ctx->abi.vertex_id = LLVMBuildSelect(ctx->ac.builder, hs_empty, ctx->abi.tcs_patch_id, ctx->abi.vertex_id, "");
4270 }
4271
4272 static void prepare_gs_input_vgprs(struct radv_shader_context *ctx)
4273 {
4274 for(int i = 5; i >= 0; --i) {
4275 ctx->gs_vtx_offset[i] = ac_unpack_param(&ctx->ac, ctx->gs_vtx_offset[i & ~1],
4276 (i & 1) * 16, 16);
4277 }
4278
4279 ctx->gs_wave_id = ac_unpack_param(&ctx->ac, ctx->merged_wave_info, 16, 8);
4280 }
4281
4282 /* Ensure that the esgs ring is declared.
4283 *
4284 * We declare it with 64KB alignment as a hint that the
4285 * pointer value will always be 0.
4286 */
4287 static void declare_esgs_ring(struct radv_shader_context *ctx)
4288 {
4289 if (ctx->esgs_ring)
4290 return;
4291
4292 assert(!LLVMGetNamedGlobal(ctx->ac.module, "esgs_ring"));
4293
4294 ctx->esgs_ring = LLVMAddGlobalInAddressSpace(
4295 ctx->ac.module, LLVMArrayType(ctx->ac.i32, 0),
4296 "esgs_ring",
4297 AC_ADDR_SPACE_LDS);
4298 LLVMSetLinkage(ctx->esgs_ring, LLVMExternalLinkage);
4299 LLVMSetAlignment(ctx->esgs_ring, 64 * 1024);
4300 }
4301
4302 static
4303 LLVMModuleRef ac_translate_nir_to_llvm(struct ac_llvm_compiler *ac_llvm,
4304 struct nir_shader *const *shaders,
4305 int shader_count,
4306 struct radv_shader_variant_info *shader_info,
4307 const struct radv_nir_compiler_options *options)
4308 {
4309 struct radv_shader_context ctx = {0};
4310 unsigned i;
4311 ctx.options = options;
4312 ctx.shader_info = shader_info;
4313
4314 ac_llvm_context_init(&ctx.ac, options->chip_class, options->family);
4315 ctx.context = ctx.ac.context;
4316 ctx.ac.module = ac_create_module(ac_llvm->tm, ctx.context);
4317
4318 enum ac_float_mode float_mode =
4319 options->unsafe_math ? AC_FLOAT_MODE_UNSAFE_FP_MATH :
4320 AC_FLOAT_MODE_DEFAULT;
4321
4322 ctx.ac.builder = ac_create_builder(ctx.context, float_mode);
4323
4324 radv_nir_shader_info_init(&shader_info->info);
4325
4326 for(int i = 0; i < shader_count; ++i)
4327 radv_nir_shader_info_pass(shaders[i], options, &shader_info->info);
4328
4329 for (i = 0; i < RADV_UD_MAX_SETS; i++)
4330 shader_info->user_sgprs_locs.descriptor_sets[i].sgpr_idx = -1;
4331 for (i = 0; i < AC_UD_MAX_UD; i++)
4332 shader_info->user_sgprs_locs.shader_data[i].sgpr_idx = -1;
4333
4334 ctx.max_workgroup_size = 0;
4335 for (int i = 0; i < shader_count; ++i) {
4336 ctx.max_workgroup_size = MAX2(ctx.max_workgroup_size,
4337 radv_nir_get_max_workgroup_size(ctx.options->chip_class,
4338 shaders[i]));
4339 }
4340
4341 if (ctx.ac.chip_class >= GFX10) {
4342 if (is_pre_gs_stage(shaders[0]->info.stage) &&
4343 options->key.vs_common_out.as_ngg) {
4344 ctx.max_workgroup_size = 128;
4345 }
4346 }
4347
4348 create_function(&ctx, shaders[shader_count - 1]->info.stage, shader_count >= 2,
4349 shader_count >= 2 ? shaders[shader_count - 2]->info.stage : MESA_SHADER_VERTEX);
4350
4351 ctx.abi.inputs = &ctx.inputs[0];
4352 ctx.abi.emit_outputs = handle_shader_outputs_post;
4353 ctx.abi.emit_vertex = visit_emit_vertex;
4354 ctx.abi.load_ubo = radv_load_ubo;
4355 ctx.abi.load_ssbo = radv_load_ssbo;
4356 ctx.abi.load_sampler_desc = radv_get_sampler_desc;
4357 ctx.abi.load_resource = radv_load_resource;
4358 ctx.abi.clamp_shadow_reference = false;
4359 ctx.abi.gfx9_stride_size_workaround = ctx.ac.chip_class == GFX9 && HAVE_LLVM < 0x800;
4360
4361 /* Because the new raw/struct atomic intrinsics are buggy with LLVM 8,
4362 * we fallback to the old intrinsics for atomic buffer image operations
4363 * and thus we need to apply the indexing workaround...
4364 */
4365 ctx.abi.gfx9_stride_size_workaround_for_atomic = ctx.ac.chip_class == GFX9 && HAVE_LLVM < 0x900;
4366
4367 bool is_ngg = is_pre_gs_stage(shaders[0]->info.stage) && ctx.options->key.vs_common_out.as_ngg;
4368 if (shader_count >= 2 || is_ngg)
4369 ac_init_exec_full_mask(&ctx.ac);
4370
4371 if ((ctx.ac.family == CHIP_VEGA10 ||
4372 ctx.ac.family == CHIP_RAVEN) &&
4373 shaders[shader_count - 1]->info.stage == MESA_SHADER_TESS_CTRL)
4374 ac_nir_fixup_ls_hs_input_vgprs(&ctx);
4375
4376 for(int i = 0; i < shader_count; ++i) {
4377 ctx.stage = shaders[i]->info.stage;
4378 ctx.output_mask = 0;
4379
4380 if (shaders[i]->info.stage == MESA_SHADER_GEOMETRY) {
4381 for (int i = 0; i < 4; i++) {
4382 ctx.gs_next_vertex[i] =
4383 ac_build_alloca(&ctx.ac, ctx.ac.i32, "");
4384 }
4385 if (ctx.options->key.vs_common_out.as_ngg) {
4386 for (unsigned i = 0; i < 4; ++i) {
4387 ctx.gs_curprim_verts[i] =
4388 ac_build_alloca(&ctx.ac, ctx.ac.i32, "");
4389 ctx.gs_generated_prims[i] =
4390 ac_build_alloca(&ctx.ac, ctx.ac.i32, "");
4391 }
4392
4393 /* TODO: streamout */
4394
4395 LLVMTypeRef ai32 = LLVMArrayType(ctx.ac.i32, 8);
4396 ctx.gs_ngg_scratch =
4397 LLVMAddGlobalInAddressSpace(ctx.ac.module,
4398 ai32, "ngg_scratch", AC_ADDR_SPACE_LDS);
4399 LLVMSetInitializer(ctx.gs_ngg_scratch, LLVMGetUndef(ai32));
4400 LLVMSetAlignment(ctx.gs_ngg_scratch, 4);
4401
4402 ctx.gs_ngg_emit = LLVMBuildIntToPtr(ctx.ac.builder, ctx.ac.i32_0,
4403 LLVMPointerType(LLVMArrayType(ctx.ac.i32, 0), AC_ADDR_SPACE_LDS),
4404 "ngg_emit");
4405 }
4406
4407 ctx.gs_max_out_vertices = shaders[i]->info.gs.vertices_out;
4408 ctx.gs_output_prim = shaders[i]->info.gs.output_primitive;
4409 ctx.abi.load_inputs = load_gs_input;
4410 ctx.abi.emit_primitive = visit_end_primitive;
4411 } else if (shaders[i]->info.stage == MESA_SHADER_TESS_CTRL) {
4412 ctx.tcs_outputs_read = shaders[i]->info.outputs_read;
4413 ctx.tcs_patch_outputs_read = shaders[i]->info.patch_outputs_read;
4414 ctx.abi.load_tess_varyings = load_tcs_varyings;
4415 ctx.abi.load_patch_vertices_in = load_patch_vertices_in;
4416 ctx.abi.store_tcs_outputs = store_tcs_output;
4417 ctx.tcs_vertices_per_patch = shaders[i]->info.tess.tcs_vertices_out;
4418 if (shader_count == 1)
4419 ctx.tcs_num_inputs = ctx.options->key.tcs.num_inputs;
4420 else
4421 ctx.tcs_num_inputs = util_last_bit64(shader_info->info.vs.ls_outputs_written);
4422 ctx.tcs_num_patches = get_tcs_num_patches(&ctx);
4423 } else if (shaders[i]->info.stage == MESA_SHADER_TESS_EVAL) {
4424 ctx.tes_primitive_mode = shaders[i]->info.tess.primitive_mode;
4425 ctx.abi.load_tess_varyings = load_tes_input;
4426 ctx.abi.load_tess_coord = load_tess_coord;
4427 ctx.abi.load_patch_vertices_in = load_patch_vertices_in;
4428 ctx.tcs_vertices_per_patch = shaders[i]->info.tess.tcs_vertices_out;
4429 ctx.tcs_num_patches = ctx.options->key.tes.num_patches;
4430 } else if (shaders[i]->info.stage == MESA_SHADER_VERTEX) {
4431 ctx.abi.load_base_vertex = radv_load_base_vertex;
4432 } else if (shaders[i]->info.stage == MESA_SHADER_FRAGMENT) {
4433 shader_info->fs.can_discard = shaders[i]->info.fs.uses_discard;
4434 ctx.abi.lookup_interp_param = lookup_interp_param;
4435 ctx.abi.load_sample_position = load_sample_position;
4436 ctx.abi.load_sample_mask_in = load_sample_mask_in;
4437 ctx.abi.emit_kill = radv_emit_kill;
4438 }
4439
4440 if (shaders[i]->info.stage == MESA_SHADER_VERTEX &&
4441 ctx.options->key.vs_common_out.as_ngg &&
4442 ctx.options->key.vs_common_out.export_prim_id) {
4443 declare_esgs_ring(&ctx);
4444 }
4445
4446 if (i)
4447 ac_emit_barrier(&ctx.ac, ctx.stage);
4448
4449 nir_foreach_variable(variable, &shaders[i]->outputs)
4450 scan_shader_output_decl(&ctx, variable, shaders[i], shaders[i]->info.stage);
4451
4452 if (shaders[i]->info.stage == MESA_SHADER_GEOMETRY) {
4453 unsigned addclip = shaders[i]->info.clip_distance_array_size +
4454 shaders[i]->info.cull_distance_array_size > 4;
4455 ctx.gsvs_vertex_size = (util_bitcount64(ctx.output_mask) + addclip) * 16;
4456 ctx.max_gsvs_emit_size = ctx.gsvs_vertex_size *
4457 shaders[i]->info.gs.vertices_out;
4458 }
4459
4460 ac_setup_rings(&ctx);
4461
4462 LLVMBasicBlockRef merge_block;
4463 if (shader_count >= 2 || is_ngg) {
4464
4465 if (shaders[i]->info.stage == MESA_SHADER_GEOMETRY &&
4466 ctx.options->key.vs_common_out.as_ngg) {
4467 gfx10_ngg_gs_emit_prologue(&ctx);
4468 }
4469
4470 LLVMValueRef fn = LLVMGetBasicBlockParent(LLVMGetInsertBlock(ctx.ac.builder));
4471 LLVMBasicBlockRef then_block = LLVMAppendBasicBlockInContext(ctx.ac.context, fn, "");
4472 merge_block = LLVMAppendBasicBlockInContext(ctx.ac.context, fn, "");
4473
4474 LLVMValueRef count = ac_unpack_param(&ctx.ac, ctx.merged_wave_info, 8 * i, 8);
4475 LLVMValueRef thread_id = ac_get_thread_id(&ctx.ac);
4476 LLVMValueRef cond = LLVMBuildICmp(ctx.ac.builder, LLVMIntULT,
4477 thread_id, count, "");
4478 LLVMBuildCondBr(ctx.ac.builder, cond, then_block, merge_block);
4479
4480 LLVMPositionBuilderAtEnd(ctx.ac.builder, then_block);
4481 }
4482
4483 if (shaders[i]->info.stage == MESA_SHADER_FRAGMENT)
4484 prepare_interp_optimize(&ctx, shaders[i]);
4485 else if(shaders[i]->info.stage == MESA_SHADER_VERTEX)
4486 handle_vs_inputs(&ctx, shaders[i]);
4487 else if(shader_count >= 2 && shaders[i]->info.stage == MESA_SHADER_GEOMETRY)
4488 prepare_gs_input_vgprs(&ctx);
4489
4490 ac_nir_translate(&ctx.ac, &ctx.abi, shaders[i]);
4491
4492 if (shader_count >= 2 || is_ngg) {
4493 LLVMBuildBr(ctx.ac.builder, merge_block);
4494 LLVMPositionBuilderAtEnd(ctx.ac.builder, merge_block);
4495 }
4496
4497 /* This needs to be outside the if wrapping the shader body, as sometimes
4498 * the HW generates waves with 0 es/vs threads. */
4499 if (is_pre_gs_stage(shaders[i]->info.stage) &&
4500 ctx.options->key.vs_common_out.as_ngg &&
4501 i == shader_count - 1) {
4502 handle_ngg_outputs_post(&ctx);
4503 } else if (shaders[i]->info.stage == MESA_SHADER_GEOMETRY &&
4504 ctx.options->key.vs_common_out.as_ngg) {
4505 gfx10_ngg_gs_emit_epilogue_2(&ctx);
4506 }
4507
4508 if (shaders[i]->info.stage == MESA_SHADER_GEOMETRY) {
4509 shader_info->gs.gsvs_vertex_size = ctx.gsvs_vertex_size;
4510 shader_info->gs.max_gsvs_emit_size = ctx.max_gsvs_emit_size;
4511 } else if (shaders[i]->info.stage == MESA_SHADER_TESS_CTRL) {
4512 shader_info->tcs.num_patches = ctx.tcs_num_patches;
4513 shader_info->tcs.lds_size = calculate_tess_lds_size(&ctx);
4514 }
4515 }
4516
4517 LLVMBuildRetVoid(ctx.ac.builder);
4518
4519 if (options->dump_preoptir) {
4520 fprintf(stderr, "%s LLVM IR:\n\n",
4521 radv_get_shader_name(shader_info,
4522 shaders[shader_count - 1]->info.stage));
4523 ac_dump_module(ctx.ac.module);
4524 fprintf(stderr, "\n");
4525 }
4526
4527 ac_llvm_finalize_module(&ctx, ac_llvm->passmgr, options);
4528
4529 if (shader_count == 1)
4530 ac_nir_eliminate_const_vs_outputs(&ctx);
4531
4532 if (options->dump_shader) {
4533 ctx.shader_info->private_mem_vgprs =
4534 ac_count_scratch_private_memory(ctx.main_function);
4535 }
4536
4537 return ctx.ac.module;
4538 }
4539
4540 static void ac_diagnostic_handler(LLVMDiagnosticInfoRef di, void *context)
4541 {
4542 unsigned *retval = (unsigned *)context;
4543 LLVMDiagnosticSeverity severity = LLVMGetDiagInfoSeverity(di);
4544 char *description = LLVMGetDiagInfoDescription(di);
4545
4546 if (severity == LLVMDSError) {
4547 *retval = 1;
4548 fprintf(stderr, "LLVM triggered Diagnostic Handler: %s\n",
4549 description);
4550 }
4551
4552 LLVMDisposeMessage(description);
4553 }
4554
4555 static unsigned radv_llvm_compile(LLVMModuleRef M,
4556 char **pelf_buffer, size_t *pelf_size,
4557 struct ac_llvm_compiler *ac_llvm)
4558 {
4559 unsigned retval = 0;
4560 LLVMContextRef llvm_ctx;
4561
4562 /* Setup Diagnostic Handler*/
4563 llvm_ctx = LLVMGetModuleContext(M);
4564
4565 LLVMContextSetDiagnosticHandler(llvm_ctx, ac_diagnostic_handler,
4566 &retval);
4567
4568 /* Compile IR*/
4569 if (!radv_compile_to_elf(ac_llvm, M, pelf_buffer, pelf_size))
4570 retval = 1;
4571 return retval;
4572 }
4573
4574 static void ac_compile_llvm_module(struct ac_llvm_compiler *ac_llvm,
4575 LLVMModuleRef llvm_module,
4576 struct radv_shader_binary **rbinary,
4577 struct radv_shader_variant_info *shader_info,
4578 gl_shader_stage stage,
4579 const char *name,
4580 const struct radv_nir_compiler_options *options)
4581 {
4582 char *elf_buffer = NULL;
4583 size_t elf_size = 0;
4584 char *llvm_ir_string = NULL;
4585
4586 if (options->dump_shader) {
4587 fprintf(stderr, "%s LLVM IR:\n\n", name);
4588 ac_dump_module(llvm_module);
4589 fprintf(stderr, "\n");
4590 }
4591
4592 if (options->record_llvm_ir) {
4593 char *llvm_ir = LLVMPrintModuleToString(llvm_module);
4594 llvm_ir_string = strdup(llvm_ir);
4595 LLVMDisposeMessage(llvm_ir);
4596 }
4597
4598 int v = radv_llvm_compile(llvm_module, &elf_buffer, &elf_size, ac_llvm);
4599 if (v) {
4600 fprintf(stderr, "compile failed\n");
4601 }
4602
4603 LLVMContextRef ctx = LLVMGetModuleContext(llvm_module);
4604 LLVMDisposeModule(llvm_module);
4605 LLVMContextDispose(ctx);
4606
4607 size_t llvm_ir_size = llvm_ir_string ? strlen(llvm_ir_string) : 0;
4608 size_t alloc_size = sizeof(struct radv_shader_binary_rtld) + elf_size + llvm_ir_size + 1;
4609 struct radv_shader_binary_rtld *rbin = calloc(1, alloc_size);
4610 memcpy(rbin->data, elf_buffer, elf_size);
4611 if (llvm_ir_string)
4612 memcpy(rbin->data + elf_size, llvm_ir_string, llvm_ir_size + 1);
4613
4614 rbin->base.type = RADV_BINARY_TYPE_RTLD;
4615 rbin->base.stage = stage;
4616 rbin->base.total_size = alloc_size;
4617 rbin->elf_size = elf_size;
4618 rbin->llvm_ir_size = llvm_ir_size;
4619 *rbinary = &rbin->base;
4620
4621 free(llvm_ir_string);
4622 free(elf_buffer);
4623 }
4624
4625 static void
4626 ac_fill_shader_info(struct radv_shader_variant_info *shader_info, struct nir_shader *nir, const struct radv_nir_compiler_options *options)
4627 {
4628 switch (nir->info.stage) {
4629 case MESA_SHADER_COMPUTE:
4630 for (int i = 0; i < 3; ++i)
4631 shader_info->cs.block_size[i] = nir->info.cs.local_size[i];
4632 break;
4633 case MESA_SHADER_FRAGMENT:
4634 shader_info->fs.early_fragment_test = nir->info.fs.early_fragment_tests;
4635 break;
4636 case MESA_SHADER_GEOMETRY:
4637 shader_info->gs.vertices_in = nir->info.gs.vertices_in;
4638 shader_info->gs.vertices_out = nir->info.gs.vertices_out;
4639 shader_info->gs.output_prim = nir->info.gs.output_primitive;
4640 shader_info->gs.invocations = nir->info.gs.invocations;
4641 break;
4642 case MESA_SHADER_TESS_EVAL:
4643 shader_info->tes.primitive_mode = nir->info.tess.primitive_mode;
4644 shader_info->tes.spacing = nir->info.tess.spacing;
4645 shader_info->tes.ccw = nir->info.tess.ccw;
4646 shader_info->tes.point_mode = nir->info.tess.point_mode;
4647 shader_info->tes.as_es = options->key.vs_common_out.as_es;
4648 shader_info->tes.export_prim_id = options->key.vs_common_out.export_prim_id;
4649 shader_info->is_ngg = options->key.vs_common_out.as_ngg;
4650 break;
4651 case MESA_SHADER_TESS_CTRL:
4652 shader_info->tcs.tcs_vertices_out = nir->info.tess.tcs_vertices_out;
4653 break;
4654 case MESA_SHADER_VERTEX:
4655 shader_info->vs.as_es = options->key.vs_common_out.as_es;
4656 shader_info->vs.as_ls = options->key.vs_common_out.as_ls;
4657 shader_info->vs.export_prim_id = options->key.vs_common_out.export_prim_id;
4658 shader_info->is_ngg = options->key.vs_common_out.as_ngg;
4659 break;
4660 default:
4661 break;
4662 }
4663 }
4664
4665 void
4666 radv_compile_nir_shader(struct ac_llvm_compiler *ac_llvm,
4667 struct radv_shader_binary **rbinary,
4668 struct radv_shader_variant_info *shader_info,
4669 struct nir_shader *const *nir,
4670 int nir_count,
4671 const struct radv_nir_compiler_options *options)
4672 {
4673
4674 LLVMModuleRef llvm_module;
4675
4676 llvm_module = ac_translate_nir_to_llvm(ac_llvm, nir, nir_count, shader_info,
4677 options);
4678
4679 ac_compile_llvm_module(ac_llvm, llvm_module, rbinary, shader_info,
4680 nir[nir_count - 1]->info.stage,
4681 radv_get_shader_name(shader_info,
4682 nir[nir_count - 1]->info.stage),
4683 options);
4684
4685 for (int i = 0; i < nir_count; ++i)
4686 ac_fill_shader_info(shader_info, nir[i], options);
4687
4688 /* Determine the ES type (VS or TES) for the GS on GFX9. */
4689 if (options->chip_class >= GFX9) {
4690 if (nir_count == 2 &&
4691 nir[1]->info.stage == MESA_SHADER_GEOMETRY) {
4692 shader_info->gs.es_type = nir[0]->info.stage;
4693 }
4694 }
4695 }
4696
4697 static void
4698 ac_gs_copy_shader_emit(struct radv_shader_context *ctx)
4699 {
4700 LLVMValueRef vtx_offset =
4701 LLVMBuildMul(ctx->ac.builder, ctx->abi.vertex_id,
4702 LLVMConstInt(ctx->ac.i32, 4, false), "");
4703 LLVMValueRef stream_id;
4704
4705 /* Fetch the vertex stream ID. */
4706 if (ctx->shader_info->info.so.num_outputs) {
4707 stream_id =
4708 ac_unpack_param(&ctx->ac, ctx->streamout_config, 24, 2);
4709 } else {
4710 stream_id = ctx->ac.i32_0;
4711 }
4712
4713 LLVMBasicBlockRef end_bb;
4714 LLVMValueRef switch_inst;
4715
4716 end_bb = LLVMAppendBasicBlockInContext(ctx->ac.context,
4717 ctx->main_function, "end");
4718 switch_inst = LLVMBuildSwitch(ctx->ac.builder, stream_id, end_bb, 4);
4719
4720 for (unsigned stream = 0; stream < 4; stream++) {
4721 unsigned num_components =
4722 ctx->shader_info->info.gs.num_stream_output_components[stream];
4723 LLVMBasicBlockRef bb;
4724 unsigned offset;
4725
4726 if (!num_components)
4727 continue;
4728
4729 if (stream > 0 && !ctx->shader_info->info.so.num_outputs)
4730 continue;
4731
4732 bb = LLVMInsertBasicBlockInContext(ctx->ac.context, end_bb, "out");
4733 LLVMAddCase(switch_inst, LLVMConstInt(ctx->ac.i32, stream, 0), bb);
4734 LLVMPositionBuilderAtEnd(ctx->ac.builder, bb);
4735
4736 offset = 0;
4737 for (unsigned i = 0; i < AC_LLVM_MAX_OUTPUTS; ++i) {
4738 unsigned output_usage_mask =
4739 ctx->shader_info->info.gs.output_usage_mask[i];
4740 unsigned output_stream =
4741 ctx->shader_info->info.gs.output_streams[i];
4742 int length = util_last_bit(output_usage_mask);
4743
4744 if (!(ctx->output_mask & (1ull << i)) ||
4745 output_stream != stream)
4746 continue;
4747
4748 for (unsigned j = 0; j < length; j++) {
4749 LLVMValueRef value, soffset;
4750
4751 if (!(output_usage_mask & (1 << j)))
4752 continue;
4753
4754 soffset = LLVMConstInt(ctx->ac.i32,
4755 offset *
4756 ctx->gs_max_out_vertices * 16 * 4, false);
4757
4758 offset++;
4759
4760 value = ac_build_buffer_load(&ctx->ac,
4761 ctx->gsvs_ring[0],
4762 1, ctx->ac.i32_0,
4763 vtx_offset, soffset,
4764 0, ac_glc | ac_slc, true, false);
4765
4766 LLVMTypeRef type = LLVMGetAllocatedType(ctx->abi.outputs[ac_llvm_reg_index_soa(i, j)]);
4767 if (ac_get_type_size(type) == 2) {
4768 value = LLVMBuildBitCast(ctx->ac.builder, value, ctx->ac.i32, "");
4769 value = LLVMBuildTrunc(ctx->ac.builder, value, ctx->ac.i16, "");
4770 }
4771
4772 LLVMBuildStore(ctx->ac.builder,
4773 ac_to_float(&ctx->ac, value), ctx->abi.outputs[ac_llvm_reg_index_soa(i, j)]);
4774 }
4775 }
4776
4777 if (ctx->shader_info->info.so.num_outputs)
4778 radv_emit_streamout(ctx, stream);
4779
4780 if (stream == 0) {
4781 handle_vs_outputs_post(ctx, false, true,
4782 &ctx->shader_info->vs.outinfo);
4783 }
4784
4785 LLVMBuildBr(ctx->ac.builder, end_bb);
4786 }
4787
4788 LLVMPositionBuilderAtEnd(ctx->ac.builder, end_bb);
4789 }
4790
4791 void
4792 radv_compile_gs_copy_shader(struct ac_llvm_compiler *ac_llvm,
4793 struct nir_shader *geom_shader,
4794 struct radv_shader_binary **rbinary,
4795 struct radv_shader_variant_info *shader_info,
4796 const struct radv_nir_compiler_options *options)
4797 {
4798 struct radv_shader_context ctx = {0};
4799 ctx.options = options;
4800 ctx.shader_info = shader_info;
4801
4802 ac_llvm_context_init(&ctx.ac, options->chip_class, options->family);
4803 ctx.context = ctx.ac.context;
4804 ctx.ac.module = ac_create_module(ac_llvm->tm, ctx.context);
4805
4806 ctx.is_gs_copy_shader = true;
4807
4808 enum ac_float_mode float_mode =
4809 options->unsafe_math ? AC_FLOAT_MODE_UNSAFE_FP_MATH :
4810 AC_FLOAT_MODE_DEFAULT;
4811
4812 ctx.ac.builder = ac_create_builder(ctx.context, float_mode);
4813 ctx.stage = MESA_SHADER_VERTEX;
4814
4815 radv_nir_shader_info_pass(geom_shader, options, &shader_info->info);
4816
4817 create_function(&ctx, MESA_SHADER_VERTEX, false, MESA_SHADER_VERTEX);
4818
4819 ctx.gs_max_out_vertices = geom_shader->info.gs.vertices_out;
4820 ac_setup_rings(&ctx);
4821
4822 nir_foreach_variable(variable, &geom_shader->outputs) {
4823 scan_shader_output_decl(&ctx, variable, geom_shader, MESA_SHADER_VERTEX);
4824 ac_handle_shader_output_decl(&ctx.ac, &ctx.abi, geom_shader,
4825 variable, MESA_SHADER_VERTEX);
4826 }
4827
4828 ac_gs_copy_shader_emit(&ctx);
4829
4830 LLVMBuildRetVoid(ctx.ac.builder);
4831
4832 ac_llvm_finalize_module(&ctx, ac_llvm->passmgr, options);
4833
4834 ac_compile_llvm_module(ac_llvm, ctx.ac.module, rbinary, shader_info,
4835 MESA_SHADER_VERTEX, "GS Copy Shader", options);
4836 (*rbinary)->is_gs_copy_shader = true;
4837
4838 }