aboutsummaryrefslogtreecommitdiff
path: root/mesalib/src/mesa/main/texcompress_bptc.c
blob: 9204f123e251a1b8cecbc0781bca2ba06cff5cfb (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
/*
 * Copyright (C) 2014 Intel Corporation
 *
 * 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 AUTHORS OR COPYRIGHT HOLDERS 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.
 */

/**
 * \file texcompress_bptc.c
 * GL_ARB_texture_compression_bptc support.
 */

#include <stdbool.h>
#include "texcompress.h"
#include "texcompress_bptc.h"
#include "util/format_srgb.h"
#include "texstore.h"
#include "macros.h"
#include "image.h"

#define BLOCK_SIZE 4
#define N_PARTITIONS 64
#define BLOCK_BYTES 16

struct bptc_unorm_mode {
   int n_subsets;
   int n_partition_bits;
   bool has_rotation_bits;
   bool has_index_selection_bit;
   int n_color_bits;
   int n_alpha_bits;
   bool has_endpoint_pbits;
   bool has_shared_pbits;
   int n_index_bits;
   int n_secondary_index_bits;
};

struct bptc_float_bitfield {
   int8_t endpoint;
   uint8_t component;
   uint8_t offset;
   uint8_t n_bits;
   bool reverse;
};

struct bptc_float_mode {
   bool reserved;
   bool transformed_endpoints;
   int n_partition_bits;
   int n_endpoint_bits;
   int n_index_bits;
   int n_delta_bits[3];
   struct bptc_float_bitfield bitfields[24];
};

struct bit_writer {
   uint8_t buf;
   int pos;
   uint8_t *dst;
};

static const struct bptc_unorm_mode
bptc_unorm_modes[] = {
   /* 0 */ { 3, 4, false, false, 4, 0, true,  false, 3, 0 },
   /* 1 */ { 2, 6, false, false, 6, 0, false, true,  3, 0 },
   /* 2 */ { 3, 6, false, false, 5, 0, false, false, 2, 0 },
   /* 3 */ { 2, 6, false, false, 7, 0, true,  false, 2, 0 },
   /* 4 */ { 1, 0, true,  true,  5, 6, false, false, 2, 3 },
   /* 5 */ { 1, 0, true,  false, 7, 8, false, false, 2, 2 },
   /* 6 */ { 1, 0, false, false, 7, 7, true,  false, 4, 0 },
   /* 7 */ { 2, 6, false, false, 5, 5, true,  false, 2, 0 }
};

static const struct bptc_float_mode
bptc_float_modes[] = {
   /* 00 */
   { false, true, 5, 10, 3, { 5, 5, 5 },
     { { 2, 1, 4, 1, false }, { 2, 2, 4, 1, false }, { 3, 2, 4, 1, false },
       { 0, 0, 0, 10, false }, { 0, 1, 0, 10, false }, { 0, 2, 0, 10, false },
       { 1, 0, 0, 5, false }, { 3, 1, 4, 1, false }, { 2, 1, 0, 4, false },
       { 1, 1, 0, 5, false }, { 3, 2, 0, 1, false }, { 3, 1, 0, 4, false },
       { 1, 2, 0, 5, false }, { 3, 2, 1, 1, false }, { 2, 2, 0, 4, false },
       { 2, 0, 0, 5, false }, { 3, 2, 2, 1, false }, { 3, 0, 0, 5, false },
       { 3, 2, 3, 1, false },
       { -1 } }
   },
   /* 01 */
   { false, true, 5, 7, 3, { 6, 6, 6 },
     { { 2, 1, 5, 1, false }, { 3, 1, 4, 1, false }, { 3, 1, 5, 1, false },
       { 0, 0, 0, 7, false }, { 3, 2, 0, 1, false }, { 3, 2, 1, 1, false },
       { 2, 2, 4, 1, false }, { 0, 1, 0, 7, false }, { 2, 2, 5, 1, false },
       { 3, 2, 2, 1, false }, { 2, 1, 4, 1, false }, { 0, 2, 0, 7, false },
       { 3, 2, 3, 1, false }, { 3, 2, 5, 1, false }, { 3, 2, 4, 1, false },
       { 1, 0, 0, 6, false }, { 2, 1, 0, 4, false }, { 1, 1, 0, 6, false },
       { 3, 1, 0, 4, false }, { 1, 2, 0, 6, false }, { 2, 2, 0, 4, false },
       { 2, 0, 0, 6, false },
       { 3, 0, 0, 6, false },
       { -1 } }
   },
   /* 00010 */
   { false, true, 5, 11, 3, { 5, 4, 4 },
     { { 0, 0, 0, 10, false }, { 0, 1, 0, 10, false }, { 0, 2, 0, 10, false },
       { 1, 0, 0, 5, false }, { 0, 0, 10, 1, false }, { 2, 1, 0, 4, false },
       { 1, 1, 0, 4, false }, { 0, 1, 10, 1, false }, { 3, 2, 0, 1, false },
       { 3, 1, 0, 4, false }, { 1, 2, 0, 4, false }, { 0, 2, 10, 1, false },
       { 3, 2, 1, 1, false }, { 2, 2, 0, 4, false }, { 2, 0, 0, 5, false },
       { 3, 2, 2, 1, false }, { 3, 0, 0, 5, false }, { 3, 2, 3, 1, false },
       { -1 } }
   },
   /* 00011 */
   { false, false, 0, 10, 4, { 10, 10, 10 },
     { { 0, 0, 0, 10, false }, { 0, 1, 0, 10, false }, { 0, 2, 0, 10, false },
       { 1, 0, 0, 10, false }, { 1, 1, 0, 10, false }, { 1, 2, 0, 10, false },
       { -1 } }
   },
   /* 00110 */
   { false, true, 5, 11, 3, { 4, 5, 4 },
     { { 0, 0, 0, 10, false }, { 0, 1, 0, 10, false }, { 0, 2, 0, 10, false },
       { 1, 0, 0, 4, false }, { 0, 0, 10, 1, false }, { 3, 1, 4, 1, false },
       { 2, 1, 0, 4, false }, { 1, 1, 0, 5, false }, { 0, 1, 10, 1, false },
       { 3, 1, 0, 4, false }, { 1, 2, 0, 4, false }, { 0, 2, 10, 1, false },
       { 3, 2, 1, 1, false }, { 2, 2, 0, 4, false }, { 2, 0, 0, 4, false },
       { 3, 2, 0, 1, false }, { 3, 2, 2, 1, false }, { 3, 0, 0, 4, false },
       { 2, 1, 4, 1, false }, { 3, 2, 3, 1, false },
       { -1 } }
   },
   /* 00111 */
   { false, true, 0, 11, 4, { 9, 9, 9 },
     { { 0, 0, 0, 10, false }, { 0, 1, 0, 10, false }, { 0, 2, 0, 10, false },
       { 1, 0, 0, 9, false }, { 0, 0, 10, 1, false }, { 1, 1, 0, 9, false },
       { 0, 1, 10, 1, false }, { 1, 2, 0, 9, false }, { 0, 2, 10, 1, false },
       { -1 } }
   },
   /* 01010 */
   { false, true, 5, 11, 3, { 4, 4, 5 },
     { { 0, 0, 0, 10, false }, { 0, 1, 0, 10, false }, { 0, 2, 0, 10, false },
       { 1, 0, 0, 4, false }, { 0, 0, 10, 1, false }, { 2, 2, 4, 1, false },
       { 2, 1, 0, 4, false }, { 1, 1, 0, 4, false }, { 0, 1, 10, 1, false },
       { 3, 2, 0, 1, false }, { 3, 1, 0, 4, false }, { 1, 2, 0, 5, false },
       { 0, 2, 10, 1, false }, { 2, 2, 0, 4, false }, { 2, 0, 0, 4, false },
       { 3, 2, 1, 1, false }, { 3, 2, 2, 1, false }, { 3, 0, 0, 4, false },
       { 3, 2, 4, 1, false }, { 3, 2, 3, 1, false },
       { -1 } }
   },
   /* 01011 */
   { false, true, 0, 12, 4, { 8, 8, 8 },
     { { 0, 0, 0, 10, false }, { 0, 1, 0, 10, false }, { 0, 2, 0, 10, false },
       { 1, 0, 0, 8, false }, { 0, 0, 10, 2, true }, { 1, 1, 0, 8, false },
       { 0, 1, 10, 2, true }, { 1, 2, 0, 8, false }, { 0, 2, 10, 2, true },
       { -1 } }
   },
   /* 01110 */
   { false, true, 5, 9, 3, { 5, 5, 5 },
     { { 0, 0, 0, 9, false }, { 2, 2, 4, 1, false }, { 0, 1, 0, 9, false },
       { 2, 1, 4, 1, false }, { 0, 2, 0, 9, false }, { 3, 2, 4, 1, false },
       { 1, 0, 0, 5, false }, { 3, 1, 4, 1, false }, { 2, 1, 0, 4, false },
       { 1, 1, 0, 5, false }, { 3, 2, 0, 1, false }, { 3, 1, 0, 4, false },
       { 1, 2, 0, 5, false }, { 3, 2, 1, 1, false }, { 2, 2, 0, 4, false },
       { 2, 0, 0, 5, false }, { 3, 2, 2, 1, false }, { 3, 0, 0, 5, false },
       { 3, 2, 3, 1, false },
       { -1 } }
   },
   /* 01111 */
   { false, true, 0, 16, 4, { 4, 4, 4 },
     { { 0, 0, 0, 10, false }, { 0, 1, 0, 10, false }, { 0, 2, 0, 10, false },
       { 1, 0, 0, 4, false }, { 0, 0, 10, 6, true }, { 1, 1, 0, 4, false },
       { 0, 1, 10, 6, true }, { 1, 2, 0, 4, false }, { 0, 2, 10, 6, true },
       { -1 } }
   },
   /* 10010 */
   { false, true, 5, 8, 3, { 6, 5, 5 },
     { { 0, 0, 0, 8, false }, { 3, 1, 4, 1, false }, { 2, 2, 4, 1, false },
       { 0, 1, 0, 8, false }, { 3, 2, 2, 1, false }, { 2, 1, 4, 1, false },
       { 0, 2, 0, 8, false }, { 3, 2, 3, 1, false }, { 3, 2, 4, 1, false },
       { 1, 0, 0, 6, false }, { 2, 1, 0, 4, false }, { 1, 1, 0, 5, false },
       { 3, 2, 0, 1, false }, { 3, 1, 0, 4, false }, { 1, 2, 0, 5, false },
       { 3, 2, 1, 1, false }, { 2, 2, 0, 4, false }, { 2, 0, 0, 6, false },
       { 3, 0, 0, 6, false },
       { -1 } }
   },
   /* 10011 */
   { true /* reserved */ },
   /* 10110 */
   { false, true, 5, 8, 3, { 5, 6, 5 },
     { { 0, 0, 0, 8, false }, { 3, 2, 0, 1, false }, { 2, 2, 4, 1, false },
       { 0, 1, 0, 8, false }, { 2, 1, 5, 1, false }, { 2, 1, 4, 1, false },
       { 0, 2, 0, 8, false }, { 3, 1, 5, 1, false }, { 3, 2, 4, 1, false },
       { 1, 0, 0, 5, false }, { 3, 1, 4, 1, false }, { 2, 1, 0, 4, false },
       { 1, 1, 0, 6, false }, { 3, 1, 0, 4, false }, { 1, 2, 0, 5, false },
       { 3, 2, 1, 1, false }, { 2, 2, 0, 4, false }, { 2, 0, 0, 5, false },
       { 3, 2, 2, 1, false }, { 3, 0, 0, 5, false }, { 3, 2, 3, 1, false },
       { -1 } }
   },
   /* 10111 */
   { true /* reserved */ },
   /* 11010 */
   { false, true, 5, 8, 3, { 5, 5, 6 },
     { { 0, 0, 0, 8, false }, { 3, 2, 1, 1, false }, { 2, 2, 4, 1, false },
       { 0, 1, 0, 8, false }, { 2, 2, 5, 1, false }, { 2, 1, 4, 1, false },
       { 0, 2, 0, 8, false }, { 3, 2, 5, 1, false }, { 3, 2, 4, 1, false },
       { 1, 0, 0, 5, false }, { 3, 1, 4, 1, false }, { 2, 1, 0, 4, false },
       { 1, 1, 0, 5, false }, { 3, 2, 0, 1, false }, { 3, 1, 0, 4, false },
       { 1, 2, 0, 6, false }, { 2, 2, 0, 4, false }, { 2, 0, 0, 5, false },
       { 3, 2, 2, 1, false }, { 3, 0, 0, 5, false }, { 3, 2, 3, 1, false },
       { -1 } }
   },
   /* 11011 */
   { true /* reserved */ },
   /* 11110 */
   { false, false, 5, 6, 3, { 6, 6, 6 },
     { { 0, 0, 0, 6, false }, { 3, 1, 4, 1, false }, { 3, 2, 0, 1, false },
       { 3, 2, 1, 1, false }, { 2, 2, 4, 1, false }, { 0, 1, 0, 6, false },
       { 2, 1, 5, 1, false }, { 2, 2, 5, 1, false }, { 3, 2, 2, 1, false },
       { 2, 1, 4, 1, false }, { 0, 2, 0, 6, false }, { 3, 1, 5, 1, false },
       { 3, 2, 3, 1, false }, { 3, 2, 5, 1, false }, { 3, 2, 4, 1, false },
       { 1, 0, 0, 6, false }, { 2, 1, 0, 4, false }, { 1, 1, 0, 6, false },
       { 3, 1, 0, 4, false }, { 1, 2, 0, 6, false }, { 2, 2, 0, 4, false },
       { 2, 0, 0, 6, false }, { 3, 0, 0, 6, false },
       { -1 } }
   },
   /* 11111 */
   { true /* reserved */ },
};

/* This partition table is used when the mode has two subsets. Each
 * partition is represented by a 32-bit value which gives 2 bits per texel
 * within the block. The value of the two bits represents which subset to use
 * (0 or 1).
 */
static const uint32_t
partition_table1[N_PARTITIONS] = {
   0x50505050U, 0x40404040U, 0x54545454U, 0x54505040U,
   0x50404000U, 0x55545450U, 0x55545040U, 0x54504000U,
   0x50400000U, 0x55555450U, 0x55544000U, 0x54400000U,
   0x55555440U, 0x55550000U, 0x55555500U, 0x55000000U,
   0x55150100U, 0x00004054U, 0x15010000U, 0x00405054U,
   0x00004050U, 0x15050100U, 0x05010000U, 0x40505054U,
   0x00404050U, 0x05010100U, 0x14141414U, 0x05141450U,
   0x01155440U, 0x00555500U, 0x15014054U, 0x05414150U,
   0x44444444U, 0x55005500U, 0x11441144U, 0x05055050U,
   0x05500550U, 0x11114444U, 0x41144114U, 0x44111144U,
   0x15055054U, 0x01055040U, 0x05041050U, 0x05455150U,
   0x14414114U, 0x50050550U, 0x41411414U, 0x00141400U,
   0x00041504U, 0x00105410U, 0x10541000U, 0x04150400U,
   0x50410514U, 0x41051450U, 0x05415014U, 0x14054150U,
   0x41050514U, 0x41505014U, 0x40011554U, 0x54150140U,
   0x50505500U, 0x00555050U, 0x15151010U, 0x54540404U,
};

/* This partition table is used when the mode has three subsets. In this case
 * the values can be 0, 1 or 2.
 */
static const uint32_t
partition_table2[N_PARTITIONS] = {
   0xaa685050U, 0x6a5a5040U, 0x5a5a4200U, 0x5450a0a8U,
   0xa5a50000U, 0xa0a05050U, 0x5555a0a0U, 0x5a5a5050U,
   0xaa550000U, 0xaa555500U, 0xaaaa5500U, 0x90909090U,
   0x94949494U, 0xa4a4a4a4U, 0xa9a59450U, 0x2a0a4250U,
   0xa5945040U, 0x0a425054U, 0xa5a5a500U, 0x55a0a0a0U,
   0xa8a85454U, 0x6a6a4040U, 0xa4a45000U, 0x1a1a0500U,
   0x0050a4a4U, 0xaaa59090U, 0x14696914U, 0x69691400U,
   0xa08585a0U, 0xaa821414U, 0x50a4a450U, 0x6a5a0200U,
   0xa9a58000U, 0x5090a0a8U, 0xa8a09050U, 0x24242424U,
   0x00aa5500U, 0x24924924U, 0x24499224U, 0x50a50a50U,
   0x500aa550U, 0xaaaa4444U, 0x66660000U, 0xa5a0a5a0U,
   0x50a050a0U, 0x69286928U, 0x44aaaa44U, 0x66666600U,
   0xaa444444U, 0x54a854a8U, 0x95809580U, 0x96969600U,
   0xa85454a8U, 0x80959580U, 0xaa141414U, 0x96960000U,
   0xaaaa1414U, 0xa05050a0U, 0xa0a5a5a0U, 0x96000000U,
   0x40804080U, 0xa9a8a9a8U, 0xaaaaaa44U, 0x2a4a5254U
};

static const uint8_t
anchor_indices[][N_PARTITIONS] = {
   /* Anchor index values for the second subset of two-subset partitioning */
   {
      0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,
      0xf,0x2,0x8,0x2,0x2,0x8,0x8,0xf,0x2,0x8,0x2,0x2,0x8,0x8,0x2,0x2,
      0xf,0xf,0x6,0x8,0x2,0x8,0xf,0xf,0x2,0x8,0x2,0x2,0x2,0xf,0xf,0x6,
      0x6,0x2,0x6,0x8,0xf,0xf,0x2,0x2,0xf,0xf,0xf,0xf,0xf,0x2,0x2,0xf
   },

   /* Anchor index values for the second subset of three-subset partitioning */
   {
      0x3,0x3,0xf,0xf,0x8,0x3,0xf,0xf,0x8,0x8,0x6,0x6,0x6,0x5,0x3,0x3,
      0x3,0x3,0x8,0xf,0x3,0x3,0x6,0xa,0x5,0x8,0x8,0x6,0x8,0x5,0xf,0xf,
      0x8,0xf,0x3,0x5,0x6,0xa,0x8,0xf,0xf,0x3,0xf,0x5,0xf,0xf,0xf,0xf,
      0x3,0xf,0x5,0x5,0x5,0x8,0x5,0xa,0x5,0xa,0x8,0xd,0xf,0xc,0x3,0x3
   },

   /* Anchor index values for the third subset of three-subset
    * partitioning
    */
   {
      0xf,0x8,0x8,0x3,0xf,0xf,0x3,0x8,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0x8,
      0xf,0x8,0xf,0x3,0xf,0x8,0xf,0x8,0x3,0xf,0x6,0xa,0xf,0xf,0xa,0x8,
      0xf,0x3,0xf,0xa,0xa,0x8,0x9,0xa,0x6,0xf,0x8,0xf,0x3,0x6,0x6,0x8,
      0xf,0x3,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0xf,0x3,0xf,0xf,0x8
   }
};

static int
extract_bits(const uint8_t *block,
             int offset,
             int n_bits)
{
   int byte_index = offset / 8;
   int bit_index = offset % 8;
   int n_bits_in_byte = MIN2(n_bits, 8 - bit_index);
   int result = 0;
   int bit = 0;

   while (true) {
      result |= ((block[byte_index] >> bit_index) &
                 ((1 << n_bits_in_byte) - 1)) << bit;

      n_bits -= n_bits_in_byte;

      if (n_bits <= 0)
         return result;

      bit += n_bits_in_byte;
      byte_index++;
      bit_index = 0;
      n_bits_in_byte = MIN2(n_bits, 8);
   }
}

static uint8_t
expand_component(uint8_t byte,
                 int n_bits)
{
   /* Expands a n-bit quantity into a byte by copying the most-significant
    * bits into the unused least-significant bits.
    */
   return byte << (8 - n_bits) | (byte >> (2 * n_bits - 8));
}

static int
extract_unorm_endpoints(const struct bptc_unorm_mode *mode,
                        const uint8_t *block,
                        int bit_offset,
                        uint8_t endpoints[][4])
{
   int component;
   int subset;
   int endpoint;
   int pbit;
   int n_components;

   /* Extract each color component */
   for (component = 0; component < 3; component++) {
      for (subset = 0; subset < mode->n_subsets; subset++) {
         for (endpoint = 0; endpoint < 2; endpoint++) {
            endpoints[subset * 2 + endpoint][component] =
               extract_bits(block, bit_offset, mode->n_color_bits);
            bit_offset += mode->n_color_bits;
         }
      }
   }

   /* Extract the alpha values */
   if (mode->n_alpha_bits > 0) {
      for (subset = 0; subset < mode->n_subsets; subset++) {
         for (endpoint = 0; endpoint < 2; endpoint++) {
            endpoints[subset * 2 + endpoint][3] =
               extract_bits(block, bit_offset, mode->n_alpha_bits);
            bit_offset += mode->n_alpha_bits;
         }
      }

      n_components = 4;
   } else {
      for (subset = 0; subset < mode->n_subsets; subset++)
         for (endpoint = 0; endpoint < 2; endpoint++)
            endpoints[subset * 2 + endpoint][3] = 255;

      n_components = 3;
   }

   /* Add in the p-bits */
   if (mode->has_endpoint_pbits) {
      for (subset = 0; subset < mode->n_subsets; subset++) {
         for (endpoint = 0; endpoint < 2; endpoint++) {
            pbit = extract_bits(block, bit_offset, 1);
            bit_offset += 1;

            for (component = 0; component < n_components; component++) {
               endpoints[subset * 2 + endpoint][component] <<= 1;
               endpoints[subset * 2 + endpoint][component] |= pbit;
            }
         }
      }
   } else if (mode->has_shared_pbits) {
      for (subset = 0; subset < mode->n_subsets; subset++) {
         pbit = extract_bits(block, bit_offset, 1);
         bit_offset += 1;

         for (endpoint = 0; endpoint < 2; endpoint++) {
            for (component = 0; component < n_components; component++) {
               endpoints[subset * 2 + endpoint][component] <<= 1;
               endpoints[subset * 2 + endpoint][component] |= pbit;
            }
         }
      }
   }

   /* Expand the n-bit values to a byte */
   for (subset = 0; subset < mode->n_subsets; subset++) {
      for (endpoint = 0; endpoint < 2; endpoint++) {
         for (component = 0; component < 3; component++) {
            endpoints[subset * 2 + endpoint][component] =
               expand_component(endpoints[subset * 2 + endpoint][component],
                                mode->n_color_bits +
                                mode->has_endpoint_pbits +
                                mode->has_shared_pbits);
         }

         if (mode->n_alpha_bits > 0) {
            endpoints[subset * 2 + endpoint][3] =
               expand_component(endpoints[subset * 2 + endpoint][3],
                                mode->n_alpha_bits +
                                mode->has_endpoint_pbits +
                                mode->has_shared_pbits);
         }
      }
   }

   return bit_offset;
}

static bool
is_anchor(int n_subsets,
          int partition_num,
          int texel)
{
   if (texel == 0)
      return true;

   switch (n_subsets) {
   case 1:
      return false;
   case 2:
      return anchor_indices[0][partition_num] == texel;
   case 3:
      return (anchor_indices[1][partition_num] == texel ||
              anchor_indices[2][partition_num] == texel);
   default:
      assert(false);
      return false;
   }
}

static int
count_anchors_before_texel(int n_subsets,
                           int partition_num,
                           int texel)
{
   int count = 1;

   if (texel == 0)
      return 0;

   switch (n_subsets) {
   case 1:
      break;
   case 2:
      if (texel > anchor_indices[0][partition_num])
         count++;
      break;
   case 3:
      if (texel > anchor_indices[1][partition_num])
         count++;
      if (texel > anchor_indices[2][partition_num])
         count++;
      break;
   default:
      assert(false);
      return 0;
   }

   return count;
}

static int32_t
interpolate(int32_t a, int32_t b,
            int index,
            int index_bits)
{
   static const uint8_t weights2[] = { 0, 21, 43, 64 };
   static const uint8_t weights3[] = { 0, 9, 18, 27, 37, 46, 55, 64 };
   static const uint8_t weights4[] =
      { 0, 4, 9, 13, 17, 21, 26, 30, 34, 38, 43, 47, 51, 55, 60, 64 };
   static const uint8_t *weights[] = {
      NULL, NULL, weights2, weights3, weights4
   };
   int weight;

   weight = weights[index_bits][index];

   return ((64 - weight) * a + weight * b + 32) >> 6;
}

static void
apply_rotation(int rotation,
               uint8_t *result)
{
   uint8_t t;

   if (rotation == 0)
      return;

   rotation--;

   t = result[rotation];
   result[rotation] = result[3];
   result[3] = t;
}

static void
fetch_rgba_unorm_from_block(const uint8_t *block,
                            uint8_t *result,
                            int texel)
{
   int mode_num = ffs(block[0]);
   const struct bptc_unorm_mode *mode;
   int bit_offset, secondary_bit_offset;
   int partition_num;
   int subset_num;
   int rotation;
   int index_selection;
   int index_bits;
   int indices[2];
   int index;
   int anchors_before_texel;
   bool anchor;
   uint8_t endpoints[3 * 2][4];
   uint32_t subsets;
   int component;

   if (mode_num == 0) {
      /* According to the spec this mode is reserved and shouldn't be used. */
      memset(result, 0, 3);
      result[3] = 0xff;
      return;
   }

   mode = bptc_unorm_modes + mode_num - 1;
   bit_offset = mode_num;

   partition_num = extract_bits(block, bit_offset, mode->n_partition_bits);
   bit_offset += mode->n_partition_bits;

   switch (mode->n_subsets) {
   case 1:
      subsets = 0;
      break;
   case 2:
      subsets = partition_table1[partition_num];
      break;
   case 3:
      subsets = partition_table2[partition_num];
      break;
   default:
      assert(false);
      return;
   }

   if (mode->has_rotation_bits) {
      rotation = extract_bits(block, bit_offset, 2);
      bit_offset += 2;
   } else {
      rotation = 0;
   }

   if (mode->has_index_selection_bit) {
      index_selection = extract_bits(block, bit_offset, 1);
      bit_offset++;
   } else {
      index_selection = 0;
   }

   bit_offset = extract_unorm_endpoints(mode, block, bit_offset, endpoints);

   anchors_before_texel = count_anchors_before_texel(mode->n_subsets,
                                                     partition_num, texel);

   /* Calculate the offset to the secondary index */
   secondary_bit_offset = (bit_offset +
                           BLOCK_SIZE * BLOCK_SIZE * mode->n_index_bits -
                           mode->n_subsets +
                           mode->n_secondary_index_bits * texel -
                           anchors_before_texel);

   /* Calculate the offset to the primary index for this texel */
   bit_offset += mode->n_index_bits * texel - anchors_before_texel;

   subset_num = (subsets >> (texel * 2)) & 3;

   anchor = is_anchor(mode->n_subsets, partition_num, texel);

   index_bits = mode->n_index_bits;
   if (anchor)
      index_bits--;
   indices[0] = extract_bits(block, bit_offset, index_bits);

   if (mode->n_secondary_index_bits) {
      index_bits = mode->n_secondary_index_bits;
      if (anchor)
         index_bits--;
      indices[1] = extract_bits(block, secondary_bit_offset, index_bits);
   }

   index = indices[index_selection];
   index_bits = (index_selection ?
                 mode->n_secondary_index_bits :
                 mode->n_index_bits);

   for (component = 0; component < 3; component++)
      result[component] = interpolate(endpoints[subset_num * 2][component],
                                      endpoints[subset_num * 2 + 1][component],
                                      index,
                                      index_bits);

   /* Alpha uses the opposite index from the color components */
   if (mode->n_secondary_index_bits && !index_selection) {
      index = indices[1];
      index_bits = mode->n_secondary_index_bits;
   } else {
      index = indices[0];
      index_bits = mode->n_index_bits;
   }

   result[3] = interpolate(endpoints[subset_num * 2][3],
                           endpoints[subset_num * 2 + 1][3],
                           index,
                           index_bits);

   apply_rotation(rotation, result);
}

static void
fetch_bptc_rgba_unorm_bytes(const GLubyte *map,
                            GLint rowStride, GLint i, GLint j,
                            GLubyte *texel)
{
   const GLubyte *block;

   block = map + (((rowStride + 3) / 4) * (j / 4) + (i / 4)) * 16;

   fetch_rgba_unorm_from_block(block, texel, (i % 4) + (j % 4) * 4);
}

static void
fetch_bptc_rgba_unorm(const GLubyte *map,
                      GLint rowStride, GLint i, GLint j,
                      GLfloat *texel)
{
   GLubyte texel_bytes[4];

   fetch_bptc_rgba_unorm_bytes(map, rowStride, i, j, texel_bytes);

   texel[RCOMP] = UBYTE_TO_FLOAT(texel_bytes[0]);
   texel[GCOMP] = UBYTE_TO_FLOAT(texel_bytes[1]);
   texel[BCOMP] = UBYTE_TO_FLOAT(texel_bytes[2]);
   texel[ACOMP] = UBYTE_TO_FLOAT(texel_bytes[3]);
}

static void
fetch_bptc_srgb_alpha_unorm(const GLubyte *map,
                            GLint rowStride, GLint i, GLint j,
                            GLfloat *texel)
{
   GLubyte texel_bytes[4];

   fetch_bptc_rgba_unorm_bytes(map, rowStride, i, j, texel_bytes);

   texel[RCOMP] = util_format_srgb_8unorm_to_linear_float(texel_bytes[0]);
   texel[GCOMP] = util_format_srgb_8unorm_to_linear_float(texel_bytes[1]);
   texel[BCOMP] = util_format_srgb_8unorm_to_linear_float(texel_bytes[2]);
   texel[ACOMP] = UBYTE_TO_FLOAT(texel_bytes[3]);
}

static int32_t
sign_extend(int32_t value,
            int n_bits)
{
   if ((value & (1 << (n_bits - 1)))) {
      value |= (~(int32_t) 0) << n_bits;
   }

   return value;
}

static int
signed_unquantize(int value, int n_endpoint_bits)
{
   bool sign;

   if (n_endpoint_bits >= 16)
      return value;

   if (value == 0)
      return 0;

   sign = false;

   if (value < 0) {
      sign = true;
      value = -value;
   }

   if (value >= (1 << (n_endpoint_bits - 1)) - 1)
      value = 0x7fff;
   else
      value = ((value << 15) + 0x4000) >> (n_endpoint_bits - 1);

   if (sign)
      value = -value;

   return value;
}

static int
unsigned_unquantize(int value, int n_endpoint_bits)
{
   if (n_endpoint_bits >= 15)
      return value;

   if (value == 0)
      return 0;

   if (value == (1 << n_endpoint_bits) - 1)
      return 0xffff;

   return ((value << 15) + 0x4000) >> (n_endpoint_bits - 1);
}

static int
extract_float_endpoints(const struct bptc_float_mode *mode,
                        const uint8_t *block,
                        int bit_offset,
                        int32_t endpoints[][3],
                        bool is_signed)
{
   const struct bptc_float_bitfield *bitfield;
   int endpoint, component;
   int n_endpoints;
   int value;
   int i;

   if (mode->n_partition_bits)
      n_endpoints = 4;
   else
      n_endpoints = 2;

   memset(endpoints, 0, sizeof endpoints[0][0] * n_endpoints * 3);

   for (bitfield = mode->bitfields; bitfield->endpoint != -1; bitfield++) {
      value = extract_bits(block, bit_offset, bitfield->n_bits);
      bit_offset += bitfield->n_bits;

      if (bitfield->reverse) {
         for (i = 0; i < bitfield->n_bits; i++) {
            if (value & (1 << i))
               endpoints[bitfield->endpoint][bitfield->component] |=
                  1 << ((bitfield->n_bits - 1 - i) + bitfield->offset);
         }
      } else {
         endpoints[bitfield->endpoint][bitfield->component] |=
            value << bitfield->offset;
      }
   }

   if (mode->transformed_endpoints) {
      /* The endpoints are specified as signed offsets from e0 */
      for (endpoint = 1; endpoint < n_endpoints; endpoint++) {
         for (component = 0; component < 3; component++) {
            value = sign_extend(endpoints[endpoint][component],
                                mode->n_delta_bits[component]);
            endpoints[endpoint][component] =
               ((endpoints[0][component] + value) &
                ((1 << mode->n_endpoint_bits) - 1));
         }
      }
   }

   if (is_signed) {
      for (endpoint = 0; endpoint < n_endpoints; endpoint++) {
         for (component = 0; component < 3; component++) {
            value = sign_extend(endpoints[endpoint][component],
                                mode->n_endpoint_bits);
            endpoints[endpoint][component] =
               signed_unquantize(value, mode->n_endpoint_bits);
         }
      }
   } else {
      for (endpoint = 0; endpoint < n_endpoints; endpoint++) {
         for (component = 0; component < 3; component++) {
            endpoints[endpoint][component] =
               unsigned_unquantize(endpoints[endpoint][component],
                                   mode->n_endpoint_bits);
         }
      }
   }

   return bit_offset;
}

static int32_t
finish_unsigned_unquantize(int32_t value)
{
   return value * 31 / 64;
}

static int32_t
finish_signed_unquantize(int32_t value)
{
   if (value < 0)
      return (-value * 31 / 32) | 0x8000;
   else
      return value * 31 / 32;
}

static void
fetch_rgb_float_from_block(const uint8_t *block,
                           float *result,
                           int texel,
                           bool is_signed)
{
   int mode_num;
   const struct bptc_float_mode *mode;
   int bit_offset;
   int partition_num;
   int subset_num;
   int index_bits;
   int index;
   int anchors_before_texel;
   int32_t endpoints[2 * 2][3];
   uint32_t subsets;
   int n_subsets;
   int component;
   int32_t value;

   if (block[0] & 0x2) {
      mode_num = (((block[0] >> 1) & 0xe) | (block[0] & 1)) + 2;
      bit_offset = 5;
   } else {
      mode_num = block[0] & 3;
      bit_offset = 2;
   }

   mode = bptc_float_modes + mode_num;

   if (mode->reserved) {
      memset(result, 0, sizeof result[0] * 3);
      result[3] = 1.0f;
      return;
   }

   bit_offset = extract_float_endpoints(mode, block, bit_offset,
                                        endpoints, is_signed);

   if (mode->n_partition_bits) {
      partition_num = extract_bits(block, bit_offset, mode->n_partition_bits);
      bit_offset += mode->n_partition_bits;

      subsets = partition_table1[partition_num];
      n_subsets = 2;
   } else {
      partition_num = 0;
      subsets = 0;
      n_subsets = 1;
   }

   anchors_before_texel =
      count_anchors_before_texel(n_subsets, partition_num, texel);

   /* Calculate the offset to the primary index for this texel */
   bit_offset += mode->n_index_bits * texel - anchors_before_texel;

   subset_num = (subsets >> (texel * 2)) & 3;

   index_bits = mode->n_index_bits;
   if (is_anchor(n_subsets, partition_num, texel))
      index_bits--;
   index = extract_bits(block, bit_offset, index_bits);

   for (component = 0; component < 3; component++) {
      value = interpolate(endpoints[subset_num * 2][component],
                          endpoints[subset_num * 2 + 1][component],
                          index,
                          mode->n_index_bits);

      if (is_signed)
         value = finish_signed_unquantize(value);
      else
         value = finish_unsigned_unquantize(value);

      result[component] = _mesa_half_to_float(value);
   }

   result[3] = 1.0f;
}

static void
fetch_bptc_rgb_float(const GLubyte *map,
                     GLint rowStride, GLint i, GLint j,
                     GLfloat *texel,
                     bool is_signed)
{
   const GLubyte *block;

   block = map + (((rowStride + 3) / 4) * (j / 4) + (i / 4)) * 16;

   fetch_rgb_float_from_block(block, texel, (i % 4) + (j % 4) * 4, is_signed);
}

static void
fetch_bptc_rgb_signed_float(const GLubyte *map,
                            GLint rowStride, GLint i, GLint j,
                            GLfloat *texel)
{
   fetch_bptc_rgb_float(map, rowStride, i, j, texel, true);
}

static void
fetch_bptc_rgb_unsigned_float(const GLubyte *map,
                              GLint rowStride, GLint i, GLint j,
                              GLfloat *texel)
{
   fetch_bptc_rgb_float(map, rowStride, i, j, texel, false);
}

compressed_fetch_func
_mesa_get_bptc_fetch_func(mesa_format format)
{
   switch (format) {
   case MESA_FORMAT_BPTC_RGBA_UNORM:
      return fetch_bptc_rgba_unorm;
   case MESA_FORMAT_BPTC_SRGB_ALPHA_UNORM:
      return fetch_bptc_srgb_alpha_unorm;
   case MESA_FORMAT_BPTC_RGB_SIGNED_FLOAT:
      return fetch_bptc_rgb_signed_float;
   case MESA_FORMAT_BPTC_RGB_UNSIGNED_FLOAT:
      return fetch_bptc_rgb_unsigned_float;
   default:
      return NULL;
   }
}

static void
write_bits(struct bit_writer *writer, int n_bits, int value)
{
   do {
      if (n_bits + writer->pos >= 8) {
         *(writer->dst++) = writer->buf | (value << writer->pos);
         writer->buf = 0;
         value >>= (8 - writer->pos);
         n_bits -= (8 - writer->pos);
         writer->pos = 0;
      } else {
         writer->buf |= value << writer->pos;
         writer->pos += n_bits;
         break;
      }
   } while (n_bits > 0);
}

static void
get_average_luminance_alpha_unorm(int width, int height,
                                  const uint8_t *src, int src_rowstride,
                                  int *average_luminance, int *average_alpha)
{
   int luminance_sum = 0, alpha_sum = 0;
   int y, x;

   for (y = 0; y < height; y++) {
      for (x = 0; x < width; x++) {
         luminance_sum += src[0] + src[1] + src[2];
         alpha_sum += src[3];
         src += 4;
      }
      src += src_rowstride - width * 4;
   }

   *average_luminance = luminance_sum / (width * height);
   *average_alpha = alpha_sum / (width * height);
}

static void
get_rgba_endpoints_unorm(int width, int height,
                         const uint8_t *src, int src_rowstride,
                         int average_luminance, int average_alpha,
                         uint8_t endpoints[][4])
{
   int endpoint_luminances[2];
   int midpoint;
   int sums[2][4];
   int endpoint;
   int luminance;
   uint8_t temp[3];
   const uint8_t *p = src;
   int rgb_left_endpoint_count = 0;
   int alpha_left_endpoint_count = 0;
   int y, x, i;

   memset(sums, 0, sizeof sums);

   for (y = 0; y < height; y++) {
      for (x = 0; x < width; x++) {
         luminance = p[0] + p[1] + p[2];
         if (luminance < average_luminance) {
            endpoint = 0;
            rgb_left_endpoint_count++;
         } else {
            endpoint = 1;
         }
         for (i = 0; i < 3; i++)
            sums[endpoint][i] += p[i];

         if (p[2] < average_alpha) {
            endpoint = 0;
            alpha_left_endpoint_count++;
         } else {
            endpoint = 1;
         }
         sums[endpoint][3] += p[3];

         p += 4;
      }

      p += src_rowstride - width * 4;
   }

   if (rgb_left_endpoint_count == 0 ||
       rgb_left_endpoint_count == width * height) {
      for (i = 0; i < 3; i++)
         endpoints[0][i] = endpoints[1][i] =
            (sums[0][i] + sums[1][i]) / (width * height);
   } else {
      for (i = 0; i < 3; i++) {
         endpoints[0][i] = sums[0][i] / rgb_left_endpoint_count;
         endpoints[1][i] = (sums[1][i] /
                            (width * height - rgb_left_endpoint_count));
      }
   }

   if (alpha_left_endpoint_count == 0 ||
       alpha_left_endpoint_count == width * height) {
      endpoints[0][3] = endpoints[1][3] =
         (sums[0][3] + sums[1][3]) / (width * height);
   } else {
         endpoints[0][3] = sums[0][3] / alpha_left_endpoint_count;
         endpoints[1][3] = (sums[1][3] /
                            (width * height - alpha_left_endpoint_count));
   }

   /* We may need to swap the endpoints to ensure the most-significant bit of
    * the first index is zero */

   for (endpoint = 0; endpoint < 2; endpoint++) {
      endpoint_luminances[endpoint] =
         endpoints[endpoint][0] +
         endpoints[endpoint][1] +
         endpoints[endpoint][2];
   }
   midpoint = (endpoint_luminances[0] + endpoint_luminances[1]) / 2;

   if ((src[0] + src[1] + src[2] <= midpoint) !=
       (endpoint_luminances[0] <= midpoint)) {
      memcpy(temp, endpoints[0], 3);
      memcpy(endpoints[0], endpoints[1], 3);
      memcpy(endpoints[1], temp, 3);
   }

   /* Same for the alpha endpoints */

   midpoint = (endpoints[0][3] + endpoints[1][3]) / 2;

   if ((src[3] <= midpoint) != (endpoints[0][3] <= midpoint)) {
      temp[0] = endpoints[0][3];
      endpoints[0][3] = endpoints[1][3];
      endpoints[1][3] = temp[0];
   }
}

static void
write_rgb_indices_unorm(struct bit_writer *writer,
                        int src_width, int src_height,
                        const uint8_t *src, int src_rowstride,
                        uint8_t endpoints[][4])
{
   int luminance;
   int endpoint_luminances[2];
   int endpoint;
   int index;
   int y, x;

   for (endpoint = 0; endpoint < 2; endpoint++) {
      endpoint_luminances[endpoint] =
         endpoints[endpoint][0] +
         endpoints[endpoint][1] +
         endpoints[endpoint][2];
   }

   /* If the endpoints have the same luminance then we'll just use index 0 for
    * all of the texels */
   if (endpoint_luminances[0] == endpoint_luminances[1]) {
      write_bits(writer, BLOCK_SIZE * BLOCK_SIZE * 2 - 1, 0);
      return;
   }

   for (y = 0; y < src_height; y++) {
      for (x = 0; x < src_width; x++) {
         luminance = src[0] + src[1] + src[2];

         index = ((luminance - endpoint_luminances[0]) * 3 /
                  (endpoint_luminances[1] - endpoint_luminances[0]));
         if (index < 0)
            index = 0;
         else if (index > 3)
            index = 3;

         assert(x != 0 || y != 0 || index < 2);

         write_bits(writer, (x == 0 && y == 0) ? 1 : 2, index);

         src += 4;
      }

      /* Pad the indices out to the block size */
      if (src_width < BLOCK_SIZE)
         write_bits(writer, 2 * (BLOCK_SIZE - src_width), 0);

      src += src_rowstride - src_width * 4;
   }

   /* Pad the indices out to the block size */
   if (src_height < BLOCK_SIZE)
      write_bits(writer, 2 * BLOCK_SIZE * (BLOCK_SIZE - src_height), 0);
}

static void
write_alpha_indices_unorm(struct bit_writer *writer,
                          int src_width, int src_height,
                          const uint8_t *src, int src_rowstride,
                          uint8_t endpoints[][4])
{
   int index;
   int y, x;

   /* If the endpoints have the same alpha then we'll just use index 0 for
    * all of the texels */
   if (endpoints[0][3] == endpoints[1][3]) {
      write_bits(writer, BLOCK_SIZE * BLOCK_SIZE * 3 - 1, 0);
      return;
   }

   for (y = 0; y < src_height; y++) {
      for (x = 0; x < src_width; x++) {
         index = (((int) src[3] - (int) endpoints[0][3]) * 7 /
                  ((int) endpoints[1][3] - endpoints[0][3]));
         if (index < 0)
            index = 0;
         else if (index > 7)
            index = 7;

         assert(x != 0 || y != 0 || index < 4);

         /* The first index has one less bit */
         write_bits(writer, (x == 0 && y == 0) ? 2 : 3, index);

         src += 4;
      }

      /* Pad the indices out to the block size */
      if (src_width < BLOCK_SIZE)
         write_bits(writer, 3 * (BLOCK_SIZE - src_width), 0);

      src += src_rowstride - src_width * 4;
   }

   /* Pad the indices out to the block size */
   if (src_height < BLOCK_SIZE)
      write_bits(writer, 3 * BLOCK_SIZE * (BLOCK_SIZE - src_height), 0);
}

static void
compress_rgba_unorm_block(int src_width, int src_height,
                          const uint8_t *src, int src_rowstride,
                          uint8_t *dst)
{
   int average_luminance, average_alpha;
   uint8_t endpoints[2][4];
   struct bit_writer writer;
   int component, endpoint;

   get_average_luminance_alpha_unorm(src_width, src_height, src, src_rowstride,
                                     &average_luminance, &average_alpha);
   get_rgba_endpoints_unorm(src_width, src_height, src, src_rowstride,
                            average_luminance, average_alpha,
                            endpoints);

   writer.dst = dst;
   writer.pos = 0;
   writer.buf = 0;

   write_bits(&writer, 5, 0x10); /* mode 4 */
   write_bits(&writer, 2, 0); /* rotation 0 */
   write_bits(&writer, 1, 0); /* index selection bit */

   /* Write the color endpoints */
   for (component = 0; component < 3; component++)
      for (endpoint = 0; endpoint < 2; endpoint++)
         write_bits(&writer, 5, endpoints[endpoint][component] >> 3);

   /* Write the alpha endpoints */
   for (endpoint = 0; endpoint < 2; endpoint++)
      write_bits(&writer, 6, endpoints[endpoint][3] >> 2);

   write_rgb_indices_unorm(&writer,
                           src_width, src_height,
                           src, src_rowstride,
                           endpoints);
   write_alpha_indices_unorm(&writer,
                             src_width, src_height,
                             src, src_rowstride,
                             endpoints);
}

static void
compress_rgba_unorm(int width, int height,
                    const uint8_t *src, int src_rowstride,
                    uint8_t *dst, int dst_rowstride)
{
   int dst_row_diff;
   int y, x;

   if (dst_rowstride >= width * 4)
      dst_row_diff = dst_rowstride - ((width + 3) & ~3) * 4;
   else
      dst_row_diff = 0;

   for (y = 0; y < height; y += BLOCK_SIZE) {
      for (x = 0; x < width; x += BLOCK_SIZE) {
         compress_rgba_unorm_block(MIN2(width - x, BLOCK_SIZE),
                                   MIN2(height - y, BLOCK_SIZE),
                                   src + x * 4 + y * src_rowstride,
                                   src_rowstride,
                                   dst);
         dst += BLOCK_BYTES;
      }
      dst += dst_row_diff;
   }
}

GLboolean
_mesa_texstore_bptc_rgba_unorm(TEXSTORE_PARAMS)
{
   const GLubyte *pixels;
   const GLubyte *tempImage = NULL;
   GLenum baseFormat;
   int rowstride;

   if (srcFormat != GL_RGBA ||
       srcType != GL_UNSIGNED_BYTE ||
       ctx->_ImageTransferState ||
       srcPacking->SwapBytes) {
      /* convert image to RGBA/ubyte */
      baseFormat = _mesa_get_format_base_format(dstFormat);
      tempImage = _mesa_make_temp_ubyte_image(ctx, dims,
                                              baseInternalFormat,
                                              baseFormat,
                                              srcWidth, srcHeight, srcDepth,
                                              srcFormat, srcType, srcAddr,
                                              srcPacking);
      if (!tempImage)
         return GL_FALSE; /* out of memory */

      pixels = tempImage;
      rowstride = srcWidth * 4;
   } else {
      pixels = _mesa_image_address2d(srcPacking, srcAddr, srcWidth, srcHeight,
                                     srcFormat, srcType, 0, 0);
      rowstride = _mesa_image_row_stride(srcPacking, srcWidth,
                                         srcFormat, srcType);
   }

   compress_rgba_unorm(srcWidth, srcHeight,
                       pixels, rowstride,
                       dstSlices[0], dstRowStride);

   free((void *) tempImage);

   return GL_TRUE;
}

static float
get_average_luminance_float(int width, int height,
                            const float *src, int src_rowstride)
{
   float luminance_sum = 0;
   int y, x;

   for (y = 0; y < height; y++) {
      for (x = 0; x < width; x++) {
         luminance_sum += src[0] + src[1] + src[2];
         src += 3;
      }
      src += (src_rowstride - width * 3 * sizeof (float)) / sizeof (float);
   }

   return luminance_sum / (width * height);
}

static float
clamp_value(float value, bool is_signed)
{
   if (value > 65504.0f)
      return 65504.0f;

   if (is_signed) {
      if (value < -65504.0f)
         return -65504.0f;
      else
         return value;
   }

   if (value < 0.0f)
      return 0.0f;

   return value;
}

static void
get_endpoints_float(int width, int height,
                    const float *src, int src_rowstride,
                    float average_luminance, float endpoints[][3],
                    bool is_signed)
{
   float endpoint_luminances[2];
   float midpoint;
   float sums[2][3];
   int endpoint, component;
   float luminance;
   float temp[3];
   const float *p = src;
   int left_endpoint_count = 0;
   int y, x, i;

   memset(sums, 0, sizeof sums);

   for (y = 0; y < height; y++) {
      for (x = 0; x < width; x++) {
         luminance = p[0] + p[1] + p[2];
         if (luminance < average_luminance) {
            endpoint = 0;
            left_endpoint_count++;
         } else {
            endpoint = 1;
         }
         for (i = 0; i < 3; i++)
            sums[endpoint][i] += p[i];

         p += 3;
      }

      p += (src_rowstride - width * 3 * sizeof (float)) / sizeof (float);
   }

   if (left_endpoint_count == 0 ||
       left_endpoint_count == width * height) {
      for (i = 0; i < 3; i++)
         endpoints[0][i] = endpoints[1][i] =
            (sums[0][i] + sums[1][i]) / (width * height);
   } else {
      for (i = 0; i < 3; i++) {
         endpoints[0][i] = sums[0][i] / left_endpoint_count;
         endpoints[1][i] = sums[1][i] / (width * height - left_endpoint_count);
      }
   }

   /* Clamp the endpoints to the range of a half float and strip out
    * infinities */
   for (endpoint = 0; endpoint < 2; endpoint++) {
      for (component = 0; component < 3; component++) {
         endpoints[endpoint][component] =
            clamp_value(endpoints[endpoint][component], is_signed);
      }
   }

   /* We may need to swap the endpoints to ensure the most-significant bit of
    * the first index is zero */

   for (endpoint = 0; endpoint < 2; endpoint++) {
      endpoint_luminances[endpoint] =
         endpoints[endpoint][0] +
         endpoints[endpoint][1] +
         endpoints[endpoint][2];
   }
   midpoint = (endpoint_luminances[0] + endpoint_luminances[1]) / 2.0f;

   if ((src[0] + src[1] + src[2] <= midpoint) !=
       (endpoint_luminances[0] <= midpoint)) {
      memcpy(temp, endpoints[0], sizeof temp);
      memcpy(endpoints[0], endpoints[1], sizeof temp);
      memcpy(endpoints[1], temp, sizeof temp);
   }
}

static void
write_rgb_indices_float(struct bit_writer *writer,
                        int src_width, int src_height,
                        const float *src, int src_rowstride,
                        float endpoints[][3])
{
   float luminance;
   float endpoint_luminances[2];
   int endpoint;
   int index;
   int y, x;

   for (endpoint = 0; endpoint < 2; endpoint++) {
      endpoint_luminances[endpoint] =
         endpoints[endpoint][0] +
         endpoints[endpoint][1] +
         endpoints[endpoint][2];
   }

   /* If the endpoints have the same luminance then we'll just use index 0 for
    * all of the texels */
   if (endpoint_luminances[0] == endpoint_luminances[1]) {
      write_bits(writer, BLOCK_SIZE * BLOCK_SIZE * 4 - 1, 0);
      return;
   }

   for (y = 0; y < src_height; y++) {
      for (x = 0; x < src_width; x++) {
         luminance = src[0] + src[1] + src[2];

         index = ((luminance - endpoint_luminances[0]) * 15 /
                  (endpoint_luminances[1] - endpoint_luminances[0]));
         if (index < 0)
            index = 0;
         else if (index > 15)
            index = 15;

         assert(x != 0 || y != 0 || index < 8);

         write_bits(writer, (x == 0 && y == 0) ? 3 : 4, index);

         src += 3;
      }

      /* Pad the indices out to the block size */
      if (src_width < BLOCK_SIZE)
         write_bits(writer, 4 * (BLOCK_SIZE - src_width), 0);

      src += (src_rowstride - src_width * 3 * sizeof (float)) / sizeof (float);
   }

   /* Pad the indices out to the block size */
   if (src_height < BLOCK_SIZE)
      write_bits(writer, 4 * BLOCK_SIZE * (BLOCK_SIZE - src_height), 0);
}

static int
get_endpoint_value(float value, bool is_signed)
{
   bool sign = false;
   int half;

   if (is_signed) {
      half = _mesa_float_to_half(value);

      if (half & 0x8000) {
         half &= 0x7fff;
         sign = true;
      }

      half = (32 * half / 31) >> 6;

      if (sign)
         half = -half & ((1 << 10) - 1);

      return half;
   } else {
      if (value <= 0.0f)
         return 0;

      half = _mesa_float_to_half(value);

      return (64 * half / 31) >> 6;
   }
}

static void
compress_rgb_float_block(int src_width, int src_height,
                         const float *src, int src_rowstride,
                         uint8_t *dst,
                         bool is_signed)
{
   float average_luminance;
   float endpoints[2][3];
   struct bit_writer writer;
   int component, endpoint;
   int endpoint_value;

   average_luminance =
      get_average_luminance_float(src_width, src_height, src, src_rowstride);
   get_endpoints_float(src_width, src_height, src, src_rowstride,
                       average_luminance, endpoints, is_signed);

   writer.dst = dst;
   writer.pos = 0;
   writer.buf = 0;

   write_bits(&writer, 5, 3); /* mode 3 */

   /* Write the endpoints */
   for (endpoint = 0; endpoint < 2; endpoint++) {
      for (component = 0; component < 3; component++) {
         endpoint_value =
            get_endpoint_value(endpoints[endpoint][component], is_signed);
         write_bits(&writer, 10, endpoint_value);
      }
   }

   write_rgb_indices_float(&writer,
                           src_width, src_height,
                           src, src_rowstride,
                           endpoints);
}

static void
compress_rgb_float(int width, int height,
                   const float *src, int src_rowstride,
                   uint8_t *dst, int dst_rowstride,
                   bool is_signed)
{
   int dst_row_diff;
   int y, x;

   if (dst_rowstride >= width * 4)
      dst_row_diff = dst_rowstride - ((width + 3) & ~3) * 4;
   else
      dst_row_diff = 0;

   for (y = 0; y < height; y += BLOCK_SIZE) {
      for (x = 0; x < width; x += BLOCK_SIZE) {
         compress_rgb_float_block(MIN2(width - x, BLOCK_SIZE),
                                  MIN2(height - y, BLOCK_SIZE),
                                  src + x * 3 +
                                  y * src_rowstride / sizeof (float),
                                  src_rowstride,
                                  dst,
                                  is_signed);
         dst += BLOCK_BYTES;
      }
      dst += dst_row_diff;
   }
}

static GLboolean
texstore_bptc_rgb_float(TEXSTORE_PARAMS,
                        bool is_signed)
{
   const float *pixels;
   const float *tempImage = NULL;
   GLenum baseFormat;
   int rowstride;

   if (srcFormat != GL_RGB ||
       srcType != GL_FLOAT ||
       ctx->_ImageTransferState ||
       srcPacking->SwapBytes) {
      /* convert image to RGB/float */
      baseFormat = _mesa_get_format_base_format(dstFormat);
      tempImage = _mesa_make_temp_float_image(ctx, dims,
                                              baseInternalFormat,
                                              baseFormat,
                                              srcWidth, srcHeight, srcDepth,
                                              srcFormat, srcType, srcAddr,
                                              srcPacking,
                                              ctx->_ImageTransferState);
      if (!tempImage)
         return GL_FALSE; /* out of memory */

      pixels = tempImage;
      rowstride = srcWidth * sizeof(float) * 3;
   } else {
      pixels = _mesa_image_address2d(srcPacking, srcAddr, srcWidth, srcHeight,
                                     srcFormat, srcType, 0, 0);
      rowstride = _mesa_image_row_stride(srcPacking, srcWidth,
                                         srcFormat, srcType);
   }

   compress_rgb_float(srcWidth, srcHeight,
                      pixels, rowstride,
                      dstSlices[0], dstRowStride,
                      is_signed);

   free((void *) tempImage);

   return GL_TRUE;
}

GLboolean
_mesa_texstore_bptc_rgb_signed_float(TEXSTORE_PARAMS)
{
   ASSERT(dstFormat == MESA_FORMAT_BPTC_RGB_SIGNED_FLOAT);

   return texstore_bptc_rgb_float(ctx, dims, baseInternalFormat,
                                  dstFormat, dstRowStride, dstSlices,
                                  srcWidth, srcHeight, srcDepth,
                                  srcFormat, srcType,
                                  srcAddr, srcPacking,
                                  true /* signed */);
}

GLboolean
_mesa_texstore_bptc_rgb_unsigned_float(TEXSTORE_PARAMS)
{
   ASSERT(dstFormat == MESA_FORMAT_BPTC_RGB_UNSIGNED_FLOAT);

   return texstore_bptc_rgb_float(ctx, dims, baseInternalFormat,
                                  dstFormat, dstRowStride, dstSlices,
                                  srcWidth, srcHeight, srcDepth,
                                  srcFormat, srcType,
                                  srcAddr, srcPacking,
                                  false /* unsigned */);
}