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
& R300_ZS_MASK
;
48 int ret
= R300_SC_HYPERZ_MIN
;
50 if (func
>= R300_ZS_GEQUAL
&& func
<= R300_ZS_ALWAYS
)
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
== R300_ZS_LESS
|| func1
== R300_ZS_LEQUAL
) &&
59 (func2
== R300_ZS_EQUAL
|| func2
== R300_ZS_GEQUAL
||
60 func2
== R300_ZS_GREATER
))
63 /* func1 is greater/greaterthan */
64 if ((func1
== R300_ZS_GEQUAL
|| func1
== R300_ZS_GREATER
) &&
65 (func2
== R300_ZS_LESS
|| func2
== R300_ZS_LEQUAL
))
71 static int r300_get_hiz_min(struct r300_context
*r300
)
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
;
77 if (func
== R300_ZS_LESS
|| func
== R300_ZS_LEQUAL
)
82 static boolean
r300_dsa_stencil_op_not_keep(struct pipe_stencil_state
*s
)
84 if (s
->enabled
&& (s
->fail_op
!= PIPE_STENCIL_OP_KEEP
||
85 s
->zfail_op
!= PIPE_STENCIL_OP_KEEP
))
90 static boolean
r300_can_hiz(struct r300_context
*r300
)
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
;
97 /* shader writes depth - no HiZ */
98 if (r300_fragment_shader_writes_depth(r300_fs(r300
))) /* (5) */
101 if (r300
->query_current
)
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]))
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
)
112 /* if depth func is NOTEQUAL */
113 if (dsa
->depth
.func
== PIPE_FUNC_NOTEQUAL
)
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
;
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
);
134 static void r300_update_hyperz(struct r300_context
* r300
)
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
;
145 z
->gb_z_peq_config
= 0;
147 z
->sc_hyperz
= R300_SC_HYPERZ_ADJ_2
;
150 if (r300
->cbzb_clear
) {
151 z
->zb_bw_cntl
|= R300_ZB_CB_CLEAR_CACHE_LINE_WRITE_ONLY
;
158 if (!r300
->rws
->get_value(r300
->rws
, R300_CAN_HYPERZ
))
161 zmask_in_use
= zstex
->zmask_in_use
[fb
->zsbuf
->level
];
162 hiz_in_use
= zstex
->hiz_in_use
[fb
->zsbuf
->level
];
166 z
->zb_bw_cntl
|= R300_FAST_FILL_ENABLE
; /* | R300_FORCE_COMPRESSED_STENCIL_VALUE_ENABLE;*/
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
;
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
;
181 bool can_hiz
= r300_can_hiz(r300
);
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
);
190 /* R500-specific features and optimizations. */
191 if (r300
->screen
->caps
.is_r500
) {
192 z
->zb_bw_cntl
|= R500_HIZ_FP_EXP_BITS_3
;
194 R500_HIZ_EQUAL_REJECT_ENABLE
|
195 R500_PEQ_PACKING_ENABLE
|
196 R500_COVERED_PTR_MASKING_ENABLE
;
200 /*****************************************************************************/
202 /*****************************************************************************/
204 static boolean
r300_dsa_writes_stencil(
205 struct pipe_stencil_state
*s
)
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
);
213 static boolean
r300_dsa_writes_depth_stencil(
214 struct pipe_depth_stencil_alpha_state
*dsa
)
216 /* We are interested only in the cases when a depth or stencil value
219 if (dsa
->depth
.enabled
&& dsa
->depth
.writemask
&&
220 dsa
->depth
.func
!= PIPE_FUNC_NEVER
)
223 if (r300_dsa_writes_stencil(&dsa
->stencil
[0]) ||
224 r300_dsa_writes_stencil(&dsa
->stencil
[1]))
230 static boolean
r300_dsa_alpha_test_enabled(
231 struct pipe_depth_stencil_alpha_state
*dsa
)
233 /* We are interested only in the cases when alpha testing can kill
236 return dsa
->alpha
.enabled
&& dsa
->alpha
.func
!= PIPE_FUNC_ALWAYS
;
239 static void r300_update_ztop(struct r300_context
* r300
)
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
;
245 /* This is important enough that I felt it warranted a comment.
247 * According to the docs, these are the conditions where ZTOP must be
249 * 1) Alpha testing enabled
250 * 2) Texture kill instructions in fragment shader
251 * 3) Chroma key culling enabled
252 * 4) W-buffering enabled
254 * The docs claim that for the first three cases, if no ZS writes happen,
255 * then ZTOP can be used.
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.
261 * Additionally, the following conditions require disabled ZTOP:
262 * 5) Depth writes in fragment shader
263 * 6) Outstanding occlusion queries
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
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
;
282 ztop_state
->z_buffer_top
= R300_ZTOP_ENABLE
;
284 if (ztop_state
->z_buffer_top
!= old_ztop
)
285 r300
->ztop_state
.dirty
= TRUE
;
288 #define ALIGN_DIVUP(x, y) (((x) + (y) - 1) / (y))
290 static void r300_update_hiz_clear(struct r300_context
*r300
)
292 struct pipe_framebuffer_state
*fb
=
293 (struct pipe_framebuffer_state
*)r300
->fb_state
.state
;
296 height
= ALIGN_DIVUP(fb
->zsbuf
->height
, 4);
297 r300
->hiz_clear
.size
= height
* 4;
300 static void r300_update_zmask_clear(struct r300_context
*r300
)
302 struct pipe_framebuffer_state
*fb
=
303 (struct pipe_framebuffer_state
*)r300
->fb_state
.state
;
307 if (r300
->z_compression
== RV350_Z_COMPRESS_88
)
312 height
= ALIGN_DIVUP(fb
->zsbuf
->height
, mult
);
314 r300
->zmask_clear
.size
= height
* 4;
317 void r300_update_hyperz_state(struct r300_context
* r300
)
319 r300_update_ztop(r300
);
320 if (r300
->hyperz_state
.dirty
) {
321 r300_update_hyperz(r300
);
324 if (r300
->hiz_clear
.dirty
) {
325 r300_update_hiz_clear(r300
);
327 if (r300
->zmask_clear
.dirty
) {
328 r300_update_zmask_clear(r300
);
332 void r300_hiz_alloc_block(struct r300_context
*r300
, struct r300_surface
*surf
)
334 struct r300_texture
*tex
;
336 int level
= surf
->base
.level
;
338 tex
= r300_texture(surf
->base
.texture
);
340 if (tex
->hiz_mem
[level
])
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);
347 tex
->hiz_mem
[level
] = u_mmAllocMem(r300
->hiz_mm
, ndw
, 0, 0);
351 void r300_zmask_alloc_block(struct r300_context
*r300
, struct r300_surface
*surf
, int compress
)
355 int level
= surf
->base
.level
;
356 struct r300_texture
*tex
;
358 tex
= r300_texture(surf
->base
.texture
);
360 /* We currently don't handle decompression for 3D textures and cubemaps
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
)
367 /* Cannot flush zmask of 16-bit zbuffers. */
368 if (util_format_get_blocksizebits(tex
->desc
.b
.b
.format
) == 16)
371 if (tex
->zmask_mem
[level
])
374 zsize
= tex
->desc
.layer_size_in_bytes
[level
];
375 zsize
/= util_format_get_blocksize(tex
->desc
.b
.b
.format
);
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
)
382 ndw
= ALIGN_DIVUP(zsize
, bsize
);
383 tex
->zmask_mem
[level
] = u_mmAllocMem(r300
->zmask_mm
, ndw
, 0, 0);
387 boolean
r300_hyperz_init_mm(struct r300_context
*r300
)
389 struct r300_screen
* r300screen
= r300
->screen
;
390 int frag_pipes
= r300screen
->caps
.num_frag_pipes
;
392 r300
->zmask_mm
= u_mmInit(0, r300screen
->caps
.zmask_ram
* frag_pipes
);
396 if (r300screen
->caps
.hiz_ram
) {
397 r300
->hiz_mm
= u_mmInit(0, r300screen
->caps
.hiz_ram
* frag_pipes
);
399 u_mmDestroy(r300
->zmask_mm
);
400 r300
->zmask_mm
= NULL
;
408 void r300_hyperz_destroy_mm(struct r300_context
*r300
)
410 struct r300_screen
* r300screen
= r300
->screen
;
412 if (r300screen
->caps
.hiz_ram
) {
413 u_mmDestroy(r300
->hiz_mm
);
417 u_mmDestroy(r300
->zmask_mm
);
418 r300
->zmask_mm
= NULL
;