Better driver notification on changes.
[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 <strings.h>
29
30 #include "glheader.h"
31 #include "macros.h"
32 #include "enums.h"
33 #include "texenvprogram.h"
34
35 #include "shader/program.h"
36 #include "shader/nvfragprog.h"
37 #include "shader/arbfragparse.h"
38
39
40 #define DISASSEM 0
41
42 /* Use uregs to represent registers internally, translate to Mesa's
43 * expected formats on emit.
44 *
45 * NOTE: These are passed by value extensively in this file rather
46 * than as usual by pointer reference. If this disturbs you, try
47 * remembering they are just 32bits in size.
48 *
49 * GCC is smart enough to deal with these dword-sized structures in
50 * much the same way as if I had defined them as dwords and was using
51 * macros to access and set the fields. This is much nicer and easier
52 * to evolve.
53 */
54 struct ureg {
55 GLuint file:4;
56 GLuint idx:8;
57 GLuint negatebase:1;
58 GLuint abs:1;
59 GLuint negateabs:1;
60 GLuint swz:12;
61 GLuint pad:5;
62 };
63
64 const static struct ureg undef = {
65 ~0,
66 ~0,
67 0,
68 0,
69 0,
70 0,
71 0
72 };
73
74 #define X 0
75 #define Y 1
76 #define Z 2
77 #define W 3
78
79 /* State used to build the fragment program:
80 */
81 struct texenv_fragment_program {
82 struct fragment_program *program;
83 GLcontext *ctx;
84
85 GLuint tex_temp_flag; /* Temps which have been the result of a texture
86 * operation.
87 */
88
89 GLuint temp_flag; /* Tracks temporary regs which are in
90 * use.
91 */
92
93
94 GLboolean error;
95
96 struct ureg src_texture; /* Reg containing sampled texture color,
97 * else undef.
98 */
99
100 struct ureg src_previous; /* Reg containing color from previous
101 * stage. May need to be decl'd.
102 */
103
104 GLuint last_tex_stage; /* Number of last enabled texture unit */
105 };
106
107
108
109 static struct ureg make_ureg(GLuint file, GLuint idx)
110 {
111 struct ureg reg;
112 reg.file = file;
113 reg.idx = idx;
114 reg.negatebase = 0;
115 reg.abs = 0;
116 reg.negateabs = 0;
117 reg.swz = SWIZZLE_NOOP;
118 reg.pad = 0;
119 return reg;
120 }
121
122 static struct ureg swizzle( struct ureg reg, int x, int y, int z, int w )
123 {
124 reg.swz = MAKE_SWIZZLE4(GET_SWZ(reg.swz, x),
125 GET_SWZ(reg.swz, y),
126 GET_SWZ(reg.swz, z),
127 GET_SWZ(reg.swz, w));
128
129 return reg;
130 }
131
132 static struct ureg swizzle1( struct ureg reg, int x )
133 {
134 return swizzle(reg, x, x, x, x);
135 }
136
137 static GLboolean is_undef( struct ureg reg )
138 {
139 return reg.file == 0xf;
140 }
141
142
143 static struct ureg get_temp( struct texenv_fragment_program *p )
144 {
145 int bit;
146
147 /* First try and reuse texture results:
148 */
149 bit = ffs( ~(p->temp_flag & p->tex_temp_flag) );
150
151 /* Then any unused temporary:
152 */
153 if (!bit)
154 bit = ffs( ~p->temp_flag );
155
156 if (!bit) {
157 fprintf(stderr, "%s: out of temporaries\n", __FILE__);
158 exit(1);
159 }
160
161 p->temp_flag |= 1<<(bit-1);
162 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
163 }
164
165 static struct ureg get_tex_temp( struct texenv_fragment_program *p )
166 {
167 int bit;
168
169 /* First try to find temp not previously used as a texture result:
170 */
171 bit = ffs( ~(p->temp_flag & ~p->tex_temp_flag) );
172
173 /* Then any unused temporary:
174 */
175 if (!bit) {
176 bit = ffs( ~p->temp_flag );
177 p->program->NumTexIndirections++;
178 }
179
180 if (!bit) {
181 fprintf(stderr, "%s: out of temporaries\n", __FILE__);
182 exit(1);
183 }
184
185 p->temp_flag |= 1<<(bit-1);
186 p->tex_temp_flag |= 1<<(bit-1);
187 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
188 }
189
190
191 static void release_temps( struct texenv_fragment_program *p )
192 {
193 p->temp_flag = ~0x7;
194 }
195
196
197 static struct ureg register_param6( struct texenv_fragment_program *p,
198 GLint s0,
199 GLint s1,
200 GLint s2,
201 GLint s3,
202 GLint s4,
203 GLint s5)
204 {
205 GLint tokens[6];
206 GLuint idx;
207 tokens[0] = s0;
208 tokens[1] = s1;
209 tokens[2] = s2;
210 tokens[3] = s3;
211 tokens[4] = s4;
212 tokens[5] = s5;
213 idx = _mesa_add_state_reference( p->program->Parameters, tokens );
214 return make_ureg(PROGRAM_STATE_VAR, idx);
215 }
216
217
218 #define register_param1(p,s0) register_param6(p,s0,0,0,0,0,0)
219 #define register_param2(p,s0,s1) register_param6(p,s0,s1,0,0,0,0)
220 #define register_param3(p,s0,s1,s2) register_param6(p,s0,s1,s2,0,0,0)
221 #define register_param4(p,s0,s1,s2,s3) register_param6(p,s0,s1,s2,s3,0,0)
222
223
224 static struct ureg register_input( struct texenv_fragment_program *p, GLuint input )
225 {
226 p->program->InputsRead |= (1<<input);
227 return make_ureg(PROGRAM_INPUT, input);
228 }
229
230
231 static void emit_arg( struct fp_src_register *reg,
232 struct ureg ureg )
233 {
234 reg->File = ureg.file;
235 reg->Index = ureg.idx;
236 reg->Swizzle = ureg.swz;
237 reg->NegateBase = ureg.negatebase;
238 reg->Abs = ureg.abs;
239 reg->NegateAbs = ureg.negateabs;
240 }
241
242 static void emit_dst( struct fp_dst_register *dst,
243 struct ureg ureg, GLuint mask )
244 {
245 dst->File = ureg.file;
246 dst->Index = ureg.idx;
247 dst->WriteMask = mask;
248 dst->CondMask = 0;
249 dst->CondSwizzle = 0;
250 }
251
252 static struct fp_instruction *
253 emit_op(struct texenv_fragment_program *p,
254 GLuint op,
255 struct ureg dest,
256 GLuint mask,
257 GLuint saturate,
258 struct ureg src0,
259 struct ureg src1,
260 struct ureg src2 )
261 {
262 GLuint nr = p->program->Base.NumInstructions++;
263 struct fp_instruction *inst = &p->program->Instructions[nr];
264
265 memset(inst, 0, sizeof(*inst));
266 inst->Opcode = op;
267
268 emit_arg( &inst->SrcReg[0], src0 );
269 emit_arg( &inst->SrcReg[1], src1 );
270 emit_arg( &inst->SrcReg[2], src2 );
271
272 inst->Saturate = saturate;
273
274 emit_dst( &inst->DstReg, dest, mask );
275
276 return inst;
277 }
278
279
280 static struct ureg emit_arith( struct texenv_fragment_program *p,
281 GLuint op,
282 struct ureg dest,
283 GLuint mask,
284 GLuint saturate,
285 struct ureg src0,
286 struct ureg src1,
287 struct ureg src2 )
288 {
289 emit_op(p, op, dest, mask, saturate, src0, src1, src2);
290
291 p->program->NumAluInstructions++;
292 return dest;
293 }
294
295 static struct ureg emit_texld( struct texenv_fragment_program *p,
296 GLuint op,
297 struct ureg dest,
298 GLuint destmask,
299 GLuint tex_unit,
300 GLuint tex_idx,
301 struct ureg coord )
302 {
303 struct fp_instruction *inst = emit_op( p, op,
304 dest, destmask,
305 0, /* don't saturate? */
306 coord, /* arg 0? */
307 undef,
308 undef);
309
310 inst->TexSrcIdx = tex_idx;
311 inst->TexSrcUnit = tex_unit;
312
313 p->program->NumTexInstructions++;
314
315 if (coord.file != PROGRAM_INPUT &&
316 (coord.idx < FRAG_ATTRIB_TEX0 ||
317 coord.idx > FRAG_ATTRIB_TEX7)) {
318 p->program->NumTexIndirections++;
319 }
320
321 return dest;
322 }
323
324
325 static struct ureg register_const4f( struct texenv_fragment_program *p,
326 GLfloat s0,
327 GLfloat s1,
328 GLfloat s2,
329 GLfloat s3)
330 {
331 GLfloat values[4];
332 GLuint idx;
333 values[0] = s0;
334 values[1] = s1;
335 values[2] = s2;
336 values[3] = s3;
337 idx = _mesa_add_unnamed_constant( p->program->Parameters, values );
338 return make_ureg(PROGRAM_STATE_VAR, idx);
339 }
340
341 #define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
342 #define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
343 #define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
344
345
346
347
348
349
350
351
352 static void program_error( struct texenv_fragment_program *p, const char *msg )
353 {
354 fprintf(stderr, "%s\n", msg);
355 p->error = 1;
356 }
357
358
359 static GLuint translate_tex_src_bit( struct texenv_fragment_program *p,
360 GLuint bit )
361 {
362 switch (bit) {
363 case TEXTURE_1D_BIT: return TEXTURE_1D_INDEX;
364 case TEXTURE_2D_BIT: return TEXTURE_2D_INDEX;
365 case TEXTURE_RECT_BIT: return TEXTURE_RECT_INDEX;
366 case TEXTURE_3D_BIT: return TEXTURE_3D_INDEX;
367 case TEXTURE_CUBE_BIT: return TEXTURE_CUBE_INDEX;
368 default: program_error(p, "TexSrcBit"); return 0;
369 }
370 }
371
372
373 static struct ureg get_source( struct texenv_fragment_program *p,
374 GLenum src, GLuint unit )
375 {
376 switch (src) {
377 case GL_TEXTURE:
378 if (is_undef(p->src_texture)) {
379
380 GLuint dim = translate_tex_src_bit( p, p->ctx->Texture.Unit[unit]._ReallyEnabled);
381 struct ureg texcoord = register_input(p, FRAG_ATTRIB_TEX0+unit);
382 struct ureg tmp = get_tex_temp( p );
383
384 /* TODO: Use D0_MASK_XY where possible.
385 */
386 p->src_texture = emit_texld( p, FP_OPCODE_TXP,
387 tmp, WRITEMASK_XYZW,
388 unit, dim, texcoord );
389 }
390
391 return p->src_texture;
392
393 /* Crossbar: */
394 case GL_TEXTURE0:
395 case GL_TEXTURE1:
396 case GL_TEXTURE2:
397 case GL_TEXTURE3:
398 case GL_TEXTURE4:
399 case GL_TEXTURE5:
400 case GL_TEXTURE6:
401 case GL_TEXTURE7: {
402 return undef;
403 }
404
405 case GL_CONSTANT:
406 return register_param2(p, STATE_TEXENV_COLOR, unit);
407 case GL_PRIMARY_COLOR:
408 return register_input(p, FRAG_ATTRIB_COL0);
409 case GL_PREVIOUS:
410 default:
411 if (is_undef(p->src_previous))
412 return register_input(p, FRAG_ATTRIB_COL0);
413 else
414 return p->src_previous;
415 }
416 }
417
418
419 static struct ureg emit_combine_source( struct texenv_fragment_program *p,
420 GLuint mask,
421 GLuint unit,
422 GLenum source,
423 GLenum operand )
424 {
425 struct ureg arg, src, one;
426
427 src = get_source(p, source, unit);
428
429 switch (operand) {
430 case GL_ONE_MINUS_SRC_COLOR:
431 /* Get unused tmp,
432 * Emit tmp = 1.0 - arg.xyzw
433 */
434 arg = get_temp( p );
435 one = register_const1f(p, 1.0);
436 return emit_arith( p, FP_OPCODE_SUB, arg, mask, 0, one, src, undef);
437
438 case GL_SRC_ALPHA:
439 if (mask == WRITEMASK_W)
440 return src;
441 else
442 return swizzle1( src, W );
443 case GL_ONE_MINUS_SRC_ALPHA:
444 /* Get unused tmp,
445 * Emit tmp = 1.0 - arg.wwww
446 */
447 arg = get_temp( p );
448 one = register_const1f(p, 1.0);
449 return emit_arith( p, FP_OPCODE_SUB, arg, mask, 0,
450 one, swizzle1(src, W), undef);
451 case GL_SRC_COLOR:
452 default:
453 return src;
454 }
455 }
456
457
458
459 static int nr_args( GLenum mode )
460 {
461 switch (mode) {
462 case GL_REPLACE: return 1;
463 case GL_MODULATE: return 2;
464 case GL_ADD: return 2;
465 case GL_ADD_SIGNED: return 2;
466 case GL_INTERPOLATE: return 3;
467 case GL_SUBTRACT: return 2;
468 case GL_DOT3_RGB_EXT: return 2;
469 case GL_DOT3_RGBA_EXT: return 2;
470 case GL_DOT3_RGB: return 2;
471 case GL_DOT3_RGBA: return 2;
472 default: return 0;
473 }
474 }
475
476
477 static GLboolean args_match( struct gl_texture_unit *texUnit )
478 {
479 int i, nr = nr_args(texUnit->_CurrentCombine->ModeRGB);
480
481 for (i = 0 ; i < nr ; i++) {
482 if (texUnit->_CurrentCombine->SourceA[i] != texUnit->_CurrentCombine->SourceRGB[i])
483 return GL_FALSE;
484
485 switch(texUnit->_CurrentCombine->OperandA[i]) {
486 case GL_SRC_ALPHA:
487 switch(texUnit->_CurrentCombine->OperandRGB[i]) {
488 case GL_SRC_COLOR:
489 case GL_SRC_ALPHA:
490 break;
491 default:
492 return GL_FALSE;
493 }
494 break;
495 case GL_ONE_MINUS_SRC_ALPHA:
496 switch(texUnit->_CurrentCombine->OperandRGB[i]) {
497 case GL_ONE_MINUS_SRC_COLOR:
498 case GL_ONE_MINUS_SRC_ALPHA:
499 break;
500 default:
501 return GL_FALSE;
502 }
503 break;
504 default:
505 return GL_FALSE; /* impossible */
506 }
507 }
508
509 return GL_TRUE;
510 }
511
512
513 static struct ureg emit_combine( struct texenv_fragment_program *p,
514 struct ureg dest,
515 GLuint mask,
516 GLuint saturate,
517 GLuint unit,
518 GLenum mode,
519 const GLenum *source,
520 const GLenum *operand)
521 {
522 int nr = nr_args(mode);
523 struct ureg src[3];
524 struct ureg tmp;
525 int i;
526
527 for (i = 0; i < nr; i++)
528 src[i] = emit_combine_source( p, mask, unit, source[i], operand[i] );
529
530 switch (mode) {
531 case GL_REPLACE:
532 if (mask == WRITEMASK_XYZW && !saturate)
533 return src[0];
534 else
535 return emit_arith( p, FP_OPCODE_MOV, dest, mask, saturate, src[0], undef, undef );
536 case GL_MODULATE:
537 return emit_arith( p, FP_OPCODE_MUL, dest, mask, saturate,
538 src[0], src[1], undef );
539 case GL_ADD:
540 return emit_arith( p, FP_OPCODE_ADD, dest, mask, saturate,
541 src[0], src[1], undef );
542 case GL_ADD_SIGNED:
543 /* tmp = arg0 + arg1
544 * result = tmp + -.5
545 */
546 tmp = register_const1f(p, .5);
547 tmp = swizzle1(tmp,X);
548 emit_arith( p, FP_OPCODE_ADD, dest, mask, 0, src[0], src[1], undef );
549 emit_arith( p, FP_OPCODE_SUB, dest, mask, saturate, dest, tmp, undef );
550 return dest;
551 case GL_INTERPOLATE:
552 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
553 */
554 return emit_arith( p, FP_OPCODE_LRP, dest, mask, saturate, src[2], src[0], src[1] );
555
556 case GL_SUBTRACT:
557 return emit_arith( p, FP_OPCODE_SUB, dest, mask, saturate, src[0], src[1], undef );
558
559 case GL_DOT3_RGBA:
560 case GL_DOT3_RGBA_EXT:
561 case GL_DOT3_RGB_EXT:
562 case GL_DOT3_RGB: {
563 struct ureg tmp0 = get_temp( p );
564 struct ureg tmp1 = get_temp( p );
565 struct ureg neg1 = register_const1f(p, -1);
566 struct ureg two = register_const1f(p, 2);
567
568 /* tmp0 = 2*src0 - 1
569 * tmp1 = 2*src1 - 1
570 *
571 * dst = tmp0 dot3 tmp1
572 */
573 emit_arith( p, FP_OPCODE_MAD, tmp0, WRITEMASK_XYZW, 0,
574 two, src[0], neg1);
575
576 if (memcmp(&src[0], &src[1], sizeof(struct ureg)) == 0)
577 tmp1 = tmp0;
578 else
579 emit_arith( p, FP_OPCODE_MAD, tmp1, WRITEMASK_XYZW, 0,
580 two, src[1], neg1);
581 emit_arith( p, FP_OPCODE_DP3, dest, mask, saturate, tmp0, tmp1, undef);
582 return dest;
583 }
584
585 default:
586 return src[0];
587 }
588 }
589
590 static struct ureg get_dest( struct texenv_fragment_program *p, int unit )
591 {
592 if (p->ctx->_TriangleCaps & DD_SEPARATE_SPECULAR)
593 return get_temp( p );
594 else if (unit != p->last_tex_stage)
595 return get_temp( p );
596 else
597 return make_ureg(PROGRAM_OUTPUT, FRAG_OUTPUT_COLR);
598 }
599
600
601
602 static struct ureg emit_texenv( struct texenv_fragment_program *p, int unit )
603 {
604 struct gl_texture_unit *texUnit = &p->ctx->Texture.Unit[unit];
605 GLuint saturate = (unit < p->last_tex_stage);
606 GLuint rgb_shift, alpha_shift;
607 struct ureg out, shift;
608 struct ureg dest = get_dest(p, unit);
609
610 if (!texUnit->_ReallyEnabled) {
611 return get_source(p, GL_PREVIOUS, 0);
612 }
613
614 switch (texUnit->_CurrentCombine->ModeRGB) {
615 case GL_DOT3_RGB_EXT:
616 alpha_shift = texUnit->_CurrentCombine->ScaleShiftA;
617 rgb_shift = 0;
618 break;
619
620 case GL_DOT3_RGBA_EXT:
621 alpha_shift = 0;
622 rgb_shift = 0;
623 break;
624
625 default:
626 rgb_shift = texUnit->_CurrentCombine->ScaleShiftRGB;
627 alpha_shift = texUnit->_CurrentCombine->ScaleShiftA;
628 break;
629 }
630
631
632 /* Emit the RGB and A combine ops
633 */
634 if (texUnit->_CurrentCombine->ModeRGB == texUnit->_CurrentCombine->ModeA &&
635 args_match( texUnit )) {
636 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
637 unit,
638 texUnit->_CurrentCombine->ModeRGB,
639 texUnit->_CurrentCombine->SourceRGB,
640 texUnit->_CurrentCombine->OperandRGB );
641 }
642 else if (texUnit->_CurrentCombine->ModeRGB == GL_DOT3_RGBA_EXT ||
643 texUnit->_CurrentCombine->ModeRGB == GL_DOT3_RGBA) {
644
645 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
646 unit,
647 texUnit->_CurrentCombine->ModeRGB,
648 texUnit->_CurrentCombine->SourceRGB,
649 texUnit->_CurrentCombine->OperandRGB );
650 }
651 else {
652 /* Need to do something to stop from re-emitting identical
653 * argument calculations here:
654 */
655 out = emit_combine( p, dest, WRITEMASK_XYZ, saturate,
656 unit,
657 texUnit->_CurrentCombine->ModeRGB,
658 texUnit->_CurrentCombine->SourceRGB,
659 texUnit->_CurrentCombine->OperandRGB );
660 out = emit_combine( p, dest, WRITEMASK_W, saturate,
661 unit,
662 texUnit->_CurrentCombine->ModeA,
663 texUnit->_CurrentCombine->SourceA,
664 texUnit->_CurrentCombine->OperandA );
665 }
666
667 /* Deal with the final shift:
668 */
669 if (alpha_shift || rgb_shift) {
670 if (rgb_shift == alpha_shift) {
671 shift = register_const1f(p, 1<<rgb_shift);
672 shift = swizzle1(shift,X);
673 }
674 else {
675 shift = register_const2f(p, 1<<rgb_shift, 1<<alpha_shift);
676 shift = swizzle(shift,X,X,X,Y);
677 }
678 return emit_arith( p, FP_OPCODE_MUL, dest, WRITEMASK_XYZW,
679 saturate, out, shift, undef );
680 }
681 else
682 return out;
683 }
684
685 void _mesa_UpdateTexEnvProgram( GLcontext *ctx )
686 {
687 struct texenv_fragment_program p;
688 GLuint unit;
689 struct ureg cf, out;
690
691 if (ctx->FragmentProgram._Enabled)
692 return;
693
694 if (!ctx->_TexEnvProgram)
695 ctx->FragmentProgram._Current = ctx->_TexEnvProgram =
696 (struct fragment_program *)
697 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
698
699 p.ctx = ctx;
700 p.program = ctx->_TexEnvProgram;
701
702 if (p.program->Instructions == NULL) {
703 p.program->Instructions = MALLOC(sizeof(struct fp_instruction) * 100);
704 }
705
706 p.program->Base.NumInstructions = 0;
707 p.program->Base.Target = GL_FRAGMENT_PROGRAM_ARB;
708 p.program->NumTexIndirections = 1; /* correct? */
709 p.program->NumTexInstructions = 0;
710 p.program->NumAluInstructions = 0;
711 p.program->Base.String = 0;
712 p.program->Base.NumInstructions =
713 p.program->Base.NumTemporaries =
714 p.program->Base.NumParameters =
715 p.program->Base.NumAttributes = p.program->Base.NumAddressRegs = 0;
716 if (p.program->Parameters)
717 _mesa_free_parameter_list(p.program->Parameters);
718 p.program->Parameters = _mesa_new_parameter_list();
719 p.program->InputsRead = 0;
720 p.program->OutputsWritten = 0;
721
722 p.src_texture = undef;
723 p.src_previous = undef;
724 p.last_tex_stage = 0;
725
726 if (ctx->Texture._EnabledUnits) {
727 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits ; unit++)
728 if (ctx->Texture.Unit[unit]._ReallyEnabled) {
729 p.last_tex_stage = unit;
730 }
731
732 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits; unit++)
733 if (ctx->Texture.Unit[unit]._ReallyEnabled) {
734 p.src_previous = emit_texenv( &p, unit );
735 p.src_texture = undef;
736 p.temp_flag = 0xffff000;
737 p.temp_flag |= 1 << p.src_previous.idx;
738 }
739 }
740
741 cf = get_source( &p, GL_PREVIOUS, 0 );
742 out = make_ureg( PROGRAM_OUTPUT, FRAG_OUTPUT_COLR );
743
744 if (ctx->_TriangleCaps & DD_SEPARATE_SPECULAR) {
745 /* Emit specular add.
746 */
747 struct ureg s = register_input(&p, FRAG_ATTRIB_COL1);
748 emit_arith( &p, FP_OPCODE_ADD, out, WRITEMASK_XYZ, 0, cf, s, undef );
749 }
750 else if (memcmp(&cf, &out, sizeof(cf)) != 0) {
751 /* Will wind up in here if no texture enabled or a couple of
752 * other scenarios (GL_REPLACE for instance).
753 */
754 emit_arith( &p, FP_OPCODE_MOV, out, WRITEMASK_XYZW, 0, cf, undef, undef );
755 }
756
757 /* Finish up:
758 */
759 emit_arith( &p, FP_OPCODE_END, undef, WRITEMASK_XYZW, 0, undef, undef, undef);
760
761 if (ctx->Fog.Enabled)
762 p.program->FogOption = ctx->Fog.Mode;
763 else
764 p.program->FogOption = GL_NONE;
765
766 if (p.program->NumTexIndirections > ctx->Const.MaxFragmentProgramTexIndirections)
767 program_error(&p, "Exceeded max nr indirect texture lookups");
768
769 if (p.program->NumTexInstructions > ctx->Const.MaxFragmentProgramTexInstructions)
770 program_error(&p, "Exceeded max TEX instructions");
771
772 if (p.program->NumAluInstructions > ctx->Const.MaxFragmentProgramAluInstructions)
773 program_error(&p, "Exceeded max ALU instructions");
774
775 #if DISASSEM
776 _mesa_debug_fp_inst(p.program->NumTexInstructions + p.program->NumAluInstructions,
777 p.program->Instructions);
778 _mesa_printf("\n");
779 #endif
780
781 /* Notify driver the fragment program has (potentially) changed.
782 */
783 ctx->Driver.ProgramStringNotify( ctx, GL_FRAGMENT_PROGRAM_ARB,
784 p.program );
785 }
786
787