0c2caa0169ec6b41e11182effcd3a06a4d38904a
[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 (MESA_VERBOSE & VERBOSE_DISASSEM)
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 temp_used_for_txp; /* Temps which have been the result of a texture
86 * operation.
87 */
88
89 GLuint temp_in_use; /* 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 temps which have been used for texture
148 * results:
149 */
150 bit = ffs( ~p->temp_in_use & p->temp_used_for_txp );
151
152 /* Then any unused temporary:
153 */
154 if (!bit)
155 bit = ffs( ~p->temp_in_use );
156
157 if (!bit) {
158 fprintf(stderr, "%s: out of temporaries\n", __FILE__);
159 exit(1);
160 }
161
162 p->temp_in_use |= 1<<(bit-1);
163 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
164 }
165
166 static struct ureg get_tex_temp( struct texenv_fragment_program *p )
167 {
168 int bit;
169
170 /* First try to find availble temp not previously used as a texture
171 * result:
172 */
173 bit = ffs( ~p->temp_in_use & ~p->temp_used_for_txp );
174
175 /* Then any unused temporary:
176 */
177 if (!bit) {
178 bit = ffs( ~p->temp_in_use );
179 p->program->NumTexIndirections++;
180 }
181
182 if (!bit) {
183 fprintf(stderr, "%s: out of temporaries\n", __FILE__);
184 exit(1);
185 }
186
187 p->temp_in_use |= 1<<(bit-1);
188 p->temp_used_for_txp |= 1<<(bit-1);
189 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
190 }
191
192
193 static void release_temps( struct texenv_fragment_program *p )
194 {
195 GLuint max_temp = p->ctx->Const.MaxFragmentProgramTemps;
196
197 if (max_temp >= sizeof(int) * 8)
198 p->temp_in_use = 0;
199 else
200 p->temp_in_use = ~((1<<max_temp)-1);
201 }
202
203
204 static struct ureg register_param6( struct texenv_fragment_program *p,
205 GLint s0,
206 GLint s1,
207 GLint s2,
208 GLint s3,
209 GLint s4,
210 GLint s5)
211 {
212 GLint tokens[6];
213 GLuint idx;
214 tokens[0] = s0;
215 tokens[1] = s1;
216 tokens[2] = s2;
217 tokens[3] = s3;
218 tokens[4] = s4;
219 tokens[5] = s5;
220 idx = _mesa_add_state_reference( p->program->Parameters, tokens );
221 return make_ureg(PROGRAM_STATE_VAR, idx);
222 }
223
224
225 #define register_param1(p,s0) register_param6(p,s0,0,0,0,0,0)
226 #define register_param2(p,s0,s1) register_param6(p,s0,s1,0,0,0,0)
227 #define register_param3(p,s0,s1,s2) register_param6(p,s0,s1,s2,0,0,0)
228 #define register_param4(p,s0,s1,s2,s3) register_param6(p,s0,s1,s2,s3,0,0)
229
230
231 static struct ureg register_input( struct texenv_fragment_program *p, GLuint input )
232 {
233 p->program->InputsRead |= (1<<input);
234 return make_ureg(PROGRAM_INPUT, input);
235 }
236
237
238 static void emit_arg( struct fp_src_register *reg,
239 struct ureg ureg )
240 {
241 reg->File = ureg.file;
242 reg->Index = ureg.idx;
243 reg->Swizzle = ureg.swz;
244 reg->NegateBase = ureg.negatebase;
245 reg->Abs = ureg.abs;
246 reg->NegateAbs = ureg.negateabs;
247 }
248
249 static void emit_dst( struct fp_dst_register *dst,
250 struct ureg ureg, GLuint mask )
251 {
252 dst->File = ureg.file;
253 dst->Index = ureg.idx;
254 dst->WriteMask = mask;
255 dst->CondMask = 0;
256 dst->CondSwizzle = 0;
257 }
258
259 static struct fp_instruction *
260 emit_op(struct texenv_fragment_program *p,
261 GLuint op,
262 struct ureg dest,
263 GLuint mask,
264 GLuint saturate,
265 struct ureg src0,
266 struct ureg src1,
267 struct ureg src2 )
268 {
269 GLuint nr = p->program->Base.NumInstructions++;
270 struct fp_instruction *inst = &p->program->Instructions[nr];
271
272 memset(inst, 0, sizeof(*inst));
273 inst->Opcode = op;
274
275 emit_arg( &inst->SrcReg[0], src0 );
276 emit_arg( &inst->SrcReg[1], src1 );
277 emit_arg( &inst->SrcReg[2], src2 );
278
279 inst->Saturate = saturate;
280
281 emit_dst( &inst->DstReg, dest, mask );
282
283 return inst;
284 }
285
286
287 static struct ureg emit_arith( struct texenv_fragment_program *p,
288 GLuint op,
289 struct ureg dest,
290 GLuint mask,
291 GLuint saturate,
292 struct ureg src0,
293 struct ureg src1,
294 struct ureg src2 )
295 {
296 emit_op(p, op, dest, mask, saturate, src0, src1, src2);
297
298 p->program->NumAluInstructions++;
299 return dest;
300 }
301
302 static struct ureg emit_texld( struct texenv_fragment_program *p,
303 GLuint op,
304 struct ureg dest,
305 GLuint destmask,
306 GLuint tex_unit,
307 GLuint tex_idx,
308 struct ureg coord )
309 {
310 struct fp_instruction *inst = emit_op( p, op,
311 dest, destmask,
312 0, /* don't saturate? */
313 coord, /* arg 0? */
314 undef,
315 undef);
316
317 inst->TexSrcIdx = tex_idx;
318 inst->TexSrcUnit = tex_unit;
319
320 p->program->NumTexInstructions++;
321
322 if (coord.file != PROGRAM_INPUT &&
323 (coord.idx < FRAG_ATTRIB_TEX0 ||
324 coord.idx > FRAG_ATTRIB_TEX7)) {
325 p->program->NumTexIndirections++;
326 }
327
328 return dest;
329 }
330
331
332 static struct ureg register_const4f( struct texenv_fragment_program *p,
333 GLfloat s0,
334 GLfloat s1,
335 GLfloat s2,
336 GLfloat s3)
337 {
338 GLfloat values[4];
339 GLuint idx;
340 values[0] = s0;
341 values[1] = s1;
342 values[2] = s2;
343 values[3] = s3;
344 idx = _mesa_add_unnamed_constant( p->program->Parameters, values );
345 return make_ureg(PROGRAM_STATE_VAR, idx);
346 }
347
348 #define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
349 #define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
350 #define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
351 #define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
352
353
354
355
356
357
358
359
360 static void program_error( struct texenv_fragment_program *p, const char *msg )
361 {
362 fprintf(stderr, "%s\n", msg);
363 p->error = 1;
364 }
365
366
367 static GLuint translate_tex_src_bit( struct texenv_fragment_program *p,
368 GLuint bit )
369 {
370 switch (bit) {
371 case TEXTURE_1D_BIT: return TEXTURE_1D_INDEX;
372 case TEXTURE_2D_BIT: return TEXTURE_2D_INDEX;
373 case TEXTURE_RECT_BIT: return TEXTURE_RECT_INDEX;
374 case TEXTURE_3D_BIT: return TEXTURE_3D_INDEX;
375 case TEXTURE_CUBE_BIT: return TEXTURE_CUBE_INDEX;
376 default: program_error(p, "TexSrcBit"); return 0;
377 }
378 }
379
380
381 static struct ureg get_source( struct texenv_fragment_program *p,
382 GLenum src, GLuint unit )
383 {
384 switch (src) {
385 case GL_TEXTURE:
386 if (is_undef(p->src_texture)) {
387
388 GLuint dim = translate_tex_src_bit( p, p->ctx->Texture.Unit[unit]._ReallyEnabled);
389 struct ureg texcoord = register_input(p, FRAG_ATTRIB_TEX0+unit);
390 struct ureg tmp = get_tex_temp( p );
391
392 /* TODO: Use D0_MASK_XY where possible.
393 */
394 p->src_texture = emit_texld( p, FP_OPCODE_TXP,
395 tmp, WRITEMASK_XYZW,
396 unit, dim, texcoord );
397 }
398
399 return p->src_texture;
400
401 /* Crossbar: */
402 case GL_TEXTURE0:
403 case GL_TEXTURE1:
404 case GL_TEXTURE2:
405 case GL_TEXTURE3:
406 case GL_TEXTURE4:
407 case GL_TEXTURE5:
408 case GL_TEXTURE6:
409 case GL_TEXTURE7: {
410 return undef;
411 }
412
413 case GL_CONSTANT:
414 return register_param2(p, STATE_TEXENV_COLOR, unit);
415 case GL_PRIMARY_COLOR:
416 return register_input(p, FRAG_ATTRIB_COL0);
417 case GL_PREVIOUS:
418 default:
419 if (is_undef(p->src_previous))
420 return register_input(p, FRAG_ATTRIB_COL0);
421 else
422 return p->src_previous;
423 }
424 }
425
426
427 static struct ureg emit_combine_source( struct texenv_fragment_program *p,
428 GLuint mask,
429 GLuint unit,
430 GLenum source,
431 GLenum operand )
432 {
433 struct ureg arg, src, one;
434
435 src = get_source(p, source, unit);
436
437 switch (operand) {
438 case GL_ONE_MINUS_SRC_COLOR:
439 /* Get unused tmp,
440 * Emit tmp = 1.0 - arg.xyzw
441 */
442 arg = get_temp( p );
443 one = register_scalar_const(p, 1.0);
444 return emit_arith( p, FP_OPCODE_SUB, arg, mask, 0, one, src, undef);
445
446 case GL_SRC_ALPHA:
447 if (mask == WRITEMASK_W)
448 return src;
449 else
450 return swizzle1( src, W );
451 case GL_ONE_MINUS_SRC_ALPHA:
452 /* Get unused tmp,
453 * Emit tmp = 1.0 - arg.wwww
454 */
455 arg = get_temp( p );
456 one = register_scalar_const(p, 1.0);
457 return emit_arith( p, FP_OPCODE_SUB, arg, mask, 0,
458 one, swizzle1(src, W), undef);
459 case GL_SRC_COLOR:
460 default:
461 return src;
462 }
463 }
464
465
466
467 static int nr_args( GLenum mode )
468 {
469 switch (mode) {
470 case GL_REPLACE: return 1;
471 case GL_MODULATE: return 2;
472 case GL_ADD: return 2;
473 case GL_ADD_SIGNED: return 2;
474 case GL_INTERPOLATE: return 3;
475 case GL_SUBTRACT: return 2;
476 case GL_DOT3_RGB_EXT: return 2;
477 case GL_DOT3_RGBA_EXT: return 2;
478 case GL_DOT3_RGB: return 2;
479 case GL_DOT3_RGBA: return 2;
480 default: return 0;
481 }
482 }
483
484
485 static GLboolean args_match( struct gl_texture_unit *texUnit )
486 {
487 int i, nr = nr_args(texUnit->_CurrentCombine->ModeRGB);
488
489 for (i = 0 ; i < nr ; i++) {
490 if (texUnit->_CurrentCombine->SourceA[i] != texUnit->_CurrentCombine->SourceRGB[i])
491 return GL_FALSE;
492
493 switch(texUnit->_CurrentCombine->OperandA[i]) {
494 case GL_SRC_ALPHA:
495 switch(texUnit->_CurrentCombine->OperandRGB[i]) {
496 case GL_SRC_COLOR:
497 case GL_SRC_ALPHA:
498 break;
499 default:
500 return GL_FALSE;
501 }
502 break;
503 case GL_ONE_MINUS_SRC_ALPHA:
504 switch(texUnit->_CurrentCombine->OperandRGB[i]) {
505 case GL_ONE_MINUS_SRC_COLOR:
506 case GL_ONE_MINUS_SRC_ALPHA:
507 break;
508 default:
509 return GL_FALSE;
510 }
511 break;
512 default:
513 return GL_FALSE; /* impossible */
514 }
515 }
516
517 return GL_TRUE;
518 }
519
520
521 static struct ureg emit_combine( struct texenv_fragment_program *p,
522 struct ureg dest,
523 GLuint mask,
524 GLuint saturate,
525 GLuint unit,
526 GLenum mode,
527 const GLenum *source,
528 const GLenum *operand)
529 {
530 int nr = nr_args(mode);
531 struct ureg src[3];
532 struct ureg tmp;
533 int i;
534
535 for (i = 0; i < nr; i++)
536 src[i] = emit_combine_source( p, mask, unit, source[i], operand[i] );
537
538 switch (mode) {
539 case GL_REPLACE:
540 if (mask == WRITEMASK_XYZW && !saturate)
541 return src[0];
542 else
543 return emit_arith( p, FP_OPCODE_MOV, dest, mask, saturate, src[0], undef, undef );
544 case GL_MODULATE:
545 return emit_arith( p, FP_OPCODE_MUL, dest, mask, saturate,
546 src[0], src[1], undef );
547 case GL_ADD:
548 return emit_arith( p, FP_OPCODE_ADD, dest, mask, saturate,
549 src[0], src[1], undef );
550 case GL_ADD_SIGNED:
551 /* tmp = arg0 + arg1
552 * result = tmp + -.5
553 */
554 tmp = register_scalar_const(p, .5);
555 emit_arith( p, FP_OPCODE_ADD, dest, mask, 0, src[0], src[1], undef );
556 emit_arith( p, FP_OPCODE_SUB, dest, mask, saturate, dest, tmp, undef );
557 return dest;
558 case GL_INTERPOLATE:
559 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
560 */
561 return emit_arith( p, FP_OPCODE_LRP, dest, mask, saturate, src[2], src[0], src[1] );
562
563 case GL_SUBTRACT:
564 return emit_arith( p, FP_OPCODE_SUB, dest, mask, saturate, src[0], src[1], undef );
565
566 case GL_DOT3_RGBA:
567 case GL_DOT3_RGBA_EXT:
568 case GL_DOT3_RGB_EXT:
569 case GL_DOT3_RGB: {
570 struct ureg tmp0 = get_temp( p );
571 struct ureg tmp1 = get_temp( p );
572 struct ureg neg1 = register_scalar_const(p, -1);
573 struct ureg two = register_scalar_const(p, 2);
574
575 /* tmp0 = 2*src0 - 1
576 * tmp1 = 2*src1 - 1
577 *
578 * dst = tmp0 dot3 tmp1
579 */
580 emit_arith( p, FP_OPCODE_MAD, tmp0, WRITEMASK_XYZW, 0,
581 two, src[0], neg1);
582
583 if (memcmp(&src[0], &src[1], sizeof(struct ureg)) == 0)
584 tmp1 = tmp0;
585 else
586 emit_arith( p, FP_OPCODE_MAD, tmp1, WRITEMASK_XYZW, 0,
587 two, src[1], neg1);
588 emit_arith( p, FP_OPCODE_DP3, dest, mask, saturate, tmp0, tmp1, undef);
589 return dest;
590 }
591
592 default:
593 return src[0];
594 }
595 }
596
597 static struct ureg get_dest( struct texenv_fragment_program *p, int unit )
598 {
599 if (p->ctx->_TriangleCaps & DD_SEPARATE_SPECULAR)
600 return get_temp( p );
601 else if (unit != p->last_tex_stage)
602 return get_temp( p );
603 else
604 return make_ureg(PROGRAM_OUTPUT, FRAG_OUTPUT_COLR);
605 }
606
607
608
609 static struct ureg emit_texenv( struct texenv_fragment_program *p, int unit )
610 {
611 struct gl_texture_unit *texUnit = &p->ctx->Texture.Unit[unit];
612 GLuint saturate = (unit < p->last_tex_stage);
613 GLuint rgb_shift, alpha_shift;
614 struct ureg out, shift;
615 struct ureg dest = get_dest(p, unit);
616
617 if (!texUnit->_ReallyEnabled) {
618 return get_source(p, GL_PREVIOUS, 0);
619 }
620
621 switch (texUnit->_CurrentCombine->ModeRGB) {
622 case GL_DOT3_RGB_EXT:
623 alpha_shift = texUnit->_CurrentCombine->ScaleShiftA;
624 rgb_shift = 0;
625 break;
626
627 case GL_DOT3_RGBA_EXT:
628 alpha_shift = 0;
629 rgb_shift = 0;
630 break;
631
632 default:
633 rgb_shift = texUnit->_CurrentCombine->ScaleShiftRGB;
634 alpha_shift = texUnit->_CurrentCombine->ScaleShiftA;
635 break;
636 }
637
638
639 /* Emit the RGB and A combine ops
640 */
641 if (texUnit->_CurrentCombine->ModeRGB == texUnit->_CurrentCombine->ModeA &&
642 args_match( texUnit )) {
643 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
644 unit,
645 texUnit->_CurrentCombine->ModeRGB,
646 texUnit->_CurrentCombine->SourceRGB,
647 texUnit->_CurrentCombine->OperandRGB );
648 }
649 else if (texUnit->_CurrentCombine->ModeRGB == GL_DOT3_RGBA_EXT ||
650 texUnit->_CurrentCombine->ModeRGB == GL_DOT3_RGBA) {
651
652 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
653 unit,
654 texUnit->_CurrentCombine->ModeRGB,
655 texUnit->_CurrentCombine->SourceRGB,
656 texUnit->_CurrentCombine->OperandRGB );
657 }
658 else {
659 /* Need to do something to stop from re-emitting identical
660 * argument calculations here:
661 */
662 out = emit_combine( p, dest, WRITEMASK_XYZ, saturate,
663 unit,
664 texUnit->_CurrentCombine->ModeRGB,
665 texUnit->_CurrentCombine->SourceRGB,
666 texUnit->_CurrentCombine->OperandRGB );
667 out = emit_combine( p, dest, WRITEMASK_W, saturate,
668 unit,
669 texUnit->_CurrentCombine->ModeA,
670 texUnit->_CurrentCombine->SourceA,
671 texUnit->_CurrentCombine->OperandA );
672 }
673
674 /* Deal with the final shift:
675 */
676 if (alpha_shift || rgb_shift) {
677 if (rgb_shift == alpha_shift) {
678 shift = register_scalar_const(p, 1<<rgb_shift);
679 }
680 else {
681 shift = register_const2f(p, 1<<rgb_shift, 1<<alpha_shift);
682 shift = swizzle(shift,X,X,X,Y);
683 }
684 return emit_arith( p, FP_OPCODE_MUL, dest, WRITEMASK_XYZW,
685 saturate, out, shift, undef );
686 }
687 else
688 return out;
689 }
690
691 void _mesa_UpdateTexEnvProgram( GLcontext *ctx )
692 {
693 struct texenv_fragment_program p;
694 GLuint unit;
695 struct ureg cf, out;
696 GLuint db_NumInstructions = 0;
697 struct fp_instruction *db_Instructions = NULL;
698
699 if (ctx->FragmentProgram._Enabled)
700 return;
701
702 if (!ctx->_TexEnvProgram)
703 ctx->FragmentProgram._Current = ctx->_TexEnvProgram =
704 (struct fragment_program *)
705 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
706
707 _mesa_memset(&p, 0, sizeof(p));
708 p.ctx = ctx;
709 p.program = ctx->_TexEnvProgram;
710
711 if (ctx->Driver.ProgramStringNotify || DISASSEM) {
712 db_Instructions = p.program->Instructions;
713 db_NumInstructions = p.program->Base.NumInstructions;
714 p.program->Instructions = NULL;
715 }
716
717 if (!p.program->Instructions)
718 p.program->Instructions = MALLOC(sizeof(struct fp_instruction) * 100);
719
720 p.program->Base.NumInstructions = 0;
721 p.program->Base.Target = GL_FRAGMENT_PROGRAM_ARB;
722 p.program->NumTexIndirections = 1; /* correct? */
723 p.program->NumTexInstructions = 0;
724 p.program->NumAluInstructions = 0;
725 p.program->Base.String = 0;
726 p.program->Base.NumInstructions =
727 p.program->Base.NumTemporaries =
728 p.program->Base.NumParameters =
729 p.program->Base.NumAttributes = p.program->Base.NumAddressRegs = 0;
730
731 if (p.program->Parameters)
732 _mesa_free_parameters(p.program->Parameters);
733 else
734 p.program->Parameters = _mesa_new_parameter_list();
735
736 p.program->InputsRead = 0;
737 p.program->OutputsWritten = 0;
738
739 p.src_texture = undef;
740 p.src_previous = undef;
741 p.last_tex_stage = 0;
742 release_temps(&p);
743
744 if (ctx->Texture._EnabledUnits) {
745 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits ; unit++)
746 if (ctx->Texture.Unit[unit]._ReallyEnabled) {
747 p.last_tex_stage = unit;
748 }
749
750 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits; unit++)
751 if (ctx->Texture.Unit[unit]._ReallyEnabled) {
752 p.src_previous = emit_texenv( &p, unit );
753 p.src_texture = undef;
754 release_temps(&p); /* release all temps */
755 if (p.src_previous.file == PROGRAM_TEMPORARY)
756 p.temp_in_use |= 1 << p.src_previous.idx; /* except for this one */
757 }
758 }
759
760 cf = get_source( &p, GL_PREVIOUS, 0 );
761 out = make_ureg( PROGRAM_OUTPUT, FRAG_OUTPUT_COLR );
762
763 if (ctx->_TriangleCaps & DD_SEPARATE_SPECULAR) {
764 /* Emit specular add.
765 */
766 struct ureg s = register_input(&p, FRAG_ATTRIB_COL1);
767 emit_arith( &p, FP_OPCODE_ADD, out, WRITEMASK_XYZ, 0, cf, s, undef );
768 }
769 else if (memcmp(&cf, &out, sizeof(cf)) != 0) {
770 /* Will wind up in here if no texture enabled or a couple of
771 * other scenarios (GL_REPLACE for instance).
772 */
773 emit_arith( &p, FP_OPCODE_MOV, out, WRITEMASK_XYZW, 0, cf, undef, undef );
774 }
775
776 /* Finish up:
777 */
778 emit_arith( &p, FP_OPCODE_END, undef, WRITEMASK_XYZW, 0, undef, undef, undef);
779
780 if (ctx->Fog.Enabled)
781 p.program->FogOption = ctx->Fog.Mode;
782 else
783 p.program->FogOption = GL_NONE;
784
785 if (p.program->NumTexIndirections > ctx->Const.MaxFragmentProgramTexIndirections)
786 program_error(&p, "Exceeded max nr indirect texture lookups");
787
788 if (p.program->NumTexInstructions > ctx->Const.MaxFragmentProgramTexInstructions)
789 program_error(&p, "Exceeded max TEX instructions");
790
791 if (p.program->NumAluInstructions > ctx->Const.MaxFragmentProgramAluInstructions)
792 program_error(&p, "Exceeded max ALU instructions");
793
794
795 /* Notify driver the fragment program has (actually) changed.
796 */
797 if (ctx->Driver.ProgramStringNotify || DISASSEM) {
798 if (db_Instructions == NULL ||
799 db_NumInstructions != p.program->Base.NumInstructions ||
800 memcmp(db_Instructions, p.program->Instructions,
801 db_NumInstructions * sizeof(*db_Instructions)) != 0) {
802
803 if (ctx->Driver.ProgramStringNotify)
804 ctx->Driver.ProgramStringNotify( ctx, GL_FRAGMENT_PROGRAM_ARB,
805 &p.program->Base );
806
807 if (DISASSEM) {
808 _mesa_debug_fp_inst(p.program->NumTexInstructions + p.program->NumAluInstructions,
809 p.program->Instructions);
810 _mesa_printf("\n");
811 }
812 }
813
814 FREE(db_Instructions);
815 }
816 }
817
818