gallium: add PIPE_SHADER_CAP_GLSL_16BIT_TEMPS for LowerPrecisionTemporaries
[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_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"
33
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"
43
44
45 static enum pipe_error
46 retry_draw_range_elements(struct svga_context *svga,
47 const struct pipe_draw_info *info,
48 unsigned count)
49 {
50 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_DRAWELEMENTS);
51
52 SVGA_RETRY(svga, svga_hwtnl_draw_range_elements(svga->hwtnl, info, count));
53
54 SVGA_STATS_TIME_POP(svga_sws(svga));
55 return PIPE_OK;
56 }
57
58
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)
64 {
65 enum pipe_error ret;
66
67 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_DRAWARRAYS);
68
69 SVGA_RETRY_OOM(svga, ret, svga_hwtnl_draw_arrays(svga->hwtnl, prim, start,
70 count, start_instance,
71 instance_count,
72 vertices_per_patch));
73 SVGA_STATS_TIME_POP(svga_sws(svga));
74 return ret;
75 }
76
77
78 /**
79 * Auto draw (get vertex count from a transform feedback result).
80 */
81 static enum pipe_error
82 retry_draw_auto(struct svga_context *svga,
83 const struct pipe_draw_info *info)
84 {
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);
92
93 if (info->mode == PIPE_PRIM_LINE_LOOP) {
94 /* XXX need to do a fallback */
95 assert(!"draw auto fallback not supported yet");
96 return PIPE_OK;
97 }
98 else {
99 SVGA3dPrimitiveRange range;
100 unsigned hw_count;
101
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;
109 range.indexBias = 0;
110
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));
121
122 return PIPE_OK;
123 }
124 }
125
126
127 /**
128 * Indirect draw (get vertex count, start index, etc. from a buffer object.
129 */
130 static enum pipe_error
131 retry_draw_indirect(struct svga_context *svga,
132 const struct pipe_draw_info *info)
133 {
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);
140
141 if (info->mode == PIPE_PRIM_LINE_LOOP) {
142 /* need to do a fallback */
143 util_draw_indirect(&svga->pipe, info);
144 return PIPE_OK;
145 }
146 else {
147 SVGA3dPrimitiveRange range;
148 unsigned hw_count;
149
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 */
158
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 */
167 info->indirect,
168 NULL)); /* SO vertex count */
169
170 return PIPE_OK;
171 }
172 }
173
174
175 /**
176 * Determine if we need to implement primitive restart with a fallback
177 * path which breaks the original primitive into sub-primitive at the
178 * restart indexes.
179 */
180 static boolean
181 need_fallback_prim_restart(const struct svga_context *svga,
182 const struct pipe_draw_info *info)
183 {
184 if (info->primitive_restart && info->index_size) {
185 if (!svga_have_vgpu10(svga))
186 return TRUE;
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;
192 else
193 return info->restart_index != 0xffffffff;
194 }
195 }
196
197 return FALSE;
198 }
199
200
201 /**
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.
204 */
205 static unsigned
206 get_vcount_from_stream_output(struct svga_context *svga,
207 const struct pipe_draw_info *info,
208 unsigned stream)
209 {
210 unsigned primcount;
211 primcount = svga_get_primcount_from_stream_output(svga, stream);
212 return u_vertices_for_prims(info->mode, primcount);
213 }
214
215
216 static void
217 svga_draw_vbo(struct pipe_context *pipe, const struct pipe_draw_info *info)
218 {
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;
224
225 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_DRAWVBO);
226
227 svga->hud.num_draw_calls++; /* for SVGA_QUERY_NUM_DRAW_CALLS */
228
229 if (u_reduced_prim(info->mode) == PIPE_PRIM_TRIANGLES &&
230 svga->curr.rast->templ.cull_face == PIPE_FACE_FRONT_AND_BACK)
231 goto done;
232
233 /*
234 * Mark currently bound target surfaces as dirty
235 * doesn't really matter if it is done before drawing.
236 *
237 * TODO If we ever normaly return something other then
238 * true we should not mark it as dirty then.
239 */
240 svga_mark_surfaces_dirty(svga_context(pipe));
241
242 if (svga->curr.reduced_prim != reduced_prim) {
243 svga->curr.reduced_prim = reduced_prim;
244 svga->dirty |= SVGA_NEW_REDUCED_PRIMITIVE;
245 }
246
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
249 * DrawIndexed.
250 */
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;
254 }
255
256 if (svga->curr.vertices_per_patch != info->vertices_per_patch) {
257 svga->curr.vertices_per_patch = info->vertices_per_patch;
258
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.
263 */
264 if (svga->curr.tcs || svga->curr.tes)
265 svga->dirty |= SVGA_NEW_TCS_PARAM;
266 }
267
268 if (need_fallback_prim_restart(svga, info)) {
269 enum pipe_error r;
270 r = util_draw_vbo_without_prim_restart(pipe, info);
271 assert(r == PIPE_OK);
272 (void) r;
273 goto done;
274 }
275
276 if (!info->indirect && !info->count_from_stream_output &&
277 !u_trim_pipe_prim(info->mode, &count))
278 goto done;
279
280 needed_swtnl = svga->state.sw.need_swtnl;
281
282 svga_update_state_retry(svga, SVGA_STATE_NEED_SWTNL);
283
284 if (svga->state.sw.need_swtnl) {
285 svga->hud.num_fallbacks++; /* for SVGA_QUERY_NUM_FALLBACKS */
286 if (!needed_swtnl) {
287 /*
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.
293 */
294
295 svga_context_flush(svga, NULL);
296 }
297
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);
301 }
302 else {
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);
307 goto done;
308 }
309 svga_hwtnl_set_fillmode(svga->hwtnl, svga->curr.rast->hw_fillmode);
310
311 svga_update_state_retry(svga, SVGA_STATE_HW_DRAW);
312
313 /** determine if flatshade is to be used after svga_update_state()
314 * in case the fragment shader is changed.
315 */
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);
320
321 if (info->count_from_stream_output) {
322 unsigned stream = 0;
323 assert(count == 0);
324
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.
331 */
332
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;
337 break;
338 }
339 }
340 if (info->instance_count > 1 || stream > 0) {
341 count = get_vcount_from_stream_output(svga, info, stream);
342 }
343 }
344
345 if (info->count_from_stream_output && count == 0) {
346 ret = retry_draw_auto(svga, info);
347 }
348 else if (info->indirect) {
349 ret = retry_draw_indirect(svga, info);
350 }
351 else if (info->index_size) {
352 ret = retry_draw_range_elements(svga, info, count);
353 }
354 else {
355 ret = retry_draw_arrays(svga, info->mode, info->start, count,
356 info->start_instance, info->instance_count,
357 info->vertices_per_patch);
358 }
359 }
360
361 /* XXX: Silence warnings, do something sensible here? */
362 (void)ret;
363
364 if (SVGA_DEBUG & DEBUG_FLUSH) {
365 svga_hwtnl_flush_retry(svga);
366 svga_context_flush(svga, NULL);
367 }
368
369 done:
370 SVGA_STATS_TIME_POP(svga_sws(svga));
371 }
372
373
374 void
375 svga_init_draw_functions(struct svga_context *svga)
376 {
377 svga->pipe.draw_vbo = svga_draw_vbo;
378 }