r300: Don't disable destination read if the src blend factor needs it
[mesa.git] / src / gallium / drivers / r300 / r300_texture_desc.c
index 3846fb8b6b380d6492e6984cd99681f968f1db3e..04d439bcc1f854c820ed07917368879aa93f35bc 100644 (file)
@@ -22,9 +22,7 @@
  * 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"
 
@@ -32,8 +30,8 @@
  * 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 radeon_bo_layout microtile,
+                                  enum radeon_bo_layout macrotile,
                                   enum r300_dim dim, boolean is_rs690)
 {
     static const unsigned table[2][5][3][2] =
@@ -62,8 +60,8 @@ unsigned r300_get_pixel_alignment(enum pipe_format format,
     unsigned tile = 0;
     unsigned pixsize = util_format_get_blocksize(format);
 
-    assert(macrotile <= R300_BUFFER_TILED);
-    assert(microtile <= R300_BUFFER_SQUARETILED);
+    assert(macrotile <= RADEON_LAYOUT_TILED);
+    assert(microtile <= RADEON_LAYOUT_SQUARETILED);
     assert(pixsize <= 16);
     assert(dim <= DIM_HEIGHT);
 
@@ -97,8 +95,8 @@ static boolean r300_texture_macro_switch(struct r300_resource *tex,
 {
     unsigned tile, texdim;
 
-    tile = r300_get_pixel_alignment(tex->b.b.b.format, tex->b.b.b.nr_samples,
-                                    tex->tex.microtile, R300_BUFFER_TILED, dim, 0);
+    tile = r300_get_pixel_alignment(tex->b.b.format, tex->b.b.nr_samples,
+                                    tex->tex.microtile, RADEON_LAYOUT_TILED, dim, 0);
     if (dim == DIM_WIDTH) {
         texdim = u_minify(tex->tex.width0, level);
     } else {
@@ -130,27 +128,27 @@ static unsigned r300_texture_get_stride(struct r300_screen *screen,
         return tex->tex.stride_in_bytes_override;
 
     /* Check the level. */
-    if (level > tex->b.b.b.last_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.b.last_level);
+                   __FUNCTION__, level, tex->b.b.last_level);
         return 0;
     }
 
     width = u_minify(tex->tex.width0, level);
 
-    if (util_format_is_plain(tex->b.b.b.format)) {
-        tile_width = r300_get_pixel_alignment(tex->b.b.b.format,
-                                              tex->b.b.b.nr_samples,
+    if (util_format_is_plain(tex->b.b.format)) {
+        tile_width = r300_get_pixel_alignment(tex->b.b.format,
+                                              tex->b.b.nr_samples,
                                               tex->tex.microtile,
                                               tex->tex.macrotile[level],
                                               DIM_WIDTH, is_rs690);
         width = align(width, tile_width);
 
-        stride = util_format_get_stride(tex->b.b.b.format, width);
+        stride = util_format_get_stride(tex->b.b.format, width);
         /* The alignment to 32 bytes is sort of implied by the layout... */
         return stride;
     } else {
-        return align(util_format_get_stride(tex->b.b.b.format, width), is_rs690 ? 64 : 32);
+        return align(util_format_get_stride(tex->b.b.format, width), is_rs690 ? 64 : 32);
     }
 }
 
@@ -163,16 +161,16 @@ static unsigned r300_texture_get_nblocksy(struct r300_resource *tex,
     height = u_minify(tex->tex.height0, level);
 
     /* Mipmapped and 3D textures must have their height aligned to POT. */
-    if ((tex->b.b.b.target != PIPE_TEXTURE_1D &&
-         tex->b.b.b.target != PIPE_TEXTURE_2D &&
-         tex->b.b.b.target != PIPE_TEXTURE_RECT) ||
-        tex->b.b.b.last_level != 0) {
+    if ((tex->b.b.target != PIPE_TEXTURE_1D &&
+         tex->b.b.target != PIPE_TEXTURE_2D &&
+         tex->b.b.target != PIPE_TEXTURE_RECT) ||
+        tex->b.b.last_level != 0) {
         height = util_next_power_of_two(height);
     }
 
-    if (util_format_is_plain(tex->b.b.b.format)) {
-        tile_height = r300_get_pixel_alignment(tex->b.b.b.format,
-                                               tex->b.b.b.nr_samples,
+    if (util_format_is_plain(tex->b.b.format)) {
+        tile_height = r300_get_pixel_alignment(tex->b.b.format,
+                                               tex->b.b.nr_samples,
                                                tex->tex.microtile,
                                                tex->tex.macrotile[level],
                                                DIM_HEIGHT, 0);
@@ -189,10 +187,10 @@ static unsigned r300_texture_get_nblocksy(struct r300_resource *tex,
 
                 /* Align the height so that there is an even number of macrotiles.
                  * Do so for 3 or more macrotiles in the Y direction. */
-                if (level == 0 && tex->b.b.b.last_level == 0 &&
-                    (tex->b.b.b.target == PIPE_TEXTURE_1D ||
-                     tex->b.b.b.target == PIPE_TEXTURE_2D ||
-                     tex->b.b.b.target == PIPE_TEXTURE_RECT) &&
+                if (level == 0 && tex->b.b.last_level == 0 &&
+                    (tex->b.b.target == PIPE_TEXTURE_1D ||
+                     tex->b.b.target == PIPE_TEXTURE_2D ||
+                     tex->b.b.target == PIPE_TEXTURE_RECT) &&
                     height >= tile_height * 3) {
                     height = align(height, tile_height * 2);
                 }
@@ -204,12 +202,12 @@ static unsigned r300_texture_get_nblocksy(struct r300_resource *tex,
         }
     }
 
-    return util_format_get_nblocksy(tex->b.b.b.format, height);
+    return util_format_get_nblocksy(tex->b.b.format, height);
 }
 
 /* Get a width in pixels from a stride in bytes. */
-static unsigned stride_to_width(enum pipe_format format,
-                                unsigned stride_in_bytes)
+unsigned r300_stride_to_width(enum pipe_format format,
+                              unsigned stride_in_bytes)
 {
     return (stride_in_bytes / util_format_get_blocksize(format)) *
             util_format_get_blockwidth(format);
@@ -219,7 +217,7 @@ static void r300_setup_miptree(struct r300_screen *screen,
                                struct r300_resource *tex,
                                boolean align_for_cbzb)
 {
-    struct pipe_resource *base = &tex->b.b.b;
+    struct pipe_resource *base = &tex->b.b;
     unsigned stride, size, layer_size, nblocksy, i;
     boolean rv350_mode = screen->caps.family >= CHIP_FAMILY_R350;
     boolean aligned_for_cbzb;
@@ -233,10 +231,10 @@ static void r300_setup_miptree(struct r300_screen *screen,
     for (i = 0; i <= base->last_level; i++) {
         /* Let's see if this miplevel can be macrotiled. */
         tex->tex.macrotile[i] =
-            (tex->tex.macrotile[0] == R300_BUFFER_TILED &&
+            (tex->tex.macrotile[0] == RADEON_LAYOUT_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;
+             RADEON_LAYOUT_TILED : RADEON_LAYOUT_LINEAR;
 
         stride = r300_texture_get_stride(screen, tex, i);
 
@@ -263,7 +261,6 @@ static void r300_setup_miptree(struct r300_screen *screen,
         tex->tex.size_in_bytes = tex->tex.offset_in_bytes[i] + size;
         tex->tex.layer_size_in_bytes[i] = layer_size;
         tex->tex.stride_in_bytes[i] = stride;
-        tex->tex.stride_in_pixels[i] = stride_to_width(tex->b.b.b.format, stride);
         tex->tex.cbzb_allowed[i] = tex->tex.cbzb_allowed[i] && aligned_for_cbzb;
 
         SCREEN_DBG(screen, DBG_TEXALLOC, "r300: Texture miptree: Level %d "
@@ -277,15 +274,15 @@ static void r300_setup_miptree(struct r300_screen *screen,
 static void r300_setup_flags(struct r300_resource *tex)
 {
     tex->tex.uses_stride_addressing =
-        !util_is_power_of_two(tex->b.b.b.width0) ||
+        !util_is_power_of_two(tex->b.b.width0) ||
         (tex->tex.stride_in_bytes_override &&
-         stride_to_width(tex->b.b.b.format,
-                         tex->tex.stride_in_bytes_override) != tex->b.b.b.width0);
+         r300_stride_to_width(tex->b.b.format,
+                         tex->tex.stride_in_bytes_override) != tex->b.b.width0);
 
     tex->tex.is_npot =
         tex->tex.uses_stride_addressing ||
-        !util_is_power_of_two(tex->b.b.b.height0) ||
-        !util_is_power_of_two(tex->b.b.b.depth0);
+        !util_is_power_of_two(tex->b.b.height0) ||
+        !util_is_power_of_two(tex->b.b.depth0);
 }
 
 static void r300_setup_cbzb_flags(struct r300_screen *rscreen,
@@ -294,20 +291,20 @@ static void r300_setup_cbzb_flags(struct r300_screen *rscreen,
     unsigned i, bpp;
     boolean first_level_valid;
 
-    bpp = util_format_get_blocksizebits(tex->b.b.b.format);
+    bpp = util_format_get_blocksizebits(tex->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 = tex->b.b.b.nr_samples <= 1 &&
+    first_level_valid = tex->b.b.nr_samples <= 1 &&
                        (bpp == 16 || bpp == 32) &&
                        tex->tex.macrotile[0];
 
     if (SCREEN_DBG_ON(rscreen, DBG_NO_CBZB))
         first_level_valid = FALSE;
 
-    for (i = 0; i <= tex->b.b.b.last_level; i++)
+    for (i = 0; i <= tex->b.b.last_level; i++)
         tex->tex.cbzb_allowed[i] = first_level_valid && tex->tex.macrotile[i];
 }
 
@@ -315,7 +312,7 @@ static unsigned r300_pixels_to_dwords(unsigned stride,
                                       unsigned height,
                                       unsigned xblock, unsigned yblock)
 {
-    return (align(stride, xblock) * align(height, yblock)) / (xblock * yblock);
+    return (util_align_npot(stride, xblock) * align(height, yblock)) / (xblock * yblock);
 }
 
 static void r300_setup_hyperz_properties(struct r300_screen *screen,
@@ -356,27 +353,29 @@ static void r300_setup_hyperz_properties(struct r300_screen *screen,
     static unsigned hiz_align_x[4] = {8, 32, 48, 32};
     static unsigned hiz_align_y[4] = {8, 8, 8, 32};
 
-    if (util_format_is_depth_or_stencil(tex->b.b.b.format) &&
-        util_format_get_blocksizebits(tex->b.b.b.format) == 32 &&
+    if (util_format_is_depth_or_stencil(tex->b.b.format) &&
+        util_format_get_blocksizebits(tex->b.b.format) == 32 &&
         tex->tex.microtile) {
         unsigned i, pipes;
 
         if (screen->caps.family == CHIP_FAMILY_RV530) {
-            pipes = screen->caps.num_z_pipes;
+            pipes = screen->info.r300_num_z_pipes;
         } else {
-            pipes = screen->caps.num_frag_pipes;
+            pipes = screen->info.r300_num_gb_pipes;
         }
 
-        for (i = 0; i <= tex->b.b.b.last_level; i++) {
+        for (i = 0; i <= tex->b.b.last_level; i++) {
             unsigned zcomp_numdw, zcompsize, hiz_numdw, stride, height;
 
-            stride = align(tex->tex.stride_in_pixels[i], 16);
-            height = u_minify(tex->b.b.b.height0, i);
+            stride = r300_stride_to_width(tex->b.b.format,
+                                          tex->tex.stride_in_bytes[i]);
+            stride = align(stride, 16);
+            height = u_minify(tex->b.b.height0, i);
 
             /* The 8x8 compression mode needs macrotiling. */
             zcompsize = screen->caps.z_compress == R300_ZCOMP_8X8 &&
                        tex->tex.macrotile[i] &&
-                       tex->b.b.b.nr_samples <= 1 ? 8 : 4;
+                       tex->b.b.nr_samples <= 1 ? 8 : 4;
 
             /* Get the ZMASK buffer size in dwords. */
             zcomp_numdw = r300_pixels_to_dwords(stride, height,
@@ -384,13 +383,13 @@ static void r300_setup_hyperz_properties(struct r300_screen *screen,
                                 zmask_blocks_y_per_dw[pipes-1] * zcompsize);
 
             /* Check whether we have enough ZMASK memory. */
-            if (util_format_get_blocksizebits(tex->b.b.b.format) == 32 &&
+            if (util_format_get_blocksizebits(tex->b.b.format) == 32 &&
                 zcomp_numdw <= screen->caps.zmask_ram * pipes) {
                 tex->tex.zmask_dwords[i] = zcomp_numdw;
                 tex->tex.zcomp8x8[i] = zcompsize == 8;
 
                 tex->tex.zmask_stride_in_pixels[i] =
-                    align(stride, zmask_blocks_x_per_dw[pipes-1] * zcompsize);
+                    util_align_npot(stride, zmask_blocks_x_per_dw[pipes-1] * zcompsize);
             } else {
                 tex->tex.zmask_dwords[i] = 0;
                 tex->tex.zcomp8x8[i] = FALSE;
@@ -398,7 +397,7 @@ static void r300_setup_hyperz_properties(struct r300_screen *screen,
             }
 
             /* Now setup HIZ. */
-            stride = align(stride, hiz_align_x[pipes-1]);
+            stride = util_align_npot(stride, hiz_align_x[pipes-1]);
             height = align(height, hiz_align_y[pipes-1]);
 
             /* Get the HIZ buffer size in dwords. */
@@ -419,20 +418,20 @@ static void r300_setup_hyperz_properties(struct r300_screen *screen,
 static void r300_setup_tiling(struct r300_screen *screen,
                               struct r300_resource *tex)
 {
-    enum pipe_format format = tex->b.b.b.format;
+    enum pipe_format format = tex->b.b.format;
     boolean rv350_mode = screen->caps.family >= CHIP_FAMILY_R350;
     boolean is_zb = util_format_is_depth_or_stencil(format);
     boolean dbg_no_tiling = SCREEN_DBG_ON(screen, DBG_NO_TILING);
 
-    tex->tex.microtile = R300_BUFFER_LINEAR;
-    tex->tex.macrotile[0] = R300_BUFFER_LINEAR;
+    tex->tex.microtile = RADEON_LAYOUT_LINEAR;
+    tex->tex.macrotile[0] = RADEON_LAYOUT_LINEAR;
 
     if (!util_format_is_plain(format)) {
         return;
     }
 
     /* If height == 1, disable microtiling except for zbuffer. */
-    if (!is_zb && (tex->b.b.b.height0 == 1 || dbg_no_tiling)) {
+    if (!is_zb && (tex->b.b.height0 == 1 || dbg_no_tiling)) {
         return;
     }
 
@@ -441,11 +440,11 @@ static void r300_setup_tiling(struct r300_screen *screen,
         case 1:
         case 4:
         case 8:
-            tex->tex.microtile = R300_BUFFER_TILED;
+            tex->tex.microtile = RADEON_LAYOUT_TILED;
             break;
 
         case 2:
-            tex->tex.microtile = R300_BUFFER_SQUARETILED;
+            tex->tex.microtile = RADEON_LAYOUT_SQUARETILED;
             break;
     }
 
@@ -456,7 +455,7 @@ static void r300_setup_tiling(struct r300_screen *screen,
     /* Set macrotiling. */
     if (r300_texture_macro_switch(tex, 0, rv350_mode, DIM_WIDTH) &&
         r300_texture_macro_switch(tex, 0, rv350_mode, DIM_HEIGHT)) {
-        tex->tex.macrotile[0] = R300_BUFFER_TILED;
+        tex->tex.macrotile[0] = RADEON_LAYOUT_TILED;
     }
 }
 
@@ -469,24 +468,24 @@ static void r300_tex_print_info(struct r300_resource *tex,
             func,
             tex->tex.macrotile[0] ? "YES" : " NO",
             tex->tex.microtile ? "YES" : " NO",
-            tex->tex.stride_in_pixels[0],
-            tex->b.b.b.width0, tex->b.b.b.height0, tex->b.b.b.depth0,
-            tex->b.b.b.last_level, tex->tex.size_in_bytes,
-            util_format_short_name(tex->b.b.b.format));
+            r300_stride_to_width(tex->b.b.format, tex->tex.stride_in_bytes[0]),
+            tex->b.b.width0, tex->b.b.height0, tex->b.b.depth0,
+            tex->b.b.last_level, tex->tex.size_in_bytes,
+            util_format_short_name(tex->b.b.format));
 }
 
-boolean r300_texture_desc_init(struct r300_screen *rscreen,
-                               struct r300_resource *tex,
-                               const struct pipe_resource *base)
+void r300_texture_desc_init(struct r300_screen *rscreen,
+                            struct r300_resource *tex,
+                            const struct pipe_resource *base)
 {
-    tex->b.b.b.target = base->target;
-    tex->b.b.b.format = base->format;
-    tex->b.b.b.width0 = base->width0;
-    tex->b.b.b.height0 = base->height0;
-    tex->b.b.b.depth0 = base->depth0;
-    tex->b.b.b.array_size = base->array_size;
-    tex->b.b.b.last_level = base->last_level;
-    tex->b.b.b.nr_samples = base->nr_samples;
+    tex->b.b.target = base->target;
+    tex->b.b.format = base->format;
+    tex->b.b.width0 = base->width0;
+    tex->b.b.height0 = base->height0;
+    tex->b.b.depth0 = base->depth0;
+    tex->b.b.array_size = base->array_size;
+    tex->b.b.last_level = base->last_level;
+    tex->b.b.nr_samples = base->nr_samples;
     tex->tex.width0 = base->width0;
     tex->tex.height0 = base->height0;
     tex->tex.depth0 = base->depth0;
@@ -501,7 +500,7 @@ boolean r300_texture_desc_init(struct r300_screen *rscreen,
     }
 
     /* Setup tiling. */
-    if (tex->tex.microtile == R300_BUFFER_SELECT_LAYOUT) {
+    if (tex->tex.microtile == RADEON_LAYOUT_UNKNOWN) {
         r300_setup_tiling(rscreen, tex);
     }
 
@@ -509,33 +508,29 @@ boolean r300_texture_desc_init(struct r300_screen *rscreen,
 
     /* Setup the miptree description. */
     r300_setup_miptree(rscreen, tex, TRUE);
-    /* If the required buffer size is larger the given max size,
+    /* If the required buffer size is larger than the given max size,
      * try again without the alignment for the CBZB clear. */
-    if (tex->buf_size && tex->tex.size_in_bytes > tex->buf_size) {
+    if (tex->buf && tex->tex.size_in_bytes > tex->buf->size) {
         r300_setup_miptree(rscreen, tex, FALSE);
-    }
-
-    r300_setup_hyperz_properties(rscreen, tex);
 
-    if (tex->buf_size) {
         /* Make sure the buffer we got is large enough. */
-        if (tex->tex.size_in_bytes > tex->buf_size) {
-            fprintf(stderr, "r300: texture_desc_init: The buffer is not "
-                            "large enough. Got: %i, Need: %i, Info:\n",
-                            tex->buf_size, tex->tex.size_in_bytes);
+        if (tex->tex.size_in_bytes > tex->buf->size) {
+            fprintf(stderr,
+                "r300: I got a pre-allocated buffer to use it as a texture "
+                "storage, but the buffer is too small. I'll use the buffer "
+                "anyway, because I can't crash here, but it's dangerous. "
+                "This can be a DDX bug. Got: %iB, Need: %iB, Info:\n",
+                tex->buf->size, tex->tex.size_in_bytes);
             r300_tex_print_info(tex, "texture_desc_init");
-            return FALSE;
+            /* Ooops, what now. Apps will break if we fail this,
+             * so just pretend everything's okay. */
         }
-
-        tex->tex.buffer_size_in_bytes = tex->buf_size;
-    } else {
-        tex->tex.buffer_size_in_bytes = tex->tex.size_in_bytes;
     }
 
+    r300_setup_hyperz_properties(rscreen, tex);
+
     if (SCREEN_DBG_ON(rscreen, DBG_TEX))
         r300_tex_print_info(tex, "texture_desc_init");
-
-    return TRUE;
 }
 
 unsigned r300_texture_get_offset(struct r300_resource *tex,
@@ -543,7 +538,7 @@ unsigned r300_texture_get_offset(struct r300_resource *tex,
 {
     unsigned offset = tex->tex.offset_in_bytes[level];
 
-    switch (tex->b.b.b.target) {
+    switch (tex->b.b.target) {
         case PIPE_TEXTURE_3D:
         case PIPE_TEXTURE_CUBE:
             return offset + layer * tex->tex.layer_size_in_bytes[level];