u_vbuf.c revision fb0aa34fab77fe8a7fc3253d5ecf635ce37a21c7
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_format.h" 31#include "util/u_inlines.h" 32#include "util/u_memory.h" 33#include "util/u_upload_mgr.h" 34#include "translate/translate.h" 35#include "translate/translate_cache.h" 36 37struct u_vbuf_elements { 38 unsigned count; 39 struct pipe_vertex_element ve[PIPE_MAX_ATTRIBS]; 40 41 unsigned src_format_size[PIPE_MAX_ATTRIBS]; 42 43 /* If (velem[i].src_format != native_format[i]), the vertex buffer 44 * referenced by the vertex element cannot be used for rendering and 45 * its vertex data must be translated to native_format[i]. */ 46 enum pipe_format native_format[PIPE_MAX_ATTRIBS]; 47 unsigned native_format_size[PIPE_MAX_ATTRIBS]; 48 49 /* This might mean two things: 50 * - src_format != native_format, as discussed above. 51 * - src_offset % 4 != 0 (if the caps don't allow such an offset). */ 52 boolean incompatible_layout; 53 /* Per-element flags. */ 54 boolean incompatible_layout_elem[PIPE_MAX_ATTRIBS]; 55}; 56 57struct u_vbuf_priv { 58 struct u_vbuf b; 59 struct pipe_context *pipe; 60 struct translate_cache *translate_cache; 61 62 /* Vertex element state bound by the state tracker. */ 63 void *saved_ve; 64 /* and its associated helper structure for this module. */ 65 struct u_vbuf_elements *ve; 66 67 /* Vertex elements used for the translate fallback. */ 68 struct pipe_vertex_element fallback_velems[PIPE_MAX_ATTRIBS]; 69 /* If non-NULL, this is a vertex element state used for the translate 70 * fallback and therefore used for rendering too. */ 71 void *fallback_ve; 72 /* The vertex buffer slot index where translated vertices have been 73 * stored in. */ 74 unsigned fallback_vb_slot; 75 /* When binding the fallback vertex element state, we don't want to 76 * change saved_ve and ve. This is set to TRUE in such cases. */ 77 boolean ve_binding_lock; 78 79 /* Whether there is any user buffer. */ 80 boolean any_user_vbs; 81 /* Whether there is a buffer with a non-native layout. */ 82 boolean incompatible_vb_layout; 83 /* Per-buffer flags. */ 84 boolean incompatible_vb[PIPE_MAX_ATTRIBS]; 85}; 86 87static void u_vbuf_init_format_caps(struct u_vbuf_priv *mgr) 88{ 89 struct pipe_screen *screen = mgr->pipe->screen; 90 91 mgr->b.caps.format_fixed32 = 92 screen->is_format_supported(screen, PIPE_FORMAT_R32_FIXED, PIPE_BUFFER, 93 0, PIPE_BIND_VERTEX_BUFFER); 94 95 mgr->b.caps.format_float16 = 96 screen->is_format_supported(screen, PIPE_FORMAT_R16_FLOAT, PIPE_BUFFER, 97 0, PIPE_BIND_VERTEX_BUFFER); 98 99 mgr->b.caps.format_float64 = 100 screen->is_format_supported(screen, PIPE_FORMAT_R64_FLOAT, PIPE_BUFFER, 101 0, PIPE_BIND_VERTEX_BUFFER); 102 103 mgr->b.caps.format_norm32 = 104 screen->is_format_supported(screen, PIPE_FORMAT_R32_UNORM, PIPE_BUFFER, 105 0, PIPE_BIND_VERTEX_BUFFER) && 106 screen->is_format_supported(screen, PIPE_FORMAT_R32_SNORM, PIPE_BUFFER, 107 0, PIPE_BIND_VERTEX_BUFFER); 108 109 mgr->b.caps.format_scaled32 = 110 screen->is_format_supported(screen, PIPE_FORMAT_R32_USCALED, PIPE_BUFFER, 111 0, PIPE_BIND_VERTEX_BUFFER) && 112 screen->is_format_supported(screen, PIPE_FORMAT_R32_SSCALED, PIPE_BUFFER, 113 0, PIPE_BIND_VERTEX_BUFFER); 114} 115 116struct u_vbuf * 117u_vbuf_create(struct pipe_context *pipe, 118 unsigned upload_buffer_size, 119 unsigned upload_buffer_alignment, 120 unsigned upload_buffer_bind, 121 enum u_fetch_alignment fetch_alignment) 122{ 123 struct u_vbuf_priv *mgr = CALLOC_STRUCT(u_vbuf_priv); 124 125 mgr->pipe = pipe; 126 mgr->translate_cache = translate_cache_create(); 127 mgr->fallback_vb_slot = ~0; 128 129 mgr->b.uploader = u_upload_create(pipe, upload_buffer_size, 130 upload_buffer_alignment, 131 upload_buffer_bind); 132 133 mgr->b.caps.fetch_dword_unaligned = 134 fetch_alignment == U_VERTEX_FETCH_BYTE_ALIGNED; 135 136 u_vbuf_init_format_caps(mgr); 137 138 return &mgr->b; 139} 140 141void u_vbuf_destroy(struct u_vbuf *mgrb) 142{ 143 struct u_vbuf_priv *mgr = (struct u_vbuf_priv*)mgrb; 144 unsigned i; 145 146 for (i = 0; i < mgr->b.nr_vertex_buffers; i++) { 147 pipe_resource_reference(&mgr->b.vertex_buffer[i].buffer, NULL); 148 } 149 for (i = 0; i < mgr->b.nr_real_vertex_buffers; i++) { 150 pipe_resource_reference(&mgr->b.real_vertex_buffer[i].buffer, NULL); 151 } 152 153 translate_cache_destroy(mgr->translate_cache); 154 u_upload_destroy(mgr->b.uploader); 155 FREE(mgr); 156} 157 158 159static unsigned u_vbuf_get_free_real_vb_slot(struct u_vbuf_priv *mgr) 160{ 161 unsigned i, nr = mgr->ve->count; 162 boolean used_vb[PIPE_MAX_ATTRIBS] = {0}; 163 164 for (i = 0; i < nr; i++) { 165 if (!mgr->ve->incompatible_layout_elem[i]) { 166 unsigned index = mgr->ve->ve[i].vertex_buffer_index; 167 168 if (!mgr->incompatible_vb[index]) { 169 used_vb[index] = TRUE; 170 } 171 } 172 } 173 174 for (i = 0; i < PIPE_MAX_ATTRIBS; i++) { 175 if (!used_vb[i]) { 176 if (i >= mgr->b.nr_real_vertex_buffers) { 177 mgr->b.nr_real_vertex_buffers = i+1; 178 } 179 return i; 180 } 181 } 182 return ~0; 183} 184 185static void 186u_vbuf_translate_begin(struct u_vbuf_priv *mgr, 187 int min_index, int max_index) 188{ 189 struct translate_key key; 190 struct translate_element *te; 191 unsigned tr_elem_index[PIPE_MAX_ATTRIBS]; 192 struct translate *tr; 193 boolean vb_translated[PIPE_MAX_ATTRIBS] = {0}; 194 uint8_t *out_map; 195 struct pipe_transfer *vb_transfer[PIPE_MAX_ATTRIBS] = {0}; 196 struct pipe_resource *out_buffer = NULL; 197 unsigned i, num_verts, out_offset; 198 boolean upload_flushed = FALSE; 199 200 memset(&key, 0, sizeof(key)); 201 memset(tr_elem_index, 0xff, sizeof(tr_elem_index)); 202 203 /* Get a new vertex buffer slot. */ 204 mgr->fallback_vb_slot = u_vbuf_get_free_real_vb_slot(mgr); 205 206 if (mgr->fallback_vb_slot == ~0) { 207 return; /* XXX error, not enough attribs */ 208 } 209 210 /* Initialize the description of how vertices should be translated. */ 211 for (i = 0; i < mgr->ve->count; i++) { 212 enum pipe_format output_format = mgr->ve->native_format[i]; 213 unsigned output_format_size = mgr->ve->native_format_size[i]; 214 215 /* Check for support. */ 216 if (!mgr->ve->incompatible_layout_elem[i] && 217 !mgr->incompatible_vb[mgr->ve->ve[i].vertex_buffer_index]) { 218 continue; 219 } 220 221 assert(translate_is_output_format_supported(output_format)); 222 223 /* Add this vertex element. */ 224 te = &key.element[key.nr_elements]; 225 te->type = TRANSLATE_ELEMENT_NORMAL; 226 te->instance_divisor = 0; 227 te->input_buffer = mgr->ve->ve[i].vertex_buffer_index; 228 te->input_format = mgr->ve->ve[i].src_format; 229 te->input_offset = mgr->ve->ve[i].src_offset; 230 te->output_format = output_format; 231 te->output_offset = key.output_stride; 232 233 key.output_stride += output_format_size; 234 vb_translated[mgr->ve->ve[i].vertex_buffer_index] = TRUE; 235 tr_elem_index[i] = key.nr_elements; 236 key.nr_elements++; 237 } 238 239 /* Get a translate object. */ 240 tr = translate_cache_find(mgr->translate_cache, &key); 241 242 /* Map buffers we want to translate. */ 243 for (i = 0; i < mgr->b.nr_vertex_buffers; i++) { 244 if (vb_translated[i]) { 245 struct pipe_vertex_buffer *vb = &mgr->b.vertex_buffer[i]; 246 247 uint8_t *map = pipe_buffer_map(mgr->pipe, vb->buffer, 248 PIPE_TRANSFER_READ, &vb_transfer[i]); 249 250 tr->set_buffer(tr, i, 251 map + vb->buffer_offset + vb->stride * min_index, 252 vb->stride, ~0); 253 } 254 } 255 256 /* Create and map the output buffer. */ 257 num_verts = max_index + 1 - min_index; 258 259 u_upload_alloc(mgr->b.uploader, 260 key.output_stride * min_index, 261 key.output_stride * num_verts, 262 &out_offset, &out_buffer, &upload_flushed, 263 (void**)&out_map); 264 265 out_offset -= key.output_stride * min_index; 266 267 /* Translate. */ 268 tr->run(tr, 0, num_verts, 0, out_map); 269 270 /* Unmap all buffers. */ 271 for (i = 0; i < mgr->b.nr_vertex_buffers; i++) { 272 if (vb_translated[i]) { 273 pipe_buffer_unmap(mgr->pipe, vb_transfer[i]); 274 } 275 } 276 277 /* Setup the new vertex buffer. */ 278 mgr->b.real_vertex_buffer[mgr->fallback_vb_slot].buffer_offset = out_offset; 279 mgr->b.real_vertex_buffer[mgr->fallback_vb_slot].stride = key.output_stride; 280 281 /* Move the buffer reference. */ 282 pipe_resource_reference( 283 &mgr->b.real_vertex_buffer[mgr->fallback_vb_slot].buffer, NULL); 284 mgr->b.real_vertex_buffer[mgr->fallback_vb_slot].buffer = out_buffer; 285 out_buffer = NULL; 286 287 /* Setup new vertex elements. */ 288 for (i = 0; i < mgr->ve->count; i++) { 289 if (tr_elem_index[i] < key.nr_elements) { 290 te = &key.element[tr_elem_index[i]]; 291 mgr->fallback_velems[i].instance_divisor = mgr->ve->ve[i].instance_divisor; 292 mgr->fallback_velems[i].src_format = te->output_format; 293 mgr->fallback_velems[i].src_offset = te->output_offset; 294 mgr->fallback_velems[i].vertex_buffer_index = mgr->fallback_vb_slot; 295 } else { 296 memcpy(&mgr->fallback_velems[i], &mgr->ve->ve[i], 297 sizeof(struct pipe_vertex_element)); 298 } 299 } 300 301 302 mgr->fallback_ve = 303 mgr->pipe->create_vertex_elements_state(mgr->pipe, mgr->ve->count, 304 mgr->fallback_velems); 305 306 /* Preserve saved_ve. */ 307 mgr->ve_binding_lock = TRUE; 308 mgr->pipe->bind_vertex_elements_state(mgr->pipe, mgr->fallback_ve); 309 mgr->ve_binding_lock = FALSE; 310} 311 312static void u_vbuf_translate_end(struct u_vbuf_priv *mgr) 313{ 314 if (mgr->fallback_ve == NULL) { 315 return; 316 } 317 318 /* Restore vertex elements. */ 319 /* Note that saved_ve will be overwritten in bind_vertex_elements_state. */ 320 mgr->pipe->bind_vertex_elements_state(mgr->pipe, mgr->saved_ve); 321 mgr->pipe->delete_vertex_elements_state(mgr->pipe, mgr->fallback_ve); 322 mgr->fallback_ve = NULL; 323 324 /* Delete the now-unused VBO. */ 325 pipe_resource_reference(&mgr->b.real_vertex_buffer[mgr->fallback_vb_slot].buffer, 326 NULL); 327 mgr->fallback_vb_slot = ~0; 328 mgr->b.nr_real_vertex_buffers = mgr->b.nr_vertex_buffers; 329} 330 331#define FORMAT_REPLACE(what, withwhat) \ 332 case PIPE_FORMAT_##what: format = PIPE_FORMAT_##withwhat; break 333 334struct u_vbuf_elements * 335u_vbuf_create_vertex_elements(struct u_vbuf *mgrb, 336 unsigned count, 337 const struct pipe_vertex_element *attribs, 338 struct pipe_vertex_element *native_attribs) 339{ 340 struct u_vbuf_priv *mgr = (struct u_vbuf_priv*)mgrb; 341 unsigned i; 342 struct u_vbuf_elements *ve = CALLOC_STRUCT(u_vbuf_elements); 343 344 ve->count = count; 345 346 if (!count) { 347 return ve; 348 } 349 350 memcpy(ve->ve, attribs, sizeof(struct pipe_vertex_element) * count); 351 memcpy(native_attribs, attribs, sizeof(struct pipe_vertex_element) * count); 352 353 /* Set the best native format in case the original format is not 354 * supported. */ 355 for (i = 0; i < count; i++) { 356 enum pipe_format format = ve->ve[i].src_format; 357 358 ve->src_format_size[i] = util_format_get_blocksize(format); 359 360 /* Choose a native format. 361 * For now we don't care about the alignment, that's going to 362 * be sorted out later. */ 363 if (!mgr->b.caps.format_fixed32) { 364 switch (format) { 365 FORMAT_REPLACE(R32_FIXED, R32_FLOAT); 366 FORMAT_REPLACE(R32G32_FIXED, R32G32_FLOAT); 367 FORMAT_REPLACE(R32G32B32_FIXED, R32G32B32_FLOAT); 368 FORMAT_REPLACE(R32G32B32A32_FIXED, R32G32B32A32_FLOAT); 369 default:; 370 } 371 } 372 if (!mgr->b.caps.format_float16) { 373 switch (format) { 374 FORMAT_REPLACE(R16_FLOAT, R32_FLOAT); 375 FORMAT_REPLACE(R16G16_FLOAT, R32G32_FLOAT); 376 FORMAT_REPLACE(R16G16B16_FLOAT, R32G32B32_FLOAT); 377 FORMAT_REPLACE(R16G16B16A16_FLOAT, R32G32B32A32_FLOAT); 378 default:; 379 } 380 } 381 if (!mgr->b.caps.format_float64) { 382 switch (format) { 383 FORMAT_REPLACE(R64_FLOAT, R32_FLOAT); 384 FORMAT_REPLACE(R64G64_FLOAT, R32G32_FLOAT); 385 FORMAT_REPLACE(R64G64B64_FLOAT, R32G32B32_FLOAT); 386 FORMAT_REPLACE(R64G64B64A64_FLOAT, R32G32B32A32_FLOAT); 387 default:; 388 } 389 } 390 if (!mgr->b.caps.format_norm32) { 391 switch (format) { 392 FORMAT_REPLACE(R32_UNORM, R32_FLOAT); 393 FORMAT_REPLACE(R32G32_UNORM, R32G32_FLOAT); 394 FORMAT_REPLACE(R32G32B32_UNORM, R32G32B32_FLOAT); 395 FORMAT_REPLACE(R32G32B32A32_UNORM, R32G32B32A32_FLOAT); 396 FORMAT_REPLACE(R32_SNORM, R32_FLOAT); 397 FORMAT_REPLACE(R32G32_SNORM, R32G32_FLOAT); 398 FORMAT_REPLACE(R32G32B32_SNORM, R32G32B32_FLOAT); 399 FORMAT_REPLACE(R32G32B32A32_SNORM, R32G32B32A32_FLOAT); 400 default:; 401 } 402 } 403 if (!mgr->b.caps.format_scaled32) { 404 switch (format) { 405 FORMAT_REPLACE(R32_USCALED, R32_FLOAT); 406 FORMAT_REPLACE(R32G32_USCALED, R32G32_FLOAT); 407 FORMAT_REPLACE(R32G32B32_USCALED, R32G32B32_FLOAT); 408 FORMAT_REPLACE(R32G32B32A32_USCALED,R32G32B32A32_FLOAT); 409 FORMAT_REPLACE(R32_SSCALED, R32_FLOAT); 410 FORMAT_REPLACE(R32G32_SSCALED, R32G32_FLOAT); 411 FORMAT_REPLACE(R32G32B32_SSCALED, R32G32B32_FLOAT); 412 FORMAT_REPLACE(R32G32B32A32_SSCALED,R32G32B32A32_FLOAT); 413 default:; 414 } 415 } 416 417 native_attribs[i].src_format = format; 418 ve->native_format[i] = format; 419 ve->native_format_size[i] = 420 util_format_get_blocksize(ve->native_format[i]); 421 422 ve->incompatible_layout_elem[i] = 423 ve->ve[i].src_format != ve->native_format[i] || 424 (!mgr->b.caps.fetch_dword_unaligned && ve->ve[i].src_offset % 4 != 0); 425 ve->incompatible_layout = 426 ve->incompatible_layout || 427 ve->incompatible_layout_elem[i]; 428 } 429 430 /* Align the formats to the size of DWORD if needed. */ 431 if (!mgr->b.caps.fetch_dword_unaligned) { 432 for (i = 0; i < count; i++) { 433 ve->native_format_size[i] = align(ve->native_format_size[i], 4); 434 } 435 } 436 437 return ve; 438} 439 440void u_vbuf_bind_vertex_elements(struct u_vbuf *mgrb, 441 void *cso, 442 struct u_vbuf_elements *ve) 443{ 444 struct u_vbuf_priv *mgr = (struct u_vbuf_priv*)mgrb; 445 446 if (!cso) { 447 return; 448 } 449 450 if (!mgr->ve_binding_lock) { 451 mgr->saved_ve = cso; 452 mgr->ve = ve; 453 } 454} 455 456void u_vbuf_destroy_vertex_elements(struct u_vbuf *mgr, 457 struct u_vbuf_elements *ve) 458{ 459 FREE(ve); 460} 461 462void u_vbuf_set_vertex_buffers(struct u_vbuf *mgrb, 463 unsigned count, 464 const struct pipe_vertex_buffer *bufs) 465{ 466 struct u_vbuf_priv *mgr = (struct u_vbuf_priv*)mgrb; 467 unsigned i; 468 469 mgr->any_user_vbs = FALSE; 470 mgr->incompatible_vb_layout = FALSE; 471 memset(mgr->incompatible_vb, 0, sizeof(mgr->incompatible_vb)); 472 473 if (!mgr->b.caps.fetch_dword_unaligned) { 474 /* Check if the strides and offsets are aligned to the size of DWORD. */ 475 for (i = 0; i < count; i++) { 476 if (bufs[i].buffer) { 477 if (bufs[i].stride % 4 != 0 || 478 bufs[i].buffer_offset % 4 != 0) { 479 mgr->incompatible_vb_layout = TRUE; 480 mgr->incompatible_vb[i] = TRUE; 481 } 482 } 483 } 484 } 485 486 for (i = 0; i < count; i++) { 487 const struct pipe_vertex_buffer *vb = &bufs[i]; 488 489 pipe_resource_reference(&mgr->b.vertex_buffer[i].buffer, vb->buffer); 490 491 mgr->b.real_vertex_buffer[i].buffer_offset = 492 mgr->b.vertex_buffer[i].buffer_offset = vb->buffer_offset; 493 494 mgr->b.real_vertex_buffer[i].stride = 495 mgr->b.vertex_buffer[i].stride = vb->stride; 496 497 if (!vb->buffer || 498 mgr->incompatible_vb[i]) { 499 pipe_resource_reference(&mgr->b.real_vertex_buffer[i].buffer, NULL); 500 continue; 501 } 502 503 if (u_vbuf_resource(vb->buffer)->user_ptr) { 504 pipe_resource_reference(&mgr->b.real_vertex_buffer[i].buffer, NULL); 505 mgr->any_user_vbs = TRUE; 506 continue; 507 } 508 509 pipe_resource_reference(&mgr->b.real_vertex_buffer[i].buffer, vb->buffer); 510 } 511 512 for (i = count; i < mgr->b.nr_vertex_buffers; i++) { 513 pipe_resource_reference(&mgr->b.vertex_buffer[i].buffer, NULL); 514 } 515 for (i = count; i < mgr->b.nr_real_vertex_buffers; i++) { 516 pipe_resource_reference(&mgr->b.real_vertex_buffer[i].buffer, NULL); 517 } 518 519 mgr->b.nr_vertex_buffers = count; 520 mgr->b.nr_real_vertex_buffers = count; 521} 522 523void u_vbuf_set_index_buffer(struct u_vbuf *mgr, 524 const struct pipe_index_buffer *ib) 525{ 526 if (ib && ib->buffer) { 527 assert(ib->offset % ib->index_size == 0); 528 pipe_resource_reference(&mgr->index_buffer.buffer, ib->buffer); 529 mgr->index_buffer.offset = ib->offset; 530 mgr->index_buffer.index_size = ib->index_size; 531 } else { 532 pipe_resource_reference(&mgr->index_buffer.buffer, NULL); 533 } 534} 535 536static void 537u_vbuf_upload_buffers(struct u_vbuf_priv *mgr, 538 int min_index, int max_index, 539 unsigned instance_count) 540{ 541 unsigned i; 542 unsigned count = max_index + 1 - min_index; 543 unsigned nr_velems = mgr->ve->count; 544 unsigned nr_vbufs = mgr->b.nr_vertex_buffers; 545 struct pipe_vertex_element *velems = 546 mgr->fallback_ve ? mgr->fallback_velems : mgr->ve->ve; 547 unsigned start_offset[PIPE_MAX_ATTRIBS]; 548 unsigned end_offset[PIPE_MAX_ATTRIBS] = {0}; 549 550 /* Determine how much data needs to be uploaded. */ 551 for (i = 0; i < nr_velems; i++) { 552 struct pipe_vertex_element *velem = &velems[i]; 553 unsigned index = velem->vertex_buffer_index; 554 struct pipe_vertex_buffer *vb = &mgr->b.vertex_buffer[index]; 555 unsigned instance_div, first, size; 556 557 /* Skip the buffer generated by translate. */ 558 if (index == mgr->fallback_vb_slot) { 559 continue; 560 } 561 562 assert(vb->buffer); 563 564 if (!u_vbuf_resource(vb->buffer)->user_ptr) { 565 continue; 566 } 567 568 instance_div = velem->instance_divisor; 569 first = vb->buffer_offset + velem->src_offset; 570 571 if (!vb->stride) { 572 /* Constant attrib. */ 573 size = mgr->ve->src_format_size[i]; 574 } else if (instance_div) { 575 /* Per-instance attrib. */ 576 unsigned count = (instance_count + instance_div - 1) / instance_div; 577 size = vb->stride * (count - 1) + mgr->ve->src_format_size[i]; 578 } else { 579 /* Per-vertex attrib. */ 580 first += vb->stride * min_index; 581 size = vb->stride * (count - 1) + mgr->ve->src_format_size[i]; 582 } 583 584 /* Update offsets. */ 585 if (!end_offset[index]) { 586 start_offset[index] = first; 587 end_offset[index] = first + size; 588 } else { 589 if (first < start_offset[index]) 590 start_offset[index] = first; 591 if (first + size > end_offset[index]) 592 end_offset[index] = first + size; 593 } 594 } 595 596 /* Upload buffers. */ 597 for (i = 0; i < nr_vbufs; i++) { 598 unsigned start, end = end_offset[i]; 599 boolean flushed; 600 struct pipe_vertex_buffer *real_vb; 601 uint8_t *ptr; 602 603 if (!end) { 604 continue; 605 } 606 607 start = start_offset[i]; 608 assert(start < end); 609 610 real_vb = &mgr->b.real_vertex_buffer[i]; 611 ptr = u_vbuf_resource(mgr->b.vertex_buffer[i].buffer)->user_ptr; 612 613 u_upload_data(mgr->b.uploader, start, end - start, ptr + start, 614 &real_vb->buffer_offset, &real_vb->buffer, &flushed); 615 616 real_vb->buffer_offset -= start; 617 } 618} 619 620unsigned u_vbuf_draw_max_vertex_count(struct u_vbuf *mgrb) 621{ 622 struct u_vbuf_priv *mgr = (struct u_vbuf_priv*)mgrb; 623 unsigned i, nr = mgr->ve->count; 624 struct pipe_vertex_element *velems = 625 mgr->fallback_ve ? mgr->fallback_velems : mgr->ve->ve; 626 unsigned result = ~0; 627 628 for (i = 0; i < nr; i++) { 629 struct pipe_vertex_buffer *vb = 630 &mgr->b.real_vertex_buffer[velems[i].vertex_buffer_index]; 631 unsigned size, max_count, value; 632 633 /* We're not interested in constant and per-instance attribs. */ 634 if (!vb->buffer || 635 !vb->stride || 636 velems[i].instance_divisor) { 637 continue; 638 } 639 640 size = vb->buffer->width0; 641 642 /* Subtract buffer_offset. */ 643 value = vb->buffer_offset; 644 if (value >= size) { 645 return 0; 646 } 647 size -= value; 648 649 /* Subtract src_offset. */ 650 value = velems[i].src_offset; 651 if (value >= size) { 652 return 0; 653 } 654 size -= value; 655 656 /* Subtract format_size. */ 657 value = mgr->ve->native_format_size[i]; 658 if (value >= size) { 659 return 0; 660 } 661 size -= value; 662 663 /* Compute the max count. */ 664 max_count = 1 + size / vb->stride; 665 result = MIN2(result, max_count); 666 } 667 return result; 668} 669 670static boolean u_vbuf_need_minmax_index(struct u_vbuf_priv *mgr) 671{ 672 unsigned i, nr = mgr->ve->count; 673 674 for (i = 0; i < nr; i++) { 675 struct pipe_vertex_buffer *vb; 676 unsigned index; 677 678 /* Per-instance attribs don't need min/max_index. */ 679 if (mgr->ve->ve[i].instance_divisor) { 680 continue; 681 } 682 683 index = mgr->ve->ve[i].vertex_buffer_index; 684 vb = &mgr->b.vertex_buffer[index]; 685 686 /* Constant attribs don't need min/max_index. */ 687 if (!vb->stride) { 688 continue; 689 } 690 691 /* Per-vertex attribs need min/max_index. */ 692 if (u_vbuf_resource(vb->buffer)->user_ptr || 693 mgr->ve->incompatible_layout_elem[i] || 694 mgr->incompatible_vb[index]) { 695 return TRUE; 696 } 697 } 698 699 return FALSE; 700} 701 702static void u_vbuf_get_minmax_index(struct pipe_context *pipe, 703 struct pipe_index_buffer *ib, 704 const struct pipe_draw_info *info, 705 int *out_min_index, 706 int *out_max_index) 707{ 708 struct pipe_transfer *transfer = NULL; 709 const void *indices; 710 unsigned i; 711 unsigned restart_index = info->restart_index; 712 713 if (u_vbuf_resource(ib->buffer)->user_ptr) { 714 indices = u_vbuf_resource(ib->buffer)->user_ptr + 715 ib->offset + info->start * ib->index_size; 716 } else { 717 indices = pipe_buffer_map_range(pipe, ib->buffer, 718 ib->offset + info->start * ib->index_size, 719 info->count * ib->index_size, 720 PIPE_TRANSFER_READ, &transfer); 721 } 722 723 switch (ib->index_size) { 724 case 4: { 725 const unsigned *ui_indices = (const unsigned*)indices; 726 unsigned max_ui = 0; 727 unsigned min_ui = ~0U; 728 if (info->primitive_restart) { 729 for (i = 0; i < info->count; i++) { 730 if (ui_indices[i] != restart_index) { 731 if (ui_indices[i] > max_ui) max_ui = ui_indices[i]; 732 if (ui_indices[i] < min_ui) min_ui = ui_indices[i]; 733 } 734 } 735 } 736 else { 737 for (i = 0; i < info->count; i++) { 738 if (ui_indices[i] > max_ui) max_ui = ui_indices[i]; 739 if (ui_indices[i] < min_ui) min_ui = ui_indices[i]; 740 } 741 } 742 *out_min_index = min_ui; 743 *out_max_index = max_ui; 744 break; 745 } 746 case 2: { 747 const unsigned short *us_indices = (const unsigned short*)indices; 748 unsigned max_us = 0; 749 unsigned min_us = ~0U; 750 if (info->primitive_restart) { 751 for (i = 0; i < info->count; i++) { 752 if (us_indices[i] != restart_index) { 753 if (us_indices[i] > max_us) max_us = us_indices[i]; 754 if (us_indices[i] < min_us) min_us = us_indices[i]; 755 } 756 } 757 } 758 else { 759 for (i = 0; i < info->count; i++) { 760 if (us_indices[i] > max_us) max_us = us_indices[i]; 761 if (us_indices[i] < min_us) min_us = us_indices[i]; 762 } 763 } 764 *out_min_index = min_us; 765 *out_max_index = max_us; 766 break; 767 } 768 case 1: { 769 const unsigned char *ub_indices = (const unsigned char*)indices; 770 unsigned max_ub = 0; 771 unsigned min_ub = ~0U; 772 if (info->primitive_restart) { 773 for (i = 0; i < info->count; i++) { 774 if (ub_indices[i] != restart_index) { 775 if (ub_indices[i] > max_ub) max_ub = ub_indices[i]; 776 if (ub_indices[i] < min_ub) min_ub = ub_indices[i]; 777 } 778 } 779 } 780 else { 781 for (i = 0; i < info->count; i++) { 782 if (ub_indices[i] > max_ub) max_ub = ub_indices[i]; 783 if (ub_indices[i] < min_ub) min_ub = ub_indices[i]; 784 } 785 } 786 *out_min_index = min_ub; 787 *out_max_index = max_ub; 788 break; 789 } 790 default: 791 assert(0); 792 *out_min_index = 0; 793 *out_max_index = 0; 794 } 795 796 if (transfer) { 797 pipe_buffer_unmap(pipe, transfer); 798 } 799} 800 801enum u_vbuf_return_flags 802u_vbuf_draw_begin(struct u_vbuf *mgrb, 803 const struct pipe_draw_info *info) 804{ 805 struct u_vbuf_priv *mgr = (struct u_vbuf_priv*)mgrb; 806 int min_index, max_index; 807 808 if (!mgr->incompatible_vb_layout && 809 !mgr->ve->incompatible_layout && 810 !mgr->any_user_vbs) { 811 return 0; 812 } 813 814 if (info->indexed) { 815 if (info->max_index != ~0) { 816 min_index = info->min_index + info->index_bias; 817 max_index = info->max_index + info->index_bias; 818 } else if (u_vbuf_need_minmax_index(mgr)) { 819 u_vbuf_get_minmax_index(mgr->pipe, &mgr->b.index_buffer, info, 820 &min_index, &max_index); 821 min_index += info->index_bias; 822 max_index += info->index_bias; 823 } else { 824 min_index = 0; 825 max_index = 0; 826 } 827 } else { 828 min_index = info->start; 829 max_index = info->start + info->count - 1; 830 } 831 832 /* Translate vertices with non-native layouts or formats. */ 833 if (mgr->incompatible_vb_layout || mgr->ve->incompatible_layout) { 834 u_vbuf_translate_begin(mgr, min_index, max_index); 835 } 836 837 /* Upload user buffers. */ 838 if (mgr->any_user_vbs) { 839 u_vbuf_upload_buffers(mgr, min_index, max_index, info->instance_count); 840 } 841 return U_VBUF_BUFFERS_UPDATED; 842} 843 844void u_vbuf_draw_end(struct u_vbuf *mgrb) 845{ 846 struct u_vbuf_priv *mgr = (struct u_vbuf_priv*)mgrb; 847 848 if (mgr->fallback_ve) { 849 u_vbuf_translate_end(mgr); 850 } 851} 852