util: Move gallium's PIPE_FORMAT utils to /util/format/
[mesa.git] / src / gallium / drivers / llvmpipe / lp_texture.c
index e895485e9476264bcf25e03295aa92a8d4637fc8..0d5c4ac544d112aa54bcfa711d2fcd1508894f73 100644 (file)
@@ -1,6 +1,6 @@
 /**************************************************************************
  * 
- * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
+ * Copyright 2006 VMware, Inc.
  * All Rights Reserved.
  * 
  * Permission is hereby granted, free of charge, to any person obtaining a
@@ -18,7 +18,7 @@
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
- * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -26,8 +26,8 @@
  **************************************************************************/
  /*
   * Authors:
-  *   Keith Whitwell <keith@tungstengraphics.com>
-  *   Michel Dänzer <michel@tungstengraphics.com>
+  *   Keith Whitwell <keithw@vmware.com>
+  *   Michel Dänzer <daenzer@vmware.com>
   */
 
 #include <stdio.h>
 
 #include "util/u_inlines.h"
 #include "util/u_cpu_detect.h"
-#include "util/u_format.h"
+#include "util/format/u_format.h"
 #include "util/u_math.h"
 #include "util/u_memory.h"
-#include "util/u_simple_list.h"
+#include "util/simple_list.h"
 #include "util/u_transfer.h"
 
 #include "lp_context.h"
 #include "lp_flush.h"
 #include "lp_screen.h"
-#include "lp_tile_image.h"
 #include "lp_texture.h"
 #include "lp_setup.h"
 #include "lp_state.h"
+#include "lp_rast.h"
 
 #include "state_tracker/sw_winsys.h"
 
@@ -60,112 +60,104 @@ static struct llvmpipe_resource resource_list;
 static unsigned id_counter = 0;
 
 
