3
3
// SPDX-License-Identifier: Apache-2.0
4
4
5
5
#include " command.h"
6
+ #include " encode_utils_common.h"
6
7
#include " platform_utils.h"
7
8
#include " metrics_utils.h"
8
- #include " compress_utils.h"
9
- #include " encode_utils.h"
9
+ #include " deflate_utils.h"
10
+ #include " encode_utils_basis.h"
11
+ #include " encode_utils_astc.h"
10
12
#include " format_descriptor.h"
11
13
#include " formats.h"
12
14
#include " utility.h"
@@ -561,89 +563,6 @@ struct OptionsCreate {
561
563
}
562
564
};
563
565
564
- struct OptionsASTC : public ktxAstcParams {
565
- inline static const char * kAstcQuality = " astc-quality" ;
566
- inline static const char * kAstcPerceptual = " astc-perceptual" ;
567
-
568
- inline static const char * kAstcOptions [] = {
569
- kAstcQuality ,
570
- kAstcPerceptual
571
- };
572
-
573
- std::string astcOptions{};
574
- bool encodeASTC = false ;
575
- ClampedOption<ktx_uint32_t > qualityLevel{ktxAstcParams::qualityLevel, 0 , KTX_PACK_ASTC_QUALITY_LEVEL_MAX};
576
-
577
- OptionsASTC () : ktxAstcParams() {
578
- threadCount = std::thread::hardware_concurrency ();
579
- if (threadCount == 0 )
580
- threadCount = 1 ;
581
- structSize = sizeof (ktxAstcParams);
582
- normalMap = false ;
583
- for (int i = 0 ; i < 4 ; i++)
584
- inputSwizzle[i] = 0 ;
585
- qualityLevel.clear ();
586
- }
587
-
588
- void init (cxxopts::Options& opts) {
589
- opts.add_options (" Encode ASTC" )
590
- (kAstcQuality ,
591
- " The quality level configures the quality-performance tradeoff for "
592
- " the compressor; more complete searches of the search space "
593
- " improve image quality at the expense of compression time. Default "
594
- " is 'medium'. The quality level can be set between fastest (0) and "
595
- " exhaustive (100) via the following fixed quality presets:\n\n "
596
- " Level | Quality\n "
597
- " ---------- | -----------------------------\n "
598
- " fastest | (equivalent to quality = 0)\n "
599
- " fast | (equivalent to quality = 10)\n "
600
- " medium | (equivalent to quality = 60)\n "
601
- " thorough | (equivalent to quality = 98)\n "
602
- " exhaustive | (equivalent to quality = 100)" ,
603
- cxxopts::value<std::string>(), " <level>" )
604
- (kAstcPerceptual ,
605
- " The codec should optimize for perceptual error, instead of direct "
606
- " RMS error. This aims to improve perceived image quality, but "
607
- " typically lowers the measured PSNR score. Perceptual methods are "
608
- " currently only available for normal maps and RGB color data." );
609
- }
610
-
611
- void captureASTCOption (const char * name) {
612
- astcOptions += fmt::format (" --{}" , name);
613
- }
614
-
615
- template <typename T>
616
- T captureASTCOption (cxxopts::ParseResult& args, const char * name) {
617
- const T value = args[name].as <T>();
618
- astcOptions += fmt::format (" --{} {}" , name, value);
619
- return value;
620
- }
621
-
622
- void process (cxxopts::Options&, cxxopts::ParseResult& args, Reporter& report) {
623
- if (args[kAstcQuality ].count ()) {
624
- static std::unordered_map<std::string, ktx_pack_astc_quality_levels_e> astc_quality_mapping{
625
- {" fastest" , KTX_PACK_ASTC_QUALITY_LEVEL_FASTEST},
626
- {" fast" , KTX_PACK_ASTC_QUALITY_LEVEL_FAST},
627
- {" medium" , KTX_PACK_ASTC_QUALITY_LEVEL_MEDIUM},
628
- {" thorough" , KTX_PACK_ASTC_QUALITY_LEVEL_THOROUGH},
629
- {" exhaustive" , KTX_PACK_ASTC_QUALITY_LEVEL_EXHAUSTIVE}
630
- };
631
- const auto qualityLevelStr = to_lower_copy (captureASTCOption<std::string>(args, kAstcQuality ));
632
- const auto it = astc_quality_mapping.find (qualityLevelStr);
633
- if (it == astc_quality_mapping.end ())
634
- report.fatal_usage (" Invalid astc-quality: \" {}\" " , qualityLevelStr);
635
- qualityLevel = it->second ;
636
- } else {
637
- qualityLevel = KTX_PACK_ASTC_QUALITY_LEVEL_MEDIUM;
638
- }
639
-
640
- if (args[kAstcPerceptual ].count ()) {
641
- captureASTCOption (kAstcPerceptual );
642
- perceptual = KTX_TRUE;
643
- }
644
- }
645
- };
646
-
647
566
// -------------------------------------------------------------------------------------------------
648
567
649
568
/* * @page ktx_create ktx create
@@ -693,30 +612,7 @@ Create a KTX2 file from various input files.
693
612
otherwise they are ignored.<br />
694
613
The format will be used to verify and load all input files into a texture before encoding.<br />
695
614
Case insensitive. Required.</dd>
696
- <dl>
697
- <dt>\--astc-quality <level></dt>
698
- <dd>The quality level configures the quality-performance
699
- tradeoff for the compressor; more complete searches of the
700
- search space improve image quality at the expense of
701
- compression time. Default is 'medium'. The quality level can be
702
- set between fastest (0) and exhaustive (100) via the
703
- following fixed quality presets:
704
- <table>
705
- <tr><th>Level </th> <th> Quality </th></tr>
706
- <tr><td>fastest </td> <td>(equivalent to quality = 0) </td></tr>
707
- <tr><td>fast </td> <td>(equivalent to quality = 10) </td></tr>
708
- <tr><td>medium </td> <td>(equivalent to quality = 60) </td></tr>
709
- <tr><td>thorough </td> <td>(equivalent to quality = 98) </td></tr>
710
- <tr><td>exhaustive </td> <td>(equivalent to quality = 100) </td></tr>
711
- </table>
712
- </dd>
713
- <dt>\--astc-perceptual</dt>
714
- <dd>The codec should optimize for perceptual error, instead of
715
- direct RMS error. This aims to improve perceived image quality,
716
- but typically lowers the measured PSNR score. Perceptual
717
- methods are currently only available for normal maps and RGB
718
- color data.</dd>
719
- </dl>
615
+ @snippet{doc} ktx/astc_utils.h command options_encode_astc
720
616
<dt>\--1d</dt>
721
617
<dd>Create a 1D texture. If not set the texture will be a 2D or 3D texture.</dd>
722
618
<dt>\--cubemap</dt>
@@ -768,7 +664,8 @@ Create a KTX2 file from various input files.
768
664
With each encoding option the following encoder specific options become valid,
769
665
otherwise they are ignored. Case-insensitive.</dd>
770
666
771
- @snippet{doc} ktx/encode_utils.h command options_codec
667
+ @snippet{doc} ktx/basis_utils.h command options_encode_basis
668
+ @snippet{doc} ktx/encode_utils_common.h command options_encode_common
772
669
@snippet{doc} ktx/metrics_utils.h command options_metrics
773
670
</dl>
774
671
<dl>
@@ -813,7 +710,7 @@ Create a KTX2 file from various input files.
813
710
<dt>\--warn-on-color-conversions</dt>
814
711
<dd>Generates a warning if any of the input images are color converted.</dd>
815
712
</dl>
816
- @snippet{doc} ktx/compress_utils .h command options_compress
713
+ @snippet{doc} ktx/deflate_utils .h command options_deflate
817
714
@snippet{doc} ktx/command.h command options_generic
818
715
819
716
@section ktx_create_exitstatus EXIT STATUS
@@ -830,7 +727,7 @@ Create a KTX2 file from various input files.
830
727
*/
831
728
class CommandCreate : public Command {
832
729
private:
833
- Combine<OptionsCreate, OptionsASTC, OptionsCodec <false >, OptionsMetrics, OptionsCompress , OptionsMultiInSingleOut, OptionsGeneric> options;
730
+ Combine<OptionsCreate, OptionsEncodeASTC, OptionsEncodeBasis <false >, OptionsEncodeCommon, OptionsMetrics, OptionsDeflate , OptionsMultiInSingleOut, OptionsGeneric> options;
834
731
835
732
uint32_t targetChannelCount = 0 ; // Derived from VkFormat
836
733
@@ -846,9 +743,9 @@ class CommandCreate : public Command {
846
743
847
744
private:
848
745
void executeCreate ();
849
- void encode (KTXTexture2& texture, OptionsCodec <false >& opts);
850
- void encodeASTC (KTXTexture2& texture, OptionsASTC & opts);
851
- void compress (KTXTexture2& texture, const OptionsCompress & opts);
746
+ void encodeBasis (KTXTexture2& texture, OptionsEncodeBasis <false >& opts);
747
+ void encodeASTC (KTXTexture2& texture, OptionsEncodeASTC & opts);
748
+ void compress (KTXTexture2& texture, const OptionsDeflate & opts);
852
749
853
750
private:
854
751
template <typename F>
@@ -918,20 +815,24 @@ void CommandCreate::processOptions(cxxopts::Options& opts, cxxopts::ParseResult&
918
815
}
919
816
920
817
if (!isFormatAstc (options.vkFormat )) {
921
- for (const char * astcOption : OptionsASTC ::kAstcOptions )
818
+ for (const char * astcOption : OptionsEncodeASTC ::kAstcOptions )
922
819
if (args[astcOption].count ())
923
820
fatal_usage (" --{} can only be used with ASTC formats." , astcOption);
821
+ } else {
822
+ fillOptionsCodecAstc<decltype (options)>(options);
823
+ if (options.OptionsEncodeCommon ::noSSE)
824
+ fatal_usage (" --{} is not allowed with ASTC encode" , OptionsEncodeCommon::kNoSse );
924
825
}
925
826
926
- if (options.codec == EncodeCodec ::BasisLZ) {
827
+ if (options.codec == BasisCodec ::BasisLZ) {
927
828
if (options.zstd .has_value ())
928
829
fatal_usage (" Cannot encode to BasisLZ and supercompress with Zstd." );
929
830
930
831
if (options.zlib .has_value ())
931
832
fatal_usage (" Cannot encode to BasisLZ and supercompress with ZLIB." );
932
833
}
933
834
934
- if (options.codec != EncodeCodec ::NONE) {
835
+ if (options.codec != BasisCodec ::NONE) {
935
836
switch (options.vkFormat ) {
936
837
case VK_FORMAT_R8_UNORM:
937
838
case VK_FORMAT_R8_SRGB:
@@ -950,7 +851,11 @@ void CommandCreate::processOptions(cxxopts::Options& opts, cxxopts::ParseResult&
950
851
}
951
852
}
952
853
953
- const auto canCompare = options.codec == EncodeCodec::BasisLZ || options.codec == EncodeCodec::UASTC;
854
+ const auto canCompare = options.codec == BasisCodec::BasisLZ || options.codec == BasisCodec::UASTC;
855
+
856
+ if (canCompare)
857
+ fillOptionsCodecBasis<decltype (options)>(options);
858
+
954
859
if (options.compare_ssim && !canCompare)
955
860
fatal_usage (" --compare-ssim can only be used with BasisLZ or UASTC encoding." );
956
861
if (options.compare_psnr && !canCompare)
@@ -1258,12 +1163,12 @@ void CommandCreate::executeCreate() {
1258
1163
}
1259
1164
1260
1165
// Encode and apply compression
1261
- encode (texture, options);
1166
+ encodeBasis (texture, options);
1262
1167
encodeASTC (texture, options);
1263
1168
compress (texture, options);
1264
1169
1265
1170
// Add KTXwriterScParams metadata if ASTC encoding, BasisU encoding, or other supercompression was used
1266
- const auto writerScParams = fmt::format (" {}{}{}" , options.astcOptions , options.codecOptions , options.compressOptions );
1171
+ const auto writerScParams = fmt::format (" {}{}{}{} " , options.astcOptions , options.codecOptions , options. commonOptions , options.compressOptions );
1267
1172
if (writerScParams.size () > 0 ) {
1268
1173
// Options always contain a leading space
1269
1174
assert (writerScParams[0 ] == ' ' );
@@ -1283,12 +1188,12 @@ void CommandCreate::executeCreate() {
1283
1188
1284
1189
// -------------------------------------------------------------------------------------------------
1285
1190
1286
- void CommandCreate::encode (KTXTexture2& texture, OptionsCodec <false >& opts) {
1191
+ void CommandCreate::encodeBasis (KTXTexture2& texture, OptionsEncodeBasis <false >& opts) {
1287
1192
MetricsCalculator metrics;
1288
1193
metrics.saveReferenceImages (texture, options, *this );
1289
1194
1290
- if (opts.codec != EncodeCodec ::NONE) {
1291
- auto ret = ktxTexture2_CompressBasisEx (texture, &opts. basisOpts );
1195
+ if (opts.codec != BasisCodec ::NONE) {
1196
+ auto ret = ktxTexture2_CompressBasisEx (texture, &opts);
1292
1197
if (ret != KTX_SUCCESS)
1293
1198
fatal (rc::KTX_FAILURE, " Failed to encode KTX2 file with codec \" {}\" . KTX Error: {}" ,
1294
1199
to_underlying (opts.codec ), ktxErrorString (ret));
@@ -1297,15 +1202,15 @@ void CommandCreate::encode(KTXTexture2& texture, OptionsCodec<false>& opts) {
1297
1202
metrics.decodeAndCalculateMetrics (texture, options, *this );
1298
1203
}
1299
1204
1300
- void CommandCreate::encodeASTC (KTXTexture2& texture, OptionsASTC & opts) {
1205
+ void CommandCreate::encodeASTC (KTXTexture2& texture, OptionsEncodeASTC & opts) {
1301
1206
if (opts.encodeASTC ) {
1302
1207
const auto ret = ktxTexture2_CompressAstcEx (texture, &opts);
1303
1208
if (ret != KTX_SUCCESS)
1304
1209
fatal (rc::KTX_FAILURE, " Failed to encode KTX2 file with codec ASTC. KTX Error: {}" , ktxErrorString (ret));
1305
1210
}
1306
1211
}
1307
1212
1308
- void CommandCreate::compress (KTXTexture2& texture, const OptionsCompress & opts) {
1213
+ void CommandCreate::compress (KTXTexture2& texture, const OptionsDeflate & opts) {
1309
1214
if (opts.zstd ) {
1310
1215
const auto ret = ktxTexture2_DeflateZstd (texture, *opts.zstd );
1311
1216
if (ret != KTX_SUCCESS)
0 commit comments