Reuse cloud geometry and GPU buffers between frames

This commit is contained in:
LookImEcho-Rick
2026-10-16 05:45:09 -05:00
parent 8da9c8c4c6
commit 56fec974ee
5 changed files with 355 additions and 150 deletions
+213
View File
@@ -68,9 +68,222 @@ pub fn cells(radius: i32) -> impl Iterator<Item = (i32, i32)> {
})
}
/// Per-frame cloud inputs. Resources are immutable for the renderer's lifetime:
/// resource-pack reloads replace the renderer and its cache together.
pub struct Input<'a> {
pub texture: &'a (u32, u32, Vec<bool>),
pub uv: [f32; 2],
pub camera: glam::DVec3,
pub time: i64,
pub partial_tick: f64,
pub range: i32,
pub fast: bool,
pub color: [f32; 4],
}
#[derive(Debug, PartialEq, Eq)]
struct MeshKey {
cell_x: i64,
cell_z: i64,
radius: i32,
fast: bool,
// The two vertical face tests include both surfaces at the boundaries.
top: bool,
bottom: bool,
}
#[repr(C)]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
pub(crate) struct Uniform {
offset: [f32; 4],
colors: [[f32; 4]; 4],
}
impl Input<'_> {
fn state(&self) -> (MeshKey, Uniform) {
let (w, h, _) = self.texture;
let offset = self.time.rem_euclid(i64::from(*w) * 400) as f64 + self.partial_tick;
let mut x = self.camera.x + offset * 0.030_000_001;
let mut z = self.camera.z + 3.96;
let (tw, th) = (f64::from(*w) * 12.0, f64::from(*h) * 12.0);
x -= (x / tw).floor() * tw;
z -= (z / th).floor() * th;
let cell_x = (x / 12.0).floor() as i64;
let cell_z = (z / 12.0).floor() as i64;
let bottom = (192.33 - self.camera.y) as f32;
let key = MeshKey {
cell_x,
cell_z,
radius: radius_cells(self.range),
fast: self.fast,
top: bottom <= 0.0,
bottom: bottom + 4.0 >= 0.0,
};
let colors = [1.0, 0.7, 0.8, 0.9].map(|shade| {
self.color
.map(|v| ((v * shade).clamp(0.0, 1.0) * 255.0) as u8 as f32 / 255.0)
});
(
key,
Uniform {
offset: [
-(x - cell_x as f64 * 12.0) as f32,
bottom,
-(z - cell_z as f64 * 12.0) as f32,
0.0,
],
colors,
},
)
}
}
#[derive(Default)]
pub(crate) struct Mesh {
key: Option<MeshKey>,
pub vertices: Vec<crate::mesher::Vertex>,
}
impl Mesh {
/// Returns true only when vertices must be uploaded. Sub-cell motion and
/// color changes update only the uniform, as in CloudRenderer.prepare.
pub fn prepare(&mut self, input: &Input<'_>) -> (bool, Uniform) {
let (key, uniform) = input.state();
if self.key.as_ref() == Some(&key) {
return (false, uniform);
}
self.vertices.clear();
let (w, h, cells) = input.texture;
let (w, h) = (i64::from(*w), i64::from(*h));
let (cell_x, cell_z, radius) = (key.cell_x, key.cell_z, key.radius);
let bottom = uniform.offset[1];
let uv = input.uv;
let filled = |cx: i64, cz: i64| cells[(cz.rem_euclid(h) * w + cx.rem_euclid(w)) as usize];
for (dx, dz) in self::cells(radius) {
let (dx, dz) = (i64::from(dx), i64::from(dz));
let (cx, cz) = (cell_x + dx, cell_z + dz);
if !filled(cx, cz) {
continue;
}
let x0 = dx as f32 * 12.0;
let z0 = dz as f32 * 12.0;
let (x1, z1, y0, y1) = (x0 + 12.0, z0 + 12.0, 0.0, 4.0);
let mut quad = |p: [[f32; 3]; 4], shade: u32| {
for v in p {
// Overlay 4: clouds (unlit, faded by the cloud fog end).
self.vertices.push(crate::mesher::Vertex {
normal: [0; 4],
pos: v,
uv,
layer: 4,
color: u32::MAX,
light: shade,
});
}
};
if input.fast {
for p in flat_cell(x0, z0, 0.0) {
quad(p, 0);
}
continue;
}
let quads = [
([[x1, y0, z0], [x1, y0, z1], [x0, y0, z1], [x0, y0, z0]], 1),
([[x0, y1, z0], [x0, y1, z1], [x1, y1, z1], [x1, y1, z0]], 0),
([[x0, y0, z0], [x0, y1, z0], [x1, y1, z0], [x1, y0, z0]], 2),
([[x1, y0, z1], [x1, y1, z1], [x0, y1, z1], [x0, y0, z1]], 2),
([[x0, y0, z1], [x0, y1, z1], [x0, y1, z0], [x0, y0, z0]], 3),
([[x1, y0, z0], [x1, y1, z0], [x1, y1, z1], [x1, y0, z1]], 3),
];
let mask = face_mask(
dx as i32,
dz as i32,
bottom,
[
!filled(cx, cz - 1),
!filled(cx, cz + 1),
!filled(cx - 1, cz),
!filled(cx + 1, cz),
],
);
for (direction, inside) in faces(mask) {
let (p, shade) = quads[usize::from(direction)];
quad(if inside { [p[3], p[2], p[1], p[0]] } else { p }, shade);
}
}
self.key = Some(key);
(true, uniform)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn cached_mesh_tracks_geometry_but_not_subcell_motion_or_color() {
let texture = (8, 8, (0..64).map(|i| i % 3 != 0).collect());
let mut input = Input {
texture: &texture,
uv: [0.5; 2],
camera: glam::DVec3::new(1.0, 100.0, 1.0),
time: 0,
partial_tick: 0.0,
range: 2,
fast: false,
color: [1.0; 4],
};
let mut mesh = Mesh::default();
assert!(mesh.prepare(&input).0);
let vertices = bytemuck::cast_slice::<_, u8>(&mesh.vertices).to_vec();
let pointer = mesh.vertices.as_ptr();
for tick in 0..100 {
input.time = tick;
input.partial_tick = 0.5;
input.camera.x = 2.0;
input.camera.y = 101.0;
input.color = [0.8, 0.7, 0.6, 0.5];
let (rebuilt, uniform) = mesh.prepare(&input);
assert!(!rebuilt);
assert_eq!(mesh.vertices.as_ptr(), pointer);
assert_eq!(bytemuck::cast_slice::<_, u8>(&mesh.vertices), vertices);
assert_eq!(uniform.offset[1], (192.33 - input.camera.y) as f32);
assert_eq!(
uniform.colors[1],
[0.8, 0.7, 0.6, 0.5].map(|v| ((v * 0.7f32) * 255.0) as u8 as f32 / 255.0)
);
}
for (x, y, z, range, fast) in [
(13.0, 101.0, 1.0, 2, false),
(13.0, 101.0, -5.0, 2, false),
(13.0, 192.33, -5.0, 2, false),
(13.0, 196.34, -5.0, 2, false),
(13.0, 196.34, -5.0, 3, false),
(13.0, 196.34, -5.0, 3, true),
] {
input.camera = glam::DVec3::new(x, y, z);
input.range = range;
input.fast = fast;
assert!(mesh.prepare(&input).0);
let mut fresh = Mesh::default();
fresh.prepare(&input);
assert_eq!(
bytemuck::cast_slice::<_, u8>(&mesh.vertices),
bytemuck::cast_slice::<_, u8>(&fresh.vertices)
);
assert!(!mesh.prepare(&input).0);
}
// Drift alone crosses a cell, even with a stationary camera.
input.time += 400;
assert!(mesh.prepare(&input).0);
// Resource reload constructs a new renderer and cache, including empty packs.
let mut reloaded = Mesh::default();
let empty = (1, 1, vec![false]);
input.texture = &empty;
assert!(reloaded.prepare(&input).0);
assert!(reloaded.vertices.is_empty());
assert!(!reloaded.prepare(&input).0);
}
#[test]
fn settings_and_packed_alpha_gate_clouds() {
for mode in ["false", "fast", "true"] {
+15 -2
View File
@@ -58,8 +58,8 @@ fn overlay_texel(overlay: u32) -> vec4<f32> {
if overlay >= 16u && overlay < 32u { return vec4(1.0, 1.0, 1.0, WHITE_OVERLAY_ALPHA[overlay - 16u]); }
return vec4(1.0);
}
@vertex fn vs_entity(@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>) -> Out {
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.proj = vec4(out.clip.xy * 0.5 + out.clip.w * 0.5, out.clip.zw);
@@ -90,6 +90,19 @@ fn overlay_texel(overlay: u32) -> vec4<f32> {
if overlay == 4u { out.spherical = length(position); }
return out;
}
@vertex fn vs_entity(@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>) -> Out {
return entity_vertex(position, uv, overlay, color, light, normal);
}
struct CloudInfo {
offset: vec4<f32>,
colors: array<vec4<f32>, 4>,
}
@group(1) @binding(0) var<uniform> cloud: CloudInfo;
@vertex fn vs_cloud(@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>) -> Out {
return entity_vertex(position + cloud.offset.xyz, uv, overlay, cloud.colors[light.x], light, normal);
}
// rendertype_end_portal.fsh: the layer colours.
const PORTAL_COLORS = array<vec3<f32>, 16>(
vec3(0.022087, 0.098399, 0.110818),
+19 -117
View File
@@ -6016,112 +6016,6 @@ impl Game<'_> {
}
out
}
/// CloudRenderer: flat or 12x4x12 extruded cells drifting east.
fn cloud_geometry(
&self,
sky: &horizon_client::environment::Sky,
camera: glam::DVec3,
alpha: f64,
out: &mut Vec<Vertex>,
) {
let (w, h, cells) = &self.res.clouds;
let (w, h) = (i64::from(*w), i64::from(*h));
let Some(white) = self.res.atlas.sprites.get("horizon/white").copied() else {
return;
};
let uv = [(white.u0 + white.u1) / 2.0, (white.v0 + white.v1) / 2.0];
let cloud_height = 192.33;
let offset = (self.world.game_time.rem_euclid(w * 400)) as f64 + alpha;
let mut cloud_x = camera.x + offset * 0.030_000_001;
let mut cloud_z = camera.z + 3.96;
let (tw, th) = (w as f64 * 12.0, h as f64 * 12.0);
cloud_x -= (cloud_x / tw).floor() * tw;
cloud_z -= (cloud_z / th).floor() * th;
let cell_x = (cloud_x / 12.0).floor() as i64;
let cell_z = (cloud_z / 12.0).floor() as i64;
let x_in = (cloud_x - cell_x as f64 * 12.0) as f32;
let z_in = (cloud_z - cell_z as f64 * 12.0) as f32;
let bottom = (cloud_height - camera.y) as f32;
let filled = |cx: i64, cz: i64| cells[(cz.rem_euclid(h) * w + cx.rem_euclid(w)) as usize];
// The Cloud Distance option, not the render distance.
let radius = horizon_client::clouds::radius_cells(self.settings.cloud_range);
let c = sky.cloud_color;
let color = |shade: f32| {
let ch = |v: f32| ((v * shade).clamp(0.0, 1.0) * 255.0) as u32;
ch(c[3].clamp(0.0, 1.0)) << 24 | ch(c[2]) << 16 | ch(c[1]) << 8 | ch(c[0])
};
let light = 240 | 240 << 8;
for (dx, dz) in horizon_client::clouds::cells(radius) {
let (dx, dz) = (i64::from(dx), i64::from(dz));
let (cx, cz) = (cell_x + dx, cell_z + dz);
if !filled(cx, cz) {
continue;
}
let x0 = dx as f32 * 12.0 - x_in;
let z0 = dz as f32 * 12.0 - z_in;
let (x1, z1, y0, y1) = (x0 + 12.0, z0 + 12.0, bottom, bottom + 4.0);
let mut quad = |p: [[f32; 3]; 4], shade: f32| {
for v in p {
// Overlay 4: clouds (unlit, faded by the cloud fog end).
out.push(Vertex {
normal: [0; 4],
pos: v,
uv,
layer: 4,
color: color(shade),
light,
});
}
};
if self.settings.clouds == "fast" {
for p in horizon_client::clouds::flat_cell(x0, z0, bottom) {
quad(p, 1.0);
}
continue;
}
let faces = [
(
[[x1, y0, z0], [x1, y0, z1], [x0, y0, z1], [x0, y0, z0]],
0.7,
),
(
[[x0, y1, z0], [x0, y1, z1], [x1, y1, z1], [x1, y1, z0]],
1.0,
),
(
[[x0, y0, z0], [x0, y1, z0], [x1, y1, z0], [x1, y0, z0]],
0.8,
),
(
[[x1, y0, z1], [x1, y1, z1], [x0, y1, z1], [x0, y0, z1]],
0.8,
),
(
[[x0, y0, z1], [x0, y1, z1], [x0, y1, z0], [x0, y0, z0]],
0.9,
),
(
[[x1, y0, z0], [x1, y1, z0], [x1, y1, z1], [x1, y0, z1]],
0.9,
),
];
let mask = horizon_client::clouds::face_mask(
dx as i32,
dz as i32,
bottom,
[
!filled(cx, cz - 1),
!filled(cx, cz + 1),
!filled(cx - 1, cz),
!filled(cx + 1, cz),
],
);
for (direction, inside) in horizon_client::clouds::faces(mask) {
let (p, shade) = faces[usize::from(direction)];
quad(if inside { [p[3], p[2], p[1], p[0]] } else { p }, shade);
}
}
}
/// Load (or create) a world folder under saves/ and start streaming it.
fn open_world(
&mut self,
@@ -8538,17 +8432,10 @@ impl Game<'_> {
if !title {
self.end_geometry(camera, &mut entities);
}
if (!title || live) && self.dim == Dim::Overworld {
if horizon_client::clouds::visible(&self.settings.clouds, sky.cloud_color[3]) {
horizon_client::spans::time("clouds", || {
self.cloud_geometry(&sky, camera, alpha, &mut entities)
});
}
if self.settings.horizon.weather {
horizon_client::spans::time("weather", || {
self.weather_geometry(camera, alpha, &mut entities)
});
}
if (!title || live) && self.dim == Dim::Overworld && self.settings.horizon.weather {
horizon_client::spans::time("weather", || {
self.weather_geometry(camera, alpha, &mut entities)
});
}
let water_fog = {
let c = info.water_fog;
@@ -8600,6 +8487,21 @@ impl Game<'_> {
debug_lines_on_top: &debug_lines_on_top,
debug_crosshair: &debug_crosshair,
hud: &hud,
clouds: ((!title || live)
&& self.dim == Dim::Overworld
&& horizon_client::clouds::visible(&self.settings.clouds, sky.cloud_color[3]))
.then(|| self.res.atlas.sprites.get("horizon/white"))
.flatten()
.map(|white| horizon_client::clouds::Input {
texture: &self.res.clouds,
uv: [(white.u0 + white.u1) / 2.0, (white.v0 + white.v1) / 2.0],
camera,
time: self.world.game_time,
partial_tick: alpha,
range: self.settings.cloud_range,
fast: self.settings.clouds == "fast",
color: sky.cloud_color,
}),
entities: &entities,
additive: &additive,
celestial: if self.settings.horizon.sky || title {
+102 -23
View File
@@ -144,6 +144,8 @@ pub struct Frame<'a> {
pub nether_lighting: bool,
/// Camera-relative entity geometry textured from the shared atlas.
pub entities: &'a [Vertex],
/// Cloud mesh inputs; geometry and GPU storage are retained by the renderer.
pub clouds: Option<crate::clouds::Input<'a>>,
/// Additive translucent geometry after the entities (lightning, dragon rays).
pub additive: &'a [Vertex],
/// Camera-relative sun, moon and stars (drawn behind everything, additive).
@@ -255,6 +257,11 @@ pub struct Renderer {
entity_pipeline: wgpu::RenderPipeline,
crumbling_pipeline: wgpu::RenderPipeline,
cloud_pipeline: wgpu::RenderPipeline,
cloud_mesh: crate::clouds::Mesh,
cloud_buffer: wgpu::Buffer,
cloud_capacity: u64,
cloud_uniform: wgpu::Buffer,
cloud_group: wgpu::BindGroup,
additive_pipeline: wgpu::RenderPipeline,
celestial_pipeline: wgpu::RenderPipeline,
celestial_buffer: wgpu::Buffer,
@@ -1007,19 +1014,60 @@ impl Renderer {
bind_group_layouts: &[Some(&entity_bindings)],
..Default::default()
});
let cloud_bindings = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("Cloud bindings"),
entries: &[entry(
0,
wgpu::ShaderStages::VERTEX,
wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None,
},
)],
});
let cloud_uniform = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("Cloud transforms and colors"),
size: std::mem::size_of::<crate::clouds::Uniform>() as u64,
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
let cloud_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("Cloud resources"),
layout: &cloud_bindings,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: cloud_uniform.as_entire_binding(),
}],
});
let cloud_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("Cloud layout"),
bind_group_layouts: &[Some(&entity_bindings), Some(&cloud_bindings)],
..Default::default()
});
let cloud_buffer = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("Cloud vertices"),
size: 4 * std::mem::size_of::<Vertex>() as u64,
usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
let entity_attributes = wgpu::vertex_attr_array![0=>Float32x3,1=>Float32x2,2=>Uint32,3=>Unorm8x4,4=>Uint8x4,5=>Snorm8x4];
let entity_vertex = [Some(wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<Vertex>() as u64,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &entity_attributes,
})];
let entity_pipeline = |label, cull_mode, depth_write, bias, blend| {
let entity_pipeline = |label, cull_mode, depth_write, bias, blend, clouds| {
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some(label),
layout: Some(&entity_layout),
layout: Some(if clouds {
&cloud_layout
} else {
&entity_layout
}),
vertex: wgpu::VertexState {
module: &entity_shader,
entry_point: Some("vs_entity"),
entry_point: Some(if clouds { "vs_cloud" } else { "vs_entity" }),
compilation_options: Default::default(),
buffers: &entity_vertex,
},
@@ -1059,6 +1107,7 @@ impl Renderer {
clamp: 0.0,
},
CRUMBLING_BLEND,
false,
);
let cloud_pipeline = entity_pipeline(
"Clouds",
@@ -1066,9 +1115,16 @@ impl Renderer {
true,
Default::default(),
alpha_blend,
true,
);
let entity_pipeline = entity_pipeline(
"Entities",
None,
true,
Default::default(),
alpha_blend,
false,
);
let entity_pipeline =
entity_pipeline("Entities", None, true, Default::default(), alpha_blend);
// BlendFunction.LIGHTNING: src alpha, one; no depth writes.
let additive_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("Additive entities"),
@@ -1451,6 +1507,11 @@ impl Renderer {
entity_pipeline,
crumbling_pipeline,
cloud_pipeline,
cloud_mesh: Default::default(),
cloud_buffer,
cloud_capacity: 4,
cloud_uniform,
cloud_group,
additive_pipeline,
celestial_pipeline,
celestial_buffer,
@@ -2221,16 +2282,36 @@ impl Renderer {
self.queue
.write_buffer(&self.outline, 0, bytemuck::cast_slice(&lines));
}
let [
entity_quads,
crumbling_quads,
translucent_crumbling_quads,
cloud_quads,
] = self
let [entity_quads, crumbling_quads, translucent_crumbling_quads] = self
.entity_batch
.update(frame.entities, frame.additive, frame.hand);
let world_quads =
entity_quads + crumbling_quads + translucent_crumbling_quads + cloud_quads;
let world_quads = entity_quads + crumbling_quads + translucent_crumbling_quads;
let cloud_quads = if let Some(input) = &frame.clouds {
let (rebuilt, uniform) =
crate::spans::time("clouds", || self.cloud_mesh.prepare(input));
let vertices = self.cloud_mesh.vertices.len() as u64;
if rebuilt && vertices > 0 {
if vertices > self.cloud_capacity {
self.cloud_capacity = vertices.next_power_of_two();
self.cloud_buffer = self.device.create_buffer(&wgpu::BufferDescriptor {
label: Some("Cloud vertices"),
size: self.cloud_capacity * std::mem::size_of::<Vertex>() as u64,
usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
}
self.queue.write_buffer(
&self.cloud_buffer,
0,
bytemuck::cast_slice(&self.cloud_mesh.vertices),
);
}
self.queue
.write_buffer(&self.cloud_uniform, 0, bytemuck::bytes_of(&uniform));
(vertices / 4) as u32
} else {
0
};
let all_entities = &self.entity_batch.combined;
let hand_quads = (frame.hand.len() / 4) as u32;
let additive_quads = (frame.additive.len() / 4) as u32;
@@ -2256,8 +2337,9 @@ impl Renderer {
bytemuck::cast_slice(&frame.celestial[..celestial_vertices]),
);
}
if world_quads + additive_quads + hand_quads > self.index_quads {
self.index_quads = (world_quads + additive_quads + hand_quads).next_power_of_two();
let max_quads = (world_quads + additive_quads + hand_quads).max(cloud_quads);
if max_quads > self.index_quads {
self.index_quads = max_quads.next_power_of_two();
self.indices = Self::quad_indices(&self.device, self.index_quads);
}
if let Some(b) = &frame.border {
@@ -2437,12 +2519,9 @@ impl Renderer {
if cloud_quads > 0 {
pass.set_pipeline(&self.cloud_pipeline);
pass.set_bind_group(0, &self.entity_group, &[]);
pass.set_vertex_buffer(0, self.entity_buffer.slice(..));
pass.draw_indexed(
0..cloud_quads * 6,
((entity_quads + crumbling_quads + translucent_crumbling_quads) * 4) as i32,
0..1,
);
pass.set_bind_group(1, &self.cloud_group, &[]);
pass.set_vertex_buffer(0, self.cloud_buffer.slice(..));
pass.draw_indexed(0..cloud_quads * 6, 0, 0..1);
}
}
if let (true, Some((color, depth)), Some(size), Some(stages)) =
@@ -2871,7 +2950,7 @@ mod tests {
let mut batch = super::EntityBatch::default();
let counts = batch.update(&entities, &quad(3, 5), &quad(0, 6));
let vertices = &batch.combined;
assert_eq!(counts, [2, 1, 1, 2]);
assert_eq!(counts, [4, 1, 1]);
assert_eq!(
vertices
.as_chunks::<4>()
@@ -2879,7 +2958,7 @@ mod tests {
.iter()
.map(|q| q[0].light)
.collect::<Vec<_>>(),
[0, 3, 2, 7, 1, 4, 5, 6]
[0, 1, 3, 4, 2, 7, 5, 6]
);
}
#[test]
@@ -6,7 +6,7 @@ pub(super) struct EntityBatch {
// Quad indices avoid copying vertices twice while grouping materials.
// Keep initialized high-water storage; local counts delimit active entries.
// Each bucket can hold all quads, removing per-quad Vec growth checks.
buckets: [Vec<usize>; 4],
buckets: [Vec<usize>; 3],
pub(super) combined: Vec<Vertex>,
}
@@ -16,19 +16,18 @@ impl EntityBatch {
entities: &[Vertex],
additive: &[Vertex],
hand: &[Vertex],
) -> [u32; 4] {
) -> [u32; 3] {
let quads = entities.as_chunks::<4>().0;
for bucket in &mut self.buckets {
if bucket.len() < quads.len() {
bucket.resize(quads.len(), 0);
}
}
let mut counts = [0usize; 4];
let mut counts = [0usize; 3];
for (index, quad) in quads.iter().enumerate() {
let material = match quad[0].layer {
11 => 1,
12 => 2,
4 => 3,
_ => 0,
};
self.buckets[material][counts[material]] = index;
@@ -75,15 +74,14 @@ mod tests {
entities: &[Vertex],
additive: &[Vertex],
hand: &[Vertex],
) -> (Vec<Vertex>, [u32; 4]) {
) -> (Vec<Vertex>, [u32; 3]) {
let mut combined = Vec::with_capacity(entities.len() + additive.len() + hand.len());
let mut counts = [0; 4];
for (i, layer) in [0, 11, 12, 4].into_iter().enumerate() {
let mut counts = [0; 3];
for (i, layer) in [0, 11, 12].into_iter().enumerate() {
for quad in entities.as_chunks::<4>().0 {
let material = match quad[0].layer {
11 => 11,
12 => 12,
4 => 4,
_ => 0,
};
if material == layer {