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