i915_fragprog.c revision c92550be64abd454560556599cd95b237f4375b1
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 /* The KIL instruction discards the fragment if any component of 573 * the source is < 0. Emit an immediate operand of {-1}.xywz. 574 */ 575 i915_emit_texld(p, get_live_regs(p, inst), 576 tmp, A0_DEST_CHANNEL_ALL, 577 0, /* use a dummy dest reg */ 578 negate(swizzle(tmp, ONE, ONE, ONE, ONE), 579 1, 1, 1, 1), 580 T0_TEXKILL); 581 } else { 582 p->error = 1; 583 i915_program_error(p, "Unsupported KIL_NV condition code: %d", 584 inst->DstReg.CondMask); 585 } 586 break; 587 588 case OPCODE_LG2: 589 src0 = src_vector(p, &inst->SrcReg[0], program); 590 591 i915_emit_arith(p, 592 A0_LOG, 593 get_result_vector(p, inst), 594 get_result_flags(inst), 0, 595 swizzle(src0, X, X, X, X), 0, 0); 596 break; 597 598 case OPCODE_LIT: 599 src0 = src_vector(p, &inst->SrcReg[0], program); 600 tmp = i915_get_utemp(p); 601 602 /* tmp = max( a.xyzw, a.00zw ) 603 * XXX: Clamp tmp.w to -128..128 604 * tmp.y = log(tmp.y) 605 * tmp.y = tmp.w * tmp.y 606 * tmp.y = exp(tmp.y) 607 * result = cmp (a.11-x1, a.1x01, a.1xy1 ) 608 */ 609 i915_emit_arith(p, A0_MAX, tmp, A0_DEST_CHANNEL_ALL, 0, 610 src0, swizzle(src0, ZERO, ZERO, Z, W), 0); 611 612 i915_emit_arith(p, A0_LOG, tmp, A0_DEST_CHANNEL_Y, 0, 613 swizzle(tmp, Y, Y, Y, Y), 0, 0); 614 615 i915_emit_arith(p, A0_MUL, tmp, A0_DEST_CHANNEL_Y, 0, 616 swizzle(tmp, ZERO, Y, ZERO, ZERO), 617 swizzle(tmp, ZERO, W, ZERO, ZERO), 0); 618 619 i915_emit_arith(p, A0_EXP, tmp, A0_DEST_CHANNEL_Y, 0, 620 swizzle(tmp, Y, Y, Y, Y), 0, 0); 621 622 i915_emit_arith(p, A0_CMP, 623 get_result_vector(p, inst), 624 get_result_flags(inst), 0, 625 negate(swizzle(tmp, ONE, ONE, X, ONE), 0, 0, 1, 0), 626 swizzle(tmp, ONE, X, ZERO, ONE), 627 swizzle(tmp, ONE, X, Y, ONE)); 628 629 break; 630 631 case OPCODE_LRP: 632 src0 = src_vector(p, &inst->SrcReg[0], program); 633 src1 = src_vector(p, &inst->SrcReg[1], program); 634 src2 = src_vector(p, &inst->SrcReg[2], program); 635 flags = get_result_flags(inst); 636 tmp = i915_get_utemp(p); 637 638 /* b*a + c*(1-a) 639 * 640 * b*a + c - ca 641 * 642 * tmp = b*a + c, 643 * result = (-c)*a + tmp 644 */ 645 i915_emit_arith(p, A0_MAD, tmp, 646 flags & A0_DEST_CHANNEL_ALL, 0, src1, src0, src2); 647 648 i915_emit_arith(p, A0_MAD, 649 get_result_vector(p, inst), 650 flags, 0, negate(src2, 1, 1, 1, 1), src0, tmp); 651 break; 652 653 case OPCODE_MAD: 654 EMIT_3ARG_ARITH(A0_MAD); 655 break; 656 657 case OPCODE_MAX: 658 EMIT_2ARG_ARITH(A0_MAX); 659 break; 660 661 case OPCODE_MIN: 662 src0 = src_vector(p, &inst->SrcReg[0], program); 663 src1 = src_vector(p, &inst->SrcReg[1], program); 664 tmp = i915_get_utemp(p); 665 flags = get_result_flags(inst); 666 667 i915_emit_arith(p, 668 A0_MAX, 669 tmp, flags & A0_DEST_CHANNEL_ALL, 0, 670 negate(src0, 1, 1, 1, 1), 671 negate(src1, 1, 1, 1, 1), 0); 672 673 i915_emit_arith(p, 674 A0_MOV, 675 get_result_vector(p, inst), 676 flags, 0, negate(tmp, 1, 1, 1, 1), 0, 0); 677 break; 678 679 case OPCODE_MOV: 680 EMIT_1ARG_ARITH(A0_MOV); 681 break; 682 683 case OPCODE_MUL: 684 EMIT_2ARG_ARITH(A0_MUL); 685 break; 686 687 case OPCODE_POW: 688 src0 = src_vector(p, &inst->SrcReg[0], program); 689 src1 = src_vector(p, &inst->SrcReg[1], program); 690 tmp = i915_get_utemp(p); 691 flags = get_result_flags(inst); 692 693 /* XXX: masking on intermediate values, here and elsewhere. 694 */ 695 i915_emit_arith(p, 696 A0_LOG, 697 tmp, A0_DEST_CHANNEL_X, 0, 698 swizzle(src0, X, X, X, X), 0, 0); 699 700 i915_emit_arith(p, A0_MUL, tmp, A0_DEST_CHANNEL_X, 0, tmp, src1, 0); 701 702 703 i915_emit_arith(p, 704 A0_EXP, 705 get_result_vector(p, inst), 706 flags, 0, swizzle(tmp, X, X, X, X), 0, 0); 707 708 break; 709 710 case OPCODE_RCP: 711 src0 = src_vector(p, &inst->SrcReg[0], program); 712 713 i915_emit_arith(p, 714 A0_RCP, 715 get_result_vector(p, inst), 716 get_result_flags(inst), 0, 717 swizzle(src0, X, X, X, X), 0, 0); 718 break; 719 720 case OPCODE_RSQ: 721 722 src0 = src_vector(p, &inst->SrcReg[0], program); 723 724 i915_emit_arith(p, 725 A0_RSQ, 726 get_result_vector(p, inst), 727 get_result_flags(inst), 0, 728 swizzle(src0, X, X, X, X), 0, 0); 729 break; 730 731 case OPCODE_SCS: 732 src0 = src_vector(p, &inst->SrcReg[0], program); 733 tmp = i915_get_utemp(p); 734 735 /* 736 * t0.xy = MUL x.xx11, x.x1111 ; x^2, x, 1, 1 737 * t0 = MUL t0.xyxy t0.xx11 ; x^4, x^3, x^2, x 738 * t1 = MUL t0.xyyw t0.yz11 ; x^7 x^5 x^3 x 739 * scs.x = DP4 t1, sin_constants 740 * t1 = MUL t0.xxz1 t0.z111 ; x^6 x^4 x^2 1 741 * scs.y = DP4 t1, cos_constants 742 */ 743 i915_emit_arith(p, 744 A0_MUL, 745 tmp, A0_DEST_CHANNEL_XY, 0, 746 swizzle(src0, X, X, ONE, ONE), 747 swizzle(src0, X, ONE, ONE, ONE), 0); 748 749 i915_emit_arith(p, 750 A0_MUL, 751 tmp, A0_DEST_CHANNEL_ALL, 0, 752 swizzle(tmp, X, Y, X, Y), 753 swizzle(tmp, X, X, ONE, ONE), 0); 754 755 if (inst->DstReg.WriteMask & WRITEMASK_Y) { 756 GLuint tmp1; 757 758 if (inst->DstReg.WriteMask & WRITEMASK_X) 759 tmp1 = i915_get_utemp(p); 760 else 761 tmp1 = tmp; 762 763 i915_emit_arith(p, 764 A0_MUL, 765 tmp1, A0_DEST_CHANNEL_ALL, 0, 766 swizzle(tmp, X, Y, Y, W), 767 swizzle(tmp, X, Z, ONE, ONE), 0); 768 769 i915_emit_arith(p, 770 A0_DP4, 771 get_result_vector(p, inst), 772 A0_DEST_CHANNEL_Y, 0, 773 swizzle(tmp1, W, Z, Y, X), 774 i915_emit_const4fv(p, sin_constants), 0); 775 } 776 777 if (inst->DstReg.WriteMask & WRITEMASK_X) { 778 i915_emit_arith(p, 779 A0_MUL, 780 tmp, A0_DEST_CHANNEL_XYZ, 0, 781 swizzle(tmp, X, X, Z, ONE), 782 swizzle(tmp, Z, ONE, ONE, ONE), 0); 783 784 i915_emit_arith(p, 785 A0_DP4, 786 get_result_vector(p, inst), 787 A0_DEST_CHANNEL_X, 0, 788 swizzle(tmp, ONE, Z, Y, X), 789 i915_emit_const4fv(p, cos_constants), 0); 790 } 791 break; 792 793 case OPCODE_SEQ: 794 tmp = i915_get_utemp(p); 795 flags = get_result_flags(inst); 796 dst = get_result_vector(p, inst); 797 798 /* dst = src1 >= src2 */ 799 i915_emit_arith(p, 800 A0_SGE, 801 dst, 802 flags, 0, 803 src_vector(p, &inst->SrcReg[0], program), 804 src_vector(p, &inst->SrcReg[1], program), 805 0); 806 /* tmp = src1 <= src2 */ 807 i915_emit_arith(p, 808 A0_SGE, 809 tmp, 810 flags, 0, 811 negate(src_vector(p, &inst->SrcReg[0], program), 812 1, 1, 1, 1), 813 negate(src_vector(p, &inst->SrcReg[1], program), 814 1, 1, 1, 1), 815 0); 816 /* dst = tmp && dst */ 817 i915_emit_arith(p, 818 A0_MUL, 819 dst, 820 flags, 0, 821 dst, 822 tmp, 823 0); 824 break; 825 826 case OPCODE_SIN: 827 src0 = src_vector(p, &inst->SrcReg[0], program); 828 tmp = i915_get_utemp(p); 829 consts0 = i915_emit_const4fv(p, sin_quad_constants[0]); 830 consts1 = i915_emit_const4fv(p, sin_quad_constants[1]); 831 832 /* Reduce range from repeating about [-pi,pi] to [-1,1] */ 833 i915_emit_arith(p, 834 A0_MAD, 835 tmp, A0_DEST_CHANNEL_X, 0, 836 src0, 837 swizzle(consts1, Z, ZERO, ZERO, ZERO), /* 1/(2pi) */ 838 swizzle(consts0, Z, ZERO, ZERO, ZERO)); /* .5 */ 839 840 i915_emit_arith(p, A0_FRC, tmp, A0_DEST_CHANNEL_X, 0, tmp, 0, 0); 841 842 i915_emit_arith(p, 843 A0_MAD, 844 tmp, A0_DEST_CHANNEL_X, 0, 845 tmp, 846 swizzle(consts0, X, ZERO, ZERO, ZERO), /* 2 */ 847 swizzle(consts0, Y, ZERO, ZERO, ZERO)); /* -1 */ 848 849 /* Compute sin using a quadratic and quartic. It gives continuity 850 * that repeating the Taylor series lacks every 2*pi, and has 851 * reduced error. 852 * 853 * The idea was described at: 854 * http://www.devmaster.net/forums/showthread.php?t=5784 855 */ 856 857 /* tmp.y = abs(tmp.x); {x, abs(x), 0, 0} */ 858 i915_emit_arith(p, 859 A0_MAX, 860 tmp, A0_DEST_CHANNEL_Y, 0, 861 swizzle(tmp, ZERO, X, ZERO, ZERO), 862 negate(swizzle(tmp, ZERO, X, ZERO, ZERO), 0, 1, 0, 0), 863 0); 864 865 /* tmp.y = tmp.y * tmp.x; {x, x * abs(x), 0, 0} */ 866 i915_emit_arith(p, 867 A0_MUL, 868 tmp, A0_DEST_CHANNEL_Y, 0, 869 swizzle(tmp, ZERO, X, ZERO, ZERO), 870 tmp, 871 0); 872 873 /* tmp.x = tmp.xy DP sin_quad_constants[2].xy */ 874 i915_emit_arith(p, 875 A0_DP3, 876 tmp, A0_DEST_CHANNEL_X, 0, 877 tmp, 878 swizzle(consts1, X, Y, ZERO, ZERO), 879 0); 880 881 /* tmp.x now contains a first approximation (y). Now, weight it 882 * against tmp.y**2 to get closer. 883 */ 884 i915_emit_arith(p, 885 A0_MAX, 886 tmp, A0_DEST_CHANNEL_Y, 0, 887 swizzle(tmp, ZERO, X, ZERO, ZERO), 888 negate(swizzle(tmp, ZERO, X, ZERO, ZERO), 0, 1, 0, 0), 889 0); 890 891 /* tmp.y = tmp.x * tmp.y - tmp.x; {y, y * abs(y) - y, 0, 0} */ 892 i915_emit_arith(p, 893 A0_MAD, 894 tmp, A0_DEST_CHANNEL_Y, 0, 895 swizzle(tmp, ZERO, X, ZERO, ZERO), 896 swizzle(tmp, ZERO, Y, ZERO, ZERO), 897 negate(swizzle(tmp, ZERO, X, ZERO, ZERO), 0, 1, 0, 0)); 898 899 /* result = .2225 * tmp.y + tmp.x =.2225(y * abs(y) - y) + y= */ 900 i915_emit_arith(p, 901 A0_MAD, 902 get_result_vector(p, inst), 903 get_result_flags(inst), 0, 904 swizzle(consts1, W, W, W, W), 905 swizzle(tmp, Y, Y, Y, Y), 906 swizzle(tmp, X, X, X, X)); 907 908 break; 909 910 case OPCODE_SGE: 911 EMIT_2ARG_ARITH(A0_SGE); 912 break; 913 914 case OPCODE_SGT: 915 i915_emit_arith(p, 916 A0_SLT, 917 get_result_vector( p, inst ), 918 get_result_flags( inst ), 0, 919 negate(src_vector( p, &inst->SrcReg[0], program), 920 1, 1, 1, 1), 921 negate(src_vector( p, &inst->SrcReg[1], program), 922 1, 1, 1, 1), 923 0); 924 break; 925 926 case OPCODE_SLE: 927 i915_emit_arith(p, 928 A0_SGE, 929 get_result_vector( p, inst ), 930 get_result_flags( inst ), 0, 931 negate(src_vector( p, &inst->SrcReg[0], program), 932 1, 1, 1, 1), 933 negate(src_vector( p, &inst->SrcReg[1], program), 934 1, 1, 1, 1), 935 0); 936 break; 937 938 case OPCODE_SLT: 939 EMIT_2ARG_ARITH(A0_SLT); 940 break; 941 942 case OPCODE_SNE: 943 tmp = i915_get_utemp(p); 944 flags = get_result_flags(inst); 945 dst = get_result_vector(p, inst); 946 947 /* dst = src1 < src2 */ 948 i915_emit_arith(p, 949 A0_SLT, 950 dst, 951 flags, 0, 952 src_vector(p, &inst->SrcReg[0], program), 953 src_vector(p, &inst->SrcReg[1], program), 954 0); 955 /* tmp = src1 > src2 */ 956 i915_emit_arith(p, 957 A0_SLT, 958 tmp, 959 flags, 0, 960 negate(src_vector(p, &inst->SrcReg[0], program), 961 1, 1, 1, 1), 962 negate(src_vector(p, &inst->SrcReg[1], program), 963 1, 1, 1, 1), 964 0); 965 /* dst = tmp || dst */ 966 i915_emit_arith(p, 967 A0_ADD, 968 dst, 969 flags | A0_DEST_SATURATE, 0, 970 dst, 971 tmp, 972 0); 973 break; 974 975 case OPCODE_SSG: 976 dst = get_result_vector(p, inst); 977 flags = get_result_flags(inst); 978 src0 = src_vector(p, &inst->SrcReg[0], program); 979 tmp = i915_get_utemp(p); 980 981 /* tmp = (src < 0.0) */ 982 i915_emit_arith(p, 983 A0_SLT, 984 tmp, 985 flags, 0, 986 src0, 987 swizzle(src0, ZERO, ZERO, ZERO, ZERO), 988 0); 989 990 /* dst = (0.0 < src) */ 991 i915_emit_arith(p, 992 A0_SLT, 993 dst, 994 flags, 0, 995 swizzle(src0, ZERO, ZERO, ZERO, ZERO), 996 src0, 997 0); 998 999 /* dst = (src > 0.0) - (src < 0.0) */ 1000 i915_emit_arith(p, 1001 A0_ADD, 1002 dst, 1003 flags, 0, 1004 dst, 1005 negate(tmp, 1, 1, 1, 1), 1006 0); 1007 1008 break; 1009 1010 case OPCODE_SUB: 1011 src0 = src_vector(p, &inst->SrcReg[0], program); 1012 src1 = src_vector(p, &inst->SrcReg[1], program); 1013 1014 i915_emit_arith(p, 1015 A0_ADD, 1016 get_result_vector(p, inst), 1017 get_result_flags(inst), 0, 1018 src0, negate(src1, 1, 1, 1, 1), 0); 1019 break; 1020 1021 case OPCODE_SWZ: 1022 EMIT_1ARG_ARITH(A0_MOV); /* extended swizzle handled natively */ 1023 break; 1024 1025 case OPCODE_TEX: 1026 EMIT_TEX(T0_TEXLD); 1027 break; 1028 1029 case OPCODE_TXB: 1030 EMIT_TEX(T0_TEXLDB); 1031 break; 1032 1033 case OPCODE_TXP: 1034 EMIT_TEX(T0_TEXLDP); 1035 break; 1036 1037 case OPCODE_XPD: 1038 /* Cross product: 1039 * result.x = src0.y * src1.z - src0.z * src1.y; 1040 * result.y = src0.z * src1.x - src0.x * src1.z; 1041 * result.z = src0.x * src1.y - src0.y * src1.x; 1042 * result.w = undef; 1043 */ 1044 src0 = src_vector(p, &inst->SrcReg[0], program); 1045 src1 = src_vector(p, &inst->SrcReg[1], program); 1046 tmp = i915_get_utemp(p); 1047 1048 i915_emit_arith(p, 1049 A0_MUL, 1050 tmp, A0_DEST_CHANNEL_ALL, 0, 1051 swizzle(src0, Z, X, Y, ONE), 1052 swizzle(src1, Y, Z, X, ONE), 0); 1053 1054 i915_emit_arith(p, 1055 A0_MAD, 1056 get_result_vector(p, inst), 1057 get_result_flags(inst), 0, 1058 swizzle(src0, Y, Z, X, ONE), 1059 swizzle(src1, Z, X, Y, ONE), 1060 negate(tmp, 1, 1, 1, 0)); 1061 break; 1062 1063 case OPCODE_END: 1064 return; 1065 1066 case OPCODE_BGNLOOP: 1067 case OPCODE_BGNSUB: 1068 case OPCODE_BRA: 1069 case OPCODE_BRK: 1070 case OPCODE_CAL: 1071 case OPCODE_CONT: 1072 case OPCODE_DDX: 1073 case OPCODE_DDY: 1074 case OPCODE_ELSE: 1075 case OPCODE_ENDIF: 1076 case OPCODE_ENDLOOP: 1077 case OPCODE_ENDSUB: 1078 case OPCODE_IF: 1079 case OPCODE_RET: 1080 p->error = 1; 1081 i915_program_error(p, "Unsupported opcode: %s", 1082 _mesa_opcode_string(inst->Opcode)); 1083 return; 1084 1085 case OPCODE_EXP: 1086 case OPCODE_LOG: 1087 /* These opcodes are claimed as GLSL, NV_vp, and ARB_vp in 1088 * prog_instruction.h, but apparently GLSL doesn't ever emit them. 1089 * Instead, it translates to EX2 or LG2. 1090 */ 1091 case OPCODE_TXD: 1092 case OPCODE_TXL: 1093 /* These opcodes are claimed by GLSL in prog_instruction.h, but 1094 * only NV_vp/fp appears to emit them. 1095 */ 1096 default: 1097 i915_program_error(p, "bad opcode: %s", 1098 _mesa_opcode_string(inst->Opcode)); 1099 return; 1100 } 1101 1102 inst++; 1103 i915_release_utemps(p); 1104 } 1105} 1106 1107/* Rather than trying to intercept and jiggle depth writes during 1108 * emit, just move the value into its correct position at the end of 1109 * the program: 1110 */ 1111static void 1112fixup_depth_write(struct i915_fragment_program *p) 1113{ 1114 if (p->depth_written) { 1115 GLuint depth = UREG(REG_TYPE_OD, 0); 1116 1117 i915_emit_arith(p, 1118 A0_MOV, 1119 depth, A0_DEST_CHANNEL_W, 0, 1120 swizzle(depth, X, Y, Z, Z), 0, 0); 1121 } 1122} 1123 1124 1125static void 1126check_wpos(struct i915_fragment_program *p) 1127{ 1128 GLuint inputs = p->FragProg.Base.InputsRead; 1129 GLint i; 1130 1131 p->wpos_tex = -1; 1132 1133 for (i = 0; i < p->ctx->Const.MaxTextureCoordUnits; i++) { 1134 if (inputs & (FRAG_BIT_TEX(i) | FRAG_BIT_VAR(i))) 1135 continue; 1136 else if (inputs & FRAG_BIT_WPOS) { 1137 p->wpos_tex = i; 1138 inputs &= ~FRAG_BIT_WPOS; 1139 } 1140 } 1141 1142 if (inputs & FRAG_BIT_WPOS) { 1143 i915_program_error(p, "No free texcoord for wpos value"); 1144 } 1145} 1146 1147 1148static void 1149translate_program(struct i915_fragment_program *p) 1150{ 1151 struct i915_context *i915 = I915_CONTEXT(p->ctx); 1152 1153 if (INTEL_DEBUG & DEBUG_WM) { 1154 printf("fp:\n"); 1155 _mesa_print_program(&p->ctx->FragmentProgram._Current->Base); 1156 printf("\n"); 1157 } 1158 1159 i915_init_program(i915, p); 1160 check_wpos(p); 1161 upload_program(p); 1162 fixup_depth_write(p); 1163 i915_fini_program(p); 1164 1165 if (INTEL_DEBUG & DEBUG_WM) { 1166 printf("i915:\n"); 1167 i915_disassemble_program(i915->state.Program, i915->state.ProgramSize); 1168 } 1169 1170 p->translated = 1; 1171} 1172 1173 1174static void 1175track_params(struct i915_fragment_program *p) 1176{ 1177 GLint i; 1178 1179 if (p->nr_params) 1180 _mesa_load_state_parameters(p->ctx, p->FragProg.Base.Parameters); 1181 1182 for (i = 0; i < p->nr_params; i++) { 1183 GLint reg = p->param[i].reg; 1184 COPY_4V(p->constant[reg], p->param[i].values); 1185 } 1186 1187 p->params_uptodate = 1; 1188 p->on_hardware = 0; /* overkill */ 1189} 1190 1191 1192static void 1193i915BindProgram(struct gl_context * ctx, GLenum target, struct gl_program *prog) 1194{ 1195 if (target == GL_FRAGMENT_PROGRAM_ARB) { 1196 struct i915_context *i915 = I915_CONTEXT(ctx); 1197 struct i915_fragment_program *p = (struct i915_fragment_program *) prog; 1198 1199 if (i915->current_program == p) 1200 return; 1201 1202 if (i915->current_program) { 1203 i915->current_program->on_hardware = 0; 1204 i915->current_program->params_uptodate = 0; 1205 } 1206 1207 i915->current_program = p; 1208 1209 assert(p->on_hardware == 0); 1210 assert(p->params_uptodate == 0); 1211 1212 } 1213} 1214 1215static struct gl_program * 1216i915NewProgram(struct gl_context * ctx, GLenum target, GLuint id) 1217{ 1218 switch (target) { 1219 case GL_VERTEX_PROGRAM_ARB: 1220 return _mesa_init_vertex_program(ctx, CALLOC_STRUCT(gl_vertex_program), 1221 target, id); 1222 1223 case GL_FRAGMENT_PROGRAM_ARB:{ 1224 struct i915_fragment_program *prog = 1225 CALLOC_STRUCT(i915_fragment_program); 1226 if (prog) { 1227 i915_init_program(I915_CONTEXT(ctx), prog); 1228 1229 return _mesa_init_fragment_program(ctx, &prog->FragProg, 1230 target, id); 1231 } 1232 else 1233 return NULL; 1234 } 1235 1236 default: 1237 /* Just fallback: 1238 */ 1239 return _mesa_new_program(ctx, target, id); 1240 } 1241} 1242 1243static void 1244i915DeleteProgram(struct gl_context * ctx, struct gl_program *prog) 1245{ 1246 if (prog->Target == GL_FRAGMENT_PROGRAM_ARB) { 1247 struct i915_context *i915 = I915_CONTEXT(ctx); 1248 struct i915_fragment_program *p = (struct i915_fragment_program *) prog; 1249 1250 if (i915->current_program == p) 1251 i915->current_program = 0; 1252 } 1253 1254 _mesa_delete_program(ctx, prog); 1255} 1256 1257 1258static GLboolean 1259i915IsProgramNative(struct gl_context * ctx, GLenum target, struct gl_program *prog) 1260{ 1261 if (target == GL_FRAGMENT_PROGRAM_ARB) { 1262 struct i915_fragment_program *p = (struct i915_fragment_program *) prog; 1263 1264 if (!p->translated) 1265 translate_program(p); 1266 1267 return !p->error; 1268 } 1269 else 1270 return GL_TRUE; 1271} 1272 1273static GLboolean 1274i915ProgramStringNotify(struct gl_context * ctx, 1275 GLenum target, struct gl_program *prog) 1276{ 1277 if (target == GL_FRAGMENT_PROGRAM_ARB) { 1278 struct i915_fragment_program *p = (struct i915_fragment_program *) prog; 1279 p->translated = 0; 1280 1281 /* Hack: make sure fog is correctly enabled according to this 1282 * fragment program's fog options. 1283 */ 1284 if (p->FragProg.FogOption) { 1285 /* add extra instructions to do fog, then turn off FogOption field */ 1286 _mesa_append_fog_code(ctx, &p->FragProg); 1287 p->FragProg.FogOption = GL_NONE; 1288 } 1289 } 1290 1291 (void) _tnl_program_string(ctx, target, prog); 1292 1293 /* XXX check if program is legal, within limits */ 1294 return GL_TRUE; 1295} 1296 1297void 1298i915_update_program(struct gl_context *ctx) 1299{ 1300 struct intel_context *intel = intel_context(ctx); 1301 struct i915_context *i915 = i915_context(&intel->ctx); 1302 struct i915_fragment_program *fp = 1303 (struct i915_fragment_program *) ctx->FragmentProgram._Current; 1304 1305 if (i915->current_program != fp) { 1306 if (i915->current_program) { 1307 i915->current_program->on_hardware = 0; 1308 i915->current_program->params_uptodate = 0; 1309 } 1310 1311 i915->current_program = fp; 1312 } 1313 1314 if (!fp->translated) 1315 translate_program(fp); 1316 1317 FALLBACK(&i915->intel, I915_FALLBACK_PROGRAM, fp->error); 1318} 1319 1320void 1321i915ValidateFragmentProgram(struct i915_context *i915) 1322{ 1323 struct gl_context *ctx = &i915->intel.ctx; 1324 struct intel_context *intel = intel_context(ctx); 1325 TNLcontext *tnl = TNL_CONTEXT(ctx); 1326 struct vertex_buffer *VB = &tnl->vb; 1327 1328 struct i915_fragment_program *p = 1329 (struct i915_fragment_program *) ctx->FragmentProgram._Current; 1330 1331 const GLuint inputsRead = p->FragProg.Base.InputsRead; 1332 GLuint s4 = i915->state.Ctx[I915_CTXREG_LIS4] & ~S4_VFMT_MASK; 1333 GLuint s2 = S2_TEXCOORD_NONE; 1334 int i, offset = 0; 1335 1336 /* Important: 1337 */ 1338 VB->AttribPtr[VERT_ATTRIB_POS] = VB->NdcPtr; 1339 1340 if (!p->translated) 1341 translate_program(p); 1342 1343 intel->vertex_attr_count = 0; 1344 intel->wpos_offset = 0; 1345 intel->wpos_size = 0; 1346 intel->coloroffset = 0; 1347 intel->specoffset = 0; 1348 1349 if (inputsRead & FRAG_BITS_TEX_ANY) { 1350 EMIT_ATTR(_TNL_ATTRIB_POS, EMIT_4F_VIEWPORT, S4_VFMT_XYZW, 16); 1351 } 1352 else { 1353 EMIT_ATTR(_TNL_ATTRIB_POS, EMIT_3F_VIEWPORT, S4_VFMT_XYZ, 12); 1354 } 1355 1356 if (inputsRead & FRAG_BIT_COL0) { 1357 intel->coloroffset = offset / 4; 1358 EMIT_ATTR(_TNL_ATTRIB_COLOR0, EMIT_4UB_4F_BGRA, S4_VFMT_COLOR, 4); 1359 } 1360 1361 if (inputsRead & FRAG_BIT_COL1) { 1362 intel->specoffset = offset / 4; 1363 EMIT_ATTR(_TNL_ATTRIB_COLOR1, EMIT_4UB_4F_BGRA, S4_VFMT_SPEC_FOG, 4); 1364 } 1365 1366 if ((inputsRead & FRAG_BIT_FOGC) || i915->vertex_fog != I915_FOG_NONE) { 1367 EMIT_ATTR(_TNL_ATTRIB_FOG, EMIT_1F, S4_VFMT_FOG_PARAM, 4); 1368 } 1369 1370 for (i = 0; i < p->ctx->Const.MaxTextureCoordUnits; i++) { 1371 if (inputsRead & FRAG_BIT_TEX(i)) { 1372 int sz = VB->AttribPtr[_TNL_ATTRIB_TEX0 + i]->size; 1373 1374 s2 &= ~S2_TEXCOORD_FMT(i, S2_TEXCOORD_FMT0_MASK); 1375 s2 |= S2_TEXCOORD_FMT(i, SZ_TO_HW(sz)); 1376 1377 EMIT_ATTR(_TNL_ATTRIB_TEX0 + i, EMIT_SZ(sz), 0, sz * 4); 1378 } 1379 else if (inputsRead & FRAG_BIT_VAR(i)) { 1380 int sz = VB->AttribPtr[_TNL_ATTRIB_GENERIC0 + i]->size; 1381 1382 s2 &= ~S2_TEXCOORD_FMT(i, S2_TEXCOORD_FMT0_MASK); 1383 s2 |= S2_TEXCOORD_FMT(i, SZ_TO_HW(sz)); 1384 1385 EMIT_ATTR(_TNL_ATTRIB_GENERIC0 + i, EMIT_SZ(sz), 0, sz * 4); 1386 } 1387 else if (i == p->wpos_tex) { 1388 1389 /* If WPOS is required, duplicate the XYZ position data in an 1390 * unused texture coordinate: 1391 */ 1392 s2 &= ~S2_TEXCOORD_FMT(i, S2_TEXCOORD_FMT0_MASK); 1393 s2 |= S2_TEXCOORD_FMT(i, SZ_TO_HW(3)); 1394 1395 intel->wpos_offset = offset; 1396 intel->wpos_size = 3 * sizeof(GLuint); 1397 1398 EMIT_PAD(intel->wpos_size); 1399 } 1400 } 1401 1402 if (s2 != i915->state.Ctx[I915_CTXREG_LIS2] || 1403 s4 != i915->state.Ctx[I915_CTXREG_LIS4]) { 1404 int k; 1405 1406 I915_STATECHANGE(i915, I915_UPLOAD_CTX); 1407 1408 /* Must do this *after* statechange, so as not to affect 1409 * buffered vertices reliant on the old state: 1410 */ 1411 intel->vertex_size = _tnl_install_attrs(&intel->ctx, 1412 intel->vertex_attrs, 1413 intel->vertex_attr_count, 1414 intel->ViewportMatrix.m, 0); 1415 1416 intel->vertex_size >>= 2; 1417 1418 i915->state.Ctx[I915_CTXREG_LIS2] = s2; 1419 i915->state.Ctx[I915_CTXREG_LIS4] = s4; 1420 1421 k = intel->vtbl.check_vertex_size(intel, intel->vertex_size); 1422 assert(k); 1423 } 1424 1425 if (!p->params_uptodate) 1426 track_params(p); 1427 1428 if (!p->on_hardware) 1429 i915_upload_program(i915, p); 1430} 1431 1432void 1433i915InitFragProgFuncs(struct dd_function_table *functions) 1434{ 1435 functions->BindProgram = i915BindProgram; 1436 functions->NewProgram = i915NewProgram; 1437 functions->DeleteProgram = i915DeleteProgram; 1438 functions->IsProgramNative = i915IsProgramNative; 1439 functions->ProgramStringNotify = i915ProgramStringNotify; 1440} 1441