Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions crates/bevy_image/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,36 @@ impl ImageSamplerDescriptor {
}
}

/// Returns this sampler descriptor with a new `ImageFilterMode` min, mag, and mipmap filters
#[inline]
Copy link
Contributor

@greeble-dev greeble-dev Dec 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[inline]

I don't think inline is appropriate here. I respect that some other functions in the struct are inline, but they're probably from the olden days when inline was more necessary.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're probably right. But out of curiosity, the compiler wouldn't inline this since it would always be used across a crate boundary right (without LTO)? So if this were being applied on a huge set of images it might be slower without the inline?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was true in the past, but since Rust 1.75 (2023/12) smaller functions are automatically tagged as cross-crate inlineable even without LTO.

So my personal preference is to let the compiler decide for these kinds of cases. But I don't think it's a big deal if the function is small and someone wants to add an inline hint to make sure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to match the rest of the file for this PR and do an inline removal on all of these in a future PR instead.

pub fn set_filter(&mut self, filter: ImageFilterMode) -> &mut Self {
self.mag_filter = filter;
self.min_filter = filter;
self.mipmap_filter = filter;
self
}

/// Returns this sampler descriptor with a new `ImageAddressMode` for u, v, and w
#[inline]
pub fn set_address_mode(&mut self, address_mode: ImageAddressMode) -> &mut Self {
self.address_mode_u = address_mode;
self.address_mode_v = address_mode;
self.address_mode_w = address_mode;
self
}

/// Returns this sampler descriptor with an `anisotropy_clamp` value and also
/// set filters to `ImageFilterMode::Linear`, which is required to
/// use anisotropy.
#[inline]
pub fn set_anisotropic_filter(&mut self, anisotropy_clamp: u16) -> &mut Self {
self.mag_filter = ImageFilterMode::Linear;
self.min_filter = ImageFilterMode::Linear;
self.mipmap_filter = ImageFilterMode::Linear;
self.anisotropy_clamp = anisotropy_clamp;
self
}

pub fn as_wgpu(&self) -> SamplerDescriptor<Option<&str>> {
SamplerDescriptor {
label: self.label.as_deref(),
Expand Down Expand Up @@ -2212,4 +2242,34 @@ mod test {

assert!(image.data.as_ref().unwrap().iter().all(|&p| p == 255));
}

#[test]
fn get_or_init_sampler_modifications() {
// given some sampler
let mut default_sampler = ImageSampler::Default;
// a load_with_settings call wants to customize the descriptor
let my_sampler_in_a_loader = default_sampler
.get_or_init_descriptor()
.set_filter(ImageFilterMode::Linear)
.set_address_mode(ImageAddressMode::Repeat);

assert_eq!(
my_sampler_in_a_loader.address_mode_u,
ImageAddressMode::Repeat
);
assert_eq!(my_sampler_in_a_loader.min_filter, ImageFilterMode::Linear);
}

#[test]
fn get_or_init_sampler_anisotropy() {
// given some sampler
let mut default_sampler = ImageSampler::Default;
// a load_with_settings call wants to customize the descriptor
let my_sampler_in_a_loader = default_sampler
.get_or_init_descriptor()
.set_anisotropic_filter(8);

assert_eq!(my_sampler_in_a_loader.min_filter, ImageFilterMode::Linear);
assert_eq!(my_sampler_in_a_loader.anisotropy_clamp, 8);
}
}