1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
|
/*
* Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
* Copyright 2009 Marek Olšák <maraeo@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* on the rights to use, copy, modify, merge, publish, distribute, sub
* license, and/or sell copies of the Software, and to permit persons to whom
* the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
* THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
* USE OR OTHER DEALINGS IN THE SOFTWARE. */
#include "r300_context.h"
#include "r300_hyperz.h"
#include "r300_reg.h"
#include "r300_fs.h"
#include "r300_winsys.h"
#include "util/u_format.h"
#include "util/u_mm.h"
/*
HiZ rules - taken from various docs
1. HiZ only works on depth values
2. Cannot HiZ if stencil fail or zfail is !KEEP
3. on R300/400, HiZ is disabled if depth test is EQUAL
4. comparison changes without clears usually mean disabling HiZ
*/
/*****************************************************************************/
/* The HyperZ setup */
/*****************************************************************************/
static bool r300_get_sc_hz_max(struct r300_context *r300)
{
struct r300_dsa_state *dsa_state = r300->dsa_state.state;
int func = dsa_state->z_stencil_control & R300_ZS_MASK;
int ret = R300_SC_HYPERZ_MIN;
if (func >= R300_ZS_GEQUAL && func <= R300_ZS_ALWAYS)
ret = R300_SC_HYPERZ_MAX;
return ret;
}
static bool r300_zfunc_same_direction(int func1, int func2)
{
/* func1 is less/lessthan */
if ((func1 == R300_ZS_LESS || func1 == R300_ZS_LEQUAL) &&
(func2 == R300_ZS_EQUAL || func2 == R300_ZS_GEQUAL ||
func2 == R300_ZS_GREATER))
return FALSE;
/* func1 is greater/greaterthan */
if ((func1 == R300_ZS_GEQUAL || func1 == R300_ZS_GREATER) &&
(func2 == R300_ZS_LESS || func2 == R300_ZS_LEQUAL))
return FALSE;
return TRUE;
}
static int r300_get_hiz_min(struct r300_context *r300)
{
struct r300_dsa_state *dsa_state = r300->dsa_state.state;
int func = dsa_state->z_stencil_control & R300_ZS_MASK;
int ret = R300_HIZ_MIN;
if (func == R300_ZS_LESS || func == R300_ZS_LEQUAL)
ret = R300_HIZ_MAX;
return ret;
}
static boolean r300_dsa_stencil_op_not_keep(struct pipe_stencil_state *s)
{
if (s->enabled && (s->fail_op != PIPE_STENCIL_OP_KEEP ||
s->zfail_op != PIPE_STENCIL_OP_KEEP))
return TRUE;
return FALSE;
}
static boolean r300_can_hiz(struct r300_context *r300)
{
struct r300_dsa_state *dsa_state = r300->dsa_state.state;
struct pipe_depth_stencil_alpha_state *dsa = &dsa_state->dsa;
struct r300_screen* r300screen = r300->screen;
struct r300_hyperz_state *z = r300->hyperz_state.state;
/* shader writes depth - no HiZ */
if (r300_fragment_shader_writes_depth(r300_fs(r300))) /* (5) */
return FALSE;
if (r300->query_current)
return FALSE;
/* if stencil fail/zfail op is not KEEP */
if (r300_dsa_stencil_op_not_keep(&dsa->stencil[0]) ||
r300_dsa_stencil_op_not_keep(&dsa->stencil[1]))
return FALSE;
if (dsa->depth.enabled) {
/* if depth func is EQUAL pre-r500 */
if (dsa->depth.func == PIPE_FUNC_EQUAL && !r300screen->caps.is_r500)
return FALSE;
/* if depth func is NOTEQUAL */
if (dsa->depth.func == PIPE_FUNC_NOTEQUAL)
return FALSE;
}
/* depth comparison function - if just cleared save and return okay */
if (z->current_func == -1) {
int func = dsa_state->z_stencil_control & R300_ZS_MASK;
if (func != 0 && func != 7)
z->current_func = dsa_state->z_stencil_control & R300_ZS_MASK;
} else {
/* simple don't change */
if (!r300_zfunc_same_direction(z->current_func,
(dsa_state->z_stencil_control & R300_ZS_MASK))) {
DBG(r300, DBG_HYPERZ,
"z func changed direction - disabling hyper-z %d -> %d\n",
z->current_func, dsa_state->z_stencil_control);
return FALSE;
}
}
return TRUE;
}
static void r300_update_hyperz(struct r300_context* r300)
{
struct r300_hyperz_state *z =
(struct r300_hyperz_state*)r300->hyperz_state.state;
struct pipe_framebuffer_state *fb =
(struct pipe_framebuffer_state*)r300->fb_state.state;
struct r300_texture *zstex =
fb->zsbuf ? r300_texture(fb->zsbuf->texture) : NULL;
boolean zmask_in_use = FALSE;
boolean hiz_in_use = FALSE;
z->gb_z_peq_config = 0;
z->zb_bw_cntl = 0;
z->sc_hyperz = R300_SC_HYPERZ_ADJ_2;
z->flush = 0;
if (r300->cbzb_clear) {
z->zb_bw_cntl |= R300_ZB_CB_CLEAR_CACHE_LINE_WRITE_ONLY;
return;
}
if (!zstex)
return;
if (!r300->rws->get_value(r300->rws, R300_CAN_HYPERZ))
return;
zmask_in_use = zstex->zmask_in_use[fb->zsbuf->u.tex.level];
hiz_in_use = zstex->hiz_in_use[fb->zsbuf->u.tex.level];
/* Z fastfill. */
if (zmask_in_use) {
z->zb_bw_cntl |= R300_FAST_FILL_ENABLE; /* | R300_FORCE_COMPRESSED_STENCIL_VALUE_ENABLE;*/
}
/* Zbuffer compression. */
if (zmask_in_use && r300->z_compression) {
z->zb_bw_cntl |= R300_RD_COMP_ENABLE;
if (r300->z_decomp_rd == false)
z->zb_bw_cntl |= R300_WR_COMP_ENABLE;
}
/* RV350 and up optimizations. */
/* The section 10.4.9 in the docs is a lie. */
if (r300->z_compression == RV350_Z_COMPRESS_88)
z->gb_z_peq_config |= R300_GB_Z_PEQ_CONFIG_Z_PEQ_SIZE_8_8;
if (hiz_in_use) {
bool can_hiz = r300_can_hiz(r300);
if (can_hiz) {
z->zb_bw_cntl |= R300_HIZ_ENABLE;
z->sc_hyperz |= R300_SC_HYPERZ_ENABLE;
z->sc_hyperz |= r300_get_sc_hz_max(r300);
z->zb_bw_cntl |= r300_get_hiz_min(r300);
}
}
/* R500-specific features and optimizations. */
if (r300->screen->caps.is_r500) {
z->zb_bw_cntl |= R500_HIZ_FP_EXP_BITS_3;
z->zb_bw_cntl |=
R500_HIZ_EQUAL_REJECT_ENABLE |
R500_PEQ_PACKING_ENABLE |
R500_COVERED_PTR_MASKING_ENABLE;
}
}
/*****************************************************************************/
/* The ZTOP state */
/*****************************************************************************/
static boolean r300_dsa_writes_stencil(
struct pipe_stencil_state *s)
{
return s->enabled && s->writemask &&
(s->fail_op != PIPE_STENCIL_OP_KEEP ||
s->zfail_op != PIPE_STENCIL_OP_KEEP ||
s->zpass_op != PIPE_STENCIL_OP_KEEP);
}
static boolean r300_dsa_writes_depth_stencil(
struct pipe_depth_stencil_alpha_state *dsa)
{
/* We are interested only in the cases when a depth or stencil value
* can be changed. */
if (dsa->depth.enabled && dsa->depth.writemask &&
dsa->depth.func != PIPE_FUNC_NEVER)
return TRUE;
if (r300_dsa_writes_stencil(&dsa->stencil[0]) ||
r300_dsa_writes_stencil(&dsa->stencil[1]))
return TRUE;
return FALSE;
}
static boolean r300_dsa_alpha_test_enabled(
struct pipe_depth_stencil_alpha_state *dsa)
{
/* We are interested only in the cases when alpha testing can kill
* a fragment. */
return dsa->alpha.enabled && dsa->alpha.func != PIPE_FUNC_ALWAYS;
}
static void r300_update_ztop(struct r300_context* r300)
{
struct r300_ztop_state* ztop_state =
(struct r300_ztop_state*)r300->ztop_state.state;
uint32_t old_ztop = ztop_state->z_buffer_top;
/* This is important enough that I felt it warranted a comment.
*
* According to the docs, these are the conditions where ZTOP must be
* disabled:
* 1) Alpha testing enabled
* 2) Texture kill instructions in fragment shader
* 3) Chroma key culling enabled
* 4) W-buffering enabled
*
* The docs claim that for the first three cases, if no ZS writes happen,
* then ZTOP can be used.
*
* (3) will never apply since we do not support chroma-keyed operations.
* (4) will need to be re-examined (and this comment updated) if/when
* Hyper-Z becomes supported.
*
* Additionally, the following conditions require disabled ZTOP:
* 5) Depth writes in fragment shader
* 6) Outstanding occlusion queries
*
* This register causes stalls all the way from SC to CB when changed,
* but it is buffered on-chip so it does not hurt to write it if it has
* not changed.
*
* ~C.
*/
/* ZS writes */
if (r300_dsa_writes_depth_stencil(r300->dsa_state.state) &&
(r300_dsa_alpha_test_enabled(r300->dsa_state.state) || /* (1) */
r300_fs(r300)->shader->info.uses_kill)) { /* (2) */
ztop_state->z_buffer_top = R300_ZTOP_DISABLE;
} else if (r300_fragment_shader_writes_depth(r300_fs(r300))) { /* (5) */
ztop_state->z_buffer_top = R300_ZTOP_DISABLE;
} else if (r300->query_current) { /* (6) */
ztop_state->z_buffer_top = R300_ZTOP_DISABLE;
} else {
ztop_state->z_buffer_top = R300_ZTOP_ENABLE;
}
if (ztop_state->z_buffer_top != old_ztop)
r300->ztop_state.dirty = TRUE;
}
#define ALIGN_DIVUP(x, y) (((x) + (y) - 1) / (y))
static void r300_update_hiz_clear(struct r300_context *r300)
{
struct pipe_framebuffer_state *fb =
(struct pipe_framebuffer_state*)r300->fb_state.state;
uint32_t height;
height = ALIGN_DIVUP(fb->zsbuf->height, 4);
r300->hiz_clear.size = height * 4;
}
static void r300_update_zmask_clear(struct r300_context *r300)
{
struct pipe_framebuffer_state *fb =
(struct pipe_framebuffer_state*)r300->fb_state.state;
uint32_t height;
int mult;
if (r300->z_compression == RV350_Z_COMPRESS_88)
mult = 8;
else
mult = 4;
height = ALIGN_DIVUP(fb->zsbuf->height, mult);
r300->zmask_clear.size = height * 4;
}
void r300_update_hyperz_state(struct r300_context* r300)
{
r300_update_ztop(r300);
if (r300->hyperz_state.dirty) {
r300_update_hyperz(r300);
}
if (r300->hiz_clear.dirty) {
r300_update_hiz_clear(r300);
}
if (r300->zmask_clear.dirty) {
r300_update_zmask_clear(r300);
}
}
void r300_hiz_alloc_block(struct r300_context *r300, struct r300_surface *surf)
{
struct r300_texture *tex;
uint32_t zsize, ndw;
int level = surf->base.u.tex.level;
tex = r300_texture(surf->base.texture);
if (tex->hiz_mem[level])
return;
zsize = tex->desc.layer_size_in_bytes[level];
zsize /= util_format_get_blocksize(tex->desc.b.b.format);
ndw = ALIGN_DIVUP(zsize, 64);
tex->hiz_mem[level] = u_mmAllocMem(r300->hiz_mm, ndw, 0, 0);
return;
}
void r300_zmask_alloc_block(struct r300_context *r300, struct r300_surface *surf, int compress)
{
int bsize = 256;
uint32_t zsize, ndw;
int level = surf->base.u.tex.level;
struct r300_texture *tex;
tex = r300_texture(surf->base.texture);
/* We currently don't handle decompression for 3D textures and cubemaps
* correctly. */
if (tex->desc.b.b.target != PIPE_TEXTURE_1D &&
tex->desc.b.b.target != PIPE_TEXTURE_2D &&
tex->desc.b.b.target != PIPE_TEXTURE_RECT)
return;
/* Cannot flush zmask of 16-bit zbuffers. */
if (util_format_get_blocksizebits(tex->desc.b.b.format) == 16)
return;
if (tex->zmask_mem[level])
return;
zsize = tex->desc.layer_size_in_bytes[level];
zsize /= util_format_get_blocksize(tex->desc.b.b.format);
/* each zmask dword represents 16 4x4 blocks - which is 256 pixels
or 16 8x8 depending on the gb peq flag = 1024 pixels */
if (compress == RV350_Z_COMPRESS_88)
bsize = 1024;
ndw = ALIGN_DIVUP(zsize, bsize);
tex->zmask_mem[level] = u_mmAllocMem(r300->zmask_mm, ndw, 0, 0);
return;
}
boolean r300_hyperz_init_mm(struct r300_context *r300)
{
struct r300_screen* r300screen = r300->screen;
int frag_pipes = r300screen->caps.num_frag_pipes;
r300->zmask_mm = u_mmInit(0, r300screen->caps.zmask_ram * frag_pipes);
if (!r300->zmask_mm)
return FALSE;
if (r300screen->caps.hiz_ram) {
r300->hiz_mm = u_mmInit(0, r300screen->caps.hiz_ram * frag_pipes);
if (!r300->hiz_mm) {
u_mmDestroy(r300->zmask_mm);
r300->zmask_mm = NULL;
return FALSE;
}
}
return TRUE;
}
void r300_hyperz_destroy_mm(struct r300_context *r300)
{
struct r300_screen* r300screen = r300->screen;
if (r300screen->caps.hiz_ram) {
u_mmDestroy(r300->hiz_mm);
r300->hiz_mm = NULL;
}
u_mmDestroy(r300->zmask_mm);
r300->zmask_mm = NULL;
}
|