svga: implement user index buffers
[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
27 #include "util/u_format.h"
28 #include "util/u_helpers.h"
29 #include "util/u_inlines.h"
30 #include "util/u_prim.h"
31 #include "util/u_prim_restart.h"
32 #include "util/u_time.h"
33 #include "util/u_upload_mgr.h"
34 #include "indices/u_indices.h"
35
36 #include "svga_hw_reg.h"
37 #include "svga_cmd.h"
38 #include "svga_context.h"
39 #include "svga_screen.h"
40 #include "svga_draw.h"
41 #include "svga_shader.h"
42 #include "svga_state.h"
43 #include "svga_surface.h"
44 #include "svga_swtnl.h"
45 #include "svga_debug.h"
46 #include "svga_resource_buffer.h"
47
48 static enum pipe_error
49 retry_draw_range_elements( struct svga_context *svga,
50 struct pipe_resource *index_buffer,
51 unsigned index_size,
52 int index_bias,
53 unsigned min_index,
54 unsigned max_index,
55 enum pipe_prim_type prim,
56 unsigned start,
57 unsigned count,
58 unsigned start_instance,
59 unsigned instance_count,
60 boolean do_retry )
61 {
62 enum pipe_error ret = PIPE_OK;
63
64 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_DRAWELEMENTS);
65
66 svga_hwtnl_set_fillmode(svga->hwtnl, svga->curr.rast->hw_fillmode);
67
68 ret = svga_update_state( svga, SVGA_STATE_HW_DRAW );
69 if (ret != PIPE_OK)
70 goto retry;
71
72 /** determine if flatshade is to be used after svga_update_state()
73 * in case the fragment shader is changed.
74 */
75 svga_hwtnl_set_flatshade(svga->hwtnl,
76 svga->curr.rast->templ.flatshade ||
77 svga->state.hw_draw.fs->uses_flat_interp,
78 svga->curr.rast->templ.flatshade_first);
79
80 ret = svga_hwtnl_draw_range_elements( svga->hwtnl,
81 index_buffer, index_size, index_bias,
82 min_index, max_index,
83 prim, start, count,
84 start_instance, instance_count);
85 if (ret != PIPE_OK)
86 goto retry;
87
88 goto done;
89
90 retry:
91 svga_context_flush( svga, NULL );
92
93 if (do_retry)
94 {
95 ret = retry_draw_range_elements(svga,
96 index_buffer, index_size, index_bias,
97 min_index, max_index,
98 prim, start, count,
99 start_instance, instance_count, FALSE);
100 }
101
102 done:
103 SVGA_STATS_TIME_POP(svga_sws(svga));
104 return ret;
105 }
106
107
108 static enum pipe_error
109 retry_draw_arrays( struct svga_context *svga,
110 enum pipe_prim_type prim, unsigned start, unsigned count,
111 unsigned start_instance, unsigned instance_count,
112 boolean do_retry )
113 {
114 enum pipe_error ret;
115
116 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_DRAWARRAYS);
117
118 svga_hwtnl_set_fillmode(svga->hwtnl, svga->curr.rast->hw_fillmode);
119
120 ret = svga_update_state( svga, SVGA_STATE_HW_DRAW );
121 if (ret != PIPE_OK)
122 goto retry;
123
124 /** determine if flatshade is to be used after svga_update_state()
125 * in case the fragment shader is changed.
126 */
127 svga_hwtnl_set_flatshade(svga->hwtnl,
128 svga->curr.rast->templ.flatshade ||
129 svga->state.hw_draw.fs->uses_flat_interp,
130 svga->curr.rast->templ.flatshade_first);
131
132 ret = svga_hwtnl_draw_arrays(svga->hwtnl, prim, start, count,
133 start_instance, instance_count);
134 if (ret != PIPE_OK)
135 goto retry;
136
137 goto done;
138
139 retry:
140 if (ret == PIPE_ERROR_OUT_OF_MEMORY && do_retry)
141 {
142 svga_context_flush( svga, NULL );
143
144 ret = retry_draw_arrays(svga, prim, start, count,
145 start_instance, instance_count,
146 FALSE);
147 }
148
149 done:
150 SVGA_STATS_TIME_POP(svga_sws(svga));
151 return ret;
152 }
153
154
155 /**
156 * Determine if we need to implement primitive restart with a fallback
157 * path which breaks the original primitive into sub-primitive at the
158 * restart indexes.
159 */
160 static boolean
161 need_fallback_prim_restart(const struct svga_context *svga,
162 const struct pipe_draw_info *info)
163 {
164 if (info->primitive_restart && info->indexed) {
165 if (!svga_have_vgpu10(svga))
166 return TRUE;
167 else if (!svga->state.sw.need_swtnl) {
168 if (svga->curr.ib.index_size == 1)
169 return TRUE; /* no device support for 1-byte indexes */
170 else if (svga->curr.ib.index_size == 2)
171 return info->restart_index != 0xffff;
172 else
173 return info->restart_index != 0xffffffff;
174 }
175 }
176
177 return FALSE;
178 }
179
180
181 static void
182 svga_draw_vbo(struct pipe_context *pipe, const struct pipe_draw_info *info)
183 {
184 struct svga_context *svga = svga_context( pipe );
185 unsigned reduced_prim = u_reduced_prim( info->mode );
186 unsigned count = info->count;
187 enum pipe_error ret = 0;
188 boolean needed_swtnl;
189
190 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_DRAWVBO);
191
192 svga->hud.num_draw_calls++; /* for SVGA_QUERY_NUM_DRAW_CALLS */
193
194 if (u_reduced_prim(info->mode) == PIPE_PRIM_TRIANGLES &&
195 svga->curr.rast->templ.cull_face == PIPE_FACE_FRONT_AND_BACK)
196 goto done;
197
198 /* Upload a user index buffer. */
199 struct pipe_index_buffer ibuffer_saved = {};
200 if (info->indexed && svga->curr.ib.user_buffer &&
201 !util_save_and_upload_index_buffer(pipe, info, &svga->curr.ib,
202 &ibuffer_saved)) {
203 return;
204 }
205
206 /*
207 * Mark currently bound target surfaces as dirty
208 * doesn't really matter if it is done before drawing.
209 *
210 * TODO If we ever normaly return something other then
211 * true we should not mark it as dirty then.
212 */
213 svga_mark_surfaces_dirty(svga_context(pipe));
214
215 if (svga->curr.reduced_prim != reduced_prim) {
216 svga->curr.reduced_prim = reduced_prim;
217 svga->dirty |= SVGA_NEW_REDUCED_PRIMITIVE;
218 }
219
220 if (need_fallback_prim_restart(svga, info)) {
221 enum pipe_error r;
222 r = util_draw_vbo_without_prim_restart(pipe, &svga->curr.ib, info);
223 assert(r == PIPE_OK);
224 (void) r;
225 goto done;
226 }
227
228 if (!u_trim_pipe_prim( info->mode, &count ))
229 goto done;
230
231 needed_swtnl = svga->state.sw.need_swtnl;
232
233 svga_update_state_retry( svga, SVGA_STATE_NEED_SWTNL );
234
235 if (svga->state.sw.need_swtnl) {
236 svga->hud.num_fallbacks++; /* for SVGA_QUERY_NUM_FALLBACKS */
237 if (!needed_swtnl) {
238 /*
239 * We're switching from HW to SW TNL. SW TNL will require mapping all
240 * currently bound vertex buffers, some of which may already be
241 * referenced in the current command buffer as result of previous HW
242 * TNL. So flush now, to prevent the context to flush while a referred
243 * vertex buffer is mapped.
244 */
245
246 svga_context_flush(svga, NULL);
247 }
248
249 /* Avoid leaking the previous hwtnl bias to swtnl */
250 svga_hwtnl_set_index_bias( svga->hwtnl, 0 );
251 ret = svga_swtnl_draw_vbo( svga, info );
252 }
253 else {
254 if (info->indexed && svga->curr.ib.buffer) {
255 unsigned offset;
256
257 assert(svga->curr.ib.offset % svga->curr.ib.index_size == 0);
258 offset = svga->curr.ib.offset / svga->curr.ib.index_size;
259
260 ret = retry_draw_range_elements( svga,
261 svga->curr.ib.buffer,
262 svga->curr.ib.index_size,
263 info->index_bias,
264 info->min_index,
265 info->max_index,
266 info->mode,
267 info->start + offset,
268 count,
269 info->start_instance,
270 info->instance_count,
271 TRUE );
272 }
273 else {
274 ret = retry_draw_arrays(svga, info->mode, info->start, count,
275 info->start_instance, info->instance_count,
276 TRUE);
277 }
278 }
279
280 /* XXX: Silence warnings, do something sensible here? */
281 (void)ret;
282
283 if (SVGA_DEBUG & DEBUG_FLUSH) {
284 svga_hwtnl_flush_retry( svga );
285 svga_context_flush(svga, NULL);
286 }
287
288 done:
289 if (info->indexed && ibuffer_saved.user_buffer)
290 pipe->set_index_buffer(pipe, &ibuffer_saved);
291
292 SVGA_STATS_TIME_POP(svga_sws(svga));
293 }
294
295
296 void svga_init_draw_functions( struct svga_context *svga )
297 {
298 svga->pipe.draw_vbo = svga_draw_vbo;
299 }