bb3e80a080f952b5f66ff9f637eae3d1f109a5cc
[mesa.git] / src / gallium / drivers / svga / svga_state.c
1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 **********************************************************/
25
26 #include "util/u_debug.h"
27 #include "pipe/p_defines.h"
28 #include "util/u_memory.h"
29 #include "draw/draw_context.h"
30
31 #include "svga_context.h"
32 #include "svga_screen.h"
33 #include "svga_state.h"
34 #include "svga_draw.h"
35 #include "svga_cmd.h"
36 #include "svga_hw_reg.h"
37
38 /* This is just enough to decide whether we need to use the draw
39 * module (swtnl) or not.
40 */
41 static const struct svga_tracked_state *need_swtnl_state[] =
42 {
43 &svga_update_need_swvfetch,
44 &svga_update_need_pipeline,
45 &svga_update_need_swtnl,
46 NULL
47 };
48
49
50 /* Atoms to update hardware state prior to emitting a clear or draw
51 * packet.
52 */
53 static const struct svga_tracked_state *hw_clear_state[] =
54 {
55 &svga_hw_scissor,
56 &svga_hw_viewport,
57 &svga_hw_framebuffer,
58 NULL
59 };
60
61
62 /* Atoms to update hardware state prior to emitting a draw packet.
63 */
64 static const struct svga_tracked_state *hw_draw_state[] =
65 {
66 &svga_hw_update_zero_stride,
67 &svga_hw_fs,
68 &svga_hw_vs,
69 &svga_hw_rss,
70 &svga_hw_tss,
71 &svga_hw_tss_binding,
72 &svga_hw_clip_planes,
73 &svga_hw_vdecl,
74 &svga_hw_fs_parameters,
75 &svga_hw_vs_parameters,
76 NULL
77 };
78
79
80 static const struct svga_tracked_state *swtnl_draw_state[] =
81 {
82 &svga_update_swtnl_draw,
83 &svga_update_swtnl_vdecl,
84 NULL
85 };
86
87 /* Flattens the graph of state dependencies. Could swap the positions
88 * of hw_clear_state and need_swtnl_state without breaking anything.
89 */
90 static const struct svga_tracked_state **state_levels[] =
91 {
92 need_swtnl_state,
93 hw_clear_state,
94 hw_draw_state,
95 swtnl_draw_state
96 };
97
98
99
100 static unsigned check_state( unsigned a,
101 unsigned b )
102 {
103 return (a & b);
104 }
105
106 static void accumulate_state( unsigned *a,
107 unsigned b )
108 {
109 *a |= b;
110 }
111
112
113 static void xor_states( unsigned *result,
114 unsigned a,
115 unsigned b )
116 {
117 *result = a ^ b;
118 }
119
120
121
122 static enum pipe_error
123 update_state(struct svga_context *svga,
124 const struct svga_tracked_state *atoms[],
125 unsigned *state)
126 {
127 boolean debug = TRUE;
128 enum pipe_error ret = PIPE_OK;
129 unsigned i;
130
131 ret = svga_hwtnl_flush( svga->hwtnl );
132 if (ret != PIPE_OK)
133 return ret;
134
135 if (debug) {
136 /* Debug version which enforces various sanity checks on the
137 * state flags which are generated and checked to help ensure
138 * state atoms are ordered correctly in the list.
139 */
140 unsigned examined, prev;
141
142 examined = 0;
143 prev = *state;
144
145 for (i = 0; atoms[i] != NULL; i++) {
146 unsigned generated;
147
148 assert(atoms[i]->dirty);
149 assert(atoms[i]->update);
150
151 if (check_state(*state, atoms[i]->dirty)) {
152 if (0)
153 debug_printf("update: %s\n", atoms[i]->name);
154 ret = atoms[i]->update( svga, *state );
155 if (ret != PIPE_OK)
156 return ret;
157 }
158
159 /* generated = (prev ^ state)
160 * if (examined & generated)
161 * fail;
162 */
163 xor_states(&generated, prev, *state);
164 if (check_state(examined, generated)) {
165 debug_printf("state atom %s generated state already examined\n",
166 atoms[i]->name);
167 assert(0);
168 }
169
170 prev = *state;
171 accumulate_state(&examined, atoms[i]->dirty);
172 }
173 }
174 else {
175 for (i = 0; atoms[i] != NULL; i++) {
176 if (check_state(*state, atoms[i]->dirty)) {
177 ret = atoms[i]->update( svga, *state );
178 if (ret != PIPE_OK)
179 return ret;
180 }
181 }
182 }
183
184 return PIPE_OK;
185 }
186
187
188
189 enum pipe_error
190 svga_update_state(struct svga_context *svga, unsigned max_level)
191 {
192 struct svga_screen *screen = svga_screen(svga->pipe.screen);
193 enum pipe_error ret = PIPE_OK;
194 int i;
195
196 /* Check for updates to bound textures. This can't be done in an
197 * atom as there is no flag which could provoke this test, and we
198 * cannot create one.
199 */
200 if (svga->state.texture_timestamp != screen->texture_timestamp) {
201 svga->state.texture_timestamp = screen->texture_timestamp;
202 svga->dirty |= SVGA_NEW_TEXTURE;
203 }
204
205 for (i = 0; i <= max_level; i++) {
206 svga->dirty |= svga->state.dirty[i];
207
208 if (svga->dirty) {
209 ret = update_state( svga,
210 state_levels[i],
211 &svga->dirty );
212 if (ret != PIPE_OK)
213 return ret;
214
215 svga->state.dirty[i] = 0;
216 }
217 }
218
219 for (; i < SVGA_STATE_MAX; i++)
220 svga->state.dirty[i] |= svga->dirty;
221
222 svga->dirty = 0;
223 return PIPE_OK;
224 }
225
226
227
228
229 void svga_update_state_retry( struct svga_context *svga,
230 unsigned max_level )
231 {
232 enum pipe_error ret;
233
234 ret = svga_update_state( svga, max_level );
235
236 if (ret == PIPE_ERROR_OUT_OF_MEMORY) {
237 svga_context_flush(svga, NULL);
238 ret = svga_update_state( svga, max_level );
239 }
240
241 assert( ret == PIPE_OK );
242 }
243
244
245
246 #define EMIT_RS(_rs, _count, _name, _value) \
247 do { \
248 _rs[_count].state = _name; \
249 _rs[_count].uintValue = _value; \
250 _count++; \
251 } while (0)
252
253
254 /* Setup any hardware state which will be constant through the life of
255 * a context.
256 */
257 enum pipe_error svga_emit_initial_state( struct svga_context *svga )
258 {
259 SVGA3dRenderState *rs;
260 unsigned count = 0;
261 const unsigned COUNT = 2;
262 enum pipe_error ret;
263
264 ret = SVGA3D_BeginSetRenderState( svga->swc, &rs, COUNT );
265 if (ret != PIPE_OK)
266 return ret;
267
268 /* Always use D3D style coordinate space as this is the only one
269 * which is implemented on all backends.
270 */
271 EMIT_RS(rs, count, SVGA3D_RS_COORDINATETYPE, SVGA3D_COORDINATE_LEFTHANDED );
272 EMIT_RS(rs, count, SVGA3D_RS_FRONTWINDING, SVGA3D_FRONTWINDING_CW );
273
274 assert( COUNT == count );
275 SVGA_FIFOCommitAll( svga->swc );
276
277 return PIPE_OK;
278 }