radeonsi: kill point size VS output if it's not used by the rasterizer
[mesa.git] / src / gallium / drivers / radeonsi / gfx10_shader_ngg.c
1 /*
2 * Copyright 2017 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "ac_llvm_cull.h"
25 #include "si_pipe.h"
26 #include "si_shader_internal.h"
27 #include "sid.h"
28 #include "util/u_memory.h"
29 #include "util/u_prim.h"
30
31 static LLVMValueRef get_wave_id_in_tg(struct si_shader_context *ctx)
32 {
33 return si_unpack_param(ctx, ctx->merged_wave_info, 24, 4);
34 }
35
36 static LLVMValueRef get_tgsize(struct si_shader_context *ctx)
37 {
38 return si_unpack_param(ctx, ctx->merged_wave_info, 28, 4);
39 }
40
41 static LLVMValueRef get_thread_id_in_tg(struct si_shader_context *ctx)
42 {
43 LLVMBuilderRef builder = ctx->ac.builder;
44 LLVMValueRef tmp;
45 tmp = LLVMBuildMul(builder, get_wave_id_in_tg(ctx),
46 LLVMConstInt(ctx->ac.i32, ctx->ac.wave_size, false), "");
47 return LLVMBuildAdd(builder, tmp, ac_get_thread_id(&ctx->ac), "");
48 }
49
50 static LLVMValueRef ngg_get_vtx_cnt(struct si_shader_context *ctx)
51 {
52 return si_unpack_param(ctx, ctx->gs_tg_info, 12, 9);
53 }
54
55 static LLVMValueRef ngg_get_prim_cnt(struct si_shader_context *ctx)
56 {
57 return si_unpack_param(ctx, ctx->gs_tg_info, 22, 9);
58 }
59
60 static LLVMValueRef ngg_get_ordered_id(struct si_shader_context *ctx)
61 {
62 return si_unpack_param(ctx, ctx->gs_tg_info, 0, 12);
63 }
64
65 static LLVMValueRef ngg_get_query_buf(struct si_shader_context *ctx)
66 {
67 LLVMValueRef buf_ptr = ac_get_arg(&ctx->ac, ctx->rw_buffers);
68
69 return ac_build_load_to_sgpr(&ctx->ac, buf_ptr,
70 LLVMConstInt(ctx->ac.i32, GFX10_GS_QUERY_BUF, false));
71 }
72
73 static LLVMValueRef ngg_get_initial_edgeflag(struct si_shader_context *ctx, unsigned index)
74 {
75 if (ctx->stage == MESA_SHADER_VERTEX) {
76 LLVMValueRef tmp;
77 tmp = LLVMBuildLShr(ctx->ac.builder, ac_get_arg(&ctx->ac, ctx->args.gs_invocation_id),
78 LLVMConstInt(ctx->ac.i32, 8 + index, false), "");
79 return LLVMBuildTrunc(ctx->ac.builder, tmp, ctx->ac.i1, "");
80 }
81 return ctx->ac.i1false;
82 }
83
84 /**
85 * Return the number of vertices as a constant in \p num_vertices,
86 * and return a more precise value as LLVMValueRef from the function.
87 */
88 static LLVMValueRef ngg_get_vertices_per_prim(struct si_shader_context *ctx, unsigned *num_vertices)
89 {
90 const struct si_shader_info *info = &ctx->shader->selector->info;
91
92 if (ctx->stage == MESA_SHADER_VERTEX) {
93 if (info->base.vs.blit_sgprs_amd) {
94 /* Blits always use axis-aligned rectangles with 3 vertices. */
95 *num_vertices = 3;
96 return LLVMConstInt(ctx->ac.i32, 3, 0);
97 } else {
98 /* We always build up all three indices for the prim export
99 * independent of the primitive type. The additional garbage
100 * data shouldn't hurt. This number doesn't matter with
101 * NGG passthrough.
102 */
103 *num_vertices = 3;
104
105 /* Extract OUTPRIM field. */
106 LLVMValueRef num = si_unpack_param(ctx, ctx->vs_state_bits, 2, 2);
107 return LLVMBuildAdd(ctx->ac.builder, num, ctx->ac.i32_1, "");
108 }
109 } else {
110 assert(ctx->stage == MESA_SHADER_TESS_EVAL);
111
112 if (info->base.tess.point_mode)
113 *num_vertices = 1;
114 else if (info->base.tess.primitive_mode == GL_LINES)
115 *num_vertices = 2;
116 else
117 *num_vertices = 3;
118
119 return LLVMConstInt(ctx->ac.i32, *num_vertices, false);
120 }
121 }
122
123 bool gfx10_ngg_export_prim_early(struct si_shader *shader)
124 {
125 struct si_shader_selector *sel = shader->selector;
126
127 assert(shader->key.as_ngg && !shader->key.as_es);
128
129 return sel->info.stage != MESA_SHADER_GEOMETRY && !sel->info.writes_edgeflag;
130 }
131
132 void gfx10_ngg_build_sendmsg_gs_alloc_req(struct si_shader_context *ctx)
133 {
134 ac_build_sendmsg_gs_alloc_req(&ctx->ac, get_wave_id_in_tg(ctx), ngg_get_vtx_cnt(ctx),
135 ngg_get_prim_cnt(ctx));
136 }
137
138 void gfx10_ngg_build_export_prim(struct si_shader_context *ctx, LLVMValueRef user_edgeflags[3],
139 LLVMValueRef prim_passthrough)
140 {
141 LLVMBuilderRef builder = ctx->ac.builder;
142
143 if (gfx10_is_ngg_passthrough(ctx->shader) || ctx->shader->key.opt.ngg_culling) {
144 ac_build_ifcc(&ctx->ac, si_is_gs_thread(ctx), 6001);
145 {
146 struct ac_ngg_prim prim = {};
147
148 if (prim_passthrough)
149 prim.passthrough = prim_passthrough;
150 else
151 prim.passthrough = ac_get_arg(&ctx->ac, ctx->gs_vtx01_offset);
152
153 /* This is only used with NGG culling, which returns the NGG
154 * passthrough prim export encoding.
155 */
156 if (ctx->shader->selector->info.writes_edgeflag) {
157 unsigned all_bits_no_edgeflags = ~SI_NGG_PRIM_EDGE_FLAG_BITS;
158 LLVMValueRef edgeflags = LLVMConstInt(ctx->ac.i32, all_bits_no_edgeflags, 0);
159
160 unsigned num_vertices;
161 ngg_get_vertices_per_prim(ctx, &num_vertices);
162
163 for (unsigned i = 0; i < num_vertices; i++) {
164 unsigned shift = 9 + i * 10;
165 LLVMValueRef edge;
166
167 edge = LLVMBuildLoad(builder, user_edgeflags[i], "");
168 edge = LLVMBuildZExt(builder, edge, ctx->ac.i32, "");
169 edge = LLVMBuildShl(builder, edge, LLVMConstInt(ctx->ac.i32, shift, 0), "");
170 edgeflags = LLVMBuildOr(builder, edgeflags, edge, "");
171 }
172 prim.passthrough = LLVMBuildAnd(builder, prim.passthrough, edgeflags, "");
173 }
174
175 ac_build_export_prim(&ctx->ac, &prim);
176 }
177 ac_build_endif(&ctx->ac, 6001);
178 return;
179 }
180
181 ac_build_ifcc(&ctx->ac, si_is_gs_thread(ctx), 6001);
182 {
183 struct ac_ngg_prim prim = {};
184
185 ngg_get_vertices_per_prim(ctx, &prim.num_vertices);
186
187 prim.isnull = ctx->ac.i1false;
188 prim.index[0] = si_unpack_param(ctx, ctx->gs_vtx01_offset, 0, 16);
189 prim.index[1] = si_unpack_param(ctx, ctx->gs_vtx01_offset, 16, 16);
190 prim.index[2] = si_unpack_param(ctx, ctx->gs_vtx23_offset, 0, 16);
191
192 for (unsigned i = 0; i < prim.num_vertices; ++i) {
193 prim.edgeflag[i] = ngg_get_initial_edgeflag(ctx, i);
194
195 if (ctx->shader->selector->info.writes_edgeflag) {
196 LLVMValueRef edge;
197
198 edge = LLVMBuildLoad(ctx->ac.builder, user_edgeflags[i], "");
199 edge = LLVMBuildAnd(ctx->ac.builder, prim.edgeflag[i], edge, "");
200 prim.edgeflag[i] = edge;
201 }
202 }
203
204 ac_build_export_prim(&ctx->ac, &prim);
205 }
206 ac_build_endif(&ctx->ac, 6001);
207 }
208
209 static void build_streamout_vertex(struct si_shader_context *ctx, LLVMValueRef *so_buffer,
210 LLVMValueRef *wg_offset_dw, unsigned stream,
211 LLVMValueRef offset_vtx, LLVMValueRef vertexptr)
212 {
213 struct si_shader_info *info = &ctx->shader->selector->info;
214 struct pipe_stream_output_info *so = &ctx->shader->selector->so;
215 LLVMBuilderRef builder = ctx->ac.builder;
216 LLVMValueRef offset[4] = {};
217 LLVMValueRef tmp;
218
219 for (unsigned buffer = 0; buffer < 4; ++buffer) {
220 if (!wg_offset_dw[buffer])
221 continue;
222
223 tmp = LLVMBuildMul(builder, offset_vtx, LLVMConstInt(ctx->ac.i32, so->stride[buffer], false),
224 "");
225 tmp = LLVMBuildAdd(builder, wg_offset_dw[buffer], tmp, "");
226 offset[buffer] = LLVMBuildShl(builder, tmp, LLVMConstInt(ctx->ac.i32, 2, false), "");
227 }
228
229 for (unsigned i = 0; i < so->num_outputs; ++i) {
230 if (so->output[i].stream != stream)
231 continue;
232
233 unsigned reg = so->output[i].register_index;
234 struct si_shader_output_values out;
235 out.semantic = info->output_semantic[reg];
236
237 for (unsigned comp = 0; comp < 4; comp++) {
238 tmp = ac_build_gep0(&ctx->ac, vertexptr, LLVMConstInt(ctx->ac.i32, 4 * reg + comp, false));
239 out.values[comp] = LLVMBuildLoad(builder, tmp, "");
240 out.vertex_stream[comp] = (info->output_streams[reg] >> (2 * comp)) & 3;
241 }
242
243 si_llvm_streamout_store_output(ctx, so_buffer, offset, &so->output[i], &out);
244 }
245 }
246
247 struct ngg_streamout {
248 LLVMValueRef num_vertices;
249
250 /* per-thread data */
251 LLVMValueRef prim_enable[4]; /* i1 per stream */
252 LLVMValueRef vertices[3]; /* [N x i32] addrspace(LDS)* */
253
254 /* Output */
255 LLVMValueRef emit[4]; /* per-stream emitted primitives (only valid for used streams) */
256 };
257
258 /**
259 * Build streamout logic.
260 *
261 * Implies a barrier.
262 *
263 * Writes number of emitted primitives to gs_ngg_scratch[4:8].
264 *
265 * Clobbers gs_ngg_scratch[8:].
266 */
267 static void build_streamout(struct si_shader_context *ctx, struct ngg_streamout *nggso)
268 {
269 struct si_shader_info *info = &ctx->shader->selector->info;
270 struct pipe_stream_output_info *so = &ctx->shader->selector->so;
271 LLVMBuilderRef builder = ctx->ac.builder;
272 LLVMValueRef buf_ptr = ac_get_arg(&ctx->ac, ctx->rw_buffers);
273 LLVMValueRef tid = get_thread_id_in_tg(ctx);
274 LLVMValueRef tmp, tmp2;
275 LLVMValueRef i32_2 = LLVMConstInt(ctx->ac.i32, 2, false);
276 LLVMValueRef i32_4 = LLVMConstInt(ctx->ac.i32, 4, false);
277 LLVMValueRef i32_8 = LLVMConstInt(ctx->ac.i32, 8, false);
278 LLVMValueRef so_buffer[4] = {};
279 unsigned max_num_vertices = 1 + (nggso->vertices[1] ? 1 : 0) + (nggso->vertices[2] ? 1 : 0);
280 LLVMValueRef prim_stride_dw[4] = {};
281 LLVMValueRef prim_stride_dw_vgpr = LLVMGetUndef(ctx->ac.i32);
282 int stream_for_buffer[4] = {-1, -1, -1, -1};
283 unsigned bufmask_for_stream[4] = {};
284 bool isgs = ctx->stage == MESA_SHADER_GEOMETRY;
285 unsigned scratch_emit_base = isgs ? 4 : 0;
286 LLVMValueRef scratch_emit_basev = isgs ? i32_4 : ctx->ac.i32_0;
287 unsigned scratch_offset_base = isgs ? 8 : 4;
288 LLVMValueRef scratch_offset_basev = isgs ? i32_8 : i32_4;
289
290 ac_llvm_add_target_dep_function_attr(ctx->main_fn, "amdgpu-gds-size", 256);
291
292 /* Determine the mapping of streamout buffers to vertex streams. */
293 for (unsigned i = 0; i < so->num_outputs; ++i) {
294 unsigned buf = so->output[i].output_buffer;
295 unsigned stream = so->output[i].stream;
296 assert(stream_for_buffer[buf] < 0 || stream_for_buffer[buf] == stream);
297 stream_for_buffer[buf] = stream;
298 bufmask_for_stream[stream] |= 1 << buf;
299 }
300
301 for (unsigned buffer = 0; buffer < 4; ++buffer) {
302 if (stream_for_buffer[buffer] == -1)
303 continue;
304
305 assert(so->stride[buffer]);
306
307 tmp = LLVMConstInt(ctx->ac.i32, so->stride[buffer], false);
308 prim_stride_dw[buffer] = LLVMBuildMul(builder, tmp, nggso->num_vertices, "");
309 prim_stride_dw_vgpr =
310 ac_build_writelane(&ctx->ac, prim_stride_dw_vgpr, prim_stride_dw[buffer],
311 LLVMConstInt(ctx->ac.i32, buffer, false));
312
313 so_buffer[buffer] = ac_build_load_to_sgpr(
314 &ctx->ac, buf_ptr, LLVMConstInt(ctx->ac.i32, SI_VS_STREAMOUT_BUF0 + buffer, false));
315 }
316
317 tmp = LLVMBuildICmp(builder, LLVMIntEQ, get_wave_id_in_tg(ctx), ctx->ac.i32_0, "");
318 ac_build_ifcc(&ctx->ac, tmp, 5200);
319 {
320 LLVMTypeRef gdsptr = LLVMPointerType(ctx->ac.i32, AC_ADDR_SPACE_GDS);
321 LLVMValueRef gdsbase = LLVMBuildIntToPtr(builder, ctx->ac.i32_0, gdsptr, "");
322
323 /* Advance the streamout offsets in GDS. */
324 LLVMValueRef offsets_vgpr = ac_build_alloca_undef(&ctx->ac, ctx->ac.i32, "");
325 LLVMValueRef generated_by_stream_vgpr = ac_build_alloca_undef(&ctx->ac, ctx->ac.i32, "");
326
327 tmp = LLVMBuildICmp(builder, LLVMIntULT, ac_get_thread_id(&ctx->ac), i32_4, "");
328 ac_build_ifcc(&ctx->ac, tmp, 5210);
329 {
330 if (isgs) {
331 tmp = ac_build_gep0(&ctx->ac, ctx->gs_ngg_scratch, tid);
332 tmp = LLVMBuildLoad(builder, tmp, "");
333 } else {
334 tmp = ac_build_writelane(&ctx->ac, ctx->ac.i32_0, ngg_get_prim_cnt(ctx), ctx->ac.i32_0);
335 }
336 LLVMBuildStore(builder, tmp, generated_by_stream_vgpr);
337
338 unsigned swizzle[4];
339 int unused_stream = -1;
340 for (unsigned stream = 0; stream < 4; ++stream) {
341 if (!info->num_stream_output_components[stream]) {
342 unused_stream = stream;
343 break;
344 }
345 }
346 for (unsigned buffer = 0; buffer < 4; ++buffer) {
347 if (stream_for_buffer[buffer] >= 0) {
348 swizzle[buffer] = stream_for_buffer[buffer];
349 } else {
350 assert(unused_stream >= 0);
351 swizzle[buffer] = unused_stream;
352 }
353 }
354
355 tmp = ac_build_quad_swizzle(&ctx->ac, tmp, swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
356 tmp = LLVMBuildMul(builder, tmp, prim_stride_dw_vgpr, "");
357
358 LLVMValueRef args[] = {
359 LLVMBuildIntToPtr(builder, ngg_get_ordered_id(ctx), gdsptr, ""),
360 tmp,
361 ctx->ac.i32_0, // ordering
362 ctx->ac.i32_0, // scope
363 ctx->ac.i1false, // isVolatile
364 LLVMConstInt(ctx->ac.i32, 4 << 24, false), // OA index
365 ctx->ac.i1true, // wave release
366 ctx->ac.i1true, // wave done
367 };
368 tmp = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.ds.ordered.add", ctx->ac.i32, args,
369 ARRAY_SIZE(args), 0);
370
371 /* Keep offsets in a VGPR for quick retrieval via readlane by
372 * the first wave for bounds checking, and also store in LDS
373 * for retrieval by all waves later. */
374 LLVMBuildStore(builder, tmp, offsets_vgpr);
375
376 tmp2 = LLVMBuildAdd(builder, ac_get_thread_id(&ctx->ac), scratch_offset_basev, "");
377 tmp2 = ac_build_gep0(&ctx->ac, ctx->gs_ngg_scratch, tmp2);
378 LLVMBuildStore(builder, tmp, tmp2);
379 }
380 ac_build_endif(&ctx->ac, 5210);
381
382 /* Determine the max emit per buffer. This is done via the SALU, in part
383 * because LLVM can't generate divide-by-multiply if we try to do this
384 * via VALU with one lane per buffer.
385 */
386 LLVMValueRef max_emit[4] = {};
387 for (unsigned buffer = 0; buffer < 4; ++buffer) {
388 if (stream_for_buffer[buffer] == -1)
389 continue;
390
391 LLVMValueRef bufsize_dw = LLVMBuildLShr(
392 builder, LLVMBuildExtractElement(builder, so_buffer[buffer], i32_2, ""), i32_2, "");
393
394 tmp = LLVMBuildLoad(builder, offsets_vgpr, "");
395 LLVMValueRef offset_dw =
396 ac_build_readlane(&ctx->ac, tmp, LLVMConstInt(ctx->ac.i32, buffer, false));
397
398 tmp = LLVMBuildSub(builder, bufsize_dw, offset_dw, "");
399 tmp = LLVMBuildUDiv(builder, tmp, prim_stride_dw[buffer], "");
400
401 tmp2 = LLVMBuildICmp(builder, LLVMIntULT, bufsize_dw, offset_dw, "");
402 max_emit[buffer] = LLVMBuildSelect(builder, tmp2, ctx->ac.i32_0, tmp, "");
403 }
404
405 /* Determine the number of emitted primitives per stream and fixup the
406 * GDS counter if necessary.
407 *
408 * This is complicated by the fact that a single stream can emit to
409 * multiple buffers (but luckily not vice versa).
410 */
411 LLVMValueRef emit_vgpr = ctx->ac.i32_0;
412
413 for (unsigned stream = 0; stream < 4; ++stream) {
414 if (!info->num_stream_output_components[stream])
415 continue;
416
417 tmp = LLVMBuildLoad(builder, generated_by_stream_vgpr, "");
418 LLVMValueRef generated =
419 ac_build_readlane(&ctx->ac, tmp, LLVMConstInt(ctx->ac.i32, stream, false));
420
421 LLVMValueRef emit = generated;
422 for (unsigned buffer = 0; buffer < 4; ++buffer) {
423 if (stream_for_buffer[buffer] == stream)
424 emit = ac_build_umin(&ctx->ac, emit, max_emit[buffer]);
425 }
426
427 emit_vgpr =
428 ac_build_writelane(&ctx->ac, emit_vgpr, emit, LLVMConstInt(ctx->ac.i32, stream, false));
429
430 /* Fixup the offset using a plain GDS atomic if we overflowed. */
431 tmp = LLVMBuildICmp(builder, LLVMIntULT, emit, generated, "");
432 ac_build_ifcc(&ctx->ac, tmp, 5221); /* scalar branch */
433 tmp = LLVMBuildLShr(builder, LLVMConstInt(ctx->ac.i32, bufmask_for_stream[stream], false),
434 ac_get_thread_id(&ctx->ac), "");
435 tmp = LLVMBuildTrunc(builder, tmp, ctx->ac.i1, "");
436 ac_build_ifcc(&ctx->ac, tmp, 5222);
437 {
438 tmp = LLVMBuildSub(builder, generated, emit, "");
439 tmp = LLVMBuildMul(builder, tmp, prim_stride_dw_vgpr, "");
440 tmp2 = LLVMBuildGEP(builder, gdsbase, &tid, 1, "");
441 LLVMBuildAtomicRMW(builder, LLVMAtomicRMWBinOpSub, tmp2, tmp,
442 LLVMAtomicOrderingMonotonic, false);
443 }
444 ac_build_endif(&ctx->ac, 5222);
445 ac_build_endif(&ctx->ac, 5221);
446 }
447
448 tmp = LLVMBuildICmp(builder, LLVMIntULT, ac_get_thread_id(&ctx->ac), i32_4, "");
449 ac_build_ifcc(&ctx->ac, tmp, 5225);
450 {
451 tmp = LLVMBuildAdd(builder, ac_get_thread_id(&ctx->ac), scratch_emit_basev, "");
452 tmp = ac_build_gep0(&ctx->ac, ctx->gs_ngg_scratch, tmp);
453 LLVMBuildStore(builder, emit_vgpr, tmp);
454 }
455 ac_build_endif(&ctx->ac, 5225);
456 }
457 ac_build_endif(&ctx->ac, 5200);
458
459 /* Determine the workgroup-relative per-thread / primitive offset into
460 * the streamout buffers */
461 struct ac_wg_scan primemit_scan[4] = {};
462
463 if (isgs) {
464 for (unsigned stream = 0; stream < 4; ++stream) {
465 if (!info->num_stream_output_components[stream])
466 continue;
467
468 primemit_scan[stream].enable_exclusive = true;
469 primemit_scan[stream].op = nir_op_iadd;
470 primemit_scan[stream].src = nggso->prim_enable[stream];
471 primemit_scan[stream].scratch = ac_build_gep0(
472 &ctx->ac, ctx->gs_ngg_scratch, LLVMConstInt(ctx->ac.i32, 12 + 8 * stream, false));
473 primemit_scan[stream].waveidx = get_wave_id_in_tg(ctx);
474 primemit_scan[stream].numwaves = get_tgsize(ctx);
475 primemit_scan[stream].maxwaves = 8;
476 ac_build_wg_scan_top(&ctx->ac, &primemit_scan[stream]);
477 }
478 }
479
480 ac_build_s_barrier(&ctx->ac);
481
482 /* Fetch the per-buffer offsets and per-stream emit counts in all waves. */
483 LLVMValueRef wgoffset_dw[4] = {};
484
485 {
486 LLVMValueRef scratch_vgpr;
487
488 tmp = ac_build_gep0(&ctx->ac, ctx->gs_ngg_scratch, ac_get_thread_id(&ctx->ac));
489 scratch_vgpr = LLVMBuildLoad(builder, tmp, "");
490
491 for (unsigned buffer = 0; buffer < 4; ++buffer) {
492 if (stream_for_buffer[buffer] >= 0) {
493 wgoffset_dw[buffer] =
494 ac_build_readlane(&ctx->ac, scratch_vgpr,
495 LLVMConstInt(ctx->ac.i32, scratch_offset_base + buffer, false));
496 }
497 }
498
499 for (unsigned stream = 0; stream < 4; ++stream) {
500 if (info->num_stream_output_components[stream]) {
501 nggso->emit[stream] =
502 ac_build_readlane(&ctx->ac, scratch_vgpr,
503 LLVMConstInt(ctx->ac.i32, scratch_emit_base + stream, false));
504 }
505 }
506 }
507
508 /* Write out primitive data */
509 for (unsigned stream = 0; stream < 4; ++stream) {
510 if (!info->num_stream_output_components[stream])
511 continue;
512
513 if (isgs) {
514 ac_build_wg_scan_bottom(&ctx->ac, &primemit_scan[stream]);
515 } else {
516 primemit_scan[stream].result_exclusive = tid;
517 }
518
519 tmp = LLVMBuildICmp(builder, LLVMIntULT, primemit_scan[stream].result_exclusive,
520 nggso->emit[stream], "");
521 tmp = LLVMBuildAnd(builder, tmp, nggso->prim_enable[stream], "");
522 ac_build_ifcc(&ctx->ac, tmp, 5240);
523 {
524 LLVMValueRef offset_vtx =
525 LLVMBuildMul(builder, primemit_scan[stream].result_exclusive, nggso->num_vertices, "");
526
527 for (unsigned i = 0; i < max_num_vertices; ++i) {
528 tmp = LLVMBuildICmp(builder, LLVMIntULT, LLVMConstInt(ctx->ac.i32, i, false),
529 nggso->num_vertices, "");
530 ac_build_ifcc(&ctx->ac, tmp, 5241);
531 build_streamout_vertex(ctx, so_buffer, wgoffset_dw, stream, offset_vtx,
532 nggso->vertices[i]);
533 ac_build_endif(&ctx->ac, 5241);
534 offset_vtx = LLVMBuildAdd(builder, offset_vtx, ctx->ac.i32_1, "");
535 }
536 }
537 ac_build_endif(&ctx->ac, 5240);
538 }
539 }
540
541 /* LDS layout of ES vertex data for NGG culling. */
542 enum
543 {
544 /* Byte 0: Boolean ES thread accepted (unculled) flag, and later the old
545 * ES thread ID. After vertex compaction, compacted ES threads
546 * store the old thread ID here to copy input VGPRs from uncompacted
547 * ES threads.
548 * Byte 1: New ES thread ID, loaded by GS to prepare the prim export value.
549 * Byte 2: TES rel patch ID
550 * Byte 3: Unused
551 */
552 lds_byte0_accept_flag = 0,
553 lds_byte0_old_thread_id = 0,
554 lds_byte1_new_thread_id,
555 lds_byte2_tes_rel_patch_id,
556 lds_byte3_unused,
557
558 lds_packed_data = 0, /* lds_byteN_... */
559
560 lds_pos_x,
561 lds_pos_y,
562 lds_pos_z,
563 lds_pos_w,
564 lds_pos_x_div_w,
565 lds_pos_y_div_w,
566 /* If VS: */
567 lds_vertex_id,
568 lds_instance_id, /* optional */
569 /* If TES: */
570 lds_tes_u = lds_vertex_id,
571 lds_tes_v = lds_instance_id,
572 lds_tes_patch_id, /* optional */
573 };
574
575 static LLVMValueRef si_build_gep_i8(struct si_shader_context *ctx, LLVMValueRef ptr,
576 unsigned byte_index)
577 {
578 assert(byte_index < 4);
579 LLVMTypeRef pi8 = LLVMPointerType(ctx->ac.i8, AC_ADDR_SPACE_LDS);
580 LLVMValueRef index = LLVMConstInt(ctx->ac.i32, byte_index, 0);
581
582 return LLVMBuildGEP(ctx->ac.builder, LLVMBuildPointerCast(ctx->ac.builder, ptr, pi8, ""), &index,
583 1, "");
584 }
585
586 static unsigned ngg_nogs_vertex_size(struct si_shader *shader)
587 {
588 unsigned lds_vertex_size = 0;
589
590 /* The edgeflag is always stored in the last element that's also
591 * used for padding to reduce LDS bank conflicts. */
592 if (shader->selector->so.num_outputs)
593 lds_vertex_size = 4 * shader->selector->info.num_outputs + 1;
594 if (shader->selector->info.writes_edgeflag)
595 lds_vertex_size = MAX2(lds_vertex_size, 1);
596
597 /* LDS size for passing data from GS to ES.
598 * GS stores Primitive IDs into LDS at the address corresponding
599 * to the ES thread of the provoking vertex. All ES threads
600 * load and export PrimitiveID for their thread.
601 */
602 if (shader->selector->info.stage == MESA_SHADER_VERTEX && shader->key.mono.u.vs_export_prim_id)
603 lds_vertex_size = MAX2(lds_vertex_size, 1);
604
605 if (shader->key.opt.ngg_culling) {
606 if (shader->selector->info.stage == MESA_SHADER_VERTEX) {
607 STATIC_ASSERT(lds_instance_id + 1 == 9);
608 lds_vertex_size = MAX2(lds_vertex_size, 9);
609 } else {
610 assert(shader->selector->info.stage == MESA_SHADER_TESS_EVAL);
611
612 if (shader->selector->info.uses_primid || shader->key.mono.u.vs_export_prim_id) {
613 STATIC_ASSERT(lds_tes_patch_id + 2 == 11);
614 lds_vertex_size = MAX2(lds_vertex_size, 11);
615 } else {
616 STATIC_ASSERT(lds_tes_v + 1 == 9);
617 lds_vertex_size = MAX2(lds_vertex_size, 9);
618 }
619 }
620 }
621
622 return lds_vertex_size;
623 }
624
625 /**
626 * Returns an `[N x i32] addrspace(LDS)*` pointing at contiguous LDS storage
627 * for the vertex outputs.
628 */
629 static LLVMValueRef ngg_nogs_vertex_ptr(struct si_shader_context *ctx, LLVMValueRef vtxid)
630 {
631 /* The extra dword is used to avoid LDS bank conflicts. */
632 unsigned vertex_size = ngg_nogs_vertex_size(ctx->shader);
633 LLVMTypeRef ai32 = LLVMArrayType(ctx->ac.i32, vertex_size);
634 LLVMTypeRef pai32 = LLVMPointerType(ai32, AC_ADDR_SPACE_LDS);
635 LLVMValueRef tmp = LLVMBuildBitCast(ctx->ac.builder, ctx->esgs_ring, pai32, "");
636 return LLVMBuildGEP(ctx->ac.builder, tmp, &vtxid, 1, "");
637 }
638
639 static LLVMValueRef si_insert_input_v4i32(struct si_shader_context *ctx, LLVMValueRef ret,
640 struct ac_arg param, unsigned return_index)
641 {
642 LLVMValueRef v = ac_get_arg(&ctx->ac, param);
643
644 for (unsigned i = 0; i < 4; i++) {
645 ret = LLVMBuildInsertValue(ctx->ac.builder, ret, ac_llvm_extract_elem(&ctx->ac, v, i),
646 return_index + i, "");
647 }
648 return ret;
649 }
650
651 static void load_bitmasks_2x64(struct si_shader_context *ctx, LLVMValueRef lds_ptr,
652 unsigned dw_offset, LLVMValueRef mask[2],
653 LLVMValueRef *total_bitcount)
654 {
655 LLVMBuilderRef builder = ctx->ac.builder;
656 LLVMValueRef ptr64 = LLVMBuildPointerCast(
657 builder, lds_ptr, LLVMPointerType(LLVMArrayType(ctx->ac.i64, 2), AC_ADDR_SPACE_LDS), "");
658 for (unsigned i = 0; i < 2; i++) {
659 LLVMValueRef index = LLVMConstInt(ctx->ac.i32, dw_offset / 2 + i, 0);
660 mask[i] = LLVMBuildLoad(builder, ac_build_gep0(&ctx->ac, ptr64, index), "");
661 }
662
663 /* We get better code if we don't use the 128-bit bitcount. */
664 *total_bitcount = LLVMBuildAdd(builder, ac_build_bit_count(&ctx->ac, mask[0]),
665 ac_build_bit_count(&ctx->ac, mask[1]), "");
666 }
667
668 /**
669 * Given a total thread count, update total and per-wave thread counts in input SGPRs
670 * and return the per-wave thread count.
671 *
672 * \param new_num_threads Total thread count on the input, per-wave thread count on the output.
673 * \param tg_info tg_info SGPR value
674 * \param tg_info_num_bits the bit size of thread count field in tg_info
675 * \param tg_info_shift the bit offset of the thread count field in tg_info
676 * \param wave_info merged_wave_info SGPR value
677 * \param wave_info_num_bits the bit size of thread count field in merged_wave_info
678 * \param wave_info_shift the bit offset of the thread count field in merged_wave_info
679 */
680 static void update_thread_counts(struct si_shader_context *ctx, LLVMValueRef *new_num_threads,
681 LLVMValueRef *tg_info, unsigned tg_info_num_bits,
682 unsigned tg_info_shift, LLVMValueRef *wave_info,
683 unsigned wave_info_num_bits, unsigned wave_info_shift)
684 {
685 LLVMBuilderRef builder = ctx->ac.builder;
686
687 /* Update the total thread count. */
688 unsigned tg_info_mask = ~(u_bit_consecutive(0, tg_info_num_bits) << tg_info_shift);
689 *tg_info = LLVMBuildAnd(builder, *tg_info, LLVMConstInt(ctx->ac.i32, tg_info_mask, 0), "");
690 *tg_info = LLVMBuildOr(
691 builder, *tg_info,
692 LLVMBuildShl(builder, *new_num_threads, LLVMConstInt(ctx->ac.i32, tg_info_shift, 0), ""), "");
693
694 /* Update the per-wave thread count. */
695 LLVMValueRef prev_threads = LLVMBuildMul(builder, get_wave_id_in_tg(ctx),
696 LLVMConstInt(ctx->ac.i32, ctx->ac.wave_size, 0), "");
697 *new_num_threads = LLVMBuildSub(builder, *new_num_threads, prev_threads, "");
698 *new_num_threads = ac_build_imax(&ctx->ac, *new_num_threads, ctx->ac.i32_0);
699 *new_num_threads =
700 ac_build_imin(&ctx->ac, *new_num_threads, LLVMConstInt(ctx->ac.i32, ctx->ac.wave_size, 0));
701 unsigned wave_info_mask = ~(u_bit_consecutive(0, wave_info_num_bits) << wave_info_shift);
702 *wave_info = LLVMBuildAnd(builder, *wave_info, LLVMConstInt(ctx->ac.i32, wave_info_mask, 0), "");
703 *wave_info = LLVMBuildOr(
704 builder, *wave_info,
705 LLVMBuildShl(builder, *new_num_threads, LLVMConstInt(ctx->ac.i32, wave_info_shift, 0), ""),
706 "");
707 }
708
709 /**
710 * Cull primitives for NGG VS or TES, then compact vertices, which happens
711 * before the VS or TES main function. Return values for the main function.
712 * Also return the position, which is passed to the shader as an input,
713 * so that we don't compute it twice.
714 */
715 void gfx10_emit_ngg_culling_epilogue(struct ac_shader_abi *abi, unsigned max_outputs,
716 LLVMValueRef *addrs)
717 {
718 struct si_shader_context *ctx = si_shader_context_from_abi(abi);
719 struct si_shader *shader = ctx->shader;
720 struct si_shader_selector *sel = shader->selector;
721 struct si_shader_info *info = &sel->info;
722 LLVMBuilderRef builder = ctx->ac.builder;
723 unsigned max_waves = ctx->ac.wave_size == 64 ? 2 : 4;
724 LLVMValueRef ngg_scratch = ctx->gs_ngg_scratch;
725
726 if (ctx->ac.wave_size == 64) {
727 ngg_scratch = LLVMBuildPointerCast(builder, ngg_scratch,
728 LLVMPointerType(LLVMArrayType(ctx->ac.i64, max_waves),
729 AC_ADDR_SPACE_LDS), "");
730 }
731
732 assert(shader->key.opt.ngg_culling);
733 assert(shader->key.as_ngg);
734 assert(sel->info.stage == MESA_SHADER_VERTEX ||
735 (sel->info.stage == MESA_SHADER_TESS_EVAL && !shader->key.as_es));
736
737 LLVMValueRef position[4] = {};
738 for (unsigned i = 0; i < info->num_outputs; i++) {
739 switch (info->output_semantic[i]) {
740 case VARYING_SLOT_POS:
741 for (unsigned j = 0; j < 4; j++) {
742 position[j] = LLVMBuildLoad(ctx->ac.builder, addrs[4 * i + j], "");
743 }
744 break;
745 }
746 }
747 assert(position[0]);
748
749 /* Store Position.XYZW into LDS. */
750 LLVMValueRef es_vtxptr = ngg_nogs_vertex_ptr(ctx, get_thread_id_in_tg(ctx));
751 for (unsigned chan = 0; chan < 4; chan++) {
752 LLVMBuildStore(
753 builder, ac_to_integer(&ctx->ac, position[chan]),
754 ac_build_gep0(&ctx->ac, es_vtxptr, LLVMConstInt(ctx->ac.i32, lds_pos_x + chan, 0)));
755 }
756 /* Store Position.XY / W into LDS. */
757 for (unsigned chan = 0; chan < 2; chan++) {
758 LLVMValueRef val = ac_build_fdiv(&ctx->ac, position[chan], position[3]);
759 LLVMBuildStore(
760 builder, ac_to_integer(&ctx->ac, val),
761 ac_build_gep0(&ctx->ac, es_vtxptr, LLVMConstInt(ctx->ac.i32, lds_pos_x_div_w + chan, 0)));
762 }
763
764 /* Store VertexID and InstanceID. ES threads will have to load them
765 * from LDS after vertex compaction and use them instead of their own
766 * system values.
767 */
768 bool uses_instance_id = false;
769 bool uses_tes_prim_id = false;
770 LLVMValueRef packed_data = ctx->ac.i32_0;
771
772 if (ctx->stage == MESA_SHADER_VERTEX) {
773 uses_instance_id = sel->info.uses_instanceid ||
774 shader->key.part.vs.prolog.instance_divisor_is_one ||
775 shader->key.part.vs.prolog.instance_divisor_is_fetched;
776
777 LLVMBuildStore(
778 builder, ctx->abi.vertex_id,
779 ac_build_gep0(&ctx->ac, es_vtxptr, LLVMConstInt(ctx->ac.i32, lds_vertex_id, 0)));
780 if (uses_instance_id) {
781 LLVMBuildStore(
782 builder, ctx->abi.instance_id,
783 ac_build_gep0(&ctx->ac, es_vtxptr, LLVMConstInt(ctx->ac.i32, lds_instance_id, 0)));
784 }
785 } else {
786 uses_tes_prim_id = sel->info.uses_primid || shader->key.mono.u.vs_export_prim_id;
787
788 assert(ctx->stage == MESA_SHADER_TESS_EVAL);
789 LLVMBuildStore(builder, ac_to_integer(&ctx->ac, ac_get_arg(&ctx->ac, ctx->tes_u)),
790 ac_build_gep0(&ctx->ac, es_vtxptr, LLVMConstInt(ctx->ac.i32, lds_tes_u, 0)));
791 LLVMBuildStore(builder, ac_to_integer(&ctx->ac, ac_get_arg(&ctx->ac, ctx->tes_v)),
792 ac_build_gep0(&ctx->ac, es_vtxptr, LLVMConstInt(ctx->ac.i32, lds_tes_v, 0)));
793 packed_data = LLVMBuildShl(builder, ac_get_arg(&ctx->ac, ctx->tes_rel_patch_id),
794 LLVMConstInt(ctx->ac.i32, lds_byte2_tes_rel_patch_id * 8, 0), "");
795 if (uses_tes_prim_id) {
796 LLVMBuildStore(
797 builder, ac_get_arg(&ctx->ac, ctx->args.tes_patch_id),
798 ac_build_gep0(&ctx->ac, es_vtxptr, LLVMConstInt(ctx->ac.i32, lds_tes_patch_id, 0)));
799 }
800 }
801 /* Initialize the packed data. */
802 LLVMBuildStore(
803 builder, packed_data,
804 ac_build_gep0(&ctx->ac, es_vtxptr, LLVMConstInt(ctx->ac.i32, lds_packed_data, 0)));
805 ac_build_endif(&ctx->ac, ctx->merged_wrap_if_label);
806
807 LLVMValueRef tid = ac_get_thread_id(&ctx->ac);
808
809 /* Initialize all but the first element of ngg_scratch to 0, because we may have less
810 * than the maximum number of waves, but we always read all values. This is where
811 * the thread bitmasks of unculled threads will be stored.
812 *
813 * ngg_scratch layout: iN_wavemask esmask[0..n]
814 */
815 ac_build_ifcc(&ctx->ac,
816 LLVMBuildICmp(builder, LLVMIntULT, get_thread_id_in_tg(ctx),
817 LLVMConstInt(ctx->ac.i32, max_waves - 1, 0), ""),
818 16101);
819 {
820 LLVMValueRef index = LLVMBuildAdd(builder, tid, ctx->ac.i32_1, "");
821 LLVMBuildStore(builder, LLVMConstInt(ctx->ac.iN_wavemask, 0, 0),
822 ac_build_gep0(&ctx->ac, ngg_scratch, index));
823 }
824 ac_build_endif(&ctx->ac, 16101);
825 ac_build_s_barrier(&ctx->ac);
826
827 /* The hardware requires that there are no holes between unculled vertices,
828 * which means we have to pack ES threads, i.e. reduce the ES thread count
829 * and move ES input VGPRs to lower threads. The upside is that varyings
830 * are only fetched and computed for unculled vertices.
831 *
832 * Vertex compaction in GS threads:
833 *
834 * Part 1: Compute the surviving vertex mask in GS threads:
835 * - Compute 4 32-bit surviving vertex masks in LDS. (max 4 waves)
836 * - In GS, notify ES threads whether the vertex survived.
837 * - Barrier
838 * - ES threads will create the mask and store it in LDS.
839 * - Barrier
840 * - Each GS thread loads the vertex masks from LDS.
841 *
842 * Part 2: Compact ES threads in GS threads:
843 * - Compute the prefix sum for all 3 vertices from the masks. These are the new
844 * thread IDs for each vertex within the primitive.
845 * - Write the value of the old thread ID into the LDS address of the new thread ID.
846 * The ES thread will load the old thread ID and use it to load the position, VertexID,
847 * and InstanceID.
848 * - Update vertex indices and null flag in the GS input VGPRs.
849 * - Barrier
850 *
851 * Part 3: Update inputs GPRs
852 * - For all waves, update per-wave thread counts in input SGPRs.
853 * - In ES threads, update the ES input VGPRs (VertexID, InstanceID, TES inputs).
854 */
855
856 LLVMValueRef vtxindex[3];
857 if (shader->key.opt.ngg_culling & SI_NGG_CULL_GS_FAST_LAUNCH_ALL) {
858 /* For the GS fast launch, the VS prologs simply puts the Vertex IDs
859 * into these VGPRs.
860 */
861 vtxindex[0] = ac_get_arg(&ctx->ac, ctx->gs_vtx01_offset);
862 vtxindex[1] = ac_get_arg(&ctx->ac, ctx->gs_vtx23_offset);
863 vtxindex[2] = ac_get_arg(&ctx->ac, ctx->gs_vtx45_offset);
864 } else {
865 vtxindex[0] = si_unpack_param(ctx, ctx->gs_vtx01_offset, 0, 16);
866 vtxindex[1] = si_unpack_param(ctx, ctx->gs_vtx01_offset, 16, 16);
867 vtxindex[2] = si_unpack_param(ctx, ctx->gs_vtx23_offset, 0, 16);
868 };
869 LLVMValueRef gs_vtxptr[] = {
870 ngg_nogs_vertex_ptr(ctx, vtxindex[0]),
871 ngg_nogs_vertex_ptr(ctx, vtxindex[1]),
872 ngg_nogs_vertex_ptr(ctx, vtxindex[2]),
873 };
874 es_vtxptr = ngg_nogs_vertex_ptr(ctx, get_thread_id_in_tg(ctx));
875
876 LLVMValueRef gs_accepted = ac_build_alloca(&ctx->ac, ctx->ac.i32, "");
877
878 /* Do culling in GS threads. */
879 ac_build_ifcc(&ctx->ac, si_is_gs_thread(ctx), 16002);
880 {
881 /* Load positions. */
882 LLVMValueRef pos[3][4] = {};
883 for (unsigned vtx = 0; vtx < 3; vtx++) {
884 for (unsigned chan = 0; chan < 4; chan++) {
885 unsigned index;
886 if (chan == 0 || chan == 1)
887 index = lds_pos_x_div_w + chan;
888 else if (chan == 3)
889 index = lds_pos_w;
890 else
891 continue;
892
893 LLVMValueRef addr =
894 ac_build_gep0(&ctx->ac, gs_vtxptr[vtx], LLVMConstInt(ctx->ac.i32, index, 0));
895 pos[vtx][chan] = LLVMBuildLoad(builder, addr, "");
896 pos[vtx][chan] = ac_to_float(&ctx->ac, pos[vtx][chan]);
897 }
898 }
899
900 /* Load the viewport state for small prim culling. */
901 LLVMValueRef vp = ac_build_load_invariant(
902 &ctx->ac, ac_get_arg(&ctx->ac, ctx->small_prim_cull_info), ctx->ac.i32_0);
903 vp = LLVMBuildBitCast(builder, vp, ctx->ac.v4f32, "");
904 LLVMValueRef vp_scale[2], vp_translate[2];
905 vp_scale[0] = ac_llvm_extract_elem(&ctx->ac, vp, 0);
906 vp_scale[1] = ac_llvm_extract_elem(&ctx->ac, vp, 1);
907 vp_translate[0] = ac_llvm_extract_elem(&ctx->ac, vp, 2);
908 vp_translate[1] = ac_llvm_extract_elem(&ctx->ac, vp, 3);
909
910 /* Get the small prim filter precision. */
911 LLVMValueRef small_prim_precision = si_unpack_param(ctx, ctx->vs_state_bits, 7, 4);
912 small_prim_precision =
913 LLVMBuildOr(builder, small_prim_precision, LLVMConstInt(ctx->ac.i32, 0x70, 0), "");
914 small_prim_precision =
915 LLVMBuildShl(builder, small_prim_precision, LLVMConstInt(ctx->ac.i32, 23, 0), "");
916 small_prim_precision = LLVMBuildBitCast(builder, small_prim_precision, ctx->ac.f32, "");
917
918 /* Execute culling code. */
919 struct ac_cull_options options = {};
920 options.cull_front = shader->key.opt.ngg_culling & SI_NGG_CULL_FRONT_FACE;
921 options.cull_back = shader->key.opt.ngg_culling & SI_NGG_CULL_BACK_FACE;
922 options.cull_view_xy = shader->key.opt.ngg_culling & SI_NGG_CULL_VIEW_SMALLPRIMS;
923 options.cull_small_prims = options.cull_view_xy;
924 options.cull_zero_area = options.cull_front || options.cull_back;
925 options.cull_w = true;
926
927 /* Tell ES threads whether their vertex survived. */
928 ac_build_ifcc(&ctx->ac,
929 ac_cull_triangle(&ctx->ac, pos, ctx->ac.i1true, vp_scale, vp_translate,
930 small_prim_precision, &options),
931 16003);
932 {
933 LLVMBuildStore(builder, ctx->ac.i32_1, gs_accepted);
934 for (unsigned vtx = 0; vtx < 3; vtx++) {
935 LLVMBuildStore(builder, ctx->ac.i8_1,
936 si_build_gep_i8(ctx, gs_vtxptr[vtx], lds_byte0_accept_flag));
937 }
938 }
939 ac_build_endif(&ctx->ac, 16003);
940 }
941 ac_build_endif(&ctx->ac, 16002);
942 ac_build_s_barrier(&ctx->ac);
943
944 gs_accepted = LLVMBuildLoad(builder, gs_accepted, "");
945
946 LLVMValueRef es_accepted = ac_build_alloca(&ctx->ac, ctx->ac.i1, "");
947
948 /* Convert the per-vertex flag to a thread bitmask in ES threads and store it in LDS. */
949 ac_build_ifcc(&ctx->ac, si_is_es_thread(ctx), 16007);
950 {
951 LLVMValueRef es_accepted_flag =
952 LLVMBuildLoad(builder, si_build_gep_i8(ctx, es_vtxptr, lds_byte0_accept_flag), "");
953
954 LLVMValueRef es_accepted_bool =
955 LLVMBuildICmp(builder, LLVMIntNE, es_accepted_flag, ctx->ac.i8_0, "");
956 LLVMValueRef es_mask = ac_get_i1_sgpr_mask(&ctx->ac, es_accepted_bool);
957
958 LLVMBuildStore(builder, es_accepted_bool, es_accepted);
959
960 ac_build_ifcc(&ctx->ac, LLVMBuildICmp(builder, LLVMIntEQ, tid, ctx->ac.i32_0, ""), 16008);
961 {
962 LLVMBuildStore(builder, es_mask,
963 ac_build_gep0(&ctx->ac, ngg_scratch, get_wave_id_in_tg(ctx)));
964 }
965 ac_build_endif(&ctx->ac, 16008);
966 }
967 ac_build_endif(&ctx->ac, 16007);
968 ac_build_s_barrier(&ctx->ac);
969
970 /* Load the vertex masks and compute the new ES thread count. */
971 LLVMValueRef es_mask[2], new_num_es_threads, kill_wave;
972 load_bitmasks_2x64(ctx, ngg_scratch, 0, es_mask, &new_num_es_threads);
973 new_num_es_threads = ac_build_readlane_no_opt_barrier(&ctx->ac, new_num_es_threads, NULL);
974
975 /* ES threads compute their prefix sum, which is the new ES thread ID.
976 * Then they write the value of the old thread ID into the LDS address
977 * of the new thread ID. It will be used it to load input VGPRs from
978 * the old thread's LDS location.
979 */
980 ac_build_ifcc(&ctx->ac, LLVMBuildLoad(builder, es_accepted, ""), 16009);
981 {
982 LLVMValueRef old_id = get_thread_id_in_tg(ctx);
983 LLVMValueRef new_id = ac_prefix_bitcount_2x64(&ctx->ac, es_mask, old_id);
984
985 LLVMBuildStore(
986 builder, LLVMBuildTrunc(builder, old_id, ctx->ac.i8, ""),
987 si_build_gep_i8(ctx, ngg_nogs_vertex_ptr(ctx, new_id), lds_byte0_old_thread_id));
988 LLVMBuildStore(builder, LLVMBuildTrunc(builder, new_id, ctx->ac.i8, ""),
989 si_build_gep_i8(ctx, es_vtxptr, lds_byte1_new_thread_id));
990 }
991 ac_build_endif(&ctx->ac, 16009);
992
993 /* Kill waves that have inactive threads. */
994 kill_wave = LLVMBuildICmp(builder, LLVMIntULE,
995 ac_build_imax(&ctx->ac, new_num_es_threads, ngg_get_prim_cnt(ctx)),
996 LLVMBuildMul(builder, get_wave_id_in_tg(ctx),
997 LLVMConstInt(ctx->ac.i32, ctx->ac.wave_size, 0), ""),
998 "");
999 ac_build_ifcc(&ctx->ac, kill_wave, 19202);
1000 {
1001 /* If we are killing wave 0, send that there are no primitives
1002 * in this threadgroup.
1003 */
1004 ac_build_sendmsg_gs_alloc_req(&ctx->ac, get_wave_id_in_tg(ctx), ctx->ac.i32_0, ctx->ac.i32_0);
1005 ac_build_s_endpgm(&ctx->ac);
1006 }
1007 ac_build_endif(&ctx->ac, 19202);
1008 ac_build_s_barrier(&ctx->ac);
1009
1010 /* Send the final vertex and primitive counts. */
1011 ac_build_sendmsg_gs_alloc_req(&ctx->ac, get_wave_id_in_tg(ctx), new_num_es_threads,
1012 ngg_get_prim_cnt(ctx));
1013
1014 /* Update thread counts in SGPRs. */
1015 LLVMValueRef new_gs_tg_info = ac_get_arg(&ctx->ac, ctx->gs_tg_info);
1016 LLVMValueRef new_merged_wave_info = ac_get_arg(&ctx->ac, ctx->merged_wave_info);
1017
1018 /* This also converts the thread count from the total count to the per-wave count. */
1019 update_thread_counts(ctx, &new_num_es_threads, &new_gs_tg_info, 9, 12, &new_merged_wave_info, 8,
1020 0);
1021
1022 /* Update vertex indices in VGPR0 (same format as NGG passthrough). */
1023 LLVMValueRef new_vgpr0 = ac_build_alloca_undef(&ctx->ac, ctx->ac.i32, "");
1024
1025 /* Set the null flag at the beginning (culled), and then
1026 * overwrite it for accepted primitives.
1027 */
1028 LLVMBuildStore(builder, LLVMConstInt(ctx->ac.i32, 1u << 31, 0), new_vgpr0);
1029
1030 /* Get vertex indices after vertex compaction. */
1031 ac_build_ifcc(&ctx->ac, LLVMBuildTrunc(builder, gs_accepted, ctx->ac.i1, ""), 16011);
1032 {
1033 struct ac_ngg_prim prim = {};
1034 prim.num_vertices = 3;
1035 prim.isnull = ctx->ac.i1false;
1036
1037 for (unsigned vtx = 0; vtx < 3; vtx++) {
1038 prim.index[vtx] = LLVMBuildLoad(
1039 builder, si_build_gep_i8(ctx, gs_vtxptr[vtx], lds_byte1_new_thread_id), "");
1040 prim.index[vtx] = LLVMBuildZExt(builder, prim.index[vtx], ctx->ac.i32, "");
1041 prim.edgeflag[vtx] = ngg_get_initial_edgeflag(ctx, vtx);
1042 }
1043
1044 /* Set the new GS input VGPR. */
1045 LLVMBuildStore(builder, ac_pack_prim_export(&ctx->ac, &prim), new_vgpr0);
1046 }
1047 ac_build_endif(&ctx->ac, 16011);
1048
1049 if (gfx10_ngg_export_prim_early(shader))
1050 gfx10_ngg_build_export_prim(ctx, NULL, LLVMBuildLoad(builder, new_vgpr0, ""));
1051
1052 /* Set the new ES input VGPRs. */
1053 LLVMValueRef es_data[4];
1054 LLVMValueRef old_thread_id = ac_build_alloca_undef(&ctx->ac, ctx->ac.i32, "");
1055
1056 for (unsigned i = 0; i < 4; i++)
1057 es_data[i] = ac_build_alloca_undef(&ctx->ac, ctx->ac.i32, "");
1058
1059 ac_build_ifcc(&ctx->ac, LLVMBuildICmp(ctx->ac.builder, LLVMIntULT, tid, new_num_es_threads, ""),
1060 16012);
1061 {
1062 LLVMValueRef old_id, old_es_vtxptr, tmp;
1063
1064 /* Load ES input VGPRs from the ES thread before compaction. */
1065 old_id = LLVMBuildLoad(builder, si_build_gep_i8(ctx, es_vtxptr, lds_byte0_old_thread_id), "");
1066 old_id = LLVMBuildZExt(builder, old_id, ctx->ac.i32, "");
1067
1068 LLVMBuildStore(builder, old_id, old_thread_id);
1069 old_es_vtxptr = ngg_nogs_vertex_ptr(ctx, old_id);
1070
1071 for (unsigned i = 0; i < 2; i++) {
1072 tmp = LLVMBuildLoad(
1073 builder,
1074 ac_build_gep0(&ctx->ac, old_es_vtxptr, LLVMConstInt(ctx->ac.i32, lds_vertex_id + i, 0)),
1075 "");
1076 LLVMBuildStore(builder, tmp, es_data[i]);
1077 }
1078
1079 if (ctx->stage == MESA_SHADER_TESS_EVAL) {
1080 tmp = LLVMBuildLoad(builder,
1081 si_build_gep_i8(ctx, old_es_vtxptr, lds_byte2_tes_rel_patch_id), "");
1082 tmp = LLVMBuildZExt(builder, tmp, ctx->ac.i32, "");
1083 LLVMBuildStore(builder, tmp, es_data[2]);
1084
1085 if (uses_tes_prim_id) {
1086 tmp = LLVMBuildLoad(builder,
1087 ac_build_gep0(&ctx->ac, old_es_vtxptr,
1088 LLVMConstInt(ctx->ac.i32, lds_tes_patch_id, 0)),
1089 "");
1090 LLVMBuildStore(builder, tmp, es_data[3]);
1091 }
1092 }
1093 }
1094 ac_build_endif(&ctx->ac, 16012);
1095
1096 /* Return values for the main function. */
1097 LLVMValueRef ret = ctx->return_value;
1098 LLVMValueRef val;
1099
1100 ret = LLVMBuildInsertValue(ctx->ac.builder, ret, new_gs_tg_info, 2, "");
1101 ret = LLVMBuildInsertValue(ctx->ac.builder, ret, new_merged_wave_info, 3, "");
1102 if (ctx->stage == MESA_SHADER_TESS_EVAL)
1103 ret = si_insert_input_ret(ctx, ret, ctx->tcs_offchip_offset, 4);
1104
1105 ret = si_insert_input_ptr(ctx, ret, ctx->rw_buffers, 8 + SI_SGPR_RW_BUFFERS);
1106 ret = si_insert_input_ptr(ctx, ret, ctx->bindless_samplers_and_images,
1107 8 + SI_SGPR_BINDLESS_SAMPLERS_AND_IMAGES);
1108 ret = si_insert_input_ptr(ctx, ret, ctx->const_and_shader_buffers,
1109 8 + SI_SGPR_CONST_AND_SHADER_BUFFERS);
1110 ret = si_insert_input_ptr(ctx, ret, ctx->samplers_and_images, 8 + SI_SGPR_SAMPLERS_AND_IMAGES);
1111 ret = si_insert_input_ptr(ctx, ret, ctx->vs_state_bits, 8 + SI_SGPR_VS_STATE_BITS);
1112
1113 if (ctx->stage == MESA_SHADER_VERTEX) {
1114 ret = si_insert_input_ptr(ctx, ret, ctx->args.base_vertex, 8 + SI_SGPR_BASE_VERTEX);
1115 ret = si_insert_input_ptr(ctx, ret, ctx->args.start_instance, 8 + SI_SGPR_START_INSTANCE);
1116 ret = si_insert_input_ptr(ctx, ret, ctx->args.draw_id, 8 + SI_SGPR_DRAWID);
1117 ret = si_insert_input_ptr(ctx, ret, ctx->vertex_buffers, 8 + SI_VS_NUM_USER_SGPR);
1118
1119 for (unsigned i = 0; i < shader->selector->num_vbos_in_user_sgprs; i++) {
1120 ret = si_insert_input_v4i32(ctx, ret, ctx->vb_descriptors[i],
1121 8 + SI_SGPR_VS_VB_DESCRIPTOR_FIRST + i * 4);
1122 }
1123 } else {
1124 assert(ctx->stage == MESA_SHADER_TESS_EVAL);
1125 ret = si_insert_input_ptr(ctx, ret, ctx->tcs_offchip_layout, 8 + SI_SGPR_TES_OFFCHIP_LAYOUT);
1126 ret = si_insert_input_ptr(ctx, ret, ctx->tes_offchip_addr, 8 + SI_SGPR_TES_OFFCHIP_ADDR);
1127 }
1128
1129 unsigned vgpr;
1130 if (ctx->stage == MESA_SHADER_VERTEX) {
1131 if (shader->selector->num_vbos_in_user_sgprs) {
1132 vgpr = 8 + SI_SGPR_VS_VB_DESCRIPTOR_FIRST + shader->selector->num_vbos_in_user_sgprs * 4;
1133 } else {
1134 vgpr = 8 + GFX9_VSGS_NUM_USER_SGPR + 1;
1135 }
1136 } else {
1137 vgpr = 8 + GFX9_TESGS_NUM_USER_SGPR;
1138 }
1139
1140 val = LLVMBuildLoad(builder, new_vgpr0, "");
1141 ret = LLVMBuildInsertValue(builder, ret, ac_to_float(&ctx->ac, val), vgpr++, "");
1142 vgpr++; /* gs_vtx23_offset */
1143
1144 ret = si_insert_input_ret_float(ctx, ret, ctx->args.gs_prim_id, vgpr++);
1145 ret = si_insert_input_ret_float(ctx, ret, ctx->args.gs_invocation_id, vgpr++);
1146 vgpr++; /* gs_vtx45_offset */
1147
1148 if (ctx->stage == MESA_SHADER_VERTEX) {
1149 val = LLVMBuildLoad(builder, es_data[0], "");
1150 ret = LLVMBuildInsertValue(builder, ret, ac_to_float(&ctx->ac, val), vgpr++,
1151 ""); /* VGPR5 - VertexID */
1152 vgpr += 2;
1153 if (uses_instance_id) {
1154 val = LLVMBuildLoad(builder, es_data[1], "");
1155 ret = LLVMBuildInsertValue(builder, ret, ac_to_float(&ctx->ac, val), vgpr++,
1156 ""); /* VGPR8 - InstanceID */
1157 } else {
1158 vgpr++;
1159 }
1160 } else {
1161 assert(ctx->stage == MESA_SHADER_TESS_EVAL);
1162 unsigned num_vgprs = uses_tes_prim_id ? 4 : 3;
1163 for (unsigned i = 0; i < num_vgprs; i++) {
1164 val = LLVMBuildLoad(builder, es_data[i], "");
1165 ret = LLVMBuildInsertValue(builder, ret, ac_to_float(&ctx->ac, val), vgpr++, "");
1166 }
1167 if (num_vgprs == 3)
1168 vgpr++;
1169 }
1170 /* Return the old thread ID. */
1171 val = LLVMBuildLoad(builder, old_thread_id, "");
1172 ret = LLVMBuildInsertValue(builder, ret, ac_to_float(&ctx->ac, val), vgpr++, "");
1173
1174 /* These two also use LDS. */
1175 if (sel->info.writes_edgeflag ||
1176 (ctx->stage == MESA_SHADER_VERTEX && shader->key.mono.u.vs_export_prim_id))
1177 ac_build_s_barrier(&ctx->ac);
1178
1179 ctx->return_value = ret;
1180 }
1181
1182 /**
1183 * Emit the epilogue of an API VS or TES shader compiled as ESGS shader.
1184 */
1185 void gfx10_emit_ngg_epilogue(struct ac_shader_abi *abi, unsigned max_outputs, LLVMValueRef *addrs)
1186 {
1187 struct si_shader_context *ctx = si_shader_context_from_abi(abi);
1188 struct si_shader_selector *sel = ctx->shader->selector;
1189 struct si_shader_info *info = &sel->info;
1190 struct si_shader_output_values outputs[PIPE_MAX_SHADER_OUTPUTS];
1191 LLVMBuilderRef builder = ctx->ac.builder;
1192 LLVMValueRef tmp, tmp2;
1193
1194 assert(!ctx->shader->is_gs_copy_shader);
1195 assert(info->num_outputs <= max_outputs);
1196
1197 LLVMValueRef vertex_ptr = NULL;
1198
1199 if (sel->so.num_outputs || sel->info.writes_edgeflag)
1200 vertex_ptr = ngg_nogs_vertex_ptr(ctx, get_thread_id_in_tg(ctx));
1201
1202 for (unsigned i = 0; i < info->num_outputs; i++) {
1203 outputs[i].semantic = info->output_semantic[i];
1204
1205 for (unsigned j = 0; j < 4; j++) {
1206 outputs[i].vertex_stream[j] = (info->output_streams[i] >> (2 * j)) & 3;
1207
1208 /* TODO: we may store more outputs than streamout needs,
1209 * but streamout performance isn't that important.
1210 */
1211 if (sel->so.num_outputs) {
1212 tmp = ac_build_gep0(&ctx->ac, vertex_ptr, LLVMConstInt(ctx->ac.i32, 4 * i + j, false));
1213 tmp2 = LLVMBuildLoad(builder, addrs[4 * i + j], "");
1214 tmp2 = ac_to_integer(&ctx->ac, tmp2);
1215 LLVMBuildStore(builder, tmp2, tmp);
1216 }
1217 }
1218
1219 /* Store the edgeflag at the end (if streamout is enabled) */
1220 if (info->output_semantic[i] == VARYING_SLOT_EDGE && sel->info.writes_edgeflag) {
1221 LLVMValueRef edgeflag = LLVMBuildLoad(builder, addrs[4 * i], "");
1222 /* The output is a float, but the hw expects a 1-bit integer. */
1223 edgeflag = LLVMBuildFPToUI(ctx->ac.builder, edgeflag, ctx->ac.i32, "");
1224 edgeflag = ac_build_umin(&ctx->ac, edgeflag, ctx->ac.i32_1);
1225
1226 tmp = LLVMConstInt(ctx->ac.i32, ngg_nogs_vertex_size(ctx->shader) - 1, 0);
1227 tmp = ac_build_gep0(&ctx->ac, vertex_ptr, tmp);
1228 LLVMBuildStore(builder, edgeflag, tmp);
1229 }
1230 }
1231
1232 bool unterminated_es_if_block =
1233 !sel->so.num_outputs && !sel->info.writes_edgeflag &&
1234 !ctx->screen->use_ngg_streamout && /* no query buffer */
1235 (ctx->stage != MESA_SHADER_VERTEX || !ctx->shader->key.mono.u.vs_export_prim_id);
1236
1237 if (!unterminated_es_if_block)
1238 ac_build_endif(&ctx->ac, ctx->merged_wrap_if_label);
1239
1240 LLVMValueRef is_gs_thread = si_is_gs_thread(ctx);
1241 LLVMValueRef is_es_thread = si_is_es_thread(ctx);
1242 LLVMValueRef vtxindex[3];
1243
1244 if (ctx->shader->key.opt.ngg_culling) {
1245 vtxindex[0] = si_unpack_param(ctx, ctx->gs_vtx01_offset, 0, 9);
1246 vtxindex[1] = si_unpack_param(ctx, ctx->gs_vtx01_offset, 10, 9);
1247 vtxindex[2] = si_unpack_param(ctx, ctx->gs_vtx01_offset, 20, 9);
1248 } else {
1249 vtxindex[0] = si_unpack_param(ctx, ctx->gs_vtx01_offset, 0, 16);
1250 vtxindex[1] = si_unpack_param(ctx, ctx->gs_vtx01_offset, 16, 16);
1251 vtxindex[2] = si_unpack_param(ctx, ctx->gs_vtx23_offset, 0, 16);
1252 }
1253
1254 /* Determine the number of vertices per primitive. */
1255 unsigned num_vertices;
1256 LLVMValueRef num_vertices_val = ngg_get_vertices_per_prim(ctx, &num_vertices);
1257
1258 /* Streamout */
1259 LLVMValueRef emitted_prims = NULL;
1260
1261 if (sel->so.num_outputs) {
1262 assert(!unterminated_es_if_block);
1263
1264 struct ngg_streamout nggso = {};
1265 nggso.num_vertices = num_vertices_val;
1266 nggso.prim_enable[0] = is_gs_thread;
1267
1268 for (unsigned i = 0; i < num_vertices; ++i)
1269 nggso.vertices[i] = ngg_nogs_vertex_ptr(ctx, vtxindex[i]);
1270
1271 build_streamout(ctx, &nggso);
1272 emitted_prims = nggso.emit[0];
1273 }
1274
1275 LLVMValueRef user_edgeflags[3] = {};
1276
1277 if (sel->info.writes_edgeflag) {
1278 assert(!unterminated_es_if_block);
1279
1280 /* Streamout already inserted the barrier, so don't insert it again. */
1281 if (!sel->so.num_outputs)
1282 ac_build_s_barrier(&ctx->ac);
1283
1284 ac_build_ifcc(&ctx->ac, is_gs_thread, 5400);
1285 /* Load edge flags from ES threads and store them into VGPRs in GS threads. */
1286 for (unsigned i = 0; i < num_vertices; i++) {
1287 tmp = ngg_nogs_vertex_ptr(ctx, vtxindex[i]);
1288 tmp2 = LLVMConstInt(ctx->ac.i32, ngg_nogs_vertex_size(ctx->shader) - 1, 0);
1289 tmp = ac_build_gep0(&ctx->ac, tmp, tmp2);
1290 tmp = LLVMBuildLoad(builder, tmp, "");
1291 tmp = LLVMBuildTrunc(builder, tmp, ctx->ac.i1, "");
1292
1293 user_edgeflags[i] = ac_build_alloca_undef(&ctx->ac, ctx->ac.i1, "");
1294 LLVMBuildStore(builder, tmp, user_edgeflags[i]);
1295 }
1296 ac_build_endif(&ctx->ac, 5400);
1297 }
1298
1299 /* Copy Primitive IDs from GS threads to the LDS address corresponding
1300 * to the ES thread of the provoking vertex.
1301 */
1302 if (ctx->stage == MESA_SHADER_VERTEX && ctx->shader->key.mono.u.vs_export_prim_id) {
1303 assert(!unterminated_es_if_block);
1304
1305 /* Streamout and edge flags use LDS. Make it idle, so that we can reuse it. */
1306 if (sel->so.num_outputs || sel->info.writes_edgeflag)
1307 ac_build_s_barrier(&ctx->ac);
1308
1309 ac_build_ifcc(&ctx->ac, is_gs_thread, 5400);
1310 /* Extract the PROVOKING_VTX_INDEX field. */
1311 LLVMValueRef provoking_vtx_in_prim = si_unpack_param(ctx, ctx->vs_state_bits, 4, 2);
1312
1313 /* provoking_vtx_index = vtxindex[provoking_vtx_in_prim]; */
1314 LLVMValueRef indices = ac_build_gather_values(&ctx->ac, vtxindex, 3);
1315 LLVMValueRef provoking_vtx_index =
1316 LLVMBuildExtractElement(builder, indices, provoking_vtx_in_prim, "");
1317 LLVMValueRef vertex_ptr = ngg_nogs_vertex_ptr(ctx, provoking_vtx_index);
1318
1319 LLVMBuildStore(builder, ac_get_arg(&ctx->ac, ctx->args.gs_prim_id),
1320 ac_build_gep0(&ctx->ac, vertex_ptr, ctx->ac.i32_0));
1321 ac_build_endif(&ctx->ac, 5400);
1322 }
1323
1324 /* Update query buffer */
1325 if (ctx->screen->use_ngg_streamout && !info->base.vs.blit_sgprs_amd) {
1326 assert(!unterminated_es_if_block);
1327
1328 tmp = si_unpack_param(ctx, ctx->vs_state_bits, 6, 1);
1329 tmp = LLVMBuildTrunc(builder, tmp, ctx->ac.i1, "");
1330 ac_build_ifcc(&ctx->ac, tmp, 5029); /* if (STREAMOUT_QUERY_ENABLED) */
1331 tmp = LLVMBuildICmp(builder, LLVMIntEQ, get_wave_id_in_tg(ctx), ctx->ac.i32_0, "");
1332 ac_build_ifcc(&ctx->ac, tmp, 5030);
1333 tmp = LLVMBuildICmp(builder, LLVMIntULE, ac_get_thread_id(&ctx->ac),
1334 sel->so.num_outputs ? ctx->ac.i32_1 : ctx->ac.i32_0, "");
1335 ac_build_ifcc(&ctx->ac, tmp, 5031);
1336 {
1337 LLVMValueRef args[] = {
1338 ngg_get_prim_cnt(ctx),
1339 ngg_get_query_buf(ctx),
1340 LLVMConstInt(ctx->ac.i32, 16, false), /* offset of stream[0].generated_primitives */
1341 ctx->ac.i32_0, /* soffset */
1342 ctx->ac.i32_0, /* cachepolicy */
1343 };
1344
1345 if (sel->so.num_outputs) {
1346 args[0] = ac_build_writelane(&ctx->ac, args[0], emitted_prims, ctx->ac.i32_1);
1347 args[2] = ac_build_writelane(&ctx->ac, args[2], LLVMConstInt(ctx->ac.i32, 24, false),
1348 ctx->ac.i32_1);
1349 }
1350
1351 /* TODO: should this be 64-bit atomics? */
1352 ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.raw.buffer.atomic.add.i32", ctx->ac.i32, args, 5,
1353 0);
1354 }
1355 ac_build_endif(&ctx->ac, 5031);
1356 ac_build_endif(&ctx->ac, 5030);
1357 ac_build_endif(&ctx->ac, 5029);
1358 }
1359
1360 /* Build the primitive export. */
1361 if (!gfx10_ngg_export_prim_early(ctx->shader)) {
1362 assert(!unterminated_es_if_block);
1363 gfx10_ngg_build_export_prim(ctx, user_edgeflags, NULL);
1364 }
1365
1366 /* Export per-vertex data (positions and parameters). */
1367 if (!unterminated_es_if_block)
1368 ac_build_ifcc(&ctx->ac, is_es_thread, 6002);
1369 {
1370 unsigned i;
1371
1372 /* Unconditionally (re-)load the values for proper SSA form. */
1373 for (i = 0; i < info->num_outputs; i++) {
1374 /* If the NGG cull shader part computed the position, don't
1375 * use the position from the current shader part. Instead,
1376 * load it from LDS.
1377 */
1378 if (info->output_semantic[i] == VARYING_SLOT_POS &&
1379 ctx->shader->key.opt.ngg_culling) {
1380 vertex_ptr = ngg_nogs_vertex_ptr(ctx, ac_get_arg(&ctx->ac, ctx->ngg_old_thread_id));
1381
1382 for (unsigned j = 0; j < 4; j++) {
1383 tmp = LLVMConstInt(ctx->ac.i32, lds_pos_x + j, 0);
1384 tmp = ac_build_gep0(&ctx->ac, vertex_ptr, tmp);
1385 tmp = LLVMBuildLoad(builder, tmp, "");
1386 outputs[i].values[j] = ac_to_float(&ctx->ac, tmp);
1387 }
1388 } else {
1389 for (unsigned j = 0; j < 4; j++) {
1390 outputs[i].values[j] = LLVMBuildLoad(builder, addrs[4 * i + j], "");
1391 }
1392 }
1393 }
1394
1395 if (ctx->shader->key.mono.u.vs_export_prim_id) {
1396 outputs[i].semantic = VARYING_SLOT_PRIMITIVE_ID;
1397
1398 if (ctx->stage == MESA_SHADER_VERTEX) {
1399 /* Wait for GS stores to finish. */
1400 ac_build_s_barrier(&ctx->ac);
1401
1402 tmp = ngg_nogs_vertex_ptr(ctx, get_thread_id_in_tg(ctx));
1403 tmp = ac_build_gep0(&ctx->ac, tmp, ctx->ac.i32_0);
1404 outputs[i].values[0] = LLVMBuildLoad(builder, tmp, "");
1405 } else {
1406 assert(ctx->stage == MESA_SHADER_TESS_EVAL);
1407 outputs[i].values[0] = si_get_primitive_id(ctx, 0);
1408 }
1409
1410 outputs[i].values[0] = ac_to_float(&ctx->ac, outputs[i].values[0]);
1411 for (unsigned j = 1; j < 4; j++)
1412 outputs[i].values[j] = LLVMGetUndef(ctx->ac.f32);
1413
1414 memset(outputs[i].vertex_stream, 0, sizeof(outputs[i].vertex_stream));
1415 i++;
1416 }
1417
1418 si_llvm_build_vs_exports(ctx, outputs, i);
1419 }
1420 ac_build_endif(&ctx->ac, 6002);
1421 }
1422
1423 static LLVMValueRef ngg_gs_get_vertex_storage(struct si_shader_context *ctx)
1424 {
1425 const struct si_shader_selector *sel = ctx->shader->selector;
1426 const struct si_shader_info *info = &sel->info;
1427
1428 LLVMTypeRef elements[2] = {
1429 LLVMArrayType(ctx->ac.i32, 4 * info->num_outputs),
1430 LLVMArrayType(ctx->ac.i8, 4),
1431 };
1432 LLVMTypeRef type = LLVMStructTypeInContext(ctx->ac.context, elements, 2, false);
1433 type = LLVMPointerType(LLVMArrayType(type, 0), AC_ADDR_SPACE_LDS);
1434 return LLVMBuildBitCast(ctx->ac.builder, ctx->gs_ngg_emit, type, "");
1435 }
1436
1437 /**
1438 * Return a pointer to the LDS storage reserved for the N'th vertex, where N
1439 * is in emit order; that is:
1440 * - during the epilogue, N is the threadidx (relative to the entire threadgroup)
1441 * - during vertex emit, i.e. while the API GS shader invocation is running,
1442 * N = threadidx * gs.vertices_out + emitidx
1443 *
1444 * Goals of the LDS memory layout:
1445 * 1. Eliminate bank conflicts on write for geometry shaders that have all emits
1446 * in uniform control flow
1447 * 2. Eliminate bank conflicts on read for export if, additionally, there is no
1448 * culling
1449 * 3. Agnostic to the number of waves (since we don't know it before compiling)
1450 * 4. Allow coalescing of LDS instructions (ds_write_b128 etc.)
1451 * 5. Avoid wasting memory.
1452 *
1453 * We use an AoS layout due to point 4 (this also helps point 3). In an AoS
1454 * layout, elimination of bank conflicts requires that each vertex occupy an
1455 * odd number of dwords. We use the additional dword to store the output stream
1456 * index as well as a flag to indicate whether this vertex ends a primitive
1457 * for rasterization.
1458 *
1459 * Swizzling is required to satisfy points 1 and 2 simultaneously.
1460 *
1461 * Vertices are stored in export order (gsthread * gs.vertices_out + emitidx).
1462 * Indices are swizzled in groups of 32, which ensures point 1 without
1463 * disturbing point 2.
1464 *
1465 * \return an LDS pointer to type {[N x i32], [4 x i8]}
1466 */
1467 static LLVMValueRef ngg_gs_vertex_ptr(struct si_shader_context *ctx, LLVMValueRef vertexidx)
1468 {
1469 struct si_shader_selector *sel = ctx->shader->selector;
1470 LLVMBuilderRef builder = ctx->ac.builder;
1471 LLVMValueRef storage = ngg_gs_get_vertex_storage(ctx);
1472
1473 /* gs.vertices_out = 2^(write_stride_2exp) * some odd number */
1474 unsigned write_stride_2exp = ffs(sel->info.base.gs.vertices_out) - 1;
1475 if (write_stride_2exp) {
1476 LLVMValueRef row = LLVMBuildLShr(builder, vertexidx, LLVMConstInt(ctx->ac.i32, 5, false), "");
1477 LLVMValueRef swizzle = LLVMBuildAnd(
1478 builder, row, LLVMConstInt(ctx->ac.i32, (1u << write_stride_2exp) - 1, false), "");
1479 vertexidx = LLVMBuildXor(builder, vertexidx, swizzle, "");
1480 }
1481
1482 return ac_build_gep0(&ctx->ac, storage, vertexidx);
1483 }
1484
1485 static LLVMValueRef ngg_gs_emit_vertex_ptr(struct si_shader_context *ctx, LLVMValueRef gsthread,
1486 LLVMValueRef emitidx)
1487 {
1488 struct si_shader_selector *sel = ctx->shader->selector;
1489 LLVMBuilderRef builder = ctx->ac.builder;
1490 LLVMValueRef tmp;
1491
1492 tmp = LLVMConstInt(ctx->ac.i32, sel->info.base.gs.vertices_out, false);
1493 tmp = LLVMBuildMul(builder, tmp, gsthread, "");
1494 const LLVMValueRef vertexidx = LLVMBuildAdd(builder, tmp, emitidx, "");
1495 return ngg_gs_vertex_ptr(ctx, vertexidx);
1496 }
1497
1498 static LLVMValueRef ngg_gs_get_emit_output_ptr(struct si_shader_context *ctx,
1499 LLVMValueRef vertexptr, unsigned out_idx)
1500 {
1501 LLVMValueRef gep_idx[3] = {
1502 ctx->ac.i32_0, /* implied C-style array */
1503 ctx->ac.i32_0, /* first struct entry */
1504 LLVMConstInt(ctx->ac.i32, out_idx, false),
1505 };
1506 return LLVMBuildGEP(ctx->ac.builder, vertexptr, gep_idx, 3, "");
1507 }
1508
1509 static LLVMValueRef ngg_gs_get_emit_primflag_ptr(struct si_shader_context *ctx,
1510 LLVMValueRef vertexptr, unsigned stream)
1511 {
1512 LLVMValueRef gep_idx[3] = {
1513 ctx->ac.i32_0, /* implied C-style array */
1514 ctx->ac.i32_1, /* second struct entry */
1515 LLVMConstInt(ctx->ac.i32, stream, false),
1516 };
1517 return LLVMBuildGEP(ctx->ac.builder, vertexptr, gep_idx, 3, "");
1518 }
1519
1520 void gfx10_ngg_gs_emit_vertex(struct si_shader_context *ctx, unsigned stream, LLVMValueRef *addrs)
1521 {
1522 const struct si_shader_selector *sel = ctx->shader->selector;
1523 const struct si_shader_info *info = &sel->info;
1524 LLVMBuilderRef builder = ctx->ac.builder;
1525 LLVMValueRef tmp;
1526 const LLVMValueRef vertexidx = LLVMBuildLoad(builder, ctx->gs_next_vertex[stream], "");
1527
1528 /* If this thread has already emitted the declared maximum number of
1529 * vertices, skip the write: excessive vertex emissions are not
1530 * supposed to have any effect.
1531 */
1532 const LLVMValueRef can_emit =
1533 LLVMBuildICmp(builder, LLVMIntULT, vertexidx,
1534 LLVMConstInt(ctx->ac.i32, sel->info.base.gs.vertices_out, false), "");
1535
1536 tmp = LLVMBuildAdd(builder, vertexidx, ctx->ac.i32_1, "");
1537 tmp = LLVMBuildSelect(builder, can_emit, tmp, vertexidx, "");
1538 LLVMBuildStore(builder, tmp, ctx->gs_next_vertex[stream]);
1539
1540 ac_build_ifcc(&ctx->ac, can_emit, 9001);
1541
1542 const LLVMValueRef vertexptr = ngg_gs_emit_vertex_ptr(ctx, get_thread_id_in_tg(ctx), vertexidx);
1543 unsigned out_idx = 0;
1544 for (unsigned i = 0; i < info->num_outputs; i++) {
1545 for (unsigned chan = 0; chan < 4; chan++, out_idx++) {
1546 if (!(info->output_usagemask[i] & (1 << chan)) ||
1547 ((info->output_streams[i] >> (2 * chan)) & 3) != stream)
1548 continue;
1549
1550 LLVMValueRef out_val = LLVMBuildLoad(builder, addrs[4 * i + chan], "");
1551 out_val = ac_to_integer(&ctx->ac, out_val);
1552 LLVMBuildStore(builder, out_val, ngg_gs_get_emit_output_ptr(ctx, vertexptr, out_idx));
1553 }
1554 }
1555 assert(out_idx * 4 == sel->gsvs_vertex_size);
1556
1557 /* Determine and store whether this vertex completed a primitive. */
1558 const LLVMValueRef curverts = LLVMBuildLoad(builder, ctx->gs_curprim_verts[stream], "");
1559
1560 tmp = LLVMConstInt(ctx->ac.i32, u_vertices_per_prim(sel->info.base.gs.output_primitive) - 1, false);
1561 const LLVMValueRef iscompleteprim = LLVMBuildICmp(builder, LLVMIntUGE, curverts, tmp, "");
1562
1563 /* Since the geometry shader emits triangle strips, we need to
1564 * track which primitive is odd and swap vertex indices to get
1565 * the correct vertex order.
1566 */
1567 LLVMValueRef is_odd = ctx->ac.i1false;
1568 if (stream == 0 && u_vertices_per_prim(sel->info.base.gs.output_primitive) == 3) {
1569 tmp = LLVMBuildAnd(builder, curverts, ctx->ac.i32_1, "");
1570 is_odd = LLVMBuildICmp(builder, LLVMIntEQ, tmp, ctx->ac.i32_1, "");
1571 }
1572
1573 tmp = LLVMBuildAdd(builder, curverts, ctx->ac.i32_1, "");
1574 LLVMBuildStore(builder, tmp, ctx->gs_curprim_verts[stream]);
1575
1576 /* The per-vertex primitive flag encoding:
1577 * bit 0: whether this vertex finishes a primitive
1578 * bit 1: whether the primitive is odd (if we are emitting triangle strips)
1579 */
1580 tmp = LLVMBuildZExt(builder, iscompleteprim, ctx->ac.i8, "");
1581 tmp = LLVMBuildOr(
1582 builder, tmp,
1583 LLVMBuildShl(builder, LLVMBuildZExt(builder, is_odd, ctx->ac.i8, ""), ctx->ac.i8_1, ""), "");
1584 LLVMBuildStore(builder, tmp, ngg_gs_get_emit_primflag_ptr(ctx, vertexptr, stream));
1585
1586 tmp = LLVMBuildLoad(builder, ctx->gs_generated_prims[stream], "");
1587 tmp = LLVMBuildAdd(builder, tmp, LLVMBuildZExt(builder, iscompleteprim, ctx->ac.i32, ""), "");
1588 LLVMBuildStore(builder, tmp, ctx->gs_generated_prims[stream]);
1589
1590 ac_build_endif(&ctx->ac, 9001);
1591 }
1592
1593 void gfx10_ngg_gs_emit_prologue(struct si_shader_context *ctx)
1594 {
1595 /* Zero out the part of LDS scratch that is used to accumulate the
1596 * per-stream generated primitive count.
1597 */
1598 LLVMBuilderRef builder = ctx->ac.builder;
1599 LLVMValueRef scratchptr = ctx->gs_ngg_scratch;
1600 LLVMValueRef tid = get_thread_id_in_tg(ctx);
1601 LLVMValueRef tmp;
1602
1603 tmp = LLVMBuildICmp(builder, LLVMIntULT, tid, LLVMConstInt(ctx->ac.i32, 4, false), "");
1604 ac_build_ifcc(&ctx->ac, tmp, 5090);
1605 {
1606 LLVMValueRef ptr = ac_build_gep0(&ctx->ac, scratchptr, tid);
1607 LLVMBuildStore(builder, ctx->ac.i32_0, ptr);
1608 }
1609 ac_build_endif(&ctx->ac, 5090);
1610
1611 ac_build_s_barrier(&ctx->ac);
1612 }
1613
1614 void gfx10_ngg_gs_emit_epilogue(struct si_shader_context *ctx)
1615 {
1616 const struct si_shader_selector *sel = ctx->shader->selector;
1617 const struct si_shader_info *info = &sel->info;
1618 const unsigned verts_per_prim = u_vertices_per_prim(sel->info.base.gs.output_primitive);
1619 LLVMBuilderRef builder = ctx->ac.builder;
1620 LLVMValueRef i8_0 = LLVMConstInt(ctx->ac.i8, 0, false);
1621 LLVMValueRef tmp, tmp2;
1622
1623 /* Zero out remaining (non-emitted) primitive flags.
1624 *
1625 * Note: Alternatively, we could pass the relevant gs_next_vertex to
1626 * the emit threads via LDS. This is likely worse in the expected
1627 * typical case where each GS thread emits the full set of
1628 * vertices.
1629 */
1630 for (unsigned stream = 0; stream < 4; ++stream) {
1631 if (!info->num_stream_output_components[stream])
1632 continue;
1633
1634 const LLVMValueRef gsthread = get_thread_id_in_tg(ctx);
1635
1636 ac_build_bgnloop(&ctx->ac, 5100);
1637
1638 const LLVMValueRef vertexidx = LLVMBuildLoad(builder, ctx->gs_next_vertex[stream], "");
1639 tmp = LLVMBuildICmp(builder, LLVMIntUGE, vertexidx,
1640 LLVMConstInt(ctx->ac.i32, sel->info.base.gs.vertices_out, false), "");
1641 ac_build_ifcc(&ctx->ac, tmp, 5101);
1642 ac_build_break(&ctx->ac);
1643 ac_build_endif(&ctx->ac, 5101);
1644
1645 tmp = LLVMBuildAdd(builder, vertexidx, ctx->ac.i32_1, "");
1646 LLVMBuildStore(builder, tmp, ctx->gs_next_vertex[stream]);
1647
1648 tmp = ngg_gs_emit_vertex_ptr(ctx, gsthread, vertexidx);
1649 LLVMBuildStore(builder, i8_0, ngg_gs_get_emit_primflag_ptr(ctx, tmp, stream));
1650
1651 ac_build_endloop(&ctx->ac, 5100);
1652 }
1653
1654 /* Accumulate generated primitives counts across the entire threadgroup. */
1655 for (unsigned stream = 0; stream < 4; ++stream) {
1656 if (!info->num_stream_output_components[stream])
1657 continue;
1658
1659 LLVMValueRef numprims = LLVMBuildLoad(builder, ctx->gs_generated_prims[stream], "");
1660 numprims = ac_build_reduce(&ctx->ac, numprims, nir_op_iadd, ctx->ac.wave_size);
1661
1662 tmp = LLVMBuildICmp(builder, LLVMIntEQ, ac_get_thread_id(&ctx->ac), ctx->ac.i32_0, "");
1663 ac_build_ifcc(&ctx->ac, tmp, 5105);
1664 {
1665 LLVMBuildAtomicRMW(
1666 builder, LLVMAtomicRMWBinOpAdd,
1667 ac_build_gep0(&ctx->ac, ctx->gs_ngg_scratch, LLVMConstInt(ctx->ac.i32, stream, false)),
1668 numprims, LLVMAtomicOrderingMonotonic, false);
1669 }
1670 ac_build_endif(&ctx->ac, 5105);
1671 }
1672
1673 ac_build_endif(&ctx->ac, ctx->merged_wrap_if_label);
1674
1675 ac_build_s_barrier(&ctx->ac);
1676
1677 const LLVMValueRef tid = get_thread_id_in_tg(ctx);
1678 LLVMValueRef num_emit_threads = ngg_get_prim_cnt(ctx);
1679
1680 /* Streamout */
1681 if (sel->so.num_outputs) {
1682 struct ngg_streamout nggso = {};
1683
1684 nggso.num_vertices = LLVMConstInt(ctx->ac.i32, verts_per_prim, false);
1685
1686 LLVMValueRef vertexptr = ngg_gs_vertex_ptr(ctx, tid);
1687 for (unsigned stream = 0; stream < 4; ++stream) {
1688 if (!info->num_stream_output_components[stream])
1689 continue;
1690
1691 tmp = LLVMBuildLoad(builder, ngg_gs_get_emit_primflag_ptr(ctx, vertexptr, stream), "");
1692 tmp = LLVMBuildTrunc(builder, tmp, ctx->ac.i1, "");
1693 tmp2 = LLVMBuildICmp(builder, LLVMIntULT, tid, num_emit_threads, "");
1694 nggso.prim_enable[stream] = LLVMBuildAnd(builder, tmp, tmp2, "");
1695 }
1696
1697 for (unsigned i = 0; i < verts_per_prim; ++i) {
1698 tmp = LLVMBuildSub(builder, tid, LLVMConstInt(ctx->ac.i32, verts_per_prim - i - 1, false),
1699 "");
1700 tmp = ngg_gs_vertex_ptr(ctx, tmp);
1701 nggso.vertices[i] = ac_build_gep0(&ctx->ac, tmp, ctx->ac.i32_0);
1702 }
1703
1704 build_streamout(ctx, &nggso);
1705 }
1706
1707 /* Write shader query data. */
1708 if (ctx->screen->use_ngg_streamout) {
1709 tmp = si_unpack_param(ctx, ctx->vs_state_bits, 6, 1);
1710 tmp = LLVMBuildTrunc(builder, tmp, ctx->ac.i1, "");
1711 ac_build_ifcc(&ctx->ac, tmp, 5109); /* if (STREAMOUT_QUERY_ENABLED) */
1712 unsigned num_query_comps = sel->so.num_outputs ? 8 : 4;
1713 tmp = LLVMBuildICmp(builder, LLVMIntULT, tid,
1714 LLVMConstInt(ctx->ac.i32, num_query_comps, false), "");
1715 ac_build_ifcc(&ctx->ac, tmp, 5110);
1716 {
1717 LLVMValueRef offset;
1718 tmp = tid;
1719 if (sel->so.num_outputs)
1720 tmp = LLVMBuildAnd(builder, tmp, LLVMConstInt(ctx->ac.i32, 3, false), "");
1721 offset = LLVMBuildNUWMul(builder, tmp, LLVMConstInt(ctx->ac.i32, 32, false), "");
1722 if (sel->so.num_outputs) {
1723 tmp = LLVMBuildLShr(builder, tid, LLVMConstInt(ctx->ac.i32, 2, false), "");
1724 tmp = LLVMBuildNUWMul(builder, tmp, LLVMConstInt(ctx->ac.i32, 8, false), "");
1725 offset = LLVMBuildAdd(builder, offset, tmp, "");
1726 }
1727
1728 tmp = LLVMBuildLoad(builder, ac_build_gep0(&ctx->ac, ctx->gs_ngg_scratch, tid), "");
1729 LLVMValueRef args[] = {
1730 tmp, ngg_get_query_buf(ctx),
1731 offset, LLVMConstInt(ctx->ac.i32, 16, false), /* soffset */
1732 ctx->ac.i32_0, /* cachepolicy */
1733 };
1734 ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.raw.buffer.atomic.add.i32", ctx->ac.i32, args, 5,
1735 0);
1736 }
1737 ac_build_endif(&ctx->ac, 5110);
1738 ac_build_endif(&ctx->ac, 5109);
1739 }
1740
1741 /* Determine vertex liveness. */
1742 LLVMValueRef vertliveptr = ac_build_alloca(&ctx->ac, ctx->ac.i1, "vertexlive");
1743
1744 tmp = LLVMBuildICmp(builder, LLVMIntULT, tid, num_emit_threads, "");
1745 ac_build_ifcc(&ctx->ac, tmp, 5120);
1746 {
1747 for (unsigned i = 0; i < verts_per_prim; ++i) {
1748 const LLVMValueRef primidx =
1749 LLVMBuildAdd(builder, tid, LLVMConstInt(ctx->ac.i32, i, false), "");
1750
1751 if (i > 0) {
1752 tmp = LLVMBuildICmp(builder, LLVMIntULT, primidx, num_emit_threads, "");
1753 ac_build_ifcc(&ctx->ac, tmp, 5121 + i);
1754 }
1755
1756 /* Load primitive liveness */
1757 tmp = ngg_gs_vertex_ptr(ctx, primidx);
1758 tmp = LLVMBuildLoad(builder, ngg_gs_get_emit_primflag_ptr(ctx, tmp, 0), "");
1759 const LLVMValueRef primlive = LLVMBuildTrunc(builder, tmp, ctx->ac.i1, "");
1760
1761 tmp = LLVMBuildLoad(builder, vertliveptr, "");
1762 tmp = LLVMBuildOr(builder, tmp, primlive, ""), LLVMBuildStore(builder, tmp, vertliveptr);
1763
1764 if (i > 0)
1765 ac_build_endif(&ctx->ac, 5121 + i);
1766 }
1767 }
1768 ac_build_endif(&ctx->ac, 5120);
1769
1770 /* Inclusive scan addition across the current wave. */
1771 LLVMValueRef vertlive = LLVMBuildLoad(builder, vertliveptr, "");
1772 struct ac_wg_scan vertlive_scan = {};
1773 vertlive_scan.op = nir_op_iadd;
1774 vertlive_scan.enable_reduce = true;
1775 vertlive_scan.enable_exclusive = true;
1776 vertlive_scan.src = vertlive;
1777 vertlive_scan.scratch = ac_build_gep0(&ctx->ac, ctx->gs_ngg_scratch, ctx->ac.i32_0);
1778 vertlive_scan.waveidx = get_wave_id_in_tg(ctx);
1779 vertlive_scan.numwaves = get_tgsize(ctx);
1780 vertlive_scan.maxwaves = 8;
1781
1782 ac_build_wg_scan(&ctx->ac, &vertlive_scan);
1783
1784 /* Skip all exports (including index exports) when possible. At least on
1785 * early gfx10 revisions this is also to avoid hangs.
1786 */
1787 LLVMValueRef have_exports =
1788 LLVMBuildICmp(builder, LLVMIntNE, vertlive_scan.result_reduce, ctx->ac.i32_0, "");
1789 num_emit_threads = LLVMBuildSelect(builder, have_exports, num_emit_threads, ctx->ac.i32_0, "");
1790
1791 /* Allocate export space. Send this message as early as possible, to
1792 * hide the latency of the SQ <-> SPI roundtrip.
1793 *
1794 * Note: We could consider compacting primitives for export as well.
1795 * PA processes 1 non-null prim / clock, but it fetches 4 DW of
1796 * prim data per clock and skips null primitives at no additional
1797 * cost. So compacting primitives can only be beneficial when
1798 * there are 4 or more contiguous null primitives in the export
1799 * (in the common case of single-dword prim exports).
1800 */
1801 ac_build_sendmsg_gs_alloc_req(&ctx->ac, get_wave_id_in_tg(ctx), vertlive_scan.result_reduce,
1802 num_emit_threads);
1803
1804 /* Setup the reverse vertex compaction permutation. We re-use stream 1
1805 * of the primitive liveness flags, relying on the fact that each
1806 * threadgroup can have at most 256 threads. */
1807 ac_build_ifcc(&ctx->ac, vertlive, 5130);
1808 {
1809 tmp = ngg_gs_vertex_ptr(ctx, vertlive_scan.result_exclusive);
1810 tmp2 = LLVMBuildTrunc(builder, tid, ctx->ac.i8, "");
1811 LLVMBuildStore(builder, tmp2, ngg_gs_get_emit_primflag_ptr(ctx, tmp, 1));
1812 }
1813 ac_build_endif(&ctx->ac, 5130);
1814
1815 ac_build_s_barrier(&ctx->ac);
1816
1817 /* Export primitive data */
1818 tmp = LLVMBuildICmp(builder, LLVMIntULT, tid, num_emit_threads, "");
1819 ac_build_ifcc(&ctx->ac, tmp, 5140);
1820 {
1821 LLVMValueRef flags;
1822 struct ac_ngg_prim prim = {};
1823 prim.num_vertices = verts_per_prim;
1824
1825 tmp = ngg_gs_vertex_ptr(ctx, tid);
1826 flags = LLVMBuildLoad(builder, ngg_gs_get_emit_primflag_ptr(ctx, tmp, 0), "");
1827 prim.isnull = LLVMBuildNot(builder, LLVMBuildTrunc(builder, flags, ctx->ac.i1, ""), "");
1828
1829 for (unsigned i = 0; i < verts_per_prim; ++i) {
1830 prim.index[i] = LLVMBuildSub(builder, vertlive_scan.result_exclusive,
1831 LLVMConstInt(ctx->ac.i32, verts_per_prim - i - 1, false), "");
1832 prim.edgeflag[i] = ctx->ac.i1false;
1833 }
1834
1835 /* Geometry shaders output triangle strips, but NGG expects triangles. */
1836 if (verts_per_prim == 3) {
1837 LLVMValueRef is_odd = LLVMBuildLShr(builder, flags, ctx->ac.i8_1, "");
1838 is_odd = LLVMBuildTrunc(builder, is_odd, ctx->ac.i1, "");
1839 LLVMValueRef flatshade_first = LLVMBuildICmp(
1840 builder, LLVMIntEQ, si_unpack_param(ctx, ctx->vs_state_bits, 4, 2), ctx->ac.i32_0, "");
1841
1842 ac_build_triangle_strip_indices_to_triangle(&ctx->ac, is_odd, flatshade_first, prim.index);
1843 }
1844
1845 ac_build_export_prim(&ctx->ac, &prim);
1846 }
1847 ac_build_endif(&ctx->ac, 5140);
1848
1849 /* Export position and parameter data */
1850 tmp = LLVMBuildICmp(builder, LLVMIntULT, tid, vertlive_scan.result_reduce, "");
1851 ac_build_ifcc(&ctx->ac, tmp, 5145);
1852 {
1853 struct si_shader_output_values outputs[PIPE_MAX_SHADER_OUTPUTS];
1854
1855 tmp = ngg_gs_vertex_ptr(ctx, tid);
1856 tmp = LLVMBuildLoad(builder, ngg_gs_get_emit_primflag_ptr(ctx, tmp, 1), "");
1857 tmp = LLVMBuildZExt(builder, tmp, ctx->ac.i32, "");
1858 const LLVMValueRef vertexptr = ngg_gs_vertex_ptr(ctx, tmp);
1859
1860 unsigned out_idx = 0;
1861 for (unsigned i = 0; i < info->num_outputs; i++) {
1862 outputs[i].semantic = info->output_semantic[i];
1863
1864 for (unsigned j = 0; j < 4; j++, out_idx++) {
1865 tmp = ngg_gs_get_emit_output_ptr(ctx, vertexptr, out_idx);
1866 tmp = LLVMBuildLoad(builder, tmp, "");
1867 outputs[i].values[j] = ac_to_float(&ctx->ac, tmp);
1868 outputs[i].vertex_stream[j] = (info->output_streams[i] >> (2 * j)) & 3;
1869 }
1870 }
1871
1872 si_llvm_build_vs_exports(ctx, outputs, info->num_outputs);
1873 }
1874 ac_build_endif(&ctx->ac, 5145);
1875 }
1876
1877 static void clamp_gsprims_to_esverts(unsigned *max_gsprims, unsigned max_esverts,
1878 unsigned min_verts_per_prim, bool use_adjacency)
1879 {
1880 unsigned max_reuse = max_esverts - min_verts_per_prim;
1881 if (use_adjacency)
1882 max_reuse /= 2;
1883 *max_gsprims = MIN2(*max_gsprims, 1 + max_reuse);
1884 }
1885
1886 unsigned gfx10_ngg_get_scratch_dw_size(struct si_shader *shader)
1887 {
1888 const struct si_shader_selector *sel = shader->selector;
1889
1890 if (sel->info.stage == MESA_SHADER_GEOMETRY && sel->so.num_outputs)
1891 return 44;
1892
1893 return 8;
1894 }
1895
1896 /**
1897 * Determine subgroup information like maximum number of vertices and prims.
1898 *
1899 * This happens before the shader is uploaded, since LDS relocations during
1900 * upload depend on the subgroup size.
1901 */
1902 bool gfx10_ngg_calculate_subgroup_info(struct si_shader *shader)
1903 {
1904 const struct si_shader_selector *gs_sel = shader->selector;
1905 const struct si_shader_selector *es_sel =
1906 shader->previous_stage_sel ? shader->previous_stage_sel : gs_sel;
1907 const gl_shader_stage gs_stage = gs_sel->info.stage;
1908 const unsigned gs_num_invocations = MAX2(gs_sel->info.base.gs.invocations, 1);
1909 const unsigned input_prim = si_get_input_prim(gs_sel);
1910 const bool use_adjacency =
1911 input_prim >= PIPE_PRIM_LINES_ADJACENCY && input_prim <= PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY;
1912 const unsigned max_verts_per_prim = u_vertices_per_prim(input_prim);
1913 const unsigned min_verts_per_prim = gs_stage == MESA_SHADER_GEOMETRY ? max_verts_per_prim : 1;
1914
1915 /* All these are in dwords: */
1916 /* GE can only use 8K dwords (32KB) of LDS per workgroup.
1917 */
1918 const unsigned max_lds_size = 8 * 1024 - gfx10_ngg_get_scratch_dw_size(shader);
1919 const unsigned target_lds_size = max_lds_size;
1920 unsigned esvert_lds_size = 0;
1921 unsigned gsprim_lds_size = 0;
1922
1923 /* All these are per subgroup: */
1924 const unsigned min_esverts = gs_sel->screen->info.chip_class >= GFX10_3 ? 29 : 24;
1925 bool max_vert_out_per_gs_instance = false;
1926 unsigned max_gsprims_base = 128; /* default prim group size clamp */
1927 unsigned max_esverts_base = 128;
1928
1929 if (shader->key.opt.ngg_culling & SI_NGG_CULL_GS_FAST_LAUNCH_TRI_LIST) {
1930 max_gsprims_base = 128 / 3;
1931 max_esverts_base = max_gsprims_base * 3;
1932 } else if (shader->key.opt.ngg_culling & SI_NGG_CULL_GS_FAST_LAUNCH_TRI_STRIP) {
1933 max_gsprims_base = 126;
1934 max_esverts_base = 128;
1935 }
1936
1937 /* Hardware has the following non-natural restrictions on the value
1938 * of GE_CNTL.VERT_GRP_SIZE based on based on the primitive type of
1939 * the draw:
1940 * - at most 252 for any line input primitive type
1941 * - at most 251 for any quad input primitive type
1942 * - at most 251 for triangle strips with adjacency (this happens to
1943 * be the natural limit for triangle *lists* with adjacency)
1944 */
1945 max_esverts_base = MIN2(max_esverts_base, 251 + max_verts_per_prim - 1);
1946
1947 if (gs_stage == MESA_SHADER_GEOMETRY) {
1948 bool force_multi_cycling = false;
1949 unsigned max_out_verts_per_gsprim = gs_sel->info.base.gs.vertices_out * gs_num_invocations;
1950
1951 retry_select_mode:
1952 if (max_out_verts_per_gsprim <= 256 && !force_multi_cycling) {
1953 if (max_out_verts_per_gsprim) {
1954 max_gsprims_base = MIN2(max_gsprims_base, 256 / max_out_verts_per_gsprim);
1955 }
1956 } else {
1957 /* Use special multi-cycling mode in which each GS
1958 * instance gets its own subgroup. Does not work with
1959 * tessellation. */
1960 max_vert_out_per_gs_instance = true;
1961 max_gsprims_base = 1;
1962 max_out_verts_per_gsprim = gs_sel->info.base.gs.vertices_out;
1963 }
1964
1965 esvert_lds_size = es_sel->esgs_itemsize / 4;
1966 gsprim_lds_size = (gs_sel->gsvs_vertex_size / 4 + 1) * max_out_verts_per_gsprim;
1967
1968 if (gsprim_lds_size > target_lds_size && !force_multi_cycling) {
1969 if (gs_sel->tess_turns_off_ngg || es_sel->info.stage != MESA_SHADER_TESS_EVAL) {
1970 force_multi_cycling = true;
1971 goto retry_select_mode;
1972 }
1973 }
1974 } else {
1975 /* VS and TES. */
1976 /* LDS size for passing data from ES to GS. */
1977 esvert_lds_size = ngg_nogs_vertex_size(shader);
1978 }
1979
1980 unsigned max_gsprims = max_gsprims_base;
1981 unsigned max_esverts = max_esverts_base;
1982
1983 if (esvert_lds_size)
1984 max_esverts = MIN2(max_esverts, target_lds_size / esvert_lds_size);
1985 if (gsprim_lds_size)
1986 max_gsprims = MIN2(max_gsprims, target_lds_size / gsprim_lds_size);
1987
1988 max_esverts = MIN2(max_esverts, max_gsprims * max_verts_per_prim);
1989 clamp_gsprims_to_esverts(&max_gsprims, max_esverts, min_verts_per_prim, use_adjacency);
1990 assert(max_esverts >= max_verts_per_prim && max_gsprims >= 1);
1991
1992 if (esvert_lds_size || gsprim_lds_size) {
1993 /* Now that we have a rough proportionality between esverts
1994 * and gsprims based on the primitive type, scale both of them
1995 * down simultaneously based on required LDS space.
1996 *
1997 * We could be smarter about this if we knew how much vertex
1998 * reuse to expect.
1999 */
2000 unsigned lds_total = max_esverts * esvert_lds_size + max_gsprims * gsprim_lds_size;
2001 if (lds_total > target_lds_size) {
2002 max_esverts = max_esverts * target_lds_size / lds_total;
2003 max_gsprims = max_gsprims * target_lds_size / lds_total;
2004
2005 max_esverts = MIN2(max_esverts, max_gsprims * max_verts_per_prim);
2006 clamp_gsprims_to_esverts(&max_gsprims, max_esverts, min_verts_per_prim, use_adjacency);
2007 assert(max_esverts >= max_verts_per_prim && max_gsprims >= 1);
2008 }
2009 }
2010
2011 /* Round up towards full wave sizes for better ALU utilization. */
2012 if (!max_vert_out_per_gs_instance) {
2013 const unsigned wavesize = si_get_shader_wave_size(shader);
2014 unsigned orig_max_esverts;
2015 unsigned orig_max_gsprims;
2016 do {
2017 orig_max_esverts = max_esverts;
2018 orig_max_gsprims = max_gsprims;
2019
2020 max_esverts = align(max_esverts, wavesize);
2021 max_esverts = MIN2(max_esverts, max_esverts_base);
2022 if (esvert_lds_size)
2023 max_esverts =
2024 MIN2(max_esverts, (max_lds_size - max_gsprims * gsprim_lds_size) / esvert_lds_size);
2025 max_esverts = MIN2(max_esverts, max_gsprims * max_verts_per_prim);
2026 /* Hardware restriction: minimum value of max_esverts */
2027 max_esverts = MAX2(max_esverts, min_esverts - 1 + max_verts_per_prim);
2028
2029 max_gsprims = align(max_gsprims, wavesize);
2030 max_gsprims = MIN2(max_gsprims, max_gsprims_base);
2031 if (gsprim_lds_size) {
2032 /* Don't count unusable vertices to the LDS size. Those are vertices above
2033 * the maximum number of vertices that can occur in the workgroup,
2034 * which is e.g. max_gsprims * 3 for triangles.
2035 */
2036 unsigned usable_esverts = MIN2(max_esverts, max_gsprims * max_verts_per_prim);
2037 max_gsprims =
2038 MIN2(max_gsprims, (max_lds_size - usable_esverts * esvert_lds_size) / gsprim_lds_size);
2039 }
2040 clamp_gsprims_to_esverts(&max_gsprims, max_esverts, min_verts_per_prim, use_adjacency);
2041 assert(max_esverts >= max_verts_per_prim && max_gsprims >= 1);
2042 } while (orig_max_esverts != max_esverts || orig_max_gsprims != max_gsprims);
2043
2044 /* Verify the restriction. */
2045 assert(max_esverts >= min_esverts - 1 + max_verts_per_prim);
2046 } else {
2047 /* Hardware restriction: minimum value of max_esverts */
2048 max_esverts = MAX2(max_esverts, min_esverts - 1 + max_verts_per_prim);
2049 }
2050
2051 unsigned max_out_vertices =
2052 max_vert_out_per_gs_instance
2053 ? gs_sel->info.base.gs.vertices_out
2054 : gs_stage == MESA_SHADER_GEOMETRY
2055 ? max_gsprims * gs_num_invocations * gs_sel->info.base.gs.vertices_out
2056 : max_esverts;
2057 assert(max_out_vertices <= 256);
2058
2059 unsigned prim_amp_factor = 1;
2060 if (gs_stage == MESA_SHADER_GEOMETRY) {
2061 /* Number of output primitives per GS input primitive after
2062 * GS instancing. */
2063 prim_amp_factor = gs_sel->info.base.gs.vertices_out;
2064 }
2065
2066 /* The GE only checks against the maximum number of ES verts after
2067 * allocating a full GS primitive. So we need to ensure that whenever
2068 * this check passes, there is enough space for a full primitive without
2069 * vertex reuse.
2070 */
2071 shader->ngg.hw_max_esverts = max_esverts - max_verts_per_prim + 1;
2072 shader->ngg.max_gsprims = max_gsprims;
2073 shader->ngg.max_out_verts = max_out_vertices;
2074 shader->ngg.prim_amp_factor = prim_amp_factor;
2075 shader->ngg.max_vert_out_per_gs_instance = max_vert_out_per_gs_instance;
2076
2077 /* Don't count unusable vertices. */
2078 shader->gs_info.esgs_ring_size = MIN2(max_esverts, max_gsprims * max_verts_per_prim) *
2079 esvert_lds_size;
2080 shader->ngg.ngg_emit_size = max_gsprims * gsprim_lds_size;
2081
2082 assert(shader->ngg.hw_max_esverts >= min_esverts); /* HW limitation */
2083
2084 /* If asserts are disabled, we use the same conditions to return false */
2085 return max_esverts >= max_verts_per_prim && max_gsprims >= 1 &&
2086 max_out_vertices <= 256 &&
2087 shader->ngg.hw_max_esverts >= min_esverts;
2088 }