Skip to content

Commit

Permalink
Support MSAA sample 1.
Browse files Browse the repository at this point in the history
  • Loading branch information
cryscan committed Mar 25, 2022
1 parent 5cc9c4e commit a2ad0d7
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 85 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ bevy = { version = "0.6", default-features = false, features = [
"bevy_render",
"bevy_pbr"
] }
wgpu = { version = "0.12.0", features = ["spirv"] }
itertools = "0.10"

[dev-dependencies]
Expand Down
36 changes: 12 additions & 24 deletions src/deferred.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,14 +283,10 @@ impl render_graph::Node for DeferredPassNode {
{
let pass_descriptor = RenderPassDescriptor {
label: Some("deferred_opaque_pass"),
color_attachments: &[RenderPassColorAttachment {
view: &overlay.albedo,
resolve_target: Some(&overlay.albedo_resolve),
ops: Operations {
load: LoadOp::Clear(Color::NONE.into()),
store: true,
},
}],
color_attachments: &[overlay.albedo_color_attachment(Operations {
load: LoadOp::Clear(Color::NONE.into()),
store: true,
})],
depth_stencil_attachment: Some(RenderPassDepthStencilAttachment {
view: &overlay.albedo_depth.default_view,
depth_ops: Some(Operations {
Expand Down Expand Up @@ -320,14 +316,10 @@ impl render_graph::Node for DeferredPassNode {
{
let pass_descriptor = RenderPassDescriptor {
label: Some("deferred_alpha_mask_pass"),
color_attachments: &[RenderPassColorAttachment {
view: &overlay.albedo,
resolve_target: Some(&overlay.albedo_resolve),
ops: Operations {
load: LoadOp::Load,
store: true,
},
}],
color_attachments: &[overlay.albedo_color_attachment(Operations {
load: LoadOp::Load,
store: true,
})],
depth_stencil_attachment: Some(RenderPassDepthStencilAttachment {
view: &overlay.albedo_depth.default_view,
depth_ops: Some(Operations {
Expand Down Expand Up @@ -357,14 +349,10 @@ impl render_graph::Node for DeferredPassNode {
{
let pass_descriptor = RenderPassDescriptor {
label: Some("deferred_transparent_pass"),
color_attachments: &[RenderPassColorAttachment {
view: &overlay.albedo,
resolve_target: Some(&overlay.albedo_resolve),
ops: Operations {
load: LoadOp::Load,
store: true,
},
}],
color_attachments: &[overlay.albedo_color_attachment(Operations {
load: LoadOp::Load,
store: true,
})],
depth_stencil_attachment: Some(RenderPassDepthStencilAttachment {
view: &overlay.albedo_depth.default_view,
depth_ops: Some(Operations {
Expand Down
108 changes: 79 additions & 29 deletions src/overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,12 @@ fn setup(
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<OverlayMaterial>>,
) {
let irradiance_resolve = overlay.irradiance_resolve.clone();
let albedo_resolve = overlay.albedo_resolve.clone();
let irradiance = overlay.irradiance.clone();
let albedo = overlay.albedo.clone();

commands.spawn_bundle(MaterialMeshBundle {
mesh: meshes.add(shape::Quad::new(Vec2::ZERO).into()),
material: materials.add(OverlayMaterial {
irradiance_image: irradiance_resolve,
albedo_image: albedo_resolve,
}),
material: materials.add(OverlayMaterial { irradiance, albedo }),
..Default::default()
});
}
Expand Down Expand Up @@ -106,23 +103,28 @@ fn prepare_screen_overlay(
);

let retrieve_textures = || {
let irradiance_sampled = match &overlay.irradiance_sampled {
Some(handle) => Some(images.get(handle)?.texture_view.clone()),
None => None,
};
let irradiance = images.get(&overlay.irradiance)?.texture_view.clone();
let irradiance_resolve = images
.get(&overlay.irradiance_resolve)?
.texture_view
.clone();

let albedo_sampled = match &overlay.albedo_sampled {
Some(handle) => Some(images.get(handle)?.texture_view.clone()),
None => None,
};
let albedo = images.get(&overlay.albedo)?.texture_view.clone();
let albedo_resolve = images.get(&overlay.albedo_resolve)?.texture_view.clone();
Some((irradiance, irradiance_resolve, albedo, albedo_resolve))

Some((irradiance_sampled, irradiance, albedo_sampled, albedo))
};

if let Some((irradiance, irradiance_resolve, albedo, albedo_resolve)) = retrieve_textures() {
if let Some((irradiance_sampled, irradiance, albedo_sampled, albedo)) = retrieve_textures() {
commands.insert_resource(GpuScreenOverlay {
irradiance_sampled,
irradiance,
irradiance_resolve,
irradiance_depth,
albedo_sampled,
albedo,
albedo_resolve,
albedo_depth,
});
}
Expand All @@ -131,24 +133,64 @@ fn prepare_screen_overlay(
#[derive(Debug, Clone)]
pub struct ScreenOverlay {
pub irradiance_size: Extent3d,
pub irradiance_sampled: Option<Handle<Image>>,
pub irradiance: Handle<Image>,
pub irradiance_resolve: Handle<Image>,

pub albedo_size: Extent3d,
pub albedo_sampled: Option<Handle<Image>>,
pub albedo: Handle<Image>,
pub albedo_resolve: Handle<Image>,
}

pub struct GpuScreenOverlay {
pub irradiance: TextureView,
pub irradiance_resolve: TextureView,
pub irradiance_sampled: Option<TextureView>,
pub irradiance_depth: CachedTexture,

pub albedo: TextureView,
pub albedo_resolve: TextureView,
pub albedo_sampled: Option<TextureView>,
pub albedo_depth: CachedTexture,
}

impl GpuScreenOverlay {
pub fn irradiance_color_attachment(
&self,
ops: Operations<wgpu::Color>,
) -> RenderPassColorAttachment {
RenderPassColorAttachment {
view: if let Some(sampled) = &self.irradiance_sampled {
sampled
} else {
&self.irradiance
},
resolve_target: if self.irradiance_sampled.is_some() {
Some(&self.irradiance)
} else {
None
},
ops,
}
}

pub fn albedo_color_attachment(
&self,
ops: Operations<wgpu::Color>,
) -> RenderPassColorAttachment {
RenderPassColorAttachment {
view: if let Some(sampled) = &self.albedo_sampled {
sampled
} else {
&self.albedo
},
resolve_target: if self.albedo_sampled.is_some() {
Some(&self.albedo)
} else {
None
},
ops,
}
}
}

impl FromWorld for ScreenOverlay {
fn from_world(world: &mut World) -> Self {
let windows = world.get_resource::<Windows>().unwrap();
Expand Down Expand Up @@ -179,10 +221,14 @@ impl FromWorld for ScreenOverlay {
image.sampler_descriptor.min_filter = FilterMode::Linear;

image.texture_descriptor.sample_count = samples;
let irradiance = images.add(image.clone());
let irradiance_sampled = if samples > 1 {
Some(images.add(image.clone()))
} else {
None
};

image.texture_descriptor.sample_count = 1;
let irradiance_resolve = images.add(image);
let irradiance = images.add(image);

let albedo_size = Extent3d {
width,
Expand All @@ -200,27 +246,31 @@ impl FromWorld for ScreenOverlay {
| TextureUsages::TEXTURE_BINDING;

image.texture_descriptor.sample_count = samples;
let albedo = images.add(image.clone());
let albedo_sampled = if samples > 1 {
Some(images.add(image.clone()))
} else {
None
};

image.texture_descriptor.sample_count = 1;
let albedo_resolve = images.add(image);
let albedo = images.add(image);

Self {
irradiance_size,
irradiance_sampled,
irradiance,
irradiance_resolve,
albedo_size,
albedo_sampled,
albedo,
albedo_resolve,
}
}
}

#[derive(Debug, Clone, TypeUuid)]
#[uuid = "3eb25222-95fd-11ec-b909-0242ac120002"]
pub struct OverlayMaterial {
pub irradiance_image: Handle<Image>,
pub albedo_image: Handle<Image>,
pub irradiance: Handle<Image>,
pub albedo: Handle<Image>,
}

#[derive(Clone)]
Expand All @@ -245,13 +295,13 @@ impl RenderAsset for OverlayMaterial {
material: Self::ExtractedAsset,
(render_device, material_pipeline, images): &mut SystemParamItem<Self::Param>,
) -> Result<Self::PreparedAsset, PrepareAssetError<Self::ExtractedAsset>> {
let irradiance = if let Some(result) = images.get(&material.irradiance_image) {
let irradiance = if let Some(result) = images.get(&material.irradiance) {
result
} else {
return Err(PrepareAssetError::RetryNextUpdate(material));
};

let albedo = if let Some(result) = images.get(&material.albedo_image) {
let albedo = if let Some(result) = images.get(&material.albedo) {
result
} else {
return Err(PrepareAssetError::RetryNextUpdate(material));
Expand Down
48 changes: 16 additions & 32 deletions src/tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,14 +590,10 @@ impl render_graph::Node for TracingPassNode {
{
let pass_descriptor = RenderPassDescriptor {
label: Some("tracing_opaque_pass"),
color_attachments: &[RenderPassColorAttachment {
view: &overlay.irradiance,
resolve_target: Some(&overlay.irradiance_resolve),
ops: Operations {
load: LoadOp::Clear(Color::NONE.into()),
store: true,
},
}],
color_attachments: &[overlay.irradiance_color_attachment(Operations {
load: LoadOp::Clear(Color::NONE.into()),
store: true,
})],
depth_stencil_attachment: Some(RenderPassDepthStencilAttachment {
view: &overlay.irradiance_depth.default_view,
depth_ops: Some(Operations {
Expand Down Expand Up @@ -627,14 +623,10 @@ impl render_graph::Node for TracingPassNode {
{
let pass_descriptor = RenderPassDescriptor {
label: Some("tracing_alpha_mask_pass"),
color_attachments: &[RenderPassColorAttachment {
view: &overlay.irradiance,
resolve_target: Some(&overlay.irradiance_resolve),
ops: Operations {
load: LoadOp::Load,
store: true,
},
}],
color_attachments: &[overlay.irradiance_color_attachment(Operations {
load: LoadOp::Load,
store: true,
})],
depth_stencil_attachment: Some(RenderPassDepthStencilAttachment {
view: &overlay.irradiance_depth.default_view,
depth_ops: Some(Operations {
Expand Down Expand Up @@ -664,14 +656,10 @@ impl render_graph::Node for TracingPassNode {
{
let pass_descriptor = RenderPassDescriptor {
label: Some("tracing_transparent_pass"),
color_attachments: &[RenderPassColorAttachment {
view: &overlay.irradiance,
resolve_target: Some(&overlay.irradiance_resolve),
ops: Operations {
load: LoadOp::Load,
store: true,
},
}],
color_attachments: &[overlay.irradiance_color_attachment(Operations {
load: LoadOp::Load,
store: true,
})],
depth_stencil_attachment: Some(RenderPassDepthStencilAttachment {
view: &overlay.irradiance_depth.default_view,
depth_ops: Some(Operations {
Expand Down Expand Up @@ -701,14 +689,10 @@ impl render_graph::Node for TracingPassNode {
{
let pass_descriptor = RenderPassDescriptor {
label: Some("tracing_ambient_occlusion_pass"),
color_attachments: &[RenderPassColorAttachment {
view: &overlay.irradiance,
resolve_target: Some(&overlay.irradiance_resolve),
ops: Operations {
load: LoadOp::Load,
store: true,
},
}],
color_attachments: &[overlay.irradiance_color_attachment(Operations {
load: LoadOp::Load,
store: true,
})],
depth_stencil_attachment: Some(RenderPassDepthStencilAttachment {
view: &overlay.irradiance_depth.default_view,
depth_ops: Some(Operations {
Expand Down

0 comments on commit a2ad0d7

Please sign in to comment.