2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3 * Copyright 2009 Marek Olšák <maraeo@gmail.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
24 #include "r300_context.h"
25 #include "r300_hyperz.h"
29 /*****************************************************************************/
30 /* The HyperZ setup */
31 /*****************************************************************************/
33 static void r300_update_hyperz(struct r300_context
* r300
)
35 struct r300_hyperz_state
*z
=
36 (struct r300_hyperz_state
*)r300
->hyperz_state
.state
;
39 z
->sc_hyperz
= R300_SC_HYPERZ_ADJ_2
;
42 z
->zb_bw_cntl
|= R300_ZB_CB_CLEAR_CACHE_LINE_WRITE_ONLY
;
45 /*****************************************************************************/
47 /*****************************************************************************/
49 static boolean
r300_dsa_writes_stencil(
50 struct pipe_stencil_state
*s
)
52 return s
->enabled
&& s
->writemask
&&
53 (s
->fail_op
!= PIPE_STENCIL_OP_KEEP
||
54 s
->zfail_op
!= PIPE_STENCIL_OP_KEEP
||
55 s
->zpass_op
!= PIPE_STENCIL_OP_KEEP
);
58 static boolean
r300_dsa_writes_depth_stencil(
59 struct pipe_depth_stencil_alpha_state
*dsa
)
61 /* We are interested only in the cases when a depth or stencil value
64 if (dsa
->depth
.enabled
&& dsa
->depth
.writemask
&&
65 dsa
->depth
.func
!= PIPE_FUNC_NEVER
)
68 if (r300_dsa_writes_stencil(&dsa
->stencil
[0]) ||
69 r300_dsa_writes_stencil(&dsa
->stencil
[1]))
75 static boolean
r300_dsa_alpha_test_enabled(
76 struct pipe_depth_stencil_alpha_state
*dsa
)
78 /* We are interested only in the cases when alpha testing can kill
81 return dsa
->alpha
.enabled
&& dsa
->alpha
.func
!= PIPE_FUNC_ALWAYS
;
84 static void r300_update_ztop(struct r300_context
* r300
)
86 struct r300_ztop_state
* ztop_state
=
87 (struct r300_ztop_state
*)r300
->ztop_state
.state
;
88 uint32_t old_ztop
= ztop_state
->z_buffer_top
;
90 /* This is important enough that I felt it warranted a comment.
92 * According to the docs, these are the conditions where ZTOP must be
94 * 1) Alpha testing enabled
95 * 2) Texture kill instructions in fragment shader
96 * 3) Chroma key culling enabled
97 * 4) W-buffering enabled
99 * The docs claim that for the first three cases, if no ZS writes happen,
100 * then ZTOP can be used.
102 * (3) will never apply since we do not support chroma-keyed operations.
103 * (4) will need to be re-examined (and this comment updated) if/when
104 * Hyper-Z becomes supported.
106 * Additionally, the following conditions require disabled ZTOP:
107 * 5) Depth writes in fragment shader
108 * 6) Outstanding occlusion queries
110 * This register causes stalls all the way from SC to CB when changed,
111 * but it is buffered on-chip so it does not hurt to write it if it has
118 if (r300_dsa_writes_depth_stencil(r300
->dsa_state
.state
) &&
119 (r300_dsa_alpha_test_enabled(r300
->dsa_state
.state
) || /* (1) */
120 r300_fs(r300
)->shader
->info
.uses_kill
)) { /* (2) */
121 ztop_state
->z_buffer_top
= R300_ZTOP_DISABLE
;
122 } else if (r300_fragment_shader_writes_depth(r300_fs(r300
))) { /* (5) */
123 ztop_state
->z_buffer_top
= R300_ZTOP_DISABLE
;
124 } else if (r300
->query_current
) { /* (6) */
125 ztop_state
->z_buffer_top
= R300_ZTOP_DISABLE
;
127 ztop_state
->z_buffer_top
= R300_ZTOP_ENABLE
;
130 if (ztop_state
->z_buffer_top
!= old_ztop
)
131 r300
->ztop_state
.dirty
= TRUE
;
134 void r300_update_hyperz_state(struct r300_context
* r300
)
136 r300_update_ztop(r300
);
137 if (r300
->hyperz_state
.dirty
) {
138 r300_update_hyperz(r300
);