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