svga: Performance fixes
[mesa.git] / src / gallium / drivers / svga / svga_surface.c
index 486d900e117b5ffe3f050e98ee42351675c0c3d2..68edf1c21db55af445f50dc2964f02da2b65fd25 100644 (file)
@@ -30,7 +30,7 @@
 #include "util/u_inlines.h"
 #include "os/os_thread.h"
 #include "util/u_bitmask.h"
-#include "util/u_format.h"
+#include "util/format/u_format.h"
 #include "util/u_math.h"
 #include "util/u_memory.h"
 
 
 static void svga_mark_surface_dirty(struct pipe_surface *surf);
 
+void
+svga_texture_copy_region(struct svga_context *svga,
+                         struct svga_winsys_surface *src_handle,
+                         unsigned srcSubResource,
+                         unsigned src_x, unsigned src_y, unsigned src_z,
+                         struct svga_winsys_surface *dst_handle,
+                         unsigned dstSubResource,
+                         unsigned dst_x, unsigned dst_y, unsigned dst_z,
+                         unsigned width, unsigned height, unsigned depth)
+{
+   SVGA3dCopyBox box;
+
+   assert(svga_have_vgpu10(svga));
+
+   box.x = dst_x;
+   box.y = dst_y;
+   box.z = dst_z;
+   box.w = width;
+   box.h = height;
+   box.d = depth;
+   box.srcx = src_x;
+   box.srcy = src_y;
+   box.srcz = src_z;
+
+   SVGA_RETRY(svga, SVGA3D_vgpu10_PredCopyRegion
+              (svga->swc, dst_handle, dstSubResource,
+               src_handle, srcSubResource, &box));
+}
+
+
 void
 svga_texture_copy_handle(struct svga_context *svga,
                          struct svga_winsys_surface *src_handle,
@@ -55,7 +85,6 @@ svga_texture_copy_handle(struct svga_context *svga,
                          unsigned width, unsigned height, unsigned depth)
 {
    struct svga_surface dst, src;
-   enum pipe_error ret;
    SVGA3dCopyBox box, *boxes;
 
    assert(svga);
@@ -86,18 +115,11 @@ svga_texture_copy_handle(struct svga_context *svga,
             dst_handle, dst_level, dst_x, dst_y, dst_z);
 */
 
-   ret = SVGA3D_BeginSurfaceCopy(svga->swc,
-                                 &src.base,
-                                 &dst.base,
-                                 &boxes, 1);
-   if (ret != PIPE_OK) {
-      svga_context_flush(svga, NULL);
-      ret = SVGA3D_BeginSurfaceCopy(svga->swc,
-                                    &src.base,
-                                    &dst.base,
-                                    &boxes, 1);
-      assert(ret == PIPE_OK);
-   }
+   SVGA_RETRY(svga, SVGA3D_BeginSurfaceCopy(svga->swc,
+                                            &src.base,
+                                            &dst.base,
+                                            &boxes, 1));
+
    *boxes = box;
    SVGA_FIFOCommitAll(svga->swc);
 }
@@ -132,15 +154,24 @@ svga_texture_copy_handle_resource(struct svga_context *svga,
             unsigned depth = (zslice_pick < 0 ?
                               u_minify(src_tex->b.b.depth0, miplevel) : 1);
 
-            svga_texture_copy_handle(svga,
-                                     src_tex->handle,
-                                     0, 0, zoffset,
-                                     miplevel,
-                                     j + layeroffset,
-                                     dst, 0, 0, 0, i, j,
-                                     u_minify(src_tex->b.b.width0, miplevel),
-                                     u_minify(src_tex->b.b.height0, miplevel),
-                                     depth);
+            if (src_tex->b.b.nr_samples > 1) {
+               unsigned subResource = j * numMipLevels + i;
+               svga_texture_copy_region(svga, src_tex->handle,
+                                        subResource, 0, 0, zoffset,
+                                        dst, subResource, 0, 0, 0,
+                                        src_tex->b.b.width0, src_tex->b.b.height0, depth);
+            }
+            else {
+               svga_texture_copy_handle(svga,
+                                        src_tex->handle,
+                                        0, 0, zoffset,
+                                        miplevel,
+                                        j + layeroffset,
+                                        dst, 0, 0, 0, i, j,
+                                        u_minify(src_tex->b.b.width0, miplevel),
+                                        u_minify(src_tex->b.b.height0, miplevel),
+                                        depth);
+            }
          }
       }
    }
