use crate::shading::gbuffer;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct Sizes {
pub render: [u32; 2],
pub scene: [u32; 2],
pub output: [u32; 2],
}
pub(crate) const MASK_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::R8Unorm;
pub(crate) fn mask_targets() -> [Option<wgpu::ColorTargetState>; 2] {
let blend = wgpu::BlendState {
color: wgpu::BlendComponent {
src_factor: wgpu::BlendFactor::OneMinusDst,
dst_factor: wgpu::BlendFactor::One,
operation: wgpu::BlendOperation::Add,
},
alpha: wgpu::BlendComponent {
src_factor: wgpu::BlendFactor::One,
dst_factor: wgpu::BlendFactor::Zero,
operation: wgpu::BlendOperation::Add,
},
};
[0, 1].map(|_| {
Some(wgpu::ColorTargetState {
format: MASK_FORMAT,
blend: Some(blend),
write_mask: wgpu::ColorWrites::RED,
})
})
}
pub(crate) fn composition_targets() -> [Option<wgpu::ColorTargetState>; 2] {
[wgpu::ColorWrites::empty(), wgpu::ColorWrites::RED].map(|write_mask| {
Some(wgpu::ColorTargetState {
format: MASK_FORMAT,
blend: None,
write_mask,
})
})
}
pub(crate) struct SharedTargets {
pub color: wgpu::TextureView,
pub ambient: wgpu::TextureView,
pub source_id: wgpu::TextureView,
pub depth: wgpu::TextureView,
pub normal: wgpu::TextureView,
pub material: wgpu::TextureView,
pub anisotropy: wgpu::TextureView,
pub f0: wgpu::TextureView,
pub motion: wgpu::TextureView,
pub composite: wgpu::TextureView,
pub fsr2_masks: [wgpu::TextureView; 2],
pub surface: Option<SurfaceTargets>,
}
pub(crate) struct SurfaceTargets {
pub depth: wgpu::TextureView,
pub receivers: wgpu::TextureView,
}
impl SurfaceTargets {
fn new(device: &wgpu::Device, size: [u32; 2]) -> Self {
Self {
depth: target(device, "surface depth", size, gbuffer::DEPTH),
receivers: target(device, "receiver layer", size, gbuffer::RECEIVER),
}
}
}
#[derive(Clone, Copy)]
pub(crate) struct Surface<'a> {
pub depth: &'a wgpu::TextureView,
pub receivers: &'a wgpu::TextureView,
}
impl SharedTargets {
pub fn new(device: &wgpu::Device, size: [u32; 2], surface: bool) -> Self {
let gbuffer_target = |label, format| target(device, label, size, format);
let depth = gbuffer_target("stable depth", gbuffer::DEPTH);
Self {
color: gbuffer_target("jittered HDR scene", gbuffer::COLOR),
ambient: gbuffer_target("jittered ambient light", gbuffer::AMBIENT),
source_id: gbuffer_target("stable lit primitive identity", gbuffer::SOURCE_ID),
depth,
normal: gbuffer_target("stable world normals", gbuffer::NORMAL),
material: gbuffer_target(
"stable coat and base roughness, coat strength and F90",
gbuffer::MATERIAL,
),
anisotropy: gbuffer_target(
"stable world anisotropy tangent and strength and environment scale",
gbuffer::ANISOTROPY,
),
f0: gbuffer_target(
"stable material F0, lit and baked-light flags and material occlusion",
gbuffer::F0,
),
motion: gbuffer_target("stable rigid motion", gbuffer::MOTION),
composite: target_with_usage(
device,
"HDR composition",
size,
gbuffer::COLOR,
wgpu::TextureUsages::STORAGE_BINDING,
),
fsr2_masks: [
"FSR2 reactive mask",
"FSR2 transparency and composition mask",
]
.map(|label| target(device, label, size, MASK_FORMAT)),
surface: surface.then(|| SurfaceTargets::new(device, size)),
}
}
pub fn hold_surface(&mut self, device: &wgpu::Device) {
if self.surface.is_none() {
let size = self.depth.texture().size();
self.surface = Some(SurfaceTargets::new(device, [size.width, size.height]));
}
}
pub fn surface(&self, drew: bool) -> Surface<'_> {
match &self.surface {
Some(surface) if drew => Surface {
depth: &surface.depth,
receivers: &surface.receivers,
},
_ => Surface {
depth: &self.depth,
receivers: &self.normal,
},
}
}
}
pub(crate) struct CaptureFace {
pub color: wgpu::TextureView,
pub motion: wgpu::TextureView,
pub depth: wgpu::TextureView,
}
pub(crate) fn target(
device: &wgpu::Device,
label: &str,
size: [u32; 2],
format: wgpu::TextureFormat,
) -> wgpu::TextureView {
target_with_usage(device, label, size, format, wgpu::TextureUsages::empty())
}
pub(crate) fn target_with_usage(
device: &wgpu::Device,
label: &str,
size: [u32; 2],
format: wgpu::TextureFormat,
extra_usage: wgpu::TextureUsages,
) -> wgpu::TextureView {
device
.create_texture(&wgpu::TextureDescriptor {
label: Some(label),
size: wgpu::Extent3d {
width: size[0].max(1),
height: size[1].max(1),
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format,
usage: wgpu::TextureUsages::RENDER_ATTACHMENT
| wgpu::TextureUsages::TEXTURE_BINDING
| wgpu::TextureUsages::COPY_SRC
| wgpu::TextureUsages::COPY_DST
| extra_usage,
view_formats: &[],
})
.create_view(&Default::default())
}
pub(crate) fn hold<T>(
held: &mut Option<T>,
texture: impl Fn(&T) -> &wgpu::Texture,
size: wgpu::Extent3d,
make: impl FnOnce() -> T,
) -> &T {
if held
.as_ref()
.is_none_or(|kept| texture(kept).size() != size)
{
*held = Some(make());
}
held.as_ref().unwrap()
}
pub(crate) fn attachment(view: &wgpu::TextureView) -> Option<wgpu::RenderPassColorAttachment<'_>> {
Some(wgpu::RenderPassColorAttachment {
view,
depth_slice: None,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT),
store: wgpu::StoreOp::Store,
},
})
}