Skip to content

Commit

Permalink
Default image sampler doc fix (#5047)
Browse files Browse the repository at this point in the history
# Objective

Attempt to more clearly document `ImageSettings` and setting a default sampler for new images, as per #5046

## Changelog

- Moved ImageSettings into image.rs, image::* is already exported. Makes it simpler for linking docs. 
- Renamed "DefaultImageSampler" to "RenderDefaultImageSampler". Not a great name, but more consistent with other render resources.
- Added/updated related docs
  • Loading branch information
sarkahn committed Jun 26, 2022
1 parent 92eec47 commit 1bd33ca
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 34 deletions.
47 changes: 41 additions & 6 deletions crates/bevy_render/src/texture/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,18 @@ pub struct Image {
pub data: Vec<u8>,
// TODO: this nesting makes accessing Image metadata verbose. Either flatten out descriptor or add accessors
pub texture_descriptor: wgpu::TextureDescriptor<'static>,
/// The [`ImageSampler`] to use during rendering.
pub sampler_descriptor: ImageSampler,
}

/// Used in `Image`, this determines what image sampler to use when rendering. The default setting,
/// [`ImageSampler::Default`], will result in reading the sampler set in the [`DefaultImageSampler`]
/// resource - the global default sampler - at runtime. Setting this to [`ImageSampler::Descriptor`]
/// will override the global default descriptor for this [`Image`].
/// Used in [`Image`], this determines what image sampler to use when rendering. The default setting,
/// [`ImageSampler::Default`], will read the sampler from the [`ImageSettings`] resource at runtime.
/// Setting this to [`ImageSampler::Descriptor`] will override the global default descriptor for this [`Image`].
#[derive(Debug, Clone)]
pub enum ImageSampler {
/// Default image sampler, derived from the [`ImageSettings`] resource.
Default,
/// Custom sampler for this image which will override global default.
Descriptor(wgpu::SamplerDescriptor<'static>),
}
impl Default for ImageSampler {
Expand Down Expand Up @@ -146,8 +148,41 @@ impl ImageSampler {
}
}

/// Resource used as the global default image sampler for [`Image`]s with their `sampler_descriptor`
/// set to [`ImageSampler::Default`].
/// Global resource for [`Image`] settings.
///
/// Can be set via `insert_resource` during app initialization to change the default settings.
pub struct ImageSettings {
/// The default image sampler to use when [`ImageSampler`] is set to `Default`.
pub default_sampler: wgpu::SamplerDescriptor<'static>,
}

impl Default for ImageSettings {
fn default() -> Self {
ImageSettings::default_linear()
}
}

impl ImageSettings {
/// Creates image settings with linear sampling by default.
pub fn default_linear() -> ImageSettings {
ImageSettings {
default_sampler: ImageSampler::linear_descriptor(),
}
}

/// Creates image settings with nearest sampling by default.
pub fn default_nearest() -> ImageSettings {
ImageSettings {
default_sampler: ImageSampler::nearest_descriptor(),
}
}
}

/// A rendering resource for the default image sampler which is set during renderer
/// intialization.
///
/// The [`ImageSettings`] resource can be set during app initialization to change the default
/// image sampler.
#[derive(Debug, Clone, Deref, DerefMut)]
pub struct DefaultImageSampler(pub(crate) Sampler);

Expand Down
28 changes: 0 additions & 28 deletions crates/bevy_render/src/texture/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,34 +82,6 @@ impl Plugin for ImagePlugin {
}
}

/// [`ImagePlugin`] settings.
pub struct ImageSettings {
/// The default image sampler to use when [`ImageSampler`] is set to `Default`.
pub default_sampler: wgpu::SamplerDescriptor<'static>,
}

impl Default for ImageSettings {
fn default() -> Self {
ImageSettings::default_linear()
}
}

impl ImageSettings {
/// Creates image settings with default linear sampling.
pub fn default_linear() -> ImageSettings {
ImageSettings {
default_sampler: ImageSampler::linear_descriptor(),
}
}

/// Creates image settings with default nearest sampling.
pub fn default_nearest() -> ImageSettings {
ImageSettings {
default_sampler: ImageSampler::nearest_descriptor(),
}
}
}

pub trait BevyDefault {
fn bevy_default() -> Self;
}
Expand Down

0 comments on commit 1bd33ca

Please sign in to comment.