iris: Use iris_flush_bits_for_history in iris_transfer_flush_region
[mesa.git] / src / gallium / drivers / radeonsi / si_state_draw.c
index ad1cf246996626409f485c43af5945a1becaf775..d780547659ec81429aac3186bba5cc53a0e58fc4 100644 (file)
  */
 
 #include "si_build_pm4.h"
-#include "gfx9d.h"
+#include "sid.h"
 
 #include "util/u_index_modify.h"
 #include "util/u_log.h"
 #include "util/u_upload_mgr.h"
 #include "util/u_prim.h"
+#include "util/u_suballoc.h"
 
 #include "ac_debug.h"
 
@@ -251,6 +252,11 @@ static void si_emit_derived_tess_state(struct si_context *sctx,
                                  C_VS_STATE_LS_OUT_VERTEX_SIZE;
        sctx->current_vs_state |= tcs_in_layout;
 
+       /* We should be able to support in-shader LDS use with LLVM >= 9
+        * by just adding the lds_sizes together, but it has never
+        * been tested. */
+       assert(ls_current->config.lds_size == 0);
+
        if (sctx->chip_class >= GFX9) {
                unsigned hs_rsrc2 = ls_current->config.rsrc2 |
                                    S_00B42C_LDS_SIZE(lds_size);
@@ -676,7 +682,9 @@ static void si_emit_draw_packets(struct si_context *sctx,
                                 struct pipe_resource *indexbuf,
                                 unsigned index_size,
                                 unsigned index_offset,
-                                unsigned instance_count)
+                                unsigned instance_count,
+                                bool dispatch_prim_discard_cs,
+                                unsigned original_index_size)
 {
        struct pipe_draw_indirect_info *indirect = info->indirect;
        struct radeon_cmdbuf *cs = sctx->gfx_cs;
@@ -735,13 +743,15 @@ static void si_emit_draw_packets(struct si_context *sctx,
                        sctx->last_index_size = index_size;
                }
 
-               index_max_size = (indexbuf->width0 - index_offset) /
-                                 index_size;
-               index_va = si_resource(indexbuf)->gpu_address + index_offset;
+               if (original_index_size) {
+                       index_max_size = (indexbuf->width0 - index_offset) /
+                                         original_index_size;
+                       index_va = si_resource(indexbuf)->gpu_address + index_offset;
 
-               radeon_add_to_buffer_list(sctx, sctx->gfx_cs,
-                                     si_resource(indexbuf),
-                                     RADEON_USAGE_READ, RADEON_PRIO_INDEX_BUFFER);
+                       radeon_add_to_buffer_list(sctx, sctx->gfx_cs,
+                                             si_resource(indexbuf),
+                                             RADEON_USAGE_READ, RADEON_PRIO_INDEX_BUFFER);
+               }
        } else {
                /* On GFX7 and later, non-indexed draws overwrite VGT_INDEX_TYPE,
                 * so the state must be re-emitted before the next indexed draw.
@@ -828,7 +838,7 @@ static void si_emit_draw_packets(struct si_context *sctx,
                }
 
                /* Base vertex and start instance. */
-               base_vertex = index_size ? info->index_bias : info->start;
+               base_vertex = original_index_size ? info->index_bias : info->start;
 
                if (sctx->num_vs_blit_sgprs) {
                        /* Re-emit draw constants after we leave u_blitter. */
@@ -856,6 +866,17 @@ static void si_emit_draw_packets(struct si_context *sctx,
                }
 
                if (index_size) {
+                       if (dispatch_prim_discard_cs) {
+                               index_va += info->start * original_index_size;
+                               index_max_size = MIN2(index_max_size, info->count);
+
+                               si_dispatch_prim_discard_cs_and_draw(sctx, info,
+                                                                    original_index_size,
+                                                                    base_vertex,
+                                                                    index_va, index_max_size);
+                               return;
+                       }
+
                        index_va += info->start * index_size;
 
                        radeon_emit(cs, PKT3(PKT3_DRAW_INDEX_2, 4, render_cond_bit));
@@ -873,12 +894,13 @@ static void si_emit_draw_packets(struct si_context *sctx,
        }
 }
 
-static void si_emit_surface_sync(struct si_context *sctx,
-                                unsigned cp_coher_cntl)
+void si_emit_surface_sync(struct si_context *sctx, struct radeon_cmdbuf *cs,
+                         unsigned cp_coher_cntl)
 {
-       struct radeon_cmdbuf *cs = sctx->gfx_cs;
+       bool compute_ib = !sctx->has_graphics ||
+                         cs == sctx->prim_discard_compute_cs;
 
-       if (sctx->chip_class >= GFX9 || !sctx->has_graphics) {
+       if (sctx->chip_class >= GFX9 || compute_ib) {
                /* Flush caches and wait for the caches to assert idle. */
                radeon_emit(cs, PKT3(PKT3_ACQUIRE_MEM, 5, 0));
                radeon_emit(cs, cp_coher_cntl); /* CP_COHER_CNTL */
@@ -898,10 +920,37 @@ static void si_emit_surface_sync(struct si_context *sctx,
 
        /* ACQUIRE_MEM has an implicit context roll if the current context
         * is busy. */
-       if (sctx->has_graphics)
+       if (!compute_ib)
                sctx->context_roll = true;
 }
 
+void si_prim_discard_signal_next_compute_ib_start(struct si_context *sctx)
+{
+       if (!si_compute_prim_discard_enabled(sctx))
+               return;
+
+       if (!sctx->barrier_buf) {
+               u_suballocator_alloc(sctx->allocator_zeroed_memory, 4, 4,
+                                    &sctx->barrier_buf_offset,
+                                    (struct pipe_resource**)&sctx->barrier_buf);
+       }
+
+       /* Emit a placeholder to signal the next compute IB to start.
+        * See si_compute_prim_discard.c for explanation.
+        */
+       uint32_t signal = 1;
+       si_cp_write_data(sctx, sctx->barrier_buf, sctx->barrier_buf_offset,
+                        4, V_370_MEM, V_370_ME, &signal);
+
+       sctx->last_pkt3_write_data =
+                       &sctx->gfx_cs->current.buf[sctx->gfx_cs->current.cdw - 5];
+
+       /* Only the last occurence of WRITE_DATA will be executed.
+        * The packet will be enabled in si_flush_gfx_cs.
+        */
+       *sctx->last_pkt3_write_data = PKT3(PKT3_NOP, 3, 0);
+}
+
 void si_emit_cache_flush(struct si_context *sctx)
 {
        struct radeon_cmdbuf *cs = sctx->gfx_cs;
@@ -919,8 +968,18 @@ void si_emit_cache_flush(struct si_context *sctx)
        }
 
        uint32_t cp_coher_cntl = 0;
-       uint32_t flush_cb_db = flags & (SI_CONTEXT_FLUSH_AND_INV_CB |
-                                       SI_CONTEXT_FLUSH_AND_INV_DB);
+       const uint32_t flush_cb_db = flags & (SI_CONTEXT_FLUSH_AND_INV_CB |
+                                             SI_CONTEXT_FLUSH_AND_INV_DB);
+       const bool is_barrier = flush_cb_db ||
+                               /* INV_ICACHE == beginning of gfx IB. Checking
+                                * INV_ICACHE fixes corruption for DeusExMD with
+                                * compute-based culling, but I don't know why.
+                                */
+                               flags & (SI_CONTEXT_INV_ICACHE |
+                                        SI_CONTEXT_PS_PARTIAL_FLUSH |
+                                        SI_CONTEXT_VS_PARTIAL_FLUSH) ||
+                               (flags & SI_CONTEXT_CS_PARTIAL_FLUSH &&
+                                sctx->compute_is_busy);
 
        if (flags & SI_CONTEXT_FLUSH_AND_INV_CB)
                sctx->num_cb_cache_flushes++;
@@ -1109,7 +1168,7 @@ void si_emit_cache_flush(struct si_context *sctx)
                /* Invalidate L1 & L2. (L1 is always invalidated on GFX6)
                 * WB must be set on GFX8+ when TC_ACTION is set.
                 */
-               si_emit_surface_sync(sctx, cp_coher_cntl |
+               si_emit_surface_sync(sctx, sctx->gfx_cs, cp_coher_cntl |
                                     S_0085F0_TC_ACTION_ENA(1) |
                                     S_0085F0_TCL1_ACTION_ENA(1) |
                                     S_0301F0_TC_WB_ACTION_ENA(sctx->chip_class >= GFX8));
@@ -1126,7 +1185,7 @@ void si_emit_cache_flush(struct si_context *sctx)
                         *
                         * WB doesn't work without NC.
                         */
-                       si_emit_surface_sync(sctx, cp_coher_cntl |
+                       si_emit_surface_sync(sctx, sctx->gfx_cs, cp_coher_cntl |
                                             S_0301F0_TC_WB_ACTION_ENA(1) |
                                             S_0301F0_TC_NC_ACTION_ENA(1));
                        cp_coher_cntl = 0;
@@ -1134,7 +1193,7 @@ void si_emit_cache_flush(struct si_context *sctx)
                }
                if (flags & SI_CONTEXT_INV_VMEM_L1) {
                        /* Invalidate per-CU VMEM L1. */
-                       si_emit_surface_sync(sctx, cp_coher_cntl |
+                       si_emit_surface_sync(sctx, sctx->gfx_cs, cp_coher_cntl |
                                             S_0085F0_TCL1_ACTION_ENA(1));
                        cp_coher_cntl = 0;
                }
@@ -1142,7 +1201,10 @@ void si_emit_cache_flush(struct si_context *sctx)
 
        /* If TC flushes haven't cleared this... */
        if (cp_coher_cntl)
-               si_emit_surface_sync(sctx, cp_coher_cntl);
+               si_emit_surface_sync(sctx, sctx->gfx_cs, cp_coher_cntl);
+
+       if (is_barrier)
+               si_prim_discard_signal_next_compute_ib_start(sctx);
 
        if (flags & SI_CONTEXT_START_PIPELINE_STATS) {
                radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
@@ -1260,17 +1322,119 @@ static void si_emit_all_states(struct si_context *sctx, const struct pipe_draw_i
                               primitive_restart);
 }
 
+static bool
+si_all_vs_resources_read_only(struct si_context *sctx,
+                             struct pipe_resource *indexbuf)
+{
+       struct radeon_winsys *ws = sctx->ws;
+       struct radeon_cmdbuf *cs = sctx->gfx_cs;
+
+       /* Index buffer. */
+       if (indexbuf &&
+           ws->cs_is_buffer_referenced(cs, si_resource(indexbuf)->buf,
+                                       RADEON_USAGE_WRITE))
+               goto has_write_reference;
+
+       /* Vertex buffers. */
+       struct si_vertex_elements *velems = sctx->vertex_elements;
+       unsigned num_velems = velems->count;
+
+       for (unsigned i = 0; i < num_velems; i++) {
+               if (!((1 << i) & velems->first_vb_use_mask))
+                       continue;
+
+               unsigned vb_index = velems->vertex_buffer_index[i];
+               struct pipe_resource *res = sctx->vertex_buffer[vb_index].buffer.resource;
+               if (!res)
+                       continue;
+
+               if (ws->cs_is_buffer_referenced(cs, si_resource(res)->buf,
+                                               RADEON_USAGE_WRITE))
+                       goto has_write_reference;
+       }
+
+       /* Constant and shader buffers. */
+       struct si_descriptors *buffers =
+               &sctx->descriptors[si_const_and_shader_buffer_descriptors_idx(PIPE_SHADER_VERTEX)];
+       for (unsigned i = 0; i < buffers->num_active_slots; i++) {
+               unsigned index = buffers->first_active_slot + i;
+               struct pipe_resource *res =
+                       sctx->const_and_shader_buffers[PIPE_SHADER_VERTEX].buffers[index];
+               if (!res)
+                       continue;
+
+               if (ws->cs_is_buffer_referenced(cs, si_resource(res)->buf,
+                                               RADEON_USAGE_WRITE))
+                       goto has_write_reference;
+       }
+
+       /* Samplers. */
+       struct si_shader_selector *vs = sctx->vs_shader.cso;
+       if (vs->info.samplers_declared) {
+               unsigned num_samplers = util_last_bit(vs->info.samplers_declared);
+
+               for (unsigned i = 0; i < num_samplers; i++) {
+                       struct pipe_sampler_view *view = sctx->samplers[PIPE_SHADER_VERTEX].views[i];
+                       if (!view)
+                               continue;
+
+                       if (ws->cs_is_buffer_referenced(cs,
+                                                       si_resource(view->texture)->buf,
+                                                       RADEON_USAGE_WRITE))
+                               goto has_write_reference;
+               }
+       }
+
+       /* Images. */
+       if (vs->info.images_declared) {
+               unsigned num_images = util_last_bit(vs->info.images_declared);
+
+               for (unsigned i = 0; i < num_images; i++) {
+                       struct pipe_resource *res = sctx->images[PIPE_SHADER_VERTEX].views[i].resource;
+                       if (!res)
+                               continue;
+
+                       if (ws->cs_is_buffer_referenced(cs, si_resource(res)->buf,
+                                                       RADEON_USAGE_WRITE))
+                               goto has_write_reference;
+               }
+       }
+
+       return true;
+
+has_write_reference:
+       /* If the current gfx IB has enough packets, flush it to remove write
+        * references to buffers.
+        */
+       if (cs->prev_dw + cs->current.cdw > 2048) {
+               si_flush_gfx_cs(sctx, RADEON_FLUSH_ASYNC_START_NEXT_GFX_IB_NOW, NULL);
+               assert(si_all_vs_resources_read_only(sctx, indexbuf));
+               return true;
+       }
+       return false;
+}
+
+static ALWAYS_INLINE bool pd_msg(const char *s)
+{
+       if (SI_PRIM_DISCARD_DEBUG)
+               printf("PD failed: %s\n", s);
+       return false;
+}
+
 static void si_draw_vbo(struct pipe_context *ctx, const struct pipe_draw_info *info)
 {
        struct si_context *sctx = (struct si_context *)ctx;
        struct si_state_rasterizer *rs = sctx->queued.named.rasterizer;
        struct pipe_resource *indexbuf = info->index.resource;
-       unsigned dirty_tex_counter;
+       unsigned dirty_tex_counter, dirty_buf_counter;
        enum pipe_prim_type rast_prim, prim = info->mode;
        unsigned index_size = info->index_size;
        unsigned index_offset = info->indirect ? info->start * index_size : 0;
        unsigned instance_count = info->instance_count;
-       bool primitive_restart = info->primitive_restart;
+       bool primitive_restart = info->primitive_restart &&
+                                (!sctx->screen->options.prim_restart_tri_strips_only ||
+                                 (prim != PIPE_PRIM_TRIANGLE_STRIP &&
+                                  prim != PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY));
 
        if (likely(!info->indirect)) {
                /* GFX6-GFX7 treat instance_count==0 as instance_count==1. There is
@@ -1305,6 +1469,13 @@ static void si_draw_vbo(struct pipe_context *ctx, const struct pipe_draw_info *i
                si_update_all_texture_descriptors(sctx);
        }
 
+       dirty_buf_counter = p_atomic_read(&sctx->screen->dirty_buf_counter);
+       if (unlikely(dirty_buf_counter != sctx->last_dirty_buf_counter)) {
+               sctx->last_dirty_buf_counter = dirty_buf_counter;
+               /* Rebind all buffers unconditionally. */
+               si_rebind_buffer(sctx, NULL);
+       }
+
        si_decompress_textures(sctx, u_bit_consecutive(0, SI_NUM_GRAPHICS_SHADERS));
 
        /* Set the rasterization primitive type.
@@ -1362,7 +1533,7 @@ static void si_draw_vbo(struct pipe_context *ctx, const struct pipe_draw_info *i
                bool gs_tri_strip_adj_fix =
                        !sctx->tes_shader.cso &&
                        prim == PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY &&
-                       !info->primitive_restart;
+                       !primitive_restart;
 
                if (gs_tri_strip_adj_fix != sctx->gs_tri_strip_adj_fix) {
                        sctx->gs_tri_strip_adj_fix = gs_tri_strip_adj_fix;
@@ -1370,9 +1541,6 @@ static void si_draw_vbo(struct pipe_context *ctx, const struct pipe_draw_info *i
                }
        }
 
-       if (sctx->do_update_shaders && !si_update_shaders(sctx))
-               goto return_cleanup;
-
        if (index_size) {
                /* Translate or upload, if needed. */
                /* 8-bit indices are supported on GFX8. */
@@ -1425,6 +1593,11 @@ static void si_draw_vbo(struct pipe_context *ctx, const struct pipe_draw_info *i
                }
        }
 
+       bool dispatch_prim_discard_cs = false;
+       bool prim_discard_cs_instancing = false;
+       unsigned original_index_size = index_size;
+       unsigned direct_count = 0;
+
        if (info->indirect) {
                struct pipe_draw_indirect_info *indirect = info->indirect;
 
@@ -1444,8 +1617,86 @@ static void si_draw_vbo(struct pipe_context *ctx, const struct pipe_draw_info *i
                                si_resource(indirect->indirect_draw_count)->TC_L2_dirty = false;
                        }
                }
+       } else {
+               /* Multiply by 3 for strips and fans to get an approximate vertex
+                * count as triangles. */
+               direct_count = info->count * instance_count *
+                              (prim == PIPE_PRIM_TRIANGLES ? 1 : 3);
+       }
+
+       /* Determine if we can use the primitive discard compute shader. */
+       if (si_compute_prim_discard_enabled(sctx) &&
+           (direct_count > sctx->prim_discard_vertex_count_threshold ?
+            (sctx->compute_num_verts_rejected += direct_count, true) : /* Add, then return true. */
+            (sctx->compute_num_verts_ineligible += direct_count, false)) && /* Add, then return false. */
+           (!info->count_from_stream_output || pd_msg("draw_opaque")) &&
+           (primitive_restart ?
+            /* Supported prim types with primitive restart: */
+            (prim == PIPE_PRIM_TRIANGLE_STRIP || pd_msg("bad prim type with primitive restart")) &&
+            /* Disallow instancing with primitive restart: */
+            (instance_count == 1 || pd_msg("instance_count > 1 with primitive restart")) :
+            /* Supported prim types without primitive restart + allow instancing: */
+            (1 << prim) & ((1 << PIPE_PRIM_TRIANGLES) |
+                           (1 << PIPE_PRIM_TRIANGLE_STRIP) |
+                           (1 << PIPE_PRIM_TRIANGLE_FAN)) &&
+            /* Instancing is limited to 16-bit indices, because InstanceID is packed into VertexID. */
+            /* TODO: DrawArraysInstanced doesn't sometimes work, so it's disabled. */
+            (instance_count == 1 ||
+             (instance_count <= USHRT_MAX && index_size && index_size <= 2) ||
+             pd_msg("instance_count too large or index_size == 4 or DrawArraysInstanced"))) &&
+           (info->drawid == 0 || !sctx->vs_shader.cso->info.uses_drawid || pd_msg("draw_id > 0")) &&
+           (!sctx->render_cond || pd_msg("render condition")) &&
+           /* Forced enablement ignores pipeline statistics queries. */
+           (sctx->screen->debug_flags & (DBG(PD) | DBG(ALWAYS_PD)) ||
+            (!sctx->num_pipeline_stat_queries && !sctx->streamout.prims_gen_query_enabled) ||
+            pd_msg("pipestat or primgen query")) &&
+           (!sctx->vertex_elements->instance_divisor_is_fetched || pd_msg("loads instance divisors")) &&
+           (!sctx->tes_shader.cso || pd_msg("uses tess")) &&
+           (!sctx->gs_shader.cso || pd_msg("uses GS")) &&
+           (!sctx->ps_shader.cso->info.uses_primid || pd_msg("PS uses PrimID")) &&
+#if SI_PRIM_DISCARD_DEBUG /* same as cso->prim_discard_cs_allowed */
+           (!sctx->vs_shader.cso->info.uses_bindless_images || pd_msg("uses bindless images")) &&
+           (!sctx->vs_shader.cso->info.uses_bindless_samplers || pd_msg("uses bindless samplers")) &&
+           (!sctx->vs_shader.cso->info.writes_memory || pd_msg("writes memory")) &&
+           (!sctx->vs_shader.cso->info.writes_viewport_index || pd_msg("writes viewport index")) &&
+           !sctx->vs_shader.cso->info.properties[TGSI_PROPERTY_VS_WINDOW_SPACE_POSITION] &&
+           !sctx->vs_shader.cso->so.num_outputs &&
+#else
+           (sctx->vs_shader.cso->prim_discard_cs_allowed || pd_msg("VS shader uses unsupported features")) &&
+#endif
+           /* Check that all buffers are used for read only, because compute
+            * dispatches can run ahead. */
+           (si_all_vs_resources_read_only(sctx, index_size ? indexbuf : NULL) || pd_msg("write reference"))) {
+               switch (si_prepare_prim_discard_or_split_draw(sctx, info, primitive_restart)) {
+               case SI_PRIM_DISCARD_ENABLED:
+                       original_index_size = index_size;
+                       prim_discard_cs_instancing = instance_count > 1;
+                       dispatch_prim_discard_cs = true;
+
+                       /* The compute shader changes/lowers the following: */
+                       prim = PIPE_PRIM_TRIANGLES;
+                       index_size = 4;
+                       instance_count = 1;
+                       primitive_restart = false;
+                       sctx->compute_num_verts_rejected -= direct_count;
+                       sctx->compute_num_verts_accepted += direct_count;
+                       break;
+               case SI_PRIM_DISCARD_DISABLED:
+                       break;
+               case SI_PRIM_DISCARD_DRAW_SPLIT:
+                       sctx->compute_num_verts_rejected -= direct_count;
+                       goto return_cleanup;
+               }
        }
 
+       if (prim_discard_cs_instancing != sctx->prim_discard_cs_instancing) {
+               sctx->prim_discard_cs_instancing = prim_discard_cs_instancing;
+               sctx->do_update_shaders = true;
+       }
+
+       if (sctx->do_update_shaders && !si_update_shaders(sctx))
+               goto return_cleanup;
+
        si_need_gfx_cs_space(sctx);
 
        if (sctx->bo_list_add_all_gfx_resources)
@@ -1507,7 +1758,8 @@ static void si_draw_vbo(struct pipe_context *ctx, const struct pipe_draw_info *i
                sctx->dirty_atoms = 0;
 
                si_emit_draw_packets(sctx, info, indexbuf, index_size, index_offset,
-                                    instance_count);
+                                    instance_count, dispatch_prim_discard_cs,
+                                    original_index_size);
                /* <-- CUs are busy here. */
 
                /* Start prefetches after the draw has been started. Both will run
@@ -1527,7 +1779,7 @@ static void si_draw_vbo(struct pipe_context *ctx, const struct pipe_draw_info *i
                        cik_emit_prefetch_L2(sctx, true);
 
                if (!si_upload_graphics_shader_descriptors(sctx))
-                       return;
+                       goto return_cleanup;
 
                si_emit_all_states(sctx, info, prim, instance_count,
                                   primitive_restart, masked_atoms);
@@ -1540,7 +1792,8 @@ static void si_draw_vbo(struct pipe_context *ctx, const struct pipe_draw_info *i
                sctx->dirty_atoms = 0;
 
                si_emit_draw_packets(sctx, info, indexbuf, index_size, index_offset,
-                                    instance_count);
+                                    instance_count, dispatch_prim_discard_cs,
+                                    original_index_size);
 
                /* Prefetch the remaining shaders after the draw has been
                 * started. */