pub(crate) mod ambient_occlusion;
pub(crate) mod backdrop;
pub(crate) mod screen_indirect;
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::{CameraMask, FrameContext};
use crate::view::pipelines::{CameraShadows, GeometryPass, GeometryPipelines};
use crate::view::targets::{CaptureFace, attachment};
pub(crate) struct Opaque {
backdrop: backdrop::BackdropDraw,
ambient_occlusion: Option<ambient_occlusion::AmbientOcclusion>,
ambient_occlusion_ran: bool,
screen_indirect: Option<screen_indirect::ScreenIndirect>,
screen_indirect_read: bool,
shadow_mask: CachedGroup,
contact_shadow: CachedGroup,
screen_indirect_groups: [[CachedGroup; 2]; 3],
}
fn mask_index(mask: Option<CameraMask<'_>>) -> usize {
match mask {
None => 0,
Some(CameraMask::Traced(_)) => 1,
Some(CameraMask::Contact(_)) => 2,
}
}
impl Opaque {
pub fn new(
device: &wgpu::Device,
unlit: &wgpu::BindGroupLayout,
[
shadow_mask,
contact_shadow,
screen_indirect,
shadow_mask_screen_indirect,
contact_screen_indirect,
]: [&wgpu::BindGroupLayout; 5],
) -> Self {
Self {
backdrop: backdrop::BackdropDraw::new(device, unlit),
ambient_occlusion: None,
ambient_occlusion_ran: false,
screen_indirect: None,
screen_indirect_read: false,
shadow_mask: CachedGroup::new(shadow_mask.clone()),
contact_shadow: CachedGroup::new(contact_shadow.clone()),
screen_indirect_groups: [
screen_indirect,
shadow_mask_screen_indirect,
contact_screen_indirect,
]
.map(|layout| std::array::from_fn(|_| CachedGroup::new(layout.clone()))),
}
}
fn begin_screen_indirect(&mut self, ctx: &FrameContext<'_>) {
let settings = ctx.effective.screen_indirect;
if settings.is_none() && self.screen_indirect.is_none() {
self.screen_indirect_read = false;
return;
}
let stage = self
.screen_indirect
.get_or_insert_with(|| screen_indirect::ScreenIndirect::new(ctx.device));
self.screen_indirect_read = stage.begin(
ctx.device,
ctx.queue,
settings,
ctx.sizes.render,
&ctx.history,
);
}
fn screen_indirect_group(
&mut self,
device: &wgpu::Device,
mask: Option<CameraMask<'_>>,
) -> Option<&wgpu::BindGroup> {
if !self.screen_indirect_read {
return None;
}
let read = self.screen_indirect.as_ref()?.read()?;
let mut entries = vec![
(
crate::shading::bind::screen_indirect::KEPT,
wgpu::BindingResource::TextureView(read.kept),
),
(
crate::shading::bind::screen_indirect::VIEW,
read.view.as_entire_binding(),
),
];
match mask {
None => {}
Some(CameraMask::Traced(shadow_mask)) => entries.extend([
(
crate::shading::bind::shadow_mask::MASK,
wgpu::BindingResource::TextureView(shadow_mask.mask),
),
(
crate::shading::bind::shadow_mask::SLOTS,
shadow_mask.slots.as_entire_binding(),
),
]),
Some(CameraMask::Contact(contact)) => entries.push((
crate::shading::bind::contact_shadow::MASK,
wgpu::BindingResource::TextureView(contact),
)),
}
let groups = &mut self.screen_indirect_groups[mask_index(mask)];
Some(groups[read.half].get(device, "screen-space indirect light", &entries))
}
pub fn encode_gbuffer(&mut self, ctx: &mut FrameContext<'_>, phase: Phase) {
if phase == Phase::Early {
self.begin_screen_indirect(ctx);
}
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<'_>, mask: Option<CameraMask<'_>>) {
if !matches!(mask, Some(CameraMask::Contact(_))) {
self.contact_shadow.forget();
self.screen_indirect_groups[2]
.iter_mut()
.for_each(CachedGroup::forget);
}
if !ctx.effective.fused {
self.encode_lighting_pass(ctx, mask);
}
}
pub fn encode_ambient_occlusion(&mut self, ctx: &mut FrameContext<'_>) {
self.ambient_occlusion_ran = ctx.effective.ambient_occlusion.is_some();
let pyramid = match (
ctx.effective.ambient_occlusion,
ctx.effective.screen_indirect,
) {
(Some(settings), _) => (settings.radius, ambient_occlusion::TIMING_GROUP),
(None, Some(settings)) => (settings.pyramid_radius, screen_indirect::TIMING_GROUP),
(None, None) => {
if self.ambient_occlusion.as_ref().is_some_and(|ao| !ao.ran()) {
self.ambient_occlusion = None;
}
return;
}
};
let targets = ctx.targets;
let ao = self
.ambient_occlusion
.get_or_insert_with(|| ambient_occlusion::AmbientOcclusion::new(ctx.device, ctx.queue));
ao.encode_depth_pyramid(
ctx.device,
ctx.queue,
ctx.encoder,
&targets.depth,
&ctx.views.camera.view.uniform,
pyramid.0,
(ctx.timing, pyramid.1),
);
if let Some(settings) = ctx.effective.ambient_occlusion {
ao.encode_visibility(
ctx.device,
ctx.queue,
ctx.encoder,
&targets.normal,
&ctx.views.camera.view.uniform,
settings.quality,
settings.radius,
ctx.history.frames,
ctx.timing,
);
ao.resolve(
ctx.device,
ctx.queue,
ctx.encoder,
&targets.depth,
&targets.motion,
ctx.history,
settings.quality,
settings.radius,
ctx.timing,
);
}
}
pub fn encode_screen_indirect(&mut self, ctx: &mut FrameContext<'_>) {
let Some(settings) = ctx.effective.screen_indirect else {
return;
};
let ao = self
.ambient_occlusion
.as_ref()
.expect("the frame built the depth pyramid");
let targets = ctx.targets;
let inputs = screen_indirect::Inputs {
depth_pyramid: ao.depth_view().expect("the frame built the depth pyramid"),
hilbert: ao.hilbert(),
depth: &targets.depth,
normals: &targets.normal,
color: &targets.color,
motion: &targets.motion,
};
self.screen_indirect
.as_mut()
.expect("the frame began screen-space indirect light")
.encode(
ctx.device,
ctx.queue,
ctx.encoder,
&inputs,
&ctx.views.camera.view.uniform,
&ctx.history,
settings,
ctx.timing,
);
}
pub fn settle(&mut self, finished: bool) {
if let Some(ambient_occlusion) = &mut self.ambient_occlusion {
ambient_occlusion.settle(finished);
}
if let Some(screen_indirect) = &mut self.screen_indirect {
screen_indirect.settle(finished);
}
}
#[cfg(all(test, not(target_arch = "wasm32")))]
pub fn holds_occlusion_and_bounce(&self) -> (bool, bool) {
(
self.ambient_occlusion.is_some(),
self.screen_indirect
.as_ref()
.is_some_and(screen_indirect::ScreenIndirect::holds_targets),
)
}
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.backdrop.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(), &[]);
let screen_indirect = self.screen_indirect_group(ctx.device, None);
if let Some(group) = screen_indirect {
pass.set_bind_group(3, group, &[]);
}
ctx.views.camera.list.draw(
ctx.scene,
ctx.pipelines,
&mut pass,
GeometryPass::Fused {
screen_indirect: screen_indirect.is_some(),
},
);
}
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<'_>, mask: Option<CameraMask<'_>>) {
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.backdrop.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(), &[]);
let screen_indirect = self.screen_indirect_group(ctx.device, mask);
let takes_screen_indirect = screen_indirect.is_some();
if let Some(group) = screen_indirect {
pass.set_bind_group(3, group, &[]);
} else {
match mask {
None => {}
Some(CameraMask::Traced(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, &[]);
}
Some(CameraMask::Contact(contact)) => {
let group = self.contact_shadow.get(
ctx.device,
"contact shadow mask",
&[(
crate::shading::bind::contact_shadow::MASK,
wgpu::BindingResource::TextureView(contact),
)],
);
pass.set_bind_group(3, group, &[]);
}
}
}
let shadows = match mask {
None => CameraShadows::Maps,
Some(CameraMask::Traced(_)) => CameraShadows::ShadowMask,
Some(CameraMask::Contact(_)) => CameraShadows::ContactMask,
};
ctx.views.camera.list.draw(
ctx.scene,
ctx.pipelines,
&mut pass,
GeometryPass::Lighting {
shadows,
screen_indirect: takes_screen_indirect,
},
);
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: &mut 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_backdrop(&self, pass: &mut wgpu::RenderPass<'_>, unlit: &wgpu::BindGroup) {
self.backdrop.draw(pass, unlit);
}
fn encode_forward(
&self,
pass: &mut wgpu::RenderPass<'_>,
scene: &crate::Scene,
pipelines: &mut GeometryPipelines,
(list, drawn): (&DrawList, &DrawInstances),
unlit: &wgpu::BindGroup,
lit: &wgpu::BindGroup,
) {
self.backdrop.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 test_geometry;
#[cfg(all(test, not(target_arch = "wasm32")))]
mod tests;