svga: return PIPE_OK instead of 0
[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 * Determine the ranges to upload for the user-buffers referenced
46 * by the next draw command.
47 *
48 * TODO: It might be beneficial to support multiple ranges. In that case,
49 * the struct svga_buffer::uploaded member should be made an array or a
50 * list, since we need to account for the possibility that different ranges
51 * may be uploaded to different hardware buffers chosen by the utility
52 * upload manager.
53 */
54
55 static void
56 svga_user_buffer_range(struct svga_context *svga,
57 unsigned start,
58 unsigned count,
59 unsigned instance_count)
60 {
61 const struct pipe_vertex_element *ve = svga->curr.velems->velem;
62 int i;
63
64 /*
65 * Release old uploaded range (if not done already) and
66 * initialize new ranges.
67 */
68
69 for (i=0; i < svga->curr.velems->count; i++) {
70 struct pipe_vertex_buffer *vb =
71 &svga->curr.vb[ve[i].vertex_buffer_index];
72
73 if (vb->buffer && svga_buffer_is_user_buffer(vb->buffer)) {
74 struct svga_buffer *buffer = svga_buffer(vb->buffer);
75
76 pipe_resource_reference(&buffer->uploaded.buffer, NULL);
77 buffer->uploaded.start = ~0;
78 buffer->uploaded.end = 0;
79 }
80 }
81
82 for (i=0; i < svga->curr.velems->count; i++) {
83 struct pipe_vertex_buffer *vb =
84 &svga->curr.vb[ve[i].vertex_buffer_index];
85
86 if (vb->buffer && svga_buffer_is_user_buffer(vb->buffer)) {
87 struct svga_buffer *buffer = svga_buffer(vb->buffer);
88 unsigned first, size;
89 unsigned instance_div = ve[i].instance_divisor;
90 unsigned elemSize = util_format_get_blocksize(ve[i].src_format);
91
92 svga->dirty |= SVGA_NEW_VBUFFER;
93
94 if (instance_div) {
95 first = ve[i].src_offset;
96 count = (instance_count + instance_div - 1) / instance_div;
97 size = vb->stride * (count - 1) + elemSize;
98 } else if (vb->stride) {
99 first = vb->stride * start + ve[i].src_offset;
100 size = vb->stride * (count - 1) + elemSize;
101 } else {
102 /* Only a single vertex!
103 * Upload with the largest vertex size the hw supports,
104 * if possible.
105 */
106 first = ve[i].src_offset;
107 size = MIN2(16, vb->buffer->width0);
108 }
109
110 buffer->uploaded.start = MIN2(buffer->uploaded.start, first);
111 buffer->uploaded.end = MAX2(buffer->uploaded.end, first + size);
112 }
113 }
114 }
115
116 /**
117 * svga_upload_user_buffers - upload parts of user buffers
118 *
119 * This function streams a part of a user buffer to hw and fills
120 * svga_buffer::uploaded with information on the upload.
121 */
122
123 static int
124 svga_upload_user_buffers(struct svga_context *svga,
125 unsigned start,
126 unsigned count,
127 unsigned instance_count)
128 {
129 const struct pipe_vertex_element *ve = svga->curr.velems->velem;
130 unsigned i;
131 int ret;
132
133 svga_user_buffer_range(svga, start, count, instance_count);
134
135 for (i=0; i < svga->curr.velems->count; i++) {
136 struct pipe_vertex_buffer *vb =
137 &svga->curr.vb[ve[i].vertex_buffer_index];
138
139 if (vb->buffer && svga_buffer_is_user_buffer(vb->buffer)) {
140 struct svga_buffer *buffer = svga_buffer(vb->buffer);
141
142 /*
143 * Check if already uploaded. Otherwise go ahead and upload.
144 */
145
146 if (buffer->uploaded.buffer)
147 continue;
148
149 ret = u_upload_buffer( svga->upload_vb,
150 0,
151 buffer->uploaded.start,
152 buffer->uploaded.end - buffer->uploaded.start,
153 &buffer->b.b,
154 &buffer->uploaded.offset,
155 &buffer->uploaded.buffer);
156
157 if (ret)
158 return ret;
159
160 if (0)
161 debug_printf("%s: %d: orig buf %p upl buf %p ofs %d sofs %d"
162 " sz %d\n",
163 __FUNCTION__,
164 i,
165 buffer,
166 buffer->uploaded.buffer,
167 buffer->uploaded.offset,
168 buffer->uploaded.start,
169 buffer->uploaded.end - buffer->uploaded.start);
170
171 vb->buffer_offset = buffer->uploaded.offset;
172 }
173 }
174
175 return PIPE_OK;
176 }
177
178 /**
179 * svga_release_user_upl_buffers - release uploaded parts of user buffers
180 *
181 * This function releases the hw copy of the uploaded fraction of the
182 * user-buffer. It's important to do this as soon as all draw calls
183 * affecting the uploaded fraction are issued, as this allows for
184 * efficient reuse of the hardware surface backing the uploaded fraction.
185 *
186 * svga_buffer::source_offset is set to 0, and svga_buffer::uploaded::buffer
187 * is set to 0.
188 */
189
190 static void
191 svga_release_user_upl_buffers(struct svga_context *svga)
192 {
193 unsigned i;
194 unsigned nr;
195
196 nr = svga->curr.num_vertex_buffers;
197
198 for (i = 0; i < nr; ++i) {
199 struct pipe_vertex_buffer *vb = &svga->curr.vb[i];
200
201 if (vb->buffer && svga_buffer_is_user_buffer(vb->buffer)) {
202 struct svga_buffer *buffer = svga_buffer(vb->buffer);
203
204 /* The buffer_offset is relative to the uploaded buffer.
205 * Since we're discarding that buffer we need to reset this offset
206 * so it's not inadvertantly applied to a subsequent draw.
207 *
208 * XXX a root problem here is that the svga->curr.vb[] information
209 * is getting set both by gallium API calls and by code in
210 * svga_upload_user_buffers(). We should instead have two copies
211 * of the vertex buffer information and choose between as needed.
212 */
213 vb->buffer_offset = 0;
214
215 buffer->uploaded.start = ~0;
216 buffer->uploaded.end = 0;
217 if (buffer->uploaded.buffer)
218 pipe_resource_reference(&buffer->uploaded.buffer, NULL);
219 }
220 }
221 }
222
223
224
225 static enum pipe_error
226 retry_draw_range_elements( struct svga_context *svga,
227 struct pipe_resource *index_buffer,
228 unsigned index_size,
229 int index_bias,
230 unsigned min_index,
231 unsigned max_index,
232 unsigned prim,
233 unsigned start,
234 unsigned count,
235 unsigned instance_count,
236 boolean do_retry )
237 {
238 enum pipe_error ret = PIPE_OK;
239
240 svga_hwtnl_set_unfilled( svga->hwtnl,
241 svga->curr.rast->hw_unfilled );
242
243 svga_hwtnl_set_flatshade( svga->hwtnl,
244 svga->curr.rast->templ.flatshade,
245 svga->curr.rast->templ.flatshade_first );
246
247 ret = svga_upload_user_buffers( svga, min_index + index_bias,
248 max_index - min_index + 1, instance_count );
249 if (ret != PIPE_OK)
250 goto retry;
251
252 ret = svga_update_state( svga, SVGA_STATE_HW_DRAW );
253 if (ret != PIPE_OK)
254 goto retry;
255
256 ret = svga_hwtnl_draw_range_elements( svga->hwtnl,
257 index_buffer, index_size, index_bias,
258 min_index, max_index,
259 prim, start, count );
260 if (ret != PIPE_OK)
261 goto retry;
262
263 return PIPE_OK;
264
265 retry:
266 svga_context_flush( svga, NULL );
267
268 if (do_retry)
269 {
270 return retry_draw_range_elements( svga,
271 index_buffer, index_size, index_bias,
272 min_index, max_index,
273 prim, start, count,
274 instance_count, FALSE );
275 }
276
277 return ret;
278 }
279
280
281 static enum pipe_error
282 retry_draw_arrays( struct svga_context *svga,
283 unsigned prim,
284 unsigned start,
285 unsigned count,
286 unsigned instance_count,
287 boolean do_retry )
288 {
289 enum pipe_error ret;
290
291 svga_hwtnl_set_unfilled( svga->hwtnl,
292 svga->curr.rast->hw_unfilled );
293
294 svga_hwtnl_set_flatshade( svga->hwtnl,
295 svga->curr.rast->templ.flatshade,
296 svga->curr.rast->templ.flatshade_first );
297
298 ret = svga_upload_user_buffers( svga, start, count, instance_count );
299
300 if (ret != PIPE_OK)
301 goto retry;
302
303 ret = svga_update_state( svga, SVGA_STATE_HW_DRAW );
304 if (ret != PIPE_OK)
305 goto retry;
306
307 ret = svga_hwtnl_draw_arrays( svga->hwtnl, prim,
308 start, count );
309 if (ret != PIPE_OK)
310 goto retry;
311
312 return PIPE_OK;
313
314 retry:
315 if (ret == PIPE_ERROR_OUT_OF_MEMORY && do_retry)
316 {
317 svga_context_flush( svga, NULL );
318
319 return retry_draw_arrays( svga,
320 prim,
321 start,
322 count,
323 instance_count,
324 FALSE );
325 }
326
327 return ret;
328 }
329
330
331 static void
332 svga_draw_vbo(struct pipe_context *pipe, const struct pipe_draw_info *info)
333 {
334 struct svga_context *svga = svga_context( pipe );
335 unsigned reduced_prim = u_reduced_prim( info->mode );
336 unsigned count = info->count;
337 enum pipe_error ret = 0;
338 boolean needed_swtnl;
339
340 if (!u_trim_pipe_prim( info->mode, &count ))
341 return;
342
343 /*
344 * Mark currently bound target surfaces as dirty
345 * doesn't really matter if it is done before drawing.
346 *
347 * TODO If we ever normaly return something other then
348 * true we should not mark it as dirty then.
349 */
350 svga_mark_surfaces_dirty(svga_context(pipe));
351
352 if (svga->curr.reduced_prim != reduced_prim) {
353 svga->curr.reduced_prim = reduced_prim;
354 svga->dirty |= SVGA_NEW_REDUCED_PRIMITIVE;
355 }
356
357 needed_swtnl = svga->state.sw.need_swtnl;
358
359 svga_update_state_retry( svga, SVGA_STATE_NEED_SWTNL );
360
361 #ifdef DEBUG
362 if (svga->curr.vs->base.id == svga->debug.disable_shader ||
363 svga->curr.fs->base.id == svga->debug.disable_shader)
364 return;
365 #endif
366
367 if (svga->state.sw.need_swtnl) {
368 if (!needed_swtnl) {
369 /*
370 * We're switching from HW to SW TNL. SW TNL will require mapping all
371 * currently bound vertex buffers, some of which may already be
372 * referenced in the current command buffer as result of previous HW
373 * TNL. So flush now, to prevent the context to flush while a referred
374 * vertex buffer is mapped.
375 */
376
377 svga_context_flush(svga, NULL);
378 }
379
380 /* Avoid leaking the previous hwtnl bias to swtnl */
381 svga_hwtnl_set_index_bias( svga->hwtnl, 0 );
382 ret = svga_swtnl_draw_vbo( svga, info );
383 }
384 else {
385 if (info->indexed && svga->curr.ib.buffer) {
386 unsigned offset;
387
388 assert(svga->curr.ib.offset % svga->curr.ib.index_size == 0);
389 offset = svga->curr.ib.offset / svga->curr.ib.index_size;
390
391 ret = retry_draw_range_elements( svga,
392 svga->curr.ib.buffer,
393 svga->curr.ib.index_size,
394 info->index_bias,
395 info->min_index,
396 info->max_index,
397 info->mode,
398 info->start + offset,
399 info->count,
400 info->instance_count,
401 TRUE );
402 }
403 else {
404 ret = retry_draw_arrays( svga,
405 info->mode,
406 info->start,
407 info->count,
408 info->instance_count,
409 TRUE );
410 }
411 }
412
413 /* XXX: Silence warnings, do something sensible here? */
414 (void)ret;
415
416 svga_release_user_upl_buffers( svga );
417
418 if (SVGA_DEBUG & DEBUG_FLUSH) {
419 svga_hwtnl_flush_retry( svga );
420 svga_context_flush(svga, NULL);
421 }
422 }
423
424
425 void svga_init_draw_functions( struct svga_context *svga )
426 {
427 svga->pipe.draw_vbo = svga_draw_vbo;
428 }