Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Change default Image FilterMode to Linear #4465

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 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
4 changes: 2 additions & 2 deletions crates/bevy_gltf/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use bevy_render::{
primitives::{Aabb, Frustum},
render_resource::{AddressMode, Face, FilterMode, PrimitiveTopology, SamplerDescriptor},
renderer::RenderDevice,
texture::{CompressedImageFormats, Image, ImageType, TextureError},
texture::{CompressedImageFormats, Image, ImageSampler, ImageType, TextureError},
view::VisibleEntities,
};
use bevy_scene::Scene;
Expand Down Expand Up @@ -619,7 +619,7 @@ async fn load_texture<'a>(
)?
}
};
texture.sampler_descriptor = texture_sampler(&gltf_texture);
texture.sampler_descriptor = ImageSampler::Descriptor(texture_sampler(&gltf_texture));

Ok((texture, texture_label(&gltf_texture)))
}
Expand Down
9 changes: 7 additions & 2 deletions crates/bevy_pbr/src/render/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ use bevy_render::{
render_phase::{EntityRenderCommand, RenderCommandResult, TrackedRenderPass},
render_resource::*,
renderer::{RenderDevice, RenderQueue},
texture::{BevyDefault, GpuImage, Image, TextureFormatPixelInfo},
texture::{
BevyDefault, DefaultImageSampler, GpuImage, Image, ImageSampler, TextureFormatPixelInfo,
},
view::{ComputedVisibility, ViewUniform, ViewUniformOffset, ViewUniforms},
RenderApp, RenderStage,
};
Expand Down Expand Up @@ -435,7 +437,10 @@ impl FromWorld for MeshPipeline {
TextureFormat::bevy_default(),
);
let texture = render_device.create_texture(&image.texture_descriptor);
let sampler = render_device.create_sampler(&image.sampler_descriptor);
let sampler = render_device.create_sampler(match image.sampler_descriptor {
aevyrie marked this conversation as resolved.
Show resolved Hide resolved
ImageSampler::Default => &world.resource::<DefaultImageSampler>().0,
ImageSampler::Descriptor(ref d) => d,
});

