| Crates.io | ktx2-rw |
| lib.rs | ktx2-rw |
| version | 0.2.2 |
| created_at | 2025-09-15 14:03:51.044261+00 |
| updated_at | 2025-09-16 12:04:03.343182+00 |
| description | A high-level Rust wrapper for KTX2 texture format with full Basis Universal support |
| homepage | https://github.com/AllenDang/ktx2-rw |
| repository | https://github.com/AllenDang/ktx2-rw |
| max_upload_size | |
| id | 1840024 |
| size | 192,630 |
A high-level Rust wrapper for the KTX2 texture format with full Basis Universal support.
KTX2 is the next-generation texture format developed by Khronos, designed for efficient GPU texture storage and transmission. This library provides safe, idiomatic Rust bindings with comprehensive support for texture compression, transcoding, and metadata management.
Add this to your Cargo.toml:
[dependencies]
ktx2-rw = { git = "https://github.com/AllenDang/ktx2-rw" }
The library builds KTX-Software from source, so you need:
Platform-specific requirements:
ANDROID_NDK_ROOT)use ktx2_rw::{Ktx2Texture, BasisCompressionParams, TranscodeFormat, VkFormat};
// Create a new texture
let mut texture = Ktx2Texture::create(512, 512, 1, 1, 1, 1, VkFormat::R8G8B8A8Unorm)?; // RGBA8
// Add image data (512x512 RGBA)
let rgba_data: Vec<u8> = generate_image_data();
texture.set_image_data(0, 0, 0, &rgba_data)?;
// Compress with Basis Universal using the builder pattern
let compression_params = BasisCompressionParams::builder()
.quality_level(128)
.thread_count(4)
.build();
texture.compress_basis(&compression_params)?;
// For simple compression, you can also use:
texture.compress_basis_simple(128)?; // Quality level only
// Save to file
texture.write_to_file("texture.ktx2")?;
// Load from file
let loaded = Ktx2Texture::from_file("texture.ktx2")?;
// Transcode to GPU format
let mut transcoded = loaded;
transcoded.transcode_basis(TranscodeFormat::Bc7Rgba)?;
use ktx2_rw::BasisCompressionParams;
// Using the builder pattern for configuration
let params = BasisCompressionParams::builder()
.uastc(true) // Use UASTC (higher quality)
.quality_level(255) // Maximum quality
.thread_count(8) // Multi-threaded compression
.normal_map(true) // Optimize for normal maps
.uastc_rdo_quality_scalar(1.0) // RDO quality
.build();
texture.compress_basis(¶ms)?;
| Platform | Architecture | Status |
|---|---|---|
| Windows | x64 (GNU) | ✅ |
| Windows | x64 (MSVC) | ✅ |
| Windows | ARM64 | ✅ |
| macOS | x64 | ✅ |
| macOS | ARM64 | ✅ |
| Linux | x64 | ✅ |
| Linux | x64 (musl) | ✅ |
| Linux | ARM64 | ✅ |
| Android | All ABIs | ✅ |
| iOS | All | ✅ |
The library now builds KTX-Software from source at compile time, providing excellent cross-compilation support.
The first build downloads and compiles KTX-Software (~5-10 minutes), but subsequent builds use cached results.
The library supports transcoding to all major GPU texture formats:
Ktx2Texture - Main texture handle with safe lifetime managementBasisCompressionParams - Comprehensive compression settings
BasisCompressionParams::builder() - Fluent builder for creating paramsTranscodeFormat - Supported GPU texture formatsError - Detailed error types with proper error messagesKtx2Texture::create(width, height, depth, layers, faces, levels, vk_format)
Ktx2Texture::from_file(path)
Ktx2Texture::from_memory(bytes)
texture.compress_basis(params) // Compress with Basis Universal
texture.compress_basis_simple(quality) // Simple compression with quality level
texture.transcode_basis(format) // Transcode to GPU format
texture.get_image_data(level, layer, face) // Get raw image data
texture.set_image_data(level, layer, face, data) // Set image data
texture.write_to_file(path) // Save to file
texture.write_to_memory() // Export to bytes
texture.set_metadata(key, value) // Set custom metadata
texture.get_metadata(key) // Read metadata
texture.width(), texture.height(), texture.depth()
texture.layers(), texture.faces(), texture.levels()
texture.is_compressed(), texture.needs_transcoding()
texture.vk_format()
All operations return Result<T, Error> with detailed error information:
match texture.compress_basis(¶ms) {
Ok(()) => println!("Compression successful"),
Err(ktx2_rw::Error::OutOfMemory) => println!("Not enough memory"),
Err(ktx2_rw::Error::UnsupportedFeature) => println!("Feature not supported"),
Err(e) => println!("Other error: {}", e),
}
Ktx2Texture implements Send + Sync and can be safely used across threads.
This library is provided under the same license terms as the underlying KTX2 library.
Contributions are welcome! Please ensure all tests pass and follow Rust coding conventions.