t_vp_build.c revision f6e7cfb60d3c4ceb2242cbc57c7e87c3c8e362fe
1/* 2 * Mesa 3-D graphics library 3 * Version: 6.3 4 * 5 * Copyright (C) 2005 Tungsten Graphics 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 * TUNGSTEN GRAPHICS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 21 * 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 * \file t_vp_build.c 28 * Create a vertex program to execute the current fixed function T&L pipeline. 29 * \author Keith Whitwell 30 */ 31 32 33#include "glheader.h" 34#include "macros.h" 35#include "enums.h" 36#include "t_context.h" 37#include "t_vp_build.h" 38 39#include "shader/program.h" 40#include "shader/nvvertprog.h" 41#include "shader/arbvertparse.h" 42 43struct state_key { 44 unsigned light_global_enabled:1; 45 unsigned light_local_viewer:1; 46 unsigned light_twoside:1; 47 unsigned light_color_material:1; 48 unsigned light_color_material_mask:12; 49 unsigned light_material_mask:12; 50 51 unsigned normalize:1; 52 unsigned rescale_normals:1; 53 unsigned fog_source_is_depth:1; 54 unsigned tnl_do_vertex_fog:1; 55 unsigned separate_specular:1; 56 unsigned fog_enabled:1; 57 unsigned fog_mode:2; 58 unsigned point_attenuated:1; 59 unsigned texture_enabled_global:1; 60 61 struct { 62 unsigned light_enabled:1; 63 unsigned light_eyepos3_is_zero:1; 64 unsigned light_spotcutoff_is_180:1; 65 unsigned light_attenuated:1; 66 unsigned texunit_really_enabled:1; 67 unsigned texmat_enabled:1; 68 unsigned texgen_enabled:4; 69 unsigned texgen_mode0:4; 70 unsigned texgen_mode1:4; 71 unsigned texgen_mode2:4; 72 unsigned texgen_mode3:4; 73 } unit[8]; 74}; 75 76 77 78#define FOG_LINEAR 0 79#define FOG_EXP 1 80#define FOG_EXP2 2 81#define FOG_UNKNOWN 3 82 83static GLuint translate_fog_mode( GLenum mode ) 84{ 85 switch (mode) { 86 case GL_LINEAR: return FOG_LINEAR; 87 case GL_EXP: return FOG_EXP; 88 case GL_EXP2: return FOG_EXP2; 89 default: return FOG_UNKNOWN; 90 } 91} 92 93#define TXG_NONE 0 94#define TXG_OBJ_LINEAR 1 95#define TXG_EYE_LINEAR 2 96#define TXG_SPHERE_MAP 3 97#define TXG_REFLECTION_MAP 4 98#define TXG_NORMAL_MAP 5 99 100static GLuint translate_texgen( GLboolean enabled, GLenum mode ) 101{ 102 if (!enabled) 103 return TXG_NONE; 104 105 switch (mode) { 106 case GL_OBJECT_LINEAR: return TXG_OBJ_LINEAR; 107 case GL_EYE_LINEAR: return TXG_EYE_LINEAR; 108 case GL_SPHERE_MAP: return TXG_SPHERE_MAP; 109 case GL_REFLECTION_MAP_NV: return TXG_REFLECTION_MAP; 110 case GL_NORMAL_MAP_NV: return TXG_NORMAL_MAP; 111 default: return TXG_NONE; 112 } 113} 114 115static struct state_key *make_state_key( GLcontext *ctx ) 116{ 117 TNLcontext *tnl = TNL_CONTEXT(ctx); 118 struct vertex_buffer *VB = &tnl->vb; 119 struct state_key *key = CALLOC_STRUCT(state_key); 120 GLuint i; 121 122 key->separate_specular = (ctx->Light.Model.ColorControl == 123 GL_SEPARATE_SPECULAR_COLOR); 124 125 if (ctx->Light.Enabled) { 126 key->light_global_enabled = 1; 127 128 if (ctx->Light.Model.LocalViewer) 129 key->light_local_viewer = 1; 130 131 if (ctx->Light.Model.TwoSide) 132 key->light_twoside = 1; 133 134 if (ctx->Light.ColorMaterialEnabled) { 135 key->light_color_material = 1; 136 key->light_color_material_mask = ctx->Light.ColorMaterialBitmask; 137 _mesa_printf("ColorMaterialBitmask %x / %x\n", ctx->Light.ColorMaterialBitmask, 138 key->light_color_material_mask); 139 } 140 141 for (i = _TNL_ATTRIB_MAT_FRONT_AMBIENT ; i < _TNL_ATTRIB_INDEX ; i++) 142 if (VB->AttribPtr[i]->stride) 143 key->light_material_mask |= 1<<(i-_TNL_ATTRIB_MAT_FRONT_AMBIENT); 144 145 for (i = 0; i < MAX_LIGHTS; i++) { 146 struct gl_light *light = &ctx->Light.Light[i]; 147 148 if (light->Enabled) { 149 key->unit[i].light_enabled = 1; 150 151 if (light->EyePosition[3] == 0.0) 152 key->unit[i].light_eyepos3_is_zero = 1; 153 154 if (light->SpotCutoff == 180.0) 155 key->unit[i].light_spotcutoff_is_180 = 1; 156 157 if (light->ConstantAttenuation != 1.0 || 158 light->LinearAttenuation != 1.0 || 159 light->QuadraticAttenuation != 1.0) 160 key->unit[i].light_attenuated = 1; 161 } 162 } 163 } 164 165 if (ctx->Transform.Normalize) 166 key->normalize = 1; 167 168 if (ctx->Transform.RescaleNormals) 169 key->rescale_normals = 1; 170 171 if (ctx->Fog.Enabled) 172 key->fog_enabled = 1; 173 174 if (key->fog_enabled) { 175 if (ctx->Fog.FogCoordinateSource == GL_FRAGMENT_DEPTH_EXT) 176 key->fog_source_is_depth = 1; 177 178 if (tnl->_DoVertexFog) 179 key->tnl_do_vertex_fog = 1; 180 181 key->fog_mode = translate_fog_mode(ctx->Fog.Mode); 182 } 183 184 if (ctx->Point._Attenuated) 185 key->point_attenuated = 1; 186 187 if (ctx->Texture._TexGenEnabled || 188 ctx->Texture._TexMatEnabled || 189 ctx->Texture._EnabledUnits) 190 key->texture_enabled_global = 1; 191 192 for (i = 0; i < MAX_TEXTURE_UNITS; i++) { 193 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i]; 194 195 if (texUnit->_ReallyEnabled) 196 key->unit[i].texunit_really_enabled = 1; 197 198 if (ctx->Texture._TexMatEnabled & ENABLE_TEXMAT(i)) 199 key->unit[i].texmat_enabled = 1; 200 201 if (texUnit->TexGenEnabled) { 202 key->unit[i].texgen_enabled = 1; 203 204 key->unit[i].texgen_mode0 = 205 translate_texgen( texUnit->TexGenEnabled & (1<<0), 206 texUnit->GenModeS ); 207 key->unit[i].texgen_mode1 = 208 translate_texgen( texUnit->TexGenEnabled & (1<<1), 209 texUnit->GenModeT ); 210 key->unit[i].texgen_mode2 = 211 translate_texgen( texUnit->TexGenEnabled & (1<<2), 212 texUnit->GenModeR ); 213 key->unit[i].texgen_mode3 = 214 translate_texgen( texUnit->TexGenEnabled & (1<<3), 215 texUnit->GenModeQ ); 216 } 217 } 218 219 return key; 220} 221 222 223 224/* Very useful debugging tool - produces annotated listing of 225 * generated program with line/function references for each 226 * instruction back into this file: 227 */ 228#define DISASSEM 0 229 230/* Should be tunable by the driver - do we want to do matrix 231 * multiplications with DP4's or with MUL/MAD's? SSE works better 232 * with the latter, drivers may differ. 233 */ 234#define PREFER_DP4 0 235 236#define MAX_INSN 200 237 238/* Use uregs to represent registers internally, translate to Mesa's 239 * expected formats on emit. 240 * 241 * NOTE: These are passed by value extensively in this file rather 242 * than as usual by pointer reference. If this disturbs you, try 243 * remembering they are just 32bits in size. 244 * 245 * GCC is smart enough to deal with these dword-sized structures in 246 * much the same way as if I had defined them as dwords and was using 247 * macros to access and set the fields. This is much nicer and easier 248 * to evolve. 249 */ 250struct ureg { 251 GLuint file:4; 252 GLuint idx:8; 253 GLuint negate:1; 254 GLuint swz:12; 255 GLuint pad:7; 256}; 257 258 259struct tnl_program { 260 const struct state_key *state; 261 struct vertex_program *program; 262 263 GLuint temp_in_use; 264 GLuint temp_reserved; 265 266 struct ureg eye_position; 267 struct ureg eye_position_normalized; 268 struct ureg eye_normal; 269 struct ureg identity; 270 271 GLuint materials; 272 GLuint color_materials; 273}; 274 275 276const static struct ureg undef = { 277 ~0, 278 ~0, 279 0, 280 0, 281 0 282}; 283 284/* Local shorthand: 285 */ 286#define X SWIZZLE_X 287#define Y SWIZZLE_Y 288#define Z SWIZZLE_Z 289#define W SWIZZLE_W 290 291 292/* Construct a ureg: 293 */ 294static struct ureg make_ureg(GLuint file, GLuint idx) 295{ 296 struct ureg reg; 297 reg.file = file; 298 reg.idx = idx; 299 reg.negate = 0; 300 reg.swz = SWIZZLE_NOOP; 301 reg.pad = 0; 302 return reg; 303} 304 305 306 307static struct ureg negate( struct ureg reg ) 308{ 309 reg.negate ^= 1; 310 return reg; 311} 312 313 314static struct ureg swizzle( struct ureg reg, int x, int y, int z, int w ) 315{ 316 reg.swz = MAKE_SWIZZLE4(GET_SWZ(reg.swz, x), 317 GET_SWZ(reg.swz, y), 318 GET_SWZ(reg.swz, z), 319 GET_SWZ(reg.swz, w)); 320 321 return reg; 322} 323 324static struct ureg swizzle1( struct ureg reg, int x ) 325{ 326 return swizzle(reg, x, x, x, x); 327} 328 329static struct ureg get_temp( struct tnl_program *p ) 330{ 331 int bit = ffs( ~p->temp_in_use ); 332 if (!bit) { 333 fprintf(stderr, "%s: out of temporaries\n", __FILE__); 334 exit(1); 335 } 336 337 p->temp_in_use |= 1<<(bit-1); 338 return make_ureg(PROGRAM_TEMPORARY, bit-1); 339} 340 341static struct ureg reserve_temp( struct tnl_program *p ) 342{ 343 struct ureg temp = get_temp( p ); 344 p->temp_reserved |= 1<<temp.idx; 345 return temp; 346} 347 348static void release_temp( struct tnl_program *p, struct ureg reg ) 349{ 350 if (reg.file == PROGRAM_TEMPORARY) { 351 p->temp_in_use &= ~(1<<reg.idx); 352 p->temp_in_use |= p->temp_reserved; /* can't release reserved temps */ 353 } 354} 355 356static void release_temps( struct tnl_program *p ) 357{ 358 p->temp_in_use = p->temp_reserved; 359} 360 361 362 363static struct ureg register_input( struct tnl_program *p, GLuint input ) 364{ 365 p->program->InputsRead |= (1<<input); 366 return make_ureg(PROGRAM_INPUT, input); 367} 368 369static struct ureg register_output( struct tnl_program *p, GLuint output ) 370{ 371 p->program->OutputsWritten |= (1<<output); 372 return make_ureg(PROGRAM_OUTPUT, output); 373} 374 375static struct ureg register_const4f( struct tnl_program *p, 376 GLfloat s0, 377 GLfloat s1, 378 GLfloat s2, 379 GLfloat s3) 380{ 381 GLfloat values[4]; 382 GLuint idx; 383 values[0] = s0; 384 values[1] = s1; 385 values[2] = s2; 386 values[3] = s3; 387 idx = _mesa_add_unnamed_constant( p->program->Parameters, values ); 388 return make_ureg(PROGRAM_STATE_VAR, idx); 389} 390 391#define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1) 392#define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0) 393#define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1) 394#define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1) 395 396static GLboolean is_undef( struct ureg reg ) 397{ 398 return reg.file == 0xf; 399} 400 401static struct ureg get_identity_param( struct tnl_program *p ) 402{ 403 if (is_undef(p->identity)) 404 p->identity = register_const4f(p, 0,0,0,1); 405 406 return p->identity; 407} 408 409static struct ureg register_param6( struct tnl_program *p, 410 GLint s0, 411 GLint s1, 412 GLint s2, 413 GLint s3, 414 GLint s4, 415 GLint s5) 416{ 417 GLint tokens[6]; 418 GLuint idx; 419 tokens[0] = s0; 420 tokens[1] = s1; 421 tokens[2] = s2; 422 tokens[3] = s3; 423 tokens[4] = s4; 424 tokens[5] = s5; 425 idx = _mesa_add_state_reference( p->program->Parameters, tokens ); 426 return make_ureg(PROGRAM_STATE_VAR, idx); 427} 428 429 430#define register_param1(p,s0) register_param6(p,s0,0,0,0,0,0) 431#define register_param2(p,s0,s1) register_param6(p,s0,s1,0,0,0,0) 432#define register_param3(p,s0,s1,s2) register_param6(p,s0,s1,s2,0,0,0) 433#define register_param4(p,s0,s1,s2,s3) register_param6(p,s0,s1,s2,s3,0,0) 434 435 436static void register_matrix_param6( struct tnl_program *p, 437 GLint s0, 438 GLint s1, 439 GLint s2, 440 GLint s3, 441 GLint s4, 442 GLint s5, 443 struct ureg *matrix ) 444{ 445 GLuint i; 446 447 /* This is a bit sad as the support is there to pull the whole 448 * matrix out in one go: 449 */ 450 for (i = 0; i <= s4 - s3; i++) 451 matrix[i] = register_param6( p, s0, s1, s2, i, i, s5 ); 452} 453 454 455static void emit_arg( struct vp_src_register *src, 456 struct ureg reg ) 457{ 458 src->File = reg.file; 459 src->Index = reg.idx; 460 src->Swizzle = reg.swz; 461 src->Negate = reg.negate; 462 src->RelAddr = 0; 463 src->pad = 0; 464} 465 466static void emit_dst( struct vp_dst_register *dst, 467 struct ureg reg, GLuint mask ) 468{ 469 dst->File = reg.file; 470 dst->Index = reg.idx; 471 /* allow zero as a shorthand for xyzw */ 472 dst->WriteMask = mask ? mask : WRITEMASK_XYZW; 473 dst->pad = 0; 474} 475 476static void debug_insn( struct vp_instruction *inst, const char *fn, 477 GLuint line ) 478{ 479#if DISASSEM 480 static const char *last_fn; 481 482 if (fn != last_fn) { 483 last_fn = fn; 484 _mesa_printf("%s:\n", fn); 485 } 486 487 _mesa_printf("%d:\t", line); 488 _mesa_debug_vp_inst(1, inst); 489#endif 490} 491 492 493static void emit_op3fn(struct tnl_program *p, 494 GLuint op, 495 struct ureg dest, 496 GLuint mask, 497 struct ureg src0, 498 struct ureg src1, 499 struct ureg src2, 500 const char *fn, 501 GLuint line) 502{ 503 GLuint nr = p->program->Base.NumInstructions++; 504 struct vp_instruction *inst = &p->program->Instructions[nr]; 505 506 if (p->program->Base.NumInstructions > MAX_INSN) { 507 _mesa_problem(0, "Out of instructions in emit_op3fn\n"); 508 return; 509 } 510 511 inst->Opcode = op; 512 inst->StringPos = 0; 513 inst->Data = 0; 514 515 emit_arg( &inst->SrcReg[0], src0 ); 516 emit_arg( &inst->SrcReg[1], src1 ); 517 emit_arg( &inst->SrcReg[2], src2 ); 518 519 emit_dst( &inst->DstReg, dest, mask ); 520 521 debug_insn(inst, fn, line); 522} 523 524 525 526#define emit_op3(p, op, dst, mask, src0, src1, src2) \ 527 emit_op3fn(p, op, dst, mask, src0, src1, src2, __FUNCTION__, __LINE__) 528 529#define emit_op2(p, op, dst, mask, src0, src1) \ 530 emit_op3fn(p, op, dst, mask, src0, src1, undef, __FUNCTION__, __LINE__) 531 532#define emit_op1(p, op, dst, mask, src0) \ 533 emit_op3fn(p, op, dst, mask, src0, undef, undef, __FUNCTION__, __LINE__) 534 535 536static struct ureg make_temp( struct tnl_program *p, struct ureg reg ) 537{ 538 if (reg.file == PROGRAM_TEMPORARY && 539 !(p->temp_reserved & (1<<reg.idx))) 540 return reg; 541 else { 542 struct ureg temp = get_temp(p); 543 emit_op1(p, VP_OPCODE_MOV, temp, 0, reg); 544 return temp; 545 } 546} 547 548 549/* Currently no tracking performed of input/output/register size or 550 * active elements. Could be used to reduce these operations, as 551 * could the matrix type. 552 */ 553static void emit_matrix_transform_vec4( struct tnl_program *p, 554 struct ureg dest, 555 const struct ureg *mat, 556 struct ureg src) 557{ 558 emit_op2(p, VP_OPCODE_DP4, dest, WRITEMASK_X, src, mat[0]); 559 emit_op2(p, VP_OPCODE_DP4, dest, WRITEMASK_Y, src, mat[1]); 560 emit_op2(p, VP_OPCODE_DP4, dest, WRITEMASK_Z, src, mat[2]); 561 emit_op2(p, VP_OPCODE_DP4, dest, WRITEMASK_W, src, mat[3]); 562} 563 564/* This version is much easier to implement if writemasks are not 565 * supported natively on the target or (like SSE), the target doesn't 566 * have a clean/obvious dotproduct implementation. 567 */ 568static void emit_transpose_matrix_transform_vec4( struct tnl_program *p, 569 struct ureg dest, 570 const struct ureg *mat, 571 struct ureg src) 572{ 573 struct ureg tmp; 574 575 if (dest.file != PROGRAM_TEMPORARY) 576 tmp = get_temp(p); 577 else 578 tmp = dest; 579 580 emit_op2(p, VP_OPCODE_MUL, tmp, 0, swizzle1(src,X), mat[0]); 581 emit_op3(p, VP_OPCODE_MAD, tmp, 0, swizzle1(src,Y), mat[1], tmp); 582 emit_op3(p, VP_OPCODE_MAD, tmp, 0, swizzle1(src,Z), mat[2], tmp); 583 emit_op3(p, VP_OPCODE_MAD, dest, 0, swizzle1(src,W), mat[3], tmp); 584 585 if (dest.file != PROGRAM_TEMPORARY) 586 release_temp(p, tmp); 587} 588 589static void emit_matrix_transform_vec3( struct tnl_program *p, 590 struct ureg dest, 591 const struct ureg *mat, 592 struct ureg src) 593{ 594 emit_op2(p, VP_OPCODE_DP3, dest, WRITEMASK_X, src, mat[0]); 595 emit_op2(p, VP_OPCODE_DP3, dest, WRITEMASK_Y, src, mat[1]); 596 emit_op2(p, VP_OPCODE_DP3, dest, WRITEMASK_Z, src, mat[2]); 597} 598 599 600static void emit_normalize_vec3( struct tnl_program *p, 601 struct ureg dest, 602 struct ureg src ) 603{ 604 struct ureg tmp = get_temp(p); 605 emit_op2(p, VP_OPCODE_DP3, tmp, 0, src, src); 606 emit_op1(p, VP_OPCODE_RSQ, tmp, 0, tmp); 607 emit_op2(p, VP_OPCODE_MUL, dest, 0, src, tmp); 608 release_temp(p, tmp); 609} 610 611static void emit_passthrough( struct tnl_program *p, 612 GLuint input, 613 GLuint output ) 614{ 615 struct ureg out = register_output(p, output); 616 emit_op1(p, VP_OPCODE_MOV, out, 0, register_input(p, input)); 617} 618 619static struct ureg get_eye_position( struct tnl_program *p ) 620{ 621 if (is_undef(p->eye_position)) { 622 struct ureg pos = register_input( p, VERT_ATTRIB_POS ); 623 struct ureg modelview[4]; 624 625 p->eye_position = reserve_temp(p); 626 627 if (PREFER_DP4) { 628 register_matrix_param6( p, STATE_MATRIX, STATE_MODELVIEW, 0, 0, 3, 629 STATE_MATRIX, modelview ); 630 631 emit_matrix_transform_vec4(p, p->eye_position, modelview, pos); 632 } 633 else { 634 register_matrix_param6( p, STATE_MATRIX, STATE_MODELVIEW, 0, 0, 3, 635 STATE_MATRIX_TRANSPOSE, modelview ); 636 637 emit_transpose_matrix_transform_vec4(p, p->eye_position, modelview, pos); 638 } 639 } 640 641 return p->eye_position; 642} 643 644 645static struct ureg get_eye_position_normalized( struct tnl_program *p ) 646{ 647 if (is_undef(p->eye_position_normalized)) { 648 struct ureg eye = get_eye_position(p); 649 p->eye_position_normalized = reserve_temp(p); 650 emit_normalize_vec3(p, p->eye_position_normalized, eye); 651 } 652 653 return p->eye_position_normalized; 654} 655 656 657static struct ureg get_eye_normal( struct tnl_program *p ) 658{ 659 if (is_undef(p->eye_normal)) { 660 struct ureg normal = register_input(p, VERT_ATTRIB_NORMAL ); 661 struct ureg mvinv[3]; 662 663 register_matrix_param6( p, STATE_MATRIX, STATE_MODELVIEW, 0, 0, 2, 664 STATE_MATRIX_INVTRANS, mvinv ); 665 666 p->eye_normal = reserve_temp(p); 667 668 /* Transform to eye space: 669 */ 670 emit_matrix_transform_vec3( p, p->eye_normal, mvinv, normal ); 671 672 /* Normalize/Rescale: 673 */ 674 if (p->state->normalize) { 675 emit_normalize_vec3( p, p->eye_normal, p->eye_normal ); 676 } 677 else if (p->state->rescale_normals) { 678 struct ureg rescale = register_param2(p, STATE_INTERNAL, 679 STATE_NORMAL_SCALE); 680 681 emit_op2( p, VP_OPCODE_MUL, p->eye_normal, 0, normal, 682 swizzle1(rescale, X)); 683 } 684 } 685 686 return p->eye_normal; 687} 688 689 690 691static void build_hpos( struct tnl_program *p ) 692{ 693 struct ureg pos = register_input( p, VERT_ATTRIB_POS ); 694 struct ureg hpos = register_output( p, VERT_RESULT_HPOS ); 695 struct ureg mvp[4]; 696 697 if (PREFER_DP4) { 698 register_matrix_param6( p, STATE_MATRIX, STATE_MVP, 0, 0, 3, 699 STATE_MATRIX, mvp ); 700 emit_matrix_transform_vec4( p, hpos, mvp, pos ); 701 } 702 else { 703 register_matrix_param6( p, STATE_MATRIX, STATE_MVP, 0, 0, 3, 704 STATE_MATRIX_TRANSPOSE, mvp ); 705 emit_transpose_matrix_transform_vec4( p, hpos, mvp, pos ); 706 } 707} 708 709 710static GLuint material_attrib( GLuint side, GLuint property ) 711{ 712 return ((property - STATE_AMBIENT) * 2 + 713 side); 714} 715 716static void set_material_flags( struct tnl_program *p ) 717{ 718 p->color_materials = 0; 719 p->materials = 0; 720 721 if (p->state->light_color_material) { 722 p->materials = 723 p->color_materials = p->state->light_color_material_mask; 724 } 725 726 p->materials |= p->state->light_material_mask; 727} 728 729 730static struct ureg get_material( struct tnl_program *p, GLuint side, 731 GLuint property ) 732{ 733 GLuint attrib = material_attrib(side, property); 734 735 if (p->color_materials & (1<<attrib)) 736 return register_input(p, VERT_ATTRIB_COLOR0); 737 else if (p->materials & (1<<attrib)) 738 return register_input( p, attrib + _TNL_ATTRIB_MAT_FRONT_AMBIENT ); 739 else 740 return register_param3( p, STATE_MATERIAL, side, property ); 741} 742 743#define SCENE_COLOR_BITS(side) (( MAT_BIT_FRONT_EMISSION | \ 744 MAT_BIT_FRONT_AMBIENT | \ 745 MAT_BIT_FRONT_DIFFUSE) << (side)) 746 747/* Either return a precalculated constant value or emit code to 748 * calculate these values dynamically in the case where material calls 749 * are present between begin/end pairs. 750 * 751 * Probably want to shift this to the program compilation phase - if 752 * we always emitted the calculation here, a smart compiler could 753 * detect that it was constant (given a certain set of inputs), and 754 * lift it out of the main loop. That way the programs created here 755 * would be independent of the vertex_buffer details. 756 */ 757static struct ureg get_scenecolor( struct tnl_program *p, GLuint side ) 758{ 759 if (p->materials & SCENE_COLOR_BITS(side)) { 760 struct ureg lm_ambient = register_param1(p, STATE_LIGHTMODEL_AMBIENT); 761 struct ureg material_emission = get_material(p, side, STATE_EMISSION); 762 struct ureg material_ambient = get_material(p, side, STATE_AMBIENT); 763 struct ureg material_diffuse = get_material(p, side, STATE_DIFFUSE); 764 struct ureg tmp = make_temp(p, material_diffuse); 765 emit_op3(p, VP_OPCODE_MAD, tmp, WRITEMASK_XYZ, lm_ambient, 766 material_ambient, material_emission); 767 return tmp; 768 } 769 else 770 return register_param2( p, STATE_LIGHTMODEL_SCENECOLOR, side ); 771} 772 773 774static struct ureg get_lightprod( struct tnl_program *p, GLuint light, 775 GLuint side, GLuint property ) 776{ 777 GLuint attrib = material_attrib(side, property); 778 if (p->materials & (1<<attrib)) { 779 struct ureg light_value = 780 register_param3(p, STATE_LIGHT, light, property); 781 struct ureg material_value = get_material(p, side, property); 782 struct ureg tmp = get_temp(p); 783 emit_op2(p, VP_OPCODE_MUL, tmp, 0, light_value, material_value); 784 return tmp; 785 } 786 else 787 return register_param4(p, STATE_LIGHTPROD, light, side, property); 788} 789 790static struct ureg calculate_light_attenuation( struct tnl_program *p, 791 GLuint i, 792 struct ureg VPpli, 793 struct ureg dist ) 794{ 795 struct ureg attenuation = register_param3(p, STATE_LIGHT, i, 796 STATE_ATTENUATION); 797 struct ureg att = get_temp(p); 798 799 /* Calculate spot attenuation: 800 */ 801 if (!p->state->unit[i].light_spotcutoff_is_180) { 802 struct ureg spot_dir = register_param3(p, STATE_LIGHT, i, 803 STATE_SPOT_DIRECTION); 804 struct ureg spot = get_temp(p); 805 struct ureg slt = get_temp(p); 806 807 emit_normalize_vec3( p, spot, spot_dir ); /* XXX: precompute! */ 808 emit_op2(p, VP_OPCODE_DP3, spot, 0, negate(VPpli), spot_dir); 809 emit_op2(p, VP_OPCODE_SLT, slt, 0, swizzle1(spot_dir,W), spot); 810 emit_op2(p, VP_OPCODE_POW, spot, 0, spot, swizzle1(attenuation, W)); 811 emit_op2(p, VP_OPCODE_MUL, att, 0, slt, spot); 812 813 release_temp(p, spot); 814 release_temp(p, slt); 815 } 816 817 /* Calculate distance attenuation: 818 */ 819 if (p->state->unit[i].light_attenuated) { 820 821 /* 1/d,d,d,1/d */ 822 emit_op1(p, VP_OPCODE_RCP, dist, WRITEMASK_YZ, dist); 823 /* 1,d,d*d,1/d */ 824 emit_op2(p, VP_OPCODE_MUL, dist, WRITEMASK_XZ, dist, swizzle1(dist,Y)); 825 /* 1/dist-atten */ 826 emit_op2(p, VP_OPCODE_DP3, dist, 0, attenuation, dist); 827 828 if (!p->state->unit[i].light_spotcutoff_is_180) { 829 /* dist-atten */ 830 emit_op1(p, VP_OPCODE_RCP, dist, 0, dist); 831 /* spot-atten * dist-atten */ 832 emit_op2(p, VP_OPCODE_MUL, att, 0, dist, att); 833 } else { 834 /* dist-atten */ 835 emit_op1(p, VP_OPCODE_RCP, att, 0, dist); 836 } 837 } 838 839 return att; 840} 841 842 843 844 845 846/* Need to add some addtional parameters to allow lighting in object 847 * space - STATE_SPOT_DIRECTION and STATE_HALF implicitly assume eye 848 * space lighting. 849 */ 850static void build_lighting( struct tnl_program *p ) 851{ 852 const GLboolean twoside = p->state->light_twoside; 853 const GLboolean separate = p->state->separate_specular; 854 GLuint nr_lights = 0, count = 0; 855 struct ureg normal = get_eye_normal(p); 856 struct ureg lit = get_temp(p); 857 struct ureg dots = get_temp(p); 858 struct ureg _col0 = undef, _col1 = undef; 859 struct ureg _bfc0 = undef, _bfc1 = undef; 860 GLuint i; 861 862 for (i = 0; i < MAX_LIGHTS; i++) 863 if (p->state->unit[i].light_enabled) 864 nr_lights++; 865 866 set_material_flags(p); 867 868 { 869 struct ureg shininess = get_material(p, 0, STATE_SHININESS); 870 emit_op1(p, VP_OPCODE_MOV, dots, WRITEMASK_W, swizzle1(shininess,X)); 871 release_temp(p, shininess); 872 873 _col0 = make_temp(p, get_scenecolor(p, 0)); 874 if (separate) 875 _col1 = make_temp(p, get_identity_param(p)); 876 else 877 _col1 = _col0; 878 879 } 880 881 if (twoside) { 882 struct ureg shininess = get_material(p, 1, STATE_SHININESS); 883 emit_op1(p, VP_OPCODE_MOV, dots, WRITEMASK_Z, 884 negate(swizzle1(shininess,X))); 885 release_temp(p, shininess); 886 887 _bfc0 = make_temp(p, get_scenecolor(p, 1)); 888 if (separate) 889 _bfc1 = make_temp(p, get_identity_param(p)); 890 else 891 _bfc1 = _bfc0; 892 } 893 894 895 /* If no lights, still need to emit the scenecolor. 896 */ 897 { 898 struct ureg res0 = register_output( p, VERT_RESULT_COL0 ); 899 emit_op1(p, VP_OPCODE_MOV, res0, 0, _col0); 900 } 901 902 if (separate) { 903 struct ureg res1 = register_output( p, VERT_RESULT_COL1 ); 904 emit_op1(p, VP_OPCODE_MOV, res1, 0, _col1); 905 } 906 907 if (twoside) { 908 struct ureg res0 = register_output( p, VERT_RESULT_BFC0 ); 909 emit_op1(p, VP_OPCODE_MOV, res0, 0, _bfc0); 910 } 911 912 if (twoside && separate) { 913 struct ureg res1 = register_output( p, VERT_RESULT_BFC1 ); 914 emit_op1(p, VP_OPCODE_MOV, res1, 0, _bfc1); 915 } 916 917 if (nr_lights == 0) { 918 release_temps(p); 919 return; 920 } 921 922 923 for (i = 0; i < MAX_LIGHTS; i++) { 924 if (p->state->unit[i].light_enabled) { 925 struct ureg half = undef; 926 struct ureg att = undef, VPpli = undef; 927 928 count++; 929 930 if (p->state->unit[i].light_eyepos3_is_zero) { 931 /* Can used precomputed constants in this case. 932 * Attenuation never applies to infinite lights. 933 */ 934 VPpli = register_param3(p, STATE_LIGHT, i, 935 STATE_POSITION_NORMALIZED); 936 half = register_param3(p, STATE_LIGHT, i, STATE_HALF); 937 } 938 else { 939 struct ureg Ppli = register_param3(p, STATE_LIGHT, i, 940 STATE_POSITION); 941 struct ureg V = get_eye_position(p); 942 struct ureg dist = get_temp(p); 943 944 VPpli = get_temp(p); 945 half = get_temp(p); 946 947 /* Calulate VPpli vector 948 */ 949 emit_op2(p, VP_OPCODE_SUB, VPpli, 0, Ppli, V); 950 951 /* Normalize VPpli. The dist value also used in 952 * attenuation below. 953 */ 954 emit_op2(p, VP_OPCODE_DP3, dist, 0, VPpli, VPpli); 955 emit_op1(p, VP_OPCODE_RSQ, dist, 0, dist); 956 emit_op2(p, VP_OPCODE_MUL, VPpli, 0, VPpli, dist); 957 958 959 /* Calculate attenuation: 960 */ 961 if (!p->state->unit[i].light_spotcutoff_is_180 || 962 p->state->unit[i].light_attenuated) { 963 att = calculate_light_attenuation(p, i, VPpli, dist); 964 } 965 966 967 /* Calculate viewer direction, or use infinite viewer: 968 */ 969 if (p->state->light_local_viewer) { 970 struct ureg eye_hat = get_eye_position_normalized(p); 971 emit_op2(p, VP_OPCODE_SUB, half, 0, VPpli, eye_hat); 972 } 973 else { 974 struct ureg z_dir = swizzle(get_identity_param(p),X,Y,W,Z); 975 emit_op2(p, VP_OPCODE_ADD, half, 0, VPpli, z_dir); 976 } 977 978 emit_normalize_vec3(p, half, half); 979 980 release_temp(p, dist); 981 } 982 983 /* Calculate dot products: 984 */ 985 emit_op2(p, VP_OPCODE_DP3, dots, WRITEMASK_X, normal, VPpli); 986 emit_op2(p, VP_OPCODE_DP3, dots, WRITEMASK_Y, normal, half); 987 988 989 /* Front face lighting: 990 */ 991 { 992 struct ureg ambient = get_lightprod(p, i, 0, STATE_AMBIENT); 993 struct ureg diffuse = get_lightprod(p, i, 0, STATE_DIFFUSE); 994 struct ureg specular = get_lightprod(p, i, 0, STATE_SPECULAR); 995 struct ureg res0, res1; 996 GLuint mask0, mask1; 997 998 emit_op1(p, VP_OPCODE_LIT, lit, 0, dots); 999 1000 if (!is_undef(att)) 1001 emit_op2(p, VP_OPCODE_MUL, lit, 0, lit, att); 1002 1003 1004 if (count == nr_lights) { 1005 if (separate) { 1006 mask0 = WRITEMASK_XYZ; 1007 mask1 = WRITEMASK_XYZ; 1008 res0 = register_output( p, VERT_RESULT_COL0 ); 1009 res1 = register_output( p, VERT_RESULT_COL1 ); 1010 } 1011 else { 1012 mask0 = 0; 1013 mask1 = WRITEMASK_XYZ; 1014 res0 = _col0; 1015 res1 = register_output( p, VERT_RESULT_COL0 ); 1016 } 1017 } else { 1018 mask0 = 0; 1019 mask1 = 0; 1020 res0 = _col0; 1021 res1 = _col1; 1022 } 1023 1024 emit_op3(p, VP_OPCODE_MAD, _col0, 0, swizzle1(lit,X), ambient, _col0); 1025 emit_op3(p, VP_OPCODE_MAD, res0, mask0, swizzle1(lit,Y), diffuse, _col0); 1026 emit_op3(p, VP_OPCODE_MAD, res1, mask1, swizzle1(lit,Z), specular, _col1); 1027 1028 release_temp(p, ambient); 1029 release_temp(p, diffuse); 1030 release_temp(p, specular); 1031 } 1032 1033 /* Back face lighting: 1034 */ 1035 if (twoside) { 1036 struct ureg ambient = get_lightprod(p, i, 1, STATE_AMBIENT); 1037 struct ureg diffuse = get_lightprod(p, i, 1, STATE_DIFFUSE); 1038 struct ureg specular = get_lightprod(p, i, 1, STATE_SPECULAR); 1039 struct ureg res0, res1; 1040 GLuint mask0, mask1; 1041 1042 emit_op1(p, VP_OPCODE_LIT, lit, 0, negate(swizzle(dots,X,Y,W,Z))); 1043 1044 if (!is_undef(att)) 1045 emit_op2(p, VP_OPCODE_MUL, lit, 0, lit, att); 1046 1047 if (count == nr_lights) { 1048 if (separate) { 1049 mask0 = WRITEMASK_XYZ; 1050 mask1 = WRITEMASK_XYZ; 1051 res0 = register_output( p, VERT_RESULT_BFC0 ); 1052 res1 = register_output( p, VERT_RESULT_BFC1 ); 1053 } 1054 else { 1055 mask0 = 0; 1056 mask1 = WRITEMASK_XYZ; 1057 res0 = _bfc0; 1058 res1 = register_output( p, VERT_RESULT_BFC0 ); 1059 } 1060 } else { 1061 res0 = _bfc0; 1062 res1 = _bfc1; 1063 mask0 = 0; 1064 mask1 = 0; 1065 } 1066 1067 emit_op3(p, VP_OPCODE_MAD, _bfc0, 0, swizzle1(lit,X), ambient, _bfc0); 1068 emit_op3(p, VP_OPCODE_MAD, res0, mask0, swizzle1(lit,Y), diffuse, _bfc0); 1069 emit_op3(p, VP_OPCODE_MAD, res1, mask1, swizzle1(lit,Z), specular, _bfc1); 1070 1071 release_temp(p, ambient); 1072 release_temp(p, diffuse); 1073 release_temp(p, specular); 1074 } 1075 1076 release_temp(p, half); 1077 release_temp(p, VPpli); 1078 release_temp(p, att); 1079 } 1080 } 1081 1082 release_temps( p ); 1083} 1084 1085 1086static void build_fog( struct tnl_program *p ) 1087{ 1088 struct ureg fog = register_output(p, VERT_RESULT_FOGC); 1089 struct ureg input; 1090 1091 if (p->state->fog_source_is_depth) { 1092 input = swizzle1(get_eye_position(p), Z); 1093 } 1094 else { 1095 input = swizzle1(register_input(p, VERT_ATTRIB_FOG), X); 1096 } 1097 1098 if (p->state->tnl_do_vertex_fog) { 1099 struct ureg params = register_param1(p, STATE_FOG_PARAMS); 1100 struct ureg tmp = get_temp(p); 1101 1102 switch (p->state->fog_mode) { 1103 case FOG_LINEAR: { 1104 struct ureg id = get_identity_param(p); 1105 emit_op2(p, VP_OPCODE_SUB, tmp, 0, swizzle1(params,Z), input); 1106 emit_op2(p, VP_OPCODE_MUL, tmp, 0, tmp, swizzle1(params,W)); 1107 emit_op2(p, VP_OPCODE_MAX, tmp, 0, tmp, swizzle1(id,X)); /* saturate */ 1108 emit_op2(p, VP_OPCODE_MIN, fog, WRITEMASK_X, tmp, swizzle1(id,W)); 1109 break; 1110 } 1111 case FOG_EXP: 1112 emit_op1(p, VP_OPCODE_ABS, tmp, 0, input); 1113 emit_op2(p, VP_OPCODE_MUL, tmp, 0, tmp, swizzle1(params,X)); 1114 emit_op2(p, VP_OPCODE_POW, fog, WRITEMASK_X, 1115 register_const1f(p, M_E), negate(tmp)); 1116 break; 1117 case FOG_EXP2: 1118 emit_op2(p, VP_OPCODE_MUL, tmp, 0, input, swizzle1(params,X)); 1119 emit_op2(p, VP_OPCODE_MUL, tmp, 0, tmp, tmp); 1120 emit_op2(p, VP_OPCODE_POW, fog, WRITEMASK_X, 1121 register_const1f(p, M_E), negate(tmp)); 1122 break; 1123 } 1124 1125 release_temp(p, tmp); 1126 } 1127 else { 1128 /* results = incoming fog coords (compute fog per-fragment later) 1129 * 1130 * KW: Is it really necessary to do anything in this case? 1131 */ 1132 emit_op1(p, VP_OPCODE_MOV, fog, WRITEMASK_X, input); 1133 } 1134} 1135 1136static void build_reflect_texgen( struct tnl_program *p, 1137 struct ureg dest, 1138 GLuint writemask ) 1139{ 1140 struct ureg normal = get_eye_normal(p); 1141 struct ureg eye_hat = get_eye_position_normalized(p); 1142 struct ureg tmp = get_temp(p); 1143 1144 /* n.u */ 1145 emit_op2(p, VP_OPCODE_DP3, tmp, 0, normal, eye_hat); 1146 /* 2n.u */ 1147 emit_op2(p, VP_OPCODE_ADD, tmp, 0, tmp, tmp); 1148 /* (-2n.u)n + u */ 1149 emit_op3(p, VP_OPCODE_MAD, dest, writemask, negate(tmp), normal, eye_hat); 1150} 1151 1152static void build_sphere_texgen( struct tnl_program *p, 1153 struct ureg dest, 1154 GLuint writemask ) 1155{ 1156 struct ureg normal = get_eye_normal(p); 1157 struct ureg eye_hat = get_eye_position_normalized(p); 1158 struct ureg tmp = get_temp(p); 1159 struct ureg half = register_scalar_const(p, .5); 1160 struct ureg r = get_temp(p); 1161 struct ureg inv_m = get_temp(p); 1162 struct ureg id = get_identity_param(p); 1163 1164 /* Could share the above calculations, but it would be 1165 * a fairly odd state for someone to set (both sphere and 1166 * reflection active for different texture coordinate 1167 * components. Of course - if two texture units enable 1168 * reflect and/or sphere, things start to tilt in favour 1169 * of seperating this out: 1170 */ 1171 1172 /* n.u */ 1173 emit_op2(p, VP_OPCODE_DP3, tmp, 0, normal, eye_hat); 1174 /* 2n.u */ 1175 emit_op2(p, VP_OPCODE_ADD, tmp, 0, tmp, tmp); 1176 /* (-2n.u)n + u */ 1177 emit_op3(p, VP_OPCODE_MAD, r, 0, negate(tmp), normal, eye_hat); 1178 /* r + 0,0,1 */ 1179 emit_op2(p, VP_OPCODE_ADD, tmp, 0, r, swizzle(id,X,Y,W,Z)); 1180 /* rx^2 + ry^2 + (rz+1)^2 */ 1181 emit_op2(p, VP_OPCODE_DP3, tmp, 0, tmp, tmp); 1182 /* 2/m */ 1183 emit_op1(p, VP_OPCODE_RSQ, tmp, 0, tmp); 1184 /* 1/m */ 1185 emit_op2(p, VP_OPCODE_MUL, inv_m, 0, tmp, half); 1186 /* r/m + 1/2 */ 1187 emit_op3(p, VP_OPCODE_MAD, dest, writemask, r, inv_m, half); 1188 1189 release_temp(p, tmp); 1190 release_temp(p, r); 1191 release_temp(p, inv_m); 1192} 1193 1194 1195static void build_texture_transform( struct tnl_program *p ) 1196{ 1197 GLuint i, j; 1198 1199 for (i = 0; i < MAX_TEXTURE_UNITS; i++) { 1200 GLuint texmat_enabled = p->state->unit[i].texmat_enabled; 1201 1202 if (p->state->unit[i].texgen_enabled || texmat_enabled) { 1203 struct ureg out = register_output(p, VERT_RESULT_TEX0 + i); 1204 struct ureg out_texgen = undef; 1205 1206 if (p->state->unit[i].texgen_enabled) { 1207 GLuint copy_mask = 0; 1208 GLuint sphere_mask = 0; 1209 GLuint reflect_mask = 0; 1210 GLuint normal_mask = 0; 1211 GLuint modes[4]; 1212 1213 if (texmat_enabled) 1214 out_texgen = get_temp(p); 1215 else 1216 out_texgen = out; 1217 1218 modes[0] = p->state->unit[i].texgen_mode0; 1219 modes[1] = p->state->unit[i].texgen_mode1; 1220 modes[2] = p->state->unit[i].texgen_mode2; 1221 modes[3] = p->state->unit[i].texgen_mode3; 1222 1223 for (j = 0; j < 4; j++) { 1224 switch (modes[j]) { 1225 case TXG_OBJ_LINEAR: { 1226 struct ureg obj = register_input(p, VERT_ATTRIB_POS); 1227 struct ureg plane = 1228 register_param3(p, STATE_TEXGEN, i, 1229 STATE_TEXGEN_OBJECT_S + j); 1230 1231 emit_op2(p, VP_OPCODE_DP4, out_texgen, WRITEMASK_X << j, 1232 obj, plane ); 1233 break; 1234 } 1235 case TXG_EYE_LINEAR: { 1236 struct ureg eye = get_eye_position(p); 1237 struct ureg plane = 1238 register_param3(p, STATE_TEXGEN, i, 1239 STATE_TEXGEN_EYE_S + j); 1240 1241 emit_op2(p, VP_OPCODE_DP4, out_texgen, WRITEMASK_X << j, 1242 eye, plane ); 1243 break; 1244 } 1245 case TXG_SPHERE_MAP: 1246 sphere_mask |= WRITEMASK_X << j; 1247 break; 1248 case TXG_REFLECTION_MAP: 1249 reflect_mask |= WRITEMASK_X << j; 1250 break; 1251 case TXG_NORMAL_MAP: 1252 normal_mask |= WRITEMASK_X << j; 1253 break; 1254 case TXG_NONE: 1255 copy_mask |= WRITEMASK_X << j; 1256 } 1257 1258 } 1259 1260 1261 if (sphere_mask) { 1262 build_sphere_texgen(p, out_texgen, sphere_mask); 1263 } 1264 1265 if (reflect_mask) { 1266 build_reflect_texgen(p, out_texgen, reflect_mask); 1267 } 1268 1269 if (normal_mask) { 1270 struct ureg normal = get_eye_normal(p); 1271 emit_op1(p, VP_OPCODE_MOV, out_texgen, normal_mask, normal ); 1272 } 1273 1274 if (copy_mask) { 1275 struct ureg in = register_input(p, VERT_ATTRIB_TEX0+i); 1276 emit_op1(p, VP_OPCODE_MOV, out_texgen, copy_mask, in ); 1277 } 1278 } 1279 1280 if (texmat_enabled) { 1281 struct ureg texmat[4]; 1282 struct ureg in = (!is_undef(out_texgen) ? 1283 out_texgen : 1284 register_input(p, VERT_ATTRIB_TEX0+i)); 1285 if (PREFER_DP4) { 1286 register_matrix_param6( p, STATE_MATRIX, STATE_TEXTURE, i, 1287 0, 3, STATE_MATRIX, texmat ); 1288 emit_matrix_transform_vec4( p, out, texmat, in ); 1289 } 1290 else { 1291 register_matrix_param6( p, STATE_MATRIX, STATE_TEXTURE, i, 1292 0, 3, STATE_MATRIX_TRANSPOSE, texmat ); 1293 emit_transpose_matrix_transform_vec4( p, out, texmat, in ); 1294 } 1295 } 1296 1297 release_temps(p); 1298 } 1299 else if (p->state->unit[i].texunit_really_enabled) { 1300 /* KW: _ReallyEnabled isn't sufficient? Need to know whether 1301 * this texture unit is referenced by the fragment shader. 1302 */ 1303 emit_passthrough(p, VERT_ATTRIB_TEX0+i, VERT_RESULT_TEX0+i); 1304 } 1305 } 1306} 1307 1308 1309/* Seems like it could be tighter: 1310 */ 1311static void build_pointsize( struct tnl_program *p ) 1312{ 1313 struct ureg eye = get_eye_position(p); 1314 struct ureg state_size = register_param1(p, STATE_POINT_SIZE); 1315 struct ureg state_attenuation = register_param1(p, STATE_POINT_ATTENUATION); 1316 struct ureg out = register_output(p, VERT_RESULT_PSIZ); 1317 struct ureg ut = get_temp(p); 1318 1319 /* 1, -Z, Z * Z, 1 */ 1320 emit_op1(p, VP_OPCODE_MOV, ut, 0, swizzle1(get_identity_param(p), W)); 1321 emit_op2(p, VP_OPCODE_MUL, ut, WRITEMASK_YZ, ut, negate(swizzle1(eye, Z))); 1322 emit_op2(p, VP_OPCODE_MUL, ut, WRITEMASK_Z, ut, negate(swizzle1(eye, Z))); 1323 1324 1325 /* p1 + p2 * dist + p3 * dist * dist, 0 */ 1326 emit_op2(p, VP_OPCODE_DP3, ut, 0, ut, state_attenuation); 1327 1328 /* 1 / factor */ 1329 emit_op1(p, VP_OPCODE_RCP, ut, 0, ut ); 1330 1331 /* out = pointSize / factor */ 1332 emit_op2(p, VP_OPCODE_MUL, out, WRITEMASK_X, ut, state_size); 1333 1334 release_temp(p, ut); 1335} 1336 1337static void build_tnl_program( struct tnl_program *p ) 1338{ /* Emit the program, starting with modelviewproject: 1339 */ 1340 build_hpos(p); 1341 1342 /* Lighting calculations: 1343 */ 1344 if (p->state->light_global_enabled) 1345 build_lighting(p); 1346 else 1347 emit_passthrough(p, VERT_ATTRIB_COLOR0, VERT_RESULT_COL0); 1348 1349 if (p->state->fog_enabled) 1350 build_fog(p); 1351 1352 if (p->state->texture_enabled_global) 1353 build_texture_transform(p); 1354 1355 if (p->state->point_attenuated) 1356 build_pointsize(p); 1357 1358 /* Finish up: 1359 */ 1360 emit_op1(p, VP_OPCODE_END, undef, 0, undef); 1361 1362 /* Disassemble: 1363 */ 1364 if (DISASSEM) { 1365 _mesa_printf ("\n"); 1366 } 1367} 1368 1369 1370void create_new_program( const struct state_key *key, 1371 struct vertex_program *program, 1372 GLuint max_temps) 1373{ 1374 struct tnl_program p; 1375 1376 _mesa_memset(&p, 0, sizeof(p)); 1377 p.state = key; 1378 p.program = program; 1379 p.eye_position = undef; 1380 p.eye_position_normalized = undef; 1381 p.eye_normal = undef; 1382 p.identity = undef; 1383 p.temp_in_use = 0; 1384 1385 if (max_temps >= sizeof(int) * 8) 1386 p.temp_reserved = 0; 1387 else 1388 p.temp_reserved = ~((1<<max_temps)-1); 1389 1390 p.program->Instructions = MALLOC(sizeof(struct vp_instruction) * MAX_INSN); 1391 p.program->Base.String = 0; 1392 p.program->Base.NumInstructions = 1393 p.program->Base.NumTemporaries = 1394 p.program->Base.NumParameters = 1395 p.program->Base.NumAttributes = p.program->Base.NumAddressRegs = 0; 1396 p.program->Parameters = _mesa_new_parameter_list(); 1397 p.program->InputsRead = 0; 1398 p.program->OutputsWritten = 0; 1399 1400 build_tnl_program( &p ); 1401} 1402 1403static void *search_cache( struct tnl_cache *cache, 1404 GLuint hash, 1405 const void *key, 1406 GLuint keysize) 1407{ 1408 struct tnl_cache *c; 1409 1410 for (c = cache; c; c = c->next) { 1411 if (c->hash == hash && memcmp(c->key, key, keysize) == 0) 1412 return c->data; 1413 } 1414 1415 return NULL; 1416} 1417 1418static void cache_item( struct tnl_cache **cache, 1419 GLuint hash, 1420 void *key, 1421 void *data ) 1422{ 1423 struct tnl_cache *c = MALLOC(sizeof(*c)); 1424 c->hash = hash; 1425 c->key = key; 1426 c->data = data; 1427 c->next = *cache; 1428 *cache = c; 1429} 1430 1431static GLuint hash_key( struct state_key *key ) 1432{ 1433 GLuint *ikey = (GLuint *)key; 1434 GLuint hash = 0, i; 1435 1436 /* I'm sure this can be improved on, but speed is important: 1437 */ 1438 for (i = 0; i < sizeof(*key)/sizeof(GLuint); i++) 1439 hash ^= ikey[i]; 1440 1441 return hash; 1442} 1443 1444void _tnl_UpdateFixedFunctionProgram( GLcontext *ctx ) 1445{ 1446 TNLcontext *tnl = TNL_CONTEXT(ctx); 1447 struct state_key *key; 1448 GLuint hash; 1449 1450 if (ctx->VertexProgram._Enabled) 1451 return; 1452 1453 /* Grab all the relevent state and put it in a single structure: 1454 */ 1455 key = make_state_key(ctx); 1456 hash = hash_key(key); 1457 1458 /* Look for an already-prepared program for this state: 1459 */ 1460 ctx->_TnlProgram = (struct vertex_program *) 1461 search_cache( tnl->vp_cache, hash, key, sizeof(*key) ); 1462 1463 /* OK, we'll have to build a new one: 1464 */ 1465 if (!ctx->_TnlProgram) { 1466 if (0) 1467 _mesa_printf("Build new TNL program\n"); 1468 1469 ctx->_TnlProgram = (struct vertex_program *) 1470 ctx->Driver.NewProgram(ctx, GL_VERTEX_PROGRAM_ARB, 0); 1471 1472 create_new_program( key, ctx->_TnlProgram, 1473 ctx->Const.MaxVertexProgramTemps ); 1474 1475 cache_item(&tnl->vp_cache, hash, key, ctx->_TnlProgram ); 1476 } 1477 else { 1478 if (0) 1479 _mesa_printf("Found existing TNL program for key %x\n", hash); 1480 } 1481 1482 /* Need a BindProgram callback for the driver? 1483 */ 1484} 1485 1486 1487void _tnl_ProgramCacheDestroy( GLcontext *ctx ) 1488{ 1489 TNLcontext *tnl = TNL_CONTEXT(ctx); 1490 struct tnl_cache *a, *tmp; 1491 1492 for (a = tnl->vp_cache ; a; a = tmp) { 1493 tmp = a->next; 1494 FREE(a->key); 1495 FREE(a->data); 1496 FREE(a); 1497 } 1498} 1499