mesa: fix temp register allocation problems.
[mesa.git] / src / mesa / main / texenvprogram.c
1 /**************************************************************************
2 *
3 * Copyright 2007 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 "glheader.h"
29 #include "macros.h"
30 #include "enums.h"
31 #include "shader/prog_parameter.h"
32 #include "shader/prog_cache.h"
33 #include "shader/prog_instruction.h"
34 #include "shader/prog_print.h"
35 #include "shader/prog_statevars.h"
36 #include "shader/programopt.h"
37 #include "texenvprogram.h"
38
39 /**
40 * Up to nine instructions per tex unit, plus fog, specular color.
41 */
42 #define MAX_INSTRUCTIONS ((MAX_TEXTURE_UNITS * 9) + 12)
43
44 #define DISASSEM (MESA_VERBOSE & VERBOSE_DISASSEM)
45
46 struct mode_opt {
47 GLuint Source:4;
48 GLuint Operand:3;
49 };
50
51 struct state_key {
52 GLbitfield enabled_units;
53 GLuint separate_specular:1;
54 GLuint fog_enabled:1;
55 GLuint fog_mode:2;
56
57 struct {
58 GLuint enabled:1;
59 GLuint source_index:3; /* one of TEXTURE_1D/2D/3D/CUBE/RECT_INDEX */
60 GLuint ScaleShiftRGB:2;
61 GLuint ScaleShiftA:2;
62
63 GLuint NumArgsRGB:2;
64 GLuint ModeRGB:4;
65 struct mode_opt OptRGB[3];
66
67 GLuint NumArgsA:2;
68 GLuint ModeA:4;
69 struct mode_opt OptA[3];
70 } unit[8];
71 };
72
73 #define FOG_LINEAR 0
74 #define FOG_EXP 1
75 #define FOG_EXP2 2
76 #define FOG_UNKNOWN 3
77
78 static GLuint translate_fog_mode( GLenum mode )
79 {
80 switch (mode) {
81 case GL_LINEAR: return FOG_LINEAR;
82 case GL_EXP: return FOG_EXP;
83 case GL_EXP2: return FOG_EXP2;
84 default: return FOG_UNKNOWN;
85 }
86 }
87
88 #define OPR_SRC_COLOR 0
89 #define OPR_ONE_MINUS_SRC_COLOR 1
90 #define OPR_SRC_ALPHA 2
91 #define OPR_ONE_MINUS_SRC_ALPHA 3
92 #define OPR_ZERO 4
93 #define OPR_ONE 5
94 #define OPR_UNKNOWN 7
95
96 static GLuint translate_operand( GLenum operand )
97 {
98 switch (operand) {
99 case GL_SRC_COLOR: return OPR_SRC_COLOR;
100 case GL_ONE_MINUS_SRC_COLOR: return OPR_ONE_MINUS_SRC_COLOR;
101 case GL_SRC_ALPHA: return OPR_SRC_ALPHA;
102 case GL_ONE_MINUS_SRC_ALPHA: return OPR_ONE_MINUS_SRC_ALPHA;
103 case GL_ZERO: return OPR_ZERO;
104 case GL_ONE: return OPR_ONE;
105 default: return OPR_UNKNOWN;
106 }
107 }
108
109 #define SRC_TEXTURE 0
110 #define SRC_TEXTURE0 1
111 #define SRC_TEXTURE1 2
112 #define SRC_TEXTURE2 3
113 #define SRC_TEXTURE3 4
114 #define SRC_TEXTURE4 5
115 #define SRC_TEXTURE5 6
116 #define SRC_TEXTURE6 7
117 #define SRC_TEXTURE7 8
118 #define SRC_CONSTANT 9
119 #define SRC_PRIMARY_COLOR 10
120 #define SRC_PREVIOUS 11
121 #define SRC_UNKNOWN 15
122
123 static GLuint translate_source( GLenum src )
124 {
125 switch (src) {
126 case GL_TEXTURE: return SRC_TEXTURE;
127 case GL_TEXTURE0:
128 case GL_TEXTURE1:
129 case GL_TEXTURE2:
130 case GL_TEXTURE3:
131 case GL_TEXTURE4:
132 case GL_TEXTURE5:
133 case GL_TEXTURE6:
134 case GL_TEXTURE7: return SRC_TEXTURE0 + (src - GL_TEXTURE0);
135 case GL_CONSTANT: return SRC_CONSTANT;
136 case GL_PRIMARY_COLOR: return SRC_PRIMARY_COLOR;
137 case GL_PREVIOUS: return SRC_PREVIOUS;
138 default: return SRC_UNKNOWN;
139 }
140 }
141
142 #define MODE_REPLACE 0
143 #define MODE_MODULATE 1
144 #define MODE_ADD 2
145 #define MODE_ADD_SIGNED 3
146 #define MODE_INTERPOLATE 4
147 #define MODE_SUBTRACT 5
148 #define MODE_DOT3_RGB 6
149 #define MODE_DOT3_RGB_EXT 7
150 #define MODE_DOT3_RGBA 8
151 #define MODE_DOT3_RGBA_EXT 9
152 #define MODE_MODULATE_ADD_ATI 10
153 #define MODE_MODULATE_SIGNED_ADD_ATI 11
154 #define MODE_MODULATE_SUBTRACT_ATI 12
155 #define MODE_UNKNOWN 15
156
157 static GLuint translate_mode( GLenum mode )
158 {
159 switch (mode) {
160 case GL_REPLACE: return MODE_REPLACE;
161 case GL_MODULATE: return MODE_MODULATE;
162 case GL_ADD: return MODE_ADD;
163 case GL_ADD_SIGNED: return MODE_ADD_SIGNED;
164 case GL_INTERPOLATE: return MODE_INTERPOLATE;
165 case GL_SUBTRACT: return MODE_SUBTRACT;
166 case GL_DOT3_RGB: return MODE_DOT3_RGB;
167 case GL_DOT3_RGB_EXT: return MODE_DOT3_RGB_EXT;
168 case GL_DOT3_RGBA: return MODE_DOT3_RGBA;
169 case GL_DOT3_RGBA_EXT: return MODE_DOT3_RGBA_EXT;
170 case GL_MODULATE_ADD_ATI: return MODE_MODULATE_ADD_ATI;
171 case GL_MODULATE_SIGNED_ADD_ATI: return MODE_MODULATE_SIGNED_ADD_ATI;
172 case GL_MODULATE_SUBTRACT_ATI: return MODE_MODULATE_SUBTRACT_ATI;
173 default: return MODE_UNKNOWN;
174 }
175 }
176
177 #define TEXTURE_UNKNOWN_INDEX 7
178 static GLuint translate_tex_src_bit( GLbitfield bit )
179 {
180 switch (bit) {
181 case TEXTURE_1D_BIT: return TEXTURE_1D_INDEX;
182 case TEXTURE_2D_BIT: return TEXTURE_2D_INDEX;
183 case TEXTURE_RECT_BIT: return TEXTURE_RECT_INDEX;
184 case TEXTURE_3D_BIT: return TEXTURE_3D_INDEX;
185 case TEXTURE_CUBE_BIT: return TEXTURE_CUBE_INDEX;
186 default: return TEXTURE_UNKNOWN_INDEX;
187 }
188 }
189
190 /**
191 * Examine current texture environment state and generate a unique
192 * key to identify it.
193 */
194 static void make_state_key( GLcontext *ctx, struct state_key *key )
195 {
196 GLuint i, j;
197
198 memset(key, 0, sizeof(*key));
199
200 for (i=0;i<MAX_TEXTURE_UNITS;i++) {
201 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
202
203 if (!texUnit->_ReallyEnabled)
204 continue;
205
206 key->unit[i].enabled = 1;
207 key->enabled_units |= (1<<i);
208
209 key->unit[i].source_index =
210 translate_tex_src_bit(texUnit->_ReallyEnabled);
211
212 key->unit[i].NumArgsRGB = texUnit->_CurrentCombine->_NumArgsRGB;
213 key->unit[i].NumArgsA = texUnit->_CurrentCombine->_NumArgsA;
214
215 key->unit[i].ModeRGB =
216 translate_mode(texUnit->_CurrentCombine->ModeRGB);
217 key->unit[i].ModeA =
218 translate_mode(texUnit->_CurrentCombine->ModeA);
219
220 key->unit[i].ScaleShiftRGB = texUnit->_CurrentCombine->ScaleShiftRGB;
221 key->unit[i].ScaleShiftA = texUnit->_CurrentCombine->ScaleShiftA;
222
223 for (j=0;j<3;j++) {
224 key->unit[i].OptRGB[j].Operand =
225 translate_operand(texUnit->_CurrentCombine->OperandRGB[j]);
226 key->unit[i].OptA[j].Operand =
227 translate_operand(texUnit->_CurrentCombine->OperandA[j]);
228 key->unit[i].OptRGB[j].Source =
229 translate_source(texUnit->_CurrentCombine->SourceRGB[j]);
230 key->unit[i].OptA[j].Source =
231 translate_source(texUnit->_CurrentCombine->SourceA[j]);
232 }
233 }
234
235 if (ctx->_TriangleCaps & DD_SEPARATE_SPECULAR)
236 key->separate_specular = 1;
237
238 if (ctx->Fog.Enabled) {
239 key->fog_enabled = 1;
240 key->fog_mode = translate_fog_mode(ctx->Fog.Mode);
241 }
242 }
243
244 /* Use uregs to represent registers internally, translate to Mesa's
245 * expected formats on emit.
246 *
247 * NOTE: These are passed by value extensively in this file rather
248 * than as usual by pointer reference. If this disturbs you, try
249 * remembering they are just 32bits in size.
250 *
251 * GCC is smart enough to deal with these dword-sized structures in
252 * much the same way as if I had defined them as dwords and was using
253 * macros to access and set the fields. This is much nicer and easier
254 * to evolve.
255 */
256 struct ureg {
257 GLuint file:4;
258 GLuint idx:8;
259 GLuint negatebase:1;
260 GLuint abs:1;
261 GLuint negateabs:1;
262 GLuint swz:12;
263 GLuint pad:5;
264 };
265
266 static const struct ureg undef = {
267 PROGRAM_UNDEFINED,
268 ~0,
269 0,
270 0,
271 0,
272 0,
273 0
274 };
275
276
277 /* State used to build the fragment program:
278 */
279 struct texenv_fragment_program {
280 struct gl_fragment_program *program;
281 GLcontext *ctx;
282 struct state_key *state;
283
284 GLbitfield alu_temps; /* Track texture indirections, see spec. */
285 GLbitfield temps_output; /* Track texture indirections, see spec. */
286 GLbitfield temp_in_use; /* Tracks temporary regs which are in use. */
287 GLboolean error;
288
289 struct ureg src_texture[MAX_TEXTURE_UNITS];
290 /* Reg containing each texture unit's sampled texture color,
291 * else undef.
292 */
293
294 struct ureg src_previous; /* Reg containing color from previous
295 * stage. May need to be decl'd.
296 */
297
298 GLuint last_tex_stage; /* Number of last enabled texture unit */
299
300 struct ureg half;
301 struct ureg one;
302 struct ureg zero;
303 };
304
305
306
307 static struct ureg make_ureg(GLuint file, GLuint idx)
308 {
309 struct ureg reg;
310 reg.file = file;
311 reg.idx = idx;
312 reg.negatebase = 0;
313 reg.abs = 0;
314 reg.negateabs = 0;
315 reg.swz = SWIZZLE_NOOP;
316 reg.pad = 0;
317 return reg;
318 }
319
320 static struct ureg swizzle( struct ureg reg, int x, int y, int z, int w )
321 {
322 reg.swz = MAKE_SWIZZLE4(GET_SWZ(reg.swz, x),
323 GET_SWZ(reg.swz, y),
324 GET_SWZ(reg.swz, z),
325 GET_SWZ(reg.swz, w));
326
327 return reg;
328 }
329
330 static struct ureg swizzle1( struct ureg reg, int x )
331 {
332 return swizzle(reg, x, x, x, x);
333 }
334
335 static struct ureg negate( struct ureg reg )
336 {
337 reg.negatebase ^= 1;
338 return reg;
339 }
340
341 static GLboolean is_undef( struct ureg reg )
342 {
343 return reg.file == PROGRAM_UNDEFINED;
344 }
345
346
347 static struct ureg get_temp( struct texenv_fragment_program *p )
348 {
349 GLint bit;
350
351 /* First try and reuse temps which have been used already:
352 */
353 bit = _mesa_ffs( ~p->temp_in_use & p->alu_temps );
354
355 /* Then any unused temporary:
356 */
357 if (!bit)
358 bit = _mesa_ffs( ~p->temp_in_use );
359
360 if (!bit) {
361 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
362 _mesa_exit(1);
363 }
364
365 if ((GLuint) bit > p->program->Base.NumTemporaries)
366 p->program->Base.NumTemporaries = bit;
367
368 p->temp_in_use |= 1<<(bit-1);
369 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
370 }
371
372 static struct ureg get_tex_temp( struct texenv_fragment_program *p )
373 {
374 int bit;
375
376 /* First try to find available temp not previously used (to avoid
377 * starting a new texture indirection). According to the spec, the
378 * ~p->temps_output isn't necessary, but will keep it there for
379 * now:
380 */
381 bit = _mesa_ffs( ~p->temp_in_use & ~p->alu_temps & ~p->temps_output );
382
383 /* Then any unused temporary:
384 */
385 if (!bit)
386 bit = _mesa_ffs( ~p->temp_in_use );
387
388 if (!bit) {
389 _mesa_problem(NULL, "%s: out of temporaries\n", __FILE__);
390 _mesa_exit(1);
391 }
392
393 if ((GLuint) bit > p->program->Base.NumTemporaries)
394 p->program->Base.NumTemporaries = bit;
395
396 p->temp_in_use |= 1<<(bit-1);
397 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
398 }
399
400
401 /** Mark a temp reg as being no longer allocatable. */
402 static void reserve_temp( struct texenv_fragment_program *p, struct ureg r )
403 {
404 if (r.file == PROGRAM_TEMPORARY)
405 p->temps_output |= (1 << r.idx);
406 }
407
408
409 static void release_temps(GLcontext *ctx, struct texenv_fragment_program *p )
410 {
411 GLuint max_temp = ctx->Const.FragmentProgram.MaxTemps;
412
413 /* KW: To support tex_env_crossbar, don't release the registers in
414 * temps_output.
415 */
416 if (max_temp >= sizeof(int) * 8)
417 p->temp_in_use = p->temps_output;
418 else
419 p->temp_in_use = ~((1<<max_temp)-1) | p->temps_output;
420 }
421
422
423 static struct ureg register_param5( struct texenv_fragment_program *p,
424 GLint s0,
425 GLint s1,
426 GLint s2,
427 GLint s3,
428 GLint s4)
429 {
430 gl_state_index tokens[STATE_LENGTH];
431 GLuint idx;
432 tokens[0] = s0;
433 tokens[1] = s1;
434 tokens[2] = s2;
435 tokens[3] = s3;
436 tokens[4] = s4;
437 idx = _mesa_add_state_reference( p->program->Base.Parameters, tokens );
438 return make_ureg(PROGRAM_STATE_VAR, idx);
439 }
440
441
442 #define register_param1(p,s0) register_param5(p,s0,0,0,0,0)
443 #define register_param2(p,s0,s1) register_param5(p,s0,s1,0,0,0)
444 #define register_param3(p,s0,s1,s2) register_param5(p,s0,s1,s2,0,0)
445 #define register_param4(p,s0,s1,s2,s3) register_param5(p,s0,s1,s2,s3,0)
446
447
448 static struct ureg register_input( struct texenv_fragment_program *p, GLuint input )
449 {
450 p->program->Base.InputsRead |= (1 << input);
451 return make_ureg(PROGRAM_INPUT, input);
452 }
453
454
455 static void emit_arg( struct prog_src_register *reg,
456 struct ureg ureg )
457 {
458 reg->File = ureg.file;
459 reg->Index = ureg.idx;
460 reg->Swizzle = ureg.swz;
461 reg->NegateBase = ureg.negatebase ? 0xf : 0x0;
462 reg->Abs = ureg.abs;
463 reg->NegateAbs = ureg.negateabs;
464 }
465
466 static void emit_dst( struct prog_dst_register *dst,
467 struct ureg ureg, GLuint mask )
468 {
469 dst->File = ureg.file;
470 dst->Index = ureg.idx;
471 dst->WriteMask = mask;
472 dst->CondMask = COND_TR; /* always pass cond test */
473 dst->CondSwizzle = SWIZZLE_NOOP;
474 }
475
476 static struct prog_instruction *
477 emit_op(struct texenv_fragment_program *p,
478 enum prog_opcode op,
479 struct ureg dest,
480 GLuint mask,
481 GLboolean saturate,
482 struct ureg src0,
483 struct ureg src1,
484 struct ureg src2 )
485 {
486 GLuint nr = p->program->Base.NumInstructions++;
487 struct prog_instruction *inst = &p->program->Base.Instructions[nr];
488
489 assert(nr < MAX_INSTRUCTIONS);
490
491 _mesa_init_instructions(inst, 1);
492 inst->Opcode = op;
493
494 emit_arg( &inst->SrcReg[0], src0 );
495 emit_arg( &inst->SrcReg[1], src1 );
496 emit_arg( &inst->SrcReg[2], src2 );
497
498 inst->SaturateMode = saturate ? SATURATE_ZERO_ONE : SATURATE_OFF;
499
500 emit_dst( &inst->DstReg, dest, mask );
501
502 #if 0
503 /* Accounting for indirection tracking:
504 */
505 if (dest.file == PROGRAM_TEMPORARY)
506 p->temps_output |= 1 << dest.idx;
507 #endif
508
509 return inst;
510 }
511
512
513 static struct ureg emit_arith( struct texenv_fragment_program *p,
514 enum prog_opcode op,
515 struct ureg dest,
516 GLuint mask,
517 GLboolean saturate,
518 struct ureg src0,
519 struct ureg src1,
520 struct ureg src2 )
521 {
522 emit_op(p, op, dest, mask, saturate, src0, src1, src2);
523
524 /* Accounting for indirection tracking:
525 */
526 if (src0.file == PROGRAM_TEMPORARY)
527 p->alu_temps |= 1 << src0.idx;
528
529 if (!is_undef(src1) && src1.file == PROGRAM_TEMPORARY)
530 p->alu_temps |= 1 << src1.idx;
531
532 if (!is_undef(src2) && src2.file == PROGRAM_TEMPORARY)
533 p->alu_temps |= 1 << src2.idx;
534
535 if (dest.file == PROGRAM_TEMPORARY)
536 p->alu_temps |= 1 << dest.idx;
537
538 p->program->Base.NumAluInstructions++;
539 return dest;
540 }
541
542 static struct ureg emit_texld( struct texenv_fragment_program *p,
543 enum prog_opcode op,
544 struct ureg dest,
545 GLuint destmask,
546 GLuint tex_unit,
547 GLuint tex_idx,
548 struct ureg coord )
549 {
550 struct prog_instruction *inst = emit_op( p, op,
551 dest, destmask,
552 GL_FALSE, /* don't saturate? */
553 coord, /* arg 0? */
554 undef,
555 undef);
556
557 inst->TexSrcTarget = tex_idx;
558 inst->TexSrcUnit = tex_unit;
559
560 p->program->Base.NumTexInstructions++;
561
562 /* Accounting for indirection tracking:
563 */
564 reserve_temp(p, dest);
565
566 /* Is this a texture indirection?
567 */
568 if ((coord.file == PROGRAM_TEMPORARY &&
569 (p->temps_output & (1<<coord.idx))) ||
570 (dest.file == PROGRAM_TEMPORARY &&
571 (p->alu_temps & (1<<dest.idx)))) {
572 p->program->Base.NumTexIndirections++;
573 p->temps_output = 1<<coord.idx;
574 p->alu_temps = 0;
575 assert(0); /* KW: texture env crossbar */
576 }
577
578 return dest;
579 }
580
581
582 static struct ureg register_const4f( struct texenv_fragment_program *p,
583 GLfloat s0,
584 GLfloat s1,
585 GLfloat s2,
586 GLfloat s3)
587 {
588 GLfloat values[4];
589 GLuint idx, swizzle;
590 struct ureg r;
591 values[0] = s0;
592 values[1] = s1;
593 values[2] = s2;
594 values[3] = s3;
595 idx = _mesa_add_unnamed_constant( p->program->Base.Parameters, values, 4,
596 &swizzle );
597 r = make_ureg(PROGRAM_CONSTANT, idx);
598 r.swz = swizzle;
599 return r;
600 }
601
602 #define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
603 #define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
604 #define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
605 #define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
606
607
608 static struct ureg get_one( struct texenv_fragment_program *p )
609 {
610 if (is_undef(p->one))
611 p->one = register_scalar_const(p, 1.0);
612 return p->one;
613 }
614
615 static struct ureg get_half( struct texenv_fragment_program *p )
616 {
617 if (is_undef(p->half))
618 p->half = register_scalar_const(p, 0.5);
619 return p->half;
620 }
621
622 static struct ureg get_zero( struct texenv_fragment_program *p )
623 {
624 if (is_undef(p->zero))
625 p->zero = register_scalar_const(p, 0.0);
626 return p->zero;
627 }
628
629
630 static void program_error( struct texenv_fragment_program *p, const char *msg )
631 {
632 _mesa_problem(NULL, msg);
633 p->error = 1;
634 }
635
636 static struct ureg get_source( struct texenv_fragment_program *p,
637 GLuint src, GLuint unit )
638 {
639 switch (src) {
640 case SRC_TEXTURE:
641 assert(!is_undef(p->src_texture[unit]));
642 return p->src_texture[unit];
643
644 case SRC_TEXTURE0:
645 case SRC_TEXTURE1:
646 case SRC_TEXTURE2:
647 case SRC_TEXTURE3:
648 case SRC_TEXTURE4:
649 case SRC_TEXTURE5:
650 case SRC_TEXTURE6:
651 case SRC_TEXTURE7:
652 assert(!is_undef(p->src_texture[src - SRC_TEXTURE0]));
653 return p->src_texture[src - SRC_TEXTURE0];
654
655 case SRC_CONSTANT:
656 return register_param2(p, STATE_TEXENV_COLOR, unit);
657
658 case SRC_PRIMARY_COLOR:
659 return register_input(p, FRAG_ATTRIB_COL0);
660
661 case SRC_PREVIOUS:
662 default:
663 if (is_undef(p->src_previous))
664 return register_input(p, FRAG_ATTRIB_COL0);
665 else
666 return p->src_previous;
667 }
668 }
669
670 static struct ureg emit_combine_source( struct texenv_fragment_program *p,
671 GLuint mask,
672 GLuint unit,
673 GLuint source,
674 GLuint operand )
675 {
676 struct ureg arg, src, one;
677
678 src = get_source(p, source, unit);
679
680 switch (operand) {
681 case OPR_ONE_MINUS_SRC_COLOR:
682 /* Get unused tmp,
683 * Emit tmp = 1.0 - arg.xyzw
684 */
685 arg = get_temp( p );
686 one = get_one( p );
687 return emit_arith( p, OPCODE_SUB, arg, mask, 0, one, src, undef);
688
689 case OPR_SRC_ALPHA:
690 if (mask == WRITEMASK_W)
691 return src;
692 else
693 return swizzle1( src, SWIZZLE_W );
694 case OPR_ONE_MINUS_SRC_ALPHA:
695 /* Get unused tmp,
696 * Emit tmp = 1.0 - arg.wwww
697 */
698 arg = get_temp(p);
699 one = get_one(p);
700 return emit_arith(p, OPCODE_SUB, arg, mask, 0,
701 one, swizzle1(src, SWIZZLE_W), undef);
702 case OPR_ZERO:
703 return get_zero(p);
704 case OPR_ONE:
705 return get_one(p);
706 case OPR_SRC_COLOR:
707 default:
708 return src;
709 }
710 }
711
712 static GLboolean args_match( struct state_key *key, GLuint unit )
713 {
714 GLuint i, nr = key->unit[unit].NumArgsRGB;
715
716 for (i = 0 ; i < nr ; i++) {
717 if (key->unit[unit].OptA[i].Source != key->unit[unit].OptRGB[i].Source)
718 return GL_FALSE;
719
720 switch(key->unit[unit].OptA[i].Operand) {
721 case OPR_SRC_ALPHA:
722 switch(key->unit[unit].OptRGB[i].Operand) {
723 case OPR_SRC_COLOR:
724 case OPR_SRC_ALPHA:
725 break;
726 default:
727 return GL_FALSE;
728 }
729 break;
730 case OPR_ONE_MINUS_SRC_ALPHA:
731 switch(key->unit[unit].OptRGB[i].Operand) {
732 case OPR_ONE_MINUS_SRC_COLOR:
733 case OPR_ONE_MINUS_SRC_ALPHA:
734 break;
735 default:
736 return GL_FALSE;
737 }
738 break;
739 default:
740 return GL_FALSE; /* impossible */
741 }
742 }
743
744 return GL_TRUE;
745 }
746
747 static struct ureg emit_combine( struct texenv_fragment_program *p,
748 struct ureg dest,
749 GLuint mask,
750 GLboolean saturate,
751 GLuint unit,
752 GLuint nr,
753 GLuint mode,
754 const struct mode_opt *opt)
755 {
756 struct ureg src[3];
757 struct ureg tmp, half;
758 GLuint i;
759
760 tmp = undef; /* silence warning (bug 5318) */
761
762 for (i = 0; i < nr; i++)
763 src[i] = emit_combine_source( p, mask, unit, opt[i].Source, opt[i].Operand );
764
765 switch (mode) {
766 case MODE_REPLACE:
767 if (mask == WRITEMASK_XYZW && !saturate)
768 return src[0];
769 else
770 return emit_arith( p, OPCODE_MOV, dest, mask, saturate, src[0], undef, undef );
771 case MODE_MODULATE:
772 return emit_arith( p, OPCODE_MUL, dest, mask, saturate,
773 src[0], src[1], undef );
774 case MODE_ADD:
775 return emit_arith( p, OPCODE_ADD, dest, mask, saturate,
776 src[0], src[1], undef );
777 case MODE_ADD_SIGNED:
778 /* tmp = arg0 + arg1
779 * result = tmp - .5
780 */
781 half = get_half(p);
782 tmp = get_temp( p );
783 emit_arith( p, OPCODE_ADD, tmp, mask, 0, src[0], src[1], undef );
784 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp, half, undef );
785 return dest;
786 case MODE_INTERPOLATE:
787 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
788 */
789 return emit_arith( p, OPCODE_LRP, dest, mask, saturate, src[2], src[0], src[1] );
790
791 case MODE_SUBTRACT:
792 return emit_arith( p, OPCODE_SUB, dest, mask, saturate, src[0], src[1], undef );
793
794 case MODE_DOT3_RGBA:
795 case MODE_DOT3_RGBA_EXT:
796 case MODE_DOT3_RGB_EXT:
797 case MODE_DOT3_RGB: {
798 struct ureg tmp0 = get_temp( p );
799 struct ureg tmp1 = get_temp( p );
800 struct ureg neg1 = register_scalar_const(p, -1);
801 struct ureg two = register_scalar_const(p, 2);
802
803 /* tmp0 = 2*src0 - 1
804 * tmp1 = 2*src1 - 1
805 *
806 * dst = tmp0 dot3 tmp1
807 */
808 emit_arith( p, OPCODE_MAD, tmp0, WRITEMASK_XYZW, 0,
809 two, src[0], neg1);
810
811 if (_mesa_memcmp(&src[0], &src[1], sizeof(struct ureg)) == 0)
812 tmp1 = tmp0;
813 else
814 emit_arith( p, OPCODE_MAD, tmp1, WRITEMASK_XYZW, 0,
815 two, src[1], neg1);
816 emit_arith( p, OPCODE_DP3, dest, mask, saturate, tmp0, tmp1, undef);
817 return dest;
818 }
819 case MODE_MODULATE_ADD_ATI:
820 /* Arg0 * Arg2 + Arg1 */
821 return emit_arith( p, OPCODE_MAD, dest, mask, saturate,
822 src[0], src[2], src[1] );
823 case MODE_MODULATE_SIGNED_ADD_ATI: {
824 /* Arg0 * Arg2 + Arg1 - 0.5 */
825 struct ureg tmp0 = get_temp(p);
826 half = get_half(p);
827 emit_arith( p, OPCODE_MAD, tmp0, mask, 0, src[0], src[2], src[1] );
828 emit_arith( p, OPCODE_SUB, dest, mask, saturate, tmp0, half, undef );
829 return dest;
830 }
831 case MODE_MODULATE_SUBTRACT_ATI:
832 /* Arg0 * Arg2 - Arg1 */
833 emit_arith( p, OPCODE_MAD, dest, mask, 0, src[0], src[2], negate(src[1]) );
834 return dest;
835 default:
836 return src[0];
837 }
838 }
839
840
841 /**
842 * Generate instructions for one texture unit's env/combiner mode.
843 */
844 static struct ureg
845 emit_texenv(struct texenv_fragment_program *p, GLuint unit)
846 {
847 struct state_key *key = p->state;
848 GLboolean saturate = (unit < p->last_tex_stage);
849 GLuint rgb_shift, alpha_shift;
850 struct ureg out, shift;
851 struct ureg dest;
852
853 if (!key->unit[unit].enabled) {
854 return get_source(p, SRC_PREVIOUS, 0);
855 }
856
857 switch (key->unit[unit].ModeRGB) {
858 case MODE_DOT3_RGB_EXT:
859 alpha_shift = key->unit[unit].ScaleShiftA;
860 rgb_shift = 0;
861 break;
862 case MODE_DOT3_RGBA_EXT:
863 alpha_shift = 0;
864 rgb_shift = 0;
865 break;
866 default:
867 rgb_shift = key->unit[unit].ScaleShiftRGB;
868 alpha_shift = key->unit[unit].ScaleShiftA;
869 break;
870 }
871
872 /* If this is the very last calculation, emit direct to output reg:
873 */
874 if (key->separate_specular ||
875 unit != p->last_tex_stage ||
876 alpha_shift ||
877 rgb_shift)
878 dest = get_temp( p );
879 else
880 dest = make_ureg(PROGRAM_OUTPUT, FRAG_RESULT_COLR);
881
882 /* Emit the RGB and A combine ops
883 */
884 if (key->unit[unit].ModeRGB == key->unit[unit].ModeA &&
885 args_match(key, unit)) {
886 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
887 unit,
888 key->unit[unit].NumArgsRGB,
889 key->unit[unit].ModeRGB,
890 key->unit[unit].OptRGB);
891 }
892 else if (key->unit[unit].ModeRGB == MODE_DOT3_RGBA_EXT ||
893 key->unit[unit].ModeRGB == MODE_DOT3_RGBA) {
894
895 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
896 unit,
897 key->unit[unit].NumArgsRGB,
898 key->unit[unit].ModeRGB,
899 key->unit[unit].OptRGB);
900 }
901 else {
902 /* Need to do something to stop from re-emitting identical
903 * argument calculations here:
904 */
905 out = emit_combine( p, dest, WRITEMASK_XYZ, saturate,
906 unit,
907 key->unit[unit].NumArgsRGB,
908 key->unit[unit].ModeRGB,
909 key->unit[unit].OptRGB);
910 out = emit_combine( p, dest, WRITEMASK_W, saturate,
911 unit,
912 key->unit[unit].NumArgsA,
913 key->unit[unit].ModeA,
914 key->unit[unit].OptA);
915 }
916
917 /* Deal with the final shift:
918 */
919 if (alpha_shift || rgb_shift) {
920 if (rgb_shift == alpha_shift) {
921 shift = register_scalar_const(p, (GLfloat)(1<<rgb_shift));
922 }
923 else {
924 shift = register_const4f(p,
925 (GLfloat)(1<<rgb_shift),
926 (GLfloat)(1<<rgb_shift),
927 (GLfloat)(1<<rgb_shift),
928 (GLfloat)(1<<alpha_shift));
929 }
930 return emit_arith( p, OPCODE_MUL, dest, WRITEMASK_XYZW,
931 saturate, out, shift, undef );
932 }
933 else
934 return out;
935 }
936
937
938 /**
939 * Generate instruction for getting a texture source term.
940 */
941 static void load_texture( struct texenv_fragment_program *p, GLuint unit )
942 {
943 if (is_undef(p->src_texture[unit])) {
944 GLuint dim = p->state->unit[unit].source_index;
945 struct ureg texcoord = register_input(p, FRAG_ATTRIB_TEX0+unit);
946 struct ureg tmp = get_tex_temp( p );
947
948 if (dim == TEXTURE_UNKNOWN_INDEX)
949 program_error(p, "TexSrcBit");
950
951 /* TODO: Use D0_MASK_XY where possible.
952 */
953 if (p->state->unit[unit].enabled) {
954 p->src_texture[unit] = emit_texld( p, OPCODE_TXP,
955 tmp, WRITEMASK_XYZW,
956 unit, dim, texcoord );
957 p->program->Base.SamplersUsed |= (1 << unit);
958 /* This identity mapping should already be in place
959 * (see _mesa_init_program_struct()) but let's be safe.
960 */
961 p->program->Base.SamplerUnits[unit] = unit;
962 }
963 else
964 p->src_texture[unit] = get_zero(p);
965 }
966 }
967
968 static GLboolean load_texenv_source( struct texenv_fragment_program *p,
969 GLuint src, GLuint unit )
970 {
971 switch (src) {
972 case SRC_TEXTURE:
973 load_texture(p, unit);
974 break;
975
976 case SRC_TEXTURE0:
977 case SRC_TEXTURE1:
978 case SRC_TEXTURE2:
979 case SRC_TEXTURE3:
980 case SRC_TEXTURE4:
981 case SRC_TEXTURE5:
982 case SRC_TEXTURE6:
983 case SRC_TEXTURE7:
984 load_texture(p, src - SRC_TEXTURE0);
985 break;
986
987 default:
988 break;
989 }
990
991 return GL_TRUE;
992 }
993
994
995 /**
996 * Generate instructions for loading all texture source terms.
997 */
998 static GLboolean
999 load_texunit_sources( struct texenv_fragment_program *p, int unit )
1000 {
1001 struct state_key *key = p->state;
1002 GLuint i;
1003
1004 for (i = 0; i < key->unit[unit].NumArgsRGB; i++) {
1005 load_texenv_source( p, key->unit[unit].OptRGB[i].Source, unit);
1006 }
1007
1008 for (i = 0; i < key->unit[unit].NumArgsA; i++) {
1009 load_texenv_source( p, key->unit[unit].OptA[i].Source, unit );
1010 }
1011
1012 return GL_TRUE;
1013 }
1014
1015
1016 /**
1017 * Generate a new fragment program which implements the context's
1018 * current texture env/combine mode.
1019 */
1020 static void
1021 create_new_program(GLcontext *ctx, struct state_key *key,
1022 struct gl_fragment_program *program)
1023 {
1024 struct prog_instruction instBuffer[MAX_INSTRUCTIONS];
1025 struct texenv_fragment_program p;
1026 GLuint unit;
1027 struct ureg cf, out;
1028
1029 _mesa_memset(&p, 0, sizeof(p));
1030 p.ctx = ctx;
1031 p.state = key;
1032 p.program = program;
1033
1034 /* During code generation, use locally-allocated instruction buffer,
1035 * then alloc dynamic storage below.
1036 */
1037 p.program->Base.Instructions = instBuffer;
1038 p.program->Base.Target = GL_FRAGMENT_PROGRAM_ARB;
1039 p.program->Base.NumTexIndirections = 1; /* correct? */
1040 p.program->Base.NumTexInstructions = 0;
1041 p.program->Base.NumAluInstructions = 0;
1042 p.program->Base.String = NULL;
1043 p.program->Base.NumInstructions =
1044 p.program->Base.NumTemporaries =
1045 p.program->Base.NumParameters =
1046 p.program->Base.NumAttributes = p.program->Base.NumAddressRegs = 0;
1047 p.program->Base.Parameters = _mesa_new_parameter_list();
1048
1049 p.program->Base.InputsRead = 0;
1050 p.program->Base.OutputsWritten = 1 << FRAG_RESULT_COLR;
1051
1052 for (unit = 0; unit < MAX_TEXTURE_UNITS; unit++)
1053 p.src_texture[unit] = undef;
1054
1055 p.src_previous = undef;
1056 p.half = undef;
1057 p.zero = undef;
1058 p.one = undef;
1059
1060 p.last_tex_stage = 0;
1061 release_temps(ctx, &p);
1062
1063 if (key->enabled_units) {
1064 /* First pass - to support texture_env_crossbar, first identify
1065 * all referenced texture sources and emit texld instructions
1066 * for each:
1067 */
1068 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits ; unit++)
1069 if (key->unit[unit].enabled) {
1070 load_texunit_sources( &p, unit );
1071 p.last_tex_stage = unit;
1072 }
1073
1074 /* Second pass - emit combine instructions to build final color:
1075 */
1076 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits; unit++)
1077 if (key->enabled_units & (1<<unit)) {
1078 p.src_previous = emit_texenv( &p, unit );
1079 reserve_temp(&p, p.src_previous); /* don't re-use this temp reg */
1080 release_temps(ctx, &p); /* release all temps */
1081 }
1082 }
1083
1084 cf = get_source( &p, SRC_PREVIOUS, 0 );
1085 out = make_ureg( PROGRAM_OUTPUT, FRAG_RESULT_COLR );
1086
1087 if (key->separate_specular) {
1088 /* Emit specular add.
1089 */
1090 struct ureg s = register_input(&p, FRAG_ATTRIB_COL1);
1091 emit_arith( &p, OPCODE_ADD, out, WRITEMASK_XYZ, 0, cf, s, undef );
1092 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_W, 0, cf, undef, undef );
1093 }
1094 else if (_mesa_memcmp(&cf, &out, sizeof(cf)) != 0) {
1095 /* Will wind up in here if no texture enabled or a couple of
1096 * other scenarios (GL_REPLACE for instance).
1097 */
1098 emit_arith( &p, OPCODE_MOV, out, WRITEMASK_XYZW, 0, cf, undef, undef );
1099 }
1100
1101 /* Finish up:
1102 */
1103 emit_arith( &p, OPCODE_END, undef, WRITEMASK_XYZW, 0, undef, undef, undef);
1104
1105 if (key->fog_enabled) {
1106 /* Pull fog mode from GLcontext, the value in the state key is
1107 * a reduced value and not what is expected in FogOption
1108 */
1109 p.program->FogOption = ctx->Fog.Mode;
1110 p.program->Base.InputsRead |= FRAG_BIT_FOGC; /* XXX new */
1111 } else
1112 p.program->FogOption = GL_NONE;
1113
1114 if (p.program->Base.NumTexIndirections > ctx->Const.FragmentProgram.MaxTexIndirections)
1115 program_error(&p, "Exceeded max nr indirect texture lookups");
1116
1117 if (p.program->Base.NumTexInstructions > ctx->Const.FragmentProgram.MaxTexInstructions)
1118 program_error(&p, "Exceeded max TEX instructions");
1119
1120 if (p.program->Base.NumAluInstructions > ctx->Const.FragmentProgram.MaxAluInstructions)
1121 program_error(&p, "Exceeded max ALU instructions");
1122
1123 ASSERT(p.program->Base.NumInstructions <= MAX_INSTRUCTIONS);
1124
1125 /* Allocate final instruction array */
1126 p.program->Base.Instructions
1127 = _mesa_alloc_instructions(p.program->Base.NumInstructions);
1128 if (!p.program->Base.Instructions) {
1129 _mesa_error(ctx, GL_OUT_OF_MEMORY,
1130 "generating tex env program");
1131 return;
1132 }
1133 _mesa_copy_instructions(p.program->Base.Instructions, instBuffer,
1134 p.program->Base.NumInstructions);
1135
1136 if (p.program->FogOption) {
1137 _mesa_append_fog_code(ctx, p.program);
1138 p.program->FogOption = GL_NONE;
1139 }
1140
1141
1142 /* Notify driver the fragment program has (actually) changed.
1143 */
1144 if (ctx->Driver.ProgramStringNotify) {
1145 ctx->Driver.ProgramStringNotify( ctx, GL_FRAGMENT_PROGRAM_ARB,
1146 &p.program->Base );
1147 }
1148
1149 if (DISASSEM) {
1150 _mesa_print_program(&p.program->Base);
1151 _mesa_printf("\n");
1152 }
1153 }
1154
1155
1156 /**
1157 * Return a fragment program which implements the current
1158 * fixed-function texture, fog and color-sum operations.
1159 */
1160 struct gl_fragment_program *
1161 _mesa_get_fixed_func_fragment_program(GLcontext *ctx)
1162 {
1163 struct gl_fragment_program *prog;
1164 struct state_key key;
1165
1166 make_state_key(ctx, &key);
1167
1168 prog = (struct gl_fragment_program *)
1169 _mesa_search_program_cache(ctx->FragmentProgram.Cache,
1170 &key, sizeof(key));
1171
1172 if (!prog) {
1173 prog = (struct gl_fragment_program *)
1174 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
1175
1176 create_new_program(ctx, &key, prog);
1177
1178 _mesa_program_cache_insert(ctx, ctx->FragmentProgram.Cache,
1179 &key, sizeof(key), &prog->Base);
1180 }
1181
1182 return prog;
1183 }
1184
1185
1186
1187 /**
1188 * If _MaintainTexEnvProgram is set we'll generate a fragment program that
1189 * implements the current texture env/combine mode.
1190 * This function generates that program and puts it into effect.
1191 */
1192 void
1193 _mesa_UpdateTexEnvProgram( GLcontext *ctx )
1194 {
1195 const struct gl_fragment_program *prev = ctx->FragmentProgram._Current;
1196
1197 ASSERT(ctx->FragmentProgram._MaintainTexEnvProgram);
1198
1199 /* If a conventional fragment program/shader isn't in effect... */
1200 if (!ctx->FragmentProgram._Enabled &&
1201 (!ctx->Shader.CurrentProgram ||
1202 !ctx->Shader.CurrentProgram->FragmentProgram) ) {
1203
1204 ctx->FragmentProgram._Current
1205 = ctx->FragmentProgram._TexEnvProgram
1206 = _mesa_get_fixed_func_fragment_program(ctx);
1207 }
1208
1209 /* Tell the driver about the change. Could define a new target for
1210 * this?
1211 */
1212 if (ctx->FragmentProgram._Current != prev && ctx->Driver.BindProgram) {
1213 ctx->Driver.BindProgram(ctx, GL_FRAGMENT_PROGRAM_ARB,
1214 (struct gl_program *) ctx->FragmentProgram._Current);
1215 }
1216 }