From 2e36b2719ca9fa29b41f28f4c6126d4eb84dff35 Mon Sep 17 00:00:00 2001 From: Stepan Koltsov Date: Mon, 26 Aug 2024 18:56:37 +0100 Subject: [PATCH] ImageSampler::init_descriptor (#11113) Shortcut to avoid repetition in code like https://github.com/bevyengine/bevy/pull/11109. --------- Co-authored-by: Alice Cecile --- crates/bevy_render/src/texture/image.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/crates/bevy_render/src/texture/image.rs b/crates/bevy_render/src/texture/image.rs index 4eca33b20fa62..8d92395b63c09 100644 --- a/crates/bevy_render/src/texture/image.rs +++ b/crates/bevy_render/src/texture/image.rs @@ -202,6 +202,23 @@ impl ImageSampler { pub fn nearest() -> ImageSampler { ImageSampler::Descriptor(ImageSamplerDescriptor::nearest()) } + + /// Initialize the descriptor if it is not already initialized. + /// + /// Descriptor is typically initialized by Bevy when the image is loaded, + /// so this is convenient shortcut for updating the descriptor. + pub fn get_or_init_descriptor(&mut self) -> &mut ImageSamplerDescriptor { + match self { + ImageSampler::Default => { + *self = ImageSampler::Descriptor(ImageSamplerDescriptor::default()); + match self { + ImageSampler::Descriptor(descriptor) => descriptor, + _ => unreachable!(), + } + } + ImageSampler::Descriptor(descriptor) => descriptor, + } + } } /// A rendering resource for the default image sampler which is set during renderer