2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
4 develop this 3D driver.
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:
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.
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.
26 **********************************************************************/
29 * Keith Whitwell <keith@tungstengraphics.com>
34 #include "brw_context.h"
35 #include "brw_state.h"
36 #include "dri_bufmgr.h"
37 #include "intel_batchbuffer.h"
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
45 const struct brw_tracked_state
*atoms
[] =
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
63 &brw_recalculate_urb_fence
,
69 &brw_wm_surfaces
, /* must do before samplers */
75 &brw_vs_unit
, /* always required, enabled or not */
82 &brw_state_base_address
,
85 &brw_binding_table_pointers
,
86 &brw_blend_constant_color
,
91 &brw_polygon_stipple_offset
,
94 &brw_aa_line_parameters
,
95 /* Ordering of the commands below is documented as fixed.
98 &brw_pipelined_state_pointers
,
100 &brw_constant_buffer_state
,
106 NULL
, /* brw_constant_buffer */
110 void brw_init_state( struct brw_context
*brw
)
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
));
120 /* Patch in a pointer to the dynamic state atom:
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
;
126 _mesa_memcpy(&brw
->curbe
.tracked_state
,
127 &brw_constant_buffer
,
128 sizeof(brw_constant_buffer
));
132 void brw_destroy_state( struct brw_context
*brw
)
134 if (brw
->state
.atoms
) {
135 _mesa_free(brw
->state
.atoms
);
136 brw
->state
.atoms
= NULL
;
139 brw_destroy_cache(brw
);
140 brw_destroy_batch_cache(brw
);
143 /***********************************************************************
146 static GLboolean
check_state( const struct brw_state_flags
*a
,
147 const struct brw_state_flags
*b
)
149 return ((a
->mesa
& b
->mesa
) ||
151 (a
->cache
& b
->cache
));
154 static void accumulate_state( struct brw_state_flags
*a
,
155 const struct brw_state_flags
*b
)
159 a
->cache
|= b
->cache
;
163 static void xor_states( struct brw_state_flags
*result
,
164 const struct brw_state_flags
*a
,
165 const struct brw_state_flags
*b
)
167 result
->mesa
= a
->mesa
^ b
->mesa
;
168 result
->brw
= a
->brw
^ b
->brw
;
169 result
->cache
= a
->cache
^ b
->cache
;
173 /***********************************************************************
176 int brw_validate_state( struct brw_context
*brw
)
178 struct brw_state_flags
*state
= &brw
->state
.dirty
;
179 GLuint i
, ret
, count
;
181 state
->mesa
|= brw
->intel
.NewGLState
;
182 brw
->intel
.NewGLState
= 0;
185 state
->brw
|= BRW_NEW_CONTEXT
;
187 if (brw
->emit_state_always
) {
192 /* texenv program needs to notify us somehow when this happens:
193 * Some confusion about which state flag should represent this change.
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
;
202 if (state
->mesa
== 0 &&
207 if (brw
->state
.dirty
.brw
& BRW_NEW_CONTEXT
)
208 brw_clear_batch_cache_flush(brw
);
210 brw
->intel
.Fallback
= 0;
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
];
218 if (brw
->intel
.Fallback
)
221 if (check_state(state
, &atom
->dirty
)) {
223 ret
= atom
->prepare(brw
);
230 if (brw
->intel
.Fallback
)
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.
238 struct brw_state_flags examined
, prev
;
239 _mesa_memset(&examined
, 0, sizeof(examined
));
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
;
246 assert(atom
->dirty
.mesa
||
250 if (brw
->intel
.Fallback
)
253 if (check_state(state
, &atom
->dirty
)) {
258 accumulate_state(&examined
, &atom
->dirty
);
260 /* generated = (prev ^ state)
261 * if (examined & generated)
264 xor_states(&generated
, &prev
, state
);
265 assert(!check_state(&examined
, &generated
));
270 for (i
= 0; i
< Elements(atoms
); i
++) {
271 const struct brw_tracked_state
*atom
= brw
->state
.atoms
[i
];
273 if (brw
->intel
.Fallback
)
276 if (check_state(state
, &atom
->dirty
)) {
283 if (!brw
->intel
.Fallback
)
284 memset(state
, 0, sizeof(*state
));