Merge branch 'instanced-arrays'
[mesa.git] / src / gallium / drivers / svga / svga_pipe_draw.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 "svga_cmd.h"
27
28 #include "pipe/p_inlines.h"
29 #include "util/u_prim.h"
30 #include "util/u_time.h"
31 #include "indices/u_indices.h"
32
33 #include "svga_hw_reg.h"
34 #include "svga_context.h"
35 #include "svga_screen.h"
36 #include "svga_winsys.h"
37 #include "svga_draw.h"
38 #include "svga_state.h"
39 #include "svga_swtnl.h"
40 #include "svga_debug.h"
41
42
43
44 static enum pipe_error
45 retry_draw_range_elements( struct svga_context *svga,
46 struct pipe_buffer *index_buffer,
47 unsigned index_size,
48 unsigned min_index,
49 unsigned max_index,
50 unsigned prim,
51 unsigned start,
52 unsigned count,
53 boolean do_retry )
54 {
55 enum pipe_error ret = 0;
56
57 svga_hwtnl_set_unfilled( svga->hwtnl,
58 svga->curr.rast->hw_unfilled );
59
60 svga_hwtnl_set_flatshade( svga->hwtnl,
61 svga->curr.rast->templ.flatshade,
62 svga->curr.rast->templ.flatshade_first );
63
64
65 ret = svga_update_state( svga, SVGA_STATE_HW_DRAW );
66 if (ret)
67 goto retry;
68
69 ret = svga_hwtnl_draw_range_elements( svga->hwtnl,
70 index_buffer, index_size,
71 min_index, max_index,
72 prim, start, count, 0 );
73 if (ret)
74 goto retry;
75
76 if (svga->curr.any_user_vertex_buffers) {
77 ret = svga_hwtnl_flush( svga->hwtnl );
78 if (ret)
79 goto retry;
80 }
81
82 return PIPE_OK;
83
84 retry:
85 svga_context_flush( svga, NULL );
86
87 if (do_retry)
88 {
89 return retry_draw_range_elements( svga,
90 index_buffer, index_size,
91 min_index, max_index,
92 prim, start, count,
93 FALSE );
94 }
95
96 return ret;
97 }
98
99
100 static enum pipe_error
101 retry_draw_arrays( struct svga_context *svga,
102 unsigned prim,
103 unsigned start,
104 unsigned count,
105 boolean do_retry )
106 {
107 enum pipe_error ret;
108
109 svga_hwtnl_set_unfilled( svga->hwtnl,
110 svga->curr.rast->hw_unfilled );
111
112 svga_hwtnl_set_flatshade( svga->hwtnl,
113 svga->curr.rast->templ.flatshade,
114 svga->curr.rast->templ.flatshade_first );
115
116 ret = svga_update_state( svga, SVGA_STATE_HW_DRAW );
117 if (ret)
118 goto retry;
119
120 ret = svga_hwtnl_draw_arrays( svga->hwtnl, prim,
121 start, count );
122 if (ret)
123 goto retry;
124
125 if (svga->curr.any_user_vertex_buffers) {
126 ret = svga_hwtnl_flush( svga->hwtnl );
127 if (ret)
128 goto retry;
129 }
130
131 return 0;
132
133 retry:
134 if (ret == PIPE_ERROR_OUT_OF_MEMORY && do_retry)
135 {
136 svga_context_flush( svga, NULL );
137
138 return retry_draw_arrays( svga,
139 prim,
140 start,
141 count,
142 FALSE );
143 }
144
145 return ret;
146 }
147
148
149
150
151
152 static void
153 svga_draw_range_elements( struct pipe_context *pipe,
154 struct pipe_buffer *index_buffer,
155 unsigned index_size,
156 unsigned min_index,
157 unsigned max_index,
158 unsigned prim, unsigned start, unsigned count)
159 {
160 struct svga_context *svga = svga_context( pipe );
161 unsigned reduced_prim = u_reduced_prim(prim);
162 enum pipe_error ret = 0;
163
164 if (!u_trim_pipe_prim( prim, &count ))
165 return;
166
167 /*
168 * Mark currently bound target surfaces as dirty
169 * doesn't really matter if it is done before drawing.
170 *
171 * TODO If we ever normaly return something other then
172 * true we should not mark it as dirty then.
173 */
174 svga_mark_surfaces_dirty(svga_context(pipe));
175
176 if (svga->curr.reduced_prim != reduced_prim) {
177 svga->curr.reduced_prim = reduced_prim;
178 svga->dirty |= SVGA_NEW_REDUCED_PRIMITIVE;
179 }
180
181 svga_update_state_retry( svga, SVGA_STATE_NEED_SWTNL );
182
183 #ifdef DEBUG
184 if (svga->curr.vs->base.id == svga->debug.disable_shader ||
185 svga->curr.fs->base.id == svga->debug.disable_shader)
186 return;
187 #endif
188
189 if (svga->state.sw.need_swtnl)
190 {
191 ret = svga_swtnl_draw_range_elements( svga,
192 index_buffer,
193 index_size,
194 min_index, max_index,
195 prim,
196 start, count );
197 }
198 else {
199 if (index_buffer) {
200 ret = retry_draw_range_elements( svga,
201 index_buffer,
202 index_size,
203 min_index,
204 max_index,
205 prim,
206 start,
207 count,
208 TRUE );
209 }
210 else {
211 ret = retry_draw_arrays( svga,
212 prim,
213 start,
214 count,
215 TRUE );
216 }
217 }
218
219 if (SVGA_DEBUG & DEBUG_FLUSH) {
220 static unsigned id;
221 debug_printf("%s %d\n", __FUNCTION__, id++);
222 if (id > 1300)
223 util_time_sleep( 2000 );
224
225 svga_hwtnl_flush_retry( svga );
226 svga_context_flush(svga, NULL);
227 }
228 }
229
230
231 static void
232 svga_draw_elements( struct pipe_context *pipe,
233 struct pipe_buffer *index_buffer,
234 unsigned index_size,
235 unsigned prim, unsigned start, unsigned count)
236 {
237 svga_draw_range_elements( pipe, index_buffer,
238 index_size,
239 0, 0xffffffff,
240 prim, start, count );
241 }
242
243 static void
244 svga_draw_arrays( struct pipe_context *pipe,
245 unsigned prim, unsigned start, unsigned count)
246 {
247 svga_draw_range_elements(pipe, NULL, 0,
248 start, start + count - 1,
249 prim,
250 start, count);
251 }
252
253
254 void svga_init_draw_functions( struct svga_context *svga )
255 {
256 svga->pipe.draw_arrays = svga_draw_arrays;
257 svga->pipe.draw_elements = svga_draw_elements;
258 svga->pipe.draw_range_elements = svga_draw_range_elements;
259 }