Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 37 additions & 16 deletions gltf/encodebasis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,39 @@ struct BasisSettings
int etc1s_q;
int uastc_l;
float uastc_q;
int xuastc_e;
int xuastc_q;
};

static const BasisSettings kBasisSettings[10] = {
{1, 1, 0, 4.f},
{1, 32, 0, 3.f},
{1, 64, 1, 2.f},
{1, 96, 1, 1.5f},
{1, 128, 1, 1.f}, // quality arguments aligned with basisu defaults
{1, 150, 1, 0.8f},
{1, 170, 1, 0.6f},
{1, 192, 1, 0.4f}, // gltfpack defaults
{1, 224, 2, 0.2f},
{1, 255, 2, 0.f},
{1, 1, 0, 4.f, 0, 10},
{1, 32, 0, 3.f, 0, 20},
{1, 64, 1, 2.f, 1, 30},
{1, 96, 1, 1.5f, 1, 40},
{1, 128, 1, 1.f, 1, 50},
{1, 150, 1, 0.8f, 1, 60},
{1, 170, 1, 0.6f, 1, 70},
{1, 192, 1, 0.4f, 1, 80}, // gltfpack defaults
{1, 224, 2, 0.2f, 2, 85},
{1, 255, 2, 0.f, 2, 90},
};

