u_vbuf.c revision bf469f4edc60bd1c5fd770cb231b8d5ab801427f
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#include "util/u_vbuf.h" 29 30#include "util/u_dump.h" 31#include "util/u_format.h" 32#include "util/u_inlines.h" 33#include "util/u_memory.h" 34#include "util/u_upload_mgr.h" 35#include "translate/translate.h" 36#include "translate/translate_cache.h" 37#include "cso_cache/cso_cache.h" 38#include "cso_cache/cso_hash.h" 39 40struct u_vbuf_elements { 41 unsigned count; 42 struct pipe_vertex_element ve[PIPE_MAX_ATTRIBS]; 43 44 unsigned src_format_size[PIPE_MAX_ATTRIBS]; 45 46 /* If (velem[i].src_format != native_format[i]), the vertex buffer 47 * referenced by the vertex element cannot be used for rendering and 48 * its vertex data must be translated to native_format[i]. */ 49 enum pipe_format native_format[PIPE_MAX_ATTRIBS]; 50 unsigned native_format_size[PIPE_MAX_ATTRIBS]; 51 52 /* This might mean two things: 53 * - src_format != native_format, as discussed above. 54 * - src_offset % 4 != 0 (if the caps don't allow such an offset). */ 55 uint32_t incompatible_elem_mask; /* each bit describes a corresp. attrib */ 56 /* Which buffer has at least one vertex element referencing it 57 * incompatible. */ 58 uint32_t incompatible_vb_mask_any; 59 /* Which buffer has all vertex elements referencing it incompatible. */ 60 uint32_t incompatible_vb_mask_all; 61 /* Which buffer has at least one vertex element referencing it 62 * compatible. */ 63 uint32_t compatible_vb_mask_any; 64 /* Which buffer has all vertex elements referencing it compatible. */ 65 uint32_t compatible_vb_mask_all; 66 67 /* Which buffer has at least one vertex element referencing it 68 * non-instanced. */ 69 uint32_t noninstance_vb_mask_any; 70 71 void *driver_cso; 72}; 73 74enum { 75 VB_VERTEX = 0, 76 VB_INSTANCE = 1, 77 VB_CONST = 2, 78 VB_NUM = 3 79}; 80 81struct u_vbuf { 82 struct u_vbuf_caps caps; 83 84 struct pipe_context *pipe; 85 struct translate_cache *translate_cache; 86 struct cso_cache *cso_cache; 87 struct u_upload_mgr *uploader; 88 89 /* This is what was set in set_vertex_buffers. 90 * May contain user buffers. */ 91 struct pipe_vertex_buffer vertex_buffer[PIPE_MAX_ATTRIBS]; 92 unsigned nr_vertex_buffers; 93 94 /* Saved vertex buffers. */ 95 struct pipe_vertex_buffer vertex_buffer_saved[PIPE_MAX_ATTRIBS]; 96 unsigned nr_vertex_buffers_saved; 97 98 /* Vertex buffers for the driver. 99 * There are no user buffers. */ 100 struct pipe_vertex_buffer real_vertex_buffer[PIPE_MAX_ATTRIBS]; 101 int nr_real_vertex_buffers; 102 boolean vertex_buffers_dirty; 103 104 /* The index buffer. */ 105 struct pipe_index_buffer index_buffer; 106 107 /* Vertex elements. */ 108 struct u_vbuf_elements *ve, *ve_saved; 109 110 /* Vertex elements used for the translate fallback. */ 111 struct pipe_vertex_element fallback_velems[PIPE_MAX_ATTRIBS]; 112 /* If non-NULL, this is a vertex element state used for the translate 113 * fallback and therefore used for rendering too. */ 114 boolean using_translate; 115 /* The vertex buffer slot index where translated vertices have been 116 * stored in. */ 117 unsigned fallback_vbs[VB_NUM]; 118 119 /* Which buffer is a user buffer. */ 120 uint32_t user_vb_mask; /* each bit describes a corresp. buffer */ 121 /* Which buffer is incompatible (unaligned). */ 122 uint32_t incompatible_vb_mask; /* each bit describes a corresp. buffer */ 123 /* Which buffer has a non-zero stride. */ 124 uint32_t nonzero_stride_vb_mask; /* each bit describes a corresp. buffer */ 125}; 126 127static void * 128u_vbuf_create_vertex_elements(struct u_vbuf *mgr, unsigned count, 129 const struct pipe_vertex_element *attribs); 130static void u_vbuf_delete_vertex_elements(struct u_vbuf *mgr, void *cso); 131 132 133void u_vbuf_get_caps(struct pipe_screen *screen, struct u_vbuf_caps *caps) 134{ 135 caps->format_fixed32 = 136 screen->is_format_supported(screen, PIPE_FORMAT_R32_FIXED, PIPE_BUFFER, 137 0, PIPE_BIND_VERTEX_BUFFER); 138 139 caps->format_float16 = 140 screen->is_format_supported(screen, PIPE_FORMAT_R16_FLOAT, PIPE_BUFFER, 141 0, PIPE_BIND_VERTEX_BUFFER); 142 143 caps->format_float64 = 144 screen->is_format_supported(screen, PIPE_FORMAT_R64_FLOAT, PIPE_BUFFER, 145 0, PIPE_BIND_VERTEX_BUFFER); 146 147 caps->format_norm32 = 148 screen->is_format_supported(screen, PIPE_FORMAT_R32_UNORM, PIPE_BUFFER, 149 0, PIPE_BIND_VERTEX_BUFFER) && 150 screen->is_format_supported(screen, PIPE_FORMAT_R32_SNORM, PIPE_BUFFER, 151 0, PIPE_BIND_VERTEX_BUFFER); 152 153 caps->format_scaled32 = 154 screen->is_format_supported(screen, PIPE_FORMAT_R32_USCALED, PIPE_BUFFER, 155 0, PIPE_BIND_VERTEX_BUFFER) && 156 screen->is_format_supported(screen, PIPE_FORMAT_R32_SSCALED, PIPE_BUFFER, 157 0, PIPE_BIND_VERTEX_BUFFER); 158 159 caps->buffer_offset_unaligned = 160 !screen->get_param(screen, 161 PIPE_CAP_VERTEX_BUFFER_OFFSET_4BYTE_ALIGNED_ONLY); 162 163 caps->buffer_stride_unaligned = 164 !screen->get_param(screen, 165 PIPE_CAP_VERTEX_BUFFER_STRIDE_4BYTE_ALIGNED_ONLY); 166 167 caps->velem_src_offset_unaligned = 168 !screen->get_param(screen, 169 PIPE_CAP_VERTEX_ELEMENT_SRC_OFFSET_4BYTE_ALIGNED_ONLY); 170 171 caps->user_vertex_buffers = 172 screen->get_param(screen, PIPE_CAP_USER_VERTEX_BUFFERS); 173} 174 175struct u_vbuf * 176u_vbuf_create(struct pipe_context *pipe, 177 struct u_vbuf_caps *caps) 178{ 179 struct u_vbuf *mgr = CALLOC_STRUCT(u_vbuf); 180 181 mgr->caps = *caps; 182 mgr->pipe = pipe; 183 mgr->cso_cache = cso_cache_create(); 184 mgr->translate_cache = translate_cache_create(); 185 memset(mgr->fallback_vbs, ~0, sizeof(mgr->fallback_vbs)); 186 187 mgr->uploader = u_upload_create(pipe, 1024 * 1024, 4, 188 PIPE_BIND_VERTEX_BUFFER); 189 190 return mgr; 191} 192 193/* u_vbuf uses its own caching for vertex elements, because it needs to keep 194 * its own preprocessed state per vertex element CSO. */ 195static struct u_vbuf_elements * 196u_vbuf_set_vertex_elements_internal(struct u_vbuf *mgr, unsigned count, 197 const struct pipe_vertex_element *states) 198{ 199 struct pipe_context *pipe = mgr->pipe; 200 unsigned key_size, hash_key; 201 struct cso_hash_iter iter; 202 struct u_vbuf_elements *ve; 203 struct cso_velems_state velems_state; 204 205 /* need to include the count into the stored state data too. */ 206 key_size = sizeof(struct pipe_vertex_element) * count + sizeof(unsigned); 207 velems_state.count = count; 208 memcpy(velems_state.velems, states, 209 sizeof(struct pipe_vertex_element) * count); 210 hash_key = cso_construct_key((void*)&velems_state, key_size); 211 iter = cso_find_state_template(mgr->cso_cache, hash_key, CSO_VELEMENTS, 212 (void*)&velems_state, key_size); 213 214 if (cso_hash_iter_is_null(iter)) { 215 struct cso_velements *cso = MALLOC_STRUCT(cso_velements); 216 memcpy(&cso->state, &velems_state, key_size); 217 cso->data = u_vbuf_create_vertex_elements(mgr, count, states); 218 cso->delete_state = (cso_state_callback)u_vbuf_delete_vertex_elements; 219 cso->context = (void*)mgr; 220 221 iter = cso_insert_state(mgr->cso_cache, hash_key, CSO_VELEMENTS, cso); 222 ve = cso->data; 223 } else { 224 ve = ((struct cso_velements *)cso_hash_iter_data(iter))->data; 225 } 226 227 assert(ve); 228 pipe->bind_vertex_elements_state(pipe, ve->driver_cso); 229 return ve; 230} 231 232void u_vbuf_set_vertex_elements(struct u_vbuf *mgr, unsigned count, 233 const struct pipe_vertex_element *states) 234{ 235 mgr->ve = u_vbuf_set_vertex_elements_internal(mgr, count, states); 236} 237 238void u_vbuf_destroy(struct u_vbuf *mgr) 239{ 240 unsigned i; 241 242 mgr->pipe->set_vertex_buffers(mgr->pipe, 0, NULL); 243 244 for (i = 0; i < mgr->nr_vertex_buffers; i++) { 245 pipe_resource_reference(&mgr->vertex_buffer[i].buffer, NULL); 246 } 247 for (i = 0; i < mgr->nr_real_vertex_buffers; i++) { 248 pipe_resource_reference(&mgr->real_vertex_buffer[i].buffer, NULL); 249 } 250 251 translate_cache_destroy(mgr->translate_cache); 252 u_upload_destroy(mgr->uploader); 253 cso_cache_delete(mgr->cso_cache); 254 FREE(mgr); 255} 256 257static void 258u_vbuf_translate_buffers(struct u_vbuf *mgr, struct translate_key *key, 259 unsigned vb_mask, unsigned out_vb, 260 int start_vertex, unsigned num_vertices, 261 int start_index, unsigned num_indices, int min_index, 262 boolean unroll_indices) 263{ 264 struct translate *tr; 265 struct pipe_transfer *vb_transfer[PIPE_MAX_ATTRIBS] = {0}; 266 struct pipe_resource *out_buffer = NULL; 267 uint8_t *out_map; 268 unsigned i, out_offset; 269 270 /* Get a translate object. */ 271 tr = translate_cache_find(mgr->translate_cache, key); 272 273 /* Map buffers we want to translate. */ 274 for (i = 0; i < mgr->nr_vertex_buffers; i++) { 275 if (vb_mask & (1 << i)) { 276 struct pipe_vertex_buffer *vb = &mgr->vertex_buffer[i]; 277 unsigned offset = vb->buffer_offset + vb->stride * start_vertex; 278 uint8_t *map; 279 280 if (vb->user_buffer) { 281 map = (uint8_t*)vb->user_buffer + offset; 282 } else { 283 unsigned size = vb->stride ? num_vertices * vb->stride 284 : sizeof(double)*4; 285 286 if (offset+size > vb->buffer->width0) { 287 size = vb->buffer->width0 - offset; 288 } 289 290 map = pipe_buffer_map_range(mgr->pipe, vb->buffer, offset, size, 291 PIPE_TRANSFER_READ, &vb_transfer[i]); 292 } 293 294 /* Subtract min_index so that indexing with the index buffer works. */ 295 if (unroll_indices) { 296 map -= vb->stride * min_index; 297 } 298 299 tr->set_buffer(tr, i, map, vb->stride, ~0); 300 } 301 } 302 303 /* Translate. */ 304 if (unroll_indices) { 305 struct pipe_index_buffer *ib = &mgr->index_buffer; 306 struct pipe_transfer *transfer = NULL; 307 unsigned offset = ib->offset + start_index * ib->index_size; 308 uint8_t *map; 309 310 assert((ib->buffer || ib->user_buffer) && ib->index_size); 311 312 if (ib->user_buffer) { 313 map = (uint8_t*)ib->user_buffer + offset; 314 } else { 315 map = pipe_buffer_map_range(mgr->pipe, ib->buffer, offset, 316 num_indices * ib->index_size, 317 PIPE_TRANSFER_READ, &transfer); 318 } 319 320 /* Create and map the output buffer. */ 321 u_upload_alloc(mgr->uploader, 0, 322 key->output_stride * num_indices, 323 &out_offset, &out_buffer, 324 (void**)&out_map); 325 326 switch (ib->index_size) { 327 case 4: 328 tr->run_elts(tr, (unsigned*)map, num_indices, 0, out_map); 329 break; 330 case 2: 331 tr->run_elts16(tr, (uint16_t*)map, num_indices, 0, out_map); 332 break; 333 case 1: 334 tr->run_elts8(tr, map, num_indices, 0, out_map); 335 break; 336 } 337 338 if (transfer) { 339 pipe_buffer_unmap(mgr->pipe, transfer); 340 } 341 } else { 342 /* Create and map the output buffer. */ 343 u_upload_alloc(mgr->uploader, 344 key->output_stride * start_vertex, 345 key->output_stride * num_vertices, 346 &out_offset, &out_buffer, 347 (void**)&out_map); 348 349 out_offset -= key->output_stride * start_vertex; 350 351 tr->run(tr, 0, num_vertices, 0, out_map); 352 } 353 354 /* Unmap all buffers. */ 355 for (i = 0; i < mgr->nr_vertex_buffers; i++) { 356 if (vb_transfer[i]) { 357 pipe_buffer_unmap(mgr->pipe, vb_transfer[i]); 358 } 359 } 360 361 /* Setup the new vertex buffer. */ 362 mgr->real_vertex_buffer[out_vb].buffer_offset = out_offset; 363 mgr->real_vertex_buffer[out_vb].stride = key->output_stride; 364 365 /* Move the buffer reference. */ 366 pipe_resource_reference( 367 &mgr->real_vertex_buffer[out_vb].buffer, NULL); 368 mgr->real_vertex_buffer[out_vb].buffer = out_buffer; 369} 370 371static boolean 372u_vbuf_translate_find_free_vb_slots(struct u_vbuf *mgr, 373 unsigned mask[VB_NUM]) 374{ 375 unsigned type; 376 unsigned fallback_vbs[VB_NUM]; 377 /* Set the bit for each buffer which is incompatible, or isn't set. */ 378 uint32_t unused_vb_mask = 379 mgr->ve->incompatible_vb_mask_all | mgr->incompatible_vb_mask | 380 ~((1 << mgr->nr_vertex_buffers) - 1); 381 382 memset(fallback_vbs, ~0, sizeof(fallback_vbs)); 383 384 /* Find free slots for each type if needed. */ 385 for (type = 0; type < VB_NUM; type++) { 386 if (mask[type]) { 387 uint32_t index; 388 389 if (!unused_vb_mask) { 390 /* fail, reset the number to its original value */ 391 mgr->nr_real_vertex_buffers = mgr->nr_vertex_buffers; 392 return FALSE; 393 } 394 395 index = ffs(unused_vb_mask) - 1; 396 fallback_vbs[type] = index; 397 if (index >= mgr->nr_real_vertex_buffers) { 398 mgr->nr_real_vertex_buffers = index + 1; 399 } 400 /*printf("found slot=%i for type=%i\n", index, type);*/ 401 } 402 } 403 404 memcpy(mgr->fallback_vbs, fallback_vbs, sizeof(fallback_vbs)); 405 return TRUE; 406} 407 408static boolean 409u_vbuf_translate_begin(struct u_vbuf *mgr, 410 int start_vertex, unsigned num_vertices, 411 int start_instance, unsigned num_instances, 412 int start_index, unsigned num_indices, int min_index, 413 boolean unroll_indices) 414{ 415 unsigned mask[VB_NUM] = {0}; 416 struct translate_key key[VB_NUM]; 417 unsigned elem_index[VB_NUM][PIPE_MAX_ATTRIBS]; /* ... into key.elements */ 418 unsigned i, type; 419 420 int start[VB_NUM] = { 421 start_vertex, /* VERTEX */ 422 start_instance, /* INSTANCE */ 423 0 /* CONST */ 424 }; 425 426 unsigned num[VB_NUM] = { 427 num_vertices, /* VERTEX */ 428 num_instances, /* INSTANCE */ 429 1 /* CONST */ 430 }; 431 432 memset(key, 0, sizeof(key)); 433 memset(elem_index, ~0, sizeof(elem_index)); 434 435 /* See if there are vertex attribs of each type to translate and 436 * which ones. */ 437 for (i = 0; i < mgr->ve->count; i++) { 438 unsigned vb_index = mgr->ve->ve[i].vertex_buffer_index; 439 440 if (!mgr->vertex_buffer[vb_index].stride) { 441 if (!(mgr->ve->incompatible_elem_mask & (1 << i)) && 442 !(mgr->incompatible_vb_mask & (1 << vb_index))) { 443 continue; 444 } 445 mask[VB_CONST] |= 1 << vb_index; 446 } else if (mgr->ve->ve[i].instance_divisor) { 447 if (!(mgr->ve->incompatible_elem_mask & (1 << i)) && 448 !(mgr->incompatible_vb_mask & (1 << vb_index))) { 449 continue; 450 } 451 mask[VB_INSTANCE] |= 1 << vb_index; 452 } else { 453 if (!unroll_indices && 454 !(mgr->ve->incompatible_elem_mask & (1 << i)) && 455 !(mgr->incompatible_vb_mask & (1 << vb_index))) { 456 continue; 457 } 458 mask[VB_VERTEX] |= 1 << vb_index; 459 } 460 } 461 462 assert(mask[VB_VERTEX] || mask[VB_INSTANCE] || mask[VB_CONST]); 463 464 /* Find free vertex buffer slots. */ 465 if (!u_vbuf_translate_find_free_vb_slots(mgr, mask)) { 466 return FALSE; 467 } 468 469 /* Initialize the translate keys. */ 470 for (i = 0; i < mgr->ve->count; i++) { 471 struct translate_key *k; 472 struct translate_element *te; 473 unsigned bit, vb_index = mgr->ve->ve[i].vertex_buffer_index; 474 bit = 1 << vb_index; 475 476 if (!(mgr->ve->incompatible_elem_mask & (1 << i)) && 477 !(mgr->incompatible_vb_mask & (1 << vb_index)) && 478 (!unroll_indices || !(mask[VB_VERTEX] & bit))) { 479 continue; 480 } 481 482 /* Set type to what we will translate. 483 * Whether vertex, instance, or constant attribs. */ 484 for (type = 0; type < VB_NUM; type++) { 485 if (mask[type] & bit) { 486 break; 487 } 488 } 489 assert(type < VB_NUM); 490 assert(translate_is_output_format_supported(mgr->ve->native_format[i])); 491 /*printf("velem=%i type=%i\n", i, type);*/ 492 493 /* Add the vertex element. */ 494 k = &key[type]; 495 elem_index[type][i] = k->nr_elements; 496 497 te = &k->element[k->nr_elements]; 498 te->type = TRANSLATE_ELEMENT_NORMAL; 499 te->instance_divisor = 0; 500 te->input_buffer = vb_index; 501 te->input_format = mgr->ve->ve[i].src_format; 502 te->input_offset = mgr->ve->ve[i].src_offset; 503 te->output_format = mgr->ve->native_format[i]; 504 te->output_offset = k->output_stride; 505 506 k->output_stride += mgr->ve->native_format_size[i]; 507 k->nr_elements++; 508 } 509 510 /* Translate buffers. */ 511 for (type = 0; type < VB_NUM; type++) { 512 if (key[type].nr_elements) { 513 u_vbuf_translate_buffers(mgr, &key[type], mask[type], 514 mgr->fallback_vbs[type], 515 start[type], num[type], 516 start_index, num_indices, min_index, 517 unroll_indices && type == VB_VERTEX); 518 519 /* Fixup the stride for constant attribs. */ 520 if (type == VB_CONST) { 521 mgr->real_vertex_buffer[mgr->fallback_vbs[VB_CONST]].stride = 0; 522 } 523 } 524 } 525 526 /* Setup new vertex elements. */ 527 for (i = 0; i < mgr->ve->count; i++) { 528 for (type = 0; type < VB_NUM; type++) { 529 if (elem_index[type][i] < key[type].nr_elements) { 530 struct translate_element *te = &key[type].element[elem_index[type][i]]; 531 mgr->fallback_velems[i].instance_divisor = mgr->ve->ve[i].instance_divisor; 532 mgr->fallback_velems[i].src_format = te->output_format; 533 mgr->fallback_velems[i].src_offset = te->output_offset; 534 mgr->fallback_velems[i].vertex_buffer_index = mgr->fallback_vbs[type]; 535 536 /* elem_index[type][i] can only be set for one type. */ 537 assert(type > VB_INSTANCE || elem_index[type+1][i] == ~0); 538 assert(type > VB_VERTEX || elem_index[type+2][i] == ~0); 539 break; 540 } 541 } 542 /* No translating, just copy the original vertex element over. */ 543 if (type == VB_NUM) { 544 memcpy(&mgr->fallback_velems[i], &mgr->ve->ve[i], 545 sizeof(struct pipe_vertex_element)); 546 } 547 } 548 549 u_vbuf_set_vertex_elements_internal(mgr, mgr->ve->count, 550 mgr->fallback_velems); 551 mgr->using_translate = TRUE; 552 return TRUE; 553} 554 555static void u_vbuf_translate_end(struct u_vbuf *mgr) 556{ 557 unsigned i; 558 559 /* Restore vertex elements. */ 560 mgr->pipe->bind_vertex_elements_state(mgr->pipe, mgr->ve->driver_cso); 561 mgr->using_translate = FALSE; 562 563 /* Unreference the now-unused VBOs. */ 564 for (i = 0; i < VB_NUM; i++) { 565 unsigned vb = mgr->fallback_vbs[i]; 566 if (vb != ~0) { 567 pipe_resource_reference(&mgr->real_vertex_buffer[vb].buffer, NULL); 568 mgr->fallback_vbs[i] = ~0; 569 } 570 } 571 mgr->nr_real_vertex_buffers = mgr->nr_vertex_buffers; 572} 573 574#define FORMAT_REPLACE(what, withwhat) \ 575 case PIPE_FORMAT_##what: format = PIPE_FORMAT_##withwhat; break 576 577static void * 578u_vbuf_create_vertex_elements(struct u_vbuf *mgr, unsigned count, 579 const struct pipe_vertex_element *attribs) 580{ 581 struct pipe_context *pipe = mgr->pipe; 582 unsigned i; 583 struct pipe_vertex_element driver_attribs[PIPE_MAX_ATTRIBS]; 584 struct u_vbuf_elements *ve = CALLOC_STRUCT(u_vbuf_elements); 585 uint32_t used_buffers = 0; 586 587 ve->count = count; 588 589 memcpy(ve->ve, attribs, sizeof(struct pipe_vertex_element) * count); 590 memcpy(driver_attribs, attribs, sizeof(struct pipe_vertex_element) * count); 591 592 /* Set the best native format in case the original format is not 593 * supported. */ 594 for (i = 0; i < count; i++) { 595 enum pipe_format format = ve->ve[i].src_format; 596 597 ve->src_format_size[i] = util_format_get_blocksize(format); 598 599 used_buffers |= 1 << ve->ve[i].vertex_buffer_index; 600 601 if (!ve->ve[i].instance_divisor) { 602 ve->noninstance_vb_mask_any |= 1 << ve->ve[i].vertex_buffer_index; 603 } 604 605 /* Choose a native format. 606 * For now we don't care about the alignment, that's going to 607 * be sorted out later. */ 608 if (!mgr->caps.format_fixed32) { 609 switch (format) { 610 FORMAT_REPLACE(R32_FIXED, R32_FLOAT); 611 FORMAT_REPLACE(R32G32_FIXED, R32G32_FLOAT); 612 FORMAT_REPLACE(R32G32B32_FIXED, R32G32B32_FLOAT); 613 FORMAT_REPLACE(R32G32B32A32_FIXED, R32G32B32A32_FLOAT); 614 default:; 615 } 616 } 617 if (!mgr->caps.format_float16) { 618 switch (format) { 619 FORMAT_REPLACE(R16_FLOAT, R32_FLOAT); 620 FORMAT_REPLACE(R16G16_FLOAT, R32G32_FLOAT); 621 FORMAT_REPLACE(R16G16B16_FLOAT, R32G32B32_FLOAT); 622 FORMAT_REPLACE(R16G16B16A16_FLOAT, R32G32B32A32_FLOAT); 623 default:; 624 } 625 } 626 if (!mgr->caps.format_float64) { 627 switch (format) { 628 FORMAT_REPLACE(R64_FLOAT, R32_FLOAT); 629 FORMAT_REPLACE(R64G64_FLOAT, R32G32_FLOAT); 630 FORMAT_REPLACE(R64G64B64_FLOAT, R32G32B32_FLOAT); 631 FORMAT_REPLACE(R64G64B64A64_FLOAT, R32G32B32A32_FLOAT); 632 default:; 633 } 634 } 635 if (!mgr->caps.format_norm32) { 636 switch (format) { 637 FORMAT_REPLACE(R32_UNORM, R32_FLOAT); 638 FORMAT_REPLACE(R32G32_UNORM, R32G32_FLOAT); 639 FORMAT_REPLACE(R32G32B32_UNORM, R32G32B32_FLOAT); 640 FORMAT_REPLACE(R32G32B32A32_UNORM, R32G32B32A32_FLOAT); 641 FORMAT_REPLACE(R32_SNORM, R32_FLOAT); 642 FORMAT_REPLACE(R32G32_SNORM, R32G32_FLOAT); 643 FORMAT_REPLACE(R32G32B32_SNORM, R32G32B32_FLOAT); 644 FORMAT_REPLACE(R32G32B32A32_SNORM, R32G32B32A32_FLOAT); 645 default:; 646 } 647 } 648 if (!mgr->caps.format_scaled32) { 649 switch (format) { 650 FORMAT_REPLACE(R32_USCALED, R32_FLOAT); 651 FORMAT_REPLACE(R32G32_USCALED, R32G32_FLOAT); 652 FORMAT_REPLACE(R32G32B32_USCALED, R32G32B32_FLOAT); 653 FORMAT_REPLACE(R32G32B32A32_USCALED,R32G32B32A32_FLOAT); 654 FORMAT_REPLACE(R32_SSCALED, R32_FLOAT); 655 FORMAT_REPLACE(R32G32_SSCALED, R32G32_FLOAT); 656 FORMAT_REPLACE(R32G32B32_SSCALED, R32G32B32_FLOAT); 657 FORMAT_REPLACE(R32G32B32A32_SSCALED,R32G32B32A32_FLOAT); 658 default:; 659 } 660 } 661 662 driver_attribs[i].src_format = format; 663 ve->native_format[i] = format; 664 ve->native_format_size[i] = 665 util_format_get_blocksize(ve->native_format[i]); 666 667 if (ve->ve[i].src_format != format || 668 (!mgr->caps.velem_src_offset_unaligned && 669 ve->ve[i].src_offset % 4 != 0)) { 670 ve->incompatible_elem_mask |= 1 << i; 671 ve->incompatible_vb_mask_any |= 1 << ve->ve[i].vertex_buffer_index; 672 } else { 673 ve->compatible_vb_mask_any |= 1 << ve->ve[i].vertex_buffer_index; 674 } 675 } 676 677 ve->compatible_vb_mask_all = ~ve->incompatible_vb_mask_any & used_buffers; 678 ve->incompatible_vb_mask_all = ~ve->compatible_vb_mask_any & used_buffers; 679 680 /* Align the formats to the size of DWORD if needed. */ 681 if (!mgr->caps.velem_src_offset_unaligned) { 682 for (i = 0; i < count; i++) { 683 ve->native_format_size[i] = align(ve->native_format_size[i], 4); 684 } 685 } 686 687 ve->driver_cso = 688 pipe->create_vertex_elements_state(pipe, count, driver_attribs); 689 return ve; 690} 691 692static void u_vbuf_delete_vertex_elements(struct u_vbuf *mgr, void *cso) 693{ 694 struct pipe_context *pipe = mgr->pipe; 695 struct u_vbuf_elements *ve = cso; 696 697 pipe->delete_vertex_elements_state(pipe, ve->driver_cso); 698 FREE(ve); 699} 700 701void u_vbuf_set_vertex_buffers(struct u_vbuf *mgr, unsigned count, 702 const struct pipe_vertex_buffer *bufs) 703{ 704 unsigned i; 705 706 mgr->user_vb_mask = 0; 707 mgr->incompatible_vb_mask = 0; 708 mgr->nonzero_stride_vb_mask = 0; 709 710 for (i = 0; i < count; i++) { 711 const struct pipe_vertex_buffer *vb = &bufs[i]; 712 struct pipe_vertex_buffer *orig_vb = &mgr->vertex_buffer[i]; 713 struct pipe_vertex_buffer *real_vb = &mgr->real_vertex_buffer[i]; 714 715 pipe_resource_reference(&orig_vb->buffer, vb->buffer); 716 orig_vb->user_buffer = vb->user_buffer; 717 718 real_vb->buffer_offset = orig_vb->buffer_offset = vb->buffer_offset; 719 real_vb->stride = orig_vb->stride = vb->stride; 720 721 if (vb->stride) { 722 mgr->nonzero_stride_vb_mask |= 1 << i; 723 } 724 725 if (!vb->buffer && !vb->user_buffer) { 726 pipe_resource_reference(&real_vb->buffer, NULL); 727 continue; 728 } 729 730 if ((!mgr->caps.buffer_offset_unaligned && vb->buffer_offset % 4 != 0) || 731 (!mgr->caps.buffer_stride_unaligned && vb->stride % 4 != 0)) { 732 mgr->incompatible_vb_mask |= 1 << i; 733 pipe_resource_reference(&real_vb->buffer, NULL); 734 continue; 735 } 736 737 if (!mgr->caps.user_vertex_buffers && vb->user_buffer) { 738 mgr->user_vb_mask |= 1 << i; 739 pipe_resource_reference(&real_vb->buffer, NULL); 740 continue; 741 } 742 743 pipe_resource_reference(&real_vb->buffer, vb->buffer); 744 } 745 746 for (i = count; i < mgr->nr_vertex_buffers; i++) { 747 pipe_resource_reference(&mgr->vertex_buffer[i].buffer, NULL); 748 } 749 for (i = count; i < mgr->nr_real_vertex_buffers; i++) { 750 pipe_resource_reference(&mgr->real_vertex_buffer[i].buffer, NULL); 751 } 752 753 mgr->nr_vertex_buffers = count; 754 mgr->nr_real_vertex_buffers = count; 755 mgr->vertex_buffers_dirty = TRUE; 756} 757 758void u_vbuf_set_index_buffer(struct u_vbuf *mgr, 759 const struct pipe_index_buffer *ib) 760{ 761 struct pipe_context *pipe = mgr->pipe; 762 763 if (ib) { 764 assert(ib->offset % ib->index_size == 0); 765 pipe_resource_reference(&mgr->index_buffer.buffer, ib->buffer); 766 memcpy(&mgr->index_buffer, ib, sizeof(*ib)); 767 } else { 768 pipe_resource_reference(&mgr->index_buffer.buffer, NULL); 769 } 770 771 pipe->set_index_buffer(pipe, ib); 772} 773 774static void 775u_vbuf_upload_buffers(struct u_vbuf *mgr, 776 int start_vertex, unsigned num_vertices, 777 int start_instance, unsigned num_instances) 778{ 779 unsigned i; 780 unsigned nr_velems = mgr->ve->count; 781 unsigned nr_vbufs = mgr->nr_vertex_buffers; 782 struct pipe_vertex_element *velems = 783 mgr->using_translate ? mgr->fallback_velems : mgr->ve->ve; 784 unsigned start_offset[PIPE_MAX_ATTRIBS]; 785 unsigned end_offset[PIPE_MAX_ATTRIBS] = {0}; 786 787 /* Determine how much data needs to be uploaded. */ 788 for (i = 0; i < nr_velems; i++) { 789 struct pipe_vertex_element *velem = &velems[i]; 790 unsigned index = velem->vertex_buffer_index; 791 struct pipe_vertex_buffer *vb = &mgr->vertex_buffer[index]; 792 unsigned instance_div, first, size; 793 794 /* Skip the buffers generated by translate. */ 795 if (index == mgr->fallback_vbs[VB_VERTEX] || 796 index == mgr->fallback_vbs[VB_INSTANCE] || 797 index == mgr->fallback_vbs[VB_CONST]) { 798 continue; 799 } 800 801 if (!vb->user_buffer) { 802 continue; 803 } 804 805 instance_div = velem->instance_divisor; 806 first = vb->buffer_offset + velem->src_offset; 807 808 if (!vb->stride) { 809 /* Constant attrib. */ 810 size = mgr->ve->src_format_size[i]; 811 } else if (instance_div) { 812 /* Per-instance attrib. */ 813 unsigned count = (num_instances + instance_div - 1) / instance_div; 814 first += vb->stride * start_instance; 815 size = vb->stride * (count - 1) + mgr->ve->src_format_size[i]; 816 } else { 817 /* Per-vertex attrib. */ 818 first += vb->stride * start_vertex; 819 size = vb->stride * (num_vertices - 1) + mgr->ve->src_format_size[i]; 820 } 821 822 /* Update offsets. */ 823 if (!end_offset[index]) { 824 start_offset[index] = first; 825 end_offset[index] = first + size; 826 } else { 827 if (first < start_offset[index]) 828 start_offset[index] = first; 829 if (first + size > end_offset[index]) 830 end_offset[index] = first + size; 831 } 832 } 833 834 /* Upload buffers. */ 835 for (i = 0; i < nr_vbufs; i++) { 836 unsigned start, end = end_offset[i]; 837 struct pipe_vertex_buffer *real_vb; 838 const uint8_t *ptr; 839 840 if (!end) { 841 continue; 842 } 843 844 start = start_offset[i]; 845 assert(start < end); 846 847 real_vb = &mgr->real_vertex_buffer[i]; 848 ptr = mgr->vertex_buffer[i].user_buffer; 849 850 u_upload_data(mgr->uploader, start, end - start, ptr + start, 851 &real_vb->buffer_offset, &real_vb->buffer); 852 853 real_vb->buffer_offset -= start; 854 } 855} 856 857static boolean u_vbuf_need_minmax_index(struct u_vbuf *mgr) 858{ 859 /* See if there are any per-vertex attribs which will be uploaded or 860 * translated. Use bitmasks to get the info instead of looping over vertex 861 * elements. */ 862 return ((mgr->user_vb_mask | mgr->incompatible_vb_mask | 863 mgr->ve->incompatible_vb_mask_any) & 864 mgr->ve->noninstance_vb_mask_any & mgr->nonzero_stride_vb_mask) != 0; 865} 866 867static boolean u_vbuf_mapping_vertex_buffer_blocks(struct u_vbuf *mgr) 868{ 869 /* Return true if there are hw buffers which don't need to be translated. 870 * 871 * We could query whether each buffer is busy, but that would 872 * be way more costly than this. */ 873 return (~mgr->user_vb_mask & ~mgr->incompatible_vb_mask & 874 mgr->ve->compatible_vb_mask_all & mgr->ve->noninstance_vb_mask_any & 875 mgr->nonzero_stride_vb_mask) != 0; 876} 877 878static void u_vbuf_get_minmax_index(struct pipe_context *pipe, 879 struct pipe_index_buffer *ib, 880 const struct pipe_draw_info *info, 881 int *out_min_index, 882 int *out_max_index) 883{ 884 struct pipe_transfer *transfer = NULL; 885 const void *indices; 886 unsigned i; 887 unsigned restart_index = info->restart_index; 888 889 if (ib->user_buffer) { 890 indices = (uint8_t*)ib->user_buffer + 891 ib->offset + info->start * ib->index_size; 892 } else { 893 indices = pipe_buffer_map_range(pipe, ib->buffer, 894 ib->offset + info->start * ib->index_size, 895 info->count * ib->index_size, 896 PIPE_TRANSFER_READ, &transfer); 897 } 898 899 switch (ib->index_size) { 900 case 4: { 901 const unsigned *ui_indices = (const unsigned*)indices; 902 unsigned max_ui = 0; 903 unsigned min_ui = ~0U; 904 if (info->primitive_restart) { 905 for (i = 0; i < info->count; i++) { 906 if (ui_indices[i] != restart_index) { 907 if (ui_indices[i] > max_ui) max_ui = ui_indices[i]; 908 if (ui_indices[i] < min_ui) min_ui = ui_indices[i]; 909 } 910 } 911 } 912 else { 913 for (i = 0; i < info->count; i++) { 914 if (ui_indices[i] > max_ui) max_ui = ui_indices[i]; 915 if (ui_indices[i] < min_ui) min_ui = ui_indices[i]; 916 } 917 } 918 *out_min_index = min_ui; 919 *out_max_index = max_ui; 920 break; 921 } 922 case 2: { 923 const unsigned short *us_indices = (const unsigned short*)indices; 924 unsigned max_us = 0; 925 unsigned min_us = ~0U; 926 if (info->primitive_restart) { 927 for (i = 0; i < info->count; i++) { 928 if (us_indices[i] != restart_index) { 929 if (us_indices[i] > max_us) max_us = us_indices[i]; 930 if (us_indices[i] < min_us) min_us = us_indices[i]; 931 } 932 } 933 } 934 else { 935 for (i = 0; i < info->count; i++) { 936 if (us_indices[i] > max_us) max_us = us_indices[i]; 937 if (us_indices[i] < min_us) min_us = us_indices[i]; 938 } 939 } 940 *out_min_index = min_us; 941 *out_max_index = max_us; 942 break; 943 } 944 case 1: { 945 const unsigned char *ub_indices = (const unsigned char*)indices; 946 unsigned max_ub = 0; 947 unsigned min_ub = ~0U; 948 if (info->primitive_restart) { 949 for (i = 0; i < info->count; i++) { 950 if (ub_indices[i] != restart_index) { 951 if (ub_indices[i] > max_ub) max_ub = ub_indices[i]; 952 if (ub_indices[i] < min_ub) min_ub = ub_indices[i]; 953 } 954 } 955 } 956 else { 957 for (i = 0; i < info->count; i++) { 958 if (ub_indices[i] > max_ub) max_ub = ub_indices[i]; 959 if (ub_indices[i] < min_ub) min_ub = ub_indices[i]; 960 } 961 } 962 *out_min_index = min_ub; 963 *out_max_index = max_ub; 964 break; 965 } 966 default: 967 assert(0); 968 *out_min_index = 0; 969 *out_max_index = 0; 970 } 971 972 if (transfer) { 973 pipe_buffer_unmap(pipe, transfer); 974 } 975} 976 977void u_vbuf_draw_vbo(struct u_vbuf *mgr, const struct pipe_draw_info *info) 978{ 979 struct pipe_context *pipe = mgr->pipe; 980 int start_vertex, min_index; 981 unsigned num_vertices; 982 boolean unroll_indices = FALSE; 983 uint32_t user_vb_mask = mgr->user_vb_mask; 984 985 /* Normal draw. No fallback and no user buffers. */ 986 if (!mgr->incompatible_vb_mask && 987 !mgr->ve->incompatible_elem_mask && 988 !user_vb_mask) { 989 /* Set vertex buffers if needed. */ 990 if (mgr->vertex_buffers_dirty) { 991 pipe->set_vertex_buffers(pipe, mgr->nr_real_vertex_buffers, 992 mgr->real_vertex_buffer); 993 mgr->vertex_buffers_dirty = FALSE; 994 } 995 996 pipe->draw_vbo(pipe, info); 997 return; 998 } 999 1000 if (info->indexed) { 1001 /* See if anything needs to be done for per-vertex attribs. */ 1002 if (u_vbuf_need_minmax_index(mgr)) { 1003 int max_index; 1004 1005 if (info->max_index != ~0) { 1006 min_index = info->min_index; 1007 max_index = info->max_index; 1008 } else { 1009 u_vbuf_get_minmax_index(mgr->pipe, &mgr->index_buffer, info, 1010 &min_index, &max_index); 1011 } 1012 1013 assert(min_index <= max_index); 1014 1015 start_vertex = min_index + info->index_bias; 1016 num_vertices = max_index + 1 - min_index; 1017 1018 /* Primitive restart doesn't work when unrolling indices. 1019 * We would have to break this drawing operation into several ones. */ 1020 /* Use some heuristic to see if unrolling indices improves 1021 * performance. */ 1022 if (!info->primitive_restart && 1023 num_vertices > info->count*2 && 1024 num_vertices-info->count > 32 && 1025 !u_vbuf_mapping_vertex_buffer_blocks(mgr)) { 1026 /*printf("num_vertices=%i count=%i\n", num_vertices, info->count);*/ 1027 unroll_indices = TRUE; 1028 user_vb_mask &= ~(mgr->nonzero_stride_vb_mask & 1029 mgr->ve->noninstance_vb_mask_any); 1030 } 1031 } else { 1032 /* Nothing to do for per-vertex attribs. */ 1033 start_vertex = 0; 1034 num_vertices = 0; 1035 min_index = 0; 1036 } 1037 } else { 1038 start_vertex = info->start; 1039 num_vertices = info->count; 1040 min_index = 0; 1041 } 1042 1043 /* Translate vertices with non-native layouts or formats. */ 1044 if (unroll_indices || 1045 mgr->incompatible_vb_mask || 1046 mgr->ve->incompatible_elem_mask) { 1047 /* XXX check the return value */ 1048 u_vbuf_translate_begin(mgr, start_vertex, num_vertices, 1049 info->start_instance, info->instance_count, 1050 info->start, info->count, min_index, 1051 unroll_indices); 1052 1053 user_vb_mask &= ~(mgr->incompatible_vb_mask | 1054 mgr->ve->incompatible_vb_mask_all); 1055 } 1056 1057 /* Upload user buffers. */ 1058 if (user_vb_mask) { 1059 u_vbuf_upload_buffers(mgr, start_vertex, num_vertices, 1060 info->start_instance, info->instance_count); 1061 } 1062 1063 /* 1064 if (unroll_indices) { 1065 printf("unrolling indices: start_vertex = %i, num_vertices = %i\n", 1066 start_vertex, num_vertices); 1067 util_dump_draw_info(stdout, info); 1068 printf("\n"); 1069 } 1070 1071 unsigned i; 1072 for (i = 0; i < mgr->nr_vertex_buffers; i++) { 1073 printf("input %i: ", i); 1074 util_dump_vertex_buffer(stdout, mgr->vertex_buffer+i); 1075 printf("\n"); 1076 } 1077 for (i = 0; i < mgr->nr_real_vertex_buffers; i++) { 1078 printf("real %i: ", i); 1079 util_dump_vertex_buffer(stdout, mgr->real_vertex_buffer+i); 1080 printf("\n"); 1081 } 1082 */ 1083 1084 u_upload_unmap(mgr->uploader); 1085 pipe->set_vertex_buffers(pipe, mgr->nr_real_vertex_buffers, 1086 mgr->real_vertex_buffer); 1087 1088 if (unlikely(unroll_indices)) { 1089 struct pipe_draw_info new_info = *info; 1090 new_info.indexed = FALSE; 1091 new_info.index_bias = 0; 1092 new_info.min_index = 0; 1093 new_info.max_index = info->count - 1; 1094 new_info.start = 0; 1095 1096 pipe->draw_vbo(pipe, &new_info); 1097 } else { 1098 pipe->draw_vbo(pipe, info); 1099 } 1100 1101 if (mgr->using_translate) { 1102 u_vbuf_translate_end(mgr); 1103 } 1104 mgr->vertex_buffers_dirty = TRUE; 1105} 1106 1107void u_vbuf_save_vertex_elements(struct u_vbuf *mgr) 1108{ 1109 assert(!mgr->ve_saved); 1110 mgr->ve_saved = mgr->ve; 1111} 1112 1113void u_vbuf_restore_vertex_elements(struct u_vbuf *mgr) 1114{ 1115 if (mgr->ve != mgr->ve_saved) { 1116 struct pipe_context *pipe = mgr->pipe; 1117 1118 mgr->ve = mgr->ve_saved; 1119 pipe->bind_vertex_elements_state(pipe, 1120 mgr->ve ? mgr->ve->driver_cso : NULL); 1121 } 1122 mgr->ve_saved = NULL; 1123} 1124 1125void u_vbuf_save_vertex_buffers(struct u_vbuf *mgr) 1126{ 1127 util_copy_vertex_buffers(mgr->vertex_buffer_saved, 1128 &mgr->nr_vertex_buffers_saved, 1129 mgr->vertex_buffer, 1130 mgr->nr_vertex_buffers); 1131} 1132 1133void u_vbuf_restore_vertex_buffers(struct u_vbuf *mgr) 1134{ 1135 unsigned i; 1136 1137 u_vbuf_set_vertex_buffers(mgr, mgr->nr_vertex_buffers_saved, 1138 mgr->vertex_buffer_saved); 1139 for (i = 0; i < mgr->nr_vertex_buffers_saved; i++) { 1140 pipe_resource_reference(&mgr->vertex_buffer_saved[i].buffer, NULL); 1141 } 1142 mgr->nr_vertex_buffers_saved = 0; 1143} 1144