use super::shared::Shared;
use super::view::{Pending, View, ViewFrame};
use crate::settings::{ReflectionMethod, Settings};
use crate::stages::cull::Cull;
use crate::stages::opaque::Opaque;
use crate::stages::prepare::Prepare;
use crate::stages::shadows::contact::ContactShadowStage;
use crate::stages::shadows::traced::TracedShadows;
use crate::stages::shadows::traced::slots::SlotLights;
use crate::timing::GpuTiming;
use crate::view::draw_list::gpu::Phase;
use crate::view::effective::Effective;
use crate::view::frame::{CameraMask, Completed, FrameContext, HardwareRays};
use crate::view::history::HistoryFrame;
use crate::view::post_fx::PostFx;
use crate::view::targets::SharedTargets;
use crate::{FrameInput, Scene};
use std::sync::Arc;
use std::sync::atomic::AtomicBool;
#[derive(Clone, Copy, PartialEq, Eq)]
pub(super) enum Role {
Primary,
Secondary,
}
pub(super) struct ViewOutput<'a> {
pub input: &'a FrameInput,
pub output: &'a wgpu::TextureView,
pub timing: Option<&'a GpuTiming>,
}
fn scene_wide(settings: &Settings, primary: &Settings) -> Settings {
Settings {
hardware_ray_tracing: primary.hardware_ray_tracing,
dynamic_gi: primary.dynamic_gi,
anisotropic_filtering: primary.anisotropic_filtering,
..*settings
}
}
fn secondary(effective: Effective, primary: &Effective) -> Effective {
Effective {
layers: primary.layers,
anisotropy: primary.anisotropy,
..effective
}
}
pub(super) fn render(
shared: &mut Shared,
(device, queue, encoder): (&wgpu::Device, &wgpu::Queue, &mut wgpu::CommandEncoder),
scene: &mut Scene,
views: &mut [ViewFrame<'_>],
finished: &Arc<AtomicBool>,
) -> u64 {
let attempt = scene.begin_frame(device, queue);
let primary_settings = *views[0].settings;
let mut begun: Vec<(HistoryFrame, Effective)> = Vec::with_capacity(views.len());
for (index, frame) in views.iter_mut().enumerate() {
let settings = match index {
0 => primary_settings,
_ => scene_wide(frame.settings, &primary_settings),
};
let (history, effective) = frame
.view
.begin(device, shared, scene, frame.input, &settings);
let effective = match begun.first() {
Some((_, primary)) => secondary(effective, primary),
None => effective,
};
begun.push((history, effective));
}
let traced = begun
.iter()
.any(|(_, effective)| Prepare::traces(scene, effective));
shared.run_scene((device, queue), scene, &begun[0].1, views[0].input, traced);
for (index, (frame, begun)) in views.iter_mut().zip(begun).enumerate() {
render_view(
shared,
frame.view,
(device, queue, encoder),
scene,
ViewOutput {
input: frame.input,
output: frame.output,
timing: frame.timing,
},
begun,
(
if index == 0 {
Role::Primary
} else {
Role::Secondary
},
attempt,
finished,
),
);
}
attempt
}
pub(super) fn render_view(
shared: &mut Shared,
view: &mut View,
(device, queue, encoder): (&wgpu::Device, &wgpu::Queue, &mut wgpu::CommandEncoder),
scene: &Scene,
drawn: ViewOutput<'_>,
(mut history, effective): (HistoryFrame, Effective),
(role, attempt, finished): (Role, u64, &Arc<AtomicBool>),
) {
let ViewOutput {
input,
output,
timing,
} = drawn;
let primary = role == Role::Primary;
let Shared {
pipelines,
prepare,
deform,
dynamic_gi,
ray_form,
..
} = shared;
let View {
sizes,
targets,
views,
bindings,
post_fx,
cull,
sky,
statistics,
shadows,
traced_shadows,
contact_shadows,
fog,
opaque,
reflections,
transparent,
exposure,
antialiasing,
motion_blur,
post,
rendered,
targets_generation,
#[cfg(feature = "diagnostics")]
probe,
#[cfg(any(test, feature = "diagnostics"))]
completed: diagnostic_completed,
..
} = view;
let targets: &SharedTargets = targets;
#[cfg(feature = "diagnostics")]
let mut probe =
crate::stages::frame_probe::FrameProbe::for_frame(probe, device, effective.frame_probe);
#[cfg(feature = "diagnostics")]
if let Some(probe) = probe.as_deref_mut() {
let metadata = crate::stages::frame_probe::metadata(*sizes, input, history);
probe.begin(device, encoder, metadata);
}
let crystal = effective
.screen_space
.is_some_and(|ssr| ssr.method == ReflectionMethod::Crystal);
if !effective.post_fx {
if post_fx.take().is_some() {
post.forget_inputs();
motion_blur.forget_inputs();
}
} else if post_fx
.as_ref()
.is_none_or(|post_fx| post_fx.taa() != effective.taa)
{
*post_fx = Some(Box::new(PostFx::new(device, queue, effective.taa)));
post.forget_inputs();
motion_blur.forget_inputs();
}
if let Some(post_fx) = post_fx.as_mut()
&& !crystal
{
post_fx.release_screen_space_reflections();
}
let context_jitter = post_fx.as_mut().map(|post_fx| {
post_fx.prepare(device, encoder, sizes.render, history.frames, history.valid)
});
let jitter = antialiasing.prepare(
&effective,
*sizes,
history,
post_fx.as_deref_mut(),
context_jitter,
);
history.camera.jitter = jitter.map_or([0.; 2], |jitter| jitter.ndc);
*rendered = Some(Pending {
attempt,
finished: finished.clone(),
history,
generation: *targets_generation,
});
statistics.poll(device);
statistics.begin();
let values = Prepare::run_view(
device,
queue,
scene,
input,
history,
&effective,
jitter,
sizes.render,
views,
&bindings.frame,
);
shadows.resize(device, effective.shadow_quality);
shadows.local.prepare(
device,
queue,
bindings,
(scene, &mut views.instances),
(input.camera.view, input.camera.projection),
crate::stages::shadows::local::ShadowFrame {
mask: values.frame.visibility_mask,
time: input.elapsed_seconds,
layers: effective.layers,
},
effective.local_lights,
);
views.instances.upload(device, queue);
fog.prepare(
device,
(effective.fog, effective.aerial_perspective),
sizes.render,
);
if let Some(dynamic_gi) = dynamic_gi.as_mut().filter(|_| primary) {
dynamic_gi.prepare(device, scene, &effective);
}
let slot_lights = SlotLights::of(input, &values.frame, scene, shadows.local.ranking());
let effective =
super::effective::shadow_stages(effective, prepare.hardware_rays(), &slot_lights);
sky.prepare(
device,
input,
input.camera.eye,
(!history.valid, effective.aerial_perspective),
);
bindings.refresh(
device,
scene,
crate::view::bindings::EnvironmentViews::resolve(
&scene.environments,
&input.environment,
sky.views(),
),
views,
shadows.maps(),
fog.volume(),
dynamic_gi
.as_ref()
.map(crate::stages::dynamic_gi::DynamicGi::probes),
);
let mut ctx = FrameContext {
device,
queue,
encoder,
timing,
effective: &effective,
sizes: *sizes,
targets,
surface: targets.surface(false),
scene,
values: &values,
input,
views: &*views,
bindings,
pipelines,
history,
environment_revision: sky.environment_revision(),
hardware_rays: HardwareRays::of(
&effective,
scene,
prepare.hardware_rays(),
ray_form.as_ref(),
),
};
if primary {
deform.encode(&mut ctx);
prepare.encode_acceleration_structures(&mut ctx);
}
cull.encode_early(&mut ctx);
sky.encode(&mut ctx);
if let Some(dynamic_gi) = dynamic_gi.as_mut().filter(|_| primary) {
dynamic_gi.encode(&mut ctx);
}
shadows.encode_local(&mut ctx);
shadows.encode_directional(&mut ctx);
fog.encode(&mut ctx, sky.aerial_volume());
encode_opaque(
opaque,
cull,
(traced_shadows, contact_shadows),
&slot_lights,
&mut ctx,
);
statistics.copy(
device,
ctx.encoder,
&ctx.views.camera.list,
ctx.views.blended.stats,
#[cfg(feature = "diagnostics")]
crate::view::draw_list::readback::ModelStatistics {
slots: ctx.scene.candidates.models(),
blended: ctx.views.blended.stats_by_model(ctx.scene),
},
);
#[cfg(feature = "diagnostics")]
if let Some(probe) = probe.as_deref() {
probe.observe(
device,
ctx.encoder,
0,
&targets.color,
&targets.color,
false,
);
probe.coverage(device, ctx.encoder, &targets.depth, &targets.source_id);
}
let receivers = transparent.encode_receivers(&mut ctx);
ctx.surface = targets.surface(receivers);
transparent.encode_volumes(&mut ctx);
transparent.order_mist(&ctx);
if let Some(post_fx) = post_fx.as_mut() {
let camera = ctx.views.reflection_camera;
post_fx.begin(
device,
queue,
ctx.encoder,
targets,
ctx.surface,
sizes.render,
glam::Mat4::from_cols_array_2d(&camera.view),
glam::Mat4::from_cols_array_2d(&camera.proj),
history.previous_camera,
timing,
);
}
let ambient_occlusion = opaque.visibility();
reflections.complete(&mut ctx, ambient_occlusion);
if let Some(incident) = reflections.incident() {
transparent.encode(
&mut ctx,
crate::stages::transparent::Beauty::Incident(incident),
);
}
let reflected = reflections.resolve(&mut ctx, post_fx.as_deref_mut(), ambient_occlusion);
transparent.encode(
&mut ctx,
crate::stages::transparent::Beauty::Composite {
reflections: reflected.as_ref(),
},
);
#[cfg(feature = "diagnostics")]
if let Some(probe) = probe.as_deref() {
probe.observe(
device,
ctx.encoder,
2,
&targets.composite,
&targets.color,
true,
);
}
if effective.composite.heat {
transparent.encode_heat(&mut ctx, &targets.composite);
}
exposure.encode(&mut ctx, &targets.composite);
let completed_kind = antialiasing.encode(&mut ctx, post_fx.as_deref_mut(), exposure.texture());
if antialiasing.take_failure() {
post.forget_inputs();
motion_blur.forget_inputs();
}
#[cfg(any(test, feature = "diagnostics"))]
{
*diagnostic_completed = Some(completed_kind);
}
let antialiased = completed(completed_kind, post_fx, antialiasing, targets);
let completed_view = motion_blur
.encode(&mut ctx, antialiased)
.unwrap_or(antialiased);
#[cfg(feature = "diagnostics")]
if let Some(probe) = probe.as_deref() {
probe.observe(
device,
ctx.encoder,
3,
completed_view,
completed_view,
false,
);
}
post.encode(
&mut ctx,
completed_view,
completed_kind,
exposure.view(),
output,
);
#[cfg(feature = "diagnostics")]
if let Some(probe) = probe.as_deref() {
probe.observe(
device,
ctx.encoder,
4,
post.tone_mapped(),
completed_view,
post.tone_mapped().texture().size() == completed_view.texture().size(),
);
probe.finish(ctx.encoder);
}
}
pub(super) fn encode_opaque(
opaque: &mut Opaque,
cull: &mut Cull,
(traced_shadows, contact_shadows): (&mut TracedShadows, &mut ContactShadowStage),
slot_lights: &SlotLights,
ctx: &mut FrameContext<'_>,
) {
opaque.encode_gbuffer(ctx, Phase::Early);
if ctx.effective.occlusion_culling {
cull.encode_late(ctx);
opaque.encode_gbuffer(ctx, Phase::Late);
cull.encode_pyramid(ctx);
}
let traced = traced_shadows.encode(ctx, slot_lights);
let contact = contact_shadows.encode(ctx, slot_lights.directional);
let mask = traced
.map(CameraMask::Traced)
.or(contact.map(CameraMask::Contact));
opaque.encode_lighting(ctx, mask);
opaque.encode_ambient_occlusion(ctx);
opaque.encode_screen_indirect(ctx);
}
pub(super) fn completed<'a>(
kind: Completed,
post_fx: &'a Option<Box<PostFx>>,
antialiasing: &'a crate::stages::antialiasing::Antialiasing,
targets: &'a SharedTargets,
) -> &'a wgpu::TextureView {
match kind {
Completed::Taa => post_fx.as_ref().unwrap().taa_output(),
Completed::Fsr => antialiasing.output(),
Completed::Composite => &targets.composite,
}
}