static void fillParams(basisu::basis_compressor_params& params, const char* input, const char* output, bool uastc, int width, int height, const BasisSettings& bs, const ImageInfo& info, const Settings& settings)
static void fillParams(basisu::basis_compressor_params& params, const char* input, const char* output, TextureMode mode, int width, int height, const BasisSettings& bs, const ImageInfo& info, const Settings& settings)
{
if (uastc)
if (mode == TextureMode_XUASTC)
{
params.m_uastc = true;

#if BASISU_LIB_VERSION >= 200
params.m_xuastc_or_astc_ldr_basis_tex_format = int(basist::basis_tex_format::cXUASTC_LDR_4x4);
params.m_quality_level = bs.xuastc_q;
params.m_xuastc_ldr_effort_level = bs.xuastc_e;
params.m_xuastc_ldr_use_dct = true;
params.m_xuastc_ldr_use_lossy_supercompression = true;
params.m_xuastc_ldr_syntax = int(basist::astc_ldr_t::xuastc_ldr_syntax::cHybridArithZStd);
#endif
}
else if (mode == TextureMode_UASTC)
{
static const uint32_t s_level_flags[basisu::TOTAL_PACK_UASTC_LEVELS] = {basisu::cPackUASTCLevelFastest, basisu::cPackUASTCLevelFaster, basisu::cPackUASTCLevelDefault, basisu::cPackUASTCLevelSlower, basisu::cPackUASTCLevelVerySlow};

Expand Down Expand Up @@ -110,7 +125,7 @@ static void fillParams(basisu::basis_compressor_params& params, const char* inpu
params.m_ktx2_srgb_transfer_func = info.srgb;
#endif

if (uastc)
if (mode == TextureMode_UASTC)
{
params.m_ktx2_uastc_supercompression = basist::KTX2_SS_ZSTANDARD;
params.m_ktx2_zstd_supercompression_level = 9;
Expand All @@ -134,6 +149,11 @@ static void fillParams(basisu::basis_compressor_params& params, const char* inpu

static const char* prepareEncode(basisu::basis_compressor_params& params, const cgltf_image& image, const char* input_path, const ImageInfo& info, const Settings& settings, const std::string& temp_prefix, std::string& temp_input, std::string& temp_output)
{
#if BASISU_LIB_VERSION < 200
if (settings.texture_mode[info.kind] == TextureMode_XUASTC)
return "XUASTC mode requires BasisU library version 2.0 or higher";
#endif

std::string img_data;
std::string mime_type;

Expand All @@ -156,11 +176,11 @@ static const char* prepareEncode(basisu::basis_compressor_params& params, const
return "error writing temporary file";

int quality = settings.texture_quality[info.kind];
bool uastc = settings.texture_mode[info.kind] == TextureMode_UASTC;
TextureMode mode = settings.texture_mode[info.kind];

const BasisSettings& bs = kBasisSettings[quality - 1];

fillParams(params, temp_input.c_str(), temp_output.c_str(), uastc, width, height, bs, info, settings);
fillParams(params, temp_input.c_str(), temp_output.c_str(), mode, width, height, bs, info, settings);

return NULL;
}
Expand All @@ -181,8 +201,9 @@ void encodeImagesBasis(std::string* encoded, const cgltf_data* data, const std::
{
const cgltf_image& image = data->images[i];
ImageInfo info = images[i];
TextureMode mode = settings.texture_mode[info.kind];

if (settings.texture_mode[info.kind] == TextureMode_ETC1S || settings.texture_mode[info.kind] == TextureMode_UASTC)
if (mode == TextureMode_ETC1S || mode == TextureMode_UASTC || mode == TextureMode_XUASTC)
if (const char* error = prepareEncode(params[i], image, input_path, info, settings, temp_prefix + "-" + std::to_string(i), temp_inputs[i], temp_outputs[i]))
encoded[i] = error;
}
Expand Down
25 changes: 22 additions & 3 deletions gltf/gltfpack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1453,6 +1453,16 @@ int main(int argc, char** argv)

applySetting(settings.texture_mode, TextureMode_ETC1S, mask);
}
else if (strcmp(arg, "-tx") == 0)
{
settings.texture_ktx2 = true;

unsigned int mask = ~0u;
if (i + 1 < argc && isalpha(argv[i + 1][0]))
mask = textureMask(argv[++i]);

applySetting(settings.texture_mode, TextureMode_XUASTC, mask);
}
else if (strcmp(arg, "-tw") == 0)
{
settings.texture_webp = true;
Expand Down Expand Up @@ -1634,8 +1644,9 @@ int main(int argc, char** argv)
fprintf(stderr, "\t-o file: output file path, .gltf/.glb\n");
fprintf(stderr, "\t-c: produce compressed gltf/glb files (-cc/-cz for higher compression ratio)\n");
fprintf(stderr, "\nTextures:\n");
fprintf(stderr, "\t-tc: convert all textures to KTX2 with BasisU supercompression\n");
fprintf(stderr, "\t-tu: use UASTC when encoding textures (much higher quality and much larger size)\n");
fprintf(stderr, "\t-tc: convert all textures to KTX2 with BasisU ETC1S supercompression\n");
fprintf(stderr, "\t-tu: use UASTC when encoding textures (higher quality and larger size)\n");
fprintf(stderr, "\t-tx: use XUASTC when encoding textures (DCT supercompression)\n");
fprintf(stderr, "\t-tw: convert all textures to WebP\n");
fprintf(stderr, "\t-tq N: set texture encoding quality (default: 8; N should be between 1 and 10)\n");
fprintf(stderr, "\t-ts R: scale texture dimensions by the ratio R (default: 1; R should be between 0 and 1)\n");
Expand All @@ -1647,6 +1658,7 @@ int main(int argc, char** argv)
fprintf(stderr, "\tTexture classes:\n");
fprintf(stderr, "\t-tc C: use ETC1S when encoding textures of class C\n");
fprintf(stderr, "\t-tu C: use UASTC when encoding textures of class C\n");
fprintf(stderr, "\t-tx C: use XUASTC when encoding textures of class C\n");
fprintf(stderr, "\t-tw C: use WebP when encoding textures of class C\n");
fprintf(stderr, "\t-tq C N: set texture encoding quality for class C\n");
fprintf(stderr, "\t-ts C R: scale texture dimensions for class C\n");
Expand Down Expand Up @@ -1710,7 +1722,7 @@ int main(int argc, char** argv)

if (require_texc && !settings.texture_ktx2 && !settings.texture_webp)
{
fprintf(stderr, "Texture processing is only supported when texture compression is enabled via -tc/-tu/-tw\n");
fprintf(stderr, "Texture processing is only supported when texture compression is enabled via -tc/-tu/-tx/-tw\n");
return 1;
}

Expand All @@ -1735,6 +1747,13 @@ int main(int argc, char** argv)
if (settings.keep_nodes && (settings.mesh_merge || settings.mesh_instancing))
fprintf(stderr, "Warning: option -kn disables mesh merge (-mm) and mesh instancing (-mi) optimizations\n");

for (int i = 0; i < TextureKind__Count; ++i)
if (settings.texture_mode[i] == TextureMode_XUASTC)
{
fprintf(stderr, "Warning: -tx uses XUASTC format which is not part of the KHR_texture_basisu specification\n");
break;
}

return gltfpack(input, output, report, settings);
}
#endif
Expand Down
1 change: 1 addition & 0 deletions gltf/gltfpack.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ enum TextureMode
TextureMode_Raw,
TextureMode_ETC1S,
TextureMode_UASTC,
TextureMode_XUASTC,
TextureMode_WebP,
};

Expand Down