pub(crate) mod ambient_occlusion;
pub(crate) mod sky;
use crate::counters::Moment;
use crate::view::cached_group::CachedGroup;
use crate::view::draw_list::gpu::Phase;
use crate::view::draw_list::{DrawInstances, DrawList};
use crate::view::frame::{FrameContext, ShadowMask};
use crate::view::pipelines::{GeometryPass, GeometryPipelines};
use crate::view::targets::{CaptureFace, attachment};
pub(crate) struct Opaque {
sky: sky::Sky,
ambient_occlusion: Option<ambient_occlusion::AmbientOcclusion>,
ambient_occlusion_ran: bool,
shadow_mask: CachedGroup,
}
impl Opaque {
pub fn new(
device: &wgpu::Device,
unlit: &wgpu::BindGroupLayout,
shadow_mask: &wgpu::BindGroupLayout,
) -> Self {
Self {
sky: sky::Sky::new(device, unlit),
ambient_occlusion: None,
ambient_occlusion_ran: false,
shadow_mask: CachedGroup::new(shadow_mask.clone()),
}
}
pub fn encode_gbuffer(&mut self, ctx: &mut FrameContext<'_>, phase: Phase) {
if ctx.effective.fused {
if phase == Phase::Early {
self.encode_fused(ctx);
}
} else {
Self::encode_gbuffer_pass(ctx, phase);
}
}
pub fn encode_lighting(
&mut self,
ctx: &mut FrameContext<'_>,
shadow_mask: Option<ShadowMask<'_>>,
) {
if !ctx.effective.fused {
self.encode_lighting_pass(ctx, shadow_mask);
}
}
pub fn encode_ambient_occlusion(&mut self, ctx: &mut FrameContext<'_>) {
self.ambient_occlusion_ran = ctx.effective.ambient_occlusion.is_some();
if let Some(settings) = ctx.effective.ambient_occlusion {
let targets = ctx.targets;
self.ambient_occlusion
.get_or_insert_with(|| ambient_occlusion::AmbientOcclusion::new(ctx.device))
.encode(
ctx.device,
ctx.queue,
ctx.encoder,
&targets.depth,
&targets.normal,
&ctx.views.camera.view.uniform,
settings.quality,
settings.radius,
ctx.timing,
);
}
}
pub fn visibility(&self) -> Option<&wgpu::TextureView> {
self.ambient_occlusion
.as_ref()
.filter(|_| self.ambient_occlusion_ran)
.and_then(|ambient_occlusion| ambient_occlusion.output())
}
fn encode_fused(&mut self, ctx: &mut FrameContext<'_>) {
let targets = ctx.targets;
{
let sky = [attachment(&targets.color), attachment(&targets.motion)];
let mut pass = ctx.encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("fused scene sky and depth clear"),
color_attachments: &sky,
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
view: &targets.depth,
depth_ops: Some(wgpu::Operations {
load: wgpu::LoadOp::Clear(0.),
store: wgpu::StoreOp::Store,
}),
stencil_ops: None,
}),
timestamp_writes: ctx.timing.and_then(|t| t.render_pass("sky")),
..Default::default()
});
self.sky.draw(&mut pass, ctx.bindings.camera_unlit());
}
let views = [
&targets.normal,
&targets.material,
&targets.motion,
&targets.f0,
&targets.color,
&targets.ambient,
&targets.source_id,
&targets.anisotropy,
];
let attachments = std::array::from_fn::<_, 8, _>(|index| {
let mut target = attachment(views[index]);
if matches!(index, 2 | 4) {
target.as_mut().unwrap().ops.load = wgpu::LoadOp::Load;
}
target
});
let started = Moment::now();
{
let mut pass = ctx.encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("fused opaque material and lighting"),
color_attachments: &attachments,
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
view: &targets.depth,
depth_ops: Some(wgpu::Operations {
load: wgpu::LoadOp::Load,
store: wgpu::StoreOp::Store,
}),
stencil_ops: None,
}),
timestamp_writes: ctx
.timing
.and_then(|t| t.render_pass("opaque geometry + lighting")),
..Default::default()
});
pass.set_bind_group(0, ctx.bindings.camera_lit(), &[]);
ctx.views
.camera
.list
.draw(ctx.scene, ctx.pipelines, &mut pass, GeometryPass::Fused);
}
ctx.views.camera.recorded_since(started);
}
fn encode_gbuffer_pass(ctx: &mut FrameContext<'_>, phase: Phase) {
let targets = ctx.targets;
let anisotropy_inline = ctx.pipelines.anisotropy_inline;
let late = phase == Phase::Late;
let last = late || !ctx.views.camera.list.late();
let started = Moment::now();
{
let colors = [
&targets.normal,
&targets.material,
&targets.motion,
&targets.f0,
&targets.anisotropy,
];
let attachments = colors.map(|view| {
let mut target = attachment(view);
if late {
target.as_mut().unwrap().ops.load = wgpu::LoadOp::Load;
}
target
});
let (label, group) = if late {
("stable scene geometry, late set", "geometry late")
} else {
("stable scene geometry", "geometry")
};
let mut pass = ctx.encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some(label),
timestamp_writes: ctx.timing.and_then(|t| t.render_pass(group)),
color_attachments: &attachments[..if anisotropy_inline { 5 } else { 4 }],
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
view: &targets.depth,
depth_ops: Some(wgpu::Operations {
load: if late {
wgpu::LoadOp::Load
} else {
wgpu::LoadOp::Clear(0.0)
},
store: wgpu::StoreOp::Store,
}),
stencil_ops: None,
}),
..Default::default()
});
pass.set_bind_group(0, ctx.bindings.camera_lit(), &[]);
ctx.views.camera.list.draw_phase(
ctx.scene,
ctx.pipelines,
&mut pass,
GeometryPass::GBuffer,
phase,
);
}
ctx.views.camera.recorded_since(started);
if !anisotropy_inline && last {
let started = Moment::now();
let mut pass = ctx.encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("stable anisotropy attachment fallback"),
color_attachments: &[attachment(&targets.anisotropy)],
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
view: &targets.depth,
depth_ops: Some(wgpu::Operations {
load: wgpu::LoadOp::Load,
store: wgpu::StoreOp::Store,
}),
stencil_ops: None,
}),
timestamp_writes: ctx.timing.and_then(|t| t.render_pass("geometry")),
..Default::default()
});
pass.set_bind_group(0, ctx.bindings.camera_lit(), &[]);
ctx.views.camera.list.draw(
ctx.scene,
ctx.pipelines,
&mut pass,
GeometryPass::GBufferAnisotropy,
);
drop(pass);
ctx.views.camera.recorded_since(started);
}
}
fn encode_lighting_pass(
&mut self,
ctx: &mut FrameContext<'_>,
shadow_mask: Option<ShadowMask<'_>>,
) {
let targets = ctx.targets;
{
let mut motion = attachment(&targets.motion);
motion.as_mut().unwrap().ops.load = wgpu::LoadOp::Load;
let mut pass = ctx.encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("source sky background"),
color_attachments: &[attachment(&targets.color), motion],
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
view: &targets.depth,
depth_ops: Some(wgpu::Operations {
load: wgpu::LoadOp::Load,
store: wgpu::StoreOp::Store,
}),
stencil_ops: None,
}),
timestamp_writes: ctx.timing.and_then(|t| t.render_pass("sky")),
..Default::default()
});
self.sky.draw(&mut pass, ctx.bindings.camera_unlit());
}
let colors = [
&targets.color,
&targets.ambient,
&targets.motion,
&targets.source_id,
];
let attachments = std::array::from_fn::<_, 4, _>(|index| {
let mut target = attachment(colors[index]);
if matches!(index, 0 | 2) {
target.as_mut().unwrap().ops.load = wgpu::LoadOp::Load;
}
target
});
let started = Moment::now();
let mut pass = ctx.encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("opaque HDR with stable depth ownership"),
timestamp_writes: ctx.timing.and_then(|t| t.render_pass("opaque lighting")),
color_attachments: &attachments,
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
view: &targets.depth,
depth_ops: Some(wgpu::Operations {
load: wgpu::LoadOp::Load,
store: wgpu::StoreOp::Store,
}),
stencil_ops: None,
}),
..Default::default()
});
pass.set_bind_group(0, ctx.bindings.camera_lit(), &[]);
if let Some(shadow_mask) = shadow_mask {
let group = self.shadow_mask.get(
ctx.device,
"ray-traced shadow mask",
&[
(
crate::shading::bind::shadow_mask::MASK,
wgpu::BindingResource::TextureView(shadow_mask.mask),
),
(
crate::shading::bind::shadow_mask::SLOTS,
shadow_mask.slots.as_entire_binding(),
),
],
);
pass.set_bind_group(3, group, &[]);
}
ctx.views.camera.list.draw(
ctx.scene,
ctx.pipelines,
&mut pass,
GeometryPass::Lighting {
shadow_mask: shadow_mask.is_some(),
},
);
drop(pass);
ctx.views.camera.recorded_since(started);
}
#[allow(clippy::too_many_arguments)]
pub fn encode_capture(
&self,
encoder: &mut wgpu::CommandEncoder,
scene: &crate::Scene,
pipelines: &GeometryPipelines,
list: (&DrawList, &DrawInstances),
face: &CaptureFace,
unlit: &wgpu::BindGroup,
lit: &wgpu::BindGroup,
) {
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("static instances and sky capture"),
color_attachments: &[attachment(&face.color), attachment(&face.motion)],
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
view: &face.depth,
depth_ops: Some(wgpu::Operations {
load: wgpu::LoadOp::Clear(0.),
store: wgpu::StoreOp::Store,
}),
stencil_ops: None,
}),
..Default::default()
});
self.encode_forward(&mut pass, scene, pipelines, list, unlit, lit);
}
#[cfg(all(test, not(target_arch = "wasm32")))]
pub fn draw_sky(&self, pass: &mut wgpu::RenderPass<'_>, unlit: &wgpu::BindGroup) {
self.sky.draw(pass, unlit);
}
fn encode_forward(
&self,
pass: &mut wgpu::RenderPass<'_>,
scene: &crate::Scene,
pipelines: &GeometryPipelines,
(list, drawn): (&DrawList, &DrawInstances),
unlit: &wgpu::BindGroup,
lit: &wgpu::BindGroup,
) {
self.sky.draw(pass, unlit);
pass.set_bind_group(0, lit, &[]);
list.draw(scene, pipelines, drawn, pass, GeometryPass::Forward);
}
}
#[cfg(all(test, not(target_arch = "wasm32")))]
mod tests;