-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
bc7enc.cpp
4126 lines (3534 loc) · 159 KB
/
bc7enc.cpp
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: bc7enc.c - Richard Geldreich, Jr. 3/31/2020 - MIT license or public domain (see end of file)
// Currently supports modes 1, 6 for RGB blocks, and modes 5, 6, 7 for RGBA blocks.
#include "bc7enc.h"
#include <bit>
#include <math.h>
#include <memory.h>
#include <assert.h>
#include <limits.h>
#include <algorithm>
#if defined __SSE4_1__ || defined __AVX2__ || defined _MSC_VER
# ifdef _MSC_VER
# include <intrin.h>
# include <Windows.h>
# define _bswap(x) _byteswap_ulong(x)
# define _bswap64(x) _byteswap_uint64(x)
# else
# include <x86intrin.h>
# endif
#endif
// Helpers
static inline int32_t clampi(int32_t value, int32_t low, int32_t high) { if (value < low) value = low; else if (value > high) value = high; return value; }
static inline float clampf(float value, float low, float high) { if (value < low) value = low; else if (value > high) value = high; return value; }
static inline float saturate(float value) { return clampf(value, 0, 1.0f); }
static inline uint8_t minimumub(uint8_t a, uint8_t b) { return (a < b) ? a : b; }
static inline int32_t minimumi(int32_t a, int32_t b) { return (a < b) ? a : b; }
static inline uint32_t minimumu(uint32_t a, uint32_t b) { return (a < b) ? a : b; }
static inline float minimumf(float a, float b) { return (a < b) ? a : b; }
static inline uint8_t maximumub(uint8_t a, uint8_t b) { return (a > b) ? a : b; }
static inline uint32_t maximumu(uint32_t a, uint32_t b) { return (a > b) ? a : b; }
static inline int32_t maximumi(int32_t a, int32_t b) { return (a > b) ? a : b; }
static inline float maximumf(float a, float b) { return (a > b) ? a : b; }
static inline int squarei(int i) { return i * i; }
static inline float squaref(float i) { return i * i; }
template <typename T0, typename T1> inline T0 lerp(T0 a, T0 b, T1 c) { return a + (b - a) * c; }
static inline int32_t iabs32(int32_t v) { uint32_t msk = v >> 31; return (v ^ msk) - msk; }
static inline void swapub(uint8_t* a, uint8_t* b) { uint8_t t = *a; *a = *b; *b = t; }
static inline void swapu(uint32_t* a, uint32_t* b) { uint32_t t = *a; *a = *b; *b = t; }
static inline void swapf(float* a, float* b) { float t = *a; *a = *b; *b = t; }
struct vec4F { float m_c[4]; };
static inline color_rgba *color_quad_u8_set_clamped(color_rgba *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_rgba *color_quad_u8_set(color_rgba *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 bool color_quad_u8_notequals(const color_rgba *pLHS, const color_rgba *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 vec4F *vec4F_set_scalar(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 vec4F *vec4F_set(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 vec4F *vec4F_saturate_in_place(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 vec4F vec4F_saturate(const vec4F *pV) { 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 vec4F vec4F_from_color(const color_rgba *pC) { 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 vec4F vec4F_add(const vec4F *pLHS, const vec4F *pRHS) { 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 vec4F vec4F_sub(const vec4F *pLHS, const vec4F *pRHS) { 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 vec4F *pLHS, const 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 vec4F vec4F_mul(const vec4F *pLHS, float s) { 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 vec4F *vec4F_normalize_in_place(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; }
// Various BC7 tables
static const uint32_t g_bc7_weights2[4] = { 0, 21, 43, 64 };
static const uint32_t g_bc7_weights3[8] = { 0, 9, 18, 27, 37, 46, 55, 64 };
static const uint32_t g_bc7_weights4[16] = { 0, 4, 9, 13, 17, 21, 26, 30, 34, 38, 43, 47, 51, 55, 60, 64 };
static const uint16_t g_bc7_weights2_16[4] = { 0, 21, 43, 64 };
static const uint16_t g_bc7_weights3_16[8] = { 0, 9, 18, 27, 37, 46, 55, 64 };
static const uint16_t g_bc7_weights4_16[16] = { 0, 4, 9, 13, 17, 21, 26, 30, 34, 38, 43, 47, 51, 55, 60, 64 };
// 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
static 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 };
static 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 };
static 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 };
static const uint8_t g_bc7_partition1[16] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
static const uint8_t g_bc7_partition2[64 * 16] =
{
0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1, 0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1, 0,1,1,1,0,1,1,1,0,1,1,1,0,1,1,1, 0,0,0,1,0,0,1,1,0,0,1,1,0,1,1,1, 0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,1, 0,0,1,1,0,1,1,1,0,1,1,1,1,1,1,1, 0,0,0,1,0,0,1,1,0,1,1,1,1,1,1,1, 0,0,0,0,0,0,0,1,0,0,1,1,0,1,1,1,
0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,1, 0,0,1,1,0,1,1,1,1,1,1,1,1,1,1,1, 0,0,0,0,0,0,0,1,0,1,1,1,1,1,1,1, 0,0,0,0,0,0,0,0,0,0,0,1,0,1,1,1, 0,0,0,1,0,1,1,1,1,1,1,1,1,1,1,1, 0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1, 0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1, 0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,
0,0,0,0,1,0,0,0,1,1,1,0,1,1,1,1, 0,1,1,1,0,0,0,1,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,1,0,0,0,1,1,1,0, 0,1,1,1,0,0,1,1,0,0,0,1,0,0,0,0, 0,0,1,1,0,0,0,1,0,0,0,0,0,0,0,0, 0,0,0,0,1,0,0,0,1,1,0,0,1,1,1,0, 0,0,0,0,0,0,0,0,1,0,0,0,1,1,0,0, 0,1,1,1,0,0,1,1,0,0,1,1,0,0,0,1,
0,0,1,1,0,0,0,1,0,0,0,1,0,0,0,0, 0,0,0,0,1,0,0,0,1,0,0,0,1,1,0,0, 0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0, 0,0,1,1,0,1,1,0,0,1,1,0,1,1,0,0, 0,0,0,1,0,1,1,1,1,1,1,0,1,0,0,0, 0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0, 0,1,1,1,0,0,0,1,1,0,0,0,1,1,1,0, 0,0,1,1,1,0,0,1,1,0,0,1,1,1,0,0,
0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1, 0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1, 0,1,0,1,1,0,1,0,0,1,0,1,1,0,1,0, 0,0,1,1,0,0,1,1,1,1,0,0,1,1,0,0, 0,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0, 0,1,0,1,0,1,0,1,1,0,1,0,1,0,1,0, 0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1, 0,1,0,1,1,0,1,0,1,0,1,0,0,1,0,1,
0,1,1,1,0,0,1,1,1,1,0,0,1,1,1,0, 0,0,0,1,0,0,1,1,1,1,0,0,1,0,0,0, 0,0,1,1,0,0,1,0,0,1,0,0,1,1,0,0, 0,0,1,1,1,0,1,1,1,1,0,1,1,1,0,0, 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0, 0,0,1,1,1,1,0,0,1,1,0,0,0,0,1,1, 0,1,1,0,0,1,1,0,1,0,0,1,1,0,0,1, 0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0,
0,1,0,0,1,1,1,0,0,1,0,0,0,0,0,0, 0,0,1,0,0,1,1,1,0,0,1,0,0,0,0,0, 0,0,0,0,0,0,1,0,0,1,1,1,0,0,1,0, 0,0,0,0,0,1,0,0,1,1,1,0,0,1,0,0, 0,1,1,0,1,1,0,0,1,0,0,1,0,0,1,1, 0,0,1,1,0,1,1,0,1,1,0,0,1,0,0,1, 0,1,1,0,0,0,1,1,1,0,0,1,1,1,0,0, 0,0,1,1,1,0,0,1,1,1,0,0,0,1,1,0,
0,1,1,0,1,1,0,0,1,1,0,0,1,0,0,1, 0,1,1,0,0,0,1,1,0,0,1,1,1,0,0,1, 0,1,1,1,1,1,1,0,1,0,0,0,0,0,0,1, 0,0,0,1,1,0,0,0,1,1,1,0,0,1,1,1, 0,0,0,0,1,1,1,1,0,0,1,1,0,0,1,1, 0,0,1,1,0,0,1,1,1,1,1,1,0,0,0,0, 0,0,1,0,0,0,1,0,1,1,1,0,1,1,1,0, 0,1,0,0,0,1,0,0,0,1,1,1,0,1,1,1
};
#ifdef __AVX512F__
static const __mmask16 g_bc7_partition2_mask[64] = {
0xcccc, 0x8888, 0xeeee, 0xecc8, 0xc880, 0xfeec, 0xfec8, 0xec80, 0xc800, 0xffec, 0xfe80, 0xe800, 0xffe8, 0xff00, 0xfff0, 0xf000,
0xf710, 0x008e, 0x7100, 0x08ce, 0x008c, 0x7310, 0x3100, 0x8cce, 0x088c, 0x3110, 0x6666, 0x366c, 0x17e8, 0x0ff0, 0x718e, 0x399c,
0xaaaa, 0xf0f0, 0x5a5a, 0x33cc, 0x3c3c, 0x55aa, 0x9696, 0xa55a, 0x73ce, 0x13c8, 0x324c, 0x3bdc, 0x6996, 0xc33c, 0x9966, 0x0660,
0x0272, 0x04e4, 0x4e40, 0x2720, 0xc936, 0x936c, 0x39c6, 0x639c, 0x9336, 0x9cc6, 0x817e, 0xe718, 0xccf0, 0x0fcc, 0x7744, 0xee22
};
#endif
static const uint8_t g_bc7_partition3[64 * 16] =
{
0,0,1,1,0,0,1,1,0,2,2,1,2,2,2,2, 0,0,0,1,0,0,1,1,2,2,1,1,2,2,2,1, 0,0,0,0,2,0,0,1,2,2,1,1,2,2,1,1, 0,2,2,2,0,0,2,2,0,0,1,1,0,1,1,1, 0,0,0,0,0,0,0,0,1,1,2,2,1,1,2,2, 0,0,1,1,0,0,1,1,0,0,2,2,0,0,2,2, 0,0,2,2,0,0,2,2,1,1,1,1,1,1,1,1, 0,0,1,1,0,0,1,1,2,2,1,1,2,2,1,1,
0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2, 0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2, 0,0,0,0,1,1,1,1,2,2,2,2,2,2,2,2, 0,0,1,2,0,0,1,2,0,0,1,2,0,0,1,2, 0,1,1,2,0,1,1,2,0,1,1,2,0,1,1,2, 0,1,2,2,0,1,2,2,0,1,2,2,0,1,2,2, 0,0,1,1,0,1,1,2,1,1,2,2,1,2,2,2, 0,0,1,1,2,0,0,1,2,2,0,0,2,2,2,0,
0,0,0,1,0,0,1,1,0,1,1,2,1,1,2,2, 0,1,1,1,0,0,1,1,2,0,0,1,2,2,0,0, 0,0,0,0,1,1,2,2,1,1,2,2,1,1,2,2, 0,0,2,2,0,0,2,2,0,0,2,2,1,1,1,1, 0,1,1,1,0,1,1,1,0,2,2,2,0,2,2,2, 0,0,0,1,0,0,0,1,2,2,2,1,2,2,2,1, 0,0,0,0,0,0,1,1,0,1,2,2,0,1,2,2, 0,0,0,0,1,1,0,0,2,2,1,0,2,2,1,0,
0,1,2,2,0,1,2,2,0,0,1,1,0,0,0,0, 0,0,1,2,0,0,1,2,1,1,2,2,2,2,2,2, 0,1,1,0,1,2,2,1,1,2,2,1,0,1,1,0, 0,0,0,0,0,1,1,0,1,2,2,1,1,2,2,1, 0,0,2,2,1,1,0,2,1,1,0,2,0,0,2,2, 0,1,1,0,0,1,1,0,2,0,0,2,2,2,2,2, 0,0,1,1,0,1,2,2,0,1,2,2,0,0,1,1, 0,0,0,0,2,0,0,0,2,2,1,1,2,2,2,1,
0,0,0,0,0,0,0,2,1,1,2,2,1,2,2,2, 0,2,2,2,0,0,2,2,0,0,1,2,0,0,1,1, 0,0,1,1,0,0,1,2,0,0,2,2,0,2,2,2, 0,1,2,0,0,1,2,0,0,1,2,0,0,1,2,0, 0,0,0,0,1,1,1,1,2,2,2,2,0,0,0,0, 0,1,2,0,1,2,0,1,2,0,1,2,0,1,2,0, 0,1,2,0,2,0,1,2,1,2,0,1,0,1,2,0, 0,0,1,1,2,2,0,0,1,1,2,2,0,0,1,1,
0,0,1,1,1,1,2,2,2,2,0,0,0,0,1,1, 0,1,0,1,0,1,0,1,2,2,2,2,2,2,2,2, 0,0,0,0,0,0,0,0,2,1,2,1,2,1,2,1, 0,0,2,2,1,1,2,2,0,0,2,2,1,1,2,2, 0,0,2,2,0,0,1,1,0,0,2,2,0,0,1,1, 0,2,2,0,1,2,2,1,0,2,2,0,1,2,2,1, 0,1,0,1,2,2,2,2,2,2,2,2,0,1,0,1, 0,0,0,0,2,1,2,1,2,1,2,1,2,1,2,1,
0,1,0,1,0,1,0,1,0,1,0,1,2,2,2,2, 0,2,2,2,0,1,1,1,0,2,2,2,0,1,1,1, 0,0,0,2,1,1,1,2,0,0,0,2,1,1,1,2, 0,0,0,0,2,1,1,2,2,1,1,2,2,1,1,2, 0,2,2,2,0,1,1,1,0,1,1,1,0,2,2,2, 0,0,0,2,1,1,1,2,1,1,1,2,0,0,0,2, 0,1,1,0,0,1,1,0,0,1,1,0,2,2,2,2, 0,0,0,0,0,0,0,0,2,1,1,2,2,1,1,2,
0,1,1,0,0,1,1,0,2,2,2,2,2,2,2,2, 0,0,2,2,0,0,1,1,0,0,1,1,0,0,2,2, 0,0,2,2,1,1,2,2,1,1,2,2,0,0,2,2, 0,0,0,0,0,0,0,0,0,0,0,0,2,1,1,2, 0,0,0,2,0,0,0,1,0,0,0,2,0,0,0,1, 0,2,2,2,1,2,2,2,0,2,2,2,1,2,2,2, 0,1,0,1,2,2,2,2,2,2,2,2,2,2,2,2, 0,1,1,1,2,0,1,1,2,2,0,1,2,2,2,0,
};
static const uint8_t g_bc7_table_anchor_index_third_subset_1[64] =
{
3, 3,15,15, 8, 3,15,15, 8, 8, 6, 6, 6, 5, 3, 3, 3, 3, 8,15, 3, 3, 6,10, 5, 8, 8, 6, 8, 5,15,15, 8,15, 3, 5, 6,10, 8,15, 15, 3,15, 5,15,15,15,15, 3,15, 5, 5, 5, 8, 5,10, 5,10, 8,13,15,12, 3, 3
};
static const uint8_t g_bc7_table_anchor_index_third_subset_2[64] =
{
15, 8, 8, 3,15,15, 3, 8, 15,15,15,15,15,15,15, 8, 15, 8,15, 3,15, 8,15, 8, 3,15, 6,10,15,15,10, 8, 15, 3,15,10,10, 8, 9,10, 6,15, 8,15, 3, 6, 6, 8, 15, 3,15,15,15,15,15,15, 15,15,15,15, 3,15,15, 8
};
static const uint8_t g_bc7_table_anchor_index_second_subset[64] = { 15,15,15,15,15,15,15,15, 15,15,15,15,15,15,15,15, 15, 2, 8, 2, 2, 8, 8,15, 2, 8, 2, 2, 8, 8, 2, 2, 15,15, 6, 8, 2, 8,15,15, 2, 8, 2, 2, 2,15,15, 6, 6, 2, 6, 8,15,15, 2, 2, 15,15,15,15,15, 2, 2,15 };
static const uint8_t g_bc7_num_subsets[8] = { 3, 2, 3, 2, 1, 1, 1, 2 };
static const uint8_t g_bc7_partition_bits[8] = { 4, 6, 6, 6, 0, 0, 0, 6 };
static const uint8_t g_bc7_color_index_bitcount[8] = { 3, 3, 2, 2, 2, 2, 4, 2 };
static int get_bc7_color_index_size(int mode, int index_selection_bit) { return g_bc7_color_index_bitcount[mode] + index_selection_bit; }
static uint8_t g_bc7_alpha_index_bitcount[8] = { 0, 0, 0, 0, 3, 2, 4, 2 };
static int get_bc7_alpha_index_size(int mode, int index_selection_bit) { return g_bc7_alpha_index_bitcount[mode] - index_selection_bit; }
static const uint8_t g_bc7_mode_has_p_bits[8] = { 1, 1, 0, 1, 0, 0, 1, 1 };
static const uint8_t g_bc7_mode_has_shared_p_bits[8] = { 0, 1, 0, 0, 0, 0, 0, 0 };
static const uint8_t g_bc7_color_precision_table[8] = { 4, 6, 5, 7, 5, 7, 7, 5 };
static const int8_t g_bc7_alpha_precision_table[8] = { 0, 0, 0, 0, 6, 8, 7, 5 };
static bool get_bc7_mode_has_seperate_alpha_selectors(int mode) { return (mode == 4) || (mode == 5); }
typedef struct { uint16_t m_error; uint8_t m_lo; uint8_t m_hi; } endpoint_err;
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_bc7_mode_7_optimal_endpoints[256][2][2]; // [c][pbit][hp][lp]
const uint32_t BC7E_MODE_7_OPTIMAL_INDEX = 1;
static float g_mode1_rgba_midpoints[64][2];
static float g_mode5_rgba_midpoints[128];
static float g_mode7_rgba_midpoints[32][2];
static uint8_t g_mode6_reduced_quant[2048][2];
static bool g_initialized;
// Initialize the lookup table used for optimal single color compression in mode 1/7. Must be called before encoding.
void bc7enc_compress_block_init()
{
if (g_initialized)
return;
// Mode 7 endpoint midpoints
for (uint32_t p = 0; p < 2; p++)
{
for (uint32_t i = 0; i < 32; i++)
{
uint32_t vl = ((i << 1) | p) << 2;
vl |= (vl >> 6);
float lo = vl / 255.0f;
uint32_t vh = ((minimumi(31, (i + 1)) << 1) | p) << 2;
vh |= (vh >> 6);
float hi = vh / 255.0f;
//g_mode7_quant_values[i][p] = lo;
if (i == 31)
g_mode7_rgba_midpoints[i][p] = 1.0f;
else
g_mode7_rgba_midpoints[i][p] = (lo + hi) / 2.0f;
}
}
// Mode 1 endpoint midpoints
for (uint32_t p = 0; p < 2; p++)
{
for (uint32_t i = 0; i < 64; i++)
{
uint32_t vl = ((i << 1) | p) << 1;
vl |= (vl >> 7);
float lo = vl / 255.0f;
uint32_t vh = ((minimumi(63, (i + 1)) << 1) | p) << 1;
vh |= (vh >> 7);
float hi = vh / 255.0f;
//g_mode1_quant_values[i][p] = lo;
if (i == 63)
g_mode1_rgba_midpoints[i][p] = 1.0f;
else
g_mode1_rgba_midpoints[i][p] = (lo + hi) / 2.0f;
}
}
// Mode 5 endpoint midpoints
for (uint32_t i = 0; i < 128; i++)
{
uint32_t vl = (i << 1);
vl |= (vl >> 7);
float lo = vl / 255.0f;
uint32_t vh = minimumi(127, i + 1) << 1;
vh |= (vh >> 7);
float hi = vh / 255.0f;
if (i == 127)
g_mode5_rgba_midpoints[i] = 1.0f;
else
g_mode5_rgba_midpoints[i] = (lo + hi) / 2.0f;
}
for (uint32_t p = 0; p < 2; p++)
{
for (uint32_t i = 0; i < 2048; i++)
{
float f = i / 2047.0f;
float best_err = 1e+9f;
int best_index = 0;
for (int j = 0; j < 64; j++)
{
int ik = (j * 127 + 31) / 63;
float k = ((ik << 1) + p) / 255.0f;
float e = fabsf(k - f);
if (e < best_err)
{
best_err = e;
best_index = ik;
}
}
g_mode6_reduced_quant[i][p] = (uint8_t)best_index;
}
} // p
// Mode 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
// Mode 7: 555.1 2-bit indices
for (int c = 0; c < 256; c++)
{
for (uint32_t hp = 0; hp < 2; hp++)
{
for (uint32_t lp = 0; lp < 2; lp++)
{
endpoint_err best;
best.m_error = (uint16_t)UINT16_MAX;
best.m_lo = 0;
best.m_hi = 0;
for (uint32_t l = 0; l < 32; l++)
{
uint32_t low = ((l << 1) | lp) << 2;
low |= (low >> 6);
for (uint32_t h = 0; h < 32; h++)
{
uint32_t high = ((h << 1) | hp) << 2;
high |= (high >> 6);
const int k = (low * (64 - g_bc7_weights2[BC7E_MODE_7_OPTIMAL_INDEX]) + high * g_bc7_weights2[BC7E_MODE_7_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_7_optimal_endpoints[c][hp][lp] = best;
} // hp
} // lp
} // c
g_initialized = true;
}
static void compute_least_squares_endpoints_rgba(uint32_t N, const uint8_t *pSelectors, const vec4F *pSelector_weights, vec4F *pXl, vec4F *pXh, const color_rgba *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.
float z00 = 0.0f, z01 = 0.0f, z10 = 0.0f, z11 = 0.0f;
float q00_r = 0.0f, q10_r = 0.0f, t_r = 0.0f;
float q00_g = 0.0f, q10_g = 0.0f, t_g = 0.0f;
float q00_b = 0.0f, q10_b = 0.0f, t_b = 0.0f;
float 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;
float det = z00 * z11 - z01 * z10;
if (det != 0.0f)
det = 1.0f / det;
float 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 vec4F *pSelector_weights, vec4F *pXl, vec4F *pXh, const color_rgba*pColors)
{
float z00 = 0.0f, z01 = 0.0f, z10 = 0.0f, z11 = 0.0f;
float q00_r = 0.0f, q10_r = 0.0f, t_r = 0.0f;
float q00_g = 0.0f, q10_g = 0.0f, t_g = 0.0f;
float 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;
float det = z00 * z11 - z01 * z10;
if (det != 0.0f)
det = 1.0f / det;
float 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 void compute_least_squares_endpoints_a(uint32_t N, const uint8_t* pSelectors, const vec4F* pSelector_weights, float* pXl, float* pXh, const color_rgba *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.
float z00 = 0.0f, z01 = 0.0f, z10 = 0.0f, z11 = 0.0f;
float 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_a += w * pColors[i].m_c[3]; t_a += pColors[i].m_c[3];
}
q10_a = t_a - q00_a;
z01 = z10;
float det = z00 * z11 - z01 * z10;
if (det != 0.0f)
det = 1.0f / det;
float iz00, iz01, iz10, iz11;
iz00 = z11 * det;
iz01 = -z01 * det;
iz10 = -z10 * det;
iz11 = z00 * det;
*pXl = (float)(iz00 * q00_a + iz01 * q10_a); *pXh = (float)(iz10 * q00_a + iz11 * q10_a);
if ((*pXl < 0.0f) || (*pXh > 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[3]);
hi_v = maximumu(hi_v, pColors[i].m_c[3]);
}
if (lo_v == hi_v)
{
*pXl = (float)lo_v;
*pXh = (float)hi_v;
}
}
}
struct color_cell_compressor_params
{
uint32_t m_num_pixels;
const color_rgba *m_pPixels;
uint32_t m_num_selector_weights;
const uint32_t *m_pSelector_weights;
const uint16_t *m_pSelector_weights16;
const vec4F *m_pSelector_weightsx;
uint32_t m_comp_bits;
uint32_t m_weights[4];
bool m_has_alpha;
bool m_has_pbits;
bool m_endpoints_share_pbit;
bool m_perceptual;
};
struct color_cell_compressor_results
{
uint64_t m_best_overall_err;
color_rgba m_low_endpoint;
color_rgba m_high_endpoint;
uint32_t m_pbits[2];
uint8_t *m_pSelectors;
uint8_t *m_pSelectors_temp;
};
static inline color_rgba scale_color(const color_rgba *pC, const color_cell_compressor_params *pParams)
{
color_rgba results;
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;
}
#ifdef __AVX2__
static inline void scale_color_x2( const color_rgba* pC, color_rgba* pOut, const color_cell_compressor_params* pParams )
{
const uint32_t n = pParams->m_comp_bits + (pParams->m_has_pbits ? 1 : 0);
assert((n >= 4) && (n <= 8));
uint64_t px;
memcpy( &px, pC, 8 );
__m128i vPx = _mm_cvtepu8_epi16( _mm_set_epi64x( 0, px ) );
__m128i vShift = _mm_slli_epi16( vPx, 8 - n );
__m128i vShift2 = _mm_srli_epi16( vShift, n );
__m128i vOr = _mm_or_si128( vShift, vShift2 );
__m128i vShuffle = _mm_shuffle_epi8( vOr, _mm_set_epi8( 0, 0, 0, 0, 0, 0, 0, 0, 14, 12, 10, 8, 6, 4, 2, 0 ) );
_mm_storel_epi64( (__m128i*)pOut, vShuffle );
}
static inline __m128i scale_color_x2_128( const color_rgba* pC, const color_cell_compressor_params* pParams )
{
const uint32_t n = pParams->m_comp_bits + (pParams->m_has_pbits ? 1 : 0);
assert((n >= 4) && (n <= 8));
uint64_t px;
memcpy( &px, pC, 8 );
__m128i vPx = _mm_cvtepu8_epi16( _mm_set_epi64x( 0, px ) );
__m128i vShift = _mm_slli_epi16( vPx, 8 - n );
__m128i vShift2 = _mm_srli_epi16( vShift, n );
__m128i vOr = _mm_or_si128( vShift, vShift2 );
return vOr;
}
static inline __m256i compute_ycbcr_128x2( const color_rgba *pC )
{
uint32_t px;
memcpy( &px, pC, 4 );
__m128i vPercWeights = _mm_set_epi32( 0, 37, 366, 109 );
__m128i vE2 = _mm_cvtepu8_epi32( _mm_cvtsi32_si128( px ) );
__m128i vL1 = _mm_mullo_epi32( vE2, vPercWeights );
__m128i vL2 = _mm_shuffle_epi32( vL1, _MM_SHUFFLE( 2, 3, 0, 1 ) );
__m128i vL3 = _mm_add_epi32( vL1, vL2 );
__m128i vL4 = _mm_shuffle_epi32( vL3, _MM_SHUFFLE( 1, 0, 3, 2 ) );
__m128i vL5 = _mm_add_epi32( vL3, vL4 );
__m128i vL6 = _mm_blend_epi32( _mm_setzero_si128(), vL5, 0x1 );
__m128i vCrb1 = _mm_slli_epi32( vE2, 9 );
__m128i vCrb2 = _mm_sub_epi32( vCrb1, vL5 );
__m128i vCrb3 = _mm_and_si128( vCrb2, _mm_set_epi64x( 0xFFFFFFFF, 0xFFFFFFFF ) );
__m128i vCrb4 = _mm_shuffle_epi32( vCrb3, _MM_SHUFFLE( 3, 2, 0, 3 ) );
__m128i vD1 = _mm_or_si128( vL6, vCrb4 );
return _mm256_broadcastsi128_si256( vD1 );
}
static inline __m256i compute_ycbcr_128x2_a( const color_rgba *pC )
{
uint32_t px;
memcpy( &px, pC, 4 );
__m128i vPercWeights = _mm_set_epi32( 0, 37, 366, 109 );
__m128i vE2 = _mm_cvtepu8_epi32( _mm_cvtsi32_si128( px ) );
__m128i vL1 = _mm_mullo_epi32( vE2, vPercWeights );
__m128i vL2 = _mm_shuffle_epi32( vL1, _MM_SHUFFLE( 2, 3, 0, 1 ) );
__m128i vL3 = _mm_add_epi32( vL1, vL2 );
__m128i vL4 = _mm_shuffle_epi32( vL3, _MM_SHUFFLE( 1, 0, 3, 2 ) );
__m128i vL5 = _mm_add_epi32( vL3, vL4 );
__m128i vL6 = _mm_blend_epi32( _mm_setzero_si128(), vL5, 0x1 );
__m128i vCrb1 = _mm_slli_epi32( vE2, 9 );
__m128i vCrb2 = _mm_sub_epi32( vCrb1, vL5 );
__m128i vCrb3 = _mm_and_si128( vCrb2, _mm_set_epi64x( 0xFFFFFFFF, 0xFFFFFFFF ) );
__m128i vCrb4 = _mm_shuffle_epi32( vCrb3, _MM_SHUFFLE( 3, 2, 0, 3 ) );
__m128i vD1 = _mm_or_si128( vL6, vCrb4 );
__m128i vD2 = _mm_blend_epi32( vD1, vE2, 0x8 );
return _mm256_broadcastsi128_si256( vD2 );
}
static inline __m256i compute_ycbcr_256( const color_rgba* pC )
{
uint32_t px0, px1;
memcpy( &px0, pC, 4 );
memcpy( &px1, pC + 1, 4 );
__m256i vE1 = _mm256_cvtepu8_epi32( _mm_set1_epi64x( px0 | ( uint64_t(px1) << 32 ) ) );
__m256i vPercWeights = _mm256_set_epi32( 0, 37, 366, 109, 0, 37, 366, 109 );
__m256i vL1 = _mm256_mullo_epi32( vE1, vPercWeights );
__m256i vL2 = _mm256_shuffle_epi32( vL1, _MM_SHUFFLE( 2, 3, 0, 1 ) );
__m256i vL3 = _mm256_add_epi32( vL1, vL2 );
__m256i vL4 = _mm256_shuffle_epi32( vL3, _MM_SHUFFLE( 1, 0, 3, 2 ) );
__m256i vL5 = _mm256_add_epi32( vL3, vL4 );
__m256i vL6 = _mm256_blend_epi32( _mm256_setzero_si256(), vL5, 0x11 );
__m256i vCrb1 = _mm256_slli_epi32( vE1, 9 );
__m256i vCrb2 = _mm256_sub_epi32( vCrb1, vL5 );
__m256i vCrb3 = _mm256_and_si256( vCrb2, _mm256_set_epi64x( 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF ) );
__m256i vCrb4 = _mm256_shuffle_epi32( vCrb3, (_MM_PERM_ENUM)_MM_SHUFFLE( 3, 2, 0, 3 ) );
return _mm256_or_si256( vL6, vCrb4 );
}
static inline __m256i compute_ycbcr_256_a( const color_rgba* pC )
{
uint32_t px0, px1;
memcpy( &px0, pC, 4 );
memcpy( &px1, pC + 1, 4 );
__m256i vE1 = _mm256_cvtepu8_epi32( _mm_set1_epi64x( px0 | ( uint64_t(px1) << 32 ) ) );
__m256i vPercWeights = _mm256_set_epi32( 0, 37, 366, 109, 0, 37, 366, 109 );
__m256i vL1 = _mm256_mullo_epi32( vE1, vPercWeights );
__m256i vL2 = _mm256_shuffle_epi32( vL1, _MM_SHUFFLE( 2, 3, 0, 1 ) );
__m256i vL3 = _mm256_add_epi32( vL1, vL2 );
__m256i vL4 = _mm256_shuffle_epi32( vL3, _MM_SHUFFLE( 1, 0, 3, 2 ) );
__m256i vL5 = _mm256_add_epi32( vL3, vL4 );
__m256i vL6 = _mm256_blend_epi32( _mm256_setzero_si256(), vL5, 0x11 );
__m256i vCrb1 = _mm256_slli_epi32( vE1, 9 );
__m256i vCrb2 = _mm256_sub_epi32( vCrb1, vL5 );
__m256i vCrb3 = _mm256_and_si256( vCrb2, _mm256_set_epi64x( 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF ) );
__m256i vCrb4 = _mm256_shuffle_epi32( vCrb3, (_MM_PERM_ENUM)_MM_SHUFFLE( 3, 2, 0, 3 ) );
__m256i vD1 = _mm256_or_si256( vL6, vCrb4 );
__m256i vD2 = _mm256_blend_epi32( vD1, vE1, 0x88 );
return vD2;
}
static inline __m128i compute_color_distance_rgb_perc_2x2_256(const __m256i vD1, const __m256i vD1b, const __m256i vD1c, const __m256i vWeights)
{
__m256i vD2a = _mm256_sub_epi32( vD1, vD1c );
__m256i vD2b = _mm256_sub_epi32( vD1b, vD1c );
__m256i vDelta1a = _mm256_srai_epi32( vD2a, 8 );
__m256i vDelta1b = _mm256_srai_epi32( vD2b, 8 );
__m256i vDelta2a = _mm256_mullo_epi32( vDelta1a, vDelta1a );
__m256i vDelta2b = _mm256_mullo_epi32( vDelta1b, vDelta1b );
__m256i vDelta3a = _mm256_mullo_epi32( vDelta2a, vWeights );
__m256i vDelta3b = _mm256_mullo_epi32( vDelta2b, vWeights );
__m256i vDelta4a = _mm256_shuffle_epi32( vDelta3a, _MM_SHUFFLE( 2, 3, 0, 1 ) );
__m256i vDelta4b = _mm256_shuffle_epi32( vDelta3b, _MM_SHUFFLE( 2, 3, 0, 1 ) );
__m256i vDelta5a = _mm256_add_epi32( vDelta3a, vDelta4a );
__m256i vDelta5b = _mm256_add_epi32( vDelta3b, vDelta4b );
__m256i vDelta6a = _mm256_shuffle_epi32( vDelta5a, _MM_SHUFFLE( 1, 0, 3, 2 ) );
__m256i vDelta6b = _mm256_shuffle_epi32( vDelta5b, _MM_SHUFFLE( 1, 0, 3, 2 ) );
__m256i vDelta7a = _mm256_add_epi32( vDelta5a, vDelta6a );
__m256i vDelta7b = _mm256_add_epi32( vDelta5b, vDelta6b );
__m256i vDelta8 = _mm256_unpacklo_epi32( vDelta7a, vDelta7b );
__m256i vDelta9 = _mm256_permutevar8x32_epi32( vDelta8, _mm256_set_epi32( 0, 0, 0, 0, 5, 1, 4, 0 ) );
return _mm256_castsi256_si128( vDelta9 );
}
static inline __m128i compute_color_distance_rgb_perc_2x2_256_a(const __m256i vD1, const __m256i vD1b, const __m256i vD1c, const __m256i vWeights)
{
__m256i vD2a = _mm256_sub_epi32( vD1, vD1c );
__m256i vD2b = _mm256_sub_epi32( vD1b, vD1c );
__m256i vDelta0a = _mm256_srai_epi32( vD2a, 8 );
__m256i vDelta0b = _mm256_srai_epi32( vD2b, 8 );
__m256i vDelta1a = _mm256_blend_epi32( vDelta0a, vD2a, 0x88 );
__m256i vDelta1b = _mm256_blend_epi32( vDelta0b, vD2b, 0x88 );
__m256i vDelta2a = _mm256_mullo_epi32( vDelta1a, vDelta1a );
__m256i vDelta2b = _mm256_mullo_epi32( vDelta1b, vDelta1b );
__m256i vDelta3a = _mm256_mullo_epi32( vDelta2a, vWeights );
__m256i vDelta3b = _mm256_mullo_epi32( vDelta2b, vWeights );
__m256i vDelta4a = _mm256_shuffle_epi32( vDelta3a, _MM_SHUFFLE( 2, 3, 0, 1 ) );
__m256i vDelta4b = _mm256_shuffle_epi32( vDelta3b, _MM_SHUFFLE( 2, 3, 0, 1 ) );
__m256i vDelta5a = _mm256_add_epi32( vDelta3a, vDelta4a );
__m256i vDelta5b = _mm256_add_epi32( vDelta3b, vDelta4b );
__m256i vDelta6a = _mm256_shuffle_epi32( vDelta5a, _MM_SHUFFLE( 1, 0, 3, 2 ) );
__m256i vDelta6b = _mm256_shuffle_epi32( vDelta5b, _MM_SHUFFLE( 1, 0, 3, 2 ) );
__m256i vDelta7a = _mm256_add_epi32( vDelta5a, vDelta6a );
__m256i vDelta7b = _mm256_add_epi32( vDelta5b, vDelta6b );
__m256i vDelta8 = _mm256_unpacklo_epi32( vDelta7a, vDelta7b );
__m256i vDelta9 = _mm256_permutevar8x32_epi32( vDelta8, _mm256_set_epi32( 0, 0, 0, 0, 5, 1, 4, 0 ) );
return _mm256_castsi256_si128( vDelta9 );
}
#endif
#ifdef __AVX512BW__
static inline __m512i compute_ycbcr_128x4( const color_rgba *pC )
{
uint32_t px;
memcpy( &px, pC, 4 );
__m128i vPercWeights = _mm_set_epi32( 0, 37, 366, 109 );
__m128i vE2 = _mm_cvtepu8_epi32( _mm_cvtsi32_si128( px ) );
__m128i vL1 = _mm_mullo_epi32( vE2, vPercWeights );
__m128i vL2 = _mm_shuffle_epi32( vL1, _MM_SHUFFLE( 2, 3, 0, 1 ) );
__m128i vL3 = _mm_add_epi32( vL1, vL2 );
__m128i vL4 = _mm_shuffle_epi32( vL3, _MM_SHUFFLE( 1, 0, 3, 2 ) );
__m128i vL5 = _mm_add_epi32( vL3, vL4 );
__m128i vL6 = _mm_blend_epi32( _mm_setzero_si128(), vL5, 0x1 );
__m128i vCrb1 = _mm_slli_epi32( vE2, 9 );
__m128i vCrb2 = _mm_sub_epi32( vCrb1, vL5 );
__m128i vCrb3 = _mm_and_si128( vCrb2, _mm_set_epi64x( 0xFFFFFFFF, 0xFFFFFFFF ) );
__m128i vCrb4 = _mm_shuffle_epi32( vCrb3, _MM_SHUFFLE( 3, 2, 0, 3 ) );
__m128i vD1 = _mm_or_si128( vL6, vCrb4 );
return _mm512_broadcast_i32x4( vD1 );
}
static inline __m512i compute_ycbcr_128x4_a( const color_rgba *pC )
{
uint32_t px;
memcpy( &px, pC, 4 );
__m128i vPercWeights = _mm_set_epi32( 0, 37, 366, 109 );
__m128i vE2 = _mm_cvtepu8_epi32( _mm_cvtsi32_si128( px ) );
__m128i vL1 = _mm_mullo_epi32( vE2, vPercWeights );
__m128i vL2 = _mm_shuffle_epi32( vL1, _MM_SHUFFLE( 2, 3, 0, 1 ) );
__m128i vL3 = _mm_add_epi32( vL1, vL2 );
__m128i vL4 = _mm_shuffle_epi32( vL3, _MM_SHUFFLE( 1, 0, 3, 2 ) );
__m128i vL5 = _mm_add_epi32( vL3, vL4 );
__m128i vL6 = _mm_blend_epi32( _mm_setzero_si128(), vL5, 0x1 );
__m128i vCrb1 = _mm_slli_epi32( vE2, 9 );
__m128i vCrb2 = _mm_sub_epi32( vCrb1, vL5 );
__m128i vCrb3 = _mm_and_si128( vCrb2, _mm_set_epi64x( 0xFFFFFFFF, 0xFFFFFFFF ) );
__m128i vCrb4 = _mm_shuffle_epi32( vCrb3, _MM_SHUFFLE( 3, 2, 0, 3 ) );
__m128i vD1 = _mm_or_si128( vL6, vCrb4 );
__m128i vD2 = _mm_blend_epi32( vD1, vE2, 0x8 );
return _mm512_broadcast_i32x4( vD2 );
}
static inline __m512i compute_ycbcr_512( const color_rgba* pC )
{
__m512i vE1 = _mm512_cvtepu8_epi32( _mm_loadu_si128((const __m128i*)pC) );
__m512i vPercWeights = _mm512_set_epi32( 0, 37, 366, 109, 0, 37, 366, 109, 0, 37, 366, 109, 0, 37, 366, 109 );
__m512i vL1 = _mm512_mullo_epi32( vE1, vPercWeights );
__m512i vL2 = _mm512_shuffle_epi32( vL1, (_MM_PERM_ENUM)_MM_SHUFFLE( 2, 3, 0, 1 ) );
__m512i vL3 = _mm512_add_epi32( vL1, vL2 );
__m512i vL4 = _mm512_shuffle_epi32( vL3, (_MM_PERM_ENUM)_MM_SHUFFLE( 1, 0, 3, 2 ) );
__m512i vL5 = _mm512_add_epi32( vL3, vL4 );
__m512i vL6 = _mm512_mask_blend_epi32( 0x1111, _mm512_setzero_si512(), vL5 );
__m512i vCrb1 = _mm512_slli_epi32( vE1, 9 );
__m512i vCrb2 = _mm512_sub_epi32( vCrb1, vL5 );
__m512i vCrb3 = _mm512_and_si512( vCrb2, _mm512_set_epi64( 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF ) );
__m512i vCrb4 = _mm512_shuffle_epi32( vCrb3, (_MM_PERM_ENUM)_MM_SHUFFLE( 3, 2, 0, 3 ) );
return _mm512_or_si512( vL6, vCrb4 );
}
static inline __m512i compute_ycbcr_512_a( const color_rgba* pC )
{
__m512i vE1 = _mm512_cvtepu8_epi32( _mm_loadu_si128((const __m128i*)pC) );
__m512i vPercWeights = _mm512_set_epi32( 0, 37, 366, 109, 0, 37, 366, 109, 0, 37, 366, 109, 0, 37, 366, 109 );
__m512i vL1 = _mm512_mullo_epi32( vE1, vPercWeights );
__m512i vL2 = _mm512_shuffle_epi32( vL1, (_MM_PERM_ENUM)_MM_SHUFFLE( 2, 3, 0, 1 ) );
__m512i vL3 = _mm512_add_epi32( vL1, vL2 );
__m512i vL4 = _mm512_shuffle_epi32( vL3, (_MM_PERM_ENUM)_MM_SHUFFLE( 1, 0, 3, 2 ) );
__m512i vL5 = _mm512_add_epi32( vL3, vL4 );
__m512i vL6 = _mm512_mask_blend_epi32( 0x1111, _mm512_setzero_si512(), vL5 );
__m512i vCrb1 = _mm512_slli_epi32( vE1, 9 );
__m512i vCrb2 = _mm512_sub_epi32( vCrb1, vL5 );
__m512i vCrb3 = _mm512_and_si512( vCrb2, _mm512_set_epi64( 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF ) );
__m512i vCrb4 = _mm512_shuffle_epi32( vCrb3, (_MM_PERM_ENUM)_MM_SHUFFLE( 3, 2, 0, 3 ) );
__m512i vD1 = _mm512_or_si512( vL6, vCrb4 );
__m512i vD2 = _mm512_mask_blend_epi32( 0x8888, vD1, vE1 );
return vD2;
}
static inline __m128i compute_color_distance_rgb_perc_4x_512(const __m512i vD1, const __m512i vD1c, const __m512i vWeights)
{
__m512i vD2 = _mm512_sub_epi32( vD1, vD1c );
__m512i vDelta = _mm512_srai_epi32( vD2, 8 );
__m512i vDelta2 = _mm512_mullo_epi32( vDelta, vDelta );
__m512i vDelta3 = _mm512_mullo_epi32( vDelta2, vWeights );
__m512i vDelta4 = _mm512_shuffle_epi32( vDelta3, (_MM_PERM_ENUM)_MM_SHUFFLE( 2, 3, 0, 1 ) );
__m512i vDelta5 = _mm512_add_epi32( vDelta3, vDelta4 );
__m512i vDelta6 = _mm512_shuffle_epi32( vDelta5, (_MM_PERM_ENUM)_MM_SHUFFLE( 1, 0, 3, 2 ) );
__m512i vDelta7 = _mm512_add_epi32( vDelta5, vDelta6 );
__m512i vDelta8 = _mm512_permutexvar_epi32( _mm512_castsi128_si512( _mm_set_epi32( 12, 8, 4, 0 ) ), vDelta7 );
return _mm512_castsi512_si128( vDelta8 );
}
static inline __m128i compute_color_distance_rgb_perc_4x_512_a(const __m512i vD1, const __m512i vD1c, const __m512i vWeights)
{
__m512i vD2 = _mm512_sub_epi32( vD1, vD1c );
__m512i vDelta = _mm512_srai_epi32( vD2, 8 );
__m512i vDelta1 = _mm512_mask_blend_epi32( 0x8888, vDelta, vD2 );
__m512i vDelta2 = _mm512_mullo_epi32( vDelta1, vDelta1 );
__m512i vDelta3 = _mm512_mullo_epi32( vDelta2, vWeights );
__m512i vDelta4 = _mm512_shuffle_epi32( vDelta3, (_MM_PERM_ENUM)_MM_SHUFFLE( 2, 3, 0, 1 ) );
__m512i vDelta5 = _mm512_add_epi32( vDelta3, vDelta4 );
__m512i vDelta6 = _mm512_shuffle_epi32( vDelta5, (_MM_PERM_ENUM)_MM_SHUFFLE( 1, 0, 3, 2 ) );
__m512i vDelta7 = _mm512_add_epi32( vDelta5, vDelta6 );
__m512i vDelta8 = _mm512_permutexvar_epi32( _mm512_castsi128_si512( _mm_set_epi32( 12, 8, 4, 0 ) ), vDelta7 );
return _mm512_castsi512_si128( vDelta8 );
}
#endif
static inline uint32_t compute_color_distance_rgb(const color_rgba *pE1, const color_rgba *pE2, bool perceptual, const uint32_t weights[4])
{
#ifdef __AVX2__
uint32_t e1, e2;
memcpy( &e1, pE1, 4 );
memcpy( &e2, pE2, 4 );
__m128i vE1 = _mm_cvtepu8_epi32( _mm_cvtsi32_si128( e1 ) );
__m128i vE2 = _mm_cvtepu8_epi32( _mm_cvtsi32_si128( e2 ) );
__m256i vE = _mm256_inserti128_si256( _mm256_castsi128_si256( vE1 ), vE2, 1 );
__m128i vDelta;
if (perceptual)
{
__m256i vPercWeights = _mm256_set_epi32( 0, 37, 366, 109, 0, 37, 366, 109 );
__m256i vL1 = _mm256_mullo_epi32( vE, vPercWeights );
__m256i vL2 = _mm256_shuffle_epi32( vL1, _MM_SHUFFLE( 2, 3, 0, 1 ) );
__m256i vL3 = _mm256_add_epi32( vL1, vL2 );
__m256i vL4 = _mm256_shuffle_epi32( vL3, _MM_SHUFFLE( 1, 0, 3, 2 ) );
__m256i vL5 = _mm256_add_epi32( vL3, vL4 );
__m256i vL6 = _mm256_blend_epi32( _mm256_setzero_si256(), vL5, 0x11 );
__m256i vCrb1 = _mm256_slli_epi32( vE, 9 );
__m256i vCrb2 = _mm256_sub_epi32( vCrb1, vL5 );
__m256i vCrb3 = _mm256_and_si256( vCrb2, _mm256_set_epi32( 0, 0xFFFFFFFF, 0, 0xFFFFFFFF, 0, 0xFFFFFFFF, 0, 0xFFFFFFFF ) );
__m256i vCrb4 = _mm256_shuffle_epi32( vCrb3, _MM_SHUFFLE( 3, 2, 0, 3 ) );
__m256i vD1 = _mm256_or_si256( vL6, vCrb4 );
__m128i vD2 = _mm256_castsi256_si128( vD1 );
__m128i vD3 = _mm256_extracti128_si256( vD1, 1 );
__m128i vD4 = _mm_sub_epi32( vD2, vD3 );
vDelta = _mm_srai_epi32( vD4, 8 );
}
else
{
vDelta = _mm_sub_epi32(vE1, vE2);
}
__m128i vWeights = _mm_loadu_si128( (const __m128i*)weights );
__m128i vDelta2 = _mm_mullo_epi32( vDelta, vDelta );
__m128i vDelta3 = _mm_mullo_epi32( vDelta2, vWeights );
__m128i vDelta4 = _mm_shuffle_epi32( vDelta3, _MM_SHUFFLE( 2, 3, 0, 1 ) );
__m128i vDelta5 = _mm_add_epi32( vDelta3, vDelta4 );
__m128i vDelta6 = _mm_shuffle_epi32( vDelta5, _MM_SHUFFLE( 1, 0, 3, 2 ) );
__m128i vDelta7 = _mm_add_epi32( vDelta5, vDelta6 );
return _mm_cvtsi128_si32( vDelta7 );
#else
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);
#endif
}
static inline uint32_t compute_color_distance_rgba(const color_rgba *pE1, const color_rgba *pE2, 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;
if (!best_err)
break;
}
}
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_rgba 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_mode7_to_one_color(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, uint32_t num_pixels, const color_rgba *pPixels)
{
uint32_t best_err = UINT_MAX;
uint32_t best_p = 0;
for (uint32_t p = 0; p < 4; p++)
{
uint32_t hi_p = p >> 1;
uint32_t lo_p = p & 1;
uint32_t err = g_bc7_mode_7_optimal_endpoints[r][hi_p][lo_p].m_error + g_bc7_mode_7_optimal_endpoints[g][hi_p][lo_p].m_error + g_bc7_mode_7_optimal_endpoints[b][hi_p][lo_p].m_error + g_bc7_mode_7_optimal_endpoints[a][hi_p][lo_p].m_error;
if (err < best_err)
{
best_err = err;
best_p = p;
if (!best_err)
break;
}
}
uint32_t best_hi_p = best_p >> 1;
uint32_t best_lo_p = best_p & 1;
const endpoint_err* pEr = &g_bc7_mode_7_optimal_endpoints[r][best_hi_p][best_lo_p];
const endpoint_err* pEg = &g_bc7_mode_7_optimal_endpoints[g][best_hi_p][best_lo_p];
const endpoint_err* pEb = &g_bc7_mode_7_optimal_endpoints[b][best_hi_p][best_lo_p];
const endpoint_err* pEa = &g_bc7_mode_7_optimal_endpoints[a][best_hi_p][best_lo_p];
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] = best_lo_p;
pResults->m_pbits[1] = best_hi_p;
for (uint32_t i = 0; i < num_pixels; i++)
pSelectors[i] = (uint8_t)BC7E_MODE_7_OPTIMAL_INDEX;
color_rgba p;
for (uint32_t i = 0; i < 4; i++)
{
uint32_t low = (pResults->m_low_endpoint.m_c[i] << 1) | pResults->m_pbits[0];
uint32_t high = (pResults->m_high_endpoint.m_c[i] << 1) | pResults->m_pbits[1];
low = (low << 2) | (low >> 6);
high = (high << 2) | (high >> 6);