aboutsummaryrefslogtreecommitdiffstats
path: root/src/mesa/drivers/dri/r300/r300_state.c
blob: ebff4fc23d9cc4a8003b2c7fc066b63bdf0d94ed (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
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
/*
Copyright (C) The Weather Channel, Inc.  2002.
Copyright (C) 2004 Nicolai Haehnle.
All Rights Reserved.

The Weather Channel (TM) funded Tungsten Graphics to develop the
initial release of the Radeon 8500 driver under the XFree86 license.
This notice must be preserved.

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 (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 NONINFRINGEMENT.
IN NO EVENT SHALL THE COPYRIGHT OWNER(S) 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.

**************************************************************************/

/*
 * Authors:
 *   Nicolai Haehnle <prefect_@gmx.net>
 */

#include "glheader.h"
#include "state.h"
#include "imports.h"
#include "enums.h"
#include "macros.h"
#include "context.h"
#include "dd.h"
#include "simple_list.h"

#include "api_arrayelt.h"
#include "swrast/swrast.h"
#include "swrast_setup/swrast_setup.h"
#include "array_cache/acache.h"
#include "tnl/tnl.h"
#include "texformat.h"

#include "radeon_ioctl.h"
#include "radeon_state.h"
#include "r300_context.h"
#include "r300_ioctl.h"
#include "r300_state.h"
#include "r300_reg.h"
#include "r300_program.h"
#include "r300_emit.h"
#include "r300_fixed_pipelines.h"
#include "r300_tex.h"
#include "r300_maos.h"
#include "r300_texprog.h"

static void r300AlphaFunc(GLcontext * ctx, GLenum func, GLfloat ref)
{
	r300ContextPtr rmesa = R300_CONTEXT(ctx);
	int pp_misc = rmesa->hw.at.cmd[R300_AT_ALPHA_TEST];
	GLubyte refByte;

	CLAMPED_FLOAT_TO_UBYTE(refByte, ref);
	
	R300_STATECHANGE(rmesa, at);

	pp_misc &= ~(R300_ALPHA_TEST_OP_MASK | R300_REF_ALPHA_MASK);
	pp_misc |= (refByte & R300_REF_ALPHA_MASK);

	switch (func) {
	case GL_NEVER:
		pp_misc |= R300_ALPHA_TEST_FAIL;
		break;
	case GL_LESS:
		pp_misc |= R300_ALPHA_TEST_LESS;
		break;
	case GL_EQUAL:
		pp_misc |= R300_ALPHA_TEST_EQUAL;
		break;
	case GL_LEQUAL:
		pp_misc |= R300_ALPHA_TEST_LEQUAL;
		break;
	case GL_GREATER:
		pp_misc |= R300_ALPHA_TEST_GREATER;
		break;
	case GL_NOTEQUAL:
		pp_misc |= R300_ALPHA_TEST_NEQUAL;
		break;
	case GL_GEQUAL:
		pp_misc |= R300_ALPHA_TEST_GEQUAL;
		break;
	case GL_ALWAYS:
		pp_misc |= R300_ALPHA_TEST_PASS;
		//pp_misc &= ~R300_ALPHA_TEST_ENABLE;
		break;
	}

	rmesa->hw.at.cmd[R300_AT_ALPHA_TEST] = pp_misc;
}

static void r300BlendColor(GLcontext * ctx, const GLfloat cf[4])
{
	GLubyte color[4];
	r300ContextPtr rmesa = R300_CONTEXT(ctx);

	R300_STATECHANGE(rmesa, unk4E10);

	/* Ordering might be wrong */
	CLAMPED_FLOAT_TO_UBYTE(color[0], cf[0]);
	CLAMPED_FLOAT_TO_UBYTE(color[1], cf[1]);
	CLAMPED_FLOAT_TO_UBYTE(color[2], cf[2]);
	CLAMPED_FLOAT_TO_UBYTE(color[3], cf[3]);

	rmesa->hw.unk4E10.cmd[1]=r300PackColor(4, color[0], color[1], color[2], color[3]);
	//fprintf(stderr, "%s:%s is not implemented yet. Fixme !\n", __FILE__, __FUNCTION__);
#if 0
	R200_STATECHANGE(rmesa, ctx);
	CLAMPED_FLOAT_TO_UBYTE(color[0], cf[0]);
	CLAMPED_FLOAT_TO_UBYTE(color[1], cf[1]);
	CLAMPED_FLOAT_TO_UBYTE(color[2], cf[2]);
	CLAMPED_FLOAT_TO_UBYTE(color[3], cf[3]);
	if (rmesa->radeon.radeonScreen->drmSupportsBlendColor)
		rmesa->hw.ctx.cmd[CTX_RB3D_BLENDCOLOR] =
		    radeonPackColor(4, color[0], color[1], color[2], color[3]);
#endif
}

/**
 * Calculate the hardware blend factor setting.  This same function is used
 * for source and destination of both alpha and RGB.
 *
 * \returns
 * The hardware register value for the specified blend factor.  This value
 * will need to be shifted into the correct position for either source or
 * destination factor.
 *
 * \todo
 * Since the two cases where source and destination are handled differently
 * are essentially error cases, they should never happen.  Determine if these
 * cases can be removed.
 */
static int blend_factor(GLenum factor, GLboolean is_src)
{
	int func;

	switch (factor) {
	case GL_ZERO:
		func = R200_BLEND_GL_ZERO;
		break;
	case GL_ONE:
		func = R200_BLEND_GL_ONE;
		break;
	case GL_DST_COLOR:
		func = R200_BLEND_GL_DST_COLOR;
		break;
	case GL_ONE_MINUS_DST_COLOR:
		func = R200_BLEND_GL_ONE_MINUS_DST_COLOR;
		break;
	case GL_SRC_COLOR:
		func = R200_BLEND_GL_SRC_COLOR;
		break;
	case GL_ONE_MINUS_SRC_COLOR:
		func = R200_BLEND_GL_ONE_MINUS_SRC_COLOR;
		break;
	case GL_SRC_ALPHA:
		func = R200_BLEND_GL_SRC_ALPHA;
		break;
	case GL_ONE_MINUS_SRC_ALPHA:
		func = R200_BLEND_GL_ONE_MINUS_SRC_ALPHA;
		break;
	case GL_DST_ALPHA:
		func = R200_BLEND_GL_DST_ALPHA;
		break;
	case GL_ONE_MINUS_DST_ALPHA:
		func = R200_BLEND_GL_ONE_MINUS_DST_ALPHA;
		break;
	case GL_SRC_ALPHA_SATURATE:
		func =
		    (is_src) ? R200_BLEND_GL_SRC_ALPHA_SATURATE :
		    R200_BLEND_GL_ZERO;
		break;
	case GL_CONSTANT_COLOR:
		func = R200_BLEND_GL_CONST_COLOR;
		break;
	case GL_ONE_MINUS_CONSTANT_COLOR:
		func = R200_BLEND_GL_ONE_MINUS_CONST_COLOR;
		break;
	case GL_CONSTANT_ALPHA:
		func = R200_BLEND_GL_CONST_ALPHA;
		break;
	case GL_ONE_MINUS_CONSTANT_ALPHA:
		func = R200_BLEND_GL_ONE_MINUS_CONST_ALPHA;
		break;
	default:
		func = (is_src) ? R200_BLEND_GL_ONE : R200_BLEND_GL_ZERO;
	}
	return func;
}

/**
 * Sets both the blend equation and the blend function.
 * This is done in a single
 * function because some blend equations (i.e., \c GL_MIN and \c GL_MAX)
 * change the interpretation of the blend function.
 * Also, make sure that blend function and blend equation are set to their default
 * value if color blending is not enabled, since at least blend equations GL_MIN
 * and GL_FUNC_REVERSE_SUBTRACT will cause wrong results otherwise for
 * unknown reasons.
 */

/* helper function */
static void r300_set_blend_cntl(r300ContextPtr r300, int func, int eqn, int cbits, int funcA, int eqnA)
{
	GLuint new_ablend, new_cblend;

#if 0
	fprintf(stderr, "eqnA=%08x funcA=%08x eqn=%08x func=%08x cbits=%08x\n", eqnA, funcA, eqn, func, cbits);
#endif
	new_ablend = eqnA | funcA;
	new_cblend = eqn | func;

	/* Some blend factor combinations don't seem to work when the
	 * BLEND_NO_SEPARATE bit is set.
	 *
	 * Especially problematic candidates are the ONE_MINUS_* flags,
	 * but I can't see a real pattern.
	 */
#if 0
	if (new_ablend == new_cblend) {
		new_cblend |=  R300_BLEND_NO_SEPARATE;
	}
#endif
	new_cblend |= cbits;

	if((new_ablend != r300->hw.bld.cmd[R300_BLD_ABLEND]) ||
	   (new_cblend != r300->hw.bld.cmd[R300_BLD_CBLEND])) {
		R300_STATECHANGE(r300, bld);
		r300->hw.bld.cmd[R300_BLD_ABLEND]=new_ablend;
		r300->hw.bld.cmd[R300_BLD_CBLEND]=new_cblend;
	}
}


static void r300_set_blend_state(GLcontext * ctx)
{
	r300ContextPtr r300 = R300_CONTEXT(ctx);
	int func = (R200_BLEND_GL_ONE << R200_SRC_BLEND_SHIFT) |
	    (R200_BLEND_GL_ZERO << R200_DST_BLEND_SHIFT);
	int eqn = R200_COMB_FCN_ADD_CLAMP;
	int funcA = (R200_BLEND_GL_ONE << R200_SRC_BLEND_SHIFT) |
	    (R200_BLEND_GL_ZERO << R200_DST_BLEND_SHIFT);
	int eqnA = R200_COMB_FCN_ADD_CLAMP;

	if (ctx->Color._LogicOpEnabled || !ctx->Color.BlendEnabled) {
		r300_set_blend_cntl(r300,
			func, eqn, 0,
			func, eqn);
		return;
	}

	func = (blend_factor(ctx->Color.BlendSrcRGB, GL_TRUE) << R200_SRC_BLEND_SHIFT) |
		(blend_factor(ctx->Color.BlendDstRGB, GL_FALSE) << R200_DST_BLEND_SHIFT);

	switch (ctx->Color.BlendEquationRGB) {
	case GL_FUNC_ADD:
		eqn = R300_COMB_FCN_ADD_CLAMP;
		break;

	case GL_FUNC_SUBTRACT:
		eqn = R300_COMB_FCN_SUB_CLAMP;
		break;

	case GL_FUNC_REVERSE_SUBTRACT:
		eqn = R200_COMB_FCN_RSUB_CLAMP;
		break;

	case GL_MIN:
		eqn = R200_COMB_FCN_MIN;
		func = (R200_BLEND_GL_ONE << R200_SRC_BLEND_SHIFT) |
		    (R200_BLEND_GL_ONE << R200_DST_BLEND_SHIFT);
		break;

	case GL_MAX:
		eqn = R200_COMB_FCN_MAX;
		func = (R200_BLEND_GL_ONE << R200_SRC_BLEND_SHIFT) |
		    (R200_BLEND_GL_ONE << R200_DST_BLEND_SHIFT);
		break;

	default:
		fprintf(stderr,
			"[%s:%u] Invalid RGB blend equation (0x%04x).\n",
			__func__, __LINE__, ctx->Color.BlendEquationRGB);
		return;
	}


	funcA = (blend_factor(ctx->Color.BlendSrcA, GL_TRUE) << R200_SRC_BLEND_SHIFT) |
		(blend_factor(ctx->Color.BlendDstA, GL_FALSE) << R200_DST_BLEND_SHIFT);

	switch (ctx->Color.BlendEquationA) {
	case GL_FUNC_ADD:
		eqnA = R300_COMB_FCN_ADD_CLAMP;
		break;

	case GL_FUNC_SUBTRACT:
		eqnA = R300_COMB_FCN_SUB_CLAMP;
		break;

	case GL_FUNC_REVERSE_SUBTRACT:
		eqnA = R200_COMB_FCN_RSUB_CLAMP;
		break;

	case GL_MIN:
		eqnA = R200_COMB_FCN_MIN;
		funcA = (R200_BLEND_GL_ONE << R200_SRC_BLEND_SHIFT) |
		    (R200_BLEND_GL_ONE << R200_DST_BLEND_SHIFT);
		break;

	case GL_MAX:
		eqnA = R200_COMB_FCN_MAX;
		funcA = (R200_BLEND_GL_ONE << R200_SRC_BLEND_SHIFT) |
		    (R200_BLEND_GL_ONE << R200_DST_BLEND_SHIFT);
		break;

	default:
		fprintf(stderr, "[%s:%u] Invalid A blend equation (0x%04x).\n",
			__func__, __LINE__, ctx->Color.BlendEquationA);
		return;
	}

	r300_set_blend_cntl(r300,
		func, eqn, R300_BLEND_UNKNOWN | R300_BLEND_ENABLE,
		funcA, eqnA);
}

static void r300BlendEquationSeparate(GLcontext * ctx,
				      GLenum modeRGB, GLenum modeA)
{
	r300_set_blend_state(ctx);
}

static void r300BlendFuncSeparate(GLcontext * ctx,
				  GLenum sfactorRGB, GLenum dfactorRGB,
				  GLenum sfactorA, GLenum dfactorA)
{
	r300_set_blend_state(ctx);
}

/**
 * Update our tracked culling state based on Mesa's state.
 */
static void r300UpdateCulling(GLcontext* ctx)
{
	r300ContextPtr r300 = R300_CONTEXT(ctx);
	uint32_t val = 0;

	R300_STATECHANGE(r300, cul);
	if (ctx->Polygon.CullFlag) {
		if (ctx->Polygon.CullFaceMode == GL_FRONT_AND_BACK)
			val = R300_CULL_FRONT|R300_CULL_BACK;
		else if (ctx->Polygon.CullFaceMode == GL_FRONT)
			val = R300_CULL_FRONT;
		else
			val = R300_CULL_BACK;

		if (ctx->Polygon.FrontFace == GL_CW)
			val |= R300_FRONT_FACE_CW;
		else
			val |= R300_FRONT_FACE_CCW;
	}
	r300->hw.cul.cmd[R300_CUL_CULL] = val;
}

static void update_early_z(GLcontext* ctx)
{
	/* updates register 0x4f14 
	   if depth test is not enabled it should be 0x00000000
	   if depth is enabled and alpha not it should be 0x00000001
	   if depth and alpha is enabled it should be 0x00000000
	*/
	r300ContextPtr r300 = R300_CONTEXT(ctx);

	R300_STATECHANGE(r300, unk4F10);
	if (ctx->Color.AlphaEnabled)
		/* disable early Z */
		r300->hw.unk4F10.cmd[2] = 0x00000000;
	else {
		if (ctx->Depth.Test)
			/* enable early Z */
			r300->hw.unk4F10.cmd[2] = 0x00000001;
		else
			/* disable early Z */
			r300->hw.unk4F10.cmd[2] = 0x00000000;
	}
}

/**
 * Handle glEnable()/glDisable().
 *
 * \note Mesa already filters redundant calls to glEnable/glDisable.
 */
static void r300Enable(GLcontext* ctx, GLenum cap, GLboolean state)
{
	r300ContextPtr r300 = R300_CONTEXT(ctx);
	uint32_t newval;

	if (RADEON_DEBUG & DEBUG_STATE)
		fprintf(stderr, "%s( %s = %s )\n", __FUNCTION__,
			_mesa_lookup_enum_by_nr(cap),
			state ? "GL_TRUE" : "GL_FALSE");

	switch (cap) {
		/* Fast track this one...
		 */
	case GL_TEXTURE_1D:
	case GL_TEXTURE_2D:
	case GL_TEXTURE_3D:
		break;

	case GL_ALPHA_TEST:
		R300_STATECHANGE(r300, at);
		if (state) {
			r300->hw.at.cmd[R300_AT_ALPHA_TEST] |=
			    R300_ALPHA_TEST_ENABLE;
		} else {
			r300->hw.at.cmd[R300_AT_ALPHA_TEST] &=
			    ~R300_ALPHA_TEST_ENABLE;
		}
		update_early_z(ctx);
		break;

	case GL_BLEND:
	case GL_COLOR_LOGIC_OP:
		r300_set_blend_state(ctx);
		break;

	case GL_DEPTH_TEST:
		R300_STATECHANGE(r300, zs);

		if (state) {
			if (ctx->Depth.Mask)
				newval = R300_RB3D_Z_TEST_AND_WRITE;
			else
				newval = R300_RB3D_Z_TEST;
		} else
			newval = R300_RB3D_Z_DISABLED_1;

		r300->hw.zs.cmd[R300_ZS_CNTL_0] &= R300_RB3D_STENCIL_ENABLE;
		r300->hw.zs.cmd[R300_ZS_CNTL_0] |= newval;
		update_early_z(ctx);
		break;

	case GL_STENCIL_TEST:
		WARN_ONCE("TODO - double side stencil !\n");
		if (r300->state.stencil.hw_stencil) {
			R300_STATECHANGE(r300, zs);
			if (state) {
				r300->hw.zs.cmd[R300_ZS_CNTL_0] |=
				    R300_RB3D_STENCIL_ENABLE;
			} else {
				r300->hw.zs.cmd[R300_ZS_CNTL_0] &=
				    ~R300_RB3D_STENCIL_ENABLE;
			}
		} else {
#if R200_MERGED
			FALLBACK(&r300->radeon, RADEON_FALLBACK_STENCIL, state);
#endif			
		}
		break;

	case GL_CULL_FACE:
		r300UpdateCulling(ctx);
		break;

	case GL_POLYGON_OFFSET_POINT:
	case GL_POLYGON_OFFSET_LINE:
		WARN_ONCE("Don't know how to enable polygon offset point/line. Help me !\n");

		/* Something is apparently blocking these from working */
		R300_STATECHANGE(r300, unk42B4);
		if(state){
			r300->hw.unk42B4.cmd[1] |= ~(3<<0);
		} else {
			r300->hw.unk42B4.cmd[1] &= (3<<0);
		}
		break;

	case GL_POLYGON_OFFSET_FILL:
		R300_STATECHANGE(r300, unk42B4);
		if(state){
			r300->hw.unk42B4.cmd[1] |= (3<<0);
		} else {
			r300->hw.unk42B4.cmd[1] &= ~(3<<0);
		}
		break;

	case GL_VERTEX_PROGRAM_ARB:
		//TCL_FALLBACK(rmesa->glCtx, R200_TCL_FALLBACK_TCL_DISABLE, state);
	break;

	default:
		radeonEnable(ctx, cap, state);
		return;
	}
}


static void r300UpdatePolygonMode(GLcontext *ctx)
{
	r300ContextPtr r300 = R300_CONTEXT(ctx);
	uint32_t hw_mode=0;

	if (ctx->Polygon.FrontMode != GL_FILL ||
	    ctx->Polygon.BackMode != GL_FILL) {
		GLenum f, b;
		
		if (ctx->Polygon.FrontFace == GL_CCW) {
			f = ctx->Polygon.FrontMode;
			b = ctx->Polygon.BackMode;
		} else {
			f = ctx->Polygon.BackMode;
			b = ctx->Polygon.FrontMode;
		}

		hw_mode |= R300_PM_ENABLED;

		switch (f) {
		case GL_LINE:
			hw_mode |= R300_PM_FRONT_LINE;
		break;
		case GL_POINT: /* noop */
			hw_mode |= R300_PM_FRONT_POINT;
		break;
		case GL_FILL:
			hw_mode |= R300_PM_FRONT_FILL;
		break;
		}

		switch (b) {
		case GL_LINE:
			hw_mode |= R300_PM_BACK_LINE;
		break;
		case GL_POINT: /* noop */
			hw_mode |= R300_PM_BACK_POINT;
		break;
		case GL_FILL:
			hw_mode |= R300_PM_BACK_FILL;
		break;
		}
	}

	if (r300->hw.unk4288.cmd[1] != hw_mode) {
		R300_STATECHANGE(r300, unk4288);
		r300->hw.unk4288.cmd[1] = hw_mode;
	}
}

/**
 * Change the culling mode.
 *
 * \note Mesa already filters redundant calls to this function.
 */
static void r300CullFace(GLcontext* ctx, GLenum mode)
{
	(void)mode;

	r300UpdateCulling(ctx);
}


/**
 * Change the polygon orientation.
 *
 * \note Mesa already filters redundant calls to this function.
 */
static void r300FrontFace(GLcontext* ctx, GLenum mode)
{
	(void)mode;

	r300UpdateCulling(ctx);
	r300UpdatePolygonMode(ctx);
}


/**
 * Change the depth testing function.
 *
 * \note Mesa already filters redundant calls to this function.
 */
static void r300DepthFunc(GLcontext* ctx, GLenum func)
{
	r300ContextPtr r300 = R300_CONTEXT(ctx);

	R300_STATECHANGE(r300, zs);

	r300->hw.zs.cmd[R300_ZS_CNTL_1] &= ~(R300_ZS_MASK << R300_RB3D_ZS1_DEPTH_FUNC_SHIFT);

	switch(func) {
	case GL_NEVER:
		r300->hw.zs.cmd[R300_ZS_CNTL_1] |= R300_ZS_NEVER << R300_RB3D_ZS1_DEPTH_FUNC_SHIFT;
		break;
	case GL_LESS:
		r300->hw.zs.cmd[R300_ZS_CNTL_1] |= R300_ZS_LESS << R300_RB3D_ZS1_DEPTH_FUNC_SHIFT;
		break;
	case GL_EQUAL:
		r300->hw.zs.cmd[R300_ZS_CNTL_1] |= R300_ZS_EQUAL << R300_RB3D_ZS1_DEPTH_FUNC_SHIFT;
		break;
	case GL_LEQUAL:
		r300->hw.zs.cmd[R300_ZS_CNTL_1] |= R300_ZS_LEQUAL << R300_RB3D_ZS1_DEPTH_FUNC_SHIFT;
		break;
	case GL_GREATER:
		r300->hw.zs.cmd[R300_ZS_CNTL_1] |= R300_ZS_GREATER << R300_RB3D_ZS1_DEPTH_FUNC_SHIFT;
		break;
	case GL_NOTEQUAL:
		r300->hw.zs.cmd[R300_ZS_CNTL_1] |= R300_ZS_NOTEQUAL << R300_RB3D_ZS1_DEPTH_FUNC_SHIFT;
		break;
	case GL_GEQUAL:
		r300->hw.zs.cmd[R300_ZS_CNTL_1] |= R300_ZS_GEQUAL << R300_RB3D_ZS1_DEPTH_FUNC_SHIFT;
		break;
	case GL_ALWAYS:
		r300->hw.zs.cmd[R300_ZS_CNTL_1] |= R300_ZS_ALWAYS << R300_RB3D_ZS1_DEPTH_FUNC_SHIFT;
		break;
	}
}


/**
 * Enable/Disable depth writing.
 *
 * \note Mesa already filters redundant calls to this function.
 */
static void r300DepthMask(GLcontext* ctx, GLboolean mask)
{
	r300ContextPtr r300 = R300_CONTEXT(ctx);

	if (!ctx->Depth.Test)
		return;

	R300_STATECHANGE(r300, zs);
	r300->hw.zs.cmd[R300_ZS_CNTL_0] &= R300_RB3D_STENCIL_ENABLE;
	r300->hw.zs.cmd[R300_ZS_CNTL_0] |= mask 
	    ? R300_RB3D_Z_TEST_AND_WRITE : R300_RB3D_Z_TEST;
}


/**
 * Handle glColorMask()
 */
static void r300ColorMask(GLcontext* ctx,
			  GLboolean r, GLboolean g, GLboolean b, GLboolean a)
{
	r300ContextPtr r300 = R300_CONTEXT(ctx);
	int mask = (r ? R300_COLORMASK0_R : 0) |
		   (g ? R300_COLORMASK0_G : 0) |
		   (b ? R300_COLORMASK0_B : 0) |
		   (a ? R300_COLORMASK0_A : 0);

	if (mask != r300->hw.cmk.cmd[R300_CMK_COLORMASK]) {
		R300_STATECHANGE(r300, cmk);
		r300->hw.cmk.cmd[R300_CMK_COLORMASK] = mask;
	}
}

/* =============================================================
 * Point state
 */
static void r300PointSize(GLcontext * ctx, GLfloat size)
{
	r300ContextPtr r300 = R300_CONTEXT(ctx);

	size = ctx->Point._Size;

	R300_STATECHANGE(r300, ps);
	r300->hw.ps.cmd[R300_PS_POINTSIZE] = 
		((int)(size * 6) << R300_POINTSIZE_X_SHIFT) |
		((int)(size * 6) << R300_POINTSIZE_Y_SHIFT);

#if 0 /* r200 reg? */
	/* This might need fixing later */
	R300_STATECHANGE(r300, vps);
	r300->hw.vps.cmd[R300_VPS_POINTSIZE] = r300PackFloat32(1.0);
#endif
}

/* =============================================================
 * Line state
 */
static void r300LineWidth(GLcontext *ctx, GLfloat widthf)
{
	r300ContextPtr r300 = R300_CONTEXT(ctx);

	widthf = ctx->Line._Width;

	R300_STATECHANGE(r300, lcntl);
	r300->hw.lcntl.cmd[1] = (int)(widthf * 6.0);
	r300->hw.lcntl.cmd[1] |= R300_LINE_CNT_VE;
}

static void r300PolygonMode(GLcontext *ctx, GLenum face, GLenum mode)
{
	(void)face;
	(void)mode;
	
	r300UpdatePolygonMode(ctx);
}

/* =============================================================
 * Stencil
 */

 static int translate_stencil_func(int func)
 {
	switch (func) {
	case GL_NEVER:
		    return R300_ZS_NEVER;
		break;
	case GL_LESS:
		    return R300_ZS_LESS;
		break;
	case GL_EQUAL:
		    return R300_ZS_EQUAL;
		break;
	case GL_LEQUAL:
		    return R300_ZS_LEQUAL;
		break;
	case GL_GREATER:
		    return R300_ZS_GREATER;
		break;
	case GL_NOTEQUAL:
		    return R300_ZS_NOTEQUAL;
		break;
	case GL_GEQUAL:
		    return R300_ZS_GEQUAL;
		break;
	case GL_ALWAYS:
		    return R300_ZS_ALWAYS;
		break;
	}
 return 0;
 }

 static int translate_stencil_op(int op)
{
	switch (op) {
	case GL_KEEP:
		    return R300_ZS_KEEP;
	case GL_ZERO:
		    return R300_ZS_ZERO;
	case GL_REPLACE:
		    return R300_ZS_REPLACE;
	case GL_INCR:
		    return R300_ZS_INCR;
	case GL_DECR:
		    return R300_ZS_DECR;
	case GL_INCR_WRAP_EXT:
		    return R300_ZS_INCR_WRAP;
	case GL_DECR_WRAP_EXT:
		    return R300_ZS_DECR_WRAP;
	case GL_INVERT:
		    return R300_ZS_INVERT;
	default:
		WARN_ONCE("Do not know how to translate stencil op");
		return R300_ZS_KEEP;
	}
}

static void r300ShadeModel(GLcontext * ctx, GLenum mode)
{
	r300ContextPtr rmesa = R300_CONTEXT(ctx);
	
	R300_STATECHANGE(rmesa, unk4274);
	switch (mode) {
	case GL_FLAT:
		rmesa->hw.unk4274.cmd[2] = R300_RE_SHADE_MODEL_FLAT;
		break;
	case GL_SMOOTH:
		rmesa->hw.unk4274.cmd[2] = R300_RE_SHADE_MODEL_SMOOTH;
		break;
	default:
		return;
	}
}

static void r300StencilFunc(GLcontext * ctx, GLenum func,
			    GLint ref, GLuint mask)
{
	r300ContextPtr rmesa = R300_CONTEXT(ctx);
	GLuint refmask = ((ctx->Stencil.Ref[0] << R300_RB3D_ZS2_STENCIL_REF_SHIFT) |
			  (ctx->Stencil.ValueMask[0] << R300_RB3D_ZS2_STENCIL_MASK_SHIFT));
			  
	GLuint flag;

	R300_STATECHANGE(rmesa, zs);

	rmesa->hw.zs.cmd[R300_ZS_CNTL_1] &= ~(
		(R300_ZS_MASK << R300_RB3D_ZS1_FRONT_FUNC_SHIFT)
		| (R300_ZS_MASK << R300_RB3D_ZS1_BACK_FUNC_SHIFT));
	
	rmesa->hw.zs.cmd[R300_ZS_CNTL_2] &=  ~((R300_RB3D_ZS2_STENCIL_MASK << R300_RB3D_ZS2_STENCIL_REF_SHIFT) |
						(R300_RB3D_ZS2_STENCIL_MASK << R300_RB3D_ZS2_STENCIL_MASK_SHIFT));
	
	flag = translate_stencil_func(ctx->Stencil.Function[0]);

	rmesa->hw.zs.cmd[R300_ZS_CNTL_1] |= (flag << R300_RB3D_ZS1_FRONT_FUNC_SHIFT)
					  | (flag << R300_RB3D_ZS1_BACK_FUNC_SHIFT);
	rmesa->hw.zs.cmd[R300_ZS_CNTL_2] |= refmask;
}

static void r300StencilMask(GLcontext * ctx, GLuint mask)
{
	r300ContextPtr rmesa = R300_CONTEXT(ctx);

	R300_STATECHANGE(rmesa, zs);
	rmesa->hw.zs.cmd[R300_ZS_CNTL_2]  &= ~(R300_RB3D_ZS2_STENCIL_MASK << R300_RB3D_ZS2_STENCIL_WRITE_MASK_SHIFT);
	rmesa->hw.zs.cmd[R300_ZS_CNTL_2] |= ctx->Stencil.WriteMask[0] << R300_RB3D_ZS2_STENCIL_WRITE_MASK_SHIFT;
}


static void r300StencilOp(GLcontext * ctx, GLenum fail,
			  GLenum zfail, GLenum zpass)
{
	r300ContextPtr rmesa = R300_CONTEXT(ctx);

	R300_STATECHANGE(rmesa, zs);
		/* It is easier to mask what's left.. */
	rmesa->hw.zs.cmd[R300_ZS_CNTL_1] &= 
	    (R300_ZS_MASK << R300_RB3D_ZS1_DEPTH_FUNC_SHIFT) | 
	    (R300_ZS_MASK << R300_RB3D_ZS1_FRONT_FUNC_SHIFT) | 
	    (R300_ZS_MASK << R300_RB3D_ZS1_BACK_FUNC_SHIFT);

	rmesa->hw.zs.cmd[R300_ZS_CNTL_1] |=
		 (translate_stencil_op(ctx->Stencil.FailFunc[0]) << R300_RB3D_ZS1_FRONT_FAIL_OP_SHIFT)
		|(translate_stencil_op(ctx->Stencil.ZFailFunc[0]) << R300_RB3D_ZS1_FRONT_ZFAIL_OP_SHIFT)
		|(translate_stencil_op(ctx->Stencil.ZPassFunc[0]) << R300_RB3D_ZS1_FRONT_ZPASS_OP_SHIFT)
		|(translate_stencil_op(ctx->Stencil.FailFunc[0]) << R300_RB3D_ZS1_BACK_FAIL_OP_SHIFT)
		|(translate_stencil_op(ctx->Stencil.ZFailFunc[0]) << R300_RB3D_ZS1_BACK_ZFAIL_OP_SHIFT)
		|(translate_stencil_op(ctx->Stencil.ZPassFunc[0]) << R300_RB3D_ZS1_BACK_ZPASS_OP_SHIFT);
}

static void r300ClearStencil(GLcontext * ctx, GLint s)
{
	r300ContextPtr rmesa = R300_CONTEXT(ctx);

	rmesa->state.stencil.clear =
	    ((GLuint) ctx->Stencil.Clear |
	     (R300_RB3D_ZS2_STENCIL_MASK << R300_RB3D_ZS2_STENCIL_MASK_SHIFT) |
	     (ctx->Stencil.WriteMask[0] << R300_RB3D_ZS2_STENCIL_WRITE_MASK_SHIFT));
}

/* =============================================================
 * Window position and viewport transformation
 */

/*
 * To correctly position primitives:
 */
#define SUBPIXEL_X 0.125
#define SUBPIXEL_Y 0.125

void r300UpdateWindow(GLcontext * ctx)
{
	r300ContextPtr rmesa = R300_CONTEXT(ctx);
	__DRIdrawablePrivate *dPriv = rmesa->radeon.dri.drawable;
	GLfloat xoffset = dPriv ? (GLfloat) dPriv->x : 0;
	GLfloat yoffset = dPriv ? (GLfloat) dPriv->y + dPriv->h : 0;
	const GLfloat *v = ctx->Viewport._WindowMap.m;

	GLfloat sx = v[MAT_SX];
	GLfloat tx = v[MAT_TX] + xoffset + SUBPIXEL_X;
	GLfloat sy = -v[MAT_SY];
	GLfloat ty = (-v[MAT_TY]) + yoffset + SUBPIXEL_Y;
	GLfloat sz = v[MAT_SZ] * rmesa->state.depth.scale;
	GLfloat tz = v[MAT_TZ] * rmesa->state.depth.scale;

	R300_FIREVERTICES(rmesa);
	R300_STATECHANGE(rmesa, vpt);

	rmesa->hw.vpt.cmd[R300_VPT_XSCALE]  = r300PackFloat32(sx);
	rmesa->hw.vpt.cmd[R300_VPT_XOFFSET] = r300PackFloat32(tx);
	rmesa->hw.vpt.cmd[R300_VPT_YSCALE]  = r300PackFloat32(sy);
	rmesa->hw.vpt.cmd[R300_VPT_YOFFSET] = r300PackFloat32(ty);
	rmesa->hw.vpt.cmd[R300_VPT_ZSCALE]  = r300PackFloat32(sz);
	rmesa->hw.vpt.cmd[R300_VPT_ZOFFSET] = r300PackFloat32(tz);
}

static void r300Viewport(GLcontext * ctx, GLint x, GLint y,
			 GLsizei width, GLsizei height)
{
	/* Don't pipeline viewport changes, conflict with window offset
	 * setting below.  Could apply deltas to rescue pipelined viewport
	 * values, or keep the originals hanging around.
	 */
	R300_FIREVERTICES(R300_CONTEXT(ctx));
	r300UpdateWindow(ctx);
}

static void r300DepthRange(GLcontext * ctx, GLclampd nearval, GLclampd farval)
{
	r300UpdateWindow(ctx);
}

/* =============================================================
 * Polygon state
 */
static void r300PolygonOffset(GLcontext * ctx, GLfloat factor, GLfloat units)
{
	r300ContextPtr rmesa = R300_CONTEXT(ctx);
	GLfloat constant = units;
	
	switch (ctx->Visual.depthBits) {
	case 16:
		constant *= 4.0;
	break;
	case 24:
		constant *= 2.0;
	break;
	}

	factor *= 12.0;

/*    fprintf(stderr, "%s f:%f u:%f\n", __FUNCTION__, factor, constant); */

	R300_STATECHANGE(rmesa, zbs);
	rmesa->hw.zbs.cmd[R300_ZBS_T_FACTOR] = r300PackFloat32(factor);
	rmesa->hw.zbs.cmd[R300_ZBS_T_CONSTANT] = r300PackFloat32(constant);
	rmesa->hw.zbs.cmd[R300_ZBS_W_FACTOR] = r300PackFloat32(factor);
	rmesa->hw.zbs.cmd[R300_ZBS_W_CONSTANT] = r300PackFloat32(constant);
}

/* Routing and texture-related */
#if 0
void r300_setup_routing(GLcontext *ctx, GLboolean immediate)
{
	int i, count=0,reg=0;
	GLuint dw, mask;
	TNLcontext *tnl = TNL_CONTEXT(ctx);
	struct vertex_buffer *VB = &tnl->vb;
	r300ContextPtr r300 = R300_CONTEXT(ctx);


	/* Stage 1 - input to VAP */

	/* Assign register number automatically, retaining it in rmesa->state.reg */

	/* Note: immediate vertex data includes all coordinates.
	To save bandwidth use either VBUF or state-based vertex generation */

#define CONFIGURE_AOS(v, o, r, f) \
		{\
		if (RADEON_DEBUG & DEBUG_STATE)fprintf(stderr, "Enabling "#r "\n"); \
		r300->state.aos[count].aos_size=4; \
		r300->state.aos[count].aos_stride=4; \
		r300->state.aos[count].aos_offset=o; \
		r300->state.aos[count].aos_reg=reg; \
		r300->state.aos[count].aos_format=(f); \
		r300->state.vap_reg.r=reg; \
		count++; \
		reg++; \
		}

		/* All offsets are 0 - for use by immediate mode.
		Should change later to handle vertex buffers */
	if(r300->current_vp!=NULL){

	/* VERT_ATTRIB_WEIGHT, VERT_ATTRIB_SIX, VERT_ATTRIB_SEVEN, VERT_ATTRIB_GENERIC0,
	   VERT_ATTRIB_GENERIC1, VERT_ATTRIB_GENERIC2, VERT_ATTRIB_GENERIC3 */
	r300->state.render_inputs = 0;

	if(r300->current_vp->inputs[VERT_ATTRIB_POS] != -1){
		reg=r300->current_vp->inputs[VERT_ATTRIB_POS];
		CONFIGURE_AOS(VB->ObjPtr, 0, i_coords, AOS_FORMAT_FLOAT);
		r300->state.render_inputs |= _TNL_BIT_POS;
	}
	if(r300->current_vp->inputs[VERT_ATTRIB_NORMAL] != -1){
		reg=r300->current_vp->inputs[VERT_ATTRIB_NORMAL];
		CONFIGURE_AOS(VB->NormalPtr, 0, i_normal, AOS_FORMAT_FLOAT);
		r300->state.render_inputs |= _TNL_BIT_NORMAL;
	}
	if(r300->current_vp->inputs[VERT_ATTRIB_COLOR0] != -1){
		reg=r300->current_vp->inputs[VERT_ATTRIB_COLOR0];
		CONFIGURE_AOS(VB->ColorPtr[0], 0, i_color[0], AOS_FORMAT_FLOAT_COLOR);
		r300->state.render_inputs |= _TNL_BIT_COLOR0;
	}
	if(r300->current_vp->inputs[VERT_ATTRIB_COLOR1] != -1){
		reg=r300->current_vp->inputs[VERT_ATTRIB_COLOR1];
		CONFIGURE_AOS(VB->SecondaryColorPtr[0], 0, i_color[1], AOS_FORMAT_FLOAT_COLOR);
		r300->state.render_inputs |= _TNL_BIT_COLOR1;
	}
	if(r300->current_vp->inputs[VERT_ATTRIB_FOG] != -1){
		reg=r300->current_vp->inputs[VERT_ATTRIB_FOG];
		CONFIGURE_AOS(VB->FogCoordPtr, 0, i_fog, AOS_FORMAT_FLOAT);
		r300->state.render_inputs |= _TNL_BIT_FOG;
	}
	for(i=0;i < ctx->Const.MaxTextureUnits;i++) // tex 7 is last
		if(r300->current_vp->inputs[VERT_ATTRIB_TEX0+i] != -1){
			reg=r300->current_vp->inputs[VERT_ATTRIB_TEX0+i];
			CONFIGURE_AOS(VB->TexCoordPtr[i], 0, i_tex[i], AOS_FORMAT_FLOAT);
			r300->state.render_inputs |= _TNL_BIT_TEX0<<i;
		}
#if 0
	if((tnl->render_inputs & _TNL_BIT_INDEX))
		CONFIGURE_AOS(VB->IndexPtr[0], 0, i_index, AOS_FORMAT_FLOAT);

	if((tnl->render_inputs & _TNL_BIT_POINTSIZE))
		CONFIGURE_AOS(VB->PointSizePtr, 0, i_pointsize, AOS_FORMAT_FLOAT);
#endif
	}else{

	r300->state.render_inputs = tnl->render_inputs;

	if(tnl->render_inputs & _TNL_BIT_POS)
		CONFIGURE_AOS(VB->ObjPtr, 0, i_coords, AOS_FORMAT_FLOAT);
	if(tnl->render_inputs & _TNL_BIT_NORMAL)
		CONFIGURE_AOS(VB->NormalPtr, 0, i_normal, AOS_FORMAT_FLOAT);

	if(tnl->render_inputs & _TNL_BIT_COLOR0)
		CONFIGURE_AOS(VB->ColorPtr[0], 0, i_color[0], AOS_FORMAT_FLOAT_COLOR);
	if(tnl->render_inputs & _TNL_BIT_COLOR1)
		CONFIGURE_AOS(VB->SecondaryColorPtr[0], 0, i_color[1], AOS_FORMAT_FLOAT_COLOR);

	/*if(tnl->render_inputs & _TNL_BIT_FOG) // Causes lock ups when immediate mode is on
		CONFIGURE_AOS(VB->FogCoordPtr, 0, i_fog, AOS_FORMAT_FLOAT);*/

	for(i=0;i < ctx->Const.MaxTextureUnits;i++)
		if(tnl->render_inputs & (_TNL_BIT_TEX0<<i))
			CONFIGURE_AOS(VB->TexCoordPtr[i], 0, i_tex[i], AOS_FORMAT_FLOAT);

	if(tnl->render_inputs & _TNL_BIT_INDEX)
		CONFIGURE_AOS(VB->IndexPtr[0], 0, i_index, AOS_FORMAT_FLOAT);
	if(tnl->render_inputs & _TNL_BIT_POINTSIZE)
		CONFIGURE_AOS(VB->PointSizePtr, 0, i_pointsize, AOS_FORMAT_FLOAT);
	}

	r300->state.aos_count=count;

	if (RADEON_DEBUG & DEBUG_STATE)
		fprintf(stderr, "aos_count=%d render_inputs=%08x\n", count, r300->state.render_inputs);


	if(count>R300_MAX_AOS_ARRAYS){
		fprintf(stderr, "Aieee ! AOS array count exceeded !\n");
		exit(-1);
		}

	/* Implement AOS */

	/* setup INPUT_ROUTE */
	R300_STATECHANGE(r300, vir[0]);
	for(i=0;i+1<count;i+=2){
		dw=(r300->state.aos[i].aos_size-1)
		| ((r300->state.aos[i].aos_reg)<<8)
		| (r300->state.aos[i].aos_format<<14)
		| (((r300->state.aos[i+1].aos_size-1)
		| ((r300->state.aos[i+1].aos_reg)<<8)
		| (r300->state.aos[i+1].aos_format<<14))<<16);

		if(i+2==count){
			dw|=(1<<(13+16));
			}
		r300->hw.vir[0].cmd[R300_VIR_CNTL_0+(i>>1)]=dw;
		}
	if(count & 1){
		dw=(r300->state.aos[count-1].aos_size-1)
		| (r300->state.aos[count-1].aos_format<<14)
		| ((r300->state.aos[count-1].aos_reg)<<8)
		| (1<<13);
		r300->hw.vir[0].cmd[R300_VIR_CNTL_0+(count>>1)]=dw;
		//fprintf(stderr, "vir0 dw=%08x\n", dw);
		}
	/* Set the rest of INPUT_ROUTE_0 to 0 */
	//for(i=((count+1)>>1); i<8; i++)r300->hw.vir[0].cmd[R300_VIR_CNTL_0+i]=(0x0);
	((drm_r300_cmd_header_t*)r300->hw.vir[0].cmd)->unchecked_state.count = (count+1)>>1;


	/* Mesa assumes that all missing components are from (0, 0, 0, 1) */
#define ALL_COMPONENTS ((R300_INPUT_ROUTE_SELECT_X<<R300_INPUT_ROUTE_X_SHIFT) \
		| (R300_INPUT_ROUTE_SELECT_Y<<R300_INPUT_ROUTE_Y_SHIFT) \
		| (R300_INPUT_ROUTE_SELECT_Z<<R300_INPUT_ROUTE_Z_SHIFT) \
		| (R300_INPUT_ROUTE_SELECT_W<<R300_INPUT_ROUTE_W_SHIFT))

#define ALL_DEFAULT ((R300_INPUT_ROUTE_SELECT_ZERO<<R300_INPUT_ROUTE_X_SHIFT) \
		| (R300_INPUT_ROUTE_SELECT_ZERO<<R300_INPUT_ROUTE_Y_SHIFT) \
		| (R300_INPUT_ROUTE_SELECT_ZERO<<R300_INPUT_ROUTE_Z_SHIFT) \
		| (R300_INPUT_ROUTE_SELECT_ONE<<R300_INPUT_ROUTE_W_SHIFT))

	R300_STATECHANGE(r300, vir[1]);

	for(i=0;i+1<count;i+=2){
		/* do i first.. */
		mask=(1<<(r300->state.aos[i].aos_size*3))-1;
		dw=(ALL_COMPONENTS & mask)
		| (ALL_DEFAULT & ~mask)
		| R300_INPUT_ROUTE_ENABLE;

		/* i+1 */
		mask=(1<<(r300->state.aos[i+1].aos_size*3))-1;
		dw|=(
		(ALL_COMPONENTS & mask)
		| (ALL_DEFAULT & ~mask)
		| R300_INPUT_ROUTE_ENABLE
		)<<16;

		r300->hw.vir[1].cmd[R300_VIR_CNTL_0+(i>>1)]=dw;
		}
	if(count & 1){
		mask=(1<<(r300->state.aos[count-1].aos_size*3))-1;
		dw=(ALL_COMPONENTS & mask)
		| (ALL_DEFAULT & ~mask)
		| R300_INPUT_ROUTE_ENABLE;
		r300->hw.vir[1].cmd[R300_VIR_CNTL_0+(count>>1)]=dw;
		//fprintf(stderr, "vir1 dw=%08x\n", dw);
		}
	/* Set the rest of INPUT_ROUTE_1 to 0 */
	//for(i=((count+1)>>1); i<8; i++)r300->hw.vir[1].cmd[R300_VIR_CNTL_0+i]=0x0;
	((drm_r300_cmd_header_t*)r300->hw.vir[1].cmd)->unchecked_state.count = (count+1)>>1;

	/* Set up input_cntl */

	R300_STATECHANGE(r300, vic);
	r300->hw.vic.cmd[R300_VIC_CNTL_0]=0x5555;  /* Hard coded value, no idea what it means */

	r300->hw.vic.cmd[R300_VIC_CNTL_1]=0;

	if(r300->state.render_inputs & _TNL_BIT_POS)
		r300->hw.vic.cmd[R300_VIC_CNTL_1]|=R300_INPUT_CNTL_POS;

	if(r300->state.render_inputs & _TNL_BIT_NORMAL)
		r300->hw.vic.cmd[R300_VIC_CNTL_1]|=R300_INPUT_CNTL_NORMAL;

	if(r300->state.render_inputs & _TNL_BIT_COLOR0)
		r300->hw.vic.cmd[R300_VIC_CNTL_1]|=R300_INPUT_CNTL_COLOR;

	for(i=0;i < ctx->Const.MaxTextureUnits;i++)
		if(r300->state.render_inputs & (_TNL_BIT_TEX0<<i))
			r300->hw.vic.cmd[R300_VIC_CNTL_1]|=(R300_INPUT_CNTL_TC0<<i);

	/* Stage 3: VAP output */
	R300_STATECHANGE(r300, vof);
	r300->hw.vof.cmd[R300_VOF_CNTL_0]=R300_VAP_OUTPUT_VTX_FMT_0__POS_PRESENT
					| R300_VAP_OUTPUT_VTX_FMT_0__COLOR_PRESENT;

	r300->hw.vof.cmd[R300_VOF_CNTL_1]=0;
	for(i=0;i < ctx->Const.MaxTextureUnits;i++)
		if(r300->state.render_inputs & (_TNL_BIT_TEX0<<i))
			r300->hw.vof.cmd[R300_VOF_CNTL_1]|=(4<<(3*i));

}
#endif
static r300TexObj default_tex_obj={
	filter:R300_TX_MAG_FILTER_LINEAR | R300_TX_MIN_FILTER_LINEAR,
	pitch: 0x8000,
	size: (0xff << R300_TX_WIDTHMASK_SHIFT)
	      | (0xff << R300_TX_HEIGHTMASK_SHIFT)
	      | (0x8 << R300_TX_SIZE_SHIFT),
	format: 0x88a0c,
	offset: 0x0,
	unknown4: 0x0,
	unknown5: 0x0
	};

	/* there is probably a system to these value, but, for now,
	   we just try by hand */

