summaryrefslogtreecommitdiffstats
path: root/src/glx/x11/indirect_vertex_array.c
blob: d4e6ab99ca99fe2591f26402cac96f71d6b686ef (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
/*
 * (C) Copyright IBM Corporation 2004, 2005
 * All Rights Reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sub license,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
 * IBM,
 * AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <inttypes.h>
#include <assert.h>
#include <string.h>

#include "glxclient.h"
#include "indirect.h"
#include <GL/glxproto.h>
#include "glxextensions.h"
#include "indirect_vertex_array.h"
#include "indirect_va_private.h"

#define __GLX_PAD(n) (((n)+3) & ~3)

/**
 * \file indirect_vertex_array.c
 * Implement GLX protocol for vertex arrays and vertex buffer objects.
 * 
 * The most important function in this fill is \c fill_array_info_cache.
 * The \c array_state_vector contains a cache of the ARRAY_INFO data sent
 * in the DrawArrays protocol.  Certain operations, such as enabling or
 * disabling an array, can invalidate this cache.  \c fill_array_info_cache
 * fills-in this data.  Additionally, it examines the enabled state and
 * other factors to determine what "version" of DrawArrays protocoal can be
 * used.
 * 
 * Current, only two versions of DrawArrays protocol are implemented.  The
 * first version is the "none" protocol.  This is the fallback when the
 * server does not support GL 1.1 / EXT_vertex_arrays.  It is implemented
 * by sending batches of immediate mode commands that are equivalent to the
 * DrawArrays protocol.
 *
 * The other protocol that is currently implemented is the "old" protocol.
 * This is the GL 1.1 DrawArrays protocol.  The only difference between GL
 * 1.1 and EXT_vertex_arrays is the opcode used for the DrawArrays command.
 * This protocol is called "old" because the ARB is in the process of
 * defining a new protocol, which will probably be called wither "new" or
 * "vbo", to support multiple texture coordinate arrays, generic attributes,
 * and vertex buffer objects.
 *
 * \author Ian Romanick <idr@us.ibm.com>
 */

static void emit_DrawArrays_none( GLenum mode, GLint first, GLsizei count );
static void emit_DrawArrays_old ( GLenum mode, GLint first, GLsizei count );

static void emit_DrawElements_none( GLenum mode, GLsizei count, GLenum type,
    const GLvoid *indices );
static void emit_DrawElements_old ( GLenum mode, GLsizei count, GLenum type,
    const GLvoid *indices );


static GLubyte * emit_element_none( GLubyte * dst,
    const struct array_state_vector * arrays, unsigned index );
static GLubyte * emit_element_old( GLubyte * dst,
    const struct array_state_vector * arrays, unsigned index );
static struct array_state * get_array_entry(
    const struct array_state_vector * arrays, GLenum key, unsigned index );
static void fill_array_info_cache( struct array_state_vector * arrays );
static GLboolean validate_mode(__GLXcontext *gc, GLenum mode);
static GLboolean validate_count(__GLXcontext *gc, GLsizei count);
static GLboolean validate_type(__GLXcontext *gc, GLenum type);


/**
 * Table of sizes, in bytes, of a GL types.  All of the type enums are be in 
 * the range 0x1400 - 0x140F.  That includes types added by extensions (i.e.,
 * \c GL_HALF_FLOAT_NV).  This elements of this table correspond to the
 * type enums masked with 0x0f.
 * 
 * \notes
 * \c GL_HALF_FLOAT_NV is not included.  Neither are \c GL_2_BYTES,
 * \c GL_3_BYTES, or \c GL_4_BYTES.
 */
const GLuint __glXTypeSize_table[16] = {
    1, 1, 2, 2, 4, 4, 4, 0, 0, 0, 8, 0, 0, 0, 0, 0
};



/**
 * Initialize vertex array state of a GLX context.
 * 
 * \param gc  GLX context whose vertex array state is to be initialized.
 * 
 * \warning
 * This function may only be called after __GLXcontext::gl_extension_bits,
 * __GLXcontext::server_minor, and __GLXcontext::server_major have been
 * initialized.  These values are used to determine what vertex arrays are
 * supported.
 * 
 * \bug
 * Return values from malloc are not properly tested.
 */
void
__glXInitVertexArrayState( __GLXcontext * gc )
{
    __GLXattribute * state = (__GLXattribute *)(gc->client_state_private);
    struct array_state_vector * arrays;

    unsigned array_count;
    unsigned texture_units = 1;
    unsigned i;
    unsigned j;
    unsigned vertex_program_attribs = 0;

    GLboolean got_fog = GL_FALSE;
    GLboolean got_secondary_color = GL_FALSE;


    arrays = calloc( 1, sizeof( struct array_state_vector ) );
    state->array_state = arrays;

    arrays->old_DrawArrays_possible = !state->NoDrawArraysProtocol;
    arrays->new_DrawArrays_possible = GL_FALSE;
    arrays->DrawArrays = NULL;

    arrays->active_texture_unit = 0;


    /* Determine how many arrays are actually needed.  Only arrays that
     * are supported by the server are create.  For example, if the server
     * supports only 2 texture units, then only 2 texture coordinate arrays
     * are created.
     * 
     * At the very least, GL_VERTEX_ARRAY, GL_NORMAL_ARRAY,
     * GL_COLOR_ARRAY, GL_INDEX_ARRAY, GL_TEXTURE_COORD_ARRAY, and
     * GL_EDGE_FLAG_ARRAY are supported.
     */

    array_count = 5;
    
    if ( __glExtensionBitIsEnabled( gc, GL_EXT_fog_coord_bit )
	 || (gc->server_major > 1) || (gc->server_minor >= 4) ) {
	got_fog = GL_TRUE;
	array_count++;
    }

    if ( __glExtensionBitIsEnabled( gc, GL_EXT_secondary_color_bit )
	 || (gc->server_major > 1) || (gc->server_minor >= 4) ) {
	got_secondary_color = GL_TRUE;
	array_count++;
    }

    if ( __glExtensionBitIsEnabled( gc, GL_ARB_multitexture_bit )
	 || (gc->server_major > 1) || (gc->server_minor >= 3) ) {
	__indirect_glGetIntegerv( GL_MAX_TEXTURE_UNITS, & texture_units );
    }

    if ( __glExtensionBitIsEnabled( gc, GL_ARB_vertex_program_bit ) ) {
	__indirect_glGetProgramivARB( GL_VERTEX_PROGRAM_ARB,
				      GL_MAX_PROGRAM_ATTRIBS_ARB,
				      & vertex_program_attribs );
    }

    arrays->num_texture_units = texture_units;
    arrays->num_vertex_program_attribs = vertex_program_attribs;
    array_count += texture_units + vertex_program_attribs;
    arrays->num_arrays = array_count;
    arrays->arrays = calloc( array_count, sizeof( struct array_state ) );

    arrays->arrays[0].data_type = GL_FLOAT;
    arrays->arrays[0].count = 3;
    arrays->arrays[0].key = GL_NORMAL_ARRAY;
    arrays->arrays[0].normalized = GL_TRUE;
    arrays->arrays[0].old_DrawArrays_possible = GL_TRUE;

    arrays->arrays[1].data_type = GL_FLOAT;
    arrays->arrays[1].count = 4;
    arrays->arrays[1].key = GL_COLOR_ARRAY;
    arrays->arrays[1].normalized = GL_TRUE;
    arrays->arrays[1].old_DrawArrays_possible = GL_TRUE;

    arrays->arrays[2].data_type = GL_FLOAT;
    arrays->arrays[2].count = 1;
    arrays->arrays[2].key = GL_INDEX_ARRAY;
    arrays->arrays[2].old_DrawArrays_possible = GL_TRUE;

    arrays->arrays[3].data_type = GL_UNSIGNED_BYTE;
    arrays->arrays[3].count = 1;
    arrays->arrays[3].key = GL_EDGE_FLAG_ARRAY;
    arrays->arrays[3].old_DrawArrays_possible = GL_TRUE;

    for ( i = 0 ; i < texture_units ; i++ ) {
	arrays->arrays[4 + i].data_type = GL_FLOAT;
	arrays->arrays[4 + i].count = 4;
	arrays->arrays[4 + i].key = GL_TEXTURE_COORD_ARRAY;

	arrays->arrays[4 + i].old_DrawArrays_possible = (i == 0);
	arrays->arrays[4 + i].index = i;

	arrays->arrays[4 + i].header[1] = i + GL_TEXTURE0;
    }
    
    i = 4 + texture_units;

    if ( got_fog ) {
	arrays->arrays[i].data_type = GL_FLOAT;
	arrays->arrays[i].count = 1;
	arrays->arrays[i].key = GL_FOG_COORDINATE_ARRAY;
	arrays->arrays[i].old_DrawArrays_possible = GL_TRUE;
	i++;
    }

    if ( got_secondary_color ) {
	arrays->arrays[i].data_type = GL_FLOAT;
	arrays->arrays[i].count = 3;
	arrays->arrays[i].key = GL_SECONDARY_COLOR_ARRAY;
	arrays->arrays[i].old_DrawArrays_possible = GL_TRUE;
	arrays->arrays[i].normalized = GL_TRUE;
	i++;
    }


    for ( j = 0 ; j < vertex_program_attribs ; j++ ) {
	const unsigned idx = (vertex_program_attribs - (j + 1));


	arrays->arrays[idx + i].data_type = GL_FLOAT;
	arrays->arrays[idx + i].count = 4;
	arrays->arrays[idx + i].key = GL_VERTEX_ATTRIB_ARRAY_POINTER;

	arrays->arrays[idx + i].old_DrawArrays_possible = 0;
	arrays->arrays[idx + i].index = idx;

	arrays->arrays[idx + i].header[1] = idx;
    }

    i += vertex_program_attribs;


    /* Vertex array *must* be last becuase of the way that
     * emit_DrawArrays_none works.
     */

    arrays->arrays[i].data_type = GL_FLOAT;
    arrays->arrays[i].count = 4;
    arrays->arrays[i].key = GL_VERTEX_ARRAY;
    arrays->arrays[i].old_DrawArrays_possible = GL_TRUE;

    assert( (i + 1) == arrays->num_arrays );

    arrays->stack_index = 0;
    arrays->stack = malloc( sizeof( struct array_stack_state )
			    * arrays->num_arrays );
}


/**
 * Calculate the size of a single vertex for the "none" protocol.  This is
 * essentially the size of all the immediate-mode commands required to
 * implement the enabled vertex arrays.
 */
static size_t
calculate_single_vertex_size_none( const struct array_state_vector * arrays )
{
    size_t single_vertex_size = 0;
    unsigned   i;


    for ( i = 0 ; i < arrays->num_arrays ; i++ ) {
    	if ( arrays->arrays[i].enabled ) {
	    single_vertex_size += ((uint16_t *)arrays->arrays[i].header)[0];
	}
    }
    
    return single_vertex_size;
}


/**
 * Emit a single element using non-DrawArrays protocol.
 */
GLubyte *
emit_element_none( GLubyte * dst,
		   const struct array_state_vector * arrays,
		   unsigned index )
{
    unsigned i;


    for ( i = 0 ; i < arrays->num_arrays ; i++ ) {
	if ( arrays->arrays[i].enabled ) {
	    const size_t offset = index * arrays->arrays[i].true_stride;

	    /* The generic attributes can have more data than is in the
	     * elements.  This is because a vertex array can be a 2 element,
	     * normalized, unsigned short, but the "closest" immediate mode
	     * protocol is for a 4Nus.  Since the sizes are small, the
	     * performance impact on modern processors should be negligible.
	     */
	    (void) memset( dst, 0,
			   ((uint16_t *)arrays->arrays[i].header)[0] );

	    (void) memcpy( dst, arrays->arrays[i].header, 
			   arrays->arrays[i].header_size );

	    dst += arrays->arrays[i].header_size;

	    (void) memcpy( dst, ((GLubyte *) arrays->arrays[i].data) + offset,
			   arrays->arrays[i].element_size );

	    dst += __GLX_PAD( arrays->arrays[i].element_size );
	}
    }

    return dst;
}


/**
 * Emit a single element using "old" DrawArrays protocol from
 * EXT_vertex_arrays / OpenGL 1.1.
 */
GLubyte *
emit_element_old( GLubyte * dst,
		  const struct array_state_vector * arrays,
		  unsigned index )
{
    unsigned i;


    for ( i = 0 ; i < arrays->num_arrays ; i++ ) {
	if ( arrays->arrays[i].enabled ) {
	    const size_t offset = index * arrays->arrays[i].true_stride;

	    (void) memcpy( dst, ((GLubyte *) arrays->arrays[i].data) + offset,
			   arrays->arrays[i].element_size );

	    dst += __GLX_PAD( arrays->arrays[i].element_size );
	}
    }

    return dst;
}


struct array_state *
get_array_entry( const struct array_state_vector * arrays,
		 GLenum key, unsigned index )
{
    unsigned  i;

    for ( i = 0 ; i < arrays->num_arrays ; i++ ) {
	if ( (arrays->arrays[i].key == key)
	     && (arrays->arrays[i].index == index) ) {
	    return & arrays->arrays[i];
	}
    }
    
    return NULL;
}


static GLboolean
allocate_array_info_cache( struct array_state_vector * arrays,
			   size_t required_size )
{
#define MAX_HEADER_SIZE 20
    if ( arrays->array_info_cache_buffer_size < required_size ) {
	GLubyte * temp = realloc( arrays->array_info_cache, required_size 
				  + MAX_HEADER_SIZE );

	if ( temp == NULL ) {
	    return GL_FALSE;
	}

	arrays->array_info_cache = temp + MAX_HEADER_SIZE;
	arrays->array_info_cache_buffer_size = required_size;
    }

    arrays->array_info_cache_size = required_size;
    return GL_TRUE;
}


/**
 */
void
fill_array_info_cache( struct array_state_vector * arrays )
{
    GLboolean old_DrawArrays_possible;
    unsigned  i;


    /* Determine how many arrays are enabled.
     */

    arrays->enabled_client_array_count = 0;
    old_DrawArrays_possible = arrays->old_DrawArrays_possible;
    for ( i = 0 ; i < arrays->num_arrays ; i++ ) {
	if ( arrays->arrays[i].enabled ) {
	    arrays->enabled_client_array_count++;
	    old_DrawArrays_possible &= arrays->arrays[i].old_DrawArrays_possible;
	}
    }
    
    
    if ( arrays->new_DrawArrays_possible ) {
	assert( ! arrays->new_DrawArrays_possible );
    }
    else if ( old_DrawArrays_possible ) {
	const size_t required_size = arrays->enabled_client_array_count * 12;
	uint32_t * info;


	if ( ! allocate_array_info_cache( arrays, required_size ) ) {
	    return;
	}


	info = (uint32_t *) arrays->array_info_cache;
	for ( i = 0 ; i < arrays->num_arrays ; i++ ) {
	    if ( arrays->arrays[i].enabled ) {
		*(info++) = arrays->arrays[i].data_type;
		*(info++) = arrays->arrays[i].count;
		*(info++) = arrays->arrays[i].key;
	    }
	}

	arrays->DrawArrays = emit_DrawArrays_old;
	arrays->DrawElements = emit_DrawElements_old;
    }
    else {
	arrays->DrawArrays = emit_DrawArrays_none;
	arrays->DrawElements = emit_DrawElements_none;
    }

    arrays->array_info_cache_valid = GL_TRUE;
}


/**
 * Emit a \c glDrawArrays command using the "none" protocol.  That is,
 * emit immediate-mode commands that are equivalent to the requiested
 * \c glDrawArrays command.  This is used with servers that don't support
 * the OpenGL 1.1 / EXT_vertex_arrays DrawArrays protocol or in cases where
 * vertex state is enabled that is not compatible with that protocol.
 */
void
emit_DrawArrays_none( GLenum mode, GLint first, GLsizei count )
{
    __GLXcontext *gc = __glXGetCurrentContext();
    const __GLXattribute * state = 
       (const __GLXattribute *)(gc->client_state_private);
    struct array_state_vector * arrays = state->array_state;

    size_t single_vertex_size;
    GLubyte * pc;
    unsigned  i;
    static const uint16_t begin_cmd[2] = { 8, X_GLrop_Begin };
    static const uint16_t end_cmd[2]   = { 4, X_GLrop_End };


    single_vertex_size = calculate_single_vertex_size_none( arrays );

    pc = gc->pc;

    (void) memcpy( pc, begin_cmd, 4 );
    *(int *)(pc +  4) = mode;

    pc += 8;

    for ( i = 0 ; i < count ; i++ ) {
	if ( (pc + single_vertex_size) >= gc->bufEnd ) {
	    pc = __glXFlushRenderBuffer(gc, gc->pc);
	}

	pc = emit_element_none( pc, arrays, first + i );
    }

    if ( (pc + 4) >= gc->bufEnd ) {
	pc = __glXFlushRenderBuffer(gc, gc->pc);
    }

    (void) memcpy( pc, end_cmd, 4 );
    pc += 4;

    gc->pc = pc;
    if ( gc->pc > gc->limit ) {
	(void) __glXFlushRenderBuffer(gc, gc->pc);
    }
}


/**
 * Emit the header data for the GL 1.1 / EXT_vertex_arrays DrawArrays
 * protocol.
 * 
 * \param gc                    GLX context.
 * \param arrays                Array state.
 * \param elements_per_request  Location to store the number of elements that
 *                              can fit in a single Render / RenderLarge
 *                              command.
 * \param total_request         Total number of requests for a RenderLarge
 *                              command.  If a Render command is used, this
 *                              will be zero.
 * \param mode                  Drawing mode.
 * \param count                 Number of vertices.
 * 
 * \returns
 * A pointer to the buffer for array data.
 */
static GLubyte *
emit_DrawArrays_header_old( __GLXcontext * gc,
			    struct array_state_vector * arrays,
			    size_t * elements_per_request,
			    size_t * total_requests,
			    GLenum mode, GLsizei count )
{
    size_t command_size;
    size_t single_vertex_size;
    const unsigned header_size = 16;
    unsigned  i;
    GLubyte * pc;


    /* Determine the size of the whole command.  This includes the header,
     * the ARRAY_INFO data and the array data.  Once this size is calculated,
     * it will be known whether a Render or RenderLarge command is needed.
     */

    single_vertex_size = 0;
    for ( i = 0 ; i < arrays->num_arrays ; i++ ) {
	if ( arrays->arrays[i].enabled ) {
	    single_vertex_size += __GLX_PAD( arrays->arrays[i].element_size );
	}
    }

    command_size = arrays->array_info_cache_size + header_size 
      + (single_vertex_size * count);


    /* Write the header for either a Render command or a RenderLarge
     * command.  After the header is written, write the ARRAY_INFO data.
     */

    if ( command_size > gc->maxSmallRenderCommandSize ) {
	/* maxSize is the maximum amount of data can be stuffed into a single
	 * packet.  sz_xGLXRenderReq is added because bufSize is the maximum
	 * packet size minus sz_xGLXRenderReq.
	 */
	const size_t maxSize = (gc->bufSize + sz_xGLXRenderReq)
	  - sz_xGLXRenderLargeReq;
	unsigned vertex_requests;


	/* Calculate the number of data packets that will be required to send
	 * the whole command.  To do this, the number of verticies that
	 * will fit in a single buffer must be calculated.
	 * 
	 * The important value here is elements_per_request.  This is the
	 * number of complete array elements that will fit in a single
	 * buffer.  There may be some wasted space at the end of the buffer,
	 * but splitting elements across buffer boundries would be painful.
	 */

	elements_per_request[0] = maxSize / single_vertex_size;

	vertex_requests = (count + elements_per_request[0] - 1)
	  / elements_per_request[0];
	  
	*total_requests = vertex_requests + 1;


	__glXFlushRenderBuffer(gc, gc->pc);

	command_size += 4;

	pc = ((GLubyte *) arrays->array_info_cache) - (header_size + 4);
	*(uint32_t *)(pc +  0) = command_size;
	*(uint32_t *)(pc +  4) = X_GLrop_DrawArrays;
	*(uint32_t *)(pc +  8) = count;
	*(uint32_t *)(pc + 12) = arrays->enabled_client_array_count;
	*(uint32_t *)(pc + 16) = mode;

	__glXSendLargeChunk( gc, 1, *total_requests, pc,
			     header_size + 4 + arrays->array_info_cache_size );

	pc = gc->pc;
    }
    else {
	if ( (gc->pc + command_size) >= gc->bufEnd ) {
	    (void) __glXFlushRenderBuffer(gc, gc->pc);
	}

	pc = gc->pc;
	*(uint16_t *)(pc +  0) = command_size;
	*(uint16_t *)(pc +  2) = X_GLrop_DrawArrays;
	*(uint32_t *)(pc +  4) = count;
	*(uint32_t *)(pc +  8) = arrays->enabled_client_array_count;
	*(uint32_t *)(pc + 12) = mode;

	pc += header_size;

	(void) memcpy( pc, arrays->array_info_cache,
		       arrays->array_info_cache_size );
	pc += arrays->array_info_cache_size;

	*elements_per_request = count;
	*total_requests = 0;
    }


    return pc;
}


/**
 */
void
emit_DrawArrays_old( GLenum mode, GLint first, GLsizei count )
{
    __GLXcontext *gc = __glXGetCurrentContext();
    const __GLXattribute * state = 
       (const __GLXattribute *)(gc->client_state_private);
    struct array_state_vector * arrays = state->array_state;

    GLubyte * pc;
    size_t elements_per_request;
    unsigned total_requests = 0;
    unsigned i;
    size_t total_sent = 0;


    pc = emit_DrawArrays_header_old( gc, arrays, & elements_per_request,
				     & total_requests, mode, count);

    
    /* Write the arrays.
     */

    if ( total_requests == 0 ) {
	assert( elements_per_request >= count );

	for ( i = 0 ; i < count ; i++ ) {
	    pc = emit_element_old( pc, arrays, i + first );
	}

	assert( pc <= gc->bufEnd );

	gc->pc = pc;
	if ( gc->pc > gc->limit ) {
	    (void) __glXFlushRenderBuffer(gc, gc->pc);
	}
    }
    else {
	unsigned req;


	for ( req = 2 ; req <= total_requests ; req++ ) {
	    if ( count < elements_per_request ) {
		elements_per_request = count;
	    }

	    pc = gc->pc;
	    for ( i = 0 ; i < elements_per_request ; i++ ) {
		pc = emit_element_old( pc, arrays, i + first );
	    }

	    first += elements_per_request;

	    total_sent += (size_t) (pc - gc->pc);
	    __glXSendLargeChunk( gc, req, total_requests, gc->pc,
				 pc - gc->pc );

	    count -= elements_per_request;
	}
    }
}


void
emit_DrawElements_none( GLenum mode, GLsizei count, GLenum type,
			const GLvoid *indices )
{
    __GLXcontext *gc = __glXGetCurrentContext();
    const __GLXattribute * state = 
       (const __GLXattribute *)(gc->client_state_private);
    struct array_state_vector * arrays = state->array_state;
    static const uint16_t begin_cmd[2] = { 8, X_GLrop_Begin };
    static const uint16_t end_cmd[2]   = { 4, X_GLrop_End };

    GLubyte * pc;
    size_t single_vertex_size;
    unsigned  i;


    single_vertex_size = calculate_single_vertex_size_none( arrays );


    if ( (gc->pc + single_vertex_size) >= gc->bufEnd ) {
	gc->pc = __glXFlushRenderBuffer(gc, gc->pc);
    }

    pc = gc->pc;

    (void) memcpy( pc, begin_cmd, 4 );
    *(int *)(pc +  4) = mode;

    pc += 8;

    for ( i = 0 ; i < count ; i++ ) {
	unsigned  index = 0;

	if ( (pc + single_vertex_size) >= gc->bufEnd ) {
	    pc = __glXFlushRenderBuffer(gc, gc->pc);
	}

	switch( type ) {
	case GL_UNSIGNED_INT:
	    index = (unsigned) (((GLuint *) indices)[i]);
	    break;
	case GL_UNSIGNED_SHORT:
	    index = (unsigned) (((GLushort *) indices)[i]);
	    break;
	case GL_UNSIGNED_BYTE:
	    index = (unsigned) (((GLubyte *) indices)[i]);
	    break;
	}
	pc = emit_element_none( pc, arrays, index );
    }

    if ( (pc + 4) >= gc->bufEnd ) {
	pc = __glXFlushRenderBuffer(gc, gc->pc);
    }

    (void) memcpy( pc, end_cmd, 4 );
    pc += 4;

    gc->pc = pc;
    if ( gc->pc > gc->limit ) {
	(void) __glXFlushRenderBuffer(gc, gc->pc);
    }
}


/**
 */
void
emit_DrawElements_old( GLenum mode, GLsizei count, GLenum type,
		       const GLvoid *indices )
{
    __GLXcontext *gc = __glXGetCurrentContext();
    const __GLXattribute * state = 
       (const __GLXattribute *)(gc->client_state_private);
    struct array_state_vector * arrays = state->array_state;

    GLubyte * pc;
    size_t elements_per_request;
    unsigned total_requests = 0;
    unsigned i;
    unsigned req;


    pc = emit_DrawArrays_header_old( gc, arrays, & elements_per_request,
				     & total_requests, mode, count);

    
    /* Write the arrays.
     */

    req = 2;
    while ( count > 0 ) {
	if ( count < elements_per_request ) {
	    elements_per_request = count;
	}

	switch( type ) {
	case GL_UNSIGNED_INT: {
	    const GLuint   * ui_ptr = (const GLuint   *) indices;

	    for ( i = 0 ; i < elements_per_request ; i++ ) {
		const GLint index = (GLint) *(ui_ptr++);
		pc = emit_element_old( pc, arrays, index );
	    }
	    break;
	}
	case GL_UNSIGNED_SHORT: {
	    const GLushort * us_ptr = (const GLushort *) indices;

	    for ( i = 0 ; i < elements_per_request ; i++ ) {
		const GLint index = (GLint) *(us_ptr++);
		pc = emit_element_old( pc, arrays, index );
	    }
	    break;
	}
	case GL_UNSIGNED_BYTE: {
	    const GLubyte  * ub_ptr = (const GLubyte  *) indices;

	    for ( i = 0 ; i < elements_per_request ; i++ ) {
		const GLint index = (GLint) *(ub_ptr++);
		pc = emit_element_old( pc, arrays, index );
	    }
	    break;
	}
	}

	if ( total_requests != 0 ) {
	    __glXSendLargeChunk( gc, req, total_requests, gc->pc,
				 pc - gc->pc );
	    pc = gc->pc;
	    req++;
	}

	count -= elements_per_request;
    }


    assert( (total_requests == 0) || ((req - 1) == total_requests) );

    if ( total_requests == 0 ) {
	assert( pc <= gc->bufEnd );

	gc->pc = pc;
	if ( gc->pc > gc->limit ) {
	    (void) __glXFlushRenderBuffer(gc, gc->pc);
	}
    }
}


/**
 * Validate that the \c mode parameter to \c glDrawArrays, et. al. is valid.
 * If it is not valid, then an error code is set in the GLX context.
 * 
 * \returns
 * \c GL_TRUE if the argument is valid, \c GL_FALSE if is not.
 */
static GLboolean
validate_mode(__GLXcontext *gc, GLenum mode)
{
    switch(mode) {
      case GL_POINTS:
      case GL_LINE_STRIP:
      case GL_LINE_LOOP:
      case GL_LINES:
      case GL_TRIANGLE_STRIP:
      case GL_TRIANGLE_FAN:
      case GL_TRIANGLES:
      case GL_QUAD_STRIP:
      case GL_QUADS:
      case GL_POLYGON:
	break;
      default:
        __glXSetError(gc, GL_INVALID_ENUM);
        return GL_FALSE;
    }
    
    return GL_TRUE;
}


/**
 * Validate that the \c count parameter to \c glDrawArrays, et. al. is valid.
 * A value less than zero is invalid and will result in \c GL_INVALID_VALUE
 * being set.  A value of zero will not result in an error being set, but
 * will result in \c GL_FALSE being returned.
 * 
 * \returns
 * \c GL_TRUE if the argument is valid, \c GL_FALSE if it is not.
 */
static GLboolean
validate_count(__GLXcontext *gc, GLsizei count)
{
    if (count < 0) {
	__glXSetError(gc, GL_INVALID_VALUE);
    }

    return (count > 0);
}


/**
 * Validate that the \c type parameter to \c glDrawElements, et. al. is
 * valid.  Only \c GL_UNSIGNED_BYTE, \c GL_UNSIGNED_SHORT, and
 * \c GL_UNSIGNED_INT are valid.
 *
 * \returns
 * \c GL_TRUE if the argument is valid, \c GL_FALSE if it is not.
 */
static GLboolean validate_type(__GLXcontext *gc, GLenum type)
{
    switch( type ) {
    case GL_UNSIGNED_INT:
    case GL_UNSIGNED_SHORT:
    case GL_UNSIGNED_BYTE:
	return GL_TRUE;
     default:
	__glXSetError(gc, GL_INVALID_ENUM);
	return GL_FALSE;
    }
}


void __indirect_glDrawArrays(GLenum mode, GLint first, GLsizei count)
{
    __GLXcontext *gc = __glXGetCurrentContext();
    const __GLXattribute * state = 
       (const __GLXattribute *)(gc->client_state_private);
    struct array_state_vector * arrays = state->array_state;
    

    if ( validate_mode(gc, mode) && validate_count(gc, count) ) {
	if ( ! arrays->array_info_cache_valid ) {
	    fill_array_info_cache( arrays );
	}

	arrays->DrawArrays(mode, first, count);
    }
}


void __indirect_glArrayElement(GLint index)
{
    __GLXcontext *gc = __glXGetCurrentContext();
    const __GLXattribute * state = 
       (const __GLXattribute *)(gc->client_state_private);
    struct array_state_vector * arrays = state->array_state;

    size_t single_vertex_size;


    single_vertex_size = calculate_single_vertex_size_none( arrays );

    if ( (gc->pc + single_vertex_size) >= gc->bufEnd ) {
	gc->pc = __glXFlushRenderBuffer(gc, gc->pc);
    }

    gc->pc = emit_element_none( gc->pc, arrays, index );

    if ( gc->pc > gc->limit ) {
	(void) __glXFlushRenderBuffer(gc, gc->pc);
    }
}


void __indirect_glDrawElements(GLenum mode, GLsizei count, GLenum type,
		    const GLvoid *indices)
{
    __GLXcontext *gc = __glXGetCurrentContext();
    const __GLXattribute * state = 
       (const __GLXattribute *)(gc->client_state_private);
    struct array_state_vector * arrays = state->array_state;


    if ( validate_mode(gc, mode) && validate_count(gc, count)
	 && validate_type(gc, type) ) {
	if ( ! arrays->array_info_cache_valid ) {
	    fill_array_info_cache( arrays );
	}

	arrays->DrawElements(mode, count, type, indices);
    }
}


void __indirect_glDrawRangeElements(GLenum mode, GLuint start, GLuint end,
				    GLsizei count, GLenum type,
				    const GLvoid *indices)
{
    __GLXcontext *gc = __glXGetCurrentContext();
    const __GLXattribute * state = 
       (const __GLXattribute *)(gc->client_state_private);
    struct array_state_vector * arrays = state->array_state;


    if ( validate_mode(gc, mode) && validate_count(gc, count)
	 && validate_type(gc, type) ) {
	if (end < start) {
	    __glXSetError(gc, GL_INVALID_VALUE);
	    return;
	}

	if ( ! arrays->array_info_cache_valid ) {
	    fill_array_info_cache( arrays );
	}

	arrays->DrawElements(mode, count, type, indices);
    }
}


void __indirect_glMultiDrawArraysEXT(GLenum mode, GLint *first, GLsizei *count,
		       GLsizei primcount)
{
    __GLXcontext *gc = __glXGetCurrentContext();
    const __GLXattribute * state = 
       (const __GLXattribute *)(gc->client_state_private);
    struct array_state_vector * arrays = state->array_state;
    GLsizei  i;


    if ( validate_mode(gc, mode) ) {
	if ( ! arrays->array_info_cache_valid ) {
	    fill_array_info_cache( arrays );
	}

	for ( i = 0 ; i < primcount ; i++ ) {
	    if ( validate_count( gc, count[i] ) ) {
		arrays->DrawArrays(mode, first[i], count[i]);
	    }
	}
    }
}


void __indirect_glMultiDrawElementsEXT(GLenum mode, const GLsizei *count,
			 GLenum type, const GLvoid ** indices,
			 GLsizei primcount)
{
    __GLXcontext *gc = __glXGetCurrentContext();
    const __GLXattribute * state = 
       (const __GLXattribute *)(gc->client_state_private);
    struct array_state_vector * arrays = state->array_state;
    GLsizei  i;


    if ( validate_mode(gc, mode) && validate_type(gc, type) ) {
	if ( ! arrays->array_info_cache_valid ) {
	    fill_array_info_cache( arrays );
	}

	for ( i = 0 ; i < primcount ; i++ ) {
	    if ( validate_count( gc, count[i] ) ) {
		arrays->DrawElements(mode, count[i], type, indices[i]);
	    }
	}
    }
}


#define COMMON_ARRAY_DATA_INIT(a, PTR, TYPE, STRIDE, COUNT, NORMALIZED, HDR_SIZE, OPCODE) \
    do { \
	(a)->data = PTR; \
	(a)->data_type = TYPE; \
	(a)->user_stride = STRIDE; \
	(a)->count = COUNT; \
	(a)->normalized = NORMALIZED; \
	\
	(a)->element_size = __glXTypeSize( TYPE ) * COUNT; \
	(a)->true_stride = (STRIDE == 0) \
	  ? (a)->element_size : STRIDE; \
	\
	(a)->header_size = HDR_SIZE; \
	((uint16_t *) (a)->header)[0] = __GLX_PAD((a)->header_size + (a)->element_size); \
	((uint16_t *) (a)->header)[1] = OPCODE; \
    } while(0)


void __indirect_glVertexPointer( GLint size, GLenum type, GLsizei stride,
				 const GLvoid * pointer )
{
    static const uint16_t short_ops[5] = {
	0, 0, X_GLrop_Vertex2sv, X_GLrop_Vertex3sv, X_GLrop_Vertex4sv
    };
    static const uint16_t int_ops[5] = {
	0, 0, X_GLrop_Vertex2iv, X_GLrop_Vertex3iv, X_GLrop_Vertex4iv
    };
    static const uint16_t float_ops[5] = {
	0, 0, X_GLrop_Vertex2fv, X_GLrop_Vertex3fv, X_GLrop_Vertex4fv
    };
    static const uint16_t double_ops[5] = {
	0, 0, X_GLrop_Vertex2dv, X_GLrop_Vertex3dv, X_GLrop_Vertex4dv
    };
    uint16_t opcode;
    __GLXcontext *gc = __glXGetCurrentContext();
    __GLXattribute * state = (__GLXattribute *)(gc->client_state_private);
    struct array_state_vector * arrays = state->array_state;
    struct array_state * a;


    if (size < 2 || size > 4 || stride < 0) {
        __glXSetError(gc, GL_INVALID_VALUE);
        return;
    }
    
    switch ( type ) {
    case GL_SHORT:	opcode = short_ops[size]; break;
    case GL_INT:	opcode = int_ops[size]; break;
    case GL_FLOAT:	opcode = float_ops[size]; break;
    case GL_DOUBLE:	opcode = double_ops[size]; break;
    default:
        __glXSetError(gc, GL_INVALID_ENUM);
        return;
    }

    a = get_array_entry( arrays, GL_VERTEX_ARRAY, 0 );
    assert( a != NULL );
    COMMON_ARRAY_DATA_INIT( a, pointer, type, stride, size, GL_FALSE, 4,
			    opcode );

    if ( a->enabled ) {
	arrays->array_info_cache_valid = GL_FALSE;
    }
}


void __indirect_glNormalPointer( GLenum type, GLsizei stride,
				 const GLvoid * pointer )
{
    uint16_t opcode;
    __GLXcontext *gc = __glXGetCurrentContext();
    __GLXattribute * state = (__GLXattribute *)(gc->client_state_private);
    struct array_state_vector * arrays = state->array_state;
    struct array_state * a;


    if (stride < 0) {
        __glXSetError(gc, GL_INVALID_VALUE);
        return;
    }

    switch ( type ) {
    case GL_BYTE:	opcode = X_GLrop_Normal3bv; break;
    case GL_SHORT:	opcode = X_GLrop_Normal3sv; break;
    case GL_INT:	opcode = X_GLrop_Normal3iv; break;
    case GL_FLOAT:	opcode = X_GLrop_Normal3fv; break;
    case GL_DOUBLE:	opcode = X_GLrop_Normal3dv; break;
    default:
        __glXSetError(gc, GL_INVALID_ENUM);
        return;
    }

    a = get_array_entry( arrays, GL_NORMAL_ARRAY, 0 );
    assert( a != NULL );
    COMMON_ARRAY_DATA_INIT( a, pointer, type, stride, 3, GL_TRUE, 4,
			    opcode );

    if ( a->enabled ) {
	arrays->array_info_cache_valid = GL_FALSE;
    }
}


void __indirect_glColorPointer( GLint size, GLenum type, GLsizei stride,
				const GLvoid * pointer )
{
    static const uint16_t byte_ops[5] = {
	0, 0, 0, X_GLrop_Color3bv, X_GLrop_Color4bv
    };
    static const uint16_t ubyte_ops[5] = {
	0, 0, 0, X_GLrop_Color3ubv, X_GLrop_Color4ubv
    };
    static const uint16_t short_ops[5] = {
	0, 0, 0, X_GLrop_Color3sv, X_GLrop_Color4sv
    };
    static const uint16_t ushort_ops[5] = {
	0, 0, 0, X_GLrop_Color3usv, X_GLrop_Color4usv
    };
    static const uint16_t int_ops[5] = {
	0, 0, 0, X_GLrop_Color3iv, X_GLrop_Color4iv
    };
    static const uint16_t uint_ops[5] = {
	0, 0, 0, X_GLrop_Color3uiv, X_GLrop_Color4uiv
    };
    static const uint16_t float_ops[5] = {
	0, 0, 0, X_GLrop_Color3fv, X_GLrop_Color4fv
    };
    static const uint16_t double_ops[5] = {
	0, 0, 0, X_GLrop_Color3dv, X_GLrop_Color4dv
    };
    uint16_t opcode;
    __GLXcontext *gc = __glXGetCurrentContext();
    __GLXattribute * state = (__GLXattribute *)(gc->client_state_private);
    struct array_state_vector * arrays = state->array_state;
    struct array_state * a;


    if (size < 3 || size > 4 || stride < 0) {
        __glXSetError(gc, GL_INVALID_VALUE);
        return;
    }
    
    switch ( type ) {
    case GL_BYTE:		opcode = byte_ops[size]; break;
    case GL_UNSIGNED_BYTE:	opcode = ubyte_ops[size]; break;
    case GL_SHORT:		opcode = short_ops[size]; break;
    case GL_UNSIGNED_SHORT:	opcode = ushort_ops[size]; break;
    case GL_INT:		opcode = int_ops[size]; break;
    case GL_UNSIGNED_INT:	opcode = uint_ops[size]; break;
    case GL_FLOAT:		opcode = float_ops[size]; break;
    case GL_DOUBLE:		opcode = double_ops[size]; break;
    default:
        __glXSetError(gc, GL_INVALID_ENUM);
        return;
    }

    a = get_array_entry( arrays, GL_COLOR_ARRAY, 0 );
    assert( a != NULL );
    COMMON_ARRAY_DATA_INIT( a, pointer, type, stride, size, GL_TRUE, 4,
			    opcode );

    if ( a->enabled ) {
	arrays->array_info_cache_valid = GL_FALSE;
    }
}


void __indirect_glIndexPointer( GLenum type, GLsizei stride,
				const GLvoid * pointer )
{
    uint16_t opcode;
    __GLXcontext *gc = __glXGetCurrentContext();
    __GLXattribute * state = (__GLXattribute *)(gc->client_state_private);
    struct array_state_vector * arrays = state->array_state;
    struct array_state * a;


    if (stride < 0) {
        __glXSetError(gc, GL_INVALID_VALUE);
        return;
    }
    
    switch ( type ) {
    case GL_UNSIGNED_BYTE:	opcode = X_GLrop_Indexubv; break;
    case GL_SHORT:		opcode = X_GLrop_Indexsv; break;
    case GL_INT:		opcode = X_GLrop_Indexiv; break;
    case GL_FLOAT:		opcode = X_GLrop_Indexfv; break;
    case GL_DOUBLE:		opcode = X_GLrop_Indexdv; break;
    default:
        __glXSetError(gc, GL_INVALID_ENUM);
        return;
    }

    a = get_array_entry( arrays, GL_INDEX_ARRAY, 0 );
    assert( a != NULL );
    COMMON_ARRAY_DATA_INIT( a, pointer, type, stride, 1, GL_FALSE, 4,
			    opcode );

    if ( a->enabled ) {
	arrays->array_info_cache_valid = GL_FALSE;
    }
}


void __indirect_glEdgeFlagPointer( GLsizei stride, const GLvoid * pointer )
{
    __GLXcontext *gc = __glXGetCurrentContext();
    __GLXattribute * state = (__GLXattribute *)(gc->client_state_private);
    struct array_state_vector * arrays = state->array_state;
    struct array_state * a;


    if (stride < 0) {
        __glXSetError(gc, GL_INVALID_VALUE);
        return;
    }
    

    a = get_array_entry( arrays, GL_EDGE_FLAG_ARRAY, 0 );
    assert( a != NULL );
    COMMON_ARRAY_DATA_INIT( a, pointer, GL_UNSIGNED_BYTE, stride, 1, GL_FALSE,
			    4, X_GLrop_EdgeFlagv );

    if ( a->enabled ) {
	arrays->array_info_cache_valid = GL_FALSE;
    }
}


void __indirect_glTexCoordPointer( GLint size, GLenum type, GLsizei stride,
				   const GLvoid * pointer )
{
    static const uint16_t short_ops[5] = {
	0, X_GLrop_TexCoord1sv, X_GLrop_TexCoord2sv, X_GLrop_TexCoord3sv, X_GLrop_TexCoord4sv
    };
    static const uint16_t int_ops[5] = {
	0, X_GLrop_TexCoord1iv, X_GLrop_TexCoord2iv, X_GLrop_TexCoord3iv, X_GLrop_TexCoord4iv
    };
    static const uint16_t float_ops[5] = {
	0, X_GLrop_TexCoord1dv, X_GLrop_TexCoord2fv, X_GLrop_TexCoord3fv, X_GLrop_TexCoord4fv
    };
    static const uint16_t double_ops[5] = {
	0, X_GLrop_TexCoord1dv, X_GLrop_TexCoord2dv, X_GLrop_TexCoord3dv, X_GLrop_TexCoord4dv
    };

    static const uint16_t mshort_ops[5] = {
	0, X_GLrop_MultiTexCoord1svARB, X_GLrop_MultiTexCoord2svARB, X_GLrop_MultiTexCoord3svARB, X_GLrop_MultiTexCoord4svARB
    };
    static const uint16_t mint_ops[5] = {
	0, X_GLrop_MultiTexCoord1ivARB, X_GLrop_MultiTexCoord2ivARB, X_GLrop_MultiTexCoord3ivARB, X_GLrop_MultiTexCoord4ivARB
    };
    static const uint16_t mfloat_ops[5] = {
	0, X_GLrop_MultiTexCoord1dvARB, X_GLrop_MultiTexCoord2fvARB, X_GLrop_MultiTexCoord3fvARB, X_GLrop_MultiTexCoord4fvARB
    };
    static const uint16_t mdouble_ops[5] = {
	0, X_GLrop_MultiTexCoord1dvARB, X_GLrop_MultiTexCoord2dvARB, X_GLrop_MultiTexCoord3dvARB, X_GLrop_MultiTexCoord4dvARB
    };

    uint16_t opcode;
    __GLXcontext *gc = __glXGetCurrentContext();
    __GLXattribute * state = (__GLXattribute *)(gc->client_state_private);
    struct array_state_vector * arrays = state->array_state;
    struct array_state * a;
    unsigned header_size;
    unsigned index;


    if (size < 1 || size > 4 || stride < 0) {
        __glXSetError(gc, GL_INVALID_VALUE);
        return;
    }
    
    index = arrays->active_texture_unit;
    if ( index == 0 ) {
	switch ( type ) {
	case GL_SHORT:		opcode = short_ops[size]; break;
	case GL_INT:		opcode = int_ops[size]; break;
	case GL_FLOAT:		opcode = float_ops[size]; break;
	case GL_DOUBLE:		opcode = double_ops[size]; break;
	default:
	    __glXSetError(gc, GL_INVALID_ENUM);
	    return;
	}

	header_size = 4;
    }
    else {
	switch ( type ) {
	case GL_SHORT:		opcode = mshort_ops[size]; break;
	case GL_INT:		opcode = mint_ops[size]; break;
	case GL_FLOAT:		opcode = mfloat_ops[size]; break;
	case GL_DOUBLE:		opcode = mdouble_ops[size]; break;
	default:
	    __glXSetError(gc, GL_INVALID_ENUM);
	    return;
	}

	header_size = 8;
    }

    a = get_array_entry( arrays, GL_TEXTURE_COORD_ARRAY, index );
    assert( a != NULL );
    COMMON_ARRAY_DATA_INIT( a, pointer, type, stride, size, GL_FALSE,
			    header_size, opcode );

    if ( a->enabled ) {
	arrays->array_info_cache_valid = GL_FALSE;
    }
}


void __indirect_glSecondaryColorPointerEXT( GLint size, GLenum type, GLsizei stride,
				const GLvoid * pointer )
{
    uint16_t opcode;
    __GLXcontext *gc = __glXGetCurrentContext();
    __GLXattribute * state = (__GLXattribute *)(gc->client_state_private);
    struct array_state_vector * arrays = state->array_state;
    struct array_state * a;


    if (size != 3 || stride < 0) {
        __glXSetError(gc, GL_INVALID_VALUE);
        return;
    }
    
    switch ( type ) {
    case GL_BYTE:		opcode = 4126; break;
    case GL_UNSIGNED_BYTE:	opcode = 4131; break;
    case GL_SHORT:		opcode = 4127; break;
    case GL_UNSIGNED_SHORT:	opcode = 4132; break;
    case GL_INT:		opcode = 4128; break;
    case GL_UNSIGNED_INT:	opcode = 4133; break;
    case GL_FLOAT:		opcode = 4129; break;
    case GL_DOUBLE:		opcode = 4130; break;
    default:
        __glXSetError(gc, GL_INVALID_ENUM);
        return;
    }

    a = get_array_entry( arrays, GL_SECONDARY_COLOR_ARRAY, 0 );
    if ( a == NULL ) {
        __glXSetError(gc, GL_INVALID_OPERATION);
        return;
    }

    COMMON_ARRAY_DATA_INIT( a, pointer, type, stride, size, GL_TRUE, 4,
			    opcode );

    if ( a->enabled ) {
	arrays->array_info_cache_valid = GL_FALSE;
    }
}


void __indirect_glFogCoordPointerEXT( GLenum type, GLsizei stride,
				const GLvoid * pointer )
{
    uint16_t opcode;
    __GLXcontext *gc = __glXGetCurrentContext();
    __GLXattribute * state = (__GLXattribute *)(gc->client_state_private);
    struct array_state_vector * arrays = state->array_state;
    struct array_state * a;


    if (stride < 0) {
        __glXSetError(gc, GL_INVALID_VALUE);
        return;
    }

    switch ( type ) {
    case GL_FLOAT:		opcode = 4124; break;
    case GL_DOUBLE:		opcode = 4125; break;
    default:
        __glXSetError(gc, GL_INVALID_ENUM);
        return;
    }

    a = get_array_entry( arrays, GL_FOG_COORD_ARRAY, 0 );
    if ( a == NULL ) {
        __glXSetError(gc, GL_INVALID_OPERATION);
        return;
    }

    COMMON_ARRAY_DATA_INIT( a, pointer, type, stride, 1, GL_FALSE, 4,
			    opcode );

    if ( a->enabled ) {
	arrays->array_info_cache_valid = GL_FALSE;
    }
}


void __indirect_glVertexAttribPointerARB(GLuint index, GLint size,
					 GLenum type, GLboolean normalized,
					 GLsizei stride,
					 const GLvoid * pointer)
{
    static const uint16_t short_ops[5]  = { 0, 4189, 4190, 4191, 4192 };
    static const uint16_t float_ops[5]  = { 0, 4193, 4194, 4195, 4196 };
    static const uint16_t double_ops[5] = { 0, 4197, 4198, 4199, 4200 };

    uint16_t opcode;
    __GLXcontext *gc = __glXGetCurrentContext();
    __GLXattribute * state = (__GLXattribute *)(gc->client_state_private);
    struct array_state_vector * arrays = state->array_state;
    struct array_state * a;
    unsigned true_immediate_count;
    unsigned true_immediate_size;


    if ( (size < 1) || (size > 4) || (stride < 0) 
	 || (index > arrays->num_vertex_program_attribs) ){
        __glXSetError(gc, GL_INVALID_VALUE);
        return;
    }

    if ( normalized && (type != GL_FLOAT) && (type != GL_DOUBLE)) {
	switch( type ) {
	case GL_BYTE:           opcode = X_GLrop_VertexAttrib4NbvARB;  break;
	case GL_UNSIGNED_BYTE:  opcode = X_GLrop_VertexAttrib4NubvARB; break;
	case GL_SHORT:          opcode = X_GLrop_VertexAttrib4NsvARB;  break;
	case GL_UNSIGNED_SHORT: opcode = X_GLrop_VertexAttrib4NusvARB; break;
	case GL_INT:            opcode = X_GLrop_VertexAttrib4NivARB;  break;
	case GL_UNSIGNED_INT:   opcode = X_GLrop_VertexAttrib4NuivARB; break;
	default:
	    __glXSetError(gc, GL_INVALID_ENUM);
	    return;
	}
	
	true_immediate_count = 4;
    }
    else {
	true_immediate_count = size;

	switch( type ) {
	case GL_BYTE:
	    opcode = X_GLrop_VertexAttrib4bvARB;
	    true_immediate_count = 4;
	    break;
	case GL_UNSIGNED_BYTE:
	    opcode = X_GLrop_VertexAttrib4ubvARB;
	    true_immediate_count = 4;
	    break;
	case GL_SHORT:
	    opcode = short_ops[size];
	    break;
	case GL_UNSIGNED_SHORT:
	    opcode = X_GLrop_VertexAttrib4usvARB;
	    true_immediate_count = 4;
	    break;
	case GL_INT:
            opcode = X_GLrop_VertexAttrib4ivARB;
	    true_immediate_count = 4;
	    break;
	case GL_UNSIGNED_INT:
	    opcode = X_GLrop_VertexAttrib4uivARB;
	    true_immediate_count = 4;
	    break;
	case GL_FLOAT:
	    opcode = float_ops[size];
	    break;
	case GL_DOUBLE:
	    opcode = double_ops[size];
	    break;
	default:
	    __glXSetError(gc, GL_INVALID_ENUM);
	    return;
	}
    }

    a = get_array_entry( arrays, GL_VERTEX_ATTRIB_ARRAY_POINTER, index );
    if ( a == NULL ) {
        __glXSetError(gc, GL_INVALID_OPERATION);
        return;
    }

    COMMON_ARRAY_DATA_INIT( a, pointer, type, stride, size, normalized, 8,
			    opcode );

    true_immediate_size = __glXTypeSize(type) * true_immediate_count;
    ((uint16_t *) (a)->header)[0] = __GLX_PAD(a->header_size 
					      + true_immediate_size);

    if ( a->enabled ) {
	arrays->array_info_cache_valid = GL_FALSE;
    }
}


/**
 * I don't have 100% confidence that this is correct.  The different rules
 * about whether or not generic vertex attributes alias "classic" vertex
 * attributes (i.e., attrib1 ?= primary color) between ARB_vertex_program,
 * ARB_vertex_shader, and NV_vertex_program are a bit confusing.  My
 * feeling is that the client-side doesn't have to worry about it.  The
 * client just sends all the data to the server and lets the server deal
 * with it.
 */
void __indirect_glVertexAttribPointerNV( GLuint index, GLint size,
					 GLenum type, GLsizei stride,
					 const GLvoid * pointer)
{
    __GLXcontext *gc = __glXGetCurrentContext();
    GLboolean normalized = GL_FALSE;


    switch( type ) {
    case GL_UNSIGNED_BYTE:
	if ( size != 4 ) {
	    __glXSetError(gc, GL_INVALID_VALUE);
	    return;
	}
	normalized = GL_TRUE;

    case GL_SHORT:
    case GL_FLOAT:
    case GL_DOUBLE:
	__indirect_glVertexAttribPointerARB(index, size, type,
					    normalized,
					    stride, pointer);
	return;
    default:
	__glXSetError(gc, GL_INVALID_ENUM);
	return;
    }
}


void __indirect_glClientActiveTextureARB(GLenum texture)
{
    __GLXcontext * const gc = __glXGetCurrentContext();
    __GLXattribute * const state = (__GLXattribute *)(gc->client_state_private);
    struct array_state_vector * const arrays = state->array_state;
    const GLint unit = (GLint) texture - GL_TEXTURE0;


    if ( (unit < 0) || (unit > arrays->num_texture_units) ) {
	__glXSetError(gc, GL_INVALID_ENUM);
	return;
    }

    arrays->active_texture_unit = unit;
}


/**
 */
GLboolean
__glXSetArrayEnable( __GLXattribute * state,
		     GLenum key, unsigned index, GLboolean enable )
{
    struct array_state_vector * arrays = state->array_state;
    struct array_state * a;
    

    if ( key == GL_TEXTURE_COORD_ARRAY ) {
	index = arrays->active_texture_unit;
    }

    a = get_array_entry( arrays, key, index );

    if ( (a != NULL) && (a->enabled != enable) ) {
	a->enabled = enable;
	arrays->array_info_cache_valid = GL_FALSE;
    }

    return (a != NULL);
}


void
__glXArrayDisableAll( __GLXattribute * state )
{
    struct array_state_vector * arrays = state->array_state;
    unsigned  i;


    for ( i = 0 ; i < arrays->num_arrays ; i++ ) {
	arrays->arrays[i].enabled = GL_FALSE;
    }

    arrays->array_info_cache_valid = GL_FALSE;
}


/**
 */
GLboolean
__glXGetArrayEnable( const __GLXattribute * const state,
		     GLenum key, unsigned index, GLintptr * dest )
{
    const struct array_state_vector * arrays = state->array_state;
    const struct array_state * a = get_array_entry( (struct array_state_vector *) arrays,
						    key, index );

    if ( a != NULL ) {
	*dest = (GLintptr) a->enabled;
    }

    return (a != NULL);
}


/**
 */
GLboolean
__glXGetArrayType( const __GLXattribute * const state,
		     GLenum key, unsigned index, GLintptr * dest )
{
    const struct array_state_vector * arrays = state->array_state;
    const struct array_state * a = get_array_entry( (struct array_state_vector *) arrays,
						    key, index );

    if ( a != NULL ) {
	*dest = (GLintptr) a->enabled;
    }

    return (a != NULL);
}


/**
 */
GLboolean
__glXGetArraySize( const __GLXattribute * const state,
		     GLenum key, unsigned index, GLintptr * dest )
{
    const struct array_state_vector * arrays = state->array_state;
    const struct array_state * a = get_array_entry( (struct array_state_vector *) arrays,
						    key, index );

    if ( a != NULL ) {
	*dest = (GLintptr) a->count;
    }

    return (a != NULL);
}


/**
 */
GLboolean
__glXGetArrayStride( const __GLXattribute * const state,
		     GLenum key, unsigned index, GLintptr * dest )
{
    const struct array_state_vector * arrays = state->array_state;
    const struct array_state * a = get_array_entry( (struct array_state_vector *) arrays,
						    key, index );

    if ( a != NULL ) {
	*dest = (GLintptr) a->user_stride;
    }

    return (a != NULL);
}


/**
 */
GLboolean
__glXGetArrayPointer( const __GLXattribute * const state,
		      GLenum key, unsigned index, void ** dest )
{
    const struct array_state_vector * arrays = state->array_state;
    const struct array_state * a = get_array_entry( (struct array_state_vector *) arrays,
						    key, index );


    if ( a != NULL ) {
	*dest = (void *) (a->data);
    }

    return (a != NULL);
}


/**
 */
GLboolean
__glXGetArrayNormalized( const __GLXattribute * const state,
			 GLenum key, unsigned index, GLintptr * dest )
{
    const struct array_state_vector * arrays = state->array_state;
    const struct array_state * a = get_array_entry( (struct array_state_vector *) arrays,
						    key, index );


    if ( a != NULL ) {
	*dest = (GLintptr) a->normalized;
    }

    return (a != NULL);
}


/**
 */
GLuint
__glXGetActiveTextureUnit( const __GLXattribute * const state )
{
    return state->array_state->active_texture_unit;
}


void
__glXPushArrayState( __GLXattribute * state )
{
    struct array_state_vector * arrays = state->array_state;
    struct array_stack_state * stack = & arrays->stack[ (arrays->stack_index * arrays->num_arrays)];
    unsigned  i;


    for ( i = 0 ; i < arrays->num_arrays ; i++ ) {
	stack[i].data = arrays->arrays[i].data;
	stack[i].data_type = arrays->arrays[i].data_type;
	stack[i].user_stride = arrays->arrays[i].user_stride;
	stack[i].count = arrays->arrays[i].count;
	stack[i].key = arrays->arrays[i].key;
	stack[i].enabled = arrays->arrays[i].enabled;
    }

    arrays->active_texture_unit_stack[ arrays->stack_index ] =
      arrays->active_texture_unit;

    arrays->stack_index++;
}


void
__glXPopArrayState( __GLXattribute * state )
{
    struct array_state_vector * arrays = state->array_state;
    struct array_stack_state * stack;
    unsigned  i;


    arrays->stack_index--;
    stack = & arrays->stack[ (arrays->stack_index * arrays->num_arrays) ];

    for ( i = 0 ; i < arrays->num_arrays ; i++ ) {
	switch ( stack[i].key ) {
	case GL_NORMAL_ARRAY:
	    __indirect_glNormalPointer( stack[i].data_type,
					stack[i].user_stride,
					stack[i].data );
	    break;
	case GL_COLOR_ARRAY:
	    __indirect_glColorPointer( stack[i].count,
				       stack[i].data_type,
				       stack[i].user_stride,
				       stack[i].data );
	    break;
	case GL_INDEX_ARRAY:
	    __indirect_glIndexPointer( stack[i].data_type,
				       stack[i].user_stride,
				       stack[i].data );
	    break;
	case GL_EDGE_FLAG_ARRAY:
	    __indirect_glEdgeFlagPointer( stack[i].user_stride,
					  stack[i].data );
	    break;
	case GL_TEXTURE_COORD_ARRAY:
	    arrays->active_texture_unit = stack[i].index;
	    __indirect_glTexCoordPointer( stack[i].count,
					  stack[i].data_type,
					  stack[i].user_stride,
					  stack[i].data );
	    break;
	case GL_SECONDARY_COLOR_ARRAY:
	    __indirect_glSecondaryColorPointerEXT( stack[i].count,
						   stack[i].data_type,
						   stack[i].user_stride,
						   stack[i].data );
	    break;
	case GL_FOG_COORDINATE_ARRAY:
	    __indirect_glFogCoordPointerEXT( stack[i].data_type,
					     stack[i].user_stride,
					     stack[i].data );
	    break;

	}
	
	__glXSetArrayEnable( state, stack[i].key, stack[i].index,
			     stack[i].enabled );
    }

    arrays->active_texture_unit = 
      arrays->active_texture_unit_stack[ arrays->stack_index ];
}