summaryrefslogtreecommitdiffstats
path: root/src/mesa/drivers/dri/i915/i915_fragprog.c
blob: 5cd6ea4de5190e1a213760f06f87e125d87f665e (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
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
/**************************************************************************
 * 
 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
 * 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, sub license, 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 (including the
 * next paragraph) 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 NON-INFRINGEMENT.
 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS 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.
 * 
 **************************************************************************/

#include "glheader.h"
#include "macros.h"
#include "enums.h"

#include "tnl/t_context.h"
#include "intel_batchbuffer.h"

#include "i915_reg.h"
#include "i915_context.h"
#include "i915_program.h"

#include "program_instruction.h"
#include "program.h"



/* 1, -1/3!, 1/5!, -1/7! */
static const GLfloat sin_constants[4] = {  1.0, 
					   -1.0/(3*2*1),
					   1.0/(5*4*3*2*1),
					   -1.0/(7*6*5*4*3*2*1) };

/* 1, -1/2!, 1/4!, -1/6! */
static const GLfloat cos_constants[4] = {  1.0, 
					   -1.0/(2*1),
					   1.0/(4*3*2*1),
					   -1.0/(6*5*4*3*2*1) };

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

   switch (source->File) {

      /* Registers:
       */
      case PROGRAM_TEMPORARY:
	 if (source->Index >= I915_MAX_TEMPORARY) {
	    i915_program_error( p, "Exceeded max temporary reg" );
	    return 0;
	 }
	 src = UREG( REG_TYPE_R, source->Index );
         break;
      case PROGRAM_INPUT:
	 switch (source->Index) {
	 case FRAG_ATTRIB_WPOS:
	    src = i915_emit_decl( p,  REG_TYPE_T, p->wpos_tex, D0_CHANNEL_ALL ); 
	    break;
	 case FRAG_ATTRIB_COL0:
	    src = i915_emit_decl( p,  REG_TYPE_T, T_DIFFUSE, D0_CHANNEL_ALL ); 
	    break;
	 case FRAG_ATTRIB_COL1:
	    src = i915_emit_decl( p,  REG_TYPE_T, T_SPECULAR, D0_CHANNEL_XYZ ); 
	    src = swizzle( src, X, Y, Z, ONE );
	    break;
	 case FRAG_ATTRIB_FOGC:
	    src = i915_emit_decl( p,  REG_TYPE_T, T_FOG_W, D0_CHANNEL_W ); 
	    src = swizzle( src, W, W, W, W );
	    break;
	 case FRAG_ATTRIB_TEX0:
	 case FRAG_ATTRIB_TEX1:
	 case FRAG_ATTRIB_TEX2:
	 case FRAG_ATTRIB_TEX3:
	 case FRAG_ATTRIB_TEX4:
	 case FRAG_ATTRIB_TEX5:
	 case FRAG_ATTRIB_TEX6:
	 case FRAG_ATTRIB_TEX7:
	    src = i915_emit_decl( p,  REG_TYPE_T, 
				 T_TEX0 + (source->Index - FRAG_ATTRIB_TEX0),
				 D0_CHANNEL_ALL ); 
	    break;

	 default:
	    i915_program_error( p, "Bad source->Index" ); 
	    return 0;
	 }
         break;

	 /* Various paramters and env values.  All emitted to
	  * hardware as program constants.
	  */
      case PROGRAM_LOCAL_PARAM:
         src = i915_emit_param4fv( 
	    p, program->Base.LocalParams[source->Index]);
	 break;

      case PROGRAM_ENV_PARAM:
         src = i915_emit_param4fv( 
	    p, p->ctx->FragmentProgram.Parameters[source->Index]);
	 break;

      case PROGRAM_STATE_VAR:
      case PROGRAM_NAMED_PARAM:
         src = i915_emit_param4fv( 
	    p, program->Base.Parameters->ParameterValues[source->Index] );
	 break;

      default:
	 i915_program_error( p, "Bad source->File" ); 
	 return 0;
   }

   src = swizzle(src, 
		 GET_SWZ(source->Swizzle, 0),
		 GET_SWZ(source->Swizzle, 1),
		 GET_SWZ(source->Swizzle, 2),
		 GET_SWZ(source->Swizzle, 3));

   if (source->NegateBase)
      src = negate( src, 
		    GET_BIT(source->NegateBase, 0),
		    GET_BIT(source->NegateBase, 1),
		    GET_BIT(source->NegateBase, 2),
		    GET_BIT(source->NegateBase, 3));

   return src;
}


