Finish M4: cart, log facade, idle diagnostics and terrain LOD
Add the core::log facade with five levels and a sink; migrate the app dev log and enable trace in --dev. Replace the dropped cubes with a four-wheel cart on revolute joints driven by velocity motors, add WASD controls, a golden trajectory and a slope test. Instrument idle frame spikes and throttle the window title. Add chunked terrain LOD with skirts (2368 vs 8192 triangles) while physics keeps the full mesh. Pin Rust 1.100.0; edition stays 2024.
This commit is contained in:
parent
190ef9cc4b
commit
1042269d7e
38 changed files with 2348 additions and 374 deletions
|
|
@ -1,5 +1,5 @@
|
|||
//! Custom wgpu renderer: depth-tested terrain meshes, instanced boxes, one
|
||||
//! directional light.
|
||||
//! Custom wgpu renderer: depth-tested terrain meshes, instanced boxes and
|
||||
//! wheels, one directional light.
|
||||
//!
|
||||
//! The renderer is not part of the authoritative simulation: it may use `f32`
|
||||
//! and never reads wall-clock time. Headless use builds a [`Gpu`] and renders
|
||||
|
|
|
|||
|
|
@ -201,6 +201,7 @@ impl Renderer {
|
|||
pass.set_bind_group(0, &self.pipelines.bind_group, &[]);
|
||||
geometry.draw_mesh(&mut pass, &self.pipelines);
|
||||
geometry.draw_boxes(&mut pass, &self.pipelines);
|
||||
geometry.draw_wheels(&mut pass, &self.pipelines);
|
||||
}
|
||||
self.queue.submit([encoder.finish()]);
|
||||
}
|
||||
|
|
@ -225,8 +226,10 @@ impl Renderer {
|
|||
mesh_vertices: upload_vertices(&self.device, scene),
|
||||
mesh_indices: upload_indices(&self.device, scene),
|
||||
mesh_index_count: scene.mesh.indices.len() as u32,
|
||||
boxes: upload_instances(&self.device, scene),
|
||||
boxes: upload_instances(&self.device, "fabrilis box instances", &scene.instances),
|
||||
instance_count: scene.instances.len() as u32,
|
||||
wheels: upload_instances(&self.device, "fabrilis wheel instances", &scene.wheels),
|
||||
wheel_count: scene.wheels.len() as u32,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -237,6 +240,8 @@ struct Geometry {
|
|||
mesh_index_count: u32,
|
||||
boxes: Option<wgpu::Buffer>,
|
||||
instance_count: u32,
|
||||
wheels: Option<wgpu::Buffer>,
|
||||
wheel_count: u32,
|
||||
}
|
||||
|
||||
impl Geometry {
|
||||
|
|
@ -259,6 +264,16 @@ impl Geometry {
|
|||
pass.set_index_buffer(pipelines.cube_indices.slice(..), wgpu::IndexFormat::Uint32);
|
||||
pass.draw_indexed(0..pipelines.cube_index_count, 0, 0..self.instance_count);
|
||||
}
|
||||
|
||||
fn draw_wheels(&self, pass: &mut wgpu::RenderPass<'_>, pipelines: &Pipelines) {
|
||||
let Some(instances) = &self.wheels else {
|
||||
return;
|
||||
};
|
||||
pass.set_vertex_buffer(0, pipelines.wheel_vertices.slice(..));
|
||||
pass.set_vertex_buffer(1, instances.slice(..));
|
||||
pass.set_index_buffer(pipelines.wheel_indices.slice(..), wgpu::IndexFormat::Uint32);
|
||||
pass.draw_indexed(0..pipelines.wheel_index_count, 0, 0..self.wheel_count);
|
||||
}
|
||||
}
|
||||
|
||||
fn upload_vertices(device: &wgpu::Device, scene: &Scene) -> Option<wgpu::Buffer> {
|
||||
|
|
@ -285,12 +300,15 @@ fn upload_indices(device: &wgpu::Device, scene: &Scene) -> Option<wgpu::Buffer>
|
|||
))
|
||||
}
|
||||
|
||||
fn upload_instances(device: &wgpu::Device, scene: &Scene) -> Option<wgpu::Buffer> {
|
||||
if scene.instances.is_empty() {
|
||||
fn upload_instances(
|
||||
device: &wgpu::Device,
|
||||
label: &str,
|
||||
instances: &[crate::Instance],
|
||||
) -> Option<wgpu::Buffer> {
|
||||
if instances.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let instances: Vec<pipeline::RawInstance> = scene
|
||||
.instances
|
||||
let raw: Vec<pipeline::RawInstance> = instances
|
||||
.iter()
|
||||
.map(|instance| pipeline::RawInstance {
|
||||
model: instance.model,
|
||||
|
|
@ -299,8 +317,8 @@ fn upload_instances(device: &wgpu::Device, scene: &Scene) -> Option<wgpu::Buffer
|
|||
.collect();
|
||||
Some(pipeline::upload(
|
||||
device,
|
||||
"fabrilis box instances",
|
||||
bytemuck::cast_slice(&instances),
|
||||
label,
|
||||
bytemuck::cast_slice(&raw),
|
||||
wgpu::BufferUsages::VERTEX,
|
||||
))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -83,6 +83,9 @@ pub(crate) struct Pipelines {
|
|||
pub cube_vertices: wgpu::Buffer,
|
||||
pub cube_indices: wgpu::Buffer,
|
||||
pub cube_index_count: u32,
|
||||
pub wheel_vertices: wgpu::Buffer,
|
||||
pub wheel_indices: wgpu::Buffer,
|
||||
pub wheel_index_count: u32,
|
||||
pub terrain_instance: wgpu::Buffer,
|
||||
}
|
||||
|
||||
|
|
@ -96,6 +99,7 @@ pub(crate) fn build(device: &wgpu::Device, format: wgpu::TextureFormat) -> Pipel
|
|||
let (globals, bind_group) = create_globals(device, &layout);
|
||||
let pipeline = create_pipeline(device, format, &shader, &layout);
|
||||
let (cube_vertices, cube_indices, cube_index_count) = cube_geometry(device);
|
||||
let (wheel_vertices, wheel_indices, wheel_index_count) = wheel_geometry(device);
|
||||
let terrain_instance = upload(
|
||||
device,
|
||||
"fabrilis terrain instance",
|
||||
|
|
@ -109,6 +113,9 @@ pub(crate) fn build(device: &wgpu::Device, format: wgpu::TextureFormat) -> Pipel
|
|||
cube_vertices,
|
||||
cube_indices,
|
||||
cube_index_count,
|
||||
wheel_vertices,
|
||||
wheel_indices,
|
||||
wheel_index_count,
|
||||
terrain_instance,
|
||||
}
|
||||
}
|
||||
|
|
@ -177,6 +184,71 @@ fn cube_geometry(device: &wgpu::Device) -> (wgpu::Buffer, wgpu::Buffer, u32) {
|
|||
(vertex_buffer, index_buffer, indices.len() as u32)
|
||||
}
|
||||
|
||||
/// Segments around the wheel cylinder; a 24-gon reads as round at wheel size.
|
||||
const WHEEL_SEGMENTS: usize = 24;
|
||||
|
||||
/// A point on the unit wheel's rim: the circle lies in the XZ plane, radius 0.5.
|
||||
fn rim_point(segment: usize) -> (f32, f32) {
|
||||
let angle = fabrilis_core::math::TAU * (segment as f64) / (WHEEL_SEGMENTS as f64);
|
||||
let (sin, cos) = fabrilis_core::math::sin_cos(angle);
|
||||
(cos as f32 * 0.5, sin as f32 * 0.5)
|
||||
}
|
||||
|
||||
/// Unit wheel: radius 0.5, height 1.0, axis along the local Y, closed with caps.
|
||||
///
|
||||
/// The instance matrix places, rotates and scales it, so the mesh is built once.
|
||||
fn wheel_geometry(device: &wgpu::Device) -> (wgpu::Buffer, wgpu::Buffer, u32) {
|
||||
let mut vertices: Vec<[f32; 3]> = Vec::with_capacity(WHEEL_SEGMENTS * 5 + 2);
|
||||
let mut indices: Vec<u32> = Vec::with_capacity(WHEEL_SEGMENTS * 12);
|
||||
let half_height = 0.5;
|
||||
for segment in 0..WHEEL_SEGMENTS {
|
||||
let (x0, z0) = rim_point(segment);
|
||||
let (x1, z1) = rim_point(segment + 1);
|
||||
let base = vertices.len() as u32;
|
||||
vertices.extend([
|
||||
[x0, -half_height, z0],
|
||||
[x0, half_height, z0],
|
||||
[x1, half_height, z1],
|
||||
[x1, -half_height, z1],
|
||||
]);
|
||||
indices.extend_from_slice(&[base, base + 1, base + 2, base, base + 2, base + 3]);
|
||||
}
|
||||
let top_centre = vertices.len() as u32;
|
||||
vertices.push([0.0, half_height, 0.0]);
|
||||
for segment in 0..WHEEL_SEGMENTS {
|
||||
let (x, z) = rim_point(segment);
|
||||
vertices.push([x, half_height, z]);
|
||||
}
|
||||
let bottom_centre = vertices.len() as u32;
|
||||
vertices.push([0.0, -half_height, 0.0]);
|
||||
for segment in 0..WHEEL_SEGMENTS {
|
||||
let (x, z) = rim_point(segment);
|
||||
vertices.push([x, -half_height, z]);
|
||||
}
|
||||
for segment in 0..WHEEL_SEGMENTS {
|
||||
let next = (segment + 1) % WHEEL_SEGMENTS;
|
||||
let top = top_centre + 1 + segment as u32;
|
||||
let top_next = top_centre + 1 + next as u32;
|
||||
indices.extend_from_slice(&[top_centre, top_next, top]);
|
||||
let bottom = bottom_centre + 1 + segment as u32;
|
||||
let bottom_next = bottom_centre + 1 + next as u32;
|
||||
indices.extend_from_slice(&[bottom_centre, bottom, bottom_next]);
|
||||
}
|
||||
let vertex_buffer = upload(
|
||||
device,
|
||||
"fabrilis wheel vertices",
|
||||
bytemuck::cast_slice(&vertices),
|
||||
wgpu::BufferUsages::VERTEX,
|
||||
);
|
||||
let index_buffer = upload(
|
||||
device,
|
||||
"fabrilis wheel indices",
|
||||
bytemuck::cast_slice(&indices),
|
||||
wgpu::BufferUsages::INDEX,
|
||||
);
|
||||
(vertex_buffer, index_buffer, indices.len() as u32)
|
||||
}
|
||||
|
||||
fn create_bind_group_layout(device: &wgpu::Device) -> wgpu::BindGroupLayout {
|
||||
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
label: Some("fabrilis globals layout"),
|
||||
|
|
|
|||
|
|
@ -78,6 +78,29 @@ fn renders_terrain_and_box() {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn renders_a_wheel() {
|
||||
let (_gpu, mut renderer) = setup((SIZE, SIZE));
|
||||
let mut scene = Scene::new(MeshData::default());
|
||||
scene.wheels.push(Instance::new(
|
||||
[
|
||||
[2.0, 0.0, 0.0, 0.0],
|
||||
[0.0, 0.5, 0.0, 0.0],
|
||||
[0.0, 0.0, 2.0, 0.0],
|
||||
[0.0, 0.0, 0.0, 1.0],
|
||||
],
|
||||
[0.9, 0.7, 0.2, 1.0],
|
||||
));
|
||||
renderer.draw(&scene, &orbit_camera()).expect("draw");
|
||||
let pixels = renderer.pixels().expect("pixels");
|
||||
let corner = pixel_at(&pixels, (SIZE, SIZE), 2, 2);
|
||||
let centre = pixel_at(&pixels, (SIZE, SIZE), SIZE / 2, SIZE / 2);
|
||||
assert_ne!(
|
||||
corner, centre,
|
||||
"the wheel must cover the centre of the frame"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resize_reallocates_the_pixel_buffer() {
|
||||
let (_gpu, mut renderer) = setup((SIZE, SIZE));
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
//! Scene data handed to the renderer: one triangle mesh plus instanced boxes.
|
||||
//! Scene data handed to the renderer: one triangle mesh plus instanced boxes
|
||||
//! and wheels.
|
||||
|
||||
type Mat4 = [[f32; 4]; 4];
|
||||
|
||||
|
|
@ -65,6 +66,8 @@ pub struct Scene {
|
|||
pub mesh_color: [f32; 4],
|
||||
/// Boxes drawn with instancing.
|
||||
pub instances: Vec<Instance>,
|
||||
/// Wheels: instanced cylinders with the axis along their local Y.
|
||||
pub wheels: Vec<Instance>,
|
||||
}
|
||||
|
||||
impl Scene {
|
||||
|
|
@ -75,6 +78,7 @@ impl Scene {
|
|||
mesh,
|
||||
mesh_color: [0.34, 0.46, 0.29, 1.0],
|
||||
instances: Vec::new(),
|
||||
wheels: Vec::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue