blob: 92848d346aead9c55f760099019f0199d6ab5683 [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 Whitwell8b88f622005-05-10 11:39:50 +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 Whitwell93cd9232005-05-11 08:30:23 +000085 GLuint temp_used_for_txp; /* Temps which have been the result of a texture
Keith Whitwellecb6bfc2005-05-10 08:58:44 +000086 * operation.
87 */
88
Keith Whitwell93cd9232005-05-11 08:30:23 +000089 GLuint temp_in_use; /* Tracks temporary regs which are in
Keith Whitwell15e75e02005-04-29 15:11:39 +000090 * use.
91 */
92
93
Keith Whitwell15e75e02005-04-29 15:11:39 +000094 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
109static 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
122static 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
132static struct ureg swizzle1( struct ureg reg, int x )
133{
134 return swizzle(reg, x, x, x, x);
135}
136
137static GLboolean is_undef( struct ureg reg )
138{
139 return reg.file == 0xf;
140}
141
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000142
Keith Whitwell15e75e02005-04-29 15:11:39 +0000143static struct ureg get_temp( struct texenv_fragment_program *p )
144{
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000145 int bit;
146
Keith Whitwell93cd9232005-05-11 08:30:23 +0000147 /* First try and reuse temps which have been used for texture
148 * results:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000149 */
Keith Whitwell93cd9232005-05-11 08:30:23 +0000150 bit = ffs( ~p->temp_in_use & p->temp_used_for_txp );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000151
152 /* Then any unused temporary:
153 */
154 if (!bit)
Keith Whitwell93cd9232005-05-11 08:30:23 +0000155 bit = ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000156
Keith Whitwell15e75e02005-04-29 15:11:39 +0000157 if (!bit) {
158 fprintf(stderr, "%s: out of temporaries\n", __FILE__);
159 exit(1);
160 }
161
Keith Whitwell93cd9232005-05-11 08:30:23 +0000162 p->temp_in_use |= 1<<(bit-1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000163 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
164}
165
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000166static struct ureg get_tex_temp( struct texenv_fragment_program *p )
167{
168 int bit;
169
Keith Whitwell93cd9232005-05-11 08:30:23 +0000170 /* First try to find availble temp not previously used as a texture
171 * result:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000172 */
Keith Whitwell93cd9232005-05-11 08:30:23 +0000173 bit = ffs( ~p->temp_in_use & ~p->temp_used_for_txp );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000174
175 /* Then any unused temporary:
176 */
Keith Whitwell586f2c52005-05-10 10:25:16 +0000177 if (!bit) {
Keith Whitwell93cd9232005-05-11 08:30:23 +0000178 bit = ffs( ~p->temp_in_use );
Keith Whitwell586f2c52005-05-10 10:25:16 +0000179 p->program->NumTexIndirections++;
180 }
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000181
182 if (!bit) {
183 fprintf(stderr, "%s: out of temporaries\n", __FILE__);
184 exit(1);
185 }
186
Keith Whitwell93cd9232005-05-11 08:30:23 +0000187 p->temp_in_use |= 1<<(bit-1);
188 p->temp_used_for_txp |= 1<<(bit-1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000189 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
190}
191
Keith Whitwell15e75e02005-04-29 15:11:39 +0000192
193static void release_temps( struct texenv_fragment_program *p )
194{
Keith Whitwell93cd9232005-05-11 08:30:23 +0000195 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);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000201}
202
203
Keith Whitwell47b29f52005-05-04 11:44:44 +0000204static 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)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000211{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000212 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);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000222}
223
Keith Whitwell47b29f52005-05-04 11:44:44 +0000224
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
231static 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
Keith Whitwell15e75e02005-04-29 15:11:39 +0000238static 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
249static 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
259static struct fp_instruction *
260emit_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{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000269 GLuint nr = p->program->Base.NumInstructions++;
270 struct fp_instruction *inst = &p->program->Instructions[nr];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000271
272 memset(inst, 0, sizeof(*inst));
273 inst->Opcode = op;
274
Keith Whitwell47b29f52005-05-04 11:44:44 +0000275 emit_arg( &inst->SrcReg[0], src0 );
276 emit_arg( &inst->SrcReg[1], src1 );
277 emit_arg( &inst->SrcReg[2], src2 );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000278
279 inst->Saturate = saturate;
280
281 emit_dst( &inst->DstReg, dest, mask );
282
283 return inst;
284}
285
286
287static struct ureg emit_arith( struct texenv_fragment_program *p,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000288 GLuint op,
289 struct ureg dest,
290 GLuint mask,
291 GLuint saturate,
292 struct ureg src0,
293 struct ureg src1,
294 struct ureg src2 )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000295{
296 emit_op(p, op, dest, mask, saturate, src0, src1, src2);
297
Keith Whitwell47b29f52005-05-04 11:44:44 +0000298 p->program->NumAluInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000299 return dest;
300}
301
302static 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
Keith Whitwell47b29f52005-05-04 11:44:44 +0000320 p->program->NumTexInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000321
322 if (coord.file != PROGRAM_INPUT &&
Keith Whitwell47b29f52005-05-04 11:44:44 +0000323 (coord.idx < FRAG_ATTRIB_TEX0 ||
324 coord.idx > FRAG_ATTRIB_TEX7)) {
325 p->program->NumTexIndirections++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000326 }
327
328 return dest;
329}
330
331
Keith Whitwell47b29f52005-05-04 11:44:44 +0000332static struct ureg register_const4f( struct texenv_fragment_program *p,
333 GLfloat s0,
334 GLfloat s1,
335 GLfloat s2,
336 GLfloat s3)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000337{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000338 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);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000346}
347
Keith Whitwell47b29f52005-05-04 11:44:44 +0000348#define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
349#define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
350#define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000351
352
353
Keith Whitwell15e75e02005-04-29 15:11:39 +0000354
355
356
357
358
359static void program_error( struct texenv_fragment_program *p, const char *msg )
360{
361 fprintf(stderr, "%s\n", msg);
362 p->error = 1;
363}
364
365
366static GLuint translate_tex_src_bit( struct texenv_fragment_program *p,
367 GLuint bit )
368{
369 switch (bit) {
370 case TEXTURE_1D_BIT: return TEXTURE_1D_INDEX;
371 case TEXTURE_2D_BIT: return TEXTURE_2D_INDEX;
372 case TEXTURE_RECT_BIT: return TEXTURE_RECT_INDEX;
373 case TEXTURE_3D_BIT: return TEXTURE_3D_INDEX;
374 case TEXTURE_CUBE_BIT: return TEXTURE_CUBE_INDEX;
375 default: program_error(p, "TexSrcBit"); return 0;
376 }
377}
378
379
380static struct ureg get_source( struct texenv_fragment_program *p,
381 GLenum src, GLuint unit )
382{
383 switch (src) {
384 case GL_TEXTURE:
385 if (is_undef(p->src_texture)) {
386
387 GLuint dim = translate_tex_src_bit( p, p->ctx->Texture.Unit[unit]._ReallyEnabled);
Keith Whitwell47b29f52005-05-04 11:44:44 +0000388 struct ureg texcoord = register_input(p, FRAG_ATTRIB_TEX0+unit);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000389 struct ureg tmp = get_tex_temp( p );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000390
391 /* TODO: Use D0_MASK_XY where possible.
392 */
393 p->src_texture = emit_texld( p, FP_OPCODE_TXP,
394 tmp, WRITEMASK_XYZW,
395 unit, dim, texcoord );
396 }
397
398 return p->src_texture;
399
400 /* Crossbar: */
401 case GL_TEXTURE0:
402 case GL_TEXTURE1:
403 case GL_TEXTURE2:
404 case GL_TEXTURE3:
405 case GL_TEXTURE4:
406 case GL_TEXTURE5:
407 case GL_TEXTURE6:
408 case GL_TEXTURE7: {
409 return undef;
410 }
411
412 case GL_CONSTANT:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000413 return register_param2(p, STATE_TEXENV_COLOR, unit);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000414 case GL_PRIMARY_COLOR:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000415 return register_input(p, FRAG_ATTRIB_COL0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000416 case GL_PREVIOUS:
417 default:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000418 if (is_undef(p->src_previous))
419 return register_input(p, FRAG_ATTRIB_COL0);
420 else
421 return p->src_previous;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000422 }
423}
424
425
426static struct ureg emit_combine_source( struct texenv_fragment_program *p,
427 GLuint mask,
428 GLuint unit,
429 GLenum source,
430 GLenum operand )
431{
432 struct ureg arg, src, one;
433
434 src = get_source(p, source, unit);
435
436 switch (operand) {
437 case GL_ONE_MINUS_SRC_COLOR:
438 /* Get unused tmp,
439 * Emit tmp = 1.0 - arg.xyzw
440 */
441 arg = get_temp( p );
Keith Whitwell47b29f52005-05-04 11:44:44 +0000442 one = register_const1f(p, 1.0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000443 return emit_arith( p, FP_OPCODE_SUB, arg, mask, 0, one, src, undef);
444
445 case GL_SRC_ALPHA:
446 if (mask == WRITEMASK_W)
447 return src;
448 else
449 return swizzle1( src, W );
450 case GL_ONE_MINUS_SRC_ALPHA:
451 /* Get unused tmp,
452 * Emit tmp = 1.0 - arg.wwww
453 */
454 arg = get_temp( p );
Keith Whitwell47b29f52005-05-04 11:44:44 +0000455 one = register_const1f(p, 1.0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000456 return emit_arith( p, FP_OPCODE_SUB, arg, mask, 0,
457 one, swizzle1(src, W), undef);
458 case GL_SRC_COLOR:
459 default:
460 return src;
461 }
462}
463
464
465
466static int nr_args( GLenum mode )
467{
468 switch (mode) {
469 case GL_REPLACE: return 1;
470 case GL_MODULATE: return 2;
471 case GL_ADD: return 2;
472 case GL_ADD_SIGNED: return 2;
473 case GL_INTERPOLATE: return 3;
474 case GL_SUBTRACT: return 2;
475 case GL_DOT3_RGB_EXT: return 2;
476 case GL_DOT3_RGBA_EXT: return 2;
477 case GL_DOT3_RGB: return 2;
478 case GL_DOT3_RGBA: return 2;
479 default: return 0;
480 }
481}
482
483
484static GLboolean args_match( struct gl_texture_unit *texUnit )
485{
486 int i, nr = nr_args(texUnit->_CurrentCombine->ModeRGB);
487
488 for (i = 0 ; i < nr ; i++) {
489 if (texUnit->_CurrentCombine->SourceA[i] != texUnit->_CurrentCombine->SourceRGB[i])
490 return GL_FALSE;
491
492 switch(texUnit->_CurrentCombine->OperandA[i]) {
493 case GL_SRC_ALPHA:
494 switch(texUnit->_CurrentCombine->OperandRGB[i]) {
495 case GL_SRC_COLOR:
496 case GL_SRC_ALPHA:
497 break;
498 default:
499 return GL_FALSE;
500 }
501 break;
502 case GL_ONE_MINUS_SRC_ALPHA:
503 switch(texUnit->_CurrentCombine->OperandRGB[i]) {
504 case GL_ONE_MINUS_SRC_COLOR:
505 case GL_ONE_MINUS_SRC_ALPHA:
506 break;
507 default:
508 return GL_FALSE;
509 }
510 break;
511 default:
512 return GL_FALSE; /* impossible */
513 }
514 }
515
516 return GL_TRUE;
517}
518
519
520static struct ureg emit_combine( struct texenv_fragment_program *p,
521 struct ureg dest,
522 GLuint mask,
523 GLuint saturate,
524 GLuint unit,
525 GLenum mode,
526 const GLenum *source,
527 const GLenum *operand)
528{
529 int nr = nr_args(mode);
530 struct ureg src[3];
531 struct ureg tmp;
532 int i;
533
534 for (i = 0; i < nr; i++)
535 src[i] = emit_combine_source( p, mask, unit, source[i], operand[i] );
536
537 switch (mode) {
538 case GL_REPLACE:
539 if (mask == WRITEMASK_XYZW && !saturate)
540 return src[0];
541 else
542 return emit_arith( p, FP_OPCODE_MOV, dest, mask, saturate, src[0], undef, undef );
543 case GL_MODULATE:
544 return emit_arith( p, FP_OPCODE_MUL, dest, mask, saturate,
545 src[0], src[1], undef );
546 case GL_ADD:
547 return emit_arith( p, FP_OPCODE_ADD, dest, mask, saturate,
548 src[0], src[1], undef );
549 case GL_ADD_SIGNED:
550 /* tmp = arg0 + arg1
551 * result = tmp + -.5
552 */
Keith Whitwell47b29f52005-05-04 11:44:44 +0000553 tmp = register_const1f(p, .5);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000554 tmp = swizzle1(tmp,X);
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 );
Keith Whitwell47b29f52005-05-04 11:44:44 +0000572 struct ureg neg1 = register_const1f(p, -1);
573 struct ureg two = register_const1f(p, 2);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000574
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
597static 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
Keith Whitwell47b29f52005-05-04 11:44:44 +0000604 return make_ureg(PROGRAM_OUTPUT, FRAG_OUTPUT_COLR);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000605}
606
607
608
609static 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) {
Keith Whitwell47b29f52005-05-04 11:44:44 +0000678 shift = register_const1f(p, 1<<rgb_shift);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000679 shift = swizzle1(shift,X);
680 }
681 else {
Keith Whitwell47b29f52005-05-04 11:44:44 +0000682 shift = register_const2f(p, 1<<rgb_shift, 1<<alpha_shift);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000683 shift = swizzle(shift,X,X,X,Y);
684 }
685 return emit_arith( p, FP_OPCODE_MUL, dest, WRITEMASK_XYZW,
686 saturate, out, shift, undef );
687 }
688 else
689 return out;
690}
691
692void _mesa_UpdateTexEnvProgram( GLcontext *ctx )
693{
694 struct texenv_fragment_program p;
695 GLuint unit;
696 struct ureg cf, out;
Keith Whitwelldbeea252005-05-10 13:57:50 +0000697 GLuint db_NumInstructions;
698 struct fp_instruction *db_Instructions;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000699
Keith Whitwell47b29f52005-05-04 11:44:44 +0000700 if (ctx->FragmentProgram._Enabled)
701 return;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000702
Keith Whitwell8b88f622005-05-10 11:39:50 +0000703 if (!ctx->_TexEnvProgram)
704 ctx->FragmentProgram._Current = ctx->_TexEnvProgram =
705 (struct fragment_program *)
706 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
Keith Whitwell276330b2005-05-09 17:58:13 +0000707
Keith Whitwell47b29f52005-05-04 11:44:44 +0000708 p.ctx = ctx;
Keith Whitwell276330b2005-05-09 17:58:13 +0000709 p.program = ctx->_TexEnvProgram;
Keith Whitwell47b29f52005-05-04 11:44:44 +0000710
Keith Whitwelldbeea252005-05-10 13:57:50 +0000711 db_Instructions = p.program->Instructions;
712 db_NumInstructions = p.program->Base.NumInstructions;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000713
Keith Whitwelldbeea252005-05-10 13:57:50 +0000714 p.program->Instructions = MALLOC(sizeof(struct fp_instruction) * 100);
Keith Whitwell47b29f52005-05-04 11:44:44 +0000715 p.program->Base.NumInstructions = 0;
Keith Whitwell276330b2005-05-09 17:58:13 +0000716 p.program->Base.Target = GL_FRAGMENT_PROGRAM_ARB;
Keith Whitwell47b29f52005-05-04 11:44:44 +0000717 p.program->NumTexIndirections = 1; /* correct? */
718 p.program->NumTexInstructions = 0;
719 p.program->NumAluInstructions = 0;
Keith Whitwell9ca88152005-05-10 09:56:02 +0000720 p.program->Base.String = 0;
721 p.program->Base.NumInstructions =
722 p.program->Base.NumTemporaries =
723 p.program->Base.NumParameters =
724 p.program->Base.NumAttributes = p.program->Base.NumAddressRegs = 0;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000725
Keith Whitwell9ca88152005-05-10 09:56:02 +0000726 if (p.program->Parameters)
Keith Whitwelldbeea252005-05-10 13:57:50 +0000727 _mesa_free_parameters(p.program->Parameters);
728 else
729 p.program->Parameters = _mesa_new_parameter_list();
730
Keith Whitwell9ca88152005-05-10 09:56:02 +0000731 p.program->InputsRead = 0;
732 p.program->OutputsWritten = 0;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000733
734 p.src_texture = undef;
Keith Whitwell47b29f52005-05-04 11:44:44 +0000735 p.src_previous = undef;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000736 p.last_tex_stage = 0;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000737 release_temps(&p);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000738
739 if (ctx->Texture._EnabledUnits) {
740 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits ; unit++)
741 if (ctx->Texture.Unit[unit]._ReallyEnabled) {
742 p.last_tex_stage = unit;
743 }
744
745 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits; unit++)
746 if (ctx->Texture.Unit[unit]._ReallyEnabled) {
747 p.src_previous = emit_texenv( &p, unit );
748 p.src_texture = undef;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000749 release_temps(&p); /* release all temps */
750 if (p.src_previous.file == PROGRAM_TEMPORARY)
751 p.temp_in_use |= 1 << p.src_previous.idx; /* except for this one */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000752 }
753 }
754
755 cf = get_source( &p, GL_PREVIOUS, 0 );
Keith Whitwell47b29f52005-05-04 11:44:44 +0000756 out = make_ureg( PROGRAM_OUTPUT, FRAG_OUTPUT_COLR );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000757
758 if (ctx->_TriangleCaps & DD_SEPARATE_SPECULAR) {
759 /* Emit specular add.
760 */
Keith Whitwell47b29f52005-05-04 11:44:44 +0000761 struct ureg s = register_input(&p, FRAG_ATTRIB_COL1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000762 emit_arith( &p, FP_OPCODE_ADD, out, WRITEMASK_XYZ, 0, cf, s, undef );
763 }
764 else if (memcmp(&cf, &out, sizeof(cf)) != 0) {
765 /* Will wind up in here if no texture enabled or a couple of
766 * other scenarios (GL_REPLACE for instance).
767 */
768 emit_arith( &p, FP_OPCODE_MOV, out, WRITEMASK_XYZW, 0, cf, undef, undef );
769 }
770
Keith Whitwell47b29f52005-05-04 11:44:44 +0000771 /* Finish up:
772 */
773 emit_arith( &p, FP_OPCODE_END, undef, WRITEMASK_XYZW, 0, undef, undef, undef);
774
Keith Whitwell276330b2005-05-09 17:58:13 +0000775 if (ctx->Fog.Enabled)
776 p.program->FogOption = ctx->Fog.Mode;
777 else
778 p.program->FogOption = GL_NONE;
Keith Whitwell47b29f52005-05-04 11:44:44 +0000779
780 if (p.program->NumTexIndirections > ctx->Const.MaxFragmentProgramTexIndirections)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000781 program_error(&p, "Exceeded max nr indirect texture lookups");
782
Keith Whitwell47b29f52005-05-04 11:44:44 +0000783 if (p.program->NumTexInstructions > ctx->Const.MaxFragmentProgramTexInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000784 program_error(&p, "Exceeded max TEX instructions");
785
Keith Whitwell47b29f52005-05-04 11:44:44 +0000786 if (p.program->NumAluInstructions > ctx->Const.MaxFragmentProgramAluInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000787 program_error(&p, "Exceeded max ALU instructions");
788
789#if DISASSEM
Keith Whitwell47b29f52005-05-04 11:44:44 +0000790 _mesa_debug_fp_inst(p.program->NumTexInstructions + p.program->NumAluInstructions,
791 p.program->Instructions);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000792 _mesa_printf("\n");
793#endif
Keith Whitwell8b88f622005-05-10 11:39:50 +0000794
Keith Whitwelldbeea252005-05-10 13:57:50 +0000795 /* Notify driver the fragment program has (actually) changed.
Keith Whitwell8b88f622005-05-10 11:39:50 +0000796 */
Keith Whitwelldbeea252005-05-10 13:57:50 +0000797 if (db_Instructions == NULL ||
798 db_NumInstructions != p.program->Base.NumInstructions ||
799 memcmp(db_Instructions, p.program->Instructions,
800 db_NumInstructions * sizeof(*db_Instructions)) != 0) {
Keith Whitwelldbeea252005-05-10 13:57:50 +0000801 ctx->Driver.ProgramStringNotify( ctx, GL_FRAGMENT_PROGRAM_ARB,
802 &p.program->Base );
803 }
804
805 FREE(db_Instructions);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000806}
807
808