r300g: cleanup texture creation code
authorMarek Olšák <maraeo@gmail.com>
Sat, 24 Jul 2010 19:32:53 +0000 (21:32 +0200)
committerMarek Olšák <maraeo@gmail.com>
Sun, 25 Jul 2010 08:25:21 +0000 (10:25 +0200)
This decouples initializing a texture layout/miptree description
from an actual texture creation, it also partially unifies texture_create
and texture_from_handle.

r300_texture inherits r300_texture_desc, which inherits u_resource.

The CBZB clear criteria are moved to r300_texture_desc::cbzb_allowed[level].

And other minor cleanups.

13 files changed:
src/gallium/drivers/r300/Makefile
src/gallium/drivers/r300/SConscript
src/gallium/drivers/r300/r300_blit.c
src/gallium/drivers/r300/r300_context.h
src/gallium/drivers/r300/r300_defines.h
src/gallium/drivers/r300/r300_fs.c
src/gallium/drivers/r300/r300_state.c
src/gallium/drivers/r300/r300_state_derived.c
src/gallium/drivers/r300/r300_texture.c
src/gallium/drivers/r300/r300_texture.h
src/gallium/drivers/r300/r300_texture_desc.c [new file with mode: 0644]
src/gallium/drivers/r300/r300_texture_desc.h [new file with mode: 0644]
src/gallium/drivers/r300/r300_transfer.c

index 13152635a65ff192194baa6835f85e4ebfb14e95..728bc40a5bb269647a6e85e7296f9b55718d86e5 100644 (file)
@@ -24,6 +24,7 @@ C_SOURCES = \
        r300_vs.c \
        r300_vs_draw.c \
        r300_texture.c \
+       r300_texture_desc.c \
        r300_tgsi_to_rc.c \
        r300_transfer.c
 
index 552ed4e5bef142e5573568816b264ea16a20cd4d..bf023daaa56c9687d1ac73cbf000a00f38987945 100644 (file)
@@ -34,6 +34,7 @@ r300 = env.ConvenienceLibrary(
         'r300_vs.c',
         'r300_vs_draw.c',
         'r300_texture.c',
+        'r300_texture_desc.c',
         'r300_tgsi_to_rc.c',
         'r300_transfer.c',
     ] + r300compiler) + r300compiler