let format_size = image.texture_descriptor.format.pixel_size();
let render_queue = world.resource_mut::<RenderQueue>();
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ impl Plugin for RenderPlugin {
.insert_resource(queue)
.insert_resource(adapter_info)
.insert_resource(pipeline_cache)
.insert_resource(asset_server);
.insert_resource(asset_server)
.init_resource::<RenderGraph>();

app.add_sub_app(RenderApp, render_app, move |app_world, render_app| {
#[cfg(feature = "trace")]
Expand Down
61 changes: 56 additions & 5 deletions crates/bevy_render/src/texture/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use super::ktx2::*;

use super::image_texture_conversion::image_to_texture;
use crate::{
extract_resource::ExtractResource,
render_asset::{PrepareAssetError, RenderAsset},
render_resource::{Sampler, Texture, TextureView},
renderer::{RenderDevice, RenderQueue},
Expand Down Expand Up @@ -106,7 +107,49 @@ 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>,
pub sampler_descriptor: wgpu::SamplerDescriptor<'static>,
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`].
#[derive(Debug, Clone)]
pub enum ImageSampler {
aevyrie marked this conversation as resolved.
Show resolved Hide resolved
Default,
Descriptor(wgpu::SamplerDescriptor<'static>),
}
impl Default for ImageSampler {
fn default() -> Self {
Self::Default
}
}

/// Resource used as the global default image sampler for [`Image`]s with their `sampler_descriptor`
/// set to [`ImageSampler::Default`].
#[derive(Debug, Clone, ExtractResource)]
pub struct DefaultImageSampler(pub wgpu::SamplerDescriptor<'static>);
impl DefaultImageSampler {
pub fn linear() -> Self {
DefaultImageSampler(wgpu::SamplerDescriptor {
mag_filter: wgpu::FilterMode::Linear,
min_filter: wgpu::FilterMode::Linear,
..Default::default()
})
}
pub fn nearest() -> Self {
DefaultImageSampler(wgpu::SamplerDescriptor {
mag_filter: wgpu::FilterMode::Nearest,
min_filter: wgpu::FilterMode::Nearest,
..Default::default()
})
}
}

impl Default for DefaultImageSampler {
fn default() -> Self {
Self::linear()
}
}

impl Default for Image {
Expand All @@ -128,7 +171,7 @@ impl Default for Image {
sample_count: 1,
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
},
sampler_descriptor: wgpu::SamplerDescriptor::default(),
sampler_descriptor: ImageSampler::Default,
}
}
}
Expand Down Expand Up @@ -540,7 +583,11 @@ pub struct GpuImage {
impl RenderAsset for Image {
type ExtractedAsset = Image;
type PreparedAsset = GpuImage;
type Param = (SRes<RenderDevice>, SRes<RenderQueue>);
type Param = (
SRes<RenderDevice>,
SRes<RenderQueue>,
SRes<DefaultImageSampler>,
);

/// Clones the Image.
fn extract_asset(&self) -> Self::ExtractedAsset {
Expand All @@ -550,7 +597,7 @@ impl RenderAsset for Image {
/// Converts the extracted image into a [`GpuImage`].
fn prepare_asset(
image: Self::ExtractedAsset,
(render_device, render_queue): &mut SystemParamItem<Self::Param>,
(render_device, render_queue, default_sampler): &mut SystemParamItem<Self::Param>,
) -> Result<Self::PreparedAsset, PrepareAssetError<Self::ExtractedAsset>> {
let texture = if image.texture_descriptor.mip_level_count > 1 || image.is_compressed() {
render_device.create_texture_with_data(
Expand Down Expand Up @@ -593,7 +640,11 @@ impl RenderAsset for Image {
image.texture_descriptor.size.width as f32,
image.texture_descriptor.size.height as f32,
);
let sampler = render_device.create_sampler(&image.sampler_descriptor);
let descriptor = match image.sampler_descriptor {
ImageSampler::Default => &default_sampler.0,
ImageSampler::Descriptor(ref d) => d,
};
let sampler = render_device.create_sampler(descriptor);
Ok(GpuImage {
texture,
texture_view,
Expand Down
4 changes: 4 additions & 0 deletions crates/bevy_render/src/texture/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub use image_texture_loader::*;
pub use texture_cache::*;

use crate::{
extract_resource::ExtractResourcePlugin,
render_asset::{PrepareAssetLabel, RenderAssetPlugin},
RenderApp, RenderStage,
};
Expand Down Expand Up @@ -58,13 +59,16 @@ impl Plugin for ImagePlugin {
app.add_plugin(RenderAssetPlugin::<Image>::with_prepare_asset_label(
PrepareAssetLabel::PreAssetPrepare,
))
.init_resource::<DefaultImageSampler>()
.add_plugin(ExtractResourcePlugin::<DefaultImageSampler>::default())
.add_asset::<Image>();
app.world
.resource_mut::<Assets<Image>>()
.set_untracked(DEFAULT_IMAGE_HANDLE, Image::default());

if let Ok(render_app) = app.get_sub_app_mut(RenderApp) {
render_app
.init_resource::<DefaultImageSampler>()
.init_resource::<TextureCache>()
.add_system_to_stage(RenderStage::Cleanup, update_texture_cache_system);
}
Expand Down
9 changes: 7 additions & 2 deletions crates/bevy_sprite/src/mesh2d/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ use bevy_render::{
render_phase::{EntityRenderCommand, RenderCommandResult, TrackedRenderPass},
render_resource::*,
renderer::{RenderDevice, RenderQueue},
texture::{BevyDefault, GpuImage, Image, TextureFormatPixelInfo},
texture::{
BevyDefault, DefaultImageSampler, GpuImage, Image, ImageSampler, TextureFormatPixelInfo,
},
view::{ComputedVisibility, ExtractedView, ViewUniform, ViewUniformOffset, ViewUniforms},
RenderApp, RenderStage,
};
Expand Down Expand Up @@ -180,7 +182,10 @@ impl FromWorld for Mesh2dPipeline {
TextureFormat::bevy_default(),
);
let texture = render_device.create_texture(&image.texture_descriptor);
let sampler = render_device.create_sampler(&image.sampler_descriptor);
let sampler = render_device.create_sampler(match image.sampler_descriptor {
ImageSampler::Default => &world.resource::<DefaultImageSampler>().0,
ImageSampler::Descriptor(ref d) => d,
});

let format_size = image.texture_descriptor.format.pixel_size();
let render_queue = world.resource_mut::<RenderQueue>();
Expand Down
3 changes: 2 additions & 1 deletion examples/2d/sprite_sheet.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
//! Renders an animated sprite by loading all animation frames from a single image (a sprite sheet)
//! into a texture atlas, and changing the displayed image periodically.

use bevy::prelude::*;
use bevy::{prelude::*, render::texture::DefaultImageSampler};

fn main() {
App::new()
.add_plugins(DefaultPlugins)
.insert_resource(DefaultImageSampler::nearest()) // prevents blurry sprites
.add_startup_system(setup)
.add_system(animate_sprite)
.run();
Expand Down
3 changes: 2 additions & 1 deletion examples/2d/texture_atlas.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//! In this example we generate a new texture atlas (sprite sheet) from a folder containing
//! individual sprites.

use bevy::{asset::LoadState, prelude::*};
use bevy::{asset::LoadState, prelude::*, render::texture::DefaultImageSampler};

fn main() {
App::new()
.init_resource::<RpgSpriteHandles>()
.add_plugins(DefaultPlugins)
.insert_resource(DefaultImageSampler::nearest()) // prevents blurry sprites
.add_state(AppState::Setup)
.add_system_set(SystemSet::on_enter(AppState::Setup).with_system(load_textures))
.add_system_set(SystemSet::on_update(AppState::Setup).with_system(check_textures))
Expand Down