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 * svga_upload_user_buffers - upload parts of user buffers
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
53 svga_upload_user_buffers(struct svga_context
*svga
,
56 unsigned instance_count
)
58 const struct pipe_vertex_element
*ve
= svga
->curr
.velems
->velem
;
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
];
66 if (vb
->buffer
&& svga_buffer_is_user_buffer(vb
->buffer
)) {
67 struct svga_buffer
*buffer
= svga_buffer(vb
->buffer
);
70 unsigned instance_div
= ve
[i
].instance_divisor
;
72 svga
->dirty
|= SVGA_NEW_VBUFFER
;
77 (instance_count
+ instance_div
- 1) / instance_div
;
78 } else if (vb
->stride
) {
79 uint elemSize
= util_format_get_blocksize(ve
->src_format
);
80 first
= vb
->stride
* start
;
81 size
= vb
->stride
* (count
- 1) + elemSize
;
83 /* Only a single vertex!
84 * Upload with the largest vertex size the hw supports,
88 size
= MIN2(16, vb
->buffer
->width0
);
91 ret
= u_upload_buffer( svga
->upload_vb
,
94 &buffer
->uploaded
.offset
,
95 &buffer
->uploaded
.buffer
,
102 debug_printf("%s: %d: orig buf %p upl buf %p ofs %d sofs %d"
107 buffer
->uploaded
.buffer
,
108 buffer
->uploaded
.offset
,
112 vb
->buffer_offset
= buffer
->uploaded
.offset
;
113 buffer
->source_offset
= first
;
121 * svga_release_user_upl_buffers - release uploaded parts of user buffers
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.
128 * svga_buffer::source_offset is set to 0, and svga_buffer::uploaded::buffer
133 svga_release_user_upl_buffers(struct svga_context
*svga
)
138 nr
= svga
->curr
.num_vertex_buffers
;
140 for (i
= 0; i
< nr
; ++i
) {
141 struct pipe_vertex_buffer
*vb
= &svga
->curr
.vb
[i
];
143 if (vb
->buffer
&& svga_buffer_is_user_buffer(vb
->buffer
)) {
144 struct svga_buffer
*buffer
= svga_buffer(vb
->buffer
);
146 buffer
->source_offset
= 0;
147 if (buffer
->uploaded
.buffer
)
148 pipe_resource_reference(&buffer
->uploaded
.buffer
, NULL
);
155 static enum pipe_error
156 retry_draw_range_elements( struct svga_context
*svga
,
157 struct pipe_resource
*index_buffer
,
165 unsigned instance_count
,
168 enum pipe_error ret
= 0;
170 svga_hwtnl_set_unfilled( svga
->hwtnl
,
171 svga
->curr
.rast
->hw_unfilled
);
173 svga_hwtnl_set_flatshade( svga
->hwtnl
,
174 svga
->curr
.rast
->templ
.flatshade
,
175 svga
->curr
.rast
->templ
.flatshade_first
);
177 ret
= svga_upload_user_buffers( svga
, min_index
+ index_bias
,
178 max_index
- min_index
+ 1, instance_count
);
182 ret
= svga_update_state( svga
, SVGA_STATE_HW_DRAW
);
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
);
196 svga_context_flush( svga
, NULL
);
200 return retry_draw_range_elements( svga
,
201 index_buffer
, index_size
, index_bias
,
202 min_index
, max_index
,
204 instance_count
, FALSE
);
211 static enum pipe_error
212 retry_draw_arrays( struct svga_context
*svga
,
216 unsigned instance_count
,
221 svga_hwtnl_set_unfilled( svga
->hwtnl
,
222 svga
->curr
.rast
->hw_unfilled
);
224 svga_hwtnl_set_flatshade( svga
->hwtnl
,
225 svga
->curr
.rast
->templ
.flatshade
,
226 svga
->curr
.rast
->templ
.flatshade_first
);
228 ret
= svga_upload_user_buffers( svga
, start
, count
, instance_count
);
233 ret
= svga_update_state( svga
, SVGA_STATE_HW_DRAW
);
237 ret
= svga_hwtnl_draw_arrays( svga
->hwtnl
, prim
,
245 if (ret
== PIPE_ERROR_OUT_OF_MEMORY
&& do_retry
)
247 svga_context_flush( svga
, NULL
);
249 return retry_draw_arrays( svga
,
262 svga_draw_vbo(struct pipe_context
*pipe
, const struct pipe_draw_info
*info
)
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
;
270 if (!u_trim_pipe_prim( info
->mode
, &count
))
274 * Mark currently bound target surfaces as dirty
275 * doesn't really matter if it is done before drawing.
277 * TODO If we ever normaly return something other then
278 * true we should not mark it as dirty then.
280 svga_mark_surfaces_dirty(svga_context(pipe
));
282 if (svga
->curr
.reduced_prim
!= reduced_prim
) {
283 svga
->curr
.reduced_prim
= reduced_prim
;
284 svga
->dirty
|= SVGA_NEW_REDUCED_PRIMITIVE
;
287 needed_swtnl
= svga
->state
.sw
.need_swtnl
;
289 svga_update_state_retry( svga
, SVGA_STATE_NEED_SWTNL
);
292 if (svga
->curr
.vs
->base
.id
== svga
->debug
.disable_shader
||
293 svga
->curr
.fs
->base
.id
== svga
->debug
.disable_shader
)
297 if (svga
->state
.sw
.need_swtnl
) {
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.
307 svga_context_flush(svga
, NULL
);
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
);
315 if (info
->indexed
&& svga
->curr
.ib
.buffer
) {
318 assert(svga
->curr
.ib
.offset
% svga
->curr
.ib
.index_size
== 0);
319 offset
= svga
->curr
.ib
.offset
/ svga
->curr
.ib
.index_size
;
321 ret
= retry_draw_range_elements( svga
,
322 svga
->curr
.ib
.buffer
,
323 svga
->curr
.ib
.index_size
,
328 info
->start
+ offset
,
330 info
->instance_count
,
334 ret
= retry_draw_arrays( svga
,
338 info
->instance_count
,
343 svga_release_user_upl_buffers( svga
);
345 if (SVGA_DEBUG
& DEBUG_FLUSH
) {
346 svga_hwtnl_flush_retry( svga
);
347 svga_context_flush(svga
, NULL
);
352 void svga_init_draw_functions( struct svga_context
*svga
)
354 svga
->pipe
.draw_vbo
= svga_draw_vbo
;