t_draw.c revision 298be2b028263b2c343a707662c6fbfa18293cb2
1/* 2 * Mesa 3-D graphics library 3 * Version: 7.1 4 * 5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved. 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a 8 * copy of this software and associated documentation files (the "Software"), 9 * to deal in the Software without restriction, including without limitation 10 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 11 * and/or sell copies of the Software, and to permit persons to whom the 12 * Software is furnished to do so, subject to the following conditions: 13 * 14 * The above copyright notice and this permission notice shall be included 15 * in all copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 * 24 * Authors: 25 * Keith Whitwell <keith@tungstengraphics.com> 26 */ 27 28#include "main/glheader.h" 29#include "main/condrender.h" 30#include "main/context.h" 31#include "main/imports.h" 32#include "main/mtypes.h" 33#include "main/macros.h" 34#include "main/enums.h" 35 36#include "t_context.h" 37#include "tnl.h" 38 39 40 41static GLubyte *get_space(GLcontext *ctx, GLuint bytes) 42{ 43 TNLcontext *tnl = TNL_CONTEXT(ctx); 44 GLubyte *space = malloc(bytes); 45 46 tnl->block[tnl->nr_blocks++] = space; 47 return space; 48} 49 50 51static void free_space(GLcontext *ctx) 52{ 53 TNLcontext *tnl = TNL_CONTEXT(ctx); 54 GLuint i; 55 for (i = 0; i < tnl->nr_blocks; i++) 56 free(tnl->block[i]); 57 tnl->nr_blocks = 0; 58} 59 60 61/* Convert the incoming array to GLfloats. Understands the 62 * array->Normalized flag and selects the correct conversion method. 63 */ 64#define CONVERT( TYPE, MACRO ) do { \ 65 GLuint i, j; \ 66 if (input->Normalized) { \ 67 for (i = 0; i < count; i++) { \ 68 const TYPE *in = (TYPE *)ptr; \ 69 for (j = 0; j < sz; j++) { \ 70 *fptr++ = MACRO(*in); \ 71 in++; \ 72 } \ 73 ptr += input->StrideB; \ 74 } \ 75 } else { \ 76 for (i = 0; i < count; i++) { \ 77 const TYPE *in = (TYPE *)ptr; \ 78 for (j = 0; j < sz; j++) { \ 79 *fptr++ = (GLfloat)(*in); \ 80 in++; \ 81 } \ 82 ptr += input->StrideB; \ 83 } \ 84 } \ 85} while (0) 86 87 88/** 89 * Convert array of BGRA/GLubyte[4] values to RGBA/float[4] 90 * \param ptr input/ubyte array 91 * \param fptr output/float array 92 */ 93static void 94convert_bgra_to_float(const struct gl_client_array *input, 95 const GLubyte *ptr, GLfloat *fptr, 96 GLuint count ) 97{ 98 GLuint i; 99 assert(input->Normalized); 100 assert(input->Size == 4); 101 for (i = 0; i < count; i++) { 102 const GLubyte *in = (GLubyte *) ptr; /* in is in BGRA order */ 103 *fptr++ = UBYTE_TO_FLOAT(in[2]); /* red */ 104 *fptr++ = UBYTE_TO_FLOAT(in[1]); /* green */ 105 *fptr++ = UBYTE_TO_FLOAT(in[0]); /* blue */ 106 *fptr++ = UBYTE_TO_FLOAT(in[3]); /* alpha */ 107 ptr += input->StrideB; 108 } 109} 110 111static void 112convert_half_to_float(const struct gl_client_array *input, 113 const GLubyte *ptr, GLfloat *fptr, 114 GLuint count, GLuint sz) 115{ 116 GLuint i, j; 117 118 for (i = 0; i < count; i++) { 119 GLhalfARB *in = (GLhalfARB *)ptr; 120 121 for (j = 0; j < sz; j++) { 122 *fptr++ = _mesa_half_to_float(in[j]); 123 } 124 ptr += input->StrideB; 125 } 126} 127 128/* Adjust pointer to point at first requested element, convert to 129 * floating point, populate VB->AttribPtr[]. 130 */ 131static void _tnl_import_array( GLcontext *ctx, 132 GLuint attrib, 133 GLuint count, 134 const struct gl_client_array *input, 135 const GLubyte *ptr ) 136{ 137 TNLcontext *tnl = TNL_CONTEXT(ctx); 138 struct vertex_buffer *VB = &tnl->vb; 139 GLuint stride = input->StrideB; 140 141 if (input->Type != GL_FLOAT) { 142 const GLuint sz = input->Size; 143 GLubyte *buf = get_space(ctx, count * sz * sizeof(GLfloat)); 144 GLfloat *fptr = (GLfloat *)buf; 145 146 switch (input->Type) { 147 case GL_BYTE: 148 CONVERT(GLbyte, BYTE_TO_FLOAT); 149 break; 150 case GL_UNSIGNED_BYTE: 151 if (input->Format == GL_BGRA) { 152 /* See GL_EXT_vertex_array_bgra */ 153 convert_bgra_to_float(input, ptr, fptr, count); 154 } 155 else { 156 CONVERT(GLubyte, UBYTE_TO_FLOAT); 157 } 158 break; 159 case GL_SHORT: 160 CONVERT(GLshort, SHORT_TO_FLOAT); 161 break; 162 case GL_UNSIGNED_SHORT: 163 CONVERT(GLushort, USHORT_TO_FLOAT); 164 break; 165 case GL_INT: 166 CONVERT(GLint, INT_TO_FLOAT); 167 break; 168 case GL_UNSIGNED_INT: 169 CONVERT(GLuint, UINT_TO_FLOAT); 170 break; 171 case GL_DOUBLE: 172 CONVERT(GLdouble, (GLfloat)); 173 break; 174 case GL_HALF_FLOAT: 175 convert_half_to_float(input, ptr, fptr, count, sz); 176 break; 177 default: 178 assert(0); 179 break; 180 } 181 182 ptr = buf; 183 stride = sz * sizeof(GLfloat); 184 } 185 186 VB->AttribPtr[attrib] = &tnl->tmp_inputs[attrib]; 187 VB->AttribPtr[attrib]->data = (GLfloat (*)[4])ptr; 188 VB->AttribPtr[attrib]->start = (GLfloat *)ptr; 189 VB->AttribPtr[attrib]->count = count; 190 VB->AttribPtr[attrib]->stride = stride; 191 VB->AttribPtr[attrib]->size = input->Size; 192 193 /* This should die, but so should the whole GLvector4f concept: 194 */ 195 VB->AttribPtr[attrib]->flags = (((1<<input->Size)-1) | 196 VEC_NOT_WRITEABLE | 197 (stride == 4*sizeof(GLfloat) ? 0 : VEC_BAD_STRIDE)); 198 199 VB->AttribPtr[attrib]->storage = NULL; 200} 201 202#define CLIPVERTS ((6 + MAX_CLIP_PLANES) * 2) 203 204 205static GLboolean *_tnl_import_edgeflag( GLcontext *ctx, 206 const GLvector4f *input, 207 GLuint count) 208{ 209 const GLubyte *ptr = (const GLubyte *)input->data; 210 const GLuint stride = input->stride; 211 GLboolean *space = (GLboolean *)get_space(ctx, count + CLIPVERTS); 212 GLboolean *bptr = space; 213 GLuint i; 214 215 for (i = 0; i < count; i++) { 216 *bptr++ = ((GLfloat *)ptr)[0] == 1.0; 217 ptr += stride; 218 } 219 220 return space; 221} 222 223 224static void bind_inputs( GLcontext *ctx, 225 const struct gl_client_array *inputs[], 226 GLint count, 227 struct gl_buffer_object **bo, 228 GLuint *nr_bo ) 229{ 230 TNLcontext *tnl = TNL_CONTEXT(ctx); 231 struct vertex_buffer *VB = &tnl->vb; 232 GLuint i; 233 234 /* Map all the VBOs 235 */ 236 for (i = 0; i < VERT_ATTRIB_MAX; i++) { 237 const void *ptr; 238 239 if (inputs[i]->BufferObj->Name) { 240 if (!inputs[i]->BufferObj->Pointer) { 241 bo[*nr_bo] = inputs[i]->BufferObj; 242 (*nr_bo)++; 243 ctx->Driver.MapBuffer(ctx, 244 GL_ARRAY_BUFFER, 245 GL_READ_ONLY_ARB, 246 inputs[i]->BufferObj); 247 248 assert(inputs[i]->BufferObj->Pointer); 249 } 250 251 ptr = ADD_POINTERS(inputs[i]->BufferObj->Pointer, 252 inputs[i]->Ptr); 253 } 254 else 255 ptr = inputs[i]->Ptr; 256 257 /* Just make sure the array is floating point, otherwise convert to 258 * temporary storage. 259 * 260 * XXX: remove the GLvector4f type at some stage and just use 261 * client arrays. 262 */ 263 _tnl_import_array(ctx, i, count, inputs[i], ptr); 264 } 265 266 /* We process only the vertices between min & max index: 267 */ 268 VB->Count = count; 269 270 /* These should perhaps be part of _TNL_ATTRIB_* */ 271 VB->BackfaceColorPtr = NULL; 272 VB->BackfaceIndexPtr = NULL; 273 VB->BackfaceSecondaryColorPtr = NULL; 274 275 /* Clipping and drawing code still requires this to be a packed 276 * array of ubytes which can be written into. TODO: Fix and 277 * remove. 278 */ 279 if (ctx->Polygon.FrontMode != GL_FILL || 280 ctx->Polygon.BackMode != GL_FILL) 281 { 282 VB->EdgeFlag = _tnl_import_edgeflag( ctx, 283 VB->AttribPtr[_TNL_ATTRIB_EDGEFLAG], 284 VB->Count ); 285 } 286 else { 287 /* the data previously pointed to by EdgeFlag may have been freed */ 288 VB->EdgeFlag = NULL; 289 } 290} 291 292 293/* Translate indices to GLuints and store in VB->Elts. 294 */ 295static void bind_indices( GLcontext *ctx, 296 const struct _mesa_index_buffer *ib, 297 struct gl_buffer_object **bo, 298 GLuint *nr_bo) 299{ 300 TNLcontext *tnl = TNL_CONTEXT(ctx); 301 struct vertex_buffer *VB = &tnl->vb; 302 GLuint i; 303 void *ptr; 304 305 if (!ib) { 306 VB->Elts = NULL; 307 return; 308 } 309 310 if (ib->obj->Name && !ib->obj->Pointer) { 311 bo[*nr_bo] = ib->obj; 312 (*nr_bo)++; 313 ctx->Driver.MapBuffer(ctx, 314 GL_ELEMENT_ARRAY_BUFFER, 315 GL_READ_ONLY_ARB, 316 ib->obj); 317 318 assert(ib->obj->Pointer); 319 } 320 321 ptr = ADD_POINTERS(ib->obj->Pointer, ib->ptr); 322 323 if (ib->type == GL_UNSIGNED_INT && VB->Primitive[0].basevertex == 0) { 324 VB->Elts = (GLuint *) ptr; 325 } 326 else { 327 GLuint *elts = (GLuint *)get_space(ctx, ib->count * sizeof(GLuint)); 328 VB->Elts = elts; 329 330 if (ib->type == GL_UNSIGNED_INT) { 331 const GLuint *in = (GLuint *)ptr; 332 for (i = 0; i < ib->count; i++) 333 *elts++ = (GLuint)(*in++) + VB->Primitive[0].basevertex; 334 } 335 else if (ib->type == GL_UNSIGNED_SHORT) { 336 const GLushort *in = (GLushort *)ptr; 337 for (i = 0; i < ib->count; i++) 338 *elts++ = (GLuint)(*in++) + VB->Primitive[0].basevertex; 339 } 340 else { 341 const GLubyte *in = (GLubyte *)ptr; 342 for (i = 0; i < ib->count; i++) 343 *elts++ = (GLuint)(*in++) + VB->Primitive[0].basevertex; 344 } 345 } 346} 347 348static void bind_prims( GLcontext *ctx, 349 const struct _mesa_prim *prim, 350 GLuint nr_prims ) 351{ 352 TNLcontext *tnl = TNL_CONTEXT(ctx); 353 struct vertex_buffer *VB = &tnl->vb; 354 355 VB->Primitive = prim; 356 VB->PrimitiveCount = nr_prims; 357} 358 359static void unmap_vbos( GLcontext *ctx, 360 struct gl_buffer_object **bo, 361 GLuint nr_bo ) 362{ 363 GLuint i; 364 for (i = 0; i < nr_bo; i++) { 365 ctx->Driver.UnmapBuffer(ctx, 366 0, /* target -- I don't see why this would be needed */ 367 bo[i]); 368 } 369} 370 371 372void _tnl_vbo_draw_prims(GLcontext *ctx, 373 const struct gl_client_array *arrays[], 374 const struct _mesa_prim *prim, 375 GLuint nr_prims, 376 const struct _mesa_index_buffer *ib, 377 GLboolean index_bounds_valid, 378 GLuint min_index, 379 GLuint max_index) 380{ 381 if (!index_bounds_valid) 382 vbo_get_minmax_index(ctx, prim, ib, &min_index, &max_index); 383 384 _tnl_draw_prims(ctx, arrays, prim, nr_prims, ib, min_index, max_index); 385} 386 387/* This is the main entrypoint into the slimmed-down software tnl 388 * module. In a regular swtnl driver, this can be plugged straight 389 * into the vbo->Driver.DrawPrims() callback. 390 */ 391void _tnl_draw_prims( GLcontext *ctx, 392 const struct gl_client_array *arrays[], 393 const struct _mesa_prim *prim, 394 GLuint nr_prims, 395 const struct _mesa_index_buffer *ib, 396 GLuint min_index, 397 GLuint max_index) 398{ 399 TNLcontext *tnl = TNL_CONTEXT(ctx); 400 const GLuint TEST_SPLIT = 0; 401 const GLint max = TEST_SPLIT ? 8 : tnl->vb.Size - MAX_CLIPPED_VERTICES; 402 GLint max_basevertex = prim->basevertex; 403 GLuint i; 404 405 /* Mesa core state should have been validated already */ 406 assert(ctx->NewState == 0x0); 407 408 if (!_mesa_check_conditional_render(ctx)) 409 return; /* don't draw */ 410 411 for (i = 1; i < nr_prims; i++) 412 max_basevertex = MAX2(max_basevertex, prim[i].basevertex); 413 414 if (0) 415 { 416 printf("%s %d..%d\n", __FUNCTION__, min_index, max_index); 417 for (i = 0; i < nr_prims; i++) 418 printf("prim %d: %s start %d count %d\n", i, 419 _mesa_lookup_enum_by_nr(prim[i].mode), 420 prim[i].start, 421 prim[i].count); 422 } 423 424 if (min_index) { 425 /* We always translate away calls with min_index != 0. 426 */ 427 vbo_rebase_prims( ctx, arrays, prim, nr_prims, ib, 428 min_index, max_index, 429 _tnl_vbo_draw_prims ); 430 return; 431 } 432 else if ((GLint)max_index + max_basevertex > max) { 433 /* The software TNL pipeline has a fixed amount of storage for 434 * vertices and it is necessary to split incoming drawing commands 435 * if they exceed that limit. 436 */ 437 struct split_limits limits; 438 limits.max_verts = max; 439 limits.max_vb_size = ~0; 440 limits.max_indices = ~0; 441 442 /* This will split the buffers one way or another and 443 * recursively call back into this function. 444 */ 445 vbo_split_prims( ctx, arrays, prim, nr_prims, ib, 446 0, max_index + prim->basevertex, 447 _tnl_vbo_draw_prims, 448 &limits ); 449 } 450 else { 451 /* May need to map a vertex buffer object for every attribute plus 452 * one for the index buffer. 453 */ 454 struct gl_buffer_object *bo[VERT_ATTRIB_MAX + 1]; 455 GLuint nr_bo = 0; 456 457 for (i = 0; i < nr_prims;) { 458 GLuint this_nr_prims; 459 460 /* Our SW TNL pipeline doesn't handle basevertex yet, so bind_indices 461 * will rebase the elements to the basevertex, and we'll only 462 * emit strings of prims with the same basevertex in one draw call. 463 */ 464 for (this_nr_prims = 1; i + this_nr_prims < nr_prims; 465 this_nr_prims++) { 466 if (prim[i].basevertex != prim[i + this_nr_prims].basevertex) 467 break; 468 } 469 470 /* Binding inputs may imply mapping some vertex buffer objects. 471 * They will need to be unmapped below. 472 */ 473 bind_prims(ctx, &prim[i], this_nr_prims); 474 bind_inputs(ctx, arrays, max_index + prim[i].basevertex + 1, 475 bo, &nr_bo); 476 bind_indices(ctx, ib, bo, &nr_bo); 477 478 TNL_CONTEXT(ctx)->Driver.RunPipeline(ctx); 479 480 unmap_vbos(ctx, bo, nr_bo); 481 free_space(ctx); 482 483 i += this_nr_prims; 484 } 485 } 486} 487 488