static GLuint get_result_vector( struct i915_fragment_program *p,
				 const struct prog_instruction *inst )
{
   switch (inst->DstReg.File) {
   case PROGRAM_OUTPUT:
      switch (inst->DstReg.Index) {
      case FRAG_RESULT_COLR: 
	 return UREG(REG_TYPE_OC, 0);
      case FRAG_RESULT_DEPR: 
	 p->depth_written = 1;
	 return UREG(REG_TYPE_OD, 0);
      default: 
	 i915_program_error( p, "Bad inst->DstReg.Index" ); 
	 return 0;
      }
   case PROGRAM_TEMPORARY:
      return UREG(REG_TYPE_R, inst->DstReg.Index);
   default:
      i915_program_error( p, "Bad inst->DstReg.File" ); 
      return 0;
   }
}
   
static GLuint get_result_flags( const struct prog_instruction *inst )
{
   GLuint flags = 0;

   if (inst->SaturateMode == SATURATE_ZERO_ONE) flags |= A0_DEST_SATURATE;
   if (inst->DstReg.WriteMask & WRITEMASK_X) flags |= A0_DEST_CHANNEL_X;
   if (inst->DstReg.WriteMask & WRITEMASK_Y) flags |= A0_DEST_CHANNEL_Y;
   if (inst->DstReg.WriteMask & WRITEMASK_Z) flags |= A0_DEST_CHANNEL_Z;
   if (inst->DstReg.WriteMask & WRITEMASK_W) flags |= A0_DEST_CHANNEL_W;

   return flags;
}

static GLuint translate_tex_src_target( struct i915_fragment_program *p,
				     GLubyte bit )
{
   switch (bit) {
   case TEXTURE_1D_INDEX:   return D0_SAMPLE_TYPE_2D;
   case TEXTURE_2D_INDEX:   return D0_SAMPLE_TYPE_2D;
   case TEXTURE_RECT_INDEX: return D0_SAMPLE_TYPE_2D;
   case TEXTURE_3D_INDEX:   return D0_SAMPLE_TYPE_VOLUME;
   case TEXTURE_CUBE_INDEX: return D0_SAMPLE_TYPE_CUBE;
   default: i915_program_error(p, "TexSrcBit"); return 0;
   }
}

#define EMIT_TEX( OP )						\
do {								\
   GLuint dim = translate_tex_src_target( p, inst->TexSrcTarget );	\
   GLuint sampler = i915_emit_decl(p, REG_TYPE_S,		\
				  inst->TexSrcUnit, dim);	\
   GLuint coord = src_vector( p, &inst->SrcReg[0], program);	\
   /* Texel lookup */						\
								\
   i915_emit_texld( p,						\
	       get_result_vector( p, inst ),			\
	       get_result_flags( inst ),			\
	       sampler,						\
	       coord,						\
	       OP);						\
} while (0)

#define EMIT_ARITH( OP, N )						\
do {									\
   i915_emit_arith( p,							\
	       OP,							\
	       get_result_vector( p, inst ), 				\
	       get_result_flags( inst ), 0,			\
	       (N<1)?0:src_vector( p, &inst->SrcReg[0], program),	\
	       (N<2)?0:src_vector( p, &inst->SrcReg[1], program),	\
	       (N<3)?0:src_vector( p, &inst->SrcReg[2], program));	\
} while (0)

#define EMIT_1ARG_ARITH( OP ) EMIT_ARITH( OP, 1 )
#define EMIT_2ARG_ARITH( OP ) EMIT_ARITH( OP, 2 )
#define EMIT_3ARG_ARITH( OP ) EMIT_ARITH( OP, 3 )


/* Possible concerns:
 *
 * SIN, COS -- could use another taylor step?
 * LIT      -- results seem a little different to sw mesa
 * LOG      -- different to mesa on negative numbers, but this is conformant.
 * 
 * Parse failures -- Mesa doesn't currently give a good indication
 * internally whether a particular program string parsed or not.  This
 * can lead to confusion -- hopefully we cope with it ok now.
 *
 */
