i965: initial attempt at fixing the aperture overflow
[mesa.git] / src / mesa / drivers / dri / i965 / brw_state_upload.c
1 /*
2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
4 develop this 3D driver.
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
13
14 The above copyright notice and this permission notice (including the
15 next paragraph) shall be included in all copies or substantial
16 portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 **********************************************************************/
27 /*
28 * Authors:
29 * Keith Whitwell <keith@tungstengraphics.com>
30 */
31
32
33
34 #include "brw_context.h"
35 #include "brw_state.h"
36 #include "dri_bufmgr.h"
37 #include "intel_batchbuffer.h"
38
39 /* This is used to initialize brw->state.atoms[]. We could use this
40 * list directly except for a single atom, brw_constant_buffer, which
41 * has a .dirty value which changes according to the parameters of the
42 * current fragment and vertex programs, and so cannot be a static
43 * value.
44 */
45 const struct brw_tracked_state *atoms[] =
46 {
47 &brw_check_fallback,
48
49 &brw_tnl_vertprog,
50 &brw_active_vertprog,
51 &brw_wm_input_sizes,
52 &brw_vs_prog,
53 &brw_gs_prog,
54 &brw_clip_prog,
55 &brw_sf_prog,
56 &brw_wm_prog,
57
58 /* Once all the programs are done, we know how large urb entry
59 * sizes need to be and can decide if we need to change the urb
60 * layout.
61 */
62 &brw_curbe_offsets,
63 &brw_recalculate_urb_fence,
64
65
66 &brw_cc_vp,
67 &brw_cc_unit,
68
69 &brw_wm_surfaces, /* must do before samplers */
70 &brw_wm_samplers,
71
72 &brw_wm_unit,
73 &brw_sf_vp,
74 &brw_sf_unit,
75 &brw_vs_unit, /* always required, enabled or not */
76 &brw_clip_unit,
77 &brw_gs_unit,
78
79 /* Command packets:
80 */
81 &brw_invarient_state,
82 &brw_state_base_address,
83 &brw_pipe_control,
84
85 &brw_binding_table_pointers,
86 &brw_blend_constant_color,
87
88 &brw_depthbuffer,
89
90 &brw_polygon_stipple,
91 &brw_polygon_stipple_offset,
92
93 &brw_line_stipple,
94 &brw_aa_line_parameters,
95 /* Ordering of the commands below is documented as fixed.
96 */
97 #if 0
98 &brw_pipelined_state_pointers,
99 &brw_urb_fence,
100 &brw_constant_buffer_state,
101 #else
102 &brw_psp_urb_cbs,
103 #endif
104
105
106 NULL, /* brw_constant_buffer */
107 };
108
109
110 void brw_init_state( struct brw_context *brw )
111 {
112 GLuint i;
113
114 brw_init_cache(brw);
115
116 brw->state.atoms = _mesa_malloc(sizeof(atoms));
117 brw->state.nr_atoms = sizeof(atoms)/sizeof(*atoms);
118 _mesa_memcpy(brw->state.atoms, atoms, sizeof(atoms));
119
120 /* Patch in a pointer to the dynamic state atom:
121 */
122 for (i = 0; i < brw->state.nr_atoms; i++)
123 if (brw->state.atoms[i] == NULL)
124 brw->state.atoms[i] = &brw->curbe.tracked_state;
125
126 _mesa_memcpy(&brw->curbe.tracked_state,
127 &brw_constant_buffer,
128 sizeof(brw_constant_buffer));
129 }
130
131
132 void brw_destroy_state( struct brw_context *brw )
133 {
134 if (brw->state.atoms) {
135 _mesa_free(brw->state.atoms);
136 brw->state.atoms = NULL;
137 }
138
139 brw_destroy_cache(brw);
140 brw_destroy_batch_cache(brw);
141 }
142
143 /***********************************************************************
144 */
145
146 static GLboolean check_state( const struct brw_state_flags *a,
147 const struct brw_state_flags *b )
148 {
149 return ((a->mesa & b->mesa) ||
150 (a->brw & b->brw) ||
151 (a->cache & b->cache));
152 }
153
154 static void accumulate_state( struct brw_state_flags *a,
155 const struct brw_state_flags *b )
156 {
157 a->mesa |= b->mesa;
158 a->brw |= b->brw;
159 a->cache |= b->cache;
160 }
161
162
163 static void xor_states( struct brw_state_flags *result,
164 const struct brw_state_flags *a,
165 const struct brw_state_flags *b )
166 {
167 result->mesa = a->mesa ^ b->mesa;
168 result->brw = a->brw ^ b->brw;
169 result->cache = a->cache ^ b->cache;
170 }
171
172
173 /***********************************************************************
174 * Emit all state:
175 */
176 int brw_validate_state( struct brw_context *brw )
177 {
178 struct brw_state_flags *state = &brw->state.dirty;
179 GLuint i, ret, count;
180
181 state->mesa |= brw->intel.NewGLState;
182 brw->intel.NewGLState = 0;
183
184 if (brw->wrap)
185 state->brw |= BRW_NEW_CONTEXT;
186
187 if (brw->emit_state_always) {
188 state->mesa |= ~0;
189 state->brw |= ~0;
190 }
191
192 /* texenv program needs to notify us somehow when this happens:
193 * Some confusion about which state flag should represent this change.
194 */
195 if (brw->fragment_program != brw->attribs.FragmentProgram->_Current) {
196 brw->fragment_program = brw->attribs.FragmentProgram->_Current;
197 brw->state.dirty.mesa |= _NEW_PROGRAM;
198 brw->state.dirty.brw |= BRW_NEW_FRAGMENT_PROGRAM;
199 }
200
201
202 if (state->mesa == 0 &&
203 state->cache == 0 &&
204 state->brw == 0)
205 return 0;
206
207 if (brw->state.dirty.brw & BRW_NEW_CONTEXT)
208 brw_clear_batch_cache_flush(brw);
209
210 brw->intel.Fallback = 0;
211
212 count = 0;
213
214 /* do prepare stage for all atoms */
215 for (i = 0; i < Elements(atoms); i++) {
216 const struct brw_tracked_state *atom = brw->state.atoms[i];
217
218 if (brw->intel.Fallback)
219 break;
220
221 if (check_state(state, &atom->dirty)) {
222 if (atom->prepare) {
223 ret = atom->prepare(brw);
224 if (ret)
225 return ret;
226 }
227 }
228 }
229
230 if (brw->intel.Fallback)
231 return 0;
232
233 if (INTEL_DEBUG) {
234 /* Debug version which enforces various sanity checks on the
235 * state flags which are generated and checked to help ensure
236 * state atoms are ordered correctly in the list.
237 */
238 struct brw_state_flags examined, prev;
239 _mesa_memset(&examined, 0, sizeof(examined));
240 prev = *state;
241
242 for (i = 0; i < brw->state.nr_atoms; i++) {
243 const struct brw_tracked_state *atom = brw->state.atoms[i];
244 struct brw_state_flags generated;
245
246 assert(atom->dirty.mesa ||
247 atom->dirty.brw ||
248 atom->dirty.cache);
249
250 if (brw->intel.Fallback)
251 break;
252
253 if (check_state(state, &atom->dirty)) {
254 if (atom->emit)
255 atom->emit( brw );
256 }
257
258 accumulate_state(&examined, &atom->dirty);
259
260 /* generated = (prev ^ state)
261 * if (examined & generated)
262 * fail;
263 */
264 xor_states(&generated, &prev, state);
265 assert(!check_state(&examined, &generated));
266 prev = *state;
267 }
268 }
269 else {
270 for (i = 0; i < Elements(atoms); i++) {
271 const struct brw_tracked_state *atom = brw->state.atoms[i];
272
273 if (brw->intel.Fallback)
274 break;
275
276 if (check_state(state, &atom->dirty)) {
277 if (atom->emit)
278 atom->emit( brw );
279 }
280 }
281 }
282
283 if (!brw->intel.Fallback)
284 memset(state, 0, sizeof(*state));
285 return 0;
286 }