1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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
24 **********************************************************/
26 #include "util/u_debug.h"
27 #include "pipe/p_defines.h"
28 #include "util/u_memory.h"
29 #include "draw/draw_context.h"
31 #include "svga_context.h"
32 #include "svga_screen.h"
33 #include "svga_state.h"
34 #include "svga_draw.h"
36 #include "svga_hw_reg.h"
38 /* This is just enough to decide whether we need to use the draw
39 * module (swtnl) or not.
41 static const struct svga_tracked_state *need_swtnl_state[] =
43 &svga_update_need_swvfetch,
44 &svga_update_need_pipeline,
45 &svga_update_need_swtnl,
50 /* Atoms to update hardware state prior to emitting a clear or draw
53 static const struct svga_tracked_state *hw_clear_state[] =
62 /* Atoms to update hardware state prior to emitting a draw packet.
64 static const struct svga_tracked_state *hw_draw_state[] =
66 &svga_hw_update_zero_stride,
74 &svga_hw_fs_parameters,
75 &svga_hw_vs_parameters,
80 static const struct svga_tracked_state *swtnl_draw_state[] =
82 &svga_update_swtnl_draw,
83 &svga_update_swtnl_vdecl,
87 /* Flattens the graph of state dependencies. Could swap the positions
88 * of hw_clear_state and need_swtnl_state without breaking anything.
90 static const struct svga_tracked_state **state_levels[] =
100 static unsigned check_state( unsigned a,
106 static void accumulate_state( unsigned *a,
113 static void xor_states( unsigned *result,
122 static int update_state( struct svga_context *svga,
123 const struct svga_tracked_state *atoms[],
126 boolean debug = TRUE;
127 enum pipe_error ret = 0;
130 ret = svga_hwtnl_flush( svga->hwtnl );
135 /* Debug version which enforces various sanity checks on the
136 * state flags which are generated and checked to help ensure
137 * state atoms are ordered correctly in the list.
139 unsigned examined, prev;
144 for (i = 0; atoms[i] != NULL; i++) {
147 assert(atoms[i]->dirty);
148 assert(atoms[i]->update);
150 if (check_state(*state, atoms[i]->dirty)) {
152 debug_printf("update: %s\n", atoms[i]->name);
153 ret = atoms[i]->update( svga, *state );
158 /* generated = (prev ^ state)
159 * if (examined & generated)
162 xor_states(&generated, prev, *state);
163 if (check_state(examined, generated)) {
164 debug_printf("state atom %s generated state already examined\n",
170 accumulate_state(&examined, atoms[i]->dirty);
174 for (i = 0; atoms[i] != NULL; i++) {
175 if (check_state(*state, atoms[i]->dirty)) {
176 ret = atoms[i]->update( svga, *state );
188 int svga_update_state( struct svga_context *svga,
191 struct svga_screen *screen = svga_screen(svga->pipe.screen);
195 /* Check for updates to bound textures. This can't be done in an
196 * atom as there is no flag which could provoke this test, and we
199 if (svga->state.texture_timestamp != screen->texture_timestamp) {
200 svga->state.texture_timestamp = screen->texture_timestamp;
201 svga->dirty |= SVGA_NEW_TEXTURE;
204 for (i = 0; i <= max_level; i++) {
205 svga->dirty |= svga->state.dirty[i];
208 ret = update_state( svga,
214 svga->state.dirty[i] = 0;
218 for (; i < SVGA_STATE_MAX; i++)
219 svga->state.dirty[i] |= svga->dirty;
228 void svga_update_state_retry( struct svga_context *svga,
233 ret = svga_update_state( svga, max_level );
235 if (ret == PIPE_ERROR_OUT_OF_MEMORY) {
236 svga_context_flush(svga, NULL);
237 ret = svga_update_state( svga, max_level );
245 #define EMIT_RS(_rs, _count, _name, _value) \
247 _rs[_count].state = _name; \
248 _rs[_count].uintValue = _value; \
253 /* Setup any hardware state which will be constant through the life of
256 enum pipe_error svga_emit_initial_state( struct svga_context *svga )
258 SVGA3dRenderState *rs;
260 const unsigned COUNT = 2;
263 ret = SVGA3D_BeginSetRenderState( svga->swc, &rs, COUNT );
267 /* Always use D3D style coordinate space as this is the only one
268 * which is implemented on all backends.
270 EMIT_RS(rs, count, SVGA3D_RS_COORDINATETYPE, SVGA3D_COORDINATE_LEFTHANDED );
271 EMIT_RS(rs, count, SVGA3D_RS_FRONTWINDING, SVGA3D_FRONTWINDING_CW );
273 assert( COUNT == count );
274 SVGA_FIFOCommitAll( svga->swc );