radeonsi: Set PIPE_SHADER_CAP_MAX_SHADER_IMAGES
[mesa.git] / src / gallium / drivers / radeonsi / si_descriptors.c
index fcf4dbfd98978850355ba3aebc8fb53215fc973d..815b87bbd7e3b9be6076817882c2144153810466 100644 (file)
  *
  * Also, uploading descriptors to newly allocated memory doesn't require
  * a KCACHE flush.
+ *
+ *
+ * Possible scenarios for one 16 dword image+sampler slot:
+ *
+ *       | Image        | w/ FMASK   | Buffer       | NULL
+ * [ 0: 3] Image[0:3]   | Image[0:3] | Null[0:3]    | Null[0:3]
+ * [ 4: 7] Image[4:7]   | Image[4:7] | Buffer[0:3]  | 0
+ * [ 8:11] Null[0:3]    | Fmask[0:3] | Null[0:3]    | Null[0:3]
+ * [12:15] Sampler[0:3] | Fmask[4:7] | Sampler[0:3] | Sampler[0:3]
+ *
+ * FMASK implies MSAA, therefore no sampler state.
+ * Sampler states are never unbound except when FMASK is bound.
  */
 
 #include "radeon/r600_cs.h"
@@ -52,7 +64,8 @@
 #include "util/u_upload_mgr.h"
 
 
-/* NULL image and buffer descriptor.
+/* NULL image and buffer descriptor for textures (alpha = 1) and images
+ * (alpha = 0).
  *
  * For images, all fields must be zero except for the swizzle, which
  * supports arbitrary combinations of 0s and 1s. The texture type must be
@@ -62,7 +75,7 @@
  *
  * This is the only reason why the buffer descriptor must be in words [4:7].
  */
