Record previous mod-model transforms for temporal motion (#124)

Keep each mod model's last rendered world transform and emit a separate previous-position vertex stream. A depth-read-only motion pass overwrites camera-only vectors with exact previous-frame object motion and matching alpha cutouts. Add HORIZON_HD_DEBUG_MOTION for visual verification.

Co-Authored-By: Kimi Code <noreply@moonshot.ai>
This commit is contained in:
2026-11-14 21:04:21 +03:00
parent 17382445af
commit 7a73e8d26a
8 changed files with 233 additions and 7 deletions
+14
View File
@@ -148,6 +148,15 @@ fn entity_vertex(position: vec3<f32>, uv: vec2<f32>,
out.glint_uv = glint_uv;
return out;
}
@vertex fn vs_entity_motion(@location(0) position: vec3<f32>, @location(1) uv: vec2<f32>,
@location(2) overlay: u32, @location(3) color: vec4<f32>, @location(4) light: vec4<u32>,
@location(5) normal: vec4<f32>, @location(6) glint_uv: vec2<f32>,
@location(7) previous: vec3<f32>) -> Out {
var out = entity_vertex(position, uv, overlay, color, light, normal);
out.glint_uv = glint_uv;
out.prev_clip = globals.prev_view_proj * vec4(previous + globals.camera_delta.xyz, 1.0);
return out;
}
// Retained bolt-local geometry, translated using the same f64 camera
// subtraction as the former CPU path before adding each local f32 position.
// The bolt offset is instance attribute 7: 6 is the entity vertex's glint UV.
@@ -296,6 +305,11 @@ fn motion_pixels(pos: vec3<f32>, previous: vec4<f32>) -> vec2<f32> {
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_motion(in: Out, @builtin(front_facing) front: bool) -> @location(0) vec2<f32> {
let color = textureSample(atlas, atlas_sampler, in.uv) * select(in.back_color, in.color, front);
if color.a < 0.1 || (in.cull != 0u && !front) || in.overlay == 4u || in.overlay == 9u || in.overlay == 10u { discard; }
return motion_pixels(in.pos, in.prev_clip);
}
@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);
if color.a < 0.1 || (in.cull != 0u && !front) || in.overlay == 4u || in.overlay == 9u || in.overlay == 10u { discard; }
+10
View File
@@ -11989,6 +11989,10 @@ impl Game<'_> {
hud.patches.append(&mut self.map_patches);
let entity_spans = std::mem::take(self.entity_spans.get_mut());
let glowing = std::mem::take(self.glowing_vertices.get_mut());
#[cfg(feature = "mods")]
let motion_models = std::mem::take(&mut self.mods.models.motion);
#[cfg(not(feature = "mods"))]
let motion_models = Vec::new();
// The main view is the frame itself, not a picture of its own.
let mod_views: Vec<_> = self
.mod_views(alpha)
@@ -12100,6 +12104,7 @@ impl Game<'_> {
color: sky.cloud_color,
}),
entities: &entities,
motion_models: &motion_models,
entity_spans: &entity_spans,
glowing: &glowing,
dynamic_lights: &self.dynamic_lights,
@@ -12166,6 +12171,10 @@ impl Game<'_> {
}
// Restore storage even when rendering fails (for example, a lost surface).
self.entity_vertices = entities;
#[cfg(feature = "mods")]
{
self.mods.models.motion = motion_models;
}
*self.entity_spans.get_mut() = entity_spans;
*self.glowing_vertices.get_mut() = glowing;
self.see_through_vertices = see_through;
@@ -15021,6 +15030,7 @@ fn loading_frame(hud: &HudBatch) -> Frame<'_> {
glint_ticks: 0.0,
nether_lighting: false,
entities: &[],
motion_models: &[],
entity_spans: &[],
glowing: &[],
dynamic_lights: &[],
+39 -3
View File
@@ -7,7 +7,7 @@ use super::Game;
use {
glam::{DVec3, Mat4, Quat, Vec3},
horizon_client::{
mesher::Vertex,
mesher::{MotionVertex, Vertex},
particles::{ParticleOptions, ParticleType},
},
horizon_core::random::RandomSource,
@@ -16,8 +16,18 @@ use {
std::collections::HashMap,
};
/// The last transform a frame actually drew, independent of the camera.
#[cfg(feature = "mods")]
#[derive(Clone, Copy)]
struct RenderedPose {
at: DVec3,
rotation: Quat,
scale: f32,
}
/// One model of a mod: where it was a tick ago and where it is now.
#[cfg(feature = "mods")]
#[derive(Clone, Copy)]
struct Pose {
state: BlockStateId,
previous: DVec3,
@@ -27,6 +37,7 @@ struct Pose {
old_scale: f32,
scale: f32,
glow: bool,
rendered: Option<RenderedPose>,
}
/// The models the running mods keep, by (mod, id).
@@ -34,6 +45,8 @@ struct Pose {
#[derive(Default)]
pub(super) struct ModModels {
poses: HashMap<(usize, u32), Pose>,
/// Current and previous-frame vertices for the HD motion-only pass.
pub(super) motion: Vec<MotionVertex>,
/// Block states by the text mods name them, parsed once.
states: HashMap<String, Option<BlockStateId>>,
/// The version of the mod post pass the renderer has (0: none).
@@ -158,6 +171,7 @@ impl Game<'_> {
old_scale: m.scale,
scale: m.scale,
glow: m.glow,
rendered: None,
},
);
}
@@ -389,10 +403,12 @@ impl Game<'_> {
/// Draws every mod model between its last two poses, lit by the world
/// (or at full brightness), its faces shaded by its own rotation.
pub(super) fn mod_model_geometry(&self, camera: DVec3, alpha: f64, out: &mut Vec<Vertex>) {
pub(super) fn mod_model_geometry(&mut self, camera: DVec3, alpha: f64, out: &mut Vec<Vertex>) {
self.mods.models.motion.clear();
if self.mods.models.poses.is_empty() {
return;
}
let motion = self.mod_hd().is_some();
let t = alpha.clamp(0.0, 1.0);
// Missiles are seen from far off: past the far plane a model is drawn nearer and
// smaller, the same size on screen.
@@ -422,10 +438,11 @@ impl Game<'_> {
Some(([r.u0, r.v0, r.u1, r.v1], m))
})
.collect();
for p in self.mods.models.poses.values() {
for (key, p) in self.mods.models.poses.iter().map(|(key, pose)| (*key, *pose)).collect::<Vec<_>>() {
let mut at = p.previous.lerp(p.position, t);
let d = at.distance(camera);
if d > 4096.0 {
self.mods.models.poses.get_mut(&key).unwrap().rendered = None;
continue;
}
let mut shrink = 1.0f32;
@@ -436,8 +453,11 @@ impl Game<'_> {
let rotation = p.old_rotation.slerp(p.rotation, t as f32);
let scale = (p.old_scale + (p.scale - p.old_scale) * t as f32) * shrink;
if scale <= 0.0 {
self.mods.models.poses.get_mut(&key).unwrap().rendered = None;
continue;
}
let rendered = RenderedPose { at, rotation, scale };
let previous = p.rendered.unwrap_or(rendered);
let light = if p.glow {
GLOW
} else {
@@ -481,6 +501,22 @@ impl Game<'_> {
}
}
}
if motion {
let previous_matrix = Mat4::from_scale_rotation_translation(
Vec3::splat(previous.scale),
previous.rotation,
(previous.at - camera).as_vec3(),
) * Mat4::from_translation(Vec3::splat(-0.5));
let mut old = Vec::with_capacity(out.len() - start);
self.block_model_vertices(p.state, 0, previous_matrix, light, true, &mut old);
self.mods.models.motion.extend(
out[start..]
.iter()
.zip(old)
.map(|(current, previous)| MotionVertex::new(*current, previous.pos)),
);
}
self.mods.models.poses.get_mut(&key).unwrap().rendered = Some(rendered);
}
}
+79 -1
View File
@@ -6,7 +6,7 @@ use crate::{
environment::Sky,
graphics::GraphicsApi,
hud::{HudBatch, HudRenderer},
mesher::{SectionMesh, Sides, TerrainVertex, Vertex},
mesher::{MotionVertex, SectionMesh, Sides, TerrainVertex, Vertex},
sprites::SpriteAnimation,
};
use glam::{DVec3, Mat4, Vec3, Vec4};
@@ -315,6 +315,8 @@ pub struct Frame<'a> {
pub nether_lighting: bool,
/// Camera-relative entity geometry textured from the shared atlas.
pub entities: &'a [Vertex],
/// Mod models with their previous drawn transforms, for the HD motion-only pass.
pub motion_models: &'a [MotionVertex],
/// The glowing entities' silhouettes in their team colours (`OutlineBufferSource`),
/// outlined through walls after the hand.
pub glowing: &'a [Vertex],
@@ -605,6 +607,9 @@ pub struct Renderer {
/// The water quads alone into the HD water targets (section draws; `flat.water` for flat ones).
gbuffer_water: wgpu::RenderPipeline,
gbuffer_entity: wgpu::RenderPipeline,
gbuffer_motion_models: wgpu::RenderPipeline,
motion_models_buffer: wgpu::Buffer,
motion_models_capacity: u64,
/// The HD sun shadows: depth only, from the light, and the sections in its farther cascade.
shadow_terrain: wgpu::RenderPipeline,
/// The near cascade's depth without leaves, and its colour map's leaves and glass
@@ -2181,6 +2186,12 @@ impl Renderer {
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &entity_attributes,
})];
let motion_attributes = wgpu::vertex_attr_array![0=>Float32x3,1=>Float32x2,2=>Uint32,3=>Unorm8x4,4=>Uint8x4,5=>Snorm8x4,6=>Float32x2,7=>Float32x3];
let motion_vertex = [Some(wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<MotionVertex>() as u64,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &motion_attributes,
})];
#[allow(clippy::too_many_arguments)]
let entity_pipeline_with = |label,
cull_mode,
@@ -2252,6 +2263,37 @@ impl Renderer {
multiview_mask: None,
cache: None,
});
let gbuffer_motion_models = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("HD motion models"),
layout: Some(&entity_layout),
vertex: wgpu::VertexState {
module: &entity_shader,
entry_point: Some("vs_entity_motion"),
compilation_options: Default::default(),
buffers: &motion_vertex,
},
primitive: Default::default(),
depth_stencil: Some(wgpu::DepthStencilState {
format: wgpu::TextureFormat::Depth32Float,
depth_write_enabled: Some(false),
depth_compare: Some(wgpu::CompareFunction::Equal),
stencil: Default::default(),
bias: Default::default(),
}),
multisample: Default::default(),
fragment: Some(wgpu::FragmentState {
module: &entity_shader,
entry_point: Some("fs_entity_motion"),
compilation_options: Default::default(),
targets: &[Some(wgpu::ColorTargetState {
format: hd::MOTION,
blend: None,
write_mask: wgpu::ColorWrites::ALL,
})],
}),
multiview_mask: None,
cache: None,
});
let shadow_entity = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("HD shadows entities"),
layout: Some(&entity_layout),
@@ -2770,6 +2812,13 @@ impl Renderer {
usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
let motion_models_capacity = 1 << 10;
let motion_models_buffer = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("Motion model vertices"),
size: motion_models_capacity * std::mem::size_of::<MotionVertex>() as u64,
usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
let flat_wanted = flat::wanted(&adapter, &device);
// Readable as storage where flat draws run: a shader pack's terrain program reads them.
let arena = arena::Arena::new(
@@ -3014,6 +3063,9 @@ impl Renderer {
hd_materials,
gbuffer_water,
gbuffer_entity,
gbuffer_motion_models,
motion_models_buffer,
motion_models_capacity,
shadow_terrain,
shadow_terrain_near,
shadow_tint_leaves,
@@ -5130,6 +5182,24 @@ impl Renderer {
bytemuck::cast_slice(all_entities),
);
}
let motion_vertices = frame.motion_models.len() as u64;
if motion_vertices > self.motion_models_capacity {
self.motion_models_capacity = motion_vertices.next_power_of_two();
self.motion_models_buffer = self.device.create_buffer(&wgpu::BufferDescriptor {
label: Some("Motion model vertices"),
size: self.motion_models_capacity * std::mem::size_of::<MotionVertex>() as u64,
usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
}
if motion_vertices > 0 {
self.uploads.buffer(
&mut encoder,
&self.motion_models_buffer,
0,
bytemuck::cast_slice(frame.motion_models),
);
}
self.entity_outline.prepare(
&self.device,
&mut self.uploads,
@@ -5607,6 +5677,14 @@ impl Renderer {
pass.draw_indexed(0..entity_quads * 6, 0, 0..1);
}
drop(pass);
if motion_vertices > 0 {
let mut pass = hd.motion_models_pass(&mut encoder);
pass.set_pipeline(&self.gbuffer_motion_models);
pass.set_bind_group(0, &self.entity_group, &[]);
pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32);
pass.set_vertex_buffer(0, self.motion_models_buffer.slice(..));
pass.draw_indexed(0..motion_vertices as u32 / 4 * 6, 0, 0..1);
}
// Contact shadows from the G-buffer's depth and normals, for the lighting.
hd.ao(&mut encoder);
// The water quads alone, with their own depth: the lighting finds the water by it and
+33 -2
View File
@@ -8,8 +8,8 @@
//! - `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.
//! projections as actually drawn, jitter included. Mod models overwrite camera/static motion with
//! their previous drawn transforms in `motion_models_pass`; vanilla entities and wind remain camera-only.
//! - `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
@@ -86,4 +86,35 @@ impl Hd {
})
.forget_lifetime()
}
/// Overwrite only motion for mod models with previous drawn transforms; depth stays read-only.
pub fn motion_models_pass<'e>(
&mut self,
encoder: &'e mut wgpu::CommandEncoder,
) -> wgpu::RenderPass<'e> {
encoder
.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("HD motion models"),
timestamp_writes: stamp(&mut self.timing, "hd motion models"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: &self.gbuffer.motion,
resolve_target: None,
depth_slice: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Load,
store: wgpu::StoreOp::Store,
},
})],
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
view: &self.gbuffer.depth,
depth_ops: Some(wgpu::Operations {
load: wgpu::LoadOp::Load,
store: wgpu::StoreOp::Store,
}),
stencil_ops: None,
}),
..Default::default()
})
.forget_lifetime()
}
}
+11 -1
View File
@@ -31,6 +31,12 @@ pub fn taa_enabled() -> bool {
*ON.get_or_init(|| std::env::var("HORIZON_HD_TAA").map_or(true, |v| v != "0"))
}
/// Visualise opaque motion instead of resolving history (`HORIZON_HD_DEBUG_MOTION=1`).
pub fn debug_motion() -> bool {
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*ON.get_or_init(|| std::env::var("HORIZON_HD_DEBUG_MOTION").is_ok_and(|v| v == "1"))
}
/// The Halton sequence's `index`-th number in `base`, in (0, 1).
fn halton(mut index: u32, base: u32) -> f32 {
let (mut f, mut r) = (1.0, 0.0);
@@ -118,6 +124,7 @@ impl Taa {
entries: &entries(&TAA_BINDINGS),
});
let taa_module = module(device, "HD TAA", TAA);
let constants = [("DEBUG_MOTION", if debug_motion() { 1.0 } else { 0.0 })];
let pipeline = |entry: &str| {
let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("HD TAA"),
@@ -141,7 +148,10 @@ impl Taa {
fragment: Some(wgpu::FragmentState {
module: &taa_module,
entry_point: Some(entry),
compilation_options: Default::default(),
compilation_options: wgpu::PipelineCompilationOptions {
constants: &constants,
..Default::default()
},
targets: &[target.clone(), target],
}),
primitive: Default::default(),
+17
View File
@@ -21,6 +21,11 @@ fn history_motion(px: vec2<i32>, world_depth: f32) -> vec3<f32> {
return vec3<f32>(0.0);
}
override DEBUG_MOTION: bool = false;
fn debug_motion(motion: vec3<f32>) -> vec4<f32> {
return vec4<f32>(abs(motion.xy) * 0.25, motion.z, 1.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);
}
@@ -113,6 +118,12 @@ fn fs_taa(in: FullOut) -> TaaOut {
if motion.z > 0.5 {
uv = in.uv + motion.xy / vec2<f32>(size);
}
if DEBUG_MOTION {
var out: TaaOut;
out.color = debug_motion(motion);
out.history = vec4<f32>(0.0);
return out;
}
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;
@@ -215,6 +226,12 @@ fn fs_taau(in: FullOut) -> TaaOut {
if motion.z > 0.5 {
uv = in.uv + motion.xy / low;
}
if DEBUG_MOTION {
var out: TaaOut;
out.color = debug_motion(motion);
out.history = vec4<f32>(0.0);
return out;
}
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;
+30
View File
@@ -44,6 +44,36 @@ pub struct Vertex {
}
/// `Vertex::normal` of a baked-colour quad of an `entitySolid` model: the render type culls
/// back faces, which the entity pipelines here do not, so `entity.wgsl` discards them.
/// A mod-model vertex with its previous drawn world pose. Kept out of `Vertex` so ordinary
/// entities do not pay for a second position they do not have.
#[repr(C)]
#[derive(Clone, Copy, Debug, bytemuck::Pod, bytemuck::Zeroable)]
pub struct MotionVertex {
pub pos: [f32; 3],
pub uv: [f32; 2],
pub layer: u32,
pub color: u32,
pub light: u32,
pub normal: [i8; 4],
pub glint_uv: [f32; 2],
pub previous: [f32; 3],
}
impl MotionVertex {
pub fn new(vertex: Vertex, previous: [f32; 3]) -> Self {
Self {
pos: vertex.pos,
uv: vertex.uv,
layer: vertex.layer,
color: vertex.color,
light: vertex.light,
normal: vertex.normal,
glint_uv: vertex.glint_uv,
previous,
}
}
}
pub const CULL_BACK: [i8; 4] = [0, 0, 0, 1];
/// A section vertex: vanilla's `DefaultVertexFormat.BLOCK` (position, colour,
/// UV0, UV2) with the sprite's array layer beside the light, in 24 bytes