r300g: fix possible crash in destroy_context
[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
29 /*****************************************************************************/
30 /* The HyperZ setup */
31 /*****************************************************************************/
32
33 static void r300_update_hyperz(struct r300_context* r300)
34 {
35 struct r300_hyperz_state *z =
36 (struct r300_hyperz_state*)r300->hyperz_state.state;
37
38 z->zb_bw_cntl = 0;
39 z->sc_hyperz = R300_SC_HYPERZ_ADJ_2;
40
41 if (r300->cbzb_clear)
42 z->zb_bw_cntl |= R300_ZB_CB_CLEAR_CACHE_LINE_WRITE_ONLY;
43 }
44
45 /*****************************************************************************/
46 /* The ZTOP state */
47 /*****************************************************************************/
48
49 static boolean r300_dsa_writes_stencil(
50 struct pipe_stencil_state *s)
51 {
52 return s->enabled && s->writemask &&
53 (s->fail_op != PIPE_STENCIL_OP_KEEP ||
54 s->zfail_op != PIPE_STENCIL_OP_KEEP ||
55 s->zpass_op != PIPE_STENCIL_OP_KEEP);
56 }
57
58 static boolean r300_dsa_writes_depth_stencil(
59 struct pipe_depth_stencil_alpha_state *dsa)
60 {
61 /* We are interested only in the cases when a depth or stencil value
62 * can be changed. */
63
64 if (dsa->depth.enabled && dsa->depth.writemask &&
65 dsa->depth.func != PIPE_FUNC_NEVER)
66 return TRUE;
67
68 if (r300_dsa_writes_stencil(&dsa->stencil[0]) ||
69 r300_dsa_writes_stencil(&dsa->stencil[1]))
70 return TRUE;
71
72 return FALSE;
73 }
74
75 static boolean r300_dsa_alpha_test_enabled(
76 struct pipe_depth_stencil_alpha_state *dsa)
77 {
78 /* We are interested only in the cases when alpha testing can kill
79 * a fragment. */
80
81 return dsa->alpha.enabled && dsa->alpha.func != PIPE_FUNC_ALWAYS;
82 }
83
84 static void r300_update_ztop(struct r300_context* r300)
85 {
86 struct r300_ztop_state* ztop_state =
87 (struct r300_ztop_state*)r300->ztop_state.state;
88 uint32_t old_ztop = ztop_state->z_buffer_top;
89
90 /* This is important enough that I felt it warranted a comment.
91 *
92 * According to the docs, these are the conditions where ZTOP must be
93 * disabled:
94 * 1) Alpha testing enabled
95 * 2) Texture kill instructions in fragment shader
96 * 3) Chroma key culling enabled
97 * 4) W-buffering enabled
98 *
99 * The docs claim that for the first three cases, if no ZS writes happen,
100 * then ZTOP can be used.
101 *
102 * (3) will never apply since we do not support chroma-keyed operations.
103 * (4) will need to be re-examined (and this comment updated) if/when
104 * Hyper-Z becomes supported.
105 *
106 * Additionally, the following conditions require disabled ZTOP:
107 * 5) Depth writes in fragment shader
108 * 6) Outstanding occlusion queries
109 *
110 * This register causes stalls all the way from SC to CB when changed,
111 * but it is buffered on-chip so it does not hurt to write it if it has
112 * not changed.
113 *
114 * ~C.
115 */
116
117 /* ZS writes */
118 if (r300_dsa_writes_depth_stencil(r300->dsa_state.state) &&
119 (r300_dsa_alpha_test_enabled(r300->dsa_state.state) || /* (1) */
120 r300_fs(r300)->shader->info.uses_kill)) { /* (2) */
121 ztop_state->z_buffer_top = R300_ZTOP_DISABLE;
122 } else if (r300_fragment_shader_writes_depth(r300_fs(r300))) { /* (5) */
123 ztop_state->z_buffer_top = R300_ZTOP_DISABLE;
124 } else if (r300->query_current) { /* (6) */
125 ztop_state->z_buffer_top = R300_ZTOP_DISABLE;
126 } else {
127 ztop_state->z_buffer_top = R300_ZTOP_ENABLE;
128 }
129
130 if (ztop_state->z_buffer_top != old_ztop)
131 r300->ztop_state.dirty = TRUE;
132 }
133
134 void r300_update_hyperz_state(struct r300_context* r300)
135 {
136 r300_update_ztop(r300);
137 if (r300->hyperz_state.dirty) {
138 r300_update_hyperz(r300);
139 }
140 }