index 895efaa1c4ac146948f86f24e536979ed60a9c51..d125196b6dc9e869bf3bb003d13cdcfbe8dd77c3 100644 (file)
@@ -97,29 +97,12 @@ static boolean r300_cbzb_clear_allowed(struct r300_context *r300,
 {
     struct pipe_framebuffer_state *fb =
         (struct pipe_framebuffer_state*)r300->fb_state.state;
-    struct r300_surface *surf = r300_surface(fb->cbufs[0]);
-    unsigned bpp;
 
     /* Only color clear allowed, and only one colorbuffer. */
     if (clear_buffers != PIPE_CLEAR_COLOR || fb->nr_cbufs != 1)
         return FALSE;
 
-    /* The colorbuffer must be point-sampled. */
-    if (surf->base.texture->nr_samples > 1)
-        return FALSE;
-
-    bpp = util_format_get_blocksizebits(surf->base.format);
-
-    /* ZB can only work with the two pixel sizes. */
-    if (bpp != 16 && bpp != 32)
-        return FALSE;
-
-    /* If the midpoint ZB offset is not aligned to 2048, it returns garbage
-     * with certain texture sizes. Macrotiling ensures the alignment. */
-    if (!r300_texture(surf->base.texture)->mip_macrotile[surf->base.level])
-        return FALSE;
-
-    return TRUE;
+    return r300_surface(fb->cbufs[0])->cbzb_allowed;
 }
 
 /* Clear currently bound buffers. */
index 7b58587a2a5b4ec328571730d6c174967f429eed..06e4e12558fd5e6a9f1321a208bebba8e08a35db 100644 (file)
@@ -318,29 +318,38 @@ struct r300_surface {
     uint32_t cbzb_midpoint_offset;  /* DEPTHOFFSET. */
     uint32_t cbzb_pitch;            /* DEPTHPITCH. */
     uint32_t cbzb_format;           /* ZB_FORMAT. */
+
+    /* Whether the CBZB clear is allowed on the surface. */
+    boolean cbzb_allowed;
 };
 
-struct r300_texture {
-    /* Parent class */
+struct r300_texture_desc {
+    /* Parent class. */
     struct u_resource b;
 
-    enum r300_buffer_domain domain;
+    /* Buffer tiling.
+     * Macrotiling is specified per-level because small mipmaps cannot
+     * be macrotiled. */
+    enum r300_buffer_tiling microtile;
+    enum r300_buffer_tiling macrotile[R300_MAX_TEXTURE_LEVELS];
 
     /* Offsets into the buffer. */
-    unsigned offset[R300_MAX_TEXTURE_LEVELS];
+    unsigned offset_in_bytes[R300_MAX_TEXTURE_LEVELS];
 
-    /* A pitch for each mip-level */
-    unsigned pitch[R300_MAX_TEXTURE_LEVELS];
+    /* Strides for each mip-level. */
+    unsigned stride_in_pixels[R300_MAX_TEXTURE_LEVELS];
+    unsigned stride_in_bytes[R300_MAX_TEXTURE_LEVELS];
 
-    /* A pitch multiplied by blockwidth as hardware wants
-     * the number of pixels instead of the number of blocks. */
-    unsigned hwpitch[R300_MAX_TEXTURE_LEVELS];
+    /* Size of one zslice or face or 2D image based on the texture target. */
+    unsigned layer_size_in_bytes[R300_MAX_TEXTURE_LEVELS];
 
-    /* Size of one zslice or face based on the texture target */
-    unsigned layer_size[R300_MAX_TEXTURE_LEVELS];
+    /* Total size of this texture, in bytes,
+     * derived from the texture properties. */
+    unsigned size_in_bytes;
 
-    /* Whether the mipmap level is macrotiled. */
-    enum r300_buffer_tiling mip_macrotile[R300_MAX_TEXTURE_LEVELS];
+    /* Total size of the buffer backing this texture, in bytes.
+     * It must be >= size. */
+    unsigned buffer_size_in_bytes;
 
     /**
      * If non-zero, override the natural texture layout with
@@ -350,21 +359,25 @@ struct r300_texture {
      *
      * \sa r300_texture_get_stride
      */
-    unsigned stride_override;
-
-    /* Total size of this texture, in bytes,
-     * derived from the texture properties. */
-    unsigned size;
-
-    /* Total size of the buffer backing this texture, in bytes.
-     * It must be >= size. */
-    unsigned buffer_size;
+    unsigned stride_in_bytes_override;
 
     /* Whether this texture has non-power-of-two dimensions
-     * or a user-specified pitch.
+     * or a user-specified stride.
      * It can be either a regular texture or a rectangle one.
+     *
+     * This flag says that hardware must use the stride for addressing
+     * instead of the width.
      */
-    boolean uses_pitch;
+    boolean uses_stride_addressing;
+
+    /* Whether CBZB fast color clear is allowed on the miplevel. */
+    boolean cbzb_allowed[R300_MAX_TEXTURE_LEVELS];
+};
+
+struct r300_texture {
+    struct r300_texture_desc desc;
+
+    enum r300_buffer_domain domain;
 
     /* Pipe buffer backing this texture. */
     struct r300_winsys_buffer *buffer;
@@ -375,9 +388,6 @@ struct r300_texture {
     /* All bits should be filled in. */
     struct r300_texture_fb_state fb_state;
 
-    /* Buffer tiling */
-    enum r300_buffer_tiling microtile, macrotile;
-
     /* This is the level tiling flags were last time set for.
      * It's used to prevent redundant tiling-flags changes from happening.*/
     unsigned surface_level;
index d510d80a7bb293e807f40746e2ba8994a6148b83..896aeef395d7544c0b443d531958fe24e5aa7aa6 100644 (file)
 enum r300_buffer_tiling {
     R300_BUFFER_LINEAR = 0,
     R300_BUFFER_TILED,
-    R300_BUFFER_SQUARETILED
+    R300_BUFFER_SQUARETILED,
+
+    R300_BUFFER_UNKNOWN,
+    R300_BUFFER_SELECT_LAYOUT = R300_BUFFER_UNKNOWN
 };
 
 enum r300_buffer_domain { /* bitfield */
index b145ded63993aa34685dd685b5463808672543fe..6eac12bfb9f6ccdd877d5008474102ceeb026314 100644 (file)
@@ -173,7 +173,7 @@ static void get_external_state(
             t = (struct r300_texture*)texstate->sampler_views[i]->base.texture;
 
             /* XXX this should probably take into account STR, not just S. */
-            if (t->uses_pitch) {
+            if (t->desc.uses_stride_addressing) {
                 switch (s->state.wrap_s) {
                     case PIPE_TEX_WRAP_REPEAT:
                         state->unit[i].wrap_mode = RC_WRAP_REPEAT;
index f52265b1c03affc5ba1e107edd34dd1132158212..6e2a6ca0e42d87f850ecc3b409d5beeeacac975f 100644 (file)
@@ -619,7 +619,8 @@ static void r300_tex_set_tiling_flags(struct r300_context *r300,
 {
     /* Check if the macrotile flag needs to be changed.
      * Skip changing the flags otherwise. */
-    if (tex->mip_macrotile[tex->surface_level] != tex->mip_macrotile[level]) {
+    if (tex->desc.macrotile[tex->surface_level] !=
+        tex->desc.macrotile[level]) {
         /* Tiling determines how DRM treats the buffer data.
          * We must flush CS when changing it if the buffer is referenced. */
         if (r300->rws->cs_is_buffer_referenced(r300->cs,
@@ -627,8 +628,8 @@ static void r300_tex_set_tiling_flags(struct r300_context *r300,
             r300->context.flush(&r300->context, 0, NULL);
 
         r300->rws->buffer_set_tiling(r300->rws, tex->buffer,
-                tex->microtile, tex->mip_macrotile[level],
-                tex->pitch[0] * util_format_get_blocksize(tex->b.b.format));
+                tex->desc.microtile, tex->desc.macrotile[level],
+                tex->desc.stride_in_bytes[0]);
 
         tex->surface_level = level;
     }
@@ -670,8 +671,10 @@ static void r300_print_fb_surf_info(struct pipe_surface *surf, unsigned index,
             surf->zslice, surf->face, surf->level,
             util_format_short_name(surf->format),
 
-            rtex->macrotile ? "YES" : " NO", rtex->microtile ? "YES" : " NO",
-            rtex->hwpitch[0], tex->width0, tex->height0, tex->depth0,
+            rtex->desc.macrotile[0] ? "YES" : " NO",
+            rtex->desc.microtile ? "YES" : " NO",
+            rtex->desc.stride_in_pixels[0],
+            tex->width0, tex->height0, tex->depth0,
             tex->last_level, util_format_short_name(tex->format));
 }
 
@@ -1293,7 +1296,7 @@ static void r300_set_fragment_sampler_views(struct pipe_context* pipe,
             /* Set the texrect factor in the fragment shader.
              * Needed for RECT and NPOT fallback. */
             texture = r300_texture(views[i]->texture);
-            if (texture->uses_pitch) {
+            if (texture->desc.uses_stride_addressing) {
                 r300->fs_rc_constant_state.dirty = TRUE;
             }
 
index 2ef9766578c5d54f40741028fb69ec75c91e6105..e20d8d0fdf00c2e4b3eb2bdb2b75d8458dc34b49 100644 (file)
@@ -567,7 +567,7 @@ static void r300_merge_textures_and_samplers(struct r300_context* r300)
              * are stored in the format.
              * Otherwise, swizzles must be applied after the compare mode
              * in the fragment shader. */
-            if (util_format_is_depth_or_stencil(tex->b.b.format)) {
+            if (util_format_is_depth_or_stencil(tex->desc.b.b.format)) {
                 if (sampler->state.compare_mode == PIPE_TEX_COMPARE_NONE) {
                     texstate->format.format1 |=
                         r300_get_swizzle_combined(depth_swizzle, view->swizzle);
@@ -578,12 +578,12 @@ static void r300_merge_textures_and_samplers(struct r300_context* r300)
             }
 
             /* to emulate 1D textures through 2D ones correctly */
-            if (tex->b.b.target == PIPE_TEXTURE_1D) {
+            if (tex->desc.b.b.target == PIPE_TEXTURE_1D) {
                 texstate->filter0 &= ~R300_TX_WRAP_T_MASK;
                 texstate->filter0 |= R300_TX_WRAP_T(R300_TX_CLAMP_TO_EDGE);
             }
 
-            if (tex->uses_pitch) {
+            if (tex->desc.uses_stride_addressing) {
                 /* NPOT textures don't support mip filter, unfortunately.
                  * This prevents incorrect rendering. */
                 texstate->filter0 &= ~R300_TX_MIN_FILTER_MIP_MASK;
@@ -610,7 +610,7 @@ static void r300_merge_textures_and_samplers(struct r300_context* r300)
                 /* determine min/max levels */
                 /* the MAX_MIP level is the largest (finest) one */
                 max_level = MIN3(sampler->max_lod + view->base.first_level,
-                                 tex->b.b.last_level, view->base.last_level);
+                                 tex->desc.b.b.last_level, view->base.last_level);
                 min_level = MIN2(sampler->min_lod + view->base.first_level,
                                  max_level);
                 texstate->format.format0 |= R300_TX_NUM_LEVELS(max_level);
index 711042722cca747d4f8fb697deb037869a2fae44..e99a4630ee4294e40a818ad819c213c08e561a2e 100644 (file)
@@ -26,6 +26,7 @@
 
 #include "r300_context.h"
 #include "r300_reg.h"
+#include "r300_texture_desc.h"
 #include "r300_transfer.h"
 #include "r300_screen.h"
 #include "r300_winsys.h"
 
 #include "pipe/p_screen.h"
 
-enum r300_dim {
-    DIM_WIDTH  = 0,
-    DIM_HEIGHT = 1
-};
-
 unsigned r300_get_swizzle_combined(const unsigned char *swizzle_format,
                                    const unsigned char *swizzle_view)
 {
@@ -544,17 +540,17 @@ static void r300_texture_setup_immutable_state(struct r300_screen* screen,
                                                struct r300_texture* tex)
 {
     struct r300_texture_format_state* f = &tex->tx_format;
-    struct pipe_resource *pt = &tex->b.b;
+    struct pipe_resource *pt = &tex->desc.b.b;
     boolean is_r500 = screen->caps.is_r500;
 
     /* Set sampler state. */
     f->format0 = R300_TX_WIDTH((pt->width0 - 1) & 0x7ff) |
                  R300_TX_HEIGHT((pt->height0 - 1) & 0x7ff);
 
-    if (tex->uses_pitch) {
+    if (tex->desc.uses_stride_addressing) {
         /* rectangles love this */
         f->format0 |= R300_TX_PITCH_EN;
-        f->format2 = (tex->hwpitch[0] - 1) & 0x1fff;
+        f->format2 = (tex->desc.stride_in_pixels[0] - 1) & 0x1fff;
     } else {
         /* power of two textures (3D, mipmaps, and no pitch) */
         f->format0 |= R300_TX_DEPTH(util_logbase2(pt->depth0) & 0xf);
@@ -579,8 +575,8 @@ static void r300_texture_setup_immutable_state(struct r300_screen* screen,
         }
     }
 
-    f->tile_config = R300_TXO_MACRO_TILE(tex->macrotile) |
-                     R300_TXO_MICRO_TILE(tex->microtile);
+    f->tile_config = R300_TXO_MACRO_TILE(tex->desc.macrotile[0]) |
+                     R300_TXO_MICRO_TILE(tex->desc.microtile);
 }
 
 static void r300_texture_setup_fb_state(struct r300_screen* screen,
@@ -589,23 +585,23 @@ static void r300_texture_setup_fb_state(struct r300_screen* screen,
     unsigned i;
 
     /* Set framebuffer state. */
-    if (util_format_is_depth_or_stencil(tex->b.b.format)) {
-        for (i = 0; i <= tex->b.b.last_level; i++) {
+    if (util_format_is_depth_or_stencil(tex->desc.b.b.format)) {
+        for (i = 0; i <= tex->desc.b.b.last_level; i++) {
             tex->fb_state.pitch[i] =
-                tex->hwpitch[i] |
-                R300_DEPTHMACROTILE(tex->mip_macrotile[i]) |
-                R300_DEPTHMICROTILE(tex->microtile);
+                tex->desc.stride_in_pixels[i] |
+                R300_DEPTHMACROTILE(tex->desc.macrotile[i]) |
+                R300_DEPTHMICROTILE(tex->desc.microtile);
         }
-        tex->fb_state.format = r300_translate_zsformat(tex->b.b.format);
+        tex->fb_state.format = r300_translate_zsformat(tex->desc.b.b.format);
     } else {
-        for (i = 0; i <= tex->b.b.last_level; i++) {
+        for (i = 0; i <= tex->desc.b.b.last_level; i++) {
             tex->fb_state.pitch[i] =
-                tex->hwpitch[i] |
-                r300_translate_colorformat(tex->b.b.format) |
-                R300_COLOR_TILE(tex->mip_macrotile[i]) |
-                R300_COLOR_MICROTILE(tex->microtile);
+                tex->desc.stride_in_pixels[i] |
+                r300_translate_colorformat(tex->desc.b.b.format) |
+                R300_COLOR_TILE(tex->desc.macrotile[i]) |
+                R300_COLOR_MICROTILE(tex->desc.microtile);
         }
-        tex->fb_state.format = r300_translate_out_fmt(tex->b.b.format);
+        tex->fb_state.format = r300_translate_out_fmt(tex->desc.b.b.format);
     }
 }
 
@@ -625,286 +621,6 @@ void r300_texture_reinterpret_format(struct pipe_screen *screen,
     r300_texture_setup_fb_state(r300_screen(screen), r300_texture(tex));
 }
 
-unsigned r300_texture_get_offset(struct r300_texture* tex, unsigned level,
-                                 unsigned zslice, unsigned face)
-{
-    unsigned offset = tex->offset[level];
-
-    switch (tex->b.b.target) {
-        case PIPE_TEXTURE_3D:
-            assert(face == 0);
-            return offset + zslice * tex->layer_size[level];
-
-        case PIPE_TEXTURE_CUBE:
-            assert(zslice == 0);
-            return offset + face * tex->layer_size[level];
-
-        default:
-            assert(zslice == 0 && face == 0);
-            return offset;
-    }
-}
-
-/* Returns the number of pixels that the texture should be aligned to
- * in the given dimension. */
-static unsigned r300_get_pixel_alignment(struct r300_texture *tex,
-                                         enum r300_buffer_tiling macrotile,
-                                         enum r300_dim dim)
-{
-    static const unsigned table[2][5][3][2] =
-    {
-        {
-    /* Macro: linear    linear    linear
-       Micro: linear    tiled  square-tiled */
-            {{ 32, 1}, { 8,  4}, { 0,  0}}, /*   8 bits per pixel */
-            {{ 16, 1}, { 8,  2}, { 4,  4}}, /*  16 bits per pixel */
-            {{  8, 1}, { 4,  2}, { 0,  0}}, /*  32 bits per pixel */
-            {{  4, 1}, { 0,  0}, { 2,  2}}, /*  64 bits per pixel */
-            {{  2, 1}, { 0,  0}, { 0,  0}}  /* 128 bits per pixel */
-        },
-        {
-    /* Macro: tiled     tiled     tiled
-       Micro: linear    tiled  square-tiled */
-            {{256, 8}, {64, 32}, { 0,  0}}, /*   8 bits per pixel */
-            {{128, 8}, {64, 16}, {32, 32}}, /*  16 bits per pixel */
-            {{ 64, 8}, {32, 16}, { 0,  0}}, /*  32 bits per pixel */
-            {{ 32, 8}, { 0,  0}, {16, 16}}, /*  64 bits per pixel */
-            {{ 16, 8}, { 0,  0}, { 0,  0}}  /* 128 bits per pixel */
-        }
-    };
-    static const unsigned aa_block[2] = {4, 8};
-    unsigned res = 0;
-    unsigned pixsize = util_format_get_blocksize(tex->b.b.format);
-
-    assert(macrotile <= R300_BUFFER_TILED);
-    assert(tex->microtile <= R300_BUFFER_SQUARETILED);
-    assert(pixsize <= 16);
-    assert(dim <= DIM_HEIGHT);
-
-    if (tex->b.b.nr_samples > 1) {
-        /* Multisampled textures have their own alignment scheme. */
-        if (pixsize == 4)
-            res = aa_block[dim];
-    } else {
-        /* Standard alignment. */
-        res = table[macrotile][util_logbase2(pixsize)][tex->microtile][dim];
-    }
-
-    assert(res);
-    return res;
-}
-
-/* Return true if macrotiling should be enabled on the miplevel. */
-static boolean r300_texture_macro_switch(struct r300_texture *tex,
-                                         unsigned level,
-                                         boolean rv350_mode,
-                                         enum r300_dim dim)
-{
-    unsigned tile, texdim;
-
-    tile = r300_get_pixel_alignment(tex, R300_BUFFER_TILED, dim);
-    if (dim == DIM_WIDTH) {
-        texdim = u_minify(tex->b.b.width0, level);
-    } else {
-        texdim = u_minify(tex->b.b.height0, level);
-    }
-
-    /* See TX_FILTER1_n.MACRO_SWITCH. */
-    if (rv350_mode) {
-        return texdim >= tile;
-    } else {
-        return texdim > tile;
-    }
-}
-
-/**
- * Return the stride, in bytes, of the texture images of the given texture
- * at the given level.
- */
-unsigned r300_texture_get_stride(struct r300_screen* screen,
-                                 struct r300_texture* tex, unsigned level)
-{
-    unsigned tile_width, width, stride;
-
-    if (tex->stride_override)
-        return tex->stride_override;
-
-    /* Check the level. */
-    if (level > tex->b.b.last_level) {
-        SCREEN_DBG(screen, DBG_TEX, "%s: level (%u) > last_level (%u)\n",
-                   __FUNCTION__, level, tex->b.b.last_level);
-        return 0;
-    }
-
-    width = u_minify(tex->b.b.width0, level);
-
-    if (util_format_is_plain(tex->b.b.format)) {
-        tile_width = r300_get_pixel_alignment(tex, tex->mip_macrotile[level],
-                                              DIM_WIDTH);
-        width = align(width, tile_width);
-
-        stride = util_format_get_stride(tex->b.b.format, width);
-
-        /* Some IGPs need a minimum stride of 64 bytes, hmm...
-         * This doesn't seem to apply to tiled textures, according to r300c. */
-        if (!tex->microtile && !tex->mip_macrotile[level] &&
-            (screen->caps.family == CHIP_FAMILY_RS600 ||
-             screen->caps.family == CHIP_FAMILY_RS690 ||
-             screen->caps.family == CHIP_FAMILY_RS740)) {
-            return stride < 64 ? 64 : stride;
-        }
-
-        /* The alignment to 32 bytes is sort of implied by the layout... */
-        return stride;
-    } else {
-        return align(util_format_get_stride(tex->b.b.format, width), 32);
-    }
-}
-
-static unsigned r300_texture_get_nblocksy(struct r300_texture* tex,
-                                          unsigned level)
-{
-    unsigned height, tile_height;
-
-    height = u_minify(tex->b.b.height0, level);
-
-    if (util_format_is_plain(tex->b.b.format)) {
-        tile_height = r300_get_pixel_alignment(tex, tex->mip_macrotile[level],
-                                               DIM_HEIGHT);
-        height = align(height, tile_height);
-
-        /* This is needed for the kernel checker, unfortunately. */
-        if ((tex->b.b.target != PIPE_TEXTURE_1D &&
-             tex->b.b.target != PIPE_TEXTURE_2D) ||
-            tex->b.b.last_level != 0) {
-            height = util_next_power_of_two(height);
-        }
-    }
-
-    return util_format_get_nblocksy(tex->b.b.format, height);
-}
-
-static void r300_texture_3d_fix_mipmapping(struct r300_screen *screen,
-                                           struct r300_texture *tex)
-{
-    /* The kernels <= 2.6.34-rc4 compute the size of mipmapped 3D textures
-     * incorrectly. This is a workaround to prevent CS from being rejected. */
-
-    unsigned i, size;
-
-    if (!screen->rws->get_value(screen->rws, R300_VID_DRM_2_3_0) &&
-        tex->b.b.target == PIPE_TEXTURE_3D &&
-        tex->b.b.last_level > 0) {
-        size = 0;
-
-        for (i = 0; i <= tex->b.b.last_level; i++) {
-            size += r300_texture_get_stride(screen, tex, i) *
-                    r300_texture_get_nblocksy(tex, i);
-        }
-
-        size *= tex->b.b.depth0;
-        tex->size = size;
-    }
-}
-
-static void r300_setup_miptree(struct r300_screen* screen,
-                               struct r300_texture* tex)
-{
-    struct pipe_resource* base = &tex->b.b;
-    unsigned stride, size, layer_size, nblocksy, i;
-    boolean rv350_mode = screen->caps.is_rv350;
-
-    SCREEN_DBG(screen, DBG_TEXALLOC,
-        "r300: Making miptree for texture, format %s\n",
-        util_format_short_name(base->format));
-
-    for (i = 0; i <= base->last_level; i++) {
-        /* Let's see if this miplevel can be macrotiled. */
-        tex->mip_macrotile[i] =
-            (tex->macrotile == R300_BUFFER_TILED &&
-             r300_texture_macro_switch(tex, i, rv350_mode, DIM_WIDTH) &&
-             r300_texture_macro_switch(tex, i, rv350_mode, DIM_HEIGHT)) ?
-             R300_BUFFER_TILED : R300_BUFFER_LINEAR;
-
-        stride = r300_texture_get_stride(screen, tex, i);
-        nblocksy = r300_texture_get_nblocksy(tex, i);
-        layer_size = stride * nblocksy;
-
-        if (base->nr_samples) {
-            layer_size *= base->nr_samples;
-        }
-
-        if (base->target == PIPE_TEXTURE_CUBE)
-            size = layer_size * 6;
-        else
-            size = layer_size * u_minify(base->depth0, i);
-
-        tex->offset[i] = tex->size;
-        tex->size = tex->offset[i] + size;
-        tex->layer_size[i] = layer_size;
-        tex->pitch[i] = stride / util_format_get_blocksize(base->format);
-        tex->hwpitch[i] =
-                tex->pitch[i] * util_format_get_blockwidth(base->format);
-
-        SCREEN_DBG(screen, DBG_TEXALLOC, "r300: Texture miptree: Level %d "
-                "(%dx%dx%d px, pitch %d bytes) %d bytes total, macrotiled %s\n",
-                i, u_minify(base->width0, i), u_minify(base->height0, i),
-                u_minify(base->depth0, i), stride, tex->size,
-                tex->mip_macrotile[i] ? "TRUE" : "FALSE");
-    }
-}
-
-static void r300_setup_flags(struct r300_texture* tex)
-{
-    tex->uses_pitch = !util_is_power_of_two(tex->b.b.width0) ||
-                      !util_is_power_of_two(tex->b.b.height0) ||
-                      tex->stride_override;
-}
-
-static void r300_setup_tiling(struct pipe_screen *screen,
-                              struct r300_texture *tex)
-{
-    struct r300_winsys_screen *rws = (struct r300_winsys_screen *)screen->winsys;
-    enum pipe_format format = tex->b.b.format;
-    boolean rv350_mode = r300_screen(screen)->caps.is_rv350;
-    boolean is_zb = util_format_is_depth_or_stencil(format);
-    boolean dbg_no_tiling = SCREEN_DBG_ON(r300_screen(screen), DBG_NO_TILING);
-
-    if (!util_format_is_plain(format)) {
-        return;
-    }
-
-    /* If height == 1, disable microtiling except for zbuffer. */
-    if (!is_zb && (tex->b.b.height0 == 1 || dbg_no_tiling)) {
-        return;
-    }
-
-    /* Set microtiling. */
-    switch (util_format_get_blocksize(format)) {
-        case 1:
-        case 4:
-            tex->microtile = R300_BUFFER_TILED;
-            break;
-
-        case 2:
-        case 8:
-            if (rws->get_value(rws, R300_VID_SQUARE_TILING_SUPPORT)) {
-                tex->microtile = R300_BUFFER_SQUARETILED;
-            }
-            break;
-    }
-
-    if (dbg_no_tiling) {
-        return;
-    }
-
-    /* Set macrotiling. */
-    if (r300_texture_macro_switch(tex, 0, rv350_mode, DIM_WIDTH) &&
-        r300_texture_macro_switch(tex, 0, rv350_mode, DIM_HEIGHT)) {
-        tex->macrotile = R300_BUFFER_TILED;
-    }
-}
-
 static unsigned r300_texture_is_referenced(struct pipe_context *context,
                                         struct pipe_resource *texture,
                                         unsigned face, unsigned level)
@@ -941,11 +657,10 @@ static boolean r300_texture_get_handle(struct pipe_screen* screen,
     }
 
     return rws->buffer_get_handle(rws, tex->buffer,
-                    r300_texture_get_stride(r300_screen(screen), tex, 0),
-                    whandle);
+                                  tex->desc.stride_in_bytes[0], whandle);
 }
 
-struct u_resource_vtbl r300_texture_vtbl = 
+struct u_resource_vtbl r300_texture_vtbl =
 {
    r300_texture_get_handle,          /* get_handle */
    r300_texture_destroy,             /* resource_destroy */
@@ -958,32 +673,69 @@ struct u_resource_vtbl r300_texture_vtbl =
    u_default_transfer_inline_write    /* transfer_inline_write */
 };
 
-static void r300_tex_print_info(struct r300_screen *rscreen, struct r300_texture *tex,
-                                const char *func)
+/* The common texture constructor. */
+static struct r300_texture*
+r300_texture_create_object(struct r300_screen *rscreen,
+                           const struct pipe_resource *base,
+                           enum r300_buffer_tiling microtile,
+                           enum r300_buffer_tiling macrotile,
+                           unsigned stride_in_bytes_override,
+                           unsigned max_buffer_size,
+                           struct r300_winsys_buffer *buffer)
 {
-    fprintf(stderr,
-            "r300: %s: Macro: %s, Micro: %s, Pitch: %i, Dim: %ix%ix%i, "
-            "LastLevel: %i, Size: %i, Format: %s\n",
-            func,
-            tex->macrotile ? "YES" : " NO",
-            tex->microtile ? "YES" : " NO",
-            tex->hwpitch[0],
-            tex->b.b.width0, tex->b.b.height0, tex->b.b.depth0,
-            tex->b.b.last_level, tex->size,
-            util_format_short_name(tex->b.b.format));
+    struct r300_winsys_screen *rws = rscreen->rws;
+    struct r300_texture *tex = CALLOC_STRUCT(r300_texture);
+    if (!tex) {
+        if (buffer)
+            rws->buffer_reference(rws, &buffer, NULL);
+        return NULL;
+    }
+
+    /* Initialize the descriptor. */
+    if (!r300_texture_desc_init(rscreen, &tex->desc, base,
+                                microtile, macrotile,
+                                stride_in_bytes_override,
+                                max_buffer_size)) {
+        if (buffer)
+            rws->buffer_reference(rws, &buffer, NULL);
+        FREE(tex);
+        return NULL;
+    }
+    /* Initialize the hardware state. */
+    r300_texture_setup_immutable_state(rscreen, tex);
+    r300_texture_setup_fb_state(rscreen, tex);
+
+    tex->desc.b.vtbl = &r300_texture_vtbl;
+    pipe_reference_init(&tex->desc.b.b.reference, 1);
+    tex->domain = base->flags & R300_RESOURCE_FLAG_TRANSFER ?
+                  R300_DOMAIN_GTT :
+                  R300_DOMAIN_VRAM | R300_DOMAIN_GTT;
+    tex->buffer = buffer;
+
+    /* Create the backing buffer if needed. */
+    if (!tex->buffer) {
+        tex->buffer = rws->buffer_create(rws, tex->desc.size_in_bytes, 2048,
+                                         base->bind, base->usage, tex->domain);
+
+        if (!tex->buffer) {
+            FREE(tex);
+            return NULL;
+        }
+    }
+
+    rws->buffer_set_tiling(rws, tex->buffer,
+            tex->desc.microtile, tex->desc.macrotile[0],
+            tex->desc.stride_in_bytes[0]);
+
+    return tex;
 }
 
 /* Create a new texture. */
-struct pipe_resource* r300_texture_create(struct pipe_screen* screen,
-                                          const struct pipe_resourcebase)
+struct pipe_resource *r300_texture_create(struct pipe_screen *screen,
+                                          const struct pipe_resource *base)
 {
-    struct r300_texture* tex = CALLOC_STRUCT(r300_texture);
-    struct r300_screen* rscreen = r300_screen(screen);
-    struct r300_winsys_screen *rws = (struct r300_winsys_screen *)screen->winsys;
-
-    if (!tex) {
-        return NULL;
-    }
+    struct r300_screen *rscreen = r300_screen(screen);
+    enum r300_buffer_tiling microtile, macrotile;
 
     /* Refuse to create a texture with size 0. */
     if (!base->width0 ||
@@ -993,53 +745,70 @@ struct pipe_resource* r300_texture_create(struct pipe_screen* screen,
         fprintf(stderr, "r300: texture_create: "
                 "Got invalid texture dimensions: %ix%ix%i\n",
                 base->width0, base->height0, base->depth0);
-        FREE(tex);
         return NULL;
     }
 
-    tex->b.b = *base;
-    tex->b.vtbl = &r300_texture_vtbl;
-    pipe_reference_init(&tex->b.b.reference, 1);
-    tex->b.b.screen = screen;
-
-    r300_setup_flags(tex);
-    if (!(base->flags & R300_RESOURCE_FLAG_TRANSFER) &&
-        !(base->bind & PIPE_BIND_SCANOUT)) {
-        r300_setup_tiling(screen, tex);
+    if ((base->flags & R300_RESOURCE_FLAG_TRANSFER) ||
+        (base->bind & PIPE_BIND_SCANOUT)) {
+        microtile = R300_BUFFER_LINEAR;
+        macrotile = R300_BUFFER_LINEAR;
+    } else {
+        microtile = R300_BUFFER_SELECT_LAYOUT;
+        macrotile = R300_BUFFER_SELECT_LAYOUT;
     }
-    r300_setup_miptree(rscreen, tex);
-    r300_texture_3d_fix_mipmapping(rscreen, tex);
-    r300_texture_setup_immutable_state(rscreen, tex);
-    r300_texture_setup_fb_state(rscreen, tex);
 
-    if (SCREEN_DBG_ON(rscreen, DBG_TEX)) {
-        r300_tex_print_info(rscreen, tex, "texture_create");
+    return (struct pipe_resource*)
+           r300_texture_create_object(rscreen, base, microtile, macrotile,
+                                      0, 0, NULL);
+}
+
+struct pipe_resource *r300_texture_from_handle(struct pipe_screen *screen,
+                                               const struct pipe_resource *base,
+                                               struct winsys_handle *whandle)
+{
+    struct r300_winsys_screen *rws = (struct r300_winsys_screen*)screen->winsys;
+    struct r300_screen *rscreen = r300_screen(screen);
+    struct r300_winsys_buffer *buffer;
+    enum r300_buffer_tiling microtile, macrotile;
+    unsigned stride, size;
+
+    /* Support only 2D textures without mipmaps */
+    if (base->target != PIPE_TEXTURE_2D ||
+        base->depth0 != 1 ||
+        base->last_level != 0) {
+        return NULL;
     }
 
-    tex->domain = base->flags & R300_RESOURCE_FLAG_TRANSFER ?
-                  R300_DOMAIN_GTT :
-                  R300_DOMAIN_VRAM | R300_DOMAIN_GTT;
+    buffer = rws->buffer_from_handle(rws, whandle, &stride, &size);
+    if (!buffer)
+        return NULL;
 
-    tex->buffer = rws->buffer_create(rws, tex->size, 2048, base->bind,
-                                     base->usage, tex->domain);
-    tex->buffer_size = tex->size;
+    rws->buffer_get_tiling(rws, buffer, &microtile, &macrotile);
 
-    if (!tex->buffer) {
-       FREE(tex);
-       return NULL;
-    }
+    /* Enforce a microtiled zbuffer. */
+    if (util_format_is_depth_or_stencil(base->format) &&
+        microtile == R300_BUFFER_LINEAR) {
+        switch (util_format_get_blocksize(base->format)) {
+            case 4:
+                microtile = R300_BUFFER_TILED;
+                break;
 
-    rws->buffer_set_tiling(rws, tex->buffer,
-            tex->microtile, tex->macrotile,
-            tex->pitch[0] * util_format_get_blocksize(tex->b.b.format));
+            case 2:
+                if (rws->get_value(rws, R300_VID_SQUARE_TILING_SUPPORT))
+                    microtile = R300_BUFFER_SQUARETILED;
+                break;
+        }
+    }
 
-    return (struct pipe_resource*)tex;
+    return (struct pipe_resource*)
+           r300_texture_create_object(rscreen, base, microtile, macrotile,
+                                      stride, size, buffer);
 }
 
 /* Not required to implement u_resource_vtbl, consider moving to another file:
  */
 struct pipe_surface* r300_get_tex_surface(struct pipe_screen* screen,
-                                         struct pipe_resource* texture,
+                                          struct pipe_resource* texture,
                                          unsigned face,
                                          unsigned level,
                                          unsigned zslice,
@@ -1049,7 +818,7 @@ struct pipe_surface* r300_get_tex_surface(struct pipe_screen* screen,
     struct r300_surface* surface = CALLOC_STRUCT(r300_surface);
 
     if (surface) {
-        uint32_t stride, offset, tile_height;
+        uint32_t offset, tile_height;
 
         pipe_reference_init(&surface->base.reference, 1);
         pipe_resource_reference(&surface->base.texture, texture);
@@ -1068,23 +837,29 @@ struct pipe_surface* r300_get_tex_surface(struct pipe_screen* screen,
         if (surface->domain & R300_DOMAIN_VRAM)
             surface->domain &= ~R300_DOMAIN_GTT;
 
-        surface->offset = r300_texture_get_offset(tex, level, zslice, face);
+        surface->offset = r300_texture_get_offset(&tex->desc,
+                                                  level, zslice, face);
         surface->pitch = tex->fb_state.pitch[level];
         surface->format = tex->fb_state.format;
 
         /* Parameters for the CBZB clear. */
+        surface->cbzb_allowed = tex->desc.cbzb_allowed[level];
         surface->cbzb_width = align(surface->base.width, 64);
 
         /* Height must be aligned to the size of a tile. */
-        tile_height = r300_get_pixel_alignment(tex, tex->mip_macrotile[level],
+        tile_height = r300_get_pixel_alignment(tex->desc.b.b.format,
+                                               tex->desc.b.b.nr_samples,
+                                               tex->desc.microtile,
+                                               tex->desc.macrotile[level],
                                                DIM_HEIGHT);
+
         surface->cbzb_height = align((surface->base.height + 1) / 2,
                                      tile_height);
 
         /* Offset must be aligned to 2K and must point at the beginning
          * of a scanline. */
-        stride = r300_texture_get_stride(r300_screen(screen), tex, level);
-        offset = surface->offset + stride * surface->cbzb_height;
+        offset = surface->offset +
+                 tex->desc.stride_in_bytes[level] * surface->cbzb_height;
         surface->cbzb_midpoint_offset = offset & ~2047;
 
         surface->cbzb_pitch = surface->pitch & 0x1ffffc;
@@ -1098,7 +873,7 @@ struct pipe_surface* r300_get_tex_surface(struct pipe_screen* screen,
                    "CBZB Dim: %ix%i, Misalignment: %i, Macro: %s\n",
                    surface->cbzb_width, surface->cbzb_height,
                    offset & 2047,
-                   tex->mip_macrotile[level] ? "YES" : " NO");
+                   tex->desc.macrotile[level] ? "YES" : " NO");
     }
 
     return &surface->base;
@@ -1111,95 +886,3 @@ void r300_tex_surface_destroy(struct pipe_surface* s)
     pipe_resource_reference(&s->texture, NULL);
     FREE(s);
 }
-
-struct pipe_resource*
-r300_texture_from_handle(struct pipe_screen* screen,
-                         const struct pipe_resource* base,
-                         struct winsys_handle *whandle)
-{
-    struct r300_winsys_screen *rws = (struct r300_winsys_screen*)screen->winsys;
-    struct r300_screen* rscreen = r300_screen(screen);
-    struct r300_winsys_buffer *buffer;
-    struct r300_texture* tex;
-    unsigned stride, size;
-    boolean override_zb_flags;
-
-    /* Support only 2D textures without mipmaps */
-    if (base->target != PIPE_TEXTURE_2D ||
-        base->depth0 != 1 ||
-        base->last_level != 0) {
-        return NULL;
-    }
-
-    buffer = rws->buffer_from_handle(rws, whandle, &stride, &size);
-    if (!buffer) {
-        return NULL;
-    }
-
-    tex = CALLOC_STRUCT(r300_texture);
-    if (!tex) {
-        return NULL;
-    }
-
-    tex->b.b = *base;
-    tex->b.vtbl = &r300_texture_vtbl;
-    pipe_reference_init(&tex->b.b.reference, 1);
-    tex->b.b.screen = screen;
-    tex->domain = R300_DOMAIN_VRAM;
-
-    tex->stride_override = stride;
-
-    /* one ref already taken */
-    tex->buffer = buffer;
-    tex->buffer_size = size;
-
-    rws->buffer_get_tiling(rws, buffer, &tex->microtile, &tex->macrotile);
-    r300_setup_flags(tex);
-
-    /* Enforce microtiled zbuffer. */
-    override_zb_flags = util_format_is_depth_or_stencil(base->format) &&
-                        tex->microtile == R300_BUFFER_LINEAR;
-
-    if (override_zb_flags) {
-        switch (util_format_get_blocksize(base->format)) {
-            case 4:
-                tex->microtile = R300_BUFFER_TILED;
-                break;
-
-            case 2:
-                if (rws->get_value(rws, R300_VID_SQUARE_TILING_SUPPORT)) {
-                    tex->microtile = R300_BUFFER_SQUARETILED;
-                    break;
-                }
-                /* Pass through. */
-
-            default:
-                override_zb_flags = FALSE;
-        }
-    }
-
-    r300_setup_miptree(rscreen, tex);
-    r300_texture_setup_immutable_state(rscreen, tex);
-    r300_texture_setup_fb_state(rscreen, tex);
-
-    if (override_zb_flags) {
-        rws->buffer_set_tiling(rws, tex->buffer,
-                tex->microtile, tex->macrotile,
-                tex->pitch[0] * util_format_get_blocksize(tex->b.b.format));
-    }
-
-    /* Make sure the buffer we got is large enough. */
-    if (tex->size > tex->buffer_size) {
-        fprintf(stderr, "r300: texture_from_handle: The buffer is not "
-                        "large enough. Got: %i, Need: %i, Info:\n",
-                        tex->buffer_size, tex->size);
-        r300_tex_print_info(rscreen, tex, "texture_from_handle");
-        pipe_resource_reference((struct pipe_resource**)&tex, NULL);
-        return NULL;
-    } else {
-        if (SCREEN_DBG_ON(rscreen, DBG_TEX))
-            r300_tex_print_info(rscreen, tex, "texture_from_handle");
-    }
-
-    return (struct pipe_resource*)tex;
-}
index 99e7694254e071d5daac13e92545c1a33711e6e4..585036ab3b3db5da920f4b0390c32c676a5ea739 100644 (file)
@@ -39,12 +39,6 @@ uint32_t r300_translate_texformat(enum pipe_format format,
 
 uint32_t r500_tx_format_msb_bit(enum pipe_format format);
 
-unsigned r300_texture_get_stride(struct r300_screen* screen,
-                                 struct r300_texture* tex, unsigned level);
-
-unsigned r300_texture_get_offset(struct r300_texture* tex, unsigned level,
-                                 unsigned zslice, unsigned face);
-
 void r300_texture_reinterpret_format(struct pipe_screen *screen,
                                      struct pipe_resource *tex,
                                      enum pipe_format new_format);
diff --git a/src/gallium/drivers/r300/r300_texture_desc.c b/src/gallium/drivers/r300/r300_texture_desc.c
new file mode 100644 (file)
index 0000000..18a2bd3
--- /dev/null
@@ -0,0 +1,456 @@
+/*
+ * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
+ * Copyright 2010 Marek Olšák <maraeo@gmail.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * on the rights to use, copy, modify, merge, publish, distribute, sub
+ * license, and/or sell copies of the Software, and to permit persons to whom
+ * the Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * 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
+ * THE AUTHOR(S) AND/OR THEIR 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. */
+
+#include "r300_texture_desc.h"
+
+#include "r300_context.h"
+#include "r300_winsys.h"
+
+#include "util/u_format.h"
+
+/* Returns the number of pixels that the texture should be aligned to
+ * in the given dimension. */
+unsigned r300_get_pixel_alignment(enum pipe_format format,
+                                  unsigned num_samples,
+                                  enum r300_buffer_tiling microtile,
+                                  enum r300_buffer_tiling macrotile,
+                                  enum r300_dim dim)
+{
+    static const unsigned table[2][5][3][2] =
+    {
+        {
+    /* Macro: linear    linear    linear
+       Micro: linear    tiled  square-tiled */
+            {{ 32, 1}, { 8,  4}, { 0,  0}}, /*   8 bits per pixel */
+            {{ 16, 1}, { 8,  2}, { 4,  4}}, /*  16 bits per pixel */
+            {{  8, 1}, { 4,  2}, { 0,  0}}, /*  32 bits per pixel */
+            {{  4, 1}, { 0,  0}, { 2,  2}}, /*  64 bits per pixel */
+            {{  2, 1}, { 0,  0}, { 0,  0}}  /* 128 bits per pixel */
+        },
+        {
+    /* Macro: tiled     tiled     tiled
+       Micro: linear    tiled  square-tiled */
+            {{256, 8}, {64, 32}, { 0,  0}}, /*   8 bits per pixel */
+            {{128, 8}, {64, 16}, {32, 32}}, /*  16 bits per pixel */
+            {{ 64, 8}, {32, 16}, { 0,  0}}, /*  32 bits per pixel */
+            {{ 32, 8}, { 0,  0}, {16, 16}}, /*  64 bits per pixel */
+            {{ 16, 8}, { 0,  0}, { 0,  0}}  /* 128 bits per pixel */
+        }
+    };
+    static const unsigned aa_block[2] = {4, 8};
+    unsigned tile = 0;
+    unsigned pixsize = util_format_get_blocksize(format);
+
+    assert(macrotile <= R300_BUFFER_TILED);
+    assert(microtile <= R300_BUFFER_SQUARETILED);
+    assert(pixsize <= 16);
+    assert(dim <= DIM_HEIGHT);
+
+    if (num_samples > 1) {
+        /* Multisampled textures have their own alignment scheme. */
+        if (pixsize == 4)
+            tile = aa_block[dim];
+        /* XXX FP16 AA. */
+    } else {
+        /* Standard alignment. */
+        tile = table[macrotile][util_logbase2(pixsize)][microtile][dim];
+    }
+
+    assert(tile);
+    return tile;
+}
+
+/* Return true if macrotiling should be enabled on the miplevel. */
+static boolean r300_texture_macro_switch(struct r300_texture_desc *desc,
+                                         unsigned level,
+                                         boolean rv350_mode,
+                                         enum r300_dim dim)
+{
+    unsigned tile, texdim;
+
+    tile = r300_get_pixel_alignment(desc->b.b.format, desc->b.b.nr_samples,
+                                    desc->microtile, R300_BUFFER_TILED, dim);
+    if (dim == DIM_WIDTH) {
+        texdim = u_minify(desc->b.b.width0, level);
+    } else {
+        texdim = u_minify(desc->b.b.height0, level);
+    }
+
+    /* See TX_FILTER1_n.MACRO_SWITCH. */
+    if (rv350_mode) {
+        return texdim >= tile;
+    } else {
+        return texdim > tile;
+    }
+}
+
+/**
+ * Return the stride, in bytes, of the texture image of the given texture
+ * at the given level.
+ */
+static unsigned r300_texture_get_stride(struct r300_screen *screen,
+                                        struct r300_texture_desc *desc,
+                                        unsigned level)
+{
+    unsigned tile_width, width, stride;
+
+    if (desc->stride_in_bytes_override)
+        return desc->stride_in_bytes_override;
+
+    /* Check the level. */
+    if (level > desc->b.b.last_level) {
+        SCREEN_DBG(screen, DBG_TEX, "%s: level (%u) > last_level (%u)\n",
+                   __FUNCTION__, level, desc->b.b.last_level);
+        return 0;
+    }
+
+    width = u_minify(desc->b.b.width0, level);
+
+    if (util_format_is_plain(desc->b.b.format)) {
+        tile_width = r300_get_pixel_alignment(desc->b.b.format,
+                                              desc->b.b.nr_samples,
+                                              desc->microtile,
+                                              desc->macrotile[level],
+                                              DIM_WIDTH);
+        width = align(width, tile_width);
+
+        stride = util_format_get_stride(desc->b.b.format, width);
+
+        /* Some IGPs need a minimum stride of 64 bytes, hmm...
+         * This doesn't seem to apply to tiled textures, according to r300c. */
+        if (!desc->microtile && !desc->macrotile[level] &&
+            (screen->caps.family == CHIP_FAMILY_RS600 ||
+             screen->caps.family == CHIP_FAMILY_RS690 ||
+             screen->caps.family == CHIP_FAMILY_RS740)) {
+            return stride < 64 ? 64 : stride;
+        }
+
+        /* The alignment to 32 bytes is sort of implied by the layout... */
+        return stride;
+    } else {
+        return align(util_format_get_stride(desc->b.b.format, width), 32);
+    }
+}
+
+static unsigned r300_texture_get_nblocksy(struct r300_texture_desc *desc,
+                                          unsigned level,
+                                          boolean align_for_cbzb)
+{
+    unsigned height, tile_height;
+
+    height = u_minify(desc->b.b.height0, level);
+
+    if (util_format_is_plain(desc->b.b.format)) {
+        tile_height = r300_get_pixel_alignment(desc->b.b.format,
+                                               desc->b.b.nr_samples,
+                                               desc->microtile,
+                                               desc->macrotile[level],
+                                               DIM_HEIGHT);
+        height = align(height, tile_height);
+
+        /* This is needed for the kernel checker, unfortunately. */
+        if ((desc->b.b.target != PIPE_TEXTURE_1D &&
+             desc->b.b.target != PIPE_TEXTURE_2D) ||
+            desc->b.b.last_level != 0) {
+            height = util_next_power_of_two(height);
+        }
+
+        /* Allocate an even number of macrotiles for the CBZB clear.
+         * Do so for 3 or more macrotiles in the Y direction. */
+        if (align_for_cbzb &&
+            level == 0 && desc->b.b.last_level == 0 &&
+            desc->macrotile[0] && height >= tile_height * 3) {
+            height = align(height, tile_height * 2);
+        }
+    }
+
+    return util_format_get_nblocksy(desc->b.b.format, height);
+}
+
+static void r300_texture_3d_fix_mipmapping(struct r300_screen *screen,
+                                           struct r300_texture_desc *desc)
+{
+    /* The kernels <= 2.6.34-rc4 compute the size of mipmapped 3D textures
+     * incorrectly. This is a workaround to prevent CS from being rejected. */
+
+    unsigned i, size;
+
+    if (!screen->rws->get_value(screen->rws, R300_VID_DRM_2_3_0) &&
+        desc->b.b.target == PIPE_TEXTURE_3D &&
+        desc->b.b.last_level > 0) {
+        size = 0;
+
+        for (i = 0; i <= desc->b.b.last_level; i++) {
+            size += desc->stride_in_bytes[i] *
+                    r300_texture_get_nblocksy(desc, i, FALSE);
+        }
+
+        size *= desc->b.b.depth0;
+        desc->size_in_bytes = size;
+    }
+}
+
+static void r300_setup_miptree(struct r300_screen *screen,
+                               struct r300_texture_desc *desc)
+{
+    struct pipe_resource *base = &desc->b.b;
+    unsigned stride, size, layer_size, nblocksy, i;
+    boolean rv350_mode = screen->caps.is_rv350;
+
+    SCREEN_DBG(screen, DBG_TEXALLOC,
+        "r300: Making miptree for texture, format %s\n",
+        util_format_short_name(base->format));
+
+    for (i = 0; i <= base->last_level; i++) {
+        /* Let's see if this miplevel can be macrotiled. */
+        desc->macrotile[i] =
+            (desc->macrotile[0] == R300_BUFFER_TILED &&
+             r300_texture_macro_switch(desc, i, rv350_mode, DIM_WIDTH) &&
+             r300_texture_macro_switch(desc, i, rv350_mode, DIM_HEIGHT)) ?
+             R300_BUFFER_TILED : R300_BUFFER_LINEAR;
+
+        stride = r300_texture_get_stride(screen, desc, i);
+        nblocksy = r300_texture_get_nblocksy(desc, i, desc->stride_in_bytes_override == 0);
+        layer_size = stride * nblocksy;
+
+        if (base->nr_samples) {
+            layer_size *= base->nr_samples;
+        }
+
+        if (base->target == PIPE_TEXTURE_CUBE)
+            size = layer_size * 6;
+        else
+            size = layer_size * u_minify(base->depth0, i);
+
+        desc->offset_in_bytes[i] = desc->size_in_bytes;
+        desc->size_in_bytes = desc->offset_in_bytes[i] + size;
+        desc->layer_size_in_bytes[i] = layer_size;
+        desc->stride_in_bytes[i] = stride;
+        desc->stride_in_pixels[i] =
+                (stride / util_format_get_blocksize(base->format)) *
+                util_format_get_blockwidth(base->format);
+
+        SCREEN_DBG(screen, DBG_TEXALLOC, "r300: Texture miptree: Level %d "
+                "(%dx%dx%d px, pitch %d bytes) %d bytes total, macrotiled %s\n",
+                i, u_minify(base->width0, i), u_minify(base->height0, i),
+                u_minify(base->depth0, i), stride, desc->size_in_bytes,
+                desc->macrotile[i] ? "TRUE" : "FALSE");
+    }
+}
+
+static void r300_setup_flags(struct r300_texture_desc *desc)
+{
+    desc->uses_stride_addressing =
+            !util_is_power_of_two(desc->b.b.width0) ||
+            !util_is_power_of_two(desc->b.b.height0) ||
+            desc->stride_in_bytes_override;
+}
+
+static void r300_setup_cbzb_flags(struct r300_screen *rscreen,
+                                  struct r300_texture_desc *desc)
+{
+    unsigned i, bpp;
+    boolean first_level_valid;
+
+    bpp = util_format_get_blocksizebits(desc->b.b.format);
+
+    /* 1) The texture must be point-sampled,
+     * 2) The depth must be 16 or 32 bits.
+     * 3) If the midpoint ZB offset is not aligned to 2048, it returns garbage
+     *    with certain texture sizes. Macrotiling ensures the alignment. */
+    first_level_valid = desc->b.b.nr_samples <= 1 &&
+                       (bpp == 16 || bpp == 32) &&
+                       desc->macrotile[0];
+
+    for (i = 0; i <= desc->b.b.last_level; i++)
+        desc->cbzb_allowed[i] = first_level_valid && desc->macrotile[i];
+    return;
+#if 0
+    /* When clearing, the layer (width*height) is horizontally split
+     * into two, and the upper and lower halves are cleared by the CB
+     * and ZB units, respectively. Therefore, the number of macrotiles
+     * in the Y direction must be even. */
+
+    if (desc->b.b.last_level > 0 ||
+        desc->b.b.target == PIPE_TEXTURE_3D ||
+        desc->b.b.target == PIPE_TEXTURE_CUBE) {
+        /* For mipmapped, 3D, or cube textures, just check if there are
+         * enough macrotiles per layer. */
+        for (i = 0; i <= desc->b.b.last_level; i++) {
+            desc->cbzb_allowed[i] = FALSE;
+
+            if (first_level_valid && desc->macrotile[i]) {
+                unsigned height, tile_height, num_macrotiles;
+
+                /* Compute the number of macrotiles in the Y direction. */
+                tile_height = r300_get_pixel_alignment(desc->b.b.format,
+                                                       desc->b.b.nr_samples,
+                                                       desc->microtile,
+                                                       R300_BUFFER_TILED,
+                                                       DIM_HEIGHT);
+                height = r300_texture_get_height(desc, i);
+                num_macrotiles = height / tile_height;
+
+                desc->cbzb_allowed[i] = num_macrotiles % 2 == 0;
+            }
+        }
+    } else {
+        /* For 1D and 2D non-mipmapped textures */
+        unsigned layer_size;
+
+        layer_size = desc->stride_in_bytes[0] *
+                     r300_texture_get_nblocksy(desc, 0, TRUE);
+    }
+#endif
+}
+
+static void r300_setup_tiling(struct r300_screen *screen,
+                              struct r300_texture_desc *desc)
+{
+    struct r300_winsys_screen *rws = screen->rws;
+    enum pipe_format format = desc->b.b.format;
+    boolean rv350_mode = screen->caps.is_rv350;
+    boolean is_zb = util_format_is_depth_or_stencil(format);
+    boolean dbg_no_tiling = SCREEN_DBG_ON(screen, DBG_NO_TILING);
+
+    if (!util_format_is_plain(format)) {
+        return;
+    }
+
+    /* If height == 1, disable microtiling except for zbuffer. */
+    if (!is_zb && (desc->b.b.height0 == 1 || dbg_no_tiling)) {
+        return;
+    }
+
+    /* Set microtiling. */
+    switch (util_format_get_blocksize(format)) {
+        case 1:
+        case 4:
+            desc->microtile = R300_BUFFER_TILED;
+            break;
+
+        case 2:
+        case 8:
+            if (rws->get_value(rws, R300_VID_SQUARE_TILING_SUPPORT)) {
+                desc->microtile = R300_BUFFER_SQUARETILED;
+            }
+            break;
+    }
+
+    if (dbg_no_tiling) {
+        return;
+    }
+
+    /* Set macrotiling. */
+    if (r300_texture_macro_switch(desc, 0, rv350_mode, DIM_WIDTH) &&
+        r300_texture_macro_switch(desc, 0, rv350_mode, DIM_HEIGHT)) {
+        desc->macrotile[0] = R300_BUFFER_TILED;
+    }
+}
+
+static void r300_tex_print_info(struct r300_screen *rscreen,
+                                struct r300_texture_desc *desc,
+                                const char *func)
+{
+    fprintf(stderr,
+            "r300: %s: Macro: %s, Micro: %s, Pitch: %i, Dim: %ix%ix%i, "
+            "LastLevel: %i, Size: %i, Format: %s\n",
+            func,
+            desc->macrotile[0] ? "YES" : " NO",
+            desc->microtile ? "YES" : " NO",
+            desc->stride_in_pixels[0],
+            desc->b.b.width0, desc->b.b.height0, desc->b.b.depth0,
+            desc->b.b.last_level, desc->size_in_bytes,
+            util_format_short_name(desc->b.b.format));
+}
+
+boolean r300_texture_desc_init(struct r300_screen *rscreen,
+                               struct r300_texture_desc *desc,
+                               const struct pipe_resource *base,
+                               enum r300_buffer_tiling microtile,
+                               enum r300_buffer_tiling macrotile,
+                               unsigned stride_in_bytes_override,
+                               unsigned max_buffer_size)
+{
+    desc->b.b = *base;
+    desc->b.b.screen = &rscreen->screen;
+
+    desc->stride_in_bytes_override = stride_in_bytes_override;
+
+    r300_setup_flags(desc);
+
+    if (microtile == R300_BUFFER_SELECT_LAYOUT ||
+        macrotile == R300_BUFFER_SELECT_LAYOUT) {
+        r300_setup_tiling(rscreen, desc);
+    } else {
+        desc->microtile = microtile;
+        desc->macrotile[0] = macrotile;
+        assert(desc->b.b.last_level == 0);
+    }
+
+    r300_setup_miptree(rscreen, desc);
+    r300_texture_3d_fix_mipmapping(rscreen, desc);
+    r300_setup_cbzb_flags(rscreen, desc);
+
+    if (max_buffer_size) {
+        /* Make sure the buffer we got is large enough. */
+        if (desc->size_in_bytes > max_buffer_size) {
+            fprintf(stderr, "r300: texture_from_handle: The buffer is not "
+                            "large enough. Got: %i, Need: %i, Info:\n",
+                            max_buffer_size, desc->size_in_bytes);
+            r300_tex_print_info(rscreen, desc, "texture_from_handle");
+            return FALSE;
+        }
+
+        desc->buffer_size_in_bytes = max_buffer_size;
+    } else {
+        desc->buffer_size_in_bytes = desc->size_in_bytes;
+
+    }
+
+    if (SCREEN_DBG_ON(rscreen, DBG_TEX))
+        r300_tex_print_info(rscreen, desc, "texture_from_handle");
+
+    return TRUE;
+}
+
+unsigned r300_texture_get_offset(struct r300_texture_desc *desc,
+                                 unsigned level, unsigned zslice,
+                                 unsigned face)
+{
+    unsigned offset = desc->offset_in_bytes[level];
+
+    switch (desc->b.b.target) {
+        case PIPE_TEXTURE_3D:
+            assert(face == 0);
+            return offset + zslice * desc->layer_size_in_bytes[level];
+
+        case PIPE_TEXTURE_CUBE:
+            assert(zslice == 0);
+            return offset + face * desc->layer_size_in_bytes[level];
+
+        default:
+            assert(zslice == 0 && face == 0);
+            return offset;
+    }
+}
diff --git a/src/gallium/drivers/r300/r300_texture_desc.h b/src/gallium/drivers/r300/r300_texture_desc.h
new file mode 100644 (file)
index 0000000..95de66f
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
+ * Copyright 2010 Marek Olšák <maraeo@gmail.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * on the rights to use, copy, modify, merge, publish, distribute, sub
+ * license, and/or sell copies of the Software, and to permit persons to whom
+ * the Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * 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
+ * THE AUTHOR(S) AND/OR THEIR 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. */
+
+#ifndef R300_TEXTURE_DESC_H
+#define R300_TEXTURE_DESC_H
+
+#include "r300_defines.h"
+
+struct pipe_resource;
+struct r300_screen;
+struct r300_texture_desc;
+struct r300_texture;
+
+enum r300_dim {
+    DIM_WIDTH  = 0,
+    DIM_HEIGHT = 1
+};
+
+unsigned r300_get_pixel_alignment(enum pipe_format format,
+                                  unsigned num_samples,
+                                  enum r300_buffer_tiling microtile,
+                                  enum r300_buffer_tiling macrotile,
+                                  enum r300_dim dim);
+
+boolean r300_texture_desc_init(struct r300_screen *rscreen,
+                               struct r300_texture_desc *desc,
+                               const struct pipe_resource *base,
+                               enum r300_buffer_tiling microtile,
+                               enum r300_buffer_tiling macrotile,
+                               unsigned stride_in_bytes_override,
+                               unsigned max_buffer_size);
+
+unsigned r300_texture_get_offset(struct r300_texture_desc *desc,
+                                 unsigned level, unsigned zslice,
+                                 unsigned face);
+
+#endif
index 3cc4c8c958279f3cb9b3dd52b5e7a1c7e8878f30..e9333b35ef53be62420294be95fcc8859f0903b3 100644 (file)
@@ -22,7 +22,7 @@
  * USE OR OTHER DEALINGS IN THE SOFTWARE. */
 
 #include "r300_transfer.h"
-#include "r300_texture.h"
+#include "r300_texture_desc.h"
 #include "r300_screen_buffer.h"
 
 #include "util/u_memory.h"
@@ -35,8 +35,8 @@ struct r300_transfer {
     /* Offset from start of buffer. */
     unsigned offset;
 
-    /* Detiled texture. */
-    struct r300_texture *detiled_texture;
+    /* Linear texture. */
+    struct r300_texture *linear_texture;
 };
 
 /* Convenience cast wrapper. */
@@ -57,7 +57,7 @@ static void r300_copy_from_tiled_texture(struct pipe_context *ctx,
     subdst.face = 0;
     subdst.level = 0;
 
-    ctx->resource_copy_region(ctx, &r300transfer->detiled_texture->b.b, subdst,
+    ctx->resource_copy_region(ctx, &r300transfer->linear_texture->desc.b.b, subdst,
                              0, 0, 0,
                              tex, transfer->sr,
                              transfer->box.x, transfer->box.y, transfer->box.z,
@@ -77,7 +77,7 @@ static void r300_copy_into_tiled_texture(struct pipe_context *ctx,
 
     ctx->resource_copy_region(ctx, tex, transfer->sr,
                              transfer->box.x, transfer->box.y, transfer->box.z,
-                             &r300transfer->detiled_texture->b.b, subsrc,
+                              &r300transfer->linear_texture->desc.b.b, subsrc,
                              0, 0, 0,
                              transfer->box.width, transfer->box.height);
 
@@ -93,7 +93,6 @@ r300_texture_get_transfer(struct pipe_context *ctx,
 {
     struct r300_context *r300 = r300_context(ctx);
     struct r300_texture *tex = r300_texture(texture);
-    struct r300_screen *r300screen = r300_screen(ctx->screen);
     struct r300_transfer *trans;
     struct pipe_resource base;
     boolean referenced_cs, referenced_hw, blittable;
@@ -124,7 +123,7 @@ r300_texture_get_transfer(struct pipe_context *ctx,
         /* If the texture is tiled, we must create a temporary detiled texture
          * for this transfer.
          * Also make write transfers pipelined. */
-        if (tex->microtile || tex->macrotile ||
+        if (tex->desc.microtile || tex->desc.macrotile[sr.level] ||
             ((referenced_hw & !(usage & PIPE_TRANSFER_READ)) && blittable)) {
             base.target = PIPE_TEXTURE_2D;
             base.format = texture->format;
@@ -149,23 +148,23 @@ r300_texture_get_transfer(struct pipe_context *ctx,
             }
 
             /* Create the temporary texture. */
-            trans->detiled_texture = r300_texture(
+            trans->linear_texture = r300_texture(
                ctx->screen->resource_create(ctx->screen,
                                             &base));
 
-            if (!trans->detiled_texture) {
+            if (!trans->linear_texture) {
                 /* Oh crap, the thing can't create the texture.
                  * Let's flush and try again. */
                 ctx->flush(ctx, 0, NULL);
 
-                trans->detiled_texture = r300_texture(
+                trans->linear_texture = r300_texture(
                    ctx->screen->resource_create(ctx->screen,
                                                 &base));
 
-                if (!trans->detiled_texture) {
+                if (!trans->linear_texture) {
                     /* For linear textures, it's safe to fallback to
                      * an unpipelined transfer. */
-                    if (!tex->microtile && !tex->macrotile) {
+                    if (!tex->desc.microtile && !tex->desc.macrotile[sr.level]) {
                         goto unpipelined;
                     }
 
@@ -177,8 +176,8 @@ r300_texture_get_transfer(struct pipe_context *ctx,
                 }
             }
 
-            assert(!trans->detiled_texture->microtile &&
-                   !trans->detiled_texture->macrotile);
+            assert(!trans->linear_texture->desc.microtile &&
+                   !trans->linear_texture->desc.macrotile[0]);
 
             /* Set the stride.
             *
@@ -188,7 +187,7 @@ r300_texture_get_transfer(struct pipe_context *ctx,
             * right thing internally.
             */
             trans->transfer.stride =
-                r300_texture_get_stride(r300screen, trans->detiled_texture, 0);
+                    trans->linear_texture->desc.stride_in_bytes[0];
 
             if (usage & PIPE_TRANSFER_READ) {
                 /* We cannot map a tiled texture directly because the data is
@@ -203,9 +202,9 @@ r300_texture_get_transfer(struct pipe_context *ctx,
 
     unpipelined:
         /* Unpipelined transfer. */
-        trans->transfer.stride =
-                r300_texture_get_stride(r300screen, tex, sr.level);
-        trans->offset = r300_texture_get_offset(tex, sr.level, box->z, sr.face);
+        trans->transfer.stride = tex->desc.stride_in_bytes[sr.level];
+        trans->offset = r300_texture_get_offset(&tex->desc,
+                                                sr.level, box->z, sr.face);
 
         if (referenced_cs)
             ctx->flush(ctx, PIPE_FLUSH_RENDER_CACHE, NULL);
@@ -219,13 +218,13 @@ void r300_texture_transfer_destroy(struct pipe_context *ctx,
 {
     struct r300_transfer *r300transfer = r300_transfer(trans);
 
-    if (r300transfer->detiled_texture) {
+    if (r300transfer->linear_texture) {
         if (trans->usage & PIPE_TRANSFER_WRITE) {
             r300_copy_into_tiled_texture(ctx, r300transfer);
         }
 
         pipe_resource_reference(
-            (struct pipe_resource**)&r300transfer->detiled_texture, NULL);
+            (struct pipe_resource**)&r300transfer->linear_texture, NULL);
     }
     pipe_resource_reference(&trans->resource, NULL);
     FREE(trans);
@@ -239,13 +238,13 @@ void* r300_texture_transfer_map(struct pipe_context *ctx,
     struct r300_transfer *r300transfer = r300_transfer(transfer);
     struct r300_texture *tex = r300_texture(transfer->resource);
     char *map;
-    enum pipe_format format = tex->b.b.format;
+    enum pipe_format format = tex->desc.b.b.format;
 
-    if (r300transfer->detiled_texture) {
+    if (r300transfer->linear_texture) {
         /* The detiled texture is of the same size as the region being mapped
          * (no offset needed). */
         return rws->buffer_map(rws,
-                               r300transfer->detiled_texture->buffer,
+                               r300transfer->linear_texture->buffer,
                                r300->cs,
                                transfer->usage);
     } else {
@@ -270,8 +269,8 @@ void r300_texture_transfer_unmap(struct pipe_context *ctx,
     struct r300_transfer *r300transfer = r300_transfer(transfer);
     struct r300_texture *tex = r300_texture(transfer->resource);
 
-    if (r300transfer->detiled_texture) {
-       rws->buffer_unmap(rws, r300transfer->detiled_texture->buffer);
+    if (r300transfer->linear_texture) {
+        rws->buffer_unmap(rws, r300transfer->linear_texture->buffer);
     } else {
         rws->buffer_unmap(rws, tex->buffer);
     }