Skip to content

Commit

Permalink
mipmap levels can be 0 and they should be interpreted as 1 (#11767)
Browse files Browse the repository at this point in the history
# 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
  • Loading branch information
anarelion authored Feb 11, 2024
1 parent 61e01e4 commit 94ab84e
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 3 deletions.
1 change: 1 addition & 0 deletions crates/bevy_core_pipeline/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ license = "MIT OR Apache-2.0"
keywords = ["bevy"]

[features]
dds = []
trace = []
webgl = []
webgpu = []
Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_core_pipeline/src/tonemapping/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_gltf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ license = "MIT OR Apache-2.0"
keywords = ["bevy"]

[features]
dds = []
pbr_transmission_textures = []

[dependencies]
Expand Down
8 changes: 8 additions & 0 deletions crates/bevy_gltf/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_internal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion crates/bevy_render/src/texture/dds.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[cfg(debug_assertions)]
use bevy_utils::warn_once;
use ddsfile::{Caps2, D3DFormat, Dds, DxgiFormat};
use std::io::Cursor;
use wgpu::{
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion crates/bevy_render/src/texture/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)?
Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_render/src/texture/image_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 94ab84e

Please sign in to comment.