-
Notifications
You must be signed in to change notification settings - Fork 254
/
Copy pathastcenc_compress_symbolic.cpp
1456 lines (1214 loc) · 48.2 KB
/
astcenc_compress_symbolic.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
// SPDX-License-Identifier: Apache-2.0
// ----------------------------------------------------------------------------
// Copyright 2011-2024 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 to compress a symbolic block.
*/
#include "astcenc_internal.h"
#include "astcenc_diagnostic_trace.h"
#include <cassert>
/**
* @brief Merge two planes of endpoints into a single vector.
*
* @param ep_plane1 The endpoints for plane 1.
* @param ep_plane2 The endpoints for plane 2.
* @param component_plane2 The color component for plane 2.
* @param[out] result The merged output.
*/
static void merge_endpoints(
const endpoints& ep_plane1,
const endpoints& ep_plane2,
unsigned int component_plane2,
endpoints& result
) {
unsigned int partition_count = ep_plane1.partition_count;
assert(partition_count == 1);
vmask4 sep_mask = vint4::lane_id() == vint4(component_plane2);
result.partition_count = partition_count;
result.endpt0[0] = select(ep_plane1.endpt0[0], ep_plane2.endpt0[0], sep_mask);
result.endpt1[0] = select(ep_plane1.endpt1[0], ep_plane2.endpt1[0], sep_mask);
}
/**
* @brief Attempt to improve weights given a chosen configuration.
*
* Given a fixed weight grid decimation and weight value quantization, iterate over all weights (per
* partition and per plane) and attempt to improve image quality by moving each weight up by one or
* down by one quantization step.
*
* This is a specialized function which only supports operating on undecimated weight grids,
* therefore primarily improving the performance of 4x4 and 5x5 blocks where grid decimation
* is needed less often.
*
* @param decode_mode The decode mode (LDR, HDR).
* @param bsd The block size information.
* @param blk The image block color data to compress.
* @param[out] scb The symbolic compressed block output.
*/
static bool realign_weights_undecimated(
astcenc_profile decode_mode,
const block_size_descriptor& bsd,
const image_block& blk,
symbolic_compressed_block& scb
) {
// Get the partition descriptor
unsigned int partition_count = scb.partition_count;
const auto& pi = bsd.get_partition_info(partition_count, scb.partition_index);
// Get the quantization table
const block_mode& bm = bsd.get_block_mode(scb.block_mode);
unsigned int weight_quant_level = bm.quant_mode;
const quant_and_transfer_table& qat = quant_and_xfer_tables[weight_quant_level];
unsigned int max_plane = bm.is_dual_plane;
int plane2_component = scb.plane2_component;
vmask4 plane_mask = vint4::lane_id() == vint4(plane2_component);
// Decode the color endpoints
bool rgb_hdr;
bool alpha_hdr;
vint4 endpnt0[BLOCK_MAX_PARTITIONS];
vint4 endpnt1[BLOCK_MAX_PARTITIONS];
vfloat4 endpnt0f[BLOCK_MAX_PARTITIONS];
vfloat4 offset[BLOCK_MAX_PARTITIONS];
promise(partition_count > 0);
for (unsigned int pa_idx = 0; pa_idx < partition_count; pa_idx++)
{
unpack_color_endpoints(decode_mode,
scb.color_formats[pa_idx],
scb.color_values[pa_idx],
rgb_hdr, alpha_hdr,
endpnt0[pa_idx],
endpnt1[pa_idx]);
}
uint8_t* dec_weights_uquant = scb.weights;
bool adjustments = false;
// For each plane and partition ...
for (unsigned int pl_idx = 0; pl_idx <= max_plane; pl_idx++)
{
for (unsigned int pa_idx = 0; pa_idx < partition_count; pa_idx++)
{
// Compute the endpoint delta for all components in current plane
vint4 epd = endpnt1[pa_idx] - endpnt0[pa_idx];
epd = select(epd, vint4::zero(), plane_mask);
endpnt0f[pa_idx] = int_to_float(endpnt0[pa_idx]);
offset[pa_idx] = int_to_float(epd) * (1.0f / 64.0f);
}
// For each weight compute previous, current, and next errors
promise(bsd.texel_count > 0);
for (unsigned int texel = 0; texel < bsd.texel_count; texel++)
{
int uqw = dec_weights_uquant[texel];
uint32_t prev_and_next = qat.prev_next_values[uqw];
int uqw_down = prev_and_next & 0xFF;
int uqw_up = (prev_and_next >> 8) & 0xFF;
// Interpolate the colors to create the diffs
float weight_base = static_cast<float>(uqw);
float weight_down = static_cast<float>(uqw_down - uqw);
float weight_up = static_cast<float>(uqw_up - uqw);
unsigned int partition = pi.partition_of_texel[texel];
vfloat4 color_offset = offset[partition];
vfloat4 color_base = endpnt0f[partition];
vfloat4 color = color_base + color_offset * weight_base;
vfloat4 orig_color = blk.texel(texel);
vfloat4 error_weight = blk.channel_weight;
vfloat4 color_diff = color - orig_color;
vfloat4 color_diff_down = color_diff + color_offset * weight_down;
vfloat4 color_diff_up = color_diff + color_offset * weight_up;
float error_base = dot_s(color_diff * color_diff, error_weight);
float error_down = dot_s(color_diff_down * color_diff_down, error_weight);
float error_up = dot_s(color_diff_up * color_diff_up, error_weight);
// Check if the prev or next error is better, and if so use it
if ((error_up < error_base) && (error_up < error_down) && (uqw < 64))
{
dec_weights_uquant[texel] = static_cast<uint8_t>(uqw_up);
adjustments = true;
}
else if ((error_down < error_base) && (uqw > 0))
{
dec_weights_uquant[texel] = static_cast<uint8_t>(uqw_down);
adjustments = true;
}
}
// Prepare iteration for plane 2
dec_weights_uquant += WEIGHTS_PLANE2_OFFSET;
plane_mask = ~plane_mask;
}
return adjustments;
}
/**
* @brief Attempt to improve weights given a chosen configuration.
*
* Given a fixed weight grid decimation and weight value quantization, iterate over all weights (per
* partition and per plane) and attempt to improve image quality by moving each weight up by one or
* down by one quantization step.
*
* @param decode_mode The decode mode (LDR, HDR).
* @param bsd The block size information.
* @param blk The image block color data to compress.
* @param[out] scb The symbolic compressed block output.
*/
static bool realign_weights_decimated(
astcenc_profile decode_mode,
const block_size_descriptor& bsd,
const image_block& blk,
symbolic_compressed_block& scb
) {
// Get the partition descriptor
unsigned int partition_count = scb.partition_count;
const auto& pi = bsd.get_partition_info(partition_count, scb.partition_index);
// Get the quantization table
const block_mode& bm = bsd.get_block_mode(scb.block_mode);
unsigned int weight_quant_level = bm.quant_mode;
const quant_and_transfer_table& qat = quant_and_xfer_tables[weight_quant_level];
// Get the decimation table
const decimation_info& di = bsd.get_decimation_info(bm.decimation_mode);
unsigned int weight_count = di.weight_count;
assert(weight_count != bsd.texel_count);
unsigned int max_plane = bm.is_dual_plane;
int plane2_component = scb.plane2_component;
vmask4 plane_mask = vint4::lane_id() == vint4(plane2_component);
// Decode the color endpoints
bool rgb_hdr;
bool alpha_hdr;
vint4 endpnt0[BLOCK_MAX_PARTITIONS];
vint4 endpnt1[BLOCK_MAX_PARTITIONS];
vfloat4 endpnt0f[BLOCK_MAX_PARTITIONS];
vfloat4 offset[BLOCK_MAX_PARTITIONS];
promise(partition_count > 0);
promise(weight_count > 0);
for (unsigned int pa_idx = 0; pa_idx < partition_count; pa_idx++)
{
unpack_color_endpoints(decode_mode,
scb.color_formats[pa_idx],
scb.color_values[pa_idx],
rgb_hdr, alpha_hdr,
endpnt0[pa_idx],
endpnt1[pa_idx]);
}
uint8_t* dec_weights_uquant = scb.weights;
bool adjustments = false;
// For each plane and partition ...
for (unsigned int pl_idx = 0; pl_idx <= max_plane; pl_idx++)
{
for (unsigned int pa_idx = 0; pa_idx < partition_count; pa_idx++)
{
// Compute the endpoint delta for all components in current plane
vint4 epd = endpnt1[pa_idx] - endpnt0[pa_idx];
epd = select(epd, vint4::zero(), plane_mask);
endpnt0f[pa_idx] = int_to_float(endpnt0[pa_idx]);
offset[pa_idx] = int_to_float(epd) * (1.0f / 64.0f);
}
// Create an unquantized weight grid for this decimation level
ASTCENC_ALIGNAS float uq_weightsf[BLOCK_MAX_WEIGHTS];
for (unsigned int we_idx = 0; we_idx < weight_count; we_idx += ASTCENC_SIMD_WIDTH)
{
vint unquant_value(dec_weights_uquant + we_idx);
vfloat unquant_valuef = int_to_float(unquant_value);
storea(unquant_valuef, uq_weightsf + we_idx);
}
// For each weight compute previous, current, and next errors
for (unsigned int we_idx = 0; we_idx < weight_count; we_idx++)
{
int uqw = dec_weights_uquant[we_idx];
uint32_t prev_and_next = qat.prev_next_values[uqw];
float uqw_base = uq_weightsf[we_idx];
float uqw_down = static_cast<float>(prev_and_next & 0xFF);
float uqw_up = static_cast<float>((prev_and_next >> 8) & 0xFF);
float uqw_diff_down = uqw_down - uqw_base;
float uqw_diff_up = uqw_up - uqw_base;
vfloat4 error_basev = vfloat4::zero();
vfloat4 error_downv = vfloat4::zero();
vfloat4 error_upv = vfloat4::zero();
// Interpolate the colors to create the diffs
unsigned int texels_to_evaluate = di.weight_texel_count[we_idx];
promise(texels_to_evaluate > 0);
for (unsigned int te_idx = 0; te_idx < texels_to_evaluate; te_idx++)
{
unsigned int texel = di.weight_texels_tr[te_idx][we_idx];
float tw_base = di.texel_contrib_for_weight[te_idx][we_idx];
float weight_base = (uq_weightsf[di.texel_weights_tr[0][texel]] * di.texel_weight_contribs_float_tr[0][texel]
+ uq_weightsf[di.texel_weights_tr[1][texel]] * di.texel_weight_contribs_float_tr[1][texel])
+ (uq_weightsf[di.texel_weights_tr[2][texel]] * di.texel_weight_contribs_float_tr[2][texel]
+ uq_weightsf[di.texel_weights_tr[3][texel]] * di.texel_weight_contribs_float_tr[3][texel]);
// Ideally this is integer rounded, but IQ gain it isn't worth the overhead
// float weight = astc::flt_rd(weight_base + 0.5f);
// float weight_down = astc::flt_rd(weight_base + 0.5f + uqw_diff_down * tw_base) - weight;
// float weight_up = astc::flt_rd(weight_base + 0.5f + uqw_diff_up * tw_base) - weight;
float weight_down = weight_base + uqw_diff_down * tw_base - weight_base;
float weight_up = weight_base + uqw_diff_up * tw_base - weight_base;
unsigned int partition = pi.partition_of_texel[texel];
vfloat4 color_offset = offset[partition];
vfloat4 color_base = endpnt0f[partition];
vfloat4 color = color_base + color_offset * weight_base;
vfloat4 orig_color = blk.texel(texel);
vfloat4 color_diff = color - orig_color;
vfloat4 color_down_diff = color_diff + color_offset * weight_down;
vfloat4 color_up_diff = color_diff + color_offset * weight_up;
error_basev += color_diff * color_diff;
error_downv += color_down_diff * color_down_diff;
error_upv += color_up_diff * color_up_diff;
}
vfloat4 error_weight = blk.channel_weight;
float error_base = hadd_s(error_basev * error_weight);
float error_down = hadd_s(error_downv * error_weight);
float error_up = hadd_s(error_upv * error_weight);
// Check if the prev or next error is better, and if so use it
if ((error_up < error_base) && (error_up < error_down) && (uqw < 64))
{
uq_weightsf[we_idx] = uqw_up;
dec_weights_uquant[we_idx] = static_cast<uint8_t>(uqw_up);
adjustments = true;
}
else if ((error_down < error_base) && (uqw > 0))
{
uq_weightsf[we_idx] = uqw_down;
dec_weights_uquant[we_idx] = static_cast<uint8_t>(uqw_down);
adjustments = true;
}
}
// Prepare iteration for plane 2
dec_weights_uquant += WEIGHTS_PLANE2_OFFSET;
plane_mask = ~plane_mask;
}
return adjustments;
}
/**
* @brief Compress a block using a chosen partitioning and 1 plane of weights.
*
* @param config The compressor configuration.
* @param bsd The block size information.
* @param blk The image block color data to compress.
* @param only_always True if we only use "always" percentile block modes.
* @param tune_errorval_threshold The error value threshold.
* @param partition_count The partition count.
* @param partition_index The partition index if @c partition_count is 2-4.
* @param[out] scb The symbolic compressed block output.
* @param[out] tmpbuf The quantized weights for plane 1.
*/
static float compress_symbolic_block_for_partition_1plane(
const astcenc_config& config,
const block_size_descriptor& bsd,
const image_block& blk,
bool only_always,
float tune_errorval_threshold,
unsigned int partition_count,
unsigned int partition_index,
symbolic_compressed_block& scb,
compression_working_buffers& tmpbuf,
int quant_limit
) {
promise(partition_count > 0);
promise(config.tune_candidate_limit > 0);
promise(config.tune_refinement_limit > 0);
int max_weight_quant = astc::min(static_cast<int>(QUANT_32), quant_limit);
auto compute_difference = &compute_symbolic_block_difference_1plane;
if ((partition_count == 1) && !(config.flags & ASTCENC_FLG_MAP_RGBM))
{
compute_difference = &compute_symbolic_block_difference_1plane_1partition;
}
const auto& pi = bsd.get_partition_info(partition_count, partition_index);
// Compute ideal weights and endpoint colors, with no quantization or decimation
endpoints_and_weights& ei = tmpbuf.ei1;
compute_ideal_colors_and_weights_1plane(blk, pi, ei);
// Compute ideal weights and endpoint colors for every decimation
float* dec_weights_ideal = tmpbuf.dec_weights_ideal;
uint8_t* dec_weights_uquant = tmpbuf.dec_weights_uquant;
// For each decimation mode, compute an ideal set of weights with no quantization
unsigned int max_decimation_modes = only_always ? bsd.decimation_mode_count_always
: bsd.decimation_mode_count_selected;
promise(max_decimation_modes > 0);
for (unsigned int i = 0; i < max_decimation_modes; i++)
{
const auto& dm = bsd.get_decimation_mode(i);
if (!dm.is_ref_1plane(static_cast<quant_method>(max_weight_quant)))
{
continue;
}
const auto& di = bsd.get_decimation_info(i);
compute_ideal_weights_for_decimation(
ei,
di,
dec_weights_ideal + i * BLOCK_MAX_WEIGHTS);
}
// Compute maximum colors for the endpoints and ideal weights, then for each endpoint and ideal
// weight pair, compute the smallest weight that will result in a color value greater than 1
vfloat4 min_ep(10.0f);
for (unsigned int i = 0; i < partition_count; i++)
{
vfloat4 ep = (vfloat4(1.0f) - ei.ep.endpt0[i]) / (ei.ep.endpt1[i] - ei.ep.endpt0[i]);
vmask4 use_ep = (ep > vfloat4(0.5f)) & (ep < min_ep);
min_ep = select(min_ep, ep, use_ep);
}
float min_wt_cutoff = hmin_s(min_ep);
// For each mode, use the angular method to compute a shift
compute_angular_endpoints_1plane(
only_always, bsd, dec_weights_ideal, max_weight_quant, tmpbuf);
float* weight_low_value = tmpbuf.weight_low_value1;
float* weight_high_value = tmpbuf.weight_high_value1;
int8_t* qwt_bitcounts = tmpbuf.qwt_bitcounts;
float* qwt_errors = tmpbuf.qwt_errors;
// For each mode (which specifies a decimation and a quantization):
// * Compute number of bits needed for the quantized weights
// * Generate an optimized set of quantized weights
// * Compute quantization errors for the mode
static const int8_t free_bits_for_partition_count[4] {
115 - 4, 111 - 4 - PARTITION_INDEX_BITS, 108 - 4 - PARTITION_INDEX_BITS, 105 - 4 - PARTITION_INDEX_BITS
};
unsigned int max_block_modes = only_always ? bsd.block_mode_count_1plane_always
: bsd.block_mode_count_1plane_selected;
promise(max_block_modes > 0);
for (unsigned int i = 0; i < max_block_modes; i++)
{
const block_mode& bm = bsd.block_modes[i];
if (bm.quant_mode > max_weight_quant)
{
qwt_errors[i] = 1e38f;
continue;
}
assert(!bm.is_dual_plane);
int bitcount = free_bits_for_partition_count[partition_count - 1] - bm.weight_bits;
if (bitcount <= 0)
{
qwt_errors[i] = 1e38f;
continue;
}
if (weight_high_value[i] > 1.02f * min_wt_cutoff)
{
weight_high_value[i] = 1.0f;
}
int decimation_mode = bm.decimation_mode;
const auto& di = bsd.get_decimation_info(decimation_mode);
qwt_bitcounts[i] = static_cast<int8_t>(bitcount);
ASTCENC_ALIGNAS float dec_weights_uquantf[BLOCK_MAX_WEIGHTS];
// Generate the optimized set of weights for the weight mode
compute_quantized_weights_for_decimation(
di,
weight_low_value[i], weight_high_value[i],
dec_weights_ideal + BLOCK_MAX_WEIGHTS * decimation_mode,
dec_weights_uquantf,
dec_weights_uquant + BLOCK_MAX_WEIGHTS * i,
bm.get_weight_quant_mode());
// Compute weight quantization errors for the block mode
qwt_errors[i] = compute_error_of_weight_set_1plane(
ei,
di,
dec_weights_uquantf);
}
// Decide the optimal combination of color endpoint encodings and weight encodings
uint8_t partition_format_specifiers[TUNE_MAX_TRIAL_CANDIDATES][BLOCK_MAX_PARTITIONS];
int block_mode_index[TUNE_MAX_TRIAL_CANDIDATES];
quant_method color_quant_level[TUNE_MAX_TRIAL_CANDIDATES];
quant_method color_quant_level_mod[TUNE_MAX_TRIAL_CANDIDATES];
unsigned int candidate_count = compute_ideal_endpoint_formats(
pi, blk, ei.ep, qwt_bitcounts, qwt_errors,
config.tune_candidate_limit, 0, max_block_modes,
partition_format_specifiers, block_mode_index,
color_quant_level, color_quant_level_mod, tmpbuf);
// Iterate over the N believed-to-be-best modes to find out which one is actually best
float best_errorval_in_mode = ERROR_CALC_DEFAULT;
float best_errorval_in_scb = scb.errorval;
for (unsigned int i = 0; i < candidate_count; i++)
{
TRACE_NODE(node0, "candidate");
const int bm_packed_index = block_mode_index[i];
assert(bm_packed_index >= 0 && bm_packed_index < static_cast<int>(bsd.block_mode_count_1plane_selected));
const block_mode& qw_bm = bsd.block_modes[bm_packed_index];
int decimation_mode = qw_bm.decimation_mode;
const auto& di = bsd.get_decimation_info(decimation_mode);
promise(di.weight_count > 0);
trace_add_data("weight_x", di.weight_x);
trace_add_data("weight_y", di.weight_y);
trace_add_data("weight_z", di.weight_z);
trace_add_data("weight_quant", qw_bm.quant_mode);
// Recompute the ideal color endpoints before storing them
vfloat4 rgbs_colors[BLOCK_MAX_PARTITIONS];
vfloat4 rgbo_colors[BLOCK_MAX_PARTITIONS];
symbolic_compressed_block workscb;
endpoints workep = ei.ep;
uint8_t* u8_weight_src = dec_weights_uquant + BLOCK_MAX_WEIGHTS * bm_packed_index;
for (unsigned int j = 0; j < di.weight_count; j++)
{
workscb.weights[j] = u8_weight_src[j];
}
for (unsigned int l = 0; l < config.tune_refinement_limit; l++)
{
recompute_ideal_colors_1plane(
blk, pi, di, workscb.weights,
workep, rgbs_colors, rgbo_colors);
// Quantize the chosen color, tracking if worth trying the mod value
bool all_same = color_quant_level[i] != color_quant_level_mod[i];
for (unsigned int j = 0; j < partition_count; j++)
{
workscb.color_formats[j] = pack_color_endpoints(
workep.endpt0[j],
workep.endpt1[j],
rgbs_colors[j],
rgbo_colors[j],
partition_format_specifiers[i][j],
workscb.color_values[j],
color_quant_level[i]);
all_same = all_same && workscb.color_formats[j] == workscb.color_formats[0];
}
// If all the color endpoint modes are the same, we get a few more bits to store colors;
// let's see if we can take advantage of this: requantize all the colors and see if the
// endpoint modes remain the same.
workscb.color_formats_matched = 0;
if (partition_count >= 2 && all_same)
{
uint8_t colorvals[BLOCK_MAX_PARTITIONS][8];
uint8_t color_formats_mod[BLOCK_MAX_PARTITIONS] { 0 };
bool all_same_mod = true;
for (unsigned int j = 0; j < partition_count; j++)
{
color_formats_mod[j] = pack_color_endpoints(
workep.endpt0[j],
workep.endpt1[j],
rgbs_colors[j],
rgbo_colors[j],
partition_format_specifiers[i][j],
colorvals[j],
color_quant_level_mod[i]);
// Early out as soon as it's no longer possible to use mod
if (color_formats_mod[j] != color_formats_mod[0])
{
all_same_mod = false;
break;
}
}
if (all_same_mod)
{
workscb.color_formats_matched = 1;
for (unsigned int j = 0; j < BLOCK_MAX_PARTITIONS; j++)
{
for (unsigned int k = 0; k < 8; k++)
{
workscb.color_values[j][k] = colorvals[j][k];
}
workscb.color_formats[j] = color_formats_mod[j];
}
}
}
// Store header fields
workscb.partition_count = static_cast<uint8_t>(partition_count);
workscb.partition_index = static_cast<uint16_t>(partition_index);
workscb.plane2_component = -1;
workscb.quant_mode = workscb.color_formats_matched ? color_quant_level_mod[i] : color_quant_level[i];
workscb.block_mode = qw_bm.mode_index;
workscb.block_type = SYM_BTYPE_NONCONST;
// Pre-realign test
if (l == 0)
{
float errorval = compute_difference(config, bsd, workscb, blk);
if (errorval == -ERROR_CALC_DEFAULT)
{
errorval = -errorval;
workscb.block_type = SYM_BTYPE_ERROR;
}
trace_add_data("error_prerealign", errorval);
best_errorval_in_mode = astc::min(errorval, best_errorval_in_mode);
// Average refinement improvement is 3.5% per iteration (allow 4.5%), but the first
// iteration can help more so we give it a extra 8% leeway. Use this knowledge to
// drive a heuristic to skip blocks that are unlikely to catch up with the best
// block we have already.
unsigned int iters_remaining = config.tune_refinement_limit - l;
float threshold = (0.045f * static_cast<float>(iters_remaining)) + 1.08f;
if (errorval > (threshold * best_errorval_in_scb))
{
break;
}
if (errorval < best_errorval_in_scb)
{
best_errorval_in_scb = errorval;
workscb.errorval = errorval;
scb = workscb;
if (errorval < tune_errorval_threshold)
{
// Skip remaining candidates - this is "good enough"
i = candidate_count;
break;
}
}
}
bool adjustments;
if (di.weight_count != bsd.texel_count)
{
adjustments = realign_weights_decimated(
config.profile, bsd, blk, workscb);
}
else
{
adjustments = realign_weights_undecimated(
config.profile, bsd, blk, workscb);
}
// Post-realign test
float errorval = compute_difference(config, bsd, workscb, blk);
if (errorval == -ERROR_CALC_DEFAULT)
{
errorval = -errorval;
workscb.block_type = SYM_BTYPE_ERROR;
}
trace_add_data("error_postrealign", errorval);
best_errorval_in_mode = astc::min(errorval, best_errorval_in_mode);
// Average refinement improvement is 3.5% per iteration, so skip blocks that are
// unlikely to catch up with the best block we have already. Assume a 4.5% per step to
// give benefit of the doubt ...
unsigned int iters_remaining = config.tune_refinement_limit - 1 - l;
float threshold = (0.045f * static_cast<float>(iters_remaining)) + 1.0f;
if (errorval > (threshold * best_errorval_in_scb))
{
break;
}
if (errorval < best_errorval_in_scb)
{
best_errorval_in_scb = errorval;
workscb.errorval = errorval;
scb = workscb;
if (errorval < tune_errorval_threshold)
{
// Skip remaining candidates - this is "good enough"
i = candidate_count;
break;
}
}
if (!adjustments)
{
break;
}
}
}
return best_errorval_in_mode;
}
/**
* @brief Compress a block using a chosen partitioning and 2 planes of weights.
*
* @param config The compressor configuration.
* @param bsd The block size information.
* @param blk The image block color data to compress.
* @param tune_errorval_threshold The error value threshold.
* @param plane2_component The component index for the second plane of weights.
* @param[out] scb The symbolic compressed block output.
* @param[out] tmpbuf The quantized weights for plane 1.
*/
static float compress_symbolic_block_for_partition_2planes(
const astcenc_config& config,
const block_size_descriptor& bsd,
const image_block& blk,
float tune_errorval_threshold,
unsigned int plane2_component,
symbolic_compressed_block& scb,
compression_working_buffers& tmpbuf,
int quant_limit
) {
promise(config.tune_candidate_limit > 0);
promise(config.tune_refinement_limit > 0);
promise(bsd.decimation_mode_count_selected > 0);
int max_weight_quant = astc::min(static_cast<int>(QUANT_32), quant_limit);
// Compute ideal weights and endpoint colors, with no quantization or decimation
endpoints_and_weights& ei1 = tmpbuf.ei1;
endpoints_and_weights& ei2 = tmpbuf.ei2;
compute_ideal_colors_and_weights_2planes(bsd, blk, plane2_component, ei1, ei2);
// Compute ideal weights and endpoint colors for every decimation
float* dec_weights_ideal = tmpbuf.dec_weights_ideal;
uint8_t* dec_weights_uquant = tmpbuf.dec_weights_uquant;
// For each decimation mode, compute an ideal set of weights with no quantization
for (unsigned int i = 0; i < bsd.decimation_mode_count_selected; i++)
{
const auto& dm = bsd.get_decimation_mode(i);
if (!dm.is_ref_2plane(static_cast<quant_method>(max_weight_quant)))
{
continue;
}
const auto& di = bsd.get_decimation_info(i);
compute_ideal_weights_for_decimation(
ei1,
di,
dec_weights_ideal + i * BLOCK_MAX_WEIGHTS);
compute_ideal_weights_for_decimation(
ei2,
di,
dec_weights_ideal + i * BLOCK_MAX_WEIGHTS + WEIGHTS_PLANE2_OFFSET);
}
// Compute maximum colors for the endpoints and ideal weights, then for each endpoint and ideal
// weight pair, compute the smallest weight that will result in a color value greater than 1
vfloat4 min_ep1(10.0f);
vfloat4 min_ep2(10.0f);
vfloat4 ep1 = (vfloat4(1.0f) - ei1.ep.endpt0[0]) / (ei1.ep.endpt1[0] - ei1.ep.endpt0[0]);
vmask4 use_ep1 = (ep1 > vfloat4(0.5f)) & (ep1 < min_ep1);
min_ep1 = select(min_ep1, ep1, use_ep1);
vfloat4 ep2 = (vfloat4(1.0f) - ei2.ep.endpt0[0]) / (ei2.ep.endpt1[0] - ei2.ep.endpt0[0]);
vmask4 use_ep2 = (ep2 > vfloat4(0.5f)) & (ep2 < min_ep2);
min_ep2 = select(min_ep2, ep2, use_ep2);
vfloat4 err_max(ERROR_CALC_DEFAULT);
vmask4 err_mask = vint4::lane_id() == vint4(plane2_component);
// Set the plane2 component to max error in ep1
min_ep1 = select(min_ep1, err_max, err_mask);
float min_wt_cutoff1 = hmin_s(min_ep1);
// Set the minwt2 to the plane2 component min in ep2
float min_wt_cutoff2 = hmin_s(select(err_max, min_ep2, err_mask));
compute_angular_endpoints_2planes(
bsd, dec_weights_ideal, max_weight_quant, tmpbuf);
// For each mode (which specifies a decimation and a quantization):
// * Compute number of bits needed for the quantized weights
// * Generate an optimized set of quantized weights
// * Compute quantization errors for the mode
float* weight_low_value1 = tmpbuf.weight_low_value1;
float* weight_high_value1 = tmpbuf.weight_high_value1;
float* weight_low_value2 = tmpbuf.weight_low_value2;
float* weight_high_value2 = tmpbuf.weight_high_value2;
int8_t* qwt_bitcounts = tmpbuf.qwt_bitcounts;
float* qwt_errors = tmpbuf.qwt_errors;
unsigned int start_2plane = bsd.block_mode_count_1plane_selected;
unsigned int end_2plane = bsd.block_mode_count_1plane_2plane_selected;
for (unsigned int i = start_2plane; i < end_2plane; i++)
{
const block_mode& bm = bsd.block_modes[i];
assert(bm.is_dual_plane);
if (bm.quant_mode > max_weight_quant)
{
qwt_errors[i] = 1e38f;
continue;
}
qwt_bitcounts[i] = static_cast<int8_t>(109 - bm.weight_bits);
if (weight_high_value1[i] > 1.02f * min_wt_cutoff1)
{
weight_high_value1[i] = 1.0f;
}
if (weight_high_value2[i] > 1.02f * min_wt_cutoff2)
{
weight_high_value2[i] = 1.0f;
}
unsigned int decimation_mode = bm.decimation_mode;
const auto& di = bsd.get_decimation_info(decimation_mode);
ASTCENC_ALIGNAS float dec_weights_uquantf[BLOCK_MAX_WEIGHTS];
// Generate the optimized set of weights for the mode
compute_quantized_weights_for_decimation(
di,
weight_low_value1[i],
weight_high_value1[i],
dec_weights_ideal + BLOCK_MAX_WEIGHTS * decimation_mode,
dec_weights_uquantf,
dec_weights_uquant + BLOCK_MAX_WEIGHTS * i,
bm.get_weight_quant_mode());
compute_quantized_weights_for_decimation(
di,
weight_low_value2[i],
weight_high_value2[i],
dec_weights_ideal + BLOCK_MAX_WEIGHTS * decimation_mode + WEIGHTS_PLANE2_OFFSET,
dec_weights_uquantf + WEIGHTS_PLANE2_OFFSET,
dec_weights_uquant + BLOCK_MAX_WEIGHTS * i + WEIGHTS_PLANE2_OFFSET,
bm.get_weight_quant_mode());
// Compute weight quantization errors for the block mode
qwt_errors[i] = compute_error_of_weight_set_2planes(
ei1,
ei2,
di,
dec_weights_uquantf,
dec_weights_uquantf + WEIGHTS_PLANE2_OFFSET);
}
// Decide the optimal combination of color endpoint encodings and weight encodings
uint8_t partition_format_specifiers[TUNE_MAX_TRIAL_CANDIDATES][BLOCK_MAX_PARTITIONS];
int block_mode_index[TUNE_MAX_TRIAL_CANDIDATES];
quant_method color_quant_level[TUNE_MAX_TRIAL_CANDIDATES];
quant_method color_quant_level_mod[TUNE_MAX_TRIAL_CANDIDATES];
endpoints epm;
merge_endpoints(ei1.ep, ei2.ep, plane2_component, epm);
const auto& pi = bsd.get_partition_info(1, 0);
unsigned int candidate_count = compute_ideal_endpoint_formats(
pi, blk, epm, qwt_bitcounts, qwt_errors,
config.tune_candidate_limit,
bsd.block_mode_count_1plane_selected, bsd.block_mode_count_1plane_2plane_selected,
partition_format_specifiers, block_mode_index,
color_quant_level, color_quant_level_mod, tmpbuf);
// Iterate over the N believed-to-be-best modes to find out which one is actually best
float best_errorval_in_mode = ERROR_CALC_DEFAULT;
float best_errorval_in_scb = scb.errorval;
for (unsigned int i = 0; i < candidate_count; i++)
{
TRACE_NODE(node0, "candidate");
const int bm_packed_index = block_mode_index[i];
assert(bm_packed_index >= static_cast<int>(bsd.block_mode_count_1plane_selected) &&
bm_packed_index < static_cast<int>(bsd.block_mode_count_1plane_2plane_selected));
const block_mode& qw_bm = bsd.block_modes[bm_packed_index];
int decimation_mode = qw_bm.decimation_mode;
const auto& di = bsd.get_decimation_info(decimation_mode);
promise(di.weight_count > 0);
trace_add_data("weight_x", di.weight_x);
trace_add_data("weight_y", di.weight_y);
trace_add_data("weight_z", di.weight_z);
trace_add_data("weight_quant", qw_bm.quant_mode);
vfloat4 rgbs_color;
vfloat4 rgbo_color;
symbolic_compressed_block workscb;
endpoints workep = epm;
uint8_t* u8_weight1_src = dec_weights_uquant + BLOCK_MAX_WEIGHTS * bm_packed_index;
uint8_t* u8_weight2_src = dec_weights_uquant + BLOCK_MAX_WEIGHTS * bm_packed_index + WEIGHTS_PLANE2_OFFSET;
for (int j = 0; j < di.weight_count; j++)
{
workscb.weights[j] = u8_weight1_src[j];
workscb.weights[j + WEIGHTS_PLANE2_OFFSET] = u8_weight2_src[j];
}
for (unsigned int l = 0; l < config.tune_refinement_limit; l++)
{
recompute_ideal_colors_2planes(
blk, bsd, di,
workscb.weights, workscb.weights + WEIGHTS_PLANE2_OFFSET,
workep, rgbs_color, rgbo_color, plane2_component);
// Quantize the chosen color
workscb.color_formats[0] = pack_color_endpoints(
workep.endpt0[0],
workep.endpt1[0],
rgbs_color, rgbo_color,
partition_format_specifiers[i][0],
workscb.color_values[0],
color_quant_level[i]);
// Store header fields
workscb.partition_count = 1;
workscb.partition_index = 0;
workscb.quant_mode = color_quant_level[i];
workscb.color_formats_matched = 0;
workscb.block_mode = qw_bm.mode_index;
workscb.plane2_component = static_cast<int8_t>(plane2_component);
workscb.block_type = SYM_BTYPE_NONCONST;
// Pre-realign test
if (l == 0)
{
float errorval = compute_symbolic_block_difference_2plane(config, bsd, workscb, blk);
if (errorval == -ERROR_CALC_DEFAULT)
{
errorval = -errorval;
workscb.block_type = SYM_BTYPE_ERROR;
}
trace_add_data("error_prerealign", errorval);
best_errorval_in_mode = astc::min(errorval, best_errorval_in_mode);
// Average refinement improvement is 3.5% per iteration (allow 4.5%), but the first
// iteration can help more so we give it a extra 8% leeway. Use this knowledge to
// drive a heuristic to skip blocks that are unlikely to catch up with the best
// block we have already.
unsigned int iters_remaining = config.tune_refinement_limit - l;
float threshold = (0.045f * static_cast<float>(iters_remaining)) + 1.08f;
if (errorval > (threshold * best_errorval_in_scb))
{
break;
}
if (errorval < best_errorval_in_scb)
{
best_errorval_in_scb = errorval;
workscb.errorval = errorval;
scb = workscb;
if (errorval < tune_errorval_threshold)
{
// Skip remaining candidates - this is "good enough"
i = candidate_count;
break;
}
}
}
// Perform a final pass over the weights to try to improve them.
bool adjustments;
if (di.weight_count != bsd.texel_count)
{
adjustments = realign_weights_decimated(
config.profile, bsd, blk, workscb);
}
else
{
adjustments = realign_weights_undecimated(
config.profile, bsd, blk, workscb);
}
// Post-realign test
float errorval = compute_symbolic_block_difference_2plane(config, bsd, workscb, blk);
if (errorval == -ERROR_CALC_DEFAULT)
{
errorval = -errorval;
workscb.block_type = SYM_BTYPE_ERROR;