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