Skip to content

Commit

Permalink
Use ImageSamplerDescriptor in ImageLoaderSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
Kanabenki committed Oct 21, 2023
1 parent 200a44f commit 53387be
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 30 deletions.
15 changes: 9 additions & 6 deletions crates/bevy_core_pipeline/src/tonemapping/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,10 +356,7 @@ pub fn get_lut_bind_group_layout_entries(bindings: [u32; 2]) -> [BindGroupLayout
// allow(dead_code) so it doesn't complain when the tonemapping_luts feature is disabled
#[allow(dead_code)]
fn setup_tonemapping_lut_image(bytes: &[u8], image_type: ImageType) -> Image {
let mut image =
Image::from_buffer(bytes, image_type, CompressedImageFormats::NONE, false).unwrap();

image.sampler_descriptor = bevy_render::texture::ImageSampler::Descriptor(SamplerDescriptor {
let image_sampler = bevy_render::texture::ImageSampler::Descriptor(SamplerDescriptor {
label: Some("Tonemapping LUT sampler"),
address_mode_u: AddressMode::ClampToEdge,
address_mode_v: AddressMode::ClampToEdge,
Expand All @@ -369,8 +366,14 @@ fn setup_tonemapping_lut_image(bytes: &[u8], image_type: ImageType) -> Image {
mipmap_filter: FilterMode::Linear,
..default()
});

image
Image::from_buffer(
bytes,
image_type,
CompressedImageFormats::NONE,
false,
image_sampler,
)
.unwrap()
}

pub fn lut_placeholder() -> Image {
Expand Down
60 changes: 38 additions & 22 deletions crates/bevy_gltf/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ use bevy_render::{
},
prelude::SpatialBundle,
primitives::Aabb,
render_resource::{AddressMode, Face, FilterMode, PrimitiveTopology, SamplerDescriptor},
render_resource::{Face, PrimitiveTopology},
texture::{
CompressedImageFormats, Image, ImageLoaderSettings, ImageSampler, ImageType, TextureError,
CompressedImageFormats, Image, ImageAddressMode, ImageFilterMode, ImageLoaderSettings,
ImageSampler, ImageSamplerDescriptor, ImageType, TextureError,
},
};
use bevy_scene::Scene;
Expand Down Expand Up @@ -256,9 +257,14 @@ async fn load_gltf<'a, 'b, 'c>(
) {
let handle = match texture {
ImageOrPath::Image { label, image } => load_context.add_labeled_asset(label, image),
ImageOrPath::Path { path, is_srgb } => {
ImageOrPath::Path {
path,
is_srgb,
sampler_descriptor,
} => {
load_context.load_with_settings(path, move |settings: &mut ImageLoaderSettings| {
settings.is_srgb = is_srgb;
settings.sampler_descriptor = sampler_descriptor;
})
}
};
Expand Down Expand Up @@ -667,18 +673,19 @@ async fn load_image<'a, 'b>(
supported_compressed_formats: CompressedImageFormats,
) -> Result<ImageOrPath, GltfError> {
let is_srgb = !linear_textures.contains(&gltf_texture.index());
let sampler_descriptor = texture_sampler(&gltf_texture);
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 mut image = Image::from_buffer(
let image = Image::from_buffer(
buffer,
ImageType::MimeType(mime_type),
supported_compressed_formats,
is_srgb,
ImageSampler::Descriptor(sampler_descriptor.into()),
)?;
image.sampler_descriptor = ImageSampler::Descriptor(texture_sampler(&gltf_texture));
Ok(ImageOrPath::Image {
image,
label: texture_label(&gltf_texture),
Expand All @@ -698,6 +705,7 @@ async fn load_image<'a, 'b>(
mime_type.map(ImageType::MimeType).unwrap_or(image_type),
supported_compressed_formats,
is_srgb,
ImageSampler::Descriptor(sampler_descriptor.into()),
)?,
label: texture_label(&gltf_texture),
})
Expand All @@ -706,6 +714,7 @@ async fn load_image<'a, 'b>(
Ok(ImageOrPath::Path {
path: image_path,
is_srgb,
sampler_descriptor,
})
}
}
Expand Down Expand Up @@ -1110,56 +1119,56 @@ fn skin_label(skin: &gltf::Skin) -> String {
}

/// Extracts the texture sampler data from the glTF texture.
fn texture_sampler<'a>(texture: &gltf::Texture) -> SamplerDescriptor<'a> {
fn texture_sampler(texture: &gltf::Texture) -> ImageSamplerDescriptor {
let gltf_sampler = texture.sampler();

SamplerDescriptor {
ImageSamplerDescriptor {
address_mode_u: texture_address_mode(&gltf_sampler.wrap_s()),
address_mode_v: texture_address_mode(&gltf_sampler.wrap_t()),

mag_filter: gltf_sampler
.mag_filter()
.map(|mf| match mf {
MagFilter::Nearest => FilterMode::Nearest,
MagFilter::Linear => FilterMode::Linear,
MagFilter::Nearest => ImageFilterMode::Nearest,
MagFilter::Linear => ImageFilterMode::Linear,
})
.unwrap_or(SamplerDescriptor::default().mag_filter),
.unwrap_or(ImageSamplerDescriptor::default().mag_filter),

min_filter: gltf_sampler
.min_filter()
.map(|mf| match mf {
MinFilter::Nearest
| MinFilter::NearestMipmapNearest
| MinFilter::NearestMipmapLinear => FilterMode::Nearest,
| MinFilter::NearestMipmapLinear => ImageFilterMode::Nearest,
MinFilter::Linear
| MinFilter::LinearMipmapNearest
| MinFilter::LinearMipmapLinear => FilterMode::Linear,
| MinFilter::LinearMipmapLinear => ImageFilterMode::Linear,
})
.unwrap_or(SamplerDescriptor::default().min_filter),
.unwrap_or(ImageSamplerDescriptor::default().min_filter),

mipmap_filter: gltf_sampler
.min_filter()
.map(|mf| match mf {
MinFilter::Nearest
| MinFilter::Linear
| MinFilter::NearestMipmapNearest
| MinFilter::LinearMipmapNearest => FilterMode::Nearest,
| MinFilter::LinearMipmapNearest => ImageFilterMode::Nearest,
MinFilter::NearestMipmapLinear | MinFilter::LinearMipmapLinear => {
FilterMode::Linear
ImageFilterMode::Linear
}
})
.unwrap_or(SamplerDescriptor::default().mipmap_filter),
.unwrap_or(ImageSamplerDescriptor::default().mipmap_filter),

..Default::default()
}
}

/// Maps the texture address mode form glTF to wgpu.
fn texture_address_mode(gltf_address_mode: &gltf::texture::WrappingMode) -> AddressMode {
fn texture_address_mode(gltf_address_mode: &gltf::texture::WrappingMode) -> ImageAddressMode {
match gltf_address_mode {
WrappingMode::ClampToEdge => AddressMode::ClampToEdge,
WrappingMode::Repeat => AddressMode::Repeat,
WrappingMode::MirroredRepeat => AddressMode::MirrorRepeat,
WrappingMode::ClampToEdge => ImageAddressMode::ClampToEdge,
WrappingMode::Repeat => ImageAddressMode::Repeat,
WrappingMode::MirroredRepeat => ImageAddressMode::MirrorRepeat,
}
}

Expand Down Expand Up @@ -1280,8 +1289,15 @@ fn resolve_node_hierarchy(
}

enum ImageOrPath {
Image { image: Image, label: String },
Path { path: PathBuf, is_srgb: bool },
Image {
image: Image,
label: String,
},
Path {
path: PathBuf,
is_srgb: bool,
sampler_descriptor: ImageSamplerDescriptor,
},
}

struct DataUri<'a> {
Expand Down
5 changes: 4 additions & 1 deletion crates/bevy_render/src/texture/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,7 @@ impl Image {
image_type: ImageType,
#[allow(unused_variables)] supported_compressed_formats: CompressedImageFormats,
is_srgb: bool,
image_sampler: ImageSampler,
) -> Result<Image, TextureError> {
let format = image_type.to_image_format()?;

Expand Down Expand Up @@ -582,7 +583,9 @@ impl Image {
reader.set_format(image_crate_format);
reader.no_limits();
let dyn_img = reader.decode()?;
Ok(Self::from_dynamic(dyn_img, is_srgb))
let mut img = Self::from_dynamic(dyn_img, is_srgb);
img.sampler_descriptor = image_sampler;
Ok(img)
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion crates/bevy_render/src/texture/image_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
texture::{Image, ImageFormat, ImageType, TextureError},
};

use super::CompressedImageFormats;
use super::{CompressedImageFormats, ImageSampler, ImageSamplerDescriptor};
use serde::{Deserialize, Serialize};

/// Loader for images that can be read by the `image` crate.
Expand Down Expand Up @@ -56,13 +56,15 @@ pub enum ImageFormatSetting {
pub struct ImageLoaderSettings {
pub format: ImageFormatSetting,
pub is_srgb: bool,
pub sampler_descriptor: ImageSamplerDescriptor,
}

impl Default for ImageLoaderSettings {
fn default() -> Self {
Self {
format: ImageFormatSetting::default(),
is_srgb: true,
sampler_descriptor: ImageSamplerDescriptor::default(),
}
}
}
Expand Down Expand Up @@ -101,6 +103,7 @@ impl AssetLoader for ImageLoader {
image_type,
self.supported_compressed_formats,
settings.is_srgb,
ImageSampler::Descriptor(settings.sampler_descriptor.into()),
)
.map_err(|err| FileTextureError {
error: err,
Expand Down

0 comments on commit 53387be

Please sign in to comment.