blob: ee75303a29b9557619829a0a7ce55e91569d1842 [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
Keith Whitwell15e75e02005-04-29 15:11:39 +000028#include "glheader.h"
29#include "macros.h"
30#include "enums.h"
31#include "texenvprogram.h"
32
33#include "shader/program.h"
34#include "shader/nvfragprog.h"
35#include "shader/arbfragparse.h"
36
37
Keith Whitwell269e3892005-05-12 10:28:43 +000038#define DISASSEM (MESA_VERBOSE & VERBOSE_DISASSEM)
Keith Whitwell15e75e02005-04-29 15:11:39 +000039
40/* Use uregs to represent registers internally, translate to Mesa's
41 * expected formats on emit.
42 *
43 * NOTE: These are passed by value extensively in this file rather
44 * than as usual by pointer reference. If this disturbs you, try
45 * remembering they are just 32bits in size.
46 *
47 * GCC is smart enough to deal with these dword-sized structures in
48 * much the same way as if I had defined them as dwords and was using
49 * macros to access and set the fields. This is much nicer and easier
50 * to evolve.
51 */
52struct ureg {
53 GLuint file:4;
54 GLuint idx:8;
55 GLuint negatebase:1;
56 GLuint abs:1;
57 GLuint negateabs:1;
58 GLuint swz:12;
59 GLuint pad:5;
60};
61
62const static struct ureg undef = {
63 ~0,
64 ~0,
65 0,
66 0,
67 0,
68 0,
69 0
70};
71
72#define X 0
73#define Y 1
74#define Z 2
75#define W 3
76
Keith Whitwell15e75e02005-04-29 15:11:39 +000077/* State used to build the fragment program:
78 */
79struct texenv_fragment_program {
Keith Whitwell47b29f52005-05-04 11:44:44 +000080 struct fragment_program *program;
Keith Whitwell15e75e02005-04-29 15:11:39 +000081 GLcontext *ctx;
82
Keith Whitwellcf4f3c52005-05-16 12:15:01 +000083 GLuint alu_temps; /* Track texture indirections, see spec. */
84 GLuint temps_output; /* Track texture indirections, see spec. */
Keith Whitwellecb6bfc2005-05-10 08:58:44 +000085
Keith Whitwell93cd9232005-05-11 08:30:23 +000086 GLuint temp_in_use; /* Tracks temporary regs which are in
Keith Whitwell15e75e02005-04-29 15:11:39 +000087 * use.
88 */
89
90
Keith Whitwell15e75e02005-04-29 15:11:39 +000091 GLboolean error;
92
93 struct ureg src_texture; /* Reg containing sampled texture color,
94 * else undef.
95 */
96
97 struct ureg src_previous; /* Reg containing color from previous
98 * stage. May need to be decl'd.
99 */
100
101 GLuint last_tex_stage; /* Number of last enabled texture unit */
102};
103
104
105
106static struct ureg make_ureg(GLuint file, GLuint idx)
107{
108 struct ureg reg;
109 reg.file = file;
110 reg.idx = idx;
111 reg.negatebase = 0;
112 reg.abs = 0;
113 reg.negateabs = 0;
114 reg.swz = SWIZZLE_NOOP;
115 reg.pad = 0;
116 return reg;
117}
118
119static struct ureg swizzle( struct ureg reg, int x, int y, int z, int w )
120{
121 reg.swz = MAKE_SWIZZLE4(GET_SWZ(reg.swz, x),
122 GET_SWZ(reg.swz, y),
123 GET_SWZ(reg.swz, z),
124 GET_SWZ(reg.swz, w));
125
126 return reg;
127}
128
129static struct ureg swizzle1( struct ureg reg, int x )
130{
131 return swizzle(reg, x, x, x, x);
132}
133
134static GLboolean is_undef( struct ureg reg )
135{
136 return reg.file == 0xf;
137}
138
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000139
Keith Whitwell15e75e02005-04-29 15:11:39 +0000140static struct ureg get_temp( struct texenv_fragment_program *p )
141{
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000142 int bit;
143
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000144 /* First try and reuse temps which have been used already:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000145 */
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000146 bit = ffs( ~p->temp_in_use & p->alu_temps );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000147
148 /* Then any unused temporary:
149 */
150 if (!bit)
Keith Whitwell93cd9232005-05-11 08:30:23 +0000151 bit = ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000152
Keith Whitwell15e75e02005-04-29 15:11:39 +0000153 if (!bit) {
154 fprintf(stderr, "%s: out of temporaries\n", __FILE__);
155 exit(1);
156 }
157
Keith Whitwell93cd9232005-05-11 08:30:23 +0000158 p->temp_in_use |= 1<<(bit-1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000159 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
160}
161
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000162static struct ureg get_tex_temp( struct texenv_fragment_program *p )
163{
164 int bit;
165
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000166 /* First try to find availble temp not previously used (to avoid
167 * starting a new texture indirection). According to the spec, the
168 * ~p->temps_output isn't necessary, but will keep it there for
169 * now:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000170 */
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000171 bit = ffs( ~p->temp_in_use & ~p->alu_temps & ~p->temps_output );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000172
173 /* Then any unused temporary:
174 */
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000175 if (!bit)
Keith Whitwell93cd9232005-05-11 08:30:23 +0000176 bit = ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000177
178 if (!bit) {
179 fprintf(stderr, "%s: out of temporaries\n", __FILE__);
180 exit(1);
181 }
182
Keith Whitwell93cd9232005-05-11 08:30:23 +0000183 p->temp_in_use |= 1<<(bit-1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000184 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
185}
186
Keith Whitwell15e75e02005-04-29 15:11:39 +0000187
188static void release_temps( struct texenv_fragment_program *p )
189{
Keith Whitwell93cd9232005-05-11 08:30:23 +0000190 GLuint max_temp = p->ctx->Const.MaxFragmentProgramTemps;
191
192 if (max_temp >= sizeof(int) * 8)
193 p->temp_in_use = 0;
194 else
195 p->temp_in_use = ~((1<<max_temp)-1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000196}
197
198
Keith Whitwell47b29f52005-05-04 11:44:44 +0000199static struct ureg register_param6( struct texenv_fragment_program *p,
200 GLint s0,
201 GLint s1,
202 GLint s2,
203 GLint s3,
204 GLint s4,
205 GLint s5)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000206{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000207 GLint tokens[6];
208 GLuint idx;
209 tokens[0] = s0;
210 tokens[1] = s1;
211 tokens[2] = s2;
212 tokens[3] = s3;
213 tokens[4] = s4;
214 tokens[5] = s5;
215 idx = _mesa_add_state_reference( p->program->Parameters, tokens );
216 return make_ureg(PROGRAM_STATE_VAR, idx);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000217}
218
Keith Whitwell47b29f52005-05-04 11:44:44 +0000219
220#define register_param1(p,s0) register_param6(p,s0,0,0,0,0,0)
221#define register_param2(p,s0,s1) register_param6(p,s0,s1,0,0,0,0)
222#define register_param3(p,s0,s1,s2) register_param6(p,s0,s1,s2,0,0,0)
223#define register_param4(p,s0,s1,s2,s3) register_param6(p,s0,s1,s2,s3,0,0)
224
225
226static struct ureg register_input( struct texenv_fragment_program *p, GLuint input )
227{
228 p->program->InputsRead |= (1<<input);
229 return make_ureg(PROGRAM_INPUT, input);
230}
231
232
Keith Whitwell15e75e02005-04-29 15:11:39 +0000233static void emit_arg( struct fp_src_register *reg,
234 struct ureg ureg )
235{
236 reg->File = ureg.file;
237 reg->Index = ureg.idx;
238 reg->Swizzle = ureg.swz;
239 reg->NegateBase = ureg.negatebase;
240 reg->Abs = ureg.abs;
241 reg->NegateAbs = ureg.negateabs;
242}
243
244static void emit_dst( struct fp_dst_register *dst,
245 struct ureg ureg, GLuint mask )
246{
247 dst->File = ureg.file;
248 dst->Index = ureg.idx;
249 dst->WriteMask = mask;
250 dst->CondMask = 0;
251 dst->CondSwizzle = 0;
252}
253
254static struct fp_instruction *
255emit_op(struct texenv_fragment_program *p,
256 GLuint op,
257 struct ureg dest,
258 GLuint mask,
259 GLuint saturate,
260 struct ureg src0,
261 struct ureg src1,
262 struct ureg src2 )
263{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000264 GLuint nr = p->program->Base.NumInstructions++;
265 struct fp_instruction *inst = &p->program->Instructions[nr];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000266
267 memset(inst, 0, sizeof(*inst));
268 inst->Opcode = op;
269
Keith Whitwell47b29f52005-05-04 11:44:44 +0000270 emit_arg( &inst->SrcReg[0], src0 );
271 emit_arg( &inst->SrcReg[1], src1 );
272 emit_arg( &inst->SrcReg[2], src2 );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000273
274 inst->Saturate = saturate;
275
276 emit_dst( &inst->DstReg, dest, mask );
277
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000278 /* Accounting for indirection tracking:
279 */
280 if (dest.file == PROGRAM_TEMPORARY)
281 p->temps_output |= 1 << dest.idx;
282
Keith Whitwell15e75e02005-04-29 15:11:39 +0000283 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 Whitwellcf4f3c52005-05-16 12:15:01 +0000298 /* Accounting for indirection tracking:
299 */
300 if (src0.file == PROGRAM_TEMPORARY)
301 p->alu_temps |= 1 << src0.idx;
302
303 if (!is_undef(src1) && src1.file == PROGRAM_TEMPORARY)
304 p->alu_temps |= 1 << src1.idx;
305
306 if (!is_undef(src2) && src2.file == PROGRAM_TEMPORARY)
307 p->alu_temps |= 1 << src2.idx;
308
309 if (dest.file == PROGRAM_TEMPORARY)
310 p->alu_temps |= 1 << dest.idx;
311
Keith Whitwell47b29f52005-05-04 11:44:44 +0000312 p->program->NumAluInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000313 return dest;
314}
315
316static struct ureg emit_texld( struct texenv_fragment_program *p,
317 GLuint op,
318 struct ureg dest,
319 GLuint destmask,
320 GLuint tex_unit,
321 GLuint tex_idx,
322 struct ureg coord )
323{
324 struct fp_instruction *inst = emit_op( p, op,
325 dest, destmask,
326 0, /* don't saturate? */
327 coord, /* arg 0? */
328 undef,
329 undef);
330
331 inst->TexSrcIdx = tex_idx;
332 inst->TexSrcUnit = tex_unit;
333
Keith Whitwell47b29f52005-05-04 11:44:44 +0000334 p->program->NumTexInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000335
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000336 /* Is this a texture indirection?
337 */
338 if ((coord.file == PROGRAM_TEMPORARY &&
339 (p->temps_output & (1<<coord.idx))) ||
340 (dest.file == PROGRAM_TEMPORARY &&
341 (p->alu_temps & (1<<dest.idx)))) {
Keith Whitwell47b29f52005-05-04 11:44:44 +0000342 p->program->NumTexIndirections++;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000343 p->temps_output = 0;
344 p->alu_temps = 0;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000345 }
346
347 return dest;
348}
349
350
Keith Whitwell47b29f52005-05-04 11:44:44 +0000351static struct ureg register_const4f( struct texenv_fragment_program *p,
352 GLfloat s0,
353 GLfloat s1,
354 GLfloat s2,
355 GLfloat s3)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000356{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000357 GLfloat values[4];
358 GLuint idx;
359 values[0] = s0;
360 values[1] = s1;
361 values[2] = s2;
362 values[3] = s3;
363 idx = _mesa_add_unnamed_constant( p->program->Parameters, values );
364 return make_ureg(PROGRAM_STATE_VAR, idx);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000365}
366
Keith Whitwelle4902422005-05-11 15:16:35 +0000367#define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000368#define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
369#define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
370#define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000371
372
373
Keith Whitwell15e75e02005-04-29 15:11:39 +0000374
375
376
377
378
379static void program_error( struct texenv_fragment_program *p, const char *msg )
380{
381 fprintf(stderr, "%s\n", msg);
382 p->error = 1;
383}
384
385
386static GLuint translate_tex_src_bit( struct texenv_fragment_program *p,
387 GLuint bit )
388{
389 switch (bit) {
390 case TEXTURE_1D_BIT: return TEXTURE_1D_INDEX;
391 case TEXTURE_2D_BIT: return TEXTURE_2D_INDEX;
392 case TEXTURE_RECT_BIT: return TEXTURE_RECT_INDEX;
393 case TEXTURE_3D_BIT: return TEXTURE_3D_INDEX;
394 case TEXTURE_CUBE_BIT: return TEXTURE_CUBE_INDEX;
395 default: program_error(p, "TexSrcBit"); return 0;
396 }
397}
398
399
400static struct ureg get_source( struct texenv_fragment_program *p,
401 GLenum src, GLuint unit )
402{
403 switch (src) {
404 case GL_TEXTURE:
405 if (is_undef(p->src_texture)) {
406
407 GLuint dim = translate_tex_src_bit( p, p->ctx->Texture.Unit[unit]._ReallyEnabled);
Keith Whitwell47b29f52005-05-04 11:44:44 +0000408 struct ureg texcoord = register_input(p, FRAG_ATTRIB_TEX0+unit);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000409 struct ureg tmp = get_tex_temp( p );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000410
411 /* TODO: Use D0_MASK_XY where possible.
412 */
413 p->src_texture = emit_texld( p, FP_OPCODE_TXP,
414 tmp, WRITEMASK_XYZW,
415 unit, dim, texcoord );
416 }
417
418 return p->src_texture;
419
420 /* Crossbar: */
421 case GL_TEXTURE0:
422 case GL_TEXTURE1:
423 case GL_TEXTURE2:
424 case GL_TEXTURE3:
425 case GL_TEXTURE4:
426 case GL_TEXTURE5:
427 case GL_TEXTURE6:
428 case GL_TEXTURE7: {
429 return undef;
430 }
431
432 case GL_CONSTANT:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000433 return register_param2(p, STATE_TEXENV_COLOR, unit);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000434 case GL_PRIMARY_COLOR:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000435 return register_input(p, FRAG_ATTRIB_COL0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000436 case GL_PREVIOUS:
437 default:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000438 if (is_undef(p->src_previous))
439 return register_input(p, FRAG_ATTRIB_COL0);
440 else
441 return p->src_previous;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000442 }
443}
444
445
446static struct ureg emit_combine_source( struct texenv_fragment_program *p,
447 GLuint mask,
448 GLuint unit,
449 GLenum source,
450 GLenum operand )
451{
452 struct ureg arg, src, one;
453
454 src = get_source(p, source, unit);
455
456 switch (operand) {
457 case GL_ONE_MINUS_SRC_COLOR:
458 /* Get unused tmp,
459 * Emit tmp = 1.0 - arg.xyzw
460 */
461 arg = get_temp( p );
Keith Whitwelle4902422005-05-11 15:16:35 +0000462 one = register_scalar_const(p, 1.0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000463 return emit_arith( p, FP_OPCODE_SUB, arg, mask, 0, one, src, undef);
464
465 case GL_SRC_ALPHA:
466 if (mask == WRITEMASK_W)
467 return src;
468 else
469 return swizzle1( src, W );
470 case GL_ONE_MINUS_SRC_ALPHA:
471 /* Get unused tmp,
472 * Emit tmp = 1.0 - arg.wwww
473 */
474 arg = get_temp( p );
Keith Whitwelle4902422005-05-11 15:16:35 +0000475 one = register_scalar_const(p, 1.0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000476 return emit_arith( p, FP_OPCODE_SUB, arg, mask, 0,
477 one, swizzle1(src, W), undef);
478 case GL_SRC_COLOR:
479 default:
480 return src;
481 }
482}
483
484
485
486static int nr_args( GLenum mode )
487{
488 switch (mode) {
489 case GL_REPLACE: return 1;
490 case GL_MODULATE: return 2;
491 case GL_ADD: return 2;
492 case GL_ADD_SIGNED: return 2;
493 case GL_INTERPOLATE: return 3;
494 case GL_SUBTRACT: return 2;
495 case GL_DOT3_RGB_EXT: return 2;
496 case GL_DOT3_RGBA_EXT: return 2;
497 case GL_DOT3_RGB: return 2;
498 case GL_DOT3_RGBA: return 2;
499 default: return 0;
500 }
501}
502
503
504static GLboolean args_match( struct gl_texture_unit *texUnit )
505{
506 int i, nr = nr_args(texUnit->_CurrentCombine->ModeRGB);
507
508 for (i = 0 ; i < nr ; i++) {
509 if (texUnit->_CurrentCombine->SourceA[i] != texUnit->_CurrentCombine->SourceRGB[i])
510 return GL_FALSE;
511
512 switch(texUnit->_CurrentCombine->OperandA[i]) {
513 case GL_SRC_ALPHA:
514 switch(texUnit->_CurrentCombine->OperandRGB[i]) {
515 case GL_SRC_COLOR:
516 case GL_SRC_ALPHA:
517 break;
518 default:
519 return GL_FALSE;
520 }
521 break;
522 case GL_ONE_MINUS_SRC_ALPHA:
523 switch(texUnit->_CurrentCombine->OperandRGB[i]) {
524 case GL_ONE_MINUS_SRC_COLOR:
525 case GL_ONE_MINUS_SRC_ALPHA:
526 break;
527 default:
528 return GL_FALSE;
529 }
530 break;
531 default:
532 return GL_FALSE; /* impossible */
533 }
534 }
535
536 return GL_TRUE;
537}
538
539
540static struct ureg emit_combine( struct texenv_fragment_program *p,
541 struct ureg dest,
542 GLuint mask,
543 GLuint saturate,
544 GLuint unit,
545 GLenum mode,
546 const GLenum *source,
547 const GLenum *operand)
548{
549 int nr = nr_args(mode);
550 struct ureg src[3];
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000551 struct ureg tmp, half;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000552 int i;
553
554 for (i = 0; i < nr; i++)
555 src[i] = emit_combine_source( p, mask, unit, source[i], operand[i] );
556
557 switch (mode) {
558 case GL_REPLACE:
559 if (mask == WRITEMASK_XYZW && !saturate)
560 return src[0];
561 else
562 return emit_arith( p, FP_OPCODE_MOV, dest, mask, saturate, src[0], undef, undef );
563 case GL_MODULATE:
564 return emit_arith( p, FP_OPCODE_MUL, dest, mask, saturate,
565 src[0], src[1], undef );
566 case GL_ADD:
567 return emit_arith( p, FP_OPCODE_ADD, dest, mask, saturate,
568 src[0], src[1], undef );
569 case GL_ADD_SIGNED:
570 /* tmp = arg0 + arg1
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000571 * result = tmp - .5
Keith Whitwell15e75e02005-04-29 15:11:39 +0000572 */
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000573 half = register_scalar_const(p, .5);
574 emit_arith( p, FP_OPCODE_ADD, tmp, mask, 0, src[0], src[1], undef );
575 emit_arith( p, FP_OPCODE_SUB, dest, mask, saturate, tmp, half, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000576 return dest;
577 case GL_INTERPOLATE:
578 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
579 */
580 return emit_arith( p, FP_OPCODE_LRP, dest, mask, saturate, src[2], src[0], src[1] );
581
582 case GL_SUBTRACT:
583 return emit_arith( p, FP_OPCODE_SUB, dest, mask, saturate, src[0], src[1], undef );
584
585 case GL_DOT3_RGBA:
586 case GL_DOT3_RGBA_EXT:
587 case GL_DOT3_RGB_EXT:
588 case GL_DOT3_RGB: {
589 struct ureg tmp0 = get_temp( p );
590 struct ureg tmp1 = get_temp( p );
Keith Whitwelle4902422005-05-11 15:16:35 +0000591 struct ureg neg1 = register_scalar_const(p, -1);
592 struct ureg two = register_scalar_const(p, 2);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000593
594 /* tmp0 = 2*src0 - 1
595 * tmp1 = 2*src1 - 1
596 *
597 * dst = tmp0 dot3 tmp1
598 */
599 emit_arith( p, FP_OPCODE_MAD, tmp0, WRITEMASK_XYZW, 0,
600 two, src[0], neg1);
601
602 if (memcmp(&src[0], &src[1], sizeof(struct ureg)) == 0)
603 tmp1 = tmp0;
604 else
605 emit_arith( p, FP_OPCODE_MAD, tmp1, WRITEMASK_XYZW, 0,
606 two, src[1], neg1);
607 emit_arith( p, FP_OPCODE_DP3, dest, mask, saturate, tmp0, tmp1, undef);
608 return dest;
609 }
610
611 default:
612 return src[0];
613 }
614}
615
Keith Whitwell15e75e02005-04-29 15:11:39 +0000616
617
618static struct ureg emit_texenv( struct texenv_fragment_program *p, int unit )
619{
620 struct gl_texture_unit *texUnit = &p->ctx->Texture.Unit[unit];
621 GLuint saturate = (unit < p->last_tex_stage);
622 GLuint rgb_shift, alpha_shift;
623 struct ureg out, shift;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000624 struct ureg dest;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000625
626 if (!texUnit->_ReallyEnabled) {
627 return get_source(p, GL_PREVIOUS, 0);
628 }
629
630 switch (texUnit->_CurrentCombine->ModeRGB) {
631 case GL_DOT3_RGB_EXT:
632 alpha_shift = texUnit->_CurrentCombine->ScaleShiftA;
633 rgb_shift = 0;
634 break;
635
636 case GL_DOT3_RGBA_EXT:
637 alpha_shift = 0;
638 rgb_shift = 0;
639 break;
640
641 default:
642 rgb_shift = texUnit->_CurrentCombine->ScaleShiftRGB;
643 alpha_shift = texUnit->_CurrentCombine->ScaleShiftA;
644 break;
645 }
646
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000647 /* If this is the very last calculation, emit direct to output reg:
648 */
649 if ((p->ctx->_TriangleCaps & DD_SEPARATE_SPECULAR) ||
650 unit != p->last_tex_stage ||
651 alpha_shift ||
652 rgb_shift)
653 dest = get_temp( p );
654 else
655 dest = make_ureg(PROGRAM_OUTPUT, FRAG_OUTPUT_COLR);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000656
657 /* Emit the RGB and A combine ops
658 */
659 if (texUnit->_CurrentCombine->ModeRGB == texUnit->_CurrentCombine->ModeA &&
660 args_match( texUnit )) {
661 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
662 unit,
663 texUnit->_CurrentCombine->ModeRGB,
664 texUnit->_CurrentCombine->SourceRGB,
665 texUnit->_CurrentCombine->OperandRGB );
666 }
667 else if (texUnit->_CurrentCombine->ModeRGB == GL_DOT3_RGBA_EXT ||
668 texUnit->_CurrentCombine->ModeRGB == GL_DOT3_RGBA) {
669
670 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
671 unit,
672 texUnit->_CurrentCombine->ModeRGB,
673 texUnit->_CurrentCombine->SourceRGB,
674 texUnit->_CurrentCombine->OperandRGB );
675 }
676 else {
677 /* Need to do something to stop from re-emitting identical
678 * argument calculations here:
679 */
680 out = emit_combine( p, dest, WRITEMASK_XYZ, saturate,
681 unit,
682 texUnit->_CurrentCombine->ModeRGB,
683 texUnit->_CurrentCombine->SourceRGB,
684 texUnit->_CurrentCombine->OperandRGB );
685 out = emit_combine( p, dest, WRITEMASK_W, saturate,
686 unit,
687 texUnit->_CurrentCombine->ModeA,
688 texUnit->_CurrentCombine->SourceA,
689 texUnit->_CurrentCombine->OperandA );
690 }
691
692 /* Deal with the final shift:
693 */
694 if (alpha_shift || rgb_shift) {
695 if (rgb_shift == alpha_shift) {
Keith Whitwelle4902422005-05-11 15:16:35 +0000696 shift = register_scalar_const(p, 1<<rgb_shift);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000697 }
698 else {
Keith Whitwell47b29f52005-05-04 11:44:44 +0000699 shift = register_const2f(p, 1<<rgb_shift, 1<<alpha_shift);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000700 shift = swizzle(shift,X,X,X,Y);
701 }
702 return emit_arith( p, FP_OPCODE_MUL, dest, WRITEMASK_XYZW,
703 saturate, out, shift, undef );
704 }
705 else
706 return out;
707}
708
709void _mesa_UpdateTexEnvProgram( GLcontext *ctx )
710{
711 struct texenv_fragment_program p;
712 GLuint unit;
713 struct ureg cf, out;
Keith Whitwelle4902422005-05-11 15:16:35 +0000714 GLuint db_NumInstructions = 0;
715 struct fp_instruction *db_Instructions = NULL;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000716
Keith Whitwell47b29f52005-05-04 11:44:44 +0000717 if (ctx->FragmentProgram._Enabled)
718 return;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000719
Keith Whitwell8b88f622005-05-10 11:39:50 +0000720 if (!ctx->_TexEnvProgram)
721 ctx->FragmentProgram._Current = ctx->_TexEnvProgram =
722 (struct fragment_program *)
723 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
Keith Whitwell276330b2005-05-09 17:58:13 +0000724
Keith Whitwelle4902422005-05-11 15:16:35 +0000725 _mesa_memset(&p, 0, sizeof(p));
Keith Whitwell47b29f52005-05-04 11:44:44 +0000726 p.ctx = ctx;
Keith Whitwell276330b2005-05-09 17:58:13 +0000727 p.program = ctx->_TexEnvProgram;
Keith Whitwell47b29f52005-05-04 11:44:44 +0000728
Keith Whitwelle4902422005-05-11 15:16:35 +0000729 if (ctx->Driver.ProgramStringNotify || DISASSEM) {
730 db_Instructions = p.program->Instructions;
731 db_NumInstructions = p.program->Base.NumInstructions;
732 p.program->Instructions = NULL;
733 }
Keith Whitwell15e75e02005-04-29 15:11:39 +0000734
Keith Whitwelle4902422005-05-11 15:16:35 +0000735 if (!p.program->Instructions)
736 p.program->Instructions = MALLOC(sizeof(struct fp_instruction) * 100);
737
Keith Whitwell47b29f52005-05-04 11:44:44 +0000738 p.program->Base.NumInstructions = 0;
Keith Whitwell276330b2005-05-09 17:58:13 +0000739 p.program->Base.Target = GL_FRAGMENT_PROGRAM_ARB;
Keith Whitwell47b29f52005-05-04 11:44:44 +0000740 p.program->NumTexIndirections = 1; /* correct? */
741 p.program->NumTexInstructions = 0;
742 p.program->NumAluInstructions = 0;
Keith Whitwell9ca88152005-05-10 09:56:02 +0000743 p.program->Base.String = 0;
744 p.program->Base.NumInstructions =
745 p.program->Base.NumTemporaries =
746 p.program->Base.NumParameters =
747 p.program->Base.NumAttributes = p.program->Base.NumAddressRegs = 0;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000748
Keith Whitwell9ca88152005-05-10 09:56:02 +0000749 if (p.program->Parameters)
Keith Whitwelldbeea252005-05-10 13:57:50 +0000750 _mesa_free_parameters(p.program->Parameters);
751 else
752 p.program->Parameters = _mesa_new_parameter_list();
753
Keith Whitwell9ca88152005-05-10 09:56:02 +0000754 p.program->InputsRead = 0;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000755 p.program->OutputsWritten = 1 << FRAG_OUTPUT_COLR;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000756
757 p.src_texture = undef;
Keith Whitwell47b29f52005-05-04 11:44:44 +0000758 p.src_previous = undef;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000759 p.last_tex_stage = 0;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000760 release_temps(&p);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000761
762 if (ctx->Texture._EnabledUnits) {
763 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits ; unit++)
764 if (ctx->Texture.Unit[unit]._ReallyEnabled) {
765 p.last_tex_stage = unit;
766 }
767
768 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits; unit++)
769 if (ctx->Texture.Unit[unit]._ReallyEnabled) {
770 p.src_previous = emit_texenv( &p, unit );
771 p.src_texture = undef;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000772 release_temps(&p); /* release all temps */
773 if (p.src_previous.file == PROGRAM_TEMPORARY)
774 p.temp_in_use |= 1 << p.src_previous.idx; /* except for this one */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000775 }
776 }
777
778 cf = get_source( &p, GL_PREVIOUS, 0 );
Keith Whitwell47b29f52005-05-04 11:44:44 +0000779 out = make_ureg( PROGRAM_OUTPUT, FRAG_OUTPUT_COLR );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000780
781 if (ctx->_TriangleCaps & DD_SEPARATE_SPECULAR) {
782 /* Emit specular add.
783 */
Keith Whitwell47b29f52005-05-04 11:44:44 +0000784 struct ureg s = register_input(&p, FRAG_ATTRIB_COL1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000785 emit_arith( &p, FP_OPCODE_ADD, out, WRITEMASK_XYZ, 0, cf, s, undef );
786 }
787 else if (memcmp(&cf, &out, sizeof(cf)) != 0) {
788 /* Will wind up in here if no texture enabled or a couple of
789 * other scenarios (GL_REPLACE for instance).
790 */
791 emit_arith( &p, FP_OPCODE_MOV, out, WRITEMASK_XYZW, 0, cf, undef, undef );
792 }
793
Keith Whitwell47b29f52005-05-04 11:44:44 +0000794 /* Finish up:
795 */
796 emit_arith( &p, FP_OPCODE_END, undef, WRITEMASK_XYZW, 0, undef, undef, undef);
797
Keith Whitwell276330b2005-05-09 17:58:13 +0000798 if (ctx->Fog.Enabled)
799 p.program->FogOption = ctx->Fog.Mode;
800 else
801 p.program->FogOption = GL_NONE;
Keith Whitwell47b29f52005-05-04 11:44:44 +0000802
803 if (p.program->NumTexIndirections > ctx->Const.MaxFragmentProgramTexIndirections)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000804 program_error(&p, "Exceeded max nr indirect texture lookups");
805
Keith Whitwell47b29f52005-05-04 11:44:44 +0000806 if (p.program->NumTexInstructions > ctx->Const.MaxFragmentProgramTexInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000807 program_error(&p, "Exceeded max TEX instructions");
808
Keith Whitwell47b29f52005-05-04 11:44:44 +0000809 if (p.program->NumAluInstructions > ctx->Const.MaxFragmentProgramAluInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000810 program_error(&p, "Exceeded max ALU instructions");
811
Keith Whitwell8b88f622005-05-10 11:39:50 +0000812
Keith Whitwelldbeea252005-05-10 13:57:50 +0000813 /* Notify driver the fragment program has (actually) changed.
Keith Whitwell8b88f622005-05-10 11:39:50 +0000814 */
Keith Whitwelle4902422005-05-11 15:16:35 +0000815 if (ctx->Driver.ProgramStringNotify || DISASSEM) {
816 if (db_Instructions == NULL ||
817 db_NumInstructions != p.program->Base.NumInstructions ||
818 memcmp(db_Instructions, p.program->Instructions,
819 db_NumInstructions * sizeof(*db_Instructions)) != 0) {
820
821 if (ctx->Driver.ProgramStringNotify)
822 ctx->Driver.ProgramStringNotify( ctx, GL_FRAGMENT_PROGRAM_ARB,
823 &p.program->Base );
Keith Whitwelldbeea252005-05-10 13:57:50 +0000824
Keith Whitwelle4902422005-05-11 15:16:35 +0000825 if (DISASSEM) {
826 _mesa_debug_fp_inst(p.program->NumTexInstructions + p.program->NumAluInstructions,
827 p.program->Instructions);
828 _mesa_printf("\n");
829 }
830 }
831
832 FREE(db_Instructions);
833 }
Keith Whitwell15e75e02005-04-29 15:11:39 +0000834}
835
836