summaryrefslogtreecommitdiff
path: root/src/mesa/swrast/s_fragprog_to_c.c
blob: f4bae2a82d000d4c0fccb5517ab163d1694829f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
/*
 * Mesa 3-D graphics library
 * Version:  6.1
 *
 * Copyright (C) 1999-2004  Brian Paul   All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/* An amusing little utility to print ARB fragment programs out as a C
 * function.   Resulting code not tested except visually.
 */


#include "glheader.h"
#include "colormac.h"
#include "context.h"
#include "nvfragprog.h"
#include "macros.h"
#include "program.h"

#include "s_nvfragprog.h"
#include "s_span.h"
#include "s_texture.h"


/* UREG - a way of representing an FP source register including
 * swizzling and negation in a single GLuint.  Major flaw is the
 * limitiation to source->Index < 32.  Secondary flaw is the fact that
 * it's overkill & we could probably just pass around the original
 * datatypes instead.
 */

#define UREG_TYPE_TEMP              0
#define UREG_TYPE_INTERP            1
#define UREG_TYPE_LOCAL_CONST       2
#define UREG_TYPE_ENV_CONST         3
#define UREG_TYPE_STATE_CONST       4
#define UREG_TYPE_PARAM             5
#define UREG_TYPE_OUTPUT            6
#define UREG_TYPE_MASK              0x7

#define UREG_TYPE_SHIFT               29
#define UREG_NR_SHIFT                 24
#define UREG_NR_MASK                  0x1f /* 31 */
#define UREG_CHANNEL_X_NEGATE_SHIFT   23
#define UREG_CHANNEL_X_SHIFT          20
#define UREG_CHANNEL_Y_NEGATE_SHIFT   19
#define UREG_CHANNEL_Y_SHIFT          16
#define UREG_CHANNEL_Z_NEGATE_SHIFT   15
#define UREG_CHANNEL_Z_SHIFT          12
#define UREG_CHANNEL_W_NEGATE_SHIFT   11
#define UREG_CHANNEL_W_SHIFT          8
#define UREG_CHANNEL_ZERO_NEGATE_MBZ  5
#define UREG_CHANNEL_ZERO_SHIFT       4      
#define UREG_CHANNEL_ONE_NEGATE_MBZ   1
#define UREG_CHANNEL_ONE_SHIFT        0      

#define UREG_BAD          0xffffffff /* not a valid ureg */

#define _X    0
#define _Y    1
#define _Z    2
#define _W    3
#define _ZERO 4			/* NOTE! */
#define _ONE  5			/* NOTE! */


/* Construct a ureg:
 */
#define UREG( type, nr ) (((type)<< UREG_TYPE_SHIFT) |		\
			  ((nr)  << UREG_NR_SHIFT) |		\
			  (_X     << UREG_CHANNEL_X_SHIFT) |	\
			  (_Y     << UREG_CHANNEL_Y_SHIFT) |	\
			  (_Z     << UREG_CHANNEL_Z_SHIFT) |	\
			  (_W     << UREG_CHANNEL_W_SHIFT) |	\
			  (_ZERO  << UREG_CHANNEL_ZERO_SHIFT) |	\
			  (_ONE   << UREG_CHANNEL_ONE_SHIFT))

#define GET_CHANNEL_SRC( reg, channel ) ((reg<<(channel*4)) & \
                                         (0xf<<UREG_CHANNEL_X_SHIFT))
#define CHANNEL_SRC( src, channel ) (src>>(channel*4))

#define GET_UREG_TYPE(reg) (((reg)>>UREG_TYPE_SHIFT)&UREG_TYPE_MASK)
#define GET_UREG_NR(reg)   (((reg)>>UREG_NR_SHIFT)&UREG_NR_MASK)



#define UREG_XYZW_CHANNEL_MASK 0x00ffff00

#define deref(reg,pos) swizzle(reg, pos, pos, pos, pos)


static INLINE int is_swizzled( int reg )
{
   return ((reg & UREG_XYZW_CHANNEL_MASK) != 
	   (UREG(0,0) & UREG_XYZW_CHANNEL_MASK));
}


/* One neat thing about the UREG representation:  
 */
