t_vb_program.c revision f4511c4835879090ce7e6afe3ac26b98fb91899a
1/* 2 * Mesa 3-D graphics library 3 * Version: 7.6 4 * 5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved. 6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved. 7 * 8 * Permission is hereby granted, free of charge, to any person obtaining a 9 * copy of this software and associated documentation files (the "Software"), 10 * to deal in the Software without restriction, including without limitation 11 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 12 * and/or sell copies of the Software, and to permit persons to whom the 13 * Software is furnished to do so, subject to the following conditions: 14 * 15 * The above copyright notice and this permission notice shall be included 16 * in all copies or substantial portions 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 MERCHANTABILITY, 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 */ 25 26 27/** 28 * \file tnl/t_vb_program.c 29 * \brief Pipeline stage for executing vertex programs. 30 * \author Brian Paul, Keith Whitwell 31 */ 32 33 34#include "main/glheader.h" 35#include "main/colormac.h" 36#include "main/macros.h" 37#include "main/imports.h" 38#include "math/m_xform.h" 39#include "program/prog_instruction.h" 40#include "program/prog_statevars.h" 41#include "program/prog_execute.h" 42#include "swrast/s_context.h" 43 44#include "tnl/tnl.h" 45#include "tnl/t_context.h" 46#include "tnl/t_pipeline.h" 47 48 49#ifdef NAN_CHECK 50/** Check for NaNs and very large values */ 51static INLINE void 52check_float(float x) 53{ 54 assert(!IS_INF_OR_NAN(x)); 55 assert(1.0e-15 <= x && x <= 1.0e15); 56} 57#endif 58 59 60/*! 61 * Private storage for the vertex program pipeline stage. 62 */ 63struct vp_stage_data { 64 /** The results of running the vertex program go into these arrays. */ 65 GLvector4f results[VERT_RESULT_MAX]; 66 67 GLvector4f ndcCoords; /**< normalized device coords */ 68 GLubyte *clipmask; /**< clip flags */ 69 GLubyte ormask, andmask; /**< for clipping */ 70}; 71 72 73#define VP_STAGE_DATA(stage) ((struct vp_stage_data *)(stage->privatePtr)) 74 75 76static void 77userclip( GLcontext *ctx, 78 GLvector4f *clip, 79 GLubyte *clipmask, 80 GLubyte *clipormask, 81 GLubyte *clipandmask ) 82{ 83 GLuint p; 84 85 for (p = 0; p < ctx->Const.MaxClipPlanes; p++) { 86 if (ctx->Transform.ClipPlanesEnabled & (1 << p)) { 87 GLuint nr, i; 88 const GLfloat a = ctx->Transform._ClipUserPlane[p][0]; 89 const GLfloat b = ctx->Transform._ClipUserPlane[p][1]; 90 const GLfloat c = ctx->Transform._ClipUserPlane[p][2]; 91 const GLfloat d = ctx->Transform._ClipUserPlane[p][3]; 92 GLfloat *coord = (GLfloat *)clip->data; 93 GLuint stride = clip->stride; 94 GLuint count = clip->count; 95 96 for (nr = 0, i = 0 ; i < count ; i++) { 97 GLfloat dp = (coord[0] * a + 98 coord[1] * b + 99 coord[2] * c + 100 coord[3] * d); 101 102 if (dp < 0) { 103 nr++; 104 clipmask[i] |= CLIP_USER_BIT; 105 } 106 107 STRIDE_F(coord, stride); 108 } 109 110 if (nr > 0) { 111 *clipormask |= CLIP_USER_BIT; 112 if (nr == count) { 113 *clipandmask |= CLIP_USER_BIT; 114 return; 115 } 116 } 117 } 118 } 119} 120 121 122static GLboolean 123do_ndc_cliptest(GLcontext *ctx, struct vp_stage_data *store) 124{ 125 TNLcontext *tnl = TNL_CONTEXT(ctx); 126 struct vertex_buffer *VB = &tnl->vb; 127 /* Cliptest and perspective divide. Clip functions must clear 128 * the clipmask. 129 */ 130 store->ormask = 0; 131 store->andmask = CLIP_FRUSTUM_BITS; 132 133 tnl_clip_prepare(ctx); 134 135 if (tnl->NeedNdcCoords) { 136 VB->NdcPtr = 137 _mesa_clip_tab[VB->ClipPtr->size]( VB->ClipPtr, 138 &store->ndcCoords, 139 store->clipmask, 140 &store->ormask, 141 &store->andmask, 142 !ctx->Transform.DepthClamp ); 143 } 144 else { 145 VB->NdcPtr = NULL; 146 _mesa_clip_np_tab[VB->ClipPtr->size]( VB->ClipPtr, 147 NULL, 148 store->clipmask, 149 &store->ormask, 150 &store->andmask, 151 !ctx->Transform.DepthClamp ); 152 } 153 154 if (store->andmask) { 155 /* All vertices are outside the frustum */ 156 return GL_FALSE; 157 } 158 159 /* Test userclip planes. This contributes to VB->ClipMask. 160 */ 161 /** XXX NEW_SLANG _Enabled ??? */ 162 if (ctx->Transform.ClipPlanesEnabled && (!ctx->VertexProgram._Enabled || 163 ctx->VertexProgram.Current->IsPositionInvariant)) { 164 userclip( ctx, 165 VB->ClipPtr, 166 store->clipmask, 167 &store->ormask, 168 &store->andmask ); 169 170 if (store->andmask) { 171 return GL_FALSE; 172 } 173 } 174 175 VB->ClipAndMask = store->andmask; 176 VB->ClipOrMask = store->ormask; 177 VB->ClipMask = store->clipmask; 178 179 return GL_TRUE; 180} 181 182 183/** 184 * XXX the texture sampling code in this module is a bit of a hack. 185 * The texture sampling code is in swrast, though it doesn't have any 186 * real dependencies on the rest of swrast. It should probably be 187 * moved into main/ someday. 188 */ 189static void 190vp_fetch_texel(GLcontext *ctx, const GLfloat texcoord[4], GLfloat lambda, 191 GLuint unit, GLfloat color[4]) 192{ 193 SWcontext *swrast = SWRAST_CONTEXT(ctx); 194 195 /* XXX use a float-valued TextureSample routine here!!! */ 196 swrast->TextureSample[unit](ctx, ctx->Texture.Unit[unit]._Current, 197 1, (const GLfloat (*)[4]) texcoord, 198 &lambda, (GLfloat (*)[4]) color); 199} 200 201 202/** 203 * Called via ctx->Driver.ProgramStringNotify() after a new vertex program 204 * string has been parsed. 205 */ 206GLboolean 207_tnl_program_string(GLcontext *ctx, GLenum target, struct gl_program *program) 208{ 209 /* No-op. 210 * If we had derived anything from the program that was private to this 211 * stage we'd recompute/validate it here. 212 */ 213 return GL_TRUE; 214} 215 216 217/** 218 * Initialize virtual machine state prior to executing vertex program. 219 */ 220static void 221init_machine(GLcontext *ctx, struct gl_program_machine *machine) 222{ 223 /* Input registers get initialized from the current vertex attribs */ 224 memcpy(machine->VertAttribs, ctx->Current.Attrib, 225 MAX_VERTEX_GENERIC_ATTRIBS * 4 * sizeof(GLfloat)); 226 227 if (ctx->VertexProgram._Current->IsNVProgram) { 228 GLuint i; 229 /* Output/result regs are initialized to [0,0,0,1] */ 230 for (i = 0; i < MAX_NV_VERTEX_PROGRAM_OUTPUTS; i++) { 231 ASSIGN_4V(machine->Outputs[i], 0.0F, 0.0F, 0.0F, 1.0F); 232 } 233 /* Temp regs are initialized to [0,0,0,0] */ 234 for (i = 0; i < MAX_NV_VERTEX_PROGRAM_TEMPS; i++) { 235 ASSIGN_4V(machine->Temporaries[i], 0.0F, 0.0F, 0.0F, 0.0F); 236 } 237 for (i = 0; i < MAX_VERTEX_PROGRAM_ADDRESS_REGS; i++) { 238 ASSIGN_4V(machine->AddressReg[i], 0, 0, 0, 0); 239 } 240 } 241 242 machine->NumDeriv = 0; 243 244 /* init condition codes */ 245 machine->CondCodes[0] = COND_EQ; 246 machine->CondCodes[1] = COND_EQ; 247 machine->CondCodes[2] = COND_EQ; 248 machine->CondCodes[3] = COND_EQ; 249 250 /* init call stack */ 251 machine->StackDepth = 0; 252 253 machine->FetchTexelLod = vp_fetch_texel; 254 machine->FetchTexelDeriv = NULL; /* not used by vertex programs */ 255 256 machine->Samplers = ctx->VertexProgram._Current->Base.SamplerUnits; 257} 258 259 260/** 261 * Map the texture images which the vertex program will access (if any). 262 */ 263static void 264map_textures(GLcontext *ctx, const struct gl_vertex_program *vp) 265{ 266 GLuint u; 267 268 if (!ctx->Driver.MapTexture) 269 return; 270 271 for (u = 0; u < ctx->Const.MaxVertexTextureImageUnits; u++) { 272 if (vp->Base.TexturesUsed[u]) { 273 /* Note: _Current *should* correspond to the target indicated 274 * in TexturesUsed[u]. 275 */ 276 ctx->Driver.MapTexture(ctx, ctx->Texture.Unit[u]._Current); 277 } 278 } 279} 280 281 282/** 283 * Unmap the texture images which were used by the vertex program (if any). 284 */ 285static void 286unmap_textures(GLcontext *ctx, const struct gl_vertex_program *vp) 287{ 288 GLuint u; 289 290 if (!ctx->Driver.MapTexture) 291 return; 292 293 for (u = 0; u < ctx->Const.MaxVertexTextureImageUnits; u++) { 294 if (vp->Base.TexturesUsed[u]) { 295 /* Note: _Current *should* correspond to the target indicated 296 * in TexturesUsed[u]. 297 */ 298 ctx->Driver.UnmapTexture(ctx, ctx->Texture.Unit[u]._Current); 299 } 300 } 301} 302 303 304/** 305 * This function executes vertex programs 306 */ 307static GLboolean 308run_vp( GLcontext *ctx, struct tnl_pipeline_stage *stage ) 309{ 310 TNLcontext *tnl = TNL_CONTEXT(ctx); 311 struct vp_stage_data *store = VP_STAGE_DATA(stage); 312 struct vertex_buffer *VB = &tnl->vb; 313 struct gl_vertex_program *program = ctx->VertexProgram._Current; 314 struct gl_program_machine machine; 315 GLuint outputs[VERT_RESULT_MAX], numOutputs; 316 GLuint i, j; 317 318 if (!program) 319 return GL_TRUE; 320 321 if (program->IsNVProgram) { 322 _mesa_load_tracked_matrices(ctx); 323 } 324 else { 325 /* ARB program or vertex shader */ 326 _mesa_load_state_parameters(ctx, program->Base.Parameters); 327 } 328 329 /* make list of outputs to save some time below */ 330 numOutputs = 0; 331 for (i = 0; i < VERT_RESULT_MAX; i++) { 332 if (program->Base.OutputsWritten & BITFIELD64_BIT(i)) { 333 outputs[numOutputs++] = i; 334 } 335 } 336 337 map_textures(ctx, program); 338 339 for (i = 0; i < VB->Count; i++) { 340 GLuint attr; 341 342 init_machine(ctx, &machine); 343 344#if 0 345 printf("Input %d: %f, %f, %f, %f\n", i, 346 VB->AttribPtr[0]->data[i][0], 347 VB->AttribPtr[0]->data[i][1], 348 VB->AttribPtr[0]->data[i][2], 349 VB->AttribPtr[0]->data[i][3]); 350 printf(" color: %f, %f, %f, %f\n", 351 VB->AttribPtr[3]->data[i][0], 352 VB->AttribPtr[3]->data[i][1], 353 VB->AttribPtr[3]->data[i][2], 354 VB->AttribPtr[3]->data[i][3]); 355 printf(" normal: %f, %f, %f, %f\n", 356 VB->AttribPtr[2]->data[i][0], 357 VB->AttribPtr[2]->data[i][1], 358 VB->AttribPtr[2]->data[i][2], 359 VB->AttribPtr[2]->data[i][3]); 360#endif 361 362 /* the vertex array case */ 363 for (attr = 0; attr < VERT_ATTRIB_MAX; attr++) { 364 if (program->Base.InputsRead & (1 << attr)) { 365 const GLubyte *ptr = (const GLubyte*) VB->AttribPtr[attr]->data; 366 const GLuint size = VB->AttribPtr[attr]->size; 367 const GLuint stride = VB->AttribPtr[attr]->stride; 368 const GLfloat *data = (GLfloat *) (ptr + stride * i); 369#ifdef NAN_CHECK 370 check_float(data[0]); 371 check_float(data[1]); 372 check_float(data[2]); 373 check_float(data[3]); 374#endif 375 COPY_CLEAN_4V(machine.VertAttribs[attr], size, data); 376 } 377 } 378 379 /* execute the program */ 380 _mesa_execute_program(ctx, &program->Base, &machine); 381 382 /* copy the output registers into the VB->attribs arrays */ 383 for (j = 0; j < numOutputs; j++) { 384 const GLuint attr = outputs[j]; 385#ifdef NAN_CHECK 386 check_float(machine.Outputs[attr][0]); 387 check_float(machine.Outputs[attr][1]); 388 check_float(machine.Outputs[attr][2]); 389 check_float(machine.Outputs[attr][3]); 390#endif 391 COPY_4V(store->results[attr].data[i], machine.Outputs[attr]); 392 } 393 394 /* FOGC is a special case. Fragment shader expects (f,0,0,1) */ 395 if (program->Base.OutputsWritten & BITFIELD64_BIT(VERT_RESULT_FOGC)) { 396 store->results[VERT_RESULT_FOGC].data[i][1] = 0.0; 397 store->results[VERT_RESULT_FOGC].data[i][2] = 0.0; 398 store->results[VERT_RESULT_FOGC].data[i][3] = 1.0; 399 } 400#ifdef NAN_CHECK 401 ASSERT(machine.Outputs[0][3] != 0.0F); 402#endif 403#if 0 404 printf("HPOS: %f %f %f %f\n", 405 machine.Outputs[0][0], 406 machine.Outputs[0][1], 407 machine.Outputs[0][2], 408 machine.Outputs[0][3]); 409#endif 410 } 411 412 unmap_textures(ctx, program); 413 414 /* Fixup fog and point size results if needed */ 415 if (program->IsNVProgram) { 416 if (ctx->Fog.Enabled && 417 (program->Base.OutputsWritten & BITFIELD64_BIT(VERT_RESULT_FOGC)) == 0) { 418 for (i = 0; i < VB->Count; i++) { 419 store->results[VERT_RESULT_FOGC].data[i][0] = 1.0; 420 } 421 } 422 423 if (ctx->VertexProgram.PointSizeEnabled && 424 (program->Base.OutputsWritten & BITFIELD64_BIT(VERT_RESULT_PSIZ)) == 0) { 425 for (i = 0; i < VB->Count; i++) { 426 store->results[VERT_RESULT_PSIZ].data[i][0] = ctx->Point.Size; 427 } 428 } 429 } 430 431 if (program->IsPositionInvariant) { 432 /* We need the exact same transform as in the fixed function path here 433 * to guarantee invariance, depending on compiler optimization flags 434 * results could be different otherwise. 435 */ 436 VB->ClipPtr = TransformRaw( &store->results[0], 437 &ctx->_ModelProjectMatrix, 438 VB->AttribPtr[0] ); 439 440 /* Drivers expect this to be clean to element 4... 441 */ 442 switch (VB->ClipPtr->size) { 443 case 1: 444 /* impossible */ 445 case 2: 446 _mesa_vector4f_clean_elem( VB->ClipPtr, VB->Count, 2 ); 447 /* fall-through */ 448 case 3: 449 _mesa_vector4f_clean_elem( VB->ClipPtr, VB->Count, 3 ); 450 /* fall-through */ 451 case 4: 452 break; 453 } 454 } 455 else { 456 /* Setup the VB pointers so that the next pipeline stages get 457 * their data from the right place (the program output arrays). 458 */ 459 VB->ClipPtr = &store->results[VERT_RESULT_HPOS]; 460 VB->ClipPtr->size = 4; 461 VB->ClipPtr->count = VB->Count; 462 } 463 464 VB->AttribPtr[VERT_ATTRIB_COLOR0] = &store->results[VERT_RESULT_COL0]; 465 VB->AttribPtr[VERT_ATTRIB_COLOR1] = &store->results[VERT_RESULT_COL1]; 466 VB->AttribPtr[VERT_ATTRIB_FOG] = &store->results[VERT_RESULT_FOGC]; 467 VB->AttribPtr[_TNL_ATTRIB_POINTSIZE] = &store->results[VERT_RESULT_PSIZ]; 468 VB->BackfaceColorPtr = &store->results[VERT_RESULT_BFC0]; 469 VB->BackfaceSecondaryColorPtr = &store->results[VERT_RESULT_BFC1]; 470 471 for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++) { 472 VB->AttribPtr[_TNL_ATTRIB_TEX0 + i] 473 = &store->results[VERT_RESULT_TEX0 + i]; 474 } 475 476 for (i = 0; i < ctx->Const.MaxVarying; i++) { 477 if (program->Base.OutputsWritten & BITFIELD64_BIT(VERT_RESULT_VAR0 + i)) { 478 /* Note: varying results get put into the generic attributes */ 479 VB->AttribPtr[VERT_ATTRIB_GENERIC0+i] 480 = &store->results[VERT_RESULT_VAR0 + i]; 481 } 482 } 483 484 485 /* Perform NDC and cliptest operations: 486 */ 487 return do_ndc_cliptest(ctx, store); 488} 489 490 491/** 492 * Called the first time stage->run is called. In effect, don't 493 * allocate data until the first time the stage is run. 494 */ 495static GLboolean 496init_vp(GLcontext *ctx, struct tnl_pipeline_stage *stage) 497{ 498 TNLcontext *tnl = TNL_CONTEXT(ctx); 499 struct vertex_buffer *VB = &(tnl->vb); 500 struct vp_stage_data *store; 501 const GLuint size = VB->Size; 502 GLuint i; 503 504 stage->privatePtr = MALLOC(sizeof(*store)); 505 store = VP_STAGE_DATA(stage); 506 if (!store) 507 return GL_FALSE; 508 509 /* Allocate arrays of vertex output values */ 510 for (i = 0; i < VERT_RESULT_MAX; i++) { 511 _mesa_vector4f_alloc( &store->results[i], 0, size, 32 ); 512 store->results[i].size = 4; 513 } 514 515 /* a few other misc allocations */ 516 _mesa_vector4f_alloc( &store->ndcCoords, 0, size, 32 ); 517 store->clipmask = (GLubyte *) _mesa_align_malloc(sizeof(GLubyte)*size, 32 ); 518 519 return GL_TRUE; 520} 521 522 523/** 524 * Destructor for this pipeline stage. 525 */ 526static void 527dtr(struct tnl_pipeline_stage *stage) 528{ 529 struct vp_stage_data *store = VP_STAGE_DATA(stage); 530 531 if (store) { 532 GLuint i; 533 534 /* free the vertex program result arrays */ 535 for (i = 0; i < VERT_RESULT_MAX; i++) 536 _mesa_vector4f_free( &store->results[i] ); 537 538 /* free misc arrays */ 539 _mesa_vector4f_free( &store->ndcCoords ); 540 _mesa_align_free( store->clipmask ); 541 542 FREE( store ); 543 stage->privatePtr = NULL; 544 } 545} 546 547 548static void 549validate_vp_stage(GLcontext *ctx, struct tnl_pipeline_stage *stage) 550{ 551 if (ctx->VertexProgram._Current) { 552 _swrast_update_texture_samplers(ctx); 553 } 554} 555 556 557 558/** 559 * Public description of this pipeline stage. 560 */ 561const struct tnl_pipeline_stage _tnl_vertex_program_stage = 562{ 563 "vertex-program", 564 NULL, /* private_data */ 565 init_vp, /* create */ 566 dtr, /* destroy */ 567 validate_vp_stage, /* validate */ 568 run_vp /* run -- initially set to ctr */ 569}; 570