t_draw.c revision 7ec681f3
1/* 2 * Mesa 3-D graphics library 3 * 4 * Copyright (C) 1999-2007 Brian Paul 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 "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included 14 * in all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 * OTHER DEALINGS IN THE SOFTWARE. 23 * 24 * Authors: 25 * Keith Whitwell <keithw@vmware.com> 26 */ 27 28#include <stdio.h> 29 30#include "main/glheader.h" 31#include "main/arrayobj.h" 32#include "main/bufferobj.h" 33#include "main/condrender.h" 34#include "main/context.h" 35 36#include "main/mtypes.h" 37#include "main/macros.h" 38#include "main/enums.h" 39#include "main/varray.h" 40#include "util/half_float.h" 41 42#include "t_context.h" 43#include "t_rebase.h" 44#include "tnl.h" 45 46 47static GLubyte *get_space(struct gl_context *ctx, GLuint bytes) 48{ 49 TNLcontext *tnl = TNL_CONTEXT(ctx); 50 GLubyte *space = malloc(bytes); 51 52 tnl->block[tnl->nr_blocks++] = space; 53 return space; 54} 55 56 57static void free_space(struct gl_context *ctx) 58{ 59 TNLcontext *tnl = TNL_CONTEXT(ctx); 60 61 for (GLuint i = 0; i < tnl->nr_blocks; i++) 62 free(tnl->block[i]); 63 64 tnl->nr_blocks = 0; 65} 66 67 68/* Convert the incoming array to GLfloats. Understands the 69 * array->Normalized flag and selects the correct conversion method. 70 */ 71#define CONVERT( TYPE, MACRO ) do { \ 72 GLuint i, j; \ 73 if (attrib->Format.Normalized) { \ 74 for (i = 0; i < count; i++) { \ 75 const TYPE *in = (TYPE *)ptr; \ 76 for (j = 0; j < sz; j++) { \ 77 *fptr++ = MACRO(*in); \ 78 in++; \ 79 } \ 80 ptr += binding->Stride; \ 81 } \ 82 } else { \ 83 for (i = 0; i < count; i++) { \ 84 const TYPE *in = (TYPE *)ptr; \ 85 for (j = 0; j < sz; j++) { \ 86 *fptr++ = (GLfloat)(*in); \ 87 in++; \ 88 } \ 89 ptr += binding->Stride; \ 90 } \ 91 } \ 92} while (0) 93 94 95/** 96 * Convert array of BGRA/GLubyte[4] values to RGBA/float[4] 97 * \param ptr input/ubyte array 98 * \param fptr output/float array 99 */ 100static void 101convert_bgra_to_float(const struct gl_vertex_buffer_binding *binding, 102 const struct gl_array_attributes *attrib, 103 const GLubyte *ptr, GLfloat *fptr, 104 GLuint count) 105{ 106 GLuint i; 107 assert(attrib->Format.Normalized); 108 assert(attrib->Format.Size == 4); 109 for (i = 0; i < count; i++) { 110 const GLubyte *in = (GLubyte *) ptr; /* in is in BGRA order */ 111 *fptr++ = UBYTE_TO_FLOAT(in[2]); /* red */ 112 *fptr++ = UBYTE_TO_FLOAT(in[1]); /* green */ 113 *fptr++ = UBYTE_TO_FLOAT(in[0]); /* blue */ 114 *fptr++ = UBYTE_TO_FLOAT(in[3]); /* alpha */ 115 ptr += binding->Stride; 116 } 117} 118 119static void 120convert_half_to_float(const struct gl_vertex_buffer_binding *binding, 121 const GLubyte *ptr, GLfloat *fptr, 122 GLuint count, GLuint sz) 123{ 124 GLuint i, j; 125 126 for (i = 0; i < count; i++) { 127 GLhalfARB *in = (GLhalfARB *)ptr; 128 129 for (j = 0; j < sz; j++) 130 *fptr++ = _mesa_half_to_float(in[j]); 131 132 ptr += binding->Stride; 133 } 134} 135 136/** 137 * \brief Convert fixed-point to floating-point. 138 * 139 * In OpenGL, a fixed-point number is a "signed 2's complement 16.16 scaled 140 * integer" (Table 2.2 of the OpenGL ES 2.0 spec). 141 * 142 * If the buffer has the \c normalized flag set, the formula 143 * \code normalize(x) := (2*x + 1) / (2^16 - 1) \endcode 144 * is used to map the fixed-point numbers into the range [-1, 1]. 145 */ 146static void 147convert_fixed_to_float(const struct gl_vertex_buffer_binding *binding, 148 const struct gl_array_attributes *attrib, 149 const GLubyte *ptr, GLfloat *fptr, 150 GLuint count) 151{ 152 GLuint i; 153 GLint j; 154 const GLint size = attrib->Format.Size; 155 156 if (attrib->Format.Normalized) { 157 for (i = 0; i < count; ++i) { 158 const GLfixed *in = (GLfixed *) ptr; 159 for (j = 0; j < size; ++j) { 160 *fptr++ = (GLfloat) (2 * in[j] + 1) / (GLfloat) ((1 << 16) - 1); 161 } 162 ptr += binding->Stride; 163 } 164 } else { 165 for (i = 0; i < count; ++i) { 166 const GLfixed *in = (GLfixed *) ptr; 167 for (j = 0; j < size; ++j) { 168 *fptr++ = in[j] / (GLfloat) (1 << 16); 169 } 170 ptr += binding->Stride; 171 } 172 } 173} 174 175/* Adjust pointer to point at first requested element, convert to 176 * floating point, populate VB->AttribPtr[]. 177 */ 178static void _tnl_import_array(struct gl_context *ctx, 179 GLuint attr, 180 GLuint count, 181 const struct gl_vertex_buffer_binding *binding, 182 const struct gl_array_attributes *attrib, 183 const GLubyte *ptr) 184{ 185 TNLcontext *tnl = TNL_CONTEXT(ctx); 186 struct vertex_buffer *VB = &tnl->vb; 187 GLuint stride = binding->Stride; 188 189 if (attrib->Format.Type != GL_FLOAT) { 190 const GLuint sz = attrib->Format.Size; 191 GLubyte *buf = get_space(ctx, count * sz * sizeof(GLfloat)); 192 GLfloat *fptr = (GLfloat *)buf; 193 194 switch (attrib->Format.Type) { 195 case GL_BYTE: 196 CONVERT(GLbyte, BYTE_TO_FLOAT); 197 break; 198 case GL_UNSIGNED_BYTE: 199 if (attrib->Format.Format == GL_BGRA) { 200 /* See GL_EXT_vertex_array_bgra */ 201 convert_bgra_to_float(binding, attrib, ptr, fptr, count); 202 } 203 else { 204 CONVERT(GLubyte, UBYTE_TO_FLOAT); 205 } 206 break; 207 case GL_SHORT: 208 CONVERT(GLshort, SHORT_TO_FLOAT); 209 break; 210 case GL_UNSIGNED_SHORT: 211 CONVERT(GLushort, USHORT_TO_FLOAT); 212 break; 213 case GL_INT: 214 CONVERT(GLint, INT_TO_FLOAT); 215 break; 216 case GL_UNSIGNED_INT: 217 CONVERT(GLuint, UINT_TO_FLOAT); 218 break; 219 case GL_DOUBLE: 220 CONVERT(GLdouble, (GLfloat)); 221 break; 222 case GL_HALF_FLOAT: 223 convert_half_to_float(binding, ptr, fptr, count, sz); 224 break; 225 case GL_FIXED: 226 convert_fixed_to_float(binding, attrib, ptr, fptr, count); 227 break; 228 default: 229 unreachable("Invalid type."); 230 } 231 232 ptr = buf; 233 stride = sz * sizeof(GLfloat); 234 } 235 236 VB->AttribPtr[attr] = &tnl->tmp_inputs[attr]; 237 VB->AttribPtr[attr]->data = (GLfloat (*)[4])ptr; 238 VB->AttribPtr[attr]->start = (GLfloat *)ptr; 239 VB->AttribPtr[attr]->count = count; 240 VB->AttribPtr[attr]->stride = stride; 241 VB->AttribPtr[attr]->size = attrib->Format.Size; 242 243 /* This should die, but so should the whole GLvector4f concept: 244 */ 245 VB->AttribPtr[attr]->flags = (((1<<attrib->Format.Size)-1) | 246 VEC_NOT_WRITEABLE | 247 (stride == 4*sizeof(GLfloat) ? 0 : VEC_BAD_STRIDE)); 248 249 VB->AttribPtr[attr]->storage = NULL; 250} 251 252#define CLIPVERTS ((6 + MAX_CLIP_PLANES) * 2) 253 254 255static GLboolean *_tnl_import_edgeflag(struct gl_context *ctx, 256 const GLvector4f *input, 257 GLuint count) 258{ 259 const GLubyte *ptr = (const GLubyte *)input->data; 260 const GLuint stride = input->stride; 261 GLboolean *space = (GLboolean *)get_space(ctx, count + CLIPVERTS); 262 GLboolean *bptr = space; 263 264 for (GLuint i = 0; i < count; i++) { 265 *bptr++ = ((GLfloat *)ptr)[0] == 1.0F; 266 ptr += stride; 267 } 268 269 return space; 270} 271 272 273static void bind_inputs(struct gl_context *ctx, 274 const struct tnl_vertex_array *inputs, 275 GLint count, 276 struct gl_buffer_object **bo, 277 GLuint *nr_bo) 278{ 279 TNLcontext *tnl = TNL_CONTEXT(ctx); 280 struct vertex_buffer *VB = &tnl->vb; 281 282 /* Map all the VBOs 283 */ 284 for (unsigned i = 0; i < VERT_ATTRIB_MAX; i++) { 285 const struct tnl_vertex_array *array = &inputs[i]; 286 const struct gl_vertex_buffer_binding *binding = array->BufferBinding; 287 const struct gl_array_attributes *attrib = array->VertexAttrib; 288 const void *ptr; 289 290 if (binding->BufferObj) { 291 if (!binding->BufferObj->Mappings[MAP_INTERNAL].Pointer) { 292 bo[*nr_bo] = binding->BufferObj; 293 (*nr_bo)++; 294 ctx->Driver.MapBufferRange(ctx, 0, binding->BufferObj->Size, 295 GL_MAP_READ_BIT, 296 binding->BufferObj, 297 MAP_INTERNAL); 298 299 assert(binding->BufferObj->Mappings[MAP_INTERNAL].Pointer); 300 } 301 302 ptr = ADD_POINTERS(binding->BufferObj->Mappings[MAP_INTERNAL].Pointer, 303 binding->Offset + attrib->RelativeOffset); 304 } else 305 ptr = attrib->Ptr; 306 307 /* Just make sure the array is floating point, otherwise convert to 308 * temporary storage. 309 * 310 * XXX: remove the GLvector4f type at some stage and just use 311 * client arrays. 312 */ 313 _tnl_import_array(ctx, i, count, binding, attrib, ptr); 314 } 315 316 /* We process only the vertices between min & max index: 317 */ 318 VB->Count = count; 319 320 /* These should perhaps be part of _TNL_ATTRIB_* */ 321 VB->BackfaceColorPtr = NULL; 322 VB->BackfaceIndexPtr = NULL; 323 VB->BackfaceSecondaryColorPtr = NULL; 324 325 /* Clipping and drawing code still requires this to be a packed 326 * array of ubytes which can be written into. TODO: Fix and 327 * remove. 328 */ 329 if (ctx->Polygon.FrontMode != GL_FILL || 330 ctx->Polygon.BackMode != GL_FILL) { 331 VB->EdgeFlag = _tnl_import_edgeflag(ctx, 332 VB->AttribPtr[_TNL_ATTRIB_EDGEFLAG], 333 VB->Count); 334 } else { 335 /* the data previously pointed to by EdgeFlag may have been freed */ 336 VB->EdgeFlag = NULL; 337 } 338} 339 340 341/* Translate indices to GLuints and store in VB->Elts. 342 */ 343static void bind_indices(struct gl_context *ctx, 344 unsigned start, 345 const struct _mesa_index_buffer *ib, 346 struct gl_buffer_object **bo, 347 GLuint *nr_bo) 348{ 349 TNLcontext *tnl = TNL_CONTEXT(ctx); 350 struct vertex_buffer *VB = &tnl->vb; 351 GLuint i; 352 const void *ptr; 353 354 if (!ib) { 355 VB->Elts = NULL; 356 return; 357 } 358 359 if (ib->obj) { 360 if (!_mesa_bufferobj_mapped(ib->obj, MAP_INTERNAL)) { 361 /* if the buffer object isn't mapped yet, map it now */ 362 bo[*nr_bo] = ib->obj; 363 (*nr_bo)++; 364 ptr = ctx->Driver.MapBufferRange(ctx, (GLsizeiptr) ib->ptr, 365 ib->count << ib->index_size_shift, 366 GL_MAP_READ_BIT, ib->obj, 367 MAP_INTERNAL); 368 assert(ib->obj->Mappings[MAP_INTERNAL].Pointer); 369 } else { 370 /* user-space elements, or buffer already mapped */ 371 ptr = ADD_POINTERS(ib->obj->Mappings[MAP_INTERNAL].Pointer, ib->ptr); 372 } 373 } else 374 ptr = ib->ptr; 375 376 if (ib->index_size_shift == 2 && VB->Primitive[0].basevertex == 0) { 377 VB->Elts = (GLuint *) ptr; 378 } 379 else { 380 GLuint *elts = (GLuint *)get_space(ctx, (start + ib->count) * sizeof(GLuint)); 381 VB->Elts = elts; 382 383 elts += start; 384 385 if (ib->index_size_shift == 2) { 386 const GLuint *in = (GLuint *)ptr + start; 387 for (i = 0; i < ib->count; i++) 388 *elts++ = (GLuint)(*in++) + VB->Primitive[0].basevertex; 389 } 390 else if (ib->index_size_shift == 1) { 391 const GLushort *in = (GLushort *)ptr + start; 392 for (i = 0; i < ib->count; i++) 393 *elts++ = (GLuint)(*in++) + VB->Primitive[0].basevertex; 394 } 395 else { 396 const GLubyte *in = (GLubyte *)ptr + start; 397 for (i = 0; i < ib->count; i++) 398 *elts++ = (GLuint)(*in++) + VB->Primitive[0].basevertex; 399 } 400 } 401} 402 403static void bind_prims(struct gl_context *ctx, 404 const struct _mesa_prim *prim, 405 GLuint nr_prims) 406{ 407 TNLcontext *tnl = TNL_CONTEXT(ctx); 408 struct vertex_buffer *VB = &tnl->vb; 409 410 VB->Primitive = prim; 411 VB->PrimitiveCount = nr_prims; 412} 413 414static void unmap_vbos(struct gl_context *ctx, 415 struct gl_buffer_object **bo, 416 GLuint nr_bo) 417{ 418 for (GLuint i = 0; i < nr_bo; i++) { 419 ctx->Driver.UnmapBuffer(ctx, bo[i], MAP_INTERNAL); 420 } 421} 422 423 424/* This is the main workhorse doing all the rendering work. 425 */ 426void _tnl_draw_prims(struct gl_context *ctx, 427 const struct tnl_vertex_array *arrays, 428 const struct _mesa_prim *prim, 429 GLuint nr_prims, 430 const struct _mesa_index_buffer *ib, 431 GLboolean index_bounds_valid, 432 GLuint min_index, 433 GLuint max_index, 434 GLuint num_instances, 435 GLuint base_instance) 436{ 437 TNLcontext *tnl = TNL_CONTEXT(ctx); 438 const GLuint TEST_SPLIT = 0; 439 const GLint max = TEST_SPLIT ? 8 : tnl->vb.Size - MAX_CLIPPED_VERTICES; 440 GLint max_basevertex = prim->basevertex; 441 GLuint i; 442 443 if (!index_bounds_valid) 444 vbo_get_minmax_indices(ctx, prim, ib, &min_index, &max_index, nr_prims, 445 false, 0); 446 447 /* Mesa core state should have been validated already */ 448 assert(ctx->NewState == 0x0); 449 450 if (!_mesa_check_conditional_render(ctx)) 451 return; /* don't draw */ 452 453 for (i = 1; i < nr_prims; i++) 454 max_basevertex = MAX2(max_basevertex, prim[i].basevertex); 455 456 if (0) { 457 printf("%s %d..%d\n", __func__, min_index, max_index); 458 for (i = 0; i < nr_prims; i++) 459 printf("prim %d: %s start %d count %d\n", i, 460 _mesa_enum_to_string(prim[i].mode), 461 prim[i].start, 462 prim[i].count); 463 } 464 465 if (min_index) { 466 /* We always translate away calls with min_index != 0. 467 */ 468 t_rebase_prims(ctx, arrays, prim, nr_prims, ib, 469 min_index, max_index, num_instances, base_instance, 470 _tnl_draw_prims); 471 return; 472 } 473 else if ((GLint)max_index + max_basevertex > max) { 474 /* The software TNL pipeline has a fixed amount of storage for 475 * vertices and it is necessary to split incoming drawing commands 476 * if they exceed that limit. 477 */ 478 struct split_limits limits; 479 limits.max_verts = max; 480 limits.max_vb_size = ~0; 481 limits.max_indices = ~0; 482 483 /* This will split the buffers one way or another and 484 * recursively call back into this function. 485 */ 486 _tnl_split_prims(ctx, arrays, prim, nr_prims, ib, 487 0, max_index + prim->basevertex, 488 num_instances, base_instance, 489 _tnl_draw_prims, 490 &limits); 491 } 492 else { 493 /* May need to map a vertex buffer object for every attribute plus 494 * one for the index buffer. 495 */ 496 struct gl_buffer_object *bo[VERT_ATTRIB_MAX + 1]; 497 GLuint nr_bo; 498 GLuint inst; 499 500 assert(num_instances > 0); 501 502 for (i = 0; i < nr_prims;) { 503 GLuint this_nr_prims; 504 505 /* Our SW TNL pipeline doesn't handle basevertex yet, so bind_indices 506 * will rebase the elements to the basevertex, and we'll only 507 * emit strings of prims with the same basevertex in one draw call. 508 */ 509 for (this_nr_prims = 1; i + this_nr_prims < nr_prims; 510 this_nr_prims++) { 511 if (prim[i].basevertex != prim[i + this_nr_prims].basevertex || 512 prim[i].start != prim[i + this_nr_prims].start) 513 break; 514 } 515 516 /* Binding inputs may imply mapping some vertex buffer objects. 517 * They will need to be unmapped below. 518 */ 519 for (inst = 0; inst < num_instances; inst++) { 520 nr_bo = 0; 521 522 bind_prims(ctx, &prim[i], this_nr_prims); 523 bind_inputs(ctx, arrays, max_index + prim[i].basevertex + 1, 524 bo, &nr_bo); 525 bind_indices(ctx, prim[i].start, ib, bo, &nr_bo); 526 527 tnl->CurInstance = inst; 528 TNL_CONTEXT(ctx)->Driver.RunPipeline(ctx); 529 530 unmap_vbos(ctx, bo, nr_bo); 531 free_space(ctx); 532 } 533 534 i += this_nr_prims; 535 } 536 } 537} 538 539 540void 541_tnl_init_inputs(struct tnl_inputs *inputs) 542{ 543 inputs->current = 0; 544 inputs->vertex_processing_mode = VP_MODE_FF; 545} 546 547 548/** 549 * Update the tnl_inputs's arrays to point to the vao->_VertexArray arrays 550 * according to the 'enable' bitmask. 551 * \param enable bitfield of VERT_BIT_x flags. 552 */ 553static inline void 554update_vao_inputs(struct gl_context *ctx, 555 struct tnl_inputs *inputs, GLbitfield enable) 556{ 557 const struct gl_vertex_array_object *vao = ctx->Array._DrawVAO; 558 559 /* Make sure we process only arrays enabled in the VAO */ 560 assert((enable & ~vao->_EnabledWithMapMode) == 0); 561 562 /* Fill in the client arrays from the VAO */ 563 const struct gl_vertex_buffer_binding *bindings = &vao->BufferBinding[0]; 564 while (enable) { 565 const int attr = u_bit_scan(&enable); 566 struct tnl_vertex_array *input = &inputs->inputs[attr]; 567 const struct gl_array_attributes *attrib; 568 attrib = _mesa_draw_array_attrib(vao, attr); 569 input->VertexAttrib = attrib; 570 input->BufferBinding = &bindings[attrib->BufferBindingIndex]; 571 } 572} 573 574 575/** 576 * Update the tnl_inputs's arrays to point to the vbo->currval arrays 577 * according to the 'current' bitmask. 578 * \param current bitfield of VERT_BIT_x flags. 579 */ 580static inline void 581update_current_inputs(struct gl_context *ctx, 582 struct tnl_inputs *inputs, GLbitfield current) 583{ 584 gl_vertex_processing_mode mode = ctx->VertexProgram._VPMode; 585 586 /* All previously non current array pointers need update. */ 587 GLbitfield mask = current & ~inputs->current; 588 /* On mode change, the slots aliasing with materials need update too */ 589 if (mode != inputs->vertex_processing_mode) 590 mask |= current & VERT_BIT_MAT_ALL; 591 592 while (mask) { 593 const int attr = u_bit_scan(&mask); 594 struct tnl_vertex_array *input = &inputs->inputs[attr]; 595 input->VertexAttrib = _vbo_current_attrib(ctx, attr); 596 input->BufferBinding = _vbo_current_binding(ctx); 597 } 598 599 inputs->current = current; 600 inputs->vertex_processing_mode = mode; 601} 602 603 604/** 605 * Update the tnl_inputs's arrays to point to the vao->_VertexArray and 606 * vbo->currval arrays according to Array._DrawVAO and 607 * Array._DrawVAOEnableAttribs. 608 */ 609void 610_tnl_update_inputs(struct gl_context *ctx, struct tnl_inputs *inputs) 611{ 612 const GLbitfield enable = ctx->Array._DrawVAOEnabledAttribs; 613 614 /* Update array input pointers */ 615 update_vao_inputs(ctx, inputs, enable); 616 617 /* The rest must be current inputs. */ 618 update_current_inputs(ctx, inputs, ~enable & VERT_BIT_ALL); 619} 620 621 622const struct tnl_vertex_array * 623_tnl_bind_inputs(struct gl_context *ctx) 624{ 625 TNLcontext *tnl = TNL_CONTEXT(ctx); 626 _tnl_update_inputs(ctx, &tnl->draw_arrays); 627 return tnl->draw_arrays.inputs; 628} 629 630 631/* This is the main entrypoint into the slimmed-down software tnl 632 * module. In a regular swtnl driver, this can be plugged straight 633 * into the ctx->Driver.Draw() callback. 634 */ 635void 636_tnl_draw(struct gl_context *ctx, 637 const struct _mesa_prim *prim, unsigned nr_prims, 638 const struct _mesa_index_buffer *ib, 639 bool index_bounds_valid, bool primitive_restart, 640 unsigned restart_index, unsigned min_index, unsigned max_index, 641 unsigned num_instances, unsigned base_instance) 642{ 643 /* Update TNLcontext::draw_arrays and return that pointer. 644 */ 645 const struct tnl_vertex_array* arrays = _tnl_bind_inputs(ctx); 646 647 _tnl_draw_prims(ctx, arrays, prim, nr_prims, ib, 648 index_bounds_valid, min_index, max_index, 649 num_instances, base_instance); 650} 651 652 653void 654_tnl_init_driver_draw_function(struct dd_function_table *functions) 655{ 656 functions->Draw = _tnl_draw; 657} 658