static INLINE int swizzle( int reg, int x, int y, int z, int w )
{
   return ((reg & ~UREG_XYZW_CHANNEL_MASK) |
	   CHANNEL_SRC( GET_CHANNEL_SRC( reg, x ), 0 ) |
	   CHANNEL_SRC( GET_CHANNEL_SRC( reg, y ), 1 ) |
	   CHANNEL_SRC( GET_CHANNEL_SRC( reg, z ), 2 ) |
	   CHANNEL_SRC( GET_CHANNEL_SRC( reg, w ), 3 ));
}

/* Another neat thing about the UREG representation:  
 */
static INLINE int negate( int reg, int x, int y, int z, int w )
{
   return reg ^ (((x&1)<<UREG_CHANNEL_X_NEGATE_SHIFT)|
		 ((y&1)<<UREG_CHANNEL_Y_NEGATE_SHIFT)|
		 ((z&1)<<UREG_CHANNEL_Z_NEGATE_SHIFT)|
		 ((w&1)<<UREG_CHANNEL_W_NEGATE_SHIFT));
}



static GLuint src_reg_file( GLuint file )
{
   switch (file) {
   case PROGRAM_TEMPORARY: return UREG_TYPE_TEMP;
   case PROGRAM_INPUT: return UREG_TYPE_INTERP;
   case PROGRAM_LOCAL_PARAM: return UREG_TYPE_LOCAL_CONST;
   case PROGRAM_ENV_PARAM: return UREG_TYPE_ENV_CONST;

   case PROGRAM_STATE_VAR: return UREG_TYPE_STATE_CONST;
   case PROGRAM_NAMED_PARAM: return UREG_TYPE_PARAM; 
   default: return UREG_BAD;
   }
}

static void emit( struct fragment_program *p,
		  const char *fmt,
		  ... )
{
   va_list ap;
   va_start( ap, fmt );  

   p->c_strlen += vsnprintf( p->c_str + p->c_strlen, 
			     sizeof(p->c_str) - p->c_strlen,
			     fmt, ap );
   
   va_end( ap );
}

static INLINE void emit_char( struct fragment_program *p, char c )
{
   if (p->c_strlen < sizeof(p->c_str))
       p->c_str[p->c_strlen] = c;
   
   p->c_strlen++;
}


/**
 * Retrieve a ureg for the given source register.  Will emit
 * constants, apply swizzling and negation as needed.
 */
static GLuint src_vector( const struct fp_src_register *source )
{
   GLuint src;

   assert(source->Index < 32);	/* limitiation of UREG representation */

   src = UREG( src_reg_file( source->File ), source->Index );

   src = swizzle(src, 
		 _X + source->Swizzle[0],
		 _X + source->Swizzle[1],
		 _X + source->Swizzle[2],
		 _X + source->Swizzle[3]);

   if (source->NegateBase)
      src = negate( src, 1,1,1,1 );

   return src;
}


static void print_header( struct fragment_program *p )
{
   emit(p, "\n\n\n");

   /* Texture samplers, not written yet:
    */
   emit(p, "extern void TEX( void *ctx, const float *txc, int unit, float *rslt );\n"
	  "extern void TXB( void *ctx, const float *txc, int unit, float *rslt );\n"
	  "extern void TXP( void *ctx, const float *txc, int unit, float *rslt );\n");

   /* Resort to the standard math library (float versions):
    */
   emit(p, "extern float fabsf( float );\n"
	  "extern float cosf( float );\n"
	  "extern float sinf( float );\n"
	  "extern float expf( float );\n"
	  "extern float powf( float, float );\n"
	  "extern float floorf( float );\n");

   /* These ones we have fast code in Mesa for:
    */
   emit(p, "extern float LOG2( float );\n"
	  "extern float _mesa_inv_sqrtf( float );\n");

   /* The usual macros, not really needed, but handy:
    */
   emit(p, "#define MIN2(x,y) ((x)<(y)?(x):(y))\n"
	  "#define MAX2(x,y) ((x)<(y)?(x):(y))\n");

   /* Our function!
    */
   emit(p, "void run_program( void *ctx, \n"
	  "                  const float (*local_param)[4], \n"
	  "                  const float (*env_param)[4], \n"
	  "                  const float (*state_param)[4], \n"
	  "                  const float (*interp)[4], \n"
	  "                  float (*outputs)[4])\n"
	  "{\n"
	  "   float temp[32][4];\n"
      );
}

