gallium: remove PIPE_CAP_TEXTURE_SHADOW_MAP
[mesa.git] / src / gallium / drivers / radeonsi / si_descriptors.c
index 41f6e054615b8de6411ba8e354e298577c9a372a..17115e1355a01a51f2591c49d1c0ce19abc37287 100644 (file)
@@ -19,9 +19,6 @@
  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  * USE OR OTHER DEALINGS IN THE SOFTWARE.
- *
- * Authors:
- *      Marek Olšák <marek.olsak@amd.com>
  */
 
 /* Resource binding slots and sampler states (each described with 8 or
@@ -61,6 +58,7 @@
 #include "gfx9d.h"
 
 #include "util/hash_table.h"
+#include "util/u_idalloc.h"
 #include "util/u_format.h"
 #include "util/u_memory.h"
 #include "util/u_upload_mgr.h"
@@ -96,6 +94,11 @@ static uint32_t null_image_descriptor[8] = {
         * descriptor */
 };
 
+static uint64_t si_desc_extract_buffer_address(uint32_t *desc)
+{
+       return desc[0] | ((uint64_t)G_008F04_BASE_ADDRESS_HI(desc[1]) << 32);
+}
+
 static void si_init_descriptor_list(uint32_t *desc_list,
                                    unsigned element_dw_size,
                                    unsigned num_elements,
@@ -111,31 +114,16 @@ static void si_init_descriptor_list(uint32_t *desc_list,
        }
 }
 
-static void si_init_descriptors(struct si_context *sctx,
-                               struct si_descriptors *desc,
+static void si_init_descriptors(struct si_descriptors *desc,
                                unsigned shader_userdata_index,
                                unsigned element_dw_size,
-                               unsigned num_elements,
-                               unsigned first_ce_slot,
-                               unsigned num_ce_slots,
-                               unsigned *ce_offset)
+                               unsigned num_elements)
 {
-       assert(num_elements <= sizeof(desc->dirty_mask)*8);
-
        desc->list = CALLOC(num_elements, element_dw_size * 4);
        desc->element_dw_size = element_dw_size;
        desc->num_elements = num_elements;
-       desc->first_ce_slot = sctx->ce_ib ? first_ce_slot : 0;
-       desc->num_ce_slots = sctx->ce_ib ? num_ce_slots : 0;
-       desc->dirty_mask = u_bit_consecutive64(0, num_elements);
        desc->shader_userdata_offset = shader_userdata_index * 4;
-
-       if (desc->num_ce_slots) {
-               desc->uses_ce = true;
-               desc->ce_offset = *ce_offset;
-
-               *ce_offset += element_dw_size * desc->num_ce_slots * 4;
-       }
+       desc->slot_index_to_bind_directly = -1;
 }
 
 static void si_release_descriptors(struct si_descriptors *desc)
@@ -144,76 +132,8 @@ static void si_release_descriptors(struct si_descriptors *desc)
        FREE(desc->list);
 }
 
-static bool si_ce_upload(struct si_context *sctx, unsigned ce_offset, unsigned size,
-                        unsigned *out_offset, struct r600_resource **out_buf)
-{
-       uint64_t va;
-
-       u_suballocator_alloc(sctx->ce_suballocator, size,
-                            si_optimal_tcc_alignment(sctx, size),
-                            out_offset,
-                            (struct pipe_resource**)out_buf);
-       if (!out_buf)
-                       return false;
-
-       va = (*out_buf)->gpu_address + *out_offset;
-
-       radeon_emit(sctx->ce_ib, PKT3(PKT3_DUMP_CONST_RAM, 3, 0));
-       radeon_emit(sctx->ce_ib, ce_offset);
-       radeon_emit(sctx->ce_ib, size / 4);
-       radeon_emit(sctx->ce_ib, va);
-       radeon_emit(sctx->ce_ib, va >> 32);
-
-       radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx, *out_buf,
-                              RADEON_USAGE_READWRITE, RADEON_PRIO_DESCRIPTORS);
-
-       sctx->ce_need_synchronization = true;
-       return true;
-}
-
-void si_ce_save_all_descriptors_at_ib_end(struct si_context* sctx)
-{
-       bool success = si_ce_upload(sctx, 0, sctx->total_ce_ram_allocated,
-                                   &sctx->ce_ram_saved_offset,
-                                   &sctx->ce_ram_saved_buffer);
-       (void)success;
-       assert(success);
-}
-
-void si_ce_restore_all_descriptors_at_ib_start(struct si_context *sctx)
-{
-       if (!sctx->ce_ram_saved_buffer)
-               return;
-
-       struct radeon_winsys_cs *ib = sctx->ce_preamble_ib;
-       if (!ib)
-               ib = sctx->ce_ib;
-
-       uint64_t va = sctx->ce_ram_saved_buffer->gpu_address +
-                     sctx->ce_ram_saved_offset;
-
-       radeon_emit(ib, PKT3(PKT3_LOAD_CONST_RAM, 3, 0));
-       radeon_emit(ib, va);
-       radeon_emit(ib, va >> 32);
-       radeon_emit(ib, sctx->total_ce_ram_allocated / 4);
-       radeon_emit(ib, 0);
-
-       radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx,
-                                 sctx->ce_ram_saved_buffer,
-                                 RADEON_USAGE_READ, RADEON_PRIO_DESCRIPTORS);
-}
-
-void si_ce_enable_loads(struct radeon_winsys_cs *ib)
-{
-       radeon_emit(ib, PKT3(PKT3_CONTEXT_CONTROL, 1, 0));
-       radeon_emit(ib, CONTEXT_CONTROL_LOAD_ENABLE(1) |
-                       CONTEXT_CONTROL_LOAD_CE_RAM(1));
-       radeon_emit(ib, CONTEXT_CONTROL_SHADOW_ENABLE(1));
-}
-
 static bool si_upload_descriptors(struct si_context *sctx,
-                                 struct si_descriptors *desc,
-                                 struct r600_atom * atom)
+                                 struct si_descriptors *desc)
 {
        unsigned slot_size = desc->element_dw_size * 4;
        unsigned first_slot_offset = desc->first_active_slot * slot_size;
@@ -226,59 +146,44 @@ static bool si_upload_descriptors(struct si_context *sctx,
        if (!upload_size)
                return true;
 
-       if (desc->uses_ce) {
-               const uint32_t *list = desc->list +
-                                      desc->first_ce_slot * desc->element_dw_size;
-               uint64_t mask = (desc->dirty_mask >> desc->first_ce_slot) &
-                               u_bit_consecutive64(0, desc->num_ce_slots);
-
-
-               while (mask) {
-                       int begin, count;
-                       u_bit_scan_consecutive_range64(&mask, &begin, &count);
-
-                       begin *= desc->element_dw_size;
-                       count *= desc->element_dw_size;
-
-                       radeon_emit(sctx->ce_ib,
-                                   PKT3(PKT3_WRITE_CONST_RAM, count, 0));
-                       radeon_emit(sctx->ce_ib, desc->ce_offset + begin * 4);
-                       radeon_emit_array(sctx->ce_ib, list + begin, count);
-               }
-
-               if (!si_ce_upload(sctx,
-                                 desc->ce_offset +
-                                 (first_slot_offset - desc->first_ce_slot * slot_size),
-                                 upload_size, (unsigned*)&desc->buffer_offset,
-                                 &desc->buffer))
-                       return false;
-       } else {
-               uint32_t *ptr;
-
-               u_upload_alloc(sctx->b.b.const_uploader, 0, upload_size,
-                              si_optimal_tcc_alignment(sctx, upload_size),
-                              (unsigned*)&desc->buffer_offset,
-                              (struct pipe_resource**)&desc->buffer,
-                              (void**)&ptr);
-               if (!desc->buffer)
-                       return false; /* skip the draw call */
-
-               util_memcpy_cpu_to_le32(ptr, (char*)desc->list + first_slot_offset,
-                                       upload_size);
-               desc->gpu_list = ptr - first_slot_offset / 4;
+       /* If there is just one active descriptor, bind it directly. */
+       if ((int)desc->first_active_slot == desc->slot_index_to_bind_directly &&
+           desc->num_active_slots == 1) {
+               uint32_t *descriptor = &desc->list[desc->slot_index_to_bind_directly *
+                                                  desc->element_dw_size];
+
+               /* The buffer is already in the buffer list. */
+               r600_resource_reference(&desc->buffer, NULL);
+               desc->gpu_list = NULL;
+               desc->gpu_address = si_desc_extract_buffer_address(descriptor);
+               si_mark_atom_dirty(sctx, &sctx->shader_pointers.atom);
+               return true;
+       }
 
-               radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx, desc->buffer,
-                                   RADEON_USAGE_READ, RADEON_PRIO_DESCRIPTORS);
+       uint32_t *ptr;
+       int buffer_offset;
+       u_upload_alloc(sctx->b.b.const_uploader, 0, upload_size,
+                      si_optimal_tcc_alignment(sctx, upload_size),
+                      (unsigned*)&buffer_offset,
+                      (struct pipe_resource**)&desc->buffer,
+                      (void**)&ptr);
+       if (!desc->buffer) {
+               desc->gpu_address = 0;
+               return false; /* skip the draw call */
        }
 
-       /* The shader pointer should point to slot 0. */
-       desc->buffer_offset -= first_slot_offset;
+       util_memcpy_cpu_to_le32(ptr, (char*)desc->list + first_slot_offset,
+                               upload_size);
+       desc->gpu_list = ptr - first_slot_offset / 4;
 
-       desc->dirty_mask = 0;
+       radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx, desc->buffer,
+                            RADEON_USAGE_READ, RADEON_PRIO_DESCRIPTORS);
 
-       if (atom)
-               si_mark_atom_dirty(sctx, atom);
+       /* The shader pointer should point to slot 0. */
+       buffer_offset -= first_slot_offset;
+       desc->gpu_address = desc->buffer->gpu_address + buffer_offset;
 
+       si_mark_atom_dirty(sctx, &sctx->shader_pointers.atom);
        return true;
 }
 