static void upload_program( struct i915_fragment_program *p )
{
   const struct fragment_program *program = p->ctx->FragmentProgram._Current;
   const struct prog_instruction *inst = program->Base.Instructions;

/*    _mesa_debug_fp_inst(program->Base.NumInstructions, inst); */

   /* Is this a parse-failed program?  Ensure a valid program is
    * loaded, as the flagging of an error isn't sufficient to stop
    * this being uploaded to hardware.
    */
   if (inst[0].Opcode == OPCODE_END) {
      GLuint tmp = i915_get_utemp( p );
      i915_emit_arith( p,
		      A0_MOV,
		      UREG(REG_TYPE_OC, 0), 
		      A0_DEST_CHANNEL_ALL, 0,
		      swizzle(tmp,ONE,ZERO,ONE,ONE), 0, 0);
      return;
   }

   while (1) {
      GLuint src0, src1, src2, flags;
      GLuint tmp = 0;

      switch (inst->Opcode) {
      case OPCODE_ABS: 
	 src0 = src_vector( p, &inst->SrcReg[0], program);
	 i915_emit_arith( p, 
			 A0_MAX,
			 get_result_vector( p, inst ), 
			 get_result_flags( inst ), 0,
			 src0, negate(src0, 1,1,1,1), 0);
	 break;

      case OPCODE_ADD: 
	 EMIT_2ARG_ARITH( A0_ADD );
	 break;

      case OPCODE_CMP: 
	 src0 = src_vector( p, &inst->SrcReg[0], program);
	 src1 = src_vector( p, &inst->SrcReg[1], program);
	 src2 = src_vector( p, &inst->SrcReg[2], program);
	 i915_emit_arith( p, 
			 A0_CMP,
			 get_result_vector( p, inst ), 
			 get_result_flags( inst ), 0,
			 src0, src2, src1);	/* NOTE: order of src2, src1 */
	 break;

      case OPCODE_COS:
	 src0 = src_vector( p, &inst->SrcReg[0], program);
	 tmp = i915_get_utemp( p );

	 i915_emit_arith( p, 
			 A0_MUL,
			 tmp, A0_DEST_CHANNEL_X, 0,
			 src0, 
			 i915_emit_const1f(p, 1.0/(M_PI * 2)),
			 0);

	 i915_emit_arith( p, 
			 A0_MOD,
			 tmp, A0_DEST_CHANNEL_X, 0,
			 tmp, 
			 0, 0 );

	 /* By choosing different taylor constants, could get rid of this mul:
	  */
	 i915_emit_arith( p, 
			 A0_MUL,
			 tmp, A0_DEST_CHANNEL_X, 0,
			 tmp, 
			 i915_emit_const1f(p, (M_PI * 2)),
			 0);

	 /* 
	  * t0.xy = MUL x.xx11, x.x1111  ; x^2, x, 1, 1
	  * t0 = MUL t0.xyxy t0.xx11 ; x^4, x^3, x^2, 1
	  * t0 = MUL t0.xxz1 t0.z111    ; x^6 x^4 x^2 1
	  * result = DP4 t0, cos_constants
	  */
	 i915_emit_arith( p, 
			 A0_MUL,
			 tmp, A0_DEST_CHANNEL_XY, 0,
			 swizzle(tmp, X,X,ONE,ONE), 
			 swizzle(tmp, X,ONE,ONE,ONE), 0);

	 i915_emit_arith( p, 
			 A0_MUL,
			 tmp, A0_DEST_CHANNEL_XYZ, 0,
			 swizzle(tmp, X,Y,X,ONE), 
			 swizzle(tmp, X,X,ONE,ONE), 0);

	 i915_emit_arith( p, 
			 A0_MUL,
			 tmp, A0_DEST_CHANNEL_XYZ, 0,
			 swizzle(tmp, X,X,Z,ONE), 
			 swizzle(tmp, Z,ONE,ONE,ONE), 0);
	    
	 i915_emit_arith( p, 
			 A0_DP4,
			 get_result_vector( p, inst ), 
			 get_result_flags( inst ), 0,
			 swizzle(tmp, ONE,Z,Y,X),
			 i915_emit_const4fv( p, cos_constants ), 0);

	 break;

      case OPCODE_DP3: 
	 EMIT_2ARG_ARITH( A0_DP3 );
	 break;

      case OPCODE_DP4: 
	 EMIT_2ARG_ARITH( A0_DP4 );
	 break;

      case OPCODE_DPH:  
	 src0 = src_vector( p, &inst->SrcReg[0], program);
	 src1 = src_vector( p, &inst->SrcReg[1], program);

	 i915_emit_arith( p, 
			 A0_DP4,
			 get_result_vector( p, inst ), 
			 get_result_flags( inst ), 0,
			 swizzle(src0, X,Y,Z,ONE), src1, 0);
	 break;

      case OPCODE_DST: 
	 src0 = src_vector( p, &inst->SrcReg[0], program);
	 src1 = src_vector( p, &inst->SrcReg[1], program);

	 /* result[0] = 1    * 1;
	  * result[1] = a[1] * b[1];
	  * result[2] = a[2] * 1;
	  * result[3] = 1    * b[3];
	  */
	 i915_emit_arith( p, 
			 A0_MUL,
			 get_result_vector( p, inst ), 
			 get_result_flags( inst ), 0,
			 swizzle(src0, ONE, Y, Z,   ONE), 
			 swizzle(src1, ONE, Y, ONE, W  ),
			 0);
	 break;

      case OPCODE_EX2: 
	 src0 = src_vector( p, &inst->SrcReg[0], program);

	 i915_emit_arith( p, 
			 A0_EXP,
			 get_result_vector( p, inst ), 
			 get_result_flags( inst ), 0,
			 swizzle(src0,X,X,X,X), 0, 0);
	 break;

      case OPCODE_FLR: 
	 EMIT_1ARG_ARITH( A0_FLR );
	 break;

      case OPCODE_FRC: 
	 EMIT_1ARG_ARITH( A0_FRC );
	 break;

      case OPCODE_KIL:
	 src0 = src_vector( p, &inst->SrcReg[0], program);
	 tmp = i915_get_utemp( p );

	 i915_emit_texld( p,
			 tmp, A0_DEST_CHANNEL_ALL, /* use a dummy dest reg */
			 0,
			 src0,
			 T0_TEXKILL );
	 break;

      case OPCODE_LG2: 
	 src0 = src_vector( p, &inst->SrcReg[0], program);

	 i915_emit_arith( p, 
			 A0_LOG,
			 get_result_vector( p, inst ), 
			 get_result_flags( inst ), 0,
			 swizzle(src0,X,X,X,X), 0, 0);
	 break;

      case OPCODE_LIT: 
	 src0 = src_vector( p, &inst->SrcReg[0], program);
	 tmp = i915_get_utemp( p );

	 /* tmp = max( a.xyzw, a.00zw )
	  * XXX: Clamp tmp.w to -128..128
	  * tmp.y = log(tmp.y)
	  * tmp.y = tmp.w * tmp.y
	  * tmp.y = exp(tmp.y)
	  * result = cmp (a.11-x1, a.1x01, a.1xy1 )
	  */
	 i915_emit_arith( p, A0_MAX, tmp, A0_DEST_CHANNEL_ALL, 0, 
			 src0, swizzle(src0, ZERO, ZERO, Z, W), 0 );

	 i915_emit_arith( p, A0_LOG, tmp, A0_DEST_CHANNEL_Y, 0, 
			 swizzle(tmp, Y, Y, Y, Y), 0, 0 );

	 i915_emit_arith( p, A0_MUL, tmp, A0_DEST_CHANNEL_Y, 0, 
			 swizzle(tmp, ZERO, Y, ZERO, ZERO), 
			 swizzle(tmp, ZERO, W, ZERO, ZERO), 0 );

	 i915_emit_arith( p, A0_EXP, tmp, A0_DEST_CHANNEL_Y, 0, 
			 swizzle(tmp, Y, Y, Y, Y), 0, 0 );

	 i915_emit_arith( p, A0_CMP,
			 get_result_vector( p, inst ), 
			 get_result_flags( inst ), 0,
			 negate(swizzle(tmp, ONE, ONE, X, ONE),0,0,1,0),
			 swizzle(tmp, ONE, X, ZERO, ONE),
			 swizzle(tmp, ONE, X, Y, ONE));
		     
	 break;

      case OPCODE_LRP: 
	 src0 = src_vector( p, &inst->SrcReg[0], program);
	 src1 = src_vector( p, &inst->SrcReg[1], program);
	 src2 = src_vector( p, &inst->SrcReg[2], program);
	 flags = get_result_flags( inst );
	 tmp = i915_get_utemp( p );

	 /* b*a + c*(1-a)
	  *
	  * b*a + c - ca 
	  *
	  * tmp = b*a + c, 
	  * result = (-c)*a + tmp 
	  */
	 i915_emit_arith( p, A0_MAD, tmp, 
			 flags & A0_DEST_CHANNEL_ALL, 0,
			 src1, src0, src2 );

	 i915_emit_arith( p, A0_MAD, 
			 get_result_vector( p, inst ), 
			 flags, 0, 
			 negate(src2, 1,1,1,1), src0, tmp );
	 break;

      case OPCODE_MAD:
	 EMIT_3ARG_ARITH( A0_MAD );
	 break;

      case OPCODE_MAX:
	 EMIT_2ARG_ARITH( A0_MAX );
	 break;

      case OPCODE_MIN: 
	 src0 = src_vector( p, &inst->SrcReg[0], program);
	 src1 = src_vector( p, &inst->SrcReg[1], program);
	 tmp = i915_get_utemp( p );
	 flags = get_result_flags( inst );

	 i915_emit_arith( p, 
			 A0_MAX,
			 tmp, flags & A0_DEST_CHANNEL_ALL, 0,
			 negate(src0,1,1,1,1), 
			 negate(src1,1,1,1,1), 0);

	 i915_emit_arith( p,
			 A0_MOV,
			 get_result_vector( p, inst ), 
			 flags, 0,
			 negate(tmp, 1,1,1,1), 0, 0);
	 break;

      case OPCODE_MOV: 
	 EMIT_1ARG_ARITH( A0_MOV );
	 break;

      case OPCODE_MUL: 
	 EMIT_2ARG_ARITH( A0_MUL );
	 break;

      case OPCODE_POW: 
	 src0 = src_vector( p, &inst->SrcReg[0], program);
	 src1 = src_vector( p, &inst->SrcReg[1], program);
	 tmp = i915_get_utemp( p );
	 flags = get_result_flags( inst );

	 /* XXX: masking on intermediate values, here and elsewhere.
	  */
	 i915_emit_arith( p, 
			 A0_LOG,
			 tmp, A0_DEST_CHANNEL_X, 0,
			 swizzle(src0,X,X,X,X), 0, 0);

	 i915_emit_arith( p,
			 A0_MUL,
			 tmp, A0_DEST_CHANNEL_X, 0,
			 tmp, src1, 0);


	 i915_emit_arith( p,
			 A0_EXP,
			 get_result_vector( p, inst ), 
			 flags, 0,
			 swizzle(tmp,X,X,X,X), 0, 0);

	 break;

      case OPCODE_RCP: 
	 src0 = src_vector( p, &inst->SrcReg[0], program);

	 i915_emit_arith( p, 
			 A0_RCP,
			 get_result_vector( p, inst ), 
			 get_result_flags( inst ), 0,
			 swizzle(src0,X,X,X,X), 0, 0);
	 break;

      case OPCODE_RSQ: 

	 src0 = src_vector( p, &inst->SrcReg[0], program);

	 i915_emit_arith( p, 
			 A0_RSQ,
			 get_result_vector( p, inst ), 
			 get_result_flags( inst ), 0,
			 swizzle(src0,X,X,X,X), 0, 0);
	 break;
	 
      case OPCODE_SCS:
	 src0 = src_vector( p, &inst->SrcReg[0], program);
	 tmp = i915_get_utemp( p );

	 /* 
	  * t0.xy = MUL x.xx11, x.x1111  ; x^2, x, 1, 1
	  * t0 = MUL t0.xyxy t0.xx11 ; x^4, x^3, x^2, x
	  * t1 = MUL t0.xyyw t0.yz11    ; x^7 x^5 x^3 x
	  * scs.x = DP4 t1, sin_constants
	  * t1 = MUL t0.xxz1 t0.z111    ; x^6 x^4 x^2 1
	  * scs.y = DP4 t1, cos_constants
	  */
	 i915_emit_arith( p, 
			 A0_MUL,
			 tmp, A0_DEST_CHANNEL_XY, 0,
			 swizzle(src0, X,X,ONE,ONE), 
			 swizzle(src0, X,ONE,ONE,ONE), 0);

	 i915_emit_arith( p, 
			 A0_MUL,
			 tmp, A0_DEST_CHANNEL_ALL, 0,
			 swizzle(tmp, X,Y,X,Y), 
			 swizzle(tmp, X,X,ONE,ONE), 0);

	 if (inst->DstReg.WriteMask & WRITEMASK_Y) {
	    GLuint tmp1;
	    
	    if (inst->DstReg.WriteMask & WRITEMASK_X)
	       tmp1 = i915_get_utemp( p );
	    else
	       tmp1 = tmp;

	    i915_emit_arith( p, 
			    A0_MUL,
			    tmp1, A0_DEST_CHANNEL_ALL, 0,
			    swizzle(tmp, X,Y,Y,W), 
			    swizzle(tmp, X,Z,ONE,ONE), 0);
	    
	    i915_emit_arith( p, 
			    A0_DP4,
			    get_result_vector( p, inst ), 
			    A0_DEST_CHANNEL_Y, 0,
			    swizzle(tmp1, W,Z,Y,X),
			    i915_emit_const4fv( p, sin_constants ), 0);
	 }

	 if (inst->DstReg.WriteMask & WRITEMASK_X) {
	    i915_emit_arith( p, 
			    A0_MUL,
			    tmp, A0_DEST_CHANNEL_XYZ, 0,
			    swizzle(tmp, X,X,Z,ONE), 
			    swizzle(tmp, Z,ONE,ONE,ONE), 0);
	    
	    i915_emit_arith( p, 
			    A0_DP4,
			    get_result_vector( p, inst ), 
			    A0_DEST_CHANNEL_X, 0,
			    swizzle(tmp, ONE,Z,Y,X), 
			    i915_emit_const4fv( p, cos_constants ), 0);
	 }
	 break;

      case OPCODE_SGE: 
	 EMIT_2ARG_ARITH( A0_SGE );
	 break;

      case OPCODE_SIN:
	 src0 = src_vector( p, &inst->SrcReg[0], program);
	 tmp = i915_get_utemp( p );

	 i915_emit_arith( p, 
			 A0_MUL,
			 tmp, A0_DEST_CHANNEL_X, 0,
			 src0, 
			 i915_emit_const1f(p, 1.0/(M_PI * 2)),
			 0);

	 i915_emit_arith( p, 
			 A0_MOD,
			 tmp, A0_DEST_CHANNEL_X, 0,
			 tmp, 
			 0, 0 );

	 /* By choosing different taylor constants, could get rid of this mul:
	  */
	 i915_emit_arith( p, 
			 A0_MUL,
			 tmp, A0_DEST_CHANNEL_X, 0,
			 tmp, 
			 i915_emit_const1f(p, (M_PI * 2)),
			 0);

	 /* 
	  * t0.xy = MUL x.xx11, x.x1111  ; x^2, x, 1, 1
	  * t0 = MUL t0.xyxy t0.xx11 ; x^4, x^3, x^2, x
	  * t1 = MUL t0.xyyw t0.yz11    ; x^7 x^5 x^3 x
	  * result = DP4 t1.wzyx, sin_constants
	  */
	 i915_emit_arith( p, 
			 A0_MUL,
			 tmp, A0_DEST_CHANNEL_XY, 0,
			 swizzle(tmp, X,X,ONE,ONE), 
			 swizzle(tmp, X,ONE,ONE,ONE), 0);

	 i915_emit_arith( p, 
			 A0_MUL,
			 tmp, A0_DEST_CHANNEL_ALL, 0,
			 swizzle(tmp, X,Y,X,Y), 
			 swizzle(tmp, X,X,ONE,ONE), 0);

	 i915_emit_arith( p, 
			 A0_MUL,
			 tmp, A0_DEST_CHANNEL_ALL, 0,
			 swizzle(tmp, X,Y,Y,W), 
			 swizzle(tmp, X,Z,ONE,ONE), 0);
	    
	 i915_emit_arith( p, 
			 A0_DP4,
			 get_result_vector( p, inst ), 
			 get_result_flags( inst ), 0,
			 swizzle(tmp, W, Z, Y, X ),
			 i915_emit_const4fv( p, sin_constants ), 0);
	 break;

      case OPCODE_SLT: 
	 EMIT_2ARG_ARITH( A0_SLT );
	 break;

      case OPCODE_SUB: 
	 src0 = src_vector( p, &inst->SrcReg[0], program);
	 src1 = src_vector( p, &inst->SrcReg[1], program);

	 i915_emit_arith( p, 
			 A0_ADD,
			 get_result_vector( p, inst ), 
			 get_result_flags( inst ), 0,
			 src0, negate(src1, 1,1,1,1), 0);
	 break;

      case OPCODE_SWZ: 
	 EMIT_1ARG_ARITH( A0_MOV ); /* extended swizzle handled natively */
	 break;

      case OPCODE_TEX: 
	 EMIT_TEX( T0_TEXLD );
	 break;

      case OPCODE_TXB:
	 EMIT_TEX( T0_TEXLDB );
	 break;

      case OPCODE_TXP:
	 EMIT_TEX( T0_TEXLDP );
	 break;

      case OPCODE_XPD:
	 /* Cross product:
	  *      result.x = src0.y * src1.z - src0.z * src1.y;
	  *      result.y = src0.z * src1.x - src0.x * src1.z;
	  *      result.z = src0.x * src1.y - src0.y * src1.x;
	  *      result.w = undef;
	  */
	 src0 = src_vector( p, &inst->SrcReg[0], program);
	 src1 = src_vector( p, &inst->SrcReg[1], program);
	 tmp = i915_get_utemp( p );
	 
	 i915_emit_arith( p, 
			 A0_MUL,
			 tmp, A0_DEST_CHANNEL_ALL, 0,
			 swizzle(src0,Z,X,Y,ONE), 
			 swizzle(src1,Y,Z,X,ONE), 0);

	 i915_emit_arith( p, 
			 A0_MAD,
			 get_result_vector( p, inst ), 
			 get_result_flags( inst ), 0,
			 swizzle(src0,Y,Z,X,ONE), 
			 swizzle(src1,Z,X,Y,ONE), 
			 negate(tmp,1,1,1,0));
	 break;

      case OPCODE_END:
	 return;
	 
      default:
	 i915_program_error( p, "bad opcode" );
	 return;
      }

      inst++;
      i915_release_utemps( p ); 
   }
}

