blob: 187526e6775fd24efb43e4b890736f70069c934e [file] [log] [blame]
Keith Whitwell15e75e02005-04-29 15:11:39 +00001/**************************************************************************
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
Keith Whitwell47b29f52005-05-04 11:44:44 +000040#define DISASSEM 0
Keith Whitwell15e75e02005-04-29 15:11:39 +000041
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 */
54struct 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
64const 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
Keith Whitwell15e75e02005-04-29 15:11:39 +000079/* State used to build the fragment program:
80 */
81struct texenv_fragment_program {
Keith Whitwell47b29f52005-05-04 11:44:44 +000082 struct fragment_program *program;
Keith Whitwell15e75e02005-04-29 15:11:39 +000083 GLcontext *ctx;
84
Keith Whitwell15e75e02005-04-29 15:11:39 +000085 GLuint temp_flag; /* Tracks temporary regs which are in
86 * use.
87 */
88
89
Keith Whitwell15e75e02005-04-29 15:11:39 +000090 GLboolean error;
91
92 struct ureg src_texture; /* Reg containing sampled texture color,
93 * else undef.
94 */
95
96 struct ureg src_previous; /* Reg containing color from previous
97 * stage. May need to be decl'd.
98 */
99
100 GLuint last_tex_stage; /* Number of last enabled texture unit */
101};
102
103
104
105static struct ureg make_ureg(GLuint file, GLuint idx)
106{
107 struct ureg reg;
108 reg.file = file;
109 reg.idx = idx;
110 reg.negatebase = 0;
111 reg.abs = 0;
112 reg.negateabs = 0;
113 reg.swz = SWIZZLE_NOOP;
114 reg.pad = 0;
115 return reg;
116}
117
118static struct ureg swizzle( struct ureg reg, int x, int y, int z, int w )
119{
120 reg.swz = MAKE_SWIZZLE4(GET_SWZ(reg.swz, x),
121 GET_SWZ(reg.swz, y),
122 GET_SWZ(reg.swz, z),
123 GET_SWZ(reg.swz, w));
124
125 return reg;
126}
127
128static struct ureg swizzle1( struct ureg reg, int x )
129{
130 return swizzle(reg, x, x, x, x);
131}
132
133static GLboolean is_undef( struct ureg reg )
134{
135 return reg.file == 0xf;
136}
137
138static struct ureg get_temp( struct texenv_fragment_program *p )
139{
140 int bit = ffs( ~p->temp_flag );
141 if (!bit) {
142 fprintf(stderr, "%s: out of temporaries\n", __FILE__);
143 exit(1);
144 }
145
146 p->temp_flag |= 1<<(bit-1);
147 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
148}
149
150
151static void release_temps( struct texenv_fragment_program *p )
152{
153 p->temp_flag = ~0x7;
154}
155
156
Keith Whitwell47b29f52005-05-04 11:44:44 +0000157static struct ureg register_param6( struct texenv_fragment_program *p,
158 GLint s0,
159 GLint s1,
160 GLint s2,
161 GLint s3,
162 GLint s4,
163 GLint s5)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000164{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000165 GLint tokens[6];
166 GLuint idx;
167 tokens[0] = s0;
168 tokens[1] = s1;
169 tokens[2] = s2;
170 tokens[3] = s3;
171 tokens[4] = s4;
172 tokens[5] = s5;
173 idx = _mesa_add_state_reference( p->program->Parameters, tokens );
174 return make_ureg(PROGRAM_STATE_VAR, idx);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000175}
176
Keith Whitwell47b29f52005-05-04 11:44:44 +0000177
178#define register_param1(p,s0) register_param6(p,s0,0,0,0,0,0)
179#define register_param2(p,s0,s1) register_param6(p,s0,s1,0,0,0,0)
180#define register_param3(p,s0,s1,s2) register_param6(p,s0,s1,s2,0,0,0)
181#define register_param4(p,s0,s1,s2,s3) register_param6(p,s0,s1,s2,s3,0,0)
182
183
184static struct ureg register_input( struct texenv_fragment_program *p, GLuint input )
185{
186 p->program->InputsRead |= (1<<input);
187 return make_ureg(PROGRAM_INPUT, input);
188}
189
190
Keith Whitwell15e75e02005-04-29 15:11:39 +0000191static void emit_arg( struct fp_src_register *reg,
192 struct ureg ureg )
193{
194 reg->File = ureg.file;
195 reg->Index = ureg.idx;
196 reg->Swizzle = ureg.swz;
197 reg->NegateBase = ureg.negatebase;
198 reg->Abs = ureg.abs;
199 reg->NegateAbs = ureg.negateabs;
200}
201
202static void emit_dst( struct fp_dst_register *dst,
203 struct ureg ureg, GLuint mask )
204{
205 dst->File = ureg.file;
206 dst->Index = ureg.idx;
207 dst->WriteMask = mask;
208 dst->CondMask = 0;
209 dst->CondSwizzle = 0;
210}
211
212static struct fp_instruction *
213emit_op(struct texenv_fragment_program *p,
214 GLuint op,
215 struct ureg dest,
216 GLuint mask,
217 GLuint saturate,
218 struct ureg src0,
219 struct ureg src1,
220 struct ureg src2 )
221{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000222 GLuint nr = p->program->Base.NumInstructions++;
223 struct fp_instruction *inst = &p->program->Instructions[nr];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000224
225 memset(inst, 0, sizeof(*inst));
226 inst->Opcode = op;
227
Keith Whitwell47b29f52005-05-04 11:44:44 +0000228 emit_arg( &inst->SrcReg[0], src0 );
229 emit_arg( &inst->SrcReg[1], src1 );
230 emit_arg( &inst->SrcReg[2], src2 );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000231
232 inst->Saturate = saturate;
233
234 emit_dst( &inst->DstReg, dest, mask );
235
236 return inst;
237}
238
239
240static struct ureg emit_arith( struct texenv_fragment_program *p,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000241 GLuint op,
242 struct ureg dest,
243 GLuint mask,
244 GLuint saturate,
245 struct ureg src0,
246 struct ureg src1,
247 struct ureg src2 )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000248{
249 emit_op(p, op, dest, mask, saturate, src0, src1, src2);
250
Keith Whitwell47b29f52005-05-04 11:44:44 +0000251 p->program->NumAluInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000252 return dest;
253}
254
255static struct ureg emit_texld( struct texenv_fragment_program *p,
256 GLuint op,
257 struct ureg dest,
258 GLuint destmask,
259 GLuint tex_unit,
260 GLuint tex_idx,
261 struct ureg coord )
262{
263 struct fp_instruction *inst = emit_op( p, op,
264 dest, destmask,
265 0, /* don't saturate? */
266 coord, /* arg 0? */
267 undef,
268 undef);
269
270 inst->TexSrcIdx = tex_idx;
271 inst->TexSrcUnit = tex_unit;
272
Keith Whitwell47b29f52005-05-04 11:44:44 +0000273 p->program->NumTexInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000274
275 if (coord.file != PROGRAM_INPUT &&
Keith Whitwell47b29f52005-05-04 11:44:44 +0000276 (coord.idx < FRAG_ATTRIB_TEX0 ||
277 coord.idx > FRAG_ATTRIB_TEX7)) {
278 p->program->NumTexIndirections++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000279 }
280
281 return dest;
282}
283
284
Keith Whitwell47b29f52005-05-04 11:44:44 +0000285static struct ureg register_const4f( struct texenv_fragment_program *p,
286 GLfloat s0,
287 GLfloat s1,
288 GLfloat s2,
289 GLfloat s3)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000290{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000291 GLfloat values[4];
292 GLuint idx;
293 values[0] = s0;
294 values[1] = s1;
295 values[2] = s2;
296 values[3] = s3;
297 idx = _mesa_add_unnamed_constant( p->program->Parameters, values );
298 return make_ureg(PROGRAM_STATE_VAR, idx);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000299}
300
Keith Whitwell47b29f52005-05-04 11:44:44 +0000301#define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
302#define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
303#define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000304
305
306
Keith Whitwell15e75e02005-04-29 15:11:39 +0000307
308
309
310
311
312static void program_error( struct texenv_fragment_program *p, const char *msg )
313{
314 fprintf(stderr, "%s\n", msg);
315 p->error = 1;
316}
317
318
319static GLuint translate_tex_src_bit( struct texenv_fragment_program *p,
320 GLuint bit )
321{
322 switch (bit) {
323 case TEXTURE_1D_BIT: return TEXTURE_1D_INDEX;
324 case TEXTURE_2D_BIT: return TEXTURE_2D_INDEX;
325 case TEXTURE_RECT_BIT: return TEXTURE_RECT_INDEX;
326 case TEXTURE_3D_BIT: return TEXTURE_3D_INDEX;
327 case TEXTURE_CUBE_BIT: return TEXTURE_CUBE_INDEX;
328 default: program_error(p, "TexSrcBit"); return 0;
329 }
330}
331
332
333static struct ureg get_source( struct texenv_fragment_program *p,
334 GLenum src, GLuint unit )
335{
336 switch (src) {
337 case GL_TEXTURE:
338 if (is_undef(p->src_texture)) {
339
340 GLuint dim = translate_tex_src_bit( p, p->ctx->Texture.Unit[unit]._ReallyEnabled);
Keith Whitwell47b29f52005-05-04 11:44:44 +0000341 struct ureg texcoord = register_input(p, FRAG_ATTRIB_TEX0+unit);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000342 struct ureg tmp = get_temp( p );
343
344 /* TODO: Use D0_MASK_XY where possible.
345 */
346 p->src_texture = emit_texld( p, FP_OPCODE_TXP,
347 tmp, WRITEMASK_XYZW,
348 unit, dim, texcoord );
349 }
350
351 return p->src_texture;
352
353 /* Crossbar: */
354 case GL_TEXTURE0:
355 case GL_TEXTURE1:
356 case GL_TEXTURE2:
357 case GL_TEXTURE3:
358 case GL_TEXTURE4:
359 case GL_TEXTURE5:
360 case GL_TEXTURE6:
361 case GL_TEXTURE7: {
362 return undef;
363 }
364
365 case GL_CONSTANT:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000366 return register_param2(p, STATE_TEXENV_COLOR, unit);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000367 case GL_PRIMARY_COLOR:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000368 return register_input(p, FRAG_ATTRIB_COL0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000369 case GL_PREVIOUS:
370 default:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000371 if (is_undef(p->src_previous))
372 return register_input(p, FRAG_ATTRIB_COL0);
373 else
374 return p->src_previous;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000375 }
376}
377
378
379static struct ureg emit_combine_source( struct texenv_fragment_program *p,
380 GLuint mask,
381 GLuint unit,
382 GLenum source,
383 GLenum operand )
384{
385 struct ureg arg, src, one;
386
387 src = get_source(p, source, unit);
388
389 switch (operand) {
390 case GL_ONE_MINUS_SRC_COLOR:
391 /* Get unused tmp,
392 * Emit tmp = 1.0 - arg.xyzw
393 */
394 arg = get_temp( p );
Keith Whitwell47b29f52005-05-04 11:44:44 +0000395 one = register_const1f(p, 1.0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000396 return emit_arith( p, FP_OPCODE_SUB, arg, mask, 0, one, src, undef);
397
398 case GL_SRC_ALPHA:
399 if (mask == WRITEMASK_W)
400 return src;
401 else
402 return swizzle1( src, W );
403 case GL_ONE_MINUS_SRC_ALPHA:
404 /* Get unused tmp,
405 * Emit tmp = 1.0 - arg.wwww
406 */
407 arg = get_temp( p );
Keith Whitwell47b29f52005-05-04 11:44:44 +0000408 one = register_const1f(p, 1.0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000409 return emit_arith( p, FP_OPCODE_SUB, arg, mask, 0,
410 one, swizzle1(src, W), undef);
411 case GL_SRC_COLOR:
412 default:
413 return src;
414 }
415}
416
417
418
419static int nr_args( GLenum mode )
420{
421 switch (mode) {
422 case GL_REPLACE: return 1;
423 case GL_MODULATE: return 2;
424 case GL_ADD: return 2;
425 case GL_ADD_SIGNED: return 2;
426 case GL_INTERPOLATE: return 3;
427 case GL_SUBTRACT: return 2;
428 case GL_DOT3_RGB_EXT: return 2;
429 case GL_DOT3_RGBA_EXT: return 2;
430 case GL_DOT3_RGB: return 2;
431 case GL_DOT3_RGBA: return 2;
432 default: return 0;
433 }
434}
435
436
437static GLboolean args_match( struct gl_texture_unit *texUnit )
438{
439 int i, nr = nr_args(texUnit->_CurrentCombine->ModeRGB);
440
441 for (i = 0 ; i < nr ; i++) {
442 if (texUnit->_CurrentCombine->SourceA[i] != texUnit->_CurrentCombine->SourceRGB[i])
443 return GL_FALSE;
444
445 switch(texUnit->_CurrentCombine->OperandA[i]) {
446 case GL_SRC_ALPHA:
447 switch(texUnit->_CurrentCombine->OperandRGB[i]) {
448 case GL_SRC_COLOR:
449 case GL_SRC_ALPHA:
450 break;
451 default:
452 return GL_FALSE;
453 }
454 break;
455 case GL_ONE_MINUS_SRC_ALPHA:
456 switch(texUnit->_CurrentCombine->OperandRGB[i]) {
457 case GL_ONE_MINUS_SRC_COLOR:
458 case GL_ONE_MINUS_SRC_ALPHA:
459 break;
460 default:
461 return GL_FALSE;
462 }
463 break;
464 default:
465 return GL_FALSE; /* impossible */
466 }
467 }
468
469 return GL_TRUE;
470}
471
472
473static struct ureg emit_combine( struct texenv_fragment_program *p,
474 struct ureg dest,
475 GLuint mask,
476 GLuint saturate,
477 GLuint unit,
478 GLenum mode,
479 const GLenum *source,
480 const GLenum *operand)
481{
482 int nr = nr_args(mode);
483 struct ureg src[3];
484 struct ureg tmp;
485 int i;
486
487 for (i = 0; i < nr; i++)
488 src[i] = emit_combine_source( p, mask, unit, source[i], operand[i] );
489
490 switch (mode) {
491 case GL_REPLACE:
492 if (mask == WRITEMASK_XYZW && !saturate)
493 return src[0];
494 else
495 return emit_arith( p, FP_OPCODE_MOV, dest, mask, saturate, src[0], undef, undef );
496 case GL_MODULATE:
497 return emit_arith( p, FP_OPCODE_MUL, dest, mask, saturate,
498 src[0], src[1], undef );
499 case GL_ADD:
500 return emit_arith( p, FP_OPCODE_ADD, dest, mask, saturate,
501 src[0], src[1], undef );
502 case GL_ADD_SIGNED:
503 /* tmp = arg0 + arg1
504 * result = tmp + -.5
505 */
Keith Whitwell47b29f52005-05-04 11:44:44 +0000506 tmp = register_const1f(p, .5);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000507 tmp = swizzle1(tmp,X);
508 emit_arith( p, FP_OPCODE_ADD, dest, mask, 0, src[0], src[1], undef );
509 emit_arith( p, FP_OPCODE_SUB, dest, mask, saturate, dest, tmp, undef );
510 return dest;
511 case GL_INTERPOLATE:
512 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
513 */
514 return emit_arith( p, FP_OPCODE_LRP, dest, mask, saturate, src[2], src[0], src[1] );
515
516 case GL_SUBTRACT:
517 return emit_arith( p, FP_OPCODE_SUB, dest, mask, saturate, src[0], src[1], undef );
518
519 case GL_DOT3_RGBA:
520 case GL_DOT3_RGBA_EXT:
521 case GL_DOT3_RGB_EXT:
522 case GL_DOT3_RGB: {
523 struct ureg tmp0 = get_temp( p );
524 struct ureg tmp1 = get_temp( p );
Keith Whitwell47b29f52005-05-04 11:44:44 +0000525 struct ureg neg1 = register_const1f(p, -1);
526 struct ureg two = register_const1f(p, 2);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000527
528 /* tmp0 = 2*src0 - 1
529 * tmp1 = 2*src1 - 1
530 *
531 * dst = tmp0 dot3 tmp1
532 */
533 emit_arith( p, FP_OPCODE_MAD, tmp0, WRITEMASK_XYZW, 0,
534 two, src[0], neg1);
535
536 if (memcmp(&src[0], &src[1], sizeof(struct ureg)) == 0)
537 tmp1 = tmp0;
538 else
539 emit_arith( p, FP_OPCODE_MAD, tmp1, WRITEMASK_XYZW, 0,
540 two, src[1], neg1);
541 emit_arith( p, FP_OPCODE_DP3, dest, mask, saturate, tmp0, tmp1, undef);
542 return dest;
543 }
544
545 default:
546 return src[0];
547 }
548}
549
550static struct ureg get_dest( struct texenv_fragment_program *p, int unit )
551{
552 if (p->ctx->_TriangleCaps & DD_SEPARATE_SPECULAR)
553 return get_temp( p );
554 else if (unit != p->last_tex_stage)
555 return get_temp( p );
556 else
Keith Whitwell47b29f52005-05-04 11:44:44 +0000557 return make_ureg(PROGRAM_OUTPUT, FRAG_OUTPUT_COLR);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000558}
559
560
561
562static struct ureg emit_texenv( struct texenv_fragment_program *p, int unit )
563{
564 struct gl_texture_unit *texUnit = &p->ctx->Texture.Unit[unit];
565 GLuint saturate = (unit < p->last_tex_stage);
566 GLuint rgb_shift, alpha_shift;
567 struct ureg out, shift;
568 struct ureg dest = get_dest(p, unit);
569
570 if (!texUnit->_ReallyEnabled) {
571 return get_source(p, GL_PREVIOUS, 0);
572 }
573
574 switch (texUnit->_CurrentCombine->ModeRGB) {
575 case GL_DOT3_RGB_EXT:
576 alpha_shift = texUnit->_CurrentCombine->ScaleShiftA;
577 rgb_shift = 0;
578 break;
579
580 case GL_DOT3_RGBA_EXT:
581 alpha_shift = 0;
582 rgb_shift = 0;
583 break;
584
585 default:
586 rgb_shift = texUnit->_CurrentCombine->ScaleShiftRGB;
587 alpha_shift = texUnit->_CurrentCombine->ScaleShiftA;
588 break;
589 }
590
591
592 /* Emit the RGB and A combine ops
593 */
594 if (texUnit->_CurrentCombine->ModeRGB == texUnit->_CurrentCombine->ModeA &&
595 args_match( texUnit )) {
596 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
597 unit,
598 texUnit->_CurrentCombine->ModeRGB,
599 texUnit->_CurrentCombine->SourceRGB,
600 texUnit->_CurrentCombine->OperandRGB );
601 }
602 else if (texUnit->_CurrentCombine->ModeRGB == GL_DOT3_RGBA_EXT ||
603 texUnit->_CurrentCombine->ModeRGB == GL_DOT3_RGBA) {
604
605 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
606 unit,
607 texUnit->_CurrentCombine->ModeRGB,
608 texUnit->_CurrentCombine->SourceRGB,
609 texUnit->_CurrentCombine->OperandRGB );
610 }
611 else {
612 /* Need to do something to stop from re-emitting identical
613 * argument calculations here:
614 */
615 out = emit_combine( p, dest, WRITEMASK_XYZ, saturate,
616 unit,
617 texUnit->_CurrentCombine->ModeRGB,
618 texUnit->_CurrentCombine->SourceRGB,
619 texUnit->_CurrentCombine->OperandRGB );
620 out = emit_combine( p, dest, WRITEMASK_W, saturate,
621 unit,
622 texUnit->_CurrentCombine->ModeA,
623 texUnit->_CurrentCombine->SourceA,
624 texUnit->_CurrentCombine->OperandA );
625 }
626
627 /* Deal with the final shift:
628 */
629 if (alpha_shift || rgb_shift) {
630 if (rgb_shift == alpha_shift) {
Keith Whitwell47b29f52005-05-04 11:44:44 +0000631 shift = register_const1f(p, 1<<rgb_shift);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000632 shift = swizzle1(shift,X);
633 }
634 else {
Keith Whitwell47b29f52005-05-04 11:44:44 +0000635 shift = register_const2f(p, 1<<rgb_shift, 1<<alpha_shift);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000636 shift = swizzle(shift,X,X,X,Y);
637 }
638 return emit_arith( p, FP_OPCODE_MUL, dest, WRITEMASK_XYZW,
639 saturate, out, shift, undef );
640 }
641 else
642 return out;
643}
644
645void _mesa_UpdateTexEnvProgram( GLcontext *ctx )
646{
647 struct texenv_fragment_program p;
648 GLuint unit;
649 struct ureg cf, out;
650
Keith Whitwell47b29f52005-05-04 11:44:44 +0000651 if (ctx->FragmentProgram._Enabled)
652 return;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000653
Keith Whitwell47b29f52005-05-04 11:44:44 +0000654 p.ctx = ctx;
655 p.program = &ctx->_TexEnvProgram;
656
657 if (p.program->Instructions == NULL) {
658 p.program->Instructions = MALLOC(sizeof(struct fp_instruction) * 100);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000659 }
660
Keith Whitwell47b29f52005-05-04 11:44:44 +0000661 p.program->Base.NumInstructions = 0;
662 p.program->NumTexIndirections = 1; /* correct? */
663 p.program->NumTexInstructions = 0;
664 p.program->NumAluInstructions = 0;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000665
666 p.src_texture = undef;
Keith Whitwell47b29f52005-05-04 11:44:44 +0000667 p.src_previous = undef;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000668 p.last_tex_stage = 0;
669
670 if (ctx->Texture._EnabledUnits) {
671 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits ; unit++)
672 if (ctx->Texture.Unit[unit]._ReallyEnabled) {
673 p.last_tex_stage = unit;
674 }
675
676 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits; unit++)
677 if (ctx->Texture.Unit[unit]._ReallyEnabled) {
678 p.src_previous = emit_texenv( &p, unit );
679 p.src_texture = undef;
680 p.temp_flag = 0xffff000;
681 p.temp_flag |= 1 << p.src_previous.idx;
682 }
683 }
684
685 cf = get_source( &p, GL_PREVIOUS, 0 );
Keith Whitwell47b29f52005-05-04 11:44:44 +0000686 out = make_ureg( PROGRAM_OUTPUT, FRAG_OUTPUT_COLR );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000687
688 if (ctx->_TriangleCaps & DD_SEPARATE_SPECULAR) {
689 /* Emit specular add.
690 */
Keith Whitwell47b29f52005-05-04 11:44:44 +0000691 struct ureg s = register_input(&p, FRAG_ATTRIB_COL1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000692 emit_arith( &p, FP_OPCODE_ADD, out, WRITEMASK_XYZ, 0, cf, s, undef );
693 }
694 else if (memcmp(&cf, &out, sizeof(cf)) != 0) {
695 /* Will wind up in here if no texture enabled or a couple of
696 * other scenarios (GL_REPLACE for instance).
697 */
698 emit_arith( &p, FP_OPCODE_MOV, out, WRITEMASK_XYZW, 0, cf, undef, undef );
699 }
700
Keith Whitwell47b29f52005-05-04 11:44:44 +0000701 /* Finish up:
702 */
703 emit_arith( &p, FP_OPCODE_END, undef, WRITEMASK_XYZW, 0, undef, undef, undef);
704
705
706 if (p.program->NumTexIndirections > ctx->Const.MaxFragmentProgramTexIndirections)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000707 program_error(&p, "Exceeded max nr indirect texture lookups");
708
Keith Whitwell47b29f52005-05-04 11:44:44 +0000709 if (p.program->NumTexInstructions > ctx->Const.MaxFragmentProgramTexInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000710 program_error(&p, "Exceeded max TEX instructions");
711
Keith Whitwell47b29f52005-05-04 11:44:44 +0000712 if (p.program->NumAluInstructions > ctx->Const.MaxFragmentProgramAluInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000713 program_error(&p, "Exceeded max ALU instructions");
714
715#if DISASSEM
Keith Whitwell47b29f52005-05-04 11:44:44 +0000716 _mesa_debug_fp_inst(p.program->NumTexInstructions + p.program->NumAluInstructions,
717 p.program->Instructions);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000718 _mesa_printf("\n");
719#endif
720}
721
722