Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gfx10_shader_ngg.c 51KB

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