radeonsi/gfx10: update comments and remove invalid TODOs
[mesa.git] / src / gallium / drivers / radeonsi / gfx10_shader_ngg.c
1 /*
2 * Copyright 2017 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "si_pipe.h"
25 #include "si_shader_internal.h"
26
27 #include "sid.h"
28
29 #include "util/u_memory.h"
30 #include "util/u_prim.h"
31
32 static LLVMValueRef get_wave_id_in_tg(struct si_shader_context *ctx)
33 {
34 return si_unpack_param(ctx, ctx->merged_wave_info, 24, 4);
35 }
36
37 static LLVMValueRef get_tgsize(struct si_shader_context *ctx)
38 {
39 return si_unpack_param(ctx, ctx->merged_wave_info, 28, 4);
40 }
41
42 static LLVMValueRef get_thread_id_in_tg(struct si_shader_context *ctx)
43 {
44 LLVMBuilderRef builder = ctx->ac.builder;
45 LLVMValueRef tmp;
46 tmp = LLVMBuildMul(builder, get_wave_id_in_tg(ctx),
47 LLVMConstInt(ctx->ac.i32, ctx->ac.wave_size, false), "");
48 return LLVMBuildAdd(builder, tmp, ac_get_thread_id(&ctx->ac), "");
49 }
50
51 static LLVMValueRef ngg_get_vtx_cnt(struct si_shader_context *ctx)
52 {
53 return si_unpack_param(ctx, ctx->gs_tg_info, 12, 9);
54 }
55
56 static LLVMValueRef ngg_get_prim_cnt(struct si_shader_context *ctx)
57 {
58 return si_unpack_param(ctx, ctx->gs_tg_info, 22, 9);
59 }
60
61 static LLVMValueRef ngg_get_ordered_id(struct si_shader_context *ctx)
62 {
63 return si_unpack_param(ctx, ctx->gs_tg_info, 0, 12);
64 }
65
66 static LLVMValueRef ngg_get_query_buf(struct si_shader_context *ctx)
67 {
68 LLVMValueRef buf_ptr = ac_get_arg(&ctx->ac, ctx->rw_buffers);
69
70 return ac_build_load_to_sgpr(&ctx->ac, buf_ptr,
71 LLVMConstInt(ctx->i32, GFX10_GS_QUERY_BUF, false));
72 }
73
74 static LLVMValueRef ngg_get_initial_edgeflag(struct si_shader_context *ctx, unsigned index)
75 {
76 if (ctx->type == PIPE_SHADER_VERTEX) {
77 LLVMValueRef tmp;
78 tmp = LLVMBuildLShr(ctx->ac.builder,
79 ac_get_arg(&ctx->ac, ctx->args.gs_invocation_id),
80 LLVMConstInt(ctx->ac.i32, 8 + index, false), "");
81 return LLVMBuildTrunc(ctx->ac.builder, tmp, ctx->ac.i1, "");
82 }
83 return ctx->i1false;
84 }
85
86 /**
87 * Return the number of vertices as a constant in \p num_vertices,
88 * and return a more precise value as LLVMValueRef from the function.
89 */
90 static LLVMValueRef ngg_get_vertices_per_prim(struct si_shader_context *ctx,
91 unsigned *num_vertices)
92 {
93 const struct si_shader_info *info = &ctx->shader->selector->info;
94
95 if (ctx->type == PIPE_SHADER_VERTEX) {
96 if (info->properties[TGSI_PROPERTY_VS_BLIT_SGPRS_AMD]) {
97 /* Blits always use axis-aligned rectangles with 3 vertices. */
98 *num_vertices = 3;
99 return LLVMConstInt(ctx->i32, 3, 0);
100 } else {
101 /* We always build up all three indices for the prim export
102 * independent of the primitive type. The additional garbage
103 * data shouldn't hurt. This number doesn't matter with
104 * NGG passthrough.
105 */
106 *num_vertices = 3;
107
108 /* Extract OUTPRIM field. */
109 LLVMValueRef num = si_unpack_param(ctx, ctx->vs_state_bits, 2, 2);
110 return LLVMBuildAdd(ctx->ac.builder, num, ctx->i32_1, "");
111 }
112 } else {
113 assert(ctx->type == PIPE_SHADER_TESS_EVAL);
114
115 if (info->properties[TGSI_PROPERTY_TES_POINT_MODE])
116 *num_vertices = 1;
117 else if (info->properties[TGSI_PROPERTY_TES_PRIM_MODE] == PIPE_PRIM_LINES)
118 *num_vertices = 2;
119 else
120 *num_vertices = 3;
121
122 return LLVMConstInt(ctx->i32, *num_vertices, false);
123 }
124 }
125
126 static void build_streamout_vertex(struct si_shader_context *ctx,
127 LLVMValueRef *so_buffer, LLVMValueRef *wg_offset_dw,
128 unsigned stream, LLVMValueRef offset_vtx,
129 LLVMValueRef vertexptr)
130 {
131 struct si_shader_info *info = &ctx->shader->selector->info;
132 struct pipe_stream_output_info *so = &ctx->shader->selector->so;
133 LLVMBuilderRef builder = ctx->ac.builder;
134 LLVMValueRef offset[4] = {};
135 LLVMValueRef tmp;
136
137 for (unsigned buffer = 0; buffer < 4; ++buffer) {
138 if (!wg_offset_dw[buffer])
139 continue;
140
141 tmp = LLVMBuildMul(builder, offset_vtx,
142 LLVMConstInt(ctx->i32, so->stride[buffer], false), "");
143 tmp = LLVMBuildAdd(builder, wg_offset_dw[buffer], tmp, "");
144 offset[buffer] = LLVMBuildShl(builder, tmp, LLVMConstInt(ctx->i32, 2, false), "");
145 }
146
147 for (unsigned i = 0; i < so->num_outputs; ++i) {
148 if (so->output[i].stream != stream)
149 continue;
150
151 unsigned reg = so->output[i].register_index;
152 struct si_shader_output_values out;
153 out.semantic_name = info->output_semantic_name[reg];
154 out.semantic_index = info->output_semantic_index[reg];
155
156 for (unsigned comp = 0; comp < 4; comp++) {
157 tmp = ac_build_gep0(&ctx->ac, vertexptr,
158 LLVMConstInt(ctx->i32, 4 * reg + comp, false));
159 out.values[comp] = LLVMBuildLoad(builder, tmp, "");
160 out.vertex_stream[comp] =
161 (info->output_streams[reg] >> (2 * comp)) & 3;
162 }
163
164 si_emit_streamout_output(ctx, so_buffer, offset, &so->output[i], &out);
165 }
166 }
167
168 struct ngg_streamout {
169 LLVMValueRef num_vertices;
170
171 /* per-thread data */
172 LLVMValueRef prim_enable[4]; /* i1 per stream */
173 LLVMValueRef vertices[3]; /* [N x i32] addrspace(LDS)* */
174
175 /* Output */
176 LLVMValueRef emit[4]; /* per-stream emitted primitives (only valid for used streams) */
177 };
178
179 /**
180 * Build streamout logic.
181 *
182 * Implies a barrier.
183 *
184 * Writes number of emitted primitives to gs_ngg_scratch[4:8].
185 *
186 * Clobbers gs_ngg_scratch[8:].
187 */
188 static void build_streamout(struct si_shader_context *ctx,
189 struct ngg_streamout *nggso)
190 {
191 struct si_shader_info *info = &ctx->shader->selector->info;
192 struct pipe_stream_output_info *so = &ctx->shader->selector->so;
193 LLVMBuilderRef builder = ctx->ac.builder;
194 LLVMValueRef buf_ptr = ac_get_arg(&ctx->ac, ctx->rw_buffers);
195 LLVMValueRef tid = get_thread_id_in_tg(ctx);
196 LLVMValueRef tmp, tmp2;
197 LLVMValueRef i32_2 = LLVMConstInt(ctx->i32, 2, false);
198 LLVMValueRef i32_4 = LLVMConstInt(ctx->i32, 4, false);
199 LLVMValueRef i32_8 = LLVMConstInt(ctx->i32, 8, false);
200 LLVMValueRef so_buffer[4] = {};
201 unsigned max_num_vertices = 1 + (nggso->vertices[1] ? 1 : 0) +
202 (nggso->vertices[2] ? 1 : 0);
203 LLVMValueRef prim_stride_dw[4] = {};
204 LLVMValueRef prim_stride_dw_vgpr = LLVMGetUndef(ctx->i32);
205 int stream_for_buffer[4] = { -1, -1, -1, -1 };
206 unsigned bufmask_for_stream[4] = {};
207 bool isgs = ctx->type == PIPE_SHADER_GEOMETRY;
208 unsigned scratch_emit_base = isgs ? 4 : 0;
209 LLVMValueRef scratch_emit_basev = isgs ? i32_4 : ctx->i32_0;
210 unsigned scratch_offset_base = isgs ? 8 : 4;
211 LLVMValueRef scratch_offset_basev = isgs ? i32_8 : i32_4;
212
213 ac_llvm_add_target_dep_function_attr(ctx->main_fn, "amdgpu-gds-size", 256);
214
215 /* Determine the mapping of streamout buffers to vertex streams. */
216 for (unsigned i = 0; i < so->num_outputs; ++i) {
217 unsigned buf = so->output[i].output_buffer;
218 unsigned stream = so->output[i].stream;
219 assert(stream_for_buffer[buf] < 0 || stream_for_buffer[buf] == stream);
220 stream_for_buffer[buf] = stream;
221 bufmask_for_stream[stream] |= 1 << buf;
222 }
223
224 for (unsigned buffer = 0; buffer < 4; ++buffer) {
225 if (stream_for_buffer[buffer] == -1)
226 continue;
227
228 assert(so->stride[buffer]);
229
230 tmp = LLVMConstInt(ctx->i32, so->stride[buffer], false);
231 prim_stride_dw[buffer] = LLVMBuildMul(builder, tmp, nggso->num_vertices, "");
232 prim_stride_dw_vgpr = ac_build_writelane(
233 &ctx->ac, prim_stride_dw_vgpr, prim_stride_dw[buffer],
234 LLVMConstInt(ctx->i32, buffer, false));
235
236 so_buffer[buffer] = ac_build_load_to_sgpr(
237 &ctx->ac, buf_ptr,
238 LLVMConstInt(ctx->i32, SI_VS_STREAMOUT_BUF0 + buffer, false));
239 }
240
241 tmp = LLVMBuildICmp(builder, LLVMIntEQ, get_wave_id_in_tg(ctx), ctx->i32_0, "");
242 ac_build_ifcc(&ctx->ac, tmp, 5200);
243 {
244 LLVMTypeRef gdsptr = LLVMPointerType(ctx->i32, AC_ADDR_SPACE_GDS);
245 LLVMValueRef gdsbase = LLVMBuildIntToPtr(builder, ctx->i32_0, gdsptr, "");
246
247 /* Advance the streamout offsets in GDS. */
248 LLVMValueRef offsets_vgpr = ac_build_alloca_undef(&ctx->ac, ctx->i32, "");
249 LLVMValueRef generated_by_stream_vgpr = ac_build_alloca_undef(&ctx->ac, ctx->i32, "");
250
251 tmp = LLVMBuildICmp(builder, LLVMIntULT, ac_get_thread_id(&ctx->ac), i32_4, "");
252 ac_build_ifcc(&ctx->ac, tmp, 5210);
253 {
254 if (isgs) {
255 tmp = ac_build_gep0(&ctx->ac, ctx->gs_ngg_scratch, tid);
256 tmp = LLVMBuildLoad(builder, tmp, "");
257 } else {
258 tmp = ac_build_writelane(&ctx->ac, ctx->i32_0,
259 ngg_get_prim_cnt(ctx), ctx->i32_0);
260 }
261 LLVMBuildStore(builder, tmp, generated_by_stream_vgpr);
262
263 unsigned swizzle[4];
264 int unused_stream = -1;
265 for (unsigned stream = 0; stream < 4; ++stream) {
266 if (!info->num_stream_output_components[stream]) {
267 unused_stream = stream;
268 break;
269 }
270 }
271 for (unsigned buffer = 0; buffer < 4; ++buffer) {
272 if (stream_for_buffer[buffer] >= 0) {
273 swizzle[buffer] = stream_for_buffer[buffer];
274 } else {
275 assert(unused_stream >= 0);
276 swizzle[buffer] = unused_stream;
277 }
278 }
279
280 tmp = ac_build_quad_swizzle(&ctx->ac, tmp,
281 swizzle[0], swizzle[1], swizzle[2], swizzle[3]);
282 tmp = LLVMBuildMul(builder, tmp, prim_stride_dw_vgpr, "");
283
284 LLVMValueRef args[] = {
285 LLVMBuildIntToPtr(builder, ngg_get_ordered_id(ctx), gdsptr, ""),
286 tmp,
287 ctx->i32_0, // ordering
288 ctx->i32_0, // scope
289 ctx->ac.i1false, // isVolatile
290 LLVMConstInt(ctx->i32, 4 << 24, false), // OA index
291 ctx->ac.i1true, // wave release
292 ctx->ac.i1true, // wave done
293 };
294 tmp = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.ds.ordered.add",
295 ctx->i32, args, ARRAY_SIZE(args), 0);
296
297 /* Keep offsets in a VGPR for quick retrieval via readlane by
298 * the first wave for bounds checking, and also store in LDS
299 * for retrieval by all waves later. */
300 LLVMBuildStore(builder, tmp, offsets_vgpr);
301
302 tmp2 = LLVMBuildAdd(builder, ac_get_thread_id(&ctx->ac),
303 scratch_offset_basev, "");
304 tmp2 = ac_build_gep0(&ctx->ac, ctx->gs_ngg_scratch, tmp2);
305 LLVMBuildStore(builder, tmp, tmp2);
306 }
307 ac_build_endif(&ctx->ac, 5210);
308
309 /* Determine the max emit per buffer. This is done via the SALU, in part
310 * because LLVM can't generate divide-by-multiply if we try to do this
311 * via VALU with one lane per buffer.
312 */
313 LLVMValueRef max_emit[4] = {};
314 for (unsigned buffer = 0; buffer < 4; ++buffer) {
315 if (stream_for_buffer[buffer] == -1)
316 continue;
317
318 LLVMValueRef bufsize_dw =
319 LLVMBuildLShr(builder,
320 LLVMBuildExtractElement(builder, so_buffer[buffer], i32_2, ""),
321 i32_2, "");
322
323 tmp = LLVMBuildLoad(builder, offsets_vgpr, "");
324 LLVMValueRef offset_dw =
325 ac_build_readlane(&ctx->ac, tmp,
326 LLVMConstInt(ctx->i32, buffer, false));
327
328 tmp = LLVMBuildSub(builder, bufsize_dw, offset_dw, "");
329 tmp = LLVMBuildUDiv(builder, tmp, prim_stride_dw[buffer], "");
330
331 tmp2 = LLVMBuildICmp(builder, LLVMIntULT, bufsize_dw, offset_dw, "");
332 max_emit[buffer] = LLVMBuildSelect(builder, tmp2, ctx->i32_0, tmp, "");
333 }
334
335 /* Determine the number of emitted primitives per stream and fixup the
336 * GDS counter if necessary.
337 *
338 * This is complicated by the fact that a single stream can emit to
339 * multiple buffers (but luckily not vice versa).
340 */
341 LLVMValueRef emit_vgpr = ctx->i32_0;
342
343 for (unsigned stream = 0; stream < 4; ++stream) {
344 if (!info->num_stream_output_components[stream])
345 continue;
346
347 tmp = LLVMBuildLoad(builder, generated_by_stream_vgpr, "");
348 LLVMValueRef generated =
349 ac_build_readlane(&ctx->ac, tmp,
350 LLVMConstInt(ctx->i32, stream, false));
351
352 LLVMValueRef emit = generated;
353 for (unsigned buffer = 0; buffer < 4; ++buffer) {
354 if (stream_for_buffer[buffer] == stream)
355 emit = ac_build_umin(&ctx->ac, emit, max_emit[buffer]);
356 }
357
358 emit_vgpr = ac_build_writelane(&ctx->ac, emit_vgpr, emit,
359 LLVMConstInt(ctx->i32, stream, false));
360
361 /* Fixup the offset using a plain GDS atomic if we overflowed. */
362 tmp = LLVMBuildICmp(builder, LLVMIntULT, emit, generated, "");
363 ac_build_ifcc(&ctx->ac, tmp, 5221); /* scalar branch */
364 tmp = LLVMBuildLShr(builder,
365 LLVMConstInt(ctx->i32, bufmask_for_stream[stream], false),
366 ac_get_thread_id(&ctx->ac), "");
367 tmp = LLVMBuildTrunc(builder, tmp, ctx->i1, "");
368 ac_build_ifcc(&ctx->ac, tmp, 5222);
369 {
370 tmp = LLVMBuildSub(builder, generated, emit, "");
371 tmp = LLVMBuildMul(builder, tmp, prim_stride_dw_vgpr, "");
372 tmp2 = LLVMBuildGEP(builder, gdsbase, &tid, 1, "");
373 LLVMBuildAtomicRMW(builder, LLVMAtomicRMWBinOpSub, tmp2, tmp,
374 LLVMAtomicOrderingMonotonic, false);
375 }
376 ac_build_endif(&ctx->ac, 5222);
377 ac_build_endif(&ctx->ac, 5221);
378 }
379
380 tmp = LLVMBuildICmp(builder, LLVMIntULT, ac_get_thread_id(&ctx->ac), i32_4, "");
381 ac_build_ifcc(&ctx->ac, tmp, 5225);
382 {
383 tmp = LLVMBuildAdd(builder, ac_get_thread_id(&ctx->ac),
384 scratch_emit_basev, "");
385 tmp = ac_build_gep0(&ctx->ac, ctx->gs_ngg_scratch, tmp);
386 LLVMBuildStore(builder, emit_vgpr, tmp);
387 }
388 ac_build_endif(&ctx->ac, 5225);
389 }
390 ac_build_endif(&ctx->ac, 5200);
391
392 /* Determine the workgroup-relative per-thread / primitive offset into
393 * the streamout buffers */
394 struct ac_wg_scan primemit_scan[4] = {};
395
396 if (isgs) {
397 for (unsigned stream = 0; stream < 4; ++stream) {
398 if (!info->num_stream_output_components[stream])
399 continue;
400
401 primemit_scan[stream].enable_exclusive = true;
402 primemit_scan[stream].op = nir_op_iadd;
403 primemit_scan[stream].src = nggso->prim_enable[stream];
404 primemit_scan[stream].scratch =
405 ac_build_gep0(&ctx->ac, ctx->gs_ngg_scratch,
406 LLVMConstInt(ctx->i32, 12 + 8 * stream, false));
407 primemit_scan[stream].waveidx = get_wave_id_in_tg(ctx);
408 primemit_scan[stream].numwaves = get_tgsize(ctx);
409 primemit_scan[stream].maxwaves = 8;
410 ac_build_wg_scan_top(&ctx->ac, &primemit_scan[stream]);
411 }
412 }
413
414 ac_build_s_barrier(&ctx->ac);
415
416 /* Fetch the per-buffer offsets and per-stream emit counts in all waves. */
417 LLVMValueRef wgoffset_dw[4] = {};
418
419 {
420 LLVMValueRef scratch_vgpr;
421
422 tmp = ac_build_gep0(&ctx->ac, ctx->gs_ngg_scratch, ac_get_thread_id(&ctx->ac));
423 scratch_vgpr = LLVMBuildLoad(builder, tmp, "");
424
425 for (unsigned buffer = 0; buffer < 4; ++buffer) {
426 if (stream_for_buffer[buffer] >= 0) {
427 wgoffset_dw[buffer] = ac_build_readlane(
428 &ctx->ac, scratch_vgpr,
429 LLVMConstInt(ctx->i32, scratch_offset_base + buffer, false));
430 }
431 }
432
433 for (unsigned stream = 0; stream < 4; ++stream) {
434 if (info->num_stream_output_components[stream]) {
435 nggso->emit[stream] = ac_build_readlane(
436 &ctx->ac, scratch_vgpr,
437 LLVMConstInt(ctx->i32, scratch_emit_base + stream, false));
438 }
439 }
440 }
441
442 /* Write out primitive data */
443 for (unsigned stream = 0; stream < 4; ++stream) {
444 if (!info->num_stream_output_components[stream])
445 continue;
446
447 if (isgs) {
448 ac_build_wg_scan_bottom(&ctx->ac, &primemit_scan[stream]);
449 } else {
450 primemit_scan[stream].result_exclusive = tid;
451 }
452
453 tmp = LLVMBuildICmp(builder, LLVMIntULT,
454 primemit_scan[stream].result_exclusive,
455 nggso->emit[stream], "");
456 tmp = LLVMBuildAnd(builder, tmp, nggso->prim_enable[stream], "");
457 ac_build_ifcc(&ctx->ac, tmp, 5240);
458 {
459 LLVMValueRef offset_vtx =
460 LLVMBuildMul(builder, primemit_scan[stream].result_exclusive,
461 nggso->num_vertices, "");
462
463 for (unsigned i = 0; i < max_num_vertices; ++i) {
464 tmp = LLVMBuildICmp(builder, LLVMIntULT,
465 LLVMConstInt(ctx->i32, i, false),
466 nggso->num_vertices, "");
467 ac_build_ifcc(&ctx->ac, tmp, 5241);
468 build_streamout_vertex(ctx, so_buffer, wgoffset_dw,
469 stream, offset_vtx, nggso->vertices[i]);
470 ac_build_endif(&ctx->ac, 5241);
471 offset_vtx = LLVMBuildAdd(builder, offset_vtx, ctx->i32_1, "");
472 }
473 }
474 ac_build_endif(&ctx->ac, 5240);
475 }
476 }
477
478 static unsigned ngg_nogs_vertex_size(struct si_shader *shader)
479 {
480 unsigned lds_vertex_size = 0;
481
482 /* The edgeflag is always stored in the last element that's also
483 * used for padding to reduce LDS bank conflicts. */
484 if (shader->selector->so.num_outputs)
485 lds_vertex_size = 4 * shader->selector->info.num_outputs + 1;
486 if (shader->selector->info.writes_edgeflag)
487 lds_vertex_size = MAX2(lds_vertex_size, 1);
488
489 return lds_vertex_size;
490 }
491
492 /**
493 * Returns an `[N x i32] addrspace(LDS)*` pointing at contiguous LDS storage
494 * for the vertex outputs.
495 */
496 static LLVMValueRef ngg_nogs_vertex_ptr(struct si_shader_context *ctx,
497 LLVMValueRef vtxid)
498 {
499 /* The extra dword is used to avoid LDS bank conflicts. */
500 unsigned vertex_size = ngg_nogs_vertex_size(ctx->shader);
501 LLVMTypeRef ai32 = LLVMArrayType(ctx->i32, vertex_size);
502 LLVMTypeRef pai32 = LLVMPointerType(ai32, AC_ADDR_SPACE_LDS);
503 LLVMValueRef tmp = LLVMBuildBitCast(ctx->ac.builder, ctx->esgs_ring, pai32, "");
504 return LLVMBuildGEP(ctx->ac.builder, tmp, &vtxid, 1, "");
505 }
506
507 /**
508 * Emit the epilogue of an API VS or TES shader compiled as ESGS shader.
509 */
510 void gfx10_emit_ngg_epilogue(struct ac_shader_abi *abi,
511 unsigned max_outputs,
512 LLVMValueRef *addrs)
513 {
514 struct si_shader_context *ctx = si_shader_context_from_abi(abi);
515 struct si_shader_selector *sel = ctx->shader->selector;
516 struct si_shader_info *info = &sel->info;
517 struct si_shader_output_values outputs[PIPE_MAX_SHADER_OUTPUTS];
518 LLVMBuilderRef builder = ctx->ac.builder;
519 LLVMValueRef tmp, tmp2;
520
521 assert(!ctx->shader->is_gs_copy_shader);
522 assert(info->num_outputs <= max_outputs);
523
524 LLVMValueRef vertex_ptr = NULL;
525
526 if (sel->so.num_outputs || sel->info.writes_edgeflag)
527 vertex_ptr = ngg_nogs_vertex_ptr(ctx, get_thread_id_in_tg(ctx));
528
529 for (unsigned i = 0; i < info->num_outputs; i++) {
530 outputs[i].semantic_name = info->output_semantic_name[i];
531 outputs[i].semantic_index = info->output_semantic_index[i];
532
533 for (unsigned j = 0; j < 4; j++) {
534 outputs[i].vertex_stream[j] =
535 (info->output_streams[i] >> (2 * j)) & 3;
536
537 /* TODO: we may store more outputs than streamout needs,
538 * but streamout performance isn't that important.
539 */
540 if (sel->so.num_outputs) {
541 tmp = ac_build_gep0(&ctx->ac, vertex_ptr,
542 LLVMConstInt(ctx->i32, 4 * i + j, false));
543 tmp2 = LLVMBuildLoad(builder, addrs[4 * i + j], "");
544 tmp2 = ac_to_integer(&ctx->ac, tmp2);
545 LLVMBuildStore(builder, tmp2, tmp);
546 }
547 }
548
549 /* Store the edgeflag at the end (if streamout is enabled) */
550 if (info->output_semantic_name[i] == TGSI_SEMANTIC_EDGEFLAG &&
551 sel->info.writes_edgeflag) {
552 LLVMValueRef edgeflag = LLVMBuildLoad(builder, addrs[4 * i], "");
553 /* The output is a float, but the hw expects a 1-bit integer. */
554 edgeflag = LLVMBuildFPToUI(ctx->ac.builder, edgeflag, ctx->i32, "");
555 edgeflag = ac_build_umin(&ctx->ac, edgeflag, ctx->i32_1);
556
557 tmp = LLVMConstInt(ctx->i32, ngg_nogs_vertex_size(ctx->shader) - 1, 0);
558 tmp = ac_build_gep0(&ctx->ac, vertex_ptr, tmp);
559 LLVMBuildStore(builder, edgeflag, tmp);
560 }
561 }
562
563 ac_build_endif(&ctx->ac, ctx->merged_wrap_if_label);
564
565 LLVMValueRef is_gs_thread = si_is_gs_thread(ctx);
566 LLVMValueRef is_es_thread = si_is_es_thread(ctx);
567 LLVMValueRef vtxindex[] = {
568 si_unpack_param(ctx, ctx->gs_vtx01_offset, 0, 16),
569 si_unpack_param(ctx, ctx->gs_vtx01_offset, 16, 16),
570 si_unpack_param(ctx, ctx->gs_vtx23_offset, 0, 16),
571 };
572
573 /* Determine the number of vertices per primitive. */
574 unsigned num_vertices;
575 LLVMValueRef num_vertices_val = ngg_get_vertices_per_prim(ctx, &num_vertices);
576
577 /* Streamout */
578 LLVMValueRef emitted_prims = NULL;
579
580 if (sel->so.num_outputs) {
581 struct ngg_streamout nggso = {};
582
583 nggso.num_vertices = num_vertices_val;
584 nggso.prim_enable[0] = is_gs_thread;
585
586 for (unsigned i = 0; i < num_vertices; ++i)
587 nggso.vertices[i] = ngg_nogs_vertex_ptr(ctx, vtxindex[i]);
588
589 build_streamout(ctx, &nggso);
590 emitted_prims = nggso.emit[0];
591 }
592
593 LLVMValueRef user_edgeflags[3] = {};
594
595 if (sel->info.writes_edgeflag) {
596 /* Streamout already inserted the barrier, so don't insert it again. */
597 if (!sel->so.num_outputs)
598 ac_build_s_barrier(&ctx->ac);
599
600 ac_build_ifcc(&ctx->ac, is_gs_thread, 5400);
601 /* Load edge flags from ES threads and store them into VGPRs in GS threads. */
602 for (unsigned i = 0; i < num_vertices; i++) {
603 tmp = ngg_nogs_vertex_ptr(ctx, vtxindex[i]);
604 tmp2 = LLVMConstInt(ctx->i32, ngg_nogs_vertex_size(ctx->shader) - 1, 0);
605 tmp = ac_build_gep0(&ctx->ac, tmp, tmp2);
606 tmp = LLVMBuildLoad(builder, tmp, "");
607 tmp = LLVMBuildTrunc(builder, tmp, ctx->i1, "");
608
609 user_edgeflags[i] = ac_build_alloca_undef(&ctx->ac, ctx->i1, "");
610 LLVMBuildStore(builder, tmp, user_edgeflags[i]);
611 }
612 ac_build_endif(&ctx->ac, 5400);
613 }
614
615 /* Copy Primitive IDs from GS threads to the LDS address corresponding
616 * to the ES thread of the provoking vertex.
617 */
618 if (ctx->type == PIPE_SHADER_VERTEX &&
619 ctx->shader->key.mono.u.vs_export_prim_id) {
620 /* Streamout and edge flags use LDS. Make it idle, so that we can reuse it. */
621 if (sel->so.num_outputs || sel->info.writes_edgeflag)
622 ac_build_s_barrier(&ctx->ac);
623
624 ac_build_ifcc(&ctx->ac, is_gs_thread, 5400);
625 /* Extract the PROVOKING_VTX_INDEX field. */
626 LLVMValueRef provoking_vtx_in_prim =
627 si_unpack_param(ctx, ctx->vs_state_bits, 4, 2);
628
629 /* provoking_vtx_index = vtxindex[provoking_vtx_in_prim]; */
630 LLVMValueRef indices = ac_build_gather_values(&ctx->ac, vtxindex, 3);
631 LLVMValueRef provoking_vtx_index =
632 LLVMBuildExtractElement(builder, indices, provoking_vtx_in_prim, "");
633
634 LLVMBuildStore(builder, ac_get_arg(&ctx->ac, ctx->args.gs_prim_id),
635 ac_build_gep0(&ctx->ac, ctx->esgs_ring, provoking_vtx_index));
636 ac_build_endif(&ctx->ac, 5400);
637 }
638
639 ac_build_sendmsg_gs_alloc_req(&ctx->ac, get_wave_id_in_tg(ctx),
640 ngg_get_vtx_cnt(ctx), ngg_get_prim_cnt(ctx));
641
642 /* Update query buffer */
643 if (ctx->screen->use_ngg_streamout &&
644 !info->properties[TGSI_PROPERTY_VS_BLIT_SGPRS_AMD]) {
645 tmp = si_unpack_param(ctx, ctx->vs_state_bits, 6, 1);
646 tmp = LLVMBuildTrunc(builder, tmp, ctx->i1, "");
647 ac_build_ifcc(&ctx->ac, tmp, 5029); /* if (STREAMOUT_QUERY_ENABLED) */
648 tmp = LLVMBuildICmp(builder, LLVMIntEQ, get_wave_id_in_tg(ctx), ctx->ac.i32_0, "");
649 ac_build_ifcc(&ctx->ac, tmp, 5030);
650 tmp = LLVMBuildICmp(builder, LLVMIntULE, ac_get_thread_id(&ctx->ac),
651 sel->so.num_outputs ? ctx->ac.i32_1 : ctx->ac.i32_0, "");
652 ac_build_ifcc(&ctx->ac, tmp, 5031);
653 {
654 LLVMValueRef args[] = {
655 ngg_get_prim_cnt(ctx),
656 ngg_get_query_buf(ctx),
657 LLVMConstInt(ctx->i32, 16, false), /* offset of stream[0].generated_primitives */
658 ctx->i32_0, /* soffset */
659 ctx->i32_0, /* cachepolicy */
660 };
661
662 if (sel->so.num_outputs) {
663 args[0] = ac_build_writelane(&ctx->ac, args[0], emitted_prims, ctx->i32_1);
664 args[2] = ac_build_writelane(&ctx->ac, args[2],
665 LLVMConstInt(ctx->i32, 24, false), ctx->i32_1);
666 }
667
668 /* TODO: should this be 64-bit atomics? */
669 ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.raw.buffer.atomic.add.i32",
670 ctx->i32, args, 5, 0);
671 }
672 ac_build_endif(&ctx->ac, 5031);
673 ac_build_endif(&ctx->ac, 5030);
674 ac_build_endif(&ctx->ac, 5029);
675 }
676
677 /* Build the primitive export. */
678 ac_build_ifcc(&ctx->ac, is_gs_thread, 6001);
679 {
680 struct ac_ngg_prim prim = {};
681
682 if (gfx10_is_ngg_passthrough(ctx->shader)) {
683 prim.passthrough = ac_get_arg(&ctx->ac, ctx->gs_vtx01_offset);
684 } else {
685 prim.num_vertices = num_vertices;
686 prim.isnull = ctx->ac.i1false;
687 memcpy(prim.index, vtxindex, sizeof(vtxindex[0]) * 3);
688
689 for (unsigned i = 0; i < num_vertices; ++i) {
690 prim.edgeflag[i] = ngg_get_initial_edgeflag(ctx, i);
691
692 if (sel->info.writes_edgeflag) {
693 tmp2 = LLVMBuildLoad(builder, user_edgeflags[i], "");
694 prim.edgeflag[i] = LLVMBuildAnd(builder, prim.edgeflag[i],
695 tmp2, "");
696 }
697 }
698 }
699
700 ac_build_export_prim(&ctx->ac, &prim);
701 }
702 ac_build_endif(&ctx->ac, 6001);
703
704 /* Export per-vertex data (positions and parameters). */
705 ac_build_ifcc(&ctx->ac, is_es_thread, 6002);
706 {
707 unsigned i;
708
709 /* Unconditionally (re-)load the values for proper SSA form. */
710 for (i = 0; i < info->num_outputs; i++) {
711 for (unsigned j = 0; j < 4; j++) {
712 outputs[i].values[j] =
713 LLVMBuildLoad(builder,
714 addrs[4 * i + j],
715 "");
716 }
717 }
718
719 if (ctx->shader->key.mono.u.vs_export_prim_id) {
720 outputs[i].semantic_name = TGSI_SEMANTIC_PRIMID;
721 outputs[i].semantic_index = 0;
722
723 if (ctx->type == PIPE_SHADER_VERTEX) {
724 /* Wait for GS stores to finish. */
725 ac_build_s_barrier(&ctx->ac);
726
727 tmp = ac_build_gep0(&ctx->ac, ctx->esgs_ring,
728 get_thread_id_in_tg(ctx));
729 outputs[i].values[0] = LLVMBuildLoad(builder, tmp, "");
730 } else {
731 assert(ctx->type == PIPE_SHADER_TESS_EVAL);
732 outputs[i].values[0] = si_get_primitive_id(ctx, 0);
733 }
734
735 outputs[i].values[0] = ac_to_float(&ctx->ac, outputs[i].values[0]);
736 for (unsigned j = 1; j < 4; j++)
737 outputs[i].values[j] = LLVMGetUndef(ctx->f32);
738
739 memset(outputs[i].vertex_stream, 0,
740 sizeof(outputs[i].vertex_stream));
741 i++;
742 }
743
744 si_llvm_export_vs(ctx, outputs, i);
745 }
746 ac_build_endif(&ctx->ac, 6002);
747 }
748
749 static LLVMValueRef
750 ngg_gs_get_vertex_storage(struct si_shader_context *ctx)
751 {
752 const struct si_shader_selector *sel = ctx->shader->selector;
753 const struct si_shader_info *info = &sel->info;
754
755 LLVMTypeRef elements[2] = {
756 LLVMArrayType(ctx->ac.i32, 4 * info->num_outputs),
757 LLVMArrayType(ctx->ac.i8, 4),
758 };
759 LLVMTypeRef type = LLVMStructTypeInContext(ctx->ac.context, elements, 2, false);
760 type = LLVMPointerType(LLVMArrayType(type, 0), AC_ADDR_SPACE_LDS);
761 return LLVMBuildBitCast(ctx->ac.builder, ctx->gs_ngg_emit, type, "");
762 }
763
764 /**
765 * Return a pointer to the LDS storage reserved for the N'th vertex, where N
766 * is in emit order; that is:
767 * - during the epilogue, N is the threadidx (relative to the entire threadgroup)
768 * - during vertex emit, i.e. while the API GS shader invocation is running,
769 * N = threadidx * gs_max_out_vertices + emitidx
770 *
771 * Goals of the LDS memory layout:
772 * 1. Eliminate bank conflicts on write for geometry shaders that have all emits
773 * in uniform control flow
774 * 2. Eliminate bank conflicts on read for export if, additionally, there is no
775 * culling
776 * 3. Agnostic to the number of waves (since we don't know it before compiling)
777 * 4. Allow coalescing of LDS instructions (ds_write_b128 etc.)
778 * 5. Avoid wasting memory.
779 *
780 * We use an AoS layout due to point 4 (this also helps point 3). In an AoS
781 * layout, elimination of bank conflicts requires that each vertex occupy an
782 * odd number of dwords. We use the additional dword to store the output stream
783 * index as well as a flag to indicate whether this vertex ends a primitive
784 * for rasterization.
785 *
786 * Swizzling is required to satisfy points 1 and 2 simultaneously.
787 *
788 * Vertices are stored in export order (gsthread * gs_max_out_vertices + emitidx).
789 * Indices are swizzled in groups of 32, which ensures point 1 without
790 * disturbing point 2.
791 *
792 * \return an LDS pointer to type {[N x i32], [4 x i8]}
793 */
794 static LLVMValueRef
795 ngg_gs_vertex_ptr(struct si_shader_context *ctx, LLVMValueRef vertexidx)
796 {
797 struct si_shader_selector *sel = ctx->shader->selector;
798 LLVMBuilderRef builder = ctx->ac.builder;
799 LLVMValueRef storage = ngg_gs_get_vertex_storage(ctx);
800
801 /* gs_max_out_vertices = 2^(write_stride_2exp) * some odd number */
802 unsigned write_stride_2exp = ffs(sel->gs_max_out_vertices) - 1;
803 if (write_stride_2exp) {
804 LLVMValueRef row =
805 LLVMBuildLShr(builder, vertexidx,
806 LLVMConstInt(ctx->ac.i32, 5, false), "");
807 LLVMValueRef swizzle =
808 LLVMBuildAnd(builder, row,
809 LLVMConstInt(ctx->ac.i32, (1u << write_stride_2exp) - 1,
810 false), "");
811 vertexidx = LLVMBuildXor(builder, vertexidx, swizzle, "");
812 }
813
814 return ac_build_gep0(&ctx->ac, storage, vertexidx);
815 }
816
817 static LLVMValueRef
818 ngg_gs_emit_vertex_ptr(struct si_shader_context *ctx, LLVMValueRef gsthread,
819 LLVMValueRef emitidx)
820 {
821 struct si_shader_selector *sel = ctx->shader->selector;
822 LLVMBuilderRef builder = ctx->ac.builder;
823 LLVMValueRef tmp;
824
825 tmp = LLVMConstInt(ctx->ac.i32, sel->gs_max_out_vertices, false);
826 tmp = LLVMBuildMul(builder, tmp, gsthread, "");
827 const LLVMValueRef vertexidx = LLVMBuildAdd(builder, tmp, emitidx, "");
828 return ngg_gs_vertex_ptr(ctx, vertexidx);
829 }
830
831 static LLVMValueRef
832 ngg_gs_get_emit_output_ptr(struct si_shader_context *ctx, LLVMValueRef vertexptr,
833 unsigned out_idx)
834 {
835 LLVMValueRef gep_idx[3] = {
836 ctx->ac.i32_0, /* implied C-style array */
837 ctx->ac.i32_0, /* first struct entry */
838 LLVMConstInt(ctx->ac.i32, out_idx, false),
839 };
840 return LLVMBuildGEP(ctx->ac.builder, vertexptr, gep_idx, 3, "");
841 }
842
843 static LLVMValueRef
844 ngg_gs_get_emit_primflag_ptr(struct si_shader_context *ctx, LLVMValueRef vertexptr,
845 unsigned stream)
846 {
847 LLVMValueRef gep_idx[3] = {
848 ctx->ac.i32_0, /* implied C-style array */
849 ctx->ac.i32_1, /* second struct entry */
850 LLVMConstInt(ctx->ac.i32, stream, false),
851 };
852 return LLVMBuildGEP(ctx->ac.builder, vertexptr, gep_idx, 3, "");
853 }
854
855 void gfx10_ngg_gs_emit_vertex(struct si_shader_context *ctx,
856 unsigned stream,
857 LLVMValueRef *addrs)
858 {
859 const struct si_shader_selector *sel = ctx->shader->selector;
860 const struct si_shader_info *info = &sel->info;
861 LLVMBuilderRef builder = ctx->ac.builder;
862 LLVMValueRef tmp;
863 const LLVMValueRef vertexidx =
864 LLVMBuildLoad(builder, ctx->gs_next_vertex[stream], "");
865
866 /* If this thread has already emitted the declared maximum number of
867 * vertices, skip the write: excessive vertex emissions are not
868 * supposed to have any effect.
869 */
870 const LLVMValueRef can_emit =
871 LLVMBuildICmp(builder, LLVMIntULT, vertexidx,
872 LLVMConstInt(ctx->i32, sel->gs_max_out_vertices, false), "");
873
874 tmp = LLVMBuildAdd(builder, vertexidx, ctx->ac.i32_1, "");
875 tmp = LLVMBuildSelect(builder, can_emit, tmp, vertexidx, "");
876 LLVMBuildStore(builder, tmp, ctx->gs_next_vertex[stream]);
877
878 ac_build_ifcc(&ctx->ac, can_emit, 9001);
879
880 const LLVMValueRef vertexptr =
881 ngg_gs_emit_vertex_ptr(ctx, get_thread_id_in_tg(ctx), vertexidx);
882 unsigned out_idx = 0;
883 for (unsigned i = 0; i < info->num_outputs; i++) {
884 for (unsigned chan = 0; chan < 4; chan++, out_idx++) {
885 if (!(info->output_usagemask[i] & (1 << chan)) ||
886 ((info->output_streams[i] >> (2 * chan)) & 3) != stream)
887 continue;
888
889 LLVMValueRef out_val = LLVMBuildLoad(builder, addrs[4 * i + chan], "");
890 out_val = ac_to_integer(&ctx->ac, out_val);
891 LLVMBuildStore(builder, out_val,
892 ngg_gs_get_emit_output_ptr(ctx, vertexptr, out_idx));
893 }
894 }
895 assert(out_idx * 4 == sel->gsvs_vertex_size);
896
897 /* Determine and store whether this vertex completed a primitive. */
898 const LLVMValueRef curverts = LLVMBuildLoad(builder, ctx->gs_curprim_verts[stream], "");
899
900 tmp = LLVMConstInt(ctx->ac.i32, u_vertices_per_prim(sel->gs_output_prim) - 1, false);
901 const LLVMValueRef iscompleteprim =
902 LLVMBuildICmp(builder, LLVMIntUGE, curverts, tmp, "");
903
904 /* Since the geometry shader emits triangle strips, we need to
905 * track which primitive is odd and swap vertex indices to get
906 * the correct vertex order.
907 */
908 LLVMValueRef is_odd = ctx->i1false;
909 if (stream == 0 && u_vertices_per_prim(sel->gs_output_prim) == 3) {
910 tmp = LLVMBuildAnd(builder, curverts, ctx->i32_1, "");
911 is_odd = LLVMBuildICmp(builder, LLVMIntEQ, tmp, ctx->i32_1, "");
912 }
913
914 tmp = LLVMBuildAdd(builder, curverts, ctx->ac.i32_1, "");
915 LLVMBuildStore(builder, tmp, ctx->gs_curprim_verts[stream]);
916
917 /* The per-vertex primitive flag encoding:
918 * bit 0: whether this vertex finishes a primitive
919 * bit 1: whether the primitive is odd (if we are emitting triangle strips)
920 */
921 tmp = LLVMBuildZExt(builder, iscompleteprim, ctx->ac.i8, "");
922 tmp = LLVMBuildOr(builder, tmp,
923 LLVMBuildShl(builder,
924 LLVMBuildZExt(builder, is_odd, ctx->ac.i8, ""),
925 ctx->ac.i8_1, ""), "");
926 LLVMBuildStore(builder, tmp, ngg_gs_get_emit_primflag_ptr(ctx, vertexptr, stream));
927
928 tmp = LLVMBuildLoad(builder, ctx->gs_generated_prims[stream], "");
929 tmp = LLVMBuildAdd(builder, tmp, LLVMBuildZExt(builder, iscompleteprim, ctx->ac.i32, ""), "");
930 LLVMBuildStore(builder, tmp, ctx->gs_generated_prims[stream]);
931
932 ac_build_endif(&ctx->ac, 9001);
933 }
934
935 void gfx10_ngg_gs_emit_prologue(struct si_shader_context *ctx)
936 {
937 /* Zero out the part of LDS scratch that is used to accumulate the
938 * per-stream generated primitive count.
939 */
940 LLVMBuilderRef builder = ctx->ac.builder;
941 LLVMValueRef scratchptr = ctx->gs_ngg_scratch;
942 LLVMValueRef tid = get_thread_id_in_tg(ctx);
943 LLVMValueRef tmp;
944
945 tmp = LLVMBuildICmp(builder, LLVMIntULT, tid, LLVMConstInt(ctx->i32, 4, false), "");
946 ac_build_ifcc(&ctx->ac, tmp, 5090);
947 {
948 LLVMValueRef ptr = ac_build_gep0(&ctx->ac, scratchptr, tid);
949 LLVMBuildStore(builder, ctx->i32_0, ptr);
950 }
951 ac_build_endif(&ctx->ac, 5090);
952
953 ac_build_s_barrier(&ctx->ac);
954 }
955
956 void gfx10_ngg_gs_emit_epilogue(struct si_shader_context *ctx)
957 {
958 const struct si_shader_selector *sel = ctx->shader->selector;
959 const struct si_shader_info *info = &sel->info;
960 const unsigned verts_per_prim = u_vertices_per_prim(sel->gs_output_prim);
961 LLVMBuilderRef builder = ctx->ac.builder;
962 LLVMValueRef i8_0 = LLVMConstInt(ctx->ac.i8, 0, false);
963 LLVMValueRef tmp, tmp2;
964
965 /* Zero out remaining (non-emitted) primitive flags.
966 *
967 * Note: Alternatively, we could pass the relevant gs_next_vertex to
968 * the emit threads via LDS. This is likely worse in the expected
969 * typical case where each GS thread emits the full set of
970 * vertices.
971 */
972 for (unsigned stream = 0; stream < 4; ++stream) {
973 if (!info->num_stream_output_components[stream])
974 continue;
975
976 const LLVMValueRef gsthread = get_thread_id_in_tg(ctx);
977
978 ac_build_bgnloop(&ctx->ac, 5100);
979
980 const LLVMValueRef vertexidx =
981 LLVMBuildLoad(builder, ctx->gs_next_vertex[stream], "");
982 tmp = LLVMBuildICmp(builder, LLVMIntUGE, vertexidx,
983 LLVMConstInt(ctx->ac.i32, sel->gs_max_out_vertices, false), "");
984 ac_build_ifcc(&ctx->ac, tmp, 5101);
985 ac_build_break(&ctx->ac);
986 ac_build_endif(&ctx->ac, 5101);
987
988 tmp = LLVMBuildAdd(builder, vertexidx, ctx->ac.i32_1, "");
989 LLVMBuildStore(builder, tmp, ctx->gs_next_vertex[stream]);
990
991 tmp = ngg_gs_emit_vertex_ptr(ctx, gsthread, vertexidx);
992 LLVMBuildStore(builder, i8_0, ngg_gs_get_emit_primflag_ptr(ctx, tmp, stream));
993
994 ac_build_endloop(&ctx->ac, 5100);
995 }
996
997 /* Accumulate generated primitives counts across the entire threadgroup. */
998 for (unsigned stream = 0; stream < 4; ++stream) {
999 if (!info->num_stream_output_components[stream])
1000 continue;
1001
1002 LLVMValueRef numprims =
1003 LLVMBuildLoad(builder, ctx->gs_generated_prims[stream], "");
1004 numprims = ac_build_reduce(&ctx->ac, numprims, nir_op_iadd, ctx->ac.wave_size);
1005
1006 tmp = LLVMBuildICmp(builder, LLVMIntEQ, ac_get_thread_id(&ctx->ac), ctx->i32_0, "");
1007 ac_build_ifcc(&ctx->ac, tmp, 5105);
1008 {
1009 LLVMBuildAtomicRMW(builder, LLVMAtomicRMWBinOpAdd,
1010 ac_build_gep0(&ctx->ac, ctx->gs_ngg_scratch,
1011 LLVMConstInt(ctx->i32, stream, false)),
1012 numprims, LLVMAtomicOrderingMonotonic, false);
1013 }
1014 ac_build_endif(&ctx->ac, 5105);
1015 }
1016
1017 ac_build_endif(&ctx->ac, ctx->merged_wrap_if_label);
1018
1019 ac_build_s_barrier(&ctx->ac);
1020
1021 const LLVMValueRef tid = get_thread_id_in_tg(ctx);
1022 LLVMValueRef num_emit_threads = ngg_get_prim_cnt(ctx);
1023
1024 /* Streamout */
1025 if (sel->so.num_outputs) {
1026 struct ngg_streamout nggso = {};
1027
1028 nggso.num_vertices = LLVMConstInt(ctx->i32, verts_per_prim, false);
1029
1030 LLVMValueRef vertexptr = ngg_gs_vertex_ptr(ctx, tid);
1031 for (unsigned stream = 0; stream < 4; ++stream) {
1032 if (!info->num_stream_output_components[stream])
1033 continue;
1034
1035 tmp = LLVMBuildLoad(builder, ngg_gs_get_emit_primflag_ptr(ctx, vertexptr, stream), "");
1036 tmp = LLVMBuildTrunc(builder, tmp, ctx->i1, "");
1037 tmp2 = LLVMBuildICmp(builder, LLVMIntULT, tid, num_emit_threads, "");
1038 nggso.prim_enable[stream] = LLVMBuildAnd(builder, tmp, tmp2, "");
1039 }
1040
1041 for (unsigned i = 0; i < verts_per_prim; ++i) {
1042 tmp = LLVMBuildSub(builder, tid,
1043 LLVMConstInt(ctx->i32, verts_per_prim - i - 1, false), "");
1044 tmp = ngg_gs_vertex_ptr(ctx, tmp);
1045 nggso.vertices[i] = ac_build_gep0(&ctx->ac, tmp, ctx->i32_0);
1046 }
1047
1048 build_streamout(ctx, &nggso);
1049 }
1050
1051 /* Write shader query data. */
1052 if (ctx->screen->use_ngg_streamout) {
1053 tmp = si_unpack_param(ctx, ctx->vs_state_bits, 6, 1);
1054 tmp = LLVMBuildTrunc(builder, tmp, ctx->i1, "");
1055 ac_build_ifcc(&ctx->ac, tmp, 5109); /* if (STREAMOUT_QUERY_ENABLED) */
1056 unsigned num_query_comps = sel->so.num_outputs ? 8 : 4;
1057 tmp = LLVMBuildICmp(builder, LLVMIntULT, tid,
1058 LLVMConstInt(ctx->i32, num_query_comps, false), "");
1059 ac_build_ifcc(&ctx->ac, tmp, 5110);
1060 {
1061 LLVMValueRef offset;
1062 tmp = tid;
1063 if (sel->so.num_outputs)
1064 tmp = LLVMBuildAnd(builder, tmp, LLVMConstInt(ctx->i32, 3, false), "");
1065 offset = LLVMBuildNUWMul(builder, tmp, LLVMConstInt(ctx->i32, 32, false), "");
1066 if (sel->so.num_outputs) {
1067 tmp = LLVMBuildLShr(builder, tid, LLVMConstInt(ctx->i32, 2, false), "");
1068 tmp = LLVMBuildNUWMul(builder, tmp, LLVMConstInt(ctx->i32, 8, false), "");
1069 offset = LLVMBuildAdd(builder, offset, tmp, "");
1070 }
1071
1072 tmp = LLVMBuildLoad(builder, ac_build_gep0(&ctx->ac, ctx->gs_ngg_scratch, tid), "");
1073 LLVMValueRef args[] = {
1074 tmp,
1075 ngg_get_query_buf(ctx),
1076 offset,
1077 LLVMConstInt(ctx->i32, 16, false), /* soffset */
1078 ctx->i32_0, /* cachepolicy */
1079 };
1080 ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.raw.buffer.atomic.add.i32",
1081 ctx->i32, args, 5, 0);
1082 }
1083 ac_build_endif(&ctx->ac, 5110);
1084 ac_build_endif(&ctx->ac, 5109);
1085 }
1086
1087 /* Determine vertex liveness. */
1088 LLVMValueRef vertliveptr = ac_build_alloca(&ctx->ac, ctx->ac.i1, "vertexlive");
1089
1090 tmp = LLVMBuildICmp(builder, LLVMIntULT, tid, num_emit_threads, "");
1091 ac_build_ifcc(&ctx->ac, tmp, 5120);
1092 {
1093 for (unsigned i = 0; i < verts_per_prim; ++i) {
1094 const LLVMValueRef primidx =
1095 LLVMBuildAdd(builder, tid,
1096 LLVMConstInt(ctx->ac.i32, i, false), "");
1097
1098 if (i > 0) {
1099 tmp = LLVMBuildICmp(builder, LLVMIntULT, primidx, num_emit_threads, "");
1100 ac_build_ifcc(&ctx->ac, tmp, 5121 + i);
1101 }
1102
1103 /* Load primitive liveness */
1104 tmp = ngg_gs_vertex_ptr(ctx, primidx);
1105 tmp = LLVMBuildLoad(builder, ngg_gs_get_emit_primflag_ptr(ctx, tmp, 0), "");
1106 const LLVMValueRef primlive =
1107 LLVMBuildTrunc(builder, tmp, ctx->ac.i1, "");
1108
1109 tmp = LLVMBuildLoad(builder, vertliveptr, "");
1110 tmp = LLVMBuildOr(builder, tmp, primlive, ""),
1111 LLVMBuildStore(builder, tmp, vertliveptr);
1112
1113 if (i > 0)
1114 ac_build_endif(&ctx->ac, 5121 + i);
1115 }
1116 }
1117 ac_build_endif(&ctx->ac, 5120);
1118
1119 /* Inclusive scan addition across the current wave. */
1120 LLVMValueRef vertlive = LLVMBuildLoad(builder, vertliveptr, "");
1121 struct ac_wg_scan vertlive_scan = {};
1122 vertlive_scan.op = nir_op_iadd;
1123 vertlive_scan.enable_reduce = true;
1124 vertlive_scan.enable_exclusive = true;
1125 vertlive_scan.src = vertlive;
1126 vertlive_scan.scratch = ac_build_gep0(&ctx->ac, ctx->gs_ngg_scratch, ctx->i32_0);
1127 vertlive_scan.waveidx = get_wave_id_in_tg(ctx);
1128 vertlive_scan.numwaves = get_tgsize(ctx);
1129 vertlive_scan.maxwaves = 8;
1130
1131 ac_build_wg_scan(&ctx->ac, &vertlive_scan);
1132
1133 /* Skip all exports (including index exports) when possible. At least on
1134 * early gfx10 revisions this is also to avoid hangs.
1135 */
1136 LLVMValueRef have_exports =
1137 LLVMBuildICmp(builder, LLVMIntNE, vertlive_scan.result_reduce, ctx->ac.i32_0, "");
1138 num_emit_threads =
1139 LLVMBuildSelect(builder, have_exports, num_emit_threads, ctx->ac.i32_0, "");
1140
1141 /* Allocate export space. Send this message as early as possible, to
1142 * hide the latency of the SQ <-> SPI roundtrip.
1143 *
1144 * Note: We could consider compacting primitives for export as well.
1145 * PA processes 1 non-null prim / clock, but it fetches 4 DW of
1146 * prim data per clock and skips null primitives at no additional
1147 * cost. So compacting primitives can only be beneficial when
1148 * there are 4 or more contiguous null primitives in the export
1149 * (in the common case of single-dword prim exports).
1150 */
1151 ac_build_sendmsg_gs_alloc_req(&ctx->ac, get_wave_id_in_tg(ctx),
1152 vertlive_scan.result_reduce, num_emit_threads);
1153
1154 /* Setup the reverse vertex compaction permutation. We re-use stream 1
1155 * of the primitive liveness flags, relying on the fact that each
1156 * threadgroup can have at most 256 threads. */
1157 ac_build_ifcc(&ctx->ac, vertlive, 5130);
1158 {
1159 tmp = ngg_gs_vertex_ptr(ctx, vertlive_scan.result_exclusive);
1160 tmp2 = LLVMBuildTrunc(builder, tid, ctx->ac.i8, "");
1161 LLVMBuildStore(builder, tmp2, ngg_gs_get_emit_primflag_ptr(ctx, tmp, 1));
1162 }
1163 ac_build_endif(&ctx->ac, 5130);
1164
1165 ac_build_s_barrier(&ctx->ac);
1166
1167 /* Export primitive data */
1168 tmp = LLVMBuildICmp(builder, LLVMIntULT, tid, num_emit_threads, "");
1169 ac_build_ifcc(&ctx->ac, tmp, 5140);
1170 {
1171 LLVMValueRef flags;
1172 struct ac_ngg_prim prim = {};
1173 prim.num_vertices = verts_per_prim;
1174
1175 tmp = ngg_gs_vertex_ptr(ctx, tid);
1176 flags = LLVMBuildLoad(builder, ngg_gs_get_emit_primflag_ptr(ctx, tmp, 0), "");
1177 prim.isnull = LLVMBuildNot(builder, LLVMBuildTrunc(builder, flags, ctx->i1, ""), "");
1178
1179 for (unsigned i = 0; i < verts_per_prim; ++i) {
1180 prim.index[i] = LLVMBuildSub(builder, vertlive_scan.result_exclusive,
1181 LLVMConstInt(ctx->ac.i32, verts_per_prim - i - 1, false), "");
1182 prim.edgeflag[i] = ctx->ac.i1false;
1183 }
1184
1185 /* Geometry shaders output triangle strips, but NGG expects triangles.
1186 * We need to change the vertex order for odd triangles to get correct
1187 * front/back facing by swapping 2 vertex indices, but we also have to
1188 * keep the provoking vertex in the same place.
1189 *
1190 * If the first vertex is provoking, swap index 1 and 2.
1191 * If the last vertex is provoking, swap index 0 and 1.
1192 */
1193 if (verts_per_prim == 3) {
1194 LLVMValueRef is_odd = LLVMBuildLShr(builder, flags, ctx->ac.i8_1, "");
1195 is_odd = LLVMBuildTrunc(builder, is_odd, ctx->i1, "");
1196 LLVMValueRef flatshade_first =
1197 LLVMBuildICmp(builder, LLVMIntEQ,
1198 si_unpack_param(ctx, ctx->vs_state_bits, 4, 2),
1199 ctx->i32_0, "");
1200
1201 struct ac_ngg_prim in = prim;
1202 prim.index[0] = LLVMBuildSelect(builder, flatshade_first,
1203 in.index[0],
1204 LLVMBuildSelect(builder, is_odd,
1205 in.index[1], in.index[0], ""), "");
1206 prim.index[1] = LLVMBuildSelect(builder, flatshade_first,
1207 LLVMBuildSelect(builder, is_odd,
1208 in.index[2], in.index[1], ""),
1209 LLVMBuildSelect(builder, is_odd,
1210 in.index[0], in.index[1], ""), "");
1211 prim.index[2] = LLVMBuildSelect(builder, flatshade_first,
1212 LLVMBuildSelect(builder, is_odd,
1213 in.index[1], in.index[2], ""),
1214 in.index[2], "");
1215 }
1216
1217 ac_build_export_prim(&ctx->ac, &prim);
1218 }
1219 ac_build_endif(&ctx->ac, 5140);
1220
1221 /* Export position and parameter data */
1222 tmp = LLVMBuildICmp(builder, LLVMIntULT, tid, vertlive_scan.result_reduce, "");
1223 ac_build_ifcc(&ctx->ac, tmp, 5145);
1224 {
1225 struct si_shader_output_values outputs[PIPE_MAX_SHADER_OUTPUTS];
1226
1227 tmp = ngg_gs_vertex_ptr(ctx, tid);
1228 tmp = LLVMBuildLoad(builder, ngg_gs_get_emit_primflag_ptr(ctx, tmp, 1), "");
1229 tmp = LLVMBuildZExt(builder, tmp, ctx->ac.i32, "");
1230 const LLVMValueRef vertexptr = ngg_gs_vertex_ptr(ctx, tmp);
1231
1232 unsigned out_idx = 0;
1233 for (unsigned i = 0; i < info->num_outputs; i++) {
1234 outputs[i].semantic_name = info->output_semantic_name[i];
1235 outputs[i].semantic_index = info->output_semantic_index[i];
1236
1237 for (unsigned j = 0; j < 4; j++, out_idx++) {
1238 tmp = ngg_gs_get_emit_output_ptr(ctx, vertexptr, out_idx);
1239 tmp = LLVMBuildLoad(builder, tmp, "");
1240 outputs[i].values[j] = ac_to_float(&ctx->ac, tmp);
1241 outputs[i].vertex_stream[j] =
1242 (info->output_streams[i] >> (2 * j)) & 3;
1243 }
1244 }
1245
1246 si_llvm_export_vs(ctx, outputs, info->num_outputs);
1247 }
1248 ac_build_endif(&ctx->ac, 5145);
1249 }
1250
1251 static void clamp_gsprims_to_esverts(unsigned *max_gsprims, unsigned max_esverts,
1252 unsigned min_verts_per_prim, bool use_adjacency)
1253 {
1254 unsigned max_reuse = max_esverts - min_verts_per_prim;
1255 if (use_adjacency)
1256 max_reuse /= 2;
1257 *max_gsprims = MIN2(*max_gsprims, 1 + max_reuse);
1258 }
1259
1260 /**
1261 * Determine subgroup information like maximum number of vertices and prims.
1262 *
1263 * This happens before the shader is uploaded, since LDS relocations during
1264 * upload depend on the subgroup size.
1265 */
1266 void gfx10_ngg_calculate_subgroup_info(struct si_shader *shader)
1267 {
1268 const struct si_shader_selector *gs_sel = shader->selector;
1269 const struct si_shader_selector *es_sel =
1270 shader->previous_stage_sel ? shader->previous_stage_sel : gs_sel;
1271 const enum pipe_shader_type gs_type = gs_sel->type;
1272 const unsigned gs_num_invocations = MAX2(gs_sel->gs_num_invocations, 1);
1273 const unsigned input_prim = si_get_input_prim(gs_sel);
1274 const bool use_adjacency = input_prim >= PIPE_PRIM_LINES_ADJACENCY &&
1275 input_prim <= PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY;
1276 const unsigned max_verts_per_prim = u_vertices_per_prim(input_prim);
1277 const unsigned min_verts_per_prim =
1278 gs_type == PIPE_SHADER_GEOMETRY ? max_verts_per_prim : 1;
1279
1280 /* All these are in dwords: */
1281 /* We can't allow using the whole LDS, because GS waves compete with
1282 * other shader stages for LDS space.
1283 *
1284 * TODO: We should really take the shader's internal LDS use into
1285 * account. The linker will fail if the size is greater than
1286 * 8K dwords.
1287 */
1288 const unsigned max_lds_size = 8 * 1024 - 768;
1289 const unsigned target_lds_size = max_lds_size;
1290 unsigned esvert_lds_size = 0;
1291 unsigned gsprim_lds_size = 0;
1292
1293 /* All these are per subgroup: */
1294 bool max_vert_out_per_gs_instance = false;
1295 unsigned max_esverts_base = 128;
1296 unsigned max_gsprims_base = 128; /* default prim group size clamp */
1297
1298 /* Hardware has the following non-natural restrictions on the value
1299 * of GE_CNTL.VERT_GRP_SIZE based on based on the primitive type of
1300 * the draw:
1301 * - at most 252 for any line input primitive type
1302 * - at most 251 for any quad input primitive type
1303 * - at most 251 for triangle strips with adjacency (this happens to
1304 * be the natural limit for triangle *lists* with adjacency)
1305 */
1306 max_esverts_base = MIN2(max_esverts_base, 251 + max_verts_per_prim - 1);
1307
1308 if (gs_type == PIPE_SHADER_GEOMETRY) {
1309 unsigned max_out_verts_per_gsprim =
1310 gs_sel->gs_max_out_vertices * gs_num_invocations;
1311
1312 if (max_out_verts_per_gsprim <= 256) {
1313 if (max_out_verts_per_gsprim) {
1314 max_gsprims_base = MIN2(max_gsprims_base,
1315 256 / max_out_verts_per_gsprim);
1316 }
1317 } else {
1318 /* Use special multi-cycling mode in which each GS
1319 * instance gets its own subgroup. Does not work with
1320 * tessellation. */
1321 max_vert_out_per_gs_instance = true;
1322 max_gsprims_base = 1;
1323 max_out_verts_per_gsprim = gs_sel->gs_max_out_vertices;
1324 }
1325
1326 esvert_lds_size = es_sel->esgs_itemsize / 4;
1327 gsprim_lds_size = (gs_sel->gsvs_vertex_size / 4 + 1) * max_out_verts_per_gsprim;
1328 } else {
1329 /* VS and TES. */
1330 /* LDS size for passing data from ES to GS. */
1331 esvert_lds_size = ngg_nogs_vertex_size(shader);
1332
1333 /* LDS size for passing data from GS to ES.
1334 * GS stores Primitive IDs into LDS at the address corresponding
1335 * to the ES thread of the provoking vertex. All ES threads
1336 * load and export PrimitiveID for their thread.
1337 */
1338 if (gs_sel->type == PIPE_SHADER_VERTEX &&
1339 shader->key.mono.u.vs_export_prim_id)
1340 esvert_lds_size = MAX2(esvert_lds_size, 1);
1341 }
1342
1343 unsigned max_gsprims = max_gsprims_base;
1344 unsigned max_esverts = max_esverts_base;
1345
1346 if (esvert_lds_size)
1347 max_esverts = MIN2(max_esverts, target_lds_size / esvert_lds_size);
1348 if (gsprim_lds_size)
1349 max_gsprims = MIN2(max_gsprims, target_lds_size / gsprim_lds_size);
1350
1351 max_esverts = MIN2(max_esverts, max_gsprims * max_verts_per_prim);
1352 clamp_gsprims_to_esverts(&max_gsprims, max_esverts, min_verts_per_prim, use_adjacency);
1353 assert(max_esverts >= max_verts_per_prim && max_gsprims >= 1);
1354
1355 if (esvert_lds_size || gsprim_lds_size) {
1356 /* Now that we have a rough proportionality between esverts
1357 * and gsprims based on the primitive type, scale both of them
1358 * down simultaneously based on required LDS space.
1359 *
1360 * We could be smarter about this if we knew how much vertex
1361 * reuse to expect.
1362 */
1363 unsigned lds_total = max_esverts * esvert_lds_size +
1364 max_gsprims * gsprim_lds_size;
1365 if (lds_total > target_lds_size) {
1366 max_esverts = max_esverts * target_lds_size / lds_total;
1367 max_gsprims = max_gsprims * target_lds_size / lds_total;
1368
1369 max_esverts = MIN2(max_esverts, max_gsprims * max_verts_per_prim);
1370 clamp_gsprims_to_esverts(&max_gsprims, max_esverts,
1371 min_verts_per_prim, use_adjacency);
1372 assert(max_esverts >= max_verts_per_prim && max_gsprims >= 1);
1373 }
1374 }
1375
1376 /* Round up towards full wave sizes for better ALU utilization. */
1377 if (!max_vert_out_per_gs_instance) {
1378 const unsigned wavesize = gs_sel->screen->ge_wave_size;
1379 unsigned orig_max_esverts;
1380 unsigned orig_max_gsprims;
1381 do {
1382 orig_max_esverts = max_esverts;
1383 orig_max_gsprims = max_gsprims;
1384
1385 max_esverts = align(max_esverts, wavesize);
1386 max_esverts = MIN2(max_esverts, max_esverts_base);
1387 if (esvert_lds_size)
1388 max_esverts = MIN2(max_esverts,
1389 (max_lds_size - max_gsprims * gsprim_lds_size) /
1390 esvert_lds_size);
1391 max_esverts = MIN2(max_esverts, max_gsprims * max_verts_per_prim);
1392
1393 max_gsprims = align(max_gsprims, wavesize);
1394 max_gsprims = MIN2(max_gsprims, max_gsprims_base);
1395 if (gsprim_lds_size)
1396 max_gsprims = MIN2(max_gsprims,
1397 (max_lds_size - max_esverts * esvert_lds_size) /
1398 gsprim_lds_size);
1399 clamp_gsprims_to_esverts(&max_gsprims, max_esverts,
1400 min_verts_per_prim, use_adjacency);
1401 assert(max_esverts >= max_verts_per_prim && max_gsprims >= 1);
1402 } while (orig_max_esverts != max_esverts || orig_max_gsprims != max_gsprims);
1403 }
1404
1405 /* Hardware restriction: minimum value of max_esverts */
1406 max_esverts = MAX2(max_esverts, 23 + max_verts_per_prim);
1407
1408 unsigned max_out_vertices =
1409 max_vert_out_per_gs_instance ? gs_sel->gs_max_out_vertices :
1410 gs_type == PIPE_SHADER_GEOMETRY ?
1411 max_gsprims * gs_num_invocations * gs_sel->gs_max_out_vertices :
1412 max_esverts;
1413 assert(max_out_vertices <= 256);
1414
1415 unsigned prim_amp_factor = 1;
1416 if (gs_type == PIPE_SHADER_GEOMETRY) {
1417 /* Number of output primitives per GS input primitive after
1418 * GS instancing. */
1419 prim_amp_factor = gs_sel->gs_max_out_vertices;
1420 }
1421
1422 /* The GE only checks against the maximum number of ES verts after
1423 * allocating a full GS primitive. So we need to ensure that whenever
1424 * this check passes, there is enough space for a full primitive without
1425 * vertex reuse.
1426 */
1427 shader->ngg.hw_max_esverts = max_esverts - max_verts_per_prim + 1;
1428 shader->ngg.max_gsprims = max_gsprims;
1429 shader->ngg.max_out_verts = max_out_vertices;
1430 shader->ngg.prim_amp_factor = prim_amp_factor;
1431 shader->ngg.max_vert_out_per_gs_instance = max_vert_out_per_gs_instance;
1432
1433 shader->gs_info.esgs_ring_size = 4 * max_esverts * esvert_lds_size;
1434 shader->ngg.ngg_emit_size = max_gsprims * gsprim_lds_size;
1435
1436 assert(shader->ngg.hw_max_esverts >= 24); /* HW limitation */
1437 }