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 **********************************************************/
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_upload_mgr.h"
33 #include "indices/u_indices.h"
35 #include "svga_hw_reg.h"
37 #include "svga_context.h"
38 #include "svga_screen.h"
39 #include "svga_draw.h"
40 #include "svga_shader.h"
41 #include "svga_state.h"
42 #include "svga_surface.h"
43 #include "svga_swtnl.h"
44 #include "svga_debug.h"
45 #include "svga_resource_buffer.h"
47 /* Returns TRUE if we are currently using flat shading.
50 is_using_flat_shading(const struct svga_context
*svga
)
53 svga
->state
.hw_draw
.fs
? svga
->state
.hw_draw
.fs
->uses_flat_interp
: FALSE
;
57 static enum pipe_error
58 retry_draw_range_elements( struct svga_context
*svga
,
59 struct pipe_resource
*index_buffer
,
64 enum pipe_prim_type prim
,
67 unsigned start_instance
,
68 unsigned instance_count
)
72 SVGA_STATS_TIME_PUSH(svga_sws(svga
), SVGA_STATS_TIME_DRAWELEMENTS
);
74 svga_hwtnl_set_fillmode(svga
->hwtnl
, svga
->curr
.rast
->hw_fillmode
);
76 /** determine if flatshade is to be used after svga_update_state()
77 * in case the fragment shader is changed.
79 svga_hwtnl_set_flatshade(svga
->hwtnl
,
80 svga
->curr
.rast
->templ
.flatshade
||
81 is_using_flat_shading(svga
),
82 svga
->curr
.rast
->templ
.flatshade_first
);
84 for (unsigned try = 0; try < 2; try++) {
85 ret
= svga_update_state(svga
, SVGA_STATE_HW_DRAW
);
87 ret
= svga_hwtnl_draw_range_elements(svga
->hwtnl
,
88 index_buffer
, index_size
,
92 start_instance
, instance_count
);
96 svga_context_flush(svga
, NULL
);
99 SVGA_STATS_TIME_POP(svga_sws(svga
));
104 static enum pipe_error
105 retry_draw_arrays( struct svga_context
*svga
,
106 enum pipe_prim_type prim
, unsigned start
, unsigned count
,
107 unsigned start_instance
, unsigned instance_count
)
111 SVGA_STATS_TIME_PUSH(svga_sws(svga
), SVGA_STATS_TIME_DRAWARRAYS
);
113 svga_hwtnl_set_fillmode(svga
->hwtnl
, svga
->curr
.rast
->hw_fillmode
);
115 /** determine if flatshade is to be used after svga_update_state()
116 * in case the fragment shader is changed.
118 svga_hwtnl_set_flatshade(svga
->hwtnl
,
119 svga
->curr
.rast
->templ
.flatshade
||
120 is_using_flat_shading(svga
),
121 svga
->curr
.rast
->templ
.flatshade_first
);
123 for (unsigned try = 0; try < 2; try++) {
124 ret
= svga_update_state( svga
, SVGA_STATE_HW_DRAW
);
125 if (ret
== PIPE_OK
) {
126 ret
= svga_hwtnl_draw_arrays(svga
->hwtnl
, prim
, start
, count
,
127 start_instance
, instance_count
);
131 svga_context_flush(svga
, NULL
);
134 SVGA_STATS_TIME_POP(svga_sws(svga
));
140 * Determine if we need to implement primitive restart with a fallback
141 * path which breaks the original primitive into sub-primitive at the
145 need_fallback_prim_restart(const struct svga_context
*svga
,
146 const struct pipe_draw_info
*info
)
148 if (info
->primitive_restart
&& info
->index_size
) {
149 if (!svga_have_vgpu10(svga
))
151 else if (!svga
->state
.sw
.need_swtnl
) {
152 if (info
->index_size
== 1)
153 return TRUE
; /* no device support for 1-byte indexes */
154 else if (info
->index_size
== 2)
155 return info
->restart_index
!= 0xffff;
157 return info
->restart_index
!= 0xffffffff;
166 svga_draw_vbo(struct pipe_context
*pipe
, const struct pipe_draw_info
*info
)
168 struct svga_context
*svga
= svga_context( pipe
);
169 enum pipe_prim_type reduced_prim
= u_reduced_prim( info
->mode
);
170 unsigned count
= info
->count
;
171 enum pipe_error ret
= 0;
172 boolean needed_swtnl
;
173 struct pipe_resource
*indexbuf
=
174 info
->has_user_indices
? NULL
: info
->index
.resource
;
176 SVGA_STATS_TIME_PUSH(svga_sws(svga
), SVGA_STATS_TIME_DRAWVBO
);
178 svga
->hud
.num_draw_calls
++; /* for SVGA_QUERY_NUM_DRAW_CALLS */
180 if (u_reduced_prim(info
->mode
) == PIPE_PRIM_TRIANGLES
&&
181 svga
->curr
.rast
->templ
.cull_face
== PIPE_FACE_FRONT_AND_BACK
)
184 /* Upload a user index buffer. */
185 unsigned index_offset
= 0;
186 if (info
->index_size
&& info
->has_user_indices
&&
187 !util_upload_index_buffer(pipe
, info
, &indexbuf
, &index_offset
)) {
192 * Mark currently bound target surfaces as dirty
193 * doesn't really matter if it is done before drawing.
195 * TODO If we ever normaly return something other then
196 * true we should not mark it as dirty then.
198 svga_mark_surfaces_dirty(svga_context(pipe
));
200 if (svga
->curr
.reduced_prim
!= reduced_prim
) {
201 svga
->curr
.reduced_prim
= reduced_prim
;
202 svga
->dirty
|= SVGA_NEW_REDUCED_PRIMITIVE
;
205 if (need_fallback_prim_restart(svga
, info
)) {
207 r
= util_draw_vbo_without_prim_restart(pipe
, info
);
208 assert(r
== PIPE_OK
);
213 if (!u_trim_pipe_prim( info
->mode
, &count
))
216 needed_swtnl
= svga
->state
.sw
.need_swtnl
;
218 svga_update_state_retry( svga
, SVGA_STATE_NEED_SWTNL
);
220 if (svga
->state
.sw
.need_swtnl
) {
221 svga
->hud
.num_fallbacks
++; /* for SVGA_QUERY_NUM_FALLBACKS */
224 * We're switching from HW to SW TNL. SW TNL will require mapping all
225 * currently bound vertex buffers, some of which may already be
226 * referenced in the current command buffer as result of previous HW
227 * TNL. So flush now, to prevent the context to flush while a referred
228 * vertex buffer is mapped.
231 svga_context_flush(svga
, NULL
);
234 /* Avoid leaking the previous hwtnl bias to swtnl */
235 svga_hwtnl_set_index_bias( svga
->hwtnl
, 0 );
236 ret
= svga_swtnl_draw_vbo(svga
, info
, indexbuf
, index_offset
);
239 if (info
->index_size
&& indexbuf
) {
242 assert(index_offset
% info
->index_size
== 0);
243 offset
= index_offset
/ info
->index_size
;
245 ret
= retry_draw_range_elements( svga
,
252 info
->start
+ offset
,
254 info
->start_instance
,
255 info
->instance_count
);
258 ret
= retry_draw_arrays(svga
, info
->mode
, info
->start
, count
,
259 info
->start_instance
, info
->instance_count
);
263 /* XXX: Silence warnings, do something sensible here? */
266 if (SVGA_DEBUG
& DEBUG_FLUSH
) {
267 svga_hwtnl_flush_retry( svga
);
268 svga_context_flush(svga
, NULL
);
272 if (info
->index_size
&& info
->index
.resource
!= indexbuf
)
273 pipe_resource_reference(&indexbuf
, NULL
);
274 SVGA_STATS_TIME_POP(svga_sws(svga
));
278 void svga_init_draw_functions( struct svga_context
*svga
)
280 svga
->pipe
.draw_vbo
= svga_draw_vbo
;