st_draw.c revision 14bb957b996dcc5392b8fa589bd3ffa5c55cb6b4
1/************************************************************************** 2 * 3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas. 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 TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR 22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 * 26 **************************************************************************/ 27 28/* 29 * This file implements the st_draw_vbo() function which is called from 30 * Mesa's VBO module. All point/line/triangle rendering is done through 31 * this function whether the user called glBegin/End, glDrawArrays, 32 * glDrawElements, glEvalMesh, or glCalList, etc. 33 * 34 * We basically convert the VBO's vertex attribute/array information into 35 * Gallium vertex state, bind the vertex buffer objects and call 36 * pipe->draw_vbo(). 37 * 38 * Authors: 39 * Keith Whitwell <keith@tungstengraphics.com> 40 */ 41 42 43#include "main/imports.h" 44#include "main/image.h" 45#include "main/bufferobj.h" 46#include "main/macros.h" 47#include "main/mfeatures.h" 48 49#include "vbo/vbo.h" 50 51#include "st_context.h" 52#include "st_atom.h" 53#include "st_cb_bufferobjects.h" 54#include "st_draw.h" 55#include "st_program.h" 56 57#include "pipe/p_context.h" 58#include "pipe/p_defines.h" 59#include "util/u_inlines.h" 60#include "util/u_format.h" 61#include "util/u_prim.h" 62#include "util/u_draw_quad.h" 63#include "draw/draw_context.h" 64#include "cso_cache/cso_context.h" 65 66#include "../glsl/ir_uniform.h" 67 68 69static GLuint double_types[4] = { 70 PIPE_FORMAT_R64_FLOAT, 71 PIPE_FORMAT_R64G64_FLOAT, 72 PIPE_FORMAT_R64G64B64_FLOAT, 73 PIPE_FORMAT_R64G64B64A64_FLOAT 74}; 75 76static GLuint float_types[4] = { 77 PIPE_FORMAT_R32_FLOAT, 78 PIPE_FORMAT_R32G32_FLOAT, 79 PIPE_FORMAT_R32G32B32_FLOAT, 80 PIPE_FORMAT_R32G32B32A32_FLOAT 81}; 82 83static GLuint half_float_types[4] = { 84 PIPE_FORMAT_R16_FLOAT, 85 PIPE_FORMAT_R16G16_FLOAT, 86 PIPE_FORMAT_R16G16B16_FLOAT, 87 PIPE_FORMAT_R16G16B16A16_FLOAT 88}; 89 90static GLuint uint_types_norm[4] = { 91 PIPE_FORMAT_R32_UNORM, 92 PIPE_FORMAT_R32G32_UNORM, 93 PIPE_FORMAT_R32G32B32_UNORM, 94 PIPE_FORMAT_R32G32B32A32_UNORM 95}; 96 97static GLuint uint_types_scale[4] = { 98 PIPE_FORMAT_R32_USCALED, 99 PIPE_FORMAT_R32G32_USCALED, 100 PIPE_FORMAT_R32G32B32_USCALED, 101 PIPE_FORMAT_R32G32B32A32_USCALED 102}; 103 104static GLuint int_types_norm[4] = { 105 PIPE_FORMAT_R32_SNORM, 106 PIPE_FORMAT_R32G32_SNORM, 107 PIPE_FORMAT_R32G32B32_SNORM, 108 PIPE_FORMAT_R32G32B32A32_SNORM 109}; 110 111static GLuint int_types_scale[4] = { 112 PIPE_FORMAT_R32_SSCALED, 113 PIPE_FORMAT_R32G32_SSCALED, 114 PIPE_FORMAT_R32G32B32_SSCALED, 115 PIPE_FORMAT_R32G32B32A32_SSCALED 116}; 117 118static GLuint ushort_types_norm[4] = { 119 PIPE_FORMAT_R16_UNORM, 120 PIPE_FORMAT_R16G16_UNORM, 121 PIPE_FORMAT_R16G16B16_UNORM, 122 PIPE_FORMAT_R16G16B16A16_UNORM 123}; 124 125static GLuint ushort_types_scale[4] = { 126 PIPE_FORMAT_R16_USCALED, 127 PIPE_FORMAT_R16G16_USCALED, 128 PIPE_FORMAT_R16G16B16_USCALED, 129 PIPE_FORMAT_R16G16B16A16_USCALED 130}; 131 132static GLuint short_types_norm[4] = { 133 PIPE_FORMAT_R16_SNORM, 134 PIPE_FORMAT_R16G16_SNORM, 135 PIPE_FORMAT_R16G16B16_SNORM, 136 PIPE_FORMAT_R16G16B16A16_SNORM 137}; 138 139static GLuint short_types_scale[4] = { 140 PIPE_FORMAT_R16_SSCALED, 141 PIPE_FORMAT_R16G16_SSCALED, 142 PIPE_FORMAT_R16G16B16_SSCALED, 143 PIPE_FORMAT_R16G16B16A16_SSCALED 144}; 145 146static GLuint ubyte_types_norm[4] = { 147 PIPE_FORMAT_R8_UNORM, 148 PIPE_FORMAT_R8G8_UNORM, 149 PIPE_FORMAT_R8G8B8_UNORM, 150 PIPE_FORMAT_R8G8B8A8_UNORM 151}; 152 153static GLuint ubyte_types_scale[4] = { 154 PIPE_FORMAT_R8_USCALED, 155 PIPE_FORMAT_R8G8_USCALED, 156 PIPE_FORMAT_R8G8B8_USCALED, 157 PIPE_FORMAT_R8G8B8A8_USCALED 158}; 159 160static GLuint byte_types_norm[4] = { 161 PIPE_FORMAT_R8_SNORM, 162 PIPE_FORMAT_R8G8_SNORM, 163 PIPE_FORMAT_R8G8B8_SNORM, 164 PIPE_FORMAT_R8G8B8A8_SNORM 165}; 166 167static GLuint byte_types_scale[4] = { 168 PIPE_FORMAT_R8_SSCALED, 169 PIPE_FORMAT_R8G8_SSCALED, 170 PIPE_FORMAT_R8G8B8_SSCALED, 171 PIPE_FORMAT_R8G8B8A8_SSCALED 172}; 173 174static GLuint fixed_types[4] = { 175 PIPE_FORMAT_R32_FIXED, 176 PIPE_FORMAT_R32G32_FIXED, 177 PIPE_FORMAT_R32G32B32_FIXED, 178 PIPE_FORMAT_R32G32B32A32_FIXED 179}; 180 181 182 183/** 184 * Return a PIPE_FORMAT_x for the given GL datatype and size. 185 */ 186enum pipe_format 187st_pipe_vertex_format(GLenum type, GLuint size, GLenum format, 188 GLboolean normalized) 189{ 190 assert((type >= GL_BYTE && type <= GL_DOUBLE) || 191 type == GL_FIXED || type == GL_HALF_FLOAT || 192 type == GL_INT_2_10_10_10_REV || 193 type == GL_UNSIGNED_INT_2_10_10_10_REV); 194 assert(size >= 1); 195 assert(size <= 4); 196 assert(format == GL_RGBA || format == GL_BGRA); 197 198 if (type == GL_INT_2_10_10_10_REV || 199 type == GL_UNSIGNED_INT_2_10_10_10_REV) { 200 assert(size == 4); 201 202 if (format == GL_BGRA) { 203 if (type == GL_INT_2_10_10_10_REV) { 204 if (normalized) 205 return PIPE_FORMAT_B10G10R10A2_SNORM; 206 else 207 return PIPE_FORMAT_B10G10R10A2_SSCALED; 208 } else { 209 if (normalized) 210 return PIPE_FORMAT_B10G10R10A2_UNORM; 211 else 212 return PIPE_FORMAT_B10G10R10A2_USCALED; 213 } 214 } else { 215 if (type == GL_INT_2_10_10_10_REV) { 216 if (normalized) 217 return PIPE_FORMAT_R10G10B10A2_SNORM; 218 else 219 return PIPE_FORMAT_R10G10B10A2_SSCALED; 220 } else { 221 if (normalized) 222 return PIPE_FORMAT_R10G10B10A2_UNORM; 223 else 224 return PIPE_FORMAT_R10G10B10A2_USCALED; 225 } 226 } 227 } 228 229 if (format == GL_BGRA) { 230 /* this is an odd-ball case */ 231 assert(type == GL_UNSIGNED_BYTE); 232 assert(normalized); 233 return PIPE_FORMAT_B8G8R8A8_UNORM; 234 } 235 236 if (normalized) { 237 switch (type) { 238 case GL_DOUBLE: return double_types[size-1]; 239 case GL_FLOAT: return float_types[size-1]; 240 case GL_HALF_FLOAT: return half_float_types[size-1]; 241 case GL_INT: return int_types_norm[size-1]; 242 case GL_SHORT: return short_types_norm[size-1]; 243 case GL_BYTE: return byte_types_norm[size-1]; 244 case GL_UNSIGNED_INT: return uint_types_norm[size-1]; 245 case GL_UNSIGNED_SHORT: return ushort_types_norm[size-1]; 246 case GL_UNSIGNED_BYTE: return ubyte_types_norm[size-1]; 247 case GL_FIXED: return fixed_types[size-1]; 248 default: assert(0); return 0; 249 } 250 } 251 else { 252 switch (type) { 253 case GL_DOUBLE: return double_types[size-1]; 254 case GL_FLOAT: return float_types[size-1]; 255 case GL_HALF_FLOAT: return half_float_types[size-1]; 256 case GL_INT: return int_types_scale[size-1]; 257 case GL_SHORT: return short_types_scale[size-1]; 258 case GL_BYTE: return byte_types_scale[size-1]; 259 case GL_UNSIGNED_INT: return uint_types_scale[size-1]; 260 case GL_UNSIGNED_SHORT: return ushort_types_scale[size-1]; 261 case GL_UNSIGNED_BYTE: return ubyte_types_scale[size-1]; 262 case GL_FIXED: return fixed_types[size-1]; 263 default: assert(0); return 0; 264 } 265 } 266 return PIPE_FORMAT_NONE; /* silence compiler warning */ 267} 268 269 270/** 271 * This is very similar to vbo_all_varyings_in_vbos() but we are 272 * only interested in per-vertex data. See bug 38626. 273 */ 274static GLboolean 275all_varyings_in_vbos(const struct gl_client_array *arrays[]) 276{ 277 GLuint i; 278 279 for (i = 0; i < VERT_ATTRIB_MAX; i++) 280 if (arrays[i]->StrideB && 281 !arrays[i]->InstanceDivisor && 282 !_mesa_is_bufferobj(arrays[i]->BufferObj)) 283 return GL_FALSE; 284 285 return GL_TRUE; 286} 287 288 289/** 290 * Examine the active arrays to determine if we have interleaved 291 * vertex arrays all living in one VBO, or all living in user space. 292 */ 293static GLboolean 294is_interleaved_arrays(const struct st_vertex_program *vp, 295 const struct st_vp_variant *vpv, 296 const struct gl_client_array **arrays) 297{ 298 GLuint attr; 299 const struct gl_buffer_object *firstBufObj = NULL; 300 GLint firstStride = -1; 301 const GLubyte *firstPtr = NULL; 302 GLboolean userSpaceBuffer = GL_FALSE; 303 304 for (attr = 0; attr < vpv->num_inputs; attr++) { 305 const GLuint mesaAttr = vp->index_to_input[attr]; 306 const struct gl_client_array *array = arrays[mesaAttr]; 307 const struct gl_buffer_object *bufObj = array->BufferObj; 308 const GLsizei stride = array->StrideB; /* in bytes */ 309 310 if (attr == 0) { 311 /* save info about the first array */ 312 firstStride = stride; 313 firstPtr = array->Ptr; 314 firstBufObj = bufObj; 315 userSpaceBuffer = !bufObj || !bufObj->Name; 316 } 317 else { 318 /* check if other arrays interleave with the first, in same buffer */ 319 if (stride != firstStride) 320 return GL_FALSE; /* strides don't match */ 321 322 if (bufObj != firstBufObj) 323 return GL_FALSE; /* arrays in different VBOs */ 324 325 if (abs(array->Ptr - firstPtr) > firstStride) 326 return GL_FALSE; /* arrays start too far apart */ 327 328 if ((!_mesa_is_bufferobj(bufObj)) != userSpaceBuffer) 329 return GL_FALSE; /* mix of VBO and user-space arrays */ 330 } 331 } 332 333 return GL_TRUE; 334} 335 336 337/** 338 * Set up for drawing interleaved arrays that all live in one VBO 339 * or all live in user space. 340 * \param vbuffer returns vertex buffer info 341 * \param velements returns vertex element info 342 * \return GL_TRUE for success, GL_FALSE otherwise (probably out of memory) 343 */ 344static GLboolean 345setup_interleaved_attribs(struct gl_context *ctx, 346 const struct st_vertex_program *vp, 347 const struct st_vp_variant *vpv, 348 const struct gl_client_array **arrays, 349 struct pipe_vertex_buffer *vbuffer, 350 struct pipe_vertex_element velements[], 351 unsigned max_index, 352 unsigned num_instances) 353{ 354 struct st_context *st = st_context(ctx); 355 struct pipe_context *pipe = st->pipe; 356 GLuint attr; 357 const GLubyte *low_addr = NULL; 358 GLboolean usingVBO; /* all arrays in a VBO? */ 359 struct gl_buffer_object *bufobj; 360 GLuint user_buffer_size = 0; 361 GLuint vertex_size = 0; /* bytes per vertex, in bytes */ 362 GLsizei stride; 363 364 /* Find the lowest address of the arrays we're drawing, 365 * Init bufobj and stride. 366 */ 367 if (vpv->num_inputs) { 368 const GLuint mesaAttr0 = vp->index_to_input[0]; 369 const struct gl_client_array *array = arrays[mesaAttr0]; 370 371 /* Since we're doing interleaved arrays, we know there'll be at most 372 * one buffer object and the stride will be the same for all arrays. 373 * Grab them now. 374 */ 375 bufobj = array->BufferObj; 376 stride = array->StrideB; 377 378 low_addr = arrays[vp->index_to_input[0]]->Ptr; 379 380 for (attr = 1; attr < vpv->num_inputs; attr++) { 381 const GLubyte *start = arrays[vp->index_to_input[attr]]->Ptr; 382 low_addr = MIN2(low_addr, start); 383 } 384 } 385 else { 386 /* not sure we'll ever have zero inputs, but play it safe */ 387 bufobj = NULL; 388 stride = 0; 389 low_addr = 0; 390 } 391 392 /* are the arrays in user space? */ 393 usingVBO = _mesa_is_bufferobj(bufobj); 394 395 for (attr = 0; attr < vpv->num_inputs; attr++) { 396 const GLuint mesaAttr = vp->index_to_input[attr]; 397 const struct gl_client_array *array = arrays[mesaAttr]; 398 unsigned src_offset = (unsigned) (array->Ptr - low_addr); 399 GLuint element_size = array->_ElementSize; 400 401 assert(element_size == array->Size * _mesa_sizeof_type(array->Type)); 402 403 velements[attr].src_offset = src_offset; 404 velements[attr].instance_divisor = array->InstanceDivisor; 405 velements[attr].vertex_buffer_index = 0; 406 velements[attr].src_format = st_pipe_vertex_format(array->Type, 407 array->Size, 408 array->Format, 409 array->Normalized); 410 assert(velements[attr].src_format); 411 412 if (!usingVBO) { 413 /* how many bytes referenced by this attribute array? */ 414 uint divisor = array->InstanceDivisor; 415 uint last_index = divisor ? num_instances / divisor : max_index; 416 uint bytes = src_offset + stride * last_index + element_size; 417 418 user_buffer_size = MAX2(user_buffer_size, bytes); 419 420 /* update vertex size */ 421 vertex_size = MAX2(vertex_size, src_offset + element_size); 422 } 423 } 424 425 /* 426 * Return the vbuffer info and setup user-space attrib info, if needed. 427 */ 428 if (vpv->num_inputs == 0) { 429 /* just defensive coding here */ 430 vbuffer->buffer = NULL; 431 vbuffer->buffer_offset = 0; 432 vbuffer->stride = 0; 433 st->num_user_attribs = 0; 434 } 435 else if (usingVBO) { 436 /* all interleaved arrays in a VBO */ 437 struct st_buffer_object *stobj = st_buffer_object(bufobj); 438 439 if (!stobj || !stobj->buffer) { 440 /* probably out of memory (or zero-sized buffer) */ 441 return GL_FALSE; 442 } 443 444 vbuffer->buffer = NULL; 445 pipe_resource_reference(&vbuffer->buffer, stobj->buffer); 446 vbuffer->buffer_offset = pointer_to_offset(low_addr); 447 vbuffer->stride = stride; 448 st->num_user_attribs = 0; 449 } 450 else { 451 /* all interleaved arrays in user memory */ 452 vbuffer->buffer = pipe_user_buffer_create(pipe->screen, 453 (void*) low_addr, 454 user_buffer_size, 455 PIPE_BIND_VERTEX_BUFFER); 456 vbuffer->buffer_offset = 0; 457 vbuffer->stride = stride; 458 459 /* Track user vertex buffers. */ 460 pipe_resource_reference(&st->user_attrib[0].buffer, vbuffer->buffer); 461 st->user_attrib[0].element_size = vertex_size; 462 st->user_attrib[0].stride = stride; 463 st->num_user_attribs = 1; 464 } 465 466 return GL_TRUE; 467} 468 469 470/** 471 * Set up a separate pipe_vertex_buffer and pipe_vertex_element for each 472 * vertex attribute. 473 * \param vbuffer returns vertex buffer info 474 * \param velements returns vertex element info 475 * \return GL_TRUE for success, GL_FALSE otherwise (probably out of memory) 476 */ 477static GLboolean 478setup_non_interleaved_attribs(struct gl_context *ctx, 479 const struct st_vertex_program *vp, 480 const struct st_vp_variant *vpv, 481 const struct gl_client_array **arrays, 482 struct pipe_vertex_buffer vbuffer[], 483 struct pipe_vertex_element velements[], 484 unsigned max_index, 485 unsigned num_instances) 486{ 487 struct st_context *st = st_context(ctx); 488 struct pipe_context *pipe = st->pipe; 489 GLuint attr; 490 491 for (attr = 0; attr < vpv->num_inputs; attr++) { 492 const GLuint mesaAttr = vp->index_to_input[attr]; 493 const struct gl_client_array *array = arrays[mesaAttr]; 494 struct gl_buffer_object *bufobj = array->BufferObj; 495 GLuint element_size = array->_ElementSize; 496 GLsizei stride = array->StrideB; 497 498 assert(element_size == array->Size * _mesa_sizeof_type(array->Type)); 499 500 if (_mesa_is_bufferobj(bufobj)) { 501 /* Attribute data is in a VBO. 502 * Recall that for VBOs, the gl_client_array->Ptr field is 503 * really an offset from the start of the VBO, not a pointer. 504 */ 505 struct st_buffer_object *stobj = st_buffer_object(bufobj); 506 507 if (!stobj || !stobj->buffer) { 508 /* probably out of memory (or zero-sized buffer) */ 509 return GL_FALSE; 510 } 511 512 vbuffer[attr].buffer = NULL; 513 pipe_resource_reference(&vbuffer[attr].buffer, stobj->buffer); 514 vbuffer[attr].buffer_offset = pointer_to_offset(array->Ptr); 515 } 516 else { 517 /* wrap user data */ 518 uint bytes; 519 void *ptr; 520 521 if (array->Ptr) { 522 uint divisor = array->InstanceDivisor; 523 uint last_index = divisor ? num_instances / divisor : max_index; 524 525 bytes = stride * last_index + element_size; 526 527 ptr = (void *) array->Ptr; 528 } 529 else { 530 /* no array, use ctx->Current.Attrib[] value */ 531 bytes = element_size = sizeof(ctx->Current.Attrib[0]); 532 ptr = (void *) ctx->Current.Attrib[mesaAttr]; 533 stride = 0; 534 } 535 536 assert(ptr); 537 assert(bytes); 538 539 vbuffer[attr].buffer = 540 pipe_user_buffer_create(pipe->screen, ptr, bytes, 541 PIPE_BIND_VERTEX_BUFFER); 542 543 vbuffer[attr].buffer_offset = 0; 544 545 /* Track user vertex buffers. */ 546 pipe_resource_reference(&st->user_attrib[attr].buffer, vbuffer[attr].buffer); 547 st->user_attrib[attr].element_size = element_size; 548 st->user_attrib[attr].stride = stride; 549 st->num_user_attribs = MAX2(st->num_user_attribs, attr + 1); 550 551 if (!vbuffer[attr].buffer) { 552 /* probably ran out of memory */ 553 return GL_FALSE; 554 } 555 } 556 557 /* common-case setup */ 558 vbuffer[attr].stride = stride; /* in bytes */ 559 560 velements[attr].src_offset = 0; 561 velements[attr].instance_divisor = array->InstanceDivisor; 562 velements[attr].vertex_buffer_index = attr; 563 velements[attr].src_format = st_pipe_vertex_format(array->Type, 564 array->Size, 565 array->Format, 566 array->Normalized); 567 assert(velements[attr].src_format); 568 } 569 570 return GL_TRUE; 571} 572 573 574static void 575setup_index_buffer(struct gl_context *ctx, 576 const struct _mesa_index_buffer *ib, 577 struct pipe_index_buffer *ibuffer) 578{ 579 struct st_context *st = st_context(ctx); 580 struct pipe_context *pipe = st->pipe; 581 582 memset(ibuffer, 0, sizeof(*ibuffer)); 583 if (ib) { 584 struct gl_buffer_object *bufobj = ib->obj; 585 586 switch (ib->type) { 587 case GL_UNSIGNED_INT: 588 ibuffer->index_size = 4; 589 break; 590 case GL_UNSIGNED_SHORT: 591 ibuffer->index_size = 2; 592 break; 593 case GL_UNSIGNED_BYTE: 594 ibuffer->index_size = 1; 595 break; 596 default: 597 assert(0); 598 return; 599 } 600 601 /* get/create the index buffer object */ 602 if (_mesa_is_bufferobj(bufobj)) { 603 /* elements/indexes are in a real VBO */ 604 struct st_buffer_object *stobj = st_buffer_object(bufobj); 605 pipe_resource_reference(&ibuffer->buffer, stobj->buffer); 606 ibuffer->offset = pointer_to_offset(ib->ptr); 607 } 608 else { 609 /* element/indicies are in user space memory */ 610 ibuffer->buffer = 611 pipe_user_buffer_create(pipe->screen, (void *) ib->ptr, 612 ib->count * ibuffer->index_size, 613 PIPE_BIND_INDEX_BUFFER); 614 } 615 } 616} 617 618 619/** 620 * Prior to drawing, check that any uniforms referenced by the 621 * current shader have been set. If a uniform has not been set, 622 * issue a warning. 623 */ 624static void 625check_uniforms(struct gl_context *ctx) 626{ 627 struct gl_shader_program *shProg[3] = { 628 ctx->Shader.CurrentVertexProgram, 629 ctx->Shader.CurrentGeometryProgram, 630 ctx->Shader.CurrentFragmentProgram, 631 }; 632 unsigned j; 633 634 for (j = 0; j < 3; j++) { 635 unsigned i; 636 637 if (shProg[j] == NULL || !shProg[j]->LinkStatus) 638 continue; 639 640 for (i = 0; i < shProg[j]->NumUserUniformStorage; i++) { 641 const struct gl_uniform_storage *u = &shProg[j]->UniformStorage[i]; 642 if (!u->initialized) { 643 _mesa_warning(ctx, 644 "Using shader with uninitialized uniform: %s", 645 u->name); 646 } 647 } 648 } 649} 650 651 652/* 653 * Notes on primitive restart: 654 * The code below is used when the gallium driver does not support primitive 655 * restart itself. We map the index buffer, find the restart indexes, unmap 656 * the index buffer then draw the sub-primitives delineated by the restarts. 657 * A couple possible optimizations: 658 * 1. Save the list of sub-primitive (start, count) values in a list attached 659 * to the index buffer for re-use in subsequent draws. The list would be 660 * invalidated when the contents of the buffer changed. 661 * 2. If drawing triangle strips or quad strips, create a new index buffer 662 * that uses duplicated vertices to render the disjoint strips as one 663 * long strip. We'd have to be careful to avoid using too much memory 664 * for this. 665 * Finally, some apps might perform better if they don't use primitive restart 666 * at all rather than this fallback path. Set MESA_EXTENSION_OVERRIDE to 667 * "-GL_NV_primitive_restart" to test that. 668 */ 669 670 671struct sub_primitive 672{ 673 unsigned start, count; 674}; 675 676 677/** 678 * Scan the elements array to find restart indexes. Return a list 679 * of primitive (start,count) pairs to indicate how to draw the sub- 680 * primitives delineated by the restart index. 681 */ 682static struct sub_primitive * 683find_sub_primitives(const void *elements, unsigned element_size, 684 unsigned start, unsigned end, unsigned restart_index, 685 unsigned *num_sub_prims) 686{ 687 const unsigned max_prims = end - start; 688 struct sub_primitive *sub_prims; 689 unsigned i, cur_start, cur_count, num; 690 691 sub_prims = (struct sub_primitive *) 692 malloc(max_prims * sizeof(struct sub_primitive)); 693 694 if (!sub_prims) { 695 *num_sub_prims = 0; 696 return NULL; 697 } 698 699 cur_start = start; 700 cur_count = 0; 701 num = 0; 702 703#define SCAN_ELEMENTS(TYPE) \ 704 for (i = start; i < end; i++) { \ 705 if (((const TYPE *) elements)[i] == restart_index) { \ 706 if (cur_count > 0) { \ 707 assert(num < max_prims); \ 708 sub_prims[num].start = cur_start; \ 709 sub_prims[num].count = cur_count; \ 710 num++; \ 711 } \ 712 cur_start = i + 1; \ 713 cur_count = 0; \ 714 } \ 715 else { \ 716 cur_count++; \ 717 } \ 718 } \ 719 if (cur_count > 0) { \ 720 assert(num < max_prims); \ 721 sub_prims[num].start = cur_start; \ 722 sub_prims[num].count = cur_count; \ 723 num++; \ 724 } 725 726 switch (element_size) { 727 case 1: 728 SCAN_ELEMENTS(ubyte); 729 break; 730 case 2: 731 SCAN_ELEMENTS(ushort); 732 break; 733 case 4: 734 SCAN_ELEMENTS(uint); 735 break; 736 default: 737 assert(0 && "bad index_size in find_sub_primitives()"); 738 } 739 740#undef SCAN_ELEMENTS 741 742 *num_sub_prims = num; 743 744 return sub_prims; 745} 746 747 748/** 749 * For gallium drivers that don't support the primitive restart 750 * feature, handle it here by breaking up the indexed primitive into 751 * sub-primitives. 752 */ 753static void 754handle_fallback_primitive_restart(struct pipe_context *pipe, 755 const struct _mesa_index_buffer *ib, 756 struct pipe_index_buffer *ibuffer, 757 struct pipe_draw_info *orig_info) 758{ 759 const unsigned start = orig_info->start; 760 const unsigned count = orig_info->count; 761 struct pipe_draw_info info = *orig_info; 762 struct pipe_transfer *transfer = NULL; 763 unsigned instance, i; 764 const void *ptr = NULL; 765 struct sub_primitive *sub_prims; 766 unsigned num_sub_prims; 767 768 assert(info.indexed); 769 assert(ibuffer->buffer); 770 assert(ib); 771 772 if (!ibuffer->buffer || !ib) 773 return; 774 775 info.primitive_restart = FALSE; 776 info.instance_count = 1; 777 778 if (_mesa_is_bufferobj(ib->obj)) { 779 ptr = pipe_buffer_map_range(pipe, ibuffer->buffer, 780 start * ibuffer->index_size, /* start */ 781 count * ibuffer->index_size, /* length */ 782 PIPE_TRANSFER_READ, &transfer); 783 } 784 else { 785 ptr = ib->ptr; 786 } 787 788 if (!ptr) 789 return; 790 791 ptr = ADD_POINTERS(ptr, ibuffer->offset); 792 793 sub_prims = find_sub_primitives(ptr, ibuffer->index_size, 794 0, count, orig_info->restart_index, 795 &num_sub_prims); 796 797 if (transfer) 798 pipe_buffer_unmap(pipe, transfer); 799 800 /* Now draw the sub primitives. 801 * Need to loop over instances as well to preserve draw order. 802 */ 803 for (instance = 0; instance < orig_info->instance_count; instance++) { 804 info.start_instance = instance + orig_info->start_instance; 805 for (i = 0; i < num_sub_prims; i++) { 806 info.start = sub_prims[i].start; 807 info.count = sub_prims[i].count; 808 if (u_trim_pipe_prim(info.mode, &info.count)) { 809 pipe->draw_vbo(pipe, &info); 810 } 811 } 812 } 813 814 if (sub_prims) 815 free(sub_prims); 816} 817 818 819/** 820 * Translate OpenGL primtive type (GL_POINTS, GL_TRIANGLE_STRIP, etc) to 821 * the corresponding Gallium type. 822 */ 823static unsigned 824translate_prim(const struct gl_context *ctx, unsigned prim) 825{ 826 /* GL prims should match Gallium prims, spot-check a few */ 827 assert(GL_POINTS == PIPE_PRIM_POINTS); 828 assert(GL_QUADS == PIPE_PRIM_QUADS); 829 assert(GL_TRIANGLE_STRIP_ADJACENCY == PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY); 830 831 /* Avoid quadstrips if it's easy to do so: 832 * Note: it's important to do the correct trimming if we change the 833 * prim type! We do that wherever this function is called. 834 */ 835 if (prim == GL_QUAD_STRIP && 836 ctx->Light.ShadeModel != GL_FLAT && 837 ctx->Polygon.FrontMode == GL_FILL && 838 ctx->Polygon.BackMode == GL_FILL) 839 prim = GL_TRIANGLE_STRIP; 840 841 return prim; 842} 843 844 845/** 846 * Setup vertex arrays and buffers prior to drawing. 847 * \return GL_TRUE for success, GL_FALSE otherwise (probably out of memory) 848 */ 849static GLboolean 850st_validate_varrays(struct gl_context *ctx, 851 const struct gl_client_array **arrays, 852 unsigned max_index, 853 unsigned num_instances) 854{ 855 struct st_context *st = st_context(ctx); 856 const struct st_vertex_program *vp; 857 const struct st_vp_variant *vpv; 858 struct pipe_vertex_buffer vbuffer[PIPE_MAX_SHADER_INPUTS]; 859 struct pipe_vertex_element velements[PIPE_MAX_ATTRIBS]; 860 unsigned num_vbuffers, num_velements; 861 GLuint attr; 862 unsigned i; 863 864 /* must get these after state validation! */ 865 vp = st->vp; 866 vpv = st->vp_variant; 867 868 memset(velements, 0, sizeof(struct pipe_vertex_element) * vpv->num_inputs); 869 870 /* Unreference any user vertex buffers. */ 871 for (i = 0; i < st->num_user_attribs; i++) { 872 pipe_resource_reference(&st->user_attrib[i].buffer, NULL); 873 } 874 st->num_user_attribs = 0; 875 876 /* 877 * Setup the vbuffer[] and velements[] arrays. 878 */ 879 if (is_interleaved_arrays(vp, vpv, arrays)) { 880 if (!setup_interleaved_attribs(ctx, vp, vpv, arrays, vbuffer, velements, 881 max_index, num_instances)) { 882 return GL_FALSE; 883 } 884 885 num_vbuffers = 1; 886 num_velements = vpv->num_inputs; 887 if (num_velements == 0) 888 num_vbuffers = 0; 889 } 890 else { 891 if (!setup_non_interleaved_attribs(ctx, vp, vpv, arrays, 892 vbuffer, velements, max_index, 893 num_instances)) { 894 return GL_FALSE; 895 } 896 897 num_vbuffers = vpv->num_inputs; 898 num_velements = vpv->num_inputs; 899 } 900 901 cso_set_vertex_buffers(st->cso_context, num_vbuffers, vbuffer); 902 cso_set_vertex_elements(st->cso_context, num_velements, velements); 903 904 /* unreference buffers (frees wrapped user-space buffer objects) 905 * This is OK, because the pipe driver should reference buffers by itself 906 * in set_vertex_buffers. */ 907 for (attr = 0; attr < num_vbuffers; attr++) { 908 pipe_resource_reference(&vbuffer[attr].buffer, NULL); 909 assert(!vbuffer[attr].buffer); 910 } 911 912 return GL_TRUE; 913} 914 915 916/** 917 * This function gets plugged into the VBO module and is called when 918 * we have something to render. 919 * Basically, translate the information into the format expected by gallium. 920 */ 921void 922st_draw_vbo(struct gl_context *ctx, 923 const struct gl_client_array **arrays, 924 const struct _mesa_prim *prims, 925 GLuint nr_prims, 926 const struct _mesa_index_buffer *ib, 927 GLboolean index_bounds_valid, 928 GLuint min_index, 929 GLuint max_index, 930 struct gl_transform_feedback_object *tfb_vertcount) 931{ 932 struct st_context *st = st_context(ctx); 933 struct pipe_context *pipe = st->pipe; 934 struct pipe_index_buffer ibuffer; 935 struct pipe_draw_info info; 936 unsigned i, num_instances = 1; 937 GLboolean new_array = 938 st->dirty.st && 939 (st->dirty.mesa & (_NEW_ARRAY | _NEW_PROGRAM | _NEW_BUFFER_OBJECT)) != 0; 940 941 /* Mesa core state should have been validated already */ 942 assert(ctx->NewState == 0x0); 943 assert(!tfb_vertcount); 944 945 if (ib) { 946 /* Gallium probably doesn't want this in some cases. */ 947 if (!index_bounds_valid) 948 if (!all_varyings_in_vbos(arrays)) 949 vbo_get_minmax_index(ctx, prims, ib, &min_index, &max_index); 950 951 for (i = 0; i < nr_prims; i++) { 952 num_instances = MAX2(num_instances, prims[i].num_instances); 953 } 954 } 955 else { 956 /* Get min/max index for non-indexed drawing. */ 957 min_index = ~0; 958 max_index = 0; 959 960 for (i = 0; i < nr_prims; i++) { 961 min_index = MIN2(min_index, prims[i].start); 962 max_index = MAX2(max_index, prims[i].start + prims[i].count - 1); 963 num_instances = MAX2(num_instances, prims[i].num_instances); 964 } 965 } 966 967 /* Validate state. */ 968 if (st->dirty.st) { 969 GLboolean vertDataEdgeFlags; 970 971 /* sanity check for pointer arithmetic below */ 972 assert(sizeof(arrays[0]->Ptr[0]) == 1); 973 974 vertDataEdgeFlags = arrays[VERT_ATTRIB_EDGEFLAG]->BufferObj && 975 arrays[VERT_ATTRIB_EDGEFLAG]->BufferObj->Name; 976 if (vertDataEdgeFlags != st->vertdata_edgeflags) { 977 st->vertdata_edgeflags = vertDataEdgeFlags; 978 st->dirty.st |= ST_NEW_EDGEFLAGS_DATA; 979 } 980 981 st_validate_state(st); 982 983 if (new_array) { 984 if (!st_validate_varrays(ctx, arrays, max_index, num_instances)) { 985 /* probably out of memory, no-op the draw call */ 986 return; 987 } 988 } 989 990#if 0 991 if (MESA_VERBOSE & VERBOSE_GLSL) { 992 check_uniforms(ctx); 993 } 994#else 995 (void) check_uniforms; 996#endif 997 } 998 999 /* Notify the driver that the content of user buffers may have been 1000 * changed. */ 1001 assert(max_index >= min_index); 1002 if (!new_array && st->num_user_attribs) { 1003 for (i = 0; i < st->num_user_attribs; i++) { 1004 if (st->user_attrib[i].buffer) { 1005 unsigned element_size = st->user_attrib[i].element_size; 1006 unsigned stride = st->user_attrib[i].stride; 1007 unsigned min_offset = min_index * stride; 1008 unsigned max_offset = max_index * stride + element_size; 1009 1010 assert(max_offset > min_offset); 1011 1012 pipe->redefine_user_buffer(pipe, st->user_attrib[i].buffer, 1013 min_offset, 1014 max_offset - min_offset); 1015 } 1016 } 1017 } 1018 1019 setup_index_buffer(ctx, ib, &ibuffer); 1020 pipe->set_index_buffer(pipe, &ibuffer); 1021 1022 util_draw_init_info(&info); 1023 if (ib) { 1024 info.indexed = TRUE; 1025 if (min_index != ~0 && max_index != ~0) { 1026 info.min_index = min_index; 1027 info.max_index = max_index; 1028 } 1029 1030 /* The VBO module handles restart for the non-indexed GLDrawArrays 1031 * so we only set these fields for indexed drawing: 1032 */ 1033 info.primitive_restart = ctx->Array.PrimitiveRestart; 1034 info.restart_index = ctx->Array.RestartIndex; 1035 } 1036 1037 /* do actual drawing */ 1038 for (i = 0; i < nr_prims; i++) { 1039 info.mode = translate_prim( ctx, prims[i].mode ); 1040 info.start = prims[i].start; 1041 info.count = prims[i].count; 1042 info.instance_count = prims[i].num_instances; 1043 info.index_bias = prims[i].basevertex; 1044 if (!ib) { 1045 info.min_index = info.start; 1046 info.max_index = info.start + info.count - 1; 1047 } 1048 1049 if (info.primitive_restart) { 1050 if (st->sw_primitive_restart) { 1051 /* Handle primitive restart for drivers that doesn't support it */ 1052 handle_fallback_primitive_restart(pipe, ib, &ibuffer, &info); 1053 } 1054 else { 1055 /* don't trim, restarts might be inside index list */ 1056 pipe->draw_vbo(pipe, &info); 1057 } 1058 } 1059 else if (u_trim_pipe_prim(info.mode, &info.count)) 1060 pipe->draw_vbo(pipe, &info); 1061 } 1062 1063 pipe_resource_reference(&ibuffer.buffer, NULL); 1064} 1065 1066 1067void 1068st_init_draw(struct st_context *st) 1069{ 1070 struct gl_context *ctx = st->ctx; 1071 1072 vbo_set_draw_func(ctx, st_draw_vbo); 1073 1074#if FEATURE_feedback || FEATURE_rastpos 1075 st->draw = draw_create(st->pipe); /* for selection/feedback */ 1076 1077 /* Disable draw options that might convert points/lines to tris, etc. 1078 * as that would foul-up feedback/selection mode. 1079 */ 1080 draw_wide_line_threshold(st->draw, 1000.0f); 1081 draw_wide_point_threshold(st->draw, 1000.0f); 1082 draw_enable_line_stipple(st->draw, FALSE); 1083 draw_enable_point_sprites(st->draw, FALSE); 1084#endif 1085} 1086 1087 1088void 1089st_destroy_draw(struct st_context *st) 1090{ 1091#if FEATURE_feedback || FEATURE_rastpos 1092 draw_destroy(st->draw); 1093#endif 1094} 1095