-static INLINE boolean
-resource_is_texture(const struct pipe_resource *resource)
-{
-   switch (resource->target) {
-   case PIPE_BUFFER:
-      return FALSE;
-   case PIPE_TEXTURE_1D:
-   case PIPE_TEXTURE_2D:
-   case PIPE_TEXTURE_RECT:
-   case PIPE_TEXTURE_3D:
-   case PIPE_TEXTURE_CUBE:
-      return TRUE;
-   default:
-      assert(0);
-      return FALSE;
-   }
-}
-
-
-
-/**
- * Allocate storage for llvmpipe_texture::layout array.
- * The number of elements is width_in_tiles * height_in_tiles.
- */
-static enum lp_texture_layout *
-alloc_layout_array(unsigned num_slices, unsigned width, unsigned height)
-{
-   const unsigned tx = align(width, TILE_SIZE) / TILE_SIZE;
-   const unsigned ty = align(height, TILE_SIZE) / TILE_SIZE;
-
-   assert(num_slices * tx * ty > 0);
-   assert(LP_TEX_LAYOUT_NONE == 0); /* calloc'ing LP_TEX_LAYOUT_NONE here */
-
-   return (enum lp_texture_layout *)
-      CALLOC(num_slices * tx * ty, sizeof(enum lp_texture_layout));
-}
-
-
-
 /**
  * Conventional allocation path for non-display textures:
- * Just compute row strides here.  Storage is allocated on demand later.
+ * Compute strides and allocate data (unless asked not to).
  */
 static boolean
 llvmpipe_texture_layout(struct llvmpipe_screen *screen,
-                        struct llvmpipe_resource *lpr)
+                        struct llvmpipe_resource *lpr,
+                        boolean allocate)
 {
    struct pipe_resource *pt = &lpr->base;
    unsigned level;
    unsigned width = pt->width0;
    unsigned height = pt->height0;
    unsigned depth = pt->depth0;
+   uint64_t total_size = 0;
+   unsigned layers = pt->array_size;
+   /* XXX:
+    * This alignment here (same for displaytarget) was added for the purpose of
+    * ARB_map_buffer_alignment. I am not convinced it's needed for non-buffer
+    * resources. Otherwise we'd want the max of cacheline size and 16 (max size
+    * of a block for all formats) though this should not be strictly necessary
+    * neither. In any case it can only affect compressed or 1d textures.
+    */
+   unsigned mip_align = MAX2(64, util_cpu_caps.cacheline);
 
    assert(LP_MAX_TEXTURE_2D_LEVELS <= LP_MAX_TEXTURE_LEVELS);
    assert(LP_MAX_TEXTURE_3D_LEVELS <= LP_MAX_TEXTURE_LEVELS);
 
    for (level = 0; level <= pt->last_level; level++) {
-
-      /* Row stride and image stride (for linear layout) */
-      {
-         unsigned alignment, nblocksx, nblocksy, block_size;
-
-         /* For non-compressed formats we need to align the texture size
-          * to the tile size to facilitate render-to-texture.
-          */
-         if (util_format_is_compressed(pt->format))
-            alignment = 1;
+      uint64_t mipsize;
+      unsigned align_x, align_y, nblocksx, nblocksy, block_size, num_slices;
+
+      /* Row stride and image stride */
+
+      /* For non-compressed formats we need 4x4 pixel alignment
+       * so we can read/write LP_RASTER_BLOCK_SIZE when rendering to them.
+       * We also want cache line size in x direction,
+       * otherwise same cache line could end up in multiple threads.
+       * For explicit 1d resources however we reduce this to 4x1 and
+       * handle specially in render output code (as we need to do special
+       * handling there for buffers in any case).
+       */
+      if (util_format_is_compressed(pt->format))
+         align_x = align_y = 1;
+      else {
+         align_x = LP_RASTER_BLOCK_SIZE;
+         if (llvmpipe_resource_is_1d(&lpr->base))
+            align_y = 1;
          else
-            alignment = TILE_SIZE;
+            align_y = LP_RASTER_BLOCK_SIZE;
+      }
 
-         nblocksx = util_format_get_nblocksx(pt->format,
-                                             align(width, alignment));
-         nblocksy = util_format_get_nblocksy(pt->format,
-                                             align(height, alignment));
-         block_size = util_format_get_blocksize(pt->format);
+      nblocksx = util_format_get_nblocksx(pt->format,
+                                          align(width, align_x));
+      nblocksy = util_format_get_nblocksy(pt->format,
+                                          align(height, align_y));
+      block_size = util_format_get_blocksize(pt->format);
 
-         lpr->row_stride[level] = align(nblocksx * block_size, 16);
+      if (util_format_is_compressed(pt->format))
+         lpr->row_stride[level] = nblocksx * block_size;
+      else
+         lpr->row_stride[level] = align(nblocksx * block_size, util_cpu_caps.cacheline);
 
-         lpr->img_stride[level] = lpr->row_stride[level] * nblocksy;
+      /* if row_stride * height > LP_MAX_TEXTURE_SIZE */
+      if ((uint64_t)lpr->row_stride[level] * nblocksy > LP_MAX_TEXTURE_SIZE) {
+         /* image too large */
+         goto fail;
       }
 
-      /* Size of the image in tiles (for tiled layout) */
-      {
-         const unsigned width_t = align(width, TILE_SIZE) / TILE_SIZE;
-         const unsigned height_t = align(height, TILE_SIZE) / TILE_SIZE;
-         lpr->tiles_per_row[level] = width_t;
-         lpr->tiles_per_image[level] = width_t * height_t;
-      }
+      lpr->img_stride[level] = lpr->row_stride[level] * nblocksy;
 
-      /* Number of 3D image slices or cube faces */
-      {
-         unsigned num_slices;
+      /* Number of 3D image slices, cube faces or texture array layers */
+      if (lpr->base.target == PIPE_TEXTURE_CUBE) {
+         assert(layers == 6);
+      }
 
-         if (lpr->base.target == PIPE_TEXTURE_CUBE)
-            num_slices = 6;
-         else if (lpr->base.target == PIPE_TEXTURE_3D)
-            num_slices = depth;
-         else
-            num_slices = 1;
+      if (lpr->base.target == PIPE_TEXTURE_3D)
+         num_slices = depth;
+      else if (lpr->base.target == PIPE_TEXTURE_1D_ARRAY ||
+               lpr->base.target == PIPE_TEXTURE_2D_ARRAY ||
+               lpr->base.target == PIPE_TEXTURE_CUBE ||
+               lpr->base.target == PIPE_TEXTURE_CUBE_ARRAY)
+         num_slices = layers;
+      else
+         num_slices = 1;
+
+      /* if img_stride * num_slices_faces > LP_MAX_TEXTURE_SIZE */
+      mipsize = (uint64_t)lpr->img_stride[level] * num_slices;
+      if (mipsize > LP_MAX_TEXTURE_SIZE) {
+         /* volume too large */
+         goto fail;
+      }
 
-         lpr->num_slices_faces[level] = num_slices;
+      lpr->mip_offsets[level] = total_size;
 
-         lpr->layout[level] = alloc_layout_array(num_slices, width, height);
-         if (!lpr->layout[level]) {
-            goto fail;
-         }
+      total_size += align((unsigned)mipsize, mip_align);
+      if (total_size > LP_MAX_TEXTURE_SIZE) {
+         goto fail;
       }
 
       /* Compute size of next mipmap level */
@@ -174,55 +166,63 @@ llvmpipe_texture_layout(struct llvmpipe_screen *screen,
       depth = u_minify(depth, 1);
    }
 
-   return TRUE;
-
-fail:
-   for (level = 0; level <= pt->last_level; level++) {
-      if (lpr->layout[level]) {
-         FREE(lpr->layout[level]);
+   if (allocate) {
+      lpr->tex_data = align_malloc(total_size, mip_align);
+      if (!lpr->tex_data) {
+         return FALSE;
+      }
+      else {
+         memset(lpr->tex_data, 0, total_size);
       }
    }
 
+   return TRUE;
+
+fail:
    return FALSE;
 }
 
 
+/**
+ * Check the size of the texture specified by 'res'.
+ * \return TRUE if OK, FALSE if too large.
+ */
+static bool
+llvmpipe_can_create_resource(struct pipe_screen *screen,
+                             const struct pipe_resource *res)
+{
+   struct llvmpipe_resource lpr;
+   memset(&lpr, 0, sizeof(lpr));
+   lpr.base = *res;
+   return llvmpipe_texture_layout(llvmpipe_screen(screen), &lpr, false);
+}
+
 
 static boolean
 llvmpipe_displaytarget_layout(struct llvmpipe_screen *screen,
-                              struct llvmpipe_resource *lpr)
+                              struct llvmpipe_resource *lpr,
+                              const void *map_front_private)
 {
    struct sw_winsys *winsys = screen->winsys;
 
    /* Round up the surface size to a multiple of the tile size to
     * avoid tile clipping.
     */
-   const unsigned width = align(lpr->base.width0, TILE_SIZE);
-   const unsigned height = align(lpr->base.height0, TILE_SIZE);
-   const unsigned width_t = width / TILE_SIZE;
-   const unsigned height_t = height / TILE_SIZE;
-
-   lpr->tiles_per_row[0] = width_t;
-   lpr->tiles_per_image[0] = width_t * height_t;
-   lpr->num_slices_faces[0] = 1;
-   lpr->img_stride[0] = 0;
-
-   lpr->layout[0] = alloc_layout_array(1, width, height);
-   if (!lpr->layout[0]) {
-      return FALSE;
-   }
+   const unsigned width = MAX2(1, align(lpr->base.width0, TILE_SIZE));
+   const unsigned height = MAX2(1, align(lpr->base.height0, TILE_SIZE));
 
    lpr->dt = winsys->displaytarget_create(winsys,
                                           lpr->base.bind,
                                           lpr->base.format,
                                           width, height,
-                                          16,
+                                          64,
+                                          map_front_private,
                                           &lpr->row_stride[0] );
 
    if (lpr->dt == NULL)
       return FALSE;
 
-   {
+   if (!map_front_private) {
       void *map = winsys->displaytarget_map(winsys, lpr->dt,
                                             PIPE_TRANSFER_WRITE);
 
@@ -237,8 +237,9 @@ llvmpipe_displaytarget_layout(struct llvmpipe_screen *screen,
 
 
 static struct pipe_resource *
-llvmpipe_resource_create(struct pipe_screen *_screen,
-                         const struct pipe_resource *templat)
+llvmpipe_resource_create_front(struct pipe_screen *_screen,
+                               const struct pipe_resource *templat,
+                               const void *map_front_private)
 {
    struct llvmpipe_screen *screen = llvmpipe_screen(_screen);
    struct llvmpipe_resource *lpr = CALLOC_STRUCT(llvmpipe_resource);
@@ -251,31 +252,40 @@ llvmpipe_resource_create(struct pipe_screen *_screen,
 
    /* assert(lpr->base.bind); */
 
-   if (resource_is_texture(&lpr->base)) {
-      if (lpr->base.bind & PIPE_BIND_DISPLAY_TARGET) {
+   if (llvmpipe_resource_is_texture(&lpr->base)) {
+      if (lpr->base.bind & (PIPE_BIND_DISPLAY_TARGET |
+                            PIPE_BIND_SCANOUT |
+                            PIPE_BIND_SHARED)) {
          /* displayable surface */
-         if (!llvmpipe_displaytarget_layout(screen, lpr))
+         if (!llvmpipe_displaytarget_layout(screen, lpr, map_front_private))
             goto fail;
-         assert(lpr->layout[0][0] == LP_TEX_LAYOUT_NONE);
       }
       else {
          /* texture map */
-         if (!llvmpipe_texture_layout(screen, lpr))
+         if (!llvmpipe_texture_layout(screen, lpr, true))
             goto fail;
-         assert(lpr->layout[0][0] == LP_TEX_LAYOUT_NONE);
       }
-      assert(lpr->layout[0]);
    }
    else {
       /* other data (vertex buffer, const buffer, etc) */
-      const enum pipe_format format = templat->format;
-      const uint w = templat->width0 / util_format_get_blockheight(format);
-      /* XXX buffers should only have one dimension, those values should be 1 */
-      const uint h = templat->height0 / util_format_get_blockwidth(format);
-      const uint d = templat->depth0;
-      const uint bpp = util_format_get_blocksize(format);
-      const uint bytes = w * h * d * bpp;
-      lpr->data = align_malloc(bytes, 16);
+      const uint bytes = templat->width0;
+      assert(util_format_get_blocksize(templat->format) == 1);
+      assert(templat->height0 == 1);
+      assert(templat->depth0 == 1);
+      assert(templat->last_level == 0);
+      /*
+       * Reserve some extra storage since if we'd render to a buffer we
+       * read/write always LP_RASTER_BLOCK_SIZE pixels, but the element
+       * offset doesn't need to be aligned to LP_RASTER_BLOCK_SIZE.
+       */
+      lpr->data = align_malloc(bytes + (LP_RASTER_BLOCK_SIZE - 1) * 4 * sizeof(float), 64);
+
+      /*
+       * buffers don't really have stride but it's probably safer
+       * (for code doing same calculations for buffers and textures)
+       * to put something sane in there.
+       */
+      lpr->row_stride[0] = bytes;
       if (!lpr->data)
          goto fail;
       memset(lpr->data, 0, bytes);
@@ -295,9 +305,17 @@ llvmpipe_resource_create(struct pipe_screen *_screen,
 }
 
 
+static struct pipe_resource *
+llvmpipe_resource_create(struct pipe_screen *_screen,
+                         const struct pipe_resource *templat)
+{
+   return llvmpipe_resource_create_front(_screen, templat, NULL);
+}
+
+
 static void
 llvmpipe_resource_destroy(struct pipe_screen *pscreen,
-                         struct pipe_resource *pt)
+                          struct pipe_resource *pt)
 {
    struct llvmpipe_screen *screen = llvmpipe_screen(pscreen);
    struct llvmpipe_resource *lpr = llvmpipe_resource(pt);
@@ -306,38 +324,12 @@ llvmpipe_resource_destroy(struct pipe_screen *pscreen,
       /* display target */
       struct sw_winsys *winsys = screen->winsys;
       winsys->displaytarget_destroy(winsys, lpr->dt);
-
-      if (lpr->tiled[0].data) {
-         align_free(lpr->tiled[0].data);
-         lpr->tiled[0].data = NULL;
-      }
-
-      FREE(lpr->layout[0]);
    }
-   else if (resource_is_texture(pt)) {
-      /* regular texture */
-      uint level;
-
+   else if (llvmpipe_resource_is_texture(pt)) {
       /* free linear image data */
-      for (level = 0; level < Elements(lpr->linear); level++) {
-         if (lpr->linear[level].data) {
-            align_free(lpr->linear[level].data);
-            lpr->linear[level].data = NULL;
-         }
-      }
-
-      /* free tiled image data */
-      for (level = 0; level < Elements(lpr->tiled); level++) {
-         if (lpr->tiled[level].data) {
-            align_free(lpr->tiled[level].data);
-            lpr->tiled[level].data = NULL;
-         }
-      }
-
-      /* free layout flag arrays */
-      for (level = 0; level < Elements(lpr->tiled); level++) {
-         FREE(lpr->layout[level]);
-         lpr->layout[level] = NULL;
+      if (lpr->tex_data) {
+         align_free(lpr->tex_data);
+         lpr->tex_data = NULL;
       }
    }
    else if (!lpr->userBuffer) {
@@ -361,8 +353,7 @@ void *
 llvmpipe_resource_map(struct pipe_resource *resource,
                       unsigned level,
                       unsigned layer,
-                      enum lp_texture_usage tex_usage,
-                      enum lp_texture_layout layout)
+                      enum lp_texture_usage tex_usage)
 {
    struct llvmpipe_resource *lpr = llvmpipe_resource(resource);
    uint8_t *map;
@@ -374,16 +365,11 @@ llvmpipe_resource_map(struct pipe_resource *resource,
           tex_usage == LP_TEX_USAGE_READ_WRITE ||
           tex_usage == LP_TEX_USAGE_WRITE_ALL);
 
-   assert(layout == LP_TEX_LAYOUT_NONE ||
-          layout == LP_TEX_LAYOUT_TILED ||
-          layout == LP_TEX_LAYOUT_LINEAR);
-
    if (lpr->dt) {
       /* display target */
       struct llvmpipe_screen *screen = llvmpipe_screen(resource->screen);
       struct sw_winsys *winsys = screen->winsys;
       unsigned dt_usage;
-      uint8_t *map2;
 
       if (tex_usage == LP_TEX_USAGE_READ) {
          dt_usage = PIPE_TRANSFER_READ;
@@ -399,19 +385,13 @@ llvmpipe_resource_map(struct pipe_resource *resource,
       map = winsys->displaytarget_map(winsys, lpr->dt, dt_usage);
 
       /* install this linear image in texture data structure */
-      lpr->linear[level].data = map;
-
-      /* make sure tiled data gets converted to linear data */
-      map2 = llvmpipe_get_texture_image(lpr, 0, 0, tex_usage, layout);
-      if (layout == LP_TEX_LAYOUT_LINEAR)
-         assert(map == map2);
+      lpr->tex_data = map;
 
-      return map2;
+      return map;
    }
-   else if (resource_is_texture(resource)) {
+   else if (llvmpipe_resource_is_texture(resource)) {
 
-      map = llvmpipe_get_texture_image(lpr, layer, level,
-                                       tex_usage, layout);
+      map = llvmpipe_get_texture_image_address(lpr, layer, level);
       return map;
    }
    else {
@@ -438,11 +418,6 @@ llvmpipe_resource_unmap(struct pipe_resource *resource,
       assert(level == 0);
       assert(layer == 0);
 
-      /* make sure linear image is up to date */
-      (void) llvmpipe_get_texture_image(lpr, layer, level,
-                                        LP_TEX_USAGE_READ,
-                                        LP_TEX_LAYOUT_LINEAR);
-
       winsys->displaytarget_unmap(winsys, lpr->dt);
    }
 }
@@ -453,7 +428,7 @@ llvmpipe_resource_data(struct pipe_resource *resource)
 {
    struct llvmpipe_resource *lpr = llvmpipe_resource(resource);
 
-   assert(!resource_is_texture(resource));
+   assert(!llvmpipe_resource_is_texture(resource));
 
    return lpr->data;
 }
@@ -461,12 +436,12 @@ llvmpipe_resource_data(struct pipe_resource *resource)
 
 static struct pipe_resource *
 llvmpipe_resource_from_handle(struct pipe_screen *screen,
-                             const struct pipe_resource *template,
-                             struct winsys_handle *whandle)
+                              const struct pipe_resource *template,
+                              struct winsys_handle *whandle,
+                              unsigned usage)
 {
    struct sw_winsys *winsys = llvmpipe_screen(screen)->winsys;
    struct llvmpipe_resource *lpr;
-   unsigned width, height, width_t, height_t;
 
    /* XXX Seems like from_handled depth textures doesn't work that well */
 
@@ -479,11 +454,6 @@ llvmpipe_resource_from_handle(struct pipe_screen *screen,
    pipe_reference_init(&lpr->base.reference, 1);
    lpr->base.screen = screen;
 
-   width = align(lpr->base.width0, TILE_SIZE);
-   height = align(lpr->base.height0, TILE_SIZE);
-   width_t = width / TILE_SIZE;
-   height_t = height / TILE_SIZE;
-
    /*
     * Looks like unaligned displaytargets work just fine,
     * at least sampler/render ones.
@@ -493,11 +463,6 @@ llvmpipe_resource_from_handle(struct pipe_screen *screen,
    assert(lpr->base.height0 == height);
 #endif
 
-   lpr->tiles_per_row[0] = width_t;
-   lpr->tiles_per_image[0] = width_t * height_t;
-   lpr->num_slices_faces[0] = 1;
-   lpr->img_stride[0] = 0;
-
    lpr->dt = winsys->displaytarget_from_handle(winsys,
                                                template,
                                                whandle,
@@ -506,13 +471,6 @@ llvmpipe_resource_from_handle(struct pipe_screen *screen,
       goto no_dt;
    }
 
-   lpr->layout[0] = alloc_layout_array(1, lpr->base.width0, lpr->base.height0);
-   if (!lpr->layout[0]) {
-      goto no_layout_0;
-   }
-
-   assert(lpr->layout[0][0] == LP_TEX_LAYOUT_NONE);
-
    lpr->id = id_counter++;
 
 #ifdef DEBUG
@@ -521,8 +479,6 @@ llvmpipe_resource_from_handle(struct pipe_screen *screen,
 
    return &lpr->base;
 
-no_layout_0:
-   winsys->displaytarget_destroy(winsys, lpr->dt);
 no_dt:
    FREE(lpr);
 no_lpr:
@@ -530,81 +486,45 @@ no_lpr:
 }
 
 
-static boolean
+static bool
 llvmpipe_resource_get_handle(struct pipe_screen *screen,
+                             struct pipe_context *ctx,
                             struct pipe_resource *pt,
-                            struct winsys_handle *whandle)
+                            struct winsys_handle *whandle,
+                             unsigned usage)
 {
    struct sw_winsys *winsys = llvmpipe_screen(screen)->winsys;
    struct llvmpipe_resource *lpr = llvmpipe_resource(pt);
 
    assert(lpr->dt);
    if (!lpr->dt)
-      return FALSE;
+      return false;
 
    return winsys->displaytarget_get_handle(winsys, lpr->dt, whandle);
 }
 
 
-static struct pipe_surface *
-llvmpipe_create_surface(struct pipe_context *pipe,
-                        struct pipe_resource *pt,
-                        const struct pipe_surface *surf_tmpl)
-{
-   struct pipe_surface *ps;
-
-   assert(surf_tmpl->u.tex.level <= pt->last_level);
-
-   ps = CALLOC_STRUCT(pipe_surface);
-   if (ps) {
-      pipe_reference_init(&ps->reference, 1);
-      pipe_resource_reference(&ps->texture, pt);
-      ps->context = pipe;
-      ps->format = surf_tmpl->format;
-      ps->width = u_minify(pt->width0, surf_tmpl->u.tex.level);
-      ps->height = u_minify(pt->height0, surf_tmpl->u.tex.level);
-      ps->usage = surf_tmpl->usage;
-
-      ps->u.tex.level = surf_tmpl->u.tex.level;
-      ps->u.tex.first_layer = surf_tmpl->u.tex.first_layer;
-      ps->u.tex.last_layer = surf_tmpl->u.tex.last_layer;
-   }
-   return ps;
-}
-
-
-static void 
-llvmpipe_surface_destroy(struct pipe_context *pipe,
-                         struct pipe_surface *surf)
-{
-   /* Effectively do the texture_update work here - if texture images
-    * needed post-processing to put them into hardware layout, this is
-    * where it would happen.  For llvmpipe, nothing to do.
-    */
-   assert(surf->texture);
-   pipe_resource_reference(&surf->texture, NULL);
-   FREE(surf);
-}
-
-
-static struct pipe_transfer *
-llvmpipe_get_transfer(struct pipe_context *pipe,
-                      struct pipe_resource *resource,
-                      unsigned level,
-                      unsigned usage,
-                      const struct pipe_box *box)
+static void *
+llvmpipe_transfer_map( struct pipe_context *pipe,
+                       struct pipe_resource *resource,
+                       unsigned level,
+                       unsigned usage,
+                       const struct pipe_box *box,
+                       struct pipe_transfer **transfer )
 {
    struct llvmpipe_context *llvmpipe = llvmpipe_context(pipe);
-   struct llvmpipe_resource *lprex = llvmpipe_resource(resource);
-   struct llvmpipe_transfer *lpr;
+   struct llvmpipe_screen *screen = llvmpipe_screen(pipe->screen);
+   struct llvmpipe_resource *lpr = llvmpipe_resource(resource);
+   struct llvmpipe_transfer *lpt;
+   struct pipe_transfer *pt;
+   ubyte *map;
+   enum pipe_format format;
+   enum lp_texture_usage tex_usage;
+   const char *mode;
 
    assert(resource);
    assert(level <= resource->last_level);
 
-   if (usage & PIPE_TRANSFER_MAP_PERMANENTLY) {
-      return NULL;
-   }
-
    /*
     * Transfers, like other pipe operations, must happen in order, so flush the
     * context if necessary.
@@ -614,7 +534,6 @@ llvmpipe_get_transfer(struct pipe_context *pipe,
       boolean do_not_block = !!(usage & PIPE_TRANSFER_DONTBLOCK);
       if (!llvmpipe_flush_resource(pipe, resource,
                                    level,
-                                   box->depth > 1 ? -1 : box->z,
                                    read_only,
                                    TRUE, /* cpu_access */
                                    do_not_block,
@@ -627,51 +546,32 @@ llvmpipe_get_transfer(struct pipe_context *pipe,
       }
    }
 
-   if (resource == llvmpipe->constants[PIPE_SHADER_FRAGMENT][0])
-      llvmpipe->dirty |= LP_NEW_CONSTANTS;
-
-   lpr = CALLOC_STRUCT(llvmpipe_transfer);
-   if (lpr) {
-      struct pipe_transfer *pt = &lpr->base;
-      pipe_resource_reference(&pt->resource, resource);
-      pt->box = *box;
-      pt->level = level;
-      pt->stride = lprex->row_stride[level];
-      pt->layer_stride = lprex->img_stride[level];
-      pt->usage = usage;
-
-      return pt;
+   /* Check if we're mapping a current constant buffer */
+   if ((usage & PIPE_TRANSFER_WRITE) &&
+       (resource->bind & PIPE_BIND_CONSTANT_BUFFER)) {
+      unsigned i;
+      for (i = 0; i < ARRAY_SIZE(llvmpipe->constants[PIPE_SHADER_FRAGMENT]); ++i) {
+         if (resource == llvmpipe->constants[PIPE_SHADER_FRAGMENT][i].buffer) {
+            /* constants may have changed */
+            llvmpipe->dirty |= LP_NEW_FS_CONSTANTS;
+            break;
+         }
+      }
    }
-   return NULL;
-}
-
-
-static void 
-llvmpipe_transfer_destroy(struct pipe_context *pipe,
-                              struct pipe_transfer *transfer)
-{
-   /* Effectively do the texture_update work here - if texture images
-    * needed post-processing to put them into hardware layout, this is
-    * where it would happen.  For llvmpipe, nothing to do.
-    */
-   assert (transfer->resource);
-   pipe_resource_reference(&transfer->resource, NULL);
-   FREE(transfer);
-}
-
 
-static void *
-llvmpipe_transfer_map( struct pipe_context *pipe,
-                       struct pipe_transfer *transfer )
-{
-   struct llvmpipe_screen *screen = llvmpipe_screen(pipe->screen);
-   ubyte *map;
-   struct llvmpipe_resource *lpr;
-   enum pipe_format format;
-   enum lp_texture_usage tex_usage;
-   const char *mode;
+   lpt = CALLOC_STRUCT(llvmpipe_transfer);
+   if (!lpt)
+      return NULL;
+   pt = &lpt->base;
+   pipe_resource_reference(&pt->resource, resource);
+   pt->box = *box;
+   pt->level = level;
+   pt->stride = lpr->row_stride[level];
+   pt->layer_stride = lpr->img_stride[level];
+   pt->usage = usage;
+   *transfer = pt;
 
-   assert(transfer->level < LP_MAX_TEXTURE_LEVELS);
+   assert(level < LP_MAX_TEXTURE_LEVELS);
 
    /*
    printf("tex_transfer_map(%d, %d  %d x %d of %d x %d,  usage %d )\n",
@@ -681,7 +581,7 @@ llvmpipe_transfer_map( struct pipe_context *pipe,
           transfer->usage);
    */
 
-   if (transfer->usage == PIPE_TRANSFER_READ) {
+   if (usage == PIPE_TRANSFER_READ) {
       tex_usage = LP_TEX_USAGE_READ;
       mode = "read";
    }
@@ -691,33 +591,29 @@ llvmpipe_transfer_map( struct pipe_context *pipe,
    }
 
    if (0) {
-      struct llvmpipe_resource *lpr = llvmpipe_resource(transfer->resource);
       printf("transfer map tex %u  mode %s\n", lpr->id, mode);
    }
 
-
-   assert(transfer->resource);
-   lpr = llvmpipe_resource(transfer->resource);
    format = lpr->base.format;
 
-   map = llvmpipe_resource_map(transfer->resource,
-                               transfer->level,
-                               transfer->box.z,
-                               tex_usage, LP_TEX_LAYOUT_LINEAR);
+   map = llvmpipe_resource_map(resource,
+                               level,
+                               box->z,
+                               tex_usage);
 
 
    /* May want to do different things here depending on read/write nature
     * of the map:
     */
-   if (transfer->usage & PIPE_TRANSFER_WRITE) {
+   if (usage & PIPE_TRANSFER_WRITE) {
       /* Do something to notify sharing contexts of a texture change.
        */
       screen->timestamp++;
    }
 
    map +=
-      transfer->box.y / util_format_get_blockheight(format) * transfer->stride +
-      transfer->box.x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
+      box->y / util_format_get_blockheight(format) * pt->stride +
+      box->x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
 
    return map;
 }
@@ -732,36 +628,76 @@ llvmpipe_transfer_unmap(struct pipe_context *pipe,
    llvmpipe_resource_unmap(transfer->resource,
                            transfer->level,
                            transfer->box.z);
+
+   /* Effectively do the texture_update work here - if texture images
+    * needed post-processing to put them into hardware layout, this is
+    * where it would happen.  For llvmpipe, nothing to do.
+    */
+   assert (transfer->resource);
+   pipe_resource_reference(&transfer->resource, NULL);
+   FREE(transfer);
 }
 
 unsigned int
 llvmpipe_is_resource_referenced( struct pipe_context *pipe,
                                  struct pipe_resource *presource,
-                                 unsigned level, int layer)
+                                 unsigned level)
 {
    struct llvmpipe_context *llvmpipe = llvmpipe_context( pipe );
-
-   if (presource->target == PIPE_BUFFER)
+   if (!(presource->bind & (PIPE_BIND_DEPTH_STENCIL |
+                            PIPE_BIND_RENDER_TARGET |
+                            PIPE_BIND_SAMPLER_VIEW |
+                            PIPE_BIND_SHADER_BUFFER |
+                            PIPE_BIND_SHADER_IMAGE)))
       return LP_UNREFERENCED;
 
    return lp_setup_is_resource_referenced(llvmpipe->setup, presource);
 }
 
 
+/**
+ * Returns the largest possible alignment for a format in llvmpipe
+ */
+unsigned
+llvmpipe_get_format_alignment( enum pipe_format format )
+{
+   const struct util_format_description *desc = util_format_description(format);
+   unsigned size = 0;
+   unsigned bytes;
+   unsigned i;
+
+   for (i = 0; i < desc->nr_channels; ++i) {
+      size += desc->channel[i].size;
+   }
+
+   bytes = size / 8;
+
+   if (!util_is_power_of_two_or_zero(bytes)) {
+      bytes /= desc->nr_channels;
+   }
+
+   if (bytes % 2 || bytes < 1) {
+      return 1;
+   } else {
+      return bytes;
+   }
+}
+
 
 /**
  * Create buffer which wraps user-space data.
+ * XXX unreachable.
  */
-static struct pipe_resource *
+struct pipe_resource *
 llvmpipe_user_buffer_create(struct pipe_screen *screen,
                             void *ptr,
                             unsigned bytes,
-                           unsigned bind_flags)
+                            unsigned bind_flags)
 {
    struct llvmpipe_resource *buffer;
 
    buffer = CALLOC_STRUCT(llvmpipe_resource);
-   if(!buffer)
+   if (!buffer)
       return NULL;
 
    pipe_reference_init(&buffer->base.reference, 1);
@@ -783,105 +719,12 @@ llvmpipe_user_buffer_create(struct pipe_screen *screen,
 
 /**
  * Compute size (in bytes) need to store a texture image / mipmap level,
- * for just one cube face or one 3D texture slice
- */
-static unsigned
-tex_image_face_size(const struct llvmpipe_resource *lpr, unsigned level,
-                    enum lp_texture_layout layout)
-{
-   const unsigned width = u_minify(lpr->base.width0, level);
-   const unsigned height = u_minify(lpr->base.height0, level);
-
-   assert(layout == LP_TEX_LAYOUT_TILED ||
-          layout == LP_TEX_LAYOUT_LINEAR);
-
-   if (layout == LP_TEX_LAYOUT_TILED) {
-      /* for tiled layout, force a 32bpp format */
-      const enum pipe_format format = PIPE_FORMAT_B8G8R8A8_UNORM;
-      const unsigned block_size = util_format_get_blocksize(format);
-      const unsigned nblocksy =
-         util_format_get_nblocksy(format, align(height, TILE_SIZE));
-      const unsigned nblocksx =
-         util_format_get_nblocksx(format, align(width, TILE_SIZE));
-      const unsigned buffer_size = block_size * nblocksy * nblocksx;
-      return buffer_size;
-   }
-   else {
-      /* we already computed this */
-      return lpr->img_stride[level];
-   }
-}
-
-
-/**
- * Compute size (in bytes) need to store a texture image / mipmap level,
- * including all cube faces or 3D image slices
+ * for just one cube face, one array layer or one 3D texture slice
  */
 static unsigned
-tex_image_size(const struct llvmpipe_resource *lpr, unsigned level,
-               enum lp_texture_layout layout)
-{
-   const unsigned buf_size = tex_image_face_size(lpr, level, layout);
-   return buf_size * lpr->num_slices_faces[level];
-}
-
-
-/**
- * This function encapsulates some complicated logic for determining
- * how to convert a tile of image data from linear layout to tiled
- * layout, or vice versa.
- * \param cur_layout  the current tile layout
- * \param target_layout  the desired tile layout
- * \param usage  how the tile will be accessed (R/W vs. read-only, etc)
- * \param new_layout_return  returns the new layout mode
- * \param convert_return  returns TRUE if image conversion is needed
- */
-static void
-layout_logic(enum lp_texture_layout cur_layout,
-             enum lp_texture_layout target_layout,
-             enum lp_texture_usage usage,
-             enum lp_texture_layout *new_layout_return,
-             boolean *convert)
+tex_image_face_size(const struct llvmpipe_resource *lpr, unsigned level)
 {
-   enum lp_texture_layout other_layout, new_layout;
-
-   *convert = FALSE;
-
-   new_layout = 99; /* debug check */
-
-   if (target_layout == LP_TEX_LAYOUT_LINEAR) {
-      other_layout = LP_TEX_LAYOUT_TILED;
-   }
-   else {
-      assert(target_layout == LP_TEX_LAYOUT_TILED);
-      other_layout = LP_TEX_LAYOUT_LINEAR;
-   }
-
-   new_layout = target_layout;  /* may get changed below */
-
-   if (cur_layout == LP_TEX_LAYOUT_BOTH) {
-      if (usage == LP_TEX_USAGE_READ) {
-         new_layout = LP_TEX_LAYOUT_BOTH;
-      }
-   }
-   else if (cur_layout == other_layout) {
-      if (usage != LP_TEX_USAGE_WRITE_ALL) {
-         /* need to convert tiled data to linear or vice versa */
-         *convert = TRUE;
-
-         if (usage == LP_TEX_USAGE_READ)
-            new_layout = LP_TEX_LAYOUT_BOTH;
-      }
-   }
-   else {
-      assert(cur_layout == LP_TEX_LAYOUT_NONE ||
-             cur_layout == target_layout);
-   }
-
-   assert(new_layout == LP_TEX_LAYOUT_BOTH ||
-          new_layout == target_layout);
-
-   *new_layout_return = new_layout;
+   return lpr->img_stride[level];
 }
 
 
@@ -891,486 +734,18 @@ layout_logic(enum lp_texture_layout cur_layout,
  */
 ubyte *
 llvmpipe_get_texture_image_address(struct llvmpipe_resource *lpr,
-                                   unsigned face_slice, unsigned level,
-                                   enum lp_texture_layout layout)
+                                   unsigned face_slice, unsigned level)
 {
-   struct llvmpipe_texture_image *img;
    unsigned offset;
 
-   if (layout == LP_TEX_LAYOUT_LINEAR) {
-      img = &lpr->linear[level];
-   }
-   else {
-      assert (layout == LP_TEX_LAYOUT_TILED);
-      img = &lpr->tiled[level];
-   }
-
-   if (face_slice > 0)
-      offset = face_slice * tex_image_face_size(lpr, level, layout);
-   else
-      offset = 0;
-
-   return (ubyte *) img->data + offset;
-}
-
-
-static INLINE enum lp_texture_layout
-llvmpipe_get_texture_tile_layout(const struct llvmpipe_resource *lpr,
-                                 unsigned face_slice, unsigned level,
-                                 unsigned x, unsigned y)
-{
-   uint i;
-   assert(resource_is_texture(&lpr->base));
-   assert(x < lpr->tiles_per_row[level]);
-   i = face_slice * lpr->tiles_per_image[level]
-      + y * lpr->tiles_per_row[level] + x;
-   return lpr->layout[level][i];
-}
-
-
-static INLINE void
-llvmpipe_set_texture_tile_layout(struct llvmpipe_resource *lpr,
-                                 unsigned face_slice, unsigned level,
-                                 unsigned x, unsigned y,
-                                 enum lp_texture_layout layout)
-{
-   uint i;
-   assert(resource_is_texture(&lpr->base));
-   assert(x < lpr->tiles_per_row[level]);
-   i = face_slice * lpr->tiles_per_image[level]
-      + y * lpr->tiles_per_row[level] + x;
-   lpr->layout[level][i] = layout;
-}
-
-
-/**
- * Set the layout mode for all tiles in a particular image.
- */
-static INLINE void
-llvmpipe_set_texture_image_layout(struct llvmpipe_resource *lpr,
-                                  unsigned face_slice, unsigned level,
-                                  unsigned width_t, unsigned height_t,
-                                  enum lp_texture_layout layout)
-{
-   const unsigned start = face_slice * lpr->tiles_per_image[level];
-   unsigned i;
-
-   for (i = 0; i < width_t * height_t; i++) {
-      lpr->layout[level][start + i] = layout;
-   }
-}
-
+   assert(llvmpipe_resource_is_texture(&lpr->base));
 
-/**
- * Allocate storage for a linear or tile texture image (all cube
- * faces and all 3D slices.
- */
-static void
-alloc_image_data(struct llvmpipe_resource *lpr, unsigned level,
-                 enum lp_texture_layout layout)
-{
-   uint alignment = MAX2(16, util_cpu_caps.cacheline);
-
-   if (lpr->dt)
-      assert(level == 0);
-
-   if (layout == LP_TEX_LAYOUT_TILED) {
-      /* tiled data is stored in regular memory */
-      uint buffer_size = tex_image_size(lpr, level, layout);
-      lpr->tiled[level].data = align_malloc(buffer_size, alignment);
-      if (lpr->tiled[level].data) {
-         memset(lpr->tiled[level].data, 0, buffer_size);
-      }
-   }
-   else {
-      assert(layout == LP_TEX_LAYOUT_LINEAR);
-      if (lpr->dt) {
-         /* we get the linear memory from the winsys, and it has
-          * already been zeroed
-          */
-         struct llvmpipe_screen *screen = llvmpipe_screen(lpr->base.screen);
-         struct sw_winsys *winsys = screen->winsys;
-
-         lpr->linear[0].data =
-            winsys->displaytarget_map(winsys, lpr->dt,
-                                      PIPE_TRANSFER_READ_WRITE);
-      }
-      else {
-         /* not a display target - allocate regular memory */
-         uint buffer_size = tex_image_size(lpr, level, LP_TEX_LAYOUT_LINEAR);
-         lpr->linear[level].data = align_malloc(buffer_size, alignment);
-         if (lpr->linear[level].data) {
-            memset(lpr->linear[level].data, 0, buffer_size);
-         }
-      }
-   }
-}
-
-
-
-/**
- * Return pointer to texture image data (either linear or tiled layout)
- * for a particular cube face or 3D texture slice.
- *
- * \param face_slice  the cube face or 3D slice of interest
- * \param usage  one of LP_TEX_USAGE_READ/WRITE_ALL/READ_WRITE
- * \param layout  either LP_TEX_LAYOUT_LINEAR or _TILED or _NONE
- */
-void *
-llvmpipe_get_texture_image(struct llvmpipe_resource *lpr,
-                           unsigned face_slice, unsigned level,
-                           enum lp_texture_usage usage,
-                           enum lp_texture_layout layout)
-{
-   /*
-    * 'target' refers to the image which we're retrieving (either in
-    * tiled or linear layout).
-    * 'other' refers to the same image but in the other layout. (it may
-    *  or may not exist.
-    */
-   struct llvmpipe_texture_image *target_img;
-   struct llvmpipe_texture_image *other_img;
-   void *target_data;
-   void *other_data;
-   const unsigned width = u_minify(lpr->base.width0, level);
-   const unsigned height = u_minify(lpr->base.height0, level);
-   const unsigned width_t = align(width, TILE_SIZE) / TILE_SIZE;
-   const unsigned height_t = align(height, TILE_SIZE) / TILE_SIZE;
-   enum lp_texture_layout other_layout;
-   boolean only_allocate;
-
-   assert(layout == LP_TEX_LAYOUT_NONE ||
-          layout == LP_TEX_LAYOUT_TILED ||
-          layout == LP_TEX_LAYOUT_LINEAR);
-
-   assert(usage == LP_TEX_USAGE_READ ||
-          usage == LP_TEX_USAGE_READ_WRITE ||
-          usage == LP_TEX_USAGE_WRITE_ALL);
-
-   /* check for the special case of layout == LP_TEX_LAYOUT_NONE */
-   if (layout == LP_TEX_LAYOUT_NONE) {
-      only_allocate = TRUE;
-      layout = LP_TEX_LAYOUT_TILED;
-   }
-   else {
-      only_allocate = FALSE;
-   }
-
-   if (lpr->dt) {
-      assert(lpr->linear[level].data);
-   }
-
-   /* which is target?  which is other? */
-   if (layout == LP_TEX_LAYOUT_LINEAR) {
-      target_img = &lpr->linear[level];
-      other_img = &lpr->tiled[level];
-      other_layout = LP_TEX_LAYOUT_TILED;
-   }
-   else {
-      target_img = &lpr->tiled[level];
-      other_img = &lpr->linear[level];
-      other_layout = LP_TEX_LAYOUT_LINEAR;
-   }
-
-   target_data = target_img->data;
-   other_data = other_img->data;
-
-   if (!target_data) {
-      /* allocate memory for the target image now */
-      alloc_image_data(lpr, level, layout);
-      target_data = target_img->data;
-   }
-
-   if (face_slice > 0) {
-      unsigned target_offset, other_offset;
-
-      target_offset = face_slice * tex_image_face_size(lpr, level, layout);
-      other_offset = face_slice * tex_image_face_size(lpr, level, other_layout);
-      if (target_data) {
-         target_data = (uint8_t *) target_data + target_offset;
-      }
-      if (other_data) {
-         other_data = (uint8_t *) other_data + other_offset;
-      }
-   }
+   offset = lpr->mip_offsets[level];
 
-   if (only_allocate) {
-      /* Just allocating tiled memory.  Don't initialize it from the
-       * linear data if it exists.
-       */
-      return target_data;
-   }
-
-   if (other_data) {
-      /* may need to convert other data to the requested layout */
-      enum lp_texture_layout new_layout;
-      unsigned x, y;
-
-      /* loop over all image tiles, doing layout conversion where needed */
-      for (y = 0; y < height_t; y++) {
-         for (x = 0; x < width_t; x++) {
-            enum lp_texture_layout cur_layout =
-               llvmpipe_get_texture_tile_layout(lpr, face_slice, level, x, y);
-            boolean convert;
-
-            layout_logic(cur_layout, layout, usage, &new_layout, &convert);
-
-            if (convert && other_data && target_data) {
-               if (layout == LP_TEX_LAYOUT_TILED) {
-                  lp_linear_to_tiled(other_data, target_data,
-                                     x * TILE_SIZE, y * TILE_SIZE,
-                                     TILE_SIZE, TILE_SIZE,
-                                     lpr->base.format,
-                                     lpr->row_stride[level],
-                                     lpr->tiles_per_row[level]);
-               }
-               else {
-                  assert(layout == LP_TEX_LAYOUT_LINEAR);
-                  lp_tiled_to_linear(other_data, target_data,
-                                     x * TILE_SIZE, y * TILE_SIZE,
-                                     TILE_SIZE, TILE_SIZE,
-                                     lpr->base.format,
-                                     lpr->row_stride[level],
-                                     lpr->tiles_per_row[level]);
-               }
-            }
-
-            if (new_layout != cur_layout)
-               llvmpipe_set_texture_tile_layout(lpr, face_slice, level, x, y,
-                                                new_layout);
-         }
-      }
-   }
-   else {
-      /* no other data */
-      llvmpipe_set_texture_image_layout(lpr, face_slice, level,
-                                        width_t, height_t, layout);
-   }
-
-   return target_data;
-}
-
-
-/**
- * Return pointer to start of a texture image (1D, 2D, 3D, CUBE).
- * All cube faces and 3D slices will be converted to the requested
- * layout if needed.
- * This is typically used when we're about to sample from a texture.
- */
-void *
-llvmpipe_get_texture_image_all(struct llvmpipe_resource *lpr,
-                               unsigned level,
-                               enum lp_texture_usage usage,
-                               enum lp_texture_layout layout)
-{
-   const int slices = lpr->num_slices_faces[level];
-   int slice;
-   void *map = NULL;
-
-   assert(slices > 0);
-
-   for (slice = slices - 1; slice >= 0; slice--) {
-      map = llvmpipe_get_texture_image(lpr, slice, level, usage, layout);
-   }
-
-   return map;
-}
-
-
-/**
- * Get pointer to a linear image (not the tile!) where the tile at (x,y)
- * is known to be in linear layout.
- * Conversion from tiled to linear will be done if necessary.
- * \return pointer to start of image/face (not the tile)
- */
-ubyte *
-llvmpipe_get_texture_tile_linear(struct llvmpipe_resource *lpr,
-                                 unsigned face_slice, unsigned level,
-                                 enum lp_texture_usage usage,
-                                 unsigned x, unsigned y)
-{
-   struct llvmpipe_texture_image *linear_img = &lpr->linear[level];
-   enum lp_texture_layout cur_layout, new_layout;
-   const unsigned tx = x / TILE_SIZE, ty = y / TILE_SIZE;
-   boolean convert;
-   uint8_t *tiled_image, *linear_image;
-
-   assert(resource_is_texture(&lpr->base));
-   assert(x % TILE_SIZE == 0);
-   assert(y % TILE_SIZE == 0);
-
-   if (!linear_img->data) {
-      /* allocate memory for the linear image now */
-      alloc_image_data(lpr, level, LP_TEX_LAYOUT_LINEAR);
-   }
-
-   /* compute address of the slice/face of the image that contains the tile */
-   tiled_image = llvmpipe_get_texture_image_address(lpr, face_slice, level,
-                                                    LP_TEX_LAYOUT_TILED);
-   linear_image = llvmpipe_get_texture_image_address(lpr, face_slice, level,
-                                                     LP_TEX_LAYOUT_LINEAR);
-
-   /* get current tile layout and determine if data conversion is needed */
-   cur_layout = llvmpipe_get_texture_tile_layout(lpr, face_slice, level, tx, ty);
-
-   layout_logic(cur_layout, LP_TEX_LAYOUT_LINEAR, usage,
-                &new_layout, &convert);
-
-   if (convert && tiled_image && linear_image) {
-      lp_tiled_to_linear(tiled_image, linear_image,
-                         x, y, TILE_SIZE, TILE_SIZE, lpr->base.format,
-                         lpr->row_stride[level],
-                         lpr->tiles_per_row[level]);
-   }
-
-   if (new_layout != cur_layout)
-      llvmpipe_set_texture_tile_layout(lpr, face_slice, level, tx, ty, new_layout);
-
-   return linear_image;
-}
-
-
-/**
- * Get pointer to tiled data for rendering.
- * \return pointer to the tiled data at the given tile position
- */
-ubyte *
-llvmpipe_get_texture_tile(struct llvmpipe_resource *lpr,
-                          unsigned face_slice, unsigned level,
-                          enum lp_texture_usage usage,
-                          unsigned x, unsigned y)
-{
-   struct llvmpipe_texture_image *tiled_img = &lpr->tiled[level];
-   enum lp_texture_layout cur_layout, new_layout;
-   const unsigned tx = x / TILE_SIZE, ty = y / TILE_SIZE;
-   boolean convert;
-   uint8_t *tiled_image, *linear_image;
-   unsigned tile_offset;
-
-   assert(x % TILE_SIZE == 0);
-   assert(y % TILE_SIZE == 0);
-
-   if (!tiled_img->data) {
-      /* allocate memory for the tiled image now */
-      alloc_image_data(lpr, level, LP_TEX_LAYOUT_TILED);
-   }
-
-   /* compute address of the slice/face of the image that contains the tile */
-   tiled_image = llvmpipe_get_texture_image_address(lpr, face_slice, level,
-                                                    LP_TEX_LAYOUT_TILED);
-   linear_image = llvmpipe_get_texture_image_address(lpr, face_slice, level,
-                                                     LP_TEX_LAYOUT_LINEAR);
-
-   /* get current tile layout and see if we need to convert the data */
-   cur_layout = llvmpipe_get_texture_tile_layout(lpr, face_slice, level, tx, ty);
-
-   layout_logic(cur_layout, LP_TEX_LAYOUT_TILED, usage, &new_layout, &convert);
-   if (convert && linear_image && tiled_image) {
-      lp_linear_to_tiled(linear_image, tiled_image,
-                         x, y, TILE_SIZE, TILE_SIZE, lpr->base.format,
-                         lpr->row_stride[level],
-                         lpr->tiles_per_row[level]);
-   }
-
-   if (!tiled_image)
-      return NULL;
-
-   if (new_layout != cur_layout)
-      llvmpipe_set_texture_tile_layout(lpr, face_slice, level, tx, ty, new_layout);
-
-   /* compute, return address of the 64x64 tile */
-   tile_offset = (ty * lpr->tiles_per_row[level] + tx)
-         * TILE_SIZE * TILE_SIZE * 4;
-
-   return (ubyte *) tiled_image + tile_offset;
-}
-
-
-/**
- * Get pointer to tiled data for rendering.
- * \return pointer to the tiled data at the given tile position
- */
-void
-llvmpipe_unswizzle_cbuf_tile(struct llvmpipe_resource *lpr,
-                             unsigned face_slice, unsigned level,
-                             unsigned x, unsigned y,
-                             uint8_t *tile)
-{
-   struct llvmpipe_texture_image *linear_img = &lpr->linear[level];
-   const unsigned tx = x / TILE_SIZE, ty = y / TILE_SIZE;
-   uint8_t *linear_image;
-
-   assert(x % TILE_SIZE == 0);
-   assert(y % TILE_SIZE == 0);
-
-   if (!linear_img->data) {
-      /* allocate memory for the linear image now */
-      alloc_image_data(lpr, level, LP_TEX_LAYOUT_LINEAR);
-   }
-
-   /* compute address of the slice/face of the image that contains the tile */
-   linear_image = llvmpipe_get_texture_image_address(lpr, face_slice, level,
-                                                     LP_TEX_LAYOUT_LINEAR);
-
-   {
-      uint ii = x, jj = y;
-      uint tile_offset = jj / TILE_SIZE + ii / TILE_SIZE;
-      uint byte_offset = tile_offset * TILE_SIZE * TILE_SIZE * 4;
-      
-      /* Note that lp_tiled_to_linear expects the tile parameter to
-       * point at the first tile in a whole-image sized array.  In
-       * this code, we have only a single tile and have to do some
-       * pointer arithmetic to figure out where the "image" would have
-       * started.
-       */
-      lp_tiled_to_linear(tile - byte_offset, linear_image,
-                         x, y, TILE_SIZE, TILE_SIZE,
-                         lpr->base.format,
-                         lpr->row_stride[level],
-                         1);       /* tiles per row */
-   }
-
-   llvmpipe_set_texture_tile_layout(lpr, face_slice, level, tx, ty,
-                                    LP_TEX_LAYOUT_LINEAR);
-}
-
-
-/**
- * Get pointer to tiled data for rendering.
- * \return pointer to the tiled data at the given tile position
- */
-void
-llvmpipe_swizzle_cbuf_tile(struct llvmpipe_resource *lpr,
-                           unsigned face_slice, unsigned level,
-                           unsigned x, unsigned y,
-                           uint8_t *tile)
-{
-   uint8_t *linear_image;
-
-   assert(x % TILE_SIZE == 0);
-   assert(y % TILE_SIZE == 0);
-
-   /* compute address of the slice/face of the image that contains the tile */
-   linear_image = llvmpipe_get_texture_image_address(lpr, face_slice, level,
-                                                     LP_TEX_LAYOUT_LINEAR);
-
-   if (linear_image) {
-      uint ii = x, jj = y;
-      uint tile_offset = jj / TILE_SIZE + ii / TILE_SIZE;
-      uint byte_offset = tile_offset * TILE_SIZE * TILE_SIZE * 4;
+   if (face_slice > 0)
+      offset += face_slice * tex_image_face_size(lpr, level);
 
-      /* Note that lp_linear_to_tiled expects the tile parameter to
-       * point at the first tile in a whole-image sized array.  In
-       * this code, we have only a single tile and have to do some
-       * pointer arithmetic to figure out where the "image" would have
-       * started.
-       */
-      lp_linear_to_tiled(linear_image, tile - byte_offset,
-                         x, y, TILE_SIZE, TILE_SIZE,
-                         lpr->base.format,
-                         lpr->row_stride[level],
-                         1);       /* tiles per row */
-   }
+   return (ubyte *) lpr->tex_data + offset;
 }
 
 
@@ -1381,19 +756,25 @@ unsigned
 llvmpipe_resource_size(const struct pipe_resource *resource)
 {
    const struct llvmpipe_resource *lpr = llvmpipe_resource_const(resource);
-   unsigned lvl, size = 0;
-
-   for (lvl = 0; lvl <= lpr->base.last_level; lvl++) {
-      if (lpr->linear[lvl].data)
-         size += tex_image_size(lpr, lvl, LP_TEX_LAYOUT_LINEAR);
+   unsigned size = 0;
 
-      if (lpr->tiled[lvl].data)
-         size += tex_image_size(lpr, lvl, LP_TEX_LAYOUT_TILED);
+   if (llvmpipe_resource_is_texture(resource)) {
+      /* Note this will always return 0 for displaytarget resources */
+      size = lpr->total_alloc_size;
+   }
+   else {
+      size = resource->width0;
    }
-
    return size;
 }
 
+static void
+llvmpipe_memory_barrier(struct pipe_context *pipe,
+                       unsigned flags)
+{
+   /* this may be an overly large hammer for this nut. */
+   llvmpipe_finish(pipe, "barrier");
+}
 
 #ifdef DEBUG
 void
@@ -1433,25 +814,23 @@ llvmpipe_init_screen_resource_funcs(struct pipe_screen *screen)
 #endif
 
    screen->resource_create = llvmpipe_resource_create;
+/*   screen->resource_create_front = llvmpipe_resource_create_front; */
    screen->resource_destroy = llvmpipe_resource_destroy;
    screen->resource_from_handle = llvmpipe_resource_from_handle;
    screen->resource_get_handle = llvmpipe_resource_get_handle;
-   screen->user_buffer_create = llvmpipe_user_buffer_create;
-
+   screen->can_create_resource = llvmpipe_can_create_resource;
 }
 
 
 void
 llvmpipe_init_context_resource_funcs(struct pipe_context *pipe)
 {
-   pipe->get_transfer = llvmpipe_get_transfer;
-   pipe->transfer_destroy = llvmpipe_transfer_destroy;
    pipe->transfer_map = llvmpipe_transfer_map;
    pipe->transfer_unmap = llvmpipe_transfer_unmap;
+
    pipe->transfer_flush_region = u_default_transfer_flush_region;
-   pipe->transfer_inline_write = u_default_transfer_inline_write;
+   pipe->buffer_subdata = u_default_buffer_subdata;
+   pipe->texture_subdata = u_default_texture_subdata;
 
-   pipe->create_surface = llvmpipe_create_surface;
-   pipe->surface_destroy = llvmpipe_surface_destroy;
+   pipe->memory_barrier = llvmpipe_memory_barrier;
 }