/* Rather than trying to intercept and jiggle depth writes during
 * emit, just move the value into its correct position at the end of
 * the program:
 */
static void fixup_depth_write( struct i915_fragment_program *p )
{
   if (p->depth_written) {
      GLuint depth = UREG(REG_TYPE_OD, 0);

      i915_emit_arith( p, 
		      A0_MOV,
		      depth, A0_DEST_CHANNEL_W, 0,
		      swizzle(depth,X,Y,Z,Z), 
		      0, 0);
   }
}


#define FRAG_BIT_TEX(n)  (FRAG_BIT_TEX0 << (n))


static void check_wpos( struct i915_fragment_program *p )
{
   GLuint inputs = p->FragProg.Base.InputsRead;
   GLint i;

   p->wpos_tex = -1;

   for (i = 0; i < p->ctx->Const.MaxTextureCoordUnits; i++) {
      if (inputs & FRAG_BIT_TEX(i)) 
	 continue;
      else if (inputs & FRAG_BIT_WPOS) {
	 p->wpos_tex = i;
	 inputs &= ~FRAG_BIT_WPOS;
      }   
   }

   if (inputs & FRAG_BIT_WPOS) {
      i915_program_error(p, "No free texcoord for wpos value");
   }
}


static void translate_program( struct i915_fragment_program *p )
{
   i915ContextPtr i915 = I915_CONTEXT(p->ctx);
   
   i915_init_program( i915, p );
   check_wpos( p ); 
   upload_program( p );
   fixup_depth_write( p );
   i915_fini_program( p ); 
   
   p->translated = 1;
}


