-
Notifications
You must be signed in to change notification settings - Fork 333
Expand file tree
/
Copy pathbasisu_astc_helpers.h
More file actions
4983 lines (4073 loc) · 148 KB
/
Copy pathbasisu_astc_helpers.h
File metadata and controls
4983 lines (4073 loc) · 148 KB
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
// basisu_astc_helpers.h
// Be sure to define ASTC_HELPERS_IMPLEMENTATION somewhere to get the implementation, otherwise you only get the header.
#ifndef BASISU_ASTC_HELPERS_HEADER
#define BASISU_ASTC_HELPERS_HEADER
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include <fenv.h>
namespace astc_helpers
{
const uint32_t MIN_GRID_DIM = 2; // the minimum dimension of a block's weight grid
const uint32_t MIN_BLOCK_DIM = 4, MAX_BLOCK_DIM = 12; // the valid block dimensions in texels
const uint32_t MAX_BLOCK_PIXELS = MAX_BLOCK_DIM * MAX_BLOCK_DIM;
const uint32_t MAX_GRID_WEIGHTS = 64; // a block may have a maximum of 64 weight grid values
const uint32_t MAX_CEM_ENDPOINT_VALS = 8; // see Table 94. ASTC LDR/HDR color endpoint modes (max 8 values to encode any CEM, minimum 2)
// The number of BISE values needed to encode endpoints for each CEM.
const uint32_t NUM_MODE0_ENDPOINTS = 2, NUM_MODE4_ENDPOINTS = 4;
const uint32_t NUM_MODE6_ENDPOINTS = 4, NUM_MODE8_ENDPOINTS = 6, NUM_MODE9_ENDPOINTS = 6; // LDR RGB
const uint32_t NUM_MODE10_ENDPOINTS = 6, NUM_MODE12_ENDPOINTS = 8, NUM_MODE13_ENDPOINTS = 8; // LDR RGBA
const uint32_t NUM_MODE11_ENDPOINTS = 6, NUM_MODE7_ENDPOINTS = 4; // hdr
const uint32_t MAX_WEIGHTS = 32; // max supported # of weights (or "selectors") in any mode, i.e. the max # of colors per endpoint pair
const uint32_t MAX_WEIGHT_INTERPOLANT_VALUE = 64; // grid texel weights must range from [0,64], i.e. the weight interpolant range is [0,64]
// 14 unique block dimensions supported by ASTC
static const uint32_t NUM_ASTC_BLOCK_SIZES = 14;
extern const uint8_t g_astc_block_sizes[NUM_ASTC_BLOCK_SIZES][2];
// The Color Endpoint Modes (CEM's)
enum cems
{
CEM_LDR_LUM_DIRECT = 0,
CEM_LDR_LUM_BASE_PLUS_OFS = 1,
CEM_HDR_LUM_LARGE_RANGE = 2,
CEM_HDR_LUM_SMALL_RANGE = 3,
CEM_LDR_LUM_ALPHA_DIRECT = 4,
CEM_LDR_LUM_ALPHA_BASE_PLUS_OFS = 5,
CEM_LDR_RGB_BASE_SCALE = 6,
CEM_HDR_RGB_BASE_SCALE = 7,
CEM_LDR_RGB_DIRECT = 8,
CEM_LDR_RGB_BASE_PLUS_OFFSET = 9,
CEM_LDR_RGB_BASE_SCALE_PLUS_TWO_A = 10,
CEM_HDR_RGB = 11,
CEM_LDR_RGBA_DIRECT = 12,
CEM_LDR_RGBA_BASE_PLUS_OFFSET = 13,
CEM_HDR_RGB_LDR_ALPHA = 14,
CEM_HDR_RGB_HDR_ALPHA = 15
};
// All Bounded Integer Sequence Coding (BISE or ISE) ranges.
// Weights: Ranges [0,11] are valid.
// Endpoints: Ranges [4,20] are valid.
enum bise_levels
{
BISE_2_LEVELS = 0,
BISE_3_LEVELS = 1,
BISE_4_LEVELS = 2,
BISE_5_LEVELS = 3,
BISE_6_LEVELS = 4,
BISE_8_LEVELS = 5,
BISE_10_LEVELS = 6,
BISE_12_LEVELS = 7,
BISE_16_LEVELS = 8,
BISE_20_LEVELS = 9,
BISE_24_LEVELS = 10,
BISE_32_LEVELS = 11,
BISE_40_LEVELS = 12,
BISE_48_LEVELS = 13,
BISE_64_LEVELS = 14,
BISE_80_LEVELS = 15,
BISE_96_LEVELS = 16,
BISE_128_LEVELS = 17,
BISE_160_LEVELS = 18,
BISE_192_LEVELS = 19,
BISE_256_LEVELS = 20
};
const uint32_t TOTAL_ISE_RANGES = 21;
enum
{
cBLOCK_SIZE_4x4 = 0, // 16 samples
cBLOCK_SIZE_5x4 = 1, // 20 samples
cBLOCK_SIZE_5x5 = 2, // 25 samples
cBLOCK_SIZE_6x5 = 3, // 30 samples
cBLOCK_SIZE_6x6 = 4, // 36 samples
cBLOCK_SIZE_8x5 = 5, // 40 samples
cBLOCK_SIZE_8x6 = 6, // 48 samples
cBLOCK_SIZE_10x5 = 7, // 50 samples
cBLOCK_SIZE_10x6 = 8, // 60 samples
cBLOCK_SIZE_8x8 = 9, // 64 samples
cBLOCK_SIZE_10x8 = 10, // 80 samples
cBLOCK_SIZE_10x10 = 11, // 100 samples
cBLOCK_SIZE_12x10 = 12, // 120 samples
cBLOCK_SIZE_12x12 = 13, // 144 samples
cTOTAL_BLOCK_SIZES = 14
};
// Valid endpoint ISE ranges
const uint32_t FIRST_VALID_ENDPOINT_ISE_RANGE = BISE_6_LEVELS; // 4
const uint32_t LAST_VALID_ENDPOINT_ISE_RANGE = BISE_256_LEVELS; // 20
const uint32_t TOTAL_ENDPOINT_ISE_RANGES = LAST_VALID_ENDPOINT_ISE_RANGE - FIRST_VALID_ENDPOINT_ISE_RANGE + 1;
// Valid weight ISE ranges
const uint32_t FIRST_VALID_WEIGHT_ISE_RANGE = BISE_2_LEVELS; // 0
const uint32_t LAST_VALID_WEIGHT_ISE_RANGE = BISE_32_LEVELS; // 11
const uint32_t TOTAL_WEIGHT_ISE_RANGES = LAST_VALID_WEIGHT_ISE_RANGE - FIRST_VALID_WEIGHT_ISE_RANGE + 1;
// The ISE range table.
extern const int8_t g_ise_range_table[TOTAL_ISE_RANGES][3]; // 0=bits (0 to 8), 1=trits (0 or 1), 2=quints (0 or 1)
// Possible Color Component Select values, used in dual plane mode.
// The CCS component will be interpolated using the 2nd weight plane.
enum ccs
{
CCS_GBA_R = 0,
CCS_RBA_G = 1,
CCS_RGA_B = 2,
CCS_RGB_A = 3
};
struct astc_block
{
uint32_t m_vals[4];
};
const uint32_t MAX_PARTITIONS = 4; // Max # of partitions or subsets for single plane mode
const uint32_t MAX_DUAL_PLANE_PARTITIONS = 3; // Max # of partitions or subsets for dual plane mode
const uint32_t NUM_PARTITION_PATTERNS = 1024; // Total # of partition pattern seeds (10-bits)
const uint32_t MAX_ENDPOINTS = 18; // Maximum # of endpoint values in a block
struct log_astc_block
{
bool m_error_flag;
bool m_solid_color_flag_ldr, m_solid_color_flag_hdr;
uint8_t m_user_mode; // user defined value, not used in this module
// Rest is only valid if !m_solid_color_flag_ldr && !m_solid_color_flag_hdr
uint8_t m_grid_width, m_grid_height; // weight grid dimensions, not the dimension of the block
bool m_dual_plane;
uint8_t m_weight_ise_range; // 0-11
uint8_t m_endpoint_ise_range; // 4-20, this is actually inferred from the size of the other config bits+weights, but this is here for checking
uint8_t m_color_component_selector; // 0-3, controls which channel uses the 2nd (odd) weights, only used in dual plane mode
uint8_t m_num_partitions; // or the # of subsets, 1-4 (1-3 if dual plane mode)
uint16_t m_partition_id; // 10-bits, must be 0 if m_num_partitions==1
uint8_t m_color_endpoint_modes[MAX_PARTITIONS]; // each subset's CEM's
union
{
// ISE weight grid values. In dual plane mode, the order is p0,p1, p0,p1, etc.
uint8_t m_weights[MAX_GRID_WEIGHTS];
uint16_t m_solid_color[4];
};
// ISE endpoint values
// Endpoint order examples:
// 1 subset LA : LL0 LH0 AL0 AH0
// 1 subset RGB : RL0 RH0 GL0 GH0 BL0 BH0
// 1 subset RGBA : RL0 RH0 GL0 GH0 BL0 BH0 AL0 AH0
// 2 subset LA : LL0 LH0 AL0 AH0 LL1 LH1 AL1 AH1
// 2 subset RGB : RL0 RH0 GL0 GH0 BL0 BH0 RL1 RH1 GL1 GH1 BL1 BH1
// 2 subset RGBA : RL0 RH0 GL0 GH0 BL0 BH0 AL0 AH0 RL1 RH1 GL1 GH1 BL1 BH1 AL1 AH1
uint8_t m_endpoints[MAX_ENDPOINTS];
void clear()
{
memset(this, 0, sizeof(*this));
}
};
// Open interval
inline int bounds_check(int v, int l, int h) { (void)v; (void)l; (void)h; assert(v >= l && v < h); return v; }
inline uint32_t bounds_check(uint32_t v, uint32_t l, uint32_t h) { (void)v; (void)l; (void)h; assert(v >= l && v < h); return v; }
inline uint32_t get_bits(uint32_t val, int low, int high)
{
const int num_bits = (high - low) + 1;
assert((num_bits >= 1) && (num_bits <= 32));
val >>= low;
if (num_bits != 32)
val &= ((1u << num_bits) - 1);
return val;
}
// Returns the number of levels in the given ISE range.
inline uint32_t get_ise_levels(uint32_t ise_range)
{
assert(ise_range < TOTAL_ISE_RANGES);
return (1 + 2 * g_ise_range_table[ise_range][1] + 4 * g_ise_range_table[ise_range][2]) << g_ise_range_table[ise_range][0];
}
inline int get_ise_sequence_bits(int count, int range)
{
// See 18.22 Data Size Determination - note this will be <= the # of bits actually written by encode_bise(). (It's magic.)
int total_bits = g_ise_range_table[range][0] * count;
total_bits += (g_ise_range_table[range][1] * 8 * count + 4) / 5;
total_bits += (g_ise_range_table[range][2] * 7 * count + 2) / 3;
return total_bits;
}
inline uint32_t weight_interpolate(uint32_t l, uint32_t h, uint32_t w)
{
assert(w <= MAX_WEIGHT_INTERPOLANT_VALUE);
return (l * (64 - w) + h * w + 32) >> 6;
}
void encode_bise(uint32_t* pDst, const uint8_t* pSrc_vals, uint32_t bit_pos, int num_vals, int range, uint32_t *pStats = nullptr);
struct pack_stats
{
uint32_t m_header_bits;
uint32_t m_endpoint_bits;
uint32_t m_weight_bits;
inline pack_stats() { clear(); }
inline void clear() { memset(this, 0, sizeof(*this)); }
};
enum
{
cValidateEarlyOutAtEndpointISEChecks = 1,
cValidateSkipFinalEndpointWeightPacking = 2,
};
// Packs a logical to physical ASTC block. Note this does not validate the block's dimensions (use is_valid_block_size()), just the grid dimensions.
bool pack_astc_block(astc_block &phys_block, const log_astc_block& log_block, int* pExpected_endpoint_range = nullptr, pack_stats *pStats = nullptr, uint32_t validate_flags = 0);
// Pack LDR void extent (really solid color) blocks. For LDR, pass in (val | (val << 8)) for each component.
void pack_void_extent_ldr(astc_block& blk, uint16_t r, uint16_t g, uint16_t b, uint16_t a, pack_stats *pStats = nullptr);
// Pack HDR void extent (16-bit values are FP16/half floats - no NaN/Inf's)
void pack_void_extent_hdr(astc_block& blk, uint16_t rh, uint16_t gh, uint16_t bh, uint16_t ah, pack_stats* pStats = nullptr);
// These helpers are all quite slow, but are useful for table preparation.
// Dequantizes ISE encoded endpoint val to [0,255]
uint32_t dequant_bise_endpoint(uint32_t val, uint32_t ise_range); // ISE ranges 4-11
// Dequantizes ISE encoded weight val to [0,64]
uint32_t dequant_bise_weight(uint32_t val, uint32_t ise_range); // ISE ranges 0-10
uint32_t find_nearest_bise_endpoint(int v, uint32_t ise_range);
uint32_t find_nearest_bise_weight(int v, uint32_t ise_range);
void create_quant_tables(
uint8_t* pVal_to_ise, // [0-255] or [0-64] value to nearest ISE symbol, array size is [256] or [65]
uint8_t* pISE_to_val, // ASTC encoded ISE symbol to [0,255] or [0,64] value, [levels]
uint8_t* pISE_to_rank, // returns the level rank index given an ISE symbol, [levels]
uint8_t* pRank_to_ISE, // returns the ISE symbol given a level rank, inverse of pISE_to_rank, [levels]
uint32_t ise_range, // ise range, [4,20] for endpoints, [0,11] for weights
bool weight_flag); // false if block endpoints, true if weights
// True if the CEM is LDR.
bool is_cem_ldr(uint32_t mode);
inline bool is_cem_hdr(uint32_t mode) { return !is_cem_ldr(mode); }
bool does_cem_have_alpha(uint32_t mode);
// True if the passed in dimensions are a valid ASTC block size. There are 14 supported configs, from 4x4 (8bpp) to 12x12 (.89bpp).
bool is_valid_block_size(uint32_t w, uint32_t h);
// w/h must be a valid ASTC block size, or it returns cBLOCK_SIZE_4x4
uint32_t get_block_size_index(uint32_t w, uint32_t h);
float get_bitrate_from_block_size(uint32_t w, uint32_t h);
uint32_t get_texel_partition_from_table(uint32_t block_width, uint32_t block_height, uint32_t seed, uint32_t subsets, uint32_t x, uint32_t y);
bool block_has_any_hdr_cems(const log_astc_block& log_blk);
bool block_has_any_ldr_cems(const log_astc_block& log_blk);
// Returns the # of endpoint values for the given CEM.
inline uint32_t get_num_cem_values(uint32_t cem) { assert(cem <= 15); return 2 + 2 * (cem >> 2); }
struct dequant_table
{
basisu::vector<uint8_t> m_val_to_ise; // [0-255] or [0-64] value to nearest ISE symbol, array size is [256] or [65]
basisu::vector<uint8_t> m_ISE_to_val; // ASTC encoded ISE symbol to [0,255] or [0,64] value, [levels]
basisu::vector<uint8_t> m_ISE_to_rank; // returns the level rank index given an ISE symbol, [levels]
basisu::vector<uint8_t> m_rank_to_ISE; // returns the ISE symbol given a level rank, inverse of pISE_to_rank, [levels]
void init(bool weight_flag, uint32_t num_levels)
{
m_val_to_ise.resize(weight_flag ? (MAX_WEIGHT_INTERPOLANT_VALUE + 1) : 256);
m_ISE_to_val.resize(num_levels);
m_ISE_to_rank.resize(num_levels);
m_rank_to_ISE.resize(num_levels);
}
uint32_t get_rank_to_val(uint32_t rank) const
{
const uint32_t ise = m_rank_to_ISE[rank];
const uint32_t val = m_ISE_to_val[ise];
return val;
}
uint32_t get_val_to_rank(uint32_t val)
{
const uint32_t ise = m_val_to_ise[val];
const uint32_t rank = m_ISE_to_rank[ise];
return rank;
}
};
struct dequant_tables
{
dequant_table m_weights[TOTAL_WEIGHT_ISE_RANGES];
dequant_table m_endpoints[TOTAL_ENDPOINT_ISE_RANGES];
bool m_initialized_flag = false;
const dequant_table& get_weight_tab(uint32_t range) const
{
assert((range >= FIRST_VALID_WEIGHT_ISE_RANGE) && (range <= LAST_VALID_WEIGHT_ISE_RANGE));
return m_weights[range - FIRST_VALID_WEIGHT_ISE_RANGE];
}
dequant_table& get_weight_tab(uint32_t range)
{
assert((range >= FIRST_VALID_WEIGHT_ISE_RANGE) && (range <= LAST_VALID_WEIGHT_ISE_RANGE));
return m_weights[range - FIRST_VALID_WEIGHT_ISE_RANGE];
}
const dequant_table& get_endpoint_tab(uint32_t range) const
{
assert((range >= FIRST_VALID_ENDPOINT_ISE_RANGE) && (range <= LAST_VALID_ENDPOINT_ISE_RANGE));
return m_endpoints[range - FIRST_VALID_ENDPOINT_ISE_RANGE];
}
dequant_table& get_endpoint_tab(uint32_t range)
{
assert((range >= FIRST_VALID_ENDPOINT_ISE_RANGE) && (range <= LAST_VALID_ENDPOINT_ISE_RANGE));
return m_endpoints[range - FIRST_VALID_ENDPOINT_ISE_RANGE];
}
void init()
{
if (m_initialized_flag)
return;
for (uint32_t range = FIRST_VALID_WEIGHT_ISE_RANGE; range <= LAST_VALID_WEIGHT_ISE_RANGE; range++)
{
const uint32_t num_levels = get_ise_levels(range);
dequant_table& tab = get_weight_tab(range);
tab.init(true, num_levels);
create_quant_tables(tab.m_val_to_ise.data(), tab.m_ISE_to_val.data(), tab.m_ISE_to_rank.data(), tab.m_rank_to_ISE.data(), range, true);
}
for (uint32_t range = FIRST_VALID_ENDPOINT_ISE_RANGE; range <= LAST_VALID_ENDPOINT_ISE_RANGE; range++)
{
const uint32_t num_levels = get_ise_levels(range);
dequant_table& tab = get_endpoint_tab(range);
tab.init(false, num_levels);
create_quant_tables(tab.m_val_to_ise.data(), tab.m_ISE_to_val.data(), tab.m_ISE_to_rank.data(), tab.m_rank_to_ISE.data(), range, false);
}
m_initialized_flag = true;
}
};
extern dequant_tables g_dequant_tables;
void init_tables();
struct weighted_sample
{
uint8_t m_src_x;
uint8_t m_src_y;
uint8_t m_weights[2][2]; // [y][x], scaled by 16, round by adding 8
};
void compute_upsample_weights(
int block_width, int block_height,
int weight_grid_width, int weight_grid_height,
weighted_sample* pWeights); // there will be block_width * block_height bilinear samples
void upsample_weight_grid(
uint32_t bx, uint32_t by, // destination/to dimension
uint32_t wx, uint32_t wy, // source/from dimension
const uint8_t* pSrc_weights, // these are dequantized [0,64] weights, NOT ISE symbols, [wy][wx]
uint8_t* pDst_weights); // [by][bx]
void upsample_weight_grid_xuastc_ldr(
uint32_t bx, uint32_t by, // destination/to dimension
uint32_t wx, uint32_t wy, // source/from dimension
const uint8_t* pSrc_weights0, // these are dequantized [0,64] weights, NOT ISE symbols, [wy][wx]
uint8_t* pDst_weights0, // [by][bx]
const uint8_t* pSrc_weights1, // these are dequantized [0,64] weights, NOT ISE symbols, [wy][wx]
uint8_t* pDst_weights1); // [by][bx]
bool is_small_block(uint32_t block_width, uint32_t block_height);
// Procedurally returns the texel partition/subset index given the block coordinate and config (very slow).
int compute_texel_partition(uint32_t seedIn, uint32_t xIn, uint32_t yIn, uint32_t zIn, int num_partitions, bool small_block);
// Returns the texel partition/subset index given the block coordinate and config - table lookup, but currently ONLY 2-3 SUBSETS to save RAM.
int get_precomputed_texel_partition(uint32_t block_width, uint32_t block_height, uint32_t seed, uint32_t x, uint32_t y, uint32_t num_partitions);
void blue_contract(
int r, int g, int b, int a,
int& dr, int& dg, int& db, int& da);
void bit_transfer_signed(int& a, int& b);
void decode_endpoint(uint32_t cem_index, int (*pEndpoints)[2], const uint8_t* pE);
typedef uint16_t half_float;
half_float float_to_half(float val, bool toward_zero);
float half_to_float(half_float hval);
// Notes:
// qlog16_to_half(half_to_qlog16(half_val_as_int)) == half_val_as_int (is lossless)
// However, this is not lossless in the general sense.
inline half_float qlog16_to_half(int k)
{
assert((k >= 0) && (k <= 0xFFFF));
int E = (k & 0xF800) >> 11;
int M = k & 0x7FF;
int Mt;
if (M < 512)
Mt = 3 * M;
else if (M >= 1536)
Mt = 5 * M - 2048;
else
Mt = 4 * M - 512;
return (half_float)((E << 10) + (Mt >> 3));
}
const int MAX_RGB9E5 = 0xff80;
void unpack_rgb9e5(uint32_t packed, float& r, float& g, float& b);
uint32_t pack_rgb9e5(float r, float g, float b);
enum decode_mode
{
cDecodeModeSRGB8 = 0, // returns uint8_t's, not valid on HDR blocks
cDecodeModeLDR8 = 1, // returns uint8_t's, not valid on HDR blocks
cDecodeModeHDR16 = 2, // returns uint16_t's (half floats), valid on all LDR/HDR blocks
cDecodeModeRGB9E5 = 3 // returns uint32_t's, packed as RGB 9E5 (shared exponent), see https://registry.khronos.org/OpenGL/extensions/EXT/EXT_texture_shared_exponent.txt
};
// Decodes logical block to output pixels.
// pPixels must point to either 32-bit pixel values (SRGB8/LDR8/9E5) or 64-bit pixel values (HDR16)
bool decode_block(const log_astc_block& log_blk, void* pPixels, uint32_t blk_width, uint32_t blk_height, decode_mode dec_mode);
// Assuming the ASTC logical block is valid, this checks for the extra XUASTC LDR constraints.
bool is_block_xuastc_ldr(const log_astc_block& log_blk);
// XUASTC LDR only - primary assumption is the logical block comes directly from our supercompressor. DO NOT call on general ASTC blocks.
bool decode_block_xuastc_ldr(const log_astc_block& log_blk, void* pPixels, uint32_t blk_width, uint32_t blk_height, decode_mode dec_mode, const uint8_t* pUpsampled_weights_to_use = nullptr, uint32_t start_x = 0, uint32_t start_y = 0, uint32_t end_x = 0, uint32_t end_y = 0);
void decode_bise(uint32_t ise_range, uint8_t* pVals, uint32_t num_vals, const uint8_t *pBits128, uint32_t bit_ofs);
// Unpack a physical ASTC encoded GPU texture block to a logical block description.
bool unpack_block(const void* pASTC_block, log_astc_block& log_blk, uint32_t blk_width, uint32_t blk_height);
uint8_t& get_weight(log_astc_block& log_block, uint32_t plane_index, uint32_t idx);
uint8_t get_weight(const log_astc_block& log_block, uint32_t plane_index, uint32_t idx);
void extract_weights(const log_astc_block& log_block, uint8_t* pWeights, uint32_t plane_index);
void set_weights(log_astc_block& log_block, const uint8_t* pWeights, uint32_t plane_index);
uint32_t get_total_weights(const log_astc_block& log_block);
uint8_t* get_endpoints(log_astc_block& log_block, uint32_t partition_index);
const uint8_t* get_endpoints(const log_astc_block& log_block, uint32_t partition_index);
const char* get_cem_name(uint32_t cem_index);
bool cem_is_ldr_direct(uint32_t cem_index);
bool cem_is_ldr_base_scale(uint32_t cem_index);
bool cem_is_ldr_base_plus_ofs(uint32_t cem_index);
bool cem_supports_bc(uint32_t cem);
void bit_transfer_signed_dec(int& a, int& b);
void bit_transfer_signed_enc(int& a, int& b);
bool cem8_or_12_used_blue_contraction(uint32_t cem_index, const uint8_t* pEndpoint_vals, uint32_t endpoint_ise_index);
bool cem9_or_13_used_blue_contraction(uint32_t cem_index, const uint8_t* pEndpoint_vals, uint32_t endpoint_ise_index);
bool used_blue_contraction(uint32_t cem_index, const uint8_t* pEndpoint_vals, uint32_t endpoint_ise_index);
uint32_t get_base_cem_without_alpha(uint32_t cem);
int apply_delta_to_bise_endpoint_val(uint32_t endpoint_ise_range, int ise_val, int delta);
// index range: [0,NUM_ASTC_BLOCK_SIZES-1]
void get_astc_block_size_by_index(uint32_t index, uint32_t& width, uint32_t& height);
// -1 if invalid
int find_astc_block_size_index(uint32_t width, uint32_t height);
// 8-bit linear8 or sRGB8, le/he are [0,255], w is [0,64]
inline int channel_interpolate(int le, int he, int w, bool astc_srgb_decode)
{
assert((w >= 0) && (w <= 64));
assert((le >= 0) && (le <= 255));
assert((he >= 0) && (he <= 255));
if (astc_srgb_decode)
{
le = (le << 8) | 0x80;
he = (he << 8) | 0x80;
}
else
{
le = (le << 8) | le;
he = (he << 8) | he;
}
return astc_helpers::weight_interpolate(le, he, w) >> 8;
}
} // namespace astc_helpers
// Alternative ASTC LDR/HDR decoder defined in encoder/3rdparty/android_astc_decomp.cpp
// If BASISU_DISABLE_ANDROID_ASTC_DECOMP is set to 1, this functionality is completely implemented/emulated here, otherwise it goes into android_astc_decomp.cpp.
namespace basisu_astc
{
namespace astc
{
// Unpacks a single ASTC block to pDst
// If isSRGB is true, the spec requires the decoder to scale the LDR 8-bit endpoints to 16-bit before interpolation slightly differently,
// which will lead to different outputs. So be sure to set it correctly (ideally it should match whatever the encoder did).
bool decompress_ldr(uint8_t* pDst, const uint8_t* data, bool isSRGB, int blockWidth, int blockHeight);
bool decompress_hdr(float* pDstRGBA, const uint8_t* data, int blockWidth, int blockHeight);
bool is_hdr(const uint8_t* data, int blockWidth, int blockHeight, bool& is_hdr_flag);
} // astc
} // basisu
#endif // BASISU_ASTC_HELPERS_HEADER
//------------------------------------------------------------------
#ifdef BASISU_ASTC_HELPERS_IMPLEMENTATION
namespace astc_helpers
{
template<typename T> inline T my_min(T a, T b) { return (a < b) ? a : b; }
template<typename T> inline T my_max(T a, T b) { return (a > b) ? a : b; }
const uint8_t g_astc_block_sizes[NUM_ASTC_BLOCK_SIZES][2] = {
{ 4, 4 }, { 5, 4 }, { 5, 5 }, { 6, 5 },
{ 6, 6 }, { 8, 5 }, { 8, 6 }, { 10, 5 },
{ 10, 6 }, { 8, 8 }, { 10, 8 }, { 10, 10 },
{ 12, 10 }, { 12, 12 }
};
const int8_t g_ise_range_table[TOTAL_ISE_RANGES][3] =
{
//b t q
//2 3 5 // rng ise_index notes
{ 1, 0, 0 }, // 0..1 0
{ 0, 1, 0 }, // 0..2 1
{ 2, 0, 0 }, // 0..3 2
{ 0, 0, 1 }, // 0..4 3
{ 1, 1, 0 }, // 0..5 4 min endpoint ISE index
{ 3, 0, 0 }, // 0..7 5
{ 1, 0, 1 }, // 0..9 6
{ 2, 1, 0 }, // 0..11 7
{ 4, 0, 0 }, // 0..15 8
{ 2, 0, 1 }, // 0..19 9
{ 3, 1, 0 }, // 0..23 10
{ 5, 0, 0 }, // 0..31 11 max weight ISE index
{ 3, 0, 1 }, // 0..39 12
{ 4, 1, 0 }, // 0..47 13
{ 6, 0, 0 }, // 0..63 14
{ 4, 0, 1 }, // 0..79 15
{ 5, 1, 0 }, // 0..95 16
{ 7, 0, 0 }, // 0..127 17
{ 5, 0, 1 }, // 0..159 18
{ 6, 1, 0 }, // 0..191 19
{ 8, 0, 0 }, // 0..255 20
};
static inline void astc_set_bits_1_to_9(uint32_t* pDst, uint32_t& bit_offset, uint32_t code, uint32_t codesize)
{
uint8_t* pBuf = reinterpret_cast<uint8_t*>(pDst);
assert(codesize <= 9);
if (codesize)
{
uint32_t byte_bit_offset = bit_offset & 7;
uint32_t val = code << byte_bit_offset;
uint32_t index = bit_offset >> 3;
pBuf[index] |= (uint8_t)val;
if (codesize > (8 - byte_bit_offset))
pBuf[index + 1] |= (uint8_t)(val >> 8);
bit_offset += codesize;
}
}
static inline uint32_t astc_extract_bits(uint32_t bits, int low, int high)
{
return (bits >> low) & ((1 << (high - low + 1)) - 1);
}
// Writes bits to output in an endian safe way
static inline void astc_set_bits(uint32_t* pOutput, uint32_t& bit_pos, uint32_t value, uint32_t total_bits)
{
assert(total_bits <= 31);
assert(value < (1u << total_bits));
uint8_t* pBytes = reinterpret_cast<uint8_t*>(pOutput);
while (total_bits)
{
const uint32_t bits_to_write = my_min<int>(total_bits, 8 - (bit_pos & 7));
pBytes[bit_pos >> 3] |= static_cast<uint8_t>(value << (bit_pos & 7));
bit_pos += bits_to_write;
total_bits -= bits_to_write;
value >>= bits_to_write;
}
}
static const uint8_t g_astc_quint_encode[125] =
{
0, 1, 2, 3, 4, 8, 9, 10, 11, 12, 16, 17, 18, 19, 20, 24, 25, 26, 27, 28, 5, 13, 21, 29, 6, 32, 33, 34, 35, 36, 40, 41, 42, 43, 44, 48, 49, 50, 51, 52, 56, 57,
58, 59, 60, 37, 45, 53, 61, 14, 64, 65, 66, 67, 68, 72, 73, 74, 75, 76, 80, 81, 82, 83, 84, 88, 89, 90, 91, 92, 69, 77, 85, 93, 22, 96, 97, 98, 99, 100, 104,
105, 106, 107, 108, 112, 113, 114, 115, 116, 120, 121, 122, 123, 124, 101, 109, 117, 125, 30, 102, 103, 70, 71, 38, 110, 111, 78, 79, 46, 118, 119, 86, 87, 54,
126, 127, 94, 95, 62, 39, 47, 55, 63, 7 /*31 - results in the same decode as 7*/
};
// Encodes 3 values to output, usable for any range that uses quints and bits
static inline void astc_encode_quints(uint32_t* pOutput, const uint8_t* pValues, uint32_t& bit_pos, int n, uint32_t* pStats)
{
// First extract the quints and the bits from the 3 input values
int quints = 0, bits[3];
const uint32_t bit_mask = (1 << n) - 1;
for (int i = 0; i < 3; i++)
{
static const int s_muls[3] = { 1, 5, 25 };
const int t = pValues[i] >> n;
quints += t * s_muls[i];
bits[i] = pValues[i] & bit_mask;
}
// Encode the quints, by inverting the bit manipulations done by the decoder, converting 3 quints into 7-bits.
// See https://www.khronos.org/registry/DataFormat/specs/1.2/dataformat.1.2.html#astc-integer-sequence-encoding
assert(quints < 125);
const int T = g_astc_quint_encode[quints];
// Now interleave the 7 encoded quint bits with the bits to form the encoded output. See table 95-96.
astc_set_bits(pOutput, bit_pos, bits[0] | (astc_extract_bits(T, 0, 2) << n) | (bits[1] << (3 + n)) | (astc_extract_bits(T, 3, 4) << (3 + n * 2)) |
(bits[2] << (5 + n * 2)) | (astc_extract_bits(T, 5, 6) << (5 + n * 3)), 7 + n * 3);
if (pStats)
*pStats += n * 3 + 7;
}
static const uint8_t g_astc_trit_encode[243] = { 0, 1, 2, 4, 5, 6, 8, 9, 10, 16, 17, 18, 20, 21, 22, 24, 25, 26, 3, 7, 11, 19, 23, 27, 12, 13, 14, 32, 33, 34, 36, 37, 38, 40, 41, 42, 48, 49, 50, 52, 53, 54, 56, 57, 58, 35, 39,
43, 51, 55, 59, 44, 45, 46, 64, 65, 66, 68, 69, 70, 72, 73, 74, 80, 81, 82, 84, 85, 86, 88, 89, 90, 67, 71, 75, 83, 87, 91, 76, 77, 78, 128, 129, 130, 132, 133, 134, 136, 137, 138, 144, 145, 146, 148, 149, 150, 152, 153, 154,
131, 135, 139, 147, 151, 155, 140, 141, 142, 160, 161, 162, 164, 165, 166, 168, 169, 170, 176, 177, 178, 180, 181, 182, 184, 185, 186, 163, 167, 171, 179, 183, 187, 172, 173, 174, 192, 193, 194, 196, 197, 198, 200, 201, 202,
208, 209, 210, 212, 213, 214, 216, 217, 218, 195, 199, 203, 211, 215, 219, 204, 205, 206, 96, 97, 98, 100, 101, 102, 104, 105, 106, 112, 113, 114, 116, 117, 118, 120, 121, 122, 99, 103, 107, 115, 119, 123, 108, 109, 110, 224,
225, 226, 228, 229, 230, 232, 233, 234, 240, 241, 242, 244, 245, 246, 248, 249, 250, 227, 231, 235, 243, 247, 251, 236, 237, 238, 28, 29, 30, 60, 61, 62, 92, 93, 94, 156, 157, 158, 188, 189, 190, 220, 221, 222, 31, 63, 95, 159,
191, 223, 124, 125, 126 };
// Encodes 5 values to output, usable for any range that uses trits and bits
static void astc_encode_trits(uint32_t* pOutput, const uint8_t* pValues, uint32_t& bit_pos, int n, uint32_t *pStats)
{
// First extract the trits and the bits from the 5 input values
int trits = 0, bits[5];
const uint32_t bit_mask = (1 << n) - 1;
for (int i = 0; i < 5; i++)
{
static const int s_muls[5] = { 1, 3, 9, 27, 81 };
const int t = pValues[i] >> n;
trits += t * s_muls[i];
bits[i] = pValues[i] & bit_mask;
}
// Encode the trits, by inverting the bit manipulations done by the decoder, converting 5 trits into 8-bits.
// See https://www.khronos.org/registry/DataFormat/specs/1.2/dataformat.1.2.html#astc-integer-sequence-encoding
assert(trits < 243);
const int T = g_astc_trit_encode[trits];
// Now interleave the 8 encoded trit bits with the bits to form the encoded output. See table 94.
astc_set_bits(pOutput, bit_pos, bits[0] | (astc_extract_bits(T, 0, 1) << n) | (bits[1] << (2 + n)), n * 2 + 2);
astc_set_bits(pOutput, bit_pos, astc_extract_bits(T, 2, 3) | (bits[2] << 2) | (astc_extract_bits(T, 4, 4) << (2 + n)) | (bits[3] << (3 + n)) | (astc_extract_bits(T, 5, 6) << (3 + n * 2)) |
(bits[4] << (5 + n * 2)) | (astc_extract_bits(T, 7, 7) << (5 + n * 3)), n * 3 + 6);
if (pStats)
*pStats += n * 5 + 8;
}
// Packs values using ASTC's BISE to output buffer.
void encode_bise(uint32_t* pDst, const uint8_t* pSrc_vals, uint32_t bit_pos, int num_vals, int range, uint32_t *pStats)
{
uint32_t temp[5] = { 0 };
const int num_bits = g_ise_range_table[range][0];
int group_size = 0;
if (g_ise_range_table[range][1])
group_size = 5;
else if (g_ise_range_table[range][2])
group_size = 3;
#ifndef NDEBUG
const uint32_t num_levels = get_ise_levels(range);
for (int i = 0; i < num_vals; i++)
{
assert(pSrc_vals[i] < num_levels);
}
#endif
if (group_size)
{
// Range has trits or quints - pack each group of 5 or 3 values
const int total_groups = (group_size == 5) ? ((num_vals + 4) / 5) : ((num_vals + 2) / 3);
for (int group_index = 0; group_index < total_groups; group_index++)
{
uint8_t vals[5] = { 0 };
const int limit = my_min(group_size, num_vals - group_index * group_size);
for (int i = 0; i < limit; i++)
vals[i] = pSrc_vals[group_index * group_size + i];
// Note this always writes a group of 3 or 5 bits values, even for incomplete groups. So it can write more than needed.
// get_ise_sequence_bits() returns the # of bits that must be written for proper decoding.
if (group_size == 5)
astc_encode_trits(temp, vals, bit_pos, num_bits, pStats);
else
astc_encode_quints(temp, vals, bit_pos, num_bits, pStats);
}
}
else
{
for (int i = 0; i < num_vals; i++)
astc_set_bits_1_to_9(temp, bit_pos, pSrc_vals[i], num_bits);
if (pStats)
*pStats += num_vals * num_bits;
}
pDst[0] |= temp[0]; pDst[1] |= temp[1];
pDst[2] |= temp[2]; pDst[3] |= temp[3];
}
inline uint32_t rev_dword(uint32_t bits)
{
uint32_t v = (bits << 16) | (bits >> 16);
v = ((v & 0x00ff00ff) << 8) | ((v & 0xff00ff00) >> 8); v = ((v & 0x0f0f0f0f) << 4) | ((v & 0xf0f0f0f0) >> 4);
v = ((v & 0x33333333) << 2) | ((v & 0xcccccccc) >> 2); v = ((v & 0x55555555) << 1) | ((v & 0xaaaaaaaa) >> 1);
return v;
}
static inline bool is_packable(int value, int num_bits) { assert((num_bits >= 1) && (num_bits < 31)); return (value >= 0) && (value < (1 << num_bits)); }
static bool get_config_bits(const log_astc_block &log_block, uint32_t &config_bits)
{
config_bits = 0;
const int W = log_block.m_grid_width, H = log_block.m_grid_height;
const uint32_t P = log_block.m_weight_ise_range >= 6; // high precision
const uint32_t Dp_P = (log_block.m_dual_plane << 1) | P; // pack dual plane+high precision bits
// See Tables 81-82
// Compute p from weight range
uint32_t p = 2 + log_block.m_weight_ise_range - (P ? 6 : 0);
// Rearrange p's bits to p0 p2 p1
p = (p >> 1) + ((p & 1) << 2);
// Try encoding each row of table 82.
// W+4 H+2
if (is_packable(W - 4, 2) && is_packable(H - 2, 2))
{
config_bits = (Dp_P << 9) | ((W - 4) << 7) | ((H - 2) << 5) | ((p & 4) << 2) | (p & 3);
return true;
}
// W+8 H+2
if (is_packable(W - 8, 2) && is_packable(H - 2, 2))
{
config_bits = (Dp_P << 9) | ((W - 8) << 7) | ((H - 2) << 5) | ((p & 4) << 2) | 4 | (p & 3);
return true;
}
// W+2 H+8
if (is_packable(W - 2, 2) && is_packable(H - 8, 2))
{
config_bits = (Dp_P << 9) | ((H - 8) << 7) | ((W - 2) << 5) | ((p & 4) << 2) | 8 | (p & 3);
return true;
}
// W+2 H+6
if (is_packable(W - 2, 2) && is_packable(H - 6, 1))
{
config_bits = (Dp_P << 9) | ((H - 6) << 7) | ((W - 2) << 5) | ((p & 4) << 2) | 12 | (p & 3);
return true;
}
// W+2 H+2
if (is_packable(W - 2, 1) && is_packable(H - 2, 2))
{
config_bits = (Dp_P << 9) | ((W) << 7) | ((H - 2) << 5) | ((p & 4) << 2) | 12 | (p & 3);
return true;
}
// 12 H+2
if ((W == 12) && is_packable(H - 2, 2))
{
config_bits = (Dp_P << 9) | ((H - 2) << 5) | (p << 2);
return true;
}
// W+2 12
if ((H == 12) && is_packable(W - 2, 2))
{
config_bits = (Dp_P << 9) | (1 << 7) | ((W - 2) << 5) | (p << 2);
return true;
}
// 6 10
if ((W == 6) && (H == 10))
{
config_bits = (Dp_P << 9) | (3 << 7) | (p << 2);
return true;
}
// 10 6
if ((W == 10) && (H == 6))
{
config_bits = (Dp_P << 9) | (0b1101 << 5) | (p << 2);
return true;
}
// W+6 H+6 (no dual plane or high prec)
if ((!Dp_P) && is_packable(W - 6, 2) && is_packable(H - 6, 2))
{
config_bits = ((H - 6) << 9) | 256 | ((W - 6) << 5) | (p << 2);
return true;
}
// Failed: unsupported weight grid dimensions or config.
return false;
}
bool pack_astc_block(astc_block& phys_block, const log_astc_block& log_block, int* pExpected_endpoint_range, pack_stats *pStats, uint32_t validate_flags)
{
// Basic sanity checking
if (!log_block.m_dual_plane)
{
assert(log_block.m_color_component_selector == 0);
}
else
{
assert(log_block.m_color_component_selector <= 3);
}
memset(&phys_block, 0, sizeof(phys_block));
if (pExpected_endpoint_range)
*pExpected_endpoint_range = -1;
assert(!log_block.m_error_flag);
if (log_block.m_error_flag)
return false;
if (log_block.m_solid_color_flag_ldr)
{
pack_void_extent_ldr(phys_block, log_block.m_solid_color[0], log_block.m_solid_color[1], log_block.m_solid_color[2], log_block.m_solid_color[3], pStats);
return true;
}
else if (log_block.m_solid_color_flag_hdr)
{
pack_void_extent_hdr(phys_block, log_block.m_solid_color[0], log_block.m_solid_color[1], log_block.m_solid_color[2], log_block.m_solid_color[3], pStats);
return true;
}
if ((log_block.m_num_partitions < 1) || (log_block.m_num_partitions > MAX_PARTITIONS))
return false;
// Max usable weight range is 11
if (log_block.m_weight_ise_range > LAST_VALID_WEIGHT_ISE_RANGE)
return false;
// See 23.24 Illegal Encodings, [0,5] is the minimum ISE encoding for endpoints
if ((log_block.m_endpoint_ise_range < FIRST_VALID_ENDPOINT_ISE_RANGE) || (log_block.m_endpoint_ise_range > LAST_VALID_ENDPOINT_ISE_RANGE))
return false;
if (log_block.m_color_component_selector > 3)
return false;
// TODO: sanity check grid width/height vs. block's physical width/height
uint32_t config_bits = 0;
if (!get_config_bits(log_block, config_bits))
return false;
uint32_t bit_pos = 0;
astc_set_bits(&phys_block.m_vals[0], bit_pos, config_bits, 11);
if (pStats)
pStats->m_header_bits += 11;
const uint32_t total_grid_weights = (log_block.m_dual_plane ? 2 : 1) * (log_block.m_grid_width * log_block.m_grid_height);
const uint32_t total_weight_bits = get_ise_sequence_bits(total_grid_weights, log_block.m_weight_ise_range);
// 18.24 Illegal Encodings
if ((!total_grid_weights) || (total_grid_weights > MAX_GRID_WEIGHTS) || (total_weight_bits < 24) || (total_weight_bits > 96))
return false;
uint32_t total_extra_bits = 0;
astc_set_bits(&phys_block.m_vals[0], bit_pos, log_block.m_num_partitions - 1, 2);
if (pStats)
pStats->m_header_bits += 2;
if (log_block.m_num_partitions > 1)
{
if (log_block.m_partition_id >= NUM_PARTITION_PATTERNS)
return false;
astc_set_bits(&phys_block.m_vals[0], bit_pos, log_block.m_partition_id, 10);
if (pStats)
pStats->m_header_bits += 10;
uint32_t highest_cem = 0, lowest_cem = UINT32_MAX;
for (uint32_t j = 0; j < log_block.m_num_partitions; j++)
{
highest_cem = my_max<uint32_t>(highest_cem, log_block.m_color_endpoint_modes[j]);
lowest_cem = my_min<uint32_t>(lowest_cem, log_block.m_color_endpoint_modes[j]);
}
if (highest_cem > 15)
return false;
// Ensure CEM range is contiguous
if (((highest_cem >> 2) > (1 + (lowest_cem >> 2))))
return false;
// See tables 79/80
uint32_t encoded_cem = log_block.m_color_endpoint_modes[0] << 2;
if (lowest_cem != highest_cem)
{
encoded_cem = my_min<uint32_t>(3, 1 + (lowest_cem >> 2));
// See tables at 23.11 Color Endpoint Mode
for (uint32_t j = 0; j < log_block.m_num_partitions; j++)
{
const int M = log_block.m_color_endpoint_modes[j] & 3;
const int C = (log_block.m_color_endpoint_modes[j] >> 2) - ((encoded_cem & 3) - 1);
if ((C & 1) != C)
return false;
encoded_cem |= (C << (2 + j)) | (M << (2 + log_block.m_num_partitions + 2 * j));
}
total_extra_bits = 3 * log_block.m_num_partitions - 4;
if ((total_weight_bits + total_extra_bits) > 128)
return false;
uint32_t cem_bit_pos = 128 - total_weight_bits - total_extra_bits;
astc_set_bits(&phys_block.m_vals[0], cem_bit_pos, encoded_cem >> 6, total_extra_bits);
if (pStats)
pStats->m_header_bits += total_extra_bits;
}
astc_set_bits(&phys_block.m_vals[0], bit_pos, encoded_cem & 0x3f, 6);
if (pStats)
pStats->m_header_bits += 6;
}
else
{