Path: blob/21.2-virgl/src/gallium/drivers/r300/r300_hyperz.c
4570 views
/*1* Copyright 2008 Corbin Simpson <[email protected]>2* Copyright 2009 Marek Olšák <[email protected]>3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* on the rights to use, copy, modify, merge, publish, distribute, sub8* license, and/or sell copies of the Software, and to permit persons to whom9* the Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the next12* paragraph) shall be included in all copies or substantial portions of the13* Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL18* THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,19* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR20* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE21* USE OR OTHER DEALINGS IN THE SOFTWARE. */2223#include "r300_context.h"24#include "r300_reg.h"25#include "r300_fs.h"2627#include "util/format/u_format.h"2829/*30HiZ rules - taken from various docs311. HiZ only works on depth values322. Cannot HiZ if stencil fail or zfail is !KEEP333. on R300/400, HiZ is disabled if depth test is EQUAL344. comparison changes without clears usually mean disabling HiZ35*/36/*****************************************************************************/37/* The HyperZ setup */38/*****************************************************************************/3940static enum r300_hiz_func r300_get_hiz_func(struct r300_context *r300)41{42struct r300_dsa_state *dsa = r300->dsa_state.state;4344switch (dsa->dsa.depth_func) {45case PIPE_FUNC_NEVER:46case PIPE_FUNC_EQUAL:47case PIPE_FUNC_NOTEQUAL:48case PIPE_FUNC_ALWAYS:49default:50/* Guess MAX for uncertain cases. */51case PIPE_FUNC_LESS:52case PIPE_FUNC_LEQUAL:53return HIZ_FUNC_MAX;5455case PIPE_FUNC_GREATER:56case PIPE_FUNC_GEQUAL:57return HIZ_FUNC_MIN;58}59}6061/* Return what's used for the depth test (either minimum or maximum). */62static unsigned r300_get_sc_hz_max(struct r300_context *r300)63{64struct r300_dsa_state *dsa = r300->dsa_state.state;65unsigned func = dsa->dsa.depth_func;6667return func >= PIPE_FUNC_GREATER ? R300_SC_HYPERZ_MAX : R300_SC_HYPERZ_MIN;68}6970static boolean r300_is_hiz_func_valid(struct r300_context *r300)71{72struct r300_dsa_state *dsa = r300->dsa_state.state;73unsigned func = dsa->dsa.depth_func;7475if (r300->hiz_func == HIZ_FUNC_NONE)76return TRUE;7778/* func1 is less/lessthan */79if (r300->hiz_func == HIZ_FUNC_MAX &&80(func == PIPE_FUNC_GEQUAL || func == PIPE_FUNC_GREATER))81return FALSE;8283/* func1 is greater/greaterthan */84if (r300->hiz_func == HIZ_FUNC_MIN &&85(func == PIPE_FUNC_LESS || func == PIPE_FUNC_LEQUAL))86return FALSE;8788return TRUE;89}9091static boolean r300_dsa_stencil_op_not_keep(struct pipe_stencil_state *s)92{93return s->enabled && (s->fail_op != PIPE_STENCIL_OP_KEEP ||94s->zfail_op != PIPE_STENCIL_OP_KEEP);95}9697static boolean r300_hiz_allowed(struct r300_context *r300)98{99struct r300_dsa_state *dsa = r300->dsa_state.state;100struct r300_screen *r300screen = r300->screen;101102if (r300_fragment_shader_writes_depth(r300_fs(r300)))103return FALSE;104105if (r300->query_current)106return FALSE;107108/* If the depth function is inverted, HiZ must be disabled. */109if (!r300_is_hiz_func_valid(r300))110return FALSE;111112/* if stencil fail/zfail op is not KEEP */113if (r300_dsa_stencil_op_not_keep(&dsa->dsa.stencil[0]) ||114r300_dsa_stencil_op_not_keep(&dsa->dsa.stencil[1]))115return FALSE;116117if (dsa->dsa.depth_enabled) {118/* if depth func is EQUAL pre-r500 */119if (dsa->dsa.depth_func == PIPE_FUNC_EQUAL && !r300screen->caps.is_r500)120return FALSE;121122/* if depth func is NOTEQUAL */123if (dsa->dsa.depth_func == PIPE_FUNC_NOTEQUAL)124return FALSE;125}126return TRUE;127}128129static void r300_update_hyperz(struct r300_context* r300)130{131struct r300_hyperz_state *z =132(struct r300_hyperz_state*)r300->hyperz_state.state;133struct pipe_framebuffer_state *fb =134(struct pipe_framebuffer_state*)r300->fb_state.state;135struct r300_dsa_state *dsa = r300->dsa_state.state;136struct r300_resource *zstex =137fb->zsbuf ? r300_resource(fb->zsbuf->texture) : NULL;138139z->gb_z_peq_config = 0;140z->zb_bw_cntl = 0;141z->sc_hyperz = R300_SC_HYPERZ_ADJ_2;142z->flush = 0;143144if (r300->cbzb_clear) {145z->zb_bw_cntl |= R300_ZB_CB_CLEAR_CACHE_LINE_WRITE_ONLY;146return;147}148149if (!zstex || !r300->hyperz_enabled)150return;151152/* Set the size of ZMASK tiles. */153if (zstex->tex.zcomp8x8[fb->zsbuf->u.tex.level]) {154z->gb_z_peq_config |= R300_GB_Z_PEQ_CONFIG_Z_PEQ_SIZE_8_8;155}156157/* R500-specific features and optimizations. */158if (r300->screen->caps.is_r500) {159z->zb_bw_cntl |= R500_PEQ_PACKING_ENABLE |160R500_COVERED_PTR_MASKING_ENABLE;161}162163/* Setup decompression if needed. No other HyperZ setting is required. */164if (r300->zmask_decompress) {165z->zb_bw_cntl |= R300_FAST_FILL_ENABLE |166R300_RD_COMP_ENABLE;167return;168}169170/* Do not set anything if depth and stencil tests are off. */171if (!dsa->dsa.depth_enabled &&172!dsa->dsa.stencil[0].enabled &&173!dsa->dsa.stencil[1].enabled) {174assert(!dsa->dsa.depth_writemask);175return;176}177178/* Zbuffer compression. */179if (r300->zmask_in_use && !r300->locked_zbuffer) {180z->zb_bw_cntl |= R300_FAST_FILL_ENABLE |181R300_RD_COMP_ENABLE |182R300_WR_COMP_ENABLE;183}184185/* HiZ. */186if (r300->hiz_in_use && !r300->locked_zbuffer) {187/* HiZ cannot be used under some circumstances. */188if (!r300_hiz_allowed(r300)) {189/* If writemask is disabled, the HiZ memory will not be changed,190* so we can keep its content for later. */191if (dsa->dsa.depth_writemask) {192r300->hiz_in_use = FALSE;193}194return;195}196DBG(r300, DBG_HYPERZ, "r300: Z-func: %i\n", dsa->dsa.depth_func);197198/* Set the HiZ function if needed. */199if (r300->hiz_func == HIZ_FUNC_NONE) {200r300->hiz_func = r300_get_hiz_func(r300);201}202203/* Setup the HiZ bits. */204z->zb_bw_cntl |= R300_HIZ_ENABLE |205(r300->hiz_func == HIZ_FUNC_MIN ? R300_HIZ_MIN : R300_HIZ_MAX);206207z->sc_hyperz |= R300_SC_HYPERZ_ENABLE |208r300_get_sc_hz_max(r300);209210if (r300->screen->caps.is_r500) {211z->zb_bw_cntl |= R500_HIZ_EQUAL_REJECT_ENABLE;212}213}214}215216/*****************************************************************************/217/* The ZTOP state */218/*****************************************************************************/219220static boolean r300_dsa_alpha_test_enabled(221struct pipe_depth_stencil_alpha_state *dsa)222{223/* We are interested only in the cases when alpha testing can kill224* a fragment. */225226return dsa->alpha_enabled && dsa->alpha_func != PIPE_FUNC_ALWAYS;227}228229static void r300_update_ztop(struct r300_context* r300)230{231struct r300_ztop_state* ztop_state =232(struct r300_ztop_state*)r300->ztop_state.state;233uint32_t old_ztop = ztop_state->z_buffer_top;234235/* This is important enough that I felt it warranted a comment.236*237* According to the docs, these are the conditions where ZTOP must be238* disabled:239* 1) Alpha testing enabled240* 2) Texture kill instructions in fragment shader241* 3) Chroma key culling enabled242* 4) W-buffering enabled243*244* The docs claim that for the first three cases, if no ZS writes happen,245* then ZTOP can be used.246*247* (3) will never apply since we do not support chroma-keyed operations.248* (4) will need to be re-examined (and this comment updated) if/when249* Hyper-Z becomes supported.250*251* Additionally, the following conditions require disabled ZTOP:252* 5) Depth writes in fragment shader253* 6) Outstanding occlusion queries254*255* This register causes stalls all the way from SC to CB when changed,256* but it is buffered on-chip so it does not hurt to write it if it has257* not changed.258*259* ~C.260*/261262/* ZS writes */263if (util_writes_depth_stencil(r300->dsa_state.state) &&264(r300_dsa_alpha_test_enabled(r300->dsa_state.state) || /* (1) */265r300_fs(r300)->shader->info.uses_kill)) { /* (2) */266ztop_state->z_buffer_top = R300_ZTOP_DISABLE;267} else if (r300_fragment_shader_writes_depth(r300_fs(r300))) { /* (5) */268ztop_state->z_buffer_top = R300_ZTOP_DISABLE;269} else if (r300->query_current) { /* (6) */270ztop_state->z_buffer_top = R300_ZTOP_DISABLE;271} else {272ztop_state->z_buffer_top = R300_ZTOP_ENABLE;273}274if (ztop_state->z_buffer_top != old_ztop)275r300_mark_atom_dirty(r300, &r300->ztop_state);276}277278void r300_update_hyperz_state(struct r300_context* r300)279{280r300_update_ztop(r300);281282if (r300->hyperz_state.dirty) {283r300_update_hyperz(r300);284}285}286287288