static int inline translate_src(int src)
{
	switch (src) {
	case GL_TEXTURE:
		return 1;
		break;
	case GL_CONSTANT:
		return 2;
		break;
	case GL_PRIMARY_COLOR:
		return 3;
		break;
	case GL_PREVIOUS:
		return 4;
		break;
	case GL_ZERO:
		return 5;
		break;
	case GL_ONE:
		return 6;
		break;
	default:
		return 0;
	}
}

/* r300 doesnt handle GL_CLAMP and GL_MIRROR_CLAMP_EXT correctly when filter is NEAREST.
 * Since texwrap produces same results for GL_CLAMP and GL_CLAMP_TO_EDGE we use them instead.
 * We need to recalculate wrap modes whenever filter mode is changed because someone might do:
 * glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 * glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
 * glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 * Since r300 completely ignores R300_TX_CLAMP when either min or mag is nearest it cant handle
 * combinations where only one of them is nearest.
 */
static unsigned long gen_fixed_filter(unsigned long f)
{
	unsigned long mag, min, needs_fixing=0;
	//return f;

	/* We ignore MIRROR bit so we dont have to do everything twice */
	if((f & ((7-1) << R300_TX_WRAP_S_SHIFT)) == (R300_TX_CLAMP << R300_TX_WRAP_S_SHIFT)){
		needs_fixing |= 1;
	}
	if((f & ((7-1) << R300_TX_WRAP_T_SHIFT)) == (R300_TX_CLAMP << R300_TX_WRAP_T_SHIFT)){
		needs_fixing |= 2;
	}
	if((f & ((7-1) << R300_TX_WRAP_Q_SHIFT)) == (R300_TX_CLAMP << R300_TX_WRAP_Q_SHIFT)){
		needs_fixing |= 4;
	}

	if(!needs_fixing)
		return f;

	mag=f & R300_TX_MAG_FILTER_MASK;
	min=f & R300_TX_MIN_FILTER_MASK;

	/* TODO: Check for anisto filters too */
	if((mag != R300_TX_MAG_FILTER_NEAREST) && (min != R300_TX_MIN_FILTER_NEAREST))
		return f;

	/* r300 cant handle these modes hence we force nearest to linear */
	if((mag == R300_TX_MAG_FILTER_NEAREST) && (min != R300_TX_MIN_FILTER_NEAREST)){
		f &= ~R300_TX_MAG_FILTER_NEAREST;
		f |= R300_TX_MAG_FILTER_LINEAR;
		return f;
	}

	if((min == R300_TX_MIN_FILTER_NEAREST) && (mag != R300_TX_MAG_FILTER_NEAREST)){
		f &= ~R300_TX_MIN_FILTER_NEAREST;
		f |= R300_TX_MIN_FILTER_LINEAR;
		return f;
	}

	/* Both are nearest */
	if(needs_fixing & 1){
		f &= ~((7-1) << R300_TX_WRAP_S_SHIFT);
		f |= R300_TX_CLAMP_TO_EDGE << R300_TX_WRAP_S_SHIFT;
	}
	if(needs_fixing & 2){
		f &= ~((7-1) << R300_TX_WRAP_T_SHIFT);
		f |= R300_TX_CLAMP_TO_EDGE << R300_TX_WRAP_T_SHIFT;
	}
	if(needs_fixing & 4){
		f &= ~((7-1) << R300_TX_WRAP_Q_SHIFT);
		f |= R300_TX_CLAMP_TO_EDGE << R300_TX_WRAP_Q_SHIFT;
	}
	return f;
}

