use std::path::Path;
use basisu::{DecodeFlags, SourceFormat, TargetFormat, Transcoder};
use ktx2::{ColorPrimaries, Format, SupercompressionScheme, TransferFunction};
use crate::error::Error;
use crate::math::ColorSpace;
use crate::textures::{MinFilter, Mipmap, Texture, TextureFilter};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct Ktx2Support {
pub astc: bool,
pub etc1: bool,
pub etc2: bool,
pub dxt: bool,
pub bptc: bool,
pub pvrtc: bool,
}
impl Ktx2Support {
pub fn from_features(features: wgpu::Features) -> Self {
Self {
astc: features.contains(wgpu::Features::TEXTURE_COMPRESSION_ASTC),
etc1: false,
etc2: features.contains(wgpu::Features::TEXTURE_COMPRESSION_ETC2),
dxt: false,
bptc: features.contains(wgpu::Features::TEXTURE_COMPRESSION_BC),
pvrtc: false,
}
}
}
#[derive(Clone, Debug, Default)]
pub struct Ktx2Loader {
config: Ktx2Support,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Ktx2Class {
CompressedTexture,
CompressedArrayTexture,
CompressedCubeTexture,
DataTexture,
Data3DTexture,
}
impl Ktx2Class {
pub fn name(self) -> &'static str {
match self {
Ktx2Class::CompressedTexture => "CompressedTexture",
Ktx2Class::CompressedArrayTexture => "CompressedArrayTexture",
Ktx2Class::CompressedCubeTexture => "CompressedCubeTexture",
Ktx2Class::DataTexture => "DataTexture",
Ktx2Class::Data3DTexture => "Data3DTexture",
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u32)]
#[non_exhaustive]
pub enum EngineFormat {
Rgb = 1022,
Rgba = 1023,
Red = 1028,
Rg = 1030,
RgbS3tcDxt1 = 33776,
RgbaS3tcDxt1 = 33777,
RgbaS3tcDxt5 = 33779,
RgbPvrtc4bppV1 = 35840,
RgbaPvrtc4bppV1 = 35842,
RgbaPvrtc2bppV1 = 35843,
RgbEtc1 = 36196,
RedRgtc1 = 36283,
SignedRedRgtc1 = 36284,
RedGreenRgtc2 = 36285,
SignedRedGreenRgtc2 = 36286,
RgbaBptc = 36492,
RgbBptcUnsigned = 36495,
R11Eac = 37488,
SignedR11Eac = 37489,
Rg11Eac = 37490,
SignedRg11Eac = 37491,
RgbEtc2 = 37492,
RgbaEtc2Eac = 37496,
RgbaAstc4x4 = 37808,
RgbaAstc6x6 = 37812,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u32)]
pub enum EngineType {
UnsignedByte = 1009,
UnsignedShort = 1012,
Float = 1015,
HalfFloat = 1016,
UnsignedInt101111 = 35899,
UnsignedInt5999 = 35902,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Ktx2ColorSpace {
NoColorSpace,
Srgb,
LinearSrgb,
DisplayP3,
LinearDisplayP3,
}
impl Ktx2ColorSpace {
pub fn name(self) -> &'static str {
match self {
Ktx2ColorSpace::NoColorSpace => "",
Ktx2ColorSpace::Srgb => "srgb",
Ktx2ColorSpace::LinearSrgb => "srgb-linear",
Ktx2ColorSpace::DisplayP3 => "display-p3",
Ktx2ColorSpace::LinearDisplayP3 => "display-p3-linear",
}
}
pub(crate) fn is_srgb_transfer(self) -> bool {
matches!(self, Ktx2ColorSpace::Srgb | Ktx2ColorSpace::DisplayP3)
}
}
#[derive(Clone, Debug)]
pub struct Ktx2Texture {
pub class: Ktx2Class,
pub format: EngineFormat,
pub texture_type: EngineType,
pub color_space: Ktx2ColorSpace,
pub premultiply_alpha: bool,
pub min_filter: MinFilter,
pub mag_filter: TextureFilter,
pub generate_mipmaps: bool,
pub normalized: bool,
pub width: u32,
pub height: u32,
pub depth: u32,
pub faces: Vec<Vec<Mipmap>>,
}
impl Ktx2Loader {
pub fn new() -> Self {
Self::default()
}
pub fn detect_support(mut self, renderer: &crate::renderer::Renderer) -> Self {
self.config = Ktx2Support::from_features(renderer.features());
self
}
pub fn with_support(mut self, support: Ktx2Support) -> Self {
self.config = support;
self
}
pub fn support(&self) -> Ktx2Support {
self.config
}
pub fn load(&self, url: impl AsRef<Path>) -> Result<Ktx2Texture, Error> {
let bytes = crate::io::read(url.as_ref())?;
self.parse(&bytes)
}
pub fn parse(&self, buffer: &[u8]) -> Result<Ktx2Texture, Error> {
let container = ktx2::Reader::new(buffer)
.map_err(|error| ktx2_error(format!("Invalid or unsupported .ktx2 file: {error}")))?;
let header = container.header();
let is_basis_hdr = header.format == Some(Format::ASTC_4x4_SFLOAT_BLOCK)
&& container.color_model().map(|model| model.value()) == Some(0xA7);
let needs_transcoder = header.format.is_none() || is_basis_hdr;
if !needs_transcoder {
return create_raw_texture(&container);
}
let transcoded = self.transcode(buffer)?;
Ok(create_texture_from(transcoded, &container))
}
fn transcode(&self, buffer: &[u8]) -> Result<Transcoded, Error> {
let file = Transcoder::new(buffer)
.map_err(|_| ktx2_error("Invalid or unsupported .ktx2 file".to_string()))?;
let basis_format = match file.source_format() {
SourceFormat::UastcLdr => BasisFormat::Uastc,
SourceFormat::Etc1s => BasisFormat::Etc1s,
SourceFormat::UastcHdr4x4 => BasisFormat::UastcHdr,
_ => return Err(ktx2_error("Unknown Basis encoding".to_string())),
};
let (width, height) = file.base_dimensions();
let layer_count = file.layer_count().max(1);
let level_count = file.level_count();
let face_count = file.face_count().max(1);
let has_alpha = file.has_alpha();
let choice = transcoder_format(&self.config, basis_format, width, height, has_alpha)?;
if width == 0 || height == 0 || level_count == 0 {
return Err(ktx2_error("Invalid texture".to_string()));
}
let flags = transcode_flags(basis_format, choice.transcoder_format);
let mut faces = Vec::with_capacity(face_count as usize);
for face in 0..face_count {
let mut mipmaps = Vec::with_capacity(level_count as usize);
for mip in 0..level_count {
let info = file
.image_level_info(mip)
.map_err(|error| ktx2_error(format!(".transcodeImage failed: {error:?}")))?;
let (mip_width, mip_height) = if level_count > 1 {
((width >> mip).max(1), (height >> mip).max(1))
} else {
let (block_width, block_height) = file.source_format().block_dims();
(
info.num_blocks_x * block_width,
info.num_blocks_y * block_height,
)
};
let mut data = Vec::new();
for layer in 0..layer_count {
let image = file
.transcode_image(mip, layer, face, choice.transcoder_format, flags)
.map_err(|error| {
ktx2_error(format!(".transcodeImage failed: {error:?}"))
})?;
data.extend_from_slice(&image);
}
mipmaps.push(Mipmap {
data,
width: mip_width,
height: mip_height,
});
}
faces.push(mipmaps);
}
Ok(Transcoded {
faces,
width,
height,
format: choice.engine_format,
texture_type: choice.engine_type,
})
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum BasisFormat {
Etc1s,
Uastc,
UastcHdr,
}
struct Transcoded {
faces: Vec<Vec<Mipmap>>,
width: u32,
height: u32,
format: EngineFormat,
texture_type: EngineType,
}
struct Choice {
transcoder_format: TargetFormat,
engine_format: EngineFormat,
engine_type: EngineType,
}
struct FormatOption {
supported: Option<fn(&Ktx2Support) -> bool>,
basis_format: &'static [BasisFormat],
transcoder_format: &'static [TargetFormat],
engine_format: &'static [EngineFormat],
engine_type: EngineType,
priority_etc1s: u32,
priority_uastc: u32,
priority_hdr: u32,
needs_power_of_two: bool,
}
const NEVER: u32 = u32::MAX;
const FORMAT_OPTIONS: &[FormatOption] = &[
FormatOption {
supported: Some(|c| c.astc),
basis_format: &[BasisFormat::Uastc],
transcoder_format: &[TargetFormat::Astc4x4Rgba, TargetFormat::Astc4x4Rgba],
engine_format: &[EngineFormat::RgbaAstc4x4, EngineFormat::RgbaAstc4x4],
engine_type: EngineType::UnsignedByte,
priority_etc1s: NEVER,
priority_uastc: 1,
priority_hdr: NEVER,
needs_power_of_two: false,
},
FormatOption {
supported: Some(|c| c.bptc),
basis_format: &[BasisFormat::Etc1s, BasisFormat::Uastc],
transcoder_format: &[TargetFormat::Bc7Rgba, TargetFormat::Bc7Rgba],
engine_format: &[EngineFormat::RgbaBptc, EngineFormat::RgbaBptc],
engine_type: EngineType::UnsignedByte,
priority_etc1s: 3,
priority_uastc: 2,
priority_hdr: NEVER,
needs_power_of_two: false,
},
FormatOption {
supported: Some(|c| c.dxt),
basis_format: &[BasisFormat::Etc1s, BasisFormat::Uastc],
transcoder_format: &[TargetFormat::Bc1Rgb, TargetFormat::Bc3Rgba],
engine_format: &[EngineFormat::RgbaS3tcDxt1, EngineFormat::RgbaS3tcDxt5],
engine_type: EngineType::UnsignedByte,
priority_etc1s: 4,
priority_uastc: 5,
priority_hdr: NEVER,
needs_power_of_two: false,
},
FormatOption {
supported: Some(|c| c.etc2),
basis_format: &[BasisFormat::Etc1s, BasisFormat::Uastc],
transcoder_format: &[TargetFormat::Etc1Rgb, TargetFormat::Etc2Rgba],
engine_format: &[EngineFormat::RgbEtc2, EngineFormat::RgbaEtc2Eac],
engine_type: EngineType::UnsignedByte,
priority_etc1s: 1,
priority_uastc: 3,
priority_hdr: NEVER,
needs_power_of_two: false,
},
FormatOption {
supported: Some(|c| c.etc1),
basis_format: &[BasisFormat::Etc1s, BasisFormat::Uastc],
transcoder_format: &[TargetFormat::Etc1Rgb],
engine_format: &[EngineFormat::RgbEtc1],
engine_type: EngineType::UnsignedByte,
priority_etc1s: 2,
priority_uastc: 4,
priority_hdr: NEVER,
needs_power_of_two: false,
},
FormatOption {
supported: Some(|c| c.pvrtc),
basis_format: &[BasisFormat::Etc1s, BasisFormat::Uastc],
transcoder_format: &[TargetFormat::Pvrtc1_4Rgb, TargetFormat::Pvrtc1_4Rgba],
engine_format: &[EngineFormat::RgbPvrtc4bppV1, EngineFormat::RgbaPvrtc4bppV1],
engine_type: EngineType::UnsignedByte,
priority_etc1s: 5,
priority_uastc: 6,
priority_hdr: NEVER,
needs_power_of_two: true,
},
FormatOption {
supported: Some(|c| c.bptc),
basis_format: &[BasisFormat::UastcHdr],
transcoder_format: &[TargetFormat::Bc6h],
engine_format: &[EngineFormat::RgbBptcUnsigned],
engine_type: EngineType::HalfFloat,
priority_etc1s: NEVER,
priority_uastc: NEVER,
priority_hdr: 1,
needs_power_of_two: false,
},
FormatOption {
supported: None,
basis_format: &[BasisFormat::Etc1s, BasisFormat::Uastc],
transcoder_format: &[TargetFormat::Rgba32, TargetFormat::Rgba32],
engine_format: &[EngineFormat::Rgba, EngineFormat::Rgba],
engine_type: EngineType::UnsignedByte,
priority_etc1s: 100,
priority_uastc: 100,
priority_hdr: NEVER,
needs_power_of_two: false,
},
FormatOption {
supported: None,
basis_format: &[BasisFormat::UastcHdr],
transcoder_format: &[TargetFormat::RgbaHalf],
engine_format: &[EngineFormat::Rgba],
engine_type: EngineType::HalfFloat,
priority_etc1s: NEVER,
priority_uastc: NEVER,
priority_hdr: 100,
needs_power_of_two: false,
},
];
fn transcoder_format(
config: &Ktx2Support,
basis_format: BasisFormat,
width: u32,
height: u32,
has_alpha: bool,
) -> Result<Choice, Error> {
let priority = |option: &FormatOption| match basis_format {
BasisFormat::Etc1s => option.priority_etc1s,
BasisFormat::Uastc => option.priority_uastc,
BasisFormat::UastcHdr => option.priority_hdr,
};
let mut options: Vec<&FormatOption> = FORMAT_OPTIONS
.iter()
.filter(|option| option.basis_format.contains(&basis_format))
.collect();
options.sort_by_key(|option| priority(option));
for option in options {
if let Some(supported) = option.supported {
if !supported(config) {
continue;
}
}
if has_alpha && option.transcoder_format.len() < 2 {
continue;
}
if option.needs_power_of_two && !(is_power_of_two(width) && is_power_of_two(height)) {
continue;
}
let index = usize::from(has_alpha);
return Ok(Choice {
transcoder_format: option.transcoder_format[index],
engine_format: option.engine_format[index],
engine_type: option.engine_type,
});
}
Err(ktx2_error(
"Failed to identify transcoding target.".to_string(),
))
}
fn is_power_of_two(value: u32) -> bool {
if value <= 2 {
return true;
}
value & (value - 1) == 0
}
fn transcode_flags(basis_format: BasisFormat, target: TargetFormat) -> DecodeFlags {
if basis_format == BasisFormat::Etc1s && target == TargetFormat::Bc7Rgba {
DecodeFlags::NO_ETC1S_CHROMA_FILTERING
} else {
DecodeFlags::NONE
}
}
fn create_texture_from(transcoded: Transcoded, container: &ktx2::Reader<&[u8]>) -> Ktx2Texture {
let header = container.header();
let single_level = transcoded.faces[0].len() == 1;
let (class, depth) = if header.face_count == 6 {
(Ktx2Class::CompressedCubeTexture, 0)
} else if header.layer_count > 1 {
(Ktx2Class::CompressedArrayTexture, header.layer_count)
} else {
(Ktx2Class::CompressedTexture, 0)
};
Ktx2Texture {
class,
format: transcoded.format,
texture_type: transcoded.texture_type,
color_space: parse_color_space(container),
premultiply_alpha: container.is_alpha_premultiplied().unwrap_or(false),
min_filter: if single_level {
MinFilter::Linear
} else {
MinFilter::LinearMipmapLinear
},
mag_filter: TextureFilter::Linear,
generate_mipmaps: false,
normalized: false,
width: transcoded.width,
height: transcoded.height,
depth,
faces: transcoded.faces,
}
}
fn raw_format(format: Format) -> Option<(EngineFormat, EngineType)> {
use EngineFormat as F;
use EngineType as T;
Some(match format {
Format::R32G32B32A32_SFLOAT => (F::Rgba, T::Float),
Format::R32G32_SFLOAT => (F::Rg, T::Float),
Format::R32_SFLOAT => (F::Red, T::Float),
Format::R16G16B16A16_SFLOAT => (F::Rgba, T::HalfFloat),
Format::R16G16_SFLOAT => (F::Rg, T::HalfFloat),
Format::R16_SFLOAT => (F::Red, T::HalfFloat),
Format::R16G16B16A16_UNORM => (F::Rgba, T::UnsignedShort),
Format::R8G8B8A8_SRGB | Format::R8G8B8A8_UNORM => (F::Rgba, T::UnsignedByte),
Format::R8G8_SRGB | Format::R8G8_UNORM => (F::Rg, T::UnsignedByte),
Format::R8_SRGB | Format::R8_UNORM => (F::Red, T::UnsignedByte),
Format::E5B9G9R9_UFLOAT_PACK32 => (F::Rgb, T::UnsignedInt5999),
Format::B10G11R11_UFLOAT_PACK32 => (F::Rgb, T::UnsignedInt101111),
Format::ETC2_R8G8B8A8_SRGB_BLOCK => (F::RgbaEtc2Eac, T::UnsignedByte),
Format::ETC2_R8G8B8_SRGB_BLOCK => (F::RgbEtc2, T::UnsignedByte),
Format::EAC_R11_UNORM_BLOCK => (F::R11Eac, T::UnsignedByte),
Format::EAC_R11_SNORM_BLOCK => (F::SignedR11Eac, T::UnsignedByte),
Format::EAC_R11G11_UNORM_BLOCK => (F::Rg11Eac, T::UnsignedByte),
Format::EAC_R11G11_SNORM_BLOCK => (F::SignedRg11Eac, T::UnsignedByte),
Format::ASTC_4x4_SFLOAT_BLOCK => (F::RgbaAstc4x4, T::HalfFloat),
Format::ASTC_4x4_SRGB_BLOCK | Format::ASTC_4x4_UNORM_BLOCK => {
(F::RgbaAstc4x4, T::UnsignedByte)
}
Format::ASTC_6x6_SFLOAT_BLOCK => (F::RgbaAstc6x6, T::HalfFloat),
Format::ASTC_6x6_SRGB_BLOCK | Format::ASTC_6x6_UNORM_BLOCK => {
(F::RgbaAstc6x6, T::UnsignedByte)
}
Format::BC1_RGBA_SRGB_BLOCK | Format::BC1_RGBA_UNORM_BLOCK => {
(F::RgbaS3tcDxt1, T::UnsignedByte)
}
Format::BC1_RGB_SRGB_BLOCK | Format::BC1_RGB_UNORM_BLOCK => {
(F::RgbS3tcDxt1, T::UnsignedByte)
}
Format::BC3_SRGB_BLOCK | Format::BC3_UNORM_BLOCK => (F::RgbaS3tcDxt5, T::UnsignedByte),
Format::BC4_SNORM_BLOCK => (F::SignedRedRgtc1, T::UnsignedByte),
Format::BC4_UNORM_BLOCK => (F::RedRgtc1, T::UnsignedByte),
Format::BC5_SNORM_BLOCK => (F::SignedRedGreenRgtc2, T::UnsignedByte),
Format::BC5_UNORM_BLOCK => (F::RedGreenRgtc2, T::UnsignedByte),
Format::BC7_SRGB_BLOCK | Format::BC7_UNORM_BLOCK => (F::RgbaBptc, T::UnsignedByte),
Format::PVRTC1_4BPP_SRGB_BLOCK | Format::PVRTC1_4BPP_UNORM_BLOCK => {
(F::RgbaPvrtc4bppV1, T::UnsignedByte)
}
Format::PVRTC1_2BPP_SRGB_BLOCK | Format::PVRTC1_2BPP_UNORM_BLOCK => {
(F::RgbaPvrtc2bppV1, T::UnsignedByte)
}
_ => return None,
})
}
fn create_raw_texture(container: &ktx2::Reader<&[u8]>) -> Result<Ktx2Texture, Error> {
let header = container.header();
let vk_format = header.format.expect("a raw texture has a vkFormat");
let (format, texture_type) = raw_format(vk_format)
.ok_or_else(|| ktx2_error(format!("Unsupported vkFormat: {}", vk_format.value())))?;
let mut mipmaps = Vec::new();
for (level_index, level) in container.levels().enumerate() {
let level_width = (header.pixel_width >> level_index).max(1);
let level_height = (header.pixel_height >> level_index).max(1);
let data = match header.supercompression_scheme {
None => level.data.to_vec(),
Some(SupercompressionScheme::Zstandard) => {
zstd_decode(level.data, level.uncompressed_byte_length as usize)?
}
Some(_) => {
return Err(ktx2_error(
"Unsupported supercompressionScheme.".to_string(),
))
}
};
mipmaps.push(Mipmap {
data,
width: level_width,
height: level_height,
});
}
let use_mipmaps = header.level_count == 0 || mipmaps.len() > 1;
let uncompressed = matches!(
format,
EngineFormat::Rgba | EngineFormat::Rgb | EngineFormat::Rg | EngineFormat::Red
);
let color_space = parse_color_space(container);
if uncompressed {
let (class, depth) = if header.pixel_depth == 0 {
(Ktx2Class::DataTexture, 0)
} else {
(Ktx2Class::Data3DTexture, header.pixel_depth)
};
return Ok(Ktx2Texture {
class,
format,
texture_type,
color_space,
premultiply_alpha: false,
min_filter: if use_mipmaps {
MinFilter::NearestMipmapNearest
} else {
MinFilter::Nearest
},
mag_filter: TextureFilter::Nearest,
generate_mipmaps: header.level_count == 0,
normalized: vk_format == Format::R16G16B16A16_UNORM,
width: header.pixel_width,
height: header.pixel_height,
depth,
faces: vec![mipmaps],
});
}
if header.pixel_depth > 0 {
return Err(ktx2_error("Unsupported pixelDepth.".to_string()));
}
let (class, faces) = if header.face_count == 6 {
let faces = (0..6)
.map(|face| {
mipmaps
.iter()
.map(|mipmap| {
let face_length = mipmap.data.len() / 6;
Mipmap {
data: mipmap.data[face * face_length..(face + 1) * face_length]
.to_vec(),
width: mipmap.width,
height: mipmap.height,
}
})
.collect()
})
.collect();
(Ktx2Class::CompressedCubeTexture, faces)
} else {
(Ktx2Class::CompressedTexture, vec![mipmaps])
};
Ok(Ktx2Texture {
class,
format,
texture_type,
color_space,
premultiply_alpha: false,
min_filter: if use_mipmaps {
MinFilter::LinearMipmapLinear
} else {
MinFilter::Linear
},
mag_filter: TextureFilter::Linear,
generate_mipmaps: false,
normalized: false,
width: header.pixel_width,
height: header.pixel_height,
depth: 0,
faces,
})
}
fn zstd_decode(compressed: &[u8], uncompressed_length: usize) -> Result<Vec<u8>, Error> {
use ruzstd::io::Read;
let mut decoder = ruzstd::StreamingDecoder::new(compressed)
.map_err(|error| ktx2_error(format!("zstd: {error}")))?;
let mut out = vec![0u8; uncompressed_length];
decoder
.read_exact(&mut out)
.map_err(|error| ktx2_error(format!("zstd: {error:?}")))?;
Ok(out)
}
fn parse_color_space(container: &ktx2::Reader<&[u8]>) -> Ktx2ColorSpace {
let srgb = container.transfer_function() == Some(TransferFunction::SRGB);
match container.color_primaries() {
Some(ColorPrimaries::BT709) => {
if srgb {
Ktx2ColorSpace::Srgb
} else {
Ktx2ColorSpace::LinearSrgb
}
}
Some(ColorPrimaries::DISPLAYP3) => {
if srgb {
Ktx2ColorSpace::DisplayP3
} else {
Ktx2ColorSpace::LinearDisplayP3
}
}
_ => Ktx2ColorSpace::NoColorSpace,
}
}
fn ktx2_error(reason: String) -> Error {
Error::Ktx2 { reason }
}
impl Ktx2Texture {
pub(crate) fn gpu_format(&self) -> Result<wgpu::TextureFormat, Error> {
use wgpu::{AstcBlock, AstcChannel, TextureFormat as G};
let srgb = self.color_space.is_srgb_transfer();
let pick = |linear: G, srgb_format: G| if srgb { srgb_format } else { linear };
let astc = |block| {
if srgb {
G::Astc {
block,
channel: AstcChannel::UnormSrgb,
}
} else {
G::Astc {
block,
channel: AstcChannel::Unorm,
}
}
};
use EngineFormat as F;
use EngineType as T;
Ok(match (self.format, self.texture_type) {
(F::Rgba, T::UnsignedByte) => pick(G::Rgba8Unorm, G::Rgba8UnormSrgb),
(F::Rgba, T::HalfFloat) => G::Rgba16Float,
(F::Rgba, T::Float) => G::Rgba32Float,
(F::Rgba, T::UnsignedShort) => G::Rgba16Unorm,
(F::Rg, T::UnsignedByte) => G::Rg8Unorm,
(F::Rg, T::HalfFloat) => G::Rg16Float,
(F::Rg, T::Float) => G::Rg32Float,
(F::Red, T::UnsignedByte) => G::R8Unorm,
(F::Red, T::HalfFloat) => G::R16Float,
(F::Red, T::Float) => G::R32Float,
(F::Rgb, T::UnsignedInt5999) => G::Rgb9e5Ufloat,
(F::Rgb, T::UnsignedInt101111) => G::Rg11b10Ufloat,
(F::RgbS3tcDxt1 | F::RgbaS3tcDxt1, _) => pick(G::Bc1RgbaUnorm, G::Bc1RgbaUnormSrgb),
(F::RgbaS3tcDxt5, _) => pick(G::Bc3RgbaUnorm, G::Bc3RgbaUnormSrgb),
(F::RedRgtc1, _) => G::Bc4RUnorm,
(F::SignedRedRgtc1, _) => G::Bc4RSnorm,
(F::RedGreenRgtc2, _) => G::Bc5RgUnorm,
(F::SignedRedGreenRgtc2, _) => G::Bc5RgSnorm,
(F::RgbaBptc, _) => pick(G::Bc7RgbaUnorm, G::Bc7RgbaUnormSrgb),
(F::RgbBptcUnsigned, _) => G::Bc6hRgbUfloat,
(F::RgbEtc1 | F::RgbEtc2, _) => pick(G::Etc2Rgb8Unorm, G::Etc2Rgb8UnormSrgb),
(F::RgbaEtc2Eac, _) => pick(G::Etc2Rgba8Unorm, G::Etc2Rgba8UnormSrgb),
(F::R11Eac, _) => G::EacR11Unorm,
(F::SignedR11Eac, _) => G::EacR11Snorm,
(F::Rg11Eac, _) => G::EacRg11Unorm,
(F::SignedRg11Eac, _) => G::EacRg11Snorm,
(F::RgbaAstc4x4, _) => astc(AstcBlock::B4x4),
(F::RgbaAstc6x6, _) => astc(AstcBlock::B6x6),
(format, texture_type) => {
return Err(Error::UnsupportedFormat {
what: "KTX2 texture format",
value: format!("{format:?} / {texture_type:?} has no WebGPU format"),
})
}
})
}
pub fn into_texture(self) -> Result<Texture, Error> {
let format = self.gpu_format()?;
let color_space = match self.color_space {
Ktx2ColorSpace::NoColorSpace => ColorSpace::NoColorSpace,
Ktx2ColorSpace::Srgb | Ktx2ColorSpace::DisplayP3 => ColorSpace::Srgb,
Ktx2ColorSpace::LinearSrgb | Ktx2ColorSpace::LinearDisplayP3 => ColorSpace::LinearSrgb,
};
let mut faces = self.faces;
let mipmaps = faces.swap_remove(0);
let texture = match self.class {
Ktx2Class::CompressedTexture | Ktx2Class::DataTexture => {
Texture::compressed(mipmaps, self.width, self.height, format)
}
Ktx2Class::CompressedArrayTexture => {
Texture::compressed_array(mipmaps, self.width, self.height, self.depth, format)
}
Ktx2Class::CompressedCubeTexture | Ktx2Class::Data3DTexture => {
return Err(Error::UnsupportedFormat {
what: "KTX2 texture class",
value: format!(
"{} (the loader reads it; the renderer has no upload path for it yet)",
self.class.name()
),
})
}
};
{
let mut inner = texture.borrow_mut_inner();
inner.color_space = color_space;
inner.min_filter = self.min_filter;
inner.mag_filter = self.mag_filter;
inner.premultiply_alpha = self.premultiply_alpha;
if self.generate_mipmaps {
let base = inner.mipmaps.remove(0);
inner.data = Some(base.data);
inner.mipmaps.clear();
inner.generate_mipmaps = true;
}
}
Ok(texture)
}
}