svga: set rendered-to flag after updating the texture using PredCopyRegion
[mesa.git] / src / gallium / drivers / svga / svga_surface.c
index ad06a1d531ef944f9098f25d334214ca3679d98a..935384150cc8f38d01d130b6718aacd522b1c607 100644 (file)
@@ -121,7 +121,7 @@ svga_texture_view_surface(struct svga_context *svga,
    uint32_t i, j;
    unsigned z_offset = 0;
 
-   SVGA_DBG(DEBUG_PERF, 
+   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);
 
@@ -134,12 +134,14 @@ 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;
    }
-   
+
    if (tex->b.b.target == PIPE_TEXTURE_CUBE && layer_pick < 0) {
       key->flags |= SVGA3D_SURFACE_CUBEMAP;
       key->numFaces = 6;
@@ -176,9 +178,9 @@ svga_texture_view_surface(struct svga_context *svga,
                               1);
 
             svga_texture_copy_handle(svga,
-                                     tex->handle, 
-                                     0, 0, z_offset, 
-                                     i + start_mip, 
+                                     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),
@@ -212,11 +214,14 @@ svga_create_surface_view(struct pipe_context *pipe,
    unsigned nlayers = 1;
    SVGA3dSurfaceFlags flags = 0;
    SVGA3dSurfaceFormat format;
+   struct pipe_surface *retVal = NULL;
 
    s = CALLOC_STRUCT(svga_surface);
    if (!s)
       return NULL;
 
+   SVGA_STATS_TIME_PUSH(ss->sws, SVGA_STATS_TIME_CREATESURFACEVIEW);
+
    if (pt->target == PIPE_TEXTURE_CUBE) {
       layer = surf_tmpl->u.tex.first_layer;
       zslice = 0;
@@ -293,12 +298,13 @@ svga_create_surface_view(struct pipe_context *pipe,
       /* When we clone the surface view resource, use the format used in
        * the creation of the original resource.
        */
-      s->handle = svga_texture_view_surface(svga, tex, bind, flags, tex->key.format,
+      s->handle = svga_texture_view_surface(svga, tex, bind, flags,
+                                            tex->key.format,
                                             surf_tmpl->u.tex.level, 1,
                                             layer, nlayers, zslice, &s->key);
       if (!s->handle) {
          FREE(s);
-         return NULL;
+         goto done;
       }
 
       s->key.format = format;
@@ -306,7 +312,8 @@ svga_create_surface_view(struct pipe_context *pipe,
       s->real_level = 0;
       s->real_zslice = 0;
    } else {
-      SVGA_DBG(DEBUG_VIEWS, "svga: Surface view: no %p, level %u, layer %u, z %u, %p\n",
+      SVGA_DBG(DEBUG_VIEWS,
+               "svga: Surface view: no %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);
@@ -318,8 +325,11 @@ svga_create_surface_view(struct pipe_context *pipe,
    }
 
    svga->hud.num_surface_views++;
+   retVal = &s->base;
 
-   return &s->base;
+done:
+   SVGA_STATS_TIME_POP(ss->sws);
+   return retVal;
 }
 
 
@@ -330,8 +340,11 @@ svga_create_surface(struct pipe_context *pipe,
 {
    struct svga_context *svga = svga_context(pipe);
    struct pipe_screen *screen = pipe->screen;
+   struct pipe_surface *surf = NULL;
    boolean view = FALSE;
 
+   SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_CREATESURFACE);
+
    if (svga_screen(screen)->debug.force_surface_view)
       view = TRUE;
 
@@ -345,7 +358,11 @@ svga_create_surface(struct pipe_context *pipe,
    if (svga_have_vgpu10(svga) || svga_screen(screen)->debug.no_surface_view)
       view = FALSE;
 
-   return svga_create_surface_view(pipe, pt, surf_tmpl, view);
+   surf = svga_create_surface_view(pipe, pt, surf_tmpl, view);
+
+   SVGA_STATS_TIME_POP(svga_sws(svga));
+
+   return surf;
 }
 
 
@@ -355,9 +372,10 @@ 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_surface *bs = s->backed;
+   SVGA_STATS_TIME_PUSH(svga_sws(svga),
+                        SVGA_STATS_TIME_CREATEBACKEDSURFACEVIEW);
 
-   if (!bs) {
+   if (!s->backed) {
       struct svga_texture *tex = svga_texture(s->base.texture);
       struct pipe_surface *backed_view;
 
@@ -368,13 +386,14 @@ create_backed_surface_view(struct svga_context *svga, struct svga_surface *s)
       if (!backed_view)
          return NULL;
 
-      bs = svga_surface(backed_view);
-      s->backed = bs;
+      s->backed = svga_surface(backed_view);
    }
 
-   svga_mark_surface_dirty(&bs->base);
+   svga_mark_surface_dirty(&s->backed->base);
 
-   return bs;
+   SVGA_STATS_TIME_POP(svga_sws(svga));
+
+   return s->backed;
 }
 
 /**
@@ -385,9 +404,13 @@ struct pipe_surface *
 svga_validate_surface_view(struct svga_context *svga, struct svga_surface *s)
 {
    enum pipe_error ret = PIPE_OK;
-   unsigned shader;
+   enum pipe_shader_type shader;
 
    assert(svga_have_vgpu10(svga));
+   assert(s);
+
+   SVGA_STATS_TIME_PUSH(svga_sws(svga),
+                        SVGA_STATS_TIME_VALIDATESURFACEVIEW);
 
    /**
     * DX spec explicitly specifies that no resource can be bound to a render
@@ -403,14 +426,12 @@ 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)
-            return NULL;
-
+         /* 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;
 
@@ -421,25 +442,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,
@@ -450,10 +453,22 @@ svga_validate_surface_view(struct svga_context *svga, struct svga_surface *s)
                                                     &desc);
       }
       else {
+         SVGA3dSurfaceFormat view_format = s->key.format;
+         const struct svga_texture *stex = svga_texture(s->base.texture);
+
+         /* Can't create RGBA render target view of a RGBX surface so adjust
+          * the view format.  We do something similar for texture samplers in
+          * svga_validate_pipe_sampler_view().
+          */
+         if (view_format == SVGA3D_B8G8R8A8_UNORM &&
+             stex->key.format == SVGA3D_B8G8R8X8_TYPELESS) {
+            view_format = SVGA3D_B8G8R8X8_UNORM;
+         }
+
          ret = SVGA3D_vgpu10_DefineRenderTargetView(svga->swc,
                                                     s->view_id,
                                                     s->handle,
-                                                    s->key.format,
+                                                    view_format,
                                                     resType,
                                                     &desc);
       }
@@ -461,10 +476,13 @@ 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;
-         return NULL;
+         s = NULL;
       }
    }
-   return &s->base;
+   
+   SVGA_STATS_TIME_POP(svga_sws(svga));
+
+   return s ? &s->base : NULL;
 }
 
 
@@ -479,6 +497,8 @@ svga_surface_destroy(struct pipe_context *pipe,
    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);
+
    /* Destroy the backed view surface if it exists */
    if (s->backed) {
       svga_surface_destroy(pipe, &s->backed->base);
@@ -513,10 +533,11 @@ svga_surface_destroy(struct pipe_context *pipe,
    FREE(surf);
 
    svga->hud.num_surface_views--;
+   SVGA_STATS_TIME_POP(ss->sws);
 }
 
 
-static void 
+static void
 svga_mark_surface_dirty(struct pipe_surface *surf)
 {
    struct svga_surface *s = svga_surface(surf);
@@ -545,10 +566,9 @@ svga_mark_surface_dirty(struct pipe_surface *surf)
 void
 svga_mark_surfaces_dirty(struct svga_context *svga)
 {
-   struct svga_screen *svgascreen = svga_screen(svga->pipe.screen);
    unsigned i;
 
-   for (i = 0; i < svgascreen->max_color_buffers; i++) {
+   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]);
    }
@@ -567,45 +587,79 @@ svga_propagate_surface(struct svga_context *svga, struct pipe_surface *surf)
    struct svga_surface *s = svga_surface(surf);
    struct svga_texture *tex = svga_texture(surf->texture);
    struct svga_screen *ss = svga_screen(surf->texture->screen);
-   unsigned zslice, layer;
-   unsigned nlayers = 1;
-   unsigned i;
 
    if (!s->dirty)
       return;
 
-   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) {
-      zslice = 0;
-      layer = surf->u.tex.first_layer;
-      nlayers = surf->u.tex.last_layer - surf->u.tex.first_layer + 1;
-   }
-   else {
-      zslice = surf->u.tex.first_layer;
-      layer = 0;
-   }
+   SVGA_STATS_TIME_PUSH(ss->sws, SVGA_STATS_TIME_PROPAGATESURFACE);
 
    s->dirty = FALSE;
    ss->texture_timestamp++;
    svga_age_texture_view(tex, surf->u.tex.level);
 
    if (s->handle != tex->handle) {
+      unsigned zslice, layer;
+      unsigned nlayers = 1;
+      unsigned i;
+
+      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) {
+         zslice = 0;
+         layer = surf->u.tex.first_layer;
+         nlayers = surf->u.tex.last_layer - surf->u.tex.first_layer + 1;
+      }
+      else {
+         zslice = surf->u.tex.first_layer;
+         layer = 0;
+      }
+
       SVGA_DBG(DEBUG_VIEWS,
                "svga: Surface propagate: tex %p, level %u, from %p\n",
                tex, surf->u.tex.level, surf);
       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,
+                                  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);
+                                  u_minify(tex->b.b.height0, surf->u.tex.level),
+                                  1);
          svga_define_texture_level(tex, layer + i, surf->u.tex.level);
       }
    }
+
+   SVGA_STATS_TIME_POP(ss->sws);
+}
+
+
+/**
+ * If any of the render targets are in backing texture views, propagate any
+ * changes to them back to the original texture.
+ */
+void
+svga_propagate_rendertargets(struct svga_context *svga)
+{
+   unsigned i;
+
+   /* 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).
+    */
+   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);
+      }
+   }
+
+   if (svga->state.hw_draw.dsv) {
+      svga_propagate_surface(svga, svga->state.hw_draw.dsv);
+   }
 }