gallium: Add capability for ARB_robust_buffer_access_behavior.
[mesa.git] / src / gallium / drivers / r300 / r300_hyperz.c
index 7ff643f84dbcf7e2fa1ddf8ab8cd79183683ca44..0f021e9f4e8ae8d077b959b982aab85adc6cd919 100644 (file)
@@ -24,7 +24,6 @@
 #include "r300_context.h"
 #include "r300_reg.h"
 #include "r300_fs.h"
-#include "r300_winsys.h"
 
 #include "util/u_format.h"
 #include "util/u_mm.h"
 /* The HyperZ setup                                                          */
 /*****************************************************************************/
 
-static bool r300_get_sc_hz_max(struct r300_context *r300)
+static enum r300_hiz_func r300_get_hiz_func(struct r300_context *r300)
 {
-    struct r300_dsa_state *dsa_state = r300->dsa_state.state;
-    int func = dsa_state->z_stencil_control & R300_ZS_MASK;
-    int ret = R300_SC_HYPERZ_MIN;
+    struct r300_dsa_state *dsa = r300->dsa_state.state;
+
+    switch (dsa->dsa.depth.func) {
+    case PIPE_FUNC_NEVER:
+    case PIPE_FUNC_EQUAL:
+    case PIPE_FUNC_NOTEQUAL:
+    case PIPE_FUNC_ALWAYS:
+    default:
+        /* Guess MAX for uncertain cases. */
+    case PIPE_FUNC_LESS:
+    case PIPE_FUNC_LEQUAL:
+        return HIZ_FUNC_MAX;
+
+    case PIPE_FUNC_GREATER:
+    case PIPE_FUNC_GEQUAL:
+        return HIZ_FUNC_MIN;
+    }
+}
+
+/* Return what's used for the depth test (either minimum or maximum). */
+static unsigned r300_get_sc_hz_max(struct r300_context *r300)
+{
+    struct r300_dsa_state *dsa = r300->dsa_state.state;
+    unsigned func = dsa->dsa.depth.func;
 
-    if (func >= R300_ZS_GEQUAL && func <= R300_ZS_ALWAYS)
-       ret = R300_SC_HYPERZ_MAX;
-    return ret;
+    return func >= PIPE_FUNC_GREATER ? R300_SC_HYPERZ_MAX : R300_SC_HYPERZ_MIN;
 }
 
