Skip to content

Commit

Permalink
feat: Basik support for ktx2
Browse files Browse the repository at this point in the history
  • Loading branch information
woelper committed Dec 21, 2023
1 parent fe725e1 commit 6a41add
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 29 deletions.
22 changes: 21 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ thiserror = "1.0.51"
bitflags = "2.4.1"
flate2 = "1.0.28"
ruzstd = "0.5.0"
byteorder = "1.5.0"
basis-universal = "0.3.1"

[features]
heif = ["libheif-rs"]
Expand Down
18 changes: 5 additions & 13 deletions src/texture/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ use super::basis::*;
use super::dds::*;
#[cfg(feature = "ktx2")]
use super::ktx2::*;
use super::ktx2_buffer_to_image;
use super::{basis::basis_buffer_to_image, ktx2_buffer_to_image};

use crate::{

};
// use bevy_math::{AspectRatio, UVec2, Vec2};
use serde::{Deserialize, Serialize};
use std::hash::Hash;
use thiserror::Error;
use wgpu::{Extent3d, TextureDimension, TextureFormat, TextureViewDescriptor};
use wgpu::{Extent3d, TextureDimension, TextureFormat};

pub const TEXTURE_ASSET_INDEX: u64 = 0;
pub const SAMPLER_ASSET_INDEX: u64 = 1;
Expand Down Expand Up @@ -95,7 +92,6 @@ impl ImageFormat {
}
}


pub struct Image {
pub data: Vec<u8>,
// TODO: this nesting makes accessing Image metadata verbose. Either flatten out descriptor or add accessors
Expand Down Expand Up @@ -128,7 +124,6 @@ impl ImageSampler {
}
}


/// How edges should be handled in texture addressing.
///
/// This type mirrors [`wgpu::AddressMode`].
Expand Down Expand Up @@ -620,7 +615,7 @@ impl Image {
image_type: ImageType,
#[allow(unused_variables)] supported_compressed_formats: CompressedImageFormats,
is_srgb: bool,
image_sampler: ImageSampler,
_image_sampler: ImageSampler,
) -> Result<Image, TextureError> {
let format = image_type.to_image_format()?;

Expand All @@ -630,8 +625,7 @@ impl Image {
// needs to be added, so the image data needs to be converted in those
// cases.

let mut image = match format {
#[cfg(feature = "basis-universal")]
let image = match format {
ImageFormat::Basis => {
basis_buffer_to_image(buffer, supported_compressed_formats, is_srgb)?
}
Expand All @@ -644,7 +638,7 @@ impl Image {
todo!()
}
};

Ok(image)
}

Expand Down Expand Up @@ -761,8 +755,6 @@ impl TextureFormatPixelInfo for TextureFormat {
}
}



bitflags::bitflags! {
#[derive(Default, Clone, Copy, Eq, PartialEq, Debug)]
#[repr(transparent)]
Expand Down
11 changes: 4 additions & 7 deletions src/texture/image_texture_conversion.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
use std::convert::TryInto;

use crate::{
image_loader::tonemap_f32,
texture::{Image, TextureFormatPixelInfo},
};
use anyhow::{bail, Context};
use image::{DynamicImage, ImageBuffer, Rgba, Rgba32FImage, RgbaImage};
use crate::texture::Image;

use image::{DynamicImage, ImageBuffer, Rgba32FImage};
use log::info;
use thiserror::Error;
use wgpu::{Extent3d, TextureDimension, TextureFormat};
use wgpu::TextureFormat;

impl Image {
/// Converts a [`DynamicImage`] to an [`Image`].
Expand Down
10 changes: 4 additions & 6 deletions src/texture/ktx2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::io::Read;


// use crate::color::SrgbColorSpace;
#[cfg(feature = "basis-universal")]
// #[cfg(feature = "basis-universal")]
use basis_universal::{
DecodeFlags, LowLevelUastcTranscoder, SliceParametersUastc, TranscoderBlockFormat,
};
Expand All @@ -12,7 +12,7 @@ use ktx2::{
Header, SampleInformation, SupercompressionScheme,
};
use wgpu::{
AstcBlock, AstcChannel, Extent3d, TextureDimension, TextureFormat, TextureViewDescriptor,
AstcBlock, AstcChannel, Extent3d, TextureDimension, TextureFormat,
TextureViewDimension,
};

Expand Down Expand Up @@ -148,7 +148,7 @@ pub fn ktx2_buffer_to_image(
TextureFormat::Rgba8Unorm
}
}
#[cfg(feature = "basis-universal")]
// #[cfg(feature = "basis-universal")]
TranscodeFormat::Uastc(data_format) => {
let (transcode_block_format, texture_format) =
get_transcoded_formats(supported_compressed_formats, data_format, is_srgb);
Expand Down Expand Up @@ -217,8 +217,6 @@ pub fn ktx2_buffer_to_image(
transcoded = levels.to_vec();
texture_format
}
#[cfg(not(feature = "basis-universal"))]
_ => return Err(error),
};
levels = transcoded;
Ok(texture_format)
Expand Down Expand Up @@ -308,7 +306,7 @@ pub fn ktx2_buffer_to_image(
Ok(image)
}

#[cfg(feature = "basis-universal")]
// #[cfg(feature = "basis-universal")]
pub fn get_transcoded_formats(
supported_compressed_formats: CompressedImageFormats,
data_format: DataFormat,
Expand Down
1 change: 0 additions & 1 deletion src/texture/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#[cfg(feature = "basis-universal")]
mod basis;
#[cfg(feature = "basis-universal")]
mod compressed_image_saver;
Expand Down

0 comments on commit 6a41add

Please sign in to comment.