static void print_footer( struct fragment_program *p )
{
   emit(p, "}\n");
}

static void print_dest_reg( struct fragment_program *p, 
			    const struct fp_instruction *inst )
{
   switch (inst->DstReg.File) {
   case PROGRAM_OUTPUT:
      emit(p, "outputs[%d]", inst->DstReg.Index);
      break;
   case PROGRAM_TEMPORARY:
      emit(p, "temp[%d]", inst->DstReg.Index);
      break;
   default:
      break;
   }
}

static void print_dest( struct fragment_program *p,
			const struct fp_instruction *inst,
			GLuint idx )
{
   print_dest_reg(p, inst);
   emit(p, "[%d]", idx);
}


#define UREG_SRC0(reg) (((reg)>>UREG_CHANNEL_X_SHIFT) & 0x7)

static void print_reg( struct fragment_program *p,
		       GLuint arg )
{
   switch (GET_UREG_TYPE(arg)) {
   case UREG_TYPE_TEMP: emit(p, "temp"); break;
   case UREG_TYPE_INTERP: emit(p, "interp"); break;
   case UREG_TYPE_LOCAL_CONST: emit(p, "local_const"); break;
   case UREG_TYPE_ENV_CONST: emit(p, "env_const"); break;
   case UREG_TYPE_STATE_CONST: emit(p, "state_const"); break;
   case UREG_TYPE_PARAM: emit(p, "param"); break;
   };
   
   emit(p, "[%d]", GET_UREG_NR(arg));
}


static void print_arg( struct fragment_program *p,
		       GLuint arg )
{
   GLuint src = UREG_SRC0(arg);

   if (src == _ZERO) {
      emit(p, "0");
      return;
   }

   if (arg & (1<<UREG_CHANNEL_X_NEGATE_SHIFT))
      emit(p, "-");

   if (src == _ONE) {
      emit(p, "1");
      return;
   }

   if (GET_UREG_TYPE(arg) == UREG_TYPE_STATE_CONST) {
      emit(p, "%g", p->Parameters->Parameters[GET_UREG_NR(arg)].Values[src]);
      return;
   }

   print_reg( p, arg );

   switch (src){
   case _X: emit(p, "[0]"); break;
   case _Y: emit(p, "[1]"); break;
   case _Z: emit(p, "[2]"); break;
   case _W: emit(p, "[3]"); break;
   }   
}


/* This is where the handling of expressions breaks down into string
 * processing:
 */
static void print_expression( struct fragment_program *p,
			      GLuint i,
			      const char *fmt,
			      va_list ap )
{
   while (*fmt) {
      if (*fmt == '%' && *(fmt+1) == 's') {
	 int reg = va_arg(ap, int);

	 /* Use of deref() is a bit of a hack:
	  */
	 print_arg( p, deref(reg, i) );
	 fmt += 2;
      }
      else { 
	 emit_char(p, *fmt); 
	 fmt++;
      }
   }

   emit(p, ";\n");
}

static void do_tex_simple( struct fragment_program *p,
			   const struct fp_instruction *inst,
			   const char *fn, GLuint texunit, GLuint arg )
{
   emit(p, "   %s( ctx, ", fn);
   print_reg( p, arg );
   emit(p, ", %d, ", texunit );
   print_dest_reg(p, inst);
   emit(p, ");\n");
}


static void do_tex( struct fragment_program *p,
		    const struct fp_instruction *inst,
		    const char *fn, GLuint texunit, GLuint arg )
{
   GLuint i;
   GLboolean need_tex = GL_FALSE, need_result = GL_FALSE;

   for (i = 0; i < 4; i++) 
      if (!inst->DstReg.WriteMask[i]) 
	 need_result = GL_TRUE;

   if (is_swizzled(arg))
      need_tex = GL_TRUE;

   if (!need_tex && !need_result) {
      do_tex_simple( p, inst, fn, texunit, arg );
      return;
   }

   emit(p, "   {\n");
   emit(p, "       float texcoord[4];\n");
   emit(p, "       float result[4];\n");

   for (i = 0; i < 4; i++) {
      emit(p, "      texcoord[%d] = ", i);
      print_arg( p, deref(arg, i) );
      emit(p, ";\n");
   }

   emit(p, "       %s( ctx, texcoord, %d, result);\n", fn, texunit );

   for (i = 0; i < 4; i++) {
      if (inst->DstReg.WriteMask[i]) {
	 emit(p, "      ");
	 print_dest(p, inst, i);
	 emit(p, " = result[%d];\n", i);
      }
   }

   emit(p, "   }\n");
}


