i915_fragprog.c revision 55aaee602078a8a57681cd4c205a71048fd184fe
1/************************************************************************** 2 * 3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas. 4 * All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sub license, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice (including the 15 * next paragraph) shall be included in all copies or substantial portions 16 * 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 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR 22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 * 26 **************************************************************************/ 27 28#include "main/glheader.h" 29#include "main/macros.h" 30#include "main/enums.h" 31 32#include "program/prog_instruction.h" 33#include "program/prog_parameter.h" 34#include "program/program.h" 35#include "program/programopt.h" 36#include "program/prog_print.h" 37 38#include "tnl/tnl.h" 39#include "tnl/t_context.h" 40 41#include "intel_batchbuffer.h" 42 43#include "i915_reg.h" 44#include "i915_context.h" 45#include "i915_program.h" 46 47static const GLfloat sin_quad_constants[2][4] = { 48 { 49 2.0, 50 -1.0, 51 .5, 52 .75 53 }, 54 { 55 4.0, 56 -4.0, 57 1.0 / (2.0 * M_PI), 58 .2225 59 } 60}; 61 62static const GLfloat sin_constants[4] = { 1.0, 63 -1.0 / (3 * 2 * 1), 64 1.0 / (5 * 4 * 3 * 2 * 1), 65 -1.0 / (7 * 6 * 5 * 4 * 3 * 2 * 1) 66}; 67 68/* 1, -1/2!, 1/4!, -1/6! */ 69static const GLfloat cos_constants[4] = { 1.0, 70 -1.0 / (2 * 1), 71 1.0 / (4 * 3 * 2 * 1), 72 -1.0 / (6 * 5 * 4 * 3 * 2 * 1) 73}; 74 75/** 76 * Retrieve a ureg for the given source register. Will emit 77 * constants, apply swizzling and negation as needed. 78 */ 79static GLuint 80src_vector(struct i915_fragment_program *p, 81 const struct prog_src_register *source, 82 const struct gl_fragment_program *program) 83{ 84 GLuint src; 85 86 switch (source->File) { 87 88 /* Registers: 89 */ 90 case PROGRAM_TEMPORARY: 91 if (source->Index >= I915_MAX_TEMPORARY) { 92 i915_program_error(p, "Exceeded max temporary reg: %d/%d", 93 source->Index, I915_MAX_TEMPORARY); 94 return 0; 95 } 96 src = UREG(REG_TYPE_R, source->Index); 97 break; 98 case PROGRAM_INPUT: 99 switch (source->Index) { 100 case FRAG_ATTRIB_WPOS: 101 src = i915_emit_decl(p, REG_TYPE_T, p->wpos_tex, D0_CHANNEL_ALL); 102 break; 103 case FRAG_ATTRIB_COL0: 104 src = i915_emit_decl(p, REG_TYPE_T, T_DIFFUSE, D0_CHANNEL_ALL); 105 break; 106 case FRAG_ATTRIB_COL1: 107 src = i915_emit_decl(p, REG_TYPE_T, T_SPECULAR, D0_CHANNEL_XYZ); 108 src = swizzle(src, X, Y, Z, ONE); 109 break; 110 case FRAG_ATTRIB_FOGC: 111 src = i915_emit_decl(p, REG_TYPE_T, T_FOG_W, D0_CHANNEL_W); 112 src = swizzle(src, W, ZERO, ZERO, ONE); 113 break; 114 case FRAG_ATTRIB_TEX0: 115 case FRAG_ATTRIB_TEX1: 116 case FRAG_ATTRIB_TEX2: 117 case FRAG_ATTRIB_TEX3: 118 case FRAG_ATTRIB_TEX4: 119 case FRAG_ATTRIB_TEX5: 120 case FRAG_ATTRIB_TEX6: 121 case FRAG_ATTRIB_TEX7: 122 src = i915_emit_decl(p, REG_TYPE_T, 123 T_TEX0 + (source->Index - FRAG_ATTRIB_TEX0), 124 D0_CHANNEL_ALL); 125 break; 126 127 case FRAG_ATTRIB_VAR0: 128 case FRAG_ATTRIB_VAR0 + 1: 129 case FRAG_ATTRIB_VAR0 + 2: 130 case FRAG_ATTRIB_VAR0 + 3: 131 case FRAG_ATTRIB_VAR0 + 4: 132 case FRAG_ATTRIB_VAR0 + 5: 133 case FRAG_ATTRIB_VAR0 + 6: 134 case FRAG_ATTRIB_VAR0 + 7: 135 src = i915_emit_decl(p, REG_TYPE_T, 136 T_TEX0 + (source->Index - FRAG_ATTRIB_VAR0), 137 D0_CHANNEL_ALL); 138 break; 139 140 default: 141 i915_program_error(p, "Bad source->Index: %d", source->Index); 142 return 0; 143 } 144 break; 145 146 case PROGRAM_OUTPUT: 147 switch (source->Index) { 148 case FRAG_RESULT_COLOR: 149 src = UREG(REG_TYPE_OC, 0); 150 break; 151 case FRAG_RESULT_DEPTH: 152 src = UREG(REG_TYPE_OD, 0); 153 break; 154 default: 155 i915_program_error(p, "Bad source->Index: %d", source->Index); 156 return 0; 157 } 158 break; 159 160 /* Various paramters and env values. All emitted to 161 * hardware as program constants. 162 */ 163 case PROGRAM_LOCAL_PARAM: 164 src = i915_emit_param4fv(p, program->Base.LocalParams[source->Index]); 165 break; 166 167 case PROGRAM_ENV_PARAM: 168 src = 169 i915_emit_param4fv(p, 170 p->ctx->FragmentProgram.Parameters[source-> 171 Index]); 172 break; 173 174 case PROGRAM_CONSTANT: 175 case PROGRAM_STATE_VAR: 176 case PROGRAM_NAMED_PARAM: 177 case PROGRAM_UNIFORM: 178 src = 179 i915_emit_param4fv(p, 180 program->Base.Parameters->ParameterValues[source-> 181 Index]); 182 break; 183 184 default: 185 i915_program_error(p, "Bad source->File: %d", source->File); 186 return 0; 187 } 188 189 src = swizzle(src, 190 GET_SWZ(source->Swizzle, 0), 191 GET_SWZ(source->Swizzle, 1), 192 GET_SWZ(source->Swizzle, 2), GET_SWZ(source->Swizzle, 3)); 193 194 if (source->Negate) 195 src = negate(src, 196 GET_BIT(source->Negate, 0), 197 GET_BIT(source->Negate, 1), 198 GET_BIT(source->Negate, 2), 199 GET_BIT(source->Negate, 3)); 200 201 return src; 202} 203 204 205static GLuint 206get_result_vector(struct i915_fragment_program *p, 207 const struct prog_instruction *inst) 208{ 209 switch (inst->DstReg.File) { 210 case PROGRAM_OUTPUT: 211 switch (inst->DstReg.Index) { 212 case FRAG_RESULT_COLOR: 213 return UREG(REG_TYPE_OC, 0); 214 case FRAG_RESULT_DEPTH: 215 p->depth_written = 1; 216 return UREG(REG_TYPE_OD, 0); 217 default: 218 i915_program_error(p, "Bad inst->DstReg.Index: %d", 219 inst->DstReg.Index); 220 return 0; 221 } 222 case PROGRAM_TEMPORARY: 223 return UREG(REG_TYPE_R, inst->DstReg.Index); 224 default: 225 i915_program_error(p, "Bad inst->DstReg.File: %d", inst->DstReg.File); 226 return 0; 227 } 228} 229 230static GLuint 231get_result_flags(const struct prog_instruction *inst) 232{ 233 GLuint flags = 0; 234 235 if (inst->SaturateMode == SATURATE_ZERO_ONE) 236 flags |= A0_DEST_SATURATE; 237 if (inst->DstReg.WriteMask & WRITEMASK_X) 238 flags |= A0_DEST_CHANNEL_X; 239 if (inst->DstReg.WriteMask & WRITEMASK_Y) 240 flags |= A0_DEST_CHANNEL_Y; 241 if (inst->DstReg.WriteMask & WRITEMASK_Z) 242 flags |= A0_DEST_CHANNEL_Z; 243 if (inst->DstReg.WriteMask & WRITEMASK_W) 244 flags |= A0_DEST_CHANNEL_W; 245 246 return flags; 247} 248 249static GLuint 250translate_tex_src_target(struct i915_fragment_program *p, GLubyte bit) 251{ 252 switch (bit) { 253 case TEXTURE_1D_INDEX: 254 return D0_SAMPLE_TYPE_2D; 255 case TEXTURE_2D_INDEX: 256 return D0_SAMPLE_TYPE_2D; 257 case TEXTURE_RECT_INDEX: 258 return D0_SAMPLE_TYPE_2D; 259 case TEXTURE_3D_INDEX: 260 return D0_SAMPLE_TYPE_VOLUME; 261 case TEXTURE_CUBE_INDEX: 262 return D0_SAMPLE_TYPE_CUBE; 263 default: 264 i915_program_error(p, "TexSrcBit: %d", bit); 265 return 0; 266 } 267} 268 269#define EMIT_TEX( OP ) \ 270do { \ 271 GLuint dim = translate_tex_src_target( p, inst->TexSrcTarget ); \ 272 const struct gl_fragment_program *program = p->ctx->FragmentProgram._Current; \ 273 GLuint unit = program->Base.SamplerUnits[inst->TexSrcUnit]; \ 274 GLuint sampler = i915_emit_decl(p, REG_TYPE_S, \ 275 unit, dim); \ 276 GLuint coord = src_vector( p, &inst->SrcReg[0], program); \ 277 /* Texel lookup */ \ 278 \ 279 i915_emit_texld( p, get_live_regs(p, inst), \ 280 get_result_vector( p, inst ), \ 281 get_result_flags( inst ), \ 282 sampler, \ 283 coord, \ 284 OP); \ 285} while (0) 286 287#define EMIT_ARITH( OP, N ) \ 288do { \ 289 i915_emit_arith( p, \ 290 OP, \ 291 get_result_vector( p, inst ), \ 292 get_result_flags( inst ), 0, \ 293 (N<1)?0:src_vector( p, &inst->SrcReg[0], program), \ 294 (N<2)?0:src_vector( p, &inst->SrcReg[1], program), \ 295 (N<3)?0:src_vector( p, &inst->SrcReg[2], program)); \ 296} while (0) 297 298#define EMIT_1ARG_ARITH( OP ) EMIT_ARITH( OP, 1 ) 299#define EMIT_2ARG_ARITH( OP ) EMIT_ARITH( OP, 2 ) 300#define EMIT_3ARG_ARITH( OP ) EMIT_ARITH( OP, 3 ) 301 302/* 303 * TODO: consider moving this into core 304 */ 305static void calc_live_regs( struct i915_fragment_program *p ) 306{ 307 const struct gl_fragment_program *program = p->ctx->FragmentProgram._Current; 308 GLuint regsUsed = 0xffff0000; 309 GLint i; 310 311 for (i = program->Base.NumInstructions - 1; i >= 0; i--) { 312 struct prog_instruction *inst = &program->Base.Instructions[i]; 313 int opArgs = _mesa_num_inst_src_regs(inst->Opcode); 314 int a; 315 316 /* Register is written to: unmark as live for this and preceeding ops */ 317 if (inst->DstReg.File == PROGRAM_TEMPORARY) 318 regsUsed &= ~(1 << inst->DstReg.Index); 319 320 for (a = 0; a < opArgs; a++) { 321 /* Register is read from: mark as live for this and preceeding ops */ 322 if (inst->SrcReg[a].File == PROGRAM_TEMPORARY) 323 regsUsed |= 1 << inst->SrcReg[a].Index; 324 } 325 326 p->usedRegs[i] = regsUsed; 327 } 328} 329 330static GLuint get_live_regs( struct i915_fragment_program *p, 331 const struct prog_instruction *inst ) 332{ 333 const struct gl_fragment_program *program = p->ctx->FragmentProgram._Current; 334 GLuint nr = inst - program->Base.Instructions; 335 336 return p->usedRegs[nr]; 337} 338 339 340/* Possible concerns: 341 * 342 * SIN, COS -- could use another taylor step? 343 * LIT -- results seem a little different to sw mesa 344 * LOG -- different to mesa on negative numbers, but this is conformant. 345 * 346 * Parse failures -- Mesa doesn't currently give a good indication 347 * internally whether a particular program string parsed or not. This 348 * can lead to confusion -- hopefully we cope with it ok now. 349 * 350 */ 351static void 352upload_program(struct i915_fragment_program *p) 353{ 354 const struct gl_fragment_program *program = 355 p->ctx->FragmentProgram._Current; 356 const struct prog_instruction *inst = program->Base.Instructions; 357 358 if (INTEL_DEBUG & DEBUG_WM) 359 _mesa_print_program(&program->Base); 360 361 /* Is this a parse-failed program? Ensure a valid program is 362 * loaded, as the flagging of an error isn't sufficient to stop 363 * this being uploaded to hardware. 364 */ 365 if (inst[0].Opcode == OPCODE_END) { 366 GLuint tmp = i915_get_utemp(p); 367 i915_emit_arith(p, 368 A0_MOV, 369 UREG(REG_TYPE_OC, 0), 370 A0_DEST_CHANNEL_ALL, 0, 371 swizzle(tmp, ONE, ZERO, ONE, ONE), 0, 0); 372 return; 373 } 374 375 if (program->Base.NumInstructions > I915_MAX_INSN) { 376 i915_program_error(p, "Exceeded max instructions (%d out of %d)", 377 program->Base.NumInstructions, I915_MAX_INSN); 378 return; 379 } 380 381 /* Not always needed: 382 */ 383 calc_live_regs(p); 384 385 while (1) { 386 GLuint src0, src1, src2, flags; 387 GLuint tmp = 0, dst, consts0 = 0, consts1 = 0; 388 389 switch (inst->Opcode) { 390 case OPCODE_ABS: 391 src0 = src_vector(p, &inst->SrcReg[0], program); 392 i915_emit_arith(p, 393 A0_MAX, 394 get_result_vector(p, inst), 395 get_result_flags(inst), 0, 396 src0, negate(src0, 1, 1, 1, 1), 0); 397 break; 398 399 case OPCODE_ADD: 400 EMIT_2ARG_ARITH(A0_ADD); 401 break; 402 403 case OPCODE_CMP: 404 src0 = src_vector(p, &inst->SrcReg[0], program); 405 src1 = src_vector(p, &inst->SrcReg[1], program); 406 src2 = src_vector(p, &inst->SrcReg[2], program); 407 i915_emit_arith(p, A0_CMP, get_result_vector(p, inst), get_result_flags(inst), 0, src0, src2, src1); /* NOTE: order of src2, src1 */ 408 break; 409 410 case OPCODE_COS: 411 src0 = src_vector(p, &inst->SrcReg[0], program); 412 tmp = i915_get_utemp(p); 413 consts0 = i915_emit_const4fv(p, sin_quad_constants[0]); 414 consts1 = i915_emit_const4fv(p, sin_quad_constants[1]); 415 416 /* Reduce range from repeating about [-pi,pi] to [-1,1] */ 417 i915_emit_arith(p, 418 A0_MAD, 419 tmp, A0_DEST_CHANNEL_X, 0, 420 src0, 421 swizzle(consts1, Z, ZERO, ZERO, ZERO), /* 1/(2pi) */ 422 swizzle(consts0, W, ZERO, ZERO, ZERO)); /* .75 */ 423 424 i915_emit_arith(p, A0_FRC, tmp, A0_DEST_CHANNEL_X, 0, tmp, 0, 0); 425 426 i915_emit_arith(p, 427 A0_MAD, 428 tmp, A0_DEST_CHANNEL_X, 0, 429 tmp, 430 swizzle(consts0, X, ZERO, ZERO, ZERO), /* 2 */ 431 swizzle(consts0, Y, ZERO, ZERO, ZERO)); /* -1 */ 432 433 /* Compute COS with the same calculation used for SIN, but a 434 * different source range has been mapped to [-1,1] this time. 435 */ 436 437 /* tmp.y = abs(tmp.x); {x, abs(x), 0, 0} */ 438 i915_emit_arith(p, 439 A0_MAX, 440 tmp, A0_DEST_CHANNEL_Y, 0, 441 swizzle(tmp, ZERO, X, ZERO, ZERO), 442 negate(swizzle(tmp, ZERO, X, ZERO, ZERO), 0, 1, 0, 0), 443 0); 444 445 /* tmp.y = tmp.y * tmp.x; {x, x * abs(x), 0, 0} */ 446 i915_emit_arith(p, 447 A0_MUL, 448 tmp, A0_DEST_CHANNEL_Y, 0, 449 swizzle(tmp, ZERO, X, ZERO, ZERO), 450 tmp, 451 0); 452 453 /* tmp.x = tmp.xy DP sin_quad_constants[2].xy */ 454 i915_emit_arith(p, 455 A0_DP3, 456 tmp, A0_DEST_CHANNEL_X, 0, 457 tmp, 458 swizzle(consts1, X, Y, ZERO, ZERO), 459 0); 460 461 /* tmp.x now contains a first approximation (y). Now, weight it 462 * against tmp.y**2 to get closer. 463 */ 464 i915_emit_arith(p, 465 A0_MAX, 466 tmp, A0_DEST_CHANNEL_Y, 0, 467 swizzle(tmp, ZERO, X, ZERO, ZERO), 468 negate(swizzle(tmp, ZERO, X, ZERO, ZERO), 0, 1, 0, 0), 469 0); 470 471 /* tmp.y = tmp.x * tmp.y - tmp.x; {y, y * abs(y) - y, 0, 0} */ 472 i915_emit_arith(p, 473 A0_MAD, 474 tmp, A0_DEST_CHANNEL_Y, 0, 475 swizzle(tmp, ZERO, X, ZERO, ZERO), 476 swizzle(tmp, ZERO, Y, ZERO, ZERO), 477 negate(swizzle(tmp, ZERO, X, ZERO, ZERO), 0, 1, 0, 0)); 478 479 /* result = .2225 * tmp.y + tmp.x =.2225(y * abs(y) - y) + y= */ 480 i915_emit_arith(p, 481 A0_MAD, 482 get_result_vector(p, inst), 483 get_result_flags(inst), 0, 484 swizzle(consts1, W, W, W, W), 485 swizzle(tmp, Y, Y, Y, Y), 486 swizzle(tmp, X, X, X, X)); 487 break; 488 489 case OPCODE_DP2: 490 src0 = src_vector(p, &inst->SrcReg[0], program); 491 src1 = src_vector(p, &inst->SrcReg[1], program); 492 i915_emit_arith(p, 493 A0_DP3, 494 get_result_vector(p, inst), 495 get_result_flags(inst), 0, 496 swizzle(src0, X, Y, ZERO, ZERO), 497 swizzle(src1, X, Y, ZERO, ZERO), 498 0); 499 break; 500 501 case OPCODE_DP3: 502 EMIT_2ARG_ARITH(A0_DP3); 503 break; 504 505 case OPCODE_DP4: 506 EMIT_2ARG_ARITH(A0_DP4); 507 break; 508 509 case OPCODE_DPH: 510 src0 = src_vector(p, &inst->SrcReg[0], program); 511 src1 = src_vector(p, &inst->SrcReg[1], program); 512 513 i915_emit_arith(p, 514 A0_DP4, 515 get_result_vector(p, inst), 516 get_result_flags(inst), 0, 517 swizzle(src0, X, Y, Z, ONE), src1, 0); 518 break; 519 520 case OPCODE_DST: 521 src0 = src_vector(p, &inst->SrcReg[0], program); 522 src1 = src_vector(p, &inst->SrcReg[1], program); 523 524 /* result[0] = 1 * 1; 525 * result[1] = a[1] * b[1]; 526 * result[2] = a[2] * 1; 527 * result[3] = 1 * b[3]; 528 */ 529 i915_emit_arith(p, 530 A0_MUL, 531 get_result_vector(p, inst), 532 get_result_flags(inst), 0, 533 swizzle(src0, ONE, Y, Z, ONE), 534 swizzle(src1, ONE, Y, ONE, W), 0); 535 break; 536 537 case OPCODE_EX2: 538 src0 = src_vector(p, &inst->SrcReg[0], program); 539 540 i915_emit_arith(p, 541 A0_EXP, 542 get_result_vector(p, inst), 543 get_result_flags(inst), 0, 544 swizzle(src0, X, X, X, X), 0, 0); 545 break; 546 547 case OPCODE_FLR: 548 EMIT_1ARG_ARITH(A0_FLR); 549 break; 550 551 case OPCODE_TRUNC: 552 EMIT_1ARG_ARITH(A0_TRC); 553 break; 554 555 case OPCODE_FRC: 556 EMIT_1ARG_ARITH(A0_FRC); 557 break; 558 559 case OPCODE_KIL: 560 src0 = src_vector(p, &inst->SrcReg[0], program); 561 tmp = i915_get_utemp(p); 562 563 i915_emit_texld(p, get_live_regs(p, inst), 564 tmp, A0_DEST_CHANNEL_ALL, /* use a dummy dest reg */ 565 0, src0, T0_TEXKILL); 566 break; 567 568 case OPCODE_KIL_NV: 569 if (inst->DstReg.CondMask == COND_TR) { 570 tmp = i915_get_utemp(p); 571 572 i915_emit_texld(p, get_live_regs(p, inst), 573 tmp, A0_DEST_CHANNEL_ALL, 574 0, /* use a dummy dest reg */ 575 swizzle(tmp, ONE, ONE, ONE, ONE), /* always */ 576 T0_TEXKILL); 577 } else { 578 p->error = 1; 579 i915_program_error(p, "Unsupported KIL_NV condition code: %d", 580 inst->DstReg.CondMask); 581 } 582 break; 583 584 case OPCODE_LG2: 585 src0 = src_vector(p, &inst->SrcReg[0], program); 586 587 i915_emit_arith(p, 588 A0_LOG, 589 get_result_vector(p, inst), 590 get_result_flags(inst), 0, 591 swizzle(src0, X, X, X, X), 0, 0); 592 break; 593 594 case OPCODE_LIT: 595 src0 = src_vector(p, &inst->SrcReg[0], program); 596 tmp = i915_get_utemp(p); 597 598 /* tmp = max( a.xyzw, a.00zw ) 599 * XXX: Clamp tmp.w to -128..128 600 * tmp.y = log(tmp.y) 601 * tmp.y = tmp.w * tmp.y 602 * tmp.y = exp(tmp.y) 603 * result = cmp (a.11-x1, a.1x01, a.1xy1 ) 604 */ 605 i915_emit_arith(p, A0_MAX, tmp, A0_DEST_CHANNEL_ALL, 0, 606 src0, swizzle(src0, ZERO, ZERO, Z, W), 0); 607 608 i915_emit_arith(p, A0_LOG, tmp, A0_DEST_CHANNEL_Y, 0, 609 swizzle(tmp, Y, Y, Y, Y), 0, 0); 610 611 i915_emit_arith(p, A0_MUL, tmp, A0_DEST_CHANNEL_Y, 0, 612 swizzle(tmp, ZERO, Y, ZERO, ZERO), 613 swizzle(tmp, ZERO, W, ZERO, ZERO), 0); 614 615 i915_emit_arith(p, A0_EXP, tmp, A0_DEST_CHANNEL_Y, 0, 616 swizzle(tmp, Y, Y, Y, Y), 0, 0); 617 618 i915_emit_arith(p, A0_CMP, 619 get_result_vector(p, inst), 620 get_result_flags(inst), 0, 621 negate(swizzle(tmp, ONE, ONE, X, ONE), 0, 0, 1, 0), 622 swizzle(tmp, ONE, X, ZERO, ONE), 623 swizzle(tmp, ONE, X, Y, ONE)); 624 625 break; 626 627 case OPCODE_LRP: 628 src0 = src_vector(p, &inst->SrcReg[0], program); 629 src1 = src_vector(p, &inst->SrcReg[1], program); 630 src2 = src_vector(p, &inst->SrcReg[2], program); 631 flags = get_result_flags(inst); 632 tmp = i915_get_utemp(p); 633 634 /* b*a + c*(1-a) 635 * 636 * b*a + c - ca 637 * 638 * tmp = b*a + c, 639 * result = (-c)*a + tmp 640 */ 641 i915_emit_arith(p, A0_MAD, tmp, 642 flags & A0_DEST_CHANNEL_ALL, 0, src1, src0, src2); 643 644 i915_emit_arith(p, A0_MAD, 645 get_result_vector(p, inst), 646 flags, 0, negate(src2, 1, 1, 1, 1), src0, tmp); 647 break; 648 649 case OPCODE_MAD: 650 EMIT_3ARG_ARITH(A0_MAD); 651 break; 652 653 case OPCODE_MAX: 654 EMIT_2ARG_ARITH(A0_MAX); 655 break; 656 657 case OPCODE_MIN: 658 src0 = src_vector(p, &inst->SrcReg[0], program); 659 src1 = src_vector(p, &inst->SrcReg[1], program); 660 tmp = i915_get_utemp(p); 661 flags = get_result_flags(inst); 662 663 i915_emit_arith(p, 664 A0_MAX, 665 tmp, flags & A0_DEST_CHANNEL_ALL, 0, 666 negate(src0, 1, 1, 1, 1), 667 negate(src1, 1, 1, 1, 1), 0); 668 669 i915_emit_arith(p, 670 A0_MOV, 671 get_result_vector(p, inst), 672 flags, 0, negate(tmp, 1, 1, 1, 1), 0, 0); 673 break; 674 675 case OPCODE_MOV: 676 EMIT_1ARG_ARITH(A0_MOV); 677 break; 678 679 case OPCODE_MUL: 680 EMIT_2ARG_ARITH(A0_MUL); 681 break; 682 683 case OPCODE_POW: 684 src0 = src_vector(p, &inst->SrcReg[0], program); 685 src1 = src_vector(p, &inst->SrcReg[1], program); 686 tmp = i915_get_utemp(p); 687 flags = get_result_flags(inst); 688 689 /* XXX: masking on intermediate values, here and elsewhere. 690 */ 691 i915_emit_arith(p, 692 A0_LOG, 693 tmp, A0_DEST_CHANNEL_X, 0, 694 swizzle(src0, X, X, X, X), 0, 0); 695 696 i915_emit_arith(p, A0_MUL, tmp, A0_DEST_CHANNEL_X, 0, tmp, src1, 0); 697 698 699 i915_emit_arith(p, 700 A0_EXP, 701 get_result_vector(p, inst), 702 flags, 0, swizzle(tmp, X, X, X, X), 0, 0); 703 704 break; 705 706 case OPCODE_RCP: 707 src0 = src_vector(p, &inst->SrcReg[0], program); 708 709 i915_emit_arith(p, 710 A0_RCP, 711 get_result_vector(p, inst), 712 get_result_flags(inst), 0, 713 swizzle(src0, X, X, X, X), 0, 0); 714 break; 715 716 case OPCODE_RSQ: 717 718 src0 = src_vector(p, &inst->SrcReg[0], program); 719 720 i915_emit_arith(p, 721 A0_RSQ, 722 get_result_vector(p, inst), 723 get_result_flags(inst), 0, 724 swizzle(src0, X, X, X, X), 0, 0); 725 break; 726 727 case OPCODE_SCS: 728 src0 = src_vector(p, &inst->SrcReg[0], program); 729 tmp = i915_get_utemp(p); 730 731 /* 732 * t0.xy = MUL x.xx11, x.x1111 ; x^2, x, 1, 1 733 * t0 = MUL t0.xyxy t0.xx11 ; x^4, x^3, x^2, x 734 * t1 = MUL t0.xyyw t0.yz11 ; x^7 x^5 x^3 x 735 * scs.x = DP4 t1, sin_constants 736 * t1 = MUL t0.xxz1 t0.z111 ; x^6 x^4 x^2 1 737 * scs.y = DP4 t1, cos_constants 738 */ 739 i915_emit_arith(p, 740 A0_MUL, 741 tmp, A0_DEST_CHANNEL_XY, 0, 742 swizzle(src0, X, X, ONE, ONE), 743 swizzle(src0, X, ONE, ONE, ONE), 0); 744 745 i915_emit_arith(p, 746 A0_MUL, 747 tmp, A0_DEST_CHANNEL_ALL, 0, 748 swizzle(tmp, X, Y, X, Y), 749 swizzle(tmp, X, X, ONE, ONE), 0); 750 751 if (inst->DstReg.WriteMask & WRITEMASK_Y) { 752 GLuint tmp1; 753 754 if (inst->DstReg.WriteMask & WRITEMASK_X) 755 tmp1 = i915_get_utemp(p); 756 else 757 tmp1 = tmp; 758 759 i915_emit_arith(p, 760 A0_MUL, 761 tmp1, A0_DEST_CHANNEL_ALL, 0, 762 swizzle(tmp, X, Y, Y, W), 763 swizzle(tmp, X, Z, ONE, ONE), 0); 764 765 i915_emit_arith(p, 766 A0_DP4, 767 get_result_vector(p, inst), 768 A0_DEST_CHANNEL_Y, 0, 769 swizzle(tmp1, W, Z, Y, X), 770 i915_emit_const4fv(p, sin_constants), 0); 771 } 772 773 if (inst->DstReg.WriteMask & WRITEMASK_X) { 774 i915_emit_arith(p, 775 A0_MUL, 776 tmp, A0_DEST_CHANNEL_XYZ, 0, 777 swizzle(tmp, X, X, Z, ONE), 778 swizzle(tmp, Z, ONE, ONE, ONE), 0); 779 780 i915_emit_arith(p, 781 A0_DP4, 782 get_result_vector(p, inst), 783 A0_DEST_CHANNEL_X, 0, 784 swizzle(tmp, ONE, Z, Y, X), 785 i915_emit_const4fv(p, cos_constants), 0); 786 } 787 break; 788 789 case OPCODE_SEQ: 790 tmp = i915_get_utemp(p); 791 flags = get_result_flags(inst); 792 dst = get_result_vector(p, inst); 793 794 /* dst = src1 >= src2 */ 795 i915_emit_arith(p, 796 A0_SGE, 797 dst, 798 flags, 0, 799 src_vector(p, &inst->SrcReg[0], program), 800 src_vector(p, &inst->SrcReg[1], program), 801 0); 802 /* tmp = src1 <= src2 */ 803 i915_emit_arith(p, 804 A0_SGE, 805 tmp, 806 flags, 0, 807 negate(src_vector(p, &inst->SrcReg[0], program), 808 1, 1, 1, 1), 809 negate(src_vector(p, &inst->SrcReg[1], program), 810 1, 1, 1, 1), 811 0); 812 /* dst = tmp && dst */ 813 i915_emit_arith(p, 814 A0_MUL, 815 dst, 816 flags, 0, 817 dst, 818 tmp, 819 0); 820 break; 821 822 case OPCODE_SIN: 823 src0 = src_vector(p, &inst->SrcReg[0], program); 824 tmp = i915_get_utemp(p); 825 consts0 = i915_emit_const4fv(p, sin_quad_constants[0]); 826 consts1 = i915_emit_const4fv(p, sin_quad_constants[1]); 827 828 /* Reduce range from repeating about [-pi,pi] to [-1,1] */ 829 i915_emit_arith(p, 830 A0_MAD, 831 tmp, A0_DEST_CHANNEL_X, 0, 832 src0, 833 swizzle(consts1, Z, ZERO, ZERO, ZERO), /* 1/(2pi) */ 834 swizzle(consts0, Z, ZERO, ZERO, ZERO)); /* .5 */ 835 836 i915_emit_arith(p, A0_FRC, tmp, A0_DEST_CHANNEL_X, 0, tmp, 0, 0); 837 838 i915_emit_arith(p, 839 A0_MAD, 840 tmp, A0_DEST_CHANNEL_X, 0, 841 tmp, 842 swizzle(consts0, X, ZERO, ZERO, ZERO), /* 2 */ 843 swizzle(consts0, Y, ZERO, ZERO, ZERO)); /* -1 */ 844 845 /* Compute sin using a quadratic and quartic. It gives continuity 846 * that repeating the Taylor series lacks every 2*pi, and has 847 * reduced error. 848 * 849 * The idea was described at: 850 * http://www.devmaster.net/forums/showthread.php?t=5784 851 */ 852 853 /* tmp.y = abs(tmp.x); {x, abs(x), 0, 0} */ 854 i915_emit_arith(p, 855 A0_MAX, 856 tmp, A0_DEST_CHANNEL_Y, 0, 857 swizzle(tmp, ZERO, X, ZERO, ZERO), 858 negate(swizzle(tmp, ZERO, X, ZERO, ZERO), 0, 1, 0, 0), 859 0); 860 861 /* tmp.y = tmp.y * tmp.x; {x, x * abs(x), 0, 0} */ 862 i915_emit_arith(p, 863 A0_MUL, 864 tmp, A0_DEST_CHANNEL_Y, 0, 865 swizzle(tmp, ZERO, X, ZERO, ZERO), 866 tmp, 867 0); 868 869 /* tmp.x = tmp.xy DP sin_quad_constants[2].xy */ 870 i915_emit_arith(p, 871 A0_DP3, 872 tmp, A0_DEST_CHANNEL_X, 0, 873 tmp, 874 swizzle(consts1, X, Y, ZERO, ZERO), 875 0); 876 877 /* tmp.x now contains a first approximation (y). Now, weight it 878 * against tmp.y**2 to get closer. 879 */ 880 i915_emit_arith(p, 881 A0_MAX, 882 tmp, A0_DEST_CHANNEL_Y, 0, 883 swizzle(tmp, ZERO, X, ZERO, ZERO), 884 negate(swizzle(tmp, ZERO, X, ZERO, ZERO), 0, 1, 0, 0), 885 0); 886 887 /* tmp.y = tmp.x * tmp.y - tmp.x; {y, y * abs(y) - y, 0, 0} */ 888 i915_emit_arith(p, 889 A0_MAD, 890 tmp, A0_DEST_CHANNEL_Y, 0, 891 swizzle(tmp, ZERO, X, ZERO, ZERO), 892 swizzle(tmp, ZERO, Y, ZERO, ZERO), 893 negate(swizzle(tmp, ZERO, X, ZERO, ZERO), 0, 1, 0, 0)); 894 895 /* result = .2225 * tmp.y + tmp.x =.2225(y * abs(y) - y) + y= */ 896 i915_emit_arith(p, 897 A0_MAD, 898 get_result_vector(p, inst), 899 get_result_flags(inst), 0, 900 swizzle(consts1, W, W, W, W), 901 swizzle(tmp, Y, Y, Y, Y), 902 swizzle(tmp, X, X, X, X)); 903 904 break; 905 906 case OPCODE_SGE: 907 EMIT_2ARG_ARITH(A0_SGE); 908 break; 909 910 case OPCODE_SGT: 911 i915_emit_arith(p, 912 A0_SLT, 913 get_result_vector( p, inst ), 914 get_result_flags( inst ), 0, 915 negate(src_vector( p, &inst->SrcReg[0], program), 916 1, 1, 1, 1), 917 negate(src_vector( p, &inst->SrcReg[1], program), 918 1, 1, 1, 1), 919 0); 920 break; 921 922 case OPCODE_SLE: 923 i915_emit_arith(p, 924 A0_SGE, 925 get_result_vector( p, inst ), 926 get_result_flags( inst ), 0, 927 negate(src_vector( p, &inst->SrcReg[0], program), 928 1, 1, 1, 1), 929 negate(src_vector( p, &inst->SrcReg[1], program), 930 1, 1, 1, 1), 931 0); 932 break; 933 934 case OPCODE_SLT: 935 EMIT_2ARG_ARITH(A0_SLT); 936 break; 937 938 case OPCODE_SNE: 939 tmp = i915_get_utemp(p); 940 flags = get_result_flags(inst); 941 dst = get_result_vector(p, inst); 942 943 /* dst = src1 < src2 */ 944 i915_emit_arith(p, 945 A0_SLT, 946 dst, 947 flags, 0, 948 src_vector(p, &inst->SrcReg[0], program), 949 src_vector(p, &inst->SrcReg[1], program), 950 0); 951 /* tmp = src1 > src2 */ 952 i915_emit_arith(p, 953 A0_SLT, 954 tmp, 955 flags, 0, 956 negate(src_vector(p, &inst->SrcReg[0], program), 957 1, 1, 1, 1), 958 negate(src_vector(p, &inst->SrcReg[1], program), 959 1, 1, 1, 1), 960 0); 961 /* dst = tmp || dst */ 962 i915_emit_arith(p, 963 A0_ADD, 964 dst, 965 flags | A0_DEST_SATURATE, 0, 966 dst, 967 tmp, 968 0); 969 break; 970 971 case OPCODE_SSG: 972 dst = get_result_vector(p, inst); 973 flags = get_result_flags(inst); 974 src0 = src_vector(p, &inst->SrcReg[0], program); 975 tmp = i915_get_utemp(p); 976 977 /* tmp = (src < 0.0) */ 978 i915_emit_arith(p, 979 A0_SLT, 980 tmp, 981 flags, 0, 982 src0, 983 swizzle(src0, ZERO, ZERO, ZERO, ZERO), 984 0); 985 986 /* dst = (0.0 < src) */ 987 i915_emit_arith(p, 988 A0_SLT, 989 dst, 990 flags, 0, 991 swizzle(src0, ZERO, ZERO, ZERO, ZERO), 992 src0, 993 0); 994 995 /* dst = (src > 0.0) - (src < 0.0) */ 996 i915_emit_arith(p, 997 A0_ADD, 998 dst, 999 flags, 0, 1000 dst, 1001 negate(tmp, 1, 1, 1, 1), 1002 0); 1003 1004 break; 1005 1006 case OPCODE_SUB: 1007 src0 = src_vector(p, &inst->SrcReg[0], program); 1008 src1 = src_vector(p, &inst->SrcReg[1], program); 1009 1010 i915_emit_arith(p, 1011 A0_ADD, 1012 get_result_vector(p, inst), 1013 get_result_flags(inst), 0, 1014 src0, negate(src1, 1, 1, 1, 1), 0); 1015 break; 1016 1017 case OPCODE_SWZ: 1018 EMIT_1ARG_ARITH(A0_MOV); /* extended swizzle handled natively */ 1019 break; 1020 1021 case OPCODE_TEX: 1022 EMIT_TEX(T0_TEXLD); 1023 break; 1024 1025 case OPCODE_TXB: 1026 EMIT_TEX(T0_TEXLDB); 1027 break; 1028 1029 case OPCODE_TXP: 1030 EMIT_TEX(T0_TEXLDP); 1031 break; 1032 1033 case OPCODE_XPD: 1034 /* Cross product: 1035 * result.x = src0.y * src1.z - src0.z * src1.y; 1036 * result.y = src0.z * src1.x - src0.x * src1.z; 1037 * result.z = src0.x * src1.y - src0.y * src1.x; 1038 * result.w = undef; 1039 */ 1040 src0 = src_vector(p, &inst->SrcReg[0], program); 1041 src1 = src_vector(p, &inst->SrcReg[1], program); 1042 tmp = i915_get_utemp(p); 1043 1044 i915_emit_arith(p, 1045 A0_MUL, 1046 tmp, A0_DEST_CHANNEL_ALL, 0, 1047 swizzle(src0, Z, X, Y, ONE), 1048 swizzle(src1, Y, Z, X, ONE), 0); 1049 1050 i915_emit_arith(p, 1051 A0_MAD, 1052 get_result_vector(p, inst), 1053 get_result_flags(inst), 0, 1054 swizzle(src0, Y, Z, X, ONE), 1055 swizzle(src1, Z, X, Y, ONE), 1056 negate(tmp, 1, 1, 1, 0)); 1057 break; 1058 1059 case OPCODE_END: 1060 return; 1061 1062 case OPCODE_BGNLOOP: 1063 case OPCODE_BGNSUB: 1064 case OPCODE_BRA: 1065 case OPCODE_BRK: 1066 case OPCODE_CAL: 1067 case OPCODE_CONT: 1068 case OPCODE_DDX: 1069 case OPCODE_DDY: 1070 case OPCODE_ELSE: 1071 case OPCODE_ENDIF: 1072 case OPCODE_ENDLOOP: 1073 case OPCODE_ENDSUB: 1074 case OPCODE_IF: 1075 case OPCODE_RET: 1076 p->error = 1; 1077 i915_program_error(p, "Unsupported opcode: %s", 1078 _mesa_opcode_string(inst->Opcode)); 1079 return; 1080 1081 case OPCODE_EXP: 1082 case OPCODE_LOG: 1083 /* These opcodes are claimed as GLSL, NV_vp, and ARB_vp in 1084 * prog_instruction.h, but apparently GLSL doesn't ever emit them. 1085 * Instead, it translates to EX2 or LG2. 1086 */ 1087 case OPCODE_TXD: 1088 case OPCODE_TXL: 1089 /* These opcodes are claimed by GLSL in prog_instruction.h, but 1090 * only NV_vp/fp appears to emit them. 1091 */ 1092 default: 1093 i915_program_error(p, "bad opcode: %s", 1094 _mesa_opcode_string(inst->Opcode)); 1095 return; 1096 } 1097 1098 inst++; 1099 i915_release_utemps(p); 1100 } 1101} 1102 1103/* Rather than trying to intercept and jiggle depth writes during 1104 * emit, just move the value into its correct position at the end of 1105 * the program: 1106 */ 1107static void 1108fixup_depth_write(struct i915_fragment_program *p) 1109{ 1110 if (p->depth_written) { 1111 GLuint depth = UREG(REG_TYPE_OD, 0); 1112 1113 i915_emit_arith(p, 1114 A0_MOV, 1115 depth, A0_DEST_CHANNEL_W, 0, 1116 swizzle(depth, X, Y, Z, Z), 0, 0); 1117 } 1118} 1119 1120 1121static void 1122check_wpos(struct i915_fragment_program *p) 1123{ 1124 GLuint inputs = p->FragProg.Base.InputsRead; 1125 GLint i; 1126 1127 p->wpos_tex = -1; 1128 1129 for (i = 0; i < p->ctx->Const.MaxTextureCoordUnits; i++) { 1130 if (inputs & (FRAG_BIT_TEX(i) | FRAG_BIT_VAR(i))) 1131 continue; 1132 else if (inputs & FRAG_BIT_WPOS) { 1133 p->wpos_tex = i; 1134 inputs &= ~FRAG_BIT_WPOS; 1135 } 1136 } 1137 1138 if (inputs & FRAG_BIT_WPOS) { 1139 i915_program_error(p, "No free texcoord for wpos value"); 1140 } 1141} 1142 1143 1144static void 1145translate_program(struct i915_fragment_program *p) 1146{ 1147 struct i915_context *i915 = I915_CONTEXT(p->ctx); 1148 1149 if (INTEL_DEBUG & DEBUG_WM) { 1150 printf("fp:\n"); 1151 _mesa_print_program(&p->ctx->FragmentProgram._Current->Base); 1152 printf("\n"); 1153 } 1154 1155 i915_init_program(i915, p); 1156 check_wpos(p); 1157 upload_program(p); 1158 fixup_depth_write(p); 1159 i915_fini_program(p); 1160 1161 if (INTEL_DEBUG & DEBUG_WM) { 1162 printf("i915:\n"); 1163 i915_disassemble_program(i915->state.Program, i915->state.ProgramSize); 1164 } 1165 1166 p->translated = 1; 1167} 1168 1169 1170static void 1171track_params(struct i915_fragment_program *p) 1172{ 1173 GLint i; 1174 1175 if (p->nr_params) 1176 _mesa_load_state_parameters(p->ctx, p->FragProg.Base.Parameters); 1177 1178 for (i = 0; i < p->nr_params; i++) { 1179 GLint reg = p->param[i].reg; 1180 COPY_4V(p->constant[reg], p->param[i].values); 1181 } 1182 1183 p->params_uptodate = 1; 1184 p->on_hardware = 0; /* overkill */ 1185} 1186 1187 1188static void 1189i915BindProgram(GLcontext * ctx, GLenum target, struct gl_program *prog) 1190{ 1191 if (target == GL_FRAGMENT_PROGRAM_ARB) { 1192 struct i915_context *i915 = I915_CONTEXT(ctx); 1193 struct i915_fragment_program *p = (struct i915_fragment_program *) prog; 1194 1195 if (i915->current_program == p) 1196 return; 1197 1198 if (i915->current_program) { 1199 i915->current_program->on_hardware = 0; 1200 i915->current_program->params_uptodate = 0; 1201 } 1202 1203 i915->current_program = p; 1204 1205 assert(p->on_hardware == 0); 1206 assert(p->params_uptodate == 0); 1207 1208 } 1209} 1210 1211static struct gl_program * 1212i915NewProgram(GLcontext * ctx, GLenum target, GLuint id) 1213{ 1214 switch (target) { 1215 case GL_VERTEX_PROGRAM_ARB: 1216 return _mesa_init_vertex_program(ctx, CALLOC_STRUCT(gl_vertex_program), 1217 target, id); 1218 1219 case GL_FRAGMENT_PROGRAM_ARB:{ 1220 struct i915_fragment_program *prog = 1221 CALLOC_STRUCT(i915_fragment_program); 1222 if (prog) { 1223 i915_init_program(I915_CONTEXT(ctx), prog); 1224 1225 return _mesa_init_fragment_program(ctx, &prog->FragProg, 1226 target, id); 1227 } 1228 else 1229 return NULL; 1230 } 1231 1232 default: 1233 /* Just fallback: 1234 */ 1235 return _mesa_new_program(ctx, target, id); 1236 } 1237} 1238 1239static void 1240i915DeleteProgram(GLcontext * ctx, struct gl_program *prog) 1241{ 1242 if (prog->Target == GL_FRAGMENT_PROGRAM_ARB) { 1243 struct i915_context *i915 = I915_CONTEXT(ctx); 1244 struct i915_fragment_program *p = (struct i915_fragment_program *) prog; 1245 1246 if (i915->current_program == p) 1247 i915->current_program = 0; 1248 } 1249 1250 _mesa_delete_program(ctx, prog); 1251} 1252 1253 1254static GLboolean 1255i915IsProgramNative(GLcontext * ctx, GLenum target, struct gl_program *prog) 1256{ 1257 if (target == GL_FRAGMENT_PROGRAM_ARB) { 1258 struct i915_fragment_program *p = (struct i915_fragment_program *) prog; 1259 1260 if (!p->translated) 1261 translate_program(p); 1262 1263 return !p->error; 1264 } 1265 else 1266 return GL_TRUE; 1267} 1268 1269static GLboolean 1270i915ProgramStringNotify(GLcontext * ctx, 1271 GLenum target, struct gl_program *prog) 1272{ 1273 if (target == GL_FRAGMENT_PROGRAM_ARB) { 1274 struct i915_fragment_program *p = (struct i915_fragment_program *) prog; 1275 p->translated = 0; 1276 1277 /* Hack: make sure fog is correctly enabled according to this 1278 * fragment program's fog options. 1279 */ 1280 if (p->FragProg.FogOption) { 1281 /* add extra instructions to do fog, then turn off FogOption field */ 1282 _mesa_append_fog_code(ctx, &p->FragProg); 1283 p->FragProg.FogOption = GL_NONE; 1284 } 1285 } 1286 1287 (void) _tnl_program_string(ctx, target, prog); 1288 1289 /* XXX check if program is legal, within limits */ 1290 return GL_TRUE; 1291} 1292 1293void 1294i915_update_program(GLcontext *ctx) 1295{ 1296 struct intel_context *intel = intel_context(ctx); 1297 struct i915_context *i915 = i915_context(&intel->ctx); 1298 struct i915_fragment_program *fp = 1299 (struct i915_fragment_program *) ctx->FragmentProgram._Current; 1300 1301 if (i915->current_program != fp) { 1302 if (i915->current_program) { 1303 i915->current_program->on_hardware = 0; 1304 i915->current_program->params_uptodate = 0; 1305 } 1306 1307 i915->current_program = fp; 1308 } 1309 1310 if (!fp->translated) 1311 translate_program(fp); 1312 1313 FALLBACK(&i915->intel, I915_FALLBACK_PROGRAM, fp->error); 1314} 1315 1316void 1317i915ValidateFragmentProgram(struct i915_context *i915) 1318{ 1319 GLcontext *ctx = &i915->intel.ctx; 1320 struct intel_context *intel = intel_context(ctx); 1321 TNLcontext *tnl = TNL_CONTEXT(ctx); 1322 struct vertex_buffer *VB = &tnl->vb; 1323 1324 struct i915_fragment_program *p = 1325 (struct i915_fragment_program *) ctx->FragmentProgram._Current; 1326 1327 const GLuint inputsRead = p->FragProg.Base.InputsRead; 1328 GLuint s4 = i915->state.Ctx[I915_CTXREG_LIS4] & ~S4_VFMT_MASK; 1329 GLuint s2 = S2_TEXCOORD_NONE; 1330 int i, offset = 0; 1331 1332 /* Important: 1333 */ 1334 VB->AttribPtr[VERT_ATTRIB_POS] = VB->NdcPtr; 1335 1336 if (!p->translated) 1337 translate_program(p); 1338 1339 intel->vertex_attr_count = 0; 1340 intel->wpos_offset = 0; 1341 intel->wpos_size = 0; 1342 intel->coloroffset = 0; 1343 intel->specoffset = 0; 1344 1345 if (inputsRead & FRAG_BITS_TEX_ANY) { 1346 EMIT_ATTR(_TNL_ATTRIB_POS, EMIT_4F_VIEWPORT, S4_VFMT_XYZW, 16); 1347 } 1348 else { 1349 EMIT_ATTR(_TNL_ATTRIB_POS, EMIT_3F_VIEWPORT, S4_VFMT_XYZ, 12); 1350 } 1351 1352 if (inputsRead & FRAG_BIT_COL0) { 1353 intel->coloroffset = offset / 4; 1354 EMIT_ATTR(_TNL_ATTRIB_COLOR0, EMIT_4UB_4F_BGRA, S4_VFMT_COLOR, 4); 1355 } 1356 1357 if (inputsRead & FRAG_BIT_COL1) { 1358 intel->specoffset = offset / 4; 1359 EMIT_ATTR(_TNL_ATTRIB_COLOR1, EMIT_4UB_4F_BGRA, S4_VFMT_SPEC_FOG, 4); 1360 } 1361 1362 if ((inputsRead & FRAG_BIT_FOGC) || i915->vertex_fog != I915_FOG_NONE) { 1363 EMIT_ATTR(_TNL_ATTRIB_FOG, EMIT_1F, S4_VFMT_FOG_PARAM, 4); 1364 } 1365 1366 for (i = 0; i < p->ctx->Const.MaxTextureCoordUnits; i++) { 1367 if (inputsRead & FRAG_BIT_TEX(i)) { 1368 int sz = VB->AttribPtr[_TNL_ATTRIB_TEX0 + i]->size; 1369 1370 s2 &= ~S2_TEXCOORD_FMT(i, S2_TEXCOORD_FMT0_MASK); 1371 s2 |= S2_TEXCOORD_FMT(i, SZ_TO_HW(sz)); 1372 1373 EMIT_ATTR(_TNL_ATTRIB_TEX0 + i, EMIT_SZ(sz), 0, sz * 4); 1374 } 1375 else if (inputsRead & FRAG_BIT_VAR(i)) { 1376 int sz = VB->AttribPtr[_TNL_ATTRIB_GENERIC0 + i]->size; 1377 1378 s2 &= ~S2_TEXCOORD_FMT(i, S2_TEXCOORD_FMT0_MASK); 1379 s2 |= S2_TEXCOORD_FMT(i, SZ_TO_HW(sz)); 1380 1381 EMIT_ATTR(_TNL_ATTRIB_GENERIC0 + i, EMIT_SZ(sz), 0, sz * 4); 1382 } 1383 else if (i == p->wpos_tex) { 1384 1385 /* If WPOS is required, duplicate the XYZ position data in an 1386 * unused texture coordinate: 1387 */ 1388 s2 &= ~S2_TEXCOORD_FMT(i, S2_TEXCOORD_FMT0_MASK); 1389 s2 |= S2_TEXCOORD_FMT(i, SZ_TO_HW(3)); 1390 1391 intel->wpos_offset = offset; 1392 intel->wpos_size = 3 * sizeof(GLuint); 1393 1394 EMIT_PAD(intel->wpos_size); 1395 } 1396 } 1397 1398 if (s2 != i915->state.Ctx[I915_CTXREG_LIS2] || 1399 s4 != i915->state.Ctx[I915_CTXREG_LIS4]) { 1400 int k; 1401 1402 I915_STATECHANGE(i915, I915_UPLOAD_CTX); 1403 1404 /* Must do this *after* statechange, so as not to affect 1405 * buffered vertices reliant on the old state: 1406 */ 1407 intel->vertex_size = _tnl_install_attrs(&intel->ctx, 1408 intel->vertex_attrs, 1409 intel->vertex_attr_count, 1410 intel->ViewportMatrix.m, 0); 1411 1412 intel->vertex_size >>= 2; 1413 1414 i915->state.Ctx[I915_CTXREG_LIS2] = s2; 1415 i915->state.Ctx[I915_CTXREG_LIS4] = s4; 1416 1417 k = intel->vtbl.check_vertex_size(intel, intel->vertex_size); 1418 assert(k); 1419 } 1420 1421 if (!p->params_uptodate) 1422 track_params(p); 1423 1424 if (!p->on_hardware) 1425 i915_upload_program(i915, p); 1426} 1427 1428void 1429i915InitFragProgFuncs(struct dd_function_table *functions) 1430{ 1431 functions->BindProgram = i915BindProgram; 1432 functions->NewProgram = i915NewProgram; 1433 functions->DeleteProgram = i915DeleteProgram; 1434 functions->IsProgramNative = i915IsProgramNative; 1435 functions->ProgramStringNotify = i915ProgramStringNotify; 1436} 1437