svga: add new begin_blit()
[mesa.git] / src / gallium / drivers / svga / svga_surface.c
index 4b0f9417c3273c06723906642aeaa3ef907e4ce3..43a00c35d1e31dd055c248fbc1b582588d2d67aa 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);
 
@@ -139,7 +139,7 @@ svga_texture_view_surface(struct svga_context *svga,
    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 +176,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 +212,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,15 +296,22 @@ 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);
+         goto done;
+      }
+
       s->key.format = format;
       s->real_layer = 0;
       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);
@@ -312,7 +322,12 @@ svga_create_surface_view(struct pipe_context *pipe,
       s->real_level = surf_tmpl->u.tex.level;
    }
 
-   return &s->base;
+   svga->hud.num_surface_views++;
+   retVal = &s->base;
+
+done:
+   SVGA_STATS_TIME_POP(ss->sws);
+   return retVal;
 }
 
 
@@ -323,8 +338,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;
 
@@ -338,7 +356,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;
 }
 
 
@@ -350,7 +372,10 @@ create_backed_surface_view(struct svga_context *svga, struct svga_surface *s)
 {
    struct svga_surface *bs = s->backed;
 
-   if (bs == NULL) {
+   SVGA_STATS_TIME_PUSH(svga_sws(svga),
+                        SVGA_STATS_TIME_CREATEBACKEDSURFACEVIEW);
+
+   if (!bs) {
       struct svga_texture *tex = svga_texture(s->base.texture);
       struct pipe_surface *backed_view;
 
@@ -358,6 +383,8 @@ create_backed_surface_view(struct svga_context *svga, struct svga_surface *s)
                                              &tex->b.b,
                                              &s->base,
                                              TRUE);
+      if (!backed_view)
+         return NULL;
 
       bs = svga_surface(backed_view);
       s->backed = bs;
@@ -365,6 +392,8 @@ create_backed_surface_view(struct svga_context *svga, struct svga_surface *s)
 
    svga_mark_surface_dirty(&bs->base);
 
+   SVGA_STATS_TIME_POP(svga_sws(svga));
+
    return bs;
 }
 
@@ -376,11 +405,14 @@ struct pipe_surface *
 svga_validate_surface_view(struct svga_context *svga, struct svga_surface *s)
 {
    enum pipe_error ret = PIPE_OK;
-   int try;
    unsigned shader;
+   struct pipe_surface *surf = NULL;
 
    assert(svga_have_vgpu10(svga));
 
+   SVGA_STATS_TIME_PUSH(svga_sws(svga),
+                        SVGA_STATS_TIME_VALIDATESURFACEVIEW);
+
    /**
     * DX spec explicitly specifies that no resource can be bound to a render
     * target view and a shader resource view simultanously.
@@ -395,6 +427,9 @@ 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;
+
          break;
       }
    }
@@ -430,35 +465,47 @@ svga_validate_surface_view(struct svga_context *svga, struct svga_surface *s)
          resType = SVGA3D_RESOURCE_TEXTURE2D;
       }
 
-      for (try = 0; try < 2; try++) {
-         if (util_format_is_depth_or_stencil(s->base.format)) {
-            ret = SVGA3D_vgpu10_DefineDepthStencilView(svga->swc,
-                                                       s->view_id,
-                                                       s->handle,
-                                                       s->key.format,
-                                                       resType,
-                                                       &desc);
-         }
-         else {
-            ret = SVGA3D_vgpu10_DefineRenderTargetView(svga->swc,
-                                                       s->view_id,
-                                                       s->handle,
-                                                       s->key.format,
-                                                       resType,
-                                                       &desc);
+      if (util_format_is_depth_or_stencil(s->base.format)) {
+         ret = SVGA3D_vgpu10_DefineDepthStencilView(svga->swc,
+                                                    s->view_id,
+                                                    s->handle,
+                                                    s->key.format,
+                                                    resType,
+                                                    &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;
          }
-         if (ret == PIPE_OK)
-            break;
-         svga_context_flush(svga, NULL);
+
+         ret = SVGA3D_vgpu10_DefineRenderTargetView(svga->swc,
+                                                    s->view_id,
+                                                    s->handle,
+                                                    view_format,
+                                                    resType,
+                                                    &desc);
       }
 
-      assert(ret == PIPE_OK);
       if (ret != PIPE_OK) {
          util_bitmask_clear(svga->surface_view_id_bm, s->view_id);
          s->view_id = SVGA3D_INVALID_ID;
+         goto done;
       }
    }
-   return &s->base;
+   surf = &s->base;
+   
+done:
+   SVGA_STATS_TIME_POP(svga_sws(svga));
+
+   return surf;
 }
 
 
@@ -473,6 +520,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);
@@ -505,17 +554,19 @@ svga_surface_destroy(struct pipe_context *pipe,
 
    pipe_resource_reference(&surf->texture, NULL);
    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);
+   struct svga_texture *tex = svga_texture(surf->texture);
 
    if (!s->dirty) {
-      struct svga_texture *tex = svga_texture(surf->texture);
-
       s->dirty = TRUE;
 
       if (s->handle == tex->handle) {
@@ -526,22 +577,21 @@ svga_mark_surface_dirty(struct pipe_surface *surf)
       else {
          /* this will happen later in svga_propagate_surface */
       }
-
-      /* 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.
-       */
-      svga_age_texture_view(tex, surf->u.tex.level);
    }
+
+   /* 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.
+    */
+   svga_age_texture_view(tex, surf->u.tex.level);
 }
 
 
 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,6 +617,8 @@ svga_propagate_surface(struct svga_context *svga, struct pipe_surface *surf)
    if (!s->dirty)
       return;
 
+   SVGA_STATS_TIME_PUSH(ss->sws, SVGA_STATS_TIME_PROPAGATESURFACE);
+
    if (surf->texture->target == PIPE_TEXTURE_CUBE) {
       zslice = 0;
       layer = surf->u.tex.first_layer;
@@ -592,13 +644,18 @@ svga_propagate_surface(struct svga_context *svga, struct pipe_surface *surf)
                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);
 }