static void track_params( struct i915_fragment_program *p )
{
   GLint i;

   if (p->nr_params)
      _mesa_load_state_parameters(p->ctx, p->FragProg.Base.Parameters); 

   for (i = 0; i < p->nr_params; i++) {
      GLint reg = p->param[i].reg;
      COPY_4V( p->constant[reg], p->param[i].values );
   }
   
   p->params_uptodate = 1;
   p->on_hardware = 0;		/* overkill */
}


static void i915BindProgram( GLcontext *ctx,
			    GLenum target, 
			    struct program *prog )
{
   if (target == GL_FRAGMENT_PROGRAM_ARB) {
      i915ContextPtr i915 = I915_CONTEXT(ctx);
      struct i915_fragment_program *p = (struct i915_fragment_program *)prog;

      if (i915->current_program == p) 
	 return;
      
      if (i915->current_program) {
	 i915->current_program->on_hardware = 0;
	 i915->current_program->params_uptodate = 0;
      }
      
      i915->current_program = p;

      assert(p->on_hardware == 0);
      assert(p->params_uptodate == 0);

      /* Hack: make sure fog is correctly enabled according to this
       * fragment program's fog options.
       */
      ctx->Driver.Enable( ctx, GL_FRAGMENT_PROGRAM_ARB, 
			  ctx->FragmentProgram.Enabled );
   }
}

