svga: Fix index buffer uploads
[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
27 #include "util/u_helpers.h"
28 #include "util/u_inlines.h"
29 #include "util/u_prim.h"
30 #include "util/u_prim_restart.h"
31
32 #include "svga_context.h"
33 #include "svga_draw.h"
34 #include "svga_shader.h"
35 #include "svga_surface.h"
36 #include "svga_swtnl.h"
37 #include "svga_debug.h"
38 #include "svga_resource_buffer.h"
39
40 /* Returns TRUE if we are currently using flat shading.
41 */
42 static boolean
43 is_using_flat_shading(const struct svga_context *svga)
44 {
45 return
46 svga->state.hw_draw.fs ? svga->state.hw_draw.fs->uses_flat_interp : FALSE;
47 }
48
49
50 static enum pipe_error
51 retry_draw_range_elements(struct svga_context *svga,
52 const struct pipe_draw_info *info,
53 unsigned count)
54 {
55 enum pipe_error ret;
56
57 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_DRAWELEMENTS);
58
59 ret = svga_hwtnl_draw_range_elements(svga->hwtnl, info, count);
60 if (ret != PIPE_OK) {
61 svga_context_flush(svga, NULL);
62 ret = svga_hwtnl_draw_range_elements(svga->hwtnl, info, count);
63 }
64
65 assert (ret == PIPE_OK);
66 SVGA_STATS_TIME_POP(svga_sws(svga));
67 return ret;
68 }
69
70
71 static enum pipe_error
72 retry_draw_arrays(struct svga_context *svga,
73 enum pipe_prim_type prim, unsigned start, unsigned count,
74 unsigned start_instance, unsigned instance_count)
75 {
76 enum pipe_error ret;
77
78 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_DRAWARRAYS);
79
80 for (unsigned try = 0; try < 2; try++) {
81 ret = svga_hwtnl_draw_arrays(svga->hwtnl, prim, start, count,
82 start_instance, instance_count);
83 if (ret == PIPE_OK)
84 break;
85 svga_context_flush(svga, NULL);
86 }
87
88 SVGA_STATS_TIME_POP(svga_sws(svga));
89 return ret;
90 }
91
92
93 /**
94 * Determine if we need to implement primitive restart with a fallback
95 * path which breaks the original primitive into sub-primitive at the
96 * restart indexes.
97 */
98 static boolean
99 need_fallback_prim_restart(const struct svga_context *svga,
100 const struct pipe_draw_info *info)
101 {
102 if (info->primitive_restart && info->index_size) {
103 if (!svga_have_vgpu10(svga))
104 return TRUE;
105 else if (!svga->state.sw.need_swtnl) {
106 if (info->index_size == 1)
107 return TRUE; /* no device support for 1-byte indexes */
108 else if (info->index_size == 2)
109 return info->restart_index != 0xffff;
110 else
111 return info->restart_index != 0xffffffff;
112 }
113 }
114
115 return FALSE;
116 }
117
118
119 static void
120 svga_draw_vbo(struct pipe_context *pipe, const struct pipe_draw_info *info)
121 {
122 struct svga_context *svga = svga_context(pipe);
123 enum pipe_prim_type reduced_prim = u_reduced_prim(info->mode);
124 unsigned count = info->count;
125 enum pipe_error ret = 0;
126 boolean needed_swtnl;
127
128 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_DRAWVBO);
129
130 svga->hud.num_draw_calls++; /* for SVGA_QUERY_NUM_DRAW_CALLS */
131
132 if (u_reduced_prim(info->mode) == PIPE_PRIM_TRIANGLES &&
133 svga->curr.rast->templ.cull_face == PIPE_FACE_FRONT_AND_BACK)
134 goto done;
135
136 /*
137 * Mark currently bound target surfaces as dirty
138 * doesn't really matter if it is done before drawing.
139 *
140 * TODO If we ever normaly return something other then
141 * true we should not mark it as dirty then.
142 */
143 svga_mark_surfaces_dirty(svga_context(pipe));
144
145 if (svga->curr.reduced_prim != reduced_prim) {
146 svga->curr.reduced_prim = reduced_prim;
147 svga->dirty |= SVGA_NEW_REDUCED_PRIMITIVE;
148 }
149
150 if (need_fallback_prim_restart(svga, info)) {
151 enum pipe_error r;
152 r = util_draw_vbo_without_prim_restart(pipe, info);
153 assert(r == PIPE_OK);
154 (void) r;
155 goto done;
156 }
157
158 if (!u_trim_pipe_prim(info->mode, &count))
159 goto done;
160
161 needed_swtnl = svga->state.sw.need_swtnl;
162
163 svga_update_state_retry(svga, SVGA_STATE_NEED_SWTNL);
164
165 if (svga->state.sw.need_swtnl) {
166 svga->hud.num_fallbacks++; /* for SVGA_QUERY_NUM_FALLBACKS */
167 if (!needed_swtnl) {
168 /*
169 * We're switching from HW to SW TNL. SW TNL will require mapping all
170 * currently bound vertex buffers, some of which may already be
171 * referenced in the current command buffer as result of previous HW
172 * TNL. So flush now, to prevent the context to flush while a referred
173 * vertex buffer is mapped.
174 */
175
176 svga_context_flush(svga, NULL);
177 }
178
179 /* Avoid leaking the previous hwtnl bias to swtnl */
180 svga_hwtnl_set_index_bias(svga->hwtnl, 0);
181 ret = svga_swtnl_draw_vbo(svga, info);
182 }
183 else {
184 if (!svga_update_state_retry(svga, SVGA_STATE_HW_DRAW)) {
185 static const char *msg = "State update failed, skipping draw call";
186 debug_printf("%s\n", msg);
187 pipe_debug_message(&svga->debug.callback, INFO, "%s", msg);
188 goto done;
189 }
190 svga_hwtnl_set_fillmode(svga->hwtnl, svga->curr.rast->hw_fillmode);
191
192 /** determine if flatshade is to be used after svga_update_state()
193 * in case the fragment shader is changed.
194 */
195 svga_hwtnl_set_flatshade(svga->hwtnl,
196 svga->curr.rast->templ.flatshade ||
197 is_using_flat_shading(svga),
198 svga->curr.rast->templ.flatshade_first);
199
200 if (info->index_size) {
201 ret = retry_draw_range_elements(svga, info, count);
202 }
203 else {
204 ret = retry_draw_arrays(svga, info->mode, info->start, count,
205 info->start_instance, info->instance_count);
206 }
207 }
208
209 /* XXX: Silence warnings, do something sensible here? */
210 (void)ret;
211
212 if (SVGA_DEBUG & DEBUG_FLUSH) {
213 svga_hwtnl_flush_retry(svga);
214 svga_context_flush(svga, NULL);
215 }
216
217 done:
218 SVGA_STATS_TIME_POP(svga_sws(svga));
219 }
220
221
222 void
223 svga_init_draw_functions(struct svga_context *svga)
224 {
225 svga->pipe.draw_vbo = svga_draw_vbo;
226 }