r300g: fix corruption when nr_cbufs==0 and multiwrites enabled
[mesa.git] / src / gallium / drivers / r300 / r300_hyperz.c
index 10e440ce3065d28d9470e9b71a3d4e26c67040f5..c22e307c679a11c251d9131902d5379faff7c6e3 100644 (file)
  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  * USE OR OTHER DEALINGS IN THE SOFTWARE. */
 
-#include "util/u_format.h"
-#include "util/u_mm.h"
 #include "r300_context.h"
 #include "r300_hyperz.h"
 #include "r300_reg.h"
 #include "r300_fs.h"
+#include "r300_winsys.h"
+
+#include "util/u_format.h"
+#include "util/u_mm.h"
 
 /*
   HiZ rules - taken from various docs 
 static bool r300_get_sc_hz_max(struct r300_context *r300)
 {
     struct r300_dsa_state *dsa_state = r300->dsa_state.state;
-    int func = dsa_state->z_stencil_control & 0x7;
+    int func = dsa_state->z_stencil_control & R300_ZS_MASK;
     int ret = R300_SC_HYPERZ_MIN;
 
-    if (func >= 4 && func <= 7)
+    if (func >= R300_ZS_GEQUAL && func <= R300_ZS_ALWAYS)
        ret = R300_SC_HYPERZ_MAX;
     return ret;
 }
@@ -53,23 +55,26 @@ static bool r300_get_sc_hz_max(struct r300_context *r300)
 static bool r300_zfunc_same_direction(int func1, int func2)
 {
     /* func1 is less/lessthan */
-    if (func1 == 1 || func1 == 2)
-        if (func2 == 3 || func2 == 4 || func2 == 5)
+    if ((func1 == R300_ZS_LESS || func1 == R300_ZS_LEQUAL) &&
+        (func2 == R300_ZS_EQUAL || func2 == R300_ZS_GEQUAL ||
+         func2 == R300_ZS_GREATER))
             return FALSE;
 
-    if (func2 == 1 || func2 == 2)
-        if (func1 == 4 || func1 == 5)
+    /* func1 is greater/greaterthan */
+    if ((func1 == R300_ZS_GEQUAL || func1 == R300_ZS_GREATER) &&
+        (func2 == R300_ZS_LESS || func2 == R300_ZS_LEQUAL))
             return FALSE;
+
     return TRUE;
 }
-    
+
 static int r300_get_hiz_min(struct r300_context *r300)
 {
     struct r300_dsa_state *dsa_state = r300->dsa_state.state;
-    int func = dsa_state->z_stencil_control & 0x7;
+    int func = dsa_state->z_stencil_control & R300_ZS_MASK;
     int ret = R300_HIZ_MIN;
 
-    if (func == 1 || func == 2)
+    if (func == R300_ZS_LESS || func == R300_ZS_LEQUAL)
        ret = R300_HIZ_MAX;
     return ret;
 }
