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