Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/cryscan/bevy-hikari into main
Browse files Browse the repository at this point in the history
  • Loading branch information
cryscan committed Mar 19, 2022
2 parents 04e45e3 + 3cbd5a3 commit 1e0ea63
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 69 deletions.
31 changes: 15 additions & 16 deletions src/deferred.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{overlay::ScreenOverlay, VolumeBindings, ALBEDO_SHADER_HANDLE};
use crate::{overlay::GpuScreenOverlay, ALBEDO_SHADER_HANDLE};
use bevy::{
core_pipeline::{AlphaMask3d, Opaque3d, Transparent3d},
pbr::{
Expand Down Expand Up @@ -238,7 +238,6 @@ pub struct DeferredPassNode {
&'static RenderPhase<Deferred<Opaque3d>>,
&'static RenderPhase<Deferred<AlphaMask3d>>,
&'static RenderPhase<Deferred<Transparent3d>>,
&'static VolumeBindings,
),
With<ExtractedView>,
>,
Expand Down Expand Up @@ -270,30 +269,30 @@ impl render_graph::Node for DeferredPassNode {
world: &World,
) -> Result<(), render_graph::NodeRunError> {
let view_entity = graph.get_input_entity(Self::IN_VIEW)?;
let (opaque_phase, alpha_mask_phase, transparent_phase, bindings) =
let (opaque_phase, alpha_mask_phase, transparent_phase) =
match self.query.get_manual(world, view_entity) {
Ok(query) => query,
Err(_) => return Ok(()),
};

let overlay = world.get_resource::<ScreenOverlay>().unwrap();
let images = world.get_resource::<RenderAssets<Image>>().unwrap();
let view = &images[&overlay.albedo].texture_view;
let resolve_target = &images[&overlay.albedo_resolve].texture_view;
let overlay = match world.get_resource::<GpuScreenOverlay>() {
Some(overlay) => overlay,
None => return Ok(()),
};

{
let pass_descriptor = RenderPassDescriptor {
label: Some("deferred_opaque_pass"),
color_attachments: &[RenderPassColorAttachment {
view,
resolve_target: Some(resolve_target),
view: &overlay.albedo,
resolve_target: Some(&overlay.albedo_resolve),
ops: Operations {
load: LoadOp::Clear(Color::NONE.into()),
store: true,
},
}],
depth_stencil_attachment: Some(RenderPassDepthStencilAttachment {
view: &bindings.albedo_depth_texture.default_view,
view: &overlay.albedo_depth.default_view,
depth_ops: Some(Operations {
load: LoadOp::Clear(0.0),
store: true,
Expand Down Expand Up @@ -322,15 +321,15 @@ impl render_graph::Node for DeferredPassNode {
let pass_descriptor = RenderPassDescriptor {
label: Some("deferred_alpha_mask_pass"),
color_attachments: &[RenderPassColorAttachment {
view,
resolve_target: Some(resolve_target),
view: &overlay.albedo,
resolve_target: Some(&overlay.albedo_resolve),
ops: Operations {
load: LoadOp::Load,
store: true,
},
}],
depth_stencil_attachment: Some(RenderPassDepthStencilAttachment {
view: &bindings.albedo_depth_texture.default_view,
view: &overlay.albedo_depth.default_view,
depth_ops: Some(Operations {
load: LoadOp::Load,
store: true,
Expand Down Expand Up @@ -359,15 +358,15 @@ impl render_graph::Node for DeferredPassNode {
let pass_descriptor = RenderPassDescriptor {
label: Some("deferred_transparent_pass"),
color_attachments: &[RenderPassColorAttachment {
view,
resolve_target: Some(resolve_target),
view: &overlay.albedo,
resolve_target: Some(&overlay.albedo_resolve),
ops: Operations {
load: LoadOp::Load,
store: true,
},
}],
depth_stencil_attachment: Some(RenderPassDepthStencilAttachment {
view: &bindings.albedo_depth_texture.default_view,
view: &overlay.albedo_depth.default_view,
depth_ops: Some(Operations {
load: LoadOp::Load,
store: false,
Expand Down
32 changes: 0 additions & 32 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,6 @@ pub struct VolumeColorAttachment {

#[derive(Component)]
pub struct VolumeBindings {
pub irradiance_depth_texture: CachedTexture,
pub albedo_depth_texture: CachedTexture,
pub voxel_texture: CachedTexture,
pub anisotropic_textures: Vec<CachedTexture>,
pub texture_sampler: Sampler,
Expand Down Expand Up @@ -274,8 +272,6 @@ fn prepare_volumes(
mut commands: Commands,
render_device: Res<RenderDevice>,
render_queue: Res<RenderQueue>,
msaa: Res<Msaa>,
overlay: Res<ScreenOverlay>,
mut texture_cache: ResMut<TextureCache>,
mut volumes: Query<(Entity, &Volume)>,
mut volume_meta: ResMut<VolumeMeta>,
Expand Down Expand Up @@ -377,37 +373,9 @@ fn prepare_volumes(
));
}

let irradiance_depth_texture = texture_cache.get(
&render_device,
TextureDescriptor {
label: Some("volume_overlay_depth_texture"),
size: overlay.irradiance_size,
mip_level_count: 1,
sample_count: msaa.samples,
dimension: TextureDimension::D2,
format: TextureFormat::Depth32Float,
usage: TextureUsages::RENDER_ATTACHMENT | TextureUsages::TEXTURE_BINDING,
},
);

let albedo_depth_texture = texture_cache.get(
&render_device,
TextureDescriptor {
label: Some("volume_overlay_depth_texture"),
size: overlay.albedo_size,
mip_level_count: 1,
sample_count: msaa.samples,
dimension: TextureDimension::D2,
format: TextureFormat::Depth32Float,
usage: TextureUsages::RENDER_ATTACHMENT | TextureUsages::TEXTURE_BINDING,
},
);

commands.entity(entity).insert_bundle((
volume_uniform_offset.clone(),
VolumeBindings {
irradiance_depth_texture,
albedo_depth_texture,
voxel_texture,
anisotropic_textures,
texture_sampler: texture_sampler.clone(),
Expand Down
79 changes: 77 additions & 2 deletions src/overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use bevy::{
},
render_resource::*,
renderer::RenderDevice,
texture::BevyDefault,
texture::{BevyDefault, CachedTexture, TextureCache},
view::{ExtractedView, ViewDepthTexture, ViewTarget},
RenderApp, RenderStage,
},
Expand All @@ -41,6 +41,7 @@ impl Plugin for OverlayPlugin {
.init_resource::<SpecializedPipelines<MaterialPipeline<OverlayMaterial>>>()
.add_render_command::<Overlay, DrawMaterial<OverlayMaterial>>()
.add_system_to_stage(RenderStage::Extract, extract_screen_overlay)
.add_system_to_stage(RenderStage::Prepare, prepare_screen_overlay)
.add_system_to_stage(RenderStage::Prepare, prepare_overlay_phase)
.add_system_to_stage(RenderStage::Queue, queue_material_meshes);
}
Expand Down Expand Up @@ -70,6 +71,63 @@ fn extract_screen_overlay(mut commands: Commands, screen_overlay: Res<ScreenOver
commands.insert_resource(screen_overlay.clone());
}

fn prepare_screen_overlay(
mut commands: Commands,
images: Res<RenderAssets<Image>>,
overlay: Res<ScreenOverlay>,
msaa: Res<Msaa>,
render_device: Res<RenderDevice>,
mut texture_cache: ResMut<TextureCache>,
) {
let irradiance_depth = texture_cache.get(
&render_device,
TextureDescriptor {
label: Some("volume_overlay_depth_texture"),
size: overlay.irradiance_size,
mip_level_count: 1,
sample_count: msaa.samples,
dimension: TextureDimension::D2,
format: TextureFormat::Depth32Float,
usage: TextureUsages::RENDER_ATTACHMENT | TextureUsages::TEXTURE_BINDING,
},
);

let albedo_depth = texture_cache.get(
&render_device,
TextureDescriptor {
label: Some("volume_overlay_depth_texture"),
size: overlay.albedo_size,
mip_level_count: 1,
sample_count: msaa.samples,
dimension: TextureDimension::D2,
format: TextureFormat::Depth32Float,
usage: TextureUsages::RENDER_ATTACHMENT | TextureUsages::TEXTURE_BINDING,
},
);

let retrieve_textures = || {
let irradiance = images.get(&overlay.irradiance)?.texture_view.clone();
let irradiance_resolve = images
.get(&overlay.irradiance_resolve)?
.texture_view
.clone();
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))
};

if let Some((irradiance, irradiance_resolve, albedo, albedo_resolve)) = retrieve_textures() {
commands.insert_resource(GpuScreenOverlay {
irradiance,
irradiance_resolve,
irradiance_depth,
albedo,
albedo_resolve,
albedo_depth,
});
}
}

#[derive(Debug, Clone)]
pub struct ScreenOverlay {
pub irradiance_size: Extent3d,
Expand All @@ -81,6 +139,16 @@ pub struct ScreenOverlay {
pub albedo_resolve: Handle<Image>,
}

pub struct GpuScreenOverlay {
pub irradiance: TextureView,
pub irradiance_resolve: TextureView,
pub irradiance_depth: CachedTexture,

pub albedo: TextureView,
pub albedo_resolve: TextureView,
pub albedo_depth: CachedTexture,
}

impl FromWorld for ScreenOverlay {
fn from_world(world: &mut World) -> Self {
let windows = world.get_resource::<Windows>().unwrap();
Expand Down Expand Up @@ -168,14 +236,15 @@ impl RenderAsset for OverlayMaterial {
SRes<RenderDevice>,
SRes<MaterialPipeline<Self>>,
SRes<RenderAssets<Image>>,
Option<SRes<GpuScreenOverlay>>,
);
fn extract_asset(&self) -> Self::ExtractedAsset {
self.clone()
}

fn prepare_asset(
material: Self::ExtractedAsset,
(render_device, material_pipeline, images): &mut SystemParamItem<Self::Param>,
(render_device, material_pipeline, images, overlay): &mut SystemParamItem<Self::Param>,
) -> Result<Self::PreparedAsset, PrepareAssetError<Self::ExtractedAsset>> {
let irradiance = if let Some(result) = images.get(&material.irradiance_image) {
result
Expand All @@ -189,6 +258,12 @@ impl RenderAsset for OverlayMaterial {
return Err(PrepareAssetError::RetryNextUpdate(material));
};

let _overlay = if let Some(overlay) = overlay {
overlay
} else {
return Err(PrepareAssetError::RetryNextUpdate(material));
};

let bind_group = render_device.create_bind_group(&BindGroupDescriptor {
entries: &[
BindGroupEntry {
Expand Down
37 changes: 18 additions & 19 deletions src/tracing.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
overlay::ScreenOverlay, GpuVolume, NotGiReceiver, Volume, VolumeBindings, VolumeMeta,
overlay::GpuScreenOverlay, GpuVolume, NotGiReceiver, Volume, VolumeBindings, VolumeMeta,
VolumeUniformOffset, TRACING_SHADER_HANDLE,
};
use bevy::{
Expand Down Expand Up @@ -547,7 +547,6 @@ pub struct TracingPassNode {
&'static RenderPhase<Tracing<AlphaMask3d>>,
&'static RenderPhase<Tracing<Transparent3d>>,
&'static RenderPhase<AmbientOcclusion>,
&'static VolumeBindings,
),
With<ExtractedView>,
>,
Expand Down Expand Up @@ -579,30 +578,30 @@ impl render_graph::Node for TracingPassNode {
world: &World,
) -> Result<(), render_graph::NodeRunError> {
let view_entity = graph.get_input_entity(Self::IN_VIEW)?;
let (opaque_phase, alpha_mask_phase, transparent_phase, ambient_occlusion_phase, bindings) =
let (opaque_phase, alpha_mask_phase, transparent_phase, ambient_occlusion_phase) =
match self.query.get_manual(world, view_entity) {
Ok(query) => query,
Err(_) => return Ok(()),
};

let overlay = world.get_resource::<ScreenOverlay>().unwrap();
let images = world.get_resource::<RenderAssets<Image>>().unwrap();
let view = &images[&overlay.irradiance].texture_view;
let resolve_target = &images[&overlay.irradiance_resolve].texture_view;
let overlay = match world.get_resource::<GpuScreenOverlay>() {
Some(overlay) => overlay,
None => return Ok(()),
};

{
let pass_descriptor = RenderPassDescriptor {
label: Some("tracing_opaque_pass"),
color_attachments: &[RenderPassColorAttachment {
view,
resolve_target: Some(resolve_target),
view: &overlay.irradiance,
resolve_target: Some(&overlay.irradiance_resolve),
ops: Operations {
load: LoadOp::Clear(Color::NONE.into()),
store: true,
},
}],
depth_stencil_attachment: Some(RenderPassDepthStencilAttachment {
view: &bindings.irradiance_depth_texture.default_view,
view: &overlay.irradiance_depth.default_view,
depth_ops: Some(Operations {
load: LoadOp::Clear(0.0),
store: true,
Expand Down Expand Up @@ -631,15 +630,15 @@ impl render_graph::Node for TracingPassNode {
let pass_descriptor = RenderPassDescriptor {
label: Some("tracing_alpha_mask_pass"),
color_attachments: &[RenderPassColorAttachment {
view,
resolve_target: Some(resolve_target),
view: &overlay.irradiance,
resolve_target: Some(&overlay.irradiance_resolve),
ops: Operations {
load: LoadOp::Load,
store: true,
},
}],
depth_stencil_attachment: Some(RenderPassDepthStencilAttachment {
view: &bindings.irradiance_depth_texture.default_view,
view: &overlay.irradiance_depth.default_view,
depth_ops: Some(Operations {
load: LoadOp::Load,
store: true,
Expand Down Expand Up @@ -668,15 +667,15 @@ impl render_graph::Node for TracingPassNode {
let pass_descriptor = RenderPassDescriptor {
label: Some("tracing_transparent_pass"),
color_attachments: &[RenderPassColorAttachment {
view,
resolve_target: Some(resolve_target),
view: &overlay.irradiance,
resolve_target: Some(&overlay.irradiance_resolve),
ops: Operations {
load: LoadOp::Load,
store: true,
},
}],
depth_stencil_attachment: Some(RenderPassDepthStencilAttachment {
view: &bindings.irradiance_depth_texture.default_view,
view: &overlay.irradiance_depth.default_view,
depth_ops: Some(Operations {
load: LoadOp::Load,
store: false,
Expand Down Expand Up @@ -705,15 +704,15 @@ impl render_graph::Node for TracingPassNode {
let pass_descriptor = RenderPassDescriptor {
label: Some("tracing_ambient_occlusion_pass"),
color_attachments: &[RenderPassColorAttachment {
view,
resolve_target: Some(resolve_target),
view: &overlay.irradiance,
resolve_target: Some(&overlay.irradiance_resolve),
ops: Operations {
load: LoadOp::Load,
store: true,
},
}],
depth_stencil_attachment: Some(RenderPassDepthStencilAttachment {
view: &bindings.irradiance_depth_texture.default_view,
view: &overlay.irradiance_depth.default_view,
depth_ops: Some(Operations {
load: LoadOp::Clear(0.0),
store: true,
Expand Down

0 comments on commit 1e0ea63

Please sign in to comment.