Merge branch 'mesa_7_7_branch'
[mesa.git] / src / gallium / drivers / svga / svga_screen_texture.c
index 8472dea04da5b3e7ed37f7234c7c837578418140..3700a80bf169d248e94f824ede8fbc9e2efee856 100644 (file)
@@ -158,7 +158,8 @@ svga_transfer_dma_band(struct svga_transfer *st,
                 st->base.x + st->base.width,
                 y + h,
                 st->base.zslice + 1,
-                texture->base.block.size*8/(texture->base.block.width*texture->base.block.height));
+                pf_get_blocksize(texture->base.format)*8/
+                (pf_get_blockwidth(texture->base.format)*pf_get_blockheight(texture->base.format)));
    
    box.x = st->base.x;
    box.y = y;
@@ -208,7 +209,8 @@ svga_transfer_dma(struct svga_transfer *st,
    }
    else {
       unsigned y, h, srcy;
-      h = st->hw_nblocksy * st->base.block.height;
+      unsigned blockheight = pf_get_blockheight(st->base.texture->format);
+      h = st->hw_nblocksy * blockheight;
       srcy = 0;
       for(y = 0; y < st->base.height; y += h) {
          unsigned offset, length;
@@ -218,11 +220,11 @@ svga_transfer_dma(struct svga_transfer *st,
             h = st->base.height - y;
 
          /* Transfer band must be aligned to pixel block boundaries */
-         assert(y % st->base.block.height == 0);
-         assert(h % st->base.block.height == 0);
+         assert(y % blockheight == 0);
+         assert(h % blockheight == 0);
          
-         offset = y * st->base.stride / st->base.block.height;
-         length = h * st->base.stride / st->base.block.height;
+         offset = y * st->base.stride / blockheight;
+         length = h * st->base.stride / blockheight;
 
          sw = (uint8_t *)st->swbuf + offset;
          
@@ -266,14 +268,8 @@ svga_texture_create(struct pipe_screen *screen,
                     const struct pipe_texture *templat)
 {
    struct svga_screen *svgascreen = svga_screen(screen);
-   struct svga_winsys_screen *sws = svgascreen->sws;
    struct svga_texture *tex = CALLOC_STRUCT(svga_texture);
    unsigned width, height, depth;
-   SVGA3dSurfaceFlags flags = 0;
-   SVGA3dSurfaceFormat format;
-   SVGA3dSize size;
-   uint32 numFaces;
-   uint32 numMipLevels;
    unsigned level;
    
    if (!tex)
@@ -287,37 +283,33 @@ svga_texture_create(struct pipe_screen *screen,
    if(templat->last_level >= SVGA_MAX_TEXTURE_LEVELS)
       goto error2;
    
-   width = templat->width[0];
-   height = templat->height[0];
-   depth = templat->depth[0];
+   width = templat->width0;
+   height = templat->height0;
+   depth = templat->depth0;
    for(level = 0; level <= templat->last_level; ++level) {
-      tex->base.width[level] = width;
-      tex->base.height[level] = height;
-      tex->base.depth[level] = depth;
-      tex->base.nblocksx[level] = pf_get_nblocksx(&tex->base.block, width);  
-      tex->base.nblocksy[level] = pf_get_nblocksy(&tex->base.block, height);  
-      width  = minify(width);
-      height = minify(height);
-      depth = minify(depth);
+      width = u_minify(width, 1);
+      height = u_minify(height, 1);
+      depth = u_minify(depth, 1);
    }
    
-   size.width = templat->width[0];
-   size.height = templat->height[0];
-   size.depth = templat->depth[0];
+   tex->key.flags = 0;
+   tex->key.size.width = templat->width0;
+   tex->key.size.height = templat->height0;
+   tex->key.size.depth = templat->depth0;
    
    if(templat->target == PIPE_TEXTURE_CUBE) {
-      flags |= SVGA3D_SURFACE_CUBEMAP;
-      numFaces = 6;
+      tex->key.flags |= SVGA3D_SURFACE_CUBEMAP;
+      tex->key.numFaces = 6;
    }
    else {
-      numFaces = 1;
+      tex->key.numFaces = 1;
    }
 
    if(templat->tex_usage & PIPE_TEXTURE_USAGE_SAMPLER)
-      flags |= SVGA3D_SURFACE_HINT_TEXTURE;
+      tex->key.flags |= SVGA3D_SURFACE_HINT_TEXTURE;
 
    if(templat->tex_usage & PIPE_TEXTURE_USAGE_PRIMARY)
-      flags |= SVGA3D_SURFACE_HINT_SCANOUT;
+      tex->key.flags |= SVGA3D_SURFACE_HINT_SCANOUT;
    
    /* 
     * XXX: Never pass the SVGA3D_SURFACE_HINT_RENDERTARGET hint. Mesa cannot
@@ -328,21 +320,24 @@ svga_texture_create(struct pipe_screen *screen,
 #if 0
    if((templat->tex_usage & PIPE_TEXTURE_USAGE_RENDER_TARGET) &&
       !pf_is_compressed(templat->format))
-      flags |= SVGA3D_SURFACE_HINT_RENDERTARGET;
+      tex->key.flags |= SVGA3D_SURFACE_HINT_RENDERTARGET;
 #endif
    
    if(templat->tex_usage & PIPE_TEXTURE_USAGE_DEPTH_STENCIL)
-      flags |= SVGA3D_SURFACE_HINT_DEPTHSTENCIL;
+      tex->key.flags |= SVGA3D_SURFACE_HINT_DEPTHSTENCIL;
    
-   numMipLevels = templat->last_level + 1;
+   tex->key.numMipLevels = templat->last_level + 1;
    
-   format = svga_translate_format(templat->format);
-   if(format == SVGA3D_FORMAT_INVALID)
+   tex->key.format = svga_translate_format(templat->format);
+   if(tex->key.format == SVGA3D_FORMAT_INVALID)
       goto error2;
+
+   tex->key.cachable = 1;
    
-   tex->handle = sws->surface_create(sws, flags, format, size, numFaces, numMipLevels);
+   SVGA_DBG(DEBUG_DMA, "surface_create for texture\n", tex->handle);
+   tex->handle = svga_screen_surface_create(svgascreen, &tex->key);
    if (tex->handle)
-      SVGA_DBG(DEBUG_DMA, "create sid %p (texture)\n", tex->handle);
+      SVGA_DBG(DEBUG_DMA, "  --> got sid %p (texture)\n", tex->handle);
 
    return &tex->base;
 
@@ -367,7 +362,7 @@ svga_texture_blanket(struct pipe_screen * screen,
    /* Only supports one type */
    if (base->target != PIPE_TEXTURE_2D ||
        base->last_level != 0 ||
-       base->depth[0] != 1) {
+       base->depth0 != 1) {
       return NULL;
    }
 
@@ -398,6 +393,7 @@ svga_texture_blanket(struct pipe_screen * screen,
       return NULL;
 
    tex->base = *base;
+   
 
    if (sbuf->key.format == 1)
       tex->base.format = PIPE_FORMAT_X8R8G8B8_UNORM;
@@ -407,6 +403,12 @@ svga_texture_blanket(struct pipe_screen * screen,
    pipe_reference_init(&tex->base.reference, 1);
    tex->base.screen = screen;
 
+   SVGA_DBG(DEBUG_DMA, "blanket sid %p\n", sbuf->handle);
+
+   /* We don't own this storage, so don't try to cache it.
+    */
+   assert(sbuf->key.cachable == 0);
+   tex->key.cachable = 0;
    sws->surface_reference(sws, &tex->handle, sbuf->handle);
 
    return &tex->base;
@@ -427,7 +429,7 @@ svga_texture_destroy(struct pipe_texture *pt)
      DBG("%s deleting %p\n", __FUNCTION__, (void *) tex);
    */
    SVGA_DBG(DEBUG_DMA, "unref sid %p (texture)\n", tex->handle);
-   ss->sws->surface_reference(ss->sws, &tex->handle, NULL);
+   svga_screen_surface_destroy(ss, &tex->key, &tex->handle);
 
    FREE(tex);
 }
@@ -518,43 +520,47 @@ svga_texture_view_surface(struct pipe_context *pipe,
                           unsigned start_mip,
                           unsigned num_mip,
                           int face_pick,
-                          int zslice_pick)
+                          int zslice_pick,
+                          struct svga_host_surface_cache_key *key) /* OUT */
 {
    struct svga_screen *ss = svga_screen(tex->base.screen);
-   struct svga_winsys_screen *sws = ss->sws;
    struct svga_winsys_surface *handle;
-   int i, j;
-   SVGA3dSurfaceFlags flags = 0;
-   SVGA3dSize size;
-   uint32 numFaces;
-   uint32 numMipLevels = num_mip;
+   uint32_t i, j;
    unsigned z_offset = 0;
 
    SVGA_DBG(DEBUG_PERF, 
             "svga: Create surface view: face %d zslice %d mips %d..%d\n",
             face_pick, zslice_pick, start_mip, start_mip+num_mip-1);
 
-   size.width = tex->base.width[start_mip];
-   size.height = tex->base.height[start_mip];
-   size.depth = zslice_pick < 0 ? tex->base.depth[start_mip] : 1;
-   assert(size.depth == 1);
+   key->flags = 0;
+   key->format = format;
+   key->numMipLevels = num_mip;
+   key->size.width = u_minify(tex->base.width0, start_mip);
+   key->size.height = u_minify(tex->base.height0, start_mip);
+   key->size.depth = zslice_pick < 0 ? u_minify(tex->base.depth0, start_mip) : 1;
+   key->cachable = 1;
+   assert(key->size.depth == 1);
    
    if(tex->base.target == PIPE_TEXTURE_CUBE && face_pick < 0) {
-      flags |= SVGA3D_SURFACE_CUBEMAP;
-      numFaces = 6;
+      key->flags |= SVGA3D_SURFACE_CUBEMAP;
+      key->numFaces = 6;
    } else {
-      numFaces = 1;
+      key->numFaces = 1;
    }
 
-   if(format == SVGA3D_FORMAT_INVALID)
+   if(key->format == SVGA3D_FORMAT_INVALID) {
+      key->cachable = 0;
       return NULL;
+   }
 
-   handle = sws->surface_create(sws, flags, format, size, numFaces, numMipLevels);
-
-   if (!handle)
+   SVGA_DBG(DEBUG_DMA, "surface_create for texture view\n");
+   handle = svga_screen_surface_create(ss, key);
+   if (!handle) {
+      key->cachable = 0;
       return NULL;
+   }
 
-   SVGA_DBG(DEBUG_DMA, "create sid %p (texture view)\n", handle);
+   SVGA_DBG(DEBUG_DMA, " --> got sid %p (texture view)\n", handle);
 
    if (face_pick < 0)
       face_pick = 0;
@@ -562,14 +568,23 @@ svga_texture_view_surface(struct pipe_context *pipe,
    if (zslice_pick >= 0)
        z_offset = zslice_pick;
 
-   for (i = 0; i < num_mip; i++) {
-      for (j = 0; j < numFaces; j++) {
+   for (i = 0; i < key->numMipLevels; i++) {
+      for (j = 0; j < key->numFaces; j++) {
          if(tex->defined[j + face_pick][i + start_mip]) {
-            unsigned depth = zslice_pick < 0 ? tex->base.depth[i + start_mip] : 1;
-            svga_texture_copy_handle(svga_context(pipe), ss,
-                                     tex->handle, 0, 0, z_offset, i + start_mip, j + face_pick,
+            unsigned depth = (zslice_pick < 0 ?
+                              u_minify(tex->base.depth0, i + start_mip) :
+                              1);
+
+            svga_texture_copy_handle(svga_context(pipe),
+                                     ss,
+                                     tex->handle, 
+                                     0, 0, z_offset, 
+                                     i + start_mip, 
+                                     j + face_pick,
                                      handle, 0, 0, 0, i, j,
-                                     tex->base.width[i + start_mip], tex->base.height[i + start_mip], depth);
+                                     u_minify(tex->base.width0, i + start_mip),
+                                     u_minify(tex->base.height0, i + start_mip),
+                                     depth);
          }
       }
    }
@@ -586,25 +601,23 @@ svga_get_tex_surface(struct pipe_screen *screen,
 {
    struct svga_texture *tex = svga_texture(pt);
    struct svga_surface *s;
-   struct pipe_surface *ps;
    boolean render = flags & PIPE_BUFFER_USAGE_GPU_WRITE ? TRUE : FALSE;
    boolean view = FALSE;
    SVGA3dSurfaceFormat format;
 
    s = CALLOC_STRUCT(svga_surface);
-   ps = &s->base;
-   if (!ps)
+   if (!s)
       return NULL;
 
-   pipe_reference_init(&ps->reference, 1);
-   pipe_texture_reference(&ps->texture, pt);
-   ps->format = pt->format;
-   ps->width = pt->width[level];
-   ps->height = pt->height[level];
-   ps->usage = flags;
-   ps->level = level;
-   ps->face = face;
-   ps->zslice = zslice;
+   pipe_reference_init(&s->base.reference, 1);
+   pipe_texture_reference(&s->base.texture, pt);
+   s->base.format = pt->format;
+   s->base.width = u_minify(pt->width0, level);
+   s->base.height = u_minify(pt->height0, level);
+   s->base.usage = flags;
+   s->base.level = level;
+   s->base.face = face;
+   s->base.zslice = zslice;
 
    if (!render)
       format = svga_translate_format(pt->format);
@@ -619,11 +632,13 @@ svga_get_tex_surface(struct pipe_screen *screen,
       view = TRUE;
 
    /* Currently only used for compressed textures */
-   if (render && (format != svga_translate_format(pt->format))) {
+   if (render && 
+       format != svga_translate_format(pt->format)) {
       view = TRUE;
    }
 
-   if (level != 0 && svga_screen(screen)->debug.force_level_surface_view)
+   if (level != 0 && 
+       svga_screen(screen)->debug.force_level_surface_view)
       view = TRUE;
 
    if (pt->target == PIPE_TEXTURE_3D)
@@ -634,25 +649,25 @@ svga_get_tex_surface(struct pipe_screen *screen,
 
    if (view) {
       SVGA_DBG(DEBUG_VIEWS, "svga: Surface view: yes %p, level %u face %u z %u, %p\n",
-               pt, level, face, zslice, ps);
+               pt, level, face, zslice, s);
 
-      s->handle = svga_texture_view_surface(NULL, tex, format, level, 1, face, zslice);
+      s->handle = svga_texture_view_surface(NULL, tex, format, level, 1, face, zslice,
+                                            &s->key);
       s->real_face = 0;
       s->real_level = 0;
       s->real_zslice = 0;
    } else {
-      struct svga_winsys_screen *sws = svga_winsys_screen(screen);
-
       SVGA_DBG(DEBUG_VIEWS, "svga: Surface view: no %p, level %u, face %u, z %u, %p\n",
-               pt, level, face, zslice, ps);
+               pt, level, face, zslice, s);
 
-      sws->surface_reference(sws, &s->handle, tex->handle);
+      memset(&s->key, 0, sizeof s->key);
+      s->handle = tex->handle;
       s->real_face = face;
       s->real_level = level;
       s->real_zslice = zslice;
    }
 
-   return ps;
+   return &s->base;
 }
 
 
@@ -660,10 +675,14 @@ static void
 svga_tex_surface_destroy(struct pipe_surface *surf)
 {
    struct svga_surface *s = svga_surface(surf);
+   struct svga_texture *t = svga_texture(surf->texture);
    struct svga_screen *ss = svga_screen(surf->texture->screen);
 
-   SVGA_DBG(DEBUG_DMA, "unref sid %p (tex surface)\n", s->handle);
-   ss->sws->surface_reference(ss->sws, &s->handle, NULL);
+   if(s->handle != t->handle) {
+      SVGA_DBG(DEBUG_DMA, "unref sid %p (tex surface)\n", s->handle);
+      svga_screen_surface_destroy(ss, &s->key, &s->handle);
+   }
+
    pipe_texture_reference(&surf->texture, NULL);
    FREE(surf);
 }
@@ -723,7 +742,8 @@ svga_propagate_surface(struct pipe_context *pipe, struct pipe_surface *surf)
       svga_texture_copy_handle(svga_context(pipe), ss,
                                s->handle, 0, 0, 0, s->real_level, s->real_face,
                                tex->handle, 0, 0, surf->zslice, surf->level, surf->face,
-                               tex->base.width[surf->level], tex->base.height[surf->level], 1);
+                               u_minify(tex->base.width0, surf->level),
+                               u_minify(tex->base.height0, surf->level), 1);
       tex->defined[surf->face][surf->level] = TRUE;
    }
 }
@@ -751,6 +771,8 @@ svga_get_tex_transfer(struct pipe_screen *screen,
    struct svga_screen *ss = svga_screen(screen);
    struct svga_winsys_screen *sws = ss->sws;
    struct svga_transfer *st;
+   unsigned nblocksx = pf_get_nblocksx(texture->format, w);
+   unsigned nblocksy = pf_get_nblocksy(texture->format, h);
 
    /* We can't map texture storage directly */
    if (usage & PIPE_TRANSFER_MAP_DIRECTLY)
@@ -760,21 +782,17 @@ svga_get_tex_transfer(struct pipe_screen *screen,
    if (!st)
       return NULL;
    
-   st->base.format = texture->format;
-   st->base.block = texture->block;
    st->base.x = x;
    st->base.y = y;
    st->base.width = w;
    st->base.height = h;
-   st->base.nblocksx = pf_get_nblocksx(&texture->block, w);
-   st->base.nblocksy = pf_get_nblocksy(&texture->block, h);
-   st->base.stride = st->base.nblocksx*st->base.block.size;
+   st->base.stride = nblocksx*pf_get_blocksize(texture->format);
    st->base.usage = usage;
    st->base.face = face;
    st->base.level = level;
    st->base.zslice = zslice;
 
-   st->hw_nblocksy = st->base.nblocksy;
+   st->hw_nblocksy = nblocksy;
    
    st->hwbuf = svga_winsys_buffer_create(ss, 
                                          1, 
@@ -790,15 +808,15 @@ svga_get_tex_transfer(struct pipe_screen *screen,
    if(!st->hwbuf)
       goto no_hwbuf;
 
-   if(st->hw_nblocksy < st->base.nblocksy) {
+   if(st->hw_nblocksy < nblocksy) {
       /* We couldn't allocate a hardware buffer big enough for the transfer, 
        * so allocate regular malloc memory instead */
       debug_printf("%s: failed to allocate %u KB of DMA, splitting into %u x %u KB DMA transfers\n",
                    __FUNCTION__,
-                   (st->base.nblocksy*st->base.stride + 1023)/1024,
-                   (st->base.nblocksy + st->hw_nblocksy - 1)/st->hw_nblocksy,
+                   (nblocksy*st->base.stride + 1023)/1024,
+                   (nblocksy + st->hw_nblocksy - 1)/st->hw_nblocksy,
                    (st->hw_nblocksy*st->base.stride + 1023)/1024);
-      st->swbuf = MALLOC(st->base.nblocksy*st->base.stride);
+      st->swbuf = MALLOC(nblocksy*st->base.stride);
       if(!st->swbuf)
          goto no_swbuf;
    }
@@ -892,7 +910,6 @@ svga_get_tex_sampler_view(struct pipe_context *pipe, struct pipe_texture *pt,
                           unsigned min_lod, unsigned max_lod)
 {
    struct svga_screen *ss = svga_screen(pt->screen);
-   struct svga_winsys_screen *sws = ss->sws;
    struct svga_texture *tex = svga_texture(pt); 
    struct svga_sampler_view *sv = NULL;
    SVGA3dSurfaceFormat format = svga_translate_format(pt->format);
@@ -943,7 +960,7 @@ svga_get_tex_sampler_view(struct pipe_context *pipe, struct pipe_texture *pt,
 
    sv = CALLOC_STRUCT(svga_sampler_view);
    pipe_reference_init(&sv->reference, 1);
-   sv->texture = tex;
+   pipe_texture_reference(&sv->texture, pt);
    sv->min_lod = min_lod;
    sv->max_lod = max_lod;
 
@@ -953,11 +970,12 @@ svga_get_tex_sampler_view(struct pipe_context *pipe, struct pipe_texture *pt,
                "svga: Sampler view: no %p, mips %u..%u, nr %u, size (%ux%ux%u), last %u\n",
                pt, min_lod, max_lod,
                max_lod - min_lod + 1,
-               pt->width[0],
-               pt->height[0],
-               pt->depth[0],
+               pt->width0,
+               pt->height0,
+               pt->depth0,
                pt->last_level);
-      sws->surface_reference(sws, &sv->handle, tex->handle);
+      sv->key.cachable = 0;
+      sv->handle = tex->handle;
       return sv;
    }
 
@@ -965,20 +983,22 @@ svga_get_tex_sampler_view(struct pipe_context *pipe, struct pipe_texture *pt,
             "svga: Sampler view: yes %p, mips %u..%u, nr %u, size (%ux%ux%u), last %u\n",
             pt, min_lod, max_lod,
             max_lod - min_lod + 1,
-            pt->width[0],
-            pt->height[0],
-            pt->depth[0],
+            pt->width0,
+            pt->height0,
+            pt->depth0,
             pt->last_level);
 
    sv->age = tex->age;
    sv->handle = svga_texture_view_surface(pipe, tex, format,
                                           min_lod,
                                           max_lod - min_lod + 1,
-                                          -1, -1);
+                                          -1, -1,
+                                          &sv->key);
 
    if (!sv->handle) {
       assert(0);
-      sws->surface_reference(sws, &sv->handle, tex->handle);
+      sv->key.cachable = 0;
+      sv->handle = tex->handle;
       return sv;
    }
 
@@ -992,14 +1012,14 @@ svga_get_tex_sampler_view(struct pipe_context *pipe, struct pipe_texture *pt,
 void
 svga_validate_sampler_view(struct svga_context *svga, struct svga_sampler_view *v)
 {
-   struct svga_texture *tex = v->texture;
+   struct svga_texture *tex = svga_texture(v->texture);
    unsigned numFaces;
    unsigned age = 0;
    int i, k;
 
    assert(svga);
 
-   if (v->handle == v->texture->handle)
+   if (v->handle == tex->handle)
       return;
 
    age = tex->age;
@@ -1015,9 +1035,9 @@ svga_validate_sampler_view(struct svga_context *svga, struct svga_sampler_view *
             svga_texture_copy_handle(svga, NULL,
                                      tex->handle, 0, 0, 0, i, k,
                                      v->handle, 0, 0, 0, i - v->min_lod, k,
-                                     tex->base.width[i],
-                                     tex->base.height[i],
-                                     tex->base.depth[i]);
+                                     u_minify(tex->base.width0, i),
+                                     u_minify(tex->base.height0, i),
+                                     u_minify(tex->base.depth0, i));
       }
    }
 
@@ -1027,11 +1047,14 @@ svga_validate_sampler_view(struct svga_context *svga, struct svga_sampler_view *
 void
 svga_destroy_sampler_view_priv(struct svga_sampler_view *v)
 {
-   struct svga_screen *ss = svga_screen(v->texture->base.screen);
-
-   SVGA_DBG(DEBUG_DMA, "unref sid %p (sampler view)\n", v->handle);
-   ss->sws->surface_reference(ss->sws, &v->handle, NULL);
+   struct svga_texture *tex = svga_texture(v->texture);
 
+   if(v->handle != tex->handle) {
+      struct svga_screen *ss = svga_screen(v->texture->screen);
+      SVGA_DBG(DEBUG_DMA, "unref sid %p (sampler view)\n", v->handle);
+      svga_screen_surface_destroy(ss, &v->key, &v->handle);
+   }
+   pipe_texture_reference(&v->texture, NULL);
    FREE(v);
 }
 
@@ -1047,8 +1070,7 @@ svga_screen_buffer_from_texture(struct pipe_texture *texture,
        svga_translate_format(texture->format),
        stex->handle);
 
-   *stride = pf_get_nblocksx(&texture->block, texture->width[0]) *
-      texture->block.size;
+   *stride = pf_get_stride(texture->format, texture->width0);
 
    return *buffer != NULL;
 }
@@ -1060,6 +1082,8 @@ svga_screen_texture_get_winsys_surface(struct pipe_texture *texture)
    struct svga_winsys_screen *sws = svga_winsys_screen(texture->screen);
    struct svga_winsys_surface *vsurf = NULL;
 
+   assert(svga_texture(texture)->key.cachable == 0);
+   svga_texture(texture)->key.cachable = 0;
    sws->surface_reference(sws, &vsurf, svga_texture(texture)->handle);
    return vsurf;
 }