Merge branch 'lp-offset-twoside'
[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 zmask_in_use = FALSE;
143 boolean hiz_in_use = FALSE;
144
145 z->gb_z_peq_config = 0;
146 z->zb_bw_cntl = 0;
147 z->sc_hyperz = R300_SC_HYPERZ_ADJ_2;
148 z->flush = 0;
149
150 if (r300->cbzb_clear) {
151 z->zb_bw_cntl |= R300_ZB_CB_CLEAR_CACHE_LINE_WRITE_ONLY;
152 return;
153 }
154
155 if (!zstex)
156 return;
157
158 if (!r300->rws->get_value(r300->rws, R300_CAN_HYPERZ))
159 return;
160
161 zmask_in_use = zstex->zmask_in_use[fb->zsbuf->level];
162 hiz_in_use = zstex->hiz_in_use[fb->zsbuf->level];
163
164 /* Z fastfill. */
165 if (zmask_in_use) {
166 z->zb_bw_cntl |= R300_FAST_FILL_ENABLE; /* | R300_FORCE_COMPRESSED_STENCIL_VALUE_ENABLE;*/
167 }
168
169 /* Zbuffer compression. */
170 if (zmask_in_use && r300->z_compression) {
171 z->zb_bw_cntl |= R300_RD_COMP_ENABLE;
172 if (r300->z_decomp_rd == false)
173 z->zb_bw_cntl |= R300_WR_COMP_ENABLE;
174 }
175 /* RV350 and up optimizations. */
176 /* The section 10.4.9 in the docs is a lie. */
177 if (r300->z_compression == RV350_Z_COMPRESS_88)
178 z->gb_z_peq_config |= R300_GB_Z_PEQ_CONFIG_Z_PEQ_SIZE_8_8;
179
180 if (hiz_in_use) {
181 bool can_hiz = r300_can_hiz(r300);
182 if (can_hiz) {
183 z->zb_bw_cntl |= R300_HIZ_ENABLE;
184 z->sc_hyperz |= R300_SC_HYPERZ_ENABLE;
185 z->sc_hyperz |= r300_get_sc_hz_max(r300);
186 z->zb_bw_cntl |= r300_get_hiz_min(r300);
187 }
188 }
189
190 /* R500-specific features and optimizations. */
191 if (r300->screen->caps.is_r500) {
192 z->zb_bw_cntl |= R500_HIZ_FP_EXP_BITS_3;
193 z->zb_bw_cntl |=
194 R500_HIZ_EQUAL_REJECT_ENABLE |
195 R500_PEQ_PACKING_ENABLE |
196 R500_COVERED_PTR_MASKING_ENABLE;
197 }
198 }
199
200 /*****************************************************************************/
201 /* The ZTOP state */
202 /*****************************************************************************/
203
204 static boolean r300_dsa_writes_stencil(
205 struct pipe_stencil_state *s)
206 {
207 return s->enabled && s->writemask &&
208 (s->fail_op != PIPE_STENCIL_OP_KEEP ||
209 s->zfail_op != PIPE_STENCIL_OP_KEEP ||
210 s->zpass_op != PIPE_STENCIL_OP_KEEP);
211 }
212
213 static boolean r300_dsa_writes_depth_stencil(
214 struct pipe_depth_stencil_alpha_state *dsa)
215 {
216 /* We are interested only in the cases when a depth or stencil value
217 * can be changed. */
218
219 if (dsa->depth.enabled && dsa->depth.writemask &&
220 dsa->depth.func != PIPE_FUNC_NEVER)
221 return TRUE;
222
223 if (r300_dsa_writes_stencil(&dsa->stencil[0]) ||
224 r300_dsa_writes_stencil(&dsa->stencil[1]))
225 return TRUE;
226
227 return FALSE;
228 }
229
230 static boolean r300_dsa_alpha_test_enabled(
231 struct pipe_depth_stencil_alpha_state *dsa)
232 {
233 /* We are interested only in the cases when alpha testing can kill
234 * a fragment. */
235
236 return dsa->alpha.enabled && dsa->alpha.func != PIPE_FUNC_ALWAYS;
237 }
238
239 static void r300_update_ztop(struct r300_context* r300)
240 {
241 struct r300_ztop_state* ztop_state =
242 (struct r300_ztop_state*)r300->ztop_state.state;
243 uint32_t old_ztop = ztop_state->z_buffer_top;
244
245 /* This is important enough that I felt it warranted a comment.
246 *
247 * According to the docs, these are the conditions where ZTOP must be
248 * disabled:
249 * 1) Alpha testing enabled
250 * 2) Texture kill instructions in fragment shader
251 * 3) Chroma key culling enabled
252 * 4) W-buffering enabled
253 *
254 * The docs claim that for the first three cases, if no ZS writes happen,
255 * then ZTOP can be used.
256 *
257 * (3) will never apply since we do not support chroma-keyed operations.
258 * (4) will need to be re-examined (and this comment updated) if/when
259 * Hyper-Z becomes supported.
260 *
261 * Additionally, the following conditions require disabled ZTOP:
262 * 5) Depth writes in fragment shader
263 * 6) Outstanding occlusion queries
264 *
265 * This register causes stalls all the way from SC to CB when changed,
266 * but it is buffered on-chip so it does not hurt to write it if it has
267 * not changed.
268 *
269 * ~C.
270 */
271
272 /* ZS writes */
273 if (r300_dsa_writes_depth_stencil(r300->dsa_state.state) &&
274 (r300_dsa_alpha_test_enabled(r300->dsa_state.state) || /* (1) */
275 r300_fs(r300)->shader->info.uses_kill)) { /* (2) */
276 ztop_state->z_buffer_top = R300_ZTOP_DISABLE;
277 } else if (r300_fragment_shader_writes_depth(r300_fs(r300))) { /* (5) */
278 ztop_state->z_buffer_top = R300_ZTOP_DISABLE;
279 } else if (r300->query_current) { /* (6) */
280 ztop_state->z_buffer_top = R300_ZTOP_DISABLE;
281 } else {
282 ztop_state->z_buffer_top = R300_ZTOP_ENABLE;
283 }
284 if (ztop_state->z_buffer_top != old_ztop)
285 r300->ztop_state.dirty = TRUE;
286 }
287
288 #define ALIGN_DIVUP(x, y) (((x) + (y) - 1) / (y))
289
290 static void r300_update_hiz_clear(struct r300_context *r300)
291 {
292 struct pipe_framebuffer_state *fb =
293 (struct pipe_framebuffer_state*)r300->fb_state.state;
294 uint32_t height;
295
296 height = ALIGN_DIVUP(fb->zsbuf->height, 4);
297 r300->hiz_clear.size = height * 4;
298 }
299
300 static void r300_update_zmask_clear(struct r300_context *r300)
301 {
302 struct pipe_framebuffer_state *fb =
303 (struct pipe_framebuffer_state*)r300->fb_state.state;
304 uint32_t height;
305 int mult;
306
307 if (r300->z_compression == RV350_Z_COMPRESS_88)
308 mult = 8;
309 else
310 mult = 4;
311
312 height = ALIGN_DIVUP(fb->zsbuf->height, mult);
313
314 r300->zmask_clear.size = height * 4;
315 }
316
317 void r300_update_hyperz_state(struct r300_context* r300)
318 {
319 r300_update_ztop(r300);
320 if (r300->hyperz_state.dirty) {
321 r300_update_hyperz(r300);
322 }
323
324 if (r300->hiz_clear.dirty) {
325 r300_update_hiz_clear(r300);
326 }
327 if (r300->zmask_clear.dirty) {
328 r300_update_zmask_clear(r300);
329 }
330 }
331
332 void r300_hiz_alloc_block(struct r300_context *r300, struct r300_surface *surf)
333 {
334 struct r300_texture *tex;
335 uint32_t zsize, ndw;
336 int level = surf->base.level;
337
338 tex = r300_texture(surf->base.texture);
339
340 if (tex->hiz_mem[level])
341 return;
342
343 zsize = tex->desc.layer_size_in_bytes[level];
344 zsize /= util_format_get_blocksize(tex->desc.b.b.format);
345 ndw = ALIGN_DIVUP(zsize, 64);
346
347 tex->hiz_mem[level] = u_mmAllocMem(r300->hiz_mm, ndw, 0, 0);
348 return;
349 }
350
351 void r300_zmask_alloc_block(struct r300_context *r300, struct r300_surface *surf, int compress)
352 {
353 int bsize = 256;
354 uint32_t zsize, ndw;
355 int level = surf->base.level;
356 struct r300_texture *tex;
357
358 tex = r300_texture(surf->base.texture);
359
360 /* We currently don't handle decompression for 3D textures and cubemaps
361 * correctly. */
362 if (tex->desc.b.b.target != PIPE_TEXTURE_1D &&
363 tex->desc.b.b.target != PIPE_TEXTURE_2D &&
364 tex->desc.b.b.target != PIPE_TEXTURE_RECT)
365 return;
366
367 /* Cannot flush zmask of 16-bit zbuffers. */
368 if (util_format_get_blocksizebits(tex->desc.b.b.format) == 16)
369 return;
370
371 if (tex->zmask_mem[level])
372 return;
373
374 zsize = tex->desc.layer_size_in_bytes[level];
375 zsize /= util_format_get_blocksize(tex->desc.b.b.format);
376
377 /* each zmask dword represents 16 4x4 blocks - which is 256 pixels
378 or 16 8x8 depending on the gb peq flag = 1024 pixels */
379 if (compress == RV350_Z_COMPRESS_88)
380 bsize = 1024;
381
382 ndw = ALIGN_DIVUP(zsize, bsize);
383 tex->zmask_mem[level] = u_mmAllocMem(r300->zmask_mm, ndw, 0, 0);
384 return;
385 }
386
387 boolean r300_hyperz_init_mm(struct r300_context *r300)
388 {
389 struct r300_screen* r300screen = r300->screen;
390 int frag_pipes = r300screen->caps.num_frag_pipes;
391
392 r300->zmask_mm = u_mmInit(0, r300screen->caps.zmask_ram * frag_pipes);
393 if (!r300->zmask_mm)
394 return FALSE;
395
396 if (r300screen->caps.hiz_ram) {
397 r300->hiz_mm = u_mmInit(0, r300screen->caps.hiz_ram * frag_pipes);
398 if (!r300->hiz_mm) {
399 u_mmDestroy(r300->zmask_mm);
400 r300->zmask_mm = NULL;
401 return FALSE;
402 }
403 }
404
405 return TRUE;
406 }
407
408 void r300_hyperz_destroy_mm(struct r300_context *r300)
409 {
410 struct r300_screen* r300screen = r300->screen;
411
412 if (r300screen->caps.hiz_ram) {
413 u_mmDestroy(r300->hiz_mm);
414 r300->hiz_mm = NULL;
415 }
416
417 u_mmDestroy(r300->zmask_mm);
418 r300->zmask_mm = NULL;
419 }