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