blob: 73681b1a6b2488f5d4c6005c1f9b7cf7487d23e3 [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 Whitwell269e3892005-05-12 10:28:43 +000040#define DISASSEM (MESA_VERBOSE & VERBOSE_DISASSEM)
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 Whitwellcf4f3c52005-05-16 12:15:01 +000085 GLuint alu_temps; /* Track texture indirections, see spec. */
86 GLuint temps_output; /* Track texture indirections, see spec. */
Keith Whitwellecb6bfc2005-05-10 08:58:44 +000087
Keith Whitwell93cd9232005-05-11 08:30:23 +000088 GLuint temp_in_use; /* Tracks temporary regs which are in
Keith Whitwell15e75e02005-04-29 15:11:39 +000089 * use.
90 */
91
92
Keith Whitwell15e75e02005-04-29 15:11:39 +000093 GLboolean error;
94
95 struct ureg src_texture; /* Reg containing sampled texture color,
96 * else undef.
97 */
98
99 struct ureg src_previous; /* Reg containing color from previous
100 * stage. May need to be decl'd.
101 */
102
103 GLuint last_tex_stage; /* Number of last enabled texture unit */
104};
105
106
107
108static struct ureg make_ureg(GLuint file, GLuint idx)
109{
110 struct ureg reg;
111 reg.file = file;
112 reg.idx = idx;
113 reg.negatebase = 0;
114 reg.abs = 0;
115 reg.negateabs = 0;
116 reg.swz = SWIZZLE_NOOP;
117 reg.pad = 0;
118 return reg;
119}
120
121static struct ureg swizzle( struct ureg reg, int x, int y, int z, int w )
122{
123 reg.swz = MAKE_SWIZZLE4(GET_SWZ(reg.swz, x),
124 GET_SWZ(reg.swz, y),
125 GET_SWZ(reg.swz, z),
126 GET_SWZ(reg.swz, w));
127
128 return reg;
129}
130
131static struct ureg swizzle1( struct ureg reg, int x )
132{
133 return swizzle(reg, x, x, x, x);
134}
135
136static GLboolean is_undef( struct ureg reg )
137{
138 return reg.file == 0xf;
139}
140
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000141
Keith Whitwell15e75e02005-04-29 15:11:39 +0000142static struct ureg get_temp( struct texenv_fragment_program *p )
143{
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000144 int bit;
145
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000146 /* First try and reuse temps which have been used already:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000147 */
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000148 bit = ffs( ~p->temp_in_use & p->alu_temps );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000149
150 /* Then any unused temporary:
151 */
152 if (!bit)
Keith Whitwell93cd9232005-05-11 08:30:23 +0000153 bit = ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000154
Keith Whitwell15e75e02005-04-29 15:11:39 +0000155 if (!bit) {
156 fprintf(stderr, "%s: out of temporaries\n", __FILE__);
157 exit(1);
158 }
159
Keith Whitwell93cd9232005-05-11 08:30:23 +0000160 p->temp_in_use |= 1<<(bit-1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000161 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
162}
163
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000164static struct ureg get_tex_temp( struct texenv_fragment_program *p )
165{
166 int bit;
167
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000168 /* First try to find availble temp not previously used (to avoid
169 * starting a new texture indirection). According to the spec, the
170 * ~p->temps_output isn't necessary, but will keep it there for
171 * now:
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000172 */
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000173 bit = ffs( ~p->temp_in_use & ~p->alu_temps & ~p->temps_output );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000174
175 /* Then any unused temporary:
176 */
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000177 if (!bit)
Keith Whitwell93cd9232005-05-11 08:30:23 +0000178 bit = ffs( ~p->temp_in_use );
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000179
180 if (!bit) {
181 fprintf(stderr, "%s: out of temporaries\n", __FILE__);
182 exit(1);
183 }
184
Keith Whitwell93cd9232005-05-11 08:30:23 +0000185 p->temp_in_use |= 1<<(bit-1);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000186 return make_ureg(PROGRAM_TEMPORARY, (bit-1));
187}
188
Keith Whitwell15e75e02005-04-29 15:11:39 +0000189
190static void release_temps( struct texenv_fragment_program *p )
191{
Keith Whitwell93cd9232005-05-11 08:30:23 +0000192 GLuint max_temp = p->ctx->Const.MaxFragmentProgramTemps;
193
194 if (max_temp >= sizeof(int) * 8)
195 p->temp_in_use = 0;
196 else
197 p->temp_in_use = ~((1<<max_temp)-1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000198}
199
200
Keith Whitwell47b29f52005-05-04 11:44:44 +0000201static struct ureg register_param6( struct texenv_fragment_program *p,
202 GLint s0,
203 GLint s1,
204 GLint s2,
205 GLint s3,
206 GLint s4,
207 GLint s5)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000208{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000209 GLint tokens[6];
210 GLuint idx;
211 tokens[0] = s0;
212 tokens[1] = s1;
213 tokens[2] = s2;
214 tokens[3] = s3;
215 tokens[4] = s4;
216 tokens[5] = s5;
217 idx = _mesa_add_state_reference( p->program->Parameters, tokens );
218 return make_ureg(PROGRAM_STATE_VAR, idx);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000219}
220
Keith Whitwell47b29f52005-05-04 11:44:44 +0000221
222#define register_param1(p,s0) register_param6(p,s0,0,0,0,0,0)
223#define register_param2(p,s0,s1) register_param6(p,s0,s1,0,0,0,0)
224#define register_param3(p,s0,s1,s2) register_param6(p,s0,s1,s2,0,0,0)
225#define register_param4(p,s0,s1,s2,s3) register_param6(p,s0,s1,s2,s3,0,0)
226
227
228static struct ureg register_input( struct texenv_fragment_program *p, GLuint input )
229{
230 p->program->InputsRead |= (1<<input);
231 return make_ureg(PROGRAM_INPUT, input);
232}
233
234
Keith Whitwell15e75e02005-04-29 15:11:39 +0000235static void emit_arg( struct fp_src_register *reg,
236 struct ureg ureg )
237{
238 reg->File = ureg.file;
239 reg->Index = ureg.idx;
240 reg->Swizzle = ureg.swz;
241 reg->NegateBase = ureg.negatebase;
242 reg->Abs = ureg.abs;
243 reg->NegateAbs = ureg.negateabs;
244}
245
246static void emit_dst( struct fp_dst_register *dst,
247 struct ureg ureg, GLuint mask )
248{
249 dst->File = ureg.file;
250 dst->Index = ureg.idx;
251 dst->WriteMask = mask;
252 dst->CondMask = 0;
253 dst->CondSwizzle = 0;
254}
255
256static struct fp_instruction *
257emit_op(struct texenv_fragment_program *p,
258 GLuint op,
259 struct ureg dest,
260 GLuint mask,
261 GLuint saturate,
262 struct ureg src0,
263 struct ureg src1,
264 struct ureg src2 )
265{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000266 GLuint nr = p->program->Base.NumInstructions++;
267 struct fp_instruction *inst = &p->program->Instructions[nr];
Keith Whitwell15e75e02005-04-29 15:11:39 +0000268
269 memset(inst, 0, sizeof(*inst));
270 inst->Opcode = op;
271
Keith Whitwell47b29f52005-05-04 11:44:44 +0000272 emit_arg( &inst->SrcReg[0], src0 );
273 emit_arg( &inst->SrcReg[1], src1 );
274 emit_arg( &inst->SrcReg[2], src2 );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000275
276 inst->Saturate = saturate;
277
278 emit_dst( &inst->DstReg, dest, mask );
279
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000280 /* Accounting for indirection tracking:
281 */
282 if (dest.file == PROGRAM_TEMPORARY)
283 p->temps_output |= 1 << dest.idx;
284
Keith Whitwell15e75e02005-04-29 15:11:39 +0000285 return inst;
286}
287
288
289static struct ureg emit_arith( struct texenv_fragment_program *p,
Keith Whitwell47b29f52005-05-04 11:44:44 +0000290 GLuint op,
291 struct ureg dest,
292 GLuint mask,
293 GLuint saturate,
294 struct ureg src0,
295 struct ureg src1,
296 struct ureg src2 )
Keith Whitwell15e75e02005-04-29 15:11:39 +0000297{
298 emit_op(p, op, dest, mask, saturate, src0, src1, src2);
299
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000300 /* Accounting for indirection tracking:
301 */
302 if (src0.file == PROGRAM_TEMPORARY)
303 p->alu_temps |= 1 << src0.idx;
304
305 if (!is_undef(src1) && src1.file == PROGRAM_TEMPORARY)
306 p->alu_temps |= 1 << src1.idx;
307
308 if (!is_undef(src2) && src2.file == PROGRAM_TEMPORARY)
309 p->alu_temps |= 1 << src2.idx;
310
311 if (dest.file == PROGRAM_TEMPORARY)
312 p->alu_temps |= 1 << dest.idx;
313
Keith Whitwell47b29f52005-05-04 11:44:44 +0000314 p->program->NumAluInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000315 return dest;
316}
317
318static struct ureg emit_texld( struct texenv_fragment_program *p,
319 GLuint op,
320 struct ureg dest,
321 GLuint destmask,
322 GLuint tex_unit,
323 GLuint tex_idx,
324 struct ureg coord )
325{
326 struct fp_instruction *inst = emit_op( p, op,
327 dest, destmask,
328 0, /* don't saturate? */
329 coord, /* arg 0? */
330 undef,
331 undef);
332
333 inst->TexSrcIdx = tex_idx;
334 inst->TexSrcUnit = tex_unit;
335
Keith Whitwell47b29f52005-05-04 11:44:44 +0000336 p->program->NumTexInstructions++;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000337
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000338 /* Is this a texture indirection?
339 */
340 if ((coord.file == PROGRAM_TEMPORARY &&
341 (p->temps_output & (1<<coord.idx))) ||
342 (dest.file == PROGRAM_TEMPORARY &&
343 (p->alu_temps & (1<<dest.idx)))) {
Keith Whitwell47b29f52005-05-04 11:44:44 +0000344 p->program->NumTexIndirections++;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000345 p->temps_output = 0;
346 p->alu_temps = 0;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000347 }
348
349 return dest;
350}
351
352
Keith Whitwell47b29f52005-05-04 11:44:44 +0000353static struct ureg register_const4f( struct texenv_fragment_program *p,
354 GLfloat s0,
355 GLfloat s1,
356 GLfloat s2,
357 GLfloat s3)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000358{
Keith Whitwell47b29f52005-05-04 11:44:44 +0000359 GLfloat values[4];
360 GLuint idx;
361 values[0] = s0;
362 values[1] = s1;
363 values[2] = s2;
364 values[3] = s3;
365 idx = _mesa_add_unnamed_constant( p->program->Parameters, values );
366 return make_ureg(PROGRAM_STATE_VAR, idx);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000367}
368
Keith Whitwelle4902422005-05-11 15:16:35 +0000369#define register_scalar_const(p, s0) register_const4f(p, s0, s0, s0, s0)
Keith Whitwell47b29f52005-05-04 11:44:44 +0000370#define register_const1f(p, s0) register_const4f(p, s0, 0, 0, 1)
371#define register_const2f(p, s0, s1) register_const4f(p, s0, s1, 0, 1)
372#define register_const3f(p, s0, s1, s2) register_const4f(p, s0, s1, s2, 1)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000373
374
375
Keith Whitwell15e75e02005-04-29 15:11:39 +0000376
377
378
379
380
381static void program_error( struct texenv_fragment_program *p, const char *msg )
382{
383 fprintf(stderr, "%s\n", msg);
384 p->error = 1;
385}
386
387
388static GLuint translate_tex_src_bit( struct texenv_fragment_program *p,
389 GLuint bit )
390{
391 switch (bit) {
392 case TEXTURE_1D_BIT: return TEXTURE_1D_INDEX;
393 case TEXTURE_2D_BIT: return TEXTURE_2D_INDEX;
394 case TEXTURE_RECT_BIT: return TEXTURE_RECT_INDEX;
395 case TEXTURE_3D_BIT: return TEXTURE_3D_INDEX;
396 case TEXTURE_CUBE_BIT: return TEXTURE_CUBE_INDEX;
397 default: program_error(p, "TexSrcBit"); return 0;
398 }
399}
400
401
402static struct ureg get_source( struct texenv_fragment_program *p,
403 GLenum src, GLuint unit )
404{
405 switch (src) {
406 case GL_TEXTURE:
407 if (is_undef(p->src_texture)) {
408
409 GLuint dim = translate_tex_src_bit( p, p->ctx->Texture.Unit[unit]._ReallyEnabled);
Keith Whitwell47b29f52005-05-04 11:44:44 +0000410 struct ureg texcoord = register_input(p, FRAG_ATTRIB_TEX0+unit);
Keith Whitwellecb6bfc2005-05-10 08:58:44 +0000411 struct ureg tmp = get_tex_temp( p );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000412
413 /* TODO: Use D0_MASK_XY where possible.
414 */
415 p->src_texture = emit_texld( p, FP_OPCODE_TXP,
416 tmp, WRITEMASK_XYZW,
417 unit, dim, texcoord );
418 }
419
420 return p->src_texture;
421
422 /* Crossbar: */
423 case GL_TEXTURE0:
424 case GL_TEXTURE1:
425 case GL_TEXTURE2:
426 case GL_TEXTURE3:
427 case GL_TEXTURE4:
428 case GL_TEXTURE5:
429 case GL_TEXTURE6:
430 case GL_TEXTURE7: {
431 return undef;
432 }
433
434 case GL_CONSTANT:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000435 return register_param2(p, STATE_TEXENV_COLOR, unit);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000436 case GL_PRIMARY_COLOR:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000437 return register_input(p, FRAG_ATTRIB_COL0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000438 case GL_PREVIOUS:
439 default:
Keith Whitwell47b29f52005-05-04 11:44:44 +0000440 if (is_undef(p->src_previous))
441 return register_input(p, FRAG_ATTRIB_COL0);
442 else
443 return p->src_previous;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000444 }
445}
446
447
448static struct ureg emit_combine_source( struct texenv_fragment_program *p,
449 GLuint mask,
450 GLuint unit,
451 GLenum source,
452 GLenum operand )
453{
454 struct ureg arg, src, one;
455
456 src = get_source(p, source, unit);
457
458 switch (operand) {
459 case GL_ONE_MINUS_SRC_COLOR:
460 /* Get unused tmp,
461 * Emit tmp = 1.0 - arg.xyzw
462 */
463 arg = get_temp( p );
Keith Whitwelle4902422005-05-11 15:16:35 +0000464 one = register_scalar_const(p, 1.0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000465 return emit_arith( p, FP_OPCODE_SUB, arg, mask, 0, one, src, undef);
466
467 case GL_SRC_ALPHA:
468 if (mask == WRITEMASK_W)
469 return src;
470 else
471 return swizzle1( src, W );
472 case GL_ONE_MINUS_SRC_ALPHA:
473 /* Get unused tmp,
474 * Emit tmp = 1.0 - arg.wwww
475 */
476 arg = get_temp( p );
Keith Whitwelle4902422005-05-11 15:16:35 +0000477 one = register_scalar_const(p, 1.0);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000478 return emit_arith( p, FP_OPCODE_SUB, arg, mask, 0,
479 one, swizzle1(src, W), undef);
480 case GL_SRC_COLOR:
481 default:
482 return src;
483 }
484}
485
486
487
488static int nr_args( GLenum mode )
489{
490 switch (mode) {
491 case GL_REPLACE: return 1;
492 case GL_MODULATE: return 2;
493 case GL_ADD: return 2;
494 case GL_ADD_SIGNED: return 2;
495 case GL_INTERPOLATE: return 3;
496 case GL_SUBTRACT: return 2;
497 case GL_DOT3_RGB_EXT: return 2;
498 case GL_DOT3_RGBA_EXT: return 2;
499 case GL_DOT3_RGB: return 2;
500 case GL_DOT3_RGBA: return 2;
501 default: return 0;
502 }
503}
504
505
506static GLboolean args_match( struct gl_texture_unit *texUnit )
507{
508 int i, nr = nr_args(texUnit->_CurrentCombine->ModeRGB);
509
510 for (i = 0 ; i < nr ; i++) {
511 if (texUnit->_CurrentCombine->SourceA[i] != texUnit->_CurrentCombine->SourceRGB[i])
512 return GL_FALSE;
513
514 switch(texUnit->_CurrentCombine->OperandA[i]) {
515 case GL_SRC_ALPHA:
516 switch(texUnit->_CurrentCombine->OperandRGB[i]) {
517 case GL_SRC_COLOR:
518 case GL_SRC_ALPHA:
519 break;
520 default:
521 return GL_FALSE;
522 }
523 break;
524 case GL_ONE_MINUS_SRC_ALPHA:
525 switch(texUnit->_CurrentCombine->OperandRGB[i]) {
526 case GL_ONE_MINUS_SRC_COLOR:
527 case GL_ONE_MINUS_SRC_ALPHA:
528 break;
529 default:
530 return GL_FALSE;
531 }
532 break;
533 default:
534 return GL_FALSE; /* impossible */
535 }
536 }
537
538 return GL_TRUE;
539}
540
541
542static struct ureg emit_combine( struct texenv_fragment_program *p,
543 struct ureg dest,
544 GLuint mask,
545 GLuint saturate,
546 GLuint unit,
547 GLenum mode,
548 const GLenum *source,
549 const GLenum *operand)
550{
551 int nr = nr_args(mode);
552 struct ureg src[3];
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000553 struct ureg tmp, half;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000554 int i;
555
556 for (i = 0; i < nr; i++)
557 src[i] = emit_combine_source( p, mask, unit, source[i], operand[i] );
558
559 switch (mode) {
560 case GL_REPLACE:
561 if (mask == WRITEMASK_XYZW && !saturate)
562 return src[0];
563 else
564 return emit_arith( p, FP_OPCODE_MOV, dest, mask, saturate, src[0], undef, undef );
565 case GL_MODULATE:
566 return emit_arith( p, FP_OPCODE_MUL, dest, mask, saturate,
567 src[0], src[1], undef );
568 case GL_ADD:
569 return emit_arith( p, FP_OPCODE_ADD, dest, mask, saturate,
570 src[0], src[1], undef );
571 case GL_ADD_SIGNED:
572 /* tmp = arg0 + arg1
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000573 * result = tmp - .5
Keith Whitwell15e75e02005-04-29 15:11:39 +0000574 */
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000575 half = register_scalar_const(p, .5);
576 emit_arith( p, FP_OPCODE_ADD, tmp, mask, 0, src[0], src[1], undef );
577 emit_arith( p, FP_OPCODE_SUB, dest, mask, saturate, tmp, half, undef );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000578 return dest;
579 case GL_INTERPOLATE:
580 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) -- note arguments are reordered:
581 */
582 return emit_arith( p, FP_OPCODE_LRP, dest, mask, saturate, src[2], src[0], src[1] );
583
584 case GL_SUBTRACT:
585 return emit_arith( p, FP_OPCODE_SUB, dest, mask, saturate, src[0], src[1], undef );
586
587 case GL_DOT3_RGBA:
588 case GL_DOT3_RGBA_EXT:
589 case GL_DOT3_RGB_EXT:
590 case GL_DOT3_RGB: {
591 struct ureg tmp0 = get_temp( p );
592 struct ureg tmp1 = get_temp( p );
Keith Whitwelle4902422005-05-11 15:16:35 +0000593 struct ureg neg1 = register_scalar_const(p, -1);
594 struct ureg two = register_scalar_const(p, 2);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000595
596 /* tmp0 = 2*src0 - 1
597 * tmp1 = 2*src1 - 1
598 *
599 * dst = tmp0 dot3 tmp1
600 */
601 emit_arith( p, FP_OPCODE_MAD, tmp0, WRITEMASK_XYZW, 0,
602 two, src[0], neg1);
603
604 if (memcmp(&src[0], &src[1], sizeof(struct ureg)) == 0)
605 tmp1 = tmp0;
606 else
607 emit_arith( p, FP_OPCODE_MAD, tmp1, WRITEMASK_XYZW, 0,
608 two, src[1], neg1);
609 emit_arith( p, FP_OPCODE_DP3, dest, mask, saturate, tmp0, tmp1, undef);
610 return dest;
611 }
612
613 default:
614 return src[0];
615 }
616}
617
Keith Whitwell15e75e02005-04-29 15:11:39 +0000618
619
620static struct ureg emit_texenv( struct texenv_fragment_program *p, int unit )
621{
622 struct gl_texture_unit *texUnit = &p->ctx->Texture.Unit[unit];
623 GLuint saturate = (unit < p->last_tex_stage);
624 GLuint rgb_shift, alpha_shift;
625 struct ureg out, shift;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000626 struct ureg dest;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000627
628 if (!texUnit->_ReallyEnabled) {
629 return get_source(p, GL_PREVIOUS, 0);
630 }
631
632 switch (texUnit->_CurrentCombine->ModeRGB) {
633 case GL_DOT3_RGB_EXT:
634 alpha_shift = texUnit->_CurrentCombine->ScaleShiftA;
635 rgb_shift = 0;
636 break;
637
638 case GL_DOT3_RGBA_EXT:
639 alpha_shift = 0;
640 rgb_shift = 0;
641 break;
642
643 default:
644 rgb_shift = texUnit->_CurrentCombine->ScaleShiftRGB;
645 alpha_shift = texUnit->_CurrentCombine->ScaleShiftA;
646 break;
647 }
648
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000649 /* If this is the very last calculation, emit direct to output reg:
650 */
651 if ((p->ctx->_TriangleCaps & DD_SEPARATE_SPECULAR) ||
652 unit != p->last_tex_stage ||
653 alpha_shift ||
654 rgb_shift)
655 dest = get_temp( p );
656 else
657 dest = make_ureg(PROGRAM_OUTPUT, FRAG_OUTPUT_COLR);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000658
659 /* Emit the RGB and A combine ops
660 */
661 if (texUnit->_CurrentCombine->ModeRGB == texUnit->_CurrentCombine->ModeA &&
662 args_match( texUnit )) {
663 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
664 unit,
665 texUnit->_CurrentCombine->ModeRGB,
666 texUnit->_CurrentCombine->SourceRGB,
667 texUnit->_CurrentCombine->OperandRGB );
668 }
669 else if (texUnit->_CurrentCombine->ModeRGB == GL_DOT3_RGBA_EXT ||
670 texUnit->_CurrentCombine->ModeRGB == GL_DOT3_RGBA) {
671
672 out = emit_combine( p, dest, WRITEMASK_XYZW, saturate,
673 unit,
674 texUnit->_CurrentCombine->ModeRGB,
675 texUnit->_CurrentCombine->SourceRGB,
676 texUnit->_CurrentCombine->OperandRGB );
677 }
678 else {
679 /* Need to do something to stop from re-emitting identical
680 * argument calculations here:
681 */
682 out = emit_combine( p, dest, WRITEMASK_XYZ, saturate,
683 unit,
684 texUnit->_CurrentCombine->ModeRGB,
685 texUnit->_CurrentCombine->SourceRGB,
686 texUnit->_CurrentCombine->OperandRGB );
687 out = emit_combine( p, dest, WRITEMASK_W, saturate,
688 unit,
689 texUnit->_CurrentCombine->ModeA,
690 texUnit->_CurrentCombine->SourceA,
691 texUnit->_CurrentCombine->OperandA );
692 }
693
694 /* Deal with the final shift:
695 */
696 if (alpha_shift || rgb_shift) {
697 if (rgb_shift == alpha_shift) {
Keith Whitwelle4902422005-05-11 15:16:35 +0000698 shift = register_scalar_const(p, 1<<rgb_shift);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000699 }
700 else {
Keith Whitwell47b29f52005-05-04 11:44:44 +0000701 shift = register_const2f(p, 1<<rgb_shift, 1<<alpha_shift);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000702 shift = swizzle(shift,X,X,X,Y);
703 }
704 return emit_arith( p, FP_OPCODE_MUL, dest, WRITEMASK_XYZW,
705 saturate, out, shift, undef );
706 }
707 else
708 return out;
709}
710
711void _mesa_UpdateTexEnvProgram( GLcontext *ctx )
712{
713 struct texenv_fragment_program p;
714 GLuint unit;
715 struct ureg cf, out;
Keith Whitwelle4902422005-05-11 15:16:35 +0000716 GLuint db_NumInstructions = 0;
717 struct fp_instruction *db_Instructions = NULL;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000718
Keith Whitwell47b29f52005-05-04 11:44:44 +0000719 if (ctx->FragmentProgram._Enabled)
720 return;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000721
Keith Whitwell8b88f622005-05-10 11:39:50 +0000722 if (!ctx->_TexEnvProgram)
723 ctx->FragmentProgram._Current = ctx->_TexEnvProgram =
724 (struct fragment_program *)
725 ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
Keith Whitwell276330b2005-05-09 17:58:13 +0000726
Keith Whitwelle4902422005-05-11 15:16:35 +0000727 _mesa_memset(&p, 0, sizeof(p));
Keith Whitwell47b29f52005-05-04 11:44:44 +0000728 p.ctx = ctx;
Keith Whitwell276330b2005-05-09 17:58:13 +0000729 p.program = ctx->_TexEnvProgram;
Keith Whitwell47b29f52005-05-04 11:44:44 +0000730
Keith Whitwelle4902422005-05-11 15:16:35 +0000731 if (ctx->Driver.ProgramStringNotify || DISASSEM) {
732 db_Instructions = p.program->Instructions;
733 db_NumInstructions = p.program->Base.NumInstructions;
734 p.program->Instructions = NULL;
735 }
Keith Whitwell15e75e02005-04-29 15:11:39 +0000736
Keith Whitwelle4902422005-05-11 15:16:35 +0000737 if (!p.program->Instructions)
738 p.program->Instructions = MALLOC(sizeof(struct fp_instruction) * 100);
739
Keith Whitwell47b29f52005-05-04 11:44:44 +0000740 p.program->Base.NumInstructions = 0;
Keith Whitwell276330b2005-05-09 17:58:13 +0000741 p.program->Base.Target = GL_FRAGMENT_PROGRAM_ARB;
Keith Whitwell47b29f52005-05-04 11:44:44 +0000742 p.program->NumTexIndirections = 1; /* correct? */
743 p.program->NumTexInstructions = 0;
744 p.program->NumAluInstructions = 0;
Keith Whitwell9ca88152005-05-10 09:56:02 +0000745 p.program->Base.String = 0;
746 p.program->Base.NumInstructions =
747 p.program->Base.NumTemporaries =
748 p.program->Base.NumParameters =
749 p.program->Base.NumAttributes = p.program->Base.NumAddressRegs = 0;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000750
Keith Whitwell9ca88152005-05-10 09:56:02 +0000751 if (p.program->Parameters)
Keith Whitwelldbeea252005-05-10 13:57:50 +0000752 _mesa_free_parameters(p.program->Parameters);
753 else
754 p.program->Parameters = _mesa_new_parameter_list();
755
Keith Whitwell9ca88152005-05-10 09:56:02 +0000756 p.program->InputsRead = 0;
Keith Whitwellcf4f3c52005-05-16 12:15:01 +0000757 p.program->OutputsWritten = 1 << FRAG_OUTPUT_COLR;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000758
759 p.src_texture = undef;
Keith Whitwell47b29f52005-05-04 11:44:44 +0000760 p.src_previous = undef;
Keith Whitwell15e75e02005-04-29 15:11:39 +0000761 p.last_tex_stage = 0;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000762 release_temps(&p);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000763
764 if (ctx->Texture._EnabledUnits) {
765 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits ; unit++)
766 if (ctx->Texture.Unit[unit]._ReallyEnabled) {
767 p.last_tex_stage = unit;
768 }
769
770 for (unit = 0 ; unit < ctx->Const.MaxTextureUnits; unit++)
771 if (ctx->Texture.Unit[unit]._ReallyEnabled) {
772 p.src_previous = emit_texenv( &p, unit );
773 p.src_texture = undef;
Keith Whitwell93cd9232005-05-11 08:30:23 +0000774 release_temps(&p); /* release all temps */
775 if (p.src_previous.file == PROGRAM_TEMPORARY)
776 p.temp_in_use |= 1 << p.src_previous.idx; /* except for this one */
Keith Whitwell15e75e02005-04-29 15:11:39 +0000777 }
778 }
779
780 cf = get_source( &p, GL_PREVIOUS, 0 );
Keith Whitwell47b29f52005-05-04 11:44:44 +0000781 out = make_ureg( PROGRAM_OUTPUT, FRAG_OUTPUT_COLR );
Keith Whitwell15e75e02005-04-29 15:11:39 +0000782
783 if (ctx->_TriangleCaps & DD_SEPARATE_SPECULAR) {
784 /* Emit specular add.
785 */
Keith Whitwell47b29f52005-05-04 11:44:44 +0000786 struct ureg s = register_input(&p, FRAG_ATTRIB_COL1);
Keith Whitwell15e75e02005-04-29 15:11:39 +0000787 emit_arith( &p, FP_OPCODE_ADD, out, WRITEMASK_XYZ, 0, cf, s, undef );
788 }
789 else if (memcmp(&cf, &out, sizeof(cf)) != 0) {
790 /* Will wind up in here if no texture enabled or a couple of
791 * other scenarios (GL_REPLACE for instance).
792 */
793 emit_arith( &p, FP_OPCODE_MOV, out, WRITEMASK_XYZW, 0, cf, undef, undef );
794 }
795
Keith Whitwell47b29f52005-05-04 11:44:44 +0000796 /* Finish up:
797 */
798 emit_arith( &p, FP_OPCODE_END, undef, WRITEMASK_XYZW, 0, undef, undef, undef);
799
Keith Whitwell276330b2005-05-09 17:58:13 +0000800 if (ctx->Fog.Enabled)
801 p.program->FogOption = ctx->Fog.Mode;
802 else
803 p.program->FogOption = GL_NONE;
Keith Whitwell47b29f52005-05-04 11:44:44 +0000804
805 if (p.program->NumTexIndirections > ctx->Const.MaxFragmentProgramTexIndirections)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000806 program_error(&p, "Exceeded max nr indirect texture lookups");
807
Keith Whitwell47b29f52005-05-04 11:44:44 +0000808 if (p.program->NumTexInstructions > ctx->Const.MaxFragmentProgramTexInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000809 program_error(&p, "Exceeded max TEX instructions");
810
Keith Whitwell47b29f52005-05-04 11:44:44 +0000811 if (p.program->NumAluInstructions > ctx->Const.MaxFragmentProgramAluInstructions)
Keith Whitwell15e75e02005-04-29 15:11:39 +0000812 program_error(&p, "Exceeded max ALU instructions");
813
Keith Whitwell8b88f622005-05-10 11:39:50 +0000814
Keith Whitwelldbeea252005-05-10 13:57:50 +0000815 /* Notify driver the fragment program has (actually) changed.
Keith Whitwell8b88f622005-05-10 11:39:50 +0000816 */
Keith Whitwelle4902422005-05-11 15:16:35 +0000817 if (ctx->Driver.ProgramStringNotify || DISASSEM) {
818 if (db_Instructions == NULL ||
819 db_NumInstructions != p.program->Base.NumInstructions ||
820 memcmp(db_Instructions, p.program->Instructions,
821 db_NumInstructions * sizeof(*db_Instructions)) != 0) {
822
823 if (ctx->Driver.ProgramStringNotify)
824 ctx->Driver.ProgramStringNotify( ctx, GL_FRAGMENT_PROGRAM_ARB,
825 &p.program->Base );
Keith Whitwelldbeea252005-05-10 13:57:50 +0000826
Keith Whitwelle4902422005-05-11 15:16:35 +0000827 if (DISASSEM) {
828 _mesa_debug_fp_inst(p.program->NumTexInstructions + p.program->NumAluInstructions,
829 p.program->Instructions);
830 _mesa_printf("\n");
831 }
832 }
833
834 FREE(db_Instructions);
835 }
Keith Whitwell15e75e02005-04-29 15:11:39 +0000836}
837
838