Path: blob/21.2-virgl/src/gallium/drivers/radeonsi/gfx10_shader_ngg.c
4570 views
/*1* Copyright 2017 Advanced Micro Devices, Inc.2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* on the rights to use, copy, modify, merge, publish, distribute, sub7* license, and/or sell copies of the Software, and to permit persons to whom8* the Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL17* THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,18* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR19* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE20* USE OR OTHER DEALINGS IN THE SOFTWARE.21*/2223#include "ac_llvm_cull.h"24#include "si_pipe.h"25#include "si_shader_internal.h"26#include "sid.h"27#include "util/u_memory.h"28#include "util/u_prim.h"2930static LLVMValueRef get_wave_id_in_tg(struct si_shader_context *ctx)31{32return si_unpack_param(ctx, ctx->args.merged_wave_info, 24, 4);33}3435static LLVMValueRef get_tgsize(struct si_shader_context *ctx)36{37return si_unpack_param(ctx, ctx->args.merged_wave_info, 28, 4);38}3940static LLVMValueRef get_thread_id_in_tg(struct si_shader_context *ctx)41{42LLVMBuilderRef builder = ctx->ac.builder;43LLVMValueRef tmp;44tmp = LLVMBuildMul(builder, get_wave_id_in_tg(ctx),45LLVMConstInt(ctx->ac.i32, ctx->ac.wave_size, false), "");46return LLVMBuildAdd(builder, tmp, ac_get_thread_id(&ctx->ac), "");47}4849static LLVMValueRef ngg_get_vtx_cnt(struct si_shader_context *ctx)50{51return si_unpack_param(ctx, ctx->args.gs_tg_info, 12, 9);52}5354static LLVMValueRef ngg_get_prim_cnt(struct si_shader_context *ctx)55{56return si_unpack_param(ctx, ctx->args.gs_tg_info, 22, 9);57}5859static LLVMValueRef ngg_get_ordered_id(struct si_shader_context *ctx)60{61return si_unpack_param(ctx, ctx->args.gs_tg_info, 0, 12);62}6364static LLVMValueRef ngg_get_query_buf(struct si_shader_context *ctx)65{66LLVMValueRef buf_ptr = ac_get_arg(&ctx->ac, ctx->internal_bindings);6768return ac_build_load_to_sgpr(&ctx->ac, buf_ptr,69LLVMConstInt(ctx->ac.i32, GFX10_GS_QUERY_BUF, false));70}7172static LLVMValueRef ngg_get_initial_edgeflag(struct si_shader_context *ctx, unsigned index)73{74if (ctx->stage == MESA_SHADER_VERTEX) {75LLVMValueRef tmp;76tmp = LLVMBuildLShr(ctx->ac.builder, ac_get_arg(&ctx->ac, ctx->args.gs_invocation_id),77LLVMConstInt(ctx->ac.i32, 8 + index, false), "");78return LLVMBuildTrunc(ctx->ac.builder, tmp, ctx->ac.i1, "");79}80return ctx->ac.i1false;81}8283/**84* Return the number of vertices as a constant in \p num_vertices,85* and return a more precise value as LLVMValueRef from the function.86*/87static LLVMValueRef ngg_get_vertices_per_prim(struct si_shader_context *ctx, unsigned *num_vertices)88{89const struct si_shader_info *info = &ctx->shader->selector->info;9091if (ctx->stage == MESA_SHADER_VERTEX) {92if (info->base.vs.blit_sgprs_amd) {93/* Blits always use axis-aligned rectangles with 3 vertices. */94*num_vertices = 3;95return LLVMConstInt(ctx->ac.i32, 3, 0);96} else {97/* We always build up all three indices for the prim export98* independent of the primitive type. The additional garbage99* data shouldn't hurt. This number doesn't matter with100* NGG passthrough.101*/102*num_vertices = 3;103104/* Extract OUTPRIM field. */105LLVMValueRef num = si_unpack_param(ctx, ctx->vs_state_bits, 2, 2);106return LLVMBuildAdd(ctx->ac.builder, num, ctx->ac.i32_1, "");107}108} else {109assert(ctx->stage == MESA_SHADER_TESS_EVAL);110111if (info->base.tess.point_mode)112*num_vertices = 1;113else if (info->base.tess.primitive_mode == GL_LINES)114*num_vertices = 2;115else116*num_vertices = 3;117118return LLVMConstInt(ctx->ac.i32, *num_vertices, false);119}120}121122bool gfx10_ngg_export_prim_early(struct si_shader *shader)123{124struct si_shader_selector *sel = shader->selector;125126assert(shader->key.as_ngg && !shader->key.as_es);127128return sel->info.stage != MESA_SHADER_GEOMETRY && !sel->info.writes_edgeflag;129}130131void gfx10_ngg_build_sendmsg_gs_alloc_req(struct si_shader_context *ctx)132{133/* Newer chips can use PRIMGEN_PASSTHRU_NO_MSG to skip gs_alloc_req for NGG passthrough. */134if (gfx10_is_ngg_passthrough(ctx->shader) &&135ctx->screen->info.family >= CHIP_DIMGREY_CAVEFISH)136return;137138ac_build_sendmsg_gs_alloc_req(&ctx->ac, get_wave_id_in_tg(ctx), ngg_get_vtx_cnt(ctx),139ngg_get_prim_cnt(ctx));140}141142void gfx10_ngg_build_export_prim(struct si_shader_context *ctx, LLVMValueRef user_edgeflags[3],143LLVMValueRef prim_passthrough)144{145LLVMBuilderRef builder = ctx->ac.builder;146147if (gfx10_is_ngg_passthrough(ctx->shader) || ctx->shader->key.opt.ngg_culling) {148ac_build_ifcc(&ctx->ac, si_is_gs_thread(ctx), 6001);149{150struct ac_ngg_prim prim = {};151152if (prim_passthrough)153prim.passthrough = prim_passthrough;154else155prim.passthrough = ac_get_arg(&ctx->ac, ctx->gs_vtx01_offset);156157/* This is only used with NGG culling, which returns the NGG158* passthrough prim export encoding.159*/160if (ctx->shader->selector->info.writes_edgeflag) {161unsigned all_bits_no_edgeflags = ~SI_NGG_PRIM_EDGE_FLAG_BITS;162LLVMValueRef edgeflags = LLVMConstInt(ctx->ac.i32, all_bits_no_edgeflags, 0);163164unsigned num_vertices;165ngg_get_vertices_per_prim(ctx, &num_vertices);166167for (unsigned i = 0; i < num_vertices; i++) {168unsigned shift = 9 + i * 10;169LLVMValueRef edge;170171edge = LLVMBuildLoad(builder, user_edgeflags[i], "");172edge = LLVMBuildZExt(builder, edge, ctx->ac.i32, "");173edge = LLVMBuildShl(builder, edge, LLVMConstInt(ctx->ac.i32, shift, 0), "");174edgeflags = LLVMBuildOr(builder, edgeflags, edge, "");175}176prim.passthrough = LLVMBuildAnd(builder, prim.passthrough, edgeflags, "");177}178179ac_build_export_prim(&ctx->ac, &prim);180}181ac_build_endif(&ctx->ac, 6001);182return;183}184185ac_build_ifcc(&ctx->ac, si_is_gs_thread(ctx), 6001);186{187struct ac_ngg_prim prim = {};188189ngg_get_vertices_per_prim(ctx, &prim.num_vertices);190191prim.isnull = ctx->ac.i1false;192prim.index[0] = si_unpack_param(ctx, ctx->gs_vtx01_offset, 0, 16);193prim.index[1] = si_unpack_param(ctx, ctx->gs_vtx01_offset, 16, 16);194prim.index[2] = si_unpack_param(ctx, ctx->gs_vtx23_offset, 0, 16);195196for (unsigned i = 0; i < prim.num_vertices; ++i) {197prim.edgeflag[i] = ngg_get_initial_edgeflag(ctx, i);198199if (ctx->shader->selector->info.writes_edgeflag) {200LLVMValueRef edge;201202edge = LLVMBuildLoad(ctx->ac.builder, user_edgeflags[i], "");203edge = LLVMBuildAnd(ctx->ac.builder, prim.edgeflag[i], edge, "");204prim.edgeflag[i] = edge;205}206}207208ac_build_export_prim(&ctx->ac, &prim);209}210ac_build_endif(&ctx->ac, 6001);211}212213static void build_streamout_vertex(struct si_shader_context *ctx, LLVMValueRef *so_buffer,214LLVMValueRef *wg_offset_dw, unsigned stream,215LLVMValueRef offset_vtx, LLVMValueRef vertexptr)216{217struct si_shader_info *info = &ctx->shader->selector->info;218struct pipe_stream_output_info *so = &ctx->shader->selector->so;219LLVMBuilderRef builder = ctx->ac.builder;220LLVMValueRef offset[4] = {};221LLVMValueRef tmp;222223for (unsigned buffer = 0; buffer < 4; ++buffer) {224if (!wg_offset_dw[buffer])225continue;226227tmp = LLVMBuildMul(builder, offset_vtx, LLVMConstInt(ctx->ac.i32, so->stride[buffer], false),228"");229tmp = LLVMBuildAdd(builder, wg_offset_dw[buffer], tmp, "");230offset[buffer] = LLVMBuildShl(builder, tmp, LLVMConstInt(ctx->ac.i32, 2, false), "");231}232233for (unsigned i = 0; i < so->num_outputs; ++i) {234if (so->output[i].stream != stream)235continue;236237unsigned reg = so->output[i].register_index;238struct si_shader_output_values out;239out.semantic = info->output_semantic[reg];240241for (unsigned comp = 0; comp < 4; comp++) {242tmp = ac_build_gep0(&ctx->ac, vertexptr, LLVMConstInt(ctx->ac.i32, 4 * reg + comp, false));243out.values[comp] = LLVMBuildLoad(builder, tmp, "");244out.vertex_stream[comp] = (info->output_streams[reg] >> (2 * comp)) & 3;245}246247si_llvm_streamout_store_output(ctx, so_buffer, offset, &so->output[i], &out);248}249}250251struct ngg_streamout {252LLVMValueRef num_vertices;253254/* per-thread data */255LLVMValueRef prim_enable[4]; /* i1 per stream */256LLVMValueRef vertices[3]; /* [N x i32] addrspace(LDS)* */257258/* Output */259LLVMValueRef emit[4]; /* per-stream emitted primitives (only valid for used streams) */260};261262/**263* Build streamout logic.264*265* Implies a barrier.266*267* Writes number of emitted primitives to gs_ngg_scratch[4:8].268*269* Clobbers gs_ngg_scratch[8:].270*/271static void build_streamout(struct si_shader_context *ctx, struct ngg_streamout *nggso)272{273struct si_shader_info *info = &ctx->shader->selector->info;274struct pipe_stream_output_info *so = &ctx->shader->selector->so;275LLVMBuilderRef builder = ctx->ac.builder;276LLVMValueRef buf_ptr = ac_get_arg(&ctx->ac, ctx->internal_bindings);277LLVMValueRef tid = get_thread_id_in_tg(ctx);278LLVMValueRef tmp, tmp2;279LLVMValueRef i32_2 = LLVMConstInt(ctx->ac.i32, 2, false);280LLVMValueRef i32_4 = LLVMConstInt(ctx->ac.i32, 4, false);281LLVMValueRef i32_8 = LLVMConstInt(ctx->ac.i32, 8, false);282LLVMValueRef so_buffer[4] = {};283unsigned max_num_vertices = 1 + (nggso->vertices[1] ? 1 : 0) + (nggso->vertices[2] ? 1 : 0);284LLVMValueRef prim_stride_dw[4] = {};285LLVMValueRef prim_stride_dw_vgpr = LLVMGetUndef(ctx->ac.i32);286int stream_for_buffer[4] = {-1, -1, -1, -1};287unsigned bufmask_for_stream[4] = {};288bool isgs = ctx->stage == MESA_SHADER_GEOMETRY;289unsigned scratch_emit_base = isgs ? 4 : 0;290LLVMValueRef scratch_emit_basev = isgs ? i32_4 : ctx->ac.i32_0;291unsigned scratch_offset_base = isgs ? 8 : 4;292LLVMValueRef scratch_offset_basev = isgs ? i32_8 : i32_4;293294ac_llvm_add_target_dep_function_attr(ctx->main_fn, "amdgpu-gds-size", 256);295296/* Determine the mapping of streamout buffers to vertex streams. */297for (unsigned i = 0; i < so->num_outputs; ++i) {298unsigned buf = so->output[i].output_buffer;299unsigned stream = so->output[i].stream;300assert(stream_for_buffer[buf] < 0 || stream_for_buffer[buf] == stream);301stream_for_buffer[buf] = stream;302bufmask_for_stream[stream] |= 1 << buf;303}304305for (unsigned buffer = 0; buffer < 4; ++buffer) {306if (stream_for_buffer[buffer] == -1)307continue;308309assert(so->stride[buffer]);310311tmp = LLVMConstInt(ctx->ac.i32, so->stride[buffer], false);312prim_stride_dw[buffer] = LLVMBuildMul(builder, tmp, nggso->num_vertices, "");313prim_stride_dw_vgpr =314ac_build_writelane(&ctx->ac, prim_stride_dw_vgpr, prim_stride_dw[buffer],315LLVMConstInt(ctx->ac.i32, buffer, false));316317so_buffer[buffer] = ac_build_load_to_sgpr(318&ctx->ac, buf_ptr, LLVMConstInt(ctx->ac.i32, SI_VS_STREAMOUT_BUF0 + buffer, false));319}320321tmp = LLVMBuildICmp(builder, LLVMIntEQ, get_wave_id_in_tg(ctx), ctx->ac.i32_0, "");322ac_build_ifcc(&ctx->ac, tmp, 5200);323{324LLVMTypeRef gdsptr = LLVMPointerType(ctx->ac.i32, AC_ADDR_SPACE_GDS);325LLVMValueRef gdsbase = LLVMBuildIntToPtr(builder, ctx->ac.i32_0, gdsptr, "");326327/* Advance the streamout offsets in GDS. */328LLVMValueRef offsets_vgpr = ac_build_alloca_undef(&ctx->ac, ctx->ac.i32, "");329LLVMValueRef generated_by_stream_vgpr = ac_build_alloca_undef(&ctx->ac, ctx->ac.i32, "");330331tmp = LLVMBuildICmp(builder, LLVMIntULT, ac_get_thread_id(&ctx->ac), i32_4, "");332ac_build_ifcc(&ctx->ac, tmp, 5210);333{334if (isgs) {335tmp = ac_build_gep0(&ctx->ac, ctx->gs_ngg_scratch, tid);336tmp = LLVMBuildLoad(builder, tmp, "");337} else {338tmp = ac_build_writelane(&ctx->ac, ctx->ac.i32_0, ngg_get_prim_cnt(ctx), ctx->ac.i32_0);339}340LLVMBuildStore(builder, tmp, generated_by_stream_vgpr);341342unsigned swizzle[4];343int unused_stream = -1;344for (unsigned stream = 0; stream < 4; ++stream) {345if (!info->num_stream_output_components[stream]) {346unused_stream = stream;347break;348}349}350for (unsigned buffer = 0; buffer < 4; ++buffer) {351if (stream_for_buffer[buffer] >= 0) {352swizzle[buffer] = stream_for_buffer[buffer];353} else {354assert(unused_stream >= 0);355swizzle[buffer] = unused_stream;356}357}358359tmp = ac_build_quad_swizzle(&ctx->ac, tmp, swizzle[0], swizzle[1], swizzle[2], swizzle[3]);360tmp = LLVMBuildMul(builder, tmp, prim_stride_dw_vgpr, "");361362LLVMValueRef args[] = {363LLVMBuildIntToPtr(builder, ngg_get_ordered_id(ctx), gdsptr, ""),364tmp,365ctx->ac.i32_0, // ordering366ctx->ac.i32_0, // scope367ctx->ac.i1false, // isVolatile368LLVMConstInt(ctx->ac.i32, 4 << 24, false), // OA index369ctx->ac.i1true, // wave release370ctx->ac.i1true, // wave done371};372tmp = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.ds.ordered.add", ctx->ac.i32, args,373ARRAY_SIZE(args), 0);374375/* Keep offsets in a VGPR for quick retrieval via readlane by376* the first wave for bounds checking, and also store in LDS377* for retrieval by all waves later. */378LLVMBuildStore(builder, tmp, offsets_vgpr);379380tmp2 = LLVMBuildAdd(builder, ac_get_thread_id(&ctx->ac), scratch_offset_basev, "");381tmp2 = ac_build_gep0(&ctx->ac, ctx->gs_ngg_scratch, tmp2);382LLVMBuildStore(builder, tmp, tmp2);383}384ac_build_endif(&ctx->ac, 5210);385386/* Determine the max emit per buffer. This is done via the SALU, in part387* because LLVM can't generate divide-by-multiply if we try to do this388* via VALU with one lane per buffer.389*/390LLVMValueRef max_emit[4] = {};391for (unsigned buffer = 0; buffer < 4; ++buffer) {392if (stream_for_buffer[buffer] == -1)393continue;394395LLVMValueRef bufsize_dw = LLVMBuildLShr(396builder, LLVMBuildExtractElement(builder, so_buffer[buffer], i32_2, ""), i32_2, "");397398tmp = LLVMBuildLoad(builder, offsets_vgpr, "");399LLVMValueRef offset_dw =400ac_build_readlane(&ctx->ac, tmp, LLVMConstInt(ctx->ac.i32, buffer, false));401402tmp = LLVMBuildSub(builder, bufsize_dw, offset_dw, "");403tmp = LLVMBuildUDiv(builder, tmp, prim_stride_dw[buffer], "");404405tmp2 = LLVMBuildICmp(builder, LLVMIntULT, bufsize_dw, offset_dw, "");406max_emit[buffer] = LLVMBuildSelect(builder, tmp2, ctx->ac.i32_0, tmp, "");407}408409/* Determine the number of emitted primitives per stream and fixup the410* GDS counter if necessary.411*412* This is complicated by the fact that a single stream can emit to413* multiple buffers (but luckily not vice versa).414*/415LLVMValueRef emit_vgpr = ctx->ac.i32_0;416417for (unsigned stream = 0; stream < 4; ++stream) {418if (!info->num_stream_output_components[stream])419continue;420421tmp = LLVMBuildLoad(builder, generated_by_stream_vgpr, "");422LLVMValueRef generated =423ac_build_readlane(&ctx->ac, tmp, LLVMConstInt(ctx->ac.i32, stream, false));424425LLVMValueRef emit = generated;426for (unsigned buffer = 0; buffer < 4; ++buffer) {427if (stream_for_buffer[buffer] == stream)428emit = ac_build_umin(&ctx->ac, emit, max_emit[buffer]);429}430431emit_vgpr =432ac_build_writelane(&ctx->ac, emit_vgpr, emit, LLVMConstInt(ctx->ac.i32, stream, false));433434/* Fixup the offset using a plain GDS atomic if we overflowed. */435tmp = LLVMBuildICmp(builder, LLVMIntULT, emit, generated, "");436ac_build_ifcc(&ctx->ac, tmp, 5221); /* scalar branch */437tmp = LLVMBuildLShr(builder, LLVMConstInt(ctx->ac.i32, bufmask_for_stream[stream], false),438ac_get_thread_id(&ctx->ac), "");439tmp = LLVMBuildTrunc(builder, tmp, ctx->ac.i1, "");440ac_build_ifcc(&ctx->ac, tmp, 5222);441{442tmp = LLVMBuildSub(builder, generated, emit, "");443tmp = LLVMBuildMul(builder, tmp, prim_stride_dw_vgpr, "");444tmp2 = LLVMBuildGEP(builder, gdsbase, &tid, 1, "");445LLVMBuildAtomicRMW(builder, LLVMAtomicRMWBinOpSub, tmp2, tmp,446LLVMAtomicOrderingMonotonic, false);447}448ac_build_endif(&ctx->ac, 5222);449ac_build_endif(&ctx->ac, 5221);450}451452tmp = LLVMBuildICmp(builder, LLVMIntULT, ac_get_thread_id(&ctx->ac), i32_4, "");453ac_build_ifcc(&ctx->ac, tmp, 5225);454{455tmp = LLVMBuildAdd(builder, ac_get_thread_id(&ctx->ac), scratch_emit_basev, "");456tmp = ac_build_gep0(&ctx->ac, ctx->gs_ngg_scratch, tmp);457LLVMBuildStore(builder, emit_vgpr, tmp);458}459ac_build_endif(&ctx->ac, 5225);460}461ac_build_endif(&ctx->ac, 5200);462463/* Determine the workgroup-relative per-thread / primitive offset into464* the streamout buffers */465struct ac_wg_scan primemit_scan[4] = {};466467if (isgs) {468for (unsigned stream = 0; stream < 4; ++stream) {469if (!info->num_stream_output_components[stream])470continue;471472primemit_scan[stream].enable_exclusive = true;473primemit_scan[stream].op = nir_op_iadd;474primemit_scan[stream].src = nggso->prim_enable[stream];475primemit_scan[stream].scratch = ac_build_gep0(476&ctx->ac, ctx->gs_ngg_scratch, LLVMConstInt(ctx->ac.i32, 12 + 8 * stream, false));477primemit_scan[stream].waveidx = get_wave_id_in_tg(ctx);478primemit_scan[stream].numwaves = get_tgsize(ctx);479if (ctx->stage == MESA_SHADER_GEOMETRY) {480/* ngg_subgroup_size is only the input size. GS can always generate up to 256 vertices. */481primemit_scan[stream].maxwaves = DIV_ROUND_UP(256, ctx->ac.wave_size);482} else {483primemit_scan[stream].maxwaves = DIV_ROUND_UP(ctx->screen->ngg_subgroup_size,484ctx->ac.wave_size);485}486ac_build_wg_scan_top(&ctx->ac, &primemit_scan[stream]);487}488}489490ac_build_s_barrier(&ctx->ac);491492/* Fetch the per-buffer offsets and per-stream emit counts in all waves. */493LLVMValueRef wgoffset_dw[4] = {};494495{496LLVMValueRef scratch_vgpr;497498tmp = ac_build_gep0(&ctx->ac, ctx->gs_ngg_scratch, ac_get_thread_id(&ctx->ac));499scratch_vgpr = LLVMBuildLoad(builder, tmp, "");500501for (unsigned buffer = 0; buffer < 4; ++buffer) {502if (stream_for_buffer[buffer] >= 0) {503wgoffset_dw[buffer] =504ac_build_readlane(&ctx->ac, scratch_vgpr,505LLVMConstInt(ctx->ac.i32, scratch_offset_base + buffer, false));506}507}508509for (unsigned stream = 0; stream < 4; ++stream) {510if (info->num_stream_output_components[stream]) {511nggso->emit[stream] =512ac_build_readlane(&ctx->ac, scratch_vgpr,513LLVMConstInt(ctx->ac.i32, scratch_emit_base + stream, false));514}515}516}517518/* Write out primitive data */519for (unsigned stream = 0; stream < 4; ++stream) {520if (!info->num_stream_output_components[stream])521continue;522523if (isgs) {524ac_build_wg_scan_bottom(&ctx->ac, &primemit_scan[stream]);525} else {526primemit_scan[stream].result_exclusive = tid;527}528529tmp = LLVMBuildICmp(builder, LLVMIntULT, primemit_scan[stream].result_exclusive,530nggso->emit[stream], "");531tmp = LLVMBuildAnd(builder, tmp, nggso->prim_enable[stream], "");532ac_build_ifcc(&ctx->ac, tmp, 5240);533{534LLVMValueRef offset_vtx =535LLVMBuildMul(builder, primemit_scan[stream].result_exclusive, nggso->num_vertices, "");536537for (unsigned i = 0; i < max_num_vertices; ++i) {538tmp = LLVMBuildICmp(builder, LLVMIntULT, LLVMConstInt(ctx->ac.i32, i, false),539nggso->num_vertices, "");540ac_build_ifcc(&ctx->ac, tmp, 5241);541build_streamout_vertex(ctx, so_buffer, wgoffset_dw, stream, offset_vtx,542nggso->vertices[i]);543ac_build_endif(&ctx->ac, 5241);544offset_vtx = LLVMBuildAdd(builder, offset_vtx, ctx->ac.i32_1, "");545}546}547ac_build_endif(&ctx->ac, 5240);548}549}550551/* LDS layout of ES vertex data for NGG culling. */552enum553{554/* Byte 0: Boolean ES thread accepted (unculled) flag, and later the old555* ES thread ID. After vertex compaction, compacted ES threads556* store the old thread ID here to copy input VGPRs from uncompacted557* ES threads.558* Byte 1: New ES thread ID, loaded by GS to prepare the prim export value.559* Byte 2: TES rel patch ID560* Byte 3: Unused561*/562lds_byte0_accept_flag = 0,563lds_byte1_new_thread_id,564lds_byte2_tes_rel_patch_id,565lds_byte3_unused,566567lds_packed_data = 0, /* lds_byteN_... */568lds_pos_cull_x_div_w,569lds_pos_cull_y_div_w,570lds_pos_cull_w,571572lds_pos_x = lds_packed_data + 1,573lds_pos_y,574lds_pos_z,575lds_pos_w,576/* If VS: */577lds_vertex_id,578lds_instance_id, /* optional */579/* If TES: */580lds_tes_u = lds_vertex_id,581lds_tes_v = lds_instance_id,582lds_tes_patch_id, /* optional */583};584585static LLVMValueRef si_build_gep_i8_var(struct si_shader_context *ctx, LLVMValueRef ptr,586LLVMValueRef index)587{588LLVMTypeRef pi8 = LLVMPointerType(ctx->ac.i8, AC_ADDR_SPACE_LDS);589590return LLVMBuildGEP(ctx->ac.builder, LLVMBuildPointerCast(ctx->ac.builder, ptr, pi8, ""), &index,5911, "");592}593594static LLVMValueRef si_build_gep_i8(struct si_shader_context *ctx, LLVMValueRef ptr,595unsigned byte_index)596{597assert(byte_index < 4);598return si_build_gep_i8_var(ctx, ptr, LLVMConstInt(ctx->ac.i32, byte_index, 0));599}600601static unsigned ngg_nogs_vertex_size(struct si_shader *shader)602{603unsigned lds_vertex_size = 0;604605/* The edgeflag is always stored in the last element that's also606* used for padding to reduce LDS bank conflicts. */607if (shader->selector->so.num_outputs)608lds_vertex_size = 4 * shader->selector->info.num_outputs + 1;609if (shader->selector->info.writes_edgeflag)610lds_vertex_size = MAX2(lds_vertex_size, 1);611612/* LDS size for passing data from GS to ES.613* GS stores Primitive IDs into LDS at the address corresponding614* to the ES thread of the provoking vertex. All ES threads615* load and export PrimitiveID for their thread.616*/617if (shader->selector->info.stage == MESA_SHADER_VERTEX && shader->key.mono.u.vs_export_prim_id)618lds_vertex_size = MAX2(lds_vertex_size, 1);619620if (shader->key.opt.ngg_culling) {621if (shader->selector->info.stage == MESA_SHADER_VERTEX) {622STATIC_ASSERT(lds_instance_id + 1 == 7);623lds_vertex_size = MAX2(lds_vertex_size, 7);624} else {625assert(shader->selector->info.stage == MESA_SHADER_TESS_EVAL);626627if (shader->selector->info.uses_primid || shader->key.mono.u.vs_export_prim_id) {628STATIC_ASSERT(lds_tes_patch_id + 2 == 9); /* +1 for LDS padding */629lds_vertex_size = MAX2(lds_vertex_size, 9);630} else {631STATIC_ASSERT(lds_tes_v + 1 == 7);632lds_vertex_size = MAX2(lds_vertex_size, 7);633}634}635}636637return lds_vertex_size;638}639640/**641* Returns an `[N x i32] addrspace(LDS)*` pointing at contiguous LDS storage642* for the vertex outputs.643*/644static LLVMValueRef ngg_nogs_vertex_ptr(struct si_shader_context *ctx, LLVMValueRef vtxid)645{646/* The extra dword is used to avoid LDS bank conflicts. */647unsigned vertex_size = ngg_nogs_vertex_size(ctx->shader);648LLVMTypeRef ai32 = LLVMArrayType(ctx->ac.i32, vertex_size);649LLVMTypeRef pai32 = LLVMPointerType(ai32, AC_ADDR_SPACE_LDS);650LLVMValueRef tmp = LLVMBuildBitCast(ctx->ac.builder, ctx->esgs_ring, pai32, "");651return LLVMBuildGEP(ctx->ac.builder, tmp, &vtxid, 1, "");652}653654static LLVMValueRef si_insert_input_v4i32(struct si_shader_context *ctx, LLVMValueRef ret,655struct ac_arg param, unsigned return_index)656{657LLVMValueRef v = ac_get_arg(&ctx->ac, param);658659for (unsigned i = 0; i < 4; i++) {660ret = LLVMBuildInsertValue(ctx->ac.builder, ret, ac_llvm_extract_elem(&ctx->ac, v, i),661return_index + i, "");662}663return ret;664}665666static void load_vertex_counts(struct si_shader_context *ctx, LLVMValueRef lds,667unsigned max_waves, LLVMValueRef tid,668LLVMValueRef *total_count,669LLVMValueRef *prefix_sum)670{671LLVMBuilderRef builder = ctx->ac.builder;672LLVMValueRef i8vec4_lane = ac_build_alloca_undef(&ctx->ac, ctx->ac.i32, "");673unsigned num_i8vec4 = DIV_ROUND_UP(max_waves, 4);674675/* If all threads loaded the vertex counts, it would cause many LDS bank conflicts676* and the performance could decrease up to WaveSize times (32x or 64x).677*678* Therefore, only load the i-th tuple of vertex counts in the i-th thread. Other threads will679* get them through readlane. 4 8-bit vertex counts are loaded per thread.680*/681ac_build_ifcc(&ctx->ac, LLVMBuildICmp(builder, LLVMIntULT, tid,682LLVMConstInt(ctx->ac.i32, num_i8vec4, 0), ""), 17771);683LLVMBuildStore(builder, LLVMBuildLoad(builder, ac_build_gep0(&ctx->ac, lds, tid), ""), i8vec4_lane);684ac_build_endif(&ctx->ac, 17771);685686/* Compute the number of ES waves. */687LLVMValueRef num_waves = get_tgsize(ctx);688689/* Compute a byte mask where each byte is either 0 or 0xff depending on whether the wave690* exists. We need the mask to clear uninitialized bytes in LDS and to compute the prefix sum.691*692* 8 waves: valid_mask = ~0ull >> (64 - num_waves * 8)693* 4 waves: valid_mask = ~0 >> (32 - num_waves * 8)694*/695LLVMValueRef num_waves8 = LLVMBuildShl(builder, num_waves, LLVMConstInt(ctx->ac.i32, 3, 0), "");696LLVMValueRef valid_mask;697698if (max_waves > 4) {699LLVMValueRef num_waves8_rev = LLVMBuildSub(builder, LLVMConstInt(ctx->ac.i32, 64, 0),700num_waves8, "");701valid_mask = LLVMBuildLShr(builder, LLVMConstInt(ctx->ac.i64, ~0ull, 0),702LLVMBuildZExt(builder, num_waves8_rev, ctx->ac.i64, ""), "");703} else {704LLVMValueRef num_waves8_rev = LLVMBuildSub(builder, LLVMConstInt(ctx->ac.i32, 32, 0),705num_waves8, "");706valid_mask = LLVMBuildLShr(builder, LLVMConstInt(ctx->ac.i32, ~0, 0), num_waves8_rev, "");707}708709/* Compute a byte mask where bytes below wave_id are 0xff, else they are 0.710*711* prefix_mask = ~(~0 << (wave_id * 8))712*/713LLVMTypeRef type = max_waves > 4 ? ctx->ac.i64 : ctx->ac.i32;714LLVMValueRef wave_id8 = LLVMBuildShl(builder, get_wave_id_in_tg(ctx),715LLVMConstInt(ctx->ac.i32, 3, 0), "");716LLVMValueRef prefix_mask =717LLVMBuildNot(builder, LLVMBuildShl(builder, LLVMConstInt(type, ~0ull, 0),718LLVMBuildZExt(builder, wave_id8, type, ""), ""), "");719720/* Compute the total vertex count and the vertex count of previous waves (prefix). */721*total_count = ctx->ac.i32_0;722*prefix_sum = ctx->ac.i32_0;723724for (unsigned i = 0; i < num_i8vec4; i++) {725LLVMValueRef i8vec4;726727i8vec4 = ac_build_readlane_no_opt_barrier(&ctx->ac, LLVMBuildLoad(builder, i8vec4_lane, ""),728LLVMConstInt(ctx->ac.i32, i, 0));729/* Inactive waves have uninitialized vertex counts. Set them to 0 using this. */730i8vec4 = LLVMBuildAnd(builder, i8vec4,731ac_unpack_param(&ctx->ac, valid_mask, 32 * i, 32), "");732/* Compute the sum of all i8vec4 components and add it to the result. */733*total_count = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.sad.u8", ctx->ac.i32,734(LLVMValueRef[]){i8vec4, ctx->ac.i32_0, *total_count},7353, AC_FUNC_ATTR_READNONE);736ac_set_range_metadata(&ctx->ac, *total_count, 0, 64*4 + 1); /* the result is at most 64*4 */737738/* Compute the sum of the vertex counts of all previous waves. */739i8vec4 = LLVMBuildAnd(builder, i8vec4,740ac_unpack_param(&ctx->ac, prefix_mask, 32 * i, 32), "");741*prefix_sum = ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.sad.u8", ctx->ac.i32,742(LLVMValueRef[]){i8vec4, ctx->ac.i32_0, *prefix_sum},7433, AC_FUNC_ATTR_READNONE);744ac_set_range_metadata(&ctx->ac, *prefix_sum, 0, 64*4 + 1); /* the result is at most 64*4 */745}746*total_count = ac_build_readlane_no_opt_barrier(&ctx->ac, *total_count, NULL);747}748749/**750* Given a total thread count, update total and per-wave thread counts in input SGPRs751* and return the per-wave thread count.752*753* \param new_num_threads Total thread count on the input, per-wave thread count on the output.754* \param tg_info tg_info SGPR value755* \param tg_info_num_bits the bit size of thread count field in tg_info756* \param tg_info_shift the bit offset of the thread count field in tg_info757* \param wave_info merged_wave_info SGPR value758* \param wave_info_num_bits the bit size of thread count field in merged_wave_info759* \param wave_info_shift the bit offset of the thread count field in merged_wave_info760*/761static void update_thread_counts(struct si_shader_context *ctx, LLVMValueRef *new_num_threads,762LLVMValueRef *tg_info, unsigned tg_info_num_bits,763unsigned tg_info_shift, LLVMValueRef *wave_info,764unsigned wave_info_num_bits, unsigned wave_info_shift)765{766LLVMBuilderRef builder = ctx->ac.builder;767768/* Update the total thread count. */769unsigned tg_info_mask = ~(u_bit_consecutive(0, tg_info_num_bits) << tg_info_shift);770*tg_info = LLVMBuildAnd(builder, *tg_info, LLVMConstInt(ctx->ac.i32, tg_info_mask, 0), "");771*tg_info = LLVMBuildOr(772builder, *tg_info,773LLVMBuildShl(builder, *new_num_threads, LLVMConstInt(ctx->ac.i32, tg_info_shift, 0), ""), "");774775/* Update the per-wave thread count. */776LLVMValueRef prev_threads = LLVMBuildMul(builder, get_wave_id_in_tg(ctx),777LLVMConstInt(ctx->ac.i32, ctx->ac.wave_size, 0), "");778*new_num_threads = LLVMBuildSub(builder, *new_num_threads, prev_threads, "");779*new_num_threads = ac_build_imax(&ctx->ac, *new_num_threads, ctx->ac.i32_0);780*new_num_threads =781ac_build_imin(&ctx->ac, *new_num_threads, LLVMConstInt(ctx->ac.i32, ctx->ac.wave_size, 0));782unsigned wave_info_mask = ~(u_bit_consecutive(0, wave_info_num_bits) << wave_info_shift);783*wave_info = LLVMBuildAnd(builder, *wave_info, LLVMConstInt(ctx->ac.i32, wave_info_mask, 0), "");784*wave_info = LLVMBuildOr(785builder, *wave_info,786LLVMBuildShl(builder, *new_num_threads, LLVMConstInt(ctx->ac.i32, wave_info_shift, 0), ""),787"");788}789790static void gfx10_build_primitive_accepted(struct ac_llvm_context *ac, LLVMValueRef accepted,791void *userdata)792{793struct si_shader_context *ctx = container_of(ac, struct si_shader_context, ac);794LLVMValueRef *params = (LLVMValueRef *)userdata;795LLVMValueRef gs_accepted = params[0];796LLVMValueRef *gs_vtxptr = (LLVMValueRef *)params[1];797798ac_build_ifcc(&ctx->ac, accepted, 0);799LLVMBuildStore(ctx->ac.builder, ctx->ac.i32_1, gs_accepted);800for (unsigned vtx = 0; vtx < 3; vtx++) {801LLVMBuildStore(ctx->ac.builder, ctx->ac.i8_1,802si_build_gep_i8(ctx, gs_vtxptr[vtx], lds_byte0_accept_flag));803}804ac_build_endif(&ctx->ac, 0);805}806807/**808* Cull primitives for NGG VS or TES, then compact vertices, which happens809* before the VS or TES main function. Return values for the main function.810* Also return the position, which is passed to the shader as an input,811* so that we don't compute it twice.812*/813void gfx10_emit_ngg_culling_epilogue(struct ac_shader_abi *abi, unsigned max_outputs,814LLVMValueRef *addrs)815{816struct si_shader_context *ctx = si_shader_context_from_abi(abi);817struct si_shader *shader = ctx->shader;818struct si_shader_selector *sel = shader->selector;819struct si_shader_info *info = &sel->info;820LLVMBuilderRef builder = ctx->ac.builder;821unsigned max_waves = DIV_ROUND_UP(ctx->screen->ngg_subgroup_size, ctx->ac.wave_size);822823assert(shader->key.opt.ngg_culling);824assert(shader->key.as_ngg);825assert(sel->info.stage == MESA_SHADER_VERTEX ||826(sel->info.stage == MESA_SHADER_TESS_EVAL && !shader->key.as_es));827828LLVMValueRef es_vtxptr = ngg_nogs_vertex_ptr(ctx, get_thread_id_in_tg(ctx));829unsigned pos_index = 0;830831for (unsigned i = 0; i < info->num_outputs; i++) {832LLVMValueRef position[4];833834switch (info->output_semantic[i]) {835case VARYING_SLOT_POS:836/* If we are going to cull everything (rasterizer_discard), discard837* the position. This is useful for analyzing maximum theoretical838* performance without VS input loads.839*/840if (shader->key.opt.ngg_culling & SI_NGG_CULL_FRONT_FACE &&841shader->key.opt.ngg_culling & SI_NGG_CULL_BACK_FACE) {842for (unsigned j = 0; j < 4; j++)843LLVMBuildStore(builder, LLVMGetUndef(ctx->ac.f32), addrs[4 * i + j]);844break;845}846847pos_index = i;848for (unsigned j = 0; j < 4; j++) {849position[j] = LLVMBuildLoad(ctx->ac.builder, addrs[4 * i + j], "");850}851852/* Store Position.W into LDS. */853LLVMBuildStore(854builder, ac_to_integer(&ctx->ac, position[3]),855ac_build_gep0(&ctx->ac, es_vtxptr, LLVMConstInt(ctx->ac.i32, lds_pos_cull_w, 0)));856857/* Store Position.XY / W into LDS. */858for (unsigned chan = 0; chan < 2; chan++) {859LLVMValueRef val = ac_build_fdiv(&ctx->ac, position[chan], position[3]);860LLVMBuildStore(861builder, ac_to_integer(&ctx->ac, val),862ac_build_gep0(&ctx->ac, es_vtxptr, LLVMConstInt(ctx->ac.i32, lds_pos_cull_x_div_w + chan, 0)));863}864break;865}866}867868/* Initialize the packed data. */869LLVMBuildStore(870builder, ctx->ac.i32_0,871ac_build_gep0(&ctx->ac, es_vtxptr, LLVMConstInt(ctx->ac.i32, lds_packed_data, 0)));872ac_build_endif(&ctx->ac, ctx->merged_wrap_if_label);873ac_build_s_barrier(&ctx->ac);874875LLVMValueRef tid = ac_get_thread_id(&ctx->ac);876877/* The hardware requires that there are no holes between unculled vertices,878* which means we have to pack ES threads, i.e. reduce the ES thread count879* and move ES input VGPRs to lower threads. The upside is that varyings880* are only fetched and computed for unculled vertices.881*882* Vertex compaction:883*884* Part 1: Store the surviving vertex count for each wave in LDS.885* - The GS culling code notifies ES threads which vertices were accepted.886* - Barrier887* - ES threads will compute the vertex count and store it in LDS.888* - Barrier889* - Each wave loads the vertex counts from LDS.890*891* Part 2: Compact ES threads:892* - Compute the prefix sum for each surviving vertex. This is the new thread ID893* of the vertex.894* - Write input VGPRs and vertex positions for each surviving vertex into the LDS895* address of the new thread ID.896* - Now kill all waves that have inactive threads.897* - Barrier898* - Update vertex indices and null flag in the GS input VGPRs.899*900* Part 3: Update inputs GPRs901* - For all waves, update per-wave thread counts in input SGPRs.902* - In ES threads, update the ES input VGPRs (VertexID, InstanceID, TES inputs).903*/904905LLVMValueRef vtxindex[3];906if (shader->key.opt.ngg_culling & SI_NGG_CULL_GS_FAST_LAUNCH_ALL) {907/* For the GS fast launch, the VS prolog simply puts the Vertex IDs908* into these VGPRs.909*/910vtxindex[0] = ac_get_arg(&ctx->ac, ctx->gs_vtx01_offset);911vtxindex[1] = ac_get_arg(&ctx->ac, ctx->gs_vtx23_offset);912vtxindex[2] = ac_get_arg(&ctx->ac, ctx->gs_vtx45_offset);913} else {914vtxindex[0] = si_unpack_param(ctx, ctx->gs_vtx01_offset, 0, 16);915vtxindex[1] = si_unpack_param(ctx, ctx->gs_vtx01_offset, 16, 16);916vtxindex[2] = si_unpack_param(ctx, ctx->gs_vtx23_offset, 0, 16);917};918LLVMValueRef gs_vtxptr[] = {919ngg_nogs_vertex_ptr(ctx, vtxindex[0]),920ngg_nogs_vertex_ptr(ctx, vtxindex[1]),921ngg_nogs_vertex_ptr(ctx, vtxindex[2]),922};923es_vtxptr = ngg_nogs_vertex_ptr(ctx, get_thread_id_in_tg(ctx));924925/* Adding these optimization barriers improves the generated code as follows. Crazy right?926*927* - s_mov_b32 s4, 0xffff928* - v_lshrrev_b32_e32 v10, 16, v0929* - v_and_b32_e32 v12, s4, v0930* - v_and_b32_e32 v11, s4, v1931* s_bfe_u32 s4, s3, 0x80008932* - s_mov_b64 s[8:9], 0933* - v_mul_u32_u24_e32 v0, 28, v10934* - v_mul_u32_u24_e32 v9, 28, v12935* - v_mul_u32_u24_e32 v1, 28, v11936* + v_mov_b32_e32 v11, 28937* v_cmp_gt_u32_e32 vcc, s4, v2938* + s_mov_b64 s[8:9], 0939* s_waitcnt lgkmcnt(0)940* s_barrier941* + v_mul_u32_u24_sdwa v10, v0, v11 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_0 src1_sel:DWORD942* + v_mul_u32_u24_sdwa v23, v0, v11 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_1 src1_sel:DWORD943* + v_mul_u32_u24_sdwa v0, v1, v11 dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_0 src1_sel:DWORD944* s_and_saveexec_b64 s[44:45], vcc945* s_cbranch_execz BB2_8946* - v_mul_u32_u24_e32 v16, 28, v12947* - v_mul_u32_u24_e32 v17, 28, v11948* - v_mul_u32_u24_e32 v18, 28, v10949*/950ac_build_optimization_barrier(&ctx->ac, &gs_vtxptr[0], false);951ac_build_optimization_barrier(&ctx->ac, &gs_vtxptr[1], false);952ac_build_optimization_barrier(&ctx->ac, &gs_vtxptr[2], false);953954LLVMValueRef gs_accepted = ac_build_alloca(&ctx->ac, ctx->ac.i32, "");955956/* Do culling in GS threads. */957ac_build_ifcc(&ctx->ac, si_is_gs_thread(ctx), 16002);958{959/* Load positions. */960LLVMValueRef pos[3][4] = {};961for (unsigned vtx = 0; vtx < 3; vtx++) {962for (unsigned chan = 0; chan < 4; chan++) {963unsigned index;964if (chan == 0 || chan == 1)965index = lds_pos_cull_x_div_w + chan;966else if (chan == 3)967index = lds_pos_cull_w;968else969continue;970971LLVMValueRef addr =972ac_build_gep0(&ctx->ac, gs_vtxptr[vtx], LLVMConstInt(ctx->ac.i32, index, 0));973pos[vtx][chan] = LLVMBuildLoad(builder, addr, "");974pos[vtx][chan] = ac_to_float(&ctx->ac, pos[vtx][chan]);975}976}977978/* Load the viewport state for small prim culling. */979LLVMValueRef vp = ac_build_load_invariant(980&ctx->ac, ac_get_arg(&ctx->ac, ctx->small_prim_cull_info), ctx->ac.i32_0);981vp = LLVMBuildBitCast(builder, vp, ctx->ac.v4f32, "");982LLVMValueRef vp_scale[2], vp_translate[2];983vp_scale[0] = ac_llvm_extract_elem(&ctx->ac, vp, 0);984vp_scale[1] = ac_llvm_extract_elem(&ctx->ac, vp, 1);985vp_translate[0] = ac_llvm_extract_elem(&ctx->ac, vp, 2);986vp_translate[1] = ac_llvm_extract_elem(&ctx->ac, vp, 3);987988/* Get the small prim filter precision. */989LLVMValueRef small_prim_precision = si_unpack_param(ctx, ctx->vs_state_bits, 7, 4);990small_prim_precision =991LLVMBuildOr(builder, small_prim_precision, LLVMConstInt(ctx->ac.i32, 0x70, 0), "");992small_prim_precision =993LLVMBuildShl(builder, small_prim_precision, LLVMConstInt(ctx->ac.i32, 23, 0), "");994small_prim_precision = LLVMBuildBitCast(builder, small_prim_precision, ctx->ac.f32, "");995996/* Execute culling code. */997struct ac_cull_options options = {};998options.cull_front = shader->key.opt.ngg_culling & SI_NGG_CULL_FRONT_FACE;999options.cull_back = shader->key.opt.ngg_culling & SI_NGG_CULL_BACK_FACE;1000options.cull_view_xy = shader->key.opt.ngg_culling & SI_NGG_CULL_VIEW_SMALLPRIMS;1001options.cull_small_prims = options.cull_view_xy;1002options.cull_zero_area = options.cull_front || options.cull_back;1003options.cull_w = true;10041005/* Tell ES threads whether their vertex survived. */1006LLVMValueRef params[] = {1007gs_accepted,1008(void*)gs_vtxptr,1009};1010ac_cull_triangle(&ctx->ac, pos, ctx->ac.i1true, vp_scale, vp_translate,1011small_prim_precision, &options,1012gfx10_build_primitive_accepted, params);1013}1014ac_build_endif(&ctx->ac, 16002);1015ac_build_s_barrier(&ctx->ac);10161017gs_accepted = LLVMBuildLoad(builder, gs_accepted, "");10181019LLVMValueRef vertex_accepted = ac_build_alloca(&ctx->ac, ctx->ac.i1, "");1020LLVMValueRef vertex_mask = ac_build_alloca(&ctx->ac, ctx->ac.iN_wavemask, "");10211022/* Convert the per-vertex accept flag to a vertex thread mask, store it in registers. */1023ac_build_ifcc(&ctx->ac, si_is_es_thread(ctx), 16007);1024{1025LLVMValueRef accepted =1026LLVMBuildLoad(builder, si_build_gep_i8(ctx, es_vtxptr, lds_byte0_accept_flag), "");1027accepted = LLVMBuildICmp(builder, LLVMIntNE, accepted, ctx->ac.i8_0, "");1028LLVMValueRef mask = ac_get_i1_sgpr_mask(&ctx->ac, accepted);10291030LLVMBuildStore(builder, accepted, vertex_accepted);1031LLVMBuildStore(builder, mask, vertex_mask);1032}1033ac_build_endif(&ctx->ac, 16007);10341035/* Store the per-wave vertex count to LDS. Non-ES waves store 0. */1036vertex_mask = LLVMBuildLoad(builder, vertex_mask, "");1037ac_build_ifcc(&ctx->ac, LLVMBuildICmp(builder, LLVMIntEQ, tid, ctx->ac.i32_0, ""), 16008);1038{1039LLVMValueRef vertex_count = ac_build_bit_count(&ctx->ac, vertex_mask);1040LLVMBuildStore(builder, LLVMBuildTrunc(builder, vertex_count, ctx->ac.i8, ""),1041si_build_gep_i8_var(ctx, ctx->gs_ngg_scratch, get_wave_id_in_tg(ctx)));1042}1043ac_build_endif(&ctx->ac, 16008);10441045ac_build_s_barrier(&ctx->ac);10461047/* Load the vertex masks and compute the new ES thread count. */1048LLVMValueRef new_num_es_threads, prefix_sum, kill_wave;1049load_vertex_counts(ctx, ctx->gs_ngg_scratch, max_waves, tid, &new_num_es_threads,1050&prefix_sum);10511052bool uses_instance_id = ctx->stage == MESA_SHADER_VERTEX &&1053(sel->info.uses_instanceid ||1054shader->key.part.vs.prolog.instance_divisor_is_one ||1055shader->key.part.vs.prolog.instance_divisor_is_fetched);1056bool uses_tes_prim_id = ctx->stage == MESA_SHADER_TESS_EVAL &&1057(sel->info.uses_primid || shader->key.mono.u.vs_export_prim_id);10581059/* ES threads compute their prefix sum, which is the new ES thread ID.1060* Then they write the vertex position and input VGPRs into the LDS address1061* of the new thread ID. It will be used to load input VGPRs by compacted1062* threads.1063*/1064vertex_accepted = LLVMBuildLoad(builder, vertex_accepted, "");1065ac_build_ifcc(&ctx->ac, vertex_accepted, 16009);1066{1067/* Add the number of bits set in vertex_mask up to the current thread ID - 11068* to get the prefix sum.1069*/1070prefix_sum = LLVMBuildAdd(builder, prefix_sum, ac_build_mbcnt(&ctx->ac, vertex_mask), "");10711072LLVMValueRef new_id = prefix_sum;1073LLVMValueRef new_vtx = ngg_nogs_vertex_ptr(ctx, new_id);10741075LLVMBuildStore(builder, LLVMBuildTrunc(builder, new_id, ctx->ac.i8, ""),1076si_build_gep_i8(ctx, es_vtxptr, lds_byte1_new_thread_id));10771078/* Store Position.XYZW into LDS. */1079for (unsigned chan = 0; chan < 4; chan++) {1080LLVMBuildStore(1081builder, ac_to_integer(&ctx->ac, LLVMBuildLoad(builder, addrs[4 * pos_index + chan], "")),1082ac_build_gep0(&ctx->ac, new_vtx, LLVMConstInt(ctx->ac.i32, lds_pos_x + chan, 0)));1083}10841085/* Store VertexID and InstanceID into LDS. ES threads will have to load them1086* from LDS after vertex compaction and use them instead of their own1087* system values.1088*/1089if (ctx->stage == MESA_SHADER_VERTEX) {1090LLVMBuildStore(1091builder, ctx->abi.vertex_id,1092ac_build_gep0(&ctx->ac, new_vtx, LLVMConstInt(ctx->ac.i32, lds_vertex_id, 0)));1093if (uses_instance_id) {1094LLVMBuildStore(1095builder, ctx->abi.instance_id,1096ac_build_gep0(&ctx->ac, new_vtx, LLVMConstInt(ctx->ac.i32, lds_instance_id, 0)));1097}1098} else {1099assert(ctx->stage == MESA_SHADER_TESS_EVAL);1100LLVMBuildStore(builder, ac_to_integer(&ctx->ac, ac_get_arg(&ctx->ac, ctx->args.tes_u)),1101ac_build_gep0(&ctx->ac, new_vtx, LLVMConstInt(ctx->ac.i32, lds_tes_u, 0)));1102LLVMBuildStore(builder, ac_to_integer(&ctx->ac, ac_get_arg(&ctx->ac, ctx->args.tes_v)),1103ac_build_gep0(&ctx->ac, new_vtx, LLVMConstInt(ctx->ac.i32, lds_tes_v, 0)));1104LLVMBuildStore(builder, LLVMBuildTrunc(builder, ac_get_arg(&ctx->ac, ctx->args.tes_rel_patch_id), ctx->ac.i8, ""),1105si_build_gep_i8(ctx, new_vtx, lds_byte2_tes_rel_patch_id));1106if (uses_tes_prim_id) {1107LLVMBuildStore(1108builder, ac_get_arg(&ctx->ac, ctx->args.tes_patch_id),1109ac_build_gep0(&ctx->ac, new_vtx, LLVMConstInt(ctx->ac.i32, lds_tes_patch_id, 0)));1110}1111}1112}1113ac_build_endif(&ctx->ac, 16009);11141115/* If all vertices are culled, set the primitive count to 0, so that all waves are culled here. */1116LLVMValueRef num_primitives = ngg_get_prim_cnt(ctx);1117num_primitives = LLVMBuildSelect(builder,1118LLVMBuildICmp(builder, LLVMIntEQ, new_num_es_threads,1119ctx->ac.i32_0, ""),1120ctx->ac.i32_0, num_primitives, "");1121/* Kill waves that have inactive threads. */1122kill_wave = LLVMBuildICmp(builder, LLVMIntULE,1123ac_build_imax(&ctx->ac, new_num_es_threads, num_primitives),1124LLVMBuildMul(builder, get_wave_id_in_tg(ctx),1125LLVMConstInt(ctx->ac.i32, ctx->ac.wave_size, 0), ""),1126"");1127ac_build_ifcc(&ctx->ac, kill_wave, 19202);1128{1129/* If we are killing wave 0, send that there are no primitives1130* in this threadgroup.1131*/1132ac_build_sendmsg_gs_alloc_req(&ctx->ac, get_wave_id_in_tg(ctx), ctx->ac.i32_0, ctx->ac.i32_0);1133ac_build_s_endpgm(&ctx->ac);1134}1135ac_build_endif(&ctx->ac, 19202);1136ac_build_s_barrier(&ctx->ac);11371138/* Send the final vertex and primitive counts. */1139ac_build_sendmsg_gs_alloc_req(&ctx->ac, get_wave_id_in_tg(ctx), new_num_es_threads,1140ngg_get_prim_cnt(ctx));11411142/* Update thread counts in SGPRs. */1143LLVMValueRef new_gs_tg_info = ac_get_arg(&ctx->ac, ctx->args.gs_tg_info);1144LLVMValueRef new_merged_wave_info = ac_get_arg(&ctx->ac, ctx->args.merged_wave_info);11451146/* This also converts the thread count from the total count to the per-wave count. */1147update_thread_counts(ctx, &new_num_es_threads, &new_gs_tg_info, 9, 12, &new_merged_wave_info, 8,11480);11491150/* Update vertex indices in VGPR0 (same format as NGG passthrough).1151*1152* Set the null flag at the beginning (culled), and then1153* overwrite it for accepted primitives.1154*/1155LLVMValueRef new_vgpr0 =1156ac_build_alloca_init(&ctx->ac, LLVMConstInt(ctx->ac.i32, 1u << 31, 0), "");11571158/* Get vertex indices after vertex compaction. */1159ac_build_ifcc(&ctx->ac, LLVMBuildTrunc(builder, gs_accepted, ctx->ac.i1, ""), 16011);1160{1161struct ac_ngg_prim prim = {};1162prim.num_vertices = 3;1163prim.isnull = ctx->ac.i1false;11641165for (unsigned vtx = 0; vtx < 3; vtx++) {1166prim.index[vtx] = LLVMBuildLoad(1167builder, si_build_gep_i8(ctx, gs_vtxptr[vtx], lds_byte1_new_thread_id), "");1168prim.index[vtx] = LLVMBuildZExt(builder, prim.index[vtx], ctx->ac.i32, "");1169prim.edgeflag[vtx] = ngg_get_initial_edgeflag(ctx, vtx);1170}11711172/* Set the new GS input VGPR. */1173LLVMBuildStore(builder, ac_pack_prim_export(&ctx->ac, &prim), new_vgpr0);1174}1175ac_build_endif(&ctx->ac, 16011);11761177if (gfx10_ngg_export_prim_early(shader))1178gfx10_ngg_build_export_prim(ctx, NULL, LLVMBuildLoad(builder, new_vgpr0, ""));11791180/* Prepare LDS addresses of the new ES input VGPRs. */1181LLVMValueRef input_vgpr_addresses[4] = {1182ac_build_gep0(&ctx->ac, es_vtxptr, LLVMConstInt(ctx->ac.i32, lds_vertex_id, 0)),1183ac_build_gep0(&ctx->ac, es_vtxptr, LLVMConstInt(ctx->ac.i32, lds_instance_id, 0)),1184};1185if (ctx->stage == MESA_SHADER_TESS_EVAL) {1186input_vgpr_addresses[2] = si_build_gep_i8(ctx, es_vtxptr, lds_byte2_tes_rel_patch_id);1187if (uses_tes_prim_id) {1188input_vgpr_addresses[3] = ac_build_gep0(&ctx->ac, es_vtxptr,1189LLVMConstInt(ctx->ac.i32, lds_tes_patch_id, 0));1190}1191}11921193/* Return values for the main function. */1194LLVMValueRef ret = ctx->return_value;1195LLVMValueRef val;11961197ret = LLVMBuildInsertValue(ctx->ac.builder, ret, new_gs_tg_info, 2, "");1198ret = LLVMBuildInsertValue(ctx->ac.builder, ret, new_merged_wave_info, 3, "");1199if (ctx->stage == MESA_SHADER_TESS_EVAL)1200ret = si_insert_input_ret(ctx, ret, ctx->args.tess_offchip_offset, 4);12011202ret = si_insert_input_ptr(ctx, ret, ctx->internal_bindings, 8 + SI_SGPR_INTERNAL_BINDINGS);1203ret = si_insert_input_ptr(ctx, ret, ctx->bindless_samplers_and_images,12048 + SI_SGPR_BINDLESS_SAMPLERS_AND_IMAGES);1205ret = si_insert_input_ptr(ctx, ret, ctx->const_and_shader_buffers,12068 + SI_SGPR_CONST_AND_SHADER_BUFFERS);1207ret = si_insert_input_ptr(ctx, ret, ctx->samplers_and_images, 8 + SI_SGPR_SAMPLERS_AND_IMAGES);1208ret = si_insert_input_ptr(ctx, ret, ctx->vs_state_bits, 8 + SI_SGPR_VS_STATE_BITS);12091210if (ctx->stage == MESA_SHADER_VERTEX) {1211ret = si_insert_input_ptr(ctx, ret, ctx->args.base_vertex, 8 + SI_SGPR_BASE_VERTEX);1212ret = si_insert_input_ptr(ctx, ret, ctx->args.draw_id, 8 + SI_SGPR_DRAWID);1213ret = si_insert_input_ptr(ctx, ret, ctx->args.start_instance, 8 + SI_SGPR_START_INSTANCE);1214ret = si_insert_input_ptr(ctx, ret, ctx->args.vertex_buffers, 8 + SI_VS_NUM_USER_SGPR);12151216for (unsigned i = 0; i < shader->selector->num_vbos_in_user_sgprs; i++) {1217ret = si_insert_input_v4i32(ctx, ret, ctx->vb_descriptors[i],12188 + SI_SGPR_VS_VB_DESCRIPTOR_FIRST + i * 4);1219}1220} else {1221assert(ctx->stage == MESA_SHADER_TESS_EVAL);1222ret = si_insert_input_ptr(ctx, ret, ctx->tcs_offchip_layout, 8 + SI_SGPR_TES_OFFCHIP_LAYOUT);1223ret = si_insert_input_ptr(ctx, ret, ctx->tes_offchip_addr, 8 + SI_SGPR_TES_OFFCHIP_ADDR);1224}12251226unsigned vgpr;1227if (ctx->stage == MESA_SHADER_VERTEX) {1228if (shader->selector->num_vbos_in_user_sgprs) {1229vgpr = 8 + SI_SGPR_VS_VB_DESCRIPTOR_FIRST + shader->selector->num_vbos_in_user_sgprs * 4;1230} else {1231vgpr = 8 + GFX9_VSGS_NUM_USER_SGPR + 1;1232}1233} else {1234vgpr = 8 + GFX9_TESGS_NUM_USER_SGPR;1235}12361237val = LLVMBuildLoad(builder, new_vgpr0, "");1238ret = LLVMBuildInsertValue(builder, ret, ac_to_float(&ctx->ac, val), vgpr++, "");1239vgpr++; /* gs_vtx23_offset */12401241ret = si_insert_input_ret_float(ctx, ret, ctx->args.gs_prim_id, vgpr++);1242ret = si_insert_input_ret_float(ctx, ret, ctx->args.gs_invocation_id, vgpr++);1243vgpr++; /* gs_vtx45_offset */12441245/* Set the input VPGRs to the corresponding LDS addresses where the VGPR values are1246* stored. The VS prolog will load them.1247*/1248if (ctx->stage == MESA_SHADER_VERTEX) {1249val = LLVMBuildPtrToInt(builder, input_vgpr_addresses[0], ctx->ac.i32, "");1250ret = LLVMBuildInsertValue(builder, ret, ac_to_float(&ctx->ac, val), vgpr++,1251""); /* VGPR5 - VertexID */1252vgpr += 2;1253if (uses_instance_id) {1254val = LLVMBuildPtrToInt(builder, input_vgpr_addresses[1], ctx->ac.i32, "");1255ret = LLVMBuildInsertValue(builder, ret, ac_to_float(&ctx->ac, val), vgpr++,1256""); /* VGPR8 - InstanceID */1257} else {1258vgpr++;1259}1260} else {1261assert(ctx->stage == MESA_SHADER_TESS_EVAL);1262unsigned num_vgprs = uses_tes_prim_id ? 4 : 3;1263for (unsigned i = 0; i < num_vgprs; i++) {1264val = LLVMBuildPtrToInt(builder, input_vgpr_addresses[i], ctx->ac.i32, "");1265ret = LLVMBuildInsertValue(builder, ret, ac_to_float(&ctx->ac, val), vgpr++, "");1266}1267if (num_vgprs == 3)1268vgpr++;1269}12701271/* These two also use LDS. */1272if (sel->info.writes_edgeflag ||1273(ctx->stage == MESA_SHADER_VERTEX && shader->key.mono.u.vs_export_prim_id))1274ac_build_s_barrier(&ctx->ac);12751276ctx->return_value = ret;1277}12781279/**1280* Emit the epilogue of an API VS or TES shader compiled as ESGS shader.1281*/1282void gfx10_emit_ngg_epilogue(struct ac_shader_abi *abi, unsigned max_outputs, LLVMValueRef *addrs)1283{1284struct si_shader_context *ctx = si_shader_context_from_abi(abi);1285struct si_shader_selector *sel = ctx->shader->selector;1286struct si_shader_info *info = &sel->info;1287struct si_shader_output_values outputs[PIPE_MAX_SHADER_OUTPUTS];1288LLVMBuilderRef builder = ctx->ac.builder;1289LLVMValueRef tmp, tmp2;12901291assert(!ctx->shader->is_gs_copy_shader);1292assert(info->num_outputs <= max_outputs);12931294LLVMValueRef vertex_ptr = NULL;12951296if (sel->so.num_outputs || sel->info.writes_edgeflag)1297vertex_ptr = ngg_nogs_vertex_ptr(ctx, get_thread_id_in_tg(ctx));12981299for (unsigned i = 0; i < info->num_outputs; i++) {1300outputs[i].semantic = info->output_semantic[i];13011302for (unsigned j = 0; j < 4; j++) {1303outputs[i].vertex_stream[j] = (info->output_streams[i] >> (2 * j)) & 3;13041305/* TODO: we may store more outputs than streamout needs,1306* but streamout performance isn't that important.1307*/1308if (sel->so.num_outputs) {1309tmp = ac_build_gep0(&ctx->ac, vertex_ptr, LLVMConstInt(ctx->ac.i32, 4 * i + j, false));1310tmp2 = LLVMBuildLoad(builder, addrs[4 * i + j], "");1311tmp2 = ac_to_integer(&ctx->ac, tmp2);1312LLVMBuildStore(builder, tmp2, tmp);1313}1314}13151316/* Store the edgeflag at the end (if streamout is enabled) */1317if (info->output_semantic[i] == VARYING_SLOT_EDGE && sel->info.writes_edgeflag) {1318LLVMValueRef edgeflag = LLVMBuildLoad(builder, addrs[4 * i], "");1319/* The output is a float, but the hw expects a 1-bit integer. */1320edgeflag = LLVMBuildFPToUI(ctx->ac.builder, edgeflag, ctx->ac.i32, "");1321edgeflag = ac_build_umin(&ctx->ac, edgeflag, ctx->ac.i32_1);13221323tmp = LLVMConstInt(ctx->ac.i32, ngg_nogs_vertex_size(ctx->shader) - 1, 0);1324tmp = ac_build_gep0(&ctx->ac, vertex_ptr, tmp);1325LLVMBuildStore(builder, edgeflag, tmp);1326}1327}13281329bool unterminated_es_if_block =1330!sel->so.num_outputs && !sel->info.writes_edgeflag &&1331!ctx->screen->use_ngg_streamout && /* no query buffer */1332(ctx->stage != MESA_SHADER_VERTEX || !ctx->shader->key.mono.u.vs_export_prim_id);13331334if (!unterminated_es_if_block)1335ac_build_endif(&ctx->ac, ctx->merged_wrap_if_label);13361337LLVMValueRef is_gs_thread = si_is_gs_thread(ctx);1338LLVMValueRef is_es_thread = si_is_es_thread(ctx);1339LLVMValueRef vtxindex[3];13401341if (ctx->shader->key.opt.ngg_culling) {1342vtxindex[0] = si_unpack_param(ctx, ctx->gs_vtx01_offset, 0, 9);1343vtxindex[1] = si_unpack_param(ctx, ctx->gs_vtx01_offset, 10, 9);1344vtxindex[2] = si_unpack_param(ctx, ctx->gs_vtx01_offset, 20, 9);1345} else {1346vtxindex[0] = si_unpack_param(ctx, ctx->gs_vtx01_offset, 0, 16);1347vtxindex[1] = si_unpack_param(ctx, ctx->gs_vtx01_offset, 16, 16);1348vtxindex[2] = si_unpack_param(ctx, ctx->gs_vtx23_offset, 0, 16);1349}13501351/* Determine the number of vertices per primitive. */1352unsigned num_vertices;1353LLVMValueRef num_vertices_val = ngg_get_vertices_per_prim(ctx, &num_vertices);13541355/* Streamout */1356LLVMValueRef emitted_prims = NULL;13571358if (sel->so.num_outputs) {1359assert(!unterminated_es_if_block);13601361struct ngg_streamout nggso = {};1362nggso.num_vertices = num_vertices_val;1363nggso.prim_enable[0] = is_gs_thread;13641365for (unsigned i = 0; i < num_vertices; ++i)1366nggso.vertices[i] = ngg_nogs_vertex_ptr(ctx, vtxindex[i]);13671368build_streamout(ctx, &nggso);1369emitted_prims = nggso.emit[0];1370}13711372LLVMValueRef user_edgeflags[3] = {};13731374if (sel->info.writes_edgeflag) {1375assert(!unterminated_es_if_block);13761377/* Streamout already inserted the barrier, so don't insert it again. */1378if (!sel->so.num_outputs)1379ac_build_s_barrier(&ctx->ac);13801381ac_build_ifcc(&ctx->ac, is_gs_thread, 5400);1382/* Load edge flags from ES threads and store them into VGPRs in GS threads. */1383for (unsigned i = 0; i < num_vertices; i++) {1384tmp = ngg_nogs_vertex_ptr(ctx, vtxindex[i]);1385tmp2 = LLVMConstInt(ctx->ac.i32, ngg_nogs_vertex_size(ctx->shader) - 1, 0);1386tmp = ac_build_gep0(&ctx->ac, tmp, tmp2);1387tmp = LLVMBuildLoad(builder, tmp, "");1388tmp = LLVMBuildTrunc(builder, tmp, ctx->ac.i1, "");13891390user_edgeflags[i] = ac_build_alloca_init(&ctx->ac, tmp, "");1391}1392ac_build_endif(&ctx->ac, 5400);1393}13941395/* Copy Primitive IDs from GS threads to the LDS address corresponding1396* to the ES thread of the provoking vertex.1397*/1398if (ctx->stage == MESA_SHADER_VERTEX && ctx->shader->key.mono.u.vs_export_prim_id) {1399assert(!unterminated_es_if_block);14001401/* Streamout and edge flags use LDS. Make it idle, so that we can reuse it. */1402if (sel->so.num_outputs || sel->info.writes_edgeflag)1403ac_build_s_barrier(&ctx->ac);14041405ac_build_ifcc(&ctx->ac, is_gs_thread, 5400);1406/* Extract the PROVOKING_VTX_INDEX field. */1407LLVMValueRef provoking_vtx_in_prim = si_unpack_param(ctx, ctx->vs_state_bits, 4, 2);14081409/* provoking_vtx_index = vtxindex[provoking_vtx_in_prim]; */1410LLVMValueRef indices = ac_build_gather_values(&ctx->ac, vtxindex, 3);1411LLVMValueRef provoking_vtx_index =1412LLVMBuildExtractElement(builder, indices, provoking_vtx_in_prim, "");1413LLVMValueRef vertex_ptr = ngg_nogs_vertex_ptr(ctx, provoking_vtx_index);14141415LLVMBuildStore(builder, ac_get_arg(&ctx->ac, ctx->args.gs_prim_id),1416ac_build_gep0(&ctx->ac, vertex_ptr, ctx->ac.i32_0));1417ac_build_endif(&ctx->ac, 5400);1418}14191420/* Update query buffer */1421if (ctx->screen->use_ngg_streamout && !info->base.vs.blit_sgprs_amd) {1422assert(!unterminated_es_if_block);14231424tmp = si_unpack_param(ctx, ctx->vs_state_bits, 6, 1);1425tmp = LLVMBuildTrunc(builder, tmp, ctx->ac.i1, "");1426ac_build_ifcc(&ctx->ac, tmp, 5029); /* if (STREAMOUT_QUERY_ENABLED) */1427tmp = LLVMBuildICmp(builder, LLVMIntEQ, get_wave_id_in_tg(ctx), ctx->ac.i32_0, "");1428ac_build_ifcc(&ctx->ac, tmp, 5030);1429tmp = LLVMBuildICmp(builder, LLVMIntULE, ac_get_thread_id(&ctx->ac),1430sel->so.num_outputs ? ctx->ac.i32_1 : ctx->ac.i32_0, "");1431ac_build_ifcc(&ctx->ac, tmp, 5031);1432{1433LLVMValueRef args[] = {1434ngg_get_prim_cnt(ctx),1435ngg_get_query_buf(ctx),1436LLVMConstInt(ctx->ac.i32, 16, false), /* offset of stream[0].generated_primitives */1437ctx->ac.i32_0, /* soffset */1438ctx->ac.i32_0, /* cachepolicy */1439};14401441if (sel->so.num_outputs) {1442args[0] = ac_build_writelane(&ctx->ac, args[0], emitted_prims, ctx->ac.i32_1);1443args[2] = ac_build_writelane(&ctx->ac, args[2], LLVMConstInt(ctx->ac.i32, 24, false),1444ctx->ac.i32_1);1445}14461447/* TODO: should this be 64-bit atomics? */1448ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.raw.buffer.atomic.add.i32", ctx->ac.i32, args, 5,14490);1450}1451ac_build_endif(&ctx->ac, 5031);1452ac_build_endif(&ctx->ac, 5030);1453ac_build_endif(&ctx->ac, 5029);1454}14551456/* Build the primitive export. */1457if (!gfx10_ngg_export_prim_early(ctx->shader)) {1458assert(!unterminated_es_if_block);1459gfx10_ngg_build_export_prim(ctx, user_edgeflags, NULL);1460}14611462/* Export per-vertex data (positions and parameters). */1463if (!unterminated_es_if_block)1464ac_build_ifcc(&ctx->ac, is_es_thread, 6002);1465{1466unsigned i;14671468/* Unconditionally (re-)load the values for proper SSA form. */1469for (i = 0; i < info->num_outputs; i++) {1470/* If the NGG cull shader part computed the position, don't1471* use the position from the current shader part. Instead,1472* load it from LDS.1473*/1474if (info->output_semantic[i] == VARYING_SLOT_POS &&1475ctx->shader->key.opt.ngg_culling) {1476vertex_ptr = ngg_nogs_vertex_ptr(ctx, get_thread_id_in_tg(ctx));14771478for (unsigned j = 0; j < 4; j++) {1479tmp = LLVMConstInt(ctx->ac.i32, lds_pos_x + j, 0);1480tmp = ac_build_gep0(&ctx->ac, vertex_ptr, tmp);1481tmp = LLVMBuildLoad(builder, tmp, "");1482outputs[i].values[j] = ac_to_float(&ctx->ac, tmp);1483}1484} else {1485for (unsigned j = 0; j < 4; j++) {1486outputs[i].values[j] = LLVMBuildLoad(builder, addrs[4 * i + j], "");1487}1488}1489}14901491if (ctx->shader->key.mono.u.vs_export_prim_id) {1492outputs[i].semantic = VARYING_SLOT_PRIMITIVE_ID;14931494if (ctx->stage == MESA_SHADER_VERTEX) {1495/* Wait for GS stores to finish. */1496ac_build_s_barrier(&ctx->ac);14971498tmp = ngg_nogs_vertex_ptr(ctx, get_thread_id_in_tg(ctx));1499tmp = ac_build_gep0(&ctx->ac, tmp, ctx->ac.i32_0);1500outputs[i].values[0] = LLVMBuildLoad(builder, tmp, "");1501} else {1502assert(ctx->stage == MESA_SHADER_TESS_EVAL);1503outputs[i].values[0] = si_get_primitive_id(ctx, 0);1504}15051506outputs[i].values[0] = ac_to_float(&ctx->ac, outputs[i].values[0]);1507for (unsigned j = 1; j < 4; j++)1508outputs[i].values[j] = LLVMGetUndef(ctx->ac.f32);15091510memset(outputs[i].vertex_stream, 0, sizeof(outputs[i].vertex_stream));1511i++;1512}15131514si_llvm_build_vs_exports(ctx, outputs, i);1515}1516ac_build_endif(&ctx->ac, 6002);1517}15181519static LLVMValueRef ngg_gs_get_vertex_storage(struct si_shader_context *ctx)1520{1521const struct si_shader_selector *sel = ctx->shader->selector;1522const struct si_shader_info *info = &sel->info;15231524LLVMTypeRef elements[2] = {1525LLVMArrayType(ctx->ac.i32, 4 * info->num_outputs),1526LLVMArrayType(ctx->ac.i8, 4),1527};1528LLVMTypeRef type = LLVMStructTypeInContext(ctx->ac.context, elements, 2, false);1529type = LLVMPointerType(LLVMArrayType(type, 0), AC_ADDR_SPACE_LDS);1530return LLVMBuildBitCast(ctx->ac.builder, ctx->gs_ngg_emit, type, "");1531}15321533/**1534* Return a pointer to the LDS storage reserved for the N'th vertex, where N1535* is in emit order; that is:1536* - during the epilogue, N is the threadidx (relative to the entire threadgroup)1537* - during vertex emit, i.e. while the API GS shader invocation is running,1538* N = threadidx * gs.vertices_out + emitidx1539*1540* Goals of the LDS memory layout:1541* 1. Eliminate bank conflicts on write for geometry shaders that have all emits1542* in uniform control flow1543* 2. Eliminate bank conflicts on read for export if, additionally, there is no1544* culling1545* 3. Agnostic to the number of waves (since we don't know it before compiling)1546* 4. Allow coalescing of LDS instructions (ds_write_b128 etc.)1547* 5. Avoid wasting memory.1548*1549* We use an AoS layout due to point 4 (this also helps point 3). In an AoS1550* layout, elimination of bank conflicts requires that each vertex occupy an1551* odd number of dwords. We use the additional dword to store the output stream1552* index as well as a flag to indicate whether this vertex ends a primitive1553* for rasterization.1554*1555* Swizzling is required to satisfy points 1 and 2 simultaneously.1556*1557* Vertices are stored in export order (gsthread * gs.vertices_out + emitidx).1558* Indices are swizzled in groups of 32, which ensures point 1 without1559* disturbing point 2.1560*1561* \return an LDS pointer to type {[N x i32], [4 x i8]}1562*/1563static LLVMValueRef ngg_gs_vertex_ptr(struct si_shader_context *ctx, LLVMValueRef vertexidx)1564{1565struct si_shader_selector *sel = ctx->shader->selector;1566LLVMBuilderRef builder = ctx->ac.builder;1567LLVMValueRef storage = ngg_gs_get_vertex_storage(ctx);15681569/* gs.vertices_out = 2^(write_stride_2exp) * some odd number */1570unsigned write_stride_2exp = ffs(sel->info.base.gs.vertices_out) - 1;1571if (write_stride_2exp) {1572LLVMValueRef row = LLVMBuildLShr(builder, vertexidx, LLVMConstInt(ctx->ac.i32, 5, false), "");1573LLVMValueRef swizzle = LLVMBuildAnd(1574builder, row, LLVMConstInt(ctx->ac.i32, (1u << write_stride_2exp) - 1, false), "");1575vertexidx = LLVMBuildXor(builder, vertexidx, swizzle, "");1576}15771578return ac_build_gep0(&ctx->ac, storage, vertexidx);1579}15801581static LLVMValueRef ngg_gs_emit_vertex_ptr(struct si_shader_context *ctx, LLVMValueRef gsthread,1582LLVMValueRef emitidx)1583{1584struct si_shader_selector *sel = ctx->shader->selector;1585LLVMBuilderRef builder = ctx->ac.builder;1586LLVMValueRef tmp;15871588tmp = LLVMConstInt(ctx->ac.i32, sel->info.base.gs.vertices_out, false);1589tmp = LLVMBuildMul(builder, tmp, gsthread, "");1590const LLVMValueRef vertexidx = LLVMBuildAdd(builder, tmp, emitidx, "");1591return ngg_gs_vertex_ptr(ctx, vertexidx);1592}15931594static LLVMValueRef ngg_gs_get_emit_output_ptr(struct si_shader_context *ctx,1595LLVMValueRef vertexptr, unsigned out_idx)1596{1597LLVMValueRef gep_idx[3] = {1598ctx->ac.i32_0, /* implied C-style array */1599ctx->ac.i32_0, /* first struct entry */1600LLVMConstInt(ctx->ac.i32, out_idx, false),1601};1602return LLVMBuildGEP(ctx->ac.builder, vertexptr, gep_idx, 3, "");1603}16041605static LLVMValueRef ngg_gs_get_emit_primflag_ptr(struct si_shader_context *ctx,1606LLVMValueRef vertexptr, unsigned stream)1607{1608LLVMValueRef gep_idx[3] = {1609ctx->ac.i32_0, /* implied C-style array */1610ctx->ac.i32_1, /* second struct entry */1611LLVMConstInt(ctx->ac.i32, stream, false),1612};1613return LLVMBuildGEP(ctx->ac.builder, vertexptr, gep_idx, 3, "");1614}16151616void gfx10_ngg_gs_emit_vertex(struct si_shader_context *ctx, unsigned stream, LLVMValueRef *addrs)1617{1618const struct si_shader_selector *sel = ctx->shader->selector;1619const struct si_shader_info *info = &sel->info;1620LLVMBuilderRef builder = ctx->ac.builder;1621LLVMValueRef tmp;1622const LLVMValueRef vertexidx = LLVMBuildLoad(builder, ctx->gs_next_vertex[stream], "");16231624/* If this thread has already emitted the declared maximum number of1625* vertices, skip the write: excessive vertex emissions are not1626* supposed to have any effect.1627*/1628const LLVMValueRef can_emit =1629LLVMBuildICmp(builder, LLVMIntULT, vertexidx,1630LLVMConstInt(ctx->ac.i32, sel->info.base.gs.vertices_out, false), "");16311632tmp = LLVMBuildAdd(builder, vertexidx, ctx->ac.i32_1, "");1633tmp = LLVMBuildSelect(builder, can_emit, tmp, vertexidx, "");1634LLVMBuildStore(builder, tmp, ctx->gs_next_vertex[stream]);16351636ac_build_ifcc(&ctx->ac, can_emit, 9001);16371638const LLVMValueRef vertexptr = ngg_gs_emit_vertex_ptr(ctx, get_thread_id_in_tg(ctx), vertexidx);1639unsigned out_idx = 0;1640for (unsigned i = 0; i < info->num_outputs; i++) {1641for (unsigned chan = 0; chan < 4; chan++, out_idx++) {1642if (!(info->output_usagemask[i] & (1 << chan)) ||1643((info->output_streams[i] >> (2 * chan)) & 3) != stream)1644continue;16451646LLVMValueRef out_val = LLVMBuildLoad(builder, addrs[4 * i + chan], "");1647out_val = ac_to_integer(&ctx->ac, out_val);1648LLVMBuildStore(builder, out_val, ngg_gs_get_emit_output_ptr(ctx, vertexptr, out_idx));1649}1650}1651assert(out_idx * 4 == sel->gsvs_vertex_size);16521653/* Determine and store whether this vertex completed a primitive. */1654const LLVMValueRef curverts = LLVMBuildLoad(builder, ctx->gs_curprim_verts[stream], "");16551656tmp = LLVMConstInt(ctx->ac.i32, u_vertices_per_prim(sel->info.base.gs.output_primitive) - 1, false);1657const LLVMValueRef iscompleteprim = LLVMBuildICmp(builder, LLVMIntUGE, curverts, tmp, "");16581659/* Since the geometry shader emits triangle strips, we need to1660* track which primitive is odd and swap vertex indices to get1661* the correct vertex order.1662*/1663LLVMValueRef is_odd = ctx->ac.i1false;1664if (stream == 0 && u_vertices_per_prim(sel->info.base.gs.output_primitive) == 3) {1665tmp = LLVMBuildAnd(builder, curverts, ctx->ac.i32_1, "");1666is_odd = LLVMBuildICmp(builder, LLVMIntEQ, tmp, ctx->ac.i32_1, "");1667}16681669tmp = LLVMBuildAdd(builder, curverts, ctx->ac.i32_1, "");1670LLVMBuildStore(builder, tmp, ctx->gs_curprim_verts[stream]);16711672/* The per-vertex primitive flag encoding:1673* bit 0: whether this vertex finishes a primitive1674* bit 1: whether the primitive is odd (if we are emitting triangle strips)1675*/1676tmp = LLVMBuildZExt(builder, iscompleteprim, ctx->ac.i8, "");1677tmp = LLVMBuildOr(1678builder, tmp,1679LLVMBuildShl(builder, LLVMBuildZExt(builder, is_odd, ctx->ac.i8, ""), ctx->ac.i8_1, ""), "");1680LLVMBuildStore(builder, tmp, ngg_gs_get_emit_primflag_ptr(ctx, vertexptr, stream));16811682tmp = LLVMBuildLoad(builder, ctx->gs_generated_prims[stream], "");1683tmp = LLVMBuildAdd(builder, tmp, LLVMBuildZExt(builder, iscompleteprim, ctx->ac.i32, ""), "");1684LLVMBuildStore(builder, tmp, ctx->gs_generated_prims[stream]);16851686ac_build_endif(&ctx->ac, 9001);1687}16881689void gfx10_ngg_gs_emit_prologue(struct si_shader_context *ctx)1690{1691/* Zero out the part of LDS scratch that is used to accumulate the1692* per-stream generated primitive count.1693*/1694LLVMBuilderRef builder = ctx->ac.builder;1695LLVMValueRef scratchptr = ctx->gs_ngg_scratch;1696LLVMValueRef tid = get_thread_id_in_tg(ctx);1697LLVMValueRef tmp;16981699tmp = LLVMBuildICmp(builder, LLVMIntULT, tid, LLVMConstInt(ctx->ac.i32, 4, false), "");1700ac_build_ifcc(&ctx->ac, tmp, 5090);1701{1702LLVMValueRef ptr = ac_build_gep0(&ctx->ac, scratchptr, tid);1703LLVMBuildStore(builder, ctx->ac.i32_0, ptr);1704}1705ac_build_endif(&ctx->ac, 5090);17061707ac_build_s_barrier(&ctx->ac);1708}17091710void gfx10_ngg_gs_emit_epilogue(struct si_shader_context *ctx)1711{1712const struct si_shader_selector *sel = ctx->shader->selector;1713const struct si_shader_info *info = &sel->info;1714const unsigned verts_per_prim = u_vertices_per_prim(sel->info.base.gs.output_primitive);1715LLVMBuilderRef builder = ctx->ac.builder;1716LLVMValueRef i8_0 = LLVMConstInt(ctx->ac.i8, 0, false);1717LLVMValueRef tmp, tmp2;17181719/* Zero out remaining (non-emitted) primitive flags.1720*1721* Note: Alternatively, we could pass the relevant gs_next_vertex to1722* the emit threads via LDS. This is likely worse in the expected1723* typical case where each GS thread emits the full set of1724* vertices.1725*/1726for (unsigned stream = 0; stream < 4; ++stream) {1727if (!info->num_stream_output_components[stream])1728continue;17291730const LLVMValueRef gsthread = get_thread_id_in_tg(ctx);17311732ac_build_bgnloop(&ctx->ac, 5100);17331734const LLVMValueRef vertexidx = LLVMBuildLoad(builder, ctx->gs_next_vertex[stream], "");1735tmp = LLVMBuildICmp(builder, LLVMIntUGE, vertexidx,1736LLVMConstInt(ctx->ac.i32, sel->info.base.gs.vertices_out, false), "");1737ac_build_ifcc(&ctx->ac, tmp, 5101);1738ac_build_break(&ctx->ac);1739ac_build_endif(&ctx->ac, 5101);17401741tmp = LLVMBuildAdd(builder, vertexidx, ctx->ac.i32_1, "");1742LLVMBuildStore(builder, tmp, ctx->gs_next_vertex[stream]);17431744tmp = ngg_gs_emit_vertex_ptr(ctx, gsthread, vertexidx);1745LLVMBuildStore(builder, i8_0, ngg_gs_get_emit_primflag_ptr(ctx, tmp, stream));17461747ac_build_endloop(&ctx->ac, 5100);1748}17491750/* Accumulate generated primitives counts across the entire threadgroup. */1751for (unsigned stream = 0; stream < 4; ++stream) {1752if (!info->num_stream_output_components[stream])1753continue;17541755LLVMValueRef numprims = LLVMBuildLoad(builder, ctx->gs_generated_prims[stream], "");1756numprims = ac_build_reduce(&ctx->ac, numprims, nir_op_iadd, ctx->ac.wave_size);17571758tmp = LLVMBuildICmp(builder, LLVMIntEQ, ac_get_thread_id(&ctx->ac), ctx->ac.i32_0, "");1759ac_build_ifcc(&ctx->ac, tmp, 5105);1760{1761LLVMBuildAtomicRMW(1762builder, LLVMAtomicRMWBinOpAdd,1763ac_build_gep0(&ctx->ac, ctx->gs_ngg_scratch, LLVMConstInt(ctx->ac.i32, stream, false)),1764numprims, LLVMAtomicOrderingMonotonic, false);1765}1766ac_build_endif(&ctx->ac, 5105);1767}17681769ac_build_endif(&ctx->ac, ctx->merged_wrap_if_label);17701771ac_build_s_barrier(&ctx->ac);17721773const LLVMValueRef tid = get_thread_id_in_tg(ctx);1774LLVMValueRef num_emit_threads = ngg_get_prim_cnt(ctx);17751776/* Streamout */1777if (sel->so.num_outputs) {1778struct ngg_streamout nggso = {};17791780nggso.num_vertices = LLVMConstInt(ctx->ac.i32, verts_per_prim, false);17811782LLVMValueRef vertexptr = ngg_gs_vertex_ptr(ctx, tid);1783for (unsigned stream = 0; stream < 4; ++stream) {1784if (!info->num_stream_output_components[stream])1785continue;17861787tmp = LLVMBuildLoad(builder, ngg_gs_get_emit_primflag_ptr(ctx, vertexptr, stream), "");1788tmp = LLVMBuildTrunc(builder, tmp, ctx->ac.i1, "");1789tmp2 = LLVMBuildICmp(builder, LLVMIntULT, tid, num_emit_threads, "");1790nggso.prim_enable[stream] = LLVMBuildAnd(builder, tmp, tmp2, "");1791}17921793for (unsigned i = 0; i < verts_per_prim; ++i) {1794tmp = LLVMBuildSub(builder, tid, LLVMConstInt(ctx->ac.i32, verts_per_prim - i - 1, false),1795"");1796tmp = ngg_gs_vertex_ptr(ctx, tmp);1797nggso.vertices[i] = ac_build_gep0(&ctx->ac, tmp, ctx->ac.i32_0);1798}17991800build_streamout(ctx, &nggso);1801}18021803/* Write shader query data. */1804if (ctx->screen->use_ngg_streamout) {1805tmp = si_unpack_param(ctx, ctx->vs_state_bits, 6, 1);1806tmp = LLVMBuildTrunc(builder, tmp, ctx->ac.i1, "");1807ac_build_ifcc(&ctx->ac, tmp, 5109); /* if (STREAMOUT_QUERY_ENABLED) */1808unsigned num_query_comps = sel->so.num_outputs ? 8 : 4;1809tmp = LLVMBuildICmp(builder, LLVMIntULT, tid,1810LLVMConstInt(ctx->ac.i32, num_query_comps, false), "");1811ac_build_ifcc(&ctx->ac, tmp, 5110);1812{1813LLVMValueRef offset;1814tmp = tid;1815if (sel->so.num_outputs)1816tmp = LLVMBuildAnd(builder, tmp, LLVMConstInt(ctx->ac.i32, 3, false), "");1817offset = LLVMBuildNUWMul(builder, tmp, LLVMConstInt(ctx->ac.i32, 32, false), "");1818if (sel->so.num_outputs) {1819tmp = LLVMBuildLShr(builder, tid, LLVMConstInt(ctx->ac.i32, 2, false), "");1820tmp = LLVMBuildNUWMul(builder, tmp, LLVMConstInt(ctx->ac.i32, 8, false), "");1821offset = LLVMBuildAdd(builder, offset, tmp, "");1822}18231824tmp = LLVMBuildLoad(builder, ac_build_gep0(&ctx->ac, ctx->gs_ngg_scratch, tid), "");1825LLVMValueRef args[] = {1826tmp, ngg_get_query_buf(ctx),1827offset, LLVMConstInt(ctx->ac.i32, 16, false), /* soffset */1828ctx->ac.i32_0, /* cachepolicy */1829};1830ac_build_intrinsic(&ctx->ac, "llvm.amdgcn.raw.buffer.atomic.add.i32", ctx->ac.i32, args, 5,18310);1832}1833ac_build_endif(&ctx->ac, 5110);1834ac_build_endif(&ctx->ac, 5109);1835}18361837/* Determine vertex liveness. */1838LLVMValueRef vertliveptr = ac_build_alloca(&ctx->ac, ctx->ac.i1, "vertexlive");18391840tmp = LLVMBuildICmp(builder, LLVMIntULT, tid, num_emit_threads, "");1841ac_build_ifcc(&ctx->ac, tmp, 5120);1842{1843for (unsigned i = 0; i < verts_per_prim; ++i) {1844const LLVMValueRef primidx =1845LLVMBuildAdd(builder, tid, LLVMConstInt(ctx->ac.i32, i, false), "");18461847if (i > 0) {1848tmp = LLVMBuildICmp(builder, LLVMIntULT, primidx, num_emit_threads, "");1849ac_build_ifcc(&ctx->ac, tmp, 5121 + i);1850}18511852/* Load primitive liveness */1853tmp = ngg_gs_vertex_ptr(ctx, primidx);1854tmp = LLVMBuildLoad(builder, ngg_gs_get_emit_primflag_ptr(ctx, tmp, 0), "");1855const LLVMValueRef primlive = LLVMBuildTrunc(builder, tmp, ctx->ac.i1, "");18561857tmp = LLVMBuildLoad(builder, vertliveptr, "");1858tmp = LLVMBuildOr(builder, tmp, primlive, ""), LLVMBuildStore(builder, tmp, vertliveptr);18591860if (i > 0)1861ac_build_endif(&ctx->ac, 5121 + i);1862}1863}1864ac_build_endif(&ctx->ac, 5120);18651866/* Inclusive scan addition across the current wave. */1867LLVMValueRef vertlive = LLVMBuildLoad(builder, vertliveptr, "");1868struct ac_wg_scan vertlive_scan = {};1869vertlive_scan.op = nir_op_iadd;1870vertlive_scan.enable_reduce = true;1871vertlive_scan.enable_exclusive = true;1872vertlive_scan.src = vertlive;1873vertlive_scan.scratch = ac_build_gep0(&ctx->ac, ctx->gs_ngg_scratch, ctx->ac.i32_0);1874vertlive_scan.waveidx = get_wave_id_in_tg(ctx);1875vertlive_scan.numwaves = get_tgsize(ctx);1876vertlive_scan.maxwaves = DIV_ROUND_UP(256, ctx->ac.wave_size);18771878ac_build_wg_scan(&ctx->ac, &vertlive_scan);18791880/* Skip all exports (including index exports) when possible. */1881LLVMValueRef have_exports =1882LLVMBuildICmp(builder, LLVMIntNE, vertlive_scan.result_reduce, ctx->ac.i32_0, "");1883num_emit_threads = LLVMBuildSelect(builder, have_exports, num_emit_threads, ctx->ac.i32_0, "");18841885/* Allocate export space. Send this message as early as possible, to1886* hide the latency of the SQ <-> SPI roundtrip.1887*/1888ac_build_sendmsg_gs_alloc_req(&ctx->ac, get_wave_id_in_tg(ctx), vertlive_scan.result_reduce,1889num_emit_threads);18901891/* Setup the reverse vertex compaction permutation. We re-use stream 11892* of the primitive liveness flags, relying on the fact that each1893* threadgroup can have at most 256 threads. */1894ac_build_ifcc(&ctx->ac, vertlive, 5130);1895{1896tmp = ngg_gs_vertex_ptr(ctx, vertlive_scan.result_exclusive);1897tmp2 = LLVMBuildTrunc(builder, tid, ctx->ac.i8, "");1898LLVMBuildStore(builder, tmp2, ngg_gs_get_emit_primflag_ptr(ctx, tmp, 1));1899}1900ac_build_endif(&ctx->ac, 5130);19011902ac_build_s_barrier(&ctx->ac);19031904/* Export primitive data */1905tmp = LLVMBuildICmp(builder, LLVMIntULT, tid, num_emit_threads, "");1906ac_build_ifcc(&ctx->ac, tmp, 5140);1907{1908LLVMValueRef flags;1909struct ac_ngg_prim prim = {};1910prim.num_vertices = verts_per_prim;19111912tmp = ngg_gs_vertex_ptr(ctx, tid);1913flags = LLVMBuildLoad(builder, ngg_gs_get_emit_primflag_ptr(ctx, tmp, 0), "");1914prim.isnull = LLVMBuildNot(builder, LLVMBuildTrunc(builder, flags, ctx->ac.i1, ""), "");19151916for (unsigned i = 0; i < verts_per_prim; ++i) {1917prim.index[i] = LLVMBuildSub(builder, vertlive_scan.result_exclusive,1918LLVMConstInt(ctx->ac.i32, verts_per_prim - i - 1, false), "");1919prim.edgeflag[i] = ctx->ac.i1false;1920}19211922/* Geometry shaders output triangle strips, but NGG expects triangles. */1923if (verts_per_prim == 3) {1924LLVMValueRef is_odd = LLVMBuildLShr(builder, flags, ctx->ac.i8_1, "");1925is_odd = LLVMBuildTrunc(builder, is_odd, ctx->ac.i1, "");1926LLVMValueRef flatshade_first = LLVMBuildICmp(1927builder, LLVMIntEQ, si_unpack_param(ctx, ctx->vs_state_bits, 4, 2), ctx->ac.i32_0, "");19281929ac_build_triangle_strip_indices_to_triangle(&ctx->ac, is_odd, flatshade_first, prim.index);1930}19311932ac_build_export_prim(&ctx->ac, &prim);1933}1934ac_build_endif(&ctx->ac, 5140);19351936/* Export position and parameter data */1937tmp = LLVMBuildICmp(builder, LLVMIntULT, tid, vertlive_scan.result_reduce, "");1938ac_build_ifcc(&ctx->ac, tmp, 5145);1939{1940struct si_shader_output_values outputs[PIPE_MAX_SHADER_OUTPUTS];19411942tmp = ngg_gs_vertex_ptr(ctx, tid);1943tmp = LLVMBuildLoad(builder, ngg_gs_get_emit_primflag_ptr(ctx, tmp, 1), "");1944tmp = LLVMBuildZExt(builder, tmp, ctx->ac.i32, "");1945const LLVMValueRef vertexptr = ngg_gs_vertex_ptr(ctx, tmp);19461947unsigned out_idx = 0;1948for (unsigned i = 0; i < info->num_outputs; i++) {1949outputs[i].semantic = info->output_semantic[i];19501951for (unsigned j = 0; j < 4; j++, out_idx++) {1952tmp = ngg_gs_get_emit_output_ptr(ctx, vertexptr, out_idx);1953tmp = LLVMBuildLoad(builder, tmp, "");1954outputs[i].values[j] = ac_to_float(&ctx->ac, tmp);1955outputs[i].vertex_stream[j] = (info->output_streams[i] >> (2 * j)) & 3;1956}1957}19581959si_llvm_build_vs_exports(ctx, outputs, info->num_outputs);1960}1961ac_build_endif(&ctx->ac, 5145);1962}19631964static void clamp_gsprims_to_esverts(unsigned *max_gsprims, unsigned max_esverts,1965unsigned min_verts_per_prim, bool use_adjacency)1966{1967unsigned max_reuse = max_esverts - min_verts_per_prim;1968if (use_adjacency)1969max_reuse /= 2;1970*max_gsprims = MIN2(*max_gsprims, 1 + max_reuse);1971}19721973unsigned gfx10_ngg_get_scratch_dw_size(struct si_shader *shader)1974{1975const struct si_shader_selector *sel = shader->selector;19761977if (sel->info.stage == MESA_SHADER_GEOMETRY && sel->so.num_outputs)1978return 44;19791980return 8;1981}19821983/**1984* Determine subgroup information like maximum number of vertices and prims.1985*1986* This happens before the shader is uploaded, since LDS relocations during1987* upload depend on the subgroup size.1988*/1989bool gfx10_ngg_calculate_subgroup_info(struct si_shader *shader)1990{1991const struct si_shader_selector *gs_sel = shader->selector;1992const struct si_shader_selector *es_sel =1993shader->previous_stage_sel ? shader->previous_stage_sel : gs_sel;1994const gl_shader_stage gs_stage = gs_sel->info.stage;1995const unsigned gs_num_invocations = MAX2(gs_sel->info.base.gs.invocations, 1);1996const unsigned input_prim = si_get_input_prim(gs_sel);1997const bool use_adjacency =1998input_prim >= PIPE_PRIM_LINES_ADJACENCY && input_prim <= PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY;1999const unsigned max_verts_per_prim = u_vertices_per_prim(input_prim);2000const unsigned min_verts_per_prim = gs_stage == MESA_SHADER_GEOMETRY ? max_verts_per_prim : 1;20012002/* All these are in dwords: */2003/* GE can only use 8K dwords (32KB) of LDS per workgroup.2004*/2005const unsigned max_lds_size = 8 * 1024 - gfx10_ngg_get_scratch_dw_size(shader);2006const unsigned target_lds_size = max_lds_size;2007unsigned esvert_lds_size = 0;2008unsigned gsprim_lds_size = 0;20092010/* All these are per subgroup: */2011const unsigned min_esverts = gs_sel->screen->info.chip_class >= GFX10_3 ? 29 : 24;2012bool max_vert_out_per_gs_instance = false;2013unsigned max_gsprims_base = gs_sel->screen->ngg_subgroup_size; /* default prim group size clamp */2014unsigned max_esverts_base = gs_sel->screen->ngg_subgroup_size;20152016if (shader->key.opt.ngg_culling & SI_NGG_CULL_GS_FAST_LAUNCH_TRI_LIST) {2017/* All lanes are filled in wave32. */2018max_gsprims_base = ROUND_DOWN_TO(max_gsprims_base / 3, 32);2019max_esverts_base = max_gsprims_base * 3;2020} else if (shader->key.opt.ngg_culling & SI_NGG_CULL_GS_FAST_LAUNCH_TRI_STRIP) {2021max_gsprims_base = max_esverts_base - 2;2022}20232024if (gs_stage == MESA_SHADER_GEOMETRY) {2025bool force_multi_cycling = false;2026unsigned max_out_verts_per_gsprim = gs_sel->info.base.gs.vertices_out * gs_num_invocations;20272028retry_select_mode:2029if (max_out_verts_per_gsprim <= 256 && !force_multi_cycling) {2030if (max_out_verts_per_gsprim) {2031max_gsprims_base = MIN2(max_gsprims_base, 256 / max_out_verts_per_gsprim);2032}2033} else {2034/* Use special multi-cycling mode in which each GS2035* instance gets its own subgroup. Does not work with2036* tessellation. */2037max_vert_out_per_gs_instance = true;2038max_gsprims_base = 1;2039max_out_verts_per_gsprim = gs_sel->info.base.gs.vertices_out;2040}20412042esvert_lds_size = es_sel->esgs_itemsize / 4;2043gsprim_lds_size = (gs_sel->gsvs_vertex_size / 4 + 1) * max_out_verts_per_gsprim;20442045if (gsprim_lds_size > target_lds_size && !force_multi_cycling) {2046if (gs_sel->tess_turns_off_ngg || es_sel->info.stage != MESA_SHADER_TESS_EVAL) {2047force_multi_cycling = true;2048goto retry_select_mode;2049}2050}2051} else {2052/* VS and TES. */2053/* LDS size for passing data from ES to GS. */2054esvert_lds_size = ngg_nogs_vertex_size(shader);2055}20562057unsigned max_gsprims = max_gsprims_base;2058unsigned max_esverts = max_esverts_base;20592060if (esvert_lds_size)2061max_esverts = MIN2(max_esverts, target_lds_size / esvert_lds_size);2062if (gsprim_lds_size)2063max_gsprims = MIN2(max_gsprims, target_lds_size / gsprim_lds_size);20642065max_esverts = MIN2(max_esverts, max_gsprims * max_verts_per_prim);2066clamp_gsprims_to_esverts(&max_gsprims, max_esverts, min_verts_per_prim, use_adjacency);2067assert(max_esverts >= max_verts_per_prim && max_gsprims >= 1);20682069if (esvert_lds_size || gsprim_lds_size) {2070/* Now that we have a rough proportionality between esverts2071* and gsprims based on the primitive type, scale both of them2072* down simultaneously based on required LDS space.2073*2074* We could be smarter about this if we knew how much vertex2075* reuse to expect.2076*/2077unsigned lds_total = max_esverts * esvert_lds_size + max_gsprims * gsprim_lds_size;2078if (lds_total > target_lds_size) {2079max_esverts = max_esverts * target_lds_size / lds_total;2080max_gsprims = max_gsprims * target_lds_size / lds_total;20812082max_esverts = MIN2(max_esverts, max_gsprims * max_verts_per_prim);2083clamp_gsprims_to_esverts(&max_gsprims, max_esverts, min_verts_per_prim, use_adjacency);2084assert(max_esverts >= max_verts_per_prim && max_gsprims >= 1);2085}2086}20872088/* Round up towards full wave sizes for better ALU utilization. */2089if (!max_vert_out_per_gs_instance) {2090const unsigned wavesize = si_get_shader_wave_size(shader);2091unsigned orig_max_esverts;2092unsigned orig_max_gsprims;2093do {2094orig_max_esverts = max_esverts;2095orig_max_gsprims = max_gsprims;20962097max_esverts = align(max_esverts, wavesize);2098max_esverts = MIN2(max_esverts, max_esverts_base);2099if (esvert_lds_size)2100max_esverts =2101MIN2(max_esverts, (max_lds_size - max_gsprims * gsprim_lds_size) / esvert_lds_size);2102max_esverts = MIN2(max_esverts, max_gsprims * max_verts_per_prim);21032104/* Hardware restriction: minimum value of max_esverts */2105if (gs_sel->screen->info.chip_class == GFX10)2106max_esverts = MAX2(max_esverts, min_esverts - 1 + max_verts_per_prim);2107else2108max_esverts = MAX2(max_esverts, min_esverts);21092110max_gsprims = align(max_gsprims, wavesize);2111max_gsprims = MIN2(max_gsprims, max_gsprims_base);2112if (gsprim_lds_size) {2113/* Don't count unusable vertices to the LDS size. Those are vertices above2114* the maximum number of vertices that can occur in the workgroup,2115* which is e.g. max_gsprims * 3 for triangles.2116*/2117unsigned usable_esverts = MIN2(max_esverts, max_gsprims * max_verts_per_prim);2118max_gsprims =2119MIN2(max_gsprims, (max_lds_size - usable_esverts * esvert_lds_size) / gsprim_lds_size);2120}2121clamp_gsprims_to_esverts(&max_gsprims, max_esverts, min_verts_per_prim, use_adjacency);2122assert(max_esverts >= max_verts_per_prim && max_gsprims >= 1);2123} while (orig_max_esverts != max_esverts || orig_max_gsprims != max_gsprims);21242125/* Verify the restriction. */2126if (gs_sel->screen->info.chip_class == GFX10)2127assert(max_esverts >= min_esverts - 1 + max_verts_per_prim);2128else2129assert(max_esverts >= min_esverts);2130} else {2131/* Hardware restriction: minimum value of max_esverts */2132if (gs_sel->screen->info.chip_class == GFX10)2133max_esverts = MAX2(max_esverts, min_esverts - 1 + max_verts_per_prim);2134else2135max_esverts = MAX2(max_esverts, min_esverts);2136}21372138unsigned max_out_vertices =2139max_vert_out_per_gs_instance2140? gs_sel->info.base.gs.vertices_out2141: gs_stage == MESA_SHADER_GEOMETRY2142? max_gsprims * gs_num_invocations * gs_sel->info.base.gs.vertices_out2143: max_esverts;2144assert(max_out_vertices <= 256);21452146unsigned prim_amp_factor = 1;2147if (gs_stage == MESA_SHADER_GEOMETRY) {2148/* Number of output primitives per GS input primitive after2149* GS instancing. */2150prim_amp_factor = gs_sel->info.base.gs.vertices_out;2151}21522153/* Fix up the thread counts for fast launch. */2154if (shader->key.opt.ngg_culling & SI_NGG_CULL_GS_FAST_LAUNCH_TRI_LIST) {2155/* The vertex count must be a multiple of 3. */2156max_esverts -= max_esverts % 3;2157/* We can only decrease the size, not increase it. */2158if (max_gsprims * 3 < max_esverts) {2159max_esverts = max_gsprims * 3;2160} else {2161max_gsprims = max_esverts / 3;2162}2163} else if (shader->key.opt.ngg_culling & SI_NGG_CULL_GS_FAST_LAUNCH_TRI_STRIP) {2164/* The primitive count must be even to get correct winding for triangle strips. */2165max_gsprims &= ~1;2166if (max_gsprims - 2 < max_esverts) {2167max_esverts = max_gsprims + 2;2168} else {2169max_gsprims = max_esverts - 2;2170max_gsprims &= ~1;2171max_esverts = max_gsprims + 2;2172}2173}21742175shader->ngg.hw_max_esverts = max_esverts;2176shader->ngg.max_gsprims = max_gsprims;2177shader->ngg.max_out_verts = max_out_vertices;2178shader->ngg.prim_amp_factor = prim_amp_factor;2179shader->ngg.max_vert_out_per_gs_instance = max_vert_out_per_gs_instance;21802181/* Don't count unusable vertices. */2182shader->gs_info.esgs_ring_size = MIN2(max_esverts, max_gsprims * max_verts_per_prim) *2183esvert_lds_size;2184shader->ngg.ngg_emit_size = max_gsprims * gsprim_lds_size;21852186assert(shader->ngg.hw_max_esverts >= min_esverts); /* HW limitation */21872188/* If asserts are disabled, we use the same conditions to return false */2189return max_esverts >= max_verts_per_prim && max_gsprims >= 1 &&2190max_out_vertices <= 256 &&2191shader->ngg.hw_max_esverts >= min_esverts;2192}219321942195