-static uint32_t null_descriptor[8] = {
+static uint32_t null_texture_descriptor[8] = {
        0,
        0,
        0,
@@ -72,10 +85,20 @@ static uint32_t null_descriptor[8] = {
         * descriptor */
 };
 
+static uint32_t null_image_descriptor[8] = {
+       0,
+       0,
+       0,
+       S_008F1C_TYPE(V_008F1C_SQ_RSRC_IMG_1D)
+       /* the rest must contain zeros, which is also used by the buffer
+        * descriptor */
+};
+
 static void si_init_descriptors(struct si_descriptors *desc,
                                unsigned shader_userdata_index,
                                unsigned element_dw_size,
-                               unsigned num_elements)
+                               unsigned num_elements,
+                               const uint32_t *null_descriptor)
 {
        int i;
 
@@ -88,10 +111,12 @@ static void si_init_descriptors(struct si_descriptors *desc,
        desc->shader_userdata_offset = shader_userdata_index * 4;
 
        /* Initialize the array to NULL descriptors if the element size is 8. */
-       if (element_dw_size == 8)
-               for (i = 0; i < num_elements; i++)
-                       memcpy(desc->list + i*element_dw_size, null_descriptor,
-                              sizeof(null_descriptor));
+       if (null_descriptor) {
+               assert(element_dw_size % 8 == 0);
+               for (i = 0; i < num_elements * element_dw_size / 8; i++)
+                       memcpy(desc->list + i * 8, null_descriptor,
+                              8 * 4);
+       }
 }
 
 static void si_release_descriptors(struct si_descriptors *desc)
@@ -109,7 +134,7 @@ static bool si_upload_descriptors(struct si_context *sctx,
        if (!desc->list_dirty)
                return true;
 
-       u_upload_alloc(sctx->b.uploader, 0, list_size,
+       u_upload_alloc(sctx->b.uploader, 0, list_size, 256,
                       &desc->buffer_offset,
                       (struct pipe_resource**)&desc->buffer, &ptr);
        if (!desc->buffer)
@@ -117,12 +142,12 @@ static bool si_upload_descriptors(struct si_context *sctx,
 
        util_memcpy_cpu_to_le32(ptr, desc->list, list_size);
 
-       r600_context_bo_reloc(&sctx->b, &sctx->b.rings.gfx, desc->buffer,
-                             RADEON_USAGE_READ, RADEON_PRIO_SHADER_DATA);
+       radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx, desc->buffer,
+                             RADEON_USAGE_READ, RADEON_PRIO_DESCRIPTORS);
 
        desc->list_dirty = false;
        desc->pointer_dirty = true;
-       sctx->shader_userdata.atom.dirty = true;
+       si_mark_atom_dirty(sctx, &sctx->shader_userdata.atom);
        return true;
 }
 
@@ -138,15 +163,17 @@ static void si_release_sampler_views(struct si_sampler_views *views)
        si_release_descriptors(&views->desc);
 }
 
-static enum radeon_bo_priority si_get_resource_ro_priority(struct r600_resource *res)
+static void si_sampler_view_add_buffer(struct si_context *sctx,
+                                      struct pipe_resource *resource)
 {
-       if (res->b.b.target == PIPE_BUFFER)
-               return RADEON_PRIO_SHADER_BUFFER_RO;
+       struct r600_resource *rres = (struct r600_resource*)resource;
 
-       if (res->b.b.nr_samples > 1)
-               return RADEON_PRIO_SHADER_TEXTURE_MSAA;
+       if (!resource)
+               return;
 
-       return RADEON_PRIO_SHADER_TEXTURE_RO;
+       radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx, rres,
+                                 RADEON_USAGE_READ,
+                                 r600_get_sampler_view_priority(rres));
 }
 
 static void si_sampler_views_begin_new_cs(struct si_context *sctx,
@@ -154,56 +181,73 @@ static void si_sampler_views_begin_new_cs(struct si_context *sctx,
 {
        uint64_t mask = views->desc.enabled_mask;
 
-       /* Add relocations to the CS. */
+       /* Add buffers to the CS. */
        while (mask) {
                int i = u_bit_scan64(&mask);
-               struct si_sampler_view *rview =
-                       (struct si_sampler_view*)views->views[i];
-
-               if (!rview->resource)
-                       continue;
 
-               r600_context_bo_reloc(&sctx->b, &sctx->b.rings.gfx,
-                                     rview->resource, RADEON_USAGE_READ,
-                                     si_get_resource_ro_priority(rview->resource));
+               si_sampler_view_add_buffer(sctx, views->views[i]->texture);
        }
 
        if (!views->desc.buffer)
                return;
-       r600_context_bo_reloc(&sctx->b, &sctx->b.rings.gfx, views->desc.buffer,
-                             RADEON_USAGE_READWRITE, RADEON_PRIO_SHADER_DATA);
+       radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx, views->desc.buffer,
+                             RADEON_USAGE_READWRITE, RADEON_PRIO_DESCRIPTORS);
 }
 
-static void si_set_sampler_view(struct si_context *sctx, unsigned shader,
-                               unsigned slot, struct pipe_sampler_view *view,
-                               unsigned *view_desc)
+static void si_set_sampler_view(struct si_context *sctx,
+                               struct si_sampler_views *views,
+                               unsigned slot, struct pipe_sampler_view *view)
 {
-       struct si_sampler_views *views = &sctx->samplers[shader].views;
-
-       if (views->views[slot] == view)
+       struct si_sampler_view *rview = (struct si_sampler_view*)view;
+
+       if (view && view->texture && view->texture->target != PIPE_BUFFER &&
+           G_008F28_COMPRESSION_EN(rview->state[6]) &&
+           ((struct r600_texture*)view->texture)->dcc_offset == 0) {
+               rview->state[6] &= C_008F28_COMPRESSION_EN &
+                                  C_008F28_ALPHA_IS_ON_MSB;
+       } else if (views->views[slot] == view)
                return;
 
        if (view) {
-               struct si_sampler_view *rview =
-                       (struct si_sampler_view*)view;
+               struct r600_texture *rtex = (struct r600_texture *)view->texture;
 
-               if (rview->resource)
-                       r600_context_bo_reloc(&sctx->b, &sctx->b.rings.gfx,
-                               rview->resource, RADEON_USAGE_READ,
-                               si_get_resource_ro_priority(rview->resource));
+               si_sampler_view_add_buffer(sctx, view->texture);
 
                pipe_sampler_view_reference(&views->views[slot], view);
-               memcpy(views->desc.list + slot*8, view_desc, 8*4);
+               memcpy(views->desc.list + slot * 16, rview->state, 8*4);
+
+               if (view->texture && view->texture->target != PIPE_BUFFER &&
+                   rtex->fmask.size) {
+                       memcpy(views->desc.list + slot*16 + 8,
+                              rview->fmask_state, 8*4);
+               } else {
+                       /* Disable FMASK and bind sampler state in [12:15]. */
+                       memcpy(views->desc.list + slot*16 + 8,
+                              null_texture_descriptor, 4*4);
+
+                       if (views->sampler_states[slot])
+                               memcpy(views->desc.list + slot*16 + 12,
+                                      views->sampler_states[slot], 4*4);
+               }
+
                views->desc.enabled_mask |= 1llu << slot;
        } else {
                pipe_sampler_view_reference(&views->views[slot], NULL);
-               memcpy(views->desc.list + slot*8, null_descriptor, 8*4);
+               memcpy(views->desc.list + slot*16, null_texture_descriptor, 8*4);
+               /* Only clear the lower dwords of FMASK. */
+               memcpy(views->desc.list + slot*16 + 8, null_texture_descriptor, 4*4);
                views->desc.enabled_mask &= ~(1llu << slot);
        }
 
        views->desc.list_dirty = true;
 }
 
+static bool is_compressed_colortex(struct r600_texture *rtex)
+{
+       return rtex->cmask.size || rtex->fmask.size ||
+              (rtex->dcc_offset && rtex->dirty_level_mask);
+}
+
 static void si_set_sampler_views(struct pipe_context *ctx,
                                 unsigned shader, unsigned start,
                                  unsigned count,
@@ -211,7 +255,6 @@ static void si_set_sampler_views(struct pipe_context *ctx,
 {
        struct si_context *sctx = (struct si_context *)ctx;
        struct si_textures_info *samplers = &sctx->samplers[shader];
-       struct si_sampler_view **rviews = (struct si_sampler_view **)views;
        int i;
 
        if (!count || shader >= SI_NUM_SHADERS)
@@ -223,13 +266,11 @@ static void si_set_sampler_views(struct pipe_context *ctx,
                if (!views || !views[i]) {
                        samplers->depth_texture_mask &= ~(1 << slot);
                        samplers->compressed_colortex_mask &= ~(1 << slot);
-                       si_set_sampler_view(sctx, shader, slot, NULL, NULL);
-                       si_set_sampler_view(sctx, shader, SI_FMASK_TEX_OFFSET + slot,
-                                           NULL, NULL);
+                       si_set_sampler_view(sctx, &samplers->views, slot, NULL);
                        continue;
                }
 
-               si_set_sampler_view(sctx, shader, slot, views[i], rviews[i]->state);
+               si_set_sampler_view(sctx, &samplers->views, slot, views[i]);
 
                if (views[i]->texture && views[i]->texture->target != PIPE_BUFFER) {
                        struct r600_texture *rtex =
@@ -240,61 +281,227 @@ static void si_set_sampler_views(struct pipe_context *ctx,
                        } else {
                                samplers->depth_texture_mask &= ~(1 << slot);
                        }
-                       if (rtex->cmask.size || rtex->fmask.size) {
+                       if (is_compressed_colortex(rtex)) {
                                samplers->compressed_colortex_mask |= 1 << slot;
                        } else {
                                samplers->compressed_colortex_mask &= ~(1 << slot);
                        }
-
-                       if (rtex->fmask.size) {
-                               si_set_sampler_view(sctx, shader, SI_FMASK_TEX_OFFSET + slot,
-                                                   views[i], rviews[i]->fmask_state);
-                       } else {
-                               si_set_sampler_view(sctx, shader, SI_FMASK_TEX_OFFSET + slot,
-                                                   NULL, NULL);
-                       }
                } else {
                        samplers->depth_texture_mask &= ~(1 << slot);
                        samplers->compressed_colortex_mask &= ~(1 << slot);
-                       si_set_sampler_view(sctx, shader, SI_FMASK_TEX_OFFSET + slot,
-                                           NULL, NULL);
                }
        }
 }
 
-/* SAMPLER STATES */
+static void
+si_samplers_update_compressed_colortex_mask(struct si_textures_info *samplers)
+{
+       uint64_t mask = samplers->views.desc.enabled_mask;
 
-static void si_sampler_states_begin_new_cs(struct si_context *sctx,
-                                          struct si_sampler_states *states)
+       while (mask) {
+               int i = u_bit_scan64(&mask);
+               struct pipe_resource *res = samplers->views.views[i]->texture;
+
+               if (res && res->target != PIPE_BUFFER) {
+                       struct r600_texture *rtex = (struct r600_texture *)res;
+
+                       if (is_compressed_colortex(rtex)) {
+                               samplers->compressed_colortex_mask |= 1 << i;
+                       } else {
+                               samplers->compressed_colortex_mask &= ~(1 << i);
+                       }
+               }
+       }
+}
+
+/* IMAGE VIEWS */
+
+static void
+si_release_image_views(struct si_images_info *images)
 {
-       if (!states->desc.buffer)
+       unsigned i;
+
+       for (i = 0; i < SI_NUM_IMAGES; ++i) {
+               struct pipe_image_view *view = &images->views[i];
+
+               pipe_resource_reference(&view->resource, NULL);
+       }
+
+       si_release_descriptors(&images->desc);
+}
+
+static void
+si_image_views_begin_new_cs(struct si_context *sctx, struct si_images_info *images)
+{
+       uint mask = images->desc.enabled_mask;
+
+       /* Add buffers to the CS. */
+       while (mask) {
+               int i = u_bit_scan(&mask);
+               struct pipe_image_view *view = &images->views[i];
+
+               assert(view->resource);
+
+               si_sampler_view_add_buffer(sctx, view->resource);
+       }
+
+       if (images->desc.buffer) {
+               radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx,
+                                         images->desc.buffer,
+                                         RADEON_USAGE_READ,
+                                         RADEON_PRIO_DESCRIPTORS);
+       }
+}
+
+static void
+si_disable_shader_image(struct si_images_info *images, unsigned slot)
+{
+       if (images->desc.enabled_mask & (1llu << slot)) {
+               pipe_resource_reference(&images->views[slot].resource, NULL);
+               images->compressed_colortex_mask &= ~(1 << slot);
+
+               memcpy(images->desc.list + slot*8, null_image_descriptor, 8*4);
+               images->desc.enabled_mask &= ~(1llu << slot);
+               images->desc.list_dirty = true;
+       }
+}
+
+static void
+si_set_shader_images(struct pipe_context *pipe, unsigned shader,
+                    unsigned start_slot, unsigned count,
+                    struct pipe_image_view *views)
+{
+       struct si_context *ctx = (struct si_context *)pipe;
+       struct si_screen *screen = ctx->screen;
+       struct si_images_info *images = &ctx->images[shader];
+       unsigned i, slot;
+
+       assert(shader < SI_NUM_SHADERS);
+
+       if (!count)
                return;
-       r600_context_bo_reloc(&sctx->b, &sctx->b.rings.gfx, states->desc.buffer,
-                             RADEON_USAGE_READWRITE, RADEON_PRIO_SHADER_DATA);
+
+       assert(start_slot + count <= SI_NUM_IMAGES);
+
+       for (i = 0, slot = start_slot; i < count; ++i, ++slot) {
+               struct r600_resource *res;
+
+               if (!views || !views[i].resource) {
+                       si_disable_shader_image(images, slot);
+                       continue;
+               }
+
+               res = (struct r600_resource *)views[i].resource;
+               util_copy_image_view(&images->views[slot], &views[i]);
+
+               si_sampler_view_add_buffer(ctx, &res->b.b);
+
+               if (res->b.b.target == PIPE_BUFFER) {
+                       si_make_buffer_descriptor(screen, res,
+                                                 views[i].format,
+                                                 views[i].u.buf.first_element,
+                                                 views[i].u.buf.last_element,
+                                                 images->desc.list + slot * 8);
+                       images->compressed_colortex_mask &= ~(1 << slot);
+               } else {
+                       static const unsigned char swizzle[4] = { 0, 1, 2, 3 };
+                       struct r600_texture *tex = (struct r600_texture *)res;
+                       unsigned level;
+                       unsigned width, height, depth;
+
+                       assert(!tex->is_depth);
+                       assert(tex->fmask.size == 0);
+
+                       if (tex->dcc_offset &&
+                           views[i].access & PIPE_IMAGE_ACCESS_WRITE)
+                               r600_texture_disable_dcc(&screen->b, tex);
+
+                       if (is_compressed_colortex(tex)) {
+                               images->compressed_colortex_mask |= 1 << slot;
+                       } else {
+                               images->compressed_colortex_mask &= ~(1 << slot);
+                       }
+
+                       /* Always force the base level to the selected level.
+                        *
+                        * This is required for 3D textures, where otherwise
+                        * selecting a single slice for non-layered bindings
+                        * fails. It doesn't hurt the other targets.
+                        */
+                       level = views[i].u.tex.level;
+                       width = u_minify(res->b.b.width0, level);
+                       height = u_minify(res->b.b.height0, level);
+                       depth = u_minify(res->b.b.depth0, level);
+
+                       si_make_texture_descriptor(screen, tex, false, res->b.b.target,
+                                                  views[i].format, swizzle,
+                                                  level, 0, 0,
+                                                  views[i].u.tex.first_layer, views[i].u.tex.last_layer,
+                                                  width, height, depth,
+                                                  images->desc.list + slot * 8,
+                                                  NULL);
+               }
+
+               images->desc.enabled_mask |= 1llu << slot;
+               images->desc.list_dirty = true;
+       }
+}
+
+static void
+si_images_update_compressed_colortex_mask(struct si_images_info *images)
+{
+       uint64_t mask = images->desc.enabled_mask;
+
+       while (mask) {
+               int i = u_bit_scan64(&mask);
+               struct pipe_resource *res = images->views[i].resource;
+
+               if (res && res->target != PIPE_BUFFER) {
+                       struct r600_texture *rtex = (struct r600_texture *)res;
+
+                       if (is_compressed_colortex(rtex)) {
+                               images->compressed_colortex_mask |= 1 << i;
+                       } else {
+                               images->compressed_colortex_mask &= ~(1 << i);
+                       }
+               }
+       }
 }
 
-void si_set_sampler_descriptors(struct si_context *sctx, unsigned shader,
-                               unsigned start, unsigned count, void **states)
+/* SAMPLER STATES */
+
+static void si_bind_sampler_states(struct pipe_context *ctx, unsigned shader,
+                                   unsigned start, unsigned count, void **states)
 {
-       struct si_sampler_states *samplers = &sctx->samplers[shader].states;
+       struct si_context *sctx = (struct si_context *)ctx;
+       struct si_textures_info *samplers = &sctx->samplers[shader];
+       struct si_descriptors *desc = &samplers->views.desc;
        struct si_sampler_state **sstates = (struct si_sampler_state**)states;
        int i;
 
-       if (start == 0)
-               samplers->saved_states[0] = states[0];
-       if (start == 1)
-               samplers->saved_states[1] = states[0];
-       else if (start == 0 && count >= 2)
-               samplers->saved_states[1] = states[1];
+       if (!count || shader >= SI_NUM_SHADERS)
+               return;
 
        for (i = 0; i < count; i++) {
                unsigned slot = start + i;
 
-               if (!sstates[i])
+               if (!sstates[i] ||
+                   sstates[i] == samplers->views.sampler_states[slot])
                        continue;
 
-               memcpy(samplers->desc.list + slot*4, sstates[i]->val, 4*4);
-               samplers->desc.list_dirty = true;
+               samplers->views.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[i] &&
+                   samplers->views.views[i]->texture &&
+                   samplers->views.views[i]->texture->target != PIPE_BUFFER &&
+                   ((struct r600_texture*)samplers->views.views[i]->texture)->fmask.size)
+                       continue;
+
+               memcpy(desc->list + slot * 16 + 12, sstates[i]->val, 4*4);
+               desc->list_dirty = true;
        }
 }
 
@@ -311,7 +518,7 @@ static void si_init_buffer_resources(struct si_buffer_resources *buffers,
        buffers->buffers = CALLOC(num_buffers, sizeof(struct pipe_resource*));
 
        si_init_descriptors(&buffers->desc, shader_userdata_index, 4,
-                           num_buffers);
+                           num_buffers, NULL);
 }
 
 static void si_release_buffer_resources(struct si_buffer_resources *buffers)
@@ -331,20 +538,20 @@ static void si_buffer_resources_begin_new_cs(struct si_context *sctx,
 {
        uint64_t mask = buffers->desc.enabled_mask;
 
-       /* Add relocations to the CS. */
+       /* Add buffers to the CS. */
        while (mask) {
                int i = u_bit_scan64(&mask);
 
-               r600_context_bo_reloc(&sctx->b, &sctx->b.rings.gfx,
+               radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx,
                                      (struct r600_resource*)buffers->buffers[i],
                                      buffers->shader_usage, buffers->priority);
        }
 
        if (!buffers->desc.buffer)
                return;
-       r600_context_bo_reloc(&sctx->b, &sctx->b.rings.gfx,
+       radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx,
                              buffers->desc.buffer, RADEON_USAGE_READWRITE,
-                             RADEON_PRIO_SHADER_DATA);
+                             RADEON_PRIO_DESCRIPTORS);
 }
 
 /* VERTEX BUFFERS */
@@ -363,16 +570,16 @@ static void si_vertex_buffers_begin_new_cs(struct si_context *sctx)
                if (!sctx->vertex_buffer[vb].buffer)
                        continue;
 
-               r600_context_bo_reloc(&sctx->b, &sctx->b.rings.gfx,
+               radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx,
                                      (struct r600_resource*)sctx->vertex_buffer[vb].buffer,
-                                     RADEON_USAGE_READ, RADEON_PRIO_SHADER_BUFFER_RO);
+                                     RADEON_USAGE_READ, RADEON_PRIO_VERTEX_BUFFER);
        }
 
        if (!desc->buffer)
                return;
