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