Record dense camera motion for temporal upscaling (#124)

Add an RG16F current-to-previous render-pixel target to the HD G-buffer. Terrain and entities write previous drawn-camera projections; TAA/TAAU use matching opaque vectors and retain the depth fallback elsewhere. Object-local previous transforms and vendor SDK evaluation remain separate phases.

Co-Authored-By: Kimi Code <noreply@moonshot.ai>
This commit is contained in:
2026-11-14 17:46:38 +03:00
parent 94b572cd24
commit c962cd472f
7 changed files with 141 additions and 45 deletions
+18
View File
@@ -28,6 +28,11 @@ struct Globals {
hd_wind: vec4<f32>,
// AirDef fork: the sprite layers' material classes, 4 bits each (gfx/hd/materials.rs).
hd_materials: array<vec4<u32>, 64>,
// Previous drawn (jittered) view-projection and the camera's translation since it.
prev_view_proj: mat4x4<f32>,
camera_delta: vec4<f32>,
// Render-target width and height in pixels for current-to-previous motion vectors.
hd_motion: vec4<f32>,
}
@group(0) @binding(0) var<uniform> globals: Globals;
@group(0) @binding(1) var atlas: texture_2d<f32>;
@@ -65,6 +70,8 @@ struct Out {
// AirDef fork, HD lighting's G-buffer: the camera-relative position, sky and block light.
@location(13) pos: vec3<f32>,
@location(14) lm: vec2<f32>,
// Camera motion only; previous object-local transforms are supplied by a later pass.
@location(15) prev_clip: vec4<f32>,
}
// Shipped light.glsl; normals are already transformed and RGBA8_SNORM packed.
fn entity_light(normal: vec3<f32>, nether: bool) -> f32 {
@@ -98,6 +105,7 @@ fn entity_vertex(position: vec3<f32>, uv: vec2<f32>,
overlay: u32, color: vec4<f32>, light: vec4<u32>, normal: vec4<f32>) -> Out {
var out: Out;
out.clip = globals.view_proj * vec4(position, 1.0);
out.prev_clip = globals.prev_view_proj * vec4(position + globals.camera_delta.xyz, 1.0);
out.proj = vec4(out.clip.xy * 0.5 + out.clip.w * 0.5, out.clip.zw);
out.uv = uv;
let l = vec2(max(f32(light.y), dynamic_block_light(position)), f32(light.x));
@@ -278,6 +286,15 @@ struct EntityGBufferOut {
@location(0) albedo: vec4<f32>,
@location(1) normal: vec4<f32>,
@location(2) light: vec4<f32>,
@location(3) motion: vec2<f32>,
}
fn motion_pixels(pos: vec3<f32>, previous: vec4<f32>) -> vec2<f32> {
let current = globals.view_proj * vec4(pos, 1.0);
if current.w <= 0.0 || previous.w <= 0.0 { return vec2<f32>(0.0); }
let c = current.xy / current.w;
let p = previous.xy / previous.w;
let delta = vec2<f32>((p.x - c.x) * 0.5, (c.y - p.y) * 0.5);
return delta * globals.hd_motion.xy;
}
@fragment fn fs_entity_gbuffer(in: Out, @builtin(front_facing) front: bool) -> EntityGBufferOut {
let color = textureSample(atlas, atlas_sampler, in.uv) * select(in.back_color, in.color, front);
@@ -296,6 +313,7 @@ struct EntityGBufferOut {
if in.overlay == 42u { material = vec2(0.28, 1.0); }
if in.overlay == 40u { material = vec2(0.5, 0.0); }
out.light = vec4(clamp(in.lm, vec2(0.0), vec2(1.0)), material);
out.motion = motion_pixels(in.pos, in.prev_clip);
return out;
}
// AirDef fork: entities in the sun's shadow map.
+20
View File
@@ -209,6 +209,12 @@ struct Globals {
hd_wind: [f32; 4],
/// AirDef fork: the sprite layers' material classes, 4 bits each (`hd::materials`).
hd_materials: hd::MaterialTable,
/// The previous frame's drawn (jittered) view-projection, for dense motion vectors.
prev_view_proj: [[f32; 4]; 4],
/// xyz: the camera's translation since that frame.
camera_delta: [f32; 4],
/// Render-target width and height in pixels for current-to-previous motion vectors.
hd_motion: [f32; 4],
}
pub type SectionKey = (i32, i32, i32);
struct SectionGpu {
@@ -1425,6 +1431,11 @@ impl Renderer {
blend: None,
write_mask: wgpu::ColorWrites::ALL,
}),
Some(wgpu::ColorTargetState {
format: hd::MOTION,
blend: None,
write_mask: wgpu::ColorWrites::ALL,
}),
];
// The shadow maps: plain depth from the light, biased against acne, both faces.
let shadow_depth = Some(wgpu::DepthStencilState {
@@ -4346,6 +4357,15 @@ impl Renderer {
[0.0; 4]
},
hd_materials: self.hd_materials,
prev_view_proj: taa_prev.map_or(view_proj, |p| p.2).to_cols_array_2d(),
camera_delta: {
let d = taa_prev.map_or(Vec3::ZERO, |p| (frame.camera - p.1).as_vec3());
[d.x, d.y, d.z, 0.0]
},
hd_motion: {
let size = self.world_post.size().unwrap_or([self.config.width, self.config.height]);
[size[0] as f32, size[1] as f32, 0.0, 0.0]
},
};
self.uploads
.buffer(&mut encoder, &self.globals, 0, bytemuck::bytes_of(&globals));
+9 -1
View File
@@ -7,6 +7,9 @@
//! - `normal` (`Rgba16Float`): rgb world-space unit normal (from `dpdx/dpdy`; plants use up), a unused.
//! - `levels` (`Rgba8Unorm`): x = sky light, y = block light, both the raw lightmap coordinate / 240
//! (0..1); zw unused.
//! - `motion` (`Rg16Float`): current-to-previous displacement in render-target pixels, with both
//! projections as actually drawn, jitter included. The first pass covers camera/static geometry;
//! object-local previous transforms are a separate contract.
//! - `depth` (`Depth32Float`): camera depth, **reversed-Z** (cleared to 0 = far / nothing, 1 = near).
//! An untouched pixel (depth 0) is sky.
//! - Readers: AO, lighting, water composite, TAA/TAAU (depth). Written by `gbuffer_pass`; the caller
@@ -18,6 +21,8 @@ pub const ALBEDO: wgpu::TextureFormat = wgpu::TextureFormat::Rgba8Unorm;
pub const NORMAL: wgpu::TextureFormat = wgpu::TextureFormat::Rgba16Float;
/// Sky and block light per pixel.
pub const LIGHT_LEVELS: wgpu::TextureFormat = wgpu::TextureFormat::Rgba8Unorm;
/// Current-to-previous motion in render-target pixels.
pub const MOTION: wgpu::TextureFormat = wgpu::TextureFormat::Rg16Float;
/// The camera depth formats (the G-buffer's and the water's own).
pub const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float;
@@ -25,6 +30,7 @@ pub(super) struct GBuffer {
pub(super) albedo: wgpu::TextureView,
pub(super) normal: wgpu::TextureView,
pub(super) levels: wgpu::TextureView,
pub(super) motion: wgpu::TextureView,
pub(super) depth: wgpu::TextureView,
}
@@ -35,13 +41,14 @@ impl GBuffer {
albedo: target(device, "HD albedo", size, ALBEDO).view,
normal: target(device, "HD normal", size, NORMAL).view,
levels: target(device, "HD light levels", size, LIGHT_LEVELS).view,
motion: target(device, "HD motion", size, MOTION).view,
depth: target(device, "HD depth", size, DEPTH_FORMAT).view,
}
}
}
impl Hd {
/// The G-buffer pass's attachments: albedo and normal cleared, its own depth.
/// The G-buffer pass's attachments: colour, normal, light and motion cleared, its own depth.
pub fn gbuffer_pass<'e>(
&mut self,
encoder: &'e mut wgpu::CommandEncoder,
@@ -65,6 +72,7 @@ impl Hd {
clear(&self.gbuffer.albedo),
clear(&self.gbuffer.normal),
clear(&self.gbuffer.levels),
clear(&self.gbuffer.motion),
],
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
view: &self.gbuffer.depth,
+3 -3
View File
@@ -24,14 +24,14 @@
//! |---|---|---|---|
//! | 1 | `sky` | frame | sky texture 128x72 (linear Rec. 709 radiance) |
//! | 2 | `shadow_pass` x2 (caller draws) | - | shadow array (normal Z, 1 = far) |
//! | 3 | `gbuffer_pass` (caller draws) | - | albedo (a = emission), normal, light levels, depth |
//! | 3 | `gbuffer_pass` (caller draws) | - | albedo (a = emission), normal, light levels, current→previous motion (render pixels), depth |
//! | 3a | `water_pass` (caller draws) | - | water colour (a = block light), normal (a = sky light), water depth |
//! | 3b | `ao` | G-buffer depth, normal | AO half size (x occlusion, y distance) |
//! | 3c | `volumes` | frame, sky, world depth, shadows, noise | clouds (rgb, a = transmittance), cloud shadow map, fog (rgb, a = transmittance) + ray length |
//! | 4 | `light` | world colour + depth, G-buffer, shadows, sky, AO, volumes, water depth | `lit` (a: 1 normal / 0.5 water / 0.6 in front of water), `air` |
//! | 4a | `water_composite` | `lit`, `air`, G-buffer depth, water targets, shadows, sky | `lit_water` |
//! | 5 | mod post pass (caller) | `lit_view()` + depth | `fx` |
//! | 6 | `taa` | `lit_view()` or `fx`, world depth, history | `taa` + next history (out) |
//! | 6 | `taa` | `lit_view()` or `fx`, world depth, G-buffer motion/depth, history | `taa` + next history (out) |
//! | 6a | `film` (film quality only) | the colour source, world depth | motion blur, then depth of field (out) |
//! | 7 | `finish` | colour source (`HdSource`) | bloom chain, then the screen |
//!
@@ -94,7 +94,7 @@ use volumes::HdVolumes;
use water::Water;
pub use frame::{HdLight, HdScene, HdSettings, HdSpot, MAX_SPOTS, light_of, vanilla_cloud_tint};
pub use gbuffer::{ALBEDO, LIGHT_LEVELS, NORMAL};
pub use gbuffer::{ALBEDO, LIGHT_LEVELS, MOTION, NORMAL};
pub use materials::{Table as MaterialTable, table as material_table, wind_enabled, wind_seconds};
pub use shadows::{CASCADES, SHADOW_TINT, cascades, color_shadows_enabled};
pub use smoke::{HdSmoke, smoke_enabled, smoke_possible};
+8 -4
View File
@@ -4,7 +4,9 @@
//! Contract:
//! - Reads: the colour source (`HdSource::Lit`/`Fx`, the lit world with water, or the mod's post
//! output; display space, world size), the *world* depth (reversed-Z, the pass's own depth from the
//! world pass, not the G-buffer's), and the history (output size, `Rgba16Float`). `HdFrame`:
//! world pass, not the G-buffer's), the history (output size, `Rgba16Float`), and the G-buffer's
//! motion/depth. Matching opaque pixels use explicit current-to-previous render-pixel motion;
//! other pixels keep camera/depth reprojection. `HdFrame`:
//! `prev_view_proj` (last frame's matrix; **with** jitter for `fs_taa`, **without** for `fs_taau`,
//! chosen in `Hd::write`), `taa` (xyz the camera's move since, w history usable), `taa_params`
//! (x history weight, y sharpening, zw jitter in world pixels).
@@ -19,9 +21,9 @@ use super::{
};
pub(super) const TAA: &str = include_str!("taa.wgsl");
/// Group 0 of `fs_taa` / `fs_taau`: frame, the colour, the world depth, the history, the sampler.
pub(super) const TAA_BINDINGS: [wgpu::BindingType; 5] =
[UNIFORM, UNFILTERED, DEPTH, FLOAT, SAMPLER];
/// Group 0 of `fs_taa` / `fs_taau`: frame, colour, world depth, history, sampler, motion and G-buffer depth.
pub(super) const TAA_BINDINGS: [wgpu::BindingType; 7] =
[UNIFORM, UNFILTERED, DEPTH, FLOAT, SAMPLER, UNFILTERED, DEPTH];
/// TAA is on unless `HORIZON_HD_TAA=0`.
pub fn taa_enabled() -> bool {
@@ -209,6 +211,8 @@ impl Hd {
wgpu::BindingResource::TextureView(depth),
wgpu::BindingResource::TextureView(&self.taa.history[read]),
wgpu::BindingResource::Sampler(&self.sampler),
wgpu::BindingResource::TextureView(&self.gbuffer.motion),
wgpu::BindingResource::TextureView(&self.gbuffer.depth),
],
)
});
+65 -37
View File
@@ -8,6 +8,18 @@ struct TaaOut {
@group(0) @binding(2) var depth_tex: texture_depth_2d;
@group(0) @binding(3) var history: texture_2d<f32>;
@group(0) @binding(4) var linear: sampler;
@group(0) @binding(5) var motion_tex: texture_2d<f32>;
@group(0) @binding(6) var gbuffer_depth: texture_depth_2d;
// Opaque G-buffer geometry has an explicit current-to-previous vector. World-only or translucent
// pixels keep the depth/camera fallback until their own motion/reactive contract is defined.
fn history_motion(px: vec2<i32>, world_depth: f32) -> vec3<f32> {
let surface = textureLoad(gbuffer_depth, px, 0);
if surface > 0.0 && abs(surface - world_depth) <= 0.000001 {
return vec3<f32>(textureLoad(motion_tex, px, 0).xy, 1.0);
}
return vec3<f32>(0.0);
}
fn to_ycocg(c: vec3<f32>) -> vec3<f32> {
return vec3<f32>(c.r * 0.25 + c.g * 0.5 + c.b * 0.25, c.r * 0.5 - c.b * 0.5, c.g * 0.5 - c.r * 0.25 - c.b * 0.25);
@@ -91,26 +103,31 @@ fn fs_taa(in: FullOut) -> TaaOut {
let dist = select(length(p), 0.0, sky);
var resolved = centre;
if frame.taa.w > 0.5 {
var uv = vec2<f32>(-1.0);
let prev = frame.prev_view_proj * vec4<f32>(select(p + frame.taa.xyz, p, sky), 1.0);
if prev.w > 0.0 {
let pn = prev.xy / prev.w;
let uv = vec2<f32>(pn.x * 0.5 + 0.5, 0.5 - pn.y * 0.5);
if all(uv > vec2<f32>(0.0)) && all(uv < vec2<f32>(1.0)) {
let hp = clamp(vec2<i32>(uv * vec2<f32>(size)), vec2<i32>(0), size - 1);
let seen = textureLoad(history, hp, 0).a;
let expected = select(length(p + frame.taa.xyz), 0.0, sky);
// The same surface as last frame (not uncovered), by its distance.
if abs(seen - expected) <= expected * 0.03 + 0.25 {
let mean = sum / 9.0;
let sigma = sqrt(max(sum2 / 9.0 - mean * mean, vec3<f32>(0.0)));
let box_lo = min(max(mean - sigma * 1.5, lo), centre);
let box_hi = max(min(mean + sigma * 1.5, hi), centre);
let past = clip_to_box(box_lo, box_hi, to_ycocg(squash(history_color(uv))));
// Fast motion trusts the history less.
let moved = length((uv - in.uv) * vec2<f32>(size));
let keep = mix(frame.taa_params.x, frame.taa_params.x * 0.75, clamp(moved / 4.0, 0.0, 1.0));
resolved = mix(centre, past, keep);
}
uv = vec2<f32>(pn.x * 0.5 + 0.5, 0.5 - pn.y * 0.5);
}
let motion = history_motion(px, d);
if motion.z > 0.5 {
uv = in.uv + motion.xy / vec2<f32>(size);
}
if all(uv > vec2<f32>(0.0)) && all(uv < vec2<f32>(1.0)) {
let hp = clamp(vec2<i32>(uv * vec2<f32>(size)), vec2<i32>(0), size - 1);
let seen = textureLoad(history, hp, 0).a;
let expected = select(length(p + frame.taa.xyz), 0.0, sky);
// The same surface as last frame (not uncovered), by its distance.
if abs(seen - expected) <= expected * 0.03 + 0.25 {
let mean = sum / 9.0;
let sigma = sqrt(max(sum2 / 9.0 - mean * mean, vec3<f32>(0.0)));
let box_lo = min(max(mean - sigma * 1.5, lo), centre);
let box_hi = max(min(mean + sigma * 1.5, hi), centre);
let past = clip_to_box(box_lo, box_hi, to_ycocg(squash(history_color(uv))));
// Fast motion trusts the history less.
let moved = length((uv - in.uv) * vec2<f32>(size));
let keep = mix(frame.taa_params.x, frame.taa_params.x * 0.75, clamp(moved / 4.0, 0.0, 1.0));
resolved = mix(centre, past, keep);
}
}
}
@@ -168,9 +185,15 @@ fn fs_taau(in: FullOut) -> TaaOut {
// Depth: the nearest of the 2x2 samples round the pixel (steady between frames at an edge).
let corner = vec2<i32>(floor(in.uv * low - 0.5 + jit));
var d = 0.0;
var nearest = clamp(corner, vec2<i32>(0), lowi - 1);
for (var y = 0; y <= 1; y++) {
for (var x = 0; x <= 1; x++) {
d = max(d, textureLoad(depth_tex, clamp(corner + vec2<i32>(x, y), vec2<i32>(0), lowi - 1), 0));
let sample = clamp(corner + vec2<i32>(x, y), vec2<i32>(0), lowi - 1);
let sample_depth = textureLoad(depth_tex, sample, 0);
if sample_depth > d {
d = sample_depth;
nearest = sample;
}
}
}
let sky = d <= 0.0;
@@ -182,28 +205,33 @@ fn fs_taau(in: FullOut) -> TaaOut {
let dist = select(length(p), 0.0, sky);
var resolved = current;
if frame.taa.w > 0.5 {
var uv = vec2<f32>(-1.0);
let prev = frame.prev_view_proj * vec4<f32>(select(p + frame.taa.xyz, p, sky), 1.0);
if prev.w > 0.0 {
let pn = prev.xy / prev.w;
let uv = vec2<f32>(pn.x * 0.5 + 0.5, 0.5 - pn.y * 0.5);
if all(uv > vec2<f32>(0.0)) && all(uv < vec2<f32>(1.0)) {
let hp = clamp(vec2<i32>(uv * high), vec2<i32>(0), vec2<i32>(high) - 1);
let seen = textureLoad(history, hp, 0).a;
let expected = select(length(p + frame.taa.xyz), 0.0, sky);
if abs(seen - expected) <= expected * 0.03 + 0.25 {
let mean = sum / 9.0;
let sigma = sqrt(max(sum2 / 9.0 - mean * mean, vec3<f32>(0.0)));
let box_lo = min(max(mean - sigma * 1.25, lo), current);
let box_hi = max(min(mean + sigma * 1.25, hi), current);
let past = clip_to_box(box_lo, box_hi, to_ycocg(squash(history_color(uv))));
// How far the nearest sample is from the pixel's centre (0 on it, 1 at the
// corner of the world's pixel): the farther, the less the frame says.
let near = clamp(length(t - floor(t + 0.5)) / 0.7071, 0.0, 1.0);
let fresh = (1.0 - frame.taa_params.x) * mix(1.8, 0.5, near);
let moved = length((uv - in.uv) * high);
let keep = (1.0 - fresh) * mix(1.0, 0.8, clamp(moved / 4.0, 0.0, 1.0));
resolved = mix(current, past, clamp(keep, 0.0, 0.98));
}
uv = vec2<f32>(pn.x * 0.5 + 0.5, 0.5 - pn.y * 0.5);
}
let motion = history_motion(nearest, d);
if motion.z > 0.5 {
uv = in.uv + motion.xy / low;
}
if all(uv > vec2<f32>(0.0)) && all(uv < vec2<f32>(1.0)) {
let hp = clamp(vec2<i32>(uv * high), vec2<i32>(0), vec2<i32>(high) - 1);
let seen = textureLoad(history, hp, 0).a;
let expected = select(length(p + frame.taa.xyz), 0.0, sky);
if abs(seen - expected) <= expected * 0.03 + 0.25 {
let mean = sum / 9.0;
let sigma = sqrt(max(sum2 / 9.0 - mean * mean, vec3<f32>(0.0)));
let box_lo = min(max(mean - sigma * 1.25, lo), current);
let box_hi = max(min(mean + sigma * 1.25, hi), current);
let past = clip_to_box(box_lo, box_hi, to_ycocg(squash(history_color(uv))));
// How far the nearest sample is from the pixel's centre (0 on it, 1 at the
// corner of the world's pixel): the farther, the less the frame says.
let near = clamp(length(t - floor(t + 0.5)) / 0.7071, 0.0, 1.0);
let fresh = (1.0 - frame.taa_params.x) * mix(1.8, 0.5, near);
let moved = length((uv - in.uv) * high);
let keep = (1.0 - fresh) * mix(1.0, 0.8, clamp(moved / 4.0, 0.0, 1.0));
resolved = mix(current, past, clamp(keep, 0.0, 0.98));
}
}
}
+18
View File
@@ -30,6 +30,11 @@ struct Globals {
hd_wind: vec4<f32>,
// AirDef fork: the sprite layers' material classes, 4 bits each (gfx/hd/materials.rs).
hd_materials: array<vec4<u32>, 64>,
// Previous drawn (jittered) view-projection and the camera's translation since it.
prev_view_proj: mat4x4<f32>,
camera_delta: vec4<f32>,
// Render-target width and height in pixels for current-to-previous motion vectors.
hd_motion: vec4<f32>,
}
@group(0) @binding(0) var<uniform> globals: Globals;
@group(0) @binding(1) var sprites: texture_2d_array<f32>;
@@ -82,6 +87,8 @@ struct TerrainOut {
@location(7) tint: vec4<f32>,
// Sky and block light, 0 to 1.
@location(8) lm: vec2<f32>,
// The same static world point in the previous drawn camera frame.
@location(9) prev_clip: vec4<f32>,
}
// Horizon Extras' dynamic lights (dynamic_light.rs): the block light, in the vertex's units
// (16 per level), a vertex at camera-relative `pos` gets from the frame's light sources, one
@@ -189,6 +196,7 @@ fn terrain_vertex(position: vec3<f32>, uv: vec2<f32>, layer: u32, color: vec4<f3
let pos = position + origin + wind_offset(position + origin, uv, layer);
var out: TerrainOut;
out.clip = globals.view_proj * vec4(pos, 1.0);
out.prev_clip = globals.prev_view_proj * vec4(pos + globals.camera_delta.xyz, 1.0);
out.uv = uv;
out.layer = layer;
let block = max(f32(light.y), dynamic_block_light(pos));
@@ -227,6 +235,15 @@ struct GBufferOut {
@location(0) albedo: vec4<f32>,
@location(1) normal: vec4<f32>,
@location(2) light: vec4<f32>,
@location(3) motion: vec2<f32>,
}
fn motion_pixels(pos: vec3<f32>, previous: vec4<f32>) -> vec2<f32> {
let current = globals.view_proj * vec4(pos, 1.0);
if current.w <= 0.0 || previous.w <= 0.0 { return vec2<f32>(0.0); }
let c = current.xy / current.w;
let p = previous.xy / previous.w;
let delta = vec2<f32>((p.x - c.x) * 0.5, (c.y - p.y) * 0.5);
return delta * globals.hd_motion.xy;
}
fn face_normal(pos: vec3<f32>) -> vec3<f32> {
var n = normalize(cross(dpdx(pos), dpdy(pos)));
@@ -245,6 +262,7 @@ fn face_normal(pos: vec3<f32>) -> vec3<f32> {
out.normal = vec4(face_normal(in.pos), 1.0);
// Roughness and metalness for the sun's specular and the wet look.
out.light = vec4(clamp(in.lm, vec2(0.0), vec2(1.0)), material_params(material_class(in.layer)));
out.motion = motion_pixels(in.pos, in.prev_clip);
return out;
}
// AirDef fork, HD water: the water quads once more, after the G-buffer, into small targets of