2024-11-08 21:55:34 +00:00
|
|
|
//! [DirectDraw Surface](https://en.wikipedia.org/wiki/DirectDraw_Surface) functionality.
|
|
|
|
|
2024-02-11 22:00:07 +00:00
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
use bevy_utils::warn_once;
|
2023-10-21 19:10:37 +00:00
|
|
|
use ddsfile::{Caps2, D3DFormat, Dds, DxgiFormat};
|
2022-03-15 22:26:46 +00:00
|
|
|
use std::io::Cursor;
|
2024-12-03 11:46:10 +00:00
|
|
|
use wgpu::TextureViewDescriptor;
|
|
|
|
use wgpu_types::{Extent3d, TextureDimension, TextureFormat, TextureViewDimension};
|
2022-03-15 22:26:46 +00:00
|
|
|
|
|
|
|
use super::{CompressedImageFormats, Image, TextureError};
|
|
|
|
|
Add feature requirement info to image loading docs (#13712)
# Objective
- Add "Available on crate feature <image format> only." for docs of
image format related types/functions
- Add warning "WARN bevy_render::texture::image: feature "<image
format>" is not enabled" on load attempt
- Fixes #13468 .
## Solution
- Added #[cfg(feature = "<image format>")] for types and warn!("feature
\"<image format>\" is not enabled"); for ImageFormat enum conversions
## Testing
ran reproducing example from issue #13468 and saw in logs
`WARN bevy_render::texture::image: feature "exr" is not enabled`
generated docs with command `RUSTDOCFLAGS="-Zunstable-options
--cfg=docsrs" cargo +nightly doc --workspace --all-features --no-deps
--document-private-items --open` and saw
![image](https://github.com/bevyengine/bevy/assets/17225606/820262bb-b4e6-4a5e-a306-bddbe9c40852)
that docs contain `Available on crate feature <image format> only.`
marks
![image](https://github.com/bevyengine/bevy/assets/17225606/57463440-a2ea-435f-a2c2-50d34f7f55a9)
## Migration Guide
Image format related entities are feature gated, if there are
compilation errors about unknown names there are some of features in
list (`exr`, `hdr`, `basis-universal`, `png`, `dds`, `tga`, `jpeg`,
`bmp`, `ktx2`, `webp` and `pnm`) should be added.
2024-08-16 23:43:20 +00:00
|
|
|
#[cfg(feature = "dds")]
|
2022-03-15 22:26:46 +00:00
|
|
|
pub fn dds_buffer_to_image(
|
2024-02-11 22:00:07 +00:00
|
|
|
#[cfg(debug_assertions)] name: String,
|
2022-03-15 22:26:46 +00:00
|
|
|
buffer: &[u8],
|
|
|
|
supported_compressed_formats: CompressedImageFormats,
|
|
|
|
is_srgb: bool,
|
|
|
|
) -> Result<Image, TextureError> {
|
|
|
|
let mut cursor = Cursor::new(buffer);
|
2024-11-08 21:55:34 +00:00
|
|
|
let dds = Dds::read(&mut cursor)
|
|
|
|
.map_err(|error| TextureError::InvalidData(format!("Failed to parse DDS file: {error}")))?;
|
2022-03-15 22:26:46 +00:00
|
|
|
let texture_format = dds_format_to_texture_format(&dds, is_srgb)?;
|
|
|
|
if !supported_compressed_formats.supports(texture_format) {
|
|
|
|
return Err(TextureError::UnsupportedTextureFormat(format!(
|
2023-01-11 09:51:22 +00:00
|
|
|
"Format not supported by this GPU: {texture_format:?}",
|
2022-03-15 22:26:46 +00:00
|
|
|
)));
|
|
|
|
}
|
|
|
|
let mut image = Image::default();
|
2023-10-21 19:10:37 +00:00
|
|
|
let is_cubemap = dds.header.caps2.contains(Caps2::CUBEMAP);
|
2024-04-08 17:16:25 +00:00
|
|
|
let depth_or_array_layers = if dds.get_num_array_layers() > 1 {
|
2023-10-21 19:10:37 +00:00
|
|
|
dds.get_num_array_layers()
|
|
|
|
} else {
|
|
|
|
dds.get_depth()
|
|
|
|
};
|
2024-04-08 17:16:25 +00:00
|
|
|
if is_cubemap
|
|
|
|
&& !dds.header.caps2.contains(
|
2023-10-21 19:10:37 +00:00
|
|
|
Caps2::CUBEMAP_NEGATIVEX
|
|
|
|
| Caps2::CUBEMAP_NEGATIVEY
|
|
|
|
| Caps2::CUBEMAP_NEGATIVEZ
|
|
|
|
| Caps2::CUBEMAP_POSITIVEX
|
|
|
|
| Caps2::CUBEMAP_POSITIVEY
|
|
|
|
| Caps2::CUBEMAP_POSITIVEZ,
|
2024-04-08 17:16:25 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
return Err(TextureError::IncompleteCubemap);
|
2023-10-21 19:10:37 +00:00
|
|
|
}
|
2022-03-15 22:26:46 +00:00
|
|
|
image.texture_descriptor.size = Extent3d {
|
|
|
|
width: dds.get_width(),
|
|
|
|
height: dds.get_height(),
|
2023-10-21 19:10:37 +00:00
|
|
|
depth_or_array_layers,
|
2022-07-20 22:43:36 +00:00
|
|
|
}
|
|
|
|
.physical_size(texture_format);
|
2024-02-11 22:00:07 +00:00
|
|
|
let mip_map_level = match dds.get_num_mipmap_levels() {
|
|
|
|
0 => {
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
warn_once!(
|
|
|
|
"Mipmap levels for texture {} are 0, bumping them to 1",
|
|
|
|
name
|
|
|
|
);
|
|
|
|
1
|
|
|
|
}
|
|
|
|
t => t,
|
|
|
|
};
|
|
|
|
image.texture_descriptor.mip_level_count = mip_map_level;
|
2022-03-15 22:26:46 +00:00
|
|
|
image.texture_descriptor.format = texture_format;
|
|
|
|
image.texture_descriptor.dimension = if dds.get_depth() > 1 {
|
|
|
|
TextureDimension::D3
|
|
|
|
} else if image.is_compressed() || dds.get_height() > 1 {
|
|
|
|
TextureDimension::D2
|
|
|
|
} else {
|
|
|
|
TextureDimension::D1
|
|
|
|
};
|
2023-10-21 19:10:37 +00:00
|
|
|
if is_cubemap {
|
|
|
|
let dimension = if image.texture_descriptor.size.depth_or_array_layers > 6 {
|
|
|
|
TextureViewDimension::CubeArray
|
|
|
|
} else {
|
|
|
|
TextureViewDimension::Cube
|
|
|
|
};
|
|
|
|
image.texture_view_descriptor = Some(TextureViewDescriptor {
|
|
|
|
dimension: Some(dimension),
|
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
}
|
2022-03-15 22:26:46 +00:00
|
|
|
image.data = dds.data;
|
|
|
|
Ok(image)
|
|
|
|
}
|
|
|
|
|
Add feature requirement info to image loading docs (#13712)
# Objective
- Add "Available on crate feature <image format> only." for docs of
image format related types/functions
- Add warning "WARN bevy_render::texture::image: feature "<image
format>" is not enabled" on load attempt
- Fixes #13468 .
## Solution
- Added #[cfg(feature = "<image format>")] for types and warn!("feature
\"<image format>\" is not enabled"); for ImageFormat enum conversions
## Testing
ran reproducing example from issue #13468 and saw in logs
`WARN bevy_render::texture::image: feature "exr" is not enabled`
generated docs with command `RUSTDOCFLAGS="-Zunstable-options
--cfg=docsrs" cargo +nightly doc --workspace --all-features --no-deps
--document-private-items --open` and saw
![image](https://github.com/bevyengine/bevy/assets/17225606/820262bb-b4e6-4a5e-a306-bddbe9c40852)
that docs contain `Available on crate feature <image format> only.`
marks
![image](https://github.com/bevyengine/bevy/assets/17225606/57463440-a2ea-435f-a2c2-50d34f7f55a9)
## Migration Guide
Image format related entities are feature gated, if there are
compilation errors about unknown names there are some of features in
list (`exr`, `hdr`, `basis-universal`, `png`, `dds`, `tga`, `jpeg`,
`bmp`, `ktx2`, `webp` and `pnm`) should be added.
2024-08-16 23:43:20 +00:00
|
|
|
#[cfg(feature = "dds")]
|
2022-03-15 22:26:46 +00:00
|
|
|
pub fn dds_format_to_texture_format(
|
|
|
|
dds: &Dds,
|
|
|
|
is_srgb: bool,
|
|
|
|
) -> Result<TextureFormat, TextureError> {
|
|
|
|
Ok(if let Some(d3d_format) = dds.get_d3d_format() {
|
|
|
|
match d3d_format {
|
|
|
|
D3DFormat::A8B8G8R8 => {
|
|
|
|
if is_srgb {
|
|
|
|
TextureFormat::Rgba8UnormSrgb
|
|
|
|
} else {
|
|
|
|
TextureFormat::Rgba8Unorm
|
|
|
|
}
|
|
|
|
}
|
|
|
|
D3DFormat::A8 => TextureFormat::R8Unorm,
|
|
|
|
D3DFormat::A8R8G8B8 => {
|
|
|
|
if is_srgb {
|
|
|
|
TextureFormat::Bgra8UnormSrgb
|
|
|
|
} else {
|
|
|
|
TextureFormat::Bgra8Unorm
|
|
|
|
}
|
|
|
|
}
|
|
|
|
D3DFormat::G16R16 => TextureFormat::Rg16Uint,
|
|
|
|
D3DFormat::A2B10G10R10 => TextureFormat::Rgb10a2Unorm,
|
|
|
|
D3DFormat::A8L8 => TextureFormat::Rg8Uint,
|
|
|
|
D3DFormat::L16 => TextureFormat::R16Uint,
|
|
|
|
D3DFormat::L8 => TextureFormat::R8Uint,
|
|
|
|
D3DFormat::DXT1 => {
|
|
|
|
if is_srgb {
|
|
|
|
TextureFormat::Bc1RgbaUnormSrgb
|
|
|
|
} else {
|
|
|
|
TextureFormat::Bc1RgbaUnorm
|
|
|
|
}
|
|
|
|
}
|
2022-05-31 01:38:07 +00:00
|
|
|
D3DFormat::DXT3 | D3DFormat::DXT2 => {
|
2022-03-15 22:26:46 +00:00
|
|
|
if is_srgb {
|
|
|
|
TextureFormat::Bc2RgbaUnormSrgb
|
|
|
|
} else {
|
|
|
|
TextureFormat::Bc2RgbaUnorm
|
|
|
|
}
|
|
|
|
}
|
2022-05-31 01:38:07 +00:00
|
|
|
D3DFormat::DXT5 | D3DFormat::DXT4 => {
|
2022-03-15 22:26:46 +00:00
|
|
|
if is_srgb {
|
|
|
|
TextureFormat::Bc3RgbaUnormSrgb
|
|
|
|
} else {
|
|
|
|
TextureFormat::Bc3RgbaUnorm
|
|
|
|
}
|
|
|
|
}
|
2023-09-06 05:24:44 +00:00
|
|
|
D3DFormat::A16B16G16R16 => TextureFormat::Rgba16Unorm,
|
2022-03-15 22:26:46 +00:00
|
|
|
D3DFormat::Q16W16V16U16 => TextureFormat::Rgba16Sint,
|
|
|
|
D3DFormat::R16F => TextureFormat::R16Float,
|
|
|
|
D3DFormat::G16R16F => TextureFormat::Rg16Float,
|
|
|
|
D3DFormat::A16B16G16R16F => TextureFormat::Rgba16Float,
|
|
|
|
D3DFormat::R32F => TextureFormat::R32Float,
|
|
|
|
D3DFormat::G32R32F => TextureFormat::Rg32Float,
|
|
|
|
D3DFormat::A32B32G32R32F => TextureFormat::Rgba32Float,
|
|
|
|
D3DFormat::A1R5G5B5
|
|
|
|
| D3DFormat::R5G6B5
|
|
|
|
// FIXME: Map to argb format and user has to know to ignore the alpha channel?
|
|
|
|
| D3DFormat::X8R8G8B8
|
|
|
|
// FIXME: Map to argb format and user has to know to ignore the alpha channel?
|
|
|
|
| D3DFormat::X8B8G8R8
|
|
|
|
| D3DFormat::A2R10G10B10
|
|
|
|
| D3DFormat::R8G8B8
|
|
|
|
| D3DFormat::X1R5G5B5
|
|
|
|
| D3DFormat::A4R4G4B4
|
|
|
|
| D3DFormat::X4R4G4B4
|
|
|
|
| D3DFormat::A8R3G3B2
|
|
|
|
| D3DFormat::A4L4
|
|
|
|
| D3DFormat::R8G8_B8G8
|
|
|
|
| D3DFormat::G8R8_G8B8
|
|
|
|
| D3DFormat::UYVY
|
|
|
|
| D3DFormat::YUY2
|
|
|
|
| D3DFormat::CXV8U8 => {
|
|
|
|
return Err(TextureError::UnsupportedTextureFormat(format!(
|
2023-01-11 09:51:22 +00:00
|
|
|
"{d3d_format:?}",
|
2022-03-15 22:26:46 +00:00
|
|
|
)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if let Some(dxgi_format) = dds.get_dxgi_format() {
|
|
|
|
match dxgi_format {
|
2022-05-31 01:38:07 +00:00
|
|
|
DxgiFormat::R32G32B32A32_Typeless | DxgiFormat::R32G32B32A32_Float => {
|
|
|
|
TextureFormat::Rgba32Float
|
|
|
|
}
|
2022-03-15 22:26:46 +00:00
|
|
|
DxgiFormat::R32G32B32A32_UInt => TextureFormat::Rgba32Uint,
|
|
|
|
DxgiFormat::R32G32B32A32_SInt => TextureFormat::Rgba32Sint,
|
2022-05-31 01:38:07 +00:00
|
|
|
DxgiFormat::R16G16B16A16_Typeless | DxgiFormat::R16G16B16A16_Float => {
|
|
|
|
TextureFormat::Rgba16Float
|
|
|
|
}
|
2022-03-15 22:26:46 +00:00
|
|
|
DxgiFormat::R16G16B16A16_UNorm => TextureFormat::Rgba16Unorm,
|
|
|
|
DxgiFormat::R16G16B16A16_UInt => TextureFormat::Rgba16Uint,
|
|
|
|
DxgiFormat::R16G16B16A16_SNorm => TextureFormat::Rgba16Snorm,
|
|
|
|
DxgiFormat::R16G16B16A16_SInt => TextureFormat::Rgba16Sint,
|
2022-05-31 01:38:07 +00:00
|
|
|
DxgiFormat::R32G32_Typeless | DxgiFormat::R32G32_Float => TextureFormat::Rg32Float,
|
2022-03-15 22:26:46 +00:00
|
|
|
DxgiFormat::R32G32_UInt => TextureFormat::Rg32Uint,
|
|
|
|
DxgiFormat::R32G32_SInt => TextureFormat::Rg32Sint,
|
2022-05-31 01:38:07 +00:00
|
|
|
DxgiFormat::R10G10B10A2_Typeless | DxgiFormat::R10G10B10A2_UNorm => {
|
|
|
|
TextureFormat::Rgb10a2Unorm
|
2022-03-15 22:26:46 +00:00
|
|
|
}
|
2024-11-05 21:18:48 +00:00
|
|
|
DxgiFormat::R11G11B10_Float => TextureFormat::Rg11b10Ufloat,
|
2022-05-31 01:38:07 +00:00
|
|
|
DxgiFormat::R8G8B8A8_Typeless
|
|
|
|
| DxgiFormat::R8G8B8A8_UNorm
|
|
|
|
| DxgiFormat::R8G8B8A8_UNorm_sRGB => {
|
2022-03-15 22:26:46 +00:00
|
|
|
if is_srgb {
|
|
|
|
TextureFormat::Rgba8UnormSrgb
|
|
|
|
} else {
|
|
|
|
TextureFormat::Rgba8Unorm
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DxgiFormat::R8G8B8A8_UInt => TextureFormat::Rgba8Uint,
|
|
|
|
DxgiFormat::R8G8B8A8_SNorm => TextureFormat::Rgba8Snorm,
|
|
|
|
DxgiFormat::R8G8B8A8_SInt => TextureFormat::Rgba8Sint,
|
2022-05-31 01:38:07 +00:00
|
|
|
DxgiFormat::R16G16_Typeless | DxgiFormat::R16G16_Float => TextureFormat::Rg16Float,
|
2022-03-15 22:26:46 +00:00
|
|
|
DxgiFormat::R16G16_UNorm => TextureFormat::Rg16Unorm,
|
|
|
|
DxgiFormat::R16G16_UInt => TextureFormat::Rg16Uint,
|
|
|
|
DxgiFormat::R16G16_SNorm => TextureFormat::Rg16Snorm,
|
|
|
|
DxgiFormat::R16G16_SInt => TextureFormat::Rg16Sint,
|
2022-05-31 01:38:07 +00:00
|
|
|
DxgiFormat::R32_Typeless | DxgiFormat::R32_Float => TextureFormat::R32Float,
|
2022-03-15 22:26:46 +00:00
|
|
|
DxgiFormat::D32_Float => TextureFormat::Depth32Float,
|
|
|
|
DxgiFormat::R32_UInt => TextureFormat::R32Uint,
|
|
|
|
DxgiFormat::R32_SInt => TextureFormat::R32Sint,
|
2022-05-31 01:38:07 +00:00
|
|
|
DxgiFormat::R24G8_Typeless | DxgiFormat::D24_UNorm_S8_UInt => {
|
|
|
|
TextureFormat::Depth24PlusStencil8
|
|
|
|
}
|
2022-03-15 22:26:46 +00:00
|
|
|
DxgiFormat::R24_UNorm_X8_Typeless => TextureFormat::Depth24Plus,
|
2022-05-31 01:38:07 +00:00
|
|
|
DxgiFormat::R8G8_Typeless | DxgiFormat::R8G8_UNorm => TextureFormat::Rg8Unorm,
|
2022-03-15 22:26:46 +00:00
|
|
|
DxgiFormat::R8G8_UInt => TextureFormat::Rg8Uint,
|
|
|
|
DxgiFormat::R8G8_SNorm => TextureFormat::Rg8Snorm,
|
|
|
|
DxgiFormat::R8G8_SInt => TextureFormat::Rg8Sint,
|
2022-05-31 01:38:07 +00:00
|
|
|
DxgiFormat::R16_Typeless | DxgiFormat::R16_Float => TextureFormat::R16Float,
|
2022-03-15 22:26:46 +00:00
|
|
|
DxgiFormat::R16_UNorm => TextureFormat::R16Unorm,
|
|
|
|
DxgiFormat::R16_UInt => TextureFormat::R16Uint,
|
|
|
|
DxgiFormat::R16_SNorm => TextureFormat::R16Snorm,
|
|
|
|
DxgiFormat::R16_SInt => TextureFormat::R16Sint,
|
2022-05-31 01:38:07 +00:00
|
|
|
DxgiFormat::R8_Typeless | DxgiFormat::R8_UNorm => TextureFormat::R8Unorm,
|
2022-03-15 22:26:46 +00:00
|
|
|
DxgiFormat::R8_UInt => TextureFormat::R8Uint,
|
|
|
|
DxgiFormat::R8_SNorm => TextureFormat::R8Snorm,
|
|
|
|
DxgiFormat::R8_SInt => TextureFormat::R8Sint,
|
|
|
|
DxgiFormat::R9G9B9E5_SharedExp => TextureFormat::Rgb9e5Ufloat,
|
2022-05-31 01:38:07 +00:00
|
|
|
DxgiFormat::BC1_Typeless | DxgiFormat::BC1_UNorm | DxgiFormat::BC1_UNorm_sRGB => {
|
2022-03-15 22:26:46 +00:00
|
|
|
if is_srgb {
|
|
|
|
TextureFormat::Bc1RgbaUnormSrgb
|
|
|
|
} else {
|
|
|
|
TextureFormat::Bc1RgbaUnorm
|
|
|
|
}
|
|
|
|
}
|
2022-05-31 01:38:07 +00:00
|
|
|
DxgiFormat::BC2_Typeless | DxgiFormat::BC2_UNorm | DxgiFormat::BC2_UNorm_sRGB => {
|
2022-03-15 22:26:46 +00:00
|
|
|
if is_srgb {
|
|
|
|
TextureFormat::Bc2RgbaUnormSrgb
|
|
|
|
} else {
|
|
|
|
TextureFormat::Bc2RgbaUnorm
|
|
|
|
}
|
|
|
|
}
|
2022-05-31 01:38:07 +00:00
|
|
|
DxgiFormat::BC3_Typeless | DxgiFormat::BC3_UNorm | DxgiFormat::BC3_UNorm_sRGB => {
|
2022-03-15 22:26:46 +00:00
|
|
|
if is_srgb {
|
|
|
|
TextureFormat::Bc3RgbaUnormSrgb
|
|
|
|
} else {
|
|
|
|
TextureFormat::Bc3RgbaUnorm
|
|
|
|
}
|
|
|
|
}
|
2022-05-31 01:38:07 +00:00
|
|
|
DxgiFormat::BC4_Typeless | DxgiFormat::BC4_UNorm => TextureFormat::Bc4RUnorm,
|
2022-03-15 22:26:46 +00:00
|
|
|
DxgiFormat::BC4_SNorm => TextureFormat::Bc4RSnorm,
|
2022-05-31 01:38:07 +00:00
|
|
|
DxgiFormat::BC5_Typeless | DxgiFormat::BC5_UNorm => TextureFormat::Bc5RgUnorm,
|
2022-03-15 22:26:46 +00:00
|
|
|
DxgiFormat::BC5_SNorm => TextureFormat::Bc5RgSnorm,
|
2022-05-31 01:38:07 +00:00
|
|
|
DxgiFormat::B8G8R8A8_UNorm
|
|
|
|
| DxgiFormat::B8G8R8A8_Typeless
|
|
|
|
| DxgiFormat::B8G8R8A8_UNorm_sRGB => {
|
2022-03-15 22:26:46 +00:00
|
|
|
if is_srgb {
|
|
|
|
TextureFormat::Bgra8UnormSrgb
|
|
|
|
} else {
|
|
|
|
TextureFormat::Bgra8Unorm
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-31 01:38:07 +00:00
|
|
|
DxgiFormat::BC6H_Typeless | DxgiFormat::BC6H_UF16 => TextureFormat::Bc6hRgbUfloat,
|
2023-04-26 15:34:23 +00:00
|
|
|
DxgiFormat::BC6H_SF16 => TextureFormat::Bc6hRgbFloat,
|
2022-05-31 01:38:07 +00:00
|
|
|
DxgiFormat::BC7_Typeless | DxgiFormat::BC7_UNorm | DxgiFormat::BC7_UNorm_sRGB => {
|
2022-03-15 22:26:46 +00:00
|
|
|
if is_srgb {
|
|
|
|
TextureFormat::Bc7RgbaUnormSrgb
|
|
|
|
} else {
|
|
|
|
TextureFormat::Bc7RgbaUnorm
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
return Err(TextureError::UnsupportedTextureFormat(format!(
|
2023-01-11 09:51:22 +00:00
|
|
|
"{dxgi_format:?}",
|
2022-03-15 22:26:46 +00:00
|
|
|
)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Err(TextureError::UnsupportedTextureFormat(
|
|
|
|
"unspecified".to_string(),
|
|
|
|
));
|
|
|
|
})
|
|
|
|
}
|
2024-04-08 17:16:25 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2024-12-03 11:46:10 +00:00
|
|
|
use wgpu::util::TextureDataOrder;
|
|
|
|
use wgpu_types::{TextureDescriptor, TextureDimension, TextureFormat};
|
2024-04-08 17:16:25 +00:00
|
|
|
|
2024-10-04 20:16:47 +00:00
|
|
|
use crate::CompressedImageFormats;
|
2024-04-08 17:16:25 +00:00
|
|
|
|
|
|
|
use super::dds_buffer_to_image;
|
|
|
|
|
|
|
|
/// `wgpu::create_texture_with_data` that reads from data structure but doesn't actually talk to your GPU
|
2024-12-03 11:46:10 +00:00
|
|
|
fn fake_wgpu_create_texture_with_data(
|
|
|
|
desc: &TextureDescriptor<Option<&'_ str>, &'_ [TextureFormat]>,
|
|
|
|
data: &[u8],
|
|
|
|
) {
|
2024-04-08 17:16:25 +00:00
|
|
|
// Will return None only if it's a combined depth-stencil format
|
|
|
|
// If so, default to 4, validation will fail later anyway since the depth or stencil
|
|
|
|
// aspect needs to be written to individually
|
|
|
|
let block_size = desc.format.block_copy_size(None).unwrap_or(4);
|
|
|
|
let (block_width, block_height) = desc.format.block_dimensions();
|
|
|
|
let layer_iterations = desc.array_layer_count();
|
|
|
|
|
|
|
|
let outer_iteration;
|
|
|
|
let inner_iteration;
|
|
|
|
match TextureDataOrder::default() {
|
|
|
|
TextureDataOrder::LayerMajor => {
|
|
|
|
outer_iteration = layer_iterations;
|
|
|
|
inner_iteration = desc.mip_level_count;
|
|
|
|
}
|
|
|
|
TextureDataOrder::MipMajor => {
|
|
|
|
outer_iteration = desc.mip_level_count;
|
|
|
|
inner_iteration = layer_iterations;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut binary_offset = 0;
|
|
|
|
for outer in 0..outer_iteration {
|
|
|
|
for inner in 0..inner_iteration {
|
|
|
|
let (_layer, mip) = match TextureDataOrder::default() {
|
|
|
|
TextureDataOrder::LayerMajor => (outer, inner),
|
|
|
|
TextureDataOrder::MipMajor => (inner, outer),
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut mip_size = desc.mip_level_size(mip).unwrap();
|
|
|
|
// copying layers separately
|
|
|
|
if desc.dimension != TextureDimension::D3 {
|
|
|
|
mip_size.depth_or_array_layers = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// When uploading mips of compressed textures and the mip is supposed to be
|
|
|
|
// a size that isn't a multiple of the block size, the mip needs to be uploaded
|
|
|
|
// as its "physical size" which is the size rounded up to the nearest block size.
|
|
|
|
let mip_physical = mip_size.physical_size(desc.format);
|
|
|
|
|
|
|
|
// All these calculations are performed on the physical size as that's the
|
|
|
|
// data that exists in the buffer.
|
|
|
|
let width_blocks = mip_physical.width / block_width;
|
|
|
|
let height_blocks = mip_physical.height / block_height;
|
|
|
|
|
|
|
|
let bytes_per_row = width_blocks * block_size;
|
|
|
|
let data_size = bytes_per_row * height_blocks * mip_size.depth_or_array_layers;
|
|
|
|
|
|
|
|
let end_offset = binary_offset + data_size as usize;
|
|
|
|
|
|
|
|
assert!(binary_offset < data.len());
|
|
|
|
assert!(end_offset <= data.len());
|
|
|
|
// those asserts match how the data will be accessed by wgpu:
|
|
|
|
// data[binary_offset..end_offset])
|
|
|
|
|
|
|
|
binary_offset = end_offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dds_skybox() {
|
|
|
|
let buffer: [u8; 224] = [
|
|
|
|
0x44, 0x44, 0x53, 0x20, 0x7c, 0, 0, 0, 7, 0x10, 0x08, 0, 4, 0, 0, 0, 4, 0, 0, 0, 0x10,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0x47, 0x49, 0x4d, 0x50, 0x2d, 0x44, 0x44, 0x53, 0x5c,
|
|
|
|
0x09, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0x20, 0, 0, 0, 4, 0, 0, 0, 0x44, 0x58, 0x54, 0x35, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0x10, 0, 0, 0, 0xfe, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0x49, 0x92, 0x24, 0x49, 0x92, 0x24, 0xda, 0xd6,
|
|
|
|
0x2f, 0x5b, 0x8a, 0, 0xff, 0x55, 0xff, 0xff, 0x49, 0x92, 0x24, 0x49, 0x92, 0x24, 0xd5,
|
|
|
|
0x84, 0x8e, 0x3a, 0xb7, 0, 0xaa, 0x55, 0xff, 0xff, 0x49, 0x92, 0x24, 0x49, 0x92, 0x24,
|
|
|
|
0xf5, 0x94, 0x6f, 0x32, 0x57, 0xb7, 0x8b, 0, 0xff, 0xff, 0x49, 0x92, 0x24, 0x49, 0x92,
|
|
|
|
0x24, 0x2c, 0x3a, 0x49, 0x19, 0x28, 0xf7, 0xd7, 0xbe, 0xff, 0xff, 0x49, 0x92, 0x24,
|
|
|
|
0x49, 0x92, 0x24, 0x16, 0x95, 0xae, 0x42, 0xfc, 0, 0xaa, 0x55, 0xff, 0xff, 0x49, 0x92,
|
|
|
|
0x24, 0x49, 0x92, 0x24, 0xd8, 0xad, 0xae, 0x42, 0xaf, 0x0a, 0xaa, 0x55,
|
|
|
|
];
|
|
|
|
let r = dds_buffer_to_image("".into(), &buffer, CompressedImageFormats::BC, true);
|
|
|
|
assert!(r.is_ok());
|
|
|
|
if let Ok(r) = r {
|
|
|
|
fake_wgpu_create_texture_with_data(&r.texture_descriptor, &r.data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|