svga: Move rendertarget view related fields to hw_clear state
[mesa.git] / src / gallium / drivers / svga / svga_surface.c
index 846acccacec493f5b090b70799295d8e3d751a04..02df0ca3efcb0ad33adebb6ff6c1a23a8a4cb8b7 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,6 @@ 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,
@@ -169,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;
 }
@@ -271,7 +295,8 @@ svga_create_surface_view(struct pipe_context *pipe,
    assert(format != SVGA3D_FORMAT_INVALID);
 
    if (view) {
-      SVGA_DBG(DEBUG_VIEWS, "svga: Surface view: yes %p, level %u layer %u z %u, %p\n",
+      SVGA_DBG(DEBUG_VIEWS,
+               "New backed surface view: resource %p, level %u layer %u z %u, %p\n",
                pt, surf_tmpl->u.tex.level, layer, zslice, s);
 
       if (svga_have_vgpu10(svga)) {
@@ -315,7 +340,7 @@ svga_create_surface_view(struct pipe_context *pipe,
       s->real_zslice = 0;
    } else {
       SVGA_DBG(DEBUG_VIEWS,
-               "svga: Surface view: no %p, level %u, layer %u, z %u, %p\n",
+               "New surface view: resource %p, level %u, layer %u, z %u, %p\n",
                pt, surf_tmpl->u.tex.level, layer, zslice, s);
 
       memset(&s->key, 0, sizeof s->key);
@@ -374,11 +399,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,
@@ -390,6 +416,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);
 
@@ -662,8 +715,8 @@ svga_propagate_surface(struct svga_context *svga, struct pipe_surface *surf,
       }
 
       SVGA_DBG(DEBUG_VIEWS,
-               "svga: Surface propagate: tex %p, level %u, from %p\n",
-               tex, surf->u.tex.level, surf);
+               "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,
@@ -698,15 +751,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);
    }
 }