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 <noreply@anthropic.com>
This commit is contained in:
@@ -159,15 +159,8 @@ fn scene(
|
||||
let vertices: Vec<Value> = 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}));
|
||||
|
||||
@@ -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::<TerrainVertex>(), 28);
|
||||
assert_eq!(std::mem::size_of::<TerrainVertex>(), 24);
|
||||
}
|
||||
#[test]
|
||||
fn cracks_multiply_instead_of_covering_the_block() {
|
||||
|
||||
@@ -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<TerrainVertex>,
|
||||
/// `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)),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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));
|
||||
|
||||
@@ -72,11 +72,16 @@ fn sample_lightmap(sky: f32, block: f32) -> vec4<f32> {
|
||||
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<u32>) -> vec2<f32> {
|
||||
return vec2<f32>(packed) * (1.0 / 32768.0);
|
||||
}
|
||||
@vertex fn vs_terrain(
|
||||
@location(0) position: vec3<f32>, @location(1) uv: vec2<f32>,
|
||||
@location(0) position: vec3<f32>, @location(1) uv: vec2<u32>,
|
||||
@location(2) layer: u32, @location(3) color: vec4<f32>, @location(4) light: vec2<u32>,
|
||||
@location(5) origin: vec4<f32>) -> 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<f32> {
|
||||
@group(1) @binding(1) var<storage, read> slot_origins: array<vec4<f32>>;
|
||||
@vertex fn vs_terrain_flat(
|
||||
@builtin(vertex_index) vertex: u32,
|
||||
@location(0) position: vec3<f32>, @location(1) uv: vec2<f32>,
|
||||
@location(0) position: vec3<f32>, @location(1) uv: vec2<u32>,
|
||||
@location(2) layer: u32, @location(3) color: vec4<f32>, @location(4) light: vec2<u32>
|
||||
) -> 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<f32>, uv: vec2<f32>, layer: u32, color: vec4<f32>,
|
||||
light: vec2<u32>, origin: vec3<f32>) -> TerrainOut {
|
||||
|
||||
Reference in New Issue
Block a user