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 enum pipe_error
123 update_state(struct svga_context
*svga
,
124 const struct svga_tracked_state
*atoms
[],
127 boolean debug
= TRUE
;
128 enum pipe_error ret
= PIPE_OK
;
131 ret
= svga_hwtnl_flush( svga
->hwtnl
);
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.
140 unsigned examined
, prev
;
145 for (i
= 0; atoms
[i
] != NULL
; i
++) {
148 assert(atoms
[i
]->dirty
);
149 assert(atoms
[i
]->update
);
151 if (check_state(*state
, atoms
[i
]->dirty
)) {
153 debug_printf("update: %s\n", atoms
[i
]->name
);
154 ret
= atoms
[i
]->update( svga
, *state
);
159 /* generated = (prev ^ state)
160 * if (examined & generated)
163 xor_states(&generated
, prev
, *state
);
164 if (check_state(examined
, generated
)) {
165 debug_printf("state atom %s generated state already examined\n",
171 accumulate_state(&examined
, atoms
[i
]->dirty
);
175 for (i
= 0; atoms
[i
] != NULL
; i
++) {
176 if (check_state(*state
, atoms
[i
]->dirty
)) {
177 ret
= atoms
[i
]->update( svga
, *state
);
190 svga_update_state(struct svga_context
*svga
, unsigned max_level
)
192 struct svga_screen
*screen
= svga_screen(svga
->pipe
.screen
);
193 enum pipe_error ret
= PIPE_OK
;
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
200 if (svga
->state
.texture_timestamp
!= screen
->texture_timestamp
) {
201 svga
->state
.texture_timestamp
= screen
->texture_timestamp
;
202 svga
->dirty
|= SVGA_NEW_TEXTURE
;
205 for (i
= 0; i
<= max_level
; i
++) {
206 svga
->dirty
|= svga
->state
.dirty
[i
];
209 ret
= update_state( svga
,
215 svga
->state
.dirty
[i
] = 0;
219 for (; i
< SVGA_STATE_MAX
; i
++)
220 svga
->state
.dirty
[i
] |= svga
->dirty
;
229 void svga_update_state_retry( struct svga_context
*svga
,
234 ret
= svga_update_state( svga
, max_level
);
236 if (ret
== PIPE_ERROR_OUT_OF_MEMORY
) {
237 svga_context_flush(svga
, NULL
);
238 ret
= svga_update_state( svga
, max_level
);
241 assert( ret
== PIPE_OK
);
246 #define EMIT_RS(_rs, _count, _name, _value) \
248 _rs[_count].state = _name; \
249 _rs[_count].uintValue = _value; \
254 /* Setup any hardware state which will be constant through the life of
257 enum pipe_error
svga_emit_initial_state( struct svga_context
*svga
)
259 SVGA3dRenderState
*rs
;
261 const unsigned COUNT
= 2;
264 ret
= SVGA3D_BeginSetRenderState( svga
->swc
, &rs
, COUNT
);
268 /* Always use D3D style coordinate space as this is the only one
269 * which is implemented on all backends.
271 EMIT_RS(rs
, count
, SVGA3D_RS_COORDINATETYPE
, SVGA3D_COORDINATE_LEFTHANDED
);
272 EMIT_RS(rs
, count
, SVGA3D_RS_FRONTWINDING
, SVGA3D_FRONTWINDING_CW
);
274 assert( COUNT
== count
);
275 SVGA_FIFOCommitAll( svga
->swc
);