svga: fix incorrect user buffer size computation for instance divisor case
[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 "util/u_format.h"
29 #include "util/u_inlines.h"
30 #include "util/u_prim.h"
31 #include "util/u_time.h"
32 #include "indices/u_indices.h"
33
34 #include "svga_hw_reg.h"
35 #include "svga_context.h"
36 #include "svga_screen.h"
37 #include "svga_draw.h"
38 #include "svga_state.h"
39 #include "svga_swtnl.h"
40 #include "svga_debug.h"
41 #include "svga_resource_buffer.h"
42 #include "util/u_upload_mgr.h"
43
44 /**
45 * svga_upload_user_buffers - upload parts of user buffers
46 *
47 * This function streams a part of a user buffer to hw and sets
48 * svga_buffer::source_offset to the first byte uploaded. After upload
49 * also svga_buffer::uploaded::buffer is set to !NULL
50 */
51
52 static int
53 svga_upload_user_buffers(struct svga_context *svga,
54 unsigned start,
55 unsigned count,
56 unsigned instance_count)
57 {
58 const struct pipe_vertex_element *ve = svga->curr.velems->velem;
59 unsigned i;
60 int ret;
61
62 for (i=0; i < svga->curr.velems->count; i++) {
63 struct pipe_vertex_buffer *vb =
64 &svga->curr.vb[ve[i].vertex_buffer_index];
65
66 if (vb->buffer && svga_buffer_is_user_buffer(vb->buffer)) {
67 struct svga_buffer *buffer = svga_buffer(vb->buffer);
68 unsigned first, size;
69 boolean flushed;
70 unsigned instance_div = ve[i].instance_divisor;
71 unsigned elemSize = util_format_get_blocksize(ve->src_format);
72
73 svga->dirty |= SVGA_NEW_VBUFFER;
74
75 if (instance_div) {
76 first = 0;
77 count = (instance_count + instance_div - 1) / instance_div;
78 size = vb->stride * (count - 1) + elemSize;
79 } else if (vb->stride) {
80 first = vb->stride * start;
81 size = vb->stride * (count - 1) + elemSize;
82 } else {
83 /* Only a single vertex!
84 * Upload with the largest vertex size the hw supports,
85 * if possible.
86 */
87 first = 0;
88 size = MIN2(16, vb->buffer->width0);
89 }
90
91 ret = u_upload_buffer( svga->upload_vb,
92 0, first, size,
93 &buffer->b.b,
94 &buffer->uploaded.offset,
95 &buffer->uploaded.buffer,
96 &flushed);
97
98 if (ret)
99 return ret;
100
101 if (0)
102 debug_printf("%s: %d: orig buf %p upl buf %p ofs %d sofs %d"
103 " sz %d\n",
104 __FUNCTION__,
105 i,
106 buffer,
107 buffer->uploaded.buffer,
108 buffer->uploaded.offset,
109 first,
110 size);
111
112 vb->buffer_offset = buffer->uploaded.offset;
113 buffer->source_offset = first;
114 }
115 }
116
117 return PIPE_OK;
118 }
119
120 /**
121 * svga_release_user_upl_buffers - release uploaded parts of user buffers
122 *
123 * This function releases the hw copy of the uploaded fraction of the
124 * user-buffer. It's important to do this as soon as all draw calls
125 * affecting the uploaded fraction are issued, as this allows for
126 * efficient reuse of the hardware surface backing the uploaded fraction.
127 *
128 * svga_buffer::source_offset is set to 0, and svga_buffer::uploaded::buffer
129 * is set to 0.
130 */
131
132 static void
133 svga_release_user_upl_buffers(struct svga_context *svga)
134 {
135 unsigned i;
136 unsigned nr;
137
138 nr = svga->curr.num_vertex_buffers;
139
140 for (i = 0; i < nr; ++i) {
141 struct pipe_vertex_buffer *vb = &svga->curr.vb[i];
142
143 if (vb->buffer && svga_buffer_is_user_buffer(vb->buffer)) {
144 struct svga_buffer *buffer = svga_buffer(vb->buffer);
145
146 buffer->source_offset = 0;
147 if (buffer->uploaded.buffer)
148 pipe_resource_reference(&buffer->uploaded.buffer, NULL);
149 }
150 }
151 }
152
153
154
155 static enum pipe_error
156 retry_draw_range_elements( struct svga_context *svga,
157 struct pipe_resource *index_buffer,
158 unsigned index_size,
159 int index_bias,
160 unsigned min_index,
161 unsigned max_index,
162 unsigned prim,
163 unsigned start,
164 unsigned count,
165 unsigned instance_count,
166 boolean do_retry )
167 {
168 enum pipe_error ret = 0;
169
170 svga_hwtnl_set_unfilled( svga->hwtnl,
171 svga->curr.rast->hw_unfilled );
172
173 svga_hwtnl_set_flatshade( svga->hwtnl,
174 svga->curr.rast->templ.flatshade,
175 svga->curr.rast->templ.flatshade_first );
176
177 ret = svga_upload_user_buffers( svga, min_index + index_bias,
178 max_index - min_index + 1, instance_count );
179 if (ret != PIPE_OK)
180 goto retry;
181
182 ret = svga_update_state( svga, SVGA_STATE_HW_DRAW );
183 if (ret)
184 goto retry;
185
186 ret = svga_hwtnl_draw_range_elements( svga->hwtnl,
187 index_buffer, index_size, index_bias,
188 min_index, max_index,
189 prim, start, count );
190 if (ret)
191 goto retry;
192
193 return PIPE_OK;
194
195 retry:
196 svga_context_flush( svga, NULL );
197
198 if (do_retry)
199 {
200 return retry_draw_range_elements( svga,
201 index_buffer, index_size, index_bias,
202 min_index, max_index,
203 prim, start, count,
204 instance_count, FALSE );
205 }
206
207 return ret;
208 }
209
210
211 static enum pipe_error
212 retry_draw_arrays( struct svga_context *svga,
213 unsigned prim,
214 unsigned start,
215 unsigned count,
216 unsigned instance_count,
217 boolean do_retry )
218 {
219 enum pipe_error ret;
220
221 svga_hwtnl_set_unfilled( svga->hwtnl,
222 svga->curr.rast->hw_unfilled );
223
224 svga_hwtnl_set_flatshade( svga->hwtnl,
225 svga->curr.rast->templ.flatshade,
226 svga->curr.rast->templ.flatshade_first );
227
228 ret = svga_upload_user_buffers( svga, start, count, instance_count );
229
230 if (ret != PIPE_OK)
231 goto retry;
232
233 ret = svga_update_state( svga, SVGA_STATE_HW_DRAW );
234 if (ret)
235 goto retry;
236
237 ret = svga_hwtnl_draw_arrays( svga->hwtnl, prim,
238 start, count );
239 if (ret)
240 goto retry;
241
242 return 0;
243
244 retry:
245 if (ret == PIPE_ERROR_OUT_OF_MEMORY && do_retry)
246 {
247 svga_context_flush( svga, NULL );
248
249 return retry_draw_arrays( svga,
250 prim,
251 start,
252 count,
253 instance_count,
254 FALSE );
255 }
256
257 return ret;
258 }
259
260
261 static void
262 svga_draw_vbo(struct pipe_context *pipe, const struct pipe_draw_info *info)
263 {
264 struct svga_context *svga = svga_context( pipe );
265 unsigned reduced_prim = u_reduced_prim( info->mode );
266 unsigned count = info->count;
267 enum pipe_error ret = 0;
268 boolean needed_swtnl;
269
270 if (!u_trim_pipe_prim( info->mode, &count ))
271 return;
272
273 /*
274 * Mark currently bound target surfaces as dirty
275 * doesn't really matter if it is done before drawing.
276 *
277 * TODO If we ever normaly return something other then
278 * true we should not mark it as dirty then.
279 */
280 svga_mark_surfaces_dirty(svga_context(pipe));
281
282 if (svga->curr.reduced_prim != reduced_prim) {
283 svga->curr.reduced_prim = reduced_prim;
284 svga->dirty |= SVGA_NEW_REDUCED_PRIMITIVE;
285 }
286
287 needed_swtnl = svga->state.sw.need_swtnl;
288
289 svga_update_state_retry( svga, SVGA_STATE_NEED_SWTNL );
290
291 #ifdef DEBUG
292 if (svga->curr.vs->base.id == svga->debug.disable_shader ||
293 svga->curr.fs->base.id == svga->debug.disable_shader)
294 return;
295 #endif
296
297 if (svga->state.sw.need_swtnl) {
298 if (!needed_swtnl) {
299 /*
300 * We're switching from HW to SW TNL. SW TNL will require mapping all
301 * currently bound vertex buffers, some of which may already be
302 * referenced in the current command buffer as result of previous HW
303 * TNL. So flush now, to prevent the context to flush while a referred
304 * vertex buffer is mapped.
305 */
306
307 svga_context_flush(svga, NULL);
308 }
309
310 /* Avoid leaking the previous hwtnl bias to swtnl */
311 svga_hwtnl_set_index_bias( svga->hwtnl, 0 );
312 ret = svga_swtnl_draw_vbo( svga, info );
313 }
314 else {
315 if (info->indexed && svga->curr.ib.buffer) {
316 unsigned offset;
317
318 assert(svga->curr.ib.offset % svga->curr.ib.index_size == 0);
319 offset = svga->curr.ib.offset / svga->curr.ib.index_size;
320
321 ret = retry_draw_range_elements( svga,
322 svga->curr.ib.buffer,
323 svga->curr.ib.index_size,
324 info->index_bias,
325 info->min_index,
326 info->max_index,
327 info->mode,
328 info->start + offset,
329 info->count,
330 info->instance_count,
331 TRUE );
332 }
333 else {
334 ret = retry_draw_arrays( svga,
335 info->mode,
336 info->start,
337 info->count,
338 info->instance_count,
339 TRUE );
340 }
341 }
342
343 svga_release_user_upl_buffers( svga );
344
345 if (SVGA_DEBUG & DEBUG_FLUSH) {
346 svga_hwtnl_flush_retry( svga );
347 svga_context_flush(svga, NULL);
348 }
349 }
350
351
352 void svga_init_draw_functions( struct svga_context *svga )
353 {
354 svga->pipe.draw_vbo = svga_draw_vbo;
355 }