-       r600_context_bo_reloc(&sctx->b, &sctx->b.rings.gfx,
+       radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx,
                              desc->buffer, RADEON_USAGE_READ,
-                             RADEON_PRIO_SHADER_DATA);
+                             RADEON_PRIO_DESCRIPTORS);
 }
 
 static bool si_upload_vertex_buffer_descriptors(struct si_context *sctx)
@@ -392,14 +599,14 @@ static 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.
         */
-       u_upload_alloc(sctx->b.uploader, 0, count * 16, &desc->buffer_offset,
+       u_upload_alloc(sctx->b.uploader, 0, count * 16, 256, &desc->buffer_offset,
                       (struct pipe_resource**)&desc->buffer, (void**)&ptr);
        if (!desc->buffer)
                return false;
 
-       r600_context_bo_reloc(&sctx->b, &sctx->b.rings.gfx,
+       radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx,
                              desc->buffer, RADEON_USAGE_READ,
-                             RADEON_PRIO_SHADER_DATA);
+                             RADEON_PRIO_DESCRIPTORS);
 
        assert(count <= SI_NUM_VERTEX_BUFFERS);
 
@@ -417,7 +624,7 @@ static bool si_upload_vertex_buffer_descriptors(struct si_context *sctx)
 
                vb = &sctx->vertex_buffer[ve->vertex_buffer_index];
                rbuffer = (struct r600_resource*)vb->buffer;
