panfrost: Use pack for general varying
[mesa.git] / src / gallium / drivers / llvmpipe / lp_state_sampler.c
index 32bf9fdd25d3d38d8e662923694948347e65d0fe..f802af45f5a7f45ffc830cc2ceb91cba4038b048 100644 (file)
@@ -38,7 +38,8 @@
 #include "lp_screen.h"
 #include "lp_state.h"
 #include "lp_debug.h"
-#include "state_tracker/sw_winsys.h"
+#include "frontend/sw_winsys.h"
+#include "lp_flush.h"
 
 
 static void *
@@ -66,7 +67,7 @@ llvmpipe_create_sampler_state(struct pipe_context *pipe,
 
 static void
 llvmpipe_bind_sampler_states(struct pipe_context *pipe,
-                             unsigned shader,
+                             enum pipe_shader_type shader,
                              unsigned start,
                              unsigned num,
                              void **samplers)
@@ -75,7 +76,7 @@ llvmpipe_bind_sampler_states(struct pipe_context *pipe,
    unsigned i;
 
    assert(shader < PIPE_SHADER_TYPES);
-   assert(start + num <= Elements(llvmpipe->samplers[shader]));
+   assert(start + num <= ARRAY_SIZE(llvmpipe->samplers[shader]));
 
    draw_flush(llvmpipe->draw);
 
@@ -92,13 +93,18 @@ llvmpipe_bind_sampler_states(struct pipe_context *pipe,
       llvmpipe->num_samplers[shader] = j;
    }
 
-   if (shader == PIPE_SHADER_VERTEX || shader == PIPE_SHADER_GEOMETRY) {
+   if (shader == PIPE_SHADER_VERTEX ||
+       shader == PIPE_SHADER_GEOMETRY ||
+       shader == PIPE_SHADER_TESS_CTRL ||
+       shader == PIPE_SHADER_TESS_EVAL) {
       draw_set_samplers(llvmpipe->draw,
                         shader,
                         llvmpipe->samplers[shader],
                         llvmpipe->num_samplers[shader]);
    }
-   else {
+   else if (shader == PIPE_SHADER_COMPUTE) {
+      llvmpipe->cs_dirty |= LP_CSNEW_SAMPLER;
+   } else {
       llvmpipe->dirty |= LP_NEW_SAMPLER;
    }
 }
@@ -106,7 +112,7 @@ llvmpipe_bind_sampler_states(struct pipe_context *pipe,
 
 static void
 llvmpipe_set_sampler_views(struct pipe_context *pipe,
-                           unsigned shader,
+                           enum pipe_shader_type shader,
                            unsigned start,
                            unsigned num,
                            struct pipe_sampler_view **views)
@@ -117,18 +123,12 @@ llvmpipe_set_sampler_views(struct pipe_context *pipe,
    assert(num <= PIPE_MAX_SHADER_SAMPLER_VIEWS);
 
    assert(shader < PIPE_SHADER_TYPES);
-   assert(start + num <= Elements(llvmpipe->sampler_views[shader]));
+   assert(start + num <= ARRAY_SIZE(llvmpipe->sampler_views[shader]));
 
    draw_flush(llvmpipe->draw);
 
    /* set the new sampler views */
    for (i = 0; i < num; i++) {
-      /* Note: we're using pipe_sampler_view_release() here to work around
-       * a possible crash when the old view belongs to another context that
-       * was already destroyed.
-       */
-      pipe_sampler_view_release(pipe,
-                                &llvmpipe->sampler_views[shader][start + i]);
       /*
        * Warn if someone tries to set a view created in a different context
        * (which is why we need the hack above in the first place).
@@ -138,6 +138,9 @@ llvmpipe_set_sampler_views(struct pipe_context *pipe,
          debug_printf("Illegal setting of sampler_view %d created in another "
                       "context\n", i);
       }
+
+      if (views[i])
+         llvmpipe_flush_resource(pipe, views[i]->texture, 0, true, false, false, "sampler_view");
       pipe_sampler_view_reference(&llvmpipe->sampler_views[shader][start + i],
                                   views[i]);
    }
@@ -150,13 +153,18 @@ llvmpipe_set_sampler_views(struct pipe_context *pipe,
       llvmpipe->num_sampler_views[shader] = j;
    }
 
-   if (shader == PIPE_SHADER_VERTEX || shader == PIPE_SHADER_GEOMETRY) {
+   if (shader == PIPE_SHADER_VERTEX ||
+       shader == PIPE_SHADER_GEOMETRY ||
+       shader == PIPE_SHADER_TESS_CTRL ||
+       shader == PIPE_SHADER_TESS_EVAL) {
       draw_set_sampler_views(llvmpipe->draw,
                              shader,
                              llvmpipe->sampler_views[shader],
                              llvmpipe->num_sampler_views[shader]);
    }
-   else {
+   else if (shader == PIPE_SHADER_COMPUTE) {
+      llvmpipe->cs_dirty |= LP_CSNEW_SAMPLER_VIEW;
+   } else {
       llvmpipe->dirty |= LP_NEW_SAMPLER_VIEW;
    }
 }
@@ -169,11 +177,13 @@ llvmpipe_create_sampler_view(struct pipe_context *pipe,
 {
    struct pipe_sampler_view *view = CALLOC_STRUCT(pipe_sampler_view);
    /*
-    * XXX we REALLY want to see the correct bind flag here but the OpenGL
-    * state tracker can't guarantee that at least for texture buffer objects.
+    * XXX: bind flags from OpenGL state tracker are notoriously unreliable.
+    * This looks unfixable, so fix the bind flags instead when it happens.
     */
-   if (!(texture->bind & PIPE_BIND_SAMPLER_VIEW))
+   if (!(texture->bind & PIPE_BIND_SAMPLER_VIEW)) {
       debug_printf("Illegal sampler view creation without bind flag\n");
+      texture->bind |= PIPE_BIND_SAMPLER_VIEW;
+   }
 
    if (view) {
       *view = *templ;
@@ -185,7 +195,7 @@ llvmpipe_create_sampler_view(struct pipe_context *pipe,
 #ifdef DEBUG
      /*
       * This is possibly too lenient, but the primary reason is just
-      * to catch state trackers which forget to initialize this, so
+      * to catch gallium frontends which forget to initialize this, so
       * it only catches clearly impossible view targets.
       */
       if (view->target != texture->target) {
@@ -239,7 +249,7 @@ prepare_shader_sampling(
    struct llvmpipe_context *lp,
    unsigned num,
    struct pipe_sampler_view **views,
-   unsigned shader_type)
+   enum pipe_shader_type shader_type)
 {
 
    unsigned i;
@@ -262,6 +272,8 @@ prepare_shader_sampling(
          unsigned num_layers = tex->depth0;
          unsigned first_level = 0;
          unsigned last_level = 0;
+         unsigned sample_stride = 0;
+         unsigned num_samples = tex->nr_samples;
 
          if (!lp_tex->dt) {
             /* regular texture - setup array of mipmap level offsets */
@@ -275,6 +287,8 @@ prepare_shader_sampling(
                assert(last_level <= res->last_level);
                addr = lp_tex->tex_data;
 
+               sample_stride = lp_tex->sample_stride;
+
                for (j = first_level; j <= last_level; j++) {
                   mip_offsets[j] = lp_tex->mip_offsets[j];
                   row_stride[j] = lp_tex->row_stride[j];
@@ -306,11 +320,9 @@ prepare_shader_sampling(
                img_stride[0] = 0;
 
                /* everything specified in number of elements here. */
-               width0 = view->u.buf.last_element - view->u.buf.first_element + 1;
-               addr = (uint8_t *)addr + view->u.buf.first_element *
-                               view_blocksize;
-               assert(view->u.buf.first_element <= view->u.buf.last_element);
-               assert(view->u.buf.last_element * view_blocksize < res->width0);
+               width0 = view->u.buf.size / view_blocksize;
+               addr = (uint8_t *)addr + view->u.buf.offset;
+               assert(view->u.buf.offset + view->u.buf.size <= res->width0);
             }
          }
          else {
@@ -332,6 +344,7 @@ prepare_shader_sampling(
                                  i,
                                  width0, tex->height0, num_layers,
                                  first_level, last_level,
+                                 num_samples, sample_stride,
                                  addr,
                                  row_stride, img_stride, mip_offsets);
       }
@@ -362,6 +375,167 @@ llvmpipe_prepare_geometry_sampling(struct llvmpipe_context *lp,
    prepare_shader_sampling(lp, num, views, PIPE_SHADER_GEOMETRY);
 }
 
+/**
+ * Called whenever we're about to draw (no dirty flag, FIXME?).
+ */
+void
+llvmpipe_prepare_tess_ctrl_sampling(struct llvmpipe_context *lp,
+                                   unsigned num,
+                                   struct pipe_sampler_view **views)
+{
+   prepare_shader_sampling(lp, num, views, PIPE_SHADER_TESS_CTRL);
+}
+
+/**
+ * Called whenever we're about to draw (no dirty flag, FIXME?).
+ */
+void
+llvmpipe_prepare_tess_eval_sampling(struct llvmpipe_context *lp,
+                                   unsigned num,
+                                   struct pipe_sampler_view **views)
+{
+   prepare_shader_sampling(lp, num, views, PIPE_SHADER_TESS_EVAL);
+}
+
+static void
+prepare_shader_images(
+   struct llvmpipe_context *lp,
+   unsigned num,
+   struct pipe_image_view *views,
+   enum pipe_shader_type shader_type)
+{
+
+   unsigned i;
+   uint32_t row_stride;
+   uint32_t img_stride;
+   uint32_t sample_stride;
+   const void *addr;
+
+   assert(num <= PIPE_MAX_SHADER_SAMPLER_VIEWS);
+   if (!num)
+      return;
+
+   for (i = 0; i < num; i++) {
+      struct pipe_image_view *view = i < num ? &views[i] : NULL;
+
+      if (view) {
+         struct pipe_resource *img = view->resource;
+         struct llvmpipe_resource *lp_img = llvmpipe_resource(img);
+         if (!img)
+            continue;
+
+         unsigned width = u_minify(img->width0, view->u.tex.level);
+         unsigned height = u_minify(img->height0, view->u.tex.level);
+         unsigned num_layers = img->depth0;
+         unsigned num_samples = img->nr_samples;
+
+         if (!lp_img->dt) {
+            /* regular texture - setup array of mipmap level offsets */
+            struct pipe_resource *res = view->resource;
+
+            if (llvmpipe_resource_is_texture(res)) {
+               uint32_t mip_offset = lp_img->mip_offsets[view->u.tex.level];
+               addr = lp_img->tex_data;
+
+               if (img->target == PIPE_TEXTURE_1D_ARRAY ||
+                   img->target == PIPE_TEXTURE_2D_ARRAY ||
+                   img->target == PIPE_TEXTURE_3D ||
+                   img->target == PIPE_TEXTURE_CUBE ||
+                   img->target == PIPE_TEXTURE_CUBE_ARRAY) {
+                  num_layers = view->u.tex.last_layer - view->u.tex.first_layer + 1;
+                  assert(view->u.tex.first_layer <= view->u.tex.last_layer);
+                  mip_offset += view->u.tex.first_layer * lp_img->img_stride[view->u.tex.level];
+               }
+
+               row_stride = lp_img->row_stride[view->u.tex.level];
+               img_stride = lp_img->img_stride[view->u.tex.level];
+               sample_stride = lp_img->sample_stride;
+               addr = (uint8_t *)addr + mip_offset;
+            }
+            else {
+               unsigned view_blocksize = util_format_get_blocksize(view->format);
+               addr = lp_img->data;
+               /* probably don't really need to fill that out */
+               row_stride = 0;
+               img_stride = 0;
+               sample_stride = 0;
+
+               /* everything specified in number of elements here. */
+               width = view->u.buf.size / view_blocksize;
+               addr = (uint8_t *)addr + view->u.buf.offset;
+               assert(view->u.buf.offset + view->u.buf.size <= res->width0);
+            }
+         }
+         else {
+            /* display target texture/surface */
+            /*
+             * XXX: Where should this be unmapped?
+             */
+            struct llvmpipe_screen *screen = llvmpipe_screen(img->screen);
+            struct sw_winsys *winsys = screen->winsys;
+            addr = winsys->displaytarget_map(winsys, lp_img->dt,
+                                                PIPE_TRANSFER_READ);
+            row_stride = lp_img->row_stride[0];
+            img_stride = lp_img->img_stride[0];
+            sample_stride = 0;
+            assert(addr);
+         }
+         draw_set_mapped_image(lp->draw,
+                               shader_type,
+                               i,
+                               width, height, num_layers,
+                               addr,
+                               row_stride, img_stride,
+                               num_samples, sample_stride);
+      }
+   }
+}
+
+
+/**
+ * Called whenever we're about to draw (no dirty flag, FIXME?).
+ */
+void
+llvmpipe_prepare_vertex_images(struct llvmpipe_context *lp,
+                               unsigned num,
+                               struct pipe_image_view *views)
+{
+   prepare_shader_images(lp, num, views, PIPE_SHADER_VERTEX);
+}
+
+
+/**
+ * Called whenever we're about to draw (no dirty flag, FIXME?).
+ */
+void
+llvmpipe_prepare_geometry_images(struct llvmpipe_context *lp,
+                                 unsigned num,
+                                 struct pipe_image_view *views)
+{
+   prepare_shader_images(lp, num, views, PIPE_SHADER_GEOMETRY);
+}
+
+/**
+ * Called whenever we're about to draw (no dirty flag, FIXME?).
+ */
+void
+llvmpipe_prepare_tess_ctrl_images(struct llvmpipe_context *lp,
+                                  unsigned num,
+                                  struct pipe_image_view *views)
+{
+   prepare_shader_images(lp, num, views, PIPE_SHADER_TESS_CTRL);
+}
+
+/**
+ * Called whenever we're about to draw (no dirty flag, FIXME?).
+ */
+void
+llvmpipe_prepare_tess_eval_images(struct llvmpipe_context *lp,
+                                  unsigned num,
+                                  struct pipe_image_view *views)
+{
+   prepare_shader_images(lp, num, views, PIPE_SHADER_TESS_EVAL);
+}
 
 void
 llvmpipe_init_sampler_funcs(struct llvmpipe_context *llvmpipe)