static void saturate( struct fragment_program *p,
		      const struct fp_instruction *inst,
		      GLuint i )
{
   emit(p, "   ");
   print_dest(p, inst, i);
   emit(p, " = CLAMPF( ");
   print_dest(p, inst, i);
   emit(p, ", 0.0, 1.0);\n");
}
		     
static void assign_single( GLuint i,
			   struct fragment_program *p,
			   const struct fp_instruction *inst,
			   const char *fmt,
			   ... )
{
   va_list ap;
   va_start( ap, fmt );  

   if (inst->DstReg.WriteMask[i]) {
      emit(p, "   ");
      print_dest(p, inst, i);
      emit(p, " = ");
      print_expression( p, i, fmt, ap);
      if (inst->Saturate)
	 saturate(p, inst, i);
   }

   va_end( ap );
}

static void assign4( struct fragment_program *p,
		     const struct fp_instruction *inst,
		     const char *fmt,
		     ... )
{
   GLuint i;
   va_list ap;
   va_start( ap, fmt );  

   for (i = 0; i < 4; i++)
      if (inst->DstReg.WriteMask[i]) {
	 emit(p, "   ");
	 print_dest(p, inst, i);
	 emit(p, " = ");
	 print_expression( p, i, fmt, ap);
	 if (inst->Saturate)
	    saturate(p, inst, i);
      }

   va_end( ap );
}

static void assign4_replicate( struct fragment_program *p,
			       const struct fp_instruction *inst,
			       const char *fmt,
			       ... )
{
   GLuint i, first = 0;
   GLboolean ok = 0;
   va_list ap;

   for (i = 0; i < 4; i++)
      if (inst->DstReg.WriteMask[i]) {
	 ok = 1;
	 first = i;
	 break;
      }

   if (!ok) return;

   va_start( ap, fmt );  

   emit(p, "   ");

   print_dest(p, inst, first);
   emit(p, " = ");
   print_expression( p, 0, fmt, ap);
   if (inst->Saturate)
      saturate(p, inst, first);
   va_end( ap );

   for (i = first+1; i < 4; i++)
      if (inst->DstReg.WriteMask[i]) {
	 emit(p, "   ");
	 print_dest(p, inst, i);
	 emit(p, " = ");
	 print_dest(p, inst, first);
	 emit(p, ";\n");
      }
}
	 



static GLuint nr_args( GLuint opcode )
{
   switch (opcode) {
   case FP_OPCODE_ABS: return 1;
   case FP_OPCODE_ADD: return 2;
   case FP_OPCODE_CMP: return 3;
   case FP_OPCODE_COS: return 1;
   case FP_OPCODE_DP3: return 2;
   case FP_OPCODE_DP4: return 2;
   case FP_OPCODE_DPH: return 2;
   case FP_OPCODE_DST: return 2;
   case FP_OPCODE_EX2: return 1;
   case FP_OPCODE_FLR: return 1;
   case FP_OPCODE_FRC: return 1;
   case FP_OPCODE_KIL: return 1;
   case FP_OPCODE_LG2: return 1;
   case FP_OPCODE_LIT: return 1;
   case FP_OPCODE_LRP: return 3;
   case FP_OPCODE_MAD: return 3;
   case FP_OPCODE_MAX: return 2;
   case FP_OPCODE_MIN: return 2;
   case FP_OPCODE_MOV: return 1;
   case FP_OPCODE_MUL: return 2;
   case FP_OPCODE_POW: return 2;
   case FP_OPCODE_RCP: return 1;
   case FP_OPCODE_RSQ: return 1;
   case FP_OPCODE_SCS: return 1;
   case FP_OPCODE_SGE: return 2;
   case FP_OPCODE_SIN: return 1;
   case FP_OPCODE_SLT: return 2;
   case FP_OPCODE_SUB: return 2;
   case FP_OPCODE_SWZ: return 1;
   case FP_OPCODE_TEX: return 1;
   case FP_OPCODE_TXB: return 1;
   case FP_OPCODE_TXP: return 1;
   case FP_OPCODE_XPD: return 2;
   default: return 0;
   }
}