@@ -294,6 +199,18 @@ si_descriptors_begin_new_cs(struct si_context *sctx, struct si_descriptors *desc
 
 /* SAMPLER VIEWS */
 
+static inline enum radeon_bo_priority
+si_get_sampler_view_priority(struct r600_resource *res)
+{
+       if (res->b.b.target == PIPE_BUFFER)
+               return RADEON_PRIO_SAMPLER_BUFFER;
+
+       if (res->b.b.nr_samples > 1)
+               return RADEON_PRIO_SAMPLER_TEXTURE_MSAA;
+
+       return RADEON_PRIO_SAMPLER_TEXTURE;
+}
+
 static unsigned
 si_sampler_and_image_descriptors_idx(unsigned shader)
 {
@@ -307,12 +224,12 @@ si_sampler_and_image_descriptors(struct si_context *sctx, unsigned shader)
        return &sctx->descriptors[si_sampler_and_image_descriptors_idx(shader)];
 }
 
-static void si_release_sampler_views(struct si_sampler_views *views)
+static void si_release_sampler_views(struct si_samplers *samplers)
 {
        int i;
 
-       for (i = 0; i < ARRAY_SIZE(views->views); i++) {
-               pipe_sampler_view_reference(&views->views[i], NULL);
+       for (i = 0; i < ARRAY_SIZE(samplers->views); i++) {
+               pipe_sampler_view_reference(&samplers->views[i], NULL);
        }
 }
 
@@ -332,12 +249,12 @@ static void si_sampler_view_add_buffer(struct si_context *sctx,
        if (resource->target != PIPE_BUFFER) {
                struct r600_texture *tex = (struct r600_texture*)resource;
 
-               if (tex->is_depth && !r600_can_sample_zs(tex, is_stencil_sampler))
+               if (tex->is_depth && !si_can_sample_zs(tex, is_stencil_sampler))
                        resource = &tex->flushed_depth_texture->resource.b.b;
        }
 
        rres = (struct r600_resource*)resource;
-       priority = r600_get_sampler_view_priority(rres);
+       priority = si_get_sampler_view_priority(rres);
 
        radeon_add_to_buffer_list_check_mem(&sctx->b, &sctx->b.gfx,
                                            rres, usage, priority,
@@ -356,14 +273,14 @@ static void si_sampler_view_add_buffer(struct si_context *sctx,
 }
 
 static void si_sampler_views_begin_new_cs(struct si_context *sctx,
-                                         struct si_sampler_views *views)
+                                         struct si_samplers *samplers)
 {
-       unsigned mask = views->enabled_mask;
+       unsigned mask = samplers->enabled_mask;
 
        /* Add buffers to the CS. */
        while (mask) {
                int i = u_bit_scan(&mask);
-               struct si_sampler_view *sview = (struct si_sampler_view *)views->views[i];
+               struct si_sampler_view *sview = (struct si_sampler_view *)samplers->views[i];
 
                si_sampler_view_add_buffer(sctx, sview->base.texture,
                                           RADEON_USAGE_READ,
@@ -401,14 +318,14 @@ void si_set_mutable_tex_desc_fields(struct si_screen *sscreen,
 {
        uint64_t va, meta_va = 0;
 
-       if (tex->is_depth && !r600_can_sample_zs(tex, is_stencil)) {
+       if (tex->is_depth && !si_can_sample_zs(tex, is_stencil)) {
                tex = tex->flushed_depth_texture;
                is_stencil = false;
        }
 
        va = tex->resource.gpu_address;
 
-       if (sscreen->b.chip_class >= GFX9) {
+       if (sscreen->info.chip_class >= GFX9) {
                /* Only stencil_offset needs to be added here. */
                if (is_stencil)
                        va += tex->surface.u.gfx9.stencil_offset;
@@ -422,7 +339,14 @@ void si_set_mutable_tex_desc_fields(struct si_screen *sscreen,
        state[1] &= C_008F14_BASE_ADDRESS_HI;
        state[1] |= S_008F14_BASE_ADDRESS_HI(va >> 40);
 
-       if (sscreen->b.chip_class >= VI) {
+       /* Only macrotiled modes can set tile swizzle.
+        * GFX9 doesn't use (legacy) base_level_info.
+        */
+       if (sscreen->info.chip_class >= GFX9 ||
+           base_level_info->mode == RADEON_SURF_MODE_2D)
+               state[0] |= tex->surface.tile_swizzle;
+
+       if (sscreen->info.chip_class >= VI) {
                state[6] &= C_008F28_COMPRESSION_EN;
                state[7] = 0;
 
@@ -430,9 +354,13 @@ void si_set_mutable_tex_desc_fields(struct si_screen *sscreen,
                        meta_va = (!tex->dcc_separate_buffer ? tex->resource.gpu_address : 0) +
                                  tex->dcc_offset;
 
-                       if (sscreen->b.chip_class <= VI)
+                       if (sscreen->info.chip_class == VI) {
                                meta_va += base_level_info->dcc_offset;
-               } else if (tex->tc_compatible_htile) {
+                               assert(base_level_info->mode == RADEON_SURF_MODE_2D);
+                       }
+
+                       meta_va |= (uint32_t)tex->surface.tile_swizzle << 8;
+               } else if (vi_tc_compat_htile_enabled(tex, first_level)) {
                        meta_va = tex->resource.gpu_address + tex->htile_offset;
                }
 
@@ -442,7 +370,7 @@ void si_set_mutable_tex_desc_fields(struct si_screen *sscreen,
                }
        }
 
-       if (sscreen->b.chip_class >= GFX9) {
+       if (sscreen->info.chip_class >= GFX9) {
                state[3] &= C_008F1C_SW_MODE;
                state[4] &= C_008F20_PITCH_GFX9;
 
@@ -481,6 +409,20 @@ void si_set_mutable_tex_desc_fields(struct si_screen *sscreen,
        }
 }
 
+static void si_set_sampler_state_desc(struct si_sampler_state *sstate,
+                                     struct si_sampler_view *sview,
+                                     struct r600_texture *tex,
+                                     uint32_t *desc)
+{
+       if (sview && sview->is_integer)
+               memcpy(desc, sstate->integer_val, 4*4);
+       else if (tex && tex->upgraded_depth &&
+                (!sview || !sview->is_stencil_sampler))
+               memcpy(desc, sstate->upgraded_depth_val, 4*4);
+       else
+               memcpy(desc, sstate->val, 4*4);
+}
+
 static void si_set_sampler_view_desc(struct si_context *sctx,
                                     struct si_sampler_view *sview,
                                     struct si_sampler_state *sstate,
@@ -492,7 +434,7 @@ static void si_set_sampler_view_desc(struct si_context *sctx,
 
        if (unlikely(!is_buffer && sview->dcc_incompatible)) {
                if (vi_dcc_enabled(rtex, view->u.tex.first_level))
-                       if (!r600_texture_disable_dcc(&sctx->b, rtex))
+                       if (!si_texture_disable_dcc(&sctx->b, rtex))
                                sctx->b.decompress_dcc(&sctx->b.b, rtex);
 
                sview->dcc_incompatible = false;
@@ -525,35 +467,72 @@ static void si_set_sampler_view_desc(struct si_context *sctx,
                memcpy(desc + 8, null_texture_descriptor, 4*4);
 
                if (sstate)
-                       memcpy(desc + 12, sstate->val, 4*4);
+                       si_set_sampler_state_desc(sstate, sview,
+                                                 is_buffer ? NULL : rtex,
+                                                 desc + 12);
        }
 }
 
+static bool color_needs_decompression(struct r600_texture *rtex)
+{
+       return rtex->fmask.size ||
+              (rtex->dirty_level_mask &&
+               (rtex->cmask.size || rtex->dcc_offset));
+}
+
+static bool depth_needs_decompression(struct r600_texture *rtex)
+{
+       /* If the depth/stencil texture is TC-compatible, no decompression
+        * will be done. The decompression function will only flush DB caches
+        * to make it coherent with shaders. That's necessary because the driver
+        * doesn't flush DB caches in any other case.
+        */
+       return rtex->db_compatible;
+}
+
 static void si_set_sampler_view(struct si_context *sctx,
                                unsigned shader,
                                unsigned slot, struct pipe_sampler_view *view,
                                bool disallow_early_out)
 {
-       struct si_sampler_views *views = &sctx->samplers[shader].views;
+       struct si_samplers *samplers = &sctx->samplers[shader];
        struct si_sampler_view *rview = (struct si_sampler_view*)view;
        struct si_descriptors *descs = si_sampler_and_image_descriptors(sctx, shader);
        unsigned desc_slot = si_get_sampler_slot(slot);
        uint32_t *desc = descs->list + desc_slot * 16;
 
-       if (views->views[slot] == view && !disallow_early_out)
+       if (samplers->views[slot] == view && !disallow_early_out)
                return;
 
        if (view) {
                struct r600_texture *rtex = (struct r600_texture *)view->texture;
 
                si_set_sampler_view_desc(sctx, rview,
-                                        views->sampler_states[slot], desc);
+                                        samplers->sampler_states[slot], desc);
 
-               if (rtex->resource.b.b.target == PIPE_BUFFER)
+               if (rtex->resource.b.b.target == PIPE_BUFFER) {
                        rtex->resource.bind_history |= PIPE_BIND_SAMPLER_VIEW;
+                       samplers->needs_depth_decompress_mask &= ~(1u << slot);
+                       samplers->needs_color_decompress_mask &= ~(1u << slot);
+               } else {
+                       if (depth_needs_decompression(rtex)) {
+                               samplers->needs_depth_decompress_mask |= 1u << slot;
+                       } else {
+                               samplers->needs_depth_decompress_mask &= ~(1u << slot);
+                       }
+                       if (color_needs_decompression(rtex)) {
+                               samplers->needs_color_decompress_mask |= 1u << slot;
+                       } else {
+                               samplers->needs_color_decompress_mask &= ~(1u << slot);
+                       }
+
+                       if (rtex->dcc_offset &&
+                           p_atomic_read(&rtex->framebuffers_bound))
+                               sctx->need_check_render_feedback = true;
+               }
 
-               pipe_sampler_view_reference(&views->views[slot], view);
-               views->enabled_mask |= 1u << slot;
+               pipe_sampler_view_reference(&samplers->views[slot], view);
+               samplers->enabled_mask |= 1u << slot;
 
                /* Since this can flush, it must be done after enabled_mask is
                 * updated. */
@@ -561,41 +540,27 @@ static void si_set_sampler_view(struct si_context *sctx,
                                           RADEON_USAGE_READ,
                                           rview->is_stencil_sampler, true);
        } else {
-               pipe_sampler_view_reference(&views->views[slot], NULL);
+               pipe_sampler_view_reference(&samplers->views[slot], NULL);
                memcpy(desc, null_texture_descriptor, 8*4);
                /* Only clear the lower dwords of FMASK. */
                memcpy(desc + 8, null_texture_descriptor, 4*4);
                /* Re-set the sampler state if we are transitioning from FMASK. */
-               if (views->sampler_states[slot])
-                       memcpy(desc + 12,
-                              views->sampler_states[slot]->val, 4*4);
+               if (samplers->sampler_states[slot])
+                       si_set_sampler_state_desc(samplers->sampler_states[slot], NULL, NULL,
+                                                 desc + 12);
 
-               views->enabled_mask &= ~(1u << slot);
+               samplers->enabled_mask &= ~(1u << slot);
+               samplers->needs_depth_decompress_mask &= ~(1u << slot);
+               samplers->needs_color_decompress_mask &= ~(1u << slot);
        }
 
-       descs->dirty_mask |= 1ull << desc_slot;
        sctx->descriptors_dirty |= 1u << si_sampler_and_image_descriptors_idx(shader);
 }
 
-static bool color_needs_decompression(struct r600_texture *rtex)
-{
-       return rtex->fmask.size ||
-              (rtex->dirty_level_mask &&
-               (rtex->cmask.size || rtex->dcc_offset));
-}
-
-static bool depth_needs_decompression(struct r600_texture *rtex,
-                                     struct si_sampler_view *sview)
-{
-       return rtex->db_compatible &&
-              (!rtex->tc_compatible_htile ||
-               !r600_can_sample_zs(rtex, sview->is_stencil_sampler));
-}
-
 static void si_update_shader_needs_decompress_mask(struct si_context *sctx,
                                                   unsigned shader)
 {
-       struct si_textures_info *samplers = &sctx->samplers[shader];
+       struct si_samplers *samplers = &sctx->samplers[shader];
        unsigned shader_bit = 1 << shader;
 
        if (samplers->needs_depth_decompress_mask ||
@@ -612,60 +577,30 @@ static void si_set_sampler_views(struct pipe_context *ctx,
                                 struct pipe_sampler_view **views)
 {
        struct si_context *sctx = (struct si_context *)ctx;
-       struct si_textures_info *samplers = &sctx->samplers[shader];
        int i;
 
        if (!count || shader >= SI_NUM_SHADERS)
                return;
 
-       for (i = 0; i < count; i++) {
-               unsigned slot = start + i;
-
-               if (!views || !views[i]) {
-                       samplers->needs_depth_decompress_mask &= ~(1u << slot);
-                       samplers->needs_color_decompress_mask &= ~(1u << slot);
-                       si_set_sampler_view(sctx, shader, slot, NULL, false);
-                       continue;
-               }
-
-               si_set_sampler_view(sctx, shader, slot, views[i], false);
-
-               if (views[i]->texture && views[i]->texture->target != PIPE_BUFFER) {
-                       struct r600_texture *rtex =
-                               (struct r600_texture*)views[i]->texture;
-                       struct si_sampler_view *rview = (struct si_sampler_view *)views[i];
-
-                       if (depth_needs_decompression(rtex, rview)) {
-                               samplers->needs_depth_decompress_mask |= 1u << slot;
-                       } else {
-                               samplers->needs_depth_decompress_mask &= ~(1u << slot);
-                       }
-                       if (color_needs_decompression(rtex)) {
-                               samplers->needs_color_decompress_mask |= 1u << slot;
-                       } else {
-                               samplers->needs_color_decompress_mask &= ~(1u << slot);
-                       }
-
-                       if (rtex->dcc_offset &&
-                           p_atomic_read(&rtex->framebuffers_bound))
-                               sctx->need_check_render_feedback = true;
-               } else {
-                       samplers->needs_depth_decompress_mask &= ~(1u << slot);
-                       samplers->needs_color_decompress_mask &= ~(1u << slot);
-               }
+       if (views) {
+               for (i = 0; i < count; i++)
+                       si_set_sampler_view(sctx, shader, start + i, views[i], false);
+       } else {
+               for (i = 0; i < count; i++)
+                       si_set_sampler_view(sctx, shader, start + i, NULL, false);
        }
 
        si_update_shader_needs_decompress_mask(sctx, shader);
 }
 
 static void
-si_samplers_update_needs_color_decompress_mask(struct si_textures_info *samplers)
+si_samplers_update_needs_color_decompress_mask(struct si_samplers *samplers)
 {
-       unsigned mask = samplers->views.enabled_mask;
+       unsigned mask = samplers->enabled_mask;
 
        while (mask) {
                int i = u_bit_scan(&mask);
-               struct pipe_resource *res = samplers->views.views[i]->texture;
+               struct pipe_resource *res = samplers->views[i]->texture;
 
                if (res && res->target != PIPE_BUFFER) {
                        struct r600_texture *rtex = (struct r600_texture *)res;
@@ -682,7 +617,7 @@ si_samplers_update_needs_color_decompress_mask(struct si_textures_info *samplers
 /* IMAGE VIEWS */
 
 static void
-si_release_image_views(struct si_images_info *images)
+si_release_image_views(struct si_images *images)
 {
        unsigned i;
 
@@ -694,7 +629,7 @@ si_release_image_views(struct si_images_info *images)
 }
 
 static void
-si_image_views_begin_new_cs(struct si_context *sctx, struct si_images_info *images)
+si_image_views_begin_new_cs(struct si_context *sctx, struct si_images *images)
 {
        uint mask = images->enabled_mask;
 
@@ -713,7 +648,7 @@ si_image_views_begin_new_cs(struct si_context *sctx, struct si_images_info *imag
 static void
 si_disable_shader_image(struct si_context *ctx, unsigned shader, unsigned slot)
 {
-       struct si_images_info *images = &ctx->images[shader];
+       struct si_images *images = &ctx->images[shader];
 
        if (images->enabled_mask & (1u << slot)) {
                struct si_descriptors *descs = si_sampler_and_image_descriptors(ctx, shader);
@@ -724,8 +659,6 @@ si_disable_shader_image(struct si_context *ctx, unsigned shader, unsigned slot)
 
                memcpy(descs->list + desc_slot*8, null_image_descriptor, 8*4);
                images->enabled_mask &= ~(1u << slot);
-               /* two 8-byte images share one 16-byte slot */
-               descs->dirty_mask |= 1u << (desc_slot / 2);
                ctx->descriptors_dirty |= 1u << si_sampler_and_image_descriptors_idx(shader);
        }
 }
@@ -767,6 +700,16 @@ static void si_set_shader_image_desc(struct si_context *ctx,
                unsigned level = view->u.tex.level;
                unsigned width, height, depth, hw_level;
                bool uses_dcc = vi_dcc_enabled(tex, level);
+               unsigned access = view->access;
+
+               /* Clear the write flag when writes can't occur.
+                * Note that DCC_DECOMPRESS for MSAA doesn't work in some cases,
+                * so we don't wanna trigger it.
+                */
+               if (tex->is_depth || tex->resource.b.b.nr_samples >= 2) {
+                       assert(!"Z/S and MSAA image stores are not supported");
+                       access &= ~PIPE_IMAGE_ACCESS_WRITE;
+               }
 
                assert(!tex->is_depth);
                assert(tex->fmask.size == 0);
@@ -778,7 +721,7 @@ static void si_set_shader_image_desc(struct si_context *ctx,
                         * The decompression is relatively cheap if the surface
                         * has been decompressed already.
                         */
-                       if (!r600_texture_disable_dcc(&ctx->b, tex))
+                       if (!si_texture_disable_dcc(&ctx->b, tex))
                                ctx->b.decompress_dcc(&ctx->b.b, tex);
                }
 
@@ -824,7 +767,7 @@ static void si_set_shader_image(struct si_context *ctx,
                                unsigned slot, const struct pipe_image_view *view,
                                bool skip_decompress)
 {
-       struct si_images_info *images = &ctx->images[shader];
+       struct si_images *images = &ctx->images[shader];
        struct si_descriptors *descs = si_sampler_and_image_descriptors(ctx, shader);
        struct r600_resource *res;
        unsigned desc_slot = si_get_image_slot(slot);
@@ -861,13 +804,13 @@ static void si_set_shader_image(struct si_context *ctx,
        }
 
        images->enabled_mask |= 1u << slot;
-       /* two 8-byte images share one 16-byte slot */
-       descs->dirty_mask |= 1u << (desc_slot / 2);
        ctx->descriptors_dirty |= 1u << si_sampler_and_image_descriptors_idx(shader);
 
        /* Since this can flush, it must be done after enabled_mask is updated. */
        si_sampler_view_add_buffer(ctx, &res->b.b,
-                                  RADEON_USAGE_READWRITE, false, true);
+                                  (view->access & PIPE_IMAGE_ACCESS_WRITE) ?
+                                  RADEON_USAGE_READWRITE : RADEON_USAGE_READ,
+                                  false, true);
 }
 
 static void
@@ -898,7 +841,7 @@ si_set_shader_images(struct pipe_context *pipe,
 }
 
 static void
-si_images_update_needs_color_decompress_mask(struct si_images_info *images)
+si_images_update_needs_color_decompress_mask(struct si_images *images)
 {
        unsigned mask = images->enabled_mask;
 
@@ -925,7 +868,7 @@ static void si_bind_sampler_states(struct pipe_context *ctx,
                                    unsigned start, unsigned count, void **states)
 {
        struct si_context *sctx = (struct si_context *)ctx;
-       struct si_textures_info *samplers = &sctx->samplers[shader];
+       struct si_samplers *samplers = &sctx->samplers[shader];
        struct si_descriptors *desc = si_sampler_and_image_descriptors(sctx, shader);
        struct si_sampler_state **sstates = (struct si_sampler_state**)states;
        int i;
@@ -938,43 +881,46 @@ static void si_bind_sampler_states(struct pipe_context *ctx,
                unsigned desc_slot = si_get_sampler_slot(slot);
 
                if (!sstates[i] ||
-                   sstates[i] == samplers->views.sampler_states[slot])
+                   sstates[i] == samplers->sampler_states[slot])
                        continue;
 
 #ifdef DEBUG
                assert(sstates[i]->magic == SI_SAMPLER_STATE_MAGIC);
 #endif
-               samplers->views.sampler_states[slot] = sstates[i];
+               samplers->sampler_states[slot] = sstates[i];
 
                /* If FMASK is bound, don't overwrite it.
                 * The sampler state will be set after FMASK is unbound.
                 */
-               if (samplers->views.views[slot] &&
-                   samplers->views.views[slot]->texture &&
-                   samplers->views.views[slot]->texture->target != PIPE_BUFFER &&
-                   ((struct r600_texture*)samplers->views.views[slot]->texture)->fmask.size)
+               struct si_sampler_view *sview =
+                       (struct si_sampler_view *)samplers->views[slot];
+
+               struct r600_texture *tex = NULL;
+
+               if (sview && sview->base.texture &&
+                   sview->base.texture->target != PIPE_BUFFER)
+                       tex = (struct r600_texture *)sview->base.texture;
+
+               if (tex && tex->fmask.size)
                        continue;
 
-               memcpy(desc->list + desc_slot * 16 + 12, sstates[i]->val, 4*4);
-               desc->dirty_mask |= 1ull << desc_slot;
+               si_set_sampler_state_desc(sstates[i], sview, tex,
+                                         desc->list + desc_slot * 16 + 12);
+
                sctx->descriptors_dirty |= 1u << si_sampler_and_image_descriptors_idx(shader);
        }
 }
 
 /* BUFFER RESOURCES */
 
-static void si_init_buffer_resources(struct si_context *sctx,
-                                    struct si_buffer_resources *buffers,
+static void si_init_buffer_resources(struct si_buffer_resources *buffers,
                                     struct si_descriptors *descs,
                                     unsigned num_buffers,
-                                    unsigned first_ce_slot,
-                                    unsigned num_ce_slots,
                                     unsigned shader_userdata_index,
                                     enum radeon_bo_usage shader_usage,
                                     enum radeon_bo_usage shader_usage_constbuf,
                                     enum radeon_bo_priority priority,
-                                    enum radeon_bo_priority priority_constbuf,
-                                    unsigned *ce_offset)
+                                    enum radeon_bo_priority priority_constbuf)
 {
        buffers->shader_usage = shader_usage;
        buffers->shader_usage_constbuf = shader_usage_constbuf;
@@ -982,8 +928,7 @@ static void si_init_buffer_resources(struct si_context *sctx,
        buffers->priority_constbuf = priority_constbuf;
        buffers->buffers = CALLOC(num_buffers, sizeof(struct pipe_resource*));
 
-       si_init_descriptors(sctx, descs, shader_userdata_index, 4, num_buffers,
-                           first_ce_slot, num_ce_slots, ce_offset);
+       si_init_descriptors(descs, shader_userdata_index, 4, num_buffers);
 }
 
 static void si_release_buffer_resources(struct si_buffer_resources *buffers,
@@ -1072,7 +1017,6 @@ bool si_upload_vertex_buffer_descriptors(struct si_context *sctx)
        unsigned i, count;
        unsigned desc_list_byte_size;
        unsigned first_vb_use_mask;
-       uint64_t va;
        uint32_t *ptr;
 
        if (!sctx->vertex_buffers_dirty || !velems)
@@ -1090,14 +1034,19 @@ bool si_upload_vertex_buffer_descriptors(struct si_context *sctx)
         * directly through a staging buffer and don't go through
         * the fine-grained upload path.
         */
+       unsigned buffer_offset = 0;
        u_upload_alloc(sctx->b.b.const_uploader, 0,
                       desc_list_byte_size,
                       si_optimal_tcc_alignment(sctx, desc_list_byte_size),
-                      (unsigned*)&desc->buffer_offset,
+                      &buffer_offset,
                       (struct pipe_resource**)&desc->buffer, (void**)&ptr);
-       if (!desc->buffer)
+       if (!desc->buffer) {
+               desc->gpu_address = 0;
                return false;
+       }
 
+       desc->gpu_address = desc->buffer->gpu_address + buffer_offset;
+       desc->list = ptr;
        radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx,
                              desc->buffer, RADEON_USAGE_READ,
                              RADEON_PRIO_DESCRIPTORS);
@@ -1107,7 +1056,6 @@ bool si_upload_vertex_buffer_descriptors(struct si_context *sctx)
        for (i = 0; i < count; i++) {
                struct pipe_vertex_buffer *vb;
                struct r600_resource *rbuffer;
-               unsigned offset;
                unsigned vbo_index = velems->vertex_buffer_index[i];
                uint32_t *desc = &ptr[i*4];
 
@@ -1118,23 +1066,22 @@ bool si_upload_vertex_buffer_descriptors(struct si_context *sctx)
                        continue;
                }
 
-               offset = vb->buffer_offset + velems->src_offset[i];
-               va = rbuffer->gpu_address + offset;
-
-               /* Fill in T# buffer resource description */
-               desc[0] = va;
-               desc[1] = S_008F04_BASE_ADDRESS_HI(va >> 32) |
-                         S_008F04_STRIDE(vb->stride);
+               int offset = (int)vb->buffer_offset + (int)velems->src_offset[i];
+               int64_t va = (int64_t)rbuffer->gpu_address + offset;
+               assert(va > 0);
 
+               int64_t num_records = (int64_t)rbuffer->b.b.width0 - offset;
                if (sctx->b.chip_class != VI && vb->stride) {
                        /* Round up by rounding down and adding 1 */
-                       desc[2] = (vb->buffer.resource->width0 - offset -
-                                  velems->format_size[i]) /
-                                 vb->stride + 1;
-               } else {
-                       desc[2] = vb->buffer.resource->width0 - offset;
+                       num_records = (num_records - velems->format_size[i]) /
+                                     vb->stride + 1;
                }
+               assert(num_records >= 0 && num_records <= UINT_MAX);
 
+               desc[0] = va;
+               desc[1] = S_008F04_BASE_ADDRESS_HI(va >> 32) |
+                         S_008F04_STRIDE(vb->stride);
+               desc[2] = num_records;
                desc[3] = velems->rsrc_word3[i];
 
                if (first_vb_use_mask & (1 << i)) {
@@ -1148,11 +1095,10 @@ bool si_upload_vertex_buffer_descriptors(struct si_context *sctx)
         * on performance (confirmed by testing). New descriptors are always
         * uploaded to a fresh new buffer, so I don't think flushing the const
         * cache is needed. */
-       si_mark_atom_dirty(sctx, &sctx->shader_userdata.atom);
-       if (sctx->b.chip_class >= CIK)
-               si_mark_atom_dirty(sctx, &sctx->prefetch_L2);
+       si_mark_atom_dirty(sctx, &sctx->shader_pointers.atom);
        sctx->vertex_buffers_dirty = false;
        sctx->vertex_buffer_pointer_dirty = true;
+       sctx->prefetch_L2_mask |= SI_PREFETCH_VBO_DESCRIPTORS;
        return true;
 }
 
@@ -1249,7 +1195,6 @@ static void si_set_constant_buffer(struct si_context *sctx,
                buffers->enabled_mask &= ~(1u << slot);
        }
 
-       descs->dirty_mask |= 1u << slot;
        sctx->descriptors_dirty |= 1u << descriptors_idx;
 }
 
@@ -1311,7 +1256,6 @@ static void si_set_shader_buffers(struct pipe_context *ctx,
                        pipe_resource_reference(&buffers->buffers[slot], NULL);
                        memset(desc, 0, sizeof(uint32_t) * 4);
                        buffers->enabled_mask &= ~(1u << slot);
-                       descs->dirty_mask |= 1u << slot;
                        sctx->descriptors_dirty |=
                                1u << si_const_and_shader_buffer_descriptors_idx(shader);
                        continue;
@@ -1338,7 +1282,6 @@ static void si_set_shader_buffers(struct pipe_context *ctx,
                buf->bind_history |= PIPE_BIND_SHADER_BUFFER;
 
                buffers->enabled_mask |= 1u << slot;
-               descs->dirty_mask |= 1u << slot;
                sctx->descriptors_dirty |=
                        1u << si_const_and_shader_buffer_descriptors_idx(shader);
 
@@ -1458,122 +1401,6 @@ void si_set_ring_buffer(struct pipe_context *ctx, uint slot,
                buffers->enabled_mask &= ~(1u << slot);
        }
 
-       descs->dirty_mask |= 1u << slot;
-       sctx->descriptors_dirty |= 1u << SI_DESCS_RW_BUFFERS;
-}
-
-/* STREAMOUT BUFFERS */
-
-static void si_set_streamout_targets(struct pipe_context *ctx,
-                                    unsigned num_targets,
-                                    struct pipe_stream_output_target **targets,
-                                    const unsigned *offsets)
-{
-       struct si_context *sctx = (struct si_context *)ctx;
-       struct si_buffer_resources *buffers = &sctx->rw_buffers;
-       struct si_descriptors *descs = &sctx->descriptors[SI_DESCS_RW_BUFFERS];
-       unsigned old_num_targets = sctx->b.streamout.num_targets;
-       unsigned i, bufidx;
-
-       /* We are going to unbind the buffers. Mark which caches need to be flushed. */
-       if (sctx->b.streamout.num_targets && sctx->b.streamout.begin_emitted) {
-               /* Since streamout uses vector writes which go through TC L2
-                * and most other clients can use TC L2 as well, we don't need
-                * to flush it.
-                *
-                * The only cases which requires flushing it is VGT DMA index
-                * fetching (on <= CIK) and indirect draw data, which are rare
-                * cases. Thus, flag the TC L2 dirtiness in the resource and
-                * handle it at draw call time.
-                */
-               for (i = 0; i < sctx->b.streamout.num_targets; i++)
-                       if (sctx->b.streamout.targets[i])
-                               r600_resource(sctx->b.streamout.targets[i]->b.buffer)->TC_L2_dirty = true;
-
-               /* Invalidate the scalar cache in case a streamout buffer is
-                * going to be used as a constant buffer.
-                *
-                * Invalidate TC L1, because streamout bypasses it (done by
-                * setting GLC=1 in the store instruction), but it can contain
-                * outdated data of streamout buffers.
-                *
-                * VS_PARTIAL_FLUSH is required if the buffers are going to be
-                * used as an input immediately.
-                */
-               sctx->b.flags |= SI_CONTEXT_INV_SMEM_L1 |
-                                SI_CONTEXT_INV_VMEM_L1 |
-                                SI_CONTEXT_VS_PARTIAL_FLUSH;
-       }
-
-       /* All readers of the streamout targets need to be finished before we can
-        * start writing to the targets.
-        */
-       if (num_targets)
-               sctx->b.flags |= SI_CONTEXT_PS_PARTIAL_FLUSH |
-                                SI_CONTEXT_CS_PARTIAL_FLUSH;
-
-       /* Streamout buffers must be bound in 2 places:
-        * 1) in VGT by setting the VGT_STRMOUT registers
-        * 2) as shader resources
-        */
-
-       /* Set the VGT regs. */
-       r600_set_streamout_targets(ctx, num_targets, targets, offsets);
-
-       /* Set the shader resources.*/
-       for (i = 0; i < num_targets; i++) {
-               bufidx = SI_VS_STREAMOUT_BUF0 + i;
-
-               if (targets[i]) {
-                       struct pipe_resource *buffer = targets[i]->buffer;
-                       uint64_t va = r600_resource(buffer)->gpu_address;
-
-                       /* Set the descriptor.
-                        *
-                        * On VI, the format must be non-INVALID, otherwise
-                        * the buffer will be considered not bound and store
-                        * instructions will be no-ops.
-                        */
-                       uint32_t *desc = descs->list + bufidx*4;
-                       desc[0] = va;
-                       desc[1] = S_008F04_BASE_ADDRESS_HI(va >> 32);
-                       desc[2] = 0xffffffff;
-                       desc[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
-                                 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
-                                 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
-                                 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W) |
-                                 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
-
-                       /* Set the resource. */
-                       pipe_resource_reference(&buffers->buffers[bufidx],
-                                               buffer);
-                       radeon_add_to_buffer_list_check_mem(&sctx->b, &sctx->b.gfx,
-                                                           (struct r600_resource*)buffer,
-                                                           buffers->shader_usage,
-                                                           RADEON_PRIO_SHADER_RW_BUFFER,
-                                                           true);
-                       r600_resource(buffer)->bind_history |= PIPE_BIND_STREAM_OUTPUT;
-
-                       buffers->enabled_mask |= 1u << bufidx;
-               } else {
-                       /* Clear the descriptor and unset the resource. */
-                       memset(descs->list + bufidx*4, 0,
-                              sizeof(uint32_t) * 4);
-                       pipe_resource_reference(&buffers->buffers[bufidx],
-                                               NULL);
-                       buffers->enabled_mask &= ~(1u << bufidx);
-               }
-               descs->dirty_mask |= 1u << bufidx;
-       }
-       for (; i < old_num_targets; i++) {
-               bufidx = SI_VS_STREAMOUT_BUF0 + i;
-               /* Clear the descriptor and unset the resource. */
-               memset(descs->list + bufidx*4, 0, sizeof(uint32_t) * 4);
-               pipe_resource_reference(&buffers->buffers[bufidx], NULL);
-               buffers->enabled_mask &= ~(1u << bufidx);
-               descs->dirty_mask |= 1u << bufidx;
-       }
-
        sctx->descriptors_dirty |= 1u << SI_DESCS_RW_BUFFERS;
 }
 
@@ -1582,8 +1409,7 @@ static void si_desc_reset_buffer_offset(struct pipe_context *ctx,
                                        struct pipe_resource *new_buf)
 {
        /* Retrieve the buffer offset from the descriptor. */
-       uint64_t old_desc_va =
-               desc[0] | ((uint64_t)G_008F04_BASE_ADDRESS_HI(desc[1]) << 32);
+       uint64_t old_desc_va = si_desc_extract_buffer_address(desc);
 
        assert(old_buf_va <= old_desc_va);
        uint64_t offset_within_buffer = old_desc_va - old_buf_va;
@@ -1617,42 +1443,40 @@ static void si_set_polygon_stipple(struct pipe_context *ctx,
 static void
 si_resident_handles_update_needs_color_decompress(struct si_context *sctx)
 {
-       unsigned num_resident_tex_handles, num_resident_img_handles;
-       unsigned i;
+       util_dynarray_clear(&sctx->resident_tex_needs_color_decompress);
+       util_dynarray_clear(&sctx->resident_img_needs_color_decompress);
 
-       num_resident_tex_handles = sctx->resident_tex_handles.size /
-                                  sizeof(struct si_texture_handle *);
+       util_dynarray_foreach(&sctx->resident_tex_handles,
+                             struct si_texture_handle *, tex_handle) {
+               struct pipe_resource *res = (*tex_handle)->view->texture;
+               struct r600_texture *rtex;
 
-       for (i = 0; i < num_resident_tex_handles; i++) {
-               struct si_texture_handle *tex_handle =
-                       *util_dynarray_element(&sctx->resident_tex_handles,
-                                              struct si_texture_handle *, i);
-               struct pipe_resource *res = tex_handle->view->texture;
+               if (!res || res->target == PIPE_BUFFER)
+                       continue;
 
-               if (res && res->target != PIPE_BUFFER) {
-                       struct r600_texture *rtex = (struct r600_texture *)res;
+               rtex = (struct r600_texture *)res;
+               if (!color_needs_decompression(rtex))
+                       continue;
 
-                       tex_handle->needs_color_decompress =
-                               color_needs_decompression(rtex);
-               }
+               util_dynarray_append(&sctx->resident_tex_needs_color_decompress,
+                                    struct si_texture_handle *, *tex_handle);
        }
 
-       num_resident_img_handles = sctx->resident_img_handles.size /
-                                  sizeof(struct si_image_handle *);
-
-       for (i = 0; i < num_resident_img_handles; i++) {
-               struct si_image_handle *img_handle =
-                       *util_dynarray_element(&sctx->resident_img_handles,
-                                              struct si_image_handle *, i);
-               struct pipe_image_view *view = &img_handle->view;
+       util_dynarray_foreach(&sctx->resident_img_handles,
+                             struct si_image_handle *, img_handle) {
+               struct pipe_image_view *view = &(*img_handle)->view;
                struct pipe_resource *res = view->resource;
+               struct r600_texture *rtex;
 
-               if (res && res->target != PIPE_BUFFER) {
-                       struct r600_texture *rtex = (struct r600_texture *)res;
+               if (!res || res->target == PIPE_BUFFER)
+                       continue;
 
-                       img_handle->needs_color_decompress =
-                               color_needs_decompression(rtex);
-               }
+               rtex = (struct r600_texture *)res;
+               if (!color_needs_decompression(rtex))
+                       continue;
+
+               util_dynarray_append(&sctx->resident_img_needs_color_decompress,
+                                    struct si_image_handle *, *img_handle);
        }
 }
 
@@ -1692,7 +1516,6 @@ static void si_reset_buffer_resources(struct si_context *sctx,
                        si_desc_reset_buffer_offset(&sctx->b.b,
                                                    descs->list + i*4,
                                                    old_va, buf);
-                       descs->dirty_mask |= 1u << i;
                        sctx->descriptors_dirty |= 1u << descriptors_idx;
 
                        radeon_add_to_buffer_list_check_mem(&sctx->b, &sctx->b.gfx,
@@ -1746,7 +1569,6 @@ static void si_rebind_buffer(struct pipe_context *ctx, struct pipe_resource *buf
 
                        si_desc_reset_buffer_offset(ctx, descs->list + i*4,
                                                    old_va, buf);
-                       descs->dirty_mask |= 1u << i;
                        sctx->descriptors_dirty |= 1u << SI_DESCS_RW_BUFFERS;
 
                        radeon_add_to_buffer_list_check_mem(&sctx->b, &sctx->b.gfx,
@@ -1755,11 +1577,11 @@ static void si_rebind_buffer(struct pipe_context *ctx, struct pipe_resource *buf
                                                            true);
 
                        /* Update the streamout state. */
-                       if (sctx->b.streamout.begin_emitted)
-                               r600_emit_streamout_end(&sctx->b);
-                       sctx->b.streamout.append_bitmask =
-                                       sctx->b.streamout.enabled_mask;
-                       r600_streamout_buffers_dirty(&sctx->b);
+                       if (sctx->streamout.begin_emitted)
+                               si_emit_streamout_end(sctx);
+                       sctx->streamout.append_bitmask =
+                                       sctx->streamout.enabled_mask;
+                       si_streamout_buffers_dirty(sctx);
                }
        }
 
@@ -1787,21 +1609,20 @@ static void si_rebind_buffer(struct pipe_context *ctx, struct pipe_resource *buf
        if (rbuffer->bind_history & PIPE_BIND_SAMPLER_VIEW) {
                /* Texture buffers - update bindings. */
                for (shader = 0; shader < SI_NUM_SHADERS; shader++) {
-                       struct si_sampler_views *views = &sctx->samplers[shader].views;
+                       struct si_samplers *samplers = &sctx->samplers[shader];
                        struct si_descriptors *descs =
                                si_sampler_and_image_descriptors(sctx, shader);
-                       unsigned mask = views->enabled_mask;
+                       unsigned mask = samplers->enabled_mask;
 
                        while (mask) {
                                unsigned i = u_bit_scan(&mask);
-                               if (views->views[i]->texture == buf) {
+                               if (samplers->views[i]->texture == buf) {
                                        unsigned desc_slot = si_get_sampler_slot(i);
 
                                        si_desc_reset_buffer_offset(ctx,
                                                                    descs->list +
                                                                    desc_slot * 16 + 4,
                                                                    old_va, buf);
-                                       descs->dirty_mask |= 1ull << desc_slot;
                                        sctx->descriptors_dirty |=
                                                1u << si_sampler_and_image_descriptors_idx(shader);
 
@@ -1817,7 +1638,7 @@ static void si_rebind_buffer(struct pipe_context *ctx, struct pipe_resource *buf
        /* Shader images */
        if (rbuffer->bind_history & PIPE_BIND_SHADER_IMAGE) {
                for (shader = 0; shader < SI_NUM_SHADERS; ++shader) {
-                       struct si_images_info *images = &sctx->images[shader];
+                       struct si_images *images = &sctx->images[shader];
                        struct si_descriptors *descs =
                                si_sampler_and_image_descriptors(sctx, shader);
                        unsigned mask = images->enabled_mask;
@@ -1834,8 +1655,6 @@ static void si_rebind_buffer(struct pipe_context *ctx, struct pipe_resource *buf
                                        si_desc_reset_buffer_offset(
                                                ctx, descs->list + desc_slot * 8 + 4,
                                                old_va, buf);
-                                       /* two 8-byte images share one 16-byte slot */
-                                       descs->dirty_mask |= 1u << (desc_slot / 2);
                                        sctx->descriptors_dirty |=
                                                1u << si_sampler_and_image_descriptors_idx(shader);
 
@@ -1850,23 +1669,20 @@ static void si_rebind_buffer(struct pipe_context *ctx, struct pipe_resource *buf
 
        /* Bindless texture handles */
        if (rbuffer->texture_handle_allocated) {
-               unsigned num_resident_tex_handles;
+               struct si_descriptors *descs = &sctx->bindless_descriptors;
 
-               num_resident_tex_handles = sctx->resident_tex_handles.size /
-                                          sizeof(struct si_texture_handle *);
-
-               for (i = 0; i < num_resident_tex_handles; i++) {
-                       struct si_texture_handle *tex_handle =
-                               *util_dynarray_element(&sctx->resident_tex_handles,
-                                                      struct si_texture_handle *, i);
-                       struct pipe_sampler_view *view = tex_handle->view;
-                       struct si_bindless_descriptor *desc = tex_handle->desc;
+               util_dynarray_foreach(&sctx->resident_tex_handles,
+                                     struct si_texture_handle *, tex_handle) {
+                       struct pipe_sampler_view *view = (*tex_handle)->view;
+                       unsigned desc_slot = (*tex_handle)->desc_slot;
 
                        if (view->texture == buf) {
                                si_set_buf_desc_address(rbuffer,
                                                        view->u.buf.offset,
-                                                       &desc->desc_list[4]);
-                               desc->dirty = true;
+                                                       descs->list +
+                                                       desc_slot * 16 + 4);
+
+                               (*tex_handle)->desc_dirty = true;
                                sctx->bindless_descriptors_dirty = true;
 
                                radeon_add_to_buffer_list_check_mem(
@@ -1879,17 +1695,12 @@ static void si_rebind_buffer(struct pipe_context *ctx, struct pipe_resource *buf
 
        /* Bindless image handles */
        if (rbuffer->image_handle_allocated) {
-               unsigned num_resident_img_handles;
+               struct si_descriptors *descs = &sctx->bindless_descriptors;
 
-               num_resident_img_handles = sctx->resident_img_handles.size /
-                                          sizeof(struct si_image_handle *);
-
-               for (i = 0; i < num_resident_img_handles; i++) {
-                       struct si_image_handle *img_handle =
-                               *util_dynarray_element(&sctx->resident_img_handles,
-                                                      struct si_image_handle *, i);
-                       struct pipe_image_view *view = &img_handle->view;
-                       struct si_bindless_descriptor *desc = img_handle->desc;
+               util_dynarray_foreach(&sctx->resident_img_handles,
+                                     struct si_image_handle *, img_handle) {
+                       struct pipe_image_view *view = &(*img_handle)->view;
+                       unsigned desc_slot = (*img_handle)->desc_slot;
 
                        if (view->resource == buf) {
                                if (view->access & PIPE_IMAGE_ACCESS_WRITE)
@@ -1897,8 +1708,10 @@ static void si_rebind_buffer(struct pipe_context *ctx, struct pipe_resource *buf
 
                                si_set_buf_desc_address(rbuffer,
                                                        view->u.buf.offset,
-                                                       &desc->desc_list[4]);
-                               desc->dirty = true;
+                                                       descs->list +
+                                                       desc_slot * 16 + 4);
+
+                               (*img_handle)->desc_dirty = true;
                                sctx->bindless_descriptors_dirty = true;
 
                                radeon_add_to_buffer_list_check_mem(
@@ -1924,19 +1737,143 @@ static void si_invalidate_buffer(struct pipe_context *ctx, struct pipe_resource
        uint64_t old_va = rbuffer->gpu_address;
 
        /* Reallocate the buffer in the same pipe_resource. */
-       r600_alloc_resource(&sctx->screen->b, rbuffer);
+       si_alloc_resource(sctx->screen, rbuffer);
 
        si_rebind_buffer(ctx, buf, old_va);
 }
 
+static void si_upload_bindless_descriptor(struct si_context *sctx,
+                                         unsigned desc_slot,
+                                         unsigned num_dwords)
+{
+       struct si_descriptors *desc = &sctx->bindless_descriptors;
+       struct radeon_winsys_cs *cs = sctx->b.gfx.cs;
+       unsigned desc_slot_offset = desc_slot * 16;
+       uint32_t *data;
+       uint64_t va;
+
+       data = desc->list + desc_slot_offset;
+       va = desc->gpu_address + desc_slot_offset * 4;
+
+       radeon_emit(cs, PKT3(PKT3_WRITE_DATA, 2 + num_dwords, 0));
+       radeon_emit(cs, S_370_DST_SEL(V_370_TC_L2) |
+                   S_370_WR_CONFIRM(1) |
+                   S_370_ENGINE_SEL(V_370_ME));
+       radeon_emit(cs, va);
+       radeon_emit(cs, va >> 32);
+       radeon_emit_array(cs, data, num_dwords);
+}
+
+static void si_upload_bindless_descriptors(struct si_context *sctx)
+{
+       if (!sctx->bindless_descriptors_dirty)
+               return;
+
+       /* Wait for graphics/compute to be idle before updating the resident
+        * descriptors directly in memory, in case the GPU is using them.
+        */
+       sctx->b.flags |= SI_CONTEXT_PS_PARTIAL_FLUSH |
+                        SI_CONTEXT_CS_PARTIAL_FLUSH;
+       si_emit_cache_flush(sctx);
+
+       util_dynarray_foreach(&sctx->resident_tex_handles,
+                             struct si_texture_handle *, tex_handle) {
+               unsigned desc_slot = (*tex_handle)->desc_slot;
+
+               if (!(*tex_handle)->desc_dirty)
+                       continue;
+
+               si_upload_bindless_descriptor(sctx, desc_slot, 16);
+               (*tex_handle)->desc_dirty = false;
+       }
+
+       util_dynarray_foreach(&sctx->resident_img_handles,
+                             struct si_image_handle *, img_handle) {
+               unsigned desc_slot = (*img_handle)->desc_slot;
+
+               if (!(*img_handle)->desc_dirty)
+                       continue;
+
+               si_upload_bindless_descriptor(sctx, desc_slot, 8);
+               (*img_handle)->desc_dirty = false;
+       }
+
+       /* Invalidate L1 because it doesn't know that L2 changed. */
+       sctx->b.flags |= SI_CONTEXT_INV_SMEM_L1;
+       si_emit_cache_flush(sctx);
+
+       sctx->bindless_descriptors_dirty = false;
+}
+
+/* Update mutable image descriptor fields of all resident textures. */
+static void si_update_bindless_texture_descriptor(struct si_context *sctx,
+                                                 struct si_texture_handle *tex_handle)
+{
+       struct si_sampler_view *sview = (struct si_sampler_view *)tex_handle->view;
+       struct si_descriptors *desc = &sctx->bindless_descriptors;
+       unsigned desc_slot_offset = tex_handle->desc_slot * 16;
+       uint32_t desc_list[16];
+
+       if (sview->base.texture->target == PIPE_BUFFER)
+               return;
+
+       memcpy(desc_list, desc->list + desc_slot_offset, sizeof(desc_list));
+       si_set_sampler_view_desc(sctx, sview, &tex_handle->sstate,
+                                desc->list + desc_slot_offset);
+
+       if (memcmp(desc_list, desc->list + desc_slot_offset,
+                  sizeof(desc_list))) {
+               tex_handle->desc_dirty = true;
+               sctx->bindless_descriptors_dirty = true;
+       }
+}
+
+static void si_update_bindless_image_descriptor(struct si_context *sctx,
+                                               struct si_image_handle *img_handle)
+{
+       struct si_descriptors *desc = &sctx->bindless_descriptors;
+       unsigned desc_slot_offset = img_handle->desc_slot * 16;
+       struct pipe_image_view *view = &img_handle->view;
+       uint32_t desc_list[8];
+
+       if (view->resource->target == PIPE_BUFFER)
+               return;
+
+       memcpy(desc_list, desc->list + desc_slot_offset,
+              sizeof(desc_list));
+       si_set_shader_image_desc(sctx, view, true,
+                                desc->list + desc_slot_offset);
+
+       if (memcmp(desc_list, desc->list + desc_slot_offset,
+                  sizeof(desc_list))) {
+               img_handle->desc_dirty = true;
+               sctx->bindless_descriptors_dirty = true;
+       }
+}
+
+static void si_update_all_resident_texture_descriptors(struct si_context *sctx)
+{
+       util_dynarray_foreach(&sctx->resident_tex_handles,
+                             struct si_texture_handle *, tex_handle) {
+               si_update_bindless_texture_descriptor(sctx, *tex_handle);
+       }
+
+       util_dynarray_foreach(&sctx->resident_img_handles,
+                             struct si_image_handle *, img_handle) {
+               si_update_bindless_image_descriptor(sctx, *img_handle);
+       }
+
+       si_upload_bindless_descriptors(sctx);
+}
+
 /* Update mutable image descriptor fields of all bound textures. */
 void si_update_all_texture_descriptors(struct si_context *sctx)
 {
        unsigned shader;
 
        for (shader = 0; shader < SI_NUM_SHADERS; shader++) {
-               struct si_sampler_views *samplers = &sctx->samplers[shader].views;
-               struct si_images_info *images = &sctx->images[shader];
+               struct si_samplers *samplers = &sctx->samplers[shader];
+               struct si_images *images = &sctx->images[shader];
                unsigned mask;
 
                /* Images. */
@@ -1969,6 +1906,8 @@ void si_update_all_texture_descriptors(struct si_context *sctx)
 
                si_update_shader_needs_decompress_mask(sctx, shader);
        }
+
+       si_update_all_resident_texture_descriptors(sctx);
 }
 
 /* SHADER USER DATA */
@@ -1983,14 +1922,16 @@ static void si_mark_shader_pointers_dirty(struct si_context *sctx,
        if (shader == PIPE_SHADER_VERTEX)
                sctx->vertex_buffer_pointer_dirty = sctx->vertex_buffers.buffer != NULL;
 
-       si_mark_atom_dirty(sctx, &sctx->shader_userdata.atom);
+       si_mark_atom_dirty(sctx, &sctx->shader_pointers.atom);
 }
 
-static void si_shader_userdata_begin_new_cs(struct si_context *sctx)
+static void si_shader_pointers_begin_new_cs(struct si_context *sctx)
 {
        sctx->shader_pointers_dirty = u_bit_consecutive(0, SI_NUM_DESCS);
        sctx->vertex_buffer_pointer_dirty = sctx->vertex_buffers.buffer != NULL;
-       si_mark_atom_dirty(sctx, &sctx->shader_userdata.atom);
+       si_mark_atom_dirty(sctx, &sctx->shader_pointers.atom);
+       sctx->graphics_bindless_pointer_dirty = sctx->bindless_descriptors.buffer != NULL;
+       sctx->compute_bindless_pointer_dirty = sctx->bindless_descriptors.buffer != NULL;
 }
 
 /* Set a base register address for user data constants in the given shader.
@@ -1999,7 +1940,7 @@ static void si_shader_userdata_begin_new_cs(struct si_context *sctx)
 static void si_set_user_data_base(struct si_context *sctx,
                                  unsigned shader, uint32_t new_base)
 {
-       uint32_t *base = &sctx->shader_userdata.sh_base[shader];
+       uint32_t *base = &sctx->shader_pointers.sh_base[shader];
 
        if (*base != new_base) {
                *base = new_base;
@@ -2051,71 +1992,101 @@ void si_shader_change_notify(struct si_context *sctx)
        }
 }
 
+static void si_emit_shader_pointer_head(struct radeon_winsys_cs *cs,
+                                       struct si_descriptors *desc,
+                                       unsigned sh_base,
+                                       unsigned pointer_count)
+{
+       radeon_emit(cs, PKT3(PKT3_SET_SH_REG, pointer_count * 2, 0));
+       radeon_emit(cs, (sh_base + desc->shader_userdata_offset - SI_SH_REG_OFFSET) >> 2);
+}
+
+static void si_emit_shader_pointer_body(struct radeon_winsys_cs *cs,
+                                       struct si_descriptors *desc)
+{
+       uint64_t va = desc->gpu_address;
+
+       radeon_emit(cs, va);
+       radeon_emit(cs, va >> 32);
+}
+
 static void si_emit_shader_pointer(struct si_context *sctx,
                                   struct si_descriptors *desc,
                                   unsigned sh_base)
 {
        struct radeon_winsys_cs *cs = sctx->b.gfx.cs;
-       uint64_t va;
-
-       if (!desc->buffer)
-               return; /* the pointer is not used by current shaders */
 
-       va = desc->buffer->gpu_address +
-            desc->buffer_offset;
-
-       radeon_emit(cs, PKT3(PKT3_SET_SH_REG, 2, 0));
-       radeon_emit(cs, (sh_base + desc->shader_userdata_offset - SI_SH_REG_OFFSET) >> 2);
-       radeon_emit(cs, va);
-       radeon_emit(cs, va >> 32);
+       si_emit_shader_pointer_head(cs, desc, sh_base, 1);
+       si_emit_shader_pointer_body(cs, desc);
 }
 
-void si_emit_graphics_shader_userdata(struct si_context *sctx,
-                                      struct r600_atom *atom)
+static void si_emit_consecutive_shader_pointers(struct si_context *sctx,
+                                               unsigned pointer_mask,
+                                               unsigned sh_base)
 {
-       unsigned mask;
-       uint32_t *sh_base = sctx->shader_userdata.sh_base;
-       struct si_descriptors *descs;
+       if (!sh_base)
+               return;
 
-       descs = &sctx->descriptors[SI_DESCS_RW_BUFFERS];
+       struct radeon_winsys_cs *cs = sctx->b.gfx.cs;
+       unsigned mask = sctx->shader_pointers_dirty & pointer_mask;
 
-       if (sctx->shader_pointers_dirty & (1 << SI_DESCS_RW_BUFFERS)) {
-               si_emit_shader_pointer(sctx, descs,
-                                      R_00B030_SPI_SHADER_USER_DATA_PS_0);
-               si_emit_shader_pointer(sctx, descs,
-                                      R_00B130_SPI_SHADER_USER_DATA_VS_0);
+       while (mask) {
+               int start, count;
+               u_bit_scan_consecutive_range(&mask, &start, &count);
 
-               if (sctx->b.chip_class >= GFX9) {
-                       /* GFX9 merged LS-HS and ES-GS.
-                        * Set RW_BUFFERS in the special registers, so that
-                        * it's preloaded into s[0:1] instead of s[8:9].
-                        */
-                       si_emit_shader_pointer(sctx, descs,
-                                              R_00B208_SPI_SHADER_USER_DATA_ADDR_LO_GS);
-                       si_emit_shader_pointer(sctx, descs,
-                                              R_00B408_SPI_SHADER_USER_DATA_ADDR_LO_HS);
-               } else {
-                       si_emit_shader_pointer(sctx, descs,
-                                              R_00B230_SPI_SHADER_USER_DATA_GS_0);
-                       si_emit_shader_pointer(sctx, descs,
-                                              R_00B330_SPI_SHADER_USER_DATA_ES_0);
-                       si_emit_shader_pointer(sctx, descs,
-                                              R_00B430_SPI_SHADER_USER_DATA_HS_0);
-               }
+               struct si_descriptors *descs = &sctx->descriptors[start];
+
+               si_emit_shader_pointer_head(cs, descs, sh_base, count);
+               for (int i = 0; i < count; i++)
+                       si_emit_shader_pointer_body(cs, descs + i);
        }
+}
 
-       mask = sctx->shader_pointers_dirty &
-              u_bit_consecutive(SI_DESCS_FIRST_SHADER,
-                                SI_DESCS_FIRST_COMPUTE - SI_DESCS_FIRST_SHADER);
+static void si_emit_global_shader_pointers(struct si_context *sctx,
+                                          struct si_descriptors *descs)
+{
+       if (sctx->b.chip_class == GFX9) {
+               /* Broadcast it to all shader stages. */
+               si_emit_shader_pointer(sctx, descs,
+                                      R_00B530_SPI_SHADER_USER_DATA_COMMON_0);
+               return;
+       }
 
-       while (mask) {
-               unsigned i = u_bit_scan(&mask);
-               unsigned shader = (i - SI_DESCS_FIRST_SHADER) / SI_NUM_SHADER_DESCS;
-               unsigned base = sh_base[shader];
+       si_emit_shader_pointer(sctx, descs,
+                              R_00B030_SPI_SHADER_USER_DATA_PS_0);
+       si_emit_shader_pointer(sctx, descs,
+                              R_00B130_SPI_SHADER_USER_DATA_VS_0);
+       si_emit_shader_pointer(sctx, descs,
+                              R_00B330_SPI_SHADER_USER_DATA_ES_0);
+       si_emit_shader_pointer(sctx, descs,
+                              R_00B230_SPI_SHADER_USER_DATA_GS_0);
+       si_emit_shader_pointer(sctx, descs,
+                              R_00B430_SPI_SHADER_USER_DATA_HS_0);
+       si_emit_shader_pointer(sctx, descs,
+                              R_00B530_SPI_SHADER_USER_DATA_LS_0);
+}
+
+void si_emit_graphics_shader_pointers(struct si_context *sctx,
+                                      struct r600_atom *atom)
+{
+       uint32_t *sh_base = sctx->shader_pointers.sh_base;
 
-               if (base)
-                       si_emit_shader_pointer(sctx, descs + i, base);
+       if (sctx->shader_pointers_dirty & (1 << SI_DESCS_RW_BUFFERS)) {
+               si_emit_global_shader_pointers(sctx,
+                                              &sctx->descriptors[SI_DESCS_RW_BUFFERS]);
        }
+
+       si_emit_consecutive_shader_pointers(sctx, SI_DESCS_SHADER_MASK(VERTEX),
+                                           sh_base[PIPE_SHADER_VERTEX]);
+       si_emit_consecutive_shader_pointers(sctx, SI_DESCS_SHADER_MASK(TESS_CTRL),
+                                           sh_base[PIPE_SHADER_TESS_CTRL]);
+       si_emit_consecutive_shader_pointers(sctx, SI_DESCS_SHADER_MASK(TESS_EVAL),
+                                           sh_base[PIPE_SHADER_TESS_EVAL]);
+       si_emit_consecutive_shader_pointers(sctx, SI_DESCS_SHADER_MASK(GEOMETRY),
+                                           sh_base[PIPE_SHADER_GEOMETRY]);
+       si_emit_consecutive_shader_pointers(sctx, SI_DESCS_SHADER_MASK(FRAGMENT),
+                                           sh_base[PIPE_SHADER_FRAGMENT]);
+
        sctx->shader_pointers_dirty &=
                ~u_bit_consecutive(SI_DESCS_RW_BUFFERS, SI_DESCS_FIRST_COMPUTE);
 
@@ -2124,167 +2095,136 @@ void si_emit_graphics_shader_userdata(struct si_context *sctx,
                                       sh_base[PIPE_SHADER_VERTEX]);
                sctx->vertex_buffer_pointer_dirty = false;
        }
+
+       if (sctx->graphics_bindless_pointer_dirty) {
+               si_emit_global_shader_pointers(sctx,
+                                              &sctx->bindless_descriptors);
+               sctx->graphics_bindless_pointer_dirty = false;
+       }
 }
 
-void si_emit_compute_shader_userdata(struct si_context *sctx)
+void si_emit_compute_shader_pointers(struct si_context *sctx)
 {
        unsigned base = R_00B900_COMPUTE_USER_DATA_0;
-       struct si_descriptors *descs = sctx->descriptors;
-       unsigned compute_mask =
-               u_bit_consecutive(SI_DESCS_FIRST_COMPUTE, SI_NUM_SHADER_DESCS);
-       unsigned mask = sctx->shader_pointers_dirty & compute_mask;
 
-       while (mask) {
-               unsigned i = u_bit_scan(&mask);
+       si_emit_consecutive_shader_pointers(sctx, SI_DESCS_SHADER_MASK(COMPUTE),
+                                           R_00B900_COMPUTE_USER_DATA_0);
+       sctx->shader_pointers_dirty &= ~SI_DESCS_SHADER_MASK(COMPUTE);
 
-               si_emit_shader_pointer(sctx, descs + i, base);
+       if (sctx->compute_bindless_pointer_dirty) {
+               si_emit_shader_pointer(sctx, &sctx->bindless_descriptors, base);
+               sctx->compute_bindless_pointer_dirty = false;
        }
-       sctx->shader_pointers_dirty &= ~compute_mask;
 }
 
 /* BINDLESS */
 
-struct si_bindless_descriptor_slab
-{
-       struct pb_slab base;
-       struct r600_resource *buffer;
-       struct si_bindless_descriptor *entries;
-};
-
-bool si_bindless_descriptor_can_reclaim_slab(void *priv,
-                                            struct pb_slab_entry *entry)
-{
-       /* Do not allow to reclaim any bindless descriptors for now because the
-        * GPU might be using them. This should be improved later on.
-        */
-       return false;
-}
-
-struct pb_slab *si_bindless_descriptor_slab_alloc(void *priv, unsigned heap,
-                                                 unsigned entry_size,
-                                                 unsigned group_index)
+static void si_init_bindless_descriptors(struct si_context *sctx,
+                                        struct si_descriptors *desc,
+                                        unsigned shader_userdata_index,
+                                        unsigned num_elements)
 {
-       struct si_context *sctx = priv;
-       struct si_screen *sscreen = sctx->screen;
-       struct si_bindless_descriptor_slab *slab;
+       MAYBE_UNUSED unsigned desc_slot;
 
-       slab = CALLOC_STRUCT(si_bindless_descriptor_slab);
-       if (!slab)
-               return NULL;
+       si_init_descriptors(desc, shader_userdata_index, 16, num_elements);
+       sctx->bindless_descriptors.num_active_slots = num_elements;
 
-       /* Create a buffer in VRAM for 1024 bindless descriptors. */
-       slab->buffer = (struct r600_resource *)
-               pipe_buffer_create(&sscreen->b.b, 0,
-                                  PIPE_USAGE_DEFAULT, 64 * 1024);
-       if (!slab->buffer)
-               goto fail;
-
-       slab->base.num_entries = slab->buffer->bo_size / entry_size;
-       slab->base.num_free = slab->base.num_entries;
-       slab->entries = CALLOC(slab->base.num_entries, sizeof(*slab->entries));
-       if (!slab->entries)
-               goto fail_buffer;
-
-       LIST_INITHEAD(&slab->base.free);
-
-       for (unsigned i = 0; i < slab->base.num_entries; ++i) {
-               struct si_bindless_descriptor *desc = &slab->entries[i];
-
-               desc->entry.slab = &slab->base;
-               desc->entry.group_index = group_index;
-               desc->buffer = slab->buffer;
-               desc->offset = i * entry_size;
-
-               LIST_ADDTAIL(&desc->entry.head, &slab->base.free);
-       }
+       /* The first bindless descriptor is stored at slot 1, because 0 is not
+        * considered to be a valid handle.
+        */
+       sctx->num_bindless_descriptors = 1;
 
-       /* Add the descriptor to the per-context list. */
-       util_dynarray_append(&sctx->bindless_descriptors,
-                           struct r600_resource *, slab->buffer);
+       /* Track which bindless slots are used (or not). */
+       util_idalloc_init(&sctx->bindless_used_slots);
+       util_idalloc_resize(&sctx->bindless_used_slots, num_elements);
 
-       return &slab->base;
+       /* Reserve slot 0 because it's an invalid handle for bindless. */
+       desc_slot = util_idalloc_alloc(&sctx->bindless_used_slots);
+       assert(desc_slot == 0);
+}
 
-fail_buffer:
-       r600_resource_reference(&slab->buffer, NULL);
-fail:
-       FREE(slab);
-       return NULL;
+static void si_release_bindless_descriptors(struct si_context *sctx)
+{
+       si_release_descriptors(&sctx->bindless_descriptors);
+       util_idalloc_fini(&sctx->bindless_used_slots);
 }
 
-void si_bindless_descriptor_slab_free(void *priv, struct pb_slab *pslab)
+static unsigned si_get_first_free_bindless_slot(struct si_context *sctx)
 {
-       struct si_context *sctx = priv;
-       struct si_bindless_descriptor_slab *slab =
-               (struct si_bindless_descriptor_slab *)pslab;
+       struct si_descriptors *desc = &sctx->bindless_descriptors;
+       unsigned desc_slot;
+
+       desc_slot = util_idalloc_alloc(&sctx->bindless_used_slots);
+       if (desc_slot >= desc->num_elements) {
+               /* The array of bindless descriptors is full, resize it. */
+               unsigned slot_size = desc->element_dw_size * 4;
+               unsigned new_num_elements = desc->num_elements * 2;
 
-       /* Remove the descriptor from the per-context list. */
-       util_dynarray_delete_unordered(&sctx->bindless_descriptors,
-                                      struct r600_resource *, slab->buffer);
+               desc->list = REALLOC(desc->list, desc->num_elements * slot_size,
+                                    new_num_elements * slot_size);
+               desc->num_elements = new_num_elements;
+               desc->num_active_slots = new_num_elements;
+       }
 
-       r600_resource_reference(&slab->buffer, NULL);
-       FREE(slab->entries);
-       FREE(slab);
+       assert(desc_slot);
+       return desc_slot;
 }
 
-static struct si_bindless_descriptor *
+static unsigned
 si_create_bindless_descriptor(struct si_context *sctx, uint32_t *desc_list,
                              unsigned size)
 {
-       struct si_screen *sscreen = sctx->screen;
-       struct si_bindless_descriptor *desc;
-       struct pb_slab_entry *entry;
-       void *ptr;
+       struct si_descriptors *desc = &sctx->bindless_descriptors;
+       unsigned desc_slot, desc_slot_offset;
 
-       /* Sub-allocate the bindless descriptor from a slab to avoid dealing
-        * with a ton of buffers and for reducing the winsys overhead.
+       /* Find a free slot. */
+       desc_slot = si_get_first_free_bindless_slot(sctx);
+
+       /* For simplicity, sampler and image bindless descriptors use fixed
+        * 16-dword slots for now. Image descriptors only need 8-dword but this
+        * doesn't really matter because no real apps use image handles.
         */
-       entry = pb_slab_alloc(&sctx->bindless_descriptor_slabs, 64, 0);
-       if (!entry)
-               return NULL;
+       desc_slot_offset = desc_slot * 16;
 
-       desc = NULL;
-       desc = container_of(entry, desc, entry);
+       /* Copy the descriptor into the array. */
+       memcpy(desc->list + desc_slot_offset, desc_list, size);
 
-       /* Upload the descriptor directly in VRAM. Because the slabs are
-        * currently never reclaimed, we don't need to synchronize the
-        * operation.
+       /* Re-upload the whole array of bindless descriptors into a new buffer.
         */
-       ptr = sscreen->b.ws->buffer_map(desc->buffer->buf, NULL,
-                                       PIPE_TRANSFER_WRITE |
-                                       PIPE_TRANSFER_UNSYNCHRONIZED);
-       util_memcpy_cpu_to_le32(ptr + desc->offset, desc_list, size);
+       if (!si_upload_descriptors(sctx, desc))
+               return 0;
 
-       /* Keep track of the initial descriptor especially for buffers
-        * invalidation because we might need to know the previous address.
-        */
-       memcpy(desc->desc_list, desc_list, sizeof(desc->desc_list));
+       /* Make sure to re-emit the shader pointers for all stages. */
+       sctx->graphics_bindless_pointer_dirty = true;
+       sctx->compute_bindless_pointer_dirty = true;
 
-       return desc;
+       return desc_slot;
 }
 
-static void si_invalidate_bindless_buf_desc(struct si_context *sctx,
-                                           struct si_bindless_descriptor *desc,
-                                           struct pipe_resource *resource,
-                                           uint64_t offset)
+static void si_update_bindless_buffer_descriptor(struct si_context *sctx,
+                                                unsigned desc_slot,
+                                                struct pipe_resource *resource,
+                                                uint64_t offset,
+                                                bool *desc_dirty)
 {
+       struct si_descriptors *desc = &sctx->bindless_descriptors;
        struct r600_resource *buf = r600_resource(resource);
-       uint32_t *desc_list = desc->desc_list;
+       unsigned desc_slot_offset = desc_slot * 16;
+       uint32_t *desc_list = desc->list + desc_slot_offset + 4;
        uint64_t old_desc_va;
 
        assert(resource->target == PIPE_BUFFER);
 
        /* Retrieve the old buffer addr from the descriptor. */
-       old_desc_va  = desc_list[0];
-       old_desc_va |= ((uint64_t)G_008F04_BASE_ADDRESS_HI(desc_list[1]) << 32);
+       old_desc_va = si_desc_extract_buffer_address(desc_list);
 
        if (old_desc_va != buf->gpu_address + offset) {
                /* The buffer has been invalidated when the handle wasn't
                 * resident, update the descriptor and the dirty flag.
                 */
-               si_set_buf_desc_address(buf, offset, &desc_list[4]);
+               si_set_buf_desc_address(buf, offset, &desc_list[0]);
 
-               desc->dirty = true;
-               sctx->bindless_descriptors_dirty = true;
+               *desc_dirty = true;
        }
 }
 
@@ -2313,22 +2253,20 @@ static uint64_t si_create_texture_handle(struct pipe_context *ctx,
        }
 
        si_set_sampler_view_desc(sctx, sview, sstate, &desc_list[0]);
+       memcpy(&tex_handle->sstate, sstate, sizeof(*sstate));
        ctx->delete_sampler_state(ctx, sstate);
 
-       tex_handle->desc = si_create_bindless_descriptor(sctx, desc_list,
-                                                        sizeof(desc_list));
-       if (!tex_handle->desc) {
+       tex_handle->desc_slot = si_create_bindless_descriptor(sctx, desc_list,
+                                                             sizeof(desc_list));
+       if (!tex_handle->desc_slot) {
                FREE(tex_handle);
                return 0;
        }
 
-       handle = tex_handle->desc->buffer->gpu_address +
-                tex_handle->desc->offset;
+       handle = tex_handle->desc_slot;
 
        if (!_mesa_hash_table_insert(sctx->tex_handles, (void *)handle,
                                     tex_handle)) {
-               pb_slab_free(&sctx->bindless_descriptor_slabs,
-                            &tex_handle->desc->entry);
                FREE(tex_handle);
                return 0;
        }
@@ -2352,10 +2290,11 @@ static void si_delete_texture_handle(struct pipe_context *ctx, uint64_t handle)
 
        tex_handle = (struct si_texture_handle *)entry->data;
 
+       /* Allow this descriptor slot to be re-used. */
+       util_idalloc_free(&sctx->bindless_used_slots, tex_handle->desc_slot);
+
        pipe_sampler_view_reference(&tex_handle->view, NULL);
        _mesa_hash_table_remove(sctx->tex_handles, entry);
-       pb_slab_free(&sctx->bindless_descriptor_slabs,
-                    &tex_handle->desc->entry);
        FREE(tex_handle);
 }
 
@@ -2379,20 +2318,39 @@ static void si_make_texture_handle_resident(struct pipe_context *ctx,
                        struct r600_texture *rtex =
                                (struct r600_texture *)sview->base.texture;
 
-                       tex_handle->needs_depth_decompress =
-                               depth_needs_decompression(rtex, sview);
-                       tex_handle->needs_color_decompress =
-                               color_needs_decompression(rtex);
+                       if (depth_needs_decompression(rtex)) {
+                               util_dynarray_append(
+                                       &sctx->resident_tex_needs_depth_decompress,
+                                       struct si_texture_handle *,
+                                       tex_handle);
+                       }
+
+                       if (color_needs_decompression(rtex)) {
+                               util_dynarray_append(
+                                       &sctx->resident_tex_needs_color_decompress,
+                                       struct si_texture_handle *,
+                                       tex_handle);
+                       }
 
                        if (rtex->dcc_offset &&
                            p_atomic_read(&rtex->framebuffers_bound))
                                sctx->need_check_render_feedback = true;
+
+                       si_update_bindless_texture_descriptor(sctx, tex_handle);
                } else {
-                       si_invalidate_bindless_buf_desc(sctx, tex_handle->desc,
-                                                       sview->base.texture,
-                                                       sview->base.u.buf.offset);
+                       si_update_bindless_buffer_descriptor(sctx,
+                                                            tex_handle->desc_slot,
+                                                            sview->base.texture,
+                                                            sview->base.u.buf.offset,
+                                                            &tex_handle->desc_dirty);
                }
 
+               /* Re-upload the descriptor if it has been updated while it
+                * wasn't resident.
+                */
+               if (tex_handle->desc_dirty)
+                       sctx->bindless_descriptors_dirty = true;
+
                /* Add the texture handle to the per-context list. */
                util_dynarray_append(&sctx->resident_tex_handles,
                                     struct si_texture_handle *, tex_handle);
@@ -2400,11 +2358,6 @@ static void si_make_texture_handle_resident(struct pipe_context *ctx,
                /* Add the buffers to the current CS in case si_begin_new_cs()
                 * is not going to be called.
                 */
-               radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx,
-                                         tex_handle->desc->buffer,
-                                         RADEON_USAGE_READWRITE,
-                                         RADEON_PRIO_DESCRIPTORS);
-
                si_sampler_view_add_buffer(sctx, sview->base.texture,
                                           RADEON_USAGE_READ,
                                           sview->is_stencil_sampler, false);
@@ -2413,6 +2366,16 @@ static void si_make_texture_handle_resident(struct pipe_context *ctx,
                util_dynarray_delete_unordered(&sctx->resident_tex_handles,
                                               struct si_texture_handle *,
                                               tex_handle);
+
+               if (sview->base.texture->target != PIPE_BUFFER) {
+                       util_dynarray_delete_unordered(
+                               &sctx->resident_tex_needs_depth_decompress,
+                               struct si_texture_handle *, tex_handle);
+
+                       util_dynarray_delete_unordered(
+                               &sctx->resident_tex_needs_color_decompress,
+                               struct si_texture_handle *, tex_handle);
+               }
        }
 }
 
@@ -2421,7 +2384,7 @@ static uint64_t si_create_image_handle(struct pipe_context *ctx,
 {
        struct si_context *sctx = (struct si_context *)ctx;
        struct si_image_handle *img_handle;
-       uint32_t desc_list[16];
+       uint32_t desc_list[8];
        uint64_t handle;
 
        if (!view || !view->resource)
@@ -2436,20 +2399,17 @@ static uint64_t si_create_image_handle(struct pipe_context *ctx,
 
        si_set_shader_image_desc(sctx, view, false, &desc_list[0]);
 
-       img_handle->desc = si_create_bindless_descriptor(sctx, desc_list,
-                                                        sizeof(desc_list));
-       if (!img_handle->desc) {
+       img_handle->desc_slot = si_create_bindless_descriptor(sctx, desc_list,
+                                                             sizeof(desc_list));
+       if (!img_handle->desc_slot) {
                FREE(img_handle);
                return 0;
        }
 
-       handle = img_handle->desc->buffer->gpu_address +
-                img_handle->desc->offset;
+       handle = img_handle->desc_slot;
 
        if (!_mesa_hash_table_insert(sctx->img_handles, (void *)handle,
                                     img_handle)) {
-               pb_slab_free(&sctx->bindless_descriptor_slabs,
-                            &img_handle->desc->entry);
                FREE(img_handle);
                return 0;
        }
@@ -2475,8 +2435,6 @@ static void si_delete_image_handle(struct pipe_context *ctx, uint64_t handle)
 
        util_copy_image_view(&img_handle->view, NULL);
        _mesa_hash_table_remove(sctx->img_handles, entry);
-       pb_slab_free(&sctx->bindless_descriptor_slabs,
-                    &img_handle->desc->entry);
        FREE(img_handle);
 }
 
@@ -2487,6 +2445,7 @@ static void si_make_image_handle_resident(struct pipe_context *ctx,
        struct si_context *sctx = (struct si_context *)ctx;
        struct si_image_handle *img_handle;
        struct pipe_image_view *view;
+       struct r600_resource *res;
        struct hash_entry *entry;
 
        entry = _mesa_hash_table_search(sctx->img_handles, (void *)handle);
@@ -2495,27 +2454,39 @@ static void si_make_image_handle_resident(struct pipe_context *ctx,
 
        img_handle = (struct si_image_handle *)entry->data;
        view = &img_handle->view;
+       res = (struct r600_resource *)view->resource;
 
        if (resident) {
-               struct r600_resource *res =
-                       (struct r600_resource *)view->resource;
-
                if (res->b.b.target != PIPE_BUFFER) {
                        struct r600_texture *rtex = (struct r600_texture *)res;
                        unsigned level = view->u.tex.level;
 
-                       img_handle->needs_color_decompress =
-                               color_needs_decompression(rtex);
+                       if (color_needs_decompression(rtex)) {
+                               util_dynarray_append(
+                                       &sctx->resident_img_needs_color_decompress,
+                                       struct si_image_handle *,
+                                       img_handle);
+                       }
 
                        if (vi_dcc_enabled(rtex, level) &&
                            p_atomic_read(&rtex->framebuffers_bound))
                                sctx->need_check_render_feedback = true;
+
+                       si_update_bindless_image_descriptor(sctx, img_handle);
                } else {
-                       si_invalidate_bindless_buf_desc(sctx, img_handle->desc,
-                                                       view->resource,
-                                                       view->u.buf.offset);
+                       si_update_bindless_buffer_descriptor(sctx,
+                                                            img_handle->desc_slot,
+                                                            view->resource,
+                                                            view->u.buf.offset,
+                                                            &img_handle->desc_dirty);
                }
 
+               /* Re-upload the descriptor if it has been updated while it
+                * wasn't resident.
+                */
+               if (img_handle->desc_dirty)
+                       sctx->bindless_descriptors_dirty = true;
+
                /* Add the image handle to the per-context list. */
                util_dynarray_append(&sctx->resident_img_handles,
                                     struct si_image_handle *, img_handle);
@@ -2523,11 +2494,6 @@ static void si_make_image_handle_resident(struct pipe_context *ctx,
                /* Add the buffers to the current CS in case si_begin_new_cs()
                 * is not going to be called.
                 */
-               radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx,
-                                         img_handle->desc->buffer,
-                                         RADEON_USAGE_READWRITE,
-                                         RADEON_PRIO_DESCRIPTORS);
-
                si_sampler_view_add_buffer(sctx, view->resource,
                                           (access & PIPE_IMAGE_ACCESS_WRITE) ?
                                           RADEON_USAGE_READWRITE :
@@ -2537,6 +2503,13 @@ static void si_make_image_handle_resident(struct pipe_context *ctx,
                util_dynarray_delete_unordered(&sctx->resident_img_handles,
                                               struct si_image_handle *,
                                               img_handle);
+
+               if (res->b.b.target != PIPE_BUFFER) {
+                       util_dynarray_delete_unordered(
+                               &sctx->resident_img_needs_color_decompress,
+                               struct si_image_handle *,
+                               img_handle);
+               }
        }
 }
 
@@ -2544,40 +2517,17 @@ static void si_make_image_handle_resident(struct pipe_context *ctx,
 void si_all_resident_buffers_begin_new_cs(struct si_context *sctx)
 {
        unsigned num_resident_tex_handles, num_resident_img_handles;
-       unsigned num_bindless_descriptors;
-       unsigned i;
 
        num_resident_tex_handles = sctx->resident_tex_handles.size /
                                   sizeof(struct si_texture_handle *);
        num_resident_img_handles = sctx->resident_img_handles.size /
                                   sizeof(struct si_image_handle *);
 
-       /* Skip adding the bindless descriptors when no handles are resident.
-        */
-       if (!num_resident_tex_handles && !num_resident_img_handles)
-               return;
-
-       num_bindless_descriptors = sctx->bindless_descriptors.size /
-                                  sizeof(struct r600_resource *);
-
-       /* Add all bindless descriptors. */
-       for (i = 0; i < num_bindless_descriptors; i++) {
-               struct r600_resource *desc =
-                       *util_dynarray_element(&sctx->bindless_descriptors,
-                                              struct r600_resource *, i);
-
-               radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx, desc,
-                                         RADEON_USAGE_READWRITE,
-                                         RADEON_PRIO_DESCRIPTORS);
-       }
-
        /* Add all resident texture handles. */
-       for (i = 0; i < num_resident_tex_handles; i++) {
-               struct si_texture_handle *tex_handle =
-                       *util_dynarray_element(&sctx->resident_tex_handles,
-                                              struct si_texture_handle *, i);
+       util_dynarray_foreach(&sctx->resident_tex_handles,
+                             struct si_texture_handle *, tex_handle) {
                struct si_sampler_view *sview =
-                       (struct si_sampler_view *)tex_handle->view;
+                       (struct si_sampler_view *)(*tex_handle)->view;
 
                si_sampler_view_add_buffer(sctx, sview->base.texture,
                                           RADEON_USAGE_READ,
@@ -2585,11 +2535,9 @@ void si_all_resident_buffers_begin_new_cs(struct si_context *sctx)
        }
 
        /* Add all resident image handles. */
-       for (i = 0; i < num_resident_img_handles; i++) {
-               struct si_image_handle *img_handle =
-                       *util_dynarray_element(&sctx->resident_img_handles,
-                                              struct si_image_handle *, i);
-               struct pipe_image_view *view = &img_handle->view;
+       util_dynarray_foreach(&sctx->resident_img_handles,
+                             struct si_image_handle *, img_handle) {
+               struct pipe_image_view *view = &(*img_handle)->view;
 
                si_sampler_view_add_buffer(sctx, view->resource,
                                           RADEON_USAGE_READWRITE,
@@ -2602,52 +2550,9 @@ void si_all_resident_buffers_begin_new_cs(struct si_context *sctx)
 
 /* INIT/DEINIT/UPLOAD */
 
-/* GFX9 has only 4KB of CE, while previous chips had 32KB. In order
- * to make CE RAM as useful as possible, this defines limits
- * for the number slots that can be in CE RAM on GFX9. If a shader
- * is using more, descriptors will be uploaded to memory directly and
- * CE won't be used.
- *
- * These numbers are based on shader-db.
- */
-static unsigned gfx9_max_ce_samplers[SI_NUM_SHADERS] = {
-       [PIPE_SHADER_VERTEX] = 0,
-       [PIPE_SHADER_TESS_CTRL] = 0,
-       [PIPE_SHADER_TESS_EVAL] = 1,
-       [PIPE_SHADER_GEOMETRY] = 0,
-       [PIPE_SHADER_FRAGMENT] = 24,
-       [PIPE_SHADER_COMPUTE] = 16,
-};
-static unsigned gfx9_max_ce_images[SI_NUM_SHADERS] = {
-       /* these must be even due to slot alignment */
-       [PIPE_SHADER_VERTEX] = 0,
-       [PIPE_SHADER_TESS_CTRL] = 0,
-       [PIPE_SHADER_TESS_EVAL] = 0,
-       [PIPE_SHADER_GEOMETRY] = 0,
-       [PIPE_SHADER_FRAGMENT] = 2,
-       [PIPE_SHADER_COMPUTE] = 8,
-};
-static unsigned gfx9_max_ce_const_buffers[SI_NUM_SHADERS] = {
-       [PIPE_SHADER_VERTEX] = 9,
-       [PIPE_SHADER_TESS_CTRL] = 3,
-       [PIPE_SHADER_TESS_EVAL] = 5,
-       [PIPE_SHADER_GEOMETRY] = 0,
-       [PIPE_SHADER_FRAGMENT] = 8,
-       [PIPE_SHADER_COMPUTE] = 6,
-};
-static unsigned gfx9_max_ce_shader_buffers[SI_NUM_SHADERS] = {
-       [PIPE_SHADER_VERTEX] = 0,
-       [PIPE_SHADER_TESS_CTRL] = 0,
-       [PIPE_SHADER_TESS_EVAL] = 0,
-       [PIPE_SHADER_GEOMETRY] = 0,
-       [PIPE_SHADER_FRAGMENT] = 12,
-       [PIPE_SHADER_COMPUTE] = 13,
-};
-
 void si_init_all_descriptors(struct si_context *sctx)
 {
        int i;
-       unsigned ce_offset = 0;
 
        STATIC_ASSERT(GFX9_SGPR_TCS_CONST_AND_SHADER_BUFFERS % 2 == 0);
        STATIC_ASSERT(GFX9_SGPR_GS_CONST_AND_SHADER_BUFFERS % 2 == 0);
@@ -2657,50 +2562,31 @@ void si_init_all_descriptors(struct si_context *sctx)
                bool gfx9_gs = false;
                unsigned num_sampler_slots = SI_NUM_IMAGES / 2 + SI_NUM_SAMPLERS;
                unsigned num_buffer_slots = SI_NUM_SHADER_BUFFERS + SI_NUM_CONST_BUFFERS;
+               struct si_descriptors *desc;
 
-               unsigned first_sampler_ce_slot = 0;
-               unsigned num_sampler_ce_slots = num_sampler_slots;
-
-               unsigned first_buffer_ce_slot = 0;
-               unsigned num_buffer_ce_slots = num_buffer_slots;
-
-               /* Adjust CE slot ranges based on GFX9 CE RAM limits. */
                if (sctx->b.chip_class >= GFX9) {
                        gfx9_tcs = i == PIPE_SHADER_TESS_CTRL;
                        gfx9_gs = i == PIPE_SHADER_GEOMETRY;
-
-                       first_sampler_ce_slot =
-                               si_get_image_slot(gfx9_max_ce_images[i] - 1) / 2;
-                       num_sampler_ce_slots = gfx9_max_ce_images[i] / 2 +
-                                              gfx9_max_ce_samplers[i];
-
-                       first_buffer_ce_slot =
-                               si_get_shaderbuf_slot(gfx9_max_ce_shader_buffers[i] - 1);
-                       num_buffer_ce_slots = gfx9_max_ce_shader_buffers[i] +
-                                             gfx9_max_ce_const_buffers[i];
                }
 
-               si_init_buffer_resources(sctx, &sctx->const_and_shader_buffers[i],
-                                        si_const_and_shader_buffer_descriptors(sctx, i),
+               desc = si_const_and_shader_buffer_descriptors(sctx, i);
+               si_init_buffer_resources(&sctx->const_and_shader_buffers[i], desc,
                                         num_buffer_slots,
-                                        first_buffer_ce_slot, num_buffer_ce_slots,
                                         gfx9_tcs ? GFX9_SGPR_TCS_CONST_AND_SHADER_BUFFERS :
                                         gfx9_gs ? GFX9_SGPR_GS_CONST_AND_SHADER_BUFFERS :
                                                   SI_SGPR_CONST_AND_SHADER_BUFFERS,
                                         RADEON_USAGE_READWRITE,
                                         RADEON_USAGE_READ,
                                         RADEON_PRIO_SHADER_RW_BUFFER,
-                                        RADEON_PRIO_CONST_BUFFER,
-                                        &ce_offset);
+                                        RADEON_PRIO_CONST_BUFFER);
+               desc->slot_index_to_bind_directly = si_get_constbuf_slot(0);
 
-               struct si_descriptors *desc = si_sampler_and_image_descriptors(sctx, i);
-               si_init_descriptors(sctx, desc,
+               desc = si_sampler_and_image_descriptors(sctx, i);
+               si_init_descriptors(desc,
                                    gfx9_tcs ? GFX9_SGPR_TCS_SAMPLERS_AND_IMAGES :
                                    gfx9_gs ? GFX9_SGPR_GS_SAMPLERS_AND_IMAGES :
                                              SI_SGPR_SAMPLERS_AND_IMAGES,
-                                   16, num_sampler_slots,
-                                   first_sampler_ce_slot, num_sampler_ce_slots,
-                                   &ce_offset);
+                                   16, num_sampler_slots);
 
                int j;
                for (j = 0; j < SI_NUM_IMAGES; j++)
@@ -2709,27 +2595,28 @@ void si_init_all_descriptors(struct si_context *sctx)
                        memcpy(desc->list + j * 8, null_texture_descriptor, 8 * 4);
        }
 
-       si_init_buffer_resources(sctx, &sctx->rw_buffers,
+       si_init_buffer_resources(&sctx->rw_buffers,
                                 &sctx->descriptors[SI_DESCS_RW_BUFFERS],
-                                SI_NUM_RW_BUFFERS, 0, SI_NUM_RW_BUFFERS,
-                                SI_SGPR_RW_BUFFERS,
+                                SI_NUM_RW_BUFFERS, SI_SGPR_RW_BUFFERS,
                                 /* The second set of usage/priority is used by
                                  * const buffers in RW buffer slots. */
                                 RADEON_USAGE_READWRITE, RADEON_USAGE_READ,
-                                RADEON_PRIO_SHADER_RINGS, RADEON_PRIO_CONST_BUFFER,
-                                &ce_offset);
+                                RADEON_PRIO_SHADER_RINGS, RADEON_PRIO_CONST_BUFFER);
        sctx->descriptors[SI_DESCS_RW_BUFFERS].num_active_slots = SI_NUM_RW_BUFFERS;
 
-       si_init_descriptors(sctx, &sctx->vertex_buffers, SI_SGPR_VERTEX_BUFFERS,
-                           4, SI_NUM_VERTEX_BUFFERS, 0, 0, NULL);
+       si_init_descriptors(&sctx->vertex_buffers, SI_SGPR_VERTEX_BUFFERS,
+                           4, SI_NUM_VERTEX_BUFFERS);
+       FREE(sctx->vertex_buffers.list); /* not used */
+       sctx->vertex_buffers.list = NULL;
 
-       sctx->descriptors_dirty = u_bit_consecutive(0, SI_NUM_DESCS);
-       sctx->total_ce_ram_allocated = ce_offset;
+       /* Initialize an array of 1024 bindless descriptors, when the limit is
+        * reached, just make it larger and re-upload the whole array.
+        */
+       si_init_bindless_descriptors(sctx, &sctx->bindless_descriptors,
+                                    SI_SGPR_BINDLESS_SAMPLERS_AND_IMAGES,
+                                    1024);
 
-       if (sctx->b.chip_class >= GFX9)
-               assert(ce_offset <= 4096);
-       else
-               assert(ce_offset <= 32768);
+       sctx->descriptors_dirty = u_bit_consecutive(0, SI_NUM_DESCS);
 
        /* Set pipe_context functions. */
        sctx->b.b.bind_sampler_states = si_bind_sampler_states;
@@ -2738,7 +2625,6 @@ void si_init_all_descriptors(struct si_context *sctx)
        sctx->b.b.set_polygon_stipple = si_set_polygon_stipple;
        sctx->b.b.set_shader_buffers = si_set_shader_buffers;
        sctx->b.b.set_sampler_views = si_set_sampler_views;
-       sctx->b.b.set_stream_output_targets = si_set_streamout_targets;
        sctx->b.b.create_texture_handle = si_create_texture_handle;
        sctx->b.b.delete_texture_handle = si_delete_texture_handle;
        sctx->b.b.make_texture_handle_resident = si_make_texture_handle_resident;
@@ -2749,8 +2635,8 @@ void si_init_all_descriptors(struct si_context *sctx)
        sctx->b.rebind_buffer = si_rebind_buffer;
 
        /* Shader user data. */
-       si_init_atom(sctx, &sctx->shader_userdata.atom, &sctx->atoms.s.shader_userdata,
-                    si_emit_graphics_shader_userdata);
+       si_init_atom(sctx, &sctx->shader_pointers.atom, &sctx->atoms.s.shader_pointers,
+                    si_emit_graphics_shader_pointers);
 
        /* Set default and immutable mappings. */
        si_set_user_data_base(sctx, PIPE_SHADER_VERTEX, R_00B130_SPI_SHADER_USER_DATA_VS_0);
@@ -2769,79 +2655,8 @@ void si_init_all_descriptors(struct si_context *sctx)
        si_set_user_data_base(sctx, PIPE_SHADER_FRAGMENT, R_00B030_SPI_SHADER_USER_DATA_PS_0);
 }
 
-static void si_upload_bindless_descriptor(struct si_context *sctx,
-                                         struct si_bindless_descriptor *desc)
-{
-       struct radeon_winsys_cs *cs = sctx->b.gfx.cs;
-       uint64_t va = desc->buffer->gpu_address + desc->offset;
-       unsigned num_dwords = sizeof(desc->desc_list) / 4;
-
-       radeon_emit(cs, PKT3(PKT3_WRITE_DATA, 2 + num_dwords, 0));
-       radeon_emit(cs, S_370_DST_SEL(V_370_TC_L2) |
-                   S_370_WR_CONFIRM(1) |
-                   S_370_ENGINE_SEL(V_370_ME));
-       radeon_emit(cs, va);
-       radeon_emit(cs, va >> 32);
-       radeon_emit_array(cs, desc->desc_list, num_dwords);
-}
-
-static void si_upload_bindless_descriptors(struct si_context *sctx)
+static bool si_upload_shader_descriptors(struct si_context *sctx, unsigned mask)
 {
-       unsigned num_resident_tex_handles, num_resident_img_handles;
-       unsigned i;
-
-       if (!sctx->bindless_descriptors_dirty)
-               return;
-
-       /* Wait for graphics/compute to be idle before updating the resident
-        * descriptors directly in memory, in case the GPU is using them.
-        */
-       sctx->b.flags |= SI_CONTEXT_PS_PARTIAL_FLUSH |
-                        SI_CONTEXT_CS_PARTIAL_FLUSH;
-       si_emit_cache_flush(sctx);
-
-       num_resident_tex_handles = sctx->resident_tex_handles.size /
-                                  sizeof(struct si_texture_handle *);
-
-       for (i = 0; i < num_resident_tex_handles; i++) {
-               struct si_texture_handle *tex_handle =
-                       *util_dynarray_element(&sctx->resident_tex_handles,
-                                              struct si_texture_handle *, i);
-               struct si_bindless_descriptor *desc = tex_handle->desc;
-
-               if (!desc->dirty)
-                       continue;
-
-               si_upload_bindless_descriptor(sctx, desc);
-               desc->dirty = false;
-       }
-
-       num_resident_img_handles = sctx->resident_img_handles.size /
-                                  sizeof(struct si_image_handle *);
-
-       for (i = 0; i < num_resident_img_handles; i++) {
-               struct si_image_handle *img_handle =
-                       *util_dynarray_element(&sctx->resident_img_handles,
-                                              struct si_image_handle *, i);
-               struct si_bindless_descriptor *desc = img_handle->desc;
-
-               if (!desc->dirty)
-                       continue;
-
-               si_upload_bindless_descriptor(sctx, desc);
-               desc->dirty = false;
-       }
-
-       /* Invalidate L1 because it doesn't know that L2 changed. */
-       sctx->b.flags |= SI_CONTEXT_INV_SMEM_L1;
-       si_emit_cache_flush(sctx);
-
-       sctx->bindless_descriptors_dirty = false;
-}
-
-bool si_upload_graphics_shader_descriptors(struct si_context *sctx)
-{
-       const unsigned mask = u_bit_consecutive(0, SI_DESCS_FIRST_COMPUTE);
        unsigned dirty = sctx->descriptors_dirty & mask;
 
        /* Assume nothing will go wrong: */
@@ -2850,8 +2665,7 @@ bool si_upload_graphics_shader_descriptors(struct si_context *sctx)
        while (dirty) {
                unsigned i = u_bit_scan(&dirty);
 
-               if (!si_upload_descriptors(sctx, &sctx->descriptors[i],
-                                          &sctx->shader_userdata.atom))
+               if (!si_upload_descriptors(sctx, &sctx->descriptors[i]))
                        return false;
        }
 
@@ -2862,6 +2676,12 @@ bool si_upload_graphics_shader_descriptors(struct si_context *sctx)
        return true;
 }
 
+bool si_upload_graphics_shader_descriptors(struct si_context *sctx)
+{
+       const unsigned mask = u_bit_consecutive(0, SI_DESCS_FIRST_COMPUTE);
+       return si_upload_shader_descriptors(sctx, mask);
+}
+
 bool si_upload_compute_shader_descriptors(struct si_context *sctx)
 {
        /* Does not update rw_buffers as that is not needed for compute shaders
@@ -2869,23 +2689,7 @@ bool si_upload_compute_shader_descriptors(struct si_context *sctx)
         */
        const unsigned mask = u_bit_consecutive(SI_DESCS_FIRST_COMPUTE,
                                                SI_NUM_DESCS - SI_DESCS_FIRST_COMPUTE);
-       unsigned dirty = sctx->descriptors_dirty & mask;
-
-       /* Assume nothing will go wrong: */
-       sctx->shader_pointers_dirty |= dirty;
-
-       while (dirty) {
-               unsigned i = u_bit_scan(&dirty);
-
-               if (!si_upload_descriptors(sctx, &sctx->descriptors[i], NULL))
-                       return false;
-       }
-
-       sctx->descriptors_dirty &= ~mask;
-
-       si_upload_bindless_descriptors(sctx);
-
-       return true;
+       return si_upload_shader_descriptors(sctx, mask);
 }
 
 void si_release_all_descriptors(struct si_context *sctx)
@@ -2895,15 +2699,20 @@ void si_release_all_descriptors(struct si_context *sctx)
        for (i = 0; i < SI_NUM_SHADERS; i++) {
                si_release_buffer_resources(&sctx->const_and_shader_buffers[i],
                                            si_const_and_shader_buffer_descriptors(sctx, i));
-               si_release_sampler_views(&sctx->samplers[i].views);
+               si_release_sampler_views(&sctx->samplers[i]);
                si_release_image_views(&sctx->images[i]);
        }
        si_release_buffer_resources(&sctx->rw_buffers,
                                    &sctx->descriptors[SI_DESCS_RW_BUFFERS]);
+       for (i = 0; i < SI_NUM_VERTEX_BUFFERS; i++)
+               pipe_vertex_buffer_unreference(&sctx->vertex_buffer[i]);
 
        for (i = 0; i < SI_NUM_DESCS; ++i)
                si_release_descriptors(&sctx->descriptors[i]);
+
+       sctx->vertex_buffers.list = NULL; /* points into a mapped buffer */
        si_release_descriptors(&sctx->vertex_buffers);
+       si_release_bindless_descriptors(sctx);
 }
 
 void si_all_descriptors_begin_new_cs(struct si_context *sctx)
@@ -2912,7 +2721,7 @@ void si_all_descriptors_begin_new_cs(struct si_context *sctx)
 
        for (i = 0; i < SI_NUM_SHADERS; i++) {
                si_buffer_resources_begin_new_cs(sctx, &sctx->const_and_shader_buffers[i]);
-               si_sampler_views_begin_new_cs(sctx, &sctx->samplers[i].views);
+               si_sampler_views_begin_new_cs(sctx, &sctx->samplers[i]);
                si_image_views_begin_new_cs(sctx, &sctx->images[i]);
        }
        si_buffer_resources_begin_new_cs(sctx, &sctx->rw_buffers);
@@ -2920,8 +2729,9 @@ void si_all_descriptors_begin_new_cs(struct si_context *sctx)
 
        for (i = 0; i < SI_NUM_DESCS; ++i)
                si_descriptors_begin_new_cs(sctx, &sctx->descriptors[i]);
+       si_descriptors_begin_new_cs(sctx, &sctx->bindless_descriptors);
 
-       si_shader_userdata_begin_new_cs(sctx);
+       si_shader_pointers_begin_new_cs(sctx);
 }
 
 void si_set_active_descriptors(struct si_context *sctx, unsigned desc_idx,
@@ -2944,26 +2754,6 @@ void si_set_active_descriptors(struct si_context *sctx, unsigned desc_idx,
            first + count > desc->first_active_slot + desc->num_active_slots)
                sctx->descriptors_dirty |= 1u << desc_idx;
 
-       /* Enable or disable CE for this descriptor array. */
-       bool used_ce = desc->uses_ce;
-       desc->uses_ce = desc->first_ce_slot <= first &&
-                       desc->first_ce_slot + desc->num_ce_slots >= first + count;
-
-       if (desc->uses_ce != used_ce) {
-               /* Upload or dump descriptors if we're disabling or enabling CE,
-                * respectively. */
-               sctx->descriptors_dirty |= 1u << desc_idx;
-
-               /* If we're enabling CE, re-upload all descriptors to CE RAM.
-                * When CE was disabled, uploads to CE RAM stopped.
-                */
-               if (desc->uses_ce) {
-                       desc->dirty_mask |=
-                               u_bit_consecutive64(desc->first_ce_slot,
-                                                   desc->num_ce_slots);
-               }
-       }
-
        desc->first_active_slot = first;
        desc->num_active_slots = count;
 }