aboutsummaryrefslogtreecommitdiffstats
path: root/src/mesa/drivers/glide/fxsetup.c
blob: b09a90ce8b9f071716de1b785aedea845ee9c965 (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
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
/* -*- mode: C; tab-width:8; c-basic-offset:2 -*- */

/*
 * Mesa 3-D graphics library
 * Version:  3.3
 *
 * Copyright (C) 1999-2000  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.
 *
 *
 * Original Mesa / 3Dfx device driver (C) 1999 David Bucciarelli, by the
 * terms stated above.
 *
 * Thank you for your contribution, David!
 *
 * Please make note of the above copyright/license statement.  If you
 * contributed code or bug fixes to this code under the previous (GNU
 * Library) license and object to the new license, your code will be
 * removed at your request.  Please see the Mesa docs/COPYRIGHT file
 * for more information.
 *
 * Additional Mesa/3Dfx driver developers:
 *   Daryll Strauss <daryll@precisioninsight.com>
 *   Keith Whitwell <keith@precisioninsight.com>
 *
 * See fxapi.h for more revision/author details.
 */


/* fxsetup.c - 3Dfx VooDoo rendering mode setup functions */


#ifdef HAVE_CONFIG_H
#include "conf.h"
#endif

#if defined(FX)

#include "fxdrv.h"
#include "enums.h"

static GLuint fxGetTexSetConfiguration(GLcontext *ctx,
				struct gl_texture_object *tObj0,
				struct gl_texture_object *tObj1);
static void fxSetupTextureSingleTMU_NoLock(GLcontext *ctx, GLuint textureset);
static void fxSetupDoubleTMU_NoLock(fxMesaContext fxMesa, 
			     struct gl_texture_object *tObj0,
			     struct gl_texture_object *tObj1);
static void fxSetupTexture_NoLock(GLcontext *ctx);
static void fxSetupTexture(GLcontext *ctx);
static void fxSetupBlend(GLcontext *ctx);
static void fxSetupDepthTest(GLcontext *ctx);
static void fxSetupScissor(GLcontext *ctx);
static void fxSetupCull(GLcontext *ctx);
static void gl_print_fx_state_flags( const char *msg, GLuint flags);
/*static GLboolean fxMultipassBlend(struct vertex_buffer *, GLuint);*/
static GLboolean fxMultipassTexture( struct vertex_buffer *, GLuint );

static void fxTexValidate(GLcontext *ctx, struct gl_texture_object *tObj)
{
  tfxTexInfo *ti=fxTMGetTexInfo(tObj);
  GLint minl, maxl;

  if (MESA_VERBOSE&VERBOSE_DRIVER) {
     fprintf(stderr,"fxmesa: fxTexValidate(...) Start\n");
  }

  if(ti->validated) {
     if (MESA_VERBOSE&VERBOSE_DRIVER) {
	fprintf(stderr,"fxmesa: fxTexValidate(...) End (validated=GL_TRUE)\n");
     }
    return;
  }

  ti->tObj=tObj;
  minl=ti->minLevel=tObj->BaseLevel;
  maxl=ti->maxLevel=MIN2(tObj->MaxLevel,tObj->Image[0]->MaxLog2);

  fxTexGetInfo(tObj->Image[minl]->Width, tObj->Image[minl]->Height,
	       &(FX_largeLodLog2(ti->info)), &(FX_aspectRatioLog2(ti->info)),
	       &(ti->sScale), &(ti->tScale),
	       &(ti->int_sScale), &(ti->int_tScale),	       
	       NULL, NULL);

  if((tObj->MinFilter!=GL_NEAREST) && (tObj->MinFilter!=GL_LINEAR))
    fxTexGetInfo(tObj->Image[maxl]->Width,tObj->Image[maxl]->Height,
		 &(FX_smallLodLog2(ti->info)),NULL,
		 NULL,NULL,
		 NULL,NULL,
		 NULL,NULL);
  else
    FX_smallLodLog2(ti->info)=FX_largeLodLog2(ti->info);

  fxTexGetFormat(tObj->Image[minl]->IntFormat,&(ti->info.format),&(ti->baseLevelInternalFormat));

  switch (tObj->WrapS) {
  case GL_CLAMP_TO_EDGE:
    /* What's this really mean compared to GL_CLAMP? */
  case GL_CLAMP:
    ti->sClamp=1;
    break;
  case GL_REPEAT:
    ti->sClamp=0;
    break;
  default:
    ;  /* silence compiler warning */
  }
  switch (tObj->WrapT) {
  case GL_CLAMP_TO_EDGE:
    /* What's this really mean compared to GL_CLAMP? */
  case GL_CLAMP:
    ti->tClamp=1;
    break;
  case GL_REPEAT:
    ti->tClamp=0;
    break;
  default:
    ;  /* silence compiler warning */
  }

  ti->validated=GL_TRUE;

  ti->info.data=NULL;

  if (MESA_VERBOSE&VERBOSE_DRIVER) {
     fprintf(stderr,"fxmesa: fxTexValidate(...) End\n");
  }
}

static void fxPrintUnitsMode( const char *msg, GLuint mode )
{
   fprintf(stderr, 
	   "%s: (0x%x) %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
	   msg,
	   mode,
	   (mode & FX_UM_E0_REPLACE)         ? "E0_REPLACE, " : "",
	   (mode & FX_UM_E0_MODULATE)        ? "E0_MODULATE, " : "",
	   (mode & FX_UM_E0_DECAL)           ? "E0_DECAL, " : "",
	   (mode & FX_UM_E0_BLEND)           ? "E0_BLEND, " : "",
	   (mode & FX_UM_E1_REPLACE)         ? "E1_REPLACE, " : "",
	   (mode & FX_UM_E1_MODULATE)        ? "E1_MODULATE, " : "",
	   (mode & FX_UM_E1_DECAL)           ? "E1_DECAL, " : "",
	   (mode & FX_UM_E1_BLEND)           ? "E1_BLEND, " : "",
	   (mode & FX_UM_E0_ALPHA)           ? "E0_ALPHA, " : "",
	   (mode & FX_UM_E0_LUMINANCE)       ? "E0_LUMINANCE, " : "",
	   (mode & FX_UM_E0_LUMINANCE_ALPHA) ? "E0_LUMINANCE_ALPHA, " : "",
	   (mode & FX_UM_E0_INTENSITY)       ? "E0_INTENSITY, " : "",
	   (mode & FX_UM_E0_RGB)             ? "E0_RGB, " : "",
	   (mode & FX_UM_E0_RGBA)            ? "E0_RGBA, " : "",
	   (mode & FX_UM_E1_ALPHA)           ? "E1_ALPHA, " : "",
	   (mode & FX_UM_E1_LUMINANCE)       ? "E1_LUMINANCE, " : "",
	   (mode & FX_UM_E1_LUMINANCE_ALPHA) ? "E1_LUMINANCE_ALPHA, " : "",
	   (mode & FX_UM_E1_INTENSITY)       ? "E1_INTENSITY, " : "",
	   (mode & FX_UM_E1_RGB)             ? "E1_RGB, " : "",
	   (mode & FX_UM_E1_RGBA)            ? "E1_RGBA, " : "",
	   (mode & FX_UM_COLOR_ITERATED)     ? "COLOR_ITERATED, " : "",
	   (mode & FX_UM_COLOR_CONSTANT)     ? "COLOR_CONSTANT, " : "",
	   (mode & FX_UM_ALPHA_ITERATED)     ? "ALPHA_ITERATED, " : "",
	   (mode & FX_UM_ALPHA_CONSTANT)     ? "ALPHA_CONSTANT, " : "");
}

static GLuint fxGetTexSetConfiguration(GLcontext *ctx,
				       struct gl_texture_object *tObj0,
				       struct gl_texture_object *tObj1)
{
  GLuint unitsmode=0;
  GLuint envmode=0;
  GLuint ifmt=0;

  if((ctx->Light.ShadeModel==GL_SMOOTH) ||
     (ctx->Point.SmoothFlag) ||
     (ctx->Line.SmoothFlag) ||
     (ctx->Polygon.SmoothFlag))
    unitsmode|=FX_UM_ALPHA_ITERATED;
  else
    unitsmode|=FX_UM_ALPHA_CONSTANT;

  if(ctx->Light.ShadeModel==GL_SMOOTH)
    unitsmode|=FX_UM_COLOR_ITERATED;
  else
    unitsmode|=FX_UM_COLOR_CONSTANT;

  /* 
     OpenGL Feeds Texture 0 into Texture 1
     Glide Feeds Texture 1 into Texture 0
  */
  if(tObj0) {
    tfxTexInfo *ti0=fxTMGetTexInfo(tObj0);

    switch(ti0->baseLevelInternalFormat) {
    case GL_ALPHA:
      ifmt|=FX_UM_E0_ALPHA;
      break;
    case GL_LUMINANCE:
      ifmt|=FX_UM_E0_LUMINANCE;
      break;
    case GL_LUMINANCE_ALPHA:
      ifmt|=FX_UM_E0_LUMINANCE_ALPHA;
      break;
    case GL_INTENSITY:
      ifmt|=FX_UM_E0_INTENSITY;
      break;
    case GL_RGB:
      ifmt|=FX_UM_E0_RGB;
      break;
    case GL_RGBA:
      ifmt|=FX_UM_E0_RGBA;
      break;
    }

    switch(ctx->Texture.Unit[0].EnvMode) {
    case GL_DECAL:
      envmode|=FX_UM_E0_DECAL;
      break;
    case GL_MODULATE:
      envmode|=FX_UM_E0_MODULATE;
      break;
    case GL_REPLACE:
      envmode|=FX_UM_E0_REPLACE;
      break;
    case GL_BLEND:
      envmode|=FX_UM_E0_BLEND;
      break;
    case GL_ADD:
      envmode|=FX_UM_E0_ADD;
      break;
    default:
      /* do nothing */
      break;
    }
  }

  if(tObj1) {
    tfxTexInfo *ti1=fxTMGetTexInfo(tObj1);

    switch(ti1->baseLevelInternalFormat) {
    case GL_ALPHA:
      ifmt|=FX_UM_E1_ALPHA;
      break;
    case GL_LUMINANCE:
      ifmt|=FX_UM_E1_LUMINANCE;
      break;
    case GL_LUMINANCE_ALPHA:
      ifmt|=FX_UM_E1_LUMINANCE_ALPHA;
      break;
    case GL_INTENSITY:
      ifmt|=FX_UM_E1_INTENSITY;
      break;
    case GL_RGB:
      ifmt|=FX_UM_E1_RGB;
      break;
    case GL_RGBA:
      ifmt|=FX_UM_E1_RGBA;
      break;
    default:
      /* do nothing */
      break;
    }

    switch(ctx->Texture.Unit[1].EnvMode) {
    case GL_DECAL:
      envmode|=FX_UM_E1_DECAL;
      break;
    case GL_MODULATE:
      envmode|=FX_UM_E1_MODULATE;
      break;
    case GL_REPLACE:
      envmode|=FX_UM_E1_REPLACE;
      break;
    case GL_BLEND:
      envmode|=FX_UM_E1_BLEND;
      break;
    case GL_ADD:
      envmode|=FX_UM_E1_ADD;
      break;
    default:
      /* do nothing */
      break;
    }
  }

  unitsmode|=(ifmt | envmode);

  if (MESA_VERBOSE & (VERBOSE_DRIVER|VERBOSE_TEXTURE)) 
     fxPrintUnitsMode("unitsmode", unitsmode);

  return unitsmode;
}

/************************************************************************/
/************************* Rendering Mode SetUp *************************/
/************************************************************************/

/************************* Single Texture Set ***************************/

static void fxSetupSingleTMU_NoLock(fxMesaContext fxMesa, struct gl_texture_object *tObj)
{
  tfxTexInfo *ti=fxTMGetTexInfo(tObj);
  int tmu;

  /* Make sure we're not loaded incorrectly */
  if (ti->isInTM) {
    if (ti->LODblend) {
      if (ti->whichTMU!=FX_TMU_SPLIT)
	fxTMMoveOutTM(fxMesa, tObj);
    } else {
      if (ti->whichTMU==FX_TMU_SPLIT)
	fxTMMoveOutTM(fxMesa, tObj);
    }
  }

  /* Make sure we're loaded correctly */
  if (!ti->isInTM) {
    if (ti->LODblend)
      fxTMMoveInTM_NoLock(fxMesa,tObj,FX_TMU_SPLIT);
    else {
      if (fxMesa->haveTwoTMUs) {
	if (fxMesa->freeTexMem[FX_TMU0] >
	    FX_grTexTextureMemRequired_NoLock(GR_MIPMAPLEVELMASK_BOTH,
					      &(ti->info))) {
	  fxTMMoveInTM_NoLock(fxMesa,tObj, FX_TMU0);
	} else {
	  fxTMMoveInTM_NoLock(fxMesa,tObj, FX_TMU1);
	}
      } else
	fxTMMoveInTM_NoLock(fxMesa,tObj,FX_TMU0);
    }
  }

  if (ti->LODblend && ti->whichTMU == FX_TMU_SPLIT) {
    if ((ti->info.format==GR_TEXFMT_P_8) && (!fxMesa->haveGlobalPaletteTexture)) {
       if (MESA_VERBOSE&VERBOSE_DRIVER) {
	  fprintf(stderr,"fxmesa: uploading texture palette\n");
       }
      FX_grTexDownloadTable_NoLock(GR_TMU0,GR_TEXTABLE_PALETTE,&(ti->palette));
      FX_grTexDownloadTable_NoLock(GR_TMU1,GR_TEXTABLE_PALETTE,&(ti->palette));
    }

    FX_grTexClampMode_NoLock(GR_TMU0,ti->sClamp,ti->tClamp);
    FX_grTexClampMode_NoLock(GR_TMU1,ti->sClamp,ti->tClamp);
    FX_grTexFilterMode_NoLock(GR_TMU0,ti->minFilt,ti->maxFilt);
    FX_grTexFilterMode_NoLock(GR_TMU1,ti->minFilt,ti->maxFilt);
    FX_grTexMipMapMode_NoLock(GR_TMU0,ti->mmMode,ti->LODblend);
    FX_grTexMipMapMode_NoLock(GR_TMU1,ti->mmMode,ti->LODblend);

    FX_grTexSource_NoLock(GR_TMU0,ti->tm[FX_TMU0]->startAddr,
			  GR_MIPMAPLEVELMASK_ODD,&(ti->info));
    FX_grTexSource_NoLock(GR_TMU1,ti->tm[FX_TMU1]->startAddr,
			  GR_MIPMAPLEVELMASK_EVEN,&(ti->info));
  } else {
    if (ti->whichTMU==FX_TMU_BOTH) tmu=FX_TMU0;
    else tmu=ti->whichTMU;

    if((ti->info.format==GR_TEXFMT_P_8) && (!fxMesa->haveGlobalPaletteTexture)) {
       if (MESA_VERBOSE&VERBOSE_DRIVER) {
	  fprintf(stderr,"fxmesa: uploading texture palette\n");
       }
      FX_grTexDownloadTable_NoLock(tmu, GR_TEXTABLE_PALETTE, &(ti->palette));
    }

    /* KW: The alternative is to do the download to the other tmu.  If
     * we get to this point, I think it means we are thrashing the
     * texture memory, so perhaps it's not a good idea.  
     */
    if (ti->LODblend && (MESA_VERBOSE&VERBOSE_DRIVER))
       fprintf(stderr, "fxmesa: not blending texture - only on one tmu\n");

    FX_grTexClampMode_NoLock(tmu, ti->sClamp, ti->tClamp);
    FX_grTexFilterMode_NoLock(tmu, ti->minFilt, ti->maxFilt);
    FX_grTexMipMapMode_NoLock(tmu, ti->mmMode, FXFALSE);

    FX_grTexSource_NoLock(tmu, ti->tm[tmu]->startAddr,
			  GR_MIPMAPLEVELMASK_BOTH, &(ti->info));
  }
}

static void fxSelectSingleTMUSrc_NoLock(fxMesaContext fxMesa, GLint tmu, 
					FxBool LODblend)
{
  if (MESA_VERBOSE&VERBOSE_DRIVER) {
    fprintf(stderr,"fxmesa: fxSelectSingleTMUSrc(%d,%d)\n",tmu,LODblend);
  }

  if (LODblend) {
    FX_grTexCombine_NoLock(GR_TMU0,
			   GR_COMBINE_FUNCTION_BLEND,
			   GR_COMBINE_FACTOR_ONE_MINUS_LOD_FRACTION,
			   GR_COMBINE_FUNCTION_BLEND,
			   GR_COMBINE_FACTOR_ONE_MINUS_LOD_FRACTION,
			   FXFALSE,FXFALSE);

    if (fxMesa->haveTwoTMUs)
      FX_grTexCombine_NoLock(GR_TMU1,
			     GR_COMBINE_FUNCTION_LOCAL,GR_COMBINE_FACTOR_NONE,
			     GR_COMBINE_FUNCTION_LOCAL,GR_COMBINE_FACTOR_NONE,
			     FXFALSE,FXFALSE);
    fxMesa->tmuSrc=FX_TMU_SPLIT;
  }
  else {
    if (tmu!=FX_TMU1) {
      FX_grTexCombine_NoLock(GR_TMU0,
			     GR_COMBINE_FUNCTION_LOCAL,GR_COMBINE_FACTOR_NONE,
			     GR_COMBINE_FUNCTION_LOCAL,GR_COMBINE_FACTOR_NONE,
			     FXFALSE,FXFALSE);
      if (fxMesa->haveTwoTMUs) {
        FX_grTexCombine_NoLock(GR_TMU1,
                               GR_COMBINE_FUNCTION_ZERO, GR_COMBINE_FACTOR_NONE,
                               GR_COMBINE_FUNCTION_ZERO, GR_COMBINE_FACTOR_NONE,
                               FXFALSE,FXFALSE);
      }
      fxMesa->tmuSrc=FX_TMU0;
    }
    else {
      FX_grTexCombine_NoLock(GR_TMU1,
			     GR_COMBINE_FUNCTION_LOCAL,GR_COMBINE_FACTOR_NONE,
			     GR_COMBINE_FUNCTION_LOCAL,GR_COMBINE_FACTOR_NONE,
			     FXFALSE,FXFALSE);

      /* GR_COMBINE_FUNCTION_SCALE_OTHER doesn't work ?!? */

      FX_grTexCombine_NoLock(GR_TMU0,
			     GR_COMBINE_FUNCTION_BLEND,
			     GR_COMBINE_FACTOR_ONE,
			     GR_COMBINE_FUNCTION_BLEND,
			     GR_COMBINE_FACTOR_ONE,
			     FXFALSE,FXFALSE);

      fxMesa->tmuSrc=FX_TMU1;
    }
  }
}

static void fxSetupTextureSingleTMU_NoLock(GLcontext *ctx, GLuint textureset)
{
  fxMesaContext fxMesa=(fxMesaContext)ctx->DriverCtx;
  GrCombineLocal_t localc,locala;
  GLuint unitsmode;
  GLint ifmt;
  tfxTexInfo *ti;
  struct gl_texture_object *tObj=ctx->Texture.Unit[textureset].CurrentD[2];
  int tmu;

  if (MESA_VERBOSE&VERBOSE_DRIVER) {
     fprintf(stderr,"fxmesa: fxSetupTextureSingleTMU(...) Start\n");
  }

  ti=fxTMGetTexInfo(tObj);

  fxTexValidate(ctx,tObj);

  fxSetupSingleTMU_NoLock(fxMesa,tObj);

  if (ti->whichTMU==FX_TMU_BOTH) tmu=FX_TMU0;
  else tmu=ti->whichTMU;
  if (fxMesa->tmuSrc!=tmu)
    fxSelectSingleTMUSrc_NoLock(fxMesa, tmu, ti->LODblend);

  if(textureset==0 || !fxMesa->haveTwoTMUs)
    unitsmode=fxGetTexSetConfiguration(ctx,tObj,NULL);
  else
    unitsmode=fxGetTexSetConfiguration(ctx,NULL,tObj);

  if(fxMesa->lastUnitsMode==unitsmode)
    return;

  fxMesa->lastUnitsMode=unitsmode;

  fxMesa->stw_hint_state = 0;
  FX_grHints_NoLock(GR_HINT_STWHINT,0);

  ifmt=ti->baseLevelInternalFormat;

  if(unitsmode & FX_UM_ALPHA_ITERATED)
    locala=GR_COMBINE_LOCAL_ITERATED;
  else
    locala=GR_COMBINE_LOCAL_CONSTANT;

  if(unitsmode & FX_UM_COLOR_ITERATED)
    localc=GR_COMBINE_LOCAL_ITERATED;
  else
    localc=GR_COMBINE_LOCAL_CONSTANT;

  if (MESA_VERBOSE & (VERBOSE_DRIVER|VERBOSE_TEXTURE))
     fprintf(stderr, "fxMesa: fxSetupTextureSingleTMU, envmode is %s\n",
	     gl_lookup_enum_by_nr(ctx->Texture.Unit[textureset].EnvMode));

  switch(ctx->Texture.Unit[textureset].EnvMode) {
  case GL_DECAL:
    FX_grAlphaCombine_NoLock(GR_COMBINE_FUNCTION_LOCAL,
			     GR_COMBINE_FACTOR_NONE,
			     locala,
			     GR_COMBINE_OTHER_NONE,
			     FXFALSE);

    FX_grColorCombine_NoLock(GR_COMBINE_FUNCTION_BLEND,
			     GR_COMBINE_FACTOR_TEXTURE_ALPHA,
			     localc,
			     GR_COMBINE_OTHER_TEXTURE,
			     FXFALSE);
    break;
  case GL_MODULATE:
    FX_grAlphaCombine_NoLock(GR_COMBINE_FUNCTION_SCALE_OTHER,
			     GR_COMBINE_FACTOR_LOCAL,
			     locala,
			     GR_COMBINE_OTHER_TEXTURE,
			     FXFALSE);

    if(ifmt==GL_ALPHA)
      FX_grColorCombine_NoLock(GR_COMBINE_FUNCTION_LOCAL,
			       GR_COMBINE_FACTOR_NONE,
			       localc,
			       GR_COMBINE_OTHER_NONE,
			       FXFALSE);
    else
      FX_grColorCombine_NoLock(GR_COMBINE_FUNCTION_SCALE_OTHER,
			       GR_COMBINE_FACTOR_LOCAL,
			       localc,
			       GR_COMBINE_OTHER_TEXTURE,
			       FXFALSE);
    break;
  case GL_BLEND:
#if 0
    FX_grAlphaCombine_NoLock(GR_COMBINE_FUNCTION_SCALE_OTHER,
			     GR_COMBINE_FACTOR_LOCAL,
			     locala,
			     GR_COMBINE_OTHER_TEXTURE,
			     FXFALSE);
    if (ifmt==GL_ALPHA)
      FX_grColorCombine_NoLock(GR_COMBINE_FUNCTION_LOCAL,
			       GR_COMBINE_FACTOR_NONE,
			       localc,
			       GR_COMBINE_OTHER_NONE,
			       FXFALSE);
    else
      FX_grColorCombine_NoLock(GR_COMBINE_FUNCTION_SCALE_OTHER_ADD_LOCAL,
			       GR_COMBINE_FACTOR_LOCAL,
			       localc,
			       GR_COMBINE_OTHER_TEXTURE,
			       FXTRUE);
    ctx->Driver.MultipassFunc = fxMultipassBlend;
#else
#ifndef FX_SILENT
    /*fprintf(stderr,"fx Driver: GL_BLEND not yet supported\n");*/
#endif
#endif    
    break;
  case GL_REPLACE:
    if((ifmt==GL_RGB) || (ifmt==GL_LUMINANCE))
      FX_grAlphaCombine_NoLock(GR_COMBINE_FUNCTION_LOCAL,
			       GR_COMBINE_FACTOR_NONE,
			       locala,
			       GR_COMBINE_OTHER_NONE,
			       FXFALSE);
    else
      FX_grAlphaCombine_NoLock(GR_COMBINE_FUNCTION_SCALE_OTHER,
			       GR_COMBINE_FACTOR_ONE,
			       locala,
			       GR_COMBINE_OTHER_TEXTURE,
			       FXFALSE);
    
    if(ifmt==GL_ALPHA)
      FX_grColorCombine_NoLock(GR_COMBINE_FUNCTION_LOCAL,
			       GR_COMBINE_FACTOR_NONE,
			       localc,
			       GR_COMBINE_OTHER_NONE,
			       FXFALSE);
    else
      FX_grColorCombine_NoLock(GR_COMBINE_FUNCTION_SCALE_OTHER,
			       GR_COMBINE_FACTOR_ONE,
			       localc,
			       GR_COMBINE_OTHER_TEXTURE,
			       FXFALSE);
    break;
  default:
#ifndef FX_SILENT
    fprintf(stderr,"fx Driver: %x Texture.EnvMode not yet supported\n",ctx->Texture.Unit[textureset].EnvMode);
#endif
    break;
  }

  if (MESA_VERBOSE&VERBOSE_DRIVER) {
     fprintf(stderr,"fxmesa: fxSetupTextureSingleTMU(...) End\n");
  }
}

static void fxSetupTextureSingleTMU(GLcontext *ctx, GLuint textureset) {
  BEGIN_BOARD_LOCK();
  fxSetupTextureSingleTMU_NoLock(ctx, textureset);
  END_BOARD_LOCK();
}

/************************* Double Texture Set ***************************/

static void fxSetupDoubleTMU_NoLock(fxMesaContext fxMesa, 
				    struct gl_texture_object *tObj0,
				    struct gl_texture_object *tObj1)
{
#define T0_NOT_IN_TMU  0x01
#define T1_NOT_IN_TMU  0x02
#define T0_IN_TMU0     0x04
#define T1_IN_TMU0     0x08
#define T0_IN_TMU1     0x10
#define T1_IN_TMU1     0x20

  tfxTexInfo *ti0=fxTMGetTexInfo(tObj0);
  tfxTexInfo *ti1=fxTMGetTexInfo(tObj1);
  GLuint tstate=0;
  int tmu0=0, tmu1=1;

  if (MESA_VERBOSE&VERBOSE_DRIVER) {
     fprintf(stderr,"fxmesa: fxSetupDoubleTMU(...)\n");
  }

  /* We shouldn't need to do this. There is something wrong with
     mutlitexturing when the TMUs are swapped. So, we're forcing
     them to always be loaded correctly. !!! */
  if (ti0->whichTMU==FX_TMU1)
    fxTMMoveOutTM_NoLock(fxMesa, tObj0);
  if (ti1->whichTMU==FX_TMU0)
    fxTMMoveOutTM_NoLock(fxMesa, tObj1);

  if (ti0->isInTM) {
    switch (ti0->whichTMU) {
    case FX_TMU0:
      tstate|=T0_IN_TMU0;
      break;
    case FX_TMU1:
      tstate|=T0_IN_TMU1;
      break;
    case FX_TMU_BOTH:
      tstate|=T0_IN_TMU0|T0_IN_TMU1;
      break;
    case FX_TMU_SPLIT:
      tstate|=T0_NOT_IN_TMU;
      break;
    }
  } else tstate|=T0_NOT_IN_TMU;

  if (ti1->isInTM) {
    switch (ti1->whichTMU) {
    case FX_TMU0:
      tstate|=T1_IN_TMU0;
      break;
    case FX_TMU1:
      tstate|=T1_IN_TMU1;
      break;
    case FX_TMU_BOTH:
      tstate|=T1_IN_TMU0|T1_IN_TMU1;
      break;
    case FX_TMU_SPLIT:
      tstate|=T1_NOT_IN_TMU;
      break;
    }
  } else tstate|=T1_NOT_IN_TMU;

  ti0->lastTimeUsed=fxMesa->texBindNumber;
  ti1->lastTimeUsed=fxMesa->texBindNumber;

  /* Move texture maps into TMUs */ 

  if (!(((tstate&T0_IN_TMU0) && (tstate&T1_IN_TMU1)) ||
	((tstate&T0_IN_TMU1) && (tstate&T1_IN_TMU0)))) {
    if (tObj0==tObj1) fxTMMoveInTM_NoLock(fxMesa, tObj1, FX_TMU_BOTH);
    else {
      /* Find the minimal way to correct the situation */
      if ((tstate&T0_IN_TMU0) || (tstate&T1_IN_TMU1)) {
	/* We have one in the standard order, setup the other */
	if (tstate&T0_IN_TMU0) { /* T0 is in TMU0, put T1 in TMU1 */
	  fxTMMoveInTM_NoLock(fxMesa, tObj1, FX_TMU1);
	} else {
	  fxTMMoveInTM_NoLock(fxMesa, tObj0, FX_TMU0);
	}
	/* tmu0 and tmu1 are setup */
      } else if ((tstate&T0_IN_TMU1) || (tstate&T1_IN_TMU0)) {
	/* we have one in the reverse order, setup the other */
	if (tstate&T1_IN_TMU0) { /* T1 is in TMU0, put T0 in TMU1 */
	  fxTMMoveInTM_NoLock(fxMesa, tObj0, FX_TMU1);
	} else {
	  fxTMMoveInTM_NoLock(fxMesa, tObj1, FX_TMU0);
	}
	tmu0=1;
	tmu1=0;
      } else { /* Nothing is loaded */
	fxTMMoveInTM_NoLock(fxMesa, tObj0, FX_TMU0);
	fxTMMoveInTM_NoLock(fxMesa, tObj1, FX_TMU1);
	/* tmu0 and tmu1 are setup */
      }
    }
  }

  if (!fxMesa->haveGlobalPaletteTexture) {
    if (ti0->info.format==GR_TEXFMT_P_8) {
       if (MESA_VERBOSE&VERBOSE_DRIVER) {
	  fprintf(stderr,"fxmesa: uploading texture palette TMU0\n");
       }
       FX_grTexDownloadTable_NoLock(tmu0, GR_TEXTABLE_PALETTE, &(ti0->palette));
    }

    if (ti1->info.format==GR_TEXFMT_P_8) {
       if (MESA_VERBOSE&VERBOSE_DRIVER) {
	  fprintf(stderr,"fxmesa: uploading texture palette TMU1\n");
       }
       FX_grTexDownloadTable_NoLock(tmu1, GR_TEXTABLE_PALETTE, &(ti1->palette));
    }
  }

  FX_grTexSource_NoLock(tmu0, ti0->tm[tmu0]->startAddr, 
			GR_MIPMAPLEVELMASK_BOTH, &(ti0->info));
  FX_grTexClampMode_NoLock(tmu0, ti0->sClamp, ti0->tClamp);
  FX_grTexFilterMode_NoLock(tmu0, ti0->minFilt, ti0->maxFilt);
  FX_grTexMipMapMode_NoLock(tmu0, ti0->mmMode, FXFALSE);

  FX_grTexSource_NoLock(tmu1, ti1->tm[tmu1]->startAddr,
			GR_MIPMAPLEVELMASK_BOTH, &(ti1->info));
  FX_grTexClampMode_NoLock(tmu1, ti1->sClamp, ti1->tClamp);
  FX_grTexFilterMode_NoLock(tmu1, ti1->minFilt, ti1->maxFilt);
  FX_grTexMipMapMode_NoLock(tmu1, ti1->mmMode, FXFALSE);

#undef T0_NOT_IN_TMU
#undef T1_NOT_IN_TMU
#undef T0_IN_TMU0
#undef T1_IN_TMU0
#undef T0_IN_TMU1
#undef T1_IN_TMU1
}

static void fxSetupTextureDoubleTMU_NoLock(GLcontext *ctx)
{
  fxMesaContext fxMesa=(fxMesaContext)ctx->DriverCtx;
  GrCombineLocal_t localc,locala;
  tfxTexInfo *ti0,*ti1;
  struct gl_texture_object *tObj0=ctx->Texture.Unit[0].CurrentD[2];
  struct gl_texture_object *tObj1=ctx->Texture.Unit[1].CurrentD[2];
  GLuint envmode,ifmt,unitsmode;
  int tmu0=0, tmu1=1;

  if (MESA_VERBOSE&VERBOSE_DRIVER) {
     fprintf(stderr,"fxmesa: fxSetupTextureDoubleTMU(...) Start\n");
  }

  ti0=fxTMGetTexInfo(tObj0);
  fxTexValidate(ctx,tObj0);

  ti1=fxTMGetTexInfo(tObj1);
  fxTexValidate(ctx,tObj1);

  fxSetupDoubleTMU_NoLock(fxMesa,tObj0,tObj1);

  unitsmode=fxGetTexSetConfiguration(ctx,tObj0,tObj1);

  if(fxMesa->lastUnitsMode==unitsmode)
    return;

  fxMesa->lastUnitsMode=unitsmode;

  fxMesa->stw_hint_state |= GR_STWHINT_ST_DIFF_TMU1;
  FX_grHints_NoLock(GR_HINT_STWHINT, fxMesa->stw_hint_state);

  envmode=unitsmode & FX_UM_E_ENVMODE;
  ifmt=unitsmode & FX_UM_E_IFMT;

  if(unitsmode & FX_UM_ALPHA_ITERATED)
    locala=GR_COMBINE_LOCAL_ITERATED;
  else
    locala=GR_COMBINE_LOCAL_CONSTANT;

  if(unitsmode & FX_UM_COLOR_ITERATED)
    localc=GR_COMBINE_LOCAL_ITERATED;
  else
    localc=GR_COMBINE_LOCAL_CONSTANT;


  if (MESA_VERBOSE & (VERBOSE_DRIVER|VERBOSE_TEXTURE))
     fprintf(stderr, "fxMesa: fxSetupTextureDoubleTMU, envmode is %s/%s\n",
	     gl_lookup_enum_by_nr(ctx->Texture.Unit[0].EnvMode),
	     gl_lookup_enum_by_nr(ctx->Texture.Unit[1].EnvMode));


  if ((ti0->whichTMU==FX_TMU1) || (ti1->whichTMU==FX_TMU0)) {
    tmu0=1;
    tmu1=0;
  }
  fxMesa->tmuSrc=FX_TMU_BOTH;
  switch(envmode) {
  case (FX_UM_E0_MODULATE | FX_UM_E1_MODULATE):
    {
      GLboolean isalpha[FX_NUM_TMU];

      if(ti0->baseLevelInternalFormat==GL_ALPHA)
	isalpha[tmu0]=GL_TRUE;
      else
	isalpha[tmu0]=GL_FALSE;

      if(ti1->baseLevelInternalFormat==GL_ALPHA)
	isalpha[tmu1]=GL_TRUE;
      else
	isalpha[tmu1]=GL_FALSE;
	
      if(isalpha[FX_TMU1])
	FX_grTexCombine_NoLock(GR_TMU1,
			       GR_COMBINE_FUNCTION_ZERO,
			       GR_COMBINE_FACTOR_NONE,
			       GR_COMBINE_FUNCTION_LOCAL,
			       GR_COMBINE_FACTOR_NONE,
			       FXTRUE,FXFALSE);
      else
	FX_grTexCombine_NoLock(GR_TMU1,
			       GR_COMBINE_FUNCTION_LOCAL,
			       GR_COMBINE_FACTOR_NONE,
			       GR_COMBINE_FUNCTION_LOCAL,
			       GR_COMBINE_FACTOR_NONE,
			       FXFALSE,FXFALSE);

      if(isalpha[FX_TMU0])
	FX_grTexCombine_NoLock(GR_TMU0,
			       GR_COMBINE_FUNCTION_BLEND_OTHER,
			       GR_COMBINE_FACTOR_ONE,
			       GR_COMBINE_FUNCTION_BLEND_OTHER,
			       GR_COMBINE_FACTOR_LOCAL,
			       FXFALSE,FXFALSE);
      else
	FX_grTexCombine_NoLock(GR_TMU0,
			       GR_COMBINE_FUNCTION_BLEND_OTHER,
			       GR_COMBINE_FACTOR_LOCAL,
			       GR_COMBINE_FUNCTION_BLEND_OTHER,
			       GR_COMBINE_FACTOR_LOCAL,
			       FXFALSE,FXFALSE);

      FX_grColorCombine_NoLock(GR_COMBINE_FUNCTION_SCALE_OTHER,
			       GR_COMBINE_FACTOR_LOCAL,
			       localc,
			       GR_COMBINE_OTHER_TEXTURE,
			       FXFALSE);

      FX_grAlphaCombine_NoLock(GR_COMBINE_FUNCTION_SCALE_OTHER,
			       GR_COMBINE_FACTOR_LOCAL,
			       locala,
			       GR_COMBINE_OTHER_TEXTURE,
			       FXFALSE);
      break;
    }
  case (FX_UM_E0_REPLACE | FX_UM_E1_BLEND): /* Only for GLQuake */
    if (tmu1==FX_TMU1) {
      FX_grTexCombine_NoLock(GR_TMU1,
			     GR_COMBINE_FUNCTION_LOCAL,
			     GR_COMBINE_FACTOR_NONE,
			     GR_COMBINE_FUNCTION_LOCAL,
			     GR_COMBINE_FACTOR_NONE,
			     FXTRUE,FXFALSE);
		  
      FX_grTexCombine_NoLock(GR_TMU0,
			     GR_COMBINE_FUNCTION_BLEND_OTHER,
			     GR_COMBINE_FACTOR_LOCAL,
			     GR_COMBINE_FUNCTION_BLEND_OTHER,
			     GR_COMBINE_FACTOR_LOCAL,
			     FXFALSE,FXFALSE);
    } else {
      FX_grTexCombine_NoLock(GR_TMU1,
			     GR_COMBINE_FUNCTION_LOCAL,
			     GR_COMBINE_FACTOR_NONE,
			     GR_COMBINE_FUNCTION_LOCAL,
			     GR_COMBINE_FACTOR_NONE,
			     FXFALSE,FXFALSE);
		  
      FX_grTexCombine_NoLock(GR_TMU0,
			     GR_COMBINE_FUNCTION_BLEND_OTHER,
			     GR_COMBINE_FACTOR_ONE_MINUS_LOCAL,
			     GR_COMBINE_FUNCTION_BLEND_OTHER,
			     GR_COMBINE_FACTOR_ONE_MINUS_LOCAL,
			     FXFALSE,FXFALSE);
    }
	  
    FX_grAlphaCombine_NoLock(GR_COMBINE_FUNCTION_LOCAL,
			     GR_COMBINE_FACTOR_NONE,
			     locala,
			     GR_COMBINE_OTHER_NONE,
			     FXFALSE);

    FX_grColorCombine_NoLock(GR_COMBINE_FUNCTION_SCALE_OTHER,
			     GR_COMBINE_FACTOR_ONE,
			     localc,
			     GR_COMBINE_OTHER_TEXTURE,
			     FXFALSE);
    break;
  case (FX_UM_E0_REPLACE | FX_UM_E1_MODULATE): /* Quake 2 and 3 */
    if (tmu1==FX_TMU1) {
      FX_grTexCombine_NoLock(GR_TMU1,
			     GR_COMBINE_FUNCTION_LOCAL,
			     GR_COMBINE_FACTOR_NONE,
			     GR_COMBINE_FUNCTION_ZERO,
			     GR_COMBINE_FACTOR_NONE,
			     FXFALSE,FXTRUE);
		  
      FX_grTexCombine_NoLock(GR_TMU0,
			     GR_COMBINE_FUNCTION_BLEND_OTHER,
			     GR_COMBINE_FACTOR_LOCAL,
			     GR_COMBINE_FUNCTION_BLEND_OTHER,
			     GR_COMBINE_FACTOR_LOCAL,
			     FXFALSE,FXFALSE);

    } else {
      FX_grTexCombine_NoLock(GR_TMU1,
			     GR_COMBINE_FUNCTION_LOCAL,
			     GR_COMBINE_FACTOR_NONE,
			     GR_COMBINE_FUNCTION_LOCAL,
			     GR_COMBINE_FACTOR_NONE,
			     FXFALSE,FXFALSE);
		  
      FX_grTexCombine_NoLock(GR_TMU0,
			     GR_COMBINE_FUNCTION_BLEND_OTHER,
			     GR_COMBINE_FACTOR_LOCAL,
			     GR_COMBINE_FUNCTION_BLEND_OTHER,
			     GR_COMBINE_FACTOR_ONE,
			     FXFALSE,FXFALSE);
    }
	  
    if(ti0->baseLevelInternalFormat==GL_RGB)
      FX_grAlphaCombine_NoLock(GR_COMBINE_FUNCTION_LOCAL,
			       GR_COMBINE_FACTOR_NONE,
			       locala,
			       GR_COMBINE_OTHER_NONE,
			       FXFALSE);
    else
      FX_grAlphaCombine_NoLock(GR_COMBINE_FUNCTION_SCALE_OTHER,
			       GR_COMBINE_FACTOR_ONE,
			       locala,
			       GR_COMBINE_OTHER_NONE,
			       FXFALSE);


    FX_grColorCombine_NoLock(GR_COMBINE_FUNCTION_SCALE_OTHER,
			     GR_COMBINE_FACTOR_ONE,
			     localc,
			     GR_COMBINE_OTHER_TEXTURE,
			     FXFALSE);
    break;
  

  case (FX_UM_E0_MODULATE | FX_UM_E1_ADD): /* Quake 3 Sky */
    {
      GLboolean isalpha[FX_NUM_TMU];

      if(ti0->baseLevelInternalFormat==GL_ALPHA)
	isalpha[tmu0]=GL_TRUE;
      else
	isalpha[tmu0]=GL_FALSE;

      if(ti1->baseLevelInternalFormat==GL_ALPHA)
	isalpha[tmu1]=GL_TRUE;
      else
	isalpha[tmu1]=GL_FALSE;
	
      if(isalpha[FX_TMU1])
	FX_grTexCombine_NoLock(GR_TMU1,
			       GR_COMBINE_FUNCTION_ZERO,
			       GR_COMBINE_FACTOR_NONE,
			       GR_COMBINE_FUNCTION_LOCAL,
			       GR_COMBINE_FACTOR_NONE,
			       FXTRUE,FXFALSE);
      else
	FX_grTexCombine_NoLock(GR_TMU1,
			       GR_COMBINE_FUNCTION_LOCAL,
			       GR_COMBINE_FACTOR_NONE,
			       GR_COMBINE_FUNCTION_LOCAL,
			       GR_COMBINE_FACTOR_NONE,
			       FXFALSE,FXFALSE);

      if(isalpha[FX_TMU0])
	FX_grTexCombine_NoLock(GR_TMU0,
			       GR_COMBINE_FUNCTION_SCALE_OTHER,
			       GR_COMBINE_FACTOR_ONE,
			       GR_COMBINE_FUNCTION_SCALE_OTHER_ADD_LOCAL,
			       GR_COMBINE_FACTOR_ONE,
			       FXFALSE,FXFALSE);
      else
	FX_grTexCombine_NoLock(GR_TMU0,
			       GR_COMBINE_FUNCTION_SCALE_OTHER_ADD_LOCAL,
			       GR_COMBINE_FACTOR_ONE,
			       GR_COMBINE_FUNCTION_SCALE_OTHER_ADD_LOCAL,
			       GR_COMBINE_FACTOR_ONE,
			       FXFALSE,FXFALSE);

      FX_grColorCombine_NoLock(GR_COMBINE_FUNCTION_SCALE_OTHER,
			       GR_COMBINE_FACTOR_LOCAL,
			       localc,
			       GR_COMBINE_OTHER_TEXTURE,
			       FXFALSE);

      FX_grAlphaCombine_NoLock(GR_COMBINE_FUNCTION_SCALE_OTHER,
			       GR_COMBINE_FACTOR_LOCAL,
			       locala,		     
			       GR_COMBINE_OTHER_TEXTURE,
			       FXFALSE);
      break;
    }
  default:
    fprintf(stderr, "Unexpected dual texture mode encountered\n");
    break;
  }

  if (MESA_VERBOSE&VERBOSE_DRIVER) {
     fprintf(stderr,"fxmesa: fxSetupTextureDoubleTMU(...) End\n");
  }
}

/************************* No Texture ***************************/

static void fxSetupTextureNone_NoLock(GLcontext *ctx)
{
  fxMesaContext fxMesa=(fxMesaContext)ctx->DriverCtx;
  GrCombineLocal_t localc,locala;

  if (MESA_VERBOSE&VERBOSE_DRIVER) {
     fprintf(stderr,"fxmesa: fxSetupTextureNone(...)\n");
  }

  if((ctx->Light.ShadeModel==GL_SMOOTH) ||
     (ctx->Point.SmoothFlag) ||
     (ctx->Line.SmoothFlag) ||
     (ctx->Polygon.SmoothFlag))
    locala=GR_COMBINE_LOCAL_ITERATED;
  else
    locala=GR_COMBINE_LOCAL_CONSTANT;
  
  if(ctx->Light.ShadeModel==GL_SMOOTH)
    localc=GR_COMBINE_LOCAL_ITERATED;
  else
    localc=GR_COMBINE_LOCAL_CONSTANT;
  
  FX_grAlphaCombine_NoLock(GR_COMBINE_FUNCTION_LOCAL,
			   GR_COMBINE_FACTOR_NONE,
			   locala,
			   GR_COMBINE_OTHER_NONE,
			   FXFALSE);

  FX_grColorCombine_NoLock(GR_COMBINE_FUNCTION_LOCAL,
			   GR_COMBINE_FACTOR_NONE,
			   localc,
			   GR_COMBINE_OTHER_NONE,
			   FXFALSE);

  fxMesa->lastUnitsMode=FX_UM_NONE;
}

/************************************************************************/
/************************** Texture Mode SetUp **************************/
/************************************************************************/

static void fxSetupTexture_NoLock(GLcontext *ctx)
{
  fxMesaContext fxMesa=(fxMesaContext)ctx->DriverCtx;
  GLuint tex2Denabled;

  if (MESA_VERBOSE&VERBOSE_DRIVER) {
     fprintf(stderr,"fxmesa: fxSetupTexture(...)\n");
  }

  /* Disable multipass texturing.
   */
  ctx->Driver.MultipassFunc = 0;

  /* Texture Combine, Color Combine and Alpha Combine.
   */  
  tex2Denabled = (ctx->Texture.ReallyEnabled & TEXTURE0_2D);

  if (fxMesa->emulateTwoTMUs)
     tex2Denabled |= (ctx->Texture.ReallyEnabled & TEXTURE1_2D);
  
  switch(tex2Denabled) {
  case TEXTURE0_2D:
    fxSetupTextureSingleTMU_NoLock(ctx,0);    
    break;
  case TEXTURE1_2D:
    fxSetupTextureSingleTMU_NoLock(ctx,1);
    break;
  case (TEXTURE0_2D|TEXTURE1_2D):
     if (fxMesa->haveTwoTMUs)
	fxSetupTextureDoubleTMU_NoLock(ctx);
     else {
	if (MESA_VERBOSE&VERBOSE_DRIVER)
	   fprintf(stderr, "fxmesa: enabling fake multitexture\n");

	fxSetupTextureSingleTMU_NoLock(ctx,0);
	ctx->Driver.MultipassFunc = fxMultipassTexture;
     }
    break;
  default:
    fxSetupTextureNone_NoLock(ctx);
    break;
  }
}

static void fxSetupTexture(GLcontext *ctx) {
  BEGIN_BOARD_LOCK();
  fxSetupTexture_NoLock(ctx);
  END_BOARD_LOCK();
}

/************************************************************************/
/**************************** Blend SetUp *******************************/
/************************************************************************/

/* XXX consider supporting GL_INGR_blend_func_separate */
void fxDDBlendFunc(GLcontext *ctx, GLenum sfactor, GLenum dfactor)
{
  fxMesaContext fxMesa=(fxMesaContext)ctx->DriverCtx;
  tfxUnitsState *us=&fxMesa->unitsState;
  GrAlphaBlendFnc_t sfact,dfact,asfact,adfact;

  /* From the Glide documentation:
     For alpha source and destination blend function factor
     parameters, Voodoo Graphics supports only
     GR_BLEND_ZERO and GR_BLEND_ONE.
  */

  switch(sfactor) {
  case GL_ZERO:
    asfact=sfact=GR_BLEND_ZERO;
    break;
  case GL_ONE:
    asfact=sfact=GR_BLEND_ONE;
    break;
  case GL_DST_COLOR:
    sfact=GR_BLEND_DST_COLOR;
    asfact=GR_BLEND_ONE;
    break;
  case GL_ONE_MINUS_DST_COLOR:
    sfact=GR_BLEND_ONE_MINUS_DST_COLOR;
    asfact=GR_BLEND_ONE;
    break;
  case GL_SRC_ALPHA:
    sfact=GR_BLEND_SRC_ALPHA;
    asfact=GR_BLEND_ONE;
    break;
  case GL_ONE_MINUS_SRC_ALPHA:
    sfact=GR_BLEND_ONE_MINUS_SRC_ALPHA;
    asfact=GR_BLEND_ONE;
    break;
  case GL_DST_ALPHA:
    sfact=GR_BLEND_DST_ALPHA;
    asfact=GR_BLEND_ONE;
    break;
  case GL_ONE_MINUS_DST_ALPHA:
    sfact=GR_BLEND_ONE_MINUS_DST_ALPHA;
    asfact=GR_BLEND_ONE;
    break;
  case GL_SRC_ALPHA_SATURATE:
    sfact=GR_BLEND_ALPHA_SATURATE;
    asfact=GR_BLEND_ONE;
    break;
  case GL_SRC_COLOR:
  case GL_ONE_MINUS_SRC_COLOR:
    /* USELESS */
    asfact=sfact=GR_BLEND_ONE;
    break;
  default:
    asfact=sfact=GR_BLEND_ONE;
    break;
  }

  if((sfact!=us->blendSrcFuncRGB) ||
     (asfact!=us->blendSrcFuncAlpha)) {
    us->blendSrcFuncRGB=sfact;
    us->blendSrcFuncAlpha=asfact;
    fxMesa->new_state |= FX_NEW_BLEND;
    ctx->Driver.RenderStart = fxSetupFXUnits;
  }

  switch(dfactor) {
  case GL_ZERO:
    adfact=dfact=GR_BLEND_ZERO;
    break;
  case GL_ONE:
    adfact=dfact=GR_BLEND_ONE;
    break;
  case GL_SRC_COLOR:
    dfact=GR_BLEND_SRC_COLOR;
    adfact=GR_BLEND_ZERO;
    break;
  case GL_ONE_MINUS_SRC_COLOR:
    dfact=GR_BLEND_ONE_MINUS_SRC_COLOR;
    adfact=GR_BLEND_ZERO;
    break;
  case GL_SRC_ALPHA:
    dfact=GR_BLEND_SRC_ALPHA;
    adfact=GR_BLEND_ZERO;
    break;
  case GL_ONE_MINUS_SRC_ALPHA:
    dfact=GR_BLEND_ONE_MINUS_SRC_ALPHA;
    adfact=GR_BLEND_ZERO;
    break;
  case GL_DST_ALPHA:
    /* dfact=GR_BLEND_DST_ALPHA; */
    /* We can't do DST_ALPHA */
    dfact=GR_BLEND_ONE;
    adfact=GR_BLEND_ZERO;
    break;
  case GL_ONE_MINUS_DST_ALPHA:
    /* dfact=GR_BLEND_ONE_MINUS_DST_ALPHA; */
    /* We can't do DST_ALPHA */
    dfact=GR_BLEND_ZERO;
    adfact=GR_BLEND_ZERO;
    break;
  case GL_SRC_ALPHA_SATURATE:
  case GL_DST_COLOR:
  case GL_ONE_MINUS_DST_COLOR:
    /* USELESS */
    adfact=dfact=GR_BLEND_ZERO;
    break;
  default:
    adfact=dfact=GR_BLEND_ZERO;
    break;
  }

  if((dfact!=us->blendDstFuncRGB) ||
     (adfact!=us->blendDstFuncAlpha)) {
    us->blendDstFuncRGB=dfact;
    us->blendDstFuncAlpha=adfact;
    fxMesa->new_state |= FX_NEW_BLEND;
    ctx->Driver.RenderStart = fxSetupFXUnits;
  }
}

static void fxSetupBlend(GLcontext *ctx)
{
  fxMesaContext fxMesa=(fxMesaContext)ctx->DriverCtx;
  tfxUnitsState *us=&fxMesa->unitsState;

  if(us->blendEnabled)
     FX_grAlphaBlendFunction(us->blendSrcFuncRGB,us->blendDstFuncRGB,
			  us->blendSrcFuncAlpha,us->blendDstFuncAlpha);
  else
     FX_grAlphaBlendFunction(GR_BLEND_ONE,GR_BLEND_ZERO,GR_BLEND_ONE,GR_BLEND_ZERO);
}
  
/************************************************************************/
/************************** Alpha Test SetUp ****************************/
/************************************************************************/

void fxDDAlphaFunc(GLcontext *ctx, GLenum func, GLclampf ref)
{
  fxMesaContext fxMesa=(fxMesaContext)ctx->DriverCtx;
  tfxUnitsState *us=&fxMesa->unitsState;
  GrCmpFnc_t newfunc;

  switch(func) {
  case GL_NEVER:
    newfunc=GR_CMP_NEVER;
    break;
  case GL_LESS:
    newfunc=GR_CMP_LESS;
    break;
  case GL_EQUAL:
    newfunc=GR_CMP_EQUAL;
    break;
  case GL_LEQUAL:
    newfunc=GR_CMP_LEQUAL;
    break;
  case GL_GREATER:
    newfunc=GR_CMP_GREATER;
    break;
  case GL_NOTEQUAL:
    newfunc=GR_CMP_NOTEQUAL;
    break;
  case GL_GEQUAL:
    newfunc=GR_CMP_GEQUAL;
    break;
  case GL_ALWAYS:
    newfunc=GR_CMP_ALWAYS;
    break;
  default:
    fprintf(stderr,"fx Driver: internal error in fxDDAlphaFunc()\n");
    fxCloseHardware();
    exit(-1);
    break;
  }

  if(newfunc!=us->alphaTestFunc) {
    us->alphaTestFunc=newfunc;
    fxMesa->new_state |= FX_NEW_ALPHA;
    ctx->Driver.RenderStart = fxSetupFXUnits;
  }

  if(ctx->Color.AlphaRef!=us->alphaTestRefValue) {
    us->alphaTestRefValue=ctx->Color.AlphaRef;
    fxMesa->new_state |= FX_NEW_ALPHA;
    ctx->Driver.RenderStart = fxSetupFXUnits;
  }
}

static void fxSetupAlphaTest(GLcontext *ctx)
{
  fxMesaContext fxMesa=(fxMesaContext)ctx->DriverCtx;
  tfxUnitsState *us=&fxMesa->unitsState;

  if(us->alphaTestEnabled) {
     FX_grAlphaTestFunction(us->alphaTestFunc);
     FX_grAlphaTestReferenceValue(us->alphaTestRefValue);
  } else
     FX_grAlphaTestFunction(GR_CMP_ALWAYS);
}

/************************************************************************/
/************************** Depth Test SetUp ****************************/
/************************************************************************/

void fxDDDepthFunc(GLcontext *ctx, GLenum func)
{
  fxMesaContext fxMesa=(fxMesaContext)ctx->DriverCtx;
  tfxUnitsState *us=&fxMesa->unitsState;
  GrCmpFnc_t dfunc;

  switch(func) {
  case GL_NEVER:
    dfunc=GR_CMP_NEVER;
    break;
  case GL_LESS:
    dfunc=GR_CMP_LESS;
    break;
  case GL_GEQUAL:
    dfunc=GR_CMP_GEQUAL;
    break;
  case GL_LEQUAL:
    dfunc=GR_CMP_LEQUAL;
    break;
  case GL_GREATER:
    dfunc=GR_CMP_GREATER;
    break;
  case GL_NOTEQUAL:
    dfunc=GR_CMP_NOTEQUAL;
    break;
  case GL_EQUAL:
    dfunc=GR_CMP_EQUAL;
    break;
  case GL_ALWAYS:
    dfunc=GR_CMP_ALWAYS;
    break;
  default:
    fprintf(stderr,"fx Driver: internal error in fxDDDepthFunc()\n");
    fxCloseHardware();
    exit(-1);
    break;
  }

  if(dfunc!=us->depthTestFunc) {
    us->depthTestFunc=dfunc;
    fxMesa->new_state |= FX_NEW_DEPTH;
    ctx->Driver.RenderStart = fxSetupFXUnits;
  }

}

void fxDDDepthMask(GLcontext *ctx, GLboolean flag)
{
  fxMesaContext fxMesa=(fxMesaContext)ctx->DriverCtx;
  tfxUnitsState *us=&fxMesa->unitsState;

  if(flag!=us->depthMask) {
    us->depthMask=flag;
    fxMesa->new_state |= FX_NEW_DEPTH;
    ctx->Driver.RenderStart = fxSetupFXUnits;
  }
}

static void fxSetupDepthTest(GLcontext *ctx)
{
  fxMesaContext fxMesa=(fxMesaContext)ctx->DriverCtx;
  tfxUnitsState *us=&fxMesa->unitsState;

  if(us->depthTestEnabled)
     FX_grDepthBufferFunction(us->depthTestFunc);
  else
     FX_grDepthBufferFunction(GR_CMP_ALWAYS);

  FX_grDepthMask(us->depthMask);
}

/************************************************************************/
/**************************** Color Mask SetUp **************************/
/************************************************************************/

GLboolean fxDDColorMask(GLcontext *ctx, 
			GLboolean r, GLboolean g, 
			GLboolean b, GLboolean a )
{
  fxMesaContext fxMesa=(fxMesaContext)ctx->DriverCtx;
  fxMesa->new_state |= FX_NEW_COLOR_MASK;
  ctx->Driver.RenderStart = fxSetupFXUnits;
  (void) r; (void) g; (void) b; (void) a;
  return GL_FALSE;
}

static void fxSetupColorMask(GLcontext *ctx)
{
  fxMesaContext fxMesa = FX_CONTEXT(ctx);

  FX_grColorMask(ctx->Color.ColorMask[RCOMP] ||
	      ctx->Color.ColorMask[GCOMP] ||
	      ctx->Color.ColorMask[BCOMP],
	      ctx->Color.ColorMask[ACOMP] && fxMesa->haveAlphaBuffer);
}



/************************************************************************/
/**************************** Fog Mode SetUp ****************************/
/************************************************************************/

/*
 * This is called during state update in order to update the Glide fog state.
 */
static void fxSetupFog(GLcontext *ctx)
{
  if (ctx->Fog.Enabled && ctx->FogMode==FOG_FRAGMENT) {
    fxMesaContext fxMesa = FX_CONTEXT(ctx);

    /* update fog color */
    GLubyte col[4];
    col[0]=(unsigned int)(255*ctx->Fog.Color[0]);
    col[1]=(unsigned int)(255*ctx->Fog.Color[1]);
    col[2]=(unsigned int)(255*ctx->Fog.Color[2]); 
    col[3]=(unsigned int)(255*ctx->Fog.Color[3]);
    FX_grFogColorValue(FXCOLOR4(col));

    if(fxMesa->fogTableMode != ctx->Fog.Mode ||
       fxMesa->fogDensity != ctx->Fog.Density ||
       fxMesa->fogStart != ctx->Fog.Start ||
       fxMesa->fogEnd != ctx->Fog.End) {
      /* reload the fog table */
      switch (ctx->Fog.Mode) {
        case GL_LINEAR:
          guFogGenerateLinear(fxMesa->fogTable, ctx->Fog.Start, ctx->Fog.End);
          break;
        case GL_EXP:
          guFogGenerateExp(fxMesa->fogTable, ctx->Fog.Density);
          break;
        case GL_EXP2:
          guFogGenerateExp2(fxMesa->fogTable, ctx->Fog.Density);
          break;
        default:
          ;
      }
      fxMesa->fogTableMode = ctx->Fog.Mode;
      fxMesa->fogDensity = ctx->Fog.Density;
      fxMesa->fogStart = ctx->Fog.Start;
      fxMesa->fogEnd = ctx->Fog.End;
    }

    FX_grFogTable(fxMesa->fogTable);
    FX_grFogMode(GR_FOG_WITH_TABLE);
  }
  else {
    FX_grFogMode(GR_FOG_DISABLE);
  }
}

void fxDDFogfv( GLcontext *ctx, GLenum pname, const GLfloat *params )
{
   FX_CONTEXT(ctx)->new_state |= FX_NEW_FOG;
   ctx->Driver.RenderStart = fxSetupFXUnits;  /* XXX why is this here? */
}

/************************************************************************/
/************************** Scissor Test SetUp **************************/
/************************************************************************/

/* This routine is used in managing the lock state, and therefore can't lock */
void fxSetScissorValues(GLcontext *ctx)
{
  fxMesaContext fxMesa=(fxMesaContext)ctx->DriverCtx;
  int xmin, xmax;
  int ymin, ymax, check;

  if (ctx->Scissor.Enabled) {
    xmin=ctx->Scissor.X;
    xmax=ctx->Scissor.X+ctx->Scissor.Width;
    ymin=ctx->Scissor.Y;
    ymax=ctx->Scissor.Y+ctx->Scissor.Height;
    check=1;
  } else {
    xmin=0;
    ymin=0;
    xmax=fxMesa->width;
    ymax=fxMesa->height;
    check=0;
  }
  xmin+=fxMesa->x_offset;
  xmax+=fxMesa->x_offset;
  ymin+=fxMesa->y_delta;
  ymax+=fxMesa->y_delta;
  if (xmin<fxMesa->clipMinX) xmin=fxMesa->clipMinX;
  if (xmax>fxMesa->clipMaxX) xmax=fxMesa->clipMaxX;
  if (ymin<fxMesa->screen_height-fxMesa->clipMaxY) 
    ymin=fxMesa->screen_height-fxMesa->clipMaxY;
  if (ymax>fxMesa->screen_height-fxMesa->clipMinY)
    ymax=fxMesa->screen_height-fxMesa->clipMinY;
  FX_grClipWindow_NoLock(xmin, ymin, xmax, ymax);
}

static void fxSetupScissor(GLcontext *ctx)
{
  fxMesaContext fxMesa=(fxMesaContext)ctx->DriverCtx;
  if (!fxMesa->needClip) {
    BEGIN_BOARD_LOCK();
    fxSetScissorValues(ctx);
    END_BOARD_LOCK();
  }
}

void fxDDScissor( GLcontext *ctx, GLint x, GLint y, GLsizei w, GLsizei h )
{
   FX_CONTEXT(ctx)->new_state |= FX_NEW_SCISSOR;
   ctx->Driver.RenderStart = fxSetupFXUnits;
}

/************************************************************************/
/*************************** Cull mode setup ****************************/
/************************************************************************/


void fxDDCullFace(GLcontext *ctx, GLenum mode)
{
   (void) mode;
   FX_CONTEXT(ctx)->new_state |= FX_NEW_CULL;
   ctx->Driver.RenderStart = fxSetupFXUnits;   
}

void fxDDFrontFace(GLcontext *ctx, GLenum mode)
{
   (void) mode;
   FX_CONTEXT(ctx)->new_state |= FX_NEW_CULL;
   ctx->Driver.RenderStart = fxSetupFXUnits;   
}


static void fxSetupCull(GLcontext *ctx)
{
  if (ctx->Polygon.CullFlag) {
    switch (ctx->Polygon.CullFaceMode) {
    case GL_BACK:
      if (ctx->Polygon.FrontFace==GL_CCW)
	FX_CONTEXT(ctx)->cullMode=GR_CULL_NEGATIVE;
      else
	FX_CONTEXT(ctx)->cullMode=GR_CULL_POSITIVE;
      break;
    case GL_FRONT:
      if(ctx->Polygon.FrontFace==GL_CCW)
	FX_CONTEXT(ctx)->cullMode=GR_CULL_POSITIVE;
      else
	FX_CONTEXT(ctx)->cullMode=GR_CULL_NEGATIVE;
      break;
    case GL_FRONT_AND_BACK:
      FX_CONTEXT(ctx)->cullMode=GR_CULL_DISABLE;
      break;
    default:
      break;
    }
  } else FX_CONTEXT(ctx)->cullMode=GR_CULL_DISABLE;
  FX_grCullMode(FX_CONTEXT(ctx)->cullMode);
}


/************************************************************************/
/****************************** DD Enable ******************************/
/************************************************************************/

void fxDDEnable(GLcontext *ctx, GLenum cap, GLboolean state)
{
  fxMesaContext fxMesa=(fxMesaContext)ctx->DriverCtx;
  tfxUnitsState *us=&fxMesa->unitsState;

  if (MESA_VERBOSE&VERBOSE_DRIVER) {
     fprintf(stderr,"fxmesa: fxDDEnable(...)\n");
  }

  switch(cap) {
  case GL_ALPHA_TEST:
    if(state!=us->alphaTestEnabled) {
      us->alphaTestEnabled=state;
      fxMesa->new_state |= FX_NEW_ALPHA;
      ctx->Driver.RenderStart = fxSetupFXUnits;
    }
    break;
  case GL_BLEND:
    if(state!=us->blendEnabled) {
      us->blendEnabled=state;
      fxMesa->new_state |= FX_NEW_BLEND;
      ctx->Driver.RenderStart = fxSetupFXUnits;
    }
    break;
  case GL_DEPTH_TEST:
    if(state!=us->depthTestEnabled) {
      us->depthTestEnabled=state;
      fxMesa->new_state |= FX_NEW_DEPTH;
      ctx->Driver.RenderStart = fxSetupFXUnits;
    }
    break;
  case GL_SCISSOR_TEST:
     fxMesa->new_state |= FX_NEW_SCISSOR;
     ctx->Driver.RenderStart = fxSetupFXUnits;
     break;
  case GL_SHARED_TEXTURE_PALETTE_EXT:
     fxDDTexUseGlbPalette(ctx, state);
     break;
  case GL_FOG:
     fxMesa->new_state |= FX_NEW_FOG;
     ctx->Driver.RenderStart = fxSetupFXUnits;
     break;
  case GL_CULL_FACE:
     fxMesa->new_state |= FX_NEW_CULL;
     ctx->Driver.RenderStart = fxSetupFXUnits;
     break;
  case GL_LINE_SMOOTH:
  case GL_LINE_STIPPLE:
  case GL_POINT_SMOOTH:
  case GL_POLYGON_SMOOTH:
  case GL_TEXTURE_2D:
      fxMesa->new_state |= FX_NEW_TEXTURING;
      ctx->Driver.RenderStart = fxSetupFXUnits;
      break;
  default:
    ;  /* XXX no-op? */
  }    
}

#if 0
/*
  Multipass to do GL_BLEND texture functions
  Cf*(1-Ct) has already been written to the buffer during the first pass
  Cc*Ct gets written during the second pass (in this function)
  Everything gets reset in the third call (in this function)
*/
static GLboolean fxMultipassBlend(struct vertex_buffer *VB, GLuint pass)
{
  GLcontext *ctx = VB->ctx;
  fxMesaContext fxMesa = FX_CONTEXT(ctx);

  switch (pass) {
  case 1:
    /* Add Cc*Ct */
    fxMesa->restoreUnitsState=fxMesa->unitsState;
    if (ctx->Depth.Mask) {
      /* We don't want to check or change the depth buffers */
      switch (ctx->Depth.Func) {
      case GL_NEVER:
      case GL_ALWAYS:
	break;
      default:
	fxDDDepthFunc(ctx, GL_EQUAL);
	break;
      }
      fxDDDepthMask(ctx, FALSE);
    }
    /* Enable Cc*Ct mode */
    /* XXX Set the Constant Color ? */
    fxDDEnable(ctx, GL_BLEND, GL_TRUE);
    fxDDBlendFunc(ctx, XXX, XXX);
    fxSetupTextureSingleTMU(ctx, XXX);
    fxSetupBlend(ctx);
    fxSetupDepthTest(ctx);
    break;

  case 2:
    /* Reset everything back to normal */
    fxMesa->unitsState = fxMesa->restoreUnitsState;
    fxMesa->setupdone &= XXX;
    fxSetupTextureSingleTMU(ctx, XXX);
    fxSetupBlend(ctx);
    fxSetupDepthTest(ctx);
    break;
  }

  return pass==1;
}
#endif

/************************************************************************/
/******************** Fake Multitexture Support *************************/
/************************************************************************/

/* Its considered cheeky to try to fake ARB multitexture by doing
 * multipass rendering, because it is not possible to emulate the full
 * spec in this way.  The fact is that the voodoo 2 supports only a
 * subset of the possible multitexturing modes, and it is possible to
 * support almost the same subset using multipass blending on the
 * voodoo 1.  In all other cases for both voodoo 1 and 2, we fall back
 * to software rendering, satisfying the spec if not the user.  
 */
static GLboolean fxMultipassTexture( struct vertex_buffer *VB, GLuint pass )
{
   GLcontext *ctx = VB->ctx;
   fxVertex *v = FX_DRIVER_DATA(VB)->verts;
   fxVertex *last = FX_DRIVER_DATA(VB)->last_vert;
   fxMesaContext fxMesa = FX_CONTEXT(ctx);

   switch (pass) {
   case 1:
      if (MESA_VERBOSE&(VERBOSE_DRIVER|VERBOSE_PIPELINE|VERBOSE_TEXTURE))
	 fprintf(stderr, "fxmesa: Second texture pass\n");

      for ( ; v != last ; v++) {
	 v->f[S0COORD] = v->f[S1COORD];
	 v->f[T0COORD] = v->f[T1COORD];
      }

      fxMesa->restoreUnitsState = fxMesa->unitsState; 
      fxMesa->tmu_source[0] = 1;

      if (ctx->Depth.Mask) {
	 switch (ctx->Depth.Func) {
	 case GL_NEVER:
	 case GL_ALWAYS:
	    break;
	 default:
	    fxDDDepthFunc( ctx, GL_EQUAL );
	    break;
	 }

	 fxDDDepthMask( ctx, GL_FALSE ); 
      }
      
      if (ctx->Texture.Unit[1].EnvMode == GL_MODULATE) {
	 fxDDEnable( ctx, GL_BLEND, GL_TRUE );
	 fxDDBlendFunc( ctx, GL_DST_COLOR, GL_ZERO );
      }

      fxSetupTextureSingleTMU( ctx, 1 ); 
      fxSetupBlend( ctx );
      fxSetupDepthTest( ctx );
      break;

   case 2:
      /* Restore original state.  
       */
      fxMesa->tmu_source[0] = 0;
      fxMesa->unitsState = fxMesa->restoreUnitsState;
      fxMesa->setupdone &= ~SETUP_TMU0;
      fxSetupTextureSingleTMU( ctx, 0 ); 
      fxSetupBlend( ctx );
      fxSetupDepthTest( ctx );
      break;
   }

   return pass == 1;      
}


/************************************************************************/
/************************** Changes to units state **********************/
/************************************************************************/


/* All units setup is handled under texture setup.
 */
void fxDDShadeModel(GLcontext *ctx, GLenum mode)
{
   FX_CONTEXT(ctx)->new_state |= FX_NEW_TEXTURING;
   ctx->Driver.RenderStart = fxSetupFXUnits;
}



/************************************************************************/
/****************************** Units SetUp *****************************/
/************************************************************************/
static void gl_print_fx_state_flags( const char *msg, GLuint flags )
{
   fprintf(stderr, 
	   "%s: (0x%x) %s%s%s%s%s%s%s\n",
	   msg,
	   flags,
	   (flags & FX_NEW_TEXTURING)   ? "texture, " : "",
	   (flags & FX_NEW_BLEND)       ? "blend, " : "",
	   (flags & FX_NEW_ALPHA)       ? "alpha, " : "",
	   (flags & FX_NEW_FOG)         ? "fog, " : "",
	   (flags & FX_NEW_SCISSOR)     ? "scissor, " : "",
	   (flags & FX_NEW_COLOR_MASK)  ? "colormask, " : "",
	   (flags & FX_NEW_CULL)        ? "cull, " : "");
}

void fxSetupFXUnits( GLcontext *ctx )
{
  fxMesaContext fxMesa=(fxMesaContext)ctx->DriverCtx;
  GLuint newstate = fxMesa->new_state;

  if (MESA_VERBOSE&VERBOSE_DRIVER)
     gl_print_fx_state_flags("fxmesa: fxSetupFXUnits", newstate);

  if (newstate) {
     if (newstate & FX_NEW_TEXTURING)
	fxSetupTexture(ctx);

     if (newstate & FX_NEW_BLEND)
	fxSetupBlend(ctx);

     if (newstate & FX_NEW_ALPHA)
	fxSetupAlphaTest(ctx);
     
     if (newstate & FX_NEW_DEPTH)
	fxSetupDepthTest(ctx);

     if (newstate & FX_NEW_FOG)
	fxSetupFog(ctx);

     if (newstate & FX_NEW_SCISSOR)
	fxSetupScissor(ctx);

     if (newstate & FX_NEW_COLOR_MASK)
	fxSetupColorMask(ctx);

     if (newstate & FX_NEW_CULL)
	fxSetupCull(ctx);     

     fxMesa->new_state = 0;
/*       ctx->Driver.RenderStart = 0; */
  }
}



#else


/*
 * Need this to provide at least one external definition.
 */

int gl_fx_dummy_function_setup(void)
{
  return 0;
}

#endif  /* FX */