radeonsi/gfx10: remove an obsolete VGT_REUSE_OFF workaround
[mesa.git] / src / gallium / drivers / radeonsi / si_state_shaders.c
index 48b8b7368e35d6f4afb3dfe5324077181feefd0e..d3c3677d82b15f665bb177d2cc6c8d9cfd01833b 100644 (file)
@@ -26,6 +26,7 @@
 #include "sid.h"
 
 #include "compiler/nir/nir_serialize.h"
+#include "nir/tgsi_to_nir.h"
 #include "tgsi/tgsi_parse.h"
 #include "util/hash_table.h"
 #include "util/crc32.h"
@@ -972,9 +973,6 @@ static void gfx10_emit_shader_ngg_tail(struct si_context *sctx,
        radeon_opt_set_context_reg(sctx, R_028AAC_VGT_ESGS_RING_ITEMSIZE,
                                   SI_TRACKED_VGT_ESGS_RING_ITEMSIZE,
                                   shader->ctx_reg.ngg.vgt_esgs_ring_itemsize);
-       radeon_opt_set_context_reg(sctx, R_028AB4_VGT_REUSE_OFF,
-                                  SI_TRACKED_VGT_REUSE_OFF,
-                                  shader->ctx_reg.ngg.vgt_reuse_off);
        radeon_opt_set_context_reg(sctx, R_0286C4_SPI_VS_OUT_CONFIG,
                                   SI_TRACKED_SPI_VS_OUT_CONFIG,
                                   shader->ctx_reg.ngg.spi_vs_out_config);
@@ -1052,16 +1050,7 @@ static void gfx10_emit_shader_ngg_tess_gs(struct si_context *sctx)
        gfx10_emit_shader_ngg_tail(sctx, shader, initial_cdw);
 }
 
-static void si_set_ge_pc_alloc(struct si_screen *sscreen,
-                              struct si_pm4_state *pm4, bool culling)
-{
-       si_pm4_set_reg(pm4, R_030980_GE_PC_ALLOC,
-                      S_030980_OVERSUB_EN(1) |
-                      S_030980_NUM_PC_LINES((culling ? 256 : 128) * sscreen->info.max_se - 1));
-}
-
-unsigned si_get_input_prim(const struct si_shader_selector *gs,
-                          unsigned default_worst_case)
+unsigned si_get_input_prim(const struct si_shader_selector *gs)
 {
        if (gs->type == PIPE_SHADER_GEOMETRY)
                return gs->info.properties[TGSI_PROPERTY_GS_INPUT_PRIM];
@@ -1075,7 +1064,7 @@ unsigned si_get_input_prim(const struct si_shader_selector *gs,
        }
 
        /* TODO: Set this correctly if the primitive type is set in the shader key. */
-       return default_worst_case;
+       return PIPE_PRIM_TRIANGLES; /* worst case for all callers */
 }
 
 /**
@@ -1098,8 +1087,7 @@ static void gfx10_shader_ngg(struct si_screen *sscreen, struct si_shader *shader
                gs_info->properties[TGSI_PROPERTY_VS_WINDOW_SPACE_POSITION];
        bool es_enable_prim_id = shader->key.mono.u.vs_export_prim_id || es_info->uses_primid;
        unsigned gs_num_invocations = MAX2(gs_sel->gs_num_invocations, 1);
-       /* Anything above TRIANGLES has the same effect as TRIANGLES here. */
-       unsigned input_prim = si_get_input_prim(gs_sel, PIPE_PRIM_TRIANGLES);
+       unsigned input_prim = si_get_input_prim(gs_sel);
        bool break_wave_at_eoi = false;
        struct si_pm4_state *pm4 = si_get_shader_pm4_state(shader);
        if (!pm4)
@@ -1168,7 +1156,6 @@ static void gfx10_shader_ngg(struct si_screen *sscreen, struct si_shader *shader
                       S_00B22C_USER_SGPR_MSB_GFX10(num_user_sgprs >> 5) |
                       S_00B22C_OC_LDS_EN(es_type == PIPE_SHADER_TESS_EVAL) |
                       S_00B22C_LDS_SIZE(shader->config.lds_size));
-       si_set_ge_pc_alloc(sscreen, pm4, false);
 
        nparams = MAX2(shader->info.nr_param_exports, 1);
        shader->ctx_reg.ngg.spi_vs_out_config =
