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