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