@@ -110,13 +115,16 @@ static boolean r300_can_hiz(struct r300_context *r300)
     }
     /* depth comparison function - if just cleared save and return okay */
     if (z->current_func == -1) {
-        int func = dsa_state->z_stencil_control & 0x7;
+        int func = dsa_state->z_stencil_control & R300_ZS_MASK;
         if (func != 0 && func != 7)
-            z->current_func = dsa_state->z_stencil_control & 0x7;
+            z->current_func = dsa_state->z_stencil_control & R300_ZS_MASK;
     } else {
         /* simple don't change */
-        if (!r300_zfunc_same_direction(z->current_func, (dsa_state->z_stencil_control & 0x7))) {
-            DBG(r300, DBG_HYPERZ, "z func changed direction - disabling hyper-z %d -> %d\n", z->current_func, dsa_state->z_stencil_control);
+        if (!r300_zfunc_same_direction(z->current_func,
+                                       (dsa_state->z_stencil_control & R300_ZS_MASK))) {
+            DBG(r300, DBG_HYPERZ,
+                "z func changed direction - disabling hyper-z %d -> %d\n",
+                z->current_func, dsa_state->z_stencil_control);
             return FALSE;
         }
     }    
@@ -127,6 +135,12 @@ static void r300_update_hyperz(struct r300_context* r300)
 {
     struct r300_hyperz_state *z =
         (struct r300_hyperz_state*)r300->hyperz_state.state;
+    struct pipe_framebuffer_state *fb =
+        (struct pipe_framebuffer_state*)r300->fb_state.state;
+    struct r300_texture *zstex =
+            fb->zsbuf ? r300_texture(fb->zsbuf->texture) : NULL;
+    boolean zmask_in_use = FALSE;
+    boolean hiz_in_use = FALSE;
 
     z->gb_z_peq_config = 0;
     z->zb_bw_cntl = 0;
@@ -138,22 +152,32 @@ static void r300_update_hyperz(struct r300_context* r300)
         return;
     }
 
+    if (!zstex)
+        return;
+
+    if (!r300->rws->get_value(r300->rws, R300_CAN_HYPERZ))
+        return;
+
+    zmask_in_use = zstex->zmask_in_use[fb->zsbuf->u.tex.level];
+    hiz_in_use = zstex->hiz_in_use[fb->zsbuf->u.tex.level];
+
+    /* Z fastfill. */
+    if (zmask_in_use) {
+        z->zb_bw_cntl |= R300_FAST_FILL_ENABLE; /*  | R300_FORCE_COMPRESSED_STENCIL_VALUE_ENABLE;*/
+    }
+
     /* Zbuffer compression. */
-    if (r300->z_compression) {
+    if (zmask_in_use && r300->z_compression) {
         z->zb_bw_cntl |= R300_RD_COMP_ENABLE;
         if (r300->z_decomp_rd == false)
             z->zb_bw_cntl |= R300_WR_COMP_ENABLE;
-       /* RV350 and up optimizations. */
-       if (r300->z_compression == RV350_Z_COMPRESS_88)
-           z->gb_z_peq_config |= R300_GB_Z_PEQ_CONFIG_Z_PEQ_SIZE_8_8;
-    }
-
-    /* Z fastfill. */
-    if (r300->z_fastfill) {
-        z->zb_bw_cntl |= R300_FAST_FILL_ENABLE; /*  | R300_FORCE_COMPRESSED_STENCIL_VALUE_ENABLE;*/
     }
+    /* RV350 and up optimizations. */
+    /* The section 10.4.9 in the docs is a lie. */
+    if (r300->z_compression == RV350_Z_COMPRESS_88)
+        z->gb_z_peq_config |= R300_GB_Z_PEQ_CONFIG_Z_PEQ_SIZE_8_8;
 
-    if (r300->hiz_enable) {
+    if (hiz_in_use) {
         bool can_hiz = r300_can_hiz(r300);
         if (can_hiz) {
             z->zb_bw_cntl |= R300_HIZ_ENABLE;
@@ -163,8 +187,8 @@ static void r300_update_hyperz(struct r300_context* r300)
         }
     }
 
+    /* R500-specific features and optimizations. */
     if (r300->screen->caps.is_r500) {
-        /* XXX Are these bits really available on RV350? */
         z->zb_bw_cntl |= R500_HIZ_FP_EXP_BITS_3;
         z->zb_bw_cntl |=
                 R500_HIZ_EQUAL_REJECT_ENABLE |
@@ -258,7 +282,7 @@ static void r300_update_ztop(struct r300_context* r300)
         ztop_state->z_buffer_top = R300_ZTOP_ENABLE;
     }
     if (ztop_state->z_buffer_top != old_ztop)
-        r300->ztop_state.dirty = TRUE;
+        r300_mark_atom_dirty(r300, &r300->ztop_state);
 }
 
 #define ALIGN_DIVUP(x, y) (((x) + (y) - 1) / (y))
@@ -309,7 +333,7 @@ void r300_hiz_alloc_block(struct r300_context *r300, struct r300_surface *surf)
 {
     struct r300_texture *tex;
     uint32_t zsize, ndw;
-    int level = surf->base.level;
+    int level = surf->base.u.tex.level;
 
     tex = r300_texture(surf->base.texture);
 
@@ -328,11 +352,22 @@ void r300_zmask_alloc_block(struct r300_context *r300, struct r300_surface *surf
 {
     int bsize = 256;
     uint32_t zsize, ndw;
-    int level = surf->base.level;
+    int level = surf->base.u.tex.level;
     struct r300_texture *tex;
 
     tex = r300_texture(surf->base.texture);
 
+    /* We currently don't handle decompression for 3D textures and cubemaps
+     * correctly. */
+    if (tex->desc.b.b.target != PIPE_TEXTURE_1D &&
+        tex->desc.b.b.target != PIPE_TEXTURE_2D &&
+        tex->desc.b.b.target != PIPE_TEXTURE_RECT)
+        return;
+
+    /* Cannot flush zmask of 16-bit zbuffers. */
+    if (util_format_get_blocksizebits(tex->desc.b.b.format) == 16)
+        return;
+
     if (tex->zmask_mem[level])
         return;
 
@@ -349,23 +384,36 @@ void r300_zmask_alloc_block(struct r300_context *r300, struct r300_surface *surf
     return;
 }
 
-void r300_hyperz_init_mm(struct r300_context *r300)
+boolean r300_hyperz_init_mm(struct r300_context *r300)
 {
     struct r300_screen* r300screen = r300->screen;
     int frag_pipes = r300screen->caps.num_frag_pipes;
 
-    if (r300screen->caps.hiz_ram)
+    r300->zmask_mm = u_mmInit(0, r300screen->caps.zmask_ram * frag_pipes);
+    if (!r300->zmask_mm)
+      return FALSE;
+
+    if (r300screen->caps.hiz_ram) {
       r300->hiz_mm = u_mmInit(0, r300screen->caps.hiz_ram * frag_pipes);
+      if (!r300->hiz_mm) {
+        u_mmDestroy(r300->zmask_mm);
+        r300->zmask_mm = NULL;
+        return FALSE;
+      }
+    }
 
-    r300->zmask_mm = u_mmInit(0, r300screen->caps.zmask_ram * frag_pipes);
+    return TRUE;
 }
 
 void r300_hyperz_destroy_mm(struct r300_context *r300)
 {
     struct r300_screen* r300screen = r300->screen;
 
-    if (r300screen->caps.hiz_ram)
+    if (r300screen->caps.hiz_ram) {
       u_mmDestroy(r300->hiz_mm);
+      r300->hiz_mm = NULL;
+    }
 
     u_mmDestroy(r300->zmask_mm);
+    r300->zmask_mm = NULL;
 }