-static bool r300_zfunc_same_direction(int func1, int func2)
+static boolean r300_is_hiz_func_valid(struct r300_context *r300)
 {
+    struct r300_dsa_state *dsa = r300->dsa_state.state;
+    unsigned func = dsa->dsa.depth.func;
+
+    if (r300->hiz_func == HIZ_FUNC_NONE)
+        return TRUE;
+
     /* func1 is less/lessthan */
-    if ((func1 == R300_ZS_LESS || func1 == R300_ZS_LEQUAL) &&
-        (func2 == R300_ZS_EQUAL || func2 == R300_ZS_GEQUAL ||
-         func2 == R300_ZS_GREATER))
-            return FALSE;
+    if (r300->hiz_func == HIZ_FUNC_MAX &&
+        (func == PIPE_FUNC_GEQUAL || func == PIPE_FUNC_GREATER))
+        return FALSE;
 
     /* func1 is greater/greaterthan */
-    if ((func1 == R300_ZS_GEQUAL || func1 == R300_ZS_GREATER) &&
-        (func2 == R300_ZS_LESS || func2 == R300_ZS_LEQUAL))
-            return FALSE;
+    if (r300->hiz_func == HIZ_FUNC_MIN &&
+        (func == PIPE_FUNC_LESS   || func == PIPE_FUNC_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 & R300_ZS_MASK;
-    int ret = R300_HIZ_MIN;
-
-    if (func == R300_ZS_LESS || func == R300_ZS_LEQUAL)
-       ret = R300_HIZ_MAX;
-    return ret;
-}
-
 static boolean r300_dsa_stencil_op_not_keep(struct pipe_stencil_state *s)
 {
-    if (s->enabled && (s->fail_op != PIPE_STENCIL_OP_KEEP ||
-                       s->zfail_op != PIPE_STENCIL_OP_KEEP))
-        return TRUE;
-    return FALSE;
+    return s->enabled && (s->fail_op != PIPE_STENCIL_OP_KEEP ||
+                          s->zfail_op != PIPE_STENCIL_OP_KEEP);
 }
 
-static boolean r300_can_hiz(struct r300_context *r300)
+static boolean r300_hiz_allowed(struct r300_context *r300)
 {
-    struct r300_dsa_state *dsa_state = r300->dsa_state.state;
-    struct pipe_depth_stencil_alpha_state *dsa = &dsa_state->dsa;
-    struct r300_screen* r300screen = r300->screen;
-    struct r300_hyperz_state *z = r300->hyperz_state.state;
+    struct r300_dsa_state *dsa = r300->dsa_state.state;
+    struct r300_screen *r300screen = r300->screen;
 
-    /* shader writes depth - no HiZ */
-    if (r300_fragment_shader_writes_depth(r300_fs(r300))) /* (5) */
+    if (r300_fragment_shader_writes_depth(r300_fs(r300)))
         return FALSE;
 
     if (r300->query_current)
         return FALSE;
 
+    /* If the depth function is inverted, HiZ must be disabled. */
+    if (!r300_is_hiz_func_valid(r300))
+        return FALSE;
+
     /* if stencil fail/zfail op is not KEEP */
-    if (r300_dsa_stencil_op_not_keep(&dsa->stencil[0]) ||
-        r300_dsa_stencil_op_not_keep(&dsa->stencil[1]))
+    if (r300_dsa_stencil_op_not_keep(&dsa->dsa.stencil[0]) ||
+        r300_dsa_stencil_op_not_keep(&dsa->dsa.stencil[1]))
         return FALSE;
 
-    if (dsa->depth.enabled) {
+    if (dsa->dsa.depth.enabled) {
         /* if depth func is EQUAL pre-r500 */
-        if (dsa->depth.func == PIPE_FUNC_EQUAL && !r300screen->caps.is_r500)
+        if (dsa->dsa.depth.func == PIPE_FUNC_EQUAL && !r300screen->caps.is_r500)
             return FALSE;
+
         /* if depth func is NOTEQUAL */
-        if (dsa->depth.func == PIPE_FUNC_NOTEQUAL)
+        if (dsa->dsa.depth.func == PIPE_FUNC_NOTEQUAL)
             return FALSE;
     }
-    /* depth comparison function - if just cleared save and return okay */
-    if (z->current_func == -1) {
-        int func = dsa_state->z_stencil_control & R300_ZS_MASK;
-        if (func != 0 && func != 7)
-            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 & 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;
-        }
-    }
     return TRUE;
 }
 
@@ -137,6 +134,7 @@ static void r300_update_hyperz(struct r300_context* r300)
         (struct r300_hyperz_state*)r300->hyperz_state.state;
     struct pipe_framebuffer_state *fb =
         (struct pipe_framebuffer_state*)r300->fb_state.state;
+    struct r300_dsa_state *dsa = r300->dsa_state.state;
     struct r300_resource *zstex =
             fb->zsbuf ? r300_resource(fb->zsbuf->texture) : NULL;
 
@@ -150,46 +148,71 @@ static void r300_update_hyperz(struct r300_context* r300)
         return;
     }
 
-    if (!zstex)
+    if (!zstex || !r300->hyperz_enabled)
         return;
 
-    if (!r300->rws->get_value(r300->rws, R300_CAN_HYPERZ))
-        return;
+    /* Set the size of ZMASK tiles. */
+    if (zstex->tex.zcomp8x8[fb->zsbuf->u.tex.level]) {
+        z->gb_z_peq_config |= R300_GB_Z_PEQ_CONFIG_Z_PEQ_SIZE_8_8;
+    }
 
-    /* Zbuffer compression. */
-    if (r300->zmask_in_use && !r300->hyperz_locked) {
+    /* R500-specific features and optimizations. */
+    if (r300->screen->caps.is_r500) {
+        z->zb_bw_cntl |= R500_PEQ_PACKING_ENABLE |
+                         R500_COVERED_PTR_MASKING_ENABLE;
+    }
+
+    /* Setup decompression if needed. No other HyperZ setting is required. */
+    if (r300->zmask_decompress) {
         z->zb_bw_cntl |= R300_FAST_FILL_ENABLE |
-                         /*R300_FORCE_COMPRESSED_STENCIL_VALUE_ENABLE |*/
                          R300_RD_COMP_ENABLE;
+        return;
+    }
 
-        if (!r300->zmask_decompress) {
-            z->zb_bw_cntl |= R300_WR_COMP_ENABLE;
-        }
+    /* Do not set anything if depth and stencil tests are off. */
+    if (!dsa->dsa.depth.enabled &&
+        !dsa->dsa.stencil[0].enabled &&
+        !dsa->dsa.stencil[1].enabled) {
+        assert(!dsa->dsa.depth.writemask);
+        return;
     }
 
-    if (zstex->tex.zcomp8x8[fb->zsbuf->u.tex.level]) {
-        z->gb_z_peq_config |= R300_GB_Z_PEQ_CONFIG_Z_PEQ_SIZE_8_8;
+    /* Zbuffer compression. */
+    if (r300->zmask_in_use && !r300->locked_zbuffer) {
+        z->zb_bw_cntl |= R300_FAST_FILL_ENABLE |
+                         R300_RD_COMP_ENABLE |
+                         R300_WR_COMP_ENABLE;
     }
 
-    /* XXX Use can_hiz to disable hyperz for good, instead of turning it off/on. */
-    if (r300->hiz_in_use && !r300->hyperz_locked && r300_can_hiz(r300)) {
+    /* HiZ. */
+    if (r300->hiz_in_use && !r300->locked_zbuffer) {
+        /* HiZ cannot be used under some circumstances. */
+        if (!r300_hiz_allowed(r300)) {
+            /* If writemask is disabled, the HiZ memory will not be changed,
+             * so we can keep its content for later. */
+            if (dsa->dsa.depth.writemask) {
+                r300->hiz_in_use = FALSE;
+            }
+            return;
+        }
+        DBG(r300, DBG_HYPERZ, "r300: Z-func: %i\n", dsa->dsa.depth.func);
+
+        /* Set the HiZ function if needed. */
+        if (r300->hiz_func == HIZ_FUNC_NONE) {
+            r300->hiz_func = r300_get_hiz_func(r300);
+        }
+
+        /* Setup the HiZ bits. */
         z->zb_bw_cntl |= R300_HIZ_ENABLE |
-                         r300_get_hiz_min(r300);
+                (r300->hiz_func == HIZ_FUNC_MIN ? R300_HIZ_MIN : R300_HIZ_MAX);
 
         z->sc_hyperz |= R300_SC_HYPERZ_ENABLE |
                         r300_get_sc_hz_max(r300);
 
         if (r300->screen->caps.is_r500) {
-            z->zb_bw_cntl |= R500_HIZ_FP_EXP_BITS_3 |
-                             R500_HIZ_EQUAL_REJECT_ENABLE;
+            z->zb_bw_cntl |= R500_HIZ_EQUAL_REJECT_ENABLE;
         }
     }
-
-    /* R500-specific features and optimizations. */
-    if (r300->screen->caps.is_r500) {
-        z->zb_bw_cntl |= R500_PEQ_PACKING_ENABLE |
-                         R500_COVERED_PTR_MASKING_ENABLE;
-    }
 }
 
 /*****************************************************************************/