void r300_setup_textures(GLcontext *ctx)
{
	int i, mtu;
	struct r300_tex_obj *t;
	r300ContextPtr r300 = R300_CONTEXT(ctx);
	int max_texture_unit=-1; /* -1 translates into no setup costs for fields */
	struct gl_texture_unit *texUnit;
	GLuint OutputsWritten;
	
	if(hw_tcl_on)
		OutputsWritten = CURRENT_VERTEX_SHADER(ctx)->OutputsWritten;

	R300_STATECHANGE(r300, txe);
	R300_STATECHANGE(r300, tex.filter);
	R300_STATECHANGE(r300, tex.unknown1);
	R300_STATECHANGE(r300, tex.size);
	R300_STATECHANGE(r300, tex.format);
	R300_STATECHANGE(r300, tex.offset);
	R300_STATECHANGE(r300, tex.unknown4);
	R300_STATECHANGE(r300, tex.border_color);
	
	r300->state.texture.tc_count=0;

	r300->hw.txe.cmd[R300_TXE_ENABLE]=0x0;

	mtu = r300->radeon.glCtx->Const.MaxTextureUnits;
	if (RADEON_DEBUG & DEBUG_STATE)
		fprintf(stderr, "mtu=%d\n", mtu);

	if(mtu > R300_MAX_TEXTURE_UNITS) {
		fprintf(stderr, "Aiiee ! mtu=%d is greater than R300_MAX_TEXTURE_UNITS=%d\n",
			mtu, R300_MAX_TEXTURE_UNITS);
		exit(-1);
	}
	
	for(i=0; i < mtu; i++) {
		/*if( ((r300->state.render_inputs & (_TNL_BIT_TEX0<<i))!=0) != ((ctx->Texture.Unit[i].Enabled)!=0) ) {
			WARN_ONCE("Mismatch between render_inputs and ctx->Texture.Unit[i].Enabled value(%d vs %d).\n",
					((r300->state.render_inputs & (_TNL_BIT_TEX0<<i))!=0), ((ctx->Texture.Unit[i].Enabled)!=0));
		}*/
		
		if(TMU_ENABLED(ctx, i)) {
			t=r300->state.texture.unit[i].texobj;
			//fprintf(stderr, "format=%08x\n", r300->state.texture.unit[i].format);
			r300->state.texture.tc_count++;
			
			if(t == NULL){
				fprintf(stderr, "Texture unit %d enabled, but corresponding texobj is NULL, using default object.\n", i);
				//exit(-1);
				t=&default_tex_obj;
			}
			
			//fprintf(stderr, "t->format=%08x\n", t->format);
			if((t->format & 0xffffff00)==0xffffff00) {
				WARN_ONCE("unknown texture format (entry %x) encountered. Help me !\n", t->format & 0xff);
				//fprintf(stderr, "t->format=%08x\n", t->format);
			}
			
			if (RADEON_DEBUG & DEBUG_STATE)
				fprintf(stderr, "Activating texture unit %d\n", i);
			max_texture_unit=i;
			r300->hw.txe.cmd[R300_TXE_ENABLE]|=(1<<i);
			
			r300->hw.tex.filter.cmd[R300_TEX_VALUE_0+i]=gen_fixed_filter(t->filter) | (i << 28); 
			//r300->hw.tex.unknown1.cmd[R300_TEX_VALUE_0+i]=0x0; /* move lod bias here? */
			
			/* No idea why linear filtered textures shake when puting random data */
			/*r300->hw.tex.unknown1.cmd[R300_TEX_VALUE_0+i]=(rand()%0xffffffff) & (~0x1fff);*/
			r300->hw.tex.size.cmd[R300_TEX_VALUE_0+i]=t->size;
			r300->hw.tex.format.cmd[R300_TEX_VALUE_0+i]=t->format;
			//fprintf(stderr, "t->format=%08x\n", t->format);
			r300->hw.tex.offset.cmd[R300_TEX_VALUE_0+i]=r300->radeon.radeonScreen->fbLocation+t->offset;
			r300->hw.tex.unknown4.cmd[R300_TEX_VALUE_0+i]=0x0;
			r300->hw.tex.border_color.cmd[R300_TEX_VALUE_0+i]=t->pp_border_color;
		}
	}
	
	((drm_r300_cmd_header_t*)r300->hw.tex.filter.cmd)->unchecked_state.count = max_texture_unit+1;
	((drm_r300_cmd_header_t*)r300->hw.tex.unknown1.cmd)->unchecked_state.count = max_texture_unit+1;
	((drm_r300_cmd_header_t*)r300->hw.tex.size.cmd)->unchecked_state.count = max_texture_unit+1;
	((drm_r300_cmd_header_t*)r300->hw.tex.format.cmd)->unchecked_state.count = max_texture_unit+1;
	((drm_r300_cmd_header_t*)r300->hw.tex.offset.cmd)->unchecked_state.count = max_texture_unit+1;
	((drm_r300_cmd_header_t*)r300->hw.tex.unknown4.cmd)->unchecked_state.count = max_texture_unit+1;
	((drm_r300_cmd_header_t*)r300->hw.tex.border_color.cmd)->unchecked_state.count = max_texture_unit+1;

	if (RADEON_DEBUG & DEBUG_STATE)
		fprintf(stderr, "TX_ENABLE: %08x  max_texture_unit=%d\n", r300->hw.txe.cmd[R300_TXE_ENABLE], max_texture_unit);
}

