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"
28 #include "r300_winsys.h"
30 #include "util/u_format.h"
31 #include "util/u_mm.h"
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
40 /*****************************************************************************/
41 /* The HyperZ setup */
42 /*****************************************************************************/
44 static bool r300_get_sc_hz_max(struct r300_context
*r300
)
46 struct r300_dsa_state
*dsa_state
= r300
->dsa_state
.state
;
47 int func
= dsa_state
->z_stencil_control
& 0x7;
48 int ret
= R300_SC_HYPERZ_MIN
;
50 if (func
>= 4 && func
<= 7)
51 ret
= R300_SC_HYPERZ_MAX
;
55 static bool r300_zfunc_same_direction(int func1
, int func2
)
57 /* func1 is less/lessthan */
58 if (func1
== 1 || func1
== 2)
59 if (func2
== 3 || func2
== 4 || func2
== 5)
62 if (func2
== 1 || func2
== 2)
63 if (func1
== 4 || func1
== 5)
68 static int r300_get_hiz_min(struct r300_context
*r300
)
70 struct r300_dsa_state
*dsa_state
= r300
->dsa_state
.state
;
71 int func
= dsa_state
->z_stencil_control
& 0x7;
72 int ret
= R300_HIZ_MIN
;
74 if (func
== 1 || func
== 2)
79 static boolean
r300_dsa_stencil_op_not_keep(struct pipe_stencil_state
*s
)
81 if (s
->enabled
&& (s
->fail_op
!= PIPE_STENCIL_OP_KEEP
||
82 s
->zfail_op
!= PIPE_STENCIL_OP_KEEP
))
87 static boolean
r300_can_hiz(struct r300_context
*r300
)
89 struct r300_dsa_state
*dsa_state
= r300
->dsa_state
.state
;
90 struct pipe_depth_stencil_alpha_state
*dsa
= &dsa_state
->dsa
;
91 struct r300_screen
* r300screen
= r300
->screen
;
92 struct r300_hyperz_state
*z
= r300
->hyperz_state
.state
;
94 /* shader writes depth - no HiZ */
95 if (r300_fragment_shader_writes_depth(r300_fs(r300
))) /* (5) */
98 if (r300
->query_current
)
100 /* if stencil fail/zfail op is not KEEP */
101 if (r300_dsa_stencil_op_not_keep(&dsa
->stencil
[0]) ||
102 r300_dsa_stencil_op_not_keep(&dsa
->stencil
[1]))
105 if (dsa
->depth
.enabled
) {
106 /* if depth func is EQUAL pre-r500 */
107 if (dsa
->depth
.func
== PIPE_FUNC_EQUAL
&& !r300screen
->caps
.is_r500
)
109 /* if depth func is NOTEQUAL */
110 if (dsa
->depth
.func
== PIPE_FUNC_NOTEQUAL
)
113 /* depth comparison function - if just cleared save and return okay */
114 if (z
->current_func
== -1) {
115 int func
= dsa_state
->z_stencil_control
& 0x7;
116 if (func
!= 0 && func
!= 7)
117 z
->current_func
= dsa_state
->z_stencil_control
& 0x7;
119 /* simple don't change */
120 if (!r300_zfunc_same_direction(z
->current_func
, (dsa_state
->z_stencil_control
& 0x7))) {
121 DBG(r300
, DBG_HYPERZ
, "z func changed direction - disabling hyper-z %d -> %d\n", z
->current_func
, dsa_state
->z_stencil_control
);
128 static void r300_update_hyperz(struct r300_context
* r300
)
130 struct r300_hyperz_state
*z
=
131 (struct r300_hyperz_state
*)r300
->hyperz_state
.state
;
132 struct pipe_framebuffer_state
*fb
=
133 (struct pipe_framebuffer_state
*)r300
->fb_state
.state
;
134 struct r300_texture
*zstex
=
135 fb
->zsbuf
? r300_texture(fb
->zsbuf
->texture
) : NULL
;
136 boolean zmask_in_use
= FALSE
;
137 boolean hiz_in_use
= FALSE
;
139 z
->gb_z_peq_config
= 0;
141 z
->sc_hyperz
= R300_SC_HYPERZ_ADJ_2
;
144 if (r300
->cbzb_clear
) {
145 z
->zb_bw_cntl
|= R300_ZB_CB_CLEAR_CACHE_LINE_WRITE_ONLY
;
152 if (!r300
->rws
->get_value(r300
->rws
, R300_CAN_HYPERZ
))
155 zmask_in_use
= zstex
->zmask_in_use
[fb
->zsbuf
->level
];
156 hiz_in_use
= zstex
->hiz_in_use
[fb
->zsbuf
->level
];
160 z
->zb_bw_cntl
|= R300_FAST_FILL_ENABLE
; /* | R300_FORCE_COMPRESSED_STENCIL_VALUE_ENABLE;*/
163 /* Zbuffer compression. */
164 if (zmask_in_use
&& r300
->z_compression
) {
165 z
->zb_bw_cntl
|= R300_RD_COMP_ENABLE
;
166 if (r300
->z_decomp_rd
== false)
167 z
->zb_bw_cntl
|= R300_WR_COMP_ENABLE
;
169 /* RV350 and up optimizations. */
170 /* The section 10.4.9 in the docs is a lie. */
171 if (r300
->z_compression
== RV350_Z_COMPRESS_88
)
172 z
->gb_z_peq_config
|= R300_GB_Z_PEQ_CONFIG_Z_PEQ_SIZE_8_8
;
175 bool can_hiz
= r300_can_hiz(r300
);
177 z
->zb_bw_cntl
|= R300_HIZ_ENABLE
;
178 z
->sc_hyperz
|= R300_SC_HYPERZ_ENABLE
;
179 z
->sc_hyperz
|= r300_get_sc_hz_max(r300
);
180 z
->zb_bw_cntl
|= r300_get_hiz_min(r300
);
184 /* R500-specific features and optimizations. */
185 if (r300
->screen
->caps
.is_r500
) {
186 z
->zb_bw_cntl
|= R500_HIZ_FP_EXP_BITS_3
;
188 R500_HIZ_EQUAL_REJECT_ENABLE
|
189 R500_PEQ_PACKING_ENABLE
|
190 R500_COVERED_PTR_MASKING_ENABLE
;
194 /*****************************************************************************/
196 /*****************************************************************************/
198 static boolean
r300_dsa_writes_stencil(
199 struct pipe_stencil_state
*s
)
201 return s
->enabled
&& s
->writemask
&&
202 (s
->fail_op
!= PIPE_STENCIL_OP_KEEP
||
203 s
->zfail_op
!= PIPE_STENCIL_OP_KEEP
||
204 s
->zpass_op
!= PIPE_STENCIL_OP_KEEP
);
207 static boolean
r300_dsa_writes_depth_stencil(
208 struct pipe_depth_stencil_alpha_state
*dsa
)
210 /* We are interested only in the cases when a depth or stencil value
213 if (dsa
->depth
.enabled
&& dsa
->depth
.writemask
&&
214 dsa
->depth
.func
!= PIPE_FUNC_NEVER
)
217 if (r300_dsa_writes_stencil(&dsa
->stencil
[0]) ||
218 r300_dsa_writes_stencil(&dsa
->stencil
[1]))
224 static boolean
r300_dsa_alpha_test_enabled(
225 struct pipe_depth_stencil_alpha_state
*dsa
)
227 /* We are interested only in the cases when alpha testing can kill
230 return dsa
->alpha
.enabled
&& dsa
->alpha
.func
!= PIPE_FUNC_ALWAYS
;
233 static void r300_update_ztop(struct r300_context
* r300
)
235 struct r300_ztop_state
* ztop_state
=
236 (struct r300_ztop_state
*)r300
->ztop_state
.state
;
237 uint32_t old_ztop
= ztop_state
->z_buffer_top
;
239 /* This is important enough that I felt it warranted a comment.
241 * According to the docs, these are the conditions where ZTOP must be
243 * 1) Alpha testing enabled
244 * 2) Texture kill instructions in fragment shader
245 * 3) Chroma key culling enabled
246 * 4) W-buffering enabled
248 * The docs claim that for the first three cases, if no ZS writes happen,
249 * then ZTOP can be used.
251 * (3) will never apply since we do not support chroma-keyed operations.
252 * (4) will need to be re-examined (and this comment updated) if/when
253 * Hyper-Z becomes supported.
255 * Additionally, the following conditions require disabled ZTOP:
256 * 5) Depth writes in fragment shader
257 * 6) Outstanding occlusion queries
259 * This register causes stalls all the way from SC to CB when changed,
260 * but it is buffered on-chip so it does not hurt to write it if it has
267 if (r300_dsa_writes_depth_stencil(r300
->dsa_state
.state
) &&
268 (r300_dsa_alpha_test_enabled(r300
->dsa_state
.state
) || /* (1) */
269 r300_fs(r300
)->shader
->info
.uses_kill
)) { /* (2) */
270 ztop_state
->z_buffer_top
= R300_ZTOP_DISABLE
;
271 } else if (r300_fragment_shader_writes_depth(r300_fs(r300
))) { /* (5) */
272 ztop_state
->z_buffer_top
= R300_ZTOP_DISABLE
;
273 } else if (r300
->query_current
) { /* (6) */
274 ztop_state
->z_buffer_top
= R300_ZTOP_DISABLE
;
276 ztop_state
->z_buffer_top
= R300_ZTOP_ENABLE
;
278 if (ztop_state
->z_buffer_top
!= old_ztop
)
279 r300
->ztop_state
.dirty
= TRUE
;
282 #define ALIGN_DIVUP(x, y) (((x) + (y) - 1) / (y))
284 static void r300_update_hiz_clear(struct r300_context
*r300
)
286 struct pipe_framebuffer_state
*fb
=
287 (struct pipe_framebuffer_state
*)r300
->fb_state
.state
;
290 height
= ALIGN_DIVUP(fb
->zsbuf
->height
, 4);
291 r300
->hiz_clear
.size
= height
* 4;
294 static void r300_update_zmask_clear(struct r300_context
*r300
)
296 struct pipe_framebuffer_state
*fb
=
297 (struct pipe_framebuffer_state
*)r300
->fb_state
.state
;
301 if (r300
->z_compression
== RV350_Z_COMPRESS_88
)
306 height
= ALIGN_DIVUP(fb
->zsbuf
->height
, mult
);
308 r300
->zmask_clear
.size
= height
* 4;
311 void r300_update_hyperz_state(struct r300_context
* r300
)
313 r300_update_ztop(r300
);
314 if (r300
->hyperz_state
.dirty
) {
315 r300_update_hyperz(r300
);
318 if (r300
->hiz_clear
.dirty
) {
319 r300_update_hiz_clear(r300
);
321 if (r300
->zmask_clear
.dirty
) {
322 r300_update_zmask_clear(r300
);
326 void r300_hiz_alloc_block(struct r300_context
*r300
, struct r300_surface
*surf
)
328 struct r300_texture
*tex
;
330 int level
= surf
->base
.level
;
332 tex
= r300_texture(surf
->base
.texture
);
334 if (tex
->hiz_mem
[level
])
337 zsize
= tex
->desc
.layer_size_in_bytes
[level
];
338 zsize
/= util_format_get_blocksize(tex
->desc
.b
.b
.format
);
339 ndw
= ALIGN_DIVUP(zsize
, 64);
341 tex
->hiz_mem
[level
] = u_mmAllocMem(r300
->hiz_mm
, ndw
, 0, 0);
345 void r300_zmask_alloc_block(struct r300_context
*r300
, struct r300_surface
*surf
, int compress
)
349 int level
= surf
->base
.level
;
350 struct r300_texture
*tex
;
352 tex
= r300_texture(surf
->base
.texture
);
354 /* We currently don't handle decompression for 3D textures and cubemaps
356 if (tex
->desc
.b
.b
.target
!= PIPE_TEXTURE_1D
&&
357 tex
->desc
.b
.b
.target
!= PIPE_TEXTURE_2D
&&
358 tex
->desc
.b
.b
.target
!= PIPE_TEXTURE_RECT
)
361 /* Cannot flush zmask of 16-bit zbuffers. */
362 if (util_format_get_blocksizebits(tex
->desc
.b
.b
.format
) == 16)
365 if (tex
->zmask_mem
[level
])
368 zsize
= tex
->desc
.layer_size_in_bytes
[level
];
369 zsize
/= util_format_get_blocksize(tex
->desc
.b
.b
.format
);
371 /* each zmask dword represents 16 4x4 blocks - which is 256 pixels
372 or 16 8x8 depending on the gb peq flag = 1024 pixels */
373 if (compress
== RV350_Z_COMPRESS_88
)
376 ndw
= ALIGN_DIVUP(zsize
, bsize
);
377 tex
->zmask_mem
[level
] = u_mmAllocMem(r300
->zmask_mm
, ndw
, 0, 0);
381 boolean
r300_hyperz_init_mm(struct r300_context
*r300
)
383 struct r300_screen
* r300screen
= r300
->screen
;
384 int frag_pipes
= r300screen
->caps
.num_frag_pipes
;
386 r300
->zmask_mm
= u_mmInit(0, r300screen
->caps
.zmask_ram
* frag_pipes
);
390 if (r300screen
->caps
.hiz_ram
) {
391 r300
->hiz_mm
= u_mmInit(0, r300screen
->caps
.hiz_ram
* frag_pipes
);
393 u_mmDestroy(r300
->zmask_mm
);
394 r300
->zmask_mm
= NULL
;
402 void r300_hyperz_destroy_mm(struct r300_context
*r300
)
404 struct r300_screen
* r300screen
= r300
->screen
;
406 if (r300screen
->caps
.hiz_ram
) {
407 u_mmDestroy(r300
->hiz_mm
);
411 u_mmDestroy(r300
->zmask_mm
);
412 r300
->zmask_mm
= NULL
;