From c02613b77b8012847c50abc0ddc4438c0e8334bf Mon Sep 17 00:00:00 2001 From: Nate Casey Date: Tue, 13 Dec 2022 23:26:26 -0500 Subject: [PATCH 1/5] Changed Msaa to take level rather than int --- crates/bevy_core_pipeline/src/core_3d/mod.rs | 2 +- crates/bevy_pbr/src/material.rs | 4 +-- crates/bevy_pbr/src/wireframe.rs | 2 +- crates/bevy_render/src/lib.rs | 2 +- crates/bevy_render/src/view/mod.rs | 31 ++++++++++++++++---- crates/bevy_sprite/src/mesh2d/material.rs | 2 +- crates/bevy_sprite/src/render/mod.rs | 2 +- examples/2d/mesh2d_manual.rs | 2 +- examples/3d/fxaa.rs | 16 +++++----- examples/3d/msaa.rs | 10 +++---- examples/3d/transparency_3d.rs | 4 +-- examples/shader/shader_instancing.rs | 2 +- 12 files changed, 51 insertions(+), 28 deletions(-) diff --git a/crates/bevy_core_pipeline/src/core_3d/mod.rs b/crates/bevy_core_pipeline/src/core_3d/mod.rs index 8dc51d760ac1c..579ebf3482176 100644 --- a/crates/bevy_core_pipeline/src/core_3d/mod.rs +++ b/crates/bevy_core_pipeline/src/core_3d/mod.rs @@ -283,7 +283,7 @@ pub fn prepare_core_3d_depth_textures( height: physical_target_size.y, }, mip_level_count: 1, - sample_count: msaa.samples, + sample_count: msaa.level as u32, dimension: TextureDimension::D2, format: TextureFormat::Depth32Float, /* PERF: vulkan docs recommend using 24 * bit depth for better performance */ diff --git a/crates/bevy_pbr/src/material.rs b/crates/bevy_pbr/src/material.rs index c1d0fc8297325..7d9a77b5ac9b3 100644 --- a/crates/bevy_pbr/src/material.rs +++ b/crates/bevy_pbr/src/material.rs @@ -347,8 +347,8 @@ pub fn queue_material_meshes( let draw_alpha_mask_pbr = alpha_mask_draw_functions.read().id::>(); let draw_transparent_pbr = transparent_draw_functions.read().id::>(); - let mut view_key = - MeshPipelineKey::from_msaa_samples(msaa.samples) | MeshPipelineKey::from_hdr(view.hdr); + let mut view_key = MeshPipelineKey::from_msaa_samples(msaa.level as u32) + | MeshPipelineKey::from_hdr(view.hdr); if let Some(Tonemapping::Enabled { deband_dither }) = tonemapping { if !view.hdr { diff --git a/crates/bevy_pbr/src/wireframe.rs b/crates/bevy_pbr/src/wireframe.rs index 2b31a051317d2..09bc4dcb417b3 100644 --- a/crates/bevy_pbr/src/wireframe.rs +++ b/crates/bevy_pbr/src/wireframe.rs @@ -117,7 +117,7 @@ fn queue_wireframes( mut views: Query<(&ExtractedView, &VisibleEntities, &mut RenderPhase)>, ) { let draw_custom = opaque_3d_draw_functions.read().id::(); - let msaa_key = MeshPipelineKey::from_msaa_samples(msaa.samples); + let msaa_key = MeshPipelineKey::from_msaa_samples(msaa.level as u32); for (view, visible_entities, mut opaque_phase) in &mut views { let rangefinder = view.rangefinder3d(); diff --git a/crates/bevy_render/src/lib.rs b/crates/bevy_render/src/lib.rs index f595023588e76..2d816f2601921 100644 --- a/crates/bevy_render/src/lib.rs +++ b/crates/bevy_render/src/lib.rs @@ -31,7 +31,7 @@ pub mod prelude { render_resource::Shader, spatial_bundle::SpatialBundle, texture::{Image, ImagePlugin}, - view::{ComputedVisibility, Msaa, Visibility, VisibilityBundle}, + view::{ComputedVisibility, Msaa, MultiSampleLevel, Visibility, VisibilityBundle}, }; } diff --git a/crates/bevy_render/src/view/mod.rs b/crates/bevy_render/src/view/mod.rs index bc0c12b023135..23933a675a16f 100644 --- a/crates/bevy_render/src/view/mod.rs +++ b/crates/bevy_render/src/view/mod.rs @@ -61,11 +61,12 @@ impl Plugin for ViewPlugin { /// # use bevy_app::prelude::App; /// # use bevy_render::prelude::Msaa; /// App::new() -/// .insert_resource(Msaa { samples: 4 }) +/// .insert_resource(Msaa::from(MultiSampleLevel::Sample4)) /// .run(); /// ``` #[derive(Resource, Clone, ExtractResource, Reflect)] #[reflect(Resource)] +#[non_exhaustive] pub struct Msaa { /// The number of samples to run for Multi-Sample Anti-Aliasing. Higher numbers result in /// smoother edges. @@ -74,15 +75,35 @@ pub struct Msaa { /// 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: - pub samples: u32, + pub level: MultiSampleLevel, +} + +impl Msaa { + pub fn samples(&self) -> u32 { + self.level as u32 + } } impl Default for Msaa { fn default() -> Self { - Self { samples: 4 } + Self { + level: MultiSampleLevel::Sample4, + } } } +impl From for Msaa { + fn from(level: MultiSampleLevel) -> Self { + Self { level } + } +} + +#[derive(Clone, Copy, Reflect, PartialEq)] +pub enum MultiSampleLevel { + Off = 1, + Sample4 = 4, +} + #[derive(Component)] pub struct ExtractedView { pub projection: Mat4, @@ -334,7 +355,7 @@ fn prepare_view_targets( }, ) .default_view, - sampled: (msaa.samples > 1).then(|| { + sampled: (msaa.level as u32 > 1).then(|| { texture_cache .get( &render_device, @@ -342,7 +363,7 @@ fn prepare_view_targets( label: Some("main_texture_sampled"), size, mip_level_count: 1, - sample_count: msaa.samples, + sample_count: msaa.level as u32, dimension: TextureDimension::D2, format: main_texture_format, usage: TextureUsages::RENDER_ATTACHMENT, diff --git a/crates/bevy_sprite/src/mesh2d/material.rs b/crates/bevy_sprite/src/mesh2d/material.rs index 0f3b86bf9e1d5..3e0889a399480 100644 --- a/crates/bevy_sprite/src/mesh2d/material.rs +++ b/crates/bevy_sprite/src/mesh2d/material.rs @@ -322,7 +322,7 @@ pub fn queue_material2d_meshes( for (view, visible_entities, tonemapping, mut transparent_phase) in &mut views { let draw_transparent_pbr = transparent_draw_functions.read().id::>(); - let mut view_key = Mesh2dPipelineKey::from_msaa_samples(msaa.samples) + let mut view_key = Mesh2dPipelineKey::from_msaa_samples(msaa.level as u32) | Mesh2dPipelineKey::from_hdr(view.hdr); if let Some(Tonemapping::Enabled { deband_dither }) = tonemapping { diff --git a/crates/bevy_sprite/src/render/mod.rs b/crates/bevy_sprite/src/render/mod.rs index 1cdc6d93782d8..d7b41dc5cd8f4 100644 --- a/crates/bevy_sprite/src/render/mod.rs +++ b/crates/bevy_sprite/src/render/mod.rs @@ -470,7 +470,7 @@ pub fn queue_sprites( }; } - let msaa_key = SpritePipelineKey::from_msaa_samples(msaa.samples); + let msaa_key = SpritePipelineKey::from_msaa_samples(msaa.level as u32); if let Some(view_binding) = view_uniforms.uniforms.binding() { let sprite_meta = &mut sprite_meta; diff --git a/examples/2d/mesh2d_manual.rs b/examples/2d/mesh2d_manual.rs index 977095e87f9f0..1fa5df953abb2 100644 --- a/examples/2d/mesh2d_manual.rs +++ b/examples/2d/mesh2d_manual.rs @@ -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::(); - let mesh_key = Mesh2dPipelineKey::from_msaa_samples(msaa.samples) + let mesh_key = Mesh2dPipelineKey::from_msaa_samples(msaa.level as u32) | Mesh2dPipelineKey::from_hdr(view.hdr); // Queue all entities visible to that view diff --git a/examples/3d/fxaa.rs b/examples/3d/fxaa.rs index 9a51145badb35..0101a677735ef 100644 --- a/examples/3d/fxaa.rs +++ b/examples/3d/fxaa.rs @@ -1,6 +1,6 @@ //! This examples compares MSAA (Multi-Sample Anti-Aliasing) and FXAA (Fast Approximate Anti-Aliasing). -use std::f32::consts::PI; +use std::{f32::consts::PI, ops::Mul}; use bevy::{ core_pipeline::fxaa::{Fxaa, Sensitivity}, @@ -8,13 +8,14 @@ use bevy::{ render::{ render_resource::{Extent3d, SamplerDescriptor, TextureDimension, TextureFormat}, texture::ImageSampler, + view::MultiSampleLevel, }, }; fn main() { App::new() - // Disable MSAA be default - .insert_resource(Msaa { samples: 1 }) + // Disable MSAA by default + .insert_resource(Msaa::from(MultiSampleLevel::Off)) .add_plugins(DefaultPlugins) .add_startup_system(setup) .add_system(toggle_fxaa) @@ -118,19 +119,20 @@ fn toggle_fxaa(keys: Res>, 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.level = MultiSampleLevel::Sample4; info!("MSAA 4x"); } if set_no_aa { fxaa.enabled = false; - msaa.samples = 1; + msaa.level = MultiSampleLevel::Off; info!("NO AA"); } if set_no_aa | set_fxaa { - msaa.samples = 1; + msaa.level = MultiSampleLevel::Off; } if fxaa_low { fxaa.edge_threshold = Sensitivity::Low; @@ -150,7 +152,7 @@ fn toggle_fxaa(keys: Res>, mut query: Query<&mut Fxaa>, mut msaa: } if set_fxaa { fxaa.enabled = true; - msaa.samples = 1; + msaa.level = MultiSampleLevel::Off; info!("FXAA {}", fxaa.edge_threshold.get_str()); } } diff --git a/examples/3d/msaa.rs b/examples/3d/msaa.rs index b59c1ba566c6f..21e27708315aa 100644 --- a/examples/3d/msaa.rs +++ b/examples/3d/msaa.rs @@ -6,11 +6,11 @@ //! 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. -use bevy::prelude::*; +use bevy::{prelude::*, render::view::MultiSampleLevel}; fn main() { App::new() - .insert_resource(Msaa { samples: 4 }) + .insert_resource(Msaa::from(MultiSampleLevel::Off)) .add_plugins(DefaultPlugins) .add_startup_system(setup) .add_system(cycle_msaa) @@ -46,12 +46,12 @@ fn setup( fn cycle_msaa(input: Res>, mut msaa: ResMut) { if input.just_pressed(KeyCode::M) { - if msaa.samples == 4 { + if msaa.level == MultiSampleLevel::Sample4 { info!("Not using MSAA"); - msaa.samples = 1; + msaa.level = MultiSampleLevel::Off; } else { info!("Using 4x MSAA"); - msaa.samples = 4; + msaa.level = MultiSampleLevel::Sample4; } } } diff --git a/examples/3d/transparency_3d.rs b/examples/3d/transparency_3d.rs index 437c291589f2d..3f1348620dbff 100644 --- a/examples/3d/transparency_3d.rs +++ b/examples/3d/transparency_3d.rs @@ -2,11 +2,11 @@ //! Shows the effects of different blend modes. //! The `fade_transparency` system smoothly changes the transparency over time. -use bevy::prelude::*; +use bevy::{prelude::*, render::view::MultiSampleLevel}; fn main() { App::new() - .insert_resource(Msaa { samples: 4 }) + .insert_resource(Msaa::from(MultiSampleLevel::Sample4)) .add_plugins(DefaultPlugins) .add_startup_system(setup) .add_system(fade_transparency) diff --git a/examples/shader/shader_instancing.rs b/examples/shader/shader_instancing.rs index 5d8c8b816f473..fa832f7f98f77 100644 --- a/examples/shader/shader_instancing.rs +++ b/examples/shader/shader_instancing.rs @@ -111,7 +111,7 @@ fn queue_custom( ) { let draw_custom = transparent_3d_draw_functions.read().id::(); - 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); From 5f1f5be560307f3110f7bc503d1cd1d212fb52d0 Mon Sep 17 00:00:00 2001 From: Nate Casey Date: Thu, 15 Dec 2022 22:49:47 -0500 Subject: [PATCH 2/5] Moved non-exhaustive attribute to MultiSampleLevel --- crates/bevy_render/src/view/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_render/src/view/mod.rs b/crates/bevy_render/src/view/mod.rs index 23933a675a16f..c5faf7cfb9d0e 100644 --- a/crates/bevy_render/src/view/mod.rs +++ b/crates/bevy_render/src/view/mod.rs @@ -66,7 +66,6 @@ impl Plugin for ViewPlugin { /// ``` #[derive(Resource, Clone, ExtractResource, Reflect)] #[reflect(Resource)] -#[non_exhaustive] pub struct Msaa { /// The number of samples to run for Multi-Sample Anti-Aliasing. Higher numbers result in /// smoother edges. @@ -99,6 +98,7 @@ impl From for Msaa { } #[derive(Clone, Copy, Reflect, PartialEq)] +#[non_exhaustive] pub enum MultiSampleLevel { Off = 1, Sample4 = 4, From 37416518bc5d2390b3c199217cde5b7cef484397 Mon Sep 17 00:00:00 2001 From: Nate Casey Date: Thu, 15 Dec 2022 22:59:26 -0500 Subject: [PATCH 3/5] Changed some code to use .samples() --- crates/bevy_core_pipeline/src/core_3d/mod.rs | 2 +- crates/bevy_pbr/src/material.rs | 2 +- crates/bevy_pbr/src/wireframe.rs | 2 +- crates/bevy_render/src/view/mod.rs | 4 ++-- crates/bevy_sprite/src/mesh2d/material.rs | 2 +- crates/bevy_sprite/src/render/mod.rs | 2 +- examples/2d/mesh2d_manual.rs | 2 +- examples/3d/fxaa.rs | 2 +- 8 files changed, 9 insertions(+), 9 deletions(-) diff --git a/crates/bevy_core_pipeline/src/core_3d/mod.rs b/crates/bevy_core_pipeline/src/core_3d/mod.rs index 579ebf3482176..dcfed87af9afc 100644 --- a/crates/bevy_core_pipeline/src/core_3d/mod.rs +++ b/crates/bevy_core_pipeline/src/core_3d/mod.rs @@ -283,7 +283,7 @@ pub fn prepare_core_3d_depth_textures( height: physical_target_size.y, }, mip_level_count: 1, - sample_count: msaa.level as u32, + sample_count: msaa.samples(), dimension: TextureDimension::D2, format: TextureFormat::Depth32Float, /* PERF: vulkan docs recommend using 24 * bit depth for better performance */ diff --git a/crates/bevy_pbr/src/material.rs b/crates/bevy_pbr/src/material.rs index 7d9a77b5ac9b3..6f4e958a1b9d2 100644 --- a/crates/bevy_pbr/src/material.rs +++ b/crates/bevy_pbr/src/material.rs @@ -347,7 +347,7 @@ pub fn queue_material_meshes( let draw_alpha_mask_pbr = alpha_mask_draw_functions.read().id::>(); let draw_transparent_pbr = transparent_draw_functions.read().id::>(); - let mut view_key = MeshPipelineKey::from_msaa_samples(msaa.level as u32) + let mut view_key = MeshPipelineKey::from_msaa_samples(msaa.samples()) | MeshPipelineKey::from_hdr(view.hdr); if let Some(Tonemapping::Enabled { deband_dither }) = tonemapping { diff --git a/crates/bevy_pbr/src/wireframe.rs b/crates/bevy_pbr/src/wireframe.rs index 09bc4dcb417b3..dcba9232cc235 100644 --- a/crates/bevy_pbr/src/wireframe.rs +++ b/crates/bevy_pbr/src/wireframe.rs @@ -117,7 +117,7 @@ fn queue_wireframes( mut views: Query<(&ExtractedView, &VisibleEntities, &mut RenderPhase)>, ) { let draw_custom = opaque_3d_draw_functions.read().id::(); - let msaa_key = MeshPipelineKey::from_msaa_samples(msaa.level as u32); + let msaa_key = MeshPipelineKey::from_msaa_samples(msaa.samples()); for (view, visible_entities, mut opaque_phase) in &mut views { let rangefinder = view.rangefinder3d(); diff --git a/crates/bevy_render/src/view/mod.rs b/crates/bevy_render/src/view/mod.rs index c5faf7cfb9d0e..d036361aea6fb 100644 --- a/crates/bevy_render/src/view/mod.rs +++ b/crates/bevy_render/src/view/mod.rs @@ -355,7 +355,7 @@ fn prepare_view_targets( }, ) .default_view, - sampled: (msaa.level as u32 > 1).then(|| { + sampled: (msaa.samples() > 1).then(|| { texture_cache .get( &render_device, @@ -363,7 +363,7 @@ fn prepare_view_targets( label: Some("main_texture_sampled"), size, mip_level_count: 1, - sample_count: msaa.level as u32, + sample_count: msaa.samples(), dimension: TextureDimension::D2, format: main_texture_format, usage: TextureUsages::RENDER_ATTACHMENT, diff --git a/crates/bevy_sprite/src/mesh2d/material.rs b/crates/bevy_sprite/src/mesh2d/material.rs index 3e0889a399480..d94cfc7faaa55 100644 --- a/crates/bevy_sprite/src/mesh2d/material.rs +++ b/crates/bevy_sprite/src/mesh2d/material.rs @@ -322,7 +322,7 @@ pub fn queue_material2d_meshes( for (view, visible_entities, tonemapping, mut transparent_phase) in &mut views { let draw_transparent_pbr = transparent_draw_functions.read().id::>(); - let mut view_key = Mesh2dPipelineKey::from_msaa_samples(msaa.level as u32) + let mut view_key = Mesh2dPipelineKey::from_msaa_samples(msaa.samples()) | Mesh2dPipelineKey::from_hdr(view.hdr); if let Some(Tonemapping::Enabled { deband_dither }) = tonemapping { diff --git a/crates/bevy_sprite/src/render/mod.rs b/crates/bevy_sprite/src/render/mod.rs index d7b41dc5cd8f4..fd9e31927246a 100644 --- a/crates/bevy_sprite/src/render/mod.rs +++ b/crates/bevy_sprite/src/render/mod.rs @@ -470,7 +470,7 @@ pub fn queue_sprites( }; } - let msaa_key = SpritePipelineKey::from_msaa_samples(msaa.level as u32); + let msaa_key = SpritePipelineKey::from_msaa_samples(msaa.samples()); if let Some(view_binding) = view_uniforms.uniforms.binding() { let sprite_meta = &mut sprite_meta; diff --git a/examples/2d/mesh2d_manual.rs b/examples/2d/mesh2d_manual.rs index 1fa5df953abb2..2ceb5518ba38a 100644 --- a/examples/2d/mesh2d_manual.rs +++ b/examples/2d/mesh2d_manual.rs @@ -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::(); - let mesh_key = Mesh2dPipelineKey::from_msaa_samples(msaa.level as u32) + let mesh_key = Mesh2dPipelineKey::from_msaa_samples(msaa.samples()) | Mesh2dPipelineKey::from_hdr(view.hdr); // Queue all entities visible to that view diff --git a/examples/3d/fxaa.rs b/examples/3d/fxaa.rs index 0101a677735ef..940413e0d4704 100644 --- a/examples/3d/fxaa.rs +++ b/examples/3d/fxaa.rs @@ -1,6 +1,6 @@ //! This examples compares MSAA (Multi-Sample Anti-Aliasing) and FXAA (Fast Approximate Anti-Aliasing). -use std::{f32::consts::PI, ops::Mul}; +use std::f32::consts::PI; use bevy::{ core_pipeline::fxaa::{Fxaa, Sensitivity}, From 79b0d02e0ab317134066e0875cebe69e380134cf Mon Sep 17 00:00:00 2001 From: Nate Casey Date: Thu, 15 Dec 2022 23:12:06 -0500 Subject: [PATCH 4/5] check doc --- crates/bevy_render/src/view/mod.rs | 1 + examples/3d/msaa.rs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/bevy_render/src/view/mod.rs b/crates/bevy_render/src/view/mod.rs index d036361aea6fb..6d3ee8fb99a2d 100644 --- a/crates/bevy_render/src/view/mod.rs +++ b/crates/bevy_render/src/view/mod.rs @@ -60,6 +60,7 @@ impl Plugin for ViewPlugin { /// ``` /// # use bevy_app::prelude::App; /// # use bevy_render::prelude::Msaa; +/// # use bevy_render::prelude::MultiSampleLevel; /// App::new() /// .insert_resource(Msaa::from(MultiSampleLevel::Sample4)) /// .run(); diff --git a/examples/3d/msaa.rs b/examples/3d/msaa.rs index 21e27708315aa..6869cb5f7798e 100644 --- a/examples/3d/msaa.rs +++ b/examples/3d/msaa.rs @@ -6,7 +6,7 @@ //! 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. -use bevy::{prelude::*, render::view::MultiSampleLevel}; +use bevy::prelude::*; fn main() { App::new() From bd3e95562102ba7607ab3ef7339c9418dae79106 Mon Sep 17 00:00:00 2001 From: Nate Casey Date: Fri, 16 Dec 2022 10:52:53 -0500 Subject: [PATCH 5/5] Msaa::samples() now inline --- crates/bevy_render/src/view/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/bevy_render/src/view/mod.rs b/crates/bevy_render/src/view/mod.rs index 6d3ee8fb99a2d..dcacf053a6142 100644 --- a/crates/bevy_render/src/view/mod.rs +++ b/crates/bevy_render/src/view/mod.rs @@ -79,6 +79,7 @@ pub struct Msaa { } impl Msaa { + #[inline] pub fn samples(&self) -> u32 { self.level as u32 }