gallium: split transfer_inline_write into buffer and texture callbacks
[mesa.git] / src / gallium / drivers / svga / svga_resource_texture.c
index a02d1e495ff72c807deb47c243fe9d76d6939bce..230221a386784f747d51e67ae40409028b60ddb7 100644 (file)
@@ -51,7 +51,9 @@ static void
 svga_transfer_dma_band(struct svga_context *svga,
                        struct svga_transfer *st,
                        SVGA3dTransferType transfer,
-                       unsigned y, unsigned h, unsigned srcy,
+                       unsigned x, unsigned y, unsigned z,
+                       unsigned w, unsigned h, unsigned d,
+                       unsigned srcx, unsigned srcy, unsigned srcz,
                        SVGA3dSurfaceDMAFlags flags)
 {
    struct svga_texture *texture = svga_texture(st->base.resource);
@@ -60,27 +62,27 @@ svga_transfer_dma_band(struct svga_context *svga,
 
    assert(!st->use_direct_map);
 
-   box.x = st->base.box.x;
+   box.x = x;
    box.y = y;
-   box.z = st->base.box.z;
-   box.w = st->base.box.width;
+   box.z = z;
+   box.w = w;
    box.h = h;
-   box.d = 1;
-   box.srcx = 0;
+   box.d = d;
+   box.srcx = srcx;
    box.srcy = srcy;
-   box.srcz = 0;
+   box.srcz = srcz;
 
    SVGA_DBG(DEBUG_DMA, "dma %s sid %p, face %u, (%u, %u, %u) - "
             "(%u, %u, %u), %ubpp\n",
             transfer == SVGA3D_WRITE_HOST_VRAM ? "to" : "from",
             texture->handle,
             st->slice,
-            st->base.box.x,
+            x,
             y,
-            box.z,
-            st->base.box.x + st->base.box.width,
+            z,
+            x + w,
             y + h,
-            box.z + 1,
+            z + 1,
             util_format_get_blocksize(texture->b.b.format) * 8 /
             (util_format_get_blockwidth(texture->b.b.format)
              * util_format_get_blockheight(texture->b.b.format)));
@@ -119,7 +121,9 @@ svga_transfer_dma(struct svga_context *svga,
    if (!st->swbuf) {
       /* Do the DMA transfer in a single go */
       svga_transfer_dma_band(svga, st, transfer,
-                             st->base.box.y, st->base.box.height, 0,
+                             st->base.box.x, st->base.box.y, st->base.box.z,
+                             st->base.box.width, st->base.box.height, st->base.box.depth,
+                             0, 0, 0,
                              flags);
 
       if (transfer == SVGA3D_READ_HOST_VRAM) {
@@ -170,7 +174,10 @@ svga_transfer_dma(struct svga_context *svga,
             }
          }
 
-         svga_transfer_dma_band(svga, st, transfer, y, h, srcy, flags);
+         svga_transfer_dma_band(svga, st, transfer,
+                                st->base.box.x, y, st->base.box.z,
+                                st->base.box.width, h, st->base.box.depth,
+                                0, srcy, 0, flags);
 
          /*
           * Prevent the texture contents to be discarded on the next band
@@ -234,6 +241,7 @@ svga_texture_destroy(struct pipe_screen *screen,
 
    FREE(tex->defined);
    FREE(tex->rendered_to);
+   FREE(tex->dirty);
    FREE(tex);
 
    assert(ss->hud.num_resources > 0);
@@ -342,6 +350,30 @@ svga_texture_transfer_map(struct pipe_context *pipe,
    if (!st)
       return NULL;
 
+   st->base.level = level;
+   st->base.usage = usage;
+   st->base.box = *box;
+
+   switch (tex->b.b.target) {
+   case PIPE_TEXTURE_CUBE:
+      st->slice = st->base.box.z;
+      st->base.box.z = 0;   /* so we don't apply double offsets below */
+      break;
+   case PIPE_TEXTURE_2D_ARRAY:
+   case PIPE_TEXTURE_1D_ARRAY:
+      st->slice = st->base.box.z;
+      st->base.box.z = 0;   /* so we don't apply double offsets below */
+
+      /* Force direct map for transfering multiple slices */
+      if (st->base.box.depth > 1)
+         use_direct_map = svga_have_gb_objects(svga);
+
+      break;
+   default:
+      st->slice = 0;
+      break;
+   }
+
    {
       unsigned w, h;
       if (use_direct_map) {
@@ -362,22 +394,13 @@ svga_texture_transfer_map(struct pipe_context *pipe,
 
    pipe_resource_reference(&st->base.resource, texture);
 
-   st->base.level = level;
-   st->base.usage = usage;
-   st->base.box = *box;
    st->base.stride = nblocksx*util_format_get_blocksize(texture->format);
    st->base.layer_stride = st->base.stride * nblocksy;
 
-   switch (tex->b.b.target) {
-   case PIPE_TEXTURE_CUBE:
-   case PIPE_TEXTURE_2D_ARRAY:
-   case PIPE_TEXTURE_1D_ARRAY:
-      st->slice = st->base.box.z;
-      st->base.box.z = 0;   /* so we don't apply double offsets below */
-      break;
-   default:
-      st->slice = 0;
-      break;
+   if (usage & PIPE_TRANSFER_WRITE) {
+      /* record texture upload for HUD */
+      svga->hud.num_bytes_uploaded +=
+         nblocksx * nblocksy * d * util_format_get_blocksize(texture->format);
    }
 
    if (!use_direct_map) {
@@ -430,6 +453,13 @@ svga_texture_transfer_map(struct pipe_context *pipe,
          return NULL;
       }
 
+      /* If this is the first time mapping to the surface in this
+       * command buffer, clear the dirty masks of this surface.
+       */
+      if (sws->surface_is_flushed(sws, surf)) {
+         svga_clear_texture_dirty(tex);
+      }
+
       if (need_tex_readback(transfer)) {
         enum pipe_error ret;
 
@@ -442,6 +472,8 @@ svga_texture_transfer_map(struct pipe_context *pipe,
             ret = readback_image_vgpu9(svga, surf, st->slice, transfer->level);
          }
 
+         svga->hud.num_readbacks++;
+
          assert(ret == PIPE_OK);
          (void) ret;
 
@@ -456,11 +488,23 @@ svga_texture_transfer_map(struct pipe_context *pipe,
       else {
         assert(transfer->usage & PIPE_TRANSFER_WRITE);
         if ((transfer->usage & PIPE_TRANSFER_UNSYNCHRONIZED) == 0) {
-            svga_surfaces_flush(svga);
-            if (!sws->surface_is_flushed(sws, surf))
-               svga_context_flush(svga, NULL);
+            if (svga_is_texture_dirty(tex, st->slice, transfer->level)) {
+               /*
+                * do a surface flush if the subresource has been modified
+                * in this command buffer.
+                */
+               svga_surfaces_flush(svga);
+               if (!sws->surface_is_flushed(sws, surf)) {
+                  svga->hud.surface_write_flushes++;
+                  svga_context_flush(svga, NULL);
+               }
+            }
         }
       }
+      if (transfer->usage & PIPE_TRANSFER_WRITE) {
+         /* mark this texture level as dirty */
+         svga_set_texture_dirty(tex, st->slice, transfer->level);
+      }
    }
 
    st->use_direct_map = use_direct_map;
@@ -500,7 +544,7 @@ svga_texture_transfer_map(struct pipe_context *pipe,
       /*
        * Make sure we return NULL if the map fails
        */
-      if (map == NULL) {
+      if (!map) {
          FREE(st);
          return map;
       }
@@ -512,6 +556,13 @@ svga_texture_transfer_map(struct pipe_context *pipe,
       baseLevelSize.height = tex->b.b.height0;
       baseLevelSize.depth = tex->b.b.depth0;
 
+      if ((tex->b.b.target == PIPE_TEXTURE_1D_ARRAY) ||
+          (tex->b.b.target == PIPE_TEXTURE_2D_ARRAY)) {
+         st->base.layer_stride =
+            svga3dsurface_get_image_offset(tex->key.format, baseLevelSize,
+                                           tex->b.b.last_level + 1, 1, 0);
+      }
+
       offset = svga3dsurface_get_image_offset(tex->key.format, baseLevelSize,
                                               tex->b.b.last_level + 1, /* numMips */
                                               st->slice, level);
@@ -638,27 +689,35 @@ svga_texture_transfer_unmap(struct pipe_context *pipe,
         svga_texture(transfer->resource)->handle;
       SVGA3dBox box;
       enum pipe_error ret;
+      unsigned nlayers = 1;
 
       assert(svga_have_gb_objects(svga));
 
       /* update the effected region */
       box.x = transfer->box.x;
       box.y = transfer->box.y;
+      box.w = transfer->box.width;
+      box.h = transfer->box.height;
+      box.d = transfer->box.depth;
+
       switch (tex->b.b.target) {
       case PIPE_TEXTURE_CUBE:
+         box.z = 0;
+         break;
       case PIPE_TEXTURE_2D_ARRAY:
+         nlayers = box.d;
          box.z = 0;
+         box.d = 1;
          break;
       case PIPE_TEXTURE_1D_ARRAY:
+         nlayers = box.d;
          box.y = box.z = 0;
+         box.d = 1;
          break;
       default:
          box.z = transfer->box.z;
          break;
       }
-      box.w = transfer->box.width;
-      box.h = transfer->box.height;
-      box.d = transfer->box.depth;
 
       if (0)
          debug_printf("%s %d, %d, %d  %d x %d x %d\n",
@@ -667,13 +726,21 @@ svga_texture_transfer_unmap(struct pipe_context *pipe,
                       box.w, box.h, box.d);
 
       if (svga_have_vgpu10(svga)) {
-         ret = update_image_vgpu10(svga, surf, &box, st->slice, transfer->level,
-                                   tex->b.b.last_level + 1);
+         unsigned i;
+         for (i = 0; i < nlayers; i++) {
+            ret = update_image_vgpu10(svga, surf, &box,
+                                      st->slice + i, transfer->level,
+                                      tex->b.b.last_level + 1);
+            assert(ret == PIPE_OK);
+         }
       } else {
+         assert(nlayers == 1);
          ret = update_image_vgpu9(svga, surf, &box, st->slice, transfer->level);
+         assert(ret == PIPE_OK);
       }
 
-      assert(ret == PIPE_OK);
+      svga->hud.num_resource_updates++;
+
       (void) ret;
    }
 
@@ -712,7 +779,6 @@ struct u_resource_vtbl svga_texture_vtbl =
    svga_texture_transfer_map,        /* transfer_map */
    u_default_transfer_flush_region,   /* transfer_flush_region */
    svga_texture_transfer_unmap,              /* transfer_unmap */
-   u_default_transfer_inline_write    /* transfer_inline_write */
 };
 
 
@@ -744,9 +810,13 @@ svga_texture_create(struct pipe_screen *screen,
    tex->rendered_to = CALLOC(template->depth0 * template->array_size,
                              sizeof(tex->rendered_to[0]));
    if (!tex->rendered_to) {
-      FREE(tex->defined);
-      FREE(tex);
-      return NULL;
+      goto fail;
+   }
+
+   tex->dirty = CALLOC(template->depth0 * template->array_size,
+                             sizeof(tex->dirty[0]));
+   if (!tex->dirty) {
+      goto fail;
    }
 
    tex->b.b = *template;
@@ -805,6 +875,17 @@ svga_texture_create(struct pipe_screen *screen,
 
    tex->key.cachable = 1;
 
+   if ((bindings & (PIPE_BIND_RENDER_TARGET | PIPE_BIND_DEPTH_STENCIL)) &&
+       !(bindings & PIPE_BIND_SAMPLER_VIEW)) {
+      /* Also check if the format can be sampled from */
+      if (screen->is_format_supported(screen, template->format,
+                                      template->target,
+                                      template->nr_samples,
+                                      PIPE_BIND_SAMPLER_VIEW)) {
+         bindings |= PIPE_BIND_SAMPLER_VIEW;
+      }
+   }
+
    if (bindings & PIPE_BIND_SAMPLER_VIEW) {
       tex->key.flags |= SVGA3D_SURFACE_HINT_TEXTURE;
       tex->key.flags |= SVGA3D_SURFACE_BIND_SHADER_RESOURCE;
@@ -860,20 +941,20 @@ svga_texture_create(struct pipe_screen *screen,
    tex->key.format = svga_translate_format(svgascreen, template->format,
                                            bindings);
    if (tex->key.format == SVGA3D_FORMAT_INVALID) {
-      FREE(tex->defined);
-      FREE(tex->rendered_to);
-      FREE(tex);
-      return NULL;
+      goto fail;
    }
 
-   /* Use typeless formats for sRGB and depth resources.  Typeless
+   /* The actual allocation is done with a typeless format.  Typeless
     * formats can be reinterpreted as other formats.  For example,
     * SVGA3D_R8G8B8A8_UNORM_TYPELESS can be interpreted as
     * SVGA3D_R8G8B8A8_UNORM_SRGB or SVGA3D_R8G8B8A8_UNORM.
+    * Do not use typeless formats for SHARED, DISPLAY_TARGET or SCANOUT
+    * buffers.
     */
-   if (svgascreen->sws->have_vgpu10 &&
-       (util_format_is_srgb(template->format) ||
-        format_has_depth(template->format))) {
+   if (svgascreen->sws->have_vgpu10
+       && ((bindings & (PIPE_BIND_SHARED |
+                        PIPE_BIND_DISPLAY_TARGET |
+                        PIPE_BIND_SCANOUT)) == 0)) {
       SVGA3dSurfaceFormat typeless = svga_typeless_format(tex->key.format);
       if (0) {
          debug_printf("Convert resource type %s -> %s (bind 0x%x)\n",
@@ -881,6 +962,16 @@ svga_texture_create(struct pipe_screen *screen,
                       svga_format_name(typeless),
                       bindings);
       }
+
+      if (svga_format_is_uncompressed_snorm(tex->key.format)) {
+         /* We can't normally render to snorm surfaces, but once we
+          * substitute a typeless format, we can if the rendertarget view
+          * is unorm.  This can happen with GL_ARB_copy_image.
+          */
+         tex->key.flags |= SVGA3D_SURFACE_HINT_RENDERTARGET;
+         tex->key.flags |= SVGA3D_SURFACE_BIND_RENDER_TARGET;
+      }
+
       tex->key.format = typeless;
    }
 
@@ -888,10 +979,7 @@ svga_texture_create(struct pipe_screen *screen,
    tex->handle = svga_screen_surface_create(svgascreen, bindings,
                                             tex->b.b.usage, &tex->key);
    if (!tex->handle) {
-      FREE(tex->defined);
-      FREE(tex->rendered_to);
-      FREE(tex);
-      return NULL;
+      goto fail;
    }
 
    SVGA_DBG(DEBUG_DMA, "  --> got sid %p (texture)\n", tex->handle);
@@ -904,6 +992,16 @@ svga_texture_create(struct pipe_screen *screen,
    svgascreen->hud.num_resources++;
 
    return &tex->b.b;
+
+fail:
+   if (tex->dirty)
+      FREE(tex->dirty);
+   if (tex->rendered_to)
+      FREE(tex->rendered_to);
+   if (tex->defined)
+      FREE(tex->defined);
+   FREE(tex);
+   return NULL;
 }
 
 
@@ -981,9 +1079,89 @@ svga_texture_from_handle(struct pipe_screen *screen,
    tex->handle = srf;
 
    tex->rendered_to = CALLOC(1, sizeof(tex->rendered_to[0]));
+   if (!tex->rendered_to)
+      goto fail;
+
+   tex->dirty = CALLOC(1, sizeof(tex->dirty[0]));
+   if (!tex->dirty)
+      goto fail;
+
    tex->imported = TRUE;
 
    ss->hud.num_resources++;
 
    return &tex->b.b;
+
+fail:
+   if (tex->defined)
+      FREE(tex->defined);
+   if (tex->rendered_to)
+      FREE(tex->rendered_to);
+   if (tex->dirty)
+      FREE(tex->dirty);
+   FREE(tex);
+   return NULL;
+}
+
+boolean
+svga_texture_generate_mipmap(struct pipe_context *pipe,
+                             struct pipe_resource *pt,
+                             enum pipe_format format,
+                             unsigned base_level,
+                             unsigned last_level,
+                             unsigned first_layer,
+                             unsigned last_layer)
+{
+   struct pipe_sampler_view templ, *psv;
+   struct svga_pipe_sampler_view *sv;
+   struct svga_context *svga = svga_context(pipe);
+   struct svga_texture *tex = svga_texture(pt);
+   enum pipe_error ret;
+
+   assert(svga_have_vgpu10(svga));
+
+   /* Only support 2D texture for now */
+   if (pt->target != PIPE_TEXTURE_2D)
+      return FALSE;
+
+   /* Fallback to the mipmap generation utility for those formats that
+    * do not support hw generate mipmap
+    */
+   if (!svga_format_support_gen_mips(format))
+      return FALSE;
+
+   /* Make sure the texture surface was created with
+    * SVGA3D_SURFACE_BIND_RENDER_TARGET
+    */
+   if (!tex->handle || !(tex->key.flags & SVGA3D_SURFACE_BIND_RENDER_TARGET))
+      return FALSE;
+
+   templ.format = format;
+   templ.u.tex.first_layer = first_layer;
+   templ.u.tex.last_layer = last_layer;
+   templ.u.tex.first_level = base_level;
+   templ.u.tex.last_level = last_level;
+
+   psv = pipe->create_sampler_view(pipe, pt, &templ);
+   if (psv == NULL)
+      return FALSE;
+
+   sv = svga_pipe_sampler_view(psv);
+   ret = svga_validate_pipe_sampler_view(svga, sv);
+   if (ret != PIPE_OK) {
+      svga_context_flush(svga, NULL);
+      ret = svga_validate_pipe_sampler_view(svga, sv);
+      assert(ret == PIPE_OK);
+   }
+
+   ret = SVGA3D_vgpu10_GenMips(svga->swc, sv->id, tex->handle);
+   if (ret != PIPE_OK) {
+      svga_context_flush(svga, NULL);
+      ret = SVGA3D_vgpu10_GenMips(svga->swc, sv->id, tex->handle);
+   }
+   pipe_sampler_view_reference(&psv, NULL);
+
+   svga->hud.num_generate_mipmap++;
+
+   return TRUE;
 }