Keith Whitwell | 15e75e0 | 2005-04-29 15:11:39 +0000 | [diff] [blame^] | 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 <strings.h> |
| 29 | |
| 30 | #include "glheader.h" |
| 31 | #include "macros.h" |
| 32 | #include "enums.h" |
| 33 | #include "texenvprogram.h" |
| 34 | |
| 35 | #include "shader/program.h" |
| 36 | #include "shader/nvfragprog.h" |
| 37 | #include "shader/arbfragparse.h" |
| 38 | |
| 39 | |
| 40 | #define DISASSEM 1 |
| 41 | |
| 42 | /* Use uregs to represent registers internally, translate to Mesa's |
| 43 | * expected formats on emit. |
| 44 | * |
| 45 | * NOTE: These are passed by value extensively in this file rather |
| 46 | * than as usual by pointer reference. If this disturbs you, try |
| 47 | * remembering they are just 32bits in size. |
| 48 | * |
| 49 | * GCC is smart enough to deal with these dword-sized structures in |
| 50 | * much the same way as if I had defined them as dwords and was using |
| 51 | * macros to access and set the fields. This is much nicer and easier |
| 52 | * to evolve. |
| 53 | */ |
| 54 | struct ureg { |
| 55 | GLuint file:4; |
| 56 | GLuint idx:8; |
| 57 | GLuint negatebase:1; |
| 58 | GLuint abs:1; |
| 59 | GLuint negateabs:1; |
| 60 | GLuint swz:12; |
| 61 | GLuint pad:5; |
| 62 | }; |
| 63 | |
| 64 | const static struct ureg undef = { |
| 65 | ~0, |
| 66 | ~0, |
| 67 | 0, |
| 68 | 0, |
| 69 | 0, |
| 70 | 0, |
| 71 | 0 |
| 72 | }; |
| 73 | |
| 74 | #define X 0 |
| 75 | #define Y 1 |
| 76 | #define Z 2 |
| 77 | #define W 3 |
| 78 | |
| 79 | #define MAX_CONSTANT 32 |
| 80 | |
| 81 | /* State used to build the fragment program: |
| 82 | */ |
| 83 | struct texenv_fragment_program { |
| 84 | struct fragment_program *prog; |
| 85 | GLcontext *ctx; |
| 86 | |
| 87 | GLfloat constant[MAX_CONSTANT][4]; |
| 88 | GLuint constant_flags[MAX_CONSTANT]; |
| 89 | GLuint nr_constants; |
| 90 | |
| 91 | GLuint temp_flag; /* Tracks temporary regs which are in |
| 92 | * use. |
| 93 | */ |
| 94 | |
| 95 | |
| 96 | struct { |
| 97 | GLuint reg; /* Hardware constant idx */ |
| 98 | const GLfloat *values; /* Pointer to tracked values */ |
| 99 | } param[MAX_CONSTANT]; |
| 100 | GLuint nr_params; |
| 101 | |
| 102 | |
| 103 | GLboolean error; |
| 104 | |
| 105 | struct ureg src_texture; /* Reg containing sampled texture color, |
| 106 | * else undef. |
| 107 | */ |
| 108 | |
| 109 | struct ureg src_previous; /* Reg containing color from previous |
| 110 | * stage. May need to be decl'd. |
| 111 | */ |
| 112 | |
| 113 | GLuint last_tex_stage; /* Number of last enabled texture unit */ |
| 114 | }; |
| 115 | |
| 116 | |
| 117 | |
| 118 | static struct ureg make_ureg(GLuint file, GLuint idx) |
| 119 | { |
| 120 | struct ureg reg; |
| 121 | reg.file = file; |
| 122 | reg.idx = idx; |
| 123 | reg.negatebase = 0; |
| 124 | reg.abs = 0; |
| 125 | reg.negateabs = 0; |
| 126 | reg.swz = SWIZZLE_NOOP; |
| 127 | reg.pad = 0; |
| 128 | return reg; |
| 129 | } |
| 130 | |
| 131 | static struct ureg swizzle( struct ureg reg, int x, int y, int z, int w ) |
| 132 | { |
| 133 | reg.swz = MAKE_SWIZZLE4(GET_SWZ(reg.swz, x), |
| 134 | GET_SWZ(reg.swz, y), |
| 135 | GET_SWZ(reg.swz, z), |
| 136 | GET_SWZ(reg.swz, w)); |
| 137 | |
| 138 | return reg; |
| 139 | } |
| 140 | |
| 141 | static struct ureg swizzle1( struct ureg reg, int x ) |
| 142 | { |
| 143 | return swizzle(reg, x, x, x, x); |
| 144 | } |
| 145 | |
| 146 | static GLboolean is_undef( struct ureg reg ) |
| 147 | { |
| 148 | return reg.file == 0xf; |
| 149 | } |
| 150 | |
| 151 | static struct ureg get_temp( struct texenv_fragment_program *p ) |
| 152 | { |
| 153 | int bit = ffs( ~p->temp_flag ); |
| 154 | if (!bit) { |
| 155 | fprintf(stderr, "%s: out of temporaries\n", __FILE__); |
| 156 | exit(1); |
| 157 | } |
| 158 | |
| 159 | p->temp_flag |= 1<<(bit-1); |
| 160 | return make_ureg(PROGRAM_TEMPORARY, (bit-1)); |
| 161 | } |
| 162 | |
| 163 | |
| 164 | static void release_temps( struct texenv_fragment_program *p ) |
| 165 | { |
| 166 | p->temp_flag = ~0x7; |
| 167 | } |
| 168 | |
| 169 | |
| 170 | static struct ureg emit_decl( struct texenv_fragment_program *p, |
| 171 | GLuint type, GLuint nr ) |
| 172 | { |
| 173 | struct ureg reg = make_ureg(type, nr); |
| 174 | |
| 175 | if (type == PROGRAM_INPUT) { |
| 176 | p->prog->InputsRead |= 1<<nr; |
| 177 | } |
| 178 | else { |
| 179 | /* Other ??? |
| 180 | */ |
| 181 | } |
| 182 | |
| 183 | return reg; |
| 184 | } |
| 185 | |
| 186 | static void emit_arg( struct fp_src_register *reg, |
| 187 | struct ureg ureg ) |
| 188 | { |
| 189 | reg->File = ureg.file; |
| 190 | reg->Index = ureg.idx; |
| 191 | reg->Swizzle = ureg.swz; |
| 192 | reg->NegateBase = ureg.negatebase; |
| 193 | reg->Abs = ureg.abs; |
| 194 | reg->NegateAbs = ureg.negateabs; |
| 195 | } |
| 196 | |
| 197 | static void emit_dst( struct fp_dst_register *dst, |
| 198 | struct ureg ureg, GLuint mask ) |
| 199 | { |
| 200 | dst->File = ureg.file; |
| 201 | dst->Index = ureg.idx; |
| 202 | dst->WriteMask = mask; |
| 203 | dst->CondMask = 0; |
| 204 | dst->CondSwizzle = 0; |
| 205 | } |
| 206 | |
| 207 | static struct fp_instruction * |
| 208 | emit_op(struct texenv_fragment_program *p, |
| 209 | GLuint op, |
| 210 | struct ureg dest, |
| 211 | GLuint mask, |
| 212 | GLuint saturate, |
| 213 | struct ureg src0, |
| 214 | struct ureg src1, |
| 215 | struct ureg src2 ) |
| 216 | { |
| 217 | GLuint nr = p->prog->Base.NumInstructions++; |
| 218 | struct fp_instruction *inst = &p->prog->Instructions[nr]; |
| 219 | |
| 220 | memset(inst, 0, sizeof(*inst)); |
| 221 | inst->Opcode = op; |
| 222 | |
| 223 | if (!is_undef(src0)) emit_arg( &inst->SrcReg[0], src0 ); |
| 224 | if (!is_undef(src1)) emit_arg( &inst->SrcReg[1], src1 ); |
| 225 | if (!is_undef(src2)) emit_arg( &inst->SrcReg[2], src2 ); |
| 226 | |
| 227 | inst->Saturate = saturate; |
| 228 | |
| 229 | emit_dst( &inst->DstReg, dest, mask ); |
| 230 | |
| 231 | return inst; |
| 232 | } |
| 233 | |
| 234 | |
| 235 | static struct ureg emit_arith( struct texenv_fragment_program *p, |
| 236 | GLuint op, |
| 237 | struct ureg dest, |
| 238 | GLuint mask, |
| 239 | GLuint saturate, |
| 240 | struct ureg src0, |
| 241 | struct ureg src1, |
| 242 | struct ureg src2 ) |
| 243 | { |
| 244 | emit_op(p, op, dest, mask, saturate, src0, src1, src2); |
| 245 | |
| 246 | p->prog->NumAluInstructions++; |
| 247 | return dest; |
| 248 | } |
| 249 | |
| 250 | static struct ureg emit_texld( struct texenv_fragment_program *p, |
| 251 | GLuint op, |
| 252 | struct ureg dest, |
| 253 | GLuint destmask, |
| 254 | GLuint tex_unit, |
| 255 | GLuint tex_idx, |
| 256 | struct ureg coord ) |
| 257 | { |
| 258 | struct fp_instruction *inst = emit_op( p, op, |
| 259 | dest, destmask, |
| 260 | 0, /* don't saturate? */ |
| 261 | coord, /* arg 0? */ |
| 262 | undef, |
| 263 | undef); |
| 264 | |
| 265 | inst->TexSrcIdx = tex_idx; |
| 266 | inst->TexSrcUnit = tex_unit; |
| 267 | |
| 268 | p->prog->NumTexInstructions++; |
| 269 | |
| 270 | if (coord.file != PROGRAM_INPUT && |
| 271 | (coord.idx < VERT_ATTRIB_TEX0 || |
| 272 | coord.idx > VERT_ATTRIB_TEX7)) { |
| 273 | p->prog->NumTexIndirections++; |
| 274 | } |
| 275 | |
| 276 | return dest; |
| 277 | } |
| 278 | |
| 279 | |
| 280 | static struct ureg emit_const1f( struct texenv_fragment_program *p, GLfloat c0 ) |
| 281 | { |
| 282 | GLint reg, idx; |
| 283 | |
| 284 | for (reg = 0; reg < MAX_CONSTANT; reg++) { |
| 285 | for (idx = 0; idx < 4; idx++) { |
| 286 | if (!(p->constant_flags[reg] & (1<<idx)) || |
| 287 | p->constant[reg][idx] == c0) { |
| 288 | p->constant[reg][idx] = c0; |
| 289 | p->constant_flags[reg] |= 1<<idx; |
| 290 | if (reg+1 > p->nr_constants) p->nr_constants = reg+1; |
| 291 | return swizzle1(make_ureg(PROGRAM_LOCAL_PARAM, reg),idx); |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | fprintf(stderr, "%s: out of constants\n", __FUNCTION__); |
| 297 | p->error = 1; |
| 298 | return undef; |
| 299 | } |
| 300 | |
| 301 | static struct ureg emit_const2f( struct texenv_fragment_program *p, |
| 302 | GLfloat c0, GLfloat c1 ) |
| 303 | { |
| 304 | GLint reg, idx; |
| 305 | |
| 306 | for (reg = 0; reg < MAX_CONSTANT; reg++) { |
| 307 | if (p->constant_flags[reg] == 0xf) |
| 308 | continue; |
| 309 | for (idx = 0; idx < 3; idx++) { |
| 310 | if (!(p->constant_flags[reg] & (3<<idx))) { |
| 311 | p->constant[reg][idx] = c0; |
| 312 | p->constant[reg][idx+1] = c1; |
| 313 | p->constant_flags[reg] |= 3<<idx; |
| 314 | if (reg+1 > p->nr_constants) p->nr_constants = reg+1; |
| 315 | return swizzle(make_ureg(PROGRAM_LOCAL_PARAM, reg),idx,idx+1,idx,idx+1); |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | fprintf(stderr, "%s: out of constants\n", __FUNCTION__); |
| 321 | p->error = 1; |
| 322 | return undef; |
| 323 | } |
| 324 | |
| 325 | |
| 326 | |
| 327 | static struct ureg emit_const4f( struct texenv_fragment_program *p, |
| 328 | GLfloat c0, GLfloat c1, GLfloat c2, GLfloat c3 ) |
| 329 | { |
| 330 | GLint reg; |
| 331 | |
| 332 | for (reg = 0; reg < MAX_CONSTANT; reg++) { |
| 333 | if (p->constant_flags[reg] == 0xf && |
| 334 | p->constant[reg][0] == c0 && |
| 335 | p->constant[reg][1] == c1 && |
| 336 | p->constant[reg][2] == c2 && |
| 337 | p->constant[reg][3] == c3) { |
| 338 | return make_ureg(PROGRAM_LOCAL_PARAM, reg); |
| 339 | } |
| 340 | else if (p->constant_flags[reg] == 0) { |
| 341 | p->constant[reg][0] = c0; |
| 342 | p->constant[reg][1] = c1; |
| 343 | p->constant[reg][2] = c2; |
| 344 | p->constant[reg][3] = c3; |
| 345 | p->constant_flags[reg] = 0xf; |
| 346 | if (reg+1 > p->nr_constants) p->nr_constants = reg+1; |
| 347 | return make_ureg(PROGRAM_LOCAL_PARAM, reg); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | fprintf(stderr, "%s: out of constants\n", __FUNCTION__); |
| 352 | p->error = 1; |
| 353 | return undef; |
| 354 | } |
| 355 | |
| 356 | |
| 357 | static struct ureg emit_const4fv( struct texenv_fragment_program *p, const GLfloat *c ) |
| 358 | { |
| 359 | return emit_const4f( p, c[0], c[1], c[2], c[3] ); |
| 360 | } |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | static void program_error( struct texenv_fragment_program *p, const char *msg ) |
| 368 | { |
| 369 | fprintf(stderr, "%s\n", msg); |
| 370 | p->error = 1; |
| 371 | } |
| 372 | |
| 373 | |
| 374 | static GLuint translate_tex_src_bit( struct texenv_fragment_program *p, |
| 375 | GLuint bit ) |
| 376 | { |
| 377 | switch (bit) { |
| 378 | case TEXTURE_1D_BIT: return TEXTURE_1D_INDEX; |
| 379 | case TEXTURE_2D_BIT: return TEXTURE_2D_INDEX; |
| 380 | case TEXTURE_RECT_BIT: return TEXTURE_RECT_INDEX; |
| 381 | case TEXTURE_3D_BIT: return TEXTURE_3D_INDEX; |
| 382 | case TEXTURE_CUBE_BIT: return TEXTURE_CUBE_INDEX; |
| 383 | default: program_error(p, "TexSrcBit"); return 0; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | |
| 388 | static struct ureg get_source( struct texenv_fragment_program *p, |
| 389 | GLenum src, GLuint unit ) |
| 390 | { |
| 391 | switch (src) { |
| 392 | case GL_TEXTURE: |
| 393 | if (is_undef(p->src_texture)) { |
| 394 | |
| 395 | GLuint dim = translate_tex_src_bit( p, p->ctx->Texture.Unit[unit]._ReallyEnabled); |
| 396 | struct ureg texcoord = emit_decl(p, |
| 397 | PROGRAM_INPUT, |
| 398 | VERT_ATTRIB_TEX0+unit); |
| 399 | struct ureg tmp = get_temp( p ); |
| 400 | |
| 401 | /* TODO: Use D0_MASK_XY where possible. |
| 402 | */ |
| 403 | p->src_texture = emit_texld( p, FP_OPCODE_TXP, |
| 404 | tmp, WRITEMASK_XYZW, |
| 405 | unit, dim, texcoord ); |
| 406 | } |
| 407 | |
| 408 | return p->src_texture; |
| 409 | |
| 410 | /* Crossbar: */ |
| 411 | case GL_TEXTURE0: |
| 412 | case GL_TEXTURE1: |
| 413 | case GL_TEXTURE2: |
| 414 | case GL_TEXTURE3: |
| 415 | case GL_TEXTURE4: |
| 416 | case GL_TEXTURE5: |
| 417 | case GL_TEXTURE6: |
| 418 | case GL_TEXTURE7: { |
| 419 | return undef; |
| 420 | } |
| 421 | |
| 422 | case GL_CONSTANT: |
| 423 | return emit_const4fv( p, p->ctx->Texture.Unit[unit].EnvColor ); |
| 424 | case GL_PRIMARY_COLOR: |
| 425 | return emit_decl(p, PROGRAM_INPUT, VERT_ATTRIB_COLOR0); |
| 426 | case GL_PREVIOUS: |
| 427 | default: |
| 428 | return emit_decl(p, |
| 429 | p->src_previous.file, |
| 430 | p->src_previous.idx); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | |
| 435 | static struct ureg emit_combine_source( struct texenv_fragment_program *p, |
| 436 | GLuint mask, |
| 437 | GLuint unit, |
| 438 | GLenum source, |
| 439 | GLenum operand ) |
| 440 | { |
| 441 | struct ureg arg, src, one; |
| 442 | |
| 443 | src = get_source(p, source, unit); |
| 444 | |
| 445 | switch (operand) { |
| 446 | case GL_ONE_MINUS_SRC_COLOR: |
| 447 | /* Get unused tmp, |
| 448 | * Emit tmp = 1.0 - arg.xyzw |
| 449 | */ |
| 450 | arg = get_temp( p ); |
| 451 | one = emit_const1f(p, 1); |
| 452 | return emit_arith( p, FP_OPCODE_SUB, arg, mask, 0, one, src, undef); |
| 453 | |
| 454 | case GL_SRC_ALPHA: |
| 455 | if (mask == WRITEMASK_W) |
| 456 | return src; |
| 457 | else |
| 458 | return swizzle1( src, W ); |
| 459 | case GL_ONE_MINUS_SRC_ALPHA: |
| 460 | /* Get unused tmp, |
| 461 | * Emit tmp = 1.0 - arg.wwww |
| 462 | */ |
| 463 | arg = get_temp( p ); |
| 464 | one = emit_const1f(p, 1); |
| 465 | return emit_arith( p, FP_OPCODE_SUB, arg, mask, 0, |
| 466 | one, swizzle1(src, W), undef); |
| 467 | case GL_SRC_COLOR: |
| 468 | default: |
| 469 | return src; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | |
| 474 | |
| 475 | static int nr_args( GLenum mode ) |
| 476 | { |
| 477 | switch (mode) { |
| 478 | case GL_REPLACE: return 1; |
| 479 | case GL_MODULATE: return 2; |
| 480 | case GL_ADD: return 2; |
| 481 | case GL_ADD_SIGNED: return 2; |
| 482 | case GL_INTERPOLATE: return 3; |
| 483 | case GL_SUBTRACT: return 2; |
| 484 | case GL_DOT3_RGB_EXT: return 2; |
| 485 | case GL_DOT3_RGBA_EXT: return 2; |
| 486 | case GL_DOT3_RGB: return 2; |
| 487 | case GL_DOT3_RGBA: return 2; |
| 488 | default: return 0; |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | |
| 493 | static GLboolean args_match( struct gl_texture_unit *texUnit ) |
| 494 | { |
| 495 | int i, nr = nr_args(texUnit->_CurrentCombine->ModeRGB); |
| 496 | |
| 497 | for (i = 0 ; i < nr ; i++) { |
| 498 | if (texUnit->_CurrentCombine->SourceA[i] != texUnit->_CurrentCombine->SourceRGB[i]) |
| 499 | return GL_FALSE; |
| 500 | |
| 501 | switch(texUnit->_CurrentCombine->OperandA[i]) { |
| 502 | case GL_SRC_ALPHA: |
| 503 | switch(texUnit->_CurrentCombine->OperandRGB[i]) { |
| 504 | case GL_SRC_COLOR: |
| 505 | case GL_SRC_ALPHA: |
| 506 | break; |
| 507 | default: |
| 508 | return GL_FALSE; |
| 509 | } |
| 510 | break; |
| 511 | case GL_ONE_MINUS_SRC_ALPHA: |
| 512 | switch(texUnit->_CurrentCombine->OperandRGB[i]) { |
| 513 | case GL_ONE_MINUS_SRC_COLOR: |
| 514 | case GL_ONE_MINUS_SRC_ALPHA: |
| 515 | break; |
| 516 | default: |
| 517 | return GL_FALSE; |
| 518 | } |
| 519 | break; |
| 520 | default: |
| 521 | return GL_FALSE; /* impossible */ |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | return GL_TRUE; |
| 526 | } |
| 527 | |
| 528 | |
| 529 | static struct ureg emit_combine( struct texenv_fragment_program *p, |
| 530 | struct ureg dest, |
| 531 | GLuint mask, |
| 532 | GLuint saturate, |
| 533 | GLuint unit, |
| 534 | GLenum mode, |
| 535 | const GLenum *source, |
| 536 | const GLenum *operand) |
| 537 | { |
| 538 | int nr = nr_args(mode); |
| 539 | struct ureg src[3]; |
| 540 | struct ureg tmp; |
| 541 | int i; |
| 542 | |
| 543 | for (i = 0; i < nr; i++) |
| 544 | src[i] = emit_combine_source( p, mask, unit, source[i], operand[i] ); |
| 545 | |
| 546 | switch (mode) { |
| 547 | case GL_REPLACE: |
| 548 | if (mask == WRITEMASK_XYZW && !saturate) |
| 549 | return src[0]; |
| 550 | else |
| 551 | return emit_arith( p, FP_OPCODE_MOV, dest, mask, saturate, src[0], undef, undef ); |
| 552 | case GL_MODULATE: |
| 553 | return emit_arith( p, FP_OPCODE_MUL, dest, mask, saturate, |
| 554 | src[0], src[1], undef ); |
| 555 | case GL_ADD: |
| 556 | return emit_arith( p, FP_OPCODE_ADD, dest, mask, saturate, |
| 557 | src[0], src[1], undef ); |
| 558 | case GL_ADD_SIGNED: |
| 559 | /* tmp = arg0 + arg1 |
| 560 | * result = tmp + -.5 |
| 561 | */ |
| 562 | tmp = emit_const1f(p, .5); |
| 563 | tmp = swizzle1(tmp,X); |
| 564 | emit_arith( p, FP_OPCODE_ADD, dest, mask, 0, src[0], src[1], undef ); |
| 565 | emit_arith( p, FP_OPCODE_SUB, dest, mask, saturate, dest, tmp, undef ); |
| 566 | return dest; |
| 567 | case GL_INTERPOLATE: |
| 568 | /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered: |
| 569 | */ |
| 570 | return emit_arith( p, FP_OPCODE_LRP, dest, mask, saturate, src[2], src[0], src[1] ); |
| 571 | |
| 572 | case GL_SUBTRACT: |
| 573 | return emit_arith( p, FP_OPCODE_SUB, dest, mask, saturate, src[0], src[1], undef ); |
| 574 | |
| 575 | case GL_DOT3_RGBA: |
| 576 | case GL_DOT3_RGBA_EXT: |
| 577 | case GL_DOT3_RGB_EXT: |
| 578 | case GL_DOT3_RGB: { |
| 579 | struct ureg tmp0 = get_temp( p ); |
| 580 | struct ureg tmp1 = get_temp( p ); |
| 581 | struct ureg neg1 = emit_const1f(p, -1); |
| 582 | struct ureg two = emit_const1f(p, 2); |
| 583 | |
| 584 | /* tmp0 = 2*src0 - 1 |
| 585 | * tmp1 = 2*src1 - 1 |
| 586 | * |
| 587 | * dst = tmp0 dot3 tmp1 |
| 588 | */ |
| 589 | emit_arith( p, FP_OPCODE_MAD, tmp0, WRITEMASK_XYZW, 0, |
| 590 | two, src[0], neg1); |
| 591 | |
| 592 | if (memcmp(&src[0], &src[1], sizeof(struct ureg)) == 0) |
| 593 | tmp1 = tmp0; |
| 594 | else |
| 595 | emit_arith( p, FP_OPCODE_MAD, tmp1, WRITEMASK_XYZW, 0, |
| 596 | two, src[1], neg1); |
| 597 | emit_arith( p, FP_OPCODE_DP3, dest, mask, saturate, tmp0, tmp1, undef); |
| 598 | return dest; |
| 599 | } |
| 600 | |
| 601 | default: |
| 602 | return src[0]; |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | static struct ureg get_dest( struct texenv_fragment_program *p, int unit ) |
| 607 | { |
| 608 | if (p->ctx->_TriangleCaps & DD_SEPARATE_SPECULAR) |
| 609 | return get_temp( p ); |
| 610 | else if (unit != p->last_tex_stage) |
| 611 | return get_temp( p ); |
| 612 | else |
| 613 | return make_ureg(PROGRAM_OUTPUT, VERT_ATTRIB_COLOR0); |
| 614 | } |
| 615 | |
| 616 | |
| 617 | |
| 618 | static struct ureg emit_texenv( struct texenv_fragment_program *p, int unit ) |
| 619 | { |
| 620 | struct gl_texture_unit *texUnit = &p->ctx->Texture.Unit[unit]; |
| 621 | GLuint saturate = (unit < p->last_tex_stage); |
| 622 | GLuint rgb_shift, alpha_shift; |
| 623 | struct ureg out, shift; |
| 624 | struct ureg dest = get_dest(p, unit); |
| 625 | |
| 626 | if (!texUnit->_ReallyEnabled) { |
| 627 | return get_source(p, GL_PREVIOUS, 0); |
| 628 | } |
| 629 | |
| 630 | switch (texUnit->_CurrentCombine->ModeRGB) { |
| 631 | case GL_DOT3_RGB_EXT: |
| 632 | alpha_shift = texUnit->_CurrentCombine->ScaleShiftA; |
| 633 | rgb_shift = 0; |
| 634 | break; |
| 635 | |
| 636 | case GL_DOT3_RGBA_EXT: |
| 637 | alpha_shift = 0; |
| 638 | rgb_shift = 0; |
| 639 | break; |
| 640 | |
| 641 | default: |
| 642 | rgb_shift = texUnit->_CurrentCombine->ScaleShiftRGB; |
| 643 | alpha_shift = texUnit->_CurrentCombine->ScaleShiftA; |
| 644 | break; |
| 645 | } |
| 646 | |
| 647 | |
| 648 | /* Emit the RGB and A combine ops |
| 649 | */ |
| 650 | if (texUnit->_CurrentCombine->ModeRGB == texUnit->_CurrentCombine->ModeA && |
| 651 | args_match( texUnit )) { |
| 652 | out = emit_combine( p, dest, WRITEMASK_XYZW, saturate, |
| 653 | unit, |
| 654 | texUnit->_CurrentCombine->ModeRGB, |
| 655 | texUnit->_CurrentCombine->SourceRGB, |
| 656 | texUnit->_CurrentCombine->OperandRGB ); |
| 657 | } |
| 658 | else if (texUnit->_CurrentCombine->ModeRGB == GL_DOT3_RGBA_EXT || |
| 659 | texUnit->_CurrentCombine->ModeRGB == GL_DOT3_RGBA) { |
| 660 | |
| 661 | out = emit_combine( p, dest, WRITEMASK_XYZW, saturate, |
| 662 | unit, |
| 663 | texUnit->_CurrentCombine->ModeRGB, |
| 664 | texUnit->_CurrentCombine->SourceRGB, |
| 665 | texUnit->_CurrentCombine->OperandRGB ); |
| 666 | } |
| 667 | else { |
| 668 | /* Need to do something to stop from re-emitting identical |
| 669 | * argument calculations here: |
| 670 | */ |
| 671 | out = emit_combine( p, dest, WRITEMASK_XYZ, saturate, |
| 672 | unit, |
| 673 | texUnit->_CurrentCombine->ModeRGB, |
| 674 | texUnit->_CurrentCombine->SourceRGB, |
| 675 | texUnit->_CurrentCombine->OperandRGB ); |
| 676 | out = emit_combine( p, dest, WRITEMASK_W, saturate, |
| 677 | unit, |
| 678 | texUnit->_CurrentCombine->ModeA, |
| 679 | texUnit->_CurrentCombine->SourceA, |
| 680 | texUnit->_CurrentCombine->OperandA ); |
| 681 | } |
| 682 | |
| 683 | /* Deal with the final shift: |
| 684 | */ |
| 685 | if (alpha_shift || rgb_shift) { |
| 686 | if (rgb_shift == alpha_shift) { |
| 687 | shift = emit_const1f(p, 1<<rgb_shift); |
| 688 | shift = swizzle1(shift,X); |
| 689 | } |
| 690 | else { |
| 691 | shift = emit_const2f(p, 1<<rgb_shift, 1<<alpha_shift); |
| 692 | shift = swizzle(shift,X,X,X,Y); |
| 693 | } |
| 694 | return emit_arith( p, FP_OPCODE_MUL, dest, WRITEMASK_XYZW, |
| 695 | saturate, out, shift, undef ); |
| 696 | } |
| 697 | else |
| 698 | return out; |
| 699 | } |
| 700 | |
| 701 | void _mesa_UpdateTexEnvProgram( GLcontext *ctx ) |
| 702 | { |
| 703 | struct texenv_fragment_program p; |
| 704 | GLuint unit; |
| 705 | struct ureg cf, out; |
| 706 | |
| 707 | p.ctx = ctx; |
| 708 | p.prog = &ctx->_TexEnvProgram; |
| 709 | |
| 710 | if (p.prog->Instructions == NULL) { |
| 711 | p.prog->Instructions = MALLOC(sizeof(struct fp_instruction) * 100); |
| 712 | } |
| 713 | |
| 714 | p.prog->Base.NumInstructions = 0; |
| 715 | p.prog->NumTexIndirections = 1; /* correct? */ |
| 716 | p.prog->NumTexInstructions = 0; |
| 717 | p.prog->NumAluInstructions = 0; |
| 718 | |
| 719 | memset( p.constant_flags, 0, sizeof(p.constant_flags) ); |
| 720 | |
| 721 | p.src_texture = undef; |
| 722 | p.src_previous = make_ureg(PROGRAM_INPUT, VERT_ATTRIB_COLOR0); |
| 723 | p.last_tex_stage = 0; |
| 724 | |
| 725 | if (ctx->Texture._EnabledUnits) { |
| 726 | for (unit = 0 ; unit < ctx->Const.MaxTextureUnits ; unit++) |
| 727 | if (ctx->Texture.Unit[unit]._ReallyEnabled) { |
| 728 | p.last_tex_stage = unit; |
| 729 | } |
| 730 | |
| 731 | for (unit = 0 ; unit < ctx->Const.MaxTextureUnits; unit++) |
| 732 | if (ctx->Texture.Unit[unit]._ReallyEnabled) { |
| 733 | p.src_previous = emit_texenv( &p, unit ); |
| 734 | p.src_texture = undef; |
| 735 | p.temp_flag = 0xffff000; |
| 736 | p.temp_flag |= 1 << p.src_previous.idx; |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | cf = get_source( &p, GL_PREVIOUS, 0 ); |
| 741 | out = make_ureg( PROGRAM_OUTPUT, VERT_ATTRIB_COLOR0 ); |
| 742 | |
| 743 | if (ctx->_TriangleCaps & DD_SEPARATE_SPECULAR) { |
| 744 | /* Emit specular add. |
| 745 | */ |
| 746 | struct ureg s = emit_decl(&p, PROGRAM_INPUT, VERT_ATTRIB_COLOR1); |
| 747 | emit_arith( &p, FP_OPCODE_ADD, out, WRITEMASK_XYZ, 0, cf, s, undef ); |
| 748 | } |
| 749 | else if (memcmp(&cf, &out, sizeof(cf)) != 0) { |
| 750 | /* Will wind up in here if no texture enabled or a couple of |
| 751 | * other scenarios (GL_REPLACE for instance). |
| 752 | */ |
| 753 | emit_arith( &p, FP_OPCODE_MOV, out, WRITEMASK_XYZW, 0, cf, undef, undef ); |
| 754 | } |
| 755 | |
| 756 | if (p.prog->NumTexIndirections > ctx->Const.MaxFragmentProgramTexIndirections) |
| 757 | program_error(&p, "Exceeded max nr indirect texture lookups"); |
| 758 | |
| 759 | if (p.prog->NumTexInstructions > ctx->Const.MaxFragmentProgramTexInstructions) |
| 760 | program_error(&p, "Exceeded max TEX instructions"); |
| 761 | |
| 762 | if (p.prog->NumAluInstructions > ctx->Const.MaxFragmentProgramAluInstructions) |
| 763 | program_error(&p, "Exceeded max ALU instructions"); |
| 764 | |
| 765 | #if DISASSEM |
| 766 | _mesa_debug_fp_inst(p.prog->NumTexInstructions + p.prog->NumAluInstructions, |
| 767 | p.prog->Instructions); |
| 768 | _mesa_printf("\n"); |
| 769 | #endif |
| 770 | } |
| 771 | |
| 772 | |