@@ -151,23 +182,27 @@ struct svga_winsys_surface *
 svga_texture_view_surface(struct svga_context *svga,
                           struct svga_texture *tex,
                           unsigned bind_flags,
-                          SVGA3dSurfaceFlags flags,
+                          SVGA3dSurfaceAllFlags flags,
                           SVGA3dSurfaceFormat format,
                           unsigned start_mip,
                           unsigned num_mip,
                           int layer_pick,
                           unsigned num_layers,
                           int zslice_pick,
+                          boolean cacheable,
                           struct svga_host_surface_cache_key *key) /* OUT */
 {
    struct svga_screen *ss = svga_screen(svga->pipe.screen);
-   struct svga_winsys_surface *handle;
+   struct svga_winsys_surface *handle = NULL;
    boolean validated;
+   boolean needCopyResource;
 
    SVGA_DBG(DEBUG_PERF,
             "svga: Create surface view: layer %d zslice %d mips %d..%d\n",
             layer_pick, zslice_pick, start_mip, start_mip+num_mip-1);
 
+   SVGA_STATS_TIME_PUSH(ss->sws, SVGA_STATS_TIME_EMULATESURFACEVIEW);
+
    key->flags = flags;
    key->format = format;
    key->numMipLevels = num_mip;
@@ -182,7 +217,8 @@ svga_texture_view_surface(struct svga_context *svga,
    key->sampleCount = tex->b.b.nr_samples > 1 ? tex->b.b.nr_samples : 0;
 
    if (key->sampleCount > 1) {
-      key->flags |= SVGA3D_SURFACE_MASKABLE_ANTIALIAS;
+      assert(ss->sws->have_sm4_1);
+      key->flags |= SVGA3D_SURFACE_MULTISAMPLE;
    }
 
    if (tex->b.b.target == PIPE_TEXTURE_CUBE && layer_pick < 0) {
@@ -195,15 +231,28 @@ svga_texture_view_surface(struct svga_context *svga,
 
    if (key->format == SVGA3D_FORMAT_INVALID) {
       key->cachable = 0;
-      return NULL;
+      goto done;
+   }
+
+   if (cacheable && tex->backed_handle &&
+       memcmp(key, &tex->backed_key, sizeof *key) == 0) {
+      handle = tex->backed_handle;
+      needCopyResource = tex->backed_age < tex->age;
+   } else {
+      SVGA_DBG(DEBUG_DMA, "surface_create for texture view\n");
+      handle = svga_screen_surface_create(ss, bind_flags, PIPE_USAGE_DEFAULT,
+                                          &validated, key);
+      needCopyResource = TRUE;
+
+      if (cacheable && !tex->backed_handle) {
+         tex->backed_handle = handle;
+         memcpy(&tex->backed_key, key, sizeof *key);
+      }
    }
 
-   SVGA_DBG(DEBUG_DMA, "surface_create for texture view\n");
-   handle = svga_screen_surface_create(ss, bind_flags, PIPE_USAGE_DEFAULT,
-                                       &validated, key);
    if (!handle) {
       key->cachable = 0;
-      return NULL;
+      goto done;
    }
 
    SVGA_DBG(DEBUG_DMA, " --> got sid %p (texture view)\n", handle);
@@ -211,10 +260,16 @@ svga_texture_view_surface(struct svga_context *svga,
    if (layer_pick < 0)
       layer_pick = 0;
 
-   svga_texture_copy_handle_resource(svga, tex, handle,
-                                     key->numMipLevels,
-                                     key->numFaces * key->arraySize,
-                                     zslice_pick, start_mip, layer_pick);
+   if (needCopyResource) {
+      svga_texture_copy_handle_resource(svga, tex, handle,
+                                        key->numMipLevels,
+                                        key->numFaces * key->arraySize,
+                                        zslice_pick, start_mip, layer_pick);
+      tex->backed_age = tex->age;
+   }
+
+done:
+   SVGA_STATS_TIME_POP(ss->sws);
 
    return handle;
 }
@@ -238,7 +293,7 @@ svga_create_surface_view(struct pipe_context *pipe,
    struct svga_surface *s;
    unsigned layer, zslice, bind;
    unsigned nlayers = 1;
-   SVGA3dSurfaceFlags flags = 0;
+   SVGA3dSurfaceAllFlags flags = 0;
    SVGA3dSurfaceFormat format;
    struct pipe_surface *retVal = NULL;
 
@@ -253,7 +308,8 @@ svga_create_surface_view(struct pipe_context *pipe,
       zslice = 0;
    }
    else if (pt->target == PIPE_TEXTURE_1D_ARRAY ||
-            pt->target == PIPE_TEXTURE_2D_ARRAY) {
+            pt->target == PIPE_TEXTURE_2D_ARRAY ||
+            pt->target == PIPE_TEXTURE_CUBE_ARRAY) {
       layer = surf_tmpl->u.tex.first_layer;
       zslice = 0;
       nlayers = surf_tmpl->u.tex.last_layer - surf_tmpl->u.tex.first_layer + 1;
@@ -287,10 +343,17 @@ svga_create_surface_view(struct pipe_context *pipe,
       bind = PIPE_BIND_RENDER_TARGET;
    }
 
-   if (tex->imported)
+   if (tex->imported) {
+      /* imported resource (a window) */
       format = tex->key.format;
-   else
+      if (util_format_is_srgb(surf_tmpl->format)) {
+         /* sRGB rendering to window */
+         format = svga_linear_to_srgb(format);
+      }
+   }
+   else {
       format = svga_translate_format(ss, surf_tmpl->format, bind);
+   }
 
    assert(format != SVGA3D_FORMAT_INVALID);
 
@@ -317,6 +380,10 @@ svga_create_surface_view(struct pipe_context *pipe,
             if (nlayers == 6)
                flags |= SVGA3D_SURFACE_CUBEMAP;
             break;
+         case PIPE_TEXTURE_CUBE_ARRAY:
+            if (nlayers % 6 == 0)
+               flags |= SVGA3D_SURFACE_CUBEMAP | SVGA3D_SURFACE_ARRAY;
+            break;   
          default:
             break;
          }
@@ -328,7 +395,8 @@ svga_create_surface_view(struct pipe_context *pipe,
       s->handle = svga_texture_view_surface(svga, tex, bind, flags,
                                             tex->key.format,
                                             surf_tmpl->u.tex.level, 1,
-                                            layer, nlayers, zslice, &s->key);
+                                            layer, nlayers, zslice,
+                                            TRUE, &s->key);
       if (!s->handle) {
          FREE(s);
          goto done;
@@ -401,25 +469,28 @@ create_backed_surface_view(struct svga_context *svga, struct svga_surface *s)
 {
    struct svga_texture *tex = svga_texture(s->base.texture);
 
-   SVGA_STATS_TIME_PUSH(svga_sws(svga),
-                        SVGA_STATS_TIME_CREATEBACKEDSURFACEVIEW);
-
    if (!s->backed) {
       struct pipe_surface *backed_view;
 
+      SVGA_STATS_TIME_PUSH(svga_sws(svga),
+                           SVGA_STATS_TIME_CREATEBACKEDSURFACEVIEW);
+
       backed_view = svga_create_surface_view(&svga->pipe,
                                              &tex->b.b,
                                              &s->base,
                                              TRUE);
       if (!backed_view)
-         return NULL;
+         goto done;
 
       s->backed = svga_surface(backed_view);
+
+      SVGA_STATS_TIME_POP(svga_sws(svga));
    }
-   else {
+   else if (s->backed->age < tex->age) {
       /*
        * There is already an existing backing surface, but we still need to
-       * sync the handles.
+       * sync the backing resource if the original resource has been modified
+       * since the last copy.
        */
       struct svga_surface *bs = s->backed;
       unsigned int layer, zslice;
@@ -428,6 +499,7 @@ create_backed_surface_view(struct svga_context *svga, struct svga_surface *s)
 
       switch (tex->b.b.target) {
       case PIPE_TEXTURE_CUBE:
+      case PIPE_TEXTURE_CUBE_ARRAY:
       case PIPE_TEXTURE_1D_ARRAY:
       case PIPE_TEXTURE_2D_ARRAY:
          layer = s->base.u.tex.first_layer;
@@ -445,9 +517,9 @@ create_backed_surface_view(struct svga_context *svga, struct svga_surface *s)
    }
 
    svga_mark_surface_dirty(&s->backed->base);
+   s->backed->age = tex->age;
 
-   SVGA_STATS_TIME_POP(svga_sws(svga));
-
+done:
    return s->backed;
 }
 
@@ -475,7 +547,7 @@ svga_validate_surface_view(struct svga_context *svga, struct svga_surface *s)
     * associated resource. We will then use the cloned surface view for
     * render target.
     */
-   for (shader = PIPE_SHADER_VERTEX; shader <= PIPE_SHADER_GEOMETRY; shader++) {
+   for (shader = PIPE_SHADER_VERTEX; shader <= PIPE_SHADER_TESS_EVAL; shader++) {
       if (svga_check_sampler_view_resource_collision(svga, s->handle, shader)) {
          SVGA_DBG(DEBUG_VIEWS,
                   "same resource used in shaderResource and renderTarget 0x%x\n",
@@ -490,6 +562,16 @@ svga_validate_surface_view(struct svga_context *svga, struct svga_surface *s)
       }
    }
 
+   /**
+    * Create an alternate surface view for the specified context if the
+    * view was created for another context.
+    */
+   if (s && s->base.context != &svga->pipe) {
+      struct pipe_surface *surf;
+      surf = svga_create_surface_view(&svga->pipe, s->base.texture, &s->base, FALSE);
+      s = svga_surface(surf);
+   }
+
    if (s && s->view_id == SVGA3D_INVALID_ID) {
       SVGA3dResourceType resType;
       SVGA3dRenderTargetViewDesc desc;
@@ -503,11 +585,7 @@ svga_validate_surface_view(struct svga_context *svga, struct svga_surface *s)
           * need to update the host-side copy with the invalid
           * content when the associated mob is first bound to the surface.
           */
-         ret = SVGA3D_InvalidateGBSurface(svga->swc, stex->handle);
-         if (ret != PIPE_OK) {
-            s = NULL;
-            goto done;
-         }
+         SVGA_RETRY(svga, SVGA3D_InvalidateGBSurface(svga->swc, stex->handle));
          stex->validated = TRUE;
       }
 
@@ -557,7 +635,6 @@ svga_validate_surface_view(struct svga_context *svga, struct svga_surface *s)
       }
    }
    
-done:
    SVGA_STATS_TIME_POP(svga_sws(svga));
 
    return s ? &s->base : NULL;
@@ -573,7 +650,6 @@ svga_surface_destroy(struct pipe_context *pipe,
    struct svga_surface *s = svga_surface(surf);
    struct svga_texture *t = svga_texture(surf->texture);
    struct svga_screen *ss = svga_screen(surf->texture->screen);
-   enum pipe_error ret = PIPE_OK;
 
    SVGA_STATS_TIME_PUSH(ss->sws, SVGA_STATS_TIME_DESTROYSURFACE);
 
@@ -583,14 +659,15 @@ svga_surface_destroy(struct pipe_context *pipe,
       s->backed = NULL;
    }
 
-   if (s->handle != t->handle) {
+   /* Destroy the surface handle if this is a backed handle and
+    * it is not being cached in the texture.
+    */
+   if (s->handle != t->handle && s->handle != t->backed_handle) {
       SVGA_DBG(DEBUG_DMA, "unref sid %p (tex surface)\n", s->handle);
       svga_screen_surface_destroy(ss, &s->key, &s->handle);
    }
 
    if (s->view_id != SVGA3D_INVALID_ID) {
-      unsigned try;
-
       /* The SVGA3D device will generate a device error if the
        * render target view or depth stencil view is destroyed from
        * a context other than the one it was created with.
@@ -602,18 +679,14 @@ svga_surface_destroy(struct pipe_context *pipe,
       }
       else {
          assert(svga_have_vgpu10(svga));
-         for (try = 0; try < 2; try++) {
-            if (util_format_is_depth_or_stencil(s->base.format)) {
-               ret = SVGA3D_vgpu10_DestroyDepthStencilView(svga->swc, s->view_id);
-            }
-            else {
-               ret = SVGA3D_vgpu10_DestroyRenderTargetView(svga->swc, s->view_id);
-            }
-            if (ret == PIPE_OK)
-               break;
-            svga_context_flush(svga, NULL);
+         if (util_format_is_depth_or_stencil(s->base.format)) {
+            SVGA_RETRY(svga, SVGA3D_vgpu10_DestroyDepthStencilView(svga->swc,
+                                                                   s->view_id));
+         }
+         else {
+            SVGA_RETRY(svga, SVGA3D_vgpu10_DestroyRenderTargetView(svga->swc,
+                                                                   s->view_id));
          }
-         assert(ret == PIPE_OK);
          util_bitmask_clear(svga->surface_view_id_bm, s->view_id);
       }
    }
@@ -647,8 +720,11 @@ svga_mark_surface_dirty(struct pipe_surface *surf)
 
    /* Increment the view_age and texture age for this surface's mipmap
     * level so that any sampler views into the texture are re-validated too.
+    * Note: we age the texture for backed surface view only when the
+    *       backed surface is propagated to the original surface.
     */
-   svga_age_texture_view(tex, surf->u.tex.level);
+   if (s->handle == tex->handle)
+      svga_age_texture_view(tex, surf->u.tex.level);
 }
 
 
@@ -656,13 +732,27 @@ void
 svga_mark_surfaces_dirty(struct svga_context *svga)
 {
    unsigned i;
+   struct svga_hw_clear_state *hw = &svga->state.hw_clear;
+
+   if (svga_have_vgpu10(svga)) {
 
-   for (i = 0; i < svga->curr.framebuffer.nr_cbufs; i++) {
-      if (svga->curr.framebuffer.cbufs[i])
-         svga_mark_surface_dirty(svga->curr.framebuffer.cbufs[i]);
+      /* For VGPU10, mark the dirty bit in the rendertarget/depth stencil view surface.
+       * This surface can be the backed surface.
+       */
+      for (i = 0; i < hw->num_rendertargets; i++) {
+         if (hw->rtv[i])
+            svga_mark_surface_dirty(hw->rtv[i]);
+      }
+      if (hw->dsv)
+         svga_mark_surface_dirty(hw->dsv);
+   } else {
+      for (i = 0; i < svga->curr.framebuffer.nr_cbufs; i++) {
+         if (svga->curr.framebuffer.cbufs[i])
+            svga_mark_surface_dirty(svga->curr.framebuffer.cbufs[i]);
+      }
+      if (svga->curr.framebuffer.zsbuf)
+         svga_mark_surface_dirty(svga->curr.framebuffer.zsbuf);
    }
-   if (svga->curr.framebuffer.zsbuf)
-      svga_mark_surface_dirty(svga->curr.framebuffer.zsbuf);
 }
 
 
@@ -698,13 +788,19 @@ svga_propagate_surface(struct svga_context *svga, struct pipe_surface *surf,
       unsigned zslice, layer;
       unsigned nlayers = 1;
       unsigned i;
+      unsigned numMipLevels = tex->b.b.last_level + 1;
+      unsigned srcLevel = s->real_level;
+      unsigned dstLevel = surf->u.tex.level;
+      unsigned width = u_minify(tex->b.b.width0, dstLevel);
+      unsigned height = u_minify(tex->b.b.height0, dstLevel);
 
       if (surf->texture->target == PIPE_TEXTURE_CUBE) {
          zslice = 0;
          layer = surf->u.tex.first_layer;
       }
       else if (surf->texture->target == PIPE_TEXTURE_1D_ARRAY ||
-               surf->texture->target == PIPE_TEXTURE_2D_ARRAY) {
+               surf->texture->target == PIPE_TEXTURE_2D_ARRAY ||
+               surf->texture->target == PIPE_TEXTURE_CUBE_ARRAY) {
          zslice = 0;
          layer = surf->u.tex.first_layer;
          nlayers = surf->u.tex.last_layer - surf->u.tex.first_layer + 1;
@@ -717,16 +813,42 @@ svga_propagate_surface(struct svga_context *svga, struct pipe_surface *surf,
       SVGA_DBG(DEBUG_VIEWS,
                "Propagate surface %p to resource %p, level %u\n",
                surf, tex, surf->u.tex.level);
-      for (i = 0; i < nlayers; i++) {
-         svga_texture_copy_handle(svga,
-                                  s->handle, 0, 0, 0, s->real_level,
-                                  s->real_layer + i,
-                                  tex->handle, 0, 0, zslice, surf->u.tex.level,
-                                  layer + i,
-                                  u_minify(tex->b.b.width0, surf->u.tex.level),
-                                  u_minify(tex->b.b.height0, surf->u.tex.level),
-                                  1);
-         svga_define_texture_level(tex, layer + i, surf->u.tex.level);
+
+      if (svga_have_vgpu10(svga)) {
+         unsigned srcSubResource, dstSubResource;
+
+         for (i = 0; i < nlayers; i++) {
+            srcSubResource = (s->real_layer + i) * numMipLevels + srcLevel;
+            dstSubResource = (layer + i) * numMipLevels + dstLevel;
+
+            svga_texture_copy_region(svga,
+                                     s->handle, srcSubResource, 0, 0, 0,
+                                     tex->handle, dstSubResource, 0, 0, zslice,
+                                     width, height, 1);
+            svga_define_texture_level(tex, layer + i, dstLevel);
+         }
+      }
+      else {
+         for (i = 0; i < nlayers; i++) {
+            svga_texture_copy_handle(svga,
+                                     s->handle, 0, 0, 0, srcLevel,
+                                     s->real_layer + i,
+                                     tex->handle, 0, 0, zslice, dstLevel,
+                                     layer + i,
+                                     width, height, 1);
+        
+            svga_define_texture_level(tex, layer + i, dstLevel);
+         }
+      }
+
+      /* Sync the surface view age with the texture age */
+      s->age = tex->age;
+
+      /* If this backed surface is cached in the texture,
+       * update the backed age as well.
+       */
+      if (tex->backed_handle == s->handle) {
+         tex->backed_age = tex->age;
       }
    }
 
@@ -751,15 +873,15 @@ svga_propagate_rendertargets(struct svga_context *svga)
     * not the svga->curr.framebuffer surfaces, because it's the former
     * surfaces which may be backing surface views (the actual render targets).
     */
-   for (i = 0; i < svga->state.hw_draw.num_rendertargets; i++) {
-      struct pipe_surface *s = svga->state.hw_draw.rtv[i];
+   for (i = 0; i < svga->state.hw_clear.num_rendertargets; i++) {
+      struct pipe_surface *s = svga->state.hw_clear.rtv[i];
       if (s) {
          svga_propagate_surface(svga, s, FALSE);
       }
    }
 
-   if (svga->state.hw_draw.dsv) {
-      svga_propagate_surface(svga, svga->state.hw_draw.dsv, FALSE);
+   if (svga->state.hw_clear.dsv) {
+      svga_propagate_surface(svga, svga->state.hw_clear.dsv, FALSE);
    }
 }
 
@@ -788,6 +910,10 @@ svga_get_sample_position(struct pipe_context *context,
    static const float pos1[1][2] = {
       { 0.5, 0.5 }
    };
+   static const float pos2[2][2] = {
+      { 0.75, 0.75 },
+      { 0.25, 0.25 }
+   };
    static const float pos4[4][2] = {
       { 0.375000, 0.125000 },
       { 0.875000, 0.375000 },
@@ -825,6 +951,9 @@ svga_get_sample_position(struct pipe_context *context,
    const float (*positions)[2];
 
    switch (sample_count) {
+   case 2:
+      positions = pos2;
+      break;
    case 4:
       positions = pos4;
       break;