radeonsi: deduplicate ES and GS thread enablement code
[mesa.git] / src / gallium / drivers / radeonsi / gfx10_shader_ngg.c
index f5c41cd07a47c05d037997a260c9513e29d7d264..5d77a3b6252e129e46f59a89797d6566234e888e 100644 (file)
 
 static LLVMValueRef get_wave_id_in_tg(struct si_shader_context *ctx)
 {
-       return si_unpack_param(ctx, ctx->param_merged_wave_info, 24, 4);
+       return si_unpack_param(ctx, ctx->merged_wave_info, 24, 4);
 }
 
 static LLVMValueRef get_tgsize(struct si_shader_context *ctx)
 {
-       return si_unpack_param(ctx, ctx->param_merged_wave_info, 28, 4);
+       return si_unpack_param(ctx, ctx->merged_wave_info, 28, 4);
 }
 
 static LLVMValueRef get_thread_id_in_tg(struct si_shader_context *ctx)
@@ -50,32 +50,22 @@ static LLVMValueRef get_thread_id_in_tg(struct si_shader_context *ctx)
 
 static LLVMValueRef ngg_get_vtx_cnt(struct si_shader_context *ctx)
 {
-       return ac_build_bfe(&ctx->ac, ctx->gs_tg_info,
-                           LLVMConstInt(ctx->ac.i32, 12, false),
-                           LLVMConstInt(ctx->ac.i32, 9, false),
-                           false);
+       return si_unpack_param(ctx, ctx->gs_tg_info, 12, 9);
 }
 
 static LLVMValueRef ngg_get_prim_cnt(struct si_shader_context *ctx)
 {
-       return ac_build_bfe(&ctx->ac, ctx->gs_tg_info,
-                           LLVMConstInt(ctx->ac.i32, 22, false),
-                           LLVMConstInt(ctx->ac.i32, 9, false),
-                           false);
+       return si_unpack_param(ctx, ctx->gs_tg_info, 22, 9);
 }
 
 static LLVMValueRef ngg_get_ordered_id(struct si_shader_context *ctx)
 {
-       return ac_build_bfe(&ctx->ac, ctx->gs_tg_info,
-                           ctx->i32_0,
-                           LLVMConstInt(ctx->ac.i32, 11, false),
-                           false);
+       return si_unpack_param(ctx, ctx->gs_tg_info, 0, 11);
 }
 
 static LLVMValueRef ngg_get_query_buf(struct si_shader_context *ctx)
 {
-       LLVMValueRef buf_ptr = LLVMGetParam(ctx->main_fn,
-                                           ctx->param_rw_buffers);
+       LLVMValueRef buf_ptr = ac_get_arg(&ctx->ac, ctx->rw_buffers);
 
        return ac_build_load_to_sgpr(&ctx->ac, buf_ptr,
                                     LLVMConstInt(ctx->i32, GFX10_GS_QUERY_BUF, false));
@@ -212,7 +202,7 @@ static void build_streamout(struct si_shader_context *ctx,
        struct tgsi_shader_info *info = &ctx->shader->selector->info;
        struct pipe_stream_output_info *so = &ctx->shader->selector->so;
        LLVMBuilderRef builder = ctx->ac.builder;
-       LLVMValueRef buf_ptr = LLVMGetParam(ctx->main_fn, ctx->param_rw_buffers);
+       LLVMValueRef buf_ptr = ac_get_arg(&ctx->ac, ctx->rw_buffers);
        LLVMValueRef tid = get_thread_id_in_tg(ctx);
        LLVMValueRef tmp, tmp2;
        LLVMValueRef i32_2 = LLVMConstInt(ctx->i32, 2, false);
@@ -504,7 +494,7 @@ static unsigned ngg_nogs_vertex_size(struct si_shader *shader)
         * used for padding to reduce LDS bank conflicts. */
        if (shader->selector->so.num_outputs)
                lds_vertex_size = 4 * shader->selector->info.num_outputs + 1;
-       if (shader->selector->ngg_writes_edgeflag)
+       if (shader->selector->info.writes_edgeflag)
                lds_vertex_size = MAX2(lds_vertex_size, 1);
 
        return lds_vertex_size;
@@ -537,7 +527,6 @@ void gfx10_emit_ngg_epilogue(struct ac_shader_abi *abi,
        struct tgsi_shader_info *info = &sel->info;
        struct si_shader_output_values outputs[PIPE_MAX_SHADER_OUTPUTS];
        LLVMBuilderRef builder = ctx->ac.builder;
-       struct lp_build_if_state if_state;
        LLVMValueRef tmp, tmp2;
 
        assert(!ctx->shader->is_gs_copy_shader);
@@ -545,7 +534,7 @@ void gfx10_emit_ngg_epilogue(struct ac_shader_abi *abi,
 
        LLVMValueRef vertex_ptr = NULL;
 
-       if (sel->so.num_outputs || sel->ngg_writes_edgeflag)
+       if (sel->so.num_outputs || sel->info.writes_edgeflag)
                vertex_ptr = ngg_nogs_vertex_ptr(ctx, get_thread_id_in_tg(ctx));
 
        for (unsigned i = 0; i < info->num_outputs; i++) {
@@ -570,7 +559,7 @@ void gfx10_emit_ngg_epilogue(struct ac_shader_abi *abi,
 
                /* Store the edgeflag at the end (if streamout is enabled) */
                if (info->output_semantic_name[i] == TGSI_SEMANTIC_EDGEFLAG &&
-                   sel->ngg_writes_edgeflag) {
+                   sel->info.writes_edgeflag) {
                        LLVMValueRef edgeflag = LLVMBuildLoad(builder, addrs[4 * i], "");
                        /* The output is a float, but the hw expects a 1-bit integer. */
                        edgeflag = LLVMBuildFPToUI(ctx->ac.builder, edgeflag, ctx->i32, "");
@@ -582,18 +571,14 @@ void gfx10_emit_ngg_epilogue(struct ac_shader_abi *abi,
                }
        }
 
-       lp_build_endif(&ctx->merged_wrap_if_state);
+       ac_build_endif(&ctx->ac, ctx->merged_wrap_if_label);
 
-       LLVMValueRef prims_in_wave = si_unpack_param(ctx, ctx->param_merged_wave_info, 8, 8);
-       LLVMValueRef vtx_in_wave = si_unpack_param(ctx, ctx->param_merged_wave_info, 0, 8);
-       LLVMValueRef is_gs_thread = LLVMBuildICmp(builder, LLVMIntULT,
-                                                 ac_get_thread_id(&ctx->ac), prims_in_wave, "");
-       LLVMValueRef is_es_thread = LLVMBuildICmp(builder, LLVMIntULT,
-                                                 ac_get_thread_id(&ctx->ac), vtx_in_wave, "");
+       LLVMValueRef is_gs_thread = si_is_gs_thread(ctx);
+       LLVMValueRef is_es_thread = si_is_es_thread(ctx);
        LLVMValueRef vtxindex[] = {
-               si_unpack_param(ctx, ctx->param_gs_vtx01_offset, 0, 16),
-               si_unpack_param(ctx, ctx->param_gs_vtx01_offset, 16, 16),
-               si_unpack_param(ctx, ctx->param_gs_vtx23_offset, 0, 16),
+               si_unpack_param(ctx, ctx->gs_vtx01_offset, 0, 16),
+               si_unpack_param(ctx, ctx->gs_vtx01_offset, 16, 16),
+               si_unpack_param(ctx, ctx->gs_vtx23_offset, 0, 16),
        };
 
        /* Determine the number of vertices per primitive. */
@@ -601,13 +586,13 @@ void gfx10_emit_ngg_epilogue(struct ac_shader_abi *abi,
        LLVMValueRef num_vertices_val;
 
        if (ctx->type == PIPE_SHADER_VERTEX) {
-               if (info->properties[TGSI_PROPERTY_VS_BLIT_SGPRS]) {
+               if (info->properties[TGSI_PROPERTY_VS_BLIT_SGPRS_AMD]) {
                        /* Blits always use axis-aligned rectangles with 3 vertices. */
                        num_vertices = 3;
                        num_vertices_val = LLVMConstInt(ctx->i32, 3, 0);
                } else {
                        /* Extract OUTPRIM field. */
-                       tmp = si_unpack_param(ctx, ctx->param_vs_state_bits, 2, 2);
+                       tmp = si_unpack_param(ctx, ctx->vs_state_bits, 2, 2);
                        num_vertices_val = LLVMBuildAdd(builder, tmp, ctx->i32_1, "");
                        num_vertices = 3; /* TODO: optimize for points & lines */
                }
@@ -642,7 +627,7 @@ void gfx10_emit_ngg_epilogue(struct ac_shader_abi *abi,
 
        LLVMValueRef user_edgeflags[3] = {};
 
-       if (sel->ngg_writes_edgeflag) {
+       if (sel->info.writes_edgeflag) {
                /* Streamout already inserted the barrier, so don't insert it again. */
                if (!sel->so.num_outputs)
                        ac_build_s_barrier(&ctx->ac);
@@ -668,20 +653,20 @@ void gfx10_emit_ngg_epilogue(struct ac_shader_abi *abi,
        if (ctx->type == PIPE_SHADER_VERTEX &&
            ctx->shader->key.mono.u.vs_export_prim_id) {
                /* Streamout and edge flags use LDS. Make it idle, so that we can reuse it. */
-               if (sel->so.num_outputs || sel->ngg_writes_edgeflag)
+               if (sel->so.num_outputs || sel->info.writes_edgeflag)
                        ac_build_s_barrier(&ctx->ac);
 
                ac_build_ifcc(&ctx->ac, is_gs_thread, 5400);
                /* Extract the PROVOKING_VTX_INDEX field. */
                LLVMValueRef provoking_vtx_in_prim =
-                       si_unpack_param(ctx, ctx->param_vs_state_bits, 4, 2);
+                       si_unpack_param(ctx, ctx->vs_state_bits, 4, 2);
 
                /* provoking_vtx_index = vtxindex[provoking_vtx_in_prim]; */
                LLVMValueRef indices = ac_build_gather_values(&ctx->ac, vtxindex, 3);
                LLVMValueRef provoking_vtx_index =
                        LLVMBuildExtractElement(builder, indices, provoking_vtx_in_prim, "");
 
-               LLVMBuildStore(builder, ctx->abi.gs_prim_id,
+               LLVMBuildStore(builder, ac_get_arg(&ctx->ac, ctx->args.gs_prim_id),
                               ac_build_gep0(&ctx->ac, ctx->esgs_ring, provoking_vtx_index));
                ac_build_endif(&ctx->ac, 5400);
        }
@@ -690,8 +675,8 @@ void gfx10_emit_ngg_epilogue(struct ac_shader_abi *abi,
 
        /* Update query buffer */
        /* TODO: this won't catch 96-bit clear_buffer via transform feedback. */
-       if (!info->properties[TGSI_PROPERTY_VS_BLIT_SGPRS]) {
-               tmp = si_unpack_param(ctx, ctx->param_vs_state_bits, 6, 1);
+       if (!info->properties[TGSI_PROPERTY_VS_BLIT_SGPRS_AMD]) {
+               tmp = si_unpack_param(ctx, ctx->vs_state_bits, 6, 1);
                tmp = LLVMBuildTrunc(builder, tmp, ctx->i1, "");
                ac_build_ifcc(&ctx->ac, tmp, 5029); /* if (STREAMOUT_QUERY_ENABLED) */
                tmp = LLVMBuildICmp(builder, LLVMIntEQ, get_wave_id_in_tg(ctx), ctx->ac.i32_0, "");
@@ -739,7 +724,7 @@ void gfx10_emit_ngg_epilogue(struct ac_shader_abi *abi,
         * TODO: culling depends on the primitive type, so can have some
         * interaction here.
         */
-       lp_build_if(&if_state, &ctx->gallivm, is_gs_thread);
+       ac_build_ifcc(&ctx->ac, is_gs_thread, 6001);
        {
                struct ngg_prim prim = {};
 
@@ -753,11 +738,12 @@ void gfx10_emit_ngg_epilogue(struct ac_shader_abi *abi,
                                continue;
                        }
 
-                       tmp = LLVMBuildLShr(builder, ctx->abi.gs_invocation_id,
+                       tmp = LLVMBuildLShr(builder,
+                                           ac_get_arg(&ctx->ac, ctx->args.gs_invocation_id),
                                            LLVMConstInt(ctx->ac.i32, 8 + i, false), "");
                        prim.edgeflag[i] = LLVMBuildTrunc(builder, tmp, ctx->ac.i1, "");
 
-                       if (sel->ngg_writes_edgeflag) {
+                       if (sel->info.writes_edgeflag) {
                                tmp2 = LLVMBuildLoad(builder, user_edgeflags[i], "");
                                prim.edgeflag[i] = LLVMBuildAnd(builder, prim.edgeflag[i],
                                                                tmp2, "");
@@ -766,10 +752,10 @@ void gfx10_emit_ngg_epilogue(struct ac_shader_abi *abi,
 
                build_export_prim(ctx, &prim);
        }
-       lp_build_endif(&if_state);
+       ac_build_endif(&ctx->ac, 6001);
 
        /* Export per-vertex data (positions and parameters). */
-       lp_build_if(&if_state, &ctx->gallivm, is_es_thread);
+       ac_build_ifcc(&ctx->ac, is_es_thread, 6002);
        {
                unsigned i;
 
@@ -810,7 +796,7 @@ void gfx10_emit_ngg_epilogue(struct ac_shader_abi *abi,
 
                si_llvm_export_vs(ctx, outputs, i);
        }
-       lp_build_endif(&if_state);
+       ac_build_endif(&ctx->ac, 6002);
 }
 
 static LLVMValueRef
@@ -895,6 +881,30 @@ ngg_gs_emit_vertex_ptr(struct si_shader_context *ctx, LLVMValueRef gsthread,
        return ngg_gs_vertex_ptr(ctx, vertexidx);
 }
 
+static LLVMValueRef
+ngg_gs_get_emit_output_ptr(struct si_shader_context *ctx, LLVMValueRef vertexptr,
+                          unsigned out_idx)
+{
+       LLVMValueRef gep_idx[3] = {
+               ctx->ac.i32_0, /* implied C-style array */
+               ctx->ac.i32_0, /* first struct entry */
+               LLVMConstInt(ctx->ac.i32, out_idx, false),
+       };
+       return LLVMBuildGEP(ctx->ac.builder, vertexptr, gep_idx, 3, "");
+}
+
+static LLVMValueRef
+ngg_gs_get_emit_primflag_ptr(struct si_shader_context *ctx, LLVMValueRef vertexptr,
+                            unsigned stream)
+{
+       LLVMValueRef gep_idx[3] = {
+               ctx->ac.i32_0, /* implied C-style array */
+               ctx->ac.i32_1, /* second struct entry */
+               LLVMConstInt(ctx->ac.i32, stream, false),
+       };
+       return LLVMBuildGEP(ctx->ac.builder, vertexptr, gep_idx, 3, "");
+}
+
 void gfx10_ngg_gs_emit_vertex(struct si_shader_context *ctx,
                              unsigned stream,
                              LLVMValueRef *addrs)
@@ -902,7 +912,6 @@ void gfx10_ngg_gs_emit_vertex(struct si_shader_context *ctx,
        const struct si_shader_selector *sel = ctx->shader->selector;
        const struct tgsi_shader_info *info = &sel->info;
        LLVMBuilderRef builder = ctx->ac.builder;
-       struct lp_build_if_state if_state;
        LLVMValueRef tmp;
        const LLVMValueRef vertexidx =
                LLVMBuildLoad(builder, ctx->gs_next_vertex[stream], "");
@@ -919,7 +928,7 @@ void gfx10_ngg_gs_emit_vertex(struct si_shader_context *ctx,
        tmp = LLVMBuildSelect(builder, can_emit, tmp, vertexidx, "");
        LLVMBuildStore(builder, tmp, ctx->gs_next_vertex[stream]);
 
-       lp_build_if(&if_state, &ctx->gallivm, can_emit);
+       ac_build_ifcc(&ctx->ac, can_emit, 9001);
 
        const LLVMValueRef vertexptr =
                ngg_gs_emit_vertex_ptr(ctx, get_thread_id_in_tg(ctx), vertexidx);
@@ -931,15 +940,9 @@ void gfx10_ngg_gs_emit_vertex(struct si_shader_context *ctx,
                                continue;
 
                        LLVMValueRef out_val = LLVMBuildLoad(builder, addrs[4 * i + chan], "");
-                       LLVMValueRef gep_idx[3] = {
-                               ctx->ac.i32_0, /* implied C-style array */
-                               ctx->ac.i32_0, /* first entry of struct */
-                               LLVMConstInt(ctx->ac.i32, out_idx, false),
-                       };
-                       LLVMValueRef ptr = LLVMBuildGEP(builder, vertexptr, gep_idx, 3, "");
-
                        out_val = ac_to_integer(&ctx->ac, out_val);
-                       LLVMBuildStore(builder, out_val, ptr);
+                       LLVMBuildStore(builder, out_val,
+                                      ngg_gs_get_emit_output_ptr(ctx, vertexptr, out_idx));
                }
        }
        assert(out_idx * 4 == sel->gsvs_vertex_size);
@@ -951,25 +954,35 @@ void gfx10_ngg_gs_emit_vertex(struct si_shader_context *ctx,
        const LLVMValueRef iscompleteprim =
                LLVMBuildICmp(builder, LLVMIntUGE, curverts, tmp, "");
 
+       /* Since the geometry shader emits triangle strips, we need to
+        * track which primitive is odd and swap vertex indices to get
+        * the correct vertex order.
+        */
+       LLVMValueRef is_odd = ctx->i1false;
+       if (stream == 0 && u_vertices_per_prim(sel->gs_output_prim) == 3) {
+               tmp = LLVMBuildAnd(builder, curverts, ctx->i32_1, "");
+               is_odd = LLVMBuildICmp(builder, LLVMIntEQ, tmp, ctx->i32_1, "");
+       }
+
        tmp = LLVMBuildAdd(builder, curverts, ctx->ac.i32_1, "");
        LLVMBuildStore(builder, tmp, ctx->gs_curprim_verts[stream]);
 
-       LLVMValueRef gep_idx[3] = {
-               ctx->ac.i32_0, /* implied C-style array */
-               ctx->ac.i32_1, /* second struct entry */
-               LLVMConstInt(ctx->ac.i32, stream, false),
-       };
-       const LLVMValueRef primflagptr =
-               LLVMBuildGEP(builder, vertexptr, gep_idx, 3, "");
-
+       /* The per-vertex primitive flag encoding:
+        *   bit 0: whether this vertex finishes a primitive
+        *   bit 1: whether the primitive is odd (if we are emitting triangle strips)
+        */
        tmp = LLVMBuildZExt(builder, iscompleteprim, ctx->ac.i8, "");
-       LLVMBuildStore(builder, tmp, primflagptr);
+       tmp = LLVMBuildOr(builder, tmp,
+                         LLVMBuildShl(builder,
+                                      LLVMBuildZExt(builder, is_odd, ctx->ac.i8, ""),
+                                      ctx->ac.i8_1, ""), "");
+       LLVMBuildStore(builder, tmp, ngg_gs_get_emit_primflag_ptr(ctx, vertexptr, stream));
 
        tmp = LLVMBuildLoad(builder, ctx->gs_generated_prims[stream], "");
        tmp = LLVMBuildAdd(builder, tmp, LLVMBuildZExt(builder, iscompleteprim, ctx->ac.i32, ""), "");
        LLVMBuildStore(builder, tmp, ctx->gs_generated_prims[stream]);
 
-       lp_build_endif(&if_state);
+       ac_build_endif(&ctx->ac, 9001);
 }
 
 void gfx10_ngg_gs_emit_prologue(struct si_shader_context *ctx)
@@ -1029,13 +1042,7 @@ void gfx10_ngg_gs_emit_epilogue(struct si_shader_context *ctx)
                LLVMBuildStore(builder, tmp, ctx->gs_next_vertex[stream]);
 
                tmp = ngg_gs_emit_vertex_ptr(ctx, gsthread, vertexidx);
-               LLVMValueRef gep_idx[3] = {
-                       ctx->ac.i32_0, /* implied C-style array */
-                       ctx->ac.i32_1, /* second entry of struct */
-                       LLVMConstInt(ctx->ac.i32, stream, false),
-               };
-               tmp = LLVMBuildGEP(builder, tmp, gep_idx, 3, "");
-               LLVMBuildStore(builder, i8_0, tmp);
+               LLVMBuildStore(builder, i8_0, ngg_gs_get_emit_primflag_ptr(ctx, tmp, stream));
 
                ac_build_endloop(&ctx->ac, 5100);
        }
@@ -1060,7 +1067,7 @@ void gfx10_ngg_gs_emit_epilogue(struct si_shader_context *ctx)
                ac_build_endif(&ctx->ac, 5105);
        }
 
-       lp_build_endif(&ctx->merged_wrap_if_state);
+       ac_build_endif(&ctx->ac, ctx->merged_wrap_if_label);
 
        ac_build_s_barrier(&ctx->ac);
 
@@ -1078,13 +1085,7 @@ void gfx10_ngg_gs_emit_epilogue(struct si_shader_context *ctx)
                        if (!info->num_stream_output_components[stream])
                                continue;
 
-                       LLVMValueRef gep_idx[3] = {
-                               ctx->i32_0, /* implicit C-style array */
-                               ctx->i32_1, /* second value of struct */
-                               LLVMConstInt(ctx->i32, stream, false),
-                       };
-                       tmp = LLVMBuildGEP(builder, vertexptr, gep_idx, 3, "");
-                       tmp = LLVMBuildLoad(builder, tmp, "");
+                       tmp = LLVMBuildLoad(builder, ngg_gs_get_emit_primflag_ptr(ctx, vertexptr, stream), "");
                        tmp = LLVMBuildTrunc(builder, tmp, ctx->i1, "");
                        tmp2 = LLVMBuildICmp(builder, LLVMIntULT, tid, num_emit_threads, "");
                        nggso.prim_enable[stream] = LLVMBuildAnd(builder, tmp, tmp2, "");
@@ -1101,7 +1102,7 @@ void gfx10_ngg_gs_emit_epilogue(struct si_shader_context *ctx)
        }
 
        /* Write shader query data. */
-       tmp = si_unpack_param(ctx, ctx->param_vs_state_bits, 6, 1);
+       tmp = si_unpack_param(ctx, ctx->vs_state_bits, 6, 1);
        tmp = LLVMBuildTrunc(builder, tmp, ctx->i1, "");
        ac_build_ifcc(&ctx->ac, tmp, 5109); /* if (STREAMOUT_QUERY_ENABLED) */
        unsigned num_query_comps = sel->so.num_outputs ? 8 : 4;
@@ -1137,7 +1138,7 @@ void gfx10_ngg_gs_emit_epilogue(struct si_shader_context *ctx)
        /* TODO: culling */
 
        /* Determine vertex liveness. */
-       LLVMValueRef vertliveptr = lp_build_alloca(&ctx->gallivm, ctx->ac.i1, "vertexlive");
+       LLVMValueRef vertliveptr = ac_build_alloca(&ctx->ac, ctx->ac.i1, "vertexlive");
 
        tmp = LLVMBuildICmp(builder, LLVMIntULT, tid, num_emit_threads, "");
        ac_build_ifcc(&ctx->ac, tmp, 5120);
@@ -1154,13 +1155,7 @@ void gfx10_ngg_gs_emit_epilogue(struct si_shader_context *ctx)
 
                        /* Load primitive liveness */
                        tmp = ngg_gs_vertex_ptr(ctx, primidx);
-                       LLVMValueRef gep_idx[3] = {
-                               ctx->ac.i32_0, /* implicit C-style array */
-                               ctx->ac.i32_1, /* second value of struct */
-                               ctx->ac.i32_0, /* stream 0 */
-                       };
-                       tmp = LLVMBuildGEP(builder, tmp, gep_idx, 3, "");
-                       tmp = LLVMBuildLoad(builder, tmp, "");
+                       tmp = LLVMBuildLoad(builder, ngg_gs_get_emit_primflag_ptr(ctx, tmp, 0), "");
                        const LLVMValueRef primlive =
                                LLVMBuildTrunc(builder, tmp, ctx->ac.i1, "");
 
@@ -1214,14 +1209,8 @@ void gfx10_ngg_gs_emit_epilogue(struct si_shader_context *ctx)
        ac_build_ifcc(&ctx->ac, vertlive, 5130);
        {
                tmp = ngg_gs_vertex_ptr(ctx, vertlive_scan.result_exclusive);
-               LLVMValueRef gep_idx[3] = {
-                       ctx->ac.i32_0, /* implicit C-style array */
-                       ctx->ac.i32_1, /* second value of struct */
-                       ctx->ac.i32_1, /* stream 1 */
-               };
-               tmp = LLVMBuildGEP(builder, tmp, gep_idx, 3, "");
                tmp2 = LLVMBuildTrunc(builder, tid, ctx->ac.i8, "");
-               LLVMBuildStore(builder, tmp2, tmp);
+               LLVMBuildStore(builder, tmp2, ngg_gs_get_emit_primflag_ptr(ctx, tmp, 1));
        }
        ac_build_endif(&ctx->ac, 5130);
 
@@ -1231,19 +1220,13 @@ void gfx10_ngg_gs_emit_epilogue(struct si_shader_context *ctx)
        tmp = LLVMBuildICmp(builder, LLVMIntULT, tid, num_emit_threads, "");
        ac_build_ifcc(&ctx->ac, tmp, 5140);
        {
+               LLVMValueRef flags;
                struct ngg_prim prim = {};
                prim.num_vertices = verts_per_prim;
 
                tmp = ngg_gs_vertex_ptr(ctx, tid);
-               LLVMValueRef gep_idx[3] = {
-                       ctx->ac.i32_0, /* implicit C-style array */
-                       ctx->ac.i32_1, /* second value of struct */
-                       ctx->ac.i32_0, /* primflag */
-               };
-               tmp = LLVMBuildGEP(builder, tmp, gep_idx, 3, "");
-               tmp = LLVMBuildLoad(builder, tmp, "");
-               prim.isnull = LLVMBuildICmp(builder, LLVMIntEQ, tmp,
-                                           LLVMConstInt(ctx->ac.i8, 0, false), "");
+               flags = LLVMBuildLoad(builder, ngg_gs_get_emit_primflag_ptr(ctx, tmp, 0), "");
+               prim.isnull = LLVMBuildNot(builder, LLVMBuildTrunc(builder, flags, ctx->i1, ""), "");
 
                for (unsigned i = 0; i < verts_per_prim; ++i) {
                        prim.index[i] = LLVMBuildSub(builder, vertlive_scan.result_exclusive,
@@ -1251,6 +1234,38 @@ void gfx10_ngg_gs_emit_epilogue(struct si_shader_context *ctx)
                        prim.edgeflag[i] = ctx->ac.i1false;
                }
 
+               /* Geometry shaders output triangle strips, but NGG expects triangles.
+                * We need to change the vertex order for odd triangles to get correct
+                * front/back facing by swapping 2 vertex indices, but we also have to
+                * keep the provoking vertex in the same place.
+                *
+                * If the first vertex is provoking, swap index 1 and 2.
+                * If the last vertex is provoking, swap index 0 and 1.
+                */
+               if (verts_per_prim == 3) {
+                       LLVMValueRef is_odd = LLVMBuildLShr(builder, flags, ctx->ac.i8_1, "");
+                       is_odd = LLVMBuildTrunc(builder, is_odd, ctx->i1, "");
+                       LLVMValueRef flatshade_first =
+                               LLVMBuildICmp(builder, LLVMIntEQ,
+                                             si_unpack_param(ctx, ctx->vs_state_bits, 4, 2),
+                                             ctx->i32_0, "");
+
+                       struct ngg_prim in = prim;
+                       prim.index[0] = LLVMBuildSelect(builder, flatshade_first,
+                                                       in.index[0],
+                                                       LLVMBuildSelect(builder, is_odd,
+                                                                       in.index[1], in.index[0], ""), "");
+                       prim.index[1] = LLVMBuildSelect(builder, flatshade_first,
+                                                       LLVMBuildSelect(builder, is_odd,
+                                                                       in.index[2], in.index[1], ""),
+                                                       LLVMBuildSelect(builder, is_odd,
+                                                                       in.index[0], in.index[1], ""), "");
+                       prim.index[2] = LLVMBuildSelect(builder, flatshade_first,
+                                                       LLVMBuildSelect(builder, is_odd,
+                                                                       in.index[1], in.index[2], ""),
+                                                       in.index[2], "");
+               }
+
                build_export_prim(ctx, &prim);
        }
        ac_build_endif(&ctx->ac, 5140);
@@ -1262,25 +1277,17 @@ void gfx10_ngg_gs_emit_epilogue(struct si_shader_context *ctx)
                struct si_shader_output_values outputs[PIPE_MAX_SHADER_OUTPUTS];
 
                tmp = ngg_gs_vertex_ptr(ctx, tid);
-               LLVMValueRef gep_idx[3] = {
-                       ctx->ac.i32_0, /* implicit C-style array */
-                       ctx->ac.i32_1, /* second value of struct */
-                       ctx->ac.i32_1, /* stream 1: source data index */
-               };
-               tmp = LLVMBuildGEP(builder, tmp, gep_idx, 3, "");
-               tmp = LLVMBuildLoad(builder, tmp, "");
+               tmp = LLVMBuildLoad(builder, ngg_gs_get_emit_primflag_ptr(ctx, tmp, 1), "");
                tmp = LLVMBuildZExt(builder, tmp, ctx->ac.i32, "");
                const LLVMValueRef vertexptr = ngg_gs_vertex_ptr(ctx, tmp);
 
                unsigned out_idx = 0;
-               gep_idx[1] = ctx->ac.i32_0;
                for (unsigned i = 0; i < info->num_outputs; i++) {
                        outputs[i].semantic_name = info->output_semantic_name[i];
                        outputs[i].semantic_index = info->output_semantic_index[i];
 
                        for (unsigned j = 0; j < 4; j++, out_idx++) {
-                               gep_idx[2] = LLVMConstInt(ctx->ac.i32, out_idx, false);
-                               tmp = LLVMBuildGEP(builder, vertexptr, gep_idx, 3, "");
+                               tmp = ngg_gs_get_emit_output_ptr(ctx, vertexptr, out_idx);
                                tmp = LLVMBuildLoad(builder, tmp, "");
                                outputs[i].values[j] = ac_to_float(&ctx->ac, tmp);
                                outputs[i].vertex_stream[j] =