forked from MarkCallow/basis_universal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasisu_bc7enc.cpp
More file actions
1984 lines (1659 loc) · 71.6 KB
/
basisu_bc7enc.cpp
File metadata and controls
1984 lines (1659 loc) · 71.6 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
// File: basisu_bc7enc.cpp
// Copyright (C) 2019-2020 Binomial LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "basisu_bc7enc.h"
#ifdef _DEBUG
#define BC7ENC_CHECK_OVERALL_ERROR 1
#else
#define BC7ENC_CHECK_OVERALL_ERROR 0
#endif
using namespace basist;
namespace basisu
{
// Helpers
static inline color_quad_u8 *color_quad_u8_set_clamped(color_quad_u8 *pRes, int32_t r, int32_t g, int32_t b, int32_t a) { pRes->m_c[0] = (uint8_t)clampi(r, 0, 255); pRes->m_c[1] = (uint8_t)clampi(g, 0, 255); pRes->m_c[2] = (uint8_t)clampi(b, 0, 255); pRes->m_c[3] = (uint8_t)clampi(a, 0, 255); return pRes; }
static inline color_quad_u8 *color_quad_u8_set(color_quad_u8 *pRes, int32_t r, int32_t g, int32_t b, int32_t a) { assert((uint32_t)(r | g | b | a) <= 255); pRes->m_c[0] = (uint8_t)r; pRes->m_c[1] = (uint8_t)g; pRes->m_c[2] = (uint8_t)b; pRes->m_c[3] = (uint8_t)a; return pRes; }
static inline bc7enc_bool color_quad_u8_notequals(const color_quad_u8 *pLHS, const color_quad_u8 *pRHS) { return (pLHS->m_c[0] != pRHS->m_c[0]) || (pLHS->m_c[1] != pRHS->m_c[1]) || (pLHS->m_c[2] != pRHS->m_c[2]) || (pLHS->m_c[3] != pRHS->m_c[3]); }
static inline bc7enc_vec4F*vec4F_set_scalar(bc7enc_vec4F*pV, float x) { pV->m_c[0] = x; pV->m_c[1] = x; pV->m_c[2] = x; pV->m_c[3] = x; return pV; }
static inline bc7enc_vec4F*vec4F_set(bc7enc_vec4F*pV, float x, float y, float z, float w) { pV->m_c[0] = x; pV->m_c[1] = y; pV->m_c[2] = z; pV->m_c[3] = w; return pV; }
static inline bc7enc_vec4F*vec4F_saturate_in_place(bc7enc_vec4F*pV) { pV->m_c[0] = saturate(pV->m_c[0]); pV->m_c[1] = saturate(pV->m_c[1]); pV->m_c[2] = saturate(pV->m_c[2]); pV->m_c[3] = saturate(pV->m_c[3]); return pV; }
static inline bc7enc_vec4F vec4F_saturate(const bc7enc_vec4F*pV) { bc7enc_vec4F res; res.m_c[0] = saturate(pV->m_c[0]); res.m_c[1] = saturate(pV->m_c[1]); res.m_c[2] = saturate(pV->m_c[2]); res.m_c[3] = saturate(pV->m_c[3]); return res; }
static inline bc7enc_vec4F vec4F_from_color(const color_quad_u8 *pC) { bc7enc_vec4F res; vec4F_set(&res, pC->m_c[0], pC->m_c[1], pC->m_c[2], pC->m_c[3]); return res; }
static inline bc7enc_vec4F vec4F_add(const bc7enc_vec4F*pLHS, const bc7enc_vec4F*pRHS) { bc7enc_vec4F res; vec4F_set(&res, pLHS->m_c[0] + pRHS->m_c[0], pLHS->m_c[1] + pRHS->m_c[1], pLHS->m_c[2] + pRHS->m_c[2], pLHS->m_c[3] + pRHS->m_c[3]); return res; }
static inline bc7enc_vec4F vec4F_sub(const bc7enc_vec4F*pLHS, const bc7enc_vec4F*pRHS) { bc7enc_vec4F res; vec4F_set(&res, pLHS->m_c[0] - pRHS->m_c[0], pLHS->m_c[1] - pRHS->m_c[1], pLHS->m_c[2] - pRHS->m_c[2], pLHS->m_c[3] - pRHS->m_c[3]); return res; }
static inline float vec4F_dot(const bc7enc_vec4F*pLHS, const bc7enc_vec4F*pRHS) { return pLHS->m_c[0] * pRHS->m_c[0] + pLHS->m_c[1] * pRHS->m_c[1] + pLHS->m_c[2] * pRHS->m_c[2] + pLHS->m_c[3] * pRHS->m_c[3]; }
static inline bc7enc_vec4F vec4F_mul(const bc7enc_vec4F*pLHS, float s) { bc7enc_vec4F res; vec4F_set(&res, pLHS->m_c[0] * s, pLHS->m_c[1] * s, pLHS->m_c[2] * s, pLHS->m_c[3] * s); return res; }
static inline bc7enc_vec4F* vec4F_normalize_in_place(bc7enc_vec4F*pV) { float s = pV->m_c[0] * pV->m_c[0] + pV->m_c[1] * pV->m_c[1] + pV->m_c[2] * pV->m_c[2] + pV->m_c[3] * pV->m_c[3]; if (s != 0.0f) { s = 1.0f / sqrtf(s); pV->m_c[0] *= s; pV->m_c[1] *= s; pV->m_c[2] *= s; pV->m_c[3] *= s; } return pV; }
// Precomputed weight constants used during least fit determination. For each entry in g_bc7_weights[]: w * w, (1.0f - w) * w, (1.0f - w) * (1.0f - w), w
const float g_bc7_weights1x[2 * 4] = { 0.000000f, 0.000000f, 1.000000f, 0.000000f, 1.000000f, 0.000000f, 0.000000f, 1.000000f };
const float g_bc7_weights2x[4 * 4] = { 0.000000f, 0.000000f, 1.000000f, 0.000000f, 0.107666f, 0.220459f, 0.451416f, 0.328125f, 0.451416f, 0.220459f, 0.107666f, 0.671875f, 1.000000f, 0.000000f, 0.000000f, 1.000000f };
const float g_bc7_weights3x[8 * 4] = { 0.000000f, 0.000000f, 1.000000f, 0.000000f, 0.019775f, 0.120850f, 0.738525f, 0.140625f, 0.079102f, 0.202148f, 0.516602f, 0.281250f, 0.177979f, 0.243896f, 0.334229f, 0.421875f, 0.334229f, 0.243896f, 0.177979f, 0.578125f, 0.516602f, 0.202148f,
0.079102f, 0.718750f, 0.738525f, 0.120850f, 0.019775f, 0.859375f, 1.000000f, 0.000000f, 0.000000f, 1.000000f };
const float g_bc7_weights4x[16 * 4] = { 0.000000f, 0.000000f, 1.000000f, 0.000000f, 0.003906f, 0.058594f, 0.878906f, 0.062500f, 0.019775f, 0.120850f, 0.738525f, 0.140625f, 0.041260f, 0.161865f, 0.635010f, 0.203125f, 0.070557f, 0.195068f, 0.539307f, 0.265625f, 0.107666f, 0.220459f,
0.451416f, 0.328125f, 0.165039f, 0.241211f, 0.352539f, 0.406250f, 0.219727f, 0.249023f, 0.282227f, 0.468750f, 0.282227f, 0.249023f, 0.219727f, 0.531250f, 0.352539f, 0.241211f, 0.165039f, 0.593750f, 0.451416f, 0.220459f, 0.107666f, 0.671875f, 0.539307f, 0.195068f, 0.070557f, 0.734375f,
0.635010f, 0.161865f, 0.041260f, 0.796875f, 0.738525f, 0.120850f, 0.019775f, 0.859375f, 0.878906f, 0.058594f, 0.003906f, 0.937500f, 1.000000f, 0.000000f, 0.000000f, 1.000000f };
const float g_astc_weights4x[16 * 4] = { 0.000000f, 0.000000f, 1.000000f, 0.000000f, 0.003906f, 0.058594f, 0.878906f, 0.062500f, 0.015625f, 0.109375f, 0.765625f, 0.125000f, 0.035156f, 0.152344f, 0.660156f, 0.187500f, 0.070557f, 0.195068f, 0.539307f, 0.265625f, 0.107666f, 0.220459f,
0.451416f, 0.328125f, 0.152588f, 0.238037f, 0.371338f, 0.390625f, 0.205322f, 0.247803f, 0.299072f, 0.453125f, 0.299072f, 0.247803f, 0.205322f, 0.546875f, 0.371338f, 0.238037f, 0.152588f, 0.609375f, 0.451416f, 0.220459f, 0.107666f, 0.671875f, 0.539307f, 0.195068f, 0.070557f, 0.734375f,
0.660156f, 0.152344f, 0.035156f, 0.812500f, 0.765625f, 0.109375f, 0.015625f, 0.875000f, 0.878906f, 0.058594f, 0.003906f, 0.937500f, 1.000000f, 0.000000f, 0.000000f, 1.000000f };
const float g_astc_weights5x[32 * 4] = { 0.000000f, 0.000000f, 1.000000f, 0.000000f, 0.000977f, 0.030273f, 0.938477f, 0.031250f, 0.003906f, 0.058594f, 0.878906f, 0.062500f, 0.008789f, 0.084961f, 0.821289f,
0.093750f, 0.015625f, 0.109375f, 0.765625f, 0.125000f, 0.024414f, 0.131836f, 0.711914f, 0.156250f, 0.035156f, 0.152344f, 0.660156f, 0.187500f, 0.047852f, 0.170898f, 0.610352f, 0.218750f, 0.062500f, 0.187500f,
0.562500f, 0.250000f, 0.079102f, 0.202148f, 0.516602f, 0.281250f, 0.097656f, 0.214844f, 0.472656f, 0.312500f, 0.118164f, 0.225586f, 0.430664f, 0.343750f, 0.140625f, 0.234375f, 0.390625f, 0.375000f, 0.165039f,
0.241211f, 0.352539f, 0.406250f, 0.191406f, 0.246094f, 0.316406f, 0.437500f, 0.219727f, 0.249023f, 0.282227f, 0.468750f, 0.282227f, 0.249023f, 0.219727f, 0.531250f, 0.316406f, 0.246094f, 0.191406f, 0.562500f,
0.352539f, 0.241211f, 0.165039f, 0.593750f, 0.390625f, 0.234375f, 0.140625f, 0.625000f, 0.430664f, 0.225586f, 0.118164f, 0.656250f, 0.472656f, 0.214844f, 0.097656f, 0.687500f, 0.516602f, 0.202148f, 0.079102f,
0.718750f, 0.562500f, 0.187500f, 0.062500f, 0.750000f, 0.610352f, 0.170898f, 0.047852f, 0.781250f, 0.660156f, 0.152344f, 0.035156f, 0.812500f, 0.711914f, 0.131836f, 0.024414f, 0.843750f, 0.765625f, 0.109375f,
0.015625f, 0.875000f, 0.821289f, 0.084961f, 0.008789f, 0.906250f, 0.878906f, 0.058594f, 0.003906f, 0.937500f, 0.938477f, 0.030273f, 0.000977f, 0.968750f, 1.000000f, 0.000000f, 0.000000f, 1.000000f };
const float g_astc_weights_3levelsx[3 * 4] = {
0.000000f, 0.000000f, 1.000000f, 0.000000f,
.5f * .5f, (1.0f - .5f) * .5f, (1.0f - .5f) * (1.0f - .5f), .5f,
1.000000f, 0.000000f, 0.000000f, 1.000000f };
static endpoint_err g_bc7_mode_1_optimal_endpoints[256][2]; // [c][pbit]
static const uint32_t BC7ENC_MODE_1_OPTIMAL_INDEX = 2;
static endpoint_err g_astc_4bit_3bit_optimal_endpoints[256]; // [c]
static const uint32_t BC7ENC_ASTC_4BIT_3BIT_OPTIMAL_INDEX = 2;
static endpoint_err g_astc_4bit_2bit_optimal_endpoints[256]; // [c]
static const uint32_t BC7ENC_ASTC_4BIT_2BIT_OPTIMAL_INDEX = 1;
static endpoint_err g_astc_range7_2bit_optimal_endpoints[256]; // [c]
static const uint32_t BC7ENC_ASTC_RANGE7_2BIT_OPTIMAL_INDEX = 1;
static endpoint_err g_astc_range13_4bit_optimal_endpoints[256]; // [c]
static const uint32_t BC7ENC_ASTC_RANGE13_4BIT_OPTIMAL_INDEX = 2;
static endpoint_err g_astc_range13_2bit_optimal_endpoints[256]; // [c]
static const uint32_t BC7ENC_ASTC_RANGE13_2BIT_OPTIMAL_INDEX = 1;
static endpoint_err g_astc_range11_5bit_optimal_endpoints[256]; // [c]
static const uint32_t BC7ENC_ASTC_RANGE11_5BIT_OPTIMAL_INDEX = 13; // not 1, which is optimal, because 26 losslessly maps to BC7 4-bit weights
astc_quant_bin g_astc_sorted_order_unquant[BC7ENC_TOTAL_ASTC_RANGES][256]; // [sorted unquantized order]
static uint8_t g_astc_nearest_sorted_index[BC7ENC_TOTAL_ASTC_RANGES][256];
static void astc_init()
{
for (uint32_t range = 0; range < BC7ENC_TOTAL_ASTC_RANGES; range++)
{
if (!astc_is_valid_endpoint_range(range))
continue;
const uint32_t levels = astc_get_levels(range);
uint32_t vals[256];
// TODO
for (uint32_t i = 0; i < levels; i++)
vals[i] = (unquant_astc_endpoint_val(i, range) << 8) | i;
std::sort(vals, vals + levels);
for (uint32_t i = 0; i < levels; i++)
{
uint32_t order = vals[i] & 0xFF;
uint32_t unq = vals[i] >> 8;
g_astc_sorted_order_unquant[range][i].m_unquant = (uint8_t)unq;
g_astc_sorted_order_unquant[range][i].m_index = (uint8_t)order;
} // i
#if 0
if (g_astc_bise_range_table[range][1] || g_astc_bise_range_table[range][2])
{
printf("// Range: %u, Levels: %u, Bits: %u, Trits: %u, Quints: %u\n", range, levels, g_astc_bise_range_table[range][0], g_astc_bise_range_table[range][1], g_astc_bise_range_table[range][2]);
printf("{");
for (uint32_t i = 0; i < levels; i++)
{
printf("{%u,%u}", g_astc_sorted_order_unquant[range][i].m_index, g_astc_sorted_order_unquant[range][i].m_unquant);
if (i != (levels - 1))
printf(",");
}
printf("}\n");
}
#endif
#if 0
if (g_astc_bise_range_table[range][1] || g_astc_bise_range_table[range][2])
{
printf("// Range: %u, Levels: %u, Bits: %u, Trits: %u, Quints: %u\n", range, levels, g_astc_bise_range_table[range][0], g_astc_bise_range_table[range][1], g_astc_bise_range_table[range][2]);
printf("{");
for (uint32_t i = 0; i < levels; i++)
{
printf("{%u,%u}", g_astc_unquant[range][i].m_index, g_astc_unquant[range][i].m_unquant);
if (i != (levels - 1))
printf(",");
}
printf("}\n");
}
#endif
for (uint32_t i = 0; i < 256; i++)
{
uint32_t best_index = 0;
int best_err = INT32_MAX;
for (uint32_t j = 0; j < levels; j++)
{
int err = g_astc_sorted_order_unquant[range][j].m_unquant - i;
if (err < 0)
err = -err;
if (err < best_err)
{
best_err = err;
best_index = j;
}
}
g_astc_nearest_sorted_index[range][i] = (uint8_t)best_index;
} // i
} // range
}
static inline uint32_t astc_interpolate(uint32_t l, uint32_t h, uint32_t w)
{
// This is for linear values, not sRGB.
l = (l << 8) | l;
h = (h << 8) | h;
uint32_t k = (l * (64 - w) + h * w + 32) >> 6;
return k >> 8;
}
// Initialize the lookup table used for optimal single color compression in mode 1. Must be called before encoding.
void bc7enc_compress_block_init()
{
astc_init();
// BC7 666.1
for (int c = 0; c < 256; c++)
{
for (uint32_t lp = 0; lp < 2; lp++)
{
endpoint_err best;
best.m_error = (uint16_t)UINT16_MAX;
for (uint32_t l = 0; l < 64; l++)
{
uint32_t low = ((l << 1) | lp) << 1;
low |= (low >> 7);
for (uint32_t h = 0; h < 64; h++)
{
uint32_t high = ((h << 1) | lp) << 1;
high |= (high >> 7);
const int k = (low * (64 - g_bc7_weights3[BC7ENC_MODE_1_OPTIMAL_INDEX]) + high * g_bc7_weights3[BC7ENC_MODE_1_OPTIMAL_INDEX] + 32) >> 6;
const int err = (k - c) * (k - c);
if (err < best.m_error)
{
best.m_error = (uint16_t)err;
best.m_lo = (uint8_t)l;
best.m_hi = (uint8_t)h;
}
} // h
} // l
g_bc7_mode_1_optimal_endpoints[c][lp] = best;
} // lp
} // c
// ASTC [0,15] 3-bit
for (int c = 0; c < 256; c++)
{
endpoint_err best;
best.m_error = (uint16_t)UINT16_MAX;
for (uint32_t l = 0; l < 16; l++)
{
uint32_t low = (l << 4) | l;
for (uint32_t h = 0; h < 16; h++)
{
uint32_t high = (h << 4) | h;
const int k = astc_interpolate(low, high, g_bc7_weights3[BC7ENC_ASTC_4BIT_3BIT_OPTIMAL_INDEX]);
const int err = (k - c) * (k - c);
if (err < best.m_error)
{
best.m_error = (uint16_t)err;
best.m_lo = (uint8_t)l;
best.m_hi = (uint8_t)h;
}
} // h
} // l
g_astc_4bit_3bit_optimal_endpoints[c] = best;
} // c
// ASTC [0,15] 2-bit
for (int c = 0; c < 256; c++)
{
endpoint_err best;
best.m_error = (uint16_t)UINT16_MAX;
for (uint32_t l = 0; l < 16; l++)
{
uint32_t low = (l << 4) | l;
for (uint32_t h = 0; h < 16; h++)
{
uint32_t high = (h << 4) | h;
const int k = astc_interpolate(low, high, g_bc7_weights2[BC7ENC_ASTC_4BIT_2BIT_OPTIMAL_INDEX]);
const int err = (k - c) * (k - c);
if (err < best.m_error)
{
best.m_error = (uint16_t)err;
best.m_lo = (uint8_t)l;
best.m_hi = (uint8_t)h;
}
} // h
} // l
g_astc_4bit_2bit_optimal_endpoints[c] = best;
} // c
// ASTC range 7 [0,11] 2-bit
for (int c = 0; c < 256; c++)
{
endpoint_err best;
best.m_error = (uint16_t)UINT16_MAX;
for (uint32_t l = 0; l < 12; l++)
{
uint32_t low = g_astc_sorted_order_unquant[7][l].m_unquant;
for (uint32_t h = 0; h < 12; h++)
{
uint32_t high = g_astc_sorted_order_unquant[7][h].m_unquant;
const int k = astc_interpolate(low, high, g_bc7_weights2[BC7ENC_ASTC_RANGE7_2BIT_OPTIMAL_INDEX]);
const int err = (k - c) * (k - c);
if (err < best.m_error)
{
best.m_error = (uint16_t)err;
best.m_lo = (uint8_t)l;
best.m_hi = (uint8_t)h;
}
} // h
} // l
g_astc_range7_2bit_optimal_endpoints[c] = best;
} // c
// ASTC range 13 [0,47] 4-bit
for (int c = 0; c < 256; c++)
{
endpoint_err best;
best.m_error = (uint16_t)UINT16_MAX;
for (uint32_t l = 0; l < 48; l++)
{
uint32_t low = g_astc_sorted_order_unquant[13][l].m_unquant;
for (uint32_t h = 0; h < 48; h++)
{
uint32_t high = g_astc_sorted_order_unquant[13][h].m_unquant;
const int k = astc_interpolate(low, high, g_astc_weights4[BC7ENC_ASTC_RANGE13_4BIT_OPTIMAL_INDEX]);
const int err = (k - c) * (k - c);
if (err < best.m_error)
{
best.m_error = (uint16_t)err;
best.m_lo = (uint8_t)l;
best.m_hi = (uint8_t)h;
}
} // h
} // l
g_astc_range13_4bit_optimal_endpoints[c] = best;
} // c
// ASTC range 13 [0,47] 2-bit
for (int c = 0; c < 256; c++)
{
endpoint_err best;
best.m_error = (uint16_t)UINT16_MAX;
for (uint32_t l = 0; l < 48; l++)
{
uint32_t low = g_astc_sorted_order_unquant[13][l].m_unquant;
for (uint32_t h = 0; h < 48; h++)
{
uint32_t high = g_astc_sorted_order_unquant[13][h].m_unquant;
const int k = astc_interpolate(low, high, g_bc7_weights2[BC7ENC_ASTC_RANGE13_2BIT_OPTIMAL_INDEX]);
const int err = (k - c) * (k - c);
if (err < best.m_error)
{
best.m_error = (uint16_t)err;
best.m_lo = (uint8_t)l;
best.m_hi = (uint8_t)h;
}
} // h
} // l
g_astc_range13_2bit_optimal_endpoints[c] = best;
} // c
// ASTC range 11 [0,31] 5-bit
for (int c = 0; c < 256; c++)
{
endpoint_err best;
best.m_error = (uint16_t)UINT16_MAX;
for (uint32_t l = 0; l < 32; l++)
{
uint32_t low = g_astc_sorted_order_unquant[11][l].m_unquant;
for (uint32_t h = 0; h < 32; h++)
{
uint32_t high = g_astc_sorted_order_unquant[11][h].m_unquant;
const int k = astc_interpolate(low, high, g_astc_weights5[BC7ENC_ASTC_RANGE11_5BIT_OPTIMAL_INDEX]);
const int err = (k - c) * (k - c);
if (err < best.m_error)
{
best.m_error = (uint16_t)err;
best.m_lo = (uint8_t)l;
best.m_hi = (uint8_t)h;
}
} // h
} // l
g_astc_range11_5bit_optimal_endpoints[c] = best;
} // c
}
static void compute_least_squares_endpoints_rgba(uint32_t N, const uint8_t *pSelectors, const bc7enc_vec4F* pSelector_weights, bc7enc_vec4F* pXl, bc7enc_vec4F* pXh, const color_quad_u8 *pColors)
{
// Least squares using normal equations: http://www.cs.cornell.edu/~bindel/class/cs3220-s12/notes/lec10.pdf
// I did this in matrix form first, expanded out all the ops, then optimized it a bit.
double z00 = 0.0f, z01 = 0.0f, z10 = 0.0f, z11 = 0.0f;
double q00_r = 0.0f, q10_r = 0.0f, t_r = 0.0f;
double q00_g = 0.0f, q10_g = 0.0f, t_g = 0.0f;
double q00_b = 0.0f, q10_b = 0.0f, t_b = 0.0f;
double q00_a = 0.0f, q10_a = 0.0f, t_a = 0.0f;
for (uint32_t i = 0; i < N; i++)
{
const uint32_t sel = pSelectors[i];
z00 += pSelector_weights[sel].m_c[0];
z10 += pSelector_weights[sel].m_c[1];
z11 += pSelector_weights[sel].m_c[2];
float w = pSelector_weights[sel].m_c[3];
q00_r += w * pColors[i].m_c[0]; t_r += pColors[i].m_c[0];
q00_g += w * pColors[i].m_c[1]; t_g += pColors[i].m_c[1];
q00_b += w * pColors[i].m_c[2]; t_b += pColors[i].m_c[2];
q00_a += w * pColors[i].m_c[3]; t_a += pColors[i].m_c[3];
}
q10_r = t_r - q00_r;
q10_g = t_g - q00_g;
q10_b = t_b - q00_b;
q10_a = t_a - q00_a;
z01 = z10;
double det = z00 * z11 - z01 * z10;
if (det != 0.0f)
det = 1.0f / det;
double iz00, iz01, iz10, iz11;
iz00 = z11 * det;
iz01 = -z01 * det;
iz10 = -z10 * det;
iz11 = z00 * det;
pXl->m_c[0] = (float)(iz00 * q00_r + iz01 * q10_r); pXh->m_c[0] = (float)(iz10 * q00_r + iz11 * q10_r);
pXl->m_c[1] = (float)(iz00 * q00_g + iz01 * q10_g); pXh->m_c[1] = (float)(iz10 * q00_g + iz11 * q10_g);
pXl->m_c[2] = (float)(iz00 * q00_b + iz01 * q10_b); pXh->m_c[2] = (float)(iz10 * q00_b + iz11 * q10_b);
pXl->m_c[3] = (float)(iz00 * q00_a + iz01 * q10_a); pXh->m_c[3] = (float)(iz10 * q00_a + iz11 * q10_a);
for (uint32_t c = 0; c < 4; c++)
{
if ((pXl->m_c[c] < 0.0f) || (pXh->m_c[c] > 255.0f))
{
uint32_t lo_v = UINT32_MAX, hi_v = 0;
for (uint32_t i = 0; i < N; i++)
{
lo_v = minimumu(lo_v, pColors[i].m_c[c]);
hi_v = maximumu(hi_v, pColors[i].m_c[c]);
}
if (lo_v == hi_v)
{
pXl->m_c[c] = (float)lo_v;
pXh->m_c[c] = (float)hi_v;
}
}
}
}
static void compute_least_squares_endpoints_rgb(uint32_t N, const uint8_t *pSelectors, const bc7enc_vec4F*pSelector_weights, bc7enc_vec4F*pXl, bc7enc_vec4F*pXh, const color_quad_u8 *pColors)
{
double z00 = 0.0f, z01 = 0.0f, z10 = 0.0f, z11 = 0.0f;
double q00_r = 0.0f, q10_r = 0.0f, t_r = 0.0f;
double q00_g = 0.0f, q10_g = 0.0f, t_g = 0.0f;
double q00_b = 0.0f, q10_b = 0.0f, t_b = 0.0f;
for (uint32_t i = 0; i < N; i++)
{
const uint32_t sel = pSelectors[i];
z00 += pSelector_weights[sel].m_c[0];
z10 += pSelector_weights[sel].m_c[1];
z11 += pSelector_weights[sel].m_c[2];
float w = pSelector_weights[sel].m_c[3];
q00_r += w * pColors[i].m_c[0]; t_r += pColors[i].m_c[0];
q00_g += w * pColors[i].m_c[1]; t_g += pColors[i].m_c[1];
q00_b += w * pColors[i].m_c[2]; t_b += pColors[i].m_c[2];
}
q10_r = t_r - q00_r;
q10_g = t_g - q00_g;
q10_b = t_b - q00_b;
z01 = z10;
double det = z00 * z11 - z01 * z10;
if (det != 0.0f)
det = 1.0f / det;
double iz00, iz01, iz10, iz11;
iz00 = z11 * det;
iz01 = -z01 * det;
iz10 = -z10 * det;
iz11 = z00 * det;
pXl->m_c[0] = (float)(iz00 * q00_r + iz01 * q10_r); pXh->m_c[0] = (float)(iz10 * q00_r + iz11 * q10_r);
pXl->m_c[1] = (float)(iz00 * q00_g + iz01 * q10_g); pXh->m_c[1] = (float)(iz10 * q00_g + iz11 * q10_g);
pXl->m_c[2] = (float)(iz00 * q00_b + iz01 * q10_b); pXh->m_c[2] = (float)(iz10 * q00_b + iz11 * q10_b);
pXl->m_c[3] = 255.0f; pXh->m_c[3] = 255.0f;
for (uint32_t c = 0; c < 3; c++)
{
if ((pXl->m_c[c] < 0.0f) || (pXh->m_c[c] > 255.0f))
{
uint32_t lo_v = UINT32_MAX, hi_v = 0;
for (uint32_t i = 0; i < N; i++)
{
lo_v = minimumu(lo_v, pColors[i].m_c[c]);
hi_v = maximumu(hi_v, pColors[i].m_c[c]);
}
if (lo_v == hi_v)
{
pXl->m_c[c] = (float)lo_v;
pXh->m_c[c] = (float)hi_v;
}
}
}
}
static inline color_quad_u8 scale_color(const color_quad_u8* pC, const color_cell_compressor_params* pParams)
{
color_quad_u8 results;
if (pParams->m_astc_endpoint_range)
{
for (uint32_t i = 0; i < 4; i++)
{
results.m_c[i] = g_astc_sorted_order_unquant[pParams->m_astc_endpoint_range][pC->m_c[i]].m_unquant;
}
}
else
{
const uint32_t n = pParams->m_comp_bits + (pParams->m_has_pbits ? 1 : 0);
assert((n >= 4) && (n <= 8));
for (uint32_t i = 0; i < 4; i++)
{
uint32_t v = pC->m_c[i] << (8 - n);
v |= (v >> n);
assert(v <= 255);
results.m_c[i] = (uint8_t)(v);
}
}
return results;
}
static inline uint64_t compute_color_distance_rgb(const color_quad_u8 *pE1, const color_quad_u8 *pE2, bc7enc_bool perceptual, const uint32_t weights[4])
{
int dr, dg, db;
if (perceptual)
{
const int l1 = pE1->m_c[0] * 109 + pE1->m_c[1] * 366 + pE1->m_c[2] * 37;
const int cr1 = ((int)pE1->m_c[0] << 9) - l1;
const int cb1 = ((int)pE1->m_c[2] << 9) - l1;
const int l2 = pE2->m_c[0] * 109 + pE2->m_c[1] * 366 + pE2->m_c[2] * 37;
const int cr2 = ((int)pE2->m_c[0] << 9) - l2;
const int cb2 = ((int)pE2->m_c[2] << 9) - l2;
dr = (l1 - l2) >> 8;
dg = (cr1 - cr2) >> 8;
db = (cb1 - cb2) >> 8;
}
else
{
dr = (int)pE1->m_c[0] - (int)pE2->m_c[0];
dg = (int)pE1->m_c[1] - (int)pE2->m_c[1];
db = (int)pE1->m_c[2] - (int)pE2->m_c[2];
}
return weights[0] * (uint32_t)(dr * dr) + weights[1] * (uint32_t)(dg * dg) + weights[2] * (uint32_t)(db * db);
}
static inline uint64_t compute_color_distance_rgba(const color_quad_u8 *pE1, const color_quad_u8 *pE2, bc7enc_bool perceptual, const uint32_t weights[4])
{
int da = (int)pE1->m_c[3] - (int)pE2->m_c[3];
return compute_color_distance_rgb(pE1, pE2, perceptual, weights) + (weights[3] * (uint32_t)(da * da));
}
static uint64_t pack_mode1_to_one_color(const color_cell_compressor_params *pParams, color_cell_compressor_results *pResults, uint32_t r, uint32_t g, uint32_t b, uint8_t *pSelectors)
{
uint32_t best_err = UINT_MAX;
uint32_t best_p = 0;
for (uint32_t p = 0; p < 2; p++)
{
uint32_t err = g_bc7_mode_1_optimal_endpoints[r][p].m_error + g_bc7_mode_1_optimal_endpoints[g][p].m_error + g_bc7_mode_1_optimal_endpoints[b][p].m_error;
if (err < best_err)
{
best_err = err;
best_p = p;
}
}
const endpoint_err *pEr = &g_bc7_mode_1_optimal_endpoints[r][best_p];
const endpoint_err *pEg = &g_bc7_mode_1_optimal_endpoints[g][best_p];
const endpoint_err *pEb = &g_bc7_mode_1_optimal_endpoints[b][best_p];
color_quad_u8_set(&pResults->m_low_endpoint, pEr->m_lo, pEg->m_lo, pEb->m_lo, 0);
color_quad_u8_set(&pResults->m_high_endpoint, pEr->m_hi, pEg->m_hi, pEb->m_hi, 0);
pResults->m_pbits[0] = best_p;
pResults->m_pbits[1] = 0;
memset(pSelectors, BC7ENC_MODE_1_OPTIMAL_INDEX, pParams->m_num_pixels);
color_quad_u8 p;
for (uint32_t i = 0; i < 3; i++)
{
uint32_t low = ((pResults->m_low_endpoint.m_c[i] << 1) | pResults->m_pbits[0]) << 1;
low |= (low >> 7);
uint32_t high = ((pResults->m_high_endpoint.m_c[i] << 1) | pResults->m_pbits[0]) << 1;
high |= (high >> 7);
p.m_c[i] = (uint8_t)((low * (64 - g_bc7_weights3[BC7ENC_MODE_1_OPTIMAL_INDEX]) + high * g_bc7_weights3[BC7ENC_MODE_1_OPTIMAL_INDEX] + 32) >> 6);
}
p.m_c[3] = 255;
uint64_t total_err = 0;
for (uint32_t i = 0; i < pParams->m_num_pixels; i++)
total_err += compute_color_distance_rgb(&p, &pParams->m_pPixels[i], pParams->m_perceptual, pParams->m_weights);
pResults->m_best_overall_err = total_err;
return total_err;
}
static uint64_t pack_astc_4bit_3bit_to_one_color(const color_cell_compressor_params *pParams, color_cell_compressor_results *pResults, uint32_t r, uint32_t g, uint32_t b, uint8_t *pSelectors)
{
const endpoint_err *pEr = &g_astc_4bit_3bit_optimal_endpoints[r];
const endpoint_err *pEg = &g_astc_4bit_3bit_optimal_endpoints[g];
const endpoint_err *pEb = &g_astc_4bit_3bit_optimal_endpoints[b];
color_quad_u8_set(&pResults->m_low_endpoint, pEr->m_lo, pEg->m_lo, pEb->m_lo, 0);
color_quad_u8_set(&pResults->m_high_endpoint, pEr->m_hi, pEg->m_hi, pEb->m_hi, 0);
pResults->m_pbits[0] = 0;
pResults->m_pbits[1] = 0;
for (uint32_t i = 0; i < 4; i++)
{
pResults->m_astc_low_endpoint.m_c[i] = g_astc_sorted_order_unquant[pParams->m_astc_endpoint_range][pResults->m_low_endpoint.m_c[i]].m_index;
pResults->m_astc_high_endpoint.m_c[i] = g_astc_sorted_order_unquant[pParams->m_astc_endpoint_range][pResults->m_high_endpoint.m_c[i]].m_index;
}
memset(pSelectors, BC7ENC_ASTC_4BIT_3BIT_OPTIMAL_INDEX, pParams->m_num_pixels);
color_quad_u8 p;
for (uint32_t i = 0; i < 3; i++)
{
uint32_t low = (pResults->m_low_endpoint.m_c[i] << 4) | pResults->m_low_endpoint.m_c[i];
uint32_t high = (pResults->m_high_endpoint.m_c[i] << 4) | pResults->m_high_endpoint.m_c[i];
p.m_c[i] = (uint8_t)astc_interpolate(low, high, g_bc7_weights3[BC7ENC_ASTC_4BIT_3BIT_OPTIMAL_INDEX]);
}
p.m_c[3] = 255;
uint64_t total_err = 0;
for (uint32_t i = 0; i < pParams->m_num_pixels; i++)
total_err += compute_color_distance_rgb(&p, &pParams->m_pPixels[i], pParams->m_perceptual, pParams->m_weights);
pResults->m_best_overall_err = total_err;
return total_err;
}
static uint64_t pack_astc_4bit_2bit_to_one_color_rgba(const color_cell_compressor_params *pParams, color_cell_compressor_results *pResults, uint32_t r, uint32_t g, uint32_t b, uint32_t a, uint8_t *pSelectors)
{
const endpoint_err *pEr = &g_astc_4bit_2bit_optimal_endpoints[r];
const endpoint_err *pEg = &g_astc_4bit_2bit_optimal_endpoints[g];
const endpoint_err *pEb = &g_astc_4bit_2bit_optimal_endpoints[b];
const endpoint_err *pEa = &g_astc_4bit_2bit_optimal_endpoints[a];
color_quad_u8_set(&pResults->m_low_endpoint, pEr->m_lo, pEg->m_lo, pEb->m_lo, pEa->m_lo);
color_quad_u8_set(&pResults->m_high_endpoint, pEr->m_hi, pEg->m_hi, pEb->m_hi, pEa->m_hi);
pResults->m_pbits[0] = 0;
pResults->m_pbits[1] = 0;
for (uint32_t i = 0; i < 4; i++)
{
pResults->m_astc_low_endpoint.m_c[i] = g_astc_sorted_order_unquant[pParams->m_astc_endpoint_range][pResults->m_low_endpoint.m_c[i]].m_index;
pResults->m_astc_high_endpoint.m_c[i] = g_astc_sorted_order_unquant[pParams->m_astc_endpoint_range][pResults->m_high_endpoint.m_c[i]].m_index;
}
memset(pSelectors, BC7ENC_ASTC_4BIT_2BIT_OPTIMAL_INDEX, pParams->m_num_pixels);
color_quad_u8 p;
for (uint32_t i = 0; i < 4; i++)
{
uint32_t low = (pResults->m_low_endpoint.m_c[i] << 4) | pResults->m_low_endpoint.m_c[i];
uint32_t high = (pResults->m_high_endpoint.m_c[i] << 4) | pResults->m_high_endpoint.m_c[i];
p.m_c[i] = (uint8_t)astc_interpolate(low, high, g_bc7_weights2[BC7ENC_ASTC_4BIT_2BIT_OPTIMAL_INDEX]);
}
uint64_t total_err = 0;
for (uint32_t i = 0; i < pParams->m_num_pixels; i++)
total_err += compute_color_distance_rgba(&p, &pParams->m_pPixels[i], pParams->m_perceptual, pParams->m_weights);
pResults->m_best_overall_err = total_err;
return total_err;
}
static uint64_t pack_astc_range7_2bit_to_one_color(const color_cell_compressor_params *pParams, color_cell_compressor_results *pResults, uint32_t r, uint32_t g, uint32_t b, uint8_t *pSelectors)
{
assert(pParams->m_astc_endpoint_range == 7 && pParams->m_num_selector_weights == 4);
const endpoint_err *pEr = &g_astc_range7_2bit_optimal_endpoints[r];
const endpoint_err *pEg = &g_astc_range7_2bit_optimal_endpoints[g];
const endpoint_err *pEb = &g_astc_range7_2bit_optimal_endpoints[b];
color_quad_u8_set(&pResults->m_low_endpoint, pEr->m_lo, pEg->m_lo, pEb->m_lo, 0);
color_quad_u8_set(&pResults->m_high_endpoint, pEr->m_hi, pEg->m_hi, pEb->m_hi, 0);
pResults->m_pbits[0] = 0;
pResults->m_pbits[1] = 0;
for (uint32_t i = 0; i < 4; i++)
{
pResults->m_astc_low_endpoint.m_c[i] = g_astc_sorted_order_unquant[pParams->m_astc_endpoint_range][pResults->m_low_endpoint.m_c[i]].m_index;
pResults->m_astc_high_endpoint.m_c[i] = g_astc_sorted_order_unquant[pParams->m_astc_endpoint_range][pResults->m_high_endpoint.m_c[i]].m_index;
}
memset(pSelectors, BC7ENC_ASTC_RANGE7_2BIT_OPTIMAL_INDEX, pParams->m_num_pixels);
color_quad_u8 p;
for (uint32_t i = 0; i < 3; i++)
{
uint32_t low = g_astc_sorted_order_unquant[7][pResults->m_low_endpoint.m_c[i]].m_unquant;
uint32_t high = g_astc_sorted_order_unquant[7][pResults->m_high_endpoint.m_c[i]].m_unquant;
p.m_c[i] = (uint8_t)astc_interpolate(low, high, g_bc7_weights2[BC7ENC_ASTC_RANGE7_2BIT_OPTIMAL_INDEX]);
}
p.m_c[3] = 255;
uint64_t total_err = 0;
for (uint32_t i = 0; i < pParams->m_num_pixels; i++)
total_err += compute_color_distance_rgb(&p, &pParams->m_pPixels[i], pParams->m_perceptual, pParams->m_weights);
pResults->m_best_overall_err = total_err;
return total_err;
}
static uint64_t pack_astc_range13_2bit_to_one_color(const color_cell_compressor_params *pParams, color_cell_compressor_results *pResults, uint32_t r, uint32_t g, uint32_t b, uint8_t *pSelectors)
{
assert(pParams->m_astc_endpoint_range == 13 && pParams->m_num_selector_weights == 4 && !pParams->m_has_alpha);
const endpoint_err *pEr = &g_astc_range13_2bit_optimal_endpoints[r];
const endpoint_err *pEg = &g_astc_range13_2bit_optimal_endpoints[g];
const endpoint_err *pEb = &g_astc_range13_2bit_optimal_endpoints[b];
color_quad_u8_set(&pResults->m_low_endpoint, pEr->m_lo, pEg->m_lo, pEb->m_lo, 47);
color_quad_u8_set(&pResults->m_high_endpoint, pEr->m_hi, pEg->m_hi, pEb->m_hi, 47);
pResults->m_pbits[0] = 0;
pResults->m_pbits[1] = 0;
for (uint32_t i = 0; i < 4; i++)
{
pResults->m_astc_low_endpoint.m_c[i] = g_astc_sorted_order_unquant[pParams->m_astc_endpoint_range][pResults->m_low_endpoint.m_c[i]].m_index;
pResults->m_astc_high_endpoint.m_c[i] = g_astc_sorted_order_unquant[pParams->m_astc_endpoint_range][pResults->m_high_endpoint.m_c[i]].m_index;
}
memset(pSelectors, BC7ENC_ASTC_RANGE13_2BIT_OPTIMAL_INDEX, pParams->m_num_pixels);
color_quad_u8 p;
for (uint32_t i = 0; i < 4; i++)
{
uint32_t low = g_astc_sorted_order_unquant[13][pResults->m_low_endpoint.m_c[i]].m_unquant;
uint32_t high = g_astc_sorted_order_unquant[13][pResults->m_high_endpoint.m_c[i]].m_unquant;
p.m_c[i] = (uint8_t)astc_interpolate(low, high, g_bc7_weights2[BC7ENC_ASTC_RANGE13_2BIT_OPTIMAL_INDEX]);
}
uint64_t total_err = 0;
for (uint32_t i = 0; i < pParams->m_num_pixels; i++)
total_err += compute_color_distance_rgb(&p, &pParams->m_pPixels[i], pParams->m_perceptual, pParams->m_weights);
pResults->m_best_overall_err = total_err;
return total_err;
}
static uint64_t pack_astc_range11_5bit_to_one_color(const color_cell_compressor_params* pParams, color_cell_compressor_results* pResults, uint32_t r, uint32_t g, uint32_t b, uint8_t* pSelectors)
{
assert(pParams->m_astc_endpoint_range == 11 && pParams->m_num_selector_weights == 32 && !pParams->m_has_alpha);
const endpoint_err* pEr = &g_astc_range11_5bit_optimal_endpoints[r];
const endpoint_err* pEg = &g_astc_range11_5bit_optimal_endpoints[g];
const endpoint_err* pEb = &g_astc_range11_5bit_optimal_endpoints[b];
color_quad_u8_set(&pResults->m_low_endpoint, pEr->m_lo, pEg->m_lo, pEb->m_lo, 31);
color_quad_u8_set(&pResults->m_high_endpoint, pEr->m_hi, pEg->m_hi, pEb->m_hi, 31);
pResults->m_pbits[0] = 0;
pResults->m_pbits[1] = 0;
for (uint32_t i = 0; i < 4; i++)
{
pResults->m_astc_low_endpoint.m_c[i] = g_astc_sorted_order_unquant[pParams->m_astc_endpoint_range][pResults->m_low_endpoint.m_c[i]].m_index;
pResults->m_astc_high_endpoint.m_c[i] = g_astc_sorted_order_unquant[pParams->m_astc_endpoint_range][pResults->m_high_endpoint.m_c[i]].m_index;
}
memset(pSelectors, BC7ENC_ASTC_RANGE11_5BIT_OPTIMAL_INDEX, pParams->m_num_pixels);
color_quad_u8 p;
for (uint32_t i = 0; i < 4; i++)
{
uint32_t low = g_astc_sorted_order_unquant[11][pResults->m_low_endpoint.m_c[i]].m_unquant;
uint32_t high = g_astc_sorted_order_unquant[11][pResults->m_high_endpoint.m_c[i]].m_unquant;
p.m_c[i] = (uint8_t)astc_interpolate(low, high, g_astc_weights5[BC7ENC_ASTC_RANGE11_5BIT_OPTIMAL_INDEX]);
}
uint64_t total_err = 0;
for (uint32_t i = 0; i < pParams->m_num_pixels; i++)
total_err += compute_color_distance_rgb(&p, &pParams->m_pPixels[i], pParams->m_perceptual, pParams->m_weights);
pResults->m_best_overall_err = total_err;
return total_err;
}
static uint64_t evaluate_solution(const color_quad_u8 *pLow, const color_quad_u8 *pHigh, const uint32_t pbits[2], const color_cell_compressor_params *pParams, color_cell_compressor_results *pResults)
{
color_quad_u8 quantMinColor = *pLow;
color_quad_u8 quantMaxColor = *pHigh;
if (pParams->m_has_pbits)
{
uint32_t minPBit, maxPBit;
if (pParams->m_endpoints_share_pbit)
maxPBit = minPBit = pbits[0];
else
{
minPBit = pbits[0];
maxPBit = pbits[1];
}
quantMinColor.m_c[0] = (uint8_t)((pLow->m_c[0] << 1) | minPBit);
quantMinColor.m_c[1] = (uint8_t)((pLow->m_c[1] << 1) | minPBit);
quantMinColor.m_c[2] = (uint8_t)((pLow->m_c[2] << 1) | minPBit);
quantMinColor.m_c[3] = (uint8_t)((pLow->m_c[3] << 1) | minPBit);
quantMaxColor.m_c[0] = (uint8_t)((pHigh->m_c[0] << 1) | maxPBit);
quantMaxColor.m_c[1] = (uint8_t)((pHigh->m_c[1] << 1) | maxPBit);
quantMaxColor.m_c[2] = (uint8_t)((pHigh->m_c[2] << 1) | maxPBit);
quantMaxColor.m_c[3] = (uint8_t)((pHigh->m_c[3] << 1) | maxPBit);
}
color_quad_u8 actualMinColor = scale_color(&quantMinColor, pParams);
color_quad_u8 actualMaxColor = scale_color(&quantMaxColor, pParams);
const uint32_t N = pParams->m_num_selector_weights;
assert(N >= 1 && N <= 32);
color_quad_u8 weightedColors[32];
weightedColors[0] = actualMinColor;
weightedColors[N - 1] = actualMaxColor;
const uint32_t nc = pParams->m_has_alpha ? 4 : 3;
if (pParams->m_astc_endpoint_range)
{
for (uint32_t i = 1; i < (N - 1); i++)
{
for (uint32_t j = 0; j < nc; j++)
weightedColors[i].m_c[j] = (uint8_t)(astc_interpolate(actualMinColor.m_c[j], actualMaxColor.m_c[j], pParams->m_pSelector_weights[i]));
}
}
else
{
for (uint32_t i = 1; i < (N - 1); i++)
for (uint32_t j = 0; j < nc; j++)
weightedColors[i].m_c[j] = (uint8_t)((actualMinColor.m_c[j] * (64 - pParams->m_pSelector_weights[i]) + actualMaxColor.m_c[j] * pParams->m_pSelector_weights[i] + 32) >> 6);
}
const int lr = actualMinColor.m_c[0];
const int lg = actualMinColor.m_c[1];
const int lb = actualMinColor.m_c[2];
const int dr = actualMaxColor.m_c[0] - lr;
const int dg = actualMaxColor.m_c[1] - lg;
const int db = actualMaxColor.m_c[2] - lb;
uint64_t total_err = 0;
if (pParams->m_pForce_selectors)
{
for (uint32_t i = 0; i < pParams->m_num_pixels; i++)
{
const color_quad_u8* pC = &pParams->m_pPixels[i];
const uint8_t sel = pParams->m_pForce_selectors[i];
assert(sel < N);
total_err += (pParams->m_has_alpha ? compute_color_distance_rgba : compute_color_distance_rgb)(&weightedColors[sel], pC, pParams->m_perceptual, pParams->m_weights);
pResults->m_pSelectors_temp[i] = sel;
}
}
else if (!pParams->m_perceptual)
{
if (pParams->m_has_alpha)
{
const int la = actualMinColor.m_c[3];
const int da = actualMaxColor.m_c[3] - la;
const float f = N / (float)(squarei(dr) + squarei(dg) + squarei(db) + squarei(da) + .00000125f);
for (uint32_t i = 0; i < pParams->m_num_pixels; i++)
{
const color_quad_u8 *pC = &pParams->m_pPixels[i];
int r = pC->m_c[0];
int g = pC->m_c[1];
int b = pC->m_c[2];
int a = pC->m_c[3];
int best_sel = (int)((float)((r - lr) * dr + (g - lg) * dg + (b - lb) * db + (a - la) * da) * f + .5f);
best_sel = clampi(best_sel, 1, N - 1);
uint64_t err0 = compute_color_distance_rgba(&weightedColors[best_sel - 1], pC, BC7ENC_FALSE, pParams->m_weights);
uint64_t err1 = compute_color_distance_rgba(&weightedColors[best_sel], pC, BC7ENC_FALSE, pParams->m_weights);
if (err0 == err1)
{
// Prefer non-interpolation
if ((best_sel - 1) == 0)
best_sel = 0;
}
else if (err1 > err0)
{
err1 = err0;
--best_sel;
}
total_err += err1;
pResults->m_pSelectors_temp[i] = (uint8_t)best_sel;
}
}
else
{
const float f = N / (float)(squarei(dr) + squarei(dg) + squarei(db) + .00000125f);
for (uint32_t i = 0; i < pParams->m_num_pixels; i++)
{
const color_quad_u8 *pC = &pParams->m_pPixels[i];
int r = pC->m_c[0];
int g = pC->m_c[1];
int b = pC->m_c[2];
int sel = (int)((float)((r - lr) * dr + (g - lg) * dg + (b - lb) * db) * f + .5f);
sel = clampi(sel, 1, N - 1);
uint64_t err0 = compute_color_distance_rgb(&weightedColors[sel - 1], pC, BC7ENC_FALSE, pParams->m_weights);
uint64_t err1 = compute_color_distance_rgb(&weightedColors[sel], pC, BC7ENC_FALSE, pParams->m_weights);
int best_sel = sel;
uint64_t best_err = err1;
if (err0 == err1)
{
// Prefer non-interpolation
if ((best_sel - 1) == 0)
best_sel = 0;
}
else if (err0 < best_err)
{
best_err = err0;
best_sel = sel - 1;
}
total_err += best_err;
pResults->m_pSelectors_temp[i] = (uint8_t)best_sel;
}
}
}
else
{
for (uint32_t i = 0; i < pParams->m_num_pixels; i++)
{
uint64_t best_err = UINT64_MAX;
uint32_t best_sel = 0;
if (pParams->m_has_alpha)
{
for (uint32_t j = 0; j < N; j++)
{
uint64_t err = compute_color_distance_rgba(&weightedColors[j], &pParams->m_pPixels[i], BC7ENC_TRUE, pParams->m_weights);
if (err < best_err)
{
best_err = err;
best_sel = j;
}
// Prefer non-interpolation
else if ((err == best_err) && (j == (N - 1)))
best_sel = j;
}
}
else
{
for (uint32_t j = 0; j < N; j++)
{