From df58a474fbd654e7d74b7c4298e1278bbbe611dd Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 12:05:22 +0200 Subject: [PATCH] Terrain vertices in 24 bytes: the UV as 16-bit fixed point TerrainVertex goes from 28 to 24 bytes: the UV becomes two u16 in 32768 steps per unit, which hold model UVs (multiples of 1/16 and their halves) exactly and decode exactly in terrain.wgsl. The position stays a float: fixed point moves the edges of offset plants and rotated elements by a fraction of a pixel. Section vertex memory drops 14% (RD 16: 380 -> 325 MB, arena 503 -> 369 MB); pixels are unchanged on Metal and Vulkan outside animated water. Co-Authored-By: Claude Opus 5.5 --- .../examples/section_mesh_fixture.rs | 11 +-- crates/horizon-client/src/gfx.rs | 4 +- crates/horizon-client/src/mesher.rs | 68 +++++++++++++++---- crates/horizon-client/src/mesher/facing.rs | 8 +-- crates/horizon-client/src/mesher/fluid.rs | 8 +-- crates/horizon-client/src/terrain.wgsl | 13 ++-- 6 files changed, 71 insertions(+), 41 deletions(-) diff --git a/crates/horizon-client/examples/section_mesh_fixture.rs b/crates/horizon-client/examples/section_mesh_fixture.rs index 6359f32d0..3f0caaf12 100644 --- a/crates/horizon-client/examples/section_mesh_fixture.rs +++ b/crates/horizon-client/examples/section_mesh_fixture.rs @@ -159,15 +159,8 @@ fn scene( let vertices: Vec = quad .iter() .map(|v| { - json!([ - v.pos[0], - v.pos[1], - v.pos[2], - v.uv[0], - v.uv[1], - argb(v.color), - light_coords(v.light) - ]) + let ([x, y, z], [u, w]) = (v.pos, v.uv()); + json!([x, y, z, u, w, argb(v.color), light_coords(v.light)]) }) .collect(); quads.push(json!({"layer": layer, "sprite": sprite, "vertices": vertices})); diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 22d88c770..dcc3f417d 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -24,7 +24,7 @@ const CRUMBLING_BLEND: wgpu::BlendState = wgpu::BlendState { }; /// `TerrainVertex` for `vs_terrain`: position, UV, layer, light, colour. const TERRAIN_ATTRIBUTES: [wgpu::VertexAttribute; 5] = - wgpu::vertex_attr_array![0=>Float32x3,1=>Float32x2,2=>Uint16,4=>Uint8x2,3=>Unorm8x4]; + wgpu::vertex_attr_array![0=>Float32x3,1=>Uint16x2,2=>Uint16,4=>Uint8x2,3=>Unorm8x4]; /// entity.wgsl after the OverlayTexture constants it reads. fn entity_shader() -> String { @@ -3521,7 +3521,7 @@ mod tests { (3, offset_of!(TerrainVertex, color)), ] ); - assert_eq!(std::mem::size_of::(), 28); + assert_eq!(std::mem::size_of::(), 24); } #[test] fn cracks_multiply_instead_of_covering_the_block() { diff --git a/crates/horizon-client/src/mesher.rs b/crates/horizon-client/src/mesher.rs index c3990f598..5e657e82c 100644 --- a/crates/horizon-client/src/mesher.rs +++ b/crates/horizon-client/src/mesher.rs @@ -39,16 +39,22 @@ pub struct Vertex { /// RGBA8_SNORM face normal; w = -127 for two-sided lighting, 0 to keep baked/unlit colour. pub normal: [i8; 4], } -/// A section vertex, 28 bytes like vanilla's `DefaultVertexFormat.BLOCK` -/// (position, colour, UV0, UV2) with the sprite's array layer beside the -/// light. Terrain has no normal: `ModelBlockRenderer` bakes the face shade -/// into the colour and `terrain.vsh` only multiplies it by the lightmap. +/// A section vertex: vanilla's `DefaultVertexFormat.BLOCK` (position, colour, +/// UV0, UV2) with the sprite's array layer beside the light, in 24 bytes +/// rather than vanilla's 28: the UV is fixed point, which holds model UVs +/// (multiples of 1/16 and their halves) exactly. The position stays a float: +/// fixed point would move the edges of offset plants and rotated elements by +/// a fraction of a pixel. Terrain has no normal: `ModelBlockRenderer` bakes +/// the face shade into the colour and `terrain.vsh` only multiplies it by the +/// lightmap. #[repr(C)] #[derive(Clone, Copy, Debug, bytemuck::Pod, bytemuck::Zeroable)] pub struct TerrainVertex { /// Section-relative position. pub pos: [f32; 3], - pub uv: [f32; 2], + /// UV within the sprite, `UV_STEPS` per unit (`TerrainVertex::new`, + /// `terrain.wgsl`'s `terrain_uv`). + pub(crate) uv: [u16; 2], /// Sprite array layer, with `OPAQUE_LAYER` for opaque leaves. pub layer: u16, /// Smooth light, 16x fixed point: sky in the low byte, block in the high one. @@ -56,6 +62,44 @@ pub struct TerrainVertex { /// RGBA8: tint multiplied by directional shade and ambient occlusion. pub color: u32, } +/// Fixed-point steps per unit of `TerrainVertex` UVs, which span `[0, 2)`. +pub const UV_STEPS: f32 = 32768.0; +impl TerrainVertex { + pub fn new(pos: [f32; 3], uv: [f32; 2], layer: u16, color: u32, light: u16) -> Self { + Self { + pos, + uv: uv.map(|v| (v * UV_STEPS).round().clamp(0.0, f32::from(u16::MAX)) as u16), + layer, + light, + color, + } + } + /// The UV the GPU samples. + pub fn uv(&self) -> [f32; 2] { + self.uv.map(|v| f32::from(v) / UV_STEPS) + } +} +#[cfg(test)] +mod vertex_tests { + use super::TerrainVertex; + #[test] + fn model_uvs_pack_exactly() { + for i in 0..=32 { + let u = i as f32 / 32.0; + let v = TerrainVertex::new([0.0; 3], [u, 1.0 - u], 0, 0, 0); + assert_eq!(v.uv(), [u, 1.0 - u]); + } + } + #[test] + fn other_uvs_round_within_a_step() { + // A flowing fluid's rotated UV. + let uv = [0.5 + 0.25 * 0.3f32.sin(), 0.5 - 0.25 * 0.3f32.cos()]; + let packed = TerrainVertex::new([0.0; 3], uv, 0, 0, 0).uv(); + for (a, b) in packed.iter().zip(uv) { + assert!((a - b).abs() <= 0.5 / 32768.0, "{a} {b}"); + } + } +} pub struct SectionMesh { pub solid: Vec, /// `solid`'s quads by the side they face (`facing`). @@ -786,18 +830,18 @@ impl Mesher<'_> { None => [g; 3], }; let p = q.positions[i]; - target.push(TerrainVertex { - pos: [p[0] + origin[0], p[1] + origin[1], p[2] + origin[2]], - uv: q.uv[i], + target.push(TerrainVertex::new( + [p[0] + origin[0], p[1] + origin[1], p[2] + origin[2]], + q.uv[i], // Opaque leaves (forceOpaque) draw in the solid layer: no alpha cutout. - layer: if !self.cutout_leaves && state.leaves { + if !self.cutout_leaves && state.leaves { face.layer as u16 | OPAQUE_LAYER } else { face.layer as u16 }, - color: pack_rgb(rgb), - light: pack_coords(lighting::with_emission(coords[i], q.light_emission)), - }); + pack_rgb(rgb), + pack_coords(lighting::with_emission(coords[i], q.light_emission)), + )); } } } diff --git a/crates/horizon-client/src/mesher/facing.rs b/crates/horizon-client/src/mesher/facing.rs index 2a7f0ba61..e82ad6a06 100644 --- a/crates/horizon-client/src/mesher/facing.rs +++ b/crates/horizon-client/src/mesher/facing.rs @@ -175,13 +175,7 @@ mod tests { use super::*; fn vertex(pos: [f32; 3], layer: u16) -> TerrainVertex { - TerrainVertex { - pos, - uv: [0.0; 2], - layer, - light: 0, - color: 0, - } + TerrainVertex::new(pos, [0.0; 2], layer, 0, 0) } /// A unit quad in the plane `axis = plane`, wound to face `sign`, tagged /// with `layer` so tests can follow it. diff --git a/crates/horizon-client/src/mesher/fluid.rs b/crates/horizon-client/src/mesher/fluid.rs index c29f2b92c..6e42685ba 100644 --- a/crates/horizon-client/src/mesher/fluid.rs +++ b/crates/horizon-client/src/mesher/fluid.rs @@ -306,13 +306,7 @@ pub(super) fn tesselate( }; let mut quad = |v: [([f32; 3], [f32; 2]); 4], layer: u16, color: u32, light: u16, back: bool| { - let vertex = |(pos, uv): ([f32; 3], [f32; 2])| TerrainVertex { - pos, - uv, - layer, - color, - light, - }; + let vertex = |(pos, uv)| TerrainVertex::new(pos, uv, layer, color, light); target.extend(v.map(vertex)); if back { target.extend([v[0], v[3], v[2], v[1]].map(vertex)); diff --git a/crates/horizon-client/src/terrain.wgsl b/crates/horizon-client/src/terrain.wgsl index 504d197aa..7d25cef2f 100644 --- a/crates/horizon-client/src/terrain.wgsl +++ b/crates/horizon-client/src/terrain.wgsl @@ -72,11 +72,16 @@ fn sample_lightmap(sky: f32, block: f32) -> vec4 { return textureSampleLevel(lightmap, lightmap_sampler, clamp(uv / 256.0 + 0.5 / 16.0, vec2(0.5 / 16.0), vec2(15.5 / 16.0)), 0.0); } +// mesher.rs's TerrainVertex holds the UV in 32768 steps per unit: a power of +// two on an integer below 2^24, so it decodes exactly. +fn terrain_uv(packed: vec2) -> vec2 { + return vec2(packed) * (1.0 / 32768.0); +} @vertex fn vs_terrain( - @location(0) position: vec3, @location(1) uv: vec2, + @location(0) position: vec3, @location(1) uv: vec2, @location(2) layer: u32, @location(3) color: vec4, @location(4) light: vec2, @location(5) origin: vec4) -> TerrainOut { - return terrain_vertex(position, uv, layer, color, light, origin.xyz); + return terrain_vertex(position, terrain_uv(uv), layer, color, light, origin.xyz); } // Flat section draws (gfx/flat.rs): one draw per arena page over the frame's // index list. A section's vertices fill whole 256-vertex blocks of its page: @@ -85,11 +90,11 @@ fn sample_lightmap(sky: f32, block: f32) -> vec4 { @group(1) @binding(1) var slot_origins: array>; @vertex fn vs_terrain_flat( @builtin(vertex_index) vertex: u32, - @location(0) position: vec3, @location(1) uv: vec2, + @location(0) position: vec3, @location(1) uv: vec2, @location(2) layer: u32, @location(3) color: vec4, @location(4) light: vec2 ) -> TerrainOut { let origin = slot_origins[block_slots[vertex >> 8u]]; - return terrain_vertex(position, uv, layer, color, light, origin.xyz); + return terrain_vertex(position, terrain_uv(uv), layer, color, light, origin.xyz); } fn terrain_vertex(position: vec3, uv: vec2, layer: u32, color: vec4, light: vec2, origin: vec3) -> TerrainOut {