i915_fragprog.c revision 499f7c0114cca195c9569c202ae099ef0277b010
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->FragProg; \ 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->FragProg; 308 GLuint regsUsed = 0xffff0000; 309 uint8_t live_components[16] = { 0, }; 310 GLint i; 311 312 for (i = program->Base.NumInstructions - 1; i >= 0; i--) { 313 struct prog_instruction *inst = &program->Base.Instructions[i]; 314 int opArgs = _mesa_num_inst_src_regs(inst->Opcode); 315 int a; 316 317 /* Register is written to: unmark as live for this and preceeding ops */ 318 if (inst->DstReg.File == PROGRAM_TEMPORARY) { 319 live_components[inst->DstReg.Index] &= ~inst->DstReg.WriteMask; 320 if (live_components[inst->DstReg.Index] == 0) 321 regsUsed &= ~(1 << inst->DstReg.Index); 322 } 323 324 for (a = 0; a < opArgs; a++) { 325 /* Register is read from: mark as live for this and preceeding ops */ 326 if (inst->SrcReg[a].File == PROGRAM_TEMPORARY) { 327 unsigned c; 328 329 regsUsed |= 1 << inst->SrcReg[a].Index; 330 331 for (c = 0; c < 4; c++) { 332 const unsigned field = GET_SWZ(inst->SrcReg[a].Swizzle, c); 333 334 if (field <= SWIZZLE_W) 335 live_components[inst->SrcReg[a].Index] |= (1U << field); 336 } 337 } 338 } 339 340 p->usedRegs[i] = regsUsed; 341 } 342} 343 344static GLuint get_live_regs( struct i915_fragment_program *p, 345 const struct prog_instruction *inst ) 346{ 347 const struct gl_fragment_program *program = &p->FragProg; 348 GLuint nr = inst - program->Base.Instructions; 349 350 return p->usedRegs[nr]; 351} 352 353 354/* Possible concerns: 355 * 356 * SIN, COS -- could use another taylor step? 357 * LIT -- results seem a little different to sw mesa 358 * LOG -- different to mesa on negative numbers, but this is conformant. 359 * 360 * Parse failures -- Mesa doesn't currently give a good indication 361 * internally whether a particular program string parsed or not. This 362 * can lead to confusion -- hopefully we cope with it ok now. 363 * 364 */ 365static void 366upload_program(struct i915_fragment_program *p) 367{ 368 const struct gl_fragment_program *program = &p->FragProg; 369 const struct prog_instruction *inst = program->Base.Instructions; 370 371 if (INTEL_DEBUG & DEBUG_WM) 372 _mesa_print_program(&program->Base); 373 374 /* Is this a parse-failed program? Ensure a valid program is 375 * loaded, as the flagging of an error isn't sufficient to stop 376 * this being uploaded to hardware. 377 */ 378 if (inst[0].Opcode == OPCODE_END) { 379 GLuint tmp = i915_get_utemp(p); 380 i915_emit_arith(p, 381 A0_MOV, 382 UREG(REG_TYPE_OC, 0), 383 A0_DEST_CHANNEL_ALL, 0, 384 swizzle(tmp, ONE, ZERO, ONE, ONE), 0, 0); 385 return; 386 } 387 388 if (program->Base.NumInstructions > I915_MAX_INSN) { 389 i915_program_error(p, "Exceeded max instructions (%d out of %d)", 390 program->Base.NumInstructions, I915_MAX_INSN); 391 return; 392 } 393 394 /* Not always needed: 395 */ 396 calc_live_regs(p); 397 398 while (1) { 399 GLuint src0, src1, src2, flags; 400 GLuint tmp = 0, dst, consts0 = 0, consts1 = 0; 401 402 switch (inst->Opcode) { 403 case OPCODE_ABS: 404 src0 = src_vector(p, &inst->SrcReg[0], program); 405 i915_emit_arith(p, 406 A0_MAX, 407 get_result_vector(p, inst), 408 get_result_flags(inst), 0, 409 src0, negate(src0, 1, 1, 1, 1), 0); 410 break; 411 412 case OPCODE_ADD: 413 EMIT_2ARG_ARITH(A0_ADD); 414 break; 415 416 case OPCODE_CMP: 417 src0 = src_vector(p, &inst->SrcReg[0], program); 418 src1 = src_vector(p, &inst->SrcReg[1], program); 419 src2 = src_vector(p, &inst->SrcReg[2], program); 420 i915_emit_arith(p, A0_CMP, get_result_vector(p, inst), get_result_flags(inst), 0, src0, src2, src1); /* NOTE: order of src2, src1 */ 421 break; 422 423 case OPCODE_COS: 424 src0 = src_vector(p, &inst->SrcReg[0], program); 425 tmp = i915_get_utemp(p); 426 consts0 = i915_emit_const4fv(p, sin_quad_constants[0]); 427 consts1 = i915_emit_const4fv(p, sin_quad_constants[1]); 428 429 /* Reduce range from repeating about [-pi,pi] to [-1,1] */ 430 i915_emit_arith(p, 431 A0_MAD, 432 tmp, A0_DEST_CHANNEL_X, 0, 433 src0, 434 swizzle(consts1, Z, ZERO, ZERO, ZERO), /* 1/(2pi) */ 435 swizzle(consts0, W, ZERO, ZERO, ZERO)); /* .75 */ 436 437 i915_emit_arith(p, A0_FRC, tmp, A0_DEST_CHANNEL_X, 0, tmp, 0, 0); 438 439 i915_emit_arith(p, 440 A0_MAD, 441 tmp, A0_DEST_CHANNEL_X, 0, 442 tmp, 443 swizzle(consts0, X, ZERO, ZERO, ZERO), /* 2 */ 444 swizzle(consts0, Y, ZERO, ZERO, ZERO)); /* -1 */ 445 446 /* Compute COS with the same calculation used for SIN, but a 447 * different source range has been mapped to [-1,1] this time. 448 */ 449 450 /* tmp.y = abs(tmp.x); {x, abs(x), 0, 0} */ 451 i915_emit_arith(p, 452 A0_MAX, 453 tmp, A0_DEST_CHANNEL_Y, 0, 454 swizzle(tmp, ZERO, X, ZERO, ZERO), 455 negate(swizzle(tmp, ZERO, X, ZERO, ZERO), 0, 1, 0, 0), 456 0); 457 458 /* tmp.y = tmp.y * tmp.x; {x, x * abs(x), 0, 0} */ 459 i915_emit_arith(p, 460 A0_MUL, 461 tmp, A0_DEST_CHANNEL_Y, 0, 462 swizzle(tmp, ZERO, X, ZERO, ZERO), 463 tmp, 464 0); 465 466 /* tmp.x = tmp.xy DP sin_quad_constants[2].xy */ 467 i915_emit_arith(p, 468 A0_DP3, 469 tmp, A0_DEST_CHANNEL_X, 0, 470 tmp, 471 swizzle(consts1, X, Y, ZERO, ZERO), 472 0); 473 474 /* tmp.x now contains a first approximation (y). Now, weight it 475 * against tmp.y**2 to get closer. 476 */ 477 i915_emit_arith(p, 478 A0_MAX, 479 tmp, A0_DEST_CHANNEL_Y, 0, 480 swizzle(tmp, ZERO, X, ZERO, ZERO), 481 negate(swizzle(tmp, ZERO, X, ZERO, ZERO), 0, 1, 0, 0), 482 0); 483 484 /* tmp.y = tmp.x * tmp.y - tmp.x; {y, y * abs(y) - y, 0, 0} */ 485 i915_emit_arith(p, 486 A0_MAD, 487 tmp, A0_DEST_CHANNEL_Y, 0, 488 swizzle(tmp, ZERO, X, ZERO, ZERO), 489 swizzle(tmp, ZERO, Y, ZERO, ZERO), 490 negate(swizzle(tmp, ZERO, X, ZERO, ZERO), 0, 1, 0, 0)); 491 492 /* result = .2225 * tmp.y + tmp.x =.2225(y * abs(y) - y) + y= */ 493 i915_emit_arith(p, 494 A0_MAD, 495 get_result_vector(p, inst), 496 get_result_flags(inst), 0, 497 swizzle(consts1, W, W, W, W), 498 swizzle(tmp, Y, Y, Y, Y), 499 swizzle(tmp, X, X, X, X)); 500 break; 501 502 case OPCODE_DP2: 503 src0 = src_vector(p, &inst->SrcReg[0], program); 504 src1 = src_vector(p, &inst->SrcReg[1], program); 505 i915_emit_arith(p, 506 A0_DP3, 507 get_result_vector(p, inst), 508 get_result_flags(inst), 0, 509 swizzle(src0, X, Y, ZERO, ZERO), 510 swizzle(src1, X, Y, ZERO, ZERO), 511 0); 512 break; 513 514 case OPCODE_DP3: 515 EMIT_2ARG_ARITH(A0_DP3); 516 break; 517 518 case OPCODE_DP4: 519 EMIT_2ARG_ARITH(A0_DP4); 520 break; 521 522 case OPCODE_DPH: 523 src0 = src_vector(p, &inst->SrcReg[0], program); 524 src1 = src_vector(p, &inst->SrcReg[1], program); 525 526 i915_emit_arith(p, 527 A0_DP4, 528 get_result_vector(p, inst), 529 get_result_flags(inst), 0, 530 swizzle(src0, X, Y, Z, ONE), src1, 0); 531 break; 532 533 case OPCODE_DST: 534 src0 = src_vector(p, &inst->SrcReg[0], program); 535 src1 = src_vector(p, &inst->SrcReg[1], program); 536 537 /* result[0] = 1 * 1; 538 * result[1] = a[1] * b[1]; 539 * result[2] = a[2] * 1; 540 * result[3] = 1 * b[3]; 541 */ 542 i915_emit_arith(p, 543 A0_MUL, 544 get_result_vector(p, inst), 545 get_result_flags(inst), 0, 546 swizzle(src0, ONE, Y, Z, ONE), 547 swizzle(src1, ONE, Y, ONE, W), 0); 548 break; 549 550 case OPCODE_EX2: 551 src0 = src_vector(p, &inst->SrcReg[0], program); 552 553 i915_emit_arith(p, 554 A0_EXP, 555 get_result_vector(p, inst), 556 get_result_flags(inst), 0, 557 swizzle(src0, X, X, X, X), 0, 0); 558 break; 559 560 case OPCODE_FLR: 561 EMIT_1ARG_ARITH(A0_FLR); 562 break; 563 564 case OPCODE_TRUNC: 565 EMIT_1ARG_ARITH(A0_TRC); 566 break; 567 568 case OPCODE_FRC: 569 EMIT_1ARG_ARITH(A0_FRC); 570 break; 571 572 case OPCODE_KIL: 573 src0 = src_vector(p, &inst->SrcReg[0], program); 574 tmp = i915_get_utemp(p); 575 576 i915_emit_texld(p, get_live_regs(p, inst), 577 tmp, A0_DEST_CHANNEL_ALL, /* use a dummy dest reg */ 578 0, src0, T0_TEXKILL); 579 break; 580 581 case OPCODE_KIL_NV: 582 if (inst->DstReg.CondMask == COND_TR) { 583 tmp = i915_get_utemp(p); 584 585 /* The KIL instruction discards the fragment if any component of 586 * the source is < 0. Emit an immediate operand of {-1}.xywz. 587 */ 588 i915_emit_texld(p, get_live_regs(p, inst), 589 tmp, A0_DEST_CHANNEL_ALL, 590 0, /* use a dummy dest reg */ 591 negate(swizzle(tmp, ONE, ONE, ONE, ONE), 592 1, 1, 1, 1), 593 T0_TEXKILL); 594 } else { 595 p->error = 1; 596 i915_program_error(p, "Unsupported KIL_NV condition code: %d", 597 inst->DstReg.CondMask); 598 } 599 break; 600 601 case OPCODE_LG2: 602 src0 = src_vector(p, &inst->SrcReg[0], program); 603 604 i915_emit_arith(p, 605 A0_LOG, 606 get_result_vector(p, inst), 607 get_result_flags(inst), 0, 608 swizzle(src0, X, X, X, X), 0, 0); 609 break; 610 611 case OPCODE_LIT: 612 src0 = src_vector(p, &inst->SrcReg[0], program); 613 tmp = i915_get_utemp(p); 614 615 /* tmp = max( a.xyzw, a.00zw ) 616 * XXX: Clamp tmp.w to -128..128 617 * tmp.y = log(tmp.y) 618 * tmp.y = tmp.w * tmp.y 619 * tmp.y = exp(tmp.y) 620 * result = cmp (a.11-x1, a.1x01, a.1xy1 ) 621 */ 622 i915_emit_arith(p, A0_MAX, tmp, A0_DEST_CHANNEL_ALL, 0, 623 src0, swizzle(src0, ZERO, ZERO, Z, W), 0); 624 625 i915_emit_arith(p, A0_LOG, tmp, A0_DEST_CHANNEL_Y, 0, 626 swizzle(tmp, Y, Y, Y, Y), 0, 0); 627 628 i915_emit_arith(p, A0_MUL, tmp, A0_DEST_CHANNEL_Y, 0, 629 swizzle(tmp, ZERO, Y, ZERO, ZERO), 630 swizzle(tmp, ZERO, W, ZERO, ZERO), 0); 631 632 i915_emit_arith(p, A0_EXP, tmp, A0_DEST_CHANNEL_Y, 0, 633 swizzle(tmp, Y, Y, Y, Y), 0, 0); 634 635 i915_emit_arith(p, A0_CMP, 636 get_result_vector(p, inst), 637 get_result_flags(inst), 0, 638 negate(swizzle(tmp, ONE, ONE, X, ONE), 0, 0, 1, 0), 639 swizzle(tmp, ONE, X, ZERO, ONE), 640 swizzle(tmp, ONE, X, Y, ONE)); 641 642 break; 643 644 case OPCODE_LRP: 645 src0 = src_vector(p, &inst->SrcReg[0], program); 646 src1 = src_vector(p, &inst->SrcReg[1], program); 647 src2 = src_vector(p, &inst->SrcReg[2], program); 648 flags = get_result_flags(inst); 649 tmp = i915_get_utemp(p); 650 651 /* b*a + c*(1-a) 652 * 653 * b*a + c - ca 654 * 655 * tmp = b*a + c, 656 * result = (-c)*a + tmp 657 */ 658 i915_emit_arith(p, A0_MAD, tmp, 659 flags & A0_DEST_CHANNEL_ALL, 0, src1, src0, src2); 660 661 i915_emit_arith(p, A0_MAD, 662 get_result_vector(p, inst), 663 flags, 0, negate(src2, 1, 1, 1, 1), src0, tmp); 664 break; 665 666 case OPCODE_MAD: 667 EMIT_3ARG_ARITH(A0_MAD); 668 break; 669 670 case OPCODE_MAX: 671 EMIT_2ARG_ARITH(A0_MAX); 672 break; 673 674 case OPCODE_MIN: 675 src0 = src_vector(p, &inst->SrcReg[0], program); 676 src1 = src_vector(p, &inst->SrcReg[1], program); 677 tmp = i915_get_utemp(p); 678 flags = get_result_flags(inst); 679 680 i915_emit_arith(p, 681 A0_MAX, 682 tmp, flags & A0_DEST_CHANNEL_ALL, 0, 683 negate(src0, 1, 1, 1, 1), 684 negate(src1, 1, 1, 1, 1), 0); 685 686 i915_emit_arith(p, 687 A0_MOV, 688 get_result_vector(p, inst), 689 flags, 0, negate(tmp, 1, 1, 1, 1), 0, 0); 690 break; 691 692 case OPCODE_MOV: 693 EMIT_1ARG_ARITH(A0_MOV); 694 break; 695 696 case OPCODE_MUL: 697 EMIT_2ARG_ARITH(A0_MUL); 698 break; 699 700 case OPCODE_POW: 701 src0 = src_vector(p, &inst->SrcReg[0], program); 702 src1 = src_vector(p, &inst->SrcReg[1], program); 703 tmp = i915_get_utemp(p); 704 flags = get_result_flags(inst); 705 706 /* XXX: masking on intermediate values, here and elsewhere. 707 */ 708 i915_emit_arith(p, 709 A0_LOG, 710 tmp, A0_DEST_CHANNEL_X, 0, 711 swizzle(src0, X, X, X, X), 0, 0); 712 713 i915_emit_arith(p, A0_MUL, tmp, A0_DEST_CHANNEL_X, 0, tmp, src1, 0); 714 715 716 i915_emit_arith(p, 717 A0_EXP, 718 get_result_vector(p, inst), 719 flags, 0, swizzle(tmp, X, X, X, X), 0, 0); 720 721 break; 722 723 case OPCODE_RCP: 724 src0 = src_vector(p, &inst->SrcReg[0], program); 725 726 i915_emit_arith(p, 727 A0_RCP, 728 get_result_vector(p, inst), 729 get_result_flags(inst), 0, 730 swizzle(src0, X, X, X, X), 0, 0); 731 break; 732 733 case OPCODE_RSQ: 734 735 src0 = src_vector(p, &inst->SrcReg[0], program); 736 737 i915_emit_arith(p, 738 A0_RSQ, 739 get_result_vector(p, inst), 740 get_result_flags(inst), 0, 741 swizzle(src0, X, X, X, X), 0, 0); 742 break; 743 744 case OPCODE_SCS: 745 src0 = src_vector(p, &inst->SrcReg[0], program); 746 tmp = i915_get_utemp(p); 747 748 /* 749 * t0.xy = MUL x.xx11, x.x1111 ; x^2, x, 1, 1 750 * t0 = MUL t0.xyxy t0.xx11 ; x^4, x^3, x^2, x 751 * t1 = MUL t0.xyyw t0.yz11 ; x^7 x^5 x^3 x 752 * scs.x = DP4 t1, sin_constants 753 * t1 = MUL t0.xxz1 t0.z111 ; x^6 x^4 x^2 1 754 * scs.y = DP4 t1, cos_constants 755 */ 756 i915_emit_arith(p, 757 A0_MUL, 758 tmp, A0_DEST_CHANNEL_XY, 0, 759 swizzle(src0, X, X, ONE, ONE), 760 swizzle(src0, X, ONE, ONE, ONE), 0); 761 762 i915_emit_arith(p, 763 A0_MUL, 764 tmp, A0_DEST_CHANNEL_ALL, 0, 765 swizzle(tmp, X, Y, X, Y), 766 swizzle(tmp, X, X, ONE, ONE), 0); 767 768 if (inst->DstReg.WriteMask & WRITEMASK_Y) { 769 GLuint tmp1; 770 771 if (inst->DstReg.WriteMask & WRITEMASK_X) 772 tmp1 = i915_get_utemp(p); 773 else 774 tmp1 = tmp; 775 776 i915_emit_arith(p, 777 A0_MUL, 778 tmp1, A0_DEST_CHANNEL_ALL, 0, 779 swizzle(tmp, X, Y, Y, W), 780 swizzle(tmp, X, Z, ONE, ONE), 0); 781 782 i915_emit_arith(p, 783 A0_DP4, 784 get_result_vector(p, inst), 785 A0_DEST_CHANNEL_Y, 0, 786 swizzle(tmp1, W, Z, Y, X), 787 i915_emit_const4fv(p, sin_constants), 0); 788 } 789 790 if (inst->DstReg.WriteMask & WRITEMASK_X) { 791 i915_emit_arith(p, 792 A0_MUL, 793 tmp, A0_DEST_CHANNEL_XYZ, 0, 794 swizzle(tmp, X, X, Z, ONE), 795 swizzle(tmp, Z, ONE, ONE, ONE), 0); 796 797 i915_emit_arith(p, 798 A0_DP4, 799 get_result_vector(p, inst), 800 A0_DEST_CHANNEL_X, 0, 801 swizzle(tmp, ONE, Z, Y, X), 802 i915_emit_const4fv(p, cos_constants), 0); 803 } 804 break; 805 806 case OPCODE_SEQ: 807 tmp = i915_get_utemp(p); 808 flags = get_result_flags(inst); 809 dst = get_result_vector(p, inst); 810 811 /* tmp = src1 >= src2 */ 812 i915_emit_arith(p, 813 A0_SGE, 814 tmp, 815 flags, 0, 816 src_vector(p, &inst->SrcReg[0], program), 817 src_vector(p, &inst->SrcReg[1], program), 818 0); 819 /* dst = src1 <= src2 */ 820 i915_emit_arith(p, 821 A0_SGE, 822 dst, 823 flags, 0, 824 negate(src_vector(p, &inst->SrcReg[0], program), 825 1, 1, 1, 1), 826 negate(src_vector(p, &inst->SrcReg[1], program), 827 1, 1, 1, 1), 828 0); 829 /* dst = tmp && dst */ 830 i915_emit_arith(p, 831 A0_MUL, 832 dst, 833 flags, 0, 834 dst, 835 tmp, 836 0); 837 break; 838 839 case OPCODE_SIN: 840 src0 = src_vector(p, &inst->SrcReg[0], program); 841 tmp = i915_get_utemp(p); 842 consts0 = i915_emit_const4fv(p, sin_quad_constants[0]); 843 consts1 = i915_emit_const4fv(p, sin_quad_constants[1]); 844 845 /* Reduce range from repeating about [-pi,pi] to [-1,1] */ 846 i915_emit_arith(p, 847 A0_MAD, 848 tmp, A0_DEST_CHANNEL_X, 0, 849 src0, 850 swizzle(consts1, Z, ZERO, ZERO, ZERO), /* 1/(2pi) */ 851 swizzle(consts0, Z, ZERO, ZERO, ZERO)); /* .5 */ 852 853 i915_emit_arith(p, A0_FRC, tmp, A0_DEST_CHANNEL_X, 0, tmp, 0, 0); 854 855 i915_emit_arith(p, 856 A0_MAD, 857 tmp, A0_DEST_CHANNEL_X, 0, 858 tmp, 859 swizzle(consts0, X, ZERO, ZERO, ZERO), /* 2 */ 860 swizzle(consts0, Y, ZERO, ZERO, ZERO)); /* -1 */ 861 862 /* Compute sin using a quadratic and quartic. It gives continuity 863 * that repeating the Taylor series lacks every 2*pi, and has 864 * reduced error. 865 * 866 * The idea was described at: 867 * http://www.devmaster.net/forums/showthread.php?t=5784 868 */ 869 870 /* tmp.y = abs(tmp.x); {x, abs(x), 0, 0} */ 871 i915_emit_arith(p, 872 A0_MAX, 873 tmp, A0_DEST_CHANNEL_Y, 0, 874 swizzle(tmp, ZERO, X, ZERO, ZERO), 875 negate(swizzle(tmp, ZERO, X, ZERO, ZERO), 0, 1, 0, 0), 876 0); 877 878 /* tmp.y = tmp.y * tmp.x; {x, x * abs(x), 0, 0} */ 879 i915_emit_arith(p, 880 A0_MUL, 881 tmp, A0_DEST_CHANNEL_Y, 0, 882 swizzle(tmp, ZERO, X, ZERO, ZERO), 883 tmp, 884 0); 885 886 /* tmp.x = tmp.xy DP sin_quad_constants[2].xy */ 887 i915_emit_arith(p, 888 A0_DP3, 889 tmp, A0_DEST_CHANNEL_X, 0, 890 tmp, 891 swizzle(consts1, X, Y, ZERO, ZERO), 892 0); 893 894 /* tmp.x now contains a first approximation (y). Now, weight it 895 * against tmp.y**2 to get closer. 896 */ 897 i915_emit_arith(p, 898 A0_MAX, 899 tmp, A0_DEST_CHANNEL_Y, 0, 900 swizzle(tmp, ZERO, X, ZERO, ZERO), 901 negate(swizzle(tmp, ZERO, X, ZERO, ZERO), 0, 1, 0, 0), 902 0); 903 904 /* tmp.y = tmp.x * tmp.y - tmp.x; {y, y * abs(y) - y, 0, 0} */ 905 i915_emit_arith(p, 906 A0_MAD, 907 tmp, A0_DEST_CHANNEL_Y, 0, 908 swizzle(tmp, ZERO, X, ZERO, ZERO), 909 swizzle(tmp, ZERO, Y, ZERO, ZERO), 910 negate(swizzle(tmp, ZERO, X, ZERO, ZERO), 0, 1, 0, 0)); 911 912 /* result = .2225 * tmp.y + tmp.x =.2225(y * abs(y) - y) + y= */ 913 i915_emit_arith(p, 914 A0_MAD, 915 get_result_vector(p, inst), 916 get_result_flags(inst), 0, 917 swizzle(consts1, W, W, W, W), 918 swizzle(tmp, Y, Y, Y, Y), 919 swizzle(tmp, X, X, X, X)); 920 921 break; 922 923 case OPCODE_SGE: 924 EMIT_2ARG_ARITH(A0_SGE); 925 break; 926 927 case OPCODE_SGT: 928 i915_emit_arith(p, 929 A0_SLT, 930 get_result_vector( p, inst ), 931 get_result_flags( inst ), 0, 932 negate(src_vector( p, &inst->SrcReg[0], program), 933 1, 1, 1, 1), 934 negate(src_vector( p, &inst->SrcReg[1], program), 935 1, 1, 1, 1), 936 0); 937 break; 938 939 case OPCODE_SLE: 940 i915_emit_arith(p, 941 A0_SGE, 942 get_result_vector( p, inst ), 943 get_result_flags( inst ), 0, 944 negate(src_vector( p, &inst->SrcReg[0], program), 945 1, 1, 1, 1), 946 negate(src_vector( p, &inst->SrcReg[1], program), 947 1, 1, 1, 1), 948 0); 949 break; 950 951 case OPCODE_SLT: 952 EMIT_2ARG_ARITH(A0_SLT); 953 break; 954 955 case OPCODE_SNE: 956 tmp = i915_get_utemp(p); 957 flags = get_result_flags(inst); 958 dst = get_result_vector(p, inst); 959 960 /* tmp = src1 < src2 */ 961 i915_emit_arith(p, 962 A0_SLT, 963 tmp, 964 flags, 0, 965 src_vector(p, &inst->SrcReg[0], program), 966 src_vector(p, &inst->SrcReg[1], program), 967 0); 968 /* dst = src1 > src2 */ 969 i915_emit_arith(p, 970 A0_SLT, 971 dst, 972 flags, 0, 973 negate(src_vector(p, &inst->SrcReg[0], program), 974 1, 1, 1, 1), 975 negate(src_vector(p, &inst->SrcReg[1], program), 976 1, 1, 1, 1), 977 0); 978 /* dst = tmp || dst */ 979 i915_emit_arith(p, 980 A0_ADD, 981 dst, 982 flags | A0_DEST_SATURATE, 0, 983 dst, 984 tmp, 985 0); 986 break; 987 988 case OPCODE_SSG: 989 dst = get_result_vector(p, inst); 990 flags = get_result_flags(inst); 991 src0 = src_vector(p, &inst->SrcReg[0], program); 992 tmp = i915_get_utemp(p); 993 994 /* tmp = (src < 0.0) */ 995 i915_emit_arith(p, 996 A0_SLT, 997 tmp, 998 flags, 0, 999 src0, 1000 swizzle(src0, ZERO, ZERO, ZERO, ZERO), 1001 0); 1002 1003 /* dst = (0.0 < src) */ 1004 i915_emit_arith(p, 1005 A0_SLT, 1006 dst, 1007 flags, 0, 1008 swizzle(src0, ZERO, ZERO, ZERO, ZERO), 1009 src0, 1010 0); 1011 1012 /* dst = (src > 0.0) - (src < 0.0) */ 1013 i915_emit_arith(p, 1014 A0_ADD, 1015 dst, 1016 flags, 0, 1017 dst, 1018 negate(tmp, 1, 1, 1, 1), 1019 0); 1020 1021 break; 1022 1023 case OPCODE_SUB: 1024 src0 = src_vector(p, &inst->SrcReg[0], program); 1025 src1 = src_vector(p, &inst->SrcReg[1], program); 1026 1027 i915_emit_arith(p, 1028 A0_ADD, 1029 get_result_vector(p, inst), 1030 get_result_flags(inst), 0, 1031 src0, negate(src1, 1, 1, 1, 1), 0); 1032 break; 1033 1034 case OPCODE_SWZ: 1035 EMIT_1ARG_ARITH(A0_MOV); /* extended swizzle handled natively */ 1036 break; 1037 1038 case OPCODE_TEX: 1039 EMIT_TEX(T0_TEXLD); 1040 break; 1041 1042 case OPCODE_TXB: 1043 EMIT_TEX(T0_TEXLDB); 1044 break; 1045 1046 case OPCODE_TXP: 1047 EMIT_TEX(T0_TEXLDP); 1048 break; 1049 1050 case OPCODE_XPD: 1051 /* Cross product: 1052 * result.x = src0.y * src1.z - src0.z * src1.y; 1053 * result.y = src0.z * src1.x - src0.x * src1.z; 1054 * result.z = src0.x * src1.y - src0.y * src1.x; 1055 * result.w = undef; 1056 */ 1057 src0 = src_vector(p, &inst->SrcReg[0], program); 1058 src1 = src_vector(p, &inst->SrcReg[1], program); 1059 tmp = i915_get_utemp(p); 1060 1061 i915_emit_arith(p, 1062 A0_MUL, 1063 tmp, A0_DEST_CHANNEL_ALL, 0, 1064 swizzle(src0, Z, X, Y, ONE), 1065 swizzle(src1, Y, Z, X, ONE), 0); 1066 1067 i915_emit_arith(p, 1068 A0_MAD, 1069 get_result_vector(p, inst), 1070 get_result_flags(inst), 0, 1071 swizzle(src0, Y, Z, X, ONE), 1072 swizzle(src1, Z, X, Y, ONE), 1073 negate(tmp, 1, 1, 1, 0)); 1074 break; 1075 1076 case OPCODE_END: 1077 return; 1078 1079 case OPCODE_BGNLOOP: 1080 case OPCODE_BGNSUB: 1081 case OPCODE_BRA: 1082 case OPCODE_BRK: 1083 case OPCODE_CAL: 1084 case OPCODE_CONT: 1085 case OPCODE_DDX: 1086 case OPCODE_DDY: 1087 case OPCODE_ELSE: 1088 case OPCODE_ENDIF: 1089 case OPCODE_ENDLOOP: 1090 case OPCODE_ENDSUB: 1091 case OPCODE_IF: 1092 case OPCODE_RET: 1093 p->error = 1; 1094 i915_program_error(p, "Unsupported opcode: %s", 1095 _mesa_opcode_string(inst->Opcode)); 1096 return; 1097 1098 case OPCODE_EXP: 1099 case OPCODE_LOG: 1100 /* These opcodes are claimed as GLSL, NV_vp, and ARB_vp in 1101 * prog_instruction.h, but apparently GLSL doesn't ever emit them. 1102 * Instead, it translates to EX2 or LG2. 1103 */ 1104 case OPCODE_TXD: 1105 case OPCODE_TXL: 1106 /* These opcodes are claimed by GLSL in prog_instruction.h, but 1107 * only NV_vp/fp appears to emit them. 1108 */ 1109 default: 1110 i915_program_error(p, "bad opcode: %s", 1111 _mesa_opcode_string(inst->Opcode)); 1112 return; 1113 } 1114 1115 inst++; 1116 i915_release_utemps(p); 1117 } 1118} 1119 1120/* Rather than trying to intercept and jiggle depth writes during 1121 * emit, just move the value into its correct position at the end of 1122 * the program: 1123 */ 1124static void 1125fixup_depth_write(struct i915_fragment_program *p) 1126{ 1127 if (p->depth_written) { 1128 GLuint depth = UREG(REG_TYPE_OD, 0); 1129 1130 i915_emit_arith(p, 1131 A0_MOV, 1132 depth, A0_DEST_CHANNEL_W, 0, 1133 swizzle(depth, X, Y, Z, Z), 0, 0); 1134 } 1135} 1136 1137 1138static void 1139check_wpos(struct i915_fragment_program *p) 1140{ 1141 GLuint inputs = p->FragProg.Base.InputsRead; 1142 GLint i; 1143 1144 p->wpos_tex = -1; 1145 1146 for (i = 0; i < p->ctx->Const.MaxTextureCoordUnits; i++) { 1147 if (inputs & (FRAG_BIT_TEX(i) | FRAG_BIT_VAR(i))) 1148 continue; 1149 else if (inputs & FRAG_BIT_WPOS) { 1150 p->wpos_tex = i; 1151 inputs &= ~FRAG_BIT_WPOS; 1152 } 1153 } 1154 1155 if (inputs & FRAG_BIT_WPOS) { 1156 i915_program_error(p, "No free texcoord for wpos value"); 1157 } 1158} 1159 1160 1161static void 1162translate_program(struct i915_fragment_program *p) 1163{ 1164 struct i915_context *i915 = I915_CONTEXT(p->ctx); 1165 1166 if (INTEL_DEBUG & DEBUG_WM) { 1167 printf("fp:\n"); 1168 _mesa_print_program(&p->FragProg.Base); 1169 printf("\n"); 1170 } 1171 1172 i915_init_program(i915, p); 1173 check_wpos(p); 1174 upload_program(p); 1175 fixup_depth_write(p); 1176 i915_fini_program(p); 1177 1178 p->translated = 1; 1179} 1180 1181 1182static void 1183track_params(struct i915_fragment_program *p) 1184{ 1185 GLint i; 1186 1187 if (p->nr_params) 1188 _mesa_load_state_parameters(p->ctx, p->FragProg.Base.Parameters); 1189 1190 for (i = 0; i < p->nr_params; i++) { 1191 GLint reg = p->param[i].reg; 1192 COPY_4V(p->constant[reg], p->param[i].values); 1193 } 1194 1195 p->params_uptodate = 1; 1196 p->on_hardware = 0; /* overkill */ 1197} 1198 1199 1200static void 1201i915BindProgram(struct gl_context * ctx, GLenum target, struct gl_program *prog) 1202{ 1203 if (target == GL_FRAGMENT_PROGRAM_ARB) { 1204 struct i915_context *i915 = I915_CONTEXT(ctx); 1205 struct i915_fragment_program *p = (struct i915_fragment_program *) prog; 1206 1207 if (i915->current_program == p) 1208 return; 1209 1210 if (i915->current_program) { 1211 i915->current_program->on_hardware = 0; 1212 i915->current_program->params_uptodate = 0; 1213 } 1214 1215 i915->current_program = p; 1216 1217 assert(p->on_hardware == 0); 1218 assert(p->params_uptodate == 0); 1219 1220 } 1221} 1222 1223static struct gl_program * 1224i915NewProgram(struct gl_context * ctx, GLenum target, GLuint id) 1225{ 1226 switch (target) { 1227 case GL_VERTEX_PROGRAM_ARB: 1228 return _mesa_init_vertex_program(ctx, CALLOC_STRUCT(gl_vertex_program), 1229 target, id); 1230 1231 case GL_FRAGMENT_PROGRAM_ARB:{ 1232 struct i915_fragment_program *prog = 1233 CALLOC_STRUCT(i915_fragment_program); 1234 if (prog) { 1235 i915_init_program(I915_CONTEXT(ctx), prog); 1236 1237 return _mesa_init_fragment_program(ctx, &prog->FragProg, 1238 target, id); 1239 } 1240 else 1241 return NULL; 1242 } 1243 1244 default: 1245 /* Just fallback: 1246 */ 1247 return _mesa_new_program(ctx, target, id); 1248 } 1249} 1250 1251static void 1252i915DeleteProgram(struct gl_context * ctx, struct gl_program *prog) 1253{ 1254 if (prog->Target == GL_FRAGMENT_PROGRAM_ARB) { 1255 struct i915_context *i915 = I915_CONTEXT(ctx); 1256 struct i915_fragment_program *p = (struct i915_fragment_program *) prog; 1257 1258 if (i915->current_program == p) 1259 i915->current_program = 0; 1260 } 1261 1262 _mesa_delete_program(ctx, prog); 1263} 1264 1265 1266static GLboolean 1267i915IsProgramNative(struct gl_context * ctx, GLenum target, struct gl_program *prog) 1268{ 1269 if (target == GL_FRAGMENT_PROGRAM_ARB) { 1270 struct i915_fragment_program *p = (struct i915_fragment_program *) prog; 1271 1272 if (!p->translated) 1273 translate_program(p); 1274 1275 return !p->error; 1276 } 1277 else 1278 return GL_TRUE; 1279} 1280 1281static GLboolean 1282i915ProgramStringNotify(struct gl_context * ctx, 1283 GLenum target, struct gl_program *prog) 1284{ 1285 if (target == GL_FRAGMENT_PROGRAM_ARB) { 1286 struct i915_fragment_program *p = (struct i915_fragment_program *) prog; 1287 p->translated = 0; 1288 } 1289 1290 (void) _tnl_program_string(ctx, target, prog); 1291 1292 /* XXX check if program is legal, within limits */ 1293 return GL_TRUE; 1294} 1295 1296void 1297i915_update_program(struct gl_context *ctx) 1298{ 1299 struct intel_context *intel = intel_context(ctx); 1300 struct i915_context *i915 = i915_context(&intel->ctx); 1301 struct i915_fragment_program *fp = 1302 (struct i915_fragment_program *) ctx->FragmentProgram._Current; 1303 1304 if (i915->current_program != fp) { 1305 if (i915->current_program) { 1306 i915->current_program->on_hardware = 0; 1307 i915->current_program->params_uptodate = 0; 1308 } 1309 1310 i915->current_program = fp; 1311 } 1312 1313 if (!fp->translated) 1314 translate_program(fp); 1315 1316 FALLBACK(&i915->intel, I915_FALLBACK_PROGRAM, fp->error); 1317} 1318 1319void 1320i915ValidateFragmentProgram(struct i915_context *i915) 1321{ 1322 struct gl_context *ctx = &i915->intel.ctx; 1323 struct intel_context *intel = intel_context(ctx); 1324 TNLcontext *tnl = TNL_CONTEXT(ctx); 1325 struct vertex_buffer *VB = &tnl->vb; 1326 1327 struct i915_fragment_program *p = 1328 (struct i915_fragment_program *) ctx->FragmentProgram._Current; 1329 1330 const GLuint inputsRead = p->FragProg.Base.InputsRead; 1331 GLuint s4 = i915->state.Ctx[I915_CTXREG_LIS4] & ~S4_VFMT_MASK; 1332 GLuint s2 = S2_TEXCOORD_NONE; 1333 int i, offset = 0; 1334 1335 /* Important: 1336 */ 1337 VB->AttribPtr[VERT_ATTRIB_POS] = VB->NdcPtr; 1338 1339 if (!p->translated) 1340 translate_program(p); 1341 1342 intel->vertex_attr_count = 0; 1343 intel->wpos_offset = 0; 1344 intel->wpos_size = 0; 1345 intel->coloroffset = 0; 1346 intel->specoffset = 0; 1347 1348 if (inputsRead & FRAG_BITS_TEX_ANY) { 1349 EMIT_ATTR(_TNL_ATTRIB_POS, EMIT_4F_VIEWPORT, S4_VFMT_XYZW, 16); 1350 } 1351 else { 1352 EMIT_ATTR(_TNL_ATTRIB_POS, EMIT_3F_VIEWPORT, S4_VFMT_XYZ, 12); 1353 } 1354 1355 if (inputsRead & FRAG_BIT_COL0) { 1356 intel->coloroffset = offset / 4; 1357 EMIT_ATTR(_TNL_ATTRIB_COLOR0, EMIT_4UB_4F_BGRA, S4_VFMT_COLOR, 4); 1358 } 1359 1360 if (inputsRead & FRAG_BIT_COL1) { 1361 intel->specoffset = offset / 4; 1362 EMIT_ATTR(_TNL_ATTRIB_COLOR1, EMIT_4UB_4F_BGRA, S4_VFMT_SPEC_FOG, 4); 1363 } 1364 1365 if ((inputsRead & FRAG_BIT_FOGC)) { 1366 EMIT_ATTR(_TNL_ATTRIB_FOG, EMIT_1F, S4_VFMT_FOG_PARAM, 4); 1367 } 1368 1369 for (i = 0; i < p->ctx->Const.MaxTextureCoordUnits; i++) { 1370 if (inputsRead & FRAG_BIT_TEX(i)) { 1371 int sz = VB->AttribPtr[_TNL_ATTRIB_TEX0 + i]->size; 1372 1373 s2 &= ~S2_TEXCOORD_FMT(i, S2_TEXCOORD_FMT0_MASK); 1374 s2 |= S2_TEXCOORD_FMT(i, SZ_TO_HW(sz)); 1375 1376 EMIT_ATTR(_TNL_ATTRIB_TEX0 + i, EMIT_SZ(sz), 0, sz * 4); 1377 } 1378 else if (inputsRead & FRAG_BIT_VAR(i)) { 1379 int sz = VB->AttribPtr[_TNL_ATTRIB_GENERIC0 + i]->size; 1380 1381 s2 &= ~S2_TEXCOORD_FMT(i, S2_TEXCOORD_FMT0_MASK); 1382 s2 |= S2_TEXCOORD_FMT(i, SZ_TO_HW(sz)); 1383 1384 EMIT_ATTR(_TNL_ATTRIB_GENERIC0 + i, EMIT_SZ(sz), 0, sz * 4); 1385 } 1386 else if (i == p->wpos_tex) { 1387 1388 /* If WPOS is required, duplicate the XYZ position data in an 1389 * unused texture coordinate: 1390 */ 1391 s2 &= ~S2_TEXCOORD_FMT(i, S2_TEXCOORD_FMT0_MASK); 1392 s2 |= S2_TEXCOORD_FMT(i, SZ_TO_HW(3)); 1393 1394 intel->wpos_offset = offset; 1395 intel->wpos_size = 3 * sizeof(GLuint); 1396 1397 EMIT_PAD(intel->wpos_size); 1398 } 1399 } 1400 1401 if (s2 != i915->state.Ctx[I915_CTXREG_LIS2] || 1402 s4 != i915->state.Ctx[I915_CTXREG_LIS4]) { 1403 int k; 1404 1405 I915_STATECHANGE(i915, I915_UPLOAD_CTX); 1406 1407 /* Must do this *after* statechange, so as not to affect 1408 * buffered vertices reliant on the old state: 1409 */ 1410 intel->vertex_size = _tnl_install_attrs(&intel->ctx, 1411 intel->vertex_attrs, 1412 intel->vertex_attr_count, 1413 intel->ViewportMatrix.m, 0); 1414 1415 assert(intel->prim.current_offset == intel->prim.start_offset); 1416 intel->prim.start_offset = (intel->prim.current_offset + intel->vertex_size-1) / intel->vertex_size * intel->vertex_size; 1417 intel->prim.current_offset = intel->prim.start_offset; 1418 1419 intel->vertex_size >>= 2; 1420 1421 i915->state.Ctx[I915_CTXREG_LIS2] = s2; 1422 i915->state.Ctx[I915_CTXREG_LIS4] = s4; 1423 1424 k = intel->vtbl.check_vertex_size(intel, intel->vertex_size); 1425 assert(k); 1426 } 1427 1428 if (!p->params_uptodate) 1429 track_params(p); 1430 1431 if (!p->on_hardware) 1432 i915_upload_program(i915, p); 1433 1434 if (INTEL_DEBUG & DEBUG_WM) { 1435 printf("i915:\n"); 1436 i915_disassemble_program(i915->state.Program, i915->state.ProgramSize); 1437 } 1438} 1439 1440void 1441i915InitFragProgFuncs(struct dd_function_table *functions) 1442{ 1443 functions->BindProgram = i915BindProgram; 1444 functions->NewProgram = i915NewProgram; 1445 functions->DeleteProgram = i915DeleteProgram; 1446 functions->IsProgramNative = i915IsProgramNative; 1447 functions->ProgramStringNotify = i915ProgramStringNotify; 1448} 1449