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_draw.h"
28 #include "util/format/u_format.h"
29 #include "util/u_helpers.h"
30 #include "util/u_inlines.h"
31 #include "util/u_prim.h"
32 #include "util/u_prim_restart.h"
34 #include "svga_context.h"
35 #include "svga_draw_private.h"
36 #include "svga_screen.h"
37 #include "svga_draw.h"
38 #include "svga_shader.h"
39 #include "svga_surface.h"
40 #include "svga_swtnl.h"
41 #include "svga_debug.h"
42 #include "svga_resource_buffer.h"
45 static enum pipe_error
46 retry_draw_range_elements(struct svga_context
*svga
,
47 const struct pipe_draw_info
*info
,
50 SVGA_STATS_TIME_PUSH(svga_sws(svga
), SVGA_STATS_TIME_DRAWELEMENTS
);
52 SVGA_RETRY(svga
, svga_hwtnl_draw_range_elements(svga
->hwtnl
, info
, count
));
54 SVGA_STATS_TIME_POP(svga_sws(svga
));
59 static enum pipe_error
60 retry_draw_arrays( struct svga_context
*svga
,
61 enum pipe_prim_type prim
, unsigned start
, unsigned count
,
62 unsigned start_instance
, unsigned instance_count
,
63 ubyte vertices_per_patch
)
67 SVGA_STATS_TIME_PUSH(svga_sws(svga
), SVGA_STATS_TIME_DRAWARRAYS
);
69 SVGA_RETRY_OOM(svga
, ret
, svga_hwtnl_draw_arrays(svga
->hwtnl
, prim
, start
,
70 count
, start_instance
,
73 SVGA_STATS_TIME_POP(svga_sws(svga
));
79 * Auto draw (get vertex count from a transform feedback result).
81 static enum pipe_error
82 retry_draw_auto(struct svga_context
*svga
,
83 const struct pipe_draw_info
*info
)
85 assert(svga_have_sm5(svga
));
86 assert(info
->count_from_stream_output
);
87 assert(info
->instance_count
== 1);
88 /* SO drawing implies core profile and none of these prim types */
89 assert(info
->mode
!= PIPE_PRIM_QUADS
&&
90 info
->mode
!= PIPE_PRIM_QUAD_STRIP
&&
91 info
->mode
!= PIPE_PRIM_POLYGON
);
93 if (info
->mode
== PIPE_PRIM_LINE_LOOP
) {
94 /* XXX need to do a fallback */
95 assert(!"draw auto fallback not supported yet");
99 SVGA3dPrimitiveRange range
;
102 range
.primType
= svga_translate_prim(info
->mode
, 12, &hw_count
,
103 info
->vertices_per_patch
);
104 range
.primitiveCount
= 0;
105 range
.indexArray
.surfaceId
= SVGA3D_INVALID_ID
;
106 range
.indexArray
.offset
= 0;
107 range
.indexArray
.stride
= 0;
108 range
.indexWidth
= 0;
111 SVGA_RETRY(svga
, svga_hwtnl_prim
112 (svga
->hwtnl
, &range
,
113 0, /* vertex count comes from SO buffer */
114 0, /* don't know min index */
115 ~0u, /* don't know max index */
116 NULL
, /* no index buffer */
117 0, /* start instance */
118 1, /* only 1 instance supported */
119 NULL
, /* indirect drawing info */
120 info
->count_from_stream_output
));
128 * Indirect draw (get vertex count, start index, etc. from a buffer object.
130 static enum pipe_error
131 retry_draw_indirect(struct svga_context
*svga
,
132 const struct pipe_draw_info
*info
)
134 assert(svga_have_sm5(svga
));
135 assert(info
->indirect
);
136 /* indirect drawing implies core profile and none of these prim types */
137 assert(info
->mode
!= PIPE_PRIM_QUADS
&&
138 info
->mode
!= PIPE_PRIM_QUAD_STRIP
&&
139 info
->mode
!= PIPE_PRIM_POLYGON
);
141 if (info
->mode
== PIPE_PRIM_LINE_LOOP
) {
142 /* need to do a fallback */
143 util_draw_indirect(&svga
->pipe
, info
);
147 SVGA3dPrimitiveRange range
;
150 range
.primType
= svga_translate_prim(info
->mode
, 12, &hw_count
,
151 info
->vertices_per_patch
);
152 range
.primitiveCount
= 0; /* specified in indirect buffer */
153 range
.indexArray
.surfaceId
= SVGA3D_INVALID_ID
;
154 range
.indexArray
.offset
= 0;
155 range
.indexArray
.stride
= 0;
156 range
.indexWidth
= info
->index_size
;
157 range
.indexBias
= 0; /* specified in indirect buffer */
159 SVGA_RETRY(svga
, svga_hwtnl_prim
160 (svga
->hwtnl
, &range
,
161 0, /* vertex count is in indirect buffer */
162 0, /* don't know min index */
163 ~0u, /* don't know max index */
164 info
->index
.resource
,
165 info
->start_instance
,
166 0, /* don't know instance count */
168 NULL
)); /* SO vertex count */
176 * Determine if we need to implement primitive restart with a fallback
177 * path which breaks the original primitive into sub-primitive at the
181 need_fallback_prim_restart(const struct svga_context
*svga
,
182 const struct pipe_draw_info
*info
)
184 if (info
->primitive_restart
&& info
->index_size
) {
185 if (!svga_have_vgpu10(svga
))
187 else if (!svga
->state
.sw
.need_swtnl
) {
188 if (info
->index_size
== 1)
189 return TRUE
; /* no device support for 1-byte indexes */
190 else if (info
->index_size
== 2)
191 return info
->restart_index
!= 0xffff;
193 return info
->restart_index
!= 0xffffffff;
202 * A helper function to return the vertex count from the primitive count
203 * returned from the stream output statistics query for the specified stream.
206 get_vcount_from_stream_output(struct svga_context
*svga
,
207 const struct pipe_draw_info
*info
,
211 primcount
= svga_get_primcount_from_stream_output(svga
, stream
);
212 return u_vertices_for_prims(info
->mode
, primcount
);
217 svga_draw_vbo(struct pipe_context
*pipe
, const struct pipe_draw_info
*info
)
219 struct svga_context
*svga
= svga_context(pipe
);
220 enum pipe_prim_type reduced_prim
= u_reduced_prim(info
->mode
);
221 unsigned count
= info
->count
;
222 enum pipe_error ret
= 0;
223 boolean needed_swtnl
;
225 SVGA_STATS_TIME_PUSH(svga_sws(svga
), SVGA_STATS_TIME_DRAWVBO
);
227 svga
->hud
.num_draw_calls
++; /* for SVGA_QUERY_NUM_DRAW_CALLS */
229 if (u_reduced_prim(info
->mode
) == PIPE_PRIM_TRIANGLES
&&
230 svga
->curr
.rast
->templ
.cull_face
== PIPE_FACE_FRONT_AND_BACK
)
234 * Mark currently bound target surfaces as dirty
235 * doesn't really matter if it is done before drawing.
237 * TODO If we ever normaly return something other then
238 * true we should not mark it as dirty then.
240 svga_mark_surfaces_dirty(svga_context(pipe
));
242 if (svga
->curr
.reduced_prim
!= reduced_prim
) {
243 svga
->curr
.reduced_prim
= reduced_prim
;
244 svga
->dirty
|= SVGA_NEW_REDUCED_PRIMITIVE
;
247 /* We need to adjust the vertexID in the vertex shader since SV_VertexID
248 * always start from 0 for DrawArrays and does not include baseVertex for
251 if (svga
->curr
.vertex_id_bias
!= (info
->start
+ info
->index_bias
)) {
252 svga
->curr
.vertex_id_bias
= info
->start
+ info
->index_bias
;
253 svga
->dirty
|= SVGA_NEW_VS_CONSTS
;
256 if (svga
->curr
.vertices_per_patch
!= info
->vertices_per_patch
) {
257 svga
->curr
.vertices_per_patch
= info
->vertices_per_patch
;
259 /* If input patch size changes, we need to notifiy the TCS
260 * code to reevaluate the shader variant since the
261 * vertices per patch count is a constant in the control
262 * point count declaration.
264 if (svga
->curr
.tcs
|| svga
->curr
.tes
)
265 svga
->dirty
|= SVGA_NEW_TCS_PARAM
;
268 if (need_fallback_prim_restart(svga
, info
)) {
270 r
= util_draw_vbo_without_prim_restart(pipe
, info
);
271 assert(r
== PIPE_OK
);
276 if (!info
->indirect
&& !info
->count_from_stream_output
&&
277 !u_trim_pipe_prim(info
->mode
, &count
))
280 needed_swtnl
= svga
->state
.sw
.need_swtnl
;
282 svga_update_state_retry(svga
, SVGA_STATE_NEED_SWTNL
);
284 if (svga
->state
.sw
.need_swtnl
) {
285 svga
->hud
.num_fallbacks
++; /* for SVGA_QUERY_NUM_FALLBACKS */
288 * We're switching from HW to SW TNL. SW TNL will require mapping all
289 * currently bound vertex buffers, some of which may already be
290 * referenced in the current command buffer as result of previous HW
291 * TNL. So flush now, to prevent the context to flush while a referred
292 * vertex buffer is mapped.
295 svga_context_flush(svga
, NULL
);
298 /* Avoid leaking the previous hwtnl bias to swtnl */
299 svga_hwtnl_set_index_bias(svga
->hwtnl
, 0);
300 ret
= svga_swtnl_draw_vbo(svga
, info
);
303 if (!svga_update_state_retry(svga
, SVGA_STATE_HW_DRAW
)) {
304 static const char *msg
= "State update failed, skipping draw call";
305 debug_printf("%s\n", msg
);
306 pipe_debug_message(&svga
->debug
.callback
, INFO
, "%s", msg
);
309 svga_hwtnl_set_fillmode(svga
->hwtnl
, svga
->curr
.rast
->hw_fillmode
);
311 svga_update_state_retry(svga
, SVGA_STATE_HW_DRAW
);
313 /** determine if flatshade is to be used after svga_update_state()
314 * in case the fragment shader is changed.
316 svga_hwtnl_set_flatshade(svga
->hwtnl
,
317 svga
->curr
.rast
->templ
.flatshade
||
318 svga_is_using_flat_shading(svga
),
319 svga
->curr
.rast
->templ
.flatshade_first
);
321 if (info
->count_from_stream_output
) {
325 /* If the vertex count is from the stream output of a non-zero stream
326 * or the draw info specifies instancing, we will need a workaround
327 * since the draw_auto command does not support stream instancing.
328 * The workaround requires querying the vertex count from the
329 * stream output statistics query for the specified stream and then
330 * fallback to the regular draw function.
333 /* Check the stream index of the specified stream output target */
334 for (unsigned i
= 0; i
< ARRAY_SIZE(svga
->so_targets
); i
++) {
335 if (svga
->vcount_so_targets
[i
] == info
->count_from_stream_output
) {
336 stream
= (svga
->vcount_buffer_stream
>> (i
* 4)) & 0xf;
340 if (info
->instance_count
> 1 || stream
> 0) {
341 count
= get_vcount_from_stream_output(svga
, info
, stream
);
345 if (info
->count_from_stream_output
&& count
== 0) {
346 ret
= retry_draw_auto(svga
, info
);
348 else if (info
->indirect
) {
349 ret
= retry_draw_indirect(svga
, info
);
351 else if (info
->index_size
) {
352 ret
= retry_draw_range_elements(svga
, info
, count
);
355 ret
= retry_draw_arrays(svga
, info
->mode
, info
->start
, count
,
356 info
->start_instance
, info
->instance_count
,
357 info
->vertices_per_patch
);
361 /* XXX: Silence warnings, do something sensible here? */
364 if (SVGA_DEBUG
& DEBUG_FLUSH
) {
365 svga_hwtnl_flush_retry(svga
);
366 svga_context_flush(svga
, NULL
);
370 SVGA_STATS_TIME_POP(svga_sws(svga
));
375 svga_init_draw_functions(struct svga_context
*svga
)
377 svga
->pipe
.draw_vbo
= svga_draw_vbo
;