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