svga: remove unneeded #includes in svga_pipe_draw.c
[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 struct pipe_resource *index_buffer,
53 unsigned index_size,
54 int index_bias,
55 unsigned min_index,
56 unsigned max_index,
57 enum pipe_prim_type prim,
58 unsigned start,
59 unsigned count,
60 unsigned start_instance,
61 unsigned instance_count)
62 {
63 enum pipe_error ret;
64
65 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_DRAWELEMENTS);
66
67 svga_hwtnl_set_fillmode(svga->hwtnl, svga->curr.rast->hw_fillmode);
68
69 /** determine if flatshade is to be used after svga_update_state()
70 * in case the fragment shader is changed.
71 */
72 svga_hwtnl_set_flatshade(svga->hwtnl,
73 svga->curr.rast->templ.flatshade ||
74 is_using_flat_shading(svga),
75 svga->curr.rast->templ.flatshade_first);
76
77 for (unsigned try = 0; try < 2; try++) {
78 ret = svga_update_state(svga, SVGA_STATE_HW_DRAW);
79 if (ret == PIPE_OK) {
80 ret = svga_hwtnl_draw_range_elements(svga->hwtnl,
81 index_buffer, index_size,
82 index_bias,
83 min_index, max_index,
84 prim, start, count,
85 start_instance, instance_count);
86 if (ret == PIPE_OK)
87 break;
88 }
89 svga_context_flush(svga, NULL);
90 }
91
92 SVGA_STATS_TIME_POP(svga_sws(svga));
93 return ret;
94 }
95
96
97 static enum pipe_error
98 retry_draw_arrays(struct svga_context *svga,
99 enum pipe_prim_type prim, unsigned start, unsigned count,
100 unsigned start_instance, unsigned instance_count)
101 {
102 enum pipe_error ret;
103
104 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_DRAWARRAYS);
105
106 svga_hwtnl_set_fillmode(svga->hwtnl, svga->curr.rast->hw_fillmode);
107
108 /** determine if flatshade is to be used after svga_update_state()
109 * in case the fragment shader is changed.
110 */
111 svga_hwtnl_set_flatshade(svga->hwtnl,
112 svga->curr.rast->templ.flatshade ||
113 is_using_flat_shading(svga),
114 svga->curr.rast->templ.flatshade_first);
115
116 for (unsigned try = 0; try < 2; try++) {
117 ret = svga_update_state(svga, SVGA_STATE_HW_DRAW);
118 if (ret == PIPE_OK) {
119 ret = svga_hwtnl_draw_arrays(svga->hwtnl, prim, start, count,
120 start_instance, instance_count);
121 if (ret == PIPE_OK)
122 break;
123 }
124 svga_context_flush(svga, NULL);
125 }
126
127 SVGA_STATS_TIME_POP(svga_sws(svga));
128 return ret;
129 }
130
131
132 /**
133 * Determine if we need to implement primitive restart with a fallback
134 * path which breaks the original primitive into sub-primitive at the
135 * restart indexes.
136 */
137 static boolean
138 need_fallback_prim_restart(const struct svga_context *svga,
139 const struct pipe_draw_info *info)
140 {
141 if (info->primitive_restart && info->index_size) {
142 if (!svga_have_vgpu10(svga))
143 return TRUE;
144 else if (!svga->state.sw.need_swtnl) {
145 if (info->index_size == 1)
146 return TRUE; /* no device support for 1-byte indexes */
147 else if (info->index_size == 2)
148 return info->restart_index != 0xffff;
149 else
150 return info->restart_index != 0xffffffff;
151 }
152 }
153
154 return FALSE;
155 }
156
157
158 static void
159 svga_draw_vbo(struct pipe_context *pipe, const struct pipe_draw_info *info)
160 {
161 struct svga_context *svga = svga_context(pipe);
162 enum pipe_prim_type reduced_prim = u_reduced_prim(info->mode);
163 unsigned count = info->count;
164 enum pipe_error ret = 0;
165 boolean needed_swtnl;
166 struct pipe_resource *indexbuf =
167 info->has_user_indices ? NULL : info->index.resource;
168
169 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_DRAWVBO);
170
171 svga->hud.num_draw_calls++; /* for SVGA_QUERY_NUM_DRAW_CALLS */
172
173 if (u_reduced_prim(info->mode) == PIPE_PRIM_TRIANGLES &&
174 svga->curr.rast->templ.cull_face == PIPE_FACE_FRONT_AND_BACK)
175 goto done;
176
177 /* Upload a user index buffer. */
178 unsigned index_offset = 0;
179 if (info->index_size && info->has_user_indices &&
180 !util_upload_index_buffer(pipe, info, &indexbuf, &index_offset)) {
181 goto done;
182 }
183
184 /*
185 * Mark currently bound target surfaces as dirty
186 * doesn't really matter if it is done before drawing.
187 *
188 * TODO If we ever normaly return something other then
189 * true we should not mark it as dirty then.
190 */
191 svga_mark_surfaces_dirty(svga_context(pipe));
192
193 if (svga->curr.reduced_prim != reduced_prim) {
194 svga->curr.reduced_prim = reduced_prim;
195 svga->dirty |= SVGA_NEW_REDUCED_PRIMITIVE;
196 }
197
198 if (need_fallback_prim_restart(svga, info)) {
199 enum pipe_error r;
200 r = util_draw_vbo_without_prim_restart(pipe, info);
201 assert(r == PIPE_OK);
202 (void) r;
203 goto done;
204 }
205
206 if (!u_trim_pipe_prim(info->mode, &count))
207 goto done;
208
209 needed_swtnl = svga->state.sw.need_swtnl;
210
211 svga_update_state_retry(svga, SVGA_STATE_NEED_SWTNL);
212
213 if (svga->state.sw.need_swtnl) {
214 svga->hud.num_fallbacks++; /* for SVGA_QUERY_NUM_FALLBACKS */
215 if (!needed_swtnl) {
216 /*
217 * We're switching from HW to SW TNL. SW TNL will require mapping all
218 * currently bound vertex buffers, some of which may already be
219 * referenced in the current command buffer as result of previous HW
220 * TNL. So flush now, to prevent the context to flush while a referred
221 * vertex buffer is mapped.
222 */
223
224 svga_context_flush(svga, NULL);
225 }
226
227 /* Avoid leaking the previous hwtnl bias to swtnl */
228 svga_hwtnl_set_index_bias(svga->hwtnl, 0);
229 ret = svga_swtnl_draw_vbo(svga, info, indexbuf, index_offset);
230 }
231 else {
232 if (info->index_size && indexbuf) {
233 unsigned offset;
234
235 assert(index_offset % info->index_size == 0);
236 offset = index_offset / info->index_size;
237
238 ret = retry_draw_range_elements(svga,
239 indexbuf,
240 info->index_size,
241 info->index_bias,
242 info->min_index,
243 info->max_index,
244 info->mode,
245 info->start + offset,
246 count,
247 info->start_instance,
248 info->instance_count);
249 }
250 else {
251 ret = retry_draw_arrays(svga, info->mode, info->start, count,
252 info->start_instance, info->instance_count);
253 }
254 }
255
256 /* XXX: Silence warnings, do something sensible here? */
257 (void)ret;
258
259 if (SVGA_DEBUG & DEBUG_FLUSH) {
260 svga_hwtnl_flush_retry(svga);
261 svga_context_flush(svga, NULL);
262 }
263
264 done:
265 if (info->index_size && info->index.resource != indexbuf)
266 pipe_resource_reference(&indexbuf, NULL);
267 SVGA_STATS_TIME_POP(svga_sws(svga));
268 }
269
270
271 void
272 svga_init_draw_functions(struct svga_context *svga)
273 {
274 svga->pipe.draw_vbo = svga_draw_vbo;
275 }