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