void r300_setup_rs_unit(GLcontext *ctx)
{
	r300ContextPtr r300 = R300_CONTEXT(ctx);
	int i, cur_reg;
	/* I'm still unsure if these are needed */
	GLuint interp_magic[8] = {
		0x00,
		0x40,
		0x80,
		0xC0,
		0x00,
		0x00,
		0x00,
		0x00
	};
	GLuint OutputsWritten;
	
	if(hw_tcl_on)
		OutputsWritten = CURRENT_VERTEX_SHADER(ctx)->OutputsWritten;
	
	/* This needs to be rewritten - it is a hack at best */

	R300_STATECHANGE(r300, ri);
	R300_STATECHANGE(r300, rc);
	R300_STATECHANGE(r300, rr);
	
#if 1
	cur_reg = 0;
	r300->hw.rr.cmd[R300_RR_ROUTE_0] = 0;

	for (i=0;i<ctx->Const.MaxTextureUnits;i++) {
		r300->hw.ri.cmd[R300_RI_INTERP_0+i] = 0
				| R300_RS_INTERP_USED
				| (cur_reg << R300_RS_INTERP_SRC_SHIFT)
				| interp_magic[i];
//		fprintf(stderr, "RS_INTERP[%d] = 0x%x\n", i, r300->hw.ri.cmd[R300_RI_INTERP_0+i]);

		if (TMU_ENABLED(ctx, i)) {
			assert(r300->state.texture.tc_count != 0);
			r300->hw.rr.cmd[R300_RR_ROUTE_0 + cur_reg] = 0
					| R300_RS_ROUTE_ENABLE
					| i /* source INTERP */
					| (cur_reg << R300_RS_ROUTE_DEST_SHIFT);
//			fprintf(stderr, "RS_ROUTE[%d] = 0x%x\n", cur_reg, r300->hw.rr.cmd[R300_RR_ROUTE_0 + cur_reg]);
			cur_reg++;
		} 
	}
	if (hw_tcl_on ? OutputsWritten & (1<<VERT_RESULT_COL0) : r300->state.render_inputs & _TNL_BIT_COLOR0)
		r300->hw.rr.cmd[R300_RR_ROUTE_0] |= 0
				| R300_RS_ROUTE_0_COLOR
				| (cur_reg << R300_RS_ROUTE_0_COLOR_DEST_SHIFT);

//	fprintf(stderr, "ADJ_RR0 = 0x%x\n", r300->hw.rr.cmd[R300_RR_ROUTE_0]);

	r300->hw.rc.cmd[1] = 0
			| (cur_reg /* count */ << R300_RS_CNTL_TC_CNT_SHIFT)
			| R300_RS_CNTL_0_UNKNOWN_7
			| R300_RS_CNTL_0_UNKNOWN_18;

	if (r300->state.texture.tc_count > 0) {
			r300->hw.rr.cmd[R300_RR_CMD_0] = cmducs(R300_RS_ROUTE_0, cur_reg);
			r300->hw.rc.cmd[2] = 0xC0 | (cur_reg-1); /* index of highest */
	} else {
			r300->hw.rr.cmd[R300_RR_CMD_0] = cmducs(R300_RS_ROUTE_0, 1);
			r300->hw.rc.cmd[2] = 0x0;
	}


//	fprintf(stderr, "rendering with %d texture co-ordinate sets\n", cur_reg);
	
#else
	for(i = 1; i <= 8; ++i)
		r300->hw.ri.cmd[i] = 0x00d10000;
	r300->hw.ri.cmd[R300_RI_INTERP_1] |= R300_RS_INTERP_1_UNKNOWN;
	r300->hw.ri.cmd[R300_RI_INTERP_2] |= R300_RS_INTERP_2_UNKNOWN;
	r300->hw.ri.cmd[R300_RI_INTERP_3] |= R300_RS_INTERP_3_UNKNOWN;

#if 1
	for(i = 2; i <= 8; ++i)
		r300->hw.ri.cmd[i] |= 4;
#endif

	for(i = 1; i <= 8; ++i)
		r300->hw.rr.cmd[i] = 0;
	/* textures enabled ? */
	if(r300->state.texture.tc_count>0){

		/* This code only really works with one set of texture coordinates */

		/* The second constant is needed to get glxgears display anything .. */
		r300->hw.rc.cmd[1] = R300_RS_CNTL_0_UNKNOWN_7
				| R300_RS_CNTL_0_UNKNOWN_18
				| (r300->state.texture.tc_count<<R300_RS_CNTL_TC_CNT_SHIFT);
		r300->hw.rc.cmd[2] = 0xc0;


		((drm_r300_cmd_header_t*)r300->hw.rr.cmd)->unchecked_state.count = 1;
		r300->hw.rr.cmd[R300_RR_ROUTE_0] = 0x24008;

		} else {

		/* The second constant is needed to get glxgears display anything .. */
		r300->hw.rc.cmd[1] = R300_RS_CNTL_0_UNKNOWN_7 | R300_RS_CNTL_0_UNKNOWN_18;
		r300->hw.rc.cmd[2] = 0;

		((drm_r300_cmd_header_t*)r300->hw.rr.cmd)->unchecked_state.count = 1;
		r300->hw.rr.cmd[R300_RR_ROUTE_0] = 0x4000;

		}
#endif
}

