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