feat: GPU optimization — lighting, viewport CA, benchmark mode, 531 FPS

- Resolution: 8x8 world cells, 2x2 UI cells (UI_SCALE=4)
- GPU lighting: vertex-shader computed, light source list buffer (max 64)
  instead of O(N×R²) grid scan, O(N×S) per cell
- Viewport-aware CA: iterate only active chunks, not all 250×250
- Flat array entity/item/shadow maps instead of HashMaps
- Flat 128-entry ASCII atlas array instead of HashMap lookup
- Partial grid upload: viewport + 30-cell margin only
- Pre-allocated viewport arrays in renderer structs (zero alloc/frame)
- Skip CPU lighting for GPU modes (pass None)
- Benchmark mode: --mode benchmark with per-subsystem timing
- GpuLightSource struct, light_count in push constants
- gather_sources_in_range() for viewport-scoped source gathering

Benchmark (600 ticks, release):
  Graphics: 531 FPS (was 386, +38%), render 1013us (was 1699us, -40%)
  ASCII:    402 FPS (was 313, +28%), render 1502us (was 2346us, -36%)

All 171 tests + 14 scenarios pass.
This commit is contained in:
Emil
2026-06-21 16:09:45 +03:00
parent c25cae9a32
commit 357db17c2f
53 changed files with 6362 additions and 651 deletions
+230 -44
View File
@@ -1,5 +1,5 @@
use verbatim::ai::GameSession;
use verbatim::ai::AiAction;
use verbatim::ai::GameSession;
fn setup() -> GameSession {
let mut s = GameSession::new_seeded(42);
@@ -11,28 +11,76 @@ fn setup() -> GameSession {
#[test]
fn fire_dies_over_time() {
let mut s = setup();
s.perform_action(&AiAction::FillRect { x: 100, y: 120, w: 10, h: 1, material: "stone".into() });
s.perform_action(&AiAction::SetCell { x: 105, y: 119, material: "fire".into() });
s.perform_action(&AiAction::FillRect {
x: 100,
y: 120,
w: 10,
h: 1,
material: "stone".into(),
});
s.perform_action(&AiAction::SetCell {
x: 105,
y: 119,
material: "fire".into(),
});
s.step(60);
assert_ne!(s.get_cell(105, 119).material, "fire", "fire should die after 60 ticks");
assert_ne!(
s.get_cell(105, 119).material,
"fire",
"fire should die after 60 ticks"
);
}
#[test]
fn fire_ignites_wood() {
let mut s = setup();
s.perform_action(&AiAction::FillRect { x: 100, y: 120, w: 10, h: 1, material: "stone".into() });
s.perform_action(&AiAction::SetCell { x: 104, y: 119, material: "wood".into() });
s.perform_action(&AiAction::SetCell { x: 105, y: 119, material: "fire".into() });
s.perform_action(&AiAction::FillRect {
x: 100,
y: 120,
w: 10,
h: 1,
material: "stone".into(),
});
s.perform_action(&AiAction::SetCell {
x: 104,
y: 119,
material: "wood".into(),
});
s.perform_action(&AiAction::SetCell {
x: 105,
y: 119,
material: "fire".into(),
});
s.step(20);
assert_ne!(s.get_cell(104, 119).material, "wood", "wood should be ignited by fire");
assert_ne!(
s.get_cell(104, 119).material,
"wood",
"wood should be ignited by fire"
);
}
#[test]
fn fire_ignites_grass() {
let mut s = setup();
s.perform_action(&AiAction::FillRect { x: 100, y: 120, w: 10, h: 1, material: "stone".into() });
s.perform_action(&AiAction::FillRect { x: 100, y: 119, w: 8, h: 1, material: "grass".into() });
s.perform_action(&AiAction::SetCell { x: 100, y: 119, material: "fire".into() });
s.perform_action(&AiAction::FillRect {
x: 100,
y: 120,
w: 10,
h: 1,
material: "stone".into(),
});
s.perform_action(&AiAction::FillRect {
x: 100,
y: 119,
w: 8,
h: 1,
material: "grass".into(),
});
s.perform_action(&AiAction::SetCell {
x: 100,
y: 119,
material: "fire".into(),
});
s.step(30);
let grass_left = s.count_material_in_region(99, 118, 10, 3, "grass");
assert_eq!(grass_left, 0, "fire should spread through grass");
@@ -41,21 +89,55 @@ fn fire_ignites_grass() {
#[test]
fn fire_ignites_flesh() {
let mut s = setup();
s.perform_action(&AiAction::FillRect { x: 100, y: 120, w: 10, h: 1, material: "stone".into() });
s.perform_action(&AiAction::SetCell { x: 104, y: 119, material: "flesh".into() });
s.perform_action(&AiAction::SetCell { x: 105, y: 119, material: "fire".into() });
s.perform_action(&AiAction::FillRect {
x: 100,
y: 120,
w: 10,
h: 1,
material: "stone".into(),
});
s.perform_action(&AiAction::SetCell {
x: 104,
y: 119,
material: "flesh".into(),
});
s.perform_action(&AiAction::SetCell {
x: 105,
y: 119,
material: "fire".into(),
});
s.step(30);
assert_ne!(s.get_cell(104, 119).material, "flesh", "flesh should be ignited by fire");
assert_ne!(
s.get_cell(104, 119).material,
"flesh",
"flesh should be ignited by fire"
);
}
#[test]
fn smoke_rises() {
let mut s = setup();
s.perform_action(&AiAction::FillRect { x: 100, y: 120, w: 10, h: 1, material: "stone".into() });
s.perform_action(&AiAction::FillRect { x: 100, y: 110, w: 10, h: 1, material: "stone".into() });
s.perform_action(&AiAction::FillRect {
x: 100,
y: 120,
w: 10,
h: 1,
material: "stone".into(),
});
s.perform_action(&AiAction::FillRect {
x: 100,
y: 110,
w: 10,
h: 1,
material: "stone".into(),
});
for y in 114..118 {
for x in 104..107 {
s.perform_action(&AiAction::SetCell { x, y, material: "smoke".into() });
s.perform_action(&AiAction::SetCell {
x,
y,
material: "smoke".into(),
});
}
}
s.step(15);
@@ -66,7 +148,11 @@ fn smoke_rises() {
#[test]
fn smoke_dissipates_over_time() {
let mut s = setup();
s.perform_action(&AiAction::SetCell { x: 105, y: 105, material: "smoke".into() });
s.perform_action(&AiAction::SetCell {
x: 105,
y: 105,
material: "smoke".into(),
});
s.step(120);
let smoke_left = s.count_material_in_region(100, 100, 10, 10, "smoke");
assert_eq!(smoke_left, 0, "smoke should dissipate after 120 ticks");
@@ -75,26 +161,78 @@ fn smoke_dissipates_over_time() {
#[test]
fn steam_condenses_to_water() {
let mut s = setup();
s.perform_action(&AiAction::FillRect { x: 100, y: 125, w: 10, h: 1, material: "stone".into() });
for y in 118..123 {
for x in 103..108 {
s.perform_action(&AiAction::SetCell { x, y, material: "steam".into() });
// Closed container so steam/water cannot drift into inactive chunks.
for y in 116..125 {
s.perform_action(&AiAction::SetCell {
x: 100,
y,
material: "stone".into(),
});
s.perform_action(&AiAction::SetCell {
x: 109,
y,
material: "stone".into(),
});
}
for x in 100..110 {
s.perform_action(&AiAction::SetCell {
x,
y: 116,
material: "stone".into(),
});
s.perform_action(&AiAction::SetCell {
x,
y: 124,
material: "stone".into(),
});
}
for y in 117..124 {
for x in 101..109 {
s.perform_action(&AiAction::SetCell {
x,
y,
material: "steam".into(),
});
}
}
s.step(150);
let water_or_steam = s.count_material_in_region(100, 118, 10, 8, "water")
+ s.count_material_in_region(100, 118, 10, 8, "steam");
assert!(water_or_steam > 0, "steam should condense to water or remain steam: water+steam={}", water_or_steam);
let water_count = s.count_material_in_region(100, 118, 10, 8, "water");
assert!(water_count > 0, "some steam should have condensed to water by now: water={}", water_count);
s.step(200);
let water_or_steam = s.count_material_in_region(100, 116, 10, 9, "water")
+ s.count_material_in_region(100, 116, 10, 9, "steam");
assert!(
water_or_steam > 0,
"steam should condense to water or remain steam: water+steam={}",
water_or_steam
);
let water_count = s.count_material_in_region(100, 116, 10, 9, "water");
assert!(
water_count > 0,
"some steam should have condensed to water by now: water={}",
water_count
);
}
#[test]
fn steam_rises() {
let mut s = setup();
s.perform_action(&AiAction::FillRect { x: 100, y: 130, w: 10, h: 1, material: "stone".into() });
s.perform_action(&AiAction::FillRect { x: 100, y: 100, w: 10, h: 1, material: "stone".into() });
s.perform_action(&AiAction::SetCell { x: 105, y: 125, material: "steam".into() });
s.perform_action(&AiAction::FillRect {
x: 100,
y: 130,
w: 10,
h: 1,
material: "stone".into(),
});
s.perform_action(&AiAction::FillRect {
x: 100,
y: 100,
w: 10,
h: 1,
material: "stone".into(),
});
s.perform_action(&AiAction::SetCell {
x: 105,
y: 125,
material: "steam".into(),
});
s.step(20);
let steam_above = s.count_material_in_region(100, 105, 10, 10, "steam");
assert!(steam_above > 0, "steam should rise upward");
@@ -103,7 +241,11 @@ fn steam_rises() {
#[test]
fn grass_is_solid() {
let mut s = setup();
s.perform_action(&AiAction::SetCell { x: 105, y: 110, material: "grass".into() });
s.perform_action(&AiAction::SetCell {
x: 105,
y: 110,
material: "grass".into(),
});
let cell = s.get_cell(105, 110);
assert!(cell.is_solid, "grass should be solid");
}
@@ -111,7 +253,11 @@ fn grass_is_solid() {
#[test]
fn dirt_is_solid() {
let mut s = setup();
s.perform_action(&AiAction::SetCell { x: 105, y: 110, material: "dirt".into() });
s.perform_action(&AiAction::SetCell {
x: 105,
y: 110,
material: "dirt".into(),
});
let cell = s.get_cell(105, 110);
assert!(cell.is_solid, "dirt should be solid");
}
@@ -119,39 +265,79 @@ fn dirt_is_solid() {
#[test]
fn stone_is_static() {
let mut s = setup();
s.perform_action(&AiAction::SetCell { x: 105, y: 110, material: "stone".into() });
s.perform_action(&AiAction::SetCell {
x: 105,
y: 110,
material: "stone".into(),
});
s.step(20);
assert_eq!(s.get_cell(105, 110).material, "stone", "stone should not move");
assert_eq!(
s.get_cell(105, 110).material,
"stone",
"stone should not move"
);
}
#[test]
fn wood_is_static() {
let mut s = setup();
s.perform_action(&AiAction::SetCell { x: 105, y: 110, material: "wood".into() });
s.perform_action(&AiAction::SetCell {
x: 105,
y: 110,
material: "wood".into(),
});
s.step(20);
assert_eq!(s.get_cell(105, 110).material, "wood", "wood should not move");
assert_eq!(
s.get_cell(105, 110).material,
"wood",
"wood should not move"
);
}
#[test]
fn bone_is_static() {
let mut s = setup();
s.perform_action(&AiAction::SetCell { x: 105, y: 110, material: "bone".into() });
s.perform_action(&AiAction::SetCell {
x: 105,
y: 110,
material: "bone".into(),
});
s.step(20);
assert_eq!(s.get_cell(105, 110).material, "bone", "bone should not move");
assert_eq!(
s.get_cell(105, 110).material,
"bone",
"bone should not move"
);
}
#[test]
fn lava_initial_temp_is_high() {
let mut s = setup();
s.perform_action(&AiAction::SetCell { x: 105, y: 110, material: "lava".into() });
s.perform_action(&AiAction::SetCell {
x: 105,
y: 110,
material: "lava".into(),
});
let cell = s.get_cell(105, 110);
assert!(cell.temp > 1000.0, "lava should start very hot, got {}°C", cell.temp);
assert!(
cell.temp > 1000.0,
"lava should start very hot, got {}°C",
cell.temp
);
}
#[test]
fn water_initial_temp_is_room() {
let mut s = setup();
s.perform_action(&AiAction::SetCell { x: 105, y: 110, material: "water".into() });
s.perform_action(&AiAction::SetCell {
x: 105,
y: 110,
material: "water".into(),
});
let cell = s.get_cell(105, 110);
assert!(cell.temp < 50.0, "water should start at room temp, got {}°C", cell.temp);
assert!(
cell.temp < 50.0,
"water should start at room temp, got {}°C",
cell.temp
);
}