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] - Include 2x/8x sample counts for Msaa #7684

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
8 changes: 4 additions & 4 deletions crates/bevy_render/src/view/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,9 @@ impl Plugin for ViewPlugin {
///
/// The number of samples to run for Multi-Sample Anti-Aliasing. Higher numbers result in
/// smoother edges.
/// Defaults to 4.
/// Defaults to 4 samples.
///
/// Note that WGPU currently only supports 1 or 4 samples.
/// Ultimately we plan on supporting whatever is natively supported on a given device.
/// Check out this issue for more info: <https://github.com/gfx-rs/wgpu/issues/1832>
/// Note that web currently only supports 1 or 4 samples.
///
/// # Example
/// ```
Expand All @@ -84,8 +82,10 @@ impl Plugin for ViewPlugin {
#[reflect(Resource)]
pub enum Msaa {
Off = 1,
Sample2 = 2,
#[default]
Sample4 = 4,
Sample8 = 8,
}

impl Msaa {
Expand Down
26 changes: 17 additions & 9 deletions crates/bevy_render/src/view/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,15 +237,23 @@ pub fn prepare_windows(
let sample_flags = render_adapter
.get_texture_format_features(surface_configuration.format)
.flags;
match *msaa {
Msaa::Off => (),
Msaa::Sample4 => {
if !sample_flags.contains(wgpu::TextureFormatFeatureFlags::MULTISAMPLE_X4) {
bevy_log::warn!(
"MSAA 4x is not supported on this device. Falling back to disabling MSAA."
);
*msaa = Msaa::Off;
}

if !sample_flags.sample_count_supported(msaa.samples()) {
if sample_flags.sample_count_supported(Msaa::default().samples()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Rather than two different log statements in and f, use the if statement to determine the fallback sample count, and then set that once and use one log statement. That will be cleaner.

Pseudo code:

let fallback_sample = if default_supported() { default } else { off };
log("Not supported, falling back to {fallback_sample}");
*msaa = fallback_sample;

bevy_log::warn!(
"MSAA {}x is not supported on this device. Falling back to MSAA {}x.",
msaa.samples(),
Msaa::default().samples(),
);

*msaa = Msaa::default();
} else {
bevy_log::warn!(
"MSAA {}x is not supported on this device. Falling back to disabling MSAA.",
msaa.samples(),
);

*msaa = Msaa::Off;
}
}

Expand Down
35 changes: 20 additions & 15 deletions examples/3d/msaa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
//! will result in smoother edges, but it will also increase the cost to render those edges. The
//! range should generally be somewhere between 1 (no multi sampling, but cheap) to 8 (crisp but
//! expensive).
//! Note that WGPU currently only supports 1 or 4 samples.
//! Ultimately we plan on supporting whatever is natively supported on a given device.
//! Check out [this issue](https://github.com/gfx-rs/wgpu/issues/1832) for more info.
//! Note that web currently only supports 1 or 4 samples.

use bevy::prelude::*;

Expand All @@ -23,7 +21,7 @@ fn setup(
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
info!("Press 'm' to toggle MSAA");
info!("Press '1/2/4/8' respectively to set MSAA sample count");
info!("Using 4x MSAA");

// cube
Expand All @@ -45,16 +43,23 @@ fn setup(
}

fn cycle_msaa(input: Res<Input<KeyCode>>, mut msaa: ResMut<Msaa>) {
if input.just_pressed(KeyCode::M) {
match *msaa {
Msaa::Sample4 => {
info!("Not using MSAA");
*msaa = Msaa::Off;
}
Msaa::Off => {
info!("Using 4x MSAA");
*msaa = Msaa::Sample4;
}
}
if input.just_pressed(KeyCode::Key1) {
info!("Not using MSAA");
*msaa = Msaa::Off;
}

if input.just_pressed(KeyCode::Key2) {
info!("Using 2x MSAA");
*msaa = Msaa::Sample2;
}

if input.just_pressed(KeyCode::Key4) {
info!("Using 4x MSAA");
*msaa = Msaa::Sample4;
}

if input.just_pressed(KeyCode::Key8) {
info!("Using 8x MSAA");
*msaa = Msaa::Sample8;
}
}