1 /*
2 * Copyright (c) 2008-2024 Broadcom. All Rights Reserved.
3 * The term “Broadcom” refers to Broadcom Inc.
4 * and/or its subsidiaries.
5 * SPDX-License-Identifier: MIT
6 */
7
8
9 #include "util/u_draw.h"
10 #include "util/format/u_format.h"
11 #include "util/u_helpers.h"
12 #include "util/u_inlines.h"
13 #include "util/u_prim.h"
14 #include "util/u_prim_restart.h"
15
16 #include "svga_context.h"
17 #include "svga_draw_private.h"
18 #include "svga_screen.h"
19 #include "svga_draw.h"
20 #include "svga_shader.h"
21 #include "svga_surface.h"
22 #include "svga_swtnl.h"
23 #include "svga_debug.h"
24 #include "svga_resource_buffer.h"
25
26
27 static enum pipe_error
retry_draw_range_elements(struct svga_context * svga,const struct pipe_draw_info * info,const struct pipe_draw_start_count_bias * draw,unsigned count)28 retry_draw_range_elements(struct svga_context *svga,
29 const struct pipe_draw_info *info,
30 const struct pipe_draw_start_count_bias *draw,
31 unsigned count)
32 {
33 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_DRAWELEMENTS);
34
35 SVGA_RETRY(svga, svga_hwtnl_draw_range_elements(svga->hwtnl, info, draw, count));
36
37 SVGA_STATS_TIME_POP(svga_sws(svga));
38 return PIPE_OK;
39 }
40
41
42 static enum pipe_error
retry_draw_arrays(struct svga_context * svga,enum mesa_prim prim,unsigned start,unsigned count,unsigned start_instance,unsigned instance_count,uint8_t vertices_per_patch)43 retry_draw_arrays( struct svga_context *svga,
44 enum mesa_prim prim, unsigned start, unsigned count,
45 unsigned start_instance, unsigned instance_count,
46 uint8_t vertices_per_patch)
47 {
48 enum pipe_error ret;
49
50 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_DRAWARRAYS);
51
52 SVGA_RETRY_OOM(svga, ret, svga_hwtnl_draw_arrays(svga->hwtnl, prim, start,
53 count, start_instance,
54 instance_count,
55 vertices_per_patch));
56 SVGA_STATS_TIME_POP(svga_sws(svga));
57 return ret;
58 }
59
60
61 /**
62 * Auto draw (get vertex count from a transform feedback result).
63 */
64 static enum pipe_error
retry_draw_auto(struct svga_context * svga,const struct pipe_draw_info * info,const struct pipe_draw_indirect_info * indirect)65 retry_draw_auto(struct svga_context *svga,
66 const struct pipe_draw_info *info,
67 const struct pipe_draw_indirect_info *indirect)
68 {
69 assert(svga_have_sm5(svga));
70 assert(indirect->count_from_stream_output);
71 assert(info->instance_count == 1);
72 /* SO drawing implies core profile and none of these prim types */
73 assert(info->mode != MESA_PRIM_QUADS &&
74 info->mode != MESA_PRIM_QUAD_STRIP &&
75 info->mode != MESA_PRIM_POLYGON);
76
77 if (info->mode == MESA_PRIM_LINE_LOOP) {
78 /* XXX need to do a fallback */
79 assert(!"draw auto fallback not supported yet");
80 return PIPE_OK;
81 }
82 else {
83 SVGA3dPrimitiveRange range;
84 unsigned hw_count;
85
86 range.primType = svga_translate_prim(info->mode, 12, &hw_count,
87 svga->patch_vertices);
88 range.primitiveCount = 0;
89 range.indexArray.surfaceId = SVGA3D_INVALID_ID;
90 range.indexArray.offset = 0;
91 range.indexArray.stride = 0;
92 range.indexWidth = 0;
93 range.indexBias = 0;
94
95 SVGA_RETRY(svga, svga_hwtnl_prim
96 (svga->hwtnl, &range,
97 0, /* vertex count comes from SO buffer */
98 0, /* don't know min index */
99 ~0u, /* don't know max index */
100 NULL, /* no index buffer */
101 0, /* start instance */
102 1, /* only 1 instance supported */
103 NULL, /* indirect drawing info */
104 indirect->count_from_stream_output));
105
106 return PIPE_OK;
107 }
108 }
109
110
111 /**
112 * Indirect draw (get vertex count, start index, etc. from a buffer object.
113 */
114 static enum pipe_error
retry_draw_indirect(struct svga_context * svga,const struct pipe_draw_info * info,const struct pipe_draw_indirect_info * indirect)115 retry_draw_indirect(struct svga_context *svga,
116 const struct pipe_draw_info *info,
117 const struct pipe_draw_indirect_info *indirect)
118 {
119 assert(svga_have_sm5(svga));
120 assert(indirect && indirect->buffer);
121 /* indirect drawing implies core profile and none of these prim types */
122 assert(info->mode != MESA_PRIM_QUADS &&
123 info->mode != MESA_PRIM_QUAD_STRIP &&
124 info->mode != MESA_PRIM_POLYGON);
125
126 if (info->mode == MESA_PRIM_LINE_LOOP) {
127 /* need to do a fallback */
128 util_draw_indirect(&svga->pipe, info, 0, indirect);
129 return PIPE_OK;
130 }
131 else {
132 SVGA3dPrimitiveRange range;
133 unsigned hw_count;
134
135 range.primType = svga_translate_prim(info->mode, 12, &hw_count,
136 svga->patch_vertices);
137 range.primitiveCount = 0; /* specified in indirect buffer */
138 range.indexArray.surfaceId = SVGA3D_INVALID_ID;
139 range.indexArray.offset = 0;
140 range.indexArray.stride = 0;
141 range.indexWidth = info->index_size;
142 range.indexBias = 0; /* specified in indirect buffer */
143
144 SVGA_RETRY(svga, svga_hwtnl_prim
145 (svga->hwtnl, &range,
146 0, /* vertex count is in indirect buffer */
147 0, /* don't know min index */
148 ~0u, /* don't know max index */
149 info->index.resource,
150 info->start_instance,
151 0, /* don't know instance count */
152 indirect,
153 NULL)); /* SO vertex count */
154
155 return PIPE_OK;
156 }
157 }
158
159
160 /**
161 * Determine if we need to implement primitive restart with a fallback
162 * path which breaks the original primitive into sub-primitive at the
163 * restart indexes.
164 */
165 static bool
need_fallback_prim_restart(const struct svga_context * svga,const struct pipe_draw_info * info)166 need_fallback_prim_restart(const struct svga_context *svga,
167 const struct pipe_draw_info *info)
168 {
169 if (info->primitive_restart && info->index_size) {
170 if (!svga_have_vgpu10(svga))
171 return true;
172 else if (!svga->state.sw.need_swtnl) {
173 if (info->index_size == 1)
174 return true; /* no device support for 1-byte indexes */
175 else if (info->index_size == 2)
176 return info->restart_index != 0xffff;
177 else
178 return info->restart_index != 0xffffffff;
179 }
180 }
181
182 return false;
183 }
184
185
186 /**
187 * A helper function to return the vertex count from the primitive count
188 * returned from the stream output statistics query for the specified stream.
189 */
190 static unsigned
get_vcount_from_stream_output(struct svga_context * svga,const struct pipe_draw_info * info,unsigned stream)191 get_vcount_from_stream_output(struct svga_context *svga,
192 const struct pipe_draw_info *info,
193 unsigned stream)
194 {
195 unsigned primcount;
196 primcount = svga_get_primcount_from_stream_output(svga, stream);
197 return u_vertices_for_prims(info->mode, primcount);
198 }
199
200
201 static void
svga_draw_vbo(struct pipe_context * pipe,const struct pipe_draw_info * info,unsigned drawid_offset,const struct pipe_draw_indirect_info * indirect,const struct pipe_draw_start_count_bias * draws,unsigned num_draws)202 svga_draw_vbo(struct pipe_context *pipe, const struct pipe_draw_info *info,
203 unsigned drawid_offset,
204 const struct pipe_draw_indirect_info *indirect,
205 const struct pipe_draw_start_count_bias *draws,
206 unsigned num_draws)
207 {
208 if (num_draws > 1) {
209 util_draw_multi(pipe, info, drawid_offset, indirect, draws, num_draws);
210 return;
211 }
212
213 if (!indirect && (!draws[0].count || !info->instance_count))
214 return;
215
216 struct svga_context *svga = svga_context(pipe);
217 enum mesa_prim reduced_prim = u_reduced_prim(info->mode);
218 unsigned count = draws[0].count;
219 enum pipe_error ret = 0;
220 bool needed_swtnl;
221
222 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_DRAWVBO);
223
224 svga->hud.num_draw_calls++; /* for SVGA_QUERY_NUM_DRAW_CALLS */
225
226 if (u_reduced_prim(info->mode) == MESA_PRIM_TRIANGLES &&
227 svga->curr.rast->templ.cull_face == PIPE_FACE_FRONT_AND_BACK)
228 goto done;
229
230 if (svga->curr.reduced_prim != reduced_prim) {
231 svga->curr.reduced_prim = reduced_prim;
232 svga->dirty |= SVGA_NEW_REDUCED_PRIMITIVE;
233 }
234
235 /* We need to adjust the vertexID in the vertex shader since SV_VertexID
236 * always start from 0 for DrawArrays and does not include baseVertex for
237 * DrawIndexed.
238 */
239 unsigned index_bias = info->index_size ? draws->index_bias : 0;
240 if (svga->curr.vertex_id_bias != (draws[0].start + index_bias)) {
241 svga->curr.vertex_id_bias = draws[0].start + index_bias;
242 svga->dirty |= SVGA_NEW_VS_CONSTS;
243 }
244
245 if (svga->curr.vertices_per_patch != svga->patch_vertices) {
246 svga->curr.vertices_per_patch = svga->patch_vertices;
247
248 /* If input patch size changes, we need to notifiy the TCS
249 * code to reevaluate the shader variant since the
250 * vertices per patch count is a constant in the control
251 * point count declaration.
252 */
253 if (svga->curr.tcs || svga->curr.tes)
254 svga->dirty |= SVGA_NEW_TCS_PARAM;
255 }
256
257 if (need_fallback_prim_restart(svga, info)) {
258 enum pipe_error r;
259 r = util_draw_vbo_without_prim_restart(pipe, info, drawid_offset, indirect, &draws[0]);
260 assert(r == PIPE_OK);
261 (void) r;
262 goto done;
263 }
264
265 if (!indirect && !u_trim_pipe_prim(info->mode, &count))
266 goto done;
267
268 needed_swtnl = svga->state.sw.need_swtnl;
269
270 svga_update_state_retry(svga, SVGA_STATE_NEED_SWTNL);
271
272 if (svga->state.sw.need_swtnl) {
273 svga->hud.num_fallbacks++; /* for SVGA_QUERY_NUM_FALLBACKS */
274 if (!needed_swtnl) {
275 /*
276 * We're switching from HW to SW TNL. SW TNL will require mapping all
277 * currently bound vertex buffers, some of which may already be
278 * referenced in the current command buffer as result of previous HW
279 * TNL. So flush now, to prevent the context to flush while a referred
280 * vertex buffer is mapped.
281 */
282
283 svga_context_flush(svga, NULL);
284 }
285
286 /* Avoid leaking the previous hwtnl bias to swtnl */
287 svga_hwtnl_set_index_bias(svga->hwtnl, 0);
288 ret = svga_swtnl_draw_vbo(svga, info, drawid_offset, indirect, &draws[0]);
289 }
290 else {
291 if (!svga_update_state_retry(svga, SVGA_STATE_HW_DRAW)) {
292 static const char *msg = "State update failed, skipping draw call";
293 debug_printf("%s\n", msg);
294 util_debug_message(&svga->debug.callback, INFO, "%s", msg);
295 goto done;
296 }
297 svga_hwtnl_set_fillmode(svga->hwtnl, svga->curr.rast->hw_fillmode);
298
299 svga_update_state_retry(svga, SVGA_STATE_HW_DRAW);
300
301 /** determine if flatshade is to be used after svga_update_state()
302 * in case the fragment shader is changed.
303 */
304 svga_hwtnl_set_flatshade(svga->hwtnl,
305 svga->curr.rast->templ.flatshade ||
306 svga_is_using_flat_shading(svga),
307 svga->curr.rast->templ.flatshade_first);
308
309 if (indirect && indirect->count_from_stream_output) {
310 unsigned stream = 0;
311 assert(count == 0);
312
313 /* If the vertex count is from the stream output of a non-zero stream
314 * or the draw info specifies instancing, we will need a workaround
315 * since the draw_auto command does not support stream instancing.
316 * The workaround requires querying the vertex count from the
317 * stream output statistics query for the specified stream and then
318 * fallback to the regular draw function.
319 */
320
321 /* Check the stream index of the specified stream output target */
322 for (unsigned i = 0; i < ARRAY_SIZE(svga->so_targets); i++) {
323 if (svga->vcount_so_targets[i] == indirect->count_from_stream_output) {
324 stream = (svga->vcount_buffer_stream >> (i * 4)) & 0xf;
325 break;
326 }
327 }
328 if (info->instance_count > 1 || stream > 0) {
329 count = get_vcount_from_stream_output(svga, info, stream);
330 }
331 }
332
333 if (indirect && indirect->count_from_stream_output && count == 0) {
334 ret = retry_draw_auto(svga, info, indirect);
335 }
336 else if (indirect && indirect->buffer) {
337 ret = retry_draw_indirect(svga, info, indirect);
338 }
339 else if (info->index_size) {
340 ret = retry_draw_range_elements(svga, info, &draws[0], count);
341 }
342 else {
343 ret = retry_draw_arrays(svga, info->mode, draws[0].start, count,
344 info->start_instance, info->instance_count,
345 svga->patch_vertices);
346 }
347 }
348
349 /*
350 * Mark currently bound target surfaces as dirty after draw is completed.
351 */
352 svga_mark_surfaces_dirty(svga_context(pipe));
353
354 /* XXX: Silence warnings, do something sensible here? */
355 (void)ret;
356
357 if (SVGA_DEBUG & DEBUG_FLUSH) {
358 svga_hwtnl_flush_retry(svga);
359 svga_context_flush(svga, NULL);
360 }
361
362 done:
363 SVGA_STATS_TIME_POP(svga_sws(svga));
364 }
365
366
367 void
svga_init_draw_functions(struct svga_context * svga)368 svga_init_draw_functions(struct svga_context *svga)
369 {
370 svga->pipe.draw_vbo = svga_draw_vbo;
371 }
372