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