static void translate_program( struct fragment_program *p )
{
   const struct fp_instruction *inst = p->Instructions;

   for (; inst->Opcode != FP_OPCODE_END; inst++) {

      GLuint src[3], i;
      GLuint nr = nr_args( inst->Opcode );
      
      for (i = 0; i < nr; i++) 
	 src[i] = src_vector( &inst->SrcReg[i] );

      /* Print the original program instruction string */
      if (p->Base.String)
      {
         const char *s = (const char *) p->Base.String + inst->StringPos;
         emit(p, "   /* ");
         while (*s != ';') {
            emit_char(p, *s);
            s++;
         }
         emit(p, "; */\n");
      }

      switch (inst->Opcode) {
      case FP_OPCODE_ABS: 
	 assign4(p, inst, "fabsf(%s)", src[0]);
	 break;

      case FP_OPCODE_ADD: 
	 assign4(p, inst, "%s + %s", src[0], src[1]);
	 break;

      case FP_OPCODE_CMP: 
	 assign4(p, inst, "%s < 0.0F ? %s : %s", src[0], src[1], src[2]);
	 break;

      case FP_OPCODE_COS:
	 assign4_replicate(p, inst, "COS(%s)", src[0]);
	 break;

      case FP_OPCODE_DP3: 
	 assign4_replicate(p, inst, 
			   "%s*%s + %s*%s + %s*%s", 
			   deref(src[0],_X),
			   deref(src[1],_X),
			   deref(src[0],_Y),
			   deref(src[1],_Y),
			   deref(src[0],_Z),
			   deref(src[1],_Z));
	 break;

      case FP_OPCODE_DP4: 
	 assign4_replicate(p, inst, 
			   "%s*%s + %s*%s + %s*%s + %s*%s", 
			   deref(src[0],_X),
			   deref(src[1],_X),
			   deref(src[0],_Y),
			   deref(src[1],_Y),
			   deref(src[0],_Z),
			   deref(src[1],_Z));
	 break;

      case FP_OPCODE_DPH:  
	 assign4_replicate(p, inst, 
			   "%s*%s + %s*%s + %s*%s + %s", 
			   deref(src[0],_X),
			   deref(src[1],_X),
			   deref(src[0],_Y),
			   deref(src[1],_Y),
			   deref(src[1],_Z));
	 break;

      case FP_OPCODE_DST: 
	 /* result[0] = 1    * 1;
	  * result[1] = a[1] * b[1];
	  * result[2] = a[2] * 1;
	  * result[3] = 1    * b[3];
	  */
	 assign_single(0, p, inst, "1.0");

	 assign_single(1, p, inst, "%s * %s", 
		       deref(src[0], _Y), deref(src[1], _Y));

	 assign_single(2, p, inst, "%s", deref(src[0], _Z));
	 assign_single(3, p, inst, "%s", deref(src[1], _W));
	 break;

      case FP_OPCODE_EX2: 
	 assign4_replicate(p, inst, "powf(2.0, %s)", src[0]);
	 break;

      case FP_OPCODE_FLR: 
	 assign4_replicate(p, inst, "floorf(%s)", src[0]);
	 break;

      case FP_OPCODE_FRC: 
	 assign4_replicate(p, inst, "%s - floorf(%s)", src[0], src[0]);
	 break;

      case FP_OPCODE_KIL:
	 /* TODO */
	 break;

      case FP_OPCODE_LG2: 
	 assign4_replicate(p, inst, "LOG2(%s)", src[0]);
	 break;

      case FP_OPCODE_LIT: 
	 assign_single(0, p, inst, "1.0");
	 assign_single(1, p, inst, "MIN2(%s, 0)", deref(src[0], _X));
	 assign_single(2, p, inst, "(%s > 0.0) ? expf(%s * MIN2(%s, 0)) : 0.0",
		       deref(src[0], _X),
		       deref(src[0], _Z),
		       deref(src[0], _Y));
	 assign_single(3, p, inst, "1.0");
	 break;

      case FP_OPCODE_LRP: 
	 assign4(p, inst, 
		 "%s * %s + (1.0 - %s) * %s", 
		 src[0], src[1], src[0], src[2]);
	 break;

      case FP_OPCODE_MAD:
	 assign4(p, inst, "%s * %s + %s", src[0], src[1], src[2]);
	 break;

      case FP_OPCODE_MAX:
	 assign4(p, inst, "MAX2(%s, %s)", src[0], src[1]);
	 break;

      case FP_OPCODE_MIN: 
	 assign4(p, inst, "MIN2(%s, %s)", src[0], src[1]);
	 break;

      case FP_OPCODE_MOV: 
	 assign4(p, inst, "%s", src[0]);
	 break;

      case FP_OPCODE_MUL: 
	 assign4(p, inst, "%s * %s", src[0], src[1]);
	 break;

      case FP_OPCODE_POW: 
	 assign4_replicate(p, inst, "powf(%s, %s)", src[0], src[1]);
	 break;

      case FP_OPCODE_RCP: 
	 assign4_replicate(p, inst, "1.0/%s", src[0]);
	 break;

      case FP_OPCODE_RSQ: 
	 assign4_replicate(p, inst, "_mesa_inv_sqrtf(%s)", src[0]);
	 break;
	 
      case FP_OPCODE_SCS:
	 if (inst->DstReg.WriteMask[0]) {
	    assign_single(0, p, inst, "cosf(%s)", deref(src[0], _X));
	 }

	 if (inst->DstReg.WriteMask[1]) {
	    assign_single(1, p, inst, "sinf(%s)", deref(src[0], _X));
	 }
	 break;

      case FP_OPCODE_SGE: 
	 assign4(p, inst, "%s >= %s ? 1.0 : 0.0", src[0], src[1]);
	 break;

      case FP_OPCODE_SIN:
	 assign4_replicate(p, inst, "sinf(%s)", src[0]);
	 break;

      case FP_OPCODE_SLT: 
	 assign4(p, inst, "%s < %s ? 1.0 : 0.0", src[0], src[1]);
	 break;

      case FP_OPCODE_SUB: 
	 assign4(p, inst, "%s - %s", src[0], src[1]);
	 break;

      case FP_OPCODE_SWZ: 	/* same implementation as MOV: */
	 assign4(p, inst, "%s", src[0]);
	 break;

      case FP_OPCODE_TEX: 
	 do_tex(p, inst, "TEX", inst->TexSrcUnit, src[0]);
	 break;

      case FP_OPCODE_TXB:
	 do_tex(p, inst, "TXB", inst->TexSrcUnit, src[0]);
	 break;

      case FP_OPCODE_TXP:
	 do_tex(p, inst, "TXP", inst->TexSrcUnit, src[0]);
	 break;

      case FP_OPCODE_XPD:
	 /* Cross product:
	  *      result.x = src[0].y * src[1].z - src[0].z * src[1].y;
	  *      result.y = src[0].z * src[1].x - src[0].x * src[1].z;
	  *      result.z = src[0].x * src[1].y - src[0].y * src[1].x;
	  *      result.w = undef;
	  */
	 assign4(p, inst, 
		 "%s * %s - %s * %s",
		 swizzle(src[0], _Y, _Z, _X, _ONE),
		 swizzle(src[1], _Z, _X, _Y, _ONE),
		 swizzle(src[0], _Z, _X, _Y, _ONE),
		 swizzle(src[1], _Y, _Z, _X, _ONE));
	 break;

      default:
	 emit(p, "BOGUS OPCODE\n");
	 return;
      }
   }
}





void _swrast_translate_program( GLcontext *ctx )
{
   struct fragment_program *p = ctx->FragmentProgram.Current;

   if (p) {
      p->c_strlen = 0;

      print_header( p );
      translate_program( p );
      print_footer( p );
      emit_char(p, 0);
      
      printf("C program length: %d/%d chars\n", p->c_strlen, strlen(p->c_str));
      printf(p->c_str);
   }
}