static struct program *i915NewProgram( GLcontext *ctx,
				      GLenum target, 
				      GLuint id )
{
   switch (target) {
   case GL_VERTEX_PROGRAM_ARB:
      return _mesa_init_vertex_program( ctx, CALLOC_STRUCT(vertex_program),
					target, id );

   case GL_FRAGMENT_PROGRAM_ARB: {
      struct i915_fragment_program *prog = CALLOC_STRUCT(i915_fragment_program);
      if (prog) {
	 i915_init_program( I915_CONTEXT(ctx), prog );

	 return _mesa_init_fragment_program( ctx, &prog->FragProg,
					     target, id );
      }
      else
	 return NULL;
   }

   default:
      /* Just fallback:
       */
      return _mesa_new_program( ctx, target, id );
   }
}

static void i915DeleteProgram( GLcontext *ctx,
			      struct program *prog )
{
   if (prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
      i915ContextPtr i915 = I915_CONTEXT(ctx);
      struct i915_fragment_program *p = (struct i915_fragment_program *)prog;
      
      if (i915->current_program == p) 
	 i915->current_program = 0;
   }

   _mesa_delete_program( ctx, prog );
}


static GLboolean i915IsProgramNative( GLcontext *ctx,
				     GLenum target, 
				     struct program *prog )
{
   if (target == GL_FRAGMENT_PROGRAM_ARB) {
      struct i915_fragment_program *p = (struct i915_fragment_program *)prog;

      if (!p->translated)
	 translate_program( p );
      
      return !p->error;
   }
   else
      return GL_TRUE;
}

