-
Notifications
You must be signed in to change notification settings - Fork 17
/
compress.c
1789 lines (1724 loc) · 66.4 KB
/
compress.c
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
/*
Copyright (c) 2015 Harm Hanemaaijer <fgenfb@yahoo.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
#include <malloc.h>
#include <fgen.h>
#include "texgenpack.h"
#include "decode.h"
#include "packing.h"
enum {
// The RGB components of pixels in the block only have one color.
BLOCK_FLAG_ONE_COLOR = 0x1,
// The RGB components of pixels in the block only have two colors.
BLOCK_FLAG_TWO_COLORS = 0x2,
// The RGB components of pixels in the block only have three colors.
BLOCK_FLAG_THREE_COLORS = 0x4,
// The RGB components of pixels in the block only have four colors.
BLOCK_FLAG_FOUR_COLORS = 0x8,
// All alpha values of the block are 0xFF.
BLOCK_FLAG_OPAQUE = 0x10,
// None of the alpha values of the block are 0xFF.
BLOCK_FLAG_NON_OPAQUE = 0x20,
// The alpha values are either 0x00 or 0xFF
BLOCK_FLAG_PUNCHTHROUGH = 0x40,
};
static void compress_with_single_population(Image *image, Texture *Texture);
static void compress_with_archipelago(Image *image, Texture *texture);
static void compress_multiple_blocks_concurrently(Image *image, Texture *texture);
static void compress_multiple_blocks_concurrently_second_pass(Image *image, Texture *texture);
static void set_alpha_pixels(Image *image, int x, int y, int w, int h, unsigned char *alpha_pixels);
static int get_block_flags_rgba8(Image *image, int x, int y, int w, int h, unsigned char *alpha_pixels,
unsigned int *colors);
static void optimize_alpha(Image *image, Texture *texture);
static double get_rmse_threshold(Texture *texture, int speed, Image *image);
static int nu_generations, nu_generations_second_pass;
static int population_size;
static int nu_islands, nu_islands_second_pass;
static float mutation_probability;
static float mutation_probability_second_pass;
static float crossover_probability;
static CompressCallbackFunction compress_callback_func;
static int mode_statistics[16];
static double rmse_threshold;
// Compress an image into a texture.
void compress_image(Image *image, int texture_type, CompressCallbackFunction callback_func, Texture *texture,
int genetic_parameters, float mutation_prob, float crossover_prob) {
texture->info = match_texture_type(texture_type);
if (texture_type & TEXTURE_TYPE_UNCOMPRESSED_BIT) {
copy_image_to_uncompressed_texture(image, texture_type, texture);
return;
}
if (texture_type & TEXTURE_TYPE_ASTC_BIT) {
compress_image_to_astc_texture(image, texture_type, texture);
return;
}
if ((texture_type & TEXTURE_TYPE_HALF_FLOAT_BIT) && !image->is_half_float) {
printf("Error -- image is not in half float format.\n");
exit(1);
}
texture->width = image->width;
texture->height = image->height;
texture->bits_per_block = texture->info->internal_bits_per_block;
texture->type = texture_type;
texture->block_width = texture->info->block_width;
texture->block_height = texture->info->block_height;
texture->extended_width = ((texture->width + texture->block_width - 1) / texture->block_width) *
texture->block_width;
texture->extended_height = ((texture->height + texture->block_height - 1) / texture->block_height)
* texture->block_height;
texture->pixels = (unsigned int *)malloc((texture->extended_height / texture->block_height) *
(texture->extended_width / texture->block_width) * (texture->bits_per_block / 8));
set_texture_decoding_function(texture, image);
if ((texture_type & TEXTURE_TYPE_HALF_FLOAT_BIT) || image->is_half_float)
calculate_half_float_table();
if ((texture_type == TEXTURE_TYPE_BPTC_FLOAT || texture_type == TEXTURE_TYPE_BPTC_SIGNED_FLOAT) && option_hdr)
calculate_gamma_corrected_half_float_table();
if (image->is_half_float)
calculate_normalized_float_table();
rmse_threshold = get_rmse_threshold(texture, option_compression_level, image);
if (option_verbose) {
memset(mode_statistics, 0, sizeof(int) * 16);
}
compress_callback_func = callback_func;
if (genetic_parameters) {
mutation_probability = mutation_prob;
crossover_probability = crossover_prob;
}
else {
// Emperically determined mutation probability.
#if 0
if (texture_type & TEXTURE_TYPE_128BIT_BIT)
if (texture_type == TEXTURE_TYPE_BPTC_FLOAT)
// bptc_float format needs higher mutation probability.
if (option_speed == SPEED_ULTRA)
mutation_probability = 0.016;
else
if (option_speed == SPEED_FAST)
mutation_probability = 0.014;
else
if (option_speed == SPEED_MEDIUM)
mutation_probability = 0.018;
else // SPEED_SLOW
mutation_probability = 0.018;
else
if (texture_type == TEXTURE_TYPE_BPTC)
// bptc
if (option_speed == SPEED_ULTRA)
mutation_probability = 0.010;
else
if (option_speed == SPEED_FAST)
mutation_probability = 0.011;
else
if (option_speed == SPEED_MEDIUM)
mutation_probability = 0.012;
else // SPEED_SLOW
mutation_probability = 0.012;
else
if (texture_type == TEXTURE_TYPE_DXT5)
// dxt5
if (option_speed == SPEED_ULTRA)
mutation_probability = 0.020;
else
if (option_speed == SPEED_FAST)
mutation_probability = 0.017;
else
if (option_speed == SPEED_MEDIUM)
mutation_probability = 0.018;
else // SPEED_SLOW
mutation_probability = 0.018;
else
// Other 128-bit block texture types.
if (option_speed == SPEED_ULTRA)
mutation_probability = 0.015;
else
if (option_speed == SPEED_FAST)
mutation_probability = 0.013;
else
if (option_speed == SPEED_MEDIUM)
mutation_probability = 0.015;
else // SPEED_SLOW
mutation_probability = 0.015;
else // 64-bit texture formats.
if (texture_type == TEXTURE_TYPE_ETC1)
if (option_speed == SPEED_ULTRA)
mutation_probability = 0.027;
else
if (option_speed == SPEED_FAST)
mutation_probability = 0.023;
else
if (option_speed == SPEED_MEDIUM)
mutation_probability = 0.024;
else // SPEED_SLOW
mutation_probability = 0.025;
else
if (texture_type == TEXTURE_TYPE_ETC2_RGB8)
if (option_speed == SPEED_ULTRA)
mutation_probability = 0.023;
else
if (option_speed == SPEED_FAST)
mutation_probability = 0.022;
else
if (option_speed == SPEED_MEDIUM)
mutation_probability = 0.024;
else // SPEED_SLOW
mutation_probability = 0.025;
else
if (texture_type == TEXTURE_TYPE_DXT1)
if (option_speed == SPEED_ULTRA)
mutation_probability = 0.028;
else
if (option_speed == SPEED_FAST)
mutation_probability = 0.025;
else
if (option_speed == SPEED_MEDIUM)
mutation_probability = 0.026;
else // SPEED_SLOW
mutation_probability = 0.026;
else
if (texture_type == TEXTURE_TYPE_R11_EAC)
if (option_speed == SPEED_ULTRA)
mutation_probability = 0.029;
else
if (option_speed == SPEED_FAST)
mutation_probability = 0.028;
else
if (option_speed == SPEED_MEDIUM)
mutation_probability = 0.026;
else
mutation_probability = 0.027;
else // Other 64-bit texture formats.
if (option_speed == SPEED_ULTRA)
mutation_probability = 0.025;
else
if (option_speed == SPEED_FAST)
mutation_probability = 0.024;
else
if (option_speed == SPEED_MEDIUM)
mutation_probability = 0.025;
else
mutation_probability = 0.026;
#else
if (texture_type & TEXTURE_TYPE_128BIT_BIT)
if (texture_type == TEXTURE_TYPE_BPTC_FLOAT)
// bptc_float format needs higher mutation probability.
mutation_probability = 0.014;
else if (texture_type == TEXTURE_TYPE_BPTC)
// bptc
mutation_probability = 0.012;
else if (texture_type == TEXTURE_TYPE_DXT5)
mutation_probability = 0.017;
else
mutation_probability = 0.013;
else // 64-bit texture formats.
if (texture_type == TEXTURE_TYPE_ETC1)
mutation_probability = 0.023;
else if (texture_type == TEXTURE_TYPE_ETC2_RGB8)
mutation_probability = 0.022;
else if (texture_type == TEXTURE_TYPE_DXT1)
mutation_probability = 0.025;
else if (texture_type == TEXTURE_TYPE_R11_EAC)
mutation_probability = 0.028;
else // Other 64-bit texture formats.
mutation_probability = 0.024;
#endif
crossover_probability = 0.7;
}
mutation_probability_second_pass = mutation_probability * 0.66667f;
if (option_compression_level < COMPRESSION_LEVEL_CLASS_1) {
// Compression level class 0.
population_size = 256;
nu_generations = 100 + option_compression_level * 25;
nu_islands = 8;
nu_generations_second_pass = 4000;
nu_islands_second_pass = 8;
}
else if (option_compression_level < COMPRESSION_LEVEL_CLASS_2 - 1) {
// Compression level class 1.
population_size = 128;
nu_generations = 50;
nu_islands = option_compression_level;
nu_generations_second_pass = 4000;
nu_islands_second_pass = 4;
}
else {
// Compression level class 2.
population_size = 128;
nu_generations = 50 + (option_compression_level - 32) * 10;
nu_islands = 32;
nu_generations_second_pass = 8000;
nu_islands_second_pass = 4;
}
if (option_generations != - 1)
nu_generations = option_generations;
if (option_islands != - 1)
nu_islands = option_islands;
if (option_generations_second_pass != - 1)
nu_generations_second_pass = option_generations_second_pass;
if (option_islands_second_pass != - 1)
nu_islands = option_islands_second_pass;
#if 0
if (option_max_threads != -1) {
if (option_compression_level >= COMPRESSION_LEVEL_CLASS_1) {
// When the maximum number of threads is constrained, modify the GA parameters
// for roughly similar quality by doubling the number of generations.
while (nu_islands > option_max_threads) {
nu_islands /= 2;
nu_generations *= 2;
}
while (nu_islands_second_pass > option_max_threads) {
nu_islands_second_pass /= 2;
nu_generations_second_pass *= 2;
}
}
}
#endif
if (option_perceptive && option_compression_level >= COMPRESSION_LEVEL_CLASS_1
&& texture->perceptive_comparison_function == NULL && !option_quiet)
printf("Warning: Perceptive quality strategy not available for texture format.\n");
if (option_compression_level < COMPRESSION_LEVEL_CLASS_1)
compress_multiple_blocks_concurrently(image, texture);
else
compress_with_archipelago(image, texture);
// Perform second pass (non-perceptive quality mode, compression level class 0 (ultra)).
if (option_compression_level < COMPRESSION_LEVEL_CLASS_1) {
if (option_verbose) {
// Decompress the compressed texture and calculate the difference with the original
// and report it.
Image compressed_image;
convert_texture_to_image(texture, &compressed_image);
compare_images(image, &compressed_image);
destroy_image(&compressed_image);
}
compress_multiple_blocks_concurrently_second_pass(image, texture);
}
// Optionally post-process the texture to optimize the alpha values.
// optimize_alpha(image, texture);
if (option_verbose) {
int nu_modes = 0;
if (texture->type == TEXTURE_TYPE_ETC1)
nu_modes = 2;
else if (texture->type == TEXTURE_TYPE_ETC2_RGB8 || texture->type == TEXTURE_TYPE_ETC2_EAC ||
texture->type == TEXTURE_TYPE_ETC2_PUNCHTHROUGH)
nu_modes = 5;
else if (texture->type == TEXTURE_TYPE_BPTC)
nu_modes = 8;
else if (texture->type == TEXTURE_TYPE_BPTC_FLOAT || texture->type == TEXTURE_TYPE_BPTC_SIGNED_FLOAT)
nu_modes = 14;
if (nu_modes > 0) {
printf("Mode statistics:\n");
for (int i = 0; i < nu_modes; i++)
printf("Mode %d: %d blocks\n", i, mode_statistics[i]);
}
}
}
// The fitness function of the genetic algorithm.
static double calculate_fitness(const FgenPopulation *pop, const unsigned char *bitstring) {
unsigned int image_buffer[32]; // 16 required for regular pixels, 32 for 64-bit pixel formats like half floats.
BlockUserData *user_data = (BlockUserData *)pop->user_data;
int flags = user_data->flags;
int r = user_data->texture->decoding_function(bitstring, image_buffer, flags);
if (r == 0) {
// printf("Invalid block.\n");
return 0; // Fitness is zero for invalid blocks.
}
if (option_perceptive && option_compression_level >= COMPRESSION_LEVEL_CLASS_1 &&
user_data->texture->perceptive_comparison_function != NULL)
return user_data->texture->perceptive_comparison_function(image_buffer, user_data);
else
return user_data->texture->comparison_function(image_buffer, user_data);
}
// The generation callback function of the genetic algorithm.
static void generation_callback(FgenPopulation *pop, int generation) {
if (generation == 0)
return;
if (generation >= nu_generations * 2) {
fgen_signal_stop(pop);
return;
}
// Adaptive, if the fitness is above the threshold, stop, otherwise go on for another nu_generations
// generations.
FgenIndividual *best = fgen_best_individual_of_population(pop);
double rmse = sqrt((1.0 / best->fitness) / 16);
if (generation <= nu_generations && rmse < rmse_threshold)
fgen_signal_stop(pop);
}
static void generation_callback_non_adaptive(FgenPopulation *pop, int generation) {
if (generation == 0)
return;
if (generation >= nu_generations) {
fgen_signal_stop(pop);
return;
}
}
static void generation_callback_archipelago(FgenPopulation *pop, int generation) {
if (generation == 0)
return;
if (generation >= nu_generations * 3) {
fgen_signal_stop(pop);
return;
}
// Adaptive, if the fitness is above the threshold, stop, otherwise go on for another nu_generations
// generations.
FgenIndividual *best = fgen_best_individual_of_population(pop);
double rmse = sqrt((1.0 / best->fitness) / 16);
if (generation <= nu_generations) {
if (rmse < rmse_threshold)
fgen_signal_stop(pop);
}
else if (generation <= nu_generations * 2) {
if (rmse < rmse_threshold * 1.5)
fgen_signal_stop(pop);
}
}
// Generation call-back for second-pass.
static void generation_callback_second_pass(FgenPopulation *pop, int generation) {
if (generation == 0)
return;
if (generation >= nu_generations_second_pass) {
fgen_signal_stop(pop);
return;
}
}
// Custom seeding function for 128-bit formats for archipelagos where each island is compressing the same block.
static void seed_128bit(FgenPopulation *pop, unsigned char *bitstring) {
BlockUserData *user_data = (BlockUserData *)pop->user_data;
FgenRNG *rng = fgen_get_rng(pop);
int r = fgen_random_8(rng);
// The chance of seeding with an already calculated neighbour block should be chosen carefully.
// A too high probability results in less diversity in the archipelago.
int factor;
if (population_size == 128)
factor = 1;
else // population_size == 64?
factor = 2;
if (r < 2 * factor && user_data->x_offset > 0) {
// Seed with solution to the left with chance 1/128th (1/64th if population size is 64).
int compressed_block_index = (user_data->y_offset / user_data->texture->block_height) *
(user_data->texture->extended_width / user_data->texture->block_width) +
(user_data->x_offset / user_data->texture->block_width) - 1;
*(unsigned int *)&bitstring[0] = user_data->texture->pixels[compressed_block_index * 4];
*(unsigned int *)&bitstring[4] = user_data->texture->pixels[compressed_block_index * 4 + 1];
*(unsigned int *)&bitstring[8] = user_data->texture->pixels[compressed_block_index * 4 + 2];
*(unsigned int *)&bitstring[12] = user_data->texture->pixels[compressed_block_index * 4 + 3];
goto end;
}
if (r < 4 * factor && user_data->y_offset > 0) {
// Seed with solution above with chance 1/128th (1/64th for x == 0).
// 1/64th if population size is 64.
int compressed_block_index = (user_data->y_offset / user_data->texture->block_height - 1) *
(user_data->texture->extended_width / user_data->texture->block_width) +
user_data->x_offset / user_data->texture->block_width;
*(unsigned int *)&bitstring[0] = user_data->texture->pixels[compressed_block_index * 4];
*(unsigned int *)&bitstring[4] = user_data->texture->pixels[compressed_block_index * 4 + 1];
*(unsigned int *)&bitstring[8] = user_data->texture->pixels[compressed_block_index * 4 + 2];
*(unsigned int *)&bitstring[12] = user_data->texture->pixels[compressed_block_index * 4 + 3];
goto end;
}
if (r < 6 * factor && (user_data->x_offset > 0 || user_data->y_offset > 0)) {
// Seed with a random already calculated solution with chance 1/128th.
// 1/64th if population size is 64.
int x, y;
if (user_data->y_offset == 0) {
x = fgen_random_n(rng, user_data->x_offset / user_data->texture->block_width);
y = 0;
}
else {
int i = fgen_random_n(rng, (user_data->y_offset / user_data->texture->block_height) *
(user_data->texture->extended_width / user_data->texture->block_width)
+ user_data->x_offset / user_data->texture->block_width);
x = i % (user_data->texture->extended_width / user_data->texture->block_width);
y = i / (user_data->texture->extended_width / user_data->texture->block_width);
}
int compressed_block_index = y * (user_data->texture->extended_width / user_data->texture->block_width) + x;
*(unsigned int *)&bitstring[0] = user_data->texture->pixels[compressed_block_index * 4];
*(unsigned int *)&bitstring[4] = user_data->texture->pixels[compressed_block_index * 4 + 1];
*(unsigned int *)&bitstring[8] = user_data->texture->pixels[compressed_block_index * 4 + 2];
*(unsigned int *)&bitstring[12] = user_data->texture->pixels[compressed_block_index * 4 + 3];
goto end;
}
int nu_tries = 0;
again :
fgen_seed_random(pop, bitstring);
if (user_data->texture->set_mode_function != NULL)
user_data->texture->set_mode_function(bitstring, user_data->flags);
unsigned int image_buffer[32];
bool valid;
valid = user_data->texture->decoding_function(bitstring, image_buffer, user_data->flags);
nu_tries++;
if (nu_tries == 100)
printf("Giving up seeding with constraints after 100 tries.\n");
else if (!valid)
goto again;
// printf("Seed: %d tries.\n", nu_tries);
end :
if (user_data->texture->type == TEXTURE_TYPE_BPTC)
bptc_set_block_colors(bitstring, user_data->flags, user_data->colors);
if (user_data->texture->type == TEXTURE_TYPE_DXT3)
optimize_block_alpha_dxt3(bitstring, user_data->alpha_pixels);
else if (user_data->texture->type == TEXTURE_TYPE_ETC2_EAC) {
optimize_block_alpha_etc2_eac(bitstring, user_data->alpha_pixels, user_data->flags);
#if 0
if (user_data->flags & MODES_ALLOWED_PUNCHTHROUGH_ONLY)
printf("Seed: Block is punchthrough-only.\n");
unsigned int image_buffer[16];
int r = draw_block4x4_etc2_eac(bitstring, image_buffer, ETC2_MODE_ALLOWED_ALL);
if (!r)
printf("Seed: Invalid block.\n");
printf("Seed: Target alpha values: ");
for (int i = 0; i < 16; i++) {
int a = user_data->alpha_pixels[i];
printf("0x%02X ", a);
}
printf("\n");
printf("Seed: Compressed alpha values (0x%08X): ", *(unsigned int *)bitstring);
for (int i = 0; i < 16; i++) {
int a = pixel_get_a(image_buffer[i]);
printf("0x%02X ", a);
}
printf("\n");
#endif
}
}
// Seeding function for archipelagos where each island is compressing the same block.
static void seed(FgenPopulation *pop, unsigned char *bitstring) {
BlockUserData *user_data = (BlockUserData *)pop->user_data;
if (user_data->texture->bits_per_block == 128) {
seed_128bit(pop, bitstring);
return;
}
FgenRNG *rng = fgen_get_rng(pop);
int r = fgen_random_8(rng);
// The chance of seeding with an already calculated neighbour block should be chosen carefully.
// A too high probability results in less diversity in the archipelago.
int factor;
if (population_size == 128)
factor = 1;
else // population_size == 64
factor = 2;
if (r < 2 * factor && user_data->x_offset > 0) {
// Seed with solution to the left with chance 1/128th.
int compressed_block_index = (user_data->y_offset / user_data->texture->block_height) *
(user_data->texture->extended_width / user_data->texture->block_width) +
(user_data->x_offset / user_data->texture->block_width) - 1;
*(unsigned int *)&bitstring[0] = user_data->texture->pixels[compressed_block_index * 2];
*(unsigned int *)&bitstring[4] = user_data->texture->pixels[compressed_block_index * 2 + 1];
goto end;
}
if (r < 4 * factor && user_data->y_offset > 0) {
// Seed with solution above with chance 1/128th (1/64th for x == 0).
int compressed_block_index = (user_data->y_offset / user_data->texture->block_height - 1) *
(user_data->texture->extended_width / user_data->texture->block_width) +
user_data->x_offset / user_data->texture->block_width;
*(unsigned int *)&bitstring[0] = user_data->texture->pixels[compressed_block_index * 2];
*(unsigned int *)&bitstring[4] = user_data->texture->pixels[compressed_block_index * 2 + 1];
goto end;
}
if (r < 6 * factor && (user_data->x_offset > 0 || user_data->y_offset > 0)) {
// Seed with a random already calculated solution with chance 1/128th.
int x, y;
if (user_data->y_offset == 0) {
x = fgen_random_n(rng, user_data->x_offset / user_data->texture->block_width);
y = 0;
}
else {
int i = fgen_random_n(rng, (user_data->y_offset / user_data->texture->block_height) *
(user_data->texture->extended_width / user_data->texture->block_width)
+ user_data->x_offset / user_data->texture->block_width);
x = i % (user_data->texture->extended_width / user_data->texture->block_width);
y = i / (user_data->texture->extended_width / user_data->texture->block_width);
}
int compressed_block_index = y * (user_data->texture->extended_width / user_data->texture->block_width) + x;
*(unsigned int *)&bitstring[0] = user_data->texture->pixels[compressed_block_index * 2];
*(unsigned int *)&bitstring[4] = user_data->texture->pixels[compressed_block_index * 2 + 1];
goto end;
}
int nu_tries = 0;
again :
fgen_seed_random(pop, bitstring);
if (user_data->texture->set_mode_function != NULL) {
user_data->texture->set_mode_function(bitstring, user_data->flags);
}
unsigned int image_buffer[32];
bool valid;
valid = user_data->texture->decoding_function(bitstring, image_buffer, user_data->flags);
nu_tries++;
if (nu_tries == 100)
printf("Giving up seeding with constraints after 100 tries.\n");
else if (!valid)
goto again;
// printf("Seed: %d tries (flags = 0x%08X).\n", nu_tries, user_data->flags);
end :
if (user_data->texture->type == TEXTURE_TYPE_ETC2_PUNCHTHROUGH)
optimize_block_alpha_etc2_punchthrough(bitstring, user_data->alpha_pixels);
}
// 128-bit seeding function for archipelagos where each island is compressing a different block (--ultra setting).
// Population size is assumed to be 256.
static void seed2_128bit(FgenPopulation *pop, unsigned char *bitstring) {
BlockUserData *user_data = (BlockUserData *)pop->user_data;
FgenRNG *rng = fgen_get_rng(pop);
int r = fgen_random_8(rng);
// The chance of seeding with an already calculated neighbour block should be chosen carefully.
// A too high probability results in less diversity in the archipelago.
if (r < 3 && user_data->y_offset > 0) {
// Seed with solution above with chance 3/256th.
int compressed_block_index = (user_data->y_offset / user_data->texture->block_height - 1) *
(user_data->texture->extended_width / user_data->texture->block_width) +
user_data->x_offset / user_data->texture->block_width;
*(unsigned int *)&bitstring[0] = user_data->texture->pixels[compressed_block_index * 4];
*(unsigned int *)&bitstring[4] = user_data->texture->pixels[compressed_block_index * 4 + 1];
*(unsigned int *)&bitstring[8] = user_data->texture->pixels[compressed_block_index * 4 + 2];
*(unsigned int *)&bitstring[12] = user_data->texture->pixels[compressed_block_index * 4 + 3];
goto end;
}
if (r < 6 && user_data->y_offset > 0) {
// Seed with a random already calculated solution from the area above with chance 3/256th
int x, y;
int i = fgen_random_n(rng, (user_data->y_offset / user_data->texture->block_height) *
(user_data->texture->extended_width / user_data->texture->block_width));
x = i % (user_data->texture->extended_width / user_data->texture->block_width);
y = i / (user_data->texture->extended_width / user_data->texture->block_width);
int compressed_block_index = y * (user_data->texture->extended_width / user_data->texture->block_width) + x;
*(unsigned int *)&bitstring[0] = user_data->texture->pixels[compressed_block_index * 4];
*(unsigned int *)&bitstring[4] = user_data->texture->pixels[compressed_block_index * 4 + 1];
*(unsigned int *)&bitstring[8] = user_data->texture->pixels[compressed_block_index * 4 + 2];
*(unsigned int *)&bitstring[12] = user_data->texture->pixels[compressed_block_index * 4 + 3];
goto end;
}
fgen_seed_random(pop, bitstring);
end : ;
if (user_data->texture->type == TEXTURE_TYPE_DXT3)
optimize_block_alpha_dxt3(bitstring, user_data->alpha_pixels);
}
// Seeding function for archipelagos where each island is compressing a different block (--ultra setting).
static void seed2(FgenPopulation *pop, unsigned char *bitstring) {
BlockUserData *user_data = (BlockUserData *)pop->user_data;
if (user_data->texture->bits_per_block == 128) {
seed2_128bit(pop, bitstring);
return;
}
FgenRNG *rng = fgen_get_rng(pop);
int r = fgen_random_8(rng);
// The chance of seeding with an already calculated neighbour block should be chosen carefully.
// A too high probability results in less diversity in the archipelago.
if (r < 3 && user_data->y_offset > 0) {
// Seed with solution above with chance 3/256th
int compressed_block_index = (user_data->y_offset / 4 - 1) * (user_data->texture->extended_width /
user_data->texture->block_width) + user_data->x_offset / user_data->texture->block_width;
*(unsigned int *)&bitstring[0] = user_data->texture->pixels[compressed_block_index * 2];
*(unsigned int *)&bitstring[4] = user_data->texture->pixels[compressed_block_index * 2 + 1];
goto end;
}
if (r < 6 && user_data->y_offset > 0) {
// Seed with a random already calculated solution from the area above with chance 3/256th
int x, y;
int i = fgen_random_n(rng, (user_data->y_offset / user_data->texture->block_height) *
(user_data->texture->extended_width / user_data->texture->block_width));
x = i % (user_data->texture->extended_width / user_data->texture->block_width);
y = i / (user_data->texture->extended_width / user_data->texture->block_width);
int compressed_block_index = y * (user_data->texture->extended_width / user_data->texture->block_width) + x;
*(unsigned int *)&bitstring[0] = user_data->texture->pixels[compressed_block_index * 2];
*(unsigned int *)&bitstring[4] = user_data->texture->pixels[compressed_block_index * 2 + 1];
goto end;
}
fgen_seed_random(pop, bitstring);
end : ;
if (user_data->texture->type == TEXTURE_TYPE_ETC2_PUNCHTHROUGH)
optimize_block_alpha_etc2_punchthrough(bitstring, user_data->alpha_pixels);
}
// Seeding function for second_pass. Seed whole population with same texture data.
static void seed_second_pass(FgenPopulation *pop, unsigned char *bitstring) {
BlockUserData *user_data = (BlockUserData *)pop->user_data;
Texture *texture = user_data->texture;
int x_offset = user_data->x_offset;
int y_offset = user_data->y_offset;
int compressed_block_index = (y_offset / texture->block_height) * (texture->extended_width / texture->block_width)
+ x_offset / texture->block_width;
if (texture->bits_per_block == 64) {
// Copy the 64-bit block.
*(unsigned int *)bitstring = texture->pixels[compressed_block_index * 2];
*(unsigned int *)&bitstring[4] = texture->pixels[compressed_block_index * 2 + 1];
}
else {
// Copy the 128-bit block.
*(unsigned int *)bitstring = texture->pixels[compressed_block_index * 4];
*(unsigned int *)&bitstring[4] = texture->pixels[compressed_block_index * 4 + 1];
*(unsigned int *)&bitstring[8] = texture->pixels[compressed_block_index * 4 + 2];
*(unsigned int *)&bitstring[12] = texture->pixels[compressed_block_index * 4 + 3];
}
}
// Set the auxilliary data field for the GA population.
static void set_user_data(BlockUserData *user_data, Image *image, Texture *texture, int pass) {
user_data->flags = ENCODE_BIT;
if (texture->type == TEXTURE_TYPE_ETC1)
user_data->flags |= ETC_MODE_ALLOWED_ALL;
else if (texture->type == TEXTURE_TYPE_ETC2_RGB8 || texture->type == TEXTURE_TYPE_ETC2_EAC)
user_data->flags |= ETC2_MODE_ALLOWED_ALL;
else if (texture->type == TEXTURE_TYPE_ETC2_PUNCHTHROUGH)
user_data->flags |= ETC2_PUNCHTHROUGH_MODE_ALLOWED_ALL;
else if (texture->type == TEXTURE_TYPE_BPTC)
user_data->flags |= BPTC_MODE_ALLOWED_ALL;
else if (texture->type == TEXTURE_TYPE_BPTC_FLOAT || texture->type == TEXTURE_TYPE_BPTC_SIGNED_FLOAT)
user_data->flags |= BPTC_FLOAT_MODE_ALLOWED_ALL;
user_data->image_pixels = image->pixels;
if (image->is_half_float)
user_data->image_rowstride = image->extended_width * 8;
else
user_data->image_rowstride = image->extended_width * 4;
user_data->texture = texture;
user_data->stop_signalled = 0;
user_data->pass = pass;
}
static void set_user_data_block_flags(BlockUserData *user_data, Texture *texure, int block_flags) {
user_data->flags &= ~(MODES_ALLOWED_OPAQUE_ONLY | MODES_ALLOWED_NON_OPAQUE_ONLY |
MODES_ALLOWED_PUNCHTHROUGH_ONLY | TWO_COLORS);
if (block_flags & BLOCK_FLAG_OPAQUE)
user_data->flags |= MODES_ALLOWED_OPAQUE_ONLY;
else if (block_flags & BLOCK_FLAG_NON_OPAQUE)
user_data->flags |= MODES_ALLOWED_NON_OPAQUE_ONLY;
if (block_flags & BLOCK_FLAG_PUNCHTHROUGH)
user_data->flags |= MODES_ALLOWED_PUNCHTHROUGH_ONLY;
if (block_flags & BLOCK_FLAG_TWO_COLORS)
user_data->flags |= TWO_COLORS;
}
static void set_user_data_mode_flags(int i, BlockUserData *user_data, int block_flags) {
if ((user_data->texture->type & TEXTURE_TYPE_ETC_BIT) && option_allowed_modes_etc2 != - 1)
user_data->flags = option_allowed_modes_etc2 |
ENCODE_BIT;
else if (user_data->texture->type == TEXTURE_TYPE_ETC1 && option_modal_etc2 && nu_islands >= 2) {
if ((i & 1) == 0)
user_data->flags =
ETC_MODE_ALLOWED_INDIVIDUAL | ENCODE_BIT;
else
user_data->flags =
ETC_MODE_ALLOWED_DIFFERENTIAL | ENCODE_BIT;
}
else if ((user_data->texture->type == TEXTURE_TYPE_ETC2_RGB8 ||
user_data->texture->type == TEXTURE_TYPE_ETC2_EAC)
&& option_modal_etc2 && nu_islands >= 8) {
switch (i & 7) {
case 0 :
case 1 :
user_data->flags =
ETC_MODE_ALLOWED_INDIVIDUAL;
break;
case 2 :
case 3 :
user_data->flags =
ETC_MODE_ALLOWED_DIFFERENTIAL;
break;
case 4 :
user_data->flags =
ETC2_MODE_ALLOWED_T;
break;
case 5 :
user_data->flags =
ETC2_MODE_ALLOWED_H;
break;
case 6 :
case 7 :
user_data->flags =
ETC2_MODE_ALLOWED_PLANAR;
break;
}
user_data->flags |= ENCODE_BIT;
}
else if (user_data->texture->type == TEXTURE_TYPE_ETC2_PUNCHTHROUGH && option_modal_etc2 &&
nu_islands >= 8) {
switch (i & 7) {
case 0 :
case 1 :
user_data->flags =
ETC_MODE_ALLOWED_DIFFERENTIAL | ENCODE_BIT;
break;
case 2 :
case 3 :
user_data->flags =
ETC2_MODE_ALLOWED_T | ENCODE_BIT;
break;
case 4 :
case 5 :
user_data->flags =
ETC2_MODE_ALLOWED_H | ENCODE_BIT;
break;
case 6 :
case 7 :
if (!(block_flags & BLOCK_FLAG_OPAQUE))
if (i == 6)
user_data->flags = ETC2_MODE_ALLOWED_T;
else
user_data->flags = ETC2_MODE_ALLOWED_H;
else
user_data->flags =
ETC2_MODE_ALLOWED_PLANAR | ENCODE_BIT;
break;
}
}
// For the BPTC texture format, distribute different modes over the available islands.
if (user_data->texture->type == TEXTURE_TYPE_BPTC && nu_islands >= 8) {
if (block_flags & BLOCK_FLAG_OPAQUE)
if (block_flags & BLOCK_FLAG_TWO_COLORS)
// Only use mode 3.
user_data->flags = (1 << 3) | ENCODE_BIT;
else
// Use modes 0 to 3 only.
user_data->flags = (1 << (i & 3)) | ENCODE_BIT;
else
if ((block_flags & BLOCK_FLAG_PUNCHTHROUGH) && (block_flags & BLOCK_FLAG_TWO_COLORS))
// Only use mode 5 and 6.
user_data->flags = (1 << (5 + (i & 1))) | ENCODE_BIT;
else
// Modes 4 to 7.
user_data->flags = (1 << (4 + (i & 3))) | ENCODE_BIT;
}
// For the BPTC_FLOAT texture format, distribute different modes over the available islands.
if (user_data->texture->type == TEXTURE_TYPE_BPTC_FLOAT ||
user_data->texture->type == TEXTURE_TYPE_BPTC_SIGNED_FLOAT) {
if (nu_islands >= 8) {
switch (i & 7) {
case 0 :
case 1 : // Mode 0 (very common).
user_data->flags = 0x1 | ENCODE_BIT;
break;
case 2 : // Modes 5 (common) and 9.
user_data->flags = (1 << 5) | (1 << 9) | ENCODE_BIT;
break;
case 3 : // Modes 2 and 6 (common).
user_data->flags = (1 << 2) | (1 << 6) | ENCODE_BIT;
break;
case 4 : // Modes 3 and 7 (common).
user_data->flags = (1 << 3) | (1 << 7) | ENCODE_BIT;
break;
case 5 : // Modes 1, 4, 8 (common).
user_data->flags = (1 << 1) | (1 << 4) | (1 << 8)
| ENCODE_BIT;
break;
case 6 : // Mode 11.
user_data->flags = (1 << 11) | ENCODE_BIT;
break;
case 7 : // Modes 10, 11, 12, 13.
user_data->flags = (0xF << 10) | ENCODE_BIT;
break;
}
}
else
if (/* option_modal_etc2 && */ nu_islands >= 4) {
switch (i & 3) {
case 0 : // Mode 0.
user_data->flags = 0x1 | ENCODE_BIT;
break;
case 1 : // Modes 2, 4, 6 (common), 8 (common), 9.
user_data->flags = (1 << 2) | (1 << 4) |
(1 << 6) | (1 << 8) | (1 << 9) | ENCODE_BIT;
break;
case 2 : // Modes 1, 3, 5 (common), 7 (common).
user_data->flags = (1 << 1) | (1 << 3) |
(1 << 5) | (1 << 7) | ENCODE_BIT;
break;
case 3 : // Modes 10, 11, 12, 13.
user_data->flags = (0xF << 10) | ENCODE_BIT;
break;
}
}
}
}
static char *etc2_modestr = "IDTHP";
// Report the given GA solution and store its bitstring in the texture, printing information if required.
static void report_solution(FgenIndividual *best, BlockUserData *user_data, int nu_gens, bool compress_callback) {
Texture *texture = user_data->texture;
int x_offset = user_data->x_offset;
int y_offset = user_data->y_offset;
// Calculate the block index of the block.
int compressed_block_index = (y_offset / texture->block_height) * (texture->extended_width / texture->block_width)
+ x_offset / texture->block_width;
if (texture->bits_per_block == 64) {
// Copy the 64-bit block.
texture->pixels[compressed_block_index * 2] = *(unsigned int *)best->bitstring;
texture->pixels[compressed_block_index * 2 + 1] = *(unsigned int *)&best->bitstring[4];
}
else {
// Copy the 128-bit block.
texture->pixels[compressed_block_index * 4] = *(unsigned int *)best->bitstring;
texture->pixels[compressed_block_index * 4 + 1] = *(unsigned int *)&best->bitstring[4];
texture->pixels[compressed_block_index * 4 + 2] = *(unsigned int *)&best->bitstring[8];
texture->pixels[compressed_block_index * 4 + 3] = *(unsigned int *)&best->bitstring[12];
}
// When perceptive compression is enabled, store the decompressed block data.
if (option_perceptive && option_compression_level >= COMPRESSION_LEVEL_CLASS_1)
user_data->texture->decoding_function(best->bitstring, user_data->texture_pixels, user_data->flags);
// Print info.
if (option_verbose) {
printf("Block %d: ", (y_offset / texture->block_height) * (texture->extended_width / texture->block_width)
+ (x_offset / texture->block_width));
if (texture->type & TEXTURE_TYPE_ETC_BIT) {
int mode = texture->get_mode_function(best->bitstring);
printf("Mode: %c ", etc2_modestr[mode]);
if (compress_callback)
mode_statistics[mode]++;
}
else if (texture->type == TEXTURE_TYPE_BPTC ||
texture->type == TEXTURE_TYPE_BPTC_FLOAT || texture->type == TEXTURE_TYPE_BPTC_SIGNED_FLOAT) {
int mode = texture->get_mode_function(best->bitstring);
printf("Mode: %d ", mode);
if (compress_callback)
mode_statistics[mode]++;
}
printf("Combined: ");
printf("RMSE per pixel: %lf, ", sqrt((1.0 / best->fitness) / 16));
printf("GA generations: %d\n", nu_gens);
}
if (option_progress) {
int n = (texture->extended_width / texture->block_width) * (texture->extended_height / texture->block_height);
int old_percentage = (compressed_block_index - 1) * 100 / n;
int new_percentage = compressed_block_index * 100 / n;
if (new_percentage == 99 && old_percentage == 98)
printf("99%%\n");
else
if (new_percentage != old_percentage)
printf("%d%% ", new_percentage);
fflush(stdout);
}
if (compress_callback)
compress_callback_func(user_data);
}
// Compress each block with a single GA population. Unused.
static void compress_with_single_population(Image *image, Texture *texture) {
if (!option_quiet)
printf("Running single GA for each pixel block.\n");
FgenPopulation *pop = fgen_create(
population_size, // Population size.
texture->bits_per_block, // Number of bits.
1, // Data element size.
generation_callback,
calculate_fitness,
fgen_seed_random,
fgen_mutation_per_bit_fast,
fgen_crossover_uniform_per_bit
);
fgen_set_parameters(
pop,
FGEN_ELITIST_SUS,
FGEN_SUBTRACT_MIN_FITNESS,
crossover_probability, // Crossover prob.
mutation_probability, // Mutation prob. per bit
0 // Macro-mutation prob.
);
fgen_set_generation_callback_interval(pop, nu_generations);
if (!option_deterministic)
fgen_random_seed_with_timer(fgen_get_rng(pop));
// fgen_random_seed_rng(fgen_get_rng(pop), 0);
pop->user_data = (BlockUserData *)malloc(sizeof(BlockUserData));
set_user_data((BlockUserData *)pop->user_data, image, texture, 1);
for (int y = 0; y < image->extended_height; y += texture->block_height)
for (int x = 0; x < image->extended_width; x+= texture->block_width) {
BlockUserData *user_data = (BlockUserData *)pop->user_data;
user_data->x_offset = x;
user_data->y_offset = y;
// Threading a single population doesn't work well in this case because the of the relatively quick
// fitness function, which causes too much overhead caused by threading.
// if (use_threading)
// fgen_run_threaded(pop, nu_generations);
// else
fgen_run(pop, nu_generations);
report_solution(fgen_best_individual_of_population(pop), user_data, pop->generation, true);
if (user_data->stop_signalled)
goto end;
}
end :
free(pop->user_data);
fgen_destroy(pop);
}
unsigned int *allocate_texture_pixels(Texture *texture) {
int n = (texture->extended_height / texture->block_height) *
(texture->extended_width / texture->block_width);
int bytespp = 4;
if (texture->type & TEXTURE_TYPE_HALF_FLOAT_BIT)
bytespp = 8; // 64-bit pixels
unsigned int *texture_pixels = (unsigned int *)malloc(n * texture->block_width * texture->block_height
* bytespp);