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