i965: use pack/unpackDouble lowering
[mesa.git] / src / gallium / drivers / svga / svga_resource_texture.c
index a02d1e495ff72c807deb47c243fe9d76d6939bce..b7e8549b7828b7192f7f168ff5b1ec7d5e4d919c 100644 (file)
@@ -234,6 +234,7 @@ svga_texture_destroy(struct pipe_screen *screen,
 
    FREE(tex->defined);
    FREE(tex->rendered_to);
+   FREE(tex->dirty);
    FREE(tex);
 
    assert(ss->hud.num_resources > 0);
@@ -380,6 +381,12 @@ svga_texture_transfer_map(struct pipe_context *pipe,
       break;
    }
 
+   if (usage & PIPE_TRANSFER_WRITE) {
+      /* record texture upload for HUD */
+      svga->hud.num_bytes_uploaded +=
+         nblocksx * nblocksy * d * util_format_get_blocksize(texture->format);
+   }
+
    if (!use_direct_map) {
       /* Use a DMA buffer */
       st->hw_nblocksy = nblocksy;
@@ -430,6 +437,13 @@ svga_texture_transfer_map(struct pipe_context *pipe,
          return NULL;
       }
 
+      /* If this is the first time mapping to the surface in this
+       * command buffer, clear the dirty masks of this surface.
+       */
+      if (sws->surface_is_flushed(sws, surf)) {
+         svga_clear_texture_dirty(tex);
+      }
+
       if (need_tex_readback(transfer)) {
         enum pipe_error ret;
 
@@ -442,6 +456,8 @@ svga_texture_transfer_map(struct pipe_context *pipe,
             ret = readback_image_vgpu9(svga, surf, st->slice, transfer->level);
          }
 
+         svga->hud.num_readbacks++;
+
          assert(ret == PIPE_OK);
          (void) ret;
 
@@ -456,11 +472,23 @@ svga_texture_transfer_map(struct pipe_context *pipe,
       else {
         assert(transfer->usage & PIPE_TRANSFER_WRITE);
         if ((transfer->usage & PIPE_TRANSFER_UNSYNCHRONIZED) == 0) {
-            svga_surfaces_flush(svga);
-            if (!sws->surface_is_flushed(sws, surf))
-               svga_context_flush(svga, NULL);
+            if (svga_is_texture_dirty(tex, st->slice, transfer->level)) {
+               /*
+                * do a surface flush if the subresource has been modified
+                * in this command buffer.
+                */
+               svga_surfaces_flush(svga);
+               if (!sws->surface_is_flushed(sws, surf)) {
+                  svga->hud.surface_write_flushes++;
+                  svga_context_flush(svga, NULL);
+               }
+            }
         }
       }
+      if (transfer->usage & PIPE_TRANSFER_WRITE) {
+         /* mark this texture level as dirty */
+         svga_set_texture_dirty(tex, st->slice, transfer->level);
+      }
    }
 
    st->use_direct_map = use_direct_map;
@@ -500,7 +528,7 @@ svga_texture_transfer_map(struct pipe_context *pipe,
       /*
        * Make sure we return NULL if the map fails
        */
-      if (map == NULL) {
+      if (!map) {
          FREE(st);
          return map;
       }
@@ -673,6 +701,8 @@ svga_texture_transfer_unmap(struct pipe_context *pipe,
          ret = update_image_vgpu9(svga, surf, &box, st->slice, transfer->level);
       }
 
+      svga->hud.num_resource_updates++;
+
       assert(ret == PIPE_OK);
       (void) ret;
    }
@@ -744,9 +774,13 @@ svga_texture_create(struct pipe_screen *screen,
    tex->rendered_to = CALLOC(template->depth0 * template->array_size,
                              sizeof(tex->rendered_to[0]));
    if (!tex->rendered_to) {
-      FREE(tex->defined);
-      FREE(tex);
-      return NULL;
+      goto fail;
+   }
+
+   tex->dirty = CALLOC(template->depth0 * template->array_size,
+                             sizeof(tex->dirty[0]));
+   if (!tex->dirty) {
+      goto fail;
    }
 
    tex->b.b = *template;
@@ -805,6 +839,17 @@ svga_texture_create(struct pipe_screen *screen,
 
    tex->key.cachable = 1;
 
+   if ((bindings & (PIPE_BIND_RENDER_TARGET | PIPE_BIND_DEPTH_STENCIL)) &&
+       !(bindings & PIPE_BIND_SAMPLER_VIEW)) {
+      /* Also check if the format can be sampled from */
+      if (screen->is_format_supported(screen, template->format,
+                                      template->target,
+                                      template->nr_samples,
+                                      PIPE_BIND_SAMPLER_VIEW)) {
+         bindings |= PIPE_BIND_SAMPLER_VIEW;
+      }
+   }
+
    if (bindings & PIPE_BIND_SAMPLER_VIEW) {
       tex->key.flags |= SVGA3D_SURFACE_HINT_TEXTURE;
       tex->key.flags |= SVGA3D_SURFACE_BIND_SHADER_RESOURCE;
@@ -860,10 +905,7 @@ svga_texture_create(struct pipe_screen *screen,
    tex->key.format = svga_translate_format(svgascreen, template->format,
                                            bindings);
    if (tex->key.format == SVGA3D_FORMAT_INVALID) {
-      FREE(tex->defined);
-      FREE(tex->rendered_to);
-      FREE(tex);
-      return NULL;
+      goto fail;
    }
 
    /* Use typeless formats for sRGB and depth resources.  Typeless
@@ -888,10 +930,7 @@ svga_texture_create(struct pipe_screen *screen,
    tex->handle = svga_screen_surface_create(svgascreen, bindings,
                                             tex->b.b.usage, &tex->key);
    if (!tex->handle) {
-      FREE(tex->defined);
-      FREE(tex->rendered_to);
-      FREE(tex);
-      return NULL;
+      goto fail;
    }
 
    SVGA_DBG(DEBUG_DMA, "  --> got sid %p (texture)\n", tex->handle);
@@ -904,6 +943,16 @@ svga_texture_create(struct pipe_screen *screen,
    svgascreen->hud.num_resources++;
 
    return &tex->b.b;
+
+fail:
+   if (tex->dirty)
+      FREE(tex->dirty);
+   if (tex->rendered_to)
+      FREE(tex->rendered_to);
+   if (tex->defined)
+      FREE(tex->defined);
+   FREE(tex);
+   return NULL;
 }
 
 
@@ -981,9 +1030,89 @@ svga_texture_from_handle(struct pipe_screen *screen,
    tex->handle = srf;
 
    tex->rendered_to = CALLOC(1, sizeof(tex->rendered_to[0]));
+   if (!tex->rendered_to)
+      goto fail;
+
+   tex->dirty = CALLOC(1, sizeof(tex->dirty[0]));
+   if (!tex->dirty)
+      goto fail;
+
    tex->imported = TRUE;
 
    ss->hud.num_resources++;
 
    return &tex->b.b;
+
+fail:
+   if (tex->defined)
+      FREE(tex->defined);
+   if (tex->rendered_to)
+      FREE(tex->rendered_to);
+   if (tex->dirty)
+      FREE(tex->dirty);
+   FREE(tex);
+   return NULL;
+}
+
+boolean
+svga_texture_generate_mipmap(struct pipe_context *pipe,
+                             struct pipe_resource *pt,
+                             enum pipe_format format,
+                             unsigned base_level,
+                             unsigned last_level,
+                             unsigned first_layer,
+                             unsigned last_layer)
+{
+   struct pipe_sampler_view templ, *psv;
+   struct svga_pipe_sampler_view *sv;
+   struct svga_context *svga = svga_context(pipe);
+   struct svga_texture *tex = svga_texture(pt);
+   enum pipe_error ret;
+
+   assert(svga_have_vgpu10(svga));
+
+   /* Only support 2D texture for now */
+   if (pt->target != PIPE_TEXTURE_2D)
+      return FALSE;
+
+   /* Fallback to the mipmap generation utility for those formats that
+    * do not support hw generate mipmap
+    */
+   if (!svga_format_support_gen_mips(format))
+      return FALSE;
+
+   /* Make sure the texture surface was created with
+    * SVGA3D_SURFACE_BIND_RENDER_TARGET
+    */
+   if (!tex->handle || !(tex->key.flags & SVGA3D_SURFACE_BIND_RENDER_TARGET))
+      return FALSE;
+
+   templ.format = format;
+   templ.u.tex.first_layer = first_layer;
+   templ.u.tex.last_layer = last_layer;
+   templ.u.tex.first_level = base_level;
+   templ.u.tex.last_level = last_level;
+
+   psv = pipe->create_sampler_view(pipe, pt, &templ);
+   if (psv == NULL)
+      return FALSE;
+
+   sv = svga_pipe_sampler_view(psv);
+   ret = svga_validate_pipe_sampler_view(svga, sv);
+   if (ret != PIPE_OK) {
+      svga_context_flush(svga, NULL);
+      ret = svga_validate_pipe_sampler_view(svga, sv);
+      assert(ret == PIPE_OK);
+   }
+
+   ret = SVGA3D_vgpu10_GenMips(svga->swc, sv->id, tex->handle);
+   if (ret != PIPE_OK) {
+      svga_context_flush(svga, NULL);
+      ret = SVGA3D_vgpu10_GenMips(svga->swc, sv->id, tex->handle);
+   }
+   pipe_sampler_view_reference(&psv, NULL);
+
+   svga->hud.num_generate_mipmap++;
+
+   return TRUE;
 }