@@ -1231,6 +1218,24 @@ static void gfx10_shader_ngg(struct si_screen *sscreen, struct si_shader *shader
                S_03096C_VERT_GRP_SIZE(shader->ngg.hw_max_esverts) |
                S_03096C_BREAK_WAVE_AT_EOI(break_wave_at_eoi);
 
+       /* Bug workaround for a possible hang with non-tessellation cases.
+        * Tessellation always sets GE_CNTL.VERT_GRP_SIZE = 0
+        *
+        * Requirement: GE_CNTL.VERT_GRP_SIZE = VGT_GS_ONCHIP_CNTL.ES_VERTS_PER_SUBGRP - 5
+        */
+       if ((sscreen->info.family == CHIP_NAVI10 ||
+            sscreen->info.family == CHIP_NAVI12 ||
+            sscreen->info.family == CHIP_NAVI14) &&
+           (es_type == PIPE_SHADER_VERTEX || gs_type == PIPE_SHADER_VERTEX) && /* = no tess */
+           shader->ngg.hw_max_esverts != 256) {
+               shader->ge_cntl &= C_03096C_VERT_GRP_SIZE;
+
+               if (shader->ngg.hw_max_esverts > 5) {
+                       shader->ge_cntl |=
+                               S_03096C_VERT_GRP_SIZE(shader->ngg.hw_max_esverts - 5);
+               }
+       }
+
        if (window_space) {
                shader->ctx_reg.ngg.pa_cl_vte_cntl =
                        S_028818_VTX_XY_FMT(1) | S_028818_VTX_Z_FMT(1);
@@ -1241,11 +1246,6 @@ static void gfx10_shader_ngg(struct si_screen *sscreen, struct si_shader *shader
                        S_028818_VPORT_Y_SCALE_ENA(1) | S_028818_VPORT_Y_OFFSET_ENA(1) |
                        S_028818_VPORT_Z_SCALE_ENA(1) | S_028818_VPORT_Z_OFFSET_ENA(1);
        }
-
-       shader->ctx_reg.ngg.vgt_reuse_off =
-               S_028AB4_REUSE_OFF(sscreen->info.family == CHIP_NAVI10 &&
-                                  sscreen->info.chip_external_rev == 0x1 &&
-                                  es_type == PIPE_SHADER_TESS_EVAL);
 }
 
 static void si_emit_shader_vs(struct si_context *sctx)
@@ -1355,11 +1355,15 @@ static void si_shader_vs(struct si_screen *sscreen, struct si_shader *shader,
                vgpr_comp_cnt = 0; /* only VertexID is needed for GS-COPY. */
                num_user_sgprs = SI_GSCOPY_NUM_USER_SGPR;
        } else if (shader->selector->type == PIPE_SHADER_VERTEX) {
-               /* VGPR0-3: (VertexID, InstanceID / StepRate0, PrimID, InstanceID)
-                * If PrimID is disabled. InstanceID / StepRate1 is loaded instead.
-                * StepRate0 is set to 1. so that VGPR3 doesn't have to be loaded.
-                */
-               vgpr_comp_cnt = enable_prim_id ? 2 : (shader->info.uses_instanceid ? 1 : 0);
+               if (sscreen->info.chip_class >= GFX10) {
+                       vgpr_comp_cnt = shader->info.uses_instanceid ? 3 : (enable_prim_id ? 2 : 0);
+               } else {
+                       /* VGPR0-3: (VertexID, InstanceID / StepRate0, PrimID, InstanceID)
+                        * If PrimID is disabled. InstanceID / StepRate1 is loaded instead.
+                        * StepRate0 is set to 1. so that VGPR3 doesn't have to be loaded.
+                        */
+                       vgpr_comp_cnt = enable_prim_id ? 2 : (shader->info.uses_instanceid ? 1 : 0);
+               }
 
                if (info->properties[TGSI_PROPERTY_VS_BLIT_SGPRS]) {
                        num_user_sgprs = SI_SGPR_VS_BLIT_DATA +
@@ -1398,8 +1402,6 @@ static void si_shader_vs(struct si_screen *sscreen, struct si_shader *shader,
 
        si_pm4_set_reg(pm4, R_00B120_SPI_SHADER_PGM_LO_VS, va >> 8);
        si_pm4_set_reg(pm4, R_00B124_SPI_SHADER_PGM_HI_VS, S_00B124_MEM_BASE(va >> 40));
-       if (sscreen->info.chip_class >= GFX10)
-               si_set_ge_pc_alloc(sscreen, pm4, false);
 
        uint32_t rsrc1 = S_00B128_VGPRS((shader->config.num_vgprs - 1) /
                                        (sscreen->ge_wave_size == 32 ? 8 : 4)) |
@@ -1690,10 +1692,7 @@ static void si_shader_init_pm4_state(struct si_screen *sscreen,
 static unsigned si_get_alpha_test_func(struct si_context *sctx)
 {
        /* Alpha-test should be disabled if colorbuffer 0 is integer. */
-       if (sctx->queued.named.dsa)
-               return sctx->queued.named.dsa->alpha_func;
-
-       return PIPE_FUNC_ALWAYS;
+       return sctx->queued.named.dsa->alpha_func;
 }
 
 void si_shader_selector_key_vs(struct si_context *sctx,
@@ -1760,13 +1759,11 @@ static void si_shader_selector_key_hw_vs(struct si_context *sctx,
        /* Find out if PS is disabled. */
        bool ps_disabled = true;
        if (ps) {
-               const struct si_state_blend *blend = sctx->queued.named.blend;
-               bool alpha_to_coverage = blend && blend->alpha_to_coverage;
                bool ps_modifies_zs = ps->info.uses_kill ||
                                      ps->info.writes_z ||
                                      ps->info.writes_stencil ||
                                      ps->info.writes_samplemask ||
-                                     alpha_to_coverage ||
+                                     sctx->queued.named.blend->alpha_to_coverage ||
                                      si_get_alpha_test_func(sctx) != PIPE_FUNC_ALWAYS;
                unsigned ps_colormask = si_get_total_colormask(sctx);
 
@@ -1852,10 +1849,11 @@ static inline void si_shader_selector_key(struct pipe_context *ctx,
                        key->mono.u.ff_tcs_inputs_to_copy = sctx->vs_shader.cso->outputs_written;
                break;
        case PIPE_SHADER_TESS_EVAL:
+               key->as_ngg = stages_key.u.ngg;
+
                if (sctx->gs_shader.cso)
                        key->as_es = 1;
                else {
-                       key->as_ngg = stages_key.u.ngg;
                        si_shader_selector_key_hw_vs(sctx, sel, key);
 
                        if (sctx->ps_shader.cso && sctx->ps_shader.cso->info.uses_primid)
@@ -1904,35 +1902,33 @@ static inline void si_shader_selector_key(struct pipe_context *ctx,
                    sel->info.colors_written == 0x1)
                        key->part.ps.epilog.last_cbuf = MAX2(sctx->framebuffer.state.nr_cbufs, 1) - 1;
 
-               if (blend) {
-                       /* Select the shader color format based on whether
-                        * blending or alpha are needed.
-                        */
-                       key->part.ps.epilog.spi_shader_col_format =
-                               (blend->blend_enable_4bit & blend->need_src_alpha_4bit &
-                                sctx->framebuffer.spi_shader_col_format_blend_alpha) |
-                               (blend->blend_enable_4bit & ~blend->need_src_alpha_4bit &
-                                sctx->framebuffer.spi_shader_col_format_blend) |
-                               (~blend->blend_enable_4bit & blend->need_src_alpha_4bit &
-                                sctx->framebuffer.spi_shader_col_format_alpha) |
-                               (~blend->blend_enable_4bit & ~blend->need_src_alpha_4bit &
-                                sctx->framebuffer.spi_shader_col_format);
-                       key->part.ps.epilog.spi_shader_col_format &= blend->cb_target_enabled_4bit;
-
-                       /* The output for dual source blending should have
-                        * the same format as the first output.
-                        */
-                       if (blend->dual_src_blend)
-                               key->part.ps.epilog.spi_shader_col_format |=
-                                       (key->part.ps.epilog.spi_shader_col_format & 0xf) << 4;
-               } else
-                       key->part.ps.epilog.spi_shader_col_format = sctx->framebuffer.spi_shader_col_format;
+               /* Select the shader color format based on whether
+                * blending or alpha are needed.
+                */
+               key->part.ps.epilog.spi_shader_col_format =
+                       (blend->blend_enable_4bit & blend->need_src_alpha_4bit &
+                        sctx->framebuffer.spi_shader_col_format_blend_alpha) |
+                       (blend->blend_enable_4bit & ~blend->need_src_alpha_4bit &
+                        sctx->framebuffer.spi_shader_col_format_blend) |
+                       (~blend->blend_enable_4bit & blend->need_src_alpha_4bit &
+                        sctx->framebuffer.spi_shader_col_format_alpha) |
+                       (~blend->blend_enable_4bit & ~blend->need_src_alpha_4bit &
+                        sctx->framebuffer.spi_shader_col_format);
+               key->part.ps.epilog.spi_shader_col_format &= blend->cb_target_enabled_4bit;
+
+               /* The output for dual source blending should have
+                * the same format as the first output.
+                */
+               if (blend->dual_src_blend) {
+                       key->part.ps.epilog.spi_shader_col_format |=
+                               (key->part.ps.epilog.spi_shader_col_format & 0xf) << 4;
+               }
 
                /* If alpha-to-coverage is enabled, we have to export alpha
                 * even if there is no color buffer.
                 */
                if (!(key->part.ps.epilog.spi_shader_col_format & 0xf) &&
-                   blend && blend->alpha_to_coverage)
+                   blend->alpha_to_coverage)
                        key->part.ps.epilog.spi_shader_col_format |= V_028710_SPI_SHADER_32_AR;
 
                /* On GFX6 and GFX7 except Hawaii, the CB doesn't clamp outputs
@@ -1957,10 +1953,8 @@ static inline void si_shader_selector_key(struct pipe_context *ctx,
                key->part.ps.prolog.color_two_side = rs->two_side && sel->info.colors_read;
                key->part.ps.prolog.flatshade_colors = rs->flatshade && sel->info.colors_read;
 
-               if (sctx->queued.named.blend) {
-                       key->part.ps.epilog.alpha_to_one = sctx->queued.named.blend->alpha_to_one &&
-                                                          rs->multisample_enable;
-               }
+               key->part.ps.epilog.alpha_to_one = blend->alpha_to_one &&
+                                                  rs->multisample_enable;
 
                key->part.ps.prolog.poly_stipple = rs->poly_stipple_enable && is_poly;
                key->part.ps.epilog.poly_line_smoothing = ((is_poly && rs->poly_smooth) ||
@@ -2006,7 +2000,8 @@ static inline void si_shader_selector_key(struct pipe_context *ctx,
                                sel->info.uses_linear_centroid +
                                sel->info.uses_linear_sample > 1;
 
-                       if (sel->info.opcode_count[TGSI_OPCODE_INTERP_SAMPLE])
+                       if (sel->info.uses_persp_opcode_interp_sample ||
+                           sel->info.uses_linear_opcode_interp_sample)
                                key->mono.u.ps.interpolate_at_sample_force_center = 1;
                }
 
@@ -2269,6 +2264,10 @@ current_not_ready:
                        else
                                assert(0);
 
+                       if (sel->type == PIPE_SHADER_GEOMETRY &&
+                           previous_stage_sel->type == PIPE_SHADER_TESS_EVAL)
+                               shader1_key.as_ngg = key->as_ngg;
+
                        mtx_lock(&previous_stage_sel->mutex);
                        ok = si_check_missing_main_part(sscreen,
                                                        previous_stage_sel,
@@ -2429,7 +2428,7 @@ static void si_init_shader_selector_async(void *job, int thread_index)
        if (sel->nir) {
                /* TODO: GS always sets wave size = default. Legacy GS will have
                 * incorrect subgroup_size and ballot_bit_size. */
-               si_lower_nir(sel, si_get_wave_size(sscreen, sel->type, true));
+               si_lower_nir(sel, si_get_wave_size(sscreen, sel->type, true, false));
        }
 
        /* Compile the main shader part for use with a prolog and/or epilog.
@@ -2455,9 +2454,9 @@ static void si_init_shader_selector_async(void *job, int thread_index)
                                              sel->so.num_outputs != 0,
                                              &shader->key);
                if (sscreen->info.chip_class >= GFX10 &&
-                   (((sel->type == PIPE_SHADER_VERTEX ||
-                      sel->type == PIPE_SHADER_TESS_EVAL) &&
+                   ((sel->type == PIPE_SHADER_VERTEX &&
                      !shader->key.as_ls && !shader->key.as_es) ||
+                    sel->type == PIPE_SHADER_TESS_EVAL ||
                     sel->type == PIPE_SHADER_GEOMETRY))
                        shader->key.as_ngg = 1;
 
@@ -2622,7 +2621,8 @@ static void *si_create_shader_selector(struct pipe_context *ctx,
 
        sel->so = state->stream_output;
 
-       if (state->type == PIPE_SHADER_IR_TGSI) {
+       if (state->type == PIPE_SHADER_IR_TGSI &&
+           !sscreen->options.always_nir) {
                sel->tokens = tgsi_dup_tokens(state->tokens);
                if (!sel->tokens) {
                        FREE(sel);
@@ -2631,11 +2631,30 @@ static void *si_create_shader_selector(struct pipe_context *ctx,
 
                tgsi_scan_shader(state->tokens, &sel->info);
                tgsi_scan_tess_ctrl(state->tokens, &sel->info, &sel->tcs_info);
-       } else {
-               assert(state->type == PIPE_SHADER_IR_NIR);
 
-               sel->nir = state->ir.nir;
+               /* Fixup for TGSI: Set which opcode uses which (i,j) pair. */
+               if (sel->info.uses_persp_opcode_interp_centroid)
+                       sel->info.uses_persp_centroid = true;
+
+               if (sel->info.uses_linear_opcode_interp_centroid)
+                       sel->info.uses_linear_centroid = true;
+
+               if (sel->info.uses_persp_opcode_interp_offset ||
+                   sel->info.uses_persp_opcode_interp_sample)
+                       sel->info.uses_persp_center = true;
+
+               if (sel->info.uses_linear_opcode_interp_offset ||
+                   sel->info.uses_linear_opcode_interp_sample)
+                       sel->info.uses_linear_center = true;
+       } else {
+               if (state->type == PIPE_SHADER_IR_TGSI) {
+                       sel->nir = tgsi_to_nir(state->tokens, ctx->screen);
+               } else {
+                       assert(state->type == PIPE_SHADER_IR_NIR);
+                       sel->nir = state->ir.nir;
+               }
 
+               si_nir_lower_ps_inputs(sel->nir);
                si_nir_opts(sel->nir);
                si_nir_scan_shader(sel->nir, &sel->info);
                si_nir_scan_tess_ctrl(sel->nir, &sel->tcs_info);
@@ -2682,21 +2701,6 @@ static void *si_create_shader_selector(struct pipe_context *ctx,
                        sel->pos_writes_edgeflag = true;
        }
 
-       /* Set which opcode uses which (i,j) pair. */
-       if (sel->info.uses_persp_opcode_interp_centroid)
-               sel->info.uses_persp_centroid = true;
-
-       if (sel->info.uses_linear_opcode_interp_centroid)
-               sel->info.uses_linear_centroid = true;
-
-       if (sel->info.uses_persp_opcode_interp_offset ||
-           sel->info.uses_persp_opcode_interp_sample)
-               sel->info.uses_persp_center = true;
-
-       if (sel->info.uses_linear_opcode_interp_offset ||
-           sel->info.uses_linear_opcode_interp_sample)
-               sel->info.uses_linear_center = true;
-
        switch (sel->type) {
        case PIPE_SHADER_GEOMETRY:
                sel->gs_output_prim =
@@ -2999,6 +3003,16 @@ static bool si_update_ngg(struct si_context *sctx)
                new_ngg = false;
 
        if (new_ngg != sctx->ngg) {
+               /* Transitioning from NGG to legacy GS requires VGT_FLUSH on Navi10-14.
+                * VGT_FLUSH is also emitted at the beginning of IBs when legacy GS ring
+                * pointers are set.
+                */
+               if ((sctx->family == CHIP_NAVI10 ||
+                    sctx->family == CHIP_NAVI12 ||
+                    sctx->family == CHIP_NAVI14) &&
+                   !new_ngg)
+                       sctx->flags |= SI_CONTEXT_VGT_FLUSH;
+
                sctx->ngg = new_ngg;
                sctx->last_rast_prim = -1; /* reset this so that it gets updated */
                return true;
@@ -3837,15 +3851,9 @@ bool si_update_shaders(struct si_context *sctx)
        if (sctx->gs_shader.cso)
                key.u.gs = 1;
 
-       if (sctx->chip_class >= GFX10) {
-               key.u.ngg = sctx->ngg;
-
-               if (sctx->gs_shader.cso)
-                       key.u.streamout = !!sctx->gs_shader.cso->so.num_outputs;
-               else if (sctx->tes_shader.cso)
-                       key.u.streamout = !!sctx->tes_shader.cso->so.num_outputs;
-               else
-                       key.u.streamout = !!sctx->vs_shader.cso->so.num_outputs;
+       if (sctx->ngg) {
+               key.u.ngg = 1;
+               key.u.streamout = !!si_get_vs(sctx)->cso->so.num_outputs;
        }
 
        /* Update TCS and TES. */