pub(crate) mod probe_culling;
pub(crate) mod source;
pub(crate) mod velvet;
pub(crate) mod world;
use crate::scene::probes::UploadedProbes;
use crate::settings::{ReflectionMethod, WorldSpaceReflections};
use crate::view::bindings::FogVolume;
use crate::view::effective::{Effective, ScreenSpace};
use crate::view::frame::FrameContext;
use crate::view::pipelines::LitConstants;
use crate::view::post_fx::PostFx;
use crate::view::targets::SharedTargets;
static TEMPORAL_REPROJECTION: crate::shading::Module = crate::shading::Module {
name: "temporal_reprojection",
source: include_str!("temporal_reprojection.wgsl"),
deps: &[&crate::shading::LUMINANCE, &crate::shading::DEPTH],
};
pub(crate) struct Reflections {
source: source::ReflectionSource,
environment_parameters: wgpu::Buffer,
velvet: Option<velvet::Velvet>,
world: Option<world::WorldReflections>,
full_visibility: wgpu::TextureView,
}
#[cfg(all(test, not(target_arch = "wasm32")))]
impl Reflections {
pub(crate) fn world(&self) -> Option<&world::WorldReflections> {
self.world.as_ref()
}
}
impl Reflections {
pub fn new(
device: &wgpu::Device,
queue: &wgpu::Queue,
size: [u32; 2],
first_frame: &Effective,
) -> Self {
let variant = source_variant(first_frame);
Self {
source: source::ReflectionSource::new(device, size, variant),
environment_parameters: source::environment_uniform(device, 0., 1.),
velvet: None,
world: None,
full_visibility: crate::counters::texture_init(
device,
queue,
&wgpu::TextureDescriptor {
label: Some("full ambient visibility"),
size: wgpu::Extent3d {
width: 1,
height: 1,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::R32Uint,
usage: wgpu::TextureUsages::TEXTURE_BINDING,
view_formats: &[],
},
wgpu::util::TextureDataOrder::LayerMajor,
bytemuck::bytes_of(&255u32),
)
.create_view(&Default::default()),
}
}
pub fn resize(&mut self, device: &wgpu::Device, size: [u32; 2]) {
self.source.resize(device, size);
}
pub fn incident(&self) -> Option<&wgpu::TextureView> {
self.source.incident()
}
pub fn complete(
&mut self,
ctx: &mut FrameContext<'_>,
ambient_occlusion: Option<&wgpu::TextureView>,
) {
let (traced, fade) = traced(ctx);
let frame = &ctx.values.frame;
crate::counters::write_buffer(
ctx.queue,
&self.environment_parameters,
0,
bytemuck::bytes_of(&source::ReflectionEnvironment {
yaw: frame.reflection_yaw,
intensity: frame.reflection_intensity,
fade,
traced,
}),
);
let probes = ctx
.scene
.specular_probes()
.unwrap_or(&ctx.bindings.empty_probes);
self.source
.use_variant(ctx.device, source_variant(ctx.effective));
let visibility = ambient_occlusion.unwrap_or(&self.full_visibility).clone();
let fog = ctx.bindings.fog();
let inputs = source_inputs(ctx, probes, fog, &self.environment_parameters, &visibility);
self.source
.encode(ctx.encoder, ctx.device, ctx.queue, inputs, ctx.timing);
}
pub fn resolve(
&mut self,
ctx: &mut FrameContext<'_>,
post_fx: Option<&mut PostFx>,
ambient_occlusion: Option<&wgpu::TextureView>,
) -> Option<wgpu::TextureView> {
let screen_space = ctx.effective.screen_space;
if !screen_space.is_some_and(|ssr| ssr.method == ReflectionMethod::Velvet) {
self.velvet = None;
}
let reach = ctx.effective.world_space;
if reach == WorldSpaceReflections::Off {
self.world = None;
}
let ScreenSpace {
method,
half_resolution,
..
} = screen_space?;
let (traced, fade) = traced(ctx);
let t = ctx.targets;
let surface = ctx.surface;
let camera = ctx.views.reflection_camera;
let reflected = match method {
ReflectionMethod::Crystal => post_fx
.expect("Crystal SSR runs in the post-effect context")
.screen_space_reflections(
ctx.device,
ctx.queue,
ctx.encoder,
ctx.input.frame_time_ms / 1000.,
half_resolution,
&self.source.incident,
ctx.timing,
),
ReflectionMethod::Velvet => self
.velvet
.get_or_insert_with(|| velvet::Velvet::new(ctx.device))
.encode(
ctx.device,
ctx.queue,
ctx.encoder,
ctx.sizes.render,
half_resolution,
velvet::Inputs {
depth: surface.depth,
opaque_depth: &t.depth,
receivers: surface.receivers,
normal: &t.normal,
material: &t.material,
f0: &t.f0,
radiance: &self.source.incident,
motion: &t.motion,
camera,
frame: ctx.history,
},
ctx.timing,
),
};
let world_space = if reach != WorldSpaceReflections::Off {
let world = self.world.get_or_insert_with(|| {
world::WorldReflections::new(
ctx.device,
&ctx.bindings.lit,
&ctx.bindings.scene,
ctx.sizes.render,
)
});
world.encode(
ctx.encoder,
ctx.device,
ctx.queue,
[ctx.bindings.ray_hit_lit(), &ctx.scene.scene_group],
LitConstants::of(ctx.scene),
ctx.hardware_rays,
reach,
ctx.history,
ctx.sizes.render,
world::Inputs {
depth: &t.depth,
surface_depth: surface.depth,
normal: &t.normal,
material: &t.material,
f0: &t.f0,
motion: &t.motion,
source_id: &t.source_id,
screen_space: reflected,
camera,
traced,
fade,
},
ctx.timing,
);
Some(world.output())
} else {
None
};
let probes = ctx
.scene
.specular_probes()
.unwrap_or(&ctx.bindings.empty_probes);
let visibility = ambient_occlusion.unwrap_or(&self.full_visibility).clone();
let fog = ctx.bindings.fog();
let inputs = source_inputs(ctx, probes, fog, &self.environment_parameters, &visibility);
self.source.compose(
ctx.encoder,
ctx.device,
inputs,
surface.depth,
reflected,
world_space,
ctx.timing,
);
Some(reflected.clone())
}
}
fn source_variant(effective: &Effective) -> source::Variant {
source::Variant {
environment: effective.source_environment,
incident: effective.screen_space.is_some(),
}
}
fn traced(ctx: &FrameContext<'_>) -> (f32, f32) {
ctx.effective
.screen_space
.map_or((0., 0.), |ssr| (ssr.cutoff * ssr.cutoff, ssr.fade))
}
fn source_inputs<'a: 'p, 'p>(
ctx: &FrameContext<'a>,
probes: &'p UploadedProbes,
fog: FogVolume<'p>,
parameters: &'p wgpu::Buffer,
visibility: &'p wgpu::TextureView,
) -> source::Inputs<'p> {
let t: &'a SharedTargets = ctx.targets;
let scene: &'a crate::Scene = ctx.scene;
let frame = &ctx.values.frame;
let environments = &scene.environments;
source::Inputs {
fog,
frame_fog: ctx.effective.fog.map(|_| source::SourceFog {
inverse_length: frame.fog_inverse_length,
inverse_detail_spread: frame.fog_inverse_detail_spread,
sky_affect: ctx.input.fog.sky_affect.clamp(0., 1.),
}),
camera: ctx.views.reflection_camera,
scene: &t.color,
ambient: &t.ambient,
output: &t.composite,
normal: &t.normal,
anisotropy: &t.anisotropy,
f0: &t.f0,
depth: &t.depth,
lookup_tables: &scene.lookup_tables,
ambient_occlusion: visibility,
environment: source::Environment {
sky: &environments.frame(ctx.input.environment).pmrem,
sampler: &environments.sampler,
parameters,
material: &t.material,
baked: &probes.view,
collection: &probes.metadata,
},
}
}