r300g: fix and re-enable 8x8 zbuffer compression mode
[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_hyperz.h"
26 #include "r300_reg.h"
27 #include "r300_fs.h"
28 #include "r300_winsys.h"
29
30 #include "util/u_format.h"
31 #include "util/u_mm.h"
32
33 /*
34 HiZ rules - taken from various docs
35 1. HiZ only works on depth values
36 2. Cannot HiZ if stencil fail or zfail is !KEEP
37 3. on R300/400, HiZ is disabled if depth test is EQUAL
38 4. comparison changes without clears usually mean disabling HiZ
39 */
40 /*****************************************************************************/
41 /* The HyperZ setup */
42 /*****************************************************************************/
43
44 static bool r300_get_sc_hz_max(struct r300_context *r300)
45 {
46 struct r300_dsa_state *dsa_state = r300->dsa_state.state;
47 int func = dsa_state->z_stencil_control & R300_ZS_MASK;
48 int ret = R300_SC_HYPERZ_MIN;
49
50 if (func >= R300_ZS_GEQUAL && func <= R300_ZS_ALWAYS)
51 ret = R300_SC_HYPERZ_MAX;
52 return ret;
53 }
54
55 static bool r300_zfunc_same_direction(int func1, int func2)
56 {
57 /* func1 is less/lessthan */
58 if ((func1 == R300_ZS_LESS || func1 == R300_ZS_LEQUAL) &&
59 (func2 == R300_ZS_EQUAL || func2 == R300_ZS_GEQUAL ||
60 func2 == R300_ZS_GREATER))
61 return FALSE;
62
63 /* func1 is greater/greaterthan */
64 if ((func1 == R300_ZS_GEQUAL || func1 == R300_ZS_GREATER) &&
65 (func2 == R300_ZS_LESS || func2 == R300_ZS_LEQUAL))
66 return FALSE;
67
68 return TRUE;
69 }
70
71 static int r300_get_hiz_min(struct r300_context *r300)
72 {
73 struct r300_dsa_state *dsa_state = r300->dsa_state.state;
74 int func = dsa_state->z_stencil_control & R300_ZS_MASK;
75 int ret = R300_HIZ_MIN;
76
77 if (func == R300_ZS_LESS || func == R300_ZS_LEQUAL)
78 ret = R300_HIZ_MAX;
79 return ret;
80 }
81
82 static boolean r300_dsa_stencil_op_not_keep(struct pipe_stencil_state *s)
83 {
84 if (s->enabled && (s->fail_op != PIPE_STENCIL_OP_KEEP ||
85 s->zfail_op != PIPE_STENCIL_OP_KEEP))
86 return TRUE;
87 return FALSE;
88 }
89
90 static boolean r300_can_hiz(struct r300_context *r300)
91 {
92 struct r300_dsa_state *dsa_state = r300->dsa_state.state;
93 struct pipe_depth_stencil_alpha_state *dsa = &dsa_state->dsa;
94 struct r300_screen* r300screen = r300->screen;
95 struct r300_hyperz_state *z = r300->hyperz_state.state;
96
97 /* shader writes depth - no HiZ */
98 if (r300_fragment_shader_writes_depth(r300_fs(r300))) /* (5) */
99 return FALSE;
100
101 if (r300->query_current)
102 return FALSE;
103 /* if stencil fail/zfail op is not KEEP */
104 if (r300_dsa_stencil_op_not_keep(&dsa->stencil[0]) ||
105 r300_dsa_stencil_op_not_keep(&dsa->stencil[1]))
106 return FALSE;
107
108 if (dsa->depth.enabled) {
109 /* if depth func is EQUAL pre-r500 */
110 if (dsa->depth.func == PIPE_FUNC_EQUAL && !r300screen->caps.is_r500)
111 return FALSE;
112 /* if depth func is NOTEQUAL */
113 if (dsa->depth.func == PIPE_FUNC_NOTEQUAL)
114 return FALSE;
115 }
116 /* depth comparison function - if just cleared save and return okay */
117 if (z->current_func == -1) {
118 int func = dsa_state->z_stencil_control & R300_ZS_MASK;
119 if (func != 0 && func != 7)
120 z->current_func = dsa_state->z_stencil_control & R300_ZS_MASK;
121 } else {
122 /* simple don't change */
123 if (!r300_zfunc_same_direction(z->current_func,
124 (dsa_state->z_stencil_control & R300_ZS_MASK))) {
125 DBG(r300, DBG_HYPERZ,
126 "z func changed direction - disabling hyper-z %d -> %d\n",
127 z->current_func, dsa_state->z_stencil_control);
128 return FALSE;
129 }
130 }
131 return TRUE;
132 }
133
134 static void r300_update_hyperz(struct r300_context* r300)
135 {
136 struct r300_hyperz_state *z =
137 (struct r300_hyperz_state*)r300->hyperz_state.state;
138 struct pipe_framebuffer_state *fb =
139 (struct pipe_framebuffer_state*)r300->fb_state.state;
140 struct r300_texture *zstex =
141 fb->zsbuf ? r300_texture(fb->zsbuf->texture) : NULL;
142 boolean hiz_in_use = FALSE;
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 return;
156
157 if (!r300->rws->get_value(r300->rws, R300_CAN_HYPERZ))
158 return;
159
160 hiz_in_use = zstex->hiz_in_use[fb->zsbuf->u.tex.level];
161
162 /* Zbuffer compression. */
163 if (r300->zmask_in_use && !r300->zmask_locked) {
164 z->zb_bw_cntl |= R300_FAST_FILL_ENABLE |
165 /*R300_FORCE_COMPRESSED_STENCIL_VALUE_ENABLE |*/
166 R300_RD_COMP_ENABLE;
167
168 if (!r300->zmask_decompress) {
169 z->zb_bw_cntl |= R300_WR_COMP_ENABLE;
170 }
171 }
172
173 if (zstex->desc.zcomp8x8[fb->zsbuf->u.tex.level]) {
174 z->gb_z_peq_config |= R300_GB_Z_PEQ_CONFIG_Z_PEQ_SIZE_8_8;
175 }
176
177 if (hiz_in_use && r300_can_hiz(r300)) {
178 z->zb_bw_cntl |= R300_HIZ_ENABLE |
179 r300_get_hiz_min(r300);
180
181 z->sc_hyperz |= R300_SC_HYPERZ_ENABLE |
182 r300_get_sc_hz_max(r300);
183
184 if (r300->screen->caps.is_r500) {
185 z->zb_bw_cntl |= R500_HIZ_FP_EXP_BITS_3 |
186 R500_HIZ_EQUAL_REJECT_ENABLE;
187 }
188 }
189
190 /* R500-specific features and optimizations. */
191 if (r300->screen->caps.is_r500) {
192 z->zb_bw_cntl |= R500_PEQ_PACKING_ENABLE |
193 R500_COVERED_PTR_MASKING_ENABLE;
194 }
195 }
196
197 /*****************************************************************************/
198 /* The ZTOP state */
199 /*****************************************************************************/
200
201 static boolean r300_dsa_writes_stencil(
202 struct pipe_stencil_state *s)
203 {
204 return s->enabled && s->writemask &&
205 (s->fail_op != PIPE_STENCIL_OP_KEEP ||
206 s->zfail_op != PIPE_STENCIL_OP_KEEP ||
207 s->zpass_op != PIPE_STENCIL_OP_KEEP);
208 }
209
210 static boolean r300_dsa_writes_depth_stencil(
211 struct pipe_depth_stencil_alpha_state *dsa)
212 {
213 /* We are interested only in the cases when a depth or stencil value
214 * can be changed. */
215
216 if (dsa->depth.enabled && dsa->depth.writemask &&
217 dsa->depth.func != PIPE_FUNC_NEVER)
218 return TRUE;
219
220 if (r300_dsa_writes_stencil(&dsa->stencil[0]) ||
221 r300_dsa_writes_stencil(&dsa->stencil[1]))
222 return TRUE;
223
224 return FALSE;
225 }
226
227 static boolean r300_dsa_alpha_test_enabled(
228 struct pipe_depth_stencil_alpha_state *dsa)
229 {
230 /* We are interested only in the cases when alpha testing can kill
231 * a fragment. */
232
233 return dsa->alpha.enabled && dsa->alpha.func != PIPE_FUNC_ALWAYS;
234 }
235
236 static void r300_update_ztop(struct r300_context* r300)
237 {
238 struct r300_ztop_state* ztop_state =
239 (struct r300_ztop_state*)r300->ztop_state.state;
240 uint32_t old_ztop = ztop_state->z_buffer_top;
241
242 /* This is important enough that I felt it warranted a comment.
243 *
244 * According to the docs, these are the conditions where ZTOP must be
245 * disabled:
246 * 1) Alpha testing enabled
247 * 2) Texture kill instructions in fragment shader
248 * 3) Chroma key culling enabled
249 * 4) W-buffering enabled
250 *
251 * The docs claim that for the first three cases, if no ZS writes happen,
252 * then ZTOP can be used.
253 *
254 * (3) will never apply since we do not support chroma-keyed operations.
255 * (4) will need to be re-examined (and this comment updated) if/when
256 * Hyper-Z becomes supported.
257 *
258 * Additionally, the following conditions require disabled ZTOP:
259 * 5) Depth writes in fragment shader
260 * 6) Outstanding occlusion queries
261 *
262 * This register causes stalls all the way from SC to CB when changed,
263 * but it is buffered on-chip so it does not hurt to write it if it has
264 * not changed.
265 *
266 * ~C.
267 */
268
269 /* ZS writes */
270 if (r300_dsa_writes_depth_stencil(r300->dsa_state.state) &&
271 (r300_dsa_alpha_test_enabled(r300->dsa_state.state) || /* (1) */
272 r300_fs(r300)->shader->info.uses_kill)) { /* (2) */
273 ztop_state->z_buffer_top = R300_ZTOP_DISABLE;
274 } else if (r300_fragment_shader_writes_depth(r300_fs(r300))) { /* (5) */
275 ztop_state->z_buffer_top = R300_ZTOP_DISABLE;
276 } else if (r300->query_current) { /* (6) */
277 ztop_state->z_buffer_top = R300_ZTOP_DISABLE;
278 } else {
279 ztop_state->z_buffer_top = R300_ZTOP_ENABLE;
280 }
281 if (ztop_state->z_buffer_top != old_ztop)
282 r300_mark_atom_dirty(r300, &r300->ztop_state);
283 }
284
285 #define ALIGN_DIVUP(x, y) (((x) + (y) - 1) / (y))
286
287 static void r300_update_hiz_clear(struct r300_context *r300)
288 {
289 struct pipe_framebuffer_state *fb =
290 (struct pipe_framebuffer_state*)r300->fb_state.state;
291 uint32_t height;
292
293 height = ALIGN_DIVUP(fb->zsbuf->height, 4);
294 r300->hiz_clear.size = height * 4;
295 }
296
297 void r300_update_hyperz_state(struct r300_context* r300)
298 {
299 r300_update_ztop(r300);
300
301 if (r300->hyperz_state.dirty) {
302 r300_update_hyperz(r300);
303 }
304
305 if (r300->hiz_clear.dirty) {
306 r300_update_hiz_clear(r300);
307 }
308 }
309
310 void r300_hiz_alloc_block(struct r300_context *r300, struct r300_surface *surf)
311 {
312 struct r300_texture *tex;
313 uint32_t zsize, ndw;
314 int level = surf->base.u.tex.level;
315
316 tex = r300_texture(surf->base.texture);
317
318 if (tex->hiz_mem[level])
319 return;
320
321 zsize = tex->desc.layer_size_in_bytes[level];
322 zsize /= util_format_get_blocksize(tex->desc.b.b.format);
323 ndw = ALIGN_DIVUP(zsize, 64);
324
325 tex->hiz_mem[level] = u_mmAllocMem(r300->hiz_mm, ndw, 0, 0);
326 }
327
328 boolean r300_hyperz_init_mm(struct r300_context *r300)
329 {
330 struct r300_screen* r300screen = r300->screen;
331 int frag_pipes = r300screen->caps.num_frag_pipes;
332
333 if (r300screen->caps.hiz_ram) {
334 r300->hiz_mm = u_mmInit(0, r300screen->caps.hiz_ram * frag_pipes);
335 if (!r300->hiz_mm) {
336 return FALSE;
337 }
338 }
339
340 return TRUE;
341 }
342
343 void r300_hyperz_destroy_mm(struct r300_context *r300)
344 {
345 struct r300_screen* r300screen = r300->screen;
346
347 if (r300screen->caps.hiz_ram) {
348 u_mmDestroy(r300->hiz_mm);
349 r300->hiz_mm = NULL;
350 }
351 }