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