u_vbuf: Mark vbufs incompatible if more were requested than HW supports
[mesa.git] / src / gallium / auxiliary / util / u_vbuf.c
1 /**************************************************************************
2 *
3 * Copyright 2011 Marek Olšák <maraeo@gmail.com>
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /**
29 * This module uploads user buffers and translates the vertex buffers which
30 * contain incompatible vertices (i.e. not supported by the driver/hardware)
31 * into compatible ones, based on the Gallium CAPs.
32 *
33 * It does not upload index buffers.
34 *
35 * The module heavily uses bitmasks to represent per-buffer and
36 * per-vertex-element flags to avoid looping over the list of buffers just
37 * to see if there's a non-zero stride, or user buffer, or unsupported format,
38 * etc.
39 *
40 * There are 3 categories of vertex elements, which are processed separately:
41 * - per-vertex attribs (stride != 0, instance_divisor == 0)
42 * - instanced attribs (stride != 0, instance_divisor > 0)
43 * - constant attribs (stride == 0)
44 *
45 * All needed uploads and translations are performed every draw command, but
46 * only the subset of vertices needed for that draw command is uploaded or
47 * translated. (the module never translates whole buffers)
48 *
49 *
50 * The module consists of two main parts:
51 *
52 *
53 * 1) Translate (u_vbuf_translate_begin/end)
54 *
55 * This is pretty much a vertex fetch fallback. It translates vertices from
56 * one vertex buffer to another in an unused vertex buffer slot. It does
57 * whatever is needed to make the vertices readable by the hardware (changes
58 * vertex formats and aligns offsets and strides). The translate module is
59 * used here.
60 *
61 * Each of the 3 categories is translated to a separate buffer.
62 * Only the [min_index, max_index] range is translated. For instanced attribs,
63 * the range is [start_instance, start_instance+instance_count]. For constant
64 * attribs, the range is [0, 1].
65 *
66 *
67 * 2) User buffer uploading (u_vbuf_upload_buffers)
68 *
69 * Only the [min_index, max_index] range is uploaded (just like Translate)
70 * with a single memcpy.
71 *
72 * This method works best for non-indexed draw operations or indexed draw
73 * operations where the [min_index, max_index] range is not being way bigger
74 * than the vertex count.
75 *
76 * If the range is too big (e.g. one triangle with indices {0, 1, 10000}),
77 * the per-vertex attribs are uploaded via the translate module, all packed
78 * into one vertex buffer, and the indexed draw call is turned into
79 * a non-indexed one in the process. This adds additional complexity
80 * to the translate part, but it prevents bad apps from bringing your frame
81 * rate down.
82 *
83 *
84 * If there is nothing to do, it forwards every command to the driver.
85 * The module also has its own CSO cache of vertex element states.
86 */
87
88 #include "util/u_vbuf.h"
89
90 #include "util/u_dump.h"
91 #include "util/format/u_format.h"
92 #include "util/u_inlines.h"
93 #include "util/u_memory.h"
94 #include "util/u_upload_mgr.h"
95 #include "translate/translate.h"
96 #include "translate/translate_cache.h"
97 #include "cso_cache/cso_cache.h"
98 #include "cso_cache/cso_hash.h"
99
100 struct u_vbuf_elements {
101 unsigned count;
102 struct pipe_vertex_element ve[PIPE_MAX_ATTRIBS];
103
104 unsigned src_format_size[PIPE_MAX_ATTRIBS];
105
106 /* If (velem[i].src_format != native_format[i]), the vertex buffer
107 * referenced by the vertex element cannot be used for rendering and
108 * its vertex data must be translated to native_format[i]. */
109 enum pipe_format native_format[PIPE_MAX_ATTRIBS];
110 unsigned native_format_size[PIPE_MAX_ATTRIBS];
111
112 /* Which buffers are used by the vertex element state. */
113 uint32_t used_vb_mask;
114 /* This might mean two things:
115 * - src_format != native_format, as discussed above.
116 * - src_offset % 4 != 0 (if the caps don't allow such an offset). */
117 uint32_t incompatible_elem_mask; /* each bit describes a corresp. attrib */
118 /* Which buffer has at least one vertex element referencing it
119 * incompatible. */
120 uint32_t incompatible_vb_mask_any;
121 /* Which buffer has all vertex elements referencing it incompatible. */
122 uint32_t incompatible_vb_mask_all;
123 /* Which buffer has at least one vertex element referencing it
124 * compatible. */
125 uint32_t compatible_vb_mask_any;
126 /* Which buffer has all vertex elements referencing it compatible. */
127 uint32_t compatible_vb_mask_all;
128
129 /* Which buffer has at least one vertex element referencing it
130 * non-instanced. */
131 uint32_t noninstance_vb_mask_any;
132
133 void *driver_cso;
134 };
135
136 enum {
137 VB_VERTEX = 0,
138 VB_INSTANCE = 1,
139 VB_CONST = 2,
140 VB_NUM = 3
141 };
142
143 struct u_vbuf {
144 struct u_vbuf_caps caps;
145 bool has_signed_vb_offset;
146
147 struct pipe_context *pipe;
148 struct translate_cache *translate_cache;
149 struct cso_cache *cso_cache;
150
151 /* This is what was set in set_vertex_buffers.
152 * May contain user buffers. */
153 struct pipe_vertex_buffer vertex_buffer[PIPE_MAX_ATTRIBS];
154 uint32_t enabled_vb_mask;
155
156 /* Saved vertex buffer. */
157 struct pipe_vertex_buffer vertex_buffer0_saved;
158
159 /* Vertex buffers for the driver.
160 * There are usually no user buffers. */
161 struct pipe_vertex_buffer real_vertex_buffer[PIPE_MAX_ATTRIBS];
162 uint32_t dirty_real_vb_mask; /* which buffers are dirty since the last
163 call of set_vertex_buffers */
164
165 /* Vertex elements. */
166 struct u_vbuf_elements *ve, *ve_saved;
167
168 /* Vertex elements used for the translate fallback. */
169 struct pipe_vertex_element fallback_velems[PIPE_MAX_ATTRIBS];
170 /* If non-NULL, this is a vertex element state used for the translate
171 * fallback and therefore used for rendering too. */
172 boolean using_translate;
173 /* The vertex buffer slot index where translated vertices have been
174 * stored in. */
175 unsigned fallback_vbs[VB_NUM];
176
177 /* Which buffer is a user buffer. */
178 uint32_t user_vb_mask; /* each bit describes a corresp. buffer */
179 /* Which buffer is incompatible (unaligned). */
180 uint32_t incompatible_vb_mask; /* each bit describes a corresp. buffer */
181 /* Which buffer has a non-zero stride. */
182 uint32_t nonzero_stride_vb_mask; /* each bit describes a corresp. buffer */
183 /* Which buffers are allowed (supported by hardware). */
184 uint32_t allowed_vb_mask;
185 };
186
187 static void *
188 u_vbuf_create_vertex_elements(struct u_vbuf *mgr, unsigned count,
189 const struct pipe_vertex_element *attribs);
190 static void u_vbuf_delete_vertex_elements(struct u_vbuf *mgr, void *cso);
191
192 static const struct {
193 enum pipe_format from, to;
194 } vbuf_format_fallbacks[] = {
195 { PIPE_FORMAT_R32_FIXED, PIPE_FORMAT_R32_FLOAT },
196 { PIPE_FORMAT_R32G32_FIXED, PIPE_FORMAT_R32G32_FLOAT },
197 { PIPE_FORMAT_R32G32B32_FIXED, PIPE_FORMAT_R32G32B32_FLOAT },
198 { PIPE_FORMAT_R32G32B32A32_FIXED, PIPE_FORMAT_R32G32B32A32_FLOAT },
199 { PIPE_FORMAT_R16_FLOAT, PIPE_FORMAT_R32_FLOAT },
200 { PIPE_FORMAT_R16G16_FLOAT, PIPE_FORMAT_R32G32_FLOAT },
201 { PIPE_FORMAT_R16G16B16_FLOAT, PIPE_FORMAT_R32G32B32_FLOAT },
202 { PIPE_FORMAT_R16G16B16A16_FLOAT, PIPE_FORMAT_R32G32B32A32_FLOAT },
203 { PIPE_FORMAT_R64_FLOAT, PIPE_FORMAT_R32_FLOAT },
204 { PIPE_FORMAT_R64G64_FLOAT, PIPE_FORMAT_R32G32_FLOAT },
205 { PIPE_FORMAT_R64G64B64_FLOAT, PIPE_FORMAT_R32G32B32_FLOAT },
206 { PIPE_FORMAT_R64G64B64A64_FLOAT, PIPE_FORMAT_R32G32B32A32_FLOAT },
207 { PIPE_FORMAT_R32_UNORM, PIPE_FORMAT_R32_FLOAT },
208 { PIPE_FORMAT_R32G32_UNORM, PIPE_FORMAT_R32G32_FLOAT },
209 { PIPE_FORMAT_R32G32B32_UNORM, PIPE_FORMAT_R32G32B32_FLOAT },
210 { PIPE_FORMAT_R32G32B32A32_UNORM, PIPE_FORMAT_R32G32B32A32_FLOAT },
211 { PIPE_FORMAT_R32_SNORM, PIPE_FORMAT_R32_FLOAT },
212 { PIPE_FORMAT_R32G32_SNORM, PIPE_FORMAT_R32G32_FLOAT },
213 { PIPE_FORMAT_R32G32B32_SNORM, PIPE_FORMAT_R32G32B32_FLOAT },
214 { PIPE_FORMAT_R32G32B32A32_SNORM, PIPE_FORMAT_R32G32B32A32_FLOAT },
215 { PIPE_FORMAT_R32_USCALED, PIPE_FORMAT_R32_FLOAT },
216 { PIPE_FORMAT_R32G32_USCALED, PIPE_FORMAT_R32G32_FLOAT },
217 { PIPE_FORMAT_R32G32B32_USCALED, PIPE_FORMAT_R32G32B32_FLOAT },
218 { PIPE_FORMAT_R32G32B32A32_USCALED, PIPE_FORMAT_R32G32B32A32_FLOAT },
219 { PIPE_FORMAT_R32_SSCALED, PIPE_FORMAT_R32_FLOAT },
220 { PIPE_FORMAT_R32G32_SSCALED, PIPE_FORMAT_R32G32_FLOAT },
221 { PIPE_FORMAT_R32G32B32_SSCALED, PIPE_FORMAT_R32G32B32_FLOAT },
222 { PIPE_FORMAT_R32G32B32A32_SSCALED, PIPE_FORMAT_R32G32B32A32_FLOAT },
223 { PIPE_FORMAT_R16_UNORM, PIPE_FORMAT_R32_FLOAT },
224 { PIPE_FORMAT_R16G16_UNORM, PIPE_FORMAT_R32G32_FLOAT },
225 { PIPE_FORMAT_R16G16B16_UNORM, PIPE_FORMAT_R32G32B32_FLOAT },
226 { PIPE_FORMAT_R16G16B16A16_UNORM, PIPE_FORMAT_R32G32B32A32_FLOAT },
227 { PIPE_FORMAT_R16_SNORM, PIPE_FORMAT_R32_FLOAT },
228 { PIPE_FORMAT_R16G16_SNORM, PIPE_FORMAT_R32G32_FLOAT },
229 { PIPE_FORMAT_R16G16B16_SNORM, PIPE_FORMAT_R32G32B32_FLOAT },
230 { PIPE_FORMAT_R16G16B16A16_SNORM, PIPE_FORMAT_R32G32B32A32_FLOAT },
231 { PIPE_FORMAT_R16_USCALED, PIPE_FORMAT_R32_FLOAT },
232 { PIPE_FORMAT_R16G16_USCALED, PIPE_FORMAT_R32G32_FLOAT },
233 { PIPE_FORMAT_R16G16B16_USCALED, PIPE_FORMAT_R32G32B32_FLOAT },
234 { PIPE_FORMAT_R16G16B16A16_USCALED, PIPE_FORMAT_R32G32B32A32_FLOAT },
235 { PIPE_FORMAT_R16_SSCALED, PIPE_FORMAT_R32_FLOAT },
236 { PIPE_FORMAT_R16G16_SSCALED, PIPE_FORMAT_R32G32_FLOAT },
237 { PIPE_FORMAT_R16G16B16_SSCALED, PIPE_FORMAT_R32G32B32_FLOAT },
238 { PIPE_FORMAT_R16G16B16A16_SSCALED, PIPE_FORMAT_R32G32B32A32_FLOAT },
239 { PIPE_FORMAT_R8_UNORM, PIPE_FORMAT_R32_FLOAT },
240 { PIPE_FORMAT_R8G8_UNORM, PIPE_FORMAT_R32G32_FLOAT },
241 { PIPE_FORMAT_R8G8B8_UNORM, PIPE_FORMAT_R32G32B32_FLOAT },
242 { PIPE_FORMAT_R8G8B8A8_UNORM, PIPE_FORMAT_R32G32B32A32_FLOAT },
243 { PIPE_FORMAT_R8_SNORM, PIPE_FORMAT_R32_FLOAT },
244 { PIPE_FORMAT_R8G8_SNORM, PIPE_FORMAT_R32G32_FLOAT },
245 { PIPE_FORMAT_R8G8B8_SNORM, PIPE_FORMAT_R32G32B32_FLOAT },
246 { PIPE_FORMAT_R8G8B8A8_SNORM, PIPE_FORMAT_R32G32B32A32_FLOAT },
247 { PIPE_FORMAT_R8_USCALED, PIPE_FORMAT_R32_FLOAT },
248 { PIPE_FORMAT_R8G8_USCALED, PIPE_FORMAT_R32G32_FLOAT },
249 { PIPE_FORMAT_R8G8B8_USCALED, PIPE_FORMAT_R32G32B32_FLOAT },
250 { PIPE_FORMAT_R8G8B8A8_USCALED, PIPE_FORMAT_R32G32B32A32_FLOAT },
251 { PIPE_FORMAT_R8_SSCALED, PIPE_FORMAT_R32_FLOAT },
252 { PIPE_FORMAT_R8G8_SSCALED, PIPE_FORMAT_R32G32_FLOAT },
253 { PIPE_FORMAT_R8G8B8_SSCALED, PIPE_FORMAT_R32G32B32_FLOAT },
254 { PIPE_FORMAT_R8G8B8A8_SSCALED, PIPE_FORMAT_R32G32B32A32_FLOAT },
255 };
256
257 boolean u_vbuf_get_caps(struct pipe_screen *screen, struct u_vbuf_caps *caps,
258 unsigned flags)
259 {
260 unsigned i;
261 boolean fallback = FALSE;
262
263 /* I'd rather have a bitfield of which formats are supported and a static
264 * table of the translations indexed by format, but since we don't have C99
265 * we can't easily make a sparsely-populated table indexed by format. So,
266 * we construct the sparse table here.
267 */
268 for (i = 0; i < PIPE_FORMAT_COUNT; i++)
269 caps->format_translation[i] = i;
270
271 for (i = 0; i < ARRAY_SIZE(vbuf_format_fallbacks); i++) {
272 enum pipe_format format = vbuf_format_fallbacks[i].from;
273
274 if (!screen->is_format_supported(screen, format, PIPE_BUFFER, 0, 0,
275 PIPE_BIND_VERTEX_BUFFER)) {
276 caps->format_translation[format] = vbuf_format_fallbacks[i].to;
277 fallback = TRUE;
278 }
279 }
280
281 caps->buffer_offset_unaligned =
282 !screen->get_param(screen,
283 PIPE_CAP_VERTEX_BUFFER_OFFSET_4BYTE_ALIGNED_ONLY);
284 caps->buffer_stride_unaligned =
285 !screen->get_param(screen,
286 PIPE_CAP_VERTEX_BUFFER_STRIDE_4BYTE_ALIGNED_ONLY);
287 caps->velem_src_offset_unaligned =
288 !screen->get_param(screen,
289 PIPE_CAP_VERTEX_ELEMENT_SRC_OFFSET_4BYTE_ALIGNED_ONLY);
290 caps->user_vertex_buffers =
291 screen->get_param(screen, PIPE_CAP_USER_VERTEX_BUFFERS);
292 caps->max_vertex_buffers =
293 screen->get_param(screen, PIPE_CAP_MAX_VERTEX_BUFFERS);
294
295 if (!caps->buffer_offset_unaligned ||
296 !caps->buffer_stride_unaligned ||
297 !caps->velem_src_offset_unaligned ||
298 (!(flags & U_VBUF_FLAG_NO_USER_VBOS) && !caps->user_vertex_buffers)) {
299 fallback = TRUE;
300 }
301
302 return fallback;
303 }
304
305 struct u_vbuf *
306 u_vbuf_create(struct pipe_context *pipe, struct u_vbuf_caps *caps)
307 {
308 struct u_vbuf *mgr = CALLOC_STRUCT(u_vbuf);
309
310 mgr->caps = *caps;
311 mgr->pipe = pipe;
312 mgr->cso_cache = cso_cache_create();
313 mgr->translate_cache = translate_cache_create();
314 memset(mgr->fallback_vbs, ~0, sizeof(mgr->fallback_vbs));
315 mgr->allowed_vb_mask = u_bit_consecutive(0, mgr->caps.max_vertex_buffers);
316
317 mgr->has_signed_vb_offset =
318 pipe->screen->get_param(pipe->screen,
319 PIPE_CAP_SIGNED_VERTEX_BUFFER_OFFSET);
320
321 return mgr;
322 }
323
324 /* u_vbuf uses its own caching for vertex elements, because it needs to keep
325 * its own preprocessed state per vertex element CSO. */
326 static struct u_vbuf_elements *
327 u_vbuf_set_vertex_elements_internal(struct u_vbuf *mgr, unsigned count,
328 const struct pipe_vertex_element *states)
329 {
330 struct pipe_context *pipe = mgr->pipe;
331 unsigned key_size, hash_key;
332 struct cso_hash_iter iter;
333 struct u_vbuf_elements *ve;
334 struct cso_velems_state velems_state;
335
336 /* need to include the count into the stored state data too. */
337 key_size = sizeof(struct pipe_vertex_element) * count + sizeof(unsigned);
338 velems_state.count = count;
339 memcpy(velems_state.velems, states,
340 sizeof(struct pipe_vertex_element) * count);
341 hash_key = cso_construct_key((void*)&velems_state, key_size);
342 iter = cso_find_state_template(mgr->cso_cache, hash_key, CSO_VELEMENTS,
343 (void*)&velems_state, key_size);
344
345 if (cso_hash_iter_is_null(iter)) {
346 struct cso_velements *cso = MALLOC_STRUCT(cso_velements);
347 memcpy(&cso->state, &velems_state, key_size);
348 cso->data = u_vbuf_create_vertex_elements(mgr, count, states);
349 cso->delete_state = (cso_state_callback)u_vbuf_delete_vertex_elements;
350 cso->context = (void*)mgr;
351
352 iter = cso_insert_state(mgr->cso_cache, hash_key, CSO_VELEMENTS, cso);
353 ve = cso->data;
354 } else {
355 ve = ((struct cso_velements *)cso_hash_iter_data(iter))->data;
356 }
357
358 assert(ve);
359
360 if (ve != mgr->ve)
361 pipe->bind_vertex_elements_state(pipe, ve->driver_cso);
362
363 return ve;
364 }
365
366 void u_vbuf_set_vertex_elements(struct u_vbuf *mgr, unsigned count,
367 const struct pipe_vertex_element *states)
368 {
369 mgr->ve = u_vbuf_set_vertex_elements_internal(mgr, count, states);
370 }
371
372 void u_vbuf_destroy(struct u_vbuf *mgr)
373 {
374 struct pipe_screen *screen = mgr->pipe->screen;
375 unsigned i;
376 const unsigned num_vb = screen->get_shader_param(screen, PIPE_SHADER_VERTEX,
377 PIPE_SHADER_CAP_MAX_INPUTS);
378
379 mgr->pipe->set_vertex_buffers(mgr->pipe, 0, num_vb, NULL);
380
381 for (i = 0; i < PIPE_MAX_ATTRIBS; i++)
382 pipe_vertex_buffer_unreference(&mgr->vertex_buffer[i]);
383 for (i = 0; i < PIPE_MAX_ATTRIBS; i++)
384 pipe_vertex_buffer_unreference(&mgr->real_vertex_buffer[i]);
385
386 pipe_vertex_buffer_unreference(&mgr->vertex_buffer0_saved);
387
388 translate_cache_destroy(mgr->translate_cache);
389 cso_cache_delete(mgr->cso_cache);
390 FREE(mgr);
391 }
392
393 static enum pipe_error
394 u_vbuf_translate_buffers(struct u_vbuf *mgr, struct translate_key *key,
395 const struct pipe_draw_info *info,
396 unsigned vb_mask, unsigned out_vb,
397 int start_vertex, unsigned num_vertices,
398 int min_index, boolean unroll_indices)
399 {
400 struct translate *tr;
401 struct pipe_transfer *vb_transfer[PIPE_MAX_ATTRIBS] = {0};
402 struct pipe_resource *out_buffer = NULL;
403 uint8_t *out_map;
404 unsigned out_offset, mask;
405
406 /* Get a translate object. */
407 tr = translate_cache_find(mgr->translate_cache, key);
408
409 /* Map buffers we want to translate. */
410 mask = vb_mask;
411 while (mask) {
412 struct pipe_vertex_buffer *vb;
413 unsigned offset;
414 uint8_t *map;
415 unsigned i = u_bit_scan(&mask);
416
417 vb = &mgr->vertex_buffer[i];
418 offset = vb->buffer_offset + vb->stride * start_vertex;
419
420 if (vb->is_user_buffer) {
421 map = (uint8_t*)vb->buffer.user + offset;
422 } else {
423 unsigned size = vb->stride ? num_vertices * vb->stride
424 : sizeof(double)*4;
425
426 if (!vb->buffer.resource)
427 continue;
428
429 if (offset + size > vb->buffer.resource->width0) {
430 /* Don't try to map past end of buffer. This often happens when
431 * we're translating an attribute that's at offset > 0 from the
432 * start of the vertex. If we'd subtract attrib's offset from
433 * the size, this probably wouldn't happen.
434 */
435 size = vb->buffer.resource->width0 - offset;
436
437 /* Also adjust num_vertices. A common user error is to call
438 * glDrawRangeElements() with incorrect 'end' argument. The 'end
439 * value should be the max index value, but people often
440 * accidentally add one to this value. This adjustment avoids
441 * crashing (by reading past the end of a hardware buffer mapping)
442 * when people do that.
443 */
444 num_vertices = (size + vb->stride - 1) / vb->stride;
445 }
446
447 map = pipe_buffer_map_range(mgr->pipe, vb->buffer.resource, offset, size,
448 PIPE_TRANSFER_READ, &vb_transfer[i]);
449 }
450
451 /* Subtract min_index so that indexing with the index buffer works. */
452 if (unroll_indices) {
453 map -= (ptrdiff_t)vb->stride * min_index;
454 }
455
456 tr->set_buffer(tr, i, map, vb->stride, info->max_index);
457 }
458
459 /* Translate. */
460 if (unroll_indices) {
461 struct pipe_transfer *transfer = NULL;
462 const unsigned offset = info->start * info->index_size;
463 uint8_t *map;
464
465 /* Create and map the output buffer. */
466 u_upload_alloc(mgr->pipe->stream_uploader, 0,
467 key->output_stride * info->count, 4,
468 &out_offset, &out_buffer,
469 (void**)&out_map);
470 if (!out_buffer)
471 return PIPE_ERROR_OUT_OF_MEMORY;
472
473 if (info->has_user_indices) {
474 map = (uint8_t*)info->index.user + offset;
475 } else {
476 map = pipe_buffer_map_range(mgr->pipe, info->index.resource, offset,
477 info->count * info->index_size,
478 PIPE_TRANSFER_READ, &transfer);
479 }
480
481 switch (info->index_size) {
482 case 4:
483 tr->run_elts(tr, (unsigned*)map, info->count, 0, 0, out_map);
484 break;
485 case 2:
486 tr->run_elts16(tr, (uint16_t*)map, info->count, 0, 0, out_map);
487 break;
488 case 1:
489 tr->run_elts8(tr, map, info->count, 0, 0, out_map);
490 break;
491 }
492
493 if (transfer) {
494 pipe_buffer_unmap(mgr->pipe, transfer);
495 }
496 } else {
497 /* Create and map the output buffer. */
498 u_upload_alloc(mgr->pipe->stream_uploader,
499 mgr->has_signed_vb_offset ?
500 0 : key->output_stride * start_vertex,
501 key->output_stride * num_vertices, 4,
502 &out_offset, &out_buffer,
503 (void**)&out_map);
504 if (!out_buffer)
505 return PIPE_ERROR_OUT_OF_MEMORY;
506
507 out_offset -= key->output_stride * start_vertex;
508
509 tr->run(tr, 0, num_vertices, 0, 0, out_map);
510 }
511
512 /* Unmap all buffers. */
513 mask = vb_mask;
514 while (mask) {
515 unsigned i = u_bit_scan(&mask);
516
517 if (vb_transfer[i]) {
518 pipe_buffer_unmap(mgr->pipe, vb_transfer[i]);
519 }
520 }
521
522 /* Setup the new vertex buffer. */
523 mgr->real_vertex_buffer[out_vb].buffer_offset = out_offset;
524 mgr->real_vertex_buffer[out_vb].stride = key->output_stride;
525
526 /* Move the buffer reference. */
527 pipe_vertex_buffer_unreference(&mgr->real_vertex_buffer[out_vb]);
528 mgr->real_vertex_buffer[out_vb].buffer.resource = out_buffer;
529 mgr->real_vertex_buffer[out_vb].is_user_buffer = false;
530
531 return PIPE_OK;
532 }
533
534 static boolean
535 u_vbuf_translate_find_free_vb_slots(struct u_vbuf *mgr,
536 unsigned mask[VB_NUM])
537 {
538 unsigned type;
539 unsigned fallback_vbs[VB_NUM];
540 /* Set the bit for each buffer which is incompatible, or isn't set. */
541 uint32_t unused_vb_mask =
542 mgr->ve->incompatible_vb_mask_all | mgr->incompatible_vb_mask |
543 ~mgr->enabled_vb_mask;
544
545 memset(fallback_vbs, ~0, sizeof(fallback_vbs));
546
547 /* Find free slots for each type if needed. */
548 for (type = 0; type < VB_NUM; type++) {
549 if (mask[type]) {
550 uint32_t index;
551
552 if (!unused_vb_mask) {
553 return FALSE;
554 }
555
556 index = ffs(unused_vb_mask) - 1;
557 fallback_vbs[type] = index;
558 unused_vb_mask &= ~(1 << index);
559 /*printf("found slot=%i for type=%i\n", index, type);*/
560 }
561 }
562
563 for (type = 0; type < VB_NUM; type++) {
564 if (mask[type]) {
565 mgr->dirty_real_vb_mask |= 1 << fallback_vbs[type];
566 }
567 }
568
569 memcpy(mgr->fallback_vbs, fallback_vbs, sizeof(fallback_vbs));
570 return TRUE;
571 }
572
573 static boolean
574 u_vbuf_translate_begin(struct u_vbuf *mgr,
575 const struct pipe_draw_info *info,
576 int start_vertex, unsigned num_vertices,
577 int min_index, boolean unroll_indices)
578 {
579 unsigned mask[VB_NUM] = {0};
580 struct translate_key key[VB_NUM];
581 unsigned elem_index[VB_NUM][PIPE_MAX_ATTRIBS]; /* ... into key.elements */
582 unsigned i, type;
583 const unsigned incompatible_vb_mask = mgr->incompatible_vb_mask &
584 mgr->ve->used_vb_mask;
585
586 const int start[VB_NUM] = {
587 start_vertex, /* VERTEX */
588 info->start_instance, /* INSTANCE */
589 0 /* CONST */
590 };
591
592 const unsigned num[VB_NUM] = {
593 num_vertices, /* VERTEX */
594 info->instance_count, /* INSTANCE */
595 1 /* CONST */
596 };
597
598 memset(key, 0, sizeof(key));
599 memset(elem_index, ~0, sizeof(elem_index));
600
601 /* See if there are vertex attribs of each type to translate and
602 * which ones. */
603 for (i = 0; i < mgr->ve->count; i++) {
604 unsigned vb_index = mgr->ve->ve[i].vertex_buffer_index;
605
606 if (!mgr->vertex_buffer[vb_index].stride) {
607 if (!(mgr->ve->incompatible_elem_mask & (1 << i)) &&
608 !(incompatible_vb_mask & (1 << vb_index))) {
609 continue;
610 }
611 mask[VB_CONST] |= 1 << vb_index;
612 } else if (mgr->ve->ve[i].instance_divisor) {
613 if (!(mgr->ve->incompatible_elem_mask & (1 << i)) &&
614 !(incompatible_vb_mask & (1 << vb_index))) {
615 continue;
616 }
617 mask[VB_INSTANCE] |= 1 << vb_index;
618 } else {
619 if (!unroll_indices &&
620 !(mgr->ve->incompatible_elem_mask & (1 << i)) &&
621 !(incompatible_vb_mask & (1 << vb_index))) {
622 continue;
623 }
624 mask[VB_VERTEX] |= 1 << vb_index;
625 }
626 }
627
628 assert(mask[VB_VERTEX] || mask[VB_INSTANCE] || mask[VB_CONST]);
629
630 /* Find free vertex buffer slots. */
631 if (!u_vbuf_translate_find_free_vb_slots(mgr, mask)) {
632 return FALSE;
633 }
634
635 /* Initialize the translate keys. */
636 for (i = 0; i < mgr->ve->count; i++) {
637 struct translate_key *k;
638 struct translate_element *te;
639 enum pipe_format output_format = mgr->ve->native_format[i];
640 unsigned bit, vb_index = mgr->ve->ve[i].vertex_buffer_index;
641 bit = 1 << vb_index;
642
643 if (!(mgr->ve->incompatible_elem_mask & (1 << i)) &&
644 !(incompatible_vb_mask & (1 << vb_index)) &&
645 (!unroll_indices || !(mask[VB_VERTEX] & bit))) {
646 continue;
647 }
648
649 /* Set type to what we will translate.
650 * Whether vertex, instance, or constant attribs. */
651 for (type = 0; type < VB_NUM; type++) {
652 if (mask[type] & bit) {
653 break;
654 }
655 }
656 assert(type < VB_NUM);
657 if (mgr->ve->ve[i].src_format != output_format)
658 assert(translate_is_output_format_supported(output_format));
659 /*printf("velem=%i type=%i\n", i, type);*/
660
661 /* Add the vertex element. */
662 k = &key[type];
663 elem_index[type][i] = k->nr_elements;
664
665 te = &k->element[k->nr_elements];
666 te->type = TRANSLATE_ELEMENT_NORMAL;
667 te->instance_divisor = 0;
668 te->input_buffer = vb_index;
669 te->input_format = mgr->ve->ve[i].src_format;
670 te->input_offset = mgr->ve->ve[i].src_offset;
671 te->output_format = output_format;
672 te->output_offset = k->output_stride;
673
674 k->output_stride += mgr->ve->native_format_size[i];
675 k->nr_elements++;
676 }
677
678 /* Translate buffers. */
679 for (type = 0; type < VB_NUM; type++) {
680 if (key[type].nr_elements) {
681 enum pipe_error err;
682 err = u_vbuf_translate_buffers(mgr, &key[type], info, mask[type],
683 mgr->fallback_vbs[type],
684 start[type], num[type], min_index,
685 unroll_indices && type == VB_VERTEX);
686 if (err != PIPE_OK)
687 return FALSE;
688
689 /* Fixup the stride for constant attribs. */
690 if (type == VB_CONST) {
691 mgr->real_vertex_buffer[mgr->fallback_vbs[VB_CONST]].stride = 0;
692 }
693 }
694 }
695
696 /* Setup new vertex elements. */
697 for (i = 0; i < mgr->ve->count; i++) {
698 for (type = 0; type < VB_NUM; type++) {
699 if (elem_index[type][i] < key[type].nr_elements) {
700 struct translate_element *te = &key[type].element[elem_index[type][i]];
701 mgr->fallback_velems[i].instance_divisor = mgr->ve->ve[i].instance_divisor;
702 mgr->fallback_velems[i].src_format = te->output_format;
703 mgr->fallback_velems[i].src_offset = te->output_offset;
704 mgr->fallback_velems[i].vertex_buffer_index = mgr->fallback_vbs[type];
705
706 /* elem_index[type][i] can only be set for one type. */
707 assert(type > VB_INSTANCE || elem_index[type+1][i] == ~0u);
708 assert(type > VB_VERTEX || elem_index[type+2][i] == ~0u);
709 break;
710 }
711 }
712 /* No translating, just copy the original vertex element over. */
713 if (type == VB_NUM) {
714 memcpy(&mgr->fallback_velems[i], &mgr->ve->ve[i],
715 sizeof(struct pipe_vertex_element));
716 }
717 }
718
719 u_vbuf_set_vertex_elements_internal(mgr, mgr->ve->count,
720 mgr->fallback_velems);
721 mgr->using_translate = TRUE;
722 return TRUE;
723 }
724
725 static void u_vbuf_translate_end(struct u_vbuf *mgr)
726 {
727 unsigned i;
728
729 /* Restore vertex elements. */
730 mgr->pipe->bind_vertex_elements_state(mgr->pipe, mgr->ve->driver_cso);
731 mgr->using_translate = FALSE;
732
733 /* Unreference the now-unused VBOs. */
734 for (i = 0; i < VB_NUM; i++) {
735 unsigned vb = mgr->fallback_vbs[i];
736 if (vb != ~0u) {
737 pipe_resource_reference(&mgr->real_vertex_buffer[vb].buffer.resource, NULL);
738 mgr->fallback_vbs[i] = ~0;
739
740 /* This will cause the buffer to be unbound in the driver later. */
741 mgr->dirty_real_vb_mask |= 1 << vb;
742 }
743 }
744 }
745
746 static void *
747 u_vbuf_create_vertex_elements(struct u_vbuf *mgr, unsigned count,
748 const struct pipe_vertex_element *attribs)
749 {
750 struct pipe_context *pipe = mgr->pipe;
751 unsigned i;
752 struct pipe_vertex_element driver_attribs[PIPE_MAX_ATTRIBS];
753 struct u_vbuf_elements *ve = CALLOC_STRUCT(u_vbuf_elements);
754 uint32_t used_buffers = 0;
755
756 ve->count = count;
757
758 memcpy(ve->ve, attribs, sizeof(struct pipe_vertex_element) * count);
759 memcpy(driver_attribs, attribs, sizeof(struct pipe_vertex_element) * count);
760
761 /* Set the best native format in case the original format is not
762 * supported. */
763 for (i = 0; i < count; i++) {
764 enum pipe_format format = ve->ve[i].src_format;
765
766 ve->src_format_size[i] = util_format_get_blocksize(format);
767
768 used_buffers |= 1 << ve->ve[i].vertex_buffer_index;
769
770 if (!ve->ve[i].instance_divisor) {
771 ve->noninstance_vb_mask_any |= 1 << ve->ve[i].vertex_buffer_index;
772 }
773
774 format = mgr->caps.format_translation[format];
775
776 driver_attribs[i].src_format = format;
777 ve->native_format[i] = format;
778 ve->native_format_size[i] =
779 util_format_get_blocksize(ve->native_format[i]);
780
781 if (ve->ve[i].src_format != format ||
782 (!mgr->caps.velem_src_offset_unaligned &&
783 ve->ve[i].src_offset % 4 != 0)) {
784 ve->incompatible_elem_mask |= 1 << i;
785 ve->incompatible_vb_mask_any |= 1 << ve->ve[i].vertex_buffer_index;
786 } else {
787 ve->compatible_vb_mask_any |= 1 << ve->ve[i].vertex_buffer_index;
788 }
789 }
790
791 if (used_buffers & ~mgr->allowed_vb_mask) {
792 /* More vertex buffers are used than the hardware supports. In
793 * principle, we only need to make sure that less vertex buffers are
794 * used, and mark some of the latter vertex buffers as incompatible.
795 * For now, mark all vertex buffers as incompatible.
796 */
797 ve->incompatible_vb_mask_any = used_buffers;
798 ve->compatible_vb_mask_any = 0;
799 ve->incompatible_elem_mask = u_bit_consecutive(0, count);
800 }
801
802 ve->used_vb_mask = used_buffers;
803 ve->compatible_vb_mask_all = ~ve->incompatible_vb_mask_any & used_buffers;
804 ve->incompatible_vb_mask_all = ~ve->compatible_vb_mask_any & used_buffers;
805
806 /* Align the formats and offsets to the size of DWORD if needed. */
807 if (!mgr->caps.velem_src_offset_unaligned) {
808 for (i = 0; i < count; i++) {
809 ve->native_format_size[i] = align(ve->native_format_size[i], 4);
810 driver_attribs[i].src_offset = align(ve->ve[i].src_offset, 4);
811 }
812 }
813
814 ve->driver_cso =
815 pipe->create_vertex_elements_state(pipe, count, driver_attribs);
816 return ve;
817 }
818
819 static void u_vbuf_delete_vertex_elements(struct u_vbuf *mgr, void *cso)
820 {
821 struct pipe_context *pipe = mgr->pipe;
822 struct u_vbuf_elements *ve = cso;
823
824 pipe->delete_vertex_elements_state(pipe, ve->driver_cso);
825 FREE(ve);
826 }
827
828 void u_vbuf_set_vertex_buffers(struct u_vbuf *mgr,
829 unsigned start_slot, unsigned count,
830 const struct pipe_vertex_buffer *bufs)
831 {
832 unsigned i;
833 /* which buffers are enabled */
834 uint32_t enabled_vb_mask = 0;
835 /* which buffers are in user memory */
836 uint32_t user_vb_mask = 0;
837 /* which buffers are incompatible with the driver */
838 uint32_t incompatible_vb_mask = 0;
839 /* which buffers have a non-zero stride */
840 uint32_t nonzero_stride_vb_mask = 0;
841 const uint32_t mask = ~(((1ull << count) - 1) << start_slot);
842
843 /* Zero out the bits we are going to rewrite completely. */
844 mgr->user_vb_mask &= mask;
845 mgr->incompatible_vb_mask &= mask;
846 mgr->nonzero_stride_vb_mask &= mask;
847 mgr->enabled_vb_mask &= mask;
848
849 if (!bufs) {
850 struct pipe_context *pipe = mgr->pipe;
851 /* Unbind. */
852 mgr->dirty_real_vb_mask &= mask;
853
854 for (i = 0; i < count; i++) {
855 unsigned dst_index = start_slot + i;
856
857 pipe_vertex_buffer_unreference(&mgr->vertex_buffer[dst_index]);
858 pipe_vertex_buffer_unreference(&mgr->real_vertex_buffer[dst_index]);
859 }
860
861 pipe->set_vertex_buffers(pipe, start_slot, count, NULL);
862 return;
863 }
864
865 for (i = 0; i < count; i++) {
866 unsigned dst_index = start_slot + i;
867 const struct pipe_vertex_buffer *vb = &bufs[i];
868 struct pipe_vertex_buffer *orig_vb = &mgr->vertex_buffer[dst_index];
869 struct pipe_vertex_buffer *real_vb = &mgr->real_vertex_buffer[dst_index];
870
871 if (!vb->buffer.resource) {
872 pipe_vertex_buffer_unreference(orig_vb);
873 pipe_vertex_buffer_unreference(real_vb);
874 continue;
875 }
876
877 pipe_vertex_buffer_reference(orig_vb, vb);
878
879 if (vb->stride) {
880 nonzero_stride_vb_mask |= 1 << dst_index;
881 }
882 enabled_vb_mask |= 1 << dst_index;
883
884 if ((!mgr->caps.buffer_offset_unaligned && vb->buffer_offset % 4 != 0) ||
885 (!mgr->caps.buffer_stride_unaligned && vb->stride % 4 != 0)) {
886 incompatible_vb_mask |= 1 << dst_index;
887 real_vb->buffer_offset = vb->buffer_offset;
888 real_vb->stride = vb->stride;
889 pipe_vertex_buffer_unreference(real_vb);
890 real_vb->is_user_buffer = false;
891 continue;
892 }
893
894 if (!mgr->caps.user_vertex_buffers && vb->is_user_buffer) {
895 user_vb_mask |= 1 << dst_index;
896 real_vb->buffer_offset = vb->buffer_offset;
897 real_vb->stride = vb->stride;
898 pipe_vertex_buffer_unreference(real_vb);
899 real_vb->is_user_buffer = false;
900 continue;
901 }
902
903 pipe_vertex_buffer_reference(real_vb, vb);
904 }
905
906 mgr->user_vb_mask |= user_vb_mask;
907 mgr->incompatible_vb_mask |= incompatible_vb_mask;
908 mgr->nonzero_stride_vb_mask |= nonzero_stride_vb_mask;
909 mgr->enabled_vb_mask |= enabled_vb_mask;
910
911 /* All changed buffers are marked as dirty, even the NULL ones,
912 * which will cause the NULL buffers to be unbound in the driver later. */
913 mgr->dirty_real_vb_mask |= ~mask;
914 }
915
916 static enum pipe_error
917 u_vbuf_upload_buffers(struct u_vbuf *mgr,
918 int start_vertex, unsigned num_vertices,
919 int start_instance, unsigned num_instances)
920 {
921 unsigned i;
922 unsigned nr_velems = mgr->ve->count;
923 const struct pipe_vertex_element *velems =
924 mgr->using_translate ? mgr->fallback_velems : mgr->ve->ve;
925 unsigned start_offset[PIPE_MAX_ATTRIBS];
926 unsigned end_offset[PIPE_MAX_ATTRIBS];
927 uint32_t buffer_mask = 0;
928
929 /* Determine how much data needs to be uploaded. */
930 for (i = 0; i < nr_velems; i++) {
931 const struct pipe_vertex_element *velem = &velems[i];
932 unsigned index = velem->vertex_buffer_index;
933 struct pipe_vertex_buffer *vb = &mgr->vertex_buffer[index];
934 unsigned instance_div, first, size, index_bit;
935
936 /* Skip the buffers generated by translate. */
937 if (index == mgr->fallback_vbs[VB_VERTEX] ||
938 index == mgr->fallback_vbs[VB_INSTANCE] ||
939 index == mgr->fallback_vbs[VB_CONST]) {
940 continue;
941 }
942
943 if (!vb->is_user_buffer) {
944 continue;
945 }
946
947 instance_div = velem->instance_divisor;
948 first = vb->buffer_offset + velem->src_offset;
949
950 if (!vb->stride) {
951 /* Constant attrib. */
952 size = mgr->ve->src_format_size[i];
953 } else if (instance_div) {
954 /* Per-instance attrib. */
955
956 /* Figure out how many instances we'll render given instance_div. We
957 * can't use the typical div_round_up() pattern because the CTS uses
958 * instance_div = ~0 for a test, which overflows div_round_up()'s
959 * addition.
960 */
961 unsigned count = num_instances / instance_div;
962 if (count * instance_div != num_instances)
963 count++;
964
965 first += vb->stride * start_instance;
966 size = vb->stride * (count - 1) + mgr->ve->src_format_size[i];
967 } else {
968 /* Per-vertex attrib. */
969 first += vb->stride * start_vertex;
970 size = vb->stride * (num_vertices - 1) + mgr->ve->src_format_size[i];
971 }
972
973 index_bit = 1 << index;
974
975 /* Update offsets. */
976 if (!(buffer_mask & index_bit)) {
977 start_offset[index] = first;
978 end_offset[index] = first + size;
979 } else {
980 if (first < start_offset[index])
981 start_offset[index] = first;
982 if (first + size > end_offset[index])
983 end_offset[index] = first + size;
984 }
985
986 buffer_mask |= index_bit;
987 }
988
989 /* Upload buffers. */
990 while (buffer_mask) {
991 unsigned start, end;
992 struct pipe_vertex_buffer *real_vb;
993 const uint8_t *ptr;
994
995 i = u_bit_scan(&buffer_mask);
996
997 start = start_offset[i];
998 end = end_offset[i];
999 assert(start < end);
1000
1001 real_vb = &mgr->real_vertex_buffer[i];
1002 ptr = mgr->vertex_buffer[i].buffer.user;
1003
1004 u_upload_data(mgr->pipe->stream_uploader,
1005 mgr->has_signed_vb_offset ? 0 : start,
1006 end - start, 4,
1007 ptr + start, &real_vb->buffer_offset, &real_vb->buffer.resource);
1008 if (!real_vb->buffer.resource)
1009 return PIPE_ERROR_OUT_OF_MEMORY;
1010
1011 real_vb->buffer_offset -= start;
1012 }
1013
1014 return PIPE_OK;
1015 }
1016
1017 static boolean u_vbuf_need_minmax_index(const struct u_vbuf *mgr)
1018 {
1019 /* See if there are any per-vertex attribs which will be uploaded or
1020 * translated. Use bitmasks to get the info instead of looping over vertex
1021 * elements. */
1022 return (mgr->ve->used_vb_mask &
1023 ((mgr->user_vb_mask |
1024 mgr->incompatible_vb_mask |
1025 mgr->ve->incompatible_vb_mask_any) &
1026 mgr->ve->noninstance_vb_mask_any &
1027 mgr->nonzero_stride_vb_mask)) != 0;
1028 }
1029
1030 static boolean u_vbuf_mapping_vertex_buffer_blocks(const struct u_vbuf *mgr)
1031 {
1032 /* Return true if there are hw buffers which don't need to be translated.
1033 *
1034 * We could query whether each buffer is busy, but that would
1035 * be way more costly than this. */
1036 return (mgr->ve->used_vb_mask &
1037 (~mgr->user_vb_mask &
1038 ~mgr->incompatible_vb_mask &
1039 mgr->ve->compatible_vb_mask_all &
1040 mgr->ve->noninstance_vb_mask_any &
1041 mgr->nonzero_stride_vb_mask)) != 0;
1042 }
1043
1044 static void
1045 u_vbuf_get_minmax_index_mapped(const struct pipe_draw_info *info,
1046 const void *indices, unsigned *out_min_index,
1047 unsigned *out_max_index)
1048 {
1049 if (!info->count) {
1050 *out_min_index = 0;
1051 *out_max_index = 0;
1052 return;
1053 }
1054
1055 switch (info->index_size) {
1056 case 4: {
1057 const unsigned *ui_indices = (const unsigned*)indices;
1058 unsigned max = 0;
1059 unsigned min = ~0u;
1060 if (info->primitive_restart) {
1061 for (unsigned i = 0; i < info->count; i++) {
1062 if (ui_indices[i] != info->restart_index) {
1063 if (ui_indices[i] > max) max = ui_indices[i];
1064 if (ui_indices[i] < min) min = ui_indices[i];
1065 }
1066 }
1067 }
1068 else {
1069 for (unsigned i = 0; i < info->count; i++) {
1070 if (ui_indices[i] > max) max = ui_indices[i];
1071 if (ui_indices[i] < min) min = ui_indices[i];
1072 }
1073 }
1074 *out_min_index = min;
1075 *out_max_index = max;
1076 break;
1077 }
1078 case 2: {
1079 const unsigned short *us_indices = (const unsigned short*)indices;
1080 unsigned short max = 0;
1081 unsigned short min = ~((unsigned short)0);
1082 if (info->primitive_restart) {
1083 for (unsigned i = 0; i < info->count; i++) {
1084 if (us_indices[i] != info->restart_index) {
1085 if (us_indices[i] > max) max = us_indices[i];
1086 if (us_indices[i] < min) min = us_indices[i];
1087 }
1088 }
1089 }
1090 else {
1091 for (unsigned i = 0; i < info->count; i++) {
1092 if (us_indices[i] > max) max = us_indices[i];
1093 if (us_indices[i] < min) min = us_indices[i];
1094 }
1095 }
1096 *out_min_index = min;
1097 *out_max_index = max;
1098 break;
1099 }
1100 case 1: {
1101 const unsigned char *ub_indices = (const unsigned char*)indices;
1102 unsigned char max = 0;
1103 unsigned char min = ~((unsigned char)0);
1104 if (info->primitive_restart) {
1105 for (unsigned i = 0; i < info->count; i++) {
1106 if (ub_indices[i] != info->restart_index) {
1107 if (ub_indices[i] > max) max = ub_indices[i];
1108 if (ub_indices[i] < min) min = ub_indices[i];
1109 }
1110 }
1111 }
1112 else {
1113 for (unsigned i = 0; i < info->count; i++) {
1114 if (ub_indices[i] > max) max = ub_indices[i];
1115 if (ub_indices[i] < min) min = ub_indices[i];
1116 }
1117 }
1118 *out_min_index = min;
1119 *out_max_index = max;
1120 break;
1121 }
1122 default:
1123 assert(0);
1124 }
1125 }
1126
1127 void u_vbuf_get_minmax_index(struct pipe_context *pipe,
1128 const struct pipe_draw_info *info,
1129 unsigned *out_min_index, unsigned *out_max_index)
1130 {
1131 struct pipe_transfer *transfer = NULL;
1132 const void *indices;
1133
1134 if (info->has_user_indices) {
1135 indices = (uint8_t*)info->index.user +
1136 info->start * info->index_size;
1137 } else {
1138 indices = pipe_buffer_map_range(pipe, info->index.resource,
1139 info->start * info->index_size,
1140 info->count * info->index_size,
1141 PIPE_TRANSFER_READ, &transfer);
1142 }
1143
1144 u_vbuf_get_minmax_index_mapped(info, indices, out_min_index, out_max_index);
1145
1146 if (transfer) {
1147 pipe_buffer_unmap(pipe, transfer);
1148 }
1149 }
1150
1151 static void u_vbuf_set_driver_vertex_buffers(struct u_vbuf *mgr)
1152 {
1153 struct pipe_context *pipe = mgr->pipe;
1154 unsigned start_slot, count;
1155
1156 start_slot = ffs(mgr->dirty_real_vb_mask) - 1;
1157 count = util_last_bit(mgr->dirty_real_vb_mask >> start_slot);
1158
1159 pipe->set_vertex_buffers(pipe, start_slot, count,
1160 mgr->real_vertex_buffer + start_slot);
1161 mgr->dirty_real_vb_mask = 0;
1162 }
1163
1164 static void
1165 u_vbuf_split_indexed_multidraw(struct u_vbuf *mgr, struct pipe_draw_info *info,
1166 unsigned *indirect_data, unsigned stride,
1167 unsigned draw_count)
1168 {
1169 assert(info->index_size);
1170 info->indirect = NULL;
1171
1172 for (unsigned i = 0; i < draw_count; i++) {
1173 unsigned offset = i * stride / 4;
1174
1175 info->count = indirect_data[offset + 0];
1176 info->instance_count = indirect_data[offset + 1];
1177
1178 if (!info->count || !info->instance_count)
1179 continue;
1180
1181 info->start = indirect_data[offset + 2];
1182 info->index_bias = indirect_data[offset + 3];
1183 info->start_instance = indirect_data[offset + 4];
1184
1185 u_vbuf_draw_vbo(mgr, info);
1186 }
1187 }
1188
1189 void u_vbuf_draw_vbo(struct u_vbuf *mgr, const struct pipe_draw_info *info)
1190 {
1191 struct pipe_context *pipe = mgr->pipe;
1192 int start_vertex;
1193 unsigned min_index;
1194 unsigned num_vertices;
1195 boolean unroll_indices = FALSE;
1196 const uint32_t used_vb_mask = mgr->ve->used_vb_mask;
1197 uint32_t user_vb_mask = mgr->user_vb_mask & used_vb_mask;
1198 const uint32_t incompatible_vb_mask =
1199 mgr->incompatible_vb_mask & used_vb_mask;
1200 struct pipe_draw_info new_info;
1201
1202 /* Normal draw. No fallback and no user buffers. */
1203 if (!incompatible_vb_mask &&
1204 !mgr->ve->incompatible_elem_mask &&
1205 !user_vb_mask) {
1206
1207 /* Set vertex buffers if needed. */
1208 if (mgr->dirty_real_vb_mask & used_vb_mask) {
1209 u_vbuf_set_driver_vertex_buffers(mgr);
1210 }
1211
1212 pipe->draw_vbo(pipe, info);
1213 return;
1214 }
1215
1216 new_info = *info;
1217
1218 /* Handle indirect (multi)draws. */
1219 if (new_info.indirect) {
1220 const struct pipe_draw_indirect_info *indirect = new_info.indirect;
1221 unsigned draw_count = 0;
1222
1223 /* Get the number of draws. */
1224 if (indirect->indirect_draw_count) {
1225 pipe_buffer_read(pipe, indirect->indirect_draw_count,
1226 indirect->indirect_draw_count_offset,
1227 4, &draw_count);
1228 } else {
1229 draw_count = indirect->draw_count;
1230 }
1231
1232 if (!draw_count)
1233 return;
1234
1235 unsigned data_size = (draw_count - 1) * indirect->stride +
1236 (new_info.index_size ? 20 : 16);
1237 unsigned *data = malloc(data_size);
1238 if (!data)
1239 return; /* report an error? */
1240
1241 /* Read the used buffer range only once, because the read can be
1242 * uncached.
1243 */
1244 pipe_buffer_read(pipe, indirect->buffer, indirect->offset, data_size,
1245 data);
1246
1247 if (info->index_size) {
1248 /* Indexed multidraw. */
1249 unsigned index_bias0 = data[3];
1250 bool index_bias_same = true;
1251
1252 /* If we invoke the translate path, we have to split the multidraw. */
1253 if (incompatible_vb_mask ||
1254 mgr->ve->incompatible_elem_mask) {
1255 u_vbuf_split_indexed_multidraw(mgr, &new_info, data,
1256 indirect->stride, draw_count);
1257 free(data);
1258 return;
1259 }
1260
1261 /* See if index_bias is the same for all draws. */
1262 for (unsigned i = 1; i < draw_count; i++) {
1263 if (data[i * indirect->stride / 4 + 3] != index_bias0) {
1264 index_bias_same = false;
1265 break;
1266 }
1267 }
1268
1269 /* Split the multidraw if index_bias is different. */
1270 if (!index_bias_same) {
1271 u_vbuf_split_indexed_multidraw(mgr, &new_info, data,
1272 indirect->stride, draw_count);
1273 free(data);
1274 return;
1275 }
1276
1277 /* If we don't need to use the translate path and index_bias is
1278 * the same, we can process the multidraw with the time complexity
1279 * equal to 1 draw call (except for the index range computation).
1280 * We only need to compute the index range covering all draw calls
1281 * of the multidraw.
1282 *
1283 * The driver will not look at these values because indirect != NULL.
1284 * These values determine the user buffer bounds to upload.
1285 */
1286 new_info.index_bias = index_bias0;
1287 new_info.min_index = ~0u;
1288 new_info.max_index = 0;
1289 new_info.start_instance = ~0u;
1290 unsigned end_instance = 0;
1291
1292 struct pipe_transfer *transfer = NULL;
1293 const uint8_t *indices;
1294
1295 if (info->has_user_indices) {
1296 indices = (uint8_t*)info->index.user;
1297 } else {
1298 indices = (uint8_t*)pipe_buffer_map(pipe, info->index.resource,
1299 PIPE_TRANSFER_READ, &transfer);
1300 }
1301
1302 for (unsigned i = 0; i < draw_count; i++) {
1303 unsigned offset = i * indirect->stride / 4;
1304 unsigned start = data[offset + 2];
1305 unsigned count = data[offset + 0];
1306 unsigned start_instance = data[offset + 4];
1307 unsigned instance_count = data[offset + 1];
1308
1309 if (!count || !instance_count)
1310 continue;
1311
1312 /* Update the ranges of instances. */
1313 new_info.start_instance = MIN2(new_info.start_instance,
1314 start_instance);
1315 end_instance = MAX2(end_instance, start_instance + instance_count);
1316
1317 /* Update the index range. */
1318 unsigned min, max;
1319 new_info.count = count; /* only used by get_minmax_index */
1320 u_vbuf_get_minmax_index_mapped(&new_info,
1321 indices +
1322 new_info.index_size * start,
1323 &min, &max);
1324
1325 new_info.min_index = MIN2(new_info.min_index, min);
1326 new_info.max_index = MAX2(new_info.max_index, max);
1327 }
1328 free(data);
1329
1330 if (transfer)
1331 pipe_buffer_unmap(pipe, transfer);
1332
1333 /* Set the final instance count. */
1334 new_info.instance_count = end_instance - new_info.start_instance;
1335
1336 if (new_info.start_instance == ~0u || !new_info.instance_count)
1337 return;
1338 } else {
1339 /* Non-indexed multidraw.
1340 *
1341 * Keep the draw call indirect and compute minimums & maximums,
1342 * which will determine the user buffer bounds to upload, but
1343 * the driver will not look at these values because indirect != NULL.
1344 *
1345 * This efficiently processes the multidraw with the time complexity
1346 * equal to 1 draw call.
1347 */
1348 new_info.start = ~0u;
1349 new_info.start_instance = ~0u;
1350 unsigned end_vertex = 0;
1351 unsigned end_instance = 0;
1352
1353 for (unsigned i = 0; i < draw_count; i++) {
1354 unsigned offset = i * indirect->stride / 4;
1355 unsigned start = data[offset + 2];
1356 unsigned count = data[offset + 0];
1357 unsigned start_instance = data[offset + 3];
1358 unsigned instance_count = data[offset + 1];
1359
1360 new_info.start = MIN2(new_info.start, start);
1361 new_info.start_instance = MIN2(new_info.start_instance,
1362 start_instance);
1363
1364 end_vertex = MAX2(end_vertex, start + count);
1365 end_instance = MAX2(end_instance, start_instance + instance_count);
1366 }
1367 free(data);
1368
1369 /* Set the final counts. */
1370 new_info.count = end_vertex - new_info.start;
1371 new_info.instance_count = end_instance - new_info.start_instance;
1372
1373 if (new_info.start == ~0u || !new_info.count || !new_info.instance_count)
1374 return;
1375 }
1376 }
1377
1378 if (new_info.index_size) {
1379 /* See if anything needs to be done for per-vertex attribs. */
1380 if (u_vbuf_need_minmax_index(mgr)) {
1381 unsigned max_index;
1382
1383 if (new_info.max_index != ~0u) {
1384 min_index = new_info.min_index;
1385 max_index = new_info.max_index;
1386 } else {
1387 u_vbuf_get_minmax_index(mgr->pipe, &new_info,
1388 &min_index, &max_index);
1389 }
1390
1391 assert(min_index <= max_index);
1392
1393 start_vertex = min_index + new_info.index_bias;
1394 num_vertices = max_index + 1 - min_index;
1395
1396 /* Primitive restart doesn't work when unrolling indices.
1397 * We would have to break this drawing operation into several ones. */
1398 /* Use some heuristic to see if unrolling indices improves
1399 * performance. */
1400 if (!info->indirect &&
1401 !new_info.primitive_restart &&
1402 num_vertices > new_info.count*2 &&
1403 num_vertices - new_info.count > 32 &&
1404 !u_vbuf_mapping_vertex_buffer_blocks(mgr)) {
1405 unroll_indices = TRUE;
1406 user_vb_mask &= ~(mgr->nonzero_stride_vb_mask &
1407 mgr->ve->noninstance_vb_mask_any);
1408 }
1409 } else {
1410 /* Nothing to do for per-vertex attribs. */
1411 start_vertex = 0;
1412 num_vertices = 0;
1413 min_index = 0;
1414 }
1415 } else {
1416 start_vertex = new_info.start;
1417 num_vertices = new_info.count;
1418 min_index = 0;
1419 }
1420
1421 /* Translate vertices with non-native layouts or formats. */
1422 if (unroll_indices ||
1423 incompatible_vb_mask ||
1424 mgr->ve->incompatible_elem_mask) {
1425 if (!u_vbuf_translate_begin(mgr, &new_info, start_vertex, num_vertices,
1426 min_index, unroll_indices)) {
1427 debug_warn_once("u_vbuf_translate_begin() failed");
1428 return;
1429 }
1430
1431 if (unroll_indices) {
1432 new_info.index_size = 0;
1433 new_info.index_bias = 0;
1434 new_info.min_index = 0;
1435 new_info.max_index = new_info.count - 1;
1436 new_info.start = 0;
1437 }
1438
1439 user_vb_mask &= ~(incompatible_vb_mask |
1440 mgr->ve->incompatible_vb_mask_all);
1441 }
1442
1443 /* Upload user buffers. */
1444 if (user_vb_mask) {
1445 if (u_vbuf_upload_buffers(mgr, start_vertex, num_vertices,
1446 new_info.start_instance,
1447 new_info.instance_count) != PIPE_OK) {
1448 debug_warn_once("u_vbuf_upload_buffers() failed");
1449 return;
1450 }
1451
1452 mgr->dirty_real_vb_mask |= user_vb_mask;
1453 }
1454
1455 /*
1456 if (unroll_indices) {
1457 printf("unrolling indices: start_vertex = %i, num_vertices = %i\n",
1458 start_vertex, num_vertices);
1459 util_dump_draw_info(stdout, info);
1460 printf("\n");
1461 }
1462
1463 unsigned i;
1464 for (i = 0; i < mgr->nr_vertex_buffers; i++) {
1465 printf("input %i: ", i);
1466 util_dump_vertex_buffer(stdout, mgr->vertex_buffer+i);
1467 printf("\n");
1468 }
1469 for (i = 0; i < mgr->nr_real_vertex_buffers; i++) {
1470 printf("real %i: ", i);
1471 util_dump_vertex_buffer(stdout, mgr->real_vertex_buffer+i);
1472 printf("\n");
1473 }
1474 */
1475
1476 u_upload_unmap(pipe->stream_uploader);
1477 u_vbuf_set_driver_vertex_buffers(mgr);
1478
1479 pipe->draw_vbo(pipe, &new_info);
1480
1481 if (mgr->using_translate) {
1482 u_vbuf_translate_end(mgr);
1483 }
1484 }
1485
1486 void u_vbuf_save_vertex_elements(struct u_vbuf *mgr)
1487 {
1488 assert(!mgr->ve_saved);
1489 mgr->ve_saved = mgr->ve;
1490 }
1491
1492 void u_vbuf_restore_vertex_elements(struct u_vbuf *mgr)
1493 {
1494 if (mgr->ve != mgr->ve_saved) {
1495 struct pipe_context *pipe = mgr->pipe;
1496
1497 mgr->ve = mgr->ve_saved;
1498 pipe->bind_vertex_elements_state(pipe,
1499 mgr->ve ? mgr->ve->driver_cso : NULL);
1500 }
1501 mgr->ve_saved = NULL;
1502 }
1503
1504 void u_vbuf_save_vertex_buffer0(struct u_vbuf *mgr)
1505 {
1506 pipe_vertex_buffer_reference(&mgr->vertex_buffer0_saved,
1507 &mgr->vertex_buffer[0]);
1508 }
1509
1510 void u_vbuf_restore_vertex_buffer0(struct u_vbuf *mgr)
1511 {
1512 u_vbuf_set_vertex_buffers(mgr, 0, 1, &mgr->vertex_buffer0_saved);
1513 pipe_vertex_buffer_unreference(&mgr->vertex_buffer0_saved);
1514 }