gallium/vbuf: avoid segfault when we get invalid glDrawRangeElements()
[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/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
146 struct pipe_context *pipe;
147 struct translate_cache *translate_cache;
148 struct cso_cache *cso_cache;
149
150 /* This is what was set in set_vertex_buffers.
151 * May contain user buffers. */
152 struct pipe_vertex_buffer vertex_buffer[PIPE_MAX_ATTRIBS];
153 uint32_t enabled_vb_mask;
154
155 /* Saved vertex buffer. */
156 unsigned aux_vertex_buffer_slot;
157 struct pipe_vertex_buffer aux_vertex_buffer_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 };
184
185 static void *
186 u_vbuf_create_vertex_elements(struct u_vbuf *mgr, unsigned count,
187 const struct pipe_vertex_element *attribs);
188 static void u_vbuf_delete_vertex_elements(struct u_vbuf *mgr, void *cso);
189
190 static const struct {
191 enum pipe_format from, to;
192 } vbuf_format_fallbacks[] = {
193 { PIPE_FORMAT_R32_FIXED, PIPE_FORMAT_R32_FLOAT },
194 { PIPE_FORMAT_R32G32_FIXED, PIPE_FORMAT_R32G32_FLOAT },
195 { PIPE_FORMAT_R32G32B32_FIXED, PIPE_FORMAT_R32G32B32_FLOAT },
196 { PIPE_FORMAT_R32G32B32A32_FIXED, PIPE_FORMAT_R32G32B32A32_FLOAT },
197 { PIPE_FORMAT_R16_FLOAT, PIPE_FORMAT_R32_FLOAT },
198 { PIPE_FORMAT_R16G16_FLOAT, PIPE_FORMAT_R32G32_FLOAT },
199 { PIPE_FORMAT_R16G16B16_FLOAT, PIPE_FORMAT_R32G32B32_FLOAT },
200 { PIPE_FORMAT_R16G16B16A16_FLOAT, PIPE_FORMAT_R32G32B32A32_FLOAT },
201 { PIPE_FORMAT_R64_FLOAT, PIPE_FORMAT_R32_FLOAT },
202 { PIPE_FORMAT_R64G64_FLOAT, PIPE_FORMAT_R32G32_FLOAT },
203 { PIPE_FORMAT_R64G64B64_FLOAT, PIPE_FORMAT_R32G32B32_FLOAT },
204 { PIPE_FORMAT_R64G64B64A64_FLOAT, PIPE_FORMAT_R32G32B32A32_FLOAT },
205 { PIPE_FORMAT_R32_UNORM, PIPE_FORMAT_R32_FLOAT },
206 { PIPE_FORMAT_R32G32_UNORM, PIPE_FORMAT_R32G32_FLOAT },
207 { PIPE_FORMAT_R32G32B32_UNORM, PIPE_FORMAT_R32G32B32_FLOAT },
208 { PIPE_FORMAT_R32G32B32A32_UNORM, PIPE_FORMAT_R32G32B32A32_FLOAT },
209 { PIPE_FORMAT_R32_SNORM, PIPE_FORMAT_R32_FLOAT },
210 { PIPE_FORMAT_R32G32_SNORM, PIPE_FORMAT_R32G32_FLOAT },
211 { PIPE_FORMAT_R32G32B32_SNORM, PIPE_FORMAT_R32G32B32_FLOAT },
212 { PIPE_FORMAT_R32G32B32A32_SNORM, PIPE_FORMAT_R32G32B32A32_FLOAT },
213 { PIPE_FORMAT_R32_USCALED, PIPE_FORMAT_R32_FLOAT },
214 { PIPE_FORMAT_R32G32_USCALED, PIPE_FORMAT_R32G32_FLOAT },
215 { PIPE_FORMAT_R32G32B32_USCALED, PIPE_FORMAT_R32G32B32_FLOAT },
216 { PIPE_FORMAT_R32G32B32A32_USCALED, PIPE_FORMAT_R32G32B32A32_FLOAT },
217 { PIPE_FORMAT_R32_SSCALED, PIPE_FORMAT_R32_FLOAT },
218 { PIPE_FORMAT_R32G32_SSCALED, PIPE_FORMAT_R32G32_FLOAT },
219 { PIPE_FORMAT_R32G32B32_SSCALED, PIPE_FORMAT_R32G32B32_FLOAT },
220 { PIPE_FORMAT_R32G32B32A32_SSCALED, PIPE_FORMAT_R32G32B32A32_FLOAT },
221 { PIPE_FORMAT_R16_UNORM, PIPE_FORMAT_R32_FLOAT },
222 { PIPE_FORMAT_R16G16_UNORM, PIPE_FORMAT_R32G32_FLOAT },
223 { PIPE_FORMAT_R16G16B16_UNORM, PIPE_FORMAT_R32G32B32_FLOAT },
224 { PIPE_FORMAT_R16G16B16A16_UNORM, PIPE_FORMAT_R32G32B32A32_FLOAT },
225 { PIPE_FORMAT_R16_SNORM, PIPE_FORMAT_R32_FLOAT },
226 { PIPE_FORMAT_R16G16_SNORM, PIPE_FORMAT_R32G32_FLOAT },
227 { PIPE_FORMAT_R16G16B16_SNORM, PIPE_FORMAT_R32G32B32_FLOAT },
228 { PIPE_FORMAT_R16G16B16A16_SNORM, PIPE_FORMAT_R32G32B32A32_FLOAT },
229 { PIPE_FORMAT_R16_USCALED, PIPE_FORMAT_R32_FLOAT },
230 { PIPE_FORMAT_R16G16_USCALED, PIPE_FORMAT_R32G32_FLOAT },
231 { PIPE_FORMAT_R16G16B16_USCALED, PIPE_FORMAT_R32G32B32_FLOAT },
232 { PIPE_FORMAT_R16G16B16A16_USCALED, PIPE_FORMAT_R32G32B32A32_FLOAT },
233 { PIPE_FORMAT_R16_SSCALED, PIPE_FORMAT_R32_FLOAT },
234 { PIPE_FORMAT_R16G16_SSCALED, PIPE_FORMAT_R32G32_FLOAT },
235 { PIPE_FORMAT_R16G16B16_SSCALED, PIPE_FORMAT_R32G32B32_FLOAT },
236 { PIPE_FORMAT_R16G16B16A16_SSCALED, PIPE_FORMAT_R32G32B32A32_FLOAT },
237 { PIPE_FORMAT_R8_UNORM, PIPE_FORMAT_R32_FLOAT },
238 { PIPE_FORMAT_R8G8_UNORM, PIPE_FORMAT_R32G32_FLOAT },
239 { PIPE_FORMAT_R8G8B8_UNORM, PIPE_FORMAT_R32G32B32_FLOAT },
240 { PIPE_FORMAT_R8G8B8A8_UNORM, PIPE_FORMAT_R32G32B32A32_FLOAT },
241 { PIPE_FORMAT_R8_SNORM, PIPE_FORMAT_R32_FLOAT },
242 { PIPE_FORMAT_R8G8_SNORM, PIPE_FORMAT_R32G32_FLOAT },
243 { PIPE_FORMAT_R8G8B8_SNORM, PIPE_FORMAT_R32G32B32_FLOAT },
244 { PIPE_FORMAT_R8G8B8A8_SNORM, PIPE_FORMAT_R32G32B32A32_FLOAT },
245 { PIPE_FORMAT_R8_USCALED, PIPE_FORMAT_R32_FLOAT },
246 { PIPE_FORMAT_R8G8_USCALED, PIPE_FORMAT_R32G32_FLOAT },
247 { PIPE_FORMAT_R8G8B8_USCALED, PIPE_FORMAT_R32G32B32_FLOAT },
248 { PIPE_FORMAT_R8G8B8A8_USCALED, PIPE_FORMAT_R32G32B32A32_FLOAT },
249 { PIPE_FORMAT_R8_SSCALED, PIPE_FORMAT_R32_FLOAT },
250 { PIPE_FORMAT_R8G8_SSCALED, PIPE_FORMAT_R32G32_FLOAT },
251 { PIPE_FORMAT_R8G8B8_SSCALED, PIPE_FORMAT_R32G32B32_FLOAT },
252 { PIPE_FORMAT_R8G8B8A8_SSCALED, PIPE_FORMAT_R32G32B32A32_FLOAT },
253 };
254
255 boolean u_vbuf_get_caps(struct pipe_screen *screen, struct u_vbuf_caps *caps,
256 unsigned flags)
257 {
258 unsigned i;
259 boolean fallback = FALSE;
260
261 /* I'd rather have a bitfield of which formats are supported and a static
262 * table of the translations indexed by format, but since we don't have C99
263 * we can't easily make a sparsely-populated table indexed by format. So,
264 * we construct the sparse table here.
265 */
266 for (i = 0; i < PIPE_FORMAT_COUNT; i++)
267 caps->format_translation[i] = i;
268
269 for (i = 0; i < ARRAY_SIZE(vbuf_format_fallbacks); i++) {
270 enum pipe_format format = vbuf_format_fallbacks[i].from;
271
272 if (!screen->is_format_supported(screen, format, PIPE_BUFFER, 0,
273 PIPE_BIND_VERTEX_BUFFER)) {
274 caps->format_translation[format] = vbuf_format_fallbacks[i].to;
275 fallback = TRUE;
276 }
277 }
278
279 caps->buffer_offset_unaligned =
280 !screen->get_param(screen,
281 PIPE_CAP_VERTEX_BUFFER_OFFSET_4BYTE_ALIGNED_ONLY);
282 caps->buffer_stride_unaligned =
283 !screen->get_param(screen,
284 PIPE_CAP_VERTEX_BUFFER_STRIDE_4BYTE_ALIGNED_ONLY);
285 caps->velem_src_offset_unaligned =
286 !screen->get_param(screen,
287 PIPE_CAP_VERTEX_ELEMENT_SRC_OFFSET_4BYTE_ALIGNED_ONLY);
288 caps->user_vertex_buffers =
289 screen->get_param(screen, PIPE_CAP_USER_VERTEX_BUFFERS);
290
291 if (!caps->buffer_offset_unaligned ||
292 !caps->buffer_stride_unaligned ||
293 !caps->velem_src_offset_unaligned ||
294 (!(flags & U_VBUF_FLAG_NO_USER_VBOS) && !caps->user_vertex_buffers)) {
295 fallback = TRUE;
296 }
297
298 return fallback;
299 }
300
301 struct u_vbuf *
302 u_vbuf_create(struct pipe_context *pipe,
303 struct u_vbuf_caps *caps, unsigned aux_vertex_buffer_index)
304 {
305 struct u_vbuf *mgr = CALLOC_STRUCT(u_vbuf);
306
307 mgr->caps = *caps;
308 mgr->aux_vertex_buffer_slot = aux_vertex_buffer_index;
309 mgr->pipe = pipe;
310 mgr->cso_cache = cso_cache_create();
311 mgr->translate_cache = translate_cache_create();
312 memset(mgr->fallback_vbs, ~0, sizeof(mgr->fallback_vbs));
313
314 return mgr;
315 }
316
317 /* u_vbuf uses its own caching for vertex elements, because it needs to keep
318 * its own preprocessed state per vertex element CSO. */
319 static struct u_vbuf_elements *
320 u_vbuf_set_vertex_elements_internal(struct u_vbuf *mgr, unsigned count,
321 const struct pipe_vertex_element *states)
322 {
323 struct pipe_context *pipe = mgr->pipe;
324 unsigned key_size, hash_key;
325 struct cso_hash_iter iter;
326 struct u_vbuf_elements *ve;
327 struct cso_velems_state velems_state;
328
329 /* need to include the count into the stored state data too. */
330 key_size = sizeof(struct pipe_vertex_element) * count + sizeof(unsigned);
331 velems_state.count = count;
332 memcpy(velems_state.velems, states,
333 sizeof(struct pipe_vertex_element) * count);
334 hash_key = cso_construct_key((void*)&velems_state, key_size);
335 iter = cso_find_state_template(mgr->cso_cache, hash_key, CSO_VELEMENTS,
336 (void*)&velems_state, key_size);
337
338 if (cso_hash_iter_is_null(iter)) {
339 struct cso_velements *cso = MALLOC_STRUCT(cso_velements);
340 memcpy(&cso->state, &velems_state, key_size);
341 cso->data = u_vbuf_create_vertex_elements(mgr, count, states);
342 cso->delete_state = (cso_state_callback)u_vbuf_delete_vertex_elements;
343 cso->context = (void*)mgr;
344
345 iter = cso_insert_state(mgr->cso_cache, hash_key, CSO_VELEMENTS, cso);
346 ve = cso->data;
347 } else {
348 ve = ((struct cso_velements *)cso_hash_iter_data(iter))->data;
349 }
350
351 assert(ve);
352
353 if (ve != mgr->ve)
354 pipe->bind_vertex_elements_state(pipe, ve->driver_cso);
355
356 return ve;
357 }
358
359 void u_vbuf_set_vertex_elements(struct u_vbuf *mgr, unsigned count,
360 const struct pipe_vertex_element *states)
361 {
362 mgr->ve = u_vbuf_set_vertex_elements_internal(mgr, count, states);
363 }
364
365 void u_vbuf_destroy(struct u_vbuf *mgr)
366 {
367 struct pipe_screen *screen = mgr->pipe->screen;
368 unsigned i;
369 const unsigned num_vb = screen->get_shader_param(screen, PIPE_SHADER_VERTEX,
370 PIPE_SHADER_CAP_MAX_INPUTS);
371
372 mgr->pipe->set_vertex_buffers(mgr->pipe, 0, num_vb, NULL);
373
374 for (i = 0; i < PIPE_MAX_ATTRIBS; i++)
375 pipe_vertex_buffer_unreference(&mgr->vertex_buffer[i]);
376 for (i = 0; i < PIPE_MAX_ATTRIBS; i++)
377 pipe_vertex_buffer_unreference(&mgr->real_vertex_buffer[i]);
378
379 pipe_vertex_buffer_unreference(&mgr->aux_vertex_buffer_saved);
380
381 translate_cache_destroy(mgr->translate_cache);
382 cso_cache_delete(mgr->cso_cache);
383 FREE(mgr);
384 }
385
386 static enum pipe_error
387 u_vbuf_translate_buffers(struct u_vbuf *mgr, struct translate_key *key,
388 const struct pipe_draw_info *info,
389 unsigned vb_mask, unsigned out_vb,
390 int start_vertex, unsigned num_vertices,
391 int min_index, boolean unroll_indices)
392 {
393 struct translate *tr;
394 struct pipe_transfer *vb_transfer[PIPE_MAX_ATTRIBS] = {0};
395 struct pipe_resource *out_buffer = NULL;
396 uint8_t *out_map;
397 unsigned out_offset, mask;
398
399 /* Get a translate object. */
400 tr = translate_cache_find(mgr->translate_cache, key);
401
402 /* Map buffers we want to translate. */
403 mask = vb_mask;
404 while (mask) {
405 struct pipe_vertex_buffer *vb;
406 unsigned offset;
407 uint8_t *map;
408 unsigned i = u_bit_scan(&mask);
409
410 vb = &mgr->vertex_buffer[i];
411 offset = vb->buffer_offset + vb->stride * start_vertex;
412
413 if (vb->is_user_buffer) {
414 map = (uint8_t*)vb->buffer.user + offset;
415 } else {
416 unsigned size = vb->stride ? num_vertices * vb->stride
417 : sizeof(double)*4;
418
419 if (offset + size > vb->buffer.resource->width0) {
420 /* Don't try to map past end of buffer. This often happens when
421 * we're translating an attribute that's at offset > 0 from the
422 * start of the vertex. If we'd subtract attrib's offset from
423 * the size, this probably wouldn't happen.
424 */
425 size = vb->buffer.resource->width0 - offset;
426
427 /* Also adjust num_vertices. A common user error is to call
428 * glDrawRangeElements() with incorrect 'end' argument. The 'end
429 * value should be the max index value, but people often
430 * accidentally add one to this value. This adjustment avoids
431 * crashing (by reading past the end of a hardware buffer mapping)
432 * when people do that.
433 */
434 num_vertices = (size + vb->stride - 1) / vb->stride;
435 }
436
437 map = pipe_buffer_map_range(mgr->pipe, vb->buffer.resource, offset, size,
438 PIPE_TRANSFER_READ, &vb_transfer[i]);
439 }
440
441 /* Subtract min_index so that indexing with the index buffer works. */
442 if (unroll_indices) {
443 map -= (ptrdiff_t)vb->stride * min_index;
444 }
445
446 tr->set_buffer(tr, i, map, vb->stride, ~0);
447 }
448
449 /* Translate. */
450 if (unroll_indices) {
451 struct pipe_transfer *transfer = NULL;
452 const unsigned offset = info->start * info->index_size;
453 uint8_t *map;
454
455 /* Create and map the output buffer. */
456 u_upload_alloc(mgr->pipe->stream_uploader, 0,
457 key->output_stride * info->count, 4,
458 &out_offset, &out_buffer,
459 (void**)&out_map);
460 if (!out_buffer)
461 return PIPE_ERROR_OUT_OF_MEMORY;
462
463 if (info->has_user_indices) {
464 map = (uint8_t*)info->index.user + offset;
465 } else {
466 map = pipe_buffer_map_range(mgr->pipe, info->index.resource, offset,
467 info->count * info->index_size,
468 PIPE_TRANSFER_READ, &transfer);
469 }
470
471 switch (info->index_size) {
472 case 4:
473 tr->run_elts(tr, (unsigned*)map, info->count, 0, 0, out_map);
474 break;
475 case 2:
476 tr->run_elts16(tr, (uint16_t*)map, info->count, 0, 0, out_map);
477 break;
478 case 1:
479 tr->run_elts8(tr, map, info->count, 0, 0, out_map);
480 break;
481 }
482
483 if (transfer) {
484 pipe_buffer_unmap(mgr->pipe, transfer);
485 }
486 } else {
487 /* Create and map the output buffer. */
488 u_upload_alloc(mgr->pipe->stream_uploader,
489 key->output_stride * start_vertex,
490 key->output_stride * num_vertices, 4,
491 &out_offset, &out_buffer,
492 (void**)&out_map);
493 if (!out_buffer)
494 return PIPE_ERROR_OUT_OF_MEMORY;
495
496 out_offset -= key->output_stride * start_vertex;
497
498 tr->run(tr, 0, num_vertices, 0, 0, out_map);
499 }
500
501 /* Unmap all buffers. */
502 mask = vb_mask;
503 while (mask) {
504 unsigned i = u_bit_scan(&mask);
505
506 if (vb_transfer[i]) {
507 pipe_buffer_unmap(mgr->pipe, vb_transfer[i]);
508 }
509 }
510
511 /* Setup the new vertex buffer. */
512 mgr->real_vertex_buffer[out_vb].buffer_offset = out_offset;
513 mgr->real_vertex_buffer[out_vb].stride = key->output_stride;
514
515 /* Move the buffer reference. */
516 pipe_resource_reference(
517 &mgr->real_vertex_buffer[out_vb].buffer.resource, NULL);
518 mgr->real_vertex_buffer[out_vb].buffer.resource = out_buffer;
519
520 return PIPE_OK;
521 }
522
523 static boolean
524 u_vbuf_translate_find_free_vb_slots(struct u_vbuf *mgr,
525 unsigned mask[VB_NUM])
526 {
527 unsigned type;
528 unsigned fallback_vbs[VB_NUM];
529 /* Set the bit for each buffer which is incompatible, or isn't set. */
530 uint32_t unused_vb_mask =
531 mgr->ve->incompatible_vb_mask_all | mgr->incompatible_vb_mask |
532 ~mgr->enabled_vb_mask;
533
534 memset(fallback_vbs, ~0, sizeof(fallback_vbs));
535
536 /* Find free slots for each type if needed. */
537 for (type = 0; type < VB_NUM; type++) {
538 if (mask[type]) {
539 uint32_t index;
540
541 if (!unused_vb_mask) {
542 return FALSE;
543 }
544
545 index = ffs(unused_vb_mask) - 1;
546 fallback_vbs[type] = index;
547 unused_vb_mask &= ~(1 << index);
548 /*printf("found slot=%i for type=%i\n", index, type);*/
549 }
550 }
551
552 for (type = 0; type < VB_NUM; type++) {
553 if (mask[type]) {
554 mgr->dirty_real_vb_mask |= 1 << fallback_vbs[type];
555 }
556 }
557
558 memcpy(mgr->fallback_vbs, fallback_vbs, sizeof(fallback_vbs));
559 return TRUE;
560 }
561
562 static boolean
563 u_vbuf_translate_begin(struct u_vbuf *mgr,
564 const struct pipe_draw_info *info,
565 int start_vertex, unsigned num_vertices,
566 int min_index, boolean unroll_indices)
567 {
568 unsigned mask[VB_NUM] = {0};
569 struct translate_key key[VB_NUM];
570 unsigned elem_index[VB_NUM][PIPE_MAX_ATTRIBS]; /* ... into key.elements */
571 unsigned i, type;
572 const unsigned incompatible_vb_mask = mgr->incompatible_vb_mask &
573 mgr->ve->used_vb_mask;
574
575 const int start[VB_NUM] = {
576 start_vertex, /* VERTEX */
577 info->start_instance, /* INSTANCE */
578 0 /* CONST */
579 };
580
581 const unsigned num[VB_NUM] = {
582 num_vertices, /* VERTEX */
583 info->instance_count, /* INSTANCE */
584 1 /* CONST */
585 };
586
587 memset(key, 0, sizeof(key));
588 memset(elem_index, ~0, sizeof(elem_index));
589
590 /* See if there are vertex attribs of each type to translate and
591 * which ones. */
592 for (i = 0; i < mgr->ve->count; i++) {
593 unsigned vb_index = mgr->ve->ve[i].vertex_buffer_index;
594
595 if (!mgr->vertex_buffer[vb_index].stride) {
596 if (!(mgr->ve->incompatible_elem_mask & (1 << i)) &&
597 !(incompatible_vb_mask & (1 << vb_index))) {
598 continue;
599 }
600 mask[VB_CONST] |= 1 << vb_index;
601 } else if (mgr->ve->ve[i].instance_divisor) {
602 if (!(mgr->ve->incompatible_elem_mask & (1 << i)) &&
603 !(incompatible_vb_mask & (1 << vb_index))) {
604 continue;
605 }
606 mask[VB_INSTANCE] |= 1 << vb_index;
607 } else {
608 if (!unroll_indices &&
609 !(mgr->ve->incompatible_elem_mask & (1 << i)) &&
610 !(incompatible_vb_mask & (1 << vb_index))) {
611 continue;
612 }
613 mask[VB_VERTEX] |= 1 << vb_index;
614 }
615 }
616
617 assert(mask[VB_VERTEX] || mask[VB_INSTANCE] || mask[VB_CONST]);
618
619 /* Find free vertex buffer slots. */
620 if (!u_vbuf_translate_find_free_vb_slots(mgr, mask)) {
621 return FALSE;
622 }
623
624 /* Initialize the translate keys. */
625 for (i = 0; i < mgr->ve->count; i++) {
626 struct translate_key *k;
627 struct translate_element *te;
628 enum pipe_format output_format = mgr->ve->native_format[i];
629 unsigned bit, vb_index = mgr->ve->ve[i].vertex_buffer_index;
630 bit = 1 << vb_index;
631
632 if (!(mgr->ve->incompatible_elem_mask & (1 << i)) &&
633 !(incompatible_vb_mask & (1 << vb_index)) &&
634 (!unroll_indices || !(mask[VB_VERTEX] & bit))) {
635 continue;
636 }
637
638 /* Set type to what we will translate.
639 * Whether vertex, instance, or constant attribs. */
640 for (type = 0; type < VB_NUM; type++) {
641 if (mask[type] & bit) {
642 break;
643 }
644 }
645 assert(type < VB_NUM);
646 if (mgr->ve->ve[i].src_format != output_format)
647 assert(translate_is_output_format_supported(output_format));
648 /*printf("velem=%i type=%i\n", i, type);*/
649
650 /* Add the vertex element. */
651 k = &key[type];
652 elem_index[type][i] = k->nr_elements;
653
654 te = &k->element[k->nr_elements];
655 te->type = TRANSLATE_ELEMENT_NORMAL;
656 te->instance_divisor = 0;
657 te->input_buffer = vb_index;
658 te->input_format = mgr->ve->ve[i].src_format;
659 te->input_offset = mgr->ve->ve[i].src_offset;
660 te->output_format = output_format;
661 te->output_offset = k->output_stride;
662
663 k->output_stride += mgr->ve->native_format_size[i];
664 k->nr_elements++;
665 }
666
667 /* Translate buffers. */
668 for (type = 0; type < VB_NUM; type++) {
669 if (key[type].nr_elements) {
670 enum pipe_error err;
671 err = u_vbuf_translate_buffers(mgr, &key[type], info, mask[type],
672 mgr->fallback_vbs[type],
673 start[type], num[type], min_index,
674 unroll_indices && type == VB_VERTEX);
675 if (err != PIPE_OK)
676 return FALSE;
677
678 /* Fixup the stride for constant attribs. */
679 if (type == VB_CONST) {
680 mgr->real_vertex_buffer[mgr->fallback_vbs[VB_CONST]].stride = 0;
681 }
682 }
683 }
684
685 /* Setup new vertex elements. */
686 for (i = 0; i < mgr->ve->count; i++) {
687 for (type = 0; type < VB_NUM; type++) {
688 if (elem_index[type][i] < key[type].nr_elements) {
689 struct translate_element *te = &key[type].element[elem_index[type][i]];
690 mgr->fallback_velems[i].instance_divisor = mgr->ve->ve[i].instance_divisor;
691 mgr->fallback_velems[i].src_format = te->output_format;
692 mgr->fallback_velems[i].src_offset = te->output_offset;
693 mgr->fallback_velems[i].vertex_buffer_index = mgr->fallback_vbs[type];
694
695 /* elem_index[type][i] can only be set for one type. */
696 assert(type > VB_INSTANCE || elem_index[type+1][i] == ~0u);
697 assert(type > VB_VERTEX || elem_index[type+2][i] == ~0u);
698 break;
699 }
700 }
701 /* No translating, just copy the original vertex element over. */
702 if (type == VB_NUM) {
703 memcpy(&mgr->fallback_velems[i], &mgr->ve->ve[i],
704 sizeof(struct pipe_vertex_element));
705 }
706 }
707
708 u_vbuf_set_vertex_elements_internal(mgr, mgr->ve->count,
709 mgr->fallback_velems);
710 mgr->using_translate = TRUE;
711 return TRUE;
712 }
713
714 static void u_vbuf_translate_end(struct u_vbuf *mgr)
715 {
716 unsigned i;
717
718 /* Restore vertex elements. */
719 mgr->pipe->bind_vertex_elements_state(mgr->pipe, mgr->ve->driver_cso);
720 mgr->using_translate = FALSE;
721
722 /* Unreference the now-unused VBOs. */
723 for (i = 0; i < VB_NUM; i++) {
724 unsigned vb = mgr->fallback_vbs[i];
725 if (vb != ~0u) {
726 pipe_resource_reference(&mgr->real_vertex_buffer[vb].buffer.resource, NULL);
727 mgr->fallback_vbs[i] = ~0;
728
729 /* This will cause the buffer to be unbound in the driver later. */
730 mgr->dirty_real_vb_mask |= 1 << vb;
731 }
732 }
733 }
734
735 static void *
736 u_vbuf_create_vertex_elements(struct u_vbuf *mgr, unsigned count,
737 const struct pipe_vertex_element *attribs)
738 {
739 struct pipe_context *pipe = mgr->pipe;
740 unsigned i;
741 struct pipe_vertex_element driver_attribs[PIPE_MAX_ATTRIBS];
742 struct u_vbuf_elements *ve = CALLOC_STRUCT(u_vbuf_elements);
743 uint32_t used_buffers = 0;
744
745 ve->count = count;
746
747 memcpy(ve->ve, attribs, sizeof(struct pipe_vertex_element) * count);
748 memcpy(driver_attribs, attribs, sizeof(struct pipe_vertex_element) * count);
749
750 /* Set the best native format in case the original format is not
751 * supported. */
752 for (i = 0; i < count; i++) {
753 enum pipe_format format = ve->ve[i].src_format;
754
755 ve->src_format_size[i] = util_format_get_blocksize(format);
756
757 used_buffers |= 1 << ve->ve[i].vertex_buffer_index;
758
759 if (!ve->ve[i].instance_divisor) {
760 ve->noninstance_vb_mask_any |= 1 << ve->ve[i].vertex_buffer_index;
761 }
762
763 format = mgr->caps.format_translation[format];
764
765 driver_attribs[i].src_format = format;
766 ve->native_format[i] = format;
767 ve->native_format_size[i] =
768 util_format_get_blocksize(ve->native_format[i]);
769
770 if (ve->ve[i].src_format != format ||
771 (!mgr->caps.velem_src_offset_unaligned &&
772 ve->ve[i].src_offset % 4 != 0)) {
773 ve->incompatible_elem_mask |= 1 << i;
774 ve->incompatible_vb_mask_any |= 1 << ve->ve[i].vertex_buffer_index;
775 } else {
776 ve->compatible_vb_mask_any |= 1 << ve->ve[i].vertex_buffer_index;
777 }
778 }
779
780 ve->used_vb_mask = used_buffers;
781 ve->compatible_vb_mask_all = ~ve->incompatible_vb_mask_any & used_buffers;
782 ve->incompatible_vb_mask_all = ~ve->compatible_vb_mask_any & used_buffers;
783
784 /* Align the formats and offsets to the size of DWORD if needed. */
785 if (!mgr->caps.velem_src_offset_unaligned) {
786 for (i = 0; i < count; i++) {
787 ve->native_format_size[i] = align(ve->native_format_size[i], 4);
788 driver_attribs[i].src_offset = align(ve->ve[i].src_offset, 4);
789 }
790 }
791
792 ve->driver_cso =
793 pipe->create_vertex_elements_state(pipe, count, driver_attribs);
794 return ve;
795 }
796
797 static void u_vbuf_delete_vertex_elements(struct u_vbuf *mgr, void *cso)
798 {
799 struct pipe_context *pipe = mgr->pipe;
800 struct u_vbuf_elements *ve = cso;
801
802 pipe->delete_vertex_elements_state(pipe, ve->driver_cso);
803 FREE(ve);
804 }
805
806 void u_vbuf_set_vertex_buffers(struct u_vbuf *mgr,
807 unsigned start_slot, unsigned count,
808 const struct pipe_vertex_buffer *bufs)
809 {
810 unsigned i;
811 /* which buffers are enabled */
812 uint32_t enabled_vb_mask = 0;
813 /* which buffers are in user memory */
814 uint32_t user_vb_mask = 0;
815 /* which buffers are incompatible with the driver */
816 uint32_t incompatible_vb_mask = 0;
817 /* which buffers have a non-zero stride */
818 uint32_t nonzero_stride_vb_mask = 0;
819 const uint32_t mask = ~(((1ull << count) - 1) << start_slot);
820
821 /* Zero out the bits we are going to rewrite completely. */
822 mgr->user_vb_mask &= mask;
823 mgr->incompatible_vb_mask &= mask;
824 mgr->nonzero_stride_vb_mask &= mask;
825 mgr->enabled_vb_mask &= mask;
826
827 if (!bufs) {
828 struct pipe_context *pipe = mgr->pipe;
829 /* Unbind. */
830 mgr->dirty_real_vb_mask &= mask;
831
832 for (i = 0; i < count; i++) {
833 unsigned dst_index = start_slot + i;
834
835 pipe_vertex_buffer_unreference(&mgr->vertex_buffer[dst_index]);
836 pipe_resource_reference(&mgr->real_vertex_buffer[dst_index].buffer.resource,
837 NULL);
838 }
839
840 pipe->set_vertex_buffers(pipe, start_slot, count, NULL);
841 return;
842 }
843
844 for (i = 0; i < count; i++) {
845 unsigned dst_index = start_slot + i;
846 const struct pipe_vertex_buffer *vb = &bufs[i];
847 struct pipe_vertex_buffer *orig_vb = &mgr->vertex_buffer[dst_index];
848 struct pipe_vertex_buffer *real_vb = &mgr->real_vertex_buffer[dst_index];
849
850 if (!vb->buffer.resource) {
851 pipe_vertex_buffer_unreference(orig_vb);
852 pipe_vertex_buffer_unreference(real_vb);
853 continue;
854 }
855
856 pipe_vertex_buffer_reference(orig_vb, vb);
857
858 if (vb->stride) {
859 nonzero_stride_vb_mask |= 1 << dst_index;
860 }
861 enabled_vb_mask |= 1 << dst_index;
862
863 if ((!mgr->caps.buffer_offset_unaligned && vb->buffer_offset % 4 != 0) ||
864 (!mgr->caps.buffer_stride_unaligned && vb->stride % 4 != 0)) {
865 incompatible_vb_mask |= 1 << dst_index;
866 real_vb->buffer_offset = vb->buffer_offset;
867 real_vb->stride = vb->stride;
868 pipe_vertex_buffer_unreference(real_vb);
869 real_vb->is_user_buffer = false;
870 continue;
871 }
872
873 if (!mgr->caps.user_vertex_buffers && vb->is_user_buffer) {
874 user_vb_mask |= 1 << dst_index;
875 real_vb->buffer_offset = vb->buffer_offset;
876 real_vb->stride = vb->stride;
877 pipe_vertex_buffer_unreference(real_vb);
878 real_vb->is_user_buffer = false;
879 continue;
880 }
881
882 pipe_vertex_buffer_reference(real_vb, vb);
883 }
884
885 mgr->user_vb_mask |= user_vb_mask;
886 mgr->incompatible_vb_mask |= incompatible_vb_mask;
887 mgr->nonzero_stride_vb_mask |= nonzero_stride_vb_mask;
888 mgr->enabled_vb_mask |= enabled_vb_mask;
889
890 /* All changed buffers are marked as dirty, even the NULL ones,
891 * which will cause the NULL buffers to be unbound in the driver later. */
892 mgr->dirty_real_vb_mask |= ~mask;
893 }
894
895 static enum pipe_error
896 u_vbuf_upload_buffers(struct u_vbuf *mgr,
897 int start_vertex, unsigned num_vertices,
898 int start_instance, unsigned num_instances)
899 {
900 unsigned i;
901 unsigned nr_velems = mgr->ve->count;
902 const struct pipe_vertex_element *velems =
903 mgr->using_translate ? mgr->fallback_velems : mgr->ve->ve;
904 unsigned start_offset[PIPE_MAX_ATTRIBS];
905 unsigned end_offset[PIPE_MAX_ATTRIBS];
906 uint32_t buffer_mask = 0;
907
908 /* Determine how much data needs to be uploaded. */
909 for (i = 0; i < nr_velems; i++) {
910 const struct pipe_vertex_element *velem = &velems[i];
911 unsigned index = velem->vertex_buffer_index;
912 struct pipe_vertex_buffer *vb = &mgr->vertex_buffer[index];
913 unsigned instance_div, first, size, index_bit;
914
915 /* Skip the buffers generated by translate. */
916 if (index == mgr->fallback_vbs[VB_VERTEX] ||
917 index == mgr->fallback_vbs[VB_INSTANCE] ||
918 index == mgr->fallback_vbs[VB_CONST]) {
919 continue;
920 }
921
922 if (!vb->is_user_buffer) {
923 continue;
924 }
925
926 instance_div = velem->instance_divisor;
927 first = vb->buffer_offset + velem->src_offset;
928
929 if (!vb->stride) {
930 /* Constant attrib. */
931 size = mgr->ve->src_format_size[i];
932 } else if (instance_div) {
933 /* Per-instance attrib. */
934 unsigned count = (num_instances + instance_div - 1) / instance_div;
935 first += vb->stride * start_instance;
936 size = vb->stride * (count - 1) + mgr->ve->src_format_size[i];
937 } else {
938 /* Per-vertex attrib. */
939 first += vb->stride * start_vertex;
940 size = vb->stride * (num_vertices - 1) + mgr->ve->src_format_size[i];
941 }
942
943 index_bit = 1 << index;
944
945 /* Update offsets. */
946 if (!(buffer_mask & index_bit)) {
947 start_offset[index] = first;
948 end_offset[index] = first + size;
949 } else {
950 if (first < start_offset[index])
951 start_offset[index] = first;
952 if (first + size > end_offset[index])
953 end_offset[index] = first + size;
954 }
955
956 buffer_mask |= index_bit;
957 }
958
959 /* Upload buffers. */
960 while (buffer_mask) {
961 unsigned start, end;
962 struct pipe_vertex_buffer *real_vb;
963 const uint8_t *ptr;
964
965 i = u_bit_scan(&buffer_mask);
966
967 start = start_offset[i];
968 end = end_offset[i];
969 assert(start < end);
970
971 real_vb = &mgr->real_vertex_buffer[i];
972 ptr = mgr->vertex_buffer[i].buffer.user;
973
974 u_upload_data(mgr->pipe->stream_uploader, start, end - start, 4,
975 ptr + start, &real_vb->buffer_offset, &real_vb->buffer.resource);
976 if (!real_vb->buffer.resource)
977 return PIPE_ERROR_OUT_OF_MEMORY;
978
979 real_vb->buffer_offset -= start;
980 }
981
982 return PIPE_OK;
983 }
984
985 static boolean u_vbuf_need_minmax_index(const struct u_vbuf *mgr)
986 {
987 /* See if there are any per-vertex attribs which will be uploaded or
988 * translated. Use bitmasks to get the info instead of looping over vertex
989 * elements. */
990 return (mgr->ve->used_vb_mask &
991 ((mgr->user_vb_mask |
992 mgr->incompatible_vb_mask |
993 mgr->ve->incompatible_vb_mask_any) &
994 mgr->ve->noninstance_vb_mask_any &
995 mgr->nonzero_stride_vb_mask)) != 0;
996 }
997
998 static boolean u_vbuf_mapping_vertex_buffer_blocks(const struct u_vbuf *mgr)
999 {
1000 /* Return true if there are hw buffers which don't need to be translated.
1001 *
1002 * We could query whether each buffer is busy, but that would
1003 * be way more costly than this. */
1004 return (mgr->ve->used_vb_mask &
1005 (~mgr->user_vb_mask &
1006 ~mgr->incompatible_vb_mask &
1007 mgr->ve->compatible_vb_mask_all &
1008 mgr->ve->noninstance_vb_mask_any &
1009 mgr->nonzero_stride_vb_mask)) != 0;
1010 }
1011
1012 static void u_vbuf_get_minmax_index(struct pipe_context *pipe,
1013 const struct pipe_draw_info *info,
1014 int *out_min_index, int *out_max_index)
1015 {
1016 struct pipe_transfer *transfer = NULL;
1017 const void *indices;
1018 unsigned i;
1019
1020 if (info->has_user_indices) {
1021 indices = (uint8_t*)info->index.user +
1022 info->start * info->index_size;
1023 } else {
1024 indices = pipe_buffer_map_range(pipe, info->index.resource,
1025 info->start * info->index_size,
1026 info->count * info->index_size,
1027 PIPE_TRANSFER_READ, &transfer);
1028 }
1029
1030 switch (info->index_size) {
1031 case 4: {
1032 const unsigned *ui_indices = (const unsigned*)indices;
1033 unsigned max_ui = 0;
1034 unsigned min_ui = ~0U;
1035 if (info->primitive_restart) {
1036 for (i = 0; i < info->count; i++) {
1037 if (ui_indices[i] != info->restart_index) {
1038 if (ui_indices[i] > max_ui) max_ui = ui_indices[i];
1039 if (ui_indices[i] < min_ui) min_ui = ui_indices[i];
1040 }
1041 }
1042 }
1043 else {
1044 for (i = 0; i < info->count; i++) {
1045 if (ui_indices[i] > max_ui) max_ui = ui_indices[i];
1046 if (ui_indices[i] < min_ui) min_ui = ui_indices[i];
1047 }
1048 }
1049 *out_min_index = min_ui;
1050 *out_max_index = max_ui;
1051 break;
1052 }
1053 case 2: {
1054 const unsigned short *us_indices = (const unsigned short*)indices;
1055 unsigned max_us = 0;
1056 unsigned min_us = ~0U;
1057 if (info->primitive_restart) {
1058 for (i = 0; i < info->count; i++) {
1059 if (us_indices[i] != info->restart_index) {
1060 if (us_indices[i] > max_us) max_us = us_indices[i];
1061 if (us_indices[i] < min_us) min_us = us_indices[i];
1062 }
1063 }
1064 }
1065 else {
1066 for (i = 0; i < info->count; i++) {
1067 if (us_indices[i] > max_us) max_us = us_indices[i];
1068 if (us_indices[i] < min_us) min_us = us_indices[i];
1069 }
1070 }
1071 *out_min_index = min_us;
1072 *out_max_index = max_us;
1073 break;
1074 }
1075 case 1: {
1076 const unsigned char *ub_indices = (const unsigned char*)indices;
1077 unsigned max_ub = 0;
1078 unsigned min_ub = ~0U;
1079 if (info->primitive_restart) {
1080 for (i = 0; i < info->count; i++) {
1081 if (ub_indices[i] != info->restart_index) {
1082 if (ub_indices[i] > max_ub) max_ub = ub_indices[i];
1083 if (ub_indices[i] < min_ub) min_ub = ub_indices[i];
1084 }
1085 }
1086 }
1087 else {
1088 for (i = 0; i < info->count; i++) {
1089 if (ub_indices[i] > max_ub) max_ub = ub_indices[i];
1090 if (ub_indices[i] < min_ub) min_ub = ub_indices[i];
1091 }
1092 }
1093 *out_min_index = min_ub;
1094 *out_max_index = max_ub;
1095 break;
1096 }
1097 default:
1098 assert(0);
1099 *out_min_index = 0;
1100 *out_max_index = 0;
1101 }
1102
1103 if (transfer) {
1104 pipe_buffer_unmap(pipe, transfer);
1105 }
1106 }
1107
1108 static void u_vbuf_set_driver_vertex_buffers(struct u_vbuf *mgr)
1109 {
1110 struct pipe_context *pipe = mgr->pipe;
1111 unsigned start_slot, count;
1112
1113 start_slot = ffs(mgr->dirty_real_vb_mask) - 1;
1114 count = util_last_bit(mgr->dirty_real_vb_mask >> start_slot);
1115
1116 pipe->set_vertex_buffers(pipe, start_slot, count,
1117 mgr->real_vertex_buffer + start_slot);
1118 mgr->dirty_real_vb_mask = 0;
1119 }
1120
1121 void u_vbuf_draw_vbo(struct u_vbuf *mgr, const struct pipe_draw_info *info)
1122 {
1123 struct pipe_context *pipe = mgr->pipe;
1124 int start_vertex, min_index;
1125 unsigned num_vertices;
1126 boolean unroll_indices = FALSE;
1127 const uint32_t used_vb_mask = mgr->ve->used_vb_mask;
1128 uint32_t user_vb_mask = mgr->user_vb_mask & used_vb_mask;
1129 const uint32_t incompatible_vb_mask =
1130 mgr->incompatible_vb_mask & used_vb_mask;
1131 struct pipe_draw_info new_info;
1132
1133 /* Normal draw. No fallback and no user buffers. */
1134 if (!incompatible_vb_mask &&
1135 !mgr->ve->incompatible_elem_mask &&
1136 !user_vb_mask) {
1137
1138 /* Set vertex buffers if needed. */
1139 if (mgr->dirty_real_vb_mask & used_vb_mask) {
1140 u_vbuf_set_driver_vertex_buffers(mgr);
1141 }
1142
1143 pipe->draw_vbo(pipe, info);
1144 return;
1145 }
1146
1147 new_info = *info;
1148
1149 /* Fallback. We need to know all the parameters. */
1150 if (new_info.indirect) {
1151 struct pipe_transfer *transfer = NULL;
1152 int *data;
1153
1154 if (new_info.index_size) {
1155 data = pipe_buffer_map_range(pipe, new_info.indirect->buffer,
1156 new_info.indirect->offset, 20,
1157 PIPE_TRANSFER_READ, &transfer);
1158 new_info.index_bias = data[3];
1159 new_info.start_instance = data[4];
1160 }
1161 else {
1162 data = pipe_buffer_map_range(pipe, new_info.indirect->buffer,
1163 new_info.indirect->offset, 16,
1164 PIPE_TRANSFER_READ, &transfer);
1165 new_info.start_instance = data[3];
1166 }
1167
1168 new_info.count = data[0];
1169 new_info.instance_count = data[1];
1170 new_info.start = data[2];
1171 pipe_buffer_unmap(pipe, transfer);
1172 new_info.indirect = NULL;
1173 }
1174
1175 if (new_info.index_size) {
1176 /* See if anything needs to be done for per-vertex attribs. */
1177 if (u_vbuf_need_minmax_index(mgr)) {
1178 int max_index;
1179
1180 if (new_info.max_index != ~0u) {
1181 min_index = new_info.min_index;
1182 max_index = new_info.max_index;
1183 } else {
1184 u_vbuf_get_minmax_index(mgr->pipe, &new_info,
1185 &min_index, &max_index);
1186 }
1187
1188 assert(min_index <= max_index);
1189
1190 start_vertex = min_index + new_info.index_bias;
1191 num_vertices = max_index + 1 - min_index;
1192
1193 /* Primitive restart doesn't work when unrolling indices.
1194 * We would have to break this drawing operation into several ones. */
1195 /* Use some heuristic to see if unrolling indices improves
1196 * performance. */
1197 if (!new_info.primitive_restart &&
1198 num_vertices > new_info.count*2 &&
1199 num_vertices - new_info.count > 32 &&
1200 !u_vbuf_mapping_vertex_buffer_blocks(mgr)) {
1201 unroll_indices = TRUE;
1202 user_vb_mask &= ~(mgr->nonzero_stride_vb_mask &
1203 mgr->ve->noninstance_vb_mask_any);
1204 }
1205 } else {
1206 /* Nothing to do for per-vertex attribs. */
1207 start_vertex = 0;
1208 num_vertices = 0;
1209 min_index = 0;
1210 }
1211 } else {
1212 start_vertex = new_info.start;
1213 num_vertices = new_info.count;
1214 min_index = 0;
1215 }
1216
1217 /* Translate vertices with non-native layouts or formats. */
1218 if (unroll_indices ||
1219 incompatible_vb_mask ||
1220 mgr->ve->incompatible_elem_mask) {
1221 if (!u_vbuf_translate_begin(mgr, &new_info, start_vertex, num_vertices,
1222 min_index, unroll_indices)) {
1223 debug_warn_once("u_vbuf_translate_begin() failed");
1224 return;
1225 }
1226
1227 if (unroll_indices) {
1228 new_info.index_size = 0;
1229 new_info.index_bias = 0;
1230 new_info.min_index = 0;
1231 new_info.max_index = new_info.count - 1;
1232 new_info.start = 0;
1233 }
1234
1235 user_vb_mask &= ~(incompatible_vb_mask |
1236 mgr->ve->incompatible_vb_mask_all);
1237 }
1238
1239 /* Upload user buffers. */
1240 if (user_vb_mask) {
1241 if (u_vbuf_upload_buffers(mgr, start_vertex, num_vertices,
1242 new_info.start_instance,
1243 new_info.instance_count) != PIPE_OK) {
1244 debug_warn_once("u_vbuf_upload_buffers() failed");
1245 return;
1246 }
1247
1248 mgr->dirty_real_vb_mask |= user_vb_mask;
1249 }
1250
1251 /*
1252 if (unroll_indices) {
1253 printf("unrolling indices: start_vertex = %i, num_vertices = %i\n",
1254 start_vertex, num_vertices);
1255 util_dump_draw_info(stdout, info);
1256 printf("\n");
1257 }
1258
1259 unsigned i;
1260 for (i = 0; i < mgr->nr_vertex_buffers; i++) {
1261 printf("input %i: ", i);
1262 util_dump_vertex_buffer(stdout, mgr->vertex_buffer+i);
1263 printf("\n");
1264 }
1265 for (i = 0; i < mgr->nr_real_vertex_buffers; i++) {
1266 printf("real %i: ", i);
1267 util_dump_vertex_buffer(stdout, mgr->real_vertex_buffer+i);
1268 printf("\n");
1269 }
1270 */
1271
1272 u_upload_unmap(pipe->stream_uploader);
1273 u_vbuf_set_driver_vertex_buffers(mgr);
1274
1275 pipe->draw_vbo(pipe, &new_info);
1276
1277 if (mgr->using_translate) {
1278 u_vbuf_translate_end(mgr);
1279 }
1280 }
1281
1282 void u_vbuf_save_vertex_elements(struct u_vbuf *mgr)
1283 {
1284 assert(!mgr->ve_saved);
1285 mgr->ve_saved = mgr->ve;
1286 }
1287
1288 void u_vbuf_restore_vertex_elements(struct u_vbuf *mgr)
1289 {
1290 if (mgr->ve != mgr->ve_saved) {
1291 struct pipe_context *pipe = mgr->pipe;
1292
1293 mgr->ve = mgr->ve_saved;
1294 pipe->bind_vertex_elements_state(pipe,
1295 mgr->ve ? mgr->ve->driver_cso : NULL);
1296 }
1297 mgr->ve_saved = NULL;
1298 }
1299
1300 void u_vbuf_save_aux_vertex_buffer_slot(struct u_vbuf *mgr)
1301 {
1302 pipe_vertex_buffer_reference(&mgr->aux_vertex_buffer_saved,
1303 &mgr->vertex_buffer[mgr->aux_vertex_buffer_slot]);
1304 }
1305
1306 void u_vbuf_restore_aux_vertex_buffer_slot(struct u_vbuf *mgr)
1307 {
1308 u_vbuf_set_vertex_buffers(mgr, mgr->aux_vertex_buffer_slot, 1,
1309 &mgr->aux_vertex_buffer_saved);
1310 pipe_vertex_buffer_unreference(&mgr->aux_vertex_buffer_saved);
1311 }