svga: Fix zslice index to svga_texture_copy_handle_resource()
[mesa.git] / src / gallium / drivers / svga / svga_surface.c
index f6a791332c919c71db235374c8794e912d9d7b4c..bbd31c63c8cd02b1407c4b635b3378ed69fa4ac6 100644 (file)
@@ -103,6 +103,50 @@ svga_texture_copy_handle(struct svga_context *svga,
 }
 
 
+/* A helper function to sync up the two surface handles.
+ */
+static void
+svga_texture_copy_handle_resource(struct svga_context *svga,
+                                  struct svga_texture *src_tex,
+                                  struct svga_winsys_surface *dst,
+                                  unsigned int numMipLevels,
+                                  unsigned int numLayers,
+                                  int zslice_pick,
+                                  unsigned int mipoffset,
+                                  unsigned int layeroffset)
+{
+   unsigned int i, j;
+   unsigned int zoffset = 0;
+
+   /* A negative zslice_pick implies zoffset at 0, and depth to copy is
+    * from the depth of the texture at the particular mipmap level.
+    */
+   if (zslice_pick >= 0)
+      zoffset = zslice_pick;
+
+   for (i = 0; i < numMipLevels; i++) {
+      unsigned int miplevel = i + mipoffset;
+
+      for (j = 0; j < numLayers; j++) {
+         if (svga_is_texture_level_defined(src_tex, j+layeroffset, miplevel)) {
+            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);
+         }
+      }
+   }
+}
+
+
 struct svga_winsys_surface *
 svga_texture_view_surface(struct svga_context *svga,
                           struct svga_texture *tex,
@@ -118,8 +162,7 @@ svga_texture_view_surface(struct svga_context *svga,
 {
    struct svga_screen *ss = svga_screen(svga->pipe.screen);
    struct svga_winsys_surface *handle;
-   uint32_t i, j;
-   unsigned z_offset = 0;
+   boolean validated;
 
    SVGA_DBG(DEBUG_PERF,
             "svga: Create surface view: layer %d zslice %d mips %d..%d\n",
@@ -134,7 +177,9 @@ svga_texture_view_surface(struct svga_context *svga,
    key->cachable = 1;
    key->arraySize = 1;
    key->numFaces = 1;
-   key->sampleCount = tex->b.b.nr_samples;
+
+   /* single sample surface can be treated as non-multisamples surface */
+   key->sampleCount = tex->b.b.nr_samples > 1 ? tex->b.b.nr_samples : 0;
 
    if (key->sampleCount > 1) {
       key->flags |= SVGA3D_SURFACE_MASKABLE_ANTIALIAS;
@@ -154,7 +199,8 @@ svga_texture_view_surface(struct svga_context *svga,
    }
 
    SVGA_DBG(DEBUG_DMA, "surface_create for texture view\n");
-   handle = svga_screen_surface_create(ss, bind_flags, PIPE_USAGE_DEFAULT, key);
+   handle = svga_screen_surface_create(ss, bind_flags, PIPE_USAGE_DEFAULT,
+                                       &validated, key);
    if (!handle) {
       key->cachable = 0;
       return NULL;
@@ -165,28 +211,10 @@ svga_texture_view_surface(struct svga_context *svga,
    if (layer_pick < 0)
       layer_pick = 0;
 
-   if (zslice_pick >= 0)
-      z_offset = zslice_pick;
-
-   for (i = 0; i < key->numMipLevels; i++) {
-      for (j = 0; j < key->numFaces * key->arraySize; j++) {
-         if (svga_is_texture_level_defined(tex, j + layer_pick, i + start_mip)) {
-            unsigned depth = (zslice_pick < 0 ?
-                              u_minify(tex->b.b.depth0, i + start_mip) :
-                              1);
-
-            svga_texture_copy_handle(svga,
-                                     tex->handle,
-                                     0, 0, z_offset,
-                                     i + start_mip,
-                                     j + layer_pick,
-                                     handle, 0, 0, 0, i, j,
-                                     u_minify(tex->b.b.width0, i + start_mip),
-                                     u_minify(tex->b.b.height0, i + start_mip),
-                                     depth);
-         }
-      }
-   }
+   svga_texture_copy_handle_resource(svga, tex, handle,
+                                     key->numMipLevels,
+                                     key->numFaces * key->arraySize,
+                                     zslice_pick, start_mip, layer_pick);
 
    return handle;
 }
@@ -370,11 +398,12 @@ svga_create_surface(struct pipe_context *pipe,
 static struct svga_surface *
 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 svga_texture *tex = svga_texture(s->base.texture);
       struct pipe_surface *backed_view;
 
       backed_view = svga_create_surface_view(&svga->pipe,
@@ -386,6 +415,33 @@ create_backed_surface_view(struct svga_context *svga, struct svga_surface *s)
 
       s->backed = svga_surface(backed_view);
    }
+   else {
+      /*
+       * There is already an existing backing surface, but we still need to
+       * sync the handles.
+       */
+      struct svga_surface *bs = s->backed;
+      unsigned int layer, zslice;
+
+      assert(bs->handle);
+
+      switch (tex->b.b.target) {
+      case PIPE_TEXTURE_CUBE:
+      case PIPE_TEXTURE_1D_ARRAY:
+      case PIPE_TEXTURE_2D_ARRAY:
+         layer = s->base.u.tex.first_layer;
+         zslice = 0;
+         break;
+      default:
+         layer = 0;
+         zslice = s->base.u.tex.first_layer;
+      }
+
+      svga_texture_copy_handle_resource(svga, tex, bs->handle,
+                                        bs->key.numMipLevels,
+                                        bs->key.numFaces * bs->key.arraySize,
+                                        zslice, s->base.u.tex.level, layer);
+   }
 
    svga_mark_surface_dirty(&s->backed->base);
 
@@ -403,9 +459,9 @@ svga_validate_surface_view(struct svga_context *svga, struct svga_surface *s)
 {
    enum pipe_error ret = PIPE_OK;
    enum pipe_shader_type shader;
-   struct pipe_surface *surf = NULL;
 
    assert(svga_have_vgpu10(svga));
+   assert(s);
 
    SVGA_STATS_TIME_PUSH(svga_sws(svga),
                         SVGA_STATS_TIME_VALIDATESURFACEVIEW);
@@ -424,16 +480,35 @@ svga_validate_surface_view(struct svga_context *svga, struct svga_surface *s)
                   "same resource used in shaderResource and renderTarget 0x%x\n",
                   s->handle);
          s = create_backed_surface_view(svga, s);
-         if (!s)
-            goto done;
 
+         if (s)
+            svga->state.hw_draw.has_backed_views = TRUE;
+
+         /* s may be null here if the function failed */
          break;
       }
    }
 
-   if (s->view_id == SVGA3D_INVALID_ID) {
+   if (s && s->view_id == SVGA3D_INVALID_ID) {
       SVGA3dResourceType resType;
       SVGA3dRenderTargetViewDesc desc;
+      struct svga_texture *stex = svga_texture(s->base.texture);
+
+      if (stex->validated == FALSE) {
+         assert(stex->handle);
+
+         /* We are about to render into a surface that has not been validated.
+          * First invalidate the surface so that the device does not
+          * 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;
+         }
+         stex->validated = TRUE;
+      }
 
       desc.tex.mipSlice = s->real_level;
       desc.tex.firstArraySlice = s->real_layer + s->real_zslice;
@@ -442,25 +517,7 @@ svga_validate_surface_view(struct svga_context *svga, struct svga_surface *s)
 
       s->view_id = util_bitmask_add(svga->surface_view_id_bm);
 
-      switch (s->base.texture->target) {
-      case PIPE_TEXTURE_1D:
-      case PIPE_TEXTURE_1D_ARRAY:
-         resType = SVGA3D_RESOURCE_TEXTURE1D;
-         break;
-      case PIPE_TEXTURE_RECT:
-      case PIPE_TEXTURE_2D:
-      case PIPE_TEXTURE_2D_ARRAY:
-      case PIPE_TEXTURE_CUBE:
-         /* drawing to cube map is treated as drawing to 2D array */
-         resType = SVGA3D_RESOURCE_TEXTURE2D;
-         break;
-      case PIPE_TEXTURE_3D:
-         resType = SVGA3D_RESOURCE_TEXTURE3D;
-         break;
-      default:
-         assert(!"Unexpected texture target");
-         resType = SVGA3D_RESOURCE_TEXTURE2D;
-      }
+      resType = svga_resource_type(s->base.texture->target);
 
       if (util_format_is_depth_or_stencil(s->base.format)) {
          ret = SVGA3D_vgpu10_DefineDepthStencilView(svga->swc,
@@ -479,7 +536,8 @@ svga_validate_surface_view(struct svga_context *svga, struct svga_surface *s)
           * svga_validate_pipe_sampler_view().
           */
          if (view_format == SVGA3D_B8G8R8A8_UNORM &&
-             stex->key.format == SVGA3D_B8G8R8X8_TYPELESS) {
+             (stex->key.format == SVGA3D_B8G8R8X8_UNORM ||
+              stex->key.format == SVGA3D_B8G8R8X8_TYPELESS)) {
             view_format = SVGA3D_B8G8R8X8_UNORM;
          }
 
@@ -494,15 +552,14 @@ svga_validate_surface_view(struct svga_context *svga, struct svga_surface *s)
       if (ret != PIPE_OK) {
          util_bitmask_clear(svga->surface_view_id_bm, s->view_id);
          s->view_id = SVGA3D_INVALID_ID;
-         goto done;
+         s = NULL;
       }
    }
-   surf = &s->base;
    
 done:
    SVGA_STATS_TIME_POP(svga_sws(svga));
 
-   return surf;
+   return s ? &s->base : NULL;
 }
 
 
@@ -533,20 +590,31 @@ svga_surface_destroy(struct pipe_context *pipe,
    if (s->view_id != SVGA3D_INVALID_ID) {
       unsigned try;
 
-      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);
+      /* 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.
+       * Similar to shader resource view, in this case, we will skip
+       * the destroy for now.
+       */
+      if (surf->context != pipe) {
+         _debug_printf("context mismatch in %s\n", __func__);
+      }
+      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 (ret == PIPE_OK)
-            break;
-         svga_context_flush(svga, NULL);
+         assert(ret == PIPE_OK);
+         util_bitmask_clear(svga->surface_view_id_bm, s->view_id);
       }
-      assert(ret == PIPE_OK);
-      util_bitmask_clear(svga->surface_view_id_bm, s->view_id);
    }
 
    pipe_resource_reference(&surf->texture, NULL);
@@ -602,7 +670,8 @@ svga_mark_surfaces_dirty(struct svga_context *svga)
  * pipe is optional context to inline the blit command in.
  */
 void
-svga_propagate_surface(struct svga_context *svga, struct pipe_surface *surf)
+svga_propagate_surface(struct svga_context *svga, struct pipe_surface *surf,
+                       boolean reset)
 {
    struct svga_surface *s = svga_surface(surf);
    struct svga_texture *tex = svga_texture(surf->texture);
@@ -613,7 +682,14 @@ svga_propagate_surface(struct svga_context *svga, struct pipe_surface *surf)
 
    SVGA_STATS_TIME_PUSH(ss->sws, SVGA_STATS_TIME_PROPAGATESURFACE);
 
-   s->dirty = FALSE;
+   /* Reset the dirty flag if specified. This is to ensure that
+    * the dirty flag will not be reset and stay unset when the backing
+    * surface is still being bound and rendered to.
+    * The reset flag will be set to TRUE when the surface is propagated
+    * and will be unbound.
+    */
+   s->dirty = !reset;
+
    ss->texture_timestamp++;
    svga_age_texture_view(tex, surf->u.tex.level);
 
@@ -666,6 +742,10 @@ svga_propagate_rendertargets(struct svga_context *svga)
 {
    unsigned i;
 
+   /* Early exit if there is no backing texture views in use */
+   if (!svga->state.hw_draw.has_backed_views)
+      return;
+
    /* Note that we examine the svga->state.hw_draw.framebuffer surfaces,
     * not the svga->curr.framebuffer surfaces, because it's the former
     * surfaces which may be backing surface views (the actual render targets).
@@ -673,12 +753,12 @@ svga_propagate_rendertargets(struct svga_context *svga)
    for (i = 0; i < svga->state.hw_draw.num_rendertargets; i++) {
       struct pipe_surface *s = svga->state.hw_draw.rtv[i];
       if (s) {
-         svga_propagate_surface(svga, s);
+         svga_propagate_surface(svga, s, FALSE);
       }
    }
 
    if (svga->state.hw_draw.dsv) {
-      svga_propagate_surface(svga, svga->state.hw_draw.dsv);
+      svga_propagate_surface(svga, svga->state.hw_draw.dsv, FALSE);
    }
 }