[PR #688] [MERGED] Keep terrain in system memory when video memory is full instead of crashing (#600) #2734

Closed
opened 2026-11-02 15:58:39 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/688
Author: @ludovic111
Created: 10/17/2026
Status: ✅ Merged
Merged: 10/17/2026
Merged by: @ludovic111

Base: main ← Head: fix/rd32-gpu-oom


📝 Commits (2)

  • b47f565 Reclaim unused terrain GPU arena pages
  • 814b70b Keep terrain in system memory when video memory is full instead of crashing (#600)

📊 Changes

2 files changed (+300 additions, -48 deletions)

View changed files

📝 crates/horizon-client/src/gfx.rs (+23 -6)
📝 crates/horizon-client/src/gfx/arena.rs (+277 -42)

📄 Description

Fixes #600.

Stacked on #676 (Online5277, "Release unused terrain GPU buffers"): this branch contains #676's commit b47f5658 unchanged plus one commit of mine on top. Merge #676 first; this diff then shrinks to my commit (gfx.rs +~20, gfx/arena.rs +~150 including tests).

What

At render distance 32 the section arena holds about 1.5 GB of vertices: on the Mac mini at RD32, 22.6k resident sections, 1.38 GB of vertices, 1.93 GB of GPU memory standing still and 2.1 GB while flying. Vulkan drivers don't page like OpenGL drivers do. On the reporter's 4 GB card (Linux, Wayland, Vulkan), create_buffer fails with Out of Memory, and wgpu's default uncaptured-error handler turns that into a panic. That is the crash in #600.

#676 cuts how much stays allocated: it releases empty pages and frees the arenas when a world closes. This commit handles running out anyway:

  • Arena pages are created inside an OutOfMemory error scope (try_create), so a failed allocation returns None instead of panicking.
  • A page that doesn't fit in video memory is created again with MAP_WRITE. The allocator places that buffer in host-visible system memory, and the terrain still draws, more slowly, as it would on an OpenGL driver. The first time this happens a warning is logged suggesting a lower render distance. This needs MAPPABLE_PRIMARY_BUFFERS, which is now requested when the adapter has it (Vulkan, Metal and DX12 do).
  • If a page can't grow, it keeps its contents and a smaller new page is tried.
  • With no memory left at all, the section is skipped (Renderer::upload returns early, releasing anything it had reserved) rather than the game ending.

Nothing changes until memory runs out. The only cost on the normal path is the error scope around page creation, which is rare (one per 256 MiB page or growth step):

creating 32 pages of 28 MiB (Metal) total per page
plain create_buffer 73–74 µs 2.3 µs
inside an error scope 80–82 µs 2.5 µs (+0.2 µs)

Tests

  • full_video_memory_spills_to_system_memory: a test hook makes video-memory allocations fail. Growing the arena then spills to a mappable buffer, and the contents survive. This is read back from the GPU.
  • no_memory_left_is_not_an_error: allocation returns None and nothing panics.
  • #676's two arena tests are adapted to alloc returning Option.
  • A real OOM can't be triggered on Metal (unified memory pages), so that path is covered by the hook.

Measurements

Mac mini M4 Pro, Metal, --offscreen (the session is locked), seed and position fixed ([1478,120,54]), RD32, 120 FPS default cap, 20 s standing then 20 s flying:

run phase avg FPS 1% low 0.1% low p50 ms max ms CPU ms/frame sections loaded after
base stand 38.9 8.2 7.8 16.6 128 20.9 271
base fly 14.3 2.2 1.8 41.1 558 57.5
new stand 10.2 1.9 1.8 51.9 560 80.4 25534
new fly 8.5 1.7 1.3 107.6 768 112.4
base stand 88.2 8.0 3.5 8.4 286 5.4 16511
base fly 8.1 1.6 1.1 97.9 881 122.4
new stand 46.0 4.5 3.9 12.1 257 14.6 17785
new fly 6.6 1.7 1.6 134.7 638 148.3

GPU time isn't reported offscreen. These numbers can't show a difference either way. The Mac mini was at load 60–88 on 14 threads, shared with parity runs. The same base binary gave 38.9 and 88.2 FPS standing in two identical runs. The runs also stopped with different amounts of terrain loaded (271 to 25.5k sections). The changed code runs only when a page is created, which is what the microbenchmark above measures.

--server-thread: a 3000-block flight at 50 blocks/s at RD32 ran without a crash; the arena peaked at 1503 MB.

Memory with #676 included, RD32 → title → reopen:

step main: arena / GPU memory this branch
RD32 loaded 1503 / 1896 MB 1503 / 1899 MB
title screen 1503 / 1870 MB 0 / 350 MB
reopened 1503 / 1901 MB 735 / 1136 MB (still loading)

Screenshot against origin/main: /tick freeze, a fixed tp, and a 12 s wait. Base vs base again differed in 38.0k pixels, base vs new in 37.7k, all by 8 levels or less (water animation and particles). That is within the noise, so the frames are the same.

Self-check

  • cargo fmt --check: clean
  • cargo clippy --workspace --all-targets -- -D warnings: clean
  • cargo test -p horizon-client --lib: 809 passed, 0 failed, 11 ignored
  • tools/parity.py --changed origin/main (86 harnesses depend on gfx.rs / gfx/arena.rs): 86 passed, 0 failed (116.7 min)

Needs Ludovic

Nothing. The out-of-memory path itself needs a small-VRAM Vulkan GPU to be seen live; the reporter's 4 GB card is the target.

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/horizon-rust/horizon/pull/688 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 10/17/2026 **Status:** ✅ Merged **Merged:** 10/17/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `fix/rd32-gpu-oom` --- ### 📝 Commits (2) - [`b47f565`](https://github.com/horizon-rust/horizon/commit/b47f5658496cf787db06cdb46681c36d96bffbf6) Reclaim unused terrain GPU arena pages - [`814b70b`](https://github.com/horizon-rust/horizon/commit/814b70b8c2d89eff954ac442f79c08bad0a565b4) Keep terrain in system memory when video memory is full instead of crashing (#600) ### 📊 Changes **2 files changed** (+300 additions, -48 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/gfx.rs` (+23 -6) 📝 `crates/horizon-client/src/gfx/arena.rs` (+277 -42) </details> ### 📄 Description Fixes #600. **Stacked on #676** (Online5277, "Release unused terrain GPU buffers"): this branch contains #676's commit `b47f5658` unchanged plus one commit of mine on top. Merge #676 first; this diff then shrinks to my commit (`gfx.rs` +~20, `gfx/arena.rs` +~150 including tests). ## What At render distance 32 the section arena holds about 1.5 GB of vertices: on the Mac mini at RD32, 22.6k resident sections, 1.38 GB of vertices, 1.93 GB of GPU memory standing still and 2.1 GB while flying. Vulkan drivers don't page like OpenGL drivers do. On the reporter's 4 GB card (Linux, Wayland, Vulkan), `create_buffer` fails with Out of Memory, and wgpu's default uncaptured-error handler turns that into a panic. That is the crash in #600. #676 cuts how much stays allocated: it releases empty pages and frees the arenas when a world closes. This commit handles running out anyway: - Arena pages are created inside an `OutOfMemory` error scope (`try_create`), so a failed allocation returns `None` instead of panicking. - A page that doesn't fit in video memory is created again with `MAP_WRITE`. The allocator places that buffer in host-visible system memory, and the terrain still draws, more slowly, as it would on an OpenGL driver. The first time this happens a warning is logged suggesting a lower render distance. This needs `MAPPABLE_PRIMARY_BUFFERS`, which is now requested when the adapter has it (Vulkan, Metal and DX12 do). - If a page can't grow, it keeps its contents and a smaller new page is tried. - With no memory left at all, the section is skipped (`Renderer::upload` returns early, releasing anything it had reserved) rather than the game ending. Nothing changes until memory runs out. The only cost on the normal path is the error scope around page creation, which is rare (one per 256 MiB page or growth step): | creating 32 pages of 28 MiB (Metal) | total | per page | |---|---|---| | plain `create_buffer` | 73–74 µs | 2.3 µs | | inside an error scope | 80–82 µs | 2.5 µs (+0.2 µs) | ## Tests - `full_video_memory_spills_to_system_memory`: a test hook makes video-memory allocations fail. Growing the arena then spills to a mappable buffer, and the contents survive. This is read back from the GPU. - `no_memory_left_is_not_an_error`: allocation returns `None` and nothing panics. - #676's two arena tests are adapted to `alloc` returning `Option`. - A real OOM can't be triggered on Metal (unified memory pages), so that path is covered by the hook. ## Measurements Mac mini M4 Pro, Metal, `--offscreen` (the session is locked), seed and position fixed (`[1478,120,54]`), RD32, 120 FPS default cap, 20 s standing then 20 s flying: | run | phase | avg FPS | 1% low | 0.1% low | p50 ms | max ms | CPU ms/frame | sections loaded after | |---|---|---|---|---|---|---|---|---| | base | stand | 38.9 | 8.2 | 7.8 | 16.6 | 128 | 20.9 | 271 | | base | fly | 14.3 | 2.2 | 1.8 | 41.1 | 558 | 57.5 | | | new | stand | 10.2 | 1.9 | 1.8 | 51.9 | 560 | 80.4 | 25534 | | new | fly | 8.5 | 1.7 | 1.3 | 107.6 | 768 | 112.4 | | | base | stand | 88.2 | 8.0 | 3.5 | 8.4 | 286 | 5.4 | 16511 | | base | fly | 8.1 | 1.6 | 1.1 | 97.9 | 881 | 122.4 | | | new | stand | 46.0 | 4.5 | 3.9 | 12.1 | 257 | 14.6 | 17785 | | new | fly | 6.6 | 1.7 | 1.6 | 134.7 | 638 | 148.3 | | GPU time isn't reported offscreen. **These numbers can't show a difference either way.** The Mac mini was at load 60–88 on 14 threads, shared with parity runs. The same base binary gave 38.9 and 88.2 FPS standing in two identical runs. The runs also stopped with different amounts of terrain loaded (271 to 25.5k sections). The changed code runs only when a page is created, which is what the microbenchmark above measures. `--server-thread`: a 3000-block flight at 50 blocks/s at RD32 ran without a crash; the arena peaked at 1503 MB. Memory with #676 included, RD32 → title → reopen: | step | main: arena / GPU memory | this branch | |---|---|---| | RD32 loaded | 1503 / 1896 MB | 1503 / 1899 MB | | title screen | 1503 / 1870 MB | 0 / 350 MB | | reopened | 1503 / 1901 MB | 735 / 1136 MB (still loading) | Screenshot against origin/main: `/tick freeze`, a fixed tp, and a 12 s wait. Base vs base again differed in 38.0k pixels, base vs new in 37.7k, all by 8 levels or less (water animation and particles). That is within the noise, so the frames are the same. ## Self-check - `cargo fmt --check`: clean - `cargo clippy --workspace --all-targets -- -D warnings`: clean - `cargo test -p horizon-client --lib`: 809 passed, 0 failed, 11 ignored - `tools/parity.py --changed origin/main` (86 harnesses depend on `gfx.rs` / `gfx/arena.rs`): 86 passed, 0 failed (116.7 min) ## Needs Ludovic Nothing. The out-of-memory path itself needs a small-VRAM Vulkan GPU to be seen live; the reporter's 4 GB card is the target. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 15:58:39 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
horizon-rust/horizon#2734
No description provided.