static void i915ProgramStringNotify( GLcontext *ctx,
				    GLenum target,
				    struct program *prog )
{
   if (target == GL_FRAGMENT_PROGRAM_ARB) {
      struct i915_fragment_program *p = (struct i915_fragment_program *)prog;
      p->translated = 0;

      /* Hack: make sure fog is correctly enabled according to this
       * fragment program's fog options.
       */
      ctx->Driver.Enable( ctx, GL_FRAGMENT_PROGRAM_ARB, 
			  ctx->FragmentProgram.Enabled );
   }
}


void i915ValidateFragmentProgram( i915ContextPtr i915 )
{
   GLcontext *ctx = &i915->intel.ctx;
   intelContextPtr intel = INTEL_CONTEXT(ctx);
   TNLcontext *tnl = TNL_CONTEXT(ctx);
   struct vertex_buffer *VB = &tnl->vb;

   struct i915_fragment_program *p = 
      (struct i915_fragment_program *)ctx->FragmentProgram._Current;

   const GLuint inputsRead = p->FragProg.Base.InputsRead;
   GLuint s4 = i915->state.Ctx[I915_CTXREG_LIS4] & ~S4_VFMT_MASK;
   GLuint s2 = S2_TEXCOORD_NONE;
   int i, offset = 0;

   /* Important:
    */
   VB->AttribPtr[VERT_ATTRIB_POS] = VB->NdcPtr;

   if (!p->translated) 
      translate_program( p );

   intel->vertex_attr_count = 0;
   intel->wpos_offset = 0;
   intel->wpos_size = 0;
   intel->coloroffset = 0;
   intel->specoffset = 0;

   if (inputsRead & FRAG_BITS_TEX_ANY) {
      EMIT_ATTR( _TNL_ATTRIB_POS, EMIT_4F_VIEWPORT, S4_VFMT_XYZW, 16 );
   }
   else {
      EMIT_ATTR( _TNL_ATTRIB_POS, EMIT_3F_VIEWPORT, S4_VFMT_XYZ, 12 );
   }

   if (inputsRead & FRAG_BIT_COL0) {
      intel->coloroffset = offset / 4;
      EMIT_ATTR( _TNL_ATTRIB_COLOR0, EMIT_4UB_4F_BGRA, S4_VFMT_COLOR, 4 );
   }
   
   if ((inputsRead & (FRAG_BIT_COL1|FRAG_BIT_FOGC)) || 
       i915->vertex_fog != I915_FOG_NONE) {

      if (inputsRead & FRAG_BIT_COL1) {
	 intel->specoffset = offset / 4;
	 EMIT_ATTR( _TNL_ATTRIB_COLOR1, EMIT_3UB_3F_BGR, S4_VFMT_SPEC_FOG, 3 );
      }
      else
	 EMIT_PAD(3);

      if ((inputsRead & FRAG_BIT_FOGC) || i915->vertex_fog != I915_FOG_NONE) 
	 EMIT_ATTR( _TNL_ATTRIB_FOG, EMIT_1UB_1F, S4_VFMT_SPEC_FOG, 1 );
      else
	 EMIT_PAD( 1 );
   }

#if 0
   if ((inputsRead & FRAG_BIT_FOGC) || i915->vertex_fog != I915_FOG_NONE) {
      EMIT_ATTR( _TNL_ATTRIB_FOG, EMIT_1F, S4_VFMT_FOG_PARAM, 4 );
   }
#endif

   for (i = 0; i < p->ctx->Const.MaxTextureCoordUnits; i++) {
      if (inputsRead & FRAG_BIT_TEX(i)) {
	 int sz = VB->TexCoordPtr[i]->size;
	    
	 s2 &= ~S2_TEXCOORD_FMT(i, S2_TEXCOORD_FMT0_MASK);
	 s2 |= S2_TEXCOORD_FMT(i, SZ_TO_HW(sz));

	 EMIT_ATTR( _TNL_ATTRIB_TEX0+i, EMIT_SZ(sz), 0, sz * 4 );
      }
      else if (i == p->wpos_tex) {
	
	 /* If WPOS is required, duplicate the XYZ position data in an
	  * unused texture coordinate:
	  */
	 s2 &= ~S2_TEXCOORD_FMT(i, S2_TEXCOORD_FMT0_MASK);
	 s2 |= S2_TEXCOORD_FMT(i, SZ_TO_HW(3));

	 intel->wpos_offset = offset;
	 intel->wpos_size = 3 * sizeof(GLuint);

	 EMIT_PAD( intel->wpos_size );
      }   
   }

   if (s2 != i915->state.Ctx[I915_CTXREG_LIS2] ||
       s4 != i915->state.Ctx[I915_CTXREG_LIS4]) {
    
      I915_STATECHANGE( i915, I915_UPLOAD_CTX );

      /* Must do this *after* statechange, so as not to affect
       * buffered vertices reliant on the old state:
       */
      intel->vertex_size = _tnl_install_attrs( &intel->ctx, 
					       intel->vertex_attrs, 
					       intel->vertex_attr_count,
					       intel->ViewportMatrix.m, 0 ); 

      intel->vertex_size >>= 2;

      i915->state.Ctx[I915_CTXREG_LIS2] = s2;
      i915->state.Ctx[I915_CTXREG_LIS4] = s4;

      assert(intel->vtbl.check_vertex_size( intel, intel->vertex_size ));
   }

   if (!p->params_uptodate) 
      track_params( p );

   if (!p->on_hardware) 
      i915_upload_program( i915, p );
}

void i915InitFragProgFuncs( struct dd_function_table *functions )
{
   functions->BindProgram = i915BindProgram;
   functions->NewProgram = i915NewProgram;
   functions->DeleteProgram = i915DeleteProgram;
   functions->IsProgramNative = i915IsProgramNative;
   functions->ProgramStringNotify = i915ProgramStringNotify;
}