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