t_draw.c revision af69d88d
11.67Skamil/* 21.1Sjtc * Mesa 3-D graphics library 31.1Sjtc * 41.1Sjtc * Copyright (C) 1999-2007 Brian Paul All Rights Reserved. 51.1Sjtc * 61.1Sjtc * Permission is hereby granted, free of charge, to any person obtaining a 71.1Sjtc * copy of this software and associated documentation files (the "Software"), 81.1Sjtc * to deal in the Software without restriction, including without limitation 91.1Sjtc * the rights to use, copy, modify, merge, publish, distribute, sublicense, 101.1Sjtc * and/or sell copies of the Software, and to permit persons to whom the 111.1Sjtc * Software is furnished to do so, subject to the following conditions: 121.1Sjtc * 131.1Sjtc * The above copyright notice and this permission notice shall be included 141.1Sjtc * in all copies or substantial portions of the Software. 151.31Sagc * 161.1Sjtc * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 171.1Sjtc * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 181.1Sjtc * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 191.1Sjtc * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 201.1Sjtc * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 211.1Sjtc * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 221.1Sjtc * OTHER DEALINGS IN THE SOFTWARE. 231.1Sjtc * 241.1Sjtc * Authors: 251.1Sjtc * Keith Whitwell <keithw@vmware.com> 261.1Sjtc */ 271.1Sjtc 281.1Sjtc#include "main/glheader.h" 291.1Sjtc#include "main/bufferobj.h" 301.1Sjtc#include "main/condrender.h" 311.1Sjtc#include "main/context.h" 321.1Sjtc#include "main/imports.h" 331.25Senami#include "main/mtypes.h" 341.1Sjtc#include "main/macros.h" 351.10Slukem#include "main/enums.h" 361.10Slukem 371.64Schristos#include "t_context.h" 381.64Schristos#include "tnl.h" 391.42Schristos 401.42Schristos 411.42Schristos 421.42Schristosstatic GLubyte *get_space(struct gl_context *ctx, GLuint bytes) 431.42Schristos{ 441.64Schristos TNLcontext *tnl = TNL_CONTEXT(ctx); 451.64Schristos GLubyte *space = malloc(bytes); 461.64Schristos 471.64Schristos tnl->block[tnl->nr_blocks++] = space; 481.64Schristos return space; 491.64Schristos} 501.64Schristos 511.64Schristos 521.64Schristosstatic void free_space(struct gl_context *ctx) 531.64Schristos{ 541.64Schristos TNLcontext *tnl = TNL_CONTEXT(ctx); 551.1Sjtc GLuint i; 561.7Scjs for (i = 0; i < tnl->nr_blocks; i++) 571.25Senami free(tnl->block[i]); 581.7Scjs tnl->nr_blocks = 0; 591.38Selad} 601.38Selad 611.38Selad 621.38Selad/* Convert the incoming array to GLfloats. Understands the 631.1Sjtc * array->Normalized flag and selects the correct conversion method. 641.27Selric */ 651.18Sad#define CONVERT( TYPE, MACRO ) do { \ 661.14Schristos GLuint i, j; \ 671.14Schristos if (input->Normalized) { \ 681.18Sad for (i = 0; i < count; i++) { \ 691.64Schristos const TYPE *in = (TYPE *)ptr; \ 701.14Schristos for (j = 0; j < sz; j++) { \ 711.33Schristos *fptr++ = MACRO(*in); \ 721.14Schristos in++; \ 731.48Schristos } \ 741.19Stron ptr += input->StrideB; \ 751.20Swiz } \ 761.65Smatt } else { \ 771.29Sscw for (i = 0; i < count; i++) { \ 781.30Sfvdl const TYPE *in = (TYPE *)ptr; \ 791.59Sbouyer for (j = 0; j < sz; j++) { \ 801.19Stron *fptr++ = (GLfloat)(*in); \ 811.19Stron in++; \ 821.63Schristos } \ 831.63Schristos ptr += input->StrideB; \ 841.60Schristos } \ 851.62Schristos } \ 861.50Schristos} while (0) 871.50Schristos 881.50Schristos 891.50Schristos/** 901.19Stron * Convert array of BGRA/GLubyte[4] values to RGBA/float[4] 911.19Stron * \param ptr input/ubyte array 921.23Schristos * \param fptr output/float array 931.19Stron */ 941.23Schristosstatic void 951.19Stronconvert_bgra_to_float(const struct gl_client_array *input, 961.52Spooka const GLubyte *ptr, GLfloat *fptr, 971.53Spooka GLuint count ) 981.19Stron{ 991.50Schristos GLuint i; 1001.50Schristos assert(input->Normalized); 1011.50Schristos assert(input->Size == 4); 1021.50Schristos for (i = 0; i < count; i++) { 1031.50Schristos const GLubyte *in = (GLubyte *) ptr; /* in is in BGRA order */ 1041.22Sitojun *fptr++ = UBYTE_TO_FLOAT(in[2]); /* red */ 1051.19Stron *fptr++ = UBYTE_TO_FLOAT(in[1]); /* green */ 1061.19Stron *fptr++ = UBYTE_TO_FLOAT(in[0]); /* blue */ 1071.50Schristos *fptr++ = UBYTE_TO_FLOAT(in[3]); /* alpha */ 1081.50Schristos ptr += input->StrideB; 1091.50Schristos } 1101.32Sthorpej} 1111.50Schristos 1121.50Schristosstatic void 1131.19Stronconvert_half_to_float(const struct gl_client_array *input, 1141.57Sjoerg const GLubyte *ptr, GLfloat *fptr, 1151.19Stron GLuint count, GLuint sz) 1161.50Schristos{ 1171.34Schristos GLuint i, j; 1181.50Schristos 1191.50Schristos for (i = 0; i < count; i++) { 1201.19Stron GLhalfARB *in = (GLhalfARB *)ptr; 1211.19Stron 1221.19Stron for (j = 0; j < sz; j++) { 1231.21Sad *fptr++ = _mesa_half_to_float(in[j]); 1241.19Stron } 1251.19Stron ptr += input->StrideB; 1261.47Slukem } 1271.19Stron} 1281.51Spgoyette 1291.26Schristos/** 1301.33Schristos * \brief Convert fixed-point to floating-point. 1311.33Schristos * 1321.58Schristos * In OpenGL, a fixed-point number is a "signed 2's complement 16.16 scaled 1331.61Schristos * integer" (Table 2.2 of the OpenGL ES 2.0 spec). 1341.48Schristos * 1351.19Stron * If the buffer has the \c normalized flag set, the formula 1361.19Stron * \code normalize(x) := (2*x + 1) / (2^16 - 1) \endcode 1371.19Stron * is used to map the fixed-point numbers into the range [-1, 1]. 1381.19Stron */ 1391.27Selricstatic void 1401.35Sthorpejconvert_fixed_to_float(const struct gl_client_array *input, 1411.27Selric const GLubyte *ptr, GLfloat *fptr, 1421.41Schristos GLuint count) 1431.41Schristos{ 1441.41Schristos GLuint i; 1451.41Schristos GLint j; 1461.41Schristos const GLint size = input->Size; 1471.41Schristos 1481.41Schristos if (input->Normalized) { 1491.46Spooka for (i = 0; i < count; ++i) { 1501.66Schristos const GLfixed *in = (GLfixed *) ptr; 1511.66Schristos for (j = 0; j < size; ++j) { 1521.41Schristos *fptr++ = (GLfloat) (2 * in[j] + 1) / (GLfloat) ((1 << 16) - 1); 1531.41Schristos } 1541.41Schristos ptr += input->StrideB; 1551.67Skamil } 1561.41Schristos } else { 1571.41Schristos for (i = 0; i < count; ++i) { 1581.55Sjoerg const GLfixed *in = (GLfixed *) ptr; 1591.64Schristos for (j = 0; j < size; ++j) { 1601.64Schristos *fptr++ = in[j] / (GLfloat) (1 << 16); 1611.1Sjtc } 1621.1Sjtc ptr += input->StrideB; 1631.1Sjtc } 164 } 165} 166 167/* Adjust pointer to point at first requested element, convert to 168 * floating point, populate VB->AttribPtr[]. 169 */ 170static void _tnl_import_array( struct gl_context *ctx, 171 GLuint attrib, 172 GLuint count, 173 const struct gl_client_array *input, 174 const GLubyte *ptr ) 175{ 176 TNLcontext *tnl = TNL_CONTEXT(ctx); 177 struct vertex_buffer *VB = &tnl->vb; 178 GLuint stride = input->StrideB; 179 180 if (input->Type != GL_FLOAT) { 181 const GLuint sz = input->Size; 182 GLubyte *buf = get_space(ctx, count * sz * sizeof(GLfloat)); 183 GLfloat *fptr = (GLfloat *)buf; 184 185 switch (input->Type) { 186 case GL_BYTE: 187 CONVERT(GLbyte, BYTE_TO_FLOAT); 188 break; 189 case GL_UNSIGNED_BYTE: 190 if (input->Format == GL_BGRA) { 191 /* See GL_EXT_vertex_array_bgra */ 192 convert_bgra_to_float(input, ptr, fptr, count); 193 } 194 else { 195 CONVERT(GLubyte, UBYTE_TO_FLOAT); 196 } 197 break; 198 case GL_SHORT: 199 CONVERT(GLshort, SHORT_TO_FLOAT); 200 break; 201 case GL_UNSIGNED_SHORT: 202 CONVERT(GLushort, USHORT_TO_FLOAT); 203 break; 204 case GL_INT: 205 CONVERT(GLint, INT_TO_FLOAT); 206 break; 207 case GL_UNSIGNED_INT: 208 CONVERT(GLuint, UINT_TO_FLOAT); 209 break; 210 case GL_DOUBLE: 211 CONVERT(GLdouble, (GLfloat)); 212 break; 213 case GL_HALF_FLOAT: 214 convert_half_to_float(input, ptr, fptr, count, sz); 215 break; 216 case GL_FIXED: 217 convert_fixed_to_float(input, ptr, fptr, count); 218 break; 219 default: 220 assert(0); 221 break; 222 } 223 224 ptr = buf; 225 stride = sz * sizeof(GLfloat); 226 } 227 228 VB->AttribPtr[attrib] = &tnl->tmp_inputs[attrib]; 229 VB->AttribPtr[attrib]->data = (GLfloat (*)[4])ptr; 230 VB->AttribPtr[attrib]->start = (GLfloat *)ptr; 231 VB->AttribPtr[attrib]->count = count; 232 VB->AttribPtr[attrib]->stride = stride; 233 VB->AttribPtr[attrib]->size = input->Size; 234 235 /* This should die, but so should the whole GLvector4f concept: 236 */ 237 VB->AttribPtr[attrib]->flags = (((1<<input->Size)-1) | 238 VEC_NOT_WRITEABLE | 239 (stride == 4*sizeof(GLfloat) ? 0 : VEC_BAD_STRIDE)); 240 241 VB->AttribPtr[attrib]->storage = NULL; 242} 243 244#define CLIPVERTS ((6 + MAX_CLIP_PLANES) * 2) 245 246 247static GLboolean *_tnl_import_edgeflag( struct gl_context *ctx, 248 const GLvector4f *input, 249 GLuint count) 250{ 251 const GLubyte *ptr = (const GLubyte *)input->data; 252 const GLuint stride = input->stride; 253 GLboolean *space = (GLboolean *)get_space(ctx, count + CLIPVERTS); 254 GLboolean *bptr = space; 255 GLuint i; 256 257 for (i = 0; i < count; i++) { 258 *bptr++ = ((GLfloat *)ptr)[0] == 1.0; 259 ptr += stride; 260 } 261 262 return space; 263} 264 265 266static void bind_inputs( struct gl_context *ctx, 267 const struct gl_client_array *inputs[], 268 GLint count, 269 struct gl_buffer_object **bo, 270 GLuint *nr_bo ) 271{ 272 TNLcontext *tnl = TNL_CONTEXT(ctx); 273 struct vertex_buffer *VB = &tnl->vb; 274 GLuint i; 275 276 /* Map all the VBOs 277 */ 278 for (i = 0; i < VERT_ATTRIB_MAX; i++) { 279 const void *ptr; 280 281 if (inputs[i]->BufferObj->Name) { 282 if (!inputs[i]->BufferObj->Mappings[MAP_INTERNAL].Pointer) { 283 bo[*nr_bo] = inputs[i]->BufferObj; 284 (*nr_bo)++; 285 ctx->Driver.MapBufferRange(ctx, 0, inputs[i]->BufferObj->Size, 286 GL_MAP_READ_BIT, 287 inputs[i]->BufferObj, 288 MAP_INTERNAL); 289 290 assert(inputs[i]->BufferObj->Mappings[MAP_INTERNAL].Pointer); 291 } 292 293 ptr = ADD_POINTERS(inputs[i]->BufferObj->Mappings[MAP_INTERNAL].Pointer, 294 inputs[i]->Ptr); 295 } 296 else 297 ptr = inputs[i]->Ptr; 298 299 /* Just make sure the array is floating point, otherwise convert to 300 * temporary storage. 301 * 302 * XXX: remove the GLvector4f type at some stage and just use 303 * client arrays. 304 */ 305 _tnl_import_array(ctx, i, count, inputs[i], ptr); 306 } 307 308 /* We process only the vertices between min & max index: 309 */ 310 VB->Count = count; 311 312 /* These should perhaps be part of _TNL_ATTRIB_* */ 313 VB->BackfaceColorPtr = NULL; 314 VB->BackfaceIndexPtr = NULL; 315 VB->BackfaceSecondaryColorPtr = NULL; 316 317 /* Clipping and drawing code still requires this to be a packed 318 * array of ubytes which can be written into. TODO: Fix and 319 * remove. 320 */ 321 if (ctx->Polygon.FrontMode != GL_FILL || 322 ctx->Polygon.BackMode != GL_FILL) 323 { 324 VB->EdgeFlag = _tnl_import_edgeflag( ctx, 325 VB->AttribPtr[_TNL_ATTRIB_EDGEFLAG], 326 VB->Count ); 327 } 328 else { 329 /* the data previously pointed to by EdgeFlag may have been freed */ 330 VB->EdgeFlag = NULL; 331 } 332} 333 334 335/* Translate indices to GLuints and store in VB->Elts. 336 */ 337static void bind_indices( struct gl_context *ctx, 338 const struct _mesa_index_buffer *ib, 339 struct gl_buffer_object **bo, 340 GLuint *nr_bo) 341{ 342 TNLcontext *tnl = TNL_CONTEXT(ctx); 343 struct vertex_buffer *VB = &tnl->vb; 344 GLuint i; 345 const void *ptr; 346 347 if (!ib) { 348 VB->Elts = NULL; 349 return; 350 } 351 352 if (_mesa_is_bufferobj(ib->obj) && 353 !_mesa_bufferobj_mapped(ib->obj, MAP_INTERNAL)) { 354 /* if the buffer object isn't mapped yet, map it now */ 355 bo[*nr_bo] = ib->obj; 356 (*nr_bo)++; 357 ptr = ctx->Driver.MapBufferRange(ctx, (GLsizeiptr) ib->ptr, 358 ib->count * vbo_sizeof_ib_type(ib->type), 359 GL_MAP_READ_BIT, ib->obj, 360 MAP_INTERNAL); 361 assert(ib->obj->Mappings[MAP_INTERNAL].Pointer); 362 } else { 363 /* user-space elements, or buffer already mapped */ 364 ptr = ADD_POINTERS(ib->obj->Mappings[MAP_INTERNAL].Pointer, ib->ptr); 365 } 366 367 if (ib->type == GL_UNSIGNED_INT && VB->Primitive[0].basevertex == 0) { 368 VB->Elts = (GLuint *) ptr; 369 } 370 else { 371 GLuint *elts = (GLuint *)get_space(ctx, ib->count * sizeof(GLuint)); 372 VB->Elts = elts; 373 374 if (ib->type == GL_UNSIGNED_INT) { 375 const GLuint *in = (GLuint *)ptr; 376 for (i = 0; i < ib->count; i++) 377 *elts++ = (GLuint)(*in++) + VB->Primitive[0].basevertex; 378 } 379 else if (ib->type == GL_UNSIGNED_SHORT) { 380 const GLushort *in = (GLushort *)ptr; 381 for (i = 0; i < ib->count; i++) 382 *elts++ = (GLuint)(*in++) + VB->Primitive[0].basevertex; 383 } 384 else { 385 const GLubyte *in = (GLubyte *)ptr; 386 for (i = 0; i < ib->count; i++) 387 *elts++ = (GLuint)(*in++) + VB->Primitive[0].basevertex; 388 } 389 } 390} 391 392static void bind_prims( struct gl_context *ctx, 393 const struct _mesa_prim *prim, 394 GLuint nr_prims ) 395{ 396 TNLcontext *tnl = TNL_CONTEXT(ctx); 397 struct vertex_buffer *VB = &tnl->vb; 398 399 VB->Primitive = prim; 400 VB->PrimitiveCount = nr_prims; 401} 402 403static void unmap_vbos( struct gl_context *ctx, 404 struct gl_buffer_object **bo, 405 GLuint nr_bo ) 406{ 407 GLuint i; 408 for (i = 0; i < nr_bo; i++) { 409 ctx->Driver.UnmapBuffer(ctx, bo[i], MAP_INTERNAL); 410 } 411} 412 413 414/* This is the main entrypoint into the slimmed-down software tnl 415 * module. In a regular swtnl driver, this can be plugged straight 416 * into the vbo->Driver.DrawPrims() callback. 417 */ 418void _tnl_draw_prims(struct gl_context *ctx, 419 const struct _mesa_prim *prim, 420 GLuint nr_prims, 421 const struct _mesa_index_buffer *ib, 422 GLboolean index_bounds_valid, 423 GLuint min_index, 424 GLuint max_index, 425 struct gl_transform_feedback_object *tfb_vertcount, 426 struct gl_buffer_object *indirect) 427{ 428 TNLcontext *tnl = TNL_CONTEXT(ctx); 429 const struct gl_client_array **arrays = ctx->Array._DrawArrays; 430 const GLuint TEST_SPLIT = 0; 431 const GLint max = TEST_SPLIT ? 8 : tnl->vb.Size - MAX_CLIPPED_VERTICES; 432 GLint max_basevertex = prim->basevertex; 433 GLuint i; 434 435 if (!index_bounds_valid) 436 vbo_get_minmax_indices(ctx, prim, ib, &min_index, &max_index, nr_prims); 437 438 /* Mesa core state should have been validated already */ 439 assert(ctx->NewState == 0x0); 440 441 if (!_mesa_check_conditional_render(ctx)) 442 return; /* don't draw */ 443 444 for (i = 1; i < nr_prims; i++) 445 max_basevertex = MAX2(max_basevertex, prim[i].basevertex); 446 447 if (0) 448 { 449 printf("%s %d..%d\n", __FUNCTION__, min_index, max_index); 450 for (i = 0; i < nr_prims; i++) 451 printf("prim %d: %s start %d count %d\n", i, 452 _mesa_lookup_enum_by_nr(prim[i].mode), 453 prim[i].start, 454 prim[i].count); 455 } 456 457 if (min_index) { 458 /* We always translate away calls with min_index != 0. 459 */ 460 vbo_rebase_prims( ctx, arrays, prim, nr_prims, ib, 461 min_index, max_index, 462 _tnl_draw_prims ); 463 return; 464 } 465 else if ((GLint)max_index + max_basevertex > max) { 466 /* The software TNL pipeline has a fixed amount of storage for 467 * vertices and it is necessary to split incoming drawing commands 468 * if they exceed that limit. 469 */ 470 struct split_limits limits; 471 limits.max_verts = max; 472 limits.max_vb_size = ~0; 473 limits.max_indices = ~0; 474 475 /* This will split the buffers one way or another and 476 * recursively call back into this function. 477 */ 478 vbo_split_prims( ctx, arrays, prim, nr_prims, ib, 479 0, max_index + prim->basevertex, 480 _tnl_draw_prims, 481 &limits ); 482 } 483 else { 484 /* May need to map a vertex buffer object for every attribute plus 485 * one for the index buffer. 486 */ 487 struct gl_buffer_object *bo[VERT_ATTRIB_MAX + 1]; 488 GLuint nr_bo = 0; 489 GLuint inst; 490 491 for (i = 0; i < nr_prims;) { 492 GLuint this_nr_prims; 493 494 /* Our SW TNL pipeline doesn't handle basevertex yet, so bind_indices 495 * will rebase the elements to the basevertex, and we'll only 496 * emit strings of prims with the same basevertex in one draw call. 497 */ 498 for (this_nr_prims = 1; i + this_nr_prims < nr_prims; 499 this_nr_prims++) { 500 if (prim[i].basevertex != prim[i + this_nr_prims].basevertex) 501 break; 502 } 503 504 assert(prim[i].num_instances > 0); 505 506 /* Binding inputs may imply mapping some vertex buffer objects. 507 * They will need to be unmapped below. 508 */ 509 for (inst = 0; inst < prim[i].num_instances; inst++) { 510 511 bind_prims(ctx, &prim[i], this_nr_prims); 512 bind_inputs(ctx, arrays, max_index + prim[i].basevertex + 1, 513 bo, &nr_bo); 514 bind_indices(ctx, ib, bo, &nr_bo); 515 516 tnl->CurInstance = inst; 517 TNL_CONTEXT(ctx)->Driver.RunPipeline(ctx); 518 519 unmap_vbos(ctx, bo, nr_bo); 520 free_space(ctx); 521 } 522 523 i += this_nr_prims; 524 } 525 } 526} 527 528