#define vpucount(ptr) (((drm_r300_cmd_header_t*)(ptr))->vpu.count)

#define bump_vpu_count(ptr, new_count)   do{\
	drm_r300_cmd_header_t* _p=((drm_r300_cmd_header_t*)(ptr));\
	int _nc=(new_count)/4; \
	if(_nc>_p->vpu.count)_p->vpu.count=_nc;\
	}while(0)

void static inline setup_vertex_shader_fragment(r300ContextPtr r300, int dest, struct r300_vertex_shader_fragment *vsf)
{
	int i;

	if(vsf->length==0)return;

	if(vsf->length & 0x3){
		fprintf(stderr,"VERTEX_SHADER_FRAGMENT must have length divisible by 4\n");
		exit(-1);
		}

	switch((dest>>8) & 0xf){
	case 0:
		R300_STATECHANGE(r300, vpi);
		for(i=0;i<vsf->length;i++)
			r300->hw.vpi.cmd[R300_VPI_INSTR_0+i+4*(dest & 0xff)]=(vsf->body.d[i]);
		bump_vpu_count(r300->hw.vpi.cmd, vsf->length+4*(dest & 0xff));
		break;

	case 2:
		R300_STATECHANGE(r300, vpp);
		for(i=0;i<vsf->length;i++)
			r300->hw.vpp.cmd[R300_VPP_PARAM_0+i+4*(dest & 0xff)]=(vsf->body.d[i]);
		bump_vpu_count(r300->hw.vpp.cmd, vsf->length+4*(dest & 0xff));
		break;
	case 4:
		R300_STATECHANGE(r300, vps);
		for(i=0;i<vsf->length;i++)
			r300->hw.vps.cmd[1+i+4*(dest & 0xff)]=(vsf->body.d[i]);
		bump_vpu_count(r300->hw.vps.cmd, vsf->length+4*(dest & 0xff));
		break;
	default:
		fprintf(stderr, "%s:%s don't know how to handle dest %04x\n", __FILE__, __FUNCTION__, dest);
		exit(-1);
	}
}

void r300SetupVertexProgram(r300ContextPtr rmesa);

/* just a skeleton for now.. */

/* Generate a vertex shader that simply transforms vertex and texture coordinates,
   while leaving colors intact. Nothing fancy (like lights) 
   
   If implementing lights make a copy first, so it is easy to switch between the two versions */
void r300GenerateSimpleVertexShader(r300ContextPtr r300)
{
	int i;
	GLuint o_reg = 0;

	/* Allocate parameters */
	r300->state.vap_param.transform_offset=0x0;  /* transform matrix */
	r300->state.vertex_shader.param_offset=0x0;
	r300->state.vertex_shader.param_count=0x4;  /* 4 vector values - 4x4 matrix */
	
	r300->state.vertex_shader.program_start=0x0;
	r300->state.vertex_shader.unknown_ptr1=0x4; /* magic value ? */
	r300->state.vertex_shader.program_end=0x0;
	
	r300->state.vertex_shader.unknown_ptr2=0x0; /* magic value */
	r300->state.vertex_shader.unknown_ptr3=0x4; /* magic value */
	
	/* Initialize matrix and vector parameters.. these should really be restructured */
	/* TODO: fix vertex_shader structure */
	r300->state.vertex_shader.matrix[0].length=16;
	r300->state.vertex_shader.matrix[1].length=0;
	r300->state.vertex_shader.matrix[2].length=0;
	r300->state.vertex_shader.vector[0].length=0;
	r300->state.vertex_shader.vector[1].length=0;
	r300->state.vertex_shader.unknown1.length=0;
	r300->state.vertex_shader.unknown2.length=0;
	
#define WRITE_OP(oper,source1,source2,source3)	{\
	r300->state.vertex_shader.program.body.i[r300->state.vertex_shader.program_end].op=(oper); \
	r300->state.vertex_shader.program.body.i[r300->state.vertex_shader.program_end].src1=(source1); \
	r300->state.vertex_shader.program.body.i[r300->state.vertex_shader.program_end].src2=(source2); \
	r300->state.vertex_shader.program.body.i[r300->state.vertex_shader.program_end].src3=(source3); \
	r300->state.vertex_shader.program_end++; \
	}

	/* Multiply vertex coordinates with transform matrix */
			
	WRITE_OP(
		EASY_VSF_OP(MUL, 0, ALL, TMP),
		VSF_PARAM(3),
		VSF_ATTR_W(0),
		EASY_VSF_SOURCE(0, W, W, W, W, NONE, NONE)
		)
	
	WRITE_OP(
		EASY_VSF_OP(MUL, 1, ALL, RESULT),
		VSF_REG(1),
		VSF_ATTR_UNITY(1),
		VSF_UNITY(1)
		)
	
	WRITE_OP(
		EASY_VSF_OP(MAD, 0, ALL, TMP),
		VSF_PARAM(2),
		VSF_ATTR_Z(0),
		VSF_TMP(0)
		)
	
	WRITE_OP(
		EASY_VSF_OP(MAD, 0, ALL, TMP),
		VSF_PARAM(1),
		VSF_ATTR_Y(0),
		VSF_TMP(0)
		)
	
	WRITE_OP(
		EASY_VSF_OP(MAD, 0, ALL, RESULT),
		VSF_PARAM(0),
		VSF_ATTR_X(0),
		VSF_TMP(0)
		)
	o_reg += 2;

	/* Pass through texture coordinates, if any */
	for(i=0;i < r300->radeon.glCtx->Const.MaxTextureUnits;i++)
		if(r300->state.render_inputs & (_TNL_BIT_TEX0<<i)){
			// fprintf(stderr, "i_tex[%d]=%d\n", i, r300->state.vap_reg.i_tex[i]);
			WRITE_OP(
				EASY_VSF_OP(MUL, o_reg++ /* 2+i */, ALL, RESULT),
				VSF_REG(r300->state.vap_reg.i_tex[i]),
				VSF_ATTR_UNITY(r300->state.vap_reg.i_tex[i]),
				VSF_UNITY(r300->state.vap_reg.i_tex[i])
				)
			}
	
	r300->state.vertex_shader.program_end--; /* r300 wants program length to be one more - no idea why */
	r300->state.vertex_shader.program.length=(r300->state.vertex_shader.program_end+1)*4;
	
	r300->state.vertex_shader.unknown_ptr1=r300->state.vertex_shader.program_end; /* magic value ? */
	r300->state.vertex_shader.unknown_ptr2=r300->state.vertex_shader.program_end; /* magic value ? */
	r300->state.vertex_shader.unknown_ptr3=r300->state.vertex_shader.program_end; /* magic value ? */
	
}


