-
Notifications
You must be signed in to change notification settings - Fork 30
/
bc7e.ispc
4952 lines (3930 loc) · 158 KB
/
bc7e.ispc
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
// bc7e.ispc - Fast high quality SIMD BC7 encoder
// Copyright (C) 2018-2020 Binomial LLC, All rights reserved. Apache 2.0 license - see LICENSE.
// Typically compiled as: ispc -g -O2 "%(Filename).ispc" -o "$(TargetDir)%(Filename).obj" -h "$(ProjectDir)%(Filename)_ispc.h" --target=sse2,sse4,avx,avx2 --opt=fast-math --opt=disable-assertions
// --opt=fast-math is optional (doesn't make much if any measurable difference).
// Thanks to ArasP for the determinism fix.
#define BC7E_NON_DETERMINISTIC (0)
#define BC7E_2SUBSET_CHECKERBOARD_PARTITION_INDEX (34)
#define BC7E_BLOCK_SIZE (16)
#define BC7E_MAX_PARTITIONS0 (16)
#define BC7E_MAX_PARTITIONS1 (64)
#define BC7E_MAX_PARTITIONS2 (64)
#define BC7E_MAX_PARTITIONS3 (64)
#define BC7E_MAX_PARTITIONS7 (64)
#define BC7E_MAX_UBER_LEVEL (4)
typedef unsigned int8 uint8_t;
typedef unsigned int64 uint16_t;
typedef unsigned int32 uint32_t;
typedef unsigned int64 uint64_t;
typedef int8 int8_t;
typedef int int32_t;
typedef int64 int64_t;
#ifndef UINT16_MAX
#define UINT16_MAX (0xFFFF)
#endif
#ifndef UINT_MAX
#define UINT_MAX (0xFFFFFFFFU)
#endif
#ifndef UINT64_MAX
#define UINT64_MAX (0xFFFFFFFFFFFFFFFFULL)
#endif
#ifndef INT64_MAX
#define INT64_MAX (0x7FFFFFFFFFFFFFFFULL)
#endif
struct bc7e_compress_block_params
{
uint32_t m_max_partitions_mode[8];
uint32_t m_weights[4];
uint32_t m_uber_level;
uint32_t m_refinement_passes;
uint32_t m_mode4_rotation_mask;
uint32_t m_mode4_index_mask;
uint32_t m_mode5_rotation_mask;
uint32_t m_uber1_mask;
bool m_perceptual;
bool m_pbit_search;
bool m_mode6_only;
bool m_unused0;
struct
{
uint32_t m_max_mode13_partitions_to_try;
uint32_t m_max_mode0_partitions_to_try;
uint32_t m_max_mode2_partitions_to_try;
bool m_use_mode[7];
bool m_unused1;
} m_opaque_settings;
struct
{
uint32_t m_max_mode7_partitions_to_try;
uint32_t m_mode67_error_weight_mul[4];
bool m_use_mode4;
bool m_use_mode5;
bool m_use_mode6;
bool m_use_mode7;
bool m_use_mode4_rotation;
bool m_use_mode5_rotation;
bool m_unused2;
bool m_unused3;
} m_alpha_settings;
};
static inline uniform int32_t clampi(uniform int32_t value, uniform int32_t low, uniform int32_t high) { return clamp(value, low, high); }
static inline uniform uint32_t clampu(uniform uint32_t value, uniform uint32_t low, uniform uint32_t high) { return clamp(value, low, high); }
static inline uniform float clampf(uniform float value, uniform float low, uniform float high) { return clamp(value, low, high); }
static inline int32_t clampi(int32_t value, int32_t low, int32_t high) { return clamp(value, low, high); }
static inline uint32_t clampu(uint32_t value, uint32_t low, uint32_t high) { return clamp(value, low, high); }
static inline float clampf(float value, float low, float high) { return clamp(value, low, high); }
static inline uniform float saturate(uniform float value) { return clampf(value, 0, 1.0f); }
static inline uniform float saturate255(uniform float value) { return clampf(value, 0, 255.0f); }
static inline float saturate(float value) { return clampf(value, 0, 1.0f); }
static inline float saturate255(float value) { return clampf(value, 0, 255.0f); }
static inline uniform uint8_t minimumub(uniform uint8_t a, uniform uint8_t b) { return min(a, b); }
static inline uniform int32_t minimumi(uniform int32_t a, uniform int32_t b) { return min(a, b); }
static inline uniform uint32_t minimumu(uniform uint32_t a, uniform uint32_t b) { return min(a, b); }
static inline uniform uint64_t minimumu64(uniform uint64_t a, uniform uint64_t b) { return min(a, b); }
static inline uniform float minimumf(uniform float a, uniform float b) { return min(a, b); }
static inline uniform uint8_t maximumub(uniform uint8_t a, uniform uint8_t b) { return max(a, b); }
static inline uniform int32_t maximumi(uniform int32_t a, uniform int32_t b) { return max(a, b); }
static inline uniform uint32_t maximumu(uniform uint32_t a, uniform uint32_t b) { return max(a, b); }
static inline uniform float maximumf(uniform float a, uniform float b) { return max(a, b); }
static inline uint8_t minimumub(uint8_t a, uint8_t b) { return min(a, b); }
static inline int32_t minimumi(int32_t a, int32_t b) { return min(a, b); }
static inline uint32_t minimumu(uint32_t a, uint32_t b) { return min(a, b); }
static inline uint64_t minimumu64(uint64_t a, uint64_t b) { return min(a, b); }
static inline float minimumf(float a, float b) { return min(a, b); }
static inline uint8_t maximumub(uint8_t a, uint8_t b) { return max(a, b); }
static inline int32_t maximumi(int32_t a, int32_t b) { return max(a, b); }
static inline uint32_t maximumu(uint32_t a, uint32_t b) { return max(a, b); }
static inline float maximumf(float a, float b) { return max(a, b); }
static inline uniform int32_t iabs32(uniform int32_t v) { uniform uint32_t msk = v >> 31; return (v ^ msk) - msk; }
static inline int32_t iabs32(int32_t v) { uint32_t msk = v >> 31; return (v ^ msk) - msk; }
static inline void swapub(uniform uint8_t * uniform a, uniform uint8_t * uniform b) { uniform uint8_t t = *a; *a = *b; *b = t; }
static inline void swapu(uniform uint32_t * uniform a, uniform uint32_t * uniform b) { uniform uint32_t t = *a; *a = *b; *b = t; }
static inline void swapi(uniform int32_t * uniform a, uniform int32_t * uniform b) { uniform int32_t t = *a; *a = *b; *b = t; }
static inline void swapf(uniform float * uniform a, uniform float * uniform b) { uniform float t = *a; *a = *b; *b = t; }
static inline void swapub(varying uint8_t *uniform a, varying uint8_t *uniform b) { uint8_t t = *a; *a = *b; *b = t; }
static inline void swapi(varying int32_t *uniform a, varying int32_t *uniform b) { int32_t t = *a; *a = *b; *b = t; }
static inline void swapu(varying uint32_t *uniform a, varying uint32_t *uniform b) { uint32_t t = *a; *a = *b; *b = t; }
static inline void swapf(varying float *uniform a, varying float *uniform b) { float t = *a; *a = *b; *b = t; }
static inline float square(float s) { return s * s; }
static inline int square(int s) { return s * s; }
struct color_quad_u8
{
uint8_t m_c[4];
};
struct color_quad_i
{
int32_t m_c[4];
};
struct color_quad_f
{
float m_c[4];
};
static inline color_quad_i component_min_rgb(const varying color_quad_i * uniform pA, const varying color_quad_i * uniform pB)
{
color_quad_i res;
res.m_c[0] = minimumi(pA->m_c[0], pB->m_c[0]);
res.m_c[1] = minimumi(pA->m_c[1], pB->m_c[1]);
res.m_c[2] = minimumi(pA->m_c[2], pB->m_c[2]);
res.m_c[3] = 255;
return res;
}
static inline color_quad_i component_max_rgb(const varying color_quad_i * uniform pA, const varying color_quad_i * uniform pB)
{
color_quad_i res;
res.m_c[0] = maximumi(pA->m_c[0], pB->m_c[0]);
res.m_c[1] = maximumi(pA->m_c[1], pB->m_c[1]);
res.m_c[2] = maximumi(pA->m_c[2], pB->m_c[2]);
res.m_c[3] = 255;
return res;
}
static inline varying color_quad_i *color_quad_i_set_clamped(varying color_quad_i * uniform pRes, varying int32_t r, varying int32_t g, varying int32_t b, varying int32_t a)
{
pRes->m_c[0] = clampi(r, 0, 255);
pRes->m_c[1] = clampi(g, 0, 255);
pRes->m_c[2] = clampi(b, 0, 255);
pRes->m_c[3] = clampi(a, 0, 255);
return pRes;
}
static inline varying color_quad_i *color_quad_i_set(varying color_quad_i * uniform pRes, varying int32_t r, varying int32_t g, varying int32_t b, varying int32_t a)
{
pRes->m_c[0] = r;
pRes->m_c[1] = g;
pRes->m_c[2] = b;
pRes->m_c[3] = a;
return pRes;
}
static inline varying color_quad_i *color_quad_i_set(varying color_quad_i * uniform pRes, uniform int32_t r, uniform int32_t g, uniform int32_t b, uniform int32_t a)
{
pRes->m_c[0] = r;
pRes->m_c[1] = g;
pRes->m_c[2] = b;
pRes->m_c[3] = a;
return pRes;
}
static inline bool color_quad_i_equals(const varying color_quad_i * uniform pLHS, const varying color_quad_i * uniform 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 bool color_quad_i_notequals(const varying color_quad_i * uniform pLHS, const varying color_quad_i * uniform pRHS)
{
return !color_quad_i_equals(pLHS, pRHS);
}
struct vec4F
{
float m_c[4];
};
static inline varying vec4F * uniform vec4F_set_scalar(varying vec4F * uniform 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 varying vec4F * uniform vec4F_set(varying vec4F * uniform 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 varying vec4F * uniform vec4F_saturate_in_place(varying vec4F * uniform 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 varying vec4F * uniform 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 varying color_quad_i * uniform 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 varying vec4F * uniform pLHS, const varying vec4F * uniform 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 varying vec4F * uniform pLHS, const varying vec4F *uniform 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 varying vec4F * uniform pLHS, const varying vec4F * uniform 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 varying vec4F * uniform 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 varying vec4F *vec4F_normalize_in_place(varying vec4F * uniform 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 / sqrt(s);
pV->m_c[0] *= s;
pV->m_c[1] *= s;
pV->m_c[2] *= s;
pV->m_c[3] *= s;
}
return pV;
}
static const uniform uint32_t g_bc7_weights2[4] = { 0, 21, 43, 64 };
static const uniform uint32_t g_bc7_weights3[8] = { 0, 9, 18, 27, 37, 46, 55, 64 };
static const uniform uint32_t g_bc7_weights4[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 uniform 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 uniform 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 uniform 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 uniform int g_bc7_partition1[16] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
static const uniform int 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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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
};
static const uniform int 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 uniform int 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,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10, 8,13,15,12, 3, 3
};
static const uniform int 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 uniform int g_bc7_num_subsets[8] = { 3, 2, 3, 2, 1, 1, 1, 2 };
static const uniform int g_bc7_partition_bits[8] = { 4, 6, 6, 6, 0, 0, 0, 6 };
static const uniform int g_bc7_rotation_bits[8] = { 0, 0, 0, 0, 2, 2, 0, 0 };
static const uniform int g_bc7_color_index_bitcount[8] = { 3, 3, 2, 2, 2, 2, 4, 2 };
static int uniform get_bc7_color_index_size(uniform int mode, uniform int index_selection_bit) { return g_bc7_color_index_bitcount[mode] + index_selection_bit; }
#pragma ignore warning(perf)
static int varying get_bc7_color_index_size(varying int mode, varying int index_selection_bit) { return g_bc7_color_index_bitcount[mode] + index_selection_bit; }
static uniform int g_bc7_alpha_index_bitcount[8] = { 0, 0, 0, 0, 3, 2, 4, 2 };
static uniform int get_bc7_alpha_index_size(uniform int mode, uniform int index_selection_bit) { return g_bc7_alpha_index_bitcount[mode] - index_selection_bit; }
#pragma ignore warning(perf)
static varying int get_bc7_alpha_index_size(varying int mode, varying int index_selection_bit) { return g_bc7_alpha_index_bitcount[mode] - index_selection_bit; }
static const uniform int g_bc7_mode_has_p_bits[8] = { 1, 1, 0, 1, 0, 0, 1, 1 };
static const uniform int g_bc7_mode_has_shared_p_bits[8] = { 0, 1, 0, 0, 0, 0, 0, 0 };
static const uniform int g_bc7_color_precision_table[8] = { 4, 6, 5, 7, 5, 7, 7, 5 };
static const uniform int g_bc7_color_precision_plus_pbit_table[8] = { 5, 7, 5, 8, 5, 7, 8, 6 };
static const uniform int g_bc7_alpha_precision_table[8] = { 0, 0, 0, 0, 6, 8, 7, 5 };
static const uniform int g_bc7_alpha_precision_plus_pbit_table[8] = { 0, 0, 0, 0, 6, 8, 8, 6 };
static uniform bool get_bc7_mode_has_seperate_alpha_selectors(uniform int mode) { return (mode == 4) || (mode == 5); }
static varying bool get_bc7_mode_has_seperate_alpha_selectors(varying int mode) { return (mode == 4) || (mode == 5); }
struct endpoint_err
{
uint16_t m_error;
uint8_t m_lo;
uint8_t m_hi;
};
static uniform endpoint_err g_bc7_mode_1_optimal_endpoints[256][2]; // [c][pbit]
const uniform uint32_t BC7E_MODE_1_OPTIMAL_INDEX = 2;
static uniform endpoint_err g_bc7_mode_7_optimal_endpoints[256][2][2]; // [c][pbit][hp][lp]
const uniform uint32_t BC7E_MODE_7_OPTIMAL_INDEX = 1;
static uniform endpoint_err g_bc7_mode_6_optimal_endpoints[256][2][2]; // [c][hp][lp]
const uniform uint32_t BC7E_MODE_6_OPTIMAL_INDEX = 5;
static uniform uint32_t g_bc7_mode_4_optimal_endpoints3[256]; // [c]
static uniform uint32_t g_bc7_mode_4_optimal_endpoints2[256]; // [c]
const uniform uint32_t BC7E_MODE_4_OPTIMAL_INDEX3 = 2;
const uniform uint32_t BC7E_MODE_4_OPTIMAL_INDEX2 = 1;
static uniform uint32_t g_bc7_mode_5_optimal_endpoints[256]; // [c]
const uniform uint32_t BC7E_MODE_5_OPTIMAL_INDEX = 1;
static uniform endpoint_err g_bc7_mode_0_optimal_endpoints[256][2][2]; // [c][hp][lp]
const uniform uint32_t BC7E_MODE_0_OPTIMAL_INDEX = 2;
static uniform bool g_codec_initialized;
export void bc7e_compress_block_init()
{
if (g_codec_initialized)
return;
// Mode 0: 444.1
for (uniform int c = 0; c < 256; c++)
{
for (uniform uint32_t hp = 0; hp < 2; hp++)
{
for (uniform uint32_t lp = 0; lp < 2; lp++)
{
uniform endpoint_err best;
best.m_error = (uint16_t)UINT16_MAX;
for (uniform uint32_t l = 0; l < 16; l++)
{
uniform uint32_t low = ((l << 1) | lp) << 3;
low |= (low >> 5);
for (uniform uint32_t h = 0; h < 16; h++)
{
uniform uint32_t high = ((h << 1) | hp) << 3;
high |= (high >> 5);
const uniform int k = (low * (64 - g_bc7_weights3[BC7E_MODE_0_OPTIMAL_INDEX]) + high * g_bc7_weights3[BC7E_MODE_0_OPTIMAL_INDEX] + 32) >> 6;
const uniform 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_0_optimal_endpoints[c][hp][lp] = best;
} // lp
} // hp
} // c
// Mode 1: 666.1
for (uniform int c = 0; c < 256; c++)
{
for (uniform uint32_t lp = 0; lp < 2; lp++)
{
uniform endpoint_err best;
best.m_error = (uint16_t)UINT16_MAX;
for (uniform uint32_t l = 0; l < 64; l++)
{
uniform uint32_t low = ((l << 1) | lp) << 1;
low |= (low >> 7);
for (uniform uint32_t h = 0; h < 64; h++)
{
uniform uint32_t high = ((h << 1) | lp) << 1;
high |= (high >> 7);
const uniform int k = (low * (64 - g_bc7_weights3[BC7E_MODE_1_OPTIMAL_INDEX]) + high * g_bc7_weights3[BC7E_MODE_1_OPTIMAL_INDEX] + 32) >> 6;
const uniform 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 6: 777.1 4-bit indices
for (uniform int c = 0; c < 256; c++)
{
for (uniform uint32_t hp = 0; hp < 2; hp++)
{
for (uniform uint32_t lp = 0; lp < 2; lp++)
{
uniform endpoint_err best;
best.m_error = (uint16_t)UINT16_MAX;
for (uniform uint32_t l = 0; l < 128; l++)
{
uniform uint32_t low = (l << 1) | lp;
for (uniform uint32_t h = 0; h < 128; h++)
{
uniform uint32_t high = (h << 1) | hp;
const uniform int k = (low * (64 - g_bc7_weights4[BC7E_MODE_6_OPTIMAL_INDEX]) + high * g_bc7_weights4[BC7E_MODE_6_OPTIMAL_INDEX] + 32) >> 6;
const uniform 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_6_optimal_endpoints[c][hp][lp] = best;
} // lp
} // hp
} // c
// Mode 5: 777 2-bit indices
for (uniform int c = 0; c < 256; c++)
{
uniform endpoint_err best;
best.m_error = (uint16_t)UINT16_MAX;
best.m_lo = 0;
best.m_hi = 0;
for (uniform uint32_t l = 0; l < 128; l++)
{
uniform uint32_t low = l << 1;
low |= (low >> 7);
for (uniform uint32_t h = 0; h < 128; h++)
{
uniform uint32_t high = h << 1;
high |= (high >> 7);
const uniform int k = (low * (64 - g_bc7_weights2[BC7E_MODE_5_OPTIMAL_INDEX]) + high * g_bc7_weights2[BC7E_MODE_5_OPTIMAL_INDEX] + 32) >> 6;
const uniform 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_5_optimal_endpoints[c] = (uint32_t)best.m_lo | (((uint32_t)best.m_hi) << 8);
} // c
//Mode 4: 555 3-bit indices
for (uniform int c = 0; c < 256; c++)
{
uniform endpoint_err best;
best.m_error = (uint16_t)UINT16_MAX;
best.m_lo = 0;
best.m_hi = 0;
for (uniform uint32_t l = 0; l < 32; l++)
{
uniform uint32_t low = l << 3;
low |= (low >> 5);
for (uniform uint32_t h = 0; h < 32; h++)
{
uniform uint32_t high = h << 3;
high |= (high >> 5);
const uniform int k = (low * (64 - g_bc7_weights3[BC7E_MODE_4_OPTIMAL_INDEX3]) + high * g_bc7_weights3[BC7E_MODE_4_OPTIMAL_INDEX3] + 32) >> 6;
const uniform 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_4_optimal_endpoints3[c] = (uint32_t)best.m_lo | (((uint32_t)best.m_hi) << 8);
} // c
// Mode 4: 555 2-bit indices
for (uniform int c = 0; c < 256; c++)
{
uniform endpoint_err best;
best.m_error = (uint16_t)UINT16_MAX;
best.m_lo = 0;
best.m_hi = 0;
for (uniform uint32_t l = 0; l < 32; l++)
{
uniform uint32_t low = l << 3;
low |= (low >> 5);
for (uniform uint32_t h = 0; h < 32; h++)
{
uniform uint32_t high = h << 3;
high |= (high >> 5);
const uniform int k = (low * (64 - g_bc7_weights2[BC7E_MODE_4_OPTIMAL_INDEX2]) + high * g_bc7_weights2[BC7E_MODE_4_OPTIMAL_INDEX2] + 32) >> 6;
const uniform 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_4_optimal_endpoints2[c] = (uint32_t)best.m_lo | (((uint32_t)best.m_hi) << 8);
} // c
// Mode 7: 555.1 2-bit indices
for (uniform int c = 0; c < 256; c++)
{
uniform endpoint_err best;
best.m_error = (uint16_t)UINT16_MAX;
best.m_lo = 0;
best.m_hi = 0;
for (uniform uint32_t hp = 0; hp < 2; hp++)
{
for (uniform uint32_t lp = 0; lp < 2; lp++)
{
for (uniform uint32_t l = 0; l < 32; l++)
{
uniform uint32_t low = ((l << 1) | lp) << 2;
low |= (low >> 6);
for (uniform uint32_t h = 0; h < 32; h++)
{
uniform uint32_t high = ((h << 1) | hp) << 2;
high |= (high >> 6);
const uniform int k = (low * (64 - g_bc7_weights2[BC7E_MODE_7_OPTIMAL_INDEX]) + high * g_bc7_weights2[BC7E_MODE_7_OPTIMAL_INDEX] + 32) >> 6;
const uniform 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_codec_initialized = true;
}
static void compute_least_squares_endpoints_rgba(uint32_t N, const varying int *uniform pSelectors, const uniform vec4F *uniform pSelector_weights, varying vec4F *uniform pXl, varying vec4F *uniform pXh, const varying color_quad_i * uniform 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 (uniform uint32_t i = 0; i < N; i++)
{
const uint32_t sel = pSelectors[i];
#pragma ignore warning(perf)
z00 += pSelector_weights[sel].m_c[0];
#pragma ignore warning(perf)
z10 += pSelector_weights[sel].m_c[1];
#pragma ignore warning(perf)
z11 += pSelector_weights[sel].m_c[2];
#pragma ignore warning(perf)
float w = pSelector_weights[sel].m_c[3];
q00_r += w * (int)pColors[i].m_c[0]; t_r += (int)pColors[i].m_c[0];
q00_g += w * (int)pColors[i].m_c[1]; t_g += (int)pColors[i].m_c[1];
q00_b += w * (int)pColors[i].m_c[2]; t_b += (int)pColors[i].m_c[2];
q00_a += w * (int)pColors[i].m_c[3]; t_a += (int)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);
}
static void compute_least_squares_endpoints_rgb(uint32_t N, const varying int *uniform pSelectors, const uniform vec4F *uniform pSelector_weights, varying vec4F *uniform pXl, varying vec4F *uniform pXh, const varying color_quad_i *uniform 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;
for (uniform uint32_t i = 0; i < N; i++)
{
const uint32_t sel = pSelectors[i];
#pragma ignore warning(perf)
z00 += pSelector_weights[sel].m_c[0];
#pragma ignore warning(perf)
z10 += pSelector_weights[sel].m_c[1];
#pragma ignore warning(perf)
z11 += pSelector_weights[sel].m_c[2];
#pragma ignore warning(perf)
float w = pSelector_weights[sel].m_c[3];
q00_r += w * (int)pColors[i].m_c[0]; t_r += (int)pColors[i].m_c[0];
q00_g += w * (int)pColors[i].m_c[1]; t_g += (int)pColors[i].m_c[1];
q00_b += w * (int)pColors[i].m_c[2]; t_b += (int)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);
}
static void compute_least_squares_endpoints_a(uint32_t N, const varying int *uniform pSelectors, const vec4F *uniform pSelector_weights, varying float *uniform pXl, varying float *uniform pXh, const varying color_quad_i *uniform 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 (uniform uint32_t i = 0; i < N; i++)
{
const uint32_t sel = pSelectors[i];
#pragma ignore warning(perf)
z00 += pSelector_weights[sel].m_c[0];
#pragma ignore warning(perf)
z10 += pSelector_weights[sel].m_c[1];
#pragma ignore warning(perf)
z11 += pSelector_weights[sel].m_c[2];
#pragma ignore warning(perf)
float w = pSelector_weights[sel].m_c[3];
q00_a += w * (int)pColors[i].m_c[3]; t_a += (int)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);
}
struct color_cell_compressor_params
{
uniform uint32_t m_num_selector_weights;
const uint32_t *uniform m_pSelector_weights;
const vec4F *uniform m_pSelector_weightsx;
uniform uint32_t m_comp_bits;
uniform uint32_t m_weights[4];
uniform bool m_has_alpha;
uniform bool m_has_pbits;
uniform bool m_endpoints_share_pbit;
uniform bool m_perceptual;
};
static inline void color_cell_compressor_params_clear(uniform color_cell_compressor_params *uniform p)
{
p->m_num_selector_weights = 0;
p->m_pSelector_weights = NULL;
p->m_pSelector_weightsx = NULL;
p->m_comp_bits = 0;
p->m_perceptual = false;
p->m_weights[0] = 1;
p->m_weights[1] = 1;
p->m_weights[2] = 1;
p->m_weights[3] = 1;
p->m_has_alpha = false;
p->m_has_pbits = false;
p->m_endpoints_share_pbit = false;
}
struct color_cell_compressor_results
{
uint64_t m_best_overall_err;
color_quad_i m_low_endpoint;
color_quad_i m_high_endpoint;
uint32_t m_pbits[2];
varying int *uniform m_pSelectors;
varying int *uniform m_pSelectors_temp;
};
static inline color_quad_i scale_color(const varying color_quad_i *uniform pC, const uniform color_cell_compressor_params *uniform pParams)
{
color_quad_i results;
const uint32_t n = pParams->m_comp_bits + (pParams->m_has_pbits ? 1 : 0);
assert((n >= 4) && (n <= 8));
for (uniform uint32_t i = 0; i < 4; i++)
{
uint32_t v = pC->m_c[i] << (8 - n);
#pragma ignore warning(perf)
v |= (v >> n);
assert(v <= 255);
results.m_c[i] = v;
}
return results;
}
static const float pr_weight = (.5f / (1.0f - .2126f)) * (.5f / (1.0f - .2126f));
static const float pb_weight = (.5f / (1.0f - .0722f)) * (.5f / (1.0f - .0722f));
static inline uint64_t compute_color_distance_rgb(const varying color_quad_i * uniform pE1, const varying color_quad_i *uniform pE2, uniform bool perceptual, const uint32_t uniform weights[4])
{
if (perceptual)
{
const float l1 = pE1->m_c[0] * .2126f + pE1->m_c[1] * .7152f + pE1->m_c[2] * .0722f;
const float cr1 = pE1->m_c[0] - l1;
const float cb1 = pE1->m_c[2] - l1;
const float l2 = pE2->m_c[0] * .2126f + pE2->m_c[1] * .7152f + pE2->m_c[2] * .0722f;
const float cr2 = pE2->m_c[0] - l2;
const float cb2 = pE2->m_c[2] - l2;
float dl = l1 - l2;
float dcr = cr1 - cr2;
float dcb = cb1 - cb2;
return (int64_t)(weights[0] * (dl * dl) + weights[1] * pr_weight * (dcr * dcr) + weights[2] * pb_weight * (dcb * dcb));
}
else
{
float dr = (float)pE1->m_c[0] - (float)pE2->m_c[0];
float dg = (float)pE1->m_c[1] - (float)pE2->m_c[1];
float db = (float)pE1->m_c[2] - (float)pE2->m_c[2];
return (int64_t)(weights[0] * dr * dr + weights[1] * dg * dg + weights[2] * db * db);
}
}
static inline uint64_t compute_color_distance_rgba(const varying color_quad_i *uniform pE1, const varying color_quad_i *uniform pE2, uniform bool perceptual, const uint32_t uniform weights[4])
{
float da = (float)pE1->m_c[3] - (float)pE2->m_c[3];
float a_err = weights[3] * (da * da);
if (perceptual)
{
const float l1 = pE1->m_c[0] * .2126f + pE1->m_c[1] * .7152f + pE1->m_c[2] * .0722f;
const float cr1 = pE1->m_c[0] - l1;
const float cb1 = pE1->m_c[2] - l1;
const float l2 = pE2->m_c[0] * .2126f + pE2->m_c[1] * .7152f + pE2->m_c[2] * .0722f;
const float cr2 = pE2->m_c[0] - l2;
const float cb2 = pE2->m_c[2] - l2;
float dl = l1 - l2;
float dcr = cr1 - cr2;
float dcb = cb1 - cb2;
return (int64_t)(weights[0] * (dl * dl) + weights[1] * pr_weight * (dcr * dcr) + weights[2] * pb_weight * (dcb * dcb) + a_err);
}
else
{
float dr = (float)pE1->m_c[0] - (float)pE2->m_c[0];
float dg = (float)pE1->m_c[1] - (float)pE2->m_c[1];
float db = (float)pE1->m_c[2] - (float)pE2->m_c[2];
return (int64_t)(weights[0] * dr * dr + weights[1] * dg * dg + weights[2] * db * db + a_err);
}
}
static uint64_t pack_mode1_to_one_color(const uniform color_cell_compressor_params *uniform pParams, varying color_cell_compressor_results *uniform pResults, uint32_t r, uint32_t g, uint32_t b,
varying int *uniform pSelectors, uint32_t num_pixels, const varying color_quad_i *uniform pPixels)
{
uint32_t best_err = UINT_MAX;
uint32_t best_p = 0;
for (uniform uint32_t p = 0; p < 2; p++)
{
#pragma ignore warning(perf)
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];
#pragma ignore warning(perf)
color_quad_i_set(&pResults->m_low_endpoint, pEr->m_lo, pEg->m_lo, pEb->m_lo, 0);
#pragma ignore warning(perf)
color_quad_i_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;
for (uniform uint32_t i = 0; i < num_pixels; i++)
pSelectors[i] = BC7E_MODE_1_OPTIMAL_INDEX;
color_quad_i p;
for (uniform 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] = (low * (64 - g_bc7_weights3[BC7E_MODE_1_OPTIMAL_INDEX]) + high * g_bc7_weights3[BC7E_MODE_1_OPTIMAL_INDEX] + 32) >> 6;
}
p.m_c[3] = 255;
uint64_t total_err = 0;
for (uniform uint32_t i = 0; i < num_pixels; i++)
total_err += compute_color_distance_rgb(&p, &pPixels[i], pParams->m_perceptual, pParams->m_weights);
pResults->m_best_overall_err = total_err;
return total_err;
}
static uint64_t pack_mode24_to_one_color(const uniform color_cell_compressor_params *uniform pParams, varying color_cell_compressor_results *uniform pResults, uint32_t r, uint32_t g, uint32_t b,
varying int *uniform pSelectors, uint32_t num_pixels, const varying color_quad_i *uniform pPixels)
{
uint32_t er, eg, eb;
if (pParams->m_num_selector_weights == 8)
{
er = g_bc7_mode_4_optimal_endpoints3[r];
eg = g_bc7_mode_4_optimal_endpoints3[g];
eb = g_bc7_mode_4_optimal_endpoints3[b];
}
else
{
er = g_bc7_mode_4_optimal_endpoints2[r];
eg = g_bc7_mode_4_optimal_endpoints2[g];
eb = g_bc7_mode_4_optimal_endpoints2[b];
}
color_quad_i_set(&pResults->m_low_endpoint, er & 0xFF, eg & 0xFF, eb & 0xFF, 0);
color_quad_i_set(&pResults->m_high_endpoint, er >> 8, eg >> 8, eb >> 8, 0);
for (uniform uint32_t i = 0; i < num_pixels; i++)
pSelectors[i] = (pParams->m_num_selector_weights == 8) ? BC7E_MODE_4_OPTIMAL_INDEX3 : BC7E_MODE_4_OPTIMAL_INDEX2;