-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathastcenc_color_quantize.cpp
More file actions
2149 lines (1848 loc) · 57.4 KB
/
astcenc_color_quantize.cpp
File metadata and controls
2149 lines (1848 loc) · 57.4 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
// SPDX-License-Identifier: Apache-2.0
// ----------------------------------------------------------------------------
// Copyright 2011-2023 Arm Limited
//
// 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.
// ----------------------------------------------------------------------------
#if !defined(ASTCENC_DECOMPRESS_ONLY)
/**
* @brief Functions for color quantization.
*
* The design of the color quantization functionality requires the caller to use higher level error
* analysis to determine the base encoding that should be used. This earlier analysis will select
* the basic type of the endpoint that should be used:
*
* * Mode: LDR or HDR
* * Quantization level
* * Channel count: L, LA, RGB, or RGBA
* * Endpoint 2 type: Direct color endcode, or scaled from endpoint 1.
*
* However, this leaves a number of decisions about exactly how to pack the endpoints open. In
* particular we need to determine if blue contraction can be used, or/and if delta encoding can be
* used. If they can be applied these will allow us to maintain higher precision in the endpoints
* without needing additional storage.
*/
#include <stdio.h>
#include <assert.h>
#include "astcenc_internal.h"
/**
* @brief Compute the error of an LDR RGB or RGBA encoding.
*
* @param uquant0 The original endpoint 0 color.
* @param uquant1 The original endpoint 1 color.
* @param quant0 The unpacked quantized endpoint 0 color.
* @param quant1 The unpacked quantized endpoint 1 color.
*
* @return The MSE of the encoding.
*/
static float get_rgba_encoding_error(
vfloat4 uquant0,
vfloat4 uquant1,
vint4 quant0,
vint4 quant1
) {
vfloat4 error0 = uquant0 - int_to_float(quant0);
vfloat4 error1 = uquant1 - int_to_float(quant1);
return hadd_s(error0 * error0 + error1 * error1);
}
/**
* @brief Determine the quantized value given a quantization level.
*
* @param quant_level The quantization level to use.
* @param value The value to convert. This must be in the 0-255 range.
*
* @return The unpacked quantized value, returned in 0-255 range.
*/
static inline uint8_t quant_color(
quant_method quant_level,
int value
) {
int index = value * 2 + 1;
return color_unquant_to_uquant_tables[quant_level - QUANT_6][index];
}
/**
* @brief Determine the quantized value given a quantization level.
*
* @param quant_level The quantization level to use.
* @param value The value to convert. This must be in the 0-255 range.
*
* @return The unpacked quantized value, returned in 0-255 range.
*/
static inline vint4 quant_color3(
quant_method quant_level,
vint4 value
) {
vint4 index = value * 2 + 1;
return vint4(
color_unquant_to_uquant_tables[quant_level - QUANT_6][index.lane<0>()],
color_unquant_to_uquant_tables[quant_level - QUANT_6][index.lane<1>()],
color_unquant_to_uquant_tables[quant_level - QUANT_6][index.lane<2>()],
0);
}
/**
* @brief Determine the quantized value given a quantization level and residual.
*
* @param quant_level The quantization level to use.
* @param value The value to convert. This must be in the 0-255 range.
* @param valuef The original value before rounding, used to compute a residual.
*
* @return The unpacked quantized value, returned in 0-255 range.
*/
static inline uint8_t quant_color(
quant_method quant_level,
int value,
float valuef
) {
int index = value * 2;
// Compute the residual to determine if we should round down or up ties.
// Test should be residual >= 0, but empirical testing shows small bias helps.
float residual = valuef - static_cast<float>(value);
if (residual >= -0.1f)
{
index++;
}
return color_unquant_to_uquant_tables[quant_level - QUANT_6][index];
}
/**
* @brief Determine the quantized value given a quantization level and residual.
*
* @param quant_level The quantization level to use.
* @param value The value to convert. This must be in the 0-255 range.
* @param valuef The original value before rounding, used to compute a residual.
*
* @return The unpacked quantized value, returned in 0-255 range.
*/
static inline vint4 quant_color3(
quant_method quant_level,
vint4 value,
vfloat4 valuef
) {
vint4 index = value * 2;
// Compute the residual to determine if we should round down or up ties.
// Test should be residual >= 0, but empirical testing shows small bias helps.
vfloat4 residual = valuef - int_to_float(value);
vmask4 mask = residual >= vfloat4(-0.1f);
index = select(index, index + 1, mask);
return vint4(
color_unquant_to_uquant_tables[quant_level - QUANT_6][index.lane<0>()],
color_unquant_to_uquant_tables[quant_level - QUANT_6][index.lane<1>()],
color_unquant_to_uquant_tables[quant_level - QUANT_6][index.lane<2>()],
0);
}
/**
* @brief Quantize an LDR RGB color.
*
* Since this is a fall-back encoding, we cannot actually fail but must produce a sensible result.
* For this encoding @c color0 cannot be larger than @c color1. If @c color0 is actually larger
* than @c color1, @c color0 is reduced and @c color1 is increased until the constraint is met.
*
* @param color0 The input unquantized color0 endpoint.
* @param color1 The input unquantized color1 endpoint.
* @param[out] color0_out The output quantized color0 endpoint.
* @param[out] color1_out The output quantized color1 endpoint.
* @param quant_level The quantization level to use.
*/
static void quantize_rgb(
vfloat4 color0,
vfloat4 color1,
vint4& color0_out,
vint4& color1_out,
quant_method quant_level
) {
vint4 color0i, color1i;
vfloat4 nudge(0.2f);
do
{
vint4 color0q = max(float_to_int_rtn(color0), vint4(0));
color0i = quant_color3(quant_level, color0q, color0);
color0 = color0 - nudge;
vint4 color1q = min(float_to_int_rtn(color1), vint4(255));
color1i = quant_color3(quant_level, color1q, color1);
color1 = color1 + nudge;
} while (hadd_rgb_s(color0i) > hadd_rgb_s(color1i));
color0_out = color0i;
color1_out = color1i;
}
/**
* @brief Quantize an LDR RGBA color.
*
* Since this is a fall-back encoding, we cannot actually fail but must produce a sensible result.
* For this encoding @c color0.rgb cannot be larger than @c color1.rgb (this indicates blue
* contraction). If @c color0.rgb is actually larger than @c color1.rgb, @c color0.rgb is reduced
* and @c color1.rgb is increased until the constraint is met.
*
* @param color0 The input unquantized color0 endpoint.
* @param color1 The input unquantized color1 endpoint.
* @param[out] color0_out The output quantized color0 endpoint.
* @param[out] color1_out The output quantized color1 endpoint.
* @param quant_level The quantization level to use.
*/
static void quantize_rgba(
vfloat4 color0,
vfloat4 color1,
vint4& color0_out,
vint4& color1_out,
quant_method quant_level
) {
quantize_rgb(color0, color1, color0_out, color1_out, quant_level);
float a0 = color0.lane<3>();
float a1 = color1.lane<3>();
color0_out.set_lane<3>(quant_color(quant_level, astc::flt2int_rtn(a0), a0));
color1_out.set_lane<3>(quant_color(quant_level, astc::flt2int_rtn(a1), a1));
}
/**
* @brief Try to quantize an LDR RGB color using blue-contraction.
*
* Blue-contraction is only usable if encoded color 1 is larger than color 0.
*
* @param color0 The input unquantized color0 endpoint.
* @param color1 The input unquantized color1 endpoint.
* @param[out] color0_out The output quantized color0 endpoint.
* @param[out] color1_out The output quantized color1 endpoint.
* @param quant_level The quantization level to use.
*
* @return Returns @c false on failure, @c true on success.
*/
static bool try_quantize_rgb_blue_contract(
vfloat4 color0,
vfloat4 color1,
vint4& color0_out,
vint4& color1_out,
quant_method quant_level
) {
// Apply inverse blue-contraction
color0 += color0 - color0.swz<2, 2, 2, 3>();
color1 += color1 - color1.swz<2, 2, 2, 3>();
// If anything overflows BC cannot be used
vmask4 color0_error = (color0 < vfloat4(0.0f)) | (color0 > vfloat4(255.0f));
vmask4 color1_error = (color1 < vfloat4(0.0f)) | (color1 > vfloat4(255.0f));
if (any(color0_error | color1_error))
{
return false;
}
// Quantize the inverse blue-contracted color
vint4 color0i = quant_color3(quant_level, float_to_int_rtn(color0), color0);
vint4 color1i = quant_color3(quant_level, float_to_int_rtn(color1), color1);
// If color #1 is not larger than color #0 then blue-contraction cannot be used
// We must test afterwards because quantization can change the order
if (hadd_rgb_s(color1i) <= hadd_rgb_s(color0i))
{
return false;
}
color0_out = color1i;
color1_out = color0i;
return true;
}
/**
* @brief Try to quantize an LDR RGBA color using blue-contraction.
*
* Blue-contraction is only usable if encoded color 1 RGB is larger than color 0 RGB.
*
* @param color0 The input unquantized color0 endpoint.
* @param color1 The input unquantized color1 endpoint.
* @param[out] color0_out The output quantized color0 endpoint.
* @param[out] color1_out The output quantized color1 endpoint.
* @param quant_level The quantization level to use.
*
* @return Returns @c false on failure, @c true on success.
*/
static bool try_quantize_rgba_blue_contract(
vfloat4 color0,
vfloat4 color1,
vint4& color0_out,
vint4& color1_out,
quant_method quant_level
) {
if (try_quantize_rgb_blue_contract(color0, color1, color0_out, color1_out, quant_level))
{
float a0 = color0.lane<3>();
float a1 = color1.lane<3>();
color0_out.set_lane<3>(quant_color(quant_level, astc::flt2int_rtn(a1), a1));
color1_out.set_lane<3>(quant_color(quant_level, astc::flt2int_rtn(a0), a0));
return true;
}
return false;
}
/**
* @brief Try to quantize an LDR RGB color using delta encoding.
*
* At decode time we move one bit from the offset to the base and seize another bit as a sign bit;
* we then unquantize both values as if they contain one extra bit. If the sum of the offsets is
* non-negative, then we encode a regular delta.
*
* @param color0 The input unquantized color0 endpoint.
* @param color1 The input unquantized color1 endpoint.
* @param[out] color0_out The output quantized color0 endpoint.
* @param[out] color1_out The output quantized color1 endpoint.
* @param quant_level The quantization level to use.
*
* @return Returns @c false on failure, @c true on success.
*/
static bool try_quantize_rgb_delta(
vfloat4 color0,
vfloat4 color1,
vint4& color0_out,
vint4& color1_out,
quant_method quant_level
) {
// Transform color0 to unorm9
vint4 color0a = float_to_int_rtn(color0);
color0.set_lane<3>(0.0f);
color0a = lsl<1>(color0a);
// Mask off the top bit
vint4 color0b = color0a & 0xFF;
// Quantize then unquantize in order to get a value that we take differences against
vint4 color0be = quant_color3(quant_level, color0b);
color0b = color0be | (color0a & 0x100);
// Get hold of the second value
vint4 color1d = float_to_int_rtn(color1);
color1d = lsl<1>(color1d);
// ... and take differences
color1d = color1d - color0b;
color1d.set_lane<3>(0);
// Check if the difference is too large to be encodable
if (any((color1d > vint4(63)) | (color1d < vint4(-64))))
{
return false;
}
// Insert top bit of the base into the offset
color1d = color1d & 0x7F;
color1d = color1d | lsr<1>(color0b & 0x100);
// Then quantize and unquantize; if this causes either top two bits to flip, then encoding fails
// since we have then corrupted either the top bit of the base or the sign bit of the offset
vint4 color1de = quant_color3(quant_level, color1d);
vint4 color_flips = (color1d ^ color1de) & 0xC0;
color_flips.set_lane<3>(0);
if (any(color_flips != vint4::zero()))
{
return false;
}
// If the sum of offsets triggers blue-contraction then encoding fails
vint4 ep0 = color0be;
vint4 ep1 = color1de;
bit_transfer_signed(ep1, ep0);
if (hadd_rgb_s(ep1) < 0)
{
return false;
}
// Check that the offsets produce legitimate sums as well
ep0 = ep0 + ep1;
if (any((ep0 < vint4(0)) | (ep0 > vint4(0xFF))))
{
return false;
}
color0_out = color0be;
color1_out = color1de;
return true;
}
/**
* @brief Try to quantize an LDR RGB color using delta encoding and blue-contraction.
*
* Blue-contraction is only usable if encoded color 1 RGB is larger than color 0 RGB.
*
* @param color0 The input unquantized color0 endpoint.
* @param color1 The input unquantized color1 endpoint.
* @param[out] color0_out The output quantized color0 endpoint.
* @param[out] color1_out The output quantized color1 endpoint.
* @param quant_level The quantization level to use.
*
* @return Returns @c false on failure, @c true on success.
*/
static bool try_quantize_rgb_delta_blue_contract(
vfloat4 color0,
vfloat4 color1,
vint4& color0_out,
vint4& color1_out,
quant_method quant_level
) {
// Note: Switch around endpoint colors already at start
std::swap(color0, color1);
// Apply inverse blue-contraction
color0 += color0 - color0.swz<2, 2, 2, 3>();
color1 += color1 - color1.swz<2, 2, 2, 3>();
// If anything overflows BC cannot be used
vmask4 color0_error = (color0 < vfloat4(0.0f)) | (color0 > vfloat4(255.0f));
vmask4 color1_error = (color1 < vfloat4(0.0f)) | (color1 > vfloat4(255.0f));
if (any(color0_error | color1_error))
{
return false;
}
// Transform color0 to unorm9
vint4 color0a = float_to_int_rtn(color0);
color0.set_lane<3>(0.0f);
color0a = lsl<1>(color0a);
// Mask off the top bit
vint4 color0b = color0a & 0xFF;
// Quantize then unquantize in order to get a value that we take differences against
vint4 color0be = quant_color3(quant_level, color0b);
color0b = color0be | (color0a & 0x100);
// Get hold of the second value
vint4 color1d = float_to_int_rtn(color1);
color1d = lsl<1>(color1d);
// ... and take differences
color1d = color1d - color0b;
color1d.set_lane<3>(0);
// Check if the difference is too large to be encodable
if (any((color1d > vint4(63)) | (color1d < vint4(-64))))
{
return false;
}
// Insert top bit of the base into the offset
color1d = color1d & 0x7F;
color1d = color1d | lsr<1>(color0b & 0x100);
// Then quantize and unquantize; if this causes either top two bits to flip, then encoding fails
// since we have then corrupted either the top bit of the base or the sign bit of the offset
vint4 color1de = quant_color3(quant_level, color1d);
vint4 color_flips = (color1d ^ color1de) & 0xC0;
color_flips.set_lane<3>(0);
if (any(color_flips != vint4::zero()))
{
return false;
}
// If the sum of offsets does not trigger blue-contraction then encoding fails
vint4 ep0 = color0be;
vint4 ep1 = color1de;
bit_transfer_signed(ep1, ep0);
if (hadd_rgb_s(ep1) >= 0)
{
return false;
}
// Check that the offsets produce legitimate sums as well
ep0 = ep0 + ep1;
if (any((ep0 < vint4(0)) | (ep0 > vint4(0xFF))))
{
return false;
}
color0_out = color0be;
color1_out = color1de;
return true;
}
/**
* @brief Try to quantize an LDR A color using delta encoding.
*
* At decode time we move one bit from the offset to the base and seize another bit as a sign bit;
* we then unquantize both values as if they contain one extra bit. If the sum of the offsets is
* non-negative, then we encode a regular delta.
*
* This function only compressed the alpha - the other elements in the output array are not touched.
*
* @param color0 The input unquantized color0 endpoint.
* @param color1 The input unquantized color1 endpoint.
* @param[out] color0_out The output quantized color0 endpoint; must preserve lane 0/1/2.
* @param[out] color1_out The output quantized color1 endpoint; must preserve lane 0/1/2.
* @param quant_level The quantization level to use.
*
* @return Returns @c false on failure, @c true on success.
*/
static bool try_quantize_alpha_delta(
vfloat4 color0,
vfloat4 color1,
vint4& color0_out,
vint4& color1_out,
quant_method quant_level
) {
float a0 = color0.lane<3>();
float a1 = color1.lane<3>();
int a0a = astc::flt2int_rtn(a0);
a0a <<= 1;
int a0b = a0a & 0xFF;
int a0be = quant_color(quant_level, a0b);
a0b = a0be;
a0b |= a0a & 0x100;
int a1d = astc::flt2int_rtn(a1);
a1d <<= 1;
a1d -= a0b;
if (a1d > 63 || a1d < -64)
{
return false;
}
a1d &= 0x7F;
a1d |= (a0b & 0x100) >> 1;
int a1de = quant_color(quant_level, a1d);
int a1du = a1de;
if ((a1d ^ a1du) & 0xC0)
{
return false;
}
a1du &= 0x7F;
if (a1du & 0x40)
{
a1du -= 0x80;
}
a1du += a0b;
if (a1du < 0 || a1du > 0x1FF)
{
return false;
}
color0_out.set_lane<3>(a0be);
color1_out.set_lane<3>(a1de);
return true;
}
/**
* @brief Try to quantize an LDR LA color using delta encoding.
*
* At decode time we move one bit from the offset to the base and seize another bit as a sign bit;
* we then unquantize both values as if they contain one extra bit. If the sum of the offsets is
* non-negative, then we encode a regular delta.
*
* This function only compressed the alpha - the other elements in the output array are not touched.
*
* @param color0 The input unquantized color0 endpoint.
* @param color1 The input unquantized color1 endpoint.
* @param[out] output The output endpoints, returned as (l0, l1, a0, a1).
* @param quant_level The quantization level to use.
*
* @return Returns @c false on failure, @c true on success.
*/
static bool try_quantize_luminance_alpha_delta(
vfloat4 color0,
vfloat4 color1,
uint8_t output[4],
quant_method quant_level
) {
float l0 = hadd_rgb_s(color0) * (1.0f / 3.0f);
float l1 = hadd_rgb_s(color1) * (1.0f / 3.0f);
float a0 = color0.lane<3>();
float a1 = color1.lane<3>();
int l0a = astc::flt2int_rtn(l0);
int a0a = astc::flt2int_rtn(a0);
l0a <<= 1;
a0a <<= 1;
int l0b = l0a & 0xFF;
int a0b = a0a & 0xFF;
int l0be = quant_color(quant_level, l0b);
int a0be = quant_color(quant_level, a0b);
l0b = l0be;
a0b = a0be;
l0b |= l0a & 0x100;
a0b |= a0a & 0x100;
int l1d = astc::flt2int_rtn(l1);
int a1d = astc::flt2int_rtn(a1);
l1d <<= 1;
a1d <<= 1;
l1d -= l0b;
a1d -= a0b;
if (l1d > 63 || l1d < -64)
{
return false;
}
if (a1d > 63 || a1d < -64)
{
return false;
}
l1d &= 0x7F;
a1d &= 0x7F;
l1d |= (l0b & 0x100) >> 1;
a1d |= (a0b & 0x100) >> 1;
int l1de = quant_color(quant_level, l1d);
int a1de = quant_color(quant_level, a1d);
int l1du = l1de;
int a1du = a1de;
if ((l1d ^ l1du) & 0xC0)
{
return false;
}
if ((a1d ^ a1du) & 0xC0)
{
return false;
}
l1du &= 0x7F;
a1du &= 0x7F;
if (l1du & 0x40)
{
l1du -= 0x80;
}
if (a1du & 0x40)
{
a1du -= 0x80;
}
l1du += l0b;
a1du += a0b;
if (l1du < 0 || l1du > 0x1FF)
{
return false;
}
if (a1du < 0 || a1du > 0x1FF)
{
return false;
}
output[0] = static_cast<uint8_t>(l0be);
output[1] = static_cast<uint8_t>(l1de);
output[2] = static_cast<uint8_t>(a0be);
output[3] = static_cast<uint8_t>(a1de);
return true;
}
/**
* @brief Try to quantize an LDR RGBA color using delta encoding.
*
* At decode time we move one bit from the offset to the base and seize another bit as a sign bit;
* we then unquantize both values as if they contain one extra bit. If the sum of the offsets is
* non-negative, then we encode a regular delta.
*
* This function only compressed the alpha - the other elements in the output array are not touched.
*
* @param color0 The input unquantized color0 endpoint.
* @param color1 The input unquantized color1 endpoint.
* @param[out] color0_out The output quantized color0 endpoint
* @param[out] color1_out The output quantized color1 endpoint
* @param quant_level The quantization level to use.
*
* @return Returns @c false on failure, @c true on success.
*/
static bool try_quantize_rgba_delta(
vfloat4 color0,
vfloat4 color1,
vint4& color0_out,
vint4& color1_out,
quant_method quant_level
) {
return try_quantize_rgb_delta(color0, color1, color0_out, color1_out, quant_level) &&
try_quantize_alpha_delta(color0, color1, color0_out, color1_out, quant_level);
}
/**
* @brief Try to quantize an LDR RGBA color using delta and blue contract encoding.
*
* At decode time we move one bit from the offset to the base and seize another bit as a sign bit;
* we then unquantize both values as if they contain one extra bit. If the sum of the offsets is
* non-negative, then we encode a regular delta.
*
* This function only compressed the alpha - the other elements in the output array are not touched.
*
* @param color0 The input unquantized color0 endpoint.
* @param color1 The input unquantized color1 endpoint.
* @param[out] color0_out The output quantized color0 endpoint
* @param[out] color1_out The output quantized color1 endpoint
* @param quant_level The quantization level to use.
*
* @return Returns @c false on failure, @c true on success.
*/
static bool try_quantize_rgba_delta_blue_contract(
vfloat4 color0,
vfloat4 color1,
vint4& color0_out,
vint4& color1_out,
quant_method quant_level
) {
// Note that we swap the color0 and color1 ordering for alpha to match RGB blue-contract
return try_quantize_rgb_delta_blue_contract(color0, color1, color0_out, color1_out, quant_level) &&
try_quantize_alpha_delta(color1, color0, color0_out, color1_out, quant_level);
}
/**
* @brief Quantize an LDR RGB color using scale encoding.
*
* @param color The input unquantized color endpoint and scale factor.
* @param[out] output The output endpoints, returned as (r0, g0, b0, s).
* @param quant_level The quantization level to use.
*/
static void quantize_rgbs(
vfloat4 color,
uint8_t output[4],
quant_method quant_level
) {
float scale = 1.0f / 257.0f;
float r = astc::clamp255f(color.lane<0>() * scale);
float g = astc::clamp255f(color.lane<1>() * scale);
float b = astc::clamp255f(color.lane<2>() * scale);
int ri = quant_color(quant_level, astc::flt2int_rtn(r), r);
int gi = quant_color(quant_level, astc::flt2int_rtn(g), g);
int bi = quant_color(quant_level, astc::flt2int_rtn(b), b);
float oldcolorsum = hadd_rgb_s(color) * scale;
float newcolorsum = static_cast<float>(ri + gi + bi);
float scalea = astc::clamp1f(color.lane<3>() * (oldcolorsum + 1e-10f) / (newcolorsum + 1e-10f));
int scale_idx = astc::flt2int_rtn(scalea * 256.0f);
scale_idx = astc::clamp(scale_idx, 0, 255);
output[0] = static_cast<uint8_t>(ri);
output[1] = static_cast<uint8_t>(gi);
output[2] = static_cast<uint8_t>(bi);
output[3] = quant_color(quant_level, scale_idx);
}
/**
* @brief Quantize an LDR RGBA color using scale encoding.
*
* @param color0 The input unquantized color0 alpha endpoint.
* @param color1 The input unquantized color1 alpha endpoint.
* @param color The input unquantized color endpoint and scale factor.
* @param[out] output The output endpoints, returned as (r0, g0, b0, s, a0, a1).
* @param quant_level The quantization level to use.
*/
static void quantize_rgbs_alpha(
vfloat4 color0,
vfloat4 color1,
vfloat4 color,
uint8_t output[6],
quant_method quant_level
) {
float a0 = color0.lane<3>();
float a1 = color1.lane<3>();
output[4] = quant_color(quant_level, astc::flt2int_rtn(a0), a0);
output[5] = quant_color(quant_level, astc::flt2int_rtn(a1), a1);
quantize_rgbs(color, output, quant_level);
}
/**
* @brief Quantize a LDR L color.
*
* @param color0 The input unquantized color0 endpoint.
* @param color1 The input unquantized color1 endpoint.
* @param[out] output The output endpoints, returned as (l0, l1).
* @param quant_level The quantization level to use.
*/
static void quantize_luminance(
vfloat4 color0,
vfloat4 color1,
uint8_t output[2],
quant_method quant_level
) {
float lum0 = hadd_rgb_s(color0) * (1.0f / 3.0f);
float lum1 = hadd_rgb_s(color1) * (1.0f / 3.0f);
if (lum0 > lum1)
{
float avg = (lum0 + lum1) * 0.5f;
lum0 = avg;
lum1 = avg;
}
output[0] = quant_color(quant_level, astc::flt2int_rtn(lum0), lum0);
output[1] = quant_color(quant_level, astc::flt2int_rtn(lum1), lum1);
}
/**
* @brief Quantize a LDR LA color.
*
* @param color0 The input unquantized color0 endpoint.
* @param color1 The input unquantized color1 endpoint.
* @param[out] output The output endpoints, returned as (l0, l1, a0, a1).
* @param quant_level The quantization level to use.
*/
static void quantize_luminance_alpha(
vfloat4 color0,
vfloat4 color1,
uint8_t output[4],
quant_method quant_level
) {
float lum0 = hadd_rgb_s(color0) * (1.0f / 3.0f);
float lum1 = hadd_rgb_s(color1) * (1.0f / 3.0f);
float a0 = color0.lane<3>();
float a1 = color1.lane<3>();
output[0] = quant_color(quant_level, astc::flt2int_rtn(lum0), lum0);
output[1] = quant_color(quant_level, astc::flt2int_rtn(lum1), lum1);
output[2] = quant_color(quant_level, astc::flt2int_rtn(a0), a0);
output[3] = quant_color(quant_level, astc::flt2int_rtn(a1), a1);
}
/**
* @brief Quantize and unquantize a value ensuring top two bits are the same.
*
* @param quant_level The quantization level to use.
* @param value The input unquantized value.
* @param[out] quant_value The quantized value.
*/
static inline void quantize_and_unquantize_retain_top_two_bits(
quant_method quant_level,
uint8_t value,
uint8_t& quant_value
) {
int perform_loop;
uint8_t quantval;
do
{
quantval = quant_color(quant_level, value);
// Perform looping if the top two bits were modified by quant/unquant
perform_loop = (value & 0xC0) != (quantval & 0xC0);
if ((quantval & 0xC0) > (value & 0xC0))
{
// Quant/unquant rounded UP so that the top two bits changed;
// decrement the input in hopes that this will avoid rounding up.
value--;
}
else if ((quantval & 0xC0) < (value & 0xC0))
{
// Quant/unquant rounded DOWN so that the top two bits changed;
// decrement the input in hopes that this will avoid rounding down.
value--;
}
} while (perform_loop);
quant_value = quantval;
}
/**
* @brief Quantize and unquantize a value ensuring top four bits are the same.
*
* @param quant_level The quantization level to use.
* @param value The input unquantized value.
* @param[out] quant_value The quantized value in 0-255 range.
*/
static inline void quantize_and_unquantize_retain_top_four_bits(
quant_method quant_level,
uint8_t value,
uint8_t& quant_value
) {
uint8_t perform_loop;
uint8_t quantval;
do
{
quantval = quant_color(quant_level, value);
// Perform looping if the top four bits were modified by quant/unquant
perform_loop = (value & 0xF0) != (quantval & 0xF0);
if ((quantval & 0xF0) > (value & 0xF0))
{
// Quant/unquant rounded UP so that the top four bits changed;
// decrement the input value in hopes that this will avoid rounding up.
value--;
}
else if ((quantval & 0xF0) < (value & 0xF0))
{
// Quant/unquant rounded DOWN so that the top four bits changed;
// decrement the input value in hopes that this will avoid rounding down.
value--;
}
} while (perform_loop);
quant_value = quantval;
}
/**
* @brief Quantize a HDR RGB color using RGB + offset.
*
* @param color The input unquantized color endpoint and offset.
* @param[out] output The output endpoints, returned as packed RGBS with some mode bits.
* @param quant_level The quantization level to use.
*/
static void quantize_hdr_rgbo(
vfloat4 color,
uint8_t output[4],
quant_method quant_level
) {
color.set_lane<0>(color.lane<0>() + color.lane<3>());
color.set_lane<1>(color.lane<1>() + color.lane<3>());
color.set_lane<2>(color.lane<2>() + color.lane<3>());
color = clamp(0.0f, 65535.0f, color);
vfloat4 color_bak = color;
int majcomp;
if (color.lane<0>() > color.lane<1>() && color.lane<0>() > color.lane<2>())
{
majcomp = 0; // red is largest component
}
else if (color.lane<1>() > color.lane<2>())
{
majcomp = 1; // green is largest component
}
else
{
majcomp = 2; // blue is largest component
}
// swap around the red component and the largest component.
switch (majcomp)
{
case 1:
color = color.swz<1, 0, 2, 3>();
break;
case 2:
color = color.swz<2, 1, 0, 3>();
break;
default:
break;
}
static const int mode_bits[5][3] {
{11, 5, 7},
{11, 6, 5},
{10, 5, 8},
{9, 6, 7},
{8, 7, 6}
};
static const float mode_cutoffs[5][2] {
{1024, 4096},
{2048, 1024},
{2048, 16384},
{8192, 16384},
{32768, 16384}
};
static const float mode_rscales[5] {
32.0f,
32.0f,
64.0f,
128.0f,
256.0f,
};
static const float mode_scales[5] {
1.0f / 32.0f,
1.0f / 32.0f,
1.0f / 64.0f,
1.0f / 128.0f,
1.0f / 256.0f,
};
float r_base = color.lane<0>();
float g_base = color.lane<0>() - color.lane<1>() ;
float b_base = color.lane<0>() - color.lane<2>() ;
float s_base = color.lane<3>() ;