Changed Msaa to Enum (#7292)
# Objective Fixes #6931 Continues #6954 by squashing `Msaa` to a flat enum Helps out #7215 # Solution ``` pub enum Msaa { Off = 1, #[default] Sample4 = 4, } ``` # Changelog - Modified - `Msaa` is now enum - Defaults to 4 samples - Uses `.samples()` method to get the sample number as `u32` # Migration Guide ``` let multi = Msaa { samples: 4 } // is now let multi = Msaa::Sample4 multi.samples // is now multi.samples() ``` Co-authored-by: Sjael <jakeobrien44@gmail.com>
This commit is contained in:
parent
5d5a504685
commit
06ada2e93d
@ -303,7 +303,7 @@ pub fn prepare_core_3d_depth_textures(
|
||||
label: Some("view_depth_texture"),
|
||||
size,
|
||||
mip_level_count: 1,
|
||||
sample_count: msaa.samples,
|
||||
sample_count: msaa.samples(),
|
||||
dimension: TextureDimension::D2,
|
||||
// PERF: vulkan docs recommend using 24 bit depth for better performance
|
||||
format: TextureFormat::Depth32Float,
|
||||
|
@ -391,8 +391,8 @@ pub fn queue_material_meshes<M: Material>(
|
||||
let draw_alpha_mask_pbr = alpha_mask_draw_functions.read().id::<DrawMaterial<M>>();
|
||||
let draw_transparent_pbr = transparent_draw_functions.read().id::<DrawMaterial<M>>();
|
||||
|
||||
let mut view_key =
|
||||
MeshPipelineKey::from_msaa_samples(msaa.samples) | MeshPipelineKey::from_hdr(view.hdr);
|
||||
let mut view_key = MeshPipelineKey::from_msaa_samples(msaa.samples())
|
||||
| MeshPipelineKey::from_hdr(view.hdr);
|
||||
|
||||
if let Some(Tonemapping::Enabled { deband_dither }) = tonemapping {
|
||||
if !view.hdr {
|
||||
|
@ -395,7 +395,7 @@ pub fn prepare_prepass_textures(
|
||||
label: Some("prepass_depth_texture"),
|
||||
size,
|
||||
mip_level_count: 1,
|
||||
sample_count: msaa.samples,
|
||||
sample_count: msaa.samples(),
|
||||
dimension: TextureDimension::D2,
|
||||
format: DEPTH_PREPASS_FORMAT,
|
||||
usage: TextureUsages::COPY_DST
|
||||
@ -417,7 +417,7 @@ pub fn prepare_prepass_textures(
|
||||
label: Some("prepass_normal_texture"),
|
||||
size,
|
||||
mip_level_count: 1,
|
||||
sample_count: msaa.samples,
|
||||
sample_count: msaa.samples(),
|
||||
dimension: TextureDimension::D2,
|
||||
format: NORMAL_PREPASS_FORMAT,
|
||||
usage: TextureUsages::RENDER_ATTACHMENT
|
||||
@ -499,7 +499,7 @@ pub fn queue_prepass_material_meshes<M: Material>(
|
||||
normal_prepass,
|
||||
) in &mut views
|
||||
{
|
||||
let mut view_key = MeshPipelineKey::from_msaa_samples(msaa.samples);
|
||||
let mut view_key = MeshPipelineKey::from_msaa_samples(msaa.samples());
|
||||
if depth_prepass.is_some() {
|
||||
view_key |= MeshPipelineKey::DEPTH_PREPASS;
|
||||
}
|
||||
|
@ -877,7 +877,7 @@ pub fn queue_mesh_view_bind_groups(
|
||||
globals_buffer.buffer.binding(),
|
||||
) {
|
||||
for (entity, view_shadow_bindings, view_cluster_bindings, prepass_textures) in &views {
|
||||
let layout = if msaa.samples > 1 {
|
||||
let layout = if msaa.samples() > 1 {
|
||||
&mesh_pipeline.view_layout_multisampled
|
||||
} else {
|
||||
&mesh_pipeline.view_layout
|
||||
@ -937,7 +937,7 @@ pub fn queue_mesh_view_bind_groups(
|
||||
Some(texture) => &texture.default_view,
|
||||
None => {
|
||||
&fallback_depths
|
||||
.image_for_samplecount(msaa.samples)
|
||||
.image_for_samplecount(msaa.samples())
|
||||
.texture_view
|
||||
}
|
||||
};
|
||||
@ -950,7 +950,7 @@ pub fn queue_mesh_view_bind_groups(
|
||||
Some(texture) => &texture.default_view,
|
||||
None => {
|
||||
&fallback_images
|
||||
.image_for_samplecount(msaa.samples)
|
||||
.image_for_samplecount(msaa.samples())
|
||||
.texture_view
|
||||
}
|
||||
};
|
||||
|
@ -117,7 +117,7 @@ fn queue_wireframes(
|
||||
mut views: Query<(&ExtractedView, &VisibleEntities, &mut RenderPhase<Opaque3d>)>,
|
||||
) {
|
||||
let draw_custom = opaque_3d_draw_functions.read().id::<DrawWireframes>();
|
||||
let msaa_key = MeshPipelineKey::from_msaa_samples(msaa.samples);
|
||||
let msaa_key = MeshPipelineKey::from_msaa_samples(msaa.samples());
|
||||
for (view, visible_entities, mut opaque_phase) in &mut views {
|
||||
let rangefinder = view.rangefinder3d();
|
||||
|
||||
|
@ -56,30 +56,34 @@ impl Plugin for ViewPlugin {
|
||||
|
||||
/// Configuration resource for [Multi-Sample Anti-Aliasing](https://en.wikipedia.org/wiki/Multisample_anti-aliasing).
|
||||
///
|
||||
/// The number of samples to run for Multi-Sample Anti-Aliasing. Higher numbers result in
|
||||
/// smoother edges.
|
||||
/// Defaults to 4.
|
||||
///
|
||||
/// 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>
|
||||
///
|
||||
/// # Example
|
||||
/// ```
|
||||
/// # use bevy_app::prelude::App;
|
||||
/// # use bevy_render::prelude::Msaa;
|
||||
/// App::new()
|
||||
/// .insert_resource(Msaa { samples: 4 })
|
||||
/// .insert_resource(Msaa::default())
|
||||
/// .run();
|
||||
/// ```
|
||||
#[derive(Resource, Clone, ExtractResource, Reflect)]
|
||||
#[derive(Resource, Default, Clone, Copy, ExtractResource, Reflect, PartialEq, PartialOrd)]
|
||||
#[reflect(Resource)]
|
||||
pub struct Msaa {
|
||||
/// The number of samples to run for Multi-Sample Anti-Aliasing. Higher numbers result in
|
||||
/// smoother edges.
|
||||
/// Defaults to 4.
|
||||
///
|
||||
/// 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>
|
||||
pub samples: u32,
|
||||
pub enum Msaa {
|
||||
Off = 1,
|
||||
#[default]
|
||||
Sample4 = 4,
|
||||
}
|
||||
|
||||
impl Default for Msaa {
|
||||
fn default() -> Self {
|
||||
Self { samples: 4 }
|
||||
impl Msaa {
|
||||
#[inline]
|
||||
pub fn samples(&self) -> u32 {
|
||||
*self as u32
|
||||
}
|
||||
}
|
||||
|
||||
@ -334,7 +338,7 @@ fn prepare_view_targets(
|
||||
},
|
||||
)
|
||||
.default_view,
|
||||
sampled: (msaa.samples > 1).then(|| {
|
||||
sampled: (msaa.samples() > 1).then(|| {
|
||||
texture_cache
|
||||
.get(
|
||||
&render_device,
|
||||
@ -342,7 +346,7 @@ fn prepare_view_targets(
|
||||
label: Some("main_texture_sampled"),
|
||||
size,
|
||||
mip_level_count: 1,
|
||||
sample_count: msaa.samples,
|
||||
sample_count: msaa.samples(),
|
||||
dimension: TextureDimension::D2,
|
||||
format: main_texture_format,
|
||||
usage: TextureUsages::RENDER_ATTACHMENT,
|
||||
|
@ -340,7 +340,7 @@ pub fn queue_material2d_meshes<M: Material2d>(
|
||||
for (view, visible_entities, tonemapping, mut transparent_phase) in &mut views {
|
||||
let draw_transparent_pbr = transparent_draw_functions.read().id::<DrawMaterial2d<M>>();
|
||||
|
||||
let mut view_key = Mesh2dPipelineKey::from_msaa_samples(msaa.samples)
|
||||
let mut view_key = Mesh2dPipelineKey::from_msaa_samples(msaa.samples())
|
||||
| Mesh2dPipelineKey::from_hdr(view.hdr);
|
||||
|
||||
if let Some(Tonemapping::Enabled { deband_dither }) = tonemapping {
|
||||
|
@ -474,7 +474,7 @@ pub fn queue_sprites(
|
||||
};
|
||||
}
|
||||
|
||||
let msaa_key = SpritePipelineKey::from_msaa_samples(msaa.samples);
|
||||
let msaa_key = SpritePipelineKey::from_msaa_samples(msaa.samples());
|
||||
|
||||
if let Some(view_binding) = view_uniforms.uniforms.binding() {
|
||||
let sprite_meta = &mut sprite_meta;
|
||||
|
@ -329,7 +329,7 @@ pub fn queue_colored_mesh2d(
|
||||
for (visible_entities, mut transparent_phase, view) in &mut views {
|
||||
let draw_colored_mesh2d = transparent_draw_functions.read().id::<DrawColoredMesh2d>();
|
||||
|
||||
let mesh_key = Mesh2dPipelineKey::from_msaa_samples(msaa.samples)
|
||||
let mesh_key = Mesh2dPipelineKey::from_msaa_samples(msaa.samples())
|
||||
| Mesh2dPipelineKey::from_hdr(view.hdr);
|
||||
|
||||
// Queue all entities visible to that view
|
||||
|
@ -13,8 +13,8 @@ use bevy::{
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
// Disable MSAA be default
|
||||
.insert_resource(Msaa { samples: 1 })
|
||||
// Disable MSAA by default
|
||||
.insert_resource(Msaa::Off)
|
||||
.add_plugins(DefaultPlugins)
|
||||
.add_startup_system(setup)
|
||||
.add_system(toggle_fxaa)
|
||||
@ -118,19 +118,20 @@ fn toggle_fxaa(keys: Res<Input<KeyCode>>, mut query: Query<&mut Fxaa>, mut msaa:
|
||||
let fxaa_ultra = keys.just_pressed(KeyCode::Key9);
|
||||
let fxaa_extreme = keys.just_pressed(KeyCode::Key0);
|
||||
let set_fxaa = set_fxaa | fxaa_low | fxaa_med | fxaa_high | fxaa_ultra | fxaa_extreme;
|
||||
|
||||
for mut fxaa in &mut query {
|
||||
if set_msaa {
|
||||
fxaa.enabled = false;
|
||||
msaa.samples = 4;
|
||||
*msaa = Msaa::Sample4;
|
||||
info!("MSAA 4x");
|
||||
}
|
||||
if set_no_aa {
|
||||
fxaa.enabled = false;
|
||||
msaa.samples = 1;
|
||||
*msaa = Msaa::Off;
|
||||
info!("NO AA");
|
||||
}
|
||||
if set_no_aa | set_fxaa {
|
||||
msaa.samples = 1;
|
||||
*msaa = Msaa::Off;
|
||||
}
|
||||
if fxaa_low {
|
||||
fxaa.edge_threshold = Sensitivity::Low;
|
||||
@ -150,7 +151,7 @@ fn toggle_fxaa(keys: Res<Input<KeyCode>>, mut query: Query<&mut Fxaa>, mut msaa:
|
||||
}
|
||||
if set_fxaa {
|
||||
fxaa.enabled = true;
|
||||
msaa.samples = 1;
|
||||
*msaa = Msaa::Off;
|
||||
info!("FXAA {}", fxaa.edge_threshold.get_str());
|
||||
}
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ use bevy::prelude::*;
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.insert_resource(Msaa { samples: 4 })
|
||||
.insert_resource(Msaa::default())
|
||||
.add_plugins(DefaultPlugins)
|
||||
.add_startup_system(setup)
|
||||
.add_system(cycle_msaa)
|
||||
@ -46,12 +46,15 @@ fn setup(
|
||||
|
||||
fn cycle_msaa(input: Res<Input<KeyCode>>, mut msaa: ResMut<Msaa>) {
|
||||
if input.just_pressed(KeyCode::M) {
|
||||
if msaa.samples == 4 {
|
||||
info!("Not using MSAA");
|
||||
msaa.samples = 1;
|
||||
} else {
|
||||
info!("Using 4x MSAA");
|
||||
msaa.samples = 4;
|
||||
match *msaa {
|
||||
Msaa::Sample4 => {
|
||||
info!("Not using MSAA");
|
||||
*msaa = Msaa::Off;
|
||||
}
|
||||
Msaa::Off => {
|
||||
info!("Using 4x MSAA");
|
||||
*msaa = Msaa::Sample4;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ use bevy::prelude::*;
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.insert_resource(Msaa { samples: 4 })
|
||||
.insert_resource(Msaa::default())
|
||||
.add_plugins(DefaultPlugins)
|
||||
.add_startup_system(setup)
|
||||
.add_system(fade_transparency)
|
||||
|
@ -111,7 +111,7 @@ fn queue_custom(
|
||||
) {
|
||||
let draw_custom = transparent_3d_draw_functions.read().id::<DrawCustom>();
|
||||
|
||||
let msaa_key = MeshPipelineKey::from_msaa_samples(msaa.samples);
|
||||
let msaa_key = MeshPipelineKey::from_msaa_samples(msaa.samples());
|
||||
|
||||
for (view, mut transparent_phase) in &mut views {
|
||||
let view_key = msaa_key | MeshPipelineKey::from_hdr(view.hdr);
|
||||
|
Loading…
Reference in New Issue
Block a user