t_vb_texgen.c revision 9705cff2033f1771a39ac3bb78eb5fcea522218a
1/* 2 * Mesa 3-D graphics library 3 * Version: 6.5 4 * 5 * Copyright (C) 1999-2006 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 * Brian Paul 26 * Keith Whitwell <keith@tungstengraphics.com> 27 */ 28 29/* 30 * Regarding GL_NV_texgen_reflection: 31 * 32 * Portions of this software may use or implement intellectual 33 * property owned and licensed by NVIDIA Corporation. NVIDIA disclaims 34 * any and all warranties with respect to such intellectual property, 35 * including any use thereof or modifications thereto. 36 */ 37 38#include "main/glheader.h" 39#include "main/colormac.h" 40#include "main/context.h" 41#include "main/macros.h" 42#include "main/imports.h" 43#include "main/mtypes.h" 44 45#include "math/m_xform.h" 46 47#include "t_context.h" 48#include "t_pipeline.h" 49 50 51/*********************************************************************** 52 * Automatic texture coordinate generation (texgen) code. 53 */ 54 55 56struct texgen_stage_data; 57 58typedef void (*texgen_func)( GLcontext *ctx, 59 struct texgen_stage_data *store, 60 GLuint unit); 61 62 63struct texgen_stage_data { 64 65 /* Per-texunit derived state. 66 */ 67 GLuint TexgenSize[MAX_TEXTURE_COORD_UNITS]; 68 texgen_func TexgenFunc[MAX_TEXTURE_COORD_UNITS]; 69 70 /* Temporary values used in texgen. 71 */ 72 GLfloat (*tmp_f)[3]; 73 GLfloat *tmp_m; 74 75 /* Buffered outputs of the stage. 76 */ 77 GLvector4f texcoord[MAX_TEXTURE_COORD_UNITS]; 78}; 79 80 81#define TEXGEN_STAGE_DATA(stage) ((struct texgen_stage_data *)stage->privatePtr) 82 83 84 85static GLuint all_bits[5] = { 86 0, 87 VEC_SIZE_1, 88 VEC_SIZE_2, 89 VEC_SIZE_3, 90 VEC_SIZE_4, 91}; 92 93#define VEC_SIZE_FLAGS (VEC_SIZE_1|VEC_SIZE_2|VEC_SIZE_3|VEC_SIZE_4) 94 95#define TEXGEN_NEED_M (TEXGEN_SPHERE_MAP) 96#define TEXGEN_NEED_F (TEXGEN_SPHERE_MAP | \ 97 TEXGEN_REFLECTION_MAP_NV) 98 99 100 101static void build_m3( GLfloat f[][3], GLfloat m[], 102 const GLvector4f *normal, 103 const GLvector4f *eye ) 104{ 105 GLuint stride = eye->stride; 106 GLfloat *coord = (GLfloat *)eye->start; 107 GLuint count = eye->count; 108 const GLfloat *norm = normal->start; 109 GLuint i; 110 111 for (i=0;i<count;i++,STRIDE_F(coord,stride),STRIDE_F(norm,normal->stride)) { 112 GLfloat u[3], two_nu, fx, fy, fz; 113 COPY_3V( u, coord ); 114 NORMALIZE_3FV( u ); 115 two_nu = 2.0F * DOT3(norm,u); 116 fx = f[i][0] = u[0] - norm[0] * two_nu; 117 fy = f[i][1] = u[1] - norm[1] * two_nu; 118 fz = f[i][2] = u[2] - norm[2] * two_nu; 119 m[i] = fx * fx + fy * fy + (fz + 1.0F) * (fz + 1.0F); 120 if (m[i] != 0.0F) { 121 m[i] = 0.5F * _mesa_inv_sqrtf(m[i]); 122 } 123 } 124} 125 126 127 128static void build_m2( GLfloat f[][3], GLfloat m[], 129 const GLvector4f *normal, 130 const GLvector4f *eye ) 131{ 132 GLuint stride = eye->stride; 133 GLfloat *coord = eye->start; 134 GLuint count = eye->count; 135 136 GLfloat *norm = normal->start; 137 GLuint i; 138 139 for (i=0;i<count;i++,STRIDE_F(coord,stride),STRIDE_F(norm,normal->stride)) { 140 GLfloat u[3], two_nu, fx, fy, fz; 141 COPY_2V( u, coord ); 142 u[2] = 0; 143 NORMALIZE_3FV( u ); 144 two_nu = 2.0F * DOT3(norm,u); 145 fx = f[i][0] = u[0] - norm[0] * two_nu; 146 fy = f[i][1] = u[1] - norm[1] * two_nu; 147 fz = f[i][2] = u[2] - norm[2] * two_nu; 148 m[i] = fx * fx + fy * fy + (fz + 1.0F) * (fz + 1.0F); 149 if (m[i] != 0.0F) { 150 m[i] = 0.5F * _mesa_inv_sqrtf(m[i]); 151 } 152 } 153} 154 155 156 157typedef void (*build_m_func)( GLfloat f[][3], 158 GLfloat m[], 159 const GLvector4f *normal, 160 const GLvector4f *eye ); 161 162 163static build_m_func build_m_tab[5] = { 164 NULL, 165 NULL, 166 build_m2, 167 build_m3, 168 build_m3 169}; 170 171 172/* This is unusual in that we respect the stride of the output vector 173 * (f). This allows us to pass in either a texcoord vector4f, or a 174 * temporary vector3f. 175 */ 176static void build_f3( GLfloat *f, 177 GLuint fstride, 178 const GLvector4f *normal, 179 const GLvector4f *eye ) 180{ 181 GLuint stride = eye->stride; 182 GLfloat *coord = eye->start; 183 GLuint count = eye->count; 184 185 GLfloat *norm = normal->start; 186 GLuint i; 187 188 for (i=0;i<count;i++) { 189 GLfloat u[3], two_nu; 190 COPY_3V( u, coord ); 191 NORMALIZE_3FV( u ); 192 two_nu = 2.0F * DOT3(norm,u); 193 f[0] = u[0] - norm[0] * two_nu; 194 f[1] = u[1] - norm[1] * two_nu; 195 f[2] = u[2] - norm[2] * two_nu; 196 STRIDE_F(coord,stride); 197 STRIDE_F(f,fstride); 198 STRIDE_F(norm, normal->stride); 199 } 200} 201 202 203static void build_f2( GLfloat *f, 204 GLuint fstride, 205 const GLvector4f *normal, 206 const GLvector4f *eye ) 207{ 208 GLuint stride = eye->stride; 209 GLfloat *coord = eye->start; 210 GLuint count = eye->count; 211 GLfloat *norm = normal->start; 212 GLuint i; 213 214 for (i=0;i<count;i++) { 215 216 GLfloat u[3], two_nu; 217 COPY_2V( u, coord ); 218 u[2] = 0; 219 NORMALIZE_3FV( u ); 220 two_nu = 2.0F * DOT3(norm,u); 221 f[0] = u[0] - norm[0] * two_nu; 222 f[1] = u[1] - norm[1] * two_nu; 223 f[2] = u[2] - norm[2] * two_nu; 224 225 STRIDE_F(coord,stride); 226 STRIDE_F(f,fstride); 227 STRIDE_F(norm, normal->stride); 228 } 229} 230 231typedef void (*build_f_func)( GLfloat *f, 232 GLuint fstride, 233 const GLvector4f *normal_vec, 234 const GLvector4f *eye ); 235 236 237 238/* Just treat 4-vectors as 3-vectors. 239 */ 240static build_f_func build_f_tab[5] = { 241 NULL, 242 NULL, 243 build_f2, 244 build_f3, 245 build_f3 246}; 247 248 249 250/* Special case texgen functions. 251 */ 252static void texgen_reflection_map_nv( GLcontext *ctx, 253 struct texgen_stage_data *store, 254 GLuint unit ) 255{ 256 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb; 257 GLvector4f *in = VB->AttribPtr[VERT_ATTRIB_TEX0 + unit]; 258 GLvector4f *out = &store->texcoord[unit]; 259 260 build_f_tab[VB->EyePtr->size]( out->start, 261 out->stride, 262 VB->AttribPtr[_TNL_ATTRIB_NORMAL], 263 VB->EyePtr ); 264 265 out->flags |= (in->flags & VEC_SIZE_FLAGS) | VEC_SIZE_3; 266 out->count = VB->Count; 267 out->size = MAX2(in->size, 3); 268 if (in->size == 4) 269 _mesa_copy_tab[0x8]( out, in ); 270} 271 272 273 274static void texgen_normal_map_nv( GLcontext *ctx, 275 struct texgen_stage_data *store, 276 GLuint unit ) 277{ 278 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb; 279 GLvector4f *in = VB->AttribPtr[VERT_ATTRIB_TEX0 + unit]; 280 GLvector4f *out = &store->texcoord[unit]; 281 GLvector4f *normal = VB->AttribPtr[_TNL_ATTRIB_NORMAL]; 282 GLfloat (*texcoord)[4] = (GLfloat (*)[4])out->start; 283 GLuint count = VB->Count; 284 GLuint i; 285 const GLfloat *norm = normal->start; 286 287 for (i=0;i<count;i++, STRIDE_F(norm, normal->stride)) { 288 texcoord[i][0] = norm[0]; 289 texcoord[i][1] = norm[1]; 290 texcoord[i][2] = norm[2]; 291 } 292 293 294 out->flags |= (in->flags & VEC_SIZE_FLAGS) | VEC_SIZE_3; 295 out->count = count; 296 out->size = MAX2(in->size, 3); 297 if (in->size == 4) 298 _mesa_copy_tab[0x8]( out, in ); 299} 300 301 302static void texgen_sphere_map( GLcontext *ctx, 303 struct texgen_stage_data *store, 304 GLuint unit ) 305{ 306 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb; 307 GLvector4f *in = VB->AttribPtr[VERT_ATTRIB_TEX0 + unit]; 308 GLvector4f *out = &store->texcoord[unit]; 309 GLfloat (*texcoord)[4] = (GLfloat (*)[4]) out->start; 310 GLuint count = VB->Count; 311 GLuint i; 312 GLfloat (*f)[3] = store->tmp_f; 313 GLfloat *m = store->tmp_m; 314 315 (build_m_tab[VB->EyePtr->size])( store->tmp_f, 316 store->tmp_m, 317 VB->AttribPtr[_TNL_ATTRIB_NORMAL], 318 VB->EyePtr ); 319 320 out->size = MAX2(in->size,2); 321 322 for (i=0;i<count;i++) { 323 texcoord[i][0] = f[i][0] * m[i] + 0.5F; 324 texcoord[i][1] = f[i][1] * m[i] + 0.5F; 325 } 326 327 out->count = count; 328 out->flags |= (in->flags & VEC_SIZE_FLAGS) | VEC_SIZE_2; 329 if (in->size > 2) 330 _mesa_copy_tab[all_bits[in->size] & ~0x3]( out, in ); 331} 332 333 334 335static void texgen( GLcontext *ctx, 336 struct texgen_stage_data *store, 337 GLuint unit ) 338{ 339 TNLcontext *tnl = TNL_CONTEXT(ctx); 340 struct vertex_buffer *VB = &tnl->vb; 341 GLvector4f *in = VB->AttribPtr[VERT_ATTRIB_TEX0 + unit]; 342 GLvector4f *out = &store->texcoord[unit]; 343 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit]; 344 const GLvector4f *obj = VB->ObjPtr; 345 const GLvector4f *eye = VB->EyePtr; 346 const GLvector4f *normal = VB->AttribPtr[_TNL_ATTRIB_NORMAL]; 347 const GLfloat *m = store->tmp_m; 348 const GLuint count = VB->Count; 349 GLfloat (*texcoord)[4] = (GLfloat (*)[4])out->data; 350 GLfloat (*f)[3] = store->tmp_f; 351 GLuint copy; 352 353 if (texUnit->_GenFlags & TEXGEN_NEED_M) { 354 build_m_tab[eye->size]( store->tmp_f, store->tmp_m, normal, eye ); 355 } else if (texUnit->_GenFlags & TEXGEN_NEED_F) { 356 build_f_tab[eye->size]( (GLfloat *)store->tmp_f, 3, normal, eye ); 357 } 358 359 360 out->size = MAX2(in->size, store->TexgenSize[unit]); 361 out->flags |= (in->flags & VEC_SIZE_FLAGS) | texUnit->TexGenEnabled; 362 out->count = count; 363 364 copy = (all_bits[in->size] & ~texUnit->TexGenEnabled); 365 if (copy) 366 _mesa_copy_tab[copy]( out, in ); 367 368 if (texUnit->TexGenEnabled & S_BIT) { 369 GLuint i; 370 switch (texUnit->GenS.Mode) { 371 case GL_OBJECT_LINEAR: 372 _mesa_dotprod_tab[obj->size]( (GLfloat *)out->data, 373 sizeof(out->data[0]), obj, 374 texUnit->GenS.ObjectPlane ); 375 break; 376 case GL_EYE_LINEAR: 377 _mesa_dotprod_tab[eye->size]( (GLfloat *)out->data, 378 sizeof(out->data[0]), eye, 379 texUnit->GenS.EyePlane ); 380 break; 381 case GL_SPHERE_MAP: 382 for (i = 0; i < count; i++) 383 texcoord[i][0] = f[i][0] * m[i] + 0.5F; 384 break; 385 case GL_REFLECTION_MAP_NV: 386 for (i=0;i<count;i++) 387 texcoord[i][0] = f[i][0]; 388 break; 389 case GL_NORMAL_MAP_NV: { 390 const GLfloat *norm = normal->start; 391 for (i=0;i<count;i++, STRIDE_F(norm, normal->stride)) { 392 texcoord[i][0] = norm[0]; 393 } 394 break; 395 } 396 default: 397 _mesa_problem(ctx, "Bad S texgen"); 398 } 399 } 400 401 if (texUnit->TexGenEnabled & T_BIT) { 402 GLuint i; 403 switch (texUnit->GenT.Mode) { 404 case GL_OBJECT_LINEAR: 405 _mesa_dotprod_tab[obj->size]( &(out->data[0][1]), 406 sizeof(out->data[0]), obj, 407 texUnit->GenT.ObjectPlane ); 408 break; 409 case GL_EYE_LINEAR: 410 _mesa_dotprod_tab[eye->size]( &(out->data[0][1]), 411 sizeof(out->data[0]), eye, 412 texUnit->GenT.EyePlane ); 413 break; 414 case GL_SPHERE_MAP: 415 for (i = 0; i < count; i++) 416 texcoord[i][1] = f[i][1] * m[i] + 0.5F; 417 break; 418 case GL_REFLECTION_MAP_NV: 419 for (i=0;i<count;i++) 420 texcoord[i][1] = f[i][1]; 421 break; 422 case GL_NORMAL_MAP_NV: { 423 const GLfloat *norm = normal->start; 424 for (i=0;i<count;i++, STRIDE_F(norm, normal->stride)) { 425 texcoord[i][1] = norm[1]; 426 } 427 break; 428 } 429 default: 430 _mesa_problem(ctx, "Bad T texgen"); 431 } 432 } 433 434 if (texUnit->TexGenEnabled & R_BIT) { 435 GLuint i; 436 switch (texUnit->GenR.Mode) { 437 case GL_OBJECT_LINEAR: 438 _mesa_dotprod_tab[obj->size]( &(out->data[0][2]), 439 sizeof(out->data[0]), obj, 440 texUnit->GenR.ObjectPlane ); 441 break; 442 case GL_EYE_LINEAR: 443 _mesa_dotprod_tab[eye->size]( &(out->data[0][2]), 444 sizeof(out->data[0]), eye, 445 texUnit->GenR.EyePlane ); 446 break; 447 case GL_REFLECTION_MAP_NV: 448 for (i=0;i<count;i++) 449 texcoord[i][2] = f[i][2]; 450 break; 451 case GL_NORMAL_MAP_NV: { 452 const GLfloat *norm = normal->start; 453 for (i=0;i<count;i++,STRIDE_F(norm, normal->stride)) { 454 texcoord[i][2] = norm[2]; 455 } 456 break; 457 } 458 default: 459 _mesa_problem(ctx, "Bad R texgen"); 460 } 461 } 462 463 if (texUnit->TexGenEnabled & Q_BIT) { 464 switch (texUnit->GenQ.Mode) { 465 case GL_OBJECT_LINEAR: 466 _mesa_dotprod_tab[obj->size]( &(out->data[0][3]), 467 sizeof(out->data[0]), obj, 468 texUnit->GenQ.ObjectPlane ); 469 break; 470 case GL_EYE_LINEAR: 471 _mesa_dotprod_tab[eye->size]( &(out->data[0][3]), 472 sizeof(out->data[0]), eye, 473 texUnit->GenQ.EyePlane ); 474 break; 475 default: 476 _mesa_problem(ctx, "Bad Q texgen"); 477 } 478 } 479} 480 481 482 483 484static GLboolean run_texgen_stage( GLcontext *ctx, 485 struct tnl_pipeline_stage *stage ) 486{ 487 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb; 488 struct texgen_stage_data *store = TEXGEN_STAGE_DATA(stage); 489 GLuint i; 490 491 if (!ctx->Texture._TexGenEnabled || ctx->VertexProgram._Current) 492 return GL_TRUE; 493 494 for (i = 0 ; i < ctx->Const.MaxTextureCoordUnits ; i++) { 495 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i]; 496 497 if (texUnit->TexGenEnabled) { 498 499 store->TexgenFunc[i]( ctx, store, i ); 500 501 VB->TexCoordPtr[i] = 502 VB->AttribPtr[VERT_ATTRIB_TEX0 + i] = &store->texcoord[i]; 503 } 504 } 505 506 return GL_TRUE; 507} 508 509 510static void validate_texgen_stage( GLcontext *ctx, 511 struct tnl_pipeline_stage *stage ) 512{ 513 struct texgen_stage_data *store = TEXGEN_STAGE_DATA(stage); 514 GLuint i; 515 516 if (!ctx->Texture._TexGenEnabled || ctx->VertexProgram._Current) 517 return; 518 519 for (i = 0 ; i < ctx->Const.MaxTextureCoordUnits ; i++) { 520 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i]; 521 522 if (texUnit->TexGenEnabled) { 523 GLuint sz; 524 525 if (texUnit->TexGenEnabled & Q_BIT) 526 sz = 4; 527 else if (texUnit->TexGenEnabled & R_BIT) 528 sz = 3; 529 else if (texUnit->TexGenEnabled & T_BIT) 530 sz = 2; 531 else 532 sz = 1; 533 534 store->TexgenSize[i] = sz; 535 store->TexgenFunc[i] = texgen; /* general solution */ 536 537 /* look for special texgen cases */ 538 if (texUnit->TexGenEnabled == (S_BIT|T_BIT|R_BIT)) { 539 if (texUnit->_GenFlags == TEXGEN_REFLECTION_MAP_NV) { 540 store->TexgenFunc[i] = texgen_reflection_map_nv; 541 } 542 else if (texUnit->_GenFlags == TEXGEN_NORMAL_MAP_NV) { 543 store->TexgenFunc[i] = texgen_normal_map_nv; 544 } 545 } 546 else if (texUnit->TexGenEnabled == (S_BIT|T_BIT) && 547 texUnit->_GenFlags == TEXGEN_SPHERE_MAP) { 548 store->TexgenFunc[i] = texgen_sphere_map; 549 } 550 } 551 } 552} 553 554 555 556 557 558/* Called the first time stage->run() is invoked. 559 */ 560static GLboolean alloc_texgen_data( GLcontext *ctx, 561 struct tnl_pipeline_stage *stage ) 562{ 563 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb; 564 struct texgen_stage_data *store; 565 GLuint i; 566 567 stage->privatePtr = CALLOC(sizeof(*store)); 568 store = TEXGEN_STAGE_DATA(stage); 569 if (!store) 570 return GL_FALSE; 571 572 for (i = 0 ; i < ctx->Const.MaxTextureCoordUnits ; i++) 573 _mesa_vector4f_alloc( &store->texcoord[i], 0, VB->Size, 32 ); 574 575 store->tmp_f = (GLfloat (*)[3]) MALLOC(VB->Size * sizeof(GLfloat) * 3); 576 store->tmp_m = (GLfloat *) MALLOC(VB->Size * sizeof(GLfloat)); 577 578 return GL_TRUE; 579} 580 581 582static void free_texgen_data( struct tnl_pipeline_stage *stage ) 583 584{ 585 struct texgen_stage_data *store = TEXGEN_STAGE_DATA(stage); 586 GLuint i; 587 588 if (store) { 589 for (i = 0 ; i < MAX_TEXTURE_COORD_UNITS ; i++) 590 if (store->texcoord[i].data) 591 _mesa_vector4f_free( &store->texcoord[i] ); 592 593 594 if (store->tmp_f) FREE( store->tmp_f ); 595 if (store->tmp_m) FREE( store->tmp_m ); 596 FREE( store ); 597 stage->privatePtr = NULL; 598 } 599} 600 601 602 603const struct tnl_pipeline_stage _tnl_texgen_stage = 604{ 605 "texgen", /* name */ 606 NULL, /* private data */ 607 alloc_texgen_data, /* destructor */ 608 free_texgen_data, /* destructor */ 609 validate_texgen_stage, /* check */ 610 run_texgen_stage /* run -- initially set to alloc data */ 611}; 612