void r300SetupVertexShader(r300ContextPtr rmesa)
{
	GLcontext* ctx = rmesa->radeon.glCtx;
	struct r300_vertex_shader_fragment unk4={
			length: 4,
			body: { f: {
				/*0.0*/(rand()%100)/10.0,
				/*0.0*/(rand()%100)/10.0,
				/*1.0*/(rand()%100)/10.0,
				/*0.0*/(rand()%100)/10.0
				} }
			};
	LOCAL_VARS

	/* Reset state, in case we don't use something */
	((drm_r300_cmd_header_t*)rmesa->hw.vpp.cmd)->vpu.count = 0;
	((drm_r300_cmd_header_t*)rmesa->hw.vpi.cmd)->vpu.count = 0;
	((drm_r300_cmd_header_t*)rmesa->hw.vps.cmd)->vpu.count = 0;

	/* Not sure why this doesnt work...
	   0x400 area might have something to do with pixel shaders as it appears right after pfs programming.
	   0x406 is set to { 0.0, 0.0, 1.0, 0.0 } most of the time but should change with smooth points and in other rare cases. */
	//setup_vertex_shader_fragment(rmesa, 0x406, &unk4);
	if(hw_tcl_on && ((struct r300_vertex_program *)CURRENT_VERTEX_SHADER(ctx))->translated){
		r300SetupVertexProgram(rmesa);
		return ;
	}

/* This needs to be replaced by vertex shader generation code */


#if 0
	/* textures enabled ? */
	if(rmesa->state.texture.tc_count>0){
		rmesa->state.vertex_shader=SINGLE_TEXTURE_VERTEX_SHADER;
		} else {
		rmesa->state.vertex_shader=FLAT_COLOR_VERTEX_SHADER;
		}
#endif

	r300GenerateSimpleVertexShader(rmesa);

        rmesa->state.vertex_shader.matrix[0].length=16;
        memcpy(rmesa->state.vertex_shader.matrix[0].body.f, ctx->_ModelProjectMatrix.m, 16*4);

	setup_vertex_shader_fragment(rmesa, VSF_DEST_PROGRAM, &(rmesa->state.vertex_shader.program));

	setup_vertex_shader_fragment(rmesa, VSF_DEST_MATRIX0, &(rmesa->state.vertex_shader.matrix[0]));
#if 0
	setup_vertex_shader_fragment(rmesa, VSF_DEST_MATRIX1, &(rmesa->state.vertex_shader.matrix[0]));
	setup_vertex_shader_fragment(rmesa, VSF_DEST_MATRIX2, &(rmesa->state.vertex_shader.matrix[0]));

	setup_vertex_shader_fragment(rmesa, VSF_DEST_VECTOR0, &(rmesa->state.vertex_shader.vector[0]));
	setup_vertex_shader_fragment(rmesa, VSF_DEST_VECTOR1, &(rmesa->state.vertex_shader.vector[1]));
#endif

#if 0
	setup_vertex_shader_fragment(rmesa, VSF_DEST_UNKNOWN1, &(rmesa->state.vertex_shader.unknown1));
	setup_vertex_shader_fragment(rmesa, VSF_DEST_UNKNOWN2, &(rmesa->state.vertex_shader.unknown2));
#endif

	R300_STATECHANGE(rmesa, pvs);
	rmesa->hw.pvs.cmd[R300_PVS_CNTL_1]=(rmesa->state.vertex_shader.program_start << R300_PVS_CNTL_1_PROGRAM_START_SHIFT)
		| (rmesa->state.vertex_shader.unknown_ptr1 << R300_PVS_CNTL_1_POS_END_SHIFT)
		| (rmesa->state.vertex_shader.program_end << R300_PVS_CNTL_1_PROGRAM_END_SHIFT);
	rmesa->hw.pvs.cmd[R300_PVS_CNTL_2]=(rmesa->state.vertex_shader.param_offset << R300_PVS_CNTL_2_PARAM_OFFSET_SHIFT)
		| (rmesa->state.vertex_shader.param_count << R300_PVS_CNTL_2_PARAM_COUNT_SHIFT);
	rmesa->hw.pvs.cmd[R300_PVS_CNTL_3]=(rmesa->state.vertex_shader.unknown_ptr2 << R300_PVS_CNTL_3_PROGRAM_UNKNOWN_SHIFT)
	| (rmesa->state.vertex_shader.unknown_ptr3 << 0);

	/* This is done for vertex shader fragments, but also needs to be done for vap_pvs,
	so I leave it as a reminder */
#if 0
	reg_start(R300_VAP_PVS_WAITIDLE,0);
		e32(0x00000000);
#endif
}

void r300SetupVertexProgram(r300ContextPtr rmesa)
{
	GLcontext* ctx = rmesa->radeon.glCtx;
	int inst_count;
	int param_count;
	LOCAL_VARS
	struct r300_vertex_program *prog=(struct r300_vertex_program *)CURRENT_VERTEX_SHADER(ctx);
			

	/* Reset state, in case we don't use something */
	((drm_r300_cmd_header_t*)rmesa->hw.vpp.cmd)->vpu.count = 0;
	((drm_r300_cmd_header_t*)rmesa->hw.vpi.cmd)->vpu.count = 0;
	((drm_r300_cmd_header_t*)rmesa->hw.vps.cmd)->vpu.count = 0;

	r300VertexProgUpdateParams(ctx, prog);

	setup_vertex_shader_fragment(rmesa, VSF_DEST_PROGRAM, &(prog->program));

	setup_vertex_shader_fragment(rmesa, VSF_DEST_MATRIX0, &(prog->params));

#if 0
	setup_vertex_shader_fragment(rmesa, VSF_DEST_UNKNOWN1, &(rmesa->state.vertex_shader.unknown1));
	setup_vertex_shader_fragment(rmesa, VSF_DEST_UNKNOWN2, &(rmesa->state.vertex_shader.unknown2));
#endif

	inst_count=prog->program.length/4 - 1;
	param_count=prog->params.length/4;

	R300_STATECHANGE(rmesa, pvs);
	rmesa->hw.pvs.cmd[R300_PVS_CNTL_1]=(0 << R300_PVS_CNTL_1_PROGRAM_START_SHIFT)
		| (inst_count/*pos_end*/ << R300_PVS_CNTL_1_POS_END_SHIFT)
		| (inst_count << R300_PVS_CNTL_1_PROGRAM_END_SHIFT);
	rmesa->hw.pvs.cmd[R300_PVS_CNTL_2]=(0 << R300_PVS_CNTL_2_PARAM_OFFSET_SHIFT)
		| (param_count << R300_PVS_CNTL_2_PARAM_COUNT_SHIFT);
	rmesa->hw.pvs.cmd[R300_PVS_CNTL_3]=(0/*rmesa->state.vertex_shader.unknown_ptr2*/ << R300_PVS_CNTL_3_PROGRAM_UNKNOWN_SHIFT)
	| (inst_count /*rmesa->state.vertex_shader.unknown_ptr3*/ << 0);

	/* This is done for vertex shader fragments, but also needs to be done for vap_pvs,
	so I leave it as a reminder */
#if 0
	reg_start(R300_VAP_PVS_WAITIDLE,0);
		e32(0x00000000);
#endif
}


/* just a skeleton for now.. */
void r300GenerateTexturePixelShader(r300ContextPtr r300)
{
	int i, mtu;
	mtu = r300->radeon.glCtx->Const.MaxTextureUnits;
	GLenum envMode;
	GLuint OutputsWritten = CURRENT_VERTEX_SHADER(r300->radeon.glCtx)->OutputsWritten;

	int tex_inst=0, alu_inst=0;

	for(i=0;i<mtu;i++){
		/* No need to proliferate {} */
		if(!TMU_ENABLED(r300->radeon.glCtx, i))continue;

		envMode = r300->radeon.glCtx->Texture.Unit[i].EnvMode;
		//fprintf(stderr, "envMode=%s\n", _mesa_lookup_enum_by_nr(envMode));

		/* Fetch textured pixel */

		r300->state.pixel_shader.program.tex.inst[tex_inst]=0x00018000;
		tex_inst++;

		switch(r300->radeon.glCtx->Texture.Unit[i]._CurrentCombine->ModeRGB){
			case GL_REPLACE:
				WARN_ONCE("ModeA==GL_REPLACE is possibly broken.\n");
				r300->state.pixel_shader.program.alu.inst[alu_inst].inst0=
					EASY_PFS_INSTR0(MAD, SRC0C_XYZ, ONE, ZERO);

				r300->state.pixel_shader.program.alu.inst[alu_inst].inst1=
					EASY_PFS_INSTR1(0, 0, 0 | PFS_FLAG_CONST, 0 | PFS_FLAG_CONST, NONE, ALL);
				break;
			case GL_MODULATE:
				WARN_ONCE("ModeRGB==GL_MODULATE is possibly broken.\n");
				r300->state.pixel_shader.program.alu.inst[alu_inst].inst0=
					EASY_PFS_INSTR0(MAD, SRC0C_XYZ, SRC1C_XYZ, ZERO);

				r300->state.pixel_shader.program.alu.inst[alu_inst].inst1=
					EASY_PFS_INSTR1(0, 0, 1, 0 | PFS_FLAG_CONST, NONE, ALL);

				break;
			default:
				WARN_ONCE("ModeRGB=%s is not implemented yet !\n",
					 _mesa_lookup_enum_by_nr(r300->radeon.glCtx->Texture.Unit[i]._CurrentCombine->ModeRGB));
				/* PFS_NOP */
				r300->state.pixel_shader.program.alu.inst[alu_inst].inst0=
					EASY_PFS_INSTR0(MAD, SRC0C_XYZ, ONE, ZERO);

				r300->state.pixel_shader.program.alu.inst[alu_inst].inst1=
					EASY_PFS_INSTR1(0, 0, 0 | PFS_FLAG_CONST, 0 | PFS_FLAG_CONST, NONE, ALL);
			}
		switch(r300->radeon.glCtx->Texture.Unit[i]._CurrentCombine->ModeA){
			case GL_REPLACE:
				WARN_ONCE("ModeA==GL_REPLACE is possibly broken.\n");
				r300->state.pixel_shader.program.alu.inst[alu_inst].inst2=
					EASY_PFS_INSTR2(MAD, SRC0A, ONE, ZERO);

				r300->state.pixel_shader.program.alu.inst[alu_inst].inst3=
					EASY_PFS_INSTR3(0, 0, 0| PFS_FLAG_CONST, 0 | PFS_FLAG_CONST, OUTPUT);

#if 0
				fprintf(stderr, "numArgsA=%d sourceA[0]=%s op=%d\n",
					 r300->radeon.glCtx->Texture.Unit[i]._CurrentCombine->_NumArgsA,
					 _mesa_lookup_enum_by_nr(r300->radeon.glCtx->Texture.Unit[i]._CurrentCombine->SourceA[0]),
					 r300->radeon.glCtx->Texture.Unit[i]._CurrentCombine->OperandA[0]-GL_SRC_ALPHA);
#endif
				break;
			case GL_MODULATE:
				WARN_ONCE("ModeA==GL_MODULATE is possibly broken.\n");

				r300->state.pixel_shader.program.alu.inst[alu_inst].inst2=
					EASY_PFS_INSTR2(MAD, SRC0A, SRC1A, ZERO);

				r300->state.pixel_shader.program.alu.inst[alu_inst].inst3=
					EASY_PFS_INSTR3(0, 0, 1, 0 | PFS_FLAG_CONST, OUTPUT);

				break;
			default:
				WARN_ONCE("ModeA=%s is not implemented yet !\n",
					 _mesa_lookup_enum_by_nr(r300->radeon.glCtx->Texture.Unit[i]._CurrentCombine->ModeA));
				/* PFS_NOP */
				r300->state.pixel_shader.program.alu.inst[alu_inst].inst2=
					EASY_PFS_INSTR2(MAD, SRC0A, ONE, ZERO);

				r300->state.pixel_shader.program.alu.inst[alu_inst].inst3=
					EASY_PFS_INSTR3(0, 0, 0 | PFS_FLAG_CONST, 0 | PFS_FLAG_CONST, OUTPUT);

			}

		alu_inst++;
		}
	
	r300->state.pixel_shader.program.tex.length=tex_inst;
	r300->state.pixel_shader.program.tex_offset=0;
	r300->state.pixel_shader.program.tex_end=tex_inst-1;

#if 0
	/* saturate last instruction, like i915 driver does */
	r300->state.pixel_shader.program.alu.inst[alu_inst-1].inst0|=R300_FPI0_OUTC_SAT;
	r300->state.pixel_shader.program.alu.inst[alu_inst-1].inst2|=R300_FPI2_OUTA_SAT;
#endif

	r300->state.pixel_shader.program.alu.length=alu_inst;
	r300->state.pixel_shader.program.alu_offset=0;
	r300->state.pixel_shader.program.alu_end=alu_inst-1;
}