-               if (rbuffer == NULL) {
+               if (!rbuffer) {
                        memset(desc, 0, 16);
                        continue;
                }
@@ -426,10 +633,11 @@ static bool si_upload_vertex_buffer_descriptors(struct si_context *sctx)
                va = rbuffer->gpu_address + offset;
 
                /* Fill in T# buffer resource description */
-               desc[0] = va & 0xFFFFFFFF;
+               desc[0] = va;
                desc[1] = S_008F04_BASE_ADDRESS_HI(va >> 32) |
                          S_008F04_STRIDE(vb->stride);
-               if (vb->stride)
+
+               if (sctx->b.chip_class <= CIK && vb->stride)
                        /* Round up by rounding down and adding 1 */
                        desc[2] = (vb->buffer->width0 - offset -
                                   sctx->vertex_elements->format_size[i]) /
@@ -440,9 +648,9 @@ static bool si_upload_vertex_buffer_descriptors(struct si_context *sctx)
                desc[3] = sctx->vertex_elements->rsrc_word3[i];
 
                if (!bound[ve->vertex_buffer_index]) {
-                       r600_context_bo_reloc(&sctx->b, &sctx->b.rings.gfx,
+                       radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx,
                                              (struct r600_resource*)vb->buffer,
-                                             RADEON_USAGE_READ, RADEON_PRIO_SHADER_BUFFER_RO);
+                                             RADEON_USAGE_READ, RADEON_PRIO_VERTEX_BUFFER);
                        bound[ve->vertex_buffer_index] = true;
                }
        }
