r300g: remove redundant state variable hyperz_locked
[mesa.git] / src / gallium / drivers / r300 / r300_hyperz.c
1 /*
2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3 * Copyright 2009 Marek Olšák <maraeo@gmail.com>
4 *
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:
11 *
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
14 * Software.
15 *
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. */
23
24 #include "r300_context.h"
25 #include "r300_reg.h"
26 #include "r300_fs.h"
27
28 #include "util/u_format.h"
29 #include "util/u_mm.h"
30
31 /*
32 HiZ rules - taken from various docs
33 1. HiZ only works on depth values
34 2. Cannot HiZ if stencil fail or zfail is !KEEP
35 3. on R300/400, HiZ is disabled if depth test is EQUAL
36 4. comparison changes without clears usually mean disabling HiZ
37 */
38 /*****************************************************************************/
39 /* The HyperZ setup */
40 /*****************************************************************************/
41
42 static enum r300_hiz_func r300_get_hiz_func(struct r300_context *r300)
43 {
44 struct r300_dsa_state *dsa = r300->dsa_state.state;
45
46 if (!dsa->dsa.depth.enabled || !dsa->dsa.depth.writemask)
47 return HIZ_FUNC_NONE;
48
49 switch (dsa->dsa.depth.func) {
50 case PIPE_FUNC_NEVER:
51 case PIPE_FUNC_EQUAL:
52 case PIPE_FUNC_NOTEQUAL:
53 case PIPE_FUNC_ALWAYS:
54 return HIZ_FUNC_NONE;
55
56 case PIPE_FUNC_LESS:
57 case PIPE_FUNC_LEQUAL:
58 return HIZ_FUNC_MAX;
59
60 case PIPE_FUNC_GREATER:
61 case PIPE_FUNC_GEQUAL:
62 return HIZ_FUNC_MIN;
63
64 default:
65 assert(0);
66 return HIZ_FUNC_NONE;
67 }
68 }
69
70 /* Return what's used for the depth test (either minimum or maximum). */
71 static unsigned r300_get_sc_hz_max(struct r300_context *r300)
72 {
73 struct r300_dsa_state *dsa = r300->dsa_state.state;
74 unsigned func = dsa->dsa.depth.func;
75
76 return func >= PIPE_FUNC_GREATER ? R300_SC_HYPERZ_MAX : R300_SC_HYPERZ_MIN;
77 }
78
79 static boolean r300_is_hiz_func_valid(struct r300_context *r300)
80 {
81 struct r300_dsa_state *dsa = r300->dsa_state.state;
82 unsigned func = dsa->dsa.depth.func;
83
84 if (r300->hiz_func == HIZ_FUNC_NONE)
85 return TRUE;
86
87 /* func1 is less/lessthan */
88 if (r300->hiz_func == HIZ_FUNC_MAX &&
89 (func == PIPE_FUNC_GEQUAL || func == PIPE_FUNC_GREATER))
90 return FALSE;
91
92 /* func1 is greater/greaterthan */
93 if (r300->hiz_func == HIZ_FUNC_MIN &&
94 (func == PIPE_FUNC_LESS || func == PIPE_FUNC_LEQUAL))
95 return FALSE;
96
97 return TRUE;
98 }
99
100 static boolean r300_dsa_stencil_op_not_keep(struct pipe_stencil_state *s)
101 {
102 return s->enabled && (s->fail_op != PIPE_STENCIL_OP_KEEP ||
103 s->zfail_op != PIPE_STENCIL_OP_KEEP);
104 }
105
106 static boolean r300_can_hiz(struct r300_context *r300)
107 {
108 struct r300_dsa_state *dsa = r300->dsa_state.state;
109 struct r300_screen *r300screen = r300->screen;
110
111 /* shader writes depth - no HiZ */
112 if (r300_fragment_shader_writes_depth(r300_fs(r300))) /* (5) */
113 return FALSE;
114
115 if (r300->query_current)
116 return FALSE;
117
118 /* if stencil fail/zfail op is not KEEP */
119 if (r300_dsa_stencil_op_not_keep(&dsa->dsa.stencil[0]) ||
120 r300_dsa_stencil_op_not_keep(&dsa->dsa.stencil[1]))
121 return FALSE;
122
123 if (dsa->dsa.depth.enabled) {
124 /* if depth func is EQUAL pre-r500 */
125 if (dsa->dsa.depth.func == PIPE_FUNC_EQUAL && !r300screen->caps.is_r500)
126 return FALSE;
127
128 /* if depth func is NOTEQUAL */
129 if (dsa->dsa.depth.func == PIPE_FUNC_NOTEQUAL)
130 return FALSE;
131 }
132 return TRUE;
133 }
134
135 static void r300_update_hyperz(struct r300_context* r300)
136 {
137 struct r300_hyperz_state *z =
138 (struct r300_hyperz_state*)r300->hyperz_state.state;
139 struct pipe_framebuffer_state *fb =
140 (struct pipe_framebuffer_state*)r300->fb_state.state;
141 struct r300_resource *zstex =
142 fb->zsbuf ? r300_resource(fb->zsbuf->texture) : NULL;
143
144 z->gb_z_peq_config = 0;
145 z->zb_bw_cntl = 0;
146 z->sc_hyperz = R300_SC_HYPERZ_ADJ_2;
147 z->flush = 0;
148
149 if (r300->cbzb_clear) {
150 z->zb_bw_cntl |= R300_ZB_CB_CLEAR_CACHE_LINE_WRITE_ONLY;
151 return;
152 }
153
154 if (!zstex ||
155 !r300->rws->get_value(r300->rws, RADEON_VID_CAN_HYPERZ))
156 return;
157
158 /* Zbuffer compression. */
159 if (r300->zmask_in_use && !r300->locked_zbuffer) {
160 z->zb_bw_cntl |= R300_FAST_FILL_ENABLE |
161 /*R300_FORCE_COMPRESSED_STENCIL_VALUE_ENABLE |*/
162 R300_RD_COMP_ENABLE;
163
164 if (!r300->zmask_decompress) {
165 z->zb_bw_cntl |= R300_WR_COMP_ENABLE;
166 }
167 }
168
169 if (zstex->tex.zcomp8x8[fb->zsbuf->u.tex.level]) {
170 z->gb_z_peq_config |= R300_GB_Z_PEQ_CONFIG_Z_PEQ_SIZE_8_8;
171 }
172
173 /* HiZ. */
174 if (r300->hiz_in_use && !r300->locked_zbuffer) {
175 /* Set the HiZ function if needed. */
176 if (r300->hiz_func == HIZ_FUNC_NONE) {
177 r300->hiz_func = r300_get_hiz_func(r300);
178 }
179
180 /* If the depth function is inverted, HiZ must be disabled. */
181 if (!r300_is_hiz_func_valid(r300)) {
182 r300->hiz_in_use = FALSE;
183 } else if (r300_can_hiz(r300)) {
184 /* Setup the HiZ bits. */
185 z->zb_bw_cntl |=
186 R300_HIZ_ENABLE |
187 (r300->hiz_func == HIZ_FUNC_MIN ? R300_HIZ_MIN : R300_HIZ_MAX);
188
189 z->sc_hyperz |= R300_SC_HYPERZ_ENABLE |
190 r300_get_sc_hz_max(r300);
191
192 if (r300->screen->caps.is_r500) {
193 z->zb_bw_cntl |= R500_HIZ_EQUAL_REJECT_ENABLE;
194 }
195 }
196 }
197
198 /* R500-specific features and optimizations. */
199 if (r300->screen->caps.is_r500) {
200 z->zb_bw_cntl |= R500_PEQ_PACKING_ENABLE |
201 R500_COVERED_PTR_MASKING_ENABLE;
202 }
203 }
204
205 /*****************************************************************************/
206 /* The ZTOP state */
207 /*****************************************************************************/
208
209 static boolean r300_dsa_writes_stencil(
210 struct pipe_stencil_state *s)
211 {
212 return s->enabled && s->writemask &&
213 (s->fail_op != PIPE_STENCIL_OP_KEEP ||
214 s->zfail_op != PIPE_STENCIL_OP_KEEP ||
215 s->zpass_op != PIPE_STENCIL_OP_KEEP);
216 }
217
218 static boolean r300_dsa_writes_depth_stencil(
219 struct pipe_depth_stencil_alpha_state *dsa)
220 {
221 /* We are interested only in the cases when a depth or stencil value
222 * can be changed. */
223
224 if (dsa->depth.enabled && dsa->depth.writemask &&
225 dsa->depth.func != PIPE_FUNC_NEVER)
226 return TRUE;
227
228 if (r300_dsa_writes_stencil(&dsa->stencil[0]) ||
229 r300_dsa_writes_stencil(&dsa->stencil[1]))
230 return TRUE;
231
232 return FALSE;
233 }
234
235 static boolean r300_dsa_alpha_test_enabled(
236 struct pipe_depth_stencil_alpha_state *dsa)
237 {
238 /* We are interested only in the cases when alpha testing can kill
239 * a fragment. */
240
241 return dsa->alpha.enabled && dsa->alpha.func != PIPE_FUNC_ALWAYS;
242 }
243
244 static void r300_update_ztop(struct r300_context* r300)
245 {
246 struct r300_ztop_state* ztop_state =
247 (struct r300_ztop_state*)r300->ztop_state.state;
248 uint32_t old_ztop = ztop_state->z_buffer_top;
249
250 /* This is important enough that I felt it warranted a comment.
251 *
252 * According to the docs, these are the conditions where ZTOP must be
253 * disabled:
254 * 1) Alpha testing enabled
255 * 2) Texture kill instructions in fragment shader
256 * 3) Chroma key culling enabled
257 * 4) W-buffering enabled
258 *
259 * The docs claim that for the first three cases, if no ZS writes happen,
260 * then ZTOP can be used.
261 *
262 * (3) will never apply since we do not support chroma-keyed operations.
263 * (4) will need to be re-examined (and this comment updated) if/when
264 * Hyper-Z becomes supported.
265 *
266 * Additionally, the following conditions require disabled ZTOP:
267 * 5) Depth writes in fragment shader
268 * 6) Outstanding occlusion queries
269 *
270 * This register causes stalls all the way from SC to CB when changed,
271 * but it is buffered on-chip so it does not hurt to write it if it has
272 * not changed.
273 *
274 * ~C.
275 */
276
277 /* ZS writes */
278 if (r300_dsa_writes_depth_stencil(r300->dsa_state.state) &&
279 (r300_dsa_alpha_test_enabled(r300->dsa_state.state) || /* (1) */
280 r300_fs(r300)->shader->info.uses_kill)) { /* (2) */
281 ztop_state->z_buffer_top = R300_ZTOP_DISABLE;
282 } else if (r300_fragment_shader_writes_depth(r300_fs(r300))) { /* (5) */
283 ztop_state->z_buffer_top = R300_ZTOP_DISABLE;
284 } else if (r300->query_current) { /* (6) */
285 ztop_state->z_buffer_top = R300_ZTOP_DISABLE;
286 } else {
287 ztop_state->z_buffer_top = R300_ZTOP_ENABLE;
288 }
289 if (ztop_state->z_buffer_top != old_ztop)
290 r300_mark_atom_dirty(r300, &r300->ztop_state);
291 }
292
293 void r300_update_hyperz_state(struct r300_context* r300)
294 {
295 r300_update_ztop(r300);
296
297 if (r300->hyperz_state.dirty) {
298 r300_update_hyperz(r300);
299 }
300 }