void r300SetupPixelShader(r300ContextPtr rmesa)
{
int i,k;

	/* This needs to be replaced by pixel shader generation code */

	/* textures enabled ? */
	if(rmesa->state.texture.tc_count>0){
#if 1
		r300GenerateTextureFragmentShader(rmesa);
#else
		rmesa->state.pixel_shader=SINGLE_TEXTURE_PIXEL_SHADER;
		r300GenerateTexturePixelShader(rmesa);
#endif
		} else {
		rmesa->state.pixel_shader=FLAT_COLOR_PIXEL_SHADER;
		}
	
	R300_STATECHANGE(rmesa, fpt);
	for(i=0;i<rmesa->state.pixel_shader.program.tex.length;i++)
		rmesa->hw.fpt.cmd[R300_FPT_INSTR_0+i]=rmesa->state.pixel_shader.program.tex.inst[i];
	rmesa->hw.fpt.cmd[R300_FPT_CMD_0]=cmducs(R300_PFS_TEXI_0, rmesa->state.pixel_shader.program.tex.length);

#define OUTPUT_FIELD(st, reg, field)  \
		R300_STATECHANGE(rmesa, st); \
		for(i=0;i<rmesa->state.pixel_shader.program.alu.length;i++) \
			rmesa->hw.st.cmd[R300_FPI_INSTR_0+i]=rmesa->state.pixel_shader.program.alu.inst[i].field;\
		rmesa->hw.st.cmd[R300_FPI_CMD_0]=cmducs(reg, rmesa->state.pixel_shader.program.alu.length);

	OUTPUT_FIELD(fpi[0], R300_PFS_INSTR0_0, inst0);
	OUTPUT_FIELD(fpi[1], R300_PFS_INSTR1_0, inst1);
	OUTPUT_FIELD(fpi[2], R300_PFS_INSTR2_0, inst2);
	OUTPUT_FIELD(fpi[3], R300_PFS_INSTR3_0, inst3);
#undef OUTPUT_FIELD

	R300_STATECHANGE(rmesa, fp);
	for(i=0;i<4;i++){
		rmesa->hw.fp.cmd[R300_FP_NODE0+i]=
		(rmesa->state.pixel_shader.program.node[i].alu_offset << R300_PFS_NODE_ALU_OFFSET_SHIFT)
		| (rmesa->state.pixel_shader.program.node[i].alu_end  << R300_PFS_NODE_ALU_END_SHIFT)
		| (rmesa->state.pixel_shader.program.node[i].tex_offset << R300_PFS_NODE_TEX_OFFSET_SHIFT)
		| (rmesa->state.pixel_shader.program.node[i].tex_end  << R300_PFS_NODE_TEX_END_SHIFT)
		| ( (i==3) ? R300_PFS_NODE_LAST_NODE : 0);
		}

		/*  PFS_CNTL_0 */
	rmesa->hw.fp.cmd[R300_FP_CNTL0]=
		(rmesa->state.pixel_shader.program.active_nodes-1)
		| (rmesa->state.pixel_shader.program.first_node_has_tex<<3);
		/* PFS_CNTL_1 */
	rmesa->hw.fp.cmd[R300_FP_CNTL1]=rmesa->state.pixel_shader.program.temp_register_count;
		/* PFS_CNTL_2 */
	rmesa->hw.fp.cmd[R300_FP_CNTL2]=
		(rmesa->state.pixel_shader.program.alu_offset << R300_PFS_CNTL_ALU_OFFSET_SHIFT)
		| (rmesa->state.pixel_shader.program.alu_end << R300_PFS_CNTL_ALU_END_SHIFT)
		| (rmesa->state.pixel_shader.program.tex_offset << R300_PFS_CNTL_TEX_OFFSET_SHIFT)
		| (rmesa->state.pixel_shader.program.tex_end << R300_PFS_CNTL_TEX_END_SHIFT);

	R300_STATECHANGE(rmesa, fpp);
	for(i=0;i<rmesa->state.pixel_shader.param_length;i++){
		rmesa->hw.fpp.cmd[R300_FPP_PARAM_0+4*i+0]=r300PackFloat32(rmesa->state.pixel_shader.param[i].x);
		rmesa->hw.fpp.cmd[R300_FPP_PARAM_0+4*i+1]=r300PackFloat32(rmesa->state.pixel_shader.param[i].y);
		rmesa->hw.fpp.cmd[R300_FPP_PARAM_0+4*i+2]=r300PackFloat32(rmesa->state.pixel_shader.param[i].z);
		rmesa->hw.fpp.cmd[R300_FPP_PARAM_0+4*i+3]=r300PackFloat32(rmesa->state.pixel_shader.param[i].w);
		}
	rmesa->hw.fpp.cmd[R300_FPP_CMD_0]=cmducs(R300_PFS_PARAM_0_X, rmesa->state.pixel_shader.param_length);

}

/**
 * Called by Mesa after an internal state update.
 */
static void r300InvalidateState(GLcontext * ctx, GLuint new_state)
{
	r300ContextPtr r300 = R300_CONTEXT(ctx);

	_swrast_InvalidateState(ctx, new_state);
	_swsetup_InvalidateState(ctx, new_state);
	_ac_InvalidateState(ctx, new_state);
	_tnl_InvalidateState(ctx, new_state);
	_ae_invalidate_state(ctx, new_state);

	/* Go inefficiency! */
	r300ResetHwState(r300);
}

/* Checks that r300ResetHwState actually modifies all states.
   Should probably be burried in somewhere else as this file is getting longish. */
void verify_r300ResetHwState(r300ContextPtr r300, int stage)
{
	struct r300_state_atom* atom;
	int i;
	drm_r300_cmd_header_t cmd;
	
	if(stage){ /* mess around with states */
		unsigned long fp1, cb1;
	
		fp1=r300->hw.fp.cmd[R300_FP_CMD_1]; /* some special cases... */
		cb1=r300->hw.cb.cmd[R300_CB_CMD_1];
	
		fprintf(stderr, "verify begin:\n");
	
		foreach(atom, &r300->hw.atomlist) {
			for(i=1; i < (*atom->check)(r300, atom); i++)
				atom->cmd[i]=0xdeadbeef;
		}	
		r300->hw.fp.cmd[R300_FP_CMD_1]=fp1;
		r300->hw.cb.cmd[R300_CB_CMD_1]=cb1;
			
		foreach(atom, &r300->hw.atomlist) {
			cmd.u=atom->cmd[0];
			switch(cmd.header.cmd_type){
			case R300_CMD_UNCHECKED_STATE:
			case R300_CMD_VPU:
			case R300_CMD_PACKET3:
			case R300_CMD_END3D:
			case R300_CMD_CP_DELAY:
			case R300_CMD_DMA_DISCARD:
				break;
			default: fprintf(stderr, "unknown cmd_type %d in atom %s\n",
					cmd.header.cmd_type, atom->name);
			}
		
		}	
	} else { /* check that they were set */
		foreach(atom, &r300->hw.atomlist) {
			for(i=1; i < (*atom->check)(r300, atom); i++)
				if(atom->cmd[i]==0xdeadbeef)
					fprintf(stderr, "atom %s is untouched\n", atom->name);
		}	
	}
}
		
/**
 * Completely recalculates hardware state based on the Mesa state.
 */
