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 **********************************************************/
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"
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"
45 * Determine the ranges to upload for the user-buffers referenced
46 * by the next draw command.
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
56 svga_user_buffer_range(struct svga_context
*svga
,
59 unsigned instance_count
)
61 const struct pipe_vertex_element
*ve
= svga
->curr
.velems
->velem
;
65 * Release old uploaded range (if not done already) and
66 * initialize new ranges.
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
];
73 if (vb
->buffer
&& svga_buffer_is_user_buffer(vb
->buffer
)) {
74 struct svga_buffer
*buffer
= svga_buffer(vb
->buffer
);
76 pipe_resource_reference(&buffer
->uploaded
.buffer
, NULL
);
77 buffer
->uploaded
.start
= ~0;
78 buffer
->uploaded
.end
= 0;
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
];
86 if (vb
->buffer
&& svga_buffer_is_user_buffer(vb
->buffer
)) {
87 struct svga_buffer
*buffer
= svga_buffer(vb
->buffer
);
89 unsigned instance_div
= ve
[i
].instance_divisor
;
90 unsigned elemSize
= util_format_get_blocksize(ve
[i
].src_format
);
92 svga
->dirty
|= SVGA_NEW_VBUFFER
;
95 first
= ve
[i
].src_offset
;
96 count
= (instance_count
+ instance_div
- 1) / instance_div
;
97 size
= vb
->stride
* (count
- 1) + elemSize
;
99 first
= vb
->stride
* start
+ ve
[i
].src_offset
;
100 size
= vb
->stride
* (count
- 1) + elemSize
;
103 buffer
->uploaded
.start
= MIN2(buffer
->uploaded
.start
, first
);
104 buffer
->uploaded
.end
= MAX2(buffer
->uploaded
.end
, first
+ size
);
110 * svga_upload_user_buffers - upload parts of user buffers
112 * This function streams a part of a user buffer to hw and fills
113 * svga_buffer::uploaded with information on the upload.
117 svga_upload_user_buffers(struct svga_context
*svga
,
120 unsigned instance_count
)
122 const struct pipe_vertex_element
*ve
= svga
->curr
.velems
->velem
;
126 svga_user_buffer_range(svga
, start
, count
, instance_count
);
128 for (i
=0; i
< svga
->curr
.velems
->count
; i
++) {
129 struct pipe_vertex_buffer
*vb
=
130 &svga
->curr
.vb
[ve
[i
].vertex_buffer_index
];
132 if (vb
->buffer
&& svga_buffer_is_user_buffer(vb
->buffer
)) {
133 struct svga_buffer
*buffer
= svga_buffer(vb
->buffer
);
136 * Check if already uploaded. Otherwise go ahead and upload.
139 if (buffer
->uploaded
.buffer
)
142 ret
= u_upload_buffer( svga
->upload_vb
,
144 buffer
->uploaded
.start
,
145 buffer
->uploaded
.end
- buffer
->uploaded
.start
,
147 &buffer
->uploaded
.offset
,
148 &buffer
->uploaded
.buffer
);
154 debug_printf("%s: %d: orig buf %p upl buf %p ofs %d sofs %d"
159 buffer
->uploaded
.buffer
,
160 buffer
->uploaded
.offset
,
161 buffer
->uploaded
.start
,
162 buffer
->uploaded
.end
- buffer
->uploaded
.start
);
164 vb
->buffer_offset
= buffer
->uploaded
.offset
;
172 * svga_release_user_upl_buffers - release uploaded parts of user buffers
174 * This function releases the hw copy of the uploaded fraction of the
175 * user-buffer. It's important to do this as soon as all draw calls
176 * affecting the uploaded fraction are issued, as this allows for
177 * efficient reuse of the hardware surface backing the uploaded fraction.
179 * svga_buffer::source_offset is set to 0, and svga_buffer::uploaded::buffer
184 svga_release_user_upl_buffers(struct svga_context
*svga
)
189 nr
= svga
->curr
.num_vertex_buffers
;
191 for (i
= 0; i
< nr
; ++i
) {
192 struct pipe_vertex_buffer
*vb
= &svga
->curr
.vb
[i
];
194 if (vb
->buffer
&& svga_buffer_is_user_buffer(vb
->buffer
)) {
195 struct svga_buffer
*buffer
= svga_buffer(vb
->buffer
);
197 /* The buffer_offset is relative to the uploaded buffer.
198 * Since we're discarding that buffer we need to reset this offset
199 * so it's not inadvertantly applied to a subsequent draw.
201 * XXX a root problem here is that the svga->curr.vb[] information
202 * is getting set both by gallium API calls and by code in
203 * svga_upload_user_buffers(). We should instead have two copies
204 * of the vertex buffer information and choose between as needed.
206 vb
->buffer_offset
= 0;
208 buffer
->uploaded
.start
= ~0;
209 buffer
->uploaded
.end
= 0;
210 if (buffer
->uploaded
.buffer
)
211 pipe_resource_reference(&buffer
->uploaded
.buffer
, NULL
);
218 static enum pipe_error
219 retry_draw_range_elements( struct svga_context
*svga
,
220 struct pipe_resource
*index_buffer
,
228 unsigned instance_count
,
231 enum pipe_error ret
= PIPE_OK
;
233 svga_hwtnl_set_unfilled( svga
->hwtnl
,
234 svga
->curr
.rast
->hw_unfilled
);
236 svga_hwtnl_set_flatshade( svga
->hwtnl
,
237 svga
->curr
.rast
->templ
.flatshade
,
238 svga
->curr
.rast
->templ
.flatshade_first
);
240 ret
= svga_upload_user_buffers( svga
, min_index
+ index_bias
,
241 max_index
- min_index
+ 1, instance_count
);
245 ret
= svga_update_state( svga
, SVGA_STATE_HW_DRAW
);
249 ret
= svga_hwtnl_draw_range_elements( svga
->hwtnl
,
250 index_buffer
, index_size
, index_bias
,
251 min_index
, max_index
,
252 prim
, start
, count
);
259 svga_context_flush( svga
, NULL
);
263 return retry_draw_range_elements( svga
,
264 index_buffer
, index_size
, index_bias
,
265 min_index
, max_index
,
267 instance_count
, FALSE
);
274 static enum pipe_error
275 retry_draw_arrays( struct svga_context
*svga
,
279 unsigned instance_count
,
284 svga_hwtnl_set_unfilled( svga
->hwtnl
,
285 svga
->curr
.rast
->hw_unfilled
);
287 svga_hwtnl_set_flatshade( svga
->hwtnl
,
288 svga
->curr
.rast
->templ
.flatshade
,
289 svga
->curr
.rast
->templ
.flatshade_first
);
291 ret
= svga_upload_user_buffers( svga
, start
, count
, instance_count
);
296 ret
= svga_update_state( svga
, SVGA_STATE_HW_DRAW
);
300 ret
= svga_hwtnl_draw_arrays( svga
->hwtnl
, prim
,
308 if (ret
== PIPE_ERROR_OUT_OF_MEMORY
&& do_retry
)
310 svga_context_flush( svga
, NULL
);
312 return retry_draw_arrays( svga
,
325 svga_draw_vbo(struct pipe_context
*pipe
, const struct pipe_draw_info
*info
)
327 struct svga_context
*svga
= svga_context( pipe
);
328 unsigned reduced_prim
= u_reduced_prim( info
->mode
);
329 unsigned count
= info
->count
;
330 enum pipe_error ret
= 0;
331 boolean needed_swtnl
;
333 if (!u_trim_pipe_prim( info
->mode
, &count
))
337 * Mark currently bound target surfaces as dirty
338 * doesn't really matter if it is done before drawing.
340 * TODO If we ever normaly return something other then
341 * true we should not mark it as dirty then.
343 svga_mark_surfaces_dirty(svga_context(pipe
));
345 if (svga
->curr
.reduced_prim
!= reduced_prim
) {
346 svga
->curr
.reduced_prim
= reduced_prim
;
347 svga
->dirty
|= SVGA_NEW_REDUCED_PRIMITIVE
;
350 needed_swtnl
= svga
->state
.sw
.need_swtnl
;
352 svga_update_state_retry( svga
, SVGA_STATE_NEED_SWTNL
);
355 if (svga
->curr
.vs
->base
.id
== svga
->debug
.disable_shader
||
356 svga
->curr
.fs
->base
.id
== svga
->debug
.disable_shader
)
360 if (svga
->state
.sw
.need_swtnl
) {
363 * We're switching from HW to SW TNL. SW TNL will require mapping all
364 * currently bound vertex buffers, some of which may already be
365 * referenced in the current command buffer as result of previous HW
366 * TNL. So flush now, to prevent the context to flush while a referred
367 * vertex buffer is mapped.
370 svga_context_flush(svga
, NULL
);
373 /* Avoid leaking the previous hwtnl bias to swtnl */
374 svga_hwtnl_set_index_bias( svga
->hwtnl
, 0 );
375 ret
= svga_swtnl_draw_vbo( svga
, info
);
378 if (info
->indexed
&& svga
->curr
.ib
.buffer
) {
381 assert(svga
->curr
.ib
.offset
% svga
->curr
.ib
.index_size
== 0);
382 offset
= svga
->curr
.ib
.offset
/ svga
->curr
.ib
.index_size
;
384 ret
= retry_draw_range_elements( svga
,
385 svga
->curr
.ib
.buffer
,
386 svga
->curr
.ib
.index_size
,
391 info
->start
+ offset
,
393 info
->instance_count
,
397 ret
= retry_draw_arrays( svga
,
401 info
->instance_count
,
406 /* XXX: Silence warnings, do something sensible here? */
409 svga_release_user_upl_buffers( svga
);
411 if (SVGA_DEBUG
& DEBUG_FLUSH
) {
412 svga_hwtnl_flush_retry( svga
);
413 svga_context_flush(svga
, NULL
);
418 void svga_init_draw_functions( struct svga_context
*svga
)
420 svga
->pipe
.draw_vbo
= svga_draw_vbo
;