Merge remote-tracking branch 'origin/main' into feat/dx12-fallback

# Conflicts:
#	crates/horizon-client/src/graphics.rs
#	crates/horizon-client/src/graphics_ui.rs
This commit is contained in:
Ludovic Marie
2026-10-19 09:31:58 +02:00
154 changed files with 64052 additions and 364 deletions
Generated
+2 -2
View File
@@ -1564,8 +1564,10 @@ dependencies = [
"libc",
"serde",
"serde_json",
"sha2",
"toml",
"toml_edit",
"ureq",
"windows-sys 0.61.2",
"zip",
]
@@ -4608,8 +4610,6 @@ dependencies = [
[[package]]
name = "wgpu-hal"
version = "30.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6b7fb58561a792bc237628ba0792e332de418fefe145f13b5ed8201e6d52f58"
dependencies = [
"android_system_properties",
"arrayvec",
+9 -1
View File
@@ -1,5 +1,5 @@
[workspace]
exclude = ["launcher", "crates/horizon-telemetry-server"]
exclude = ["launcher", "crates/horizon-telemetry-server", "third_party/wgpu-hal"]
members = ["crates/horizon-core", "crates/horizon-dualsense", "crates/horizon-threads", "crates/horizon-world", "crates/voxel-kernels", "crates/horizon-client", "crates/horizon-server-files", "crates/horizon-server", "crates/horizon-bots", "crates/horizon-assets", "crates/horizon-protocol", "crates/horizon-theme", "crates/horizon-shaderpack", "crates/horizon-prepare", "crates/horizon-telemetry", "crates/horizon-bench", "crates/horizon-friends", "crates/horizon-relay", "crates/horizon-mod-sdk", "crates/horizon-mods", "crates/horizon-mods/tests/guest", "crates/cargo-horizon-mod", "crates/horizon-port-pack", "crates/horizon-mod-sdk/examples/coords-hud", "xtask"]
resolver = "2"
@@ -16,6 +16,14 @@ type_complexity = "allow"
needless_range_loop = "allow"
large_enum_variant = "allow"
# wgpu-hal 30.0.1 with gfx-rs/wgpu#10124: wgpu 30 creates a Vulkan descriptor
# pool with no pool sizes while it creates the device, which Vulkan allows only
# since 1.3.215, and Intel's Windows driver for 6th-10th gen iGPUs (HD 520/530,
# UHD 620/630) crashes on it with an access violation (#746). Drop this and the
# folder once a wgpu release has the fix.
[patch.crates-io]
wgpu-hal = { path = "third_party/wgpu-hal" }
# Line tables keep file:line in backtraces; full debug info made every
# example and test binary hundreds of megabytes and slow to link.
[profile.dev]
+2
View File
@@ -43,6 +43,8 @@ Horizon is downloaded only through **the Horizon launcher**: get it from [playho
**You need** Minecraft: Java Edition installed with the official launcher, with 26.3 played once; Python 3.10 or newer and Java 25 (a JDK, e.g. Eclipse Temurin 25) for the one-time resource preparation; and a GPU with Vulkan on Linux and Windows, or any Apple Silicon Mac.
**Host a server:** `horizon-server` is one native program with no Java, one settings file, a command line, a web panel, backups and updates that roll back by themselves. It installs with one command (`curl -fsSL https://raw.githubusercontent.com/ludovic111/horizon/main/packaging/server/install.sh | sh` on Linux and macOS, `irm https://raw.githubusercontent.com/ludovic111/horizon/main/packaging/server/install.ps1 | iex` on Windows). The guide is at [playhorizon.dev/server](https://playhorizon.dev/server) ([docs/hosting.md](docs/hosting.md)).
### The launcher
<table>
+7
View File
@@ -29,3 +29,10 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```
## wgpu-hal
`third_party/wgpu-hal` is [wgpu-hal](https://github.com/gfx-rs/wgpu) 30.0.1 by the gfx-rs
developers, with one upstream fix applied
([gfx-rs/wgpu#10124](https://github.com/gfx-rs/wgpu/pull/10124)). It is licensed under MIT or
Apache-2.0; both licenses are in that folder (`LICENSE.MIT`, `LICENSE.APACHE`).
@@ -0,0 +1,167 @@
# Horizon's server against vanilla 26.3, Paper and Fabric + Lithium: mixed bots, equal work (2026-10-19)
This is the same load as `../server-equal-mixed-2026-10-18/`, now with two more servers: Paper, and vanilla on
Fabric with Lithium. It also uses a newer Horizon release (v2026.10.19). That release spawns mobs around every
player (#950) and ticks blocks around every player (#931), as vanilla does. The 2026-10-18 build did neither for
players away from spawn, so it had less work to do than vanilla, and its tick times there were lower than
they would have been with equal work. These numbers replace them.
## Summary
- **The work was equal** at every count, on all four servers. Chunks sent a second and first-time chunks a
second were within 4 % of each other, and the walking bots got about the same chunks each (6050 to 6510).
Every bot joined and none was dropped. Vanilla's 50-bot server is the exception: its watchdog stopped it.
- **Memory:** Horizon used 0.72 GB with one player and 1.11 GB with 50. The three Java servers used 3.4 to
4.5 GB (a 4 GB heap each), so Horizon was **3.8 to 4.8 times smaller** at every count.
- **Start:** **8.2 to 8.5 s** to `Done` for Horizon, against 14.7 to 15.5 s for vanilla, 16.6 to 17.6 s for
Fabric + Lithium and 19.0 to 22.4 s for Paper.
- **CPU** (100 % is one of the machine's 4 threads): from 10 players up, Horizon used the least: 143 % at 50,
against Paper's 210 % and Fabric's 236 %. With one player Paper used less (32 % against 46 %).
- **Average tick (MSPT), where Horizon loses:**
- With 1 player, Paper (10.3 ms) and Fabric + Lithium (12.5) were faster than Horizon (13.7). Vanilla was
slowest (17.3).
- With 10, Fabric (20.2) and Paper (20.6) were ahead of Horizon (22.9), which tied vanilla (22.9).
- With 25, Paper (28.3) was ahead of Horizon (30.9), and Horizon was ahead of Fabric (31.4) and vanilla (37.4).
- With 50, Paper (46.9) and Horizon (47.8) were close. Fabric + Lithium fell to 17.2 TPS (58.3 ms), and
vanilla's watchdog stopped its server.
- **Slow ticks, where Horizon wins:** Horizon's P99 was the lowest from 10 players up (72.0 ms at 25 and 73.2
at 50, against Paper's 90.4 and 109.7). Its worst 5-second P99 was the lowest at every count (133 ms at
50, against 213 for Paper and 888 for Fabric).
So on this 4-thread laptop CPU, Horizon is not yet the fastest on average tick time. Paper is ahead at every
count: Horizon's average tick is a third longer than Paper's with one player and 2 % longer with 50. Fabric +
Lithium is ahead with 1 and 10 players.
Horizon is ahead on memory, start time, CPU from 10 players up, and the steadiness of its ticks (P99). Its
average tick is what perf-server-11 is working on.
## Results
MSPT is milliseconds per tick from each server's own `/tick query` (50 is the limit for 20 TPS). P95 and P99
are means over the 5-second queries of the 300 measured seconds. "Worst P99" is the highest P99 of a single
query. RSS and CPU cover the server's process tree. "First-time chunks/s" counts chunks a bot hadn't
received before (summed over the bots). "Pulled back" counts position packets that moved a bot back.
| Bots | Server | MSPT | P95 | P99 | Worst P99 | TPS | RSS (mean / max) | CPU | Start | Chunks/s | First-time chunks/s | Pulled back |
|---:|---|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:|---:|
| 1 | Horizon | 13.70 | 19.06 | 27.38 | **43.4** | 20 | **0.72 / 0.72 GB** | 46 % | **8.3 s** | 9.94 | 0.01 | 0 |
| 1 | vanilla | 17.31 | 24.68 | 36.43 | 71.0 | 20 | 3.44 / 3.44 GB | 55 % | 14.9 s | 9.99 | 0.00 | 0 |
| 1 | Paper | **10.31** | 13.90 | 22.50 | 81.2 | 20 | 4.24 / 4.25 GB | **32 %** | 22.3 s | 9.81 | 0.02 | 0 |
| 1 | Fabric + Lithium | 12.46 | 17.69 | 32.74 | 70.1 | 20 | 3.43 / 3.43 GB | 46 % | 17.6 s | 9.98 | 0.00 | 0 |
| 10 | Horizon | 22.93 | 39.83 | 53.33 | **89.5** | 20 | **0.92 / 0.97 GB** | **67 %** | **8.2 s** | 61.21 | 1.78 | 3702 |
| 10 | vanilla | 22.94 | 36.92 | 56.48 | 196.6 | 20 | 3.71 / 3.76 GB | 101 % | 14.7 s | 61.63 | 1.84 | 3370 |
| 10 | Paper | 20.64 | 35.94 | 56.29 | 166.9 | 20 | 4.07 / 4.08 GB | 74 % | 20.5 s | 60.76 | 1.85 | 3974 |
| 10 | Fabric + Lithium | **20.18** | 37.04 | 56.08 | 217.9 | 20 | 3.73 / 3.78 GB | 94 % | 16.6 s | 61.65 | 1.84 | 4002 |
| 25 | Horizon | 30.92 | 48.25 | **72.00** | **121.1** | 20 | **1.01 / 1.12 GB** | **99 %** | **8.5 s** | 155.81 | 5.78 | 3008 |
| 25 | vanilla | 37.35 | 48.20 | 82.66 | 579.8 | 20 | 3.94 / 4.16 GB | 191 % | 15.5 s | 158.52 | 5.71 | 3762 |
| 25 | Paper | **28.30** | 61.68 | 90.40 | 124.3 | 20 | 4.16 / 4.25 GB | 124 % | 19.0 s | 156.35 | 5.72 | 4337 |
| 25 | Fabric + Lithium | 31.44 | 52.45 | 93.35 | 409.3 | 20 | 3.88 / 4.19 GB | 174 % | 17.5 s | 158.89 | 5.75 | 4194 |
| 50 | Horizon | 47.83 | 62.42 | **73.21** | **133.0** | 20 | **1.11 / 1.30 GB** | **143 %** | **8.3 s** | 302.68 | 11.02 | 8562 |
| 50 | vanilla | stopped by its watchdog | | | | | | | | | | |
| 50 | Paper | **46.86** | 61.16 | 109.72 | 213.4 | 20 | 4.35 / 4.52 GB | 210 % | 19.4 s | 310.51 | 11.16 | 7344 |
| 50 | Fabric + Lithium | 58.30 | 83.06 | 174.37 | 887.6 | 17.15 | 4.52 / 4.74 GB | 236 % | 17.5 s | 311.52 | 11.28 | 6350 |
Vanilla at 50: all 50 bots joined by 05:22:28 UTC. The server fell further and further behind ("Can't keep up!
… Running 16229ms or 324 ticks behind"), and at 05:28:17, during the warm-up, its watchdog stopped it: "A
single server tick took 60.00 seconds". That is the same as on 2026-10-18.
Chunks each bot received over the whole run, by behaviour (mean):
| Bots | Server | walk | explore | idle |
|---:|---|---:|---:|---:|
| 1 | Horizon | 6280 | | |
| 1 | vanilla | 6210 | | |
| 1 | Paper | 6049 | | |
| 1 | Fabric + Lithium | 6188 | | |
| 10 | Horizon | 6460 | 1195 | 473 |
| 10 | vanilla | 6272 | 1180 | 473 |
| 10 | Paper | 6177 | 1171 | 473 |
| 10 | Fabric + Lithium | 6291 | 1181 | 473 |
| 25 | Horizon | 6506 | 1262 | 473 |
| 25 | vanilla | 6307 | 1236 | 473 |
| 25 | Paper | 6280 | 1229 | 473 |
| 25 | Fabric + Lithium | 6357 | 1239 | 473 |
| 50 | Horizon | 6468 | 1251 | 473 |
| 50 | Paper | 6411 | 1224 | 473 |
| 50 | Fabric + Lithium | 6332 | 1228 | 473 |
## Equal work
`server_bench.py`'s work table compares, at each bot count, every server's chunks sent a second and
first-time chunks a second with the busiest server's. The work is equal when both are within 10 % (or within
a chunk a second when there are few).
| Bots | Chunks/s sent, first-time (Horizon; vanilla; Paper; Fabric + Lithium) | Equal |
|---:|---|---|
| 1 | 9.94, 0.01; 9.99, 0.00; 9.81, 0.02; 9.98, 0.00 | yes |
| 10 | 61.21, 1.78; 61.63, 1.84; 60.76, 1.85; 61.65, 1.84 | yes |
| 25 | 155.81, 5.78; 158.52, 5.71; 156.35, 5.72; 158.89, 5.75 | yes |
| 50 | 302.68, 11.02; (stopped); 310.51, 11.16; 311.52, 11.28 | yes |
Horizon pulled no bot back for moving too quickly (`moved too quickly`: 0 at every count). The Java servers
logged 0 to 46 such lines a run. All four sent position corrections ("Pulled back") at similar rates.
## Method
`tools/server_bench.py` ran each configuration on a fresh server:
- the same `server.properties` on all four: seed 12345, view and simulation distance 10, offline, creative,
difficulty easy, `sync-chunk-writes=true`, white-list off;
- `gamerule respawn_radius 0`, and otherwise each server's default game rules;
- the server waits for `Done`, the bots join one a second, warm up for 300 seconds and are measured for 300,
with `/tick query` every 5 seconds and RSS and CPU every second.
The bots are `horizon-bots --behaviour mixed`: 60 % walk, 20 % explore and 20 % idle, each choice deterministic
per bot. All are creative and fly 24 blocks above spawn. `walk` flies to random points within 48 blocks at
5.6 blocks a second, `explore` flies straight out at 0.8 blocks a second (`--explore-speed 0.8`), and `idle`
hovers.
**Machine.** The ThinkPad: Intel Core i7-7500U (2 cores, 4 threads), 32 GB, NixOS. The desktop was meant to
run it but didn't wake (`~/fleet/power wake desktop` timed out twice). No other session ran on the ThinkPad
(`load.txt`, a sample a minute: 0 other sessions throughout). The benchmark ran under `nice -n 19` and
`ionice -c3`. The bots ran on the Mac mini (M4 Pro, shared with other work at 51 to 98 % CPU, 82 % on
average) and reached the ThinkPad over the tailnet.
**What ran.**
- Horizon: release v2026.10.19 (07f2913d), the server archive `horizon-server-linux-x86_64.tar.gz` installed
with `install.sh`. `horizon-server` ran its `horizon-engine` with game files prepared from the owner's own
Minecraft.
- Vanilla: the pinned 26.3 server jar (the owner's own copy) on OpenJDK 25.0.4.1, `-Xms4g -Xmx4g`.
- Paper: 26.3 build 205 (`26.3-205-main@a90108f`), from PaperMC's download API, checked against its SHA-256.
It ran with Paper's defaults except `connection-throttle: -1` in `bukkit.yml`. Every bot connects from one
address, and the default throttle (4 s) refused all but the first. Paperclip patched the pinned server jar
from its cache. Same Java and heap.
- Fabric + Lithium: the pinned server jar on Fabric loader 0.19.5 (`packs/fabric.lock.json`) with Lithium
`mc26.3-0.26.2-fabric` (`packs/mods.lock.json`, checked against its SHA-512). Lithium needs no Fabric API.
Same Java and heap.
The Paper and Fabric jars are dev-only downloads into `.local/server-bench/jars` and are never committed or
shipped.
**How it ran.** The first pass ran all four servers at 1 and 10 bots, but its Paper 10-bot run hit the
connection throttle (1 of 10 bots joined), and it was stopped after that. The 1-bot and 10-bot lines for
Horizon, vanilla and Fabric come from that pass, along with Paper's 1-bot line. A second pass with the
throttle off ran all four at 25 and 50 bots, then Paper at 10. All runs were on the same machine on the
same morning (02:57 to 06:02 UTC). `runs.jsonl` holds the 16 run lines used here.
`report-25-50.json` and `report-paper-10.json` are the second pass's reports. The first pass, stopped
early, wrote no report. Its lines come from its output.
## Reproduce
curl -fsSL https://raw.githubusercontent.com/ludovic111/horizon/main/packaging/server/install.sh | sh
horizon-prepare --prepare --server-jar /path/to/server.jar # your own copy
cargo build --release -p horizon-bots # on the machine that runs the bots
# in a checkout whose .local/minecraft holds the pinned vanilla jars (tools/lab.py prepare):
HORIZON_BENCH_BIN=~/.local/share/horizon/current \
python3 tools/server_bench.py --servers horizon,vanilla,paper,fabric --bots 1,10,25,50 --behaviour mixed \
--explore-speed 0.8 --warmup 300 --nice 19 \
--bots-via 'ssh <bots machine>' --bots-bin /path/to/horizon-bots --connect <this machine's address> \
--out four-way.json
## Next
- One pass per configuration ran, on a 4-thread laptop CPU. A rerun on a bigger machine (the desktop's
24 threads) would show which server uses more cores best, and give vanilla at 50 a second chance.
- Horizon's average tick: where its 1-player tick goes (13.7 ms against Paper's 10.3). That work is
perf-server-11's.
- 100 bots.
@@ -0,0 +1,870 @@
2026-10-19T02:57:31Z
ludothinkpad ok: 29.0 GB free, 4 threads 1% busy, 0 sessions
desktop offline
macmini busy: 28.7 GB free, 14 threads 89% busy, 1 sessions
macbook offline
2026-10-19T02:58:36Z
ludothinkpad ok: 28.3 GB free, 4 threads 20% busy, 0 sessions
desktop offline
macmini busy: 26.0 GB free, 14 threads 92% busy, 1 sessions
macbook offline
2026-10-19T02:59:40Z
ludothinkpad ok: 28.3 GB free, 4 threads 20% busy, 0 sessions
desktop offline
macmini busy: 26.8 GB free, 14 threads 96% busy, 1 sessions
macbook offline
2026-10-19T03:00:44Z
ludothinkpad ok: 28.3 GB free, 4 threads 15% busy, 0 sessions
desktop offline
macmini busy: 25.8 GB free, 14 threads 97% busy, 1 sessions
macbook offline
2026-10-19T03:01:48Z
ludothinkpad ok: 27.8 GB free, 4 threads 27% busy, 0 sessions
desktop offline
macmini ok: 26.8 GB free, 14 threads 84% busy, 1 sessions
macbook offline
2026-10-19T03:02:52Z
ludothinkpad ok: 28.4 GB free, 4 threads 18% busy, 0 sessions
desktop offline
macmini busy: 25.7 GB free, 14 threads 93% busy, 1 sessions
macbook offline
2026-10-19T03:03:56Z
ludothinkpad ok: 28.4 GB free, 4 threads 14% busy, 0 sessions
desktop offline
macmini busy: 29.6 GB free, 14 threads 96% busy, 1 sessions
macbook offline
2026-10-19T03:05:00Z
ludothinkpad ok: 28.4 GB free, 4 threads 11% busy, 0 sessions
desktop offline
macmini ok: 29.4 GB free, 14 threads 84% busy, 1 sessions
macbook offline
2026-10-19T03:06:04Z
ludothinkpad ok: 28.3 GB free, 4 threads 22% busy, 0 sessions
desktop offline
macmini ok: 26.8 GB free, 14 threads 82% busy, 1 sessions
macbook offline
2026-10-19T03:07:09Z
ludothinkpad ok: 28.4 GB free, 4 threads 11% busy, 0 sessions
desktop offline
macmini busy: 26.1 GB free, 14 threads 91% busy, 1 sessions
macbook offline
2026-10-19T03:08:13Z
ludothinkpad busy: 26.9 GB free, 4 threads 92% busy, 0 sessions
desktop offline
macmini ok: 26.4 GB free, 14 threads 76% busy, 1 sessions
macbook offline
2026-10-19T03:09:17Z
ludothinkpad ok: 25.8 GB free, 4 threads 19% busy, 0 sessions
desktop offline
macmini busy: 29.5 GB free, 14 threads 87% busy, 1 sessions
macbook offline
2026-10-19T03:10:21Z
ludothinkpad ok: 25.8 GB free, 4 threads 49% busy, 0 sessions
desktop offline
macmini ok: 25.8 GB free, 14 threads 79% busy, 1 sessions
macbook offline
2026-10-19T03:11:25Z
ludothinkpad ok: 25.8 GB free, 4 threads 19% busy, 0 sessions
desktop offline
macmini busy: 26.7 GB free, 14 threads 85% busy, 1 sessions
macbook offline
2026-10-19T03:12:29Z
ludothinkpad ok: 25.8 GB free, 4 threads 18% busy, 0 sessions
desktop offline
macmini busy: 26.2 GB free, 14 threads 88% busy, 1 sessions
macbook offline
2026-10-19T03:13:33Z
ludothinkpad ok: 25.8 GB free, 4 threads 17% busy, 0 sessions
desktop offline
macmini ok: 28.0 GB free, 14 threads 84% busy, 1 sessions
macbook offline
2026-10-19T03:14:37Z
ludothinkpad ok: 25.8 GB free, 4 threads 17% busy, 0 sessions
desktop offline
macmini ok: 28.7 GB free, 14 threads 76% busy, 1 sessions
macbook offline
2026-10-19T03:15:41Z
ludothinkpad ok: 25.8 GB free, 4 threads 18% busy, 0 sessions
desktop offline
macmini busy: 26.2 GB free, 14 threads 87% busy, 1 sessions
macbook offline
2026-10-19T03:16:46Z
ludothinkpad ok: 25.8 GB free, 4 threads 18% busy, 0 sessions
desktop offline
macmini busy: 25.0 GB free, 14 threads 91% busy, 1 sessions
macbook offline
2026-10-19T03:17:50Z
ludothinkpad ok: 25.8 GB free, 4 threads 19% busy, 0 sessions
desktop offline
macmini ok: 25.6 GB free, 14 threads 78% busy, 1 sessions
macbook offline
2026-10-19T03:18:54Z
ludothinkpad ok: 26.7 GB free, 4 threads 71% busy, 0 sessions
desktop offline
macmini busy: 26.2 GB free, 14 threads 91% busy, 1 sessions
macbook offline
2026-10-19T03:19:58Z
ludothinkpad ok: 25.1 GB free, 4 threads 16% busy, 0 sessions
desktop offline
macmini busy: 25.6 GB free, 14 threads 85% busy, 1 sessions
macbook offline
2026-10-19T03:21:02Z
ludothinkpad ok: 25.1 GB free, 4 threads 15% busy, 0 sessions
desktop offline
macmini busy: 25.7 GB free, 14 threads 87% busy, 1 sessions
macbook offline
2026-10-19T03:22:06Z
ludothinkpad ok: 25.1 GB free, 4 threads 12% busy, 0 sessions
desktop offline
macmini busy: 26.8 GB free, 14 threads 87% busy, 1 sessions
macbook offline
2026-10-19T03:23:10Z
ludothinkpad ok: 25.1 GB free, 4 threads 12% busy, 0 sessions
desktop offline
macmini ok: 25.6 GB free, 14 threads 81% busy, 1 sessions
macbook offline
2026-10-19T03:24:14Z
ludothinkpad ok: 25.1 GB free, 4 threads 19% busy, 0 sessions
desktop offline
macmini ok: 27.1 GB free, 14 threads 82% busy, 1 sessions
macbook offline
2026-10-19T03:25:19Z
ludothinkpad ok: 25.1 GB free, 4 threads 9% busy, 0 sessions
desktop offline
macmini busy: 28.6 GB free, 14 threads 86% busy, 1 sessions
macbook offline
2026-10-19T03:26:23Z
ludothinkpad ok: 25.1 GB free, 4 threads 13% busy, 0 sessions
desktop offline
macmini ok: 25.9 GB free, 14 threads 71% busy, 1 sessions
macbook offline
2026-10-19T03:27:27Z
ludothinkpad ok: 25.1 GB free, 4 threads 14% busy, 0 sessions
desktop offline
macmini busy: 26.0 GB free, 14 threads 94% busy, 1 sessions
macbook offline
2026-10-19T03:28:31Z
ludothinkpad ok: 25.1 GB free, 4 threads 13% busy, 0 sessions
desktop offline
macmini ok: 26.1 GB free, 14 threads 79% busy, 1 sessions
macbook offline
2026-10-19T03:29:35Z
ludothinkpad ok: 27.5 GB free, 4 threads 36% busy, 0 sessions
desktop offline
macmini ok: 25.5 GB free, 14 threads 84% busy, 1 sessions
macbook offline
2026-10-19T03:30:39Z
ludothinkpad ok: 25.8 GB free, 4 threads 22% busy, 0 sessions
desktop offline
macmini ok: 26.2 GB free, 14 threads 73% busy, 1 sessions
macbook offline
2026-10-19T03:31:43Z
ludothinkpad ok: 25.8 GB free, 4 threads 20% busy, 0 sessions
desktop offline
macmini busy: 26.1 GB free, 14 threads 86% busy, 1 sessions
macbook offline
2026-10-19T03:32:47Z
ludothinkpad ok: 25.8 GB free, 4 threads 21% busy, 0 sessions
desktop offline
macmini busy: 28.4 GB free, 14 threads 90% busy, 1 sessions
macbook offline
2026-10-19T03:33:52Z
ludothinkpad ok: 25.8 GB free, 4 threads 18% busy, 0 sessions
desktop offline
macmini ok: 26.9 GB free, 14 threads 84% busy, 2 sessions
macbook offline
2026-10-19T03:34:56Z
ludothinkpad ok: 25.8 GB free, 4 threads 14% busy, 0 sessions
desktop offline
macmini ok: 26.1 GB free, 14 threads 76% busy, 1 sessions
macbook offline
2026-10-19T03:36:00Z
ludothinkpad ok: 25.8 GB free, 4 threads 16% busy, 0 sessions
desktop offline
macmini ok: 27.4 GB free, 14 threads 78% busy, 1 sessions
macbook offline
2026-10-19T03:37:04Z
ludothinkpad ok: 25.8 GB free, 4 threads 15% busy, 0 sessions
desktop offline
macmini ok: 26.6 GB free, 14 threads 77% busy, 1 sessions
macbook offline
2026-10-19T03:38:08Z
ludothinkpad ok: 25.8 GB free, 4 threads 14% busy, 0 sessions
desktop offline
macmini busy: 25.8 GB free, 14 threads 94% busy, 1 sessions
macbook offline
2026-10-19T03:39:12Z
ludothinkpad ok: 25.8 GB free, 4 threads 17% busy, 0 sessions
desktop offline
macmini busy: 27.1 GB free, 14 threads 91% busy, 1 sessions
macbook offline
2026-10-19T03:40:16Z
ludothinkpad ok: 28.4 GB free, 4 threads 73% busy, 0 sessions
desktop offline
macmini ok: 25.5 GB free, 14 threads 78% busy, 1 sessions
macbook offline
2026-10-19T03:41:20Z
ludothinkpad ok: 28.3 GB free, 4 threads 41% busy, 0 sessions
desktop offline
macmini busy: 25.8 GB free, 14 threads 86% busy, 1 sessions
macbook offline
2026-10-19T03:42:24Z
ludothinkpad ok: 28.3 GB free, 4 threads 24% busy, 0 sessions
desktop offline
macmini busy: 26.9 GB free, 14 threads 88% busy, 1 sessions
macbook offline
2026-10-19T03:43:29Z
ludothinkpad ok: 28.3 GB free, 4 threads 26% busy, 0 sessions
desktop offline
macmini busy: 25.0 GB free, 14 threads 87% busy, 1 sessions
macbook offline
2026-10-19T03:44:33Z
ludothinkpad ok: 28.2 GB free, 4 threads 22% busy, 0 sessions
desktop offline
macmini ok: 25.6 GB free, 14 threads 81% busy, 1 sessions
macbook offline
2026-10-19T03:45:37Z
ludothinkpad ok: 28.2 GB free, 4 threads 27% busy, 0 sessions
desktop offline
macmini ok: 27.8 GB free, 14 threads 76% busy, 1 sessions
macbook offline
2026-10-19T03:46:41Z
ludothinkpad ok: 28.1 GB free, 4 threads 37% busy, 0 sessions
desktop offline
macmini ok: 27.3 GB free, 14 threads 81% busy, 1 sessions
macbook offline
2026-10-19T03:47:45Z
ludothinkpad ok: 28.1 GB free, 4 threads 21% busy, 0 sessions
desktop offline
macmini ok: 27.1 GB free, 14 threads 82% busy, 1 sessions
macbook offline
2026-10-19T03:48:49Z
ludothinkpad ok: 28.1 GB free, 4 threads 32% busy, 0 sessions
desktop offline
macmini ok: 26.2 GB free, 14 threads 82% busy, 1 sessions
macbook offline
2026-10-19T03:49:53Z
ludothinkpad ok: 28.1 GB free, 4 threads 25% busy, 0 sessions
desktop offline
macmini busy: 25.7 GB free, 14 threads 85% busy, 1 sessions
macbook offline
2026-10-19T03:50:57Z
ludothinkpad busy: 26.6 GB free, 4 threads 94% busy, 0 sessions
desktop offline
macmini ok: 25.1 GB free, 14 threads 85% busy, 1 sessions
macbook offline
2026-10-19T03:52:01Z
ludothinkpad ok: 25.8 GB free, 4 threads 28% busy, 0 sessions
desktop offline
macmini busy: 25.1 GB free, 14 threads 91% busy, 1 sessions
macbook offline
2026-10-19T03:53:06Z
ludothinkpad ok: 25.8 GB free, 4 threads 50% busy, 0 sessions
desktop offline
macmini busy: 26.4 GB free, 14 threads 87% busy, 1 sessions
macbook offline
2026-10-19T03:54:10Z
ludothinkpad ok: 25.7 GB free, 4 threads 41% busy, 0 sessions
desktop offline
macmini busy: 25.8 GB free, 14 threads 87% busy, 1 sessions
macbook offline
2026-10-19T03:55:14Z
ludothinkpad ok: 25.6 GB free, 4 threads 27% busy, 0 sessions
desktop offline
macmini busy: 31.4 GB free, 14 threads 90% busy, 1 sessions
macbook offline
2026-10-19T03:56:18Z
ludothinkpad ok: 25.6 GB free, 4 threads 26% busy, 0 sessions
desktop offline
macmini busy: 31.0 GB free, 14 threads 88% busy, 1 sessions
macbook offline
2026-10-19T03:57:22Z
ludothinkpad ok: 25.6 GB free, 4 threads 33% busy, 0 sessions
desktop offline
macmini busy: 26.5 GB free, 14 threads 91% busy, 1 sessions
macbook offline
2026-10-19T03:58:26Z
ludothinkpad ok: 25.5 GB free, 4 threads 45% busy, 0 sessions
desktop offline
macmini busy: 26.7 GB free, 14 threads 89% busy, 1 sessions
macbook offline
2026-10-19T03:59:30Z
ludothinkpad ok: 25.5 GB free, 4 threads 24% busy, 0 sessions
desktop offline
macmini ok: 25.6 GB free, 14 threads 77% busy, 1 sessions
macbook offline
2026-10-19T04:00:34Z
ludothinkpad ok: 25.5 GB free, 4 threads 31% busy, 0 sessions
desktop offline
macmini ok: 25.0 GB free, 14 threads 81% busy, 1 sessions
macbook offline
2026-10-19T04:01:38Z
ludothinkpad busy: 27.0 GB free, 4 threads 99% busy, 0 sessions
desktop offline
macmini ok: 28.3 GB free, 14 threads 82% busy, 1 sessions
macbook offline
2026-10-19T04:02:42Z
ludothinkpad ok: 26.2 GB free, 4 threads 19% busy, 0 sessions
desktop offline
macmini busy: 26.5 GB free, 14 threads 87% busy, 1 sessions
macbook offline
2026-10-19T04:03:47Z
ludothinkpad ok: 25.9 GB free, 4 threads 14% busy, 0 sessions
desktop offline
macmini ok: 27.9 GB free, 14 threads 78% busy, 1 sessions
macbook offline
2026-10-19T04:04:51Z
ludothinkpad ok: 25.7 GB free, 4 threads 11% busy, 0 sessions
desktop offline
macmini busy: 30.0 GB free, 14 threads 91% busy, 1 sessions
macbook offline
2026-10-19T04:05:55Z
ludothinkpad ok: 25.3 GB free, 4 threads 10% busy, 0 sessions
desktop offline
macmini busy: 27.5 GB free, 14 threads 88% busy, 1 sessions
macbook offline
2026-10-19T04:06:59Z
ludothinkpad ok: 25.3 GB free, 4 threads 13% busy, 0 sessions
desktop offline
macmini ok: 26.9 GB free, 14 threads 77% busy, 1 sessions
macbook offline
2026-10-19T04:08:03Z
ludothinkpad ok: 25.3 GB free, 4 threads 12% busy, 0 sessions
desktop offline
macmini ok: 25.8 GB free, 14 threads 81% busy, 1 sessions
macbook offline
2026-10-19T04:09:07Z
ludothinkpad ok: 25.3 GB free, 4 threads 10% busy, 0 sessions
desktop offline
macmini busy: 26.9 GB free, 14 threads 86% busy, 1 sessions
macbook offline
2026-10-19T04:10:11Z
ludothinkpad ok: 25.3 GB free, 4 threads 8% busy, 0 sessions
desktop offline
macmini busy: 26.6 GB free, 14 threads 92% busy, 1 sessions
macbook offline
2026-10-19T04:11:15Z
ludothinkpad ok: 25.3 GB free, 4 threads 9% busy, 0 sessions
desktop offline
macmini ok: 27.3 GB free, 14 threads 77% busy, 1 sessions
macbook offline
2026-10-19T04:12:20Z
ludothinkpad busy: 27.5 GB free, 4 threads 88% busy, 0 sessions
desktop offline
macmini busy: 26.5 GB free, 14 threads 86% busy, 1 sessions
macbook offline
2026-10-19T04:13:24Z
ludothinkpad ok: 25.8 GB free, 4 threads 44% busy, 0 sessions
desktop offline
macmini busy: 27.6 GB free, 14 threads 90% busy, 1 sessions
macbook offline
2026-10-19T04:14:28Z
ludothinkpad ok: 25.7 GB free, 4 threads 28% busy, 0 sessions
desktop offline
macmini busy: 27.8 GB free, 14 threads 87% busy, 1 sessions
macbook offline
2026-10-19T04:15:32Z
ludothinkpad ok: 25.7 GB free, 4 threads 26% busy, 0 sessions
desktop offline
macmini ok: 26.7 GB free, 14 threads 73% busy, 1 sessions
macbook offline
2026-10-19T04:16:36Z
ludothinkpad ok: 25.6 GB free, 4 threads 28% busy, 0 sessions
desktop offline
macmini ok: 29.3 GB free, 14 threads 80% busy, 1 sessions
macbook offline
2026-10-19T04:17:40Z
ludothinkpad ok: 25.6 GB free, 4 threads 32% busy, 0 sessions
desktop offline
macmini busy: 26.3 GB free, 14 threads 90% busy, 1 sessions
macbook offline
2026-10-19T04:18:44Z
ludothinkpad ok: 25.5 GB free, 4 threads 76% busy, 0 sessions
desktop offline
macmini ok: 28.4 GB free, 14 threads 84% busy, 1 sessions
macbook offline
2026-10-19T04:19:48Z
ludothinkpad ok: 25.5 GB free, 4 threads 26% busy, 0 sessions
desktop offline
macmini ok: 30.4 GB free, 14 threads 79% busy, 1 sessions
macbook offline
2026-10-19T04:20:52Z
ludothinkpad ok: 25.5 GB free, 4 threads 26% busy, 0 sessions
desktop offline
macmini ok: 28.1 GB free, 14 threads 73% busy, 1 sessions
macbook offline
2026-10-19T04:21:57Z
ludothinkpad ok: 25.5 GB free, 4 threads 28% busy, 0 sessions
desktop offline
macmini ok: 31.0 GB free, 14 threads 84% busy, 1 sessions
macbook offline
2026-10-19T04:23:01Z
ludothinkpad ok: 25.5 GB free, 4 threads 10% busy, 0 sessions
desktop offline
macmini ok: 31.7 GB free, 14 threads 83% busy, 1 sessions
macbook offline
2026-10-19T04:24:05Z
ludothinkpad ok: 28.3 GB free, 4 threads 41% busy, 0 sessions
desktop offline
macmini ok: 27.1 GB free, 14 threads 71% busy, 1 sessions
macbook offline
2026-10-19T04:25:15Z
ludothinkpad ok: 28.8 GB free, 4 threads 4% busy, 0 sessions
desktop offline
macmini busy: 25.7 GB free, 14 threads 98% busy, 1 sessions
macbook offline
2026-10-19T04:26:19Z
ludothinkpad ok: 28.3 GB free, 4 threads 40% busy, 0 sessions
desktop offline
macmini ok: 32.1 GB free, 14 threads 65% busy, 1 sessions
macbook offline
2026-10-19T04:27:23Z
ludothinkpad ok: 28.2 GB free, 4 threads 41% busy, 0 sessions
desktop offline
macmini busy: 29.4 GB free, 14 threads 89% busy, 1 sessions
macbook offline
2026-10-19T04:28:27Z
ludothinkpad ok: 28.2 GB free, 4 threads 44% busy, 0 sessions
desktop offline
macmini ok: 28.9 GB free, 14 threads 77% busy, 1 sessions
macbook offline
2026-10-19T04:29:32Z
ludothinkpad ok: 28.1 GB free, 4 threads 33% busy, 0 sessions
desktop offline
macmini ok: 25.3 GB free, 14 threads 71% busy, 1 sessions
macbook offline
2026-10-19T04:30:36Z
ludothinkpad ok: 28.1 GB free, 4 threads 31% busy, 0 sessions
desktop offline
macmini ok: 30.0 GB free, 14 threads 63% busy, 1 sessions
macbook offline
2026-10-19T04:31:40Z
ludothinkpad ok: 28.0 GB free, 4 threads 40% busy, 0 sessions
desktop offline
macmini ok: 27.4 GB free, 14 threads 73% busy, 1 sessions
macbook offline
2026-10-19T04:32:44Z
ludothinkpad ok: 28.0 GB free, 4 threads 35% busy, 0 sessions
desktop offline
macmini ok: 29.3 GB free, 14 threads 51% busy, 1 sessions
macbook offline
2026-10-19T04:33:48Z
ludothinkpad ok: 28.0 GB free, 4 threads 31% busy, 0 sessions
desktop offline
macmini ok: 27.5 GB free, 14 threads 67% busy, 1 sessions
macbook offline
2026-10-19T04:34:52Z
ludothinkpad ok: 28.0 GB free, 4 threads 35% busy, 0 sessions
desktop offline
macmini ok: 26.6 GB free, 14 threads 82% busy, 1 sessions
macbook offline
2026-10-19T04:35:56Z
ludothinkpad ok: 27.9 GB free, 4 threads 34% busy, 0 sessions
desktop offline
macmini ok: 28.9 GB free, 14 threads 66% busy, 1 sessions
macbook offline
2026-10-19T04:37:00Z
ludothinkpad busy: 25.8 GB free, 4 threads 98% busy, 0 sessions
desktop offline
macmini ok: 26.9 GB free, 14 threads 75% busy, 1 sessions
macbook offline
2026-10-19T04:38:04Z
ludothinkpad ok: 25.7 GB free, 4 threads 64% busy, 0 sessions
desktop offline
macmini ok: 25.6 GB free, 14 threads 74% busy, 1 sessions
macbook offline
2026-10-19T04:39:09Z
ludothinkpad ok: 25.6 GB free, 4 threads 64% busy, 0 sessions
desktop offline
macmini ok: 26.8 GB free, 14 threads 77% busy, 1 sessions
macbook offline
2026-10-19T04:40:13Z
ludothinkpad ok: 25.5 GB free, 4 threads 51% busy, 0 sessions
desktop offline
macmini ok: 26.3 GB free, 14 threads 70% busy, 1 sessions
macbook offline
2026-10-19T04:41:17Z
ludothinkpad ok: 25.4 GB free, 4 threads 62% busy, 0 sessions
desktop offline
macmini ok: 26.3 GB free, 14 threads 83% busy, 1 sessions
macbook offline
2026-10-19T04:42:21Z
ludothinkpad ok: 25.4 GB free, 4 threads 47% busy, 0 sessions
desktop offline
macmini ok: 26.2 GB free, 14 threads 68% busy, 1 sessions
macbook offline
2026-10-19T04:43:25Z
ludothinkpad ok: 25.4 GB free, 4 threads 67% busy, 0 sessions
desktop offline
macmini busy: 26.9 GB free, 14 threads 88% busy, 1 sessions
macbook offline
2026-10-19T04:44:29Z
ludothinkpad ok: 25.3 GB free, 4 threads 64% busy, 0 sessions
desktop offline
macmini ok: 30.4 GB free, 14 threads 79% busy, 1 sessions
macbook offline
2026-10-19T04:45:33Z
ludothinkpad ok: 25.2 GB free, 4 threads 66% busy, 0 sessions
desktop offline
macmini ok: 25.0 GB free, 14 threads 69% busy, 1 sessions
macbook offline
2026-10-19T04:46:37Z
ludothinkpad ok: 25.1 GB free, 4 threads 48% busy, 0 sessions
desktop offline
macmini busy: 26.4 GB free, 14 threads 87% busy, 1 sessions
macbook offline
2026-10-19T04:47:42Z
ludothinkpad ok: 27.5 GB free, 4 threads 69% busy, 0 sessions
desktop offline
macmini ok: 28.0 GB free, 14 threads 83% busy, 1 sessions
macbook offline
2026-10-19T04:48:46Z
ludothinkpad ok: 25.3 GB free, 4 threads 83% busy, 0 sessions
desktop offline
macmini busy: 25.6 GB free, 14 threads 85% busy, 1 sessions
macbook offline
2026-10-19T04:49:50Z
ludothinkpad ok: 25.3 GB free, 4 threads 55% busy, 0 sessions
desktop offline
macmini busy: 25.3 GB free, 14 threads 88% busy, 1 sessions
macbook offline
2026-10-19T04:50:54Z
ludothinkpad ok: 25.2 GB free, 4 threads 51% busy, 0 sessions
desktop offline
macmini ok: 30.9 GB free, 14 threads 74% busy, 1 sessions
macbook offline
2026-10-19T04:51:58Z
ludothinkpad ok: 25.2 GB free, 4 threads 57% busy, 0 sessions
desktop offline
macmini ok: 29.2 GB free, 14 threads 83% busy, 1 sessions
macbook offline
2026-10-19T04:53:02Z
ludothinkpad ok: 25.1 GB free, 4 threads 59% busy, 0 sessions
desktop offline
macmini busy: 26.2 GB free, 14 threads 88% busy, 1 sessions
macbook offline
2026-10-19T04:54:06Z
ludothinkpad ok: 25.1 GB free, 4 threads 38% busy, 0 sessions
desktop offline
macmini busy: 25.4 GB free, 14 threads 90% busy, 2 sessions
macbook offline
2026-10-19T04:55:10Z
ludothinkpad ok: 25.1 GB free, 4 threads 43% busy, 0 sessions
desktop offline
macmini busy: 25.4 GB free, 14 threads 85% busy, 1 sessions
macbook offline
2026-10-19T04:56:15Z
ludothinkpad ok: 25.1 GB free, 4 threads 42% busy, 0 sessions
desktop offline
macmini ok: 27.6 GB free, 14 threads 80% busy, 1 sessions
macbook offline
2026-10-19T04:57:19Z
ludothinkpad ok: 25.0 GB free, 4 threads 45% busy, 0 sessions
desktop offline
macmini ok: 27.0 GB free, 14 threads 67% busy, 1 sessions
macbook offline
2026-10-19T04:58:23Z
ludothinkpad ok: 25.0 GB free, 4 threads 48% busy, 0 sessions
desktop offline
macmini ok: 27.4 GB free, 14 threads 74% busy, 1 sessions
macbook offline
2026-10-19T04:59:27Z
ludothinkpad busy: 26.6 GB free, 4 threads 100% busy, 0 sessions
desktop offline
macmini busy: 27.7 GB free, 14 threads 91% busy, 1 sessions
macbook offline
2026-10-19T05:00:31Z
ludothinkpad ok: 25.6 GB free, 4 threads 53% busy, 0 sessions
desktop offline
macmini ok: 32.2 GB free, 14 threads 64% busy, 1 sessions
macbook offline
2026-10-19T05:01:35Z
ludothinkpad ok: 25.7 GB free, 4 threads 54% busy, 0 sessions
desktop offline
macmini ok: 26.1 GB free, 14 threads 85% busy, 1 sessions
macbook offline
2026-10-19T05:02:39Z
ludothinkpad ok: 25.6 GB free, 4 threads 59% busy, 0 sessions
desktop offline
macmini ok: 25.4 GB free, 14 threads 84% busy, 1 sessions
macbook offline
2026-10-19T05:03:43Z
ludothinkpad ok: 25.6 GB free, 4 threads 55% busy, 0 sessions
desktop offline
macmini busy: 25.3 GB free, 14 threads 87% busy, 1 sessions
macbook offline
2026-10-19T05:04:48Z
ludothinkpad busy: 25.0 GB free, 4 threads 96% busy, 0 sessions
desktop offline
macmini ok: 26.6 GB free, 14 threads 79% busy, 1 sessions
macbook offline
2026-10-19T05:05:52Z
ludothinkpad ok: 25.4 GB free, 4 threads 51% busy, 0 sessions
desktop offline
macmini ok: 27.8 GB free, 14 threads 84% busy, 1 sessions
macbook offline
2026-10-19T05:06:56Z
ludothinkpad ok: 25.4 GB free, 4 threads 46% busy, 0 sessions
desktop offline
macmini busy: 25.9 GB free, 14 threads 92% busy, 1 sessions
macbook offline
2026-10-19T05:08:00Z
ludothinkpad ok: 25.4 GB free, 4 threads 53% busy, 0 sessions
desktop offline
macmini busy: 29.0 GB free, 14 threads 88% busy, 1 sessions
macbook offline
2026-10-19T05:09:04Z
ludothinkpad ok: 25.1 GB free, 4 threads 64% busy, 0 sessions
desktop offline
macmini ok: 27.3 GB free, 14 threads 81% busy, 1 sessions
macbook offline
2026-10-19T05:10:08Z
ludothinkpad ok: 28.6 GB free, 4 threads 28% busy, 0 sessions
desktop offline
macmini ok: 26.8 GB free, 14 threads 78% busy, 1 sessions
macbook offline
2026-10-19T05:11:12Z
ludothinkpad ok: 28.2 GB free, 4 threads 48% busy, 0 sessions
desktop offline
macmini busy: 28.5 GB free, 14 threads 88% busy, 1 sessions
macbook offline
2026-10-19T05:12:16Z
ludothinkpad ok: 28.2 GB free, 4 threads 52% busy, 0 sessions
desktop offline
macmini ok: 27.3 GB free, 14 threads 74% busy, 1 sessions
macbook offline
2026-10-19T05:13:21Z
ludothinkpad ok: 28.1 GB free, 4 threads 41% busy, 0 sessions
desktop offline
macmini busy: 25.9 GB free, 14 threads 87% busy, 1 sessions
macbook offline
2026-10-19T05:14:25Z
ludothinkpad ok: 28.1 GB free, 4 threads 56% busy, 0 sessions
desktop offline
macmini busy: 26.7 GB free, 14 threads 88% busy, 1 sessions
macbook offline
2026-10-19T05:15:29Z
ludothinkpad ok: 28.0 GB free, 4 threads 50% busy, 0 sessions
desktop offline
macmini busy: 25.4 GB free, 14 threads 90% busy, 1 sessions
macbook offline
2026-10-19T05:16:33Z
ludothinkpad ok: 27.9 GB free, 4 threads 47% busy, 0 sessions
desktop offline
macmini ok: 29.1 GB free, 14 threads 77% busy, 1 sessions
macbook offline
2026-10-19T05:17:37Z
ludothinkpad ok: 27.9 GB free, 4 threads 61% busy, 0 sessions
desktop offline
macmini ok: 27.5 GB free, 14 threads 75% busy, 1 sessions
macbook offline
2026-10-19T05:18:41Z
ludothinkpad ok: 27.9 GB free, 4 threads 68% busy, 0 sessions
desktop offline
macmini ok: 27.3 GB free, 14 threads 76% busy, 1 sessions
macbook offline
2026-10-19T05:19:45Z
ludothinkpad ok: 27.8 GB free, 4 threads 45% busy, 0 sessions
desktop offline
macmini busy: 27.5 GB free, 14 threads 85% busy, 1 sessions
macbook offline
2026-10-19T05:20:49Z
ludothinkpad ok: 27.5 GB free, 4 threads 60% busy, 0 sessions
desktop offline
macmini busy: 26.6 GB free, 14 threads 95% busy, 1 sessions
macbook offline
2026-10-19T05:21:53Z
ludothinkpad busy: 26.7 GB free, 4 threads 99% busy, 0 sessions
desktop offline
macmini ok: 29.8 GB free, 14 threads 72% busy, 1 sessions
macbook offline
2026-10-19T05:22:58Z
ludothinkpad busy: 25.7 GB free, 4 threads 97% busy, 0 sessions
desktop offline
macmini busy: 26.6 GB free, 14 threads 86% busy, 1 sessions
macbook offline
2026-10-19T05:24:02Z
ludothinkpad busy: 25.4 GB free, 4 threads 90% busy, 0 sessions
desktop offline
macmini busy: 28.2 GB free, 14 threads 85% busy, 1 sessions
macbook offline
2026-10-19T05:25:06Z
ludothinkpad ok: 25.3 GB free, 4 threads 73% busy, 0 sessions
desktop offline
macmini busy: 27.8 GB free, 14 threads 89% busy, 1 sessions
macbook offline
2026-10-19T05:26:10Z
ludothinkpad ok: 25.2 GB free, 4 threads 69% busy, 0 sessions
desktop offline
macmini ok: 28.8 GB free, 14 threads 74% busy, 1 sessions
macbook offline
2026-10-19T05:27:14Z
ludothinkpad ok: 25.2 GB free, 4 threads 82% busy, 0 sessions
desktop offline
macmini ok: 29.6 GB free, 14 threads 80% busy, 1 sessions
macbook offline
2026-10-19T05:28:18Z
ludothinkpad ok: 25.2 GB free, 4 threads 66% busy, 0 sessions
desktop offline
macmini busy: 33.7 GB free, 14 threads 88% busy, 1 sessions
macbook offline
2026-10-19T05:29:22Z
ludothinkpad busy: 26.4 GB free, 4 threads 99% busy, 0 sessions
desktop offline
macmini ok: 33.7 GB free, 14 threads 81% busy, 1 sessions
macbook offline
2026-10-19T05:30:27Z
ludothinkpad ok: 25.0 GB free, 4 threads 73% busy, 0 sessions
desktop offline
macmini busy: 31.8 GB free, 14 threads 87% busy, 1 sessions
macbook offline
2026-10-19T05:31:31Z
ludothinkpad ok: 24.9 GB free, 4 threads 66% busy, 0 sessions
desktop offline
macmini ok: 29.7 GB free, 14 threads 77% busy, 1 sessions
macbook offline
2026-10-19T05:32:35Z
ludothinkpad busy: 25.0 GB free, 4 threads 94% busy, 0 sessions
desktop offline
macmini busy: 27.8 GB free, 14 threads 87% busy, 1 sessions
macbook offline
2026-10-19T05:33:39Z
ludothinkpad ok: 24.9 GB free, 4 threads 70% busy, 0 sessions
desktop offline
macmini busy: 25.7 GB free, 14 threads 90% busy, 1 sessions
macbook offline
2026-10-19T05:34:43Z
ludothinkpad busy: 24.9 GB free, 4 threads 88% busy, 0 sessions
desktop offline
macmini busy: 32.7 GB free, 14 threads 88% busy, 1 sessions
macbook offline
2026-10-19T05:35:47Z
ludothinkpad ok: 24.9 GB free, 4 threads 72% busy, 0 sessions
desktop offline
macmini busy: 33.3 GB free, 14 threads 86% busy, 1 sessions
macbook offline
2026-10-19T05:36:51Z
ludothinkpad ok: 24.8 GB free, 4 threads 76% busy, 0 sessions
desktop offline
macmini ok: 32.6 GB free, 14 threads 83% busy, 1 sessions
macbook offline
2026-10-19T05:37:55Z
ludothinkpad ok: 24.7 GB free, 4 threads 71% busy, 0 sessions
desktop offline
macmini ok: 31.5 GB free, 14 threads 65% busy, 1 sessions
macbook offline
2026-10-19T05:39:00Z
ludothinkpad busy: 24.7 GB free, 4 threads 92% busy, 0 sessions
desktop offline
macmini ok: 29.7 GB free, 14 threads 69% busy, 1 sessions
macbook offline
2026-10-19T05:40:04Z
ludothinkpad ok: 28.6 GB free, 4 threads 55% busy, 0 sessions
desktop offline
macmini ok: 30.0 GB free, 14 threads 73% busy, 1 sessions
macbook offline
2026-10-19T05:41:08Z
ludothinkpad busy: 25.6 GB free, 4 threads 100% busy, 0 sessions
desktop offline
macmini busy: 28.3 GB free, 14 threads 88% busy, 1 sessions
macbook offline
2026-10-19T05:42:12Z
ludothinkpad busy: 25.5 GB free, 4 threads 97% busy, 0 sessions
desktop offline
macmini ok: 27.6 GB free, 14 threads 80% busy, 1 sessions
macbook offline
2026-10-19T05:43:16Z
ludothinkpad busy: 25.3 GB free, 4 threads 98% busy, 0 sessions
desktop offline
macmini ok: 29.5 GB free, 14 threads 76% busy, 1 sessions
macbook offline
2026-10-19T05:44:20Z
ludothinkpad ok: 25.2 GB free, 4 threads 85% busy, 0 sessions
desktop offline
macmini ok: 28.6 GB free, 14 threads 64% busy, 1 sessions
macbook offline
2026-10-19T05:45:25Z
ludothinkpad ok: 25.2 GB free, 4 threads 74% busy, 0 sessions
desktop offline
macmini ok: 26.6 GB free, 14 threads 78% busy, 1 sessions
macbook offline
2026-10-19T05:46:29Z
ludothinkpad ok: 25.2 GB free, 4 threads 68% busy, 0 sessions
desktop offline
macmini ok: 26.5 GB free, 14 threads 82% busy, 1 sessions
macbook offline
2026-10-19T05:47:33Z
ludothinkpad ok: 24.5 GB free, 4 threads 84% busy, 0 sessions
desktop offline
macmini busy: 26.5 GB free, 14 threads 90% busy, 1 sessions
macbook offline
2026-10-19T05:48:37Z
ludothinkpad busy: 24.4 GB free, 4 threads 86% busy, 0 sessions
desktop offline
macmini ok: 30.6 GB free, 14 threads 61% busy, 1 sessions
macbook offline
2026-10-19T05:49:41Z
ludothinkpad ok: 24.2 GB free, 4 threads 80% busy, 0 sessions
desktop offline
macmini busy: 26.9 GB free, 14 threads 85% busy, 3 sessions
macbook offline
2026-10-19T05:50:45Z
ludothinkpad ok: 24.2 GB free, 4 threads 72% busy, 0 sessions
desktop offline
macmini busy: 25.7 GB free, 14 threads 89% busy, 1 sessions
macbook offline
2026-10-19T05:51:49Z
ludothinkpad ok: 24.1 GB free, 4 threads 17% busy, 0 sessions
desktop offline
macmini busy: 29.1 GB free, 14 threads 87% busy, 1 sessions
macbook offline
2026-10-19T05:52:54Z
ludothinkpad ok: 27.5 GB free, 4 threads 83% busy, 0 sessions
desktop offline
macmini ok: 26.9 GB free, 14 threads 76% busy, 1 sessions
macbook offline
2026-10-19T05:53:58Z
ludothinkpad ok: 25.8 GB free, 4 threads 56% busy, 0 sessions
desktop offline
macmini ok: 27.3 GB free, 14 threads 82% busy, 1 sessions
macbook offline
2026-10-19T05:55:02Z
ludothinkpad ok: 25.8 GB free, 4 threads 35% busy, 0 sessions
desktop offline
macmini busy: 27.2 GB free, 14 threads 95% busy, 1 sessions
macbook offline
2026-10-19T05:56:06Z
ludothinkpad ok: 25.0 GB free, 4 threads 52% busy, 0 sessions
desktop offline
macmini ok: 29.6 GB free, 14 threads 56% busy, 1 sessions
macbook offline
2026-10-19T05:57:10Z
ludothinkpad ok: 25.1 GB free, 4 threads 30% busy, 0 sessions
desktop offline
macmini busy: 26.2 GB free, 14 threads 91% busy, 1 sessions
macbook offline
2026-10-19T05:58:14Z
ludothinkpad ok: 25.1 GB free, 4 threads 36% busy, 0 sessions
desktop offline
macmini busy: 27.8 GB free, 14 threads 94% busy, 1 sessions
macbook offline
2026-10-19T05:59:18Z
ludothinkpad ok: 25.0 GB free, 4 threads 27% busy, 0 sessions
desktop offline
macmini ok: 30.3 GB free, 14 threads 51% busy, 1 sessions
macbook offline
2026-10-19T06:00:22Z
ludothinkpad ok: 25.0 GB free, 4 threads 24% busy, 0 sessions
desktop offline
macmini busy: 26.7 GB free, 14 threads 86% busy, 1 sessions
macbook offline
2026-10-19T06:01:26Z
ludothinkpad ok: 24.4 GB free, 4 threads 38% busy, 0 sessions
desktop offline
macmini busy: 27.1 GB free, 14 threads 89% busy, 1 sessions
macbook offline
2026-10-19T06:02:31Z
ludothinkpad ok: 25.2 GB free, 4 threads 27% busy, 0 sessions
desktop offline
macmini ok: 29.1 GB free, 14 threads 55% busy, 1 sessions
macbook offline
@@ -0,0 +1,437 @@
{
"kind": "horizon-server-benchmark",
"date": "2026-10-19T05:52:44+00:00",
"machine": {
"system": "Linux",
"machine": "x86_64",
"cpus": 4
},
"commit": "38505a07b5334f3ea2c467efb4a00e5557f910f8",
"java": [
"openjdk 25.0.4.1 2026-08-18"
],
"versions": {
"paper_build": 205,
"fabric_loader": "0.19.5",
"lithium": "mc26.3-0.26.2-fabric"
},
"settings": {
"servers": "horizon,vanilla,paper,fabric",
"bots": "25,50",
"behaviour": "mixed",
"join_every": 1.0,
"warmup": 300,
"seconds": 300,
"seed": 12345,
"explore_speed": 0.8,
"gamerule": "",
"heap": "4g",
"out": "/home/ludovic/sh10/out/mixed-equal-25-50.json",
"nice": 19,
"bots_via": "ssh macmini",
"bots_bin": "/Users/ludovicmarie/code/horizon/.claude/worktrees/sh10-bench/target/release/horizon-bots",
"connect": "the ThinkPad's tailnet address",
"properties": {
"level-seed": "12345",
"view-distance": "10",
"simulation-distance": "10",
"online-mode": "false",
"white-list": "false",
"gamemode": "creative",
"force-gamemode": "true",
"spawn-protection": "0",
"max-players": "200",
"difficulty": "easy",
"sync-chunk-writes": "true"
}
},
"runs": [
{
"server": "horizon",
"bots": 25,
"behaviour": "mixed",
"start_seconds": 8.51,
"mspt": {
"average": 30.92,
"p50": 27.88,
"p95": 48.25,
"p99": 72.0
},
"mspt_worst_p99": 121.1,
"tps": 20.0,
"rss_mb": {
"mean": 1012.01,
"max": 1121.3
},
"cpu_percent": 98.57,
"chunks_per_second": 155.81,
"unique_per_second": 5.78,
"forgotten_per_second": 155.62,
"waited_ticks_per_second": 0.0,
"chunks_total": 106268,
"chunks_per_bot": {
"walk": 6506.33,
"explore": 1261.6,
"idle": 473.0
},
"unique_chunks_per_bot": {
"walk": 702.13,
"explore": 1184.4,
"idle": 473.0
},
"resent": 0,
"pulled_back": 3008,
"moved_too_quickly": 0,
"moved_wrongly": 0,
"bots_joined": 25,
"bots_failed": [],
"tick_queries": 58
},
{
"server": "vanilla",
"bots": 25,
"behaviour": "mixed",
"start_seconds": 15.47,
"mspt": {
"average": 37.35,
"p50": 35.64,
"p95": 48.2,
"p99": 82.66
},
"mspt_worst_p99": 579.8,
"tps": 20.0,
"rss_mb": {
"mean": 3935.5,
"max": 4157.4
},
"cpu_percent": 191.44,
"chunks_per_second": 158.52,
"unique_per_second": 5.71,
"forgotten_per_second": 158.39,
"waited_ticks_per_second": 0.0,
"chunks_total": 103148,
"chunks_per_bot": {
"walk": 6306.87,
"explore": 1236.0,
"idle": 473.0
},
"unique_chunks_per_bot": {
"walk": 704.2,
"explore": 1160.2,
"idle": 473.0
},
"resent": 0,
"pulled_back": 3762,
"moved_too_quickly": 11,
"moved_wrongly": 0,
"bots_joined": 25,
"bots_failed": [],
"tick_queries": 59
},
{
"server": "paper",
"bots": 25,
"behaviour": "mixed",
"start_seconds": 18.95,
"mspt": {
"average": 28.3,
"p50": 23.7,
"p95": 61.68,
"p99": 90.4
},
"mspt_worst_p99": 124.3,
"tps": 20.0,
"rss_mb": {
"mean": 4160.34,
"max": 4246.4
},
"cpu_percent": 124.24,
"chunks_per_second": 156.35,
"unique_per_second": 5.72,
"forgotten_per_second": 156.25,
"waited_ticks_per_second": 0.0,
"chunks_total": 102715,
"chunks_per_bot": {
"walk": 6280.4,
"explore": 1228.8,
"idle": 473.0
},
"unique_chunks_per_bot": {
"walk": 703.07,
"explore": 1155.6,
"idle": 473.0
},
"resent": 0,
"pulled_back": 4337,
"moved_too_quickly": 9,
"moved_wrongly": 0,
"bots_joined": 25,
"bots_failed": [],
"tick_queries": 59
},
{
"server": "fabric",
"bots": 25,
"behaviour": "mixed",
"start_seconds": 17.45,
"mspt": {
"average": 31.44,
"p50": 28.33,
"p95": 52.45,
"p99": 93.35
},
"mspt_worst_p99": 409.3,
"tps": 20.0,
"rss_mb": {
"mean": 3884.61,
"max": 4186.4
},
"cpu_percent": 174.04,
"chunks_per_second": 158.89,
"unique_per_second": 5.75,
"forgotten_per_second": 158.54,
"waited_ticks_per_second": 0.0,
"chunks_total": 103913,
"chunks_per_bot": {
"walk": 6356.8,
"explore": 1239.2,
"idle": 473.0
},
"unique_chunks_per_bot": {
"walk": 704.27,
"explore": 1163.0,
"idle": 473.0
},
"resent": 0,
"pulled_back": 4194,
"moved_too_quickly": 11,
"moved_wrongly": 0,
"bots_joined": 25,
"bots_failed": [],
"tick_queries": 59
},
{
"server": "horizon",
"bots": 50,
"behaviour": "mixed",
"start_seconds": 8.31,
"mspt": {
"average": 47.83,
"p50": 46.02,
"p95": 62.42,
"p99": 73.21
},
"mspt_worst_p99": 133.0,
"tps": 20.0,
"rss_mb": {
"mean": 1112.46,
"max": 1296.4
},
"cpu_percent": 143.22,
"chunks_per_second": 302.68,
"unique_per_second": 11.02,
"forgotten_per_second": 303.15,
"waited_ticks_per_second": 0.0,
"chunks_total": 211296,
"chunks_per_bot": {
"walk": 6468.47,
"explore": 1251.2,
"idle": 473.0
},
"unique_chunks_per_bot": {
"walk": 701.67,
"explore": 1180.5,
"idle": 473.0
},
"resent": 0,
"pulled_back": 8562,
"moved_too_quickly": 0,
"moved_wrongly": 0,
"bots_joined": 50,
"bots_failed": [],
"tick_queries": 58
},
{
"server": "vanilla",
"bots": 50,
"behaviour": "mixed",
"start_seconds": 15.45,
"error": "[Errno 32] Broken pipe"
},
{
"server": "paper",
"bots": 50,
"behaviour": "mixed",
"start_seconds": 19.35,
"mspt": {
"average": 46.86,
"p50": 45.11,
"p95": 61.16,
"p99": 109.72
},
"mspt_worst_p99": 213.4,
"tps": 20.0,
"rss_mb": {
"mean": 4353.02,
"max": 4516.1
},
"cpu_percent": 210.21,
"chunks_per_second": 310.51,
"unique_per_second": 11.16,
"forgotten_per_second": 310.64,
"waited_ticks_per_second": 0.0,
"chunks_total": 209303,
"chunks_per_bot": {
"walk": 6411.0,
"explore": 1224.3,
"idle": 473.0
},
"unique_chunks_per_bot": {
"walk": 703.47,
"explore": 1155.8,
"idle": 473.0
},
"resent": 0,
"pulled_back": 7344,
"moved_too_quickly": 35,
"moved_wrongly": 0,
"bots_joined": 50,
"bots_failed": [],
"tick_queries": 58
},
{
"server": "fabric",
"bots": 50,
"behaviour": "mixed",
"start_seconds": 17.45,
"mspt": {
"average": 58.3,
"p50": 54.73,
"p95": 83.06,
"p99": 174.37
},
"mspt_worst_p99": 887.6,
"tps": 17.15,
"rss_mb": {
"mean": 4521.47,
"max": 4744.2
},
"cpu_percent": 236.04,
"chunks_per_second": 311.52,
"unique_per_second": 11.28,
"forgotten_per_second": 311.74,
"waited_ticks_per_second": 0.0,
"chunks_total": 206965,
"chunks_per_bot": {
"walk": 6331.7,
"explore": 1228.4,
"idle": 473.0
},
"unique_chunks_per_bot": {
"walk": 703.3,
"explore": 1160.3,
"idle": 473.0
},
"resent": 0,
"pulled_back": 6350,
"moved_too_quickly": 46,
"moved_wrongly": 0,
"bots_joined": 50,
"bots_failed": [],
"tick_queries": 58
}
],
"work": [
{
"behaviour": "mixed",
"bots": 25,
"servers": {
"horizon": {
"chunks_per_second": {
"value": 155.81,
"share": 0.981
},
"unique_per_second": {
"value": 5.78,
"share": 1.0
},
"waited_ticks_per_second": 0.0
},
"vanilla": {
"chunks_per_second": {
"value": 158.52,
"share": 0.998
},
"unique_per_second": {
"value": 5.71,
"share": 0.988
},
"waited_ticks_per_second": 0.0
},
"paper": {
"chunks_per_second": {
"value": 156.35,
"share": 0.984
},
"unique_per_second": {
"value": 5.72,
"share": 0.99
},
"waited_ticks_per_second": 0.0
},
"fabric": {
"chunks_per_second": {
"value": 158.89,
"share": 1.0
},
"unique_per_second": {
"value": 5.75,
"share": 0.995
},
"waited_ticks_per_second": 0.0
}
},
"equal": true
},
{
"behaviour": "mixed",
"bots": 50,
"servers": {
"horizon": {
"chunks_per_second": {
"value": 302.68,
"share": 0.972
},
"unique_per_second": {
"value": 11.02,
"share": 0.977
},
"waited_ticks_per_second": 0.0
},
"paper": {
"chunks_per_second": {
"value": 310.51,
"share": 0.997
},
"unique_per_second": {
"value": 11.16,
"share": 0.989
},
"waited_ticks_per_second": 0.0
},
"fabric": {
"chunks_per_second": {
"value": 311.52,
"share": 1.0
},
"unique_per_second": {
"value": 11.28,
"share": 1.0
},
"waited_ticks_per_second": 0.0
}
},
"equal": true
}
]
}
@@ -0,0 +1,90 @@
{
"kind": "horizon-server-benchmark",
"date": "2026-10-19T06:03:34+00:00",
"machine": {
"system": "Linux",
"machine": "x86_64",
"cpus": 4
},
"commit": "38505a07b5334f3ea2c467efb4a00e5557f910f8",
"java": [
"openjdk 25.0.4.1 2026-08-18"
],
"versions": {
"paper_build": 205
},
"settings": {
"servers": "paper",
"bots": "10",
"behaviour": "mixed",
"join_every": 1.0,
"warmup": 300,
"seconds": 300,
"seed": 12345,
"explore_speed": 0.8,
"gamerule": "",
"heap": "4g",
"out": "/home/ludovic/sh10/out/mixed-equal-paper-10.json",
"nice": 19,
"bots_via": "ssh macmini",
"bots_bin": "/Users/ludovicmarie/code/horizon/.claude/worktrees/sh10-bench/target/release/horizon-bots",
"connect": "the ThinkPad's tailnet address",
"properties": {
"level-seed": "12345",
"view-distance": "10",
"simulation-distance": "10",
"online-mode": "false",
"white-list": "false",
"gamemode": "creative",
"force-gamemode": "true",
"spawn-protection": "0",
"max-players": "200",
"difficulty": "easy",
"sync-chunk-writes": "true"
}
},
"runs": [
{
"server": "paper",
"bots": 10,
"behaviour": "mixed",
"start_seconds": 20.48,
"mspt": {
"average": 20.64,
"p50": 18.1,
"p95": 35.94,
"p99": 56.29
},
"mspt_worst_p99": 166.9,
"tps": 20.0,
"rss_mb": {
"mean": 4069.94,
"max": 4080.2
},
"cpu_percent": 73.56,
"chunks_per_second": 60.76,
"unique_per_second": 1.85,
"forgotten_per_second": 60.96,
"waited_ticks_per_second": 0.0,
"chunks_total": 40348,
"chunks_per_bot": {
"walk": 6176.67,
"explore": 1171.0,
"idle": 473.0
},
"unique_chunks_per_bot": {
"walk": 701.0,
"explore": 1091.5,
"idle": 473.0
},
"resent": 0,
"pulled_back": 3974,
"moved_too_quickly": 7,
"moved_wrongly": 0,
"bots_joined": 10,
"bots_failed": [],
"tick_queries": 59
}
],
"work": []
}
@@ -0,0 +1,16 @@
{"server": "horizon", "bots": 1, "behaviour": "mixed", "start_seconds": 8.31, "mspt": {"average": 13.7, "p50": 14.18, "p95": 19.06, "p99": 27.38}, "mspt_worst_p99": 43.4, "tps": 20.0, "rss_mb": {"mean": 715.63, "max": 719.6}, "cpu_percent": 46.22, "chunks_per_second": 9.94, "unique_per_second": 0.01, "forgotten_per_second": 9.89, "waited_ticks_per_second": 0.0, "chunks_total": 6280, "chunks_per_bot": {"walk": 6280.0}, "unique_chunks_per_bot": {"walk": 700.0}, "resent": 0, "pulled_back": 0, "moved_too_quickly": 0, "moved_wrongly": 0, "bots_joined": 1, "bots_failed": [], "tick_queries": 58}
{"server": "vanilla", "bots": 1, "behaviour": "mixed", "start_seconds": 14.85, "mspt": {"average": 17.31, "p50": 18.3, "p95": 24.68, "p99": 36.43}, "mspt_worst_p99": 71.0, "tps": 20.0, "rss_mb": {"mean": 3442.51, "max": 3444.5}, "cpu_percent": 55.03, "chunks_per_second": 9.99, "unique_per_second": 0.0, "forgotten_per_second": 9.95, "waited_ticks_per_second": 0.0, "chunks_total": 6210, "chunks_per_bot": {"walk": 6210.0}, "unique_chunks_per_bot": {"walk": 700.0}, "resent": 0, "pulled_back": 0, "moved_too_quickly": 0, "moved_wrongly": 0, "bots_joined": 1, "bots_failed": [], "tick_queries": 58}
{"server": "paper", "bots": 1, "behaviour": "mixed", "start_seconds": 22.27, "mspt": {"average": 10.31, "p50": 10.4, "p95": 13.9, "p99": 22.5}, "mspt_worst_p99": 81.2, "tps": 20.0, "rss_mb": {"mean": 4239.22, "max": 4245.1}, "cpu_percent": 31.68, "chunks_per_second": 9.81, "unique_per_second": 0.02, "forgotten_per_second": 9.77, "waited_ticks_per_second": 0.0, "chunks_total": 6049, "chunks_per_bot": {"walk": 6049.0}, "unique_chunks_per_bot": {"walk": 698.0}, "resent": 0, "pulled_back": 0, "moved_too_quickly": 0, "moved_wrongly": 0, "bots_joined": 1, "bots_failed": [], "tick_queries": 58}
{"server": "fabric", "bots": 1, "behaviour": "mixed", "start_seconds": 17.55, "mspt": {"average": 12.46, "p50": 12.62, "p95": 17.69, "p99": 32.74}, "mspt_worst_p99": 70.1, "tps": 20.0, "rss_mb": {"mean": 3428.55, "max": 3430.2}, "cpu_percent": 45.86, "chunks_per_second": 9.98, "unique_per_second": 0.0, "forgotten_per_second": 9.93, "waited_ticks_per_second": 0.0, "chunks_total": 6188, "chunks_per_bot": {"walk": 6188.0}, "unique_chunks_per_bot": {"walk": 700.0}, "resent": 0, "pulled_back": 0, "moved_too_quickly": 0, "moved_wrongly": 0, "bots_joined": 1, "bots_failed": [], "tick_queries": 58}
{"server": "horizon", "bots": 10, "behaviour": "mixed", "start_seconds": 8.21, "mspt": {"average": 22.93, "p50": 20.03, "p95": 39.83, "p99": 53.33}, "mspt_worst_p99": 89.5, "tps": 20.0, "rss_mb": {"mean": 922.28, "max": 973.7}, "cpu_percent": 66.6, "chunks_per_second": 61.21, "unique_per_second": 1.78, "forgotten_per_second": 61.34, "waited_ticks_per_second": 0.0, "chunks_total": 42095, "chunks_per_bot": {"walk": 6460.0, "explore": 1194.5, "idle": 473.0}, "unique_chunks_per_bot": {"walk": 702.17, "explore": 1112.5, "idle": 473.0}, "resent": 0, "pulled_back": 3702, "moved_too_quickly": 0, "moved_wrongly": 0, "bots_joined": 10, "bots_failed": [], "tick_queries": 58}
{"server": "vanilla", "bots": 10, "behaviour": "mixed", "start_seconds": 14.65, "mspt": {"average": 22.94, "p50": 20.61, "p95": 36.92, "p99": 56.48}, "mspt_worst_p99": 196.6, "tps": 20.0, "rss_mb": {"mean": 3706.57, "max": 3761.6}, "cpu_percent": 100.83, "chunks_per_second": 61.63, "unique_per_second": 1.84, "forgotten_per_second": 61.68, "waited_ticks_per_second": 0.0, "chunks_total": 40938, "chunks_per_bot": {"walk": 6272.17, "explore": 1179.5, "idle": 473.0}, "unique_chunks_per_bot": {"walk": 702.0, "explore": 1097.0, "idle": 473.0}, "resent": 0, "pulled_back": 3370, "moved_too_quickly": 7, "moved_wrongly": 0, "bots_joined": 10, "bots_failed": [], "tick_queries": 59}
{"server": "paper", "bots": 10, "behaviour": "mixed", "start_seconds": 20.48, "mspt": {"average": 20.64, "p50": 18.1, "p95": 35.94, "p99": 56.29}, "mspt_worst_p99": 166.9, "tps": 20.0, "rss_mb": {"mean": 4069.94, "max": 4080.2}, "cpu_percent": 73.56, "chunks_per_second": 60.76, "unique_per_second": 1.85, "forgotten_per_second": 60.96, "waited_ticks_per_second": 0.0, "chunks_total": 40348, "chunks_per_bot": {"walk": 6176.67, "explore": 1171.0, "idle": 473.0}, "unique_chunks_per_bot": {"walk": 701.0, "explore": 1091.5, "idle": 473.0}, "resent": 0, "pulled_back": 3974, "moved_too_quickly": 7, "moved_wrongly": 0, "bots_joined": 10, "bots_failed": [], "tick_queries": 59}
{"server": "fabric", "bots": 10, "behaviour": "mixed", "start_seconds": 16.64, "mspt": {"average": 20.18, "p50": 17.63, "p95": 37.04, "p99": 56.08}, "mspt_worst_p99": 217.9, "tps": 20.0, "rss_mb": {"mean": 3732.43, "max": 3776.5}, "cpu_percent": 94.48, "chunks_per_second": 61.65, "unique_per_second": 1.84, "forgotten_per_second": 61.76, "waited_ticks_per_second": 0.0, "chunks_total": 41055, "chunks_per_bot": {"walk": 6291.17, "explore": 1181.0, "idle": 473.0}, "unique_chunks_per_bot": {"walk": 701.83, "explore": 1098.0, "idle": 473.0}, "resent": 0, "pulled_back": 4002, "moved_too_quickly": 3, "moved_wrongly": 0, "bots_joined": 10, "bots_failed": [], "tick_queries": 59}
{"server": "horizon", "bots": 25, "behaviour": "mixed", "start_seconds": 8.51, "mspt": {"average": 30.92, "p50": 27.88, "p95": 48.25, "p99": 72.0}, "mspt_worst_p99": 121.1, "tps": 20.0, "rss_mb": {"mean": 1012.01, "max": 1121.3}, "cpu_percent": 98.57, "chunks_per_second": 155.81, "unique_per_second": 5.78, "forgotten_per_second": 155.62, "waited_ticks_per_second": 0.0, "chunks_total": 106268, "chunks_per_bot": {"walk": 6506.33, "explore": 1261.6, "idle": 473.0}, "unique_chunks_per_bot": {"walk": 702.13, "explore": 1184.4, "idle": 473.0}, "resent": 0, "pulled_back": 3008, "moved_too_quickly": 0, "moved_wrongly": 0, "bots_joined": 25, "bots_failed": [], "tick_queries": 58}
{"server": "vanilla", "bots": 25, "behaviour": "mixed", "start_seconds": 15.47, "mspt": {"average": 37.35, "p50": 35.64, "p95": 48.2, "p99": 82.66}, "mspt_worst_p99": 579.8, "tps": 20.0, "rss_mb": {"mean": 3935.5, "max": 4157.4}, "cpu_percent": 191.44, "chunks_per_second": 158.52, "unique_per_second": 5.71, "forgotten_per_second": 158.39, "waited_ticks_per_second": 0.0, "chunks_total": 103148, "chunks_per_bot": {"walk": 6306.87, "explore": 1236.0, "idle": 473.0}, "unique_chunks_per_bot": {"walk": 704.2, "explore": 1160.2, "idle": 473.0}, "resent": 0, "pulled_back": 3762, "moved_too_quickly": 11, "moved_wrongly": 0, "bots_joined": 25, "bots_failed": [], "tick_queries": 59}
{"server": "paper", "bots": 25, "behaviour": "mixed", "start_seconds": 18.95, "mspt": {"average": 28.3, "p50": 23.7, "p95": 61.68, "p99": 90.4}, "mspt_worst_p99": 124.3, "tps": 20.0, "rss_mb": {"mean": 4160.34, "max": 4246.4}, "cpu_percent": 124.24, "chunks_per_second": 156.35, "unique_per_second": 5.72, "forgotten_per_second": 156.25, "waited_ticks_per_second": 0.0, "chunks_total": 102715, "chunks_per_bot": {"walk": 6280.4, "explore": 1228.8, "idle": 473.0}, "unique_chunks_per_bot": {"walk": 703.07, "explore": 1155.6, "idle": 473.0}, "resent": 0, "pulled_back": 4337, "moved_too_quickly": 9, "moved_wrongly": 0, "bots_joined": 25, "bots_failed": [], "tick_queries": 59}
{"server": "fabric", "bots": 25, "behaviour": "mixed", "start_seconds": 17.45, "mspt": {"average": 31.44, "p50": 28.33, "p95": 52.45, "p99": 93.35}, "mspt_worst_p99": 409.3, "tps": 20.0, "rss_mb": {"mean": 3884.61, "max": 4186.4}, "cpu_percent": 174.04, "chunks_per_second": 158.89, "unique_per_second": 5.75, "forgotten_per_second": 158.54, "waited_ticks_per_second": 0.0, "chunks_total": 103913, "chunks_per_bot": {"walk": 6356.8, "explore": 1239.2, "idle": 473.0}, "unique_chunks_per_bot": {"walk": 704.27, "explore": 1163.0, "idle": 473.0}, "resent": 0, "pulled_back": 4194, "moved_too_quickly": 11, "moved_wrongly": 0, "bots_joined": 25, "bots_failed": [], "tick_queries": 59}
{"server": "horizon", "bots": 50, "behaviour": "mixed", "start_seconds": 8.31, "mspt": {"average": 47.83, "p50": 46.02, "p95": 62.42, "p99": 73.21}, "mspt_worst_p99": 133.0, "tps": 20.0, "rss_mb": {"mean": 1112.46, "max": 1296.4}, "cpu_percent": 143.22, "chunks_per_second": 302.68, "unique_per_second": 11.02, "forgotten_per_second": 303.15, "waited_ticks_per_second": 0.0, "chunks_total": 211296, "chunks_per_bot": {"walk": 6468.47, "explore": 1251.2, "idle": 473.0}, "unique_chunks_per_bot": {"walk": 701.67, "explore": 1180.5, "idle": 473.0}, "resent": 0, "pulled_back": 8562, "moved_too_quickly": 0, "moved_wrongly": 0, "bots_joined": 50, "bots_failed": [], "tick_queries": 58}
{"server": "vanilla", "bots": 50, "behaviour": "mixed", "start_seconds": 15.45, "error": "[Errno 32] Broken pipe"}
{"server": "paper", "bots": 50, "behaviour": "mixed", "start_seconds": 19.35, "mspt": {"average": 46.86, "p50": 45.11, "p95": 61.16, "p99": 109.72}, "mspt_worst_p99": 213.4, "tps": 20.0, "rss_mb": {"mean": 4353.02, "max": 4516.1}, "cpu_percent": 210.21, "chunks_per_second": 310.51, "unique_per_second": 11.16, "forgotten_per_second": 310.64, "waited_ticks_per_second": 0.0, "chunks_total": 209303, "chunks_per_bot": {"walk": 6411.0, "explore": 1224.3, "idle": 473.0}, "unique_chunks_per_bot": {"walk": 703.47, "explore": 1155.8, "idle": 473.0}, "resent": 0, "pulled_back": 7344, "moved_too_quickly": 35, "moved_wrongly": 0, "bots_joined": 50, "bots_failed": [], "tick_queries": 58}
{"server": "fabric", "bots": 50, "behaviour": "mixed", "start_seconds": 17.45, "mspt": {"average": 58.3, "p50": 54.73, "p95": 83.06, "p99": 174.37}, "mspt_worst_p99": 887.6, "tps": 17.15, "rss_mb": {"mean": 4521.47, "max": 4744.2}, "cpu_percent": 236.04, "chunks_per_second": 311.52, "unique_per_second": 11.28, "forgotten_per_second": 311.74, "waited_ticks_per_second": 0.0, "chunks_total": 206965, "chunks_per_bot": {"walk": 6331.7, "explore": 1228.4, "idle": 473.0}, "unique_chunks_per_bot": {"walk": 703.3, "explore": 1160.3, "idle": 473.0}, "resent": 0, "pulled_back": 6350, "moved_too_quickly": 46, "moved_wrongly": 0, "bots_joined": 50, "bots_failed": [], "tick_queries": 58}
+36 -1
View File
@@ -6,6 +6,7 @@ use horizon_world::{nbt::Tag, registry::BlockRegistry};
pub struct FlatTerrain {
layers: Vec<u32>,
biome: u16,
surface: Option<crate::lod::Surface>,
}
impl FlatTerrain {
/// The overworld generator of `world_gen_settings` (`data.dimensions`), if it
@@ -34,9 +35,40 @@ impl FlatTerrain {
layers.extend(std::iter::repeat_n(state, height as usize));
}
}
let biome = biomes.id(&biome);
let mut water = None;
let mut surface = None;
for (i, &id) in layers
.iter()
.take(crate::chunk::HEIGHT as usize)
.enumerate()
.rev()
{
let state = horizon_world::registry::BlockStateId(id);
let name = registry.state(state)?.name.as_str();
if matches!(
name,
"minecraft:air" | "minecraft:cave_air" | "minecraft:void_air"
) {
continue;
}
let height = crate::chunk::MIN_Y + i as i32 + 1;
if matches!(name, "minecraft:water" | "minecraft:lava") {
water.get_or_insert((height, state));
} else {
surface = Some(crate::lod::Surface {
height,
state,
water,
biome,
});
break;
}
}
Some(Self {
layers,
biome: biomes.id(&biome),
biome,
surface,
})
}
/// FlatLevelSource.getSpawnHeight: the top of the layers.
@@ -154,6 +186,9 @@ pub fn settings_from_json(
impl ChunkGenerator for FlatTerrain {
type Worker = ();
fn worker(&self) {}
fn lod_surface(&self, _: &mut (), _: i32, _: i32) -> Option<crate::lod::Surface> {
self.surface
}
fn proto(&self, _: &mut (), cx: i32, cz: i32) -> crate::streaming::Proto {
let mut chunk = Chunk::empty(cx, cz);
for s in &mut chunk.sections {
+47
View File
@@ -181,6 +181,8 @@ pub struct FogInputs<'a> {
pub water_fog_color: [f32; 3],
pub biome: u16,
pub render_distance_chunks: i32,
/// Extend ordinary Overworld atmosphere for terrain beyond vanilla's range.
pub distant_terrain: bool,
/// The Cloud Distance option, in chunks.
pub cloud_range_chunks: i32,
pub camera_y: f64,
@@ -337,6 +339,18 @@ pub fn setup_fog(tables: &FogTables, state: &FogState, input: &FogInputs) -> Fog
let span = (rd / 10.0).clamp(4.0, 64.0);
fog.render_distance_start = rd - span;
fog.render_distance_end = rd;
if input.distant_terrain
&& input.skybox
&& input.fog_type == FogType::Atmospheric
&& input.blindness.is_none()
&& input.darkness.is_none()
&& !input.boss_world_fog
&& sky.fog_end >= 1024.0
{
let scale = (rd / 1024.0).max(1.0);
fog.environmental_start *= scale;
fog.environmental_end *= scale;
}
fog
}
/// `AtmosphericFogEnvironment.updateRainFogState`.
@@ -369,6 +383,7 @@ mod tests {
water_fog_color: [0.0, 0.0, 0.2],
biome: 0,
render_distance_chunks: 12,
distant_terrain: false,
cloud_range_chunks: 128,
camera_y: 70.0,
min_y: -64,
@@ -447,6 +462,38 @@ mod tests {
assert_eq!(fog.sky_end, 192.0);
}
#[test]
fn distant_atmosphere_extends_without_extending_environmental_overrides() {
let sky = Sky {
fog_end: 1024.,
..Sky::default()
};
let tables = FogTables {
water_fog_end: vec![1.],
};
let mut input = inputs(&sky);
input.render_distance_chunks = 1024;
input.distant_terrain = true;
let fog = setup_fog(&tables, &FogState::default(), &input);
assert_eq!(fog.environmental_end, 16384.);
input.fog_type = FogType::Water;
assert_eq!(
setup_fog(&tables, &FogState::default(), &input).environmental_end,
24.
);
input.fog_type = FogType::Atmospheric;
input.darkness = Some(1.);
assert_eq!(
setup_fog(&tables, &FogState::default(), &input).environmental_end,
15.
);
input.darkness = None;
input.boss_world_fog = true;
assert_eq!(
setup_fog(&tables, &FogState::default(), &input).environmental_end,
96.
);
}
#[test]
fn cloud_fog_ends_at_the_cloud_distance() {
let sky = Sky {
fog_end: 1024.0,
+133 -10
View File
@@ -1002,6 +1002,9 @@ struct Game<'a> {
screen_fx: horizon_client::screen_effects::ScreenEffects,
world: World,
streaming: Streaming,
lod: Option<horizon_client::lod::Lod>,
lod_source_attempted: bool,
lod_observe: horizon_client::fast_hash::FxHashSet<(i32, i32)>,
options: Options,
player: Option<PlayerMotion>,
spawn: Option<Vec3>,
@@ -6881,6 +6884,8 @@ impl Game<'_> {
self.using = None;
self.screen = Screen::Playing;
self.title = None;
self.clear_lod();
self.pending_uploads.clear();
if let Some(r) = self.renderer.as_mut() {
r.clear_sections();
}
@@ -7035,6 +7040,8 @@ impl Game<'_> {
self.pickups.clear();
self.orbs.clear();
self.mining = None;
self.clear_lod();
self.pending_uploads.clear();
if let Some(r) = self.renderer.as_mut() {
r.clear_sections();
}
@@ -7213,6 +7220,8 @@ impl Game<'_> {
self.sleep_timer = 0;
self.clear_entities();
self.screen = Screen::Playing;
self.clear_lod();
self.pending_uploads.clear();
if let Some(r) = self.renderer.as_mut() {
r.clear_sections();
}
@@ -9159,9 +9168,10 @@ impl Game<'_> {
} else {
self.bob_matrix(alpha as f32)
};
let distance = (self.streaming.radius * 16) as f32;
let terrain_distance = self.terrain_distance();
let distance = (terrain_distance * 16) as f32;
let depth_far =
horizon_client::camera::depth_far(self.streaming.radius, self.settings.cloud_range);
horizon_client::camera::depth_far(terrain_distance, self.settings.cloud_range);
self.view_frustum = self
.renderer
.as_ref()
@@ -9238,6 +9248,7 @@ impl Game<'_> {
bob,
sky,
distance,
detail_distance: (self.streaming.radius * 16) as f32,
depth_far,
underwater,
title: title && !live,
@@ -9249,7 +9260,13 @@ impl Game<'_> {
fog: Some({
let mut fog = self.frame_fog(camera, &sky, biome);
// Horizon's fog setting: no distance fog (fluids keep theirs).
if !self.settings.horizon.fog && !underwater && fog.environmental_end >= distance {
if !self.settings.horizon.fog
&& !underwater
&& self.darkness_fog().is_none()
&& !self.dragon_bar_shown()
&& self.potions.effects.get("blindness").is_none()
&& fog.environmental_end >= (self.streaming.radius * 16) as f32
{
(fog.render_distance_start, fog.render_distance_end) = (1e9, 2e9);
(fog.environmental_start, fog.environmental_end) = (1e9, 2e9);
}
@@ -9417,7 +9434,7 @@ impl Game<'_> {
);
// Camera.depthFar, as the renderer's far plane.
let depth_far =
horizon_client::camera::depth_far(self.streaming.radius, self.settings.cloud_range);
horizon_client::camera::depth_far(self.terrain_distance(), self.settings.cloud_range);
if !self
.border_renderer
.prepare(&self.border_state, camera, distance, depth_far)
@@ -9624,10 +9641,62 @@ impl Game<'_> {
}
Ok(())
}
/// Only enabled LOD worlds extend the renderer's range, never simulation.
fn terrain_distance(&self) -> i32 {
if self.lod.is_some() {
self.streaming
.radius
.max(self.settings.horizon.lod_distance.min(1024) as i32)
} else {
self.streaming.radius
}
}
fn clear_lod(&mut self) {
self.lod = None;
self.lod_source_attempted = false;
self.lod_observe.clear();
if let Some(renderer) = &mut self.renderer {
renderer.clear_lods();
}
}
fn prepare_lod(&mut self) {
let enabled = self.settings.horizon.lod_distance > 0
&& self.renderer.is_some()
&& self.player.is_some()
&& self.title.is_none()
&& (self.remote.is_none() || self.integrated.is_some());
if !enabled {
if self.lod.is_some() || self.lod_source_attempted {
self.clear_lod();
}
return;
}
if self.lod_source_attempted {
return;
}
self.lod_source_attempted = true;
let source = match &self.integrated {
Some(server) => server.call(|game| game.streaming.lod_source()),
None => Some(self.streaming.lod_source()),
};
if let Some(source) = source {
self.lod = Some(horizon_client::lod::Lod::new(
source,
self.pack.context.clone(),
));
self.lod_observe.extend(self.world.chunks.keys().copied());
}
}
fn update_world(&mut self) {
if let Err(e) = self.poll_remote() {
log::warn!("Horizon: {e}");
}
self.prepare_lod();
if self.lod.is_some() {
for &((x, _, z), _, _) in &self.world.light_edits {
self.lod_observe.insert((x >> 4, z >> 4));
}
}
if let Some(p) = &self.player {
self.streaming.center = (
(p.position.x.floor() as i32) >> 4,
@@ -9636,6 +9705,23 @@ impl Game<'_> {
}
let (uploads, unloaded) =
horizon_client::spans::time("streaming", || self.streaming.update(&mut self.world));
if let Some(lod) = &mut self.lod {
self.lod_observe
.extend(uploads.iter().map(|(key, _)| (key.0, key.2)));
for chunk in &unloaded {
horizon_client::spans::time("lod_observe", || lod.observe(chunk, self.res.facts()));
self.lod_observe.remove(&(chunk.x, chunk.z));
}
let observe: Vec<_> = self.lod_observe.iter().copied().take(2).collect();
for key in observe {
self.lod_observe.remove(&key);
if let Some(chunk) = self.world.chunks.get(&key) {
horizon_client::spans::time("lod_observe", || {
lod.observe(chunk, self.res.facts())
});
}
}
}
let dirty: Vec<_> = unloaded
.iter()
.filter(|c| self.world.modified.remove(&(c.x, c.z)))
@@ -9673,14 +9759,18 @@ impl Game<'_> {
} else {
// Horizon's chunk-update and upload budgets: the nearest
// sections go first, the rest wait for later frames; a newer
// mesh of a waiting section replaces it.
// mesh of a waiting section replaces it, and sections of
// chunks no longer loaded are dropped.
let pending = &mut self.pending_uploads;
let fresh: horizon_client::fast_hash::FxHashSet<(i32, i32, i32)> =
uploads.iter().map(|(k, _)| *k).collect();
let gone: horizon_client::fast_hash::FxHashSet<(i32, i32)> =
unloaded.iter().map(|c| (c.x, c.z)).collect();
pending.retain(|(k, _)| !fresh.contains(k) && !gone.contains(&(k.0, k.2)));
pending.extend(uploads);
let chunks = &self.world.chunks;
pending.retain(|(k, _)| !fresh.contains(k) && chunks.contains_key(&(k.0, k.2)));
pending.extend(
uploads
.into_iter()
.filter(|(k, _)| chunks.contains_key(&(k.0, k.2))),
);
let centre = self.streaming.center;
let far = |k: &(i32, i32, i32)| (k.0 - centre.0).pow(2) + (k.2 - centre.1).pow(2);
pending.sort_unstable_by_key(|(k, _)| std::cmp::Reverse(far(k)));
@@ -9694,7 +9784,9 @@ impl Game<'_> {
};
let (mut count, mut bytes) = (0, 0u64);
while let Some((_, mesh)) = pending.last() {
let size = ((mesh.solid.len() + mesh.translucent.len()) * 32) as u64;
let size = ((mesh.solid.len() + mesh.translucent.len())
* std::mem::size_of::<horizon_client::mesher::TerrainVertex>())
as u64;
if (limit > 0 && count >= limit)
|| (budget > 0 && bytes > 0 && bytes + size > budget)
{
@@ -9711,6 +9803,33 @@ impl Game<'_> {
renderer.remove((c.x, sy, c.z));
}
}
if let (Some(lod), Some(player)) = (&mut self.lod, &self.player) {
let covered = renderer
.lod_detail_columns()
.filter(|&(x, z)| {
horizon_client::streaming::in_view_distance(
self.streaming.center,
self.streaming.radius,
x,
z,
)
})
.collect();
for (key, mesh) in horizon_client::spans::time("lod_update", || {
lod.update(
[player.position.x, player.position.y, player.position.z],
self.yaw,
self.streaming.radius,
self.settings.horizon.lod_distance,
covered,
)
}) {
horizon_client::spans::time("lod_upload", || renderer.upload_lod(key, &mesh));
}
for key in lod.removed() {
renderer.remove_lod(key);
}
}
}
}
}
@@ -10608,6 +10727,9 @@ impl<'a> Game<'a> {
screen_fx: Default::default(),
world,
streaming,
lod: None,
lod_source_attempted: false,
lod_observe: Default::default(),
options,
player: None,
spawn: None,
@@ -11431,6 +11553,7 @@ fn loading_frame(hud: &HudBatch) -> Frame<'_> {
bob: glam::Mat4::IDENTITY,
sky: horizon_client::environment::Sky::default(),
distance: 12.0,
detail_distance: 12.0,
depth_far: 1000.0,
underwater: false,
cave_culling: false,
+25
View File
@@ -20,6 +20,7 @@ const HELP: &[&str] = &[
"hotbar 1-9 select a hotbar slot",
"give ITEM [COUNT] add items to the inventory",
"render-distance N chunks, 2..64",
"lod-distance N far surface chunks, 0 disables, up to1024",
"time TICKS set the time of day",
"summon KIND [X Y Z] [baby] spawn a mob (in front of the player without a position)",
"mobs [RADIUS] list mobs near the player with their AI state",
@@ -410,6 +411,15 @@ impl Game<'_> {
self.apply_settings();
ok()
}
"lod-distance" => {
let n: u32 = arg(1)?.parse()?;
if n > 1024 {
return Err("LOD distance must be 0..1024".into());
}
self.settings.horizon.lod_distance = n;
self.apply_settings();
ok()
}
"time" => {
let time: i64 = arg(1)?.parse()?;
self.world.day_time = time;
@@ -740,6 +750,16 @@ impl Game<'_> {
.map(|k| format!("{k:?}"))
.collect();
physical_keys.sort();
let lod = json!({
"distance": self.settings.horizon.lod_distance,
"terrain_distance": self.terrain_distance(),
"source": if self.lod.is_none() { "none" } else if self.integrated.is_some() { "integrated" } else { "local" },
"source_attempted": self.lod_source_attempted,
"active_tiles": self.lod.as_ref().map_or(0, |lod| lod.stats().0),
"cached_tiles": self.lod.as_ref().map_or(0, |lod| lod.stats().1),
"pending_observations": self.lod_observe.len(),
"refined_samples": self.lod.as_ref().map_or(0, |lod| lod.refined_samples()),
});
let mut status = json!({
"screen": self.screen_name(),
"debug": {
@@ -798,6 +818,11 @@ impl Game<'_> {
.eye_position()
.map(|e| self.shown_name_tags(glam::DVec3::from_array(e))),
});
status["lod"] = lod;
let (target, sampled) = self.lod.as_ref().map_or((0, 0), |lod| lod.progress());
status["lod"]["target_tiles"] = target.into();
status["lod"]["sampled_target_tiles"] = sampled.into();
status["pending_uploads"] = self.pending_uploads.len().into();
status["physical_keys"] = json!(physical_keys);
// Boats, minecarts, armor stands, item frames and paintings.
status["objects"] = json!(self.objects_status());
+1
View File
@@ -189,6 +189,7 @@ impl Game<'_> {
horizon_client::ui::load_metadata(root);
lang::set(lang::Language::load(&[&root.to_path_buf()], &lang::code()));
self.audio.reload(root);
self.clear_lod();
self.streaming.reload_resources(&mut self.world, context);
self.pending_uploads.clear();
// New GPU textures: a renderer built from the reloaded resources; the
+6
View File
@@ -288,6 +288,8 @@ impl Game<'_> {
self.screen = Screen::Playing;
self.title = None;
self.dim = Dim::Overworld;
self.clear_lod();
self.pending_uploads.clear();
if let Some(r) = self.renderer.as_mut() {
r.clear_sections();
}
@@ -396,6 +398,8 @@ impl Game<'_> {
self.border = Default::default();
self.screen = Screen::Playing;
self.container = None;
self.clear_lod();
self.pending_uploads.clear();
if let Some(r) = self.renderer.as_mut() {
r.clear_sections();
}
@@ -706,6 +710,8 @@ impl Game<'_> {
// A new ClientLevel: a new chunk storage, centred on 0, 0.
r.chunk_center = (0, 0);
}
self.clear_lod();
self.pending_uploads.clear();
if let Some(renderer) = self.renderer.as_mut() {
renderer.clear_sections();
}
@@ -91,7 +91,8 @@ impl Game<'_> {
(water & 255) as f32 / 255.0,
],
biome,
render_distance_chunks: self.streaming.radius,
render_distance_chunks: self.terrain_distance(),
distant_terrain: self.lod.is_some(),
cloud_range_chunks: self.settings.cloud_range,
camera_y: camera.y,
// level.getMinY(): the Nether and the End start at y 0.
+182 -54
View File
@@ -62,6 +62,7 @@ mod arena;
mod cull;
mod flat;
mod java_stages;
mod lod;
mod occlusion;
mod pack_stages;
pub mod panorama;
@@ -71,6 +72,16 @@ pub mod translucency;
pub(crate) mod upload;
mod world_post;
/// A section's instance: its camera-relative origin and, in `w`, the step of
/// its fixed-point UVs (`terrain.wgsl`'s `terrain_uv`).
fn section_instance(origin: [f32; 3]) -> [f32; 4] {
[
origin[0],
origin[1],
origin[2],
1.0 / crate::mesher::UV_STEPS,
]
}
/// A new value for `Renderer::block_entity_revision`.
fn next_block_entity_revision() -> u64 {
static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
@@ -132,6 +143,8 @@ pub struct Frame<'a> {
pub sky: Sky,
/// Render distance in blocks (fog end).
pub distance: f32,
/// Full-detail terrain distance in blocks; distant LODs bypass the cave graph.
pub detail_distance: f32,
/// `Camera.depthFar`: the far plane (`crate::camera::depth_far`).
pub depth_far: f32,
pub underwater: bool,
@@ -356,6 +369,8 @@ pub struct Renderer {
outline: wgpu::Buffer,
depth: wgpu::TextureView,
sections: resident::Resident<SectionGpu>,
lods: HashMap<crate::lod::TileKey, lod::Tile>,
detail_columns: HashMap<(i32, i32), u32>,
arena: arena::Arena,
sorted_indices: arena::Arena,
translucent_sorter: translucency::Sorter,
@@ -367,6 +382,8 @@ pub struct Renderer {
pub multi_draw: bool,
/// Per-frame scratch, kept to avoid allocating every frame.
visible: Vec<resident::Visible>,
visible_lods: Vec<lod::Visible>,
translucent_draws: Vec<(u64, usize, [u32; 5], (u32, Option<u32>))>,
origins: Vec<[f32; 4]>,
/// `DrawIndexedIndirectArgs` words: index count, instance count, first
/// index, base vertex, first instance.
@@ -756,6 +773,7 @@ impl Renderer {
let vertices: u64 = self
.sections
.values()
.chain(self.lods.values().map(|tile| &tile.mesh))
.map(|s| u64::from(s.solid + s.translucent) * 4)
.sum();
[self.arena.bytes(), vertices * stride]
@@ -2019,6 +2037,8 @@ impl Renderer {
outline,
depth,
sections: resident::Resident::default(),
lods: HashMap::new(),
detail_columns: HashMap::new(),
arena,
sorted_indices,
translucent_sorter: translucency::Sorter::default(),
@@ -2027,6 +2047,8 @@ impl Renderer {
indirect_capacity: 1024,
multi_draw,
visible: Vec::new(),
visible_lods: Vec::new(),
translucent_draws: Vec::new(),
origins: Vec::new(),
draws: Vec::new(),
draw_pages: Vec::new(),
@@ -2230,11 +2252,22 @@ impl Renderer {
if changed {
self.block_entity_revision = next_block_entity_revision();
}
self.release_section(key);
let section = self.upload_mesh(mesh);
if section.is_some() || (mesh.solid.is_empty() && mesh.translucent.is_empty()) {
*self.detail_columns.entry((key.0, key.2)).or_default() |= 1 << key.1;
} else if let Some(mask) = self.detail_columns.get_mut(&(key.0, key.2)) {
*mask &= !(1 << key.1);
}
if let Some(section) = section {
self.sections.insert(key, section);
}
}
fn upload_mesh(&mut self, mesh: &SectionMesh) -> Option<SectionGpu> {
let solid = (mesh.solid.len() / 4) as u32;
let translucent = (mesh.translucent.len() / 4) as u32;
self.release_section(key);
if solid + translucent == 0 {
return;
return None;
}
let max = solid.max(translucent);
if max > self.index_quads {
@@ -2249,16 +2282,14 @@ impl Renderer {
})
});
// With video and system memory both full, the section is not drawn.
let Some((page, start)) = self.arena.alloc(&self.device, encoder, vertices) else {
return;
};
let (page, start) = self.arena.alloc(&self.device, encoder, vertices)?;
let sorted_indices = if translucent > 0 {
let indices =
self.sorted_indices
.alloc(&self.device, encoder, u64::from(translucent) * 6);
if indices.is_none() {
self.arena.release(page, start, vertices);
return;
return None;
}
indices
} else {
@@ -2293,26 +2324,23 @@ impl Renderer {
{
self.queue.submit([copies]);
}
self.sections.insert(
key,
SectionGpu {
page,
start,
solid,
translucent,
faces: mesh.faces,
sort: translucency::SortState::new(
mesh.translucent
.as_chunks::<4>()
.0
.iter()
.map(|q| translucency::quad_center(q[0].pos, q[2].pos))
.collect(),
),
sorted_indices,
flat_id,
},
);
Some(SectionGpu {
page,
start,
solid,
translucent,
faces: mesh.faces,
sort: translucency::SortState::new(
mesh.translucent
.as_chunks::<4>()
.0
.iter()
.map(|q| translucency::quad_center(q[0].pos, q[2].pos))
.collect(),
),
sorted_indices,
flat_id,
})
}
/// The section copies recorded since the last submission, closed.
fn take_section_copies(&mut self) -> Option<wgpu::CommandBuffer> {
@@ -2357,17 +2385,20 @@ impl Renderer {
}
fn release_section(&mut self, key: SectionKey) {
if let Some(s) = self.sections.remove(key) {
if let (Some(flat), Some(id)) = (&mut self.flat, s.flat_id) {
flat.remove(id);
}
if let Some((page, start)) = s.sorted_indices {
self.sorted_indices
.release(page, start, u64::from(s.translucent) * 6);
}
self.arena
.release(s.page, s.start, u64::from(s.solid + s.translucent) * 4);
self.release_mesh(s);
}
}
fn release_mesh(&mut self, s: SectionGpu) {
if let (Some(flat), Some(id)) = (&mut self.flat, s.flat_id) {
flat.remove(id);
}
if let Some((page, start)) = s.sorted_indices {
self.sorted_indices
.release(page, start, u64::from(s.translucent) * 6);
}
self.arena
.release(s.page, s.start, u64::from(s.solid + s.translucent) * 4);
}
/// Block-entity blocks of every meshed section (see `SectionMesh::block_entities`).
pub fn block_entities(&self) -> impl Iterator<Item = &([i32; 3], u32)> {
self.block_entities.values().flatten()
@@ -2380,6 +2411,8 @@ impl Renderer {
self.block_entities.clear();
self.block_entity_revision = next_block_entity_revision();
self.sections.clear();
self.lods.clear();
self.detail_columns.clear();
self.arena.clear();
if let Some(flat) = &mut self.flat {
flat.clear();
@@ -2388,7 +2421,19 @@ impl Renderer {
self.translucency_cursor = 0;
self.occlusion.clear();
}
pub fn lod_detail_columns(&self) -> impl Iterator<Item = (i32, i32)> + '_ {
let complete = (1u32 << (crate::chunk::HEIGHT / 16)) - 1;
self.detail_columns
.iter()
.filter_map(move |(&column, &mask)| (mask == complete).then_some(column))
}
pub fn remove(&mut self, key: SectionKey) {
if let Some(mask) = self.detail_columns.get_mut(&(key.0, key.2)) {
*mask &= !(1 << key.1);
if *mask == 0 {
self.detail_columns.remove(&(key.0, key.2));
}
}
if self.block_entities.remove(&key).is_some() {
self.block_entity_revision = next_block_entity_revision();
}
@@ -2776,7 +2821,9 @@ impl Renderer {
let camera_section = camera_section(camera);
let culling = !frame.title && frame.cave_culling;
if culling {
crate::spans::time("occlusion", || self.start_occlusion(camera, frame.distance));
crate::spans::time("occlusion", || {
self.start_occlusion(camera, frame.detail_distance)
});
}
let reachable = &self.occlusion;
let mut visible = std::mem::take(&mut self.visible);
@@ -2784,15 +2831,27 @@ impl Renderer {
crate::spans::time("visibility", || {
self.sections.visible(
camera,
(frame.distance / 16.0) as i32,
(frame.detail_distance / 16.0) as i32,
&view,
|k| !culling || reachable.contains(k),
&mut visible,
)
});
self.drawn_sections = visible.len();
if visible.len() as u64 > self.instance_capacity {
self.instance_capacity = (visible.len() as u64).next_power_of_two();
let mut visible_lods = std::mem::take(&mut self.visible_lods);
visible_lods.clear();
if frame.title {
visible.clear();
} else {
visible_lods.extend(
self.lods
.iter()
.filter_map(|(&key, tile)| tile.visible(key, camera, frame.distance, &view)),
);
visible_lods.sort_unstable_by_key(|v| (v.distance, v.key));
}
self.drawn_sections = visible.len() + visible_lods.len();
if self.drawn_sections as u64 > self.instance_capacity {
self.instance_capacity = (self.drawn_sections as u64).next_power_of_two();
self.instances = self.device.create_buffer(&wgpu::BufferDescriptor {
label: Some("Section origins"),
size: self.instance_capacity * 16,
@@ -2806,7 +2865,8 @@ impl Renderer {
self.origins.extend(
visible
.iter()
.map(|v| [v.origin[0], v.origin[1], v.origin[2], 0.0]),
.map(|v| section_instance(v.origin))
.chain(visible_lods.iter().map(|v| lod::instance(v.origin))),
);
if !self.origins.is_empty() {
self.uploads.buffer(
@@ -2816,9 +2876,6 @@ impl Renderer {
bytemuck::cast_slice(&self.origins),
);
}
if frame.title {
visible.clear();
}
// Solid sections front to back, then translucent ones back to front,
// as one list of indirect draws over the section arena.
self.draws.clear();
@@ -2865,6 +2922,16 @@ impl Renderer {
None => {}
}
}
// Distant terrain tiles are never occlusion tested: phase 1.
for (i, v) in visible_lods.iter().enumerate() {
let s = &self.lods[&v.key].mesh;
s.faces.ranges(eye - Vec3::from(v.origin), |first, quads| {
let first = s.start + u64::from(first) * 4;
self.draws
.push([quads * 6, 1, 0, first as u32, (visible.len() + i) as u32]);
self.draw_pages.push((s.page, None));
});
}
let phase1_draws = self.draws.len() as u32;
self.draws.extend_from_slice(&self.phase2_draws);
self.draw_pages.extend_from_slice(&self.phase2_pages);
@@ -2872,7 +2939,8 @@ impl Renderer {
let solid_draws = self.draws.len() as u32;
// LevelRenderer: nearby sections each frame, plus a rotating eighth
// (at least 15) of the visible list. Cached views avoid needless sorts.
let count = visible.len();
self.translucent_draws.clear();
let count = visible.len() + visible_lods.len();
let budget = (count / 8).max(15).min(count);
self.translucency_cursor %= count.max(1);
for (i, v) in visible.iter().enumerate().rev() {
@@ -2900,16 +2968,67 @@ impl Renderer {
);
}
let first = s.start + u64::from(s.solid) * 4;
self.draws.push([
s.translucent * 6,
1,
index_start as u32,
first as u32,
i as u32,
]);
self.draw_pages.push((s.page, Some(index_page)));
self.translucent_draws.push((
v.distance,
i,
[
s.translucent * 6,
1,
index_start as u32,
first as u32,
i as u32,
],
(s.page, Some(index_page)),
));
}
}
// Sections went back to front (`visible` is sorted by distance, then
// key): tiles' water only needs sorting in when there is some.
let section_translucent = self.translucent_draws.len();
for (i, v) in visible_lods.iter().enumerate() {
let instance = visible.len() + i;
let tile = self.lods.get_mut(&v.key).expect("visible resident tile");
let s = &mut tile.mesh;
if let Some((index_page, index_start)) = s.sorted_indices {
let scheduled = (instance + count - self.translucency_cursor) % count < budget;
if (!s.sort.is_sorted() || scheduled)
&& let Some(indices) = s.sort.update_lod(
camera.to_array(),
v.key.origin(),
&mut self.translucent_sorter,
)
{
self.uploads.buffer(
&mut encoder,
self.sorted_indices.buffer(index_page),
index_start * 4,
bytemuck::cast_slice(indices),
);
}
let first = s.start + u64::from(s.solid) * 4;
self.translucent_draws.push((
v.distance,
instance,
[
s.translucent * 6,
1,
index_start as u32,
first as u32,
instance as u32,
],
(s.page, Some(index_page)),
));
}
}
// Near water and distant water share one back-to-front draw order.
if self.translucent_draws.len() > section_translucent {
self.translucent_draws
.sort_unstable_by(|a, b| b.0.cmp(&a.0).then(b.1.cmp(&a.1)));
}
for &(_, _, draw, pages) in &self.translucent_draws {
self.draws.push(draw);
self.draw_pages.push(pages);
}
self.translucency_cursor += budget;
let translucent_draws = self.draws.len() as u32 - solid_draws;
self.section_draws = self.draws.len();
@@ -3096,6 +3215,7 @@ impl Renderer {
if self.flat_on && !self.draws.is_empty() && self.draws.len() >= flat.min_draws =>
{
let sections = &self.sections;
let lods = &self.lods;
let sorted = &self.sorted_indices;
crate::spans::time("flatten", || {
flat.prepare(
@@ -3107,7 +3227,14 @@ impl Renderer {
solid_draws as usize,
visible
.iter()
.filter_map(|v| sections.get(v.slot).flat_id.map(|id| (id, v.origin))),
.filter_map(|v| {
let id = sections.get(v.slot).flat_id?;
Some((id, section_instance(v.origin)))
})
.chain(visible_lods.iter().filter_map(|v| {
let id = lods[&v.key].mesh.flat_id?;
Some((id, lod::instance(v.origin)))
})),
|page| sorted.buffer(page).clone(),
)
})
@@ -3629,6 +3756,7 @@ impl Renderer {
self.last_encode = started.elapsed().saturating_sub(self.last_wait);
let _ = self.started;
self.visible = visible;
self.visible_lods = visible_lods;
Ok(true)
}
}
+2 -2
View File
@@ -330,7 +330,7 @@ impl Flat {
draws: &[[u32; 5]],
pages: &[(u32, Option<u32>)],
solid_draws: usize,
origins: impl Iterator<Item = (u32, [f32; 3])>,
origins: impl Iterator<Item = (u32, [f32; 4])>,
sorted_page: impl Fn(u32) -> wgpu::Buffer,
) -> bool {
// Origins by section id.
@@ -340,7 +340,7 @@ impl Flat {
if self.origin_data.len() <= id {
self.origin_data.resize(id + 1, [0.0; 4]);
}
self.origin_data[id] = [o[0], o[1], o[2], 0.0];
self.origin_data[id] = o;
}
if self.origin_data.len() as u64 > self.origin_capacity {
self.origin_capacity = (self.origin_data.len() as u64).next_power_of_two();
+146
View File
@@ -0,0 +1,146 @@
//! Far meshes share the terrain arena and passes, with their actual bounds
//! rather than the 16³ boxes used by the near-section residency walk.
use super::{Frustum, Renderer, SectionGpu};
use crate::{lod::TileKey, mesher::SectionMesh};
use glam::{DVec3, Vec3};
pub(super) struct Tile {
pub mesh: SectionGpu,
bounds: Bounds,
}
#[derive(Clone, Copy)]
struct Bounds {
min: Vec3,
max: Vec3,
}
impl Bounds {
fn mesh(mesh: &SectionMesh) -> Option<Self> {
let mut positions = mesh
.solid
.iter()
.chain(&mesh.translucent)
.map(|v| Vec3::from(v.pos));
let first = positions.next()?;
let mut bounds = Self {
min: first,
max: first,
};
for pos in positions {
bounds.min = bounds.min.min(pos);
bounds.max = bounds.max.max(pos);
}
Some(bounds)
}
fn visible(self, origin: DVec3, distance: f32, frustum: &Frustum) -> Option<u64> {
let min = origin + self.min.as_dvec3();
let max = origin + self.max.as_dvec3();
let nearest = DVec3::ZERO.clamp(min, max);
if nearest.x * nearest.x + nearest.z * nearest.z > f64::from(distance).powi(2)
|| !frustum.intersects(min.as_vec3(), max.as_vec3())
{
return None;
}
Some(((min + max) * 0.5).length_squared().to_bits())
}
}
/// A tile's instance: like a section's, with the UV step of merged faces.
pub(super) fn instance(origin: [f32; 3]) -> [f32; 4] {
[
origin[0],
origin[1],
origin[2],
1.0 / crate::mesher::TILED_UV_STEPS,
]
}
pub(super) struct Visible {
pub key: TileKey,
pub distance: u64,
pub origin: [f32; 3],
}
impl Tile {
pub fn visible(
&self,
key: TileKey,
camera: DVec3,
distance: f32,
frustum: &Frustum,
) -> Option<Visible> {
let origin = DVec3::from(key.origin().map(f64::from)) - camera;
Some(Visible {
key,
distance: self.bounds.visible(origin, distance, frustum)?,
origin: origin.as_vec3().to_array(),
})
}
}
impl Renderer {
pub fn resident_lods(&self) -> usize {
self.lods.len()
}
pub fn upload_lod(&mut self, key: TileKey, mesh: &SectionMesh) {
self.remove_lod(key);
if let Some(bounds) = Bounds::mesh(mesh)
&& let Some(mesh) = self.upload_mesh(mesh)
{
self.lods.insert(key, Tile { mesh, bounds });
}
}
pub fn remove_lod(&mut self, key: TileKey) {
if let Some(tile) = self.lods.remove(&key) {
self.release_mesh(tile.mesh);
}
}
pub fn clear_lods(&mut self) {
for (_, tile) in std::mem::take(&mut self.lods) {
self.release_mesh(tile.mesh);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::mesher::TerrainVertex;
#[test]
fn large_tile_bounds_include_water_and_do_not_cull_by_origin() {
let vertex = |pos| TerrainVertex::tiled(pos, [0.0; 2], 0, 0, 0);
let mesh = SectionMesh {
solid: vec![vertex([0.0, -64.0, 0.0]), vertex([1024.0, 100.0, 1024.0])],
translucent: vec![vertex([512.0, 120.0, 512.0])],
..Default::default()
};
let bounds = Bounds::mesh(&mesh).unwrap();
assert_eq!(bounds.min, Vec3::new(0.0, -64.0, 0.0));
assert_eq!(bounds.max, Vec3::new(1024.0, 120.0, 1024.0));
let view = Frustum {
planes: super::super::frustum(
super::super::world_projection(70.0, 16.0 / 9.0, 8192.0)
* crate::camera::view_matrix(0.0, 0.0),
),
};
// The tile origin is farther than the range, but its far edge reaches
// the camera. Negative world origins therefore remain visible.
assert!(
bounds
.visible(DVec3::new(-1000.0, -80.0, -512.0), 256.0, &view)
.is_some()
);
assert!(
bounds
.visible(DVec3::new(-2000.0, -80.0, -512.0), 256.0, &view)
.is_none()
);
assert!(Bounds::mesh(&SectionMesh::default()).is_none());
}
}
@@ -47,6 +47,25 @@ impl SortState {
self.last.is_some()
}
/// Large tiles cannot use the section viewpoint shortcut: the camera may
/// cross a quad's plane while remaining on the same side of the first 16³.
pub fn update_lod<'a>(
&mut self,
camera: [f64; 3],
origin: [i32; 3],
sorter: &'a mut Sorter,
) -> Option<&'a [u32]> {
let block = camera.map(|v| v.floor() as i32);
if self.last.is_some_and(|(last, _)| last == block) {
return None;
}
self.last = Some((block, [0; 3]));
Some(sorter.sort(
&self.centers,
std::array::from_fn(|i| (camera[i] - f64::from(origin[i])) as f32),
))
}
/// TranslucencyPointOfView and ResortTransparencyTask: diagonal views keep
/// their ordering until the point of view changes; aligned views also
/// resort on crossing a block boundary. Sub-block movement needs no upload.
@@ -116,4 +135,28 @@ mod tests {
.is_some()
);
}
#[test]
fn large_tiles_resort_inside_the_same_section_viewpoint() {
let mut state = SortState::new(vec![[32.0, 0.0, 0.0], [96.0, 0.0, 0.0]]);
let mut sorter = Sorter::default();
assert_eq!(
state
.update_lod([48.0, 20.0, 20.0], [0; 3], &mut sorter)
.unwrap()[0],
4
);
assert!(
state
.update_lod([48.5, 20.0, 20.0], [0; 3], &mut sorter)
.is_none()
);
// Both views have positive x/y/z signs relative to the first section,
// so the section-sized shortcut would incorrectly keep the old order.
assert_eq!(
state
.update_lod([80.0, 20.0, 20.0], [0; 3], &mut sorter)
.unwrap()[0],
0
);
}
}
+20 -1
View File
@@ -159,6 +159,8 @@ pub enum Overlay {
#[derive(Clone, Debug, PartialEq)]
pub struct Graphics {
pub preset: Preset,
/// Distant surface terrain range, in chunks; 0 disables LODs.
pub lod_distance: u32,
/// Skip sections hidden behind closed ones (vanilla's occlusion graph).
pub cave_culling: bool,
/// Hide shared leaf and mangrove-root faces, reducing foliage density.
@@ -198,6 +200,7 @@ impl Default for Graphics {
fn default() -> Self {
Self {
preset: Preset::Custom,
lod_distance: 0,
cave_culling: true,
cull_leaves: false,
entity_culling: false,
@@ -244,6 +247,7 @@ impl Graphics {
let number = |d: u32| value.parse().unwrap_or(d);
match key.trim() {
"preset" => g.preset = Preset::parse(value),
"lodDistance" => g.lod_distance = number(g.lod_distance).min(1024),
"caveCulling" => g.cave_culling = flag(g.cave_culling),
"cullLeaves" => g.cull_leaves = flag(g.cull_leaves),
"entityCulling" => g.entity_culling = flag(g.entity_culling),
@@ -282,8 +286,9 @@ impl Graphics {
}
pub fn serialize(&self) -> String {
format!(
"preset:{}\ncaveCulling:{}\ncullLeaves:{}\nentityCulling:{}\nchunkUpdates:{}\nuploadBudgetKiB:{}\nsky:{}\nfog:{}\nweather:{}\nanimatedTextures:{}\npresentMode:{}\nframePacing:{}\nworkers:{}\noverlay:{}\nresolutionScale:{}\nfxaa:{}\nlevel:{}\nbatteryMode:{}\nshaderPack:{}\ngraphicsApi:{}\n",
"preset:{}\nlodDistance:{}\ncaveCulling:{}\ncullLeaves:{}\nentityCulling:{}\nchunkUpdates:{}\nuploadBudgetKiB:{}\nsky:{}\nfog:{}\nweather:{}\nanimatedTextures:{}\npresentMode:{}\nframePacing:{}\nworkers:{}\noverlay:{}\nresolutionScale:{}\nfxaa:{}\nlevel:{}\nbatteryMode:{}\nshaderPack:{}\ngraphicsApi:{}\n",
self.preset.name(),
self.lod_distance,
bool_text(self.cave_culling),
bool_text(self.cull_leaves),
bool_text(self.entity_culling),
@@ -384,6 +389,7 @@ mod tests {
fn round_trips() {
let g = Graphics {
preset: Preset::Balanced,
lod_distance: 256,
cave_culling: false,
cull_leaves: true,
entity_culling: true,
@@ -412,6 +418,19 @@ mod tests {
);
}
#[test]
fn lod_distance_is_opt_in_and_bounded() {
assert_eq!(Graphics::default().lod_distance, 0);
for distance in [0, 64, 128, 256, 512, 1024] {
let g = Graphics::parse(&format!("lodDistance:{distance}\n"));
assert_eq!(g.lod_distance, distance);
assert_eq!(Graphics::parse(&g.serialize()), g);
}
assert_eq!(Graphics::parse("lodDistance:10000\n").lod_distance, 1024);
assert_eq!(Graphics::parse("lodDistance:-1\n").lod_distance, 0);
assert_eq!(Graphics::parse("lodDistance:invalid\n").lod_distance, 0);
}
#[test]
fn automatic_workers_leave_cores_for_the_render_and_server_threads() {
let g = Graphics::default();
+54 -2
View File
@@ -21,6 +21,7 @@ pub enum Level {
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum H {
Preset,
LodDistance,
CaveCulling,
CullLeaves,
EntityCulling,
@@ -77,7 +78,12 @@ pub fn rows(level: Level) -> Vec<Row> {
let render = v(
Opt::RenderDistance,
High,
"How far terrain is drawn, in chunks. The biggest lever on frame time.",
"Full-detail terrain range. LOD Distance can extend local worlds farther.",
);
let lod = h(
H::LodDistance,
Medium,
"Coarse distant surface in local worlds; no extra simulation.",
);
let fov = v(
Opt::Fov,
@@ -101,10 +107,11 @@ pub fn rows(level: Level) -> Vec<Row> {
"Sets the options below together. Changing one makes it Custom.",
);
let mut rows = match level {
Level::Simple => vec![preset, render, fov, fps, vsync, gui],
Level::Simple => vec![preset, render, lod, fov, fps, vsync, gui],
Level::Advanced => vec![
Row::Header("Rendering"),
render,
lod,
v(
Opt::SimulationDistance,
Medium,
@@ -324,6 +331,10 @@ impl H {
let g = &s.horizon;
match self {
H::Preset => labelled("Preset", g.preset.label()),
H::LodDistance => match g.lod_distance {
0 => labelled("LOD Distance", "Off"),
n => labelled("LOD Distance", format!("{n} chunks")),
},
H::CaveCulling => labelled("Cave Culling", on_off(g.cave_culling)),
H::CullLeaves => labelled("Cull Leaves", on_off(g.cull_leaves)),
H::EntityCulling => labelled("Entity Culling", on_off(g.entity_culling)),
@@ -397,6 +408,9 @@ impl H {
}
let g = &mut s.horizon;
match self {
H::LodDistance => {
g.lod_distance = step(&[0, 64, 128, 256, 512, 1024], g.lod_distance, back)
}
H::CaveCulling => g.cave_culling = !g.cave_culling,
H::CullLeaves => g.cull_leaves = !g.cull_leaves,
H::EntityCulling => g.entity_culling = !g.entity_culling,
@@ -482,6 +496,10 @@ impl Item {
}
pub fn label(self, s: &Settings) -> Text {
match self {
Item::Vanilla(Opt::RenderDistance) if s.horizon.lod_distance > 0 => labelled(
"Full Detail Distance",
format!("{} chunks", s.render_distance),
),
Item::Vanilla(o) => o.label(s),
Item::Horizon(h) => h.label(s),
}
@@ -939,6 +957,40 @@ mod tests {
}
}
#[test]
fn lod_distance_cycles_in_both_levels_without_changing_near_distance() {
let mut s = Settings::default();
let item = Item::Horizon(H::LodDistance);
let near = s.render_distance;
assert_eq!(item.label(&s).plain(), "LOD Distance: Off");
for level in [Level::Simple, Level::Advanced] {
let items: Vec<_> = rows(level)
.into_iter()
.filter_map(|row| match row {
Row::Setting(setting) => Some(setting.item),
Row::Header(_) => None,
})
.collect();
let render = items
.iter()
.position(|i| *i == Item::Vanilla(Opt::RenderDistance))
.unwrap();
assert_eq!(items[render + 1], item);
}
for distance in [64, 128, 256, 512, 1024, 0] {
item.cycle(&mut s, false, [854, 480]);
assert_eq!(s.horizon.lod_distance, distance);
assert_eq!(s.render_distance, near);
}
item.cycle(&mut s, true, [854, 480]);
assert_eq!(s.horizon.lod_distance, 1024);
assert_eq!(item.label(&s).plain(), "LOD Distance: 1024 chunks");
assert_eq!(
Item::Vanilla(Opt::RenderDistance).label(&s).plain(),
format!("Full Detail Distance: {near} chunks")
);
}
#[test]
fn the_shader_pack_row_is_off_by_default_and_outside_presets() {
let mut s = Settings::default();
+1
View File
@@ -102,6 +102,7 @@ pub mod landing;
pub mod lang;
pub mod level_loading;
pub mod light;
pub mod lod;
pub mod logging;
pub mod menu;
pub mod menu_blur;
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+14
View File
@@ -64,6 +64,9 @@ pub struct TerrainVertex {
}
/// Fixed-point steps per unit of `TerrainVertex` UVs, which span `[0, 2)`.
pub const UV_STEPS: f32 = 32768.0;
/// Fixed-point steps per block of distant-terrain UVs (`TerrainVertex::tiled`),
/// which span `[0, 2048)`: merged faces repeat their sprite once per block.
pub const TILED_UV_STEPS: f32 = 32.0;
impl TerrainVertex {
pub fn new(pos: [f32; 3], uv: [f32; 2], layer: u16, color: u32, light: u16) -> Self {
Self {
@@ -74,6 +77,17 @@ impl TerrainVertex {
color,
}
}
/// A vertex of a merged distant-terrain face, its UV in blocks from the
/// face's corner. Drawn with an instance UV scale of `1 / TILED_UV_STEPS`.
pub fn tiled(pos: [f32; 3], uv: [f32; 2], layer: u16, color: u32, light: u16) -> Self {
Self {
pos,
uv: uv.map(|v| (v * TILED_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)
@@ -42,6 +42,11 @@ impl Default for FaceGroups {
}
impl FaceGroups {
/// Group arbitrary-sized terrain quads, including distant LOD tiles.
pub fn group(solid: &mut Vec<TerrainVertex>) -> Self {
group(solid)
}
/// Whether group `g` can hold a front-facing quad for an eye at `eye`,
/// relative to the section's minimum corner.
pub fn visible(&self, g: usize, eye: Vec3) -> bool {
+559 -1
View File
@@ -29,7 +29,7 @@ use horizon_world::{
chunk::BlockState,
chunk_status::ChunkStatus,
compression::Compression,
heightmap::{Heightmap, HeightmapChunk, HeightmapType},
heightmap::{Heightmap, HeightmapChunk, HeightmapState, HeightmapType},
nbt::{self, Compound, JavaString, Limits, NamedTag, Tag},
region::{ChunkRecord, RegionFile},
registry::{BlockRegistry, BlockStateId},
@@ -2833,11 +2833,362 @@ pub struct Persistent<G> {
pub registry: Arc<crate::assets::Assets>,
pub biomes: Arc<Biomes>,
}
type LodSurfaces = Arc<[crate::lod::Column; 256]>;
struct SavedLodSampler<G: ChunkGenerator> {
generator: Arc<Persistent<G>>,
inner: Box<dyn crate::streaming::LodSampler>,
cache: std::collections::VecDeque<((i32, i32), Option<LodSurfaces>)>,
details: Vec<crate::streaming::LodDetail>,
}
impl<G: ChunkGenerator> SavedLodSampler<G> {
fn saved_columns(&mut self, key: (i32, i32)) -> Option<LodSurfaces> {
let cached = match self.cache.iter().position(|(at, _)| *at == key) {
Some(index) => self.cache.remove(index).unwrap(),
None => (
key,
self.generator.save.lod_surfaces(
key.0,
key.1,
&self.generator.registry,
&self.generator.biomes,
),
),
};
let columns = cached.1.clone();
self.cache.push_front(cached);
self.cache.truncate(128);
columns
}
}
impl<G: ChunkGenerator> crate::streaming::LodSampler for SavedLodSampler<G> {
fn fork(&self) -> Option<Box<dyn crate::streaming::LodSampler>> {
Some(Box::new(Self {
generator: self.generator.clone(),
inner: self.inner.fork()?,
cache: Default::default(),
details: Vec::new(),
}))
}
fn request_detail(&mut self, chunks: &[(i32, i32)], center: [i32; 2]) {
let mut missing = Vec::new();
for &key in chunks {
if let Some(columns) = self.saved_columns(key) {
self.details.push((key, columns));
} else {
missing.push(key);
}
}
self.inner.request_detail(&missing, center);
}
fn drain_details(&mut self) -> Vec<crate::streaming::LodDetail> {
let mut details = std::mem::take(&mut self.details);
for (key, generated) in self.inner.drain_details() {
// A generation batch also returns neighbouring, unrequested chunks.
// Their saved builds must take precedence too.
details.push((key, self.saved_columns(key).unwrap_or(generated)));
}
details
}
fn grid(&mut self, x: i32, z: i32, stride: i32, side: usize) -> Vec<crate::lod::Column> {
let mut columns = self.inner.grid(x, z, stride, side);
if stride <= 16 {
for (index, column) in columns.iter_mut().enumerate() {
let (wx, wz) = (
x + (index % side) as i32 * stride,
z + (index / side) as i32 * stride,
);
if let Some(saved) = self.saved_columns((wx >> 4, wz >> 4)) {
*column = crate::lod::Column::reduced(
&saved,
(wx & 15) as usize,
(wz & 15) as usize,
stride as usize,
);
}
}
}
columns
}
fn sample(&mut self, x: i32, z: i32) -> Option<crate::lod::Surface> {
self.column(x, z).surface
}
fn column(&mut self, x: i32, z: i32) -> crate::lod::Column {
self.reduced_column(x, z, 1)
}
fn reduced_column(&mut self, x: i32, z: i32, width: i32) -> crate::lod::Column {
if width <= 16
&& let Some(columns) = self.saved_columns((x >> 4, z >> 4))
{
return crate::lod::Column::reduced(
&columns,
(x & 15) as usize,
(z & 15) as usize,
width as usize,
);
}
self.inner.reduced_column(x, z, width)
}
}
/// Read one palette index without expanding the other entries in the section.
fn lod_palette_index(
container: &Tag,
index: usize,
entries: usize,
minimum_bits: u32,
) -> Option<usize> {
let Tag::List(palette) = container.get("palette")? else {
return None;
};
if palette.is_empty() || palette.len() > entries || index >= entries {
return None;
}
if palette.len() == 1 {
return Some(0);
}
let bits = (usize::BITS - (palette.len() - 1).leading_zeros()).max(minimum_bits);
let per_word = 64 / bits as usize;
let Tag::LongArray(data) = container.get("data")? else {
return None;
};
if data.len() != entries.div_ceil(per_word) {
return None;
}
let value = ((data[index / per_word] as u64 >> ((index % per_word) * bits as usize))
& ((1 << bits) - 1)) as usize;
(value < palette.len()).then_some(value)
}
fn lod_light_layers(sections: &[(i32, &Tag)], sky_light: bool) -> Box<[u8]> {
let mut layers = [(None, None); SECTIONS];
for &(y, section) in sections {
let layer = |name: &str| match section.get(name) {
Some(Tag::ByteArray(data)) if data.len() == 2048 => Some(&data[..]),
_ => None,
};
layers[(y - (MIN_Y >> 4)) as usize] = (
if sky_light { layer("SkyLight") } else { None },
layer("BlockLight"),
);
}
let mut light = vec![0u8; SECTIONS * 4096].into_boxed_slice();
let mut above = [if sky_light { 15u8 } else { 0 }; 256];
let nibble = |data: &[u8], index: usize| (data[index / 2] >> ((index & 1) * 4)) & 15;
for si in (0..SECTIONS).rev() {
let (sky, block) = layers[si];
let section = &mut light[si * 4096..(si + 1) * 4096];
for (index, value) in section.iter_mut().enumerate() {
*value = sky.map_or(above[index & 255], |data| nibble(data, index)) << 4
| block.map_or(0, |data| nibble(data, index));
}
// Missing sky layers inherit the bottom layer above, as load_light does.
for (index, value) in above.iter_mut().enumerate() {
*value = section[index] >> 4;
}
}
light
}
fn lod_face_light(light: &[u8], x: usize, y: usize, z: usize) -> u16 {
let index = y * 256 + z * 16 + x;
let mut sky = light[index] >> 4;
let mut block = light[index] & 15;
for (valid, index) in [
(x > 0, index.wrapping_sub(1)),
(x < 15, index + 1),
(z > 0, index.wrapping_sub(16)),
(z < 15, index + 16),
(y > 0, index.wrapping_sub(256)),
(y + 1 < SECTIONS * 16, index + 256),
] {
if valid {
sky = sky.max(light[index] >> 4);
block = block.max(light[index] & 15);
}
}
u16::from(sky) << 4 | u16::from(block) << 12
}
fn decode_lod_surfaces(
root: &NamedTag,
assets: &crate::assets::Assets,
biomes: &Biomes,
sky_light: bool,
) -> Option<LodSurfaces> {
let Tag::List(sections) = root.tag.get("sections")? else {
return None;
};
let mut sections: Vec<_> = sections
.iter()
.filter_map(|section| {
let y = number(section.get("Y"))? as i32;
(MIN_Y >> 4..(MIN_Y >> 4) + SECTIONS as i32)
.contains(&y)
.then_some((y, section))
})
.collect();
sections.sort_unstable_by_key(|(y, _)| std::cmp::Reverse(*y));
let mut columns: Box<[crate::lod::Column; 256]> =
Box::new(std::array::from_fn(|_| crate::lod::Column {
surface: None,
spans: Vec::new(),
}));
let mut water = [None; 256];
let light = lod_light_layers(&sections, sky_light);
let mut states = BTreeMap::new();
for (section_y, section) in sections {
if let Some(blocks) = section.get("block_states") {
let Tag::List(palette) = blocks.get("palette")? else {
return None;
};
// Resolve each state once per palette, rather than parsing NBT for every block.
let palette: Vec<_> = palette
.iter()
.map(|entry| {
let descriptor = BlockState::from_nbt(entry).ok()?;
let state = *states.entry(descriptor.clone()).or_insert_with(|| {
assets
.registry
.id(&descriptor)
.or_else(|| assets.registry.default_state(&descriptor.name))
});
Some(state.map(|state| {
let properties = assets.properties.heightmap_state(state);
let fluid = matches!(
descriptor.name.as_str(),
"minecraft:water" | "minecraft:lava"
);
(state, fluid, properties.blocks_motion_in_heightmap())
}))
})
.collect::<Option<_>>()?;
let biome_palette: Vec<_> = section
.get("biomes")
.and_then(|container| match container.get("palette")? {
Tag::List(palette) => Some(palette),
_ => None,
})
.into_iter()
.flatten()
.map(|entry| match entry {
Tag::String(name) => biomes.id(&name.to_string_lossy()),
_ => 0,
})
.collect();
for z in 0..16 {
for x in 0..16 {
let cell = z * 16 + x;
let column = &mut columns[cell];
for y in (0..16).rev() {
let index = y * 256 + cell;
let palette_index = lod_palette_index(blocks, index, 4096, 4)?;
let Some((state, fluid, solid)) = palette[palette_index] else {
continue;
};
if !solid && !fluid {
continue;
}
let biome = section
.get("biomes")
.and_then(|container| {
lod_palette_index(
container,
(y / 4) * 16 + (z / 4) * 4 + x / 4,
64,
1,
)
})
.and_then(|index| biome_palette.get(index))
.copied()
.unwrap_or(0);
let height = section_y * 16 + y as i32 + 1;
if column.surface.is_none() {
if fluid {
water[cell].get_or_insert((height, state));
} else {
column.surface = Some(crate::lod::Surface {
height,
state,
water: water[cell],
biome,
});
}
}
let light = lod_face_light(&light, x, (height - 1 - MIN_Y) as usize, z);
let min = (height - 1) as i16;
let max = height as i16;
if let Some(previous) = column.spans.last_mut()
&& previous.min == max
&& (previous.state, previous.biome, previous.light)
== (state, biome, light)
{
previous.min = min;
} else {
column.spans.push(crate::lod::Span {
min,
max,
state,
biome,
light,
});
}
}
}
}
}
}
for column in &mut *columns {
column.spans.reverse();
}
Some(Arc::from(columns))
}
impl SaveDir {
fn lod_surfaces(
&self,
x: i32,
z: i32,
assets: &crate::assets::Assets,
biomes: &Biomes,
) -> Option<LodSurfaces> {
let pending = self
.pending
.lock()
.ok()?
.get(&(false, x, z))
.map(|(_, record)| record.clone());
let root = if let Some(record) = pending {
record.tag().map(|tag| (*tag).clone())?
} else {
// Far-only reads must not populate the gameplay region cache across the whole view.
let mut region = RegionFile::open(self.region_path("region", x >> 5, z >> 5)).ok()?;
region
.read(ChunkPos::new(x, z))
.ok()??
.nbt(Limits::default())
.ok()?
};
// Proto saves may still be missing decoration from their neighbors.
if text(root.tag.get("Status"))
.is_some_and(|status| status != "minecraft:full" && status != "full")
{
return None;
}
decode_lod_surfaces(&root, assets, biomes, self.sky_light)
}
}
impl<G: ChunkGenerator> ChunkGenerator for Persistent<G> {
type Worker = G::Worker;
fn worker(&self) -> G::Worker {
self.inner.worker()
}
fn lod_sampler(self: Arc<Self>) -> Box<dyn crate::streaming::LodSampler> {
let inner = ChunkGenerator::lod_sampler(self.inner.clone());
Box::new(SavedLodSampler {
generator: self,
inner,
cache: Default::default(),
details: Vec::new(),
})
}
fn load(&self, x: i32, z: i32) -> Option<Chunk> {
self.save
.tables
@@ -2849,6 +3200,10 @@ impl<G: ChunkGenerator> ChunkGenerator for Persistent<G> {
chunk.entities = self.save.load_entities(x, z);
Some(chunk)
}
fn lod_surface(&self, worker: &mut G::Worker, x: i32, z: i32) -> Option<crate::lod::Surface> {
// Resident/visited summaries override this procedural fallback in the LOD cache.
self.inner.lod_surface(worker, x, z)
}
fn prepare_loaded(&self, worker: &mut G::Worker, chunk: &Chunk) {
self.inner.prepare_loaded(worker, chunk);
}
@@ -2907,6 +3262,209 @@ impl<G: ChunkGenerator> ChunkGenerator for Persistent<G> {
#[cfg(test)]
mod tests {
#[test]
fn saved_far_surface_reads_builds_and_water_without_gameplay_chunks() {
let Some(root) = std::env::var_os("HORIZON_TEST_RESOURCES") else {
return;
};
let root = PathBuf::from(root);
let assets = crate::assets::Assets::load(&root).expect("assets");
let biomes = Biomes::load(&root).expect("biomes");
let stone = assets.registry.default_state("minecraft:stone").unwrap();
let water = assets.registry.default_state("minecraft:water").unwrap();
let sand = assets.registry.default_state("minecraft:sand").unwrap();
let dirt = assets.registry.default_state("minecraft:dirt").unwrap();
let leaves = assets
.registry
.default_state("minecraft:oak_leaves")
.unwrap();
let mut chunk = Chunk::empty(-2, 3);
chunk.set(0, 109, 0, stone.0);
chunk.set(3, 121, 5, stone.0);
chunk.set(3, 122, 5, stone.0);
chunk.set(3, 120, 5, dirt.0);
chunk.set(3, 119, 5, dirt.0);
chunk.set(3, 115, 5, stone.0);
chunk.set(11, 125, 11, stone.0);
chunk.set(14, 127, 14, stone.0);
chunk.set(14, 112, 14, stone.0);
for y in 95..98 {
chunk.set(2, y, 6, stone.0);
}
chunk.lit = true;
let mut cave_light = Box::new([0x03; 4096]);
cave_light[crate::chunk::index(12, 125, 11)] = 0xa7;
Arc::make_mut(&mut chunk.sections[((121 - MIN_Y) >> 4) as usize]).light =
Light::Full(cave_light);
chunk.set(7, 90, 9, sand.0);
chunk.set(7, 98, 9, leaves.0);
let forest = biomes.id("minecraft:forest");
chunk.set_biome_cell(7 / 4, (98 - MIN_Y) / 4, 9 / 4, forest);
chunk.set(4, 54, 4, sand.0);
for y in 55..64 {
chunk.set(4, y, 4, water.0);
}
let mut saved = encode(&chunk, assets.registry, &biomes);
put(
&mut saved.tag,
"block_entities",
Tag::String("not decoded by far sampling".into()),
);
let dir = std::env::temp_dir().join(format!("horizon-lod-save-{}", std::process::id()));
fs::create_dir_all(&dir).unwrap();
let save = SaveDir::new(dir.clone());
save.write_records("region", vec![((-2, 3), Some(saved))], true)
.unwrap();
let reopened = SaveDir::new(dir.clone());
let surfaces = reopened
.lod_surfaces(-2, 3, &assets, &biomes)
.expect("saved summary");
assert_eq!(surfaces[0].surface.unwrap().height, 110);
assert_eq!(surfaces[0].surface.unwrap().state, stone);
let narrow_build = surfaces[5 * 16 + 3].surface.unwrap();
assert_eq!((narrow_build.height, narrow_build.state), (123, stone));
assert!(
surfaces[5 * 16 + 2].surface.is_none(),
"adjacent columns stay distinct"
);
let strata = &surfaces[5 * 16 + 3].spans;
assert_eq!(
strata
.iter()
.map(|span| (span.min, span.max, span.state))
.collect::<Vec<_>>(),
vec![(115, 116, stone), (119, 121, dirt), (121, 123, stone)]
);
assert!(
strata.iter().all(|span| span.light == 0x3000),
"saved cave darkness and block light survive"
);
assert_eq!(
surfaces[11 * 16 + 11].spans[0].light,
0x70a0,
"neighbor face light uses separate sky/block channels"
);
let boundaries = &surfaces[14 * 16 + 14].spans;
assert_eq!(boundaries.len(), 2);
assert!(
boundaries.iter().all(|span| span.light == 0x30f0),
"face light crosses section boundaries above and below"
);
let across_sections = &surfaces[6 * 16 + 2].spans;
assert_eq!(across_sections.len(), 1);
assert_eq!((across_sections[0].min, across_sections[0].max), (95, 98));
let canopy = surfaces[9 * 16 + 7].surface.unwrap();
assert_eq!(
(canopy.height, canopy.state, canopy.biome),
(99, leaves, forest)
);
let underwater = surfaces[4 * 16 + 4].surface.unwrap();
assert_eq!((underwater.height, underwater.state), (55, sand));
assert_eq!(underwater.water, Some((64, water)));
assert!(surfaces[15].surface.is_none(), "saved void stays empty");
assert!(
reopened.regions.lock().unwrap().is_empty(),
"far reads do not grow gameplay region cache"
);
let mut proto = encode(&chunk, assets.registry, &biomes);
put(
&mut proto.tag,
"Status",
string(ChunkStatus::Terrain.name()),
);
put(&mut proto.tag, "xPos", Tag::Int(-3));
save.write_records("region", vec![((-3, 3), Some(proto))], true)
.unwrap();
assert!(
reopened.lod_surfaces(-3, 3, &assets, &biomes).is_none(),
"unfinished proto saves must not suppress complete decoration"
);
let mut nether = SaveDir::new(dir.clone());
nether.sky_light = false;
let dark = nether.lod_surfaces(-2, 3, &assets, &biomes).unwrap();
assert!(
dark.iter()
.flat_map(|c| &c.spans)
.all(|s| s.light & 0xf0 == 0),
"no invented skylight in Nether saves"
);
struct SavedOnly(Vec<crate::streaming::LodDetail>);
impl crate::streaming::LodSampler for SavedOnly {
fn sample(&mut self, _: i32, _: i32) -> Option<crate::lod::Surface> {
panic!("saved data must win")
}
fn request_detail(&mut self, chunks: &[(i32, i32)], _: [i32; 2]) {
assert!(
chunks.is_empty(),
"saved builds must not request speculative generation"
);
}
fn drain_details(&mut self) -> Vec<crate::streaming::LodDetail> {
std::mem::take(&mut self.0)
}
}
let inner = Arc::new(crate::testgen::TestTerrain::new(assets.registry, &biomes).unwrap());
let generator = Arc::new(Persistent {
inner,
save: Arc::new(reopened),
registry: Arc::new(assets),
biomes: Arc::new(biomes),
});
let mut sampler = SavedLodSampler {
generator,
inner: Box::new(SavedOnly(Vec::new())),
cache: Default::default(),
details: Vec::new(),
};
use crate::streaming::LodSampler;
sampler.request_detail(&[(-2, 3)], [-32, 48]);
let details = sampler.drain_details();
assert_eq!(details.len(), 1);
assert_eq!(&details[0].1[..], &surfaces[..]);
sampler.cache.clear();
let generated: LodSurfaces =
Arc::new(std::array::from_fn(|_| crate::lod::Column::default()));
sampler.inner = Box::new(SavedOnly(vec![
((-2, 3), generated.clone()),
((-9, 9), generated.clone()),
]));
let details = sampler.drain_details();
assert_eq!(
&details[0].1[..],
&surfaces[..],
"unrequested batch neighbours must preserve saved builds"
);
assert!(Arc::ptr_eq(&details[1].1, &generated));
sampler.inner =
crate::streaming::ChunkGenerator::lod_sampler(sampler.generator.inner.clone());
let grid = sampler.grid(-32, 48, 2, 16);
for z in 0..8 {
for x in 0..8 {
assert_eq!(
grid[z * 16 + x],
crate::lod::Column::reduced(&surfaces, x * 2, z * 2, 2),
"saved builds must override a batched direct grid"
);
}
}
drop((save, sampler, nether));
fs::remove_dir_all(dir).unwrap();
}
#[test]
fn saved_far_surface_rejects_invalid_packed_palettes() {
let mut states = Tag::compound([
("palette", Tag::List(vec![Tag::Byte(0), Tag::Byte(1)])),
("data", Tag::LongArray(vec![0; 256])),
]);
assert_eq!(lod_palette_index(&states, 4095, 4096, 4), Some(0));
assert_eq!(lod_palette_index(&states, 4096, 4096, 4), None);
put(&mut states, "data", Tag::LongArray(vec![15; 256]));
assert_eq!(lod_palette_index(&states, 0, 4096, 4), None);
put(&mut states, "data", Tag::LongArray(vec![0; 255]));
assert_eq!(lod_palette_index(&states, 0, 4096, 4), None);
}
#[test]
fn save_batch_preserves_order_coordinates_and_ownership() {
use crate::containers::BlockEntity;
+74 -1
View File
@@ -32,6 +32,25 @@ pub type Proto = Box<dyn std::any::Any + Send>;
pub trait ChunkGenerator: Send + Sync + 'static {
type Worker: Send + 'static;
fn worker(&self) -> Self::Worker;
fn lod_sampler(self: Arc<Self>) -> Box<dyn LodSampler>
where
Self: Sized,
{
let worker = self.worker();
Box::new(GeneratorSampler {
generator: self,
worker,
})
}
/// An undecorated far-terrain column, without allocating gameplay chunks.
fn lod_surface(
&self,
_worker: &mut Self::Worker,
_x: i32,
_z: i32,
) -> Option<crate::lod::Surface> {
None
}
/// A previously saved final chunk, bypassing generation.
fn load(&self, _x: i32, _z: i32) -> Option<Chunk> {
None
@@ -63,8 +82,58 @@ pub trait ChunkGenerator: Send + Sync + 'static {
/// neighbours' decoration is done, so it can't be generated again.
fn save_finished(&self, _chunk: Chunk) {}
}
pub type LodDetail = ((i32, i32), Arc<[crate::lod::Column; 256]>);
/// Independent, thread-owned far-terrain sampling state.
pub trait LodSampler: Send {
/// Independent sampling state; native detail generation stays with the owner.
fn fork(&self) -> Option<Box<dyn LodSampler>> {
None
}
fn request_detail(&mut self, _chunks: &[(i32, i32)], _center: [i32; 2]) {}
fn drain_details(&mut self) -> Vec<LodDetail> {
Vec::new()
}
fn sample(&mut self, x: i32, z: i32) -> Option<crate::lod::Surface>;
fn column(&mut self, x: i32, z: i32) -> crate::lod::Column {
crate::lod::Column::from_surface(self.sample(x, z))
}
fn reduced_column(&mut self, x: i32, z: i32, _width: i32) -> crate::lod::Column {
self.column(x, z)
}
fn grid(&mut self, x: i32, z: i32, stride: i32, side: usize) -> Vec<crate::lod::Column> {
let mut columns = Vec::with_capacity(side * side);
for dz in 0..side {
for dx in 0..side {
columns.push(self.reduced_column(
x + dx as i32 * stride,
z + dz as i32 * stride,
stride,
));
}
}
columns
}
}
pub trait LodSource: Send + Sync {
fn sampler(self: Arc<Self>) -> Box<dyn LodSampler>;
}
struct GeneratorSampler<G: ChunkGenerator> {
generator: Arc<G>,
worker: G::Worker,
}
impl<G: ChunkGenerator> LodSampler for GeneratorSampler<G> {
fn sample(&mut self, x: i32, z: i32) -> Option<crate::lod::Surface> {
self.generator.lod_surface(&mut self.worker, x, z)
}
}
impl<G: ChunkGenerator> LodSource for G {
fn sampler(self: Arc<Self>) -> Box<dyn LodSampler> {
ChunkGenerator::lod_sampler(self)
}
}
/// The generator's saving, for the streamer (which doesn't know its type).
trait Saves: Send + Sync {
trait Saves: LodSource {
fn save_protos(&self, protos: &[&Proto]);
fn save_finished(&self, chunk: Chunk);
}
@@ -490,6 +559,10 @@ impl Streaming {
mesh: true,
}
}
/// Shares generation resources, but gives distant work its own worker state.
pub fn lod_source(&self) -> Arc<dyn LodSource> {
self.saves.clone()
}
/// Resource packs reloaded (`LevelRenderer.allChanged`): mesh with the new
/// models and sprites, and rebuild every loaded section. Meshes in flight
/// were made with the old resources; their results are dropped.
+8 -6
View File
@@ -72,16 +72,18 @@ 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);
// mesher.rs's TerrainVertex holds the UV in fixed point; the instance's
// origin.w is the step: 1/32768 per unit for sections (a power of two on an
// integer below 2^24, so it decodes exactly), 1/32 per block for distant
// terrain tiles (TerrainVertex::tiled).
fn terrain_uv(packed: vec2<u32>, step: f32) -> vec2<f32> {
return vec2<f32>(packed) * step;
}
@vertex fn vs_terrain(
@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, terrain_uv(uv), layer, color, light, origin.xyz);
return terrain_vertex(position, terrain_uv(uv, origin.w), 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:
@@ -94,7 +96,7 @@ fn terrain_uv(packed: vec2<u32>) -> vec2<f32> {
@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, terrain_uv(uv), layer, color, light, origin.xyz);
return terrain_vertex(position, terrain_uv(uv, origin.w), 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 {
+48
View File
@@ -64,6 +64,19 @@ fn hash(x: i32, z: i32) -> u32 {
impl ChunkGenerator for TestTerrain {
type Worker = ();
fn worker(&self) {}
fn lod_surface(&self, _: &mut (), x: i32, z: i32) -> Option<crate::lod::Surface> {
let h = Self::height(x, z);
Some(crate::lod::Surface {
height: h + 1,
state: horizon_world::registry::BlockStateId(if h <= 63 {
self.sand
} else {
self.grass
}),
water: (h < 62).then_some((63, horizon_world::registry::BlockStateId(self.water))),
biome: self.plains,
})
}
fn proto(&self, w: &mut (), x: i32, z: i32) -> crate::streaming::Proto {
Box::new(self.generate(w, x, z))
}
@@ -136,3 +149,38 @@ impl TestTerrain {
chunk
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn far_surface_agrees_with_generated_land_and_water() {
let terrain = TestTerrain {
stone: 1,
dirt: 2,
grass: 3,
water: 4,
sand: 5,
bedrock: 6,
log: 7,
leaves: 8,
short_grass: 9,
plains: 10,
};
let mut wet = false;
for (cx, cz) in [(0, 0), (-7, 3), (15, -12), (-21, -8)] {
let chunk = terrain.generate(&mut (), cx, cz);
for x in 0..16 {
let surface = terrain.lod_surface(&mut (), cx * 16 + x, cz * 16).unwrap();
assert_eq!(chunk.get(x, surface.height - 1, 0), surface.state.0);
if let Some((height, state)) = surface.water {
wet = true;
assert_eq!(chunk.get(x, height - 1, 0), state.0);
assert_eq!(chunk.get(x, height, 0), 0);
}
}
}
assert!(wet, "fixture includes underwater land");
}
}
+33 -2
View File
@@ -4,7 +4,7 @@ use crate::{
biome::Biomes,
chunk::{BIOME_STRATEGY, Chunk, HEIGHT, MIN_Y, SECTIONS, Section},
save::SavedProto,
streaming::{ChunkGenerator, Proto},
streaming::{ChunkGenerator, LodSampler, Proto},
};
use horizon_core::{
paletted::PalettedContainer,
@@ -16,11 +16,15 @@ use horizon_world::{
nbt::Tag,
registry::BlockRegistry,
worldgen::{
generator::{GeneratorData, GeneratorWorker, OverworldGenerator, ProtoChunk},
generator::{
GeneratorData, GeneratorWorker, OverworldGenerator, ProtoChunk, SurfaceSample,
},
terrain::PreLightChunkStage,
},
};
use std::{path::Path, sync::Arc};
mod lod;
use lod::VanillaLodSampler;
pub struct VanillaTerrain {
generator: OverworldGenerator,
@@ -35,6 +39,8 @@ pub struct VanillaTerrain {
tick_blocks: Vec<u32>,
/// Block index -> default state id: `tick_blocks` the other way.
tick_states: Vec<u32>,
seed: i64,
dimension: String,
}
/// A proto chunk, or the position of one whose generation failed.
enum VanillaProto {
@@ -42,6 +48,18 @@ enum VanillaProto {
Failed(i32, i32),
}
impl VanillaTerrain {
fn client_surface(&self, surface: SurfaceSample) -> crate::lod::Surface {
crate::lod::Surface {
height: surface.height,
state: surface.state,
water: surface.water,
biome: self
.biome_map
.get(surface.biome.0 as usize)
.copied()
.unwrap_or(0),
}
}
pub fn new(
dir: &Path,
registry: &'static BlockRegistry,
@@ -119,6 +137,8 @@ impl VanillaTerrain {
query: std::sync::Mutex::new(None),
tick_blocks,
tick_states,
seed,
dimension: dimension.to_owned(),
})
}
/// A proto chunk's blocks, biomes, scheduled ticks and post-processing
@@ -278,6 +298,17 @@ impl ChunkGenerator for VanillaTerrain {
fn worker(&self) -> GeneratorWorker {
self.generator.worker()
}
fn lod_sampler(self: Arc<Self>) -> Box<dyn LodSampler> {
Box::new(VanillaLodSampler::new(self))
}
fn lod_surface(
&self,
worker: &mut GeneratorWorker,
x: i32,
z: i32,
) -> Option<crate::lod::Surface> {
Some(self.client_surface(worker.surface_sample(x, z)?))
}
/// The structure starts a saved chunk references, as vanilla reads them
/// with the chunk: built here, on the loading worker, into the
/// generator's cache, rather than on the game thread the first time
+700
View File
@@ -0,0 +1,700 @@
//! Speculative decoration is isolated from gameplay and never writes normal saves.
use super::*;
use crate::{
fast_hash::{FxHashMap, FxHashSet},
lod::{Column, Span, Surface},
streaming::LodDetail,
};
use horizon_world::{
heightmap::HeightmapState,
registry::BlockStateId,
worldgen::{decoration_order::vanilla_order, generator::GeneratorError},
};
use std::{
collections::VecDeque,
sync::{
Condvar, Mutex,
atomic::{AtomicBool, Ordering},
mpsc,
},
thread,
};
mod vegetation;
use vegetation::Vegetation;
const CACHE: usize = 1024;
const REQUESTS: usize = 64;
const BATCH: i32 = 4;
fn batch_origin((x, z): (i32, i32)) -> (i32, i32) {
(x.div_euclid(BATCH) * BATCH, z.div_euclid(BATCH) * BATCH)
}
#[derive(Default)]
struct Requests {
chunks: FxHashSet<(i32, i32)>,
center: [i32; 2],
current: Option<(i32, i32)>,
finished: FxHashSet<(i32, i32)>,
}
struct Shared {
requests: Mutex<Requests>,
wake: Condvar,
stop: AtomicBool,
}
fn distance((x, z): (i32, i32), center: [i32; 2]) -> i64 {
let dx = i64::from(x * 16 + 8 - center[0]);
let dz = i64::from(z * 16 + 8 - center[1]);
dx * dx + dz * dz
}
struct Builder {
shared: Arc<Shared>,
results: mpsc::Receiver<LodDetail>,
thread: Option<thread::JoinHandle<()>>,
}
impl Builder {
fn new(terrain: Arc<VanillaTerrain>) -> Self {
let shared = Arc::new(Shared {
requests: Mutex::new(Requests::default()),
wake: Condvar::new(),
stop: AtomicBool::new(false),
});
let worker = shared.clone();
let (tx, results) = mpsc::channel();
let thread = thread::Builder::new()
.name("Far decoration".into())
.spawn(move || {
// Structure pieces have mutable one-shot placement flags. Sharing
// gameplay's seeded pipeline would consume its chests/spawners.
let generator = match OverworldGenerator::for_dimension(
terrain.generator.data().clone(),
terrain.seed,
"minecraft:normal",
&terrain.dimension,
) {
Ok(generator) => generator,
Err(error) => {
log::warn!("Horizon: far decoration unavailable: {error}");
worker.stop.store(true, Ordering::Relaxed);
return;
}
};
let mut state = generator.worker();
loop {
let key = {
let mut requests = worker.requests.lock().unwrap();
while requests.chunks.is_empty() && !worker.stop.load(Ordering::Relaxed) {
requests = worker.wake.wait(requests).unwrap();
}
if worker.stop.load(Ordering::Relaxed) {
break;
}
let key = *requests
.chunks
.iter()
.min_by_key(|&&key| (distance(key, requests.center), key))
.unwrap();
let origin = batch_origin(key);
requests.chunks.retain(|&key| batch_origin(key) != origin);
requests.current = Some(origin);
origin
};
let result = decorated_batch(&mut state, key, BATCH, &worker.stop, |chunk| {
let pos = chunk.position();
let key = (pos.x, pos.z);
worker.requests.lock().unwrap().finished.insert(key);
let columns = columns(chunk, &terrain.biome_map, &terrain.dimension);
if tx.send((key, columns)).is_err() {
worker.stop.store(true, Ordering::Relaxed);
}
});
worker.requests.lock().unwrap().current = None;
match result {
Ok(_) => {}
Err(error) => {
log::warn!(
"Horizon: far decoration failed at {} {}: {error}",
key.0,
key.1
);
worker.stop.store(true, Ordering::Relaxed);
break;
}
}
}
})
.expect("far decoration worker");
Self {
shared,
results,
thread: Some(thread),
}
}
fn request(&self, chunks: &[(i32, i32)], center: [i32; 2]) {
if self.shared.stop.load(Ordering::Relaxed) {
return;
}
let mut requests = self.shared.requests.lock().unwrap();
requests.center = center;
for &key in chunks {
if requests.current != Some(batch_origin(key)) && !requests.finished.contains(&key) {
requests.chunks.insert(key);
}
}
if requests.chunks.len() > REQUESTS {
let mut sorted: Vec<_> = requests.chunks.iter().copied().collect();
sorted.sort_unstable_by_key(|&key| (distance(key, center), key));
requests.chunks = sorted.into_iter().take(REQUESTS).collect();
}
self.shared.wake.notify_one();
}
}
impl Drop for Builder {
fn drop(&mut self) {
self.shared.stop.store(true, Ordering::Relaxed);
self.shared.wake.notify_one();
if let Some(thread) = self.thread.take() {
let _ = thread.join();
}
}
}
/// Share the two-chunk terrain halo and one-chunk feature halo across adjacent
/// targets. Emit each target only after all nine feature centers have run.
/// These proto chunks never enter World, lighting or normal save queues.
fn decorated_batch(
worker: &mut GeneratorWorker,
origin: (i32, i32),
size: i32,
stop: &AtomicBool,
mut emit: impl FnMut(&ProtoChunk),
) -> Result<FxHashMap<(i32, i32), ProtoChunk>, GeneratorError> {
// Each region starts afresh; flags from a previous speculative structure
// placement must not suppress blocks in this region.
worker.forget_structure_starts();
let mut chunks = FxHashMap::default();
for dx in -2..size + 2 {
for dz in -2..size + 2 {
if stop.load(Ordering::Relaxed) {
return Ok(FxHashMap::default());
}
let key = (origin.0 + dx, origin.1 + dz);
chunks.insert(key, worker.generate_proto(ChunkPos::new(key.0, key.1))?);
}
}
let mut remaining: FxHashMap<_, u8> = (0..size)
.flat_map(|x| (0..size).map(move |z| ((origin.0 + x, origin.1 + z), 9)))
.collect();
let center = (origin.0 + size / 2, origin.1 + size / 2);
for (x, z) in vanilla_order(center, size / 2)
.into_iter()
.filter(|&(x, z)| {
x >= origin.0 - 1 && x <= origin.0 + size && z >= origin.1 - 1 && z <= origin.1 + size
})
{
if stop.load(Ordering::Relaxed) {
return Ok(FxHashMap::default());
}
let mut region: [ProtoChunk; 9] = std::array::from_fn(|i| {
chunks
.remove(&(x + i as i32 % 3 - 1, z + i as i32 / 3 - 1))
.expect("generated neighborhood")
});
worker.decorate(&mut region)?;
for chunk in region {
let pos = chunk.position();
chunks.insert((pos.x, pos.z), chunk);
}
for dx in -1..=1 {
for dz in -1..=1 {
let key = (x + dx, z + dz);
if let Some(left) = remaining.get_mut(&key) {
*left -= 1;
if *left == 0 {
emit(&chunks[&key]);
}
}
}
}
}
assert!(
remaining.values().all(|&left| left == 0),
"incomplete far decoration halo"
);
Ok(chunks)
}
#[cfg(test)]
fn decorated_region(
worker: &mut GeneratorWorker,
center: (i32, i32),
stop: &AtomicBool,
) -> Result<Option<ProtoChunk>, GeneratorError> {
Ok(decorated_batch(worker, center, 1, stop, |_| {})?.remove(&center))
}
fn columns(chunk: &ProtoChunk, biome_map: &[u16], dimension: &str) -> Arc<[Column; 256]> {
let states = chunk.data().block_states();
let registry = chunk.data().registry();
let min = chunk.terrain.min_y().max(MIN_Y);
let max = (chunk.terrain.min_y() + chunk.terrain.height()).min(MIN_Y + HEIGHT);
Arc::new(std::array::from_fn(|index| {
let x = (index & 15) as i32;
let z = (index >> 4) as i32;
let mut column = Column::default();
for y in min..max {
let state = chunk.block(x, y, z);
let properties = states.get(state);
if properties.is_air()
|| (!properties.blocks_motion_in_heightmap() && !properties.has_fluid())
{
continue;
}
let name = registry.state(state).map_or("", |s| s.name.as_str());
let fluid = matches!(name, "minecraft:water" | "minecraft:lava");
let biome = biome_map
.get(chunk.biome_id_at(x, y, z).0 as usize)
.copied()
.unwrap_or(0);
// shortcut: no propagated far lighting, use the normal lighting
// engine if accurate distant block-light propagation is required.
let light = if dimension == "minecraft:the_nether" {
0
} else {
240
} | if name == "minecraft:lava" {
15 << 12
} else {
0
};
column.push(Span {
min: y as i16,
max: (y + 1) as i16,
state,
biome,
light,
});
if fluid {
let surface = column.surface.get_or_insert(Surface {
height: MIN_Y,
state: BlockStateId(0),
water: None,
biome,
});
surface.water = Some((y + 1, state));
} else {
column.surface = Some(Surface {
height: y + 1,
state,
water: None,
biome,
});
}
}
column
}))
}
pub(super) struct VanillaLodSampler {
generator: Arc<VanillaTerrain>,
worker: GeneratorWorker,
builder: Option<Builder>,
vegetation: Arc<Vegetation>,
cache: FxHashMap<(i32, i32), Arc<[Column; 256]>>,
order: VecDeque<(i32, i32)>,
details: Vec<LodDetail>,
}
impl VanillaLodSampler {
pub(super) fn new(generator: Arc<VanillaTerrain>) -> Self {
let worker = generator.generator.worker();
let builder = Some(Builder::new(generator.clone()));
let vegetation = Arc::new(Vegetation::new(
generator.generator.data(),
&generator.biome_map,
generator.seed,
));
Self {
generator,
worker,
builder,
vegetation,
cache: FxHashMap::default(),
order: VecDeque::new(),
details: Vec::new(),
}
}
}
impl LodSampler for VanillaLodSampler {
fn fork(&self) -> Option<Box<dyn LodSampler>> {
Some(Box::new(Self {
generator: self.generator.clone(),
worker: self.generator.generator.worker(),
builder: None,
vegetation: self.vegetation.clone(),
cache: FxHashMap::default(),
order: VecDeque::new(),
details: Vec::new(),
}))
}
fn request_detail(&mut self, chunks: &[(i32, i32)], center: [i32; 2]) {
let mut missing = Vec::new();
for &key in chunks {
if let Some(columns) = self.cache.get(&key) {
self.details.push((key, columns.clone()));
} else {
missing.push(key);
}
}
if let Some(builder) = &self.builder {
builder.request(&missing, center);
}
}
fn drain_details(&mut self) -> Vec<LodDetail> {
let Some(builder) = &self.builder else {
return std::mem::take(&mut self.details);
};
while let Ok((key, columns)) = builder.results.try_recv() {
if !self.cache.contains_key(&key) {
self.order.push_back(key);
}
self.cache.insert(key, columns.clone());
while self.cache.len() > CACHE {
if let Some(old) = self.order.pop_front() {
self.cache.remove(&old);
builder
.shared
.requests
.lock()
.unwrap()
.finished
.remove(&old);
}
}
self.details.push((key, columns));
}
std::mem::take(&mut self.details)
}
fn sample(&mut self, x: i32, z: i32) -> Option<Surface> {
self.column(x, z).surface
}
fn column(&mut self, x: i32, z: i32) -> Column {
self.reduced_column(x, z, 1)
}
fn reduced_column(&mut self, x: i32, z: i32, width: i32) -> Column {
if width <= 16
&& let Some(columns) = self.cache.get(&(x >> 4, z >> 4))
{
return Column::reduced(
columns,
(x & 15) as usize,
(z & 15) as usize,
width as usize,
);
}
self.grid(x, z, width, 1).pop().unwrap_or_default()
}
fn grid(&mut self, x: i32, z: i32, width: i32, side: usize) -> Vec<Column> {
let samples = match self
.worker
.lod_grid(x + width / 2, z + width / 2, width, side)
{
Ok(samples) => samples,
Err(error) => {
log::warn!("Horizon: direct far sampling failed: {error}");
return vec![Column::default(); side * side];
}
};
samples
.into_iter()
.enumerate()
.map(|(index, (surface, spans))| {
let (wx, wz) = (
x + (index % side) as i32 * width,
z + (index / side) as i32 * width,
);
if width <= 16
&& let Some(columns) = self.cache.get(&(wx >> 4, wz >> 4))
{
return Column::reduced(
columns,
(wx & 15) as usize,
(wz & 15) as usize,
width as usize,
);
}
self.preview_column(wx + width / 2, wz + width / 2, width, surface, spans)
})
.collect()
}
}
impl VanillaLodSampler {
fn preview_column(
&self,
x: i32,
z: i32,
width: i32,
surface: Option<SurfaceSample>,
spans: Vec<(i32, i32, BlockStateId)>,
) -> Column {
let surface = surface.map(|sample| self.generator.client_surface(sample));
let biome = surface.map_or(0, |surface| surface.biome);
let registry = self.generator.generator.data().registry();
let mut column = Column {
surface,
spans: Vec::new(),
};
for (min, max, state) in spans.into_iter().rev() {
column.push(Span {
min: min as i16,
max: max as i16,
state,
biome,
light: if self.generator.dimension == "minecraft:the_nether" {
0
} else {
240
} | if registry
.state(state)
.is_some_and(|s| s.name == "minecraft:lava")
{
15 << 12
} else {
0
},
});
}
let light = if self.generator.dimension == "minecraft:the_nether" {
0
} else {
240
};
self.vegetation.apply(&mut column, x, z, width, light);
column
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore = "requires prepared worldgen data in HORIZON_TEST_RESOURCES"]
fn decorated_lod_refines_real_seed_canopies_and_all_dimensions() -> crate::Result<()> {
let root = std::path::PathBuf::from(
std::env::var_os("HORIZON_TEST_RESOURCES").expect("resources"),
);
let assets = crate::assets::Assets::load(&root)?;
let biomes = crate::biome::Biomes::load(&root)?;
let terrain = Arc::new(VanillaTerrain::new(
&root.join("worldgen"),
assets.registry,
&biomes,
1234,
)?);
let mut sampler = VanillaLodSampler::new(terrain.clone());
let mut fork = sampler.fork().expect("independent far sampler");
let direct = sampler.grid(-1024, 0, 32, 16);
assert_eq!(
direct,
fork.grid(-1024, 0, 32, 16),
"sampling workers must agree on seed/biome vegetation"
);
let grass = assets
.registry
.default_state("minecraft:grass_block")
.unwrap();
let planted = |biome, width, x, z| {
let mut column = Column::from_surface(Some(Surface {
height: 80,
state: grass,
water: None,
biome,
}));
sampler.vegetation.apply(&mut column, x, z, width, 240);
column
};
let forest = biomes.id("minecraft:forest");
let plains = biomes.id("minecraft:plains");
let leaves = |column: &Column| {
column.spans.iter().any(|span| {
assets
.registry
.state(span.state)
.unwrap()
.name
.ends_with("_leaves")
})
};
let mut forest_count = 0;
let mut plains_count = 0;
for z in 0..64 {
for x in 0..64 {
forest_count += usize::from(leaves(&planted(forest, 1, x, z)));
plains_count += usize::from(leaves(&planted(plains, 1, x, z)));
}
}
assert!(
forest_count > 1000 && forest_count > plains_count * 4,
"feature-derived vegetation density: forest={forest_count}, plains={plains_count}"
);
for width in [16, 32, 128, 512, 1024] {
assert!(
leaves(&planted(forest, width, 0, 0)),
"forest canopy missing at scale {width}"
);
}
let key = (-64, 0);
sampler.request_detail(&[key], [-1024, 0]);
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30);
let generated = loop {
if let Some((_, columns)) = sampler.drain_details().into_iter().find(|(k, _)| *k == key)
{
break columns;
}
assert!(
std::time::Instant::now() < deadline,
"decoration worker stalled"
);
std::thread::sleep(std::time::Duration::from_millis(5));
};
let count = |suffix: &str| {
generated
.iter()
.flat_map(|column| &column.spans)
.filter(|span| {
assets
.registry
.state(span.state)
.unwrap()
.name
.ends_with(suffix)
})
.map(|span| i32::from(span.max - span.min))
.sum::<i32>()
};
assert!(count("_leaves") > 0, "unexplored forest has a real canopy");
assert!(count("_log") > 0, "canopy retains its trunks");
assert!(generated.iter().all(|column| {
column
.spans
.windows(2)
.all(|pair| pair[0].max <= pair[1].min)
}));
for z in 0..16 {
for x in 0..16 {
assert_eq!(
sampler.column(key.0 * 16 + x, key.1 * 16 + z),
generated[(z * 16 + x) as usize]
);
}
}
sampler.request_detail(&[key], [-1024, 0]);
let cached = sampler.drain_details();
assert!(
Arc::ptr_eq(&cached[0].1, &generated),
"reuses completed compact data"
);
drop(sampler);
let mut worker = terrain.generator.worker();
let started = std::time::Instant::now();
for x in 0..BATCH {
for z in 0..BATCH {
decorated_region(&mut worker, (key.0 + x, key.1 + z), &AtomicBool::new(false))?
.unwrap();
}
}
let separate = started.elapsed();
let started = std::time::Instant::now();
let mut emitted = FxHashMap::default();
let region = decorated_batch(&mut worker, key, BATCH, &AtomicBool::new(false), |chunk| {
let pos = chunk.position();
assert!(
emitted
.insert(
(pos.x, pos.z),
columns(chunk, &terrain.biome_map, "minecraft:overworld")
)
.is_none()
);
})?;
eprintln!(
"far generation: {} separate targets={separate:?}, shared targets={:?}",
emitted.len(),
started.elapsed()
);
assert_eq!(emitted.len(), (BATCH * BATCH) as usize);
for (key, data) in emitted {
assert_eq!(
data,
columns(&region[&key], &terrain.biome_map, "minecraft:overworld"),
"emitted before all neighbouring features completed"
);
}
assert_eq!(batch_origin((-1, -5)), (-4, -8));
let stop = AtomicBool::new(true);
assert!(decorated_region(&mut terrain.generator.worker(), key, &stop)?.is_none());
for (dimension, center) in [
("minecraft:the_nether", (0, 0)),
("minecraft:the_end", (16, 0)),
] {
let generator = OverworldGenerator::for_dimension(
terrain.generator.data().clone(),
1234,
"minecraft:normal",
dimension,
)?;
let mut emitted = 0;
let mut region = decorated_batch(
&mut generator.worker(),
center,
BATCH,
&AtomicBool::new(false),
|_| emitted += 1,
)?;
assert_eq!(emitted, (BATCH * BATCH) as usize);
let chunk = region.remove(&center).unwrap();
let data = columns(&chunk, &terrain.biome_map, dimension);
if dimension == "minecraft:the_nether" {
assert!(
data.iter()
.flat_map(|c| &c.spans)
.all(|s| s.light & 0xf0 == 0)
);
assert!(data.iter().flat_map(|c| &c.spans).any(|s| {
assets.registry.state(s.state).unwrap().name == "minecraft:lava"
&& s.light >> 12 == 15
}));
} else {
assert!(
data.iter().all(|c| c.spans.is_empty()),
"End void stays empty"
);
}
}
// Reuse the saved-proto bastion fixture. Repeated speculative regions
// must reconstruct structures after their one-shot placement flags ran.
let generator = OverworldGenerator::for_dimension(
terrain.generator.data().clone(),
-1860902098141295193,
"minecraft:normal",
"minecraft:the_nether",
)?;
let mut worker = generator.worker();
let mut previous = None;
for _ in 0..2 {
let chunk = decorated_region(&mut worker, (3, 7), &AtomicBool::new(false))?.unwrap();
assert!(
chunk
.structures()
.iter()
.any(|(name, _, _, _)| name.contains("bastion"))
);
let data = columns(&chunk, &terrain.biome_map, "minecraft:the_nether");
let snapshot = (data, chunk.block_entities);
if let Some(previous) = &previous {
assert_eq!(
&snapshot, previous,
"speculative structure state leaked between regions"
);
}
previous = Some(snapshot);
}
Ok(())
}
}
@@ -0,0 +1,309 @@
//! Seed-stable visual vegetation from the world's own feature data.
//! shortcut: these positions approximate native placement; saved/visited and
//! completed native chunks replace them rather than changing gameplay worldgen.
use crate::{
fast_hash::FxHashMap,
lod::{Column, Span, Surface},
};
use horizon_world::{
registry::{BlockRegistry, BlockStateId},
worldgen::generator::GeneratorData,
};
use serde_json::Value;
#[derive(Clone, Copy)]
struct Tree {
leaves: BlockStateId,
log: BlockStateId,
height: i32,
variation: i32,
radius: i32,
count: f64,
}
pub(super) struct Vegetation {
trees: FxHashMap<u16, Vec<Tree>>,
seed: u64,
registry: &'static BlockRegistry,
}
fn mean(v: &Value) -> f64 {
if let Some(n) = v.as_f64() {
return n;
}
let distribution = v["distribution"].as_array();
if let Some(entries) = distribution {
let total: f64 = entries
.iter()
.map(|e| e["weight"].as_f64().unwrap_or(0.))
.sum();
if total > 0. {
return entries
.iter()
.map(|e| mean(&e["data"]) * e["weight"].as_f64().unwrap_or(0.))
.sum::<f64>()
/ total;
}
}
(v["min_inclusive"].as_f64().unwrap_or(0.) + v["max_inclusive"].as_f64().unwrap_or(0.)) * 0.5
}
fn block(v: &Value, registry: &BlockRegistry) -> Option<BlockStateId> {
let name = v
.as_str()
.or_else(|| v["id"].as_str())
.or_else(|| v["Name"].as_str())?;
registry.default_state(name)
}
fn gather(
data: &GeneratorData,
value: &Value,
placed: bool,
count: f64,
depth: usize,
out: &mut Vec<Tree>,
) {
if depth > 12 || count <= 0. {
return;
}
if let Some(name) = value.as_str() {
let json = if placed {
data.placed_feature_json(name)
} else {
data.configured_feature_json(name)
};
if let Some(json) = json.and_then(|s| serde_json::from_str::<Value>(s).ok()) {
gather(data, &json, placed, count, depth + 1, out);
}
return;
}
if placed {
let mut count = count;
if let Some(modifiers) = value["placement"].as_array() {
for modifier in modifiers {
match modifier["type"]
.as_str()
.unwrap_or("")
.strip_prefix("minecraft:")
.unwrap_or("")
{
"count" | "count_on_every_layer" => count *= mean(&modifier["count"]),
"rarity_filter" => count /= modifier["chance"].as_f64().unwrap_or(1.).max(1.),
"count_extra" => {
count *= modifier["count"].as_f64().unwrap_or(0.)
+ modifier["extra_count"].as_f64().unwrap_or(0.)
* modifier["extra_chance"].as_f64().unwrap_or(0.)
}
_ => {}
}
}
}
gather(data, &value["feature"], false, count, depth + 1, out);
return;
}
let config = if value["config"].is_object() {
&value["config"]
} else {
value
};
match value["type"].as_str().unwrap_or("") {
"minecraft:tree" => {
let (Some(leaves), Some(log)) = (
block(&config["foliage_provider"], data.registry()),
block(&config["trunk_provider"], data.registry()),
) else {
return;
};
let trunk = &config["trunk_placer"];
out.push(Tree {
leaves,
log,
height: trunk["base_height"].as_i64().unwrap_or(5) as i32,
variation: (trunk["height_rand_a"].as_i64().unwrap_or(0)
+ trunk["height_rand_b"].as_i64().unwrap_or(0))
as i32,
radius: mean(&config["foliage_placer"]["radius"])
.round()
.clamp(2., 6.) as i32,
count,
});
}
"minecraft:random_selector" => {
let mut remaining = count;
if let Some(features) = config["features"].as_array() {
for entry in features {
let chance = entry["chance"].as_f64().unwrap_or(0.).clamp(0., 1.);
gather(
data,
&entry["feature"],
true,
remaining * chance,
depth + 1,
out,
);
remaining *= 1. - chance;
}
}
gather(data, &config["default"], true, remaining, depth + 1, out);
}
"minecraft:random_boolean_selector" => {
for key in ["feature_true", "feature_false"] {
gather(data, &config[key], true, count * 0.5, depth + 1, out);
}
}
"minecraft:simple_random_selector" => {
if let Some(features) = config["features"].as_array() {
for feature in features {
gather(
data,
feature,
true,
count / features.len().max(1) as f64,
depth + 1,
out,
);
}
}
}
_ => {}
}
}
fn hash(seed: u64, x: i32, z: i32) -> u64 {
let mut n = seed
^ (x as u64).wrapping_mul(0x9e3779b97f4a7c15)
^ (z as u64).wrapping_mul(0xd1b54a32d192ed03);
n = (n ^ n >> 30).wrapping_mul(0xbf58476d1ce4e5b9);
n = (n ^ n >> 27).wrapping_mul(0x94d049bb133111eb);
n ^ n >> 31
}
impl Vegetation {
pub(super) fn new(data: &GeneratorData, map: &[u16], seed: i64) -> Self {
let mut trees = FxHashMap::default();
for (index, &biome) in map.iter().enumerate() {
let Ok(json) = serde_json::from_str::<Value>(
data.biome_json(horizon_world::worldgen::biome::BiomeId(index as u32)),
) else {
continue;
};
let mut list = Vec::new();
if let Some(steps) = json["features"].as_array() {
for step in steps {
if let Some(features) = step.as_array() {
for feature in features {
gather(data, feature, true, 1., 0, &mut list);
}
}
}
}
if !list.is_empty() {
trees.insert(biome, list);
}
}
Self {
trees,
seed: seed as u64,
registry: data.registry(),
}
}
pub(super) fn apply(&self, column: &mut Column, x: i32, z: i32, width: i32, light: u16) {
let Some(surface) = column.surface else {
return;
};
if surface.water.is_some_and(|(y, _)| y >= surface.height) {
return;
}
let ground = self
.registry
.state(surface.state)
.map_or("", |s| s.name.as_str());
if !matches!(
ground,
"minecraft:grass_block"
| "minecraft:dirt"
| "minecraft:coarse_dirt"
| "minecraft:podzol"
| "minecraft:mycelium"
| "minecraft:rooted_dirt"
| "minecraft:moss_block"
) {
return;
}
let Some(trees) = self.trees.get(&surface.biome) else {
return;
};
let total: f64 = trees.iter().map(|tree| tree.count).sum();
let choose = |n: u64| {
let mut value = (n as u32 as f64 / u32::MAX as f64) * total;
for tree in trees {
value -= tree.count;
if value <= 0. {
return tree;
}
}
&trees[trees.len() - 1]
};
let mut best = None;
if width >= 16 {
let n = hash(self.seed, x.div_euclid(4), z.div_euclid(4));
let tree = choose(n);
let coverage = 1.
- (-total * f64::from(tree.radius * tree.radius) * std::f64::consts::PI / 256.)
.exp();
if coverage >= 0.25 {
let height = tree.height + ((n >> 32) & 1) as i32;
best = Some((
surface.height + height.max(4),
surface.height + height.max(4) - tree.radius * 2,
*tree,
false,
));
}
} else {
let radius = trees.iter().map(|t| t.radius).max().unwrap_or(2);
for cz in (z - radius).div_euclid(4)..=(z + radius).div_euclid(4) {
for cx in (x - radius).div_euclid(4)..=(x + radius).div_euclid(4) {
let n = hash(self.seed, cx, cz);
if (n as u32 as f64 / u32::MAX as f64) >= (total / 16.).min(1.) {
continue;
}
let tree = *choose(n.rotate_left(21));
let tx = cx * 4 + ((n >> 32) & 3) as i32;
let tz = cz * 4 + ((n >> 34) & 3) as i32;
let d2 = (x - tx).pow(2) + (z - tz).pow(2);
if d2 > tree.radius.pow(2) {
continue;
}
let height =
tree.height + ((n >> 40) % (tree.variation.max(0) + 1) as u64) as i32;
let cap = ((tree.radius.pow(2) - d2) as f32).sqrt().ceil() as i32;
let top = surface.height + height + cap.min(2);
let bottom = surface.height + height - tree.radius;
if best.as_ref().is_none_or(|&(y, _, _, _)| top > y) {
best = Some((top, bottom, tree, x == tx && z == tz));
}
}
}
}
if let Some((top, bottom, tree, trunk)) = best {
let bottom = bottom.max(surface.height + 1);
if trunk {
column.push(Span {
min: surface.height as i16,
max: bottom as i16,
state: tree.log,
biome: surface.biome,
light,
});
}
column.push(Span {
min: bottom as i16,
max: top.max(bottom + 1) as i16,
state: tree.leaves,
biome: surface.biome,
light,
});
column.surface = Some(Surface {
height: top.max(bottom + 1),
state: tree.leaves,
..surface
});
}
}
}
+269 -9
View File
@@ -75,9 +75,12 @@ pub enum UnaryOp {
max: f32,
},
}
impl UnaryOp {
pub(super) fn apply(self, value: f32, bulk: bool) -> f32 {
match self {
// One expression definition for scalar dispatch and specialized bulk loops.
macro_rules! unary_apply {
($operation:expr, $value:expr, $bulk:expr) => {{
let value = $value;
let bulk = $bulk;
match $operation {
Self::Abs => value.abs(),
Self::Negate => -value,
Self::Square => value * value,
@@ -137,6 +140,85 @@ impl UnaryOp {
}
}
}
}};
}
impl UnaryOp {
pub(super) fn apply(self, value: f32, bulk: bool) -> f32 {
unary_apply!(self, value, bulk)
}
/// Dispatch once per volume so each loop has one fixed arithmetic operation.
/// Keep the scalar expression shared, including the bulk min/max behavior.
fn apply_bulk(self, buffer: &mut [f32]) {
// Specialization can reverse which NaN operand supplies the payload.
// Retain the original dispatch for non-finite constant operands.
let non_finite = match self {
Self::PowerBase(value) | Self::PowerExponent(value) => !value.is_finite(),
Self::LeakyRelu(value)
| Self::Add(value)
| Self::SubtractFrom(value)
| Self::Multiply(value)
| Self::DivideInto(value)
| Self::Minimum(value)
| Self::Maximum(value) => !value.is_finite(),
Self::Clamp { min, max } => !min.is_finite() || !max.is_finite(),
Self::ConstantRangeChoice {
min_inclusive,
max_exclusive,
inside,
outside,
} => {
!min_inclusive.is_finite()
|| !max_exclusive.is_finite()
|| !inside.is_finite()
|| !outside.is_finite()
}
_ => false,
};
if non_finite {
for value in buffer {
*value = self.apply(*value, true);
}
return;
}
macro_rules! apply {
($operation:expr) => {
for value in buffer {
*value = unary_apply!($operation, *value, true);
}
};
}
match self {
Self::Abs => apply!(Self::Abs),
Self::Negate => apply!(Self::Negate),
Self::Square => apply!(Self::Square),
Self::Cube => apply!(Self::Cube),
Self::Sqrt => apply!(Self::Sqrt),
Self::Log => apply!(Self::Log),
Self::PowerBase(base) => apply!(Self::PowerBase(base)),
Self::PowerExponent(exponent) => apply!(Self::PowerExponent(exponent)),
Self::Reciprocal => apply!(Self::Reciprocal),
Self::Sign => apply!(Self::Sign),
Self::Squeeze => apply!(Self::Squeeze),
Self::LeakyRelu(factor) => apply!(Self::LeakyRelu(factor)),
Self::Add(right) => apply!(Self::Add(right)),
Self::SubtractFrom(left) => apply!(Self::SubtractFrom(left)),
Self::Multiply(right) => apply!(Self::Multiply(right)),
Self::DivideInto(left) => apply!(Self::DivideInto(left)),
Self::Minimum(right) => apply!(Self::Minimum(right)),
Self::Maximum(right) => apply!(Self::Maximum(right)),
Self::ConstantRangeChoice {
min_inclusive,
max_exclusive,
inside,
outside,
} => apply!(Self::ConstantRangeChoice {
min_inclusive,
max_exclusive,
inside,
outside
}),
Self::Clamp { min, max } => apply!(Self::Clamp { min, max }),
}
}
}
#[derive(Clone, Debug)]
@@ -162,9 +244,7 @@ impl DensitySampler for UnarySampler {
) -> Result<(), SampleError> {
check_buffer(buffer, volume)?;
self.input.sample_volume(context, buffer, volume)?;
for value in buffer {
*value = self.operation.apply(*value, true);
}
self.operation.apply_bulk(buffer);
Ok(())
}
}
@@ -203,6 +283,23 @@ impl BinaryOp {
}
}
}
fn apply_bulk(self, buffer: &mut [f32], right: &[f32]) {
macro_rules! apply {
($operation:expr) => {
for (left, &right) in buffer.iter_mut().zip(right) {
*left = ($operation)(*left, right);
}
};
}
match self {
Self::Add => apply!(|left, right| left + right),
Self::Subtract => apply!(|left, right| left - right),
Self::Multiply => apply!(|left, right| left * right),
Self::Divide => apply!(|left, right| left / right),
Self::Minimum { .. } => apply!(|left, right| if right < left { right } else { left }),
Self::Maximum { .. } => apply!(|left, right| if right > left { right } else { left }),
}
}
}
#[derive(Clone, Debug)]
pub struct BinarySampler {
@@ -239,10 +336,173 @@ impl DensitySampler for BinarySampler {
self.left.sample_volume(context, buffer, volume)?;
context.with_buffer(volume.size(), |context, right| {
self.right.sample_volume(context, right, volume)?;
for (left, right) in buffer.iter_mut().zip(right) {
*left = self.operation.apply(*left, *right, true);
}
self.operation.apply_bulk(buffer, right);
Ok(())
})
}
}
#[cfg(test)]
mod bulk_tests {
use super::*;
#[inline(never)]
fn dispatched_reference(operation: UnaryOp, value: f32) -> f32 {
operation.apply(value, true)
}
#[test]
fn fixed_operation_loops_preserve_float_bits() {
let mut operations = vec![
UnaryOp::Abs,
UnaryOp::Negate,
UnaryOp::Square,
UnaryOp::Cube,
UnaryOp::Sqrt,
UnaryOp::Log,
UnaryOp::Reciprocal,
UnaryOp::Sign,
UnaryOp::Squeeze,
];
for factor in [0.0, -0.0, 0.5, -1.0, f32::INFINITY, f32::NAN] {
operations.extend([
UnaryOp::LeakyRelu(factor),
UnaryOp::Add(factor),
UnaryOp::SubtractFrom(factor),
UnaryOp::Multiply(factor),
UnaryOp::DivideInto(factor),
UnaryOp::Minimum(factor),
UnaryOp::Maximum(factor),
UnaryOp::Clamp {
min: -factor,
max: factor,
},
UnaryOp::ConstantRangeChoice {
min_inclusive: -factor,
max_exclusive: factor,
inside: -0.0,
outside: 17.0,
},
]);
}
for exponent in [0.0, -0.0, 0.5, -1.0, 2.0, 3.0, f64::INFINITY, f64::NAN] {
operations.extend([
UnaryOp::PowerBase(exponent),
UnaryOp::PowerExponent(exponent),
]);
}
let mut values: Vec<f32> = [
0,
0x8000_0000,
1,
0x8000_0001,
0x007f_ffff,
0x0080_0000,
0x3f00_0000,
0xbf00_0000,
0x3f80_0000,
0xbf80_0000,
0x7f7f_ffff,
0xff7f_ffff,
0x7f80_0000,
0xff80_0000,
0x7fc0_0000,
0x7fa0_0001,
0xffc0_1234,
]
.into_iter()
.map(f32::from_bits)
.collect();
let mut bits = 0x484f_5249u32;
for _ in 0..4096 {
bits ^= bits << 13;
bits ^= bits >> 17;
bits ^= bits << 5;
values.push(f32::from_bits(bits));
}
for operation in operations {
for length in [0, 1, 3, 16, 257, values.len()] {
let mut actual = values[..length].to_vec();
operation.apply_bulk(&mut actual);
for (i, (&input, actual)) in values.iter().zip(actual).enumerate() {
let expected = dispatched_reference(operation, input);
assert_eq!(
actual.to_bits(),
expected.to_bits(),
"{operation:?}, index {i}, input {input:?}"
);
}
}
}
}
#[inline(never)]
fn dispatched_binary_reference(operation: BinaryOp, left: f32, right: f32) -> f32 {
operation.apply(left, right, true)
}
#[test]
fn fixed_binary_loops_preserve_float_bits() {
let edges = [
0,
0x8000_0000,
1,
0x8000_0001,
0x007f_ffff,
0x0080_0000,
0x3f80_0000,
0xbf80_0000,
0x7f7f_ffff,
0xff7f_ffff,
0x7f80_0000,
0xff80_0000,
0x7fc0_0000,
0x7fa0_0001,
0xffc0_1234,
];
let mut left = Vec::new();
let mut right = Vec::new();
for a in edges {
for b in edges {
left.push(f32::from_bits(a));
right.push(f32::from_bits(b));
}
}
let mut bits = 0x484f_5249u32;
for _ in 0..8192 {
bits ^= bits << 13;
bits ^= bits >> 17;
bits ^= bits << 5;
left.push(f32::from_bits(bits));
bits ^= bits << 13;
bits ^= bits >> 17;
bits ^= bits << 5;
right.push(f32::from_bits(bits));
}
for operation in [
BinaryOp::Add,
BinaryOp::Subtract,
BinaryOp::Multiply,
BinaryOp::Divide,
BinaryOp::Minimum {
right_min: f32::NEG_INFINITY,
},
BinaryOp::Maximum {
right_max: f32::INFINITY,
},
] {
for length in [0, 1, 3, 16, 257, left.len()] {
let mut actual = left[..length].to_vec();
operation.apply_bulk(&mut actual, &right[..length]);
for (i, actual) in actual.into_iter().enumerate() {
let expected = dispatched_binary_reference(operation, left[i], right[i]);
assert_eq!(
actual.to_bits(),
expected.to_bits(),
"{operation:?}, index {i}"
);
}
}
}
}
}
+179 -6
View File
@@ -2,7 +2,7 @@
//! Discovery/filtering of live structure starts is a separate world subsystem.
use super::{DensitySampler, SampleError, SamplerContext, check_buffer};
use crate::noise::DensityVolume;
use std::sync::OnceLock;
use std::sync::{Arc, OnceLock};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct StructureBounds {
@@ -52,10 +52,93 @@ pub struct JigsawJunction {
pub source_z: i32,
}
/// A structure library's terrain kernel: YUNG's API's and MoogsStructureLib's
/// enhanced terrain adaptation (EnhancedTerrainAdaptation.computeDensityFactor).
/// Like vanilla's beard, but with its own size and falloff, and an action
/// above the piece's floor and one below it: bury adds terrain, carve takes
/// it away.
#[derive(Debug)]
pub struct TerrainKernel {
size: i32,
/// Density sign above the floor and below it: 1 buries, -1 carves, 0 none.
top: i32,
bottom: i32,
weights: Vec<f32>,
}
impl TerrainKernel {
/// A kernel `size` blocks across whose weights fall off as
/// e^(-d²/`distance`). Size 0 is the libraries' NONE: no density.
pub fn new(size: i32, distance: i32, top: i32, bottom: i32) -> Self {
let size = size.max(0);
let radius = size / 2;
let mut weights = vec![0.; (size as usize).pow(3)];
for x in 0..size {
for y in 0..size {
for z in 0..size {
let (kx, ky, kz) = (
f64::from(x - radius),
f64::from(y - radius) + 0.5,
f64::from(z - radius),
);
let squared = kx * kx + ky * ky + kz * kz;
weights[((z * size + x) * size + y) as usize] =
crate::jmath::pow(std::f64::consts::E, -squared / f64::from(distance))
as f32;
}
}
}
Self {
size,
top,
bottom,
weights,
}
}
pub fn radius(&self) -> i32 {
self.size / 2
}
/// The libraries' NONE: no kernel at all.
pub fn is_none(&self) -> bool {
self.size == 0
}
/// The density at (`dx`, `dy`, `dz`) from the nearest point of a piece
/// (or a junction), `floor` blocks above the piece's floor.
fn factor(&self, dx: i32, dy: i32, dz: i32, floor: i32) -> f64 {
let radius = self.radius();
let k = [dx, dy, dz].map(|v| v.wrapping_add(radius));
if k.iter().any(|v| !(0..self.size).contains(v)) {
return 0.;
}
let weight = self.weights[((k[2] * self.size + k[0]) * self.size + k[1]) as usize];
let floor = f64::from(floor) + 0.5;
let squared = f64::from(dx) * f64::from(dx) + floor * floor + f64::from(dz) * f64::from(dz);
// Mth.invSqrt(double): 1 / sqrt, not vanilla beard's fast estimate.
let multiplier = (floor * (1. / (squared / 2.).sqrt()) / 2.).abs();
let sign = if floor > 0. { self.top } else { self.bottom };
multiplier * f64::from(weight) * f64::from(sign)
}
}
/// A rigid piece's box under a terrain kernel (already moved, padded or cut
/// to a band as the piece's adaptation says).
#[derive(Clone, Debug)]
pub struct KernelPiece {
pub bounds: StructureBounds,
pub kernel: Arc<TerrainKernel>,
}
/// A jigsaw junction under a terrain kernel; `source` holds the ground
/// height with the adaptation's offset.
#[derive(Clone, Debug)]
pub struct KernelJunction {
pub source: [i32; 3],
pub kernel: Arc<TerrainKernel>,
}
#[derive(Clone, Debug, Default)]
pub struct Beardifier {
pieces: Vec<RigidPiece>,
junctions: Vec<JigsawJunction>,
kernel_pieces: Vec<KernelPiece>,
kernel_junctions: Vec<KernelJunction>,
affected: Option<StructureBounds>,
}
impl Beardifier {
@@ -70,13 +153,29 @@ impl Beardifier {
pieces,
junctions,
affected,
..Self::default()
}
}
/// Adds terrain kernels' pieces and junctions, summed after vanilla's
/// density in double precision as the libraries' Beardifier mixins do.
/// `affected` must cover them.
pub fn with_kernels(
mut self,
pieces: Vec<KernelPiece>,
junctions: Vec<KernelJunction>,
) -> Self {
self.kernel_pieces = pieces;
self.kernel_junctions = junctions;
self
}
pub fn affected_bounds(&self) -> Option<StructureBounds> {
self.affected
}
fn sample_unchecked(&self, [x, y, z]: [i32; 3]) -> f32 {
let mut value = 0.;
// The kernel is a process-wide constant; resolving it once keeps the
// per-piece and per-junction beard evaluations off the OnceLock.
let kernel = kernel();
for piece in &self.pieces {
let low = piece.bounds.minimum;
let high = piece.bounds.maximum;
@@ -98,7 +197,7 @@ impl Beardifier {
TerrainAdjustment::None => 0.,
TerrainAdjustment::Bury => bury(dx as f32, distance_y as f32 / 2., dz as f32),
TerrainAdjustment::BeardThin | TerrainAdjustment::BeardBox => {
beard(dx, distance_y, dz, dy) * 0.8
beard(kernel, dx, distance_y, dz, dy) * 0.8
}
TerrainAdjustment::Encapsulate => {
bury(dx as f32 / 2., distance_y as f32 / 2., dz as f32 / 2.) * 0.8
@@ -109,9 +208,32 @@ impl Beardifier {
let dx = x.wrapping_sub(junction.source_x);
let dy = y.wrapping_sub(junction.source_ground_y);
let dz = z.wrapping_sub(junction.source_z);
value += beard(dx, dy, dz, dy) * 0.4;
value += beard(kernel, dx, dy, dz, dy) * 0.4;
}
value
if self.kernel_pieces.is_empty() && self.kernel_junctions.is_empty() {
return value;
}
let mut density = f64::from(value);
for piece in &self.kernel_pieces {
let (low, high) = (piece.bounds.minimum, piece.bounds.maximum);
let d: [i32; 3] = std::array::from_fn(|i| {
0.max(
low[i]
.wrapping_sub([x, y, z][i])
.max([x, y, z][i].wrapping_sub(high[i])),
)
});
density += piece
.kernel
.factor(d[0], d[1], d[2], y.wrapping_sub(low[1]))
* 0.8;
}
for junction in &self.kernel_junctions {
let d: [i32; 3] =
std::array::from_fn(|i| [x, y, z][i].wrapping_sub(junction.source[i]));
density += junction.kernel.factor(d[0], d[1], d[2], d[1]) * 0.4;
}
density as f32
}
}
impl DensitySampler for Beardifier {
@@ -184,7 +306,7 @@ fn bury(x: f32, y: f32, z: f32) -> f32 {
1. - squared.sqrt() / 6.
}
}
fn beard(x: i32, y: i32, z: i32, ground_delta: i32) -> f32 {
fn beard(kernel: &[f32; 13824], x: i32, y: i32, z: i32, ground_delta: i32) -> f32 {
let indices = [x, y, z].map(|v| v.wrapping_add(12));
if indices.iter().any(|v| !(0..24).contains(v)) {
return 0.;
@@ -192,7 +314,7 @@ fn beard(x: i32, y: i32, z: i32, ground_delta: i32) -> f32 {
let vertical = ground_delta as f32 + 0.5;
let squared = (x as f32 * x as f32 + vertical * vertical) + z as f32 * z as f32;
let factor = (-vertical * fast_inverse_sqrt(f64::from(squared / 2.)) as f32) / 2.;
factor * kernel()[((indices[2] * 24 + indices[0]) * 24 + indices[1]) as usize]
factor * kernel[((indices[2] * 24 + indices[0]) * 24 + indices[1]) as usize]
}
fn fast_inverse_sqrt(value: f64) -> f64 {
let half = 0.5 * value;
@@ -212,3 +334,54 @@ fn kernel() -> &'static [f32; 13824] {
})
})
}
#[cfg(test)]
mod tests {
use super::*;
fn sample(beardifier: &Beardifier, position: [i32; 3]) -> f32 {
beardifier
.sample_value(&mut SamplerContext::default(), position)
.unwrap()
}
/// YUNG's carved top (kernel 24, distance 24, carve above, nothing
/// below) around a 5-block cube, values worked out from
/// EnhancedTerrainAdaptation.computeDensityFactor.
#[test]
fn kernel_pieces_carve_above_the_floor_only() {
let kernel = Arc::new(TerrainKernel::new(24, 24, -1, 0));
let bounds = StructureBounds::new([0; 3], [4; 3]).unwrap();
let affected = StructureBounds::new([-20; 3], [20; 3]);
let b = Beardifier::new(Vec::new(), Vec::new(), affected)
.with_kernels(vec![KernelPiece { bounds, kernel }], Vec::new());
// Inside, on the floor: |0.5 / sqrt(0.125) / 2| * e^(-0.25/24) * -0.8.
assert!((sample(&b, [2, 0, 2]) + 0.559_823_4).abs() < 1e-6);
// Three blocks east: |0.5 / sqrt(4.625) / 2| * e^(-9.25/24) * -0.8.
assert!((sample(&b, [7, 0, 2]) + 0.063_254_3).abs() < 1e-6);
// Below the floor the bottom action (none) applies; past the radius
// nothing does.
assert_eq!(sample(&b, [2, -3, 2]), 0.);
assert_eq!(sample(&b, [17, 2, 2]), 0.);
}
#[test]
fn kernel_junctions_bury_at_four_tenths() {
let kernel = Arc::new(TerrainKernel::new(24, 24, 1, 1));
let b = Beardifier::new(
Vec::new(),
Vec::new(),
StructureBounds::new([-20; 3], [80; 3]),
)
.with_kernels(
Vec::new(),
vec![KernelJunction {
source: [0, 64, 0],
kernel,
}],
);
assert!((sample(&b, [0, 64, 0]) - 0.279_911_7).abs() < 1e-6);
assert!(sample(&b, [0, 60, 0]) > 0.);
assert!(TerrainKernel::new(0, 0, 1, 1).is_none());
}
}
+8
View File
@@ -28,6 +28,14 @@ impl SamplerContext {
..Self::default()
}
}
/// Far previews evaluate only the supplied lattice. Ordinary generation
/// retains vanilla's dense strided-volume evaluation and rounding.
pub fn sparse() -> Self {
Self {
sparse: true,
..Self::cached()
}
}
pub fn clear_caches(&mut self) {
if let Some(caches) = &mut self.caches {
caches.clear();
@@ -172,6 +172,18 @@ impl DensitySampler for InterpolatedSampler {
volume: DensityVolume,
) -> Result<(), SampleError> {
check_buffer(output, volume)?;
if context.sparse {
if (0..3).all(|i| {
volume.steps()[i] % self.cell_size[i] == 0
&& volume.minimum()[i].rem_euclid(self.cell_size[i]) == 0
}) {
return self.input.sample_volume(context, output, volume);
}
return visit(volume, |index, position| {
output[index] = self.sample_value(context, position)?;
Ok(())
});
}
if (0..3).all(|i| {
(volume.steps()[i] == self.cell_size[i] || volume.sizes()[i] == 1)
&& volume.minimum()[i].rem_euclid(self.cell_size[i]) == 0
+5 -1
View File
@@ -9,7 +9,10 @@ use crate::{
use std::sync::Arc;
mod beardifier;
pub use beardifier::{Beardifier, JigsawJunction, RigidPiece, StructureBounds, TerrainAdjustment};
pub use beardifier::{
Beardifier, JigsawJunction, KernelJunction, KernelPiece, RigidPiece, StructureBounds,
TerrainAdjustment, TerrainKernel,
};
mod cache;
mod world_fields;
pub use world_fields::{
@@ -90,6 +93,7 @@ pub struct SamplerContext {
scratch: Vec<Vec<f32>>,
caches: Option<Vec<cache::CacheCell>>,
fields: WorldFields,
sparse: bool,
}
impl SamplerContext {
fn with_buffer<T>(
+134
View File
@@ -501,6 +501,28 @@ fn grid_interpolation_uses_child_bulk_corners_and_preserves_aligned_delegation()
.unwrap();
assert!(output.iter().all(|v| v.to_bits() == (-0.0_f32).to_bits()));
assert_eq!(traced.volumes.lock().unwrap()[1], volume);
let distant = DensityVolume::new([16, 49, 16], [-8192, -64, -8192], [1024, 8, 1024]).unwrap();
let mut output = vec![19.; distant.size()];
sampler
.sample_volume(&mut SamplerContext::sparse(), &mut output, distant)
.unwrap();
assert_eq!(
traced.volumes.lock().unwrap()[2],
distant,
"far samples must not expand to a block-resolution bounding box"
);
let unaligned = DensityVolume::new([2; 3], [-5, -9, -7], [1024, 8, 1024]).unwrap();
let mut output = [19.; 8];
sampler
.sample_volume(&mut SamplerContext::sparse(), &mut output, unaligned)
.unwrap();
assert!(output.iter().all(|&v| v == 0.));
assert!(
traced.volumes.lock().unwrap()[3..]
.iter()
.all(|v| v.size() == 8),
"unaligned far points use bounded corner queries"
);
assert!(InterpolatedSampler::new(traced.clone(), 0, 8).is_err());
assert!(InterpolatedSampler::new(traced, 4, -1).is_err());
}
@@ -825,3 +847,115 @@ fn reset_after_sampling_error_drops_blender_and_preserves_uncached_mode() {
})
.unwrap();
}
#[test]
fn unary_bulk_keeps_child_failure_writes_and_full_buffer_tail() {
#[derive(Debug)]
struct PartialInput;
impl DensitySampler for PartialInput {
fn sample_value(&self, _: &mut SamplerContext, _: [i32; 3]) -> Result<f32, SampleError> {
Err(SampleError::ZeroGradientPeriod)
}
fn sample_volume(
&self,
_: &mut SamplerContext,
buffer: &mut [f32],
_: DensityVolume,
) -> Result<(), SampleError> {
buffer[0] = -3.0;
Err(SampleError::ZeroGradientPeriod)
}
}
let volume = DensityVolume::new([1; 3], [0; 3], [1; 3]).unwrap();
let mut context = SamplerContext::default();
let mut buffer = [17.0; 3];
let sampler = UnarySampler {
input: Arc::new(PartialInput),
operation: UnaryOp::Square,
};
assert_eq!(
sampler.sample_volume(&mut context, &mut buffer, volume),
Err(SampleError::ZeroGradientPeriod)
);
assert_eq!(buffer, [-3.0, 17.0, 17.0]);
let sampler = UnarySampler {
input: Arc::new(ConstantSampler(-3.0)),
operation: UnaryOp::Square,
};
sampler
.sample_volume(&mut context, &mut buffer, volume)
.unwrap();
assert_eq!(buffer, [9.0; 3]);
}
#[test]
fn binary_bulk_preserves_child_failure_order_and_output() {
#[derive(Debug)]
struct Input {
name: &'static str,
value: f32,
fail: bool,
calls: Arc<std::sync::Mutex<Vec<&'static str>>>,
}
impl DensitySampler for Input {
fn sample_value(&self, _: &mut SamplerContext, _: [i32; 3]) -> Result<f32, SampleError> {
unreachable!("bulk-only fixture")
}
fn sample_volume(
&self,
_: &mut SamplerContext,
buffer: &mut [f32],
_: DensityVolume,
) -> Result<(), SampleError> {
self.calls.lock().unwrap().push(self.name);
buffer.fill(self.value);
if self.fail {
Err(SampleError::ZeroGradientPeriod)
} else {
Ok(())
}
}
}
let volume = DensityVolume::new([1; 3], [0; 3], [1; 3]).unwrap();
for left_fails in [true, false] {
let calls = Arc::new(std::sync::Mutex::new(Vec::new()));
let sampler = BinarySampler {
left: Arc::new(Input {
name: "left",
value: 3.0,
fail: left_fails,
calls: calls.clone(),
}),
right: Arc::new(Input {
name: "right",
value: 7.0,
fail: true,
calls: calls.clone(),
}),
operation: BinaryOp::Multiply,
};
let mut context = SamplerContext::default();
let mut wrong_size = [17.0; 2];
assert!(
sampler
.sample_volume(&mut context, &mut wrong_size, volume)
.is_err()
);
assert_eq!(wrong_size, [17.0; 2]);
assert!(calls.lock().unwrap().is_empty());
let mut output = [17.0];
assert_eq!(
sampler.sample_volume(&mut context, &mut output, volume),
Err(SampleError::ZeroGradientPeriod)
);
assert_eq!(output, [3.0]);
assert_eq!(
*calls.lock().unwrap(),
if left_fails {
vec!["left"]
} else {
vec!["left", "right"]
}
);
}
}
+21 -8
View File
@@ -40,18 +40,29 @@ impl PerlinNoise {
}
fn gradients(&self, cell: [i32; 3]) -> [[i32; 3]; 8] {
let p = self.table.permute(cell[0]);
let q = self.table.permute(cell[0].wrapping_add(1));
let lower = self.table.permute(p.wrapping_add(cell[1]));
let upper = self.table.permute(p.wrapping_add(cell[1]).wrapping_add(1));
let lower_x = self.table.permute(q.wrapping_add(cell[1]));
let upper_x = self.table.permute(q.wrapping_add(cell[1]).wrapping_add(1));
self.gradients_at(
self.table.permute(cell[0]),
self.table.permute(cell[0].wrapping_add(1)),
cell[1],
cell[2],
)
}
/// Corner gradients for a Y cell, with the X column's permutation already
/// resolved. `add_volume` reuses one X column across every Y, so hoisting
/// the two X permutes out of that loop avoids repeating them per Y.
#[inline]
fn gradients_at(&self, p: i32, q: i32, cell_y: i32, cell_z: i32) -> [[i32; 3]; 8] {
let lower = self.table.permute(p.wrapping_add(cell_y));
let upper = self.table.permute(p.wrapping_add(cell_y).wrapping_add(1));
let lower_x = self.table.permute(q.wrapping_add(cell_y));
let upper_x = self.table.permute(q.wrapping_add(cell_y).wrapping_add(1));
let rows = [
lower, lower_x, upper, upper_x, lower, lower_x, upper, upper_x,
];
std::array::from_fn(|i| {
self.table
.gradient(rows[i].wrapping_add(cell[2]).wrapping_add((i / 4) as i32))
.gradient(rows[i].wrapping_add(cell_z).wrapping_add((i / 4) as i32))
})
}
@@ -166,6 +177,8 @@ impl PerlinNoise {
let cell_x = floor_i32(shifted_x);
let dx = (shifted_x - f64::from(cell_x)) as f32;
let fade_x = smoothstep(dx);
let p = self.table.permute(cell_x);
let q = self.table.permute(cell_x.wrapping_add(1));
let mut previous_y = i32::MIN;
let mut xz_dots = [0.0_f32; 8];
let mut y_gradients = [0.0_f32; 8];
@@ -176,7 +189,7 @@ impl PerlinNoise {
let dy = shifted_y - f64::from(cell_y);
let fade_y = smoothstep(dy as f32);
if previous_y != cell_y {
let gradients = self.gradients([cell_x, cell_y, cell_z]);
let gradients = self.gradients_at(p, q, cell_y, cell_z);
for i in 0..8 {
xz_dots[i] = gradients[i][0] as f32 * (dx - (i & 1) as f32)
+ gradients[i][2] as f32 * (dz - ((i >> 2) & 1) as f32);
+212 -26
View File
@@ -97,13 +97,16 @@ const START_RULES: [(&str, &str); 3] = [
];
/// The jigsaws' rules on laying out pieces, which vanilla's jigsaw has no
/// place for.
const LAYOUT_RULES: [&str; 5] = [
const LAYOUT_RULES: [&str; 4] = [
"y_allowance",
"burying_type",
"use_bounding_box_hack",
"pools_that_ignore_boundaries",
"enhanced_terrain_adaptation",
];
/// The libraries' terrain kernel field, the game's
/// `horizon:enhanced_terrain_adaptation` (docs/mods.md).
const ENHANCED_TERRAIN: &str = "enhanced_terrain_adaptation";
const HORIZON_ENHANCED_TERRAIN: &str = "horizon:enhanced_terrain_adaptation";
/// Fields of older versions the mods' codecs no longer read: their own
/// structures ignore them, and so does carry. Repurposed's jungle fortress
/// and End stronghold still write `vertical_distance_from_start_piece`.
@@ -476,6 +479,22 @@ fn upgrade_types(
limited["layout"] = "mineshaft".into();
}
start_checks(v, liquid, lost, limited.is_some());
if library == Some("moogs") {
// GenericJigsawStructure's adaptation, NONE by default;
// its pieces may still have their own.
let own = v.as_object_mut().and_then(|o| o.remove(ENHANCED_TERRAIN));
let none = serde_json::json!({"kernel_size": 0, "kernel_distance": 0,
"top": "none", "bottom": "none"});
let mut kernel = own.and_then(|a| moogs_terrain(&a, lost)).unwrap_or(none);
kernel["rigid_junctions"] = false.into();
v[HORIZON_ENHANCED_TERRAIN] = kernel;
} else if v
.as_object_mut()
.and_then(|o| o.remove(ENHANCED_TERRAIN))
.is_some()
{
*lost.entry(ENHANCED_TERRAIN.into()).or_default() += 1;
}
if let Some(check) = check {
if !v["horizon:start_checks"].is_object() {
v["horizon:start_checks"] = Value::Object(Default::default());
@@ -533,6 +552,14 @@ fn upgrade_types(
continue;
};
if let Some((_, to)) = ELEMENTS.iter().find(|(from, _)| *from == t) {
if t.starts_with("moogs_structures:") {
let own = element
.as_object_mut()
.and_then(|o| o.remove(ENHANCED_TERRAIN));
if let Some(kernel) = own.and_then(|a| moogs_terrain(&a, lost)) {
element[HORIZON_ENHANCED_TERRAIN] = kernel;
}
}
element["element_type"] = (*to).into();
changed = true;
} else if t == "yungsapi:yung_single_element" {
@@ -552,8 +579,9 @@ fn upgrade_types(
/// YUNG's API's jigsaw as vanilla's, laid out by its own assembler in the
/// game (`horizon:yungsapi`, `horizon_world::worldgen::structure::yung`):
/// the heights pieces stay within move under the key. Its terrain
/// adaptation and a start offset in the chunk are lost.
/// the heights pieces stay within move under the key, its terrain
/// adaptation under `horizon:enhanced_terrain_adaptation`. A start offset in
/// the chunk is lost.
fn yung_structure(v: &mut Value, lost: &mut BTreeMap<String, usize>) {
let Some(o) = v.as_object_mut() else {
return;
@@ -565,14 +593,13 @@ fn yung_structure(v: &mut Value, lost: &mut BTreeMap<String, usize>) {
rules.insert(key.into(), y);
}
}
let none = |a: &Value| a["type"] == "yungsapi:none";
if o.remove("enhanced_terrain_adaptation")
.is_some_and(|a| !none(&a))
{
*lost
.entry("enhanced_terrain_adaptation".into())
.or_default() += 1;
}
// YungJigsawStructure's adaptation (NONE by default): every piece's
// junctions, and the start's box grown by its radius.
let own = o.remove(ENHANCED_TERRAIN);
let mut kernel = yung_terrain(own.as_ref().unwrap_or(&Value::Null), lost);
kernel["rigid_junctions"] = true.into();
kernel["inflate"] = true.into();
o.insert(HORIZON_ENHANCED_TERRAIN.into(), kernel);
for key in ["x_offset_in_chunk", "z_offset_in_chunk"] {
if o.remove(key).is_some_and(|n| n != 0) {
*lost.entry(key.into()).or_default() += 1;
@@ -583,8 +610,8 @@ fn yung_structure(v: &mut Value, lost: &mut BTreeMap<String, usize>) {
/// YUNG's single pool element as vanilla's, its rules under
/// `horizon:yungsapi`: name and `max_count`, depth range, priority,
/// `ignore_bounds` and its condition. Its terrain adaptation, dead-end pool
/// and modifiers are lost.
/// `ignore_bounds`, its condition, dead-end pool and modifiers; its terrain
/// adaptation under `horizon:enhanced_terrain_adaptation`.
fn yung_element(element: &mut Value, lost: &mut BTreeMap<String, usize>) {
let Some(o) = element.as_object_mut() else {
return;
@@ -609,16 +636,118 @@ fn yung_element(element: &mut Value, lost: &mut BTreeMap<String, usize>) {
if let Some(c) = o.remove("condition") {
rules.insert("condition".into(), yung_condition(&c, lost));
}
for key in ["enhanced_terrain_adaptation", "deadend_pool", "modifiers"] {
let empty =
|x: &Value| x.as_array().is_some_and(Vec::is_empty) || x["type"] == "yungsapi:none";
if o.remove(key).is_some_and(|x| !empty(&x)) {
*lost.entry(format!("yungsapi {key}")).or_default() += 1;
}
// A piece's own adaptation, NONE included, replaces its structure's.
if let Some(a) = o.remove(ENHANCED_TERRAIN) {
o.insert(HORIZON_ENHANCED_TERRAIN.into(), yung_terrain(&a, lost));
}
if let Some(pool) = o.remove("deadend_pool").filter(Value::is_string) {
rules.insert("deadend_pool".into(), pool);
}
let modifiers: Vec<Value> = o
.remove("modifiers")
.and_then(|m| m.as_array().cloned())
.unwrap_or_default()
.iter()
.filter_map(|m| yung_modifier(m, lost))
.collect();
if !modifiers.is_empty() {
rules.insert("modifiers".into(), modifiers.into());
}
o.insert("horizon:yungsapi".into(), rules.into());
}
/// One of YUNG's structure modifiers in the game's terms: its condition and
/// its transform and delay_generation actions, on the piece itself (`self`,
/// the API's only target). Another target loses the modifier, another action
/// just that action.
fn yung_modifier(m: &Value, lost: &mut BTreeMap<String, usize>) -> Option<Value> {
let target = m["target_selector"]["type"].as_str().unwrap_or_default();
if target != "yungsapi:self" {
*lost
.entry(format!("yungsapi modifier target {target}"))
.or_default() += 1;
return None;
}
let mut actions = Vec::new();
for a in m["actions"].as_array().into_iter().flatten() {
let int = |k: &str| a[k].as_i64().unwrap_or(0);
match a["type"].as_str().unwrap_or_default() {
"yungsapi:transform" => actions.push(serde_json::json!({"type": "transform",
"output": a["output"].clone(),
"offset": [int("x_offset"), int("y_offset"), int("z_offset")]})),
"yungsapi:delay_generation" => {
actions.push(serde_json::json!({"type": "delay_generation"}))
}
other => {
*lost
.entry(format!("yungsapi modifier action {other}"))
.or_default() += 1
}
}
}
let condition = match m.get("condition") {
Some(c) => yung_condition(c, lost),
None => serde_json::json!({"type": "true"}),
};
Some(serde_json::json!({"condition": condition, "actions": actions}))
}
/// YUNG's API's terrain adaptation as the game's kernel: its two presets
/// spelled out, NONE (also the default) as size 0. An aquifer override
/// (none of ATM10's) is lost.
fn yung_terrain(a: &Value, lost: &mut BTreeMap<String, usize>) -> Value {
let kernel = |size: i32, distance: i32, top: &str, bottom: &str| {
serde_json::json!({"kernel_size": size, "kernel_distance": distance,
"top": top, "bottom": bottom})
};
match a["type"].as_str() {
Some("yungsapi:carved_top_no_beard_large") => kernel(24, 16, "carve", "none"),
Some("yungsapi:carved_top_no_beard_small") => kernel(12, 6, "carve", "none"),
Some("yungsapi:custom") => {
let mut out = serde_json::Map::new();
for key in [
"kernel_size",
"kernel_distance",
"top",
"bottom",
"bottom_offset",
"padding",
] {
if let Some(x) = a.get(key) {
out.insert(key.into(), x.clone());
}
}
if a.get("aquifer_override")
.is_some_and(|x| x["type"] != "yungsapi:none")
{
*lost.entry("yungsapi aquifer_override".into()).or_default() += 1;
}
moogs_terrain(&out.into(), lost).unwrap_or_else(|| kernel(0, 0, "none", "none"))
}
None | Some("yungsapi:none") => kernel(0, 0, "none", "none"),
Some(other) => {
*lost
.entry(format!("yungsapi terrain adaptation {other}"))
.or_default() += 1;
kernel(0, 0, "none", "none")
}
}
}
/// MoogsStructureLib's terrain adaptation: already the game's shape (with
/// its band), when it has the fields the game needs (else it is lost, as
/// the library's codec would refuse it).
fn moogs_terrain(a: &Value, lost: &mut BTreeMap<String, usize>) -> Option<Value> {
let ok = ["kernel_size", "kernel_distance"]
.iter()
.all(|k| a[k].is_u64())
&& ["top", "bottom"].iter().all(|k| a[k].is_string());
if !ok {
*lost.entry(ENHANCED_TERRAIN.into()).or_default() += 1;
}
ok.then(|| a.clone())
}
/// One of YUNG's structure conditions in the game's terms. `mod_loaded`
/// holds for Minecraft alone: a mod that needs code is not ported, and the
/// pieces it guards would place its blocks. A condition the game does not
@@ -653,6 +782,11 @@ fn yung_condition(c: &Value, lost: &mut BTreeMap<String, usize>) -> Value {
"pieces": if any { Vec::new() } else { pieces },
"range": opt("range"), "rotation": opt("rotation")})
}
// The game reads "yungsapi:all" (or no list) as any piece.
"piece_in_range" => serde_json::json!({"type": "piece_in_range",
"pieces": c.get("pieces").cloned().unwrap_or_else(|| Vec::<Value>::new().into()),
"horizontal_range": opt("horizontal_range"), "above_range": opt("above_range"),
"below_range": opt("below_range")}),
"mod_loaded" if c["modid"] == "minecraft" => serde_json::json!({"type": "true"}),
"mod_loaded" => serde_json::json!({"type": "false"}),
_ => {
@@ -1095,7 +1229,9 @@ mod tests {
.is_none()
);
let mut pool = json!({"elements": [
{"weight": 1, "element": {"element_type": "moogs_structures:versioned_single_pool_element", "location": "m:x"}},
{"weight": 1, "element": {"element_type": "moogs_structures:versioned_single_pool_element", "location": "m:x",
"enhanced_terrain_adaptation": {"kernel_size": 16, "kernel_distance": 8, "top": "carve",
"bottom": "carve", "band": {"bottom": 17, "top": 23}}}},
{"weight": 1, "element": {"element_type": "minecraft:list_pool_element", "elements": [
{"element_type": "empty_pool_element"}]}},
{"weight": 1, "element": {"element_type": "somelib:element"}}
@@ -1105,6 +1241,11 @@ mod tests {
pool["elements"][0]["element"]["element_type"],
"minecraft:single_pool_element"
);
// Its own terrain kernel, band and all, under the game's key.
assert_eq!(
pool["elements"][0]["element"]["horizon:enhanced_terrain_adaptation"]["band"],
json!({"bottom": 17, "top": 23})
);
assert_eq!(
pool["elements"][1]["element"]["elements"][0]["element_type"],
"minecraft:empty_pool_element"
@@ -1143,6 +1284,22 @@ mod tests {
assert!(moogs.get(gone).is_none(), "{gone}");
}
assert_eq!(moogs["size"], 7);
// GenericJigsawStructure's terrain kernel, NONE when it has none.
assert_eq!(
moogs["horizon:enhanced_terrain_adaptation"],
json!({"kernel_size": 0, "kernel_distance": 0, "top": "none", "bottom": "none",
"rigid_junctions": false})
);
let mut fortress = json!({"type": "moogs_structures:moogs_structures_generic_nether_jigsaw_structure",
"size": 7, "enhanced_terrain_adaptation": {"kernel_size": 16, "kernel_distance": 8,
"top": "carve", "bottom": "carve", "band": {"bottom": 17, "top": 23,
"piece_heights": [24, 25, 27]}}});
assert!(upgrade("structure", &mut fortress, &mut report, &NO_TAGS));
assert_eq!(
fortress["horizon:enhanced_terrain_adaptation"]["band"]["piece_heights"],
json!([24, 25, 27])
);
assert!(fortress.get("enhanced_terrain_adaptation").is_none());
// Repurposed Structures reads the block above the bottom. Its jungle
// fortress's vertical distance is a field its code no longer reads.
let mut rs = json!({"type": "repurposed_structures:generic_jigsaw_structure",
@@ -1219,10 +1376,16 @@ mod tests {
let mut report = Upgrade::default();
let mut s = json!({"type": "yungsapi:yung_jigsaw", "start_pool": "y:start", "size": 20,
"max_y": 60, "x_offset_in_chunk": 0, "max_distance_from_center": 80,
"enhanced_terrain_adaptation": {"type": "yungsapi:custom", "kernel_size": 24}});
"enhanced_terrain_adaptation": {"type": "yungsapi:custom", "kernel_size": 24,
"kernel_distance": 24, "top": "carve", "bottom": "none"}});
assert!(upgrade("structure", &mut s, &mut report, &NO_TAGS));
assert_eq!(s["type"], "minecraft:jigsaw");
assert_eq!(s["horizon:yungsapi"], json!({"max_y": 60}));
assert_eq!(
s["horizon:enhanced_terrain_adaptation"],
json!({"kernel_size": 24, "kernel_distance": 24, "top": "carve", "bottom": "none",
"rigid_junctions": true, "inflate": true})
);
for gone in ["max_y", "x_offset_in_chunk", "enhanced_terrain_adaptation"] {
assert!(s.get(gone).is_none(), "{gone}");
}
@@ -1234,6 +1397,11 @@ mod tests {
assert!(upgrade("structure", &mut nether, &mut report, &NO_TAGS));
assert_eq!(nether["type"], "minecraft:jigsaw");
assert_eq!(nether["horizon:yungsapi"], json!({}));
assert_eq!(
nether["horizon:enhanced_terrain_adaptation"],
json!({"kernel_size": 0, "kernel_distance": 0, "top": "none", "bottom": "none",
"rigid_junctions": true, "inflate": true})
);
assert_eq!(
nether["start_jigsaw_name"],
"betterdungeons:small_nether_dungeon_anchor"
@@ -1244,6 +1412,15 @@ mod tests {
"processors": "minecraft:empty", "projection": "rigid",
"name": "room", "max_count": 2, "min_required_depth": 1, "is_priority": true,
"ignore_bounds": false, "deadend_pool": "y:end",
"modifiers": [
{"condition": {"type": "yungsapi:piece_in_range", "pieces": ["y:pillar"],
"above_range": 40},
"target_selector": {"type": "yungsapi:self"},
"actions": [{"type": "yungsapi:transform", "output": ["y:covered"], "x_offset": -1},
{"type": "yungsapi:delay_generation"}, {"type": "yungsapi:spin"}]},
{"condition": {"type": "yungsapi:always_true"},
"target_selector": {"type": "yungsapi:all"}, "actions": []}],
"enhanced_terrain_adaptation": {"type": "yungsapi:carved_top_no_beard_small"},
"condition": {"type": "yungsapi:all_of", "conditions": [
{"type": "yungsapi:piece_in_horizontal_direction", "pieces": ["yungsapi:all"],
"range": 5, "rotation": "none"},
@@ -1259,15 +1436,24 @@ mod tests {
"ignore_bounds": false, "condition": {"type": "all_of", "conditions": [
{"type": "piece_in_horizontal_direction", "pieces": [], "range": 5, "rotation": "none"},
{"type": "not", "condition": {"type": "false"}},
{"type": "false"}]}})
{"type": "false"}]},
"deadend_pool": "y:end",
"modifiers": [{"condition": {"type": "piece_in_range", "pieces": ["y:pillar"],
"horizontal_range": null, "above_range": 40, "below_range": null},
"actions": [{"type": "transform", "output": ["y:covered"], "offset": [-1, 0, 0]},
{"type": "delay_generation"}]}]})
);
assert!(e.get("deadend_pool").is_none() && e.get("modifiers").is_none());
assert_eq!(
e["horizon:enhanced_terrain_adaptation"],
json!({"kernel_size": 12, "kernel_distance": 6, "top": "carve", "bottom": "none"})
);
assert!(e.get("deadend_pool").is_none());
assert_eq!(
report.rules_lost,
BTreeMap::from([
("enhanced_terrain_adaptation".into(), 1),
("yungsapi condition yungsapi:biome".into(), 1),
("yungsapi deadend_pool".into(), 1),
("yungsapi modifier action yungsapi:spin".into(), 1),
("yungsapi modifier target yungsapi:all".into(), 1),
])
);
assert!(report.unsupported.is_empty());
+4
View File
@@ -17,6 +17,10 @@ serde_json = "1"
toml = "1.1"
# Edits server.toml in place, keeping the owner's comments and order.
toml_edit = "0.25"
# Updates: the release from GitHub, checked against the SHA-256 GitHub publishes (both already in the
# lockfile, ureq through horizon-server-files).
sha2 = "0.10"
ureq = { version = "2", default-features = false, features = ["tls"] }
# Backups: one zip anyone can open, Windows included.
zip = { version = "2", default-features = false, features = ["deflate"] }
+5
View File
@@ -21,6 +21,11 @@
</div>
</header>
<p id="update" class="notice" role="status" hidden>
<span id="update-text"></span>
<button id="update-button" type="button" hidden>Update</button>
</p>
<main id="app" hidden>
<section class="charts" aria-label="Last 10 minutes">
<figure><figcaption>TPS <b id="tps-now">–</b></figcaption><canvas id="tps" width="320" height="96"></canvas></figure>
+14
View File
@@ -126,3 +126,17 @@ code { font-family: var(--mono); font-size: 13px; }
border-radius: var(--radius);
max-width: 90vw;
}
.notice {
display: flex;
align-items: center;
gap: 12px;
margin: 16px max(24px, calc((100% - 1152px) / 2)) 0;
padding: 10px 14px;
border-radius: var(--radius);
background: var(--plate-soft);
border: 1px solid var(--rule);
}
.notice.failed {
color: var(--danger);
}
+21
View File
@@ -87,6 +87,7 @@ function renderStatus(status) {
const parts = [`up ${duration(status.engine_uptime_seconds)}`, `horizon-server ${status.version}`];
if (status.crashes) parts.push(`${status.crashes} crash${status.crashes > 1 ? "es" : ""}`);
$("summary").textContent = parts.join(" · ");
renderUpdate(status.update);
const players = (game && game.players) || [];
$("player-count").textContent = game ? `${players.length} of ${game.max_players}` : "";
@@ -108,6 +109,20 @@ function renderStatus(status) {
);
}
// Updates (horizon-server update): a newer release, one under way, or how the last one ended.
function renderUpdate(update) {
const box = $("update");
const offer = update && !update.busy && update.available;
let text = "";
if (update && update.busy) text = `Updating: ${update.busy}…`;
else if (offer) text = `Horizon ${update.available} is out (this server runs ${update.installed}).`;
else if (update && update.last && !update.ok) text = update.last;
box.hidden = !text;
box.className = update && update.last && !update.ok && !update.busy && !offer ? "notice failed" : "notice";
$("update-text").textContent = text;
$("update-button").hidden = !offer;
}
function renderBackups(list) {
const key = JSON.stringify(list);
if (key === backupsShown) return;
@@ -255,6 +270,12 @@ function setUp() {
toast(e.message);
}
});
$("update-button").addEventListener("click", async () => {
const update = lastStatus && lastStatus.update;
if (!update || !update.available) return;
if (!confirm(`Update to ${update.available}? The world is backed up, then the game restarts on it (players are disconnected for a moment). If it doesn't come up, the server goes back to ${update.installed} by itself.`)) return;
await api("POST", "/api/v1/update", {}).then(() => toast("Updating…"), (e) => toast(e.message));
});
$("restart").addEventListener("click", async () => {
if (!confirm("Restart the game? Players are disconnected for a moment.")) return;
await api("POST", "/api/v1/restart").then(() => toast("Restarting…"), (e) => toast(e.message));
+137 -2
View File
@@ -9,6 +9,9 @@
//! - `GET /api/v1/metrics?since=T`: the samples taken after T (seconds since 1970), up to 10 minutes.
//! - `GET /api/v1/backups`; `POST /api/v1/backups {"name": "..."}` backs up now; `POST /api/v1/restore
//! {"name": "..."}` restarts the game with that backup's world.
//! - `POST /api/v1/update {"tag": "...", "timeout": seconds}`: restarts the game on that installed release,
//! and goes back to the one in use if it isn't up within the timeout; without a tag, downloads the newest
//! release first (update.rs). `GET /api/v1/status` says how it went (`update`).
//! - `POST /api/v1/panel/login`: a one-time login link for the web panel (with the token only).
use crate::{
@@ -17,6 +20,7 @@ use crate::{
engine,
http::{self, Request, Response},
panel,
update::{self, Install},
};
use serde_json::{Value, json};
use std::{
@@ -91,6 +95,25 @@ impl Log {
}
}
/// How long a game started on another release has to come up before the switch is undone.
pub const UPDATE_TIMEOUT: Duration = Duration::from_secs(300);
/// Updates and rollbacks (update.rs): what the check found, what is under way, how the last one ended.
#[derive(Default)]
pub struct Updates {
/// A release newer than the one in use, from the last check.
pub available: Option<String>,
/// What is happening now, or nothing.
pub busy: Option<String>,
/// How the last update or rollback ended, and whether it went well.
pub last: Option<String>,
pub ok: bool,
/// Counts the updates that ended, so a client can wait for the one it asked for.
pub finished: u64,
/// The release to restart the game on, and how long it has to come up.
pub pending: Option<(String, Duration)>,
}
/// What the supervisor and the API threads share.
pub struct Shared {
pub dir: PathBuf,
@@ -107,10 +130,17 @@ pub struct Shared {
pub metrics: Mutex<VecDeque<Value>>,
/// The panel's address, login links and sessions.
pub panel: Mutex<panel::Auth>,
/// The installer's folder this program runs from (none for a build of your own: no updates).
pub install: Option<Install>,
pub update: Mutex<Updates>,
}
impl Shared {
pub fn new(dir: &Path) -> Arc<Self> {
Self::with_install(dir, Install::find())
}
pub fn with_install(dir: &Path, install: Option<Install>) -> Arc<Self> {
Arc::new(Self {
dir: dir.to_owned(),
state: Mutex::new(State {
@@ -129,8 +159,85 @@ impl Shared {
started: Instant::now(),
metrics: Mutex::new(VecDeque::new()),
panel: Mutex::new(panel::Auth::default()),
install,
update: Mutex::new(Updates::default()),
})
}
/// Ends an update or rollback: its outcome, in the log and in `status`.
pub fn finish_update(&self, ok: bool, message: String) {
{
let mut update = self.update.lock().unwrap();
update.busy = None;
update.ok = ok;
update.finished += 1;
if ok {
update.available = None;
}
update.last = Some(message.clone());
}
crate::supervisor::note(self, if ok { "INFO" } else { "ERROR" }, &message);
}
/// Asks for a restart on the installed release `tag` (the supervisor switches when the game stops).
fn switch_to(&self, tag: String, timeout: Duration) {
self.update.lock().unwrap().busy = Some(format!("Restarting the game on {tag}"));
self.update.lock().unwrap().pending = Some((tag, timeout));
self.restart.store(true, Ordering::SeqCst);
}
/// `POST /api/v1/update`: switches to `tag`, or downloads the newest release first.
fn start_update(
self: &Arc<Self>,
tag: Option<String>,
timeout: Duration,
) -> Result<(), String> {
let install = self.install.clone().ok_or(
"this horizon-server wasn't installed by install.sh or install.ps1: no updates",
)?;
{
let mut update = self.update.lock().unwrap();
if let Some(busy) = &update.busy {
return Err(format!("an update is under way: {busy}"));
}
update.busy = Some("Looking for the newest release".into());
}
if let Some(tag) = tag {
if !install.release(&tag).is_dir() || tag.contains(['/', '\\']) {
self.update.lock().unwrap().busy = None;
return Err(format!(
"{tag} isn't installed (`horizon-server update` downloads it)"
));
}
self.switch_to(tag, timeout);
return Ok(());
}
let shared = self.clone();
std::thread::Builder::new()
.name("update".into())
.spawn(move || {
let fetched = (|| {
let current = install.current().unwrap_or_default();
let release = update::release(None)?;
if !update::newer(&release.tag, &current) {
return Ok(None);
}
shared.update.lock().unwrap().busy =
Some(format!("Downloading {} ({})", release.tag, release.asset));
let archive = update::download(&install, &release)?;
let unpacked = update::unpack(&install, &archive, &release.tag);
let _ = std::fs::remove_file(&archive);
unpacked.map(|_| Some(release.tag))
})();
match fetched {
Ok(Some(tag)) => shared.switch_to(tag, timeout),
Ok(None) => shared.finish_update(true, "Already on the newest release".into()),
Err(e) => shared.finish_update(false, format!("Not updated: {e}")),
}
})
.map_err(|e| e.to_string())?;
Ok(())
}
/// Keeps one sample for the graphs.
pub fn record(&self, sample: Value) {
let mut metrics = self.metrics.lock().unwrap();
@@ -187,6 +294,23 @@ fn status(shared: &Shared) -> Value {
"crashes": state.crashes,
"last_crash": state.last_crash,
"sample": state.sample,
"update": updates(shared),
})
}
fn updates(shared: &Shared) -> Value {
let Some(install) = &shared.install else {
return Value::Null;
};
let update = shared.update.lock().unwrap();
json!({
"installed": install.current(),
"previous": install.previous(),
"available": update.available,
"busy": update.busy,
"last": update.last,
"ok": update.ok,
"finished": update.finished,
})
}
@@ -273,7 +397,7 @@ pub fn start(shared: Arc<Shared>) -> Result<Endpoint, String> {
}
/// The API's routes, for the CLI (with the token) and the panel (with a session).
pub fn route(shared: &Shared, request: &Request) -> Response {
pub fn route(shared: &Arc<Shared>, request: &Request) -> Response {
match (request.method.as_str(), request.path.as_str()) {
("GET", "/api/v1/status") => Response::json(200, &status(shared)),
("GET", "/api/v1/metrics") => {
@@ -354,10 +478,21 @@ pub fn route(shared: &Shared, request: &Request) -> Response {
Err(e) => Response::error(404, &e),
}
}
("POST", "/api/v1/update") => {
let body: Value = serde_json::from_slice(&request.body).unwrap_or(Value::Null);
let timeout = body["timeout"]
.as_u64()
.map_or(UPDATE_TIMEOUT, Duration::from_secs);
match shared.start_update(body["tag"].as_str().map(str::to_owned), timeout) {
Ok(()) => Response::json(200, &json!({ "ok": true })),
Err(e) => Response::error(409, &e),
}
}
(
_,
"/api/v1/status" | "/api/v1/logs" | "/api/v1/command" | "/api/v1/stop"
| "/api/v1/restart" | "/api/v1/backups" | "/api/v1/restore" | "/api/v1/metrics",
| "/api/v1/restart" | "/api/v1/backups" | "/api/v1/restore" | "/api/v1/metrics"
| "/api/v1/update",
) => Response::error(405, "wrong method"),
_ => Response::error(404, "no such endpoint"),
}
+22
View File
@@ -251,6 +251,15 @@ fn describe(status: &Value) -> String {
if !usage.is_empty() {
out += &format!("{}\n", usage.join(", "));
}
let update = &status["update"];
if let Some(busy) = update["busy"].as_str() {
out += &format!("Updating: {busy}\n");
} else if let Some(tag) = update["available"].as_str() {
out += &format!(
"Horizon {tag} is out (this server runs {}): `horizon-server update` installs it\n",
update["installed"].as_str().unwrap_or("?")
);
}
out
}
@@ -429,5 +438,18 @@ mod tests {
describe(&json!({ "phase": "starting", "uptime_seconds": 4 })),
"Starting, up 4 s\n"
);
assert_eq!(
describe(&json!({
"phase": "running",
"update": { "installed": "v2026.10.18", "available": "v2026.10.19" }
})),
"Running\nHorizon v2026.10.19 is out (this server runs v2026.10.18): `horizon-server update` installs it\n"
);
assert_eq!(
describe(
&json!({ "phase": "restarting", "update": { "busy": "Starting the game on v2" } })
),
"Restarting\nUpdating: Starting the game on v2\n"
);
}
}
+55 -6
View File
@@ -20,8 +20,18 @@ fn exe(name: &str) -> String {
format!("{name}{}", std::env::consts::EXE_SUFFIX)
}
/// The game program: `engine.path`, else one installed beside `horizon-server`, else one on the PATH.
/// The game program: `engine.path`, else one installed beside `horizon-server` (in the release in use when
/// it was installed by the installers), else one on the PATH.
pub fn program(config: &Config, dir: &Path) -> Result<PathBuf, String> {
program_in(config, dir, crate::update::Install::find().as_ref())
}
/// [`program`], for the install `install`.
pub fn program_in(
config: &Config,
dir: &Path,
install: Option<&crate::update::Install>,
) -> Result<PathBuf, String> {
if !config.engine.path.is_empty() {
let path = dir.join(&config.engine.path);
return path
@@ -29,9 +39,12 @@ pub fn program(config: &Config, dir: &Path) -> Result<PathBuf, String> {
.then_some(path)
.ok_or_else(|| format!("engine.path {} is not a file", config.engine.path));
}
let beside = std::env::current_exe()
.ok()
.and_then(|e| e.parent().map(Path::to_path_buf));
// Installed: the release `current` points at now, so a switch applies at the next start.
let beside = install.map(crate::update::Install::programs).or_else(|| {
std::env::current_exe()
.ok()
.and_then(|e| e.parent().map(Path::to_path_buf))
});
let path_dirs = std::env::var_os("PATH")
.map(|p| std::env::split_paths(&p).collect::<Vec<_>>())
.unwrap_or_default();
@@ -141,14 +154,18 @@ pub fn resources(config: &Config, dir: &Path, program: Option<&Path>) -> Result<
/// Starts the engine in the server folder, with its console and log piped to the supervisor and its status
/// on the control endpoint.
pub fn spawn(program: &Path, resources: &Path, dir: &Path) -> std::io::Result<Child> {
// The game runs in the server folder, so a folder given relative to ours (`--dir srv`) would put its
// control endpoint in `srv/srv/.horizon`, which doesn't exist.
let dir = std::path::absolute(dir)?;
let resources = std::path::absolute(resources)?;
let mut command = Command::new(program);
command
.arg("--resources")
.arg(resources)
.arg("--control-socket")
.arg(control_path(dir))
.arg(control_path(&dir))
.args(["--dedicated", "--nogui"])
.current_dir(dir)
.current_dir(&dir)
.env("HORIZON_NO_AUDIO", "1")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
@@ -276,6 +293,38 @@ mod tests {
);
}
#[cfg(unix)]
#[test]
fn a_relative_folder_reaches_the_game_whole() {
use std::os::unix::fs::PermissionsExt;
// Relative to this process's folder (the crate's, under cargo test), as `--dir srv` gives it.
let root = PathBuf::from(format!("reldir-test-{}", std::process::id()));
let server = root.join("srv");
std::fs::create_dir_all(server.join(".horizon")).unwrap();
std::fs::create_dir_all(root.join("res")).unwrap();
// A game that checks, from its own folder, the folders it was given.
let game = root.join("game");
std::fs::write(
&game,
"#!/bin/sh\nwhile [ $# -gt 0 ]; do case \"$1\" in --resources) r=$2; shift;; \
--control-socket) c=$2; shift;; esac; shift; done\n\
[ -d \"$r\" ] && [ -d \"$(dirname \"$c\")\" ] && [ -f server.toml ]\n",
)
.unwrap();
std::fs::set_permissions(&game, std::fs::Permissions::from_mode(0o755)).unwrap();
std::fs::write(server.join("server.toml"), "").unwrap();
let status = spawn(
&std::path::absolute(&game).unwrap(),
&root.join("res"),
&server,
)
.unwrap()
.wait();
std::fs::remove_dir_all(&root).unwrap();
let status = status.unwrap();
assert!(status.success(), "{status}");
}
#[cfg(unix)]
#[test]
fn the_check_runs_the_program() {
+8
View File
@@ -13,6 +13,7 @@ mod panel;
mod service;
mod signals;
mod supervisor;
mod update;
use std::{path::PathBuf, process::ExitCode};
@@ -45,6 +46,11 @@ Usage: horizon-server [--dir DIR] <command>
with sudo), a launchd agent on macOS, a task at logon on Windows
panel A one-time login link for the web panel ([panel] in server.toml; 127.0.0.1:8130 by
default)
update [--check] [--version TAG] [--timeout SECONDS]
Install the newest release (checked against GitHub's SHA-256), back up the world and
restart on it; goes back by itself if the game isn't up within the timeout (300 s).
--archive FILE --version NAME installs an archive you have
rollback Back to the release used before the update
import FILE Write server.toml from a vanilla server.properties
op NAME, deop NAME
whitelist on|off|list, whitelist add|remove NAME
@@ -92,6 +98,8 @@ fn main() -> ExitCode {
None => Err("restore needs a backup (`horizon-server backups` lists them)".into()),
},
Some("import") => import(&dir, &args[1..]),
Some("update") => update::command(&dir, &args[1..]),
Some("rollback") => update::rollback(&dir, &args[1..]),
Some(command @ ("op" | "deop" | "whitelist")) => lists::run(&dir, command, &args[1..]),
Some(other) => Err(format!(
"unknown command {other}; `horizon-server help` lists them"
+1 -1
View File
@@ -145,7 +145,7 @@ fn secure(response: Response) -> Response {
.with("Referrer-Policy", "no-referrer")
}
fn handle(shared: &Shared, address: SocketAddr, request: &Request) -> Response {
fn handle(shared: &Arc<Shared>, address: SocketAddr, request: &Request) -> Response {
if !request
.header("host")
.is_some_and(|h| host_allowed(h, address))
+365 -6
View File
@@ -1,12 +1,13 @@
//! `horizon-server run`: writes `server.properties` from `server.toml`, runs the engine with the console on
//! stdin, serves the control API, stops the game cleanly on Ctrl-C, SIGTERM or `stop`, and restarts it when
//! it crashes (docs/server.md, sections 2 and 7).
//! it crashes (docs/server.md, sections 2 and 7). An update restarts the game on another release and goes
//! back by itself when the new game doesn't come up.
use crate::{
api::{self, Phase, Shared},
backup, config,
config::Config,
engine, signals,
engine, signals, update,
};
use serde_json::json;
use std::{
@@ -30,7 +31,7 @@ const STOP_TIMEOUT: Duration = Duration::from_secs(120);
/// A line from the supervisor, in the game's log format (`[HH:mm:ss] [thread/LEVEL]: message`, local time
/// as the game's own lines), on stdout and in the log the API serves.
fn note(shared: &Shared, level: &str, message: &str) {
pub fn note(shared: &Shared, level: &str, message: &str) {
let line = format!(
"[{}] [Supervisor/{level}]: {message}",
chrono::Local::now().format("%H:%M:%S")
@@ -65,6 +66,20 @@ enum Exit {
/// Asked to restart (`horizon-server restart`).
Restart,
Crashed(ExitStatus),
/// Started on another release, the game wasn't up in time and was stopped.
NotUp,
}
/// A game started on another release that has yet to come up: what to go back to if it doesn't.
struct Watch {
from: String,
to: String,
/// The release `previous` named before the switch.
previous: Option<String>,
/// The backup of the world made just before the switch.
backup: String,
timeout: Duration,
deadline: Instant,
}
pub fn run(dir: &Path) -> Result<(), String> {
@@ -97,11 +112,20 @@ pub fn run(dir: &Path) -> Result<(), String> {
}
sample(shared.clone());
schedule_backups(shared.clone());
check_updates(shared.clone());
let console = console_lines();
let mut crashes: Vec<Instant> = Vec::new();
let mut watch: Option<Watch> = None;
let (mut program, mut resources) = (program, resources);
loop {
// Read again before every start: edits to server.toml apply on the next restart.
// Read again before every start: edits to server.toml apply on the next restart, and so does a
// switch to another release.
let config = Config::load(dir)?;
if let Ok(found) = engine::program_in(&config, dir, shared.install.as_ref())
&& let Ok(found_resources) = engine::resources(&config, dir, Some(&found))
{
(program, resources) = (found, found_resources);
}
let written = config
.write_properties(dir)
.map_err(|e| format!("server.properties: {e}"))?;
@@ -122,7 +146,11 @@ pub fn run(dir: &Path) -> Result<(), String> {
state.engine_pid = Some(child.id());
state.engine_started = Some(Instant::now());
}
let exit = supervise(&shared, &mut child, &console);
// The new release's time to come up starts with its game.
if let Some(w) = &mut watch {
w.deadline = Instant::now() + w.timeout;
}
let exit = supervise(&shared, &mut child, &console, &mut watch);
{
let mut state = shared.state.lock().unwrap();
state.engine_pid = None;
@@ -144,8 +172,25 @@ pub fn run(dir: &Path) -> Result<(), String> {
&format!("Could not update server.toml: {e}"),
),
}
// Started on another release, the game never came up: back to the release before.
if let Some(w) = watch.take() {
let reason = match &exit {
Exit::Crashed(status) => format!("the game stopped ({status})"),
Exit::NotUp => "the game wasn't up in time".into(),
Exit::Stopped => "the game stopped before it was up".into(),
Exit::Restart => "the game was restarted before it was up".into(),
};
roll_back(&shared, dir, &config, &w, &reason);
if matches!(exit, Exit::Stopped)
&& shared.state.lock().unwrap().phase == Phase::Stopping
{
return Ok(());
}
continue;
}
let status = match exit {
Exit::Stopped => return Ok(()),
Exit::NotUp => continue,
Exit::Restart => {
let restore = shared.restore.lock().unwrap().take();
if let Some(name) = restore {
@@ -162,6 +207,10 @@ pub fn run(dir: &Path) -> Result<(), String> {
Err(e) => note(&shared, "ERROR", &format!("Could not restore {name}: {e}")),
}
}
let pending = shared.update.lock().unwrap().pending.take();
if let Some((to, timeout)) = pending {
watch = switch(&shared, dir, &config, &to, timeout);
}
continue;
}
Exit::Crashed(status) => status,
@@ -206,6 +255,154 @@ pub fn run(dir: &Path) -> Result<(), String> {
}
}
/// With the game stopped: backs the world up, points `current` at the release `to` and checks that its game
/// loads on this system. What to watch while it starts; `None` when it stayed on the release it was on.
fn switch(
shared: &Shared,
dir: &Path,
config: &Config,
to: &str,
timeout: Duration,
) -> Option<Watch> {
let Some(install) = shared.install.as_ref() else {
shared.finish_update(
false,
"Not updated: this horizon-server wasn't installed".into(),
);
return None;
};
let Some(from) = install.current() else {
shared.finish_update(false, "Not updated: `current` points at no release".into());
return None;
};
let made = backup::file_name(std::time::SystemTime::now(), Some(&update::backup_name(to)))
.and_then(|file| backup::create(dir, config, &file).map(|_| file));
let backup = match made {
Ok(file) => file,
Err(e) => {
shared.finish_update(false, format!("Stayed on {from}: the backup failed: {e}"));
return None;
}
};
note(
shared,
"INFO",
&format!(
"Backed up the world to {}/{backup}; switching from {from} to {to}",
backup::DIR
),
);
let previous = install.previous();
if let Err(e) = install.switch(to) {
shared.finish_update(false, format!("Stayed on {from}: {e}"));
return None;
}
if let Err(e) =
engine::program_in(config, dir, Some(install)).and_then(|p| engine::check_starts(&p))
{
let back = install
.switch_back(&from, previous.as_deref())
.err()
.map_or(String::new(), |e| format!(" ({e})"));
shared.finish_update(false, format!("Stayed on {from}{back}: {e}"));
return None;
}
shared.update.lock().unwrap().busy = Some(format!("Starting the game on {to}"));
Some(Watch {
from,
to: to.to_owned(),
previous,
backup,
timeout,
deadline: Instant::now() + timeout,
})
}
/// Undoes a switch whose game never came up: `current` back on the release before, and the world as it
/// was just before the switch (the one the new game left is kept in backups/).
fn roll_back(shared: &Shared, dir: &Path, config: &Config, w: &Watch, reason: &str) {
note(
shared,
"ERROR",
&format!(
"The game on {} didn't come up ({reason}): going back to {}",
w.to, w.from
),
);
let Some(install) = shared.install.as_ref() else {
return;
};
if let Err(e) = install.switch_back(&w.from, w.previous.as_deref()) {
shared.finish_update(
false,
format!(
"{} didn't come up ({reason}), and going back to {} failed: {e}",
w.to, w.from
),
);
return;
}
let world = match backup::restore(dir, config, &w.backup) {
Ok(Some(old)) => format!(
"; the world is back as it was before the switch (the one {} left is in {})",
w.to,
old.display()
),
Ok(None) => "; the world is back as it was before the switch".into(),
Err(e) => format!("; the world couldn't be put back from {}: {e}", w.backup),
};
shared.finish_update(
false,
format!(
"Rolled back to {}: {} didn't come up ({reason}){world}",
w.from, w.to
),
);
}
/// The game is up: it logged `Done` and answered a status request (the sampler's, for this process).
fn up(shared: &Shared) -> bool {
let state = shared.state.lock().unwrap();
state.phase == Phase::Running
&& state.engine_pid.is_some()
&& state.sample["pid"].as_u64() == state.engine_pid.map(u64::from)
&& !state.sample["game"].is_null()
}
/// Looks for a newer release a minute after the start and then every 6 hours (installed servers only;
/// `HORIZON_NO_UPDATE_CHECK=1` turns it off). It only tells: updating stays the owner's choice.
fn check_updates(shared: Arc<Shared>) {
let Some(install) = shared.install.clone() else {
return;
};
if std::env::var_os("HORIZON_NO_UPDATE_CHECK").is_some_and(|v| !v.is_empty() && v != "0") {
return;
}
let _ = thread::Builder::new()
.name("update-check".into())
.spawn(move || {
thread::sleep(Duration::from_secs(60));
loop {
if let (Ok(release), Some(current)) = (update::release(None), install.current()) {
let newer = update::newer(&release.tag, &current).then_some(release.tag);
let told = shared.update.lock().unwrap().available.clone();
if let Some(tag) = newer.as_ref().filter(|t| told.as_ref() != Some(*t)) {
note(
&shared,
"INFO",
&format!(
"Horizon {tag} is out (this server runs {current}): `horizon-server update` \
installs it, and goes back by itself if it doesn't start"
),
);
}
shared.update.lock().unwrap().available = newer;
}
thread::sleep(Duration::from_secs(6 * 3600));
}
});
}
/// The wait before the restart that follows the `crashes`th recent crash.
fn backoff(crashes: usize) -> Duration {
let doublings = u32::try_from(crashes.saturating_sub(1))
@@ -309,6 +506,7 @@ fn sample(shared: Arc<Shared>) {
"players": game.as_ref().and_then(|g| g["players"].as_array().map(Vec::len)),
}));
shared.state.lock().unwrap().sample = json!({
"pid": pid,
"rss_bytes": rss,
"cpu_percent": cpu_percent,
"game": game,
@@ -358,7 +556,12 @@ fn schedule_backups(shared: Arc<Shared>) {
}
/// Runs one engine process to its end: console lines in, the log out, and a clean `stop` when asked.
fn supervise(shared: &Arc<Shared>, child: &mut Child, console: &mpsc::Receiver<String>) -> Exit {
fn supervise(
shared: &Arc<Shared>,
child: &mut Child,
console: &mpsc::Receiver<String>,
watch: &mut Option<Watch>,
) -> Exit {
if let Some(out) = child.stdout.take() {
pump(shared, out, false);
}
@@ -368,8 +571,27 @@ fn supervise(shared: &Arc<Shared>, child: &mut Child, console: &mpsc::Receiver<S
let mut stdin = child.stdin.take();
let mut stopping: Option<Instant> = None;
let mut restart = false;
let mut not_up = false;
let mut handled = signals::requests();
loop {
if let Some(w) = watch.as_ref() {
if up(shared) {
shared.finish_update(
true,
format!("Now on {} (was {}): the game is up", w.to, w.from),
);
*watch = None;
} else if Instant::now() > w.deadline && stopping.is_none() {
note(
shared,
"WARN",
&format!("The game on {} isn't up in time: stopping it", w.to),
);
not_up = true;
stopping = Some(Instant::now());
let _ = child.kill();
}
}
while let Ok(line) = console.try_recv() {
let command = line.trim().trim_start_matches('/');
if command == "stop" && stopping.is_none() {
@@ -419,6 +641,7 @@ fn supervise(shared: &Arc<Shared>, child: &mut Child, console: &mpsc::Receiver<S
stopping = Some(Instant::now());
}
match child.try_wait() {
Ok(Some(_)) if not_up => return Exit::NotUp,
Ok(Some(_)) if restart => return Exit::Restart,
Ok(Some(status)) if stopping.is_some() || status.success() => return Exit::Stopped,
Ok(Some(status)) => return Exit::Crashed(status),
@@ -447,6 +670,142 @@ mod tests {
assert_eq!(waits, [5, 10, 20, 40, 80, 160, 300, 300]);
}
/// An install with releases whose game exits with `code` on `--help`, and a server folder with a world.
#[cfg(unix)]
fn installed(name: &str, releases: &[(&str, i32)]) -> (Arc<Shared>, std::path::PathBuf) {
use std::os::unix::fs::PermissionsExt;
let root =
std::env::temp_dir().join(format!("horizon-server-sw-{name}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&root);
let root = {
std::fs::create_dir_all(&root).unwrap();
std::fs::canonicalize(&root).unwrap()
};
for (tag, code) in releases {
let release = root.join("releases").join(tag);
std::fs::create_dir_all(&release).unwrap();
std::fs::write(release.join("horizon-server"), "").unwrap();
let engine = release.join("horizon-engine");
std::fs::write(&engine, format!("#!/bin/sh\nexit {code}\n")).unwrap();
std::fs::set_permissions(&engine, std::fs::Permissions::from_mode(0o755)).unwrap();
}
std::os::unix::fs::symlink(
root.join("releases").join(releases[0].0),
root.join("current"),
)
.unwrap();
let dir = root.join("server");
std::fs::create_dir_all(dir.join("world")).unwrap();
std::fs::write(dir.join("world/level.dat"), "before").unwrap();
let install = update::Install { root };
(Shared::with_install(&dir, Some(install)), dir)
}
#[cfg(unix)]
#[test]
fn a_switch_backs_up_then_rolls_back_world_and_release() {
let (shared, dir) = installed("rollback", &[("v1", 0), ("v2", 0)]);
let install = shared.install.clone().unwrap();
let config = Config::default();
let watch = switch(&shared, &dir, &config, "v2", Duration::from_secs(60)).unwrap();
assert_eq!((watch.from.as_str(), watch.to.as_str()), ("v1", "v2"));
assert!(watch.backup.ends_with("-before-v2.zip"), "{}", watch.backup);
assert!(dir.join(backup::DIR).join(&watch.backup).is_file());
assert_eq!(install.current().as_deref(), Some("v2"));
assert_eq!(
shared.update.lock().unwrap().busy.as_deref(),
Some("Starting the game on v2")
);
// The new game wrote to the world, then never came up.
std::fs::write(dir.join("world/level.dat"), "after").unwrap();
roll_back(
&shared,
&dir,
&config,
&watch,
"the game stopped (exit status: 1)",
);
assert_eq!(install.current().as_deref(), Some("v1"));
// `rollback` won't go to the release that failed.
assert_eq!(install.previous(), None);
assert_eq!(
std::fs::read_to_string(dir.join("world/level.dat")).unwrap(),
"before"
);
let update = shared.update.lock().unwrap();
assert!(!update.ok && update.busy.is_none() && update.finished == 1);
let last = update.last.as_deref().unwrap();
assert!(
last.starts_with("Rolled back to v1: v2 didn't come up (the game stopped (exit status: 1)); the world is back"),
"{last}"
);
// The world the new game left is kept.
let kept = last.rsplit_once("is in ").unwrap().1.trim_end_matches(')');
assert_eq!(
std::fs::read_to_string(Path::new(kept).join("level.dat")).unwrap(),
"after"
);
std::fs::remove_dir_all(&install.root).unwrap();
}
#[cfg(unix)]
#[test]
fn a_game_that_cant_load_is_never_switched_to() {
let (shared, dir) = installed("unloadable", &[("v1", 0), ("v2", 127)]);
let install = shared.install.clone().unwrap();
assert!(
switch(
&shared,
&dir,
&Config::default(),
"v2",
Duration::from_secs(60)
)
.is_none()
);
assert_eq!(install.current().as_deref(), Some("v1"));
let update = shared.update.lock().unwrap();
assert!(!update.ok);
assert!(
update
.last
.as_deref()
.unwrap()
.starts_with("Stayed on v1: the game (")
);
std::fs::remove_dir_all(&install.root).unwrap();
}
#[test]
fn up_means_done_and_a_status_reply_from_this_game() {
let shared = Shared::with_install(Path::new("."), None);
let set = |phase, pid: Option<u32>, sample: serde_json::Value| {
let mut state = shared.state.lock().unwrap();
(state.phase, state.engine_pid, state.sample) = (phase, pid, sample);
};
set(
Phase::Starting,
Some(7),
json!({ "pid": 7, "game": { "tps": 20.0 } }),
);
assert!(!up(&shared));
set(Phase::Running, Some(7), json!({ "pid": 7, "game": null }));
assert!(!up(&shared));
// A sample of the game before the restart doesn't count.
set(
Phase::Running,
Some(8),
json!({ "pid": 7, "game": { "tps": 20.0 } }),
);
assert!(!up(&shared));
set(
Phase::Running,
Some(8),
json!({ "pid": 8, "game": { "tps": 20.0 } }),
);
assert!(up(&shared));
}
#[test]
fn one_supervisor_per_folder() {
let dir = std::env::temp_dir().join(format!("horizon-server-lock-{}", std::process::id()));
+763
View File
@@ -0,0 +1,763 @@
//! Updates and rollbacks (docs/server.md, section 7) for a `horizon-server` installed by install.sh or
//! install.ps1: each release in `<dir>/releases/<tag>`, `<dir>/current` pointing at the one in use, and
//! `<dir>/previous` naming the one before it. An update downloads the newest release from GitHub, checks it
//! against the SHA-256 GitHub publishes for it, unpacks it beside the others and switches `current`; the
//! supervisor then restarts the game on it and switches back by itself if the new game doesn't come up.
use crate::{api::Client, backup, config::Config, engine, supervisor};
use serde_json::{Value, json};
use std::{
io::Read,
path::{Path, PathBuf},
process::Command,
time::{Duration, Instant},
};
const REPO: &str = "ludovic111/horizon";
/// The release archives for this computer, most specific first: on Linux the server build (no ALSA or
/// udev), else the full game (releases before the server build).
const ASSETS: &[&str] = if cfg!(all(target_os = "linux", target_arch = "x86_64")) {
&[
"horizon-server-linux-x86_64.tar.gz",
"horizon-linux-x86_64.tar.gz",
]
} else if cfg!(all(target_os = "macos", target_arch = "aarch64")) {
&["horizon-macos-arm64.zip"]
} else if cfg!(all(target_os = "windows", target_arch = "x86_64")) {
&["horizon-windows-x64.zip"]
} else {
&[]
};
fn exe(name: &str) -> String {
format!("{name}{}", std::env::consts::EXE_SUFFIX)
}
/// The installer's folder: `releases/`, `current`, `previous` and `game-files/`.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Install {
pub root: PathBuf,
}
impl Install {
/// The install this program runs from, if it runs from one.
pub fn find() -> Option<Self> {
Self::around(&std::env::current_exe().ok()?)
}
/// The install holding `program` (`<root>/releases/<tag>/horizon-server`, reached directly or through
/// `current`).
pub fn around(program: &Path) -> Option<Self> {
let program = std::fs::canonicalize(program).ok()?;
let releases = program.parent()?.parent()?;
if releases.file_name()? != "releases" {
return None;
}
let root = releases.parent()?.to_path_buf();
root.join("current").is_dir().then_some(Self { root })
}
/// The install, or why there is none to update.
pub fn required() -> Result<Self, String> {
Self::find().ok_or_else(|| {
"this horizon-server wasn't installed by install.sh or install.ps1 (no releases/ and current \
around it): update it the way you installed it"
.into()
})
}
pub fn release(&self, tag: &str) -> PathBuf {
self.root.join("releases").join(tag)
}
/// Where the game programs in use are: `current`, so a switch applies at the next start.
pub fn programs(&self) -> PathBuf {
self.root.join("current")
}
/// The release in use.
pub fn current(&self) -> Option<String> {
let target = std::fs::canonicalize(self.root.join("current")).ok()?;
Some(target.file_name()?.to_string_lossy().into_owned())
}
/// The release used before the current one, if it is still there.
pub fn previous(&self) -> Option<String> {
let tag = std::fs::read_to_string(self.root.join("previous")).ok()?;
let tag = tag.trim();
(!tag.is_empty() && self.release(tag).is_dir()).then(|| tag.to_owned())
}
/// Points `current` at the release `tag`, and remembers the one it pointed at as `previous`.
pub fn switch(&self, tag: &str) -> Result<(), String> {
let target = self.release(tag);
if !target.join(exe("horizon-server")).is_file() {
return Err(format!("{} holds no horizon-server", target.display()));
}
if let Some(old) = self.current().filter(|old| old != tag) {
std::fs::write(self.root.join("previous"), format!("{old}\n"))
.map_err(|e| format!("{}: {e}", self.root.join("previous").display()))?;
}
point(&self.root.join("current"), &target)
.map_err(|e| format!("switching current to {tag}: {e}"))
}
/// Undoes a [`Install::switch`]: `current` back on `from` and `previous` as it was before, so a later
/// rollback doesn't go to the release that failed.
pub fn switch_back(&self, from: &str, previous: Option<&str>) -> Result<(), String> {
point(&self.root.join("current"), &self.release(from))
.map_err(|e| format!("switching current back to {from}: {e}"))?;
let file = self.root.join("previous");
match previous {
Some(tag) => std::fs::write(&file, format!("{tag}\n")),
None => std::fs::remove_file(&file).or_else(|e| {
if e.kind() == std::io::ErrorKind::NotFound {
Ok(())
} else {
Err(e)
}
}),
}
.map_err(|e| format!("{}: {e}", file.display()))
}
}
/// Makes `link` point at the folder `target`, replacing what it pointed at.
#[cfg(unix)]
fn point(link: &Path, target: &Path) -> std::io::Result<()> {
// A new link renamed over the old one: `current` always points at a whole release.
let fresh = link.with_extension("switching");
let _ = std::fs::remove_file(&fresh);
std::os::unix::fs::symlink(target, &fresh)?;
std::fs::rename(&fresh, link)
}
/// Makes `link` a junction to `target` (install.ps1's links: they need no admin rights).
#[cfg(windows)]
fn point(link: &Path, target: &Path) -> std::io::Result<()> {
if link.exists() || link.symlink_metadata().is_ok() {
std::fs::remove_dir(link)?;
}
let status = Command::new("cmd")
.arg("/C")
.arg("mklink")
.arg("/J")
.arg(link)
.arg(target)
.stdout(std::process::Stdio::null())
.status()?;
if status.success() {
Ok(())
} else {
Err(std::io::Error::other(format!(
"mklink /J failed ({status})"
)))
}
}
/// A release on GitHub: its tag and the archive for this computer.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Release {
pub tag: String,
pub asset: String,
pub url: String,
pub sha256: String,
}
/// The newest release (or `tag`), from GitHub's API.
pub fn release(tag: Option<&str>) -> Result<Release, String> {
let url = match tag {
Some(tag) => format!("https://api.github.com/repos/{REPO}/releases/tags/{tag}"),
None => format!("https://api.github.com/repos/{REPO}/releases/latest"),
};
let text = ureq::get(&url)
.set("Accept", "application/vnd.github+json")
.set("User-Agent", "horizon-server")
.call()
.map_err(|e| match e {
ureq::Error::Status(404, _) => format!("no release {} on GitHub", tag.unwrap_or("")),
e => format!("GitHub didn't answer: {e}"),
})?
.into_string()
.map_err(|e| format!("GitHub's answer: {e}"))?;
pick(&serde_json::from_str(&text).map_err(|e| format!("GitHub's answer: {e}"))?)
}
/// The archive for this computer in a release's JSON, with the SHA-256 GitHub publishes for it.
fn pick(release: &Value) -> Result<Release, String> {
let tag = release["tag_name"]
.as_str()
.ok_or("GitHub's answer has no tag")?;
let assets = release["assets"].as_array().cloned().unwrap_or_default();
let asset = ASSETS
.iter()
.find_map(|name| assets.iter().find(|a| a["name"] == *name))
.ok_or_else(|| {
format!(
"release {tag} has no archive for this computer ({})",
ASSETS.join(", ")
)
})?;
let name = asset["name"].as_str().unwrap_or_default();
let sha256 = asset["digest"]
.as_str()
.and_then(|d| d.strip_prefix("sha256:"))
.ok_or_else(|| format!("release {tag} publishes no SHA-256 for {name}"))?;
Ok(Release {
tag: tag.to_owned(),
asset: name.to_owned(),
url: asset["browser_download_url"]
.as_str()
.unwrap_or_default()
.to_owned(),
sha256: sha256.to_ascii_lowercase(),
})
}
/// Whether the release `tag` is newer than `than`: releases are tagged `vYYYY.MM.DD[.N]`, compared
/// number by number. A tag that isn't one (an archive installed by hand) is never older than a release.
pub fn newer(tag: &str, than: &str) -> bool {
let numbers = |t: &str| -> Option<Vec<u64>> {
t.strip_prefix('v')?
.split('.')
.map(|n| n.parse().ok())
.collect()
};
match (numbers(tag), numbers(than)) {
(Some(a), Some(b)) => a > b,
(Some(_), None) => true,
_ => false,
}
}
/// Downloads a release's archive into `<root>/downloads/` and checks its SHA-256; the archive's path.
pub fn download(install: &Install, release: &Release) -> Result<PathBuf, String> {
use sha2::Digest;
let folder = install.root.join("downloads");
std::fs::create_dir_all(&folder).map_err(|e| format!("{}: {e}", folder.display()))?;
let path = folder.join(&release.asset);
let part = folder.join(format!("{}.part", release.asset));
let result = (|| {
let mut reader = ureq::get(&release.url)
.set("User-Agent", "horizon-server")
.call()
.map_err(|e| format!("downloading {}: {e}", release.asset))?
.into_reader();
let mut file =
std::fs::File::create(&part).map_err(|e| format!("{}: {e}", part.display()))?;
let mut hasher = sha2::Sha256::new();
let mut buffer = vec![0; 1 << 16];
loop {
let n = reader
.read(&mut buffer)
.map_err(|e| format!("downloading {}: {e}", release.asset))?;
if n == 0 {
break;
}
hasher.update(&buffer[..n]);
std::io::Write::write_all(&mut file, &buffer[..n]).map_err(|e| e.to_string())?;
}
let got: String = hasher
.finalize()
.iter()
.map(|b| format!("{b:02x}"))
.collect();
if got != release.sha256 {
return Err(format!(
"{} doesn't match its SHA-256 (got {got}, expected {}): not installed",
release.asset, release.sha256
));
}
std::fs::rename(&part, &path).map_err(|e| e.to_string())
})();
if result.is_err() {
let _ = std::fs::remove_file(&part);
}
result.map(|()| path)
}
/// Unpacks a release archive into `<root>/releases/<tag>`, with its `.local/horizon-client` linked to the
/// install's prepared game files (which updates keep), as the installers do. It doesn't switch to it.
pub fn unpack(install: &Install, archive: &Path, tag: &str) -> Result<PathBuf, String> {
if tag.is_empty() || tag.contains(['/', '\\']) || tag.starts_with('.') {
return Err(format!("{tag:?} isn't a release name"));
}
let target = install.release(tag);
if install.current().as_deref() == Some(tag) {
return Err(format!("{tag} is the release in use"));
}
let scratch = install
.root
.join("releases")
.join(format!(".{tag}.unpacking"));
let _ = std::fs::remove_dir_all(&scratch);
std::fs::create_dir_all(&scratch).map_err(|e| format!("{}: {e}", scratch.display()))?;
let result = (|| {
let name = archive.to_string_lossy();
if name.ends_with(".zip") {
let file =
std::fs::File::open(archive).map_err(|e| format!("{}: {e}", archive.display()))?;
zip::ZipArchive::new(file)
.and_then(|mut z| z.extract(&scratch))
.map_err(|e| format!("{}: {e}", archive.display()))?;
} else if name.ends_with(".tar.gz") || name.ends_with(".tgz") {
// tar is on every Linux and macOS, and on Windows 10 and later.
let status = Command::new("tar")
.arg("-xzf")
.arg(archive)
.arg("-C")
.arg(&scratch)
.status()
.map_err(|e| format!("tar: {e}"))?;
if !status.success() {
return Err(format!(
"tar couldn't unpack {} ({status})",
archive.display()
));
}
} else {
return Err(format!(
"{} is neither a .tar.gz nor a .zip",
archive.display()
));
}
let source = find_program(&scratch, &exe("horizon-server"))
.and_then(|p| p.parent().map(Path::to_path_buf))
.ok_or_else(|| format!("{} holds no horizon-server", archive.display()))?;
#[cfg(unix)]
for name in [
"horizon-server",
"horizon-engine",
"horizon-client",
"horizon-prepare",
] {
use std::os::unix::fs::PermissionsExt;
let path = source.join(name);
if path.is_file() {
let _ = std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755));
}
}
if target.exists() {
std::fs::remove_dir_all(&target).map_err(|e| format!("{}: {e}", target.display()))?;
}
std::fs::rename(&source, &target).map_err(|e| format!("{}: {e}", target.display()))?;
let game_files = install.root.join("game-files/horizon-client");
std::fs::create_dir_all(&game_files).map_err(|e| e.to_string())?;
let local = target.join(".local");
std::fs::create_dir_all(&local).map_err(|e| e.to_string())?;
let link = local.join("horizon-client");
if link.symlink_metadata().is_err() {
point(&link, &game_files).map_err(|e| format!("{}: {e}", link.display()))?;
}
Ok(target.clone())
})();
let _ = std::fs::remove_dir_all(&scratch);
result
}
/// The first file called `name` under `dir`, breadth first.
fn find_program(dir: &Path, name: &str) -> Option<PathBuf> {
let mut queue = std::collections::VecDeque::from([dir.to_path_buf()]);
while let Some(folder) = queue.pop_front() {
let mut entries: Vec<_> = std::fs::read_dir(&folder).ok()?.flatten().collect();
entries.sort_by_key(|e| e.file_name());
for entry in entries {
let path = entry.path();
let kind = entry.file_type().ok()?;
if kind.is_file() && entry.file_name() == name {
return Some(path);
}
if kind.is_dir() {
queue.push_back(path);
}
}
}
None
}
/// A backup name for the world before a switch to `tag` (backup names take letters, digits, - and _).
pub fn backup_name(tag: &str) -> String {
let clean: String = tag
.chars()
.map(|c| if c.is_ascii_alphanumeric() { c } else { '-' })
.take(48)
.collect();
format!("before-{clean}")
}
/// `update [--check] [--version TAG] [--archive FILE --version TAG] [--timeout SECONDS]`.
pub fn command(dir: &Path, args: &[String]) -> Result<(), String> {
let mut check = false;
let mut version = None;
let mut archive = None;
let mut timeout = crate::api::UPDATE_TIMEOUT;
let mut it = args.iter();
while let Some(arg) = it.next() {
let mut value = || {
it.next()
.cloned()
.ok_or_else(|| format!("{arg} needs a value"))
};
match arg.as_str() {
"--check" => check = true,
"--version" => version = Some(value()?),
"--archive" => archive = Some(PathBuf::from(value()?)),
"--timeout" => timeout = seconds(&value()?)?,
other => return Err(format!("update: unknown option {other}")),
}
}
let install = Install::required()?;
let current = install
.current()
.ok_or("the installer's `current` points at no release")?;
let (tag, archive, downloaded) = match archive {
Some(_) if check => return Err("--check looks at GitHub's releases: drop --archive".into()),
Some(archive) => {
let tag = version
.ok_or("--archive needs --version NAME: the release name to install it as")?;
(tag, archive, false)
}
None => {
let release = release(version.as_deref())?;
if check {
if newer(&release.tag, &current) {
println!(
"Horizon {} is out; this server runs {current}. `horizon-server update` installs it.",
release.tag
);
} else {
println!(
"Up to date: {current} (the newest release is {}).",
release.tag
);
}
return Ok(());
}
if version.is_none() && !newer(&release.tag, &current) {
println!("Already on the newest release ({current}).");
return Ok(());
}
println!("Downloading Horizon {} ({})...", release.tag, release.asset);
let archive = download(&install, &release)?;
(release.tag, archive, true)
}
};
if tag == current {
println!("Already on {tag}.");
return Ok(());
}
let unpacked = unpack(&install, &archive, &tag);
if downloaded {
let _ = std::fs::remove_file(&archive);
}
unpacked?;
switch_to(dir, &install, &tag, timeout)
}
/// `rollback [--timeout SECONDS]`: back to the release used before the current one.
pub fn rollback(dir: &Path, args: &[String]) -> Result<(), String> {
let timeout = match args {
[] => crate::api::UPDATE_TIMEOUT,
[flag, value] if flag == "--timeout" => seconds(value)?,
_ => return Err("rollback takes only --timeout SECONDS".into()),
};
let install = Install::required()?;
let previous = install.previous().ok_or(
"no release to go back to: this install hasn't been updated, or the one before is gone",
)?;
switch_to(dir, &install, &previous, timeout)
}
fn seconds(text: &str) -> Result<Duration, String> {
text.parse()
.ok()
.filter(|&s| s > 0)
.map(Duration::from_secs)
.ok_or_else(|| format!("--timeout needs a number of seconds (got {text:?})"))
}
/// Switches the install to the release `tag`. A running server restarts on it through its supervisor, which
/// goes back by itself if the game isn't up within `timeout`; a stopped one is switched and its game checked.
/// Either way the world is backed up first.
fn switch_to(dir: &Path, install: &Install, tag: &str, timeout: Duration) -> Result<(), String> {
let Some(client) = Client::open(dir)? else {
return switch_stopped(dir, install, tag);
};
let finished = |status: &Value| status["update"]["finished"].as_u64().unwrap_or(0);
let before = finished(&client.call("GET", "/api/v1/status", None)?);
client
.call(
"POST",
"/api/v1/update",
Some(&json!({ "tag": tag, "timeout": timeout.as_secs() })),
)
.map_err(|e| {
if e == "no such endpoint" {
"the running server is older than updates: stop it, then run `horizon-server update` again".into()
} else {
e
}
})?;
print!("Restarting the server on {tag}");
// The backup, the stop, the start and, if it fails, the way back.
let deadline = Instant::now() + timeout + Duration::from_secs(3600);
loop {
std::thread::sleep(Duration::from_millis(500));
let status = client.call("GET", "/api/v1/status", None)?;
if finished(&status) > before {
let update = &status["update"];
let last = update["last"].as_str().unwrap_or_default().to_owned();
if update["ok"] == true {
println!("\n{last}");
return Ok(());
}
// Rolled back (or stayed): say so once the game is up again on the release it went back to.
let mut phase = status["phase"].as_str().unwrap_or_default().to_owned();
let until = Instant::now() + crate::api::UPDATE_TIMEOUT;
while phase != "running" && phase != "stopping" && Instant::now() < until {
std::thread::sleep(Duration::from_millis(500));
print!(".");
let _ = std::io::Write::flush(&mut std::io::stdout());
phase = client.call("GET", "/api/v1/status", None)?["phase"]
.as_str()
.unwrap_or_default()
.to_owned();
}
println!();
return Err(if phase == "running" {
format!("{last}. The game is up again.")
} else {
last
});
}
if Instant::now() > deadline {
println!();
return Err(
"no word from the server about the update; `horizon-server logs` shows where it is"
.into(),
);
}
print!(".");
let _ = std::io::Write::flush(&mut std::io::stdout());
}
}
/// The switch for a stopped server (or outside any server folder): the world backed up, `current` moved,
/// and the new game checked to load on this system, else switched back.
fn switch_stopped(dir: &Path, install: &Install, tag: &str) -> Result<(), String> {
let from = install
.current()
.ok_or("the installer's `current` points at no release")?;
let server = dir.join(crate::config::FILE).is_file();
let _lock = if server {
Some(supervisor::lock(dir)?)
} else {
None
};
let config = if server {
Config::load(dir)?
} else {
Config::default()
};
let backed_up = if server {
let file = backup::file_name(std::time::SystemTime::now(), Some(&backup_name(tag)))?;
backup::create(dir, &config, &file)?;
format!(" The world is backed up in {}/{file}.", backup::DIR)
} else {
String::new()
};
let previous = install.previous();
install.switch(tag)?;
if let Err(e) =
engine::program_in(&config, dir, Some(install)).and_then(|p| engine::check_starts(&p))
{
install.switch_back(&from, previous.as_deref())?;
return Err(format!("stayed on {from}: {e}"));
}
println!(
"Switched from {from} to {tag}.{backed_up}{} `horizon-server rollback` goes back to {from}.",
if server {
" The server is stopped: `horizon-server start` runs the new version."
} else {
""
}
);
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
fn scratch(name: &str) -> PathBuf {
let dir = std::env::temp_dir().join(format!(
"horizon-server-update-{name}-{}",
std::process::id()
));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
dir
}
/// An install as install.sh leaves it, with releases `tags`, the first in use.
#[cfg(unix)]
fn installed(name: &str, tags: &[&str]) -> Install {
// Canonical, as `around` sees it (macOS's temporary folder is behind a link).
let root = std::fs::canonicalize(scratch(name)).unwrap();
for tag in tags {
let release = root.join("releases").join(tag);
std::fs::create_dir_all(&release).unwrap();
std::fs::write(release.join("horizon-server"), tag).unwrap();
}
std::os::unix::fs::symlink(root.join("releases").join(tags[0]), root.join("current"))
.unwrap();
Install { root }
}
#[test]
fn tags_compare_by_number() {
assert!(newer("v2026.10.19", "v2026.10.18.27"));
assert!(newer("v2026.10.18.27", "v2026.10.18.9"));
assert!(newer("v2026.10.19.1", "v2026.10.19"));
assert!(!newer("v2026.10.19", "v2026.10.19"));
assert!(!newer("v2026.10.18", "v2026.10.19"));
// An archive installed by hand: any release is newer, and it is never newer than one.
assert!(newer("v2026.10.19", "local"));
assert!(!newer("local", "v2026.10.19"));
}
#[test]
fn the_archive_for_this_computer_is_picked_with_its_digest() {
let asset = |name: &str| {
json!({
"name": name,
"browser_download_url": format!("https://example.invalid/{name}"),
"digest": "sha256:ABCDEF",
})
};
let release = json!({
"tag_name": "v2026.10.19",
"assets": ASSETS.iter().rev().map(|n| asset(n)).chain([asset("other.zip")]).collect::<Vec<_>>(),
});
if let Some(first) = ASSETS.first() {
let picked = pick(&release).unwrap();
assert_eq!(picked.tag, "v2026.10.19");
// The most specific archive (Linux: the server build), even when listed last.
assert_eq!(picked.asset, *first);
assert_eq!(picked.sha256, "abcdef");
let undigested = json!({ "tag_name": "v1", "assets": [{ "name": first, "browser_download_url": "x" }] });
assert!(pick(&undigested).unwrap_err().contains("no SHA-256"));
}
assert!(
pick(&json!({ "tag_name": "v1", "assets": [asset("other.zip")] }))
.unwrap_err()
.contains("no archive for this computer")
);
}
#[test]
fn backup_names_are_valid() {
let name = backup_name("v2026.10.19");
assert_eq!(name, "before-v2026-10-19");
crate::backup::file_name(std::time::SystemTime::now(), Some(&name)).unwrap();
}
#[cfg(unix)]
#[test]
fn switching_keeps_the_previous_release() {
let install = installed("switch", &["v1", "v2"]);
let exe = install.root.join("current/horizon-server");
assert_eq!(Install::around(&exe), Some(install.clone()));
assert_eq!(
Install::around(&install.release("v2").join("horizon-server")),
Some(install.clone())
);
assert_eq!(install.current().as_deref(), Some("v1"));
assert_eq!(install.previous(), None);
install.switch("v2").unwrap();
assert_eq!(install.current().as_deref(), Some("v2"));
assert_eq!(install.previous().as_deref(), Some("v1"));
assert_eq!(std::fs::read_to_string(&exe).unwrap(), "v2");
// Undoing it leaves `previous` as it was.
install.switch_back("v1", None).unwrap();
assert_eq!(install.current().as_deref(), Some("v1"));
assert_eq!(install.previous(), None);
install.switch("v2").unwrap();
// Rolling back is a switch to the previous release, which then becomes the one to go back to.
install.switch("v1").unwrap();
assert_eq!(install.current().as_deref(), Some("v1"));
assert_eq!(install.previous().as_deref(), Some("v2"));
// Switching to the release in use changes nothing; to a missing one is refused.
install.switch("v1").unwrap();
assert_eq!(install.previous().as_deref(), Some("v2"));
assert!(
install
.switch("v3")
.unwrap_err()
.contains("no horizon-server")
);
assert_eq!(install.current().as_deref(), Some("v1"));
std::fs::remove_dir_all(&install.root).unwrap();
}
#[cfg(unix)]
#[test]
fn an_archive_unpacks_beside_the_others_with_the_game_files_linked() {
let install = installed("unpack", &["v1"]);
let source = scratch("unpack-source");
let inner = source.join("horizon-v2");
std::fs::create_dir_all(&inner).unwrap();
std::fs::write(inner.join("horizon-server"), "v2").unwrap();
std::fs::write(inner.join("horizon-engine"), "engine").unwrap();
let archive = source.join("horizon-server-linux-x86_64.tar.gz");
let status = Command::new("tar")
.arg("-czf")
.arg(&archive)
.arg("-C")
.arg(&source)
.arg("horizon-v2")
.status()
.unwrap();
assert!(status.success());
let release = unpack(&install, &archive, "v2").unwrap();
assert_eq!(release, install.release("v2"));
assert_eq!(
std::fs::read_to_string(release.join("horizon-engine")).unwrap(),
"engine"
);
{
use std::os::unix::fs::PermissionsExt;
let mode = std::fs::metadata(release.join("horizon-engine"))
.unwrap()
.permissions()
.mode();
assert_eq!(mode & 0o111, 0o111);
}
assert_eq!(
std::fs::canonicalize(release.join(".local/horizon-client")).unwrap(),
std::fs::canonicalize(install.root.join("game-files/horizon-client")).unwrap()
);
// Not switched yet; nothing left behind; the release in use can't be unpacked over.
assert_eq!(install.current().as_deref(), Some("v1"));
assert!(!install.root.join("releases/.v2.unpacking").exists());
assert!(
unpack(&install, &archive, "v1")
.unwrap_err()
.contains("in use")
);
assert!(unpack(&install, &archive, "../x").is_err());
let empty = source.join("empty.zip");
zip::ZipWriter::new(std::fs::File::create(&empty).unwrap())
.finish()
.unwrap();
assert!(
unpack(&install, &empty, "v3")
.unwrap_err()
.contains("holds no horizon-server")
);
assert!(!install.release("v3").exists());
std::fs::remove_dir_all(&install.root).unwrap();
std::fs::remove_dir_all(&source).unwrap();
}
}
+473 -4
View File
@@ -24,8 +24,8 @@ use super::{
decode_noise,
density::DensityRegistry,
material_condition::{
MaterialConditionRegistry, MaterialRule, MaterialRuleError, MaterialRuleLibrary,
MaterialSurfaceQueries,
MaterialConditionRegistry, MaterialConditions, MaterialDensitySources, MaterialRule,
MaterialRuleError, MaterialRuleLibrary, MaterialSurfaceQueries,
},
material_extensions::{ExtensionBlocks, ExtensionNoises, MaterialExtensions},
material_stage::{SurfaceInputs, TopMaterialInputs, build_surface},
@@ -495,6 +495,20 @@ impl GeneratorData {
&self.states
}
/// Biome identifiers in registry (identifier-sorted) order.
pub fn configured_feature_json(&self, name: &str) -> Option<&str> {
self.feature_resources
.as_ref()?
.features
.get(name)
.map(String::as_str)
}
pub fn placed_feature_json(&self, name: &str) -> Option<&str> {
self.feature_resources
.as_ref()?
.placed
.get(name)
.map(String::as_str)
}
pub fn biome_names(&self) -> &[Arc<str>] {
&self.biome_names
}
@@ -1476,6 +1490,21 @@ trait SeededPipeline: Send + Sync {
) -> Result<DecorateStats, GeneratorError>;
fn data(&self) -> &Arc<GeneratorData>;
fn sea_level(&self) -> i32;
fn surface_sample(
&self,
worker: &mut WorkerState,
x: i32,
z: i32,
spans: Option<&mut Vec<(i32, i32, BlockStateId)>>,
) -> Option<SurfaceSample>;
fn lod_grid(
&self,
worker: &mut WorkerState,
x: i32,
z: i32,
width: i32,
side: usize,
) -> Result<Vec<LodColumn>, GeneratorError>;
fn structure_starts(
&self,
worker: &mut WorkerState,
@@ -1658,6 +1687,306 @@ where
}
None
}
fn surface_sample(
&self,
worker: &mut WorkerState,
x: i32,
z: i32,
mut spans: Option<&mut Vec<(i32, i32, BlockStateId)>>,
) -> Option<SurfaceSample> {
let mut top = None;
let mut water = None;
let mut below = 0;
let mut surface_complete = false;
let mut visit = |y, state| {
let properties = self.data.states.get(state);
if let Some(spans) = spans.as_mut()
&& !properties.is_air()
{
if let Some((min, _, previous)) = spans.last_mut()
&& *min == y + 1
&& *previous == state
{
*min = y;
} else {
spans.push((y, y + 1, state));
}
}
if surface_complete {
return false;
}
if properties.is_air() || properties.has_fluid() {
if top.is_some() {
surface_complete = true;
return spans.is_none();
}
if properties.has_fluid() {
water.get_or_insert((y + 1, state));
} else {
// Match the material traversal's water reset across air gaps.
water = None;
}
} else {
top.get_or_insert((y + 1, state));
below += 1;
}
false
};
self.noise_column(x, z, &mut visit);
let (height, state) = top?;
let y = height - 1;
let position = BlockPos::new(x, y, z);
let mut resolver = self.source.resolver(
&self.climate,
self.router.get(RouterField::Erosion).as_ref(),
);
// Use a fresh bounded context: far samples must not grow the chunk/structure caches.
let mut biome_context = SamplerContext::default();
let biome = resolver
.sample(&mut biome_context, self.zoom.quart_at_block(position))
.ok()?;
let biome = self.data.biome_id(biome)?;
let volume = DensityVolume::new([1; 3], [x, y, z], [1; 3]).ok()?;
let queries = SurfaceQueries {
pipeline: self,
temperatures: &mut worker.temperatures,
biome: Biomes::Resolver(&mut resolver, &mut biome_context),
possible: None,
};
let sources = MaterialDensitySources::new(
queries,
volume,
self.preliminary.clone(),
SamplerContext::default(),
)
.ok()?;
let mut material =
MaterialConditions::<R, _>::new(self.generation(), self.root.clone(), sources);
let rule = material.add_rule(self.rule.clone()).ok()?;
// shortcut: distant surfaces omit slope rules and column extensions; nearby chunks provide full terrain.
material.update_xz(x, z, 0, 0);
material.update_y(1, below, water.map_or(i32::MIN, |(height, _)| height), y);
let state = material.try_apply(rule, position).ok()?.unwrap_or(state);
if let Some(spans) = spans
&& let Some(index) = spans.iter().position(|&(min, max, _)| min <= y && y < max)
&& spans[index].2 != state
{
let (min, max, _) = spans[index];
if min == y {
spans[index].2 = state;
} else {
spans[index].1 = y;
spans.insert(index, (y, max, state));
}
}
Some(SurfaceSample {
height,
state,
water,
biome,
})
}
fn lod_grid(
&self,
worker: &mut WorkerState,
x: i32,
z: i32,
width: i32,
side: usize,
) -> Result<Vec<LodColumn>, GeneratorError> {
if width <= 0 || side == 0 || side > 64 {
return Err(resource("far sample dimensions"));
}
let mut geometry = Vec::with_capacity(side * side);
let mut context = SamplerContext::sparse();
let (min, height) = (self.settings.noise_min_y, self.settings.noise_height);
let step_xz = width.max(4);
let ox = x.div_euclid(4) * 4;
let oz = z.div_euclid(4) * 4;
let nx = ((x - ox + (side as i32 - 1) * width + step_xz - 1) / step_xz) + 1;
let nz = ((z - oz + (side as i32 - 1) * width + step_xz - 1) / step_xz) + 1;
let ny = (height + 7) / 8 + 1;
let volume = DensityVolume::new([nx, ny, nz], [ox, min, oz], [step_xz, 8, step_xz])
.map_err(stage("far density"))?;
let mut densities = vec![0.; volume.size()];
self.router
.get(RouterField::FinalDensity)
.sample_volume(&mut context, &mut densities, volume)
.map_err(stage("far density"))?;
let aquifer = DisabledAquifer::new(
self.picker,
self.data.registry.default_state("minecraft:air").unwrap(),
);
// Interpolate the supplied lattice, never the physical bounding volume.
let value = |wx: i32, iy: usize, wz: i32| {
let rx = (wx - ox) as f32 / step_xz as f32;
let rz = (wz - oz) as f32 / step_xz as f32;
let ix = rx.floor() as usize;
let iz = rz.floor() as usize;
let at = |dx: usize, dz: usize| {
densities[volume
.index([
(ix + dx).min(nx as usize - 1),
iy,
(iz + dz).min(nz as usize - 1),
])
.unwrap()]
};
let mix = |a: f32, b: f32, t: f32| a + (b - a) * t;
mix(
mix(at(0, 0), at(1, 0), rx - ix as f32),
mix(at(0, 1), at(1, 1), rx - ix as f32),
rz - iz as f32,
)
};
let mut profile = vec![0.; ny as usize];
let mut resolver = self.source.resolver(
&self.climate,
self.router.get(RouterField::Erosion).as_ref(),
);
let mut biome_context = SamplerContext::cached();
for dz in 0..side {
for dx in 0..side {
let (wx, wz) = (x + dx as i32 * width, z + dz as i32 * width);
let mut top = None;
let mut water = None;
let mut below = 0;
let mut complete = false;
let mut spans: Vec<(i32, i32, BlockStateId)> = Vec::new();
for (iy, density) in profile.iter_mut().enumerate() {
*density = value(wx, iy, wz);
}
for y in (min..min + height).rev() {
let iy = (y - min) as usize / 8;
let alpha = ((y - min) % 8) as f32 / 8.;
let density = profile[iy] + (profile[iy + 1] - profile[iy]) * alpha;
let state = aquifer
.compute_substance([wx, y, wz], f64::from(density))
.unwrap_or(self.settings.default_block);
let properties = self.data.states.get(state);
if !properties.is_air() {
if let Some((min, _, old)) = spans.last_mut()
&& *min == y + 1
&& *old == state
{
*min = y;
} else {
spans.push((y, y + 1, state));
}
}
if !complete {
if properties.is_air() || properties.has_fluid() {
if top.is_some() {
complete = true;
} else if properties.has_fluid() {
water.get_or_insert((y + 1, state));
} else {
water = None;
}
} else {
top.get_or_insert((y + 1, state));
below += 1;
}
}
}
let biome = if let Some((height, _)) = top {
let name = resolver
.sample(
&mut biome_context,
self.zoom.quart_at_block(BlockPos::new(wx, height - 1, wz)),
)
.map_err(stage("far biome"))?;
self.data
.biome_id(name)
.ok_or_else(|| resource("far biome"))?
} else {
BiomeId(0)
};
geometry.push((top, water, below, spans, biome));
}
}
if geometry.iter().all(|column| column.0.is_none()) {
return Ok(geometry
.into_iter()
.map(|(_, _, _, spans, _)| (None, spans))
.collect());
}
let volume =
DensityVolume::new([1; 3], [x, 0, z], [1; 3]).map_err(stage("far material"))?;
let queries = SurfaceQueries {
pipeline: self,
temperatures: &mut worker.temperatures,
biome: Biomes::Resolver(&mut resolver, &mut biome_context),
possible: None,
};
let sources = MaterialDensitySources::new(
queries,
volume,
self.preliminary.clone(),
SamplerContext::cached(),
)
.map_err(stage("far material"))?;
let mut material =
MaterialConditions::<R, _>::new(self.generation(), self.root.clone(), sources);
let rule = material
.add_rule(self.rule.clone())
.map_err(stage("far material"))?;
let mut result = Vec::with_capacity(side * side);
for (index, (top, water, below, mut spans, biome)) in geometry.into_iter().enumerate() {
let (wx, wz) = (
x + (index % side) as i32 * width,
z + (index / side) as i32 * width,
);
let Some((height, mut state)) = top else {
result.push((None, spans));
continue;
};
material.update_xz(wx, wz, 0, 0);
for depth in 1..=below.min(8) {
let y = height - depth;
material.update_y(
depth,
below - depth + 1,
water.map_or(i32::MIN, |(height, _)| height),
y,
);
let Some(index) = spans.iter().position(|&(min, max, _)| min <= y && y < max)
else {
continue;
};
let (min, max, old) = spans[index];
let painted = material
.try_apply(rule, BlockPos::new(wx, y, wz))
.map_err(stage("far material"))?
.unwrap_or(old);
if depth == 1 {
state = painted;
}
if old == painted {
continue;
}
let mut replacement = Vec::with_capacity(3);
if y + 1 < max {
replacement.push((y + 1, max, old));
}
replacement.push((y, y + 1, painted));
if min < y {
replacement.push((min, y, old));
}
spans.splice(index..=index, replacement);
}
result.push((
Some(SurfaceSample {
height,
state,
water,
biome,
}),
spans,
));
}
Ok(result)
}
fn noise_biomes(&self, worker: &mut WorkerState, quarts: &[[i32; 3]]) -> Vec<Option<String>> {
let mut resolver = self.source.resolver(
&self.climate,
@@ -1942,6 +2271,17 @@ impl OverworldGenerator {
}
}
/// A surface-only noise column for far terrain. Heights are exclusive top faces.
#[derive(Clone, Copy, Debug)]
pub struct SurfaceSample {
pub height: i32,
pub state: BlockStateId,
pub water: Option<(i32, BlockStateId)>,
pub biome: BiomeId,
}
pub type LodColumn = (Option<SurfaceSample>, Vec<(i32, i32, BlockStateId)>);
/// Per-thread generation state.
pub struct GeneratorWorker {
pipeline: Arc<dyn SeededPipeline>,
@@ -1955,6 +2295,23 @@ impl GeneratorWorker {
self.state.timings.chunks += 1;
result
}
/// Noise/aquifer surface and its material rule, without chunks, structures or features.
pub fn surface_sample(&mut self, x: i32, z: i32) -> Option<SurfaceSample> {
self.pipeline.surface_sample(&mut self.state, x, z, None)
}
/// Noise/aquifer material runs, excluding air; bounds are half-open.
/// Features and carvers still require generated chunks.
pub fn surface_column(
&mut self,
x: i32,
z: i32,
) -> (Option<SurfaceSample>, Vec<(i32, i32, BlockStateId)>) {
let mut spans = Vec::new();
let surface = self
.pipeline
.surface_sample(&mut self.state, x, z, Some(&mut spans));
(surface, spans)
}
/// `/locate structure`: the nearest start of one of `names`.
pub fn locate(
&mut self,
@@ -1964,11 +2321,22 @@ impl GeneratorWorker {
) -> Option<(String, [i32; 3])> {
self.pipeline.locate(&mut self.state, names, from, radius)
}
/// `BiomeSource.findClosestBiome3d`: the first sampled position whose
/// noise biome passes `allowed`, spiralling out from the origin.
/// Direct far sampling: work scales with rendered vertical resolution,
/// without allocating chunks, carving masks or propagated aquifers.
pub fn lod_grid(
&mut self,
x: i32,
z: i32,
width: i32,
side: usize,
) -> Result<Vec<LodColumn>, GeneratorError> {
self.pipeline.lod_grid(&mut self.state, x, z, width, side)
}
pub fn noise_biomes(&mut self, quarts: &[[i32; 3]]) -> Vec<Option<String>> {
self.pipeline.noise_biomes(&mut self.state, quarts)
}
/// `BiomeSource.findClosestBiome3d`: the first sampled position whose
/// noise biome passes `allowed`, spiralling out from the origin.
pub fn find_closest_biome(&mut self, search: &BiomeSearch<'_>) -> Option<(String, [i32; 3])> {
self.pipeline.find_closest_biome(&mut self.state, search)
}
@@ -2013,6 +2381,107 @@ impl GeneratorWorker {
mod tests {
use super::*;
#[test]
fn far_surface_matches_undecorated_land_and_keeps_end_void() {
let root = std::env::var_os("HORIZON_TEST_RESOURCES")
.or_else(|| std::env::var_os("HORIZON_RESOURCES"))
.map(std::path::PathBuf::from)
.unwrap_or_else(|| {
Path::new(env!("CARGO_MANIFEST_DIR")).join("../../.local/horizon-client/26.3")
});
if !root.join("worldgen/world_presets.json").exists() {
eprintln!("skipping: no worldgen bundle at {}", root.display());
return;
}
let registry: &'static BlockRegistry = Box::leak(Box::new(
crate::level::data::load_registry(&root).expect("registry"),
));
let data = GeneratorData::load(&root.join("worldgen"), registry).expect("worldgen data");
let generator =
OverworldGenerator::without_structures(data.clone(), 1234).expect("generator");
let mut worker = generator.worker();
let sample = worker.surface_sample(0, 0).expect("land");
let (column_surface, spans) = worker.surface_column(0, 0);
let column_surface = column_surface.expect("column land");
assert_eq!(column_surface.height, sample.height);
assert_eq!(column_surface.state, sample.state);
assert_eq!(column_surface.water, sample.water);
assert_eq!(column_surface.biome, sample.biome);
assert!(spans.iter().all(|&(min, max, state)| {
min < max && !generator.data().block_states().get(state).is_air()
}));
assert!(spans.windows(2).all(|pair| {
pair[1].1 <= pair[0].0 && (pair[1].1 != pair[0].0 || pair[1].2 != pair[0].2)
}));
assert!(spans.iter().any(|&(min, max, state)| {
min < sample.height && sample.height - 1 < max && state == sample.state
}));
assert!(sample.water.is_some_and(|(height, state)| {
spans
.iter()
.any(|&(min, max, block)| min < height && height - 1 < max && block == state)
}));
let chunk = worker.generate_proto(ChunkPos::new(0, 0)).expect("terrain");
assert_eq!(
sample.water.map_or(sample.height, |(height, _)| height),
chunk.surface_height(0, 0)
);
assert_eq!(sample.state, chunk.block(0, sample.height - 1, 0));
assert!(
sample.water.is_some(),
"fixture includes underwater terrain"
);
let started = std::time::Instant::now();
let mut exact = Vec::new();
for z in 0..8 {
for x in 0..8 {
exact.push(worker.surface_column(-1024 + x * 32, z * 32));
}
}
let dense_time = started.elapsed();
let started = std::time::Instant::now();
let direct = worker.lod_grid(-1024, 0, 32, 8).unwrap();
for z in 0..8 {
for x in 0..8 {
let wx = -1024 + x * 32;
let wz = z * 32;
let (surface, spans) = &direct[(z * 8 + x) as usize];
let expected = exact[(z * 8 + x) as usize].0;
if let (Some(surface), Some(expected)) = (surface, expected) {
assert!(
(surface.height - expected.height).abs() <= 32,
"coarse height exceeds its vertical resolution at {wx} {wz}: {} vs {}",
surface.height,
expected.height
);
assert!(spans.iter().any(|&(min, max, state)| min < surface.height
&& surface.height - 1 < max
&& state == surface.state));
}
assert!(spans.iter().all(|&(min, max, _)| min < max));
assert!(spans.windows(2).all(|pair| pair[1].1 <= pair[0].0));
}
}
eprintln!(
"64 far columns: dense={dense_time:?}, direct32={:?}",
started.elapsed()
);
let end =
OverworldGenerator::for_dimension(data, 1234, "minecraft:normal", "minecraft:the_end")
.expect("End generator");
let mut worker = end.worker();
assert!(worker.surface_sample(0, 0).is_some());
assert!(worker.surface_sample(256, 0).is_none());
let (surface, spans) = worker.surface_column(256, 0);
assert!(surface.is_none());
assert!(spans.is_empty());
let (surface, spans) = worker.lod_grid(256, 0, 32, 1).unwrap().pop().unwrap();
assert!(
surface.is_none() && spans.is_empty(),
"direct sampling retains End void"
);
}
#[test]
fn local_carver_cache_is_worker_owned_and_eviction_preserves_terrain() {
let root = std::env::var_os("HORIZON_RESOURCES")
@@ -1,6 +1,7 @@
//! Structure templates, processors and placement shared by template features
//! and structure pieces.
pub mod end_city;
pub mod enhanced_terrain;
pub mod features;
pub mod fortress;
pub mod jigsaw;
@@ -0,0 +1,355 @@
//! Structure libraries' enhanced terrain adaptation: YUNG's API's and
//! MoogsStructureLib's, which their Beardifier mixins add to vanilla's
//! density. Carried as `"horizon:enhanced_terrain_adaptation"` on a structure
//! and on its pool elements (docs/mods.md); the kernel itself is
//! [`TerrainKernel`].
use super::{
BoundingBox, Rotation,
jigsaw::Projection,
start::{Piece, StructureStart},
};
use crate::worldgen::DecodeError;
use horizon_core::{
density::{KernelJunction, KernelPiece, StructureBounds, TerrainKernel},
position::ChunkPos,
};
use serde_json::Value;
use std::sync::Arc;
/// One adaptation: the kernel, and how a piece's box is moved, padded or
/// cut to a band before the kernel measures from it.
#[derive(Debug)]
pub struct Adaptation {
kernel: Arc<TerrainKernel>,
/// `bottom_offset`, cast to whole blocks as the libraries do.
offset: i32,
/// `padding`: x, top, bottom, z.
padding: [i32; 4],
band: Option<Band>,
}
/// MoogsStructureLib's band: the kernel measures from `bottom..=top` above
/// the piece's floor instead of its whole height, for pieces as tall as one
/// of `heights` (any piece without it), and junctions add nothing.
#[derive(Debug)]
struct Band {
bottom: i32,
top: i32,
heights: Option<Vec<i32>>,
}
impl Adaptation {
pub fn decode(v: &Value) -> Result<Self, DecodeError> {
let int = |v: &Value, key: &'static str| -> Result<i32, DecodeError> {
v[key]
.as_i64()
.and_then(|n| i32::try_from(n).ok())
.ok_or(DecodeError::Field(key))
};
let optional = |v: &Value, key: &str| v[key].as_i64().map_or(0, |n| n as i32);
let action = |key: &'static str| match v[key].as_str() {
Some("bury") => Ok(1),
Some("carve") => Ok(-1),
Some("none") => Ok(0),
_ => Err(DecodeError::Field(key)),
};
let size = int(v, "kernel_size")?;
let distance = int(v, "kernel_distance")?;
if size < 0 || distance < 0 {
return Err(DecodeError::Field("kernel_size"));
}
let padding = &v["padding"];
let band = match v.get("band") {
Some(b) => Some(Band {
bottom: int(b, "bottom")?,
top: int(b, "top")?,
heights: b["piece_heights"].as_array().map(|h| {
h.iter()
.filter_map(Value::as_i64)
.map(|n| n as i32)
.collect()
}),
}),
None => None,
};
Ok(Self {
kernel: Arc::new(TerrainKernel::new(
size,
distance,
action("top")?,
action("bottom")?,
)),
offset: v["bottom_offset"].as_f64().unwrap_or(0.) as i32,
padding: ["x", "top", "bottom", "z"].map(|k| optional(padding, k)),
band,
})
}
fn radius(&self) -> i32 {
self.kernel.radius()
}
/// The box the kernel measures from for a piece with this box and
/// rotation; `None` for a band piece of another height.
fn piece_box(&self, original: BoundingBox, rotation: Rotation) -> Option<BoundingBox> {
let [x, top, bottom, z] = self.padding;
// The x padding runs along the axis the piece's east turned to.
let (px, pz) = match rotation {
Rotation::None | Rotation::Clockwise180 => (x, z),
Rotation::Clockwise90 | Rotation::Counterclockwise90 => (z, x),
};
let mut b = original.moved([0, self.offset, 0]);
b.min[0] -= px;
b.max[0] += px;
b.min[2] -= pz;
b.max[2] += pz;
match &self.band {
Some(band) => {
let height = original.max[1] - original.min[1] + 1;
if band.heights.as_ref().is_some_and(|h| !h.contains(&height)) {
return None;
}
let floor = original.min[1] + self.offset;
b.min[1] = floor + band.bottom;
b.max[1] = floor + band.top;
}
None => {
b.max[1] += top;
b.min[1] -= bottom;
}
}
Some(b)
}
}
/// A structure's adaptation and its library's rules.
#[derive(Debug)]
pub struct StructureAdaptation {
adaptation: Arc<Adaptation>,
/// YUNG's API adds rigid pieces' junctions too; MoogsStructureLib only
/// terrain-matching pieces'.
rigid_junctions: bool,
/// YUNG's API grows the start's box by the kernel radius, so chunks that
/// far out reference the start.
pub inflate: bool,
}
impl StructureAdaptation {
pub fn decode(v: &Value) -> Result<Self, DecodeError> {
Ok(Self {
adaptation: Arc::new(Adaptation::decode(v)?),
rigid_junctions: v["rigid_junctions"].as_bool().unwrap_or(false),
inflate: v["inflate"].as_bool().unwrap_or(false),
})
}
pub fn radius(&self) -> i32 {
self.adaptation.radius()
}
/// EnhancedBeardifierHelper.forStructuresInChunk for one start: its
/// kernel pieces and junctions near `chunk`, and the box they reach.
pub fn collect(
&self,
start: &StructureStart,
chunk: ChunkPos,
pieces: &mut Vec<KernelPiece>,
junctions: &mut Vec<KernelJunction>,
) -> Option<BoundingBox> {
let reach = start
.pieces
.iter()
.filter_map(|piece| match piece {
Piece::Pool(p) => p.element.enhanced_terrain().map(Adaptation::radius),
Piece::Legacy(_) => None,
})
.fold(self.radius(), i32::max);
if reach <= 0 {
return None;
}
let (bx, bz) = (chunk.x * 16, chunk.z * 16);
let mut affected: Option<BoundingBox> = None;
let mut include = |b: BoundingBox, radius: i32| {
let b = b.inflated(radius, radius);
match &mut affected {
Some(a) => a.encapsulate(&b),
None => affected = Some(b),
}
};
for piece in &start.pieces {
let b = piece.bounding_box();
// StructurePiece.isCloseToChunk.
if !(b.max[0] >= bx - reach
&& b.min[0] <= bx + 15 + reach
&& b.max[2] >= bz - reach
&& b.min[2] <= bz + 15 + reach)
{
continue;
}
let Piece::Pool(pool) = piece else {
if let Some(b) = self
.adaptation
.piece_box(b, Rotation::None)
.filter(|_| !self.adaptation.kernel.is_none())
{
push_piece(pieces, &self.adaptation, b, &mut include);
}
continue;
};
let adaptation = pool.element.enhanced_terrain().unwrap_or(&self.adaptation);
if adaptation.kernel.is_none() {
continue;
}
let rigid = pool.element.projection() == Projection::Rigid;
if rigid {
if let Some(b) = adaptation.piece_box(b, pool.rotation) {
push_piece(pieces, adaptation, b, &mut include);
}
}
if (rigid && !self.rigid_junctions) || adaptation.band.is_some() {
continue;
}
let radius = adaptation.radius();
for j in &pool.junctions {
if j.source_x > bx - radius
&& j.source_z > bz - radius
&& j.source_x < bx + 15 + radius
&& j.source_z < bz + 15 + radius
{
let source = [
j.source_x,
j.source_ground_y + adaptation.offset,
j.source_z,
];
junctions.push(KernelJunction {
source,
kernel: adaptation.kernel.clone(),
});
include(BoundingBox::new(source, source), radius);
}
}
}
affected
}
}
fn push_piece(
pieces: &mut Vec<KernelPiece>,
adaptation: &Adaptation,
b: BoundingBox,
include: &mut impl FnMut(BoundingBox, i32),
) {
// Java's BoundingBox puts inverted bounds (a padding below zero) in order.
let b = BoundingBox::from_corners(b.min, b.max);
let Some(bounds) = StructureBounds::new(b.min, b.max) else {
return;
};
pieces.push(KernelPiece {
bounds,
kernel: adaptation.kernel.clone(),
});
include(b, adaptation.radius());
}
#[cfg(test)]
mod tests {
use super::*;
use crate::worldgen::structure::{
StructureTemplate,
jigsaw::{Junction, PoolElement, PoolPiece},
};
use serde_json::json;
fn adaptation(v: Value) -> Adaptation {
Adaptation::decode(&v).unwrap()
}
#[test]
fn piece_boxes_move_pad_and_cut_to_bands() {
let a = adaptation(
json!({"kernel_size": 24, "kernel_distance": 24, "top": "carve",
"bottom": "none", "bottom_offset": 2.9,
"padding": {"x": 2, "z": 5, "top": 3, "bottom": 1}}),
);
let b = BoundingBox::new([0, 10, 0], [9, 19, 9]);
// Offset 2 (truncated), then the paddings.
let none = a.piece_box(b, Rotation::None).unwrap();
assert_eq!((none.min, none.max), ([-2, 11, -5], [11, 24, 14]));
// A quarter turn puts the x padding along z.
let turned = a.piece_box(b, Rotation::Clockwise90).unwrap();
assert_eq!((turned.min, turned.max), ([-5, 11, -2], [14, 24, 11]));
let band = adaptation(
json!({"kernel_size": 16, "kernel_distance": 8, "top": "carve",
"bottom": "carve", "band": {"bottom": 17, "top": 23, "piece_heights": [24, 25]}}),
);
let tall = band
.piece_box(BoundingBox::new([0, 30, 0], [9, 53, 9]), Rotation::None)
.unwrap();
assert_eq!((tall.min[1], tall.max[1]), (47, 53));
assert!(band.piece_box(b, Rotation::None).is_none());
assert!(Adaptation::decode(&json!({"kernel_size": 24})).is_err());
}
fn piece(element: &Arc<PoolElement>, x: i32, junction: [i32; 3]) -> Piece {
Piece::Pool(PoolPiece {
element: element.clone(),
position: [x, 64, 0],
ground_level_delta: 1,
rotation: Rotation::None,
bounding_box: BoundingBox::new([x, 64, 0], [x + 9, 73, 9]),
junctions: vec![Junction {
source_x: junction[0],
source_ground_y: junction[1],
source_z: junction[2],
}],
waterlogging: false,
})
}
fn element(projection: Projection, own: Option<Adaptation>) -> Arc<PoolElement> {
Arc::new(PoolElement::Single {
template: Arc::new(StructureTemplate::empty()),
processors: Default::default(),
legacy: false,
projection,
waterlogging: None,
yung: None,
enhanced_terrain: own.map(Arc::new),
})
}
/// YUNG's API adds every piece's junctions, MoogsStructureLib only those
/// of pieces that follow the terrain; a piece's own NONE adds nothing,
/// and pieces beyond the kernel's reach of the chunk are left out.
#[test]
fn collects_pieces_and_junctions_as_each_library_does() {
let rigid = element(Projection::Rigid, None);
let matching = element(Projection::TerrainMatching, None);
let none = element(
Projection::Rigid,
Some(adaptation(json!({"kernel_size": 0, "kernel_distance": 0,
"top": "none", "bottom": "none"}))),
);
let start = StructureStart {
structure: 0,
name: "test".into(),
chunk: ChunkPos::new(0, 0),
pieces: vec![
piece(&rigid, 0, [5, 64, 5]),
piece(&matching, 10, [12, 65, 5]),
piece(&none, 20, [22, 64, 5]),
piece(&rigid, 60, [62, 64, 5]),
],
bounding_box: BoundingBox::new([0, 64, 0], [69, 73, 9]),
};
let kernel = json!({"kernel_size": 24, "kernel_distance": 24, "top": "carve",
"bottom": "none", "bottom_offset": -1});
for (yung, junctions) in [(true, 2), (false, 1)] {
let mut v = kernel.clone();
v["rigid_junctions"] = yung.into();
let s = StructureAdaptation::decode(&v).unwrap();
let (mut pieces, mut found) = (Vec::new(), Vec::new());
let affected = s
.collect(&start, ChunkPos::new(0, 0), &mut pieces, &mut found)
.unwrap();
assert_eq!(pieces.len(), 1, "the rigid piece near the chunk");
assert_eq!(pieces[0].bounds.minimum(), [0, 63, 0]);
assert_eq!(found.len(), junctions);
assert_eq!(found.last().unwrap().source, [12, 64, 5]);
assert_eq!((affected.min, affected.max), ([-12, 51, -12], [24, 84, 21]));
}
}
}
@@ -42,6 +42,9 @@ pub enum PoolElement {
/// YUNG's API's rules on the piece (`"horizon:yungsapi"`), read only
/// by its assembler ([`super::yung`]).
yung: Option<Box<super::yung::Rules>>,
/// The piece's own terrain kernel in a structure that has one
/// (`"horizon:enhanced_terrain_adaptation"`).
enhanced_terrain: Option<Arc<super::enhanced_terrain::Adaptation>>,
},
Feature {
feature: String,
@@ -143,6 +146,10 @@ impl PoolElement {
.get("horizon:yungsapi")
.map(|rules| super::yung::Rules::decode(rules, resources).map(Box::new))
.transpose()?,
enhanced_terrain: value
.get("horizon:enhanced_terrain_adaptation")
.map(|a| super::enhanced_terrain::Adaptation::decode(a).map(Arc::new))
.transpose()?,
}
}
"minecraft:feature_pool_element" => Self::Feature {
@@ -248,6 +255,15 @@ impl PoolElement {
_ => None,
}
}
/// The piece's own terrain kernel, in place of its structure's.
pub fn enhanced_terrain(&self) -> Option<&super::enhanced_terrain::Adaptation> {
match self {
Self::Single {
enhanced_terrain, ..
} => enhanced_terrain.as_deref(),
_ => None,
}
}
}
/// StructureTemplate.getJigsaws: palette chosen by a position-seeded random.
fn template_jigsaws(
@@ -1026,6 +1026,7 @@ mod tests {
projection: Projection::Rigid,
waterlogging: None,
yung: None,
enhanced_terrain: None,
});
let piece = || PoolPiece {
bounding_box: BoundingBox::new([0; 3], [4; 3]),
@@ -102,6 +102,9 @@ pub struct StructureDef {
pub biomes: BTreeSet<BiomeId>,
pub step: usize,
pub terrain: TerrainAdjustment,
/// A structure library's terrain kernel
/// (`"horizon:enhanced_terrain_adaptation"`).
pub enhanced_terrain: Option<super::enhanced_terrain::StructureAdaptation>,
/// Another loaded structure turns this one's type off
/// (`"horizon:disables"`): it never starts.
pub disabled: bool,
@@ -375,6 +378,10 @@ impl StructureRegistry {
.position(|s| Some(*s) == v["step"].as_str())
.unwrap_or(4),
terrain: terrain(v["terrain_adaptation"].as_str()),
enhanced_terrain: v
.get("horizon:enhanced_terrain_adaptation")
.map(super::enhanced_terrain::StructureAdaptation::decode)
.transpose()?,
disabled: false,
});
}
@@ -1105,6 +1112,10 @@ fn try_generate(
if def.terrain != TerrainAdjustment::None {
bounding_box = bounding_box.inflated(12, 12);
}
if let Some(enhanced) = def.enhanced_terrain.as_ref().filter(|e| e.inflate) {
let r = enhanced.radius();
bounding_box = bounding_box.inflated(r, r);
}
Some(StructureStart {
structure,
name: def.name.clone(),
@@ -1177,8 +1188,18 @@ pub fn beardifier(
Some(a) => a.encapsulate(&b),
None => *any = Some(b),
};
let (mut kernel_pieces, mut kernel_junctions) = (Vec::new(), Vec::new());
let mut kernels: Option<BoundingBox> = None;
for start in starts {
let adjustment = registry.structures[start.structure].terrain;
let def = &registry.structures[start.structure];
if let Some(enhanced) = &def.enhanced_terrain {
if let Some(b) =
enhanced.collect(start, chunk, &mut kernel_pieces, &mut kernel_junctions)
{
include(b, &mut kernels);
}
}
let adjustment = def.terrain;
if adjustment == TerrainAdjustment::None {
continue;
}
@@ -1225,13 +1246,20 @@ pub fn beardifier(
}
}
}
let any = any?;
let affected = any.inflated(24, 24);
Some(Beardifier::new(
rigids,
junctions,
StructureBounds::new(affected.min, affected.max),
))
// The libraries' kernels reach as far as their radius, beyond vanilla's.
let mut affected = any.map(|a| a.inflated(24, 24));
if let Some(k) = kernels {
include(k, &mut affected);
}
let affected = affected?;
Some(
Beardifier::new(
rigids,
junctions,
StructureBounds::new(affected.min, affected.max),
)
.with_kernels(kernel_pieces, kernel_junctions),
)
}
/// StructureStart.placeInChunk for pieces intersecting the chunk's writable box.
@@ -915,6 +915,7 @@ mod tests {
projection: Projection::Rigid,
waterlogging: None,
yung: rules.map(Box::new),
enhanced_terrain: None,
})
}
+1 -1
View File
@@ -46,4 +46,4 @@ To play, use [the launcher](../launcher/README.md); the project site is [playhor
## The earlier Java track (no longer developed)
- [Java performance packs](java-packs.md), with [Vulkan on your Mac](vulkan.md), [collector comparisons](collectors.md), [profiling](profiling.md), [process memory](process-memory.md) and [tick state](tick-state.md); [server quickstart](server-quickstart.txt).
- [Java performance packs](java-packs.md), with [Vulkan on your Mac](vulkan.md), [collector comparisons](collectors.md), [profiling](profiling.md), [process memory](process-memory.md) and [tick state](tick-state.md). The Fabric server starter is retired: [host a server](hosting.md) with horizon-server.
+2 -3
View File
@@ -28,16 +28,15 @@ is not a measured pause total. Use JFR root pause events for pause analysis.
The streaming reader is checked against the JDK's event exporter with both
default-GC and ZGC fixtures in CI on Windows, Linux and ARM macOS.
Developer demo and server launchers accept `--gc default`, `--gc g1` or `--gc zgc`.
The developer demo launcher accepts `--gc default`, `--gc g1` or `--gc zgc`.
For example:
```sh
python3 tools/runtime.py demo --instance .local/runtime/collector-test --heap 6G --gc zgc --compact-headers
python3 tools/server.py run --heap 4G --gc zgc
python3 tools/test_jfr.py --gc zgc
```
Prepare demo mods/worlds before comparing. The server retains its EULA requirement.
Prepare demo mods/worlds before comparing.
Options affect only the launched process; they do not change global Java settings.
The collector and compact-header experiments can be tested independently.
+21 -3
View File
@@ -4,7 +4,7 @@
In the Horizon menu style, the Options screen's **Video** section holds the graphics settings, with a Simple / Advanced switch at the top (remembered between sessions):
- **Simple**: a preset (Performance, Balanced, Quality, Ultra or Custom), render distance, FOV, max framerate, VSync and GUI scale.
- **Simple**: a preset (Performance, Balanced, Quality, Ultra or Custom), render distance, LOD distance, FOV, max framerate, VSync and GUI scale.
- **Advanced**: every setting, grouped under Rendering, Quality, Display, Preferences, Performance and Overlay.
In the Classic style, Video Settings starts with two buttons opening the same settings as vanilla-looking screens.
@@ -17,7 +17,7 @@ Every row has a one-line description and its cost in frame time (none, low, medi
| Group | Setting | Stored in |
| --- | --- | --- |
| Rendering | render distance, simulation distance, entity distance | `options.txt` (vanilla keys) |
| | cave culling, chunk updates per frame | `horizon-graphics.txt` |
| | LOD distance, cave culling, chunk updates per frame | `horizon-graphics.txt` |
| Quality | smooth lighting, mipmap levels, biome blend, clouds, cloud distance, weather radius, particles, entity shadows, cutout leaves | `options.txt` |
| | sky, fog, weather, animated textures | `horizon-graphics.txt` |
| Display | FOV, max framerate, VSync, fullscreen, GUI scale | `options.txt` |
@@ -31,7 +31,25 @@ Every row has a one-line description and its cost in frame time (none, low, medi
Which settings each level shows, their descriptions and costs are in `crates/horizon-client/src/graphics_ui.rs` (`rows`); the Horizon shell draws them in `options_horizon.rs` with `ui_theme::described_row`, and the Classic screens use `graphics_style.rs`.
Not implemented: distant-terrain LOD, anisotropic filtering (wgpu needs linear magnification for it, which blurs close blocks), MSAA (FXAA instead) and exclusive fullscreen (fullscreen is borderless).
## Distant terrain
**LOD Distance** is off by default. Enable it in Simple or Advanced graphics settings and choose 64, 128, 256, 512 or 1024 chunks. With LODs enabled, **Full Detail Distance** controls the nearby normal chunks; LOD Distance controls the outer view. Keep the nearby range modest, for example 12 chunks with a 256-chunk LOD distance. Increasing the distant range does not increase simulation distance.
Distant terrain uses progressively coarser voxel columns. Columns retain separate material layers and air gaps; matching exposed faces are merged, with block-scale texture repeats. Complete chunk data is reduced once on the terrain worker and cached at each supported scale, across each cell’s area instead of enlarging its corner column. Material votes combine different light levels of the same block; reduced cells retain the brightest sampled sky and block light separately. Finer tiles replace only their own footprints in a loading fallback, and a movement margin limits rapid resolution changes at tile boundaries. Local Overworld, Nether and End worlds and a LAN world you host can supply their generator. Remote servers do not supply the client's world-generation seed or generator configuration.
Unknown areas are sampled directly from the dimension's seed-bound density and surface rules, with shared per-tile scratch and material evaluation. A small pool samples tiles independently of meshing. Refinement prioritizes visible screen error; high aerial views use the distance to the world height range to avoid unnecessary close detail below the camera. Initial vegetation uses the world's configured tree types, placement densities and biome assignments at every distance level. Tree positions, shapes and large-cell coverage are visual approximations, not a replay of native feature placement.
A separate bounded worker refines the nearest LOD levels with disposable ordinary chunk generation, including supported carvers, structures and decoration. Groups of 4×4 chunks share their generation halos, and each chunk is published once every neighbouring feature center has completed. Farther tiles use direct sampling rather than waiting for every intervening ordinary chunk. Saved and visited blocks take precedence over both paths. These temporary chunks never enter gameplay, simulation or normal save queues. Generated-column caches and request queues are bounded; far data is regenerated after reopening a world.
Coverage and detail improve progressively; a large selected radius does not mean all its target detail is already resident. The control status reports target and sampled target tile counts separately. Direct far sampling approximates vertical noise interpolation, fluid boundaries and lighting; surface slopes and column extensions, small features and partial block shapes can differ. Unexplored distant structures are not synthesized by the direct path, and native decoration order can differ from subsequent gameplay generation. Horizon-supported data-driven noise and tree configurations can be reused; this does not execute arbitrary Fabric/NeoForge worldgen or structure mods.
Ordinary Overworld atmospheric fog extends with the selected LOD distance. Fog Off disables distance haze at large ranges too; fluid, darkness and boss fog retain their limits.
LOD terrain enters the existing world colour and depth passes, so supported shader-pack postprocessing can see it. This does not add Iris material passes or shadow maps: Java packs still have the [existing fullscreen-pass limitations](shader-packs.md). Shader appearance and transitions need testing with the selected pack.
The setting is stored as `lodDistance` in `horizon-graphics.txt`; `0` disables it. Existing `renderDistance` in `options.txt` keeps its normal meaning for full chunks.
Not implemented: anisotropic filtering (wgpu needs linear magnification for it, which blurs close blocks), MSAA (FXAA instead) and exclusive fullscreen (fullscreen is borderless).
## Benchmark
+40 -20
View File
@@ -204,6 +204,8 @@ horizon-server restore BACKUP [--yes]
horizon-server service install|uninstall|print [--system]
horizon-server panel a one-time login link for the web panel
horizon-server import FILE server.toml from a vanilla server.properties
horizon-server update [--check] install the newest release and restart on it (below)
horizon-server rollback back to the release before
```
While the server runs, `op`, `whitelist` and `cmd` go to the game's console, so they act exactly as vanilla's
@@ -213,14 +215,31 @@ console commands do. While it's stopped, they edit vanilla's files directly. Any
The server restarts the game by itself if it crashes: after 5 s, then waiting longer each time, up to 5
minutes. It keeps the crash report and gives up after 5 crashes in 10 minutes, and `status` says why.
Coming: `update` and `rollback`, `console` (attach to a server running in the background), `ban` and
`pardon` as their own commands, and `mcp` (an MCP server so an AI assistant can run the server).
**Updates** (a server installed with the install script). The server checks for a newer release a minute
after it starts and every 6 hours after, and says so in its log, in `status` and in the panel; it never
updates by itself. `horizon-server update --check` asks once. `horizon-server update`:
1. downloads the newest release and checks it against the SHA-256 GitHub publishes for it;
2. stops the game, backs up the world (`backups/<time>-before-<release>.zip`) and switches to the new
release;
3. starts the game on it and waits up to 5 minutes (`--timeout SECONDS`) for it to come up.
If the new game crashes or doesn't come up in time, the server goes back to the release it was on by itself
and puts the world back as it was just before the switch (the world the new game left is kept in
`backups/`). `horizon-server rollback` goes back to the release before by hand, keeping the world as it is
(it backs it up first). `--version vX.Y.Z` installs a given release, and `--archive FILE --version NAME` an
archive you have, such as your own build. A stopped server is switched and starts on the new release next
time. Set `HORIZON_NO_UPDATE_CHECK=1` in the server's environment to turn the check off.
Coming: `console` (attach to a server running in the background), `ban` and `pardon` as their own
commands, and `mcp` (an MCP server so an AI assistant can run the server).
## 6. Web panel
The panel runs inside `horizon-server` while the server runs. It shows live TPS, MSPT, RAM and CPU graphs for
the last 10 minutes, the players with their ping (and a kick button), the console with a command line, and the
backups (back up now, restore with a confirmation), with Restart and Stop.
backups (back up now, restore with a confirmation), with Restart and Stop. When a newer release is out it says
so, with an Update button that runs `horizon-server update` (section 5), and shows how it went.
```sh
horizon-server panel
@@ -297,25 +316,26 @@ docker run -d --name horizon -p 25565:25565 --stop-timeout 180 \
## 10. How fast is it
The latest bot benchmark against vanilla 26.3 is in
[benchmarks/server-2026-10-17](https://github.com/ludovic111/horizon/blob/main/benchmarks/server-2026-10-17/README.md).
It ran on `main` at `7bbbf6e9` (in release v2026.10.17.17), on a 4-thread laptop CPU (i7-7500U), with the bots
on another machine. Earlier measurements are in [docs/server.md](https://github.com/ludovic111/horizon/blob/main/docs/server.md).
In short:
The latest bot benchmark compares Horizon with vanilla 26.3, Paper and Fabric + Lithium:
[benchmarks/server-four-way-2026-10-19](https://github.com/ludovic111/horizon/blob/main/benchmarks/server-four-way-2026-10-19/README.md).
It ran release v2026.10.19 on a 4-thread laptop CPU (i7-7500U), with 1 to 50 players walking, exploring and
standing around spawn, the bots on another machine. All four servers did the same work. Earlier
measurements are in [docs/server.md](https://github.com/ludovic111/horizon/blob/main/docs/server.md). In
short:
- **Memory:** 0.8–0.9 GB with up to 100 players, and 1.4 GB at most with 100 players flying out fast. Vanilla
uses 3.4–4.0 GB with its 4 GB heap.
- **Start:** a new world is ready in 9–10 s, against 15–17 s for vanilla.
- **Same work** (players exploring at the same pace, so both servers send the same chunks): Horizon's average
tick is 44–81 % shorter than vanilla's with 1 to 50 players, and its 95th percentile is lower too.
- **Players walking around spawn:** Horizon's average tick is shorter at every count. With 25 and 50 players,
its slowest ticks (95th percentile) are a little longer than vanilla's (31 against 28 ms, 60 against 56 ms).
- **100 players** walking: Horizon keeps 15.8 TPS (63 ms ticks). Vanilla's watchdog stopped it on this machine.
- **Players flying out as fast as they can:** with 10 and 25 players vanilla's average tick is a little shorter
than Horizon's, but Horizon sends 1.6–1.8 times as many new chunks. Vanilla's watchdog stopped it at 50 and 100.
- **Memory:** 0.7 GB with one player and 1.1 GB with 50, against 3.4 to 4.5 GB for the Java servers
(each with a 4 GB heap): 3.8 to 4.8 times less.
- **Start:** ready in 8 to 9 s, against 15 s for vanilla, 17 s for Fabric and 19 to 22 s for Paper.
- **CPU:** from 10 players up, the least of the four (1.4 cores at 50, against Paper's 2.1 and Fabric's 2.4).
- **Average tick:** Paper's is shorter at every count (10.3 against 13.7 ms with one player, 46.9 against
47.8 with 50), and Fabric + Lithium's with 1 and 10 players. Horizon's is shorter than vanilla's (equal
with 10), and than Fabric's from 25 players up.
- **Slow ticks:** from 10 players up, Horizon's slowest ticks (99th percentile) are the shortest of the
four (73 ms with 50, against 110 for Paper and 174 for Fabric).
- **50 players:** Horizon and Paper keep 20 TPS, Fabric + Lithium drops to 17, and vanilla's watchdog
stopped it.
On this 4-thread machine Horizon held 20 TPS with 50 players in every scenario, and 15.8–19 TPS with 100. For more
than 50 active players, give it more cores.
For more than 50 active players, give it more cores.
## When something goes wrong
+3 -3
View File
@@ -1,6 +1,6 @@
# Java performance packs (no longer developed)
Before the Rust rewrite, Horizon was a lab for Minecraft Java client performance: pinned Fabric performance packs, distant terrain, an original frame and tick recorder, and client and server starter downloads. This track is **no longer developed**. No release downloads are published; build the packs and starters locally with `python3 tools/build_release.py` (see [Build and run](#build-and-run)). This page keeps its documentation.
Before the Rust rewrite, Horizon was a lab for Minecraft Java client performance: pinned Fabric performance packs, distant terrain, an original frame and tick recorder, and client and server starter downloads (the server starter is gone: [horizon-server](hosting.md) replaces it). This track is **no longer developed**. No release downloads are published; build the packs and starters locally with `python3 tools/build_release.py` (see [Build and run](#build-and-run)). This page keeps its documentation.
## Java packs: Mac starter (out of scope)
@@ -91,9 +91,9 @@ python3 tools/build_mod.py
python3 tools/build_release.py
```
Gitignored `dist/` contains twenty packs, our original instrumentation JAR, client/server starter ZIPs and SHA256SUMS. [mods.lock.json](../packs/mods.lock.json) pins mod versions, exact dependency closure, SHA-1/SHA-512 and source/license; [fabric.lock.json](../packs/fabric.lock.json) pins loader/library SHA-256. Packs use the [Modrinth format](https://support.modrinth.com/en/articles/8802351-modrinth-modpack-format-mrpack). They embed only original project code; third-party mods, game and assets download from their publishers.
Gitignored `dist/` contains twenty packs, our original instrumentation JAR, the client starter ZIP and SHA256SUMS. [mods.lock.json](../packs/mods.lock.json) pins mod versions, exact dependency closure, SHA-1/SHA-512 and source/license; [fabric.lock.json](../packs/fabric.lock.json) pins loader/library SHA-256. Packs use the [Modrinth format](https://support.modrinth.com/en/articles/8802351-modrinth-modpack-format-mrpack). They embed only original project code; third-party mods, game and assets download from their publishers.
The server starter uses Lithium/FerriteCore and `/horizonserver bench <label> <seconds>`. [Server instructions](server-quickstart.txt) cover setup and a baseline. Files are prepared before you accept Minecraft's EULA locally. A 50-active-player workload remains to be measured.
The Fabric server starter (Lithium/FerriteCore, `tools/server.py`) was retired on 2026-10-19. To host a server, use [horizon-server](hosting.md) ([playhorizon.dev/server](https://playhorizon.dev/server)); its benchmarks are in [server.md](server.md), section 9.
Local smoke testing uses the game's built-in limited demo; normal play uses Prism's licensed account authentication:
+20 -5
View File
@@ -504,7 +504,11 @@ Two cases the oracle fills in:
- **YUNG's API** (#839, #846 in the game; #840, #847 in carry). `yungsapi:yung_jigsaw` and its
elements become vanilla's jigsaw and single element, with their rules under
`horizon:yungsapi`. The game assembles them as YUNG's `JigsawManager` does: priority
pieces, caps by name, depth ranges, `ignore_bounds` and the conditions tree. YUNG's
pieces, caps by name, depth ranges, `ignore_bounds` and the conditions tree, with
`piece_in_range`. Dead-end pools swap a piece that got no child for one of its dead-end
pool, and once the layout is done each piece's modifiers can turn it into another template
(`transform`) or place it last (`delay_generation`). Better Fortresses' bridges need all
three: over seed 1234's Nether chunks -64..63 it goes from 3,765 to 4,106 pieces. YUNG's
placements become `horizon:` keys on a random spread: `horizon:exclusion_zone` for the
enhanced exclusion zone, `horizon:avoid_biomes` for the temples' ocean and river search,
`horizon:ring_sections` for Better Strongholds' rings. `horizon:disables` on a YUNG
@@ -518,6 +522,14 @@ Two cases the oracle fills in:
structure's piece caps and required pieces from the mod's `*_pieces_spawn_counts` files.
The game assembles them the mods' way and runs their post-layout steps (centring,
burying, the Nether land search).
- **Enhanced terrain adaptation.** YUNG's API and MoogsStructureLib add a kernel of their
own to the Beardifier: its own size and falloff, carving or burying above and below each
piece's floor. Carry writes it as `horizon:enhanced_terrain_adaptation` on the structure
and on pool elements with their own (docs/mods.md), and the game sums it after vanilla's
terrain density. In seed 1234's terrain before structures are placed, Better Fortresses'
pieces held 39 to 41 % netherrack and now 3 to 7 %, the mod's open bridges; Moog's mega
fortress band 45 % and now 30 %; Better Strongholds' pieces, buried, go from 92 to 97 %
solid to 99 %, closing the caves that cut through them.
Each mod's `carry-report.json` lists what was mapped, the rules and processors lost by name,
@@ -531,15 +543,18 @@ Two cases the oracle fills in:
(unchanged), and no pool that fails to decode (65 before #816 in the 64×64 centre).
- 1,061 files were mapped (MVS 413, MNS 210, MSS 157, MES 110, Repurposed Structures 104,
ChoiceTheorem's Overhauled Village 66), and 103 convention biome tags written.
- When Dungeons Arise, Structory and Towns and Towers use vanilla's types already.
- When Dungeons Arise and Structory use vanilla's types already. Towns and Towers does too,
but its 1,198 worldgen files sit under the `kaisyn` and `towns_and_towers` namespaces, not
its mod id `t_and_t`, and a mod's content may only use its own namespace (docs/mods.md), so
carry leaves them out and its 60 structures are missing.
- Still lost, written in Java:
- Formations' structure and element types (179): their placements read block states in the
noise column (solid, water, lava), which the structure context doesn't give yet;
- Repurposed's mineshafts, mansions and monuments, Better Mineshafts and two Better
Dungeons types: Java structure types;
- YUNG's and Moog's enhanced terrain adaptation, YUNG's piece modifiers and dead-end pools,
and the block processors written in Java (YUNG's banners, legs and decorations, Repurposed's
pillars and fluid fixes, Lithostitched's block swaps);
- the block processors written in Java
(YUNG's banners, legs and decorations, Repurposed's pillars and fluid fixes,
Lithostitched's block swaps);
- MO Structures (38; its library is only on CurseForge).
With every structure PR above, the same square (seed 1234, chunks -64..63) holds 362
+17
View File
@@ -303,6 +303,23 @@ A pool addition holds `"elements"` as a pool does. This is what lithostitched's
`add_structure_set_entries` and `add_template_pool_elements` modifiers do on
NeoForge and Fabric, and `horizon-port-pack carry` writes them from those.
A structure (and any of its pool elements, in place of the structure's) may
hold `horizon:enhanced_terrain_adaptation`: YUNG's API's and
MoogsStructureLib's terrain kernel, which carves or buries terrain around the
pieces on top of vanilla's `terrain_adaptation`. `kernel_size` and
`kernel_distance` shape the kernel; `top` and `bottom` (`carve`, `bury` or
`none`) act above and below a piece's floor; `bottom_offset`, `padding`
(`x`, `top`, `bottom`, `z`) and MoogsStructureLib's `band` (`bottom`, `top`,
`piece_heights`) move the box it measures from. On a structure,
`rigid_junctions` (YUNG's API) adds rigid pieces' junctions as well and
`inflate` grows the start's box by the kernel radius. `kernel_size` 0 is
none.
```json
{ "horizon:enhanced_terrain_adaptation": { "kernel_size": 24, "kernel_distance": 24,
"top": "carve", "bottom": "none", "rigid_junctions": true, "inflate": true } }
```
## Versions and compatibility
- The API is the WIT package `horizon:mods@0.1.0`. New functions and
+64
View File
@@ -0,0 +1,64 @@
# Bulk density and noise generation benchmark
The PR dispatches unary and binary density operations once per bulk buffer, resolves the shared Beardifier kernel once per sampled position, and reuses Perlin X-column permutations across the Y loop. Binary bit-exact tests cover every operation, distinct NaN payloads, signed zeros, infinities, subnormals, random float pairs and SIMD/tail-sized buffers. Child-failure tests retain left/right evaluation order and partial output. Scalar shortcuts, arithmetic expressions, RNG consumption and generation traversal are preserved.
## Whole-terrain measurements
**Times below are complete terrain wall elapsed divided by 256 input chunks, with no thread multiplier.** Four-worker figures are elapsed latency/throughput measurements, not per-thread CPU costs. Combined time adds the complete decoration phase, still divided by 256; 196 interior chunks are decorated with unchanged border work. At the primary site, 256 terrain chunks take about 2.578 seconds with one worker versus 0.860 seconds with four: approximately 3.00× throughput, or 75% of ideal fourfold scaling. A subsequent 1/2/4/8-worker scaling experiment is reported below.
Fresh equivalent optimized builds compare baseline `e4f8154cb4115aa1ee622381d9b12387cdf515d4` with the extended candidate. Every case has one discarded process pair, then five measured pairs alternating baseline/candidate order. Initialized processes first generate and discard the same offset 9×9 region at center+(64,64), using the same seeded generator, before timing fresh measured workers. This excludes lazy ring initialization without pre-generating target chunks. All cases use size 16; primary seed/site is 12345/(0,0), second Overworld seed/site is -1/(32,-32).
Medians below are baseline → extended candidate, ms/input chunk, followed by percent reduction.
- **overworld, 1 worker(s):** terrain 12.685400 → 10.069365 (20.62%); combined 14.696298 → 12.105007 (17.63%).
- **overworld, 4 worker(s):** terrain 4.045564 → 3.359536 (16.96%); combined 4.663124 → 3.944952 (15.40%).
- **the_nether, 1 worker(s):** terrain 5.088375 → 4.848149 (4.72%); combined 5.481617 → 5.239165 (4.42%).
- **the_nether, 4 worker(s):** terrain 1.446298 → 1.366468 (5.52%); combined 1.574890 → 1.472913 (6.48%).
- **the_end, 1 worker(s):** terrain 1.085323 → 0.980046 (9.70%); combined 1.161651 → 1.053080 (9.35%).
- **the_end, 4 worker(s):** terrain 0.281733 → 0.259724 (7.81%); combined 0.312280 → 0.288982 (7.46%).
- **overworld, 1 worker(s), second site:** terrain 11.585829 → 9.079823 (21.63%); combined 12.650823 → 10.145760 (19.80%).
- **overworld, 4 worker(s), second site:** terrain 3.105819 → 2.519504 (18.88%); combined 3.410155 → 2.812551 (17.52%).
- **overworld, 1 worker(s), non-initialized generator:** terrain 23.429316 → 20.942006 (10.62%); combined 25.599745 → 23.005476 (10.13%).
The non-initialized case retains lazy startup work and is separate from recurring-cost qualification; resource loading is outside all terrain brackets. All 45 measured terrain pairs improved, but Nether one-worker median terrain improvement remains below 5%. Results are workload-dependent. The primary one-worker noise bracket improves 5.901774 → 4.773963 ms/chunk (19.11%); shared bulk material-density work also improves, with surface 4.576660 → 3.181641 (30.48%). Whole-terrain gains must not be described as noise-stage-only gains. Parallel stage brackets are aggregate worker elapsed and are not exclusive CPU shares.
## Added benefit beyond unary dispatch
A separate five-pair alternating comparison uses the existing unary-only PR revision `594b50278044df6b72b641cd02d5050925c86203` as baseline. It uses the same initialized primary workload and fresh builds. These numbers measure the binary/Beardifier/Perlin additions together; they do not assign a separate speedup to each addition.
- **1 worker(s):** terrain 10.913267 → 10.034900 (8.05%); combined 12.867017 → 12.079432 (6.12%).
- **4 worker(s):** terrain 3.536831 → 3.312338 (6.35%); combined 4.110268 → 3.897104 (5.19%).
All ten incremental terrain pairs improved. Pair reductions ranged 4.72–9.93% with one worker and 2.10–8.61% with four, so not every individual pair exceeds 5%. A preliminary gate was excluded in its entirety because a concurrent Horizon client consumed roughly seven cores. Its raw samples remain in `contended-gate/`; all qualification runs began after the client was closed. No individual samples were selectively removed from the restarted runs.
## Worker scaling follow-up
Candidate revision `c47b30fcf0db87b8023e6784d941a4f06524b453` was tested at 1, 2, 4 and 8 workers on the same primary initialized Overworld workload. The saved `final-candidate` optimized binary is used without rebuilding or changing instrumentation. After one discarded process per worker count, eight measured processes per count run sequentially. Worker orders `[1,2,4,8]`, `[2,8,1,4]`, `[4,1,8,2]`, `[8,4,2,1]` repeat twice, placing each count in each order position twice. Every process initializes its generator using the same offset 9×9 region before measuring 256 terrain chunks and 196 interior decorations. No baseline comparison or new correctness claim is inferred from this scaling experiment.
| Workers | Terrain median ms/chunk (min–max) | Terrain chunks/s | Speedup | Efficiency | Combined median ms/chunk | Combined speedup |
| --- | --- | --- | --- | --- | --- | --- |
| 1 | 9.994644 (9.842544–10.312603) | 100.05 | 1.000× | 100.0% | 11.950737 | 1.000× |
| 2 | 5.580046 (5.499388–5.772284) | 179.21 | 1.791× | 89.6% | 6.644499 | 1.799× |
| 4 | 3.405281 (3.238112–3.525516) | 293.66 | 2.935× | 73.4% | 3.996531 | 2.990× |
| 8 | 2.750915 (2.662450–2.915874) | 363.52 | 3.633× | 45.4% | 3.220818 | 3.710× |
Speedup is the one-worker median divided by the respective median; efficiency divides speedup by worker count. Eight workers give 23.8% more terrain throughput than four workers, with 19.2% lower wall ms/chunk. This is sublinear scaling on this machine and site; eight workers exceed the six physical cores, and this experiment does not identify the causes of diminishing returns. Ordinary desktop activity was not isolated. All 32 measured runs are retained. Raw samples, per-process logs, binary SHA-256, protocol, runnable script and combined ranges are in `/srv/bob-data/horizon-noise-aquifer-e477fa56/extension/scaling/` (`raw.json`, `summary.json`, `protocol.json`, `run.py`). Run with `python /srv/bob-data/horizon-noise-aquifer-e477fa56/extension/scaling/run.py`; the script uses the prepared resources and saved optimized binary, requiring no build. The earlier comparison medians above remain unchanged.
## Machine and validation
AMD Ryzen 5 5600X, 6 cores/12 logical CPUs, 32 GiB RAM, Linux, Rust 1.99.0. Optimized `fast` profile: release optimization, LTO disabled, 16 codegen units. Builds and timed processes were sequential; ordinary desktop activity was not isolated. Primary terrain process CPU medians are 12.4 → 10.0 ms/input chunk (one worker) and 15.9 → 13.2 (four), using coarse 100 Hz accounting. Peak RSS is effectively unchanged: 786288 → 786256 KiB and 833496 → 833528 KiB. No new heap buffer is introduced. Executable text increases 1804 bytes against the original baseline and decreases 1056 bytes against the unary-only revision.
Validation of the extended candidate passed:
- 193 targeted optimized Rust unit/integration tests, including binary floating-point and failure-order tests, density/world fields, storage/palettes, material/carving stages, biome regions, retrogen and ticks.
- Fresh pinned Java client/server density parity: 15,524,627 output lines per side.
- Dedicated noise parity: 1,443,539 lines per side, covering Perlin/smeared bulk paths, strided/wrapped volumes, buffer tails, constructor RNG and derivatives.
- Dedicated Beardifier/structure-density parity: 183 fixtures, 1,360,095 lines per side.
- Terrain-fill parity: 114 fixtures, 200 fills, 7,950,873 lines per side, including prefilled chunks, repeated fills, partial vertical volumes, aquifers, WG heightmaps and ordered postprocessing.
- 24/24 exact region phase comparisons: three dimensions × two seeds/sites × one/four workers, each with 81 retained chunks and 49 interior decorations. Every retained chunk is compared before and after decoration, including borders. Snapshots include raw/public block reads and palettes, quart biomes, entities/block entities as binary NBT, complete scheduled ticks, all six heightmaps, ordered postprocessing, actual non-air counts and chunk metadata. Temporary diagnostic accessors were removed from production source.
This validates the declared corpora and sites, not every possible seed, biome or platform. Lighting, meshing, saving, streaming and full application startup were not benchmarked.
The existing `crates/horizon-world/examples/chunkgen_bench.rs` is the standard workload entry point. Both performance builds used the same saved diagnostic source adding optional generator initialization and exact elapsed/count output; its thread-multiplied human display was not used. Diagnostic builds use `CARGO_TARGET_DIR=/srv/bob-data/horizon-stage3d-target cargo build --profile fast -p horizon-world --example chunkgen_profile`. Raw timings, exact commands, binary/source hashes, scripts and parity outputs are retained outside Git at `/srv/bob-data/horizon-noise-aquifer-e477fa56/extension/`: `gate-raw.json`, `matrix-raw.json`, `summary.json`, `marginal/marginal-raw.json`, `binary-hashes.json`, and the `*-parity.json` reports. Prepared resources are `/srv/bob-data/horizon/.local/horizon-client/26.3`. Generated artifacts and private fixtures remain outside the PR.
The repository quick PR self-check also passes: public-tree validation, workspace rustfmt, workspace/all-target clippy with warnings denied, and 96 full core-crate tests. Relevant pinned Java suites were run separately as listed above. A production-only rebuild after removing diagnostic support has byte-identical executable instructions and read-only constants to the measured candidate.
-54
View File
@@ -1,54 +0,0 @@
Horizon server @PACK_VERSION@ — Minecraft 26.3, Fabric 0.19.5, Java 25
Install Python 3.10+ and native Java 25. Extract this ZIP and open a terminal here.
This starter never downloads the game: download the Minecraft 26.3 server jar
yourself (https://www.minecraft.net/download/server while 26.3 is current), then:
python3 tools/server.py prepare --server-jar PATH/TO/server.jar
Read Minecraft's EULA at https://www.minecraft.net/eula. Create/edit
.local/server/optimized/eula.txt to contain eula=true if you agree, then:
python3 tools/server.py run --heap 4G
Windows: use python in place of python3 if needed. Your server jar, mods and
assets stay in .local. No game code, third-party mod JARs or credentials are bundled.
Lithium and FerriteCore optimize the original simulation/storage; tick rate and
vanilla gameplay rules are retained. No Paper-style gameplay configuration.
From the server console:
horizonserver bench populated-50 120
horizonserver stop
From an operator's in-game chat, prefix each command with /. JSON/CSV reports
appear in the instance's horizon-benchmarks directory. They record actual peak
players, tick execution durations, observed tick count per elapsed second,
JVM heap/GC and platform metadata. meanRate is tick execution capacity, NOT TPS.
An idle server does not establish performance for 50 active players.
Baseline:
python3 tools/server.py prepare --baseline
python3 tools/server.py run --baseline --heap 4G
Use separate world copies with the same seed, settings, Java and maximum heap.
Only run one at a time unless you give them distinct ports/resources. When
benchmarking 50 players include normal exploration, entity farms, redstone and
chunk loading; repeat with 1, 10 and 100. Do not change simulation/view distance
between matched runs or turn off game logic to manufacture a speedup.
This first release has not been validated under a 50-player real workload.
Optional Java 25 HotSpot experiment: add --compact-headers to the run command.
Reports capture the actual compact object header flag and collector names.
Compare against the same workload with the flag omitted; object allocation
changes do not establish a whole-server RAM/TPS gain.
Optional collector comparison: select --gc g1 or --gc zgc on the run command.
Match world, player activity, heap, Java and header mode; use one collector at a
time. gc_time_ms includes collector cycle accounting, not a measured pause total.
Use JFR pause events for pause analysis and compare RSS/CPU/TPS together.
Compare generated reports:
python3 tools/compare_benchmarks.py baseline-ticks.json optimized-ticks.json --strict
Baseline must be a separate instance: --baseline adds no optimizers but doesn't
delete mods from an existing optimized instance. Use clean directories.
+49 -19
View File
@@ -7,8 +7,9 @@ should beat vanilla, Paper and Fabric + Lithium on TPS/MSPT, RAM, CPU, chunk gen
Gameplay stays vanilla: there are no Paper-style rule changes unless the owner turns one on.
This page covers what the server does today (measured), the design of `horizon-server`, and what ships for the
first working version (2026-10-23). It replaces the Java/Fabric starter (`docs/server-quickstart.txt`,
`tools/server.py`), which goes once the benchmark below exists.
first working version (2026-10-23). It replaced the Java/Fabric starter (`docs/server-quickstart.txt`,
`tools/server.py`, retired on 2026-10-19). The owner's guide is docs/hosting.md, also at
https://playhorizon.dev/server.
## 1. Today
@@ -325,11 +326,12 @@ horizon-server backup [NAME] backups restore <backup> [-
horizon-server import <server.properties>
horizon-server service install|uninstall|print [--system]
horizon-server panel the panel's address and a one-time login link
horizon-server update [--check] [--version vX.Y.Z] [--timeout S] rollback [--timeout S]
horizon-server update --archive FILE --version NAME an archive you have (your own build)
Planned:
horizon-server console attach: the live log, and send commands (Ctrl-D detaches)
horizon-server ban|pardon <player> [reason] ban-ip|pardon-ip <address> (`cmd ban …` works now)
horizon-server update [--check] [--version vX.Y.Z] rollback
horizon-server import-resources <dir> prepare --server-jar PATH
horizon-server mcp MCP server on stdio (section 8)
```
@@ -350,8 +352,11 @@ line), and backups (list, back up now, restore with confirmation), with restart
the supervisor, so it is up exactly when the server is: starting a stopped server stays a command (or the
service manager's job). Whitelist and bans go through the console for now.
**Later:** bans and IP bans, worlds, updates with rollback, `server.properties` import, logs and crash
reports, and modpacks (with the modpack work).
**Updates:** when a newer release is out, the panel says so with an Update button, which runs the same
path as `horizon-server update` (section 7) and then shows how it went.
**Later:** bans and IP bans, worlds, `server.properties` import, logs and crash reports, and modpacks (with
the modpack work).
**Tech:** one HTML page with plain JS and CSS (`crates/horizon-server/panel/`), embedded in the binary with
`include_str!`. No build step, no framework, no CDN. It uses Horizon's design system tokens (`horizon-theme`'s
@@ -393,16 +398,29 @@ runtime or new dependency. Server-sent events can replace the polling later if a
lines and the engine's crash report (`crash-reports/`), restarts it after 5 s, and doubles the wait up to
5 minutes. It gives up after 5 crashes in 10 minutes, so it doesn't loop on a broken world, and shows the
reason in `status` and the panel.
- **Updates.** `update --check` compares with the latest GitHub release. `update`:
1. downloads the server archive and checks its SHA-256;
2. backs up the world;
3. keeps the current binaries as `.horizon/previous/`;
4. swaps in the new ones and restarts;
5. watches for `Done` within the start timeout and a first status reply.
- **Updates** (`crates/horizon-server/src/update.rs`), for a `horizon-server` installed by `install.sh` or
`install.ps1`. Releases ship often (release-flow.md), so updates stay opt-in: a running server looks for a
newer release a minute after it starts and then every 6 hours, and only says so, in its log, in `status`
and in the panel (`HORIZON_NO_UPDATE_CHECK=1` turns the check off). `update --check` asks GitHub once.
`update`:
1. downloads the release archive for this computer (on Linux the server build) and checks it against the
SHA-256 GitHub publishes for it;
2. unpacks it into `<dir>/releases/<tag>`, beside the others, with its game files linked to
`<dir>/game-files` as the installers do;
3. stops the game, backs up the world (`backups/<time>-before-<tag>.zip`), records the release in use in
`<dir>/previous` and points `<dir>/current` at the new one;
4. checks that the new game loads on this system (`--help`), then starts it;
5. waits until it has logged `Done` and answered a status request, for 300 s (`--timeout`).
If that check fails, it puts the previous binaries back, restores the world if the new engine touched it,
and restarts. `rollback` does the same by hand. Releases ship often (release-flow.md), so updates stay
opt-in, with `update --check` in `status` and a notice in the panel.
If the new game doesn't load, stops, or isn't up in time, the supervisor goes back by itself: `current`
points at the release before, `previous` is left as it was, the world is put back from the backup (the
one the new game left is kept in `backups/replaced-<time>/`), and the game starts again on the old
release. `rollback` switches to `previous` by hand, through the same path (the world is backed up first
and kept as it is). The supervisor keeps running as it was: the new `horizon-server` itself takes over
the next time the server starts (`stop` and `start`, or the service's restart). A stopped server is
switched and its new game checked to load; it starts on the new release next time. One install serves
every server folder on the machine, so each one runs the new release from its next start. Old releases
stay in `<dir>/releases/` until they are deleted by hand (keep `current` and `previous`).
## 8. Control API and MCP
@@ -411,7 +429,9 @@ runtime or new dependency. Server-sent events can replace the polling later if a
`.horizon/api.json`. It's plain HTTP/1.1 on `std::net` (`crates/horizon-server/src/http.rs`), no runtime.
- built (PR 2b, 2c): `GET status` (phase, uptime, crashes, RSS, CPU, and the engine's TPS, MSPT
average/P50/P95/P99, players, chunks), `GET logs?since=`, `POST command {line}` (returns the output),
`POST stop`, `POST restart`, `GET backups`, `POST backups {name}`, `POST restore {name}`;
`POST stop`, `POST restart`, `GET backups`, `POST backups {name}`, `POST restore {name}`, `POST update
{tag, timeout}` (without a tag: the newest release, downloaded first; `status` has an `update` object
that says how it went);
- planned: `GET metrics?since=` (the panel's graphs), `GET events` (SSE: log lines and metrics),
`GET`/`PATCH config` (server.toml fields, validated).
- **Auth.** Every call needs `Authorization: Bearer <token from .horizon/api.json>` (file mode 0600), and the
@@ -521,6 +541,20 @@ reproducible by anyone, and show the same gameplay on both sides.
- Horizon's RSS was 4.1 to 4.9 times smaller (1.03 GB at 50).
- Served in full, vanilla's walking bots got 5400 to 6100 chunks each, against 640 to 1100 in the first
run. That is why vanilla held 50 bots there and not here.
- **Four servers, equal work (2026-10-19,** `benchmarks/server-four-way-2026-10-19/`). The same load
against vanilla, Paper (build 205, its defaults except the connection throttle, which refused bots that
share an address) and vanilla on Fabric with Lithium. Horizon was release v2026.10.19, which spawns mobs
and ticks blocks around every player as vanilla does (#950, #931), so it now does vanilla's work away
from spawn too. Results, on the ThinkPad (the desktop didn't wake):
- The work was equal on all four at every count (chunks a second within 4 %).
- Horizon's RSS was 3.8 to 4.8 times smaller (0.72 to 1.11 GB). It started in 8.2 to 8.5 s, against
14.7 to 22.4 s for the Java servers. From 10 bots up it used the least CPU.
- Average tick: Paper was ahead of Horizon at every count (10.3 against 13.7 ms at 1 bot, 28.3 against
30.9 at 25, 46.9 against 47.8 at 50). Fabric + Lithium was ahead at 1 and 10 bots (12.5 and 20.2
against 13.7 and 22.9), behind at 25, and fell to 17.2 TPS at 50. Vanilla was slowest, and its
watchdog stopped it at 50.
- Horizon's P99 was the lowest from 10 bots up (73 ms at 50, against Paper's 110 and Fabric's 174), and
its worst single P99 the lowest at every count.
- **Ownership.** perf-server-1 owns making the tick faster. This work only measures it, files what it finds,
and fixes startup, RAM and packaging.
@@ -553,11 +587,7 @@ machine, then published in `benchmarks/`; and item 5.
- the Nether and End for remote players;
- the headless engine and its RAM win;
- a server-only prepare without a JDK;
- updates and rollback (the design is above; they ship right after);
- a Windows service, hot reload, modpacks in the panel;
- Paper and Fabric + Lithium in the benchmark;
- the website's "host a server" page;
- retiring `tools/server.py` and `docs/server-quickstart.txt` (once the benchmark replaces them).
Online mode works, so a public server can check accounts. An offline server should still run only on a
LAN, a tailnet, or behind a whitelist.
+1 -1
View File
@@ -258,7 +258,7 @@ A `shaders/` folder of GLSL programs, named by where Iris (and OptiFine before i
| Custom textures, `noisetex` | easy | image loads |
| Custom uniforms (`uniform.*` expressions) | medium | a small expression evaluator per frame |
| Compute (`.csh`), `image2D` load/store | medium | wgpu has compute; storage formats are more limited than OpenGL's |
| Distant Horizons, Voxy hooks, `DH_*` | not planned | Horizon has no LOD terrain yet |
| Distant Horizons, Voxy hooks, `DH_*` | not planned | Horizon's native LOD terrain shares world colour/depth; it does not expose these mods' APIs |
Order: preprocessor and legaliser until the three test packs parse; then `composite`/`final`/`deferred` with the
uniforms; then `gbuffers_terrain`, `gbuffers_water` and `gbuffers_skybasic`/`skytextured` (the packs' look comes
+18 -1
View File
@@ -1,6 +1,8 @@
import unittest
from tools.server_bench import work
import re
from tools.server_bench import DONE, TICK_AVERAGE, TICK_PERCENTILES, work
def run(server, bots, sent, new, waited=0.0, behaviour="mixed"):
@@ -30,3 +32,18 @@ class ServerBenchWorkTests(unittest.TestCase):
if __name__ == "__main__":
unittest.main()
class ServerBenchLogTests(unittest.TestCase):
def test_every_server_says_it_is_up_the_same_way(self):
for line in ['[05:16:10] [Server thread/INFO]: Done (9.762s)! For help, type "help"',
'[02:53:58 INFO]: Done (29.649s)! For help, type "help"']:
self.assertTrue(re.search(DONE, line), line)
self.assertFalse(re.search(DONE, '[02:53:58 INFO]: Preparing spawn area: Done (12%)'))
def test_paper_answers_tick_query_as_vanilla_does(self):
# Paper 26.3 build 205, from a smoke run of this tool.
self.assertEqual(TICK_AVERAGE.search("Average time per tick: 14.4ms (Target: 50.0ms)").group(1), "14.4")
self.assertEqual(TICK_PERCENTILES.search(
"[02:54:10 INFO]: System chat: Percentiles: P50: 1.0ms P95: 34.6ms P99: 764.4ms. Sample: 100").groups(),
("1.0", "34.6", "764.4"))
+529
View File
@@ -0,0 +1,529 @@
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
#
# When uploading crates to the registry Cargo will automatically
# "normalize" Cargo.toml files for maximal compatibility
# with all versions of Cargo and also rewrite `path` dependencies
# to registry (e.g., crates.io) dependencies.
#
# If you are reading this file be aware that the original Cargo.toml
# will likely look very different (and much more reasonable).
# See Cargo.toml.orig for the original contents.
[package]
edition = "2021"
rust-version = "1.87"
name = "wgpu-hal"
version = "30.0.1"
authors = ["gfx-rs developers"]
build = "build.rs"
autolib = false
autobins = false
autoexamples = false
autotests = false
autobenches = false
description = "Hardware abstraction layer for wgpu, the cross-platform, safe, pure-rust graphics API"
homepage = "https://wgpu.rs/"
readme = "README.md"
keywords = ["graphics"]
license = "MIT OR Apache-2.0"
repository = "https://github.com/gfx-rs/wgpu"
[package.metadata.docs.rs]
features = [
"metal",
"vulkan",
"gles",
"renderdoc",
]
rustdoc-args = [
"--cfg",
"docsrs",
]
targets = [
"x86_64-unknown-linux-gnu",
"x86_64-apple-darwin",
"x86_64-pc-windows-msvc",
"wasm32-unknown-unknown",
]
[package.metadata.cargo-machete]
ignored = ["cfg_aliases"]
[features]
device_lost_panic = []
dx12 = [
"dep:arrayvec",
"dep:bit-set",
"dep:bytemuck",
"dep:gpu-allocator",
"dep:libloading",
"dep:once_cell",
"dep:ordered-float",
"dep:parking_lot",
"dep:profiling",
"dep:range-alloc",
"dep:windows-core",
"gpu-allocator/d3d12",
"naga/hlsl-out",
"once_cell/std",
"windows/Win32_Devices_DeviceAndDriverInstallation",
"windows/Win32_Devices_Display",
"windows/Win32_Graphics_Direct3D_Dxc",
"windows/Win32_Graphics_Direct3D_Fxc",
"windows/Win32_Graphics_Direct3D",
"windows/Win32_Graphics_Direct3D12",
"windows/Win32_Graphics_DirectComposition",
"windows/Win32_Graphics_Dxgi_Common",
"windows/Win32_Graphics_Gdi",
"windows/Win32_Security",
"windows/Win32_System_Diagnostics_Debug",
"windows/Win32_System_Kernel",
"windows/Win32_System_Performance",
"windows/Win32_System_Threading",
"windows/Win32_UI_WindowsAndMessaging",
]
fragile-send-sync-non-atomic-wasm = ["wgpu-types/fragile-send-sync-non-atomic-wasm"]
gles = [
"dep:arrayvec",
"dep:bytemuck",
"dep:glow",
"dep:glutin_wgl_sys",
"dep:js-sys",
"dep:khronos-egl",
"dep:libloading",
"dep:ndk-sys",
"dep:objc2",
"dep:parking_lot",
"dep:profiling",
"dep:wasm-bindgen",
"dep:wayland-sys",
"dep:web-sys",
"dep:windows-result",
"naga/glsl-out",
"wgpu-types/web",
"windows-result/std",
"windows/Win32_Graphics_Gdi",
"windows/Win32_Graphics_OpenGL",
"windows/Win32_System_LibraryLoader",
"windows/Win32_UI_WindowsAndMessaging",
]
internal_error_panic = []
metal = [
"naga/msl-out",
"dep:arrayvec",
"dep:block2",
"dep:bytemuck",
"dep:libloading",
"dep:objc2",
"dep:objc2-core-foundation",
"dep:objc2-core-graphics",
"dep:objc2-foundation",
"dep:objc2-metal",
"dep:objc2-quartz-core",
"dep:parking_lot",
"dep:profiling",
"dep:smallvec",
"dep:raw-window-metal",
]
portable-atomic = [
"dep:portable-atomic",
"dep:portable-atomic-util",
]
renderdoc = [
"dep:libloading",
"dep:renderdoc-sys",
]
static-dxc = ["dep:mach-dxcompiler-rs"]
validation_canary = ["dep:parking_lot"]
vulkan = [
"naga/spv-out",
"dep:android_system_properties",
"dep:arrayvec",
"dep:ash",
"dep:bytemuck",
"dep:libc",
"dep:libloading",
"dep:ordered-float",
"dep:parking_lot",
"dep:profiling",
"dep:raw-window-metal",
"dep:smallvec",
"dep:windows",
"gpu-allocator/vulkan",
"windows/Win32",
"windows/Win32_Devices_Display",
"windows/Win32_Foundation",
"windows/Win32_Graphics_Dxgi",
"windows/Win32_Graphics_Dxgi_Common",
"windows/Win32_Graphics_Gdi",
]
[lib]
name = "wgpu_hal"
path = "src/lib.rs"
[dependencies.arrayvec]
version = "0.7.1"
optional = true
default-features = false
[dependencies.bitflags]
version = "2.9"
[dependencies.bytemuck]
version = "1.22"
features = [
"extern_crate_alloc",
"min_const_generics",
"derive",
]
optional = true
[dependencies.cfg-if]
version = "1"
[dependencies.glow]
version = "0.17"
optional = true
[dependencies.hashbrown]
version = "0.17"
features = [
"default-hasher",
"inline-more",
]
default-features = false
[dependencies.log]
version = "0.4.29"
[dependencies.naga]
version = "30.0.0"
[dependencies.naga-types]
version = "30.0.0"
[dependencies.ordered-float]
version = ">=3, <6.0"
optional = true
default-features = false
[dependencies.parking_lot]
version = "0.12.3"
optional = true
[dependencies.profiling]
version = "1.0.1"
optional = true
default-features = false
[dependencies.raw-window-handle]
version = "0.6.2"
default-features = false
[dependencies.static_assertions]
version = "1.1"
[dependencies.thiserror]
version = "2.0.12"
default-features = false
[dependencies.wgpu-naga-bridge]
version = "30.0.0"
[dependencies.wgpu-types]
version = "30.0.0"
default-features = false
[dev-dependencies.env_logger]
version = "0.11"
default-features = false
[dev-dependencies.glam]
version = "0.33.0"
[dev-dependencies.naga]
version = "30.0.0"
features = [
"wgsl-in",
"termcolor",
]
[dev-dependencies.winit]
version = "0.30.8"
features = ["android-native-activity"]
[build-dependencies.cfg_aliases]
version = "0.2.2"
[target.'cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))'.dependencies.js-sys]
version = "0.3.104"
optional = true
default-features = true
[target.'cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))'.dependencies.wasm-bindgen]
version = "0.2.127"
optional = true
default-features = false
[target.'cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))'.dependencies.web-sys]
version = "0.3.104"
features = [
"default",
"Window",
"HtmlCanvasElement",
"WebGl2RenderingContext",
"OffscreenCanvas",
"VideoFrame",
]
optional = true
default-features = false
[target.'cfg(all(unix, not(target_vendor = "apple"), not(target_family = "wasm")))'.dependencies.drm]
version = "0.15"
optional = true
[target.'cfg(all(windows, not(target_arch = "aarch64"), target_env = "msvc"))'.dependencies.mach-dxcompiler-rs]
version = "0.1.4"
optional = true
default-features = false
[target.'cfg(any(not(target_has_atomic = "64"), not(target_has_atomic = "ptr")))'.dependencies.portable-atomic]
version = "1.10"
optional = true
[target.'cfg(not(any(target_arch = "wasm32", target_os = "ios", target_os = "visionos", target_env = "ohos")))'.dev-dependencies.glutin]
version = "0.32"
features = [
"egl",
"wgl",
"wayland",
"x11",
]
default-features = false
[target.'cfg(not(any(target_arch = "wasm32", target_os = "ios", target_os = "visionos", target_env = "ohos")))'.dev-dependencies.glutin-winit]
version = "0.5"
features = [
"egl",
"wgl",
"wayland",
"x11",
]
default-features = false
[target.'cfg(not(target_arch = "wasm32"))'.dependencies.ash]
version = "0.38"
optional = true
[target.'cfg(not(target_arch = "wasm32"))'.dependencies.gpu-allocator]
version = "0.28"
features = ["hashbrown"]
optional = true
default-features = false
[target.'cfg(not(target_arch = "wasm32"))'.dependencies.khronos-egl]
version = "6"
features = ["dynamic"]
optional = true
[target.'cfg(not(target_arch = "wasm32"))'.dependencies.libloading]
version = "0.8"
optional = true
[target.'cfg(not(target_arch = "wasm32"))'.dependencies.renderdoc-sys]
version = "1"
optional = true
[target.'cfg(not(target_arch = "wasm32"))'.dependencies.smallvec]
version = "1.14"
features = ["union"]
optional = true
[target.'cfg(not(target_has_atomic = "ptr"))'.dependencies.portable-atomic-util]
version = "0.2.4"
features = ["alloc"]
optional = true
[target.'cfg(target_os = "android")'.dependencies.android_system_properties]
version = "0.1.1"
optional = true
[target.'cfg(target_os = "android")'.dependencies.ndk-sys]
version = "0.6"
optional = true
[target.'cfg(target_os = "emscripten")'.dependencies.khronos-egl]
version = "6"
features = [
"static",
"no-pkg-config",
]
optional = true
[target.'cfg(target_os = "emscripten")'.dependencies.libloading]
version = "0.8"
optional = true
[target.'cfg(target_vendor = "apple")'.dependencies.block2]
version = "0.6.2"
optional = true
[target.'cfg(target_vendor = "apple")'.dependencies.bytemuck]
version = "1.22"
features = [
"extern_crate_alloc",
"min_const_generics",
]
optional = true
[target.'cfg(target_vendor = "apple")'.dependencies.objc2]
version = "0.6.3"
optional = true
[target.'cfg(target_vendor = "apple")'.dependencies.objc2-core-foundation]
version = "0.3.2"
features = [
"std",
"CFCGTypes",
]
optional = true
default-features = false
[target.'cfg(target_vendor = "apple")'.dependencies.objc2-core-graphics]
version = "0.3.2"
features = [
"std",
"CGColorSpace",
]
optional = true
default-features = false
[target.'cfg(target_vendor = "apple")'.dependencies.objc2-foundation]
version = "0.3.2"
features = [
"std",
"NSError",
"NSProcessInfo",
"NSRange",
"NSString",
]
optional = true
default-features = false
[target.'cfg(target_vendor = "apple")'.dependencies.objc2-metal]
version = "0.3.2"
features = [
"std",
"block2",
"MTLAllocation",
"MTLBlitCommandEncoder",
"MTLBlitPass",
"MTLBuffer",
"MTLCaptureManager",
"MTLCaptureScope",
"MTLCommandBuffer",
"MTLCommandEncoder",
"MTLCommandQueue",
"MTLComputeCommandEncoder",
"MTLComputePass",
"MTLComputePipeline",
"MTLCounters",
"MTLDepthStencil",
"MTLDevice",
"MTLDrawable",
"MTLEvent",
"MTLLibrary",
"MTLPipeline",
"MTLPixelFormat",
"MTLRenderCommandEncoder",
"MTLRenderPass",
"MTLRenderPipeline",
"MTLResource",
"MTLSampler",
"MTLStageInputOutputDescriptor",
"MTLTexture",
"MTLTypes",
"MTLVertexDescriptor",
"MTLAccelerationStructure",
"MTLAccelerationStructureTypes",
"MTLAccelerationStructureCommandEncoder",
"MTLResidencySet",
]
optional = true
default-features = false
[target.'cfg(target_vendor = "apple")'.dependencies.objc2-quartz-core]
version = "0.3.2"
features = [
"std",
"objc2-core-foundation",
"objc2-core-graphics",
"CALayer",
"CAMetalLayer",
"objc2-metal",
]
optional = true
default-features = false
[target.'cfg(target_vendor = "apple")'.dependencies.raw-window-metal]
version = "1.0"
optional = true
[target."cfg(unix)".dependencies.libc]
version = "0.2.183"
optional = true
default-features = false
[target."cfg(unix)".dependencies.wayland-sys]
version = "0.31.3"
features = [
"client",
"dlopen",
"egl",
]
optional = true
[target."cfg(windows)".dependencies.bit-set]
version = "0.10"
optional = true
default-features = false
[target."cfg(windows)".dependencies.glutin_wgl_sys]
version = "0.6"
optional = true
[target."cfg(windows)".dependencies.once_cell]
version = "1.21"
optional = true
default-features = false
[target."cfg(windows)".dependencies.range-alloc]
version = "0.1"
optional = true
[target."cfg(windows)".dependencies.windows]
version = "0.62"
optional = true
default-features = false
[target."cfg(windows)".dependencies.windows-core]
version = "0.62"
optional = true
default-features = false
[target."cfg(windows)".dependencies.windows-result]
version = "0.4"
optional = true
default-features = false
[lints.rust]
missing_debug_implementations = "warn"
[lints.rust.unexpected_cfgs]
level = "warn"
priority = 0
check-cfg = [
'cfg(feature, values("cargo-clippy"))',
"cfg(web_sys_unstable_apis)",
]
+176
View File
@@ -0,0 +1,176 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2025 The gfx-rs developers
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+119
View File
@@ -0,0 +1,119 @@
# `wgpu_hal`: a cross-platform unsafe graphics abstraction
This crate defines a set of traits abstracting over modern graphics APIs,
with implementations ("backends") for Vulkan, Metal, Direct3D, and GL.
`wgpu_hal` is a spiritual successor to
[gfx-hal](https://github.com/gfx-rs/gfx), but with reduced scope, and
oriented towards WebGPU implementation goals. It has no overhead for
validation or tracking, and the API translation overhead is kept to the bare
minimum by the design of WebGPU. This API can be used for resource-demanding
applications and engines.
The `wgpu_hal` crate's main design choices:
- Our traits are meant to be _portable_: proper use
should get equivalent results regardless of the backend.
- Our traits' contracts are _unsafe_: implementations perform minimal
validation, if any, and incorrect use will often cause undefined behavior.
This allows us to minimize the overhead we impose over the underlying
graphics system. If you need safety, the [`wgpu-core`] crate provides a
safe API for driving `wgpu_hal`, implementing all necessary validation,
resource state tracking, and so on. (Note that `wgpu-core` is designed for
use via FFI; the [`wgpu`] crate provides more idiomatic Rust bindings for
`wgpu-core`.) Or, you can do your own validation.
- In the same vein, returned errors _only cover cases the user can't
anticipate_, like running out of memory or losing the device. Any errors
that the user could reasonably anticipate are their responsibility to
avoid. For example, `wgpu_hal` returns no error for mapping a buffer that's
not mappable: as the buffer creator, the user should already know if they
can map it.
- We use _static dispatch_. The traits are not
generally object-safe. You must select a specific backend type
like [`vulkan::Api`] or [`metal::Api`], and then use that
according to the main traits, or call backend-specific methods.
- We use _idiomatic Rust parameter passing_,
taking objects by reference, returning them by value, and so on,
unlike `wgpu-core`, which refers to objects by ID.
- We map buffer contents _persistently_. This means that the buffer
can remain mapped on the CPU while the GPU reads or writes to it.
You must explicitly indicate when data might need to be
transferred between CPU and GPU, if `wgpu_hal` indicates that the
mapping is not coherent (that is, automatically synchronized
between the two devices).
- You must record _explicit barriers_ between different usages of a
resource. For example, if a buffer is written to by a compute
shader, and then used as and index buffer to a draw call, you
must use [`CommandEncoder::transition_buffers`] between those two
operations.
- Pipeline layouts are _explicitly specified_ when setting bind
group. Incompatible layouts disturb groups bound at higher indices.
- The API _accepts collections as iterators_, to avoid forcing the user to
store data in particular containers. The implementation doesn't guarantee
that any of the iterators are drained, unless stated otherwise by the
function documentation. For this reason, we recommend that iterators don't
do any mutating work.
Unfortunately, `wgpu_hal`'s safety requirements are not fully documented.
Ideally, all trait methods would have doc comments setting out the
requirements users must meet to ensure correct and portable behavior. If you
are aware of a specific requirement that a backend imposes that is not
ensured by the traits' documented rules, please file an issue. Or, if you are
a capable technical writer, please file a pull request!
[`wgpu-core`]: https://crates.io/crates/wgpu-core
[`wgpu`]: https://crates.io/crates/wgpu
[`vulkan::Api`]: vulkan/struct.Api.html
[`metal::Api`]: metal/struct.Api.html
## Primary backends
The `wgpu_hal` crate has full-featured backends implemented on the following
platform graphics APIs:
- Vulkan, available on Linux, Android, and Windows, using the [`ash`] crate's
Vulkan bindings. It's also available on macOS, if you install [MoltenVK].
- Metal on macOS, using the [`metal`] crate's bindings.
- Direct3D 12 on Windows, using the [`windows`] crate's bindings.
[`ash`]: https://crates.io/crates/ash
[MoltenVK]: https://github.com/KhronosGroup/MoltenVK
[`metal`]: https://crates.io/crates/metal
[`windows`]: https://crates.io/crates/windows
## Secondary backends
The `wgpu_hal` crate has a partial implementation based on the following
platform graphics API:
- The GL backend is available anywhere OpenGL, OpenGL ES, or WebGL are
available. See the [`gles`] module documentation for details.
[`gles`]: gles/index.html
You can see what capabilities an adapter is missing by checking the
[`DownlevelCapabilities`][tdc] in [`ExposedAdapter::capabilities`], available
from [`Instance::enumerate_adapters`].
The API is generally designed to fit the primary backends better than the
secondary backends, so the latter may impose more overhead.
[tdc]: wgt::DownlevelCapabilities
## Debugging
Most of the information on the wiki [Debugging wgpu Applications][wiki-debug]
page still applies to this API, with the exception of API tracing/replay
functionality, which is only available in `wgpu-core`.
[wiki-debug]: https://github.com/gfx-rs/wgpu/wiki/Debugging-wgpu-Applications
+36
View File
@@ -0,0 +1,36 @@
fn main() {
cfg_aliases::cfg_aliases! {
native: { not(target_arch = "wasm32") },
send_sync: { any(
not(target_arch = "wasm32"),
all(feature = "fragile-send-sync-non-atomic-wasm", not(target_feature = "atomics"))
) },
webgl: { all(target_arch = "wasm32", not(target_os = "emscripten"), gles) },
Emscripten: { all(target_os = "emscripten", gles) },
dx12: { all(target_os = "windows", feature = "dx12") },
gles: { all(feature = "gles") },
// Within the GL ES backend, use `std` and be Send + Sync only if we are using a target
// that, among the ones where the GL ES backend is supported, has `std`.
gles_with_std: { all(
feature = "gles",
any(
not(target_arch = "wasm32"),
// Accept wasm32-unknown-unknown, which uniquely has a stub `std`
all(target_vendor = "unknown", target_os = "unknown"),
// Accept wasm32-unknown-emscripten and similar, which has a real `std`
target_os = "emscripten"
)
) },
metal: { all(target_vendor = "apple", feature = "metal") },
vulkan: { all(not(target_arch = "wasm32"), feature = "vulkan") },
drm: { all(
feature = "drm",
any(target_os = "linux", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")
) },
any_backend: { any(dx12, metal, vulkan, gles) },
// ⚠️ Keep in sync with target.cfg() definition in Cargo.toml and cfg_alias in `wgpu` crate ⚠️
static_dxc: { all(target_os = "windows", feature = "static-dxc", not(target_arch = "aarch64"), target_env = "msvc") },
supports_64bit_atomics: { target_has_atomic = "64" },
supports_ptr_atomics: { target_has_atomic = "ptr" }
}
}
+301
View File
@@ -0,0 +1,301 @@
use alloc::string::String;
use std::{ffi::OsString, os::windows::ffi::OsStringExt};
use windows::Win32::Graphics::Dxgi;
// Helper to convert DXGI adapter name to a normal string
pub fn map_adapter_name(name: [u16; 128]) -> String {
let len = name.iter().take_while(|&&c| c != 0).count();
let name = OsString::from_wide(&name[..len]);
name.to_string_lossy().into_owned()
}
pub fn map_texture_format_failable(
format: wgt::TextureFormat,
) -> Option<Dxgi::Common::DXGI_FORMAT> {
use wgt::TextureFormat as Tf;
use Dxgi::Common::*;
Some(match format {
Tf::R8Unorm => DXGI_FORMAT_R8_UNORM,
Tf::R8Snorm => DXGI_FORMAT_R8_SNORM,
Tf::R8Uint => DXGI_FORMAT_R8_UINT,
Tf::R8Sint => DXGI_FORMAT_R8_SINT,
Tf::R16Uint => DXGI_FORMAT_R16_UINT,
Tf::R16Sint => DXGI_FORMAT_R16_SINT,
Tf::R16Unorm => DXGI_FORMAT_R16_UNORM,
Tf::R16Snorm => DXGI_FORMAT_R16_SNORM,
Tf::R16Float => DXGI_FORMAT_R16_FLOAT,
Tf::Rg8Unorm => DXGI_FORMAT_R8G8_UNORM,
Tf::Rg8Snorm => DXGI_FORMAT_R8G8_SNORM,
Tf::Rg8Uint => DXGI_FORMAT_R8G8_UINT,
Tf::Rg8Sint => DXGI_FORMAT_R8G8_SINT,
Tf::Rg16Unorm => DXGI_FORMAT_R16G16_UNORM,
Tf::Rg16Snorm => DXGI_FORMAT_R16G16_SNORM,
Tf::R32Uint => DXGI_FORMAT_R32_UINT,
Tf::R32Sint => DXGI_FORMAT_R32_SINT,
Tf::R32Float => DXGI_FORMAT_R32_FLOAT,
Tf::Rg16Uint => DXGI_FORMAT_R16G16_UINT,
Tf::Rg16Sint => DXGI_FORMAT_R16G16_SINT,
Tf::Rg16Float => DXGI_FORMAT_R16G16_FLOAT,
Tf::Rgba8Unorm => DXGI_FORMAT_R8G8B8A8_UNORM,
Tf::Rgba8UnormSrgb => DXGI_FORMAT_R8G8B8A8_UNORM_SRGB,
Tf::Bgra8UnormSrgb => DXGI_FORMAT_B8G8R8A8_UNORM_SRGB,
Tf::Rgba8Snorm => DXGI_FORMAT_R8G8B8A8_SNORM,
Tf::Bgra8Unorm => DXGI_FORMAT_B8G8R8A8_UNORM,
Tf::Rgba8Uint => DXGI_FORMAT_R8G8B8A8_UINT,
Tf::Rgba8Sint => DXGI_FORMAT_R8G8B8A8_SINT,
Tf::Rgb9e5Ufloat => DXGI_FORMAT_R9G9B9E5_SHAREDEXP,
Tf::Rgb10a2Uint => DXGI_FORMAT_R10G10B10A2_UINT,
Tf::Rgb10a2Unorm => DXGI_FORMAT_R10G10B10A2_UNORM,
Tf::Rg11b10Ufloat => DXGI_FORMAT_R11G11B10_FLOAT,
Tf::R64Uint => DXGI_FORMAT_R32G32_UINT, // R64 emulated by R32G32
Tf::Rg32Uint => DXGI_FORMAT_R32G32_UINT,
Tf::Rg32Sint => DXGI_FORMAT_R32G32_SINT,
Tf::Rg32Float => DXGI_FORMAT_R32G32_FLOAT,
Tf::Rgba16Uint => DXGI_FORMAT_R16G16B16A16_UINT,
Tf::Rgba16Sint => DXGI_FORMAT_R16G16B16A16_SINT,
Tf::Rgba16Unorm => DXGI_FORMAT_R16G16B16A16_UNORM,
Tf::Rgba16Snorm => DXGI_FORMAT_R16G16B16A16_SNORM,
Tf::Rgba16Float => DXGI_FORMAT_R16G16B16A16_FLOAT,
Tf::Rgba32Uint => DXGI_FORMAT_R32G32B32A32_UINT,
Tf::Rgba32Sint => DXGI_FORMAT_R32G32B32A32_SINT,
Tf::Rgba32Float => DXGI_FORMAT_R32G32B32A32_FLOAT,
Tf::Stencil8 => DXGI_FORMAT_D24_UNORM_S8_UINT,
Tf::Depth16Unorm => DXGI_FORMAT_D16_UNORM,
Tf::Depth24Plus => DXGI_FORMAT_D24_UNORM_S8_UINT,
Tf::Depth24PlusStencil8 => DXGI_FORMAT_D24_UNORM_S8_UINT,
Tf::Depth32Float => DXGI_FORMAT_D32_FLOAT,
Tf::Depth32FloatStencil8 => DXGI_FORMAT_D32_FLOAT_S8X24_UINT,
Tf::NV12 => DXGI_FORMAT_NV12,
Tf::P010 => DXGI_FORMAT_P010,
Tf::Bc1RgbaUnorm => DXGI_FORMAT_BC1_UNORM,
Tf::Bc1RgbaUnormSrgb => DXGI_FORMAT_BC1_UNORM_SRGB,
Tf::Bc2RgbaUnorm => DXGI_FORMAT_BC2_UNORM,
Tf::Bc2RgbaUnormSrgb => DXGI_FORMAT_BC2_UNORM_SRGB,
Tf::Bc3RgbaUnorm => DXGI_FORMAT_BC3_UNORM,
Tf::Bc3RgbaUnormSrgb => DXGI_FORMAT_BC3_UNORM_SRGB,
Tf::Bc4RUnorm => DXGI_FORMAT_BC4_UNORM,
Tf::Bc4RSnorm => DXGI_FORMAT_BC4_SNORM,
Tf::Bc5RgUnorm => DXGI_FORMAT_BC5_UNORM,
Tf::Bc5RgSnorm => DXGI_FORMAT_BC5_SNORM,
Tf::Bc6hRgbUfloat => DXGI_FORMAT_BC6H_UF16,
Tf::Bc6hRgbFloat => DXGI_FORMAT_BC6H_SF16,
Tf::Bc7RgbaUnorm => DXGI_FORMAT_BC7_UNORM,
Tf::Bc7RgbaUnormSrgb => DXGI_FORMAT_BC7_UNORM_SRGB,
Tf::Etc2Rgb8Unorm
| Tf::Etc2Rgb8UnormSrgb
| Tf::Etc2Rgb8A1Unorm
| Tf::Etc2Rgb8A1UnormSrgb
| Tf::Etc2Rgba8Unorm
| Tf::Etc2Rgba8UnormSrgb
| Tf::EacR11Unorm
| Tf::EacR11Snorm
| Tf::EacRg11Unorm
| Tf::EacRg11Snorm
| Tf::Astc {
block: _,
channel: _,
} => return None,
})
}
pub fn map_texture_format(format: wgt::TextureFormat) -> Dxgi::Common::DXGI_FORMAT {
match map_texture_format_failable(format) {
Some(f) => f,
None => unreachable!(),
}
}
// Note: DXGI doesn't allow sRGB format on the swapchain,
// but creating RTV of swapchain buffers with sRGB works.
pub fn map_texture_format_nosrgb(format: wgt::TextureFormat) -> Dxgi::Common::DXGI_FORMAT {
match format {
wgt::TextureFormat::Bgra8UnormSrgb => Dxgi::Common::DXGI_FORMAT_B8G8R8A8_UNORM,
wgt::TextureFormat::Rgba8UnormSrgb => Dxgi::Common::DXGI_FORMAT_R8G8B8A8_UNORM,
_ => map_texture_format(format),
}
}
// SRV and UAV can't use the depth or typeless formats
// see https://microsoft.github.io/DirectX-Specs/d3d/PlanarDepthStencilDDISpec.html#view-creation
pub fn map_texture_format_for_srv_uav(
format: wgt::TextureFormat,
aspect: crate::FormatAspects,
) -> Option<Dxgi::Common::DXGI_FORMAT> {
Some(match (format, aspect) {
(wgt::TextureFormat::Depth16Unorm, crate::FormatAspects::DEPTH) => {
Dxgi::Common::DXGI_FORMAT_R16_UNORM
}
(wgt::TextureFormat::Depth32Float, crate::FormatAspects::DEPTH) => {
Dxgi::Common::DXGI_FORMAT_R32_FLOAT
}
(wgt::TextureFormat::Depth32FloatStencil8, crate::FormatAspects::DEPTH) => {
Dxgi::Common::DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS
}
(
wgt::TextureFormat::Depth24Plus | wgt::TextureFormat::Depth24PlusStencil8,
crate::FormatAspects::DEPTH,
) => Dxgi::Common::DXGI_FORMAT_R24_UNORM_X8_TYPELESS,
(wgt::TextureFormat::Depth32FloatStencil8, crate::FormatAspects::STENCIL) => {
Dxgi::Common::DXGI_FORMAT_X32_TYPELESS_G8X24_UINT
}
(
wgt::TextureFormat::Stencil8 | wgt::TextureFormat::Depth24PlusStencil8,
crate::FormatAspects::STENCIL,
) => Dxgi::Common::DXGI_FORMAT_X24_TYPELESS_G8_UINT,
(_, crate::FormatAspects::DEPTH)
| (_, crate::FormatAspects::STENCIL)
| (_, crate::FormatAspects::DEPTH_STENCIL) => return None,
_ => map_texture_format(format),
})
}
// see https://microsoft.github.io/DirectX-Specs/d3d/PlanarDepthStencilDDISpec.html#planar-layout-for-staging-from-buffer
pub fn map_texture_format_for_copy(
format: wgt::TextureFormat,
aspect: crate::FormatAspects,
) -> Option<Dxgi::Common::DXGI_FORMAT> {
Some(match (format, aspect) {
(wgt::TextureFormat::Depth16Unorm, crate::FormatAspects::DEPTH) => {
Dxgi::Common::DXGI_FORMAT_R16_UNORM
}
(
wgt::TextureFormat::Depth32Float | wgt::TextureFormat::Depth32FloatStencil8,
crate::FormatAspects::DEPTH,
) => Dxgi::Common::DXGI_FORMAT_R32_FLOAT,
(
wgt::TextureFormat::Stencil8
| wgt::TextureFormat::Depth24PlusStencil8
| wgt::TextureFormat::Depth32FloatStencil8,
crate::FormatAspects::STENCIL,
) => Dxgi::Common::DXGI_FORMAT_R8_UINT,
// `CopyTextureRegion` on a plane subresource wants the
// single-plane DXGI format, not the planar one.
(format, aspects) if format.is_multi_planar_format() && aspects.is_one() => {
map_texture_format(format.aspect_specific_format(aspects.map())?)
}
(format, crate::FormatAspects::COLOR) => map_texture_format(format),
_ => return None,
})
}
pub fn map_texture_format_for_resource(
format: wgt::TextureFormat,
usage: wgt::TextureUses,
has_view_formats: bool,
casting_fully_typed_format_supported: bool,
) -> Dxgi::Common::DXGI_FORMAT {
use wgt::TextureFormat as Tf;
use Dxgi::Common::*;
if casting_fully_typed_format_supported {
map_texture_format(format)
// We might view this resource as srgb or non-srgb
} else if has_view_formats {
match format {
Tf::Rgba8Unorm | Tf::Rgba8UnormSrgb => DXGI_FORMAT_R8G8B8A8_TYPELESS,
Tf::Bgra8Unorm | Tf::Bgra8UnormSrgb => DXGI_FORMAT_B8G8R8A8_TYPELESS,
Tf::Bc1RgbaUnorm | Tf::Bc1RgbaUnormSrgb => DXGI_FORMAT_BC1_TYPELESS,
Tf::Bc2RgbaUnorm | Tf::Bc2RgbaUnormSrgb => DXGI_FORMAT_BC2_TYPELESS,
Tf::Bc3RgbaUnorm | Tf::Bc3RgbaUnormSrgb => DXGI_FORMAT_BC3_TYPELESS,
Tf::Bc7RgbaUnorm | Tf::Bc7RgbaUnormSrgb => DXGI_FORMAT_BC7_TYPELESS,
format => map_texture_format(format),
}
// We might view this resource as SRV/UAV but also as DSV
} else if format.is_depth_stencil_format()
&& usage.intersects(
wgt::TextureUses::RESOURCE
| wgt::TextureUses::STORAGE_READ_ONLY
| wgt::TextureUses::STORAGE_WRITE_ONLY
| wgt::TextureUses::STORAGE_READ_WRITE,
)
{
match format {
Tf::Depth16Unorm => DXGI_FORMAT_R16_TYPELESS,
Tf::Depth32Float => DXGI_FORMAT_R32_TYPELESS,
Tf::Depth32FloatStencil8 => DXGI_FORMAT_R32G8X24_TYPELESS,
Tf::Stencil8 | Tf::Depth24Plus | Tf::Depth24PlusStencil8 => DXGI_FORMAT_R24G8_TYPELESS,
_ => unreachable!(),
}
} else {
map_texture_format(format)
}
}
pub fn map_index_format(format: wgt::IndexFormat) -> Dxgi::Common::DXGI_FORMAT {
match format {
wgt::IndexFormat::Uint16 => Dxgi::Common::DXGI_FORMAT_R16_UINT,
wgt::IndexFormat::Uint32 => Dxgi::Common::DXGI_FORMAT_R32_UINT,
}
}
pub fn map_vertex_format(format: wgt::VertexFormat) -> Dxgi::Common::DXGI_FORMAT {
use wgt::VertexFormat as Vf;
use Dxgi::Common::*;
match format {
Vf::Unorm8 => DXGI_FORMAT_R8_UNORM,
Vf::Snorm8 => DXGI_FORMAT_R8_SNORM,
Vf::Uint8 => DXGI_FORMAT_R8_UINT,
Vf::Sint8 => DXGI_FORMAT_R8_SINT,
Vf::Unorm8x2 => DXGI_FORMAT_R8G8_UNORM,
Vf::Snorm8x2 => DXGI_FORMAT_R8G8_SNORM,
Vf::Uint8x2 => DXGI_FORMAT_R8G8_UINT,
Vf::Sint8x2 => DXGI_FORMAT_R8G8_SINT,
Vf::Unorm8x4 => DXGI_FORMAT_R8G8B8A8_UNORM,
Vf::Snorm8x4 => DXGI_FORMAT_R8G8B8A8_SNORM,
Vf::Uint8x4 => DXGI_FORMAT_R8G8B8A8_UINT,
Vf::Sint8x4 => DXGI_FORMAT_R8G8B8A8_SINT,
Vf::Unorm16 => DXGI_FORMAT_R16_UNORM,
Vf::Snorm16 => DXGI_FORMAT_R16_SNORM,
Vf::Uint16 => DXGI_FORMAT_R16_UINT,
Vf::Sint16 => DXGI_FORMAT_R16_SINT,
Vf::Float16 => DXGI_FORMAT_R16_FLOAT,
Vf::Unorm16x2 => DXGI_FORMAT_R16G16_UNORM,
Vf::Snorm16x2 => DXGI_FORMAT_R16G16_SNORM,
Vf::Uint16x2 => DXGI_FORMAT_R16G16_UINT,
Vf::Sint16x2 => DXGI_FORMAT_R16G16_SINT,
Vf::Float16x2 => DXGI_FORMAT_R16G16_FLOAT,
Vf::Unorm16x4 => DXGI_FORMAT_R16G16B16A16_UNORM,
Vf::Snorm16x4 => DXGI_FORMAT_R16G16B16A16_SNORM,
Vf::Uint16x4 => DXGI_FORMAT_R16G16B16A16_UINT,
Vf::Sint16x4 => DXGI_FORMAT_R16G16B16A16_SINT,
Vf::Float16x4 => DXGI_FORMAT_R16G16B16A16_FLOAT,
Vf::Uint32 => DXGI_FORMAT_R32_UINT,
Vf::Sint32 => DXGI_FORMAT_R32_SINT,
Vf::Float32 => DXGI_FORMAT_R32_FLOAT,
Vf::Uint32x2 => DXGI_FORMAT_R32G32_UINT,
Vf::Sint32x2 => DXGI_FORMAT_R32G32_SINT,
Vf::Float32x2 => DXGI_FORMAT_R32G32_FLOAT,
Vf::Uint32x3 => DXGI_FORMAT_R32G32B32_UINT,
Vf::Sint32x3 => DXGI_FORMAT_R32G32B32_SINT,
Vf::Float32x3 => DXGI_FORMAT_R32G32B32_FLOAT,
Vf::Uint32x4 => DXGI_FORMAT_R32G32B32A32_UINT,
Vf::Sint32x4 => DXGI_FORMAT_R32G32B32A32_SINT,
Vf::Float32x4 => DXGI_FORMAT_R32G32B32A32_FLOAT,
Vf::Unorm10_10_10_2 => DXGI_FORMAT_R10G10B10A2_UNORM,
Vf::Unorm8x4Bgra => DXGI_FORMAT_B8G8R8A8_UNORM,
Vf::Float64 | Vf::Float64x2 | Vf::Float64x3 | Vf::Float64x4 => unimplemented!(),
}
}
pub fn map_acomposite_alpha_mode(mode: wgt::CompositeAlphaMode) -> Dxgi::Common::DXGI_ALPHA_MODE {
match mode {
wgt::CompositeAlphaMode::PreMultiplied => Dxgi::Common::DXGI_ALPHA_MODE_PREMULTIPLIED,
wgt::CompositeAlphaMode::PostMultiplied => Dxgi::Common::DXGI_ALPHA_MODE_STRAIGHT,
wgt::CompositeAlphaMode::Opaque => Dxgi::Common::DXGI_ALPHA_MODE_IGNORE,
wgt::CompositeAlphaMode::Auto | wgt::CompositeAlphaMode::Inherit => {
Dxgi::Common::DXGI_ALPHA_MODE_UNSPECIFIED
}
}
}
+97
View File
@@ -0,0 +1,97 @@
use alloc::{borrow::Cow, string::String};
use parking_lot::Mutex;
use windows::Win32::{Foundation, System::Diagnostics::Debug};
// This is a mutex as opposed to an atomic as we need to completely
// lock everyone out until we have registered or unregistered the
// exception handler, otherwise really nasty races could happen.
//
// By routing all the registration through these functions we can guarantee
// there is either 1 or 0 exception handlers registered, not multiple.
static EXCEPTION_HANDLER_COUNT: Mutex<usize> = Mutex::new(0);
pub fn register_exception_handler() {
let mut count_guard = EXCEPTION_HANDLER_COUNT.lock();
if *count_guard == 0 {
unsafe { Debug::AddVectoredExceptionHandler(0, Some(output_debug_string_handler)) };
}
*count_guard += 1;
}
pub fn unregister_exception_handler() {
let mut count_guard = EXCEPTION_HANDLER_COUNT.lock();
if *count_guard == 1 {
unsafe { Debug::RemoveVectoredExceptionHandler(output_debug_string_handler as *mut _) };
}
*count_guard -= 1;
}
const MESSAGE_PREFIXES: &[(&str, log::Level)] = &[
("CORRUPTION", log::Level::Error),
("ERROR", log::Level::Error),
("WARNING", log::Level::Warn),
// We intentionally suppress "INFO" messages down to debug
// so that users are not innundated with info messages from the runtime.
("INFO", log::Level::Debug),
("MESSAGE", log::Level::Trace),
];
unsafe extern "system" fn output_debug_string_handler(
exception_info: *mut Debug::EXCEPTION_POINTERS,
) -> i32 {
// See https://stackoverflow.com/a/41480827
let record = unsafe { &*(*exception_info).ExceptionRecord };
if record.NumberParameters != 2 {
return Debug::EXCEPTION_CONTINUE_SEARCH;
}
let message = match record.ExceptionCode {
Foundation::DBG_PRINTEXCEPTION_C => {
String::from_utf8_lossy(bytemuck::cast_slice(&record.ExceptionInformation))
}
Foundation::DBG_PRINTEXCEPTION_WIDE_C => Cow::Owned(String::from_utf16_lossy(
bytemuck::cast_slice(&record.ExceptionInformation),
)),
_ => return Debug::EXCEPTION_CONTINUE_SEARCH,
};
let message = match message.strip_prefix("D3D12 ") {
Some(msg) => msg
.trim_end_matches("\n\0")
.trim_end_matches("[ STATE_CREATION WARNING #0: UNKNOWN]"),
None => return Debug::EXCEPTION_CONTINUE_SEARCH,
};
let (message, level) = match MESSAGE_PREFIXES
.iter()
.find(|&&(prefix, _)| message.starts_with(prefix))
{
Some(&(prefix, level)) => (&message[prefix.len() + 2..], level),
None => (message, log::Level::Debug),
};
if level == log::Level::Warn && message.contains("#82") {
// This is are useless spammy warnings (#820, #821):
// "The application did not pass any clear value to resource creation"
return Debug::EXCEPTION_CONTINUE_SEARCH;
}
if level == log::Level::Warn && message.contains("DRAW_EMPTY_SCISSOR_RECTANGLE") {
// This is normal, WebGPU allows passing empty scissor rectangles.
return Debug::EXCEPTION_CONTINUE_SEARCH;
}
let _ = std::panic::catch_unwind(|| {
log::log!(level, "{message}");
});
#[cfg(feature = "validation_canary")]
if cfg!(debug_assertions) && level == log::Level::Error {
use alloc::string::ToString as _;
// Set canary and continue
crate::VALIDATION_CANARY.add(message.to_string());
}
Debug::EXCEPTION_CONTINUE_EXECUTION
}
+181
View File
@@ -0,0 +1,181 @@
use alloc::{string::String, vec::Vec};
use core::ops::Deref;
use windows::{core::Interface as _, Win32::Graphics::Dxgi};
use crate::dx12::DxgiLib;
use super::result::HResult as _;
// We can rely on the presence of DXGI 1.4 since D3D12 requires WDDM 2.0, Windows 10 (1507), and so does DXGI 1.4.
fn should_keep_adapter(adapter: &Dxgi::IDXGIAdapter1) -> bool {
let desc = unsafe { adapter.GetDesc1() }.unwrap();
// The Intel Haswell family of iGPUs had support for the D3D12 API but it was later
// removed due to a security vulnerability.
//
// We are explicitly filtering out all the devices in the family because we are now
// getting reports of device loss at a later time than at device creation time (`D3D12CreateDevice`).
//
// See https://www.intel.com/content/www/us/en/support/articles/000057520/graphics.html
// This list of device IDs is from https://dgpu-docs.intel.com/devices/hardware-table.html
let haswell_device_ids = [
0x0422, 0x0426, 0x042A, 0x042B, 0x042E, 0x0C22, 0x0C26, 0x0C2A, 0x0C2B, 0x0C2E, 0x0A22,
0x0A2A, 0x0A2B, 0x0D2A, 0x0D2B, 0x0D2E, 0x0A26, 0x0A2E, 0x0D22, 0x0D26, 0x0412, 0x0416,
0x0D12, 0x041A, 0x041B, 0x0C12, 0x0C16, 0x0C1A, 0x0C1B, 0x0C1E, 0x0A12, 0x0A1A, 0x0A1B,
0x0D16, 0x0D1A, 0x0D1B, 0x0D1E, 0x041E, 0x0A16, 0x0A1E, 0x0402, 0x0406, 0x040A, 0x040B,
0x040E, 0x0C02, 0x0C06, 0x0C0A, 0x0C0B, 0x0C0E, 0x0A02, 0x0A06, 0x0A0A, 0x0A0B, 0x0A0E,
0x0D02, 0x0D06, 0x0D0A, 0x0D0B, 0x0D0E,
];
if desc.VendorId == 0x8086 && haswell_device_ids.contains(&desc.DeviceId) {
return false;
}
// If run completely headless, windows will show two different WARP adapters, one
// which is lying about being an integrated card. This is so that programs
// that ignore software adapters will actually run on headless/gpu-less machines.
//
// We don't want that and discourage that kind of filtering anyway, so we skip the integrated WARP.
if desc.VendorId == 5140
&& !Dxgi::DXGI_ADAPTER_FLAG(desc.Flags as i32).contains(Dxgi::DXGI_ADAPTER_FLAG_SOFTWARE)
{
let adapter_name = super::conv::map_adapter_name(desc.Description);
if adapter_name.contains("Microsoft Basic Render Driver") {
return false;
}
}
true
}
#[derive(Clone, Debug)]
pub enum DxgiAdapter {
/// Provided by DXGI 1.4
Adapter3(Dxgi::IDXGIAdapter3),
/// Provided by DXGI 1.6
Adapter4(Dxgi::IDXGIAdapter4),
}
impl DxgiAdapter {
pub fn query_video_memory_info(
&self,
group: Dxgi::DXGI_MEMORY_SEGMENT_GROUP,
) -> Result<Dxgi::DXGI_QUERY_VIDEO_MEMORY_INFO, crate::DeviceError> {
let mut info = Dxgi::DXGI_QUERY_VIDEO_MEMORY_INFO::default();
unsafe { self.QueryVideoMemoryInfo(0, group, &mut info) }
.into_device_result("QueryVideoMemoryInfo")?;
Ok(info)
}
}
impl Deref for DxgiAdapter {
type Target = Dxgi::IDXGIAdapter3;
fn deref(&self) -> &Self::Target {
match self {
DxgiAdapter::Adapter3(a) => a,
DxgiAdapter::Adapter4(a) => a,
}
}
}
pub fn enumerate_adapters(factory: DxgiFactory) -> Vec<DxgiAdapter> {
let mut adapters = Vec::with_capacity(8);
for cur_index in 0.. {
profiling::scope!("IDXGIFactory1::EnumAdapters1");
let adapter1: Dxgi::IDXGIAdapter1 = match unsafe { factory.EnumAdapters1(cur_index) } {
Ok(a) => a,
Err(e) if e.code() == Dxgi::DXGI_ERROR_NOT_FOUND => break,
Err(e) => {
log::error!("Failed enumerating adapters: {e}");
break;
}
};
if !should_keep_adapter(&adapter1) {
continue;
}
if let Ok(adapter4) = adapter1.cast::<Dxgi::IDXGIAdapter4>() {
adapters.push(DxgiAdapter::Adapter4(adapter4));
} else {
let adapter3 = adapter1.cast::<Dxgi::IDXGIAdapter3>().unwrap();
adapters.push(DxgiAdapter::Adapter3(adapter3));
}
}
adapters
}
#[derive(Clone, Debug)]
pub enum DxgiFactory {
/// Provided by DXGI 1.4
Factory4(Dxgi::IDXGIFactory4),
/// Provided by DXGI 1.5
Factory5(Dxgi::IDXGIFactory5),
/// Provided by DXGI 1.6
Factory6(Dxgi::IDXGIFactory6),
}
impl Deref for DxgiFactory {
type Target = Dxgi::IDXGIFactory4;
fn deref(&self) -> &Self::Target {
match self {
DxgiFactory::Factory4(f) => f,
DxgiFactory::Factory5(f) => f,
DxgiFactory::Factory6(f) => f,
}
}
}
impl DxgiFactory {
pub fn as_factory5(&self) -> Option<&Dxgi::IDXGIFactory5> {
match self {
Self::Factory4(_) => None,
Self::Factory5(f) => Some(f),
Self::Factory6(f) => Some(f),
}
}
}
pub fn create_factory(
instance_flags: wgt::InstanceFlags,
) -> Result<(DxgiLib, DxgiFactory), crate::InstanceError> {
let lib_dxgi = DxgiLib::new().map_err(|e| {
crate::InstanceError::with_source(String::from("failed to load dxgi.dll"), e)
})?;
let mut factory_flags = Dxgi::DXGI_CREATE_FACTORY_FLAGS::default();
if instance_flags.contains(wgt::InstanceFlags::VALIDATION) {
// The `DXGI_CREATE_FACTORY_DEBUG` flag is only allowed to be passed to
// `CreateDXGIFactory2` if the debug interface is actually available. So
// we check for whether it exists first.
if let Ok(Some(_)) = lib_dxgi.debug_interface1() {
factory_flags |= Dxgi::DXGI_CREATE_FACTORY_DEBUG;
}
}
let factory4 = match lib_dxgi.create_factory4(factory_flags) {
Ok(factory) => factory,
Err(err) => {
return Err(crate::InstanceError::with_source(
String::from("IDXGIFactory4 creation failed"),
err,
));
}
};
if let Ok(factory6) = factory4.cast::<Dxgi::IDXGIFactory6>() {
return Ok((lib_dxgi, DxgiFactory::Factory6(factory6)));
}
if let Ok(factory5) = factory4.cast::<Dxgi::IDXGIFactory5>() {
return Ok((lib_dxgi, DxgiFactory::Factory5(factory5)));
}
Ok((lib_dxgi, DxgiFactory::Factory4(factory4)))
}
+352
View File
@@ -0,0 +1,352 @@
//! Reading a window's [`wgt::DisplayHdrInfo`] through DXGI.
//!
//! Lives in `auxil/dxgi` so the DX12 and Vulkan-on-Windows backends share it:
//! both query through a [`DxgiHdrSource`], so the same monitor reports identical
//! numbers under either backend.
use parking_lot::Mutex;
use windows::{
core::Interface as _,
Win32::{
Foundation::HWND,
Graphics::{Dxgi, Gdi},
},
};
/// The primary colors of a color space, in CIE 1931 xy:
/// `[[red_x, red_y], [green_x, green_y], [blue_x, blue_y]]`.
type Primaries = [[f32; 2]; 3];
/// Reference primaries (CIE 1931 xy: red, green, blue) for the three coarse
/// gamut buckets, used to classify a display's reported primaries.
const REC709: Primaries = [[0.640, 0.330], [0.300, 0.600], [0.150, 0.060]];
const DISPLAY_P3: Primaries = [[0.680, 0.320], [0.265, 0.690], [0.150, 0.060]];
const REC2020: Primaries = [[0.708, 0.292], [0.170, 0.797], [0.131, 0.046]];
/// Classifies reported primaries into the closest coarse [`wgt::DisplayGamut`]
/// bucket (least-squares over the R/G/B chromaticities). Advisory only.
///
/// Returns `None` if every primary is zeroed (the EDID reported nothing usable),
/// so a bogus all-zero descriptor is not misclassified as sRGB.
fn classify_gamut(red: [f32; 2], green: [f32; 2], blue: [f32; 2]) -> Option<wgt::DisplayGamut> {
let measured = [red, green, blue];
if measured.iter().all(|p| p[0] == 0.0 && p[1] == 0.0) {
return None;
}
let dist = |reference: &Primaries| -> f32 {
measured
.iter()
.zip(reference)
.map(|(m, r)| {
let dx = m[0] - r[0];
let dy = m[1] - r[1];
dx * dx + dy * dy
})
.sum()
};
[
(wgt::DisplayGamut::Srgb, dist(&REC709)),
(wgt::DisplayGamut::DisplayP3, dist(&DISPLAY_P3)),
(wgt::DisplayGamut::Rec2020, dist(&REC2020)),
]
.into_iter()
.min_by(|a, b| a.1.total_cmp(&b.1))
.map(|(gamut, _)| gamut)
}
/// Maps an [`IDXGIOutput6::GetDesc1`] result and the SDR white level (from
/// [`sdr_white_nits_for_monitor`]) into the backend-agnostic
/// [`wgt::DisplayHdrInfo`].
///
/// Pure: it interprets `desc1` without any system call. Numeric values are
/// advisory (EDID-sourced); see [`wgt::DisplayLuminance`].
///
/// [`IDXGIOutput6::GetDesc1`]: windows::Win32::Graphics::Dxgi::IDXGIOutput6::GetDesc1
fn display_hdr_info_from_desc1(
desc1: &Dxgi::DXGI_OUTPUT_DESC1,
sdr_white_nits: Option<f32>,
) -> wgt::DisplayHdrInfo {
// The output is in an HDR color space if its `ColorSpace` is one of the two
// Windows uses for HDR swap-chains: HDR10 / PQ (`G2084_NONE_P2020`) or
// scRGB / linear-extended-sRGB (`G10_NONE_P709`). Checking both ensures scRGB
// HDR is not misreported as SDR.
let high_dynamic_range = desc1.ColorSpace
== Dxgi::Common::DXGI_COLOR_SPACE_RGB_FULL_G2084_NONE_P2020
|| desc1.ColorSpace == Dxgi::Common::DXGI_COLOR_SPACE_RGB_FULL_G10_NONE_P709;
let luminance = wgt::DisplayLuminance {
max_nits: Some(desc1.MaxLuminance),
max_full_frame_nits: Some(desc1.MaxFullFrameLuminance),
min_nits: Some(desc1.MinLuminance),
sdr_white_nits,
};
let chromaticity = wgt::DisplayChromaticity {
red: Some(desc1.RedPrimary),
green: Some(desc1.GreenPrimary),
blue: Some(desc1.BluePrimary),
white: Some(desc1.WhitePoint),
};
let coarse = wgt::DisplayCoarseRange {
high_dynamic_range: Some(high_dynamic_range),
gamut: classify_gamut(desc1.RedPrimary, desc1.GreenPrimary, desc1.BluePrimary),
};
// `headroom` is `None`: DXGI exposes absolute luminance (captured in
// `luminance`) rather than a relative headroom ratio. `bits_per_color` drops
// values that don't fit `u8`.
wgt::DisplayHdrInfo {
luminance: Some(luminance),
headroom: None,
chromaticity: Some(chromaticity),
coarse: Some(coarse),
bits_per_color: u8::try_from(desc1.BitsPerColor).ok(),
}
}
/// A window's display HDR info, read through DXGI.
///
/// Bundles the window's `HWND` with the DXGI factory used to query it. The factory
/// is built once and reused, rebuilt only when DXGI reports it stale; the monitor
/// lookup and adapter/output walk still run every call, since the window can move
/// between displays and the SDR white level changes at runtime.
pub struct DxgiHdrSource {
hwnd: HWND,
factory: Mutex<Option<Dxgi::IDXGIFactory1>>,
}
// SAFETY: `HWND` and `IDXGIFactory1` are `!Send`/`!Sync` in windows-rs. The `HWND`
// is only ever read to run a DXGI query (fine from any thread), and the factory is
// free-threaded for the enumeration done here with the `Mutex` serializing access.
// This mirrors the manual `Send`/`Sync` on `dx12::Surface`.
unsafe impl Send for DxgiHdrSource {}
unsafe impl Sync for DxgiHdrSource {}
impl DxgiHdrSource {
/// Creates a source for the window identified by `hwnd`. The `HWND` is
/// borrowed; using the source after that window is destroyed is undefined
/// behavior.
pub fn new(hwnd: HWND) -> Self {
Self {
hwnd,
factory: Mutex::new(None),
}
}
/// Reads the window's [`wgt::DisplayHdrInfo`], or `None` if its monitor can't
/// be identified or queried (a headless or composition window, a
/// pre-Win10-1703 system without `IDXGIOutput6`, or a COM failure). Never
/// panics.
pub fn display_hdr_info(&self) -> Option<wgt::DisplayHdrInfo> {
let desc1 = self.output_desc1()?;
let sdr_white_nits = sdr_white_nits_for_monitor(desc1.Monitor);
Some(display_hdr_info_from_desc1(&desc1, sdr_white_nits))
}
/// The [`DXGI_OUTPUT_DESC1`] for the window's monitor, reusing the cached
/// factory unless DXGI reports it stale.
///
/// [`DXGI_OUTPUT_DESC1`]: Dxgi::DXGI_OUTPUT_DESC1
fn output_desc1(&self) -> Option<Dxgi::DXGI_OUTPUT_DESC1> {
let mut factory = self.factory.lock();
// A factory only enumerates the adapter/output topology it saw at
// creation, so rebuild it once it reports `IsCurrent() == false` (a
// monitor or GPU was plugged or unplugged).
// SAFETY: a cached factory is live; `IsCurrent` takes no caller pointers.
let need_new = factory
.as_ref()
.is_none_or(|f| !unsafe { f.IsCurrent() }.as_bool());
if need_new {
// SAFETY: `CreateDXGIFactory1` takes no caller pointers; the `windows`
// binding fills the interface out-pointer itself.
match unsafe { Dxgi::CreateDXGIFactory1() } {
Ok(new) => *factory = Some(new),
Err(e) => {
log::warn!("CreateDXGIFactory1 failed: {e}");
return None;
}
}
}
output_desc1_from_factory(factory.as_ref()?, self.hwnd)
}
}
/// The [`DXGI_OUTPUT_DESC1`] for the monitor backing `wnd_handle`, found by
/// walking every adapter's outputs on `factory` — not just the rendering
/// adapter's, so the monitor is found on hybrid-GPU systems where the window sits
/// on a display wired to a different adapter. `None` if none match.
///
/// [`DXGI_OUTPUT_DESC1`]: Dxgi::DXGI_OUTPUT_DESC1
fn output_desc1_from_factory(
factory: &Dxgi::IDXGIFactory1,
wnd_handle: HWND,
) -> Option<Dxgi::DXGI_OUTPUT_DESC1> {
// SAFETY: `MonitorFromWindow` is sound for any `HWND`; an invalid one yields
// a null `HMONITOR`, checked below.
let hmonitor = unsafe { Gdi::MonitorFromWindow(wnd_handle, Gdi::MONITOR_DEFAULTTONEAREST) };
if hmonitor.is_invalid() {
log::warn!("MonitorFromWindow failed; cannot identify the window's output");
return None;
}
for adapter_index in 0.. {
// SAFETY: `factory` is live; `EnumAdapters1` takes only an index.
let adapter = match unsafe { factory.EnumAdapters1(adapter_index) } {
Ok(adapter) => adapter,
// End of the adapter list: the monitor matched none of them.
Err(e) if e.code() == Dxgi::DXGI_ERROR_NOT_FOUND => break,
Err(e) => {
log::warn!("IDXGIFactory1::EnumAdapters1 failed: {e}");
break;
}
};
for output_index in 0.. {
// SAFETY: `adapter` is live (from above); `EnumOutputs` takes only
// an index.
let output = match unsafe { adapter.EnumOutputs(output_index) } {
Ok(output) => output,
// End of this adapter's outputs: move on to the next adapter.
Err(e) if e.code() == Dxgi::DXGI_ERROR_NOT_FOUND => break,
Err(e) => {
log::warn!("IDXGIAdapter1::EnumOutputs failed: {e}");
break;
}
};
// SAFETY: `output` is live (from above); `GetDesc` returns a
// binding-owned `DXGI_OUTPUT_DESC` by value.
let desc = match unsafe { output.GetDesc() } {
Ok(desc) => desc,
Err(e) => {
log::warn!("IDXGIOutput::GetDesc failed: {e}");
continue;
}
};
if desc.Monitor != hmonitor {
continue;
}
// The window's monitor matched, so a failure past this point is a
// real anomaly. `IDXGIOutput6` (DXGI 1.6 / Win10 1703+) is required
// for `GetDesc1`.
let output6 = match output.cast::<Dxgi::IDXGIOutput6>() {
Ok(output6) => output6,
Err(e) => {
log::warn!("Casting to IDXGIOutput6 failed: {e}");
return None;
}
};
// SAFETY: `output6` is live (from the cast above); `GetDesc1`
// returns a binding-owned `DXGI_OUTPUT_DESC1` by value.
return match unsafe { output6.GetDesc1() } {
Ok(desc1) => Some(desc1),
Err(e) => {
log::warn!("IDXGIOutput6::GetDesc1 failed: {e}");
None
}
};
}
}
log::warn!("No DXGI output matches the window's monitor");
None
}
/// Best-effort SDR white level in nits for `hmonitor`, via the Windows
/// DisplayConfig API. Tracks the brightness slider, so it changes at runtime.
///
/// This is the only [`wgt::DisplayLuminance`] field `GetDesc1` doesn't carry; it
/// bridges absolute nits and relative headroom. It lives in
/// `DISPLAYCONFIG_SDR_WHITE_LEVEL`, outside DXGI, so it needs its own query.
///
/// Returns `None` on any failure (no match, query error, or a `0` reading). Never
/// panics. Pass [`Dxgi::DXGI_OUTPUT_DESC1::Monitor`] so the value matches the rest
/// of the `DisplayHdrInfo`.
fn sdr_white_nits_for_monitor(hmonitor: Gdi::HMONITOR) -> Option<f32> {
use windows::Win32::{
Devices::Display::{
DisplayConfigGetDeviceInfo, GetDisplayConfigBufferSizes, QueryDisplayConfig,
DISPLAYCONFIG_DEVICE_INFO_GET_SDR_WHITE_LEVEL,
DISPLAYCONFIG_DEVICE_INFO_GET_SOURCE_NAME, DISPLAYCONFIG_MODE_INFO,
DISPLAYCONFIG_PATH_INFO, DISPLAYCONFIG_SDR_WHITE_LEVEL,
DISPLAYCONFIG_SOURCE_DEVICE_NAME, QDC_ONLY_ACTIVE_PATHS,
},
Foundation::ERROR_SUCCESS,
Graphics::Gdi::{GetMonitorInfoW, MONITORINFO, MONITORINFOEXW},
};
// 1. HMONITOR -> GDI device name (e.g. `\\.\DISPLAY1`).
let mut monitor_info = MONITORINFOEXW::default();
monitor_info.monitorInfo.cbSize = size_of::<MONITORINFOEXW>() as u32;
// `GetMonitorInfoW` takes a `*mut MONITORINFO`; the `EXW` variant is
// layout-compatible (it starts with `MONITORINFO`) and reports `cbSize`.
let lpmi = core::ptr::from_mut(&mut monitor_info).cast::<MONITORINFO>();
// SAFETY: `lpmi` points at a live `MONITORINFOEXW` with `cbSize` set, which
// bounds the write; an invalid `hmonitor` just fails (handled below).
let got_info = unsafe { GetMonitorInfoW(hmonitor, lpmi) };
if !got_info.as_bool() {
return None;
}
let gdi_device_name = monitor_info.szDevice;
// 2. Enumerate the active DisplayConfig paths.
let mut path_count = 0u32;
let mut mode_count = 0u32;
// SAFETY: both out-params are live `u32`s; the call only writes the required
// buffer sizes into them.
if unsafe {
GetDisplayConfigBufferSizes(QDC_ONLY_ACTIVE_PATHS, &mut path_count, &mut mode_count)
} != ERROR_SUCCESS
{
return None;
}
let mut paths = alloc::vec![DISPLAYCONFIG_PATH_INFO::default(); path_count as usize];
let mut modes = alloc::vec![DISPLAYCONFIG_MODE_INFO::default(); mode_count as usize];
// SAFETY: `paths`/`modes` are sized to the counts reported above and passed
// with those counts, bounding the writes.
if unsafe {
QueryDisplayConfig(
QDC_ONLY_ACTIVE_PATHS,
&mut path_count,
paths.as_mut_ptr(),
&mut mode_count,
modes.as_mut_ptr(),
None,
)
} != ERROR_SUCCESS
{
return None;
}
// 3. Match the path whose source is this monitor, then read its target's
// SDR white level. `DisplayConfigGetDeviceInfo` returns a WIN32 code as
// `i32` (`0` == `ERROR_SUCCESS`).
for path in paths.iter().take(path_count as usize) {
let mut source = DISPLAYCONFIG_SOURCE_DEVICE_NAME::default();
source.header.r#type = DISPLAYCONFIG_DEVICE_INFO_GET_SOURCE_NAME;
source.header.size = size_of::<DISPLAYCONFIG_SOURCE_DEVICE_NAME>() as u32;
source.header.adapterId = path.sourceInfo.adapterId;
source.header.id = path.sourceInfo.id;
// SAFETY: `source` is live with `header.type`/`header.size` set, which
// bounds the call's access to that struct.
if unsafe { DisplayConfigGetDeviceInfo(&mut source.header) } != ERROR_SUCCESS.0 as i32 {
continue;
}
if source.viewGdiDeviceName != gdi_device_name {
continue;
}
let mut white = DISPLAYCONFIG_SDR_WHITE_LEVEL::default();
white.header.r#type = DISPLAYCONFIG_DEVICE_INFO_GET_SDR_WHITE_LEVEL;
white.header.size = size_of::<DISPLAYCONFIG_SDR_WHITE_LEVEL>() as u32;
white.header.adapterId = path.targetInfo.adapterId;
white.header.id = path.targetInfo.id;
// SAFETY: `white` is live with `header.type`/`header.size` set, which
// bounds the call's access to that struct.
if unsafe { DisplayConfigGetDeviceInfo(&mut white.header) } != ERROR_SUCCESS.0 as i32 {
return None;
}
// `SDRWhiteLevel` encodes nits as `(nits / 80) * 1000`; `0` means the OS
// reported nothing usable.
return (white.SDRWhiteLevel > 0).then(|| white.SDRWhiteLevel as f32 * 80.0 / 1000.0);
}
None
}
+16
View File
@@ -0,0 +1,16 @@
// Every module here is gated behind `dx12` except `hdr` (the
// `DXGI_OUTPUT_DESC1` -> `DisplayHdrInfo` mapping), which the
// Vulkan-on-Windows backend also uses.
#[cfg(dx12)]
pub mod conv;
#[cfg(dx12)]
pub mod exception;
#[cfg(dx12)]
pub mod factory;
pub mod hdr;
#[cfg(dx12)]
pub mod name;
#[cfg(dx12)]
pub mod result;
#[cfg(dx12)]
pub mod time;
+24
View File
@@ -0,0 +1,24 @@
use windows::Win32::Graphics::Direct3D12::ID3D12Object;
use crate::auxil::dxgi::result::HResult;
/// Helper trait for setting the name of a D3D12 object.
///
/// This is implemented on all types that can be converted to an [`ID3D12Object`].
pub trait ObjectExt {
fn set_name(&self, name: &str) -> Result<(), crate::DeviceError>;
}
impl<T> ObjectExt for T
where
// Windows impls `From` for all parent interfaces, so we can use that to convert to ID3D12Object.
//
// This includes implementations for references.
for<'a> &'a ID3D12Object: From<&'a T>,
{
fn set_name(&self, name: &str) -> Result<(), crate::DeviceError> {
let name = windows::core::HSTRING::from(name);
let object: &ID3D12Object = self.into();
unsafe { object.SetName(&name).into_device_result("SetName") }
}
}
+28
View File
@@ -0,0 +1,28 @@
use windows::Win32::{Foundation, Graphics::Dxgi};
pub(crate) trait HResult<O> {
fn into_device_result(self, description: &str) -> Result<O, crate::DeviceError>;
}
impl<T> HResult<T> for windows::core::Result<T> {
fn into_device_result(self, description: &str) -> Result<T, crate::DeviceError> {
#![allow(unreachable_code)]
self.map_err(|err| {
log::error!("{description} failed: {err}");
match err.code() {
Foundation::E_OUTOFMEMORY => crate::DeviceError::OutOfMemory,
Dxgi::DXGI_ERROR_DEVICE_RESET | Dxgi::DXGI_ERROR_DEVICE_REMOVED => {
#[cfg(feature = "device_lost_panic")]
panic!("{description} failed: Device lost ({err})");
crate::DeviceError::Lost
}
_ => {
#[cfg(feature = "internal_error_panic")]
panic!("{description} failed: {err}");
crate::DeviceError::Unexpected
}
}
})
}
}
+97
View File
@@ -0,0 +1,97 @@
#![allow(dead_code, reason = "IPresentationManager is unused currently")]
use windows::Win32::System::Performance::{QueryPerformanceCounter, QueryPerformanceFrequency};
pub enum PresentationTimer {
/// DXGI uses [`QueryPerformanceCounter()`]
Dxgi {
/// How many ticks of QPC per second
frequency: u64,
},
/// [`IPresentationManager`] uses [`QueryInterruptTimePrecise()`]
///
/// [`IPresentationManager`]: https://microsoft.github.io/windows-docs-rs/doc/windows/Win32/Graphics/CompositionSwapchain/struct.IPresentationManager.html
/// [`QueryInterruptTimePrecise()`]: https://microsoft.github.io/windows-docs-rs/doc/windows/Win32/System/WindowsProgramming/fn.QueryInterruptTimePrecise.html
#[allow(non_snake_case)]
IPresentationManager {
fnQueryInterruptTimePrecise: unsafe extern "system" fn(*mut u64),
},
}
impl core::fmt::Debug for PresentationTimer {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match *self {
Self::Dxgi { frequency } => f
.debug_struct("DXGI")
.field("frequency", &frequency)
.finish(),
Self::IPresentationManager {
fnQueryInterruptTimePrecise,
} => f
.debug_struct("IPresentationManager")
.field(
"QueryInterruptTimePrecise",
&(fnQueryInterruptTimePrecise as usize),
)
.finish(),
}
}
}
impl PresentationTimer {
/// Create a presentation timer using QueryPerformanceFrequency (what DXGI uses for presentation times)
pub fn new_dxgi() -> Self {
let mut frequency = 0;
unsafe { QueryPerformanceFrequency(&mut frequency) }.unwrap();
Self::Dxgi {
frequency: frequency
.try_into()
.expect("Frequency should not be negative"),
}
}
/// Create a presentation timer using QueryInterruptTimePrecise (what IPresentationManager uses for presentation times)
///
/// Panics if QueryInterruptTimePrecise isn't found (below Win10)
pub fn new_ipresentation_manager() -> Self {
// We need to load this explicitly, as QueryInterruptTimePrecise is only available on Windows 10+
//
// Docs say it's in kernel32.dll, but it's actually in kernelbase.dll.
// api-ms-win-core-realtime-l1-1-1.dll
let kernelbase =
libloading::os::windows::Library::open_already_loaded("kernelbase.dll").unwrap();
// No concerns about lifetimes here as kernelbase is always there.
let ptr = unsafe {
kernelbase
.get(c"QueryInterruptTimePrecise".to_bytes())
.unwrap()
};
Self::IPresentationManager {
fnQueryInterruptTimePrecise: *ptr,
}
}
/// Gets the current time in nanoseconds.
pub fn get_timestamp_ns(&self) -> u128 {
// Always do u128 math _after_ hitting the timing function.
match *self {
PresentationTimer::Dxgi { frequency } => {
let mut counter = 0;
unsafe { QueryPerformanceCounter(&mut counter) }.unwrap();
// counter * (1_000_000_000 / freq) but re-ordered to make more precise
(counter as u128 * 1_000_000_000) / frequency as u128
}
PresentationTimer::IPresentationManager {
fnQueryInterruptTimePrecise,
} => {
let mut counter = 0;
unsafe { fnQueryInterruptTimePrecise(&mut counter) };
// QueryInterruptTimePrecise uses units of 100ns for its tick.
counter as u128 * 100
}
}
}
}
+241
View File
@@ -0,0 +1,241 @@
// Mostly DX12-only, but also compiled for Vulkan-on-Windows, which reuses
// `dxgi::hdr` to query display HDR info. The DX12-only submodules stay gated
// behind `dx12` in `dxgi/mod.rs`.
#[cfg(any(dx12, all(vulkan, windows)))]
pub(super) mod dxgi;
#[cfg(all(native, feature = "renderdoc"))]
pub(super) mod renderdoc;
pub mod db {
pub mod amd {
/// cbindgen:ignore
pub const VENDOR: u32 = 0x1002;
}
pub mod apple {
/// cbindgen:ignore
pub const VENDOR: u32 = 0x106B;
}
pub mod arm {
/// cbindgen:ignore
pub const VENDOR: u32 = 0x13B5;
}
pub mod broadcom {
/// cbindgen:ignore
pub const VENDOR: u32 = 0x14E4;
}
pub mod imgtec {
/// cbindgen:ignore
pub const VENDOR: u32 = 0x1010;
}
pub mod intel {
/// cbindgen:ignore
pub const VENDOR: u32 = 0x8086;
pub const DEVICE_KABY_LAKE_MASK: u32 = 0x5900;
pub const DEVICE_SKY_LAKE_MASK: u32 = 0x1900;
}
pub mod mesa {
// Mesa does not actually have a PCI vendor id.
//
// To match Vulkan, we use the VkVendorId for Mesa in the gles backend so that lavapipe (Vulkan) and
// llvmpipe (OpenGL) have the same vendor id.
/// cbindgen:ignore
pub const VENDOR: u32 = 0x10005;
}
pub mod nvidia {
/// cbindgen:ignore
pub const VENDOR: u32 = 0x10DE;
}
pub mod qualcomm {
/// cbindgen:ignore
pub const VENDOR: u32 = 0x5143;
}
}
/// Maximum binding size for the shaders that only support `i32` indexing.
/// Interestingly, the index itself can't reach that high, because the minimum
/// element size is 4 bytes, but the compiler toolchain still computes the
/// offset at some intermediate point, internally, as i32.
pub const MAX_I32_BINDING_SIZE: u32 = (1 << 31) - 1;
pub use wgpu_naga_bridge::map_naga_stage;
impl crate::CopyExtent {
pub fn map_extent_to_copy_size(extent: &wgt::Extent3d, dim: wgt::TextureDimension) -> Self {
Self {
width: extent.width,
height: extent.height,
depth: match dim {
wgt::TextureDimension::D1 | wgt::TextureDimension::D2 => 1,
wgt::TextureDimension::D3 => extent.depth_or_array_layers,
},
}
}
pub fn min(&self, other: &Self) -> Self {
Self {
width: self.width.min(other.width),
height: self.height.min(other.height),
depth: self.depth.min(other.depth),
}
}
// Get the copy size at a specific mipmap level. This doesn't make most sense,
// since the copy extents are provided *for* a mipmap level to start with.
// But backends use `CopyExtent` more sparingly, and this piece is shared.
pub fn at_mip_level(&self, level: u32) -> Self {
Self {
width: (self.width >> level).max(1),
height: (self.height >> level).max(1),
depth: (self.depth >> level).max(1),
}
}
}
impl crate::TextureCopyBase {
pub fn max_copy_size(&self, full_size: &crate::CopyExtent) -> crate::CopyExtent {
let mip = full_size.at_mip_level(self.mip_level);
crate::CopyExtent {
width: mip.width - self.origin.x,
height: mip.height - self.origin.y,
depth: mip.depth - self.origin.z,
}
}
}
impl crate::BufferTextureCopy {
pub fn clamp_size_to_virtual(&mut self, full_size: &crate::CopyExtent) {
let max_size = self.texture_base.max_copy_size(full_size);
self.size = self.size.min(&max_size);
}
}
impl crate::TextureCopy {
pub fn clamp_size_to_virtual(
&mut self,
full_src_size: &crate::CopyExtent,
full_dst_size: &crate::CopyExtent,
) {
let max_src_size = self.src_base.max_copy_size(full_src_size);
let max_dst_size = self.dst_base.max_copy_size(full_dst_size);
self.size = self.size.min(&max_src_size).min(&max_dst_size);
}
}
/// Adjust `limits` to honor HAL-imposed maximums and comply with WebGPU's
/// adapter capability guarantees.
#[cfg_attr(not(any_backend), allow(dead_code))]
pub(crate) fn adjust_raw_limits(mut limits: wgt::Limits) -> wgt::Limits {
// Apply hal limits.
limits.max_bind_groups = limits.max_bind_groups.min(crate::MAX_BIND_GROUPS as u32);
limits.max_vertex_buffers = limits
.max_vertex_buffers
.min(crate::MAX_VERTEX_BUFFERS as u32);
// Once we allow the 2 limits above to be higher than 24 we should use
// `cap_limits_to_be_under_the_sum_limit` to cap them under
// `max_bind_groups_plus_vertex_buffers`.
const { assert!(crate::MAX_BIND_GROUPS + crate::MAX_VERTEX_BUFFERS == 24) };
limits.max_bind_groups_plus_vertex_buffers = limits.max_bind_groups_plus_vertex_buffers.min(24);
limits.max_color_attachments = limits
.max_color_attachments
.min(crate::MAX_COLOR_ATTACHMENTS as u32);
// Adjust limits according to WebGPU adapter capability guarantees.
// See <https://gpuweb.github.io/gpuweb/#adapter-capability-guarantees>.
// WebGPU requires maxBindingsPerBindGroup to be at least the sum of all
// per-stage limits multiplied with the maximum shader stages per pipeline.
//
// Since backends already report their maximum maxBindingsPerBindGroup,
// we need to lower all per-stage limits to satisfy this guarantee.
const MAX_SHADER_STAGES_PER_PIPELINE: u32 = 2;
let max_per_stage_resources =
limits.max_bindings_per_bind_group / MAX_SHADER_STAGES_PER_PIPELINE;
cap_limits_to_be_under_the_sum_limit(
[
&mut limits.max_sampled_textures_per_shader_stage,
&mut limits.max_uniform_buffers_per_shader_stage,
&mut limits.max_storage_textures_per_shader_stage,
&mut limits.max_storage_buffers_per_shader_stage,
&mut limits.max_samplers_per_shader_stage,
&mut limits.max_acceleration_structures_per_shader_stage,
],
max_per_stage_resources,
);
// Not required by the spec but dynamic buffers count
// towards non-dynamic buffer limits as well.
limits.max_dynamic_uniform_buffers_per_pipeline_layout = limits
.max_dynamic_uniform_buffers_per_pipeline_layout
.min(limits.max_uniform_buffers_per_shader_stage);
limits.max_dynamic_storage_buffers_per_pipeline_layout = limits
.max_dynamic_storage_buffers_per_pipeline_layout
.min(limits.max_storage_buffers_per_shader_stage);
limits.min_uniform_buffer_offset_alignment = limits.min_uniform_buffer_offset_alignment.max(32);
limits.min_storage_buffer_offset_alignment = limits.min_storage_buffer_offset_alignment.max(32);
limits.max_uniform_buffer_binding_size = limits
.max_uniform_buffer_binding_size
.min(limits.max_buffer_size);
limits.max_storage_buffer_binding_size = limits
.max_storage_buffer_binding_size
.min(limits.max_buffer_size);
limits.max_storage_buffer_binding_size &= !(u64::from(wgt::STORAGE_BINDING_SIZE_ALIGNMENT) - 1);
limits.max_vertex_buffer_array_stride &= !(wgt::VERTEX_ALIGNMENT as u32 - 1);
let x = limits.max_compute_workgroup_size_x;
let y = limits.max_compute_workgroup_size_y;
let z = limits.max_compute_workgroup_size_z;
let m = limits.max_compute_invocations_per_workgroup;
limits.max_compute_workgroup_size_x = x.min(m);
limits.max_compute_workgroup_size_y = y.min(m);
limits.max_compute_workgroup_size_z = z.min(m);
limits.max_compute_invocations_per_workgroup = m.min(x.saturating_mul(y).saturating_mul(z));
limits.max_immediate_size = limits.max_immediate_size.min(256);
limits
}
/// Evenly allocates space to each limit,
/// capping them only if strictly necessary.
pub fn cap_limits_to_be_under_the_sum_limit<const N: usize>(
mut limits: [&mut u32; N],
sum_limit: u32,
) {
limits.sort();
let mut rem_limit = sum_limit;
let mut divisor = limits.len() as u32;
for limit_to_adjust in limits {
let limit = rem_limit / divisor;
*limit_to_adjust = (*limit_to_adjust).min(limit);
rem_limit -= *limit_to_adjust;
divisor -= 1;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cap_limits_to_be_under_the_sum_limit() {
test([3, 3, 3], 3, [1, 1, 1]);
test([3, 2, 1], 3, [1, 1, 1]);
test([1, 2, 3], 6, [1, 2, 3]);
test([1, 2, 3], 3, [1, 1, 1]);
test([1, 8, 100], 6, [1, 2, 3]);
test([2, 80, 80], 6, [2, 2, 2]);
test([2, 80, 80], 12, [2, 5, 5]);
#[track_caller]
fn test<const N: usize>(mut input: [u32; N], limit: u32, output: [u32; N]) {
cap_limits_to_be_under_the_sum_limit(input.each_mut(), limit);
assert_eq!(input, output);
}
}
}
+141
View File
@@ -0,0 +1,141 @@
//! RenderDoc integration - <https://renderdoc.org/>
#![cfg_attr(not(any(feature = "gles", feature = "vulkan")), allow(dead_code))]
use alloc::format;
use alloc::string::String;
use core::{ffi, ptr};
/// The dynamically loaded RenderDoc API function table
#[repr(C)]
#[derive(Debug)]
pub struct RenderDocApi {
api: renderdoc_sys::RENDERDOC_API_1_4_1,
lib: libloading::Library,
}
unsafe impl Send for RenderDocApi {}
unsafe impl Sync for RenderDocApi {}
/// RenderDoc API type
#[derive(Debug)]
pub enum RenderDoc {
/// RenderDoc functionality is available
Available {
/// RenderDoc API with function pointers
api: RenderDocApi,
},
/// RenderDoc functionality is _not_ available
NotAvailable {
/// A description why renderdoc functionality is not available
reason: String,
},
}
// TODO: replace with libloading API once supported
#[cfg(unix)]
const RTLD_NOLOAD: i32 = 0x4;
impl RenderDoc {
pub unsafe fn new() -> Self {
type GetApiFn = unsafe extern "C" fn(version: u32, out: *mut *mut ffi::c_void) -> i32;
#[cfg(windows)]
let renderdoc_filename = "renderdoc.dll";
#[cfg(all(unix, not(target_os = "android")))]
let renderdoc_filename = "librenderdoc.so";
#[cfg(target_os = "android")]
let renderdoc_filename = "libVkLayer_GLES_RenderDoc.so";
#[cfg(unix)]
let renderdoc_result: Result<libloading::Library, libloading::Error> = unsafe {
libloading::os::unix::Library::open(
Some(renderdoc_filename),
libloading::os::unix::RTLD_NOW | RTLD_NOLOAD,
)
}
.map(|lib| lib.into());
#[cfg(windows)]
let renderdoc_result: Result<libloading::Library, libloading::Error> =
libloading::os::windows::Library::open_already_loaded(renderdoc_filename)
.map(|lib| lib.into());
let renderdoc_lib = match renderdoc_result {
Ok(lib) => lib,
Err(e) => {
return RenderDoc::NotAvailable {
reason: format!(
"Unable to load renderdoc library '{renderdoc_filename}': {e:?}"
),
}
}
};
let get_api: libloading::Symbol<GetApiFn> =
match unsafe { renderdoc_lib.get(c"RENDERDOC_GetAPI".to_bytes()) } {
Ok(api) => api,
Err(e) => {
return RenderDoc::NotAvailable {
reason: format!(
"Unable to get RENDERDOC_GetAPI from renderdoc library '{renderdoc_filename}': {e:?}"
),
}
}
};
let mut obj = ptr::null_mut();
match unsafe { get_api(10401, &mut obj) } {
1 => RenderDoc::Available {
api: RenderDocApi {
api: unsafe { *obj.cast::<renderdoc_sys::RENDERDOC_API_1_4_1>() },
lib: renderdoc_lib,
},
},
return_value => RenderDoc::NotAvailable {
reason: format!(
"Unable to get API from renderdoc library '{renderdoc_filename}': {return_value}"
),
},
}
}
}
impl Default for RenderDoc {
fn default() -> Self {
if !cfg!(debug_assertions) {
return RenderDoc::NotAvailable {
reason: "RenderDoc support is only enabled with 'debug_assertions'".into(),
};
}
unsafe { Self::new() }
}
}
/// An implementation specific handle
pub type Handle = *mut ffi::c_void;
impl RenderDoc {
/// Start a RenderDoc frame capture
pub unsafe fn start_frame_capture(&self, device_handle: Handle, window_handle: Handle) -> bool {
match *self {
Self::Available { api: ref entry } => {
unsafe { entry.api.StartFrameCapture.unwrap()(device_handle, window_handle) };
true
}
Self::NotAvailable { ref reason } => {
log::warn!("Could not start RenderDoc frame capture: {reason}");
false
}
}
}
/// End a RenderDoc frame capture
pub unsafe fn end_frame_capture(&self, device_handle: Handle, window_handle: Handle) {
match *self {
Self::Available { api: ref entry } => {
unsafe { entry.api.EndFrameCapture.unwrap()(device_handle, window_handle) };
}
Self::NotAvailable { ref reason } => {
log::warn!("Could not end RenderDoc frame capture: {reason}")
}
};
}
}
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+467
View File
@@ -0,0 +1,467 @@
use windows::Win32::Graphics::{Direct3D, Direct3D12, Dxgi};
pub fn map_buffer_usage_to_resource_flags(
usage: wgt::BufferUses,
) -> Direct3D12::D3D12_RESOURCE_FLAGS {
let mut flags = Direct3D12::D3D12_RESOURCE_FLAG_NONE;
if usage.contains(wgt::BufferUses::STORAGE_READ_WRITE)
|| usage.contains(wgt::BufferUses::ACCELERATION_STRUCTURE_QUERY)
{
flags |= Direct3D12::D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
}
flags
}
pub fn map_buffer_descriptor(
desc: &crate::BufferDescriptor<'_>,
) -> Direct3D12::D3D12_RESOURCE_DESC {
Direct3D12::D3D12_RESOURCE_DESC {
Dimension: Direct3D12::D3D12_RESOURCE_DIMENSION_BUFFER,
Alignment: 0,
Width: desc.size,
Height: 1,
DepthOrArraySize: 1,
MipLevels: 1,
Format: Dxgi::Common::DXGI_FORMAT_UNKNOWN,
SampleDesc: Dxgi::Common::DXGI_SAMPLE_DESC {
Count: 1,
Quality: 0,
},
Layout: Direct3D12::D3D12_TEXTURE_LAYOUT_ROW_MAJOR,
Flags: map_buffer_usage_to_resource_flags(desc.usage),
}
}
pub fn map_texture_dimension(dim: wgt::TextureDimension) -> Direct3D12::D3D12_RESOURCE_DIMENSION {
match dim {
wgt::TextureDimension::D1 => Direct3D12::D3D12_RESOURCE_DIMENSION_TEXTURE1D,
wgt::TextureDimension::D2 => Direct3D12::D3D12_RESOURCE_DIMENSION_TEXTURE2D,
wgt::TextureDimension::D3 => Direct3D12::D3D12_RESOURCE_DIMENSION_TEXTURE3D,
}
}
pub fn map_texture_usage_to_resource_flags(
usage: wgt::TextureUses,
) -> Direct3D12::D3D12_RESOURCE_FLAGS {
let mut flags = Direct3D12::D3D12_RESOURCE_FLAG_NONE;
if usage.contains(wgt::TextureUses::COLOR_TARGET) {
flags |= Direct3D12::D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
}
if usage
.intersects(wgt::TextureUses::DEPTH_STENCIL_READ | wgt::TextureUses::DEPTH_STENCIL_WRITE)
{
flags |= Direct3D12::D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL;
if !usage.contains(wgt::TextureUses::RESOURCE) {
flags |= Direct3D12::D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE;
}
}
if usage.intersects(
wgt::TextureUses::STORAGE_READ_ONLY
| wgt::TextureUses::STORAGE_WRITE_ONLY
| wgt::TextureUses::STORAGE_READ_WRITE,
) {
flags |= Direct3D12::D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
}
flags
}
pub fn map_address_mode(mode: wgt::AddressMode) -> Direct3D12::D3D12_TEXTURE_ADDRESS_MODE {
use wgt::AddressMode as Am;
match mode {
Am::Repeat => Direct3D12::D3D12_TEXTURE_ADDRESS_MODE_WRAP,
Am::MirrorRepeat => Direct3D12::D3D12_TEXTURE_ADDRESS_MODE_MIRROR,
Am::ClampToEdge => Direct3D12::D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
Am::ClampToBorder => Direct3D12::D3D12_TEXTURE_ADDRESS_MODE_BORDER,
//Am::MirrorClamp => Direct3D12::D3D12_TEXTURE_ADDRESS_MODE_MIRROR_ONCE,
}
}
pub fn map_filter_mode(mode: wgt::FilterMode) -> Direct3D12::D3D12_FILTER_TYPE {
match mode {
wgt::FilterMode::Nearest => Direct3D12::D3D12_FILTER_TYPE_POINT,
wgt::FilterMode::Linear => Direct3D12::D3D12_FILTER_TYPE_LINEAR,
}
}
pub fn map_mipmap_filter_mode(mode: wgt::MipmapFilterMode) -> Direct3D12::D3D12_FILTER_TYPE {
match mode {
wgt::MipmapFilterMode::Nearest => Direct3D12::D3D12_FILTER_TYPE_POINT,
wgt::MipmapFilterMode::Linear => Direct3D12::D3D12_FILTER_TYPE_LINEAR,
}
}
pub fn map_comparison(func: wgt::CompareFunction) -> Direct3D12::D3D12_COMPARISON_FUNC {
use wgt::CompareFunction as Cf;
match func {
Cf::Never => Direct3D12::D3D12_COMPARISON_FUNC_NEVER,
Cf::Less => Direct3D12::D3D12_COMPARISON_FUNC_LESS,
Cf::LessEqual => Direct3D12::D3D12_COMPARISON_FUNC_LESS_EQUAL,
Cf::Equal => Direct3D12::D3D12_COMPARISON_FUNC_EQUAL,
Cf::GreaterEqual => Direct3D12::D3D12_COMPARISON_FUNC_GREATER_EQUAL,
Cf::Greater => Direct3D12::D3D12_COMPARISON_FUNC_GREATER,
Cf::NotEqual => Direct3D12::D3D12_COMPARISON_FUNC_NOT_EQUAL,
Cf::Always => Direct3D12::D3D12_COMPARISON_FUNC_ALWAYS,
}
}
pub fn map_border_color(border_color: Option<wgt::SamplerBorderColor>) -> [f32; 4] {
use wgt::SamplerBorderColor as Sbc;
match border_color {
Some(Sbc::TransparentBlack) | Some(Sbc::Zero) | None => [0.0; 4],
Some(Sbc::OpaqueBlack) => [0.0, 0.0, 0.0, 1.0],
Some(Sbc::OpaqueWhite) => [1.0; 4],
}
}
pub fn map_visibility(visibility: wgt::ShaderStages) -> Direct3D12::D3D12_SHADER_VISIBILITY {
match visibility {
wgt::ShaderStages::VERTEX => Direct3D12::D3D12_SHADER_VISIBILITY_VERTEX,
wgt::ShaderStages::FRAGMENT => Direct3D12::D3D12_SHADER_VISIBILITY_PIXEL,
_ => Direct3D12::D3D12_SHADER_VISIBILITY_ALL,
}
}
pub fn map_binding_type(ty: &wgt::BindingType) -> Direct3D12::D3D12_DESCRIPTOR_RANGE_TYPE {
use wgt::BindingType as Bt;
match *ty {
Bt::Sampler { .. } => Direct3D12::D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER,
Bt::Buffer {
ty: wgt::BufferBindingType::Uniform,
..
} => Direct3D12::D3D12_DESCRIPTOR_RANGE_TYPE_CBV,
Bt::Buffer {
ty: wgt::BufferBindingType::Storage { read_only: true },
..
}
| Bt::Texture { .. } => Direct3D12::D3D12_DESCRIPTOR_RANGE_TYPE_SRV,
Bt::Buffer {
ty: wgt::BufferBindingType::Storage { read_only: false },
..
}
| Bt::StorageTexture { .. } => Direct3D12::D3D12_DESCRIPTOR_RANGE_TYPE_UAV,
Bt::AccelerationStructure { .. } => Direct3D12::D3D12_DESCRIPTOR_RANGE_TYPE_SRV,
// External textures require multiple bindings and therefore cannot
// be mapped to a single descriptor range type. They must be handled
// separately by the caller.
Bt::ExternalTexture => unreachable!("External textures must be handled separately"),
}
}
pub fn map_buffer_usage_to_state(usage: wgt::BufferUses) -> Direct3D12::D3D12_RESOURCE_STATES {
use wgt::BufferUses as Bu;
let mut state = Direct3D12::D3D12_RESOURCE_STATE_COMMON;
if usage.intersects(Bu::COPY_SRC) {
state |= Direct3D12::D3D12_RESOURCE_STATE_COPY_SOURCE;
}
if usage.intersects(Bu::COPY_DST) {
state |= Direct3D12::D3D12_RESOURCE_STATE_COPY_DEST;
}
if usage.intersects(Bu::INDEX) {
state |= Direct3D12::D3D12_RESOURCE_STATE_INDEX_BUFFER;
}
if usage.intersects(Bu::VERTEX | Bu::UNIFORM) {
state |= Direct3D12::D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER;
}
if usage.intersects(Bu::STORAGE_READ_WRITE) {
state |= Direct3D12::D3D12_RESOURCE_STATE_UNORDERED_ACCESS;
} else if usage.intersects(Bu::STORAGE_READ_ONLY) {
state |= Direct3D12::D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE
| Direct3D12::D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE;
}
if usage.intersects(Bu::INDIRECT) {
state |= Direct3D12::D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT;
}
if usage.intersects(Bu::ACCELERATION_STRUCTURE_QUERY) {
state |= Direct3D12::D3D12_RESOURCE_STATE_UNORDERED_ACCESS;
}
state
}
pub fn map_texture_usage_to_state(usage: wgt::TextureUses) -> Direct3D12::D3D12_RESOURCE_STATES {
use wgt::TextureUses as Tu;
let mut state = Direct3D12::D3D12_RESOURCE_STATE_COMMON;
//Note: `RESOLVE_SOURCE` and `RESOLVE_DEST` are not used here
//Note: `PRESENT` is the same as `COMMON`
if usage == wgt::TextureUses::UNINITIALIZED {
return state;
}
if usage.intersects(Tu::COPY_SRC) {
state |= Direct3D12::D3D12_RESOURCE_STATE_COPY_SOURCE;
}
if usage.intersects(Tu::COPY_DST) {
state |= Direct3D12::D3D12_RESOURCE_STATE_COPY_DEST;
}
if usage.intersects(Tu::RESOURCE) {
state |= Direct3D12::D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE
| Direct3D12::D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE;
}
if usage.intersects(Tu::COLOR_TARGET) {
state |= Direct3D12::D3D12_RESOURCE_STATE_RENDER_TARGET;
}
if usage.intersects(Tu::DEPTH_STENCIL_READ) {
state |= Direct3D12::D3D12_RESOURCE_STATE_DEPTH_READ;
}
if usage.intersects(Tu::DEPTH_STENCIL_WRITE) {
state |= Direct3D12::D3D12_RESOURCE_STATE_DEPTH_WRITE;
}
if usage.intersects(Tu::STORAGE_READ_ONLY | Tu::STORAGE_WRITE_ONLY | Tu::STORAGE_READ_WRITE) {
state |= Direct3D12::D3D12_RESOURCE_STATE_UNORDERED_ACCESS;
}
state
}
pub fn map_topology(
topology: wgt::PrimitiveTopology,
) -> (
Direct3D12::D3D12_PRIMITIVE_TOPOLOGY_TYPE,
Direct3D::D3D_PRIMITIVE_TOPOLOGY,
) {
match topology {
wgt::PrimitiveTopology::PointList => (
Direct3D12::D3D12_PRIMITIVE_TOPOLOGY_TYPE_POINT,
Direct3D::D3D_PRIMITIVE_TOPOLOGY_POINTLIST,
),
wgt::PrimitiveTopology::LineList => (
Direct3D12::D3D12_PRIMITIVE_TOPOLOGY_TYPE_LINE,
Direct3D::D3D_PRIMITIVE_TOPOLOGY_LINELIST,
),
wgt::PrimitiveTopology::LineStrip => (
Direct3D12::D3D12_PRIMITIVE_TOPOLOGY_TYPE_LINE,
Direct3D::D3D_PRIMITIVE_TOPOLOGY_LINESTRIP,
),
wgt::PrimitiveTopology::TriangleList => (
Direct3D12::D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE,
Direct3D::D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST,
),
wgt::PrimitiveTopology::TriangleStrip => (
Direct3D12::D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE,
Direct3D::D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP,
),
}
}
pub fn map_polygon_mode(mode: wgt::PolygonMode) -> Direct3D12::D3D12_FILL_MODE {
match mode {
wgt::PolygonMode::Fill => Direct3D12::D3D12_FILL_MODE_SOLID,
wgt::PolygonMode::Line => Direct3D12::D3D12_FILL_MODE_WIREFRAME,
wgt::PolygonMode::Point => panic!(
"{:?} is not enabled for this backend",
wgt::Features::POLYGON_MODE_POINT
),
}
}
/// D3D12 doesn't support passing factors ending in `_COLOR` for alpha blending
/// (see <https://learn.microsoft.com/en-us/windows/win32/api/d3d12/ns-d3d12-d3d12_render_target_blend_desc>).
/// Therefore this function takes an additional `is_alpha` argument
/// which if set will return an equivalent `_ALPHA` factor.
fn map_blend_factor(factor: wgt::BlendFactor, is_alpha: bool) -> Direct3D12::D3D12_BLEND {
use wgt::BlendFactor as Bf;
match factor {
Bf::Zero => Direct3D12::D3D12_BLEND_ZERO,
Bf::One => Direct3D12::D3D12_BLEND_ONE,
Bf::Src if is_alpha => Direct3D12::D3D12_BLEND_SRC_ALPHA,
Bf::Src => Direct3D12::D3D12_BLEND_SRC_COLOR,
Bf::OneMinusSrc if is_alpha => Direct3D12::D3D12_BLEND_INV_SRC_ALPHA,
Bf::OneMinusSrc => Direct3D12::D3D12_BLEND_INV_SRC_COLOR,
Bf::Dst if is_alpha => Direct3D12::D3D12_BLEND_DEST_ALPHA,
Bf::Dst => Direct3D12::D3D12_BLEND_DEST_COLOR,
Bf::OneMinusDst if is_alpha => Direct3D12::D3D12_BLEND_INV_DEST_ALPHA,
Bf::OneMinusDst => Direct3D12::D3D12_BLEND_INV_DEST_COLOR,
Bf::SrcAlpha => Direct3D12::D3D12_BLEND_SRC_ALPHA,
Bf::OneMinusSrcAlpha => Direct3D12::D3D12_BLEND_INV_SRC_ALPHA,
Bf::DstAlpha => Direct3D12::D3D12_BLEND_DEST_ALPHA,
Bf::OneMinusDstAlpha => Direct3D12::D3D12_BLEND_INV_DEST_ALPHA,
Bf::Constant => Direct3D12::D3D12_BLEND_BLEND_FACTOR,
Bf::OneMinusConstant => Direct3D12::D3D12_BLEND_INV_BLEND_FACTOR,
Bf::SrcAlphaSaturated => Direct3D12::D3D12_BLEND_SRC_ALPHA_SAT,
Bf::Src1 if is_alpha => Direct3D12::D3D12_BLEND_SRC1_ALPHA,
Bf::Src1 => Direct3D12::D3D12_BLEND_SRC1_COLOR,
Bf::OneMinusSrc1 if is_alpha => Direct3D12::D3D12_BLEND_INV_SRC1_ALPHA,
Bf::OneMinusSrc1 => Direct3D12::D3D12_BLEND_INV_SRC1_COLOR,
Bf::Src1Alpha => Direct3D12::D3D12_BLEND_SRC1_ALPHA,
Bf::OneMinusSrc1Alpha => Direct3D12::D3D12_BLEND_INV_SRC1_ALPHA,
}
}
fn map_blend_component(
component: &wgt::BlendComponent,
is_alpha: bool,
) -> (
Direct3D12::D3D12_BLEND_OP,
Direct3D12::D3D12_BLEND,
Direct3D12::D3D12_BLEND,
) {
let raw_op = match component.operation {
wgt::BlendOperation::Add => Direct3D12::D3D12_BLEND_OP_ADD,
wgt::BlendOperation::Subtract => Direct3D12::D3D12_BLEND_OP_SUBTRACT,
wgt::BlendOperation::ReverseSubtract => Direct3D12::D3D12_BLEND_OP_REV_SUBTRACT,
wgt::BlendOperation::Min => Direct3D12::D3D12_BLEND_OP_MIN,
wgt::BlendOperation::Max => Direct3D12::D3D12_BLEND_OP_MAX,
};
let raw_src = map_blend_factor(component.src_factor, is_alpha);
let raw_dst = map_blend_factor(component.dst_factor, is_alpha);
(raw_op, raw_src, raw_dst)
}
pub fn map_render_targets(
color_targets: &[Option<wgt::ColorTargetState>],
) -> [Direct3D12::D3D12_RENDER_TARGET_BLEND_DESC;
Direct3D12::D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT as usize] {
let dummy_target = Direct3D12::D3D12_RENDER_TARGET_BLEND_DESC {
BlendEnable: false.into(),
LogicOpEnable: false.into(),
SrcBlend: Direct3D12::D3D12_BLEND_ZERO,
DestBlend: Direct3D12::D3D12_BLEND_ZERO,
BlendOp: Direct3D12::D3D12_BLEND_OP_ADD,
SrcBlendAlpha: Direct3D12::D3D12_BLEND_ZERO,
DestBlendAlpha: Direct3D12::D3D12_BLEND_ZERO,
BlendOpAlpha: Direct3D12::D3D12_BLEND_OP_ADD,
LogicOp: Direct3D12::D3D12_LOGIC_OP_CLEAR,
RenderTargetWriteMask: 0,
};
let mut raw_targets =
[dummy_target; Direct3D12::D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT as usize];
for (raw, ct) in raw_targets.iter_mut().zip(color_targets.iter()) {
if let Some(ct) = ct.as_ref() {
raw.RenderTargetWriteMask = ct.write_mask.bits() as u8;
if let Some(ref blend) = ct.blend {
let (color_op, color_src, color_dst) = map_blend_component(&blend.color, false);
let (alpha_op, alpha_src, alpha_dst) = map_blend_component(&blend.alpha, true);
raw.BlendEnable = true.into();
raw.BlendOp = color_op;
raw.SrcBlend = color_src;
raw.DestBlend = color_dst;
raw.BlendOpAlpha = alpha_op;
raw.SrcBlendAlpha = alpha_src;
raw.DestBlendAlpha = alpha_dst;
}
}
}
raw_targets
}
fn map_stencil_op(op: wgt::StencilOperation) -> Direct3D12::D3D12_STENCIL_OP {
use wgt::StencilOperation as So;
match op {
So::Keep => Direct3D12::D3D12_STENCIL_OP_KEEP,
So::Zero => Direct3D12::D3D12_STENCIL_OP_ZERO,
So::Replace => Direct3D12::D3D12_STENCIL_OP_REPLACE,
So::IncrementClamp => Direct3D12::D3D12_STENCIL_OP_INCR_SAT,
So::IncrementWrap => Direct3D12::D3D12_STENCIL_OP_INCR,
So::DecrementClamp => Direct3D12::D3D12_STENCIL_OP_DECR_SAT,
So::DecrementWrap => Direct3D12::D3D12_STENCIL_OP_DECR,
So::Invert => Direct3D12::D3D12_STENCIL_OP_INVERT,
}
}
fn map_stencil_face(face: &wgt::StencilFaceState) -> Direct3D12::D3D12_DEPTH_STENCILOP_DESC {
Direct3D12::D3D12_DEPTH_STENCILOP_DESC {
StencilFailOp: map_stencil_op(face.fail_op),
StencilDepthFailOp: map_stencil_op(face.depth_fail_op),
StencilPassOp: map_stencil_op(face.pass_op),
StencilFunc: map_comparison(face.compare),
}
}
pub fn map_depth_stencil(ds: &wgt::DepthStencilState) -> Direct3D12::D3D12_DEPTH_STENCIL_DESC {
Direct3D12::D3D12_DEPTH_STENCIL_DESC {
DepthEnable: ds.is_depth_enabled().into(),
DepthWriteMask: if ds.depth_write_enabled.unwrap_or_default() {
Direct3D12::D3D12_DEPTH_WRITE_MASK_ALL
} else {
Direct3D12::D3D12_DEPTH_WRITE_MASK_ZERO
},
DepthFunc: map_comparison(ds.depth_compare.unwrap_or_default()),
StencilEnable: ds.stencil.is_enabled().into(),
StencilReadMask: ds.stencil.read_mask as u8,
StencilWriteMask: ds.stencil.write_mask as u8,
FrontFace: map_stencil_face(&ds.stencil.front),
BackFace: map_stencil_face(&ds.stencil.back),
}
}
pub(crate) fn map_acceleration_structure_build_flags(
flags: wgt::AccelerationStructureFlags,
mode: Option<crate::AccelerationStructureBuildMode>,
) -> Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAGS {
let mut d3d_flags = Default::default();
if flags.contains(wgt::AccelerationStructureFlags::ALLOW_COMPACTION) {
d3d_flags |=
Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_ALLOW_COMPACTION;
}
if flags.contains(wgt::AccelerationStructureFlags::ALLOW_UPDATE) {
d3d_flags |= Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_ALLOW_UPDATE;
}
if flags.contains(wgt::AccelerationStructureFlags::LOW_MEMORY) {
d3d_flags |= Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_MINIMIZE_MEMORY;
}
if flags.contains(wgt::AccelerationStructureFlags::PREFER_FAST_BUILD) {
d3d_flags |=
Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PREFER_FAST_BUILD;
}
if flags.contains(wgt::AccelerationStructureFlags::PREFER_FAST_TRACE) {
d3d_flags |=
Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PREFER_FAST_TRACE;
}
if let Some(crate::AccelerationStructureBuildMode::Update) = mode {
d3d_flags |= Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PERFORM_UPDATE
}
d3d_flags
}
pub(crate) fn map_acceleration_structure_geometry_flags(
flags: wgt::AccelerationStructureGeometryFlags,
) -> Direct3D12::D3D12_RAYTRACING_GEOMETRY_FLAGS {
let mut d3d_flags = Default::default();
if flags.contains(wgt::AccelerationStructureGeometryFlags::OPAQUE) {
d3d_flags |= Direct3D12::D3D12_RAYTRACING_GEOMETRY_FLAG_OPAQUE;
}
if flags.contains(wgt::AccelerationStructureGeometryFlags::NO_DUPLICATE_ANY_HIT_INVOCATION) {
d3d_flags |= Direct3D12::D3D12_RAYTRACING_GEOMETRY_FLAG_NO_DUPLICATE_ANYHIT_INVOCATION;
}
d3d_flags
}
pub(crate) fn map_acceleration_structure_copy_mode(
mode: wgt::AccelerationStructureCopy,
) -> Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE {
match mode {
wgt::AccelerationStructureCopy::Clone => {
Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE_CLONE
}
wgt::AccelerationStructureCopy::Compact => {
Direct3D12::D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE_COMPACT
}
}
}
pub(crate) const fn make_shader_component_mapping(
src0: Direct3D12::D3D12_SHADER_COMPONENT_MAPPING,
src1: Direct3D12::D3D12_SHADER_COMPONENT_MAPPING,
src2: Direct3D12::D3D12_SHADER_COMPONENT_MAPPING,
src3: Direct3D12::D3D12_SHADER_COMPONENT_MAPPING,
) -> Direct3D12::D3D12_SHADER_COMPONENT_MAPPING {
const M: i32 = Direct3D12::D3D12_SHADER_COMPONENT_MAPPING_MASK as i32;
const S: i32 = Direct3D12::D3D12_SHADER_COMPONENT_MAPPING_SHIFT as i32;
Direct3D12::D3D12_SHADER_COMPONENT_MAPPING(
(src0.0 & M)
| (src1.0 & M) << S
| (src2.0 & M) << (S * 2)
| (src3.0 & M) << (S * 3)
| Direct3D12::D3D12_SHADER_COMPONENT_MAPPING_ALWAYS_SET_BIT_AVOIDING_ZEROMEM_MISTAKES
as i32,
)
}
+120
View File
@@ -0,0 +1,120 @@
use alloc::sync::Arc;
use core::{ffi, ptr};
use once_cell::sync::Lazy;
use windows::{
core::Interface as _,
Win32::{Foundation::HWND, Graphics::DirectComposition},
};
use super::DynLib;
// Lazy-loaded DirectComposition library
#[derive(Debug)]
pub(crate) struct DCompLib {
lib: Lazy<Result<DynLib, crate::SurfaceError>>,
}
impl DCompLib {
pub(crate) fn new() -> Self {
Self {
lib: Lazy::new(|| unsafe {
DynLib::new("dcomp.dll").map_err(|err| {
log::error!("Error loading dcomp.dll: {err}");
crate::SurfaceError::Other("Error loading dcomp.dll")
})
}),
}
}
fn get_lib(&self) -> Result<&DynLib, crate::SurfaceError> {
match self.lib.as_ref() {
Ok(lib) => Ok(lib),
Err(err) => Err(err.clone()),
}
}
pub(crate) fn create_device(
&self,
) -> Result<DirectComposition::IDCompositionDevice, crate::SurfaceError> {
let lib = self.get_lib()?;
// Calls windows::Win32::Graphics::DirectComposition::DCompositionCreateDevice2 on dcomp.dll
type Fun = extern "system" fn(
pdxdevice: *mut ffi::c_void,
riid: *const windows_core::GUID,
ppdcompdevice: *mut *mut ffi::c_void,
) -> windows_core::HRESULT;
let func: libloading::Symbol<Fun> =
unsafe { lib.get(c"DCompositionCreateDevice2".to_bytes()) }?;
let mut res: Option<DirectComposition::IDCompositionDevice> = None;
(func)(
ptr::null_mut(),
&DirectComposition::IDCompositionDevice::IID,
<*mut _>::cast(&mut res),
)
.map(|| res.unwrap())
.map_err(|err| {
log::error!("DirectComposition::DCompositionCreateDevice2 failed: {err}");
crate::SurfaceError::Other("DirectComposition::DCompositionCreateDevice2")
})
}
}
#[derive(Default)]
pub struct DCompState {
inner: Option<InnerState>,
}
impl DCompState {
/// This will create a DirectComposition device and a target for the window handle if not already initialized.
/// If the device is already initialized, it will return the existing state.
pub unsafe fn get_or_init(
&mut self,
lib: &Arc<DCompLib>,
hwnd: &HWND,
) -> Result<&mut InnerState, crate::SurfaceError> {
if self.inner.is_none() {
self.inner = Some(unsafe { InnerState::init(lib, hwnd) }?);
}
Ok(self.inner.as_mut().unwrap())
}
}
pub struct InnerState {
pub visual: DirectComposition::IDCompositionVisual,
pub device: DirectComposition::IDCompositionDevice,
// Must be kept alive but is otherwise unused after initialization.
pub _target: DirectComposition::IDCompositionTarget,
}
impl InnerState {
/// Creates a DirectComposition device and a target for the given window handle.
pub unsafe fn init(lib: &Arc<DCompLib>, hwnd: &HWND) -> Result<Self, crate::SurfaceError> {
profiling::scope!("DCompState::init");
let dcomp_device = lib.create_device()?;
let target = unsafe { dcomp_device.CreateTargetForHwnd(*hwnd, false) }.map_err(|err| {
log::error!("IDCompositionDevice::CreateTargetForHwnd failed: {err}");
crate::SurfaceError::Other("IDCompositionDevice::CreateTargetForHwnd")
})?;
let visual = unsafe { dcomp_device.CreateVisual() }.map_err(|err| {
log::error!("IDCompositionDevice::CreateVisual failed: {err}");
crate::SurfaceError::Other("IDCompositionDevice::CreateVisual")
})?;
unsafe { target.SetRoot(&visual) }.map_err(|err| {
log::error!("IDCompositionTarget::SetRoot failed: {err}");
crate::SurfaceError::Other("IDCompositionTarget::SetRoot")
})?;
Ok(InnerState {
visual,
device: dcomp_device,
_target: target,
})
}
}
+329
View File
@@ -0,0 +1,329 @@
use alloc::vec::Vec;
use core::fmt;
use bit_set::BitSet;
use parking_lot::Mutex;
use range_alloc::RangeAllocator;
use windows::Win32::Graphics::Direct3D12;
use crate::auxil::dxgi::result::HResult as _;
const HEAP_SIZE_FIXED: usize = 64;
#[derive(Copy, Clone)]
pub(super) struct DualHandle {
cpu: Direct3D12::D3D12_CPU_DESCRIPTOR_HANDLE,
pub gpu: Direct3D12::D3D12_GPU_DESCRIPTOR_HANDLE,
/// How large the block allocated to this handle is.
count: u64,
}
impl fmt::Debug for DualHandle {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("DualHandle")
.field("cpu", &self.cpu.ptr)
.field("gpu", &self.gpu.ptr)
.field("count", &self.count)
.finish()
}
}
type DescriptorIndex = u64;
pub(super) struct GeneralHeap {
pub raw: Direct3D12::ID3D12DescriptorHeap,
ty: Direct3D12::D3D12_DESCRIPTOR_HEAP_TYPE,
handle_size: u64,
total_handles: u64,
start: DualHandle,
ranges: Mutex<RangeAllocator<DescriptorIndex>>,
}
impl GeneralHeap {
pub(super) fn new(
device: &Direct3D12::ID3D12Device,
ty: Direct3D12::D3D12_DESCRIPTOR_HEAP_TYPE,
total_handles: u64,
) -> Result<Self, crate::DeviceError> {
let raw = {
profiling::scope!("ID3D12Device::CreateDescriptorHeap");
let desc = Direct3D12::D3D12_DESCRIPTOR_HEAP_DESC {
Type: ty,
NumDescriptors: total_handles as u32,
Flags: Direct3D12::D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE,
NodeMask: 0,
};
unsafe { device.CreateDescriptorHeap::<Direct3D12::ID3D12DescriptorHeap>(&desc) }
.into_device_result("Descriptor heap creation")?
};
let start = DualHandle {
cpu: unsafe { raw.GetCPUDescriptorHandleForHeapStart() },
gpu: unsafe { raw.GetGPUDescriptorHandleForHeapStart() },
count: 0,
};
Ok(Self {
raw,
ty,
handle_size: unsafe { device.GetDescriptorHandleIncrementSize(ty) } as u64,
total_handles,
start,
ranges: Mutex::new(RangeAllocator::new(0..total_handles)),
})
}
pub(super) fn at(&self, index: DescriptorIndex, count: u64) -> DualHandle {
assert!(index < self.total_handles);
DualHandle {
cpu: self.cpu_descriptor_at(index),
gpu: self.gpu_descriptor_at(index),
count,
}
}
fn cpu_descriptor_at(&self, index: u64) -> Direct3D12::D3D12_CPU_DESCRIPTOR_HANDLE {
Direct3D12::D3D12_CPU_DESCRIPTOR_HANDLE {
ptr: self.start.cpu.ptr + (self.handle_size * index) as usize,
}
}
fn gpu_descriptor_at(&self, index: u64) -> Direct3D12::D3D12_GPU_DESCRIPTOR_HANDLE {
Direct3D12::D3D12_GPU_DESCRIPTOR_HANDLE {
ptr: self.start.gpu.ptr + self.handle_size * index,
}
}
pub(super) fn allocate_slice(&self, count: u64) -> Result<DescriptorIndex, crate::DeviceError> {
let range = self.ranges.lock().allocate_range(count).map_err(|err| {
log::error!("Unable to allocate descriptors: {err:?}");
crate::DeviceError::OutOfMemory
})?;
Ok(range.start)
}
/// Free handles previously given out by this `DescriptorHeapSlice`.
/// Do not use this with handles not given out by this `DescriptorHeapSlice`.
pub(crate) fn free_slice(&self, handle: DualHandle) {
let start = (handle.gpu.ptr - self.start.gpu.ptr) / self.handle_size;
self.ranges.lock().free_range(start..start + handle.count);
}
}
/// Fixed-size free-list allocator for CPU descriptors.
struct FixedSizeHeap {
_raw: Direct3D12::ID3D12DescriptorHeap,
/// Bit flag representation of available handles in the heap.
///
/// 0 - Occupied
/// 1 - free
availability: u64,
handle_size: usize,
start: Direct3D12::D3D12_CPU_DESCRIPTOR_HANDLE,
}
impl FixedSizeHeap {
fn new(
device: &Direct3D12::ID3D12Device,
ty: Direct3D12::D3D12_DESCRIPTOR_HEAP_TYPE,
) -> Result<Self, crate::DeviceError> {
let desc = Direct3D12::D3D12_DESCRIPTOR_HEAP_DESC {
Type: ty,
NumDescriptors: HEAP_SIZE_FIXED as u32,
Flags: Direct3D12::D3D12_DESCRIPTOR_HEAP_FLAG_NONE,
NodeMask: 0,
};
let heap =
unsafe { device.CreateDescriptorHeap::<Direct3D12::ID3D12DescriptorHeap>(&desc) }
.into_device_result("Descriptor heap creation")?;
Ok(Self {
handle_size: unsafe { device.GetDescriptorHandleIncrementSize(ty) } as usize,
availability: !0, // all free!
start: unsafe { heap.GetCPUDescriptorHandleForHeapStart() },
_raw: heap,
})
}
fn alloc_handle(
&mut self,
) -> Result<Direct3D12::D3D12_CPU_DESCRIPTOR_HANDLE, crate::DeviceError> {
// Find first free slot.
let slot = self.availability.trailing_zeros() as usize;
if slot >= HEAP_SIZE_FIXED {
log::error!("Failed to allocate a handle form a fixed size heap");
return Err(crate::DeviceError::OutOfMemory);
}
// Set the slot as occupied.
self.availability ^= 1 << slot;
Ok(Direct3D12::D3D12_CPU_DESCRIPTOR_HANDLE {
ptr: self.start.ptr + self.handle_size * slot,
})
}
fn free_handle(&mut self, handle: Direct3D12::D3D12_CPU_DESCRIPTOR_HANDLE) {
let slot = (handle.ptr - self.start.ptr) / self.handle_size;
assert!(slot < HEAP_SIZE_FIXED);
assert_eq!(self.availability & (1 << slot), 0);
self.availability ^= 1 << slot;
}
fn is_full(&self) -> bool {
self.availability == 0
}
}
#[derive(Clone, Copy)]
pub(super) struct Handle {
pub raw: Direct3D12::D3D12_CPU_DESCRIPTOR_HANDLE,
heap_index: usize,
}
impl fmt::Debug for Handle {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("Handle")
.field("ptr", &self.raw.ptr)
.field("heap_index", &self.heap_index)
.finish()
}
}
pub(super) struct CpuPool {
device: Direct3D12::ID3D12Device,
ty: Direct3D12::D3D12_DESCRIPTOR_HEAP_TYPE,
heaps: Vec<FixedSizeHeap>,
available_heap_indices: BitSet,
}
impl CpuPool {
pub(super) fn new(
device: Direct3D12::ID3D12Device,
ty: Direct3D12::D3D12_DESCRIPTOR_HEAP_TYPE,
) -> Self {
Self {
device,
ty,
heaps: Vec::new(),
available_heap_indices: BitSet::new(),
}
}
pub(super) fn alloc_handle(&mut self) -> Result<Handle, crate::DeviceError> {
let heap_index = self
.available_heap_indices
.iter()
.next()
.unwrap_or(self.heaps.len());
// Allocate a new heap
if heap_index == self.heaps.len() {
self.heaps.push(FixedSizeHeap::new(&self.device, self.ty)?);
self.available_heap_indices.insert(heap_index);
}
let heap = &mut self.heaps[heap_index];
let handle = Handle {
raw: heap.alloc_handle()?,
heap_index,
};
if heap.is_full() {
self.available_heap_indices.remove(heap_index);
}
Ok(handle)
}
pub(super) fn free_handle(&mut self, handle: Handle) {
self.heaps[handle.heap_index].free_handle(handle.raw);
self.available_heap_indices.insert(handle.heap_index);
}
}
pub(super) struct CpuHeapInner {
pub _raw: Direct3D12::ID3D12DescriptorHeap,
pub stage: Vec<Direct3D12::D3D12_CPU_DESCRIPTOR_HANDLE>,
}
pub(super) struct CpuHeap {
pub inner: Mutex<CpuHeapInner>,
start: Direct3D12::D3D12_CPU_DESCRIPTOR_HANDLE,
handle_size: u32,
total: u32,
}
unsafe impl Send for CpuHeap {}
unsafe impl Sync for CpuHeap {}
impl CpuHeap {
pub(super) fn new(
device: &Direct3D12::ID3D12Device,
ty: Direct3D12::D3D12_DESCRIPTOR_HEAP_TYPE,
total: u32,
) -> Result<Self, crate::DeviceError> {
let handle_size = unsafe { device.GetDescriptorHandleIncrementSize(ty) };
let desc = Direct3D12::D3D12_DESCRIPTOR_HEAP_DESC {
Type: ty,
NumDescriptors: total,
Flags: Direct3D12::D3D12_DESCRIPTOR_HEAP_FLAG_NONE,
NodeMask: 0,
};
let raw = unsafe { device.CreateDescriptorHeap::<Direct3D12::ID3D12DescriptorHeap>(&desc) }
.into_device_result("CPU descriptor heap creation")?;
let start = unsafe { raw.GetCPUDescriptorHandleForHeapStart() };
Ok(Self {
inner: Mutex::new(CpuHeapInner {
_raw: raw,
stage: Vec::new(),
}),
start,
handle_size,
total,
})
}
pub(super) fn at(&self, index: u32) -> Direct3D12::D3D12_CPU_DESCRIPTOR_HANDLE {
debug_assert!(
index < self.total,
"Index ({index}) out of bounds {total}",
total = self.total
);
Direct3D12::D3D12_CPU_DESCRIPTOR_HANDLE {
ptr: self.start.ptr + (self.handle_size * index) as usize,
}
}
}
impl fmt::Debug for CpuHeap {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("CpuHeap")
.field("start", &self.start.ptr)
.field("handle_size", &self.handle_size)
.field("total", &self.total)
.finish()
}
}
pub(super) unsafe fn upload(
device: &Direct3D12::ID3D12Device,
src: &CpuHeapInner,
dst: &GeneralHeap,
dummy_copy_counts: &[u32],
) -> Result<DualHandle, crate::DeviceError> {
let count = src.stage.len() as u32;
let index = dst.allocate_slice(count as u64)?;
unsafe {
device.CopyDescriptors(
1,
&dst.cpu_descriptor_at(index),
Some(&count),
count,
src.stage.as_ptr(),
Some(dummy_copy_counts.as_ptr()),
dst.ty,
)
};
Ok(dst.at(index, count as u64))
}
File diff suppressed because it is too large Load Diff
+176
View File
@@ -0,0 +1,176 @@
use alloc::sync::Arc;
use core::ops::Deref;
use windows::core::Interface as _;
use windows::Win32::Graphics::{Direct3D, Direct3D12};
use super::D3D12Lib;
use crate::auxil::dxgi::factory::DxgiAdapter;
/// Abstraction over D3D12 device creation.
///
/// Supports two paths:
/// - **Independent**: Uses `ID3D12DeviceFactory` from the Agility SDK's Independent Devices API.
/// - **Legacy**: Uses the traditional `D3D12CreateDevice` export.
pub(super) enum DeviceFactory {
/// Uses `ID3D12DeviceFactory` from the Independent Devices API.
Independent(Direct3D12::ID3D12DeviceFactory),
/// Uses the traditional `D3D12CreateDevice` export.
Legacy,
}
impl DeviceFactory {
/// Create a new `DeviceFactory`.
///
/// If `agility_sdk` is `Some`, attempts to set up the Independent Devices API path.
/// On failure, the behavior depends on
/// [`on_load_failure`](wgt::Dx12AgilitySDKLoadFailure):
/// - [`Fallback`](wgt::Dx12AgilitySDKLoadFailure::Fallback): logs a warning and
/// returns `Ok(Legacy)`.
/// - [`Error`](wgt::Dx12AgilitySDKLoadFailure::Error): returns an `Err`.
pub(super) fn new(
lib: &D3D12Lib,
agility_sdk: Option<&wgt::Dx12AgilitySDK>,
) -> Result<Self, crate::InstanceError> {
let Some(agility_sdk) = agility_sdk else {
log::debug!("No D3D12 Agility SDK configuration provided; using system D3D12 runtime");
return Ok(Self::Legacy);
};
match Self::try_create_independent(lib, agility_sdk) {
Ok(factory) => {
log::debug!(
"Using D3D12 Agility SDK v{} from '{}'",
agility_sdk.sdk_version,
agility_sdk.sdk_path
);
Ok(Self::Independent(factory))
}
Err(err) => {
let message = format!(
"Failed to initialize D3D12 Agility SDK (v{} at '{}'): {err}",
agility_sdk.sdk_version, agility_sdk.sdk_path
);
match agility_sdk.on_load_failure {
wgt::Dx12AgilitySDKLoadFailure::Fallback => {
log::warn!("{message}; falling back to system D3D12 runtime");
Ok(Self::Legacy)
}
wgt::Dx12AgilitySDKLoadFailure::Error => {
Err(crate::InstanceError::new(message))
}
}
}
}
}
fn try_create_independent(
lib: &D3D12Lib,
agility_sdk: &wgt::Dx12AgilitySDK,
) -> Result<Direct3D12::ID3D12DeviceFactory, DeviceFactoryError> {
// Step 1: Get ID3D12SDKConfiguration1 via D3D12GetInterface
let sdk_config: Direct3D12::ID3D12SDKConfiguration1 = lib
.get_interface(&Direct3D12::CLSID_D3D12SDKConfiguration)
.map_err(DeviceFactoryError::GetInterface)?;
// Step 2: Create device factory with the specified SDK version and path
let sdk_path = std::ffi::CString::new(agility_sdk.sdk_path.as_bytes())
.map_err(|_| DeviceFactoryError::InvalidPath)?;
let factory: Direct3D12::ID3D12DeviceFactory = unsafe {
sdk_config.CreateDeviceFactory(
agility_sdk.sdk_version,
windows::core::PCSTR(sdk_path.as_ptr().cast::<u8>()),
)
}
.map_err(DeviceFactoryError::CreateDeviceFactory)?;
Ok(factory)
}
/// Enable the D3D12 debug layer and optionally GPU-based validation.
///
/// - **Legacy**: configures debug globally via `D3D12GetDebugInterface`.
/// - **Independent**: uses `GetConfigurationInterface` to get an
/// `ID3D12Debug` scoped to the factory.
pub(super) fn enable_debug_layer(&self, lib: &D3D12Lib, flags: wgt::InstanceFlags) {
if !flags
.intersects(wgt::InstanceFlags::VALIDATION | wgt::InstanceFlags::GPU_BASED_VALIDATION)
{
return;
}
let debug_controller = match self {
Self::Independent(factory) => {
match unsafe {
factory.GetConfigurationInterface::<Direct3D12::ID3D12Debug>(
&Direct3D12::CLSID_D3D12Debug,
)
} {
Ok(debug) => debug,
Err(err) => {
log::warn!("Failed to get debug interface from device factory: {err}");
return;
}
}
}
Self::Legacy => match lib.debug_interface() {
Ok(Some(debug)) => debug,
Ok(None) => return,
Err(err) => {
log::warn!("Failed to get debug interface: {err}");
return;
}
},
};
if flags.intersects(wgt::InstanceFlags::VALIDATION) {
unsafe { debug_controller.EnableDebugLayer() }
}
if flags.intersects(wgt::InstanceFlags::GPU_BASED_VALIDATION) {
if let Ok(debug1) = debug_controller.cast::<Direct3D12::ID3D12Debug1>() {
unsafe { debug1.SetEnableGPUBasedValidation(true) }
} else {
log::warn!("Failed to enable GPU-based validation");
}
}
}
/// Create a D3D12 device using the appropriate method.
pub(super) fn create_device(
&self,
lib: &Arc<D3D12Lib>,
adapter: &DxgiAdapter,
feature_level: Direct3D::D3D_FEATURE_LEVEL,
) -> Result<Direct3D12::ID3D12Device, super::CreateDeviceError> {
match self {
Self::Independent(factory) => {
let mut result__: Option<Direct3D12::ID3D12Device> = None;
unsafe { factory.CreateDevice(adapter.deref(), feature_level, &mut result__) }
.map_err(|e| super::CreateDeviceError::D3D12CreateDevice(e.into()))?;
result__.ok_or(super::CreateDeviceError::RetDeviceIsNull)
}
Self::Legacy => lib.create_device(adapter, feature_level),
}
}
}
impl core::fmt::Debug for DeviceFactory {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Independent(_) => write!(f, "DeviceFactory::Independent"),
Self::Legacy => write!(f, "DeviceFactory::Legacy"),
}
}
}
#[derive(Debug, thiserror::Error)]
enum DeviceFactoryError {
#[error("failed to get ID3D12SDKConfiguration1: {0}")]
GetInterface(super::GetInterfaceError),
#[error("SDK path contains null bytes")]
InvalidPath,
#[error("CreateDeviceFactory failed: {0}")]
CreateDeviceFactory(windows::core::Error),
}
+189
View File
@@ -0,0 +1,189 @@
use alloc::{string::String, sync::Arc, vec::Vec};
use parking_lot::RwLock;
use windows::Win32::{Foundation, Graphics::Dxgi};
use super::SurfaceTarget;
use crate::{
auxil,
dx12::{
device_creation::DeviceFactory, shader_compilation::CompilerContainer, D3D12Lib, DCompLib,
},
};
impl crate::Instance for super::Instance {
type A = super::Api;
unsafe fn init(desc: &crate::InstanceDescriptor<'_>) -> Result<Self, crate::InstanceError> {
profiling::scope!("Init DX12 Backend");
let lib_main = D3D12Lib::new().map_err(|e| {
crate::InstanceError::with_source(String::from("failed to load d3d12.dll"), e)
})?;
// Create DeviceFactory first so we know which debug path to use
let device_factory =
DeviceFactory::new(&lib_main, desc.backend_options.dx12.agility_sdk.as_ref())?;
device_factory.enable_debug_layer(&lib_main, desc.flags);
let (lib_dxgi, factory) = auxil::dxgi::factory::create_factory(desc.flags)?;
// Create IDXGIFactoryMedia
let factory_media = lib_dxgi.create_factory_media().ok();
let mut supports_allow_tearing = false;
if let Some(factory5) = factory.as_factory5() {
let mut allow_tearing = Foundation::FALSE;
let hr = unsafe {
factory5.CheckFeatureSupport(
Dxgi::DXGI_FEATURE_PRESENT_ALLOW_TEARING,
<*mut _>::cast(&mut allow_tearing),
size_of_val(&allow_tearing) as u32,
)
};
match hr {
Err(err) => log::warn!("Unable to check for tearing support: {err}"),
Ok(()) => supports_allow_tearing = true,
}
}
// Initialize the shader compiler
let compiler_container = match desc.backend_options.dx12.shader_compiler.clone() {
wgt::Dx12Compiler::DynamicDxc { dxc_path } => {
CompilerContainer::new_dynamic_dxc(dxc_path.into()).map_err(|e| {
crate::InstanceError::with_source(String::from("Failed to load dynamic DXC"), e)
})?
}
wgt::Dx12Compiler::StaticDxc => CompilerContainer::new_static_dxc().map_err(|e| {
crate::InstanceError::with_source(String::from("Failed to load static DXC"), e)
})?,
wgt::Dx12Compiler::Fxc => CompilerContainer::new_fxc().map_err(|e| {
crate::InstanceError::with_source(String::from("Failed to load FXC"), e)
})?,
wgt::Dx12Compiler::Auto => {
if cfg!(static_dxc) {
// Prefer static DXC if its compiled in
CompilerContainer::new_static_dxc().map_err(|e| {
crate::InstanceError::with_source(
String::from("Failed to load static DXC"),
e,
)
})?
} else {
// Try to load dynamic DXC
let dynamic = CompilerContainer::new_dynamic_dxc("dxcompiler.dll".into());
match dynamic {
Ok(v) => v,
Err(super::shader_compilation::GetContainerError::FailedToLoad(..)) => {
// If it can't be found load FXC
CompilerContainer::new_fxc().map_err(|e| {
crate::InstanceError::with_source(
String::from("Failed to load FXC"),
e,
)
})?
}
Err(e) => {
// If another error occurs when loading static DXC return that error
return Err(crate::InstanceError::with_source(
String::from("Failed to load dynamic DXC"),
e,
));
}
}
}
}
};
match compiler_container {
CompilerContainer::DynamicDxc(..) => {
log::debug!("Using dynamic DXC for shader compilation")
}
CompilerContainer::StaticDxc(..) => {
log::debug!("Using static DXC for shader compilation")
}
CompilerContainer::Fxc(..) => {
log::debug!("Using FXC for shader compilation")
}
}
Ok(Self {
// The call to create_factory will only succeed if we get a factory4, so this is safe.
factory,
factory_media,
library: Arc::new(lib_main),
device_factory: Arc::new(device_factory),
dcomp_lib: Arc::new(DCompLib::new()),
presentation_system: desc.backend_options.dx12.presentation_system,
_lib_dxgi: lib_dxgi,
supports_allow_tearing,
flags: desc.flags,
memory_budget_thresholds: desc.memory_budget_thresholds,
compiler_container: Arc::new(compiler_container),
options: desc.backend_options.dx12.clone(),
telemetry: desc.telemetry,
})
}
unsafe fn create_surface(
&self,
display_handle: raw_window_handle::RawDisplayHandle,
window_handle: raw_window_handle::RawWindowHandle,
) -> Result<super::Surface, crate::InstanceError> {
assert!(matches!(
display_handle,
raw_window_handle::RawDisplayHandle::Windows(_)
));
match window_handle {
raw_window_handle::RawWindowHandle::Win32(handle) => {
// https://github.com/rust-windowing/raw-window-handle/issues/171
let handle = Foundation::HWND(handle.hwnd.get() as *mut _);
let target = match self.presentation_system {
wgt::Dx12SwapchainKind::DxgiFromHwnd => SurfaceTarget::WndHandle(handle),
wgt::Dx12SwapchainKind::DxgiFromVisual => SurfaceTarget::VisualFromWndHandle {
handle,
dcomp_state: Default::default(),
},
};
Ok(super::Surface {
factory: self.factory.clone(),
factory_media: self.factory_media.clone(),
target,
supports_allow_tearing: self.supports_allow_tearing,
swap_chain: RwLock::new(None),
options: self.options.clone(),
hdr_source: Some(auxil::dxgi::hdr::DxgiHdrSource::new(handle)),
})
}
_ => Err(crate::InstanceError::new(format!(
"window handle {window_handle:?} is not a Win32 handle"
))),
}
}
unsafe fn enumerate_adapters(
&self,
_surface_hint: Option<&super::Surface>,
) -> Vec<crate::ExposedAdapter<super::Api>> {
let adapters = auxil::dxgi::factory::enumerate_adapters(self.factory.clone());
adapters
.into_iter()
.filter_map(|raw| {
super::Adapter::expose(
raw,
&self.library,
&self.device_factory,
&self.dcomp_lib,
self.flags,
self.memory_budget_thresholds,
self.compiler_container.clone(),
self.options.clone(),
self.telemetry,
)
})
.collect()
}
}
File diff suppressed because it is too large Load Diff
+344
View File
@@ -0,0 +1,344 @@
//! We try to use pipeline stream descriptors where possible, but this isn't allowed
//! on some older windows 10 versions. Therefore, we also must have some logic to
//! convert such descriptors to the "traditional" equivalent,
//! `D3D12_GRAPHICS_PIPELINE_STATE_DESC`.
//!
//! Stream descriptors allow extending the pipeline, enabling more advanced features,
//! including mesh shaders and multiview/view instancing. Using a stream descriptor
//! is like using a vulkan descriptor with a `pNext` chain. It doesn't have direct
//! benefits to all use cases, but allows new use cases.
//!
//! The code for pipeline stream descriptors is very complicated, and can have bad
//! consequences if it is written incorrectly. It has been isolated to this file for
//! that reason.
use core::{ffi::c_void, mem::ManuallyDrop, ptr::NonNull};
use alloc::vec::Vec;
use windows::Win32::Graphics::Direct3D12::*;
use windows::Win32::Graphics::Dxgi::Common::*;
use windows_core::Interface;
use crate::dx12::borrow_interface_temporarily;
// Wrapper newtypes for various pipeline subobjects which
// use complicated or non-unique representations.
#[repr(transparent)]
#[derive(Copy, Clone)]
// Option<NonNull<c_void>> is guaranteed to have the same representation as a raw pointer.
struct RootSignature(Option<NonNull<c_void>>);
#[repr(transparent)]
#[derive(Copy, Clone)]
struct VertexShader(D3D12_SHADER_BYTECODE);
#[repr(transparent)]
#[derive(Copy, Clone)]
struct PixelShader(D3D12_SHADER_BYTECODE);
#[repr(transparent)]
#[derive(Copy, Clone)]
struct MeshShader(D3D12_SHADER_BYTECODE);
#[repr(transparent)]
#[derive(Copy, Clone)]
struct TaskShader(D3D12_SHADER_BYTECODE);
#[repr(transparent)]
#[derive(Copy, Clone)]
struct SampleMask(u32);
#[repr(transparent)]
#[derive(Copy, Clone)]
struct NodeMask(u32);
/// Trait for types that can be used as subobjects in a pipeline state stream.
///
/// Safety:
/// - The type must be the correct alignment and size for the subobject it represents.
/// - The type must map to exactly one `D3D12_PIPELINE_STATE_SUBOBJECT_TYPE` variant.
/// - The variant must correctly represent the type's role in the pipeline state stream.
/// - The type must be `Copy` to ensure safe duplication in the stream.
/// - The type must be valid to memcpy into the pipeline state stream.
unsafe trait RenderPipelineStreamObject: Copy {
const SUBOBJECT_TYPE: D3D12_PIPELINE_STATE_SUBOBJECT_TYPE;
}
macro_rules! implement_stream_object {
(unsafe $ty:ty => $variant:expr) => {
unsafe impl RenderPipelineStreamObject for $ty {
const SUBOBJECT_TYPE: D3D12_PIPELINE_STATE_SUBOBJECT_TYPE = $variant;
}
};
}
implement_stream_object! { unsafe RootSignature => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_ROOT_SIGNATURE }
implement_stream_object! { unsafe VertexShader => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_VS }
implement_stream_object! { unsafe PixelShader => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_PS }
implement_stream_object! { unsafe MeshShader => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_MS }
implement_stream_object! { unsafe TaskShader => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_AS }
implement_stream_object! { unsafe D3D12_BLEND_DESC => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_BLEND }
implement_stream_object! { unsafe SampleMask => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_SAMPLE_MASK }
implement_stream_object! { unsafe D3D12_RASTERIZER_DESC => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_RASTERIZER }
implement_stream_object! { unsafe D3D12_DEPTH_STENCIL_DESC => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL }
implement_stream_object! { unsafe D3D12_PRIMITIVE_TOPOLOGY_TYPE => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_PRIMITIVE_TOPOLOGY }
implement_stream_object! { unsafe D3D12_RT_FORMAT_ARRAY => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_RENDER_TARGET_FORMATS }
implement_stream_object! { unsafe DXGI_FORMAT => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL_FORMAT }
implement_stream_object! { unsafe DXGI_SAMPLE_DESC => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_SAMPLE_DESC }
implement_stream_object! { unsafe NodeMask => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_NODE_MASK }
implement_stream_object! { unsafe D3D12_CACHED_PIPELINE_STATE => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_CACHED_PSO }
implement_stream_object! { unsafe D3D12_PIPELINE_STATE_FLAGS => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_FLAGS }
implement_stream_object! { unsafe D3D12_INPUT_LAYOUT_DESC => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_INPUT_LAYOUT }
implement_stream_object! { unsafe D3D12_INDEX_BUFFER_STRIP_CUT_VALUE => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_IB_STRIP_CUT_VALUE }
implement_stream_object! { unsafe D3D12_STREAM_OUTPUT_DESC => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_STREAM_OUTPUT }
implement_stream_object! { unsafe D3D12_VIEW_INSTANCING_DESC => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_VIEW_INSTANCING }
/// Implementaation of a pipeline state stream, which is a sequence of subobjects put into
/// a byte array according to some basic alignment rules.
///
/// Each subobject must start on an 8 byte boundary. Each subobject contains a 32 bit
/// type identifier, followed by the actual subobject data, aligned as required by the
/// subobject's structure.
///
/// See <https://learn.microsoft.com/en-us/windows/win32/api/d3d12/ns-d3d12-d3d12_pipeline_state_stream_desc>
/// for more information.
pub(super) struct RenderPipelineStateStream<'a> {
bytes: Vec<u8>,
_marker: core::marker::PhantomData<&'a ()>,
}
impl<'a> RenderPipelineStateStream<'a> {
fn new() -> Self {
// Dynamic allocation is used here because the resulting stream can become very large.
// We pre-allocate the size based on an estimate of the size of the struct plus some extra space
// per member for tags and alignment padding. In practice this will always be too big, as not
// all members will be used.
let size_of_stream_desc = size_of::<RenderPipelineStateStreamDesc>();
let members = 20; // Approximate number of members we might push
let capacity = size_of_stream_desc + members * 8; // Extra space for tags and alignment
Self {
bytes: Vec::with_capacity(capacity),
_marker: core::marker::PhantomData,
}
}
/// Align the internal byte buffer to the given alignment,
/// padding with zeros as necessary.
fn align_to(&mut self, alignment: usize) {
let aligned_length = self.bytes.len().next_multiple_of(alignment);
self.bytes.resize(aligned_length, 0);
}
/// Adds a subobject to the pipeline state stream.
fn add_object<T: RenderPipelineStreamObject>(&mut self, object: T) {
// Ensure 8-byte alignment for the subobject start.
self.align_to(8);
// Append the type tag (u32)
let tag: u32 = T::SUBOBJECT_TYPE.0 as u32;
self.bytes.extend_from_slice(&tag.to_ne_bytes());
// Align the data to its natural alignment.
self.align_to(align_of_val::<T>(&object));
// Append the data itself, as raw bytes
let data_ptr: *const T = &object;
let data_u8_ptr: *const u8 = data_ptr.cast::<u8>();
let data_size = size_of_val::<T>(&object);
let slice = unsafe { core::slice::from_raw_parts::<u8>(data_u8_ptr, data_size) };
self.bytes.extend_from_slice(slice);
}
/// Creates a pipeline state object from the stream.
///
/// Safety:
/// - All unsafety invariants required by [`ID3D12Device2::CreatePipelineState`] must be upheld by the caller.
pub unsafe fn create_pipeline_state(
&mut self,
device: &ID3D12Device2,
) -> windows::core::Result<ID3D12PipelineState> {
let stream_desc = D3D12_PIPELINE_STATE_STREAM_DESC {
SizeInBytes: self.bytes.len(),
pPipelineStateSubobjectStream: self.bytes.as_mut_ptr().cast(),
};
// Safety: lifetime on Self preserved the contents
// of the stream. Other unsafety invariants are upheld by the caller.
unsafe { device.CreatePipelineState(&stream_desc) }
}
}
#[repr(C)]
#[derive(Debug)]
pub struct RenderPipelineStateStreamDesc<'a> {
pub root_signature: Option<&'a ID3D12RootSignature>,
pub pixel_shader: D3D12_SHADER_BYTECODE,
pub blend_state: D3D12_BLEND_DESC,
pub sample_mask: u32,
pub rasterizer_state: D3D12_RASTERIZER_DESC,
pub depth_stencil_state: D3D12_DEPTH_STENCIL_DESC,
pub primitive_topology_type: D3D12_PRIMITIVE_TOPOLOGY_TYPE,
pub rtv_formats: D3D12_RT_FORMAT_ARRAY,
pub dsv_format: DXGI_FORMAT,
pub sample_desc: DXGI_SAMPLE_DESC,
pub node_mask: u32,
pub cached_pso: D3D12_CACHED_PIPELINE_STATE,
pub flags: D3D12_PIPELINE_STATE_FLAGS,
pub view_instancing: Option<D3D12_VIEW_INSTANCING_DESC>,
// Vertex pipeline specific
pub vertex_shader: D3D12_SHADER_BYTECODE,
pub input_layout: D3D12_INPUT_LAYOUT_DESC,
pub index_buffer_strip_cut_value: D3D12_INDEX_BUFFER_STRIP_CUT_VALUE,
pub stream_output: D3D12_STREAM_OUTPUT_DESC,
// Mesh pipeline specific
pub task_shader: D3D12_SHADER_BYTECODE,
pub mesh_shader: D3D12_SHADER_BYTECODE,
}
impl RenderPipelineStateStreamDesc<'_> {
pub fn to_stream(&self) -> RenderPipelineStateStream<'_> {
let mut stream = RenderPipelineStateStream::new();
// Importantly here, the ID3D12RootSignature _itself_ is the pointer we're
// trying to serialize into the stream, not a pointer to the pointer.
//
// This is correct because as_raw() returns turns that smart object into the raw
// pointer that _is_ the com object handle.
let root_sig_pointer = self
.root_signature
.map(|a| NonNull::new(a.as_raw()).unwrap());
// Because the stream object borrows from self for its entire lifetime,
// it is safe to store the pointer into it.
stream.add_object(RootSignature(root_sig_pointer));
stream.add_object(self.blend_state);
stream.add_object(SampleMask(self.sample_mask));
stream.add_object(self.rasterizer_state);
stream.add_object(self.depth_stencil_state);
stream.add_object(self.primitive_topology_type);
if self.rtv_formats.NumRenderTargets != 0 {
stream.add_object(self.rtv_formats);
}
if self.dsv_format != DXGI_FORMAT_UNKNOWN {
stream.add_object(self.dsv_format);
}
stream.add_object(self.sample_desc);
if self.node_mask != 0 {
stream.add_object(NodeMask(self.node_mask));
}
if !self.cached_pso.pCachedBlob.is_null() {
stream.add_object(self.cached_pso);
}
stream.add_object(self.flags);
if let Some(view_instancing) = self.view_instancing {
stream.add_object(view_instancing);
}
if !self.pixel_shader.pShaderBytecode.is_null() {
stream.add_object(PixelShader(self.pixel_shader));
}
if !self.vertex_shader.pShaderBytecode.is_null() {
stream.add_object(VertexShader(self.vertex_shader));
stream.add_object(self.input_layout);
stream.add_object(self.index_buffer_strip_cut_value);
stream.add_object(self.stream_output);
}
if !self.task_shader.pShaderBytecode.is_null() {
stream.add_object(TaskShader(self.task_shader));
}
if !self.mesh_shader.pShaderBytecode.is_null() {
stream.add_object(MeshShader(self.mesh_shader));
}
stream
}
/// Returns a traditional D3D12_GRAPHICS_PIPELINE_STATE_DESC.
///
/// Safety:
/// - This returned struct must not outlive self.
pub unsafe fn to_graphics_pipeline_descriptor(&self) -> D3D12_GRAPHICS_PIPELINE_STATE_DESC {
D3D12_GRAPHICS_PIPELINE_STATE_DESC {
pRootSignature: if let Some(rsig) = self.root_signature {
unsafe { borrow_interface_temporarily(rsig) }
} else {
ManuallyDrop::new(None)
},
VS: self.vertex_shader,
PS: self.pixel_shader,
DS: D3D12_SHADER_BYTECODE::default(),
HS: D3D12_SHADER_BYTECODE::default(),
GS: D3D12_SHADER_BYTECODE::default(),
StreamOutput: self.stream_output,
BlendState: self.blend_state,
SampleMask: self.sample_mask,
RasterizerState: self.rasterizer_state,
DepthStencilState: self.depth_stencil_state,
InputLayout: self.input_layout,
IBStripCutValue: self.index_buffer_strip_cut_value,
PrimitiveTopologyType: self.primitive_topology_type,
NumRenderTargets: self.rtv_formats.NumRenderTargets,
RTVFormats: self.rtv_formats.RTFormats,
DSVFormat: self.dsv_format,
SampleDesc: self.sample_desc,
NodeMask: self.node_mask,
CachedPSO: self.cached_pso,
Flags: self.flags,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn wrappers() {
assert_eq!(size_of::<RootSignature>(), size_of::<ID3D12RootSignature>());
assert_eq!(
align_of::<RootSignature>(),
align_of::<ID3D12RootSignature>()
)
}
implement_stream_object!(unsafe u16 => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE(1));
implement_stream_object!(unsafe u32 => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE(2));
implement_stream_object!(unsafe u64 => D3D12_PIPELINE_STATE_SUBOBJECT_TYPE(3));
#[test]
fn stream() {
let mut stream = RenderPipelineStateStream::new();
stream.add_object(42u16);
stream.add_object(84u32);
stream.add_object(168u64);
assert_eq!(stream.bytes.len(), 32);
// Object 1: u16
// Tag at the beginning
assert_eq!(&stream.bytes[0..4], &1u32.to_ne_bytes());
// Data tucked in, aligned to the natural alignment of u16
assert_eq!(&stream.bytes[4..6], &42u16.to_ne_bytes());
// Padding to align the next subobject to an 8 byte boundary.
assert_eq!(&stream.bytes[6..8], &[0, 0]);
// Object 2: u32
// Tag at the beginning
assert_eq!(&stream.bytes[8..12], &2u32.to_ne_bytes());
// Data tucked in, aligned to the natural alignment of u32
assert_eq!(&stream.bytes[12..16], &84u32.to_ne_bytes());
// Object 3: u64
// Tag at the beginning
assert_eq!(&stream.bytes[16..20], &3u32.to_ne_bytes());
// Padding to align the u64 to an 8 byte boundary.
assert_eq!(&stream.bytes[20..24], &[0, 0, 0, 0]);
// Data tucked in, aligned to the natural alignment of u64
assert_eq!(&stream.bytes[24..32], &168u64.to_ne_bytes());
}
}
+252
View File
@@ -0,0 +1,252 @@
//! Sampler management for DX12.
//!
//! Nearly identical to the Vulkan sampler cache, with added descriptor heap management.
use alloc::vec::Vec;
use hashbrown::{hash_map::Entry, HashMap};
use ordered_float::OrderedFloat;
use parking_lot::Mutex;
use windows::Win32::Graphics::Direct3D12::*;
use crate::dx12::HResult;
/// The index of a sampler in the global sampler heap.
///
/// This is a type-safe, transparent wrapper around a u32.
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub(crate) struct SamplerIndex(u32);
/// [`D3D12_SAMPLER_DESC`] is not hashable, so we wrap it in a newtype that is.
///
/// We use [`OrderedFloat`] to allow for floating point values to be compared and
/// hashed in a defined way.
#[derive(Debug, Copy, Clone)]
struct HashableSamplerDesc(D3D12_SAMPLER_DESC);
impl PartialEq for HashableSamplerDesc {
fn eq(&self, other: &Self) -> bool {
self.0.Filter == other.0.Filter
&& self.0.AddressU == other.0.AddressU
&& self.0.AddressV == other.0.AddressV
&& self.0.AddressW == other.0.AddressW
&& OrderedFloat(self.0.MipLODBias) == OrderedFloat(other.0.MipLODBias)
&& self.0.MaxAnisotropy == other.0.MaxAnisotropy
&& self.0.ComparisonFunc == other.0.ComparisonFunc
&& self.0.BorderColor.map(OrderedFloat) == other.0.BorderColor.map(OrderedFloat)
&& OrderedFloat(self.0.MinLOD) == OrderedFloat(other.0.MinLOD)
&& OrderedFloat(self.0.MaxLOD) == OrderedFloat(other.0.MaxLOD)
}
}
impl Eq for HashableSamplerDesc {}
impl core::hash::Hash for HashableSamplerDesc {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
self.0.Filter.0.hash(state);
self.0.AddressU.0.hash(state);
self.0.AddressV.0.hash(state);
self.0.AddressW.0.hash(state);
OrderedFloat(self.0.MipLODBias).hash(state);
self.0.MaxAnisotropy.hash(state);
self.0.ComparisonFunc.0.hash(state);
self.0.BorderColor.map(OrderedFloat).hash(state);
OrderedFloat(self.0.MinLOD).hash(state);
OrderedFloat(self.0.MaxLOD).hash(state);
}
}
/// Entry in the sampler cache.
struct CacheEntry {
index: SamplerIndex,
ref_count: u32,
}
/// Container for the mutable management state of the sampler heap.
///
/// We have this separated, using interior mutability, to allow for the outside world
/// to access the heap directly without needing to take the lock.
pub(crate) struct SamplerHeapState {
/// Mapping from the sampler description to the index within the heap and the refcount.
mapping: HashMap<HashableSamplerDesc, CacheEntry>,
/// List of free sampler indices.
freelist: Vec<SamplerIndex>,
}
/// Global sampler heap for the device.
///
/// As D3D12 only allows 2048 samplers to be in a single heap, we need to cache
/// samplers aggressively and refer to them in shaders by index.
pub(crate) struct SamplerHeap {
/// Mutable management state of the sampler heap.
state: Mutex<SamplerHeapState>,
/// The heap itself.
heap: ID3D12DescriptorHeap,
/// The CPU-side handle to the first descriptor in the heap.
///
/// Both the CPU and GPU handles point to the same descriptor, just in
/// different contexts.
heap_cpu_start_handle: D3D12_CPU_DESCRIPTOR_HANDLE,
/// The GPU-side handle to the first descriptor in the heap.
///
/// Both the CPU and GPU handles point to the same descriptor, just in
/// different contexts.
heap_gpu_start_handle: D3D12_GPU_DESCRIPTOR_HANDLE,
/// This is the device-specific size of sampler descriptors.
descriptor_stride: u32,
}
impl SamplerHeap {
pub fn new(
device: &ID3D12Device,
private_caps: &super::PrivateCapabilities,
) -> Result<Self, crate::DeviceError> {
profiling::scope!("SamplerHeap::new");
// WARP can report this as 2M or more. We clamp it to 64k to be safe.
const SAMPLER_HEAP_SIZE_CLAMP: u32 = 64 * 1024;
let max_unique_samplers = private_caps
.max_sampler_descriptor_heap_size
.min(SAMPLER_HEAP_SIZE_CLAMP);
let desc = D3D12_DESCRIPTOR_HEAP_DESC {
Type: D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER,
NumDescriptors: max_unique_samplers,
Flags: D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE,
NodeMask: 0,
};
let heap = unsafe { device.CreateDescriptorHeap::<ID3D12DescriptorHeap>(&desc) }
.into_device_result("Failed to create global GPU-Visible Sampler Descriptor Heap")?;
let heap_cpu_start_handle = unsafe { heap.GetCPUDescriptorHandleForHeapStart() };
let heap_gpu_start_handle = unsafe { heap.GetGPUDescriptorHandleForHeapStart() };
let descriptor_stride =
unsafe { device.GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER) };
Ok(Self {
state: Mutex::new(SamplerHeapState {
mapping: HashMap::new(),
// Reverse so that samplers get allocated starting from zero.
freelist: (0..max_unique_samplers).map(SamplerIndex).rev().collect(),
}),
heap,
heap_cpu_start_handle,
heap_gpu_start_handle,
descriptor_stride,
})
}
/// Returns a reference to the raw descriptor heap.
pub fn heap(&self) -> &ID3D12DescriptorHeap {
&self.heap
}
/// Returns a reference the handle to be bound to the descriptor table.
pub fn gpu_descriptor_table(&self) -> D3D12_GPU_DESCRIPTOR_HANDLE {
self.heap_gpu_start_handle
}
/// Add a sampler with the given description to the heap.
///
/// If the sampler already exists, the refcount is incremented and the existing index is returned.
///
/// If the sampler does not exist, a new sampler is created and the index is returned.
///
/// If the heap is full, an error is returned.
pub fn create_sampler(
&self,
device: &ID3D12Device,
desc: D3D12_SAMPLER_DESC,
) -> Result<SamplerIndex, crate::DeviceError> {
profiling::scope!("SamplerHeap::create_sampler");
let hashable_desc = HashableSamplerDesc(desc);
// Eagarly dereference the lock to allow split borrows.
let state = &mut *self.state.lock();
// Lookup the sampler in the mapping.
match state.mapping.entry(hashable_desc) {
Entry::Occupied(occupied_entry) => {
// We have found a match, so increment the refcount and return the index.
let entry = occupied_entry.into_mut();
entry.ref_count += 1;
Ok(entry.index)
}
Entry::Vacant(vacant_entry) => {
// We need to create a new sampler.
// Try to get a new index from the freelist.
let Some(index) = state.freelist.pop() else {
// If the freelist is empty, we have hit the maximum number of samplers.
log::error!("There is no more room in the global sampler heap for more unique samplers. Your device supports a maximum of {} unique samplers.", state.mapping.len());
return Err(crate::DeviceError::OutOfMemory);
};
// Compute the CPU side handle for the new sampler.
let handle = D3D12_CPU_DESCRIPTOR_HANDLE {
ptr: self.heap_cpu_start_handle.ptr
+ self.descriptor_stride as usize * index.0 as usize,
};
unsafe {
device.CreateSampler(&desc, handle);
}
// Insert the new sampler into the mapping.
vacant_entry.insert(CacheEntry {
index,
ref_count: 1,
});
Ok(index)
}
}
}
/// Decrement the refcount of the sampler with the given description.
///
/// If the refcount reaches zero, the sampler is destroyed and the index is returned to the freelist.
///
/// The provided index is checked against the index of the sampler with the given description, ensuring
/// that there isn't a clerical error from the caller.
pub fn destroy_sampler(&self, desc: D3D12_SAMPLER_DESC, provided_index: SamplerIndex) {
profiling::scope!("SamplerHeap::destroy_sampler");
// Eagarly dereference the lock to allow split borrows.
let state = &mut *self.state.lock();
// Get the index of the sampler to destroy.
let Entry::Occupied(mut hash_map_entry) = state.mapping.entry(HashableSamplerDesc(desc))
else {
log::error!(
"Tried to destroy a sampler that doesn't exist. Sampler description: {desc:#?}"
);
return;
};
let cache_entry = hash_map_entry.get_mut();
// Ensure that the provided index matches the index of the sampler to destroy.
assert_eq!(
cache_entry.index, provided_index,
"Mismatched sampler index, this is an implementation bug"
);
// Decrement the refcount of the sampler.
cache_entry.ref_count -= 1;
// If we are the last reference, remove the sampler from the mapping and return the index to the freelist.
//
// As samplers only exist as descriptors in the heap, there is nothing needed to be done to destroy the sampler.
if cache_entry.ref_count == 0 {
state.freelist.push(cache_entry.index);
hash_map_entry.remove();
}
}
}
+450
View File
@@ -0,0 +1,450 @@
use alloc::{string::String, vec::Vec};
use core::ffi::CStr;
use std::path::PathBuf;
use crate::auxil::dxgi::result::HResult;
use thiserror::Error;
use windows::{
core::{Interface, PCSTR, PCWSTR},
Win32::Graphics::Direct3D::{Dxc, Fxc, ID3DBlob, D3D_SHADER_MACRO},
};
pub(super) enum CompilerContainer {
Fxc(CompilerFxc),
DynamicDxc(CompilerDynamicDxc),
#[cfg_attr(not(static_dxc), allow(unused))]
StaticDxc(CompilerStaticDxc),
}
pub(super) struct CompilerFxc {
fxc: FxcLib,
}
pub(super) struct CompilerDynamicDxc {
max_shader_model: wgt::DxcShaderModel,
compiler: Dxc::IDxcCompiler3,
// Has to be held onto for the lifetime of the device otherwise shaders will fail to compile.
// Only needed when using dynamic linking.
_dxc: DxcLib,
}
pub(super) struct CompilerStaticDxc {
max_shader_model: wgt::DxcShaderModel,
compiler: Dxc::IDxcCompiler3,
}
#[derive(Debug, Error)]
pub(super) enum GetContainerError {
#[error(transparent)]
Device(#[from] crate::DeviceError),
#[error("Failed to load {0}: {1}")]
FailedToLoad(&'static str, libloading::Error),
}
impl CompilerContainer {
pub(super) fn new_fxc() -> Result<Self, GetContainerError> {
FxcLib::new_dynamic().map(|fxc| Self::Fxc(CompilerFxc { fxc }))
}
pub(super) fn new_dynamic_dxc(dxc_path: PathBuf) -> Result<Self, GetContainerError> {
let dxc = DxcLib::new_dynamic(dxc_path)
.map_err(|e| GetContainerError::FailedToLoad("dxcompiler.dll", e))?;
let compiler = dxc.create_instance::<Dxc::IDxcCompiler3>()?;
let (mut major, mut minor) = (1, 0);
// DXC 1.0 didn't support this. If the cast fails assume it is DXC 1.0.
if let Ok(version_info) = compiler.cast::<Dxc::IDxcVersionInfo>() {
unsafe {
version_info.GetVersion(&mut major, &mut minor).unwrap();
}
}
Ok(Self::DynamicDxc(CompilerDynamicDxc {
max_shader_model: wgt::DxcShaderModel::from_dxc_version(major, minor),
compiler,
_dxc: dxc,
}))
}
/// Creates a [`CompilerContainer`] that delegates to the statically-linked version of DXC.
pub(super) fn new_static_dxc() -> Result<CompilerContainer, crate::DeviceError> {
#[cfg(static_dxc)]
{
unsafe {
let compiler = dxc_create_instance::<Dxc::IDxcCompiler3>(|clsid, iid, ppv| {
windows_core::HRESULT(mach_dxcompiler_rs::DxcCreateInstance(
clsid.cast(),
iid.cast(),
ppv,
))
})?;
Ok(CompilerContainer::StaticDxc(CompilerStaticDxc {
max_shader_model: wgt::DxcShaderModel::V6_7,
compiler,
}))
}
}
#[cfg(not(static_dxc))]
{
panic!("Attempted to create a static DXC shader compiler, but the static-dxc feature was not enabled")
}
}
pub(super) fn max_shader_model(&self) -> Option<wgt::DxcShaderModel> {
match self {
CompilerContainer::Fxc(..) => None,
CompilerContainer::DynamicDxc(CompilerDynamicDxc {
max_shader_model, ..
})
| CompilerContainer::StaticDxc(CompilerStaticDxc {
max_shader_model, ..
}) => Some(max_shader_model.clone()),
}
}
pub(super) fn compile(
&self,
device: &super::Device,
source: &str,
source_name: Option<&CStr>,
raw_ep: &str,
stage_bit: wgt::ShaderStages,
full_stage: &str,
) -> Result<super::CompiledShader, crate::PipelineError> {
match self {
CompilerContainer::Fxc(CompilerFxc { fxc }) => compile_fxc(
device,
source,
source_name,
raw_ep,
stage_bit,
full_stage,
fxc,
),
CompilerContainer::DynamicDxc(CompilerDynamicDxc { compiler, .. })
| CompilerContainer::StaticDxc(CompilerStaticDxc { compiler, .. }) => compile_dxc(
device,
source,
source_name,
raw_ep,
stage_bit,
full_stage,
compiler,
),
}
}
}
type D3DCompileFn = unsafe extern "system" fn(
psrcdata: *const core::ffi::c_void,
srcdatasize: usize,
psourcename: PCSTR,
pdefines: *const D3D_SHADER_MACRO,
pinclude: *mut core::ffi::c_void,
pentrypoint: PCSTR,
ptarget: PCSTR,
flags1: u32,
flags2: u32,
ppcode: *mut *mut core::ffi::c_void,
pperrormsgs: *mut *mut core::ffi::c_void,
) -> windows_core::HRESULT;
#[derive(Debug)]
struct FxcLib {
// `d3dcompile_fn` points into `_lib`, so `_lib` must be held for as long
// as we want to keep compiling shaders with FXC.
_lib: crate::dx12::DynLib,
d3dcompile_fn: D3DCompileFn,
}
impl FxcLib {
const PATH: &str = "d3dcompiler_47.dll";
fn new_dynamic() -> Result<Self, GetContainerError> {
unsafe {
let lib = crate::dx12::DynLib::new(Self::PATH)
.map_err(|e| GetContainerError::FailedToLoad(FxcLib::PATH, e))?;
let d3dcompile_fn: D3DCompileFn = *lib.get::<D3DCompileFn>(c"D3DCompile".to_bytes())?;
Ok(Self {
_lib: lib,
d3dcompile_fn,
})
}
}
#[allow(clippy::too_many_arguments)]
fn compile(
&self,
source: &str,
source_name: Option<&CStr>,
raw_ep: &str,
full_stage: &str,
compile_flags: u32,
shader_data: &mut Option<ID3DBlob>,
error: &mut Option<ID3DBlob>,
) -> Result<windows_core::Result<()>, crate::DeviceError> {
unsafe {
let raw_ep = alloc::ffi::CString::new(raw_ep).unwrap();
let full_stage = alloc::ffi::CString::new(full_stage).unwrap();
// If no name has been set, D3DCompile wants the null pointer.
let source_name = source_name
.map(|cstr| cstr.as_ptr().cast())
.unwrap_or(core::ptr::null());
let shader_data: *mut Option<ID3DBlob> = shader_data;
let error: *mut Option<ID3DBlob> = error;
{
profiling::scope!("Fxc::D3DCompile");
Ok((self.d3dcompile_fn)(
source.as_ptr().cast(),
source.len(),
PCSTR(source_name),
core::ptr::null(),
core::ptr::null_mut(),
PCSTR(raw_ep.as_ptr().cast()),
PCSTR(full_stage.as_ptr().cast()),
compile_flags,
0,
shader_data.cast(),
error.cast(),
)
.ok())
}
}
}
}
fn compile_fxc(
device: &super::Device,
source: &str,
source_name: Option<&CStr>,
raw_ep: &str,
stage_bit: wgt::ShaderStages,
full_stage: &str,
fxc: &FxcLib,
) -> Result<super::CompiledShader, crate::PipelineError> {
profiling::scope!("compile_fxc");
let mut compile_flags = Fxc::D3DCOMPILE_ENABLE_STRICTNESS;
if device
.shared
.private_caps
.instance_flags
.contains(wgt::InstanceFlags::DEBUG)
{
compile_flags |= Fxc::D3DCOMPILE_DEBUG | Fxc::D3DCOMPILE_SKIP_OPTIMIZATION;
}
let mut shader_data = None;
let mut error = None;
let hr = fxc.compile(
source,
source_name,
raw_ep,
full_stage,
compile_flags,
&mut shader_data,
&mut error,
)?;
match hr {
Ok(()) => {
let shader_data = shader_data.unwrap();
Ok(super::CompiledShader::Fxc(shader_data))
}
Err(e) => {
let mut full_msg = format!("FXC D3DCompile error ({e})");
if let Some(error) = error {
use core::fmt::Write as _;
let message = unsafe {
core::slice::from_raw_parts(
error.GetBufferPointer().cast(),
error.GetBufferSize(),
)
};
let _ = write!(full_msg, ": {}", String::from_utf8_lossy(message));
}
Err(crate::PipelineError::Linkage(stage_bit, full_msg))
}
}
}
trait DxcObj: Interface {
const CLSID: windows::core::GUID;
}
impl DxcObj for Dxc::IDxcCompiler3 {
const CLSID: windows::core::GUID = Dxc::CLSID_DxcCompiler;
}
impl DxcObj for Dxc::IDxcUtils {
const CLSID: windows::core::GUID = Dxc::CLSID_DxcUtils;
}
impl DxcObj for Dxc::IDxcValidator {
const CLSID: windows::core::GUID = Dxc::CLSID_DxcValidator;
}
#[derive(Debug)]
struct DxcLib {
lib: crate::dx12::DynLib,
}
impl DxcLib {
fn new_dynamic(lib_path: PathBuf) -> Result<Self, libloading::Error> {
unsafe { crate::dx12::DynLib::new(lib_path).map(|lib| Self { lib }) }
}
pub fn create_instance<T: DxcObj>(&self) -> Result<T, crate::DeviceError> {
unsafe {
type DxcCreateInstanceFn = unsafe extern "system" fn(
rclsid: *const windows_core::GUID,
riid: *const windows_core::GUID,
ppv: *mut *mut core::ffi::c_void,
)
-> windows_core::HRESULT;
let func: libloading::Symbol<DxcCreateInstanceFn> =
self.lib.get(c"DxcCreateInstance".to_bytes())?;
dxc_create_instance::<T>(|clsid, iid, ppv| func(clsid, iid, ppv))
}
}
}
/// Invokes the provided library function to create a DXC object.
unsafe fn dxc_create_instance<T: DxcObj>(
f: impl Fn(
*const windows_core::GUID,
*const windows_core::GUID,
*mut *mut core::ffi::c_void,
) -> windows_core::HRESULT,
) -> Result<T, crate::DeviceError> {
let mut result__ = None;
f(&T::CLSID, &T::IID, <*mut _>::cast(&mut result__))
.ok()
.into_device_result("DxcCreateInstance")?;
result__.ok_or(crate::DeviceError::Unexpected)
}
/// Owned PCWSTR
#[allow(clippy::upper_case_acronyms)]
struct OPCWSTR {
inner: Vec<u16>,
}
impl OPCWSTR {
fn new(s: &str) -> Self {
let mut inner: Vec<_> = s.encode_utf16().collect();
inner.push(0);
Self { inner }
}
fn ptr(&self) -> PCWSTR {
PCWSTR(self.inner.as_ptr())
}
}
fn get_output<T: Interface>(
res: &Dxc::IDxcResult,
kind: Dxc::DXC_OUT_KIND,
) -> Result<T, crate::DeviceError> {
let mut result__: Option<T> = None;
unsafe { res.GetOutput::<T>(kind, &mut None, <*mut _>::cast(&mut result__)) }
.into_device_result("GetOutput")?;
result__.ok_or(crate::DeviceError::Unexpected)
}
fn as_err_str(blob: &Dxc::IDxcBlobUtf8) -> Result<&str, crate::DeviceError> {
let ptr = unsafe { blob.GetStringPointer() };
let len = unsafe { blob.GetStringLength() };
core::str::from_utf8(unsafe { core::slice::from_raw_parts(ptr.0, len) })
.map_err(|_| crate::DeviceError::Unexpected)
}
fn compile_dxc(
device: &crate::dx12::Device,
source: &str,
source_name: Option<&CStr>,
raw_ep: &str,
stage_bit: wgt::ShaderStages,
full_stage: &str,
compiler: &Dxc::IDxcCompiler3,
) -> Result<crate::dx12::CompiledShader, crate::PipelineError> {
profiling::scope!("compile_dxc");
let source_name = source_name.and_then(|cstr| cstr.to_str().ok());
let source_name = source_name.map(OPCWSTR::new);
let raw_ep = OPCWSTR::new(raw_ep);
let full_stage = OPCWSTR::new(full_stage);
let mut compile_args = arrayvec::ArrayVec::<PCWSTR, 13>::new_const();
if let Some(source_name) = source_name.as_ref() {
compile_args.push(source_name.ptr())
}
compile_args.extend([
windows::core::w!("-E"),
raw_ep.ptr(),
windows::core::w!("-T"),
full_stage.ptr(),
windows::core::w!("-HV"),
windows::core::w!("2018"), // Use HLSL 2018, Naga doesn't supported 2021 yet.
windows::core::w!("-no-warnings"),
Dxc::DXC_ARG_ENABLE_STRICTNESS,
]);
if device
.shared
.private_caps
.instance_flags
.contains(wgt::InstanceFlags::DEBUG)
&& !device
.shared
.private_caps
.workarounds
.avoid_shader_debug_info
{
compile_args.push(Dxc::DXC_ARG_DEBUG);
compile_args.push(Dxc::DXC_ARG_SKIP_OPTIMIZATIONS);
}
if device
.features
.intersects(wgt::Features::SHADER_F16 | wgt::Features::SHADER_I16)
{
compile_args.push(windows::core::w!("-enable-16bit-types"));
}
let buffer = Dxc::DxcBuffer {
Ptr: source.as_ptr().cast(),
Size: source.len(),
Encoding: Dxc::DXC_CP_UTF8.0,
};
let compile_res: Dxc::IDxcResult =
unsafe { compiler.Compile(&buffer, Some(&compile_args), None) }
.into_device_result("Compile")?;
drop(compile_args);
drop(source_name);
drop(raw_ep);
drop(full_stage);
let err_blob = get_output::<Dxc::IDxcBlobUtf8>(&compile_res, Dxc::DXC_OUT_ERRORS)?;
let len = unsafe { err_blob.GetStringLength() };
if len != 0 {
let err = as_err_str(&err_blob)?;
return Err(crate::PipelineError::Linkage(
stage_bit,
format!("DXC compile error: {err}"),
));
}
let blob = get_output::<Dxc::IDxcBlob>(&compile_res, Dxc::DXC_OUT_OBJECT)?;
Ok(crate::dx12::CompiledShader::Dxc(blob))
}
+603
View File
@@ -0,0 +1,603 @@
use alloc::sync::Arc;
use gpu_allocator::{d3d12::AllocationCreateDesc, MemoryLocation};
use parking_lot::Mutex;
use windows::Win32::Graphics::{Direct3D12, Dxgi};
use crate::{
auxil::dxgi::{name::ObjectExt as _, result::HResult as _},
dx12::conv,
AllocationSizes,
};
#[derive(Debug)]
pub(crate) enum AllocationType {
Buffer,
Texture,
AccelerationStructure,
}
#[derive(Debug)]
enum AllocationInner {
/// This resource is suballocated from a heap.
Placed {
inner: gpu_allocator::d3d12::Allocation,
},
/// This resource is a committed resource and does not belong to a
/// suballocated heap. We store an approximate size, so we can manage our counters
/// correctly.
///
/// This is only used for Intel Xe drivers, which have a bug that
/// prevents suballocation from working correctly.
Committed { size: u64 },
}
#[derive(Debug)]
pub(crate) struct Allocation {
inner: AllocationInner,
ty: AllocationType,
}
impl Allocation {
pub fn placed(inner: gpu_allocator::d3d12::Allocation, ty: AllocationType) -> Self {
Self {
inner: AllocationInner::Placed { inner },
ty,
}
}
pub fn none(ty: AllocationType, size: u64) -> Self {
Self {
inner: AllocationInner::Committed { size },
ty,
}
}
pub fn size(&self) -> u64 {
match self.inner {
AllocationInner::Placed { ref inner } => inner.size(),
AllocationInner::Committed { size } => size,
}
}
}
#[derive(Clone)]
pub(crate) struct Allocator {
inner: Arc<Mutex<gpu_allocator::d3d12::Allocator>>,
device_memblock_size: u64,
host_memblock_size: u64,
pub memory_budget_thresholds: wgt::MemoryBudgetThresholds,
}
impl Allocator {
pub(crate) fn new(
raw: &Direct3D12::ID3D12Device,
memory_hints: &wgt::MemoryHints,
memory_budget_thresholds: wgt::MemoryBudgetThresholds,
) -> Result<Self, crate::DeviceError> {
let allocation_sizes = AllocationSizes::from_memory_hints(memory_hints);
let device_memblock_size = allocation_sizes.min_device_memblock_size;
let host_memblock_size = allocation_sizes.min_host_memblock_size;
let allocator_desc = gpu_allocator::d3d12::AllocatorCreateDesc {
device: gpu_allocator::d3d12::ID3D12DeviceVersion::Device(raw.clone()),
debug_settings: Default::default(),
allocation_sizes: allocation_sizes.into(),
};
let allocator = gpu_allocator::d3d12::Allocator::new(&allocator_desc).inspect_err(|e| {
log::error!("Failed to create d3d12 allocator, error: {e}");
})?;
Ok(Self {
inner: Arc::new(Mutex::new(allocator)),
device_memblock_size,
host_memblock_size,
memory_budget_thresholds,
})
}
pub(crate) fn generate_report(&self) -> wgt::AllocatorReport {
let mut upstream = self.inner.lock().generate_report();
let allocations = upstream
.allocations
.iter_mut()
.map(|alloc| wgt::AllocationReport {
name: core::mem::take(&mut alloc.name),
offset: alloc.offset,
size: alloc.size,
})
.collect();
let blocks = upstream
.blocks
.iter()
.map(|block| wgt::MemoryBlockReport {
size: block.size,
allocations: block.allocations.clone(),
})
.collect();
wgt::AllocatorReport {
allocations,
blocks,
total_allocated_bytes: upstream.total_allocated_bytes,
total_reserved_bytes: upstream.total_capacity_bytes,
}
}
}
/// To allow us to construct buffers from both a `Device` and `CommandEncoder`
/// without needing each function to take a million arguments, we create a
/// borrowed context struct that contains the relevant members.
pub(crate) struct DeviceAllocationContext<'a> {
pub(crate) raw: &'a Direct3D12::ID3D12Device,
pub(crate) shared: &'a super::DeviceShared,
pub(crate) mem_allocator: &'a Allocator,
pub(crate) counters: &'a wgt::HalCounters,
}
impl<'a> From<&'a super::Device> for DeviceAllocationContext<'a> {
fn from(device: &'a super::Device) -> Self {
Self {
raw: &device.raw,
shared: &device.shared,
mem_allocator: &device.mem_allocator,
counters: &device.counters,
}
}
}
impl<'a> From<&'a super::CommandEncoder> for DeviceAllocationContext<'a> {
fn from(encoder: &'a super::CommandEncoder) -> Self {
Self {
raw: &encoder.device,
shared: &encoder.shared,
mem_allocator: &encoder.mem_allocator,
counters: &encoder.counters,
}
}
}
impl<'a> DeviceAllocationContext<'a> {
///////////////////////
// Resource Creation //
///////////////////////
pub(crate) fn create_buffer(
&self,
desc: &crate::BufferDescriptor,
) -> Result<(Direct3D12::ID3D12Resource, Allocation), crate::DeviceError> {
let is_cpu_read = desc.usage.contains(wgt::BufferUses::MAP_READ);
let is_cpu_write = desc.usage.contains(wgt::BufferUses::MAP_WRITE);
let location = match (is_cpu_read, is_cpu_write) {
(true, true) => MemoryLocation::CpuToGpu,
(true, false) => MemoryLocation::GpuToCpu,
(false, true) => MemoryLocation::CpuToGpu,
(false, false) => MemoryLocation::GpuOnly,
};
let raw_desc = conv::map_buffer_descriptor(desc);
let allocation_info =
self.error_if_would_oom_on_resource_allocation(&raw_desc, location)?;
let (resource, allocation) = if self.shared.private_caps.suballocation_supported {
self.create_placed_buffer(desc, raw_desc, allocation_info, location)?
} else {
self.create_committed_buffer(raw_desc, location)?
};
if let Some(label) = desc.label {
resource.set_name(label)?;
}
self.counters.buffer_memory.add(allocation.size() as isize);
Ok((resource, allocation))
}
pub(crate) fn create_texture(
&self,
desc: &crate::TextureDescriptor,
raw_desc: Direct3D12::D3D12_RESOURCE_DESC,
) -> Result<(Direct3D12::ID3D12Resource, Allocation), crate::DeviceError> {
let location = MemoryLocation::GpuOnly;
let allocation_info =
self.error_if_would_oom_on_resource_allocation(&raw_desc, location)?;
let (resource, allocation) = if self.shared.private_caps.suballocation_supported {
self.create_placed_texture(desc, raw_desc, allocation_info, location)?
} else {
self.create_committed_texture(desc, raw_desc)?
};
if let Some(label) = desc.label {
resource.set_name(label)?;
}
self.counters.texture_memory.add(allocation.size() as isize);
Ok((resource, allocation))
}
pub(crate) fn create_acceleration_structure(
&self,
desc: &crate::AccelerationStructureDescriptor,
raw_desc: Direct3D12::D3D12_RESOURCE_DESC,
) -> Result<(Direct3D12::ID3D12Resource, Allocation), crate::DeviceError> {
let location = MemoryLocation::GpuOnly;
let allocation_info =
self.error_if_would_oom_on_resource_allocation(&raw_desc, location)?;
let (resource, allocation) = if self.shared.private_caps.suballocation_supported {
self.create_placed_acceleration_structure(desc, raw_desc, allocation_info, location)?
} else {
self.create_committed_acceleration_structure(desc, raw_desc)?
};
if let Some(label) = desc.label {
resource.set_name(label)?;
}
self.counters
.acceleration_structure_memory
.add(allocation.size() as isize);
Ok((resource, allocation))
}
//////////////////////////
// Resource Destruction //
//////////////////////////
pub(crate) fn free_resource(
&self,
resource: Direct3D12::ID3D12Resource,
allocation: Allocation,
) {
// Make sure the resource is released before we free the allocation.
drop(resource);
let counter = match allocation.ty {
AllocationType::Buffer => &self.counters.buffer_memory,
AllocationType::Texture => &self.counters.texture_memory,
AllocationType::AccelerationStructure => &self.counters.acceleration_structure_memory,
};
counter.sub(allocation.size() as isize);
if let AllocationInner::Placed { inner } = allocation.inner {
match self.mem_allocator.inner.lock().free(inner) {
Ok(_) => (),
// TODO: Don't panic here
Err(e) => panic!("Failed to destroy dx12 {:?}, {e}", allocation.ty),
};
}
}
///////////////////////////////
// Placed Resource Creation ///
///////////////////////////////
fn create_placed_buffer(
&self,
desc: &crate::BufferDescriptor<'_>,
raw_desc: Direct3D12::D3D12_RESOURCE_DESC,
allocation_info: Direct3D12::D3D12_RESOURCE_ALLOCATION_INFO,
location: MemoryLocation,
) -> Result<(Direct3D12::ID3D12Resource, Allocation), crate::DeviceError> {
let name = desc.label.unwrap_or("Unlabeled buffer");
let mut allocator = self.mem_allocator.inner.lock();
let allocation_desc = AllocationCreateDesc {
name,
location,
size: allocation_info.SizeInBytes,
alignment: allocation_info.Alignment,
resource_category: gpu_allocator::d3d12::ResourceCategory::from(&raw_desc),
};
let allocation = allocator.allocate(&allocation_desc)?;
let mut resource = None;
unsafe {
self.raw.CreatePlacedResource(
allocation.heap(),
allocation.offset(),
&raw_desc,
Direct3D12::D3D12_RESOURCE_STATE_COMMON,
None,
&mut resource,
)
}
.into_device_result("Placed buffer creation")?;
let resource = resource.ok_or(crate::DeviceError::Unexpected)?;
let wrapped_allocation = Allocation::placed(allocation, AllocationType::Buffer);
Ok((resource, wrapped_allocation))
}
fn create_placed_texture(
&self,
desc: &crate::TextureDescriptor<'_>,
raw_desc: Direct3D12::D3D12_RESOURCE_DESC,
allocation_info: Direct3D12::D3D12_RESOURCE_ALLOCATION_INFO,
location: MemoryLocation,
) -> Result<(Direct3D12::ID3D12Resource, Allocation), crate::DeviceError> {
let name = desc.label.unwrap_or("Unlabeled texture");
let mut allocator = self.mem_allocator.inner.lock();
let allocation_desc = AllocationCreateDesc {
name,
location,
size: allocation_info.SizeInBytes,
alignment: allocation_info.Alignment,
resource_category: gpu_allocator::d3d12::ResourceCategory::from(&raw_desc),
};
let allocation = allocator.allocate(&allocation_desc)?;
let mut resource = None;
unsafe {
self.raw.CreatePlacedResource(
allocation.heap(),
allocation.offset(),
&raw_desc,
Direct3D12::D3D12_RESOURCE_STATE_COMMON,
None, // clear value
&mut resource,
)
}
.into_device_result("Placed texture creation")?;
let resource = resource.ok_or(crate::DeviceError::Unexpected)?;
let wrapped_allocation = Allocation::placed(allocation, AllocationType::Texture);
Ok((resource, wrapped_allocation))
}
fn create_placed_acceleration_structure(
&self,
desc: &crate::AccelerationStructureDescriptor<'_>,
raw_desc: Direct3D12::D3D12_RESOURCE_DESC,
allocation_info: Direct3D12::D3D12_RESOURCE_ALLOCATION_INFO,
location: MemoryLocation,
) -> Result<(Direct3D12::ID3D12Resource, Allocation), crate::DeviceError> {
let name = desc.label.unwrap_or("Unlabeled acceleration structure");
let mut allocator = self.mem_allocator.inner.lock();
let allocation_desc = AllocationCreateDesc {
name,
location,
size: allocation_info.SizeInBytes,
alignment: allocation_info.Alignment,
resource_category: gpu_allocator::d3d12::ResourceCategory::from(&raw_desc),
};
let allocation = allocator.allocate(&allocation_desc)?;
let mut resource = None;
unsafe {
self.raw.CreatePlacedResource(
allocation.heap(),
allocation.offset(),
&raw_desc,
Direct3D12::D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE,
None,
&mut resource,
)
}
.into_device_result("Placed acceleration structure creation")?;
let resource = resource.ok_or(crate::DeviceError::Unexpected)?;
let wrapped_allocation =
Allocation::placed(allocation, AllocationType::AccelerationStructure);
Ok((resource, wrapped_allocation))
}
/////////////////////////////////
// Committed Resource Creation //
/////////////////////////////////
fn create_committed_buffer(
&self,
raw_desc: Direct3D12::D3D12_RESOURCE_DESC,
location: MemoryLocation,
) -> Result<(Direct3D12::ID3D12Resource, Allocation), crate::DeviceError> {
let is_uma = matches!(
self.shared.private_caps.memory_architecture,
crate::dx12::MemoryArchitecture::Unified { .. }
);
let heap_properties = Direct3D12::D3D12_HEAP_PROPERTIES {
Type: Direct3D12::D3D12_HEAP_TYPE_CUSTOM,
CPUPageProperty: match location {
MemoryLocation::GpuOnly => Direct3D12::D3D12_CPU_PAGE_PROPERTY_NOT_AVAILABLE,
MemoryLocation::CpuToGpu => Direct3D12::D3D12_CPU_PAGE_PROPERTY_WRITE_COMBINE,
MemoryLocation::GpuToCpu => Direct3D12::D3D12_CPU_PAGE_PROPERTY_WRITE_BACK,
_ => unreachable!(),
},
MemoryPoolPreference: match (is_uma, location) {
// On dedicated GPUs, we only use L1 for GPU-only allocations.
(false, MemoryLocation::GpuOnly) => Direct3D12::D3D12_MEMORY_POOL_L1,
(_, _) => Direct3D12::D3D12_MEMORY_POOL_L0,
},
CreationNodeMask: 0,
VisibleNodeMask: 0,
};
let mut resource = None;
unsafe {
self.raw.CreateCommittedResource(
&heap_properties,
if self.shared.private_caps.heap_create_not_zeroed {
Direct3D12::D3D12_HEAP_FLAG_CREATE_NOT_ZEROED
} else {
Direct3D12::D3D12_HEAP_FLAG_NONE
},
&raw_desc,
Direct3D12::D3D12_RESOURCE_STATE_COMMON,
None,
&mut resource,
)
}
.into_device_result("Committed buffer creation")?;
let resource = resource.ok_or(crate::DeviceError::Unexpected)?;
let wrapped_allocation = Allocation::none(AllocationType::Buffer, raw_desc.Width);
Ok((resource, wrapped_allocation))
}
fn create_committed_texture(
&self,
desc: &crate::TextureDescriptor,
raw_desc: Direct3D12::D3D12_RESOURCE_DESC,
) -> Result<(Direct3D12::ID3D12Resource, Allocation), crate::DeviceError> {
let heap_properties = Direct3D12::D3D12_HEAP_PROPERTIES {
Type: Direct3D12::D3D12_HEAP_TYPE_CUSTOM,
CPUPageProperty: Direct3D12::D3D12_CPU_PAGE_PROPERTY_NOT_AVAILABLE,
MemoryPoolPreference: match self.shared.private_caps.memory_architecture {
crate::dx12::MemoryArchitecture::NonUnified => Direct3D12::D3D12_MEMORY_POOL_L1,
crate::dx12::MemoryArchitecture::Unified { .. } => Direct3D12::D3D12_MEMORY_POOL_L0,
},
CreationNodeMask: 0,
VisibleNodeMask: 0,
};
let mut resource = None;
unsafe {
self.raw.CreateCommittedResource(
&heap_properties,
if self.shared.private_caps.heap_create_not_zeroed {
Direct3D12::D3D12_HEAP_FLAG_CREATE_NOT_ZEROED
} else {
Direct3D12::D3D12_HEAP_FLAG_NONE
},
&raw_desc,
Direct3D12::D3D12_RESOURCE_STATE_COMMON,
None, // clear value
&mut resource,
)
}
.into_device_result("Committed texture creation")?;
let resource = resource.ok_or(crate::DeviceError::Unexpected)?;
let wrapped_allocation = Allocation::none(
AllocationType::Texture,
desc.format.theoretical_memory_footprint(desc.size),
);
Ok((resource, wrapped_allocation))
}
fn create_committed_acceleration_structure(
&self,
desc: &crate::AccelerationStructureDescriptor,
raw_desc: Direct3D12::D3D12_RESOURCE_DESC,
) -> Result<(Direct3D12::ID3D12Resource, Allocation), crate::DeviceError> {
let heap_properties = Direct3D12::D3D12_HEAP_PROPERTIES {
Type: Direct3D12::D3D12_HEAP_TYPE_CUSTOM,
CPUPageProperty: Direct3D12::D3D12_CPU_PAGE_PROPERTY_NOT_AVAILABLE,
MemoryPoolPreference: match self.shared.private_caps.memory_architecture {
crate::dx12::MemoryArchitecture::NonUnified => Direct3D12::D3D12_MEMORY_POOL_L1,
crate::dx12::MemoryArchitecture::Unified { .. } => Direct3D12::D3D12_MEMORY_POOL_L0,
},
CreationNodeMask: 0,
VisibleNodeMask: 0,
};
let mut resource = None;
unsafe {
self.raw.CreateCommittedResource(
&heap_properties,
if self.shared.private_caps.heap_create_not_zeroed {
Direct3D12::D3D12_HEAP_FLAG_CREATE_NOT_ZEROED
} else {
Direct3D12::D3D12_HEAP_FLAG_NONE
},
&raw_desc,
Direct3D12::D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE,
None,
&mut resource,
)
}
.into_device_result("Committed acceleration structure creation")?;
let resource = resource.ok_or(crate::DeviceError::Unexpected)?;
let wrapped_allocation = Allocation::none(AllocationType::AccelerationStructure, desc.size);
Ok((resource, wrapped_allocation))
}
fn error_if_would_oom_on_resource_allocation(
&self,
desc: &Direct3D12::D3D12_RESOURCE_DESC,
location: MemoryLocation,
) -> Result<Direct3D12::D3D12_RESOURCE_ALLOCATION_INFO, crate::DeviceError> {
let allocation_info = unsafe {
self.raw
.GetResourceAllocationInfo(0, core::slice::from_ref(desc))
};
// Some versions of WARP return SizeInBytes == 0 for very large
// allocations. Proceeding to attempt to allocate a zero-sized resource
// will result in a device lost error, so it seems preferable to return
// an out of memory error now.
if allocation_info.SizeInBytes == 0 {
return Err(crate::DeviceError::OutOfMemory);
}
let Some(threshold) = self
.mem_allocator
.memory_budget_thresholds
.for_resource_creation
else {
return Ok(allocation_info);
};
let memory_segment_group = match location {
MemoryLocation::Unknown => unreachable!(),
MemoryLocation::GpuOnly => Dxgi::DXGI_MEMORY_SEGMENT_GROUP_LOCAL,
MemoryLocation::CpuToGpu | MemoryLocation::GpuToCpu => {
match self.shared.private_caps.memory_architecture {
super::MemoryArchitecture::Unified { .. } => {
Dxgi::DXGI_MEMORY_SEGMENT_GROUP_LOCAL
}
super::MemoryArchitecture::NonUnified => {
Dxgi::DXGI_MEMORY_SEGMENT_GROUP_NON_LOCAL
}
}
}
};
let info = self
.shared
.adapter
.query_video_memory_info(memory_segment_group)?;
let memblock_size = match location {
MemoryLocation::Unknown => unreachable!(),
MemoryLocation::GpuOnly => self.mem_allocator.device_memblock_size,
MemoryLocation::CpuToGpu | MemoryLocation::GpuToCpu => {
self.mem_allocator.host_memblock_size
}
};
if info
.CurrentUsage
.checked_add(allocation_info.SizeInBytes.max(memblock_size))
.is_none_or(|usage| usage >= info.Budget / 100 * threshold as u64)
{
return Err(crate::DeviceError::OutOfMemory);
}
Ok(allocation_info)
}
}

Some files were not shown because too many files have changed in this diff Show More