@@ -452,7 +660,7 @@ static bool si_upload_vertex_buffer_descriptors(struct si_context *sctx)
         * uploaded to a fresh new buffer, so I don't think flushing the const
         * cache is needed. */
        desc->pointer_dirty = true;
-       sctx->shader_userdata.atom.dirty = true;
+       si_mark_atom_dirty(sctx, &sctx->shader_userdata.atom);
        sctx->vertex_buffers_dirty = false;
        return true;
 }
@@ -465,9 +673,10 @@ void si_upload_const_buffer(struct si_context *sctx, struct r600_resource **rbuf
 {
        void *tmp;
 
-       u_upload_alloc(sctx->b.uploader, 0, size, const_offset,
+       u_upload_alloc(sctx->b.uploader, 0, size, 256, const_offset,
                       (struct pipe_resource**)rbuffer, &tmp);
-       util_memcpy_cpu_to_le32(tmp, ptr, size);
+       if (rbuffer)
+               util_memcpy_cpu_to_le32(tmp, ptr, size);
 }
 
 static void si_set_constant_buffer(struct pipe_context *ctx, uint shader, uint slot,
@@ -499,6 +708,11 @@ static void si_set_constant_buffer(struct pipe_context *ctx, uint shader, uint s
                        si_upload_const_buffer(sctx,
                                               (struct r600_resource**)&buffer, input->user_buffer,
                                               input->buffer_size, &buffer_offset);
+                       if (!buffer) {
+                               /* Just unbind on failure. */
+                               si_set_constant_buffer(ctx, shader, slot, NULL);
+                               return;
+                       }
                        va = r600_resource(buffer)->gpu_address + buffer_offset;
                } else {
                        pipe_resource_reference(&buffer, input->buffer);
@@ -519,7 +733,7 @@ static void si_set_constant_buffer(struct pipe_context *ctx, uint shader, uint s
                          S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
 
                buffers->buffers[slot] = buffer;
-               r600_context_bo_reloc(&sctx->b, &sctx->b.rings.gfx,
+               radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx,
                                      (struct r600_resource*)buffer,
                                      buffers->shader_usage, buffers->priority);
                buffers->desc.enabled_mask |= 1llu << slot;
@@ -593,6 +807,9 @@ void si_set_ring_buffer(struct pipe_context *ctx, uint shader, uint slot,
                        break;
                }
 
+               if (sctx->b.chip_class >= VI && stride)
+                       num_records *= stride;
+
                /* Set the descriptor. */
                uint32_t *desc = buffers->desc.list + slot*4;
                desc[0] = va;
@@ -611,7 +828,7 @@ void si_set_ring_buffer(struct pipe_context *ctx, uint shader, uint slot,
                          S_008F0C_ADD_TID_ENABLE(add_tid);
 
                pipe_resource_reference(&buffers->buffers[slot], buffer);
-               r600_context_bo_reloc(&sctx->b, &sctx->b.rings.gfx,
+               radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx,
                                      (struct r600_resource*)buffer,
                                      buffers->shader_usage, buffers->priority);
                buffers->desc.enabled_mask |= 1llu << slot;
@@ -661,8 +878,8 @@ static void si_set_streamout_targets(struct pipe_context *ctx,
                 * VS_PARTIAL_FLUSH is required if the buffers are going to be
                 * used as an input immediately.
                 */
-               sctx->b.flags |= SI_CONTEXT_INV_KCACHE |
-                                SI_CONTEXT_INV_TC_L1 |
+               sctx->b.flags |= SI_CONTEXT_INV_SMEM_L1 |
+                                SI_CONTEXT_INV_VMEM_L1 |
                                 SI_CONTEXT_VS_PARTIAL_FLUSH;
        }
 
@@ -682,7 +899,12 @@ static void si_set_streamout_targets(struct pipe_context *ctx,
                        struct pipe_resource *buffer = targets[i]->buffer;
                        uint64_t va = r600_resource(buffer)->gpu_address;
 
-                       /* Set the descriptor. */
+                       /* 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 = buffers->desc.list + bufidx*4;
                        desc[0] = va;
                        desc[1] = S_008F04_BASE_ADDRESS_HI(va >> 32);
@@ -690,12 +912,13 @@ static void si_set_streamout_targets(struct pipe_context *ctx,
                        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_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);
-                       r600_context_bo_reloc(&sctx->b, &sctx->b.rings.gfx,
+                       radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx,
                                              (struct r600_resource*)buffer,
                                              buffers->shader_usage, buffers->priority);
                        buffers->desc.enabled_mask |= 1llu << bufidx;
@@ -738,6 +961,20 @@ static void si_desc_reset_buffer_offset(struct pipe_context *ctx,
                  S_008F04_BASE_ADDRESS_HI(va >> 32);
 }
 
+/* TEXTURE METADATA ENABLE/DISABLE */
+
+/* CMASK can be enabled (for fast clear) and disabled (for texture export)
+ * while the texture is bound, possibly by a different context. In that case,
+ * call this function to update compressed_colortex_masks.
+ */
+void si_update_compressed_colortex_masks(struct si_context *sctx)
+{
+       for (int i = 0; i < SI_NUM_SHADERS; ++i) {
+               si_samplers_update_compressed_colortex_mask(&sctx->samplers[i]);
+               si_images_update_compressed_colortex_mask(&sctx->images[i]);
+       }
+}
+
 /* BUFFER DISCARD/INVALIDATION */
 
 /* Reallocate a buffer a update all resource bindings where the buffer is
@@ -794,7 +1031,7 @@ static void si_invalidate_buffer(struct pipe_context *ctx, struct pipe_resource
                                                            old_va, buf);
                                buffers->desc.list_dirty = true;
 
-                               r600_context_bo_reloc(&sctx->b, &sctx->b.rings.gfx,
+                               radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx,
                                                      rbuffer, buffers->shader_usage,
                                                      buffers->priority);
 
@@ -823,7 +1060,7 @@ static void si_invalidate_buffer(struct pipe_context *ctx, struct pipe_resource
                                                            old_va, buf);
                                buffers->desc.list_dirty = true;
 
-                               r600_context_bo_reloc(&sctx->b, &sctx->b.rings.gfx,
+                               radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx,
                                                      rbuffer, buffers->shader_usage,
                                                      buffers->priority);
                        }
@@ -844,13 +1081,37 @@ static void si_invalidate_buffer(struct pipe_context *ctx, struct pipe_resource
                while (mask) {
                        unsigned i = u_bit_scan64(&mask);
                        if (views->views[i]->texture == buf) {
-                               si_desc_reset_buffer_offset(ctx, views->desc.list + i*8+4,
+                               si_desc_reset_buffer_offset(ctx,
+                                                           views->desc.list +
+                                                           i * 16 + 4,
                                                            old_va, buf);
                                views->desc.list_dirty = true;
 
-                               r600_context_bo_reloc(&sctx->b, &sctx->b.rings.gfx,
+                               radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx,
                                                      rbuffer, RADEON_USAGE_READ,
-                                                     RADEON_PRIO_SHADER_BUFFER_RO);
+                                                     RADEON_PRIO_SAMPLER_BUFFER);
+                       }
+               }
+       }
+
+       /* Shader images */
+       for (shader = 0; shader < SI_NUM_SHADERS; ++shader) {
+               struct si_images_info *images = &sctx->images[shader];
+               unsigned mask = images->desc.enabled_mask;
+
+               while (mask) {
+                       unsigned i = u_bit_scan(&mask);
+
+                       if (images->views[i].resource == buf) {
+                               si_desc_reset_buffer_offset(
+                                       ctx, images->desc.list + i * 8 + 4,
+                                       old_va, buf);
+                               images->desc.list_dirty = true;
+
+                               radeon_add_to_buffer_list(
+                                       &sctx->b, &sctx->b.gfx, rbuffer,
+                                       RADEON_USAGE_READWRITE,
+                                       RADEON_PRIO_SAMPLER_BUFFER);
                        }
                }
        }
@@ -864,12 +1125,11 @@ static void si_mark_shader_pointers_dirty(struct si_context *sctx,
        sctx->const_buffers[shader].desc.pointer_dirty = true;
        sctx->rw_buffers[shader].desc.pointer_dirty = true;
        sctx->samplers[shader].views.desc.pointer_dirty = true;
-       sctx->samplers[shader].states.desc.pointer_dirty = true;
 
        if (shader == PIPE_SHADER_VERTEX)
                sctx->vertex_buffers.pointer_dirty = true;
 
-       sctx->shader_userdata.atom.dirty = true;
+       si_mark_atom_dirty(sctx, &sctx->shader_userdata.atom);
 }
 
 static void si_shader_userdata_begin_new_cs(struct si_context *sctx)
@@ -906,10 +1166,10 @@ static void si_set_user_data_base(struct si_context *sctx,
 void si_shader_change_notify(struct si_context *sctx)
 {
        /* VS can be bound as VS, ES, or LS. */
-       if (sctx->tes_shader)
+       if (sctx->tes_shader.cso)
                si_set_user_data_base(sctx, PIPE_SHADER_VERTEX,
                                      R_00B530_SPI_SHADER_USER_DATA_LS_0);
-       else if (sctx->gs_shader)
+       else if (sctx->gs_shader.cso)
                si_set_user_data_base(sctx, PIPE_SHADER_VERTEX,
                                      R_00B330_SPI_SHADER_USER_DATA_ES_0);
        else
@@ -917,8 +1177,8 @@ void si_shader_change_notify(struct si_context *sctx)
                                      R_00B130_SPI_SHADER_USER_DATA_VS_0);
 
        /* TES can be bound as ES, VS, or not bound. */
-       if (sctx->tes_shader) {
-               if (sctx->gs_shader)
+       if (sctx->tes_shader.cso) {
+               if (sctx->gs_shader.cso)
                        si_set_user_data_base(sctx, PIPE_SHADER_TESS_EVAL,
                                              R_00B330_SPI_SHADER_USER_DATA_ES_0);
                else
@@ -933,7 +1193,7 @@ static void si_emit_shader_pointer(struct si_context *sctx,
                                   struct si_descriptors *desc,
                                   unsigned sh_base, bool keep_dirty)
 {
-       struct radeon_winsys_cs *cs = sctx->b.rings.gfx.cs;
+       struct radeon_winsys_cs *cs = sctx->b.gfx.cs;
        uint64_t va;
 
        if (!desc->pointer_dirty || !desc->buffer)
@@ -950,13 +1210,12 @@ static void si_emit_shader_pointer(struct si_context *sctx,
        desc->pointer_dirty = keep_dirty;
 }
 
-static void si_emit_shader_userdata(struct si_context *sctx,
-                                   struct r600_atom *atom)
+void si_emit_shader_userdata(struct si_context *sctx, struct r600_atom *atom)
 {
        unsigned i;
        uint32_t *sh_base = sctx->shader_userdata.sh_base;
 
-       if (sctx->gs_shader) {
+       if (sctx->gs_shader.cso) {
                /* The VS copy shader needs these for clipping, streamout, and rings. */
                unsigned vs_base = R_00B130_SPI_SHADER_USER_DATA_VS_0;
                unsigned i = PIPE_SHADER_VERTEX;
@@ -964,10 +1223,12 @@ static void si_emit_shader_userdata(struct si_context *sctx,
                si_emit_shader_pointer(sctx, &sctx->const_buffers[i].desc, vs_base, true);
                si_emit_shader_pointer(sctx, &sctx->rw_buffers[i].desc, vs_base, true);
 
-               /* The TESSEVAL shader needs this for the ESGS ring buffer. */
-               si_emit_shader_pointer(sctx, &sctx->rw_buffers[i].desc,
-                                      R_00B330_SPI_SHADER_USER_DATA_ES_0, true);
-       } else if (sctx->tes_shader) {
+               if (sctx->tes_shader.cso) {
+                       /* The TESSEVAL shader needs this for the ESGS ring buffer. */
+                       si_emit_shader_pointer(sctx, &sctx->rw_buffers[i].desc,
+                                              R_00B330_SPI_SHADER_USER_DATA_ES_0, true);
+               }
+       } else if (sctx->tes_shader.cso) {
                /* The TESSEVAL shader needs this for streamout. */
                si_emit_shader_pointer(sctx, &sctx->rw_buffers[PIPE_SHADER_VERTEX].desc,
                                       R_00B130_SPI_SHADER_USER_DATA_VS_0, true);
@@ -984,7 +1245,7 @@ static void si_emit_shader_userdata(struct si_context *sctx,
 
                si_emit_shader_pointer(sctx, &sctx->const_buffers[i].desc, base, false);
                si_emit_shader_pointer(sctx, &sctx->samplers[i].views.desc, base, false);
-               si_emit_shader_pointer(sctx, &sctx->samplers[i].states.desc, base, false);
+               si_emit_shader_pointer(sctx, &sctx->images[i].desc, base, false);
        }
        si_emit_shader_pointer(sctx, &sctx->vertex_buffers, sh_base[PIPE_SHADER_VERTEX], false);
 }
@@ -997,33 +1258,35 @@ void si_init_all_descriptors(struct si_context *sctx)
 
        for (i = 0; i < SI_NUM_SHADERS; i++) {
                si_init_buffer_resources(&sctx->const_buffers[i],
-                                        SI_NUM_CONST_BUFFERS, SI_SGPR_CONST,
-                                        RADEON_USAGE_READ, RADEON_PRIO_SHADER_BUFFER_RO);
+                                        SI_NUM_CONST_BUFFERS, SI_SGPR_CONST_BUFFERS,
+                                        RADEON_USAGE_READ, RADEON_PRIO_CONST_BUFFER);
                si_init_buffer_resources(&sctx->rw_buffers[i],
                                         SI_NUM_RW_BUFFERS, SI_SGPR_RW_BUFFERS,
-                                        RADEON_USAGE_READWRITE, RADEON_PRIO_SHADER_RESOURCE_RW);
+                                        RADEON_USAGE_READWRITE, RADEON_PRIO_RINGS_STREAMOUT);
 
                si_init_descriptors(&sctx->samplers[i].views.desc,
-                                   SI_SGPR_RESOURCE, 8, SI_NUM_SAMPLER_VIEWS);
-               si_init_descriptors(&sctx->samplers[i].states.desc,
-                                   SI_SGPR_SAMPLER, 4, SI_NUM_SAMPLER_STATES);
+                                   SI_SGPR_SAMPLERS, 16, SI_NUM_SAMPLERS,
+                                   null_texture_descriptor);
+
+               si_init_descriptors(&sctx->images[i].desc,
+                                   SI_SGPR_IMAGES, 8, SI_NUM_IMAGES,
+                                   null_image_descriptor);
        }
 
-       si_init_descriptors(&sctx->vertex_buffers, SI_SGPR_VERTEX_BUFFER,
-                           4, SI_NUM_VERTEX_BUFFERS);
+       si_init_descriptors(&sctx->vertex_buffers, SI_SGPR_VERTEX_BUFFERS,
+                           4, SI_NUM_VERTEX_BUFFERS, NULL);
 
        /* Set pipe_context functions. */
+       sctx->b.b.bind_sampler_states = si_bind_sampler_states;
+       sctx->b.b.set_shader_images = si_set_shader_images;
        sctx->b.b.set_constant_buffer = si_set_constant_buffer;
        sctx->b.b.set_sampler_views = si_set_sampler_views;
        sctx->b.b.set_stream_output_targets = si_set_streamout_targets;
        sctx->b.invalidate_buffer = si_invalidate_buffer;
 
        /* Shader user data. */
-       sctx->atoms.s.shader_userdata = &sctx->shader_userdata.atom;
-       sctx->shader_userdata.atom.emit = (void*)si_emit_shader_userdata;
-
-       /* Upper bound, 4 pointers per shader, +1 for vertex buffers, +2 for the VS copy shader. */
-       sctx->shader_userdata.atom.num_dw = (SI_NUM_SHADERS * 4 + 1 + 2) * 4;
+       si_init_atom(sctx, &sctx->shader_userdata.atom, &sctx->atoms.s.shader_userdata,
+                    si_emit_shader_userdata);
 
        /* Set default and immutable mappings. */
        si_set_user_data_base(sctx, PIPE_SHADER_VERTEX, R_00B130_SPI_SHADER_USER_DATA_VS_0);
@@ -1040,7 +1303,7 @@ bool si_upload_shader_descriptors(struct si_context *sctx)
                if (!si_upload_descriptors(sctx, &sctx->const_buffers[i].desc) ||
                    !si_upload_descriptors(sctx, &sctx->rw_buffers[i].desc) ||
                    !si_upload_descriptors(sctx, &sctx->samplers[i].views.desc) ||
-                   !si_upload_descriptors(sctx, &sctx->samplers[i].states.desc))
+                   !si_upload_descriptors(sctx, &sctx->images[i].desc))
                        return false;
        }
        return si_upload_vertex_buffer_descriptors(sctx);
@@ -1054,7 +1317,7 @@ void si_release_all_descriptors(struct si_context *sctx)
                si_release_buffer_resources(&sctx->const_buffers[i]);
                si_release_buffer_resources(&sctx->rw_buffers[i]);
                si_release_sampler_views(&sctx->samplers[i].views);
-               si_release_descriptors(&sctx->samplers[i].states.desc);
+               si_release_image_views(&sctx->images[i]);
        }
        si_release_descriptors(&sctx->vertex_buffers);
 }
@@ -1067,7 +1330,7 @@ void si_all_descriptors_begin_new_cs(struct si_context *sctx)
                si_buffer_resources_begin_new_cs(sctx, &sctx->const_buffers[i]);
                si_buffer_resources_begin_new_cs(sctx, &sctx->rw_buffers[i]);
                si_sampler_views_begin_new_cs(sctx, &sctx->samplers[i].views);
-               si_sampler_states_begin_new_cs(sctx, &sctx->samplers[i].states);
+               si_image_views_begin_new_cs(sctx, &sctx->images[i]);
        }
        si_vertex_buffers_begin_new_cs(sctx);
        si_shader_userdata_begin_new_cs(sctx);