Skip to content

Commit

Permalink
Fix MSAA after core pipeline plugin was extracted from bevy render2
Browse files Browse the repository at this point in the history
  • Loading branch information
superdump committed Aug 21, 2021
1 parent 451bf94 commit cff1017
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 112 deletions.
186 changes: 114 additions & 72 deletions pipelined/bevy_core_pipeline/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ use bevy_render2::{
render_graph::{EmptyNode, RenderGraph, RenderGraphError, SlotInfo, SlotType},
render_phase::{sort_phase_system, RenderPhase},
render_resource::{
Extent3d, Msaa, Texture, TextureDescriptor, TextureDimension, TextureFormat, TextureUsage,
TextureView,
Extent3d, Msaa, Texture, TextureAspect, TextureDescriptor, TextureDimension, TextureFormat,
TextureUsage, TextureView, TextureViewDescriptor, TextureViewDimension,
},
renderer::RenderDevice,
texture::TextureCache,
view::{ExtractedView, ViewPlugin},
texture::{BevyDefault, TextureCache},
view::{ExtractedMsaa, ExtractedView, ExtractedWindows, ViewPlugin},
RenderStage, RenderWorld,
};

Expand Down Expand Up @@ -81,6 +81,7 @@ impl Plugin for CorePipelinePlugin {
render_app
.add_system_to_stage(RenderStage::Extract, extract_clear_color)
.add_system_to_stage(RenderStage::Extract, extract_core_pipeline_camera_phases)
.add_system_to_stage(RenderStage::Prepare, prepare_windows_msaa)
.add_system_to_stage(RenderStage::Prepare, prepare_core_views_system)
.add_system_to_stage(
RenderStage::PhaseSort,
Expand Down Expand Up @@ -157,6 +158,115 @@ impl Plugin for CorePipelinePlugin {
}
}

pub struct Transparent3dPhase;
pub struct Transparent2dPhase;

pub struct ViewDepthTexture {
pub texture: Texture,
pub view: TextureView,
}

pub fn extract_clear_color(clear_color: Res<ClearColor>, mut render_world: ResMut<RenderWorld>) {
// If the clear color has changed
if clear_color.is_changed() {
// Update the clear color resource in the render world
render_world.insert_resource(clear_color.clone())
}
}

pub fn extract_core_pipeline_camera_phases(
mut commands: Commands,
active_cameras: Res<ActiveCameras>,
) {
if let Some(camera_2d) = active_cameras.get(CameraPlugin::CAMERA_2D) {
if let Some(entity) = camera_2d.entity {
commands
.get_or_spawn(entity)
.insert(RenderPhase::<Transparent2dPhase>::default());
}
}
if let Some(camera_3d) = active_cameras.get(CameraPlugin::CAMERA_3D) {
if let Some(entity) = camera_3d.entity {
commands
.get_or_spawn(entity)
.insert(RenderPhase::<Transparent3dPhase>::default());
}
}
}

pub fn prepare_core_views_system(
mut commands: Commands,
mut texture_cache: ResMut<TextureCache>,
msaa: Res<ExtractedMsaa>,
render_device: Res<RenderDevice>,
views: Query<(Entity, &ExtractedView), With<RenderPhase<Transparent3dPhase>>>,
) {
for (entity, view) in views.iter() {
let cached_texture = texture_cache.get(
&render_device,
TextureDescriptor {
label: None,
size: Extent3d {
depth_or_array_layers: 1,
width: view.width as u32,
height: view.height as u32,
},
mip_level_count: 1,
sample_count: msaa.samples,
dimension: TextureDimension::D2,
format: TextureFormat::Depth32Float, /* PERF: vulkan docs recommend using 24
* bit depth for better performance */
usage: TextureUsage::RENDER_ATTACHMENT,
},
);
commands.entity(entity).insert(ViewDepthTexture {
texture: cached_texture.texture,
view: cached_texture.default_view,
});
}
}

pub fn prepare_windows_msaa(
msaa: Res<ExtractedMsaa>,
mut windows: ResMut<ExtractedWindows>,
render_device: Res<RenderDevice>,
mut render_graph: ResMut<RenderGraph>,
) {
for window in windows.windows.values_mut() {
if msaa.is_added() || msaa.is_changed() {
if msaa.samples > 1 {
let texture = render_device.create_texture(&TextureDescriptor {
label: Some("sampled_color_attachment_texture"),
size: Extent3d {
width: window.physical_width,
height: window.physical_height,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: msaa.samples,
dimension: TextureDimension::D2,
format: TextureFormat::bevy_default(),
usage: TextureUsage::RENDER_ATTACHMENT,
});
let texture_view = texture.create_view(&TextureViewDescriptor {
label: Some("sampled_color_attachment"),
format: None,
dimension: Some(TextureViewDimension::D2),
aspect: TextureAspect::All,
base_mip_level: 0,
mip_level_count: None,
base_array_layer: 0,
array_layer_count: None,
});
window.sampled_color_attachment_texture = Some(texture);
window.sampled_color_attachment = Some(texture_view);
}

configure_graph_msaa(msaa.samples, &mut *render_graph);
}
}
}

pub fn configure_graph_msaa(msaa_samples: u32, render_graph: &mut RenderGraph) {
if msaa_samples > 1 {
// Scope to drop the mutable borrow
Expand Down Expand Up @@ -348,71 +458,3 @@ pub fn configure_graph_msaa(msaa_samples: u32, render_graph: &mut RenderGraph) {
}
}
}

pub struct Transparent3dPhase;
pub struct Transparent2dPhase;

pub struct ViewDepthTexture {
pub texture: Texture,
pub view: TextureView,
}

pub fn extract_clear_color(clear_color: Res<ClearColor>, mut render_world: ResMut<RenderWorld>) {
// If the clear color has changed
if clear_color.is_changed() {
// Update the clear color resource in the render world
render_world.insert_resource(clear_color.clone())
}
}

pub fn extract_core_pipeline_camera_phases(
mut commands: Commands,
active_cameras: Res<ActiveCameras>,
) {
if let Some(camera_2d) = active_cameras.get(CameraPlugin::CAMERA_2D) {
if let Some(entity) = camera_2d.entity {
commands
.get_or_spawn(entity)
.insert(RenderPhase::<Transparent2dPhase>::default());
}
}
if let Some(camera_3d) = active_cameras.get(CameraPlugin::CAMERA_3D) {
if let Some(entity) = camera_3d.entity {
commands
.get_or_spawn(entity)
.insert(RenderPhase::<Transparent3dPhase>::default());
}
}
}

pub fn prepare_core_views_system(
mut commands: Commands,
mut texture_cache: ResMut<TextureCache>,
msaa: Res<ExtractedMsaa>,
render_device: Res<RenderDevice>,
views: Query<(Entity, &ExtractedView), With<RenderPhase<Transparent3dPhase>>>,
) {
for (entity, view) in views.iter() {
let cached_texture = texture_cache.get(
&render_device,
TextureDescriptor {
label: None,
size: Extent3d {
depth_or_array_layers: 1,
width: view.width as u32,
height: view.height as u32,
},
mip_level_count: 1,
sample_count: msaa.samples,
dimension: TextureDimension::D2,
format: TextureFormat::Depth32Float, /* PERF: vulkan docs recommend using 24
* bit depth for better performance */
usage: TextureUsage::RENDER_ATTACHMENT,
},
);
commands.entity(entity).insert(ViewDepthTexture {
texture: cached_texture.texture,
view: cached_texture.default_view,
});
}
}
41 changes: 1 addition & 40 deletions pipelined/bevy_render2/src/view/window.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::{
core_pipeline::configure_graph_msaa,
render_graph::RenderGraph,
render_resource::{Msaa, Texture, TextureView},
renderer::{RenderDevice, RenderInstance},
texture::BevyDefault,
Expand All @@ -11,10 +9,7 @@ use bevy_ecs::prelude::*;
use bevy_utils::HashMap;
use bevy_window::{RawWindowHandleWrapper, WindowId, Windows};
use std::ops::{Deref, DerefMut};
use wgpu::{
Extent3d, TextureAspect, TextureDescriptor, TextureDimension, TextureFormat, TextureUsage,
TextureViewDescriptor, TextureViewDimension,
};
use wgpu::TextureFormat;

// Token to ensure a system runs on the main thread.
#[derive(Default)]
Expand Down Expand Up @@ -104,10 +99,8 @@ pub fn prepare_windows(
_marker: NonSend<NonSendMarker>,
mut windows: ResMut<ExtractedWindows>,
mut window_surfaces: ResMut<WindowSurfaces>,
msaa: Res<ExtractedMsaa>,
render_device: Res<RenderDevice>,
render_instance: Res<RenderInstance>,
mut render_graph: ResMut<RenderGraph>,
) {
let window_surfaces = window_surfaces.deref_mut();
for window in windows.windows.values_mut() {
Expand Down Expand Up @@ -153,37 +146,5 @@ pub fn prepare_windows(
};

window.swap_chain_frame = Some(TextureView::from(frame));

if msaa.is_added() || msaa.is_changed() {
if msaa.samples > 1 {
let texture = render_device.create_texture(&TextureDescriptor {
label: Some("sampled_color_attachment_texture"),
size: Extent3d {
width: window.physical_width,
height: window.physical_height,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: msaa.samples,
dimension: TextureDimension::D2,
format: TextureFormat::bevy_default(),
usage: TextureUsage::RENDER_ATTACHMENT,
});
let texture_view = texture.create_view(&TextureViewDescriptor {
label: Some("sampled_color_attachment"),
format: None,
dimension: Some(TextureViewDimension::D2),
aspect: TextureAspect::All,
base_mip_level: 0,
mip_level_count: None,
base_array_layer: 0,
array_layer_count: None,
});
window.sampled_color_attachment_texture = Some(texture);
window.sampled_color_attachment = Some(texture_view);
}

configure_graph_msaa(msaa.samples, &mut *render_graph);
}
}
}

0 comments on commit cff1017

Please sign in to comment.