void r300ResetHwState(r300ContextPtr r300)
{
	GLcontext* ctx = r300->radeon.glCtx;
	int i;

	if (RADEON_DEBUG & DEBUG_STATE)
		fprintf(stderr, "%s\n", __FUNCTION__);

	//verify_r300ResetHwState(r300, 1);
			
		/* This is a place to initialize registers which
		   have bitfields accessed by different functions
		   and not all bits are used */
#if 0
	/* initialize similiar to r200 */
	r300->hw.zs.cmd[R300_ZS_CNTL_0] = 0;
	r300->hw.zs.cmd[R300_ZS_CNTL_1] =
	    (R300_ZS_ALWAYS << R300_RB3D_ZS1_FRONT_FUNC_SHIFT) |
	    (R300_ZS_KEEP << R300_RB3D_ZS1_FRONT_FAIL_OP_SHIFT) |
	    (R300_ZS_KEEP << R300_RB3D_ZS1_FRONT_ZPASS_OP_SHIFT) |
	    (R300_ZS_KEEP << R300_RB3D_ZS1_FRONT_ZFAIL_OP_SHIFT) |
	    (R300_ZS_ALWAYS << R300_RB3D_ZS1_BACK_FUNC_SHIFT) |
	    (R300_ZS_KEEP << R300_RB3D_ZS1_BACK_FAIL_OP_SHIFT) |
	    (R300_ZS_KEEP << R300_RB3D_ZS1_BACK_ZPASS_OP_SHIFT) |
	    (R300_ZS_KEEP << R300_RB3D_ZS1_BACK_ZFAIL_OP_SHIFT);
	r300->hw.zs.cmd[R300_ZS_CNTL_2] = 0x00ffff00;
#endif

		/* go and compute register values from GL state */

	r300UpdateWindow(ctx);

	r300ColorMask(ctx,
		ctx->Color.ColorMask[RCOMP],
		ctx->Color.ColorMask[GCOMP],
		ctx->Color.ColorMask[BCOMP],
		ctx->Color.ColorMask[ACOMP]);

	r300Enable(ctx, GL_DEPTH_TEST, ctx->Depth.Test);
	r300DepthMask(ctx, ctx->Depth.Mask);
	r300DepthFunc(ctx, ctx->Depth.Func);
	
	/* stencil */
	r300Enable(ctx, GL_STENCIL_TEST, ctx->Stencil.Enabled);
	r300StencilMask(ctx, ctx->Stencil.WriteMask[0]);
	r300StencilFunc(ctx, ctx->Stencil.Function[0], ctx->Stencil.Ref[0], ctx->Stencil.ValueMask[0]);
	r300StencilOp(ctx, ctx->Stencil.FailFunc[0], ctx->Stencil.ZFailFunc[0], ctx->Stencil.ZPassFunc[0]);

	r300UpdateCulling(ctx);

	r300UpdateTextureState(ctx);

//	r300_setup_routing(ctx, GL_TRUE);
	if(hw_tcl_on == GL_FALSE){
		r300EmitArrays(ctx, GL_TRUE); /* Just do the routing */
		r300_setup_textures(ctx);
		r300_setup_rs_unit(ctx);

		r300SetupVertexShader(r300);
		r300SetupPixelShader(r300);
	}

	r300_set_blend_state(ctx);

	r300AlphaFunc(ctx, ctx->Color.AlphaFunc, ctx->Color.AlphaRef);
	r300Enable(ctx, GL_ALPHA_TEST, ctx->Color.AlphaEnabled);
		
		/* Initialize magic registers
		 TODO : learn what they really do, or get rid of
		 those we don't have to touch */
	r300->hw.unk2080.cmd[1] = 0x0030045A; /* Dangerous */

	r300->hw.vte.cmd[1] = R300_VPORT_X_SCALE_ENA
				| R300_VPORT_X_OFFSET_ENA
				| R300_VPORT_Y_SCALE_ENA
				| R300_VPORT_Y_OFFSET_ENA
				| R300_VPORT_Z_SCALE_ENA
				| R300_VPORT_Z_OFFSET_ENA
				| R300_VTX_W0_FMT;
	r300->hw.vte.cmd[2] = 0x00000008;

	r300->hw.unk2134.cmd[1] = 0x00FFFFFF;
	r300->hw.unk2134.cmd[2] = 0x00000000;
#ifdef MESA_BIG_ENDIAN
	r300->hw.unk2140.cmd[1] = 0x00000002;
#else
	r300->hw.unk2140.cmd[1] = 0x00000000;
#endif

#if 0 /* Done in setup routing */
	((drm_r300_cmd_header_t*)r300->hw.vir[0].cmd)->unchecked_state.count = 1;
	r300->hw.vir[0].cmd[1] = 0x21030003;

	((drm_r300_cmd_header_t*)r300->hw.vir[1].cmd)->unchecked_state.count = 1;
	r300->hw.vir[1].cmd[1] = 0xF688F688;

	r300->hw.vic.cmd[R300_VIR_CNTL_0] = 0x00000001;
	r300->hw.vic.cmd[R300_VIR_CNTL_1] = 0x00000405;
#endif

	r300->hw.unk21DC.cmd[1] = 0xAAAAAAAA;

	r300->hw.unk221C.cmd[1] = R300_221C_NORMAL;

	r300->hw.unk2220.cmd[1] = r300PackFloat32(1.0);
	r300->hw.unk2220.cmd[2] = r300PackFloat32(1.0);
	r300->hw.unk2220.cmd[3] = r300PackFloat32(1.0);
	r300->hw.unk2220.cmd[4] = r300PackFloat32(1.0);

	if (GET_CHIP(r300->radeon.radeonScreen) == RADEON_CHIP_R300)
		r300->hw.unk2288.cmd[1] = R300_2288_R300;
	else
		r300->hw.unk2288.cmd[1] = R300_2288_RV350;

#if 0
	r300->hw.vof.cmd[R300_VOF_CNTL_0] = R300_VAP_OUTPUT_VTX_FMT_0__POS_PRESENT
				| R300_VAP_OUTPUT_VTX_FMT_0__COLOR_PRESENT;
	r300->hw.vof.cmd[R300_VOF_CNTL_1] = 0; /* no textures */


	r300->hw.pvs.cmd[R300_PVS_CNTL_1] = 0;
	r300->hw.pvs.cmd[R300_PVS_CNTL_2] = 0;
	r300->hw.pvs.cmd[R300_PVS_CNTL_3] = 0;
#endif

	r300->hw.gb_enable.cmd[1] = R300_GB_POINT_STUFF_ENABLE
		| R300_GB_LINE_STUFF_ENABLE
		| R300_GB_TRIANGLE_STUFF_ENABLE /*| R300_GB_UNK30*/;

	r300->hw.gb_misc.cmd[R300_GB_MISC_MSPOS_0] = 0x66666666;
	r300->hw.gb_misc.cmd[R300_GB_MISC_MSPOS_1] = 0x06666666;
	if (GET_CHIP(r300->radeon.radeonScreen) == RADEON_CHIP_R300)
		r300->hw.gb_misc.cmd[R300_GB_MISC_TILE_CONFIG] = R300_GB_TILE_ENABLE
							| R300_GB_TILE_PIPE_COUNT_R300
							| R300_GB_TILE_SIZE_16;
	else if (GET_CHIP(r300->radeon.radeonScreen) == RADEON_CHIP_R420)
		r300->hw.gb_misc.cmd[R300_GB_MISC_TILE_CONFIG] = R300_GB_TILE_ENABLE
							| R300_GB_TILE_PIPE_COUNT_R420
							| R300_GB_TILE_SIZE_16;
	else
		r300->hw.gb_misc.cmd[R300_GB_MISC_TILE_CONFIG] = R300_GB_TILE_ENABLE
							| R300_GB_TILE_PIPE_COUNT_RV300
							| R300_GB_TILE_SIZE_16;
	r300->hw.gb_misc.cmd[R300_GB_MISC_SELECT] = 0x00000000;
	r300->hw.gb_misc.cmd[R300_GB_MISC_AA_CONFIG] = 0x00000000; /* No antialiasing */

	//r300->hw.txe.cmd[R300_TXE_ENABLE] = 0;

	r300->hw.unk4200.cmd[1] = r300PackFloat32(0.0);
	r300->hw.unk4200.cmd[2] = r300PackFloat32(0.0);
	r300->hw.unk4200.cmd[3] = r300PackFloat32(1.0);
	r300->hw.unk4200.cmd[4] = r300PackFloat32(1.0);

	r300->hw.unk4214.cmd[1] = 0x00050005;

	r300PointSize(ctx, 0.0);
#if 0
	r300->hw.ps.cmd[R300_PS_POINTSIZE] = (6 << R300_POINTSIZE_X_SHIFT) |
					     (6 << R300_POINTSIZE_Y_SHIFT);
#endif

	r300->hw.unk4230.cmd[1] = 0x01800000;//18000006;
	r300->hw.unk4230.cmd[2] = 0x00020006;
	r300->hw.unk4230.cmd[3] = r300PackFloat32(1.0 / 192.0);

	r300LineWidth(ctx, 0.0);

#ifdef EXP_C
	static int foobar=0;
	r300->hw.lsf.cmd[1] = foobar++; //0x3a088889;
#endif
	r300->hw.unk4260.cmd[1] = 0;
	r300->hw.unk4260.cmd[2] = r300PackFloat32(0.0);
	r300->hw.unk4260.cmd[3] = r300PackFloat32(1.0);

	r300->hw.unk4274.cmd[1] = 0x00000002;
	r300ShadeModel(ctx, ctx->Light.ShadeModel);
	r300->hw.unk4274.cmd[3] = 0x00000000;
	r300->hw.unk4274.cmd[4] = 0x00000000;

	r300PolygonMode(ctx, GL_FRONT, ctx->Polygon.FrontMode);
	r300PolygonMode(ctx, GL_BACK, ctx->Polygon.BackMode);
	r300->hw.unk4288.cmd[2] = 0x00000001;
	r300->hw.unk4288.cmd[3] = 0x00000000;
	r300->hw.unk4288.cmd[4] = 0x00000000;
	r300->hw.unk4288.cmd[5] = 0x00000000;

	r300->hw.unk42A0.cmd[1] = 0x00000000;

	r300PolygonOffset(ctx, ctx->Polygon.OffsetFactor, ctx->Polygon.OffsetUnits);
	r300Enable(ctx, GL_POLYGON_OFFSET_POINT, ctx->Polygon.OffsetPoint);
	r300Enable(ctx, GL_POLYGON_OFFSET_LINE, ctx->Polygon.OffsetLine);
	r300Enable(ctx, GL_POLYGON_OFFSET_FILL, ctx->Polygon.OffsetFill);
	
	r300->hw.unk42C0.cmd[1] = 0x4B7FFFFF;
	r300->hw.unk42C0.cmd[2] = 0x00000000;


	r300->hw.unk43A4.cmd[1] = 0x0000001C;
	r300->hw.unk43A4.cmd[2] = 0x2DA49525;

	r300->hw.unk43E8.cmd[1] = 0x00FFFFFF;

#if 0
	r300->hw.fp.cmd[R300_FP_CNTL0] = 0;
	r300->hw.fp.cmd[R300_FP_CNTL1] = 0;
	r300->hw.fp.cmd[R300_FP_CNTL2] = 0;
	r300->hw.fp.cmd[R300_FP_NODE0] = 0;
	r300->hw.fp.cmd[R300_FP_NODE1] = 0;
	r300->hw.fp.cmd[R300_FP_NODE2] = 0;
	r300->hw.fp.cmd[R300_FP_NODE3] = 0;
#endif

	r300->hw.unk46A4.cmd[1] = 0x00001B01;
	r300->hw.unk46A4.cmd[2] = 0x00001B0F;
	r300->hw.unk46A4.cmd[3] = 0x00001B0F;
	r300->hw.unk46A4.cmd[4] = 0x00001B0F;
	r300->hw.unk46A4.cmd[5] = 0x00000001;

#if 0
	for(i = 1; i <= 64; ++i) {
		/* create NOP instructions */
		r300->hw.fpi[0].cmd[i] = FP_INSTRC(MAD, FP_ARGC(SRC0C_XYZ), FP_ARGC(ONE), FP_ARGC(ZERO));
		r300->hw.fpi[1].cmd[i] = FP_SELC(0,XYZ,NO,FP_TMP(0),0,0);
		r300->hw.fpi[2].cmd[i] = FP_INSTRA(MAD, FP_ARGA(SRC0A), FP_ARGA(ONE), FP_ARGA(ZERO));
		r300->hw.fpi[3].cmd[i] = FP_SELA(0,W,NO,FP_TMP(0),0,0);
	}
#endif

	r300->hw.unk4BC0.cmd[1] = 0;

	r300->hw.unk4BC8.cmd[1] = 0;
	r300->hw.unk4BC8.cmd[2] = 0;
	r300->hw.unk4BC8.cmd[3] = 0;


	r300->hw.at.cmd[R300_AT_UNKNOWN] = 0;
	r300->hw.unk4BD8.cmd[1] = 0;

	r300->hw.unk4E00.cmd[1] = 0;

#if 0
	r300->hw.bld.cmd[R300_BLD_CBLEND] = 0;
	r300->hw.bld.cmd[R300_BLD_ABLEND] = 0;
#endif

	r300BlendColor(ctx, ctx->Color.BlendColor);
	r300->hw.unk4E10.cmd[2] = 0;
	r300->hw.unk4E10.cmd[3] = 0;
	
	/* Again, r300ClearBuffer uses this */
	if(ctx->Visual.doubleBufferMode){
		r300->hw.cb.cmd[R300_CB_OFFSET] =
			r300->radeon.radeonScreen->backOffset +
			r300->radeon.radeonScreen->fbLocation;
		r300->hw.cb.cmd[R300_CB_PITCH] = r300->radeon.radeonScreen->backPitch
			| R300_COLOR_UNKNOWN_22_23;
	} else {
		r300->hw.cb.cmd[R300_CB_OFFSET] =
			r300->radeon.radeonScreen->frontOffset +
			r300->radeon.radeonScreen->fbLocation;
		r300->hw.cb.cmd[R300_CB_PITCH] = r300->radeon.radeonScreen->frontPitch
			| R300_COLOR_UNKNOWN_22_23;
		
	}
	
	r300->hw.unk4E50.cmd[1] = 0;
	r300->hw.unk4E50.cmd[2] = 0;
	r300->hw.unk4E50.cmd[3] = 0;
	r300->hw.unk4E50.cmd[4] = 0;
	r300->hw.unk4E50.cmd[5] = 0;
	r300->hw.unk4E50.cmd[6] = 0;
	r300->hw.unk4E50.cmd[7] = 0;
	r300->hw.unk4E50.cmd[8] = 0;
	r300->hw.unk4E50.cmd[9] = 0;

	r300->hw.unk4E88.cmd[1] = 0;

	r300->hw.unk4EA0.cmd[1] = 0x00000000;
	r300->hw.unk4EA0.cmd[2] = 0xffffffff;

	r300->hw.unk4F10.cmd[1] = 0x00000002; // depthbuffer format?
	//r300->hw.unk4F10.cmd[2] = 0x00000000;
	r300->hw.unk4F10.cmd[3] = 0x00000003;
	r300->hw.unk4F10.cmd[4] = 0x00000000;

	/* experiment a bit */
	//r300->hw.unk4F10.cmd[2] = 0x00000001; // depthbuffer format?

	r300->hw.zb.cmd[R300_ZB_OFFSET] =
		r300->radeon.radeonScreen->depthOffset +
		r300->radeon.radeonScreen->fbLocation;
	r300->hw.zb.cmd[R300_ZB_PITCH] = r300->radeon.radeonScreen->depthPitch;

	r300->hw.unk4F28.cmd[1] = 0;

	r300->hw.unk4F30.cmd[1] = 0;
	r300->hw.unk4F30.cmd[2] = 0;

	r300->hw.unk4F44.cmd[1] = 0;

	r300->hw.unk4F54.cmd[1] = 0;

#if 0
	((drm_r300_cmd_header_t*)r300->hw.vpi.cmd)->vpu.count = 0;
	for(i = 1; i < R300_VPI_CMDSIZE; i += 4) {
		/* MOV t0, t0 */
		r300->hw.vpi.cmd[i+0] = VP_OUT(ADD,TMP,0,XYZW);
		r300->hw.vpi.cmd[i+1] = VP_IN(TMP,0);
		r300->hw.vpi.cmd[i+2] = VP_ZERO();
		r300->hw.vpi.cmd[i+3] = VP_ZERO();
	}

	((drm_r300_cmd_header_t*)r300->hw.vpp.cmd)->vpu.count = 0;
	for(i = 1; i < R300_VPP_CMDSIZE; ++i)
		r300->hw.vpp.cmd[i] = 0;
#endif

	r300->hw.vps.cmd[R300_VPS_ZERO_0] = 0;
	r300->hw.vps.cmd[R300_VPS_ZERO_1] = 0;
	r300->hw.vps.cmd[R300_VPS_POINTSIZE] = r300PackFloat32(1.0);
	r300->hw.vps.cmd[R300_VPS_ZERO_3] = 0;

//END: TODO
	//verify_r300ResetHwState(r300, 0);
	r300->hw.all_dirty = GL_TRUE;
}



/**
 * Calculate initial hardware state and register state functions.
 * Assumes that the command buffer and state atoms have been
 * initialized already.
 */
void r300InitState(r300ContextPtr r300)
{
	GLcontext *ctx = r300->radeon.glCtx;
	GLuint depth_fmt;

	radeonInitState(&r300->radeon);

	switch (ctx->Visual.depthBits) {
	case 16:
		r300->state.depth.scale = 1.0 / (GLfloat) 0xffff;
		depth_fmt = R200_DEPTH_FORMAT_16BIT_INT_Z;
		r300->state.stencil.clear = 0x00000000;
		break;
	case 24:
		r300->state.depth.scale = 1.0 / (GLfloat) 0xffffff;
		depth_fmt = R200_DEPTH_FORMAT_24BIT_INT_Z;
		r300->state.stencil.clear = 0x00ff0000;
		break;
	default:
		fprintf(stderr, "Error: Unsupported depth %d... exiting\n",
			ctx->Visual.depthBits);
		exit(-1);
	}

	/* Only have hw stencil when depth buffer is 24 bits deep */
	r300->state.stencil.hw_stencil = (ctx->Visual.stencilBits > 0 &&
					 ctx->Visual.depthBits == 24);

	memset(&(r300->state.texture), 0, sizeof(r300->state.texture));
	
	r300ResetHwState(r300);
}


/**
 * Initialize driver's state callback functions
 */
void r300InitStateFuncs(struct dd_function_table* functions)
{
	radeonInitStateFuncs(functions);

	functions->UpdateState = r300InvalidateState;
	functions->AlphaFunc = r300AlphaFunc;
	functions->BlendColor = r300BlendColor;
	functions->BlendEquationSeparate = r300BlendEquationSeparate;
	functions->BlendFuncSeparate = r300BlendFuncSeparate;
	functions->Enable = r300Enable;
	functions->ColorMask = r300ColorMask;
	functions->DepthFunc = r300DepthFunc;
	functions->DepthMask = r300DepthMask;
	functions->CullFace = r300CullFace;
	functions->FrontFace = r300FrontFace;
	functions->ShadeModel = r300ShadeModel;

	/* Stencil related */
	functions->ClearStencil = r300ClearStencil;
	functions->StencilFunc = r300StencilFunc;
	functions->StencilMask = r300StencilMask;
	functions->StencilOp = r300StencilOp;

	/* Viewport related */
	functions->Viewport = r300Viewport;
	functions->DepthRange = r300DepthRange;
	functions->PointSize = r300PointSize;
	functions->LineWidth = r300LineWidth;

	functions->PolygonOffset = r300PolygonOffset;
	functions->PolygonMode = r300PolygonMode;
}