mipmap levels can be 0 and they should be interpreted as 1 (#11767)

# Objective

Loading some textures from the days of yonder give me errors cause the
mipmap level is 0

## Solution

Set a minimum of 1

## Changelog

Make mipmap level at least 1
This commit is contained in:
anarelion 2024-02-11 23:00:07 +01:00 committed by GitHub
parent 61e01e46b5
commit 94ab84e915
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 38 additions and 3 deletions

View file

@ -13,6 +13,7 @@ license = "MIT OR Apache-2.0"
keywords = ["bevy"]
[features]
dds = []
trace = []
webgl = []
webgpu = []

View file

@ -355,6 +355,8 @@ fn setup_tonemapping_lut_image(bytes: &[u8], image_type: ImageType) -> Image {
..default()
});
Image::from_buffer(
#[cfg(all(debug_assertions, feature = "dds"))]
"Tonemapping LUT sampler".to_string(),
bytes,
image_type,
CompressedImageFormats::NONE,

View file

@ -9,6 +9,7 @@ license = "MIT OR Apache-2.0"
keywords = ["bevy"]
[features]
dds = []
pbr_transmission_textures = []
[dependencies]

View file

@ -733,12 +733,18 @@ async fn load_image<'a, 'b>(
) -> Result<ImageOrPath, GltfError> {
let is_srgb = !linear_textures.contains(&gltf_texture.index());
let sampler_descriptor = texture_sampler(&gltf_texture);
#[cfg(all(debug_assertions, feature = "dds"))]
let name = gltf_texture
.name()
.map_or("Unknown GLTF Texture".to_string(), |s| s.to_string());
match gltf_texture.source().source() {
gltf::image::Source::View { view, mime_type } => {
let start = view.offset();
let end = view.offset() + view.length();
let buffer = &buffer_data[view.buffer().index()][start..end];
let image = Image::from_buffer(
#[cfg(all(debug_assertions, feature = "dds"))]
name,
buffer,
ImageType::MimeType(mime_type),
supported_compressed_formats,
@ -761,6 +767,8 @@ async fn load_image<'a, 'b>(
let image_type = ImageType::MimeType(data_uri.mime_type);
Ok(ImageOrPath::Image {
image: Image::from_buffer(
#[cfg(all(debug_assertions, feature = "dds"))]
name,
&bytes,
mime_type.map(ImageType::MimeType).unwrap_or(image_type),
supported_compressed_formats,

View file

@ -34,7 +34,7 @@ jpeg = ["bevy_render/jpeg"]
bmp = ["bevy_render/bmp"]
webp = ["bevy_render/webp"]
basis-universal = ["bevy_render/basis-universal"]
dds = ["bevy_render/dds"]
dds = ["bevy_render/dds", "bevy_core_pipeline/dds", "bevy_gltf/dds"]
pnm = ["bevy_render/pnm"]
ktx2 = ["bevy_render/ktx2"]
# For ktx2 supercompression

View file

@ -1,3 +1,5 @@
#[cfg(debug_assertions)]
use bevy_utils::warn_once;
use ddsfile::{Caps2, D3DFormat, Dds, DxgiFormat};
use std::io::Cursor;
use wgpu::{
@ -7,6 +9,7 @@ use wgpu::{
use super::{CompressedImageFormats, Image, TextureError};
pub fn dds_buffer_to_image(
#[cfg(debug_assertions)] name: String,
buffer: &[u8],
supported_compressed_formats: CompressedImageFormats,
is_srgb: bool,
@ -45,7 +48,18 @@ pub fn dds_buffer_to_image(
depth_or_array_layers,
}
.physical_size(texture_format);
image.texture_descriptor.mip_level_count = dds.get_num_mipmap_levels();
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;
image.texture_descriptor.format = texture_format;
image.texture_descriptor.dimension = if dds.get_depth() > 1 {
TextureDimension::D3

View file

@ -643,6 +643,7 @@ impl Image {
/// Load a bytes buffer in a [`Image`], according to type `image_type`, using the `image`
/// crate
pub fn from_buffer(
#[cfg(all(debug_assertions, feature = "dds"))] name: String,
buffer: &[u8],
image_type: ImageType,
#[allow(unused_variables)] supported_compressed_formats: CompressedImageFormats,
@ -664,7 +665,13 @@ impl Image {
basis_buffer_to_image(buffer, supported_compressed_formats, is_srgb)?
}
#[cfg(feature = "dds")]
ImageFormat::Dds => dds_buffer_to_image(buffer, supported_compressed_formats, is_srgb)?,
ImageFormat::Dds => dds_buffer_to_image(
#[cfg(debug_assertions)]
name,
buffer,
supported_compressed_formats,
is_srgb,
)?,
#[cfg(feature = "ktx2")]
ImageFormat::Ktx2 => {
ktx2_buffer_to_image(buffer, supported_compressed_formats, is_srgb)?

View file

@ -102,6 +102,8 @@ impl AssetLoader for ImageLoader {
ImageFormatSetting::Format(format) => ImageType::Format(format),
};
Ok(Image::from_buffer(
#[cfg(all(debug_assertions, feature = "dds"))]
load_context.path().display().to_string(),
&bytes,
image_type,
self.supported_compressed_formats,