blob: 7f921b75502b2176d6fc8f48e5f14cc5bf9cf557 [file] [log] [blame]
Keith Whitwell15e75e02005-04-29 15:11:39 +00001/**************************************************************************
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
Keith Whitwell15e75e02005-04-29 15:11:39 +000028#include "glheader.h"
29#include "macros.h"
30#include "enums.h"
31#include "texenvprogram.h"
32
33#include "shader/program.h"
34#include "shader/nvfragprog.h"
35#include "shader/arbfragparse.h"
36
37
Keith Whitwell269e3892005-05-12 10:28:43 +000038#define DISASSEM (MESA_VERBOSE & VERBOSE_DISASSEM)
Keith Whitwell15e75e02005-04-29 15:11:39 +000039
40/* Use uregs to represent registers internally, translate to Mesa's
41 * expected formats on emit.
42 *
43 * NOTE: These are passed by value extensively in this file rather
44 * than as usual by pointer reference. If this disturbs you, try
45 * remembering they are just 32bits in size.
46 *
47 * GCC is smart enough to deal with these dword-sized structures in
48 * much the same way as if I had defined them as dwords and was using
49 * macros to access and set the fields. This is much nicer and easier
50 * to evolve.
51 */
52struct ureg {
53 GLuint file:4;
54 GLuint idx:8;
55 GLuint negatebase:1;
56 GLuint abs:1;
57 GLuint negateabs:1;
58 GLuint swz:12;
59 GLuint pad:5;
60};
61
62const static struct ureg undef = {
63 ~0,
64 ~0,
65 0,
66 0,
67 0,
68 0,
69 0
70};
71
72#define X 0
73#define Y 1
74#define Z 2
75#define W 3
76
Keith Whitwell15e75e02005-04-29 15:11:39 +000077/* State used to build the fragment program:
78 */
79struct texenv_fragment_program {
Keith Whitwell47b29f52005-05-04 11:44:44 +000080 struct fragment_program *program;
Keith Whitwell15e75e02005-04-29 15:11:39 +000081 GLcontext *ctx;
82
Keith Whitwellcf4f3c52005-05-16 12:15:01 +000083 GLuint alu_temps; /* Track texture indirections, see spec. */
84 GLuint temps_output; /* Track texture indirections, see spec. */
Keith Whitwellecb6bfc2005-05-10 08:58:44 +000085
Keith Whitwell93cd9232005-05-11 08:30:23 +000086 GLuint temp_in_use; /* Tracks temporary regs which are in
Keith Whitwell15e75e02005-04-29 15:11:39 +000087 * use.
88 */
89
90
Keith Whitwell15e75e02005-04-29 15:11:39 +000091 GLboolean error;
92
93 struct ureg src_texture; /* Reg containing sampled texture color,
94 * else undef.
95 */
96
97 struct ureg src_previous; /* Reg containing color from previous
98 * stage. May need to be decl'd.
99 */
100
101 GLuint last_tex_stage; /* Number of last enabled texture unit */
102};
103
104
105
106static struct ureg make_ureg(GLuint file, GLuint idx)
107{
108 struct ureg reg;
109 reg.file = file;
110 reg.idx = idx;
111 reg.negatebase = 0;
112 reg.abs = 0;
113 reg.negateabs = 0;
114 reg.swz = SWIZZLE_NOOP;
115 reg.pad = 0;
116 return reg;
117}
118
119static struct ureg swizzle( struct ureg reg, int x, int y, int z, int w )
120{
121 reg.swz = MAKE_SWIZZLE4(GET_SWZ(reg.swz, x),
122 GET_SWZ(reg.swz, y),
123 GET_SWZ(reg.swz, z),
124 GET_SWZ(reg.swz, w));
125
126 return reg;
127}
128
129static struct ureg swizzle1( struct ureg reg, int x )
130{
131 return swizzle(reg, x, x, x, x);
132}
133
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000134static struct ureg negate( struct ureg reg )
135{
136 reg.negatebase ^= 1;
137 return reg;
138}
139
Keith Whitwell15e75e02005-04-29 15:11:39 +0000140static GLboolean is_undef( struct ureg reg )
141{
142 return reg.file == 0xf;
143}
144
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000145
Keith Whitwell15e75e02005-04-29 15:11:39 +0000146static struct ureg get_temp( struct texenv_fragment_program *p )
147{
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000148 int bit;
149
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000150 /* First try and reuse temps which have been used already:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000151 */
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000152 bit = ffs( ~p->temp_in_use & p->alu_temps );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000153
154 /* Then any unused temporary:
155 */
156 if (!bit)
Keith Whitwell93cd9232005-05-11 08:30:23 +0000157 bit = ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000158
Keith Whitwell15e75e02005-04-29 15:11:39 +0000159 if (!bit) {
160 fprintf(stderr, "%s: out of temporaries\n", __FILE__);
161 exit(1);
162 }
163
Keith Whitwell93cd9232005-05-11 08:30:23 +0000164 p->temp_in_use |= 1<<(bit-1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000165 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
166}
167
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000168static struct ureg get_tex_temp( struct texenv_fragment_program *p )
169{
170 int bit;
171
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000172 /* First try to find availble temp not previously used (to avoid
173 * starting a new texture indirection). According to the spec, the
174 * ~p->temps_output isn't necessary, but will keep it there for
175 * now:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000176 */
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000177 bit = ffs( ~p->temp_in_use & ~p->alu_temps & ~p->temps_output );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000178
179 /* Then any unused temporary:
180 */
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000181 if (!bit)
Keith Whitwell93cd9232005-05-11 08:30:23 +0000182 bit = ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000183
184 if (!bit) {
185 fprintf(stderr, "%s: out of temporaries\n", __FILE__);
186 exit(1);
187 }
188
Keith Whitwell93cd9232005-05-11 08:30:23 +0000189 p->temp_in_use |= 1<<(bit-1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000190 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
191}
192
Keith Whitwell15e75e02005-04-29 15:11:39 +0000193
194static void release_temps( struct texenv_fragment_program *p )
195{
Keith Whitwell93cd9232005-05-11 08:30:23 +0000196 GLuint max_temp = p->ctx->Const.MaxFragmentProgramTemps;
197
198 if (max_temp >= sizeof(int) * 8)
199 p->temp_in_use = 0;
200 else
201 p->temp_in_use = ~((1<<max_temp)-1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000202}
203
204
Keith Whitwell47b29f52005-05-04 11:44:44 +0000205static struct ureg register_param6( struct texenv_fragment_program *p,
206 GLint s0,
207 GLint s1,
208 GLint s2,
209 GLint s3,
210 GLint s4,
211 GLint s5)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000212{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000213 GLint tokens[6];
214 GLuint idx;
215 tokens[0] = s0;
216 tokens[1] = s1;
217 tokens[2] = s2;
218 tokens[3] = s3;
219 tokens[4] = s4;
220 tokens[5] = s5;
221 idx = _mesa_add_state_reference( p->program->Parameters, tokens );
222 return make_ureg(PROGRAM_STATE_VAR, idx);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000223}
224
Keith Whitwell47b29f52005-05-04 11:44:44 +0000225
226#define register_param1(p,s0) register_param6(p,s0,0,0,0,0,0)
227#define register_param2(p,s0,s1) register_param6(p,s0,s1,0,0,0,0)
228#define register_param3(p,s0,s1,s2) register_param6(p,s0,s1,s2,0,0,0)
229#define register_param4(p,s0,s1,s2,s3) register_param6(p,s0,s1,s2,s3,0,0)
230
231
232static struct ureg register_input( struct texenv_fragment_program *p, GLuint input )
233{
234 p->program->InputsRead |= (1<<input);
235 return make_ureg(PROGRAM_INPUT, input);
236}
237
238
Keith Whitwell15e75e02005-04-29 15:11:39 +0000239static void emit_arg( struct fp_src_register *reg,
240 struct ureg ureg )
241{
242 reg->File = ureg.file;
243 reg->Index = ureg.idx;
244 reg->Swizzle = ureg.swz;
245 reg->NegateBase = ureg.negatebase;
246 reg->Abs = ureg.abs;
247 reg->NegateAbs = ureg.negateabs;
248}
249
250static void emit_dst( struct fp_dst_register *dst,
251 struct ureg ureg, GLuint mask )
252{
253 dst->File = ureg.file;
254 dst->Index = ureg.idx;
255 dst->WriteMask = mask;
256 dst->CondMask = 0;
257 dst->CondSwizzle = 0;
258}
259
260static struct fp_instruction *
261emit_op(struct texenv_fragment_program *p,
262 GLuint op,
263 struct ureg dest,
264 GLuint mask,
265 GLuint saturate,
266 struct ureg src0,
267 struct ureg src1,
268 struct ureg src2 )
269{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000270 GLuint nr = p->program->Base.NumInstructions++;
271 struct fp_instruction *inst = &p->program->Instructions[nr];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000272
273 memset(inst, 0, sizeof(*inst));
274 inst->Opcode = op;
275
Keith Whitwell47b29f52005-05-04 11:44:44 +0000276 emit_arg( &inst->SrcReg[0], src0 );
277 emit_arg( &inst->SrcReg[1], src1 );
278 emit_arg( &inst->SrcReg[2], src2 );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000279
280 inst->Saturate = saturate;
281
282 emit_dst( &inst->DstReg, dest, mask );
283
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000284 /* Accounting for indirection tracking:
285 */
286 if (dest.file == PROGRAM_TEMPORARY)
287 p->temps_output |= 1 << dest.idx;
288
Keith Whitwell15e75e02005-04-29 15:11:39 +0000289 return inst;
290}
291
292
293static struct ureg emit_arith( struct texenv_fragment_program *p,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000294 GLuint op,
295 struct ureg dest,
296 GLuint mask,
297 GLuint saturate,
298 struct ureg src0,
299 struct ureg src1,
300 struct ureg src2 )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000301{
302 emit_op(p, op, dest, mask, saturate, src0, src1, src2);
303
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000304 /* Accounting for indirection tracking:
305 */
306 if (src0.file == PROGRAM_TEMPORARY)
307 p->alu_temps |= 1 << src0.idx;
308
309 if (!is_undef(src1) && src1.file == PROGRAM_TEMPORARY)
310 p->alu_temps |= 1 << src1.idx;
311
312 if (!is_undef(src2) && src2.file == PROGRAM_TEMPORARY)
313 p->alu_temps |= 1 << src2.idx;
314
315 if (dest.file == PROGRAM_TEMPORARY)
316 p->alu_temps |= 1 << dest.idx;
317
Keith Whitwell47b29f52005-05-04 11:44:44 +0000318 p->program->NumAluInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000319 return dest;
320}
321
322static struct ureg emit_texld( struct texenv_fragment_program *p,
323 GLuint op,
324 struct ureg dest,
325 GLuint destmask,
326 GLuint tex_unit,
327 GLuint tex_idx,
328 struct ureg coord )
329{
330 struct fp_instruction *inst = emit_op( p, op,
331 dest, destmask,
332 0, /* don't saturate? */
333 coord, /* arg 0? */
334 undef,
335 undef);
336
337 inst->TexSrcIdx = tex_idx;
338 inst->TexSrcUnit = tex_unit;
339
Keith Whitwell47b29f52005-05-04 11:44:44 +0000340 p->program->NumTexInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000341
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000342 /* Is this a texture indirection?
343 */
344 if ((coord.file == PROGRAM_TEMPORARY &&
345 (p->temps_output & (1<<coord.idx))) ||
346 (dest.file == PROGRAM_TEMPORARY &&
347 (p->alu_temps & (1<<dest.idx)))) {
Keith Whitwell47b29f52005-05-04 11:44:44 +0000348 p->program->NumTexIndirections++;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000349 p->temps_output = 0;
350 p->alu_temps = 0;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000351 }
352
353 return dest;
354}
355
356
Keith Whitwell47b29f52005-05-04 11:44:44 +0000357static struct ureg register_const4f( struct texenv_fragment_program *p,
358 GLfloat s0,
359 GLfloat s1,
360 GLfloat s2,
361 GLfloat s3)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000362{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000363 GLfloat values[4];
364 GLuint idx;
365 values[0] = s0;
366 values[1] = s1;
367 values[2] = s2;
368 values[3] = s3;
369 idx = _mesa_add_unnamed_constant( p->program->Parameters, values );
370 return make_ureg(PROGRAM_STATE_VAR, idx);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000371}
372
Keith Whitwelle4902422005-05-11 15:16:35 +0000373#define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000374#define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
375#define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
376#define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000377
378
379
Keith Whitwell15e75e02005-04-29 15:11:39 +0000380
381
382
383
384
385static void program_error( struct texenv_fragment_program *p, const char *msg )
386{
387 fprintf(stderr, "%s\n", msg);
388 p->error = 1;
389}
390
391
392static GLuint translate_tex_src_bit( struct texenv_fragment_program *p,
393 GLuint bit )
394{
395 switch (bit) {
396 case TEXTURE_1D_BIT: return TEXTURE_1D_INDEX;
397 case TEXTURE_2D_BIT: return TEXTURE_2D_INDEX;
398 case TEXTURE_RECT_BIT: return TEXTURE_RECT_INDEX;
399 case TEXTURE_3D_BIT: return TEXTURE_3D_INDEX;
400 case TEXTURE_CUBE_BIT: return TEXTURE_CUBE_INDEX;
401 default: program_error(p, "TexSrcBit"); return 0;
402 }
403}
404
405
406static struct ureg get_source( struct texenv_fragment_program *p,
407 GLenum src, GLuint unit )
408{
409 switch (src) {
410 case GL_TEXTURE:
411 if (is_undef(p->src_texture)) {
412
413 GLuint dim = translate_tex_src_bit( p, p->ctx->Texture.Unit[unit]._ReallyEnabled);
Keith Whitwell47b29f52005-05-04 11:44:44 +0000414 struct ureg texcoord = register_input(p, FRAG_ATTRIB_TEX0+unit);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000415 struct ureg tmp = get_tex_temp( p );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000416
417 /* TODO: Use D0_MASK_XY where possible.
418 */
419 p->src_texture = emit_texld( p, FP_OPCODE_TXP,
420 tmp, WRITEMASK_XYZW,
421 unit, dim, texcoord );
422 }
423
424 return p->src_texture;
425
426 /* Crossbar: */
427 case GL_TEXTURE0:
428 case GL_TEXTURE1:
429 case GL_TEXTURE2:
430 case GL_TEXTURE3:
431 case GL_TEXTURE4:
432 case GL_TEXTURE5:
433 case GL_TEXTURE6:
434 case GL_TEXTURE7: {
435 return undef;
436 }
437
438 case GL_CONSTANT:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000439 return register_param2(p, STATE_TEXENV_COLOR, unit);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000440 case GL_PRIMARY_COLOR:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000441 return register_input(p, FRAG_ATTRIB_COL0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000442 case GL_PREVIOUS:
443 default:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000444 if (is_undef(p->src_previous))
445 return register_input(p, FRAG_ATTRIB_COL0);
446 else
447 return p->src_previous;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000448 }
449}
450
451
452static struct ureg emit_combine_source( struct texenv_fragment_program *p,
453 GLuint mask,
454 GLuint unit,
455 GLenum source,
456 GLenum operand )
457{
458 struct ureg arg, src, one;
459
460 src = get_source(p, source, unit);
461
462 switch (operand) {
463 case GL_ONE_MINUS_SRC_COLOR:
464 /* Get unused tmp,
465 * Emit tmp = 1.0 - arg.xyzw
466 */
467 arg = get_temp( p );
Keith Whitwelle4902422005-05-11 15:16:35 +0000468 one = register_scalar_const(p, 1.0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000469 return emit_arith( p, FP_OPCODE_SUB, arg, mask, 0, one, src, undef);
470
471 case GL_SRC_ALPHA:
472 if (mask == WRITEMASK_W)
473 return src;
474 else
475 return swizzle1( src, W );
476 case GL_ONE_MINUS_SRC_ALPHA:
477 /* Get unused tmp,
478 * Emit tmp = 1.0 - arg.wwww
479 */
480 arg = get_temp( p );
Keith Whitwelle4902422005-05-11 15:16:35 +0000481 one = register_scalar_const(p, 1.0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000482 return emit_arith( p, FP_OPCODE_SUB, arg, mask, 0,
483 one, swizzle1(src, W), undef);
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000484 case GL_ZERO:
485 return register_scalar_const(p, 0.0);
486 case GL_ONE:
487 return register_scalar_const(p, 1.0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000488 case GL_SRC_COLOR:
489 default:
490 return src;
491 }
492}
493
494
495
496static int nr_args( GLenum mode )
497{
498 switch (mode) {
499 case GL_REPLACE: return 1;
500 case GL_MODULATE: return 2;
501 case GL_ADD: return 2;
502 case GL_ADD_SIGNED: return 2;
503 case GL_INTERPOLATE: return 3;
504 case GL_SUBTRACT: return 2;
505 case GL_DOT3_RGB_EXT: return 2;
506 case GL_DOT3_RGBA_EXT: return 2;
507 case GL_DOT3_RGB: return 2;
508 case GL_DOT3_RGBA: return 2;
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000509 case GL_MODULATE_ADD_ATI: return 3;
510 case GL_MODULATE_SUBTRACT_ATI: return 3;
511 case GL_MODULATE_SIGNED_ADD_ATI: return 3;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000512 default: return 0;
513 }
514}
515
516
517static GLboolean args_match( struct gl_texture_unit *texUnit )
518{
519 int i, nr = nr_args(texUnit->_CurrentCombine->ModeRGB);
520
521 for (i = 0 ; i < nr ; i++) {
522 if (texUnit->_CurrentCombine->SourceA[i] != texUnit->_CurrentCombine->SourceRGB[i])
523 return GL_FALSE;
524
525 switch(texUnit->_CurrentCombine->OperandA[i]) {
526 case GL_SRC_ALPHA:
527 switch(texUnit->_CurrentCombine->OperandRGB[i]) {
528 case GL_SRC_COLOR:
529 case GL_SRC_ALPHA:
530 break;
531 default:
532 return GL_FALSE;
533 }
534 break;
535 case GL_ONE_MINUS_SRC_ALPHA:
536 switch(texUnit->_CurrentCombine->OperandRGB[i]) {
537 case GL_ONE_MINUS_SRC_COLOR:
538 case GL_ONE_MINUS_SRC_ALPHA:
539 break;
540 default:
541 return GL_FALSE;
542 }
543 break;
544 default:
545 return GL_FALSE; /* impossible */
546 }
547 }
548
549 return GL_TRUE;
550}
551
552
553static struct ureg emit_combine( struct texenv_fragment_program *p,
554 struct ureg dest,
555 GLuint mask,
556 GLuint saturate,
557 GLuint unit,
558 GLenum mode,
559 const GLenum *source,
560 const GLenum *operand)
561{
562 int nr = nr_args(mode);
563 struct ureg src[3];
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000564 struct ureg tmp, half;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000565 int i;
566
567 for (i = 0; i < nr; i++)
568 src[i] = emit_combine_source( p, mask, unit, source[i], operand[i] );
569
570 switch (mode) {
571 case GL_REPLACE:
572 if (mask == WRITEMASK_XYZW && !saturate)
573 return src[0];
574 else
575 return emit_arith( p, FP_OPCODE_MOV, dest, mask, saturate, src[0], undef, undef );
576 case GL_MODULATE:
577 return emit_arith( p, FP_OPCODE_MUL, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000578 src[0], src[1], undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000579 case GL_ADD:
580 return emit_arith( p, FP_OPCODE_ADD, dest, mask, saturate,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000581 src[0], src[1], undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000582 case GL_ADD_SIGNED:
583 /* tmp = arg0 + arg1
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000584 * result = tmp - .5
Keith Whitwell15e75e02005-04-29 15:11:39 +0000585 */
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000586 half = register_scalar_const(p, .5);
587 emit_arith( p, FP_OPCODE_ADD, tmp, mask, 0, src[0], src[1], undef );
588 emit_arith( p, FP_OPCODE_SUB, dest, mask, saturate, tmp, half, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000589 return dest;
590 case GL_INTERPOLATE:
591 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
592 */
593 return emit_arith( p, FP_OPCODE_LRP, dest, mask, saturate, src[2], src[0], src[1] );
594
595 case GL_SUBTRACT:
596 return emit_arith( p, FP_OPCODE_SUB, dest, mask, saturate, src[0], src[1], undef );
597
598 case GL_DOT3_RGBA:
599 case GL_DOT3_RGBA_EXT:
600 case GL_DOT3_RGB_EXT:
601 case GL_DOT3_RGB: {
602 struct ureg tmp0 = get_temp( p );
603 struct ureg tmp1 = get_temp( p );
Keith Whitwelle4902422005-05-11 15:16:35 +0000604 struct ureg neg1 = register_scalar_const(p, -1);
605 struct ureg two = register_scalar_const(p, 2);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000606
607 /* tmp0 = 2*src0 - 1
608 * tmp1 = 2*src1 - 1
609 *
610 * dst = tmp0 dot3 tmp1
611 */
612 emit_arith( p, FP_OPCODE_MAD, tmp0, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000613 two, src[0], neg1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000614
615 if (memcmp(&src[0], &src[1], sizeof(struct ureg)) == 0)
616 tmp1 = tmp0;
617 else
618 emit_arith( p, FP_OPCODE_MAD, tmp1, WRITEMASK_XYZW, 0,
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000619 two, src[1], neg1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000620 emit_arith( p, FP_OPCODE_DP3, dest, mask, saturate, tmp0, tmp1, undef);
621 return dest;
622 }
Keith Whitwell6fe176a2005-05-23 08:08:43 +0000623 case GL_MODULATE_ADD_ATI:
624 /* Arg0 * Arg2 + Arg1 */
625 return emit_arith( p, FP_OPCODE_MAD, dest, mask, saturate,
626 src[0], src[2], src[1] );
627 case GL_MODULATE_SIGNED_ADD_ATI: {
628 /* Arg0 * Arg2 + Arg1 - 0.5 */
629 struct ureg tmp0 = get_temp(p);
630 half = register_scalar_const(p, .5);
631 emit_arith( p, FP_OPCODE_MAD, tmp0, mask, 0, src[0], src[2], src[1] );
632 emit_arith( p, FP_OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
633 return dest;
634 }
635 case GL_MODULATE_SUBTRACT_ATI:
636 /* Arg0 * Arg2 - Arg1 */
637 emit_arith( p, FP_OPCODE_MAD, dest, mask, 0, src[0], src[2], negate(src[1]) );
638 return dest;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000639 default:
640 return src[0];
641 }
642}
643
Keith Whitwell15e75e02005-04-29 15:11:39 +0000644
645
646static struct ureg emit_texenv( struct texenv_fragment_program *p, int unit )
647{
648 struct gl_texture_unit *texUnit = &p->ctx->Texture.Unit[unit];
649 GLuint saturate = (unit < p->last_tex_stage);
650 GLuint rgb_shift, alpha_shift;
651 struct ureg out, shift;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000652 struct ureg dest;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000653
654 if (!texUnit->_ReallyEnabled) {
655 return get_source(p, GL_PREVIOUS, 0);
656 }
657
658 switch (texUnit->_CurrentCombine->ModeRGB) {
659 case GL_DOT3_RGB_EXT:
660 alpha_shift = texUnit->_CurrentCombine->ScaleShiftA;
661 rgb_shift = 0;
662 break;
663
664 case GL_DOT3_RGBA_EXT:
665 alpha_shift = 0;
666 rgb_shift = 0;
667 break;
668
669 default:
670 rgb_shift = texUnit->_CurrentCombine->ScaleShiftRGB;
671 alpha_shift = texUnit->_CurrentCombine->ScaleShiftA;
672 break;
673 }
674
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000675 /* If this is the very last calculation, emit direct to output reg:
676 */
677 if ((p->ctx->_TriangleCaps & DD_SEPARATE_SPECULAR) ||
678 unit != p->last_tex_stage ||
679 alpha_shift ||
680 rgb_shift)
681 dest = get_temp( p );
682 else
683 dest = make_ureg(PROGRAM_OUTPUT, FRAG_OUTPUT_COLR);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000684
685 /* Emit the RGB and A combine ops
686 */
687 if (texUnit->_CurrentCombine->ModeRGB == texUnit->_CurrentCombine->ModeA &&
688 args_match( texUnit )) {
689 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
690 unit,
691 texUnit->_CurrentCombine->ModeRGB,
692 texUnit->_CurrentCombine->SourceRGB,
693 texUnit->_CurrentCombine->OperandRGB );
694 }
695 else if (texUnit->_CurrentCombine->ModeRGB == GL_DOT3_RGBA_EXT ||
696 texUnit->_CurrentCombine->ModeRGB == GL_DOT3_RGBA) {
697
698 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
699 unit,
700 texUnit->_CurrentCombine->ModeRGB,
701 texUnit->_CurrentCombine->SourceRGB,
702 texUnit->_CurrentCombine->OperandRGB );
703 }
704 else {
705 /* Need to do something to stop from re-emitting identical
706 * argument calculations here:
707 */
708 out = emit_combine( p, dest, WRITEMASK_XYZ, saturate,
709 unit,
710 texUnit->_CurrentCombine->ModeRGB,
711 texUnit->_CurrentCombine->SourceRGB,
712 texUnit->_CurrentCombine->OperandRGB );
713 out = emit_combine( p, dest, WRITEMASK_W, saturate,
714 unit,
715 texUnit->_CurrentCombine->ModeA,
716 texUnit->_CurrentCombine->SourceA,
717 texUnit->_CurrentCombine->OperandA );
718 }
719
720 /* Deal with the final shift:
721 */
722 if (alpha_shift || rgb_shift) {
723 if (rgb_shift == alpha_shift) {
Keith Whitwelle4902422005-05-11 15:16:35 +0000724 shift = register_scalar_const(p, 1<<rgb_shift);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000725 }
726 else {
Keith Whitwell47b29f52005-05-04 11:44:44 +0000727 shift = register_const2f(p, 1<<rgb_shift, 1<<alpha_shift);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000728 shift = swizzle(shift,X,X,X,Y);
729 }
730 return emit_arith( p, FP_OPCODE_MUL, dest, WRITEMASK_XYZW,
731 saturate, out, shift, undef );
732 }
733 else
734 return out;
735}
736
737void _mesa_UpdateTexEnvProgram( GLcontext *ctx )
738{
739 struct texenv_fragment_program p;
740 GLuint unit;
741 struct ureg cf, out;
Keith Whitwelle4902422005-05-11 15:16:35 +0000742 GLuint db_NumInstructions = 0;
743 struct fp_instruction *db_Instructions = NULL;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000744
Keith Whitwell47b29f52005-05-04 11:44:44 +0000745 if (ctx->FragmentProgram._Enabled)
746 return;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000747
Keith Whitwell8b88f622005-05-10 11:39:50 +0000748 if (!ctx->_TexEnvProgram)
749 ctx->FragmentProgram._Current = ctx->_TexEnvProgram =
750 (struct fragment_program *)
751 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
Keith Whitwell276330b2005-05-09 17:58:13 +0000752
Keith Whitwelle4902422005-05-11 15:16:35 +0000753 _mesa_memset(&p, 0, sizeof(p));
Keith Whitwell47b29f52005-05-04 11:44:44 +0000754 p.ctx = ctx;
Keith Whitwell276330b2005-05-09 17:58:13 +0000755 p.program = ctx->_TexEnvProgram;
Keith Whitwell47b29f52005-05-04 11:44:44 +0000756
Keith Whitwelle4902422005-05-11 15:16:35 +0000757 if (ctx->Driver.ProgramStringNotify || DISASSEM) {
758 db_Instructions = p.program->Instructions;
759 db_NumInstructions = p.program->Base.NumInstructions;
760 p.program->Instructions = NULL;
761 }
Keith Whitwell15e75e02005-04-29 15:11:39 +0000762
Keith Whitwelle4902422005-05-11 15:16:35 +0000763 if (!p.program->Instructions)
764 p.program->Instructions = MALLOC(sizeof(struct fp_instruction) * 100);
765
Keith Whitwell47b29f52005-05-04 11:44:44 +0000766 p.program->Base.NumInstructions = 0;
Keith Whitwell276330b2005-05-09 17:58:13 +0000767 p.program->Base.Target = GL_FRAGMENT_PROGRAM_ARB;
Keith Whitwell47b29f52005-05-04 11:44:44 +0000768 p.program->NumTexIndirections = 1; /* correct? */
769 p.program->NumTexInstructions = 0;
770 p.program->NumAluInstructions = 0;
Keith Whitwell9ca88152005-05-10 09:56:02 +0000771 p.program->Base.String = 0;
772 p.program->Base.NumInstructions =
773 p.program->Base.NumTemporaries =
774 p.program->Base.NumParameters =
775 p.program->Base.NumAttributes = p.program->Base.NumAddressRegs = 0;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000776
Keith Whitwell9ca88152005-05-10 09:56:02 +0000777 if (p.program->Parameters)
Keith Whitwelldbeea252005-05-10 13:57:50 +0000778 _mesa_free_parameters(p.program->Parameters);
779 else
780 p.program->Parameters = _mesa_new_parameter_list();
781
Keith Whitwell9ca88152005-05-10 09:56:02 +0000782 p.program->InputsRead = 0;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000783 p.program->OutputsWritten = 1 << FRAG_OUTPUT_COLR;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000784
785 p.src_texture = undef;
Keith Whitwell47b29f52005-05-04 11:44:44 +0000786 p.src_previous = undef;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000787 p.last_tex_stage = 0;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000788 release_temps(&p);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000789
790 if (ctx->Texture._EnabledUnits) {
791 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits ; unit++)
792 if (ctx->Texture.Unit[unit]._ReallyEnabled) {
793 p.last_tex_stage = unit;
794 }
795
796 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits; unit++)
797 if (ctx->Texture.Unit[unit]._ReallyEnabled) {
798 p.src_previous = emit_texenv( &p, unit );
799 p.src_texture = undef;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000800 release_temps(&p); /* release all temps */
801 if (p.src_previous.file == PROGRAM_TEMPORARY)
802 p.temp_in_use |= 1 << p.src_previous.idx; /* except for this one */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000803 }
804 }
805
806 cf = get_source( &p, GL_PREVIOUS, 0 );
Keith Whitwell47b29f52005-05-04 11:44:44 +0000807 out = make_ureg( PROGRAM_OUTPUT, FRAG_OUTPUT_COLR );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000808
809 if (ctx->_TriangleCaps & DD_SEPARATE_SPECULAR) {
810 /* Emit specular add.
811 */
Keith Whitwell47b29f52005-05-04 11:44:44 +0000812 struct ureg s = register_input(&p, FRAG_ATTRIB_COL1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000813 emit_arith( &p, FP_OPCODE_ADD, out, WRITEMASK_XYZ, 0, cf, s, undef );
814 }
815 else if (memcmp(&cf, &out, sizeof(cf)) != 0) {
816 /* Will wind up in here if no texture enabled or a couple of
817 * other scenarios (GL_REPLACE for instance).
818 */
819 emit_arith( &p, FP_OPCODE_MOV, out, WRITEMASK_XYZW, 0, cf, undef, undef );
820 }
821
Keith Whitwell47b29f52005-05-04 11:44:44 +0000822 /* Finish up:
823 */
824 emit_arith( &p, FP_OPCODE_END, undef, WRITEMASK_XYZW, 0, undef, undef, undef);
825
Keith Whitwell276330b2005-05-09 17:58:13 +0000826 if (ctx->Fog.Enabled)
827 p.program->FogOption = ctx->Fog.Mode;
828 else
829 p.program->FogOption = GL_NONE;
Keith Whitwell47b29f52005-05-04 11:44:44 +0000830
831 if (p.program->NumTexIndirections > ctx->Const.MaxFragmentProgramTexIndirections)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000832 program_error(&p, "Exceeded max nr indirect texture lookups");
833
Keith Whitwell47b29f52005-05-04 11:44:44 +0000834 if (p.program->NumTexInstructions > ctx->Const.MaxFragmentProgramTexInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000835 program_error(&p, "Exceeded max TEX instructions");
836
Keith Whitwell47b29f52005-05-04 11:44:44 +0000837 if (p.program->NumAluInstructions > ctx->Const.MaxFragmentProgramAluInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000838 program_error(&p, "Exceeded max ALU instructions");
839
Keith Whitwell8b88f622005-05-10 11:39:50 +0000840
Keith Whitwelldbeea252005-05-10 13:57:50 +0000841 /* Notify driver the fragment program has (actually) changed.
Keith Whitwell8b88f622005-05-10 11:39:50 +0000842 */
Keith Whitwelle4902422005-05-11 15:16:35 +0000843 if (ctx->Driver.ProgramStringNotify || DISASSEM) {
844 if (db_Instructions == NULL ||
845 db_NumInstructions != p.program->Base.NumInstructions ||
846 memcmp(db_Instructions, p.program->Instructions,
847 db_NumInstructions * sizeof(*db_Instructions)) != 0) {
848
849 if (ctx->Driver.ProgramStringNotify)
850 ctx->Driver.ProgramStringNotify( ctx, GL_FRAGMENT_PROGRAM_ARB,
851 &p.program->Base );
Keith Whitwelldbeea252005-05-10 13:57:50 +0000852
Keith Whitwelle4902422005-05-11 15:16:35 +0000853 if (DISASSEM) {
854 _mesa_debug_fp_inst(p.program->NumTexInstructions + p.program->NumAluInstructions,
855 p.program->Instructions);
856 _mesa_printf("\n");
857 }
858 }
859
860 FREE(db_Instructions);
861 }
Keith Whitwell15e75e02005-04-29 15:11:39 +0000862}
863
864